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