ar7_wdt.c 8.8 KB

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