^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/arch/arm/common/amba.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/pm_domain.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/amba/bus.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/sizes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/clk/clk-conf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/reset.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <asm/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define to_amba_driver(d) container_of(d, struct amba_driver, drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /* called on periphid match and class 0x9 coresight device. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) amba_cs_uci_id_match(const struct amba_id *table, struct amba_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct amba_cs_uci_id *uci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) uci = table->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* no table data or zero mask - return match on periphid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (!uci || (uci->devarch_mask == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* test against read devtype and masked devarch value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) ret = (dev->uci.devtype == uci->devtype) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) ((dev->uci.devarch & uci->devarch_mask) == uci->devarch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static const struct amba_id *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) amba_lookup(const struct amba_id *table, struct amba_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) while (table->mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (((dev->periphid & table->mask) == table->id) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) ((dev->cid != CORESIGHT_CID) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) (amba_cs_uci_id_match(table, dev))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) table++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static int amba_match(struct device *dev, struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct amba_device *pcdev = to_amba_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct amba_driver *pcdrv = to_amba_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /* When driver_override is set, only bind to the matching driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (pcdev->driver_override)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return !strcmp(pcdev->driver_override, drv->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return amba_lookup(pcdrv->id_table, pcdev) != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct amba_device *pcdev = to_amba_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static ssize_t driver_override_show(struct device *_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct amba_device *dev = to_amba_device(_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) ssize_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) device_lock(_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) len = sprintf(buf, "%s\n", dev->driver_override);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) device_unlock(_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static ssize_t driver_override_store(struct device *_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct amba_device *dev = to_amba_device(_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) char *driver_override, *old, *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /* We need to keep extra room for a newline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (count >= (PAGE_SIZE - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) driver_override = kstrndup(buf, count, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (!driver_override)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) cp = strchr(driver_override, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) *cp = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) device_lock(_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) old = dev->driver_override;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (strlen(driver_override)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) dev->driver_override = driver_override;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) kfree(driver_override);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) dev->driver_override = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) device_unlock(_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) kfree(old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static DEVICE_ATTR_RW(driver_override);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #define amba_attr_func(name,fmt,arg...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static ssize_t name##_show(struct device *_dev, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct device_attribute *attr, char *buf) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct amba_device *dev = to_amba_device(_dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return sprintf(buf, fmt, arg); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static DEVICE_ATTR_RO(name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) amba_attr_func(id, "%08x\n", dev->periphid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) amba_attr_func(irq0, "%u\n", dev->irq[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) amba_attr_func(irq1, "%u\n", dev->irq[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) dev->res.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static struct attribute *amba_dev_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) &dev_attr_id.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) &dev_attr_resource.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) &dev_attr_driver_override.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) ATTRIBUTE_GROUPS(amba_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * Hooks to provide runtime PM of the pclk (bus clock). It is safe to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * enable/disable the bus clock at runtime PM suspend/resume as this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * does not result in loss of context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static int amba_pm_runtime_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct amba_device *pcdev = to_amba_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int ret = pm_generic_runtime_suspend(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (ret == 0 && dev->driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (pm_runtime_is_irq_safe(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) clk_disable(pcdev->pclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) clk_disable_unprepare(pcdev->pclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static int amba_pm_runtime_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct amba_device *pcdev = to_amba_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (dev->driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (pm_runtime_is_irq_safe(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) ret = clk_enable(pcdev->pclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) ret = clk_prepare_enable(pcdev->pclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /* Failure is probably fatal to the system, but... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return pm_generic_runtime_resume(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) #endif /* CONFIG_PM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static const struct dev_pm_ops amba_pm = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) .suspend = pm_generic_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) .resume = pm_generic_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) .freeze = pm_generic_freeze,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) .thaw = pm_generic_thaw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) .poweroff = pm_generic_poweroff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) .restore = pm_generic_restore,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) SET_RUNTIME_PM_OPS(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) amba_pm_runtime_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) amba_pm_runtime_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * Primecells are part of the Advanced Microcontroller Bus Architecture,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * so we call the bus "amba".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * DMA configuration for platform and AMBA bus is same. So here we reuse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * platform's DMA config routine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct bus_type amba_bustype = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) .name = "amba",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) .dev_groups = amba_dev_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) .match = amba_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) .uevent = amba_uevent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) .dma_configure = platform_dma_configure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) .pm = &amba_pm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) EXPORT_SYMBOL_GPL(amba_bustype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static int __init amba_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return bus_register(&amba_bustype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) postcore_initcall(amba_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static int amba_get_enable_pclk(struct amba_device *pcdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) pcdev->pclk = clk_get(&pcdev->dev, "apb_pclk");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (IS_ERR(pcdev->pclk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return PTR_ERR(pcdev->pclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) ret = clk_prepare_enable(pcdev->pclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) clk_put(pcdev->pclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static void amba_put_disable_pclk(struct amba_device *pcdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) clk_disable_unprepare(pcdev->pclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) clk_put(pcdev->pclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * These are the device model conversion veneers; they convert the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * device model structures to our more specific structures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static int amba_probe(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) struct amba_device *pcdev = to_amba_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) struct amba_driver *pcdrv = to_amba_driver(dev->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) ret = of_clk_set_defaults(dev->of_node, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) ret = dev_pm_domain_attach(dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) ret = amba_get_enable_pclk(pcdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) dev_pm_domain_detach(dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) pm_runtime_get_noresume(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) pm_runtime_set_active(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) pm_runtime_enable(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) ret = pcdrv->probe(pcdev, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) pm_runtime_disable(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) pm_runtime_set_suspended(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) pm_runtime_put_noidle(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) amba_put_disable_pclk(pcdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) dev_pm_domain_detach(dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) } while (0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static int amba_remove(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) struct amba_device *pcdev = to_amba_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct amba_driver *drv = to_amba_driver(dev->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) pm_runtime_get_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (drv->remove)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) drv->remove(pcdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) pm_runtime_put_noidle(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) /* Undo the runtime PM settings in amba_probe() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) pm_runtime_disable(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) pm_runtime_set_suspended(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) pm_runtime_put_noidle(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) amba_put_disable_pclk(pcdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) dev_pm_domain_detach(dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static void amba_shutdown(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) struct amba_driver *drv = to_amba_driver(dev->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (drv->shutdown)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) drv->shutdown(to_amba_device(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * amba_driver_register - register an AMBA device driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * @drv: amba device driver structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * Register an AMBA device driver with the Linux device model
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * core. If devices pre-exist, the drivers probe function will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * be called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) int amba_driver_register(struct amba_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (!drv->probe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) drv->drv.bus = &amba_bustype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) drv->drv.probe = amba_probe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) drv->drv.remove = amba_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) drv->drv.shutdown = amba_shutdown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return driver_register(&drv->drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * amba_driver_unregister - remove an AMBA device driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * @drv: AMBA device driver structure to remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * Unregister an AMBA device driver from the Linux device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * model. The device model will call the drivers remove function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * for each device the device driver is currently handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) void amba_driver_unregister(struct amba_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) driver_unregister(&drv->drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static void amba_device_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) struct amba_device *d = to_amba_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (d->res.parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) release_resource(&d->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) kfree(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) static int amba_device_try_add(struct amba_device *dev, struct resource *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) u32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) void __iomem *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) ret = request_resource(parent, &dev->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) /* Hard-coded primecell ID instead of plug-n-play */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) if (dev->periphid != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) goto skip_probe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * Dynamically calculate the size of the resource
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * and use this for iomap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) size = resource_size(&dev->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) tmp = ioremap(dev->res.start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (!tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) goto err_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) ret = dev_pm_domain_attach(&dev->dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) iounmap(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) goto err_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) ret = amba_get_enable_pclk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) u32 pid, cid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) struct reset_control *rstc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * Find reset control(s) of the amba bus and de-assert them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) rstc = of_reset_control_array_get_optional_shared(dev->dev.of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (IS_ERR(rstc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) ret = PTR_ERR(rstc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) if (ret != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) dev_err(&dev->dev, "can't get reset: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) goto err_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) reset_control_deassert(rstc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) reset_control_put(rstc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * Read pid and cid based on size of resource
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * they are located at end of region
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) for (pid = 0, i = 0; i < 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) (i * 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) for (cid = 0, i = 0; i < 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) (i * 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (cid == CORESIGHT_CID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) /* set the base to the start of the last 4k block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) void __iomem *csbase = tmp + size - 4096;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) dev->uci.devarch =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) readl(csbase + UCI_REG_DEVARCH_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) dev->uci.devtype =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) readl(csbase + UCI_REG_DEVTYPE_OFFSET) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) amba_put_disable_pclk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) if (cid == AMBA_CID || cid == CORESIGHT_CID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) dev->periphid = pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) dev->cid = cid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) if (!dev->periphid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) iounmap(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) dev_pm_domain_detach(&dev->dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) goto err_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) skip_probe:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) ret = device_add(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) goto err_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) if (dev->irq[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) ret = device_create_file(&dev->dev, &dev_attr_irq0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (ret == 0 && dev->irq[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) ret = device_create_file(&dev->dev, &dev_attr_irq1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) device_unregister(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) err_release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) release_resource(&dev->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) err_reset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) amba_put_disable_pclk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) iounmap(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) dev_pm_domain_detach(&dev->dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) goto err_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * Registration of AMBA device require reading its pid and cid registers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) * To do this, the device must be turned on (if it is a part of power domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) * and have clocks enabled. However in some cases those resources might not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) * yet available. Returning EPROBE_DEFER is not a solution in such case,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) * because callers don't handle this special error code. Instead such devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) * are added to the special list and their registration is retried from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) * periodic worker, until all resources are available and registration succeeds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) struct deferred_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) struct amba_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) struct resource *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) static LIST_HEAD(deferred_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) static DEFINE_MUTEX(deferred_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) static void amba_deferred_retry_func(struct work_struct *dummy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) static DECLARE_DELAYED_WORK(deferred_retry_work, amba_deferred_retry_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) #define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) static int amba_deferred_retry(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) struct deferred_device *ddev, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) mutex_lock(&deferred_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) list_for_each_entry_safe(ddev, tmp, &deferred_devices, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) int ret = amba_device_try_add(ddev->dev, ddev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) if (ret == -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) list_del_init(&ddev->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) kfree(ddev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) mutex_unlock(&deferred_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) late_initcall(amba_deferred_retry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) static void amba_deferred_retry_func(struct work_struct *dummy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) amba_deferred_retry();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (!list_empty(&deferred_devices))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) schedule_delayed_work(&deferred_retry_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) DEFERRED_DEVICE_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) * amba_device_add - add a previously allocated AMBA device structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) * @dev: AMBA device allocated by amba_device_alloc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) * @parent: resource parent for this devices resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * Claim the resource, and read the device cell ID if not already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * initialized. Register the AMBA device with the Linux device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) * manager.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) int amba_device_add(struct amba_device *dev, struct resource *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) int ret = amba_device_try_add(dev, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (ret == -EPROBE_DEFER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) struct deferred_device *ddev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) ddev = kmalloc(sizeof(*ddev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if (!ddev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) ddev->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) ddev->parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) mutex_lock(&deferred_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (list_empty(&deferred_devices))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) schedule_delayed_work(&deferred_retry_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) DEFERRED_DEVICE_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) list_add_tail(&ddev->node, &deferred_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) mutex_unlock(&deferred_devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) EXPORT_SYMBOL_GPL(amba_device_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) static struct amba_device *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) amba_aphb_device_add(struct device *parent, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) resource_size_t base, size_t size, int irq1, int irq2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) void *pdata, unsigned int periphid, u64 dma_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) struct resource *resbase)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) struct amba_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) dev = amba_device_alloc(name, base, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) dev->dev.coherent_dma_mask = dma_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) dev->irq[0] = irq1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) dev->irq[1] = irq2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) dev->periphid = periphid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) dev->dev.platform_data = pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) dev->dev.parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) ret = amba_device_add(dev, resbase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) amba_device_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) struct amba_device *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) amba_apb_device_add(struct device *parent, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) resource_size_t base, size_t size, int irq1, int irq2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) void *pdata, unsigned int periphid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) periphid, 0, &iomem_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) EXPORT_SYMBOL_GPL(amba_apb_device_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) struct amba_device *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) amba_ahb_device_add(struct device *parent, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) resource_size_t base, size_t size, int irq1, int irq2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) void *pdata, unsigned int periphid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) periphid, ~0ULL, &iomem_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) EXPORT_SYMBOL_GPL(amba_ahb_device_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) struct amba_device *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) amba_apb_device_add_res(struct device *parent, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) resource_size_t base, size_t size, int irq1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) int irq2, void *pdata, unsigned int periphid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) struct resource *resbase)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) periphid, 0, resbase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) EXPORT_SYMBOL_GPL(amba_apb_device_add_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) struct amba_device *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) amba_ahb_device_add_res(struct device *parent, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) resource_size_t base, size_t size, int irq1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) int irq2, void *pdata, unsigned int periphid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) struct resource *resbase)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) periphid, ~0ULL, resbase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) static void amba_device_initialize(struct amba_device *dev, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) device_initialize(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) dev_set_name(&dev->dev, "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) dev->dev.release = amba_device_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) dev->dev.bus = &amba_bustype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) dev->dev.dma_parms = &dev->dma_parms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) dev->res.name = dev_name(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) * amba_device_alloc - allocate an AMBA device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) * @name: sysfs name of the AMBA device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) * @base: base of AMBA device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) * @size: size of AMBA device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) * Allocate and initialize an AMBA device structure. Returns %NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) * on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) struct amba_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) dev = kzalloc(sizeof(*dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) if (dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) amba_device_initialize(dev, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) dev->res.start = base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) dev->res.end = base + size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) dev->res.flags = IORESOURCE_MEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) EXPORT_SYMBOL_GPL(amba_device_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) * amba_device_register - register an AMBA device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) * @dev: AMBA device to register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) * @parent: parent memory resource
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) * Setup the AMBA device, reading the cell ID if present.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) * Claim the resource, and register the AMBA device with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) * the Linux device manager.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) int amba_device_register(struct amba_device *dev, struct resource *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) amba_device_initialize(dev, dev->dev.init_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) dev->dev.init_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) return amba_device_add(dev, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) * amba_device_put - put an AMBA device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) * @dev: AMBA device to put
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) void amba_device_put(struct amba_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) put_device(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) EXPORT_SYMBOL_GPL(amba_device_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) * amba_device_unregister - unregister an AMBA device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) * @dev: AMBA device to remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) * Remove the specified AMBA device from the Linux device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) * manager. All files associated with this object will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) * destroyed, and device drivers notified that the device has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) * been removed. The AMBA device's resources including
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) * the amba_device structure will be freed once all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) * references to it have been dropped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) void amba_device_unregister(struct amba_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) device_unregister(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) struct find_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) struct amba_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) struct device *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) const char *busid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) unsigned int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) unsigned int mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) static int amba_find_match(struct device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) struct find_data *d = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) struct amba_device *pcdev = to_amba_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) r = (pcdev->periphid & d->mask) == d->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (d->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) r &= d->parent == dev->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) if (d->busid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) r &= strcmp(dev_name(dev), d->busid) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) get_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) d->dev = pcdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) * amba_find_device - locate an AMBA device given a bus id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) * @busid: bus id for device (or NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) * @parent: parent device (or NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) * @id: peripheral ID (or 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) * @mask: peripheral ID mask (or 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) * Return the AMBA device corresponding to the supplied parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) * If no device matches, returns NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) * NOTE: When a valid device is found, its refcount is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) * incremented, and must be decremented before the returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) * reference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) struct amba_device *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) amba_find_device(const char *busid, struct device *parent, unsigned int id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) unsigned int mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) struct find_data data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) data.dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) data.parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) data.busid = busid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) data.id = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) data.mask = mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) return data.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) * amba_request_regions - request all mem regions associated with device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) * @dev: amba_device structure for device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) * @name: name, or NULL to use driver name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) int amba_request_regions(struct amba_device *dev, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) u32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) name = dev->dev.driver->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) size = resource_size(&dev->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) if (!request_mem_region(dev->res.start, size, name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) * amba_release_regions - release mem regions associated with device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) * @dev: amba_device structure for device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) * Release regions claimed by a successful call to amba_request_regions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) void amba_release_regions(struct amba_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) u32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) size = resource_size(&dev->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) release_mem_region(dev->res.start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) EXPORT_SYMBOL(amba_driver_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) EXPORT_SYMBOL(amba_driver_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) EXPORT_SYMBOL(amba_device_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) EXPORT_SYMBOL(amba_device_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) EXPORT_SYMBOL(amba_find_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) EXPORT_SYMBOL(amba_request_regions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) EXPORT_SYMBOL(amba_release_regions);