blob: af58768a039376a373c4427557eb477acf64b72e [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * linux/arch/arm/common/amba.c
4 *
5 * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006 */
7#include <linux/module.h>
8#include <linux/init.h>
9#include <linux/device.h>
10#include <linux/string.h>
11#include <linux/slab.h>
12#include <linux/io.h>
13#include <linux/pm.h>
14#include <linux/pm_runtime.h>
15#include <linux/pm_domain.h>
16#include <linux/amba/bus.h>
17#include <linux/sizes.h>
18#include <linux/limits.h>
19#include <linux/clk/clk-conf.h>
20#include <linux/platform_device.h>
David Brazdil0f672f62019-12-10 10:32:29 +000021#include <linux/reset.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000022
23#include <asm/irq.h>
24
25#define to_amba_driver(d) container_of(d, struct amba_driver, drv)
26
David Brazdil0f672f62019-12-10 10:32:29 +000027/* called on periphid match and class 0x9 coresight device. */
28static int
29amba_cs_uci_id_match(const struct amba_id *table, struct amba_device *dev)
30{
31 int ret = 0;
32 struct amba_cs_uci_id *uci;
33
34 uci = table->data;
35
36 /* no table data or zero mask - return match on periphid */
37 if (!uci || (uci->devarch_mask == 0))
38 return 1;
39
40 /* test against read devtype and masked devarch value */
41 ret = (dev->uci.devtype == uci->devtype) &&
42 ((dev->uci.devarch & uci->devarch_mask) == uci->devarch);
43 return ret;
44}
45
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000046static const struct amba_id *
47amba_lookup(const struct amba_id *table, struct amba_device *dev)
48{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000049 while (table->mask) {
David Brazdil0f672f62019-12-10 10:32:29 +000050 if (((dev->periphid & table->mask) == table->id) &&
51 ((dev->cid != CORESIGHT_CID) ||
52 (amba_cs_uci_id_match(table, dev))))
53 return table;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000054 table++;
55 }
David Brazdil0f672f62019-12-10 10:32:29 +000056 return NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000057}
58
59static int amba_match(struct device *dev, struct device_driver *drv)
60{
61 struct amba_device *pcdev = to_amba_device(dev);
62 struct amba_driver *pcdrv = to_amba_driver(drv);
63
64 /* When driver_override is set, only bind to the matching driver */
65 if (pcdev->driver_override)
66 return !strcmp(pcdev->driver_override, drv->name);
67
68 return amba_lookup(pcdrv->id_table, pcdev) != NULL;
69}
70
71static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
72{
73 struct amba_device *pcdev = to_amba_device(dev);
74 int retval = 0;
75
76 retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
77 if (retval)
78 return retval;
79
80 retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);
81 return retval;
82}
83
84static ssize_t driver_override_show(struct device *_dev,
85 struct device_attribute *attr, char *buf)
86{
87 struct amba_device *dev = to_amba_device(_dev);
88 ssize_t len;
89
90 device_lock(_dev);
91 len = sprintf(buf, "%s\n", dev->driver_override);
92 device_unlock(_dev);
93 return len;
94}
95
96static ssize_t driver_override_store(struct device *_dev,
97 struct device_attribute *attr,
98 const char *buf, size_t count)
99{
100 struct amba_device *dev = to_amba_device(_dev);
101 char *driver_override, *old, *cp;
102
103 /* We need to keep extra room for a newline */
104 if (count >= (PAGE_SIZE - 1))
105 return -EINVAL;
106
107 driver_override = kstrndup(buf, count, GFP_KERNEL);
108 if (!driver_override)
109 return -ENOMEM;
110
111 cp = strchr(driver_override, '\n');
112 if (cp)
113 *cp = '\0';
114
115 device_lock(_dev);
116 old = dev->driver_override;
117 if (strlen(driver_override)) {
118 dev->driver_override = driver_override;
119 } else {
120 kfree(driver_override);
121 dev->driver_override = NULL;
122 }
123 device_unlock(_dev);
124
125 kfree(old);
126
127 return count;
128}
129static DEVICE_ATTR_RW(driver_override);
130
131#define amba_attr_func(name,fmt,arg...) \
132static ssize_t name##_show(struct device *_dev, \
133 struct device_attribute *attr, char *buf) \
134{ \
135 struct amba_device *dev = to_amba_device(_dev); \
136 return sprintf(buf, fmt, arg); \
137} \
138static DEVICE_ATTR_RO(name)
139
140amba_attr_func(id, "%08x\n", dev->periphid);
141amba_attr_func(irq0, "%u\n", dev->irq[0]);
142amba_attr_func(irq1, "%u\n", dev->irq[1]);
143amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",
144 (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
145 dev->res.flags);
146
147static struct attribute *amba_dev_attrs[] = {
148 &dev_attr_id.attr,
149 &dev_attr_resource.attr,
150 &dev_attr_driver_override.attr,
151 NULL,
152};
153ATTRIBUTE_GROUPS(amba_dev);
154
155#ifdef CONFIG_PM
156/*
157 * Hooks to provide runtime PM of the pclk (bus clock). It is safe to
158 * enable/disable the bus clock at runtime PM suspend/resume as this
159 * does not result in loss of context.
160 */
161static int amba_pm_runtime_suspend(struct device *dev)
162{
163 struct amba_device *pcdev = to_amba_device(dev);
164 int ret = pm_generic_runtime_suspend(dev);
165
166 if (ret == 0 && dev->driver) {
167 if (pm_runtime_is_irq_safe(dev))
168 clk_disable(pcdev->pclk);
169 else
170 clk_disable_unprepare(pcdev->pclk);
171 }
172
173 return ret;
174}
175
176static int amba_pm_runtime_resume(struct device *dev)
177{
178 struct amba_device *pcdev = to_amba_device(dev);
179 int ret;
180
181 if (dev->driver) {
182 if (pm_runtime_is_irq_safe(dev))
183 ret = clk_enable(pcdev->pclk);
184 else
185 ret = clk_prepare_enable(pcdev->pclk);
186 /* Failure is probably fatal to the system, but... */
187 if (ret)
188 return ret;
189 }
190
191 return pm_generic_runtime_resume(dev);
192}
193#endif /* CONFIG_PM */
194
195static const struct dev_pm_ops amba_pm = {
196 .suspend = pm_generic_suspend,
197 .resume = pm_generic_resume,
198 .freeze = pm_generic_freeze,
199 .thaw = pm_generic_thaw,
200 .poweroff = pm_generic_poweroff,
201 .restore = pm_generic_restore,
202 SET_RUNTIME_PM_OPS(
203 amba_pm_runtime_suspend,
204 amba_pm_runtime_resume,
205 NULL
206 )
207};
208
209/*
210 * Primecells are part of the Advanced Microcontroller Bus Architecture,
211 * so we call the bus "amba".
212 * DMA configuration for platform and AMBA bus is same. So here we reuse
213 * platform's DMA config routine.
214 */
215struct bus_type amba_bustype = {
216 .name = "amba",
217 .dev_groups = amba_dev_groups,
218 .match = amba_match,
219 .uevent = amba_uevent,
220 .dma_configure = platform_dma_configure,
221 .pm = &amba_pm,
222};
223EXPORT_SYMBOL_GPL(amba_bustype);
224
225static int __init amba_init(void)
226{
227 return bus_register(&amba_bustype);
228}
229
230postcore_initcall(amba_init);
231
232static int amba_get_enable_pclk(struct amba_device *pcdev)
233{
234 int ret;
235
236 pcdev->pclk = clk_get(&pcdev->dev, "apb_pclk");
237 if (IS_ERR(pcdev->pclk))
238 return PTR_ERR(pcdev->pclk);
239
240 ret = clk_prepare_enable(pcdev->pclk);
241 if (ret)
242 clk_put(pcdev->pclk);
243
244 return ret;
245}
246
247static void amba_put_disable_pclk(struct amba_device *pcdev)
248{
249 clk_disable_unprepare(pcdev->pclk);
250 clk_put(pcdev->pclk);
251}
252
253/*
254 * These are the device model conversion veneers; they convert the
255 * device model structures to our more specific structures.
256 */
257static int amba_probe(struct device *dev)
258{
259 struct amba_device *pcdev = to_amba_device(dev);
260 struct amba_driver *pcdrv = to_amba_driver(dev->driver);
261 const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
262 int ret;
263
264 do {
265 ret = of_clk_set_defaults(dev->of_node, false);
266 if (ret < 0)
267 break;
268
269 ret = dev_pm_domain_attach(dev, true);
270 if (ret)
271 break;
272
273 ret = amba_get_enable_pclk(pcdev);
274 if (ret) {
275 dev_pm_domain_detach(dev, true);
276 break;
277 }
278
279 pm_runtime_get_noresume(dev);
280 pm_runtime_set_active(dev);
281 pm_runtime_enable(dev);
282
283 ret = pcdrv->probe(pcdev, id);
284 if (ret == 0)
285 break;
286
287 pm_runtime_disable(dev);
288 pm_runtime_set_suspended(dev);
289 pm_runtime_put_noidle(dev);
290
291 amba_put_disable_pclk(pcdev);
292 dev_pm_domain_detach(dev, true);
293 } while (0);
294
295 return ret;
296}
297
298static int amba_remove(struct device *dev)
299{
300 struct amba_device *pcdev = to_amba_device(dev);
301 struct amba_driver *drv = to_amba_driver(dev->driver);
Olivier Deprez0e641232021-09-23 10:07:05 +0200302 int ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000303
304 pm_runtime_get_sync(dev);
Olivier Deprez0e641232021-09-23 10:07:05 +0200305 if (drv->remove)
306 ret = drv->remove(pcdev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000307 pm_runtime_put_noidle(dev);
308
309 /* Undo the runtime PM settings in amba_probe() */
310 pm_runtime_disable(dev);
311 pm_runtime_set_suspended(dev);
312 pm_runtime_put_noidle(dev);
313
314 amba_put_disable_pclk(pcdev);
315 dev_pm_domain_detach(dev, true);
316
317 return ret;
318}
319
320static void amba_shutdown(struct device *dev)
321{
322 struct amba_driver *drv = to_amba_driver(dev->driver);
Olivier Deprez0e641232021-09-23 10:07:05 +0200323
324 if (drv->shutdown)
325 drv->shutdown(to_amba_device(dev));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000326}
327
328/**
329 * amba_driver_register - register an AMBA device driver
330 * @drv: amba device driver structure
331 *
332 * Register an AMBA device driver with the Linux device model
333 * core. If devices pre-exist, the drivers probe function will
334 * be called.
335 */
336int amba_driver_register(struct amba_driver *drv)
337{
Olivier Deprez0e641232021-09-23 10:07:05 +0200338 if (!drv->probe)
339 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000340
Olivier Deprez0e641232021-09-23 10:07:05 +0200341 drv->drv.bus = &amba_bustype;
342 drv->drv.probe = amba_probe;
343 drv->drv.remove = amba_remove;
344 drv->drv.shutdown = amba_shutdown;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000345
346 return driver_register(&drv->drv);
347}
348
349/**
350 * amba_driver_unregister - remove an AMBA device driver
351 * @drv: AMBA device driver structure to remove
352 *
353 * Unregister an AMBA device driver from the Linux device
354 * model. The device model will call the drivers remove function
355 * for each device the device driver is currently handling.
356 */
357void amba_driver_unregister(struct amba_driver *drv)
358{
359 driver_unregister(&drv->drv);
360}
361
362
363static void amba_device_release(struct device *dev)
364{
365 struct amba_device *d = to_amba_device(dev);
366
367 if (d->res.parent)
368 release_resource(&d->res);
369 kfree(d);
370}
371
372static int amba_device_try_add(struct amba_device *dev, struct resource *parent)
373{
374 u32 size;
375 void __iomem *tmp;
376 int i, ret;
377
378 WARN_ON(dev->irq[0] == (unsigned int)-1);
379 WARN_ON(dev->irq[1] == (unsigned int)-1);
380
381 ret = request_resource(parent, &dev->res);
382 if (ret)
383 goto err_out;
384
385 /* Hard-coded primecell ID instead of plug-n-play */
386 if (dev->periphid != 0)
387 goto skip_probe;
388
389 /*
390 * Dynamically calculate the size of the resource
391 * and use this for iomap
392 */
393 size = resource_size(&dev->res);
394 tmp = ioremap(dev->res.start, size);
395 if (!tmp) {
396 ret = -ENOMEM;
397 goto err_release;
398 }
399
400 ret = dev_pm_domain_attach(&dev->dev, true);
401 if (ret) {
402 iounmap(tmp);
403 goto err_release;
404 }
405
406 ret = amba_get_enable_pclk(dev);
407 if (ret == 0) {
408 u32 pid, cid;
David Brazdil0f672f62019-12-10 10:32:29 +0000409 struct reset_control *rstc;
410
411 /*
412 * Find reset control(s) of the amba bus and de-assert them.
413 */
414 rstc = of_reset_control_array_get_optional_shared(dev->dev.of_node);
415 if (IS_ERR(rstc)) {
416 ret = PTR_ERR(rstc);
417 if (ret != -EPROBE_DEFER)
418 dev_err(&dev->dev, "can't get reset: %d\n",
419 ret);
420 goto err_reset;
421 }
422 reset_control_deassert(rstc);
423 reset_control_put(rstc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000424
425 /*
426 * Read pid and cid based on size of resource
427 * they are located at end of region
428 */
429 for (pid = 0, i = 0; i < 4; i++)
430 pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
431 (i * 8);
432 for (cid = 0, i = 0; i < 4; i++)
433 cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
434 (i * 8);
435
David Brazdil0f672f62019-12-10 10:32:29 +0000436 if (cid == CORESIGHT_CID) {
437 /* set the base to the start of the last 4k block */
438 void __iomem *csbase = tmp + size - 4096;
439
440 dev->uci.devarch =
441 readl(csbase + UCI_REG_DEVARCH_OFFSET);
442 dev->uci.devtype =
443 readl(csbase + UCI_REG_DEVTYPE_OFFSET) & 0xff;
444 }
445
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000446 amba_put_disable_pclk(dev);
447
David Brazdil0f672f62019-12-10 10:32:29 +0000448 if (cid == AMBA_CID || cid == CORESIGHT_CID) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000449 dev->periphid = pid;
David Brazdil0f672f62019-12-10 10:32:29 +0000450 dev->cid = cid;
451 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000452
453 if (!dev->periphid)
454 ret = -ENODEV;
455 }
456
457 iounmap(tmp);
458 dev_pm_domain_detach(&dev->dev, true);
459
460 if (ret)
461 goto err_release;
462
463 skip_probe:
464 ret = device_add(&dev->dev);
465 if (ret)
466 goto err_release;
467
468 if (dev->irq[0])
469 ret = device_create_file(&dev->dev, &dev_attr_irq0);
470 if (ret == 0 && dev->irq[1])
471 ret = device_create_file(&dev->dev, &dev_attr_irq1);
472 if (ret == 0)
473 return ret;
474
475 device_unregister(&dev->dev);
476
477 err_release:
478 release_resource(&dev->res);
479 err_out:
480 return ret;
David Brazdil0f672f62019-12-10 10:32:29 +0000481
482 err_reset:
483 amba_put_disable_pclk(dev);
484 iounmap(tmp);
485 dev_pm_domain_detach(&dev->dev, true);
486 goto err_release;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000487}
488
489/*
490 * Registration of AMBA device require reading its pid and cid registers.
491 * To do this, the device must be turned on (if it is a part of power domain)
492 * and have clocks enabled. However in some cases those resources might not be
493 * yet available. Returning EPROBE_DEFER is not a solution in such case,
494 * because callers don't handle this special error code. Instead such devices
495 * are added to the special list and their registration is retried from
496 * periodic worker, until all resources are available and registration succeeds.
497 */
498struct deferred_device {
499 struct amba_device *dev;
500 struct resource *parent;
501 struct list_head node;
502};
503
504static LIST_HEAD(deferred_devices);
505static DEFINE_MUTEX(deferred_devices_lock);
506
507static void amba_deferred_retry_func(struct work_struct *dummy);
508static DECLARE_DELAYED_WORK(deferred_retry_work, amba_deferred_retry_func);
509
510#define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000))
511
512static void amba_deferred_retry_func(struct work_struct *dummy)
513{
514 struct deferred_device *ddev, *tmp;
515
516 mutex_lock(&deferred_devices_lock);
517
518 list_for_each_entry_safe(ddev, tmp, &deferred_devices, node) {
519 int ret = amba_device_try_add(ddev->dev, ddev->parent);
520
521 if (ret == -EPROBE_DEFER)
522 continue;
523
524 list_del_init(&ddev->node);
525 kfree(ddev);
526 }
527
528 if (!list_empty(&deferred_devices))
529 schedule_delayed_work(&deferred_retry_work,
530 DEFERRED_DEVICE_TIMEOUT);
531
532 mutex_unlock(&deferred_devices_lock);
533}
534
535/**
536 * amba_device_add - add a previously allocated AMBA device structure
537 * @dev: AMBA device allocated by amba_device_alloc
538 * @parent: resource parent for this devices resources
539 *
540 * Claim the resource, and read the device cell ID if not already
541 * initialized. Register the AMBA device with the Linux device
542 * manager.
543 */
544int amba_device_add(struct amba_device *dev, struct resource *parent)
545{
546 int ret = amba_device_try_add(dev, parent);
547
548 if (ret == -EPROBE_DEFER) {
549 struct deferred_device *ddev;
550
551 ddev = kmalloc(sizeof(*ddev), GFP_KERNEL);
552 if (!ddev)
553 return -ENOMEM;
554
555 ddev->dev = dev;
556 ddev->parent = parent;
557 ret = 0;
558
559 mutex_lock(&deferred_devices_lock);
560
561 if (list_empty(&deferred_devices))
562 schedule_delayed_work(&deferred_retry_work,
563 DEFERRED_DEVICE_TIMEOUT);
564 list_add_tail(&ddev->node, &deferred_devices);
565
566 mutex_unlock(&deferred_devices_lock);
567 }
568 return ret;
569}
570EXPORT_SYMBOL_GPL(amba_device_add);
571
572static struct amba_device *
573amba_aphb_device_add(struct device *parent, const char *name,
574 resource_size_t base, size_t size, int irq1, int irq2,
575 void *pdata, unsigned int periphid, u64 dma_mask,
576 struct resource *resbase)
577{
578 struct amba_device *dev;
579 int ret;
580
581 dev = amba_device_alloc(name, base, size);
582 if (!dev)
583 return ERR_PTR(-ENOMEM);
584
585 dev->dev.coherent_dma_mask = dma_mask;
586 dev->irq[0] = irq1;
587 dev->irq[1] = irq2;
588 dev->periphid = periphid;
589 dev->dev.platform_data = pdata;
590 dev->dev.parent = parent;
591
592 ret = amba_device_add(dev, resbase);
593 if (ret) {
594 amba_device_put(dev);
595 return ERR_PTR(ret);
596 }
597
598 return dev;
599}
600
601struct amba_device *
602amba_apb_device_add(struct device *parent, const char *name,
603 resource_size_t base, size_t size, int irq1, int irq2,
604 void *pdata, unsigned int periphid)
605{
606 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
607 periphid, 0, &iomem_resource);
608}
609EXPORT_SYMBOL_GPL(amba_apb_device_add);
610
611struct amba_device *
612amba_ahb_device_add(struct device *parent, const char *name,
613 resource_size_t base, size_t size, int irq1, int irq2,
614 void *pdata, unsigned int periphid)
615{
616 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
617 periphid, ~0ULL, &iomem_resource);
618}
619EXPORT_SYMBOL_GPL(amba_ahb_device_add);
620
621struct amba_device *
622amba_apb_device_add_res(struct device *parent, const char *name,
623 resource_size_t base, size_t size, int irq1,
624 int irq2, void *pdata, unsigned int periphid,
625 struct resource *resbase)
626{
627 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
628 periphid, 0, resbase);
629}
630EXPORT_SYMBOL_GPL(amba_apb_device_add_res);
631
632struct amba_device *
633amba_ahb_device_add_res(struct device *parent, const char *name,
634 resource_size_t base, size_t size, int irq1,
635 int irq2, void *pdata, unsigned int periphid,
636 struct resource *resbase)
637{
638 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
639 periphid, ~0ULL, resbase);
640}
641EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);
642
643
644static void amba_device_initialize(struct amba_device *dev, const char *name)
645{
646 device_initialize(&dev->dev);
647 if (name)
648 dev_set_name(&dev->dev, "%s", name);
649 dev->dev.release = amba_device_release;
650 dev->dev.bus = &amba_bustype;
651 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
652 dev->res.name = dev_name(&dev->dev);
653}
654
655/**
656 * amba_device_alloc - allocate an AMBA device
657 * @name: sysfs name of the AMBA device
658 * @base: base of AMBA device
659 * @size: size of AMBA device
660 *
661 * Allocate and initialize an AMBA device structure. Returns %NULL
662 * on failure.
663 */
664struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
665 size_t size)
666{
667 struct amba_device *dev;
668
669 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
670 if (dev) {
671 amba_device_initialize(dev, name);
672 dev->res.start = base;
673 dev->res.end = base + size - 1;
674 dev->res.flags = IORESOURCE_MEM;
675 }
676
677 return dev;
678}
679EXPORT_SYMBOL_GPL(amba_device_alloc);
680
681/**
682 * amba_device_register - register an AMBA device
683 * @dev: AMBA device to register
684 * @parent: parent memory resource
685 *
686 * Setup the AMBA device, reading the cell ID if present.
687 * Claim the resource, and register the AMBA device with
688 * the Linux device manager.
689 */
690int amba_device_register(struct amba_device *dev, struct resource *parent)
691{
692 amba_device_initialize(dev, dev->dev.init_name);
693 dev->dev.init_name = NULL;
694
695 return amba_device_add(dev, parent);
696}
697
698/**
699 * amba_device_put - put an AMBA device
700 * @dev: AMBA device to put
701 */
702void amba_device_put(struct amba_device *dev)
703{
704 put_device(&dev->dev);
705}
706EXPORT_SYMBOL_GPL(amba_device_put);
707
708/**
709 * amba_device_unregister - unregister an AMBA device
710 * @dev: AMBA device to remove
711 *
712 * Remove the specified AMBA device from the Linux device
713 * manager. All files associated with this object will be
714 * destroyed, and device drivers notified that the device has
715 * been removed. The AMBA device's resources including
716 * the amba_device structure will be freed once all
717 * references to it have been dropped.
718 */
719void amba_device_unregister(struct amba_device *dev)
720{
721 device_unregister(&dev->dev);
722}
723
724
725struct find_data {
726 struct amba_device *dev;
727 struct device *parent;
728 const char *busid;
729 unsigned int id;
730 unsigned int mask;
731};
732
733static int amba_find_match(struct device *dev, void *data)
734{
735 struct find_data *d = data;
736 struct amba_device *pcdev = to_amba_device(dev);
737 int r;
738
739 r = (pcdev->periphid & d->mask) == d->id;
740 if (d->parent)
741 r &= d->parent == dev->parent;
742 if (d->busid)
743 r &= strcmp(dev_name(dev), d->busid) == 0;
744
745 if (r) {
746 get_device(dev);
747 d->dev = pcdev;
748 }
749
750 return r;
751}
752
753/**
754 * amba_find_device - locate an AMBA device given a bus id
755 * @busid: bus id for device (or NULL)
756 * @parent: parent device (or NULL)
757 * @id: peripheral ID (or 0)
758 * @mask: peripheral ID mask (or 0)
759 *
760 * Return the AMBA device corresponding to the supplied parameters.
761 * If no device matches, returns NULL.
762 *
763 * NOTE: When a valid device is found, its refcount is
764 * incremented, and must be decremented before the returned
765 * reference.
766 */
767struct amba_device *
768amba_find_device(const char *busid, struct device *parent, unsigned int id,
769 unsigned int mask)
770{
771 struct find_data data;
772
773 data.dev = NULL;
774 data.parent = parent;
775 data.busid = busid;
776 data.id = id;
777 data.mask = mask;
778
779 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
780
781 return data.dev;
782}
783
784/**
785 * amba_request_regions - request all mem regions associated with device
786 * @dev: amba_device structure for device
787 * @name: name, or NULL to use driver name
788 */
789int amba_request_regions(struct amba_device *dev, const char *name)
790{
791 int ret = 0;
792 u32 size;
793
794 if (!name)
795 name = dev->dev.driver->name;
796
797 size = resource_size(&dev->res);
798
799 if (!request_mem_region(dev->res.start, size, name))
800 ret = -EBUSY;
801
802 return ret;
803}
804
805/**
806 * amba_release_regions - release mem regions associated with device
807 * @dev: amba_device structure for device
808 *
809 * Release regions claimed by a successful call to amba_request_regions.
810 */
811void amba_release_regions(struct amba_device *dev)
812{
813 u32 size;
814
815 size = resource_size(&dev->res);
816 release_mem_region(dev->res.start, size);
817}
818
819EXPORT_SYMBOL(amba_driver_register);
820EXPORT_SYMBOL(amba_driver_unregister);
821EXPORT_SYMBOL(amba_device_register);
822EXPORT_SYMBOL(amba_device_unregister);
823EXPORT_SYMBOL(amba_find_device);
824EXPORT_SYMBOL(amba_request_regions);
825EXPORT_SYMBOL(amba_release_regions);