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
6 * Copyright (C) 2005-2006 Silicon Graphics, Inc. All Rights Reserved.
9 /* This file contains the master driver module for use by SGI IOC4 subdrivers.
11 * It allocates any resources shared between multiple subdevices, and
12 * provides accessor functions (where needed) and the like for those
13 * resources. It also provides a mechanism for the subdevice modules
14 * to support loading and unloading.
16 * Non-shared resources (e.g. external interrupt A_INT_OUT register page
17 * alias, serial port and UART registers) are handled by the subdevice
20 * This is all necessary because IOC4 is not implemented as a multi-function
21 * PCI device, but an amalgamation of disparate registers for several
22 * types of device (ATA, serial, external interrupts). The normal
23 * resource management in the kernel doesn't have quite the right interfaces
24 * to handle this situation (e.g. multiple modules can't claim the same
25 * PCI ID), thus this IOC4 master module.
28 #include <linux/errno.h>
29 #include <linux/module.h>
30 #include <linux/pci.h>
31 #include <linux/ioc4.h>
32 #include <linux/ktime.h>
33 #include <linux/mutex.h>
34 #include <linux/time.h>
40 /* Tweakable values */
42 /* PCI bus speed detection/calibration */
43 #define IOC4_CALIBRATE_COUNT 63 /* Calibration cycle period */
44 #define IOC4_CALIBRATE_CYCLES 256 /* Average over this many cycles */
45 #define IOC4_CALIBRATE_DISCARD 2 /* Discard first few cycles */
46 #define IOC4_CALIBRATE_LOW_MHZ 25 /* Lower bound on bus speed sanity */
47 #define IOC4_CALIBRATE_HIGH_MHZ 75 /* Upper bound on bus speed sanity */
48 #define IOC4_CALIBRATE_DEFAULT_MHZ 66 /* Assumed if sanity check fails */
50 /************************
51 * Submodule management *
52 ************************/
54 static DEFINE_MUTEX(ioc4_mutex);
56 static LIST_HEAD(ioc4_devices);
57 static LIST_HEAD(ioc4_submodules);
59 /* Register an IOC4 submodule */
61 ioc4_register_submodule(struct ioc4_submodule *is)
63 struct ioc4_driver_data *idd;
65 mutex_lock(&ioc4_mutex);
66 list_add(&is->is_list, &ioc4_submodules);
68 /* Initialize submodule for each IOC4 */
72 list_for_each_entry(idd, &ioc4_devices, idd_list) {
73 if (is->is_probe(idd)) {
75 "%s: IOC4 submodule %s probe failed "
77 __FUNCTION__, module_name(is->is_owner),
78 pci_name(idd->idd_pdev));
82 mutex_unlock(&ioc4_mutex);
86 /* Unregister an IOC4 submodule */
88 ioc4_unregister_submodule(struct ioc4_submodule *is)
90 struct ioc4_driver_data *idd;
92 mutex_lock(&ioc4_mutex);
93 list_del(&is->is_list);
95 /* Remove submodule for each IOC4 */
99 list_for_each_entry(idd, &ioc4_devices, idd_list) {
100 if (is->is_remove(idd)) {
102 "%s: IOC4 submodule %s remove failed "
104 __FUNCTION__, module_name(is->is_owner),
105 pci_name(idd->idd_pdev));
109 mutex_unlock(&ioc4_mutex);
112 /*********************
113 * Device management *
114 *********************/
116 #define IOC4_CALIBRATE_LOW_LIMIT \
117 (1000*IOC4_EXTINT_COUNT_DIVISOR/IOC4_CALIBRATE_LOW_MHZ)
118 #define IOC4_CALIBRATE_HIGH_LIMIT \
119 (1000*IOC4_EXTINT_COUNT_DIVISOR/IOC4_CALIBRATE_HIGH_MHZ)
120 #define IOC4_CALIBRATE_DEFAULT \
121 (1000*IOC4_EXTINT_COUNT_DIVISOR/IOC4_CALIBRATE_DEFAULT_MHZ)
123 #define IOC4_CALIBRATE_END \
124 (IOC4_CALIBRATE_CYCLES + IOC4_CALIBRATE_DISCARD)
126 #define IOC4_INT_OUT_MODE_TOGGLE 0x7 /* Toggle INT_OUT every COUNT+1 ticks */
128 /* Determines external interrupt output clock period of the PCI bus an
129 * IOC4 is attached to. This value can be used to determine the PCI
132 * IOC4 has a design feature that various internal timers are derived from
133 * the PCI bus clock. This causes IOC4 device drivers to need to take the
134 * bus speed into account when setting various register values (e.g. INT_OUT
135 * register COUNT field, UART divisors, etc). Since this information is
136 * needed by several subdrivers, it is determined by the main IOC4 driver,
137 * even though the following code utilizes external interrupt registers
138 * to perform the speed calculation.
141 ioc4_clock_calibrate(struct ioc4_driver_data *idd)
143 union ioc4_int_out int_out;
144 union ioc4_gpcr gpcr;
145 unsigned int state, last_state = 1;
146 struct timespec start_ts, end_ts;
147 uint64_t start, end, period;
148 unsigned int count = 0;
152 gpcr.fields.dir = IOC4_GPCR_DIR_0;
153 gpcr.fields.int_out_en = 1;
154 writel(gpcr.raw, &idd->idd_misc_regs->gpcr_s.raw);
156 /* Reset to power-on state */
157 writel(0, &idd->idd_misc_regs->int_out.raw);
160 /* Set up square wave */
162 int_out.fields.count = IOC4_CALIBRATE_COUNT;
163 int_out.fields.mode = IOC4_INT_OUT_MODE_TOGGLE;
164 int_out.fields.diag = 0;
165 writel(int_out.raw, &idd->idd_misc_regs->int_out.raw);
168 /* Check square wave period averaged over some number of cycles */
170 int_out.raw = readl(&idd->idd_misc_regs->int_out.raw);
171 state = int_out.fields.int_out;
172 if (!last_state && state) {
174 if (count == IOC4_CALIBRATE_END) {
175 ktime_get_ts(&end_ts);
177 } else if (count == IOC4_CALIBRATE_DISCARD)
178 ktime_get_ts(&start_ts);
183 /* Calculation rearranged to preserve intermediate precision.
185 * 1. "end - start" gives us the measurement period over all
186 * the square wave cycles.
187 * 2. Divide by number of square wave cycles to get the period
188 * of a square wave cycle.
189 * 3. Divide by 2*(int_out.fields.count+1), which is the formula
190 * by which the IOC4 generates the square wave, to get the
191 * period of an IOC4 INT_OUT count.
193 end = end_ts.tv_sec * NSEC_PER_SEC + end_ts.tv_nsec;
194 start = start_ts.tv_sec * NSEC_PER_SEC + start_ts.tv_nsec;
195 period = (end - start) /
196 (IOC4_CALIBRATE_CYCLES * 2 * (IOC4_CALIBRATE_COUNT + 1));
198 /* Bounds check the result. */
199 if (period > IOC4_CALIBRATE_LOW_LIMIT ||
200 period < IOC4_CALIBRATE_HIGH_LIMIT) {
202 "IOC4 %s: Clock calibration failed. Assuming"
203 "PCI clock is %d ns.\n",
204 pci_name(idd->idd_pdev),
205 IOC4_CALIBRATE_DEFAULT / IOC4_EXTINT_COUNT_DIVISOR);
206 period = IOC4_CALIBRATE_DEFAULT;
210 do_div(ns, IOC4_EXTINT_COUNT_DIVISOR);
212 "IOC4 %s: PCI clock is %lld ns.\n",
213 pci_name(idd->idd_pdev), ns);
216 /* Remember results. We store the extint clock period rather
217 * than the PCI clock period so that greater precision is
218 * retained. Divide by IOC4_EXTINT_COUNT_DIVISOR to get
221 idd->count_period = period;
224 /* There are three variants of IOC4 cards: IO9, IO10, and PCI-RT.
225 * Each brings out different combinations of IOC4 signals, thus.
226 * the IOC4 subdrivers need to know to which we're attached.
228 * We look for the presence of a SCSI (IO9) or SATA (IO10) controller
229 * on the same PCI bus at slot number 3 to differentiate IO9 from IO10.
230 * If neither is present, it's a PCI-RT.
233 ioc4_variant(struct ioc4_driver_data *idd)
235 struct pci_dev *pdev = NULL;
238 /* IO9: Look for a QLogic ISP 12160 at the same bus and slot 3. */
240 pdev = pci_get_device(PCI_VENDOR_ID_QLOGIC,
241 PCI_DEVICE_ID_QLOGIC_ISP12160, pdev);
243 idd->idd_pdev->bus->number == pdev->bus->number &&
244 3 == PCI_SLOT(pdev->devfn))
247 } while (pdev && !found);
249 return IOC4_VARIANT_IO9;
251 /* IO10: Look for a Vitesse VSC 7174 at the same bus and slot 3. */
254 pdev = pci_get_device(PCI_VENDOR_ID_VITESSE,
255 PCI_DEVICE_ID_VITESSE_VSC7174, pdev);
257 idd->idd_pdev->bus->number == pdev->bus->number &&
258 3 == PCI_SLOT(pdev->devfn))
261 } while (pdev && !found);
263 return IOC4_VARIANT_IO10;
265 /* PCI-RT: No SCSI/SATA controller will be present */
266 return IOC4_VARIANT_PCI_RT;
269 /* Adds a new instance of an IOC4 card */
271 ioc4_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id)
273 struct ioc4_driver_data *idd;
274 struct ioc4_submodule *is;
278 /* Enable IOC4 and take ownership of it */
279 if ((ret = pci_enable_device(pdev))) {
281 "%s: Failed to enable IOC4 device for pci_dev %s.\n",
282 __FUNCTION__, pci_name(pdev));
285 pci_set_master(pdev);
287 /* Set up per-IOC4 data */
288 idd = kmalloc(sizeof(struct ioc4_driver_data), GFP_KERNEL);
291 "%s: Failed to allocate IOC4 data for pci_dev %s.\n",
292 __FUNCTION__, pci_name(pdev));
296 idd->idd_pdev = pdev;
297 idd->idd_pci_id = pci_id;
299 /* Map IOC4 misc registers. These are shared between subdevices
300 * so the main IOC4 module manages them.
302 idd->idd_bar0 = pci_resource_start(idd->idd_pdev, 0);
303 if (!idd->idd_bar0) {
305 "%s: Unable to find IOC4 misc resource "
307 __FUNCTION__, pci_name(idd->idd_pdev));
311 if (!request_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs),
314 "%s: Unable to request IOC4 misc region "
316 __FUNCTION__, pci_name(idd->idd_pdev));
320 idd->idd_misc_regs = ioremap(idd->idd_bar0,
321 sizeof(struct ioc4_misc_regs));
322 if (!idd->idd_misc_regs) {
324 "%s: Unable to remap IOC4 misc region "
326 __FUNCTION__, pci_name(idd->idd_pdev));
328 goto out_misc_region;
331 /* Failsafe portion of per-IOC4 initialization */
333 /* Detect card variant */
334 idd->idd_variant = ioc4_variant(idd);
335 printk(KERN_INFO "IOC4 %s: %s card detected.\n", pci_name(pdev),
336 idd->idd_variant == IOC4_VARIANT_IO9 ? "IO9" :
337 idd->idd_variant == IOC4_VARIANT_PCI_RT ? "PCI-RT" :
338 idd->idd_variant == IOC4_VARIANT_IO10 ? "IO10" : "unknown");
340 /* Initialize IOC4 */
341 pci_read_config_dword(idd->idd_pdev, PCI_COMMAND, &pcmd);
342 pci_write_config_dword(idd->idd_pdev, PCI_COMMAND,
343 pcmd | PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
345 /* Determine PCI clock */
346 ioc4_clock_calibrate(idd);
348 /* Disable/clear all interrupts. Need to do this here lest
349 * one submodule request the shared IOC4 IRQ, but interrupt
350 * is generated by a different subdevice.
353 writel(~0, &idd->idd_misc_regs->other_iec.raw);
354 writel(~0, &idd->idd_misc_regs->sio_iec);
355 /* Clear (i.e. acknowledge) */
356 writel(~0, &idd->idd_misc_regs->other_ir.raw);
357 writel(~0, &idd->idd_misc_regs->sio_ir);
359 /* Track PCI-device specific data */
360 idd->idd_serial_data = NULL;
361 pci_set_drvdata(idd->idd_pdev, idd);
363 mutex_lock(&ioc4_mutex);
364 list_add_tail(&idd->idd_list, &ioc4_devices);
366 /* Add this IOC4 to all submodules */
367 list_for_each_entry(is, &ioc4_submodules, is_list) {
368 if (is->is_probe && is->is_probe(idd)) {
370 "%s: IOC4 submodule 0x%s probe failed "
372 __FUNCTION__, module_name(is->is_owner),
373 pci_name(idd->idd_pdev));
376 mutex_unlock(&ioc4_mutex);
381 release_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs));
385 pci_disable_device(pdev);
390 /* Removes a particular instance of an IOC4 card. */
392 ioc4_remove(struct pci_dev *pdev)
394 struct ioc4_submodule *is;
395 struct ioc4_driver_data *idd;
397 idd = pci_get_drvdata(pdev);
399 /* Remove this IOC4 from all submodules */
400 mutex_lock(&ioc4_mutex);
401 list_for_each_entry(is, &ioc4_submodules, is_list) {
402 if (is->is_remove && is->is_remove(idd)) {
404 "%s: IOC4 submodule 0x%s remove failed "
406 __FUNCTION__, module_name(is->is_owner),
407 pci_name(idd->idd_pdev));
410 mutex_unlock(&ioc4_mutex);
412 /* Release resources */
413 iounmap(idd->idd_misc_regs);
414 if (!idd->idd_bar0) {
416 "%s: Unable to get IOC4 misc mapping for pci_dev %s. "
417 "Device removal may be incomplete.\n",
418 __FUNCTION__, pci_name(idd->idd_pdev));
420 release_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs));
422 /* Disable IOC4 and relinquish */
423 pci_disable_device(pdev);
425 /* Remove and free driver data */
426 mutex_lock(&ioc4_mutex);
427 list_del(&idd->idd_list);
428 mutex_unlock(&ioc4_mutex);
432 static struct pci_device_id ioc4_id_table[] = {
433 {PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC4, PCI_ANY_ID,
434 PCI_ANY_ID, 0x0b4000, 0xFFFFFF},
438 static struct pci_driver ioc4_driver = {
440 .id_table = ioc4_id_table,
442 .remove = ioc4_remove,
445 MODULE_DEVICE_TABLE(pci, ioc4_id_table);
447 /*********************
448 * Module management *
449 *********************/
455 return pci_register_driver(&ioc4_driver);
459 static void __devexit
462 pci_unregister_driver(&ioc4_driver);
465 module_init(ioc4_init);
466 module_exit(ioc4_exit);
468 MODULE_AUTHOR("Brent Casavant - Silicon Graphics, Inc. <bcasavan@sgi.com>");
469 MODULE_DESCRIPTION("PCI driver master module for SGI IOC4 Base-IO Card");
470 MODULE_LICENSE("GPL");
472 EXPORT_SYMBOL(ioc4_register_submodule);
473 EXPORT_SYMBOL(ioc4_unregister_submodule);