blob: c20f96b579d92015f394cc883ad632fe1443f69b [file] [log] [blame]
Sylver Bruneau22ac9232008-06-26 10:47:45 +02001/*
Nicolas Pitre3b937a72009-06-01 13:56:02 -04002 * drivers/watchdog/orion_wdt.c
Sylver Bruneau22ac9232008-06-26 10:47:45 +02003 *
Nicolas Pitre3b937a72009-06-01 13:56:02 -04004 * Watchdog driver for Orion/Kirkwood processors
Sylver Bruneau22ac9232008-06-26 10:47:45 +02005 *
6 * Author: Sylver Bruneau <sylver.bruneau@googlemail.com>
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
Joe Perches27c766a2012-02-15 15:06:19 -080013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Sylver Bruneau22ac9232008-06-26 10:47:45 +020015#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/types.h>
18#include <linux/kernel.h>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020019#include <linux/miscdevice.h>
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080020#include <linux/platform_device.h>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020021#include <linux/watchdog.h>
22#include <linux/init.h>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020023#include <linux/io.h>
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000024#include <linux/spinlock.h>
Andrew Lunn4f04be62012-03-04 16:57:31 +010025#include <linux/clk.h>
Axel Lin0dd6e482012-03-26 11:14:29 +080026#include <linux/err.h>
Andrew Lunn1e7bad02012-06-10 15:20:06 +020027#include <linux/of.h>
Nicolas Pitrefdd8b072009-04-22 20:08:17 +010028#include <mach/bridge-regs.h>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020029
30/*
31 * Watchdog timer block registers.
32 */
Jason Coopera855a7c2012-03-15 00:33:26 +000033#define TIMER_CTRL 0x0000
Axel Lin0dd6e482012-03-26 11:14:29 +080034#define WDT_EN 0x0010
Jason Coopera855a7c2012-03-15 00:33:26 +000035#define WDT_VAL 0x0024
Sylver Bruneau22ac9232008-06-26 10:47:45 +020036
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080037#define WDT_MAX_CYCLE_COUNT 0xffffffff
Sylver Bruneau22ac9232008-06-26 10:47:45 +020038#define WDT_IN_USE 0
39#define WDT_OK_TO_CLOSE 1
40
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010041static bool nowayout = WATCHDOG_NOWAYOUT;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080042static int heartbeat = -1; /* module parameter (seconds) */
43static unsigned int wdt_max_duration; /* (seconds) */
Andrew Lunn4f04be62012-03-04 16:57:31 +010044static struct clk *clk;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080045static unsigned int wdt_tclk;
Jason Coopera855a7c2012-03-15 00:33:26 +000046static void __iomem *wdt_reg;
Axel Lin1334f322011-11-29 13:54:01 +080047static DEFINE_SPINLOCK(wdt_lock);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020048
Axel Lin0dd6e482012-03-26 11:14:29 +080049static int orion_wdt_ping(struct watchdog_device *wdt_dev)
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +010050{
51 spin_lock(&wdt_lock);
52
53 /* Reload watchdog duration */
Axel Lin0dd6e482012-03-26 11:14:29 +080054 writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +010055
56 spin_unlock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +080057 return 0;
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +010058}
59
Axel Lin0dd6e482012-03-26 11:14:29 +080060static int orion_wdt_start(struct watchdog_device *wdt_dev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +020061{
62 u32 reg;
63
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000064 spin_lock(&wdt_lock);
65
Sylver Bruneau22ac9232008-06-26 10:47:45 +020066 /* Set watchdog duration */
Axel Lin0dd6e482012-03-26 11:14:29 +080067 writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020068
69 /* Clear watchdog timer interrupt */
70 reg = readl(BRIDGE_CAUSE);
71 reg &= ~WDT_INT_REQ;
72 writel(reg, BRIDGE_CAUSE);
73
74 /* Enable watchdog timer */
Jason Coopera855a7c2012-03-15 00:33:26 +000075 reg = readl(wdt_reg + TIMER_CTRL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020076 reg |= WDT_EN;
Jason Coopera855a7c2012-03-15 00:33:26 +000077 writel(reg, wdt_reg + TIMER_CTRL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020078
79 /* Enable reset on watchdog */
Thomas Reitmayr6462c612009-06-01 13:38:33 +020080 reg = readl(RSTOUTn_MASK);
81 reg |= WDT_RESET_OUT_EN;
82 writel(reg, RSTOUTn_MASK);
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000083
84 spin_unlock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +080085 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +020086}
87
Axel Lin0dd6e482012-03-26 11:14:29 +080088static int orion_wdt_stop(struct watchdog_device *wdt_dev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +020089{
90 u32 reg;
91
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000092 spin_lock(&wdt_lock);
93
Sylver Bruneau22ac9232008-06-26 10:47:45 +020094 /* Disable reset on watchdog */
Thomas Reitmayr6462c612009-06-01 13:38:33 +020095 reg = readl(RSTOUTn_MASK);
96 reg &= ~WDT_RESET_OUT_EN;
97 writel(reg, RSTOUTn_MASK);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020098
99 /* Disable watchdog timer */
Jason Coopera855a7c2012-03-15 00:33:26 +0000100 reg = readl(wdt_reg + TIMER_CTRL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200101 reg &= ~WDT_EN;
Jason Coopera855a7c2012-03-15 00:33:26 +0000102 writel(reg, wdt_reg + TIMER_CTRL);
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000103
104 spin_unlock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +0800105 return 0;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000106}
107
Axel Lin0dd6e482012-03-26 11:14:29 +0800108static unsigned int orion_wdt_get_timeleft(struct watchdog_device *wdt_dev)
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000109{
Axel Lin0dd6e482012-03-26 11:14:29 +0800110 unsigned int time_left;
111
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000112 spin_lock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +0800113 time_left = readl(wdt_reg + WDT_VAL) / wdt_tclk;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000114 spin_unlock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +0800115
116 return time_left;
117}
118
119static int orion_wdt_set_timeout(struct watchdog_device *wdt_dev,
120 unsigned int timeout)
121{
122 wdt_dev->timeout = timeout;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000123 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200124}
125
Axel Lin0dd6e482012-03-26 11:14:29 +0800126static const struct watchdog_info orion_wdt_info = {
127 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
128 .identity = "Orion Watchdog",
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200129};
130
Axel Lin0dd6e482012-03-26 11:14:29 +0800131static const struct watchdog_ops orion_wdt_ops = {
132 .owner = THIS_MODULE,
133 .start = orion_wdt_start,
134 .stop = orion_wdt_stop,
135 .ping = orion_wdt_ping,
136 .set_timeout = orion_wdt_set_timeout,
137 .get_timeleft = orion_wdt_get_timeleft,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200138};
139
Axel Lin0dd6e482012-03-26 11:14:29 +0800140static struct watchdog_device orion_wdt = {
141 .info = &orion_wdt_info,
142 .ops = &orion_wdt_ops,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200143};
144
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400145static int __devinit orion_wdt_probe(struct platform_device *pdev)
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800146{
Jason Coopera855a7c2012-03-15 00:33:26 +0000147 struct resource *res;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800148 int ret;
149
Axel Lin0dd6e482012-03-26 11:14:29 +0800150 clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunn4f04be62012-03-04 16:57:31 +0100151 if (IS_ERR(clk)) {
Axel Lin0dd6e482012-03-26 11:14:29 +0800152 dev_err(&pdev->dev, "Orion Watchdog missing clock\n");
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800153 return -ENODEV;
154 }
Andrew Lunn4f04be62012-03-04 16:57:31 +0100155 clk_prepare_enable(clk);
156 wdt_tclk = clk_get_rate(clk);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800157
Jason Coopera855a7c2012-03-15 00:33:26 +0000158 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Axel Lin0dd6e482012-03-26 11:14:29 +0800159 wdt_reg = devm_ioremap(&pdev->dev, res->start, resource_size(res));
160 if (!wdt_reg)
161 return -ENOMEM;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800162
163 wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
Axel Lin0dd6e482012-03-26 11:14:29 +0800164
165 if ((heartbeat < 1) || (heartbeat > wdt_max_duration))
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800166 heartbeat = wdt_max_duration;
167
Axel Lin0dd6e482012-03-26 11:14:29 +0800168 orion_wdt.timeout = heartbeat;
169 orion_wdt.min_timeout = 1;
170 orion_wdt.max_timeout = wdt_max_duration;
171
172 watchdog_set_nowayout(&orion_wdt, nowayout);
173 ret = watchdog_register_device(&orion_wdt);
174 if (ret) {
175 clk_disable_unprepare(clk);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800176 return ret;
Axel Lin0dd6e482012-03-26 11:14:29 +0800177 }
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800178
Joe Perches27c766a2012-02-15 15:06:19 -0800179 pr_info("Initial timeout %d sec%s\n",
180 heartbeat, nowayout ? ", nowayout" : "");
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800181 return 0;
182}
183
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400184static int __devexit orion_wdt_remove(struct platform_device *pdev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200185{
Axel Lin0dd6e482012-03-26 11:14:29 +0800186 watchdog_unregister_device(&orion_wdt);
Andrew Lunn4f04be62012-03-04 16:57:31 +0100187 clk_disable_unprepare(clk);
Axel Lin0dd6e482012-03-26 11:14:29 +0800188 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200189}
190
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400191static void orion_wdt_shutdown(struct platform_device *pdev)
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100192{
Axel Lin0dd6e482012-03-26 11:14:29 +0800193 orion_wdt_stop(&orion_wdt);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100194}
195
Andrew Lunn1e7bad02012-06-10 15:20:06 +0200196static const struct of_device_id orion_wdt_of_match_table[] __devinitdata = {
197 { .compatible = "marvell,orion-wdt", },
198 {},
199};
200MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);
201
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400202static struct platform_driver orion_wdt_driver = {
203 .probe = orion_wdt_probe,
204 .remove = __devexit_p(orion_wdt_remove),
205 .shutdown = orion_wdt_shutdown,
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800206 .driver = {
207 .owner = THIS_MODULE,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400208 .name = "orion_wdt",
Andrew Lunn1e7bad02012-06-10 15:20:06 +0200209 .of_match_table = of_match_ptr(orion_wdt_of_match_table),
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800210 },
211};
212
Axel Linb8ec6112011-11-29 13:56:27 +0800213module_platform_driver(orion_wdt_driver);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200214
215MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400216MODULE_DESCRIPTION("Orion Processor Watchdog");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200217
218module_param(heartbeat, int, 0);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100219MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200220
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100221module_param(nowayout, bool, 0);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100222MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
223 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200224
225MODULE_LICENSE("GPL");
226MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);