Merge remote-tracking branch 'lsk/v3.10/topic/tc2' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / of / address.c
1
2 #include <linux/device.h>
3 #include <linux/io.h>
4 #include <linux/ioport.h>
5 #include <linux/module.h>
6 #include <linux/of_address.h>
7 #include <linux/pci_regs.h>
8 #include <linux/string.h>
9
10 /* Max address size we deal with */
11 #define OF_MAX_ADDR_CELLS       4
12 #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
13 #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
14
15 static struct of_bus *of_match_bus(struct device_node *np);
16 static int __of_address_to_resource(struct device_node *dev,
17                 const __be32 *addrp, u64 size, unsigned int flags,
18                 const char *name, struct resource *r);
19
20 /* Debug utility */
21 #ifdef DEBUG
22 static void of_dump_addr(const char *s, const __be32 *addr, int na)
23 {
24         printk(KERN_DEBUG "%s", s);
25         while (na--)
26                 printk(" %08x", be32_to_cpu(*(addr++)));
27         printk("\n");
28 }
29 #else
30 static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
31 #endif
32
33 /* Callbacks for bus specific translators */
34 struct of_bus {
35         const char      *name;
36         const char      *addresses;
37         int             (*match)(struct device_node *parent);
38         void            (*count_cells)(struct device_node *child,
39                                        int *addrc, int *sizec);
40         u64             (*map)(__be32 *addr, const __be32 *range,
41                                 int na, int ns, int pna);
42         int             (*translate)(__be32 *addr, u64 offset, int na);
43         unsigned int    (*get_flags)(const __be32 *addr);
44 };
45
46 /*
47  * Default translator (generic bus)
48  */
49
50 static void of_bus_default_count_cells(struct device_node *dev,
51                                        int *addrc, int *sizec)
52 {
53         if (addrc)
54                 *addrc = of_n_addr_cells(dev);
55         if (sizec)
56                 *sizec = of_n_size_cells(dev);
57 }
58
59 static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
60                 int na, int ns, int pna)
61 {
62         u64 cp, s, da;
63
64         cp = of_read_number(range, na);
65         s  = of_read_number(range + na + pna, ns);
66         da = of_read_number(addr, na);
67
68         pr_debug("OF: default map, cp=%llx, s=%llx, da=%llx\n",
69                  (unsigned long long)cp, (unsigned long long)s,
70                  (unsigned long long)da);
71
72         if (da < cp || da >= (cp + s))
73                 return OF_BAD_ADDR;
74         return da - cp;
75 }
76
77 static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
78 {
79         u64 a = of_read_number(addr, na);
80         memset(addr, 0, na * 4);
81         a += offset;
82         if (na > 1)
83                 addr[na - 2] = cpu_to_be32(a >> 32);
84         addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
85
86         return 0;
87 }
88
89 static unsigned int of_bus_default_get_flags(const __be32 *addr)
90 {
91         return IORESOURCE_MEM;
92 }
93
94 #ifdef CONFIG_PCI
95 /*
96  * PCI bus specific translator
97  */
98
99 static int of_bus_pci_match(struct device_node *np)
100 {
101         /*
102          * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
103          * "ht" is hypertransport
104          */
105         return !strcmp(np->type, "pci") || !strcmp(np->type, "vci") ||
106                 !strcmp(np->type, "ht");
107 }
108
109 static void of_bus_pci_count_cells(struct device_node *np,
110                                    int *addrc, int *sizec)
111 {
112         if (addrc)
113                 *addrc = 3;
114         if (sizec)
115                 *sizec = 2;
116 }
117
118 static unsigned int of_bus_pci_get_flags(const __be32 *addr)
119 {
120         unsigned int flags = 0;
121         u32 w = be32_to_cpup(addr);
122
123         switch((w >> 24) & 0x03) {
124         case 0x01:
125                 flags |= IORESOURCE_IO;
126                 break;
127         case 0x02: /* 32 bits */
128         case 0x03: /* 64 bits */
129                 flags |= IORESOURCE_MEM;
130                 break;
131         }
132         if (w & 0x40000000)
133                 flags |= IORESOURCE_PREFETCH;
134         return flags;
135 }
136
137 static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
138                 int pna)
139 {
140         u64 cp, s, da;
141         unsigned int af, rf;
142
143         af = of_bus_pci_get_flags(addr);
144         rf = of_bus_pci_get_flags(range);
145
146         /* Check address type match */
147         if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
148                 return OF_BAD_ADDR;
149
150         /* Read address values, skipping high cell */
151         cp = of_read_number(range + 1, na - 1);
152         s  = of_read_number(range + na + pna, ns);
153         da = of_read_number(addr + 1, na - 1);
154
155         pr_debug("OF: PCI map, cp=%llx, s=%llx, da=%llx\n",
156                  (unsigned long long)cp, (unsigned long long)s,
157                  (unsigned long long)da);
158
159         if (da < cp || da >= (cp + s))
160                 return OF_BAD_ADDR;
161         return da - cp;
162 }
163
164 static int of_bus_pci_translate(__be32 *addr, u64 offset, int na)
165 {
166         return of_bus_default_translate(addr + 1, offset, na - 1);
167 }
168
169 const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
170                         unsigned int *flags)
171 {
172         const __be32 *prop;
173         unsigned int psize;
174         struct device_node *parent;
175         struct of_bus *bus;
176         int onesize, i, na, ns;
177
178         /* Get parent & match bus type */
179         parent = of_get_parent(dev);
180         if (parent == NULL)
181                 return NULL;
182         bus = of_match_bus(parent);
183         if (strcmp(bus->name, "pci")) {
184                 of_node_put(parent);
185                 return NULL;
186         }
187         bus->count_cells(dev, &na, &ns);
188         of_node_put(parent);
189         if (!OF_CHECK_ADDR_COUNT(na))
190                 return NULL;
191
192         /* Get "reg" or "assigned-addresses" property */
193         prop = of_get_property(dev, bus->addresses, &psize);
194         if (prop == NULL)
195                 return NULL;
196         psize /= 4;
197
198         onesize = na + ns;
199         for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
200                 u32 val = be32_to_cpu(prop[0]);
201                 if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
202                         if (size)
203                                 *size = of_read_number(prop + na, ns);
204                         if (flags)
205                                 *flags = bus->get_flags(prop);
206                         return prop;
207                 }
208         }
209         return NULL;
210 }
211 EXPORT_SYMBOL(of_get_pci_address);
212
213 int of_pci_address_to_resource(struct device_node *dev, int bar,
214                                struct resource *r)
215 {
216         const __be32    *addrp;
217         u64             size;
218         unsigned int    flags;
219
220         addrp = of_get_pci_address(dev, bar, &size, &flags);
221         if (addrp == NULL)
222                 return -EINVAL;
223         return __of_address_to_resource(dev, addrp, size, flags, NULL, r);
224 }
225 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
226 #endif /* CONFIG_PCI */
227
228 /*
229  * ISA bus specific translator
230  */
231
232 static int of_bus_isa_match(struct device_node *np)
233 {
234         return !strcmp(np->name, "isa");
235 }
236
237 static void of_bus_isa_count_cells(struct device_node *child,
238                                    int *addrc, int *sizec)
239 {
240         if (addrc)
241                 *addrc = 2;
242         if (sizec)
243                 *sizec = 1;
244 }
245
246 static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
247                 int pna)
248 {
249         u64 cp, s, da;
250
251         /* Check address type match */
252         if ((addr[0] ^ range[0]) & cpu_to_be32(1))
253                 return OF_BAD_ADDR;
254
255         /* Read address values, skipping high cell */
256         cp = of_read_number(range + 1, na - 1);
257         s  = of_read_number(range + na + pna, ns);
258         da = of_read_number(addr + 1, na - 1);
259
260         pr_debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n",
261                  (unsigned long long)cp, (unsigned long long)s,
262                  (unsigned long long)da);
263
264         if (da < cp || da >= (cp + s))
265                 return OF_BAD_ADDR;
266         return da - cp;
267 }
268
269 static int of_bus_isa_translate(__be32 *addr, u64 offset, int na)
270 {
271         return of_bus_default_translate(addr + 1, offset, na - 1);
272 }
273
274 static unsigned int of_bus_isa_get_flags(const __be32 *addr)
275 {
276         unsigned int flags = 0;
277         u32 w = be32_to_cpup(addr);
278
279         if (w & 1)
280                 flags |= IORESOURCE_IO;
281         else
282                 flags |= IORESOURCE_MEM;
283         return flags;
284 }
285
286 /*
287  * Array of bus specific translators
288  */
289
290 static struct of_bus of_busses[] = {
291 #ifdef CONFIG_PCI
292         /* PCI */
293         {
294                 .name = "pci",
295                 .addresses = "assigned-addresses",
296                 .match = of_bus_pci_match,
297                 .count_cells = of_bus_pci_count_cells,
298                 .map = of_bus_pci_map,
299                 .translate = of_bus_pci_translate,
300                 .get_flags = of_bus_pci_get_flags,
301         },
302 #endif /* CONFIG_PCI */
303         /* ISA */
304         {
305                 .name = "isa",
306                 .addresses = "reg",
307                 .match = of_bus_isa_match,
308                 .count_cells = of_bus_isa_count_cells,
309                 .map = of_bus_isa_map,
310                 .translate = of_bus_isa_translate,
311                 .get_flags = of_bus_isa_get_flags,
312         },
313         /* Default */
314         {
315                 .name = "default",
316                 .addresses = "reg",
317                 .match = NULL,
318                 .count_cells = of_bus_default_count_cells,
319                 .map = of_bus_default_map,
320                 .translate = of_bus_default_translate,
321                 .get_flags = of_bus_default_get_flags,
322         },
323 };
324
325 static struct of_bus *of_match_bus(struct device_node *np)
326 {
327         int i;
328
329         for (i = 0; i < ARRAY_SIZE(of_busses); i++)
330                 if (!of_busses[i].match || of_busses[i].match(np))
331                         return &of_busses[i];
332         BUG();
333         return NULL;
334 }
335
336 static int of_translate_one(struct device_node *parent, struct of_bus *bus,
337                             struct of_bus *pbus, __be32 *addr,
338                             int na, int ns, int pna, const char *rprop)
339 {
340         const __be32 *ranges;
341         unsigned int rlen;
342         int rone;
343         u64 offset = OF_BAD_ADDR;
344
345         /* Normally, an absence of a "ranges" property means we are
346          * crossing a non-translatable boundary, and thus the addresses
347          * below the current not cannot be converted to CPU physical ones.
348          * Unfortunately, while this is very clear in the spec, it's not
349          * what Apple understood, and they do have things like /uni-n or
350          * /ht nodes with no "ranges" property and a lot of perfectly
351          * useable mapped devices below them. Thus we treat the absence of
352          * "ranges" as equivalent to an empty "ranges" property which means
353          * a 1:1 translation at that level. It's up to the caller not to try
354          * to translate addresses that aren't supposed to be translated in
355          * the first place. --BenH.
356          *
357          * As far as we know, this damage only exists on Apple machines, so
358          * This code is only enabled on powerpc. --gcl
359          */
360         ranges = of_get_property(parent, rprop, &rlen);
361 #if !defined(CONFIG_PPC)
362         if (ranges == NULL) {
363                 pr_err("OF: no ranges; cannot translate\n");
364                 return 1;
365         }
366 #endif /* !defined(CONFIG_PPC) */
367         if (ranges == NULL || rlen == 0) {
368                 offset = of_read_number(addr, na);
369                 memset(addr, 0, pna * 4);
370                 pr_debug("OF: empty ranges; 1:1 translation\n");
371                 goto finish;
372         }
373
374         pr_debug("OF: walking ranges...\n");
375
376         /* Now walk through the ranges */
377         rlen /= 4;
378         rone = na + pna + ns;
379         for (; rlen >= rone; rlen -= rone, ranges += rone) {
380                 offset = bus->map(addr, ranges, na, ns, pna);
381                 if (offset != OF_BAD_ADDR)
382                         break;
383         }
384         if (offset == OF_BAD_ADDR) {
385                 pr_debug("OF: not found !\n");
386                 return 1;
387         }
388         memcpy(addr, ranges + na, 4 * pna);
389
390  finish:
391         of_dump_addr("OF: parent translation for:", addr, pna);
392         pr_debug("OF: with offset: %llx\n", (unsigned long long)offset);
393
394         /* Translate it into parent bus space */
395         return pbus->translate(addr, offset, pna);
396 }
397
398 /*
399  * Translate an address from the device-tree into a CPU physical address,
400  * this walks up the tree and applies the various bus mappings on the
401  * way.
402  *
403  * Note: We consider that crossing any level with #size-cells == 0 to mean
404  * that translation is impossible (that is we are not dealing with a value
405  * that can be mapped to a cpu physical address). This is not really specified
406  * that way, but this is traditionally the way IBM at least do things
407  */
408 static u64 __of_translate_address(struct device_node *dev,
409                                   const __be32 *in_addr, const char *rprop)
410 {
411         struct device_node *parent = NULL;
412         struct of_bus *bus, *pbus;
413         __be32 addr[OF_MAX_ADDR_CELLS];
414         int na, ns, pna, pns;
415         u64 result = OF_BAD_ADDR;
416
417         pr_debug("OF: ** translation for device %s **\n", dev->full_name);
418
419         /* Increase refcount at current level */
420         of_node_get(dev);
421
422         /* Get parent & match bus type */
423         parent = of_get_parent(dev);
424         if (parent == NULL)
425                 goto bail;
426         bus = of_match_bus(parent);
427
428         /* Count address cells & copy address locally */
429         bus->count_cells(dev, &na, &ns);
430         if (!OF_CHECK_COUNTS(na, ns)) {
431                 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
432                        dev->full_name);
433                 goto bail;
434         }
435         memcpy(addr, in_addr, na * 4);
436
437         pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
438             bus->name, na, ns, parent->full_name);
439         of_dump_addr("OF: translating address:", addr, na);
440
441         /* Translate */
442         for (;;) {
443                 /* Switch to parent bus */
444                 of_node_put(dev);
445                 dev = parent;
446                 parent = of_get_parent(dev);
447
448                 /* If root, we have finished */
449                 if (parent == NULL) {
450                         pr_debug("OF: reached root node\n");
451                         result = of_read_number(addr, na);
452                         break;
453                 }
454
455                 /* Get new parent bus and counts */
456                 pbus = of_match_bus(parent);
457                 pbus->count_cells(dev, &pna, &pns);
458                 if (!OF_CHECK_COUNTS(pna, pns)) {
459                         printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
460                                dev->full_name);
461                         break;
462                 }
463
464                 pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
465                     pbus->name, pna, pns, parent->full_name);
466
467                 /* Apply bus translation */
468                 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
469                         break;
470
471                 /* Complete the move up one level */
472                 na = pna;
473                 ns = pns;
474                 bus = pbus;
475
476                 of_dump_addr("OF: one level translation:", addr, na);
477         }
478  bail:
479         of_node_put(parent);
480         of_node_put(dev);
481
482         return result;
483 }
484
485 u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
486 {
487         return __of_translate_address(dev, in_addr, "ranges");
488 }
489 EXPORT_SYMBOL(of_translate_address);
490
491 u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
492 {
493         return __of_translate_address(dev, in_addr, "dma-ranges");
494 }
495 EXPORT_SYMBOL(of_translate_dma_address);
496
497 bool of_can_translate_address(struct device_node *dev)
498 {
499         struct device_node *parent;
500         struct of_bus *bus;
501         int na, ns;
502
503         parent = of_get_parent(dev);
504         if (parent == NULL)
505                 return false;
506
507         bus = of_match_bus(parent);
508         bus->count_cells(dev, &na, &ns);
509
510         of_node_put(parent);
511
512         return OF_CHECK_COUNTS(na, ns);
513 }
514 EXPORT_SYMBOL(of_can_translate_address);
515
516 const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
517                     unsigned int *flags)
518 {
519         const __be32 *prop;
520         unsigned int psize;
521         struct device_node *parent;
522         struct of_bus *bus;
523         int onesize, i, na, ns;
524
525         /* Get parent & match bus type */
526         parent = of_get_parent(dev);
527         if (parent == NULL)
528                 return NULL;
529         bus = of_match_bus(parent);
530         bus->count_cells(dev, &na, &ns);
531         of_node_put(parent);
532         if (!OF_CHECK_ADDR_COUNT(na))
533                 return NULL;
534
535         /* Get "reg" or "assigned-addresses" property */
536         prop = of_get_property(dev, bus->addresses, &psize);
537         if (prop == NULL)
538                 return NULL;
539         psize /= 4;
540
541         onesize = na + ns;
542         for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
543                 if (i == index) {
544                         if (size)
545                                 *size = of_read_number(prop + na, ns);
546                         if (flags)
547                                 *flags = bus->get_flags(prop);
548                         return prop;
549                 }
550         return NULL;
551 }
552 EXPORT_SYMBOL(of_get_address);
553
554 static int __of_address_to_resource(struct device_node *dev,
555                 const __be32 *addrp, u64 size, unsigned int flags,
556                 const char *name, struct resource *r)
557 {
558         u64 taddr;
559
560         if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
561                 return -EINVAL;
562         taddr = of_translate_address(dev, addrp);
563         if (taddr == OF_BAD_ADDR)
564                 return -EINVAL;
565         memset(r, 0, sizeof(struct resource));
566         if (flags & IORESOURCE_IO) {
567                 unsigned long port;
568                 port = pci_address_to_pio(taddr);
569                 if (port == (unsigned long)-1)
570                         return -EINVAL;
571                 r->start = port;
572                 r->end = port + size - 1;
573         } else {
574                 r->start = taddr;
575                 r->end = taddr + size - 1;
576         }
577         r->flags = flags;
578         r->name = name ? name : dev->full_name;
579
580         return 0;
581 }
582
583 /**
584  * of_address_to_resource - Translate device tree address and return as resource
585  *
586  * Note that if your address is a PIO address, the conversion will fail if
587  * the physical address can't be internally converted to an IO token with
588  * pci_address_to_pio(), that is because it's either called to early or it
589  * can't be matched to any host bridge IO space
590  */
591 int of_address_to_resource(struct device_node *dev, int index,
592                            struct resource *r)
593 {
594         const __be32    *addrp;
595         u64             size;
596         unsigned int    flags;
597         const char      *name = NULL;
598
599         addrp = of_get_address(dev, index, &size, &flags);
600         if (addrp == NULL)
601                 return -EINVAL;
602
603         /* Get optional "reg-names" property to add a name to a resource */
604         of_property_read_string_index(dev, "reg-names", index, &name);
605
606         return __of_address_to_resource(dev, addrp, size, flags, name, r);
607 }
608 EXPORT_SYMBOL_GPL(of_address_to_resource);
609
610 struct device_node *of_find_matching_node_by_address(struct device_node *from,
611                                         const struct of_device_id *matches,
612                                         u64 base_address)
613 {
614         struct device_node *dn = of_find_matching_node(from, matches);
615         struct resource res;
616
617         while (dn) {
618                 if (of_address_to_resource(dn, 0, &res))
619                         continue;
620                 if (res.start == base_address)
621                         return dn;
622                 dn = of_find_matching_node(dn, matches);
623         }
624
625         return NULL;
626 }
627
628
629 /**
630  * of_iomap - Maps the memory mapped IO for a given device_node
631  * @device:     the device whose io range will be mapped
632  * @index:      index of the io range
633  *
634  * Returns a pointer to the mapped memory
635  */
636 void __iomem *of_iomap(struct device_node *np, int index)
637 {
638         struct resource res;
639
640         if (of_address_to_resource(np, index, &res))
641                 return NULL;
642
643         return ioremap(res.start, resource_size(&res));
644 }
645 EXPORT_SYMBOL(of_iomap);