1 /*
2 * drivers.c
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Copyright (c) 1999 The Puffin Group
10 * Copyright (c) 2001 Matthew Wilcox for Hewlett Packard
11 * Copyright (c) 2001 Helge Deller <deller@gmx.de>
12 * Copyright (c) 2001,2002 Ryan Bradetich
13 *
14 * The file handles registering devices and drivers, then matching them.
15 * It's the closest we get to a dating agency.
16 */
17
18 #include <linux/slab.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/pci.h>
22 #include <linux/spinlock.h>
23 #include <linux/string.h>
24 #include <asm/hardware.h>
25 #include <asm/io.h>
26 #include <asm/pdc.h>
27
28 /* See comments in include/asm-parisc/pci.h */
29 struct pci_dma_ops *hppa_dma_ops;
30
31 static struct parisc_driver *pa_drivers;
32 static struct parisc_device root;
33
34 /* This lock protects the pa_drivers list _only_ since all parisc_devices
35 * are registered before smp_init() is called. If you wish to add devices
36 * after that, this muct be serialised somehow. I recommend a semaphore
37 * rather than a spinlock since driver ->probe functions are allowed to
38 * sleep (for example when allocating memory).
39 */
40 static spinlock_t pa_lock = SPIN_LOCK_UNLOCKED;
41
42 #define for_each_padev(dev) \
43 for (dev = root.child; dev != NULL; dev = next_dev(dev))
44
45 #define check_dev(dev) \
46 (dev->id.hw_type != HPHW_FAULTY) ? dev : next_dev(dev)
47
48 /**
49 * next_dev - enumerates registered devices
50 * @dev: the previous device returned from next_dev
51 *
52 * next_dev does a depth-first search of the tree, returning parents
53 * before children. Returns NULL when there are no more devices.
54 */
next_dev(struct parisc_device * dev)55 struct parisc_device *next_dev(struct parisc_device *dev)
56 {
57 if (dev->child) {
58 return check_dev(dev->child);
59 } else if (dev->sibling) {
60 return dev->sibling;
61 }
62
63 /* Exhausted tree at this level, time to go up. */
64 do {
65 dev = dev->parent;
66 if (dev && dev->sibling)
67 return dev->sibling;
68 } while (dev != &root);
69
70 return NULL;
71 }
72
73 /**
74 * match_device - Report whether this driver can handle this device
75 * @driver: the PA-RISC driver to try
76 * @dev: the PA-RISC device to try
77 */
match_device(struct parisc_driver * driver,struct parisc_device * dev)78 static int match_device(struct parisc_driver *driver, struct parisc_device *dev)
79 {
80 const struct parisc_device_id *ids;
81
82 for (ids = driver->id_table; ids->sversion; ids++) {
83 if ((ids->sversion != SVERSION_ANY_ID) &&
84 (ids->sversion != dev->id.sversion))
85 continue;
86
87 if ((ids->hw_type != HWTYPE_ANY_ID) &&
88 (ids->hw_type != dev->id.hw_type))
89 continue;
90
91 if ((ids->hversion != HVERSION_ANY_ID) &&
92 (ids->hversion != dev->id.hversion))
93 continue;
94
95 return 1;
96 }
97 return 0;
98 }
99
claim_device(struct parisc_driver * driver,struct parisc_device * dev)100 static void claim_device(struct parisc_driver *driver, struct parisc_device *dev)
101 {
102 dev->driver = driver;
103 request_mem_region(dev->hpa, 0x1000, driver->name);
104 }
105
106 /**
107 * register_parisc_driver - Register this driver if it can handle a device
108 * @driver: the PA-RISC driver to try
109 */
register_parisc_driver(struct parisc_driver * driver)110 int register_parisc_driver(struct parisc_driver *driver)
111 {
112 struct parisc_device *device;
113
114 if (driver->next) {
115 printk(KERN_WARNING
116 "BUG: Skipping previously registered driver: %s\n",
117 driver->name);
118 return 1;
119 }
120
121 for_each_padev(device) {
122 if (device->driver)
123 continue;
124 if (!match_device(driver, device))
125 continue;
126
127 if (driver->probe(device) < 0)
128 continue;
129 claim_device(driver, device);
130 }
131
132 /* Note that the list is in reverse order of registration. This
133 * may be significant if we ever actually support hotplug and have
134 * multiple drivers capable of claiming the same chip.
135 */
136
137 spin_lock(&pa_lock);
138 driver->next = pa_drivers;
139 pa_drivers = driver;
140 spin_unlock(&pa_lock);
141
142 return 0;
143 }
144
145 /**
146 * count_parisc_driver - count # of devices this driver would match
147 * @driver: the PA-RISC driver to try
148 *
149 * Use by IOMMU support to "guess" the right size IOPdir.
150 * Formula is something like memsize/(num_iommu * entry_size).
151 */
count_parisc_driver(struct parisc_driver * driver)152 int count_parisc_driver(struct parisc_driver *driver)
153 {
154 struct parisc_device *device;
155 int cnt = 0;
156
157 for_each_padev(device) {
158 if (match_device(driver, device))
159 cnt++;
160 }
161
162 return cnt;
163 }
164
165
166
167 /**
168 * unregister_parisc_driver - Unregister this driver from the list of drivers
169 * @driver: the PA-RISC driver to unregister
170 */
unregister_parisc_driver(struct parisc_driver * driver)171 int unregister_parisc_driver(struct parisc_driver *driver)
172 {
173 struct parisc_device *dev;
174
175 spin_lock(&pa_lock);
176
177 if (pa_drivers == driver) {
178 /* was head of list - update head */
179 pa_drivers = driver->next;
180 } else {
181 struct parisc_driver *prev = pa_drivers;
182
183 while (prev && driver != prev->next) {
184 prev = prev->next;
185 }
186
187 if (!prev) {
188 printk(KERN_WARNING "unregister_parisc_driver: %s wasn't registered\n", driver->name);
189 } else {
190 /* Drop driver from list */
191 prev->next = driver->next;
192 driver->next = NULL;
193 }
194
195 }
196
197 spin_unlock(&pa_lock);
198
199 for_each_padev(dev) {
200 if (dev->driver != driver)
201 continue;
202 dev->driver = NULL;
203 release_mem_region(dev->hpa, 0x1000);
204 }
205
206 return 0;
207 }
208
find_device_by_addr(unsigned long hpa)209 static struct parisc_device *find_device_by_addr(unsigned long hpa)
210 {
211 struct parisc_device *dev;
212 for_each_padev(dev) {
213 if (dev->hpa == hpa)
214 return dev;
215 }
216 return NULL;
217 }
218
219 /**
220 * find_pa_parent_type - Find a parent of a specific type
221 * @dev: The device to start searching from
222 * @type: The device type to search for.
223 *
224 * Walks up the device tree looking for a device of the specified type.
225 * If it finds it, it returns it. If not, it returns NULL.
226 */
find_pa_parent_type(const struct parisc_device * dev,int type)227 const struct parisc_device *find_pa_parent_type(const struct parisc_device *dev, int type)
228 {
229 while (dev != &root) {
230 if (dev->id.hw_type == type)
231 return dev;
232 dev = dev->parent;
233 }
234
235 return NULL;
236 }
237
238 static void
get_node_path(struct parisc_device * dev,struct hardware_path * path)239 get_node_path(struct parisc_device *dev, struct hardware_path *path)
240 {
241 int i = 5;
242 memset(&path->bc, -1, 6);
243 while (dev != &root) {
244 path->bc[i--] = dev->hw_path;
245 dev = dev->parent;
246 }
247 }
248
print_hwpath(struct hardware_path * path,char * output)249 static char *print_hwpath(struct hardware_path *path, char *output)
250 {
251 int i;
252 for (i = 0; i < 6; i++) {
253 if (path->bc[i] == -1)
254 continue;
255 output += sprintf(output, "%u/", (unsigned char) path->bc[i]);
256 }
257 output += sprintf(output, "%u", (unsigned char) path->mod);
258 return output;
259 }
260
261 /**
262 * print_pa_hwpath - Returns hardware path for PA devices
263 * dev: The device to return the path for
264 * output: Pointer to a previously-allocated array to place the path in.
265 *
266 * This function fills in the output array with a human-readable path
267 * to a PA device. This string is compatible with that used by PDC, and
268 * may be printed on the outside of the box.
269 */
print_pa_hwpath(struct parisc_device * dev,char * output)270 char *print_pa_hwpath(struct parisc_device *dev, char *output)
271 {
272 struct hardware_path path;
273
274 get_node_path(dev->parent, &path);
275 path.mod = dev->hw_path;
276 return print_hwpath(&path, output);
277 }
278
279
280 #if defined(CONFIG_PCI) || defined(CONFIG_ISA)
281 /**
282 * get_pci_node_path - Returns hardware path for PCI devices
283 * dev: The device to return the path for
284 * output: Pointer to a previously-allocated array to place the path in.
285 *
286 * This function fills in the hardware_path structure with the route to
287 * the specified PCI device. This structure is suitable for passing to
288 * PDC calls.
289 */
get_pci_node_path(struct pci_dev * dev,struct hardware_path * path)290 void get_pci_node_path(struct pci_dev *dev, struct hardware_path *path)
291 {
292 struct pci_bus *bus;
293 const struct parisc_device *padev;
294 int i = 5;
295
296 memset(&path->bc, -1, 6);
297 path->mod = PCI_FUNC(dev->devfn);
298 path->bc[i--] = PCI_SLOT(dev->devfn);
299 for (bus = dev->bus; bus->parent; bus = bus->parent) {
300 unsigned int devfn = bus->self->devfn;
301 path->bc[i--] = PCI_SLOT(devfn) | (PCI_FUNC(devfn) << 5);
302 }
303
304 padev = HBA_DATA(bus->sysdata)->dev;
305 while (padev != &root) {
306 path->bc[i--] = padev->hw_path;
307 padev = padev->parent;
308 }
309 }
310
311 /**
312 * print_pci_hwpath - Returns hardware path for PCI devices
313 * dev: The device to return the path for
314 * output: Pointer to a previously-allocated array to place the path in.
315 *
316 * This function fills in the output array with a human-readable path
317 * to a PCI device. This string is compatible with that used by PDC, and
318 * may be printed on the outside of the box.
319 */
print_pci_hwpath(struct pci_dev * dev,char * output)320 char *print_pci_hwpath(struct pci_dev *dev, char *output)
321 {
322 struct hardware_path path;
323
324 get_pci_node_path(dev, &path);
325 return print_hwpath(&path, output);
326 }
327 #endif /* defined(CONFIG_PCI) || defined(CONFIG_ISA) */
328
329
create_tree_node(char id,struct parisc_device * parent,struct parisc_device ** insert)330 struct parisc_device * create_tree_node(char id, struct parisc_device *parent,
331 struct parisc_device **insert)
332 {
333 struct parisc_device *dev = kmalloc(sizeof(*dev), GFP_KERNEL);
334 if (!dev)
335 return NULL;
336 memset(dev, 0, sizeof(*dev));
337 dev->hw_path = id;
338 dev->id.hw_type = HPHW_FAULTY;
339 dev->parent = parent;
340 dev->sibling = *insert;
341 *insert = dev;
342 return dev;
343 }
344
345 /**
346 * alloc_tree_node - returns a device entry in the iotree
347 * @parent: the parent node in the tree
348 * @id: the element of the module path for this entry
349 *
350 * Checks all the children of @parent for a matching @id. If none
351 * found, it allocates a new device and returns it.
352 */
353 struct parisc_device *
alloc_tree_node(struct parisc_device * parent,char id)354 alloc_tree_node(struct parisc_device *parent, char id)
355 {
356 struct parisc_device *prev;
357 if ((!parent->child) || (parent->child->hw_path > id)) {
358 return create_tree_node(id, parent, &parent->child);
359 }
360
361 prev = parent->child;
362 if (prev->hw_path == id)
363 return prev;
364
365 while (prev->sibling && prev->sibling->hw_path < id) {
366 prev = prev->sibling;
367 }
368
369 if ((prev->sibling) && (prev->sibling->hw_path == id))
370 return prev->sibling;
371
372 return create_tree_node(id, parent, &prev->sibling);
373 }
374
find_parisc_device(struct hardware_path * modpath)375 static struct parisc_device *find_parisc_device(struct hardware_path *modpath)
376 {
377 int i;
378 struct parisc_device *parent = &root;
379 for (i = 0; i < 6; i++) {
380 if (modpath->bc[i] == -1)
381 continue;
382 parent = alloc_tree_node(parent, modpath->bc[i]);
383 }
384 return alloc_tree_node(parent, modpath->mod);
385 }
386
387 struct parisc_device *
alloc_pa_dev(unsigned long hpa,struct hardware_path * mod_path)388 alloc_pa_dev(unsigned long hpa, struct hardware_path *mod_path)
389 {
390 int status;
391 unsigned long bytecnt;
392 u8 iodc_data[32];
393 struct parisc_device *dev;
394 const char *name;
395
396 /* Check to make sure this device has not already been added - Ryan */
397 if (find_device_by_addr(hpa) != NULL)
398 return NULL;
399
400 status = pdc_iodc_read(&bytecnt, hpa, 0, &iodc_data, 32);
401 if (status != PDC_OK)
402 return NULL;
403
404 dev = find_parisc_device(mod_path);
405 if (dev->id.hw_type != HPHW_FAULTY) {
406 char p[64];
407 print_pa_hwpath(dev, p);
408 printk("Two devices have hardware path %s. Please file a bug with HP.\n"
409 "In the meantime, you could try rearranging your cards.\n", p);
410 return NULL;
411 }
412
413 dev->id.hw_type = iodc_data[3] & 0x1f;
414 dev->id.hversion = (iodc_data[0] << 4) | ((iodc_data[1] & 0xf0) >> 4);
415 dev->id.hversion_rev = iodc_data[1] & 0x0f;
416 dev->id.sversion = ((iodc_data[4] & 0x0f) << 16) |
417 (iodc_data[5] << 8) | iodc_data[6];
418 dev->hpa = hpa;
419
420 name = parisc_hardware_description(&dev->id);
421 if (name) {
422 strncpy(dev->name, name, sizeof(dev->name)-1);
423 }
424
425 return dev;
426 }
427
428 /**
429 * register_parisc_device - Locate a driver to manage this device.
430 * @dev: The parisc device.
431 *
432 * Search the driver list for a driver that is willing to manage
433 * this device.
434 */
register_parisc_device(struct parisc_device * dev)435 int register_parisc_device(struct parisc_device *dev)
436 {
437 struct parisc_driver *driver;
438
439 if (!dev)
440 return 0;
441
442 if (dev->driver)
443 return 1;
444
445 spin_lock(&pa_lock);
446
447 /* Locate a driver which agrees to manage this device. */
448 for (driver = pa_drivers; driver; driver = driver->next) {
449 if (!match_device(driver,dev))
450 continue;
451 if (driver->probe(dev) == 0)
452 break;
453 }
454
455 if (driver != NULL) {
456 claim_device(driver, dev);
457 }
458 spin_unlock(&pa_lock);
459 return driver != NULL;
460 }
461
462 #define BC_PORT_MASK 0x8
463 #define BC_LOWER_PORT 0x8
464
465 #define IO_STATUS offsetof(struct bc_module, io_status)
466
467
468 #define BUS_CONVERTER(dev) \
469 ((dev->id.hw_type == HPHW_IOA) || (dev->id.hw_type == HPHW_BCPORT))
470
471 #define IS_LOWER_PORT(dev) \
472 ((__raw_readl(dev->hpa + IO_STATUS) & BC_PORT_MASK) == BC_LOWER_PORT)
473
474 #define MAX_NATIVE_DEVICES 64
475 #define NATIVE_DEVICE_OFFSET 0x1000
476
477 #define FLEX_MASK (unsigned long)0xfffffffffffc0000
478 #define IO_IO_LOW offsetof(struct bc_module, io_io_low)
479 #define IO_IO_HIGH offsetof(struct bc_module, io_io_high)
480 #define READ_IO_IO_LOW(dev) (unsigned long)(signed int)__raw_readl(dev->hpa + IO_IO_LOW)
481 #define READ_IO_IO_HIGH(dev) (unsigned long)(signed int)__raw_readl(dev->hpa + IO_IO_HIGH)
482
483 static void walk_native_bus(unsigned long io_io_low, unsigned long io_io_high,
484 struct parisc_device *parent);
485
walk_lower_bus(struct parisc_device * dev)486 void walk_lower_bus(struct parisc_device *dev)
487 {
488 unsigned long io_io_low, io_io_high;
489
490 if(!BUS_CONVERTER(dev) || IS_LOWER_PORT(dev))
491 return;
492
493 if(dev->id.hw_type == HPHW_IOA) {
494 io_io_low = (unsigned long)(signed int)(READ_IO_IO_LOW(dev) << 16);
495 io_io_high = io_io_low + MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET;
496 } else {
497 io_io_low = (READ_IO_IO_LOW(dev) + ~FLEX_MASK) & FLEX_MASK;
498 io_io_high = (READ_IO_IO_HIGH(dev)+ ~FLEX_MASK) & FLEX_MASK;
499 }
500
501 walk_native_bus(io_io_low, io_io_high, dev);
502 }
503
504 /**
505 * walk_native_bus -- Probe a bus for devices
506 * @io_io_low: Base address of this bus.
507 * @io_io_high: Last address of this bus.
508 * @parent: The parent bus device.
509 *
510 * A native bus (eg Runway or GSC) may have up to 64 devices on it,
511 * spaced at intervals of 0x1000 bytes. PDC may not inform us of these
512 * devices, so we have to probe for them. Unfortunately, we may find
513 * devices which are not physically connected (such as extra serial &
514 * keyboard ports). This problem is not yet solved.
515 */
walk_native_bus(unsigned long io_io_low,unsigned long io_io_high,struct parisc_device * parent)516 static void walk_native_bus(unsigned long io_io_low, unsigned long io_io_high,
517 struct parisc_device *parent)
518 {
519 int i, devices_found = 0;
520 unsigned long hpa = io_io_low;
521 struct hardware_path path;
522
523 get_node_path(parent, &path);
524 do {
525 for(i = 0; i < MAX_NATIVE_DEVICES; i++, hpa += NATIVE_DEVICE_OFFSET) {
526 struct parisc_device *dev;
527
528 /* Was the device already added by Firmware? */
529 dev = find_device_by_addr(hpa);
530 if (!dev) {
531 path.mod = i;
532 dev = alloc_pa_dev(hpa, &path);
533 if (!dev)
534 continue;
535
536 register_parisc_device(dev);
537 devices_found++;
538 }
539 walk_lower_bus(dev);
540 }
541 } while(!devices_found && hpa < io_io_high);
542 }
543
544 #define CENTRAL_BUS_ADDR (unsigned long) 0xfffffffffff80000
545
546 /**
547 * walk_central_bus - Find devices attached to the central bus
548 *
549 * PDC doesn't tell us about all devices in the system. This routine
550 * finds devices connected to the central bus.
551 */
walk_central_bus(void)552 void walk_central_bus(void)
553 {
554 walk_native_bus(CENTRAL_BUS_ADDR,
555 CENTRAL_BUS_ADDR + (MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET),
556 &root);
557 }
558
fixup_child_irqs(struct parisc_device * parent,int base,int (* choose_irq)(struct parisc_device *))559 void fixup_child_irqs(struct parisc_device *parent, int base,
560 int (*choose_irq)(struct parisc_device *))
561 {
562 struct parisc_device *dev;
563
564 if (!parent->child)
565 return;
566
567 for (dev = check_dev(parent->child); dev; dev = dev->sibling) {
568 int irq = choose_irq(dev);
569 if (irq > 0) {
570 #ifdef __LP64__
571 irq += 32;
572 #endif
573 dev->irq = base + irq;
574 }
575 }
576 }
577
print_parisc_device(struct parisc_device * dev)578 static void print_parisc_device(struct parisc_device *dev)
579 {
580 char hw_path[64];
581 static int count;
582
583 print_pa_hwpath(dev, hw_path);
584 printk(KERN_INFO "%d. %s (%d) at 0x%lx [%s], versions 0x%x, 0x%x, 0x%x",
585 ++count, dev->name, dev->id.hw_type, dev->hpa, hw_path,
586 dev->id.hversion, dev->id.hversion_rev, dev->id.sversion);
587
588 if (dev->num_addrs) {
589 int k;
590 printk(", additional addresses: ");
591 for (k = 0; k < dev->num_addrs; k++)
592 printk("0x%lx ", dev->addr[k]);
593 }
594 printk("\n");
595 }
596
print_subdevices(struct parisc_device * parent)597 void print_subdevices(struct parisc_device *parent)
598 {
599 struct parisc_device *dev;
600 for (dev = parent->child; dev != parent->sibling; dev = next_dev(dev)) {
601 print_parisc_device(dev);
602 }
603 }
604
605 /**
606 * print_parisc_devices - Print out a list of devices found in this system
607 */
print_parisc_devices(void)608 void print_parisc_devices(void)
609 {
610 struct parisc_device *dev;
611 for_each_padev(dev) {
612 print_parisc_device(dev);
613 }
614 }
615