imgpdc_wdt.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Imagination Technologies PowerDown Controller Watchdog Timer.
  3. *
  4. * Copyright (c) 2014 Imagination Technologies Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * Based on drivers/watchdog/sunxi_wdt.c Copyright (c) 2013 Carlo Caione
  11. * 2012 Henrik Nordstrom
  12. *
  13. * Notes
  14. * -----
  15. * The timeout value is rounded to the next power of two clock cycles.
  16. * This is configured using the PDC_WDT_CONFIG register, according to this
  17. * formula:
  18. *
  19. * timeout = 2^(delay + 1) clock cycles
  20. *
  21. * Where 'delay' is the value written in PDC_WDT_CONFIG register.
  22. *
  23. * Therefore, the hardware only allows to program watchdog timeouts, expressed
  24. * as a power of two number of watchdog clock cycles. The current implementation
  25. * guarantees that the actual watchdog timeout will be _at least_ the value
  26. * programmed in the imgpdg_wdt driver.
  27. *
  28. * The following table shows how the user-configured timeout relates
  29. * to the actual hardware timeout (watchdog clock @ 40000 Hz):
  30. *
  31. * input timeout | WD_DELAY | actual timeout
  32. * -----------------------------------
  33. * 10 | 18 | 13 seconds
  34. * 20 | 19 | 26 seconds
  35. * 30 | 20 | 52 seconds
  36. * 60 | 21 | 104 seconds
  37. *
  38. * Albeit coarse, this granularity would suffice most watchdog uses.
  39. * If the platform allows it, the user should be able to change the watchdog
  40. * clock rate and achieve a finer timeout granularity.
  41. */
  42. #include <linux/clk.h>
  43. #include <linux/io.h>
  44. #include <linux/log2.h>
  45. #include <linux/module.h>
  46. #include <linux/platform_device.h>
  47. #include <linux/slab.h>
  48. #include <linux/watchdog.h>
  49. /* registers */
  50. #define PDC_WDT_SOFT_RESET 0x00
  51. #define PDC_WDT_CONFIG 0x04
  52. #define PDC_WDT_CONFIG_ENABLE BIT(31)
  53. #define PDC_WDT_CONFIG_DELAY_MASK 0x1f
  54. #define PDC_WDT_TICKLE1 0x08
  55. #define PDC_WDT_TICKLE1_MAGIC 0xabcd1234
  56. #define PDC_WDT_TICKLE2 0x0c
  57. #define PDC_WDT_TICKLE2_MAGIC 0x4321dcba
  58. #define PDC_WDT_TICKLE_STATUS_MASK 0x7
  59. #define PDC_WDT_TICKLE_STATUS_SHIFT 0
  60. #define PDC_WDT_TICKLE_STATUS_HRESET 0x0 /* Hard reset */
  61. #define PDC_WDT_TICKLE_STATUS_TIMEOUT 0x1 /* Timeout */
  62. #define PDC_WDT_TICKLE_STATUS_TICKLE 0x2 /* Tickled incorrectly */
  63. #define PDC_WDT_TICKLE_STATUS_SRESET 0x3 /* Soft reset */
  64. #define PDC_WDT_TICKLE_STATUS_USER 0x4 /* User reset */
  65. /* Timeout values are in seconds */
  66. #define PDC_WDT_MIN_TIMEOUT 1
  67. #define PDC_WDT_DEF_TIMEOUT 64
  68. static int heartbeat;
  69. module_param(heartbeat, int, 0);
  70. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds "
  71. "(default=" __MODULE_STRING(PDC_WDT_DEF_TIMEOUT) ")");
  72. static bool nowayout = WATCHDOG_NOWAYOUT;
  73. module_param(nowayout, bool, 0);
  74. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  75. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  76. struct pdc_wdt_dev {
  77. struct watchdog_device wdt_dev;
  78. struct clk *wdt_clk;
  79. struct clk *sys_clk;
  80. void __iomem *base;
  81. };
  82. static int pdc_wdt_keepalive(struct watchdog_device *wdt_dev)
  83. {
  84. struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
  85. writel(PDC_WDT_TICKLE1_MAGIC, wdt->base + PDC_WDT_TICKLE1);
  86. writel(PDC_WDT_TICKLE2_MAGIC, wdt->base + PDC_WDT_TICKLE2);
  87. return 0;
  88. }
  89. static int pdc_wdt_stop(struct watchdog_device *wdt_dev)
  90. {
  91. unsigned int val;
  92. struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
  93. val = readl(wdt->base + PDC_WDT_CONFIG);
  94. val &= ~PDC_WDT_CONFIG_ENABLE;
  95. writel(val, wdt->base + PDC_WDT_CONFIG);
  96. /* Must tickle to finish the stop */
  97. pdc_wdt_keepalive(wdt_dev);
  98. return 0;
  99. }
  100. static void __pdc_wdt_set_timeout(struct pdc_wdt_dev *wdt)
  101. {
  102. unsigned long clk_rate = clk_get_rate(wdt->wdt_clk);
  103. unsigned int val;
  104. val = readl(wdt->base + PDC_WDT_CONFIG) & ~PDC_WDT_CONFIG_DELAY_MASK;
  105. val |= order_base_2(wdt->wdt_dev.timeout * clk_rate) - 1;
  106. writel(val, wdt->base + PDC_WDT_CONFIG);
  107. }
  108. static int pdc_wdt_set_timeout(struct watchdog_device *wdt_dev,
  109. unsigned int new_timeout)
  110. {
  111. struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
  112. wdt->wdt_dev.timeout = new_timeout;
  113. __pdc_wdt_set_timeout(wdt);
  114. return 0;
  115. }
  116. /* Start the watchdog timer (delay should already be set) */
  117. static int pdc_wdt_start(struct watchdog_device *wdt_dev)
  118. {
  119. unsigned int val;
  120. struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
  121. __pdc_wdt_set_timeout(wdt);
  122. val = readl(wdt->base + PDC_WDT_CONFIG);
  123. val |= PDC_WDT_CONFIG_ENABLE;
  124. writel(val, wdt->base + PDC_WDT_CONFIG);
  125. return 0;
  126. }
  127. static int pdc_wdt_restart(struct watchdog_device *wdt_dev,
  128. unsigned long action, void *data)
  129. {
  130. struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
  131. /* Assert SOFT_RESET */
  132. writel(0x1, wdt->base + PDC_WDT_SOFT_RESET);
  133. return 0;
  134. }
  135. static struct watchdog_info pdc_wdt_info = {
  136. .identity = "IMG PDC Watchdog",
  137. .options = WDIOF_SETTIMEOUT |
  138. WDIOF_KEEPALIVEPING |
  139. WDIOF_MAGICCLOSE,
  140. };
  141. static const struct watchdog_ops pdc_wdt_ops = {
  142. .owner = THIS_MODULE,
  143. .start = pdc_wdt_start,
  144. .stop = pdc_wdt_stop,
  145. .ping = pdc_wdt_keepalive,
  146. .set_timeout = pdc_wdt_set_timeout,
  147. .restart = pdc_wdt_restart,
  148. };
  149. static int pdc_wdt_probe(struct platform_device *pdev)
  150. {
  151. u64 div;
  152. int ret, val;
  153. unsigned long clk_rate;
  154. struct resource *res;
  155. struct pdc_wdt_dev *pdc_wdt;
  156. pdc_wdt = devm_kzalloc(&pdev->dev, sizeof(*pdc_wdt), GFP_KERNEL);
  157. if (!pdc_wdt)
  158. return -ENOMEM;
  159. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  160. pdc_wdt->base = devm_ioremap_resource(&pdev->dev, res);
  161. if (IS_ERR(pdc_wdt->base))
  162. return PTR_ERR(pdc_wdt->base);
  163. pdc_wdt->sys_clk = devm_clk_get(&pdev->dev, "sys");
  164. if (IS_ERR(pdc_wdt->sys_clk)) {
  165. dev_err(&pdev->dev, "failed to get the sys clock\n");
  166. return PTR_ERR(pdc_wdt->sys_clk);
  167. }
  168. pdc_wdt->wdt_clk = devm_clk_get(&pdev->dev, "wdt");
  169. if (IS_ERR(pdc_wdt->wdt_clk)) {
  170. dev_err(&pdev->dev, "failed to get the wdt clock\n");
  171. return PTR_ERR(pdc_wdt->wdt_clk);
  172. }
  173. ret = clk_prepare_enable(pdc_wdt->sys_clk);
  174. if (ret) {
  175. dev_err(&pdev->dev, "could not prepare or enable sys clock\n");
  176. return ret;
  177. }
  178. ret = clk_prepare_enable(pdc_wdt->wdt_clk);
  179. if (ret) {
  180. dev_err(&pdev->dev, "could not prepare or enable wdt clock\n");
  181. goto disable_sys_clk;
  182. }
  183. /* We use the clock rate to calculate the max timeout */
  184. clk_rate = clk_get_rate(pdc_wdt->wdt_clk);
  185. if (clk_rate == 0) {
  186. dev_err(&pdev->dev, "failed to get clock rate\n");
  187. ret = -EINVAL;
  188. goto disable_wdt_clk;
  189. }
  190. if (order_base_2(clk_rate) > PDC_WDT_CONFIG_DELAY_MASK + 1) {
  191. dev_err(&pdev->dev, "invalid clock rate\n");
  192. ret = -EINVAL;
  193. goto disable_wdt_clk;
  194. }
  195. if (order_base_2(clk_rate) == 0)
  196. pdc_wdt->wdt_dev.min_timeout = PDC_WDT_MIN_TIMEOUT + 1;
  197. else
  198. pdc_wdt->wdt_dev.min_timeout = PDC_WDT_MIN_TIMEOUT;
  199. pdc_wdt->wdt_dev.info = &pdc_wdt_info;
  200. pdc_wdt->wdt_dev.ops = &pdc_wdt_ops;
  201. div = 1ULL << (PDC_WDT_CONFIG_DELAY_MASK + 1);
  202. do_div(div, clk_rate);
  203. pdc_wdt->wdt_dev.max_timeout = div;
  204. pdc_wdt->wdt_dev.timeout = PDC_WDT_DEF_TIMEOUT;
  205. pdc_wdt->wdt_dev.parent = &pdev->dev;
  206. watchdog_set_drvdata(&pdc_wdt->wdt_dev, pdc_wdt);
  207. watchdog_init_timeout(&pdc_wdt->wdt_dev, heartbeat, &pdev->dev);
  208. pdc_wdt_stop(&pdc_wdt->wdt_dev);
  209. /* Find what caused the last reset */
  210. val = readl(pdc_wdt->base + PDC_WDT_TICKLE1);
  211. val = (val & PDC_WDT_TICKLE_STATUS_MASK) >> PDC_WDT_TICKLE_STATUS_SHIFT;
  212. switch (val) {
  213. case PDC_WDT_TICKLE_STATUS_TICKLE:
  214. case PDC_WDT_TICKLE_STATUS_TIMEOUT:
  215. pdc_wdt->wdt_dev.bootstatus |= WDIOF_CARDRESET;
  216. dev_info(&pdev->dev,
  217. "watchdog module last reset due to timeout\n");
  218. break;
  219. case PDC_WDT_TICKLE_STATUS_HRESET:
  220. dev_info(&pdev->dev,
  221. "watchdog module last reset due to hard reset\n");
  222. break;
  223. case PDC_WDT_TICKLE_STATUS_SRESET:
  224. dev_info(&pdev->dev,
  225. "watchdog module last reset due to soft reset\n");
  226. break;
  227. case PDC_WDT_TICKLE_STATUS_USER:
  228. dev_info(&pdev->dev,
  229. "watchdog module last reset due to user reset\n");
  230. break;
  231. default:
  232. dev_info(&pdev->dev,
  233. "contains an illegal status code (%08x)\n", val);
  234. break;
  235. }
  236. watchdog_set_nowayout(&pdc_wdt->wdt_dev, nowayout);
  237. watchdog_set_restart_priority(&pdc_wdt->wdt_dev, 128);
  238. platform_set_drvdata(pdev, pdc_wdt);
  239. ret = watchdog_register_device(&pdc_wdt->wdt_dev);
  240. if (ret)
  241. goto disable_wdt_clk;
  242. return 0;
  243. disable_wdt_clk:
  244. clk_disable_unprepare(pdc_wdt->wdt_clk);
  245. disable_sys_clk:
  246. clk_disable_unprepare(pdc_wdt->sys_clk);
  247. return ret;
  248. }
  249. static void pdc_wdt_shutdown(struct platform_device *pdev)
  250. {
  251. struct pdc_wdt_dev *pdc_wdt = platform_get_drvdata(pdev);
  252. pdc_wdt_stop(&pdc_wdt->wdt_dev);
  253. }
  254. static int pdc_wdt_remove(struct platform_device *pdev)
  255. {
  256. struct pdc_wdt_dev *pdc_wdt = platform_get_drvdata(pdev);
  257. pdc_wdt_stop(&pdc_wdt->wdt_dev);
  258. watchdog_unregister_device(&pdc_wdt->wdt_dev);
  259. clk_disable_unprepare(pdc_wdt->wdt_clk);
  260. clk_disable_unprepare(pdc_wdt->sys_clk);
  261. return 0;
  262. }
  263. static const struct of_device_id pdc_wdt_match[] = {
  264. { .compatible = "img,pdc-wdt" },
  265. {}
  266. };
  267. MODULE_DEVICE_TABLE(of, pdc_wdt_match);
  268. static struct platform_driver pdc_wdt_driver = {
  269. .driver = {
  270. .name = "imgpdc-wdt",
  271. .of_match_table = pdc_wdt_match,
  272. },
  273. .probe = pdc_wdt_probe,
  274. .remove = pdc_wdt_remove,
  275. .shutdown = pdc_wdt_shutdown,
  276. };
  277. module_platform_driver(pdc_wdt_driver);
  278. MODULE_AUTHOR("Jude Abraham <Jude.Abraham@imgtec.com>");
  279. MODULE_AUTHOR("Naidu Tellapati <Naidu.Tellapati@imgtec.com>");
  280. MODULE_DESCRIPTION("Imagination Technologies PDC Watchdog Timer Driver");
  281. MODULE_LICENSE("GPL v2");