]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/powerpc/kernel/of_platform.c
[POWERPC] iSeries: Correct missing newline in printk
[mirror_ubuntu-bionic-kernel.git] / arch / powerpc / kernel / of_platform.c
CommitLineData
7eebde70
BH
1/*
2 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
3 * <benh@kernel.crashing.org>
4c9d2800 4 * and Arnd Bergmann, IBM Corp.
7eebde70
BH
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>
4c9d2800 21#include <linux/pci.h>
7eebde70
BH
22
23#include <asm/errno.h>
24#include <asm/dcr.h>
25#include <asm/of_device.h>
26#include <asm/of_platform.h>
12d04eef 27#include <asm/topology.h>
4c9d2800
BH
28#include <asm/pci-bridge.h>
29#include <asm/ppc-pci.h>
9309180f
BH
30#include <asm/atomic.h>
31
7eebde70
BH
32/*
33 * The list of OF IDs below is used for matching bus types in the
34 * system whose devices are to be exposed as of_platform_devices.
35 *
36 * This is the default list valid for most platforms. This file provides
37 * functions who can take an explicit list if necessary though
38 *
39 * The search is always performed recursively looking for children of
40 * the provided device_node and recursively if such a children matches
41 * a bus type in the list
42 */
43
44static struct of_device_id of_default_bus_ids[] = {
45 { .type = "soc", },
46 { .compatible = "soc", },
47 { .type = "spider", },
48 { .type = "axon", },
49 { .type = "plb5", },
50 { .type = "plb4", },
51 { .type = "opb", },
4bc19626 52 { .type = "ebc", },
7eebde70
BH
53 {},
54};
55
9309180f
BH
56static atomic_t bus_no_reg_magic;
57
7eebde70 58struct bus_type of_platform_bus_type = {
1f9209cf 59 .uevent = of_device_uevent,
7eebde70
BH
60};
61EXPORT_SYMBOL(of_platform_bus_type);
62
63static int __init of_bus_driver_init(void)
64{
3f23de10 65 return of_bus_type_init(&of_platform_bus_type, "of_platform");
7eebde70
BH
66}
67
68postcore_initcall(of_bus_driver_init);
69
70int of_register_platform_driver(struct of_platform_driver *drv)
71{
72 /* initialize common driver fields */
73 drv->driver.name = drv->name;
74 drv->driver.bus = &of_platform_bus_type;
75
76 /* register with core */
77 return driver_register(&drv->driver);
78}
79EXPORT_SYMBOL(of_register_platform_driver);
80
81void of_unregister_platform_driver(struct of_platform_driver *drv)
82{
83 driver_unregister(&drv->driver);
84}
85EXPORT_SYMBOL(of_unregister_platform_driver);
86
87static void of_platform_make_bus_id(struct of_device *dev)
88{
89 struct device_node *node = dev->node;
90 char *name = dev->dev.bus_id;
91 const u32 *reg;
92 u64 addr;
30d36843 93 int magic;
7eebde70
BH
94
95 /*
96 * If it's a DCR based device, use 'd' for native DCRs
97 * and 'D' for MMIO DCRs.
98 */
99#ifdef CONFIG_PPC_DCR
e2eb6392 100 reg = of_get_property(node, "dcr-reg", NULL);
7eebde70
BH
101 if (reg) {
102#ifdef CONFIG_PPC_DCR_NATIVE
103 snprintf(name, BUS_ID_SIZE, "d%x.%s",
104 *reg, node->name);
105#else /* CONFIG_PPC_DCR_NATIVE */
106 addr = of_translate_dcr_address(node, *reg, NULL);
107 if (addr != OF_BAD_ADDR) {
108 snprintf(name, BUS_ID_SIZE,
109 "D%llx.%s", (unsigned long long)addr,
110 node->name);
111 return;
112 }
113#endif /* !CONFIG_PPC_DCR_NATIVE */
114 }
115#endif /* CONFIG_PPC_DCR */
116
117 /*
118 * For MMIO, get the physical address
119 */
e2eb6392 120 reg = of_get_property(node, "reg", NULL);
7eebde70
BH
121 if (reg) {
122 addr = of_translate_address(node, reg);
123 if (addr != OF_BAD_ADDR) {
124 snprintf(name, BUS_ID_SIZE,
125 "%llx.%s", (unsigned long long)addr,
126 node->name);
127 return;
128 }
129 }
130
131 /*
9309180f
BH
132 * No BusID, use the node name and add a globally incremented
133 * counter (and pray...)
7eebde70 134 */
9309180f
BH
135 magic = atomic_add_return(1, &bus_no_reg_magic);
136 snprintf(name, BUS_ID_SIZE, "%s.%d", node->name, magic - 1);
7eebde70
BH
137}
138
139struct of_device* of_platform_device_create(struct device_node *np,
140 const char *bus_id,
141 struct device *parent)
142{
143 struct of_device *dev;
144
dd00cc48 145 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
7eebde70
BH
146 if (!dev)
147 return NULL;
7eebde70
BH
148
149 dev->node = of_node_get(np);
150 dev->dma_mask = 0xffffffffUL;
151 dev->dev.dma_mask = &dev->dma_mask;
152 dev->dev.parent = parent;
153 dev->dev.bus = &of_platform_bus_type;
154 dev->dev.release = of_release_dev;
12d04eef
BH
155 dev->dev.archdata.of_node = np;
156 dev->dev.archdata.numa_node = of_node_to_nid(np);
157
158 /* We do not fill the DMA ops for platform devices by default.
159 * This is currently the responsibility of the platform code
160 * to do such, possibly using a device notifier
161 */
7eebde70
BH
162
163 if (bus_id)
164 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
165 else
166 of_platform_make_bus_id(dev);
167
168 if (of_device_register(dev) != 0) {
169 kfree(dev);
170 return NULL;
171 }
172
173 return dev;
174}
175EXPORT_SYMBOL(of_platform_device_create);
176
177
178
179/**
180 * of_platform_bus_create - Create an OF device for a bus node and all its
181 * children. Optionally recursively instanciate matching busses.
182 * @bus: device node of the bus to instanciate
183 * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
184 * disallow recursive creation of child busses
185 */
186static int of_platform_bus_create(struct device_node *bus,
187 struct of_device_id *matches,
188 struct device *parent)
189{
190 struct device_node *child;
191 struct of_device *dev;
192 int rc = 0;
193
194 for (child = NULL; (child = of_get_next_child(bus, child)); ) {
195 pr_debug(" create child: %s\n", child->full_name);
196 dev = of_platform_device_create(child, NULL, parent);
197 if (dev == NULL)
198 rc = -ENOMEM;
199 else if (!of_match_node(matches, child))
200 continue;
201 if (rc == 0) {
202 pr_debug(" and sub busses\n");
203 rc = of_platform_bus_create(child, matches, &dev->dev);
204 } if (rc) {
205 of_node_put(child);
206 break;
207 }
208 }
209 return rc;
210}
211
212/**
213 * of_platform_bus_probe - Probe the device-tree for platform busses
214 * @root: parent of the first level to probe or NULL for the root of the tree
215 * @matches: match table, NULL to use the default
216 * @parent: parent to hook devices from, NULL for toplevel
217 *
218 * Note that children of the provided root are not instanciated as devices
219 * unless the specified root itself matches the bus list and is not NULL.
220 */
221
222int of_platform_bus_probe(struct device_node *root,
223 struct of_device_id *matches,
224 struct device *parent)
225{
226 struct device_node *child;
227 struct of_device *dev;
228 int rc = 0;
229
230 if (matches == NULL)
231 matches = of_default_bus_ids;
232 if (matches == OF_NO_DEEP_PROBE)
233 return -EINVAL;
234 if (root == NULL)
235 root = of_find_node_by_path("/");
236 else
237 of_node_get(root);
238
239 pr_debug("of_platform_bus_probe()\n");
240 pr_debug(" starting at: %s\n", root->full_name);
241
242 /* Do a self check of bus type, if there's a match, create
243 * children
244 */
245 if (of_match_node(matches, root)) {
246 pr_debug(" root match, create all sub devices\n");
247 dev = of_platform_device_create(root, NULL, parent);
248 if (dev == NULL) {
249 rc = -ENOMEM;
250 goto bail;
251 }
252 pr_debug(" create all sub busses\n");
253 rc = of_platform_bus_create(root, matches, &dev->dev);
254 goto bail;
255 }
256 for (child = NULL; (child = of_get_next_child(root, child)); ) {
257 if (!of_match_node(matches, child))
258 continue;
259
260 pr_debug(" match: %s\n", child->full_name);
261 dev = of_platform_device_create(child, NULL, parent);
262 if (dev == NULL)
263 rc = -ENOMEM;
264 else
265 rc = of_platform_bus_create(child, matches, &dev->dev);
266 if (rc) {
267 of_node_put(child);
268 break;
269 }
270 }
271 bail:
272 of_node_put(root);
273 return rc;
274}
275EXPORT_SYMBOL(of_platform_bus_probe);
276
277static int of_dev_node_match(struct device *dev, void *data)
278{
279 return to_of_device(dev)->node == data;
280}
281
282struct of_device *of_find_device_by_node(struct device_node *np)
283{
284 struct device *dev;
285
286 dev = bus_find_device(&of_platform_bus_type,
287 NULL, np, of_dev_node_match);
288 if (dev)
289 return to_of_device(dev);
290 return NULL;
291}
292EXPORT_SYMBOL(of_find_device_by_node);
293
294static int of_dev_phandle_match(struct device *dev, void *data)
295{
296 phandle *ph = data;
297 return to_of_device(dev)->node->linux_phandle == *ph;
298}
299
300struct of_device *of_find_device_by_phandle(phandle ph)
301{
302 struct device *dev;
303
304 dev = bus_find_device(&of_platform_bus_type,
305 NULL, &ph, of_dev_phandle_match);
306 if (dev)
307 return to_of_device(dev);
308 return NULL;
309}
310EXPORT_SYMBOL(of_find_device_by_phandle);
4c9d2800
BH
311
312
313#ifdef CONFIG_PPC_OF_PLATFORM_PCI
314
315/* The probing of PCI controllers from of_platform is currently
316 * 64 bits only, mostly due to gratuitous differences between
317 * the 32 and 64 bits PCI code on PowerPC and the 32 bits one
318 * lacking some bits needed here.
319 */
320
321static int __devinit of_pci_phb_probe(struct of_device *dev,
322 const struct of_device_id *match)
323{
324 struct pci_controller *phb;
325
326 /* Check if we can do that ... */
327 if (ppc_md.pci_setup_phb == NULL)
328 return -ENODEV;
329
330 printk(KERN_INFO "Setting up PCI bus %s\n", dev->node->full_name);
331
332 /* Alloc and setup PHB data structure */
333 phb = pcibios_alloc_controller(dev->node);
334 if (!phb)
335 return -ENODEV;
336
337 /* Setup parent in sysfs */
338 phb->parent = &dev->dev;
339
340 /* Setup the PHB using arch provided callback */
341 if (ppc_md.pci_setup_phb(phb)) {
342 pcibios_free_controller(phb);
343 return -ENODEV;
344 }
345
346 /* Process "ranges" property */
347 pci_process_bridge_OF_ranges(phb, dev->node, 0);
348
4c9d2800
BH
349 /* Init pci_dn data structures */
350 pci_devs_phb_init_dynamic(phb);
351
352 /* Register devices with EEH */
353#ifdef CONFIG_EEH
354 if (dev->node->child)
355 eeh_add_device_tree_early(dev->node);
356#endif /* CONFIG_EEH */
357
358 /* Scan the bus */
359 scan_phb(phb);
360
361 /* Claim resources. This might need some rework as well depending
362 * wether we are doing probe-only or not, like assigning unassigned
363 * resources etc...
364 */
365 pcibios_claim_one_bus(phb->bus);
366
367 /* Finish EEH setup */
368#ifdef CONFIG_EEH
369 eeh_add_device_tree_late(phb->bus);
370#endif
371
372 /* Add probed PCI devices to the device model */
373 pci_bus_add_devices(phb->bus);
374
375 return 0;
376}
377
378static struct of_device_id of_pci_phb_ids[] = {
379 { .type = "pci", },
380 { .type = "pcix", },
381 { .type = "pcie", },
382 { .type = "pciex", },
383 { .type = "ht", },
384 {}
385};
386
387static struct of_platform_driver of_pci_phb_driver = {
388 .name = "of-pci",
389 .match_table = of_pci_phb_ids,
390 .probe = of_pci_phb_probe,
4c9d2800
BH
391};
392
393static __init int of_pci_phb_init(void)
394{
395 return of_register_platform_driver(&of_pci_phb_driver);
396}
397
398device_initcall(of_pci_phb_init);
399
400#endif /* CONFIG_PPC_OF_PLATFORM_PCI */