ar7_wdt.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * drivers/watchdog/ar7_wdt.c
  3. *
  4. * Copyright (C) 2007 Nicolas Thill <nico@openwrt.org>
  5. * Copyright (c) 2005 Enrik Berkhan <Enrik.Berkhan@akk.org>
  6. *
  7. * Some code taken from:
  8. * National Semiconductor SCx200 Watchdog support
  9. * Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/errno.h>
  28. #include <linux/init.h>
  29. #include <linux/miscdevice.h>
  30. #include <linux/watchdog.h>
  31. #include <linux/notifier.h>
  32. #include <linux/reboot.h>
  33. #include <linux/fs.h>
  34. #include <linux/ioport.h>
  35. #include <linux/io.h>
  36. #include <linux/uaccess.h>
  37. #include <asm/addrspace.h>
  38. #include <asm/ar7/ar7.h>
  39. #define DRVNAME "ar7_wdt"
  40. #define LONGNAME "TI AR7 Watchdog Timer"
  41. MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>");
  42. MODULE_DESCRIPTION(LONGNAME);
  43. MODULE_LICENSE("GPL");
  44. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  45. static int margin = 60;
  46. module_param(margin, int, 0);
  47. MODULE_PARM_DESC(margin, "Watchdog margin in seconds");
  48. static int nowayout = WATCHDOG_NOWAYOUT;
  49. module_param(nowayout, int, 0);
  50. MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
  51. #define READ_REG(x) readl((void __iomem *)&(x))
  52. #define WRITE_REG(x, v) writel((v), (void __iomem *)&(x))
  53. struct ar7_wdt {
  54. u32 kick_lock;
  55. u32 kick;
  56. u32 change_lock;
  57. u32 change;
  58. u32 disable_lock;
  59. u32 disable;
  60. u32 prescale_lock;
  61. u32 prescale;
  62. };
  63. static struct semaphore open_semaphore;
  64. static unsigned expect_close;
  65. /* XXX currently fixed, allows max margin ~68.72 secs */
  66. #define prescale_value 0xffff
  67. /* Offset of the WDT registers */
  68. static unsigned long ar7_regs_wdt;
  69. /* Pointer to the remapped WDT IO space */
  70. static struct ar7_wdt *ar7_wdt;
  71. static void ar7_wdt_get_regs(void)
  72. {
  73. u16 chip_id = ar7_chip_id();
  74. switch (chip_id) {
  75. case AR7_CHIP_7100:
  76. case AR7_CHIP_7200:
  77. ar7_regs_wdt = AR7_REGS_WDT;
  78. break;
  79. default:
  80. ar7_regs_wdt = UR8_REGS_WDT;
  81. break;
  82. }
  83. }
  84. static void ar7_wdt_kick(u32 value)
  85. {
  86. WRITE_REG(ar7_wdt->kick_lock, 0x5555);
  87. if ((READ_REG(ar7_wdt->kick_lock) & 3) == 1) {
  88. WRITE_REG(ar7_wdt->kick_lock, 0xaaaa);
  89. if ((READ_REG(ar7_wdt->kick_lock) & 3) == 3) {
  90. WRITE_REG(ar7_wdt->kick, value);
  91. return;
  92. }
  93. }
  94. printk(KERN_ERR DRVNAME ": failed to unlock WDT kick reg\n");
  95. }
  96. static void ar7_wdt_prescale(u32 value)
  97. {
  98. WRITE_REG(ar7_wdt->prescale_lock, 0x5a5a);
  99. if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 1) {
  100. WRITE_REG(ar7_wdt->prescale_lock, 0xa5a5);
  101. if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 3) {
  102. WRITE_REG(ar7_wdt->prescale, value);
  103. return;
  104. }
  105. }
  106. printk(KERN_ERR DRVNAME ": failed to unlock WDT prescale reg\n");
  107. }
  108. static void ar7_wdt_change(u32 value)
  109. {
  110. WRITE_REG(ar7_wdt->change_lock, 0x6666);
  111. if ((READ_REG(ar7_wdt->change_lock) & 3) == 1) {
  112. WRITE_REG(ar7_wdt->change_lock, 0xbbbb);
  113. if ((READ_REG(ar7_wdt->change_lock) & 3) == 3) {
  114. WRITE_REG(ar7_wdt->change, value);
  115. return;
  116. }
  117. }
  118. printk(KERN_ERR DRVNAME ": failed to unlock WDT change reg\n");
  119. }
  120. static void ar7_wdt_disable(u32 value)
  121. {
  122. WRITE_REG(ar7_wdt->disable_lock, 0x7777);
  123. if ((READ_REG(ar7_wdt->disable_lock) & 3) == 1) {
  124. WRITE_REG(ar7_wdt->disable_lock, 0xcccc);
  125. if ((READ_REG(ar7_wdt->disable_lock) & 3) == 2) {
  126. WRITE_REG(ar7_wdt->disable_lock, 0xdddd);
  127. if ((READ_REG(ar7_wdt->disable_lock) & 3) == 3) {
  128. WRITE_REG(ar7_wdt->disable, value);
  129. return;
  130. }
  131. }
  132. }
  133. printk(KERN_ERR DRVNAME ": failed to unlock WDT disable reg\n");
  134. }
  135. static void ar7_wdt_update_margin(int new_margin)
  136. {
  137. u32 change;
  138. change = new_margin * (ar7_vbus_freq() / prescale_value);
  139. if (change < 1) change = 1;
  140. if (change > 0xffff) change = 0xffff;
  141. ar7_wdt_change(change);
  142. margin = change * prescale_value / ar7_vbus_freq();
  143. printk(KERN_INFO DRVNAME
  144. ": timer margin %d seconds (prescale %d, change %d, freq %d)\n",
  145. margin, prescale_value, change, ar7_vbus_freq());
  146. }
  147. static void ar7_wdt_enable_wdt(void)
  148. {
  149. printk(KERN_DEBUG DRVNAME ": enabling watchdog timer\n");
  150. ar7_wdt_disable(1);
  151. ar7_wdt_kick(1);
  152. }
  153. static void ar7_wdt_disable_wdt(void)
  154. {
  155. printk(KERN_DEBUG DRVNAME ": disabling watchdog timer\n");
  156. ar7_wdt_disable(0);
  157. }
  158. static int ar7_wdt_open(struct inode *inode, struct file *file)
  159. {
  160. /* only allow one at a time */
  161. if (down_trylock(&open_semaphore))
  162. return -EBUSY;
  163. ar7_wdt_enable_wdt();
  164. expect_close = 0;
  165. return nonseekable_open(inode, file);
  166. }
  167. static int ar7_wdt_release(struct inode *inode, struct file *file)
  168. {
  169. if (!expect_close)
  170. printk(KERN_WARNING DRVNAME
  171. ": watchdog device closed unexpectedly,"
  172. "will not disable the watchdog timer\n");
  173. else if (!nowayout)
  174. ar7_wdt_disable_wdt();
  175. up(&open_semaphore);
  176. return 0;
  177. }
  178. static int ar7_wdt_notify_sys(struct notifier_block *this,
  179. unsigned long code, void *unused)
  180. {
  181. if (code == SYS_HALT || code == SYS_POWER_OFF)
  182. if (!nowayout)
  183. ar7_wdt_disable_wdt();
  184. return NOTIFY_DONE;
  185. }
  186. static struct notifier_block ar7_wdt_notifier = {
  187. .notifier_call = ar7_wdt_notify_sys
  188. };
  189. static ssize_t ar7_wdt_write(struct file *file, const char *data,
  190. size_t len, loff_t *ppos)
  191. {
  192. /* check for a magic close character */
  193. if (len) {
  194. size_t i;
  195. ar7_wdt_kick(1);
  196. expect_close = 0;
  197. for (i = 0; i < len; ++i) {
  198. char c;
  199. if (get_user(c, data+i))
  200. return -EFAULT;
  201. if (c == 'V')
  202. expect_close = 1;
  203. }
  204. }
  205. return len;
  206. }
  207. static int ar7_wdt_ioctl(struct inode *inode, struct file *file,
  208. unsigned int cmd, unsigned long arg)
  209. {
  210. static struct watchdog_info ident = {
  211. .identity = LONGNAME,
  212. .firmware_version = 1,
  213. .options = (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING),
  214. };
  215. int new_margin;
  216. switch (cmd) {
  217. default:
  218. return -ENOTTY;
  219. case WDIOC_GETSUPPORT:
  220. if (copy_to_user((struct watchdog_info *)arg, &ident,
  221. sizeof(ident)))
  222. return -EFAULT;
  223. return 0;
  224. case WDIOC_GETSTATUS:
  225. case WDIOC_GETBOOTSTATUS:
  226. if (put_user(0, (int *)arg))
  227. return -EFAULT;
  228. return 0;
  229. case WDIOC_KEEPALIVE:
  230. ar7_wdt_kick(1);
  231. return 0;
  232. case WDIOC_SETTIMEOUT:
  233. if (get_user(new_margin, (int *)arg))
  234. return -EFAULT;
  235. if (new_margin < 1)
  236. return -EINVAL;
  237. ar7_wdt_update_margin(new_margin);
  238. ar7_wdt_kick(1);
  239. case WDIOC_GETTIMEOUT:
  240. if (put_user(margin, (int *)arg))
  241. return -EFAULT;
  242. return 0;
  243. }
  244. }
  245. static struct file_operations ar7_wdt_fops = {
  246. .owner = THIS_MODULE,
  247. .write = ar7_wdt_write,
  248. .ioctl = ar7_wdt_ioctl,
  249. .open = ar7_wdt_open,
  250. .release = ar7_wdt_release,
  251. };
  252. static struct miscdevice ar7_wdt_miscdev = {
  253. .minor = WATCHDOG_MINOR,
  254. .name = "watchdog",
  255. .fops = &ar7_wdt_fops,
  256. };
  257. static int __init ar7_wdt_init(void)
  258. {
  259. int rc;
  260. ar7_wdt_get_regs();
  261. if (!request_mem_region(ar7_regs_wdt, sizeof(struct ar7_wdt),
  262. LONGNAME)) {
  263. printk(KERN_WARNING DRVNAME ": watchdog I/O region busy\n");
  264. return -EBUSY;
  265. }
  266. ar7_wdt = (struct ar7_wdt *)
  267. ioremap(ar7_regs_wdt, sizeof(struct ar7_wdt));
  268. ar7_wdt_disable_wdt();
  269. ar7_wdt_prescale(prescale_value);
  270. ar7_wdt_update_margin(margin);
  271. sema_init(&open_semaphore, 1);
  272. rc = register_reboot_notifier(&ar7_wdt_notifier);
  273. if (rc) {
  274. printk(KERN_ERR DRVNAME
  275. ": unable to register reboot notifier\n");
  276. goto out_alloc;
  277. }
  278. rc = misc_register(&ar7_wdt_miscdev);
  279. if (rc) {
  280. printk(KERN_ERR DRVNAME ": unable to register misc device\n");
  281. goto out_register;
  282. }
  283. goto out;
  284. out_register:
  285. unregister_reboot_notifier(&ar7_wdt_notifier);
  286. out_alloc:
  287. iounmap(ar7_wdt);
  288. release_mem_region(ar7_regs_wdt, sizeof(struct ar7_wdt));
  289. out:
  290. return rc;
  291. }
  292. static void __exit ar7_wdt_cleanup(void)
  293. {
  294. misc_deregister(&ar7_wdt_miscdev);
  295. unregister_reboot_notifier(&ar7_wdt_notifier);
  296. iounmap(ar7_wdt);
  297. release_mem_region(ar7_regs_wdt, sizeof(struct ar7_wdt));
  298. }
  299. module_init(ar7_wdt_init);
  300. module_exit(ar7_wdt_cleanup);