patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / 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 #ifdef CONFIG_ARCH_OMAP1510
85 /*
86  * NOTE: This code _should_ go somewhere else. But let's wait for the
87  *       dma-mapping code to settle down first.
88  */
89
90 /*
91  * Test for Local Bus device in order to do address translation between
92  * dma_handle and Local Bus address.
93  */
94 inline int dmadev_uses_omap_lbus(struct device * dev)
95 {
96         if (dev == NULL || !cpu_is_omap1510())
97                 return 0;
98         return dev->bus == &omap_bus_types[OMAP_BUS_LBUS] ? 1 : 0;
99 }
100
101 /*
102  * Translate bus address to Local Bus address for dma-mapping
103  */
104 inline int dmadev_to_lbus(dma_addr_t addr)
105 {
106         return bus_to_lbus(addr);
107 }
108
109 /*
110  * Translate Local Bus address to bus address for dma-mapping
111  */
112 inline int lbus_to_dmadev(dma_addr_t addr)
113 {
114         return lbus_to_bus(addr);
115 }
116 #endif
117
118 static int omap_bus_match(struct device *dev, struct device_driver *drv)
119 {
120         struct omap_dev *omapdev = OMAP_DEV(dev);
121         struct omap_driver *omapdrv = OMAP_DRV(drv);
122
123         return omapdev->devid == omapdrv->devid;
124 }
125
126 static int omap_bus_suspend(struct device *dev, u32 state)
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->suspend)
133                 ret = omapdrv->suspend(omapdev, state);
134         return ret;
135 }
136
137 static int omap_bus_resume(struct device *dev)
138 {
139         struct omap_dev *omapdev = OMAP_DEV(dev);
140         struct omap_driver *omapdrv = OMAP_DRV(dev->driver);
141         int ret = 0;
142
143         if (omapdrv && omapdrv->resume)
144                 ret = omapdrv->resume(omapdev);
145         return ret;
146 }
147
148 static int omap_device_probe(struct device *dev)
149 {
150         struct omap_dev *omapdev = OMAP_DEV(dev);
151         struct omap_driver *omapdrv = OMAP_DRV(dev->driver);
152         int ret = -ENODEV;
153
154         if (omapdrv && omapdrv->probe)
155                 ret = omapdrv->probe(omapdev);
156
157         return ret;
158 }
159
160 static int omap_device_remove(struct device *dev)
161 {
162         struct omap_dev *omapdev = OMAP_DEV(dev);
163         struct omap_driver *omapdrv = OMAP_DRV(dev->driver);
164         int ret = 0;
165
166         if (omapdrv && omapdrv->remove)
167                 ret = omapdrv->remove(omapdev);
168         return ret;
169 }
170
171 int omap_device_register(struct omap_dev *odev)
172 {
173         if (!odev)
174                 return -EINVAL;
175
176         if (odev->busid < 0 || odev->busid >= OMAP_NR_BUSES) {
177                 printk(KERN_ERR "%s: busid invalid: %s: bus: %i\n",
178                        __FUNCTION__, odev->name, odev->busid);
179                 return -EINVAL;
180         }
181
182         odev->dev.parent = &omap_bus_devices[odev->busid];
183         odev->dev.bus = &omap_bus_types[odev->busid];
184
185         /* This is needed for USB OHCI to work */
186         if (odev->dma_mask)
187                 odev->dev.dma_mask = odev->dma_mask;
188
189         if (odev->coherent_dma_mask)
190                 odev->dev.coherent_dma_mask = odev->coherent_dma_mask;
191
192         snprintf(odev->dev.bus_id, BUS_ID_SIZE, "%s%u",
193                  odev->name, odev->devid);
194
195         printk("Registering OMAP device '%s'. Parent at %s\n",
196                  odev->dev.bus_id, odev->dev.parent->bus_id);
197
198         return device_register(&odev->dev);
199 }
200
201 void omap_device_unregister(struct omap_dev *odev)
202 {
203         if (odev)
204                 device_unregister(&odev->dev);
205 }
206
207 int omap_driver_register(struct omap_driver *driver)
208 {
209         int ret;
210
211         if (driver->busid < 0 || driver->busid >= OMAP_NR_BUSES) {
212                 printk(KERN_ERR "%s: busid invalid: bus: %i device: %i\n",
213                        __FUNCTION__, driver->busid, driver->devid);
214                 return -EINVAL;
215         }
216
217         driver->drv.probe = omap_device_probe;
218         driver->drv.remove = omap_device_remove;
219         driver->drv.bus = &omap_bus_types[driver->busid];
220
221         /*
222          * driver_register calls bus_add_driver
223          */
224         ret = driver_register(&driver->drv);
225
226         return ret;
227 }
228
229 void omap_driver_unregister(struct omap_driver *driver)
230 {
231         driver_unregister(&driver->drv);
232 }
233
234 static int __init omap_bus_init(void)
235 {
236         int i, ret;
237
238         /* Initialize all OMAP virtual buses */
239         for (i = 0; i < OMAP_NR_BUSES; i++) {
240                 ret = device_register(&omap_bus_devices[i]);
241                 if (ret != 0) {
242                         printk(KERN_ERR "Unable to register bus device %s\n",
243                                omap_bus_devices[i].bus_id);
244                         continue;
245                 }
246                 ret = bus_register(&omap_bus_types[i]);
247                 if (ret != 0) {
248                         printk(KERN_ERR "Unable to register bus %s\n",
249                                omap_bus_types[i].name);
250                         device_unregister(&omap_bus_devices[i]);
251                 }
252         }
253         printk("OMAP virtual buses initialized\n");
254
255         return ret;
256 }
257
258 static void __exit omap_bus_exit(void)
259 {
260         int i;
261
262         /* Unregister all OMAP virtual buses */
263         for (i = 0; i < OMAP_NR_BUSES; i++) {
264                 bus_unregister(&omap_bus_types[i]);
265                 device_unregister(&omap_bus_devices[i]);
266         }
267 }
268
269 postcore_initcall(omap_bus_init);
270 module_exit(omap_bus_exit);
271
272 MODULE_DESCRIPTION("Virtual bus for OMAP");
273 MODULE_LICENSE("GPL");
274
275 EXPORT_SYMBOL(omap_bus_types);
276 EXPORT_SYMBOL(omap_driver_register);
277 EXPORT_SYMBOL(omap_driver_unregister);
278 EXPORT_SYMBOL(omap_device_register);
279 EXPORT_SYMBOL(omap_device_unregister);
280
281 #ifdef CONFIG_ARCH_OMAP1510
282 EXPORT_SYMBOL(dmadev_uses_omap_lbus);
283 EXPORT_SYMBOL(dmadev_to_lbus);
284 EXPORT_SYMBOL(lbus_to_dmadev);
285 #endif