Commit | Line | Data |
---|---|---|
fd531431 DM |
1 | #include <linux/config.h> |
2 | #include <linux/string.h> | |
3 | #include <linux/kernel.h> | |
4 | #include <linux/init.h> | |
5 | #include <linux/module.h> | |
6 | #include <linux/mod_devicetable.h> | |
7 | #include <linux/slab.h> | |
8 | ||
9 | #include <asm/errno.h> | |
10 | #include <asm/of_device.h> | |
11 | ||
12 | /** | |
13 | * of_match_device - Tell if an of_device structure has a matching | |
14 | * of_match structure | |
15 | * @ids: array of of device match structures to search in | |
16 | * @dev: the of device structure to match against | |
17 | * | |
18 | * Used by a driver to check whether an of_device present in the | |
19 | * system is in its list of supported devices. | |
20 | */ | |
21 | const struct of_device_id *of_match_device(const struct of_device_id *matches, | |
22 | const struct of_device *dev) | |
23 | { | |
24 | if (!dev->node) | |
25 | return NULL; | |
26 | while (matches->name[0] || matches->type[0] || matches->compatible[0]) { | |
27 | int match = 1; | |
28 | if (matches->name[0]) | |
29 | match &= dev->node->name | |
30 | && !strcmp(matches->name, dev->node->name); | |
31 | if (matches->type[0]) | |
32 | match &= dev->node->type | |
33 | && !strcmp(matches->type, dev->node->type); | |
34 | if (matches->compatible[0]) | |
35 | match &= of_device_is_compatible(dev->node, | |
36 | matches->compatible); | |
37 | if (match) | |
38 | return matches; | |
39 | matches++; | |
40 | } | |
41 | return NULL; | |
42 | } | |
43 | ||
44 | static int of_platform_bus_match(struct device *dev, struct device_driver *drv) | |
45 | { | |
46 | struct of_device * of_dev = to_of_device(dev); | |
47 | struct of_platform_driver * of_drv = to_of_platform_driver(drv); | |
48 | const struct of_device_id * matches = of_drv->match_table; | |
49 | ||
50 | if (!matches) | |
51 | return 0; | |
52 | ||
53 | return of_match_device(matches, of_dev) != NULL; | |
54 | } | |
55 | ||
56 | struct of_device *of_dev_get(struct of_device *dev) | |
57 | { | |
58 | struct device *tmp; | |
59 | ||
60 | if (!dev) | |
61 | return NULL; | |
62 | tmp = get_device(&dev->dev); | |
63 | if (tmp) | |
64 | return to_of_device(tmp); | |
65 | else | |
66 | return NULL; | |
67 | } | |
68 | ||
69 | void of_dev_put(struct of_device *dev) | |
70 | { | |
71 | if (dev) | |
72 | put_device(&dev->dev); | |
73 | } | |
74 | ||
75 | ||
76 | static int of_device_probe(struct device *dev) | |
77 | { | |
78 | int error = -ENODEV; | |
79 | struct of_platform_driver *drv; | |
80 | struct of_device *of_dev; | |
81 | const struct of_device_id *match; | |
82 | ||
83 | drv = to_of_platform_driver(dev->driver); | |
84 | of_dev = to_of_device(dev); | |
85 | ||
86 | if (!drv->probe) | |
87 | return error; | |
88 | ||
89 | of_dev_get(of_dev); | |
90 | ||
91 | match = of_match_device(drv->match_table, of_dev); | |
92 | if (match) | |
93 | error = drv->probe(of_dev, match); | |
94 | if (error) | |
95 | of_dev_put(of_dev); | |
96 | ||
97 | return error; | |
98 | } | |
99 | ||
100 | static int of_device_remove(struct device *dev) | |
101 | { | |
102 | struct of_device * of_dev = to_of_device(dev); | |
103 | struct of_platform_driver * drv = to_of_platform_driver(dev->driver); | |
104 | ||
105 | if (dev->driver && drv->remove) | |
106 | drv->remove(of_dev); | |
107 | return 0; | |
108 | } | |
109 | ||
110 | static int of_device_suspend(struct device *dev, pm_message_t state) | |
111 | { | |
112 | struct of_device * of_dev = to_of_device(dev); | |
113 | struct of_platform_driver * drv = to_of_platform_driver(dev->driver); | |
114 | int error = 0; | |
115 | ||
116 | if (dev->driver && drv->suspend) | |
117 | error = drv->suspend(of_dev, state); | |
118 | return error; | |
119 | } | |
120 | ||
121 | static int of_device_resume(struct device * dev) | |
122 | { | |
123 | struct of_device * of_dev = to_of_device(dev); | |
124 | struct of_platform_driver * drv = to_of_platform_driver(dev->driver); | |
125 | int error = 0; | |
126 | ||
127 | if (dev->driver && drv->resume) | |
128 | error = drv->resume(of_dev); | |
129 | return error; | |
130 | } | |
131 | ||
8f96cd1a DM |
132 | static int node_match(struct device *dev, void *data) |
133 | { | |
134 | struct of_device *op = to_of_device(dev); | |
135 | struct device_node *dp = data; | |
136 | ||
137 | return (op->node == dp); | |
138 | } | |
139 | ||
140 | struct of_device *of_find_device_by_node(struct device_node *dp) | |
141 | { | |
142 | struct device *dev = bus_find_device(&of_bus_type, NULL, | |
143 | dp, node_match); | |
144 | ||
145 | if (dev) | |
146 | return to_of_device(dev); | |
147 | ||
148 | return NULL; | |
149 | } | |
150 | EXPORT_SYMBOL(of_find_device_by_node); | |
151 | ||
fd531431 DM |
152 | #ifdef CONFIG_PCI |
153 | struct bus_type ebus_bus_type = { | |
154 | .name = "ebus", | |
155 | .match = of_platform_bus_match, | |
156 | .probe = of_device_probe, | |
157 | .remove = of_device_remove, | |
158 | .suspend = of_device_suspend, | |
159 | .resume = of_device_resume, | |
160 | }; | |
69853918 | 161 | EXPORT_SYMBOL(ebus_bus_type); |
fd531431 DM |
162 | #endif |
163 | ||
164 | #ifdef CONFIG_SBUS | |
165 | struct bus_type sbus_bus_type = { | |
166 | .name = "sbus", | |
167 | .match = of_platform_bus_match, | |
168 | .probe = of_device_probe, | |
169 | .remove = of_device_remove, | |
170 | .suspend = of_device_suspend, | |
171 | .resume = of_device_resume, | |
172 | }; | |
69853918 | 173 | EXPORT_SYMBOL(sbus_bus_type); |
fd531431 DM |
174 | #endif |
175 | ||
cf44bbc2 DM |
176 | struct bus_type of_bus_type = { |
177 | .name = "of", | |
178 | .match = of_platform_bus_match, | |
179 | .probe = of_device_probe, | |
180 | .remove = of_device_remove, | |
181 | .suspend = of_device_suspend, | |
182 | .resume = of_device_resume, | |
183 | }; | |
184 | EXPORT_SYMBOL(of_bus_type); | |
185 | ||
a83f9823 | 186 | static inline u64 of_read_addr(const u32 *cell, int size) |
cf44bbc2 DM |
187 | { |
188 | u64 r = 0; | |
189 | while (size--) | |
190 | r = (r << 32) | *(cell++); | |
191 | return r; | |
192 | } | |
193 | ||
194 | static void __init get_cells(struct device_node *dp, | |
195 | int *addrc, int *sizec) | |
196 | { | |
197 | if (addrc) | |
198 | *addrc = of_n_addr_cells(dp); | |
199 | if (sizec) | |
200 | *sizec = of_n_size_cells(dp); | |
201 | } | |
202 | ||
203 | /* Max address size we deal with */ | |
204 | #define OF_MAX_ADDR_CELLS 4 | |
205 | ||
206 | struct of_bus { | |
207 | const char *name; | |
208 | const char *addr_prop_name; | |
209 | int (*match)(struct device_node *parent); | |
210 | void (*count_cells)(struct device_node *child, | |
211 | int *addrc, int *sizec); | |
a83f9823 DM |
212 | int (*map)(u32 *addr, const u32 *range, |
213 | int na, int ns, int pna); | |
cf44bbc2 DM |
214 | unsigned int (*get_flags)(u32 *addr); |
215 | }; | |
216 | ||
217 | /* | |
218 | * Default translator (generic bus) | |
219 | */ | |
220 | ||
221 | static void of_bus_default_count_cells(struct device_node *dev, | |
222 | int *addrc, int *sizec) | |
223 | { | |
224 | get_cells(dev, addrc, sizec); | |
225 | } | |
226 | ||
a83f9823 DM |
227 | /* Make sure the least significant 64-bits are in-range. Even |
228 | * for 3 or 4 cell values it is a good enough approximation. | |
229 | */ | |
230 | static int of_out_of_range(const u32 *addr, const u32 *base, | |
231 | const u32 *size, int na, int ns) | |
cf44bbc2 | 232 | { |
a83f9823 DM |
233 | u64 a = of_read_addr(addr, na); |
234 | u64 b = of_read_addr(base, na); | |
235 | ||
236 | if (a < b) | |
237 | return 1; | |
cf44bbc2 | 238 | |
a83f9823 DM |
239 | b += of_read_addr(size, ns); |
240 | if (a >= b) | |
241 | return 1; | |
cf44bbc2 | 242 | |
a83f9823 | 243 | return 0; |
cf44bbc2 DM |
244 | } |
245 | ||
a83f9823 DM |
246 | static int of_bus_default_map(u32 *addr, const u32 *range, |
247 | int na, int ns, int pna) | |
cf44bbc2 | 248 | { |
a83f9823 DM |
249 | u32 result[OF_MAX_ADDR_CELLS]; |
250 | int i; | |
251 | ||
252 | if (ns > 2) { | |
253 | printk("of_device: Cannot handle size cells (%d) > 2.", ns); | |
254 | return -EINVAL; | |
255 | } | |
256 | ||
257 | if (of_out_of_range(addr, range, range + na + pna, na, ns)) | |
258 | return -EINVAL; | |
259 | ||
260 | /* Start with the parent range base. */ | |
261 | memcpy(result, range + na, pna * 4); | |
262 | ||
263 | /* Add in the child address offset. */ | |
264 | for (i = 0; i < na; i++) | |
265 | result[pna - 1 - i] += | |
266 | (addr[na - 1 - i] - | |
267 | range[na - 1 - i]); | |
268 | ||
269 | memcpy(addr, result, pna * 4); | |
cf44bbc2 DM |
270 | |
271 | return 0; | |
272 | } | |
273 | ||
274 | static unsigned int of_bus_default_get_flags(u32 *addr) | |
275 | { | |
276 | return IORESOURCE_MEM; | |
277 | } | |
278 | ||
cf44bbc2 DM |
279 | /* |
280 | * PCI bus specific translator | |
281 | */ | |
282 | ||
283 | static int of_bus_pci_match(struct device_node *np) | |
284 | { | |
a83f9823 DM |
285 | if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) { |
286 | /* Do not do PCI specific frobbing if the | |
287 | * PCI bridge lacks a ranges property. We | |
288 | * want to pass it through up to the next | |
289 | * parent as-is, not with the PCI translate | |
290 | * method which chops off the top address cell. | |
291 | */ | |
292 | if (!of_find_property(np, "ranges", NULL)) | |
293 | return 0; | |
294 | ||
295 | return 1; | |
296 | } | |
297 | ||
298 | return 0; | |
cf44bbc2 DM |
299 | } |
300 | ||
301 | static void of_bus_pci_count_cells(struct device_node *np, | |
302 | int *addrc, int *sizec) | |
303 | { | |
304 | if (addrc) | |
305 | *addrc = 3; | |
306 | if (sizec) | |
307 | *sizec = 2; | |
308 | } | |
309 | ||
a83f9823 DM |
310 | static int of_bus_pci_map(u32 *addr, const u32 *range, |
311 | int na, int ns, int pna) | |
cf44bbc2 | 312 | { |
a83f9823 DM |
313 | u32 result[OF_MAX_ADDR_CELLS]; |
314 | int i; | |
cf44bbc2 DM |
315 | |
316 | /* Check address type match */ | |
317 | if ((addr[0] ^ range[0]) & 0x03000000) | |
a83f9823 | 318 | return -EINVAL; |
cf44bbc2 | 319 | |
a83f9823 DM |
320 | if (of_out_of_range(addr + 1, range + 1, range + na + pna, |
321 | na - 1, ns)) | |
322 | return -EINVAL; | |
cf44bbc2 | 323 | |
a83f9823 DM |
324 | /* Start with the parent range base. */ |
325 | memcpy(result, range + na, pna * 4); | |
cf44bbc2 | 326 | |
a83f9823 DM |
327 | /* Add in the child address offset, skipping high cell. */ |
328 | for (i = 0; i < na - 1; i++) | |
329 | result[pna - 1 - i] += | |
330 | (addr[na - 1 - i] - | |
331 | range[na - 1 - i]); | |
332 | ||
333 | memcpy(addr, result, pna * 4); | |
334 | ||
335 | return 0; | |
cf44bbc2 DM |
336 | } |
337 | ||
338 | static unsigned int of_bus_pci_get_flags(u32 *addr) | |
339 | { | |
340 | unsigned int flags = 0; | |
341 | u32 w = addr[0]; | |
342 | ||
343 | switch((w >> 24) & 0x03) { | |
344 | case 0x01: | |
345 | flags |= IORESOURCE_IO; | |
346 | case 0x02: /* 32 bits */ | |
347 | case 0x03: /* 64 bits */ | |
348 | flags |= IORESOURCE_MEM; | |
349 | } | |
350 | if (w & 0x40000000) | |
351 | flags |= IORESOURCE_PREFETCH; | |
352 | return flags; | |
353 | } | |
354 | ||
355 | /* | |
356 | * SBUS bus specific translator | |
357 | */ | |
358 | ||
359 | static int of_bus_sbus_match(struct device_node *np) | |
360 | { | |
361 | return !strcmp(np->name, "sbus") || | |
362 | !strcmp(np->name, "sbi"); | |
363 | } | |
364 | ||
365 | static void of_bus_sbus_count_cells(struct device_node *child, | |
366 | int *addrc, int *sizec) | |
367 | { | |
368 | if (addrc) | |
369 | *addrc = 2; | |
370 | if (sizec) | |
371 | *sizec = 1; | |
372 | } | |
373 | ||
a83f9823 | 374 | static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna) |
cf44bbc2 DM |
375 | { |
376 | return of_bus_default_map(addr, range, na, ns, pna); | |
377 | } | |
378 | ||
cf44bbc2 DM |
379 | static unsigned int of_bus_sbus_get_flags(u32 *addr) |
380 | { | |
381 | return IORESOURCE_MEM; | |
382 | } | |
383 | ||
384 | ||
385 | /* | |
386 | * Array of bus specific translators | |
387 | */ | |
388 | ||
389 | static struct of_bus of_busses[] = { | |
390 | /* PCI */ | |
391 | { | |
392 | .name = "pci", | |
393 | .addr_prop_name = "assigned-addresses", | |
394 | .match = of_bus_pci_match, | |
395 | .count_cells = of_bus_pci_count_cells, | |
396 | .map = of_bus_pci_map, | |
cf44bbc2 DM |
397 | .get_flags = of_bus_pci_get_flags, |
398 | }, | |
399 | /* SBUS */ | |
400 | { | |
401 | .name = "sbus", | |
402 | .addr_prop_name = "reg", | |
403 | .match = of_bus_sbus_match, | |
404 | .count_cells = of_bus_sbus_count_cells, | |
405 | .map = of_bus_sbus_map, | |
cf44bbc2 DM |
406 | .get_flags = of_bus_sbus_get_flags, |
407 | }, | |
408 | /* Default */ | |
409 | { | |
410 | .name = "default", | |
411 | .addr_prop_name = "reg", | |
412 | .match = NULL, | |
413 | .count_cells = of_bus_default_count_cells, | |
414 | .map = of_bus_default_map, | |
cf44bbc2 DM |
415 | .get_flags = of_bus_default_get_flags, |
416 | }, | |
417 | }; | |
418 | ||
419 | static struct of_bus *of_match_bus(struct device_node *np) | |
420 | { | |
421 | int i; | |
422 | ||
423 | for (i = 0; i < ARRAY_SIZE(of_busses); i ++) | |
424 | if (!of_busses[i].match || of_busses[i].match(np)) | |
425 | return &of_busses[i]; | |
426 | BUG(); | |
427 | return NULL; | |
428 | } | |
429 | ||
430 | static int __init build_one_resource(struct device_node *parent, | |
431 | struct of_bus *bus, | |
432 | struct of_bus *pbus, | |
433 | u32 *addr, | |
434 | int na, int ns, int pna) | |
435 | { | |
436 | u32 *ranges; | |
437 | unsigned int rlen; | |
438 | int rone; | |
cf44bbc2 DM |
439 | |
440 | ranges = of_get_property(parent, "ranges", &rlen); | |
441 | if (ranges == NULL || rlen == 0) { | |
a83f9823 DM |
442 | u32 result[OF_MAX_ADDR_CELLS]; |
443 | int i; | |
444 | ||
445 | memset(result, 0, pna * 4); | |
446 | for (i = 0; i < na; i++) | |
447 | result[pna - 1 - i] = | |
448 | addr[na - 1 - i]; | |
449 | ||
450 | memcpy(addr, result, pna * 4); | |
451 | return 0; | |
cf44bbc2 DM |
452 | } |
453 | ||
454 | /* Now walk through the ranges */ | |
455 | rlen /= 4; | |
456 | rone = na + pna + ns; | |
457 | for (; rlen >= rone; rlen -= rone, ranges += rone) { | |
a83f9823 DM |
458 | if (!bus->map(addr, ranges, na, ns, pna)) |
459 | return 0; | |
cf44bbc2 | 460 | } |
cf44bbc2 | 461 | |
a83f9823 | 462 | return 1; |
cf44bbc2 DM |
463 | } |
464 | ||
a83f9823 DM |
465 | static int of_resource_verbose; |
466 | ||
cf44bbc2 DM |
467 | static void __init build_device_resources(struct of_device *op, |
468 | struct device *parent) | |
469 | { | |
470 | struct of_device *p_op; | |
471 | struct of_bus *bus; | |
472 | int na, ns; | |
473 | int index, num_reg; | |
474 | void *preg; | |
475 | ||
476 | if (!parent) | |
477 | return; | |
478 | ||
479 | p_op = to_of_device(parent); | |
480 | bus = of_match_bus(p_op->node); | |
481 | bus->count_cells(op->node, &na, &ns); | |
482 | ||
483 | preg = of_get_property(op->node, bus->addr_prop_name, &num_reg); | |
484 | if (!preg || num_reg == 0) | |
485 | return; | |
486 | ||
487 | /* Convert to num-cells. */ | |
488 | num_reg /= 4; | |
489 | ||
490 | /* Conver to num-entries. */ | |
491 | num_reg /= na + ns; | |
492 | ||
493 | for (index = 0; index < num_reg; index++) { | |
494 | struct resource *r = &op->resource[index]; | |
495 | u32 addr[OF_MAX_ADDR_CELLS]; | |
496 | u32 *reg = (preg + (index * ((na + ns) * 4))); | |
497 | struct device_node *dp = op->node; | |
498 | struct device_node *pp = p_op->node; | |
499 | struct of_bus *pbus; | |
500 | u64 size, result = OF_BAD_ADDR; | |
501 | unsigned long flags; | |
502 | int dna, dns; | |
503 | int pna, pns; | |
504 | ||
505 | size = of_read_addr(reg + na, ns); | |
506 | flags = bus->get_flags(reg); | |
507 | ||
508 | memcpy(addr, reg, na * 4); | |
509 | ||
510 | /* If the immediate parent has no ranges property to apply, | |
511 | * just use a 1<->1 mapping. | |
512 | */ | |
513 | if (of_find_property(pp, "ranges", NULL) == NULL) { | |
514 | result = of_read_addr(addr, na); | |
515 | goto build_res; | |
516 | } | |
517 | ||
518 | dna = na; | |
519 | dns = ns; | |
520 | ||
521 | while (1) { | |
522 | dp = pp; | |
523 | pp = dp->parent; | |
524 | if (!pp) { | |
525 | result = of_read_addr(addr, dna); | |
526 | break; | |
527 | } | |
528 | ||
529 | pbus = of_match_bus(pp); | |
530 | pbus->count_cells(dp, &pna, &pns); | |
531 | ||
a83f9823 DM |
532 | if (build_one_resource(dp, bus, pbus, addr, |
533 | dna, dns, pna)) | |
cf44bbc2 DM |
534 | break; |
535 | ||
536 | dna = pna; | |
537 | dns = pns; | |
538 | bus = pbus; | |
539 | } | |
540 | ||
541 | build_res: | |
542 | memset(r, 0, sizeof(*r)); | |
a83f9823 DM |
543 | |
544 | if (of_resource_verbose) | |
545 | printk("%s reg[%d] -> %llx\n", | |
546 | op->node->full_name, index, | |
547 | result); | |
548 | ||
cf44bbc2 | 549 | if (result != OF_BAD_ADDR) { |
95714e12 | 550 | r->start = result & 0xffffffff; |
cf44bbc2 | 551 | r->end = result + size - 1; |
95714e12 | 552 | r->flags = flags | ((result >> 32ULL) & 0xffUL); |
cf44bbc2 DM |
553 | } else { |
554 | r->start = ~0UL; | |
555 | r->end = ~0UL; | |
556 | } | |
557 | r->name = op->node->name; | |
558 | } | |
559 | } | |
560 | ||
561 | static struct of_device * __init scan_one_device(struct device_node *dp, | |
562 | struct device *parent) | |
563 | { | |
564 | struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL); | |
8f96cd1a DM |
565 | struct linux_prom_irqs *intr; |
566 | int len, i; | |
cf44bbc2 DM |
567 | |
568 | if (!op) | |
569 | return NULL; | |
570 | ||
571 | op->node = dp; | |
572 | ||
573 | op->clock_freq = of_getintprop_default(dp, "clock-frequency", | |
574 | (25*1000*1000)); | |
575 | op->portid = of_getintprop_default(dp, "upa-portid", -1); | |
576 | if (op->portid == -1) | |
577 | op->portid = of_getintprop_default(dp, "portid", -1); | |
578 | ||
8f96cd1a DM |
579 | intr = of_get_property(dp, "intr", &len); |
580 | if (intr) { | |
581 | op->num_irqs = len / sizeof(struct linux_prom_irqs); | |
582 | for (i = 0; i < op->num_irqs; i++) | |
583 | op->irqs[i] = intr[i].pri; | |
584 | } else { | |
585 | unsigned int *irq = of_get_property(dp, "interrupts", &len); | |
586 | ||
587 | if (irq) { | |
588 | op->num_irqs = len / sizeof(unsigned int); | |
589 | for (i = 0; i < op->num_irqs; i++) | |
590 | op->irqs[i] = irq[i]; | |
591 | } else { | |
592 | op->num_irqs = 0; | |
593 | } | |
594 | } | |
595 | if (sparc_cpu_model == sun4d) { | |
596 | static int pil_to_sbus[] = { | |
597 | 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0, | |
598 | }; | |
599 | struct device_node *busp = dp->parent; | |
600 | struct linux_prom_registers *regs; | |
601 | int board = of_getintprop_default(busp, "board#", 0); | |
602 | int slot; | |
603 | ||
604 | regs = of_get_property(dp, "reg", NULL); | |
605 | slot = regs->which_io; | |
606 | ||
607 | for (i = 0; i < op->num_irqs; i++) { | |
608 | int this_irq = op->irqs[i]; | |
609 | int sbusl = pil_to_sbus[this_irq]; | |
610 | ||
611 | if (sbusl) | |
612 | this_irq = (((board + 1) << 5) + | |
613 | (sbusl << 2) + | |
614 | slot); | |
615 | ||
616 | op->irqs[i] = this_irq; | |
617 | } | |
618 | } | |
cf44bbc2 DM |
619 | |
620 | build_device_resources(op, parent); | |
621 | ||
622 | op->dev.parent = parent; | |
623 | op->dev.bus = &of_bus_type; | |
624 | if (!parent) | |
625 | strcpy(op->dev.bus_id, "root"); | |
626 | else | |
627 | strcpy(op->dev.bus_id, dp->path_component_name); | |
628 | ||
629 | if (of_device_register(op)) { | |
630 | printk("%s: Could not register of device.\n", | |
631 | dp->full_name); | |
632 | kfree(op); | |
633 | op = NULL; | |
634 | } | |
635 | ||
636 | return op; | |
637 | } | |
638 | ||
639 | static void __init scan_tree(struct device_node *dp, struct device *parent) | |
640 | { | |
641 | while (dp) { | |
642 | struct of_device *op = scan_one_device(dp, parent); | |
643 | ||
644 | if (op) | |
645 | scan_tree(dp->child, &op->dev); | |
646 | ||
647 | dp = dp->sibling; | |
648 | } | |
649 | } | |
650 | ||
651 | static void __init scan_of_devices(void) | |
652 | { | |
653 | struct device_node *root = of_find_node_by_path("/"); | |
654 | struct of_device *parent; | |
655 | ||
656 | parent = scan_one_device(root, NULL); | |
657 | if (!parent) | |
658 | return; | |
659 | ||
660 | scan_tree(root->child, &parent->dev); | |
661 | } | |
662 | ||
fd531431 DM |
663 | static int __init of_bus_driver_init(void) |
664 | { | |
cf44bbc2 | 665 | int err; |
fd531431 | 666 | |
cf44bbc2 | 667 | err = bus_register(&of_bus_type); |
fd531431 DM |
668 | #ifdef CONFIG_PCI |
669 | if (!err) | |
670 | err = bus_register(&ebus_bus_type); | |
671 | #endif | |
672 | #ifdef CONFIG_SBUS | |
673 | if (!err) | |
674 | err = bus_register(&sbus_bus_type); | |
675 | #endif | |
cf44bbc2 DM |
676 | |
677 | if (!err) | |
678 | scan_of_devices(); | |
679 | ||
680 | return err; | |
fd531431 DM |
681 | } |
682 | ||
683 | postcore_initcall(of_bus_driver_init); | |
684 | ||
a83f9823 DM |
685 | static int __init of_debug(char *str) |
686 | { | |
687 | int val = 0; | |
688 | ||
689 | get_option(&str, &val); | |
690 | if (val & 1) | |
691 | of_resource_verbose = 1; | |
692 | return 1; | |
693 | } | |
694 | ||
695 | __setup("of_debug=", of_debug); | |
696 | ||
fd531431 DM |
697 | int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus) |
698 | { | |
699 | /* initialize common driver fields */ | |
700 | drv->driver.name = drv->name; | |
701 | drv->driver.bus = bus; | |
702 | ||
703 | /* register with core */ | |
704 | return driver_register(&drv->driver); | |
705 | } | |
706 | ||
707 | void of_unregister_driver(struct of_platform_driver *drv) | |
708 | { | |
709 | driver_unregister(&drv->driver); | |
710 | } | |
711 | ||
712 | ||
713 | static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf) | |
714 | { | |
715 | struct of_device *ofdev; | |
716 | ||
717 | ofdev = to_of_device(dev); | |
718 | return sprintf(buf, "%s", ofdev->node->full_name); | |
719 | } | |
720 | ||
721 | static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL); | |
722 | ||
723 | /** | |
724 | * of_release_dev - free an of device structure when all users of it are finished. | |
725 | * @dev: device that's been disconnected | |
726 | * | |
727 | * Will be called only by the device core when all users of this of device are | |
728 | * done. | |
729 | */ | |
730 | void of_release_dev(struct device *dev) | |
731 | { | |
732 | struct of_device *ofdev; | |
733 | ||
734 | ofdev = to_of_device(dev); | |
735 | ||
736 | kfree(ofdev); | |
737 | } | |
738 | ||
739 | int of_device_register(struct of_device *ofdev) | |
740 | { | |
741 | int rc; | |
742 | ||
743 | BUG_ON(ofdev->node == NULL); | |
744 | ||
745 | rc = device_register(&ofdev->dev); | |
746 | if (rc) | |
747 | return rc; | |
748 | ||
6cc8b6f5 AM |
749 | rc = device_create_file(&ofdev->dev, &dev_attr_devspec); |
750 | if (rc) | |
751 | device_unregister(&ofdev->dev); | |
fd531431 | 752 | |
6cc8b6f5 | 753 | return rc; |
fd531431 DM |
754 | } |
755 | ||
756 | void of_device_unregister(struct of_device *ofdev) | |
757 | { | |
758 | device_remove_file(&ofdev->dev, &dev_attr_devspec); | |
759 | device_unregister(&ofdev->dev); | |
760 | } | |
761 | ||
762 | struct of_device* of_platform_device_create(struct device_node *np, | |
763 | const char *bus_id, | |
764 | struct device *parent, | |
765 | struct bus_type *bus) | |
766 | { | |
767 | struct of_device *dev; | |
768 | ||
769 | dev = kmalloc(sizeof(*dev), GFP_KERNEL); | |
770 | if (!dev) | |
771 | return NULL; | |
772 | memset(dev, 0, sizeof(*dev)); | |
773 | ||
774 | dev->dev.parent = parent; | |
775 | dev->dev.bus = bus; | |
776 | dev->dev.release = of_release_dev; | |
777 | ||
778 | strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE); | |
779 | ||
780 | if (of_device_register(dev) != 0) { | |
781 | kfree(dev); | |
782 | return NULL; | |
783 | } | |
784 | ||
785 | return dev; | |
786 | } | |
787 | ||
788 | EXPORT_SYMBOL(of_match_device); | |
789 | EXPORT_SYMBOL(of_register_driver); | |
790 | EXPORT_SYMBOL(of_unregister_driver); | |
791 | EXPORT_SYMBOL(of_device_register); | |
792 | EXPORT_SYMBOL(of_device_unregister); | |
793 | EXPORT_SYMBOL(of_dev_get); | |
794 | EXPORT_SYMBOL(of_dev_put); | |
795 | EXPORT_SYMBOL(of_platform_device_create); | |
796 | EXPORT_SYMBOL(of_release_dev); |