sparc: Fix handling of LANCE and ESP parent nodes in of_device.c
[linux-2.6] / arch / sparc / kernel / of_device.c
1 #include <linux/string.h>
2 #include <linux/kernel.h>
3 #include <linux/of.h>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/slab.h>
8 #include <linux/errno.h>
9 #include <linux/of_device.h>
10 #include <linux/of_platform.h>
11
12 static int node_match(struct device *dev, void *data)
13 {
14         struct of_device *op = to_of_device(dev);
15         struct device_node *dp = data;
16
17         return (op->node == dp);
18 }
19
20 struct of_device *of_find_device_by_node(struct device_node *dp)
21 {
22         struct device *dev = bus_find_device(&of_platform_bus_type, NULL,
23                                              dp, node_match);
24
25         if (dev)
26                 return to_of_device(dev);
27
28         return NULL;
29 }
30 EXPORT_SYMBOL(of_find_device_by_node);
31
32 unsigned int irq_of_parse_and_map(struct device_node *node, int index)
33 {
34         struct of_device *op = of_find_device_by_node(node);
35
36         if (!op || index >= op->num_irqs)
37                 return 0;
38
39         return op->irqs[index];
40 }
41 EXPORT_SYMBOL(irq_of_parse_and_map);
42
43 /* Take the archdata values for IOMMU, STC, and HOSTDATA found in
44  * BUS and propagate to all child of_device objects.
45  */
46 void of_propagate_archdata(struct of_device *bus)
47 {
48         struct dev_archdata *bus_sd = &bus->dev.archdata;
49         struct device_node *bus_dp = bus->node;
50         struct device_node *dp;
51
52         for (dp = bus_dp->child; dp; dp = dp->sibling) {
53                 struct of_device *op = of_find_device_by_node(dp);
54
55                 op->dev.archdata.iommu = bus_sd->iommu;
56                 op->dev.archdata.stc = bus_sd->stc;
57                 op->dev.archdata.host_controller = bus_sd->host_controller;
58                 op->dev.archdata.numa_node = bus_sd->numa_node;
59
60                 if (dp->child)
61                         of_propagate_archdata(op);
62         }
63 }
64
65 struct bus_type of_platform_bus_type;
66 EXPORT_SYMBOL(of_platform_bus_type);
67
68 static inline u64 of_read_addr(const u32 *cell, int size)
69 {
70         u64 r = 0;
71         while (size--)
72                 r = (r << 32) | *(cell++);
73         return r;
74 }
75
76 static void __init get_cells(struct device_node *dp,
77                              int *addrc, int *sizec)
78 {
79         if (addrc)
80                 *addrc = of_n_addr_cells(dp);
81         if (sizec)
82                 *sizec = of_n_size_cells(dp);
83 }
84
85 /* Max address size we deal with */
86 #define OF_MAX_ADDR_CELLS       4
87
88 struct of_bus {
89         const char      *name;
90         const char      *addr_prop_name;
91         int             (*match)(struct device_node *parent);
92         void            (*count_cells)(struct device_node *child,
93                                        int *addrc, int *sizec);
94         int             (*map)(u32 *addr, const u32 *range,
95                                int na, int ns, int pna);
96         unsigned int    (*get_flags)(const u32 *addr);
97 };
98
99 /*
100  * Default translator (generic bus)
101  */
102
103 static void of_bus_default_count_cells(struct device_node *dev,
104                                        int *addrc, int *sizec)
105 {
106         get_cells(dev, addrc, sizec);
107 }
108
109 /* Make sure the least significant 64-bits are in-range.  Even
110  * for 3 or 4 cell values it is a good enough approximation.
111  */
112 static int of_out_of_range(const u32 *addr, const u32 *base,
113                            const u32 *size, int na, int ns)
114 {
115         u64 a = of_read_addr(addr, na);
116         u64 b = of_read_addr(base, na);
117
118         if (a < b)
119                 return 1;
120
121         b += of_read_addr(size, ns);
122         if (a >= b)
123                 return 1;
124
125         return 0;
126 }
127
128 static int of_bus_default_map(u32 *addr, const u32 *range,
129                               int na, int ns, int pna)
130 {
131         u32 result[OF_MAX_ADDR_CELLS];
132         int i;
133
134         if (ns > 2) {
135                 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
136                 return -EINVAL;
137         }
138
139         if (of_out_of_range(addr, range, range + na + pna, na, ns))
140                 return -EINVAL;
141
142         /* Start with the parent range base.  */
143         memcpy(result, range + na, pna * 4);
144
145         /* Add in the child address offset.  */
146         for (i = 0; i < na; i++)
147                 result[pna - 1 - i] +=
148                         (addr[na - 1 - i] -
149                          range[na - 1 - i]);
150
151         memcpy(addr, result, pna * 4);
152
153         return 0;
154 }
155
156 static unsigned int of_bus_default_get_flags(const u32 *addr)
157 {
158         return IORESOURCE_MEM;
159 }
160
161 /*
162  * PCI bus specific translator
163  */
164
165 static int of_bus_pci_match(struct device_node *np)
166 {
167         if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
168                 /* Do not do PCI specific frobbing if the
169                  * PCI bridge lacks a ranges property.  We
170                  * want to pass it through up to the next
171                  * parent as-is, not with the PCI translate
172                  * method which chops off the top address cell.
173                  */
174                 if (!of_find_property(np, "ranges", NULL))
175                         return 0;
176
177                 return 1;
178         }
179
180         return 0;
181 }
182
183 static void of_bus_pci_count_cells(struct device_node *np,
184                                    int *addrc, int *sizec)
185 {
186         if (addrc)
187                 *addrc = 3;
188         if (sizec)
189                 *sizec = 2;
190 }
191
192 static int of_bus_pci_map(u32 *addr, const u32 *range,
193                           int na, int ns, int pna)
194 {
195         u32 result[OF_MAX_ADDR_CELLS];
196         int i;
197
198         /* Check address type match */
199         if ((addr[0] ^ range[0]) & 0x03000000)
200                 return -EINVAL;
201
202         if (of_out_of_range(addr + 1, range + 1, range + na + pna,
203                             na - 1, ns))
204                 return -EINVAL;
205
206         /* Start with the parent range base.  */
207         memcpy(result, range + na, pna * 4);
208
209         /* Add in the child address offset, skipping high cell.  */
210         for (i = 0; i < na - 1; i++)
211                 result[pna - 1 - i] +=
212                         (addr[na - 1 - i] -
213                          range[na - 1 - i]);
214
215         memcpy(addr, result, pna * 4);
216
217         return 0;
218 }
219
220 static unsigned int of_bus_pci_get_flags(const u32 *addr)
221 {
222         unsigned int flags = 0;
223         u32 w = addr[0];
224
225         switch((w >> 24) & 0x03) {
226         case 0x01:
227                 flags |= IORESOURCE_IO;
228         case 0x02: /* 32 bits */
229         case 0x03: /* 64 bits */
230                 flags |= IORESOURCE_MEM;
231         }
232         if (w & 0x40000000)
233                 flags |= IORESOURCE_PREFETCH;
234         return flags;
235 }
236
237 /*
238  * SBUS bus specific translator
239  */
240
241 static int of_bus_sbus_match(struct device_node *np)
242 {
243         return !strcmp(np->name, "sbus") ||
244                 !strcmp(np->name, "sbi");
245 }
246
247 static void of_bus_sbus_count_cells(struct device_node *child,
248                                    int *addrc, int *sizec)
249 {
250         if (addrc)
251                 *addrc = 2;
252         if (sizec)
253                 *sizec = 1;
254 }
255
256 static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna)
257 {
258         return of_bus_default_map(addr, range, na, ns, pna);
259 }
260
261 static unsigned int of_bus_sbus_get_flags(const u32 *addr)
262 {
263         return IORESOURCE_MEM;
264 }
265
266
267 /*
268  * Array of bus specific translators
269  */
270
271 static struct of_bus of_busses[] = {
272         /* PCI */
273         {
274                 .name = "pci",
275                 .addr_prop_name = "assigned-addresses",
276                 .match = of_bus_pci_match,
277                 .count_cells = of_bus_pci_count_cells,
278                 .map = of_bus_pci_map,
279                 .get_flags = of_bus_pci_get_flags,
280         },
281         /* SBUS */
282         {
283                 .name = "sbus",
284                 .addr_prop_name = "reg",
285                 .match = of_bus_sbus_match,
286                 .count_cells = of_bus_sbus_count_cells,
287                 .map = of_bus_sbus_map,
288                 .get_flags = of_bus_sbus_get_flags,
289         },
290         /* Default */
291         {
292                 .name = "default",
293                 .addr_prop_name = "reg",
294                 .match = NULL,
295                 .count_cells = of_bus_default_count_cells,
296                 .map = of_bus_default_map,
297                 .get_flags = of_bus_default_get_flags,
298         },
299 };
300
301 static struct of_bus *of_match_bus(struct device_node *np)
302 {
303         int i;
304
305         for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
306                 if (!of_busses[i].match || of_busses[i].match(np))
307                         return &of_busses[i];
308         BUG();
309         return NULL;
310 }
311
312 static int __init build_one_resource(struct device_node *parent,
313                                      struct of_bus *bus,
314                                      struct of_bus *pbus,
315                                      u32 *addr,
316                                      int na, int ns, int pna)
317 {
318         const u32 *ranges;
319         unsigned int rlen;
320         int rone;
321
322         ranges = of_get_property(parent, "ranges", &rlen);
323         if (ranges == NULL || rlen == 0) {
324                 u32 result[OF_MAX_ADDR_CELLS];
325                 int i;
326
327                 memset(result, 0, pna * 4);
328                 for (i = 0; i < na; i++)
329                         result[pna - 1 - i] =
330                                 addr[na - 1 - i];
331
332                 memcpy(addr, result, pna * 4);
333                 return 0;
334         }
335
336         /* Now walk through the ranges */
337         rlen /= 4;
338         rone = na + pna + ns;
339         for (; rlen >= rone; rlen -= rone, ranges += rone) {
340                 if (!bus->map(addr, ranges, na, ns, pna))
341                         return 0;
342         }
343
344         return 1;
345 }
346
347 static int __init use_1to1_mapping(struct device_node *pp)
348 {
349         /* If we have a ranges property in the parent, use it.  */
350         if (of_find_property(pp, "ranges", NULL) != NULL)
351                 return 0;
352
353         /* Some SBUS devices use intermediate nodes to express
354          * hierarchy within the device itself.  These aren't
355          * real bus nodes, and don't have a 'ranges' property.
356          * But, we should still pass the translation work up
357          * to the SBUS itself.
358          */
359         if (!strcmp(pp->name, "dma") ||
360             !strcmp(pp->name, "espdma") ||
361             !strcmp(pp->name, "ledma") ||
362             !strcmp(pp->name, "lebuffer"))
363                 return 0;
364
365         return 1;
366 }
367
368 static int of_resource_verbose;
369
370 static void __init build_device_resources(struct of_device *op,
371                                           struct device *parent)
372 {
373         struct of_device *p_op;
374         struct of_bus *bus;
375         int na, ns;
376         int index, num_reg;
377         const void *preg;
378
379         if (!parent)
380                 return;
381
382         p_op = to_of_device(parent);
383         bus = of_match_bus(p_op->node);
384         bus->count_cells(op->node, &na, &ns);
385
386         preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
387         if (!preg || num_reg == 0)
388                 return;
389
390         /* Convert to num-cells.  */
391         num_reg /= 4;
392
393         /* Conver to num-entries.  */
394         num_reg /= na + ns;
395
396         for (index = 0; index < num_reg; index++) {
397                 struct resource *r = &op->resource[index];
398                 u32 addr[OF_MAX_ADDR_CELLS];
399                 const u32 *reg = (preg + (index * ((na + ns) * 4)));
400                 struct device_node *dp = op->node;
401                 struct device_node *pp = p_op->node;
402                 struct of_bus *pbus, *dbus;
403                 u64 size, result = OF_BAD_ADDR;
404                 unsigned long flags;
405                 int dna, dns;
406                 int pna, pns;
407
408                 size = of_read_addr(reg + na, ns);
409                 flags = bus->get_flags(reg);
410
411                 memcpy(addr, reg, na * 4);
412
413                 if (use_1to1_mapping(pp)) {
414                         result = of_read_addr(addr, na);
415                         goto build_res;
416                 }
417
418                 dna = na;
419                 dns = ns;
420                 dbus = bus;
421
422                 while (1) {
423                         dp = pp;
424                         pp = dp->parent;
425                         if (!pp) {
426                                 result = of_read_addr(addr, dna);
427                                 break;
428                         }
429
430                         pbus = of_match_bus(pp);
431                         pbus->count_cells(dp, &pna, &pns);
432
433                         if (build_one_resource(dp, dbus, pbus, addr,
434                                                dna, dns, pna))
435                                 break;
436
437                         dna = pna;
438                         dns = pns;
439                         dbus = pbus;
440                 }
441
442         build_res:
443                 memset(r, 0, sizeof(*r));
444
445                 if (of_resource_verbose)
446                         printk("%s reg[%d] -> %llx\n",
447                                op->node->full_name, index,
448                                result);
449
450                 if (result != OF_BAD_ADDR) {
451                         r->start = result & 0xffffffff;
452                         r->end = result + size - 1;
453                         r->flags = flags | ((result >> 32ULL) & 0xffUL);
454                 }
455                 r->name = op->node->name;
456         }
457 }
458
459 static struct of_device * __init scan_one_device(struct device_node *dp,
460                                                  struct device *parent)
461 {
462         struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
463         const struct linux_prom_irqs *intr;
464         struct dev_archdata *sd;
465         int len, i;
466
467         if (!op)
468                 return NULL;
469
470         sd = &op->dev.archdata;
471         sd->prom_node = dp;
472         sd->op = op;
473
474         op->node = dp;
475
476         op->clock_freq = of_getintprop_default(dp, "clock-frequency",
477                                                (25*1000*1000));
478         op->portid = of_getintprop_default(dp, "upa-portid", -1);
479         if (op->portid == -1)
480                 op->portid = of_getintprop_default(dp, "portid", -1);
481
482         intr = of_get_property(dp, "intr", &len);
483         if (intr) {
484                 op->num_irqs = len / sizeof(struct linux_prom_irqs);
485                 for (i = 0; i < op->num_irqs; i++)
486                         op->irqs[i] = intr[i].pri;
487         } else {
488                 const unsigned int *irq =
489                         of_get_property(dp, "interrupts", &len);
490
491                 if (irq) {
492                         op->num_irqs = len / sizeof(unsigned int);
493                         for (i = 0; i < op->num_irqs; i++)
494                                 op->irqs[i] = irq[i];
495                 } else {
496                         op->num_irqs = 0;
497                 }
498         }
499         if (sparc_cpu_model == sun4d) {
500                 static int pil_to_sbus[] = {
501                         0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
502                 };
503                 struct device_node *io_unit, *sbi = dp->parent;
504                 const struct linux_prom_registers *regs;
505                 int board, slot;
506
507                 while (sbi) {
508                         if (!strcmp(sbi->name, "sbi"))
509                                 break;
510
511                         sbi = sbi->parent;
512                 }
513                 if (!sbi)
514                         goto build_resources;
515
516                 regs = of_get_property(dp, "reg", NULL);
517                 if (!regs)
518                         goto build_resources;
519
520                 slot = regs->which_io;
521
522                 /* If SBI's parent is not io-unit or the io-unit lacks
523                  * a "board#" property, something is very wrong.
524                  */
525                 if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) {
526                         printk("%s: Error, parent is not io-unit.\n",
527                                sbi->full_name);
528                         goto build_resources;
529                 }
530                 io_unit = sbi->parent;
531                 board = of_getintprop_default(io_unit, "board#", -1);
532                 if (board == -1) {
533                         printk("%s: Error, lacks board# property.\n",
534                                io_unit->full_name);
535                         goto build_resources;
536                 }
537
538                 for (i = 0; i < op->num_irqs; i++) {
539                         int this_irq = op->irqs[i];
540                         int sbusl = pil_to_sbus[this_irq];
541
542                         if (sbusl)
543                                 this_irq = (((board + 1) << 5) +
544                                             (sbusl << 2) +
545                                             slot);
546
547                         op->irqs[i] = this_irq;
548                 }
549         }
550
551 build_resources:
552         build_device_resources(op, parent);
553
554         op->dev.parent = parent;
555         op->dev.bus = &of_platform_bus_type;
556         if (!parent)
557                 strcpy(op->dev.bus_id, "root");
558         else
559                 sprintf(op->dev.bus_id, "%08x", dp->node);
560
561         if (of_device_register(op)) {
562                 printk("%s: Could not register of device.\n",
563                        dp->full_name);
564                 kfree(op);
565                 op = NULL;
566         }
567
568         return op;
569 }
570
571 static void __init scan_tree(struct device_node *dp, struct device *parent)
572 {
573         while (dp) {
574                 struct of_device *op = scan_one_device(dp, parent);
575
576                 if (op)
577                         scan_tree(dp->child, &op->dev);
578
579                 dp = dp->sibling;
580         }
581 }
582
583 static void __init scan_of_devices(void)
584 {
585         struct device_node *root = of_find_node_by_path("/");
586         struct of_device *parent;
587
588         parent = scan_one_device(root, NULL);
589         if (!parent)
590                 return;
591
592         scan_tree(root->child, &parent->dev);
593 }
594
595 static int __init of_bus_driver_init(void)
596 {
597         int err;
598
599         err = of_bus_type_init(&of_platform_bus_type, "of");
600         if (!err)
601                 scan_of_devices();
602
603         return err;
604 }
605
606 postcore_initcall(of_bus_driver_init);
607
608 static int __init of_debug(char *str)
609 {
610         int val = 0;
611
612         get_option(&str, &val);
613         if (val & 1)
614                 of_resource_verbose = 1;
615         return 1;
616 }
617
618 __setup("of_debug=", of_debug);