powerpc/pci: Remove dead checks for CONFIG_PPC_OF
[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/ppc-pci.h>
24 #include <asm/byteorder.h>
25 #include <asm/uaccess.h>
26 #include <asm/machdep.h>
27
28 #undef DEBUG
29
30 unsigned long isa_io_base     = 0;
31 unsigned long pci_dram_offset = 0;
32 int pcibios_assign_bus_offset = 1;
33
34 void pcibios_make_OF_bus_map(void);
35
36 static void fixup_cpc710_pci64(struct pci_dev* dev);
37 static u8* pci_to_OF_bus_map;
38
39 /* By default, we don't re-assign bus numbers. We do this only on
40  * some pmacs
41  */
42 static int pci_assign_all_buses;
43
44 static int pci_bus_count;
45
46 /* This will remain NULL for now, until isa-bridge.c is made common
47  * to both 32-bit and 64-bit.
48  */
49 struct pci_dev *isa_bridge_pcidev;
50 EXPORT_SYMBOL_GPL(isa_bridge_pcidev);
51
52 static void
53 fixup_hide_host_resource_fsl(struct pci_dev *dev)
54 {
55         int i, class = dev->class >> 8;
56
57         if ((class == PCI_CLASS_PROCESSOR_POWERPC ||
58              class == PCI_CLASS_BRIDGE_OTHER) &&
59                 (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
60                 (dev->bus->parent == NULL)) {
61                 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
62                         dev->resource[i].start = 0;
63                         dev->resource[i].end = 0;
64                         dev->resource[i].flags = 0;
65                 }
66         }
67 }
68 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl); 
69 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl); 
70
71 static void
72 fixup_cpc710_pci64(struct pci_dev* dev)
73 {
74         /* Hide the PCI64 BARs from the kernel as their content doesn't
75          * fit well in the resource management
76          */
77         dev->resource[0].start = dev->resource[0].end = 0;
78         dev->resource[0].flags = 0;
79         dev->resource[1].start = dev->resource[1].end = 0;
80         dev->resource[1].flags = 0;
81 }
82 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM,     PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
83
84 /*
85  * Functions below are used on OpenFirmware machines.
86  */
87 static void
88 make_one_node_map(struct device_node* node, u8 pci_bus)
89 {
90         const int *bus_range;
91         int len;
92
93         if (pci_bus >= pci_bus_count)
94                 return;
95         bus_range = of_get_property(node, "bus-range", &len);
96         if (bus_range == NULL || len < 2 * sizeof(int)) {
97                 printk(KERN_WARNING "Can't get bus-range for %s, "
98                        "assuming it starts at 0\n", node->full_name);
99                 pci_to_OF_bus_map[pci_bus] = 0;
100         } else
101                 pci_to_OF_bus_map[pci_bus] = bus_range[0];
102
103         for_each_child_of_node(node, node) {
104                 struct pci_dev* dev;
105                 const unsigned int *class_code, *reg;
106         
107                 class_code = of_get_property(node, "class-code", NULL);
108                 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
109                         (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
110                         continue;
111                 reg = of_get_property(node, "reg", NULL);
112                 if (!reg)
113                         continue;
114                 dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
115                 if (!dev || !dev->subordinate) {
116                         pci_dev_put(dev);
117                         continue;
118                 }
119                 make_one_node_map(node, dev->subordinate->number);
120                 pci_dev_put(dev);
121         }
122 }
123         
124 void
125 pcibios_make_OF_bus_map(void)
126 {
127         int i;
128         struct pci_controller *hose, *tmp;
129         struct property *map_prop;
130         struct device_node *dn;
131
132         pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
133         if (!pci_to_OF_bus_map) {
134                 printk(KERN_ERR "Can't allocate OF bus map !\n");
135                 return;
136         }
137
138         /* We fill the bus map with invalid values, that helps
139          * debugging.
140          */
141         for (i=0; i<pci_bus_count; i++)
142                 pci_to_OF_bus_map[i] = 0xff;
143
144         /* For each hose, we begin searching bridges */
145         list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
146                 struct device_node* node = hose->dn;
147
148                 if (!node)
149                         continue;
150                 make_one_node_map(node, hose->first_busno);
151         }
152         dn = of_find_node_by_path("/");
153         map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
154         if (map_prop) {
155                 BUG_ON(pci_bus_count > map_prop->length);
156                 memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
157         }
158         of_node_put(dn);
159 #ifdef DEBUG
160         printk("PCI->OF bus map:\n");
161         for (i=0; i<pci_bus_count; i++) {
162                 if (pci_to_OF_bus_map[i] == 0xff)
163                         continue;
164                 printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
165         }
166 #endif
167 }
168
169 typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
170
171 static struct device_node*
172 scan_OF_pci_childs(struct device_node *parent, pci_OF_scan_iterator filter, void* data)
173 {
174         struct device_node *node;
175         struct device_node* sub_node;
176
177         for_each_child_of_node(parent, node) {
178                 const unsigned int *class_code;
179         
180                 if (filter(node, data)) {
181                         of_node_put(node);
182                         return node;
183                 }
184
185                 /* For PCI<->PCI bridges or CardBus bridges, we go down
186                  * Note: some OFs create a parent node "multifunc-device" as
187                  * a fake root for all functions of a multi-function device,
188                  * we go down them as well.
189                  */
190                 class_code = of_get_property(node, "class-code", NULL);
191                 if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
192                         (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
193                         strcmp(node->name, "multifunc-device"))
194                         continue;
195                 sub_node = scan_OF_pci_childs(node, filter, data);
196                 if (sub_node) {
197                         of_node_put(node);
198                         return sub_node;
199                 }
200         }
201         return NULL;
202 }
203
204 static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
205                                                unsigned int devfn)
206 {
207         struct device_node *np, *cnp;
208         const u32 *reg;
209         unsigned int psize;
210
211         for_each_child_of_node(parent, np) {
212                 reg = of_get_property(np, "reg", &psize);
213                 if (reg && psize >= 4 && ((reg[0] >> 8) & 0xff) == devfn)
214                         return np;
215
216                 /* Note: some OFs create a parent node "multifunc-device" as
217                  * a fake root for all functions of a multi-function device,
218                  * we go down them as well. */
219                 if (!strcmp(np->name, "multifunc-device")) {
220                         cnp = scan_OF_for_pci_dev(np, devfn);
221                         if (cnp)
222                                 return cnp;
223                 }
224         }
225         return NULL;
226 }
227
228
229 static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
230 {
231         struct device_node *parent, *np;
232
233         /* Are we a root bus ? */
234         if (bus->self == NULL || bus->parent == NULL) {
235                 struct pci_controller *hose = pci_bus_to_host(bus);
236                 if (hose == NULL)
237                         return NULL;
238                 return of_node_get(hose->dn);
239         }
240
241         /* not a root bus, we need to get our parent */
242         parent = scan_OF_for_pci_bus(bus->parent);
243         if (parent == NULL)
244                 return NULL;
245
246         /* now iterate for children for a match */
247         np = scan_OF_for_pci_dev(parent, bus->self->devfn);
248         of_node_put(parent);
249
250         return np;
251 }
252
253 /*
254  * Scans the OF tree for a device node matching a PCI device
255  */
256 struct device_node *
257 pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
258 {
259         struct device_node *parent, *np;
260
261         pr_debug("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
262         parent = scan_OF_for_pci_bus(bus);
263         if (parent == NULL)
264                 return NULL;
265         pr_debug(" parent is %s\n", parent ? parent->full_name : "<NULL>");
266         np = scan_OF_for_pci_dev(parent, devfn);
267         of_node_put(parent);
268         pr_debug(" result is %s\n", np ? np->full_name : "<NULL>");
269
270         /* XXX most callers don't release the returned node
271          * mostly because ppc64 doesn't increase the refcount,
272          * we need to fix that.
273          */
274         return np;
275 }
276 EXPORT_SYMBOL(pci_busdev_to_OF_node);
277
278 struct device_node*
279 pci_device_to_OF_node(struct pci_dev *dev)
280 {
281         return pci_busdev_to_OF_node(dev->bus, dev->devfn);
282 }
283 EXPORT_SYMBOL(pci_device_to_OF_node);
284
285 static int
286 find_OF_pci_device_filter(struct device_node* node, void* data)
287 {
288         return ((void *)node == data);
289 }
290
291 /*
292  * Returns the PCI device matching a given OF node
293  */
294 int
295 pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
296 {
297         const unsigned int *reg;
298         struct pci_controller* hose;
299         struct pci_dev* dev = NULL;
300         
301         /* Make sure it's really a PCI device */
302         hose = pci_find_hose_for_OF_device(node);
303         if (!hose || !hose->dn)
304                 return -ENODEV;
305         if (!scan_OF_pci_childs(hose->dn,
306                         find_OF_pci_device_filter, (void *)node))
307                 return -ENODEV;
308         reg = of_get_property(node, "reg", NULL);
309         if (!reg)
310                 return -ENODEV;
311         *bus = (reg[0] >> 16) & 0xff;
312         *devfn = ((reg[0] >> 8) & 0xff);
313
314         /* Ok, here we need some tweak. If we have already renumbered
315          * all busses, we can't rely on the OF bus number any more.
316          * the pci_to_OF_bus_map is not enough as several PCI busses
317          * may match the same OF bus number.
318          */
319         if (!pci_to_OF_bus_map)
320                 return 0;
321
322         for_each_pci_dev(dev)
323                 if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
324                                 dev->devfn == *devfn) {
325                         *bus = dev->bus->number;
326                         pci_dev_put(dev);
327                         return 0;
328                 }
329
330         return -ENODEV;
331 }
332 EXPORT_SYMBOL(pci_device_from_OF_node);
333
334 /* We create the "pci-OF-bus-map" property now so it appears in the
335  * /proc device tree
336  */
337 void __init
338 pci_create_OF_bus_map(void)
339 {
340         struct property* of_prop;
341         struct device_node *dn;
342
343         of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
344         if (!of_prop)
345                 return;
346         dn = of_find_node_by_path("/");
347         if (dn) {
348                 memset(of_prop, -1, sizeof(struct property) + 256);
349                 of_prop->name = "pci-OF-bus-map";
350                 of_prop->length = 256;
351                 of_prop->value = &of_prop[1];
352                 prom_add_property(dn, of_prop);
353                 of_node_put(dn);
354         }
355 }
356
357 static void __devinit pcibios_scan_phb(struct pci_controller *hose)
358 {
359         struct pci_bus *bus;
360         struct device_node *node = hose->dn;
361         unsigned long io_offset;
362         struct resource *res = &hose->io_resource;
363
364         pr_debug("PCI: Scanning PHB %s\n",
365                  node ? node->full_name : "<NO NAME>");
366
367         /* Create an empty bus for the toplevel */
368         bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, hose);
369         if (bus == NULL) {
370                 printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
371                        hose->global_number);
372                 return;
373         }
374         bus->secondary = hose->first_busno;
375         hose->bus = bus;
376
377         /* Fixup IO space offset */
378         io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
379         res->start = (res->start + io_offset) & 0xffffffffu;
380         res->end = (res->end + io_offset) & 0xffffffffu;
381
382         /* Wire up PHB bus resources */
383         pcibios_setup_phb_resources(hose);
384
385         /* Scan children */
386         hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
387 }
388
389 static int __init pcibios_init(void)
390 {
391         struct pci_controller *hose, *tmp;
392         int next_busno = 0;
393
394         printk(KERN_INFO "PCI: Probing PCI hardware\n");
395
396         if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_BUS)
397                 pci_assign_all_buses = 1;
398
399         /* Scan all of the recorded PCI controllers.  */
400         list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
401                 if (pci_assign_all_buses)
402                         hose->first_busno = next_busno;
403                 hose->last_busno = 0xff;
404                 pcibios_scan_phb(hose);
405                 pci_bus_add_devices(hose->bus);
406                 if (pci_assign_all_buses || next_busno <= hose->last_busno)
407                         next_busno = hose->last_busno + pcibios_assign_bus_offset;
408         }
409         pci_bus_count = next_busno;
410
411         /* OpenFirmware based machines need a map of OF bus
412          * numbers vs. kernel bus numbers since we may have to
413          * remap them.
414          */
415         if (pci_assign_all_buses)
416                 pcibios_make_OF_bus_map();
417
418         /* Call common code to handle resource allocation */
419         pcibios_resource_survey();
420
421         /* Call machine dependent post-init code */
422         if (ppc_md.pcibios_after_init)
423                 ppc_md.pcibios_after_init();
424
425         return 0;
426 }
427
428 subsys_initcall(pcibios_init);
429
430 static struct pci_controller*
431 pci_bus_to_hose(int bus)
432 {
433         struct pci_controller *hose, *tmp;
434
435         list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
436                 if (bus >= hose->first_busno && bus <= hose->last_busno)
437                         return hose;
438         return NULL;
439 }
440
441 /* Provide information on locations of various I/O regions in physical
442  * memory.  Do this on a per-card basis so that we choose the right
443  * root bridge.
444  * Note that the returned IO or memory base is a physical address
445  */
446
447 long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
448 {
449         struct pci_controller* hose;
450         long result = -EOPNOTSUPP;
451
452         hose = pci_bus_to_hose(bus);
453         if (!hose)
454                 return -ENODEV;
455
456         switch (which) {
457         case IOBASE_BRIDGE_NUMBER:
458                 return (long)hose->first_busno;
459         case IOBASE_MEMORY:
460                 return (long)hose->pci_mem_offset;
461         case IOBASE_IO:
462                 return (long)hose->io_base_phys;
463         case IOBASE_ISA_IO:
464                 return (long)isa_io_base;
465         case IOBASE_ISA_MEM:
466                 return (long)isa_mem_base;
467         }
468
469         return result;
470 }
471
472 /*
473  * Null PCI config access functions, for the case when we can't
474  * find a hose.
475  */
476 #define NULL_PCI_OP(rw, size, type)                                     \
477 static int                                                              \
478 null_##rw##_config_##size(struct pci_dev *dev, int offset, type val)    \
479 {                                                                       \
480         return PCIBIOS_DEVICE_NOT_FOUND;                                \
481 }
482
483 static int
484 null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
485                  int len, u32 *val)
486 {
487         return PCIBIOS_DEVICE_NOT_FOUND;
488 }
489
490 static int
491 null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
492                   int len, u32 val)
493 {
494         return PCIBIOS_DEVICE_NOT_FOUND;
495 }
496
497 static struct pci_ops null_pci_ops =
498 {
499         .read = null_read_config,
500         .write = null_write_config,
501 };
502
503 /*
504  * These functions are used early on before PCI scanning is done
505  * and all of the pci_dev and pci_bus structures have been created.
506  */
507 static struct pci_bus *
508 fake_pci_bus(struct pci_controller *hose, int busnr)
509 {
510         static struct pci_bus bus;
511
512         if (hose == 0) {
513                 hose = pci_bus_to_hose(busnr);
514                 if (hose == 0)
515                         printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
516         }
517         bus.number = busnr;
518         bus.sysdata = hose;
519         bus.ops = hose? hose->ops: &null_pci_ops;
520         return &bus;
521 }
522
523 #define EARLY_PCI_OP(rw, size, type)                                    \
524 int early_##rw##_config_##size(struct pci_controller *hose, int bus,    \
525                                int devfn, int offset, type value)       \
526 {                                                                       \
527         return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus),    \
528                                             devfn, offset, value);      \
529 }
530
531 EARLY_PCI_OP(read, byte, u8 *)
532 EARLY_PCI_OP(read, word, u16 *)
533 EARLY_PCI_OP(read, dword, u32 *)
534 EARLY_PCI_OP(write, byte, u8)
535 EARLY_PCI_OP(write, word, u16)
536 EARLY_PCI_OP(write, dword, u32)
537
538 extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
539 int early_find_capability(struct pci_controller *hose, int bus, int devfn,
540                           int cap)
541 {
542         return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
543 }