bfin_wdt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * Blackfin On-Chip Watchdog Driver
  3. * Supports BF53[123]/BF53[467]/BF54[2489]/BF561
  4. *
  5. * Originally based on softdog.c
  6. * Copyright 2006-2007 Analog Devices Inc.
  7. * Copyright 2006-2007 Michele d'Amico
  8. * Copyright 1996 Alan Cox <alan@redhat.com>
  9. *
  10. * Enter bugs at http://blackfin.uclinux.org/
  11. *
  12. * Licensed under the GPL-2 or later.
  13. */
  14. #include <linux/platform_device.h>
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/types.h>
  18. #include <linux/timer.h>
  19. #include <linux/miscdevice.h>
  20. #include <linux/watchdog.h>
  21. #include <linux/fs.h>
  22. #include <linux/notifier.h>
  23. #include <linux/reboot.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <asm/blackfin.h>
  27. #include <asm/uaccess.h>
  28. #define stamp(fmt, args...) pr_debug("%s:%i: " fmt "\n", __func__, __LINE__, ## args)
  29. #define stampit() stamp("here i am")
  30. #define pr_devinit(fmt, args...) ({ static const __devinitconst char __fmt[] = fmt; printk(__fmt, ## args); })
  31. #define pr_init(fmt, args...) ({ static const __initconst char __fmt[] = fmt; printk(__fmt, ## args); })
  32. #define WATCHDOG_NAME "bfin-wdt"
  33. #define PFX WATCHDOG_NAME ": "
  34. /* The BF561 has two watchdogs (one per core), but since Linux
  35. * only runs on core A, we'll just work with that one.
  36. */
  37. #ifdef BF561_FAMILY
  38. # define bfin_read_WDOG_CTL() bfin_read_WDOGA_CTL()
  39. # define bfin_read_WDOG_CNT() bfin_read_WDOGA_CNT()
  40. # define bfin_read_WDOG_STAT() bfin_read_WDOGA_STAT()
  41. # define bfin_write_WDOG_CTL(x) bfin_write_WDOGA_CTL(x)
  42. # define bfin_write_WDOG_CNT(x) bfin_write_WDOGA_CNT(x)
  43. # define bfin_write_WDOG_STAT(x) bfin_write_WDOGA_STAT(x)
  44. #endif
  45. /* Bit in SWRST that indicates boot caused by watchdog */
  46. #define SWRST_RESET_WDOG 0x4000
  47. /* Bit in WDOG_CTL that indicates watchdog has expired (WDR0) */
  48. #define WDOG_EXPIRED 0x8000
  49. /* Masks for WDEV field in WDOG_CTL register */
  50. #define ICTL_RESET 0x0
  51. #define ICTL_NMI 0x2
  52. #define ICTL_GPI 0x4
  53. #define ICTL_NONE 0x6
  54. #define ICTL_MASK 0x6
  55. /* Masks for WDEN field in WDOG_CTL register */
  56. #define WDEN_MASK 0x0FF0
  57. #define WDEN_ENABLE 0x0000
  58. #define WDEN_DISABLE 0x0AD0
  59. /* some defaults */
  60. #define WATCHDOG_TIMEOUT 20
  61. static unsigned int timeout = WATCHDOG_TIMEOUT;
  62. static int nowayout = WATCHDOG_NOWAYOUT;
  63. static struct watchdog_info bfin_wdt_info;
  64. static unsigned long open_check;
  65. static char expect_close;
  66. static DEFINE_SPINLOCK(bfin_wdt_spinlock);
  67. /**
  68. * bfin_wdt_keepalive - Keep the Userspace Watchdog Alive
  69. *
  70. * The Userspace watchdog got a KeepAlive: schedule the next timeout.
  71. */
  72. static int bfin_wdt_keepalive(void)
  73. {
  74. stampit();
  75. bfin_write_WDOG_STAT(0);
  76. return 0;
  77. }
  78. /**
  79. * bfin_wdt_stop - Stop the Watchdog
  80. *
  81. * Stops the on-chip watchdog.
  82. */
  83. static int bfin_wdt_stop(void)
  84. {
  85. stampit();
  86. bfin_write_WDOG_CTL(WDEN_DISABLE);
  87. return 0;
  88. }
  89. /**
  90. * bfin_wdt_start - Start the Watchdog
  91. *
  92. * Starts the on-chip watchdog. Automatically loads WDOG_CNT
  93. * into WDOG_STAT for us.
  94. */
  95. static int bfin_wdt_start(void)
  96. {
  97. stampit();
  98. bfin_write_WDOG_CTL(WDEN_ENABLE | ICTL_RESET);
  99. return 0;
  100. }
  101. /**
  102. * bfin_wdt_running - Check Watchdog status
  103. *
  104. * See if the watchdog is running.
  105. */
  106. static int bfin_wdt_running(void)
  107. {
  108. stampit();
  109. return ((bfin_read_WDOG_CTL() & WDEN_MASK) != WDEN_DISABLE);
  110. }
  111. /**
  112. * bfin_wdt_set_timeout - Set the Userspace Watchdog timeout
  113. * @t: new timeout value (in seconds)
  114. *
  115. * Translate the specified timeout in seconds into System Clock
  116. * terms which is what the on-chip Watchdog requires.
  117. */
  118. static int bfin_wdt_set_timeout(unsigned long t)
  119. {
  120. u32 cnt;
  121. unsigned long flags;
  122. stampit();
  123. cnt = t * get_sclk();
  124. if (cnt < get_sclk()) {
  125. printk(KERN_WARNING PFX "timeout value is too large\n");
  126. return -EINVAL;
  127. }
  128. spin_lock_irqsave(&bfin_wdt_spinlock, flags);
  129. {
  130. int run = bfin_wdt_running();
  131. bfin_wdt_stop();
  132. bfin_write_WDOG_CNT(cnt);
  133. if (run) bfin_wdt_start();
  134. }
  135. spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
  136. timeout = t;
  137. return 0;
  138. }
  139. /**
  140. * bfin_wdt_open - Open the Device
  141. * @inode: inode of device
  142. * @file: file handle of device
  143. *
  144. * Watchdog device is opened and started.
  145. */
  146. static int bfin_wdt_open(struct inode *inode, struct file *file)
  147. {
  148. stampit();
  149. if (test_and_set_bit(0, &open_check))
  150. return -EBUSY;
  151. if (nowayout)
  152. __module_get(THIS_MODULE);
  153. bfin_wdt_keepalive();
  154. bfin_wdt_start();
  155. return nonseekable_open(inode, file);
  156. }
  157. /**
  158. * bfin_wdt_close - Close the Device
  159. * @inode: inode of device
  160. * @file: file handle of device
  161. *
  162. * Watchdog device is closed and stopped.
  163. */
  164. static int bfin_wdt_release(struct inode *inode, struct file *file)
  165. {
  166. stampit();
  167. if (expect_close == 42) {
  168. bfin_wdt_stop();
  169. } else {
  170. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  171. bfin_wdt_keepalive();
  172. }
  173. expect_close = 0;
  174. clear_bit(0, &open_check);
  175. return 0;
  176. }
  177. /**
  178. * bfin_wdt_write - Write to Device
  179. * @file: file handle of device
  180. * @buf: buffer to write
  181. * @count: length of buffer
  182. * @ppos: offset
  183. *
  184. * Pings the watchdog on write.
  185. */
  186. static ssize_t bfin_wdt_write(struct file *file, const char __user *data,
  187. size_t len, loff_t *ppos)
  188. {
  189. stampit();
  190. if (len) {
  191. if (!nowayout) {
  192. size_t i;
  193. /* In case it was set long ago */
  194. expect_close = 0;
  195. for (i = 0; i != len; i++) {
  196. char c;
  197. if (get_user(c, data + i))
  198. return -EFAULT;
  199. if (c == 'V')
  200. expect_close = 42;
  201. }
  202. }
  203. bfin_wdt_keepalive();
  204. }
  205. return len;
  206. }
  207. /**
  208. * bfin_wdt_ioctl - Query Device
  209. * @inode: inode of device
  210. * @file: file handle of device
  211. * @cmd: watchdog command
  212. * @arg: argument
  213. *
  214. * Query basic information from the device or ping it, as outlined by the
  215. * watchdog API.
  216. */
  217. static int bfin_wdt_ioctl(struct inode *inode, struct file *file,
  218. unsigned int cmd, unsigned long arg)
  219. {
  220. void __user *argp = (void __user *)arg;
  221. int __user *p = argp;
  222. stampit();
  223. switch (cmd) {
  224. default:
  225. return -ENOTTY;
  226. case WDIOC_GETSUPPORT:
  227. if (copy_to_user(argp, &bfin_wdt_info, sizeof(bfin_wdt_info)))
  228. return -EFAULT;
  229. else
  230. return 0;
  231. case WDIOC_GETSTATUS:
  232. case WDIOC_GETBOOTSTATUS:
  233. return put_user(!!(_bfin_swrst & SWRST_RESET_WDOG), p);
  234. case WDIOC_KEEPALIVE:
  235. bfin_wdt_keepalive();
  236. return 0;
  237. case WDIOC_SETTIMEOUT: {
  238. int new_timeout;
  239. if (get_user(new_timeout, p))
  240. return -EFAULT;
  241. if (bfin_wdt_set_timeout(new_timeout))
  242. return -EINVAL;
  243. }
  244. /* Fall */
  245. case WDIOC_GETTIMEOUT:
  246. return put_user(timeout, p);
  247. case WDIOC_SETOPTIONS: {
  248. unsigned long flags;
  249. int options, ret = -EINVAL;
  250. if (get_user(options, p))
  251. return -EFAULT;
  252. spin_lock_irqsave(&bfin_wdt_spinlock, flags);
  253. if (options & WDIOS_DISABLECARD) {
  254. bfin_wdt_stop();
  255. ret = 0;
  256. }
  257. if (options & WDIOS_ENABLECARD) {
  258. bfin_wdt_start();
  259. ret = 0;
  260. }
  261. spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
  262. return ret;
  263. }
  264. }
  265. }
  266. /**
  267. * bfin_wdt_notify_sys - Notifier Handler
  268. * @this: notifier block
  269. * @code: notifier event
  270. * @unused: unused
  271. *
  272. * Handles specific events, such as turning off the watchdog during a
  273. * shutdown event.
  274. */
  275. static int bfin_wdt_notify_sys(struct notifier_block *this, unsigned long code,
  276. void *unused)
  277. {
  278. stampit();
  279. if (code == SYS_DOWN || code == SYS_HALT)
  280. bfin_wdt_stop();
  281. return NOTIFY_DONE;
  282. }
  283. #ifdef CONFIG_PM
  284. static int state_before_suspend;
  285. /**
  286. * bfin_wdt_suspend - suspend the watchdog
  287. * @pdev: device being suspended
  288. * @state: requested suspend state
  289. *
  290. * Remember if the watchdog was running and stop it.
  291. * TODO: is this even right? Doesn't seem to be any
  292. * standard in the watchdog world ...
  293. */
  294. static int bfin_wdt_suspend(struct platform_device *pdev, pm_message_t state)
  295. {
  296. stampit();
  297. state_before_suspend = bfin_wdt_running();
  298. bfin_wdt_stop();
  299. return 0;
  300. }
  301. /**
  302. * bfin_wdt_resume - resume the watchdog
  303. * @pdev: device being resumed
  304. *
  305. * If the watchdog was running, turn it back on.
  306. */
  307. static int bfin_wdt_resume(struct platform_device *pdev)
  308. {
  309. stampit();
  310. if (state_before_suspend) {
  311. bfin_wdt_set_timeout(timeout);
  312. bfin_wdt_start();
  313. }
  314. return 0;
  315. }
  316. #else
  317. # define bfin_wdt_suspend NULL
  318. # define bfin_wdt_resume NULL
  319. #endif
  320. static const struct file_operations bfin_wdt_fops = {
  321. .owner = THIS_MODULE,
  322. .llseek = no_llseek,
  323. .write = bfin_wdt_write,
  324. .ioctl = bfin_wdt_ioctl,
  325. .open = bfin_wdt_open,
  326. .release = bfin_wdt_release,
  327. };
  328. static struct miscdevice bfin_wdt_miscdev = {
  329. .minor = WATCHDOG_MINOR,
  330. .name = "watchdog",
  331. .fops = &bfin_wdt_fops,
  332. };
  333. static struct watchdog_info bfin_wdt_info = {
  334. .identity = "Blackfin Watchdog",
  335. .options = WDIOF_SETTIMEOUT |
  336. WDIOF_KEEPALIVEPING |
  337. WDIOF_MAGICCLOSE,
  338. };
  339. static struct notifier_block bfin_wdt_notifier = {
  340. .notifier_call = bfin_wdt_notify_sys,
  341. };
  342. /**
  343. * bfin_wdt_probe - Initialize module
  344. *
  345. * Registers the misc device and notifier handler. Actual device
  346. * initialization is handled by bfin_wdt_open().
  347. */
  348. static int __devinit bfin_wdt_probe(struct platform_device *pdev)
  349. {
  350. int ret;
  351. ret = register_reboot_notifier(&bfin_wdt_notifier);
  352. if (ret) {
  353. pr_devinit(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n", ret);
  354. return ret;
  355. }
  356. ret = misc_register(&bfin_wdt_miscdev);
  357. if (ret) {
  358. pr_devinit(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  359. WATCHDOG_MINOR, ret);
  360. unregister_reboot_notifier(&bfin_wdt_notifier);
  361. return ret;
  362. }
  363. pr_devinit(KERN_INFO PFX "initialized: timeout=%d sec (nowayout=%d)\n",
  364. timeout, nowayout);
  365. return 0;
  366. }
  367. /**
  368. * bfin_wdt_remove - Initialize module
  369. *
  370. * Unregisters the misc device and notifier handler. Actual device
  371. * deinitialization is handled by bfin_wdt_close().
  372. */
  373. static int __devexit bfin_wdt_remove(struct platform_device *pdev)
  374. {
  375. misc_deregister(&bfin_wdt_miscdev);
  376. unregister_reboot_notifier(&bfin_wdt_notifier);
  377. return 0;
  378. }
  379. static struct platform_device *bfin_wdt_device;
  380. static struct platform_driver bfin_wdt_driver = {
  381. .probe = bfin_wdt_probe,
  382. .remove = __devexit_p(bfin_wdt_remove),
  383. .suspend = bfin_wdt_suspend,
  384. .resume = bfin_wdt_resume,
  385. .driver = {
  386. .name = WATCHDOG_NAME,
  387. .owner = THIS_MODULE,
  388. },
  389. };
  390. /**
  391. * bfin_wdt_init - Initialize module
  392. *
  393. * Checks the module params and registers the platform device & driver.
  394. * Real work is in the platform probe function.
  395. */
  396. static int __init bfin_wdt_init(void)
  397. {
  398. int ret;
  399. stampit();
  400. /* Check that the timeout value is within range */
  401. if (bfin_wdt_set_timeout(timeout))
  402. return -EINVAL;
  403. /* Since this is an on-chip device and needs no board-specific
  404. * resources, we'll handle all the platform device stuff here.
  405. */
  406. ret = platform_driver_register(&bfin_wdt_driver);
  407. if (ret) {
  408. pr_init(KERN_ERR PFX "unable to register driver\n");
  409. return ret;
  410. }
  411. bfin_wdt_device = platform_device_register_simple(WATCHDOG_NAME, -1, NULL, 0);
  412. if (IS_ERR(bfin_wdt_device)) {
  413. pr_init(KERN_ERR PFX "unable to register device\n");
  414. platform_driver_unregister(&bfin_wdt_driver);
  415. return PTR_ERR(bfin_wdt_device);
  416. }
  417. return 0;
  418. }
  419. /**
  420. * bfin_wdt_exit - Deinitialize module
  421. *
  422. * Back out the platform device & driver steps. Real work is in the
  423. * platform remove function.
  424. */
  425. static void __exit bfin_wdt_exit(void)
  426. {
  427. platform_device_unregister(bfin_wdt_device);
  428. platform_driver_unregister(&bfin_wdt_driver);
  429. }
  430. module_init(bfin_wdt_init);
  431. module_exit(bfin_wdt_exit);
  432. MODULE_AUTHOR("Michele d'Amico, Mike Frysinger <vapier@gentoo.org>");
  433. MODULE_DESCRIPTION("Blackfin Watchdog Device Driver");
  434. MODULE_LICENSE("GPL");
  435. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  436. module_param(timeout, uint, 0);
  437. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<=((2^32)/SCLK), default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  438. module_param(nowayout, int, 0);
  439. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");