Merge tag 'omap-for-v3.17/soc-fixes' of git://git.kernel.org/pub/scm/linux/kernel...
[deliverable/linux.git] / drivers / watchdog / sunxi_wdt.c
1 /*
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>
17 #include <linux/delay.h>
18 #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>
26 #include <linux/reboot.h>
27 #include <linux/types.h>
28 #include <linux/watchdog.h>
29
30 #include <asm/system_misc.h>
31
32 #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
47 static bool nowayout = WATCHDOG_NOWAYOUT;
48 static unsigned int timeout = WDT_MAX_TIMEOUT;
49
50 struct 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
63 static const int wdt_timeout_map[] = {
64 [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 */
75 };
76
77 static void __iomem *reboot_wdt_base;
78
79 static 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
97 static 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
107 static 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
129 static 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
139 static 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
158 static const struct watchdog_info sunxi_wdt_info = {
159 .identity = DRV_NAME,
160 .options = WDIOF_SETTIMEOUT |
161 WDIOF_KEEPALIVEPING |
162 WDIOF_MAGICCLOSE,
163 };
164
165 static 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
173 static int sunxi_wdt_probe(struct platform_device *pdev)
174 {
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
208 reboot_wdt_base = sunxi_wdt->wdt_base;
209 arm_pm_restart = sun4i_wdt_restart;
210
211 dev_info(&pdev->dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)",
212 sunxi_wdt->wdt_dev.timeout, nowayout);
213
214 return 0;
215 }
216
217 static int sunxi_wdt_remove(struct platform_device *pdev)
218 {
219 struct sunxi_wdt_dev *sunxi_wdt = platform_get_drvdata(pdev);
220
221 arm_pm_restart = NULL;
222
223 watchdog_unregister_device(&sunxi_wdt->wdt_dev);
224 watchdog_set_drvdata(&sunxi_wdt->wdt_dev, NULL);
225
226 return 0;
227 }
228
229 static 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
236 static const struct of_device_id sunxi_wdt_dt_ids[] = {
237 { .compatible = "allwinner,sun4i-a10-wdt" },
238 { /* sentinel */ }
239 };
240 MODULE_DEVICE_TABLE(of, sunxi_wdt_dt_ids);
241
242 static 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,
249 .of_match_table = sunxi_wdt_dt_ids,
250 },
251 };
252
253 module_platform_driver(sunxi_wdt_driver);
254
255 module_param(timeout, uint, 0);
256 MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
257
258 module_param(nowayout, bool, 0);
259 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
260 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
261
262 MODULE_LICENSE("GPL");
263 MODULE_AUTHOR("Carlo Caione <carlo.caione@gmail.com>");
264 MODULE_AUTHOR("Henrik Nordstrom <henrik@henriknordstrom.net>");
265 MODULE_DESCRIPTION("sunxi WatchDog Timer Driver");
266 MODULE_VERSION(DRV_VERSION);
This page took 0.044619 seconds and 5 git commands to generate.