1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/arch/arm/common/amba.c
5 * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
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>
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>
24 #define to_amba_driver(d) container_of(d, struct amba_driver, drv)
26 /* called on periphid match and class 0x9 coresight device. */
28 amba_cs_uci_id_match(const struct amba_id
*table
, struct amba_device
*dev
)
31 struct amba_cs_uci_id
*uci
;
35 /* no table data or zero mask - return match on periphid */
36 if (!uci
|| (uci
->devarch_mask
== 0))
39 /* test against read devtype and masked devarch value */
40 ret
= (dev
->uci
.devtype
== uci
->devtype
) &&
41 ((dev
->uci
.devarch
& uci
->devarch_mask
) == uci
->devarch
);
45 static const struct amba_id
*
46 amba_lookup(const struct amba_id
*table
, struct amba_device
*dev
)
49 if (((dev
->periphid
& table
->mask
) == table
->id
) &&
50 ((dev
->cid
!= CORESIGHT_CID
) ||
51 (amba_cs_uci_id_match(table
, dev
))))
58 static int amba_match(struct device
*dev
, struct device_driver
*drv
)
60 struct amba_device
*pcdev
= to_amba_device(dev
);
61 struct amba_driver
*pcdrv
= to_amba_driver(drv
);
63 /* When driver_override is set, only bind to the matching driver */
64 if (pcdev
->driver_override
)
65 return !strcmp(pcdev
->driver_override
, drv
->name
);
67 return amba_lookup(pcdrv
->id_table
, pcdev
) != NULL
;
70 static int amba_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
72 struct amba_device
*pcdev
= to_amba_device(dev
);
75 retval
= add_uevent_var(env
, "AMBA_ID=%08x", pcdev
->periphid
);
79 retval
= add_uevent_var(env
, "MODALIAS=amba:d%08X", pcdev
->periphid
);
83 static ssize_t
driver_override_show(struct device
*_dev
,
84 struct device_attribute
*attr
, char *buf
)
86 struct amba_device
*dev
= to_amba_device(_dev
);
90 len
= sprintf(buf
, "%s\n", dev
->driver_override
);
95 static ssize_t
driver_override_store(struct device
*_dev
,
96 struct device_attribute
*attr
,
97 const char *buf
, size_t count
)
99 struct amba_device
*dev
= to_amba_device(_dev
);
100 char *driver_override
, *old
, *cp
;
102 /* We need to keep extra room for a newline */
103 if (count
>= (PAGE_SIZE
- 1))
106 driver_override
= kstrndup(buf
, count
, GFP_KERNEL
);
107 if (!driver_override
)
110 cp
= strchr(driver_override
, '\n');
115 old
= dev
->driver_override
;
116 if (strlen(driver_override
)) {
117 dev
->driver_override
= driver_override
;
119 kfree(driver_override
);
120 dev
->driver_override
= NULL
;
128 static DEVICE_ATTR_RW(driver_override
);
130 #define amba_attr_func(name,fmt,arg...) \
131 static ssize_t name##_show(struct device *_dev, \
132 struct device_attribute *attr, char *buf) \
134 struct amba_device *dev = to_amba_device(_dev); \
135 return sprintf(buf, fmt, arg); \
137 static DEVICE_ATTR_RO(name)
139 amba_attr_func(id
, "%08x\n", dev
->periphid
);
140 amba_attr_func(irq0
, "%u\n", dev
->irq
[0]);
141 amba_attr_func(irq1
, "%u\n", dev
->irq
[1]);
142 amba_attr_func(resource
, "\t%016llx\t%016llx\t%016lx\n",
143 (unsigned long long)dev
->res
.start
, (unsigned long long)dev
->res
.end
,
146 static struct attribute
*amba_dev_attrs
[] = {
148 &dev_attr_resource
.attr
,
149 &dev_attr_driver_override
.attr
,
152 ATTRIBUTE_GROUPS(amba_dev
);
156 * Hooks to provide runtime PM of the pclk (bus clock). It is safe to
157 * enable/disable the bus clock at runtime PM suspend/resume as this
158 * does not result in loss of context.
160 static int amba_pm_runtime_suspend(struct device
*dev
)
162 struct amba_device
*pcdev
= to_amba_device(dev
);
163 int ret
= pm_generic_runtime_suspend(dev
);
165 if (ret
== 0 && dev
->driver
) {
166 if (pm_runtime_is_irq_safe(dev
))
167 clk_disable(pcdev
->pclk
);
169 clk_disable_unprepare(pcdev
->pclk
);
175 static int amba_pm_runtime_resume(struct device
*dev
)
177 struct amba_device
*pcdev
= to_amba_device(dev
);
181 if (pm_runtime_is_irq_safe(dev
))
182 ret
= clk_enable(pcdev
->pclk
);
184 ret
= clk_prepare_enable(pcdev
->pclk
);
185 /* Failure is probably fatal to the system, but... */
190 return pm_generic_runtime_resume(dev
);
192 #endif /* CONFIG_PM */
194 static const struct dev_pm_ops amba_pm
= {
195 .suspend
= pm_generic_suspend
,
196 .resume
= pm_generic_resume
,
197 .freeze
= pm_generic_freeze
,
198 .thaw
= pm_generic_thaw
,
199 .poweroff
= pm_generic_poweroff
,
200 .restore
= pm_generic_restore
,
202 amba_pm_runtime_suspend
,
203 amba_pm_runtime_resume
,
209 * Primecells are part of the Advanced Microcontroller Bus Architecture,
210 * so we call the bus "amba".
211 * DMA configuration for platform and AMBA bus is same. So here we reuse
212 * platform's DMA config routine.
214 struct bus_type amba_bustype
= {
216 .dev_groups
= amba_dev_groups
,
218 .uevent
= amba_uevent
,
219 .dma_configure
= platform_dma_configure
,
222 EXPORT_SYMBOL_GPL(amba_bustype
);
224 static int __init
amba_init(void)
226 return bus_register(&amba_bustype
);
229 postcore_initcall(amba_init
);
231 static int amba_get_enable_pclk(struct amba_device
*pcdev
)
235 pcdev
->pclk
= clk_get(&pcdev
->dev
, "apb_pclk");
236 if (IS_ERR(pcdev
->pclk
))
237 return PTR_ERR(pcdev
->pclk
);
239 ret
= clk_prepare_enable(pcdev
->pclk
);
241 clk_put(pcdev
->pclk
);
246 static void amba_put_disable_pclk(struct amba_device
*pcdev
)
248 clk_disable_unprepare(pcdev
->pclk
);
249 clk_put(pcdev
->pclk
);
253 * These are the device model conversion veneers; they convert the
254 * device model structures to our more specific structures.
256 static int amba_probe(struct device
*dev
)
258 struct amba_device
*pcdev
= to_amba_device(dev
);
259 struct amba_driver
*pcdrv
= to_amba_driver(dev
->driver
);
260 const struct amba_id
*id
= amba_lookup(pcdrv
->id_table
, pcdev
);
264 ret
= of_clk_set_defaults(dev
->of_node
, false);
268 ret
= dev_pm_domain_attach(dev
, true);
272 ret
= amba_get_enable_pclk(pcdev
);
274 dev_pm_domain_detach(dev
, true);
278 pm_runtime_get_noresume(dev
);
279 pm_runtime_set_active(dev
);
280 pm_runtime_enable(dev
);
282 ret
= pcdrv
->probe(pcdev
, id
);
286 pm_runtime_disable(dev
);
287 pm_runtime_set_suspended(dev
);
288 pm_runtime_put_noidle(dev
);
290 amba_put_disable_pclk(pcdev
);
291 dev_pm_domain_detach(dev
, true);
297 static int amba_remove(struct device
*dev
)
299 struct amba_device
*pcdev
= to_amba_device(dev
);
300 struct amba_driver
*drv
= to_amba_driver(dev
->driver
);
303 pm_runtime_get_sync(dev
);
304 ret
= drv
->remove(pcdev
);
305 pm_runtime_put_noidle(dev
);
307 /* Undo the runtime PM settings in amba_probe() */
308 pm_runtime_disable(dev
);
309 pm_runtime_set_suspended(dev
);
310 pm_runtime_put_noidle(dev
);
312 amba_put_disable_pclk(pcdev
);
313 dev_pm_domain_detach(dev
, true);
318 static void amba_shutdown(struct device
*dev
)
320 struct amba_driver
*drv
= to_amba_driver(dev
->driver
);
321 drv
->shutdown(to_amba_device(dev
));
325 * amba_driver_register - register an AMBA device driver
326 * @drv: amba device driver structure
328 * Register an AMBA device driver with the Linux device model
329 * core. If devices pre-exist, the drivers probe function will
332 int amba_driver_register(struct amba_driver
*drv
)
334 drv
->drv
.bus
= &amba_bustype
;
336 #define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
341 return driver_register(&drv
->drv
);
345 * amba_driver_unregister - remove an AMBA device driver
346 * @drv: AMBA device driver structure to remove
348 * Unregister an AMBA device driver from the Linux device
349 * model. The device model will call the drivers remove function
350 * for each device the device driver is currently handling.
352 void amba_driver_unregister(struct amba_driver
*drv
)
354 driver_unregister(&drv
->drv
);
358 static void amba_device_release(struct device
*dev
)
360 struct amba_device
*d
= to_amba_device(dev
);
363 release_resource(&d
->res
);
367 static int amba_device_try_add(struct amba_device
*dev
, struct resource
*parent
)
373 WARN_ON(dev
->irq
[0] == (unsigned int)-1);
374 WARN_ON(dev
->irq
[1] == (unsigned int)-1);
376 ret
= request_resource(parent
, &dev
->res
);
380 /* Hard-coded primecell ID instead of plug-n-play */
381 if (dev
->periphid
!= 0)
385 * Dynamically calculate the size of the resource
386 * and use this for iomap
388 size
= resource_size(&dev
->res
);
389 tmp
= ioremap(dev
->res
.start
, size
);
395 ret
= dev_pm_domain_attach(&dev
->dev
, true);
401 ret
= amba_get_enable_pclk(dev
);
406 * Read pid and cid based on size of resource
407 * they are located at end of region
409 for (pid
= 0, i
= 0; i
< 4; i
++)
410 pid
|= (readl(tmp
+ size
- 0x20 + 4 * i
) & 255) <<
412 for (cid
= 0, i
= 0; i
< 4; i
++)
413 cid
|= (readl(tmp
+ size
- 0x10 + 4 * i
) & 255) <<
416 if (cid
== CORESIGHT_CID
) {
417 /* set the base to the start of the last 4k block */
418 void __iomem
*csbase
= tmp
+ size
- 4096;
421 readl(csbase
+ UCI_REG_DEVARCH_OFFSET
);
423 readl(csbase
+ UCI_REG_DEVTYPE_OFFSET
) & 0xff;
426 amba_put_disable_pclk(dev
);
428 if (cid
== AMBA_CID
|| cid
== CORESIGHT_CID
) {
438 dev_pm_domain_detach(&dev
->dev
, true);
444 ret
= device_add(&dev
->dev
);
449 ret
= device_create_file(&dev
->dev
, &dev_attr_irq0
);
450 if (ret
== 0 && dev
->irq
[1])
451 ret
= device_create_file(&dev
->dev
, &dev_attr_irq1
);
455 device_unregister(&dev
->dev
);
458 release_resource(&dev
->res
);
464 * Registration of AMBA device require reading its pid and cid registers.
465 * To do this, the device must be turned on (if it is a part of power domain)
466 * and have clocks enabled. However in some cases those resources might not be
467 * yet available. Returning EPROBE_DEFER is not a solution in such case,
468 * because callers don't handle this special error code. Instead such devices
469 * are added to the special list and their registration is retried from
470 * periodic worker, until all resources are available and registration succeeds.
472 struct deferred_device
{
473 struct amba_device
*dev
;
474 struct resource
*parent
;
475 struct list_head node
;
478 static LIST_HEAD(deferred_devices
);
479 static DEFINE_MUTEX(deferred_devices_lock
);
481 static void amba_deferred_retry_func(struct work_struct
*dummy
);
482 static DECLARE_DELAYED_WORK(deferred_retry_work
, amba_deferred_retry_func
);
484 #define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000))
486 static void amba_deferred_retry_func(struct work_struct
*dummy
)
488 struct deferred_device
*ddev
, *tmp
;
490 mutex_lock(&deferred_devices_lock
);
492 list_for_each_entry_safe(ddev
, tmp
, &deferred_devices
, node
) {
493 int ret
= amba_device_try_add(ddev
->dev
, ddev
->parent
);
495 if (ret
== -EPROBE_DEFER
)
498 list_del_init(&ddev
->node
);
502 if (!list_empty(&deferred_devices
))
503 schedule_delayed_work(&deferred_retry_work
,
504 DEFERRED_DEVICE_TIMEOUT
);
506 mutex_unlock(&deferred_devices_lock
);
510 * amba_device_add - add a previously allocated AMBA device structure
511 * @dev: AMBA device allocated by amba_device_alloc
512 * @parent: resource parent for this devices resources
514 * Claim the resource, and read the device cell ID if not already
515 * initialized. Register the AMBA device with the Linux device
518 int amba_device_add(struct amba_device
*dev
, struct resource
*parent
)
520 int ret
= amba_device_try_add(dev
, parent
);
522 if (ret
== -EPROBE_DEFER
) {
523 struct deferred_device
*ddev
;
525 ddev
= kmalloc(sizeof(*ddev
), GFP_KERNEL
);
530 ddev
->parent
= parent
;
533 mutex_lock(&deferred_devices_lock
);
535 if (list_empty(&deferred_devices
))
536 schedule_delayed_work(&deferred_retry_work
,
537 DEFERRED_DEVICE_TIMEOUT
);
538 list_add_tail(&ddev
->node
, &deferred_devices
);
540 mutex_unlock(&deferred_devices_lock
);
544 EXPORT_SYMBOL_GPL(amba_device_add
);
546 static struct amba_device
*
547 amba_aphb_device_add(struct device
*parent
, const char *name
,
548 resource_size_t base
, size_t size
, int irq1
, int irq2
,
549 void *pdata
, unsigned int periphid
, u64 dma_mask
,
550 struct resource
*resbase
)
552 struct amba_device
*dev
;
555 dev
= amba_device_alloc(name
, base
, size
);
557 return ERR_PTR(-ENOMEM
);
559 dev
->dev
.coherent_dma_mask
= dma_mask
;
562 dev
->periphid
= periphid
;
563 dev
->dev
.platform_data
= pdata
;
564 dev
->dev
.parent
= parent
;
566 ret
= amba_device_add(dev
, resbase
);
568 amba_device_put(dev
);
576 amba_apb_device_add(struct device
*parent
, const char *name
,
577 resource_size_t base
, size_t size
, int irq1
, int irq2
,
578 void *pdata
, unsigned int periphid
)
580 return amba_aphb_device_add(parent
, name
, base
, size
, irq1
, irq2
, pdata
,
581 periphid
, 0, &iomem_resource
);
583 EXPORT_SYMBOL_GPL(amba_apb_device_add
);
586 amba_ahb_device_add(struct device
*parent
, const char *name
,
587 resource_size_t base
, size_t size
, int irq1
, int irq2
,
588 void *pdata
, unsigned int periphid
)
590 return amba_aphb_device_add(parent
, name
, base
, size
, irq1
, irq2
, pdata
,
591 periphid
, ~0ULL, &iomem_resource
);
593 EXPORT_SYMBOL_GPL(amba_ahb_device_add
);
596 amba_apb_device_add_res(struct device
*parent
, const char *name
,
597 resource_size_t base
, size_t size
, int irq1
,
598 int irq2
, void *pdata
, unsigned int periphid
,
599 struct resource
*resbase
)
601 return amba_aphb_device_add(parent
, name
, base
, size
, irq1
, irq2
, pdata
,
602 periphid
, 0, resbase
);
604 EXPORT_SYMBOL_GPL(amba_apb_device_add_res
);
607 amba_ahb_device_add_res(struct device
*parent
, const char *name
,
608 resource_size_t base
, size_t size
, int irq1
,
609 int irq2
, void *pdata
, unsigned int periphid
,
610 struct resource
*resbase
)
612 return amba_aphb_device_add(parent
, name
, base
, size
, irq1
, irq2
, pdata
,
613 periphid
, ~0ULL, resbase
);
615 EXPORT_SYMBOL_GPL(amba_ahb_device_add_res
);
618 static void amba_device_initialize(struct amba_device
*dev
, const char *name
)
620 device_initialize(&dev
->dev
);
622 dev_set_name(&dev
->dev
, "%s", name
);
623 dev
->dev
.release
= amba_device_release
;
624 dev
->dev
.bus
= &amba_bustype
;
625 dev
->dev
.dma_mask
= &dev
->dev
.coherent_dma_mask
;
626 dev
->res
.name
= dev_name(&dev
->dev
);
630 * amba_device_alloc - allocate an AMBA device
631 * @name: sysfs name of the AMBA device
632 * @base: base of AMBA device
633 * @size: size of AMBA device
635 * Allocate and initialize an AMBA device structure. Returns %NULL
638 struct amba_device
*amba_device_alloc(const char *name
, resource_size_t base
,
641 struct amba_device
*dev
;
643 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
645 amba_device_initialize(dev
, name
);
646 dev
->res
.start
= base
;
647 dev
->res
.end
= base
+ size
- 1;
648 dev
->res
.flags
= IORESOURCE_MEM
;
653 EXPORT_SYMBOL_GPL(amba_device_alloc
);
656 * amba_device_register - register an AMBA device
657 * @dev: AMBA device to register
658 * @parent: parent memory resource
660 * Setup the AMBA device, reading the cell ID if present.
661 * Claim the resource, and register the AMBA device with
662 * the Linux device manager.
664 int amba_device_register(struct amba_device
*dev
, struct resource
*parent
)
666 amba_device_initialize(dev
, dev
->dev
.init_name
);
667 dev
->dev
.init_name
= NULL
;
669 return amba_device_add(dev
, parent
);
673 * amba_device_put - put an AMBA device
674 * @dev: AMBA device to put
676 void amba_device_put(struct amba_device
*dev
)
678 put_device(&dev
->dev
);
680 EXPORT_SYMBOL_GPL(amba_device_put
);
683 * amba_device_unregister - unregister an AMBA device
684 * @dev: AMBA device to remove
686 * Remove the specified AMBA device from the Linux device
687 * manager. All files associated with this object will be
688 * destroyed, and device drivers notified that the device has
689 * been removed. The AMBA device's resources including
690 * the amba_device structure will be freed once all
691 * references to it have been dropped.
693 void amba_device_unregister(struct amba_device
*dev
)
695 device_unregister(&dev
->dev
);
700 struct amba_device
*dev
;
701 struct device
*parent
;
707 static int amba_find_match(struct device
*dev
, void *data
)
709 struct find_data
*d
= data
;
710 struct amba_device
*pcdev
= to_amba_device(dev
);
713 r
= (pcdev
->periphid
& d
->mask
) == d
->id
;
715 r
&= d
->parent
== dev
->parent
;
717 r
&= strcmp(dev_name(dev
), d
->busid
) == 0;
728 * amba_find_device - locate an AMBA device given a bus id
729 * @busid: bus id for device (or NULL)
730 * @parent: parent device (or NULL)
731 * @id: peripheral ID (or 0)
732 * @mask: peripheral ID mask (or 0)
734 * Return the AMBA device corresponding to the supplied parameters.
735 * If no device matches, returns NULL.
737 * NOTE: When a valid device is found, its refcount is
738 * incremented, and must be decremented before the returned
742 amba_find_device(const char *busid
, struct device
*parent
, unsigned int id
,
745 struct find_data data
;
748 data
.parent
= parent
;
753 bus_for_each_dev(&amba_bustype
, NULL
, &data
, amba_find_match
);
759 * amba_request_regions - request all mem regions associated with device
760 * @dev: amba_device structure for device
761 * @name: name, or NULL to use driver name
763 int amba_request_regions(struct amba_device
*dev
, const char *name
)
769 name
= dev
->dev
.driver
->name
;
771 size
= resource_size(&dev
->res
);
773 if (!request_mem_region(dev
->res
.start
, size
, name
))
780 * amba_release_regions - release mem regions associated with device
781 * @dev: amba_device structure for device
783 * Release regions claimed by a successful call to amba_request_regions.
785 void amba_release_regions(struct amba_device
*dev
)
789 size
= resource_size(&dev
->res
);
790 release_mem_region(dev
->res
.start
, size
);
793 EXPORT_SYMBOL(amba_driver_register
);
794 EXPORT_SYMBOL(amba_driver_unregister
);
795 EXPORT_SYMBOL(amba_device_register
);
796 EXPORT_SYMBOL(amba_device_unregister
);
797 EXPORT_SYMBOL(amba_find_device
);
798 EXPORT_SYMBOL(amba_request_regions
);
799 EXPORT_SYMBOL(amba_release_regions
);