blob: c2f14d2bd6957c4c1cc476d87747713c75aede83 [file] [log] [blame]
John Crispin2f58b8d2011-05-05 23:00:23 +02001/*
2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License version 2 as published
4 * by the Free Software Foundation.
5 *
John Crispinf3519a62016-12-20 19:56:59 +01006 * Copyright (C) 2010 John Crispin <john@phrozen.org>
Hauke Mehrtens710322b2017-08-20 00:18:11 +02007 * Copyright (C) 2017 Hauke Mehrtens <hauke@hauke-m.de>
John Crispin2f58b8d2011-05-05 23:00:23 +02008 * Based on EP93xx wdt driver
9 */
10
11#include <linux/module.h>
Hauke Mehrtens1f59f8a2018-09-13 23:32:09 +020012#include <linux/bitops.h>
John Crispin2f58b8d2011-05-05 23:00:23 +020013#include <linux/watchdog.h>
John Crispincdb86122012-04-12 21:21:56 +020014#include <linux/of_platform.h>
John Crispin2f58b8d2011-05-05 23:00:23 +020015#include <linux/uaccess.h>
16#include <linux/clk.h>
17#include <linux/io.h>
Hauke Mehrtens710322b2017-08-20 00:18:11 +020018#include <linux/regmap.h>
19#include <linux/mfd/syscon.h>
John Crispin2f58b8d2011-05-05 23:00:23 +020020
John Crispincdb86122012-04-12 21:21:56 +020021#include <lantiq_soc.h>
John Crispin2f58b8d2011-05-05 23:00:23 +020022
Hauke Mehrtens710322b2017-08-20 00:18:11 +020023#define LTQ_XRX_RCU_RST_STAT 0x0014
24#define LTQ_XRX_RCU_RST_STAT_WDT BIT(31)
25
26/* CPU0 Reset Source Register */
27#define LTQ_FALCON_SYS1_CPU0RS 0x0060
28/* reset cause mask */
29#define LTQ_FALCON_SYS1_CPU0RS_MASK 0x0007
30#define LTQ_FALCON_SYS1_CPU0RS_WDT 0x02
31
John Crispincdb86122012-04-12 21:21:56 +020032/*
33 * Section 3.4 of the datasheet
John Crispin2f58b8d2011-05-05 23:00:23 +020034 * The password sequence protects the WDT control register from unintended
35 * write actions, which might cause malfunction of the WDT.
36 *
37 * essentially the following two magic passwords need to be written to allow
38 * IO access to the WDT core
39 */
Hauke Mehrtens1f59f8a2018-09-13 23:32:09 +020040#define LTQ_WDT_CR_PW1 0x00BE0000
41#define LTQ_WDT_CR_PW2 0x00DC0000
John Crispin2f58b8d2011-05-05 23:00:23 +020042
Hauke Mehrtens1f59f8a2018-09-13 23:32:09 +020043#define LTQ_WDT_CR 0x0 /* watchdog control register */
44#define LTQ_WDT_CR_GEN BIT(31) /* enable bit */
45/* Pre-warning limit set to 1/16 of max WDT period */
46#define LTQ_WDT_CR_PWL (0x3 << 26)
47/* set clock divider to 0x40000 */
48#define LTQ_WDT_CR_CLKDIV (0x3 << 24)
49#define LTQ_WDT_CR_PW_MASK GENMASK(23, 16) /* Password field */
50#define LTQ_WDT_CR_MAX_TIMEOUT ((1 << 16) - 1) /* The reload field is 16 bit */
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020051#define LTQ_WDT_SR 0x8 /* watchdog status register */
52#define LTQ_WDT_SR_EN BIT(31) /* Enable */
John Crispin2f58b8d2011-05-05 23:00:23 +020053
John Crispin2f58b8d2011-05-05 23:00:23 +020054#define LTQ_WDT_DIVIDER 0x40000
John Crispin2f58b8d2011-05-05 23:00:23 +020055
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010056static bool nowayout = WATCHDOG_NOWAYOUT;
John Crispin2f58b8d2011-05-05 23:00:23 +020057
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020058struct ltq_wdt_hw {
59 int (*bootstatus_get)(struct device *dev);
60};
John Crispin2f58b8d2011-05-05 23:00:23 +020061
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020062struct ltq_wdt_priv {
63 struct watchdog_device wdt;
64 void __iomem *membase;
65 unsigned long clk_rate;
66};
John Crispin2f58b8d2011-05-05 23:00:23 +020067
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020068static u32 ltq_wdt_r32(struct ltq_wdt_priv *priv, u32 offset)
John Crispin2f58b8d2011-05-05 23:00:23 +020069{
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020070 return __raw_readl(priv->membase + offset);
John Crispin2f58b8d2011-05-05 23:00:23 +020071}
72
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020073static void ltq_wdt_w32(struct ltq_wdt_priv *priv, u32 val, u32 offset)
John Crispin2f58b8d2011-05-05 23:00:23 +020074{
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020075 __raw_writel(val, priv->membase + offset);
John Crispin2f58b8d2011-05-05 23:00:23 +020076}
77
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020078static void ltq_wdt_mask(struct ltq_wdt_priv *priv, u32 clear, u32 set,
79 u32 offset)
John Crispin2f58b8d2011-05-05 23:00:23 +020080{
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020081 u32 val = ltq_wdt_r32(priv, offset);
John Crispin2f58b8d2011-05-05 23:00:23 +020082
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020083 val &= ~(clear);
84 val |= set;
85 ltq_wdt_w32(priv, val, offset);
John Crispin2f58b8d2011-05-05 23:00:23 +020086}
87
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020088static struct ltq_wdt_priv *ltq_wdt_get_priv(struct watchdog_device *wdt)
89{
90 return container_of(wdt, struct ltq_wdt_priv, wdt);
91}
92
93static struct watchdog_info ltq_wdt_info = {
John Crispin2f58b8d2011-05-05 23:00:23 +020094 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020095 WDIOF_CARDRESET,
John Crispin2f58b8d2011-05-05 23:00:23 +020096 .identity = "ltq_wdt",
97};
98
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020099static int ltq_wdt_start(struct watchdog_device *wdt)
John Crispin2f58b8d2011-05-05 23:00:23 +0200100{
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200101 struct ltq_wdt_priv *priv = ltq_wdt_get_priv(wdt);
102 u32 timeout;
John Crispin2f58b8d2011-05-05 23:00:23 +0200103
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200104 timeout = wdt->timeout * priv->clk_rate;
John Crispin2f58b8d2011-05-05 23:00:23 +0200105
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200106 ltq_wdt_mask(priv, LTQ_WDT_CR_PW_MASK, LTQ_WDT_CR_PW1, LTQ_WDT_CR);
107 /* write the second magic plus the configuration and new timeout */
108 ltq_wdt_mask(priv, LTQ_WDT_CR_PW_MASK | LTQ_WDT_CR_MAX_TIMEOUT,
109 LTQ_WDT_CR_GEN | LTQ_WDT_CR_PWL | LTQ_WDT_CR_CLKDIV |
110 LTQ_WDT_CR_PW2 | timeout,
111 LTQ_WDT_CR);
John Crispin2f58b8d2011-05-05 23:00:23 +0200112
113 return 0;
114}
115
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200116static int ltq_wdt_stop(struct watchdog_device *wdt)
Hauke Mehrtens710322b2017-08-20 00:18:11 +0200117{
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200118 struct ltq_wdt_priv *priv = ltq_wdt_get_priv(wdt);
119
120 ltq_wdt_mask(priv, LTQ_WDT_CR_PW_MASK, LTQ_WDT_CR_PW1, LTQ_WDT_CR);
121 ltq_wdt_mask(priv, LTQ_WDT_CR_GEN | LTQ_WDT_CR_PW_MASK,
122 LTQ_WDT_CR_PW2, LTQ_WDT_CR);
123
124 return 0;
125}
126
127static int ltq_wdt_ping(struct watchdog_device *wdt)
128{
129 struct ltq_wdt_priv *priv = ltq_wdt_get_priv(wdt);
130 u32 timeout;
131
132 timeout = wdt->timeout * priv->clk_rate;
133
134 ltq_wdt_mask(priv, LTQ_WDT_CR_PW_MASK, LTQ_WDT_CR_PW1, LTQ_WDT_CR);
135 /* write the second magic plus the configuration and new timeout */
136 ltq_wdt_mask(priv, LTQ_WDT_CR_PW_MASK | LTQ_WDT_CR_MAX_TIMEOUT,
137 LTQ_WDT_CR_PW2 | timeout, LTQ_WDT_CR);
138
139 return 0;
140}
141
142static const struct watchdog_ops ltq_wdt_ops = {
143 .owner = THIS_MODULE,
144 .start = ltq_wdt_start,
145 .stop = ltq_wdt_stop,
146 .ping = ltq_wdt_ping,
147};
148
149static int ltq_wdt_xrx_bootstatus_get(struct device *dev)
150{
Hauke Mehrtens710322b2017-08-20 00:18:11 +0200151 struct regmap *rcu_regmap;
152 u32 val;
153 int err;
154
155 rcu_regmap = syscon_regmap_lookup_by_phandle(dev->of_node, "regmap");
156 if (IS_ERR(rcu_regmap))
157 return PTR_ERR(rcu_regmap);
158
159 err = regmap_read(rcu_regmap, LTQ_XRX_RCU_RST_STAT, &val);
160 if (err)
161 return err;
162
163 if (val & LTQ_XRX_RCU_RST_STAT_WDT)
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200164 return WDIOF_CARDRESET;
Hauke Mehrtens710322b2017-08-20 00:18:11 +0200165
166 return 0;
167}
168
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200169static int ltq_wdt_falcon_bootstatus_get(struct device *dev)
Hauke Mehrtens710322b2017-08-20 00:18:11 +0200170{
Hauke Mehrtens710322b2017-08-20 00:18:11 +0200171 struct regmap *rcu_regmap;
172 u32 val;
173 int err;
174
175 rcu_regmap = syscon_regmap_lookup_by_phandle(dev->of_node,
176 "lantiq,rcu");
177 if (IS_ERR(rcu_regmap))
178 return PTR_ERR(rcu_regmap);
179
180 err = regmap_read(rcu_regmap, LTQ_FALCON_SYS1_CPU0RS, &val);
181 if (err)
182 return err;
183
184 if ((val & LTQ_FALCON_SYS1_CPU0RS_MASK) == LTQ_FALCON_SYS1_CPU0RS_WDT)
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200185 return WDIOF_CARDRESET;
Hauke Mehrtens710322b2017-08-20 00:18:11 +0200186
187 return 0;
188}
189
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200190static int ltq_wdt_probe(struct platform_device *pdev)
John Crispin2f58b8d2011-05-05 23:00:23 +0200191{
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200192 struct device *dev = &pdev->dev;
193 struct ltq_wdt_priv *priv;
194 struct watchdog_device *wdt;
195 struct resource *res;
John Crispin2f58b8d2011-05-05 23:00:23 +0200196 struct clk *clk;
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200197 const struct ltq_wdt_hw *ltq_wdt_hw;
Hauke Mehrtens710322b2017-08-20 00:18:11 +0200198 int ret;
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200199 u32 status;
John Crispin2f58b8d2011-05-05 23:00:23 +0200200
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200201 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
202 if (!priv)
203 return -ENOMEM;
John Crispin2f58b8d2011-05-05 23:00:23 +0200204
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200205 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
206 priv->membase = devm_ioremap_resource(dev, res);
207 if (IS_ERR(priv->membase))
208 return PTR_ERR(priv->membase);
Hauke Mehrtens710322b2017-08-20 00:18:11 +0200209
John Crispin2f58b8d2011-05-05 23:00:23 +0200210 /* we do not need to enable the clock as it is always running */
John Crispincdb86122012-04-12 21:21:56 +0200211 clk = clk_get_io();
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200212 priv->clk_rate = clk_get_rate(clk) / LTQ_WDT_DIVIDER;
213 if (!priv->clk_rate) {
214 dev_err(dev, "clock rate less than divider %i\n",
215 LTQ_WDT_DIVIDER);
216 return -EINVAL;
John Crispincdb86122012-04-12 21:21:56 +0200217 }
John Crispin2f58b8d2011-05-05 23:00:23 +0200218
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200219 wdt = &priv->wdt;
220 wdt->info = &ltq_wdt_info;
221 wdt->ops = &ltq_wdt_ops;
222 wdt->min_timeout = 1;
223 wdt->max_timeout = LTQ_WDT_CR_MAX_TIMEOUT / priv->clk_rate;
224 wdt->timeout = wdt->max_timeout;
225 wdt->parent = dev;
226
227 ltq_wdt_hw = of_device_get_match_data(dev);
228 if (ltq_wdt_hw && ltq_wdt_hw->bootstatus_get) {
229 ret = ltq_wdt_hw->bootstatus_get(dev);
230 if (ret >= 0)
231 wdt->bootstatus = ret;
232 }
233
234 watchdog_set_nowayout(wdt, nowayout);
235 watchdog_init_timeout(wdt, 0, dev);
236
237 status = ltq_wdt_r32(priv, LTQ_WDT_SR);
238 if (status & LTQ_WDT_SR_EN) {
239 /*
240 * If the watchdog is already running overwrite it with our
241 * new settings. Stop is not needed as the start call will
242 * replace all settings anyway.
243 */
244 ltq_wdt_start(wdt);
245 set_bit(WDOG_HW_RUNNING, &wdt->status);
246 }
247
248 return devm_watchdog_register_device(dev, wdt);
John Crispin2f58b8d2011-05-05 23:00:23 +0200249}
250
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200251static const struct ltq_wdt_hw ltq_wdt_xrx100 = {
252 .bootstatus_get = ltq_wdt_xrx_bootstatus_get,
253};
John Crispin2f58b8d2011-05-05 23:00:23 +0200254
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200255static const struct ltq_wdt_hw ltq_wdt_falcon = {
256 .bootstatus_get = ltq_wdt_falcon_bootstatus_get,
257};
John Crispin2f58b8d2011-05-05 23:00:23 +0200258
John Crispincdb86122012-04-12 21:21:56 +0200259static const struct of_device_id ltq_wdt_match[] = {
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200260 { .compatible = "lantiq,wdt", .data = NULL },
261 { .compatible = "lantiq,xrx100-wdt", .data = &ltq_wdt_xrx100 },
262 { .compatible = "lantiq,falcon-wdt", .data = &ltq_wdt_falcon },
John Crispincdb86122012-04-12 21:21:56 +0200263 {},
264};
265MODULE_DEVICE_TABLE(of, ltq_wdt_match);
John Crispin2f58b8d2011-05-05 23:00:23 +0200266
267static struct platform_driver ltq_wdt_driver = {
John Crispincdb86122012-04-12 21:21:56 +0200268 .probe = ltq_wdt_probe,
John Crispin2f58b8d2011-05-05 23:00:23 +0200269 .driver = {
John Crispincdb86122012-04-12 21:21:56 +0200270 .name = "wdt",
John Crispincdb86122012-04-12 21:21:56 +0200271 .of_match_table = ltq_wdt_match,
John Crispin2f58b8d2011-05-05 23:00:23 +0200272 },
273};
274
John Crispincdb86122012-04-12 21:21:56 +0200275module_platform_driver(ltq_wdt_driver);
John Crispin2f58b8d2011-05-05 23:00:23 +0200276
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100277module_param(nowayout, bool, 0);
John Crispin2f58b8d2011-05-05 23:00:23 +0200278MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
John Crispinf3519a62016-12-20 19:56:59 +0100279MODULE_AUTHOR("John Crispin <john@phrozen.org>");
John Crispin2f58b8d2011-05-05 23:00:23 +0200280MODULE_DESCRIPTION("Lantiq SoC Watchdog");
281MODULE_LICENSE("GPL");