workqueues: introduce __cancel_delayed_work()
[linux-2.6/mini2440.git] / drivers / amba / bus.c
blob246650673010594d08c139551da1973318f78685
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/io.h>
16 #include <linux/amba/bus.h>
18 #include <asm/irq.h>
19 #include <asm/sizes.h>
21 #define to_amba_device(d) container_of(d, struct amba_device, dev)
22 #define to_amba_driver(d) container_of(d, struct amba_driver, drv)
24 static struct amba_id *
25 amba_lookup(struct amba_id *table, struct amba_device *dev)
27 int ret = 0;
29 while (table->mask) {
30 ret = (dev->periphid & table->mask) == table->id;
31 if (ret)
32 break;
33 table++;
36 return ret ? table : NULL;
39 static int amba_match(struct device *dev, struct device_driver *drv)
41 struct amba_device *pcdev = to_amba_device(dev);
42 struct amba_driver *pcdrv = to_amba_driver(drv);
44 return amba_lookup(pcdrv->id_table, pcdev) != NULL;
47 #ifdef CONFIG_HOTPLUG
48 static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
50 struct amba_device *pcdev = to_amba_device(dev);
51 int retval = 0;
53 retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
54 return retval;
56 #else
57 #define amba_uevent NULL
58 #endif
60 static int amba_suspend(struct device *dev, pm_message_t state)
62 struct amba_driver *drv = to_amba_driver(dev->driver);
63 int ret = 0;
65 if (dev->driver && drv->suspend)
66 ret = drv->suspend(to_amba_device(dev), state);
67 return ret;
70 static int amba_resume(struct device *dev)
72 struct amba_driver *drv = to_amba_driver(dev->driver);
73 int ret = 0;
75 if (dev->driver && drv->resume)
76 ret = drv->resume(to_amba_device(dev));
77 return ret;
80 #define amba_attr_func(name,fmt,arg...) \
81 static ssize_t name##_show(struct device *_dev, \
82 struct device_attribute *attr, char *buf) \
83 { \
84 struct amba_device *dev = to_amba_device(_dev); \
85 return sprintf(buf, fmt, arg); \
88 #define amba_attr(name,fmt,arg...) \
89 amba_attr_func(name,fmt,arg) \
90 static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL)
92 amba_attr_func(id, "%08x\n", dev->periphid);
93 amba_attr(irq0, "%u\n", dev->irq[0]);
94 amba_attr(irq1, "%u\n", dev->irq[1]);
95 amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",
96 (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
97 dev->res.flags);
99 static struct device_attribute amba_dev_attrs[] = {
100 __ATTR_RO(id),
101 __ATTR_RO(resource),
102 __ATTR_NULL,
106 * Primecells are part of the Advanced Microcontroller Bus Architecture,
107 * so we call the bus "amba".
109 static struct bus_type amba_bustype = {
110 .name = "amba",
111 .dev_attrs = amba_dev_attrs,
112 .match = amba_match,
113 .uevent = amba_uevent,
114 .suspend = amba_suspend,
115 .resume = amba_resume,
118 static int __init amba_init(void)
120 return bus_register(&amba_bustype);
123 postcore_initcall(amba_init);
126 * These are the device model conversion veneers; they convert the
127 * device model structures to our more specific structures.
129 static int amba_probe(struct device *dev)
131 struct amba_device *pcdev = to_amba_device(dev);
132 struct amba_driver *pcdrv = to_amba_driver(dev->driver);
133 struct amba_id *id;
135 id = amba_lookup(pcdrv->id_table, pcdev);
137 return pcdrv->probe(pcdev, id);
140 static int amba_remove(struct device *dev)
142 struct amba_driver *drv = to_amba_driver(dev->driver);
143 return drv->remove(to_amba_device(dev));
146 static void amba_shutdown(struct device *dev)
148 struct amba_driver *drv = to_amba_driver(dev->driver);
149 drv->shutdown(to_amba_device(dev));
153 * amba_driver_register - register an AMBA device driver
154 * @drv: amba device driver structure
156 * Register an AMBA device driver with the Linux device model
157 * core. If devices pre-exist, the drivers probe function will
158 * be called.
160 int amba_driver_register(struct amba_driver *drv)
162 drv->drv.bus = &amba_bustype;
164 #define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
165 SETFN(probe);
166 SETFN(remove);
167 SETFN(shutdown);
169 return driver_register(&drv->drv);
173 * amba_driver_unregister - remove an AMBA device driver
174 * @drv: AMBA device driver structure to remove
176 * Unregister an AMBA device driver from the Linux device
177 * model. The device model will call the drivers remove function
178 * for each device the device driver is currently handling.
180 void amba_driver_unregister(struct amba_driver *drv)
182 driver_unregister(&drv->drv);
186 static void amba_device_release(struct device *dev)
188 struct amba_device *d = to_amba_device(dev);
190 if (d->res.parent)
191 release_resource(&d->res);
192 kfree(d);
196 * amba_device_register - register an AMBA device
197 * @dev: AMBA device to register
198 * @parent: parent memory resource
200 * Setup the AMBA device, reading the cell ID if present.
201 * Claim the resource, and register the AMBA device with
202 * the Linux device manager.
204 int amba_device_register(struct amba_device *dev, struct resource *parent)
206 u32 pid, cid;
207 void __iomem *tmp;
208 int i, ret;
210 device_initialize(&dev->dev);
213 * Copy from device_add
215 if (dev->dev.init_name) {
216 dev_set_name(&dev->dev, "%s", dev->dev.init_name);
217 dev->dev.init_name = NULL;
220 dev->dev.release = amba_device_release;
221 dev->dev.bus = &amba_bustype;
222 dev->dev.dma_mask = &dev->dma_mask;
223 dev->res.name = dev_name(&dev->dev);
225 if (!dev->dev.coherent_dma_mask && dev->dma_mask)
226 dev_warn(&dev->dev, "coherent dma mask is unset\n");
228 ret = request_resource(parent, &dev->res);
229 if (ret)
230 goto err_out;
232 tmp = ioremap(dev->res.start, SZ_4K);
233 if (!tmp) {
234 ret = -ENOMEM;
235 goto err_release;
238 for (pid = 0, i = 0; i < 4; i++)
239 pid |= (readl(tmp + 0xfe0 + 4 * i) & 255) << (i * 8);
240 for (cid = 0, i = 0; i < 4; i++)
241 cid |= (readl(tmp + 0xff0 + 4 * i) & 255) << (i * 8);
243 iounmap(tmp);
245 if (cid == 0xb105f00d)
246 dev->periphid = pid;
248 if (!dev->periphid) {
249 ret = -ENODEV;
250 goto err_release;
253 ret = device_add(&dev->dev);
254 if (ret)
255 goto err_release;
257 if (dev->irq[0] != NO_IRQ)
258 ret = device_create_file(&dev->dev, &dev_attr_irq0);
259 if (ret == 0 && dev->irq[1] != NO_IRQ)
260 ret = device_create_file(&dev->dev, &dev_attr_irq1);
261 if (ret == 0)
262 return ret;
264 device_unregister(&dev->dev);
266 err_release:
267 release_resource(&dev->res);
268 err_out:
269 return ret;
273 * amba_device_unregister - unregister an AMBA device
274 * @dev: AMBA device to remove
276 * Remove the specified AMBA device from the Linux device
277 * manager. All files associated with this object will be
278 * destroyed, and device drivers notified that the device has
279 * been removed. The AMBA device's resources including
280 * the amba_device structure will be freed once all
281 * references to it have been dropped.
283 void amba_device_unregister(struct amba_device *dev)
285 device_unregister(&dev->dev);
289 struct find_data {
290 struct amba_device *dev;
291 struct device *parent;
292 const char *busid;
293 unsigned int id;
294 unsigned int mask;
297 static int amba_find_match(struct device *dev, void *data)
299 struct find_data *d = data;
300 struct amba_device *pcdev = to_amba_device(dev);
301 int r;
303 r = (pcdev->periphid & d->mask) == d->id;
304 if (d->parent)
305 r &= d->parent == dev->parent;
306 if (d->busid)
307 r &= strcmp(dev_name(dev), d->busid) == 0;
309 if (r) {
310 get_device(dev);
311 d->dev = pcdev;
314 return r;
318 * amba_find_device - locate an AMBA device given a bus id
319 * @busid: bus id for device (or NULL)
320 * @parent: parent device (or NULL)
321 * @id: peripheral ID (or 0)
322 * @mask: peripheral ID mask (or 0)
324 * Return the AMBA device corresponding to the supplied parameters.
325 * If no device matches, returns NULL.
327 * NOTE: When a valid device is found, its refcount is
328 * incremented, and must be decremented before the returned
329 * reference.
331 struct amba_device *
332 amba_find_device(const char *busid, struct device *parent, unsigned int id,
333 unsigned int mask)
335 struct find_data data;
337 data.dev = NULL;
338 data.parent = parent;
339 data.busid = busid;
340 data.id = id;
341 data.mask = mask;
343 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
345 return data.dev;
349 * amba_request_regions - request all mem regions associated with device
350 * @dev: amba_device structure for device
351 * @name: name, or NULL to use driver name
353 int amba_request_regions(struct amba_device *dev, const char *name)
355 int ret = 0;
357 if (!name)
358 name = dev->dev.driver->name;
360 if (!request_mem_region(dev->res.start, SZ_4K, name))
361 ret = -EBUSY;
363 return ret;
367 * amba_release_regions - release mem regions assoicated with device
368 * @dev: amba_device structure for device
370 * Release regions claimed by a successful call to amba_request_regions.
372 void amba_release_regions(struct amba_device *dev)
374 release_mem_region(dev->res.start, SZ_4K);
377 EXPORT_SYMBOL(amba_driver_register);
378 EXPORT_SYMBOL(amba_driver_unregister);
379 EXPORT_SYMBOL(amba_device_register);
380 EXPORT_SYMBOL(amba_device_unregister);
381 EXPORT_SYMBOL(amba_find_device);
382 EXPORT_SYMBOL(amba_request_regions);
383 EXPORT_SYMBOL(amba_release_regions);