coh901327_wdt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 struct device *parent;
  76. /*
  77. * The watchdog block is of course always clocked, the
  78. * clk_enable()/clk_disable() calls are mainly for performing reference
  79. * counting higher up in the clock hierarchy.
  80. */
  81. static struct clk *clk;
  82. /*
  83. * Enabling and disabling functions.
  84. */
  85. static void coh901327_enable(u16 timeout)
  86. {
  87. u16 val;
  88. unsigned long freq;
  89. unsigned long delay_ns;
  90. clk_enable(clk);
  91. /* Restart timer if it is disabled */
  92. val = readw(virtbase + U300_WDOG_D2R);
  93. if (val == U300_WDOG_D2R_DISABLE_STATUS_DISABLED)
  94. writew(U300_WDOG_RR_RESTART_VALUE_RESUME,
  95. virtbase + U300_WDOG_RR);
  96. /* Acknowledge any pending interrupt so it doesn't just fire off */
  97. writew(U300_WDOG_IER_WILL_BARK_IRQ_ACK_ENABLE,
  98. virtbase + U300_WDOG_IER);
  99. /*
  100. * The interrupt is cleared in the 32 kHz clock domain.
  101. * Wait 3 32 kHz cycles for it to take effect
  102. */
  103. freq = clk_get_rate(clk);
  104. delay_ns = DIV_ROUND_UP(1000000000, freq); /* Freq to ns and round up */
  105. delay_ns = 3 * delay_ns; /* Wait 3 cycles */
  106. ndelay(delay_ns);
  107. /* Enable the watchdog interrupt */
  108. writew(U300_WDOG_IMR_WILL_BARK_IRQ_ENABLE, virtbase + U300_WDOG_IMR);
  109. /* Activate the watchdog timer */
  110. writew(timeout, virtbase + U300_WDOG_TR);
  111. /* Start the watchdog timer */
  112. writew(U300_WDOG_FR_FEED_RESTART_TIMER, virtbase + U300_WDOG_FR);
  113. /*
  114. * Extra read so that this change propagate in the watchdog.
  115. */
  116. (void) readw(virtbase + U300_WDOG_CR);
  117. val = readw(virtbase + U300_WDOG_D2R);
  118. clk_disable(clk);
  119. if (val != U300_WDOG_D2R_DISABLE_STATUS_ENABLED)
  120. dev_err(parent,
  121. "%s(): watchdog not enabled! D2R value %04x\n",
  122. __func__, val);
  123. }
  124. static void coh901327_disable(void)
  125. {
  126. u16 val;
  127. clk_enable(clk);
  128. /* Disable the watchdog interrupt if it is active */
  129. writew(0x0000U, virtbase + U300_WDOG_IMR);
  130. /* If the watchdog is currently enabled, attempt to disable it */
  131. val = readw(virtbase + U300_WDOG_D2R);
  132. if (val != U300_WDOG_D2R_DISABLE_STATUS_DISABLED) {
  133. writew(U300_WDOG_D1R_DISABLE1_DISABLE_TIMER,
  134. virtbase + U300_WDOG_D1R);
  135. writew(U300_WDOG_D2R_DISABLE2_DISABLE_TIMER,
  136. virtbase + U300_WDOG_D2R);
  137. /* Write this twice (else problems occur) */
  138. writew(U300_WDOG_D2R_DISABLE2_DISABLE_TIMER,
  139. virtbase + U300_WDOG_D2R);
  140. }
  141. val = readw(virtbase + U300_WDOG_D2R);
  142. clk_disable(clk);
  143. if (val != U300_WDOG_D2R_DISABLE_STATUS_DISABLED)
  144. dev_err(parent,
  145. "%s(): watchdog not disabled! D2R value %04x\n",
  146. __func__, val);
  147. }
  148. static void coh901327_start(void)
  149. {
  150. coh901327_enable(margin * 100);
  151. }
  152. static void coh901327_keepalive(void)
  153. {
  154. clk_enable(clk);
  155. /* Feed the watchdog */
  156. writew(U300_WDOG_FR_FEED_RESTART_TIMER,
  157. virtbase + U300_WDOG_FR);
  158. clk_disable(clk);
  159. }
  160. static int coh901327_settimeout(int time)
  161. {
  162. /*
  163. * Max margin is 327 since the 10ms
  164. * timeout register is max
  165. * 0x7FFF = 327670ms ~= 327s.
  166. */
  167. if (time <= 0 || time > 327)
  168. return -EINVAL;
  169. margin = time;
  170. clk_enable(clk);
  171. /* Set new timeout value */
  172. writew(margin * 100, virtbase + U300_WDOG_TR);
  173. /* Feed the dog */
  174. writew(U300_WDOG_FR_FEED_RESTART_TIMER,
  175. virtbase + U300_WDOG_FR);
  176. clk_disable(clk);
  177. return 0;
  178. }
  179. /*
  180. * This interrupt occurs 10 ms before the watchdog WILL bark.
  181. */
  182. static irqreturn_t coh901327_interrupt(int irq, void *data)
  183. {
  184. u16 val;
  185. /*
  186. * Ack IRQ? If this occurs we're FUBAR anyway, so
  187. * just acknowledge, disable the interrupt and await the imminent end.
  188. * If you at some point need a host of callbacks to be called
  189. * when the system is about to watchdog-reset, add them here!
  190. *
  191. * NOTE: on future versions of this IP-block, it will be possible
  192. * to prevent a watchdog reset by feeding the watchdog at this
  193. * point.
  194. */
  195. clk_enable(clk);
  196. val = readw(virtbase + U300_WDOG_IER);
  197. if (val == U300_WDOG_IER_WILL_BARK_IRQ_EVENT_IND)
  198. writew(U300_WDOG_IER_WILL_BARK_IRQ_ACK_ENABLE,
  199. virtbase + U300_WDOG_IER);
  200. writew(0x0000U, virtbase + U300_WDOG_IMR);
  201. clk_disable(clk);
  202. dev_crit(parent, "watchdog is barking!\n");
  203. return IRQ_HANDLED;
  204. }
  205. /*
  206. * Allow only one user (daemon) to open the watchdog
  207. */
  208. static int coh901327_open(struct inode *inode, struct file *file)
  209. {
  210. if (test_and_set_bit(1, &coh901327_users))
  211. return -EBUSY;
  212. coh901327_start();
  213. return nonseekable_open(inode, file);
  214. }
  215. static int coh901327_release(struct inode *inode, struct file *file)
  216. {
  217. clear_bit(1, &coh901327_users);
  218. coh901327_disable();
  219. return 0;
  220. }
  221. static ssize_t coh901327_write(struct file *file, const char __user *data,
  222. size_t len, loff_t *ppos)
  223. {
  224. if (len)
  225. coh901327_keepalive();
  226. return len;
  227. }
  228. static long coh901327_ioctl(struct file *file, unsigned int cmd,
  229. unsigned long arg)
  230. {
  231. int ret = -ENOTTY;
  232. u16 val;
  233. int time;
  234. int new_options;
  235. union {
  236. struct watchdog_info __user *ident;
  237. int __user *i;
  238. } uarg;
  239. static const struct watchdog_info ident = {
  240. .options = WDIOF_CARDRESET |
  241. WDIOF_SETTIMEOUT |
  242. WDIOF_KEEPALIVEPING,
  243. .identity = "COH 901 327 Watchdog",
  244. .firmware_version = 1,
  245. };
  246. uarg.i = (int __user *)arg;
  247. switch (cmd) {
  248. case WDIOC_GETSUPPORT:
  249. ret = copy_to_user(uarg.ident, &ident,
  250. sizeof(ident)) ? -EFAULT : 0;
  251. break;
  252. case WDIOC_GETSTATUS:
  253. ret = put_user(0, uarg.i);
  254. break;
  255. case WDIOC_GETBOOTSTATUS:
  256. ret = put_user(boot_status, uarg.i);
  257. break;
  258. case WDIOC_SETOPTIONS:
  259. ret = get_user(new_options, uarg.i);
  260. if (ret)
  261. break;
  262. if (new_options & WDIOS_DISABLECARD)
  263. coh901327_disable();
  264. if (new_options & WDIOS_ENABLECARD)
  265. coh901327_start();
  266. ret = 0;
  267. break;
  268. case WDIOC_KEEPALIVE:
  269. coh901327_keepalive();
  270. ret = 0;
  271. break;
  272. case WDIOC_SETTIMEOUT:
  273. ret = get_user(time, uarg.i);
  274. if (ret)
  275. break;
  276. ret = coh901327_settimeout(time);
  277. if (ret)
  278. break;
  279. /* Then fall through to return set value */
  280. case WDIOC_GETTIMEOUT:
  281. ret = put_user(margin, uarg.i);
  282. break;
  283. case WDIOC_GETTIMELEFT:
  284. clk_enable(clk);
  285. /* Read repeatedly until the value is stable! */
  286. val = readw(virtbase + U300_WDOG_CR);
  287. while (val & U300_WDOG_CR_VALID_IND)
  288. val = readw(virtbase + U300_WDOG_CR);
  289. val &= U300_WDOG_CR_COUNT_VALUE_MASK;
  290. clk_disable(clk);
  291. if (val != 0)
  292. val /= 100;
  293. ret = put_user(val, uarg.i);
  294. break;
  295. }
  296. return ret;
  297. }
  298. static const struct file_operations coh901327_fops = {
  299. .owner = THIS_MODULE,
  300. .llseek = no_llseek,
  301. .write = coh901327_write,
  302. .unlocked_ioctl = coh901327_ioctl,
  303. .open = coh901327_open,
  304. .release = coh901327_release,
  305. };
  306. static struct miscdevice coh901327_miscdev = {
  307. .minor = WATCHDOG_MINOR,
  308. .name = "watchdog",
  309. .fops = &coh901327_fops,
  310. };
  311. static int __exit coh901327_remove(struct platform_device *pdev)
  312. {
  313. misc_deregister(&coh901327_miscdev);
  314. coh901327_disable();
  315. free_irq(irq, pdev);
  316. clk_put(clk);
  317. iounmap(virtbase);
  318. release_mem_region(phybase, physize);
  319. return 0;
  320. }
  321. static int __init coh901327_probe(struct platform_device *pdev)
  322. {
  323. int ret;
  324. u16 val;
  325. struct resource *res;
  326. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  327. if (!res)
  328. return -ENOENT;
  329. parent = &pdev->dev;
  330. physize = resource_size(res);
  331. phybase = res->start;
  332. if (request_mem_region(phybase, physize, DRV_NAME) == NULL) {
  333. ret = -EBUSY;
  334. goto out;
  335. }
  336. virtbase = ioremap(phybase, physize);
  337. if (!virtbase) {
  338. ret = -ENOMEM;
  339. goto out_no_remap;
  340. }
  341. clk = clk_get(&pdev->dev, NULL);
  342. if (IS_ERR(clk)) {
  343. ret = PTR_ERR(clk);
  344. dev_err(&pdev->dev, "could not get clock\n");
  345. goto out_no_clk;
  346. }
  347. ret = clk_enable(clk);
  348. if (ret) {
  349. dev_err(&pdev->dev, "could not enable clock\n");
  350. goto out_no_clk_enable;
  351. }
  352. val = readw(virtbase + U300_WDOG_SR);
  353. switch (val) {
  354. case U300_WDOG_SR_STATUS_TIMED_OUT:
  355. dev_info(&pdev->dev,
  356. "watchdog timed out since last chip reset!\n");
  357. boot_status = WDIOF_CARDRESET;
  358. /* Status will be cleared below */
  359. break;
  360. case U300_WDOG_SR_STATUS_NORMAL:
  361. dev_info(&pdev->dev,
  362. "in normal status, no timeouts have occurred.\n");
  363. break;
  364. default:
  365. dev_info(&pdev->dev,
  366. "contains an illegal status code (%08x)\n", val);
  367. break;
  368. }
  369. val = readw(virtbase + U300_WDOG_D2R);
  370. switch (val) {
  371. case U300_WDOG_D2R_DISABLE_STATUS_DISABLED:
  372. dev_info(&pdev->dev, "currently disabled.\n");
  373. break;
  374. case U300_WDOG_D2R_DISABLE_STATUS_ENABLED:
  375. dev_info(&pdev->dev,
  376. "currently enabled! (disabling it now)\n");
  377. coh901327_disable();
  378. break;
  379. default:
  380. dev_err(&pdev->dev,
  381. "contains an illegal enable/disable code (%08x)\n",
  382. val);
  383. break;
  384. }
  385. /* Reset the watchdog */
  386. writew(U300_WDOG_SR_RESET_STATUS_RESET, virtbase + U300_WDOG_SR);
  387. irq = platform_get_irq(pdev, 0);
  388. if (request_irq(irq, coh901327_interrupt, 0,
  389. DRV_NAME " Bark", pdev)) {
  390. ret = -EIO;
  391. goto out_no_irq;
  392. }
  393. clk_disable(clk);
  394. ret = misc_register(&coh901327_miscdev);
  395. if (ret == 0)
  396. dev_info(&pdev->dev,
  397. "initialized. timer margin=%d sec\n", margin);
  398. else
  399. goto out_no_wdog;
  400. return 0;
  401. out_no_wdog:
  402. free_irq(irq, pdev);
  403. out_no_irq:
  404. clk_disable(clk);
  405. out_no_clk_enable:
  406. clk_put(clk);
  407. out_no_clk:
  408. iounmap(virtbase);
  409. out_no_remap:
  410. release_mem_region(phybase, SZ_4K);
  411. out:
  412. return ret;
  413. }
  414. #ifdef CONFIG_PM
  415. static u16 wdogenablestore;
  416. static u16 irqmaskstore;
  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);