coh901327_wdt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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_unprepare(clk);
  245. clk_put(clk);
  246. iounmap(virtbase);
  247. release_mem_region(phybase, physize);
  248. return 0;
  249. }
  250. static int __init coh901327_probe(struct platform_device *pdev)
  251. {
  252. int ret;
  253. u16 val;
  254. struct resource *res;
  255. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  256. if (!res)
  257. return -ENOENT;
  258. parent = &pdev->dev;
  259. physize = resource_size(res);
  260. phybase = res->start;
  261. if (request_mem_region(phybase, physize, DRV_NAME) == NULL) {
  262. ret = -EBUSY;
  263. goto out;
  264. }
  265. virtbase = ioremap(phybase, physize);
  266. if (!virtbase) {
  267. ret = -ENOMEM;
  268. goto out_no_remap;
  269. }
  270. clk = clk_get(&pdev->dev, NULL);
  271. if (IS_ERR(clk)) {
  272. ret = PTR_ERR(clk);
  273. dev_err(&pdev->dev, "could not get clock\n");
  274. goto out_no_clk;
  275. }
  276. ret = clk_prepare_enable(clk);
  277. if (ret) {
  278. dev_err(&pdev->dev, "could not prepare and enable clock\n");
  279. goto out_no_clk_enable;
  280. }
  281. val = readw(virtbase + U300_WDOG_SR);
  282. switch (val) {
  283. case U300_WDOG_SR_STATUS_TIMED_OUT:
  284. dev_info(&pdev->dev,
  285. "watchdog timed out since last chip reset!\n");
  286. coh901327_wdt.bootstatus |= WDIOF_CARDRESET;
  287. /* Status will be cleared below */
  288. break;
  289. case U300_WDOG_SR_STATUS_NORMAL:
  290. dev_info(&pdev->dev,
  291. "in normal status, no timeouts have occurred.\n");
  292. break;
  293. default:
  294. dev_info(&pdev->dev,
  295. "contains an illegal status code (%08x)\n", val);
  296. break;
  297. }
  298. val = readw(virtbase + U300_WDOG_D2R);
  299. switch (val) {
  300. case U300_WDOG_D2R_DISABLE_STATUS_DISABLED:
  301. dev_info(&pdev->dev, "currently disabled.\n");
  302. break;
  303. case U300_WDOG_D2R_DISABLE_STATUS_ENABLED:
  304. dev_info(&pdev->dev,
  305. "currently enabled! (disabling it now)\n");
  306. coh901327_disable();
  307. break;
  308. default:
  309. dev_err(&pdev->dev,
  310. "contains an illegal enable/disable code (%08x)\n",
  311. val);
  312. break;
  313. }
  314. /* Reset the watchdog */
  315. writew(U300_WDOG_SR_RESET_STATUS_RESET, virtbase + U300_WDOG_SR);
  316. irq = platform_get_irq(pdev, 0);
  317. if (request_irq(irq, coh901327_interrupt, 0,
  318. DRV_NAME " Bark", pdev)) {
  319. ret = -EIO;
  320. goto out_no_irq;
  321. }
  322. clk_disable(clk);
  323. ret = watchdog_init_timeout(&coh901327_wdt, margin, &pdev->dev);
  324. if (ret < 0)
  325. coh901327_wdt.timeout = 60;
  326. coh901327_wdt.parent = &pdev->dev;
  327. ret = watchdog_register_device(&coh901327_wdt);
  328. if (ret == 0)
  329. dev_info(&pdev->dev,
  330. "initialized. timer margin=%d sec\n", margin);
  331. else
  332. goto out_no_wdog;
  333. return 0;
  334. out_no_wdog:
  335. free_irq(irq, pdev);
  336. out_no_irq:
  337. clk_disable_unprepare(clk);
  338. out_no_clk_enable:
  339. clk_put(clk);
  340. out_no_clk:
  341. iounmap(virtbase);
  342. out_no_remap:
  343. release_mem_region(phybase, SZ_4K);
  344. out:
  345. return ret;
  346. }
  347. #ifdef CONFIG_PM
  348. static u16 wdogenablestore;
  349. static u16 irqmaskstore;
  350. static int coh901327_suspend(struct platform_device *pdev, pm_message_t state)
  351. {
  352. irqmaskstore = readw(virtbase + U300_WDOG_IMR) & 0x0001U;
  353. wdogenablestore = readw(virtbase + U300_WDOG_D2R);
  354. /* If watchdog is on, disable it here and now */
  355. if (wdogenablestore == U300_WDOG_D2R_DISABLE_STATUS_ENABLED)
  356. coh901327_disable();
  357. return 0;
  358. }
  359. static int coh901327_resume(struct platform_device *pdev)
  360. {
  361. /* Restore the watchdog interrupt */
  362. writew(irqmaskstore, virtbase + U300_WDOG_IMR);
  363. if (wdogenablestore == U300_WDOG_D2R_DISABLE_STATUS_ENABLED) {
  364. /* Restart the watchdog timer */
  365. writew(U300_WDOG_RR_RESTART_VALUE_RESUME,
  366. virtbase + U300_WDOG_RR);
  367. writew(U300_WDOG_FR_FEED_RESTART_TIMER,
  368. virtbase + U300_WDOG_FR);
  369. }
  370. return 0;
  371. }
  372. #else
  373. #define coh901327_suspend NULL
  374. #define coh901327_resume NULL
  375. #endif
  376. /*
  377. * Mistreating the watchdog is the only way to perform a software reset of the
  378. * system on EMP platforms. So we implement this and export a symbol for it.
  379. */
  380. void coh901327_watchdog_reset(void)
  381. {
  382. /* Enable even if on JTAG too */
  383. writew(U300_WDOG_JOR_JTAG_WATCHDOG_ENABLE,
  384. virtbase + U300_WDOG_JOR);
  385. /*
  386. * Timeout = 5s, we have to wait for the watchdog reset to
  387. * actually take place: the watchdog will be reloaded with the
  388. * default value immediately, so we HAVE to reboot and get back
  389. * into the kernel in 30s, or the device will reboot again!
  390. * The boot loader will typically deactivate the watchdog, so we
  391. * need time enough for the boot loader to get to the point of
  392. * deactivating the watchdog before it is shut down by it.
  393. *
  394. * NOTE: on future versions of the watchdog, this restriction is
  395. * gone: the watchdog will be reloaded with a default value (1 min)
  396. * instead of last value, and you can conveniently set the watchdog
  397. * timeout to 10ms (value = 1) without any problems.
  398. */
  399. coh901327_enable(500);
  400. /* Return and await doom */
  401. }
  402. static const struct of_device_id coh901327_dt_match[] = {
  403. { .compatible = "stericsson,coh901327" },
  404. {},
  405. };
  406. static struct platform_driver coh901327_driver = {
  407. .driver = {
  408. .name = "coh901327_wdog",
  409. .of_match_table = coh901327_dt_match,
  410. },
  411. .remove = __exit_p(coh901327_remove),
  412. .suspend = coh901327_suspend,
  413. .resume = coh901327_resume,
  414. };
  415. module_platform_driver_probe(coh901327_driver, coh901327_probe);
  416. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  417. MODULE_DESCRIPTION("COH 901 327 Watchdog");
  418. module_param(margin, uint, 0);
  419. MODULE_PARM_DESC(margin, "Watchdog margin in seconds (default 60s)");
  420. MODULE_LICENSE("GPL");
  421. MODULE_ALIAS("platform:coh901327-watchdog");