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
13 * Copyright (c) 2004-2005 Thibaut VARENE <varenet@parisc-linux.org>
15 * The file handles registering devices and drivers, then matching them.
16 * It's the closest we get to a dating agency.
18 * If you're thinking about modifying this file, here are some gotchas to
20 * - 715/Mirage device paths have a dummy device between Lasi and its children
21 * - The EISA adapter may show up as a sibling or child of Wax
22 * - Dino has an optionally functional serial port. If firmware enables it,
23 * it shows up as a child of Dino. If firmware disables it, the buswalk
24 * finds it and it shows up as a child of Cujo
25 * - Dino has both parisc and pci devices as children
26 * - parisc devices are discovered in a random order, including children
27 * before parents in some cases.
30 #include <linux/slab.h>
31 #include <linux/types.h>
32 #include <linux/kernel.h>
33 #include <linux/pci.h>
34 #include <linux/spinlock.h>
35 #include <linux/string.h>
36 #include <asm/hardware.h>
39 #include <asm/parisc-device.h>
41 /* See comments in include/asm-parisc/pci.h */
42 struct hppa_dma_ops *hppa_dma_ops;
43 EXPORT_SYMBOL(hppa_dma_ops);
45 static struct device root = {
49 #define for_each_padev(padev) \
50 for (padev = next_dev(&root); padev != NULL; \
51 padev = next_dev(&padev->dev))
53 #define check_dev(padev) \
54 (padev->id.hw_type != HPHW_FAULTY) ? padev : next_dev(&padev->dev)
57 * next_dev - enumerates registered devices
58 * @dev: the previous device returned from next_dev
60 * next_dev does a depth-first search of the tree, returning parents
61 * before children. Returns NULL when there are no more devices.
63 static struct parisc_device *next_dev(struct device *dev)
65 if (!list_empty(&dev->children)) {
66 dev = list_to_dev(dev->children.next);
67 return check_dev(to_parisc_device(dev));
70 while (dev != &root) {
71 if (dev->node.next != &dev->parent->children) {
72 dev = list_to_dev(dev->node.next);
73 return to_parisc_device(dev);
82 * match_device - Report whether this driver can handle this device
83 * @driver: the PA-RISC driver to try
84 * @dev: the PA-RISC device to try
86 static int match_device(struct parisc_driver *driver, struct parisc_device *dev)
88 const struct parisc_device_id *ids;
90 for (ids = driver->id_table; ids->sversion; ids++) {
91 if ((ids->sversion != SVERSION_ANY_ID) &&
92 (ids->sversion != dev->id.sversion))
95 if ((ids->hw_type != HWTYPE_ANY_ID) &&
96 (ids->hw_type != dev->id.hw_type))
99 if ((ids->hversion != HVERSION_ANY_ID) &&
100 (ids->hversion != dev->id.hversion))
108 static void claim_device(struct parisc_driver *driver, struct parisc_device *dev)
110 dev->driver = driver;
111 request_mem_region(dev->hpa, 0x1000, driver->name);
114 static int parisc_driver_probe(struct device *dev)
117 struct parisc_device *pa_dev = to_parisc_device(dev);
118 struct parisc_driver *pa_drv = to_parisc_driver(dev->driver);
120 rc = pa_drv->probe(pa_dev);
123 claim_device(pa_drv, pa_dev);
128 static int parisc_driver_remove(struct device *dev)
130 struct parisc_device *pa_dev = to_parisc_device(dev);
131 struct parisc_driver *pa_drv = to_parisc_driver(dev->driver);
133 pa_drv->remove(pa_dev);
134 release_mem_region(pa_dev->hpa, 0x1000);
141 * register_parisc_driver - Register this driver if it can handle a device
142 * @driver: the PA-RISC driver to try
144 int register_parisc_driver(struct parisc_driver *driver)
146 /* FIXME: we need this because apparently the sti
147 * driver can be registered twice */
148 if(driver->drv.name) {
150 "BUG: skipping previously registered driver %s\n",
155 if (!driver->probe) {
157 "BUG: driver %s has no probe routine\n",
162 driver->drv.bus = &parisc_bus_type;
164 /* We install our own probe and remove routines */
165 WARN_ON(driver->drv.probe != NULL);
166 WARN_ON(driver->drv.remove != NULL);
168 driver->drv.probe = parisc_driver_probe;
169 driver->drv.remove = parisc_driver_remove;
170 driver->drv.name = driver->name;
172 return driver_register(&driver->drv);
174 EXPORT_SYMBOL(register_parisc_driver);
177 * count_parisc_driver - count # of devices this driver would match
178 * @driver: the PA-RISC driver to try
180 * Use by IOMMU support to "guess" the right size IOPdir.
181 * Formula is something like memsize/(num_iommu * entry_size).
183 int count_parisc_driver(struct parisc_driver *driver)
185 struct parisc_device *device;
188 for_each_padev(device) {
189 if (match_device(driver, device))
199 * unregister_parisc_driver - Unregister this driver from the list of drivers
200 * @driver: the PA-RISC driver to unregister
202 int unregister_parisc_driver(struct parisc_driver *driver)
204 driver_unregister(&driver->drv);
207 EXPORT_SYMBOL(unregister_parisc_driver);
209 static struct parisc_device *find_device_by_addr(unsigned long hpa)
211 struct parisc_device *dev;
212 for_each_padev(dev) {
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.
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.
227 const struct parisc_device *
228 find_pa_parent_type(const struct parisc_device *padev, int type)
230 const struct device *dev = &padev->dev;
231 while (dev != &root) {
232 struct parisc_device *candidate = to_parisc_device(dev);
233 if (candidate->id.hw_type == type)
242 static inline int is_pci_dev(struct device *dev)
244 return dev->bus == &pci_bus_type;
247 static inline int is_pci_dev(struct device *dev)
254 * get_node_path fills in @path with the firmware path to the device.
255 * Note that if @node is a parisc device, we don't fill in the 'mod' field.
256 * This is because both callers pass the parent and fill in the mod
257 * themselves. If @node is a PCI device, we do fill it in, even though this
260 static void get_node_path(struct device *dev, struct hardware_path *path)
263 memset(&path->bc, -1, 6);
265 if (is_pci_dev(dev)) {
266 unsigned int devfn = to_pci_dev(dev)->devfn;
267 path->mod = PCI_FUNC(devfn);
268 path->bc[i--] = PCI_SLOT(devfn);
272 while (dev != &root) {
273 if (is_pci_dev(dev)) {
274 unsigned int devfn = to_pci_dev(dev)->devfn;
275 path->bc[i--] = PCI_SLOT(devfn) | (PCI_FUNC(devfn)<< 5);
276 } else if (dev->bus == &parisc_bus_type) {
277 path->bc[i--] = to_parisc_device(dev)->hw_path;
283 static char *print_hwpath(struct hardware_path *path, char *output)
286 for (i = 0; i < 6; i++) {
287 if (path->bc[i] == -1)
289 output += sprintf(output, "%u/", (unsigned char) path->bc[i]);
291 output += sprintf(output, "%u", (unsigned char) path->mod);
296 * print_pa_hwpath - Returns hardware path for PA devices
297 * dev: The device to return the path for
298 * output: Pointer to a previously-allocated array to place the path in.
300 * This function fills in the output array with a human-readable path
301 * to a PA device. This string is compatible with that used by PDC, and
302 * may be printed on the outside of the box.
304 char *print_pa_hwpath(struct parisc_device *dev, char *output)
306 struct hardware_path path;
308 get_node_path(dev->dev.parent, &path);
309 path.mod = dev->hw_path;
310 return print_hwpath(&path, output);
312 EXPORT_SYMBOL(print_pa_hwpath);
314 #if defined(CONFIG_PCI) || defined(CONFIG_ISA)
316 * get_pci_node_path - Determines the hardware path for a PCI device
317 * @pdev: The device to return the path for
318 * @path: Pointer to a previously-allocated array to place the path in.
320 * This function fills in the hardware_path structure with the route to
321 * the specified PCI device. This structure is suitable for passing to
324 void get_pci_node_path(struct pci_dev *pdev, struct hardware_path *path)
326 get_node_path(&pdev->dev, path);
328 EXPORT_SYMBOL(get_pci_node_path);
331 * print_pci_hwpath - Returns hardware path for PCI devices
332 * dev: The device to return the path for
333 * output: Pointer to a previously-allocated array to place the path in.
335 * This function fills in the output array with a human-readable path
336 * to a PCI device. This string is compatible with that used by PDC, and
337 * may be printed on the outside of the box.
339 char *print_pci_hwpath(struct pci_dev *dev, char *output)
341 struct hardware_path path;
343 get_pci_node_path(dev, &path);
344 return print_hwpath(&path, output);
346 EXPORT_SYMBOL(print_pci_hwpath);
348 #endif /* defined(CONFIG_PCI) || defined(CONFIG_ISA) */
350 static void setup_bus_id(struct parisc_device *padev)
352 struct hardware_path path;
353 char *output = padev->dev.bus_id;
356 get_node_path(padev->dev.parent, &path);
358 for (i = 0; i < 6; i++) {
359 if (path.bc[i] == -1)
361 output += sprintf(output, "%u:", (unsigned char) path.bc[i]);
363 sprintf(output, "%u", (unsigned char) padev->hw_path);
366 struct parisc_device * create_tree_node(char id, struct device *parent)
368 struct parisc_device *dev = kmalloc(sizeof(*dev), GFP_KERNEL);
372 memset(dev, 0, sizeof(*dev));
374 dev->id.hw_type = HPHW_FAULTY;
376 dev->dev.parent = parent;
379 dev->dev.bus = &parisc_bus_type;
380 dev->dma_mask = 0xffffffffUL; /* PARISC devices are 32-bit */
382 /* make the generic dma mask a pointer to the parisc one */
383 dev->dev.dma_mask = &dev->dma_mask;
384 dev->dev.coherent_dma_mask = dev->dma_mask;
385 device_register(&dev->dev);
391 * alloc_tree_node - returns a device entry in the iotree
392 * @parent: the parent node in the tree
393 * @id: the element of the module path for this entry
395 * Checks all the children of @parent for a matching @id. If none
396 * found, it allocates a new device and returns it.
398 static struct parisc_device * alloc_tree_node(struct device *parent, char id)
402 list_for_each_entry(dev, &parent->children, node) {
403 struct parisc_device *padev = to_parisc_device(dev);
404 if (padev->hw_path == id)
408 return create_tree_node(id, parent);
411 static struct parisc_device *create_parisc_device(struct hardware_path *modpath)
414 struct device *parent = &root;
415 for (i = 0; i < 6; i++) {
416 if (modpath->bc[i] == -1)
418 parent = &alloc_tree_node(parent, modpath->bc[i])->dev;
420 return alloc_tree_node(parent, modpath->mod);
423 struct parisc_device *
424 alloc_pa_dev(unsigned long hpa, struct hardware_path *mod_path)
427 unsigned long bytecnt;
429 struct parisc_device *dev;
432 /* Check to make sure this device has not already been added - Ryan */
433 if (find_device_by_addr(hpa) != NULL)
436 status = pdc_iodc_read(&bytecnt, hpa, 0, &iodc_data, 32);
437 if (status != PDC_OK)
440 dev = create_parisc_device(mod_path);
441 if (dev->id.hw_type != HPHW_FAULTY) {
443 print_pa_hwpath(dev, p);
444 printk("Two devices have hardware path %s. Please file a bug with HP.\n"
445 "In the meantime, you could try rearranging your cards.\n", p);
449 dev->id.hw_type = iodc_data[3] & 0x1f;
450 dev->id.hversion = (iodc_data[0] << 4) | ((iodc_data[1] & 0xf0) >> 4);
451 dev->id.hversion_rev = iodc_data[1] & 0x0f;
452 dev->id.sversion = ((iodc_data[4] & 0x0f) << 16) |
453 (iodc_data[5] << 8) | iodc_data[6];
455 name = parisc_hardware_description(&dev->id);
457 strlcpy(dev->name, name, sizeof(dev->name));
463 static int parisc_generic_match(struct device *dev, struct device_driver *drv)
465 return match_device(to_parisc_driver(drv), to_parisc_device(dev));
468 #define pa_dev_attr(name, field, format_string) \
469 static ssize_t name##_show(struct device *dev, struct device_attribute *attr, char *buf) \
471 struct parisc_device *padev = to_parisc_device(dev); \
472 return sprintf(buf, format_string, padev->field); \
475 #define pa_dev_attr_id(field, format) pa_dev_attr(field, id.field, format)
477 pa_dev_attr(irq, irq, "%u\n");
478 pa_dev_attr_id(hw_type, "0x%02x\n");
479 pa_dev_attr(rev, id.hversion_rev, "0x%x\n");
480 pa_dev_attr_id(hversion, "0x%03x\n");
481 pa_dev_attr_id(sversion, "0x%05x\n");
483 static struct device_attribute parisc_device_attrs[] = {
492 struct bus_type parisc_bus_type = {
494 .match = parisc_generic_match,
495 .dev_attrs = parisc_device_attrs,
499 * register_parisc_device - Locate a driver to manage this device.
500 * @dev: The parisc device.
502 * Search the driver list for a driver that is willing to manage
505 int register_parisc_device(struct parisc_device *dev)
517 * match_pci_device - Matches a pci device against a given hardware path
519 * @dev: the generic device (known to be contained by a pci_dev).
520 * @index: the current BC index
521 * @modpath: the hardware path.
522 * @return: true if the device matches the hardware path.
524 static int match_pci_device(struct device *dev, int index,
525 struct hardware_path *modpath)
527 struct pci_dev *pdev = to_pci_dev(dev);
531 /* we are at the end of the path, and on the actual device */
532 unsigned int devfn = pdev->devfn;
533 return ((modpath->bc[5] == PCI_SLOT(devfn)) &&
534 (modpath->mod == PCI_FUNC(devfn)));
537 id = PCI_SLOT(pdev->devfn) | (PCI_FUNC(pdev->devfn) << 5);
538 return (modpath->bc[index] == id);
542 * match_parisc_device - Matches a parisc device against a given hardware
544 * @dev: the generic device (known to be contained by a parisc_device).
545 * @index: the current BC index
546 * @modpath: the hardware path.
547 * @return: true if the device matches the hardware path.
549 static int match_parisc_device(struct device *dev, int index,
550 struct hardware_path *modpath)
552 struct parisc_device *curr = to_parisc_device(dev);
553 char id = (index == 6) ? modpath->mod : modpath->bc[index];
555 return (curr->hw_path == id);
559 * parse_tree_node - returns a device entry in the iotree
560 * @parent: the parent node in the tree
561 * @index: the current BC index
562 * @modpath: the hardware_path struct to match a device against
563 * @return: The corresponding device if found, NULL otherwise.
565 * Checks all the children of @parent for a matching @id. If none
566 * found, it returns NULL.
568 static struct device *
569 parse_tree_node(struct device *parent, int index, struct hardware_path *modpath)
571 struct device *device;
573 list_for_each_entry(device, &parent->children, node) {
574 if (device->bus == &parisc_bus_type) {
575 if (match_parisc_device(device, index, modpath))
577 } else if (is_pci_dev(device)) {
578 if (match_pci_device(device, index, modpath))
580 } else if (device->bus == NULL) {
581 /* we are on a bus bridge */
582 struct device *new = parse_tree_node(device, index, modpath);
592 * hwpath_to_device - Finds the generic device corresponding to a given hardware path.
593 * @modpath: the hardware path.
594 * @return: The target device, NULL if not found.
596 struct device *hwpath_to_device(struct hardware_path *modpath)
599 struct device *parent = &root;
600 for (i = 0; i < 6; i++) {
601 if (modpath->bc[i] == -1)
603 parent = parse_tree_node(parent, i, modpath);
607 if (is_pci_dev(parent)) /* pci devices already parse MOD */
610 return parse_tree_node(parent, 6, modpath);
612 EXPORT_SYMBOL(hwpath_to_device);
615 * device_to_hwpath - Populates the hwpath corresponding to the given device.
616 * @param dev the target device
617 * @param path pointer to a previously allocated hwpath struct to be filled in
619 void device_to_hwpath(struct device *dev, struct hardware_path *path)
621 struct parisc_device *padev;
622 if (dev->bus == &parisc_bus_type) {
623 padev = to_parisc_device(dev);
624 get_node_path(dev->parent, path);
625 path->mod = padev->hw_path;
626 } else if (is_pci_dev(dev)) {
627 get_node_path(dev, path);
630 EXPORT_SYMBOL(device_to_hwpath);
632 #define BC_PORT_MASK 0x8
633 #define BC_LOWER_PORT 0x8
635 #define BUS_CONVERTER(dev) \
636 ((dev->id.hw_type == HPHW_IOA) || (dev->id.hw_type == HPHW_BCPORT))
638 #define IS_LOWER_PORT(dev) \
639 ((gsc_readl(dev->hpa + offsetof(struct bc_module, io_status)) \
640 & BC_PORT_MASK) == BC_LOWER_PORT)
642 #define MAX_NATIVE_DEVICES 64
643 #define NATIVE_DEVICE_OFFSET 0x1000
645 #define FLEX_MASK F_EXTEND(0xfffc0000)
646 #define IO_IO_LOW offsetof(struct bc_module, io_io_low)
647 #define IO_IO_HIGH offsetof(struct bc_module, io_io_high)
648 #define READ_IO_IO_LOW(dev) (unsigned long)(signed int)gsc_readl(dev->hpa + IO_IO_LOW)
649 #define READ_IO_IO_HIGH(dev) (unsigned long)(signed int)gsc_readl(dev->hpa + IO_IO_HIGH)
651 static void walk_native_bus(unsigned long io_io_low, unsigned long io_io_high,
652 struct device *parent);
654 void walk_lower_bus(struct parisc_device *dev)
656 unsigned long io_io_low, io_io_high;
658 if(!BUS_CONVERTER(dev) || IS_LOWER_PORT(dev))
661 if(dev->id.hw_type == HPHW_IOA) {
662 io_io_low = (unsigned long)(signed int)(READ_IO_IO_LOW(dev) << 16);
663 io_io_high = io_io_low + MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET;
665 io_io_low = (READ_IO_IO_LOW(dev) + ~FLEX_MASK) & FLEX_MASK;
666 io_io_high = (READ_IO_IO_HIGH(dev)+ ~FLEX_MASK) & FLEX_MASK;
669 walk_native_bus(io_io_low, io_io_high, &dev->dev);
673 * walk_native_bus -- Probe a bus for devices
674 * @io_io_low: Base address of this bus.
675 * @io_io_high: Last address of this bus.
676 * @parent: The parent bus device.
678 * A native bus (eg Runway or GSC) may have up to 64 devices on it,
679 * spaced at intervals of 0x1000 bytes. PDC may not inform us of these
680 * devices, so we have to probe for them. Unfortunately, we may find
681 * devices which are not physically connected (such as extra serial &
682 * keyboard ports). This problem is not yet solved.
684 static void walk_native_bus(unsigned long io_io_low, unsigned long io_io_high,
685 struct device *parent)
687 int i, devices_found = 0;
688 unsigned long hpa = io_io_low;
689 struct hardware_path path;
691 get_node_path(parent, &path);
693 for(i = 0; i < MAX_NATIVE_DEVICES; i++, hpa += NATIVE_DEVICE_OFFSET) {
694 struct parisc_device *dev;
696 /* Was the device already added by Firmware? */
697 dev = find_device_by_addr(hpa);
700 dev = alloc_pa_dev(hpa, &path);
704 register_parisc_device(dev);
709 } while(!devices_found && hpa < io_io_high);
712 #define CENTRAL_BUS_ADDR F_EXTEND(0xfff80000)
715 * walk_central_bus - Find devices attached to the central bus
717 * PDC doesn't tell us about all devices in the system. This routine
718 * finds devices connected to the central bus.
720 void walk_central_bus(void)
722 walk_native_bus(CENTRAL_BUS_ADDR,
723 CENTRAL_BUS_ADDR + (MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET),
727 static void print_parisc_device(struct parisc_device *dev)
732 print_pa_hwpath(dev, hw_path);
733 printk(KERN_INFO "%d. %s at 0x%lx [%s] { %d, 0x%x, 0x%.3x, 0x%.5x }",
734 ++count, dev->name, dev->hpa, hw_path, dev->id.hw_type,
735 dev->id.hversion_rev, dev->id.hversion, dev->id.sversion);
737 if (dev->num_addrs) {
739 printk(", additional addresses: ");
740 for (k = 0; k < dev->num_addrs; k++)
741 printk("0x%lx ", dev->addr[k]);
747 * init_parisc_bus - Some preparation to be done before inventory
749 void init_parisc_bus(void)
751 bus_register(&parisc_bus_type);
752 device_register(&root);
757 * print_parisc_devices - Print out a list of devices found in this system
759 void print_parisc_devices(void)
761 struct parisc_device *dev;
762 for_each_padev(dev) {
763 print_parisc_device(dev);