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