Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / amba / bus.c
blobecc304149067cafb06b9258df7333430dde7a0df
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/arch/arm/common/amba.c
5 * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
6 */
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>
12 #include <linux/io.h>
13 #include <linux/pm.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>
23 #include <asm/irq.h>
25 #define to_amba_driver(d) container_of(d, struct amba_driver, drv)
27 /* called on periphid match and class 0x9 coresight device. */
28 static int
29 amba_cs_uci_id_match(const struct amba_id *table, struct amba_device *dev)
31 int ret = 0;
32 struct amba_cs_uci_id *uci;
34 uci = table->data;
36 /* no table data or zero mask - return match on periphid */
37 if (!uci || (uci->devarch_mask == 0))
38 return 1;
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);
43 return ret;
46 static const struct amba_id *
47 amba_lookup(const struct amba_id *table, struct amba_device *dev)
49 while (table->mask) {
50 if (((dev->periphid & table->mask) == table->id) &&
51 ((dev->cid != CORESIGHT_CID) ||
52 (amba_cs_uci_id_match(table, dev))))
53 return table;
54 table++;
56 return NULL;
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);
74 int retval = 0;
76 retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
77 if (retval)
78 return retval;
80 retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);
81 return retval;
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);
88 ssize_t len;
90 device_lock(_dev);
91 len = sprintf(buf, "%s\n", dev->driver_override);
92 device_unlock(_dev);
93 return len;
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))
105 return -EINVAL;
107 driver_override = kstrndup(buf, count, GFP_KERNEL);
108 if (!driver_override)
109 return -ENOMEM;
111 cp = strchr(driver_override, '\n');
112 if (cp)
113 *cp = '\0';
115 device_lock(_dev);
116 old = dev->driver_override;
117 if (strlen(driver_override)) {
118 dev->driver_override = driver_override;
119 } else {
120 kfree(driver_override);
121 dev->driver_override = NULL;
123 device_unlock(_dev);
125 kfree(old);
127 return count;
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,
145 dev->res.flags);
147 static struct attribute *amba_dev_attrs[] = {
148 &dev_attr_id.attr,
149 &dev_attr_resource.attr,
150 &dev_attr_driver_override.attr,
151 NULL,
153 ATTRIBUTE_GROUPS(amba_dev);
155 #ifdef CONFIG_PM
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);
169 else
170 clk_disable_unprepare(pcdev->pclk);
173 return ret;
176 static int amba_pm_runtime_resume(struct device *dev)
178 struct amba_device *pcdev = to_amba_device(dev);
179 int ret;
181 if (dev->driver) {
182 if (pm_runtime_is_irq_safe(dev))
183 ret = clk_enable(pcdev->pclk);
184 else
185 ret = clk_prepare_enable(pcdev->pclk);
186 /* Failure is probably fatal to the system, but... */
187 if (ret)
188 return ret;
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,
202 SET_RUNTIME_PM_OPS(
203 amba_pm_runtime_suspend,
204 amba_pm_runtime_resume,
205 NULL
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 = {
216 .name = "amba",
217 .dev_groups = amba_dev_groups,
218 .match = amba_match,
219 .uevent = amba_uevent,
220 .dma_configure = platform_dma_configure,
221 .pm = &amba_pm,
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)
234 int ret;
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);
241 if (ret)
242 clk_put(pcdev->pclk);
244 return ret;
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);
262 int ret;
264 do {
265 ret = of_clk_set_defaults(dev->of_node, false);
266 if (ret < 0)
267 break;
269 ret = dev_pm_domain_attach(dev, true);
270 if (ret)
271 break;
273 ret = amba_get_enable_pclk(pcdev);
274 if (ret) {
275 dev_pm_domain_detach(dev, true);
276 break;
279 pm_runtime_get_noresume(dev);
280 pm_runtime_set_active(dev);
281 pm_runtime_enable(dev);
283 ret = pcdrv->probe(pcdev, id);
284 if (ret == 0)
285 break;
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);
293 } while (0);
295 return ret;
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);
302 int ret;
304 pm_runtime_get_sync(dev);
305 ret = drv->remove(pcdev);
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);
316 return ret;
319 static void amba_shutdown(struct device *dev)
321 struct amba_driver *drv = to_amba_driver(dev->driver);
322 drv->shutdown(to_amba_device(dev));
326 * amba_driver_register - register an AMBA device driver
327 * @drv: amba device driver structure
329 * Register an AMBA device driver with the Linux device model
330 * core. If devices pre-exist, the drivers probe function will
331 * be called.
333 int amba_driver_register(struct amba_driver *drv)
335 drv->drv.bus = &amba_bustype;
337 #define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
338 SETFN(probe);
339 SETFN(remove);
340 SETFN(shutdown);
342 return driver_register(&drv->drv);
346 * amba_driver_unregister - remove an AMBA device driver
347 * @drv: AMBA device driver structure to remove
349 * Unregister an AMBA device driver from the Linux device
350 * model. The device model will call the drivers remove function
351 * for each device the device driver is currently handling.
353 void amba_driver_unregister(struct amba_driver *drv)
355 driver_unregister(&drv->drv);
359 static void amba_device_release(struct device *dev)
361 struct amba_device *d = to_amba_device(dev);
363 if (d->res.parent)
364 release_resource(&d->res);
365 kfree(d);
368 static int amba_device_try_add(struct amba_device *dev, struct resource *parent)
370 u32 size;
371 void __iomem *tmp;
372 int i, ret;
374 WARN_ON(dev->irq[0] == (unsigned int)-1);
375 WARN_ON(dev->irq[1] == (unsigned int)-1);
377 ret = request_resource(parent, &dev->res);
378 if (ret)
379 goto err_out;
381 /* Hard-coded primecell ID instead of plug-n-play */
382 if (dev->periphid != 0)
383 goto skip_probe;
386 * Dynamically calculate the size of the resource
387 * and use this for iomap
389 size = resource_size(&dev->res);
390 tmp = ioremap(dev->res.start, size);
391 if (!tmp) {
392 ret = -ENOMEM;
393 goto err_release;
396 ret = dev_pm_domain_attach(&dev->dev, true);
397 if (ret) {
398 iounmap(tmp);
399 goto err_release;
402 ret = amba_get_enable_pclk(dev);
403 if (ret == 0) {
404 u32 pid, cid;
405 struct reset_control *rstc;
408 * Find reset control(s) of the amba bus and de-assert them.
410 rstc = of_reset_control_array_get_optional_shared(dev->dev.of_node);
411 if (IS_ERR(rstc)) {
412 ret = PTR_ERR(rstc);
413 if (ret != -EPROBE_DEFER)
414 dev_err(&dev->dev, "can't get reset: %d\n",
415 ret);
416 goto err_reset;
418 reset_control_deassert(rstc);
419 reset_control_put(rstc);
422 * Read pid and cid based on size of resource
423 * they are located at end of region
425 for (pid = 0, i = 0; i < 4; i++)
426 pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
427 (i * 8);
428 for (cid = 0, i = 0; i < 4; i++)
429 cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
430 (i * 8);
432 if (cid == CORESIGHT_CID) {
433 /* set the base to the start of the last 4k block */
434 void __iomem *csbase = tmp + size - 4096;
436 dev->uci.devarch =
437 readl(csbase + UCI_REG_DEVARCH_OFFSET);
438 dev->uci.devtype =
439 readl(csbase + UCI_REG_DEVTYPE_OFFSET) & 0xff;
442 amba_put_disable_pclk(dev);
444 if (cid == AMBA_CID || cid == CORESIGHT_CID) {
445 dev->periphid = pid;
446 dev->cid = cid;
449 if (!dev->periphid)
450 ret = -ENODEV;
453 iounmap(tmp);
454 dev_pm_domain_detach(&dev->dev, true);
456 if (ret)
457 goto err_release;
459 skip_probe:
460 ret = device_add(&dev->dev);
461 if (ret)
462 goto err_release;
464 if (dev->irq[0])
465 ret = device_create_file(&dev->dev, &dev_attr_irq0);
466 if (ret == 0 && dev->irq[1])
467 ret = device_create_file(&dev->dev, &dev_attr_irq1);
468 if (ret == 0)
469 return ret;
471 device_unregister(&dev->dev);
473 err_release:
474 release_resource(&dev->res);
475 err_out:
476 return ret;
478 err_reset:
479 amba_put_disable_pclk(dev);
480 iounmap(tmp);
481 dev_pm_domain_detach(&dev->dev, true);
482 goto err_release;
486 * Registration of AMBA device require reading its pid and cid registers.
487 * To do this, the device must be turned on (if it is a part of power domain)
488 * and have clocks enabled. However in some cases those resources might not be
489 * yet available. Returning EPROBE_DEFER is not a solution in such case,
490 * because callers don't handle this special error code. Instead such devices
491 * are added to the special list and their registration is retried from
492 * periodic worker, until all resources are available and registration succeeds.
494 struct deferred_device {
495 struct amba_device *dev;
496 struct resource *parent;
497 struct list_head node;
500 static LIST_HEAD(deferred_devices);
501 static DEFINE_MUTEX(deferred_devices_lock);
503 static void amba_deferred_retry_func(struct work_struct *dummy);
504 static DECLARE_DELAYED_WORK(deferred_retry_work, amba_deferred_retry_func);
506 #define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000))
508 static int amba_deferred_retry(void)
510 struct deferred_device *ddev, *tmp;
512 mutex_lock(&deferred_devices_lock);
514 list_for_each_entry_safe(ddev, tmp, &deferred_devices, node) {
515 int ret = amba_device_try_add(ddev->dev, ddev->parent);
517 if (ret == -EPROBE_DEFER)
518 continue;
520 list_del_init(&ddev->node);
521 kfree(ddev);
524 mutex_unlock(&deferred_devices_lock);
526 return 0;
528 late_initcall(amba_deferred_retry);
530 static void amba_deferred_retry_func(struct work_struct *dummy)
532 amba_deferred_retry();
534 if (!list_empty(&deferred_devices))
535 schedule_delayed_work(&deferred_retry_work,
536 DEFERRED_DEVICE_TIMEOUT);
540 * amba_device_add - add a previously allocated AMBA device structure
541 * @dev: AMBA device allocated by amba_device_alloc
542 * @parent: resource parent for this devices resources
544 * Claim the resource, and read the device cell ID if not already
545 * initialized. Register the AMBA device with the Linux device
546 * manager.
548 int amba_device_add(struct amba_device *dev, struct resource *parent)
550 int ret = amba_device_try_add(dev, parent);
552 if (ret == -EPROBE_DEFER) {
553 struct deferred_device *ddev;
555 ddev = kmalloc(sizeof(*ddev), GFP_KERNEL);
556 if (!ddev)
557 return -ENOMEM;
559 ddev->dev = dev;
560 ddev->parent = parent;
561 ret = 0;
563 mutex_lock(&deferred_devices_lock);
565 if (list_empty(&deferred_devices))
566 schedule_delayed_work(&deferred_retry_work,
567 DEFERRED_DEVICE_TIMEOUT);
568 list_add_tail(&ddev->node, &deferred_devices);
570 mutex_unlock(&deferred_devices_lock);
572 return ret;
574 EXPORT_SYMBOL_GPL(amba_device_add);
576 static struct amba_device *
577 amba_aphb_device_add(struct device *parent, const char *name,
578 resource_size_t base, size_t size, int irq1, int irq2,
579 void *pdata, unsigned int periphid, u64 dma_mask,
580 struct resource *resbase)
582 struct amba_device *dev;
583 int ret;
585 dev = amba_device_alloc(name, base, size);
586 if (!dev)
587 return ERR_PTR(-ENOMEM);
589 dev->dev.coherent_dma_mask = dma_mask;
590 dev->irq[0] = irq1;
591 dev->irq[1] = irq2;
592 dev->periphid = periphid;
593 dev->dev.platform_data = pdata;
594 dev->dev.parent = parent;
596 ret = amba_device_add(dev, resbase);
597 if (ret) {
598 amba_device_put(dev);
599 return ERR_PTR(ret);
602 return dev;
605 struct amba_device *
606 amba_apb_device_add(struct device *parent, const char *name,
607 resource_size_t base, size_t size, int irq1, int irq2,
608 void *pdata, unsigned int periphid)
610 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
611 periphid, 0, &iomem_resource);
613 EXPORT_SYMBOL_GPL(amba_apb_device_add);
615 struct amba_device *
616 amba_ahb_device_add(struct device *parent, const char *name,
617 resource_size_t base, size_t size, int irq1, int irq2,
618 void *pdata, unsigned int periphid)
620 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
621 periphid, ~0ULL, &iomem_resource);
623 EXPORT_SYMBOL_GPL(amba_ahb_device_add);
625 struct amba_device *
626 amba_apb_device_add_res(struct device *parent, const char *name,
627 resource_size_t base, size_t size, int irq1,
628 int irq2, void *pdata, unsigned int periphid,
629 struct resource *resbase)
631 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
632 periphid, 0, resbase);
634 EXPORT_SYMBOL_GPL(amba_apb_device_add_res);
636 struct amba_device *
637 amba_ahb_device_add_res(struct device *parent, const char *name,
638 resource_size_t base, size_t size, int irq1,
639 int irq2, void *pdata, unsigned int periphid,
640 struct resource *resbase)
642 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
643 periphid, ~0ULL, resbase);
645 EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);
648 static void amba_device_initialize(struct amba_device *dev, const char *name)
650 device_initialize(&dev->dev);
651 if (name)
652 dev_set_name(&dev->dev, "%s", name);
653 dev->dev.release = amba_device_release;
654 dev->dev.bus = &amba_bustype;
655 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
656 dev->dev.dma_parms = &dev->dma_parms;
657 dev->res.name = dev_name(&dev->dev);
661 * amba_device_alloc - allocate an AMBA device
662 * @name: sysfs name of the AMBA device
663 * @base: base of AMBA device
664 * @size: size of AMBA device
666 * Allocate and initialize an AMBA device structure. Returns %NULL
667 * on failure.
669 struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
670 size_t size)
672 struct amba_device *dev;
674 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
675 if (dev) {
676 amba_device_initialize(dev, name);
677 dev->res.start = base;
678 dev->res.end = base + size - 1;
679 dev->res.flags = IORESOURCE_MEM;
682 return dev;
684 EXPORT_SYMBOL_GPL(amba_device_alloc);
687 * amba_device_register - register an AMBA device
688 * @dev: AMBA device to register
689 * @parent: parent memory resource
691 * Setup the AMBA device, reading the cell ID if present.
692 * Claim the resource, and register the AMBA device with
693 * the Linux device manager.
695 int amba_device_register(struct amba_device *dev, struct resource *parent)
697 amba_device_initialize(dev, dev->dev.init_name);
698 dev->dev.init_name = NULL;
700 return amba_device_add(dev, parent);
704 * amba_device_put - put an AMBA device
705 * @dev: AMBA device to put
707 void amba_device_put(struct amba_device *dev)
709 put_device(&dev->dev);
711 EXPORT_SYMBOL_GPL(amba_device_put);
714 * amba_device_unregister - unregister an AMBA device
715 * @dev: AMBA device to remove
717 * Remove the specified AMBA device from the Linux device
718 * manager. All files associated with this object will be
719 * destroyed, and device drivers notified that the device has
720 * been removed. The AMBA device's resources including
721 * the amba_device structure will be freed once all
722 * references to it have been dropped.
724 void amba_device_unregister(struct amba_device *dev)
726 device_unregister(&dev->dev);
730 struct find_data {
731 struct amba_device *dev;
732 struct device *parent;
733 const char *busid;
734 unsigned int id;
735 unsigned int mask;
738 static int amba_find_match(struct device *dev, void *data)
740 struct find_data *d = data;
741 struct amba_device *pcdev = to_amba_device(dev);
742 int r;
744 r = (pcdev->periphid & d->mask) == d->id;
745 if (d->parent)
746 r &= d->parent == dev->parent;
747 if (d->busid)
748 r &= strcmp(dev_name(dev), d->busid) == 0;
750 if (r) {
751 get_device(dev);
752 d->dev = pcdev;
755 return r;
759 * amba_find_device - locate an AMBA device given a bus id
760 * @busid: bus id for device (or NULL)
761 * @parent: parent device (or NULL)
762 * @id: peripheral ID (or 0)
763 * @mask: peripheral ID mask (or 0)
765 * Return the AMBA device corresponding to the supplied parameters.
766 * If no device matches, returns NULL.
768 * NOTE: When a valid device is found, its refcount is
769 * incremented, and must be decremented before the returned
770 * reference.
772 struct amba_device *
773 amba_find_device(const char *busid, struct device *parent, unsigned int id,
774 unsigned int mask)
776 struct find_data data;
778 data.dev = NULL;
779 data.parent = parent;
780 data.busid = busid;
781 data.id = id;
782 data.mask = mask;
784 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
786 return data.dev;
790 * amba_request_regions - request all mem regions associated with device
791 * @dev: amba_device structure for device
792 * @name: name, or NULL to use driver name
794 int amba_request_regions(struct amba_device *dev, const char *name)
796 int ret = 0;
797 u32 size;
799 if (!name)
800 name = dev->dev.driver->name;
802 size = resource_size(&dev->res);
804 if (!request_mem_region(dev->res.start, size, name))
805 ret = -EBUSY;
807 return ret;
811 * amba_release_regions - release mem regions associated with device
812 * @dev: amba_device structure for device
814 * Release regions claimed by a successful call to amba_request_regions.
816 void amba_release_regions(struct amba_device *dev)
818 u32 size;
820 size = resource_size(&dev->res);
821 release_mem_region(dev->res.start, size);
824 EXPORT_SYMBOL(amba_driver_register);
825 EXPORT_SYMBOL(amba_driver_unregister);
826 EXPORT_SYMBOL(amba_device_register);
827 EXPORT_SYMBOL(amba_device_unregister);
828 EXPORT_SYMBOL(amba_find_device);
829 EXPORT_SYMBOL(amba_request_regions);
830 EXPORT_SYMBOL(amba_release_regions);