Merge branch 'imx/compile-fixes' of git://git.linaro.org/people/shawnguo/linux-2...
[pandora-kernel.git] / arch / powerpc / kernel / ibmebus.c
1 /*
2  * IBM PowerPC IBM eBus Infrastructure Support.
3  *
4  * Copyright (c) 2005 IBM Corporation
5  *  Joachim Fenkes <fenkes@de.ibm.com>
6  *  Heiko J Schick <schickhj@de.ibm.com>
7  *
8  * All rights reserved.
9  *
10  * This source code is distributed under a dual license of GPL v2.0 and OpenIB
11  * BSD.
12  *
13  * OpenIB BSD License
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions are met:
17  *
18  * Redistributions of source code must retain the above copyright notice, this
19  * list of conditions and the following disclaimer.
20  *
21  * Redistributions in binary form must reproduce the above copyright notice,
22  * this list of conditions and the following disclaimer in the documentation
23  * and/or other materials
24  * provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
33  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38
39 #include <linux/init.h>
40 #include <linux/export.h>
41 #include <linux/console.h>
42 #include <linux/kobject.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/interrupt.h>
45 #include <linux/of.h>
46 #include <linux/slab.h>
47 #include <linux/stat.h>
48 #include <linux/of_platform.h>
49 #include <asm/ibmebus.h>
50 #include <asm/abs_addr.h>
51
52 static struct device ibmebus_bus_device = { /* fake "parent" device */
53         .init_name = "ibmebus",
54 };
55
56 struct bus_type ibmebus_bus_type;
57
58 /* These devices will automatically be added to the bus during init */
59 static struct of_device_id __initdata ibmebus_matches[] = {
60         { .compatible = "IBM,lhca" },
61         { .compatible = "IBM,lhea" },
62         {},
63 };
64
65 static void *ibmebus_alloc_coherent(struct device *dev,
66                                     size_t size,
67                                     dma_addr_t *dma_handle,
68                                     gfp_t flag)
69 {
70         void *mem;
71
72         mem = kmalloc(size, flag);
73         *dma_handle = (dma_addr_t)mem;
74
75         return mem;
76 }
77
78 static void ibmebus_free_coherent(struct device *dev,
79                                   size_t size, void *vaddr,
80                                   dma_addr_t dma_handle)
81 {
82         kfree(vaddr);
83 }
84
85 static dma_addr_t ibmebus_map_page(struct device *dev,
86                                    struct page *page,
87                                    unsigned long offset,
88                                    size_t size,
89                                    enum dma_data_direction direction,
90                                    struct dma_attrs *attrs)
91 {
92         return (dma_addr_t)(page_address(page) + offset);
93 }
94
95 static void ibmebus_unmap_page(struct device *dev,
96                                dma_addr_t dma_addr,
97                                size_t size,
98                                enum dma_data_direction direction,
99                                struct dma_attrs *attrs)
100 {
101         return;
102 }
103
104 static int ibmebus_map_sg(struct device *dev,
105                           struct scatterlist *sgl,
106                           int nents, enum dma_data_direction direction,
107                           struct dma_attrs *attrs)
108 {
109         struct scatterlist *sg;
110         int i;
111
112         for_each_sg(sgl, sg, nents, i) {
113                 sg->dma_address = (dma_addr_t) sg_virt(sg);
114                 sg->dma_length = sg->length;
115         }
116
117         return nents;
118 }
119
120 static void ibmebus_unmap_sg(struct device *dev,
121                              struct scatterlist *sg,
122                              int nents, enum dma_data_direction direction,
123                              struct dma_attrs *attrs)
124 {
125         return;
126 }
127
128 static int ibmebus_dma_supported(struct device *dev, u64 mask)
129 {
130         return mask == DMA_BIT_MASK(64);
131 }
132
133 static u64 ibmebus_dma_get_required_mask(struct device *dev)
134 {
135         return DMA_BIT_MASK(64);
136 }
137
138 static struct dma_map_ops ibmebus_dma_ops = {
139         .alloc_coherent     = ibmebus_alloc_coherent,
140         .free_coherent      = ibmebus_free_coherent,
141         .map_sg             = ibmebus_map_sg,
142         .unmap_sg           = ibmebus_unmap_sg,
143         .dma_supported      = ibmebus_dma_supported,
144         .get_required_mask  = ibmebus_dma_get_required_mask,
145         .map_page           = ibmebus_map_page,
146         .unmap_page         = ibmebus_unmap_page,
147 };
148
149 static int ibmebus_match_path(struct device *dev, void *data)
150 {
151         struct device_node *dn = to_platform_device(dev)->dev.of_node;
152         return (dn->full_name &&
153                 (strcasecmp((char *)data, dn->full_name) == 0));
154 }
155
156 static int ibmebus_match_node(struct device *dev, void *data)
157 {
158         return to_platform_device(dev)->dev.of_node == data;
159 }
160
161 static int ibmebus_create_device(struct device_node *dn)
162 {
163         struct platform_device *dev;
164         int ret;
165
166         dev = of_device_alloc(dn, NULL, &ibmebus_bus_device);
167         if (!dev)
168                 return -ENOMEM;
169
170         dev->dev.bus = &ibmebus_bus_type;
171         dev->dev.archdata.dma_ops = &ibmebus_dma_ops;
172
173         ret = of_device_add(dev);
174         if (ret)
175                 platform_device_put(dev);
176         return ret;
177 }
178
179 static int ibmebus_create_devices(const struct of_device_id *matches)
180 {
181         struct device_node *root, *child;
182         int ret = 0;
183
184         root = of_find_node_by_path("/");
185
186         for_each_child_of_node(root, child) {
187                 if (!of_match_node(matches, child))
188                         continue;
189
190                 if (bus_find_device(&ibmebus_bus_type, NULL, child,
191                                     ibmebus_match_node))
192                         continue;
193
194                 ret = ibmebus_create_device(child);
195                 if (ret) {
196                         printk(KERN_ERR "%s: failed to create device (%i)",
197                                __func__, ret);
198                         of_node_put(child);
199                         break;
200                 }
201         }
202
203         of_node_put(root);
204         return ret;
205 }
206
207 int ibmebus_register_driver(struct of_platform_driver *drv)
208 {
209         /* If the driver uses devices that ibmebus doesn't know, add them */
210         ibmebus_create_devices(drv->driver.of_match_table);
211
212         drv->driver.bus = &ibmebus_bus_type;
213         return driver_register(&drv->driver);
214 }
215 EXPORT_SYMBOL(ibmebus_register_driver);
216
217 void ibmebus_unregister_driver(struct of_platform_driver *drv)
218 {
219         driver_unregister(&drv->driver);
220 }
221 EXPORT_SYMBOL(ibmebus_unregister_driver);
222
223 int ibmebus_request_irq(u32 ist, irq_handler_t handler,
224                         unsigned long irq_flags, const char *devname,
225                         void *dev_id)
226 {
227         unsigned int irq = irq_create_mapping(NULL, ist);
228
229         if (irq == NO_IRQ)
230                 return -EINVAL;
231
232         return request_irq(irq, handler, irq_flags, devname, dev_id);
233 }
234 EXPORT_SYMBOL(ibmebus_request_irq);
235
236 void ibmebus_free_irq(u32 ist, void *dev_id)
237 {
238         unsigned int irq = irq_find_mapping(NULL, ist);
239
240         free_irq(irq, dev_id);
241         irq_dispose_mapping(irq);
242 }
243 EXPORT_SYMBOL(ibmebus_free_irq);
244
245 static char *ibmebus_chomp(const char *in, size_t count)
246 {
247         char *out = kmalloc(count + 1, GFP_KERNEL);
248
249         if (!out)
250                 return NULL;
251
252         memcpy(out, in, count);
253         out[count] = '\0';
254         if (out[count - 1] == '\n')
255                 out[count - 1] = '\0';
256
257         return out;
258 }
259
260 static ssize_t ibmebus_store_probe(struct bus_type *bus,
261                                    const char *buf, size_t count)
262 {
263         struct device_node *dn = NULL;
264         char *path;
265         ssize_t rc = 0;
266
267         path = ibmebus_chomp(buf, count);
268         if (!path)
269                 return -ENOMEM;
270
271         if (bus_find_device(&ibmebus_bus_type, NULL, path,
272                             ibmebus_match_path)) {
273                 printk(KERN_WARNING "%s: %s has already been probed\n",
274                        __func__, path);
275                 rc = -EEXIST;
276                 goto out;
277         }
278
279         if ((dn = of_find_node_by_path(path))) {
280                 rc = ibmebus_create_device(dn);
281                 of_node_put(dn);
282         } else {
283                 printk(KERN_WARNING "%s: no such device node: %s\n",
284                        __func__, path);
285                 rc = -ENODEV;
286         }
287
288 out:
289         kfree(path);
290         if (rc)
291                 return rc;
292         return count;
293 }
294
295 static ssize_t ibmebus_store_remove(struct bus_type *bus,
296                                     const char *buf, size_t count)
297 {
298         struct device *dev;
299         char *path;
300
301         path = ibmebus_chomp(buf, count);
302         if (!path)
303                 return -ENOMEM;
304
305         if ((dev = bus_find_device(&ibmebus_bus_type, NULL, path,
306                                    ibmebus_match_path))) {
307                 of_device_unregister(to_platform_device(dev));
308
309                 kfree(path);
310                 return count;
311         } else {
312                 printk(KERN_WARNING "%s: %s not on the bus\n",
313                        __func__, path);
314
315                 kfree(path);
316                 return -ENODEV;
317         }
318 }
319
320
321 static struct bus_attribute ibmebus_bus_attrs[] = {
322         __ATTR(probe, S_IWUSR, NULL, ibmebus_store_probe),
323         __ATTR(remove, S_IWUSR, NULL, ibmebus_store_remove),
324         __ATTR_NULL
325 };
326
327 static int ibmebus_bus_bus_match(struct device *dev, struct device_driver *drv)
328 {
329         const struct of_device_id *matches = drv->of_match_table;
330
331         if (!matches)
332                 return 0;
333
334         return of_match_device(matches, dev) != NULL;
335 }
336
337 static int ibmebus_bus_device_probe(struct device *dev)
338 {
339         int error = -ENODEV;
340         struct of_platform_driver *drv;
341         struct platform_device *of_dev;
342         const struct of_device_id *match;
343
344         drv = to_of_platform_driver(dev->driver);
345         of_dev = to_platform_device(dev);
346
347         if (!drv->probe)
348                 return error;
349
350         of_dev_get(of_dev);
351
352         match = of_match_device(drv->driver.of_match_table, dev);
353         if (match)
354                 error = drv->probe(of_dev, match);
355         if (error)
356                 of_dev_put(of_dev);
357
358         return error;
359 }
360
361 static int ibmebus_bus_device_remove(struct device *dev)
362 {
363         struct platform_device *of_dev = to_platform_device(dev);
364         struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
365
366         if (dev->driver && drv->remove)
367                 drv->remove(of_dev);
368         return 0;
369 }
370
371 static void ibmebus_bus_device_shutdown(struct device *dev)
372 {
373         struct platform_device *of_dev = to_platform_device(dev);
374         struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
375
376         if (dev->driver && drv->shutdown)
377                 drv->shutdown(of_dev);
378 }
379
380 /*
381  * ibmebus_bus_device_attrs
382  */
383 static ssize_t devspec_show(struct device *dev,
384                                 struct device_attribute *attr, char *buf)
385 {
386         struct platform_device *ofdev;
387
388         ofdev = to_platform_device(dev);
389         return sprintf(buf, "%s\n", ofdev->dev.of_node->full_name);
390 }
391
392 static ssize_t name_show(struct device *dev,
393                                 struct device_attribute *attr, char *buf)
394 {
395         struct platform_device *ofdev;
396
397         ofdev = to_platform_device(dev);
398         return sprintf(buf, "%s\n", ofdev->dev.of_node->name);
399 }
400
401 static ssize_t modalias_show(struct device *dev,
402                                 struct device_attribute *attr, char *buf)
403 {
404         ssize_t len = of_device_get_modalias(dev, buf, PAGE_SIZE - 2);
405         buf[len] = '\n';
406         buf[len+1] = 0;
407         return len+1;
408 }
409
410 struct device_attribute ibmebus_bus_device_attrs[] = {
411         __ATTR_RO(devspec),
412         __ATTR_RO(name),
413         __ATTR_RO(modalias),
414         __ATTR_NULL
415 };
416
417 #ifdef CONFIG_PM_SLEEP
418 static int ibmebus_bus_legacy_suspend(struct device *dev, pm_message_t mesg)
419 {
420         struct platform_device *of_dev = to_platform_device(dev);
421         struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
422         int ret = 0;
423
424         if (dev->driver && drv->suspend)
425                 ret = drv->suspend(of_dev, mesg);
426         return ret;
427 }
428
429 static int ibmebus_bus_legacy_resume(struct device *dev)
430 {
431         struct platform_device *of_dev = to_platform_device(dev);
432         struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
433         int ret = 0;
434
435         if (dev->driver && drv->resume)
436                 ret = drv->resume(of_dev);
437         return ret;
438 }
439
440 static int ibmebus_bus_pm_prepare(struct device *dev)
441 {
442         struct device_driver *drv = dev->driver;
443         int ret = 0;
444
445         if (drv && drv->pm && drv->pm->prepare)
446                 ret = drv->pm->prepare(dev);
447
448         return ret;
449 }
450
451 static void ibmebus_bus_pm_complete(struct device *dev)
452 {
453         struct device_driver *drv = dev->driver;
454
455         if (drv && drv->pm && drv->pm->complete)
456                 drv->pm->complete(dev);
457 }
458
459 #ifdef CONFIG_SUSPEND
460
461 static int ibmebus_bus_pm_suspend(struct device *dev)
462 {
463         struct device_driver *drv = dev->driver;
464         int ret = 0;
465
466         if (!drv)
467                 return 0;
468
469         if (drv->pm) {
470                 if (drv->pm->suspend)
471                         ret = drv->pm->suspend(dev);
472         } else {
473                 ret = ibmebus_bus_legacy_suspend(dev, PMSG_SUSPEND);
474         }
475
476         return ret;
477 }
478
479 static int ibmebus_bus_pm_suspend_noirq(struct device *dev)
480 {
481         struct device_driver *drv = dev->driver;
482         int ret = 0;
483
484         if (!drv)
485                 return 0;
486
487         if (drv->pm) {
488                 if (drv->pm->suspend_noirq)
489                         ret = drv->pm->suspend_noirq(dev);
490         }
491
492         return ret;
493 }
494
495 static int ibmebus_bus_pm_resume(struct device *dev)
496 {
497         struct device_driver *drv = dev->driver;
498         int ret = 0;
499
500         if (!drv)
501                 return 0;
502
503         if (drv->pm) {
504                 if (drv->pm->resume)
505                         ret = drv->pm->resume(dev);
506         } else {
507                 ret = ibmebus_bus_legacy_resume(dev);
508         }
509
510         return ret;
511 }
512
513 static int ibmebus_bus_pm_resume_noirq(struct device *dev)
514 {
515         struct device_driver *drv = dev->driver;
516         int ret = 0;
517
518         if (!drv)
519                 return 0;
520
521         if (drv->pm) {
522                 if (drv->pm->resume_noirq)
523                         ret = drv->pm->resume_noirq(dev);
524         }
525
526         return ret;
527 }
528
529 #else /* !CONFIG_SUSPEND */
530
531 #define ibmebus_bus_pm_suspend          NULL
532 #define ibmebus_bus_pm_resume           NULL
533 #define ibmebus_bus_pm_suspend_noirq    NULL
534 #define ibmebus_bus_pm_resume_noirq     NULL
535
536 #endif /* !CONFIG_SUSPEND */
537
538 #ifdef CONFIG_HIBERNATE_CALLBACKS
539
540 static int ibmebus_bus_pm_freeze(struct device *dev)
541 {
542         struct device_driver *drv = dev->driver;
543         int ret = 0;
544
545         if (!drv)
546                 return 0;
547
548         if (drv->pm) {
549                 if (drv->pm->freeze)
550                         ret = drv->pm->freeze(dev);
551         } else {
552                 ret = ibmebus_bus_legacy_suspend(dev, PMSG_FREEZE);
553         }
554
555         return ret;
556 }
557
558 static int ibmebus_bus_pm_freeze_noirq(struct device *dev)
559 {
560         struct device_driver *drv = dev->driver;
561         int ret = 0;
562
563         if (!drv)
564                 return 0;
565
566         if (drv->pm) {
567                 if (drv->pm->freeze_noirq)
568                         ret = drv->pm->freeze_noirq(dev);
569         }
570
571         return ret;
572 }
573
574 static int ibmebus_bus_pm_thaw(struct device *dev)
575 {
576         struct device_driver *drv = dev->driver;
577         int ret = 0;
578
579         if (!drv)
580                 return 0;
581
582         if (drv->pm) {
583                 if (drv->pm->thaw)
584                         ret = drv->pm->thaw(dev);
585         } else {
586                 ret = ibmebus_bus_legacy_resume(dev);
587         }
588
589         return ret;
590 }
591
592 static int ibmebus_bus_pm_thaw_noirq(struct device *dev)
593 {
594         struct device_driver *drv = dev->driver;
595         int ret = 0;
596
597         if (!drv)
598                 return 0;
599
600         if (drv->pm) {
601                 if (drv->pm->thaw_noirq)
602                         ret = drv->pm->thaw_noirq(dev);
603         }
604
605         return ret;
606 }
607
608 static int ibmebus_bus_pm_poweroff(struct device *dev)
609 {
610         struct device_driver *drv = dev->driver;
611         int ret = 0;
612
613         if (!drv)
614                 return 0;
615
616         if (drv->pm) {
617                 if (drv->pm->poweroff)
618                         ret = drv->pm->poweroff(dev);
619         } else {
620                 ret = ibmebus_bus_legacy_suspend(dev, PMSG_HIBERNATE);
621         }
622
623         return ret;
624 }
625
626 static int ibmebus_bus_pm_poweroff_noirq(struct device *dev)
627 {
628         struct device_driver *drv = dev->driver;
629         int ret = 0;
630
631         if (!drv)
632                 return 0;
633
634         if (drv->pm) {
635                 if (drv->pm->poweroff_noirq)
636                         ret = drv->pm->poweroff_noirq(dev);
637         }
638
639         return ret;
640 }
641
642 static int ibmebus_bus_pm_restore(struct device *dev)
643 {
644         struct device_driver *drv = dev->driver;
645         int ret = 0;
646
647         if (!drv)
648                 return 0;
649
650         if (drv->pm) {
651                 if (drv->pm->restore)
652                         ret = drv->pm->restore(dev);
653         } else {
654                 ret = ibmebus_bus_legacy_resume(dev);
655         }
656
657         return ret;
658 }
659
660 static int ibmebus_bus_pm_restore_noirq(struct device *dev)
661 {
662         struct device_driver *drv = dev->driver;
663         int ret = 0;
664
665         if (!drv)
666                 return 0;
667
668         if (drv->pm) {
669                 if (drv->pm->restore_noirq)
670                         ret = drv->pm->restore_noirq(dev);
671         }
672
673         return ret;
674 }
675
676 #else /* !CONFIG_HIBERNATE_CALLBACKS */
677
678 #define ibmebus_bus_pm_freeze           NULL
679 #define ibmebus_bus_pm_thaw             NULL
680 #define ibmebus_bus_pm_poweroff         NULL
681 #define ibmebus_bus_pm_restore          NULL
682 #define ibmebus_bus_pm_freeze_noirq     NULL
683 #define ibmebus_bus_pm_thaw_noirq               NULL
684 #define ibmebus_bus_pm_poweroff_noirq   NULL
685 #define ibmebus_bus_pm_restore_noirq    NULL
686
687 #endif /* !CONFIG_HIBERNATE_CALLBACKS */
688
689 static struct dev_pm_ops ibmebus_bus_dev_pm_ops = {
690         .prepare = ibmebus_bus_pm_prepare,
691         .complete = ibmebus_bus_pm_complete,
692         .suspend = ibmebus_bus_pm_suspend,
693         .resume = ibmebus_bus_pm_resume,
694         .freeze = ibmebus_bus_pm_freeze,
695         .thaw = ibmebus_bus_pm_thaw,
696         .poweroff = ibmebus_bus_pm_poweroff,
697         .restore = ibmebus_bus_pm_restore,
698         .suspend_noirq = ibmebus_bus_pm_suspend_noirq,
699         .resume_noirq = ibmebus_bus_pm_resume_noirq,
700         .freeze_noirq = ibmebus_bus_pm_freeze_noirq,
701         .thaw_noirq = ibmebus_bus_pm_thaw_noirq,
702         .poweroff_noirq = ibmebus_bus_pm_poweroff_noirq,
703         .restore_noirq = ibmebus_bus_pm_restore_noirq,
704 };
705
706 #define IBMEBUS_BUS_PM_OPS_PTR  (&ibmebus_bus_dev_pm_ops)
707
708 #else /* !CONFIG_PM_SLEEP */
709
710 #define IBMEBUS_BUS_PM_OPS_PTR  NULL
711
712 #endif /* !CONFIG_PM_SLEEP */
713
714 struct bus_type ibmebus_bus_type = {
715         .name      = "ibmebus",
716         .uevent    = of_device_uevent,
717         .bus_attrs = ibmebus_bus_attrs,
718         .match     = ibmebus_bus_bus_match,
719         .probe     = ibmebus_bus_device_probe,
720         .remove    = ibmebus_bus_device_remove,
721         .shutdown  = ibmebus_bus_device_shutdown,
722         .dev_attrs = ibmebus_bus_device_attrs,
723         .pm        = IBMEBUS_BUS_PM_OPS_PTR,
724 };
725 EXPORT_SYMBOL(ibmebus_bus_type);
726
727 static int __init ibmebus_bus_init(void)
728 {
729         int err;
730
731         printk(KERN_INFO "IBM eBus Device Driver\n");
732
733         err = bus_register(&ibmebus_bus_type);
734         if (err) {
735                 printk(KERN_ERR "%s: failed to register IBM eBus.\n",
736                        __func__);
737                 return err;
738         }
739
740         err = device_register(&ibmebus_bus_device);
741         if (err) {
742                 printk(KERN_WARNING "%s: device_register returned %i\n",
743                        __func__, err);
744                 bus_unregister(&ibmebus_bus_type);
745
746                 return err;
747         }
748
749         err = ibmebus_create_devices(ibmebus_matches);
750         if (err) {
751                 device_unregister(&ibmebus_bus_device);
752                 bus_unregister(&ibmebus_bus_type);
753                 return err;
754         }
755
756         return 0;
757 }
758 postcore_initcall(ibmebus_bus_init);