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