pic32-wdt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PIC32 watchdog driver
  4. *
  5. * Joshua Henderson <joshua.henderson@microchip.com>
  6. * Copyright (c) 2016, Microchip Technology Inc.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_data/pic32.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm.h>
  18. #include <linux/watchdog.h>
  19. /* Watchdog Timer Registers */
  20. #define WDTCON_REG 0x00
  21. /* Watchdog Timer Control Register fields */
  22. #define WDTCON_WIN_EN BIT(0)
  23. #define WDTCON_RMCS_MASK 0x0003
  24. #define WDTCON_RMCS_SHIFT 0x0006
  25. #define WDTCON_RMPS_MASK 0x001F
  26. #define WDTCON_RMPS_SHIFT 0x0008
  27. #define WDTCON_ON BIT(15)
  28. #define WDTCON_CLR_KEY 0x5743
  29. /* Reset Control Register fields for watchdog */
  30. #define RESETCON_TIMEOUT_IDLE BIT(2)
  31. #define RESETCON_TIMEOUT_SLEEP BIT(3)
  32. #define RESETCON_WDT_TIMEOUT BIT(4)
  33. struct pic32_wdt {
  34. void __iomem *regs;
  35. void __iomem *rst_base;
  36. struct clk *clk;
  37. };
  38. static inline bool pic32_wdt_is_win_enabled(struct pic32_wdt *wdt)
  39. {
  40. return !!(readl(wdt->regs + WDTCON_REG) & WDTCON_WIN_EN);
  41. }
  42. static inline u32 pic32_wdt_get_post_scaler(struct pic32_wdt *wdt)
  43. {
  44. u32 v = readl(wdt->regs + WDTCON_REG);
  45. return (v >> WDTCON_RMPS_SHIFT) & WDTCON_RMPS_MASK;
  46. }
  47. static inline u32 pic32_wdt_get_clk_id(struct pic32_wdt *wdt)
  48. {
  49. u32 v = readl(wdt->regs + WDTCON_REG);
  50. return (v >> WDTCON_RMCS_SHIFT) & WDTCON_RMCS_MASK;
  51. }
  52. static int pic32_wdt_bootstatus(struct pic32_wdt *wdt)
  53. {
  54. u32 v = readl(wdt->rst_base);
  55. writel(RESETCON_WDT_TIMEOUT, PIC32_CLR(wdt->rst_base));
  56. return v & RESETCON_WDT_TIMEOUT;
  57. }
  58. static u32 pic32_wdt_get_timeout_secs(struct pic32_wdt *wdt, struct device *dev)
  59. {
  60. unsigned long rate;
  61. u32 period, ps, terminal;
  62. rate = clk_get_rate(wdt->clk);
  63. dev_dbg(dev, "wdt: clk_id %d, clk_rate %lu (prescale)\n",
  64. pic32_wdt_get_clk_id(wdt), rate);
  65. /* default, prescaler of 32 (i.e. div-by-32) is implicit. */
  66. rate >>= 5;
  67. if (!rate)
  68. return 0;
  69. /* calculate terminal count from postscaler. */
  70. ps = pic32_wdt_get_post_scaler(wdt);
  71. terminal = BIT(ps);
  72. /* find time taken (in secs) to reach terminal count */
  73. period = terminal / rate;
  74. dev_dbg(dev,
  75. "wdt: clk_rate %lu (postscale) / terminal %d, timeout %dsec\n",
  76. rate, terminal, period);
  77. return period;
  78. }
  79. static void pic32_wdt_keepalive(struct pic32_wdt *wdt)
  80. {
  81. /* write key through single half-word */
  82. writew(WDTCON_CLR_KEY, wdt->regs + WDTCON_REG + 2);
  83. }
  84. static int pic32_wdt_start(struct watchdog_device *wdd)
  85. {
  86. struct pic32_wdt *wdt = watchdog_get_drvdata(wdd);
  87. writel(WDTCON_ON, PIC32_SET(wdt->regs + WDTCON_REG));
  88. pic32_wdt_keepalive(wdt);
  89. return 0;
  90. }
  91. static int pic32_wdt_stop(struct watchdog_device *wdd)
  92. {
  93. struct pic32_wdt *wdt = watchdog_get_drvdata(wdd);
  94. writel(WDTCON_ON, PIC32_CLR(wdt->regs + WDTCON_REG));
  95. /*
  96. * Cannot touch registers in the CPU cycle following clearing the
  97. * ON bit.
  98. */
  99. nop();
  100. return 0;
  101. }
  102. static int pic32_wdt_ping(struct watchdog_device *wdd)
  103. {
  104. struct pic32_wdt *wdt = watchdog_get_drvdata(wdd);
  105. pic32_wdt_keepalive(wdt);
  106. return 0;
  107. }
  108. static const struct watchdog_ops pic32_wdt_fops = {
  109. .owner = THIS_MODULE,
  110. .start = pic32_wdt_start,
  111. .stop = pic32_wdt_stop,
  112. .ping = pic32_wdt_ping,
  113. };
  114. static const struct watchdog_info pic32_wdt_ident = {
  115. .options = WDIOF_KEEPALIVEPING |
  116. WDIOF_MAGICCLOSE | WDIOF_CARDRESET,
  117. .identity = "PIC32 Watchdog",
  118. };
  119. static struct watchdog_device pic32_wdd = {
  120. .info = &pic32_wdt_ident,
  121. .ops = &pic32_wdt_fops,
  122. };
  123. static const struct of_device_id pic32_wdt_dt_ids[] = {
  124. { .compatible = "microchip,pic32mzda-wdt", },
  125. { /* sentinel */ }
  126. };
  127. MODULE_DEVICE_TABLE(of, pic32_wdt_dt_ids);
  128. static int pic32_wdt_drv_probe(struct platform_device *pdev)
  129. {
  130. struct device *dev = &pdev->dev;
  131. int ret;
  132. struct watchdog_device *wdd = &pic32_wdd;
  133. struct pic32_wdt *wdt;
  134. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  135. if (!wdt)
  136. return -ENOMEM;
  137. wdt->regs = devm_platform_ioremap_resource(pdev, 0);
  138. if (IS_ERR(wdt->regs))
  139. return PTR_ERR(wdt->regs);
  140. wdt->rst_base = devm_ioremap(dev, PIC32_BASE_RESET, 0x10);
  141. if (!wdt->rst_base)
  142. return -ENOMEM;
  143. wdt->clk = devm_clk_get_enabled(dev, NULL);
  144. if (IS_ERR(wdt->clk)) {
  145. dev_err(dev, "clk not found\n");
  146. return PTR_ERR(wdt->clk);
  147. }
  148. if (pic32_wdt_is_win_enabled(wdt)) {
  149. dev_err(dev, "windowed-clear mode is not supported.\n");
  150. return -ENODEV;
  151. }
  152. wdd->timeout = pic32_wdt_get_timeout_secs(wdt, dev);
  153. if (!wdd->timeout) {
  154. dev_err(dev, "failed to read watchdog register timeout\n");
  155. return -EINVAL;
  156. }
  157. dev_info(dev, "timeout %d\n", wdd->timeout);
  158. wdd->bootstatus = pic32_wdt_bootstatus(wdt) ? WDIOF_CARDRESET : 0;
  159. watchdog_set_nowayout(wdd, WATCHDOG_NOWAYOUT);
  160. watchdog_set_drvdata(wdd, wdt);
  161. ret = devm_watchdog_register_device(dev, wdd);
  162. if (ret)
  163. return ret;
  164. platform_set_drvdata(pdev, wdd);
  165. return 0;
  166. }
  167. static struct platform_driver pic32_wdt_driver = {
  168. .probe = pic32_wdt_drv_probe,
  169. .driver = {
  170. .name = "pic32-wdt",
  171. .of_match_table = of_match_ptr(pic32_wdt_dt_ids),
  172. }
  173. };
  174. module_platform_driver(pic32_wdt_driver);
  175. MODULE_AUTHOR("Joshua Henderson <joshua.henderson@microchip.com>");
  176. MODULE_DESCRIPTION("Microchip PIC32 Watchdog Timer");
  177. MODULE_LICENSE("GPL");