blob: 83da84d6074b80ee5ff9d56c6a1106b17c106302 [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 */
Hauke Mehrtensc99d9df2018-09-13 23:32:11 +020053#define LTQ_WDT_SR_VALUE_MASK GENMASK(15, 0) /* Timer value */
John Crispin2f58b8d2011-05-05 23:00:23 +020054
John Crispin2f58b8d2011-05-05 23:00:23 +020055#define LTQ_WDT_DIVIDER 0x40000
John Crispin2f58b8d2011-05-05 23:00:23 +020056
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010057static bool nowayout = WATCHDOG_NOWAYOUT;
John Crispin2f58b8d2011-05-05 23:00:23 +020058
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020059struct ltq_wdt_hw {
60 int (*bootstatus_get)(struct device *dev);
61};
John Crispin2f58b8d2011-05-05 23:00:23 +020062
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020063struct ltq_wdt_priv {
64 struct watchdog_device wdt;
65 void __iomem *membase;
66 unsigned long clk_rate;
67};
John Crispin2f58b8d2011-05-05 23:00:23 +020068
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020069static u32 ltq_wdt_r32(struct ltq_wdt_priv *priv, u32 offset)
John Crispin2f58b8d2011-05-05 23:00:23 +020070{
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020071 return __raw_readl(priv->membase + offset);
John Crispin2f58b8d2011-05-05 23:00:23 +020072}
73
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020074static void ltq_wdt_w32(struct ltq_wdt_priv *priv, u32 val, u32 offset)
John Crispin2f58b8d2011-05-05 23:00:23 +020075{
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020076 __raw_writel(val, priv->membase + offset);
John Crispin2f58b8d2011-05-05 23:00:23 +020077}
78
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020079static void ltq_wdt_mask(struct ltq_wdt_priv *priv, u32 clear, u32 set,
80 u32 offset)
John Crispin2f58b8d2011-05-05 23:00:23 +020081{
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020082 u32 val = ltq_wdt_r32(priv, offset);
John Crispin2f58b8d2011-05-05 23:00:23 +020083
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020084 val &= ~(clear);
85 val |= set;
86 ltq_wdt_w32(priv, val, offset);
John Crispin2f58b8d2011-05-05 23:00:23 +020087}
88
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020089static struct ltq_wdt_priv *ltq_wdt_get_priv(struct watchdog_device *wdt)
90{
91 return container_of(wdt, struct ltq_wdt_priv, wdt);
92}
93
94static struct watchdog_info ltq_wdt_info = {
John Crispin2f58b8d2011-05-05 23:00:23 +020095 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +020096 WDIOF_CARDRESET,
John Crispin2f58b8d2011-05-05 23:00:23 +020097 .identity = "ltq_wdt",
98};
99
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200100static int ltq_wdt_start(struct watchdog_device *wdt)
John Crispin2f58b8d2011-05-05 23:00:23 +0200101{
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200102 struct ltq_wdt_priv *priv = ltq_wdt_get_priv(wdt);
103 u32 timeout;
John Crispin2f58b8d2011-05-05 23:00:23 +0200104
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200105 timeout = wdt->timeout * priv->clk_rate;
John Crispin2f58b8d2011-05-05 23:00:23 +0200106
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200107 ltq_wdt_mask(priv, LTQ_WDT_CR_PW_MASK, LTQ_WDT_CR_PW1, LTQ_WDT_CR);
108 /* write the second magic plus the configuration and new timeout */
109 ltq_wdt_mask(priv, LTQ_WDT_CR_PW_MASK | LTQ_WDT_CR_MAX_TIMEOUT,
110 LTQ_WDT_CR_GEN | LTQ_WDT_CR_PWL | LTQ_WDT_CR_CLKDIV |
111 LTQ_WDT_CR_PW2 | timeout,
112 LTQ_WDT_CR);
John Crispin2f58b8d2011-05-05 23:00:23 +0200113
114 return 0;
115}
116
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200117static int ltq_wdt_stop(struct watchdog_device *wdt)
Hauke Mehrtens710322b2017-08-20 00:18:11 +0200118{
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200119 struct ltq_wdt_priv *priv = ltq_wdt_get_priv(wdt);
120
121 ltq_wdt_mask(priv, LTQ_WDT_CR_PW_MASK, LTQ_WDT_CR_PW1, LTQ_WDT_CR);
122 ltq_wdt_mask(priv, LTQ_WDT_CR_GEN | LTQ_WDT_CR_PW_MASK,
123 LTQ_WDT_CR_PW2, LTQ_WDT_CR);
124
125 return 0;
126}
127
128static int ltq_wdt_ping(struct watchdog_device *wdt)
129{
130 struct ltq_wdt_priv *priv = ltq_wdt_get_priv(wdt);
131 u32 timeout;
132
133 timeout = wdt->timeout * priv->clk_rate;
134
135 ltq_wdt_mask(priv, LTQ_WDT_CR_PW_MASK, LTQ_WDT_CR_PW1, LTQ_WDT_CR);
136 /* write the second magic plus the configuration and new timeout */
137 ltq_wdt_mask(priv, LTQ_WDT_CR_PW_MASK | LTQ_WDT_CR_MAX_TIMEOUT,
138 LTQ_WDT_CR_PW2 | timeout, LTQ_WDT_CR);
139
140 return 0;
141}
142
Hauke Mehrtensc99d9df2018-09-13 23:32:11 +0200143static unsigned int ltq_wdt_get_timeleft(struct watchdog_device *wdt)
144{
145 struct ltq_wdt_priv *priv = ltq_wdt_get_priv(wdt);
146 u64 timeout;
147
148 timeout = ltq_wdt_r32(priv, LTQ_WDT_SR) & LTQ_WDT_SR_VALUE_MASK;
149 return do_div(timeout, priv->clk_rate);
150}
151
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200152static const struct watchdog_ops ltq_wdt_ops = {
153 .owner = THIS_MODULE,
154 .start = ltq_wdt_start,
155 .stop = ltq_wdt_stop,
156 .ping = ltq_wdt_ping,
Hauke Mehrtensc99d9df2018-09-13 23:32:11 +0200157 .get_timeleft = ltq_wdt_get_timeleft,
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200158};
159
160static int ltq_wdt_xrx_bootstatus_get(struct device *dev)
161{
Hauke Mehrtens710322b2017-08-20 00:18:11 +0200162 struct regmap *rcu_regmap;
163 u32 val;
164 int err;
165
166 rcu_regmap = syscon_regmap_lookup_by_phandle(dev->of_node, "regmap");
167 if (IS_ERR(rcu_regmap))
168 return PTR_ERR(rcu_regmap);
169
170 err = regmap_read(rcu_regmap, LTQ_XRX_RCU_RST_STAT, &val);
171 if (err)
172 return err;
173
174 if (val & LTQ_XRX_RCU_RST_STAT_WDT)
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200175 return WDIOF_CARDRESET;
Hauke Mehrtens710322b2017-08-20 00:18:11 +0200176
177 return 0;
178}
179
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200180static int ltq_wdt_falcon_bootstatus_get(struct device *dev)
Hauke Mehrtens710322b2017-08-20 00:18:11 +0200181{
Hauke Mehrtens710322b2017-08-20 00:18:11 +0200182 struct regmap *rcu_regmap;
183 u32 val;
184 int err;
185
186 rcu_regmap = syscon_regmap_lookup_by_phandle(dev->of_node,
187 "lantiq,rcu");
188 if (IS_ERR(rcu_regmap))
189 return PTR_ERR(rcu_regmap);
190
191 err = regmap_read(rcu_regmap, LTQ_FALCON_SYS1_CPU0RS, &val);
192 if (err)
193 return err;
194
195 if ((val & LTQ_FALCON_SYS1_CPU0RS_MASK) == LTQ_FALCON_SYS1_CPU0RS_WDT)
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200196 return WDIOF_CARDRESET;
Hauke Mehrtens710322b2017-08-20 00:18:11 +0200197
198 return 0;
199}
200
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200201static int ltq_wdt_probe(struct platform_device *pdev)
John Crispin2f58b8d2011-05-05 23:00:23 +0200202{
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200203 struct device *dev = &pdev->dev;
204 struct ltq_wdt_priv *priv;
205 struct watchdog_device *wdt;
206 struct resource *res;
John Crispin2f58b8d2011-05-05 23:00:23 +0200207 struct clk *clk;
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200208 const struct ltq_wdt_hw *ltq_wdt_hw;
Hauke Mehrtens710322b2017-08-20 00:18:11 +0200209 int ret;
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200210 u32 status;
John Crispin2f58b8d2011-05-05 23:00:23 +0200211
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200212 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
213 if (!priv)
214 return -ENOMEM;
John Crispin2f58b8d2011-05-05 23:00:23 +0200215
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200216 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
217 priv->membase = devm_ioremap_resource(dev, res);
218 if (IS_ERR(priv->membase))
219 return PTR_ERR(priv->membase);
Hauke Mehrtens710322b2017-08-20 00:18:11 +0200220
John Crispin2f58b8d2011-05-05 23:00:23 +0200221 /* we do not need to enable the clock as it is always running */
John Crispincdb86122012-04-12 21:21:56 +0200222 clk = clk_get_io();
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200223 priv->clk_rate = clk_get_rate(clk) / LTQ_WDT_DIVIDER;
224 if (!priv->clk_rate) {
225 dev_err(dev, "clock rate less than divider %i\n",
226 LTQ_WDT_DIVIDER);
227 return -EINVAL;
John Crispincdb86122012-04-12 21:21:56 +0200228 }
John Crispin2f58b8d2011-05-05 23:00:23 +0200229
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200230 wdt = &priv->wdt;
231 wdt->info = &ltq_wdt_info;
232 wdt->ops = &ltq_wdt_ops;
233 wdt->min_timeout = 1;
234 wdt->max_timeout = LTQ_WDT_CR_MAX_TIMEOUT / priv->clk_rate;
235 wdt->timeout = wdt->max_timeout;
236 wdt->parent = dev;
237
238 ltq_wdt_hw = of_device_get_match_data(dev);
239 if (ltq_wdt_hw && ltq_wdt_hw->bootstatus_get) {
240 ret = ltq_wdt_hw->bootstatus_get(dev);
241 if (ret >= 0)
242 wdt->bootstatus = ret;
243 }
244
245 watchdog_set_nowayout(wdt, nowayout);
246 watchdog_init_timeout(wdt, 0, dev);
247
248 status = ltq_wdt_r32(priv, LTQ_WDT_SR);
249 if (status & LTQ_WDT_SR_EN) {
250 /*
251 * If the watchdog is already running overwrite it with our
252 * new settings. Stop is not needed as the start call will
253 * replace all settings anyway.
254 */
255 ltq_wdt_start(wdt);
256 set_bit(WDOG_HW_RUNNING, &wdt->status);
257 }
258
259 return devm_watchdog_register_device(dev, wdt);
John Crispin2f58b8d2011-05-05 23:00:23 +0200260}
261
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200262static const struct ltq_wdt_hw ltq_wdt_xrx100 = {
263 .bootstatus_get = ltq_wdt_xrx_bootstatus_get,
264};
John Crispin2f58b8d2011-05-05 23:00:23 +0200265
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200266static const struct ltq_wdt_hw ltq_wdt_falcon = {
267 .bootstatus_get = ltq_wdt_falcon_bootstatus_get,
268};
John Crispin2f58b8d2011-05-05 23:00:23 +0200269
John Crispincdb86122012-04-12 21:21:56 +0200270static const struct of_device_id ltq_wdt_match[] = {
Hauke Mehrtensdcd7e042018-09-13 23:32:10 +0200271 { .compatible = "lantiq,wdt", .data = NULL },
272 { .compatible = "lantiq,xrx100-wdt", .data = &ltq_wdt_xrx100 },
273 { .compatible = "lantiq,falcon-wdt", .data = &ltq_wdt_falcon },
John Crispincdb86122012-04-12 21:21:56 +0200274 {},
275};
276MODULE_DEVICE_TABLE(of, ltq_wdt_match);
John Crispin2f58b8d2011-05-05 23:00:23 +0200277
278static struct platform_driver ltq_wdt_driver = {
John Crispincdb86122012-04-12 21:21:56 +0200279 .probe = ltq_wdt_probe,
John Crispin2f58b8d2011-05-05 23:00:23 +0200280 .driver = {
John Crispincdb86122012-04-12 21:21:56 +0200281 .name = "wdt",
John Crispincdb86122012-04-12 21:21:56 +0200282 .of_match_table = ltq_wdt_match,
John Crispin2f58b8d2011-05-05 23:00:23 +0200283 },
284};
285
John Crispincdb86122012-04-12 21:21:56 +0200286module_platform_driver(ltq_wdt_driver);
John Crispin2f58b8d2011-05-05 23:00:23 +0200287
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100288module_param(nowayout, bool, 0);
John Crispin2f58b8d2011-05-05 23:00:23 +0200289MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
John Crispinf3519a62016-12-20 19:56:59 +0100290MODULE_AUTHOR("John Crispin <john@phrozen.org>");
John Crispin2f58b8d2011-05-05 23:00:23 +0200291MODULE_DESCRIPTION("Lantiq SoC Watchdog");
292MODULE_LICENSE("GPL");