blob: 60deb9d304c0dfcea5bb8b5b8111d164cffcf791 [file] [log] [blame]
Carlo Caioned00680e2013-07-30 21:20:46 +02001/*
2 * sunxi Watchdog Driver
3 *
4 * Copyright (c) 2013 Carlo Caione
5 * 2012 Henrik Nordstrom
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * Based on xen_wdt.c
13 * (c) Copyright 2010 Novell, Inc.
14 */
15
16#include <linux/clk.h>
Maxime Ripard440e96b2014-05-06 21:44:19 -050017#include <linux/delay.h>
Carlo Caioned00680e2013-07-30 21:20:46 +020018#include <linux/err.h>
19#include <linux/init.h>
20#include <linux/io.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/moduleparam.h>
24#include <linux/of.h>
25#include <linux/platform_device.h>
Maxime Ripard440e96b2014-05-06 21:44:19 -050026#include <linux/reboot.h>
Carlo Caioned00680e2013-07-30 21:20:46 +020027#include <linux/types.h>
28#include <linux/watchdog.h>
29
Maxime Ripard440e96b2014-05-06 21:44:19 -050030#include <asm/system_misc.h>
31
Carlo Caioned00680e2013-07-30 21:20:46 +020032#define WDT_MAX_TIMEOUT 16
33#define WDT_MIN_TIMEOUT 1
34#define WDT_MODE_TIMEOUT(n) ((n) << 3)
35#define WDT_TIMEOUT_MASK WDT_MODE_TIMEOUT(0x0F)
36
37#define WDT_CTRL 0x00
38#define WDT_CTRL_RELOAD ((1 << 0) | (0x0a57 << 1))
39
40#define WDT_MODE 0x04
41#define WDT_MODE_EN (1 << 0)
42#define WDT_MODE_RST_EN (1 << 1)
43
44#define DRV_NAME "sunxi-wdt"
45#define DRV_VERSION "1.0"
46
47static bool nowayout = WATCHDOG_NOWAYOUT;
48static unsigned int timeout = WDT_MAX_TIMEOUT;
49
50struct sunxi_wdt_dev {
51 struct watchdog_device wdt_dev;
52 void __iomem *wdt_base;
53};
54
55/*
56 * wdt_timeout_map maps the watchdog timer interval value in seconds to
57 * the value of the register WDT_MODE bit 3:6
58 *
59 * [timeout seconds] = register value
60 *
61 */
62
63static const int wdt_timeout_map[] = {
Emilio López51ee34a2014-04-04 14:24:25 -030064 [1] = 0x1, /* 1s */
65 [2] = 0x2, /* 2s */
66 [3] = 0x3, /* 3s */
67 [4] = 0x4, /* 4s */
68 [5] = 0x5, /* 5s */
69 [6] = 0x6, /* 6s */
70 [8] = 0x7, /* 8s */
71 [10] = 0x8, /* 10s */
72 [12] = 0x9, /* 12s */
73 [14] = 0xA, /* 14s */
74 [16] = 0xB, /* 16s */
Carlo Caioned00680e2013-07-30 21:20:46 +020075};
76
Maxime Ripard440e96b2014-05-06 21:44:19 -050077static void __iomem *reboot_wdt_base;
78
79static void sun4i_wdt_restart(enum reboot_mode mode, const char *cmd)
80{
81 /* Enable timer and set reset bit in the watchdog */
82 writel(WDT_MODE_EN | WDT_MODE_RST_EN, reboot_wdt_base + WDT_MODE);
83
84 /*
85 * Restart the watchdog. The default (and lowest) interval
86 * value for the watchdog is 0.5s.
87 */
88 writel(WDT_CTRL_RELOAD, reboot_wdt_base + WDT_CTRL);
89
90 while (1) {
91 mdelay(5);
92 writel(WDT_MODE_EN | WDT_MODE_RST_EN,
93 reboot_wdt_base + WDT_MODE);
94 }
95}
96
Carlo Caioned00680e2013-07-30 21:20:46 +020097static int sunxi_wdt_ping(struct watchdog_device *wdt_dev)
98{
99 struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
100 void __iomem *wdt_base = sunxi_wdt->wdt_base;
101
102 iowrite32(WDT_CTRL_RELOAD, wdt_base + WDT_CTRL);
103
104 return 0;
105}
106
107static int sunxi_wdt_set_timeout(struct watchdog_device *wdt_dev,
108 unsigned int timeout)
109{
110 struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
111 void __iomem *wdt_base = sunxi_wdt->wdt_base;
112 u32 reg;
113
114 if (wdt_timeout_map[timeout] == 0)
115 timeout++;
116
117 sunxi_wdt->wdt_dev.timeout = timeout;
118
119 reg = ioread32(wdt_base + WDT_MODE);
120 reg &= ~WDT_TIMEOUT_MASK;
121 reg |= WDT_MODE_TIMEOUT(wdt_timeout_map[timeout]);
122 iowrite32(reg, wdt_base + WDT_MODE);
123
124 sunxi_wdt_ping(wdt_dev);
125
126 return 0;
127}
128
129static int sunxi_wdt_stop(struct watchdog_device *wdt_dev)
130{
131 struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
132 void __iomem *wdt_base = sunxi_wdt->wdt_base;
133
134 iowrite32(0, wdt_base + WDT_MODE);
135
136 return 0;
137}
138
139static int sunxi_wdt_start(struct watchdog_device *wdt_dev)
140{
141 u32 reg;
142 struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
143 void __iomem *wdt_base = sunxi_wdt->wdt_base;
144 int ret;
145
146 ret = sunxi_wdt_set_timeout(&sunxi_wdt->wdt_dev,
147 sunxi_wdt->wdt_dev.timeout);
148 if (ret < 0)
149 return ret;
150
151 reg = ioread32(wdt_base + WDT_MODE);
152 reg |= (WDT_MODE_RST_EN | WDT_MODE_EN);
153 iowrite32(reg, wdt_base + WDT_MODE);
154
155 return 0;
156}
157
158static const struct watchdog_info sunxi_wdt_info = {
159 .identity = DRV_NAME,
160 .options = WDIOF_SETTIMEOUT |
161 WDIOF_KEEPALIVEPING |
162 WDIOF_MAGICCLOSE,
163};
164
165static const struct watchdog_ops sunxi_wdt_ops = {
166 .owner = THIS_MODULE,
167 .start = sunxi_wdt_start,
168 .stop = sunxi_wdt_stop,
169 .ping = sunxi_wdt_ping,
170 .set_timeout = sunxi_wdt_set_timeout,
171};
172
Maxime Ripard1d5898b2013-10-05 16:20:17 +0200173static int sunxi_wdt_probe(struct platform_device *pdev)
Carlo Caioned00680e2013-07-30 21:20:46 +0200174{
175 struct sunxi_wdt_dev *sunxi_wdt;
176 struct resource *res;
177 int err;
178
179 sunxi_wdt = devm_kzalloc(&pdev->dev, sizeof(*sunxi_wdt), GFP_KERNEL);
180 if (!sunxi_wdt)
181 return -EINVAL;
182
183 platform_set_drvdata(pdev, sunxi_wdt);
184
185 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
186 sunxi_wdt->wdt_base = devm_ioremap_resource(&pdev->dev, res);
187 if (IS_ERR(sunxi_wdt->wdt_base))
188 return PTR_ERR(sunxi_wdt->wdt_base);
189
190 sunxi_wdt->wdt_dev.info = &sunxi_wdt_info;
191 sunxi_wdt->wdt_dev.ops = &sunxi_wdt_ops;
192 sunxi_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT;
193 sunxi_wdt->wdt_dev.max_timeout = WDT_MAX_TIMEOUT;
194 sunxi_wdt->wdt_dev.min_timeout = WDT_MIN_TIMEOUT;
195 sunxi_wdt->wdt_dev.parent = &pdev->dev;
196
197 watchdog_init_timeout(&sunxi_wdt->wdt_dev, timeout, &pdev->dev);
198 watchdog_set_nowayout(&sunxi_wdt->wdt_dev, nowayout);
199
200 watchdog_set_drvdata(&sunxi_wdt->wdt_dev, sunxi_wdt);
201
202 sunxi_wdt_stop(&sunxi_wdt->wdt_dev);
203
204 err = watchdog_register_device(&sunxi_wdt->wdt_dev);
205 if (unlikely(err))
206 return err;
207
Maxime Ripard440e96b2014-05-06 21:44:19 -0500208 reboot_wdt_base = sunxi_wdt->wdt_base;
209 arm_pm_restart = sun4i_wdt_restart;
210
Carlo Caioned00680e2013-07-30 21:20:46 +0200211 dev_info(&pdev->dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)",
212 sunxi_wdt->wdt_dev.timeout, nowayout);
213
214 return 0;
215}
216
Maxime Ripard1d5898b2013-10-05 16:20:17 +0200217static int sunxi_wdt_remove(struct platform_device *pdev)
Carlo Caioned00680e2013-07-30 21:20:46 +0200218{
219 struct sunxi_wdt_dev *sunxi_wdt = platform_get_drvdata(pdev);
220
Maxime Ripard440e96b2014-05-06 21:44:19 -0500221 arm_pm_restart = NULL;
222
Carlo Caioned00680e2013-07-30 21:20:46 +0200223 watchdog_unregister_device(&sunxi_wdt->wdt_dev);
224 watchdog_set_drvdata(&sunxi_wdt->wdt_dev, NULL);
225
226 return 0;
227}
228
229static void sunxi_wdt_shutdown(struct platform_device *pdev)
230{
231 struct sunxi_wdt_dev *sunxi_wdt = platform_get_drvdata(pdev);
232
233 sunxi_wdt_stop(&sunxi_wdt->wdt_dev);
234}
235
236static const struct of_device_id sunxi_wdt_dt_ids[] = {
Maxime Ripardb0f1d8b2014-02-07 22:29:24 +0100237 { .compatible = "allwinner,sun4i-a10-wdt" },
Carlo Caioned00680e2013-07-30 21:20:46 +0200238 { /* sentinel */ }
239};
240MODULE_DEVICE_TABLE(of, sunxi_wdt_dt_ids);
241
242static struct platform_driver sunxi_wdt_driver = {
243 .probe = sunxi_wdt_probe,
244 .remove = sunxi_wdt_remove,
245 .shutdown = sunxi_wdt_shutdown,
246 .driver = {
247 .owner = THIS_MODULE,
248 .name = DRV_NAME,
Sachin Kamat85eee812013-09-30 10:12:51 +0530249 .of_match_table = sunxi_wdt_dt_ids,
Carlo Caioned00680e2013-07-30 21:20:46 +0200250 },
251};
252
253module_platform_driver(sunxi_wdt_driver);
254
255module_param(timeout, uint, 0);
256MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
257
258module_param(nowayout, bool, 0);
259MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
260 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
261
262MODULE_LICENSE("GPL");
263MODULE_AUTHOR("Carlo Caione <carlo.caione@gmail.com>");
264MODULE_AUTHOR("Henrik Nordstrom <henrik@henriknordstrom.net>");
265MODULE_DESCRIPTION("sunxi WatchDog Timer Driver");
266MODULE_VERSION(DRV_VERSION);