blob: e6eaba6bae5b487f39dc1474a4b931fe0a53a54c [file] [log] [blame]
Guenter Roeckd0173272019-06-20 09:28:46 -07001// SPDX-License-Identifier: GPL-2.0
Vladimir Barinov7d831bf2007-06-12 18:09:50 +04002/*
3 * drivers/char/watchdog/davinci_wdt.c
4 *
5 * Watchdog driver for DaVinci DM644x/DM646x processors
6 *
Ivan Khoronzhukf48f3ce2013-12-05 13:26:24 +02007 * Copyright (C) 2006-2013 Texas Instruments.
Vladimir Barinov7d831bf2007-06-12 18:09:50 +04008 *
Guenter Roeckd0173272019-06-20 09:28:46 -07009 * 2007 (c) MontaVista Software, Inc.
Vladimir Barinov7d831bf2007-06-12 18:09:50 +040010 */
11
12#include <linux/module.h>
13#include <linux/moduleparam.h>
Randy Dunlapac316722018-06-19 22:47:28 -070014#include <linux/mod_devicetable.h>
Vladimir Barinov7d831bf2007-06-12 18:09:50 +040015#include <linux/types.h>
16#include <linux/kernel.h>
Vladimir Barinov7d831bf2007-06-12 18:09:50 +040017#include <linux/watchdog.h>
Vladimir Barinov7d831bf2007-06-12 18:09:50 +040018#include <linux/platform_device.h>
Alan Coxf78b0a82008-05-19 14:05:30 +010019#include <linux/io.h>
Kevin Hilman371d3522009-01-29 14:14:30 -080020#include <linux/device.h>
Kevin Hilman9fd868f2009-02-10 20:30:37 -080021#include <linux/clk.h>
Sachin Kamat6330c702013-03-04 10:36:41 +053022#include <linux/err.h>
Vladimir Barinov7d831bf2007-06-12 18:09:50 +040023
24#define MODULE_NAME "DAVINCI-WDT: "
25
26#define DEFAULT_HEARTBEAT 60
27#define MAX_HEARTBEAT 600 /* really the max margin is 264/27MHz*/
28
29/* Timer register set definition */
30#define PID12 (0x0)
31#define EMUMGT (0x4)
32#define TIM12 (0x10)
33#define TIM34 (0x14)
34#define PRD12 (0x18)
35#define PRD34 (0x1C)
36#define TCR (0x20)
37#define TGCR (0x24)
38#define WDTCR (0x28)
39
40/* TCR bit definitions */
41#define ENAMODE12_DISABLED (0 << 6)
42#define ENAMODE12_ONESHOT (1 << 6)
43#define ENAMODE12_PERIODIC (2 << 6)
44
45/* TGCR bit definitions */
46#define TIM12RS_UNRESET (1 << 0)
47#define TIM34RS_UNRESET (1 << 1)
48#define TIMMODE_64BIT_WDOG (2 << 2)
49
50/* WDTCR bit definitions */
51#define WDEN (1 << 14)
52#define WDFLAG (1 << 15)
53#define WDKEY_SEQ0 (0xa5c6 << 16)
54#define WDKEY_SEQ1 (0xda7e << 16)
55
Ivan Khoronzhukf48f3ce2013-12-05 13:26:24 +020056static int heartbeat;
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +020057
58/*
59 * struct to hold data for each WDT device
60 * @base - base io address of WD device
61 * @clk - source clock of WDT
62 * @wdd - hold watchdog device as is in WDT core
63 */
64struct davinci_wdt_device {
65 void __iomem *base;
66 struct clk *clk;
67 struct watchdog_device wdd;
68};
Vladimir Barinov7d831bf2007-06-12 18:09:50 +040069
Ivan Khoronzhukf48f3ce2013-12-05 13:26:24 +020070static int davinci_wdt_start(struct watchdog_device *wdd)
Vladimir Barinov7d831bf2007-06-12 18:09:50 +040071{
72 u32 tgcr;
73 u32 timer_margin;
Kevin Hilman9fd868f2009-02-10 20:30:37 -080074 unsigned long wdt_freq;
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +020075 struct davinci_wdt_device *davinci_wdt = watchdog_get_drvdata(wdd);
Kevin Hilman9fd868f2009-02-10 20:30:37 -080076
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +020077 wdt_freq = clk_get_rate(davinci_wdt->clk);
Vladimir Barinov7d831bf2007-06-12 18:09:50 +040078
Vladimir Barinov7d831bf2007-06-12 18:09:50 +040079 /* disable, internal clock source */
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +020080 iowrite32(0, davinci_wdt->base + TCR);
Vladimir Barinov7d831bf2007-06-12 18:09:50 +040081 /* reset timer, set mode to 64-bit watchdog, and unreset */
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +020082 iowrite32(0, davinci_wdt->base + TGCR);
Vladimir Barinov7d831bf2007-06-12 18:09:50 +040083 tgcr = TIMMODE_64BIT_WDOG | TIM12RS_UNRESET | TIM34RS_UNRESET;
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +020084 iowrite32(tgcr, davinci_wdt->base + TGCR);
Vladimir Barinov7d831bf2007-06-12 18:09:50 +040085 /* clear counter regs */
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +020086 iowrite32(0, davinci_wdt->base + TIM12);
87 iowrite32(0, davinci_wdt->base + TIM34);
Vladimir Barinov7d831bf2007-06-12 18:09:50 +040088 /* set timeout period */
Ivan Khoronzhukf48f3ce2013-12-05 13:26:24 +020089 timer_margin = (((u64)wdd->timeout * wdt_freq) & 0xffffffff);
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +020090 iowrite32(timer_margin, davinci_wdt->base + PRD12);
Ivan Khoronzhukf48f3ce2013-12-05 13:26:24 +020091 timer_margin = (((u64)wdd->timeout * wdt_freq) >> 32);
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +020092 iowrite32(timer_margin, davinci_wdt->base + PRD34);
Vladimir Barinov7d831bf2007-06-12 18:09:50 +040093 /* enable run continuously */
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +020094 iowrite32(ENAMODE12_PERIODIC, davinci_wdt->base + TCR);
Vladimir Barinov7d831bf2007-06-12 18:09:50 +040095 /* Once the WDT is in pre-active state write to
96 * TIM12, TIM34, PRD12, PRD34, TCR, TGCR, WDTCR are
97 * write protected (except for the WDKEY field)
98 */
99 /* put watchdog in pre-active state */
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +0200100 iowrite32(WDKEY_SEQ0 | WDEN, davinci_wdt->base + WDTCR);
Vladimir Barinov7d831bf2007-06-12 18:09:50 +0400101 /* put watchdog in active state */
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +0200102 iowrite32(WDKEY_SEQ1 | WDEN, davinci_wdt->base + WDTCR);
Ivan Khoronzhukf48f3ce2013-12-05 13:26:24 +0200103 return 0;
Vladimir Barinov7d831bf2007-06-12 18:09:50 +0400104}
105
Ivan Khoronzhukf48f3ce2013-12-05 13:26:24 +0200106static int davinci_wdt_ping(struct watchdog_device *wdd)
Vladimir Barinov7d831bf2007-06-12 18:09:50 +0400107{
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +0200108 struct davinci_wdt_device *davinci_wdt = watchdog_get_drvdata(wdd);
109
Ivan Khoronzhukf48f3ce2013-12-05 13:26:24 +0200110 /* put watchdog in service state */
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +0200111 iowrite32(WDKEY_SEQ0, davinci_wdt->base + WDTCR);
Ivan Khoronzhukf48f3ce2013-12-05 13:26:24 +0200112 /* put watchdog in active state */
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +0200113 iowrite32(WDKEY_SEQ1, davinci_wdt->base + WDTCR);
Ivan Khoronzhukf48f3ce2013-12-05 13:26:24 +0200114 return 0;
Vladimir Barinov7d831bf2007-06-12 18:09:50 +0400115}
116
Ivan Khoronzhuka7719942013-12-04 21:39:28 +0200117static unsigned int davinci_wdt_get_timeleft(struct watchdog_device *wdd)
118{
119 u64 timer_counter;
120 unsigned long freq;
121 u32 val;
122 struct davinci_wdt_device *davinci_wdt = watchdog_get_drvdata(wdd);
123
124 /* if timeout has occured then return 0 */
125 val = ioread32(davinci_wdt->base + WDTCR);
126 if (val & WDFLAG)
127 return 0;
128
129 freq = clk_get_rate(davinci_wdt->clk);
130
131 if (!freq)
132 return 0;
133
134 timer_counter = ioread32(davinci_wdt->base + TIM12);
135 timer_counter |= ((u64)ioread32(davinci_wdt->base + TIM34) << 32);
136
137 do_div(timer_counter, freq);
138
139 return wdd->timeout - timer_counter;
140}
141
David Lechner71d1f052017-12-11 11:21:08 -0600142static int davinci_wdt_restart(struct watchdog_device *wdd,
143 unsigned long action, void *data)
144{
145 struct davinci_wdt_device *davinci_wdt = watchdog_get_drvdata(wdd);
146 u32 tgcr, wdtcr;
147
148 /* disable, internal clock source */
149 iowrite32(0, davinci_wdt->base + TCR);
150
151 /* reset timer, set mode to 64-bit watchdog, and unreset */
152 tgcr = 0;
153 iowrite32(tgcr, davinci_wdt->base + TGCR);
154 tgcr = TIMMODE_64BIT_WDOG | TIM12RS_UNRESET | TIM34RS_UNRESET;
155 iowrite32(tgcr, davinci_wdt->base + TGCR);
156
157 /* clear counter and period regs */
158 iowrite32(0, davinci_wdt->base + TIM12);
159 iowrite32(0, davinci_wdt->base + TIM34);
160 iowrite32(0, davinci_wdt->base + PRD12);
161 iowrite32(0, davinci_wdt->base + PRD34);
162
163 /* put watchdog in pre-active state */
164 wdtcr = WDKEY_SEQ0 | WDEN;
165 iowrite32(wdtcr, davinci_wdt->base + WDTCR);
166
167 /* put watchdog in active state */
168 wdtcr = WDKEY_SEQ1 | WDEN;
169 iowrite32(wdtcr, davinci_wdt->base + WDTCR);
170
171 /* write an invalid value to the WDKEY field to trigger a restart */
172 wdtcr = 0x00004000;
173 iowrite32(wdtcr, davinci_wdt->base + WDTCR);
174
175 return 0;
176}
177
Ivan Khoronzhukf48f3ce2013-12-05 13:26:24 +0200178static const struct watchdog_info davinci_wdt_info = {
Wim Van Sebroeckf1a08cc2007-07-20 21:47:55 +0000179 .options = WDIOF_KEEPALIVEPING,
Ivan Khoronzhuk8832b202013-12-04 21:39:30 +0200180 .identity = "DaVinci/Keystone Watchdog",
Vladimir Barinov7d831bf2007-06-12 18:09:50 +0400181};
182
Ivan Khoronzhukf48f3ce2013-12-05 13:26:24 +0200183static const struct watchdog_ops davinci_wdt_ops = {
184 .owner = THIS_MODULE,
185 .start = davinci_wdt_start,
186 .stop = davinci_wdt_ping,
187 .ping = davinci_wdt_ping,
Ivan Khoronzhuka7719942013-12-04 21:39:28 +0200188 .get_timeleft = davinci_wdt_get_timeleft,
David Lechner71d1f052017-12-11 11:21:08 -0600189 .restart = davinci_wdt_restart,
Vladimir Barinov7d831bf2007-06-12 18:09:50 +0400190};
191
Guenter Roeckcecda012019-04-08 12:38:37 -0700192static void davinci_clk_disable_unprepare(void *data)
193{
194 clk_disable_unprepare(data);
195}
196
Bill Pemberton2d991a12012-11-19 13:21:41 -0500197static int davinci_wdt_probe(struct platform_device *pdev)
Vladimir Barinov7d831bf2007-06-12 18:09:50 +0400198{
Kumar, Anile20880e2013-02-08 13:09:30 +0530199 int ret = 0;
Kevin Hilman371d3522009-01-29 14:14:30 -0800200 struct device *dev = &pdev->dev;
Ivan Khoronzhukf48f3ce2013-12-05 13:26:24 +0200201 struct watchdog_device *wdd;
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +0200202 struct davinci_wdt_device *davinci_wdt;
Vladimir Barinov7d831bf2007-06-12 18:09:50 +0400203
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +0200204 davinci_wdt = devm_kzalloc(dev, sizeof(*davinci_wdt), GFP_KERNEL);
205 if (!davinci_wdt)
206 return -ENOMEM;
Kevin Hilman9fd868f2009-02-10 20:30:37 -0800207
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +0200208 davinci_wdt->clk = devm_clk_get(dev, NULL);
Krzysztof Kozlowskifc772042020-09-01 17:31:40 +0200209 if (IS_ERR(davinci_wdt->clk))
210 return dev_err_probe(dev, PTR_ERR(davinci_wdt->clk),
211 "failed to get clock node\n");
Kevin Hilman9fd868f2009-02-10 20:30:37 -0800212
Arvind Yadav8f11eb52017-06-06 15:47:53 +0530213 ret = clk_prepare_enable(davinci_wdt->clk);
214 if (ret) {
Guenter Roeckcecda012019-04-08 12:38:37 -0700215 dev_err(dev, "failed to prepare clock\n");
Arvind Yadav8f11eb52017-06-06 15:47:53 +0530216 return ret;
217 }
Guenter Roeckcecda012019-04-08 12:38:37 -0700218 ret = devm_add_action_or_reset(dev, davinci_clk_disable_unprepare,
219 davinci_wdt->clk);
220 if (ret)
221 return ret;
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +0200222
223 platform_set_drvdata(pdev, davinci_wdt);
224
225 wdd = &davinci_wdt->wdd;
Ivan Khoronzhukf48f3ce2013-12-05 13:26:24 +0200226 wdd->info = &davinci_wdt_info;
227 wdd->ops = &davinci_wdt_ops;
228 wdd->min_timeout = 1;
229 wdd->max_timeout = MAX_HEARTBEAT;
230 wdd->timeout = DEFAULT_HEARTBEAT;
Guenter Roeckcecda012019-04-08 12:38:37 -0700231 wdd->parent = dev;
Vladimir Barinov7d831bf2007-06-12 18:09:50 +0400232
Ivan Khoronzhukf48f3ce2013-12-05 13:26:24 +0200233 watchdog_init_timeout(wdd, heartbeat, dev);
234
235 dev_info(dev, "heartbeat %d sec\n", wdd->timeout);
236
Ivan Khoronzhuk6d9a6cf2013-12-04 21:39:27 +0200237 watchdog_set_drvdata(wdd, davinci_wdt);
Ivan Khoronzhukf48f3ce2013-12-05 13:26:24 +0200238 watchdog_set_nowayout(wdd, 1);
David Lechner71d1f052017-12-11 11:21:08 -0600239 watchdog_set_restart_priority(wdd, 128);
Vladimir Barinov7d831bf2007-06-12 18:09:50 +0400240
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700241 davinci_wdt->base = devm_platform_ioremap_resource(pdev, 0);
Guenter Roeckcecda012019-04-08 12:38:37 -0700242 if (IS_ERR(davinci_wdt->base))
243 return PTR_ERR(davinci_wdt->base);
Vladimir Barinov7d831bf2007-06-12 18:09:50 +0400244
Wolfram Sang6ab6d332019-05-18 23:27:25 +0200245 return devm_watchdog_register_device(dev, wdd);
Vladimir Barinov7d831bf2007-06-12 18:09:50 +0400246}
247
Murali Karicheri902e2e72012-11-26 16:41:35 -0500248static const struct of_device_id davinci_wdt_of_match[] = {
249 { .compatible = "ti,davinci-wdt", },
250 {},
251};
252MODULE_DEVICE_TABLE(of, davinci_wdt_of_match);
253
Vladimir Barinov7d831bf2007-06-12 18:09:50 +0400254static struct platform_driver platform_wdt_driver = {
255 .driver = {
Ivan Khoronzhuk84374812013-11-27 15:31:53 +0200256 .name = "davinci-wdt",
Murali Karicheri902e2e72012-11-26 16:41:35 -0500257 .of_match_table = davinci_wdt_of_match,
Vladimir Barinov7d831bf2007-06-12 18:09:50 +0400258 },
259 .probe = davinci_wdt_probe,
Vladimir Barinov7d831bf2007-06-12 18:09:50 +0400260};
261
Axel Linb8ec6112011-11-29 13:56:27 +0800262module_platform_driver(platform_wdt_driver);
Vladimir Barinov7d831bf2007-06-12 18:09:50 +0400263
264MODULE_AUTHOR("Texas Instruments");
265MODULE_DESCRIPTION("DaVinci Watchdog Driver");
266
267module_param(heartbeat, int, 0);
268MODULE_PARM_DESC(heartbeat,
269 "Watchdog heartbeat period in seconds from 1 to "
270 __MODULE_STRING(MAX_HEARTBEAT) ", default "
271 __MODULE_STRING(DEFAULT_HEARTBEAT));
272
273MODULE_LICENSE("GPL");
Ivan Khoronzhuk84374812013-11-27 15:31:53 +0200274MODULE_ALIAS("platform:davinci-wdt");