ioc4.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2005 Silicon Graphics, Inc. All Rights Reserved.
  7. */
  8. /* This file contains the master driver module for use by SGI IOC4 subdrivers.
  9. *
  10. * It allocates any resources shared between multiple subdevices, and
  11. * provides accessor functions (where needed) and the like for those
  12. * resources. It also provides a mechanism for the subdevice modules
  13. * to support loading and unloading.
  14. *
  15. * Non-shared resources (e.g. external interrupt A_INT_OUT register page
  16. * alias, serial port and UART registers) are handled by the subdevice
  17. * modules themselves.
  18. *
  19. * This is all necessary because IOC4 is not implemented as a multi-function
  20. * PCI device, but an amalgamation of disparate registers for several
  21. * types of device (ATA, serial, external interrupts). The normal
  22. * resource management in the kernel doesn't have quite the right interfaces
  23. * to handle this situation (e.g. multiple modules can't claim the same
  24. * PCI ID), thus this IOC4 master module.
  25. */
  26. #include <linux/errno.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/ioc4.h>
  30. #include <linux/mmtimer.h>
  31. #include <linux/rtc.h>
  32. #include <linux/mutex.h>
  33. #include <asm/sn/addrs.h>
  34. #include <asm/sn/clksupport.h>
  35. #include <asm/sn/shub_mmr.h>
  36. /***************
  37. * Definitions *
  38. ***************/
  39. /* Tweakable values */
  40. /* PCI bus speed detection/calibration */
  41. #define IOC4_CALIBRATE_COUNT 63 /* Calibration cycle period */
  42. #define IOC4_CALIBRATE_CYCLES 256 /* Average over this many cycles */
  43. #define IOC4_CALIBRATE_DISCARD 2 /* Discard first few cycles */
  44. #define IOC4_CALIBRATE_LOW_MHZ 25 /* Lower bound on bus speed sanity */
  45. #define IOC4_CALIBRATE_HIGH_MHZ 75 /* Upper bound on bus speed sanity */
  46. #define IOC4_CALIBRATE_DEFAULT_MHZ 66 /* Assumed if sanity check fails */
  47. /************************
  48. * Submodule management *
  49. ************************/
  50. static DEFINE_MUTEX(ioc4_mutex);
  51. static LIST_HEAD(ioc4_devices);
  52. static LIST_HEAD(ioc4_submodules);
  53. /* Register an IOC4 submodule */
  54. int
  55. ioc4_register_submodule(struct ioc4_submodule *is)
  56. {
  57. struct ioc4_driver_data *idd;
  58. mutex_lock(&ioc4_mutex);
  59. list_add(&is->is_list, &ioc4_submodules);
  60. /* Initialize submodule for each IOC4 */
  61. if (!is->is_probe)
  62. goto out;
  63. list_for_each_entry(idd, &ioc4_devices, idd_list) {
  64. if (is->is_probe(idd)) {
  65. printk(KERN_WARNING
  66. "%s: IOC4 submodule %s probe failed "
  67. "for pci_dev %s",
  68. __FUNCTION__, module_name(is->is_owner),
  69. pci_name(idd->idd_pdev));
  70. }
  71. }
  72. out:
  73. mutex_unlock(&ioc4_mutex);
  74. return 0;
  75. }
  76. /* Unregister an IOC4 submodule */
  77. void
  78. ioc4_unregister_submodule(struct ioc4_submodule *is)
  79. {
  80. struct ioc4_driver_data *idd;
  81. mutex_lock(&ioc4_mutex);
  82. list_del(&is->is_list);
  83. /* Remove submodule for each IOC4 */
  84. if (!is->is_remove)
  85. goto out;
  86. list_for_each_entry(idd, &ioc4_devices, idd_list) {
  87. if (is->is_remove(idd)) {
  88. printk(KERN_WARNING
  89. "%s: IOC4 submodule %s remove failed "
  90. "for pci_dev %s.\n",
  91. __FUNCTION__, module_name(is->is_owner),
  92. pci_name(idd->idd_pdev));
  93. }
  94. }
  95. out:
  96. mutex_unlock(&ioc4_mutex);
  97. }
  98. /*********************
  99. * Device management *
  100. *********************/
  101. #define IOC4_CALIBRATE_LOW_LIMIT \
  102. (1000*IOC4_EXTINT_COUNT_DIVISOR/IOC4_CALIBRATE_LOW_MHZ)
  103. #define IOC4_CALIBRATE_HIGH_LIMIT \
  104. (1000*IOC4_EXTINT_COUNT_DIVISOR/IOC4_CALIBRATE_HIGH_MHZ)
  105. #define IOC4_CALIBRATE_DEFAULT \
  106. (1000*IOC4_EXTINT_COUNT_DIVISOR/IOC4_CALIBRATE_DEFAULT_MHZ)
  107. #define IOC4_CALIBRATE_END \
  108. (IOC4_CALIBRATE_CYCLES + IOC4_CALIBRATE_DISCARD)
  109. #define IOC4_INT_OUT_MODE_TOGGLE 0x7 /* Toggle INT_OUT every COUNT+1 ticks */
  110. /* Determines external interrupt output clock period of the PCI bus an
  111. * IOC4 is attached to. This value can be used to determine the PCI
  112. * bus speed.
  113. *
  114. * IOC4 has a design feature that various internal timers are derived from
  115. * the PCI bus clock. This causes IOC4 device drivers to need to take the
  116. * bus speed into account when setting various register values (e.g. INT_OUT
  117. * register COUNT field, UART divisors, etc). Since this information is
  118. * needed by several subdrivers, it is determined by the main IOC4 driver,
  119. * even though the following code utilizes external interrupt registers
  120. * to perform the speed calculation.
  121. */
  122. static void
  123. ioc4_clock_calibrate(struct ioc4_driver_data *idd)
  124. {
  125. extern unsigned long sn_rtc_cycles_per_second;
  126. union ioc4_int_out int_out;
  127. union ioc4_gpcr gpcr;
  128. unsigned int state, last_state = 1;
  129. uint64_t start = 0, end, period;
  130. unsigned int count = 0;
  131. /* Enable output */
  132. gpcr.raw = 0;
  133. gpcr.fields.dir = IOC4_GPCR_DIR_0;
  134. gpcr.fields.int_out_en = 1;
  135. writel(gpcr.raw, &idd->idd_misc_regs->gpcr_s.raw);
  136. /* Reset to power-on state */
  137. writel(0, &idd->idd_misc_regs->int_out.raw);
  138. mmiowb();
  139. printk(KERN_INFO
  140. "%s: Calibrating PCI bus speed "
  141. "for pci_dev %s ... ", __FUNCTION__, pci_name(idd->idd_pdev));
  142. /* Set up square wave */
  143. int_out.raw = 0;
  144. int_out.fields.count = IOC4_CALIBRATE_COUNT;
  145. int_out.fields.mode = IOC4_INT_OUT_MODE_TOGGLE;
  146. int_out.fields.diag = 0;
  147. writel(int_out.raw, &idd->idd_misc_regs->int_out.raw);
  148. mmiowb();
  149. /* Check square wave period averaged over some number of cycles */
  150. do {
  151. int_out.raw = readl(&idd->idd_misc_regs->int_out.raw);
  152. state = int_out.fields.int_out;
  153. if (!last_state && state) {
  154. count++;
  155. if (count == IOC4_CALIBRATE_END) {
  156. end = rtc_time();
  157. break;
  158. } else if (count == IOC4_CALIBRATE_DISCARD)
  159. start = rtc_time();
  160. }
  161. last_state = state;
  162. } while (1);
  163. /* Calculation rearranged to preserve intermediate precision.
  164. * Logically:
  165. * 1. "end - start" gives us number of RTC cycles over all the
  166. * square wave cycles measured.
  167. * 2. Divide by number of square wave cycles to get number of
  168. * RTC cycles per square wave cycle.
  169. * 3. Divide by 2*(int_out.fields.count+1), which is the formula
  170. * by which the IOC4 generates the square wave, to get the
  171. * number of RTC cycles per IOC4 INT_OUT count.
  172. * 4. Divide by sn_rtc_cycles_per_second to get seconds per
  173. * count.
  174. * 5. Multiply by 1E9 to get nanoseconds per count.
  175. */
  176. period = ((end - start) * 1000000000) /
  177. (IOC4_CALIBRATE_CYCLES * 2 * (IOC4_CALIBRATE_COUNT + 1)
  178. * sn_rtc_cycles_per_second);
  179. /* Bounds check the result. */
  180. if (period > IOC4_CALIBRATE_LOW_LIMIT ||
  181. period < IOC4_CALIBRATE_HIGH_LIMIT) {
  182. printk("failed. Assuming PCI clock ticks are %d ns.\n",
  183. IOC4_CALIBRATE_DEFAULT / IOC4_EXTINT_COUNT_DIVISOR);
  184. period = IOC4_CALIBRATE_DEFAULT;
  185. } else {
  186. printk("succeeded. PCI clock ticks are %ld ns.\n",
  187. period / IOC4_EXTINT_COUNT_DIVISOR);
  188. }
  189. /* Remember results. We store the extint clock period rather
  190. * than the PCI clock period so that greater precision is
  191. * retained. Divide by IOC4_EXTINT_COUNT_DIVISOR to get
  192. * PCI clock period.
  193. */
  194. idd->count_period = period;
  195. }
  196. /* Adds a new instance of an IOC4 card */
  197. static int
  198. ioc4_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id)
  199. {
  200. struct ioc4_driver_data *idd;
  201. struct ioc4_submodule *is;
  202. uint32_t pcmd;
  203. int ret;
  204. /* Enable IOC4 and take ownership of it */
  205. if ((ret = pci_enable_device(pdev))) {
  206. printk(KERN_WARNING
  207. "%s: Failed to enable IOC4 device for pci_dev %s.\n",
  208. __FUNCTION__, pci_name(pdev));
  209. goto out;
  210. }
  211. pci_set_master(pdev);
  212. /* Set up per-IOC4 data */
  213. idd = kmalloc(sizeof(struct ioc4_driver_data), GFP_KERNEL);
  214. if (!idd) {
  215. printk(KERN_WARNING
  216. "%s: Failed to allocate IOC4 data for pci_dev %s.\n",
  217. __FUNCTION__, pci_name(pdev));
  218. ret = -ENODEV;
  219. goto out_idd;
  220. }
  221. idd->idd_pdev = pdev;
  222. idd->idd_pci_id = pci_id;
  223. /* Map IOC4 misc registers. These are shared between subdevices
  224. * so the main IOC4 module manages them.
  225. */
  226. idd->idd_bar0 = pci_resource_start(idd->idd_pdev, 0);
  227. if (!idd->idd_bar0) {
  228. printk(KERN_WARNING
  229. "%s: Unable to find IOC4 misc resource "
  230. "for pci_dev %s.\n",
  231. __FUNCTION__, pci_name(idd->idd_pdev));
  232. ret = -ENODEV;
  233. goto out_pci;
  234. }
  235. if (!request_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs),
  236. "ioc4_misc")) {
  237. printk(KERN_WARNING
  238. "%s: Unable to request IOC4 misc region "
  239. "for pci_dev %s.\n",
  240. __FUNCTION__, pci_name(idd->idd_pdev));
  241. ret = -ENODEV;
  242. goto out_pci;
  243. }
  244. idd->idd_misc_regs = ioremap(idd->idd_bar0,
  245. sizeof(struct ioc4_misc_regs));
  246. if (!idd->idd_misc_regs) {
  247. printk(KERN_WARNING
  248. "%s: Unable to remap IOC4 misc region "
  249. "for pci_dev %s.\n",
  250. __FUNCTION__, pci_name(idd->idd_pdev));
  251. ret = -ENODEV;
  252. goto out_misc_region;
  253. }
  254. /* Failsafe portion of per-IOC4 initialization */
  255. /* Initialize IOC4 */
  256. pci_read_config_dword(idd->idd_pdev, PCI_COMMAND, &pcmd);
  257. pci_write_config_dword(idd->idd_pdev, PCI_COMMAND,
  258. pcmd | PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
  259. /* Determine PCI clock */
  260. ioc4_clock_calibrate(idd);
  261. /* Disable/clear all interrupts. Need to do this here lest
  262. * one submodule request the shared IOC4 IRQ, but interrupt
  263. * is generated by a different subdevice.
  264. */
  265. /* Disable */
  266. writel(~0, &idd->idd_misc_regs->other_iec.raw);
  267. writel(~0, &idd->idd_misc_regs->sio_iec);
  268. /* Clear (i.e. acknowledge) */
  269. writel(~0, &idd->idd_misc_regs->other_ir.raw);
  270. writel(~0, &idd->idd_misc_regs->sio_ir);
  271. /* Track PCI-device specific data */
  272. idd->idd_serial_data = NULL;
  273. pci_set_drvdata(idd->idd_pdev, idd);
  274. mutex_lock(&ioc4_mutex);
  275. list_add(&idd->idd_list, &ioc4_devices);
  276. /* Add this IOC4 to all submodules */
  277. list_for_each_entry(is, &ioc4_submodules, is_list) {
  278. if (is->is_probe && is->is_probe(idd)) {
  279. printk(KERN_WARNING
  280. "%s: IOC4 submodule 0x%s probe failed "
  281. "for pci_dev %s.\n",
  282. __FUNCTION__, module_name(is->is_owner),
  283. pci_name(idd->idd_pdev));
  284. }
  285. }
  286. mutex_unlock(&ioc4_mutex);
  287. return 0;
  288. out_misc_region:
  289. release_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs));
  290. out_pci:
  291. kfree(idd);
  292. out_idd:
  293. pci_disable_device(pdev);
  294. out:
  295. return ret;
  296. }
  297. /* Removes a particular instance of an IOC4 card. */
  298. static void
  299. ioc4_remove(struct pci_dev *pdev)
  300. {
  301. struct ioc4_submodule *is;
  302. struct ioc4_driver_data *idd;
  303. idd = pci_get_drvdata(pdev);
  304. /* Remove this IOC4 from all submodules */
  305. mutex_lock(&ioc4_mutex);
  306. list_for_each_entry(is, &ioc4_submodules, is_list) {
  307. if (is->is_remove && is->is_remove(idd)) {
  308. printk(KERN_WARNING
  309. "%s: IOC4 submodule 0x%s remove failed "
  310. "for pci_dev %s.\n",
  311. __FUNCTION__, module_name(is->is_owner),
  312. pci_name(idd->idd_pdev));
  313. }
  314. }
  315. mutex_unlock(&ioc4_mutex);
  316. /* Release resources */
  317. iounmap(idd->idd_misc_regs);
  318. if (!idd->idd_bar0) {
  319. printk(KERN_WARNING
  320. "%s: Unable to get IOC4 misc mapping for pci_dev %s. "
  321. "Device removal may be incomplete.\n",
  322. __FUNCTION__, pci_name(idd->idd_pdev));
  323. }
  324. release_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs));
  325. /* Disable IOC4 and relinquish */
  326. pci_disable_device(pdev);
  327. /* Remove and free driver data */
  328. mutex_lock(&ioc4_mutex);
  329. list_del(&idd->idd_list);
  330. mutex_unlock(&ioc4_mutex);
  331. kfree(idd);
  332. }
  333. static struct pci_device_id ioc4_id_table[] = {
  334. {PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC4, PCI_ANY_ID,
  335. PCI_ANY_ID, 0x0b4000, 0xFFFFFF},
  336. {0}
  337. };
  338. static struct pci_driver __devinitdata ioc4_driver = {
  339. .name = "IOC4",
  340. .id_table = ioc4_id_table,
  341. .probe = ioc4_probe,
  342. .remove = ioc4_remove,
  343. };
  344. MODULE_DEVICE_TABLE(pci, ioc4_id_table);
  345. /*********************
  346. * Module management *
  347. *********************/
  348. /* Module load */
  349. static int __devinit
  350. ioc4_init(void)
  351. {
  352. return pci_register_driver(&ioc4_driver);
  353. }
  354. /* Module unload */
  355. static void __devexit
  356. ioc4_exit(void)
  357. {
  358. pci_unregister_driver(&ioc4_driver);
  359. }
  360. module_init(ioc4_init);
  361. module_exit(ioc4_exit);
  362. MODULE_AUTHOR("Brent Casavant - Silicon Graphics, Inc. <bcasavan@sgi.com>");
  363. MODULE_DESCRIPTION("PCI driver master module for SGI IOC4 Base-IO Card");
  364. MODULE_LICENSE("GPL");
  365. EXPORT_SYMBOL(ioc4_register_submodule);
  366. EXPORT_SYMBOL(ioc4_unregister_submodule);