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/amba/bus.h>
19 #include <linux/sizes.h>
23 #define to_amba_driver(d) container_of(d, struct amba_driver, drv)
25 static const struct amba_id *
26 amba_lookup(const struct amba_id *table, struct amba_device *dev)
31 ret = (dev->periphid & table->mask) == table->id;
37 return ret ? table : NULL;
40 static int amba_match(struct device *dev, struct device_driver *drv)
42 struct amba_device *pcdev = to_amba_device(dev);
43 struct amba_driver *pcdrv = to_amba_driver(drv);
45 return amba_lookup(pcdrv->id_table, pcdev) != NULL;
48 static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
50 struct amba_device *pcdev = to_amba_device(dev);
53 retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
57 retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);
61 #define amba_attr_func(name,fmt,arg...) \
62 static ssize_t name##_show(struct device *_dev, \
63 struct device_attribute *attr, char *buf) \
65 struct amba_device *dev = to_amba_device(_dev); \
66 return sprintf(buf, fmt, arg); \
69 #define amba_attr(name,fmt,arg...) \
70 amba_attr_func(name,fmt,arg) \
71 static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL)
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,
80 static struct device_attribute amba_dev_attrs[] = {
86 #ifdef CONFIG_PM_SLEEP
88 static int amba_legacy_suspend(struct device *dev, pm_message_t mesg)
90 struct amba_driver *adrv = to_amba_driver(dev->driver);
91 struct amba_device *adev = to_amba_device(dev);
94 if (dev->driver && adrv->suspend)
95 ret = adrv->suspend(adev, mesg);
100 static int amba_legacy_resume(struct device *dev)
102 struct amba_driver *adrv = to_amba_driver(dev->driver);
103 struct amba_device *adev = to_amba_device(dev);
106 if (dev->driver && adrv->resume)
107 ret = adrv->resume(adev);
112 #endif /* CONFIG_PM_SLEEP */
114 #ifdef CONFIG_SUSPEND
116 static int amba_pm_suspend(struct device *dev)
118 struct device_driver *drv = dev->driver;
125 if (drv->pm->suspend)
126 ret = drv->pm->suspend(dev);
128 ret = amba_legacy_suspend(dev, PMSG_SUSPEND);
134 static int amba_pm_resume(struct device *dev)
136 struct device_driver *drv = dev->driver;
144 ret = drv->pm->resume(dev);
146 ret = amba_legacy_resume(dev);
152 #else /* !CONFIG_SUSPEND */
154 #define amba_pm_suspend NULL
155 #define amba_pm_resume NULL
157 #endif /* !CONFIG_SUSPEND */
159 #ifdef CONFIG_HIBERNATE_CALLBACKS
161 static int amba_pm_freeze(struct device *dev)
163 struct device_driver *drv = dev->driver;
171 ret = drv->pm->freeze(dev);
173 ret = amba_legacy_suspend(dev, PMSG_FREEZE);
179 static int amba_pm_thaw(struct device *dev)
181 struct device_driver *drv = dev->driver;
189 ret = drv->pm->thaw(dev);
191 ret = amba_legacy_resume(dev);
197 static int amba_pm_poweroff(struct device *dev)
199 struct device_driver *drv = dev->driver;
206 if (drv->pm->poweroff)
207 ret = drv->pm->poweroff(dev);
209 ret = amba_legacy_suspend(dev, PMSG_HIBERNATE);
215 static int amba_pm_restore(struct device *dev)
217 struct device_driver *drv = dev->driver;
224 if (drv->pm->restore)
225 ret = drv->pm->restore(dev);
227 ret = amba_legacy_resume(dev);
233 #else /* !CONFIG_HIBERNATE_CALLBACKS */
235 #define amba_pm_freeze NULL
236 #define amba_pm_thaw NULL
237 #define amba_pm_poweroff NULL
238 #define amba_pm_restore NULL
240 #endif /* !CONFIG_HIBERNATE_CALLBACKS */
242 #ifdef CONFIG_PM_RUNTIME
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.
248 static int amba_pm_runtime_suspend(struct device *dev)
250 struct amba_device *pcdev = to_amba_device(dev);
251 int ret = pm_generic_runtime_suspend(dev);
253 if (ret == 0 && dev->driver)
254 clk_disable(pcdev->pclk);
259 static int amba_pm_runtime_resume(struct device *dev)
261 struct amba_device *pcdev = to_amba_device(dev);
265 ret = clk_enable(pcdev->pclk);
266 /* Failure is probably fatal to the system, but... */
271 return pm_generic_runtime_resume(dev);
277 static const struct dev_pm_ops amba_pm = {
278 .suspend = amba_pm_suspend,
279 .resume = amba_pm_resume,
280 .freeze = amba_pm_freeze,
281 .thaw = amba_pm_thaw,
282 .poweroff = amba_pm_poweroff,
283 .restore = amba_pm_restore,
285 amba_pm_runtime_suspend,
286 amba_pm_runtime_resume,
291 #define AMBA_PM (&amba_pm)
293 #else /* !CONFIG_PM */
297 #endif /* !CONFIG_PM */
300 * Primecells are part of the Advanced Microcontroller Bus Architecture,
301 * so we call the bus "amba".
303 struct bus_type amba_bustype = {
305 .dev_attrs = amba_dev_attrs,
307 .uevent = amba_uevent,
311 static int __init amba_init(void)
313 return bus_register(&amba_bustype);
316 postcore_initcall(amba_init);
318 static int amba_get_enable_pclk(struct amba_device *pcdev)
320 struct clk *pclk = clk_get(&pcdev->dev, "apb_pclk");
326 return PTR_ERR(pclk);
328 ret = clk_prepare(pclk);
334 ret = clk_enable(pclk);
343 static void amba_put_disable_pclk(struct amba_device *pcdev)
345 struct clk *pclk = pcdev->pclk;
353 * These are the device model conversion veneers; they convert the
354 * device model structures to our more specific structures.
356 static int amba_probe(struct device *dev)
358 struct amba_device *pcdev = to_amba_device(dev);
359 struct amba_driver *pcdrv = to_amba_driver(dev->driver);
360 const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
364 //ret = amba_get_enable_pclk(pcdev);
368 pm_runtime_get_noresume(dev);
369 pm_runtime_set_active(dev);
370 pm_runtime_enable(dev);
372 ret = pcdrv->probe(pcdev, id);
376 pm_runtime_disable(dev);
377 pm_runtime_set_suspended(dev);
378 pm_runtime_put_noidle(dev);
380 //amba_put_disable_pclk(pcdev);
386 static int amba_remove(struct device *dev)
388 struct amba_device *pcdev = to_amba_device(dev);
389 struct amba_driver *drv = to_amba_driver(dev->driver);
392 pm_runtime_get_sync(dev);
393 ret = drv->remove(pcdev);
394 pm_runtime_put_noidle(dev);
396 /* Undo the runtime PM settings in amba_probe() */
397 pm_runtime_disable(dev);
398 pm_runtime_set_suspended(dev);
399 pm_runtime_put_noidle(dev);
401 amba_put_disable_pclk(pcdev);
406 static void amba_shutdown(struct device *dev)
408 struct amba_driver *drv = to_amba_driver(dev->driver);
409 drv->shutdown(to_amba_device(dev));
413 * amba_driver_register - register an AMBA device driver
414 * @drv: amba device driver structure
416 * Register an AMBA device driver with the Linux device model
417 * core. If devices pre-exist, the drivers probe function will
420 int amba_driver_register(struct amba_driver *drv)
422 drv->drv.bus = &amba_bustype;
425 #define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
430 return driver_register(&drv->drv);
434 * amba_driver_unregister - remove an AMBA device driver
435 * @drv: AMBA device driver structure to remove
437 * Unregister an AMBA device driver from the Linux device
438 * model. The device model will call the drivers remove function
439 * for each device the device driver is currently handling.
441 void amba_driver_unregister(struct amba_driver *drv)
443 driver_unregister(&drv->drv);
447 static void amba_device_release(struct device *dev)
449 struct amba_device *d = to_amba_device(dev);
452 release_resource(&d->res);
457 * amba_device_add - add a previously allocated AMBA device structure
458 * @dev: AMBA device allocated by amba_device_alloc
459 * @parent: resource parent for this devices resources
461 * Claim the resource, and read the device cell ID if not already
462 * initialized. Register the AMBA device with the Linux device
465 int amba_device_add(struct amba_device *dev, struct resource *parent)
471 WARN_ON(dev->irq[0] == (unsigned int)-1);
472 WARN_ON(dev->irq[1] == (unsigned int)-1);
474 ret = request_resource(parent, &dev->res);
478 /* Hard-coded primecell ID instead of plug-n-play */
479 if (dev->periphid != 0)
483 * Dynamically calculate the size of the resource
484 * and use this for iomap
486 size = resource_size(&dev->res);
487 tmp = ioremap(dev->res.start, size);
493 // ret = amba_get_enable_pclk(dev);
499 * Read pid and cid based on size of resource
500 * they are located at end of region
502 for (pid = 0, i = 0; i < 4; i++)
503 pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
505 for (cid = 0, i = 0; i < 4; i++)
506 cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
509 // amba_put_disable_pclk(dev);
524 ret = device_add(&dev->dev);
529 ret = device_create_file(&dev->dev, &dev_attr_irq0);
530 if (ret == 0 && dev->irq[1])
531 ret = device_create_file(&dev->dev, &dev_attr_irq1);
535 device_unregister(&dev->dev);
538 release_resource(&dev->res);
542 EXPORT_SYMBOL_GPL(amba_device_add);
544 static struct amba_device *
545 amba_aphb_device_add(struct device *parent, const char *name,
546 resource_size_t base, size_t size, int irq1, int irq2,
547 void *pdata, unsigned int periphid, u64 dma_mask,
548 struct resource *resbase)
550 struct amba_device *dev;
553 dev = amba_device_alloc(name, base, size);
555 return ERR_PTR(-ENOMEM);
557 dev->dma_mask = dma_mask;
558 dev->dev.coherent_dma_mask = dma_mask;
561 dev->periphid = periphid;
562 dev->dev.platform_data = pdata;
563 dev->dev.parent = parent;
565 ret = amba_device_add(dev, resbase);
567 amba_device_put(dev);
575 amba_apb_device_add(struct device *parent, const char *name,
576 resource_size_t base, size_t size, int irq1, int irq2,
577 void *pdata, unsigned int periphid)
579 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
580 periphid, 0, &iomem_resource);
582 EXPORT_SYMBOL_GPL(amba_apb_device_add);
585 amba_ahb_device_add(struct device *parent, const char *name,
586 resource_size_t base, size_t size, int irq1, int irq2,
587 void *pdata, unsigned int periphid)
589 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
590 periphid, ~0ULL, &iomem_resource);
592 EXPORT_SYMBOL_GPL(amba_ahb_device_add);
595 amba_apb_device_add_res(struct device *parent, const char *name,
596 resource_size_t base, size_t size, int irq1,
597 int irq2, void *pdata, unsigned int periphid,
598 struct resource *resbase)
600 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
601 periphid, 0, resbase);
603 EXPORT_SYMBOL_GPL(amba_apb_device_add_res);
606 amba_ahb_device_add_res(struct device *parent, const char *name,
607 resource_size_t base, size_t size, int irq1,
608 int irq2, void *pdata, unsigned int periphid,
609 struct resource *resbase)
611 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
612 periphid, ~0ULL, resbase);
614 EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);
617 static void amba_device_initialize(struct amba_device *dev, const char *name)
619 device_initialize(&dev->dev);
621 dev_set_name(&dev->dev, "%s", name);
622 dev->dev.release = amba_device_release;
623 dev->dev.bus = &amba_bustype;
624 dev->dev.dma_mask = &dev->dma_mask;
625 dev->res.name = dev_name(&dev->dev);
629 * amba_device_alloc - allocate an AMBA device
630 * @name: sysfs name of the AMBA device
631 * @base: base of AMBA device
632 * @size: size of AMBA device
634 * Allocate and initialize an AMBA device structure. Returns %NULL
637 struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
640 struct amba_device *dev;
642 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
644 amba_device_initialize(dev, name);
645 dev->res.start = base;
646 dev->res.end = base + size - 1;
647 dev->res.flags = IORESOURCE_MEM;
652 EXPORT_SYMBOL_GPL(amba_device_alloc);
655 * amba_device_register - register an AMBA device
656 * @dev: AMBA device to register
657 * @parent: parent memory resource
659 * Setup the AMBA device, reading the cell ID if present.
660 * Claim the resource, and register the AMBA device with
661 * the Linux device manager.
663 int amba_device_register(struct amba_device *dev, struct resource *parent)
665 amba_device_initialize(dev, dev->dev.init_name);
666 dev->dev.init_name = NULL;
668 if (!dev->dev.coherent_dma_mask && dev->dma_mask)
669 dev_warn(&dev->dev, "coherent dma mask is unset\n");
671 return amba_device_add(dev, parent);
675 * amba_device_put - put an AMBA device
676 * @dev: AMBA device to put
678 void amba_device_put(struct amba_device *dev)
680 put_device(&dev->dev);
682 EXPORT_SYMBOL_GPL(amba_device_put);
685 * amba_device_unregister - unregister an AMBA device
686 * @dev: AMBA device to remove
688 * Remove the specified AMBA device from the Linux device
689 * manager. All files associated with this object will be
690 * destroyed, and device drivers notified that the device has
691 * been removed. The AMBA device's resources including
692 * the amba_device structure will be freed once all
693 * references to it have been dropped.
695 void amba_device_unregister(struct amba_device *dev)
697 device_unregister(&dev->dev);
702 struct amba_device *dev;
703 struct device *parent;
709 static int amba_find_match(struct device *dev, void *data)
711 struct find_data *d = data;
712 struct amba_device *pcdev = to_amba_device(dev);
715 r = (pcdev->periphid & d->mask) == d->id;
717 r &= d->parent == dev->parent;
719 r &= strcmp(dev_name(dev), d->busid) == 0;
730 * amba_find_device - locate an AMBA device given a bus id
731 * @busid: bus id for device (or NULL)
732 * @parent: parent device (or NULL)
733 * @id: peripheral ID (or 0)
734 * @mask: peripheral ID mask (or 0)
736 * Return the AMBA device corresponding to the supplied parameters.
737 * If no device matches, returns NULL.
739 * NOTE: When a valid device is found, its refcount is
740 * incremented, and must be decremented before the returned
744 amba_find_device(const char *busid, struct device *parent, unsigned int id,
747 struct find_data data;
750 data.parent = parent;
755 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
761 * amba_request_regions - request all mem regions associated with device
762 * @dev: amba_device structure for device
763 * @name: name, or NULL to use driver name
765 int amba_request_regions(struct amba_device *dev, const char *name)
771 name = dev->dev.driver->name;
773 size = resource_size(&dev->res);
775 if (!request_mem_region(dev->res.start, size, name))
782 * amba_release_regions - release mem regions associated with device
783 * @dev: amba_device structure for device
785 * Release regions claimed by a successful call to amba_request_regions.
787 void amba_release_regions(struct amba_device *dev)
791 size = resource_size(&dev->res);
792 release_mem_region(dev->res.start, size);
795 EXPORT_SYMBOL(amba_driver_register);
796 EXPORT_SYMBOL(amba_driver_unregister);
797 EXPORT_SYMBOL(amba_device_register);
798 EXPORT_SYMBOL(amba_device_unregister);
799 EXPORT_SYMBOL(amba_find_device);
800 EXPORT_SYMBOL(amba_request_regions);
801 EXPORT_SYMBOL(amba_release_regions);