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