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>
22 #include <linux/of_irq.h>
23 #include <linux/of_device.h>
24 #include <linux/acpi.h>
25 #include <linux/iommu.h>
26 #include <linux/dma-map-ops.h>
28 #define to_amba_driver(d) container_of(d, struct amba_driver, drv)
30 /* called on periphid match and class 0x9 coresight device. */
32 amba_cs_uci_id_match(const struct amba_id *table, struct amba_device *dev)
35 struct amba_cs_uci_id *uci;
39 /* no table data or zero mask - return match on periphid */
40 if (!uci || (uci->devarch_mask == 0))
43 /* test against read devtype and masked devarch value */
44 ret = (dev->uci.devtype == uci->devtype) &&
45 ((dev->uci.devarch & uci->devarch_mask) == uci->devarch);
49 static const struct amba_id *
50 amba_lookup(const struct amba_id *table, struct amba_device *dev)
53 if (((dev->periphid & table->mask) == table->id) &&
54 ((dev->cid != CORESIGHT_CID) ||
55 (amba_cs_uci_id_match(table, dev))))
62 static int amba_get_enable_pclk(struct amba_device *pcdev)
66 pcdev->pclk = clk_get(&pcdev->dev, "apb_pclk");
67 if (IS_ERR(pcdev->pclk))
68 return PTR_ERR(pcdev->pclk);
70 ret = clk_prepare_enable(pcdev->pclk);
77 static void amba_put_disable_pclk(struct amba_device *pcdev)
79 clk_disable_unprepare(pcdev->pclk);
84 static ssize_t driver_override_show(struct device *_dev,
85 struct device_attribute *attr, char *buf)
87 struct amba_device *dev = to_amba_device(_dev);
91 len = sprintf(buf, "%s\n", dev->driver_override);
96 static ssize_t driver_override_store(struct device *_dev,
97 struct device_attribute *attr,
98 const char *buf, size_t count)
100 struct amba_device *dev = to_amba_device(_dev);
103 ret = driver_set_override(_dev, &dev->driver_override, buf, count);
109 static DEVICE_ATTR_RW(driver_override);
111 #define amba_attr_func(name,fmt,arg...) \
112 static ssize_t name##_show(struct device *_dev, \
113 struct device_attribute *attr, char *buf) \
115 struct amba_device *dev = to_amba_device(_dev); \
116 return sprintf(buf, fmt, arg); \
118 static DEVICE_ATTR_RO(name)
120 amba_attr_func(id, "%08x\n", dev->periphid);
121 amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",
122 (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
125 static struct attribute *amba_dev_attrs[] = {
127 &dev_attr_resource.attr,
128 &dev_attr_driver_override.attr,
131 ATTRIBUTE_GROUPS(amba_dev);
133 static int amba_match(struct device *dev, struct device_driver *drv)
135 struct amba_device *pcdev = to_amba_device(dev);
136 struct amba_driver *pcdrv = to_amba_driver(drv);
138 /* When driver_override is set, only bind to the matching driver */
139 if (pcdev->driver_override)
140 return !strcmp(pcdev->driver_override, drv->name);
142 return amba_lookup(pcdrv->id_table, pcdev) != NULL;
145 static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
147 struct amba_device *pcdev = to_amba_device(dev);
150 retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
154 retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);
158 static int of_amba_device_decode_irq(struct amba_device *dev)
160 struct device_node *node = dev->dev.of_node;
163 if (IS_ENABLED(CONFIG_OF_IRQ) && node) {
164 /* Decode the IRQs and address ranges */
165 for (i = 0; i < AMBA_NR_IRQS; i++) {
166 irq = of_irq_get(node, i);
168 if (irq == -EPROBE_DEFER)
181 * These are the device model conversion veneers; they convert the
182 * device model structures to our more specific structures.
184 static int amba_probe(struct device *dev)
186 struct amba_device *pcdev = to_amba_device(dev);
187 struct amba_driver *pcdrv = to_amba_driver(dev->driver);
188 const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
192 ret = of_amba_device_decode_irq(pcdev);
196 ret = of_clk_set_defaults(dev->of_node, false);
200 ret = dev_pm_domain_attach(dev, true);
204 ret = amba_get_enable_pclk(pcdev);
206 dev_pm_domain_detach(dev, true);
210 pm_runtime_get_noresume(dev);
211 pm_runtime_set_active(dev);
212 pm_runtime_enable(dev);
214 ret = pcdrv->probe(pcdev, id);
218 pm_runtime_disable(dev);
219 pm_runtime_set_suspended(dev);
220 pm_runtime_put_noidle(dev);
222 amba_put_disable_pclk(pcdev);
223 dev_pm_domain_detach(dev, true);
229 static void amba_remove(struct device *dev)
231 struct amba_device *pcdev = to_amba_device(dev);
232 struct amba_driver *drv = to_amba_driver(dev->driver);
234 pm_runtime_get_sync(dev);
237 pm_runtime_put_noidle(dev);
239 /* Undo the runtime PM settings in amba_probe() */
240 pm_runtime_disable(dev);
241 pm_runtime_set_suspended(dev);
242 pm_runtime_put_noidle(dev);
244 amba_put_disable_pclk(pcdev);
245 dev_pm_domain_detach(dev, true);
248 static void amba_shutdown(struct device *dev)
250 struct amba_driver *drv;
255 drv = to_amba_driver(dev->driver);
257 drv->shutdown(to_amba_device(dev));
260 static int amba_dma_configure(struct device *dev)
262 struct amba_driver *drv = to_amba_driver(dev->driver);
263 enum dev_dma_attr attr;
267 ret = of_dma_configure(dev, dev->of_node, true);
268 } else if (has_acpi_companion(dev)) {
269 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
270 ret = acpi_dma_configure(dev, attr);
273 if (!ret && !drv->driver_managed_dma) {
274 ret = iommu_device_use_default_domain(dev);
276 arch_teardown_dma_ops(dev);
282 static void amba_dma_cleanup(struct device *dev)
284 struct amba_driver *drv = to_amba_driver(dev->driver);
286 if (!drv->driver_managed_dma)
287 iommu_device_unuse_default_domain(dev);
292 * Hooks to provide runtime PM of the pclk (bus clock). It is safe to
293 * enable/disable the bus clock at runtime PM suspend/resume as this
294 * does not result in loss of context.
296 static int amba_pm_runtime_suspend(struct device *dev)
298 struct amba_device *pcdev = to_amba_device(dev);
299 int ret = pm_generic_runtime_suspend(dev);
301 if (ret == 0 && dev->driver) {
302 if (pm_runtime_is_irq_safe(dev))
303 clk_disable(pcdev->pclk);
305 clk_disable_unprepare(pcdev->pclk);
311 static int amba_pm_runtime_resume(struct device *dev)
313 struct amba_device *pcdev = to_amba_device(dev);
317 if (pm_runtime_is_irq_safe(dev))
318 ret = clk_enable(pcdev->pclk);
320 ret = clk_prepare_enable(pcdev->pclk);
321 /* Failure is probably fatal to the system, but... */
326 return pm_generic_runtime_resume(dev);
328 #endif /* CONFIG_PM */
330 static const struct dev_pm_ops amba_pm = {
331 .suspend = pm_generic_suspend,
332 .resume = pm_generic_resume,
333 .freeze = pm_generic_freeze,
334 .thaw = pm_generic_thaw,
335 .poweroff = pm_generic_poweroff,
336 .restore = pm_generic_restore,
338 amba_pm_runtime_suspend,
339 amba_pm_runtime_resume,
345 * Primecells are part of the Advanced Microcontroller Bus Architecture,
346 * so we call the bus "amba".
347 * DMA configuration for platform and AMBA bus is same. So here we reuse
348 * platform's DMA config routine.
350 struct bus_type amba_bustype = {
352 .dev_groups = amba_dev_groups,
354 .uevent = amba_uevent,
356 .remove = amba_remove,
357 .shutdown = amba_shutdown,
358 .dma_configure = amba_dma_configure,
359 .dma_cleanup = amba_dma_cleanup,
362 EXPORT_SYMBOL_GPL(amba_bustype);
364 static int __init amba_init(void)
366 return bus_register(&amba_bustype);
369 postcore_initcall(amba_init);
372 * amba_driver_register - register an AMBA device driver
373 * @drv: amba device driver structure
375 * Register an AMBA device driver with the Linux device model
376 * core. If devices pre-exist, the drivers probe function will
379 int amba_driver_register(struct amba_driver *drv)
384 drv->drv.bus = &amba_bustype;
386 return driver_register(&drv->drv);
388 EXPORT_SYMBOL(amba_driver_register);
391 * amba_driver_unregister - remove an AMBA device driver
392 * @drv: AMBA device driver structure to remove
394 * Unregister an AMBA device driver from the Linux device
395 * model. The device model will call the drivers remove function
396 * for each device the device driver is currently handling.
398 void amba_driver_unregister(struct amba_driver *drv)
400 driver_unregister(&drv->drv);
402 EXPORT_SYMBOL(amba_driver_unregister);
404 static void amba_device_release(struct device *dev)
406 struct amba_device *d = to_amba_device(dev);
409 release_resource(&d->res);
413 static int amba_read_periphid(struct amba_device *dev)
415 struct reset_control *rstc;
420 ret = dev_pm_domain_attach(&dev->dev, true);
424 ret = amba_get_enable_pclk(dev);
429 * Find reset control(s) of the amba bus and de-assert them.
431 rstc = of_reset_control_array_get_optional_shared(dev->dev.of_node);
434 if (ret != -EPROBE_DEFER)
435 dev_err(&dev->dev, "can't get reset: %d\n", ret);
438 reset_control_deassert(rstc);
439 reset_control_put(rstc);
441 size = resource_size(&dev->res);
442 tmp = ioremap(dev->res.start, size);
449 * Read pid and cid based on size of resource
450 * they are located at end of region
452 for (pid = 0, i = 0; i < 4; i++)
453 pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) << (i * 8);
454 for (cid = 0, i = 0; i < 4; i++)
455 cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) << (i * 8);
457 if (cid == CORESIGHT_CID) {
458 /* set the base to the start of the last 4k block */
459 void __iomem *csbase = tmp + size - 4096;
461 dev->uci.devarch = readl(csbase + UCI_REG_DEVARCH_OFFSET);
462 dev->uci.devtype = readl(csbase + UCI_REG_DEVTYPE_OFFSET) & 0xff;
465 if (cid == AMBA_CID || cid == CORESIGHT_CID) {
476 amba_put_disable_pclk(dev);
478 dev_pm_domain_detach(&dev->dev, true);
483 static int amba_device_try_add(struct amba_device *dev, struct resource *parent)
487 ret = request_resource(parent, &dev->res);
491 /* Hard-coded primecell ID instead of plug-n-play */
492 if (dev->periphid != 0)
495 ret = amba_read_periphid(dev);
497 if (ret != -EPROBE_DEFER) {
498 amba_device_put(dev);
505 ret = device_add(&dev->dev);
508 release_resource(&dev->res);
514 * Registration of AMBA device require reading its pid and cid registers.
515 * To do this, the device must be turned on (if it is a part of power domain)
516 * and have clocks enabled. However in some cases those resources might not be
517 * yet available. Returning EPROBE_DEFER is not a solution in such case,
518 * because callers don't handle this special error code. Instead such devices
519 * are added to the special list and their registration is retried from
520 * periodic worker, until all resources are available and registration succeeds.
522 struct deferred_device {
523 struct amba_device *dev;
524 struct resource *parent;
525 struct list_head node;
528 static LIST_HEAD(deferred_devices);
529 static DEFINE_MUTEX(deferred_devices_lock);
531 static void amba_deferred_retry_func(struct work_struct *dummy);
532 static DECLARE_DELAYED_WORK(deferred_retry_work, amba_deferred_retry_func);
534 #define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000))
536 static int amba_deferred_retry(void)
538 struct deferred_device *ddev, *tmp;
540 mutex_lock(&deferred_devices_lock);
542 list_for_each_entry_safe(ddev, tmp, &deferred_devices, node) {
543 int ret = amba_device_try_add(ddev->dev, ddev->parent);
545 if (ret == -EPROBE_DEFER)
548 list_del_init(&ddev->node);
552 mutex_unlock(&deferred_devices_lock);
556 late_initcall(amba_deferred_retry);
558 static void amba_deferred_retry_func(struct work_struct *dummy)
560 amba_deferred_retry();
562 if (!list_empty(&deferred_devices))
563 schedule_delayed_work(&deferred_retry_work,
564 DEFERRED_DEVICE_TIMEOUT);
568 * amba_device_add - add a previously allocated AMBA device structure
569 * @dev: AMBA device allocated by amba_device_alloc
570 * @parent: resource parent for this devices resources
572 * Claim the resource, and read the device cell ID if not already
573 * initialized. Register the AMBA device with the Linux device
576 int amba_device_add(struct amba_device *dev, struct resource *parent)
578 int ret = amba_device_try_add(dev, parent);
580 if (ret == -EPROBE_DEFER) {
581 struct deferred_device *ddev;
583 ddev = kmalloc(sizeof(*ddev), GFP_KERNEL);
588 ddev->parent = parent;
591 mutex_lock(&deferred_devices_lock);
593 if (list_empty(&deferred_devices))
594 schedule_delayed_work(&deferred_retry_work,
595 DEFERRED_DEVICE_TIMEOUT);
596 list_add_tail(&ddev->node, &deferred_devices);
598 mutex_unlock(&deferred_devices_lock);
602 EXPORT_SYMBOL_GPL(amba_device_add);
604 static void amba_device_initialize(struct amba_device *dev, const char *name)
606 device_initialize(&dev->dev);
608 dev_set_name(&dev->dev, "%s", name);
609 dev->dev.release = amba_device_release;
610 dev->dev.bus = &amba_bustype;
611 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
612 dev->dev.dma_parms = &dev->dma_parms;
613 dev->res.name = dev_name(&dev->dev);
617 * amba_device_alloc - allocate an AMBA device
618 * @name: sysfs name of the AMBA device
619 * @base: base of AMBA device
620 * @size: size of AMBA device
622 * Allocate and initialize an AMBA device structure. Returns %NULL
625 struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
628 struct amba_device *dev;
630 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
632 amba_device_initialize(dev, name);
633 dev->res.start = base;
634 dev->res.end = base + size - 1;
635 dev->res.flags = IORESOURCE_MEM;
640 EXPORT_SYMBOL_GPL(amba_device_alloc);
643 * amba_device_register - register an AMBA device
644 * @dev: AMBA device to register
645 * @parent: parent memory resource
647 * Setup the AMBA device, reading the cell ID if present.
648 * Claim the resource, and register the AMBA device with
649 * the Linux device manager.
651 int amba_device_register(struct amba_device *dev, struct resource *parent)
653 amba_device_initialize(dev, dev->dev.init_name);
654 dev->dev.init_name = NULL;
656 return amba_device_add(dev, parent);
658 EXPORT_SYMBOL(amba_device_register);
661 * amba_device_put - put an AMBA device
662 * @dev: AMBA device to put
664 void amba_device_put(struct amba_device *dev)
666 put_device(&dev->dev);
668 EXPORT_SYMBOL_GPL(amba_device_put);
671 * amba_device_unregister - unregister an AMBA device
672 * @dev: AMBA device to remove
674 * Remove the specified AMBA device from the Linux device
675 * manager. All files associated with this object will be
676 * destroyed, and device drivers notified that the device has
677 * been removed. The AMBA device's resources including
678 * the amba_device structure will be freed once all
679 * references to it have been dropped.
681 void amba_device_unregister(struct amba_device *dev)
683 device_unregister(&dev->dev);
685 EXPORT_SYMBOL(amba_device_unregister);
688 * amba_request_regions - request all mem regions associated with device
689 * @dev: amba_device structure for device
690 * @name: name, or NULL to use driver name
692 int amba_request_regions(struct amba_device *dev, const char *name)
698 name = dev->dev.driver->name;
700 size = resource_size(&dev->res);
702 if (!request_mem_region(dev->res.start, size, name))
707 EXPORT_SYMBOL(amba_request_regions);
710 * amba_release_regions - release mem regions associated with device
711 * @dev: amba_device structure for device
713 * Release regions claimed by a successful call to amba_request_regions.
715 void amba_release_regions(struct amba_device *dev)
719 size = resource_size(&dev->res);
720 release_mem_region(dev->res.start, size);
722 EXPORT_SYMBOL(amba_release_regions);