779 lines · 16.5 KB · C
RawDownload
/* *  linux/arch/arm/common/amba.c * *  Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */#include <linux/module.h>#include <linux/init.h>#include <linux/device.h>#include <linux/string.h>#include <linux/slab.h>#include <linux/io.h>#include <linux/pm.h>#include <linux/pm_runtime.h>#include <linux/amba/bus.h> #include <asm/irq.h>#include <asm/sizes.h> #define to_amba_driver(d)	container_of(d, struct amba_driver, drv) static const struct amba_id *amba_lookup(const struct amba_id *table, struct amba_device *dev){	int ret = 0; 	while (table->mask) {		ret = (dev->periphid & table->mask) == table->id;		if (ret)			break;		table++;	} 	return ret ? table : NULL;} static int amba_match(struct device *dev, struct device_driver *drv){	struct amba_device *pcdev = to_amba_device(dev);	struct amba_driver *pcdrv = to_amba_driver(drv); 	return amba_lookup(pcdrv->id_table, pcdev) != NULL;} #ifdef CONFIG_HOTPLUGstatic int amba_uevent(struct device *dev, struct kobj_uevent_env *env){	struct amba_device *pcdev = to_amba_device(dev);	int retval = 0; 	retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);	if (retval)		return retval; 	retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);	return retval;}#else#define amba_uevent NULL#endif #define amba_attr_func(name,fmt,arg...)					\static ssize_t name##_show(struct device *_dev,				\			   struct device_attribute *attr, char *buf)	\{									\	struct amba_device *dev = to_amba_device(_dev);			\	return sprintf(buf, fmt, arg);					\} #define amba_attr(name,fmt,arg...)	\amba_attr_func(name,fmt,arg)		\static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL) amba_attr_func(id, "%08x\n", dev->periphid);amba_attr(irq0, "%u\n", dev->irq[0]);amba_attr(irq1, "%u\n", dev->irq[1]);amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",	 (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,	 dev->res.flags); static struct device_attribute amba_dev_attrs[] = {	__ATTR_RO(id),	__ATTR_RO(resource),	__ATTR_NULL,}; #ifdef CONFIG_PM_SLEEP static int amba_legacy_suspend(struct device *dev, pm_message_t mesg){	struct amba_driver *adrv = to_amba_driver(dev->driver);	struct amba_device *adev = to_amba_device(dev);	int ret = 0; 	if (dev->driver && adrv->suspend)		ret = adrv->suspend(adev, mesg); 	return ret;} static int amba_legacy_resume(struct device *dev){	struct amba_driver *adrv = to_amba_driver(dev->driver);	struct amba_device *adev = to_amba_device(dev);	int ret = 0; 	if (dev->driver && adrv->resume)		ret = adrv->resume(adev); 	return ret;} #endif /* CONFIG_PM_SLEEP */ #ifdef CONFIG_SUSPEND static int amba_pm_suspend(struct device *dev){	struct device_driver *drv = dev->driver;	int ret = 0; 	if (!drv)		return 0; 	if (drv->pm) {		if (drv->pm->suspend)			ret = drv->pm->suspend(dev);	} else {		ret = amba_legacy_suspend(dev, PMSG_SUSPEND);	} 	return ret;} static int amba_pm_resume(struct device *dev){	struct device_driver *drv = dev->driver;	int ret = 0; 	if (!drv)		return 0; 	if (drv->pm) {		if (drv->pm->resume)			ret = drv->pm->resume(dev);	} else {		ret = amba_legacy_resume(dev);	} 	return ret;} #else /* !CONFIG_SUSPEND */ #define amba_pm_suspend		NULL#define amba_pm_resume		NULL #endif /* !CONFIG_SUSPEND */ #ifdef CONFIG_HIBERNATE_CALLBACKS static int amba_pm_freeze(struct device *dev){	struct device_driver *drv = dev->driver;	int ret = 0; 	if (!drv)		return 0; 	if (drv->pm) {		if (drv->pm->freeze)			ret = drv->pm->freeze(dev);	} else {		ret = amba_legacy_suspend(dev, PMSG_FREEZE);	} 	return ret;} static int amba_pm_thaw(struct device *dev){	struct device_driver *drv = dev->driver;	int ret = 0; 	if (!drv)		return 0; 	if (drv->pm) {		if (drv->pm->thaw)			ret = drv->pm->thaw(dev);	} else {		ret = amba_legacy_resume(dev);	} 	return ret;} static int amba_pm_poweroff(struct device *dev){	struct device_driver *drv = dev->driver;	int ret = 0; 	if (!drv)		return 0; 	if (drv->pm) {		if (drv->pm->poweroff)			ret = drv->pm->poweroff(dev);	} else {		ret = amba_legacy_suspend(dev, PMSG_HIBERNATE);	} 	return ret;} static int amba_pm_restore(struct device *dev){	struct device_driver *drv = dev->driver;	int ret = 0; 	if (!drv)		return 0; 	if (drv->pm) {		if (drv->pm->restore)			ret = drv->pm->restore(dev);	} else {		ret = amba_legacy_resume(dev);	} 	return ret;} #else /* !CONFIG_HIBERNATE_CALLBACKS */ #define amba_pm_freeze		NULL#define amba_pm_thaw		NULL#define amba_pm_poweroff		NULL#define amba_pm_restore		NULL #endif /* !CONFIG_HIBERNATE_CALLBACKS */ #ifdef CONFIG_PM_RUNTIME/* * Hooks to provide runtime PM of the pclk (bus clock).  It is safe to * enable/disable the bus clock at runtime PM suspend/resume as this * does not result in loss of context. */static int amba_pm_runtime_suspend(struct device *dev){	struct amba_device *pcdev = to_amba_device(dev);	int ret = pm_generic_runtime_suspend(dev); 	if (ret == 0 && dev->driver)		clk_disable(pcdev->pclk); 	return ret;} static int amba_pm_runtime_resume(struct device *dev){	struct amba_device *pcdev = to_amba_device(dev);	int ret; 	if (dev->driver) {		ret = clk_enable(pcdev->pclk);		/* Failure is probably fatal to the system, but... */		if (ret)			return ret;	} 	return pm_generic_runtime_resume(dev);}#endif #ifdef CONFIG_PM static const struct dev_pm_ops amba_pm = {	.suspend	= amba_pm_suspend,	.resume		= amba_pm_resume,	.freeze		= amba_pm_freeze,	.thaw		= amba_pm_thaw,	.poweroff	= amba_pm_poweroff,	.restore	= amba_pm_restore,	SET_RUNTIME_PM_OPS(		amba_pm_runtime_suspend,		amba_pm_runtime_resume,		pm_generic_runtime_idle	)}; #define AMBA_PM (&amba_pm) #else /* !CONFIG_PM */ #define AMBA_PM	NULL #endif /* !CONFIG_PM */ /* * Primecells are part of the Advanced Microcontroller Bus Architecture, * so we call the bus "amba". */struct bus_type amba_bustype = {	.name		= "amba",	.dev_attrs	= amba_dev_attrs,	.match		= amba_match,	.uevent		= amba_uevent,	.pm		= AMBA_PM,}; static int __init amba_init(void){	return bus_register(&amba_bustype);} postcore_initcall(amba_init); static int amba_get_enable_pclk(struct amba_device *pcdev){	struct clk *pclk = clk_get(&pcdev->dev, "apb_pclk");	int ret; 	pcdev->pclk = pclk; 	if (IS_ERR(pclk))		return PTR_ERR(pclk); 	ret = clk_prepare(pclk);	if (ret) {		clk_put(pclk);		return ret;	} 	ret = clk_enable(pclk);	if (ret) {		clk_unprepare(pclk);		clk_put(pclk);	} 	return ret;} static void amba_put_disable_pclk(struct amba_device *pcdev){	struct clk *pclk = pcdev->pclk; 	clk_disable(pclk);	clk_unprepare(pclk);	clk_put(pclk);} /* * These are the device model conversion veneers; they convert the * device model structures to our more specific structures. */static int amba_probe(struct device *dev){	struct amba_device *pcdev = to_amba_device(dev);	struct amba_driver *pcdrv = to_amba_driver(dev->driver);	const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);	int ret; 	do {		ret = amba_get_enable_pclk(pcdev);		if (ret)			break; 		pm_runtime_get_noresume(dev);		pm_runtime_set_active(dev);		pm_runtime_enable(dev); 		ret = pcdrv->probe(pcdev, id);		if (ret == 0)			break; 		pm_runtime_disable(dev);		pm_runtime_set_suspended(dev);		pm_runtime_put_noidle(dev); 		amba_put_disable_pclk(pcdev);	} while (0); 	return ret;} static int amba_remove(struct device *dev){	struct amba_device *pcdev = to_amba_device(dev);	struct amba_driver *drv = to_amba_driver(dev->driver);	int ret; 	pm_runtime_get_sync(dev);	ret = drv->remove(pcdev);	pm_runtime_put_noidle(dev); 	/* Undo the runtime PM settings in amba_probe() */	pm_runtime_disable(dev);	pm_runtime_set_suspended(dev);	pm_runtime_put_noidle(dev); 	amba_put_disable_pclk(pcdev); 	return ret;} static void amba_shutdown(struct device *dev){	struct amba_driver *drv = to_amba_driver(dev->driver);	drv->shutdown(to_amba_device(dev));} /** *	amba_driver_register - register an AMBA device driver *	@drv: amba device driver structure * *	Register an AMBA device driver with the Linux device model *	core.  If devices pre-exist, the drivers probe function will *	be called. */int amba_driver_register(struct amba_driver *drv){	drv->drv.bus = &amba_bustype; #define SETFN(fn)	if (drv->fn) drv->drv.fn = amba_##fn	SETFN(probe);	SETFN(remove);	SETFN(shutdown); 	return driver_register(&drv->drv);} /** *	amba_driver_unregister - remove an AMBA device driver *	@drv: AMBA device driver structure to remove * *	Unregister an AMBA device driver from the Linux device *	model.  The device model will call the drivers remove function *	for each device the device driver is currently handling. */void amba_driver_unregister(struct amba_driver *drv){	driver_unregister(&drv->drv);}  static void amba_device_release(struct device *dev){	struct amba_device *d = to_amba_device(dev); 	if (d->res.parent)		release_resource(&d->res);	kfree(d);} /** *	amba_device_add - add a previously allocated AMBA device structure *	@dev: AMBA device allocated by amba_device_alloc *	@parent: resource parent for this devices resources * *	Claim the resource, and read the device cell ID if not already *	initialized.  Register the AMBA device with the Linux device *	manager. */int amba_device_add(struct amba_device *dev, struct resource *parent){	u32 size;	void __iomem *tmp;	int i, ret; 	WARN_ON(dev->irq[0] == (unsigned int)-1);	WARN_ON(dev->irq[1] == (unsigned int)-1); 	ret = request_resource(parent, &dev->res);	if (ret)		goto err_out; 	/* Hard-coded primecell ID instead of plug-n-play */	if (dev->periphid != 0)		goto skip_probe; 	/*	 * Dynamically calculate the size of the resource	 * and use this for iomap	 */	size = resource_size(&dev->res);	tmp = ioremap(dev->res.start, size);	if (!tmp) {		ret = -ENOMEM;		goto err_release;	} 	ret = amba_get_enable_pclk(dev);	if (ret == 0) {		u32 pid, cid; 		/*

Lines 1–500 of 779.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t