ioc4.c 14 KB

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