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