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