blob: 6711b4e3a862d43ca43f131e0dbbd5b0e06032b5 [file] [log] [blame]
Fabio Estevamcd6100f2018-07-10 11:21:22 -03001// SPDX-License-Identifier: GPL-2.0
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +02002/*
3 * Watchdog driver for IMX2 and later processors
4 *
5 * Copyright (C) 2010 Wolfram Sang, Pengutronix e.K. <w.sang@pengutronix.de>
Anson Huang1a9c5ef2014-01-13 19:58:34 +08006 * Copyright (C) 2014 Freescale Semiconductor, Inc.
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +02007 *
8 * some parts adapted by similar drivers from Darius Augulis and Vladimir
9 * Zapolskiy, additional improvements by Wim Van Sebroeck.
10 *
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020011 * NOTE: MX1 has a slightly different Watchdog than MX2 and later:
12 *
13 * MX1: MX2+:
14 * ---- -----
15 * Registers: 32-bit 16-bit
16 * Stopable timer: Yes No
17 * Need to enable clk: No Yes
18 * Halt on suspend: Manual Can be automatic
19 */
20
Xiubo Li30cb0422014-04-04 09:33:24 +080021#include <linux/clk.h>
Jingchang Lu334a9d82014-09-12 15:24:36 +080022#include <linux/delay.h>
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020023#include <linux/init.h>
Vladimir Zapolskiy39487f62016-10-07 15:41:39 +030024#include <linux/interrupt.h>
Xiubo Li30cb0422014-04-04 09:33:24 +080025#include <linux/io.h>
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020026#include <linux/kernel.h>
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020027#include <linux/module.h>
28#include <linux/moduleparam.h>
Xiubo Lif728f4b2014-06-03 10:45:14 +080029#include <linux/of_address.h>
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020030#include <linux/platform_device.h>
Xiubo Lia7977002014-04-04 09:33:25 +080031#include <linux/regmap.h>
Xiubo Li30cb0422014-04-04 09:33:24 +080032#include <linux/watchdog.h>
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020033
34#define DRIVER_NAME "imx2-wdt"
35
36#define IMX2_WDT_WCR 0x00 /* Control Register */
37#define IMX2_WDT_WCR_WT (0xFF << 8) /* -> Watchdog Timeout Field */
Vladimir Zapolskiy68d4cb82016-08-31 14:52:49 +030038#define IMX2_WDT_WCR_WDA BIT(5) /* -> External Reset WDOG_B */
39#define IMX2_WDT_WCR_SRS BIT(4) /* -> Software Reset Signal */
40#define IMX2_WDT_WCR_WRE BIT(3) /* -> WDOG Reset Enable */
41#define IMX2_WDT_WCR_WDE BIT(2) /* -> Watchdog Enable */
42#define IMX2_WDT_WCR_WDZST BIT(0) /* -> Watchdog timer Suspend */
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020043
44#define IMX2_WDT_WSR 0x02 /* Service Register */
45#define IMX2_WDT_SEQ1 0x5555 /* -> service sequence 1 */
46#define IMX2_WDT_SEQ2 0xAAAA /* -> service sequence 2 */
47
Oskar Schirmer474ef122012-02-16 12:17:45 +000048#define IMX2_WDT_WRSR 0x04 /* Reset Status Register */
Vladimir Zapolskiy68d4cb82016-08-31 14:52:49 +030049#define IMX2_WDT_WRSR_TOUT BIT(1) /* -> Reset due to Timeout */
Oskar Schirmer474ef122012-02-16 12:17:45 +000050
Vladimir Zapolskiy39487f62016-10-07 15:41:39 +030051#define IMX2_WDT_WICR 0x06 /* Interrupt Control Register */
52#define IMX2_WDT_WICR_WIE BIT(15) /* -> Interrupt Enable */
53#define IMX2_WDT_WICR_WTIS BIT(14) /* -> Interrupt Status */
54#define IMX2_WDT_WICR_WICT 0xFF /* -> Interrupt Count Timeout */
55
Markus Pargmann5fe65ce2014-09-08 09:14:07 +020056#define IMX2_WDT_WMCR 0x08 /* Misc Register */
57
Rasmus Villemoes144783a2019-08-12 15:13:56 +020058#define IMX2_WDT_MAX_TIME 128U
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020059#define IMX2_WDT_DEFAULT_TIME 60 /* in seconds */
60
61#define WDOG_SEC_TO_COUNT(s) ((s * 2 - 1) << 8)
62
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +020063struct imx2_wdt_device {
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020064 struct clk *clk;
Xiubo Lia7977002014-04-04 09:33:25 +080065 struct regmap *regmap;
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +020066 struct watchdog_device wdog;
Tim Harveybc677ff42016-04-01 08:16:43 -070067 bool ext_reset;
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +020068};
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020069
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010070static bool nowayout = WATCHDOG_NOWAYOUT;
71module_param(nowayout, bool, 0);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020072MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
73 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
74
Marcus Folkesson2b77f002018-02-08 14:11:08 +010075static unsigned timeout;
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020076module_param(timeout, uint, 0);
77MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default="
78 __MODULE_STRING(IMX2_WDT_DEFAULT_TIME) ")");
79
80static const struct watchdog_info imx2_wdt_info = {
81 .identity = "imx2+ watchdog",
82 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
83};
84
Vladimir Zapolskiy39487f62016-10-07 15:41:39 +030085static const struct watchdog_info imx2_wdt_pretimeout_info = {
86 .identity = "imx2+ watchdog",
87 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
88 WDIOF_PRETIMEOUT,
89};
90
Guenter Roeck4d8b2292016-02-26 17:32:49 -080091static int imx2_wdt_restart(struct watchdog_device *wdog, unsigned long action,
92 void *data)
Jingchang Lu334a9d82014-09-12 15:24:36 +080093{
Damien Riegel2d9d24752015-11-16 12:28:04 -050094 struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
Jingchang Lu334a9d82014-09-12 15:24:36 +080095 unsigned int wcr_enable = IMX2_WDT_WCR_WDE;
Damien Riegel2d9d24752015-11-16 12:28:04 -050096
Tim Harveybc677ff42016-04-01 08:16:43 -070097 /* Use internal reset or external - not both */
98 if (wdev->ext_reset)
99 wcr_enable |= IMX2_WDT_WCR_SRS; /* do not assert int reset */
100 else
101 wcr_enable |= IMX2_WDT_WCR_WDA; /* do not assert ext-reset */
102
Jingchang Lu334a9d82014-09-12 15:24:36 +0800103 /* Assert SRS signal */
Fabio Estevam9493c0d2015-10-02 00:25:28 -0300104 regmap_write(wdev->regmap, IMX2_WDT_WCR, wcr_enable);
Jingchang Lu334a9d82014-09-12 15:24:36 +0800105 /*
106 * Due to imx6q errata ERR004346 (WDOG: WDOG SRS bit requires to be
107 * written twice), we add another two writes to ensure there must be at
108 * least two writes happen in the same one 32kHz clock period. We save
109 * the target check here, since the writes shouldn't be a huge burden
110 * for other platforms.
111 */
Fabio Estevam9493c0d2015-10-02 00:25:28 -0300112 regmap_write(wdev->regmap, IMX2_WDT_WCR, wcr_enable);
113 regmap_write(wdev->regmap, IMX2_WDT_WCR, wcr_enable);
Jingchang Lu334a9d82014-09-12 15:24:36 +0800114
115 /* wait for reset to assert... */
116 mdelay(500);
117
Damien Riegel2d9d24752015-11-16 12:28:04 -0500118 return 0;
Jingchang Lu334a9d82014-09-12 15:24:36 +0800119}
120
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200121static inline void imx2_wdt_setup(struct watchdog_device *wdog)
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200122{
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200123 struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
Xiubo Lia7977002014-04-04 09:33:25 +0800124 u32 val;
125
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200126 regmap_read(wdev->regmap, IMX2_WDT_WCR, &val);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200127
Anson Huang1a9c5ef2014-01-13 19:58:34 +0800128 /* Suspend timer in low power mode, write once-only */
129 val |= IMX2_WDT_WCR_WDZST;
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200130 /* Strip the old watchdog Time-Out value */
131 val &= ~IMX2_WDT_WCR_WT;
Tim Harveybc677ff42016-04-01 08:16:43 -0700132 /* Generate internal chip-level reset if WDOG times out */
133 if (!wdev->ext_reset)
134 val &= ~IMX2_WDT_WCR_WRE;
135 /* Or if external-reset assert WDOG_B reset only on time-out */
136 else
137 val |= IMX2_WDT_WCR_WRE;
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200138 /* Keep Watchdog Disabled */
139 val &= ~IMX2_WDT_WCR_WDE;
140 /* Set the watchdog's Time-Out value */
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200141 val |= WDOG_SEC_TO_COUNT(wdog->timeout);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200142
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200143 regmap_write(wdev->regmap, IMX2_WDT_WCR, val);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200144
145 /* enable the watchdog */
146 val |= IMX2_WDT_WCR_WDE;
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200147 regmap_write(wdev->regmap, IMX2_WDT_WCR, val);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200148}
149
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200150static inline bool imx2_wdt_is_running(struct imx2_wdt_device *wdev)
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200151{
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200152 u32 val;
153
154 regmap_read(wdev->regmap, IMX2_WDT_WCR, &val);
155
156 return val & IMX2_WDT_WCR_WDE;
157}
158
159static int imx2_wdt_ping(struct watchdog_device *wdog)
160{
161 struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
162
163 regmap_write(wdev->regmap, IMX2_WDT_WSR, IMX2_WDT_SEQ1);
164 regmap_write(wdev->regmap, IMX2_WDT_WSR, IMX2_WDT_SEQ2);
165 return 0;
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200166}
167
Martin Kaiser0be26722018-01-01 18:26:47 +0100168static void __imx2_wdt_set_timeout(struct watchdog_device *wdog,
169 unsigned int new_timeout)
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200170{
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200171 struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200172
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200173 regmap_update_bits(wdev->regmap, IMX2_WDT_WCR, IMX2_WDT_WCR_WT,
Xiubo Lia7977002014-04-04 09:33:25 +0800174 WDOG_SEC_TO_COUNT(new_timeout));
Martin Kaiser0be26722018-01-01 18:26:47 +0100175}
176
177static int imx2_wdt_set_timeout(struct watchdog_device *wdog,
178 unsigned int new_timeout)
179{
Georg Hofmannb07e2282019-04-08 21:25:54 +0200180 unsigned int actual;
Martin Kaiser0be26722018-01-01 18:26:47 +0100181
Rasmus Villemoes144783a2019-08-12 15:13:56 +0200182 actual = min(new_timeout, IMX2_WDT_MAX_TIME);
Georg Hofmannb07e2282019-04-08 21:25:54 +0200183 __imx2_wdt_set_timeout(wdog, actual);
Martin Kaiser0be26722018-01-01 18:26:47 +0100184 wdog->timeout = new_timeout;
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200185 return 0;
186}
187
Vladimir Zapolskiy39487f62016-10-07 15:41:39 +0300188static int imx2_wdt_set_pretimeout(struct watchdog_device *wdog,
189 unsigned int new_pretimeout)
190{
191 struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
192
193 if (new_pretimeout >= IMX2_WDT_MAX_TIME)
194 return -EINVAL;
195
196 wdog->pretimeout = new_pretimeout;
197
198 regmap_update_bits(wdev->regmap, IMX2_WDT_WICR,
199 IMX2_WDT_WICR_WIE | IMX2_WDT_WICR_WICT,
200 IMX2_WDT_WICR_WIE | (new_pretimeout << 1));
201 return 0;
202}
203
204static irqreturn_t imx2_wdt_isr(int irq, void *wdog_arg)
205{
206 struct watchdog_device *wdog = wdog_arg;
207 struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
208
209 regmap_write_bits(wdev->regmap, IMX2_WDT_WICR,
210 IMX2_WDT_WICR_WTIS, IMX2_WDT_WICR_WTIS);
211
212 watchdog_notify_pretimeout(wdog);
213
214 return IRQ_HANDLED;
215}
216
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200217static int imx2_wdt_start(struct watchdog_device *wdog)
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200218{
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200219 struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200220
Guenter Roeck11d7aba2016-02-28 13:12:20 -0800221 if (imx2_wdt_is_running(wdev))
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200222 imx2_wdt_set_timeout(wdog, wdog->timeout);
Guenter Roeck11d7aba2016-02-28 13:12:20 -0800223 else
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200224 imx2_wdt_setup(wdog);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200225
Guenter Roeck11d7aba2016-02-28 13:12:20 -0800226 set_bit(WDOG_HW_RUNNING, &wdog->status);
227
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200228 return imx2_wdt_ping(wdog);
229}
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200230
Krzysztof Kozlowski4bd8ce32015-01-05 10:09:17 +0100231static const struct watchdog_ops imx2_wdt_ops = {
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200232 .owner = THIS_MODULE,
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200233 .start = imx2_wdt_start,
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200234 .ping = imx2_wdt_ping,
235 .set_timeout = imx2_wdt_set_timeout,
Vladimir Zapolskiy39487f62016-10-07 15:41:39 +0300236 .set_pretimeout = imx2_wdt_set_pretimeout,
Damien Riegel2d9d24752015-11-16 12:28:04 -0500237 .restart = imx2_wdt_restart,
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200238};
239
Krzysztof Kozlowski4bd8ce32015-01-05 10:09:17 +0100240static const struct regmap_config imx2_wdt_regmap_config = {
Xiubo Lia7977002014-04-04 09:33:25 +0800241 .reg_bits = 16,
242 .reg_stride = 2,
243 .val_bits = 16,
244 .max_register = 0x8,
245};
246
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200247static int __init imx2_wdt_probe(struct platform_device *pdev)
248{
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200249 struct imx2_wdt_device *wdev;
250 struct watchdog_device *wdog;
Xiubo Lia7977002014-04-04 09:33:25 +0800251 void __iomem *base;
252 int ret;
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200253 u32 val;
254
255 wdev = devm_kzalloc(&pdev->dev, sizeof(*wdev), GFP_KERNEL);
256 if (!wdev)
257 return -ENOMEM;
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200258
Anson Huang24b82252019-04-01 05:04:30 +0000259 base = devm_platform_ioremap_resource(pdev, 0);
Xiubo Lia7977002014-04-04 09:33:25 +0800260 if (IS_ERR(base))
261 return PTR_ERR(base);
262
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200263 wdev->regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
264 &imx2_wdt_regmap_config);
265 if (IS_ERR(wdev->regmap)) {
Xiubo Lia7977002014-04-04 09:33:25 +0800266 dev_err(&pdev->dev, "regmap init failed\n");
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200267 return PTR_ERR(wdev->regmap);
Xiubo Lia7977002014-04-04 09:33:25 +0800268 }
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200269
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200270 wdev->clk = devm_clk_get(&pdev->dev, NULL);
271 if (IS_ERR(wdev->clk)) {
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200272 dev_err(&pdev->dev, "can't get Watchdog clock\n");
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200273 return PTR_ERR(wdev->clk);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200274 }
275
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200276 wdog = &wdev->wdog;
277 wdog->info = &imx2_wdt_info;
278 wdog->ops = &imx2_wdt_ops;
279 wdog->min_timeout = 1;
Marcus Folkesson2b77f002018-02-08 14:11:08 +0100280 wdog->timeout = IMX2_WDT_DEFAULT_TIME;
Guenter Roeck11d7aba2016-02-28 13:12:20 -0800281 wdog->max_hw_heartbeat_ms = IMX2_WDT_MAX_TIME * 1000;
Vladimir Zapolskiy81351932015-06-02 15:46:18 +0300282 wdog->parent = &pdev->dev;
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200283
Vladimir Zapolskiy39487f62016-10-07 15:41:39 +0300284 ret = platform_get_irq(pdev, 0);
285 if (ret > 0)
286 if (!devm_request_irq(&pdev->dev, ret, imx2_wdt_isr, 0,
287 dev_name(&pdev->dev), wdog))
288 wdog->info = &imx2_wdt_pretimeout_info;
289
Fabio Estevamaefb1632015-06-22 01:16:18 -0300290 ret = clk_prepare_enable(wdev->clk);
291 if (ret)
292 return ret;
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200293
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200294 regmap_read(wdev->regmap, IMX2_WDT_WRSR, &val);
295 wdog->bootstatus = val & IMX2_WDT_WRSR_TOUT ? WDIOF_CARDRESET : 0;
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200296
Tim Harveybc677ff42016-04-01 08:16:43 -0700297 wdev->ext_reset = of_property_read_bool(pdev->dev.of_node,
298 "fsl,ext-reset-output");
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200299 platform_set_drvdata(pdev, wdog);
300 watchdog_set_drvdata(wdog, wdev);
301 watchdog_set_nowayout(wdog, nowayout);
Damien Riegel2d9d24752015-11-16 12:28:04 -0500302 watchdog_set_restart_priority(wdog, 128);
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200303 watchdog_init_timeout(wdog, timeout, &pdev->dev);
304
Guenter Roeck11d7aba2016-02-28 13:12:20 -0800305 if (imx2_wdt_is_running(wdev)) {
306 imx2_wdt_set_timeout(wdog, wdog->timeout);
307 set_bit(WDOG_HW_RUNNING, &wdog->status);
308 }
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200309
Markus Pargmann5fe65ce2014-09-08 09:14:07 +0200310 /*
311 * Disable the watchdog power down counter at boot. Otherwise the power
312 * down counter will pull down the #WDOG interrupt line for one clock
313 * cycle.
314 */
315 regmap_write(wdev->regmap, IMX2_WDT_WMCR, 0);
316
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200317 ret = watchdog_register_device(wdog);
Wolfram Sang63c1cd52019-05-18 23:27:31 +0200318 if (ret)
Fabio Estevamdb11cba2015-06-22 01:16:19 -0300319 goto disable_clk;
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200320
321 dev_info(&pdev->dev, "timeout %d sec (nowayout=%d)\n",
322 wdog->timeout, nowayout);
323
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200324 return 0;
Fabio Estevamdb11cba2015-06-22 01:16:19 -0300325
326disable_clk:
327 clk_disable_unprepare(wdev->clk);
328 return ret;
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200329}
330
331static int __exit imx2_wdt_remove(struct platform_device *pdev)
332{
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200333 struct watchdog_device *wdog = platform_get_drvdata(pdev);
334 struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200335
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200336 watchdog_unregister_device(wdog);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200337
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200338 if (imx2_wdt_is_running(wdev)) {
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200339 imx2_wdt_ping(wdog);
340 dev_crit(&pdev->dev, "Device removed: Expect reboot!\n");
Jingoo Hanbdf49572013-04-29 18:15:53 +0900341 }
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200342 return 0;
343}
344
345static void imx2_wdt_shutdown(struct platform_device *pdev)
346{
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200347 struct watchdog_device *wdog = platform_get_drvdata(pdev);
348 struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200349
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200350 if (imx2_wdt_is_running(wdev)) {
351 /*
Guenter Roeck11d7aba2016-02-28 13:12:20 -0800352 * We are running, configure max timeout before reboot
353 * will take place.
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200354 */
Anatolij Gustschinfaad5de2014-04-11 08:57:14 +0200355 imx2_wdt_set_timeout(wdog, IMX2_WDT_MAX_TIME);
356 imx2_wdt_ping(wdog);
357 dev_crit(&pdev->dev, "Device shutdown: Expect reboot!\n");
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200358 }
359}
360
Xiubo Liaefbaf32014-09-22 18:00:52 +0800361#ifdef CONFIG_PM_SLEEP
Xiubo Libbd59002014-10-16 11:44:15 +0800362/* Disable watchdog if it is active or non-active but still running */
Xiubo Liaefbaf32014-09-22 18:00:52 +0800363static int imx2_wdt_suspend(struct device *dev)
364{
365 struct watchdog_device *wdog = dev_get_drvdata(dev);
366 struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
367
Xiubo Libbd59002014-10-16 11:44:15 +0800368 /* The watchdog IP block is running */
369 if (imx2_wdt_is_running(wdev)) {
Martin Kaiser0be26722018-01-01 18:26:47 +0100370 /*
371 * Don't update wdog->timeout, we'll restore the current value
372 * during resume.
373 */
374 __imx2_wdt_set_timeout(wdog, IMX2_WDT_MAX_TIME);
Xiubo Libbd59002014-10-16 11:44:15 +0800375 imx2_wdt_ping(wdog);
Xiubo Libbd59002014-10-16 11:44:15 +0800376 }
Xiubo Liaefbaf32014-09-22 18:00:52 +0800377
378 clk_disable_unprepare(wdev->clk);
379
380 return 0;
381}
382
383/* Enable watchdog and configure it if necessary */
384static int imx2_wdt_resume(struct device *dev)
385{
386 struct watchdog_device *wdog = dev_get_drvdata(dev);
387 struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
Fabio Estevamaefb1632015-06-22 01:16:18 -0300388 int ret;
Xiubo Liaefbaf32014-09-22 18:00:52 +0800389
Fabio Estevamaefb1632015-06-22 01:16:18 -0300390 ret = clk_prepare_enable(wdev->clk);
391 if (ret)
392 return ret;
Xiubo Liaefbaf32014-09-22 18:00:52 +0800393
394 if (watchdog_active(wdog) && !imx2_wdt_is_running(wdev)) {
Xiubo Libbd59002014-10-16 11:44:15 +0800395 /*
396 * If the watchdog is still active and resumes
397 * from deep sleep state, need to restart the
398 * watchdog again.
Xiubo Liaefbaf32014-09-22 18:00:52 +0800399 */
400 imx2_wdt_setup(wdog);
Guenter Roeck11d7aba2016-02-28 13:12:20 -0800401 }
402 if (imx2_wdt_is_running(wdev)) {
Xiubo Liaefbaf32014-09-22 18:00:52 +0800403 imx2_wdt_set_timeout(wdog, wdog->timeout);
404 imx2_wdt_ping(wdog);
Xiubo Liaefbaf32014-09-22 18:00:52 +0800405 }
406
407 return 0;
408}
409#endif
410
411static SIMPLE_DEV_PM_OPS(imx2_wdt_pm_ops, imx2_wdt_suspend,
412 imx2_wdt_resume);
413
Shawn Guof5a427e2011-07-18 11:15:21 +0800414static const struct of_device_id imx2_wdt_dt_ids[] = {
415 { .compatible = "fsl,imx21-wdt", },
416 { /* sentinel */ }
417};
Niels de Vos813296a2013-07-29 09:38:18 +0200418MODULE_DEVICE_TABLE(of, imx2_wdt_dt_ids);
Shawn Guof5a427e2011-07-18 11:15:21 +0800419
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200420static struct platform_driver imx2_wdt_driver = {
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200421 .remove = __exit_p(imx2_wdt_remove),
422 .shutdown = imx2_wdt_shutdown,
423 .driver = {
424 .name = DRIVER_NAME,
Xiubo Liaefbaf32014-09-22 18:00:52 +0800425 .pm = &imx2_wdt_pm_ops,
Shawn Guof5a427e2011-07-18 11:15:21 +0800426 .of_match_table = imx2_wdt_dt_ids,
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200427 },
428};
429
Fabio Porcedda1cb92042013-01-09 12:15:27 +0100430module_platform_driver_probe(imx2_wdt_driver, imx2_wdt_probe);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200431
432MODULE_AUTHOR("Wolfram Sang");
433MODULE_DESCRIPTION("Watchdog driver for IMX2 and later");
434MODULE_LICENSE("GPL v2");
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200435MODULE_ALIAS("platform:" DRIVER_NAME);