xref: /openbmc/linux/drivers/watchdog/jz4740_wdt.c (revision 1d9c30745455c42bff07f500fc6ecaf4c10e942f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (C) 2010, Paul Cercueil <paul@crapouillou.net>
4  *  JZ4740 Watchdog driver
5  */
6 
7 #include <linux/mfd/ingenic-tcu.h>
8 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/watchdog.h>
13 #include <linux/platform_device.h>
14 #include <linux/io.h>
15 #include <linux/device.h>
16 #include <linux/clk.h>
17 #include <linux/slab.h>
18 #include <linux/err.h>
19 #include <linux/of.h>
20 
21 #define DEFAULT_HEARTBEAT 5
22 #define MAX_HEARTBEAT     2048
23 
24 static bool nowayout = WATCHDOG_NOWAYOUT;
25 module_param(nowayout, bool, 0);
26 MODULE_PARM_DESC(nowayout,
27 		 "Watchdog cannot be stopped once started (default="
28 		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
29 
30 static unsigned int heartbeat = DEFAULT_HEARTBEAT;
31 module_param(heartbeat, uint, 0);
32 MODULE_PARM_DESC(heartbeat,
33 		"Watchdog heartbeat period in seconds from 1 to "
34 		__MODULE_STRING(MAX_HEARTBEAT) ", default "
35 		__MODULE_STRING(DEFAULT_HEARTBEAT));
36 
37 struct jz4740_wdt_drvdata {
38 	struct watchdog_device wdt;
39 	void __iomem *base;
40 	struct clk *clk;
41 	unsigned long clk_rate;
42 };
43 
44 static int jz4740_wdt_ping(struct watchdog_device *wdt_dev)
45 {
46 	struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
47 
48 	writew(0x0, drvdata->base + TCU_REG_WDT_TCNT);
49 	return 0;
50 }
51 
52 static int jz4740_wdt_set_timeout(struct watchdog_device *wdt_dev,
53 				    unsigned int new_timeout)
54 {
55 	struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
56 	u16 timeout_value = (u16)(drvdata->clk_rate * new_timeout);
57 	u8 tcer;
58 
59 	tcer = readb(drvdata->base + TCU_REG_WDT_TCER);
60 	writeb(0x0, drvdata->base + TCU_REG_WDT_TCER);
61 
62 	writew((u16)timeout_value, drvdata->base + TCU_REG_WDT_TDR);
63 	writew(0x0, drvdata->base + TCU_REG_WDT_TCNT);
64 
65 	if (tcer & TCU_WDT_TCER_TCEN)
66 		writeb(TCU_WDT_TCER_TCEN, drvdata->base + TCU_REG_WDT_TCER);
67 
68 	wdt_dev->timeout = new_timeout;
69 	return 0;
70 }
71 
72 static int jz4740_wdt_start(struct watchdog_device *wdt_dev)
73 {
74 	struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
75 	int ret;
76 	u8 tcer;
77 
78 	ret = clk_prepare_enable(drvdata->clk);
79 	if (ret)
80 		return ret;
81 
82 	tcer = readb(drvdata->base + TCU_REG_WDT_TCER);
83 
84 	jz4740_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
85 
86 	/* Start watchdog if it wasn't started already */
87 	if (!(tcer & TCU_WDT_TCER_TCEN))
88 		writeb(TCU_WDT_TCER_TCEN, drvdata->base + TCU_REG_WDT_TCER);
89 
90 	return 0;
91 }
92 
93 static int jz4740_wdt_stop(struct watchdog_device *wdt_dev)
94 {
95 	struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
96 
97 	writeb(0x0, drvdata->base + TCU_REG_WDT_TCER);
98 	clk_disable_unprepare(drvdata->clk);
99 
100 	return 0;
101 }
102 
103 static int jz4740_wdt_restart(struct watchdog_device *wdt_dev,
104 			      unsigned long action, void *data)
105 {
106 	wdt_dev->timeout = 0;
107 	jz4740_wdt_start(wdt_dev);
108 	return 0;
109 }
110 
111 static const struct watchdog_info jz4740_wdt_info = {
112 	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
113 	.identity = "jz4740 Watchdog",
114 };
115 
116 static const struct watchdog_ops jz4740_wdt_ops = {
117 	.owner = THIS_MODULE,
118 	.start = jz4740_wdt_start,
119 	.stop = jz4740_wdt_stop,
120 	.ping = jz4740_wdt_ping,
121 	.set_timeout = jz4740_wdt_set_timeout,
122 	.restart = jz4740_wdt_restart,
123 };
124 
125 #ifdef CONFIG_OF
126 static const struct of_device_id jz4740_wdt_of_matches[] = {
127 	{ .compatible = "ingenic,jz4740-watchdog", },
128 	{ .compatible = "ingenic,jz4780-watchdog", },
129 	{ /* sentinel */ }
130 };
131 MODULE_DEVICE_TABLE(of, jz4740_wdt_of_matches);
132 #endif
133 
134 static int jz4740_wdt_probe(struct platform_device *pdev)
135 {
136 	struct device *dev = &pdev->dev;
137 	struct jz4740_wdt_drvdata *drvdata;
138 	struct watchdog_device *jz4740_wdt;
139 	long rate;
140 	int ret;
141 
142 	drvdata = devm_kzalloc(dev, sizeof(struct jz4740_wdt_drvdata),
143 			       GFP_KERNEL);
144 	if (!drvdata)
145 		return -ENOMEM;
146 
147 	drvdata->clk = devm_clk_get(&pdev->dev, "wdt");
148 	if (IS_ERR(drvdata->clk)) {
149 		dev_err(&pdev->dev, "cannot find WDT clock\n");
150 		return PTR_ERR(drvdata->clk);
151 	}
152 
153 	/* Set smallest clock possible */
154 	rate = clk_round_rate(drvdata->clk, 1);
155 	if (rate < 0)
156 		return rate;
157 
158 	ret = clk_set_rate(drvdata->clk, rate);
159 	if (ret)
160 		return ret;
161 
162 	drvdata->clk_rate = rate;
163 	jz4740_wdt = &drvdata->wdt;
164 	jz4740_wdt->info = &jz4740_wdt_info;
165 	jz4740_wdt->ops = &jz4740_wdt_ops;
166 	jz4740_wdt->min_timeout = 1;
167 	jz4740_wdt->max_timeout = 0xffff / rate;
168 	jz4740_wdt->timeout = clamp(heartbeat,
169 				    jz4740_wdt->min_timeout,
170 				    jz4740_wdt->max_timeout);
171 	jz4740_wdt->parent = dev;
172 	watchdog_set_nowayout(jz4740_wdt, nowayout);
173 	watchdog_set_drvdata(jz4740_wdt, drvdata);
174 
175 	drvdata->base = devm_platform_ioremap_resource(pdev, 0);
176 	if (IS_ERR(drvdata->base))
177 		return PTR_ERR(drvdata->base);
178 
179 	return devm_watchdog_register_device(dev, &drvdata->wdt);
180 }
181 
182 static struct platform_driver jz4740_wdt_driver = {
183 	.probe = jz4740_wdt_probe,
184 	.driver = {
185 		.name = "jz4740-wdt",
186 		.of_match_table = of_match_ptr(jz4740_wdt_of_matches),
187 	},
188 };
189 
190 module_platform_driver(jz4740_wdt_driver);
191 
192 MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
193 MODULE_DESCRIPTION("jz4740 Watchdog Driver");
194 MODULE_LICENSE("GPL");
195 MODULE_ALIAS("platform:jz4740-wdt");
196