ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / parisc / kernel / drivers.c
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 #include <asm/parisc-device.h>
28
29 /* See comments in include/asm-parisc/pci.h */
30 struct hppa_dma_ops *hppa_dma_ops;
31 EXPORT_SYMBOL(hppa_dma_ops);
32
33 static struct parisc_device root;
34
35 #define for_each_padev(dev) \
36         for (dev = root.child; dev != NULL; dev = next_dev(dev))
37
38 #define check_dev(dev) \
39         (dev->id.hw_type != HPHW_FAULTY) ? dev : next_dev(dev)
40
41 /**
42  * next_dev - enumerates registered devices
43  * @dev: the previous device returned from next_dev
44  *
45  * next_dev does a depth-first search of the tree, returning parents
46  * before children.  Returns NULL when there are no more devices.
47  */
48 struct parisc_device *next_dev(struct parisc_device *dev)
49 {
50         if (dev->child) {
51                 return check_dev(dev->child);
52         } else if (dev->sibling) {
53                 return dev->sibling;
54         }
55
56         /* Exhausted tree at this level, time to go up. */
57         do {
58                 dev = dev->parent;
59                 if (dev && dev->sibling)
60                         return dev->sibling;
61         } while (dev != &root);
62
63         return NULL;
64 }
65
66 /**
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
70  */
71 static int match_device(struct parisc_driver *driver, struct parisc_device *dev)
72 {
73         const struct parisc_device_id *ids;
74
75         for (ids = driver->id_table; ids->sversion; ids++) {
76                 if ((ids->sversion != SVERSION_ANY_ID) &&
77                     (ids->sversion != dev->id.sversion))
78                         continue;
79
80                 if ((ids->hw_type != HWTYPE_ANY_ID) &&
81                     (ids->hw_type != dev->id.hw_type))
82                         continue;
83
84                 if ((ids->hversion != HVERSION_ANY_ID) &&
85                     (ids->hversion != dev->id.hversion))
86                         continue;
87
88                 return 1;
89         }
90         return 0;
91 }
92
93 static void claim_device(struct parisc_driver *driver, struct parisc_device *dev)
94 {
95         dev->driver = driver;
96         request_mem_region(dev->hpa, 0x1000, driver->name);
97 }
98
99 static int parisc_driver_probe(struct device *dev)
100 {
101         int rc;
102         struct parisc_device *pa_dev = to_parisc_device(dev);
103         struct parisc_driver *pa_drv = to_parisc_driver(dev->driver);
104
105         rc = pa_drv->probe(pa_dev);
106
107         if(!rc)
108                 claim_device(pa_drv, pa_dev);
109
110         return rc;
111 }
112
113 static int parisc_driver_remove(struct device *dev)
114 {
115         struct parisc_device *pa_dev = to_parisc_device(dev);
116         struct parisc_driver *pa_drv = to_parisc_driver(dev->driver);
117         if (pa_drv->remove)
118                 pa_drv->remove(pa_dev);
119         release_mem_region(pa_dev->hpa, 0x1000);
120
121         return 0;
122 }
123         
124
125 /**
126  * register_parisc_driver - Register this driver if it can handle a device
127  * @driver: the PA-RISC driver to try
128  */
129 int register_parisc_driver(struct parisc_driver *driver)
130 {
131         /* FIXME: we need this because apparently the sti
132          * driver can be registered twice */
133         if(driver->drv.name) {
134                 printk(KERN_WARNING 
135                        "BUG: skipping previously registered driver %s\n",
136                        driver->name);
137                 return 1;
138         }
139
140         if (!driver->probe) {
141                 printk(KERN_WARNING 
142                        "BUG: driver %s has no probe routine\n",
143                        driver->name);
144                 return 1;
145         }
146
147         driver->drv.bus = &parisc_bus_type;
148
149         /* We install our own probe and remove routines */
150         WARN_ON(driver->drv.probe != NULL);
151         WARN_ON(driver->drv.remove != NULL);
152
153         driver->drv.probe = parisc_driver_probe;
154         driver->drv.remove = parisc_driver_remove;
155         driver->drv.name = driver->name;
156
157         return driver_register(&driver->drv);
158 }
159 EXPORT_SYMBOL(register_parisc_driver);
160
161 /**
162  * count_parisc_driver - count # of devices this driver would match
163  * @driver: the PA-RISC driver to try
164  *
165  * Use by IOMMU support to "guess" the right size IOPdir.
166  * Formula is something like memsize/(num_iommu * entry_size).
167  */
168 int count_parisc_driver(struct parisc_driver *driver)
169 {
170         struct parisc_device *device;
171         int cnt = 0;
172
173         for_each_padev(device) {
174                 if (match_device(driver, device))
175                         cnt++;
176         }
177
178         return cnt;
179 }
180
181
182
183 /**
184  * unregister_parisc_driver - Unregister this driver from the list of drivers
185  * @driver: the PA-RISC driver to unregister
186  */
187 int unregister_parisc_driver(struct parisc_driver *driver)
188 {
189         driver_unregister(&driver->drv);
190         return 0;
191 }
192 EXPORT_SYMBOL(unregister_parisc_driver);
193
194 static struct parisc_device *find_device_by_addr(unsigned long hpa)
195 {
196         struct parisc_device *dev;
197         for_each_padev(dev) {
198                 if (dev->hpa == hpa)
199                         return dev;
200         }
201         return NULL;
202 }
203
204 /**
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.
208  *
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.
211  */
212 const struct parisc_device *find_pa_parent_type(const struct parisc_device *dev, int type)
213 {
214         while (dev != &root) {
215                 if (dev->id.hw_type == type)
216                         return dev;
217                 dev = dev->parent;
218         }
219
220         return NULL;
221 }
222
223 static void
224 get_node_path(struct parisc_device *dev, struct hardware_path *path)
225 {
226         int i = 5;
227         memset(&path->bc, -1, 6);
228         while (dev != &root) {
229                 path->bc[i--] = dev->hw_path;
230                 dev = dev->parent;
231         }
232 }
233
234 static char *print_hwpath(struct hardware_path *path, char *output)
235 {
236         int i;
237         for (i = 0; i < 6; i++) {
238                 if (path->bc[i] == -1)
239                         continue;
240                 output += sprintf(output, "%u/", (unsigned char) path->bc[i]);
241         }
242         output += sprintf(output, "%u", (unsigned char) path->mod);
243         return output;
244 }
245
246 /**
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.
250  *
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.
254  */
255 char *print_pa_hwpath(struct parisc_device *dev, char *output)
256 {
257         struct hardware_path path;
258
259         get_node_path(dev->parent, &path);
260         path.mod = dev->hw_path;
261         return print_hwpath(&path, output);
262 }
263 EXPORT_SYMBOL(print_pa_hwpath);
264
265 #if defined(CONFIG_PCI) || defined(CONFIG_ISA)
266 /**
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.
270  *
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
273  * PDC calls.
274  */
275 void get_pci_node_path(struct pci_dev *dev, struct hardware_path *path)
276 {
277         struct pci_bus *bus;
278         const struct parisc_device *padev;
279         int i = 5;
280
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);
287         }
288
289         padev = HBA_DATA(bus->bridge->platform_data)->dev;
290         while (padev != &root) {
291                 path->bc[i--] = padev->hw_path;
292                 padev = padev->parent;
293         }
294 }
295 EXPORT_SYMBOL(get_pci_node_path);
296
297 /**
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.
301  *
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.
305  */
306 char *print_pci_hwpath(struct pci_dev *dev, char *output)
307 {
308         struct hardware_path path;
309
310         get_pci_node_path(dev, &path);
311         return print_hwpath(&path, output);
312 }
313 EXPORT_SYMBOL(print_pci_hwpath);
314
315 #endif /* defined(CONFIG_PCI) || defined(CONFIG_ISA) */
316
317
318 struct parisc_device * create_tree_node(char id, struct parisc_device *parent,
319                 struct parisc_device **insert)
320 {
321         struct parisc_device *dev = kmalloc(sizeof(*dev), GFP_KERNEL);
322         if (!dev)
323                 return NULL;
324         memset(dev, 0, sizeof(*dev));
325         dev->hw_path = id;
326         dev->id.hw_type = HPHW_FAULTY;
327         dev->parent = parent;
328         dev->sibling = *insert;
329         *insert = dev;
330         return dev;
331 }
332
333 /**
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
337  *
338  * Checks all the children of @parent for a matching @id.  If none
339  * found, it allocates a new device and returns it.
340  */
341 struct parisc_device *
342 alloc_tree_node(struct parisc_device *parent, char id)
343 {
344         struct parisc_device *prev;
345         if ((!parent->child) || (parent->child->hw_path > id)) {
346                 return create_tree_node(id, parent, &parent->child);
347         }
348
349         prev = parent->child;
350         if (prev->hw_path == id)
351                 return prev;
352
353         while (prev->sibling && prev->sibling->hw_path < id) {
354                 prev = prev->sibling;
355         }
356
357         if ((prev->sibling) && (prev->sibling->hw_path == id))
358                 return prev->sibling;
359
360         return create_tree_node(id, parent, &prev->sibling);
361 }
362
363 static struct parisc_device *find_parisc_device(struct hardware_path *modpath)
364 {
365         int i;
366         struct parisc_device *parent = &root;
367         for (i = 0; i < 6; i++) {
368                 if (modpath->bc[i] == -1)
369                         continue;
370                 parent = alloc_tree_node(parent, modpath->bc[i]);
371         }
372         return alloc_tree_node(parent, modpath->mod);
373 }
374
375 struct parisc_device *
376 alloc_pa_dev(unsigned long hpa, struct hardware_path *mod_path)
377 {
378         int status;
379         unsigned long bytecnt;
380         u8 iodc_data[32];
381         struct parisc_device *dev;
382         const char *name;
383
384         /* Check to make sure this device has not already been added - Ryan */
385         if (find_device_by_addr(hpa) != NULL)
386                 return NULL;
387
388         status = pdc_iodc_read(&bytecnt, hpa, 0, &iodc_data, 32);
389         if (status != PDC_OK)
390                 return NULL;
391
392         dev = find_parisc_device(mod_path);
393         if (dev->id.hw_type != HPHW_FAULTY) {
394                 char p[64];
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);
398                 return NULL;
399         }
400
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];
406         dev->hpa = hpa;
407         name = parisc_hardware_description(&dev->id);
408         if (name) {
409                 strlcpy(dev->name, name, sizeof(dev->name));
410         }
411
412         return dev;
413 }
414
415 static int parisc_generic_match(struct device *dev, struct device_driver *drv)
416 {
417         return  match_device(to_parisc_driver(drv), to_parisc_device(dev));
418 }
419
420 struct bus_type parisc_bus_type = {
421         .name = "parisc",
422         .match = parisc_generic_match,
423 };
424
425 /**
426  * register_parisc_device - Locate a driver to manage this device.
427  * @dev: The parisc device.
428  *
429  * Search the driver list for a driver that is willing to manage
430  * this device.
431  */
432 int register_parisc_device(struct parisc_device *dev)
433 {
434         if (!dev)
435                 return 0;
436
437         if (dev->driver)
438                 return 1;
439
440         return 0;
441 }
442
443 #define BC_PORT_MASK 0x8
444 #define BC_LOWER_PORT 0x8
445
446 #define BUS_CONVERTER(dev) \
447         ((dev->id.hw_type == HPHW_IOA) || (dev->id.hw_type == HPHW_BCPORT))
448
449 #define IS_LOWER_PORT(dev) \
450         ((gsc_readl(&((struct bc_module *)dev->hpa)->io_status) \
451                 & BC_PORT_MASK) == BC_LOWER_PORT)
452
453 #define MAX_NATIVE_DEVICES 64
454 #define NATIVE_DEVICE_OFFSET 0x1000
455
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)
461
462 static void walk_native_bus(unsigned long io_io_low, unsigned long io_io_high,
463                             struct parisc_device *parent);
464
465 void walk_lower_bus(struct parisc_device *dev)
466 {
467         unsigned long io_io_low, io_io_high;
468
469         if(!BUS_CONVERTER(dev) || IS_LOWER_PORT(dev))
470                 return;
471
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;
475         } else {
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;
478         }
479
480         walk_native_bus(io_io_low, io_io_high, dev);
481 }
482
483 /**
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.
488  * 
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.
494  */
495 static void walk_native_bus(unsigned long io_io_low, unsigned long io_io_high,
496                             struct parisc_device *parent)
497 {
498         int i, devices_found = 0;
499         unsigned long hpa = io_io_low;
500         struct hardware_path path;
501
502         get_node_path(parent, &path);
503         do {
504                 for(i = 0; i < MAX_NATIVE_DEVICES; i++, hpa += NATIVE_DEVICE_OFFSET) {
505                         struct parisc_device *dev;
506
507                         /* Was the device already added by Firmware? */
508                         dev = find_device_by_addr(hpa);
509                         if (!dev) {
510                                 path.mod = i;
511                                 dev = alloc_pa_dev(hpa, &path);
512                                 if (!dev)
513                                         continue;
514
515                                 register_parisc_device(dev);
516                                 devices_found++;
517                         }
518                         walk_lower_bus(dev);
519                 }
520         } while(!devices_found && hpa < io_io_high);
521 }
522
523 #define CENTRAL_BUS_ADDR F_EXTEND(0xfff80000)
524
525 /**
526  * walk_central_bus - Find devices attached to the central bus
527  *
528  * PDC doesn't tell us about all devices in the system.  This routine
529  * finds devices connected to the central bus.
530  */
531 void walk_central_bus(void)
532 {
533         walk_native_bus(CENTRAL_BUS_ADDR,
534                         CENTRAL_BUS_ADDR + (MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET),
535                         &root);
536 }
537
538 void fixup_child_irqs(struct parisc_device *parent, int base,
539                         int (*choose_irq)(struct parisc_device *))
540 {
541         struct parisc_device *dev;
542
543         if (!parent->child)
544                 return;
545
546         for (dev = check_dev(parent->child); dev; dev = dev->sibling) {
547                 int irq = choose_irq(dev);
548                 if (irq > 0) {
549 #ifdef __LP64__
550                         irq += 32;
551 #endif
552                         dev->irq = base + irq;
553                 }
554         }
555 }
556
557 static void print_parisc_device(struct parisc_device *dev)
558 {
559         char hw_path[64];
560         static int count;
561
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);
566
567         if (dev->num_addrs) {
568                 int k;
569                 printk(",  additional addresses: ");
570                 for (k = 0; k < dev->num_addrs; k++)
571                         printk("0x%lx ", dev->addr[k]);
572         }
573         printk("\n");
574 }
575
576 void print_subdevices(struct parisc_device *parent)
577 {
578         struct parisc_device *dev;
579         for (dev = parent->child; dev != parent->sibling; dev = next_dev(dev)) {
580                 print_parisc_device(dev);
581         }
582 }
583
584
585 /*
586  * parisc_generic_device_register_recursive() - internal function to recursively
587  *      register all parisc devices
588  */
589 static void parisc_generic_device_register_recursive( struct parisc_device *dev )
590 {
591         char tmp1[32];
592         
593         /* has this device been registered already ? */
594         if (dev->dev.dma_mask != NULL)
595                 return;
596         
597         /* register all parents recursively */
598         if (dev->parent && dev->parent!=&root)
599                 parisc_generic_device_register_recursive(dev->parent);
600         
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;
605                 char tmp2[32];
606
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));
613                 }
614         }
615
616         dev->dev.bus = &parisc_bus_type;
617         snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id), "parisc%s",
618                  tmp1);
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);
624 }
625
626 /*
627  * parisc_generic_device_register() - register all parisc devices
628  */
629 void parisc_generic_device_register(void)
630 {
631         struct parisc_device *dev;
632         
633         bus_register(&parisc_bus_type);
634
635         for_each_padev(dev) {
636                 parisc_generic_device_register_recursive( dev );
637         }
638 }
639
640 /**
641  * print_parisc_devices - Print out a list of devices found in this system
642  */
643 void print_parisc_devices(void)
644 {
645         struct parisc_device *dev;
646         for_each_padev(dev) {
647                 print_parisc_device(dev);
648         }
649 }