ioc4.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 __devinit
  240. ioc4_load_modules(struct work_struct *work)
  241. {
  242. /* arg just has to be freed */
  243. request_module("sgiioc4");
  244. kfree(work);
  245. }
  246. /* Adds a new instance of an IOC4 card */
  247. static int __devinit
  248. ioc4_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id)
  249. {
  250. struct ioc4_driver_data *idd;
  251. struct ioc4_submodule *is;
  252. uint32_t pcmd;
  253. int ret;
  254. /* Enable IOC4 and take ownership of it */
  255. if ((ret = pci_enable_device(pdev))) {
  256. printk(KERN_WARNING
  257. "%s: Failed to enable IOC4 device for pci_dev %s.\n",
  258. __func__, pci_name(pdev));
  259. goto out;
  260. }
  261. pci_set_master(pdev);
  262. /* Set up per-IOC4 data */
  263. idd = kmalloc(sizeof(struct ioc4_driver_data), GFP_KERNEL);
  264. if (!idd) {
  265. printk(KERN_WARNING
  266. "%s: Failed to allocate IOC4 data for pci_dev %s.\n",
  267. __func__, pci_name(pdev));
  268. ret = -ENODEV;
  269. goto out_idd;
  270. }
  271. idd->idd_pdev = pdev;
  272. idd->idd_pci_id = pci_id;
  273. /* Map IOC4 misc registers. These are shared between subdevices
  274. * so the main IOC4 module manages them.
  275. */
  276. idd->idd_bar0 = pci_resource_start(idd->idd_pdev, 0);
  277. if (!idd->idd_bar0) {
  278. printk(KERN_WARNING
  279. "%s: Unable to find IOC4 misc resource "
  280. "for pci_dev %s.\n",
  281. __func__, pci_name(idd->idd_pdev));
  282. ret = -ENODEV;
  283. goto out_pci;
  284. }
  285. if (!request_mem_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs),
  286. "ioc4_misc")) {
  287. printk(KERN_WARNING
  288. "%s: Unable to request IOC4 misc region "
  289. "for pci_dev %s.\n",
  290. __func__, pci_name(idd->idd_pdev));
  291. ret = -ENODEV;
  292. goto out_pci;
  293. }
  294. idd->idd_misc_regs = ioremap(idd->idd_bar0,
  295. sizeof(struct ioc4_misc_regs));
  296. if (!idd->idd_misc_regs) {
  297. printk(KERN_WARNING
  298. "%s: Unable to remap IOC4 misc region "
  299. "for pci_dev %s.\n",
  300. __func__, pci_name(idd->idd_pdev));
  301. ret = -ENODEV;
  302. goto out_misc_region;
  303. }
  304. /* Failsafe portion of per-IOC4 initialization */
  305. /* Detect card variant */
  306. idd->idd_variant = ioc4_variant(idd);
  307. printk(KERN_INFO "IOC4 %s: %s card detected.\n", pci_name(pdev),
  308. idd->idd_variant == IOC4_VARIANT_IO9 ? "IO9" :
  309. idd->idd_variant == IOC4_VARIANT_PCI_RT ? "PCI-RT" :
  310. idd->idd_variant == IOC4_VARIANT_IO10 ? "IO10" : "unknown");
  311. /* Initialize IOC4 */
  312. pci_read_config_dword(idd->idd_pdev, PCI_COMMAND, &pcmd);
  313. pci_write_config_dword(idd->idd_pdev, PCI_COMMAND,
  314. pcmd | PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
  315. /* Determine PCI clock */
  316. ioc4_clock_calibrate(idd);
  317. /* Disable/clear all interrupts. Need to do this here lest
  318. * one submodule request the shared IOC4 IRQ, but interrupt
  319. * is generated by a different subdevice.
  320. */
  321. /* Disable */
  322. writel(~0, &idd->idd_misc_regs->other_iec.raw);
  323. writel(~0, &idd->idd_misc_regs->sio_iec);
  324. /* Clear (i.e. acknowledge) */
  325. writel(~0, &idd->idd_misc_regs->other_ir.raw);
  326. writel(~0, &idd->idd_misc_regs->sio_ir);
  327. /* Track PCI-device specific data */
  328. idd->idd_serial_data = NULL;
  329. pci_set_drvdata(idd->idd_pdev, idd);
  330. mutex_lock(&ioc4_mutex);
  331. list_add_tail(&idd->idd_list, &ioc4_devices);
  332. /* Add this IOC4 to all submodules */
  333. list_for_each_entry(is, &ioc4_submodules, is_list) {
  334. if (is->is_probe && is->is_probe(idd)) {
  335. printk(KERN_WARNING
  336. "%s: IOC4 submodule 0x%s probe failed "
  337. "for pci_dev %s.\n",
  338. __func__, module_name(is->is_owner),
  339. pci_name(idd->idd_pdev));
  340. }
  341. }
  342. mutex_unlock(&ioc4_mutex);
  343. /* Request sgiioc4 IDE driver on boards that bring that functionality
  344. * off of IOC4. The root filesystem may be hosted on a drive connected
  345. * to IOC4, so we need to make sure the sgiioc4 driver is loaded as it
  346. * won't be picked up by modprobes due to the ioc4 module owning the
  347. * PCI device.
  348. */
  349. if (idd->idd_variant != IOC4_VARIANT_PCI_RT) {
  350. struct work_struct *work;
  351. work = kzalloc(sizeof(struct work_struct), GFP_KERNEL);
  352. if (!work) {
  353. printk(KERN_WARNING
  354. "%s: IOC4 unable to allocate memory for "
  355. "load of sub-modules.\n", __func__);
  356. } else {
  357. /* Request the module from a work procedure as the
  358. * modprobe goes out to a userland helper and that
  359. * will hang if done directly from ioc4_probe().
  360. */
  361. printk(KERN_INFO "IOC4 loading sgiioc4 submodule\n");
  362. INIT_WORK(work, ioc4_load_modules);
  363. schedule_work(work);
  364. }
  365. }
  366. return 0;
  367. out_misc_region:
  368. release_mem_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs));
  369. out_pci:
  370. kfree(idd);
  371. out_idd:
  372. pci_disable_device(pdev);
  373. out:
  374. return ret;
  375. }
  376. /* Removes a particular instance of an IOC4 card. */
  377. static void __devexit
  378. ioc4_remove(struct pci_dev *pdev)
  379. {
  380. struct ioc4_submodule *is;
  381. struct ioc4_driver_data *idd;
  382. idd = pci_get_drvdata(pdev);
  383. /* Remove this IOC4 from all submodules */
  384. mutex_lock(&ioc4_mutex);
  385. list_for_each_entry(is, &ioc4_submodules, is_list) {
  386. if (is->is_remove && is->is_remove(idd)) {
  387. printk(KERN_WARNING
  388. "%s: IOC4 submodule 0x%s remove failed "
  389. "for pci_dev %s.\n",
  390. __func__, module_name(is->is_owner),
  391. pci_name(idd->idd_pdev));
  392. }
  393. }
  394. mutex_unlock(&ioc4_mutex);
  395. /* Release resources */
  396. iounmap(idd->idd_misc_regs);
  397. if (!idd->idd_bar0) {
  398. printk(KERN_WARNING
  399. "%s: Unable to get IOC4 misc mapping for pci_dev %s. "
  400. "Device removal may be incomplete.\n",
  401. __func__, pci_name(idd->idd_pdev));
  402. }
  403. release_mem_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs));
  404. /* Disable IOC4 and relinquish */
  405. pci_disable_device(pdev);
  406. /* Remove and free driver data */
  407. mutex_lock(&ioc4_mutex);
  408. list_del(&idd->idd_list);
  409. mutex_unlock(&ioc4_mutex);
  410. kfree(idd);
  411. }
  412. static struct pci_device_id ioc4_id_table[] = {
  413. {PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC4, PCI_ANY_ID,
  414. PCI_ANY_ID, 0x0b4000, 0xFFFFFF},
  415. {0}
  416. };
  417. static struct pci_driver ioc4_driver = {
  418. .name = "IOC4",
  419. .id_table = ioc4_id_table,
  420. .probe = ioc4_probe,
  421. .remove = __devexit_p(ioc4_remove),
  422. };
  423. MODULE_DEVICE_TABLE(pci, ioc4_id_table);
  424. /*********************
  425. * Module management *
  426. *********************/
  427. /* Module load */
  428. static int __init
  429. ioc4_init(void)
  430. {
  431. return pci_register_driver(&ioc4_driver);
  432. }
  433. /* Module unload */
  434. static void __exit
  435. ioc4_exit(void)
  436. {
  437. /* Ensure ioc4_load_modules() has completed before exiting */
  438. flush_scheduled_work();
  439. pci_unregister_driver(&ioc4_driver);
  440. }
  441. module_init(ioc4_init);
  442. module_exit(ioc4_exit);
  443. MODULE_AUTHOR("Brent Casavant - Silicon Graphics, Inc. <bcasavan@sgi.com>");
  444. MODULE_DESCRIPTION("PCI driver master module for SGI IOC4 Base-IO Card");
  445. MODULE_LICENSE("GPL");
  446. EXPORT_SYMBOL(ioc4_register_submodule);
  447. EXPORT_SYMBOL(ioc4_unregister_submodule);