Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  *  PNX833x Hardware Watchdog Driver
  3  *  Copyright 2008 NXP Semiconductors
  4  *  Daniel Laird <daniel.j.laird@nxp.com>
  5  *  Andre McCurdy <andre.mccurdy@nxp.com>
  6  *
  7  *  Heavily based upon - IndyDog        0.3
  8  *  A Hardware Watchdog Device for SGI IP22
  9  *
 10  * (c) Copyright 2002 Guido Guenther <agx@sigxcpu.org>, All Rights Reserved.
 11  *
 12  * This program is free software; you can redistribute it and/or
 13  * modify it under the terms of the GNU General Public License
 14  * as published by the Free Software Foundation; either version
 15  * 2 of the License, or (at your option) any later version.
 16  *
 17  * based on softdog.c by Alan Cox <alan@redhat.com>
 18  */
 19 
 20 #include <linux/module.h>
 21 #include <linux/moduleparam.h>
 22 #include <linux/types.h>
 23 #include <linux/kernel.h>
 24 #include <linux/fs.h>
 25 #include <linux/mm.h>
 26 #include <linux/miscdevice.h>
 27 #include <linux/watchdog.h>
 28 #include <linux/notifier.h>
 29 #include <linux/reboot.h>
 30 #include <linux/init.h>
 31 #include <asm/mach-pnx833x/pnx833x.h>
 32 
 33 #define PFX "pnx833x: "
 34 #define WATCHDOG_TIMEOUT 30             /* 30 sec Maximum timeout */
 35 #define WATCHDOG_COUNT_FREQUENCY 68000000U /* Watchdog counts at 68MHZ. */
 36 
 37 /** CONFIG block */
 38 #define PNX833X_CONFIG                      (0x07000U)
 39 #define PNX833X_CONFIG_CPU_WATCHDOG         (0x54)
 40 #define PNX833X_CONFIG_CPU_WATCHDOG_COMPARE (0x58)
 41 #define PNX833X_CONFIG_CPU_COUNTERS_CONTROL (0x1c)
 42 
 43 /** RESET block */
 44 #define PNX833X_RESET                       (0x08000U)
 45 #define PNX833X_RESET_CONFIG                (0x08)
 46 
 47 static int pnx833x_wdt_alive;
 48 
 49 /* Set default timeout in MHZ.*/
 50 static int pnx833x_wdt_timeout = (WATCHDOG_TIMEOUT * WATCHDOG_COUNT_FREQUENCY);
 51 module_param(pnx833x_wdt_timeout, int, 0);
 52 MODULE_PARM_DESC(timeout, "Watchdog timeout in Mhz. (68Mhz clock), default="
 53                         __MODULE_STRING(pnx833x_wdt_timeout) "(30 seconds).");
 54 
 55 static int nowayout = WATCHDOG_NOWAYOUT;
 56 module_param(nowayout, int, 0);
 57 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
 58                                         __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 59 
 60 static int start_enabled = 1;
 61 module_param(start_enabled, int, 0);
 62 MODULE_PARM_DESC(start_enabled, "Watchdog is started on module insertion "
 63                                 "(default=" __MODULE_STRING(start_enabled) ")");
 64 
 65 static void pnx833x_wdt_start(void)
 66 {
 67         /* Enable watchdog causing reset. */
 68         PNX833X_REG(PNX833X_RESET + PNX833X_RESET_CONFIG) |= 0x1;
 69         /* Set timeout.*/
 70         PNX833X_REG(PNX833X_CONFIG +
 71                 PNX833X_CONFIG_CPU_WATCHDOG_COMPARE) = pnx833x_wdt_timeout;
 72         /* Enable watchdog. */
 73         PNX833X_REG(PNX833X_CONFIG +
 74                                 PNX833X_CONFIG_CPU_COUNTERS_CONTROL) |= 0x1;
 75 
 76         printk(KERN_INFO PFX "Started watchdog timer.\n");
 77 }
 78 
 79 static void pnx833x_wdt_stop(void)
 80 {
 81         /* Disable watchdog causing reset. */
 82         PNX833X_REG(PNX833X_RESET + PNX833X_CONFIG) &= 0xFFFFFFFE;
 83         /* Disable watchdog.*/
 84         PNX833X_REG(PNX833X_CONFIG +
 85                         PNX833X_CONFIG_CPU_COUNTERS_CONTROL) &= 0xFFFFFFFE;
 86 
 87         printk(KERN_INFO PFX "Stopped watchdog timer.\n");
 88 }
 89 
 90 static void pnx833x_wdt_ping(void)
 91 {
 92         PNX833X_REG(PNX833X_CONFIG +
 93                 PNX833X_CONFIG_CPU_WATCHDOG_COMPARE) = pnx833x_wdt_timeout;
 94 }
 95 
 96 /*
 97  *      Allow only one person to hold it open
 98  */
 99 static int pnx833x_wdt_open(struct inode *inode, struct file *file)
100 {
101         if (test_and_set_bit(0, &pnx833x_wdt_alive))
102                 return -EBUSY;
103 
104         if (nowayout)
105                 __module_get(THIS_MODULE);
106 
107         /* Activate timer */
108         if (!start_enabled)
109                 pnx833x_wdt_start();
110 
111         pnx833x_wdt_ping();
112 
113         printk(KERN_INFO "Started watchdog timer.\n");
114 
115         return nonseekable_open(inode, file);
116 }
117 
118 static int pnx833x_wdt_release(struct inode *inode, struct file *file)
119 {
120         /* Shut off the timer.
121          * Lock it in if it's a module and we defined ...NOWAYOUT */
122         if (!nowayout)
123                 pnx833x_wdt_stop(); /* Turn the WDT off */
124 
125         clear_bit(0, &pnx833x_wdt_alive);
126         return 0;
127 }
128 
129 static ssize_t pnx833x_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)
130 {
131         /* Refresh the timer. */
132         if (len)
133                 pnx833x_wdt_ping();
134 
135         return len;
136 }
137 
138 static long pnx833x_wdt_ioctl(struct file *file, unsigned int cmd,
139                                                         unsigned long arg)
140 {
141         int options, new_timeout = 0;
142         uint32_t timeout, timeout_left = 0;
143 
144         static struct watchdog_info ident = {
145                 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT,
146                 .firmware_version = 0,
147                 .identity = "Hardware Watchdog for PNX833x",
148         };
149 
150         switch (cmd) {
151         default:
152                 return -ENOTTY;
153 
154         case WDIOC_GETSUPPORT:
155                 if (copy_to_user((struct watchdog_info *)arg,
156                                  &ident, sizeof(ident)))
157                         return -EFAULT;
158                 return 0;
159 
160         case WDIOC_GETSTATUS:
161         case WDIOC_GETBOOTSTATUS:
162                 return put_user(0, (int *)arg);
163 
164         case WDIOC_SETOPTIONS:
165                 if (get_user(options, (int *)arg))
166                         return -EFAULT;
167 
168                 if (options & WDIOS_DISABLECARD)
169                         pnx833x_wdt_stop();
170 
171                 if (options & WDIOS_ENABLECARD)
172                         pnx833x_wdt_start();
173 
174                 return 0;
175 
176         case WDIOC_KEEPALIVE:
177                 pnx833x_wdt_ping();
178                 return 0;
179 
180         case WDIOC_SETTIMEOUT:
181         {
182                 if (get_user(new_timeout, (int *)arg))
183                         return -EFAULT;
184 
185                 pnx833x_wdt_timeout = new_timeout;
186                 PNX833X_REG(PNX833X_CONFIG +
187                         PNX833X_CONFIG_CPU_WATCHDOG_COMPARE) = new_timeout;
188                 return put_user(new_timeout, (int *)arg);
189         }
190 
191         case WDIOC_GETTIMEOUT:
192                 timeout = PNX833X_REG(PNX833X_CONFIG +
193                                         PNX833X_CONFIG_CPU_WATCHDOG_COMPARE);
194                 return put_user(timeout, (int *)arg);
195 
196         case WDIOC_GETTIMELEFT:
197                 timeout_left = PNX833X_REG(PNX833X_CONFIG +
198                                                 PNX833X_CONFIG_CPU_WATCHDOG);
199                 return put_user(timeout_left, (int *)arg);
200 
201         }
202 }
203 
204 static int pnx833x_wdt_notify_sys(struct notifier_block *this,
205                                         unsigned long code, void *unused)
206 {
207         if (code == SYS_DOWN || code == SYS_HALT)
208                 pnx833x_wdt_stop(); /* Turn the WDT off */
209 
210         return NOTIFY_DONE;
211 }
212 
213 static const struct file_operations pnx833x_wdt_fops = {
214         .owner          = THIS_MODULE,
215         .llseek         = no_llseek,
216         .write          = pnx833x_wdt_write,
217         .unlocked_ioctl = pnx833x_wdt_ioctl,
218         .open           = pnx833x_wdt_open,
219         .release        = pnx833x_wdt_release,
220 };
221 
222 static struct miscdevice pnx833x_wdt_miscdev = {
223         .minor          = WATCHDOG_MINOR,
224         .name           = "watchdog",
225         .fops           = &pnx833x_wdt_fops,
226 };
227 
228 static struct notifier_block pnx833x_wdt_notifier = {
229         .notifier_call = pnx833x_wdt_notify_sys,
230 };
231 
232 static char banner[] __initdata =
233         KERN_INFO PFX "Hardware Watchdog Timer for PNX833x: Version 0.1\n";
234 
235 static int __init watchdog_init(void)
236 {
237         int ret, cause;
238 
239         /* Lets check the reason for the reset.*/
240         cause = PNX833X_REG(PNX833X_RESET);
241         /*If bit 31 is set then watchdog was cause of reset.*/
242         if (cause & 0x80000000) {
243                 printk(KERN_INFO PFX "The system was previously reset due to "
244                         "the watchdog firing - please investigate...\n");
245         }
246 
247         ret = register_reboot_notifier(&pnx833x_wdt_notifier);
248         if (ret) {
249                 printk(KERN_ERR PFX
250                         "cannot register reboot notifier (err=%d)\n", ret);
251                 return ret;
252         }
253 
254         ret = misc_register(&pnx833x_wdt_miscdev);
255         if (ret) {
256                 printk(KERN_ERR PFX
257                         "cannot register miscdev on minor=%d (err=%d)\n",
258                         WATCHDOG_MINOR, ret);
259                 unregister_reboot_notifier(&pnx833x_wdt_notifier);
260                 return ret;
261         }
262 
263         printk(banner);
264         if (start_enabled)
265                 pnx833x_wdt_start();
266 
267         return 0;
268 }
269 
270 static void __exit watchdog_exit(void)
271 {
272         misc_deregister(&pnx833x_wdt_miscdev);
273         unregister_reboot_notifier(&pnx833x_wdt_notifier);
274 }
275 
276 module_init(watchdog_init);
277 module_exit(watchdog_exit);
278 
279 MODULE_AUTHOR("Daniel Laird/Andre McCurdy");
280 MODULE_DESCRIPTION("Hardware Watchdog Device for PNX833x");
281 MODULE_LICENSE("GPL");
282 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
283 
  This page was automatically generated by the LXR engine.