coh901327_wdt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. * coh901327_wdt.c
  3. *
  4. * Copyright (C) 2008-2009 ST-Ericsson AB
  5. * License terms: GNU General Public License (GPL) version 2
  6. * Watchdog driver for the ST-Ericsson AB COH 901 327 IP core
  7. * Author: Linus Walleij <linus.walleij@stericsson.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/types.h>
  11. #include <linux/fs.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/watchdog.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/pm.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/io.h>
  18. #include <linux/bitops.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/clk.h>
  21. #include <linux/delay.h>
  22. #define DRV_NAME "WDOG COH 901 327"
  23. /*
  24. * COH 901 327 register definitions
  25. */
  26. /* WDOG_FEED Register 32bit (-/W) */
  27. #define U300_WDOG_FR 0x00
  28. #define U300_WDOG_FR_FEED_RESTART_TIMER 0xFEEDU
  29. /* WDOG_TIMEOUT Register 32bit (R/W) */
  30. #define U300_WDOG_TR 0x04
  31. #define U300_WDOG_TR_TIMEOUT_MASK 0x7FFFU
  32. /* WDOG_DISABLE1 Register 32bit (-/W) */
  33. #define U300_WDOG_D1R 0x08
  34. #define U300_WDOG_D1R_DISABLE1_DISABLE_TIMER 0x2BADU
  35. /* WDOG_DISABLE2 Register 32bit (R/W) */
  36. #define U300_WDOG_D2R 0x0C
  37. #define U300_WDOG_D2R_DISABLE2_DISABLE_TIMER 0xCAFEU
  38. #define U300_WDOG_D2R_DISABLE_STATUS_DISABLED 0xDABEU
  39. #define U300_WDOG_D2R_DISABLE_STATUS_ENABLED 0x0000U
  40. /* WDOG_STATUS Register 32bit (R/W) */
  41. #define U300_WDOG_SR 0x10
  42. #define U300_WDOG_SR_STATUS_TIMED_OUT 0xCFE8U
  43. #define U300_WDOG_SR_STATUS_NORMAL 0x0000U
  44. #define U300_WDOG_SR_RESET_STATUS_RESET 0xE8B4U
  45. /* WDOG_COUNT Register 32bit (R/-) */
  46. #define U300_WDOG_CR 0x14
  47. #define U300_WDOG_CR_VALID_IND 0x8000U
  48. #define U300_WDOG_CR_VALID_STABLE 0x0000U
  49. #define U300_WDOG_CR_COUNT_VALUE_MASK 0x7FFFU
  50. /* WDOG_JTAGOVR Register 32bit (R/W) */
  51. #define U300_WDOG_JOR 0x18
  52. #define U300_WDOG_JOR_JTAG_MODE_IND 0x0002U
  53. #define U300_WDOG_JOR_JTAG_WATCHDOG_ENABLE 0x0001U
  54. /* WDOG_RESTART Register 32bit (-/W) */
  55. #define U300_WDOG_RR 0x1C
  56. #define U300_WDOG_RR_RESTART_VALUE_RESUME 0xACEDU
  57. /* WDOG_IRQ_EVENT Register 32bit (R/W) */
  58. #define U300_WDOG_IER 0x20
  59. #define U300_WDOG_IER_WILL_BARK_IRQ_EVENT_IND 0x0001U
  60. #define U300_WDOG_IER_WILL_BARK_IRQ_ACK_ENABLE 0x0001U
  61. /* WDOG_IRQ_MASK Register 32bit (R/W) */
  62. #define U300_WDOG_IMR 0x24
  63. #define U300_WDOG_IMR_WILL_BARK_IRQ_ENABLE 0x0001U
  64. /* WDOG_IRQ_FORCE Register 32bit (R/W) */
  65. #define U300_WDOG_IFR 0x28
  66. #define U300_WDOG_IFR_WILL_BARK_IRQ_FORCE_ENABLE 0x0001U
  67. /* Default timeout in seconds = 1 minute */
  68. static int margin = 60;
  69. static resource_size_t phybase;
  70. static resource_size_t physize;
  71. static int irq;
  72. static void __iomem *virtbase;
  73. static unsigned long coh901327_users;
  74. static unsigned long boot_status;
  75. static u16 wdogenablestore;
  76. static u16 irqmaskstore;
  77. static struct device *parent;
  78. /*
  79. * The watchdog block is of course always clocked, the
  80. * clk_enable()/clk_disable() calls are mainly for performing reference
  81. * counting higher up in the clock hierarchy.
  82. */
  83. static struct clk *clk;
  84. /*
  85. * Enabling and disabling functions.
  86. */
  87. static void coh901327_enable(u16 timeout)
  88. {
  89. u16 val;
  90. unsigned long freq;
  91. unsigned long delay_ns;
  92. clk_enable(clk);
  93. /* Restart timer if it is disabled */
  94. val = readw(virtbase + U300_WDOG_D2R);
  95. if (val == U300_WDOG_D2R_DISABLE_STATUS_DISABLED)
  96. writew(U300_WDOG_RR_RESTART_VALUE_RESUME,
  97. virtbase + U300_WDOG_RR);
  98. /* Acknowledge any pending interrupt so it doesn't just fire off */
  99. writew(U300_WDOG_IER_WILL_BARK_IRQ_ACK_ENABLE,
  100. virtbase + U300_WDOG_IER);
  101. /*
  102. * The interrupt is cleared in the 32 kHz clock domain.
  103. * Wait 3 32 kHz cycles for it to take effect
  104. */
  105. freq = clk_get_rate(clk);
  106. delay_ns = DIV_ROUND_UP(1000000000, freq); /* Freq to ns and round up */
  107. delay_ns = 3 * delay_ns; /* Wait 3 cycles */
  108. ndelay(delay_ns);
  109. /* Enable the watchdog interrupt */
  110. writew(U300_WDOG_IMR_WILL_BARK_IRQ_ENABLE, virtbase + U300_WDOG_IMR);
  111. /* Activate the watchdog timer */
  112. writew(timeout, virtbase + U300_WDOG_TR);
  113. /* Start the watchdog timer */
  114. writew(U300_WDOG_FR_FEED_RESTART_TIMER, virtbase + U300_WDOG_FR);
  115. /*
  116. * Extra read so that this change propagate in the watchdog.
  117. */
  118. (void) readw(virtbase + U300_WDOG_CR);
  119. val = readw(virtbase + U300_WDOG_D2R);
  120. clk_disable(clk);
  121. if (val != U300_WDOG_D2R_DISABLE_STATUS_ENABLED)
  122. dev_err(parent,
  123. "%s(): watchdog not enabled! D2R value %04x\n",
  124. __func__, val);
  125. }
  126. static void coh901327_disable(void)
  127. {
  128. u16 val;
  129. clk_enable(clk);
  130. /* Disable the watchdog interrupt if it is active */
  131. writew(0x0000U, virtbase + U300_WDOG_IMR);
  132. /* If the watchdog is currently enabled, attempt to disable it */
  133. val = readw(virtbase + U300_WDOG_D2R);
  134. if (val != U300_WDOG_D2R_DISABLE_STATUS_DISABLED) {
  135. writew(U300_WDOG_D1R_DISABLE1_DISABLE_TIMER,
  136. virtbase + U300_WDOG_D1R);
  137. writew(U300_WDOG_D2R_DISABLE2_DISABLE_TIMER,
  138. virtbase + U300_WDOG_D2R);
  139. /* Write this twice (else problems occur) */
  140. writew(U300_WDOG_D2R_DISABLE2_DISABLE_TIMER,
  141. virtbase + U300_WDOG_D2R);
  142. }
  143. val = readw(virtbase + U300_WDOG_D2R);
  144. clk_disable(clk);
  145. if (val != U300_WDOG_D2R_DISABLE_STATUS_DISABLED)
  146. dev_err(parent,
  147. "%s(): watchdog not disabled! D2R value %04x\n",
  148. __func__, val);
  149. }
  150. static void coh901327_start(void)
  151. {
  152. coh901327_enable(margin * 100);
  153. }
  154. static void coh901327_keepalive(void)
  155. {
  156. clk_enable(clk);
  157. /* Feed the watchdog */
  158. writew(U300_WDOG_FR_FEED_RESTART_TIMER,
  159. virtbase + U300_WDOG_FR);
  160. clk_disable(clk);
  161. }
  162. static int coh901327_settimeout(int time)
  163. {
  164. /*
  165. * Max margin is 327 since the 10ms
  166. * timeout register is max
  167. * 0x7FFF = 327670ms ~= 327s.
  168. */
  169. if (time <= 0 || time > 327)
  170. return -EINVAL;
  171. margin = time;
  172. clk_enable(clk);
  173. /* Set new timeout value */
  174. writew(margin * 100, virtbase + U300_WDOG_TR);
  175. /* Feed the dog */
  176. writew(U300_WDOG_FR_FEED_RESTART_TIMER,
  177. virtbase + U300_WDOG_FR);
  178. clk_disable(clk);
  179. return 0;
  180. }
  181. /*
  182. * This interrupt occurs 10 ms before the watchdog WILL bark.
  183. */
  184. static irqreturn_t coh901327_interrupt(int irq, void *data)
  185. {
  186. u16 val;
  187. /*
  188. * Ack IRQ? If this occurs we're FUBAR anyway, so
  189. * just acknowledge, disable the interrupt and await the imminent end.
  190. * If you at some point need a host of callbacks to be called
  191. * when the system is about to watchdog-reset, add them here!
  192. *
  193. * NOTE: on future versions of this IP-block, it will be possible
  194. * to prevent a watchdog reset by feeding the watchdog at this
  195. * point.
  196. */
  197. clk_enable(clk);
  198. val = readw(virtbase + U300_WDOG_IER);
  199. if (val == U300_WDOG_IER_WILL_BARK_IRQ_EVENT_IND)
  200. writew(U300_WDOG_IER_WILL_BARK_IRQ_ACK_ENABLE,
  201. virtbase + U300_WDOG_IER);
  202. writew(0x0000U, virtbase + U300_WDOG_IMR);
  203. clk_disable(clk);
  204. dev_crit(parent, "watchdog is barking!\n");
  205. return IRQ_HANDLED;
  206. }
  207. /*
  208. * Allow only one user (daemon) to open the watchdog
  209. */
  210. static int coh901327_open(struct inode *inode, struct file *file)
  211. {
  212. if (test_and_set_bit(1, &coh901327_users))
  213. return -EBUSY;
  214. coh901327_start();
  215. return nonseekable_open(inode, file);
  216. }
  217. static int coh901327_release(struct inode *inode, struct file *file)
  218. {
  219. clear_bit(1, &coh901327_users);
  220. coh901327_disable();
  221. return 0;
  222. }
  223. static ssize_t coh901327_write(struct file *file, const char __user *data,
  224. size_t len, loff_t *ppos)
  225. {
  226. if (len)
  227. coh901327_keepalive();
  228. return len;
  229. }
  230. static long coh901327_ioctl(struct file *file, unsigned int cmd,
  231. unsigned long arg)
  232. {
  233. int ret = -ENOTTY;
  234. u16 val;
  235. int time;
  236. int new_options;
  237. union {
  238. struct watchdog_info __user *ident;
  239. int __user *i;
  240. } uarg;
  241. static const struct watchdog_info ident = {
  242. .options = WDIOF_CARDRESET |
  243. WDIOF_SETTIMEOUT |
  244. WDIOF_KEEPALIVEPING,
  245. .identity = "COH 901 327 Watchdog",
  246. .firmware_version = 1,
  247. };
  248. uarg.i = (int __user *)arg;
  249. switch (cmd) {
  250. case WDIOC_GETSUPPORT:
  251. ret = copy_to_user(uarg.ident, &ident,
  252. sizeof(ident)) ? -EFAULT : 0;
  253. break;
  254. case WDIOC_GETSTATUS:
  255. ret = put_user(0, uarg.i);
  256. break;
  257. case WDIOC_GETBOOTSTATUS:
  258. ret = put_user(boot_status, uarg.i);
  259. break;
  260. case WDIOC_SETOPTIONS:
  261. ret = get_user(new_options, uarg.i);
  262. if (ret)
  263. break;
  264. if (new_options & WDIOS_DISABLECARD)
  265. coh901327_disable();
  266. if (new_options & WDIOS_ENABLECARD)
  267. coh901327_start();
  268. ret = 0;
  269. break;
  270. case WDIOC_KEEPALIVE:
  271. coh901327_keepalive();
  272. ret = 0;
  273. break;
  274. case WDIOC_SETTIMEOUT:
  275. ret = get_user(time, uarg.i);
  276. if (ret)
  277. break;
  278. ret = coh901327_settimeout(time);
  279. if (ret)
  280. break;
  281. /* Then fall through to return set value */
  282. case WDIOC_GETTIMEOUT:
  283. ret = put_user(margin, uarg.i);
  284. break;
  285. case WDIOC_GETTIMELEFT:
  286. clk_enable(clk);
  287. /* Read repeatedly until the value is stable! */
  288. val = readw(virtbase + U300_WDOG_CR);
  289. while (val & U300_WDOG_CR_VALID_IND)
  290. val = readw(virtbase + U300_WDOG_CR);
  291. val &= U300_WDOG_CR_COUNT_VALUE_MASK;
  292. clk_disable(clk);
  293. if (val != 0)
  294. val /= 100;
  295. ret = put_user(val, uarg.i);
  296. break;
  297. }
  298. return ret;
  299. }
  300. static const struct file_operations coh901327_fops = {
  301. .owner = THIS_MODULE,
  302. .llseek = no_llseek,
  303. .write = coh901327_write,
  304. .unlocked_ioctl = coh901327_ioctl,
  305. .open = coh901327_open,
  306. .release = coh901327_release,
  307. };
  308. static struct miscdevice coh901327_miscdev = {
  309. .minor = WATCHDOG_MINOR,
  310. .name = "watchdog",
  311. .fops = &coh901327_fops,
  312. };
  313. static int __exit coh901327_remove(struct platform_device *pdev)
  314. {
  315. misc_deregister(&coh901327_miscdev);
  316. coh901327_disable();
  317. free_irq(irq, pdev);
  318. clk_put(clk);
  319. iounmap(virtbase);
  320. release_mem_region(phybase, physize);
  321. return 0;
  322. }
  323. static int __init coh901327_probe(struct platform_device *pdev)
  324. {
  325. int ret;
  326. u16 val;
  327. struct resource *res;
  328. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  329. if (!res)
  330. return -ENOENT;
  331. parent = &pdev->dev;
  332. physize = resource_size(res);
  333. phybase = res->start;
  334. if (request_mem_region(phybase, physize, DRV_NAME) == NULL) {
  335. ret = -EBUSY;
  336. goto out;
  337. }
  338. virtbase = ioremap(phybase, physize);
  339. if (!virtbase) {
  340. ret = -ENOMEM;
  341. goto out_no_remap;
  342. }
  343. clk = clk_get(&pdev->dev, NULL);
  344. if (IS_ERR(clk)) {
  345. ret = PTR_ERR(clk);
  346. dev_err(&pdev->dev, "could not get clock\n");
  347. goto out_no_clk;
  348. }
  349. ret = clk_enable(clk);
  350. if (ret) {
  351. dev_err(&pdev->dev, "could not enable clock\n");
  352. goto out_no_clk_enable;
  353. }
  354. val = readw(virtbase + U300_WDOG_SR);
  355. switch (val) {
  356. case U300_WDOG_SR_STATUS_TIMED_OUT:
  357. dev_info(&pdev->dev,
  358. "watchdog timed out since last chip reset!\n");
  359. boot_status = WDIOF_CARDRESET;
  360. /* Status will be cleared below */
  361. break;
  362. case U300_WDOG_SR_STATUS_NORMAL:
  363. dev_info(&pdev->dev,
  364. "in normal status, no timeouts have occurred.\n");
  365. break;
  366. default:
  367. dev_info(&pdev->dev,
  368. "contains an illegal status code (%08x)\n", val);
  369. break;
  370. }
  371. val = readw(virtbase + U300_WDOG_D2R);
  372. switch (val) {
  373. case U300_WDOG_D2R_DISABLE_STATUS_DISABLED:
  374. dev_info(&pdev->dev, "currently disabled.\n");
  375. break;
  376. case U300_WDOG_D2R_DISABLE_STATUS_ENABLED:
  377. dev_info(&pdev->dev,
  378. "currently enabled! (disabling it now)\n");
  379. coh901327_disable();
  380. break;
  381. default:
  382. dev_err(&pdev->dev,
  383. "contains an illegal enable/disable code (%08x)\n",
  384. val);
  385. break;
  386. }
  387. /* Reset the watchdog */
  388. writew(U300_WDOG_SR_RESET_STATUS_RESET, virtbase + U300_WDOG_SR);
  389. irq = platform_get_irq(pdev, 0);
  390. if (request_irq(irq, coh901327_interrupt, IRQF_DISABLED,
  391. DRV_NAME " Bark", pdev)) {
  392. ret = -EIO;
  393. goto out_no_irq;
  394. }
  395. clk_disable(clk);
  396. ret = misc_register(&coh901327_miscdev);
  397. if (ret == 0)
  398. dev_info(&pdev->dev,
  399. "initialized. timer margin=%d sec\n", margin);
  400. else
  401. goto out_no_wdog;
  402. return 0;
  403. out_no_wdog:
  404. free_irq(irq, pdev);
  405. out_no_irq:
  406. clk_disable(clk);
  407. out_no_clk_enable:
  408. clk_put(clk);
  409. out_no_clk:
  410. iounmap(virtbase);
  411. out_no_remap:
  412. release_mem_region(phybase, SZ_4K);
  413. out:
  414. return ret;
  415. }
  416. #ifdef CONFIG_PM
  417. static int coh901327_suspend(struct platform_device *pdev, pm_message_t state)
  418. {
  419. irqmaskstore = readw(virtbase + U300_WDOG_IMR) & 0x0001U;
  420. wdogenablestore = readw(virtbase + U300_WDOG_D2R);
  421. /* If watchdog is on, disable it here and now */
  422. if (wdogenablestore == U300_WDOG_D2R_DISABLE_STATUS_ENABLED)
  423. coh901327_disable();
  424. return 0;
  425. }
  426. static int coh901327_resume(struct platform_device *pdev)
  427. {
  428. /* Restore the watchdog interrupt */
  429. writew(irqmaskstore, virtbase + U300_WDOG_IMR);
  430. if (wdogenablestore == U300_WDOG_D2R_DISABLE_STATUS_ENABLED) {
  431. /* Restart the watchdog timer */
  432. writew(U300_WDOG_RR_RESTART_VALUE_RESUME,
  433. virtbase + U300_WDOG_RR);
  434. writew(U300_WDOG_FR_FEED_RESTART_TIMER,
  435. virtbase + U300_WDOG_FR);
  436. }
  437. return 0;
  438. }
  439. #else
  440. #define coh901327_suspend NULL
  441. #define coh901327_resume NULL
  442. #endif
  443. /*
  444. * Mistreating the watchdog is the only way to perform a software reset of the
  445. * system on EMP platforms. So we implement this and export a symbol for it.
  446. */
  447. void coh901327_watchdog_reset(void)
  448. {
  449. /* Enable even if on JTAG too */
  450. writew(U300_WDOG_JOR_JTAG_WATCHDOG_ENABLE,
  451. virtbase + U300_WDOG_JOR);
  452. /*
  453. * Timeout = 5s, we have to wait for the watchdog reset to
  454. * actually take place: the watchdog will be reloaded with the
  455. * default value immediately, so we HAVE to reboot and get back
  456. * into the kernel in 30s, or the device will reboot again!
  457. * The boot loader will typically deactivate the watchdog, so we
  458. * need time enough for the boot loader to get to the point of
  459. * deactivating the watchdog before it is shut down by it.
  460. *
  461. * NOTE: on future versions of the watchdog, this restriction is
  462. * gone: the watchdog will be reloaded with a default value (1 min)
  463. * instead of last value, and you can conveniently set the watchdog
  464. * timeout to 10ms (value = 1) without any problems.
  465. */
  466. coh901327_enable(500);
  467. /* Return and await doom */
  468. }
  469. static struct platform_driver coh901327_driver = {
  470. .driver = {
  471. .owner = THIS_MODULE,
  472. .name = "coh901327_wdog",
  473. },
  474. .remove = __exit_p(coh901327_remove),
  475. .suspend = coh901327_suspend,
  476. .resume = coh901327_resume,
  477. };
  478. static int __init coh901327_init(void)
  479. {
  480. return platform_driver_probe(&coh901327_driver, coh901327_probe);
  481. }
  482. module_init(coh901327_init);
  483. static void __exit coh901327_exit(void)
  484. {
  485. platform_driver_unregister(&coh901327_driver);
  486. }
  487. module_exit(coh901327_exit);
  488. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  489. MODULE_DESCRIPTION("COH 901 327 Watchdog");
  490. module_param(margin, int, 0);
  491. MODULE_PARM_DESC(margin, "Watchdog margin in seconds (default 60s)");
  492. MODULE_LICENSE("GPL");
  493. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);