• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Broadcom Corporation
4  *
5  */
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>
13 #include <linux/platform_data/bcm7038_wdt.h>
14 #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 
29 struct bcm7038_watchdog {
30 	void __iomem		*base;
31 	struct watchdog_device	wdd;
32 	u32			rate;
33 	struct clk		*clk;
34 };
35 
36 static bool nowayout = WATCHDOG_NOWAYOUT;
37 
bcm7038_wdt_write(u32 value,void __iomem * addr)38 static 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 
bcm7038_wdt_read(void __iomem * addr)49 static 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 
bcm7038_wdt_set_timeout_reg(struct watchdog_device * wdog)57 static 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 
64 	bcm7038_wdt_write(timeout, wdt->base + WDT_TIMEOUT_REG);
65 }
66 
bcm7038_wdt_ping(struct watchdog_device * wdog)67 static int bcm7038_wdt_ping(struct watchdog_device *wdog)
68 {
69 	struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
70 
71 	bcm7038_wdt_write(WDT_START_1, wdt->base + WDT_CMD_REG);
72 	bcm7038_wdt_write(WDT_START_2, wdt->base + WDT_CMD_REG);
73 
74 	return 0;
75 }
76 
bcm7038_wdt_start(struct watchdog_device * wdog)77 static 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 
bcm7038_wdt_stop(struct watchdog_device * wdog)85 static int bcm7038_wdt_stop(struct watchdog_device *wdog)
86 {
87 	struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
88 
89 	bcm7038_wdt_write(WDT_STOP_1, wdt->base + WDT_CMD_REG);
90 	bcm7038_wdt_write(WDT_STOP_2, wdt->base + WDT_CMD_REG);
91 
92 	return 0;
93 }
94 
bcm7038_wdt_set_timeout(struct watchdog_device * wdog,unsigned int t)95 static 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 
bcm7038_wdt_get_timeleft(struct watchdog_device * wdog)106 static 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 
111 	time_left = bcm7038_wdt_read(wdt->base + WDT_CMD_REG);
112 
113 	return time_left / wdt->rate;
114 }
115 
116 static const struct watchdog_info bcm7038_wdt_info = {
117 	.identity	= "Broadcom BCM7038 Watchdog Timer",
118 	.options	= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
119 				WDIOF_MAGICCLOSE
120 };
121 
122 static const struct watchdog_ops bcm7038_wdt_ops = {
123 	.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 
bcm7038_clk_disable_unprepare(void * data)130 static void bcm7038_clk_disable_unprepare(void *data)
131 {
132 	clk_disable_unprepare(data);
133 }
134 
bcm7038_wdt_probe(struct platform_device * pdev)135 static int bcm7038_wdt_probe(struct platform_device *pdev)
136 {
137 	struct bcm7038_wdt_platform_data *pdata = pdev->dev.platform_data;
138 	struct device *dev = &pdev->dev;
139 	struct bcm7038_watchdog *wdt;
140 	const char *clk_name = NULL;
141 	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 
149 	wdt->base = devm_platform_ioremap_resource(pdev, 0);
150 	if (IS_ERR(wdt->base))
151 		return PTR_ERR(wdt->base);
152 
153 	if (pdata && pdata->clk_name)
154 		clk_name = pdata->clk_name;
155 
156 	wdt->clk = devm_clk_get(dev, clk_name);
157 	/* If unable to get clock, use default frequency */
158 	if (!IS_ERR(wdt->clk)) {
159 		err = clk_prepare_enable(wdt->clk);
160 		if (err)
161 			return err;
162 		err = devm_add_action_or_reset(dev,
163 					       bcm7038_clk_disable_unprepare,
164 					       wdt->clk);
165 		if (err)
166 			return err;
167 		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 
184 	watchdog_stop_on_reboot(&wdt->wdd);
185 	watchdog_stop_on_unregister(&wdt->wdd);
186 	err = devm_watchdog_register_device(dev, &wdt->wdd);
187 	if (err)
188 		return err;
189 
190 	dev_info(dev, "Registered BCM7038 Watchdog\n");
191 
192 	return 0;
193 }
194 
bcm7038_wdt_suspend(struct device * dev)195 static int bcm7038_wdt_suspend(struct device *dev)
196 {
197 	struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
198 
199 	if (watchdog_active(&wdt->wdd))
200 		return bcm7038_wdt_stop(&wdt->wdd);
201 
202 	return 0;
203 }
204 
bcm7038_wdt_resume(struct device * dev)205 static int bcm7038_wdt_resume(struct device *dev)
206 {
207 	struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
208 
209 	if (watchdog_active(&wdt->wdd))
210 		return bcm7038_wdt_start(&wdt->wdd);
211 
212 	return 0;
213 }
214 
215 static DEFINE_SIMPLE_DEV_PM_OPS(bcm7038_wdt_pm_ops,
216 				bcm7038_wdt_suspend, bcm7038_wdt_resume);
217 
218 static const struct of_device_id bcm7038_wdt_match[] = {
219 	{ .compatible = "brcm,bcm6345-wdt" },
220 	{ .compatible = "brcm,bcm7038-wdt" },
221 	{},
222 };
223 MODULE_DEVICE_TABLE(of, bcm7038_wdt_match);
224 
225 static const struct platform_device_id bcm7038_wdt_devtype[] = {
226 	{ .name = "bcm63xx-wdt" },
227 	{ /* sentinel */ },
228 };
229 MODULE_DEVICE_TABLE(platform, bcm7038_wdt_devtype);
230 
231 static struct platform_driver bcm7038_wdt_driver = {
232 	.probe		= bcm7038_wdt_probe,
233 	.id_table	= bcm7038_wdt_devtype,
234 	.driver		= {
235 		.name		= "bcm7038-wdt",
236 		.of_match_table	= bcm7038_wdt_match,
237 		.pm		= pm_sleep_ptr(&bcm7038_wdt_pm_ops),
238 	}
239 };
240 module_platform_driver(bcm7038_wdt_driver);
241 
242 module_param(nowayout, bool, 0);
243 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
244 	__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
245 MODULE_LICENSE("GPL");
246 MODULE_DESCRIPTION("Driver for Broadcom 7038 SoCs Watchdog");
247 MODULE_AUTHOR("Justin Chen");
248