coh901327_wdt.c 13 KB

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