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