Merge branch 'for-2.6.24' of git://git.secretlab.ca/git/linux-2.6-mpc52xx into for...
[pandora-kernel.git] / arch / powerpc / kernel / of_platform.c
1 /*
2  *    Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
3  *                       <benh@kernel.crashing.org>
4  *    and                Arnd Bergmann, IBM Corp.
5  *
6  *  This program is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU General Public License
8  *  as published by the Free Software Foundation; either version
9  *  2 of the License, or (at your option) any later version.
10  *
11  */
12
13 #undef DEBUG
14
15 #include <linux/string.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/slab.h>
21 #include <linux/pci.h>
22 #include <linux/of_device.h>
23 #include <linux/of_platform.h>
24
25 #include <asm/errno.h>
26 #include <asm/dcr.h>
27 #include <asm/topology.h>
28 #include <asm/pci-bridge.h>
29 #include <asm/ppc-pci.h>
30 #include <asm/atomic.h>
31
32 /*
33  * The list of OF IDs below is used for matching bus types in the
34  * system whose devices are to be exposed as of_platform_devices.
35  *
36  * This is the default list valid for most platforms. This file provides
37  * functions who can take an explicit list if necessary though
38  *
39  * The search is always performed recursively looking for children of
40  * the provided device_node and recursively if such a children matches
41  * a bus type in the list
42  */
43
44 static struct of_device_id of_default_bus_ids[] = {
45         { .type = "soc", },
46         { .compatible = "soc", },
47         { .type = "spider", },
48         { .type = "axon", },
49         { .type = "plb5", },
50         { .type = "plb4", },
51         { .type = "opb", },
52         { .type = "ebc", },
53         {},
54 };
55
56 static atomic_t bus_no_reg_magic;
57
58 struct bus_type of_platform_bus_type = {
59        .uevent  = of_device_uevent,
60 };
61 EXPORT_SYMBOL(of_platform_bus_type);
62
63 static int __init of_bus_driver_init(void)
64 {
65         return of_bus_type_init(&of_platform_bus_type, "of_platform");
66 }
67
68 postcore_initcall(of_bus_driver_init);
69
70 int of_register_platform_driver(struct of_platform_driver *drv)
71 {
72         /* initialize common driver fields */
73         if (!drv->driver.name)
74                 drv->driver.name = drv->name;
75         if (!drv->driver.owner)
76                 drv->driver.owner = drv->owner;
77         drv->driver.bus = &of_platform_bus_type;
78
79         /* register with core */
80         return driver_register(&drv->driver);
81 }
82 EXPORT_SYMBOL(of_register_platform_driver);
83
84 void of_unregister_platform_driver(struct of_platform_driver *drv)
85 {
86         driver_unregister(&drv->driver);
87 }
88 EXPORT_SYMBOL(of_unregister_platform_driver);
89
90 static void of_platform_make_bus_id(struct of_device *dev)
91 {
92         struct device_node *node = dev->node;
93         char *name = dev->dev.bus_id;
94         const u32 *reg;
95         u64 addr;
96         int magic;
97
98         /*
99          * If it's a DCR based device, use 'd' for native DCRs
100          * and 'D' for MMIO DCRs.
101          */
102 #ifdef CONFIG_PPC_DCR
103         reg = of_get_property(node, "dcr-reg", NULL);
104         if (reg) {
105 #ifdef CONFIG_PPC_DCR_NATIVE
106                 snprintf(name, BUS_ID_SIZE, "d%x.%s",
107                          *reg, node->name);
108 #else /* CONFIG_PPC_DCR_NATIVE */
109                 addr = of_translate_dcr_address(node, *reg, NULL);
110                 if (addr != OF_BAD_ADDR) {
111                         snprintf(name, BUS_ID_SIZE,
112                                  "D%llx.%s", (unsigned long long)addr,
113                                  node->name);
114                         return;
115                 }
116 #endif /* !CONFIG_PPC_DCR_NATIVE */
117         }
118 #endif /* CONFIG_PPC_DCR */
119
120         /*
121          * For MMIO, get the physical address
122          */
123         reg = of_get_property(node, "reg", NULL);
124         if (reg) {
125                 addr = of_translate_address(node, reg);
126                 if (addr != OF_BAD_ADDR) {
127                         snprintf(name, BUS_ID_SIZE,
128                                  "%llx.%s", (unsigned long long)addr,
129                                  node->name);
130                         return;
131                 }
132         }
133
134         /*
135          * No BusID, use the node name and add a globally incremented
136          * counter (and pray...)
137          */
138         magic = atomic_add_return(1, &bus_no_reg_magic);
139         snprintf(name, BUS_ID_SIZE, "%s.%d", node->name, magic - 1);
140 }
141
142 struct of_device* of_platform_device_create(struct device_node *np,
143                                             const char *bus_id,
144                                             struct device *parent)
145 {
146         struct of_device *dev;
147
148         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
149         if (!dev)
150                 return NULL;
151
152         dev->node = of_node_get(np);
153         dev->dma_mask = 0xffffffffUL;
154         dev->dev.dma_mask = &dev->dma_mask;
155         dev->dev.parent = parent;
156         dev->dev.bus = &of_platform_bus_type;
157         dev->dev.release = of_release_dev;
158         dev->dev.archdata.of_node = np;
159         dev->dev.archdata.numa_node = of_node_to_nid(np);
160
161         /* We do not fill the DMA ops for platform devices by default.
162          * This is currently the responsibility of the platform code
163          * to do such, possibly using a device notifier
164          */
165
166         if (bus_id)
167                 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
168         else
169                 of_platform_make_bus_id(dev);
170
171         if (of_device_register(dev) != 0) {
172                 kfree(dev);
173                 return NULL;
174         }
175
176         return dev;
177 }
178 EXPORT_SYMBOL(of_platform_device_create);
179
180
181
182 /**
183  * of_platform_bus_create - Create an OF device for a bus node and all its
184  * children. Optionally recursively instanciate matching busses.
185  * @bus: device node of the bus to instanciate
186  * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
187  * disallow recursive creation of child busses
188  */
189 static int of_platform_bus_create(struct device_node *bus,
190                                   struct of_device_id *matches,
191                                   struct device *parent)
192 {
193         struct device_node *child;
194         struct of_device *dev;
195         int rc = 0;
196
197         for (child = NULL; (child = of_get_next_child(bus, child)); ) {
198                 pr_debug("   create child: %s\n", child->full_name);
199                 dev = of_platform_device_create(child, NULL, parent);
200                 if (dev == NULL)
201                         rc = -ENOMEM;
202                 else if (!of_match_node(matches, child))
203                         continue;
204                 if (rc == 0) {
205                         pr_debug("   and sub busses\n");
206                         rc = of_platform_bus_create(child, matches, &dev->dev);
207                 } if (rc) {
208                         of_node_put(child);
209                         break;
210                 }
211         }
212         return rc;
213 }
214
215 /**
216  * of_platform_bus_probe - Probe the device-tree for platform busses
217  * @root: parent of the first level to probe or NULL for the root of the tree
218  * @matches: match table, NULL to use the default
219  * @parent: parent to hook devices from, NULL for toplevel
220  *
221  * Note that children of the provided root are not instanciated as devices
222  * unless the specified root itself matches the bus list and is not NULL.
223  */
224
225 int of_platform_bus_probe(struct device_node *root,
226                           struct of_device_id *matches,
227                           struct device *parent)
228 {
229         struct device_node *child;
230         struct of_device *dev;
231         int rc = 0;
232
233         if (matches == NULL)
234                 matches = of_default_bus_ids;
235         if (matches == OF_NO_DEEP_PROBE)
236                 return -EINVAL;
237         if (root == NULL)
238                 root = of_find_node_by_path("/");
239         else
240                 of_node_get(root);
241
242         pr_debug("of_platform_bus_probe()\n");
243         pr_debug(" starting at: %s\n", root->full_name);
244
245         /* Do a self check of bus type, if there's a match, create
246          * children
247          */
248         if (of_match_node(matches, root)) {
249                 pr_debug(" root match, create all sub devices\n");
250                 dev = of_platform_device_create(root, NULL, parent);
251                 if (dev == NULL) {
252                         rc = -ENOMEM;
253                         goto bail;
254                 }
255                 pr_debug(" create all sub busses\n");
256                 rc = of_platform_bus_create(root, matches, &dev->dev);
257                 goto bail;
258         }
259         for (child = NULL; (child = of_get_next_child(root, child)); ) {
260                 if (!of_match_node(matches, child))
261                         continue;
262
263                 pr_debug("  match: %s\n", child->full_name);
264                 dev = of_platform_device_create(child, NULL, parent);
265                 if (dev == NULL)
266                         rc = -ENOMEM;
267                 else
268                         rc = of_platform_bus_create(child, matches, &dev->dev);
269                 if (rc) {
270                         of_node_put(child);
271                         break;
272                 }
273         }
274  bail:
275         of_node_put(root);
276         return rc;
277 }
278 EXPORT_SYMBOL(of_platform_bus_probe);
279
280 static int of_dev_node_match(struct device *dev, void *data)
281 {
282         return to_of_device(dev)->node == data;
283 }
284
285 struct of_device *of_find_device_by_node(struct device_node *np)
286 {
287         struct device *dev;
288
289         dev = bus_find_device(&of_platform_bus_type,
290                               NULL, np, of_dev_node_match);
291         if (dev)
292                 return to_of_device(dev);
293         return NULL;
294 }
295 EXPORT_SYMBOL(of_find_device_by_node);
296
297 static int of_dev_phandle_match(struct device *dev, void *data)
298 {
299         phandle *ph = data;
300         return to_of_device(dev)->node->linux_phandle == *ph;
301 }
302
303 struct of_device *of_find_device_by_phandle(phandle ph)
304 {
305         struct device *dev;
306
307         dev = bus_find_device(&of_platform_bus_type,
308                               NULL, &ph, of_dev_phandle_match);
309         if (dev)
310                 return to_of_device(dev);
311         return NULL;
312 }
313 EXPORT_SYMBOL(of_find_device_by_phandle);
314
315
316 #ifdef CONFIG_PPC_OF_PLATFORM_PCI
317
318 /* The probing of PCI controllers from of_platform is currently
319  * 64 bits only, mostly due to gratuitous differences between
320  * the 32 and 64 bits PCI code on PowerPC and the 32 bits one
321  * lacking some bits needed here.
322  */
323
324 static int __devinit of_pci_phb_probe(struct of_device *dev,
325                                       const struct of_device_id *match)
326 {
327         struct pci_controller *phb;
328
329         /* Check if we can do that ... */
330         if (ppc_md.pci_setup_phb == NULL)
331                 return -ENODEV;
332
333         printk(KERN_INFO "Setting up PCI bus %s\n", dev->node->full_name);
334
335         /* Alloc and setup PHB data structure */
336         phb = pcibios_alloc_controller(dev->node);
337         if (!phb)
338                 return -ENODEV;
339
340         /* Setup parent in sysfs */
341         phb->parent = &dev->dev;
342
343         /* Setup the PHB using arch provided callback */
344         if (ppc_md.pci_setup_phb(phb)) {
345                 pcibios_free_controller(phb);
346                 return -ENODEV;
347         }
348
349         /* Process "ranges" property */
350         pci_process_bridge_OF_ranges(phb, dev->node, 0);
351
352         /* Init pci_dn data structures */
353         pci_devs_phb_init_dynamic(phb);
354
355         /* Register devices with EEH */
356 #ifdef CONFIG_EEH
357         if (dev->node->child)
358                 eeh_add_device_tree_early(dev->node);
359 #endif /* CONFIG_EEH */
360
361         /* Scan the bus */
362         scan_phb(phb);
363
364         /* Claim resources. This might need some rework as well depending
365          * wether we are doing probe-only or not, like assigning unassigned
366          * resources etc...
367          */
368         pcibios_claim_one_bus(phb->bus);
369
370         /* Finish EEH setup */
371 #ifdef CONFIG_EEH
372         eeh_add_device_tree_late(phb->bus);
373 #endif
374
375         /* Add probed PCI devices to the device model */
376         pci_bus_add_devices(phb->bus);
377
378         return 0;
379 }
380
381 static struct of_device_id of_pci_phb_ids[] = {
382         { .type = "pci", },
383         { .type = "pcix", },
384         { .type = "pcie", },
385         { .type = "pciex", },
386         { .type = "ht", },
387         {}
388 };
389
390 static struct of_platform_driver of_pci_phb_driver = {
391         .match_table = of_pci_phb_ids,
392         .probe = of_pci_phb_probe,
393         .driver = {
394                 .name = "of-pci",
395         },
396 };
397
398 static __init int of_pci_phb_init(void)
399 {
400         return of_register_platform_driver(&of_pci_phb_driver);
401 }
402
403 device_initcall(of_pci_phb_init);
404
405 #endif /* CONFIG_PPC_OF_PLATFORM_PCI */