2 * Renesas RZ/A Series WDT Driver
4 * Copyright (C) 2017 Renesas Electronics America, Inc.
5 * Copyright (C) 2017 Chris Brandt
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
12 #include <linux/bitops.h>
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/platform_device.h>
18 #include <linux/watchdog.h>
20 #define DEFAULT_TIMEOUT 30
22 /* Watchdog Timer Registers */
24 #define WTCSR_MAGIC 0xA500
25 #define WTSCR_WT BIT(6)
26 #define WTSCR_TME BIT(5)
27 #define WTSCR_CKS(i) (i)
30 #define WTCNT_MAGIC 0x5A00
33 #define WRCSR_MAGIC 0x5A00
34 #define WRCSR_RSTE BIT(6)
35 #define WRCSR_CLEAR_WOVF 0xA500 /* special value */
38 struct watchdog_device wdev;
43 static int rza_wdt_start(struct watchdog_device *wdev)
45 struct rza_wdt *priv = watchdog_get_drvdata(wdev);
48 writew(WTCSR_MAGIC | 0, priv->base + WTCSR);
50 /* Must dummy read WRCSR:WOVF at least once before clearing */
51 readb(priv->base + WRCSR);
52 writew(WRCSR_CLEAR_WOVF, priv->base + WRCSR);
55 * Start timer with slowest clock source and reset option enabled.
57 writew(WRCSR_MAGIC | WRCSR_RSTE, priv->base + WRCSR);
58 writew(WTCNT_MAGIC | 0, priv->base + WTCNT);
59 writew(WTCSR_MAGIC | WTSCR_WT | WTSCR_TME | WTSCR_CKS(7),
65 static int rza_wdt_stop(struct watchdog_device *wdev)
67 struct rza_wdt *priv = watchdog_get_drvdata(wdev);
69 writew(WTCSR_MAGIC | 0, priv->base + WTCSR);
74 static int rza_wdt_ping(struct watchdog_device *wdev)
76 struct rza_wdt *priv = watchdog_get_drvdata(wdev);
78 writew(WTCNT_MAGIC | 0, priv->base + WTCNT);
83 static int rza_wdt_restart(struct watchdog_device *wdev, unsigned long action,
86 struct rza_wdt *priv = watchdog_get_drvdata(wdev);
89 writew(WTCSR_MAGIC | 0, priv->base + WTCSR);
91 /* Must dummy read WRCSR:WOVF at least once before clearing */
92 readb(priv->base + WRCSR);
93 writew(WRCSR_CLEAR_WOVF, priv->base + WRCSR);
96 * Start timer with fastest clock source and only 1 clock left before
97 * overflow with reset option enabled.
99 writew(WRCSR_MAGIC | WRCSR_RSTE, priv->base + WRCSR);
100 writew(WTCNT_MAGIC | 255, priv->base + WTCNT);
101 writew(WTCSR_MAGIC | WTSCR_WT | WTSCR_TME, priv->base + WTCSR);
104 * Actually make sure the above sequence hits hardware before sleeping.
108 /* Wait for WDT overflow (reset) */
114 static const struct watchdog_info rza_wdt_ident = {
115 .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT,
116 .identity = "Renesas RZ/A WDT Watchdog",
119 static const struct watchdog_ops rza_wdt_ops = {
120 .owner = THIS_MODULE,
121 .start = rza_wdt_start,
122 .stop = rza_wdt_stop,
123 .ping = rza_wdt_ping,
124 .restart = rza_wdt_restart,
127 static int rza_wdt_probe(struct platform_device *pdev)
129 struct rza_wdt *priv;
130 struct resource *res;
134 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
138 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
139 priv->base = devm_ioremap_resource(&pdev->dev, res);
140 if (IS_ERR(priv->base))
141 return PTR_ERR(priv->base);
143 priv->clk = devm_clk_get(&pdev->dev, NULL);
144 if (IS_ERR(priv->clk))
145 return PTR_ERR(priv->clk);
147 rate = clk_get_rate(priv->clk);
149 dev_err(&pdev->dev, "invalid clock rate (%ld)\n", rate);
153 /* Assume slowest clock rate possible (CKS=7) */
156 priv->wdev.info = &rza_wdt_ident,
157 priv->wdev.ops = &rza_wdt_ops,
158 priv->wdev.parent = &pdev->dev;
161 * Since the max possible timeout of our 8-bit count register is less
162 * than a second, we must use max_hw_heartbeat_ms.
164 priv->wdev.max_hw_heartbeat_ms = (1000 * U8_MAX) / rate;
165 dev_dbg(&pdev->dev, "max hw timeout of %dms\n",
166 priv->wdev.max_hw_heartbeat_ms);
168 priv->wdev.min_timeout = 1;
169 priv->wdev.timeout = DEFAULT_TIMEOUT;
171 watchdog_init_timeout(&priv->wdev, 0, &pdev->dev);
172 watchdog_set_drvdata(&priv->wdev, priv);
174 ret = devm_watchdog_register_device(&pdev->dev, &priv->wdev);
176 dev_err(&pdev->dev, "Cannot register watchdog device\n");
181 static const struct of_device_id rza_wdt_of_match[] = {
182 { .compatible = "renesas,rza-wdt", },
185 MODULE_DEVICE_TABLE(of, rza_wdt_of_match);
187 static struct platform_driver rza_wdt_driver = {
188 .probe = rza_wdt_probe,
191 .of_match_table = rza_wdt_of_match,
195 module_platform_driver(rza_wdt_driver);
197 MODULE_DESCRIPTION("Renesas RZ/A WDT Driver");
199 MODULE_LICENSE("GPL v2");