aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/of
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/of')
-rw-r--r--drivers/of/Kconfig40
-rw-r--r--drivers/of/Makefile2
-rw-r--r--drivers/of/address.c595
-rw-r--r--drivers/of/base.c76
-rw-r--r--drivers/of/device.c91
-rw-r--r--drivers/of/fdt.c26
-rw-r--r--drivers/of/gpio.c93
-rw-r--r--drivers/of/irq.c349
-rw-r--r--drivers/of/of_i2c.c50
-rw-r--r--drivers/of/of_mdio.c1
-rw-r--r--drivers/of/of_spi.c11
-rw-r--r--drivers/of/platform.c384
12 files changed, 1548 insertions, 170 deletions
diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig
index 7cecc8fea9bd..6acbff389ab6 100644
--- a/drivers/of/Kconfig
+++ b/drivers/of/Kconfig
@@ -1,35 +1,61 @@
1config OF_FLATTREE 1config DTC
2 bool
3
4config OF
2 bool 5 bool
6
7menu "Flattened Device Tree and Open Firmware support"
3 depends on OF 8 depends on OF
4 9
10config PROC_DEVICETREE
11 bool "Support for device tree in /proc"
12 depends on PROC_FS && !SPARC
13 help
14 This option adds a device-tree directory under /proc which contains
15 an image of the device tree that the kernel copies from Open
16 Firmware or other boot firmware. If unsure, say Y here.
17
18config OF_FLATTREE
19 bool
20 select DTC
21
5config OF_DYNAMIC 22config OF_DYNAMIC
6 def_bool y 23 def_bool y
7 depends on OF && PPC_OF 24 depends on PPC_OF
25
26config OF_ADDRESS
27 def_bool y
28 depends on !SPARC
29
30config OF_IRQ
31 def_bool y
32 depends on !SPARC
8 33
9config OF_DEVICE 34config OF_DEVICE
10 def_bool y 35 def_bool y
11 depends on OF && (SPARC || PPC_OF || MICROBLAZE)
12 36
13config OF_GPIO 37config OF_GPIO
14 def_bool y 38 def_bool y
15 depends on OF && (PPC_OF || MICROBLAZE) && GPIOLIB 39 depends on GPIOLIB && !SPARC
16 help 40 help
17 OpenFirmware GPIO accessors 41 OpenFirmware GPIO accessors
18 42
19config OF_I2C 43config OF_I2C
20 def_tristate I2C 44 def_tristate I2C
21 depends on (PPC_OF || MICROBLAZE) && I2C 45 depends on I2C && !SPARC
22 help 46 help
23 OpenFirmware I2C accessors 47 OpenFirmware I2C accessors
24 48
25config OF_SPI 49config OF_SPI
26 def_tristate SPI 50 def_tristate SPI
27 depends on OF && (PPC_OF || MICROBLAZE) && SPI 51 depends on SPI && !SPARC
28 help 52 help
29 OpenFirmware SPI accessors 53 OpenFirmware SPI accessors
30 54
31config OF_MDIO 55config OF_MDIO
32 def_tristate PHYLIB 56 def_tristate PHYLIB
33 depends on OF && PHYLIB 57 depends on PHYLIB
34 help 58 help
35 OpenFirmware MDIO bus (Ethernet PHY) accessors 59 OpenFirmware MDIO bus (Ethernet PHY) accessors
60
61endmenu # OF
diff --git a/drivers/of/Makefile b/drivers/of/Makefile
index f232cc98ce00..0052c405463a 100644
--- a/drivers/of/Makefile
+++ b/drivers/of/Makefile
@@ -1,5 +1,7 @@
1obj-y = base.o 1obj-y = base.o
2obj-$(CONFIG_OF_FLATTREE) += fdt.o 2obj-$(CONFIG_OF_FLATTREE) += fdt.o
3obj-$(CONFIG_OF_ADDRESS) += address.o
4obj-$(CONFIG_OF_IRQ) += irq.o
3obj-$(CONFIG_OF_DEVICE) += device.o platform.o 5obj-$(CONFIG_OF_DEVICE) += device.o platform.o
4obj-$(CONFIG_OF_GPIO) += gpio.o 6obj-$(CONFIG_OF_GPIO) += gpio.o
5obj-$(CONFIG_OF_I2C) += of_i2c.o 7obj-$(CONFIG_OF_I2C) += of_i2c.o
diff --git a/drivers/of/address.c b/drivers/of/address.c
new file mode 100644
index 000000000000..fcadb726d4f9
--- /dev/null
+++ b/drivers/of/address.c
@@ -0,0 +1,595 @@
1
2#include <linux/io.h>
3#include <linux/ioport.h>
4#include <linux/module.h>
5#include <linux/of_address.h>
6#include <linux/pci_regs.h>
7#include <linux/string.h>
8
9/* Max address size we deal with */
10#define OF_MAX_ADDR_CELLS 4
11#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
12 (ns) > 0)
13
14static struct of_bus *of_match_bus(struct device_node *np);
15static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
16 u64 size, unsigned int flags,
17 struct resource *r);
18
19/* Debug utility */
20#ifdef DEBUG
21static void of_dump_addr(const char *s, const u32 *addr, int na)
22{
23 printk(KERN_DEBUG "%s", s);
24 while (na--)
25 printk(" %08x", be32_to_cpu(*(addr++)));
26 printk("\n");
27}
28#else
29static void of_dump_addr(const char *s, const u32 *addr, int na) { }
30#endif
31
32/* Callbacks for bus specific translators */
33struct of_bus {
34 const char *name;
35 const char *addresses;
36 int (*match)(struct device_node *parent);
37 void (*count_cells)(struct device_node *child,
38 int *addrc, int *sizec);
39 u64 (*map)(u32 *addr, const u32 *range,
40 int na, int ns, int pna);
41 int (*translate)(u32 *addr, u64 offset, int na);
42 unsigned int (*get_flags)(const u32 *addr);
43};
44
45/*
46 * Default translator (generic bus)
47 */
48
49static void of_bus_default_count_cells(struct device_node *dev,
50 int *addrc, int *sizec)
51{
52 if (addrc)
53 *addrc = of_n_addr_cells(dev);
54 if (sizec)
55 *sizec = of_n_size_cells(dev);
56}
57
58static u64 of_bus_default_map(u32 *addr, const u32 *range,
59 int na, int ns, int pna)
60{
61 u64 cp, s, da;
62
63 cp = of_read_number(range, na);
64 s = of_read_number(range + na + pna, ns);
65 da = of_read_number(addr, na);
66
67 pr_debug("OF: default map, cp=%llx, s=%llx, da=%llx\n",
68 (unsigned long long)cp, (unsigned long long)s,
69 (unsigned long long)da);
70
71 if (da < cp || da >= (cp + s))
72 return OF_BAD_ADDR;
73 return da - cp;
74}
75
76static int of_bus_default_translate(u32 *addr, u64 offset, int na)
77{
78 u64 a = of_read_number(addr, na);
79 memset(addr, 0, na * 4);
80 a += offset;
81 if (na > 1)
82 addr[na - 2] = cpu_to_be32(a >> 32);
83 addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
84
85 return 0;
86}
87
88static unsigned int of_bus_default_get_flags(const u32 *addr)
89{
90 return IORESOURCE_MEM;
91}
92
93#ifdef CONFIG_PCI
94/*
95 * PCI bus specific translator
96 */
97
98static int of_bus_pci_match(struct device_node *np)
99{
100 /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */
101 return !strcmp(np->type, "pci") || !strcmp(np->type, "vci");
102}
103
104static void of_bus_pci_count_cells(struct device_node *np,
105 int *addrc, int *sizec)
106{
107 if (addrc)
108 *addrc = 3;
109 if (sizec)
110 *sizec = 2;
111}
112
113static unsigned int of_bus_pci_get_flags(const u32 *addr)
114{
115 unsigned int flags = 0;
116 u32 w = addr[0];
117
118 switch((w >> 24) & 0x03) {
119 case 0x01:
120 flags |= IORESOURCE_IO;
121 break;
122 case 0x02: /* 32 bits */
123 case 0x03: /* 64 bits */
124 flags |= IORESOURCE_MEM;
125 break;
126 }
127 if (w & 0x40000000)
128 flags |= IORESOURCE_PREFETCH;
129 return flags;
130}
131
132static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna)
133{
134 u64 cp, s, da;
135 unsigned int af, rf;
136
137 af = of_bus_pci_get_flags(addr);
138 rf = of_bus_pci_get_flags(range);
139
140 /* Check address type match */
141 if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
142 return OF_BAD_ADDR;
143
144 /* Read address values, skipping high cell */
145 cp = of_read_number(range + 1, na - 1);
146 s = of_read_number(range + na + pna, ns);
147 da = of_read_number(addr + 1, na - 1);
148
149 pr_debug("OF: PCI map, cp=%llx, s=%llx, da=%llx\n",
150 (unsigned long long)cp, (unsigned long long)s,
151 (unsigned long long)da);
152
153 if (da < cp || da >= (cp + s))
154 return OF_BAD_ADDR;
155 return da - cp;
156}
157
158static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
159{
160 return of_bus_default_translate(addr + 1, offset, na - 1);
161}
162
163const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
164 unsigned int *flags)
165{
166 const u32 *prop;
167 unsigned int psize;
168 struct device_node *parent;
169 struct of_bus *bus;
170 int onesize, i, na, ns;
171
172 /* Get parent & match bus type */
173 parent = of_get_parent(dev);
174 if (parent == NULL)
175 return NULL;
176 bus = of_match_bus(parent);
177 if (strcmp(bus->name, "pci")) {
178 of_node_put(parent);
179 return NULL;
180 }
181 bus->count_cells(dev, &na, &ns);
182 of_node_put(parent);
183 if (!OF_CHECK_COUNTS(na, ns))
184 return NULL;
185
186 /* Get "reg" or "assigned-addresses" property */
187 prop = of_get_property(dev, bus->addresses, &psize);
188 if (prop == NULL)
189 return NULL;
190 psize /= 4;
191
192 onesize = na + ns;
193 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
194 u32 val = be32_to_cpu(prop[0]);
195 if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
196 if (size)
197 *size = of_read_number(prop + na, ns);
198 if (flags)
199 *flags = bus->get_flags(prop);
200 return prop;
201 }
202 }
203 return NULL;
204}
205EXPORT_SYMBOL(of_get_pci_address);
206
207int of_pci_address_to_resource(struct device_node *dev, int bar,
208 struct resource *r)
209{
210 const u32 *addrp;
211 u64 size;
212 unsigned int flags;
213
214 addrp = of_get_pci_address(dev, bar, &size, &flags);
215 if (addrp == NULL)
216 return -EINVAL;
217 return __of_address_to_resource(dev, addrp, size, flags, r);
218}
219EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
220#endif /* CONFIG_PCI */
221
222/*
223 * ISA bus specific translator
224 */
225
226static int of_bus_isa_match(struct device_node *np)
227{
228 return !strcmp(np->name, "isa");
229}
230
231static void of_bus_isa_count_cells(struct device_node *child,
232 int *addrc, int *sizec)
233{
234 if (addrc)
235 *addrc = 2;
236 if (sizec)
237 *sizec = 1;
238}
239
240static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
241{
242 u64 cp, s, da;
243
244 /* Check address type match */
245 if ((addr[0] ^ range[0]) & 0x00000001)
246 return OF_BAD_ADDR;
247
248 /* Read address values, skipping high cell */
249 cp = of_read_number(range + 1, na - 1);
250 s = of_read_number(range + na + pna, ns);
251 da = of_read_number(addr + 1, na - 1);
252
253 pr_debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n",
254 (unsigned long long)cp, (unsigned long long)s,
255 (unsigned long long)da);
256
257 if (da < cp || da >= (cp + s))
258 return OF_BAD_ADDR;
259 return da - cp;
260}
261
262static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
263{
264 return of_bus_default_translate(addr + 1, offset, na - 1);
265}
266
267static unsigned int of_bus_isa_get_flags(const u32 *addr)
268{
269 unsigned int flags = 0;
270 u32 w = addr[0];
271
272 if (w & 1)
273 flags |= IORESOURCE_IO;
274 else
275 flags |= IORESOURCE_MEM;
276 return flags;
277}
278
279/*
280 * Array of bus specific translators
281 */
282
283static struct of_bus of_busses[] = {
284#ifdef CONFIG_PCI
285 /* PCI */
286 {
287 .name = "pci",
288 .addresses = "assigned-addresses",
289 .match = of_bus_pci_match,
290 .count_cells = of_bus_pci_count_cells,
291 .map = of_bus_pci_map,
292 .translate = of_bus_pci_translate,
293 .get_flags = of_bus_pci_get_flags,
294 },
295#endif /* CONFIG_PCI */
296 /* ISA */
297 {
298 .name = "isa",
299 .addresses = "reg",
300 .match = of_bus_isa_match,
301 .count_cells = of_bus_isa_count_cells,
302 .map = of_bus_isa_map,
303 .translate = of_bus_isa_translate,
304 .get_flags = of_bus_isa_get_flags,
305 },
306 /* Default */
307 {
308 .name = "default",
309 .addresses = "reg",
310 .match = NULL,
311 .count_cells = of_bus_default_count_cells,
312 .map = of_bus_default_map,
313 .translate = of_bus_default_translate,
314 .get_flags = of_bus_default_get_flags,
315 },
316};
317
318static struct of_bus *of_match_bus(struct device_node *np)
319{
320 int i;
321
322 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
323 if (!of_busses[i].match || of_busses[i].match(np))
324 return &of_busses[i];
325 BUG();
326 return NULL;
327}
328
329static int of_translate_one(struct device_node *parent, struct of_bus *bus,
330 struct of_bus *pbus, u32 *addr,
331 int na, int ns, int pna, const char *rprop)
332{
333 const u32 *ranges;
334 unsigned int rlen;
335 int rone;
336 u64 offset = OF_BAD_ADDR;
337
338 /* Normally, an absence of a "ranges" property means we are
339 * crossing a non-translatable boundary, and thus the addresses
340 * below the current not cannot be converted to CPU physical ones.
341 * Unfortunately, while this is very clear in the spec, it's not
342 * what Apple understood, and they do have things like /uni-n or
343 * /ht nodes with no "ranges" property and a lot of perfectly
344 * useable mapped devices below them. Thus we treat the absence of
345 * "ranges" as equivalent to an empty "ranges" property which means
346 * a 1:1 translation at that level. It's up to the caller not to try
347 * to translate addresses that aren't supposed to be translated in
348 * the first place. --BenH.
349 *
350 * As far as we know, this damage only exists on Apple machines, so
351 * This code is only enabled on powerpc. --gcl
352 */
353 ranges = of_get_property(parent, rprop, &rlen);
354#if !defined(CONFIG_PPC)
355 if (ranges == NULL) {
356 pr_err("OF: no ranges; cannot translate\n");
357 return 1;
358 }
359#endif /* !defined(CONFIG_PPC) */
360 if (ranges == NULL || rlen == 0) {
361 offset = of_read_number(addr, na);
362 memset(addr, 0, pna * 4);
363 pr_debug("OF: empty ranges; 1:1 translation\n");
364 goto finish;
365 }
366
367 pr_debug("OF: walking ranges...\n");
368
369 /* Now walk through the ranges */
370 rlen /= 4;
371 rone = na + pna + ns;
372 for (; rlen >= rone; rlen -= rone, ranges += rone) {
373 offset = bus->map(addr, ranges, na, ns, pna);
374 if (offset != OF_BAD_ADDR)
375 break;
376 }
377 if (offset == OF_BAD_ADDR) {
378 pr_debug("OF: not found !\n");
379 return 1;
380 }
381 memcpy(addr, ranges + na, 4 * pna);
382
383 finish:
384 of_dump_addr("OF: parent translation for:", addr, pna);
385 pr_debug("OF: with offset: %llx\n", (unsigned long long)offset);
386
387 /* Translate it into parent bus space */
388 return pbus->translate(addr, offset, pna);
389}
390
391/*
392 * Translate an address from the device-tree into a CPU physical address,
393 * this walks up the tree and applies the various bus mappings on the
394 * way.
395 *
396 * Note: We consider that crossing any level with #size-cells == 0 to mean
397 * that translation is impossible (that is we are not dealing with a value
398 * that can be mapped to a cpu physical address). This is not really specified
399 * that way, but this is traditionally the way IBM at least do things
400 */
401u64 __of_translate_address(struct device_node *dev, const u32 *in_addr,
402 const char *rprop)
403{
404 struct device_node *parent = NULL;
405 struct of_bus *bus, *pbus;
406 u32 addr[OF_MAX_ADDR_CELLS];
407 int na, ns, pna, pns;
408 u64 result = OF_BAD_ADDR;
409
410 pr_debug("OF: ** translation for device %s **\n", dev->full_name);
411
412 /* Increase refcount at current level */
413 of_node_get(dev);
414
415 /* Get parent & match bus type */
416 parent = of_get_parent(dev);
417 if (parent == NULL)
418 goto bail;
419 bus = of_match_bus(parent);
420
421 /* Cound address cells & copy address locally */
422 bus->count_cells(dev, &na, &ns);
423 if (!OF_CHECK_COUNTS(na, ns)) {
424 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
425 dev->full_name);
426 goto bail;
427 }
428 memcpy(addr, in_addr, na * 4);
429
430 pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
431 bus->name, na, ns, parent->full_name);
432 of_dump_addr("OF: translating address:", addr, na);
433
434 /* Translate */
435 for (;;) {
436 /* Switch to parent bus */
437 of_node_put(dev);
438 dev = parent;
439 parent = of_get_parent(dev);
440
441 /* If root, we have finished */
442 if (parent == NULL) {
443 pr_debug("OF: reached root node\n");
444 result = of_read_number(addr, na);
445 break;
446 }
447
448 /* Get new parent bus and counts */
449 pbus = of_match_bus(parent);
450 pbus->count_cells(dev, &pna, &pns);
451 if (!OF_CHECK_COUNTS(pna, pns)) {
452 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
453 dev->full_name);
454 break;
455 }
456
457 pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
458 pbus->name, pna, pns, parent->full_name);
459
460 /* Apply bus translation */
461 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
462 break;
463
464 /* Complete the move up one level */
465 na = pna;
466 ns = pns;
467 bus = pbus;
468
469 of_dump_addr("OF: one level translation:", addr, na);
470 }
471 bail:
472 of_node_put(parent);
473 of_node_put(dev);
474
475 return result;
476}
477
478u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
479{
480 return __of_translate_address(dev, in_addr, "ranges");
481}
482EXPORT_SYMBOL(of_translate_address);
483
484u64 of_translate_dma_address(struct device_node *dev, const u32 *in_addr)
485{
486 return __of_translate_address(dev, in_addr, "dma-ranges");
487}
488EXPORT_SYMBOL(of_translate_dma_address);
489
490const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
491 unsigned int *flags)
492{
493 const u32 *prop;
494 unsigned int psize;
495 struct device_node *parent;
496 struct of_bus *bus;
497 int onesize, i, na, ns;
498
499 /* Get parent & match bus type */
500 parent = of_get_parent(dev);
501 if (parent == NULL)
502 return NULL;
503 bus = of_match_bus(parent);
504 bus->count_cells(dev, &na, &ns);
505 of_node_put(parent);
506 if (!OF_CHECK_COUNTS(na, ns))
507 return NULL;
508
509 /* Get "reg" or "assigned-addresses" property */
510 prop = of_get_property(dev, bus->addresses, &psize);
511 if (prop == NULL)
512 return NULL;
513 psize /= 4;
514
515 onesize = na + ns;
516 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
517 if (i == index) {
518 if (size)
519 *size = of_read_number(prop + na, ns);
520 if (flags)
521 *flags = bus->get_flags(prop);
522 return prop;
523 }
524 return NULL;
525}
526EXPORT_SYMBOL(of_get_address);
527
528static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
529 u64 size, unsigned int flags,
530 struct resource *r)
531{
532 u64 taddr;
533
534 if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
535 return -EINVAL;
536 taddr = of_translate_address(dev, addrp);
537 if (taddr == OF_BAD_ADDR)
538 return -EINVAL;
539 memset(r, 0, sizeof(struct resource));
540 if (flags & IORESOURCE_IO) {
541 unsigned long port;
542 port = pci_address_to_pio(taddr);
543 if (port == (unsigned long)-1)
544 return -EINVAL;
545 r->start = port;
546 r->end = port + size - 1;
547 } else {
548 r->start = taddr;
549 r->end = taddr + size - 1;
550 }
551 r->flags = flags;
552 r->name = dev->full_name;
553 return 0;
554}
555
556/**
557 * of_address_to_resource - Translate device tree address and return as resource
558 *
559 * Note that if your address is a PIO address, the conversion will fail if
560 * the physical address can't be internally converted to an IO token with
561 * pci_address_to_pio(), that is because it's either called to early or it
562 * can't be matched to any host bridge IO space
563 */
564int of_address_to_resource(struct device_node *dev, int index,
565 struct resource *r)
566{
567 const u32 *addrp;
568 u64 size;
569 unsigned int flags;
570
571 addrp = of_get_address(dev, index, &size, &flags);
572 if (addrp == NULL)
573 return -EINVAL;
574 return __of_address_to_resource(dev, addrp, size, flags, r);
575}
576EXPORT_SYMBOL_GPL(of_address_to_resource);
577
578
579/**
580 * of_iomap - Maps the memory mapped IO for a given device_node
581 * @device: the device whose io range will be mapped
582 * @index: index of the io range
583 *
584 * Returns a pointer to the mapped memory
585 */
586void __iomem *of_iomap(struct device_node *np, int index)
587{
588 struct resource res;
589
590 if (of_address_to_resource(np, index, &res))
591 return NULL;
592
593 return ioremap(res.start, 1 + res.end - res.start);
594}
595EXPORT_SYMBOL(of_iomap);
diff --git a/drivers/of/base.c b/drivers/of/base.c
index b5ad9740d8b2..aa805250de76 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -545,74 +545,28 @@ struct device_node *of_find_matching_node(struct device_node *from,
545EXPORT_SYMBOL(of_find_matching_node); 545EXPORT_SYMBOL(of_find_matching_node);
546 546
547/** 547/**
548 * of_modalias_table: Table of explicit compatible ==> modalias mappings
549 *
550 * This table allows particulare compatible property values to be mapped
551 * to modalias strings. This is useful for busses which do not directly
552 * understand the OF device tree but are populated based on data contained
553 * within the device tree. SPI and I2C are the two current users of this
554 * table.
555 *
556 * In most cases, devices do not need to be listed in this table because
557 * the modalias value can be derived directly from the compatible table.
558 * However, if for any reason a value cannot be derived, then this table
559 * provides a method to override the implicit derivation.
560 *
561 * At the moment, a single table is used for all bus types because it is
562 * assumed that the data size is small and that the compatible values
563 * should already be distinct enough to differentiate between SPI, I2C
564 * and other devices.
565 */
566struct of_modalias_table {
567 char *of_device;
568 char *modalias;
569};
570static struct of_modalias_table of_modalias_table[] = {
571 { "fsl,mcu-mpc8349emitx", "mcu-mpc8349emitx" },
572 { "mmc-spi-slot", "mmc_spi" },
573};
574
575/**
576 * of_modalias_node - Lookup appropriate modalias for a device node 548 * of_modalias_node - Lookup appropriate modalias for a device node
577 * @node: pointer to a device tree node 549 * @node: pointer to a device tree node
578 * @modalias: Pointer to buffer that modalias value will be copied into 550 * @modalias: Pointer to buffer that modalias value will be copied into
579 * @len: Length of modalias value 551 * @len: Length of modalias value
580 * 552 *
581 * Based on the value of the compatible property, this routine will determine 553 * Based on the value of the compatible property, this routine will attempt
582 * an appropriate modalias value for a particular device tree node. Two 554 * to choose an appropriate modalias value for a particular device tree node.
583 * separate methods are attempted to derive a modalias value. 555 * It does this by stripping the manufacturer prefix (as delimited by a ',')
556 * from the first entry in the compatible list property.
584 * 557 *
585 * First method is to lookup the compatible value in of_modalias_table. 558 * This routine returns 0 on success, <0 on failure.
586 * Second is to strip off the manufacturer prefix from the first
587 * compatible entry and use the remainder as modalias
588 *
589 * This routine returns 0 on success
590 */ 559 */
591int of_modalias_node(struct device_node *node, char *modalias, int len) 560int of_modalias_node(struct device_node *node, char *modalias, int len)
592{ 561{
593 int i, cplen; 562 const char *compatible, *p;
594 const char *compatible; 563 int cplen;
595 const char *p;
596
597 /* 1. search for exception list entry */
598 for (i = 0; i < ARRAY_SIZE(of_modalias_table); i++) {
599 compatible = of_modalias_table[i].of_device;
600 if (!of_device_is_compatible(node, compatible))
601 continue;
602 strlcpy(modalias, of_modalias_table[i].modalias, len);
603 return 0;
604 }
605 564
606 compatible = of_get_property(node, "compatible", &cplen); 565 compatible = of_get_property(node, "compatible", &cplen);
607 if (!compatible) 566 if (!compatible || strlen(compatible) > cplen)
608 return -ENODEV; 567 return -ENODEV;
609
610 /* 2. take first compatible entry and strip manufacturer */
611 p = strchr(compatible, ','); 568 p = strchr(compatible, ',');
612 if (!p) 569 strlcpy(modalias, p ? p + 1 : compatible, len);
613 return -ENODEV;
614 p++;
615 strlcpy(modalias, p, len);
616 return 0; 570 return 0;
617} 571}
618EXPORT_SYMBOL_GPL(of_modalias_node); 572EXPORT_SYMBOL_GPL(of_modalias_node);
@@ -651,14 +605,14 @@ EXPORT_SYMBOL(of_find_node_by_phandle);
651struct device_node * 605struct device_node *
652of_parse_phandle(struct device_node *np, const char *phandle_name, int index) 606of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
653{ 607{
654 const phandle *phandle; 608 const __be32 *phandle;
655 int size; 609 int size;
656 610
657 phandle = of_get_property(np, phandle_name, &size); 611 phandle = of_get_property(np, phandle_name, &size);
658 if ((!phandle) || (size < sizeof(*phandle) * (index + 1))) 612 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
659 return NULL; 613 return NULL;
660 614
661 return of_find_node_by_phandle(phandle[index]); 615 return of_find_node_by_phandle(be32_to_cpup(phandle + index));
662} 616}
663EXPORT_SYMBOL(of_parse_phandle); 617EXPORT_SYMBOL(of_parse_phandle);
664 618
@@ -714,16 +668,16 @@ int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
714 668
715 while (list < list_end) { 669 while (list < list_end) {
716 const __be32 *cells; 670 const __be32 *cells;
717 const phandle *phandle; 671 phandle phandle;
718 672
719 phandle = list++; 673 phandle = be32_to_cpup(list++);
720 args = list; 674 args = list;
721 675
722 /* one cell hole in the list = <>; */ 676 /* one cell hole in the list = <>; */
723 if (!*phandle) 677 if (!phandle)
724 goto next; 678 goto next;
725 679
726 node = of_find_node_by_phandle(*phandle); 680 node = of_find_node_by_phandle(phandle);
727 if (!node) { 681 if (!node) {
728 pr_debug("%s: could not find phandle\n", 682 pr_debug("%s: could not find phandle\n",
729 np->full_name); 683 np->full_name);
diff --git a/drivers/of/device.c b/drivers/of/device.c
index 7d18f8e0b013..0d8a0644f540 100644
--- a/drivers/of/device.c
+++ b/drivers/of/device.c
@@ -20,13 +20,13 @@
20const struct of_device_id *of_match_device(const struct of_device_id *matches, 20const struct of_device_id *of_match_device(const struct of_device_id *matches,
21 const struct device *dev) 21 const struct device *dev)
22{ 22{
23 if (!dev->of_node) 23 if ((!matches) || (!dev->of_node))
24 return NULL; 24 return NULL;
25 return of_match_node(matches, dev->of_node); 25 return of_match_node(matches, dev->of_node);
26} 26}
27EXPORT_SYMBOL(of_match_device); 27EXPORT_SYMBOL(of_match_device);
28 28
29struct of_device *of_dev_get(struct of_device *dev) 29struct platform_device *of_dev_get(struct platform_device *dev)
30{ 30{
31 struct device *tmp; 31 struct device *tmp;
32 32
@@ -34,13 +34,13 @@ struct of_device *of_dev_get(struct of_device *dev)
34 return NULL; 34 return NULL;
35 tmp = get_device(&dev->dev); 35 tmp = get_device(&dev->dev);
36 if (tmp) 36 if (tmp)
37 return to_of_device(tmp); 37 return to_platform_device(tmp);
38 else 38 else
39 return NULL; 39 return NULL;
40} 40}
41EXPORT_SYMBOL(of_dev_get); 41EXPORT_SYMBOL(of_dev_get);
42 42
43void of_dev_put(struct of_device *dev) 43void of_dev_put(struct platform_device *dev)
44{ 44{
45 if (dev) 45 if (dev)
46 put_device(&dev->dev); 46 put_device(&dev->dev);
@@ -50,28 +50,25 @@ EXPORT_SYMBOL(of_dev_put);
50static ssize_t devspec_show(struct device *dev, 50static ssize_t devspec_show(struct device *dev,
51 struct device_attribute *attr, char *buf) 51 struct device_attribute *attr, char *buf)
52{ 52{
53 struct of_device *ofdev; 53 struct platform_device *ofdev;
54 54
55 ofdev = to_of_device(dev); 55 ofdev = to_platform_device(dev);
56 return sprintf(buf, "%s\n", ofdev->dev.of_node->full_name); 56 return sprintf(buf, "%s\n", ofdev->dev.of_node->full_name);
57} 57}
58 58
59static ssize_t name_show(struct device *dev, 59static ssize_t name_show(struct device *dev,
60 struct device_attribute *attr, char *buf) 60 struct device_attribute *attr, char *buf)
61{ 61{
62 struct of_device *ofdev; 62 struct platform_device *ofdev;
63 63
64 ofdev = to_of_device(dev); 64 ofdev = to_platform_device(dev);
65 return sprintf(buf, "%s\n", ofdev->dev.of_node->name); 65 return sprintf(buf, "%s\n", ofdev->dev.of_node->name);
66} 66}
67 67
68static ssize_t modalias_show(struct device *dev, 68static ssize_t modalias_show(struct device *dev,
69 struct device_attribute *attr, char *buf) 69 struct device_attribute *attr, char *buf)
70{ 70{
71 struct of_device *ofdev = to_of_device(dev); 71 ssize_t len = of_device_get_modalias(dev, buf, PAGE_SIZE - 2);
72 ssize_t len = 0;
73
74 len = of_device_get_modalias(ofdev, buf, PAGE_SIZE - 2);
75 buf[len] = '\n'; 72 buf[len] = '\n';
76 buf[len+1] = 0; 73 buf[len+1] = 0;
77 return len+1; 74 return len+1;
@@ -93,20 +90,25 @@ struct device_attribute of_platform_device_attrs[] = {
93 */ 90 */
94void of_release_dev(struct device *dev) 91void of_release_dev(struct device *dev)
95{ 92{
96 struct of_device *ofdev; 93 struct platform_device *ofdev;
97 94
98 ofdev = to_of_device(dev); 95 ofdev = to_platform_device(dev);
99 of_node_put(ofdev->dev.of_node); 96 of_node_put(ofdev->dev.of_node);
100 kfree(ofdev); 97 kfree(ofdev);
101} 98}
102EXPORT_SYMBOL(of_release_dev); 99EXPORT_SYMBOL(of_release_dev);
103 100
104int of_device_register(struct of_device *ofdev) 101int of_device_register(struct platform_device *ofdev)
105{ 102{
106 BUG_ON(ofdev->dev.of_node == NULL); 103 BUG_ON(ofdev->dev.of_node == NULL);
107 104
108 device_initialize(&ofdev->dev); 105 device_initialize(&ofdev->dev);
109 106
107 /* name and id have to be set so that the platform bus doesn't get
108 * confused on matching */
109 ofdev->name = dev_name(&ofdev->dev);
110 ofdev->id = -1;
111
110 /* device_add will assume that this device is on the same node as 112 /* device_add will assume that this device is on the same node as
111 * the parent. If there is no parent defined, set the node 113 * the parent. If there is no parent defined, set the node
112 * explicitly */ 114 * explicitly */
@@ -117,25 +119,24 @@ int of_device_register(struct of_device *ofdev)
117} 119}
118EXPORT_SYMBOL(of_device_register); 120EXPORT_SYMBOL(of_device_register);
119 121
120void of_device_unregister(struct of_device *ofdev) 122void of_device_unregister(struct platform_device *ofdev)
121{ 123{
122 device_unregister(&ofdev->dev); 124 device_unregister(&ofdev->dev);
123} 125}
124EXPORT_SYMBOL(of_device_unregister); 126EXPORT_SYMBOL(of_device_unregister);
125 127
126ssize_t of_device_get_modalias(struct of_device *ofdev, 128ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
127 char *str, ssize_t len)
128{ 129{
129 const char *compat; 130 const char *compat;
130 int cplen, i; 131 int cplen, i;
131 ssize_t tsize, csize, repend; 132 ssize_t tsize, csize, repend;
132 133
133 /* Name & Type */ 134 /* Name & Type */
134 csize = snprintf(str, len, "of:N%sT%s", ofdev->dev.of_node->name, 135 csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name,
135 ofdev->dev.of_node->type); 136 dev->of_node->type);
136 137
137 /* Get compatible property if any */ 138 /* Get compatible property if any */
138 compat = of_get_property(ofdev->dev.of_node, "compatible", &cplen); 139 compat = of_get_property(dev->of_node, "compatible", &cplen);
139 if (!compat) 140 if (!compat)
140 return csize; 141 return csize;
141 142
@@ -170,3 +171,51 @@ ssize_t of_device_get_modalias(struct of_device *ofdev,
170 171
171 return tsize; 172 return tsize;
172} 173}
174
175/**
176 * of_device_uevent - Display OF related uevent information
177 */
178int of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
179{
180 const char *compat;
181 int seen = 0, cplen, sl;
182
183 if ((!dev) || (!dev->of_node))
184 return -ENODEV;
185
186 if (add_uevent_var(env, "OF_NAME=%s", dev->of_node->name))
187 return -ENOMEM;
188
189 if (add_uevent_var(env, "OF_TYPE=%s", dev->of_node->type))
190 return -ENOMEM;
191
192 /* Since the compatible field can contain pretty much anything
193 * it's not really legal to split it out with commas. We split it
194 * up using a number of environment variables instead. */
195
196 compat = of_get_property(dev->of_node, "compatible", &cplen);
197 while (compat && *compat && cplen > 0) {
198 if (add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat))
199 return -ENOMEM;
200
201 sl = strlen(compat) + 1;
202 compat += sl;
203 cplen -= sl;
204 seen++;
205 }
206
207 if (add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen))
208 return -ENOMEM;
209
210 /* modalias is trickier, we add it in 2 steps */
211 if (add_uevent_var(env, "MODALIAS="))
212 return -ENOMEM;
213
214 sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
215 sizeof(env->buf) - env->buflen);
216 if (sl >= (sizeof(env->buf) - env->buflen))
217 return -ENOMEM;
218 env->buflen += sl;
219
220 return 0;
221}
diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index b6987bba8556..65da5aec7552 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -69,9 +69,9 @@ int __init of_scan_flat_dt(int (*it)(unsigned long node,
69 u32 sz = be32_to_cpup((__be32 *)p); 69 u32 sz = be32_to_cpup((__be32 *)p);
70 p += 8; 70 p += 8;
71 if (be32_to_cpu(initial_boot_params->version) < 0x10) 71 if (be32_to_cpu(initial_boot_params->version) < 0x10)
72 p = _ALIGN(p, sz >= 8 ? 8 : 4); 72 p = ALIGN(p, sz >= 8 ? 8 : 4);
73 p += sz; 73 p += sz;
74 p = _ALIGN(p, 4); 74 p = ALIGN(p, 4);
75 continue; 75 continue;
76 } 76 }
77 if (tag != OF_DT_BEGIN_NODE) { 77 if (tag != OF_DT_BEGIN_NODE) {
@@ -80,7 +80,7 @@ int __init of_scan_flat_dt(int (*it)(unsigned long node,
80 } 80 }
81 depth++; 81 depth++;
82 pathp = (char *)p; 82 pathp = (char *)p;
83 p = _ALIGN(p + strlen(pathp) + 1, 4); 83 p = ALIGN(p + strlen(pathp) + 1, 4);
84 if ((*pathp) == '/') { 84 if ((*pathp) == '/') {
85 char *lp, *np; 85 char *lp, *np;
86 for (lp = NULL, np = pathp; *np; np++) 86 for (lp = NULL, np = pathp; *np; np++)
@@ -109,7 +109,7 @@ unsigned long __init of_get_flat_dt_root(void)
109 p += 4; 109 p += 4;
110 BUG_ON(be32_to_cpup((__be32 *)p) != OF_DT_BEGIN_NODE); 110 BUG_ON(be32_to_cpup((__be32 *)p) != OF_DT_BEGIN_NODE);
111 p += 4; 111 p += 4;
112 return _ALIGN(p + strlen((char *)p) + 1, 4); 112 return ALIGN(p + strlen((char *)p) + 1, 4);
113} 113}
114 114
115/** 115/**
@@ -138,7 +138,7 @@ void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
138 noff = be32_to_cpup((__be32 *)(p + 4)); 138 noff = be32_to_cpup((__be32 *)(p + 4));
139 p += 8; 139 p += 8;
140 if (be32_to_cpu(initial_boot_params->version) < 0x10) 140 if (be32_to_cpu(initial_boot_params->version) < 0x10)
141 p = _ALIGN(p, sz >= 8 ? 8 : 4); 141 p = ALIGN(p, sz >= 8 ? 8 : 4);
142 142
143 nstr = find_flat_dt_string(noff); 143 nstr = find_flat_dt_string(noff);
144 if (nstr == NULL) { 144 if (nstr == NULL) {
@@ -151,7 +151,7 @@ void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
151 return (void *)p; 151 return (void *)p;
152 } 152 }
153 p += sz; 153 p += sz;
154 p = _ALIGN(p, 4); 154 p = ALIGN(p, 4);
155 } while (1); 155 } while (1);
156} 156}
157 157
@@ -169,7 +169,7 @@ int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
169 if (cp == NULL) 169 if (cp == NULL)
170 return 0; 170 return 0;
171 while (cplen > 0) { 171 while (cplen > 0) {
172 if (strncasecmp(cp, compat, strlen(compat)) == 0) 172 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
173 return 1; 173 return 1;
174 l = strlen(cp) + 1; 174 l = strlen(cp) + 1;
175 cp += l; 175 cp += l;
@@ -184,7 +184,7 @@ static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
184{ 184{
185 void *res; 185 void *res;
186 186
187 *mem = _ALIGN(*mem, align); 187 *mem = ALIGN(*mem, align);
188 res = (void *)*mem; 188 res = (void *)*mem;
189 *mem += size; 189 *mem += size;
190 190
@@ -220,7 +220,7 @@ unsigned long __init unflatten_dt_node(unsigned long mem,
220 *p += 4; 220 *p += 4;
221 pathp = (char *)*p; 221 pathp = (char *)*p;
222 l = allocl = strlen(pathp) + 1; 222 l = allocl = strlen(pathp) + 1;
223 *p = _ALIGN(*p + l, 4); 223 *p = ALIGN(*p + l, 4);
224 224
225 /* version 0x10 has a more compact unit name here instead of the full 225 /* version 0x10 has a more compact unit name here instead of the full
226 * path. we accumulate the full path size using "fpsize", we'll rebuild 226 * path. we accumulate the full path size using "fpsize", we'll rebuild
@@ -299,7 +299,7 @@ unsigned long __init unflatten_dt_node(unsigned long mem,
299 noff = be32_to_cpup((__be32 *)((*p) + 4)); 299 noff = be32_to_cpup((__be32 *)((*p) + 4));
300 *p += 8; 300 *p += 8;
301 if (be32_to_cpu(initial_boot_params->version) < 0x10) 301 if (be32_to_cpu(initial_boot_params->version) < 0x10)
302 *p = _ALIGN(*p, sz >= 8 ? 8 : 4); 302 *p = ALIGN(*p, sz >= 8 ? 8 : 4);
303 303
304 pname = find_flat_dt_string(noff); 304 pname = find_flat_dt_string(noff);
305 if (pname == NULL) { 305 if (pname == NULL) {
@@ -320,20 +320,20 @@ unsigned long __init unflatten_dt_node(unsigned long mem,
320 if ((strcmp(pname, "phandle") == 0) || 320 if ((strcmp(pname, "phandle") == 0) ||
321 (strcmp(pname, "linux,phandle") == 0)) { 321 (strcmp(pname, "linux,phandle") == 0)) {
322 if (np->phandle == 0) 322 if (np->phandle == 0)
323 np->phandle = *((u32 *)*p); 323 np->phandle = be32_to_cpup((__be32*)*p);
324 } 324 }
325 /* And we process the "ibm,phandle" property 325 /* And we process the "ibm,phandle" property
326 * used in pSeries dynamic device tree 326 * used in pSeries dynamic device tree
327 * stuff */ 327 * stuff */
328 if (strcmp(pname, "ibm,phandle") == 0) 328 if (strcmp(pname, "ibm,phandle") == 0)
329 np->phandle = *((u32 *)*p); 329 np->phandle = be32_to_cpup((__be32 *)*p);
330 pp->name = pname; 330 pp->name = pname;
331 pp->length = sz; 331 pp->length = sz;
332 pp->value = (void *)*p; 332 pp->value = (void *)*p;
333 *prev_pp = pp; 333 *prev_pp = pp;
334 prev_pp = &pp->next; 334 prev_pp = &pp->next;
335 } 335 }
336 *p = _ALIGN((*p) + sz, 4); 336 *p = ALIGN((*p) + sz, 4);
337 } 337 }
338 /* with version 0x10 we may not have the name property, recreate 338 /* with version 0x10 we may not have the name property, recreate
339 * it here from the unit name if absent 339 * it here from the unit name if absent
diff --git a/drivers/of/gpio.c b/drivers/of/gpio.c
index a1b31a4abae4..905960338fb2 100644
--- a/drivers/of/gpio.c
+++ b/drivers/of/gpio.c
@@ -11,13 +11,14 @@
11 * (at your option) any later version. 11 * (at your option) any later version.
12 */ 12 */
13 13
14#include <linux/kernel.h> 14#include <linux/device.h>
15#include <linux/errno.h> 15#include <linux/errno.h>
16#include <linux/module.h>
16#include <linux/io.h> 17#include <linux/io.h>
17#include <linux/of.h> 18#include <linux/of.h>
18#include <linux/slab.h> 19#include <linux/of_address.h>
19#include <linux/of_gpio.h> 20#include <linux/of_gpio.h>
20#include <asm/prom.h> 21#include <linux/slab.h>
21 22
22/** 23/**
23 * of_get_gpio_flags - Get a GPIO number and flags to use with GPIO API 24 * of_get_gpio_flags - Get a GPIO number and flags to use with GPIO API
@@ -33,32 +34,32 @@ int of_get_gpio_flags(struct device_node *np, int index,
33 enum of_gpio_flags *flags) 34 enum of_gpio_flags *flags)
34{ 35{
35 int ret; 36 int ret;
36 struct device_node *gc; 37 struct device_node *gpio_np;
37 struct of_gpio_chip *of_gc = NULL; 38 struct gpio_chip *gc;
38 int size; 39 int size;
39 const void *gpio_spec; 40 const void *gpio_spec;
40 const __be32 *gpio_cells; 41 const __be32 *gpio_cells;
41 42
42 ret = of_parse_phandles_with_args(np, "gpios", "#gpio-cells", index, 43 ret = of_parse_phandles_with_args(np, "gpios", "#gpio-cells", index,
43 &gc, &gpio_spec); 44 &gpio_np, &gpio_spec);
44 if (ret) { 45 if (ret) {
45 pr_debug("%s: can't parse gpios property\n", __func__); 46 pr_debug("%s: can't parse gpios property\n", __func__);
46 goto err0; 47 goto err0;
47 } 48 }
48 49
49 of_gc = gc->data; 50 gc = of_node_to_gpiochip(gpio_np);
50 if (!of_gc) { 51 if (!gc) {
51 pr_debug("%s: gpio controller %s isn't registered\n", 52 pr_debug("%s: gpio controller %s isn't registered\n",
52 np->full_name, gc->full_name); 53 np->full_name, gpio_np->full_name);
53 ret = -ENODEV; 54 ret = -ENODEV;
54 goto err1; 55 goto err1;
55 } 56 }
56 57
57 gpio_cells = of_get_property(gc, "#gpio-cells", &size); 58 gpio_cells = of_get_property(gpio_np, "#gpio-cells", &size);
58 if (!gpio_cells || size != sizeof(*gpio_cells) || 59 if (!gpio_cells || size != sizeof(*gpio_cells) ||
59 be32_to_cpup(gpio_cells) != of_gc->gpio_cells) { 60 be32_to_cpup(gpio_cells) != gc->of_gpio_n_cells) {
60 pr_debug("%s: wrong #gpio-cells for %s\n", 61 pr_debug("%s: wrong #gpio-cells for %s\n",
61 np->full_name, gc->full_name); 62 np->full_name, gpio_np->full_name);
62 ret = -EINVAL; 63 ret = -EINVAL;
63 goto err1; 64 goto err1;
64 } 65 }
@@ -67,13 +68,13 @@ int of_get_gpio_flags(struct device_node *np, int index,
67 if (flags) 68 if (flags)
68 *flags = 0; 69 *flags = 0;
69 70
70 ret = of_gc->xlate(of_gc, np, gpio_spec, flags); 71 ret = gc->of_xlate(gc, np, gpio_spec, flags);
71 if (ret < 0) 72 if (ret < 0)
72 goto err1; 73 goto err1;
73 74
74 ret += of_gc->gc.base; 75 ret += gc->base;
75err1: 76err1:
76 of_node_put(gc); 77 of_node_put(gpio_np);
77err0: 78err0:
78 pr_debug("%s exited with status %d\n", __func__, ret); 79 pr_debug("%s exited with status %d\n", __func__, ret);
79 return ret; 80 return ret;
@@ -116,7 +117,7 @@ EXPORT_SYMBOL(of_gpio_count);
116 117
117/** 118/**
118 * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags 119 * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
119 * @of_gc: pointer to the of_gpio_chip structure 120 * @gc: pointer to the gpio_chip structure
120 * @np: device node of the GPIO chip 121 * @np: device node of the GPIO chip
121 * @gpio_spec: gpio specifier as found in the device tree 122 * @gpio_spec: gpio specifier as found in the device tree
122 * @flags: a flags pointer to fill in 123 * @flags: a flags pointer to fill in
@@ -125,8 +126,8 @@ EXPORT_SYMBOL(of_gpio_count);
125 * gpio chips. This function performs only one sanity check: whether gpio 126 * gpio chips. This function performs only one sanity check: whether gpio
126 * is less than ngpios (that is specified in the gpio_chip). 127 * is less than ngpios (that is specified in the gpio_chip).
127 */ 128 */
128int of_gpio_simple_xlate(struct of_gpio_chip *of_gc, struct device_node *np, 129static int of_gpio_simple_xlate(struct gpio_chip *gc, struct device_node *np,
129 const void *gpio_spec, enum of_gpio_flags *flags) 130 const void *gpio_spec, u32 *flags)
130{ 131{
131 const __be32 *gpio = gpio_spec; 132 const __be32 *gpio = gpio_spec;
132 const u32 n = be32_to_cpup(gpio); 133 const u32 n = be32_to_cpup(gpio);
@@ -137,12 +138,12 @@ int of_gpio_simple_xlate(struct of_gpio_chip *of_gc, struct device_node *np,
137 * number and the flags from a single gpio cell -- this is possible, 138 * number and the flags from a single gpio cell -- this is possible,
138 * but not recommended). 139 * but not recommended).
139 */ 140 */
140 if (of_gc->gpio_cells < 2) { 141 if (gc->of_gpio_n_cells < 2) {
141 WARN_ON(1); 142 WARN_ON(1);
142 return -EINVAL; 143 return -EINVAL;
143 } 144 }
144 145
145 if (n > of_gc->gc.ngpio) 146 if (n > gc->ngpio)
146 return -EINVAL; 147 return -EINVAL;
147 148
148 if (flags) 149 if (flags)
@@ -150,7 +151,6 @@ int of_gpio_simple_xlate(struct of_gpio_chip *of_gc, struct device_node *np,
150 151
151 return n; 152 return n;
152} 153}
153EXPORT_SYMBOL(of_gpio_simple_xlate);
154 154
155/** 155/**
156 * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank) 156 * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
@@ -161,10 +161,8 @@ EXPORT_SYMBOL(of_gpio_simple_xlate);
161 * 161 *
162 * 1) In the gpio_chip structure: 162 * 1) In the gpio_chip structure:
163 * - all the callbacks 163 * - all the callbacks
164 * 164 * - of_gpio_n_cells
165 * 2) In the of_gpio_chip structure: 165 * - of_xlate callback (optional)
166 * - gpio_cells
167 * - xlate callback (optional)
168 * 166 *
169 * 3) In the of_mm_gpio_chip structure: 167 * 3) In the of_mm_gpio_chip structure:
170 * - save_regs callback (optional) 168 * - save_regs callback (optional)
@@ -177,8 +175,7 @@ int of_mm_gpiochip_add(struct device_node *np,
177 struct of_mm_gpio_chip *mm_gc) 175 struct of_mm_gpio_chip *mm_gc)
178{ 176{
179 int ret = -ENOMEM; 177 int ret = -ENOMEM;
180 struct of_gpio_chip *of_gc = &mm_gc->of_gc; 178 struct gpio_chip *gc = &mm_gc->gc;
181 struct gpio_chip *gc = &of_gc->gc;
182 179
183 gc->label = kstrdup(np->full_name, GFP_KERNEL); 180 gc->label = kstrdup(np->full_name, GFP_KERNEL);
184 if (!gc->label) 181 if (!gc->label)
@@ -190,26 +187,19 @@ int of_mm_gpiochip_add(struct device_node *np,
190 187
191 gc->base = -1; 188 gc->base = -1;
192 189
193 if (!of_gc->xlate)
194 of_gc->xlate = of_gpio_simple_xlate;
195
196 if (mm_gc->save_regs) 190 if (mm_gc->save_regs)
197 mm_gc->save_regs(mm_gc); 191 mm_gc->save_regs(mm_gc);
198 192
199 np->data = of_gc; 193 mm_gc->gc.of_node = np;
200 194
201 ret = gpiochip_add(gc); 195 ret = gpiochip_add(gc);
202 if (ret) 196 if (ret)
203 goto err2; 197 goto err2;
204 198
205 /* We don't want to lose the node and its ->data */
206 of_node_get(np);
207
208 pr_debug("%s: registered as generic GPIO chip, base is %d\n", 199 pr_debug("%s: registered as generic GPIO chip, base is %d\n",
209 np->full_name, gc->base); 200 np->full_name, gc->base);
210 return 0; 201 return 0;
211err2: 202err2:
212 np->data = NULL;
213 iounmap(mm_gc->regs); 203 iounmap(mm_gc->regs);
214err1: 204err1:
215 kfree(gc->label); 205 kfree(gc->label);
@@ -219,3 +209,36 @@ err0:
219 return ret; 209 return ret;
220} 210}
221EXPORT_SYMBOL(of_mm_gpiochip_add); 211EXPORT_SYMBOL(of_mm_gpiochip_add);
212
213void of_gpiochip_add(struct gpio_chip *chip)
214{
215 if ((!chip->of_node) && (chip->dev))
216 chip->of_node = chip->dev->of_node;
217
218 if (!chip->of_node)
219 return;
220
221 if (!chip->of_xlate) {
222 chip->of_gpio_n_cells = 2;
223 chip->of_xlate = of_gpio_simple_xlate;
224 }
225
226 of_node_get(chip->of_node);
227}
228
229void of_gpiochip_remove(struct gpio_chip *chip)
230{
231 if (chip->of_node)
232 of_node_put(chip->of_node);
233}
234
235/* Private function for resolving node pointer to gpio_chip */
236static int of_gpiochip_is_match(struct gpio_chip *chip, void *data)
237{
238 return chip->of_node == data;
239}
240
241struct gpio_chip *of_node_to_gpiochip(struct device_node *np)
242{
243 return gpiochip_find(np, of_gpiochip_is_match);
244}
diff --git a/drivers/of/irq.c b/drivers/of/irq.c
new file mode 100644
index 000000000000..6e595e5a3977
--- /dev/null
+++ b/drivers/of/irq.c
@@ -0,0 +1,349 @@
1/*
2 * Derived from arch/i386/kernel/irq.c
3 * Copyright (C) 1992 Linus Torvalds
4 * Adapted from arch/i386 by Gary Thomas
5 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6 * Updated and modified by Cort Dougan <cort@fsmlabs.com>
7 * Copyright (C) 1996-2001 Cort Dougan
8 * Adapted for Power Macintosh by Paul Mackerras
9 * Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 * This file contains the code used to make IRQ descriptions in the
17 * device tree to actual irq numbers on an interrupt controller
18 * driver.
19 */
20
21#include <linux/errno.h>
22#include <linux/module.h>
23#include <linux/of.h>
24#include <linux/of_irq.h>
25#include <linux/string.h>
26
27/**
28 * irq_of_parse_and_map - Parse and map an interrupt into linux virq space
29 * @device: Device node of the device whose interrupt is to be mapped
30 * @index: Index of the interrupt to map
31 *
32 * This function is a wrapper that chains of_irq_map_one() and
33 * irq_create_of_mapping() to make things easier to callers
34 */
35unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
36{
37 struct of_irq oirq;
38
39 if (of_irq_map_one(dev, index, &oirq))
40 return NO_IRQ;
41
42 return irq_create_of_mapping(oirq.controller, oirq.specifier,
43 oirq.size);
44}
45EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
46
47/**
48 * of_irq_find_parent - Given a device node, find its interrupt parent node
49 * @child: pointer to device node
50 *
51 * Returns a pointer to the interrupt parent node, or NULL if the interrupt
52 * parent could not be determined.
53 */
54static struct device_node *of_irq_find_parent(struct device_node *child)
55{
56 struct device_node *p;
57 const __be32 *parp;
58
59 if (!of_node_get(child))
60 return NULL;
61
62 do {
63 parp = of_get_property(child, "interrupt-parent", NULL);
64 if (parp == NULL)
65 p = of_get_parent(child);
66 else {
67 if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
68 p = of_node_get(of_irq_dflt_pic);
69 else
70 p = of_find_node_by_phandle(be32_to_cpup(parp));
71 }
72 of_node_put(child);
73 child = p;
74 } while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
75
76 return p;
77}
78
79/**
80 * of_irq_map_raw - Low level interrupt tree parsing
81 * @parent: the device interrupt parent
82 * @intspec: interrupt specifier ("interrupts" property of the device)
83 * @ointsize: size of the passed in interrupt specifier
84 * @addr: address specifier (start of "reg" property of the device)
85 * @out_irq: structure of_irq filled by this function
86 *
87 * Returns 0 on success and a negative number on error
88 *
89 * This function is a low-level interrupt tree walking function. It
90 * can be used to do a partial walk with synthetized reg and interrupts
91 * properties, for example when resolving PCI interrupts when no device
92 * node exist for the parent.
93 */
94int of_irq_map_raw(struct device_node *parent, const __be32 *intspec,
95 u32 ointsize, const __be32 *addr, struct of_irq *out_irq)
96{
97 struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
98 const __be32 *tmp, *imap, *imask;
99 u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
100 int imaplen, match, i;
101
102 pr_debug("of_irq_map_raw: par=%s,intspec=[0x%08x 0x%08x...],ointsize=%d\n",
103 parent->full_name, be32_to_cpup(intspec),
104 be32_to_cpup(intspec + 1), ointsize);
105
106 ipar = of_node_get(parent);
107
108 /* First get the #interrupt-cells property of the current cursor
109 * that tells us how to interpret the passed-in intspec. If there
110 * is none, we are nice and just walk up the tree
111 */
112 do {
113 tmp = of_get_property(ipar, "#interrupt-cells", NULL);
114 if (tmp != NULL) {
115 intsize = be32_to_cpu(*tmp);
116 break;
117 }
118 tnode = ipar;
119 ipar = of_irq_find_parent(ipar);
120 of_node_put(tnode);
121 } while (ipar);
122 if (ipar == NULL) {
123 pr_debug(" -> no parent found !\n");
124 goto fail;
125 }
126
127 pr_debug("of_irq_map_raw: ipar=%s, size=%d\n", ipar->full_name, intsize);
128
129 if (ointsize != intsize)
130 return -EINVAL;
131
132 /* Look for this #address-cells. We have to implement the old linux
133 * trick of looking for the parent here as some device-trees rely on it
134 */
135 old = of_node_get(ipar);
136 do {
137 tmp = of_get_property(old, "#address-cells", NULL);
138 tnode = of_get_parent(old);
139 of_node_put(old);
140 old = tnode;
141 } while (old && tmp == NULL);
142 of_node_put(old);
143 old = NULL;
144 addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp);
145
146 pr_debug(" -> addrsize=%d\n", addrsize);
147
148 /* Now start the actual "proper" walk of the interrupt tree */
149 while (ipar != NULL) {
150 /* Now check if cursor is an interrupt-controller and if it is
151 * then we are done
152 */
153 if (of_get_property(ipar, "interrupt-controller", NULL) !=
154 NULL) {
155 pr_debug(" -> got it !\n");
156 for (i = 0; i < intsize; i++)
157 out_irq->specifier[i] =
158 of_read_number(intspec +i, 1);
159 out_irq->size = intsize;
160 out_irq->controller = ipar;
161 of_node_put(old);
162 return 0;
163 }
164
165 /* Now look for an interrupt-map */
166 imap = of_get_property(ipar, "interrupt-map", &imaplen);
167 /* No interrupt map, check for an interrupt parent */
168 if (imap == NULL) {
169 pr_debug(" -> no map, getting parent\n");
170 newpar = of_irq_find_parent(ipar);
171 goto skiplevel;
172 }
173 imaplen /= sizeof(u32);
174
175 /* Look for a mask */
176 imask = of_get_property(ipar, "interrupt-map-mask", NULL);
177
178 /* If we were passed no "reg" property and we attempt to parse
179 * an interrupt-map, then #address-cells must be 0.
180 * Fail if it's not.
181 */
182 if (addr == NULL && addrsize != 0) {
183 pr_debug(" -> no reg passed in when needed !\n");
184 goto fail;
185 }
186
187 /* Parse interrupt-map */
188 match = 0;
189 while (imaplen > (addrsize + intsize + 1) && !match) {
190 /* Compare specifiers */
191 match = 1;
192 for (i = 0; i < addrsize && match; ++i) {
193 u32 mask = imask ? imask[i] : 0xffffffffu;
194 match = ((addr[i] ^ imap[i]) & mask) == 0;
195 }
196 for (; i < (addrsize + intsize) && match; ++i) {
197 u32 mask = imask ? imask[i] : 0xffffffffu;
198 match =
199 ((intspec[i-addrsize] ^ imap[i]) & mask) == 0;
200 }
201 imap += addrsize + intsize;
202 imaplen -= addrsize + intsize;
203
204 pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen);
205
206 /* Get the interrupt parent */
207 if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
208 newpar = of_node_get(of_irq_dflt_pic);
209 else
210 newpar = of_find_node_by_phandle(be32_to_cpup(imap));
211 imap++;
212 --imaplen;
213
214 /* Check if not found */
215 if (newpar == NULL) {
216 pr_debug(" -> imap parent not found !\n");
217 goto fail;
218 }
219
220 /* Get #interrupt-cells and #address-cells of new
221 * parent
222 */
223 tmp = of_get_property(newpar, "#interrupt-cells", NULL);
224 if (tmp == NULL) {
225 pr_debug(" -> parent lacks #interrupt-cells!\n");
226 goto fail;
227 }
228 newintsize = be32_to_cpu(*tmp);
229 tmp = of_get_property(newpar, "#address-cells", NULL);
230 newaddrsize = (tmp == NULL) ? 0 : be32_to_cpu(*tmp);
231
232 pr_debug(" -> newintsize=%d, newaddrsize=%d\n",
233 newintsize, newaddrsize);
234
235 /* Check for malformed properties */
236 if (imaplen < (newaddrsize + newintsize))
237 goto fail;
238
239 imap += newaddrsize + newintsize;
240 imaplen -= newaddrsize + newintsize;
241
242 pr_debug(" -> imaplen=%d\n", imaplen);
243 }
244 if (!match)
245 goto fail;
246
247 of_node_put(old);
248 old = of_node_get(newpar);
249 addrsize = newaddrsize;
250 intsize = newintsize;
251 intspec = imap - intsize;
252 addr = intspec - addrsize;
253
254 skiplevel:
255 /* Iterate again with new parent */
256 pr_debug(" -> new parent: %s\n", newpar ? newpar->full_name : "<>");
257 of_node_put(ipar);
258 ipar = newpar;
259 newpar = NULL;
260 }
261 fail:
262 of_node_put(ipar);
263 of_node_put(old);
264 of_node_put(newpar);
265
266 return -EINVAL;
267}
268EXPORT_SYMBOL_GPL(of_irq_map_raw);
269
270/**
271 * of_irq_map_one - Resolve an interrupt for a device
272 * @device: the device whose interrupt is to be resolved
273 * @index: index of the interrupt to resolve
274 * @out_irq: structure of_irq filled by this function
275 *
276 * This function resolves an interrupt, walking the tree, for a given
277 * device-tree node. It's the high level pendant to of_irq_map_raw().
278 */
279int of_irq_map_one(struct device_node *device, int index, struct of_irq *out_irq)
280{
281 struct device_node *p;
282 const __be32 *intspec, *tmp, *addr;
283 u32 intsize, intlen;
284 int res = -EINVAL;
285
286 pr_debug("of_irq_map_one: dev=%s, index=%d\n", device->full_name, index);
287
288 /* OldWorld mac stuff is "special", handle out of line */
289 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
290 return of_irq_map_oldworld(device, index, out_irq);
291
292 /* Get the interrupts property */
293 intspec = of_get_property(device, "interrupts", &intlen);
294 if (intspec == NULL)
295 return -EINVAL;
296 intlen /= sizeof(*intspec);
297
298 pr_debug(" intspec=%d intlen=%d\n", be32_to_cpup(intspec), intlen);
299
300 /* Get the reg property (if any) */
301 addr = of_get_property(device, "reg", NULL);
302
303 /* Look for the interrupt parent. */
304 p = of_irq_find_parent(device);
305 if (p == NULL)
306 return -EINVAL;
307
308 /* Get size of interrupt specifier */
309 tmp = of_get_property(p, "#interrupt-cells", NULL);
310 if (tmp == NULL)
311 goto out;
312 intsize = be32_to_cpu(*tmp);
313
314 pr_debug(" intsize=%d intlen=%d\n", intsize, intlen);
315
316 /* Check index */
317 if ((index + 1) * intsize > intlen)
318 goto out;
319
320 /* Get new specifier and map it */
321 res = of_irq_map_raw(p, intspec + index * intsize, intsize,
322 addr, out_irq);
323 out:
324 of_node_put(p);
325 return res;
326}
327EXPORT_SYMBOL_GPL(of_irq_map_one);
328
329/**
330 * of_irq_to_resource - Decode a node's IRQ and return it as a resource
331 * @dev: pointer to device tree node
332 * @index: zero-based index of the irq
333 * @r: pointer to resource structure to return result into.
334 */
335int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
336{
337 int irq = irq_of_parse_and_map(dev, index);
338
339 /* Only dereference the resource if both the
340 * resource and the irq are valid. */
341 if (r && irq != NO_IRQ) {
342 r->start = r->end = irq;
343 r->flags = IORESOURCE_IRQ;
344 r->name = dev->full_name;
345 }
346
347 return irq;
348}
349EXPORT_SYMBOL_GPL(of_irq_to_resource);
diff --git a/drivers/of/of_i2c.c b/drivers/of/of_i2c.c
index ab6522c8e4fe..0a694debd226 100644
--- a/drivers/of/of_i2c.c
+++ b/drivers/of/of_i2c.c
@@ -14,57 +14,65 @@
14#include <linux/i2c.h> 14#include <linux/i2c.h>
15#include <linux/of.h> 15#include <linux/of.h>
16#include <linux/of_i2c.h> 16#include <linux/of_i2c.h>
17#include <linux/of_irq.h>
17#include <linux/module.h> 18#include <linux/module.h>
18 19
19void of_register_i2c_devices(struct i2c_adapter *adap, 20void of_i2c_register_devices(struct i2c_adapter *adap)
20 struct device_node *adap_node)
21{ 21{
22 void *result; 22 void *result;
23 struct device_node *node; 23 struct device_node *node;
24 24
25 for_each_child_of_node(adap_node, node) { 25 /* Only register child devices if the adapter has a node pointer set */
26 if (!adap->dev.of_node)
27 return;
28
29 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
30
31 for_each_child_of_node(adap->dev.of_node, node) {
26 struct i2c_board_info info = {}; 32 struct i2c_board_info info = {};
27 struct dev_archdata dev_ad = {}; 33 struct dev_archdata dev_ad = {};
28 const __be32 *addr; 34 const __be32 *addr;
29 int len; 35 int len;
30 36
31 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) 37 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
38
39 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
40 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
41 node->full_name);
32 continue; 42 continue;
43 }
33 44
34 addr = of_get_property(node, "reg", &len); 45 addr = of_get_property(node, "reg", &len);
35 if (!addr || len < sizeof(int) || *addr > (1 << 10) - 1) { 46 if (!addr || (len < sizeof(int))) {
36 printk(KERN_ERR 47 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
37 "of-i2c: invalid i2c device entry\n"); 48 node->full_name);
38 continue; 49 continue;
39 } 50 }
40 51
41 info.irq = irq_of_parse_and_map(node, 0);
42
43 info.addr = be32_to_cpup(addr); 52 info.addr = be32_to_cpup(addr);
53 if (info.addr > (1 << 10) - 1) {
54 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
55 info.addr, node->full_name);
56 continue;
57 }
44 58
45 info.of_node = node; 59 info.irq = irq_of_parse_and_map(node, 0);
60 info.of_node = of_node_get(node);
46 info.archdata = &dev_ad; 61 info.archdata = &dev_ad;
47 62
48 request_module("%s", info.type); 63 request_module("%s", info.type);
49 64
50 result = i2c_new_device(adap, &info); 65 result = i2c_new_device(adap, &info);
51 if (result == NULL) { 66 if (result == NULL) {
52 printk(KERN_ERR 67 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
53 "of-i2c: Failed to load driver for %s\n", 68 node->full_name);
54 info.type); 69 of_node_put(node);
55 irq_dispose_mapping(info.irq); 70 irq_dispose_mapping(info.irq);
56 continue; 71 continue;
57 } 72 }
58
59 /*
60 * Get the node to not lose the dev_archdata->of_node.
61 * Currently there is no way to put it back, as well as no
62 * of_unregister_i2c_devices() call.
63 */
64 of_node_get(node);
65 } 73 }
66} 74}
67EXPORT_SYMBOL(of_register_i2c_devices); 75EXPORT_SYMBOL(of_i2c_register_devices);
68 76
69static int of_dev_node_match(struct device *dev, void *data) 77static int of_dev_node_match(struct device *dev, void *data)
70{ 78{
diff --git a/drivers/of/of_mdio.c b/drivers/of/of_mdio.c
index 42a6715f8e84..1fce00eb421b 100644
--- a/drivers/of/of_mdio.c
+++ b/drivers/of/of_mdio.c
@@ -15,6 +15,7 @@
15#include <linux/err.h> 15#include <linux/err.h>
16#include <linux/phy.h> 16#include <linux/phy.h>
17#include <linux/of.h> 17#include <linux/of.h>
18#include <linux/of_irq.h>
18#include <linux/of_mdio.h> 19#include <linux/of_mdio.h>
19#include <linux/module.h> 20#include <linux/module.h>
20 21
diff --git a/drivers/of/of_spi.c b/drivers/of/of_spi.c
index 5fed7e3c7da3..1dbce58a58b0 100644
--- a/drivers/of/of_spi.c
+++ b/drivers/of/of_spi.c
@@ -9,17 +9,17 @@
9#include <linux/of.h> 9#include <linux/of.h>
10#include <linux/device.h> 10#include <linux/device.h>
11#include <linux/spi/spi.h> 11#include <linux/spi/spi.h>
12#include <linux/of_irq.h>
12#include <linux/of_spi.h> 13#include <linux/of_spi.h>
13 14
14/** 15/**
15 * of_register_spi_devices - Register child devices onto the SPI bus 16 * of_register_spi_devices - Register child devices onto the SPI bus
16 * @master: Pointer to spi_master device 17 * @master: Pointer to spi_master device
17 * @np: parent node of SPI device nodes
18 * 18 *
19 * Registers an spi_device for each child node of 'np' which has a 'reg' 19 * Registers an spi_device for each child node of master node which has a 'reg'
20 * property. 20 * property.
21 */ 21 */
22void of_register_spi_devices(struct spi_master *master, struct device_node *np) 22void of_register_spi_devices(struct spi_master *master)
23{ 23{
24 struct spi_device *spi; 24 struct spi_device *spi;
25 struct device_node *nc; 25 struct device_node *nc;
@@ -27,7 +27,10 @@ void of_register_spi_devices(struct spi_master *master, struct device_node *np)
27 int rc; 27 int rc;
28 int len; 28 int len;
29 29
30 for_each_child_of_node(np, nc) { 30 if (!master->dev.of_node)
31 return;
32
33 for_each_child_of_node(master->dev.of_node, nc) {
31 /* Alloc an spi_device */ 34 /* Alloc an spi_device */
32 spi = spi_alloc_device(master); 35 spi = spi_alloc_device(master);
33 if (!spi) { 36 if (!spi) {
diff --git a/drivers/of/platform.c b/drivers/of/platform.c
index 7dacc1ebe91e..bb72223c22ae 100644
--- a/drivers/of/platform.c
+++ b/drivers/of/platform.c
@@ -14,8 +14,105 @@
14#include <linux/errno.h> 14#include <linux/errno.h>
15#include <linux/module.h> 15#include <linux/module.h>
16#include <linux/device.h> 16#include <linux/device.h>
17#include <linux/dma-mapping.h>
18#include <linux/slab.h>
19#include <linux/of_address.h>
17#include <linux/of_device.h> 20#include <linux/of_device.h>
21#include <linux/of_irq.h>
18#include <linux/of_platform.h> 22#include <linux/of_platform.h>
23#include <linux/platform_device.h>
24
25static int of_dev_node_match(struct device *dev, void *data)
26{
27 return dev->of_node == data;
28}
29
30/**
31 * of_find_device_by_node - Find the platform_device associated with a node
32 * @np: Pointer to device tree node
33 *
34 * Returns platform_device pointer, or NULL if not found
35 */
36struct platform_device *of_find_device_by_node(struct device_node *np)
37{
38 struct device *dev;
39
40 dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
41 return dev ? to_platform_device(dev) : NULL;
42}
43EXPORT_SYMBOL(of_find_device_by_node);
44
45static int platform_driver_probe_shim(struct platform_device *pdev)
46{
47 struct platform_driver *pdrv;
48 struct of_platform_driver *ofpdrv;
49 const struct of_device_id *match;
50
51 pdrv = container_of(pdev->dev.driver, struct platform_driver, driver);
52 ofpdrv = container_of(pdrv, struct of_platform_driver, platform_driver);
53
54 /* There is an unlikely chance that an of_platform driver might match
55 * on a non-OF platform device. If so, then of_match_device() will
56 * come up empty. Return -EINVAL in this case so other drivers get
57 * the chance to bind. */
58 match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev);
59 return match ? ofpdrv->probe(pdev, match) : -EINVAL;
60}
61
62static void platform_driver_shutdown_shim(struct platform_device *pdev)
63{
64 struct platform_driver *pdrv;
65 struct of_platform_driver *ofpdrv;
66
67 pdrv = container_of(pdev->dev.driver, struct platform_driver, driver);
68 ofpdrv = container_of(pdrv, struct of_platform_driver, platform_driver);
69 ofpdrv->shutdown(pdev);
70}
71
72/**
73 * of_register_platform_driver
74 */
75int of_register_platform_driver(struct of_platform_driver *drv)
76{
77 char *of_name;
78
79 /* setup of_platform_driver to platform_driver adaptors */
80 drv->platform_driver.driver = drv->driver;
81
82 /* Prefix the driver name with 'of:' to avoid namespace collisions
83 * and bogus matches. There are some drivers in the tree that
84 * register both an of_platform_driver and a platform_driver with
85 * the same name. This is a temporary measure until they are all
86 * cleaned up --gcl July 29, 2010 */
87 of_name = kmalloc(strlen(drv->driver.name) + 5, GFP_KERNEL);
88 if (!of_name)
89 return -ENOMEM;
90 sprintf(of_name, "of:%s", drv->driver.name);
91 drv->platform_driver.driver.name = of_name;
92
93 if (drv->probe)
94 drv->platform_driver.probe = platform_driver_probe_shim;
95 drv->platform_driver.remove = drv->remove;
96 if (drv->shutdown)
97 drv->platform_driver.shutdown = platform_driver_shutdown_shim;
98 drv->platform_driver.suspend = drv->suspend;
99 drv->platform_driver.resume = drv->resume;
100
101 return platform_driver_register(&drv->platform_driver);
102}
103EXPORT_SYMBOL(of_register_platform_driver);
104
105void of_unregister_platform_driver(struct of_platform_driver *drv)
106{
107 platform_driver_unregister(&drv->platform_driver);
108 kfree(drv->platform_driver.driver.name);
109 drv->platform_driver.driver.name = NULL;
110}
111EXPORT_SYMBOL(of_unregister_platform_driver);
112
113#if defined(CONFIG_PPC_DCR)
114#include <asm/dcr.h>
115#endif
19 116
20extern struct device_attribute of_platform_device_attrs[]; 117extern struct device_attribute of_platform_device_attrs[];
21 118
@@ -33,11 +130,11 @@ static int of_platform_device_probe(struct device *dev)
33{ 130{
34 int error = -ENODEV; 131 int error = -ENODEV;
35 struct of_platform_driver *drv; 132 struct of_platform_driver *drv;
36 struct of_device *of_dev; 133 struct platform_device *of_dev;
37 const struct of_device_id *match; 134 const struct of_device_id *match;
38 135
39 drv = to_of_platform_driver(dev->driver); 136 drv = to_of_platform_driver(dev->driver);
40 of_dev = to_of_device(dev); 137 of_dev = to_platform_device(dev);
41 138
42 if (!drv->probe) 139 if (!drv->probe)
43 return error; 140 return error;
@@ -55,7 +152,7 @@ static int of_platform_device_probe(struct device *dev)
55 152
56static int of_platform_device_remove(struct device *dev) 153static int of_platform_device_remove(struct device *dev)
57{ 154{
58 struct of_device *of_dev = to_of_device(dev); 155 struct platform_device *of_dev = to_platform_device(dev);
59 struct of_platform_driver *drv = to_of_platform_driver(dev->driver); 156 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
60 157
61 if (dev->driver && drv->remove) 158 if (dev->driver && drv->remove)
@@ -65,7 +162,7 @@ static int of_platform_device_remove(struct device *dev)
65 162
66static void of_platform_device_shutdown(struct device *dev) 163static void of_platform_device_shutdown(struct device *dev)
67{ 164{
68 struct of_device *of_dev = to_of_device(dev); 165 struct platform_device *of_dev = to_platform_device(dev);
69 struct of_platform_driver *drv = to_of_platform_driver(dev->driver); 166 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
70 167
71 if (dev->driver && drv->shutdown) 168 if (dev->driver && drv->shutdown)
@@ -76,7 +173,7 @@ static void of_platform_device_shutdown(struct device *dev)
76 173
77static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg) 174static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg)
78{ 175{
79 struct of_device *of_dev = to_of_device(dev); 176 struct platform_device *of_dev = to_platform_device(dev);
80 struct of_platform_driver *drv = to_of_platform_driver(dev->driver); 177 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
81 int ret = 0; 178 int ret = 0;
82 179
@@ -87,7 +184,7 @@ static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg)
87 184
88static int of_platform_legacy_resume(struct device *dev) 185static int of_platform_legacy_resume(struct device *dev)
89{ 186{
90 struct of_device *of_dev = to_of_device(dev); 187 struct platform_device *of_dev = to_platform_device(dev);
91 struct of_platform_driver *drv = to_of_platform_driver(dev->driver); 188 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
92 int ret = 0; 189 int ret = 0;
93 190
@@ -384,15 +481,286 @@ int of_bus_type_init(struct bus_type *bus, const char *name)
384 481
385int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus) 482int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
386{ 483{
387 drv->driver.bus = bus; 484 /*
485 * Temporary: of_platform_bus used to be distinct from the platform
486 * bus. It isn't anymore, and so drivers on the platform bus need
487 * to be registered in a special way.
488 *
489 * After all of_platform_bus_type drivers are converted to
490 * platform_drivers, this exception can be removed.
491 */
492 if (bus == &platform_bus_type)
493 return of_register_platform_driver(drv);
388 494
389 /* register with core */ 495 /* register with core */
496 drv->driver.bus = bus;
390 return driver_register(&drv->driver); 497 return driver_register(&drv->driver);
391} 498}
392EXPORT_SYMBOL(of_register_driver); 499EXPORT_SYMBOL(of_register_driver);
393 500
394void of_unregister_driver(struct of_platform_driver *drv) 501void of_unregister_driver(struct of_platform_driver *drv)
395{ 502{
396 driver_unregister(&drv->driver); 503 if (drv->driver.bus == &platform_bus_type)
504 of_unregister_platform_driver(drv);
505 else
506 driver_unregister(&drv->driver);
397} 507}
398EXPORT_SYMBOL(of_unregister_driver); 508EXPORT_SYMBOL(of_unregister_driver);
509
510#if !defined(CONFIG_SPARC)
511/*
512 * The following routines scan a subtree and registers a device for
513 * each applicable node.
514 *
515 * Note: sparc doesn't use these routines because it has a different
516 * mechanism for creating devices from device tree nodes.
517 */
518
519/**
520 * of_device_make_bus_id - Use the device node data to assign a unique name
521 * @dev: pointer to device structure that is linked to a device tree node
522 *
523 * This routine will first try using either the dcr-reg or the reg property
524 * value to derive a unique name. As a last resort it will use the node
525 * name followed by a unique number.
526 */
527void of_device_make_bus_id(struct device *dev)
528{
529 static atomic_t bus_no_reg_magic;
530 struct device_node *node = dev->of_node;
531 const u32 *reg;
532 u64 addr;
533 int magic;
534
535#ifdef CONFIG_PPC_DCR
536 /*
537 * If it's a DCR based device, use 'd' for native DCRs
538 * and 'D' for MMIO DCRs.
539 */
540 reg = of_get_property(node, "dcr-reg", NULL);
541 if (reg) {
542#ifdef CONFIG_PPC_DCR_NATIVE
543 dev_set_name(dev, "d%x.%s", *reg, node->name);
544#else /* CONFIG_PPC_DCR_NATIVE */
545 u64 addr = of_translate_dcr_address(node, *reg, NULL);
546 if (addr != OF_BAD_ADDR) {
547 dev_set_name(dev, "D%llx.%s",
548 (unsigned long long)addr, node->name);
549 return;
550 }
551#endif /* !CONFIG_PPC_DCR_NATIVE */
552 }
553#endif /* CONFIG_PPC_DCR */
554
555 /*
556 * For MMIO, get the physical address
557 */
558 reg = of_get_property(node, "reg", NULL);
559 if (reg) {
560 addr = of_translate_address(node, reg);
561 if (addr != OF_BAD_ADDR) {
562 dev_set_name(dev, "%llx.%s",
563 (unsigned long long)addr, node->name);
564 return;
565 }
566 }
567
568 /*
569 * No BusID, use the node name and add a globally incremented
570 * counter (and pray...)
571 */
572 magic = atomic_add_return(1, &bus_no_reg_magic);
573 dev_set_name(dev, "%s.%d", node->name, magic - 1);
574}
575
576/**
577 * of_device_alloc - Allocate and initialize an of_device
578 * @np: device node to assign to device
579 * @bus_id: Name to assign to the device. May be null to use default name.
580 * @parent: Parent device.
581 */
582struct platform_device *of_device_alloc(struct device_node *np,
583 const char *bus_id,
584 struct device *parent)
585{
586 struct platform_device *dev;
587 int rc, i, num_reg = 0, num_irq = 0;
588 struct resource *res, temp_res;
589
590 /* First count how many resources are needed */
591 while (of_address_to_resource(np, num_reg, &temp_res) == 0)
592 num_reg++;
593 while (of_irq_to_resource(np, num_irq, &temp_res) != NO_IRQ)
594 num_irq++;
595
596 /* Allocate memory for both the struct device and the resource table */
597 dev = kzalloc(sizeof(*dev) + (sizeof(*res) * (num_reg + num_irq)),
598 GFP_KERNEL);
599 if (!dev)
600 return NULL;
601 res = (struct resource *) &dev[1];
602
603 /* Populate the resource table */
604 if (num_irq || num_reg) {
605 dev->num_resources = num_reg + num_irq;
606 dev->resource = res;
607 for (i = 0; i < num_reg; i++, res++) {
608 rc = of_address_to_resource(np, i, res);
609 WARN_ON(rc);
610 }
611 for (i = 0; i < num_irq; i++, res++) {
612 rc = of_irq_to_resource(np, i, res);
613 WARN_ON(rc == NO_IRQ);
614 }
615 }
616
617 dev->dev.of_node = of_node_get(np);
618#if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
619 dev->dev.dma_mask = &dev->archdata.dma_mask;
620#endif
621 dev->dev.parent = parent;
622 dev->dev.release = of_release_dev;
623
624 if (bus_id)
625 dev_set_name(&dev->dev, "%s", bus_id);
626 else
627 of_device_make_bus_id(&dev->dev);
628
629 return dev;
630}
631EXPORT_SYMBOL(of_device_alloc);
632
633/**
634 * of_platform_device_create - Alloc, initialize and register an of_device
635 * @np: pointer to node to create device for
636 * @bus_id: name to assign device
637 * @parent: Linux device model parent device.
638 */
639struct platform_device *of_platform_device_create(struct device_node *np,
640 const char *bus_id,
641 struct device *parent)
642{
643 struct platform_device *dev;
644
645 dev = of_device_alloc(np, bus_id, parent);
646 if (!dev)
647 return NULL;
648
649#if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
650 dev->archdata.dma_mask = 0xffffffffUL;
651#endif
652 dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
653 dev->dev.bus = &platform_bus_type;
654
655 /* We do not fill the DMA ops for platform devices by default.
656 * This is currently the responsibility of the platform code
657 * to do such, possibly using a device notifier
658 */
659
660 if (of_device_register(dev) != 0) {
661 of_device_free(dev);
662 return NULL;
663 }
664
665 return dev;
666}
667EXPORT_SYMBOL(of_platform_device_create);
668
669/**
670 * of_platform_bus_create - Create an OF device for a bus node and all its
671 * children. Optionally recursively instantiate matching busses.
672 * @bus: device node of the bus to instantiate
673 * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
674 * disallow recursive creation of child busses
675 */
676static int of_platform_bus_create(const struct device_node *bus,
677 const struct of_device_id *matches,
678 struct device *parent)
679{
680 struct device_node *child;
681 struct platform_device *dev;
682 int rc = 0;
683
684 for_each_child_of_node(bus, child) {
685 pr_debug(" create child: %s\n", child->full_name);
686 dev = of_platform_device_create(child, NULL, parent);
687 if (dev == NULL)
688 rc = -ENOMEM;
689 else if (!of_match_node(matches, child))
690 continue;
691 if (rc == 0) {
692 pr_debug(" and sub busses\n");
693 rc = of_platform_bus_create(child, matches, &dev->dev);
694 }
695 if (rc) {
696 of_node_put(child);
697 break;
698 }
699 }
700 return rc;
701}
702
703/**
704 * of_platform_bus_probe - Probe the device-tree for platform busses
705 * @root: parent of the first level to probe or NULL for the root of the tree
706 * @matches: match table, NULL to use the default
707 * @parent: parent to hook devices from, NULL for toplevel
708 *
709 * Note that children of the provided root are not instantiated as devices
710 * unless the specified root itself matches the bus list and is not NULL.
711 */
712int of_platform_bus_probe(struct device_node *root,
713 const struct of_device_id *matches,
714 struct device *parent)
715{
716 struct device_node *child;
717 struct platform_device *dev;
718 int rc = 0;
719
720 if (WARN_ON(!matches || matches == OF_NO_DEEP_PROBE))
721 return -EINVAL;
722 if (root == NULL)
723 root = of_find_node_by_path("/");
724 else
725 of_node_get(root);
726 if (root == NULL)
727 return -EINVAL;
728
729 pr_debug("of_platform_bus_probe()\n");
730 pr_debug(" starting at: %s\n", root->full_name);
731
732 /* Do a self check of bus type, if there's a match, create
733 * children
734 */
735 if (of_match_node(matches, root)) {
736 pr_debug(" root match, create all sub devices\n");
737 dev = of_platform_device_create(root, NULL, parent);
738 if (dev == NULL) {
739 rc = -ENOMEM;
740 goto bail;
741 }
742 pr_debug(" create all sub busses\n");
743 rc = of_platform_bus_create(root, matches, &dev->dev);
744 goto bail;
745 }
746 for_each_child_of_node(root, child) {
747 if (!of_match_node(matches, child))
748 continue;
749
750 pr_debug(" match: %s\n", child->full_name);
751 dev = of_platform_device_create(child, NULL, parent);
752 if (dev == NULL)
753 rc = -ENOMEM;
754 else
755 rc = of_platform_bus_create(child, matches, &dev->dev);
756 if (rc) {
757 of_node_put(child);
758 break;
759 }
760 }
761 bail:
762 of_node_put(root);
763 return rc;
764}
765EXPORT_SYMBOL(of_platform_bus_probe);
766#endif /* !CONFIG_SPARC */