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);
179 * Hooks to provide runtime PM of the pclk (bus clock). It is safe to
180 * enable/disable the bus clock at runtime PM suspend/resume as this
181 * does not result in loss of context.
183 static int amba_pm_runtime_suspend(struct device *dev)
185 struct amba_device *pcdev = to_amba_device(dev);
186 int ret = pm_generic_runtime_suspend(dev);
188 if (ret == 0 && dev->driver) {
189 if (pm_runtime_is_irq_safe(dev))
190 clk_disable(pcdev->pclk);
192 clk_disable_unprepare(pcdev->pclk);
198 static int amba_pm_runtime_resume(struct device *dev)
200 struct amba_device *pcdev = to_amba_device(dev);
204 if (pm_runtime_is_irq_safe(dev))
205 ret = clk_enable(pcdev->pclk);
207 ret = clk_prepare_enable(pcdev->pclk);
208 /* Failure is probably fatal to the system, but... */
213 return pm_generic_runtime_resume(dev);
215 #endif /* CONFIG_PM */
217 static const struct dev_pm_ops amba_pm = {
218 .suspend = pm_generic_suspend,
219 .resume = pm_generic_resume,
220 .freeze = pm_generic_freeze,
221 .thaw = pm_generic_thaw,
222 .poweroff = pm_generic_poweroff,
223 .restore = pm_generic_restore,
225 amba_pm_runtime_suspend,
226 amba_pm_runtime_resume,
232 * Primecells are part of the Advanced Microcontroller Bus Architecture,
233 * so we call the bus "amba".
234 * DMA configuration for platform and AMBA bus is same. So here we reuse
235 * platform's DMA config routine.
237 struct bus_type amba_bustype = {
239 .dev_groups = amba_dev_groups,
241 .uevent = amba_uevent,
242 .dma_configure = platform_dma_configure,
245 EXPORT_SYMBOL_GPL(amba_bustype);
247 static int __init amba_init(void)
249 return bus_register(&amba_bustype);
252 postcore_initcall(amba_init);
255 * These are the device model conversion veneers; they convert the
256 * device model structures to our more specific structures.
258 static int amba_probe(struct device *dev)
260 struct amba_device *pcdev = to_amba_device(dev);
261 struct amba_driver *pcdrv = to_amba_driver(dev->driver);
262 const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
266 ret = of_clk_set_defaults(dev->of_node, false);
270 ret = dev_pm_domain_attach(dev, true);
274 ret = amba_get_enable_pclk(pcdev);
276 dev_pm_domain_detach(dev, true);
280 pm_runtime_get_noresume(dev);
281 pm_runtime_set_active(dev);
282 pm_runtime_enable(dev);
284 ret = pcdrv->probe(pcdev, id);
288 pm_runtime_disable(dev);
289 pm_runtime_set_suspended(dev);
290 pm_runtime_put_noidle(dev);
292 amba_put_disable_pclk(pcdev);
293 dev_pm_domain_detach(dev, true);
299 static int amba_remove(struct device *dev)
301 struct amba_device *pcdev = to_amba_device(dev);
302 struct amba_driver *drv = to_amba_driver(dev->driver);
305 pm_runtime_get_sync(dev);
307 ret = drv->remove(pcdev);
308 pm_runtime_put_noidle(dev);
310 /* Undo the runtime PM settings in amba_probe() */
311 pm_runtime_disable(dev);
312 pm_runtime_set_suspended(dev);
313 pm_runtime_put_noidle(dev);
315 amba_put_disable_pclk(pcdev);
316 dev_pm_domain_detach(dev, true);
321 static void amba_shutdown(struct device *dev)
323 struct amba_driver *drv = to_amba_driver(dev->driver);
326 drv->shutdown(to_amba_device(dev));
330 * amba_driver_register - register an AMBA device driver
331 * @drv: amba device driver structure
333 * Register an AMBA device driver with the Linux device model
334 * core. If devices pre-exist, the drivers probe function will
337 int amba_driver_register(struct amba_driver *drv)
342 drv->drv.bus = &amba_bustype;
343 drv->drv.probe = amba_probe;
344 drv->drv.remove = amba_remove;
345 drv->drv.shutdown = amba_shutdown;
347 return driver_register(&drv->drv);
351 * amba_driver_unregister - remove an AMBA device driver
352 * @drv: AMBA device driver structure to remove
354 * Unregister an AMBA device driver from the Linux device
355 * model. The device model will call the drivers remove function
356 * for each device the device driver is currently handling.
358 void amba_driver_unregister(struct amba_driver *drv)
360 driver_unregister(&drv->drv);
364 static void amba_device_release(struct device *dev)
366 struct amba_device *d = to_amba_device(dev);
369 release_resource(&d->res);
373 static int amba_device_try_add(struct amba_device *dev, struct resource *parent)
379 WARN_ON(dev->irq[0] == (unsigned int)-1);
380 WARN_ON(dev->irq[1] == (unsigned int)-1);
382 ret = request_resource(parent, &dev->res);
386 /* Hard-coded primecell ID instead of plug-n-play */
387 if (dev->periphid != 0)
391 * Dynamically calculate the size of the resource
392 * and use this for iomap
394 size = resource_size(&dev->res);
395 tmp = ioremap(dev->res.start, size);
401 ret = dev_pm_domain_attach(&dev->dev, true);
407 ret = amba_get_enable_pclk(dev);
410 struct reset_control *rstc;
413 * Find reset control(s) of the amba bus and de-assert them.
415 rstc = of_reset_control_array_get_optional_shared(dev->dev.of_node);
418 if (ret != -EPROBE_DEFER)
419 dev_err(&dev->dev, "can't get reset: %d\n",
423 reset_control_deassert(rstc);
424 reset_control_put(rstc);
427 * Read pid and cid based on size of resource
428 * they are located at end of region
430 for (pid = 0, i = 0; i < 4; i++)
431 pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
433 for (cid = 0, i = 0; i < 4; i++)
434 cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
437 if (cid == CORESIGHT_CID) {
438 /* set the base to the start of the last 4k block */
439 void __iomem *csbase = tmp + size - 4096;
442 readl(csbase + UCI_REG_DEVARCH_OFFSET);
444 readl(csbase + UCI_REG_DEVTYPE_OFFSET) & 0xff;
447 amba_put_disable_pclk(dev);
449 if (cid == AMBA_CID || cid == CORESIGHT_CID) {
459 dev_pm_domain_detach(&dev->dev, true);
465 ret = device_add(&dev->dev);
470 ret = device_create_file(&dev->dev, &dev_attr_irq0);
471 if (ret == 0 && dev->irq[1])
472 ret = device_create_file(&dev->dev, &dev_attr_irq1);
476 device_unregister(&dev->dev);
479 release_resource(&dev->res);
484 amba_put_disable_pclk(dev);
486 dev_pm_domain_detach(&dev->dev, true);
491 * Registration of AMBA device require reading its pid and cid registers.
492 * To do this, the device must be turned on (if it is a part of power domain)
493 * and have clocks enabled. However in some cases those resources might not be
494 * yet available. Returning EPROBE_DEFER is not a solution in such case,
495 * because callers don't handle this special error code. Instead such devices
496 * are added to the special list and their registration is retried from
497 * periodic worker, until all resources are available and registration succeeds.
499 struct deferred_device {
500 struct amba_device *dev;
501 struct resource *parent;
502 struct list_head node;
505 static LIST_HEAD(deferred_devices);
506 static DEFINE_MUTEX(deferred_devices_lock);
508 static void amba_deferred_retry_func(struct work_struct *dummy);
509 static DECLARE_DELAYED_WORK(deferred_retry_work, amba_deferred_retry_func);
511 #define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000))
513 static int amba_deferred_retry(void)
515 struct deferred_device *ddev, *tmp;
517 mutex_lock(&deferred_devices_lock);
519 list_for_each_entry_safe(ddev, tmp, &deferred_devices, node) {
520 int ret = amba_device_try_add(ddev->dev, ddev->parent);
522 if (ret == -EPROBE_DEFER)
525 list_del_init(&ddev->node);
529 mutex_unlock(&deferred_devices_lock);
533 late_initcall(amba_deferred_retry);
535 static void amba_deferred_retry_func(struct work_struct *dummy)
537 amba_deferred_retry();
539 if (!list_empty(&deferred_devices))
540 schedule_delayed_work(&deferred_retry_work,
541 DEFERRED_DEVICE_TIMEOUT);
545 * amba_device_add - add a previously allocated AMBA device structure
546 * @dev: AMBA device allocated by amba_device_alloc
547 * @parent: resource parent for this devices resources
549 * Claim the resource, and read the device cell ID if not already
550 * initialized. Register the AMBA device with the Linux device
553 int amba_device_add(struct amba_device *dev, struct resource *parent)
555 int ret = amba_device_try_add(dev, parent);
557 if (ret == -EPROBE_DEFER) {
558 struct deferred_device *ddev;
560 ddev = kmalloc(sizeof(*ddev), GFP_KERNEL);
565 ddev->parent = parent;
568 mutex_lock(&deferred_devices_lock);
570 if (list_empty(&deferred_devices))
571 schedule_delayed_work(&deferred_retry_work,
572 DEFERRED_DEVICE_TIMEOUT);
573 list_add_tail(&ddev->node, &deferred_devices);
575 mutex_unlock(&deferred_devices_lock);
579 EXPORT_SYMBOL_GPL(amba_device_add);
581 static struct amba_device *
582 amba_aphb_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, u64 dma_mask,
585 struct resource *resbase)
587 struct amba_device *dev;
590 dev = amba_device_alloc(name, base, size);
592 return ERR_PTR(-ENOMEM);
594 dev->dev.coherent_dma_mask = dma_mask;
597 dev->periphid = periphid;
598 dev->dev.platform_data = pdata;
599 dev->dev.parent = parent;
601 ret = amba_device_add(dev, resbase);
603 amba_device_put(dev);
611 amba_apb_device_add(struct device *parent, const char *name,
612 resource_size_t base, size_t size, int irq1, int irq2,
613 void *pdata, unsigned int periphid)
615 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
616 periphid, 0, &iomem_resource);
618 EXPORT_SYMBOL_GPL(amba_apb_device_add);
621 amba_ahb_device_add(struct device *parent, const char *name,
622 resource_size_t base, size_t size, int irq1, int irq2,
623 void *pdata, unsigned int periphid)
625 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
626 periphid, ~0ULL, &iomem_resource);
628 EXPORT_SYMBOL_GPL(amba_ahb_device_add);
631 amba_apb_device_add_res(struct device *parent, const char *name,
632 resource_size_t base, size_t size, int irq1,
633 int irq2, void *pdata, unsigned int periphid,
634 struct resource *resbase)
636 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
637 periphid, 0, resbase);
639 EXPORT_SYMBOL_GPL(amba_apb_device_add_res);
642 amba_ahb_device_add_res(struct device *parent, const char *name,
643 resource_size_t base, size_t size, int irq1,
644 int irq2, void *pdata, unsigned int periphid,
645 struct resource *resbase)
647 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
648 periphid, ~0ULL, resbase);
650 EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);
653 static void amba_device_initialize(struct amba_device *dev, const char *name)
655 device_initialize(&dev->dev);
657 dev_set_name(&dev->dev, "%s", name);
658 dev->dev.release = amba_device_release;
659 dev->dev.bus = &amba_bustype;
660 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
661 dev->dev.dma_parms = &dev->dma_parms;
662 dev->res.name = dev_name(&dev->dev);
666 * amba_device_alloc - allocate an AMBA device
667 * @name: sysfs name of the AMBA device
668 * @base: base of AMBA device
669 * @size: size of AMBA device
671 * Allocate and initialize an AMBA device structure. Returns %NULL
674 struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
677 struct amba_device *dev;
679 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
681 amba_device_initialize(dev, name);
682 dev->res.start = base;
683 dev->res.end = base + size - 1;
684 dev->res.flags = IORESOURCE_MEM;
689 EXPORT_SYMBOL_GPL(amba_device_alloc);
692 * amba_device_register - register an AMBA device
693 * @dev: AMBA device to register
694 * @parent: parent memory resource
696 * Setup the AMBA device, reading the cell ID if present.
697 * Claim the resource, and register the AMBA device with
698 * the Linux device manager.
700 int amba_device_register(struct amba_device *dev, struct resource *parent)
702 amba_device_initialize(dev, dev->dev.init_name);
703 dev->dev.init_name = NULL;
705 return amba_device_add(dev, parent);
709 * amba_device_put - put an AMBA device
710 * @dev: AMBA device to put
712 void amba_device_put(struct amba_device *dev)
714 put_device(&dev->dev);
716 EXPORT_SYMBOL_GPL(amba_device_put);
719 * amba_device_unregister - unregister an AMBA device
720 * @dev: AMBA device to remove
722 * Remove the specified AMBA device from the Linux device
723 * manager. All files associated with this object will be
724 * destroyed, and device drivers notified that the device has
725 * been removed. The AMBA device's resources including
726 * the amba_device structure will be freed once all
727 * references to it have been dropped.
729 void amba_device_unregister(struct amba_device *dev)
731 device_unregister(&dev->dev);
736 struct amba_device *dev;
737 struct device *parent;
743 static int amba_find_match(struct device *dev, void *data)
745 struct find_data *d = data;
746 struct amba_device *pcdev = to_amba_device(dev);
749 r = (pcdev->periphid & d->mask) == d->id;
751 r &= d->parent == dev->parent;
753 r &= strcmp(dev_name(dev), d->busid) == 0;
764 * amba_find_device - locate an AMBA device given a bus id
765 * @busid: bus id for device (or NULL)
766 * @parent: parent device (or NULL)
767 * @id: peripheral ID (or 0)
768 * @mask: peripheral ID mask (or 0)
770 * Return the AMBA device corresponding to the supplied parameters.
771 * If no device matches, returns NULL.
773 * NOTE: When a valid device is found, its refcount is
774 * incremented, and must be decremented before the returned
778 amba_find_device(const char *busid, struct device *parent, unsigned int id,
781 struct find_data data;
784 data.parent = parent;
789 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
795 * amba_request_regions - request all mem regions associated with device
796 * @dev: amba_device structure for device
797 * @name: name, or NULL to use driver name
799 int amba_request_regions(struct amba_device *dev, const char *name)
805 name = dev->dev.driver->name;
807 size = resource_size(&dev->res);
809 if (!request_mem_region(dev->res.start, size, name))
816 * amba_release_regions - release mem regions associated with device
817 * @dev: amba_device structure for device
819 * Release regions claimed by a successful call to amba_request_regions.
821 void amba_release_regions(struct amba_device *dev)
825 size = resource_size(&dev->res);
826 release_mem_region(dev->res.start, size);
829 EXPORT_SYMBOL(amba_driver_register);
830 EXPORT_SYMBOL(amba_driver_unregister);
831 EXPORT_SYMBOL(amba_device_register);
832 EXPORT_SYMBOL(amba_device_unregister);
833 EXPORT_SYMBOL(amba_find_device);
834 EXPORT_SYMBOL(amba_request_regions);
835 EXPORT_SYMBOL(amba_release_regions);