ioc4.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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-2006 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/ktime.h>
  31. #include <linux/mutex.h>
  32. #include <linux/time.h>
  33. /***************
  34. * Definitions *
  35. ***************/
  36. /* Tweakable values */
  37. /* PCI bus speed detection/calibration */
  38. #define IOC4_CALIBRATE_COUNT 63 /* Calibration cycle period */
  39. #define IOC4_CALIBRATE_CYCLES 256 /* Average over this many cycles */
  40. #define IOC4_CALIBRATE_DISCARD 2 /* Discard first few cycles */
  41. #define IOC4_CALIBRATE_LOW_MHZ 25 /* Lower bound on bus speed sanity */
  42. #define IOC4_CALIBRATE_HIGH_MHZ 75 /* Upper bound on bus speed sanity */
  43. #define IOC4_CALIBRATE_DEFAULT_MHZ 66 /* Assumed if sanity check fails */
  44. /************************
  45. * Submodule management *
  46. ************************/
  47. static DEFINE_MUTEX(ioc4_mutex);
  48. static LIST_HEAD(ioc4_devices);
  49. static LIST_HEAD(ioc4_submodules);
  50. /* Register an IOC4 submodule */
  51. int
  52. ioc4_register_submodule(struct ioc4_submodule *is)
  53. {
  54. struct ioc4_driver_data *idd;
  55. mutex_lock(&ioc4_mutex);
  56. list_add(&is->is_list, &ioc4_submodules);
  57. /* Initialize submodule for each IOC4 */
  58. if (!is->is_probe)
  59. goto out;
  60. list_for_each_entry(idd, &ioc4_devices, idd_list) {
  61. if (is->is_probe(idd)) {
  62. printk(KERN_WARNING
  63. "%s: IOC4 submodule %s probe failed "
  64. "for pci_dev %s",
  65. __FUNCTION__, module_name(is->is_owner),
  66. pci_name(idd->idd_pdev));
  67. }
  68. }
  69. out:
  70. mutex_unlock(&ioc4_mutex);
  71. return 0;
  72. }
  73. /* Unregister an IOC4 submodule */
  74. void
  75. ioc4_unregister_submodule(struct ioc4_submodule *is)
  76. {
  77. struct ioc4_driver_data *idd;
  78. mutex_lock(&ioc4_mutex);
  79. list_del(&is->is_list);
  80. /* Remove submodule for each IOC4 */
  81. if (!is->is_remove)
  82. goto out;
  83. list_for_each_entry(idd, &ioc4_devices, idd_list) {
  84. if (is->is_remove(idd)) {
  85. printk(KERN_WARNING
  86. "%s: IOC4 submodule %s remove failed "
  87. "for pci_dev %s.\n",
  88. __FUNCTION__, module_name(is->is_owner),
  89. pci_name(idd->idd_pdev));
  90. }
  91. }
  92. out:
  93. mutex_unlock(&ioc4_mutex);
  94. }
  95. /*********************
  96. * Device management *
  97. *********************/
  98. #define IOC4_CALIBRATE_LOW_LIMIT \
  99. (1000*IOC4_EXTINT_COUNT_DIVISOR/IOC4_CALIBRATE_LOW_MHZ)
  100. #define IOC4_CALIBRATE_HIGH_LIMIT \
  101. (1000*IOC4_EXTINT_COUNT_DIVISOR/IOC4_CALIBRATE_HIGH_MHZ)
  102. #define IOC4_CALIBRATE_DEFAULT \
  103. (1000*IOC4_EXTINT_COUNT_DIVISOR/IOC4_CALIBRATE_DEFAULT_MHZ)
  104. #define IOC4_CALIBRATE_END \
  105. (IOC4_CALIBRATE_CYCLES + IOC4_CALIBRATE_DISCARD)
  106. #define IOC4_INT_OUT_MODE_TOGGLE 0x7 /* Toggle INT_OUT every COUNT+1 ticks */
  107. /* Determines external interrupt output clock period of the PCI bus an
  108. * IOC4 is attached to. This value can be used to determine the PCI
  109. * bus speed.
  110. *
  111. * IOC4 has a design feature that various internal timers are derived from
  112. * the PCI bus clock. This causes IOC4 device drivers to need to take the
  113. * bus speed into account when setting various register values (e.g. INT_OUT
  114. * register COUNT field, UART divisors, etc). Since this information is
  115. * needed by several subdrivers, it is determined by the main IOC4 driver,
  116. * even though the following code utilizes external interrupt registers
  117. * to perform the speed calculation.
  118. */
  119. static void
  120. ioc4_clock_calibrate(struct ioc4_driver_data *idd)
  121. {
  122. union ioc4_int_out int_out;
  123. union ioc4_gpcr gpcr;
  124. unsigned int state, last_state = 1;
  125. struct timespec start_ts, end_ts;
  126. uint64_t start, end, period;
  127. unsigned int count = 0;
  128. /* Enable output */
  129. gpcr.raw = 0;
  130. gpcr.fields.dir = IOC4_GPCR_DIR_0;
  131. gpcr.fields.int_out_en = 1;
  132. writel(gpcr.raw, &idd->idd_misc_regs->gpcr_s.raw);
  133. /* Reset to power-on state */
  134. writel(0, &idd->idd_misc_regs->int_out.raw);
  135. mmiowb();
  136. /* Set up square wave */
  137. int_out.raw = 0;
  138. int_out.fields.count = IOC4_CALIBRATE_COUNT;
  139. int_out.fields.mode = IOC4_INT_OUT_MODE_TOGGLE;
  140. int_out.fields.diag = 0;
  141. writel(int_out.raw, &idd->idd_misc_regs->int_out.raw);
  142. mmiowb();
  143. /* Check square wave period averaged over some number of cycles */
  144. do {
  145. int_out.raw = readl(&idd->idd_misc_regs->int_out.raw);
  146. state = int_out.fields.int_out;
  147. if (!last_state && state) {
  148. count++;
  149. if (count == IOC4_CALIBRATE_END) {
  150. ktime_get_ts(&end_ts);
  151. break;
  152. } else if (count == IOC4_CALIBRATE_DISCARD)
  153. ktime_get_ts(&start_ts);
  154. }
  155. last_state = state;
  156. } while (1);
  157. /* Calculation rearranged to preserve intermediate precision.
  158. * Logically:
  159. * 1. "end - start" gives us the measurement period over all
  160. * the square wave cycles.
  161. * 2. Divide by number of square wave cycles to get the period
  162. * of a square wave cycle.
  163. * 3. Divide by 2*(int_out.fields.count+1), which is the formula
  164. * by which the IOC4 generates the square wave, to get the
  165. * period of an IOC4 INT_OUT count.
  166. */
  167. end = end_ts.tv_sec * NSEC_PER_SEC + end_ts.tv_nsec;
  168. start = start_ts.tv_sec * NSEC_PER_SEC + start_ts.tv_nsec;
  169. period = (end - start) /
  170. (IOC4_CALIBRATE_CYCLES * 2 * (IOC4_CALIBRATE_COUNT + 1));
  171. /* Bounds check the result. */
  172. if (period > IOC4_CALIBRATE_LOW_LIMIT ||
  173. period < IOC4_CALIBRATE_HIGH_LIMIT) {
  174. printk(KERN_INFO
  175. "IOC4 %s: Clock calibration failed. Assuming"
  176. "PCI clock is %d ns.\n",
  177. pci_name(idd->idd_pdev),
  178. IOC4_CALIBRATE_DEFAULT / IOC4_EXTINT_COUNT_DIVISOR);
  179. period = IOC4_CALIBRATE_DEFAULT;
  180. } else {
  181. u64 ns = period;
  182. do_div(ns, IOC4_EXTINT_COUNT_DIVISOR);
  183. printk(KERN_DEBUG
  184. "IOC4 %s: PCI clock is %lld ns.\n",
  185. pci_name(idd->idd_pdev), ns);
  186. }
  187. /* Remember results. We store the extint clock period rather
  188. * than the PCI clock period so that greater precision is
  189. * retained. Divide by IOC4_EXTINT_COUNT_DIVISOR to get
  190. * PCI clock period.
  191. */
  192. idd->count_period = period;
  193. }
  194. /* There are three variants of IOC4 cards: IO9, IO10, and PCI-RT.
  195. * Each brings out different combinations of IOC4 signals, thus.
  196. * the IOC4 subdrivers need to know to which we're attached.
  197. *
  198. * We look for the presence of a SCSI (IO9) or SATA (IO10) controller
  199. * on the same PCI bus at slot number 3 to differentiate IO9 from IO10.
  200. * If neither is present, it's a PCI-RT.
  201. */
  202. static unsigned int
  203. ioc4_variant(struct ioc4_driver_data *idd)
  204. {
  205. struct pci_dev *pdev = NULL;
  206. int found = 0;
  207. /* IO9: Look for a QLogic ISP 12160 at the same bus and slot 3. */
  208. do {
  209. pdev = pci_get_device(PCI_VENDOR_ID_QLOGIC,
  210. PCI_DEVICE_ID_QLOGIC_ISP12160, pdev);
  211. if (pdev &&
  212. idd->idd_pdev->bus->number == pdev->bus->number &&
  213. 3 == PCI_SLOT(pdev->devfn))
  214. found = 1;
  215. pci_dev_put(pdev);
  216. } while (pdev && !found);
  217. if (NULL != pdev)
  218. return IOC4_VARIANT_IO9;
  219. /* IO10: Look for a Vitesse VSC 7174 at the same bus and slot 3. */
  220. pdev = NULL;
  221. do {
  222. pdev = pci_get_device(PCI_VENDOR_ID_VITESSE,
  223. PCI_DEVICE_ID_VITESSE_VSC7174, pdev);
  224. if (pdev &&
  225. idd->idd_pdev->bus->number == pdev->bus->number &&
  226. 3 == PCI_SLOT(pdev->devfn))
  227. found = 1;
  228. pci_dev_put(pdev);
  229. } while (pdev && !found);
  230. if (NULL != pdev)
  231. return IOC4_VARIANT_IO10;
  232. /* PCI-RT: No SCSI/SATA controller will be present */
  233. return IOC4_VARIANT_PCI_RT;
  234. }
  235. /* Adds a new instance of an IOC4 card */
  236. static int
  237. ioc4_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id)
  238. {
  239. struct ioc4_driver_data *idd;
  240. struct ioc4_submodule *is;
  241. uint32_t pcmd;
  242. int ret;
  243. /* Enable IOC4 and take ownership of it */
  244. if ((ret = pci_enable_device(pdev))) {
  245. printk(KERN_WARNING
  246. "%s: Failed to enable IOC4 device for pci_dev %s.\n",
  247. __FUNCTION__, pci_name(pdev));
  248. goto out;
  249. }
  250. pci_set_master(pdev);
  251. /* Set up per-IOC4 data */
  252. idd = kmalloc(sizeof(struct ioc4_driver_data), GFP_KERNEL);
  253. if (!idd) {
  254. printk(KERN_WARNING
  255. "%s: Failed to allocate IOC4 data for pci_dev %s.\n",
  256. __FUNCTION__, pci_name(pdev));
  257. ret = -ENODEV;
  258. goto out_idd;
  259. }
  260. idd->idd_pdev = pdev;
  261. idd->idd_pci_id = pci_id;
  262. /* Map IOC4 misc registers. These are shared between subdevices
  263. * so the main IOC4 module manages them.
  264. */
  265. idd->idd_bar0 = pci_resource_start(idd->idd_pdev, 0);
  266. if (!idd->idd_bar0) {
  267. printk(KERN_WARNING
  268. "%s: Unable to find IOC4 misc resource "
  269. "for pci_dev %s.\n",
  270. __FUNCTION__, pci_name(idd->idd_pdev));
  271. ret = -ENODEV;
  272. goto out_pci;
  273. }
  274. if (!request_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs),
  275. "ioc4_misc")) {
  276. printk(KERN_WARNING
  277. "%s: Unable to request IOC4 misc region "
  278. "for pci_dev %s.\n",
  279. __FUNCTION__, pci_name(idd->idd_pdev));
  280. ret = -ENODEV;
  281. goto out_pci;
  282. }
  283. idd->idd_misc_regs = ioremap(idd->idd_bar0,
  284. sizeof(struct ioc4_misc_regs));
  285. if (!idd->idd_misc_regs) {
  286. printk(KERN_WARNING
  287. "%s: Unable to remap IOC4 misc region "
  288. "for pci_dev %s.\n",
  289. __FUNCTION__, pci_name(idd->idd_pdev));
  290. ret = -ENODEV;
  291. goto out_misc_region;
  292. }
  293. /* Failsafe portion of per-IOC4 initialization */
  294. /* Detect card variant */
  295. idd->idd_variant = ioc4_variant(idd);
  296. printk(KERN_INFO "IOC4 %s: %s card detected.\n", pci_name(pdev),
  297. idd->idd_variant == IOC4_VARIANT_IO9 ? "IO9" :
  298. idd->idd_variant == IOC4_VARIANT_PCI_RT ? "PCI-RT" :
  299. idd->idd_variant == IOC4_VARIANT_IO10 ? "IO10" : "unknown");
  300. /* Initialize IOC4 */
  301. pci_read_config_dword(idd->idd_pdev, PCI_COMMAND, &pcmd);
  302. pci_write_config_dword(idd->idd_pdev, PCI_COMMAND,
  303. pcmd | PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
  304. /* Determine PCI clock */
  305. ioc4_clock_calibrate(idd);
  306. /* Disable/clear all interrupts. Need to do this here lest
  307. * one submodule request the shared IOC4 IRQ, but interrupt
  308. * is generated by a different subdevice.
  309. */
  310. /* Disable */
  311. writel(~0, &idd->idd_misc_regs->other_iec.raw);
  312. writel(~0, &idd->idd_misc_regs->sio_iec);
  313. /* Clear (i.e. acknowledge) */
  314. writel(~0, &idd->idd_misc_regs->other_ir.raw);
  315. writel(~0, &idd->idd_misc_regs->sio_ir);
  316. /* Track PCI-device specific data */
  317. idd->idd_serial_data = NULL;
  318. pci_set_drvdata(idd->idd_pdev, idd);
  319. mutex_lock(&ioc4_mutex);
  320. list_add_tail(&idd->idd_list, &ioc4_devices);
  321. /* Add this IOC4 to all submodules */
  322. list_for_each_entry(is, &ioc4_submodules, is_list) {
  323. if (is->is_probe && is->is_probe(idd)) {
  324. printk(KERN_WARNING
  325. "%s: IOC4 submodule 0x%s probe failed "
  326. "for pci_dev %s.\n",
  327. __FUNCTION__, module_name(is->is_owner),
  328. pci_name(idd->idd_pdev));
  329. }
  330. }
  331. mutex_unlock(&ioc4_mutex);
  332. return 0;
  333. out_misc_region:
  334. release_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs));
  335. out_pci:
  336. kfree(idd);
  337. out_idd:
  338. pci_disable_device(pdev);
  339. out:
  340. return ret;
  341. }
  342. /* Removes a particular instance of an IOC4 card. */
  343. static void
  344. ioc4_remove(struct pci_dev *pdev)
  345. {
  346. struct ioc4_submodule *is;
  347. struct ioc4_driver_data *idd;
  348. idd = pci_get_drvdata(pdev);
  349. /* Remove this IOC4 from all submodules */
  350. mutex_lock(&ioc4_mutex);
  351. list_for_each_entry(is, &ioc4_submodules, is_list) {
  352. if (is->is_remove && is->is_remove(idd)) {
  353. printk(KERN_WARNING
  354. "%s: IOC4 submodule 0x%s remove failed "
  355. "for pci_dev %s.\n",
  356. __FUNCTION__, module_name(is->is_owner),
  357. pci_name(idd->idd_pdev));
  358. }
  359. }
  360. mutex_unlock(&ioc4_mutex);
  361. /* Release resources */
  362. iounmap(idd->idd_misc_regs);
  363. if (!idd->idd_bar0) {
  364. printk(KERN_WARNING
  365. "%s: Unable to get IOC4 misc mapping for pci_dev %s. "
  366. "Device removal may be incomplete.\n",
  367. __FUNCTION__, pci_name(idd->idd_pdev));
  368. }
  369. release_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs));
  370. /* Disable IOC4 and relinquish */
  371. pci_disable_device(pdev);
  372. /* Remove and free driver data */
  373. mutex_lock(&ioc4_mutex);
  374. list_del(&idd->idd_list);
  375. mutex_unlock(&ioc4_mutex);
  376. kfree(idd);
  377. }
  378. static struct pci_device_id ioc4_id_table[] = {
  379. {PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC4, PCI_ANY_ID,
  380. PCI_ANY_ID, 0x0b4000, 0xFFFFFF},
  381. {0}
  382. };
  383. static struct pci_driver ioc4_driver = {
  384. .name = "IOC4",
  385. .id_table = ioc4_id_table,
  386. .probe = ioc4_probe,
  387. .remove = ioc4_remove,
  388. };
  389. MODULE_DEVICE_TABLE(pci, ioc4_id_table);
  390. /*********************
  391. * Module management *
  392. *********************/
  393. /* Module load */
  394. static int __devinit
  395. ioc4_init(void)
  396. {
  397. return pci_register_driver(&ioc4_driver);
  398. }
  399. /* Module unload */
  400. static void __devexit
  401. ioc4_exit(void)
  402. {
  403. pci_unregister_driver(&ioc4_driver);
  404. }
  405. module_init(ioc4_init);
  406. module_exit(ioc4_exit);
  407. MODULE_AUTHOR("Brent Casavant - Silicon Graphics, Inc. <bcasavan@sgi.com>");
  408. MODULE_DESCRIPTION("PCI driver master module for SGI IOC4 Base-IO Card");
  409. MODULE_LICENSE("GPL");
  410. EXPORT_SYMBOL(ioc4_register_submodule);
  411. EXPORT_SYMBOL(ioc4_unregister_submodule);