Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[pandora-kernel.git] / arch / microblaze / pci / 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
26 #undef DEBUG
27
28 unsigned long isa_io_base;
29 unsigned long pci_dram_offset;
30 int pcibios_assign_bus_offset = 1;
31
32 static u8 *pci_to_OF_bus_map;
33
34 /* By default, we don't re-assign bus numbers. We do this only on
35  * some pmacs
36  */
37 static int pci_assign_all_buses;
38
39 static int pci_bus_count;
40
41 /*
42  * Functions below are used on OpenFirmware machines.
43  */
44 static void
45 make_one_node_map(struct device_node *node, u8 pci_bus)
46 {
47         const int *bus_range;
48         int len;
49
50         if (pci_bus >= pci_bus_count)
51                 return;
52         bus_range = of_get_property(node, "bus-range", &len);
53         if (bus_range == NULL || len < 2 * sizeof(int)) {
54                 printk(KERN_WARNING "Can't get bus-range for %s, "
55                        "assuming it starts at 0\n", node->full_name);
56                 pci_to_OF_bus_map[pci_bus] = 0;
57         } else
58                 pci_to_OF_bus_map[pci_bus] = bus_range[0];
59
60         for_each_child_of_node(node, node) {
61                 struct pci_dev *dev;
62                 const unsigned int *class_code, *reg;
63
64                 class_code = of_get_property(node, "class-code", NULL);
65                 if (!class_code ||
66                         ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
67                         (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
68                         continue;
69                 reg = of_get_property(node, "reg", NULL);
70                 if (!reg)
71                         continue;
72                 dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
73                 if (!dev || !dev->subordinate) {
74                         pci_dev_put(dev);
75                         continue;
76                 }
77                 make_one_node_map(node, dev->subordinate->number);
78                 pci_dev_put(dev);
79         }
80 }
81
82 void
83 pcibios_make_OF_bus_map(void)
84 {
85         int i;
86         struct pci_controller *hose, *tmp;
87         struct property *map_prop;
88         struct device_node *dn;
89
90         pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
91         if (!pci_to_OF_bus_map) {
92                 printk(KERN_ERR "Can't allocate OF bus map !\n");
93                 return;
94         }
95
96         /* We fill the bus map with invalid values, that helps
97          * debugging.
98          */
99         for (i = 0; i < pci_bus_count; i++)
100                 pci_to_OF_bus_map[i] = 0xff;
101
102         /* For each hose, we begin searching bridges */
103         list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
104                 struct device_node *node = hose->dn;
105
106                 if (!node)
107                         continue;
108                 make_one_node_map(node, hose->first_busno);
109         }
110         dn = of_find_node_by_path("/");
111         map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
112         if (map_prop) {
113                 BUG_ON(pci_bus_count > map_prop->length);
114                 memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
115         }
116         of_node_put(dn);
117 #ifdef DEBUG
118         printk(KERN_INFO "PCI->OF bus map:\n");
119         for (i = 0; i < pci_bus_count; i++) {
120                 if (pci_to_OF_bus_map[i] == 0xff)
121                         continue;
122                 printk(KERN_INFO "%d -> %d\n", i, pci_to_OF_bus_map[i]);
123         }
124 #endif
125 }
126
127 typedef int (*pci_OF_scan_iterator)(struct device_node *node, void *data);
128
129 static struct device_node *scan_OF_pci_childs(struct device_node *parent,
130                                         pci_OF_scan_iterator filter, void *data)
131 {
132         struct device_node *node;
133         struct device_node *sub_node;
134
135         for_each_child_of_node(parent, node) {
136                 const unsigned int *class_code;
137
138                 if (filter(node, data)) {
139                         of_node_put(node);
140                         return node;
141                 }
142
143                 /* For PCI<->PCI bridges or CardBus bridges, we go down
144                  * Note: some OFs create a parent node "multifunc-device" as
145                  * a fake root for all functions of a multi-function device,
146                  * we go down them as well.
147                  */
148                 class_code = of_get_property(node, "class-code", NULL);
149                 if ((!class_code ||
150                         ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
151                         (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
152                         strcmp(node->name, "multifunc-device"))
153                         continue;
154                 sub_node = scan_OF_pci_childs(node, filter, data);
155                 if (sub_node) {
156                         of_node_put(node);
157                         return sub_node;
158                 }
159         }
160         return NULL;
161 }
162
163 static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
164                                                unsigned int devfn)
165 {
166         struct device_node *np, *cnp;
167         const u32 *reg;
168         unsigned int psize;
169
170         for_each_child_of_node(parent, np) {
171                 reg = of_get_property(np, "reg", &psize);
172                 if (reg && psize >= 4 && ((reg[0] >> 8) & 0xff) == devfn)
173                         return np;
174
175                 /* Note: some OFs create a parent node "multifunc-device" as
176                  * a fake root for all functions of a multi-function device,
177                  * we go down them as well. */
178                 if (!strcmp(np->name, "multifunc-device")) {
179                         cnp = scan_OF_for_pci_dev(np, devfn);
180                         if (cnp)
181                                 return cnp;
182                 }
183         }
184         return NULL;
185 }
186
187
188 static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
189 {
190         struct device_node *parent, *np;
191
192         /* Are we a root bus ? */
193         if (bus->self == NULL || bus->parent == NULL) {
194                 struct pci_controller *hose = pci_bus_to_host(bus);
195                 if (hose == NULL)
196                         return NULL;
197                 return of_node_get(hose->dn);
198         }
199
200         /* not a root bus, we need to get our parent */
201         parent = scan_OF_for_pci_bus(bus->parent);
202         if (parent == NULL)
203                 return NULL;
204
205         /* now iterate for children for a match */
206         np = scan_OF_for_pci_dev(parent, bus->self->devfn);
207         of_node_put(parent);
208
209         return np;
210 }
211
212 /*
213  * Scans the OF tree for a device node matching a PCI device
214  */
215 struct device_node *
216 pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
217 {
218         struct device_node *parent, *np;
219
220         pr_debug("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
221         parent = scan_OF_for_pci_bus(bus);
222         if (parent == NULL)
223                 return NULL;
224         pr_debug(" parent is %s\n", parent ? parent->full_name : "<NULL>");
225         np = scan_OF_for_pci_dev(parent, devfn);
226         of_node_put(parent);
227         pr_debug(" result is %s\n", np ? np->full_name : "<NULL>");
228
229         /* XXX most callers don't release the returned node
230          * mostly because ppc64 doesn't increase the refcount,
231          * we need to fix that.
232          */
233         return np;
234 }
235 EXPORT_SYMBOL(pci_busdev_to_OF_node);
236
237 struct device_node*
238 pci_device_to_OF_node(struct pci_dev *dev)
239 {
240         return pci_busdev_to_OF_node(dev->bus, dev->devfn);
241 }
242 EXPORT_SYMBOL(pci_device_to_OF_node);
243
244 static int
245 find_OF_pci_device_filter(struct device_node *node, void *data)
246 {
247         return ((void *)node == data);
248 }
249
250 /*
251  * Returns the PCI device matching a given OF node
252  */
253 int
254 pci_device_from_OF_node(struct device_node *node, u8 *bus, u8 *devfn)
255 {
256         const unsigned int *reg;
257         struct pci_controller *hose;
258         struct pci_dev *dev = NULL;
259
260         /* Make sure it's really a PCI device */
261         hose = pci_find_hose_for_OF_device(node);
262         if (!hose || !hose->dn)
263                 return -ENODEV;
264         if (!scan_OF_pci_childs(hose->dn,
265                         find_OF_pci_device_filter, (void *)node))
266                 return -ENODEV;
267         reg = of_get_property(node, "reg", NULL);
268         if (!reg)
269                 return -ENODEV;
270         *bus = (reg[0] >> 16) & 0xff;
271         *devfn = ((reg[0] >> 8) & 0xff);
272
273         /* Ok, here we need some tweak. If we have already renumbered
274          * all busses, we can't rely on the OF bus number any more.
275          * the pci_to_OF_bus_map is not enough as several PCI busses
276          * may match the same OF bus number.
277          */
278         if (!pci_to_OF_bus_map)
279                 return 0;
280
281         for_each_pci_dev(dev)
282                 if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
283                                 dev->devfn == *devfn) {
284                         *bus = dev->bus->number;
285                         pci_dev_put(dev);
286                         return 0;
287                 }
288
289         return -ENODEV;
290 }
291 EXPORT_SYMBOL(pci_device_from_OF_node);
292
293 /* We create the "pci-OF-bus-map" property now so it appears in the
294  * /proc device tree
295  */
296 void __init
297 pci_create_OF_bus_map(void)
298 {
299         struct property *of_prop;
300         struct device_node *dn;
301
302         of_prop = (struct property *) alloc_bootmem(sizeof(struct property) + \
303                                                                          256);
304         if (!of_prop)
305                 return;
306         dn = of_find_node_by_path("/");
307         if (dn) {
308                 memset(of_prop, -1, sizeof(struct property) + 256);
309                 of_prop->name = "pci-OF-bus-map";
310                 of_prop->length = 256;
311                 of_prop->value = &of_prop[1];
312                 prom_add_property(dn, of_prop);
313                 of_node_put(dn);
314         }
315 }
316
317 static void __devinit pcibios_scan_phb(struct pci_controller *hose)
318 {
319         struct pci_bus *bus;
320         struct device_node *node = hose->dn;
321         unsigned long io_offset;
322         struct resource *res = &hose->io_resource;
323
324         pr_debug("PCI: Scanning PHB %s\n",
325                  node ? node->full_name : "<NO NAME>");
326
327         /* Create an empty bus for the toplevel */
328         bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, hose);
329         if (bus == NULL) {
330                 printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
331                        hose->global_number);
332                 return;
333         }
334         bus->secondary = hose->first_busno;
335         hose->bus = bus;
336
337         /* Fixup IO space offset */
338         io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
339         res->start = (res->start + io_offset) & 0xffffffffu;
340         res->end = (res->end + io_offset) & 0xffffffffu;
341
342         /* Wire up PHB bus resources */
343         pcibios_setup_phb_resources(hose);
344
345         /* Scan children */
346         hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
347 }
348
349 static int __init pcibios_init(void)
350 {
351         struct pci_controller *hose, *tmp;
352         int next_busno = 0;
353
354         printk(KERN_INFO "PCI: Probing PCI hardware\n");
355
356         if (pci_flags & PCI_REASSIGN_ALL_BUS) {
357                 printk(KERN_INFO "setting pci_asign_all_busses\n");
358                 pci_assign_all_buses = 1;
359         }
360
361         /* Scan all of the recorded PCI controllers.  */
362         list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
363                 if (pci_assign_all_buses)
364                         hose->first_busno = next_busno;
365                 hose->last_busno = 0xff;
366                 pcibios_scan_phb(hose);
367                 printk(KERN_INFO "calling pci_bus_add_devices()\n");
368                 pci_bus_add_devices(hose->bus);
369                 if (pci_assign_all_buses || next_busno <= hose->last_busno)
370                         next_busno = hose->last_busno + \
371                                         pcibios_assign_bus_offset;
372         }
373         pci_bus_count = next_busno;
374
375         /* OpenFirmware based machines need a map of OF bus
376          * numbers vs. kernel bus numbers since we may have to
377          * remap them.
378          */
379         if (pci_assign_all_buses)
380                 pcibios_make_OF_bus_map();
381
382         /* Call common code to handle resource allocation */
383         pcibios_resource_survey();
384
385         return 0;
386 }
387
388 subsys_initcall(pcibios_init);
389
390 static struct pci_controller*
391 pci_bus_to_hose(int bus)
392 {
393         struct pci_controller *hose, *tmp;
394
395         list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
396                 if (bus >= hose->first_busno && bus <= hose->last_busno)
397                         return hose;
398         return NULL;
399 }
400
401 /* Provide information on locations of various I/O regions in physical
402  * memory.  Do this on a per-card basis so that we choose the right
403  * root bridge.
404  * Note that the returned IO or memory base is a physical address
405  */
406
407 long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
408 {
409         struct pci_controller *hose;
410         long result = -EOPNOTSUPP;
411
412         hose = pci_bus_to_hose(bus);
413         if (!hose)
414                 return -ENODEV;
415
416         switch (which) {
417         case IOBASE_BRIDGE_NUMBER:
418                 return (long)hose->first_busno;
419         case IOBASE_MEMORY:
420                 return (long)hose->pci_mem_offset;
421         case IOBASE_IO:
422                 return (long)hose->io_base_phys;
423         case IOBASE_ISA_IO:
424                 return (long)isa_io_base;
425         case IOBASE_ISA_MEM:
426                 return (long)isa_mem_base;
427         }
428
429         return result;
430 }