npcm_wdt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2018 Nuvoton Technology corporation.
  3. // Copyright (c) 2018 IBM Corp.
  4. #include <linux/bitops.h>
  5. #include <linux/clk.h>
  6. #include <linux/delay.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/of_irq.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <linux/watchdog.h>
  14. #define NPCM_WTCR 0x1C
  15. #define NPCM_WTCLK (BIT(10) | BIT(11)) /* Clock divider */
  16. #define NPCM_WTE BIT(7) /* Enable */
  17. #define NPCM_WTIE BIT(6) /* Enable irq */
  18. #define NPCM_WTIS (BIT(4) | BIT(5)) /* Interval selection */
  19. #define NPCM_WTIF BIT(3) /* Interrupt flag*/
  20. #define NPCM_WTRF BIT(2) /* Reset flag */
  21. #define NPCM_WTRE BIT(1) /* Reset enable */
  22. #define NPCM_WTR BIT(0) /* Reset counter */
  23. /*
  24. * Watchdog timeouts
  25. *
  26. * 170 msec: WTCLK=01 WTIS=00 VAL= 0x400
  27. * 670 msec: WTCLK=01 WTIS=01 VAL= 0x410
  28. * 1360 msec: WTCLK=10 WTIS=00 VAL= 0x800
  29. * 2700 msec: WTCLK=01 WTIS=10 VAL= 0x420
  30. * 5360 msec: WTCLK=10 WTIS=01 VAL= 0x810
  31. * 10700 msec: WTCLK=01 WTIS=11 VAL= 0x430
  32. * 21600 msec: WTCLK=10 WTIS=10 VAL= 0x820
  33. * 43000 msec: WTCLK=11 WTIS=00 VAL= 0xC00
  34. * 85600 msec: WTCLK=10 WTIS=11 VAL= 0x830
  35. * 172000 msec: WTCLK=11 WTIS=01 VAL= 0xC10
  36. * 687000 msec: WTCLK=11 WTIS=10 VAL= 0xC20
  37. * 2750000 msec: WTCLK=11 WTIS=11 VAL= 0xC30
  38. */
  39. struct npcm_wdt {
  40. struct watchdog_device wdd;
  41. void __iomem *reg;
  42. struct clk *clk;
  43. };
  44. static inline struct npcm_wdt *to_npcm_wdt(struct watchdog_device *wdd)
  45. {
  46. return container_of(wdd, struct npcm_wdt, wdd);
  47. }
  48. static int npcm_wdt_ping(struct watchdog_device *wdd)
  49. {
  50. struct npcm_wdt *wdt = to_npcm_wdt(wdd);
  51. u32 val;
  52. val = readl(wdt->reg);
  53. writel(val | NPCM_WTR, wdt->reg);
  54. return 0;
  55. }
  56. static int npcm_wdt_start(struct watchdog_device *wdd)
  57. {
  58. struct npcm_wdt *wdt = to_npcm_wdt(wdd);
  59. u32 val;
  60. clk_prepare_enable(wdt->clk);
  61. if (wdd->timeout < 2)
  62. val = 0x800;
  63. else if (wdd->timeout < 3)
  64. val = 0x420;
  65. else if (wdd->timeout < 6)
  66. val = 0x810;
  67. else if (wdd->timeout < 11)
  68. val = 0x430;
  69. else if (wdd->timeout < 22)
  70. val = 0x820;
  71. else if (wdd->timeout < 44)
  72. val = 0xC00;
  73. else if (wdd->timeout < 87)
  74. val = 0x830;
  75. else if (wdd->timeout < 173)
  76. val = 0xC10;
  77. else if (wdd->timeout < 688)
  78. val = 0xC20;
  79. else
  80. val = 0xC30;
  81. val |= NPCM_WTRE | NPCM_WTE | NPCM_WTR | NPCM_WTIE;
  82. writel(val, wdt->reg);
  83. return 0;
  84. }
  85. static int npcm_wdt_stop(struct watchdog_device *wdd)
  86. {
  87. struct npcm_wdt *wdt = to_npcm_wdt(wdd);
  88. writel(0, wdt->reg);
  89. clk_disable_unprepare(wdt->clk);
  90. return 0;
  91. }
  92. static int npcm_wdt_set_timeout(struct watchdog_device *wdd,
  93. unsigned int timeout)
  94. {
  95. if (timeout < 2)
  96. wdd->timeout = 1;
  97. else if (timeout < 3)
  98. wdd->timeout = 2;
  99. else if (timeout < 6)
  100. wdd->timeout = 5;
  101. else if (timeout < 11)
  102. wdd->timeout = 10;
  103. else if (timeout < 22)
  104. wdd->timeout = 21;
  105. else if (timeout < 44)
  106. wdd->timeout = 43;
  107. else if (timeout < 87)
  108. wdd->timeout = 86;
  109. else if (timeout < 173)
  110. wdd->timeout = 172;
  111. else if (timeout < 688)
  112. wdd->timeout = 687;
  113. else
  114. wdd->timeout = 2750;
  115. if (watchdog_active(wdd))
  116. npcm_wdt_start(wdd);
  117. return 0;
  118. }
  119. static irqreturn_t npcm_wdt_interrupt(int irq, void *data)
  120. {
  121. struct npcm_wdt *wdt = data;
  122. watchdog_notify_pretimeout(&wdt->wdd);
  123. return IRQ_HANDLED;
  124. }
  125. static int npcm_wdt_restart(struct watchdog_device *wdd,
  126. unsigned long action, void *data)
  127. {
  128. struct npcm_wdt *wdt = to_npcm_wdt(wdd);
  129. /* For reset, we start the WDT clock and leave it running. */
  130. clk_prepare_enable(wdt->clk);
  131. writel(NPCM_WTR | NPCM_WTRE | NPCM_WTE, wdt->reg);
  132. udelay(1000);
  133. return 0;
  134. }
  135. static bool npcm_is_running(struct watchdog_device *wdd)
  136. {
  137. struct npcm_wdt *wdt = to_npcm_wdt(wdd);
  138. return readl(wdt->reg) & NPCM_WTE;
  139. }
  140. static const struct watchdog_info npcm_wdt_info = {
  141. .identity = KBUILD_MODNAME,
  142. .options = WDIOF_SETTIMEOUT
  143. | WDIOF_KEEPALIVEPING
  144. | WDIOF_MAGICCLOSE,
  145. };
  146. static const struct watchdog_ops npcm_wdt_ops = {
  147. .owner = THIS_MODULE,
  148. .start = npcm_wdt_start,
  149. .stop = npcm_wdt_stop,
  150. .ping = npcm_wdt_ping,
  151. .set_timeout = npcm_wdt_set_timeout,
  152. .restart = npcm_wdt_restart,
  153. };
  154. static int npcm_wdt_probe(struct platform_device *pdev)
  155. {
  156. struct device *dev = &pdev->dev;
  157. struct npcm_wdt *wdt;
  158. int irq;
  159. int ret;
  160. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  161. if (!wdt)
  162. return -ENOMEM;
  163. wdt->reg = devm_platform_ioremap_resource(pdev, 0);
  164. if (IS_ERR(wdt->reg))
  165. return PTR_ERR(wdt->reg);
  166. wdt->clk = devm_clk_get_optional(&pdev->dev, NULL);
  167. if (IS_ERR(wdt->clk))
  168. return PTR_ERR(wdt->clk);
  169. irq = platform_get_irq(pdev, 0);
  170. if (irq < 0)
  171. return irq;
  172. wdt->wdd.info = &npcm_wdt_info;
  173. wdt->wdd.ops = &npcm_wdt_ops;
  174. wdt->wdd.min_timeout = 1;
  175. wdt->wdd.max_timeout = 2750;
  176. wdt->wdd.parent = dev;
  177. wdt->wdd.timeout = 86;
  178. watchdog_init_timeout(&wdt->wdd, 0, dev);
  179. /* Ensure timeout is able to be represented by the hardware */
  180. npcm_wdt_set_timeout(&wdt->wdd, wdt->wdd.timeout);
  181. if (npcm_is_running(&wdt->wdd)) {
  182. /* Restart with the default or device-tree specified timeout */
  183. npcm_wdt_start(&wdt->wdd);
  184. set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
  185. }
  186. ret = devm_request_irq(dev, irq, npcm_wdt_interrupt, 0, "watchdog",
  187. wdt);
  188. if (ret)
  189. return ret;
  190. ret = devm_watchdog_register_device(dev, &wdt->wdd);
  191. if (ret)
  192. return ret;
  193. dev_info(dev, "NPCM watchdog driver enabled\n");
  194. return 0;
  195. }
  196. #ifdef CONFIG_OF
  197. static const struct of_device_id npcm_wdt_match[] = {
  198. {.compatible = "nuvoton,wpcm450-wdt"},
  199. {.compatible = "nuvoton,npcm750-wdt"},
  200. {},
  201. };
  202. MODULE_DEVICE_TABLE(of, npcm_wdt_match);
  203. #endif
  204. static struct platform_driver npcm_wdt_driver = {
  205. .probe = npcm_wdt_probe,
  206. .driver = {
  207. .name = "npcm-wdt",
  208. .of_match_table = of_match_ptr(npcm_wdt_match),
  209. },
  210. };
  211. module_platform_driver(npcm_wdt_driver);
  212. MODULE_AUTHOR("Joel Stanley");
  213. MODULE_DESCRIPTION("Watchdog driver for NPCM");
  214. MODULE_LICENSE("GPL v2");