ioc4.c 12 KB

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