2 * linux/arch/arm/common/amba.c
4 * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
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.
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>
17 #include <linux/pm_runtime.h>
18 #include <linux/pm_domain.h>
19 #include <linux/amba/bus.h>
20 #include <linux/sizes.h>
24 #define to_amba_driver(d) container_of(d, struct amba_driver, drv)
26 static const struct amba_id *
27 amba_lookup(const struct amba_id *table, struct amba_device *dev)
32 ret = (dev->periphid & table->mask) == table->id;
38 return ret ? table : NULL;
41 static int amba_match(struct device *dev, struct device_driver *drv)
43 struct amba_device *pcdev = to_amba_device(dev);
44 struct amba_driver *pcdrv = to_amba_driver(drv);
46 return amba_lookup(pcdrv->id_table, pcdev) != NULL;
49 static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
51 struct amba_device *pcdev = to_amba_device(dev);
54 retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
58 retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);
62 #define amba_attr_func(name,fmt,arg...) \
63 static ssize_t name##_show(struct device *_dev, \
64 struct device_attribute *attr, char *buf) \
66 struct amba_device *dev = to_amba_device(_dev); \
67 return sprintf(buf, fmt, arg); \
70 #define amba_attr(name,fmt,arg...) \
71 amba_attr_func(name,fmt,arg) \
72 static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL)
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,
81 static struct device_attribute amba_dev_attrs[] = {
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
91 * does not result in loss of context.
93 static int amba_pm_runtime_suspend(struct device *dev)
95 struct amba_device *pcdev = to_amba_device(dev);
96 int ret = pm_generic_runtime_suspend(dev);
98 if (ret == 0 && dev->driver)
99 clk_disable_unprepare(pcdev->pclk);
104 static int amba_pm_runtime_resume(struct device *dev)
106 struct amba_device *pcdev = to_amba_device(dev);
110 ret = clk_prepare_enable(pcdev->pclk);
111 /* Failure is probably fatal to the system, but... */
116 return pm_generic_runtime_resume(dev);
120 static const struct dev_pm_ops amba_pm = {
121 .suspend = pm_generic_suspend,
122 .resume = pm_generic_resume,
123 .freeze = pm_generic_freeze,
124 .thaw = pm_generic_thaw,
125 .poweroff = pm_generic_poweroff,
126 .restore = pm_generic_restore,
127 SET_PM_RUNTIME_PM_OPS(
128 amba_pm_runtime_suspend,
129 amba_pm_runtime_resume,
135 * Primecells are part of the Advanced Microcontroller Bus Architecture,
136 * so we call the bus "amba".
138 struct bus_type amba_bustype = {
140 .dev_attrs = amba_dev_attrs,
142 .uevent = amba_uevent,
146 static int __init amba_init(void)
148 return bus_register(&amba_bustype);
151 postcore_initcall(amba_init);
153 static int amba_get_enable_pclk(struct amba_device *pcdev)
157 pcdev->pclk = clk_get(&pcdev->dev, "apb_pclk");
158 if (IS_ERR(pcdev->pclk))
159 return PTR_ERR(pcdev->pclk);
161 ret = clk_prepare_enable(pcdev->pclk);
163 clk_put(pcdev->pclk);
168 static void amba_put_disable_pclk(struct amba_device *pcdev)
170 clk_disable_unprepare(pcdev->pclk);
171 clk_put(pcdev->pclk);
175 * These are the device model conversion veneers; they convert the
176 * device model structures to our more specific structures.
178 static int amba_probe(struct device *dev)
180 struct amba_device *pcdev = to_amba_device(dev);
181 struct amba_driver *pcdrv = to_amba_driver(dev->driver);
182 const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
186 ret = dev_pm_domain_attach(dev, true);
187 if (ret == -EPROBE_DEFER)
190 ret = amba_get_enable_pclk(pcdev);
192 dev_pm_domain_detach(dev, true);
196 pm_runtime_get_noresume(dev);
197 pm_runtime_set_active(dev);
198 pm_runtime_enable(dev);
200 ret = pcdrv->probe(pcdev, id);
204 pm_runtime_disable(dev);
205 pm_runtime_set_suspended(dev);
206 pm_runtime_put_noidle(dev);
208 amba_put_disable_pclk(pcdev);
209 dev_pm_domain_detach(dev, true);
215 static int amba_remove(struct device *dev)
217 struct amba_device *pcdev = to_amba_device(dev);
218 struct amba_driver *drv = to_amba_driver(dev->driver);
221 pm_runtime_get_sync(dev);
222 ret = drv->remove(pcdev);
223 pm_runtime_put_noidle(dev);
225 /* Undo the runtime PM settings in amba_probe() */
226 pm_runtime_disable(dev);
227 pm_runtime_set_suspended(dev);
228 pm_runtime_put_noidle(dev);
230 amba_put_disable_pclk(pcdev);
231 dev_pm_domain_detach(dev, true);
236 static void amba_shutdown(struct device *dev)
238 struct amba_driver *drv = to_amba_driver(dev->driver);
239 drv->shutdown(to_amba_device(dev));
243 * amba_driver_register - register an AMBA device driver
244 * @drv: amba device driver structure
246 * Register an AMBA device driver with the Linux device model
247 * core. If devices pre-exist, the drivers probe function will
250 int amba_driver_register(struct amba_driver *drv)
252 drv->drv.bus = &amba_bustype;
254 #define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
259 return driver_register(&drv->drv);
263 * amba_driver_unregister - remove an AMBA device driver
264 * @drv: AMBA device driver structure to remove
266 * Unregister an AMBA device driver from the Linux device
267 * model. The device model will call the drivers remove function
268 * for each device the device driver is currently handling.
270 void amba_driver_unregister(struct amba_driver *drv)
272 driver_unregister(&drv->drv);
276 static void amba_device_release(struct device *dev)
278 struct amba_device *d = to_amba_device(dev);
281 release_resource(&d->res);
286 * amba_device_add - add a previously allocated AMBA device structure
287 * @dev: AMBA device allocated by amba_device_alloc
288 * @parent: resource parent for this devices resources
290 * Claim the resource, and read the device cell ID if not already
291 * initialized. Register the AMBA device with the Linux device
294 int amba_device_add(struct amba_device *dev, struct resource *parent)
300 WARN_ON(dev->irq[0] == (unsigned int)-1);
301 WARN_ON(dev->irq[1] == (unsigned int)-1);
303 ret = request_resource(parent, &dev->res);
307 /* Hard-coded primecell ID instead of plug-n-play */
308 if (dev->periphid != 0)
312 * Dynamically calculate the size of the resource
313 * and use this for iomap
315 size = resource_size(&dev->res);
316 tmp = ioremap(dev->res.start, size);
322 ret = amba_get_enable_pclk(dev);
327 * Read pid and cid based on size of resource
328 * they are located at end of region
330 for (pid = 0, i = 0; i < 4; i++)
331 pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
333 for (cid = 0, i = 0; i < 4; i++)
334 cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
337 amba_put_disable_pclk(dev);
352 ret = device_add(&dev->dev);
357 ret = device_create_file(&dev->dev, &dev_attr_irq0);
358 if (ret == 0 && dev->irq[1])
359 ret = device_create_file(&dev->dev, &dev_attr_irq1);
363 device_unregister(&dev->dev);
366 release_resource(&dev->res);
370 EXPORT_SYMBOL_GPL(amba_device_add);
372 static struct amba_device *
373 amba_aphb_device_add(struct device *parent, const char *name,
374 resource_size_t base, size_t size, int irq1, int irq2,
375 void *pdata, unsigned int periphid, u64 dma_mask,
376 struct resource *resbase)
378 struct amba_device *dev;
381 dev = amba_device_alloc(name, base, size);
383 return ERR_PTR(-ENOMEM);
385 dev->dev.coherent_dma_mask = dma_mask;
388 dev->periphid = periphid;
389 dev->dev.platform_data = pdata;
390 dev->dev.parent = parent;
392 ret = amba_device_add(dev, resbase);
394 amba_device_put(dev);
402 amba_apb_device_add(struct device *parent, const char *name,
403 resource_size_t base, size_t size, int irq1, int irq2,
404 void *pdata, unsigned int periphid)
406 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
407 periphid, 0, &iomem_resource);
409 EXPORT_SYMBOL_GPL(amba_apb_device_add);
412 amba_ahb_device_add(struct device *parent, const char *name,
413 resource_size_t base, size_t size, int irq1, int irq2,
414 void *pdata, unsigned int periphid)
416 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
417 periphid, ~0ULL, &iomem_resource);
419 EXPORT_SYMBOL_GPL(amba_ahb_device_add);
422 amba_apb_device_add_res(struct device *parent, const char *name,
423 resource_size_t base, size_t size, int irq1,
424 int irq2, void *pdata, unsigned int periphid,
425 struct resource *resbase)
427 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
428 periphid, 0, resbase);
430 EXPORT_SYMBOL_GPL(amba_apb_device_add_res);
433 amba_ahb_device_add_res(struct device *parent, const char *name,
434 resource_size_t base, size_t size, int irq1,
435 int irq2, void *pdata, unsigned int periphid,
436 struct resource *resbase)
438 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
439 periphid, ~0ULL, resbase);
441 EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);
444 static void amba_device_initialize(struct amba_device *dev, const char *name)
446 device_initialize(&dev->dev);
448 dev_set_name(&dev->dev, "%s", name);
449 dev->dev.release = amba_device_release;
450 dev->dev.bus = &amba_bustype;
451 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
452 dev->res.name = dev_name(&dev->dev);
456 * amba_device_alloc - allocate an AMBA device
457 * @name: sysfs name of the AMBA device
458 * @base: base of AMBA device
459 * @size: size of AMBA device
461 * Allocate and initialize an AMBA device structure. Returns %NULL
464 struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
467 struct amba_device *dev;
469 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
471 amba_device_initialize(dev, name);
472 dev->res.start = base;
473 dev->res.end = base + size - 1;
474 dev->res.flags = IORESOURCE_MEM;
479 EXPORT_SYMBOL_GPL(amba_device_alloc);
482 * amba_device_register - register an AMBA device
483 * @dev: AMBA device to register
484 * @parent: parent memory resource
486 * Setup the AMBA device, reading the cell ID if present.
487 * Claim the resource, and register the AMBA device with
488 * the Linux device manager.
490 int amba_device_register(struct amba_device *dev, struct resource *parent)
492 amba_device_initialize(dev, dev->dev.init_name);
493 dev->dev.init_name = NULL;
495 return amba_device_add(dev, parent);
499 * amba_device_put - put an AMBA device
500 * @dev: AMBA device to put
502 void amba_device_put(struct amba_device *dev)
504 put_device(&dev->dev);
506 EXPORT_SYMBOL_GPL(amba_device_put);
509 * amba_device_unregister - unregister an AMBA device
510 * @dev: AMBA device to remove
512 * Remove the specified AMBA device from the Linux device
513 * manager. All files associated with this object will be
514 * destroyed, and device drivers notified that the device has
515 * been removed. The AMBA device's resources including
516 * the amba_device structure will be freed once all
517 * references to it have been dropped.
519 void amba_device_unregister(struct amba_device *dev)
521 device_unregister(&dev->dev);
526 struct amba_device *dev;
527 struct device *parent;
533 static int amba_find_match(struct device *dev, void *data)
535 struct find_data *d = data;
536 struct amba_device *pcdev = to_amba_device(dev);
539 r = (pcdev->periphid & d->mask) == d->id;
541 r &= d->parent == dev->parent;
543 r &= strcmp(dev_name(dev), d->busid) == 0;
554 * amba_find_device - locate an AMBA device given a bus id
555 * @busid: bus id for device (or NULL)
556 * @parent: parent device (or NULL)
557 * @id: peripheral ID (or 0)
558 * @mask: peripheral ID mask (or 0)
560 * Return the AMBA device corresponding to the supplied parameters.
561 * If no device matches, returns NULL.
563 * NOTE: When a valid device is found, its refcount is
564 * incremented, and must be decremented before the returned
568 amba_find_device(const char *busid, struct device *parent, unsigned int id,
571 struct find_data data;
574 data.parent = parent;
579 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
585 * amba_request_regions - request all mem regions associated with device
586 * @dev: amba_device structure for device
587 * @name: name, or NULL to use driver name
589 int amba_request_regions(struct amba_device *dev, const char *name)
595 name = dev->dev.driver->name;
597 size = resource_size(&dev->res);
599 if (!request_mem_region(dev->res.start, size, name))
606 * amba_release_regions - release mem regions associated with device
607 * @dev: amba_device structure for device
609 * Release regions claimed by a successful call to amba_request_regions.
611 void amba_release_regions(struct amba_device *dev)
615 size = resource_size(&dev->res);
616 release_mem_region(dev->res.start, size);
619 EXPORT_SYMBOL(amba_driver_register);
620 EXPORT_SYMBOL(amba_driver_unregister);
621 EXPORT_SYMBOL(amba_device_register);
622 EXPORT_SYMBOL(amba_device_unregister);
623 EXPORT_SYMBOL(amba_find_device);
624 EXPORT_SYMBOL(amba_request_regions);
625 EXPORT_SYMBOL(amba_release_regions);