ioc4.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. /* Set up square wave */
  140. int_out.raw = 0;
  141. int_out.fields.count = IOC4_CALIBRATE_COUNT;
  142. int_out.fields.mode = IOC4_INT_OUT_MODE_TOGGLE;
  143. int_out.fields.diag = 0;
  144. writel(int_out.raw, &idd->idd_misc_regs->int_out.raw);
  145. mmiowb();
  146. /* Check square wave period averaged over some number of cycles */
  147. do {
  148. int_out.raw = readl(&idd->idd_misc_regs->int_out.raw);
  149. state = int_out.fields.int_out;
  150. if (!last_state && state) {
  151. count++;
  152. if (count == IOC4_CALIBRATE_END) {
  153. end = rtc_time();
  154. break;
  155. } else if (count == IOC4_CALIBRATE_DISCARD)
  156. start = rtc_time();
  157. }
  158. last_state = state;
  159. } while (1);
  160. /* Calculation rearranged to preserve intermediate precision.
  161. * Logically:
  162. * 1. "end - start" gives us number of RTC cycles over all the
  163. * square wave cycles measured.
  164. * 2. Divide by number of square wave cycles to get number of
  165. * RTC cycles per square wave cycle.
  166. * 3. Divide by 2*(int_out.fields.count+1), which is the formula
  167. * by which the IOC4 generates the square wave, to get the
  168. * number of RTC cycles per IOC4 INT_OUT count.
  169. * 4. Divide by sn_rtc_cycles_per_second to get seconds per
  170. * count.
  171. * 5. Multiply by 1E9 to get nanoseconds per count.
  172. */
  173. period = ((end - start) * 1000000000) /
  174. (IOC4_CALIBRATE_CYCLES * 2 * (IOC4_CALIBRATE_COUNT + 1)
  175. * sn_rtc_cycles_per_second);
  176. /* Bounds check the result. */
  177. if (period > IOC4_CALIBRATE_LOW_LIMIT ||
  178. period < IOC4_CALIBRATE_HIGH_LIMIT) {
  179. printk(KERN_INFO
  180. "IOC4 %s: Clock calibration failed. Assuming"
  181. "PCI clock is %d ns.\n",
  182. pci_name(idd->idd_pdev),
  183. IOC4_CALIBRATE_DEFAULT / IOC4_EXTINT_COUNT_DIVISOR);
  184. period = IOC4_CALIBRATE_DEFAULT;
  185. } else {
  186. printk(KERN_DEBUG
  187. "IOC4 %s: PCI clock is %ld ns.\n",
  188. pci_name(idd->idd_pdev),
  189. period / IOC4_EXTINT_COUNT_DIVISOR);
  190. }
  191. /* Remember results. We store the extint clock period rather
  192. * than the PCI clock period so that greater precision is
  193. * retained. Divide by IOC4_EXTINT_COUNT_DIVISOR to get
  194. * PCI clock period.
  195. */
  196. idd->count_period = period;
  197. }
  198. /* There are three variants of IOC4 cards: IO9, IO10, and PCI-RT.
  199. * Each brings out different combinations of IOC4 signals, thus.
  200. * the IOC4 subdrivers need to know to which we're attached.
  201. *
  202. * We look for the presence of a SCSI (IO9) or SATA (IO10) controller
  203. * on the same PCI bus at slot number 3 to differentiate IO9 from IO10.
  204. * If neither is present, it's a PCI-RT.
  205. */
  206. static unsigned int
  207. ioc4_variant(struct ioc4_driver_data *idd)
  208. {
  209. struct pci_dev *pdev = NULL;
  210. int found = 0;
  211. /* IO9: Look for a QLogic ISP 12160 at the same bus and slot 3. */
  212. do {
  213. pdev = pci_get_device(PCI_VENDOR_ID_QLOGIC,
  214. PCI_DEVICE_ID_QLOGIC_ISP12160, pdev);
  215. if (pdev &&
  216. idd->idd_pdev->bus->number == pdev->bus->number &&
  217. 3 == PCI_SLOT(pdev->devfn))
  218. found = 1;
  219. pci_dev_put(pdev);
  220. } while (pdev && !found);
  221. if (NULL != pdev)
  222. return IOC4_VARIANT_IO9;
  223. /* IO10: Look for a Vitesse VSC 7174 at the same bus and slot 3. */
  224. pdev = NULL;
  225. do {
  226. pdev = pci_get_device(PCI_VENDOR_ID_VITESSE,
  227. PCI_DEVICE_ID_VITESSE_VSC7174, pdev);
  228. if (pdev &&
  229. idd->idd_pdev->bus->number == pdev->bus->number &&
  230. 3 == PCI_SLOT(pdev->devfn))
  231. found = 1;
  232. pci_dev_put(pdev);
  233. } while (pdev && !found);
  234. if (NULL != pdev)
  235. return IOC4_VARIANT_IO10;
  236. /* PCI-RT: No SCSI/SATA controller will be present */
  237. return IOC4_VARIANT_PCI_RT;
  238. }
  239. /* Adds a new instance of an IOC4 card */
  240. static int
  241. ioc4_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id)
  242. {
  243. struct ioc4_driver_data *idd;
  244. struct ioc4_submodule *is;
  245. uint32_t pcmd;
  246. int ret;
  247. /* Enable IOC4 and take ownership of it */
  248. if ((ret = pci_enable_device(pdev))) {
  249. printk(KERN_WARNING
  250. "%s: Failed to enable IOC4 device for pci_dev %s.\n",
  251. __FUNCTION__, pci_name(pdev));
  252. goto out;
  253. }
  254. pci_set_master(pdev);
  255. /* Set up per-IOC4 data */
  256. idd = kmalloc(sizeof(struct ioc4_driver_data), GFP_KERNEL);
  257. if (!idd) {
  258. printk(KERN_WARNING
  259. "%s: Failed to allocate IOC4 data for pci_dev %s.\n",
  260. __FUNCTION__, pci_name(pdev));
  261. ret = -ENODEV;
  262. goto out_idd;
  263. }
  264. idd->idd_pdev = pdev;
  265. idd->idd_pci_id = pci_id;
  266. /* Map IOC4 misc registers. These are shared between subdevices
  267. * so the main IOC4 module manages them.
  268. */
  269. idd->idd_bar0 = pci_resource_start(idd->idd_pdev, 0);
  270. if (!idd->idd_bar0) {
  271. printk(KERN_WARNING
  272. "%s: Unable to find IOC4 misc resource "
  273. "for pci_dev %s.\n",
  274. __FUNCTION__, pci_name(idd->idd_pdev));
  275. ret = -ENODEV;
  276. goto out_pci;
  277. }
  278. if (!request_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs),
  279. "ioc4_misc")) {
  280. printk(KERN_WARNING
  281. "%s: Unable to request IOC4 misc region "
  282. "for pci_dev %s.\n",
  283. __FUNCTION__, pci_name(idd->idd_pdev));
  284. ret = -ENODEV;
  285. goto out_pci;
  286. }
  287. idd->idd_misc_regs = ioremap(idd->idd_bar0,
  288. sizeof(struct ioc4_misc_regs));
  289. if (!idd->idd_misc_regs) {
  290. printk(KERN_WARNING
  291. "%s: Unable to remap IOC4 misc region "
  292. "for pci_dev %s.\n",
  293. __FUNCTION__, pci_name(idd->idd_pdev));
  294. ret = -ENODEV;
  295. goto out_misc_region;
  296. }
  297. /* Failsafe portion of per-IOC4 initialization */
  298. /* Detect card variant */
  299. idd->idd_variant = ioc4_variant(idd);
  300. printk(KERN_INFO "IOC4 %s: %s card detected.\n", pci_name(pdev),
  301. idd->idd_variant == IOC4_VARIANT_IO9 ? "IO9" :
  302. idd->idd_variant == IOC4_VARIANT_PCI_RT ? "PCI-RT" :
  303. idd->idd_variant == IOC4_VARIANT_IO10 ? "IO10" : "unknown");
  304. /* Initialize IOC4 */
  305. pci_read_config_dword(idd->idd_pdev, PCI_COMMAND, &pcmd);
  306. pci_write_config_dword(idd->idd_pdev, PCI_COMMAND,
  307. pcmd | PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
  308. /* Determine PCI clock */
  309. ioc4_clock_calibrate(idd);
  310. /* Disable/clear all interrupts. Need to do this here lest
  311. * one submodule request the shared IOC4 IRQ, but interrupt
  312. * is generated by a different subdevice.
  313. */
  314. /* Disable */
  315. writel(~0, &idd->idd_misc_regs->other_iec.raw);
  316. writel(~0, &idd->idd_misc_regs->sio_iec);
  317. /* Clear (i.e. acknowledge) */
  318. writel(~0, &idd->idd_misc_regs->other_ir.raw);
  319. writel(~0, &idd->idd_misc_regs->sio_ir);
  320. /* Track PCI-device specific data */
  321. idd->idd_serial_data = NULL;
  322. pci_set_drvdata(idd->idd_pdev, idd);
  323. mutex_lock(&ioc4_mutex);
  324. list_add_tail(&idd->idd_list, &ioc4_devices);
  325. /* Add this IOC4 to all submodules */
  326. list_for_each_entry(is, &ioc4_submodules, is_list) {
  327. if (is->is_probe && is->is_probe(idd)) {
  328. printk(KERN_WARNING
  329. "%s: IOC4 submodule 0x%s probe failed "
  330. "for pci_dev %s.\n",
  331. __FUNCTION__, module_name(is->is_owner),
  332. pci_name(idd->idd_pdev));
  333. }
  334. }
  335. mutex_unlock(&ioc4_mutex);
  336. return 0;
  337. out_misc_region:
  338. release_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs));
  339. out_pci:
  340. kfree(idd);
  341. out_idd:
  342. pci_disable_device(pdev);
  343. out:
  344. return ret;
  345. }
  346. /* Removes a particular instance of an IOC4 card. */
  347. static void
  348. ioc4_remove(struct pci_dev *pdev)
  349. {
  350. struct ioc4_submodule *is;
  351. struct ioc4_driver_data *idd;
  352. idd = pci_get_drvdata(pdev);
  353. /* Remove this IOC4 from all submodules */
  354. mutex_lock(&ioc4_mutex);
  355. list_for_each_entry(is, &ioc4_submodules, is_list) {
  356. if (is->is_remove && is->is_remove(idd)) {
  357. printk(KERN_WARNING
  358. "%s: IOC4 submodule 0x%s remove failed "
  359. "for pci_dev %s.\n",
  360. __FUNCTION__, module_name(is->is_owner),
  361. pci_name(idd->idd_pdev));
  362. }
  363. }
  364. mutex_unlock(&ioc4_mutex);
  365. /* Release resources */
  366. iounmap(idd->idd_misc_regs);
  367. if (!idd->idd_bar0) {
  368. printk(KERN_WARNING
  369. "%s: Unable to get IOC4 misc mapping for pci_dev %s. "
  370. "Device removal may be incomplete.\n",
  371. __FUNCTION__, pci_name(idd->idd_pdev));
  372. }
  373. release_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs));
  374. /* Disable IOC4 and relinquish */
  375. pci_disable_device(pdev);
  376. /* Remove and free driver data */
  377. mutex_lock(&ioc4_mutex);
  378. list_del(&idd->idd_list);
  379. mutex_unlock(&ioc4_mutex);
  380. kfree(idd);
  381. }
  382. static struct pci_device_id ioc4_id_table[] = {
  383. {PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC4, PCI_ANY_ID,
  384. PCI_ANY_ID, 0x0b4000, 0xFFFFFF},
  385. {0}
  386. };
  387. static struct pci_driver ioc4_driver = {
  388. .name = "IOC4",
  389. .id_table = ioc4_id_table,
  390. .probe = ioc4_probe,
  391. .remove = ioc4_remove,
  392. };
  393. MODULE_DEVICE_TABLE(pci, ioc4_id_table);
  394. /*********************
  395. * Module management *
  396. *********************/
  397. /* Module load */
  398. static int __devinit
  399. ioc4_init(void)
  400. {
  401. return pci_register_driver(&ioc4_driver);
  402. }
  403. /* Module unload */
  404. static void __devexit
  405. ioc4_exit(void)
  406. {
  407. pci_unregister_driver(&ioc4_driver);
  408. }
  409. module_init(ioc4_init);
  410. module_exit(ioc4_exit);
  411. MODULE_AUTHOR("Brent Casavant - Silicon Graphics, Inc. <bcasavan@sgi.com>");
  412. MODULE_DESCRIPTION("PCI driver master module for SGI IOC4 Base-IO Card");
  413. MODULE_LICENSE("GPL");
  414. EXPORT_SYMBOL(ioc4_register_submodule);
  415. EXPORT_SYMBOL(ioc4_unregister_submodule);