http://downloads.netgear.com/files/GPL/GPL_Source_V361j_DM111PSP_series_consumer_rele...
[bcm963xx.git] / kernel / linux / arch / arm / mach-omap / bus.c
1 /*
2  * linux/arch/arm/mach-omap/bus.c
3  *
4  * Virtual bus for OMAP. Allows better power management, such as managing
5  * shared clocks, and mapping of bus addresses to Local Bus addresses.
6  *
7  * See drivers/usb/host/ohci-omap.c or drivers/video/omap/omapfb.c for
8  * examples on how to register drivers to this bus.
9  *
10  * Copyright (C) 2003 - 2004 Nokia Corporation
11  * Written by Tony Lindgren <tony@atomide.com>
12  * Portions of code based on sa1111.c.
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27  */
28
29 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/kernel.h>
33 #include <linux/delay.h>
34 #include <linux/ptrace.h>
35 #include <linux/errno.h>
36 #include <linux/ioport.h>
37 #include <linux/device.h>
38 #include <linux/slab.h>
39 #include <linux/spinlock.h>
40
41 #include <asm/hardware.h>
42 #include <asm/mach-types.h>
43 #include <asm/io.h>
44 #include <asm/irq.h>
45 #include <asm/mach/irq.h>
46
47 #include <asm/arch/bus.h>
48
49 static int omap_bus_match(struct device *_dev, struct device_driver *_drv);
50 static int omap_bus_suspend(struct device *dev, u32 state);
51 static int omap_bus_resume(struct device *dev);
52
53 /*
54  * OMAP bus definitions
55  *
56  * NOTE: Most devices should use TIPB. LBUS does automatic address mapping
57  *       to Local Bus addresses, and should only be used for Local Bus devices.
58  *       We may add new buses later on for power management reasons. Basically
59  *       we want to be able to turn off any bus if it's not used by device
60  *       drivers.
61  */
62 static struct device omap_bus_devices[OMAP_NR_BUSES] = {
63         {
64                 .bus_id         = OMAP_BUS_NAME_TIPB
65         }, {
66                 .bus_id         = OMAP_BUS_NAME_LBUS
67         },
68 };
69
70 static struct bus_type omap_bus_types[OMAP_NR_BUSES] = {
71         {
72                 .name           = OMAP_BUS_NAME_TIPB,
73                 .match          = omap_bus_match,
74                 .suspend        = omap_bus_suspend,
75                 .resume         = omap_bus_resume,
76         }, {
77                 .name           = OMAP_BUS_NAME_LBUS,   /* Local bus on 1510 */
78                 .match          = omap_bus_match,
79                 .suspend        = omap_bus_suspend,
80                 .resume         = omap_bus_resume,
81         },
82 };
83
84 static int omap_bus_match(struct device *dev, struct device_driver *drv)
85 {
86         struct omap_dev *omapdev = OMAP_DEV(dev);
87         struct omap_driver *omapdrv = OMAP_DRV(drv);
88
89         return omapdev->devid == omapdrv->devid;
90 }
91
92 static int omap_bus_suspend(struct device *dev, u32 state)
93 {
94         struct omap_dev *omapdev = OMAP_DEV(dev);
95         struct omap_driver *omapdrv = OMAP_DRV(dev->driver);
96         int ret = 0;
97
98         if (omapdrv && omapdrv->suspend)
99                 ret = omapdrv->suspend(omapdev, state);
100         return ret;
101 }
102
103 static int omap_bus_resume(struct device *dev)
104 {
105         struct omap_dev *omapdev = OMAP_DEV(dev);
106         struct omap_driver *omapdrv = OMAP_DRV(dev->driver);
107         int ret = 0;
108
109         if (omapdrv && omapdrv->resume)
110                 ret = omapdrv->resume(omapdev);
111         return ret;
112 }
113
114 static int omap_device_probe(struct device *dev)
115 {
116         struct omap_dev *omapdev = OMAP_DEV(dev);
117         struct omap_driver *omapdrv = OMAP_DRV(dev->driver);
118         int ret = -ENODEV;
119
120         if (omapdrv && omapdrv->probe)
121                 ret = omapdrv->probe(omapdev);
122
123         return ret;
124 }
125
126 static int omap_device_remove(struct device *dev)
127 {
128         struct omap_dev *omapdev = OMAP_DEV(dev);
129         struct omap_driver *omapdrv = OMAP_DRV(dev->driver);
130         int ret = 0;
131
132         if (omapdrv && omapdrv->remove)
133                 ret = omapdrv->remove(omapdev);
134         return ret;
135 }
136
137 int omap_device_register(struct omap_dev *odev)
138 {
139         if (!odev)
140                 return -EINVAL;
141
142         if (odev->busid < 0 || odev->busid >= OMAP_NR_BUSES) {
143                 printk(KERN_ERR "%s: busid invalid: %s: bus: %i\n",
144                        __FUNCTION__, odev->name, odev->busid);
145                 return -EINVAL;
146         }
147
148         odev->dev.parent = &omap_bus_devices[odev->busid];
149         odev->dev.bus = &omap_bus_types[odev->busid];
150
151         /* This is needed for USB OHCI to work */
152         if (odev->dma_mask)
153                 odev->dev.dma_mask = odev->dma_mask;
154
155         if (odev->coherent_dma_mask)
156                 odev->dev.coherent_dma_mask = odev->coherent_dma_mask;
157
158         snprintf(odev->dev.bus_id, BUS_ID_SIZE, "%s%u",
159                  odev->name, odev->devid);
160
161         printk("Registering OMAP device '%s'. Parent at %s\n",
162                  odev->dev.bus_id, odev->dev.parent->bus_id);
163
164         return device_register(&odev->dev);
165 }
166
167 void omap_device_unregister(struct omap_dev *odev)
168 {
169         if (odev)
170                 device_unregister(&odev->dev);
171 }
172
173 int omap_driver_register(struct omap_driver *driver)
174 {
175         int ret;
176
177         if (driver->busid < 0 || driver->busid >= OMAP_NR_BUSES) {
178                 printk(KERN_ERR "%s: busid invalid: bus: %i device: %i\n",
179                        __FUNCTION__, driver->busid, driver->devid);
180                 return -EINVAL;
181         }
182
183         driver->drv.probe = omap_device_probe;
184         driver->drv.remove = omap_device_remove;
185         driver->drv.bus = &omap_bus_types[driver->busid];
186
187         /*
188          * driver_register calls bus_add_driver
189          */
190         ret = driver_register(&driver->drv);
191
192         return ret;
193 }
194
195 void omap_driver_unregister(struct omap_driver *driver)
196 {
197         driver_unregister(&driver->drv);
198 }
199
200 static int __init omap_bus_init(void)
201 {
202         int i, ret;
203
204         /* Initialize all OMAP virtual buses */
205         for (i = 0; i < OMAP_NR_BUSES; i++) {
206                 ret = device_register(&omap_bus_devices[i]);
207                 if (ret != 0) {
208                         printk(KERN_ERR "Unable to register bus device %s\n",
209                                omap_bus_devices[i].bus_id);
210                         continue;
211                 }
212                 ret = bus_register(&omap_bus_types[i]);
213                 if (ret != 0) {
214                         printk(KERN_ERR "Unable to register bus %s\n",
215                                omap_bus_types[i].name);
216                         device_unregister(&omap_bus_devices[i]);
217                 }
218         }
219         printk("OMAP virtual buses initialized\n");
220
221         return ret;
222 }
223
224 static void __exit omap_bus_exit(void)
225 {
226         int i;
227
228         /* Unregister all OMAP virtual buses */
229         for (i = 0; i < OMAP_NR_BUSES; i++) {
230                 bus_unregister(&omap_bus_types[i]);
231                 device_unregister(&omap_bus_devices[i]);
232         }
233 }
234
235 postcore_initcall(omap_bus_init);
236 module_exit(omap_bus_exit);
237
238 MODULE_DESCRIPTION("Virtual bus for OMAP");
239 MODULE_LICENSE("GPL");
240
241 EXPORT_SYMBOL(omap_bus_types);
242 EXPORT_SYMBOL(omap_driver_register);
243 EXPORT_SYMBOL(omap_driver_unregister);
244 EXPORT_SYMBOL(omap_device_register);
245 EXPORT_SYMBOL(omap_device_unregister);
246