blob: e10b0b1accceeee8cc3eb73d746b2dc36ac6033a [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>
Andrew Lunn4f04be62012-03-04 16:57:31 +010023#include <linux/clk.h>
Axel Lin0dd6e482012-03-26 11:14:29 +080024#include <linux/err.h>
Andrew Lunn1e7bad02012-06-10 15:20:06 +020025#include <linux/of.h>
Nicolas Pitrefdd8b072009-04-22 20:08:17 +010026#include <mach/bridge-regs.h>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020027
28/*
29 * Watchdog timer block registers.
30 */
Jason Coopera855a7c2012-03-15 00:33:26 +000031#define TIMER_CTRL 0x0000
Axel Lin0dd6e482012-03-26 11:14:29 +080032#define WDT_EN 0x0010
Jason Coopera855a7c2012-03-15 00:33:26 +000033#define WDT_VAL 0x0024
Sylver Bruneau22ac9232008-06-26 10:47:45 +020034
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080035#define WDT_MAX_CYCLE_COUNT 0xffffffff
Sylver Bruneau22ac9232008-06-26 10:47:45 +020036
Russell Kingfa142ff2013-06-18 17:19:32 +010037#define WDT_RESET_OUT_EN BIT(1)
38#define WDT_INT_REQ BIT(3)
39
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010040static bool nowayout = WATCHDOG_NOWAYOUT;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080041static int heartbeat = -1; /* module parameter (seconds) */
42static unsigned int wdt_max_duration; /* (seconds) */
Andrew Lunn4f04be62012-03-04 16:57:31 +010043static struct clk *clk;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080044static unsigned int wdt_tclk;
Jason Coopera855a7c2012-03-15 00:33:26 +000045static void __iomem *wdt_reg;
Sylver Bruneau22ac9232008-06-26 10:47:45 +020046
Axel Lin0dd6e482012-03-26 11:14:29 +080047static int orion_wdt_ping(struct watchdog_device *wdt_dev)
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +010048{
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +010049 /* Reload watchdog duration */
Axel Lin0dd6e482012-03-26 11:14:29 +080050 writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
Axel Lin0dd6e482012-03-26 11:14:29 +080051 return 0;
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +010052}
53
Axel Lin0dd6e482012-03-26 11:14:29 +080054static int orion_wdt_start(struct watchdog_device *wdt_dev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +020055{
Sylver Bruneau22ac9232008-06-26 10:47:45 +020056 /* Set watchdog duration */
Axel Lin0dd6e482012-03-26 11:14:29 +080057 writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020058
59 /* Clear watchdog timer interrupt */
Russell King6910ceb2013-06-18 17:20:32 +010060 writel(~WDT_INT_REQ, BRIDGE_CAUSE);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020061
62 /* Enable watchdog timer */
Ezequiel Garciafc8cd2a2014-02-10 20:00:21 -030063 atomic_io_modify(wdt_reg + TIMER_CTRL, WDT_EN, WDT_EN);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020064
65 /* Enable reset on watchdog */
Ezequiel Garciafc8cd2a2014-02-10 20:00:21 -030066 atomic_io_modify(RSTOUTn_MASK, WDT_RESET_OUT_EN, WDT_RESET_OUT_EN);
Axel Lin0dd6e482012-03-26 11:14:29 +080067 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +020068}
69
Axel Lin0dd6e482012-03-26 11:14:29 +080070static int orion_wdt_stop(struct watchdog_device *wdt_dev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +020071{
Sylver Bruneau22ac9232008-06-26 10:47:45 +020072 /* Disable reset on watchdog */
Ezequiel Garciafc8cd2a2014-02-10 20:00:21 -030073 atomic_io_modify(RSTOUTn_MASK, WDT_RESET_OUT_EN, 0);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020074
75 /* Disable watchdog timer */
Ezequiel Garciafc8cd2a2014-02-10 20:00:21 -030076 atomic_io_modify(wdt_reg + TIMER_CTRL, WDT_EN, 0);
Axel Lin0dd6e482012-03-26 11:14:29 +080077 return 0;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000078}
79
Ezequiel Garciad9d0c532014-02-10 20:00:23 -030080static int orion_wdt_enabled(void)
81{
82 bool enabled, running;
83
84 enabled = readl(RSTOUTn_MASK) & WDT_RESET_OUT_EN;
85 running = readl(wdt_reg + TIMER_CTRL) & WDT_EN;
86
87 return enabled && running;
88}
89
Axel Lin0dd6e482012-03-26 11:14:29 +080090static unsigned int orion_wdt_get_timeleft(struct watchdog_device *wdt_dev)
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000091{
Ezequiel Garciafc8cd2a2014-02-10 20:00:21 -030092 return readl(wdt_reg + WDT_VAL) / wdt_tclk;
Axel Lin0dd6e482012-03-26 11:14:29 +080093}
94
95static int orion_wdt_set_timeout(struct watchdog_device *wdt_dev,
96 unsigned int timeout)
97{
98 wdt_dev->timeout = timeout;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000099 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200100}
101
Axel Lin0dd6e482012-03-26 11:14:29 +0800102static const struct watchdog_info orion_wdt_info = {
103 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
104 .identity = "Orion Watchdog",
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200105};
106
Axel Lin0dd6e482012-03-26 11:14:29 +0800107static const struct watchdog_ops orion_wdt_ops = {
108 .owner = THIS_MODULE,
109 .start = orion_wdt_start,
110 .stop = orion_wdt_stop,
111 .ping = orion_wdt_ping,
112 .set_timeout = orion_wdt_set_timeout,
113 .get_timeleft = orion_wdt_get_timeleft,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200114};
115
Axel Lin0dd6e482012-03-26 11:14:29 +0800116static struct watchdog_device orion_wdt = {
117 .info = &orion_wdt_info,
118 .ops = &orion_wdt_ops,
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100119 .min_timeout = 1,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200120};
121
Bill Pemberton2d991a12012-11-19 13:21:41 -0500122static int orion_wdt_probe(struct platform_device *pdev)
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800123{
Jason Coopera855a7c2012-03-15 00:33:26 +0000124 struct resource *res;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800125 int ret;
126
Axel Lin0dd6e482012-03-26 11:14:29 +0800127 clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunn4f04be62012-03-04 16:57:31 +0100128 if (IS_ERR(clk)) {
Axel Lin0dd6e482012-03-26 11:14:29 +0800129 dev_err(&pdev->dev, "Orion Watchdog missing clock\n");
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300130 return PTR_ERR(clk);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800131 }
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300132 ret = clk_prepare_enable(clk);
133 if (ret)
134 return ret;
Andrew Lunn4f04be62012-03-04 16:57:31 +0100135 wdt_tclk = clk_get_rate(clk);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800136
Jason Coopera855a7c2012-03-15 00:33:26 +0000137 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300138 if (!res) {
139 ret = -ENODEV;
140 goto disable_clk;
141 }
142
Axel Lin0dd6e482012-03-26 11:14:29 +0800143 wdt_reg = devm_ioremap(&pdev->dev, res->start, resource_size(res));
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300144 if (!wdt_reg) {
145 ret = -ENOMEM;
146 goto disable_clk;
147 }
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800148
149 wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
Axel Lin0dd6e482012-03-26 11:14:29 +0800150
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100151 orion_wdt.timeout = wdt_max_duration;
Axel Lin0dd6e482012-03-26 11:14:29 +0800152 orion_wdt.max_timeout = wdt_max_duration;
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100153 watchdog_init_timeout(&orion_wdt, heartbeat, &pdev->dev);
Axel Lin0dd6e482012-03-26 11:14:29 +0800154
Ezequiel Garciad9d0c532014-02-10 20:00:23 -0300155 /*
156 * Let's make sure the watchdog is fully stopped, unless it's
157 * explicitly enabled. This may be the case if the module was
158 * removed and re-insterted, or if the bootloader explicitly
159 * set a running watchdog before booting the kernel.
160 */
161 if (!orion_wdt_enabled())
162 orion_wdt_stop(&orion_wdt);
163
Axel Lin0dd6e482012-03-26 11:14:29 +0800164 watchdog_set_nowayout(&orion_wdt, nowayout);
165 ret = watchdog_register_device(&orion_wdt);
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300166 if (ret)
167 goto disable_clk;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800168
Joe Perches27c766a2012-02-15 15:06:19 -0800169 pr_info("Initial timeout %d sec%s\n",
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100170 orion_wdt.timeout, nowayout ? ", nowayout" : "");
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800171 return 0;
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300172
173disable_clk:
174 clk_disable_unprepare(clk);
175 return ret;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800176}
177
Bill Pemberton4b12b892012-11-19 13:26:24 -0500178static int orion_wdt_remove(struct platform_device *pdev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200179{
Axel Lin0dd6e482012-03-26 11:14:29 +0800180 watchdog_unregister_device(&orion_wdt);
Andrew Lunn4f04be62012-03-04 16:57:31 +0100181 clk_disable_unprepare(clk);
Axel Lin0dd6e482012-03-26 11:14:29 +0800182 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200183}
184
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400185static void orion_wdt_shutdown(struct platform_device *pdev)
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100186{
Axel Lin0dd6e482012-03-26 11:14:29 +0800187 orion_wdt_stop(&orion_wdt);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100188}
189
Bill Pemberton1d131362012-11-19 13:24:05 -0500190static const struct of_device_id orion_wdt_of_match_table[] = {
Andrew Lunn1e7bad02012-06-10 15:20:06 +0200191 { .compatible = "marvell,orion-wdt", },
192 {},
193};
194MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);
195
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400196static struct platform_driver orion_wdt_driver = {
197 .probe = orion_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500198 .remove = orion_wdt_remove,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400199 .shutdown = orion_wdt_shutdown,
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800200 .driver = {
201 .owner = THIS_MODULE,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400202 .name = "orion_wdt",
Sachin Kamat85eee812013-09-30 10:12:51 +0530203 .of_match_table = orion_wdt_of_match_table,
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800204 },
205};
206
Axel Linb8ec6112011-11-29 13:56:27 +0800207module_platform_driver(orion_wdt_driver);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200208
209MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400210MODULE_DESCRIPTION("Orion Processor Watchdog");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200211
212module_param(heartbeat, int, 0);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100213MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200214
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100215module_param(nowayout, bool, 0);
Thomas Reitmayrdf6707b22009-02-20 19:44:59 +0100216MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
217 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200218
219MODULE_LICENSE("GPL");
Lubomir Rintelf3ea7332013-01-04 15:06:28 +0100220MODULE_ALIAS("platform:orion_wdt");