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