Merge master.kernel.org:/pub/scm/linux/kernel/git/steve/gfs2-2.6-nmw
[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
23 #include <asm/errno.h>
24 #include <asm/dcr.h>
25 #include <asm/of_device.h>
26 #include <asm/of_platform.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 /*
34  * The list of OF IDs below is used for matching bus types in the
35  * system whose devices are to be exposed as of_platform_devices.
36  *
37  * This is the default list valid for most platforms. This file provides
38  * functions who can take an explicit list if necessary though
39  *
40  * The search is always performed recursively looking for children of
41  * the provided device_node and recursively if such a children matches
42  * a bus type in the list
43  */
44
45 static struct of_device_id of_default_bus_ids[] = {
46         { .type = "soc", },
47         { .compatible = "soc", },
48         { .type = "spider", },
49         { .type = "axon", },
50         { .type = "plb5", },
51         { .type = "plb4", },
52         { .type = "opb", },
53         {},
54 };
55
56 static atomic_t bus_no_reg_magic;
57
58 /*
59  *
60  * OF platform device type definition & base infrastructure
61  *
62  */
63
64 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
65 {
66         struct of_device * of_dev = to_of_device(dev);
67         struct of_platform_driver * of_drv = to_of_platform_driver(drv);
68         const struct of_device_id * matches = of_drv->match_table;
69
70         if (!matches)
71                 return 0;
72
73         return of_match_device(matches, of_dev) != NULL;
74 }
75
76 static int of_platform_device_probe(struct device *dev)
77 {
78         int error = -ENODEV;
79         struct of_platform_driver *drv;
80         struct of_device *of_dev;
81         const struct of_device_id *match;
82
83         drv = to_of_platform_driver(dev->driver);
84         of_dev = to_of_device(dev);
85
86         if (!drv->probe)
87                 return error;
88
89         of_dev_get(of_dev);
90
91         match = of_match_device(drv->match_table, of_dev);
92         if (match)
93                 error = drv->probe(of_dev, match);
94         if (error)
95                 of_dev_put(of_dev);
96
97         return error;
98 }
99
100 static int of_platform_device_remove(struct device *dev)
101 {
102         struct of_device * of_dev = to_of_device(dev);
103         struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
104
105         if (dev->driver && drv->remove)
106                 drv->remove(of_dev);
107         return 0;
108 }
109
110 static int of_platform_device_suspend(struct device *dev, pm_message_t state)
111 {
112         struct of_device * of_dev = to_of_device(dev);
113         struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
114         int error = 0;
115
116         if (dev->driver && drv->suspend)
117                 error = drv->suspend(of_dev, state);
118         return error;
119 }
120
121 static int of_platform_device_resume(struct device * dev)
122 {
123         struct of_device * of_dev = to_of_device(dev);
124         struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
125         int error = 0;
126
127         if (dev->driver && drv->resume)
128                 error = drv->resume(of_dev);
129         return error;
130 }
131
132 struct bus_type of_platform_bus_type = {
133        .name    = "of_platform",
134        .match   = of_platform_bus_match,
135        .probe   = of_platform_device_probe,
136        .remove  = of_platform_device_remove,
137        .suspend = of_platform_device_suspend,
138        .resume  = of_platform_device_resume,
139 };
140 EXPORT_SYMBOL(of_platform_bus_type);
141
142 static int __init of_bus_driver_init(void)
143 {
144         return bus_register(&of_platform_bus_type);
145 }
146
147 postcore_initcall(of_bus_driver_init);
148
149 int of_register_platform_driver(struct of_platform_driver *drv)
150 {
151         /* initialize common driver fields */
152         drv->driver.name = drv->name;
153         drv->driver.bus = &of_platform_bus_type;
154
155         /* register with core */
156         return driver_register(&drv->driver);
157 }
158 EXPORT_SYMBOL(of_register_platform_driver);
159
160 void of_unregister_platform_driver(struct of_platform_driver *drv)
161 {
162         driver_unregister(&drv->driver);
163 }
164 EXPORT_SYMBOL(of_unregister_platform_driver);
165
166 static void of_platform_make_bus_id(struct of_device *dev)
167 {
168         struct device_node *node = dev->node;
169         char *name = dev->dev.bus_id;
170         const u32 *reg;
171         u64 addr;
172         long magic;
173
174         /*
175          * If it's a DCR based device, use 'd' for native DCRs
176          * and 'D' for MMIO DCRs.
177          */
178 #ifdef CONFIG_PPC_DCR
179         reg = get_property(node, "dcr-reg", NULL);
180         if (reg) {
181 #ifdef CONFIG_PPC_DCR_NATIVE
182                 snprintf(name, BUS_ID_SIZE, "d%x.%s",
183                          *reg, node->name);
184 #else /* CONFIG_PPC_DCR_NATIVE */
185                 addr = of_translate_dcr_address(node, *reg, NULL);
186                 if (addr != OF_BAD_ADDR) {
187                         snprintf(name, BUS_ID_SIZE,
188                                  "D%llx.%s", (unsigned long long)addr,
189                                  node->name);
190                         return;
191                 }
192 #endif /* !CONFIG_PPC_DCR_NATIVE */
193         }
194 #endif /* CONFIG_PPC_DCR */
195
196         /*
197          * For MMIO, get the physical address
198          */
199         reg = get_property(node, "reg", NULL);
200         if (reg) {
201                 addr = of_translate_address(node, reg);
202                 if (addr != OF_BAD_ADDR) {
203                         snprintf(name, BUS_ID_SIZE,
204                                  "%llx.%s", (unsigned long long)addr,
205                                  node->name);
206                         return;
207                 }
208         }
209
210         /*
211          * No BusID, use the node name and add a globally incremented
212          * counter (and pray...)
213          */
214         magic = atomic_add_return(1, &bus_no_reg_magic);
215         snprintf(name, BUS_ID_SIZE, "%s.%d", node->name, magic - 1);
216 }
217
218 struct of_device* of_platform_device_create(struct device_node *np,
219                                             const char *bus_id,
220                                             struct device *parent)
221 {
222         struct of_device *dev;
223
224         dev = kmalloc(sizeof(*dev), GFP_KERNEL);
225         if (!dev)
226                 return NULL;
227         memset(dev, 0, sizeof(*dev));
228
229         dev->node = of_node_get(np);
230         dev->dma_mask = 0xffffffffUL;
231         dev->dev.dma_mask = &dev->dma_mask;
232         dev->dev.parent = parent;
233         dev->dev.bus = &of_platform_bus_type;
234         dev->dev.release = of_release_dev;
235         dev->dev.archdata.of_node = np;
236         dev->dev.archdata.numa_node = of_node_to_nid(np);
237
238         /* We do not fill the DMA ops for platform devices by default.
239          * This is currently the responsibility of the platform code
240          * to do such, possibly using a device notifier
241          */
242
243         if (bus_id)
244                 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
245         else
246                 of_platform_make_bus_id(dev);
247
248         if (of_device_register(dev) != 0) {
249                 kfree(dev);
250                 return NULL;
251         }
252
253         return dev;
254 }
255 EXPORT_SYMBOL(of_platform_device_create);
256
257
258
259 /**
260  * of_platform_bus_create - Create an OF device for a bus node and all its
261  * children. Optionally recursively instanciate matching busses.
262  * @bus: device node of the bus to instanciate
263  * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
264  * disallow recursive creation of child busses
265  */
266 static int of_platform_bus_create(struct device_node *bus,
267                                   struct of_device_id *matches,
268                                   struct device *parent)
269 {
270         struct device_node *child;
271         struct of_device *dev;
272         int rc = 0;
273
274         for (child = NULL; (child = of_get_next_child(bus, child)); ) {
275                 pr_debug("   create child: %s\n", child->full_name);
276                 dev = of_platform_device_create(child, NULL, parent);
277                 if (dev == NULL)
278                         rc = -ENOMEM;
279                 else if (!of_match_node(matches, child))
280                         continue;
281                 if (rc == 0) {
282                         pr_debug("   and sub busses\n");
283                         rc = of_platform_bus_create(child, matches, &dev->dev);
284                 } if (rc) {
285                         of_node_put(child);
286                         break;
287                 }
288         }
289         return rc;
290 }
291
292 /**
293  * of_platform_bus_probe - Probe the device-tree for platform busses
294  * @root: parent of the first level to probe or NULL for the root of the tree
295  * @matches: match table, NULL to use the default
296  * @parent: parent to hook devices from, NULL for toplevel
297  *
298  * Note that children of the provided root are not instanciated as devices
299  * unless the specified root itself matches the bus list and is not NULL.
300  */
301
302 int of_platform_bus_probe(struct device_node *root,
303                           struct of_device_id *matches,
304                           struct device *parent)
305 {
306         struct device_node *child;
307         struct of_device *dev;
308         int rc = 0;
309
310         if (matches == NULL)
311                 matches = of_default_bus_ids;
312         if (matches == OF_NO_DEEP_PROBE)
313                 return -EINVAL;
314         if (root == NULL)
315                 root = of_find_node_by_path("/");
316         else
317                 of_node_get(root);
318
319         pr_debug("of_platform_bus_probe()\n");
320         pr_debug(" starting at: %s\n", root->full_name);
321
322         /* Do a self check of bus type, if there's a match, create
323          * children
324          */
325         if (of_match_node(matches, root)) {
326                 pr_debug(" root match, create all sub devices\n");
327                 dev = of_platform_device_create(root, NULL, parent);
328                 if (dev == NULL) {
329                         rc = -ENOMEM;
330                         goto bail;
331                 }
332                 pr_debug(" create all sub busses\n");
333                 rc = of_platform_bus_create(root, matches, &dev->dev);
334                 goto bail;
335         }
336         for (child = NULL; (child = of_get_next_child(root, child)); ) {
337                 if (!of_match_node(matches, child))
338                         continue;
339
340                 pr_debug("  match: %s\n", child->full_name);
341                 dev = of_platform_device_create(child, NULL, parent);
342                 if (dev == NULL)
343                         rc = -ENOMEM;
344                 else
345                         rc = of_platform_bus_create(child, matches, &dev->dev);
346                 if (rc) {
347                         of_node_put(child);
348                         break;
349                 }
350         }
351  bail:
352         of_node_put(root);
353         return rc;
354 }
355 EXPORT_SYMBOL(of_platform_bus_probe);
356
357 static int of_dev_node_match(struct device *dev, void *data)
358 {
359         return to_of_device(dev)->node == data;
360 }
361
362 struct of_device *of_find_device_by_node(struct device_node *np)
363 {
364         struct device *dev;
365
366         dev = bus_find_device(&of_platform_bus_type,
367                               NULL, np, of_dev_node_match);
368         if (dev)
369                 return to_of_device(dev);
370         return NULL;
371 }
372 EXPORT_SYMBOL(of_find_device_by_node);
373
374 static int of_dev_phandle_match(struct device *dev, void *data)
375 {
376         phandle *ph = data;
377         return to_of_device(dev)->node->linux_phandle == *ph;
378 }
379
380 struct of_device *of_find_device_by_phandle(phandle ph)
381 {
382         struct device *dev;
383
384         dev = bus_find_device(&of_platform_bus_type,
385                               NULL, &ph, of_dev_phandle_match);
386         if (dev)
387                 return to_of_device(dev);
388         return NULL;
389 }
390 EXPORT_SYMBOL(of_find_device_by_phandle);
391
392
393 #ifdef CONFIG_PPC_OF_PLATFORM_PCI
394
395 /* The probing of PCI controllers from of_platform is currently
396  * 64 bits only, mostly due to gratuitous differences between
397  * the 32 and 64 bits PCI code on PowerPC and the 32 bits one
398  * lacking some bits needed here.
399  */
400
401 static int __devinit of_pci_phb_probe(struct of_device *dev,
402                                       const struct of_device_id *match)
403 {
404         struct pci_controller *phb;
405
406         /* Check if we can do that ... */
407         if (ppc_md.pci_setup_phb == NULL)
408                 return -ENODEV;
409
410         printk(KERN_INFO "Setting up PCI bus %s\n", dev->node->full_name);
411
412         /* Alloc and setup PHB data structure */
413         phb = pcibios_alloc_controller(dev->node);
414         if (!phb)
415                 return -ENODEV;
416
417         /* Setup parent in sysfs */
418         phb->parent = &dev->dev;
419
420         /* Setup the PHB using arch provided callback */
421         if (ppc_md.pci_setup_phb(phb)) {
422                 pcibios_free_controller(phb);
423                 return -ENODEV;
424         }
425
426         /* Process "ranges" property */
427         pci_process_bridge_OF_ranges(phb, dev->node, 0);
428
429         /* Setup IO space.
430          * This will not work properly for ISA IOs, something needs to be done
431          * about it if we ever generalize that way of probing PCI brigdes
432          */
433         pci_setup_phb_io_dynamic(phb, 0);
434
435         /* Init pci_dn data structures */
436         pci_devs_phb_init_dynamic(phb);
437
438         /* Register devices with EEH */
439 #ifdef CONFIG_EEH
440         if (dev->node->child)
441                 eeh_add_device_tree_early(dev->node);
442 #endif /* CONFIG_EEH */
443
444         /* Scan the bus */
445         scan_phb(phb);
446
447         /* Claim resources. This might need some rework as well depending
448          * wether we are doing probe-only or not, like assigning unassigned
449          * resources etc...
450          */
451         pcibios_claim_one_bus(phb->bus);
452
453         /* Finish EEH setup */
454 #ifdef CONFIG_EEH
455         eeh_add_device_tree_late(phb->bus);
456 #endif
457
458         /* Add probed PCI devices to the device model */
459         pci_bus_add_devices(phb->bus);
460
461         return 0;
462 }
463
464 static struct of_device_id of_pci_phb_ids[] = {
465         { .type = "pci", },
466         { .type = "pcix", },
467         { .type = "pcie", },
468         { .type = "pciex", },
469         { .type = "ht", },
470         {}
471 };
472
473 static struct of_platform_driver of_pci_phb_driver = {
474        .name = "of-pci",
475        .match_table = of_pci_phb_ids,
476        .probe = of_pci_phb_probe,
477        .driver = {
478                .multithread_probe = 1,
479        },
480 };
481
482 static __init int of_pci_phb_init(void)
483 {
484         return of_register_platform_driver(&of_pci_phb_driver);
485 }
486
487 device_initcall(of_pci_phb_init);
488
489 #endif /* CONFIG_PPC_OF_PLATFORM_PCI */