diff options
author | Hans-Christian Egtvedt <hcegtvedt@atmel.com> | 2007-06-07 19:06:41 -0400 |
---|---|---|
committer | Wim Van Sebroeck <wim@iguana.be> | 2007-06-10 15:49:20 -0400 |
commit | a9cb3959ace112295fdb65c99a2928eedba06926 (patch) | |
tree | 40767f7c8fb62ac6da3f2657204c87a343837f28 /drivers/char/watchdog/at32ap700x_wdt.c | |
parent | 10a29304f496ddef58473e6c53b5e66d9685536d (diff) |
[WATCHDOG] Watchdog driver for AT32AP700X devices
Add support for the built in watchdog in AT32AP700X devices.
Tested on AT32AP7000 and ATSTK1000.
Hardware documentation can be found in the AT32AP7000 datasheet.
Signed-off-by: Hans-Christian Egtvedt <hcegtvedt@atmel.com>
Signed-off-by: Haavard Skinnemoen <hskinnemoen@atmel.com>
Signed-off-by: Wim Van Sebroeck <wim@iguana.be>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Diffstat (limited to 'drivers/char/watchdog/at32ap700x_wdt.c')
-rw-r--r-- | drivers/char/watchdog/at32ap700x_wdt.c | 313 |
1 files changed, 313 insertions, 0 deletions
diff --git a/drivers/char/watchdog/at32ap700x_wdt.c b/drivers/char/watchdog/at32ap700x_wdt.c new file mode 100644 index 00000000000..036d83bcb50 --- /dev/null +++ b/drivers/char/watchdog/at32ap700x_wdt.c | |||
@@ -0,0 +1,313 @@ | |||
1 | /* | ||
2 | * Watchdog driver for Atmel AT32AP700X devices | ||
3 | * | ||
4 | * Copyright (C) 2005-2006 Atmel Corporation | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License version 2 as | ||
8 | * published by the Free Software Foundation. | ||
9 | */ | ||
10 | |||
11 | #include <linux/init.h> | ||
12 | #include <linux/kernel.h> | ||
13 | #include <linux/module.h> | ||
14 | #include <linux/moduleparam.h> | ||
15 | #include <linux/miscdevice.h> | ||
16 | #include <linux/fs.h> | ||
17 | #include <linux/platform_device.h> | ||
18 | #include <linux/watchdog.h> | ||
19 | |||
20 | #include <asm/uaccess.h> | ||
21 | #include <asm/io.h> | ||
22 | |||
23 | #define TIMEOUT_MIN 1 | ||
24 | #define TIMEOUT_DEFAULT CONFIG_AT32AP700X_WDT_TIMEOUT | ||
25 | #define TIMEOUT_MAX 2 | ||
26 | |||
27 | /* Watchdog registers and write/read macro */ | ||
28 | #define WDT_CTRL 0x00 | ||
29 | #define WDT_CTRL_EN 0 | ||
30 | #define WDT_CTRL_PSEL 8 | ||
31 | #define WDT_CTRL_KEY 24 | ||
32 | |||
33 | #define WDT_CLR 0x04 | ||
34 | |||
35 | #define WDT_BIT(name) (1 << WDT_##name) | ||
36 | #define WDT_BF(name,value) ((value) << WDT_##name) | ||
37 | |||
38 | #define wdt_readl(dev,reg) \ | ||
39 | __raw_readl((dev)->regs + WDT_##reg) | ||
40 | #define wdt_writel(dev,reg,value) \ | ||
41 | __raw_writel((value), (dev)->regs + WDT_##reg) | ||
42 | |||
43 | struct wdt_at32ap700x { | ||
44 | void __iomem *regs; | ||
45 | int timeout; | ||
46 | int users; | ||
47 | struct miscdevice miscdev; | ||
48 | }; | ||
49 | |||
50 | static struct wdt_at32ap700x *wdt; | ||
51 | |||
52 | /* | ||
53 | * Disable the watchdog. | ||
54 | */ | ||
55 | static void inline at32_wdt_stop(void) | ||
56 | { | ||
57 | unsigned long psel = wdt_readl(wdt, CTRL) & WDT_BF(CTRL_PSEL, 0x0f); | ||
58 | wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0x55)); | ||
59 | wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0xaa)); | ||
60 | } | ||
61 | |||
62 | /* | ||
63 | * Enable and reset the watchdog. | ||
64 | */ | ||
65 | static void inline at32_wdt_start(void) | ||
66 | { | ||
67 | /* 0xf is 2^16 divider = 2 sec, 0xe is 2^15 divider = 1 sec */ | ||
68 | unsigned long psel = (wdt->timeout > 1) ? 0xf : 0xe; | ||
69 | |||
70 | wdt_writel(wdt, CTRL, WDT_BIT(CTRL_EN) | ||
71 | | WDT_BF(CTRL_PSEL, psel) | ||
72 | | WDT_BF(CTRL_KEY, 0x55)); | ||
73 | wdt_writel(wdt, CTRL, WDT_BIT(CTRL_EN) | ||
74 | | WDT_BF(CTRL_PSEL, psel) | ||
75 | | WDT_BF(CTRL_KEY, 0xaa)); | ||
76 | } | ||
77 | |||
78 | /* | ||
79 | * Pat the watchdog timer. | ||
80 | */ | ||
81 | static void inline at32_wdt_pat(void) | ||
82 | { | ||
83 | wdt_writel(wdt, CLR, 0x42); | ||
84 | } | ||
85 | |||
86 | /* | ||
87 | * Watchdog device is opened, and watchdog starts running. | ||
88 | */ | ||
89 | static int at32_wdt_open(struct inode *inode, struct file *file) | ||
90 | { | ||
91 | if (test_and_set_bit(1, &wdt->users)) | ||
92 | return -EBUSY; | ||
93 | |||
94 | at32_wdt_start(); | ||
95 | return nonseekable_open(inode, file); | ||
96 | } | ||
97 | |||
98 | /* | ||
99 | * Close the watchdog device. If CONFIG_WATCHDOG_NOWAYOUT is _not_ defined then | ||
100 | * the watchdog is also disabled. | ||
101 | */ | ||
102 | static int at32_wdt_close(struct inode *inode, struct file *file) | ||
103 | { | ||
104 | #ifndef CONFIG_WATCHDOG_NOWAYOUT | ||
105 | at32_wdt_stop(); | ||
106 | #endif | ||
107 | clear_bit(1, &wdt->users); | ||
108 | return 0; | ||
109 | } | ||
110 | |||
111 | /* | ||
112 | * Change the watchdog time interval. | ||
113 | */ | ||
114 | static int at32_wdt_settimeout(int time) | ||
115 | { | ||
116 | /* | ||
117 | * All counting occurs at 1 / SLOW_CLOCK (32 kHz) and max prescaler is | ||
118 | * 2 ^ 16 allowing up to 2 seconds timeout. | ||
119 | */ | ||
120 | if ((time < TIMEOUT_MIN) || (time > TIMEOUT_MAX)) | ||
121 | return -EINVAL; | ||
122 | |||
123 | /* Set new watchdog time. It will be used when at32_wdt_start() is called. */ | ||
124 | wdt->timeout = time; | ||
125 | return 0; | ||
126 | } | ||
127 | |||
128 | static struct watchdog_info at32_wdt_info = { | ||
129 | .identity = "at32ap700x watchdog", | ||
130 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING, | ||
131 | }; | ||
132 | |||
133 | /* | ||
134 | * Handle commands from user-space. | ||
135 | */ | ||
136 | static int at32_wdt_ioctl(struct inode *inode, struct file *file, | ||
137 | unsigned int cmd, unsigned long arg) | ||
138 | { | ||
139 | int ret = -ENOTTY; | ||
140 | int time; | ||
141 | void __user *argp = (void __user *)arg; | ||
142 | int __user *p = argp; | ||
143 | |||
144 | switch(cmd) { | ||
145 | case WDIOC_KEEPALIVE: | ||
146 | at32_wdt_pat(); | ||
147 | ret = 0; | ||
148 | break; | ||
149 | case WDIOC_GETSUPPORT: | ||
150 | ret = copy_to_user(argp, &at32_wdt_info, | ||
151 | sizeof(at32_wdt_info)) ? -EFAULT : 0; | ||
152 | break; | ||
153 | case WDIOC_SETTIMEOUT: | ||
154 | ret = get_user(time, p); | ||
155 | if (ret) | ||
156 | break; | ||
157 | ret = at32_wdt_settimeout(time); | ||
158 | if (ret) | ||
159 | break; | ||
160 | /* Enable new time value */ | ||
161 | at32_wdt_start(); | ||
162 | /* fall through */ | ||
163 | case WDIOC_GETTIMEOUT: | ||
164 | ret = put_user(wdt->timeout, p); | ||
165 | break; | ||
166 | case WDIOC_GETSTATUS: /* fall through */ | ||
167 | case WDIOC_GETBOOTSTATUS: | ||
168 | ret = put_user(0, p); | ||
169 | break; | ||
170 | case WDIOC_SETOPTIONS: | ||
171 | ret = get_user(time, p); | ||
172 | if (ret) | ||
173 | break; | ||
174 | if (time & WDIOS_DISABLECARD) | ||
175 | at32_wdt_stop(); | ||
176 | if (time & WDIOS_ENABLECARD) | ||
177 | at32_wdt_start(); | ||
178 | ret = 0; | ||
179 | break; | ||
180 | } | ||
181 | |||
182 | return ret; | ||
183 | } | ||
184 | |||
185 | static ssize_t at32_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos) | ||
186 | { | ||
187 | at32_wdt_pat(); | ||
188 | return len; | ||
189 | } | ||
190 | |||
191 | static const struct file_operations at32_wdt_fops = { | ||
192 | .owner = THIS_MODULE, | ||
193 | .llseek = no_llseek, | ||
194 | .ioctl = at32_wdt_ioctl, | ||
195 | .open = at32_wdt_open, | ||
196 | .release = at32_wdt_close, | ||
197 | .write = at32_wdt_write, | ||
198 | }; | ||
199 | |||
200 | static int __init at32_wdt_probe(struct platform_device *pdev) | ||
201 | { | ||
202 | struct resource *regs; | ||
203 | int ret; | ||
204 | |||
205 | if (wdt) { | ||
206 | dev_dbg(&pdev->dev, "only 1 wdt instance supported.\n"); | ||
207 | return -EBUSY; | ||
208 | } | ||
209 | |||
210 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); | ||
211 | if (!regs) { | ||
212 | dev_dbg(&pdev->dev, "missing mmio resource\n"); | ||
213 | return -ENXIO; | ||
214 | } | ||
215 | |||
216 | wdt = kzalloc(sizeof(struct wdt_at32ap700x), GFP_KERNEL); | ||
217 | if (!wdt) { | ||
218 | dev_dbg(&pdev->dev, "no memory for wdt structure\n"); | ||
219 | return -ENOMEM; | ||
220 | } | ||
221 | |||
222 | wdt->regs = ioremap(regs->start, regs->end - regs->start + 1); | ||
223 | wdt->users = 0; | ||
224 | wdt->miscdev.minor = WATCHDOG_MINOR; | ||
225 | wdt->miscdev.name = "watchdog"; | ||
226 | wdt->miscdev.fops = &at32_wdt_fops; | ||
227 | |||
228 | if (at32_wdt_settimeout(TIMEOUT_DEFAULT)) { | ||
229 | at32_wdt_settimeout(TIMEOUT_MAX); | ||
230 | dev_dbg(&pdev->dev, | ||
231 | "default timeout invalid, set to %d sec.\n", | ||
232 | TIMEOUT_MAX); | ||
233 | } | ||
234 | |||
235 | ret = misc_register(&wdt->miscdev); | ||
236 | if (ret) { | ||
237 | dev_dbg(&pdev->dev, "failed to register wdt miscdev\n"); | ||
238 | goto err_register; | ||
239 | } | ||
240 | |||
241 | platform_set_drvdata(pdev, wdt); | ||
242 | wdt->miscdev.parent = &pdev->dev; | ||
243 | dev_info(&pdev->dev, "AT32AP700X WDT at 0x%p\n", wdt->regs); | ||
244 | |||
245 | return 0; | ||
246 | |||
247 | err_register: | ||
248 | kfree(wdt); | ||
249 | wdt = NULL; | ||
250 | return ret; | ||
251 | } | ||
252 | |||
253 | static int __exit at32_wdt_remove(struct platform_device *pdev) | ||
254 | { | ||
255 | if (wdt && platform_get_drvdata(pdev) == wdt) { | ||
256 | misc_deregister(&wdt->miscdev); | ||
257 | kfree(wdt); | ||
258 | wdt = NULL; | ||
259 | platform_set_drvdata(pdev, NULL); | ||
260 | } | ||
261 | |||
262 | return 0; | ||
263 | } | ||
264 | |||
265 | static void at32_wdt_shutdown(struct platform_device *pdev) | ||
266 | { | ||
267 | at32_wdt_stop(); | ||
268 | } | ||
269 | |||
270 | #ifdef CONFIG_PM | ||
271 | static int at32_wdt_suspend(struct platform_device *pdev, pm_message_t message) | ||
272 | { | ||
273 | at32_wdt_stop(); | ||
274 | return 0; | ||
275 | } | ||
276 | |||
277 | static int at32_wdt_resume(struct platform_device *pdev) | ||
278 | { | ||
279 | if (wdt->users) | ||
280 | at32_wdt_start(); | ||
281 | return 0; | ||
282 | } | ||
283 | #endif | ||
284 | |||
285 | static struct platform_driver at32_wdt_driver = { | ||
286 | .remove = __exit_p(at32_wdt_remove), | ||
287 | #ifdef CONFIG_PM | ||
288 | .suspend = at32_wdt_suspend, | ||
289 | .resume = at32_wdt_resume, | ||
290 | #endif | ||
291 | .driver = { | ||
292 | .name = "at32_wdt", | ||
293 | .owner = THIS_MODULE, | ||
294 | }, | ||
295 | .shutdown = at32_wdt_shutdown, | ||
296 | }; | ||
297 | |||
298 | static int __init at32_wdt_init(void) | ||
299 | { | ||
300 | return platform_driver_probe(&at32_wdt_driver, at32_wdt_probe); | ||
301 | } | ||
302 | module_init(at32_wdt_init); | ||
303 | |||
304 | static void __exit at32_wdt_exit(void) | ||
305 | { | ||
306 | platform_driver_unregister(&at32_wdt_driver); | ||
307 | } | ||
308 | module_exit(at32_wdt_exit); | ||
309 | |||
310 | MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>"); | ||
311 | MODULE_DESCRIPTION("Watchdog driver for Atmel AT32AP700X"); | ||
312 | MODULE_LICENSE("GPL"); | ||
313 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); | ||