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>
21 #include <linux/limits.h>
25 #define to_amba_driver(d) container_of(d, struct amba_driver, drv)
27 static const struct amba_id *
28 amba_lookup(const struct amba_id *table, struct amba_device *dev)
33 ret = (dev->periphid & table->mask) == table->id;
39 return ret ? table : NULL;
42 static int amba_match(struct device *dev, struct device_driver *drv)
44 struct amba_device *pcdev = to_amba_device(dev);
45 struct amba_driver *pcdrv = to_amba_driver(drv);
47 /* When driver_override is set, only bind to the matching driver */
48 if (pcdev->driver_override)
49 return !strcmp(pcdev->driver_override, drv->name);
51 return amba_lookup(pcdrv->id_table, pcdev) != NULL;
54 static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
56 struct amba_device *pcdev = to_amba_device(dev);
59 retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
63 retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);
67 static ssize_t driver_override_show(struct device *_dev,
68 struct device_attribute *attr, char *buf)
70 struct amba_device *dev = to_amba_device(_dev);
74 len = sprintf(buf, "%s\n", dev->driver_override);
79 static ssize_t driver_override_store(struct device *_dev,
80 struct device_attribute *attr,
81 const char *buf, size_t count)
83 struct amba_device *dev = to_amba_device(_dev);
84 char *driver_override, *old, *cp;
86 /* We need to keep extra room for a newline */
87 if (count >= (PAGE_SIZE - 1))
90 driver_override = kstrndup(buf, count, GFP_KERNEL);
94 cp = strchr(driver_override, '\n');
99 old = dev->driver_override;
100 if (strlen(driver_override)) {
101 dev->driver_override = driver_override;
103 kfree(driver_override);
104 dev->driver_override = NULL;
113 #define amba_attr_func(name,fmt,arg...) \
114 static ssize_t name##_show(struct device *_dev, \
115 struct device_attribute *attr, char *buf) \
117 struct amba_device *dev = to_amba_device(_dev); \
118 return sprintf(buf, fmt, arg); \
121 #define amba_attr(name,fmt,arg...) \
122 amba_attr_func(name,fmt,arg) \
123 static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL)
125 amba_attr_func(id, "%08x\n", dev->periphid);
126 amba_attr(irq0, "%u\n", dev->irq[0]);
127 amba_attr(irq1, "%u\n", dev->irq[1]);
128 amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",
129 (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
132 static struct device_attribute amba_dev_attrs[] = {
135 __ATTR_RW(driver_override),
141 * Hooks to provide runtime PM of the pclk (bus clock). It is safe to
142 * enable/disable the bus clock at runtime PM suspend/resume as this
143 * does not result in loss of context.
145 static int amba_pm_runtime_suspend(struct device *dev)
147 struct amba_device *pcdev = to_amba_device(dev);
148 int ret = pm_generic_runtime_suspend(dev);
150 if (ret == 0 && dev->driver) {
151 if (pm_runtime_is_irq_safe(dev))
152 clk_disable(pcdev->pclk);
154 clk_disable_unprepare(pcdev->pclk);
160 static int amba_pm_runtime_resume(struct device *dev)
162 struct amba_device *pcdev = to_amba_device(dev);
166 if (pm_runtime_is_irq_safe(dev))
167 ret = clk_enable(pcdev->pclk);
169 ret = clk_prepare_enable(pcdev->pclk);
170 /* Failure is probably fatal to the system, but... */
175 return pm_generic_runtime_resume(dev);
177 #endif /* CONFIG_PM */
179 static const struct dev_pm_ops amba_pm = {
180 .suspend = pm_generic_suspend,
181 .resume = pm_generic_resume,
182 .freeze = pm_generic_freeze,
183 .thaw = pm_generic_thaw,
184 .poweroff = pm_generic_poweroff,
185 .restore = pm_generic_restore,
187 amba_pm_runtime_suspend,
188 amba_pm_runtime_resume,
194 * Primecells are part of the Advanced Microcontroller Bus Architecture,
195 * so we call the bus "amba".
197 struct bus_type amba_bustype = {
199 .dev_attrs = amba_dev_attrs,
201 .uevent = amba_uevent,
205 static int __init amba_init(void)
207 return bus_register(&amba_bustype);
210 postcore_initcall(amba_init);
212 static int amba_get_enable_pclk(struct amba_device *pcdev)
216 pcdev->pclk = clk_get(&pcdev->dev, "apb_pclk");
217 if (IS_ERR(pcdev->pclk))
218 return PTR_ERR(pcdev->pclk);
220 ret = clk_prepare_enable(pcdev->pclk);
222 clk_put(pcdev->pclk);
227 static void amba_put_disable_pclk(struct amba_device *pcdev)
229 clk_disable_unprepare(pcdev->pclk);
230 clk_put(pcdev->pclk);
234 * These are the device model conversion veneers; they convert the
235 * device model structures to our more specific structures.
237 static int amba_probe(struct device *dev)
239 struct amba_device *pcdev = to_amba_device(dev);
240 struct amba_driver *pcdrv = to_amba_driver(dev->driver);
241 const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
245 ret = dev_pm_domain_attach(dev, true);
246 if (ret == -EPROBE_DEFER)
249 ret = amba_get_enable_pclk(pcdev);
251 dev_pm_domain_detach(dev, true);
255 pm_runtime_get_noresume(dev);
256 pm_runtime_set_active(dev);
257 pm_runtime_enable(dev);
259 ret = pcdrv->probe(pcdev, id);
263 pm_runtime_disable(dev);
264 pm_runtime_set_suspended(dev);
265 pm_runtime_put_noidle(dev);
267 amba_put_disable_pclk(pcdev);
268 dev_pm_domain_detach(dev, true);
274 static int amba_remove(struct device *dev)
276 struct amba_device *pcdev = to_amba_device(dev);
277 struct amba_driver *drv = to_amba_driver(dev->driver);
280 pm_runtime_get_sync(dev);
282 ret = drv->remove(pcdev);
283 pm_runtime_put_noidle(dev);
285 /* Undo the runtime PM settings in amba_probe() */
286 pm_runtime_disable(dev);
287 pm_runtime_set_suspended(dev);
288 pm_runtime_put_noidle(dev);
290 amba_put_disable_pclk(pcdev);
291 dev_pm_domain_detach(dev, true);
296 static void amba_shutdown(struct device *dev)
298 struct amba_driver *drv = to_amba_driver(dev->driver);
301 drv->shutdown(to_amba_device(dev));
305 * amba_driver_register - register an AMBA device driver
306 * @drv: amba device driver structure
308 * Register an AMBA device driver with the Linux device model
309 * core. If devices pre-exist, the drivers probe function will
312 int amba_driver_register(struct amba_driver *drv)
317 drv->drv.bus = &amba_bustype;
318 drv->drv.probe = amba_probe;
319 drv->drv.remove = amba_remove;
320 drv->drv.shutdown = amba_shutdown;
322 return driver_register(&drv->drv);
326 * amba_driver_unregister - remove an AMBA device driver
327 * @drv: AMBA device driver structure to remove
329 * Unregister an AMBA device driver from the Linux device
330 * model. The device model will call the drivers remove function
331 * for each device the device driver is currently handling.
333 void amba_driver_unregister(struct amba_driver *drv)
335 driver_unregister(&drv->drv);
339 static void amba_device_release(struct device *dev)
341 struct amba_device *d = to_amba_device(dev);
344 release_resource(&d->res);
349 * amba_device_add - add a previously allocated AMBA device structure
350 * @dev: AMBA device allocated by amba_device_alloc
351 * @parent: resource parent for this devices resources
353 * Claim the resource, and read the device cell ID if not already
354 * initialized. Register the AMBA device with the Linux device
357 int amba_device_add(struct amba_device *dev, struct resource *parent)
363 WARN_ON(dev->irq[0] == (unsigned int)-1);
364 WARN_ON(dev->irq[1] == (unsigned int)-1);
366 ret = request_resource(parent, &dev->res);
370 /* Hard-coded primecell ID instead of plug-n-play */
371 if (dev->periphid != 0)
375 * Dynamically calculate the size of the resource
376 * and use this for iomap
378 size = resource_size(&dev->res);
379 tmp = ioremap(dev->res.start, size);
385 ret = amba_get_enable_pclk(dev);
390 * Read pid and cid based on size of resource
391 * they are located at end of region
393 for (pid = 0, i = 0; i < 4; i++)
394 pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
396 for (cid = 0, i = 0; i < 4; i++)
397 cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
400 amba_put_disable_pclk(dev);
402 if (cid == AMBA_CID || cid == CORESIGHT_CID)
415 ret = device_add(&dev->dev);
420 ret = device_create_file(&dev->dev, &dev_attr_irq0);
421 if (ret == 0 && dev->irq[1])
422 ret = device_create_file(&dev->dev, &dev_attr_irq1);
426 device_unregister(&dev->dev);
429 release_resource(&dev->res);
433 EXPORT_SYMBOL_GPL(amba_device_add);
435 static struct amba_device *
436 amba_aphb_device_add(struct device *parent, const char *name,
437 resource_size_t base, size_t size, int irq1, int irq2,
438 void *pdata, unsigned int periphid, u64 dma_mask,
439 struct resource *resbase)
441 struct amba_device *dev;
444 dev = amba_device_alloc(name, base, size);
446 return ERR_PTR(-ENOMEM);
448 dev->dev.coherent_dma_mask = dma_mask;
451 dev->periphid = periphid;
452 dev->dev.platform_data = pdata;
453 dev->dev.parent = parent;
455 ret = amba_device_add(dev, resbase);
457 amba_device_put(dev);
465 amba_apb_device_add(struct device *parent, const char *name,
466 resource_size_t base, size_t size, int irq1, int irq2,
467 void *pdata, unsigned int periphid)
469 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
470 periphid, 0, &iomem_resource);
472 EXPORT_SYMBOL_GPL(amba_apb_device_add);
475 amba_ahb_device_add(struct device *parent, const char *name,
476 resource_size_t base, size_t size, int irq1, int irq2,
477 void *pdata, unsigned int periphid)
479 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
480 periphid, ~0ULL, &iomem_resource);
482 EXPORT_SYMBOL_GPL(amba_ahb_device_add);
485 amba_apb_device_add_res(struct device *parent, const char *name,
486 resource_size_t base, size_t size, int irq1,
487 int irq2, void *pdata, unsigned int periphid,
488 struct resource *resbase)
490 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
491 periphid, 0, resbase);
493 EXPORT_SYMBOL_GPL(amba_apb_device_add_res);
496 amba_ahb_device_add_res(struct device *parent, const char *name,
497 resource_size_t base, size_t size, int irq1,
498 int irq2, void *pdata, unsigned int periphid,
499 struct resource *resbase)
501 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
502 periphid, ~0ULL, resbase);
504 EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);
507 static void amba_device_initialize(struct amba_device *dev, const char *name)
509 device_initialize(&dev->dev);
511 dev_set_name(&dev->dev, "%s", name);
512 dev->dev.release = amba_device_release;
513 dev->dev.bus = &amba_bustype;
514 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
515 dev->res.name = dev_name(&dev->dev);
519 * amba_device_alloc - allocate an AMBA device
520 * @name: sysfs name of the AMBA device
521 * @base: base of AMBA device
522 * @size: size of AMBA device
524 * Allocate and initialize an AMBA device structure. Returns %NULL
527 struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
530 struct amba_device *dev;
532 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
534 amba_device_initialize(dev, name);
535 dev->res.start = base;
536 dev->res.end = base + size - 1;
537 dev->res.flags = IORESOURCE_MEM;
542 EXPORT_SYMBOL_GPL(amba_device_alloc);
545 * amba_device_register - register an AMBA device
546 * @dev: AMBA device to register
547 * @parent: parent memory resource
549 * Setup the AMBA device, reading the cell ID if present.
550 * Claim the resource, and register the AMBA device with
551 * the Linux device manager.
553 int amba_device_register(struct amba_device *dev, struct resource *parent)
555 amba_device_initialize(dev, dev->dev.init_name);
556 dev->dev.init_name = NULL;
558 return amba_device_add(dev, parent);
562 * amba_device_put - put an AMBA device
563 * @dev: AMBA device to put
565 void amba_device_put(struct amba_device *dev)
567 put_device(&dev->dev);
569 EXPORT_SYMBOL_GPL(amba_device_put);
572 * amba_device_unregister - unregister an AMBA device
573 * @dev: AMBA device to remove
575 * Remove the specified AMBA device from the Linux device
576 * manager. All files associated with this object will be
577 * destroyed, and device drivers notified that the device has
578 * been removed. The AMBA device's resources including
579 * the amba_device structure will be freed once all
580 * references to it have been dropped.
582 void amba_device_unregister(struct amba_device *dev)
584 device_unregister(&dev->dev);
589 struct amba_device *dev;
590 struct device *parent;
596 static int amba_find_match(struct device *dev, void *data)
598 struct find_data *d = data;
599 struct amba_device *pcdev = to_amba_device(dev);
602 r = (pcdev->periphid & d->mask) == d->id;
604 r &= d->parent == dev->parent;
606 r &= strcmp(dev_name(dev), d->busid) == 0;
617 * amba_find_device - locate an AMBA device given a bus id
618 * @busid: bus id for device (or NULL)
619 * @parent: parent device (or NULL)
620 * @id: peripheral ID (or 0)
621 * @mask: peripheral ID mask (or 0)
623 * Return the AMBA device corresponding to the supplied parameters.
624 * If no device matches, returns NULL.
626 * NOTE: When a valid device is found, its refcount is
627 * incremented, and must be decremented before the returned
631 amba_find_device(const char *busid, struct device *parent, unsigned int id,
634 struct find_data data;
637 data.parent = parent;
642 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
648 * amba_request_regions - request all mem regions associated with device
649 * @dev: amba_device structure for device
650 * @name: name, or NULL to use driver name
652 int amba_request_regions(struct amba_device *dev, const char *name)
658 name = dev->dev.driver->name;
660 size = resource_size(&dev->res);
662 if (!request_mem_region(dev->res.start, size, name))
669 * amba_release_regions - release mem regions associated with device
670 * @dev: amba_device structure for device
672 * Release regions claimed by a successful call to amba_request_regions.
674 void amba_release_regions(struct amba_device *dev)
678 size = resource_size(&dev->res);
679 release_mem_region(dev->res.start, size);
682 EXPORT_SYMBOL(amba_driver_register);
683 EXPORT_SYMBOL(amba_driver_unregister);
684 EXPORT_SYMBOL(amba_device_register);
685 EXPORT_SYMBOL(amba_device_unregister);
686 EXPORT_SYMBOL(amba_find_device);
687 EXPORT_SYMBOL(amba_request_regions);
688 EXPORT_SYMBOL(amba_release_regions);