2 * Imagination Technologies PowerDown Controller Watchdog Timer.
4 * Copyright (c) 2014 Imagination Technologies Ltd.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * Based on drivers/watchdog/sunxi_wdt.c Copyright (c) 2013 Carlo Caione
11 * 2012 Henrik Nordstrom
15 * The timeout value is rounded to the next power of two clock cycles.
16 * This is configured using the PDC_WDT_CONFIG register, according to this
19 * timeout = 2^(delay + 1) clock cycles
21 * Where 'delay' is the value written in PDC_WDT_CONFIG register.
23 * Therefore, the hardware only allows to program watchdog timeouts, expressed
24 * as a power of two number of watchdog clock cycles. The current implementation
25 * guarantees that the actual watchdog timeout will be _at least_ the value
26 * programmed in the imgpdg_wdt driver.
28 * The following table shows how the user-configured timeout relates
29 * to the actual hardware timeout (watchdog clock @ 40000 Hz):
31 * input timeout | WD_DELAY | actual timeout
32 * -----------------------------------
33 * 10 | 18 | 13 seconds
34 * 20 | 19 | 26 seconds
35 * 30 | 20 | 52 seconds
36 * 60 | 21 | 104 seconds
38 * Albeit coarse, this granularity would suffice most watchdog uses.
39 * If the platform allows it, the user should be able to change the watchdog
40 * clock rate and achieve a finer timeout granularity.
43 #include <linux/clk.h>
45 #include <linux/log2.h>
46 #include <linux/module.h>
47 #include <linux/platform_device.h>
48 #include <linux/reboot.h>
49 #include <linux/slab.h>
50 #include <linux/watchdog.h>
53 #define PDC_WDT_SOFT_RESET 0x00
54 #define PDC_WDT_CONFIG 0x04
55 #define PDC_WDT_CONFIG_ENABLE BIT(31)
56 #define PDC_WDT_CONFIG_DELAY_MASK 0x1f
58 #define PDC_WDT_TICKLE1 0x08
59 #define PDC_WDT_TICKLE1_MAGIC 0xabcd1234
60 #define PDC_WDT_TICKLE2 0x0c
61 #define PDC_WDT_TICKLE2_MAGIC 0x4321dcba
63 #define PDC_WDT_TICKLE_STATUS_MASK 0x7
64 #define PDC_WDT_TICKLE_STATUS_SHIFT 0
65 #define PDC_WDT_TICKLE_STATUS_HRESET 0x0 /* Hard reset */
66 #define PDC_WDT_TICKLE_STATUS_TIMEOUT 0x1 /* Timeout */
67 #define PDC_WDT_TICKLE_STATUS_TICKLE 0x2 /* Tickled incorrectly */
68 #define PDC_WDT_TICKLE_STATUS_SRESET 0x3 /* Soft reset */
69 #define PDC_WDT_TICKLE_STATUS_USER 0x4 /* User reset */
71 /* Timeout values are in seconds */
72 #define PDC_WDT_MIN_TIMEOUT 1
73 #define PDC_WDT_DEF_TIMEOUT 64
76 module_param(heartbeat
, int, 0);
77 MODULE_PARM_DESC(heartbeat
, "Watchdog heartbeats in seconds "
78 "(default=" __MODULE_STRING(PDC_WDT_DEF_TIMEOUT
) ")");
80 static bool nowayout
= WATCHDOG_NOWAYOUT
;
81 module_param(nowayout
, bool, 0);
82 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started "
83 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
86 struct watchdog_device wdt_dev
;
90 struct notifier_block restart_handler
;
93 static int pdc_wdt_keepalive(struct watchdog_device
*wdt_dev
)
95 struct pdc_wdt_dev
*wdt
= watchdog_get_drvdata(wdt_dev
);
97 writel(PDC_WDT_TICKLE1_MAGIC
, wdt
->base
+ PDC_WDT_TICKLE1
);
98 writel(PDC_WDT_TICKLE2_MAGIC
, wdt
->base
+ PDC_WDT_TICKLE2
);
103 static int pdc_wdt_stop(struct watchdog_device
*wdt_dev
)
106 struct pdc_wdt_dev
*wdt
= watchdog_get_drvdata(wdt_dev
);
108 val
= readl(wdt
->base
+ PDC_WDT_CONFIG
);
109 val
&= ~PDC_WDT_CONFIG_ENABLE
;
110 writel(val
, wdt
->base
+ PDC_WDT_CONFIG
);
112 /* Must tickle to finish the stop */
113 pdc_wdt_keepalive(wdt_dev
);
118 static void __pdc_wdt_set_timeout(struct pdc_wdt_dev
*wdt
)
120 unsigned long clk_rate
= clk_get_rate(wdt
->wdt_clk
);
123 val
= readl(wdt
->base
+ PDC_WDT_CONFIG
) & ~PDC_WDT_CONFIG_DELAY_MASK
;
124 val
|= order_base_2(wdt
->wdt_dev
.timeout
* clk_rate
) - 1;
125 writel(val
, wdt
->base
+ PDC_WDT_CONFIG
);
128 static int pdc_wdt_set_timeout(struct watchdog_device
*wdt_dev
,
129 unsigned int new_timeout
)
131 struct pdc_wdt_dev
*wdt
= watchdog_get_drvdata(wdt_dev
);
133 wdt
->wdt_dev
.timeout
= new_timeout
;
135 __pdc_wdt_set_timeout(wdt
);
140 /* Start the watchdog timer (delay should already be set) */
141 static int pdc_wdt_start(struct watchdog_device
*wdt_dev
)
144 struct pdc_wdt_dev
*wdt
= watchdog_get_drvdata(wdt_dev
);
146 __pdc_wdt_set_timeout(wdt
);
148 val
= readl(wdt
->base
+ PDC_WDT_CONFIG
);
149 val
|= PDC_WDT_CONFIG_ENABLE
;
150 writel(val
, wdt
->base
+ PDC_WDT_CONFIG
);
155 static struct watchdog_info pdc_wdt_info
= {
156 .identity
= "IMG PDC Watchdog",
157 .options
= WDIOF_SETTIMEOUT
|
158 WDIOF_KEEPALIVEPING
|
162 static const struct watchdog_ops pdc_wdt_ops
= {
163 .owner
= THIS_MODULE
,
164 .start
= pdc_wdt_start
,
165 .stop
= pdc_wdt_stop
,
166 .ping
= pdc_wdt_keepalive
,
167 .set_timeout
= pdc_wdt_set_timeout
,
170 static int pdc_wdt_restart(struct notifier_block
*this, unsigned long mode
,
173 struct pdc_wdt_dev
*wdt
= container_of(this, struct pdc_wdt_dev
,
176 /* Assert SOFT_RESET */
177 writel(0x1, wdt
->base
+ PDC_WDT_SOFT_RESET
);
182 static int pdc_wdt_probe(struct platform_device
*pdev
)
186 unsigned long clk_rate
;
187 struct resource
*res
;
188 struct pdc_wdt_dev
*pdc_wdt
;
190 pdc_wdt
= devm_kzalloc(&pdev
->dev
, sizeof(*pdc_wdt
), GFP_KERNEL
);
194 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
195 pdc_wdt
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
196 if (IS_ERR(pdc_wdt
->base
))
197 return PTR_ERR(pdc_wdt
->base
);
199 pdc_wdt
->sys_clk
= devm_clk_get(&pdev
->dev
, "sys");
200 if (IS_ERR(pdc_wdt
->sys_clk
)) {
201 dev_err(&pdev
->dev
, "failed to get the sys clock\n");
202 return PTR_ERR(pdc_wdt
->sys_clk
);
205 pdc_wdt
->wdt_clk
= devm_clk_get(&pdev
->dev
, "wdt");
206 if (IS_ERR(pdc_wdt
->wdt_clk
)) {
207 dev_err(&pdev
->dev
, "failed to get the wdt clock\n");
208 return PTR_ERR(pdc_wdt
->wdt_clk
);
211 ret
= clk_prepare_enable(pdc_wdt
->sys_clk
);
213 dev_err(&pdev
->dev
, "could not prepare or enable sys clock\n");
217 ret
= clk_prepare_enable(pdc_wdt
->wdt_clk
);
219 dev_err(&pdev
->dev
, "could not prepare or enable wdt clock\n");
220 goto disable_sys_clk
;
223 /* We use the clock rate to calculate the max timeout */
224 clk_rate
= clk_get_rate(pdc_wdt
->wdt_clk
);
226 dev_err(&pdev
->dev
, "failed to get clock rate\n");
228 goto disable_wdt_clk
;
231 if (order_base_2(clk_rate
) > PDC_WDT_CONFIG_DELAY_MASK
+ 1) {
232 dev_err(&pdev
->dev
, "invalid clock rate\n");
234 goto disable_wdt_clk
;
237 if (order_base_2(clk_rate
) == 0)
238 pdc_wdt
->wdt_dev
.min_timeout
= PDC_WDT_MIN_TIMEOUT
+ 1;
240 pdc_wdt
->wdt_dev
.min_timeout
= PDC_WDT_MIN_TIMEOUT
;
242 pdc_wdt
->wdt_dev
.info
= &pdc_wdt_info
;
243 pdc_wdt
->wdt_dev
.ops
= &pdc_wdt_ops
;
245 div
= 1ULL << (PDC_WDT_CONFIG_DELAY_MASK
+ 1);
246 do_div(div
, clk_rate
);
247 pdc_wdt
->wdt_dev
.max_timeout
= div
;
248 pdc_wdt
->wdt_dev
.timeout
= PDC_WDT_DEF_TIMEOUT
;
249 pdc_wdt
->wdt_dev
.parent
= &pdev
->dev
;
250 watchdog_set_drvdata(&pdc_wdt
->wdt_dev
, pdc_wdt
);
252 watchdog_init_timeout(&pdc_wdt
->wdt_dev
, heartbeat
, &pdev
->dev
);
254 pdc_wdt_stop(&pdc_wdt
->wdt_dev
);
256 /* Find what caused the last reset */
257 val
= readl(pdc_wdt
->base
+ PDC_WDT_TICKLE1
);
258 val
= (val
& PDC_WDT_TICKLE_STATUS_MASK
) >> PDC_WDT_TICKLE_STATUS_SHIFT
;
260 case PDC_WDT_TICKLE_STATUS_TICKLE
:
261 case PDC_WDT_TICKLE_STATUS_TIMEOUT
:
262 pdc_wdt
->wdt_dev
.bootstatus
|= WDIOF_CARDRESET
;
264 "watchdog module last reset due to timeout\n");
266 case PDC_WDT_TICKLE_STATUS_HRESET
:
268 "watchdog module last reset due to hard reset\n");
270 case PDC_WDT_TICKLE_STATUS_SRESET
:
272 "watchdog module last reset due to soft reset\n");
274 case PDC_WDT_TICKLE_STATUS_USER
:
276 "watchdog module last reset due to user reset\n");
280 "contains an illegal status code (%08x)\n", val
);
284 watchdog_set_nowayout(&pdc_wdt
->wdt_dev
, nowayout
);
286 platform_set_drvdata(pdev
, pdc_wdt
);
288 ret
= watchdog_register_device(&pdc_wdt
->wdt_dev
);
290 goto disable_wdt_clk
;
292 pdc_wdt
->restart_handler
.notifier_call
= pdc_wdt_restart
;
293 pdc_wdt
->restart_handler
.priority
= 128;
294 ret
= register_restart_handler(&pdc_wdt
->restart_handler
);
296 dev_warn(&pdev
->dev
, "failed to register restart handler: %d\n",
302 clk_disable_unprepare(pdc_wdt
->wdt_clk
);
304 clk_disable_unprepare(pdc_wdt
->sys_clk
);
308 static void pdc_wdt_shutdown(struct platform_device
*pdev
)
310 struct pdc_wdt_dev
*pdc_wdt
= platform_get_drvdata(pdev
);
312 pdc_wdt_stop(&pdc_wdt
->wdt_dev
);
315 static int pdc_wdt_remove(struct platform_device
*pdev
)
317 struct pdc_wdt_dev
*pdc_wdt
= platform_get_drvdata(pdev
);
319 pdc_wdt_stop(&pdc_wdt
->wdt_dev
);
320 watchdog_unregister_device(&pdc_wdt
->wdt_dev
);
321 clk_disable_unprepare(pdc_wdt
->wdt_clk
);
322 clk_disable_unprepare(pdc_wdt
->sys_clk
);
327 static const struct of_device_id pdc_wdt_match
[] = {
328 { .compatible
= "img,pdc-wdt" },
331 MODULE_DEVICE_TABLE(of
, pdc_wdt_match
);
333 static struct platform_driver pdc_wdt_driver
= {
335 .name
= "imgpdc-wdt",
336 .of_match_table
= pdc_wdt_match
,
338 .probe
= pdc_wdt_probe
,
339 .remove
= pdc_wdt_remove
,
340 .shutdown
= pdc_wdt_shutdown
,
342 module_platform_driver(pdc_wdt_driver
);
344 MODULE_AUTHOR("Jude Abraham <Jude.Abraham@imgtec.com>");
345 MODULE_AUTHOR("Naidu Tellapati <Naidu.Tellapati@imgtec.com>");
346 MODULE_DESCRIPTION("Imagination Technologies PDC Watchdog Timer Driver");
347 MODULE_LICENSE("GPL v2");