Loading...
1/*
2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License version 2 as published
4 * by the Free Software Foundation.
5 *
6 * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
7 * Based on EP93xx wdt driver
8 */
9
10#include <linux/module.h>
11#include <linux/fs.h>
12#include <linux/miscdevice.h>
13#include <linux/watchdog.h>
14#include <linux/platform_device.h>
15#include <linux/uaccess.h>
16#include <linux/clk.h>
17#include <linux/io.h>
18
19#include <lantiq.h>
20
21/* Section 3.4 of the datasheet
22 * The password sequence protects the WDT control register from unintended
23 * write actions, which might cause malfunction of the WDT.
24 *
25 * essentially the following two magic passwords need to be written to allow
26 * IO access to the WDT core
27 */
28#define LTQ_WDT_PW1 0x00BE0000
29#define LTQ_WDT_PW2 0x00DC0000
30
31#define LTQ_WDT_CR 0x0 /* watchdog control register */
32#define LTQ_WDT_SR 0x8 /* watchdog status register */
33
34#define LTQ_WDT_SR_EN (0x1 << 31) /* enable bit */
35#define LTQ_WDT_SR_PWD (0x3 << 26) /* turn on power */
36#define LTQ_WDT_SR_CLKDIV (0x3 << 24) /* turn on clock and set */
37 /* divider to 0x40000 */
38#define LTQ_WDT_DIVIDER 0x40000
39#define LTQ_MAX_TIMEOUT ((1 << 16) - 1) /* the reload field is 16 bit */
40
41static int nowayout = WATCHDOG_NOWAYOUT;
42
43static void __iomem *ltq_wdt_membase;
44static unsigned long ltq_io_region_clk_rate;
45
46static unsigned long ltq_wdt_bootstatus;
47static unsigned long ltq_wdt_in_use;
48static int ltq_wdt_timeout = 30;
49static int ltq_wdt_ok_to_close;
50
51static void
52ltq_wdt_enable(void)
53{
54 unsigned long int timeout = ltq_wdt_timeout *
55 (ltq_io_region_clk_rate / LTQ_WDT_DIVIDER) + 0x1000;
56 if (timeout > LTQ_MAX_TIMEOUT)
57 timeout = LTQ_MAX_TIMEOUT;
58
59 /* write the first password magic */
60 ltq_w32(LTQ_WDT_PW1, ltq_wdt_membase + LTQ_WDT_CR);
61 /* write the second magic plus the configuration and new timeout */
62 ltq_w32(LTQ_WDT_SR_EN | LTQ_WDT_SR_PWD | LTQ_WDT_SR_CLKDIV |
63 LTQ_WDT_PW2 | timeout, ltq_wdt_membase + LTQ_WDT_CR);
64}
65
66static void
67ltq_wdt_disable(void)
68{
69 /* write the first password magic */
70 ltq_w32(LTQ_WDT_PW1, ltq_wdt_membase + LTQ_WDT_CR);
71 /* write the second password magic with no config
72 * this turns the watchdog off
73 */
74 ltq_w32(LTQ_WDT_PW2, ltq_wdt_membase + LTQ_WDT_CR);
75}
76
77static ssize_t
78ltq_wdt_write(struct file *file, const char __user *data,
79 size_t len, loff_t *ppos)
80{
81 if (len) {
82 if (!nowayout) {
83 size_t i;
84
85 ltq_wdt_ok_to_close = 0;
86 for (i = 0; i != len; i++) {
87 char c;
88
89 if (get_user(c, data + i))
90 return -EFAULT;
91 if (c == 'V')
92 ltq_wdt_ok_to_close = 1;
93 else
94 ltq_wdt_ok_to_close = 0;
95 }
96 }
97 ltq_wdt_enable();
98 }
99
100 return len;
101}
102
103static struct watchdog_info ident = {
104 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
105 WDIOF_CARDRESET,
106 .identity = "ltq_wdt",
107};
108
109static long
110ltq_wdt_ioctl(struct file *file,
111 unsigned int cmd, unsigned long arg)
112{
113 int ret = -ENOTTY;
114
115 switch (cmd) {
116 case WDIOC_GETSUPPORT:
117 ret = copy_to_user((struct watchdog_info __user *)arg, &ident,
118 sizeof(ident)) ? -EFAULT : 0;
119 break;
120
121 case WDIOC_GETBOOTSTATUS:
122 ret = put_user(ltq_wdt_bootstatus, (int __user *)arg);
123 break;
124
125 case WDIOC_GETSTATUS:
126 ret = put_user(0, (int __user *)arg);
127 break;
128
129 case WDIOC_SETTIMEOUT:
130 ret = get_user(ltq_wdt_timeout, (int __user *)arg);
131 if (!ret)
132 ltq_wdt_enable();
133 /* intentional drop through */
134 case WDIOC_GETTIMEOUT:
135 ret = put_user(ltq_wdt_timeout, (int __user *)arg);
136 break;
137
138 case WDIOC_KEEPALIVE:
139 ltq_wdt_enable();
140 ret = 0;
141 break;
142 }
143 return ret;
144}
145
146static int
147ltq_wdt_open(struct inode *inode, struct file *file)
148{
149 if (test_and_set_bit(0, <q_wdt_in_use))
150 return -EBUSY;
151 ltq_wdt_in_use = 1;
152 ltq_wdt_enable();
153
154 return nonseekable_open(inode, file);
155}
156
157static int
158ltq_wdt_release(struct inode *inode, struct file *file)
159{
160 if (ltq_wdt_ok_to_close)
161 ltq_wdt_disable();
162 else
163 pr_err("ltq_wdt: watchdog closed without warning\n");
164 ltq_wdt_ok_to_close = 0;
165 clear_bit(0, <q_wdt_in_use);
166
167 return 0;
168}
169
170static const struct file_operations ltq_wdt_fops = {
171 .owner = THIS_MODULE,
172 .write = ltq_wdt_write,
173 .unlocked_ioctl = ltq_wdt_ioctl,
174 .open = ltq_wdt_open,
175 .release = ltq_wdt_release,
176 .llseek = no_llseek,
177};
178
179static struct miscdevice ltq_wdt_miscdev = {
180 .minor = WATCHDOG_MINOR,
181 .name = "watchdog",
182 .fops = <q_wdt_fops,
183};
184
185static int __init
186ltq_wdt_probe(struct platform_device *pdev)
187{
188 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
189 struct clk *clk;
190
191 if (!res) {
192 dev_err(&pdev->dev, "cannot obtain I/O memory region");
193 return -ENOENT;
194 }
195 res = devm_request_mem_region(&pdev->dev, res->start,
196 resource_size(res), dev_name(&pdev->dev));
197 if (!res) {
198 dev_err(&pdev->dev, "cannot request I/O memory region");
199 return -EBUSY;
200 }
201 ltq_wdt_membase = devm_ioremap_nocache(&pdev->dev, res->start,
202 resource_size(res));
203 if (!ltq_wdt_membase) {
204 dev_err(&pdev->dev, "cannot remap I/O memory region\n");
205 return -ENOMEM;
206 }
207
208 /* we do not need to enable the clock as it is always running */
209 clk = clk_get(&pdev->dev, "io");
210 WARN_ON(!clk);
211 ltq_io_region_clk_rate = clk_get_rate(clk);
212 clk_put(clk);
213
214 if (ltq_reset_cause() == LTQ_RST_CAUSE_WDTRST)
215 ltq_wdt_bootstatus = WDIOF_CARDRESET;
216
217 return misc_register(<q_wdt_miscdev);
218}
219
220static int __devexit
221ltq_wdt_remove(struct platform_device *pdev)
222{
223 misc_deregister(<q_wdt_miscdev);
224
225 if (ltq_wdt_membase)
226 iounmap(ltq_wdt_membase);
227
228 return 0;
229}
230
231
232static struct platform_driver ltq_wdt_driver = {
233 .remove = __devexit_p(ltq_wdt_remove),
234 .driver = {
235 .name = "ltq_wdt",
236 .owner = THIS_MODULE,
237 },
238};
239
240static int __init
241init_ltq_wdt(void)
242{
243 return platform_driver_probe(<q_wdt_driver, ltq_wdt_probe);
244}
245
246static void __exit
247exit_ltq_wdt(void)
248{
249 return platform_driver_unregister(<q_wdt_driver);
250}
251
252module_init(init_ltq_wdt);
253module_exit(exit_ltq_wdt);
254
255module_param(nowayout, int, 0);
256MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
257
258MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
259MODULE_DESCRIPTION("Lantiq SoC Watchdog");
260MODULE_LICENSE("GPL");
261MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
1/*
2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License version 2 as published
4 * by the Free Software Foundation.
5 *
6 * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
7 * Based on EP93xx wdt driver
8 */
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/module.h>
13#include <linux/fs.h>
14#include <linux/miscdevice.h>
15#include <linux/watchdog.h>
16#include <linux/of_platform.h>
17#include <linux/uaccess.h>
18#include <linux/clk.h>
19#include <linux/io.h>
20
21#include <lantiq_soc.h>
22
23/*
24 * Section 3.4 of the datasheet
25 * The password sequence protects the WDT control register from unintended
26 * write actions, which might cause malfunction of the WDT.
27 *
28 * essentially the following two magic passwords need to be written to allow
29 * IO access to the WDT core
30 */
31#define LTQ_WDT_PW1 0x00BE0000
32#define LTQ_WDT_PW2 0x00DC0000
33
34#define LTQ_WDT_CR 0x0 /* watchdog control register */
35#define LTQ_WDT_SR 0x8 /* watchdog status register */
36
37#define LTQ_WDT_SR_EN (0x1 << 31) /* enable bit */
38#define LTQ_WDT_SR_PWD (0x3 << 26) /* turn on power */
39#define LTQ_WDT_SR_CLKDIV (0x3 << 24) /* turn on clock and set */
40 /* divider to 0x40000 */
41#define LTQ_WDT_DIVIDER 0x40000
42#define LTQ_MAX_TIMEOUT ((1 << 16) - 1) /* the reload field is 16 bit */
43
44static bool nowayout = WATCHDOG_NOWAYOUT;
45
46static void __iomem *ltq_wdt_membase;
47static unsigned long ltq_io_region_clk_rate;
48
49static unsigned long ltq_wdt_bootstatus;
50static unsigned long ltq_wdt_in_use;
51static int ltq_wdt_timeout = 30;
52static int ltq_wdt_ok_to_close;
53
54static void
55ltq_wdt_enable(void)
56{
57 unsigned long int timeout = ltq_wdt_timeout *
58 (ltq_io_region_clk_rate / LTQ_WDT_DIVIDER) + 0x1000;
59 if (timeout > LTQ_MAX_TIMEOUT)
60 timeout = LTQ_MAX_TIMEOUT;
61
62 /* write the first password magic */
63 ltq_w32(LTQ_WDT_PW1, ltq_wdt_membase + LTQ_WDT_CR);
64 /* write the second magic plus the configuration and new timeout */
65 ltq_w32(LTQ_WDT_SR_EN | LTQ_WDT_SR_PWD | LTQ_WDT_SR_CLKDIV |
66 LTQ_WDT_PW2 | timeout, ltq_wdt_membase + LTQ_WDT_CR);
67}
68
69static void
70ltq_wdt_disable(void)
71{
72 /* write the first password magic */
73 ltq_w32(LTQ_WDT_PW1, ltq_wdt_membase + LTQ_WDT_CR);
74 /*
75 * write the second password magic with no config
76 * this turns the watchdog off
77 */
78 ltq_w32(LTQ_WDT_PW2, ltq_wdt_membase + LTQ_WDT_CR);
79}
80
81static ssize_t
82ltq_wdt_write(struct file *file, const char __user *data,
83 size_t len, loff_t *ppos)
84{
85 if (len) {
86 if (!nowayout) {
87 size_t i;
88
89 ltq_wdt_ok_to_close = 0;
90 for (i = 0; i != len; i++) {
91 char c;
92
93 if (get_user(c, data + i))
94 return -EFAULT;
95 if (c == 'V')
96 ltq_wdt_ok_to_close = 1;
97 else
98 ltq_wdt_ok_to_close = 0;
99 }
100 }
101 ltq_wdt_enable();
102 }
103
104 return len;
105}
106
107static struct watchdog_info ident = {
108 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
109 WDIOF_CARDRESET,
110 .identity = "ltq_wdt",
111};
112
113static long
114ltq_wdt_ioctl(struct file *file,
115 unsigned int cmd, unsigned long arg)
116{
117 int ret = -ENOTTY;
118
119 switch (cmd) {
120 case WDIOC_GETSUPPORT:
121 ret = copy_to_user((struct watchdog_info __user *)arg, &ident,
122 sizeof(ident)) ? -EFAULT : 0;
123 break;
124
125 case WDIOC_GETBOOTSTATUS:
126 ret = put_user(ltq_wdt_bootstatus, (int __user *)arg);
127 break;
128
129 case WDIOC_GETSTATUS:
130 ret = put_user(0, (int __user *)arg);
131 break;
132
133 case WDIOC_SETTIMEOUT:
134 ret = get_user(ltq_wdt_timeout, (int __user *)arg);
135 if (!ret)
136 ltq_wdt_enable();
137 /* intentional drop through */
138 case WDIOC_GETTIMEOUT:
139 ret = put_user(ltq_wdt_timeout, (int __user *)arg);
140 break;
141
142 case WDIOC_KEEPALIVE:
143 ltq_wdt_enable();
144 ret = 0;
145 break;
146 }
147 return ret;
148}
149
150static int
151ltq_wdt_open(struct inode *inode, struct file *file)
152{
153 if (test_and_set_bit(0, <q_wdt_in_use))
154 return -EBUSY;
155 ltq_wdt_in_use = 1;
156 ltq_wdt_enable();
157
158 return nonseekable_open(inode, file);
159}
160
161static int
162ltq_wdt_release(struct inode *inode, struct file *file)
163{
164 if (ltq_wdt_ok_to_close)
165 ltq_wdt_disable();
166 else
167 pr_err("watchdog closed without warning\n");
168 ltq_wdt_ok_to_close = 0;
169 clear_bit(0, <q_wdt_in_use);
170
171 return 0;
172}
173
174static const struct file_operations ltq_wdt_fops = {
175 .owner = THIS_MODULE,
176 .write = ltq_wdt_write,
177 .unlocked_ioctl = ltq_wdt_ioctl,
178 .open = ltq_wdt_open,
179 .release = ltq_wdt_release,
180 .llseek = no_llseek,
181};
182
183static struct miscdevice ltq_wdt_miscdev = {
184 .minor = WATCHDOG_MINOR,
185 .name = "watchdog",
186 .fops = <q_wdt_fops,
187};
188
189static int
190ltq_wdt_probe(struct platform_device *pdev)
191{
192 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
193 struct clk *clk;
194
195 if (!res) {
196 dev_err(&pdev->dev, "cannot obtain I/O memory region");
197 return -ENOENT;
198 }
199
200 ltq_wdt_membase = devm_ioremap_resource(&pdev->dev, res);
201 if (IS_ERR(ltq_wdt_membase))
202 return PTR_ERR(ltq_wdt_membase);
203
204 /* we do not need to enable the clock as it is always running */
205 clk = clk_get_io();
206 if (IS_ERR(clk)) {
207 dev_err(&pdev->dev, "Failed to get clock\n");
208 return -ENOENT;
209 }
210 ltq_io_region_clk_rate = clk_get_rate(clk);
211 clk_put(clk);
212
213 /* find out if the watchdog caused the last reboot */
214 if (ltq_reset_cause() == LTQ_RST_CAUSE_WDTRST)
215 ltq_wdt_bootstatus = WDIOF_CARDRESET;
216
217 dev_info(&pdev->dev, "Init done\n");
218 return misc_register(<q_wdt_miscdev);
219}
220
221static int
222ltq_wdt_remove(struct platform_device *pdev)
223{
224 misc_deregister(<q_wdt_miscdev);
225
226 return 0;
227}
228
229static const struct of_device_id ltq_wdt_match[] = {
230 { .compatible = "lantiq,wdt" },
231 {},
232};
233MODULE_DEVICE_TABLE(of, ltq_wdt_match);
234
235static struct platform_driver ltq_wdt_driver = {
236 .probe = ltq_wdt_probe,
237 .remove = ltq_wdt_remove,
238 .driver = {
239 .name = "wdt",
240 .owner = THIS_MODULE,
241 .of_match_table = ltq_wdt_match,
242 },
243};
244
245module_platform_driver(ltq_wdt_driver);
246
247module_param(nowayout, bool, 0);
248MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
249MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
250MODULE_DESCRIPTION("Lantiq SoC Watchdog");
251MODULE_LICENSE("GPL");