blob: b7067acd43a2972bc22847a7ef09254824c13f14 [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>
Ezequiel Garciae97662e2014-02-10 20:00:24 -030022#include <linux/interrupt.h>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020023#include <linux/io.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
Russell Kingfa142ff2013-06-18 17:19:32 +010038#define WDT_RESET_OUT_EN BIT(1)
39#define WDT_INT_REQ BIT(3)
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;
Sylver Bruneau22ac9232008-06-26 10:47:45 +020047
Axel Lin0dd6e482012-03-26 11:14:29 +080048static int orion_wdt_ping(struct watchdog_device *wdt_dev)
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +010049{
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +010050 /* Reload watchdog duration */
Axel Lin0dd6e482012-03-26 11:14:29 +080051 writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
Axel Lin0dd6e482012-03-26 11:14:29 +080052 return 0;
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +010053}
54
Axel Lin0dd6e482012-03-26 11:14:29 +080055static int orion_wdt_start(struct watchdog_device *wdt_dev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +020056{
Sylver Bruneau22ac9232008-06-26 10:47:45 +020057 /* Set watchdog duration */
Axel Lin0dd6e482012-03-26 11:14:29 +080058 writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020059
60 /* Clear watchdog timer interrupt */
Russell King6910ceb2013-06-18 17:20:32 +010061 writel(~WDT_INT_REQ, BRIDGE_CAUSE);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020062
63 /* Enable watchdog timer */
Ezequiel Garciafc8cd2a2014-02-10 20:00:21 -030064 atomic_io_modify(wdt_reg + TIMER_CTRL, WDT_EN, WDT_EN);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020065
66 /* Enable reset on watchdog */
Ezequiel Garciafc8cd2a2014-02-10 20:00:21 -030067 atomic_io_modify(RSTOUTn_MASK, WDT_RESET_OUT_EN, WDT_RESET_OUT_EN);
Axel Lin0dd6e482012-03-26 11:14:29 +080068 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +020069}
70
Axel Lin0dd6e482012-03-26 11:14:29 +080071static int orion_wdt_stop(struct watchdog_device *wdt_dev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +020072{
Sylver Bruneau22ac9232008-06-26 10:47:45 +020073 /* Disable reset on watchdog */
Ezequiel Garciafc8cd2a2014-02-10 20:00:21 -030074 atomic_io_modify(RSTOUTn_MASK, WDT_RESET_OUT_EN, 0);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020075
76 /* Disable watchdog timer */
Ezequiel Garciafc8cd2a2014-02-10 20:00:21 -030077 atomic_io_modify(wdt_reg + TIMER_CTRL, WDT_EN, 0);
Axel Lin0dd6e482012-03-26 11:14:29 +080078 return 0;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000079}
80
Ezequiel Garciad9d0c532014-02-10 20:00:23 -030081static int orion_wdt_enabled(void)
82{
83 bool enabled, running;
84
85 enabled = readl(RSTOUTn_MASK) & WDT_RESET_OUT_EN;
86 running = readl(wdt_reg + TIMER_CTRL) & WDT_EN;
87
88 return enabled && running;
89}
90
Axel Lin0dd6e482012-03-26 11:14:29 +080091static unsigned int orion_wdt_get_timeleft(struct watchdog_device *wdt_dev)
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000092{
Ezequiel Garciafc8cd2a2014-02-10 20:00:21 -030093 return readl(wdt_reg + WDT_VAL) / wdt_tclk;
Axel Lin0dd6e482012-03-26 11:14:29 +080094}
95
96static int orion_wdt_set_timeout(struct watchdog_device *wdt_dev,
97 unsigned int timeout)
98{
99 wdt_dev->timeout = timeout;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000100 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200101}
102
Axel Lin0dd6e482012-03-26 11:14:29 +0800103static const struct watchdog_info orion_wdt_info = {
104 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
105 .identity = "Orion Watchdog",
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200106};
107
Axel Lin0dd6e482012-03-26 11:14:29 +0800108static const struct watchdog_ops orion_wdt_ops = {
109 .owner = THIS_MODULE,
110 .start = orion_wdt_start,
111 .stop = orion_wdt_stop,
112 .ping = orion_wdt_ping,
113 .set_timeout = orion_wdt_set_timeout,
114 .get_timeleft = orion_wdt_get_timeleft,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200115};
116
Axel Lin0dd6e482012-03-26 11:14:29 +0800117static struct watchdog_device orion_wdt = {
118 .info = &orion_wdt_info,
119 .ops = &orion_wdt_ops,
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100120 .min_timeout = 1,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200121};
122
Ezequiel Garciae97662e2014-02-10 20:00:24 -0300123static irqreturn_t orion_wdt_irq(int irq, void *devid)
124{
125 panic("Watchdog Timeout");
126 return IRQ_HANDLED;
127}
128
Bill Pemberton2d991a12012-11-19 13:21:41 -0500129static int orion_wdt_probe(struct platform_device *pdev)
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800130{
Jason Coopera855a7c2012-03-15 00:33:26 +0000131 struct resource *res;
Ezequiel Garciae97662e2014-02-10 20:00:24 -0300132 int ret, irq;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800133
Axel Lin0dd6e482012-03-26 11:14:29 +0800134 clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunn4f04be62012-03-04 16:57:31 +0100135 if (IS_ERR(clk)) {
Axel Lin0dd6e482012-03-26 11:14:29 +0800136 dev_err(&pdev->dev, "Orion Watchdog missing clock\n");
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300137 return PTR_ERR(clk);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800138 }
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300139 ret = clk_prepare_enable(clk);
140 if (ret)
141 return ret;
Andrew Lunn4f04be62012-03-04 16:57:31 +0100142 wdt_tclk = clk_get_rate(clk);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800143
Jason Coopera855a7c2012-03-15 00:33:26 +0000144 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300145 if (!res) {
146 ret = -ENODEV;
147 goto disable_clk;
148 }
149
Axel Lin0dd6e482012-03-26 11:14:29 +0800150 wdt_reg = devm_ioremap(&pdev->dev, res->start, resource_size(res));
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300151 if (!wdt_reg) {
152 ret = -ENOMEM;
153 goto disable_clk;
154 }
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800155
156 wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
Axel Lin0dd6e482012-03-26 11:14:29 +0800157
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100158 orion_wdt.timeout = wdt_max_duration;
Axel Lin0dd6e482012-03-26 11:14:29 +0800159 orion_wdt.max_timeout = wdt_max_duration;
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100160 watchdog_init_timeout(&orion_wdt, heartbeat, &pdev->dev);
Axel Lin0dd6e482012-03-26 11:14:29 +0800161
Ezequiel Garciad9d0c532014-02-10 20:00:23 -0300162 /*
163 * Let's make sure the watchdog is fully stopped, unless it's
164 * explicitly enabled. This may be the case if the module was
165 * removed and re-insterted, or if the bootloader explicitly
166 * set a running watchdog before booting the kernel.
167 */
168 if (!orion_wdt_enabled())
169 orion_wdt_stop(&orion_wdt);
170
Ezequiel Garciae97662e2014-02-10 20:00:24 -0300171 /* Request the IRQ only after the watchdog is disabled */
172 irq = platform_get_irq(pdev, 0);
173 if (irq > 0) {
174 /*
175 * Not all supported platforms specify an interrupt for the
176 * watchdog, so let's make it optional.
177 */
178 ret = devm_request_irq(&pdev->dev, irq, orion_wdt_irq, 0,
179 pdev->name, &orion_wdt);
180 if (ret < 0) {
181 dev_err(&pdev->dev, "failed to request IRQ\n");
182 goto disable_clk;
183 }
184 }
185
Axel Lin0dd6e482012-03-26 11:14:29 +0800186 watchdog_set_nowayout(&orion_wdt, nowayout);
187 ret = watchdog_register_device(&orion_wdt);
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300188 if (ret)
189 goto disable_clk;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800190
Joe Perches27c766a2012-02-15 15:06:19 -0800191 pr_info("Initial timeout %d sec%s\n",
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100192 orion_wdt.timeout, nowayout ? ", nowayout" : "");
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800193 return 0;
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300194
195disable_clk:
196 clk_disable_unprepare(clk);
197 return ret;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800198}
199
Bill Pemberton4b12b892012-11-19 13:26:24 -0500200static int orion_wdt_remove(struct platform_device *pdev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200201{
Axel Lin0dd6e482012-03-26 11:14:29 +0800202 watchdog_unregister_device(&orion_wdt);
Andrew Lunn4f04be62012-03-04 16:57:31 +0100203 clk_disable_unprepare(clk);
Axel Lin0dd6e482012-03-26 11:14:29 +0800204 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200205}
206
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400207static void orion_wdt_shutdown(struct platform_device *pdev)
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100208{
Axel Lin0dd6e482012-03-26 11:14:29 +0800209 orion_wdt_stop(&orion_wdt);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100210}
211
Bill Pemberton1d131362012-11-19 13:24:05 -0500212static const struct of_device_id orion_wdt_of_match_table[] = {
Andrew Lunn1e7bad02012-06-10 15:20:06 +0200213 { .compatible = "marvell,orion-wdt", },
214 {},
215};
216MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);
217
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400218static struct platform_driver orion_wdt_driver = {
219 .probe = orion_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500220 .remove = orion_wdt_remove,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400221 .shutdown = orion_wdt_shutdown,
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800222 .driver = {
223 .owner = THIS_MODULE,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400224 .name = "orion_wdt",
Sachin Kamat85eee812013-09-30 10:12:51 +0530225 .of_match_table = orion_wdt_of_match_table,
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800226 },
227};
228
Axel Linb8ec6112011-11-29 13:56:27 +0800229module_platform_driver(orion_wdt_driver);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200230
231MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400232MODULE_DESCRIPTION("Orion Processor Watchdog");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200233
234module_param(heartbeat, int, 0);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100235MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200236
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100237module_param(nowayout, bool, 0);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100238MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
239 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200240
241MODULE_LICENSE("GPL");
Lubomir Rintelf3ea7332013-01-04 15:06:28 +0100242MODULE_ALIAS("platform:orion_wdt");