blob: fd5475071accea2d48ff5ec7f15270d61409a27b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Tim Schmielau4e57b682005-10-30 15:03:48 -080013#include <linux/string.h>
14#include <linux/slab.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000015#include <linux/amba/bus.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#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
23static struct amba_id *
24amba_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
38static 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
Paul Jackson6d20b032005-11-25 20:04:26 -080047static int amba_uevent(struct device *dev, char **envp, int nr_env, char *buf, int bufsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
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
Paul Jackson6d20b032005-11-25 20:04:26 -080060#define amba_uevent NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#endif
62
63static 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
73static 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
Russell King96b13f52006-11-30 14:04:49 +000083#define amba_attr_func(name,fmt,arg...) \
84static 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); \
89}
90
91#define amba_attr(name,fmt,arg...) \
92amba_attr_func(name,fmt,arg) \
93static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL)
94
95amba_attr_func(id, "%08x\n", dev->periphid);
96amba_attr(irq0, "%u\n", dev->irq[0]);
97amba_attr(irq1, "%u\n", dev->irq[1]);
98amba_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);
101
102static struct device_attribute amba_dev_attrs[] = {
103 __ATTR_RO(id),
104 __ATTR_RO(resource),
105 __ATTR_NULL,
106};
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/*
109 * Primecells are part of the Advanced Microcontroller Bus Architecture,
110 * so we call the bus "amba".
111 */
112static struct bus_type amba_bustype = {
113 .name = "amba",
Russell King96b13f52006-11-30 14:04:49 +0000114 .dev_attrs = amba_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .match = amba_match,
Paul Jackson6d20b032005-11-25 20:04:26 -0800116 .uevent = amba_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .suspend = amba_suspend,
118 .resume = amba_resume,
119};
120
121static int __init amba_init(void)
122{
123 return bus_register(&amba_bustype);
124}
125
126postcore_initcall(amba_init);
127
128/*
129 * These are the device model conversion veneers; they convert the
130 * device model structures to our more specific structures.
131 */
132static int amba_probe(struct device *dev)
133{
134 struct amba_device *pcdev = to_amba_device(dev);
135 struct amba_driver *pcdrv = to_amba_driver(dev->driver);
136 struct amba_id *id;
137
138 id = amba_lookup(pcdrv->id_table, pcdev);
139
140 return pcdrv->probe(pcdev, id);
141}
142
143static int amba_remove(struct device *dev)
144{
145 struct amba_driver *drv = to_amba_driver(dev->driver);
146 return drv->remove(to_amba_device(dev));
147}
148
149static void amba_shutdown(struct device *dev)
150{
151 struct amba_driver *drv = to_amba_driver(dev->driver);
152 drv->shutdown(to_amba_device(dev));
153}
154
155/**
156 * amba_driver_register - register an AMBA device driver
157 * @drv: amba device driver structure
158 *
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.
162 */
163int amba_driver_register(struct amba_driver *drv)
164{
165 drv->drv.bus = &amba_bustype;
166
167#define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
168 SETFN(probe);
169 SETFN(remove);
170 SETFN(shutdown);
171
172 return driver_register(&drv->drv);
173}
174
175/**
176 * amba_driver_unregister - remove an AMBA device driver
177 * @drv: AMBA device driver structure to remove
178 *
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.
182 */
183void amba_driver_unregister(struct amba_driver *drv)
184{
185 driver_unregister(&drv->drv);
186}
187
188
189static void amba_device_release(struct device *dev)
190{
191 struct amba_device *d = to_amba_device(dev);
192
193 if (d->res.parent)
194 release_resource(&d->res);
195 kfree(d);
196}
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/**
199 * amba_device_register - register an AMBA device
200 * @dev: AMBA device to register
201 * @parent: parent memory resource
202 *
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.
206 */
207int amba_device_register(struct amba_device *dev, struct resource *parent)
208{
209 u32 pid, cid;
210 void __iomem *tmp;
211 int i, ret;
212
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;
217
218 if (!dev->dev.coherent_dma_mask && dev->dma_mask)
219 dev_warn(&dev->dev, "coherent dma mask is unset\n");
220
221 ret = request_resource(parent, &dev->res);
Russell King96b13f52006-11-30 14:04:49 +0000222 if (ret)
223 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Russell King96b13f52006-11-30 14:04:49 +0000225 tmp = ioremap(dev->res.start, SZ_4K);
226 if (!tmp) {
227 ret = -ENOMEM;
228 goto err_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
Russell King96b13f52006-11-30 14:04:49 +0000230
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);
235
236 iounmap(tmp);
237
238 if (cid == 0xb105f00d)
239 dev->periphid = pid;
240
241 if (!dev->periphid) {
242 ret = -ENODEV;
243 goto err_release;
244 }
245
246 ret = device_register(&dev->dev);
247 if (ret)
248 goto err_release;
249
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;
256
257 device_unregister(&dev->dev);
258
259 err_release:
260 release_resource(&dev->res);
261 err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return ret;
263}
264
265/**
266 * amba_device_unregister - unregister an AMBA device
267 * @dev: AMBA device to remove
268 *
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.
275 */
276void amba_device_unregister(struct amba_device *dev)
277{
278 device_unregister(&dev->dev);
279}
280
281
282struct find_data {
283 struct amba_device *dev;
284 struct device *parent;
285 const char *busid;
286 unsigned int id;
287 unsigned int mask;
288};
289
290static int amba_find_match(struct device *dev, void *data)
291{
292 struct find_data *d = data;
293 struct amba_device *pcdev = to_amba_device(dev);
294 int r;
295
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;
301
302 if (r) {
303 get_device(dev);
304 d->dev = pcdev;
305 }
306
307 return r;
308}
309
310/**
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)
316 *
317 * Return the AMBA device corresponding to the supplied parameters.
318 * If no device matches, returns NULL.
319 *
320 * NOTE: When a valid device is found, its refcount is
321 * incremented, and must be decremented before the returned
322 * reference.
323 */
324struct amba_device *
325amba_find_device(const char *busid, struct device *parent, unsigned int id,
326 unsigned int mask)
327{
328 struct find_data data;
329
330 data.dev = NULL;
331 data.parent = parent;
332 data.busid = busid;
333 data.id = id;
334 data.mask = mask;
335
336 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
337
338 return data.dev;
339}
340
341/**
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
345 */
346int amba_request_regions(struct amba_device *dev, const char *name)
347{
348 int ret = 0;
349
350 if (!name)
351 name = dev->dev.driver->name;
352
353 if (!request_mem_region(dev->res.start, SZ_4K, name))
354 ret = -EBUSY;
355
356 return ret;
357}
358
359/**
360 * amba_release_regions - release mem regions assoicated with device
361 * @dev: amba_device structure for device
362 *
363 * Release regions claimed by a successful call to amba_request_regions.
364 */
365void amba_release_regions(struct amba_device *dev)
366{
367 release_mem_region(dev->res.start, SZ_4K);
368}
369
370EXPORT_SYMBOL(amba_driver_register);
371EXPORT_SYMBOL(amba_driver_unregister);
372EXPORT_SYMBOL(amba_device_register);
373EXPORT_SYMBOL(amba_device_unregister);
374EXPORT_SYMBOL(amba_find_device);
375EXPORT_SYMBOL(amba_request_regions);
376EXPORT_SYMBOL(amba_release_regions);