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_match(struct device *dev, struct device_driver *drv)
61 struct amba_device *pcdev = to_amba_device(dev);
62 struct amba_driver *pcdrv = to_amba_driver(drv);
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);
68 return amba_lookup(pcdrv->id_table, pcdev) != NULL;
71 static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
73 struct amba_device *pcdev = to_amba_device(dev);
76 retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
80 retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);
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);
101 char *driver_override, *old, *cp;
103 /* We need to keep extra room for a newline */
104 if (count >= (PAGE_SIZE - 1))
107 driver_override = kstrndup(buf, count, GFP_KERNEL);
108 if (!driver_override)
111 cp = strchr(driver_override, '\n');
116 old = dev->driver_override;
117 if (strlen(driver_override)) {
118 dev->driver_override = driver_override;
120 kfree(driver_override);
121 dev->driver_override = NULL;
129 static DEVICE_ATTR_RW(driver_override);
131 #define amba_attr_func(name,fmt,arg...) \
132 static ssize_t name##_show(struct device *_dev, \
133 struct device_attribute *attr, char *buf) \
135 struct amba_device *dev = to_amba_device(_dev); \
136 return sprintf(buf, fmt, arg); \
138 static DEVICE_ATTR_RO(name)
140 amba_attr_func(id, "%08x\n", dev->periphid);
141 amba_attr_func(irq0, "%u\n", dev->irq[0]);
142 amba_attr_func(irq1, "%u\n", dev->irq[1]);
143 amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",
144 (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
147 static struct attribute *amba_dev_attrs[] = {
149 &dev_attr_resource.attr,
150 &dev_attr_driver_override.attr,
153 ATTRIBUTE_GROUPS(amba_dev);
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.
161 static int amba_pm_runtime_suspend(struct device *dev)
163 struct amba_device *pcdev = to_amba_device(dev);
164 int ret = pm_generic_runtime_suspend(dev);
166 if (ret == 0 && dev->driver) {
167 if (pm_runtime_is_irq_safe(dev))
168 clk_disable(pcdev->pclk);
170 clk_disable_unprepare(pcdev->pclk);
176 static int amba_pm_runtime_resume(struct device *dev)
178 struct amba_device *pcdev = to_amba_device(dev);
182 if (pm_runtime_is_irq_safe(dev))
183 ret = clk_enable(pcdev->pclk);
185 ret = clk_prepare_enable(pcdev->pclk);
186 /* Failure is probably fatal to the system, but... */
191 return pm_generic_runtime_resume(dev);
193 #endif /* CONFIG_PM */
195 static 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,
203 amba_pm_runtime_suspend,
204 amba_pm_runtime_resume,
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.
215 struct bus_type amba_bustype = {
217 .dev_groups = amba_dev_groups,
219 .uevent = amba_uevent,
220 .dma_configure = platform_dma_configure,
223 EXPORT_SYMBOL_GPL(amba_bustype);
225 static int __init amba_init(void)
227 return bus_register(&amba_bustype);
230 postcore_initcall(amba_init);
232 static int amba_get_enable_pclk(struct amba_device *pcdev)
236 pcdev->pclk = clk_get(&pcdev->dev, "apb_pclk");
237 if (IS_ERR(pcdev->pclk))
238 return PTR_ERR(pcdev->pclk);
240 ret = clk_prepare_enable(pcdev->pclk);
242 clk_put(pcdev->pclk);
247 static void amba_put_disable_pclk(struct amba_device *pcdev)
249 clk_disable_unprepare(pcdev->pclk);
250 clk_put(pcdev->pclk);
254 * These are the device model conversion veneers; they convert the
255 * device model structures to our more specific structures.
257 static int amba_probe(struct device *dev)
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);
265 ret = of_clk_set_defaults(dev->of_node, false);
269 ret = dev_pm_domain_attach(dev, true);
273 ret = amba_get_enable_pclk(pcdev);
275 dev_pm_domain_detach(dev, true);
279 pm_runtime_get_noresume(dev);
280 pm_runtime_set_active(dev);
281 pm_runtime_enable(dev);
283 ret = pcdrv->probe(pcdev, id);
287 pm_runtime_disable(dev);
288 pm_runtime_set_suspended(dev);
289 pm_runtime_put_noidle(dev);
291 amba_put_disable_pclk(pcdev);
292 dev_pm_domain_detach(dev, true);
298 static int amba_remove(struct device *dev)
300 struct amba_device *pcdev = to_amba_device(dev);
301 struct amba_driver *drv = to_amba_driver(dev->driver);
303 pm_runtime_get_sync(dev);
306 pm_runtime_put_noidle(dev);
308 /* Undo the runtime PM settings in amba_probe() */
309 pm_runtime_disable(dev);
310 pm_runtime_set_suspended(dev);
311 pm_runtime_put_noidle(dev);
313 amba_put_disable_pclk(pcdev);
314 dev_pm_domain_detach(dev, true);
319 static void amba_shutdown(struct device *dev)
321 struct amba_driver *drv = to_amba_driver(dev->driver);
324 drv->shutdown(to_amba_device(dev));
328 * amba_driver_register - register an AMBA device driver
329 * @drv: amba device driver structure
331 * Register an AMBA device driver with the Linux device model
332 * core. If devices pre-exist, the drivers probe function will
335 int amba_driver_register(struct amba_driver *drv)
340 drv->drv.bus = &amba_bustype;
341 drv->drv.probe = amba_probe;
342 drv->drv.remove = amba_remove;
343 drv->drv.shutdown = amba_shutdown;
345 return driver_register(&drv->drv);
349 * amba_driver_unregister - remove an AMBA device driver
350 * @drv: AMBA device driver structure to remove
352 * Unregister an AMBA device driver from the Linux device
353 * model. The device model will call the drivers remove function
354 * for each device the device driver is currently handling.
356 void amba_driver_unregister(struct amba_driver *drv)
358 driver_unregister(&drv->drv);
362 static void amba_device_release(struct device *dev)
364 struct amba_device *d = to_amba_device(dev);
366 of_node_put(d->dev.of_node);
368 release_resource(&d->res);
372 static int amba_device_try_add(struct amba_device *dev, struct resource *parent)
378 ret = request_resource(parent, &dev->res);
382 /* Hard-coded primecell ID instead of plug-n-play */
383 if (dev->periphid != 0)
387 * Dynamically calculate the size of the resource
388 * and use this for iomap
390 size = resource_size(&dev->res);
391 tmp = ioremap(dev->res.start, size);
397 ret = dev_pm_domain_attach(&dev->dev, true);
403 ret = amba_get_enable_pclk(dev);
406 struct reset_control *rstc;
409 * Find reset control(s) of the amba bus and de-assert them.
411 rstc = of_reset_control_array_get_optional_shared(dev->dev.of_node);
414 if (ret != -EPROBE_DEFER)
415 dev_err(&dev->dev, "can't get reset: %d\n",
419 reset_control_deassert(rstc);
420 reset_control_put(rstc);
423 * Read pid and cid based on size of resource
424 * they are located at end of region
426 for (pid = 0, i = 0; i < 4; i++)
427 pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
429 for (cid = 0, i = 0; i < 4; i++)
430 cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
433 if (cid == CORESIGHT_CID) {
434 /* set the base to the start of the last 4k block */
435 void __iomem *csbase = tmp + size - 4096;
438 readl(csbase + UCI_REG_DEVARCH_OFFSET);
440 readl(csbase + UCI_REG_DEVTYPE_OFFSET) & 0xff;
443 amba_put_disable_pclk(dev);
445 if (cid == AMBA_CID || cid == CORESIGHT_CID) {
455 dev_pm_domain_detach(&dev->dev, true);
461 ret = device_add(&dev->dev);
466 ret = device_create_file(&dev->dev, &dev_attr_irq0);
467 if (ret == 0 && dev->irq[1])
468 ret = device_create_file(&dev->dev, &dev_attr_irq1);
472 device_unregister(&dev->dev);
475 release_resource(&dev->res);
480 amba_put_disable_pclk(dev);
482 dev_pm_domain_detach(&dev->dev, true);
487 * Registration of AMBA device require reading its pid and cid registers.
488 * To do this, the device must be turned on (if it is a part of power domain)
489 * and have clocks enabled. However in some cases those resources might not be
490 * yet available. Returning EPROBE_DEFER is not a solution in such case,
491 * because callers don't handle this special error code. Instead such devices
492 * are added to the special list and their registration is retried from
493 * periodic worker, until all resources are available and registration succeeds.
495 struct deferred_device {
496 struct amba_device *dev;
497 struct resource *parent;
498 struct list_head node;
501 static LIST_HEAD(deferred_devices);
502 static DEFINE_MUTEX(deferred_devices_lock);
504 static void amba_deferred_retry_func(struct work_struct *dummy);
505 static DECLARE_DELAYED_WORK(deferred_retry_work, amba_deferred_retry_func);
507 #define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000))
509 static int amba_deferred_retry(void)
511 struct deferred_device *ddev, *tmp;
513 mutex_lock(&deferred_devices_lock);
515 list_for_each_entry_safe(ddev, tmp, &deferred_devices, node) {
516 int ret = amba_device_try_add(ddev->dev, ddev->parent);
518 if (ret == -EPROBE_DEFER)
521 list_del_init(&ddev->node);
525 mutex_unlock(&deferred_devices_lock);
529 late_initcall(amba_deferred_retry);
531 static void amba_deferred_retry_func(struct work_struct *dummy)
533 amba_deferred_retry();
535 if (!list_empty(&deferred_devices))
536 schedule_delayed_work(&deferred_retry_work,
537 DEFERRED_DEVICE_TIMEOUT);
541 * amba_device_add - add a previously allocated AMBA device structure
542 * @dev: AMBA device allocated by amba_device_alloc
543 * @parent: resource parent for this devices resources
545 * Claim the resource, and read the device cell ID if not already
546 * initialized. Register the AMBA device with the Linux device
549 int amba_device_add(struct amba_device *dev, struct resource *parent)
551 int ret = amba_device_try_add(dev, parent);
553 if (ret == -EPROBE_DEFER) {
554 struct deferred_device *ddev;
556 ddev = kmalloc(sizeof(*ddev), GFP_KERNEL);
561 ddev->parent = parent;
564 mutex_lock(&deferred_devices_lock);
566 if (list_empty(&deferred_devices))
567 schedule_delayed_work(&deferred_retry_work,
568 DEFERRED_DEVICE_TIMEOUT);
569 list_add_tail(&ddev->node, &deferred_devices);
571 mutex_unlock(&deferred_devices_lock);
575 EXPORT_SYMBOL_GPL(amba_device_add);
577 static struct amba_device *
578 amba_aphb_device_add(struct device *parent, const char *name,
579 resource_size_t base, size_t size, int irq1, int irq2,
580 void *pdata, unsigned int periphid, u64 dma_mask,
581 struct resource *resbase)
583 struct amba_device *dev;
586 dev = amba_device_alloc(name, base, size);
588 return ERR_PTR(-ENOMEM);
590 dev->dev.coherent_dma_mask = dma_mask;
593 dev->periphid = periphid;
594 dev->dev.platform_data = pdata;
595 dev->dev.parent = parent;
597 ret = amba_device_add(dev, resbase);
599 amba_device_put(dev);
607 amba_apb_device_add(struct device *parent, const char *name,
608 resource_size_t base, size_t size, int irq1, int irq2,
609 void *pdata, unsigned int periphid)
611 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
612 periphid, 0, &iomem_resource);
614 EXPORT_SYMBOL_GPL(amba_apb_device_add);
617 amba_ahb_device_add(struct device *parent, const char *name,
618 resource_size_t base, size_t size, int irq1, int irq2,
619 void *pdata, unsigned int periphid)
621 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
622 periphid, ~0ULL, &iomem_resource);
624 EXPORT_SYMBOL_GPL(amba_ahb_device_add);
627 amba_apb_device_add_res(struct device *parent, const char *name,
628 resource_size_t base, size_t size, int irq1,
629 int irq2, void *pdata, unsigned int periphid,
630 struct resource *resbase)
632 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
633 periphid, 0, resbase);
635 EXPORT_SYMBOL_GPL(amba_apb_device_add_res);
638 amba_ahb_device_add_res(struct device *parent, const char *name,
639 resource_size_t base, size_t size, int irq1,
640 int irq2, void *pdata, unsigned int periphid,
641 struct resource *resbase)
643 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
644 periphid, ~0ULL, resbase);
646 EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);
649 static void amba_device_initialize(struct amba_device *dev, const char *name)
651 device_initialize(&dev->dev);
653 dev_set_name(&dev->dev, "%s", name);
654 dev->dev.release = amba_device_release;
655 dev->dev.bus = &amba_bustype;
656 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
657 dev->dev.dma_parms = &dev->dma_parms;
658 dev->res.name = dev_name(&dev->dev);
662 * amba_device_alloc - allocate an AMBA device
663 * @name: sysfs name of the AMBA device
664 * @base: base of AMBA device
665 * @size: size of AMBA device
667 * Allocate and initialize an AMBA device structure. Returns %NULL
670 struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
673 struct amba_device *dev;
675 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
677 amba_device_initialize(dev, name);
678 dev->res.start = base;
679 dev->res.end = base + size - 1;
680 dev->res.flags = IORESOURCE_MEM;
685 EXPORT_SYMBOL_GPL(amba_device_alloc);
688 * amba_device_register - register an AMBA device
689 * @dev: AMBA device to register
690 * @parent: parent memory resource
692 * Setup the AMBA device, reading the cell ID if present.
693 * Claim the resource, and register the AMBA device with
694 * the Linux device manager.
696 int amba_device_register(struct amba_device *dev, struct resource *parent)
698 amba_device_initialize(dev, dev->dev.init_name);
699 dev->dev.init_name = NULL;
701 return amba_device_add(dev, parent);
705 * amba_device_put - put an AMBA device
706 * @dev: AMBA device to put
708 void amba_device_put(struct amba_device *dev)
710 put_device(&dev->dev);
712 EXPORT_SYMBOL_GPL(amba_device_put);
715 * amba_device_unregister - unregister an AMBA device
716 * @dev: AMBA device to remove
718 * Remove the specified AMBA device from the Linux device
719 * manager. All files associated with this object will be
720 * destroyed, and device drivers notified that the device has
721 * been removed. The AMBA device's resources including
722 * the amba_device structure will be freed once all
723 * references to it have been dropped.
725 void amba_device_unregister(struct amba_device *dev)
727 device_unregister(&dev->dev);
732 struct amba_device *dev;
733 struct device *parent;
739 static int amba_find_match(struct device *dev, void *data)
741 struct find_data *d = data;
742 struct amba_device *pcdev = to_amba_device(dev);
745 r = (pcdev->periphid & d->mask) == d->id;
747 r &= d->parent == dev->parent;
749 r &= strcmp(dev_name(dev), d->busid) == 0;
760 * amba_find_device - locate an AMBA device given a bus id
761 * @busid: bus id for device (or NULL)
762 * @parent: parent device (or NULL)
763 * @id: peripheral ID (or 0)
764 * @mask: peripheral ID mask (or 0)
766 * Return the AMBA device corresponding to the supplied parameters.
767 * If no device matches, returns NULL.
769 * NOTE: When a valid device is found, its refcount is
770 * incremented, and must be decremented before the returned
774 amba_find_device(const char *busid, struct device *parent, unsigned int id,
777 struct find_data data;
780 data.parent = parent;
785 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
791 * amba_request_regions - request all mem regions associated with device
792 * @dev: amba_device structure for device
793 * @name: name, or NULL to use driver name
795 int amba_request_regions(struct amba_device *dev, const char *name)
801 name = dev->dev.driver->name;
803 size = resource_size(&dev->res);
805 if (!request_mem_region(dev->res.start, size, name))
812 * amba_release_regions - release mem regions associated with device
813 * @dev: amba_device structure for device
815 * Release regions claimed by a successful call to amba_request_regions.
817 void amba_release_regions(struct amba_device *dev)
821 size = resource_size(&dev->res);
822 release_mem_region(dev->res.start, size);
825 EXPORT_SYMBOL(amba_driver_register);
826 EXPORT_SYMBOL(amba_driver_unregister);
827 EXPORT_SYMBOL(amba_device_register);
828 EXPORT_SYMBOL(amba_device_unregister);
829 EXPORT_SYMBOL(amba_find_device);
830 EXPORT_SYMBOL(amba_request_regions);
831 EXPORT_SYMBOL(amba_release_regions);