blob: f60b2b6a0931d7d7279bfbfe7d29b3ac7ce6efea [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 King934848d2009-01-08 09:58:51 +000015#include <linux/io.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000016#include <linux/amba/bus.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Russell King934848d2009-01-08 09:58:51 +000018#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/sizes.h>
20
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)
23
24static struct amba_id *
25amba_lookup(struct amba_id *table, struct amba_device *dev)
26{
27 int ret = 0;
28
29 while (table->mask) {
30 ret = (dev->periphid & table->mask) == table->id;
31 if (ret)
32 break;
33 table++;
34 }
35
36 return ret ? table : NULL;
37}
38
39static int amba_match(struct device *dev, struct device_driver *drv)
40{
41 struct amba_device *pcdev = to_amba_device(dev);
42 struct amba_driver *pcdrv = to_amba_driver(drv);
43
44 return amba_lookup(pcdrv->id_table, pcdev) != NULL;
45}
46
47#ifdef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +020048static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 struct amba_device *pcdev = to_amba_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +020051 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Kay Sievers7eff2e72007-08-14 15:15:12 +020053 retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
Eric Rannaudbf624562007-03-30 22:23:12 -070054 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56#else
Paul Jackson6d20b032005-11-25 20:04:26 -080057#define amba_uevent NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59
60static int amba_suspend(struct device *dev, pm_message_t state)
61{
62 struct amba_driver *drv = to_amba_driver(dev->driver);
63 int ret = 0;
64
65 if (dev->driver && drv->suspend)
66 ret = drv->suspend(to_amba_device(dev), state);
67 return ret;
68}
69
70static int amba_resume(struct device *dev)
71{
72 struct amba_driver *drv = to_amba_driver(dev->driver);
73 int ret = 0;
74
75 if (dev->driver && drv->resume)
76 ret = drv->resume(to_amba_device(dev));
77 return ret;
78}
79
Russell King96b13f52006-11-30 14:04:49 +000080#define amba_attr_func(name,fmt,arg...) \
81static 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); \
86}
87
88#define amba_attr(name,fmt,arg...) \
89amba_attr_func(name,fmt,arg) \
90static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL)
91
92amba_attr_func(id, "%08x\n", dev->periphid);
93amba_attr(irq0, "%u\n", dev->irq[0]);
94amba_attr(irq1, "%u\n", dev->irq[1]);
95amba_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);
98
99static struct device_attribute amba_dev_attrs[] = {
100 __ATTR_RO(id),
101 __ATTR_RO(resource),
102 __ATTR_NULL,
103};
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
106 * Primecells are part of the Advanced Microcontroller Bus Architecture,
107 * so we call the bus "amba".
108 */
109static struct bus_type amba_bustype = {
110 .name = "amba",
Russell King96b13f52006-11-30 14:04:49 +0000111 .dev_attrs = amba_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .match = amba_match,
Paul Jackson6d20b032005-11-25 20:04:26 -0800113 .uevent = amba_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .suspend = amba_suspend,
115 .resume = amba_resume,
116};
117
118static int __init amba_init(void)
119{
120 return bus_register(&amba_bustype);
121}
122
123postcore_initcall(amba_init);
124
125/*
126 * These are the device model conversion veneers; they convert the
127 * device model structures to our more specific structures.
128 */
129static int amba_probe(struct device *dev)
130{
131 struct amba_device *pcdev = to_amba_device(dev);
132 struct amba_driver *pcdrv = to_amba_driver(dev->driver);
133 struct amba_id *id;
134
135 id = amba_lookup(pcdrv->id_table, pcdev);
136
137 return pcdrv->probe(pcdev, id);
138}
139
140static int amba_remove(struct device *dev)
141{
142 struct amba_driver *drv = to_amba_driver(dev->driver);
143 return drv->remove(to_amba_device(dev));
144}
145
146static void amba_shutdown(struct device *dev)
147{
148 struct amba_driver *drv = to_amba_driver(dev->driver);
149 drv->shutdown(to_amba_device(dev));
150}
151
152/**
153 * amba_driver_register - register an AMBA device driver
154 * @drv: amba device driver structure
155 *
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.
159 */
160int amba_driver_register(struct amba_driver *drv)
161{
162 drv->drv.bus = &amba_bustype;
163
164#define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
165 SETFN(probe);
166 SETFN(remove);
167 SETFN(shutdown);
168
169 return driver_register(&drv->drv);
170}
171
172/**
173 * amba_driver_unregister - remove an AMBA device driver
174 * @drv: AMBA device driver structure to remove
175 *
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.
179 */
180void amba_driver_unregister(struct amba_driver *drv)
181{
182 driver_unregister(&drv->drv);
183}
184
185
186static void amba_device_release(struct device *dev)
187{
188 struct amba_device *d = to_amba_device(dev);
189
190 if (d->res.parent)
191 release_resource(&d->res);
192 kfree(d);
193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/**
196 * amba_device_register - register an AMBA device
197 * @dev: AMBA device to register
198 * @parent: parent memory resource
199 *
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.
203 */
204int amba_device_register(struct amba_device *dev, struct resource *parent)
205{
206 u32 pid, cid;
Leo Chen8afe0b92009-07-28 23:34:59 +0100207 u32 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 void __iomem *tmp;
209 int i, ret;
210
Russell King557dca52009-07-05 22:39:08 +0100211 device_initialize(&dev->dev);
212
213 /*
214 * Copy from device_add
215 */
216 if (dev->dev.init_name) {
217 dev_set_name(&dev->dev, "%s", dev->dev.init_name);
218 dev->dev.init_name = NULL;
219 }
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 dev->dev.release = amba_device_release;
222 dev->dev.bus = &amba_bustype;
223 dev->dev.dma_mask = &dev->dma_mask;
Kay Sievers9d6b4c82009-03-24 16:38:22 -0700224 dev->res.name = dev_name(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 if (!dev->dev.coherent_dma_mask && dev->dma_mask)
227 dev_warn(&dev->dev, "coherent dma mask is unset\n");
228
229 ret = request_resource(parent, &dev->res);
Russell King96b13f52006-11-30 14:04:49 +0000230 if (ret)
231 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Leo Chen8afe0b92009-07-28 23:34:59 +0100233 /*
234 * Dynamically calculate the size of the resource
235 * and use this for iomap
236 */
237 size = resource_size(&dev->res);
238 tmp = ioremap(dev->res.start, size);
Russell King96b13f52006-11-30 14:04:49 +0000239 if (!tmp) {
240 ret = -ENOMEM;
241 goto err_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Russell King96b13f52006-11-30 14:04:49 +0000243
Leo Chen8afe0b92009-07-28 23:34:59 +0100244 /*
245 * Read pid and cid based on size of resource
246 * they are located at end of region
247 */
Russell King96b13f52006-11-30 14:04:49 +0000248 for (pid = 0, i = 0; i < 4; i++)
Leo Chen8afe0b92009-07-28 23:34:59 +0100249 pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) << (i * 8);
Russell King96b13f52006-11-30 14:04:49 +0000250 for (cid = 0, i = 0; i < 4; i++)
Leo Chen8afe0b92009-07-28 23:34:59 +0100251 cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) << (i * 8);
Russell King96b13f52006-11-30 14:04:49 +0000252
253 iounmap(tmp);
254
255 if (cid == 0xb105f00d)
256 dev->periphid = pid;
257
258 if (!dev->periphid) {
259 ret = -ENODEV;
260 goto err_release;
261 }
262
Russell King557dca52009-07-05 22:39:08 +0100263 ret = device_add(&dev->dev);
Russell King96b13f52006-11-30 14:04:49 +0000264 if (ret)
265 goto err_release;
266
267 if (dev->irq[0] != NO_IRQ)
268 ret = device_create_file(&dev->dev, &dev_attr_irq0);
269 if (ret == 0 && dev->irq[1] != NO_IRQ)
270 ret = device_create_file(&dev->dev, &dev_attr_irq1);
271 if (ret == 0)
272 return ret;
273
274 device_unregister(&dev->dev);
275
276 err_release:
277 release_resource(&dev->res);
278 err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return ret;
280}
281
282/**
283 * amba_device_unregister - unregister an AMBA device
284 * @dev: AMBA device to remove
285 *
286 * Remove the specified AMBA device from the Linux device
287 * manager. All files associated with this object will be
288 * destroyed, and device drivers notified that the device has
289 * been removed. The AMBA device's resources including
290 * the amba_device structure will be freed once all
291 * references to it have been dropped.
292 */
293void amba_device_unregister(struct amba_device *dev)
294{
295 device_unregister(&dev->dev);
296}
297
298
299struct find_data {
300 struct amba_device *dev;
301 struct device *parent;
302 const char *busid;
303 unsigned int id;
304 unsigned int mask;
305};
306
307static int amba_find_match(struct device *dev, void *data)
308{
309 struct find_data *d = data;
310 struct amba_device *pcdev = to_amba_device(dev);
311 int r;
312
313 r = (pcdev->periphid & d->mask) == d->id;
314 if (d->parent)
315 r &= d->parent == dev->parent;
316 if (d->busid)
Kay Sievers9d6b4c82009-03-24 16:38:22 -0700317 r &= strcmp(dev_name(dev), d->busid) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 if (r) {
320 get_device(dev);
321 d->dev = pcdev;
322 }
323
324 return r;
325}
326
327/**
328 * amba_find_device - locate an AMBA device given a bus id
329 * @busid: bus id for device (or NULL)
330 * @parent: parent device (or NULL)
331 * @id: peripheral ID (or 0)
332 * @mask: peripheral ID mask (or 0)
333 *
334 * Return the AMBA device corresponding to the supplied parameters.
335 * If no device matches, returns NULL.
336 *
337 * NOTE: When a valid device is found, its refcount is
338 * incremented, and must be decremented before the returned
339 * reference.
340 */
341struct amba_device *
342amba_find_device(const char *busid, struct device *parent, unsigned int id,
343 unsigned int mask)
344{
345 struct find_data data;
346
347 data.dev = NULL;
348 data.parent = parent;
349 data.busid = busid;
350 data.id = id;
351 data.mask = mask;
352
353 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
354
355 return data.dev;
356}
357
358/**
359 * amba_request_regions - request all mem regions associated with device
360 * @dev: amba_device structure for device
361 * @name: name, or NULL to use driver name
362 */
363int amba_request_regions(struct amba_device *dev, const char *name)
364{
365 int ret = 0;
Leo Chen8afe0b92009-07-28 23:34:59 +0100366 u32 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 if (!name)
369 name = dev->dev.driver->name;
370
Leo Chen8afe0b92009-07-28 23:34:59 +0100371 size = resource_size(&dev->res);
372
373 if (!request_mem_region(dev->res.start, size, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 ret = -EBUSY;
375
376 return ret;
377}
378
379/**
380 * amba_release_regions - release mem regions assoicated with device
381 * @dev: amba_device structure for device
382 *
383 * Release regions claimed by a successful call to amba_request_regions.
384 */
385void amba_release_regions(struct amba_device *dev)
386{
Leo Chen8afe0b92009-07-28 23:34:59 +0100387 u32 size;
388
389 size = resource_size(&dev->res);
390 release_mem_region(dev->res.start, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393EXPORT_SYMBOL(amba_driver_register);
394EXPORT_SYMBOL(amba_driver_unregister);
395EXPORT_SYMBOL(amba_device_register);
396EXPORT_SYMBOL(amba_device_unregister);
397EXPORT_SYMBOL(amba_find_device);
398EXPORT_SYMBOL(amba_request_regions);
399EXPORT_SYMBOL(amba_release_regions);