blob: 65aa65560730abf0b087ea73c54d8e0e80057f0d [file] [log] [blame]
Sylver Bruneau22ac9232008-06-26 10:47:45 +02001/*
Nicolas Pitre3b937a7db2009-06-01 13:56:02 -04002 * drivers/watchdog/orion_wdt.c
Sylver Bruneau22ac9232008-06-26 10:47:45 +02003 *
Nicolas Pitre3b937a7db2009-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>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020027
Ezequiel Garcia868eb612014-02-10 20:00:25 -030028/* RSTOUT mask register physical address for Orion5x, Kirkwood and Dove */
29#define ORION_RSTOUT_MASK_OFFSET 0x20108
30
31/* Internal registers can be configured at any 1 MiB aligned address */
32#define INTERNAL_REGS_MASK ~(SZ_1M - 1)
33
Sylver Bruneau22ac9232008-06-26 10:47:45 +020034/*
35 * Watchdog timer block registers.
36 */
Jason Coopera855a7c2012-03-15 00:33:26 +000037#define TIMER_CTRL 0x0000
Axel Lin0dd6e482012-03-26 11:14:29 +080038#define WDT_EN 0x0010
Jason Coopera855a7c2012-03-15 00:33:26 +000039#define WDT_VAL 0x0024
Sylver Bruneau22ac9232008-06-26 10:47:45 +020040
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080041#define WDT_MAX_CYCLE_COUNT 0xffffffff
Sylver Bruneau22ac9232008-06-26 10:47:45 +020042
Russell Kingfa142ff2013-06-18 17:19:32 +010043#define WDT_RESET_OUT_EN BIT(1)
Russell Kingfa142ff2013-06-18 17:19:32 +010044
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010045static bool nowayout = WATCHDOG_NOWAYOUT;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080046static int heartbeat = -1; /* module parameter (seconds) */
47static unsigned int wdt_max_duration; /* (seconds) */
Andrew Lunn4f04be62012-03-04 16:57:31 +010048static struct clk *clk;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080049static unsigned int wdt_tclk;
Jason Coopera855a7c2012-03-15 00:33:26 +000050static void __iomem *wdt_reg;
Ezequiel Garcia868eb612014-02-10 20:00:25 -030051static void __iomem *wdt_rstout;
Sylver Bruneau22ac9232008-06-26 10:47:45 +020052
Axel Lin0dd6e482012-03-26 11:14:29 +080053static int orion_wdt_ping(struct watchdog_device *wdt_dev)
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +010054{
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +010055 /* Reload watchdog duration */
Axel Lin0dd6e482012-03-26 11:14:29 +080056 writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
Axel Lin0dd6e482012-03-26 11:14:29 +080057 return 0;
Thomas Reitmayrdf6707b2009-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{
Sylver Bruneau22ac9232008-06-26 10:47:45 +020062 /* Set watchdog duration */
Axel Lin0dd6e482012-03-26 11:14:29 +080063 writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020064
Sylver Bruneau22ac9232008-06-26 10:47:45 +020065 /* Enable watchdog timer */
Ezequiel Garciafc8cd2a2014-02-10 20:00:21 -030066 atomic_io_modify(wdt_reg + TIMER_CTRL, WDT_EN, WDT_EN);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020067
68 /* Enable reset on watchdog */
Ezequiel Garcia868eb612014-02-10 20:00:25 -030069 atomic_io_modify(wdt_rstout, WDT_RESET_OUT_EN, WDT_RESET_OUT_EN);
Axel Lin0dd6e482012-03-26 11:14:29 +080070 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +020071}
72
Axel Lin0dd6e482012-03-26 11:14:29 +080073static int orion_wdt_stop(struct watchdog_device *wdt_dev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +020074{
Sylver Bruneau22ac9232008-06-26 10:47:45 +020075 /* Disable reset on watchdog */
Ezequiel Garcia868eb612014-02-10 20:00:25 -030076 atomic_io_modify(wdt_rstout, WDT_RESET_OUT_EN, 0);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020077
78 /* Disable watchdog timer */
Ezequiel Garciafc8cd2a2014-02-10 20:00:21 -030079 atomic_io_modify(wdt_reg + TIMER_CTRL, WDT_EN, 0);
Axel Lin0dd6e482012-03-26 11:14:29 +080080 return 0;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000081}
82
Ezequiel Garciad9d0c532014-02-10 20:00:23 -030083static int orion_wdt_enabled(void)
84{
85 bool enabled, running;
86
Ezequiel Garcia868eb612014-02-10 20:00:25 -030087 enabled = readl(wdt_rstout) & WDT_RESET_OUT_EN;
Ezequiel Garciad9d0c532014-02-10 20:00:23 -030088 running = readl(wdt_reg + TIMER_CTRL) & WDT_EN;
89
90 return enabled && running;
91}
92
Axel Lin0dd6e482012-03-26 11:14:29 +080093static unsigned int orion_wdt_get_timeleft(struct watchdog_device *wdt_dev)
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000094{
Ezequiel Garciafc8cd2a2014-02-10 20:00:21 -030095 return readl(wdt_reg + WDT_VAL) / wdt_tclk;
Axel Lin0dd6e482012-03-26 11:14:29 +080096}
97
98static int orion_wdt_set_timeout(struct watchdog_device *wdt_dev,
99 unsigned int timeout)
100{
101 wdt_dev->timeout = timeout;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000102 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200103}
104
Axel Lin0dd6e482012-03-26 11:14:29 +0800105static const struct watchdog_info orion_wdt_info = {
106 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
107 .identity = "Orion Watchdog",
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200108};
109
Axel Lin0dd6e482012-03-26 11:14:29 +0800110static const struct watchdog_ops orion_wdt_ops = {
111 .owner = THIS_MODULE,
112 .start = orion_wdt_start,
113 .stop = orion_wdt_stop,
114 .ping = orion_wdt_ping,
115 .set_timeout = orion_wdt_set_timeout,
116 .get_timeleft = orion_wdt_get_timeleft,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200117};
118
Axel Lin0dd6e482012-03-26 11:14:29 +0800119static struct watchdog_device orion_wdt = {
120 .info = &orion_wdt_info,
121 .ops = &orion_wdt_ops,
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100122 .min_timeout = 1,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200123};
124
Ezequiel Garciae97662e2014-02-10 20:00:24 -0300125static irqreturn_t orion_wdt_irq(int irq, void *devid)
126{
127 panic("Watchdog Timeout");
128 return IRQ_HANDLED;
129}
130
Ezequiel Garcia868eb612014-02-10 20:00:25 -0300131/*
132 * The original devicetree binding for this driver specified only
133 * one memory resource, so in order to keep DT backwards compatibility
134 * we try to fallback to a hardcoded register address, if the resource
135 * is missing from the devicetree.
136 */
137static void __iomem *orion_wdt_ioremap_rstout(struct platform_device *pdev,
138 phys_addr_t internal_regs)
139{
140 struct resource *res;
141 phys_addr_t rstout;
142
143 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
144 if (res)
145 return devm_ioremap(&pdev->dev, res->start,
146 resource_size(res));
147
148 /* This workaround works only for "orion-wdt", DT-enabled */
149 if (!of_device_is_compatible(pdev->dev.of_node, "marvell,orion-wdt"))
150 return NULL;
151
152 rstout = internal_regs + ORION_RSTOUT_MASK_OFFSET;
153
154 WARN(1, FW_BUG "falling back to harcoded RSTOUT reg 0x%x\n", rstout);
155 return devm_ioremap(&pdev->dev, rstout, 0x4);
156}
157
Bill Pemberton2d991a12012-11-19 13:21:41 -0500158static int orion_wdt_probe(struct platform_device *pdev)
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800159{
Jason Coopera855a7c2012-03-15 00:33:26 +0000160 struct resource *res;
Ezequiel Garciae97662e2014-02-10 20:00:24 -0300161 int ret, irq;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800162
Axel Lin0dd6e482012-03-26 11:14:29 +0800163 clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunn4f04be62012-03-04 16:57:31 +0100164 if (IS_ERR(clk)) {
Axel Lin0dd6e482012-03-26 11:14:29 +0800165 dev_err(&pdev->dev, "Orion Watchdog missing clock\n");
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300166 return PTR_ERR(clk);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800167 }
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300168 ret = clk_prepare_enable(clk);
169 if (ret)
170 return ret;
Andrew Lunn4f04be62012-03-04 16:57:31 +0100171 wdt_tclk = clk_get_rate(clk);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800172
Jason Coopera855a7c2012-03-15 00:33:26 +0000173 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300174 if (!res) {
175 ret = -ENODEV;
176 goto disable_clk;
177 }
178
Axel Lin0dd6e482012-03-26 11:14:29 +0800179 wdt_reg = devm_ioremap(&pdev->dev, res->start, resource_size(res));
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300180 if (!wdt_reg) {
181 ret = -ENOMEM;
182 goto disable_clk;
183 }
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800184
Ezequiel Garcia868eb612014-02-10 20:00:25 -0300185 wdt_rstout = orion_wdt_ioremap_rstout(pdev, res->start &
186 INTERNAL_REGS_MASK);
187 if (!wdt_rstout) {
188 ret = -ENODEV;
189 goto disable_clk;
190 }
191
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800192 wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
Axel Lin0dd6e482012-03-26 11:14:29 +0800193
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100194 orion_wdt.timeout = wdt_max_duration;
Axel Lin0dd6e482012-03-26 11:14:29 +0800195 orion_wdt.max_timeout = wdt_max_duration;
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100196 watchdog_init_timeout(&orion_wdt, heartbeat, &pdev->dev);
Axel Lin0dd6e482012-03-26 11:14:29 +0800197
Ezequiel Garciad9d0c532014-02-10 20:00:23 -0300198 /*
199 * Let's make sure the watchdog is fully stopped, unless it's
200 * explicitly enabled. This may be the case if the module was
201 * removed and re-insterted, or if the bootloader explicitly
202 * set a running watchdog before booting the kernel.
203 */
204 if (!orion_wdt_enabled())
205 orion_wdt_stop(&orion_wdt);
206
Ezequiel Garciae97662e2014-02-10 20:00:24 -0300207 /* Request the IRQ only after the watchdog is disabled */
208 irq = platform_get_irq(pdev, 0);
209 if (irq > 0) {
210 /*
211 * Not all supported platforms specify an interrupt for the
212 * watchdog, so let's make it optional.
213 */
214 ret = devm_request_irq(&pdev->dev, irq, orion_wdt_irq, 0,
215 pdev->name, &orion_wdt);
216 if (ret < 0) {
217 dev_err(&pdev->dev, "failed to request IRQ\n");
218 goto disable_clk;
219 }
220 }
221
Axel Lin0dd6e482012-03-26 11:14:29 +0800222 watchdog_set_nowayout(&orion_wdt, nowayout);
223 ret = watchdog_register_device(&orion_wdt);
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300224 if (ret)
225 goto disable_clk;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800226
Joe Perches27c766a2012-02-15 15:06:19 -0800227 pr_info("Initial timeout %d sec%s\n",
Fabio Porceddac1fd5f62013-02-14 09:14:25 +0100228 orion_wdt.timeout, nowayout ? ", nowayout" : "");
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800229 return 0;
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300230
231disable_clk:
232 clk_disable_unprepare(clk);
233 return ret;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800234}
235
Bill Pemberton4b12b892012-11-19 13:26:24 -0500236static int orion_wdt_remove(struct platform_device *pdev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200237{
Axel Lin0dd6e482012-03-26 11:14:29 +0800238 watchdog_unregister_device(&orion_wdt);
Andrew Lunn4f04be62012-03-04 16:57:31 +0100239 clk_disable_unprepare(clk);
Axel Lin0dd6e482012-03-26 11:14:29 +0800240 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200241}
242
Nicolas Pitre3b937a7db2009-06-01 13:56:02 -0400243static void orion_wdt_shutdown(struct platform_device *pdev)
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100244{
Axel Lin0dd6e482012-03-26 11:14:29 +0800245 orion_wdt_stop(&orion_wdt);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100246}
247
Bill Pemberton1d131362012-11-19 13:24:05 -0500248static const struct of_device_id orion_wdt_of_match_table[] = {
Andrew Lunn1e7bad02012-06-10 15:20:06 +0200249 { .compatible = "marvell,orion-wdt", },
250 {},
251};
252MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);
253
Nicolas Pitre3b937a7db2009-06-01 13:56:02 -0400254static struct platform_driver orion_wdt_driver = {
255 .probe = orion_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500256 .remove = orion_wdt_remove,
Nicolas Pitre3b937a7db2009-06-01 13:56:02 -0400257 .shutdown = orion_wdt_shutdown,
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800258 .driver = {
259 .owner = THIS_MODULE,
Nicolas Pitre3b937a7db2009-06-01 13:56:02 -0400260 .name = "orion_wdt",
Sachin Kamat85eee812013-09-30 10:12:51 +0530261 .of_match_table = orion_wdt_of_match_table,
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800262 },
263};
264
Axel Linb8ec6112011-11-29 13:56:27 +0800265module_platform_driver(orion_wdt_driver);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200266
267MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
Nicolas Pitre3b937a7db2009-06-01 13:56:02 -0400268MODULE_DESCRIPTION("Orion Processor Watchdog");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200269
270module_param(heartbeat, int, 0);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100271MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200272
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100273module_param(nowayout, bool, 0);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100274MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
275 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200276
277MODULE_LICENSE("GPL");
Lubomir Rintelf3ea7332013-01-04 15:06:28 +0100278MODULE_ALIAS("platform:orion_wdt");