Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/x86
[deliverable/linux.git] / drivers / amba / bus.c
1 /*
2 * linux/arch/arm/common/amba.c
3 *
4 * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
5 *
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>
16
17 #include <asm/io.h>
18 #include <asm/sizes.h>
19
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)
22
23 static struct amba_id *
24 amba_lookup(struct amba_id *table, struct amba_device *dev)
25 {
26 int ret = 0;
27
28 while (table->mask) {
29 ret = (dev->periphid & table->mask) == table->id;
30 if (ret)
31 break;
32 table++;
33 }
34
35 return ret ? table : NULL;
36 }
37
38 static int amba_match(struct device *dev, struct device_driver *drv)
39 {
40 struct amba_device *pcdev = to_amba_device(dev);
41 struct amba_driver *pcdrv = to_amba_driver(drv);
42
43 return amba_lookup(pcdrv->id_table, pcdev) != NULL;
44 }
45
46 #ifdef CONFIG_HOTPLUG
47 static int amba_uevent(struct device *dev, char **envp, int nr_env, char *buf, int bufsz)
48 {
49 struct amba_device *pcdev = to_amba_device(dev);
50
51 if (nr_env < 2)
52 return -ENOMEM;
53
54 snprintf(buf, bufsz, "AMBA_ID=%08x", pcdev->periphid);
55 *envp++ = buf;
56 *envp++ = NULL;
57 return 0;
58 }
59 #else
60 #define amba_uevent NULL
61 #endif
62
63 static int amba_suspend(struct device *dev, pm_message_t state)
64 {
65 struct amba_driver *drv = to_amba_driver(dev->driver);
66 int ret = 0;
67
68 if (dev->driver && drv->suspend)
69 ret = drv->suspend(to_amba_device(dev), state);
70 return ret;
71 }
72
73 static int amba_resume(struct device *dev)
74 {
75 struct amba_driver *drv = to_amba_driver(dev->driver);
76 int ret = 0;
77
78 if (dev->driver && drv->resume)
79 ret = drv->resume(to_amba_device(dev));
80 return ret;
81 }
82
83 /*
84 * Primecells are part of the Advanced Microcontroller Bus Architecture,
85 * so we call the bus "amba".
86 */
87 static struct bus_type amba_bustype = {
88 .name = "amba",
89 .match = amba_match,
90 .uevent = amba_uevent,
91 .suspend = amba_suspend,
92 .resume = amba_resume,
93 };
94
95 static int __init amba_init(void)
96 {
97 return bus_register(&amba_bustype);
98 }
99
100 postcore_initcall(amba_init);
101
102 /*
103 * These are the device model conversion veneers; they convert the
104 * device model structures to our more specific structures.
105 */
106 static int amba_probe(struct device *dev)
107 {
108 struct amba_device *pcdev = to_amba_device(dev);
109 struct amba_driver *pcdrv = to_amba_driver(dev->driver);
110 struct amba_id *id;
111
112 id = amba_lookup(pcdrv->id_table, pcdev);
113
114 return pcdrv->probe(pcdev, id);
115 }
116
117 static int amba_remove(struct device *dev)
118 {
119 struct amba_driver *drv = to_amba_driver(dev->driver);
120 return drv->remove(to_amba_device(dev));
121 }
122
123 static void amba_shutdown(struct device *dev)
124 {
125 struct amba_driver *drv = to_amba_driver(dev->driver);
126 drv->shutdown(to_amba_device(dev));
127 }
128
129 /**
130 * amba_driver_register - register an AMBA device driver
131 * @drv: amba device driver structure
132 *
133 * Register an AMBA device driver with the Linux device model
134 * core. If devices pre-exist, the drivers probe function will
135 * be called.
136 */
137 int amba_driver_register(struct amba_driver *drv)
138 {
139 drv->drv.bus = &amba_bustype;
140
141 #define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
142 SETFN(probe);
143 SETFN(remove);
144 SETFN(shutdown);
145
146 return driver_register(&drv->drv);
147 }
148
149 /**
150 * amba_driver_unregister - remove an AMBA device driver
151 * @drv: AMBA device driver structure to remove
152 *
153 * Unregister an AMBA device driver from the Linux device
154 * model. The device model will call the drivers remove function
155 * for each device the device driver is currently handling.
156 */
157 void amba_driver_unregister(struct amba_driver *drv)
158 {
159 driver_unregister(&drv->drv);
160 }
161
162
163 static void amba_device_release(struct device *dev)
164 {
165 struct amba_device *d = to_amba_device(dev);
166
167 if (d->res.parent)
168 release_resource(&d->res);
169 kfree(d);
170 }
171
172 #define amba_attr(name,fmt,arg...) \
173 static ssize_t show_##name(struct device *_dev, struct device_attribute *attr, char *buf) \
174 { \
175 struct amba_device *dev = to_amba_device(_dev); \
176 return sprintf(buf, fmt, arg); \
177 } \
178 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
179
180 amba_attr(id, "%08x\n", dev->periphid);
181 amba_attr(irq0, "%u\n", dev->irq[0]);
182 amba_attr(irq1, "%u\n", dev->irq[1]);
183 amba_attr(resource, "\t%08lx\t%08lx\t%08lx\n",
184 dev->res.start, dev->res.end, dev->res.flags);
185
186 /**
187 * amba_device_register - register an AMBA device
188 * @dev: AMBA device to register
189 * @parent: parent memory resource
190 *
191 * Setup the AMBA device, reading the cell ID if present.
192 * Claim the resource, and register the AMBA device with
193 * the Linux device manager.
194 */
195 int amba_device_register(struct amba_device *dev, struct resource *parent)
196 {
197 u32 pid, cid;
198 void __iomem *tmp;
199 int i, ret;
200
201 dev->dev.release = amba_device_release;
202 dev->dev.bus = &amba_bustype;
203 dev->dev.dma_mask = &dev->dma_mask;
204 dev->res.name = dev->dev.bus_id;
205
206 if (!dev->dev.coherent_dma_mask && dev->dma_mask)
207 dev_warn(&dev->dev, "coherent dma mask is unset\n");
208
209 ret = request_resource(parent, &dev->res);
210 if (ret == 0) {
211 tmp = ioremap(dev->res.start, SZ_4K);
212 if (!tmp) {
213 ret = -ENOMEM;
214 goto out;
215 }
216
217 for (pid = 0, i = 0; i < 4; i++)
218 pid |= (readl(tmp + 0xfe0 + 4 * i) & 255) << (i * 8);
219 for (cid = 0, i = 0; i < 4; i++)
220 cid |= (readl(tmp + 0xff0 + 4 * i) & 255) << (i * 8);
221
222 iounmap(tmp);
223
224 if (cid == 0xb105f00d)
225 dev->periphid = pid;
226
227 if (dev->periphid)
228 ret = device_register(&dev->dev);
229 else
230 ret = -ENODEV;
231
232 if (ret == 0) {
233 device_create_file(&dev->dev, &dev_attr_id);
234 if (dev->irq[0] != NO_IRQ)
235 device_create_file(&dev->dev, &dev_attr_irq0);
236 if (dev->irq[1] != NO_IRQ)
237 device_create_file(&dev->dev, &dev_attr_irq1);
238 device_create_file(&dev->dev, &dev_attr_resource);
239 } else {
240 out:
241 release_resource(&dev->res);
242 }
243 }
244 return ret;
245 }
246
247 /**
248 * amba_device_unregister - unregister an AMBA device
249 * @dev: AMBA device to remove
250 *
251 * Remove the specified AMBA device from the Linux device
252 * manager. All files associated with this object will be
253 * destroyed, and device drivers notified that the device has
254 * been removed. The AMBA device's resources including
255 * the amba_device structure will be freed once all
256 * references to it have been dropped.
257 */
258 void amba_device_unregister(struct amba_device *dev)
259 {
260 device_unregister(&dev->dev);
261 }
262
263
264 struct find_data {
265 struct amba_device *dev;
266 struct device *parent;
267 const char *busid;
268 unsigned int id;
269 unsigned int mask;
270 };
271
272 static int amba_find_match(struct device *dev, void *data)
273 {
274 struct find_data *d = data;
275 struct amba_device *pcdev = to_amba_device(dev);
276 int r;
277
278 r = (pcdev->periphid & d->mask) == d->id;
279 if (d->parent)
280 r &= d->parent == dev->parent;
281 if (d->busid)
282 r &= strcmp(dev->bus_id, d->busid) == 0;
283
284 if (r) {
285 get_device(dev);
286 d->dev = pcdev;
287 }
288
289 return r;
290 }
291
292 /**
293 * amba_find_device - locate an AMBA device given a bus id
294 * @busid: bus id for device (or NULL)
295 * @parent: parent device (or NULL)
296 * @id: peripheral ID (or 0)
297 * @mask: peripheral ID mask (or 0)
298 *
299 * Return the AMBA device corresponding to the supplied parameters.
300 * If no device matches, returns NULL.
301 *
302 * NOTE: When a valid device is found, its refcount is
303 * incremented, and must be decremented before the returned
304 * reference.
305 */
306 struct amba_device *
307 amba_find_device(const char *busid, struct device *parent, unsigned int id,
308 unsigned int mask)
309 {
310 struct find_data data;
311
312 data.dev = NULL;
313 data.parent = parent;
314 data.busid = busid;
315 data.id = id;
316 data.mask = mask;
317
318 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
319
320 return data.dev;
321 }
322
323 /**
324 * amba_request_regions - request all mem regions associated with device
325 * @dev: amba_device structure for device
326 * @name: name, or NULL to use driver name
327 */
328 int amba_request_regions(struct amba_device *dev, const char *name)
329 {
330 int ret = 0;
331
332 if (!name)
333 name = dev->dev.driver->name;
334
335 if (!request_mem_region(dev->res.start, SZ_4K, name))
336 ret = -EBUSY;
337
338 return ret;
339 }
340
341 /**
342 * amba_release_regions - release mem regions assoicated with device
343 * @dev: amba_device structure for device
344 *
345 * Release regions claimed by a successful call to amba_request_regions.
346 */
347 void amba_release_regions(struct amba_device *dev)
348 {
349 release_mem_region(dev->res.start, SZ_4K);
350 }
351
352 EXPORT_SYMBOL(amba_driver_register);
353 EXPORT_SYMBOL(amba_driver_unregister);
354 EXPORT_SYMBOL(amba_device_register);
355 EXPORT_SYMBOL(amba_device_unregister);
356 EXPORT_SYMBOL(amba_find_device);
357 EXPORT_SYMBOL(amba_request_regions);
358 EXPORT_SYMBOL(amba_release_regions);
This page took 0.074772 seconds and 6 git commands to generate.