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