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 | a62c80e | 2006-01-07 13:52:45 +0000 | [diff] [blame] | 15 | #include <linux/amba/bus.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | #include <asm/io.h> |
| 18 | #include <asm/irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/sizes.h> |
| 20 | |
| 21 | #define to_amba_device(d) container_of(d, struct amba_device, dev) |
| 22 | #define to_amba_driver(d) container_of(d, struct amba_driver, drv) |
| 23 | |
| 24 | static struct amba_id * |
| 25 | amba_lookup(struct amba_id *table, struct amba_device *dev) |
| 26 | { |
| 27 | int ret = 0; |
| 28 | |
| 29 | while (table->mask) { |
| 30 | ret = (dev->periphid & table->mask) == table->id; |
| 31 | if (ret) |
| 32 | break; |
| 33 | table++; |
| 34 | } |
| 35 | |
| 36 | return ret ? table : NULL; |
| 37 | } |
| 38 | |
| 39 | static int amba_match(struct device *dev, struct device_driver *drv) |
| 40 | { |
| 41 | struct amba_device *pcdev = to_amba_device(dev); |
| 42 | struct amba_driver *pcdrv = to_amba_driver(drv); |
| 43 | |
| 44 | return amba_lookup(pcdrv->id_table, pcdev) != NULL; |
| 45 | } |
| 46 | |
| 47 | #ifdef CONFIG_HOTPLUG |
Paul Jackson | 6d20b03 | 2005-11-25 20:04:26 -0800 | [diff] [blame] | 48 | static int amba_uevent(struct device *dev, char **envp, int nr_env, char *buf, int bufsz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
| 50 | struct amba_device *pcdev = to_amba_device(dev); |
| 51 | |
| 52 | if (nr_env < 2) |
| 53 | return -ENOMEM; |
| 54 | |
| 55 | snprintf(buf, bufsz, "AMBA_ID=%08x", pcdev->periphid); |
| 56 | *envp++ = buf; |
| 57 | *envp++ = NULL; |
| 58 | return 0; |
| 59 | } |
| 60 | #else |
Paul Jackson | 6d20b03 | 2005-11-25 20:04:26 -0800 | [diff] [blame] | 61 | #define amba_uevent NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #endif |
| 63 | |
| 64 | static int amba_suspend(struct device *dev, pm_message_t state) |
| 65 | { |
| 66 | struct amba_driver *drv = to_amba_driver(dev->driver); |
| 67 | int ret = 0; |
| 68 | |
| 69 | if (dev->driver && drv->suspend) |
| 70 | ret = drv->suspend(to_amba_device(dev), state); |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | static int amba_resume(struct device *dev) |
| 75 | { |
| 76 | struct amba_driver *drv = to_amba_driver(dev->driver); |
| 77 | int ret = 0; |
| 78 | |
| 79 | if (dev->driver && drv->resume) |
| 80 | ret = drv->resume(to_amba_device(dev)); |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Primecells are part of the Advanced Microcontroller Bus Architecture, |
| 86 | * so we call the bus "amba". |
| 87 | */ |
| 88 | static struct bus_type amba_bustype = { |
| 89 | .name = "amba", |
| 90 | .match = amba_match, |
Paul Jackson | 6d20b03 | 2005-11-25 20:04:26 -0800 | [diff] [blame] | 91 | .uevent = amba_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | .suspend = amba_suspend, |
| 93 | .resume = amba_resume, |
| 94 | }; |
| 95 | |
| 96 | static int __init amba_init(void) |
| 97 | { |
| 98 | return bus_register(&amba_bustype); |
| 99 | } |
| 100 | |
| 101 | postcore_initcall(amba_init); |
| 102 | |
| 103 | /* |
| 104 | * These are the device model conversion veneers; they convert the |
| 105 | * device model structures to our more specific structures. |
| 106 | */ |
| 107 | static int amba_probe(struct device *dev) |
| 108 | { |
| 109 | struct amba_device *pcdev = to_amba_device(dev); |
| 110 | struct amba_driver *pcdrv = to_amba_driver(dev->driver); |
| 111 | struct amba_id *id; |
| 112 | |
| 113 | id = amba_lookup(pcdrv->id_table, pcdev); |
| 114 | |
| 115 | return pcdrv->probe(pcdev, id); |
| 116 | } |
| 117 | |
| 118 | static int amba_remove(struct device *dev) |
| 119 | { |
| 120 | struct amba_driver *drv = to_amba_driver(dev->driver); |
| 121 | return drv->remove(to_amba_device(dev)); |
| 122 | } |
| 123 | |
| 124 | static void amba_shutdown(struct device *dev) |
| 125 | { |
| 126 | struct amba_driver *drv = to_amba_driver(dev->driver); |
| 127 | drv->shutdown(to_amba_device(dev)); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * amba_driver_register - register an AMBA device driver |
| 132 | * @drv: amba device driver structure |
| 133 | * |
| 134 | * Register an AMBA device driver with the Linux device model |
| 135 | * core. If devices pre-exist, the drivers probe function will |
| 136 | * be called. |
| 137 | */ |
| 138 | int amba_driver_register(struct amba_driver *drv) |
| 139 | { |
| 140 | drv->drv.bus = &amba_bustype; |
| 141 | |
| 142 | #define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn |
| 143 | SETFN(probe); |
| 144 | SETFN(remove); |
| 145 | SETFN(shutdown); |
| 146 | |
| 147 | return driver_register(&drv->drv); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * amba_driver_unregister - remove an AMBA device driver |
| 152 | * @drv: AMBA device driver structure to remove |
| 153 | * |
| 154 | * Unregister an AMBA device driver from the Linux device |
| 155 | * model. The device model will call the drivers remove function |
| 156 | * for each device the device driver is currently handling. |
| 157 | */ |
| 158 | void amba_driver_unregister(struct amba_driver *drv) |
| 159 | { |
| 160 | driver_unregister(&drv->drv); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | static void amba_device_release(struct device *dev) |
| 165 | { |
| 166 | struct amba_device *d = to_amba_device(dev); |
| 167 | |
| 168 | if (d->res.parent) |
| 169 | release_resource(&d->res); |
| 170 | kfree(d); |
| 171 | } |
| 172 | |
| 173 | #define amba_attr(name,fmt,arg...) \ |
Yani Ioannou | ff381d2 | 2005-05-17 06:40:51 -0400 | [diff] [blame] | 174 | static ssize_t show_##name(struct device *_dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { \ |
| 176 | struct amba_device *dev = to_amba_device(_dev); \ |
| 177 | return sprintf(buf, fmt, arg); \ |
| 178 | } \ |
| 179 | static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) |
| 180 | |
| 181 | amba_attr(id, "%08x\n", dev->periphid); |
| 182 | amba_attr(irq0, "%u\n", dev->irq[0]); |
| 183 | amba_attr(irq1, "%u\n", dev->irq[1]); |
| 184 | amba_attr(resource, "\t%08lx\t%08lx\t%08lx\n", |
| 185 | dev->res.start, dev->res.end, dev->res.flags); |
| 186 | |
| 187 | /** |
| 188 | * amba_device_register - register an AMBA device |
| 189 | * @dev: AMBA device to register |
| 190 | * @parent: parent memory resource |
| 191 | * |
| 192 | * Setup the AMBA device, reading the cell ID if present. |
| 193 | * Claim the resource, and register the AMBA device with |
| 194 | * the Linux device manager. |
| 195 | */ |
| 196 | int amba_device_register(struct amba_device *dev, struct resource *parent) |
| 197 | { |
| 198 | u32 pid, cid; |
| 199 | void __iomem *tmp; |
| 200 | int i, ret; |
| 201 | |
| 202 | dev->dev.release = amba_device_release; |
| 203 | dev->dev.bus = &amba_bustype; |
| 204 | dev->dev.dma_mask = &dev->dma_mask; |
| 205 | dev->res.name = dev->dev.bus_id; |
| 206 | |
| 207 | if (!dev->dev.coherent_dma_mask && dev->dma_mask) |
| 208 | dev_warn(&dev->dev, "coherent dma mask is unset\n"); |
| 209 | |
| 210 | ret = request_resource(parent, &dev->res); |
| 211 | if (ret == 0) { |
| 212 | tmp = ioremap(dev->res.start, SZ_4K); |
| 213 | if (!tmp) { |
| 214 | ret = -ENOMEM; |
| 215 | goto out; |
| 216 | } |
| 217 | |
| 218 | for (pid = 0, i = 0; i < 4; i++) |
| 219 | pid |= (readl(tmp + 0xfe0 + 4 * i) & 255) << (i * 8); |
| 220 | for (cid = 0, i = 0; i < 4; i++) |
| 221 | cid |= (readl(tmp + 0xff0 + 4 * i) & 255) << (i * 8); |
| 222 | |
| 223 | iounmap(tmp); |
| 224 | |
| 225 | if (cid == 0xb105f00d) |
| 226 | dev->periphid = pid; |
| 227 | |
| 228 | if (dev->periphid) |
| 229 | ret = device_register(&dev->dev); |
| 230 | else |
| 231 | ret = -ENODEV; |
| 232 | |
| 233 | if (ret == 0) { |
| 234 | device_create_file(&dev->dev, &dev_attr_id); |
| 235 | if (dev->irq[0] != NO_IRQ) |
| 236 | device_create_file(&dev->dev, &dev_attr_irq0); |
| 237 | if (dev->irq[1] != NO_IRQ) |
| 238 | device_create_file(&dev->dev, &dev_attr_irq1); |
| 239 | device_create_file(&dev->dev, &dev_attr_resource); |
| 240 | } else { |
| 241 | out: |
| 242 | release_resource(&dev->res); |
| 243 | } |
| 244 | } |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * amba_device_unregister - unregister an AMBA device |
| 250 | * @dev: AMBA device to remove |
| 251 | * |
| 252 | * Remove the specified AMBA device from the Linux device |
| 253 | * manager. All files associated with this object will be |
| 254 | * destroyed, and device drivers notified that the device has |
| 255 | * been removed. The AMBA device's resources including |
| 256 | * the amba_device structure will be freed once all |
| 257 | * references to it have been dropped. |
| 258 | */ |
| 259 | void amba_device_unregister(struct amba_device *dev) |
| 260 | { |
| 261 | device_unregister(&dev->dev); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | struct find_data { |
| 266 | struct amba_device *dev; |
| 267 | struct device *parent; |
| 268 | const char *busid; |
| 269 | unsigned int id; |
| 270 | unsigned int mask; |
| 271 | }; |
| 272 | |
| 273 | static int amba_find_match(struct device *dev, void *data) |
| 274 | { |
| 275 | struct find_data *d = data; |
| 276 | struct amba_device *pcdev = to_amba_device(dev); |
| 277 | int r; |
| 278 | |
| 279 | r = (pcdev->periphid & d->mask) == d->id; |
| 280 | if (d->parent) |
| 281 | r &= d->parent == dev->parent; |
| 282 | if (d->busid) |
| 283 | r &= strcmp(dev->bus_id, d->busid) == 0; |
| 284 | |
| 285 | if (r) { |
| 286 | get_device(dev); |
| 287 | d->dev = pcdev; |
| 288 | } |
| 289 | |
| 290 | return r; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * amba_find_device - locate an AMBA device given a bus id |
| 295 | * @busid: bus id for device (or NULL) |
| 296 | * @parent: parent device (or NULL) |
| 297 | * @id: peripheral ID (or 0) |
| 298 | * @mask: peripheral ID mask (or 0) |
| 299 | * |
| 300 | * Return the AMBA device corresponding to the supplied parameters. |
| 301 | * If no device matches, returns NULL. |
| 302 | * |
| 303 | * NOTE: When a valid device is found, its refcount is |
| 304 | * incremented, and must be decremented before the returned |
| 305 | * reference. |
| 306 | */ |
| 307 | struct amba_device * |
| 308 | amba_find_device(const char *busid, struct device *parent, unsigned int id, |
| 309 | unsigned int mask) |
| 310 | { |
| 311 | struct find_data data; |
| 312 | |
| 313 | data.dev = NULL; |
| 314 | data.parent = parent; |
| 315 | data.busid = busid; |
| 316 | data.id = id; |
| 317 | data.mask = mask; |
| 318 | |
| 319 | bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match); |
| 320 | |
| 321 | return data.dev; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * amba_request_regions - request all mem regions associated with device |
| 326 | * @dev: amba_device structure for device |
| 327 | * @name: name, or NULL to use driver name |
| 328 | */ |
| 329 | int amba_request_regions(struct amba_device *dev, const char *name) |
| 330 | { |
| 331 | int ret = 0; |
| 332 | |
| 333 | if (!name) |
| 334 | name = dev->dev.driver->name; |
| 335 | |
| 336 | if (!request_mem_region(dev->res.start, SZ_4K, name)) |
| 337 | ret = -EBUSY; |
| 338 | |
| 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * amba_release_regions - release mem regions assoicated with device |
| 344 | * @dev: amba_device structure for device |
| 345 | * |
| 346 | * Release regions claimed by a successful call to amba_request_regions. |
| 347 | */ |
| 348 | void amba_release_regions(struct amba_device *dev) |
| 349 | { |
| 350 | release_mem_region(dev->res.start, SZ_4K); |
| 351 | } |
| 352 | |
| 353 | EXPORT_SYMBOL(amba_driver_register); |
| 354 | EXPORT_SYMBOL(amba_driver_unregister); |
| 355 | EXPORT_SYMBOL(amba_device_register); |
| 356 | EXPORT_SYMBOL(amba_device_unregister); |
| 357 | EXPORT_SYMBOL(amba_find_device); |
| 358 | EXPORT_SYMBOL(amba_request_regions); |
| 359 | EXPORT_SYMBOL(amba_release_regions); |