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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Russell King | 934848d | 2009-01-08 09:58:51 +0000 | [diff] [blame] | 22 | #include <asm/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 | |
Russell King | c862aab | 2011-02-19 15:55:26 +0000 | [diff] [blame] | 26 | static const struct amba_id * |
| 27 | amba_lookup(const struct amba_id *table, struct amba_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
| 29 | int ret = 0; |
| 30 | |
| 31 | while (table->mask) { |
| 32 | ret = (dev->periphid & table->mask) == table->id; |
| 33 | if (ret) |
| 34 | break; |
| 35 | table++; |
| 36 | } |
| 37 | |
| 38 | return ret ? table : NULL; |
| 39 | } |
| 40 | |
| 41 | static int amba_match(struct device *dev, struct device_driver *drv) |
| 42 | { |
| 43 | struct amba_device *pcdev = to_amba_device(dev); |
| 44 | struct amba_driver *pcdrv = to_amba_driver(drv); |
| 45 | |
| 46 | return amba_lookup(pcdrv->id_table, pcdev) != NULL; |
| 47 | } |
| 48 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 49 | static int amba_uevent(struct device *dev, struct kobj_uevent_env *env) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
| 51 | struct amba_device *pcdev = to_amba_device(dev); |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 52 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 54 | retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid); |
Dave Martin | 523817b | 2011-10-05 14:44:57 +0100 | [diff] [blame] | 55 | if (retval) |
| 56 | return retval; |
| 57 | |
| 58 | retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid); |
Eric Rannaud | bf62456 | 2007-03-30 22:23:12 -0700 | [diff] [blame] | 59 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Russell King | 96b13f5 | 2006-11-30 14:04:49 +0000 | [diff] [blame] | 62 | #define amba_attr_func(name,fmt,arg...) \ |
| 63 | static ssize_t name##_show(struct device *_dev, \ |
| 64 | struct device_attribute *attr, char *buf) \ |
| 65 | { \ |
| 66 | struct amba_device *dev = to_amba_device(_dev); \ |
| 67 | return sprintf(buf, fmt, arg); \ |
| 68 | } |
| 69 | |
| 70 | #define amba_attr(name,fmt,arg...) \ |
| 71 | amba_attr_func(name,fmt,arg) \ |
| 72 | static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL) |
| 73 | |
| 74 | amba_attr_func(id, "%08x\n", dev->periphid); |
| 75 | amba_attr(irq0, "%u\n", dev->irq[0]); |
| 76 | amba_attr(irq1, "%u\n", dev->irq[1]); |
| 77 | amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n", |
| 78 | (unsigned long long)dev->res.start, (unsigned long long)dev->res.end, |
| 79 | dev->res.flags); |
| 80 | |
| 81 | static struct device_attribute amba_dev_attrs[] = { |
| 82 | __ATTR_RO(id), |
| 83 | __ATTR_RO(resource), |
| 84 | __ATTR_NULL, |
| 85 | }; |
| 86 | |
Ulf Hansson | f210c53 | 2014-02-12 14:06:43 +0100 | [diff] [blame] | 87 | #ifdef CONFIG_PM |
Russell King | 92b97f0 | 2011-08-14 09:13:48 +0100 | [diff] [blame] | 88 | /* |
| 89 | * Hooks to provide runtime PM of the pclk (bus clock). It is safe to |
| 90 | * enable/disable the bus clock at runtime PM suspend/resume as this |
Mark Brown | 1e45860 | 2012-04-13 13:11:50 +0100 | [diff] [blame] | 91 | * does not result in loss of context. |
Russell King | 92b97f0 | 2011-08-14 09:13:48 +0100 | [diff] [blame] | 92 | */ |
| 93 | static int amba_pm_runtime_suspend(struct device *dev) |
| 94 | { |
| 95 | struct amba_device *pcdev = to_amba_device(dev); |
| 96 | int ret = pm_generic_runtime_suspend(dev); |
| 97 | |
Krzysztof Kozlowski | 5670c2a | 2014-11-14 09:48:27 +0100 | [diff] [blame] | 98 | if (ret == 0 && dev->driver) { |
| 99 | if (pm_runtime_is_irq_safe(dev)) |
| 100 | clk_disable(pcdev->pclk); |
| 101 | else |
| 102 | clk_disable_unprepare(pcdev->pclk); |
| 103 | } |
Russell King | 92b97f0 | 2011-08-14 09:13:48 +0100 | [diff] [blame] | 104 | |
| 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | static int amba_pm_runtime_resume(struct device *dev) |
| 109 | { |
| 110 | struct amba_device *pcdev = to_amba_device(dev); |
| 111 | int ret; |
| 112 | |
| 113 | if (dev->driver) { |
Krzysztof Kozlowski | 5670c2a | 2014-11-14 09:48:27 +0100 | [diff] [blame] | 114 | if (pm_runtime_is_irq_safe(dev)) |
| 115 | ret = clk_enable(pcdev->pclk); |
| 116 | else |
| 117 | ret = clk_prepare_enable(pcdev->pclk); |
Russell King | 92b97f0 | 2011-08-14 09:13:48 +0100 | [diff] [blame] | 118 | /* Failure is probably fatal to the system, but... */ |
| 119 | if (ret) |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | return pm_generic_runtime_resume(dev); |
| 124 | } |
Krzysztof Kozlowski | 5670c2a | 2014-11-14 09:48:27 +0100 | [diff] [blame] | 125 | #endif /* CONFIG_PM */ |
Russell King | 92b97f0 | 2011-08-14 09:13:48 +0100 | [diff] [blame] | 126 | |
Rabin Vincent | ba74ec7 | 2011-02-23 04:33:17 +0100 | [diff] [blame] | 127 | static const struct dev_pm_ops amba_pm = { |
Ulf Hansson | 26825cf | 2013-12-09 10:38:20 +0100 | [diff] [blame] | 128 | .suspend = pm_generic_suspend, |
| 129 | .resume = pm_generic_resume, |
| 130 | .freeze = pm_generic_freeze, |
| 131 | .thaw = pm_generic_thaw, |
| 132 | .poweroff = pm_generic_poweroff, |
| 133 | .restore = pm_generic_restore, |
Rafael J. Wysocki | 6ed23b8 | 2014-12-04 00:34:11 +0100 | [diff] [blame] | 134 | SET_RUNTIME_PM_OPS( |
Russell King | 92b97f0 | 2011-08-14 09:13:48 +0100 | [diff] [blame] | 135 | amba_pm_runtime_suspend, |
| 136 | amba_pm_runtime_resume, |
Rafael J. Wysocki | 45f0a85 | 2013-06-03 21:49:52 +0200 | [diff] [blame] | 137 | NULL |
Rabin Vincent | ba74ec7 | 2011-02-23 04:33:17 +0100 | [diff] [blame] | 138 | ) |
| 139 | }; |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | /* |
| 142 | * Primecells are part of the Advanced Microcontroller Bus Architecture, |
| 143 | * so we call the bus "amba". |
| 144 | */ |
Rob Herring | 394d5ae | 2011-02-12 15:58:25 +0100 | [diff] [blame] | 145 | struct bus_type amba_bustype = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | .name = "amba", |
Russell King | 96b13f5 | 2006-11-30 14:04:49 +0000 | [diff] [blame] | 147 | .dev_attrs = amba_dev_attrs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | .match = amba_match, |
Paul Jackson | 6d20b03 | 2005-11-25 20:04:26 -0800 | [diff] [blame] | 149 | .uevent = amba_uevent, |
Ulf Hansson | 26825cf | 2013-12-09 10:38:20 +0100 | [diff] [blame] | 150 | .pm = &amba_pm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | static int __init amba_init(void) |
| 154 | { |
| 155 | return bus_register(&amba_bustype); |
| 156 | } |
| 157 | |
| 158 | postcore_initcall(amba_init); |
| 159 | |
Russell King | 7cfe249 | 2010-07-15 10:47:14 +0100 | [diff] [blame] | 160 | static int amba_get_enable_pclk(struct amba_device *pcdev) |
| 161 | { |
Russell King | 7cfe249 | 2010-07-15 10:47:14 +0100 | [diff] [blame] | 162 | int ret; |
| 163 | |
Ulf Hansson | 89a5c98 | 2013-12-09 10:39:13 +0100 | [diff] [blame] | 164 | pcdev->pclk = clk_get(&pcdev->dev, "apb_pclk"); |
| 165 | if (IS_ERR(pcdev->pclk)) |
| 166 | return PTR_ERR(pcdev->pclk); |
Russell King | 7cfe249 | 2010-07-15 10:47:14 +0100 | [diff] [blame] | 167 | |
Ulf Hansson | 89a5c98 | 2013-12-09 10:39:13 +0100 | [diff] [blame] | 168 | ret = clk_prepare_enable(pcdev->pclk); |
| 169 | if (ret) |
| 170 | clk_put(pcdev->pclk); |
Russell King | 7cfe249 | 2010-07-15 10:47:14 +0100 | [diff] [blame] | 171 | |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | static void amba_put_disable_pclk(struct amba_device *pcdev) |
| 176 | { |
Ulf Hansson | 89a5c98 | 2013-12-09 10:39:13 +0100 | [diff] [blame] | 177 | clk_disable_unprepare(pcdev->pclk); |
| 178 | clk_put(pcdev->pclk); |
Russell King | 7cfe249 | 2010-07-15 10:47:14 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | /* |
| 182 | * These are the device model conversion veneers; they convert the |
| 183 | * device model structures to our more specific structures. |
| 184 | */ |
| 185 | static int amba_probe(struct device *dev) |
| 186 | { |
| 187 | struct amba_device *pcdev = to_amba_device(dev); |
| 188 | struct amba_driver *pcdrv = to_amba_driver(dev->driver); |
Russell King | c862aab | 2011-02-19 15:55:26 +0000 | [diff] [blame] | 189 | const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev); |
Russell King | 7cfe249 | 2010-07-15 10:47:14 +0100 | [diff] [blame] | 190 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Russell King | 7cfe249 | 2010-07-15 10:47:14 +0100 | [diff] [blame] | 192 | do { |
Ulf Hansson | 207f1a2 | 2014-09-19 20:27:42 +0200 | [diff] [blame] | 193 | ret = dev_pm_domain_attach(dev, true); |
| 194 | if (ret == -EPROBE_DEFER) |
Russell King | 7cfe249 | 2010-07-15 10:47:14 +0100 | [diff] [blame] | 195 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
Ulf Hansson | 207f1a2 | 2014-09-19 20:27:42 +0200 | [diff] [blame] | 197 | ret = amba_get_enable_pclk(pcdev); |
| 198 | if (ret) { |
| 199 | dev_pm_domain_detach(dev, true); |
| 200 | break; |
| 201 | } |
| 202 | |
Russell King | 92b97f0 | 2011-08-14 09:13:48 +0100 | [diff] [blame] | 203 | pm_runtime_get_noresume(dev); |
| 204 | pm_runtime_set_active(dev); |
| 205 | pm_runtime_enable(dev); |
| 206 | |
Russell King | 7cfe249 | 2010-07-15 10:47:14 +0100 | [diff] [blame] | 207 | ret = pcdrv->probe(pcdev, id); |
| 208 | if (ret == 0) |
| 209 | break; |
| 210 | |
Russell King | 92b97f0 | 2011-08-14 09:13:48 +0100 | [diff] [blame] | 211 | pm_runtime_disable(dev); |
| 212 | pm_runtime_set_suspended(dev); |
| 213 | pm_runtime_put_noidle(dev); |
| 214 | |
Russell King | 7cfe249 | 2010-07-15 10:47:14 +0100 | [diff] [blame] | 215 | amba_put_disable_pclk(pcdev); |
Ulf Hansson | 207f1a2 | 2014-09-19 20:27:42 +0200 | [diff] [blame] | 216 | dev_pm_domain_detach(dev, true); |
Russell King | 7cfe249 | 2010-07-15 10:47:14 +0100 | [diff] [blame] | 217 | } while (0); |
| 218 | |
| 219 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static int amba_remove(struct device *dev) |
| 223 | { |
Russell King | 7cfe249 | 2010-07-15 10:47:14 +0100 | [diff] [blame] | 224 | struct amba_device *pcdev = to_amba_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | struct amba_driver *drv = to_amba_driver(dev->driver); |
Russell King | 92b97f0 | 2011-08-14 09:13:48 +0100 | [diff] [blame] | 226 | int ret; |
| 227 | |
| 228 | pm_runtime_get_sync(dev); |
| 229 | ret = drv->remove(pcdev); |
| 230 | pm_runtime_put_noidle(dev); |
| 231 | |
| 232 | /* Undo the runtime PM settings in amba_probe() */ |
| 233 | pm_runtime_disable(dev); |
| 234 | pm_runtime_set_suspended(dev); |
| 235 | pm_runtime_put_noidle(dev); |
Russell King | 7cfe249 | 2010-07-15 10:47:14 +0100 | [diff] [blame] | 236 | |
| 237 | amba_put_disable_pclk(pcdev); |
Ulf Hansson | 207f1a2 | 2014-09-19 20:27:42 +0200 | [diff] [blame] | 238 | dev_pm_domain_detach(dev, true); |
Russell King | 7cfe249 | 2010-07-15 10:47:14 +0100 | [diff] [blame] | 239 | |
| 240 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static void amba_shutdown(struct device *dev) |
| 244 | { |
| 245 | struct amba_driver *drv = to_amba_driver(dev->driver); |
| 246 | drv->shutdown(to_amba_device(dev)); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * amba_driver_register - register an AMBA device driver |
| 251 | * @drv: amba device driver structure |
| 252 | * |
| 253 | * Register an AMBA device driver with the Linux device model |
| 254 | * core. If devices pre-exist, the drivers probe function will |
| 255 | * be called. |
| 256 | */ |
| 257 | int amba_driver_register(struct amba_driver *drv) |
| 258 | { |
| 259 | drv->drv.bus = &amba_bustype; |
| 260 | |
| 261 | #define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn |
| 262 | SETFN(probe); |
| 263 | SETFN(remove); |
| 264 | SETFN(shutdown); |
| 265 | |
| 266 | return driver_register(&drv->drv); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * amba_driver_unregister - remove an AMBA device driver |
| 271 | * @drv: AMBA device driver structure to remove |
| 272 | * |
| 273 | * Unregister an AMBA device driver from the Linux device |
| 274 | * model. The device model will call the drivers remove function |
| 275 | * for each device the device driver is currently handling. |
| 276 | */ |
| 277 | void amba_driver_unregister(struct amba_driver *drv) |
| 278 | { |
| 279 | driver_unregister(&drv->drv); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | static void amba_device_release(struct device *dev) |
| 284 | { |
| 285 | struct amba_device *d = to_amba_device(dev); |
| 286 | |
| 287 | if (d->res.parent) |
| 288 | release_resource(&d->res); |
| 289 | kfree(d); |
| 290 | } |
| 291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | /** |
Russell King | d5dc927 | 2011-12-18 11:07:47 +0000 | [diff] [blame] | 293 | * amba_device_add - add a previously allocated AMBA device structure |
| 294 | * @dev: AMBA device allocated by amba_device_alloc |
| 295 | * @parent: resource parent for this devices resources |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | * |
Russell King | d5dc927 | 2011-12-18 11:07:47 +0000 | [diff] [blame] | 297 | * Claim the resource, and read the device cell ID if not already |
| 298 | * initialized. Register the AMBA device with the Linux device |
| 299 | * manager. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | */ |
Russell King | d5dc927 | 2011-12-18 11:07:47 +0000 | [diff] [blame] | 301 | int amba_device_add(struct amba_device *dev, struct resource *parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { |
Leo Chen | 8afe0b9 | 2009-07-28 23:34:59 +0100 | [diff] [blame] | 303 | u32 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | void __iomem *tmp; |
| 305 | int i, ret; |
| 306 | |
Russell King | 2eac58d | 2011-12-18 11:43:56 +0000 | [diff] [blame] | 307 | WARN_ON(dev->irq[0] == (unsigned int)-1); |
| 308 | WARN_ON(dev->irq[1] == (unsigned int)-1); |
| 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | ret = request_resource(parent, &dev->res); |
Russell King | 96b13f5 | 2006-11-30 14:04:49 +0000 | [diff] [blame] | 311 | if (ret) |
| 312 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
Linus Walleij | 97ceed1 | 2011-03-24 16:12:40 +0100 | [diff] [blame] | 314 | /* Hard-coded primecell ID instead of plug-n-play */ |
| 315 | if (dev->periphid != 0) |
| 316 | goto skip_probe; |
| 317 | |
Leo Chen | 8afe0b9 | 2009-07-28 23:34:59 +0100 | [diff] [blame] | 318 | /* |
| 319 | * Dynamically calculate the size of the resource |
| 320 | * and use this for iomap |
| 321 | */ |
| 322 | size = resource_size(&dev->res); |
| 323 | tmp = ioremap(dev->res.start, size); |
Russell King | 96b13f5 | 2006-11-30 14:04:49 +0000 | [diff] [blame] | 324 | if (!tmp) { |
| 325 | ret = -ENOMEM; |
| 326 | goto err_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } |
Russell King | 96b13f5 | 2006-11-30 14:04:49 +0000 | [diff] [blame] | 328 | |
Russell King | 7cfe249 | 2010-07-15 10:47:14 +0100 | [diff] [blame] | 329 | ret = amba_get_enable_pclk(dev); |
| 330 | if (ret == 0) { |
| 331 | u32 pid, cid; |
| 332 | |
| 333 | /* |
| 334 | * Read pid and cid based on size of resource |
| 335 | * they are located at end of region |
| 336 | */ |
| 337 | for (pid = 0, i = 0; i < 4; i++) |
| 338 | pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) << |
| 339 | (i * 8); |
| 340 | for (cid = 0, i = 0; i < 4; i++) |
| 341 | cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) << |
| 342 | (i * 8); |
| 343 | |
| 344 | amba_put_disable_pclk(dev); |
| 345 | |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 346 | if (cid == AMBA_CID || cid == CORESIGHT_CID) |
Russell King | 7cfe249 | 2010-07-15 10:47:14 +0100 | [diff] [blame] | 347 | dev->periphid = pid; |
| 348 | |
| 349 | if (!dev->periphid) |
| 350 | ret = -ENODEV; |
| 351 | } |
Russell King | 96b13f5 | 2006-11-30 14:04:49 +0000 | [diff] [blame] | 352 | |
| 353 | iounmap(tmp); |
| 354 | |
Russell King | 7cfe249 | 2010-07-15 10:47:14 +0100 | [diff] [blame] | 355 | if (ret) |
Russell King | 96b13f5 | 2006-11-30 14:04:49 +0000 | [diff] [blame] | 356 | goto err_release; |
Russell King | 96b13f5 | 2006-11-30 14:04:49 +0000 | [diff] [blame] | 357 | |
Linus Walleij | 97ceed1 | 2011-03-24 16:12:40 +0100 | [diff] [blame] | 358 | skip_probe: |
Russell King | 557dca5 | 2009-07-05 22:39:08 +0100 | [diff] [blame] | 359 | ret = device_add(&dev->dev); |
Russell King | 96b13f5 | 2006-11-30 14:04:49 +0000 | [diff] [blame] | 360 | if (ret) |
| 361 | goto err_release; |
| 362 | |
Russell King | dfb8518 | 2012-05-03 11:33:15 +0100 | [diff] [blame] | 363 | if (dev->irq[0]) |
Russell King | 96b13f5 | 2006-11-30 14:04:49 +0000 | [diff] [blame] | 364 | ret = device_create_file(&dev->dev, &dev_attr_irq0); |
Russell King | dfb8518 | 2012-05-03 11:33:15 +0100 | [diff] [blame] | 365 | if (ret == 0 && dev->irq[1]) |
Russell King | 96b13f5 | 2006-11-30 14:04:49 +0000 | [diff] [blame] | 366 | ret = device_create_file(&dev->dev, &dev_attr_irq1); |
| 367 | if (ret == 0) |
| 368 | return ret; |
| 369 | |
| 370 | device_unregister(&dev->dev); |
| 371 | |
| 372 | err_release: |
| 373 | release_resource(&dev->res); |
| 374 | err_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | return ret; |
| 376 | } |
Russell King | d5dc927 | 2011-12-18 11:07:47 +0000 | [diff] [blame] | 377 | EXPORT_SYMBOL_GPL(amba_device_add); |
| 378 | |
Linus Walleij | 6026aa9 | 2012-04-03 11:58:42 +0100 | [diff] [blame] | 379 | static struct amba_device * |
| 380 | amba_aphb_device_add(struct device *parent, const char *name, |
| 381 | resource_size_t base, size_t size, int irq1, int irq2, |
Linus Walleij | 3ad909b | 2012-11-30 16:30:43 +0100 | [diff] [blame] | 382 | void *pdata, unsigned int periphid, u64 dma_mask, |
| 383 | struct resource *resbase) |
Linus Walleij | 6026aa9 | 2012-04-03 11:58:42 +0100 | [diff] [blame] | 384 | { |
| 385 | struct amba_device *dev; |
| 386 | int ret; |
| 387 | |
| 388 | dev = amba_device_alloc(name, base, size); |
| 389 | if (!dev) |
| 390 | return ERR_PTR(-ENOMEM); |
| 391 | |
Linus Walleij | 6026aa9 | 2012-04-03 11:58:42 +0100 | [diff] [blame] | 392 | dev->dev.coherent_dma_mask = dma_mask; |
| 393 | dev->irq[0] = irq1; |
| 394 | dev->irq[1] = irq2; |
| 395 | dev->periphid = periphid; |
| 396 | dev->dev.platform_data = pdata; |
| 397 | dev->dev.parent = parent; |
| 398 | |
Linus Walleij | 3ad909b | 2012-11-30 16:30:43 +0100 | [diff] [blame] | 399 | ret = amba_device_add(dev, resbase); |
Linus Walleij | 6026aa9 | 2012-04-03 11:58:42 +0100 | [diff] [blame] | 400 | if (ret) { |
| 401 | amba_device_put(dev); |
| 402 | return ERR_PTR(ret); |
| 403 | } |
| 404 | |
| 405 | return dev; |
| 406 | } |
| 407 | |
| 408 | struct amba_device * |
| 409 | amba_apb_device_add(struct device *parent, const char *name, |
| 410 | resource_size_t base, size_t size, int irq1, int irq2, |
| 411 | void *pdata, unsigned int periphid) |
| 412 | { |
| 413 | return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata, |
Linus Walleij | 3ad909b | 2012-11-30 16:30:43 +0100 | [diff] [blame] | 414 | periphid, 0, &iomem_resource); |
Linus Walleij | 6026aa9 | 2012-04-03 11:58:42 +0100 | [diff] [blame] | 415 | } |
| 416 | EXPORT_SYMBOL_GPL(amba_apb_device_add); |
| 417 | |
| 418 | struct amba_device * |
| 419 | amba_ahb_device_add(struct device *parent, const char *name, |
| 420 | resource_size_t base, size_t size, int irq1, int irq2, |
| 421 | void *pdata, unsigned int periphid) |
| 422 | { |
| 423 | return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata, |
Linus Walleij | 3ad909b | 2012-11-30 16:30:43 +0100 | [diff] [blame] | 424 | periphid, ~0ULL, &iomem_resource); |
Linus Walleij | 6026aa9 | 2012-04-03 11:58:42 +0100 | [diff] [blame] | 425 | } |
| 426 | EXPORT_SYMBOL_GPL(amba_ahb_device_add); |
| 427 | |
Linus Walleij | 3ad909b | 2012-11-30 16:30:43 +0100 | [diff] [blame] | 428 | struct amba_device * |
| 429 | amba_apb_device_add_res(struct device *parent, const char *name, |
| 430 | resource_size_t base, size_t size, int irq1, |
| 431 | int irq2, void *pdata, unsigned int periphid, |
| 432 | struct resource *resbase) |
| 433 | { |
| 434 | return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata, |
| 435 | periphid, 0, resbase); |
| 436 | } |
| 437 | EXPORT_SYMBOL_GPL(amba_apb_device_add_res); |
| 438 | |
| 439 | struct amba_device * |
| 440 | amba_ahb_device_add_res(struct device *parent, const char *name, |
| 441 | resource_size_t base, size_t size, int irq1, |
| 442 | int irq2, void *pdata, unsigned int periphid, |
| 443 | struct resource *resbase) |
| 444 | { |
| 445 | return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata, |
| 446 | periphid, ~0ULL, resbase); |
| 447 | } |
| 448 | EXPORT_SYMBOL_GPL(amba_ahb_device_add_res); |
| 449 | |
| 450 | |
Russell King | d5dc927 | 2011-12-18 11:07:47 +0000 | [diff] [blame] | 451 | static void amba_device_initialize(struct amba_device *dev, const char *name) |
| 452 | { |
| 453 | device_initialize(&dev->dev); |
| 454 | if (name) |
| 455 | dev_set_name(&dev->dev, "%s", name); |
| 456 | dev->dev.release = amba_device_release; |
| 457 | dev->dev.bus = &amba_bustype; |
Russell King | 446b2a9 | 2013-06-27 10:25:33 +0100 | [diff] [blame] | 458 | dev->dev.dma_mask = &dev->dev.coherent_dma_mask; |
Russell King | d5dc927 | 2011-12-18 11:07:47 +0000 | [diff] [blame] | 459 | dev->res.name = dev_name(&dev->dev); |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * amba_device_alloc - allocate an AMBA device |
| 464 | * @name: sysfs name of the AMBA device |
| 465 | * @base: base of AMBA device |
| 466 | * @size: size of AMBA device |
| 467 | * |
| 468 | * Allocate and initialize an AMBA device structure. Returns %NULL |
| 469 | * on failure. |
| 470 | */ |
| 471 | struct amba_device *amba_device_alloc(const char *name, resource_size_t base, |
| 472 | size_t size) |
| 473 | { |
| 474 | struct amba_device *dev; |
| 475 | |
| 476 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 477 | if (dev) { |
| 478 | amba_device_initialize(dev, name); |
| 479 | dev->res.start = base; |
| 480 | dev->res.end = base + size - 1; |
| 481 | dev->res.flags = IORESOURCE_MEM; |
| 482 | } |
| 483 | |
| 484 | return dev; |
| 485 | } |
| 486 | EXPORT_SYMBOL_GPL(amba_device_alloc); |
| 487 | |
| 488 | /** |
| 489 | * amba_device_register - register an AMBA device |
| 490 | * @dev: AMBA device to register |
| 491 | * @parent: parent memory resource |
| 492 | * |
| 493 | * Setup the AMBA device, reading the cell ID if present. |
| 494 | * Claim the resource, and register the AMBA device with |
| 495 | * the Linux device manager. |
| 496 | */ |
| 497 | int amba_device_register(struct amba_device *dev, struct resource *parent) |
| 498 | { |
| 499 | amba_device_initialize(dev, dev->dev.init_name); |
| 500 | dev->dev.init_name = NULL; |
| 501 | |
Russell King | d5dc927 | 2011-12-18 11:07:47 +0000 | [diff] [blame] | 502 | return amba_device_add(dev, parent); |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * amba_device_put - put an AMBA device |
| 507 | * @dev: AMBA device to put |
| 508 | */ |
| 509 | void amba_device_put(struct amba_device *dev) |
| 510 | { |
| 511 | put_device(&dev->dev); |
| 512 | } |
| 513 | EXPORT_SYMBOL_GPL(amba_device_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
| 515 | /** |
| 516 | * amba_device_unregister - unregister an AMBA device |
| 517 | * @dev: AMBA device to remove |
| 518 | * |
| 519 | * Remove the specified AMBA device from the Linux device |
| 520 | * manager. All files associated with this object will be |
| 521 | * destroyed, and device drivers notified that the device has |
| 522 | * been removed. The AMBA device's resources including |
| 523 | * the amba_device structure will be freed once all |
| 524 | * references to it have been dropped. |
| 525 | */ |
| 526 | void amba_device_unregister(struct amba_device *dev) |
| 527 | { |
| 528 | device_unregister(&dev->dev); |
| 529 | } |
| 530 | |
| 531 | |
| 532 | struct find_data { |
| 533 | struct amba_device *dev; |
| 534 | struct device *parent; |
| 535 | const char *busid; |
| 536 | unsigned int id; |
| 537 | unsigned int mask; |
| 538 | }; |
| 539 | |
| 540 | static int amba_find_match(struct device *dev, void *data) |
| 541 | { |
| 542 | struct find_data *d = data; |
| 543 | struct amba_device *pcdev = to_amba_device(dev); |
| 544 | int r; |
| 545 | |
| 546 | r = (pcdev->periphid & d->mask) == d->id; |
| 547 | if (d->parent) |
| 548 | r &= d->parent == dev->parent; |
| 549 | if (d->busid) |
Kay Sievers | 9d6b4c8 | 2009-03-24 16:38:22 -0700 | [diff] [blame] | 550 | r &= strcmp(dev_name(dev), d->busid) == 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
| 552 | if (r) { |
| 553 | get_device(dev); |
| 554 | d->dev = pcdev; |
| 555 | } |
| 556 | |
| 557 | return r; |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * amba_find_device - locate an AMBA device given a bus id |
| 562 | * @busid: bus id for device (or NULL) |
| 563 | * @parent: parent device (or NULL) |
| 564 | * @id: peripheral ID (or 0) |
| 565 | * @mask: peripheral ID mask (or 0) |
| 566 | * |
| 567 | * Return the AMBA device corresponding to the supplied parameters. |
| 568 | * If no device matches, returns NULL. |
| 569 | * |
| 570 | * NOTE: When a valid device is found, its refcount is |
| 571 | * incremented, and must be decremented before the returned |
| 572 | * reference. |
| 573 | */ |
| 574 | struct amba_device * |
| 575 | amba_find_device(const char *busid, struct device *parent, unsigned int id, |
| 576 | unsigned int mask) |
| 577 | { |
| 578 | struct find_data data; |
| 579 | |
| 580 | data.dev = NULL; |
| 581 | data.parent = parent; |
| 582 | data.busid = busid; |
| 583 | data.id = id; |
| 584 | data.mask = mask; |
| 585 | |
| 586 | bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match); |
| 587 | |
| 588 | return data.dev; |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * amba_request_regions - request all mem regions associated with device |
| 593 | * @dev: amba_device structure for device |
| 594 | * @name: name, or NULL to use driver name |
| 595 | */ |
| 596 | int amba_request_regions(struct amba_device *dev, const char *name) |
| 597 | { |
| 598 | int ret = 0; |
Leo Chen | 8afe0b9 | 2009-07-28 23:34:59 +0100 | [diff] [blame] | 599 | u32 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
| 601 | if (!name) |
| 602 | name = dev->dev.driver->name; |
| 603 | |
Leo Chen | 8afe0b9 | 2009-07-28 23:34:59 +0100 | [diff] [blame] | 604 | size = resource_size(&dev->res); |
| 605 | |
| 606 | if (!request_mem_region(dev->res.start, size, name)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | ret = -EBUSY; |
| 608 | |
| 609 | return ret; |
| 610 | } |
| 611 | |
| 612 | /** |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 613 | * amba_release_regions - release mem regions associated with device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | * @dev: amba_device structure for device |
| 615 | * |
| 616 | * Release regions claimed by a successful call to amba_request_regions. |
| 617 | */ |
| 618 | void amba_release_regions(struct amba_device *dev) |
| 619 | { |
Leo Chen | 8afe0b9 | 2009-07-28 23:34:59 +0100 | [diff] [blame] | 620 | u32 size; |
| 621 | |
| 622 | size = resource_size(&dev->res); |
| 623 | release_mem_region(dev->res.start, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | EXPORT_SYMBOL(amba_driver_register); |
| 627 | EXPORT_SYMBOL(amba_driver_unregister); |
| 628 | EXPORT_SYMBOL(amba_device_register); |
| 629 | EXPORT_SYMBOL(amba_device_unregister); |
| 630 | EXPORT_SYMBOL(amba_find_device); |
| 631 | EXPORT_SYMBOL(amba_request_regions); |
| 632 | EXPORT_SYMBOL(amba_release_regions); |