Merge branch 'for-linus' of git://oss.sgi.com/xfs/xfs
[linux-2.6] / arch / microblaze / 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.h>
23 #include <linux/of_device.h>
24 #include <linux/of_platform.h>
25
26 #include <linux/errno.h>
27 #include <linux/topology.h>
28 #include <asm/atomic.h>
29
30 struct bus_type of_platform_bus_type = {
31        .uevent  = of_device_uevent,
32 };
33 EXPORT_SYMBOL(of_platform_bus_type);
34
35 static int __init of_bus_driver_init(void)
36 {
37         return of_bus_type_init(&of_platform_bus_type, "of_platform");
38 }
39 postcore_initcall(of_bus_driver_init);
40
41 struct of_device *of_platform_device_create(struct device_node *np,
42                                             const char *bus_id,
43                                             struct device *parent)
44 {
45         struct of_device *dev;
46
47         dev = of_device_alloc(np, bus_id, parent);
48         if (!dev)
49                 return NULL;
50
51         dev->dma_mask = 0xffffffffUL;
52         dev->dev.bus = &of_platform_bus_type;
53
54         /* We do not fill the DMA ops for platform devices by default.
55          * This is currently the responsibility of the platform code
56          * to do such, possibly using a device notifier
57          */
58
59         if (of_device_register(dev) != 0) {
60                 of_device_free(dev);
61                 return NULL;
62         }
63
64         return dev;
65 }
66 EXPORT_SYMBOL(of_platform_device_create);
67
68 /**
69  * of_platform_bus_create - Create an OF device for a bus node and all its
70  * children. Optionally recursively instanciate matching busses.
71  * @bus: device node of the bus to instanciate
72  * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
73  * disallow recursive creation of child busses
74  */
75 static int of_platform_bus_create(const struct device_node *bus,
76                                   const struct of_device_id *matches,
77                                   struct device *parent)
78 {
79         struct device_node *child;
80         struct of_device *dev;
81         int rc = 0;
82
83         for_each_child_of_node(bus, child) {
84                 pr_debug("   create child: %s\n", child->full_name);
85                 dev = of_platform_device_create(child, NULL, parent);
86                 if (dev == NULL)
87                         rc = -ENOMEM;
88                 else if (!of_match_node(matches, child))
89                         continue;
90                 if (rc == 0) {
91                         pr_debug("   and sub busses\n");
92                         rc = of_platform_bus_create(child, matches, &dev->dev);
93                 }
94                 if (rc) {
95                         of_node_put(child);
96                         break;
97                 }
98         }
99         return rc;
100 }
101
102
103 /**
104  * of_platform_bus_probe - Probe the device-tree for platform busses
105  * @root: parent of the first level to probe or NULL for the root of the tree
106  * @matches: match table, NULL to use the default
107  * @parent: parent to hook devices from, NULL for toplevel
108  *
109  * Note that children of the provided root are not instanciated as devices
110  * unless the specified root itself matches the bus list and is not NULL.
111  */
112
113 int of_platform_bus_probe(struct device_node *root,
114                           const struct of_device_id *matches,
115                           struct device *parent)
116 {
117         struct device_node *child;
118         struct of_device *dev;
119         int rc = 0;
120
121         if (matches == NULL)
122                 matches = of_default_bus_ids;
123         if (matches == OF_NO_DEEP_PROBE)
124                 return -EINVAL;
125         if (root == NULL)
126                 root = of_find_node_by_path("/");
127         else
128                 of_node_get(root);
129
130         pr_debug("of_platform_bus_probe()\n");
131         pr_debug(" starting at: %s\n", root->full_name);
132
133         /* Do a self check of bus type, if there's a match, create
134          * children
135          */
136         if (of_match_node(matches, root)) {
137                 pr_debug(" root match, create all sub devices\n");
138                 dev = of_platform_device_create(root, NULL, parent);
139                 if (dev == NULL) {
140                         rc = -ENOMEM;
141                         goto bail;
142                 }
143                 pr_debug(" create all sub busses\n");
144                 rc = of_platform_bus_create(root, matches, &dev->dev);
145                 goto bail;
146         }
147         for_each_child_of_node(root, child) {
148                 if (!of_match_node(matches, child))
149                         continue;
150
151                 pr_debug("  match: %s\n", child->full_name);
152                 dev = of_platform_device_create(child, NULL, parent);
153                 if (dev == NULL)
154                         rc = -ENOMEM;
155                 else
156                         rc = of_platform_bus_create(child, matches, &dev->dev);
157                 if (rc) {
158                         of_node_put(child);
159                         break;
160                 }
161         }
162  bail:
163         of_node_put(root);
164         return rc;
165 }
166 EXPORT_SYMBOL(of_platform_bus_probe);
167
168 static int of_dev_node_match(struct device *dev, void *data)
169 {
170         return to_of_device(dev)->node == data;
171 }
172
173 struct of_device *of_find_device_by_node(struct device_node *np)
174 {
175         struct device *dev;
176
177         dev = bus_find_device(&of_platform_bus_type,
178                               NULL, np, of_dev_node_match);
179         if (dev)
180                 return to_of_device(dev);
181         return NULL;
182 }
183 EXPORT_SYMBOL(of_find_device_by_node);
184
185 static int of_dev_phandle_match(struct device *dev, void *data)
186 {
187         phandle *ph = data;
188         return to_of_device(dev)->node->linux_phandle == *ph;
189 }
190
191 struct of_device *of_find_device_by_phandle(phandle ph)
192 {
193         struct device *dev;
194
195         dev = bus_find_device(&of_platform_bus_type,
196                               NULL, &ph, of_dev_phandle_match);
197         if (dev)
198                 return to_of_device(dev);
199         return NULL;
200 }
201 EXPORT_SYMBOL(of_find_device_by_phandle);