blob: 88f23c5cfddba5cf646d6adb4de5c4bccd1b9bb4 [file] [log] [blame]
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +09001/*
2 * txx9wdt: A Hardware Watchdog Driver for TXx9 SoCs
3 *
4 * Copyright (C) 2007 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
5 *
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.
9 */
Joe Perches27c766a2012-02-15 15:06:19 -080010
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090013#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/types.h>
16#include <linux/miscdevice.h>
17#include <linux/watchdog.h>
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090018#include <linux/init.h>
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090019#include <linux/platform_device.h>
20#include <linux/clk.h>
21#include <linux/err.h>
22#include <linux/io.h>
23#include <asm/txx9tmr.h>
24
Wim Van Sebroeckb92c8032012-03-23 11:48:22 +010025#define WD_TIMER_CCD 7 /* 1/256 */
26#define WD_TIMER_CLK (clk_get_rate(txx9_imclk) / (2 << WD_TIMER_CCD))
27#define WD_MAX_TIMEOUT ((0xffffffff >> (32 - TXX9_TIMER_BITS)) / WD_TIMER_CLK)
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090028#define TIMER_MARGIN 60 /* Default is 60 seconds */
29
Wim Van Sebroeckb92c8032012-03-23 11:48:22 +010030static unsigned int timeout = TIMER_MARGIN; /* in seconds */
31module_param(timeout, uint, 0);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090032MODULE_PARM_DESC(timeout,
33 "Watchdog timeout in seconds. "
34 "(0<timeout<((2^" __MODULE_STRING(TXX9_TIMER_BITS) ")/(IMCLK/256)), "
35 "default=" __MODULE_STRING(TIMER_MARGIN) ")");
36
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010037static bool nowayout = WATCHDOG_NOWAYOUT;
38module_param(nowayout, bool, 0);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090039MODULE_PARM_DESC(nowayout,
40 "Watchdog cannot be stopped once started "
41 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
42
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090043static struct txx9_tmr_reg __iomem *txx9wdt_reg;
44static struct clk *txx9_imclk;
Adrian Bunkf8494e02008-08-08 18:18:46 +030045static DEFINE_SPINLOCK(txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090046
Axel Lind6245842012-03-16 11:53:53 +080047static int txx9wdt_ping(struct watchdog_device *wdt_dev)
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090048{
Alan Cox8dc244f2008-05-19 14:09:12 +010049 spin_lock(&txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090050 __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
Alan Cox8dc244f2008-05-19 14:09:12 +010051 spin_unlock(&txx9_lock);
Axel Lind6245842012-03-16 11:53:53 +080052 return 0;
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090053}
54
Axel Lind6245842012-03-16 11:53:53 +080055static int txx9wdt_start(struct watchdog_device *wdt_dev)
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090056{
Alan Cox8dc244f2008-05-19 14:09:12 +010057 spin_lock(&txx9_lock);
Wim Van Sebroeckb92c8032012-03-23 11:48:22 +010058 __raw_writel(WD_TIMER_CLK * wdt_dev->timeout, &txx9wdt_reg->cpra);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090059 __raw_writel(WD_TIMER_CCD, &txx9wdt_reg->ccdr);
60 __raw_writel(0, &txx9wdt_reg->tisr); /* clear pending interrupt */
61 __raw_writel(TXx9_TMTCR_TCE | TXx9_TMTCR_CCDE | TXx9_TMTCR_TMODE_WDOG,
62 &txx9wdt_reg->tcr);
63 __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
Alan Cox8dc244f2008-05-19 14:09:12 +010064 spin_unlock(&txx9_lock);
Axel Lind6245842012-03-16 11:53:53 +080065 return 0;
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090066}
67
Axel Lind6245842012-03-16 11:53:53 +080068static int txx9wdt_stop(struct watchdog_device *wdt_dev)
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090069{
Alan Cox8dc244f2008-05-19 14:09:12 +010070 spin_lock(&txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090071 __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr);
72 __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE,
73 &txx9wdt_reg->tcr);
Alan Cox8dc244f2008-05-19 14:09:12 +010074 spin_unlock(&txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090075 return 0;
76}
77
Axel Lind6245842012-03-16 11:53:53 +080078static int txx9wdt_set_timeout(struct watchdog_device *wdt_dev,
79 unsigned int new_timeout)
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090080{
Wim Van Sebroeckb92c8032012-03-23 11:48:22 +010081 wdt_dev->timeout = new_timeout;
Axel Lind6245842012-03-16 11:53:53 +080082 txx9wdt_stop(wdt_dev);
83 txx9wdt_start(wdt_dev);
84 return 0;
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090085}
86
Axel Lind6245842012-03-16 11:53:53 +080087static const struct watchdog_info txx9wdt_info = {
88 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
89 .identity = "Hardware Watchdog for TXx9",
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090090};
91
Axel Lind6245842012-03-16 11:53:53 +080092static const struct watchdog_ops txx9wdt_ops = {
93 .owner = THIS_MODULE,
94 .start = txx9wdt_start,
95 .stop = txx9wdt_stop,
96 .ping = txx9wdt_ping,
97 .set_timeout = txx9wdt_set_timeout,
98};
99
100static struct watchdog_device txx9wdt = {
101 .info = &txx9wdt_info,
102 .ops = &txx9wdt_ops,
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900103};
104
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900105static int __init txx9wdt_probe(struct platform_device *dev)
106{
107 struct resource *res;
108 int ret;
109
110 txx9_imclk = clk_get(NULL, "imbus_clk");
111 if (IS_ERR(txx9_imclk)) {
112 ret = PTR_ERR(txx9_imclk);
113 txx9_imclk = NULL;
114 goto exit;
115 }
116 ret = clk_enable(txx9_imclk);
117 if (ret) {
118 clk_put(txx9_imclk);
119 txx9_imclk = NULL;
120 goto exit;
121 }
122
123 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
Thierry Reding4c271bb2013-01-21 11:09:25 +0100124 txx9wdt_reg = devm_ioremap_resource(&dev->dev, res);
125 if (IS_ERR(txx9wdt_reg)) {
126 ret = PTR_ERR(txx9wdt_reg);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900127 goto exit;
128 }
129
Wim Van Sebroeckb92c8032012-03-23 11:48:22 +0100130 if (timeout < 1 || timeout > WD_MAX_TIMEOUT)
131 timeout = TIMER_MARGIN;
132 txx9wdt.timeout = timeout;
Axel Lind6245842012-03-16 11:53:53 +0800133 txx9wdt.min_timeout = 1;
134 txx9wdt.max_timeout = WD_MAX_TIMEOUT;
135 watchdog_set_nowayout(&txx9wdt, nowayout);
136
137 ret = watchdog_register_device(&txx9wdt);
138 if (ret)
139 goto exit;
140
Joe Perches27c766a2012-02-15 15:06:19 -0800141 pr_info("Hardware Watchdog Timer: timeout=%d sec (max %ld) (nowayout= %d)\n",
142 timeout, WD_MAX_TIMEOUT, nowayout);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900143
144 return 0;
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900145exit:
146 if (txx9_imclk) {
147 clk_disable(txx9_imclk);
148 clk_put(txx9_imclk);
149 }
150 return ret;
151}
152
153static int __exit txx9wdt_remove(struct platform_device *dev)
154{
Axel Lind6245842012-03-16 11:53:53 +0800155 watchdog_unregister_device(&txx9wdt);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900156 clk_disable(txx9_imclk);
157 clk_put(txx9_imclk);
158 return 0;
159}
160
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000161static void txx9wdt_shutdown(struct platform_device *dev)
162{
Axel Lind6245842012-03-16 11:53:53 +0800163 txx9wdt_stop(&txx9wdt);
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000164}
165
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900166static struct platform_driver txx9wdt_driver = {
167 .remove = __exit_p(txx9wdt_remove),
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000168 .shutdown = txx9wdt_shutdown,
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900169 .driver = {
170 .name = "txx9wdt",
171 .owner = THIS_MODULE,
172 },
173};
174
Fabio Porcedda1cb92042013-01-09 12:15:27 +0100175module_platform_driver_probe(txx9wdt_driver, txx9wdt_probe);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900176
177MODULE_DESCRIPTION("TXx9 Watchdog Driver");
178MODULE_LICENSE("GPL");
179MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
Kay Sieversf37d1932008-04-10 21:29:23 -0700180MODULE_ALIAS("platform:txx9wdt");