ioc4.c 14 KB

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