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