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