1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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/slab.h>
34#include <linux/mutex.h>
35#include <linux/time.h>
36#include <asm/io.h>
37
38
39
40
41
42
43
44
45#define IOC4_CALIBRATE_COUNT 63
46#define IOC4_CALIBRATE_CYCLES 256
47#define IOC4_CALIBRATE_DISCARD 2
48#define IOC4_CALIBRATE_LOW_MHZ 25
49#define IOC4_CALIBRATE_HIGH_MHZ 75
50#define IOC4_CALIBRATE_DEFAULT_MHZ 66
51
52
53
54
55
56static DEFINE_MUTEX(ioc4_mutex);
57
58static LIST_HEAD(ioc4_devices);
59static LIST_HEAD(ioc4_submodules);
60
61
62int
63ioc4_register_submodule(struct ioc4_submodule *is)
64{
65 struct ioc4_driver_data *idd;
66
67 mutex_lock(&ioc4_mutex);
68 list_add(&is->is_list, &ioc4_submodules);
69
70
71 if (!is->is_probe)
72 goto out;
73
74 list_for_each_entry(idd, &ioc4_devices, idd_list) {
75 if (is->is_probe(idd)) {
76 printk(KERN_WARNING
77 "%s: IOC4 submodule %s probe failed "
78 "for pci_dev %s",
79 __func__, module_name(is->is_owner),
80 pci_name(idd->idd_pdev));
81 }
82 }
83 out:
84 mutex_unlock(&ioc4_mutex);
85 return 0;
86}
87
88
89void
90ioc4_unregister_submodule(struct ioc4_submodule *is)
91{
92 struct ioc4_driver_data *idd;
93
94 mutex_lock(&ioc4_mutex);
95 list_del(&is->is_list);
96
97
98 if (!is->is_remove)
99 goto out;
100
101 list_for_each_entry(idd, &ioc4_devices, idd_list) {
102 if (is->is_remove(idd)) {
103 printk(KERN_WARNING
104 "%s: IOC4 submodule %s remove failed "
105 "for pci_dev %s.\n",
106 __func__, module_name(is->is_owner),
107 pci_name(idd->idd_pdev));
108 }
109 }
110 out:
111 mutex_unlock(&ioc4_mutex);
112}
113
114
115
116
117
118#define IOC4_CALIBRATE_LOW_LIMIT \
119 (1000*IOC4_EXTINT_COUNT_DIVISOR/IOC4_CALIBRATE_LOW_MHZ)
120#define IOC4_CALIBRATE_HIGH_LIMIT \
121 (1000*IOC4_EXTINT_COUNT_DIVISOR/IOC4_CALIBRATE_HIGH_MHZ)
122#define IOC4_CALIBRATE_DEFAULT \
123 (1000*IOC4_EXTINT_COUNT_DIVISOR/IOC4_CALIBRATE_DEFAULT_MHZ)
124
125#define IOC4_CALIBRATE_END \
126 (IOC4_CALIBRATE_CYCLES + IOC4_CALIBRATE_DISCARD)
127
128#define IOC4_INT_OUT_MODE_TOGGLE 0x7
129
130
131
132
133
134
135
136
137
138
139
140
141
142static void
143ioc4_clock_calibrate(struct ioc4_driver_data *idd)
144{
145 union ioc4_int_out int_out;
146 union ioc4_gpcr gpcr;
147 unsigned int state, last_state = 1;
148 struct timespec start_ts, end_ts;
149 uint64_t start, end, period;
150 unsigned int count = 0;
151
152
153 gpcr.raw = 0;
154 gpcr.fields.dir = IOC4_GPCR_DIR_0;
155 gpcr.fields.int_out_en = 1;
156 writel(gpcr.raw, &idd->idd_misc_regs->gpcr_s.raw);
157
158
159 writel(0, &idd->idd_misc_regs->int_out.raw);
160 mmiowb();
161
162
163 int_out.raw = 0;
164 int_out.fields.count = IOC4_CALIBRATE_COUNT;
165 int_out.fields.mode = IOC4_INT_OUT_MODE_TOGGLE;
166 int_out.fields.diag = 0;
167 writel(int_out.raw, &idd->idd_misc_regs->int_out.raw);
168 mmiowb();
169
170
171 do {
172 int_out.raw = readl(&idd->idd_misc_regs->int_out.raw);
173 state = int_out.fields.int_out;
174 if (!last_state && state) {
175 count++;
176 if (count == IOC4_CALIBRATE_END) {
177 ktime_get_ts(&end_ts);
178 break;
179 } else if (count == IOC4_CALIBRATE_DISCARD)
180 ktime_get_ts(&start_ts);
181 }
182 last_state = state;
183 } while (1);
184
185
186
187
188
189
190
191
192
193
194
195 end = end_ts.tv_sec * NSEC_PER_SEC + end_ts.tv_nsec;
196 start = start_ts.tv_sec * NSEC_PER_SEC + start_ts.tv_nsec;
197 period = (end - start) /
198 (IOC4_CALIBRATE_CYCLES * 2 * (IOC4_CALIBRATE_COUNT + 1));
199
200
201 if (period > IOC4_CALIBRATE_LOW_LIMIT ||
202 period < IOC4_CALIBRATE_HIGH_LIMIT) {
203 printk(KERN_INFO
204 "IOC4 %s: Clock calibration failed. Assuming"
205 "PCI clock is %d ns.\n",
206 pci_name(idd->idd_pdev),
207 IOC4_CALIBRATE_DEFAULT / IOC4_EXTINT_COUNT_DIVISOR);
208 period = IOC4_CALIBRATE_DEFAULT;
209 } else {
210 u64 ns = period;
211
212 do_div(ns, IOC4_EXTINT_COUNT_DIVISOR);
213 printk(KERN_DEBUG
214 "IOC4 %s: PCI clock is %llu ns.\n",
215 pci_name(idd->idd_pdev), (unsigned long long)ns);
216 }
217
218
219
220
221
222
223 idd->count_period = period;
224}
225
226
227
228
229
230
231
232
233
234static unsigned int
235ioc4_variant(struct ioc4_driver_data *idd)
236{
237 struct pci_dev *pdev = NULL;
238 int found = 0;
239
240
241 do {
242 pdev = pci_get_device(PCI_VENDOR_ID_QLOGIC,
243 PCI_DEVICE_ID_QLOGIC_ISP12160, pdev);
244 if (pdev &&
245 idd->idd_pdev->bus->number == pdev->bus->number &&
246 3 == PCI_SLOT(pdev->devfn))
247 found = 1;
248 } while (pdev && !found);
249 if (NULL != pdev) {
250 pci_dev_put(pdev);
251 return IOC4_VARIANT_IO9;
252 }
253
254
255 pdev = NULL;
256 do {
257 pdev = pci_get_device(PCI_VENDOR_ID_VITESSE,
258 PCI_DEVICE_ID_VITESSE_VSC7174, pdev);
259 if (pdev &&
260 idd->idd_pdev->bus->number == pdev->bus->number &&
261 3 == PCI_SLOT(pdev->devfn))
262 found = 1;
263 } while (pdev && !found);
264 if (NULL != pdev) {
265 pci_dev_put(pdev);
266 return IOC4_VARIANT_IO10;
267 }
268
269
270 return IOC4_VARIANT_PCI_RT;
271}
272
273static void
274ioc4_load_modules(struct work_struct *work)
275{
276 request_module("sgiioc4");
277}
278
279static DECLARE_WORK(ioc4_load_modules_work, ioc4_load_modules);
280
281
282static int
283ioc4_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id)
284{
285 struct ioc4_driver_data *idd;
286 struct ioc4_submodule *is;
287 uint32_t pcmd;
288 int ret;
289
290
291 if ((ret = pci_enable_device(pdev))) {
292 printk(KERN_WARNING
293 "%s: Failed to enable IOC4 device for pci_dev %s.\n",
294 __func__, pci_name(pdev));
295 goto out;
296 }
297 pci_set_master(pdev);
298
299
300 idd = kmalloc(sizeof(struct ioc4_driver_data), GFP_KERNEL);
301 if (!idd) {
302 printk(KERN_WARNING
303 "%s: Failed to allocate IOC4 data for pci_dev %s.\n",
304 __func__, pci_name(pdev));
305 ret = -ENODEV;
306 goto out_idd;
307 }
308 idd->idd_pdev = pdev;
309 idd->idd_pci_id = pci_id;
310
311
312
313
314 idd->idd_bar0 = pci_resource_start(idd->idd_pdev, 0);
315 if (!idd->idd_bar0) {
316 printk(KERN_WARNING
317 "%s: Unable to find IOC4 misc resource "
318 "for pci_dev %s.\n",
319 __func__, pci_name(idd->idd_pdev));
320 ret = -ENODEV;
321 goto out_pci;
322 }
323 if (!request_mem_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs),
324 "ioc4_misc")) {
325 printk(KERN_WARNING
326 "%s: Unable to request IOC4 misc region "
327 "for pci_dev %s.\n",
328 __func__, pci_name(idd->idd_pdev));
329 ret = -ENODEV;
330 goto out_pci;
331 }
332 idd->idd_misc_regs = ioremap(idd->idd_bar0,
333 sizeof(struct ioc4_misc_regs));
334 if (!idd->idd_misc_regs) {
335 printk(KERN_WARNING
336 "%s: Unable to remap IOC4 misc region "
337 "for pci_dev %s.\n",
338 __func__, pci_name(idd->idd_pdev));
339 ret = -ENODEV;
340 goto out_misc_region;
341 }
342
343
344
345
346 idd->idd_variant = ioc4_variant(idd);
347 printk(KERN_INFO "IOC4 %s: %s card detected.\n", pci_name(pdev),
348 idd->idd_variant == IOC4_VARIANT_IO9 ? "IO9" :
349 idd->idd_variant == IOC4_VARIANT_PCI_RT ? "PCI-RT" :
350 idd->idd_variant == IOC4_VARIANT_IO10 ? "IO10" : "unknown");
351
352
353 pci_read_config_dword(idd->idd_pdev, PCI_COMMAND, &pcmd);
354 pci_write_config_dword(idd->idd_pdev, PCI_COMMAND,
355 pcmd | PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
356
357
358 ioc4_clock_calibrate(idd);
359
360
361
362
363
364
365 writel(~0, &idd->idd_misc_regs->other_iec.raw);
366 writel(~0, &idd->idd_misc_regs->sio_iec);
367
368 writel(~0, &idd->idd_misc_regs->other_ir.raw);
369 writel(~0, &idd->idd_misc_regs->sio_ir);
370
371
372 idd->idd_serial_data = NULL;
373 pci_set_drvdata(idd->idd_pdev, idd);
374
375 mutex_lock(&ioc4_mutex);
376 list_add_tail(&idd->idd_list, &ioc4_devices);
377
378
379 list_for_each_entry(is, &ioc4_submodules, is_list) {
380 if (is->is_probe && is->is_probe(idd)) {
381 printk(KERN_WARNING
382 "%s: IOC4 submodule 0x%s probe failed "
383 "for pci_dev %s.\n",
384 __func__, module_name(is->is_owner),
385 pci_name(idd->idd_pdev));
386 }
387 }
388 mutex_unlock(&ioc4_mutex);
389
390
391
392
393
394
395
396 if (idd->idd_variant != IOC4_VARIANT_PCI_RT) {
397
398
399
400
401 printk(KERN_INFO "IOC4 loading sgiioc4 submodule\n");
402 schedule_work(&ioc4_load_modules_work);
403 }
404
405 return 0;
406
407out_misc_region:
408 release_mem_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs));
409out_pci:
410 kfree(idd);
411out_idd:
412 pci_disable_device(pdev);
413out:
414 return ret;
415}
416
417
418static void
419ioc4_remove(struct pci_dev *pdev)
420{
421 struct ioc4_submodule *is;
422 struct ioc4_driver_data *idd;
423
424 idd = pci_get_drvdata(pdev);
425
426
427 mutex_lock(&ioc4_mutex);
428 list_for_each_entry(is, &ioc4_submodules, is_list) {
429 if (is->is_remove && is->is_remove(idd)) {
430 printk(KERN_WARNING
431 "%s: IOC4 submodule 0x%s remove failed "
432 "for pci_dev %s.\n",
433 __func__, module_name(is->is_owner),
434 pci_name(idd->idd_pdev));
435 }
436 }
437 mutex_unlock(&ioc4_mutex);
438
439
440 iounmap(idd->idd_misc_regs);
441 if (!idd->idd_bar0) {
442 printk(KERN_WARNING
443 "%s: Unable to get IOC4 misc mapping for pci_dev %s. "
444 "Device removal may be incomplete.\n",
445 __func__, pci_name(idd->idd_pdev));
446 }
447 release_mem_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs));
448
449
450 pci_disable_device(pdev);
451
452
453 mutex_lock(&ioc4_mutex);
454 list_del(&idd->idd_list);
455 mutex_unlock(&ioc4_mutex);
456 kfree(idd);
457}
458
459static struct pci_device_id ioc4_id_table[] = {
460 {PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC4, PCI_ANY_ID,
461 PCI_ANY_ID, 0x0b4000, 0xFFFFFF},
462 {0}
463};
464
465static struct pci_driver ioc4_driver = {
466 .name = "IOC4",
467 .id_table = ioc4_id_table,
468 .probe = ioc4_probe,
469 .remove = ioc4_remove,
470};
471
472MODULE_DEVICE_TABLE(pci, ioc4_id_table);
473
474
475
476
477
478
479static int __init
480ioc4_init(void)
481{
482 return pci_register_driver(&ioc4_driver);
483}
484
485
486static void __exit
487ioc4_exit(void)
488{
489
490 flush_work(&ioc4_load_modules_work);
491 pci_unregister_driver(&ioc4_driver);
492}
493
494module_init(ioc4_init);
495module_exit(ioc4_exit);
496
497MODULE_AUTHOR("Brent Casavant - Silicon Graphics, Inc. <bcasavan@sgi.com>");
498MODULE_DESCRIPTION("PCI driver master module for SGI IOC4 Base-IO Card");
499MODULE_LICENSE("GPL");
500
501EXPORT_SYMBOL(ioc4_register_submodule);
502EXPORT_SYMBOL(ioc4_unregister_submodule);
503