]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/amba/bus.c
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>
21 #include <linux/reset.h>
25 #define to_amba_driver(d) container_of(d, struct amba_driver, drv)
27 /* called on periphid match and class 0x9 coresight device. */
29 amba_cs_uci_id_match(const struct amba_id
*table
, struct amba_device
*dev
)
32 struct amba_cs_uci_id
*uci
;
36 /* no table data or zero mask - return match on periphid */
37 if (!uci
|| (uci
->devarch_mask
== 0))
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
);
46 static const struct amba_id
*
47 amba_lookup(const struct amba_id
*table
, struct amba_device
*dev
)
50 if (((dev
->periphid
& table
->mask
) == table
->id
) &&
51 ((dev
->cid
!= CORESIGHT_CID
) ||
52 (amba_cs_uci_id_match(table
, dev
))))
59 static int amba_get_enable_pclk(struct amba_device
*pcdev
)
63 pcdev
->pclk
= clk_get(&pcdev
->dev
, "apb_pclk");
64 if (IS_ERR(pcdev
->pclk
))
65 return PTR_ERR(pcdev
->pclk
);
67 ret
= clk_prepare_enable(pcdev
->pclk
);
74 static void amba_put_disable_pclk(struct amba_device
*pcdev
)
76 clk_disable_unprepare(pcdev
->pclk
);
81 static ssize_t
driver_override_show(struct device
*_dev
,
82 struct device_attribute
*attr
, char *buf
)
84 struct amba_device
*dev
= to_amba_device(_dev
);
88 len
= sprintf(buf
, "%s\n", dev
->driver_override
);
93 static ssize_t
driver_override_store(struct device
*_dev
,
94 struct device_attribute
*attr
,
95 const char *buf
, size_t count
)
97 struct amba_device
*dev
= to_amba_device(_dev
);
98 char *driver_override
, *old
, *cp
;
100 /* We need to keep extra room for a newline */
101 if (count
>= (PAGE_SIZE
- 1))
104 driver_override
= kstrndup(buf
, count
, GFP_KERNEL
);
105 if (!driver_override
)
108 cp
= strchr(driver_override
, '\n');
113 old
= dev
->driver_override
;
114 if (strlen(driver_override
)) {
115 dev
->driver_override
= driver_override
;
117 kfree(driver_override
);
118 dev
->driver_override
= NULL
;
126 static DEVICE_ATTR_RW(driver_override
);
128 #define amba_attr_func(name,fmt,arg...) \
129 static ssize_t name##_show(struct device *_dev, \
130 struct device_attribute *attr, char *buf) \
132 struct amba_device *dev = to_amba_device(_dev); \
133 return sprintf(buf, fmt, arg); \
135 static DEVICE_ATTR_RO(name)
137 amba_attr_func(id
, "%08x\n", dev
->periphid
);
138 amba_attr_func(irq0
, "%u\n", dev
->irq
[0]);
139 amba_attr_func(irq1
, "%u\n", dev
->irq
[1]);
140 amba_attr_func(resource
, "\t%016llx\t%016llx\t%016lx\n",
141 (unsigned long long)dev
->res
.start
, (unsigned long long)dev
->res
.end
,
144 static struct attribute
*amba_dev_attrs
[] = {
146 &dev_attr_resource
.attr
,
147 &dev_attr_driver_override
.attr
,
150 ATTRIBUTE_GROUPS(amba_dev
);
152 static int amba_match(struct device
*dev
, struct device_driver
*drv
)
154 struct amba_device
*pcdev
= to_amba_device(dev
);
155 struct amba_driver
*pcdrv
= to_amba_driver(drv
);
157 /* When driver_override is set, only bind to the matching driver */
158 if (pcdev
->driver_override
)
159 return !strcmp(pcdev
->driver_override
, drv
->name
);
161 return amba_lookup(pcdrv
->id_table
, pcdev
) != NULL
;
164 static int amba_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
166 struct amba_device
*pcdev
= to_amba_device(dev
);
169 retval
= add_uevent_var(env
, "AMBA_ID=%08x", pcdev
->periphid
);
173 retval
= add_uevent_var(env
, "MODALIAS=amba:d%08X", pcdev
->periphid
);
178 * These are the device model conversion veneers; they convert the
179 * device model structures to our more specific structures.
181 static int amba_probe(struct device
*dev
)
183 struct amba_device
*pcdev
= to_amba_device(dev
);
184 struct amba_driver
*pcdrv
= to_amba_driver(dev
->driver
);
185 const struct amba_id
*id
= amba_lookup(pcdrv
->id_table
, pcdev
);
189 ret
= of_clk_set_defaults(dev
->of_node
, false);
193 ret
= dev_pm_domain_attach(dev
, true);
197 ret
= amba_get_enable_pclk(pcdev
);
199 dev_pm_domain_detach(dev
, true);
203 pm_runtime_get_noresume(dev
);
204 pm_runtime_set_active(dev
);
205 pm_runtime_enable(dev
);
207 ret
= pcdrv
->probe(pcdev
, id
);
211 pm_runtime_disable(dev
);
212 pm_runtime_set_suspended(dev
);
213 pm_runtime_put_noidle(dev
);
215 amba_put_disable_pclk(pcdev
);
216 dev_pm_domain_detach(dev
, true);
222 static int amba_remove(struct device
*dev
)
224 struct amba_device
*pcdev
= to_amba_device(dev
);
225 struct amba_driver
*drv
= to_amba_driver(dev
->driver
);
227 pm_runtime_get_sync(dev
);
230 pm_runtime_put_noidle(dev
);
232 /* Undo the runtime PM settings in amba_probe() */
233 pm_runtime_disable(dev
);
234 pm_runtime_set_suspended(dev
);
235 pm_runtime_put_noidle(dev
);
237 amba_put_disable_pclk(pcdev
);
238 dev_pm_domain_detach(dev
, true);
243 static void amba_shutdown(struct device
*dev
)
245 struct amba_driver
*drv
;
250 drv
= to_amba_driver(dev
->driver
);
252 drv
->shutdown(to_amba_device(dev
));
257 * Hooks to provide runtime PM of the pclk (bus clock). It is safe to
258 * enable/disable the bus clock at runtime PM suspend/resume as this
259 * does not result in loss of context.
261 static int amba_pm_runtime_suspend(struct device
*dev
)
263 struct amba_device
*pcdev
= to_amba_device(dev
);
264 int ret
= pm_generic_runtime_suspend(dev
);
266 if (ret
== 0 && dev
->driver
) {
267 if (pm_runtime_is_irq_safe(dev
))
268 clk_disable(pcdev
->pclk
);
270 clk_disable_unprepare(pcdev
->pclk
);
276 static int amba_pm_runtime_resume(struct device
*dev
)
278 struct amba_device
*pcdev
= to_amba_device(dev
);
282 if (pm_runtime_is_irq_safe(dev
))
283 ret
= clk_enable(pcdev
->pclk
);
285 ret
= clk_prepare_enable(pcdev
->pclk
);
286 /* Failure is probably fatal to the system, but... */
291 return pm_generic_runtime_resume(dev
);
293 #endif /* CONFIG_PM */
295 static const struct dev_pm_ops amba_pm
= {
296 .suspend
= pm_generic_suspend
,
297 .resume
= pm_generic_resume
,
298 .freeze
= pm_generic_freeze
,
299 .thaw
= pm_generic_thaw
,
300 .poweroff
= pm_generic_poweroff
,
301 .restore
= pm_generic_restore
,
303 amba_pm_runtime_suspend
,
304 amba_pm_runtime_resume
,
310 * Primecells are part of the Advanced Microcontroller Bus Architecture,
311 * so we call the bus "amba".
312 * DMA configuration for platform and AMBA bus is same. So here we reuse
313 * platform's DMA config routine.
315 struct bus_type amba_bustype
= {
317 .dev_groups
= amba_dev_groups
,
319 .uevent
= amba_uevent
,
321 .remove
= amba_remove
,
322 .shutdown
= amba_shutdown
,
323 .dma_configure
= platform_dma_configure
,
326 EXPORT_SYMBOL_GPL(amba_bustype
);
328 static int __init
amba_init(void)
330 return bus_register(&amba_bustype
);
333 postcore_initcall(amba_init
);
336 * amba_driver_register - register an AMBA device driver
337 * @drv: amba device driver structure
339 * Register an AMBA device driver with the Linux device model
340 * core. If devices pre-exist, the drivers probe function will
343 int amba_driver_register(struct amba_driver
*drv
)
348 drv
->drv
.bus
= &amba_bustype
;
350 return driver_register(&drv
->drv
);
354 * amba_driver_unregister - remove an AMBA device driver
355 * @drv: AMBA device driver structure to remove
357 * Unregister an AMBA device driver from the Linux device
358 * model. The device model will call the drivers remove function
359 * for each device the device driver is currently handling.
361 void amba_driver_unregister(struct amba_driver
*drv
)
363 driver_unregister(&drv
->drv
);
367 static void amba_device_release(struct device
*dev
)
369 struct amba_device
*d
= to_amba_device(dev
);
372 release_resource(&d
->res
);
376 static int amba_device_try_add(struct amba_device
*dev
, struct resource
*parent
)
382 WARN_ON(dev
->irq
[0] == (unsigned int)-1);
383 WARN_ON(dev
->irq
[1] == (unsigned int)-1);
385 ret
= request_resource(parent
, &dev
->res
);
389 /* Hard-coded primecell ID instead of plug-n-play */
390 if (dev
->periphid
!= 0)
394 * Dynamically calculate the size of the resource
395 * and use this for iomap
397 size
= resource_size(&dev
->res
);
398 tmp
= ioremap(dev
->res
.start
, size
);
404 ret
= dev_pm_domain_attach(&dev
->dev
, true);
410 ret
= amba_get_enable_pclk(dev
);
413 struct reset_control
*rstc
;
416 * Find reset control(s) of the amba bus and de-assert them.
418 rstc
= of_reset_control_array_get_optional_shared(dev
->dev
.of_node
);
421 if (ret
!= -EPROBE_DEFER
)
422 dev_err(&dev
->dev
, "can't get reset: %d\n",
426 reset_control_deassert(rstc
);
427 reset_control_put(rstc
);
430 * Read pid and cid based on size of resource
431 * they are located at end of region
433 for (pid
= 0, i
= 0; i
< 4; i
++)
434 pid
|= (readl(tmp
+ size
- 0x20 + 4 * i
) & 255) <<
436 for (cid
= 0, i
= 0; i
< 4; i
++)
437 cid
|= (readl(tmp
+ size
- 0x10 + 4 * i
) & 255) <<
440 if (cid
== CORESIGHT_CID
) {
441 /* set the base to the start of the last 4k block */
442 void __iomem
*csbase
= tmp
+ size
- 4096;
445 readl(csbase
+ UCI_REG_DEVARCH_OFFSET
);
447 readl(csbase
+ UCI_REG_DEVTYPE_OFFSET
) & 0xff;
450 amba_put_disable_pclk(dev
);
452 if (cid
== AMBA_CID
|| cid
== CORESIGHT_CID
) {
462 dev_pm_domain_detach(&dev
->dev
, true);
468 ret
= device_add(&dev
->dev
);
473 ret
= device_create_file(&dev
->dev
, &dev_attr_irq0
);
474 if (ret
== 0 && dev
->irq
[1])
475 ret
= device_create_file(&dev
->dev
, &dev_attr_irq1
);
479 device_unregister(&dev
->dev
);
482 release_resource(&dev
->res
);
487 amba_put_disable_pclk(dev
);
489 dev_pm_domain_detach(&dev
->dev
, true);
494 * Registration of AMBA device require reading its pid and cid registers.
495 * To do this, the device must be turned on (if it is a part of power domain)
496 * and have clocks enabled. However in some cases those resources might not be
497 * yet available. Returning EPROBE_DEFER is not a solution in such case,
498 * because callers don't handle this special error code. Instead such devices
499 * are added to the special list and their registration is retried from
500 * periodic worker, until all resources are available and registration succeeds.
502 struct deferred_device
{
503 struct amba_device
*dev
;
504 struct resource
*parent
;
505 struct list_head node
;
508 static LIST_HEAD(deferred_devices
);
509 static DEFINE_MUTEX(deferred_devices_lock
);
511 static void amba_deferred_retry_func(struct work_struct
*dummy
);
512 static DECLARE_DELAYED_WORK(deferred_retry_work
, amba_deferred_retry_func
);
514 #define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000))
516 static int amba_deferred_retry(void)
518 struct deferred_device
*ddev
, *tmp
;
520 mutex_lock(&deferred_devices_lock
);
522 list_for_each_entry_safe(ddev
, tmp
, &deferred_devices
, node
) {
523 int ret
= amba_device_try_add(ddev
->dev
, ddev
->parent
);
525 if (ret
== -EPROBE_DEFER
)
528 list_del_init(&ddev
->node
);
532 mutex_unlock(&deferred_devices_lock
);
536 late_initcall(amba_deferred_retry
);
538 static void amba_deferred_retry_func(struct work_struct
*dummy
)
540 amba_deferred_retry();
542 if (!list_empty(&deferred_devices
))
543 schedule_delayed_work(&deferred_retry_work
,
544 DEFERRED_DEVICE_TIMEOUT
);
548 * amba_device_add - add a previously allocated AMBA device structure
549 * @dev: AMBA device allocated by amba_device_alloc
550 * @parent: resource parent for this devices resources
552 * Claim the resource, and read the device cell ID if not already
553 * initialized. Register the AMBA device with the Linux device
556 int amba_device_add(struct amba_device
*dev
, struct resource
*parent
)
558 int ret
= amba_device_try_add(dev
, parent
);
560 if (ret
== -EPROBE_DEFER
) {
561 struct deferred_device
*ddev
;
563 ddev
= kmalloc(sizeof(*ddev
), GFP_KERNEL
);
568 ddev
->parent
= parent
;
571 mutex_lock(&deferred_devices_lock
);
573 if (list_empty(&deferred_devices
))
574 schedule_delayed_work(&deferred_retry_work
,
575 DEFERRED_DEVICE_TIMEOUT
);
576 list_add_tail(&ddev
->node
, &deferred_devices
);
578 mutex_unlock(&deferred_devices_lock
);
582 EXPORT_SYMBOL_GPL(amba_device_add
);
584 static struct amba_device
*
585 amba_aphb_device_add(struct device
*parent
, const char *name
,
586 resource_size_t base
, size_t size
, int irq1
, int irq2
,
587 void *pdata
, unsigned int periphid
, u64 dma_mask
,
588 struct resource
*resbase
)
590 struct amba_device
*dev
;
593 dev
= amba_device_alloc(name
, base
, size
);
595 return ERR_PTR(-ENOMEM
);
597 dev
->dev
.coherent_dma_mask
= dma_mask
;
600 dev
->periphid
= periphid
;
601 dev
->dev
.platform_data
= pdata
;
602 dev
->dev
.parent
= parent
;
604 ret
= amba_device_add(dev
, resbase
);
606 amba_device_put(dev
);
614 amba_apb_device_add(struct device
*parent
, const char *name
,
615 resource_size_t base
, size_t size
, int irq1
, int irq2
,
616 void *pdata
, unsigned int periphid
)
618 return amba_aphb_device_add(parent
, name
, base
, size
, irq1
, irq2
, pdata
,
619 periphid
, 0, &iomem_resource
);
621 EXPORT_SYMBOL_GPL(amba_apb_device_add
);
624 amba_ahb_device_add(struct device
*parent
, const char *name
,
625 resource_size_t base
, size_t size
, int irq1
, int irq2
,
626 void *pdata
, unsigned int periphid
)
628 return amba_aphb_device_add(parent
, name
, base
, size
, irq1
, irq2
, pdata
,
629 periphid
, ~0ULL, &iomem_resource
);
631 EXPORT_SYMBOL_GPL(amba_ahb_device_add
);
634 amba_apb_device_add_res(struct device
*parent
, const char *name
,
635 resource_size_t base
, size_t size
, int irq1
,
636 int irq2
, void *pdata
, unsigned int periphid
,
637 struct resource
*resbase
)
639 return amba_aphb_device_add(parent
, name
, base
, size
, irq1
, irq2
, pdata
,
640 periphid
, 0, resbase
);
642 EXPORT_SYMBOL_GPL(amba_apb_device_add_res
);
645 amba_ahb_device_add_res(struct device
*parent
, const char *name
,
646 resource_size_t base
, size_t size
, int irq1
,
647 int irq2
, void *pdata
, unsigned int periphid
,
648 struct resource
*resbase
)
650 return amba_aphb_device_add(parent
, name
, base
, size
, irq1
, irq2
, pdata
,
651 periphid
, ~0ULL, resbase
);
653 EXPORT_SYMBOL_GPL(amba_ahb_device_add_res
);
656 static void amba_device_initialize(struct amba_device
*dev
, const char *name
)
658 device_initialize(&dev
->dev
);
660 dev_set_name(&dev
->dev
, "%s", name
);
661 dev
->dev
.release
= amba_device_release
;
662 dev
->dev
.bus
= &amba_bustype
;
663 dev
->dev
.dma_mask
= &dev
->dev
.coherent_dma_mask
;
664 dev
->dev
.dma_parms
= &dev
->dma_parms
;
665 dev
->res
.name
= dev_name(&dev
->dev
);
669 * amba_device_alloc - allocate an AMBA device
670 * @name: sysfs name of the AMBA device
671 * @base: base of AMBA device
672 * @size: size of AMBA device
674 * Allocate and initialize an AMBA device structure. Returns %NULL
677 struct amba_device
*amba_device_alloc(const char *name
, resource_size_t base
,
680 struct amba_device
*dev
;
682 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
684 amba_device_initialize(dev
, name
);
685 dev
->res
.start
= base
;
686 dev
->res
.end
= base
+ size
- 1;
687 dev
->res
.flags
= IORESOURCE_MEM
;
692 EXPORT_SYMBOL_GPL(amba_device_alloc
);
695 * amba_device_register - register an AMBA device
696 * @dev: AMBA device to register
697 * @parent: parent memory resource
699 * Setup the AMBA device, reading the cell ID if present.
700 * Claim the resource, and register the AMBA device with
701 * the Linux device manager.
703 int amba_device_register(struct amba_device
*dev
, struct resource
*parent
)
705 amba_device_initialize(dev
, dev
->dev
.init_name
);
706 dev
->dev
.init_name
= NULL
;
708 return amba_device_add(dev
, parent
);
712 * amba_device_put - put an AMBA device
713 * @dev: AMBA device to put
715 void amba_device_put(struct amba_device
*dev
)
717 put_device(&dev
->dev
);
719 EXPORT_SYMBOL_GPL(amba_device_put
);
722 * amba_device_unregister - unregister an AMBA device
723 * @dev: AMBA device to remove
725 * Remove the specified AMBA device from the Linux device
726 * manager. All files associated with this object will be
727 * destroyed, and device drivers notified that the device has
728 * been removed. The AMBA device's resources including
729 * the amba_device structure will be freed once all
730 * references to it have been dropped.
732 void amba_device_unregister(struct amba_device
*dev
)
734 device_unregister(&dev
->dev
);
739 struct amba_device
*dev
;
740 struct device
*parent
;
746 static int amba_find_match(struct device
*dev
, void *data
)
748 struct find_data
*d
= data
;
749 struct amba_device
*pcdev
= to_amba_device(dev
);
752 r
= (pcdev
->periphid
& d
->mask
) == d
->id
;
754 r
&= d
->parent
== dev
->parent
;
756 r
&= strcmp(dev_name(dev
), d
->busid
) == 0;
767 * amba_find_device - locate an AMBA device given a bus id
768 * @busid: bus id for device (or NULL)
769 * @parent: parent device (or NULL)
770 * @id: peripheral ID (or 0)
771 * @mask: peripheral ID mask (or 0)
773 * Return the AMBA device corresponding to the supplied parameters.
774 * If no device matches, returns NULL.
776 * NOTE: When a valid device is found, its refcount is
777 * incremented, and must be decremented before the returned
781 amba_find_device(const char *busid
, struct device
*parent
, unsigned int id
,
784 struct find_data data
;
787 data
.parent
= parent
;
792 bus_for_each_dev(&amba_bustype
, NULL
, &data
, amba_find_match
);
798 * amba_request_regions - request all mem regions associated with device
799 * @dev: amba_device structure for device
800 * @name: name, or NULL to use driver name
802 int amba_request_regions(struct amba_device
*dev
, const char *name
)
808 name
= dev
->dev
.driver
->name
;
810 size
= resource_size(&dev
->res
);
812 if (!request_mem_region(dev
->res
.start
, size
, name
))
819 * amba_release_regions - release mem regions associated with device
820 * @dev: amba_device structure for device
822 * Release regions claimed by a successful call to amba_request_regions.
824 void amba_release_regions(struct amba_device
*dev
)
828 size
= resource_size(&dev
->res
);
829 release_mem_region(dev
->res
.start
, size
);
832 EXPORT_SYMBOL(amba_driver_register
);
833 EXPORT_SYMBOL(amba_driver_unregister
);
834 EXPORT_SYMBOL(amba_device_register
);
835 EXPORT_SYMBOL(amba_device_unregister
);
836 EXPORT_SYMBOL(amba_find_device
);
837 EXPORT_SYMBOL(amba_request_regions
);
838 EXPORT_SYMBOL(amba_release_regions
);