[PATCH] hrtimers: fix HRTIMER_CB_IRQSAFE_NO_SOFTIRQ description
[wrt350n-kernel.git] / drivers / amba / bus.c
blobfd5475071accea2d48ff5ec7f15270d61409a27b
1 /*
2 * linux/arch/arm/common/amba.c
4 * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/device.h>
13 #include <linux/string.h>
14 #include <linux/slab.h>
15 #include <linux/amba/bus.h>
17 #include <asm/io.h>
18 #include <asm/sizes.h>
20 #define to_amba_device(d) container_of(d, struct amba_device, dev)
21 #define to_amba_driver(d) container_of(d, struct amba_driver, drv)
23 static struct amba_id *
24 amba_lookup(struct amba_id *table, struct amba_device *dev)
26 int ret = 0;
28 while (table->mask) {
29 ret = (dev->periphid & table->mask) == table->id;
30 if (ret)
31 break;
32 table++;
35 return ret ? table : NULL;
38 static int amba_match(struct device *dev, struct device_driver *drv)
40 struct amba_device *pcdev = to_amba_device(dev);
41 struct amba_driver *pcdrv = to_amba_driver(drv);
43 return amba_lookup(pcdrv->id_table, pcdev) != NULL;
46 #ifdef CONFIG_HOTPLUG
47 static int amba_uevent(struct device *dev, char **envp, int nr_env, char *buf, int bufsz)
49 struct amba_device *pcdev = to_amba_device(dev);
51 if (nr_env < 2)
52 return -ENOMEM;
54 snprintf(buf, bufsz, "AMBA_ID=%08x", pcdev->periphid);
55 *envp++ = buf;
56 *envp++ = NULL;
57 return 0;
59 #else
60 #define amba_uevent NULL
61 #endif
63 static int amba_suspend(struct device *dev, pm_message_t state)
65 struct amba_driver *drv = to_amba_driver(dev->driver);
66 int ret = 0;
68 if (dev->driver && drv->suspend)
69 ret = drv->suspend(to_amba_device(dev), state);
70 return ret;
73 static int amba_resume(struct device *dev)
75 struct amba_driver *drv = to_amba_driver(dev->driver);
76 int ret = 0;
78 if (dev->driver && drv->resume)
79 ret = drv->resume(to_amba_device(dev));
80 return ret;
83 #define amba_attr_func(name,fmt,arg...) \
84 static ssize_t name##_show(struct device *_dev, \
85 struct device_attribute *attr, char *buf) \
86 { \
87 struct amba_device *dev = to_amba_device(_dev); \
88 return sprintf(buf, fmt, arg); \
91 #define amba_attr(name,fmt,arg...) \
92 amba_attr_func(name,fmt,arg) \
93 static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL)
95 amba_attr_func(id, "%08x\n", dev->periphid);
96 amba_attr(irq0, "%u\n", dev->irq[0]);
97 amba_attr(irq1, "%u\n", dev->irq[1]);
98 amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",
99 (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
100 dev->res.flags);
102 static struct device_attribute amba_dev_attrs[] = {
103 __ATTR_RO(id),
104 __ATTR_RO(resource),
105 __ATTR_NULL,
109 * Primecells are part of the Advanced Microcontroller Bus Architecture,
110 * so we call the bus "amba".
112 static struct bus_type amba_bustype = {
113 .name = "amba",
114 .dev_attrs = amba_dev_attrs,
115 .match = amba_match,
116 .uevent = amba_uevent,
117 .suspend = amba_suspend,
118 .resume = amba_resume,
121 static int __init amba_init(void)
123 return bus_register(&amba_bustype);
126 postcore_initcall(amba_init);
129 * These are the device model conversion veneers; they convert the
130 * device model structures to our more specific structures.
132 static int amba_probe(struct device *dev)
134 struct amba_device *pcdev = to_amba_device(dev);
135 struct amba_driver *pcdrv = to_amba_driver(dev->driver);
136 struct amba_id *id;
138 id = amba_lookup(pcdrv->id_table, pcdev);
140 return pcdrv->probe(pcdev, id);
143 static int amba_remove(struct device *dev)
145 struct amba_driver *drv = to_amba_driver(dev->driver);
146 return drv->remove(to_amba_device(dev));
149 static void amba_shutdown(struct device *dev)
151 struct amba_driver *drv = to_amba_driver(dev->driver);
152 drv->shutdown(to_amba_device(dev));
156 * amba_driver_register - register an AMBA device driver
157 * @drv: amba device driver structure
159 * Register an AMBA device driver with the Linux device model
160 * core. If devices pre-exist, the drivers probe function will
161 * be called.
163 int amba_driver_register(struct amba_driver *drv)
165 drv->drv.bus = &amba_bustype;
167 #define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
168 SETFN(probe);
169 SETFN(remove);
170 SETFN(shutdown);
172 return driver_register(&drv->drv);
176 * amba_driver_unregister - remove an AMBA device driver
177 * @drv: AMBA device driver structure to remove
179 * Unregister an AMBA device driver from the Linux device
180 * model. The device model will call the drivers remove function
181 * for each device the device driver is currently handling.
183 void amba_driver_unregister(struct amba_driver *drv)
185 driver_unregister(&drv->drv);
189 static void amba_device_release(struct device *dev)
191 struct amba_device *d = to_amba_device(dev);
193 if (d->res.parent)
194 release_resource(&d->res);
195 kfree(d);
199 * amba_device_register - register an AMBA device
200 * @dev: AMBA device to register
201 * @parent: parent memory resource
203 * Setup the AMBA device, reading the cell ID if present.
204 * Claim the resource, and register the AMBA device with
205 * the Linux device manager.
207 int amba_device_register(struct amba_device *dev, struct resource *parent)
209 u32 pid, cid;
210 void __iomem *tmp;
211 int i, ret;
213 dev->dev.release = amba_device_release;
214 dev->dev.bus = &amba_bustype;
215 dev->dev.dma_mask = &dev->dma_mask;
216 dev->res.name = dev->dev.bus_id;
218 if (!dev->dev.coherent_dma_mask && dev->dma_mask)
219 dev_warn(&dev->dev, "coherent dma mask is unset\n");
221 ret = request_resource(parent, &dev->res);
222 if (ret)
223 goto err_out;
225 tmp = ioremap(dev->res.start, SZ_4K);
226 if (!tmp) {
227 ret = -ENOMEM;
228 goto err_release;
231 for (pid = 0, i = 0; i < 4; i++)
232 pid |= (readl(tmp + 0xfe0 + 4 * i) & 255) << (i * 8);
233 for (cid = 0, i = 0; i < 4; i++)
234 cid |= (readl(tmp + 0xff0 + 4 * i) & 255) << (i * 8);
236 iounmap(tmp);
238 if (cid == 0xb105f00d)
239 dev->periphid = pid;
241 if (!dev->periphid) {
242 ret = -ENODEV;
243 goto err_release;
246 ret = device_register(&dev->dev);
247 if (ret)
248 goto err_release;
250 if (dev->irq[0] != NO_IRQ)
251 ret = device_create_file(&dev->dev, &dev_attr_irq0);
252 if (ret == 0 && dev->irq[1] != NO_IRQ)
253 ret = device_create_file(&dev->dev, &dev_attr_irq1);
254 if (ret == 0)
255 return ret;
257 device_unregister(&dev->dev);
259 err_release:
260 release_resource(&dev->res);
261 err_out:
262 return ret;
266 * amba_device_unregister - unregister an AMBA device
267 * @dev: AMBA device to remove
269 * Remove the specified AMBA device from the Linux device
270 * manager. All files associated with this object will be
271 * destroyed, and device drivers notified that the device has
272 * been removed. The AMBA device's resources including
273 * the amba_device structure will be freed once all
274 * references to it have been dropped.
276 void amba_device_unregister(struct amba_device *dev)
278 device_unregister(&dev->dev);
282 struct find_data {
283 struct amba_device *dev;
284 struct device *parent;
285 const char *busid;
286 unsigned int id;
287 unsigned int mask;
290 static int amba_find_match(struct device *dev, void *data)
292 struct find_data *d = data;
293 struct amba_device *pcdev = to_amba_device(dev);
294 int r;
296 r = (pcdev->periphid & d->mask) == d->id;
297 if (d->parent)
298 r &= d->parent == dev->parent;
299 if (d->busid)
300 r &= strcmp(dev->bus_id, d->busid) == 0;
302 if (r) {
303 get_device(dev);
304 d->dev = pcdev;
307 return r;
311 * amba_find_device - locate an AMBA device given a bus id
312 * @busid: bus id for device (or NULL)
313 * @parent: parent device (or NULL)
314 * @id: peripheral ID (or 0)
315 * @mask: peripheral ID mask (or 0)
317 * Return the AMBA device corresponding to the supplied parameters.
318 * If no device matches, returns NULL.
320 * NOTE: When a valid device is found, its refcount is
321 * incremented, and must be decremented before the returned
322 * reference.
324 struct amba_device *
325 amba_find_device(const char *busid, struct device *parent, unsigned int id,
326 unsigned int mask)
328 struct find_data data;
330 data.dev = NULL;
331 data.parent = parent;
332 data.busid = busid;
333 data.id = id;
334 data.mask = mask;
336 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
338 return data.dev;
342 * amba_request_regions - request all mem regions associated with device
343 * @dev: amba_device structure for device
344 * @name: name, or NULL to use driver name
346 int amba_request_regions(struct amba_device *dev, const char *name)
348 int ret = 0;
350 if (!name)
351 name = dev->dev.driver->name;
353 if (!request_mem_region(dev->res.start, SZ_4K, name))
354 ret = -EBUSY;
356 return ret;
360 * amba_release_regions - release mem regions assoicated with device
361 * @dev: amba_device structure for device
363 * Release regions claimed by a successful call to amba_request_regions.
365 void amba_release_regions(struct amba_device *dev)
367 release_mem_region(dev->res.start, SZ_4K);
370 EXPORT_SYMBOL(amba_driver_register);
371 EXPORT_SYMBOL(amba_driver_unregister);
372 EXPORT_SYMBOL(amba_device_register);
373 EXPORT_SYMBOL(amba_device_unregister);
374 EXPORT_SYMBOL(amba_find_device);
375 EXPORT_SYMBOL(amba_request_regions);
376 EXPORT_SYMBOL(amba_release_regions);