blob: e7df019d29d44731b7eacf035b5edd5de78a3780 [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
Russell King7cfe2492010-07-15 10:47:14 +0100125static int amba_get_enable_pclk(struct amba_device *pcdev)
126{
127 struct clk *pclk = clk_get(&pcdev->dev, "apb_pclk");
128 int ret;
129
130 pcdev->pclk = pclk;
131
132 if (IS_ERR(pclk))
133 return PTR_ERR(pclk);
134
135 ret = clk_enable(pclk);
136 if (ret)
137 clk_put(pclk);
138
139 return ret;
140}
141
142static void amba_put_disable_pclk(struct amba_device *pcdev)
143{
144 struct clk *pclk = pcdev->pclk;
145
146 clk_disable(pclk);
147 clk_put(pclk);
148}
149
Linus Walleij65500fa2010-11-04 13:06:59 +0100150static int amba_get_enable_vcore(struct amba_device *pcdev)
151{
152 struct regulator *vcore = regulator_get(&pcdev->dev, "vcore");
153 int ret;
154
155 pcdev->vcore = vcore;
156
157 if (IS_ERR(vcore)) {
158 /* It is OK not to supply a vcore regulator */
159 if (PTR_ERR(vcore) == -ENODEV)
160 return 0;
161 return PTR_ERR(vcore);
162 }
163
164 ret = regulator_enable(vcore);
165 if (ret) {
166 regulator_put(vcore);
167 pcdev->vcore = ERR_PTR(-ENODEV);
168 }
169
170 return ret;
171}
172
173static void amba_put_disable_vcore(struct amba_device *pcdev)
174{
175 struct regulator *vcore = pcdev->vcore;
176
177 if (!IS_ERR(vcore)) {
178 regulator_disable(vcore);
179 regulator_put(vcore);
180 }
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/*
184 * These are the device model conversion veneers; they convert the
185 * device model structures to our more specific structures.
186 */
187static int amba_probe(struct device *dev)
188{
189 struct amba_device *pcdev = to_amba_device(dev);
190 struct amba_driver *pcdrv = to_amba_driver(dev->driver);
Russell King7cfe2492010-07-15 10:47:14 +0100191 struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
192 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Russell King7cfe2492010-07-15 10:47:14 +0100194 do {
Linus Walleij65500fa2010-11-04 13:06:59 +0100195 ret = amba_get_enable_vcore(pcdev);
196 if (ret)
197 break;
198
Russell King7cfe2492010-07-15 10:47:14 +0100199 ret = amba_get_enable_pclk(pcdev);
200 if (ret)
201 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Russell King7cfe2492010-07-15 10:47:14 +0100203 ret = pcdrv->probe(pcdev, id);
204 if (ret == 0)
205 break;
206
207 amba_put_disable_pclk(pcdev);
Linus Walleij65500fa2010-11-04 13:06:59 +0100208 amba_put_disable_vcore(pcdev);
Russell King7cfe2492010-07-15 10:47:14 +0100209 } while (0);
210
211 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214static int amba_remove(struct device *dev)
215{
Russell King7cfe2492010-07-15 10:47:14 +0100216 struct amba_device *pcdev = to_amba_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 struct amba_driver *drv = to_amba_driver(dev->driver);
Russell King7cfe2492010-07-15 10:47:14 +0100218 int ret = drv->remove(pcdev);
219
220 amba_put_disable_pclk(pcdev);
Linus Walleij65500fa2010-11-04 13:06:59 +0100221 amba_put_disable_vcore(pcdev);
Russell King7cfe2492010-07-15 10:47:14 +0100222
223 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226static void amba_shutdown(struct device *dev)
227{
228 struct amba_driver *drv = to_amba_driver(dev->driver);
229 drv->shutdown(to_amba_device(dev));
230}
231
232/**
233 * amba_driver_register - register an AMBA device driver
234 * @drv: amba device driver structure
235 *
236 * Register an AMBA device driver with the Linux device model
237 * core. If devices pre-exist, the drivers probe function will
238 * be called.
239 */
240int amba_driver_register(struct amba_driver *drv)
241{
242 drv->drv.bus = &amba_bustype;
243
244#define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
245 SETFN(probe);
246 SETFN(remove);
247 SETFN(shutdown);
248
249 return driver_register(&drv->drv);
250}
251
252/**
253 * amba_driver_unregister - remove an AMBA device driver
254 * @drv: AMBA device driver structure to remove
255 *
256 * Unregister an AMBA device driver from the Linux device
257 * model. The device model will call the drivers remove function
258 * for each device the device driver is currently handling.
259 */
260void amba_driver_unregister(struct amba_driver *drv)
261{
262 driver_unregister(&drv->drv);
263}
264
265
266static void amba_device_release(struct device *dev)
267{
268 struct amba_device *d = to_amba_device(dev);
269
270 if (d->res.parent)
271 release_resource(&d->res);
272 kfree(d);
273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275/**
276 * amba_device_register - register an AMBA device
277 * @dev: AMBA device to register
278 * @parent: parent memory resource
279 *
280 * Setup the AMBA device, reading the cell ID if present.
281 * Claim the resource, and register the AMBA device with
282 * the Linux device manager.
283 */
284int amba_device_register(struct amba_device *dev, struct resource *parent)
285{
Leo Chen8afe0b92009-07-28 23:34:59 +0100286 u32 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 void __iomem *tmp;
288 int i, ret;
289
Russell King557dca52009-07-05 22:39:08 +0100290 device_initialize(&dev->dev);
291
292 /*
293 * Copy from device_add
294 */
295 if (dev->dev.init_name) {
296 dev_set_name(&dev->dev, "%s", dev->dev.init_name);
297 dev->dev.init_name = NULL;
298 }
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 dev->dev.release = amba_device_release;
301 dev->dev.bus = &amba_bustype;
302 dev->dev.dma_mask = &dev->dma_mask;
Kay Sievers9d6b4c82009-03-24 16:38:22 -0700303 dev->res.name = dev_name(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (!dev->dev.coherent_dma_mask && dev->dma_mask)
306 dev_warn(&dev->dev, "coherent dma mask is unset\n");
307
308 ret = request_resource(parent, &dev->res);
Russell King96b13f52006-11-30 14:04:49 +0000309 if (ret)
310 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Leo Chen8afe0b92009-07-28 23:34:59 +0100312 /*
313 * Dynamically calculate the size of the resource
314 * and use this for iomap
315 */
316 size = resource_size(&dev->res);
317 tmp = ioremap(dev->res.start, size);
Russell King96b13f52006-11-30 14:04:49 +0000318 if (!tmp) {
319 ret = -ENOMEM;
320 goto err_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
Russell King96b13f52006-11-30 14:04:49 +0000322
Russell King7cfe2492010-07-15 10:47:14 +0100323 ret = amba_get_enable_pclk(dev);
324 if (ret == 0) {
325 u32 pid, cid;
326
327 /*
328 * Read pid and cid based on size of resource
329 * they are located at end of region
330 */
331 for (pid = 0, i = 0; i < 4; i++)
332 pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
333 (i * 8);
334 for (cid = 0, i = 0; i < 4; i++)
335 cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
336 (i * 8);
337
338 amba_put_disable_pclk(dev);
339
Linus Walleij01723a92010-09-07 22:43:19 +0100340 if (cid == AMBA_CID)
Russell King7cfe2492010-07-15 10:47:14 +0100341 dev->periphid = pid;
342
343 if (!dev->periphid)
344 ret = -ENODEV;
345 }
Russell King96b13f52006-11-30 14:04:49 +0000346
347 iounmap(tmp);
348
Russell King7cfe2492010-07-15 10:47:14 +0100349 if (ret)
Russell King96b13f52006-11-30 14:04:49 +0000350 goto err_release;
Russell King96b13f52006-11-30 14:04:49 +0000351
Russell King557dca52009-07-05 22:39:08 +0100352 ret = device_add(&dev->dev);
Russell King96b13f52006-11-30 14:04:49 +0000353 if (ret)
354 goto err_release;
355
356 if (dev->irq[0] != NO_IRQ)
357 ret = device_create_file(&dev->dev, &dev_attr_irq0);
358 if (ret == 0 && dev->irq[1] != NO_IRQ)
359 ret = device_create_file(&dev->dev, &dev_attr_irq1);
360 if (ret == 0)
361 return ret;
362
363 device_unregister(&dev->dev);
364
365 err_release:
366 release_resource(&dev->res);
367 err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return ret;
369}
370
371/**
372 * amba_device_unregister - unregister an AMBA device
373 * @dev: AMBA device to remove
374 *
375 * Remove the specified AMBA device from the Linux device
376 * manager. All files associated with this object will be
377 * destroyed, and device drivers notified that the device has
378 * been removed. The AMBA device's resources including
379 * the amba_device structure will be freed once all
380 * references to it have been dropped.
381 */
382void amba_device_unregister(struct amba_device *dev)
383{
384 device_unregister(&dev->dev);
385}
386
387
388struct find_data {
389 struct amba_device *dev;
390 struct device *parent;
391 const char *busid;
392 unsigned int id;
393 unsigned int mask;
394};
395
396static int amba_find_match(struct device *dev, void *data)
397{
398 struct find_data *d = data;
399 struct amba_device *pcdev = to_amba_device(dev);
400 int r;
401
402 r = (pcdev->periphid & d->mask) == d->id;
403 if (d->parent)
404 r &= d->parent == dev->parent;
405 if (d->busid)
Kay Sievers9d6b4c82009-03-24 16:38:22 -0700406 r &= strcmp(dev_name(dev), d->busid) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 if (r) {
409 get_device(dev);
410 d->dev = pcdev;
411 }
412
413 return r;
414}
415
416/**
417 * amba_find_device - locate an AMBA device given a bus id
418 * @busid: bus id for device (or NULL)
419 * @parent: parent device (or NULL)
420 * @id: peripheral ID (or 0)
421 * @mask: peripheral ID mask (or 0)
422 *
423 * Return the AMBA device corresponding to the supplied parameters.
424 * If no device matches, returns NULL.
425 *
426 * NOTE: When a valid device is found, its refcount is
427 * incremented, and must be decremented before the returned
428 * reference.
429 */
430struct amba_device *
431amba_find_device(const char *busid, struct device *parent, unsigned int id,
432 unsigned int mask)
433{
434 struct find_data data;
435
436 data.dev = NULL;
437 data.parent = parent;
438 data.busid = busid;
439 data.id = id;
440 data.mask = mask;
441
442 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
443
444 return data.dev;
445}
446
447/**
448 * amba_request_regions - request all mem regions associated with device
449 * @dev: amba_device structure for device
450 * @name: name, or NULL to use driver name
451 */
452int amba_request_regions(struct amba_device *dev, const char *name)
453{
454 int ret = 0;
Leo Chen8afe0b92009-07-28 23:34:59 +0100455 u32 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 if (!name)
458 name = dev->dev.driver->name;
459
Leo Chen8afe0b92009-07-28 23:34:59 +0100460 size = resource_size(&dev->res);
461
462 if (!request_mem_region(dev->res.start, size, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 ret = -EBUSY;
464
465 return ret;
466}
467
468/**
469 * amba_release_regions - release mem regions assoicated with device
470 * @dev: amba_device structure for device
471 *
472 * Release regions claimed by a successful call to amba_request_regions.
473 */
474void amba_release_regions(struct amba_device *dev)
475{
Leo Chen8afe0b92009-07-28 23:34:59 +0100476 u32 size;
477
478 size = resource_size(&dev->res);
479 release_mem_region(dev->res.start, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482EXPORT_SYMBOL(amba_driver_register);
483EXPORT_SYMBOL(amba_driver_unregister);
484EXPORT_SYMBOL(amba_device_register);
485EXPORT_SYMBOL(amba_device_unregister);
486EXPORT_SYMBOL(amba_find_device);
487EXPORT_SYMBOL(amba_request_regions);
488EXPORT_SYMBOL(amba_release_regions);