[POWERPC] pci_32.c: Use for_each_child_of_node
[safe/jmp/linux-2.6] / arch / powerpc / kernel / pci_32.c
1 /*
2  * Common pmac/prep/chrp pci routines. -- Cort
3  */
4
5 #include <linux/kernel.h>
6 #include <linux/pci.h>
7 #include <linux/delay.h>
8 #include <linux/string.h>
9 #include <linux/init.h>
10 #include <linux/capability.h>
11 #include <linux/sched.h>
12 #include <linux/errno.h>
13 #include <linux/bootmem.h>
14 #include <linux/irq.h>
15 #include <linux/list.h>
16 #include <linux/of.h>
17
18 #include <asm/processor.h>
19 #include <asm/io.h>
20 #include <asm/prom.h>
21 #include <asm/sections.h>
22 #include <asm/pci-bridge.h>
23 #include <asm/byteorder.h>
24 #include <asm/uaccess.h>
25 #include <asm/machdep.h>
26
27 #undef DEBUG
28
29 #ifdef DEBUG
30 #define DBG(x...) printk(x)
31 #else
32 #define DBG(x...)
33 #endif
34
35 unsigned long isa_io_base     = 0;
36 unsigned long pci_dram_offset = 0;
37 int pcibios_assign_bus_offset = 1;
38
39 void pcibios_make_OF_bus_map(void);
40
41 static void fixup_broken_pcnet32(struct pci_dev* dev);
42 static void fixup_cpc710_pci64(struct pci_dev* dev);
43 #ifdef CONFIG_PPC_OF
44 static u8* pci_to_OF_bus_map;
45 #endif
46
47 /* By default, we don't re-assign bus numbers. We do this only on
48  * some pmacs
49  */
50 static int pci_assign_all_buses;
51
52 LIST_HEAD(hose_list);
53
54 static int pci_bus_count;
55
56 static void
57 fixup_hide_host_resource_fsl(struct pci_dev* dev)
58 {
59         int i, class = dev->class >> 8;
60
61         if ((class == PCI_CLASS_PROCESSOR_POWERPC) &&
62                 (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
63                 (dev->bus->parent == NULL)) {
64                 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
65                         dev->resource[i].start = 0;
66                         dev->resource[i].end = 0;
67                         dev->resource[i].flags = 0;
68                 }
69         }
70 }
71 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl); 
72 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl); 
73
74 static void
75 fixup_broken_pcnet32(struct pci_dev* dev)
76 {
77         if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
78                 dev->vendor = PCI_VENDOR_ID_AMD;
79                 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
80         }
81 }
82 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID,                     fixup_broken_pcnet32);
83
84 static void
85 fixup_cpc710_pci64(struct pci_dev* dev)
86 {
87         /* Hide the PCI64 BARs from the kernel as their content doesn't
88          * fit well in the resource management
89          */
90         dev->resource[0].start = dev->resource[0].end = 0;
91         dev->resource[0].flags = 0;
92         dev->resource[1].start = dev->resource[1].end = 0;
93         dev->resource[1].flags = 0;
94 }
95 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM,     PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
96
97
98 void __init
99 update_bridge_resource(struct pci_dev *dev, struct resource *res)
100 {
101         u8 io_base_lo, io_limit_lo;
102         u16 mem_base, mem_limit;
103         u16 cmd;
104         resource_size_t start, end, off;
105         struct pci_controller *hose = dev->sysdata;
106
107         if (!hose) {
108                 printk("update_bridge_base: no hose?\n");
109                 return;
110         }
111         pci_read_config_word(dev, PCI_COMMAND, &cmd);
112         pci_write_config_word(dev, PCI_COMMAND,
113                               cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
114         if (res->flags & IORESOURCE_IO) {
115                 off = (unsigned long) hose->io_base_virt - isa_io_base;
116                 start = res->start - off;
117                 end = res->end - off;
118                 io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
119                 io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
120                 if (end > 0xffff)
121                         io_base_lo |= PCI_IO_RANGE_TYPE_32;
122                 else
123                         io_base_lo |= PCI_IO_RANGE_TYPE_16;
124                 pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
125                                 start >> 16);
126                 pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
127                                 end >> 16);
128                 pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
129                 pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
130
131         } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
132                    == IORESOURCE_MEM) {
133                 off = hose->pci_mem_offset;
134                 mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
135                 mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
136                 pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
137                 pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
138
139         } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
140                    == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
141                 off = hose->pci_mem_offset;
142                 mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
143                 mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
144                 pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
145                 pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
146
147         } else {
148                 DBG(KERN_ERR "PCI: ugh, bridge %s res has flags=%lx\n",
149                     pci_name(dev), res->flags);
150         }
151         pci_write_config_word(dev, PCI_COMMAND, cmd);
152 }
153
154
155 #ifdef CONFIG_PPC_OF
156 /*
157  * Functions below are used on OpenFirmware machines.
158  */
159 static void
160 make_one_node_map(struct device_node* node, u8 pci_bus)
161 {
162         const int *bus_range;
163         int len;
164
165         if (pci_bus >= pci_bus_count)
166                 return;
167         bus_range = of_get_property(node, "bus-range", &len);
168         if (bus_range == NULL || len < 2 * sizeof(int)) {
169                 printk(KERN_WARNING "Can't get bus-range for %s, "
170                        "assuming it starts at 0\n", node->full_name);
171                 pci_to_OF_bus_map[pci_bus] = 0;
172         } else
173                 pci_to_OF_bus_map[pci_bus] = bus_range[0];
174
175         for_each_child_of_node(node, node) {
176                 struct pci_dev* dev;
177                 const unsigned int *class_code, *reg;
178         
179                 class_code = of_get_property(node, "class-code", NULL);
180                 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
181                         (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
182                         continue;
183                 reg = of_get_property(node, "reg", NULL);
184                 if (!reg)
185                         continue;
186                 dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
187                 if (!dev || !dev->subordinate) {
188                         pci_dev_put(dev);
189                         continue;
190                 }
191                 make_one_node_map(node, dev->subordinate->number);
192                 pci_dev_put(dev);
193         }
194 }
195         
196 void
197 pcibios_make_OF_bus_map(void)
198 {
199         int i;
200         struct pci_controller *hose, *tmp;
201         struct property *map_prop;
202         struct device_node *dn;
203
204         pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
205         if (!pci_to_OF_bus_map) {
206                 printk(KERN_ERR "Can't allocate OF bus map !\n");
207                 return;
208         }
209
210         /* We fill the bus map with invalid values, that helps
211          * debugging.
212          */
213         for (i=0; i<pci_bus_count; i++)
214                 pci_to_OF_bus_map[i] = 0xff;
215
216         /* For each hose, we begin searching bridges */
217         list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
218                 struct device_node* node = hose->dn;
219
220                 if (!node)
221                         continue;
222                 make_one_node_map(node, hose->first_busno);
223         }
224         dn = of_find_node_by_path("/");
225         map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
226         if (map_prop) {
227                 BUG_ON(pci_bus_count > map_prop->length);
228                 memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
229         }
230         of_node_put(dn);
231 #ifdef DEBUG
232         printk("PCI->OF bus map:\n");
233         for (i=0; i<pci_bus_count; i++) {
234                 if (pci_to_OF_bus_map[i] == 0xff)
235                         continue;
236                 printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
237         }
238 #endif
239 }
240
241 typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
242
243 static struct device_node*
244 scan_OF_pci_childs(struct device_node *parent, pci_OF_scan_iterator filter, void* data)
245 {
246         struct device_node *node;
247         struct device_node* sub_node;
248
249         for_each_child_of_node(parent, node) {
250                 const unsigned int *class_code;
251         
252                 if (filter(node, data)) {
253                         of_node_put(node);
254                         return node;
255                 }
256
257                 /* For PCI<->PCI bridges or CardBus bridges, we go down
258                  * Note: some OFs create a parent node "multifunc-device" as
259                  * a fake root for all functions of a multi-function device,
260                  * we go down them as well.
261                  */
262                 class_code = of_get_property(node, "class-code", NULL);
263                 if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
264                         (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
265                         strcmp(node->name, "multifunc-device"))
266                         continue;
267                 sub_node = scan_OF_pci_childs(node, filter, data);
268                 if (sub_node) {
269                         of_node_put(node);
270                         return sub_node;
271                 }
272         }
273         return NULL;
274 }
275
276 static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
277                                                unsigned int devfn)
278 {
279         struct device_node *np = NULL;
280         const u32 *reg;
281         unsigned int psize;
282
283         while ((np = of_get_next_child(parent, np)) != NULL) {
284                 reg = of_get_property(np, "reg", &psize);
285                 if (reg == NULL || psize < 4)
286                         continue;
287                 if (((reg[0] >> 8) & 0xff) == devfn)
288                         return np;
289         }
290         return NULL;
291 }
292
293
294 static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
295 {
296         struct device_node *parent, *np;
297
298         /* Are we a root bus ? */
299         if (bus->self == NULL || bus->parent == NULL) {
300                 struct pci_controller *hose = pci_bus_to_host(bus);
301                 if (hose == NULL)
302                         return NULL;
303                 return of_node_get(hose->dn);
304         }
305
306         /* not a root bus, we need to get our parent */
307         parent = scan_OF_for_pci_bus(bus->parent);
308         if (parent == NULL)
309                 return NULL;
310
311         /* now iterate for children for a match */
312         np = scan_OF_for_pci_dev(parent, bus->self->devfn);
313         of_node_put(parent);
314
315         return np;
316 }
317
318 /*
319  * Scans the OF tree for a device node matching a PCI device
320  */
321 struct device_node *
322 pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
323 {
324         struct device_node *parent, *np;
325
326         if (!have_of)
327                 return NULL;
328
329         DBG("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
330         parent = scan_OF_for_pci_bus(bus);
331         if (parent == NULL)
332                 return NULL;
333         DBG(" parent is %s\n", parent ? parent->full_name : "<NULL>");
334         np = scan_OF_for_pci_dev(parent, devfn);
335         of_node_put(parent);
336         DBG(" result is %s\n", np ? np->full_name : "<NULL>");
337
338         /* XXX most callers don't release the returned node
339          * mostly because ppc64 doesn't increase the refcount,
340          * we need to fix that.
341          */
342         return np;
343 }
344 EXPORT_SYMBOL(pci_busdev_to_OF_node);
345
346 struct device_node*
347 pci_device_to_OF_node(struct pci_dev *dev)
348 {
349         return pci_busdev_to_OF_node(dev->bus, dev->devfn);
350 }
351 EXPORT_SYMBOL(pci_device_to_OF_node);
352
353 static int
354 find_OF_pci_device_filter(struct device_node* node, void* data)
355 {
356         return ((void *)node == data);
357 }
358
359 /*
360  * Returns the PCI device matching a given OF node
361  */
362 int
363 pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
364 {
365         const unsigned int *reg;
366         struct pci_controller* hose;
367         struct pci_dev* dev = NULL;
368         
369         if (!have_of)
370                 return -ENODEV;
371         /* Make sure it's really a PCI device */
372         hose = pci_find_hose_for_OF_device(node);
373         if (!hose || !hose->dn)
374                 return -ENODEV;
375         if (!scan_OF_pci_childs(hose->dn,
376                         find_OF_pci_device_filter, (void *)node))
377                 return -ENODEV;
378         reg = of_get_property(node, "reg", NULL);
379         if (!reg)
380                 return -ENODEV;
381         *bus = (reg[0] >> 16) & 0xff;
382         *devfn = ((reg[0] >> 8) & 0xff);
383
384         /* Ok, here we need some tweak. If we have already renumbered
385          * all busses, we can't rely on the OF bus number any more.
386          * the pci_to_OF_bus_map is not enough as several PCI busses
387          * may match the same OF bus number.
388          */
389         if (!pci_to_OF_bus_map)
390                 return 0;
391
392         for_each_pci_dev(dev)
393                 if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
394                                 dev->devfn == *devfn) {
395                         *bus = dev->bus->number;
396                         pci_dev_put(dev);
397                         return 0;
398                 }
399
400         return -ENODEV;
401 }
402 EXPORT_SYMBOL(pci_device_from_OF_node);
403
404 /* We create the "pci-OF-bus-map" property now so it appears in the
405  * /proc device tree
406  */
407 void __init
408 pci_create_OF_bus_map(void)
409 {
410         struct property* of_prop;
411         struct device_node *dn;
412
413         of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
414         if (!of_prop)
415                 return;
416         dn = of_find_node_by_path("/");
417         if (dn) {
418                 memset(of_prop, -1, sizeof(struct property) + 256);
419                 of_prop->name = "pci-OF-bus-map";
420                 of_prop->length = 256;
421                 of_prop->value = &of_prop[1];
422                 prom_add_property(dn, of_prop);
423                 of_node_put(dn);
424         }
425 }
426
427 #else /* CONFIG_PPC_OF */
428 void pcibios_make_OF_bus_map(void)
429 {
430 }
431 #endif /* CONFIG_PPC_OF */
432
433 static int __init pcibios_init(void)
434 {
435         struct pci_controller *hose, *tmp;
436         struct pci_bus *bus;
437         int next_busno = 0;
438
439         printk(KERN_INFO "PCI: Probing PCI hardware\n");
440
441         if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_BUS)
442                 pci_assign_all_buses = 1;
443
444         /* Scan all of the recorded PCI controllers.  */
445         list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
446                 if (pci_assign_all_buses)
447                         hose->first_busno = next_busno;
448                 hose->last_busno = 0xff;
449                 bus = pci_scan_bus_parented(hose->parent, hose->first_busno,
450                                             hose->ops, hose);
451                 if (bus) {
452                         pci_bus_add_devices(bus);
453                         hose->last_busno = bus->subordinate;
454                 }
455                 if (pci_assign_all_buses || next_busno <= hose->last_busno)
456                         next_busno = hose->last_busno + pcibios_assign_bus_offset;
457         }
458         pci_bus_count = next_busno;
459
460         /* OpenFirmware based machines need a map of OF bus
461          * numbers vs. kernel bus numbers since we may have to
462          * remap them.
463          */
464         if (pci_assign_all_buses && have_of)
465                 pcibios_make_OF_bus_map();
466
467         /* Call common code to handle resource allocation */
468         pcibios_resource_survey();
469
470         /* Call machine dependent post-init code */
471         if (ppc_md.pcibios_after_init)
472                 ppc_md.pcibios_after_init();
473
474         return 0;
475 }
476
477 subsys_initcall(pcibios_init);
478
479 void __devinit pcibios_do_bus_setup(struct pci_bus *bus)
480 {
481         struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
482         unsigned long io_offset;
483         struct resource *res;
484         int i;
485
486         /* Hookup PHB resources */
487         io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
488         if (bus->parent == NULL) {
489                 /* This is a host bridge - fill in its resources */
490                 hose->bus = bus;
491
492                 bus->resource[0] = res = &hose->io_resource;
493                 if (!res->flags) {
494                         if (io_offset)
495                                 printk(KERN_ERR "I/O resource not set for host"
496                                        " bridge %d\n", hose->global_number);
497                         res->start = 0;
498                         res->end = IO_SPACE_LIMIT;
499                         res->flags = IORESOURCE_IO;
500                 }
501                 res->start = (res->start + io_offset) & 0xffffffffu;
502                 res->end = (res->end + io_offset) & 0xffffffffu;
503
504                 for (i = 0; i < 3; ++i) {
505                         res = &hose->mem_resources[i];
506                         if (!res->flags) {
507                                 if (i > 0)
508                                         continue;
509                                 printk(KERN_ERR "Memory resource not set for "
510                                        "host bridge %d\n", hose->global_number);
511                                 res->start = hose->pci_mem_offset;
512                                 res->end = ~0U;
513                                 res->flags = IORESOURCE_MEM;
514                         }
515                         bus->resource[i+1] = res;
516                 }
517         }
518 }
519
520 /* the next one is stolen from the alpha port... */
521 void __init
522 pcibios_update_irq(struct pci_dev *dev, int irq)
523 {
524         pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
525         /* XXX FIXME - update OF device tree node interrupt property */
526 }
527
528 static struct pci_controller*
529 pci_bus_to_hose(int bus)
530 {
531         struct pci_controller *hose, *tmp;
532
533         list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
534                 if (bus >= hose->first_busno && bus <= hose->last_busno)
535                         return hose;
536         return NULL;
537 }
538
539 /* Provide information on locations of various I/O regions in physical
540  * memory.  Do this on a per-card basis so that we choose the right
541  * root bridge.
542  * Note that the returned IO or memory base is a physical address
543  */
544
545 long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
546 {
547         struct pci_controller* hose;
548         long result = -EOPNOTSUPP;
549
550         hose = pci_bus_to_hose(bus);
551         if (!hose)
552                 return -ENODEV;
553
554         switch (which) {
555         case IOBASE_BRIDGE_NUMBER:
556                 return (long)hose->first_busno;
557         case IOBASE_MEMORY:
558                 return (long)hose->pci_mem_offset;
559         case IOBASE_IO:
560                 return (long)hose->io_base_phys;
561         case IOBASE_ISA_IO:
562                 return (long)isa_io_base;
563         case IOBASE_ISA_MEM:
564                 return (long)isa_mem_base;
565         }
566
567         return result;
568 }
569
570 unsigned long pci_address_to_pio(phys_addr_t address)
571 {
572         struct pci_controller *hose, *tmp;
573
574         list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
575                 unsigned int size = hose->io_resource.end -
576                         hose->io_resource.start + 1;
577                 if (address >= hose->io_base_phys &&
578                     address < (hose->io_base_phys + size)) {
579                         unsigned long base =
580                                 (unsigned long)hose->io_base_virt - _IO_BASE;
581                         return base + (address - hose->io_base_phys);
582                 }
583         }
584         return (unsigned int)-1;
585 }
586 EXPORT_SYMBOL(pci_address_to_pio);
587
588 /*
589  * Null PCI config access functions, for the case when we can't
590  * find a hose.
591  */
592 #define NULL_PCI_OP(rw, size, type)                                     \
593 static int                                                              \
594 null_##rw##_config_##size(struct pci_dev *dev, int offset, type val)    \
595 {                                                                       \
596         return PCIBIOS_DEVICE_NOT_FOUND;                                \
597 }
598
599 static int
600 null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
601                  int len, u32 *val)
602 {
603         return PCIBIOS_DEVICE_NOT_FOUND;
604 }
605
606 static int
607 null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
608                   int len, u32 val)
609 {
610         return PCIBIOS_DEVICE_NOT_FOUND;
611 }
612
613 static struct pci_ops null_pci_ops =
614 {
615         .read = null_read_config,
616         .write = null_write_config,
617 };
618
619 /*
620  * These functions are used early on before PCI scanning is done
621  * and all of the pci_dev and pci_bus structures have been created.
622  */
623 static struct pci_bus *
624 fake_pci_bus(struct pci_controller *hose, int busnr)
625 {
626         static struct pci_bus bus;
627
628         if (hose == 0) {
629                 hose = pci_bus_to_hose(busnr);
630                 if (hose == 0)
631                         printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
632         }
633         bus.number = busnr;
634         bus.sysdata = hose;
635         bus.ops = hose? hose->ops: &null_pci_ops;
636         return &bus;
637 }
638
639 #define EARLY_PCI_OP(rw, size, type)                                    \
640 int early_##rw##_config_##size(struct pci_controller *hose, int bus,    \
641                                int devfn, int offset, type value)       \
642 {                                                                       \
643         return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus),    \
644                                             devfn, offset, value);      \
645 }
646
647 EARLY_PCI_OP(read, byte, u8 *)
648 EARLY_PCI_OP(read, word, u16 *)
649 EARLY_PCI_OP(read, dword, u32 *)
650 EARLY_PCI_OP(write, byte, u8)
651 EARLY_PCI_OP(write, word, u16)
652 EARLY_PCI_OP(write, dword, u32)
653
654 extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
655 int early_find_capability(struct pci_controller *hose, int bus, int devfn,
656                           int cap)
657 {
658         return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
659 }