2 * Watchdog driver for Atmel AT32AP700X devices
4 * Copyright (C) 2005-2006 Atmel Corporation
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.
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>
17 #include <linux/platform_device.h>
18 #include <linux/watchdog.h>
19 #include <linux/uaccess.h>
23 #define TIMEOUT_DEFAULT CONFIG_AT32AP700X_WDT_TIMEOUT
26 /* Watchdog registers and write/read macro */
29 #define WDT_CTRL_PSEL 8
30 #define WDT_CTRL_KEY 24
34 #define WDT_BIT(name) (1 << WDT_##name)
35 #define WDT_BF(name, value) ((value) << WDT_##name)
37 #define wdt_readl(dev, reg) \
38 __raw_readl((dev)->regs + WDT_##reg)
39 #define wdt_writel(dev, reg, value) \
40 __raw_writel((value), (dev)->regs + WDT_##reg)
42 struct wdt_at32ap700x
{
46 struct miscdevice miscdev
;
49 static struct wdt_at32ap700x
*wdt
;
52 * Disable the watchdog.
54 static inline void at32_wdt_stop(void)
56 unsigned long psel
= wdt_readl(wdt
, CTRL
) & WDT_BF(CTRL_PSEL
, 0x0f);
57 wdt_writel(wdt
, CTRL
, psel
| WDT_BF(CTRL_KEY
, 0x55));
58 wdt_writel(wdt
, CTRL
, psel
| WDT_BF(CTRL_KEY
, 0xaa));
62 * Enable and reset the watchdog.
64 static inline void at32_wdt_start(void)
66 /* 0xf is 2^16 divider = 2 sec, 0xe is 2^15 divider = 1 sec */
67 unsigned long psel
= (wdt
->timeout
> 1) ? 0xf : 0xe;
69 wdt_writel(wdt
, CTRL
, WDT_BIT(CTRL_EN
)
70 | WDT_BF(CTRL_PSEL
, psel
)
71 | WDT_BF(CTRL_KEY
, 0x55));
72 wdt_writel(wdt
, CTRL
, WDT_BIT(CTRL_EN
)
73 | WDT_BF(CTRL_PSEL
, psel
)
74 | WDT_BF(CTRL_KEY
, 0xaa));
78 * Pat the watchdog timer.
80 static inline void at32_wdt_pat(void)
82 wdt_writel(wdt
, CLR
, 0x42);
86 * Watchdog device is opened, and watchdog starts running.
88 static int at32_wdt_open(struct inode
*inode
, struct file
*file
)
90 if (test_and_set_bit(1, &wdt
->users
))
94 return nonseekable_open(inode
, file
);
98 * Close the watchdog device. If CONFIG_WATCHDOG_NOWAYOUT is _not_ defined then
99 * the watchdog is also disabled.
101 static int at32_wdt_close(struct inode
*inode
, struct file
*file
)
103 #ifndef CONFIG_WATCHDOG_NOWAYOUT
106 clear_bit(1, &wdt
->users
);
111 * Change the watchdog time interval.
113 static int at32_wdt_settimeout(int time
)
116 * All counting occurs at 1 / SLOW_CLOCK (32 kHz) and max prescaler is
117 * 2 ^ 16 allowing up to 2 seconds timeout.
119 if ((time
< TIMEOUT_MIN
) || (time
> TIMEOUT_MAX
))
123 * Set new watchdog time. It will be used when at32_wdt_start() is
130 static struct watchdog_info at32_wdt_info
= {
131 .identity
= "at32ap700x watchdog",
132 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
,
136 * Handle commands from user-space.
138 static int at32_wdt_ioctl(struct inode
*inode
, struct file
*file
,
139 unsigned int cmd
, unsigned long arg
)
143 void __user
*argp
= (void __user
*)arg
;
144 int __user
*p
= argp
;
147 case WDIOC_KEEPALIVE
:
151 case WDIOC_GETSUPPORT
:
152 ret
= copy_to_user(argp
, &at32_wdt_info
,
153 sizeof(at32_wdt_info
)) ? -EFAULT
: 0;
155 case WDIOC_SETTIMEOUT
:
156 ret
= get_user(time
, p
);
159 ret
= at32_wdt_settimeout(time
);
162 /* Enable new time value */
165 case WDIOC_GETTIMEOUT
:
166 ret
= put_user(wdt
->timeout
, p
);
168 case WDIOC_GETSTATUS
: /* fall through */
169 case WDIOC_GETBOOTSTATUS
:
170 ret
= put_user(0, p
);
172 case WDIOC_SETOPTIONS
:
173 ret
= get_user(time
, p
);
176 if (time
& WDIOS_DISABLECARD
)
178 if (time
& WDIOS_ENABLECARD
)
187 static ssize_t
at32_wdt_write(struct file
*file
, const char *data
, size_t len
,
194 static const struct file_operations at32_wdt_fops
= {
195 .owner
= THIS_MODULE
,
197 .ioctl
= at32_wdt_ioctl
,
198 .open
= at32_wdt_open
,
199 .release
= at32_wdt_close
,
200 .write
= at32_wdt_write
,
203 static int __init
at32_wdt_probe(struct platform_device
*pdev
)
205 struct resource
*regs
;
209 dev_dbg(&pdev
->dev
, "only 1 wdt instance supported.\n");
213 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
215 dev_dbg(&pdev
->dev
, "missing mmio resource\n");
219 wdt
= kzalloc(sizeof(struct wdt_at32ap700x
), GFP_KERNEL
);
221 dev_dbg(&pdev
->dev
, "no memory for wdt structure\n");
225 wdt
->regs
= ioremap(regs
->start
, regs
->end
- regs
->start
+ 1);
228 dev_dbg(&pdev
->dev
, "could not map I/O memory\n");
232 wdt
->miscdev
.minor
= WATCHDOG_MINOR
;
233 wdt
->miscdev
.name
= "watchdog";
234 wdt
->miscdev
.fops
= &at32_wdt_fops
;
236 if (at32_wdt_settimeout(TIMEOUT_DEFAULT
)) {
237 at32_wdt_settimeout(TIMEOUT_MAX
);
239 "default timeout invalid, set to %d sec.\n",
243 ret
= misc_register(&wdt
->miscdev
);
245 dev_dbg(&pdev
->dev
, "failed to register wdt miscdev\n");
249 platform_set_drvdata(pdev
, wdt
);
250 wdt
->miscdev
.parent
= &pdev
->dev
;
251 dev_info(&pdev
->dev
, "AT32AP700X WDT at 0x%p\n", wdt
->regs
);
263 static int __exit
at32_wdt_remove(struct platform_device
*pdev
)
265 if (wdt
&& platform_get_drvdata(pdev
) == wdt
) {
266 misc_deregister(&wdt
->miscdev
);
270 platform_set_drvdata(pdev
, NULL
);
276 static void at32_wdt_shutdown(struct platform_device
*pdev
)
282 static int at32_wdt_suspend(struct platform_device
*pdev
, pm_message_t message
)
288 static int at32_wdt_resume(struct platform_device
*pdev
)
295 #define at32_wdt_suspend NULL
296 #define at32_wdt_resume NULL
299 static struct platform_driver at32_wdt_driver
= {
300 .remove
= __exit_p(at32_wdt_remove
),
301 .suspend
= at32_wdt_suspend
,
302 .resume
= at32_wdt_resume
,
305 .owner
= THIS_MODULE
,
307 .shutdown
= at32_wdt_shutdown
,
310 static int __init
at32_wdt_init(void)
312 return platform_driver_probe(&at32_wdt_driver
, at32_wdt_probe
);
314 module_init(at32_wdt_init
);
316 static void __exit
at32_wdt_exit(void)
318 platform_driver_unregister(&at32_wdt_driver
);
320 module_exit(at32_wdt_exit
);
322 MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
323 MODULE_DESCRIPTION("Watchdog driver for Atmel AT32AP700X");
324 MODULE_LICENSE("GPL");
325 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);