diff options
Diffstat (limited to 'drivers/watchdog/imx2_wdt.c')
| -rw-r--r-- | drivers/watchdog/imx2_wdt.c | 363 | 
1 files changed, 159 insertions, 204 deletions
diff --git a/drivers/watchdog/imx2_wdt.c b/drivers/watchdog/imx2_wdt.c index 2ee7dac55a3..9d4874f0994 100644 --- a/drivers/watchdog/imx2_wdt.c +++ b/drivers/watchdog/imx2_wdt.c @@ -2,6 +2,7 @@   * Watchdog driver for IMX2 and later processors   *   *  Copyright (C) 2010 Wolfram Sang, Pengutronix e.K. <w.sang@pengutronix.de> + *  Copyright (C) 2014 Freescale Semiconductor, Inc.   *   * some parts adapted by similar drivers from Darius Augulis and Vladimir   * Zapolskiy, additional improvements by Wim Van Sebroeck. @@ -20,20 +21,17 @@   * Halt on suspend:	Manual		Can be automatic   */ +#include <linux/clk.h>  #include <linux/init.h> +#include <linux/io.h> +#include <linux/jiffies.h>  #include <linux/kernel.h> -#include <linux/miscdevice.h>  #include <linux/module.h>  #include <linux/moduleparam.h>  #include <linux/platform_device.h> -#include <linux/watchdog.h> -#include <linux/clk.h> -#include <linux/fs.h> -#include <linux/io.h> -#include <linux/uaccess.h> +#include <linux/regmap.h>  #include <linux/timer.h> -#include <linux/jiffies.h> -#include <mach/hardware.h> +#include <linux/watchdog.h>  #define DRIVER_NAME "imx2-wdt" @@ -41,32 +39,29 @@  #define IMX2_WDT_WCR_WT		(0xFF << 8)	/* -> Watchdog Timeout Field */  #define IMX2_WDT_WCR_WRE	(1 << 3)	/* -> WDOG Reset Enable */  #define IMX2_WDT_WCR_WDE	(1 << 2)	/* -> Watchdog Enable */ +#define IMX2_WDT_WCR_WDZST	(1 << 0)	/* -> Watchdog timer Suspend */  #define IMX2_WDT_WSR		0x02		/* Service Register */  #define IMX2_WDT_SEQ1		0x5555		/* -> service sequence 1 */  #define IMX2_WDT_SEQ2		0xAAAA		/* -> service sequence 2 */ +#define IMX2_WDT_WRSR		0x04		/* Reset Status Register */ +#define IMX2_WDT_WRSR_TOUT	(1 << 1)	/* -> Reset due to Timeout */ +  #define IMX2_WDT_MAX_TIME	128  #define IMX2_WDT_DEFAULT_TIME	60		/* in seconds */  #define WDOG_SEC_TO_COUNT(s)	((s * 2 - 1) << 8) -#define IMX2_WDT_STATUS_OPEN	0 -#define IMX2_WDT_STATUS_STARTED	1 -#define IMX2_WDT_EXPECT_CLOSE	2 - -static struct { +struct imx2_wdt_device {  	struct clk *clk; -	void __iomem *base; -	unsigned timeout; -	unsigned long status; +	struct regmap *regmap;  	struct timer_list timer;	/* Pings the watchdog when closed */ -} imx2_wdt; - -static struct miscdevice imx2_wdt_miscdev; +	struct watchdog_device wdog; +}; -static int nowayout = WATCHDOG_NOWAYOUT; -module_param(nowayout, int, 0); +static bool nowayout = WATCHDOG_NOWAYOUT; +module_param(nowayout, bool, 0);  MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="  				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); @@ -81,10 +76,15 @@ static const struct watchdog_info imx2_wdt_info = {  	.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,  }; -static inline void imx2_wdt_setup(void) +static inline void imx2_wdt_setup(struct watchdog_device *wdog)  { -	u16 val = __raw_readw(imx2_wdt.base + IMX2_WDT_WCR); +	struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog); +	u32 val; + +	regmap_read(wdev->regmap, IMX2_WDT_WCR, &val); +	/* Suspend timer in low power mode, write once-only */ +	val |= IMX2_WDT_WCR_WDZST;  	/* Strip the old watchdog Time-Out value */  	val &= ~IMX2_WDT_WCR_WT;  	/* Generate reset if WDOG times out */ @@ -92,266 +92,221 @@ static inline void imx2_wdt_setup(void)  	/* Keep Watchdog Disabled */  	val &= ~IMX2_WDT_WCR_WDE;  	/* Set the watchdog's Time-Out value */ -	val |= WDOG_SEC_TO_COUNT(imx2_wdt.timeout); +	val |= WDOG_SEC_TO_COUNT(wdog->timeout); -	__raw_writew(val, imx2_wdt.base + IMX2_WDT_WCR); +	regmap_write(wdev->regmap, IMX2_WDT_WCR, val);  	/* enable the watchdog */  	val |= IMX2_WDT_WCR_WDE; -	__raw_writew(val, imx2_wdt.base + IMX2_WDT_WCR); +	regmap_write(wdev->regmap, IMX2_WDT_WCR, val);  } -static inline void imx2_wdt_ping(void) +static inline bool imx2_wdt_is_running(struct imx2_wdt_device *wdev)  { -	__raw_writew(IMX2_WDT_SEQ1, imx2_wdt.base + IMX2_WDT_WSR); -	__raw_writew(IMX2_WDT_SEQ2, imx2_wdt.base + IMX2_WDT_WSR); -} +	u32 val; -static void imx2_wdt_timer_ping(unsigned long arg) -{ -	/* ping it every imx2_wdt.timeout / 2 seconds to prevent reboot */ -	imx2_wdt_ping(); -	mod_timer(&imx2_wdt.timer, jiffies + imx2_wdt.timeout * HZ / 2); +	regmap_read(wdev->regmap, IMX2_WDT_WCR, &val); + +	return val & IMX2_WDT_WCR_WDE;  } -static void imx2_wdt_start(void) +static int imx2_wdt_ping(struct watchdog_device *wdog)  { -	if (!test_and_set_bit(IMX2_WDT_STATUS_STARTED, &imx2_wdt.status)) { -		/* at our first start we enable clock and do initialisations */ -		clk_enable(imx2_wdt.clk); - -		imx2_wdt_setup(); -	} else	/* delete the timer that pings the watchdog after close */ -		del_timer_sync(&imx2_wdt.timer); +	struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog); -	/* Watchdog is enabled - time to reload the timeout value */ -	imx2_wdt_ping(); +	regmap_write(wdev->regmap, IMX2_WDT_WSR, IMX2_WDT_SEQ1); +	regmap_write(wdev->regmap, IMX2_WDT_WSR, IMX2_WDT_SEQ2); +	return 0;  } -static void imx2_wdt_stop(void) +static void imx2_wdt_timer_ping(unsigned long arg)  { -	/* we don't need a clk_disable, it cannot be disabled once started. -	 * We use a timer to ping the watchdog while /dev/watchdog is closed */ -	imx2_wdt_timer_ping(0); +	struct watchdog_device *wdog = (struct watchdog_device *)arg; +	struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog); + +	/* ping it every wdog->timeout / 2 seconds to prevent reboot */ +	imx2_wdt_ping(wdog); +	mod_timer(&wdev->timer, jiffies + wdog->timeout * HZ / 2);  } -static void imx2_wdt_set_timeout(int new_timeout) +static int imx2_wdt_set_timeout(struct watchdog_device *wdog, +				unsigned int new_timeout)  { -	u16 val = __raw_readw(imx2_wdt.base + IMX2_WDT_WCR); +	struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog); -	/* set the new timeout value in the WSR */ -	val &= ~IMX2_WDT_WCR_WT; -	val |= WDOG_SEC_TO_COUNT(new_timeout); -	__raw_writew(val, imx2_wdt.base + IMX2_WDT_WCR); +	regmap_update_bits(wdev->regmap, IMX2_WDT_WCR, IMX2_WDT_WCR_WT, +			   WDOG_SEC_TO_COUNT(new_timeout)); +	return 0;  } -static int imx2_wdt_open(struct inode *inode, struct file *file) +static int imx2_wdt_start(struct watchdog_device *wdog)  { -	if (test_and_set_bit(IMX2_WDT_STATUS_OPEN, &imx2_wdt.status)) -		return -EBUSY; +	struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog); + +	if (imx2_wdt_is_running(wdev)) { +		/* delete the timer that pings the watchdog after close */ +		del_timer_sync(&wdev->timer); +		imx2_wdt_set_timeout(wdog, wdog->timeout); +	} else +		imx2_wdt_setup(wdog); -	imx2_wdt_start(); -	return nonseekable_open(inode, file); +	return imx2_wdt_ping(wdog);  } -static int imx2_wdt_close(struct inode *inode, struct file *file) +static int imx2_wdt_stop(struct watchdog_device *wdog)  { -	if (test_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status) && !nowayout) -		imx2_wdt_stop(); -	else { -		dev_crit(imx2_wdt_miscdev.parent, -			"Unexpected close: Expect reboot!\n"); -		imx2_wdt_ping(); -	} - -	clear_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status); -	clear_bit(IMX2_WDT_STATUS_OPEN, &imx2_wdt.status); +	/* +	 * We don't need a clk_disable, it cannot be disabled once started. +	 * We use a timer to ping the watchdog while /dev/watchdog is closed +	 */ +	imx2_wdt_timer_ping((unsigned long)wdog);  	return 0;  } -static long imx2_wdt_ioctl(struct file *file, unsigned int cmd, -							unsigned long arg) +static inline void imx2_wdt_ping_if_active(struct watchdog_device *wdog)  { -	void __user *argp = (void __user *)arg; -	int __user *p = argp; -	int new_value; - -	switch (cmd) { -	case WDIOC_GETSUPPORT: -		return copy_to_user(argp, &imx2_wdt_info, -			sizeof(struct watchdog_info)) ? -EFAULT : 0; - -	case WDIOC_GETSTATUS: -	case WDIOC_GETBOOTSTATUS: -		return put_user(0, p); - -	case WDIOC_KEEPALIVE: -		imx2_wdt_ping(); -		return 0; - -	case WDIOC_SETTIMEOUT: -		if (get_user(new_value, p)) -			return -EFAULT; -		if ((new_value < 1) || (new_value > IMX2_WDT_MAX_TIME)) -			return -EINVAL; -		imx2_wdt_set_timeout(new_value); -		imx2_wdt.timeout = new_value; -		imx2_wdt_ping(); - -		/* Fallthrough to return current value */ -	case WDIOC_GETTIMEOUT: -		return put_user(imx2_wdt.timeout, p); - -	default: -		return -ENOTTY; -	} -} +	struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog); -static ssize_t imx2_wdt_write(struct file *file, const char __user *data, -						size_t len, loff_t *ppos) -{ -	size_t i; -	char c; - -	if (len == 0)	/* Can we see this even ? */ -		return 0; - -	clear_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status); -	/* scan to see whether or not we got the magic character */ -	for (i = 0; i != len; i++) { -		if (get_user(c, data + i)) -			return -EFAULT; -		if (c == 'V') -			set_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status); +	if (imx2_wdt_is_running(wdev)) { +		imx2_wdt_set_timeout(wdog, wdog->timeout); +		imx2_wdt_timer_ping((unsigned long)wdog);  	} - -	imx2_wdt_ping(); -	return len;  } -static const struct file_operations imx2_wdt_fops = { +static struct watchdog_ops imx2_wdt_ops = {  	.owner = THIS_MODULE, -	.llseek = no_llseek, -	.unlocked_ioctl = imx2_wdt_ioctl, -	.open = imx2_wdt_open, -	.release = imx2_wdt_close, -	.write = imx2_wdt_write, +	.start = imx2_wdt_start, +	.stop = imx2_wdt_stop, +	.ping = imx2_wdt_ping, +	.set_timeout = imx2_wdt_set_timeout,  }; -static struct miscdevice imx2_wdt_miscdev = { -	.minor = WATCHDOG_MINOR, -	.name = "watchdog", -	.fops = &imx2_wdt_fops, +static struct regmap_config imx2_wdt_regmap_config = { +	.reg_bits = 16, +	.reg_stride = 2, +	.val_bits = 16, +	.max_register = 0x8,  };  static int __init imx2_wdt_probe(struct platform_device *pdev)  { -	int ret; -	int res_size; +	struct imx2_wdt_device *wdev; +	struct watchdog_device *wdog;  	struct resource *res; +	void __iomem *base; +	int ret; +	u32 val; -	res = platform_get_resource(pdev, IORESOURCE_MEM, 0); -	if (!res) { -		dev_err(&pdev->dev, "can't get device resources\n"); -		return -ENODEV; -	} - -	res_size = resource_size(res); -	if (!devm_request_mem_region(&pdev->dev, res->start, res_size, -		res->name)) { -		dev_err(&pdev->dev, "can't allocate %d bytes at %d address\n", -			res_size, res->start); +	wdev = devm_kzalloc(&pdev->dev, sizeof(*wdev), GFP_KERNEL); +	if (!wdev)  		return -ENOMEM; -	} -	imx2_wdt.base = devm_ioremap_nocache(&pdev->dev, res->start, res_size); -	if (!imx2_wdt.base) { -		dev_err(&pdev->dev, "ioremap failed\n"); -		return -ENOMEM; +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0); +	base = devm_ioremap_resource(&pdev->dev, res); +	if (IS_ERR(base)) +		return PTR_ERR(base); + +	wdev->regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base, +						 &imx2_wdt_regmap_config); +	if (IS_ERR(wdev->regmap)) { +		dev_err(&pdev->dev, "regmap init failed\n"); +		return PTR_ERR(wdev->regmap);  	} -	imx2_wdt.clk = clk_get_sys("imx-wdt.0", NULL); -	if (IS_ERR(imx2_wdt.clk)) { +	wdev->clk = devm_clk_get(&pdev->dev, NULL); +	if (IS_ERR(wdev->clk)) {  		dev_err(&pdev->dev, "can't get Watchdog clock\n"); -		return PTR_ERR(imx2_wdt.clk); +		return PTR_ERR(wdev->clk);  	} -	imx2_wdt.timeout = clamp_t(unsigned, timeout, 1, IMX2_WDT_MAX_TIME); -	if (imx2_wdt.timeout != timeout) -		dev_warn(&pdev->dev, "Initial timeout out of range! " -			"Clamped from %u to %u\n", timeout, imx2_wdt.timeout); +	wdog			= &wdev->wdog; +	wdog->info		= &imx2_wdt_info; +	wdog->ops		= &imx2_wdt_ops; +	wdog->min_timeout	= 1; +	wdog->max_timeout	= IMX2_WDT_MAX_TIME; -	setup_timer(&imx2_wdt.timer, imx2_wdt_timer_ping, 0); +	clk_prepare_enable(wdev->clk); -	imx2_wdt_miscdev.parent = &pdev->dev; -	ret = misc_register(&imx2_wdt_miscdev); -	if (ret) -		goto fail; +	regmap_read(wdev->regmap, IMX2_WDT_WRSR, &val); +	wdog->bootstatus = val & IMX2_WDT_WRSR_TOUT ? WDIOF_CARDRESET : 0; -	dev_info(&pdev->dev, -		"IMX2+ Watchdog Timer enabled. timeout=%ds (nowayout=%d)\n", -						imx2_wdt.timeout, nowayout); -	return 0; +	wdog->timeout = clamp_t(unsigned, timeout, 1, IMX2_WDT_MAX_TIME); +	if (wdog->timeout != timeout) +		dev_warn(&pdev->dev, "Initial timeout out of range! Clamped from %u to %u\n", +			 timeout, wdog->timeout); + +	platform_set_drvdata(pdev, wdog); +	watchdog_set_drvdata(wdog, wdev); +	watchdog_set_nowayout(wdog, nowayout); +	watchdog_init_timeout(wdog, timeout, &pdev->dev); + +	setup_timer(&wdev->timer, imx2_wdt_timer_ping, (unsigned long)wdog); + +	imx2_wdt_ping_if_active(wdog); -fail: -	imx2_wdt_miscdev.parent = NULL; -	clk_put(imx2_wdt.clk); -	return ret; +	ret = watchdog_register_device(wdog); +	if (ret) { +		dev_err(&pdev->dev, "cannot register watchdog device\n"); +		return ret; +	} + +	dev_info(&pdev->dev, "timeout %d sec (nowayout=%d)\n", +		 wdog->timeout, nowayout); + +	return 0;  }  static int __exit imx2_wdt_remove(struct platform_device *pdev)  { -	misc_deregister(&imx2_wdt_miscdev); +	struct watchdog_device *wdog = platform_get_drvdata(pdev); +	struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog); -	if (test_bit(IMX2_WDT_STATUS_STARTED, &imx2_wdt.status)) { -		del_timer_sync(&imx2_wdt.timer); +	watchdog_unregister_device(wdog); -		dev_crit(imx2_wdt_miscdev.parent, -			"Device removed: Expect reboot!\n"); -	} else -		clk_put(imx2_wdt.clk); - -	imx2_wdt_miscdev.parent = NULL; +	if (imx2_wdt_is_running(wdev)) { +		del_timer_sync(&wdev->timer); +		imx2_wdt_ping(wdog); +		dev_crit(&pdev->dev, "Device removed: Expect reboot!\n"); +	}  	return 0;  }  static void imx2_wdt_shutdown(struct platform_device *pdev)  { -	if (test_bit(IMX2_WDT_STATUS_STARTED, &imx2_wdt.status)) { -		/* we are running, we need to delete the timer but will give -		 * max timeout before reboot will take place */ -		del_timer_sync(&imx2_wdt.timer); -		imx2_wdt_set_timeout(IMX2_WDT_MAX_TIME); -		imx2_wdt_ping(); - -		dev_crit(imx2_wdt_miscdev.parent, -			"Device shutdown: Expect reboot!\n"); +	struct watchdog_device *wdog = platform_get_drvdata(pdev); +	struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog); + +	if (imx2_wdt_is_running(wdev)) { +		/* +		 * We are running, we need to delete the timer but will +		 * give max timeout before reboot will take place +		 */ +		del_timer_sync(&wdev->timer); +		imx2_wdt_set_timeout(wdog, IMX2_WDT_MAX_TIME); +		imx2_wdt_ping(wdog); +		dev_crit(&pdev->dev, "Device shutdown: Expect reboot!\n");  	}  } +static const struct of_device_id imx2_wdt_dt_ids[] = { +	{ .compatible = "fsl,imx21-wdt", }, +	{ /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, imx2_wdt_dt_ids); +  static struct platform_driver imx2_wdt_driver = {  	.remove		= __exit_p(imx2_wdt_remove),  	.shutdown	= imx2_wdt_shutdown,  	.driver		= {  		.name	= DRIVER_NAME,  		.owner	= THIS_MODULE, +		.of_match_table = imx2_wdt_dt_ids,  	},  }; -static int __init imx2_wdt_init(void) -{ -	return platform_driver_probe(&imx2_wdt_driver, imx2_wdt_probe); -} -module_init(imx2_wdt_init); - -static void __exit imx2_wdt_exit(void) -{ -	platform_driver_unregister(&imx2_wdt_driver); -} -module_exit(imx2_wdt_exit); +module_platform_driver_probe(imx2_wdt_driver, imx2_wdt_probe);  MODULE_AUTHOR("Wolfram Sang");  MODULE_DESCRIPTION("Watchdog driver for IMX2 and later");  MODULE_LICENSE("GPL v2"); -MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);  MODULE_ALIAS("platform:" DRIVER_NAME);  | 
