ioc4.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. #include <asm/io.h>
  34. /***************
  35. * Definitions *
  36. ***************/
  37. /* Tweakable values */
  38. /* PCI bus speed detection/calibration */
  39. #define IOC4_CALIBRATE_COUNT 63 /* Calibration cycle period */
  40. #define IOC4_CALIBRATE_CYCLES 256 /* Average over this many cycles */
  41. #define IOC4_CALIBRATE_DISCARD 2 /* Discard first few cycles */
  42. #define IOC4_CALIBRATE_LOW_MHZ 25 /* Lower bound on bus speed sanity */
  43. #define IOC4_CALIBRATE_HIGH_MHZ 75 /* Upper bound on bus speed sanity */
  44. #define IOC4_CALIBRATE_DEFAULT_MHZ 66 /* Assumed if sanity check fails */
  45. /************************
  46. * Submodule management *
  47. ************************/
  48. static DEFINE_MUTEX(ioc4_mutex);
  49. static LIST_HEAD(ioc4_devices);
  50. static LIST_HEAD(ioc4_submodules);
  51. /* Register an IOC4 submodule */
  52. int
  53. ioc4_register_submodule(struct ioc4_submodule *is)
  54. {
  55. struct ioc4_driver_data *idd;
  56. mutex_lock(&ioc4_mutex);
  57. list_add(&is->is_list, &ioc4_submodules);
  58. /* Initialize submodule for each IOC4 */
  59. if (!is->is_probe)
  60. goto out;
  61. list_for_each_entry(idd, &ioc4_devices, idd_list) {
  62. if (is->is_probe(idd)) {
  63. printk(KERN_WARNING
  64. "%s: IOC4 submodule %s probe failed "
  65. "for pci_dev %s",
  66. __func__, module_name(is->is_owner),
  67. pci_name(idd->idd_pdev));
  68. }
  69. }
  70. out:
  71. mutex_unlock(&ioc4_mutex);
  72. return 0;
  73. }
  74. /* Unregister an IOC4 submodule */
  75. void
  76. ioc4_unregister_submodule(struct ioc4_submodule *is)
  77. {
  78. struct ioc4_driver_data *idd;
  79. mutex_lock(&ioc4_mutex);
  80. list_del(&is->is_list);
  81. /* Remove submodule for each IOC4 */
  82. if (!is->is_remove)
  83. goto out;
  84. list_for_each_entry(idd, &ioc4_devices, idd_list) {
  85. if (is->is_remove(idd)) {
  86. printk(KERN_WARNING
  87. "%s: IOC4 submodule %s remove failed "
  88. "for pci_dev %s.\n",
  89. __func__, module_name(is->is_owner),
  90. pci_name(idd->idd_pdev));
  91. }
  92. }
  93. out:
  94. mutex_unlock(&ioc4_mutex);
  95. }
  96. /*********************
  97. * Device management *
  98. *********************/
  99. #define IOC4_CALIBRATE_LOW_LIMIT \
  100. (1000*IOC4_EXTINT_COUNT_DIVISOR/IOC4_CALIBRATE_LOW_MHZ)
  101. #define IOC4_CALIBRATE_HIGH_LIMIT \
  102. (1000*IOC4_EXTINT_COUNT_DIVISOR/IOC4_CALIBRATE_HIGH_MHZ)
  103. #define IOC4_CALIBRATE_DEFAULT \
  104. (1000*IOC4_EXTINT_COUNT_DIVISOR/IOC4_CALIBRATE_DEFAULT_MHZ)
  105. #define IOC4_CALIBRATE_END \
  106. (IOC4_CALIBRATE_CYCLES + IOC4_CALIBRATE_DISCARD)
  107. #define IOC4_INT_OUT_MODE_TOGGLE 0x7 /* Toggle INT_OUT every COUNT+1 ticks */
  108. /* Determines external interrupt output clock period of the PCI bus an
  109. * IOC4 is attached to. This value can be used to determine the PCI
  110. * bus speed.
  111. *
  112. * IOC4 has a design feature that various internal timers are derived from
  113. * the PCI bus clock. This causes IOC4 device drivers to need to take the
  114. * bus speed into account when setting various register values (e.g. INT_OUT
  115. * register COUNT field, UART divisors, etc). Since this information is
  116. * needed by several subdrivers, it is determined by the main IOC4 driver,
  117. * even though the following code utilizes external interrupt registers
  118. * to perform the speed calculation.
  119. */
  120. static void
  121. ioc4_clock_calibrate(struct ioc4_driver_data *idd)
  122. {
  123. union ioc4_int_out int_out;
  124. union ioc4_gpcr gpcr;
  125. unsigned int state, last_state = 1;
  126. struct timespec start_ts, end_ts;
  127. uint64_t start, end, period;
  128. unsigned int count = 0;
  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. do {
  146. int_out.raw = readl(&idd->idd_misc_regs->int_out.raw);
  147. state = int_out.fields.int_out;
  148. if (!last_state && state) {
  149. count++;
  150. if (count == IOC4_CALIBRATE_END) {
  151. ktime_get_ts(&end_ts);
  152. break;
  153. } else if (count == IOC4_CALIBRATE_DISCARD)
  154. ktime_get_ts(&start_ts);
  155. }
  156. last_state = state;
  157. } while (1);
  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. end = end_ts.tv_sec * NSEC_PER_SEC + end_ts.tv_nsec;
  169. start = start_ts.tv_sec * NSEC_PER_SEC + start_ts.tv_nsec;
  170. period = (end - start) /
  171. (IOC4_CALIBRATE_CYCLES * 2 * (IOC4_CALIBRATE_COUNT + 1));
  172. /* Bounds check the result. */
  173. if (period > IOC4_CALIBRATE_LOW_LIMIT ||
  174. period < IOC4_CALIBRATE_HIGH_LIMIT) {
  175. printk(KERN_INFO
  176. "IOC4 %s: Clock calibration failed. Assuming"
  177. "PCI clock is %d ns.\n",
  178. pci_name(idd->idd_pdev),
  179. IOC4_CALIBRATE_DEFAULT / IOC4_EXTINT_COUNT_DIVISOR);
  180. period = IOC4_CALIBRATE_DEFAULT;
  181. } else {
  182. u64 ns = period;
  183. do_div(ns, IOC4_EXTINT_COUNT_DIVISOR);
  184. printk(KERN_DEBUG
  185. "IOC4 %s: PCI clock is %llu ns.\n",
  186. pci_name(idd->idd_pdev), (unsigned long long)ns);
  187. }
  188. /* Remember results. We store the extint clock period rather
  189. * than the PCI clock period so that greater precision is
  190. * retained. Divide by IOC4_EXTINT_COUNT_DIVISOR to get
  191. * PCI clock period.
  192. */
  193. idd->count_period = period;
  194. }
  195. /* There are three variants of IOC4 cards: IO9, IO10, and PCI-RT.
  196. * Each brings out different combinations of IOC4 signals, thus.
  197. * the IOC4 subdrivers need to know to which we're attached.
  198. *
  199. * We look for the presence of a SCSI (IO9) or SATA (IO10) controller
  200. * on the same PCI bus at slot number 3 to differentiate IO9 from IO10.
  201. * If neither is present, it's a PCI-RT.
  202. */
  203. static unsigned int
  204. ioc4_variant(struct ioc4_driver_data *idd)
  205. {
  206. struct pci_dev *pdev = NULL;
  207. int found = 0;
  208. /* IO9: Look for a QLogic ISP 12160 at the same bus and slot 3. */
  209. do {
  210. pdev = pci_get_device(PCI_VENDOR_ID_QLOGIC,
  211. PCI_DEVICE_ID_QLOGIC_ISP12160, pdev);
  212. if (pdev &&
  213. idd->idd_pdev->bus->number == pdev->bus->number &&
  214. 3 == PCI_SLOT(pdev->devfn))
  215. found = 1;
  216. } while (pdev && !found);
  217. if (NULL != pdev) {
  218. pci_dev_put(pdev);
  219. return IOC4_VARIANT_IO9;
  220. }
  221. /* IO10: Look for a Vitesse VSC 7174 at the same bus and slot 3. */
  222. pdev = NULL;
  223. do {
  224. pdev = pci_get_device(PCI_VENDOR_ID_VITESSE,
  225. PCI_DEVICE_ID_VITESSE_VSC7174, pdev);
  226. if (pdev &&
  227. idd->idd_pdev->bus->number == pdev->bus->number &&
  228. 3 == PCI_SLOT(pdev->devfn))
  229. found = 1;
  230. } while (pdev && !found);
  231. if (NULL != pdev) {
  232. pci_dev_put(pdev);
  233. return IOC4_VARIANT_IO10;
  234. }
  235. /* PCI-RT: No SCSI/SATA controller will be present */
  236. return IOC4_VARIANT_PCI_RT;
  237. }
  238. static void
  239. ioc4_load_modules(struct work_struct *work)
  240. {
  241. /* arg just has to be freed */
  242. request_module("sgiioc4");
  243. kfree(work);
  244. }
  245. /* Adds a new instance of an IOC4 card */
  246. static int
  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. struct work_struct *work;
  350. work = kzalloc(sizeof(struct work_struct), GFP_KERNEL);
  351. if (!work) {
  352. printk(KERN_WARNING
  353. "%s: IOC4 unable to allocate memory for "
  354. "load of sub-modules.\n", __func__);
  355. } else {
  356. /* Request the module from a work procedure as the
  357. * modprobe goes out to a userland helper and that
  358. * will hang if done directly from ioc4_probe().
  359. */
  360. printk(KERN_INFO "IOC4 loading sgiioc4 submodule\n");
  361. INIT_WORK(work, ioc4_load_modules);
  362. schedule_work(work);
  363. }
  364. }
  365. return 0;
  366. out_misc_region:
  367. release_mem_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs));
  368. out_pci:
  369. kfree(idd);
  370. out_idd:
  371. pci_disable_device(pdev);
  372. out:
  373. return ret;
  374. }
  375. /* Removes a particular instance of an IOC4 card. */
  376. static void
  377. ioc4_remove(struct pci_dev *pdev)
  378. {
  379. struct ioc4_submodule *is;
  380. struct ioc4_driver_data *idd;
  381. idd = pci_get_drvdata(pdev);
  382. /* Remove this IOC4 from all submodules */
  383. mutex_lock(&ioc4_mutex);
  384. list_for_each_entry(is, &ioc4_submodules, is_list) {
  385. if (is->is_remove && is->is_remove(idd)) {
  386. printk(KERN_WARNING
  387. "%s: IOC4 submodule 0x%s remove failed "
  388. "for pci_dev %s.\n",
  389. __func__, module_name(is->is_owner),
  390. pci_name(idd->idd_pdev));
  391. }
  392. }
  393. mutex_unlock(&ioc4_mutex);
  394. /* Release resources */
  395. iounmap(idd->idd_misc_regs);
  396. if (!idd->idd_bar0) {
  397. printk(KERN_WARNING
  398. "%s: Unable to get IOC4 misc mapping for pci_dev %s. "
  399. "Device removal may be incomplete.\n",
  400. __func__, pci_name(idd->idd_pdev));
  401. }
  402. release_mem_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs));
  403. /* Disable IOC4 and relinquish */
  404. pci_disable_device(pdev);
  405. /* Remove and free driver data */
  406. mutex_lock(&ioc4_mutex);
  407. list_del(&idd->idd_list);
  408. mutex_unlock(&ioc4_mutex);
  409. kfree(idd);
  410. }
  411. static struct pci_device_id ioc4_id_table[] = {
  412. {PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC4, PCI_ANY_ID,
  413. PCI_ANY_ID, 0x0b4000, 0xFFFFFF},
  414. {0}
  415. };
  416. static struct pci_driver ioc4_driver = {
  417. .name = "IOC4",
  418. .id_table = ioc4_id_table,
  419. .probe = ioc4_probe,
  420. .remove = ioc4_remove,
  421. };
  422. MODULE_DEVICE_TABLE(pci, ioc4_id_table);
  423. /*********************
  424. * Module management *
  425. *********************/
  426. /* Module load */
  427. static int __devinit
  428. ioc4_init(void)
  429. {
  430. return pci_register_driver(&ioc4_driver);
  431. }
  432. /* Module unload */
  433. static void __devexit
  434. ioc4_exit(void)
  435. {
  436. /* Ensure ioc4_load_modules() has completed before exiting */
  437. flush_scheduled_work();
  438. pci_unregister_driver(&ioc4_driver);
  439. }
  440. module_init(ioc4_init);
  441. module_exit(ioc4_exit);
  442. MODULE_AUTHOR("Brent Casavant - Silicon Graphics, Inc. <bcasavan@sgi.com>");
  443. MODULE_DESCRIPTION("PCI driver master module for SGI IOC4 Base-IO Card");
  444. MODULE_LICENSE("GPL");
  445. EXPORT_SYMBOL(ioc4_register_submodule);
  446. EXPORT_SYMBOL(ioc4_unregister_submodule);