ar7_wdt.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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/platform_device.h>
  31. #include <linux/watchdog.h>
  32. #include <linux/fs.h>
  33. #include <linux/ioport.h>
  34. #include <linux/io.h>
  35. #include <linux/uaccess.h>
  36. #include <asm/addrspace.h>
  37. #include <asm/mach-ar7/ar7.h>
  38. #define DRVNAME "ar7_wdt"
  39. #define LONGNAME "TI AR7 Watchdog Timer"
  40. MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>");
  41. MODULE_DESCRIPTION(LONGNAME);
  42. MODULE_LICENSE("GPL");
  43. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  44. static int margin = 60;
  45. module_param(margin, int, 0);
  46. MODULE_PARM_DESC(margin, "Watchdog margin in seconds");
  47. static int nowayout = WATCHDOG_NOWAYOUT;
  48. module_param(nowayout, int, 0);
  49. MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
  50. #define READ_REG(x) readl((void __iomem *)&(x))
  51. #define WRITE_REG(x, v) writel((v), (void __iomem *)&(x))
  52. struct ar7_wdt {
  53. u32 kick_lock;
  54. u32 kick;
  55. u32 change_lock;
  56. u32 change;
  57. u32 disable_lock;
  58. u32 disable;
  59. u32 prescale_lock;
  60. u32 prescale;
  61. };
  62. static unsigned long wdt_is_open;
  63. static spinlock_t wdt_lock;
  64. static unsigned expect_close;
  65. /* XXX currently fixed, allows max margin ~68.72 secs */
  66. #define prescale_value 0xffff
  67. /* Resource of the WDT registers */
  68. static struct resource *ar7_regs_wdt;
  69. /* Pointer to the remapped WDT IO space */
  70. static struct ar7_wdt *ar7_wdt;
  71. static void ar7_wdt_kick(u32 value)
  72. {
  73. WRITE_REG(ar7_wdt->kick_lock, 0x5555);
  74. if ((READ_REG(ar7_wdt->kick_lock) & 3) == 1) {
  75. WRITE_REG(ar7_wdt->kick_lock, 0xaaaa);
  76. if ((READ_REG(ar7_wdt->kick_lock) & 3) == 3) {
  77. WRITE_REG(ar7_wdt->kick, value);
  78. return;
  79. }
  80. }
  81. printk(KERN_ERR DRVNAME ": failed to unlock WDT kick reg\n");
  82. }
  83. static void ar7_wdt_prescale(u32 value)
  84. {
  85. WRITE_REG(ar7_wdt->prescale_lock, 0x5a5a);
  86. if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 1) {
  87. WRITE_REG(ar7_wdt->prescale_lock, 0xa5a5);
  88. if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 3) {
  89. WRITE_REG(ar7_wdt->prescale, value);
  90. return;
  91. }
  92. }
  93. printk(KERN_ERR DRVNAME ": failed to unlock WDT prescale reg\n");
  94. }
  95. static void ar7_wdt_change(u32 value)
  96. {
  97. WRITE_REG(ar7_wdt->change_lock, 0x6666);
  98. if ((READ_REG(ar7_wdt->change_lock) & 3) == 1) {
  99. WRITE_REG(ar7_wdt->change_lock, 0xbbbb);
  100. if ((READ_REG(ar7_wdt->change_lock) & 3) == 3) {
  101. WRITE_REG(ar7_wdt->change, value);
  102. return;
  103. }
  104. }
  105. printk(KERN_ERR DRVNAME ": failed to unlock WDT change reg\n");
  106. }
  107. static void ar7_wdt_disable(u32 value)
  108. {
  109. WRITE_REG(ar7_wdt->disable_lock, 0x7777);
  110. if ((READ_REG(ar7_wdt->disable_lock) & 3) == 1) {
  111. WRITE_REG(ar7_wdt->disable_lock, 0xcccc);
  112. if ((READ_REG(ar7_wdt->disable_lock) & 3) == 2) {
  113. WRITE_REG(ar7_wdt->disable_lock, 0xdddd);
  114. if ((READ_REG(ar7_wdt->disable_lock) & 3) == 3) {
  115. WRITE_REG(ar7_wdt->disable, value);
  116. return;
  117. }
  118. }
  119. }
  120. printk(KERN_ERR DRVNAME ": failed to unlock WDT disable reg\n");
  121. }
  122. static void ar7_wdt_update_margin(int new_margin)
  123. {
  124. u32 change;
  125. change = new_margin * (ar7_vbus_freq() / prescale_value);
  126. if (change < 1)
  127. change = 1;
  128. if (change > 0xffff)
  129. change = 0xffff;
  130. ar7_wdt_change(change);
  131. margin = change * prescale_value / ar7_vbus_freq();
  132. printk(KERN_INFO DRVNAME
  133. ": timer margin %d seconds (prescale %d, change %d, freq %d)\n",
  134. margin, prescale_value, change, ar7_vbus_freq());
  135. }
  136. static void ar7_wdt_enable_wdt(void)
  137. {
  138. printk(KERN_DEBUG DRVNAME ": enabling watchdog timer\n");
  139. ar7_wdt_disable(1);
  140. ar7_wdt_kick(1);
  141. }
  142. static void ar7_wdt_disable_wdt(void)
  143. {
  144. printk(KERN_DEBUG DRVNAME ": disabling watchdog timer\n");
  145. ar7_wdt_disable(0);
  146. }
  147. static int ar7_wdt_open(struct inode *inode, struct file *file)
  148. {
  149. /* only allow one at a time */
  150. if (test_and_set_bit(0, &wdt_is_open))
  151. return -EBUSY;
  152. ar7_wdt_enable_wdt();
  153. expect_close = 0;
  154. return nonseekable_open(inode, file);
  155. }
  156. static int ar7_wdt_release(struct inode *inode, struct file *file)
  157. {
  158. if (!expect_close)
  159. printk(KERN_WARNING DRVNAME
  160. ": watchdog device closed unexpectedly,"
  161. "will not disable the watchdog timer\n");
  162. else if (!nowayout)
  163. ar7_wdt_disable_wdt();
  164. clear_bit(0, &wdt_is_open);
  165. return 0;
  166. }
  167. static ssize_t ar7_wdt_write(struct file *file, const char *data,
  168. size_t len, loff_t *ppos)
  169. {
  170. /* check for a magic close character */
  171. if (len) {
  172. size_t i;
  173. spin_lock(&wdt_lock);
  174. ar7_wdt_kick(1);
  175. spin_unlock(&wdt_lock);
  176. expect_close = 0;
  177. for (i = 0; i < len; ++i) {
  178. char c;
  179. if (get_user(c, data + i))
  180. return -EFAULT;
  181. if (c == 'V')
  182. expect_close = 1;
  183. }
  184. }
  185. return len;
  186. }
  187. static long ar7_wdt_ioctl(struct file *file,
  188. unsigned int cmd, unsigned long arg)
  189. {
  190. static struct watchdog_info ident = {
  191. .identity = LONGNAME,
  192. .firmware_version = 1,
  193. .options = (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  194. WDIOF_MAGICCLOSE),
  195. };
  196. int new_margin;
  197. switch (cmd) {
  198. case WDIOC_GETSUPPORT:
  199. if (copy_to_user((struct watchdog_info *)arg, &ident,
  200. sizeof(ident)))
  201. return -EFAULT;
  202. return 0;
  203. case WDIOC_GETSTATUS:
  204. case WDIOC_GETBOOTSTATUS:
  205. if (put_user(0, (int *)arg))
  206. return -EFAULT;
  207. return 0;
  208. case WDIOC_KEEPALIVE:
  209. ar7_wdt_kick(1);
  210. return 0;
  211. case WDIOC_SETTIMEOUT:
  212. if (get_user(new_margin, (int *)arg))
  213. return -EFAULT;
  214. if (new_margin < 1)
  215. return -EINVAL;
  216. spin_lock(&wdt_lock);
  217. ar7_wdt_update_margin(new_margin);
  218. ar7_wdt_kick(1);
  219. spin_unlock(&wdt_lock);
  220. case WDIOC_GETTIMEOUT:
  221. if (put_user(margin, (int *)arg))
  222. return -EFAULT;
  223. return 0;
  224. default:
  225. return -ENOTTY;
  226. }
  227. }
  228. static const struct file_operations ar7_wdt_fops = {
  229. .owner = THIS_MODULE,
  230. .write = ar7_wdt_write,
  231. .unlocked_ioctl = ar7_wdt_ioctl,
  232. .open = ar7_wdt_open,
  233. .release = ar7_wdt_release,
  234. };
  235. static struct miscdevice ar7_wdt_miscdev = {
  236. .minor = WATCHDOG_MINOR,
  237. .name = "watchdog",
  238. .fops = &ar7_wdt_fops,
  239. };
  240. static int __devinit ar7_wdt_probe(struct platform_device *pdev)
  241. {
  242. int rc;
  243. spin_lock_init(&wdt_lock);
  244. ar7_regs_wdt =
  245. platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  246. if (!ar7_regs_wdt) {
  247. printk(KERN_ERR DRVNAME ": could not get registers resource\n");
  248. rc = -ENODEV;
  249. goto out;
  250. }
  251. if (!request_mem_region(ar7_regs_wdt->start,
  252. resource_size(ar7_regs_wdt), LONGNAME)) {
  253. printk(KERN_WARNING DRVNAME ": watchdog I/O region busy\n");
  254. rc = -EBUSY;
  255. goto out;
  256. }
  257. ar7_wdt = ioremap(ar7_regs_wdt->start, resource_size(ar7_regs_wdt));
  258. if (!ar7_wdt) {
  259. printk(KERN_ERR DRVNAME ": could not ioremap registers\n");
  260. rc = -ENXIO;
  261. goto out_mem_region;
  262. }
  263. ar7_wdt_disable_wdt();
  264. ar7_wdt_prescale(prescale_value);
  265. ar7_wdt_update_margin(margin);
  266. rc = misc_register(&ar7_wdt_miscdev);
  267. if (rc) {
  268. printk(KERN_ERR DRVNAME ": unable to register misc device\n");
  269. goto out_alloc;
  270. }
  271. goto out;
  272. out_alloc:
  273. iounmap(ar7_wdt);
  274. out_mem_region:
  275. release_mem_region(ar7_regs_wdt->start, resource_size(ar7_regs_wdt));
  276. out:
  277. return rc;
  278. }
  279. static int __devexit ar7_wdt_remove(struct platform_device *pdev)
  280. {
  281. misc_deregister(&ar7_wdt_miscdev);
  282. iounmap(ar7_wdt);
  283. release_mem_region(ar7_regs_wdt->start, resource_size(ar7_regs_wdt));
  284. return 0;
  285. }
  286. static void ar7_wdt_shutdown(struct platform_device *pdev)
  287. {
  288. if (!nowayout)
  289. ar7_wdt_disable_wdt();
  290. }
  291. static struct platform_driver ar7_wdt_driver = {
  292. .probe = ar7_wdt_probe,
  293. .remove = __devexit_p(ar7_wdt_remove),
  294. .shutdown = ar7_wdt_shutdown,
  295. .driver = {
  296. .owner = THIS_MODULE,
  297. .name = "ar7_wdt",
  298. },
  299. };
  300. static int __init ar7_wdt_init(void)
  301. {
  302. return platform_driver_register(&ar7_wdt_driver);
  303. }
  304. static void __exit ar7_wdt_cleanup(void)
  305. {
  306. platform_driver_unregister(&ar7_wdt_driver);
  307. }
  308. module_init(ar7_wdt_init);
  309. module_exit(ar7_wdt_cleanup);