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.
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
14 * The file handles registering devices and drivers, then matching them.
15 * It's the closest we get to a dating agency.
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>
27 #include <asm/parisc-device.h>
29 /* See comments in include/asm-parisc/pci.h */
30 struct hppa_dma_ops *hppa_dma_ops;
31 EXPORT_SYMBOL(hppa_dma_ops);
33 static struct parisc_device root;
35 #define for_each_padev(dev) \
36 for (dev = root.child; dev != NULL; dev = next_dev(dev))
38 #define check_dev(dev) \
39 (dev->id.hw_type != HPHW_FAULTY) ? dev : next_dev(dev)
42 * next_dev - enumerates registered devices
43 * @dev: the previous device returned from next_dev
45 * next_dev does a depth-first search of the tree, returning parents
46 * before children. Returns NULL when there are no more devices.
48 struct parisc_device *next_dev(struct parisc_device *dev)
51 return check_dev(dev->child);
52 } else if (dev->sibling) {
56 /* Exhausted tree at this level, time to go up. */
59 if (dev && dev->sibling)
61 } while (dev != &root);
67 * match_device - Report whether this driver can handle this device
68 * @driver: the PA-RISC driver to try
69 * @dev: the PA-RISC device to try
71 static int match_device(struct parisc_driver *driver, struct parisc_device *dev)
73 const struct parisc_device_id *ids;
75 for (ids = driver->id_table; ids->sversion; ids++) {
76 if ((ids->sversion != SVERSION_ANY_ID) &&
77 (ids->sversion != dev->id.sversion))
80 if ((ids->hw_type != HWTYPE_ANY_ID) &&
81 (ids->hw_type != dev->id.hw_type))
84 if ((ids->hversion != HVERSION_ANY_ID) &&
85 (ids->hversion != dev->id.hversion))
93 static void claim_device(struct parisc_driver *driver, struct parisc_device *dev)
96 request_mem_region(dev->hpa, 0x1000, driver->name);
99 static int parisc_driver_probe(struct device *dev)
102 struct parisc_device *pa_dev = to_parisc_device(dev);
103 struct parisc_driver *pa_drv = to_parisc_driver(dev->driver);
105 rc = pa_drv->probe(pa_dev);
108 claim_device(pa_drv, pa_dev);
113 static int parisc_driver_remove(struct device *dev)
115 struct parisc_device *pa_dev = to_parisc_device(dev);
116 struct parisc_driver *pa_drv = to_parisc_driver(dev->driver);
118 pa_drv->remove(pa_dev);
119 release_mem_region(pa_dev->hpa, 0x1000);
126 * register_parisc_driver - Register this driver if it can handle a device
127 * @driver: the PA-RISC driver to try
129 int register_parisc_driver(struct parisc_driver *driver)
131 /* FIXME: we need this because apparently the sti
132 * driver can be registered twice */
133 if(driver->drv.name) {
135 "BUG: skipping previously registered driver %s\n",
140 if (!driver->probe) {
142 "BUG: driver %s has no probe routine\n",
147 driver->drv.bus = &parisc_bus_type;
149 /* We install our own probe and remove routines */
150 WARN_ON(driver->drv.probe != NULL);
151 WARN_ON(driver->drv.remove != NULL);
153 driver->drv.probe = parisc_driver_probe;
154 driver->drv.remove = parisc_driver_remove;
155 driver->drv.name = driver->name;
157 return driver_register(&driver->drv);
159 EXPORT_SYMBOL(register_parisc_driver);
162 * count_parisc_driver - count # of devices this driver would match
163 * @driver: the PA-RISC driver to try
165 * Use by IOMMU support to "guess" the right size IOPdir.
166 * Formula is something like memsize/(num_iommu * entry_size).
168 int count_parisc_driver(struct parisc_driver *driver)
170 struct parisc_device *device;
173 for_each_padev(device) {
174 if (match_device(driver, device))
184 * unregister_parisc_driver - Unregister this driver from the list of drivers
185 * @driver: the PA-RISC driver to unregister
187 int unregister_parisc_driver(struct parisc_driver *driver)
189 driver_unregister(&driver->drv);
192 EXPORT_SYMBOL(unregister_parisc_driver);
194 static struct parisc_device *find_device_by_addr(unsigned long hpa)
196 struct parisc_device *dev;
197 for_each_padev(dev) {
205 * find_pa_parent_type - Find a parent of a specific type
206 * @dev: The device to start searching from
207 * @type: The device type to search for.
209 * Walks up the device tree looking for a device of the specified type.
210 * If it finds it, it returns it. If not, it returns NULL.
212 const struct parisc_device *find_pa_parent_type(const struct parisc_device *dev, int type)
214 while (dev != &root) {
215 if (dev->id.hw_type == type)
224 get_node_path(struct parisc_device *dev, struct hardware_path *path)
227 memset(&path->bc, -1, 6);
228 while (dev != &root) {
229 path->bc[i--] = dev->hw_path;
234 static char *print_hwpath(struct hardware_path *path, char *output)
237 for (i = 0; i < 6; i++) {
238 if (path->bc[i] == -1)
240 output += sprintf(output, "%u/", (unsigned char) path->bc[i]);
242 output += sprintf(output, "%u", (unsigned char) path->mod);
247 * print_pa_hwpath - Returns hardware path for PA devices
248 * dev: The device to return the path for
249 * output: Pointer to a previously-allocated array to place the path in.
251 * This function fills in the output array with a human-readable path
252 * to a PA device. This string is compatible with that used by PDC, and
253 * may be printed on the outside of the box.
255 char *print_pa_hwpath(struct parisc_device *dev, char *output)
257 struct hardware_path path;
259 get_node_path(dev->parent, &path);
260 path.mod = dev->hw_path;
261 return print_hwpath(&path, output);
263 EXPORT_SYMBOL(print_pa_hwpath);
265 #if defined(CONFIG_PCI) || defined(CONFIG_ISA)
267 * get_pci_node_path - Returns hardware path for PCI devices
268 * dev: The device to return the path for
269 * output: Pointer to a previously-allocated array to place the path in.
271 * This function fills in the hardware_path structure with the route to
272 * the specified PCI device. This structure is suitable for passing to
275 void get_pci_node_path(struct pci_dev *dev, struct hardware_path *path)
278 const struct parisc_device *padev;
281 memset(&path->bc, -1, 6);
282 path->mod = PCI_FUNC(dev->devfn);
283 path->bc[i--] = PCI_SLOT(dev->devfn);
284 for (bus = dev->bus; bus->parent; bus = bus->parent) {
285 unsigned int devfn = bus->self->devfn;
286 path->bc[i--] = PCI_SLOT(devfn) | (PCI_FUNC(devfn) << 5);
289 padev = HBA_DATA(bus->bridge->platform_data)->dev;
290 while (padev != &root) {
291 path->bc[i--] = padev->hw_path;
292 padev = padev->parent;
295 EXPORT_SYMBOL(get_pci_node_path);
298 * print_pci_hwpath - Returns hardware path for PCI devices
299 * dev: The device to return the path for
300 * output: Pointer to a previously-allocated array to place the path in.
302 * This function fills in the output array with a human-readable path
303 * to a PCI device. This string is compatible with that used by PDC, and
304 * may be printed on the outside of the box.
306 char *print_pci_hwpath(struct pci_dev *dev, char *output)
308 struct hardware_path path;
310 get_pci_node_path(dev, &path);
311 return print_hwpath(&path, output);
313 EXPORT_SYMBOL(print_pci_hwpath);
315 #endif /* defined(CONFIG_PCI) || defined(CONFIG_ISA) */
318 struct parisc_device * create_tree_node(char id, struct parisc_device *parent,
319 struct parisc_device **insert)
321 struct parisc_device *dev = kmalloc(sizeof(*dev), GFP_KERNEL);
324 memset(dev, 0, sizeof(*dev));
326 dev->id.hw_type = HPHW_FAULTY;
327 dev->parent = parent;
328 dev->sibling = *insert;
334 * alloc_tree_node - returns a device entry in the iotree
335 * @parent: the parent node in the tree
336 * @id: the element of the module path for this entry
338 * Checks all the children of @parent for a matching @id. If none
339 * found, it allocates a new device and returns it.
341 struct parisc_device *
342 alloc_tree_node(struct parisc_device *parent, char id)
344 struct parisc_device *prev;
345 if ((!parent->child) || (parent->child->hw_path > id)) {
346 return create_tree_node(id, parent, &parent->child);
349 prev = parent->child;
350 if (prev->hw_path == id)
353 while (prev->sibling && prev->sibling->hw_path < id) {
354 prev = prev->sibling;
357 if ((prev->sibling) && (prev->sibling->hw_path == id))
358 return prev->sibling;
360 return create_tree_node(id, parent, &prev->sibling);
363 static struct parisc_device *find_parisc_device(struct hardware_path *modpath)
366 struct parisc_device *parent = &root;
367 for (i = 0; i < 6; i++) {
368 if (modpath->bc[i] == -1)
370 parent = alloc_tree_node(parent, modpath->bc[i]);
372 return alloc_tree_node(parent, modpath->mod);
375 struct parisc_device *
376 alloc_pa_dev(unsigned long hpa, struct hardware_path *mod_path)
379 unsigned long bytecnt;
381 struct parisc_device *dev;
384 /* Check to make sure this device has not already been added - Ryan */
385 if (find_device_by_addr(hpa) != NULL)
388 status = pdc_iodc_read(&bytecnt, hpa, 0, &iodc_data, 32);
389 if (status != PDC_OK)
392 dev = find_parisc_device(mod_path);
393 if (dev->id.hw_type != HPHW_FAULTY) {
395 print_pa_hwpath(dev, p);
396 printk("Two devices have hardware path %s. Please file a bug with HP.\n"
397 "In the meantime, you could try rearranging your cards.\n", p);
401 dev->id.hw_type = iodc_data[3] & 0x1f;
402 dev->id.hversion = (iodc_data[0] << 4) | ((iodc_data[1] & 0xf0) >> 4);
403 dev->id.hversion_rev = iodc_data[1] & 0x0f;
404 dev->id.sversion = ((iodc_data[4] & 0x0f) << 16) |
405 (iodc_data[5] << 8) | iodc_data[6];
407 name = parisc_hardware_description(&dev->id);
409 strlcpy(dev->name, name, sizeof(dev->name));
415 static int parisc_generic_match(struct device *dev, struct device_driver *drv)
417 return match_device(to_parisc_driver(drv), to_parisc_device(dev));
420 struct bus_type parisc_bus_type = {
422 .match = parisc_generic_match,
426 * register_parisc_device - Locate a driver to manage this device.
427 * @dev: The parisc device.
429 * Search the driver list for a driver that is willing to manage
432 int register_parisc_device(struct parisc_device *dev)
443 #define BC_PORT_MASK 0x8
444 #define BC_LOWER_PORT 0x8
446 #define BUS_CONVERTER(dev) \
447 ((dev->id.hw_type == HPHW_IOA) || (dev->id.hw_type == HPHW_BCPORT))
449 #define IS_LOWER_PORT(dev) \
450 ((gsc_readl(&((struct bc_module *)dev->hpa)->io_status) \
451 & BC_PORT_MASK) == BC_LOWER_PORT)
453 #define MAX_NATIVE_DEVICES 64
454 #define NATIVE_DEVICE_OFFSET 0x1000
456 #define FLEX_MASK F_EXTEND(0xfffc0000)
457 #define IO_IO_LOW offsetof(struct bc_module, io_io_low)
458 #define IO_IO_HIGH offsetof(struct bc_module, io_io_high)
459 #define READ_IO_IO_LOW(dev) (unsigned long)(signed int)__raw_readl(dev->hpa + IO_IO_LOW)
460 #define READ_IO_IO_HIGH(dev) (unsigned long)(signed int)__raw_readl(dev->hpa + IO_IO_HIGH)
462 static void walk_native_bus(unsigned long io_io_low, unsigned long io_io_high,
463 struct parisc_device *parent);
465 void walk_lower_bus(struct parisc_device *dev)
467 unsigned long io_io_low, io_io_high;
469 if(!BUS_CONVERTER(dev) || IS_LOWER_PORT(dev))
472 if(dev->id.hw_type == HPHW_IOA) {
473 io_io_low = (unsigned long)(signed int)(READ_IO_IO_LOW(dev) << 16);
474 io_io_high = io_io_low + MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET;
476 io_io_low = (READ_IO_IO_LOW(dev) + ~FLEX_MASK) & FLEX_MASK;
477 io_io_high = (READ_IO_IO_HIGH(dev)+ ~FLEX_MASK) & FLEX_MASK;
480 walk_native_bus(io_io_low, io_io_high, dev);
484 * walk_native_bus -- Probe a bus for devices
485 * @io_io_low: Base address of this bus.
486 * @io_io_high: Last address of this bus.
487 * @parent: The parent bus device.
489 * A native bus (eg Runway or GSC) may have up to 64 devices on it,
490 * spaced at intervals of 0x1000 bytes. PDC may not inform us of these
491 * devices, so we have to probe for them. Unfortunately, we may find
492 * devices which are not physically connected (such as extra serial &
493 * keyboard ports). This problem is not yet solved.
495 static void walk_native_bus(unsigned long io_io_low, unsigned long io_io_high,
496 struct parisc_device *parent)
498 int i, devices_found = 0;
499 unsigned long hpa = io_io_low;
500 struct hardware_path path;
502 get_node_path(parent, &path);
504 for(i = 0; i < MAX_NATIVE_DEVICES; i++, hpa += NATIVE_DEVICE_OFFSET) {
505 struct parisc_device *dev;
507 /* Was the device already added by Firmware? */
508 dev = find_device_by_addr(hpa);
511 dev = alloc_pa_dev(hpa, &path);
515 register_parisc_device(dev);
520 } while(!devices_found && hpa < io_io_high);
523 #define CENTRAL_BUS_ADDR F_EXTEND(0xfff80000)
526 * walk_central_bus - Find devices attached to the central bus
528 * PDC doesn't tell us about all devices in the system. This routine
529 * finds devices connected to the central bus.
531 void walk_central_bus(void)
533 walk_native_bus(CENTRAL_BUS_ADDR,
534 CENTRAL_BUS_ADDR + (MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET),
538 void fixup_child_irqs(struct parisc_device *parent, int base,
539 int (*choose_irq)(struct parisc_device *))
541 struct parisc_device *dev;
546 for (dev = check_dev(parent->child); dev; dev = dev->sibling) {
547 int irq = choose_irq(dev);
552 dev->irq = base + irq;
557 static void print_parisc_device(struct parisc_device *dev)
562 print_pa_hwpath(dev, hw_path);
563 printk(KERN_INFO "%d. %s at 0x%lx [%s] { %d, 0x%x, 0x%.3x, 0x%.5x }",
564 ++count, dev->name, dev->hpa, hw_path, dev->id.hw_type,
565 dev->id.hversion_rev, dev->id.hversion, dev->id.sversion);
567 if (dev->num_addrs) {
569 printk(", additional addresses: ");
570 for (k = 0; k < dev->num_addrs; k++)
571 printk("0x%lx ", dev->addr[k]);
576 void print_subdevices(struct parisc_device *parent)
578 struct parisc_device *dev;
579 for (dev = parent->child; dev != parent->sibling; dev = next_dev(dev)) {
580 print_parisc_device(dev);
586 * parisc_generic_device_register_recursive() - internal function to recursively
587 * register all parisc devices
589 static void parisc_generic_device_register_recursive( struct parisc_device *dev )
593 /* has this device been registered already ? */
594 if (dev->dev.dma_mask != NULL)
597 /* register all parents recursively */
598 if (dev->parent && dev->parent!=&root)
599 parisc_generic_device_register_recursive(dev->parent);
601 /* set up the generic device tree for this */
602 snprintf(tmp1, sizeof(tmp1), "%d", dev->hw_path);
603 if (dev->parent && dev->parent != &root) {
604 struct parisc_device *ndev;
607 dev->dev.parent = &dev->parent->dev;
608 for(ndev = dev->parent; ndev != &root;
609 ndev = ndev->parent) {
610 snprintf(tmp2, sizeof(tmp2), "%d:%s",
611 ndev->hw_path, tmp1);
612 strlcpy(tmp1, tmp2, sizeof(tmp1));
616 dev->dev.bus = &parisc_bus_type;
617 snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id), "parisc%s",
619 /* make the generic dma mask a pointer to the parisc one */
620 dev->dev.dma_mask = &dev->dma_mask;
621 dev->dev.coherent_dma_mask = dev->dma_mask;
622 pr_debug("device_register(%s)\n", dev->dev.bus_id);
623 device_register(&dev->dev);
627 * parisc_generic_device_register() - register all parisc devices
629 void parisc_generic_device_register(void)
631 struct parisc_device *dev;
633 bus_register(&parisc_bus_type);
635 for_each_padev(dev) {
636 parisc_generic_device_register_recursive( dev );
641 * print_parisc_devices - Print out a list of devices found in this system
643 void print_parisc_devices(void)
645 struct parisc_device *dev;
646 for_each_padev(dev) {
647 print_parisc_device(dev);