1 #include <linux/init.h>
2 #include <linux/module.h>
3 #include <linux/miscdevice.h>
4 #include <linux/watchdog.h>
6 #include <linux/spinlock.h>
7 #include <linux/of_platform.h>
8 #include <linux/uaccess.h>
9 #include <asm/mpc52xx.h>
12 #define GPT_MODE_WDT (1<<15)
13 #define GPT_MODE_CE (1<<12)
14 #define GPT_MODE_MS_TIMER (0x4)
18 unsigned count
; /* timer ticks before watchdog kicks in */
20 struct miscdevice miscdev
;
22 struct mpc52xx_gpt __iomem
*regs
;
26 /* is_active stores wether or not the /dev/watchdog device is opened */
27 static unsigned long is_active
;
29 /* misc devices don't provide a way, to get back to 'dev' or 'miscdev' from
30 * file operations, which sucks. But there can be max 1 watchdog anyway, so...
32 static struct mpc5200_wdt
*wdt_global
;
35 /* helper to calculate timeout in timer counts */
36 static void mpc5200_wdt_set_timeout(struct mpc5200_wdt
*wdt
, int timeout
)
38 /* use biggest prescaler of 64k */
39 wdt
->count
= (wdt
->ipb_freq
+ 0xffff) / 0x10000 * timeout
;
41 if (wdt
->count
> 0xffff)
44 /* return timeout in seconds (calculated from timer count) */
45 static int mpc5200_wdt_get_timeout(struct mpc5200_wdt
*wdt
)
47 return wdt
->count
* 0x10000 / wdt
->ipb_freq
;
51 /* watchdog operations */
52 static int mpc5200_wdt_start(struct mpc5200_wdt
*wdt
)
54 spin_lock(&wdt
->io_lock
);
56 out_be32(&wdt
->regs
->mode
, 0);
57 /* set timeout, with maximum prescaler */
58 out_be32(&wdt
->regs
->count
, 0x0 | wdt
->count
);
60 out_be32(&wdt
->regs
->mode
, GPT_MODE_CE
| GPT_MODE_WDT
|
62 spin_unlock(&wdt
->io_lock
);
66 static int mpc5200_wdt_ping(struct mpc5200_wdt
*wdt
)
68 spin_lock(&wdt
->io_lock
);
69 /* writing A5 to OCPW resets the watchdog */
70 out_be32(&wdt
->regs
->mode
, 0xA5000000 |
71 (0xffffff & in_be32(&wdt
->regs
->mode
)));
72 spin_unlock(&wdt
->io_lock
);
75 static int mpc5200_wdt_stop(struct mpc5200_wdt
*wdt
)
77 spin_lock(&wdt
->io_lock
);
79 out_be32(&wdt
->regs
->mode
, 0);
80 spin_unlock(&wdt
->io_lock
);
86 static ssize_t
mpc5200_wdt_write(struct file
*file
, const char __user
*data
,
87 size_t len
, loff_t
*ppos
)
89 struct mpc5200_wdt
*wdt
= file
->private_data
;
90 mpc5200_wdt_ping(wdt
);
93 static struct watchdog_info mpc5200_wdt_info
= {
94 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
,
95 .identity
= "mpc5200 watchdog on GPT0",
97 static long mpc5200_wdt_ioctl(struct file
*file
, unsigned int cmd
,
100 struct mpc5200_wdt
*wdt
= file
->private_data
;
101 int __user
*data
= (int __user
*)arg
;
106 case WDIOC_GETSUPPORT
:
107 ret
= copy_to_user(data
, &mpc5200_wdt_info
,
108 sizeof(mpc5200_wdt_info
));
113 case WDIOC_GETSTATUS
:
114 case WDIOC_GETBOOTSTATUS
:
115 ret
= put_user(0, data
);
118 case WDIOC_KEEPALIVE
:
119 mpc5200_wdt_ping(wdt
);
122 case WDIOC_SETTIMEOUT
:
123 ret
= get_user(timeout
, data
);
126 mpc5200_wdt_set_timeout(wdt
, timeout
);
127 mpc5200_wdt_start(wdt
);
128 /* fall through and return the timeout */
130 case WDIOC_GETTIMEOUT
:
131 timeout
= mpc5200_wdt_get_timeout(wdt
);
132 ret
= put_user(timeout
, data
);
141 static int mpc5200_wdt_open(struct inode
*inode
, struct file
*file
)
143 /* /dev/watchdog can only be opened once */
144 if (test_and_set_bit(0, &is_active
))
147 /* Set and activate the watchdog */
148 mpc5200_wdt_set_timeout(wdt_global
, 30);
149 mpc5200_wdt_start(wdt_global
);
150 file
->private_data
= wdt_global
;
151 return nonseekable_open(inode
, file
);
153 static int mpc5200_wdt_release(struct inode
*inode
, struct file
*file
)
155 #if WATCHDOG_NOWAYOUT == 0
156 struct mpc5200_wdt
*wdt
= file
->private_data
;
157 mpc5200_wdt_stop(wdt
);
158 wdt
->count
= 0; /* == disabled */
160 clear_bit(0, &is_active
);
164 static const struct file_operations mpc5200_wdt_fops
= {
165 .owner
= THIS_MODULE
,
166 .write
= mpc5200_wdt_write
,
167 .unlocked_ioctl
= mpc5200_wdt_ioctl
,
168 .open
= mpc5200_wdt_open
,
169 .release
= mpc5200_wdt_release
,
172 /* module operations */
173 static int mpc5200_wdt_probe(struct of_device
*op
,
174 const struct of_device_id
*match
)
176 struct mpc5200_wdt
*wdt
;
181 has_wdt
= of_get_property(op
->node
, "has-wdt", NULL
);
183 has_wdt
= of_get_property(op
->node
, "fsl,has-wdt", NULL
);
187 wdt
= kzalloc(sizeof(*wdt
), GFP_KERNEL
);
191 wdt
->ipb_freq
= mpc52xx_find_ipb_freq(op
->node
);
193 err
= of_address_to_resource(op
->node
, 0, &wdt
->mem
);
196 size
= wdt
->mem
.end
- wdt
->mem
.start
+ 1;
197 if (!request_mem_region(wdt
->mem
.start
, size
, "mpc5200_wdt")) {
201 wdt
->regs
= ioremap(wdt
->mem
.start
, size
);
207 dev_set_drvdata(&op
->dev
, wdt
);
208 spin_lock_init(&wdt
->io_lock
);
210 wdt
->miscdev
= (struct miscdevice
) {
211 .minor
= WATCHDOG_MINOR
,
213 .fops
= &mpc5200_wdt_fops
,
217 err
= misc_register(&wdt
->miscdev
);
223 release_mem_region(wdt
->mem
.start
, size
);
229 static int mpc5200_wdt_remove(struct of_device
*op
)
231 struct mpc5200_wdt
*wdt
= dev_get_drvdata(&op
->dev
);
233 mpc5200_wdt_stop(wdt
);
234 misc_deregister(&wdt
->miscdev
);
236 release_mem_region(wdt
->mem
.start
, wdt
->mem
.end
- wdt
->mem
.start
+ 1);
241 static int mpc5200_wdt_suspend(struct of_device
*op
, pm_message_t state
)
243 struct mpc5200_wdt
*wdt
= dev_get_drvdata(&op
->dev
);
244 mpc5200_wdt_stop(wdt
);
247 static int mpc5200_wdt_resume(struct of_device
*op
)
249 struct mpc5200_wdt
*wdt
= dev_get_drvdata(&op
->dev
);
251 mpc5200_wdt_start(wdt
);
254 static int mpc5200_wdt_shutdown(struct of_device
*op
)
256 struct mpc5200_wdt
*wdt
= dev_get_drvdata(&op
->dev
);
257 mpc5200_wdt_stop(wdt
);
261 static struct of_device_id mpc5200_wdt_match
[] = {
262 { .compatible
= "mpc5200-gpt", },
263 { .compatible
= "fsl,mpc5200-gpt", },
266 static struct of_platform_driver mpc5200_wdt_driver
= {
267 .owner
= THIS_MODULE
,
268 .name
= "mpc5200-gpt-wdt",
269 .match_table
= mpc5200_wdt_match
,
270 .probe
= mpc5200_wdt_probe
,
271 .remove
= mpc5200_wdt_remove
,
272 .suspend
= mpc5200_wdt_suspend
,
273 .resume
= mpc5200_wdt_resume
,
274 .shutdown
= mpc5200_wdt_shutdown
,
278 static int __init
mpc5200_wdt_init(void)
280 return of_register_platform_driver(&mpc5200_wdt_driver
);
283 static void __exit
mpc5200_wdt_exit(void)
285 of_unregister_platform_driver(&mpc5200_wdt_driver
);
288 module_init(mpc5200_wdt_init
);
289 module_exit(mpc5200_wdt_exit
);
291 MODULE_AUTHOR("Domen Puncer <domen.puncer@telargo.com>");
292 MODULE_LICENSE("Dual BSD/GPL");
293 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);