0005-bcm2708-watchdog-driver.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. From 633194396e40f919974dd8b81e97ddfea463b733 Mon Sep 17 00:00:00 2001
  2. From: popcornmix <popcornmix@gmail.com>
  3. Date: Wed, 1 May 2013 19:54:32 +0100
  4. Subject: [PATCH 005/114] bcm2708 watchdog driver
  5. Signed-off-by: popcornmix <popcornmix@gmail.com>
  6. ---
  7. drivers/watchdog/Kconfig | 6 +
  8. drivers/watchdog/Makefile | 1 +
  9. drivers/watchdog/bcm2708_wdog.c | 382 ++++++++++++++++++++++++++++++++++++++++
  10. 3 files changed, 389 insertions(+)
  11. create mode 100644 drivers/watchdog/bcm2708_wdog.c
  12. --- a/drivers/watchdog/Kconfig
  13. +++ b/drivers/watchdog/Kconfig
  14. @@ -452,6 +452,12 @@ config RETU_WATCHDOG
  15. To compile this driver as a module, choose M here: the
  16. module will be called retu_wdt.
  17. +config BCM2708_WDT
  18. + tristate "BCM2708 Watchdog"
  19. + depends on ARCH_BCM2708
  20. + help
  21. + Enables BCM2708 watchdog support.
  22. +
  23. config MOXART_WDT
  24. tristate "MOXART watchdog"
  25. depends on ARCH_MOXART
  26. --- a/drivers/watchdog/Makefile
  27. +++ b/drivers/watchdog/Makefile
  28. @@ -56,6 +56,7 @@ obj-$(CONFIG_TS72XX_WATCHDOG) += ts72xx_
  29. obj-$(CONFIG_IMX2_WDT) += imx2_wdt.o
  30. obj-$(CONFIG_UX500_WATCHDOG) += ux500_wdt.o
  31. obj-$(CONFIG_RETU_WATCHDOG) += retu_wdt.o
  32. +obj-$(CONFIG_BCM2708_WDT) += bcm2708_wdog.o
  33. obj-$(CONFIG_BCM2835_WDT) += bcm2835_wdt.o
  34. obj-$(CONFIG_MOXART_WDT) += moxart_wdt.o
  35. obj-$(CONFIG_SIRFSOC_WATCHDOG) += sirfsoc_wdt.o
  36. --- /dev/null
  37. +++ b/drivers/watchdog/bcm2708_wdog.c
  38. @@ -0,0 +1,382 @@
  39. +/*
  40. + * Broadcom BCM2708 watchdog driver.
  41. + *
  42. + * (c) Copyright 2010 Broadcom Europe Ltd
  43. + *
  44. + * This program is free software; you can redistribute it and/or
  45. + * modify it under the terms of the GNU General Public License
  46. + * as published by the Free Software Foundation; either version
  47. + * 2 of the License, or (at your option) any later version.
  48. + *
  49. + * BCM2708 watchdog driver. Loosely based on wdt driver.
  50. + */
  51. +
  52. +#include <linux/interrupt.h>
  53. +#include <linux/module.h>
  54. +#include <linux/moduleparam.h>
  55. +#include <linux/types.h>
  56. +#include <linux/miscdevice.h>
  57. +#include <linux/watchdog.h>
  58. +#include <linux/fs.h>
  59. +#include <linux/ioport.h>
  60. +#include <linux/notifier.h>
  61. +#include <linux/reboot.h>
  62. +#include <linux/init.h>
  63. +#include <linux/io.h>
  64. +#include <linux/uaccess.h>
  65. +#include <mach/platform.h>
  66. +
  67. +#define SECS_TO_WDOG_TICKS(x) ((x) << 16)
  68. +#define WDOG_TICKS_TO_SECS(x) ((x) >> 16)
  69. +
  70. +static unsigned long wdog_is_open;
  71. +static uint32_t wdog_ticks; /* Ticks to load into wdog timer */
  72. +static char expect_close;
  73. +
  74. +/*
  75. + * Module parameters
  76. + */
  77. +
  78. +#define WD_TIMO 10 /* Default heartbeat = 60 seconds */
  79. +static int heartbeat = WD_TIMO; /* Heartbeat in seconds */
  80. +
  81. +module_param(heartbeat, int, 0);
  82. +MODULE_PARM_DESC(heartbeat,
  83. + "Watchdog heartbeat in seconds. (0 < heartbeat < 65536, default="
  84. + __MODULE_STRING(WD_TIMO) ")");
  85. +
  86. +static int nowayout = WATCHDOG_NOWAYOUT;
  87. +module_param(nowayout, int, 0);
  88. +MODULE_PARM_DESC(nowayout,
  89. + "Watchdog cannot be stopped once started (default="
  90. + __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  91. +
  92. +static DEFINE_SPINLOCK(wdog_lock);
  93. +
  94. +/**
  95. + * Start the watchdog driver.
  96. + */
  97. +
  98. +static int wdog_start(unsigned long timeout)
  99. +{
  100. + uint32_t cur;
  101. + unsigned long flags;
  102. + spin_lock_irqsave(&wdog_lock, flags);
  103. +
  104. + /* enable the watchdog */
  105. + iowrite32(PM_PASSWORD | (timeout & PM_WDOG_TIME_SET),
  106. + __io_address(PM_WDOG));
  107. + cur = ioread32(__io_address(PM_RSTC));
  108. + iowrite32(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
  109. + PM_RSTC_WRCFG_FULL_RESET, __io_address(PM_RSTC));
  110. +
  111. + spin_unlock_irqrestore(&wdog_lock, flags);
  112. + return 0;
  113. +}
  114. +
  115. +/**
  116. + * Stop the watchdog driver.
  117. + */
  118. +
  119. +static int wdog_stop(void)
  120. +{
  121. + iowrite32(PM_PASSWORD | PM_RSTC_RESET, __io_address(PM_RSTC));
  122. + printk(KERN_INFO "watchdog stopped\n");
  123. + return 0;
  124. +}
  125. +
  126. +/**
  127. + * Reload counter one with the watchdog heartbeat. We don't bother
  128. + * reloading the cascade counter.
  129. + */
  130. +
  131. +static void wdog_ping(void)
  132. +{
  133. + wdog_start(wdog_ticks);
  134. +}
  135. +
  136. +/**
  137. + * @t: the new heartbeat value that needs to be set.
  138. + *
  139. + * Set a new heartbeat value for the watchdog device. If the heartbeat
  140. + * value is incorrect we keep the old value and return -EINVAL. If
  141. + * successful we return 0.
  142. + */
  143. +
  144. +static int wdog_set_heartbeat(int t)
  145. +{
  146. + if (t < 1 || t > WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET))
  147. + return -EINVAL;
  148. +
  149. + heartbeat = t;
  150. + wdog_ticks = SECS_TO_WDOG_TICKS(t);
  151. + return 0;
  152. +}
  153. +
  154. +/**
  155. + * @file: file handle to the watchdog
  156. + * @buf: buffer to write (unused as data does not matter here
  157. + * @count: count of bytes
  158. + * @ppos: pointer to the position to write. No seeks allowed
  159. + *
  160. + * A write to a watchdog device is defined as a keepalive signal.
  161. + *
  162. + * if 'nowayout' is set then normally a close() is ignored. But
  163. + * if you write 'V' first then the close() will stop the timer.
  164. + */
  165. +
  166. +static ssize_t wdog_write(struct file *file, const char __user *buf,
  167. + size_t count, loff_t *ppos)
  168. +{
  169. + if (count) {
  170. + if (!nowayout) {
  171. + size_t i;
  172. +
  173. + /* In case it was set long ago */
  174. + expect_close = 0;
  175. +
  176. + for (i = 0; i != count; i++) {
  177. + char c;
  178. + if (get_user(c, buf + i))
  179. + return -EFAULT;
  180. + if (c == 'V')
  181. + expect_close = 42;
  182. + }
  183. + }
  184. + wdog_ping();
  185. + }
  186. + return count;
  187. +}
  188. +
  189. +static int wdog_get_status(void)
  190. +{
  191. + unsigned long flags;
  192. + int status = 0;
  193. + spin_lock_irqsave(&wdog_lock, flags);
  194. + /* FIXME: readback reset reason */
  195. + spin_unlock_irqrestore(&wdog_lock, flags);
  196. + return status;
  197. +}
  198. +
  199. +static uint32_t wdog_get_remaining(void)
  200. +{
  201. + uint32_t ret = ioread32(__io_address(PM_WDOG));
  202. + return ret & PM_WDOG_TIME_SET;
  203. +}
  204. +
  205. +/**
  206. + * @file: file handle to the device
  207. + * @cmd: watchdog command
  208. + * @arg: argument pointer
  209. + *
  210. + * The watchdog API defines a common set of functions for all watchdogs
  211. + * according to their available features. We only actually usefully support
  212. + * querying capabilities and current status.
  213. + */
  214. +
  215. +static long wdog_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  216. +{
  217. + void __user *argp = (void __user *)arg;
  218. + int __user *p = argp;
  219. + int new_heartbeat;
  220. + int status;
  221. + int options;
  222. + uint32_t remaining;
  223. +
  224. + struct watchdog_info ident = {
  225. + .options = WDIOF_SETTIMEOUT|
  226. + WDIOF_MAGICCLOSE|
  227. + WDIOF_KEEPALIVEPING,
  228. + .firmware_version = 1,
  229. + .identity = "BCM2708",
  230. + };
  231. +
  232. + switch (cmd) {
  233. + case WDIOC_GETSUPPORT:
  234. + return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  235. + case WDIOC_GETSTATUS:
  236. + status = wdog_get_status();
  237. + return put_user(status, p);
  238. + case WDIOC_GETBOOTSTATUS:
  239. + return put_user(0, p);
  240. + case WDIOC_KEEPALIVE:
  241. + wdog_ping();
  242. + return 0;
  243. + case WDIOC_SETTIMEOUT:
  244. + if (get_user(new_heartbeat, p))
  245. + return -EFAULT;
  246. + if (wdog_set_heartbeat(new_heartbeat))
  247. + return -EINVAL;
  248. + wdog_ping();
  249. + /* Fall */
  250. + case WDIOC_GETTIMEOUT:
  251. + return put_user(heartbeat, p);
  252. + case WDIOC_GETTIMELEFT:
  253. + remaining = WDOG_TICKS_TO_SECS(wdog_get_remaining());
  254. + return put_user(remaining, p);
  255. + case WDIOC_SETOPTIONS:
  256. + if (get_user(options, p))
  257. + return -EFAULT;
  258. + if (options & WDIOS_DISABLECARD)
  259. + wdog_stop();
  260. + if (options & WDIOS_ENABLECARD)
  261. + wdog_start(wdog_ticks);
  262. + return 0;
  263. + default:
  264. + return -ENOTTY;
  265. + }
  266. +}
  267. +
  268. +/**
  269. + * @inode: inode of device
  270. + * @file: file handle to device
  271. + *
  272. + * The watchdog device has been opened. The watchdog device is single
  273. + * open and on opening we load the counters.
  274. + */
  275. +
  276. +static int wdog_open(struct inode *inode, struct file *file)
  277. +{
  278. + if (test_and_set_bit(0, &wdog_is_open))
  279. + return -EBUSY;
  280. + /*
  281. + * Activate
  282. + */
  283. + wdog_start(wdog_ticks);
  284. + return nonseekable_open(inode, file);
  285. +}
  286. +
  287. +/**
  288. + * @inode: inode to board
  289. + * @file: file handle to board
  290. + *
  291. + * The watchdog has a configurable API. There is a religious dispute
  292. + * between people who want their watchdog to be able to shut down and
  293. + * those who want to be sure if the watchdog manager dies the machine
  294. + * reboots. In the former case we disable the counters, in the latter
  295. + * case you have to open it again very soon.
  296. + */
  297. +
  298. +static int wdog_release(struct inode *inode, struct file *file)
  299. +{
  300. + if (expect_close == 42) {
  301. + wdog_stop();
  302. + } else {
  303. + printk(KERN_CRIT
  304. + "wdt: WDT device closed unexpectedly. WDT will not stop!\n");
  305. + wdog_ping();
  306. + }
  307. + clear_bit(0, &wdog_is_open);
  308. + expect_close = 0;
  309. + return 0;
  310. +}
  311. +
  312. +/**
  313. + * @this: our notifier block
  314. + * @code: the event being reported
  315. + * @unused: unused
  316. + *
  317. + * Our notifier is called on system shutdowns. Turn the watchdog
  318. + * off so that it does not fire during the next reboot.
  319. + */
  320. +
  321. +static int wdog_notify_sys(struct notifier_block *this, unsigned long code,
  322. + void *unused)
  323. +{
  324. + if (code == SYS_DOWN || code == SYS_HALT)
  325. + wdog_stop();
  326. + return NOTIFY_DONE;
  327. +}
  328. +
  329. +/*
  330. + * Kernel Interfaces
  331. + */
  332. +
  333. +
  334. +static const struct file_operations wdog_fops = {
  335. + .owner = THIS_MODULE,
  336. + .llseek = no_llseek,
  337. + .write = wdog_write,
  338. + .unlocked_ioctl = wdog_ioctl,
  339. + .open = wdog_open,
  340. + .release = wdog_release,
  341. +};
  342. +
  343. +static struct miscdevice wdog_miscdev = {
  344. + .minor = WATCHDOG_MINOR,
  345. + .name = "watchdog",
  346. + .fops = &wdog_fops,
  347. +};
  348. +
  349. +/*
  350. + * The WDT card needs to learn about soft shutdowns in order to
  351. + * turn the timebomb registers off.
  352. + */
  353. +
  354. +static struct notifier_block wdog_notifier = {
  355. + .notifier_call = wdog_notify_sys,
  356. +};
  357. +
  358. +/**
  359. + * cleanup_module:
  360. + *
  361. + * Unload the watchdog. You cannot do this with any file handles open.
  362. + * If your watchdog is set to continue ticking on close and you unload
  363. + * it, well it keeps ticking. We won't get the interrupt but the board
  364. + * will not touch PC memory so all is fine. You just have to load a new
  365. + * module in 60 seconds or reboot.
  366. + */
  367. +
  368. +static void __exit wdog_exit(void)
  369. +{
  370. + misc_deregister(&wdog_miscdev);
  371. + unregister_reboot_notifier(&wdog_notifier);
  372. +}
  373. +
  374. +static int __init wdog_init(void)
  375. +{
  376. + int ret;
  377. +
  378. + /* Check that the heartbeat value is within it's range;
  379. + if not reset to the default */
  380. + if (wdog_set_heartbeat(heartbeat)) {
  381. + wdog_set_heartbeat(WD_TIMO);
  382. + printk(KERN_INFO "bcm2708_wdog: heartbeat value must be "
  383. + "0 < heartbeat < %d, using %d\n",
  384. + WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
  385. + WD_TIMO);
  386. + }
  387. +
  388. + ret = register_reboot_notifier(&wdog_notifier);
  389. + if (ret) {
  390. + printk(KERN_ERR
  391. + "wdt: cannot register reboot notifier (err=%d)\n", ret);
  392. + goto out_reboot;
  393. + }
  394. +
  395. + ret = misc_register(&wdog_miscdev);
  396. + if (ret) {
  397. + printk(KERN_ERR
  398. + "wdt: cannot register miscdev on minor=%d (err=%d)\n",
  399. + WATCHDOG_MINOR, ret);
  400. + goto out_misc;
  401. + }
  402. +
  403. + printk(KERN_INFO "bcm2708 watchdog, heartbeat=%d sec (nowayout=%d)\n",
  404. + heartbeat, nowayout);
  405. + return 0;
  406. +
  407. +out_misc:
  408. + unregister_reboot_notifier(&wdog_notifier);
  409. +out_reboot:
  410. + return ret;
  411. +}
  412. +
  413. +module_init(wdog_init);
  414. +module_exit(wdog_exit);
  415. +
  416. +MODULE_AUTHOR("Luke Diamand");
  417. +MODULE_DESCRIPTION("Driver for BCM2708 watchdog");
  418. +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  419. +MODULE_ALIAS_MISCDEV(TEMP_MINOR);
  420. +MODULE_LICENSE("GPL");