64ad92016b63f2027d7121edb56dcb16545e1066
[linux-2.6-block.git] / arch / powerpc / kernel / pci_of_scan.c
1 /*
2  * Helper routines to scan the device tree for PCI devices and busses
3  *
4  * Migrated out of PowerPC architecture pci_64.c file by Grant Likely
5  * <grant.likely@secretlab.ca> so that these routines are available for
6  * 32 bit also.
7  *
8  * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
9  *   Rework, based on alpha PCI code.
10  * Copyright (c) 2009 Secret Lab Technologies Ltd.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * version 2 as published by the Free Software Foundation.
15  */
16
17 #include <linux/pci.h>
18 #include <linux/export.h>
19 #include <asm/pci-bridge.h>
20 #include <asm/prom.h>
21
22 /**
23  * get_int_prop - Decode a u32 from a device tree property
24  */
25 static u32 get_int_prop(struct device_node *np, const char *name, u32 def)
26 {
27         const __be32 *prop;
28         int len;
29
30         prop = of_get_property(np, name, &len);
31         if (prop && len >= 4)
32                 return of_read_number(prop, 1);
33         return def;
34 }
35
36 /**
37  * pci_parse_of_flags - Parse the flags cell of a device tree PCI address
38  * @addr0: value of 1st cell of a device tree PCI address.
39  * @bridge: Set this flag if the address is from a bridge 'ranges' property
40  */
41 unsigned int pci_parse_of_flags(u32 addr0, int bridge)
42 {
43         unsigned int flags = 0;
44
45         if (addr0 & 0x02000000) {
46                 flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
47                 flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
48                 if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64)
49                         flags |= IORESOURCE_MEM_64;
50                 flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
51                 if (addr0 & 0x40000000)
52                         flags |= IORESOURCE_PREFETCH
53                                  | PCI_BASE_ADDRESS_MEM_PREFETCH;
54                 /* Note: We don't know whether the ROM has been left enabled
55                  * by the firmware or not. We mark it as disabled (ie, we do
56                  * not set the IORESOURCE_ROM_ENABLE flag) for now rather than
57                  * do a config space read, it will be force-enabled if needed
58                  */
59                 if (!bridge && (addr0 & 0xff) == 0x30)
60                         flags |= IORESOURCE_READONLY;
61         } else if (addr0 & 0x01000000)
62                 flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
63         if (flags)
64                 flags |= IORESOURCE_SIZEALIGN;
65         return flags;
66 }
67
68 /**
69  * of_pci_parse_addrs - Parse PCI addresses assigned in the device tree node
70  * @node: device tree node for the PCI device
71  * @dev: pci_dev structure for the device
72  *
73  * This function parses the 'assigned-addresses' property of a PCI devices'
74  * device tree node and writes them into the associated pci_dev structure.
75  */
76 static void of_pci_parse_addrs(struct device_node *node, struct pci_dev *dev)
77 {
78         u64 base, size;
79         unsigned int flags;
80         struct pci_bus_region region;
81         struct resource *res;
82         const __be32 *addrs;
83         u32 i;
84         int proplen;
85
86         addrs = of_get_property(node, "assigned-addresses", &proplen);
87         if (!addrs)
88                 return;
89         pr_debug("    parse addresses (%d bytes) @ %p\n", proplen, addrs);
90         for (; proplen >= 20; proplen -= 20, addrs += 5) {
91                 flags = pci_parse_of_flags(of_read_number(addrs, 1), 0);
92                 if (!flags)
93                         continue;
94                 base = of_read_number(&addrs[1], 2);
95                 size = of_read_number(&addrs[3], 2);
96                 if (!size)
97                         continue;
98                 i = of_read_number(addrs, 1) & 0xff;
99                 pr_debug("  base: %llx, size: %llx, i: %x\n",
100                          (unsigned long long)base,
101                          (unsigned long long)size, i);
102
103                 if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
104                         res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
105                 } else if (i == dev->rom_base_reg) {
106                         res = &dev->resource[PCI_ROM_RESOURCE];
107                         flags |= IORESOURCE_READONLY;
108                 } else {
109                         printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
110                         continue;
111                 }
112                 res->flags = flags;
113                 res->name = pci_name(dev);
114                 region.start = base;
115                 region.end = base + size - 1;
116                 pcibios_bus_to_resource(dev->bus, res, &region);
117         }
118 }
119
120 /**
121  * of_create_pci_dev - Given a device tree node on a pci bus, create a pci_dev
122  * @node: device tree node pointer
123  * @bus: bus the device is sitting on
124  * @devfn: PCI function number, extracted from device tree by caller.
125  */
126 struct pci_dev *of_create_pci_dev(struct device_node *node,
127                                  struct pci_bus *bus, int devfn)
128 {
129         struct pci_dev *dev;
130
131         dev = pci_alloc_dev(bus);
132         if (!dev)
133                 return NULL;
134
135         pr_debug("    create device, devfn: %x, type: %s\n", devfn,
136                  of_node_get_device_type(node));
137
138         dev->dev.of_node = of_node_get(node);
139         dev->dev.parent = bus->bridge;
140         dev->dev.bus = &pci_bus_type;
141         dev->devfn = devfn;
142         dev->multifunction = 0;         /* maybe a lie? */
143         dev->needs_freset = 0;          /* pcie fundamental reset required */
144         set_pcie_port_type(dev);
145
146         pci_dev_assign_slot(dev);
147         dev->vendor = get_int_prop(node, "vendor-id", 0xffff);
148         dev->device = get_int_prop(node, "device-id", 0xffff);
149         dev->subsystem_vendor = get_int_prop(node, "subsystem-vendor-id", 0);
150         dev->subsystem_device = get_int_prop(node, "subsystem-id", 0);
151
152         dev->cfg_size = pci_cfg_space_size(dev);
153
154         dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
155                 dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
156         dev->class = get_int_prop(node, "class-code", 0);
157         dev->revision = get_int_prop(node, "revision-id", 0);
158
159         pr_debug("    class: 0x%x\n", dev->class);
160         pr_debug("    revision: 0x%x\n", dev->revision);
161
162         dev->current_state = PCI_UNKNOWN;       /* unknown power state */
163         dev->error_state = pci_channel_io_normal;
164         dev->dma_mask = 0xffffffff;
165
166         /* Early fixups, before probing the BARs */
167         pci_fixup_device(pci_fixup_early, dev);
168
169         if (of_node_is_type(node, "pci") || of_node_is_type(node, "pciex")) {
170                 /* a PCI-PCI bridge */
171                 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
172                 dev->rom_base_reg = PCI_ROM_ADDRESS1;
173                 set_pcie_hotplug_bridge(dev);
174         } else if (of_node_is_type(node, "cardbus")) {
175                 dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
176         } else {
177                 dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
178                 dev->rom_base_reg = PCI_ROM_ADDRESS;
179                 /* Maybe do a default OF mapping here */
180                 dev->irq = 0;
181         }
182
183         of_pci_parse_addrs(node, dev);
184
185         pr_debug("    adding to system ...\n");
186
187         pci_device_add(dev, bus);
188
189         return dev;
190 }
191 EXPORT_SYMBOL(of_create_pci_dev);
192
193 /**
194  * of_scan_pci_bridge - Set up a PCI bridge and scan for child nodes
195  * @dev: pci_dev structure for the bridge
196  *
197  * of_scan_bus() calls this routine for each PCI bridge that it finds, and
198  * this routine in turn call of_scan_bus() recusively to scan for more child
199  * devices.
200  */
201 void of_scan_pci_bridge(struct pci_dev *dev)
202 {
203         struct device_node *node = dev->dev.of_node;
204         struct pci_bus *bus;
205         struct pci_controller *phb;
206         const __be32 *busrange, *ranges;
207         int len, i, mode;
208         struct pci_bus_region region;
209         struct resource *res;
210         unsigned int flags;
211         u64 size;
212
213         pr_debug("of_scan_pci_bridge(%pOF)\n", node);
214
215         /* parse bus-range property */
216         busrange = of_get_property(node, "bus-range", &len);
217         if (busrange == NULL || len != 8) {
218                 printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %pOF\n",
219                        node);
220                 return;
221         }
222         ranges = of_get_property(node, "ranges", &len);
223         if (ranges == NULL) {
224                 printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %pOF\n",
225                        node);
226                 return;
227         }
228
229         bus = pci_find_bus(pci_domain_nr(dev->bus),
230                            of_read_number(busrange, 1));
231         if (!bus) {
232                 bus = pci_add_new_bus(dev->bus, dev,
233                                       of_read_number(busrange, 1));
234                 if (!bus) {
235                         printk(KERN_ERR "Failed to create pci bus for %pOF\n",
236                                node);
237                         return;
238                 }
239         }
240
241         bus->primary = dev->bus->number;
242         pci_bus_insert_busn_res(bus, of_read_number(busrange, 1),
243                                 of_read_number(busrange+1, 1));
244         bus->bridge_ctl = 0;
245
246         /* parse ranges property */
247         /* PCI #address-cells == 3 and #size-cells == 2 always */
248         res = &dev->resource[PCI_BRIDGE_RESOURCES];
249         for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
250                 res->flags = 0;
251                 bus->resource[i] = res;
252                 ++res;
253         }
254         i = 1;
255         for (; len >= 32; len -= 32, ranges += 8) {
256                 flags = pci_parse_of_flags(of_read_number(ranges, 1), 1);
257                 size = of_read_number(&ranges[6], 2);
258                 if (flags == 0 || size == 0)
259                         continue;
260                 if (flags & IORESOURCE_IO) {
261                         res = bus->resource[0];
262                         if (res->flags) {
263                                 printk(KERN_ERR "PCI: ignoring extra I/O range"
264                                        " for bridge %pOF\n", node);
265                                 continue;
266                         }
267                 } else {
268                         if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
269                                 printk(KERN_ERR "PCI: too many memory ranges"
270                                        " for bridge %pOF\n", node);
271                                 continue;
272                         }
273                         res = bus->resource[i];
274                         ++i;
275                 }
276                 res->flags = flags;
277                 region.start = of_read_number(&ranges[1], 2);
278                 region.end = region.start + size - 1;
279                 pcibios_bus_to_resource(dev->bus, res, &region);
280         }
281         sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
282                 bus->number);
283         pr_debug("    bus name: %s\n", bus->name);
284
285         phb = pci_bus_to_host(bus);
286
287         mode = PCI_PROBE_NORMAL;
288         if (phb->controller_ops.probe_mode)
289                 mode = phb->controller_ops.probe_mode(bus);
290         pr_debug("    probe mode: %d\n", mode);
291
292         if (mode == PCI_PROBE_DEVTREE)
293                 of_scan_bus(node, bus);
294         else if (mode == PCI_PROBE_NORMAL)
295                 pci_scan_child_bus(bus);
296 }
297 EXPORT_SYMBOL(of_scan_pci_bridge);
298
299 static struct pci_dev *of_scan_pci_dev(struct pci_bus *bus,
300                             struct device_node *dn)
301 {
302         struct pci_dev *dev = NULL;
303         const __be32 *reg;
304         int reglen, devfn;
305 #ifdef CONFIG_EEH
306         struct eeh_dev *edev = pdn_to_eeh_dev(PCI_DN(dn));
307 #endif
308
309         pr_debug("  * %pOF\n", dn);
310         if (!of_device_is_available(dn))
311                 return NULL;
312
313         reg = of_get_property(dn, "reg", &reglen);
314         if (reg == NULL || reglen < 20)
315                 return NULL;
316         devfn = (of_read_number(reg, 1) >> 8) & 0xff;
317
318         /* Check if the PCI device is already there */
319         dev = pci_get_slot(bus, devfn);
320         if (dev) {
321                 pci_dev_put(dev);
322                 return dev;
323         }
324
325         /* Device removed permanently ? */
326 #ifdef CONFIG_EEH
327         if (edev && (edev->mode & EEH_DEV_REMOVED))
328                 return NULL;
329 #endif
330
331         /* create a new pci_dev for this device */
332         dev = of_create_pci_dev(dn, bus, devfn);
333         if (!dev)
334                 return NULL;
335
336         pr_debug("  dev header type: %x\n", dev->hdr_type);
337         return dev;
338 }
339
340 /**
341  * __of_scan_bus - given a PCI bus node, setup bus and scan for child devices
342  * @node: device tree node for the PCI bus
343  * @bus: pci_bus structure for the PCI bus
344  * @rescan_existing: Flag indicating bus has already been set up
345  */
346 static void __of_scan_bus(struct device_node *node, struct pci_bus *bus,
347                           int rescan_existing)
348 {
349         struct device_node *child;
350         struct pci_dev *dev;
351
352         pr_debug("of_scan_bus(%pOF) bus no %d...\n",
353                  node, bus->number);
354
355         /* Scan direct children */
356         for_each_child_of_node(node, child) {
357                 dev = of_scan_pci_dev(bus, child);
358                 if (!dev)
359                         continue;
360                 pr_debug("    dev header type: %x\n", dev->hdr_type);
361         }
362
363         /* Apply all fixups necessary. We don't fixup the bus "self"
364          * for an existing bridge that is being rescanned
365          */
366         if (!rescan_existing)
367                 pcibios_setup_bus_self(bus);
368         pcibios_setup_bus_devices(bus);
369
370         /* Now scan child busses */
371         for_each_pci_bridge(dev, bus)
372                 of_scan_pci_bridge(dev);
373 }
374
375 /**
376  * of_scan_bus - given a PCI bus node, setup bus and scan for child devices
377  * @node: device tree node for the PCI bus
378  * @bus: pci_bus structure for the PCI bus
379  */
380 void of_scan_bus(struct device_node *node, struct pci_bus *bus)
381 {
382         __of_scan_bus(node, bus, 0);
383 }
384 EXPORT_SYMBOL_GPL(of_scan_bus);
385
386 /**
387  * of_rescan_bus - given a PCI bus node, scan for child devices
388  * @node: device tree node for the PCI bus
389  * @bus: pci_bus structure for the PCI bus
390  *
391  * Same as of_scan_bus, but for a pci_bus structure that has already been
392  * setup.
393  */
394 void of_rescan_bus(struct device_node *node, struct pci_bus *bus)
395 {
396         __of_scan_bus(node, bus, 1);
397 }
398 EXPORT_SYMBOL_GPL(of_rescan_bus);
399