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