blob: 8656a137e9a4ada6f2363233d5f219d7335dccb0 [file] [log] [blame]
Marcus Folkesson2e62c492018-03-16 16:14:11 +01001// SPDX-License-Identifier: GPL-2.0+
Justin Chen7a3629f2015-08-31 11:02:43 -07002/*
3 * Copyright (C) 2015 Broadcom Corporation
4 *
Justin Chen7a3629f2015-08-31 11:02:43 -07005 */
6
7#include <linux/clk.h>
8#include <linux/init.h>
9#include <linux/io.h>
10#include <linux/module.h>
11#include <linux/of.h>
12#include <linux/platform_device.h>
Florian Fainellid6b9c672021-11-12 14:46:32 -080013#include <linux/platform_data/bcm7038_wdt.h>
Justin Chen7a3629f2015-08-31 11:02:43 -070014#include <linux/pm.h>
15#include <linux/watchdog.h>
16
17#define WDT_START_1 0xff00
18#define WDT_START_2 0x00ff
19#define WDT_STOP_1 0xee00
20#define WDT_STOP_2 0x00ee
21
22#define WDT_TIMEOUT_REG 0x0
23#define WDT_CMD_REG 0x4
24
25#define WDT_MIN_TIMEOUT 1 /* seconds */
26#define WDT_DEFAULT_TIMEOUT 30 /* seconds */
27#define WDT_DEFAULT_RATE 27000000
28
29struct bcm7038_watchdog {
30 void __iomem *base;
31 struct watchdog_device wdd;
32 u32 rate;
33 struct clk *clk;
34};
35
36static bool nowayout = WATCHDOG_NOWAYOUT;
37
Álvaro Fernández Rojase379c212021-02-23 09:00:42 +010038static inline void bcm7038_wdt_write(u32 value, void __iomem *addr)
39{
40 /* MIPS chips strapped for BE will automagically configure the
41 * peripheral registers for CPU-native byte order.
42 */
43 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
44 __raw_writel(value, addr);
45 else
46 writel_relaxed(value, addr);
47}
48
49static inline u32 bcm7038_wdt_read(void __iomem *addr)
50{
51 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
52 return __raw_readl(addr);
53 else
54 return readl_relaxed(addr);
55}
56
Justin Chen7a3629f2015-08-31 11:02:43 -070057static void bcm7038_wdt_set_timeout_reg(struct watchdog_device *wdog)
58{
59 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
60 u32 timeout;
61
62 timeout = wdt->rate * wdog->timeout;
63
Álvaro Fernández Rojase379c212021-02-23 09:00:42 +010064 bcm7038_wdt_write(timeout, wdt->base + WDT_TIMEOUT_REG);
Justin Chen7a3629f2015-08-31 11:02:43 -070065}
66
67static int bcm7038_wdt_ping(struct watchdog_device *wdog)
68{
69 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
70
Álvaro Fernández Rojase379c212021-02-23 09:00:42 +010071 bcm7038_wdt_write(WDT_START_1, wdt->base + WDT_CMD_REG);
72 bcm7038_wdt_write(WDT_START_2, wdt->base + WDT_CMD_REG);
Justin Chen7a3629f2015-08-31 11:02:43 -070073
74 return 0;
75}
76
77static int bcm7038_wdt_start(struct watchdog_device *wdog)
78{
79 bcm7038_wdt_set_timeout_reg(wdog);
80 bcm7038_wdt_ping(wdog);
81
82 return 0;
83}
84
85static int bcm7038_wdt_stop(struct watchdog_device *wdog)
86{
87 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
88
Álvaro Fernández Rojase379c212021-02-23 09:00:42 +010089 bcm7038_wdt_write(WDT_STOP_1, wdt->base + WDT_CMD_REG);
90 bcm7038_wdt_write(WDT_STOP_2, wdt->base + WDT_CMD_REG);
Justin Chen7a3629f2015-08-31 11:02:43 -070091
92 return 0;
93}
94
95static int bcm7038_wdt_set_timeout(struct watchdog_device *wdog,
96 unsigned int t)
97{
98 /* Can't modify timeout value if watchdog timer is running */
99 bcm7038_wdt_stop(wdog);
100 wdog->timeout = t;
101 bcm7038_wdt_start(wdog);
102
103 return 0;
104}
105
106static unsigned int bcm7038_wdt_get_timeleft(struct watchdog_device *wdog)
107{
108 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
109 u32 time_left;
110
Álvaro Fernández Rojase379c212021-02-23 09:00:42 +0100111 time_left = bcm7038_wdt_read(wdt->base + WDT_CMD_REG);
Justin Chen7a3629f2015-08-31 11:02:43 -0700112
113 return time_left / wdt->rate;
114}
115
Bhumika Goyal6c368932016-12-26 22:35:11 +0530116static const struct watchdog_info bcm7038_wdt_info = {
Justin Chen7a3629f2015-08-31 11:02:43 -0700117 .identity = "Broadcom BCM7038 Watchdog Timer",
118 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
119 WDIOF_MAGICCLOSE
120};
121
Julia Lawall85f15cf2016-09-01 19:35:26 +0200122static const struct watchdog_ops bcm7038_wdt_ops = {
Justin Chen7a3629f2015-08-31 11:02:43 -0700123 .owner = THIS_MODULE,
124 .start = bcm7038_wdt_start,
125 .stop = bcm7038_wdt_stop,
126 .set_timeout = bcm7038_wdt_set_timeout,
127 .get_timeleft = bcm7038_wdt_get_timeleft,
128};
129
Guenter Roeck69656dc2019-04-08 12:38:30 -0700130static void bcm7038_clk_disable_unprepare(void *data)
131{
132 clk_disable_unprepare(data);
133}
134
Justin Chen7a3629f2015-08-31 11:02:43 -0700135static int bcm7038_wdt_probe(struct platform_device *pdev)
136{
Florian Fainellid6b9c672021-11-12 14:46:32 -0800137 struct bcm7038_wdt_platform_data *pdata = pdev->dev.platform_data;
Justin Chen7a3629f2015-08-31 11:02:43 -0700138 struct device *dev = &pdev->dev;
139 struct bcm7038_watchdog *wdt;
Florian Fainellid6b9c672021-11-12 14:46:32 -0800140 const char *clk_name = NULL;
Justin Chen7a3629f2015-08-31 11:02:43 -0700141 int err;
142
143 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
144 if (!wdt)
145 return -ENOMEM;
146
147 platform_set_drvdata(pdev, wdt);
148
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700149 wdt->base = devm_platform_ioremap_resource(pdev, 0);
Justin Chen7a3629f2015-08-31 11:02:43 -0700150 if (IS_ERR(wdt->base))
151 return PTR_ERR(wdt->base);
152
Florian Fainellid6b9c672021-11-12 14:46:32 -0800153 if (pdata && pdata->clk_name)
154 clk_name = pdata->clk_name;
155
156 wdt->clk = devm_clk_get(dev, clk_name);
Justin Chen7a3629f2015-08-31 11:02:43 -0700157 /* If unable to get clock, use default frequency */
158 if (!IS_ERR(wdt->clk)) {
Fabio Estevamdd0a18c2017-07-22 17:22:59 -0300159 err = clk_prepare_enable(wdt->clk);
160 if (err)
161 return err;
Guenter Roeck69656dc2019-04-08 12:38:30 -0700162 err = devm_add_action_or_reset(dev,
163 bcm7038_clk_disable_unprepare,
164 wdt->clk);
165 if (err)
166 return err;
Justin Chen7a3629f2015-08-31 11:02:43 -0700167 wdt->rate = clk_get_rate(wdt->clk);
168 /* Prevent divide-by-zero exception */
169 if (!wdt->rate)
170 wdt->rate = WDT_DEFAULT_RATE;
171 } else {
172 wdt->rate = WDT_DEFAULT_RATE;
173 wdt->clk = NULL;
174 }
175
176 wdt->wdd.info = &bcm7038_wdt_info;
177 wdt->wdd.ops = &bcm7038_wdt_ops;
178 wdt->wdd.min_timeout = WDT_MIN_TIMEOUT;
179 wdt->wdd.timeout = WDT_DEFAULT_TIMEOUT;
180 wdt->wdd.max_timeout = 0xffffffff / wdt->rate;
181 wdt->wdd.parent = dev;
182 watchdog_set_drvdata(&wdt->wdd, wdt);
183
Guenter Roeck69656dc2019-04-08 12:38:30 -0700184 watchdog_stop_on_reboot(&wdt->wdd);
185 watchdog_stop_on_unregister(&wdt->wdd);
186 err = devm_watchdog_register_device(dev, &wdt->wdd);
Wolfram Sangf5429892019-05-18 23:27:20 +0200187 if (err)
Justin Chen7a3629f2015-08-31 11:02:43 -0700188 return err;
Justin Chen7a3629f2015-08-31 11:02:43 -0700189
190 dev_info(dev, "Registered BCM7038 Watchdog\n");
191
192 return 0;
193}
194
Justin Chen7a3629f2015-08-31 11:02:43 -0700195#ifdef CONFIG_PM_SLEEP
196static int bcm7038_wdt_suspend(struct device *dev)
197{
198 struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
199
200 if (watchdog_active(&wdt->wdd))
201 return bcm7038_wdt_stop(&wdt->wdd);
202
203 return 0;
204}
205
206static int bcm7038_wdt_resume(struct device *dev)
207{
208 struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
209
210 if (watchdog_active(&wdt->wdd))
211 return bcm7038_wdt_start(&wdt->wdd);
212
213 return 0;
214}
215#endif
216
217static SIMPLE_DEV_PM_OPS(bcm7038_wdt_pm_ops, bcm7038_wdt_suspend,
218 bcm7038_wdt_resume);
219
Justin Chen7a3629f2015-08-31 11:02:43 -0700220static const struct of_device_id bcm7038_wdt_match[] = {
221 { .compatible = "brcm,bcm7038-wdt" },
222 {},
223};
Javier Martinez Canillas57d77c62016-10-14 12:23:50 -0300224MODULE_DEVICE_TABLE(of, bcm7038_wdt_match);
Justin Chen7a3629f2015-08-31 11:02:43 -0700225
Florian Fainellie764fae2021-11-12 14:46:34 -0800226static const struct platform_device_id bcm7038_wdt_devtype[] = {
227 { .name = "bcm63xx-wdt" },
228 { /* sentinel */ },
229};
230MODULE_DEVICE_TABLE(platform, bcm7038_wdt_devtype);
231
Justin Chen7a3629f2015-08-31 11:02:43 -0700232static struct platform_driver bcm7038_wdt_driver = {
233 .probe = bcm7038_wdt_probe,
Florian Fainellie764fae2021-11-12 14:46:34 -0800234 .id_table = bcm7038_wdt_devtype,
Justin Chen7a3629f2015-08-31 11:02:43 -0700235 .driver = {
236 .name = "bcm7038-wdt",
237 .of_match_table = bcm7038_wdt_match,
238 .pm = &bcm7038_wdt_pm_ops,
239 }
240};
241module_platform_driver(bcm7038_wdt_driver);
242
243module_param(nowayout, bool, 0);
244MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
245 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Marcus Folkesson2e62c492018-03-16 16:14:11 +0100246MODULE_LICENSE("GPL");
Justin Chen7a3629f2015-08-31 11:02:43 -0700247MODULE_DESCRIPTION("Driver for Broadcom 7038 SoCs Watchdog");
248MODULE_AUTHOR("Justin Chen");