blob: c4876ac9151a56bc95a05df647e0850cea3182c4 [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>
Rabin Vincentba74ec72011-02-23 04:33:17 +010016#include <linux/pm.h>
17#include <linux/pm_runtime.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000018#include <linux/amba/bus.h>
Alessandro Rubinia875cfb2012-06-24 12:46:16 +010019#include <linux/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Russell King934848d2009-01-08 09:58:51 +000021#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#define to_amba_driver(d) container_of(d, struct amba_driver, drv)
24
Russell Kingc862aab2011-02-19 15:55:26 +000025static const struct amba_id *
26amba_lookup(const struct amba_id *table, struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
28 int ret = 0;
29
30 while (table->mask) {
31 ret = (dev->periphid & table->mask) == table->id;
32 if (ret)
33 break;
34 table++;
35 }
36
37 return ret ? table : NULL;
38}
39
40static int amba_match(struct device *dev, struct device_driver *drv)
41{
42 struct amba_device *pcdev = to_amba_device(dev);
43 struct amba_driver *pcdrv = to_amba_driver(drv);
44
45 return amba_lookup(pcdrv->id_table, pcdev) != NULL;
46}
47
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);
Dave Martin523817b2011-10-05 14:44:57 +010054 if (retval)
55 return retval;
56
57 retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);
Eric Rannaudbf624562007-03-30 22:23:12 -070058 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Russell King96b13f52006-11-30 14:04:49 +000061#define amba_attr_func(name,fmt,arg...) \
62static ssize_t name##_show(struct device *_dev, \
63 struct device_attribute *attr, char *buf) \
64{ \
65 struct amba_device *dev = to_amba_device(_dev); \
66 return sprintf(buf, fmt, arg); \
67}
68
69#define amba_attr(name,fmt,arg...) \
70amba_attr_func(name,fmt,arg) \
71static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL)
72
73amba_attr_func(id, "%08x\n", dev->periphid);
74amba_attr(irq0, "%u\n", dev->irq[0]);
75amba_attr(irq1, "%u\n", dev->irq[1]);
76amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",
77 (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
78 dev->res.flags);
79
80static struct device_attribute amba_dev_attrs[] = {
81 __ATTR_RO(id),
82 __ATTR_RO(resource),
83 __ATTR_NULL,
84};
85
Rabin Vincentba74ec72011-02-23 04:33:17 +010086#ifdef CONFIG_PM_SLEEP
87
88static int amba_legacy_suspend(struct device *dev, pm_message_t mesg)
89{
90 struct amba_driver *adrv = to_amba_driver(dev->driver);
91 struct amba_device *adev = to_amba_device(dev);
92 int ret = 0;
93
94 if (dev->driver && adrv->suspend)
95 ret = adrv->suspend(adev, mesg);
96
97 return ret;
98}
99
100static int amba_legacy_resume(struct device *dev)
101{
102 struct amba_driver *adrv = to_amba_driver(dev->driver);
103 struct amba_device *adev = to_amba_device(dev);
104 int ret = 0;
105
106 if (dev->driver && adrv->resume)
107 ret = adrv->resume(adev);
108
109 return ret;
110}
111
Rafael J. Wysocki8114ab72011-12-18 00:34:31 +0100112#endif /* CONFIG_PM_SLEEP */
Rabin Vincentba74ec72011-02-23 04:33:17 +0100113
114#ifdef CONFIG_SUSPEND
115
116static int amba_pm_suspend(struct device *dev)
117{
118 struct device_driver *drv = dev->driver;
119 int ret = 0;
120
121 if (!drv)
122 return 0;
123
124 if (drv->pm) {
125 if (drv->pm->suspend)
126 ret = drv->pm->suspend(dev);
127 } else {
128 ret = amba_legacy_suspend(dev, PMSG_SUSPEND);
129 }
130
131 return ret;
132}
133
Rabin Vincentba74ec72011-02-23 04:33:17 +0100134static int amba_pm_resume(struct device *dev)
135{
136 struct device_driver *drv = dev->driver;
137 int ret = 0;
138
139 if (!drv)
140 return 0;
141
142 if (drv->pm) {
143 if (drv->pm->resume)
144 ret = drv->pm->resume(dev);
145 } else {
146 ret = amba_legacy_resume(dev);
147 }
148
149 return ret;
150}
151
Rabin Vincentba74ec72011-02-23 04:33:17 +0100152#else /* !CONFIG_SUSPEND */
153
154#define amba_pm_suspend NULL
155#define amba_pm_resume NULL
Rabin Vincentba74ec72011-02-23 04:33:17 +0100156
157#endif /* !CONFIG_SUSPEND */
158
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200159#ifdef CONFIG_HIBERNATE_CALLBACKS
Rabin Vincentba74ec72011-02-23 04:33:17 +0100160
161static int amba_pm_freeze(struct device *dev)
162{
163 struct device_driver *drv = dev->driver;
164 int ret = 0;
165
166 if (!drv)
167 return 0;
168
169 if (drv->pm) {
170 if (drv->pm->freeze)
171 ret = drv->pm->freeze(dev);
172 } else {
173 ret = amba_legacy_suspend(dev, PMSG_FREEZE);
174 }
175
176 return ret;
177}
178
Rabin Vincentba74ec72011-02-23 04:33:17 +0100179static int amba_pm_thaw(struct device *dev)
180{
181 struct device_driver *drv = dev->driver;
182 int ret = 0;
183
184 if (!drv)
185 return 0;
186
187 if (drv->pm) {
188 if (drv->pm->thaw)
189 ret = drv->pm->thaw(dev);
190 } else {
191 ret = amba_legacy_resume(dev);
192 }
193
194 return ret;
195}
196
Rabin Vincentba74ec72011-02-23 04:33:17 +0100197static int amba_pm_poweroff(struct device *dev)
198{
199 struct device_driver *drv = dev->driver;
200 int ret = 0;
201
202 if (!drv)
203 return 0;
204
205 if (drv->pm) {
206 if (drv->pm->poweroff)
207 ret = drv->pm->poweroff(dev);
208 } else {
209 ret = amba_legacy_suspend(dev, PMSG_HIBERNATE);
210 }
211
212 return ret;
213}
214
Rabin Vincentba74ec72011-02-23 04:33:17 +0100215static int amba_pm_restore(struct device *dev)
216{
217 struct device_driver *drv = dev->driver;
218 int ret = 0;
219
220 if (!drv)
221 return 0;
222
223 if (drv->pm) {
224 if (drv->pm->restore)
225 ret = drv->pm->restore(dev);
226 } else {
227 ret = amba_legacy_resume(dev);
228 }
229
230 return ret;
231}
232
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200233#else /* !CONFIG_HIBERNATE_CALLBACKS */
Rabin Vincentba74ec72011-02-23 04:33:17 +0100234
235#define amba_pm_freeze NULL
236#define amba_pm_thaw NULL
237#define amba_pm_poweroff NULL
238#define amba_pm_restore NULL
Rabin Vincentba74ec72011-02-23 04:33:17 +0100239
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200240#endif /* !CONFIG_HIBERNATE_CALLBACKS */
Rabin Vincentba74ec72011-02-23 04:33:17 +0100241
Russell King92b97f02011-08-14 09:13:48 +0100242#ifdef CONFIG_PM_RUNTIME
243/*
244 * Hooks to provide runtime PM of the pclk (bus clock). It is safe to
245 * enable/disable the bus clock at runtime PM suspend/resume as this
Mark Brown1e458602012-04-13 13:11:50 +0100246 * does not result in loss of context.
Russell King92b97f02011-08-14 09:13:48 +0100247 */
248static int amba_pm_runtime_suspend(struct device *dev)
249{
250 struct amba_device *pcdev = to_amba_device(dev);
251 int ret = pm_generic_runtime_suspend(dev);
252
253 if (ret == 0 && dev->driver)
254 clk_disable(pcdev->pclk);
255
256 return ret;
257}
258
259static int amba_pm_runtime_resume(struct device *dev)
260{
261 struct amba_device *pcdev = to_amba_device(dev);
262 int ret;
263
264 if (dev->driver) {
265 ret = clk_enable(pcdev->pclk);
266 /* Failure is probably fatal to the system, but... */
267 if (ret)
268 return ret;
269 }
270
271 return pm_generic_runtime_resume(dev);
272}
273#endif
274
Rabin Vincentba74ec72011-02-23 04:33:17 +0100275#ifdef CONFIG_PM
276
277static const struct dev_pm_ops amba_pm = {
Rabin Vincentba74ec72011-02-23 04:33:17 +0100278 .suspend = amba_pm_suspend,
279 .resume = amba_pm_resume,
280 .freeze = amba_pm_freeze,
281 .thaw = amba_pm_thaw,
282 .poweroff = amba_pm_poweroff,
283 .restore = amba_pm_restore,
Rabin Vincentba74ec72011-02-23 04:33:17 +0100284 SET_RUNTIME_PM_OPS(
Russell King92b97f02011-08-14 09:13:48 +0100285 amba_pm_runtime_suspend,
286 amba_pm_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200287 NULL
Rabin Vincentba74ec72011-02-23 04:33:17 +0100288 )
289};
290
291#define AMBA_PM (&amba_pm)
292
293#else /* !CONFIG_PM */
294
295#define AMBA_PM NULL
296
297#endif /* !CONFIG_PM */
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/*
300 * Primecells are part of the Advanced Microcontroller Bus Architecture,
301 * so we call the bus "amba".
302 */
Rob Herring394d5ae2011-02-12 15:58:25 +0100303struct bus_type amba_bustype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 .name = "amba",
Russell King96b13f52006-11-30 14:04:49 +0000305 .dev_attrs = amba_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 .match = amba_match,
Paul Jackson6d20b032005-11-25 20:04:26 -0800307 .uevent = amba_uevent,
Rabin Vincentba74ec72011-02-23 04:33:17 +0100308 .pm = AMBA_PM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309};
310
311static int __init amba_init(void)
312{
313 return bus_register(&amba_bustype);
314}
315
316postcore_initcall(amba_init);
317
Russell King7cfe2492010-07-15 10:47:14 +0100318static int amba_get_enable_pclk(struct amba_device *pcdev)
319{
320 struct clk *pclk = clk_get(&pcdev->dev, "apb_pclk");
321 int ret;
322
323 pcdev->pclk = pclk;
324
325 if (IS_ERR(pclk))
326 return PTR_ERR(pclk);
327
Russell Kingac3e2fa2011-09-22 12:20:55 +0100328 ret = clk_prepare(pclk);
329 if (ret) {
Russell King7cfe2492010-07-15 10:47:14 +0100330 clk_put(pclk);
Russell Kingac3e2fa2011-09-22 12:20:55 +0100331 return ret;
332 }
333
334 ret = clk_enable(pclk);
335 if (ret) {
336 clk_unprepare(pclk);
337 clk_put(pclk);
338 }
Russell King7cfe2492010-07-15 10:47:14 +0100339
340 return ret;
341}
342
343static void amba_put_disable_pclk(struct amba_device *pcdev)
344{
345 struct clk *pclk = pcdev->pclk;
346
347 clk_disable(pclk);
Russell Kingac3e2fa2011-09-22 12:20:55 +0100348 clk_unprepare(pclk);
Russell King7cfe2492010-07-15 10:47:14 +0100349 clk_put(pclk);
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/*
353 * These are the device model conversion veneers; they convert the
354 * device model structures to our more specific structures.
355 */
356static int amba_probe(struct device *dev)
357{
358 struct amba_device *pcdev = to_amba_device(dev);
359 struct amba_driver *pcdrv = to_amba_driver(dev->driver);
Russell Kingc862aab2011-02-19 15:55:26 +0000360 const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
Russell King7cfe2492010-07-15 10:47:14 +0100361 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Russell King7cfe2492010-07-15 10:47:14 +0100363 do {
364 ret = amba_get_enable_pclk(pcdev);
365 if (ret)
366 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Russell King92b97f02011-08-14 09:13:48 +0100368 pm_runtime_get_noresume(dev);
369 pm_runtime_set_active(dev);
370 pm_runtime_enable(dev);
371
Russell King7cfe2492010-07-15 10:47:14 +0100372 ret = pcdrv->probe(pcdev, id);
373 if (ret == 0)
374 break;
375
Russell King92b97f02011-08-14 09:13:48 +0100376 pm_runtime_disable(dev);
377 pm_runtime_set_suspended(dev);
378 pm_runtime_put_noidle(dev);
379
Russell King7cfe2492010-07-15 10:47:14 +0100380 amba_put_disable_pclk(pcdev);
381 } while (0);
382
383 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386static int amba_remove(struct device *dev)
387{
Russell King7cfe2492010-07-15 10:47:14 +0100388 struct amba_device *pcdev = to_amba_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct amba_driver *drv = to_amba_driver(dev->driver);
Russell King92b97f02011-08-14 09:13:48 +0100390 int ret;
391
392 pm_runtime_get_sync(dev);
393 ret = drv->remove(pcdev);
394 pm_runtime_put_noidle(dev);
395
396 /* Undo the runtime PM settings in amba_probe() */
397 pm_runtime_disable(dev);
398 pm_runtime_set_suspended(dev);
399 pm_runtime_put_noidle(dev);
Russell King7cfe2492010-07-15 10:47:14 +0100400
401 amba_put_disable_pclk(pcdev);
402
403 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406static void amba_shutdown(struct device *dev)
407{
408 struct amba_driver *drv = to_amba_driver(dev->driver);
409 drv->shutdown(to_amba_device(dev));
410}
411
412/**
413 * amba_driver_register - register an AMBA device driver
414 * @drv: amba device driver structure
415 *
416 * Register an AMBA device driver with the Linux device model
417 * core. If devices pre-exist, the drivers probe function will
418 * be called.
419 */
420int amba_driver_register(struct amba_driver *drv)
421{
422 drv->drv.bus = &amba_bustype;
423
424#define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
425 SETFN(probe);
426 SETFN(remove);
427 SETFN(shutdown);
428
429 return driver_register(&drv->drv);
430}
431
432/**
433 * amba_driver_unregister - remove an AMBA device driver
434 * @drv: AMBA device driver structure to remove
435 *
436 * Unregister an AMBA device driver from the Linux device
437 * model. The device model will call the drivers remove function
438 * for each device the device driver is currently handling.
439 */
440void amba_driver_unregister(struct amba_driver *drv)
441{
442 driver_unregister(&drv->drv);
443}
444
445
446static void amba_device_release(struct device *dev)
447{
448 struct amba_device *d = to_amba_device(dev);
449
450 if (d->res.parent)
451 release_resource(&d->res);
452 kfree(d);
453}
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/**
Russell Kingd5dc9272011-12-18 11:07:47 +0000456 * amba_device_add - add a previously allocated AMBA device structure
457 * @dev: AMBA device allocated by amba_device_alloc
458 * @parent: resource parent for this devices resources
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 *
Russell Kingd5dc9272011-12-18 11:07:47 +0000460 * Claim the resource, and read the device cell ID if not already
461 * initialized. Register the AMBA device with the Linux device
462 * manager.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 */
Russell Kingd5dc9272011-12-18 11:07:47 +0000464int amba_device_add(struct amba_device *dev, struct resource *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Leo Chen8afe0b92009-07-28 23:34:59 +0100466 u32 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 void __iomem *tmp;
468 int i, ret;
469
Russell King2eac58d2011-12-18 11:43:56 +0000470 WARN_ON(dev->irq[0] == (unsigned int)-1);
471 WARN_ON(dev->irq[1] == (unsigned int)-1);
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 ret = request_resource(parent, &dev->res);
Russell King96b13f52006-11-30 14:04:49 +0000474 if (ret)
475 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Linus Walleij97ceed12011-03-24 16:12:40 +0100477 /* Hard-coded primecell ID instead of plug-n-play */
478 if (dev->periphid != 0)
479 goto skip_probe;
480
Leo Chen8afe0b92009-07-28 23:34:59 +0100481 /*
482 * Dynamically calculate the size of the resource
483 * and use this for iomap
484 */
485 size = resource_size(&dev->res);
486 tmp = ioremap(dev->res.start, size);
Russell King96b13f52006-11-30 14:04:49 +0000487 if (!tmp) {
488 ret = -ENOMEM;
489 goto err_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Russell King96b13f52006-11-30 14:04:49 +0000491
Russell King7cfe2492010-07-15 10:47:14 +0100492 ret = amba_get_enable_pclk(dev);
493 if (ret == 0) {
494 u32 pid, cid;
495
496 /*
497 * Read pid and cid based on size of resource
498 * they are located at end of region
499 */
500 for (pid = 0, i = 0; i < 4; i++)
501 pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
502 (i * 8);
503 for (cid = 0, i = 0; i < 4; i++)
504 cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
505 (i * 8);
506
507 amba_put_disable_pclk(dev);
508
Linus Walleij01723a92010-09-07 22:43:19 +0100509 if (cid == AMBA_CID)
Russell King7cfe2492010-07-15 10:47:14 +0100510 dev->periphid = pid;
511
512 if (!dev->periphid)
513 ret = -ENODEV;
514 }
Russell King96b13f52006-11-30 14:04:49 +0000515
516 iounmap(tmp);
517
Russell King7cfe2492010-07-15 10:47:14 +0100518 if (ret)
Russell King96b13f52006-11-30 14:04:49 +0000519 goto err_release;
Russell King96b13f52006-11-30 14:04:49 +0000520
Linus Walleij97ceed12011-03-24 16:12:40 +0100521 skip_probe:
Russell King557dca52009-07-05 22:39:08 +0100522 ret = device_add(&dev->dev);
Russell King96b13f52006-11-30 14:04:49 +0000523 if (ret)
524 goto err_release;
525
Russell Kingdfb85182012-05-03 11:33:15 +0100526 if (dev->irq[0])
Russell King96b13f52006-11-30 14:04:49 +0000527 ret = device_create_file(&dev->dev, &dev_attr_irq0);
Russell Kingdfb85182012-05-03 11:33:15 +0100528 if (ret == 0 && dev->irq[1])
Russell King96b13f52006-11-30 14:04:49 +0000529 ret = device_create_file(&dev->dev, &dev_attr_irq1);
530 if (ret == 0)
531 return ret;
532
533 device_unregister(&dev->dev);
534
535 err_release:
536 release_resource(&dev->res);
537 err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return ret;
539}
Russell Kingd5dc9272011-12-18 11:07:47 +0000540EXPORT_SYMBOL_GPL(amba_device_add);
541
Linus Walleij6026aa92012-04-03 11:58:42 +0100542static struct amba_device *
543amba_aphb_device_add(struct device *parent, const char *name,
544 resource_size_t base, size_t size, int irq1, int irq2,
Linus Walleij3ad909b2012-11-30 16:30:43 +0100545 void *pdata, unsigned int periphid, u64 dma_mask,
546 struct resource *resbase)
Linus Walleij6026aa92012-04-03 11:58:42 +0100547{
548 struct amba_device *dev;
549 int ret;
550
551 dev = amba_device_alloc(name, base, size);
552 if (!dev)
553 return ERR_PTR(-ENOMEM);
554
Linus Walleij6026aa92012-04-03 11:58:42 +0100555 dev->dev.coherent_dma_mask = dma_mask;
556 dev->irq[0] = irq1;
557 dev->irq[1] = irq2;
558 dev->periphid = periphid;
559 dev->dev.platform_data = pdata;
560 dev->dev.parent = parent;
561
Linus Walleij3ad909b2012-11-30 16:30:43 +0100562 ret = amba_device_add(dev, resbase);
Linus Walleij6026aa92012-04-03 11:58:42 +0100563 if (ret) {
564 amba_device_put(dev);
565 return ERR_PTR(ret);
566 }
567
568 return dev;
569}
570
571struct amba_device *
572amba_apb_device_add(struct device *parent, const char *name,
573 resource_size_t base, size_t size, int irq1, int irq2,
574 void *pdata, unsigned int periphid)
575{
576 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
Linus Walleij3ad909b2012-11-30 16:30:43 +0100577 periphid, 0, &iomem_resource);
Linus Walleij6026aa92012-04-03 11:58:42 +0100578}
579EXPORT_SYMBOL_GPL(amba_apb_device_add);
580
581struct amba_device *
582amba_ahb_device_add(struct device *parent, const char *name,
583 resource_size_t base, size_t size, int irq1, int irq2,
584 void *pdata, unsigned int periphid)
585{
586 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
Linus Walleij3ad909b2012-11-30 16:30:43 +0100587 periphid, ~0ULL, &iomem_resource);
Linus Walleij6026aa92012-04-03 11:58:42 +0100588}
589EXPORT_SYMBOL_GPL(amba_ahb_device_add);
590
Linus Walleij3ad909b2012-11-30 16:30:43 +0100591struct amba_device *
592amba_apb_device_add_res(struct device *parent, const char *name,
593 resource_size_t base, size_t size, int irq1,
594 int irq2, void *pdata, unsigned int periphid,
595 struct resource *resbase)
596{
597 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
598 periphid, 0, resbase);
599}
600EXPORT_SYMBOL_GPL(amba_apb_device_add_res);
601
602struct amba_device *
603amba_ahb_device_add_res(struct device *parent, const char *name,
604 resource_size_t base, size_t size, int irq1,
605 int irq2, void *pdata, unsigned int periphid,
606 struct resource *resbase)
607{
608 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
609 periphid, ~0ULL, resbase);
610}
611EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);
612
613
Russell Kingd5dc9272011-12-18 11:07:47 +0000614static void amba_device_initialize(struct amba_device *dev, const char *name)
615{
616 device_initialize(&dev->dev);
617 if (name)
618 dev_set_name(&dev->dev, "%s", name);
619 dev->dev.release = amba_device_release;
620 dev->dev.bus = &amba_bustype;
Russell King446b2a92013-06-27 10:25:33 +0100621 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
Russell Kingd5dc9272011-12-18 11:07:47 +0000622 dev->res.name = dev_name(&dev->dev);
623}
624
625/**
626 * amba_device_alloc - allocate an AMBA device
627 * @name: sysfs name of the AMBA device
628 * @base: base of AMBA device
629 * @size: size of AMBA device
630 *
631 * Allocate and initialize an AMBA device structure. Returns %NULL
632 * on failure.
633 */
634struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
635 size_t size)
636{
637 struct amba_device *dev;
638
639 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
640 if (dev) {
641 amba_device_initialize(dev, name);
642 dev->res.start = base;
643 dev->res.end = base + size - 1;
644 dev->res.flags = IORESOURCE_MEM;
645 }
646
647 return dev;
648}
649EXPORT_SYMBOL_GPL(amba_device_alloc);
650
651/**
652 * amba_device_register - register an AMBA device
653 * @dev: AMBA device to register
654 * @parent: parent memory resource
655 *
656 * Setup the AMBA device, reading the cell ID if present.
657 * Claim the resource, and register the AMBA device with
658 * the Linux device manager.
659 */
660int amba_device_register(struct amba_device *dev, struct resource *parent)
661{
662 amba_device_initialize(dev, dev->dev.init_name);
663 dev->dev.init_name = NULL;
664
Russell Kingd5dc9272011-12-18 11:07:47 +0000665 return amba_device_add(dev, parent);
666}
667
668/**
669 * amba_device_put - put an AMBA device
670 * @dev: AMBA device to put
671 */
672void amba_device_put(struct amba_device *dev)
673{
674 put_device(&dev->dev);
675}
676EXPORT_SYMBOL_GPL(amba_device_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678/**
679 * amba_device_unregister - unregister an AMBA device
680 * @dev: AMBA device to remove
681 *
682 * Remove the specified AMBA device from the Linux device
683 * manager. All files associated with this object will be
684 * destroyed, and device drivers notified that the device has
685 * been removed. The AMBA device's resources including
686 * the amba_device structure will be freed once all
687 * references to it have been dropped.
688 */
689void amba_device_unregister(struct amba_device *dev)
690{
691 device_unregister(&dev->dev);
692}
693
694
695struct find_data {
696 struct amba_device *dev;
697 struct device *parent;
698 const char *busid;
699 unsigned int id;
700 unsigned int mask;
701};
702
703static int amba_find_match(struct device *dev, void *data)
704{
705 struct find_data *d = data;
706 struct amba_device *pcdev = to_amba_device(dev);
707 int r;
708
709 r = (pcdev->periphid & d->mask) == d->id;
710 if (d->parent)
711 r &= d->parent == dev->parent;
712 if (d->busid)
Kay Sievers9d6b4c82009-03-24 16:38:22 -0700713 r &= strcmp(dev_name(dev), d->busid) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 if (r) {
716 get_device(dev);
717 d->dev = pcdev;
718 }
719
720 return r;
721}
722
723/**
724 * amba_find_device - locate an AMBA device given a bus id
725 * @busid: bus id for device (or NULL)
726 * @parent: parent device (or NULL)
727 * @id: peripheral ID (or 0)
728 * @mask: peripheral ID mask (or 0)
729 *
730 * Return the AMBA device corresponding to the supplied parameters.
731 * If no device matches, returns NULL.
732 *
733 * NOTE: When a valid device is found, its refcount is
734 * incremented, and must be decremented before the returned
735 * reference.
736 */
737struct amba_device *
738amba_find_device(const char *busid, struct device *parent, unsigned int id,
739 unsigned int mask)
740{
741 struct find_data data;
742
743 data.dev = NULL;
744 data.parent = parent;
745 data.busid = busid;
746 data.id = id;
747 data.mask = mask;
748
749 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
750
751 return data.dev;
752}
753
754/**
755 * amba_request_regions - request all mem regions associated with device
756 * @dev: amba_device structure for device
757 * @name: name, or NULL to use driver name
758 */
759int amba_request_regions(struct amba_device *dev, const char *name)
760{
761 int ret = 0;
Leo Chen8afe0b92009-07-28 23:34:59 +0100762 u32 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 if (!name)
765 name = dev->dev.driver->name;
766
Leo Chen8afe0b92009-07-28 23:34:59 +0100767 size = resource_size(&dev->res);
768
769 if (!request_mem_region(dev->res.start, size, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 ret = -EBUSY;
771
772 return ret;
773}
774
775/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300776 * amba_release_regions - release mem regions associated with device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 * @dev: amba_device structure for device
778 *
779 * Release regions claimed by a successful call to amba_request_regions.
780 */
781void amba_release_regions(struct amba_device *dev)
782{
Leo Chen8afe0b92009-07-28 23:34:59 +0100783 u32 size;
784
785 size = resource_size(&dev->res);
786 release_mem_region(dev->res.start, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
789EXPORT_SYMBOL(amba_driver_register);
790EXPORT_SYMBOL(amba_driver_unregister);
791EXPORT_SYMBOL(amba_device_register);
792EXPORT_SYMBOL(amba_device_unregister);
793EXPORT_SYMBOL(amba_find_device);
794EXPORT_SYMBOL(amba_request_regions);
795EXPORT_SYMBOL(amba_release_regions);