blob: 7f19fa3b543d4025752459be8352cf38c399daaa [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>
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080019#include <linux/platform_device.h>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020020#include <linux/watchdog.h>
21#include <linux/init.h>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020022#include <linux/io.h>
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000023#include <linux/spinlock.h>
Andrew Lunn4f04be62012-03-04 16:57:31 +010024#include <linux/clk.h>
Axel Lin0dd6e482012-03-26 11:14:29 +080025#include <linux/err.h>
Andrew Lunn1e7bad02012-06-10 15:20:06 +020026#include <linux/of.h>
Nicolas Pitrefdd8b072009-04-22 20:08:17 +010027#include <mach/bridge-regs.h>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020028
29/*
30 * Watchdog timer block registers.
31 */
Jason Coopera855a7c2012-03-15 00:33:26 +000032#define TIMER_CTRL 0x0000
Axel Lin0dd6e482012-03-26 11:14:29 +080033#define WDT_EN 0x0010
Jason Coopera855a7c2012-03-15 00:33:26 +000034#define WDT_VAL 0x0024
Sylver Bruneau22ac9232008-06-26 10:47:45 +020035
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080036#define WDT_MAX_CYCLE_COUNT 0xffffffff
Sylver Bruneau22ac9232008-06-26 10:47:45 +020037#define WDT_IN_USE 0
38#define WDT_OK_TO_CLOSE 1
39
Russell Kingfa142ff2013-06-18 17:19:32 +010040#define WDT_RESET_OUT_EN BIT(1)
41#define WDT_INT_REQ BIT(3)
42
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010043static bool nowayout = WATCHDOG_NOWAYOUT;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080044static int heartbeat = -1; /* module parameter (seconds) */
45static unsigned int wdt_max_duration; /* (seconds) */
Andrew Lunn4f04be62012-03-04 16:57:31 +010046static struct clk *clk;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080047static unsigned int wdt_tclk;
Jason Coopera855a7c2012-03-15 00:33:26 +000048static void __iomem *wdt_reg;
Axel Lin1334f322011-11-29 13:54:01 +080049static DEFINE_SPINLOCK(wdt_lock);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020050
Axel Lin0dd6e482012-03-26 11:14:29 +080051static int orion_wdt_ping(struct watchdog_device *wdt_dev)
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +010052{
53 spin_lock(&wdt_lock);
54
55 /* Reload watchdog duration */
Axel Lin0dd6e482012-03-26 11:14:29 +080056 writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +010057
58 spin_unlock(&wdt_lock);
Axel Lin0dd6e482012-03-26 11:14:29 +080059 return 0;
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +010060}
61
Axel Lin0dd6e482012-03-26 11:14:29 +080062static int orion_wdt_start(struct watchdog_device *wdt_dev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +020063{
64 u32 reg;
65
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000066 spin_lock(&wdt_lock);
67
Sylver Bruneau22ac9232008-06-26 10:47:45 +020068 /* Set watchdog duration */
Axel Lin0dd6e482012-03-26 11:14:29 +080069 writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020070
71 /* Clear watchdog timer interrupt */
Russell King6910ceb2013-06-18 17:20:32 +010072 writel(~WDT_INT_REQ, BRIDGE_CAUSE);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020073
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,
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100143 .min_timeout = 1,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200144};
145
Bill Pemberton2d991a12012-11-19 13:21:41 -0500146static int orion_wdt_probe(struct platform_device *pdev)
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800147{
Jason Coopera855a7c2012-03-15 00:33:26 +0000148 struct resource *res;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800149 int ret;
150
Axel Lin0dd6e482012-03-26 11:14:29 +0800151 clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunn4f04be62012-03-04 16:57:31 +0100152 if (IS_ERR(clk)) {
Axel Lin0dd6e482012-03-26 11:14:29 +0800153 dev_err(&pdev->dev, "Orion Watchdog missing clock\n");
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300154 return PTR_ERR(clk);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800155 }
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300156 ret = clk_prepare_enable(clk);
157 if (ret)
158 return ret;
Andrew Lunn4f04be62012-03-04 16:57:31 +0100159 wdt_tclk = clk_get_rate(clk);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800160
Jason Coopera855a7c2012-03-15 00:33:26 +0000161 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300162 if (!res) {
163 ret = -ENODEV;
164 goto disable_clk;
165 }
166
Axel Lin0dd6e482012-03-26 11:14:29 +0800167 wdt_reg = devm_ioremap(&pdev->dev, res->start, resource_size(res));
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300168 if (!wdt_reg) {
169 ret = -ENOMEM;
170 goto disable_clk;
171 }
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800172
173 wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
Axel Lin0dd6e482012-03-26 11:14:29 +0800174
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100175 orion_wdt.timeout = wdt_max_duration;
Axel Lin0dd6e482012-03-26 11:14:29 +0800176 orion_wdt.max_timeout = wdt_max_duration;
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100177 watchdog_init_timeout(&orion_wdt, heartbeat, &pdev->dev);
Axel Lin0dd6e482012-03-26 11:14:29 +0800178
179 watchdog_set_nowayout(&orion_wdt, nowayout);
180 ret = watchdog_register_device(&orion_wdt);
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300181 if (ret)
182 goto disable_clk;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800183
Joe Perches27c766a2012-02-15 15:06:19 -0800184 pr_info("Initial timeout %d sec%s\n",
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100185 orion_wdt.timeout, nowayout ? ", nowayout" : "");
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800186 return 0;
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300187
188disable_clk:
189 clk_disable_unprepare(clk);
190 return ret;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800191}
192
Bill Pemberton4b12b892012-11-19 13:26:24 -0500193static int orion_wdt_remove(struct platform_device *pdev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200194{
Axel Lin0dd6e482012-03-26 11:14:29 +0800195 watchdog_unregister_device(&orion_wdt);
Andrew Lunn4f04be62012-03-04 16:57:31 +0100196 clk_disable_unprepare(clk);
Axel Lin0dd6e482012-03-26 11:14:29 +0800197 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200198}
199
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400200static void orion_wdt_shutdown(struct platform_device *pdev)
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100201{
Axel Lin0dd6e482012-03-26 11:14:29 +0800202 orion_wdt_stop(&orion_wdt);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100203}
204
Bill Pemberton1d131362012-11-19 13:24:05 -0500205static const struct of_device_id orion_wdt_of_match_table[] = {
Andrew Lunn1e7bad02012-06-10 15:20:06 +0200206 { .compatible = "marvell,orion-wdt", },
207 {},
208};
209MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);
210
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400211static struct platform_driver orion_wdt_driver = {
212 .probe = orion_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500213 .remove = orion_wdt_remove,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400214 .shutdown = orion_wdt_shutdown,
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800215 .driver = {
216 .owner = THIS_MODULE,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400217 .name = "orion_wdt",
Sachin Kamat85eee812013-09-30 10:12:51 +0530218 .of_match_table = orion_wdt_of_match_table,
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800219 },
220};
221
Axel Linb8ec6112011-11-29 13:56:27 +0800222module_platform_driver(orion_wdt_driver);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200223
224MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400225MODULE_DESCRIPTION("Orion Processor Watchdog");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200226
227module_param(heartbeat, int, 0);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100228MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200229
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100230module_param(nowayout, bool, 0);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100231MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
232 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200233
234MODULE_LICENSE("GPL");
Lubomir Rintelf3ea7332013-01-04 15:06:28 +0100235MODULE_ALIAS("platform:orion_wdt");