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/mod_devicetable.h>
  47. #include <linux/platform_device.h>
  48. #include <linux/slab.h>
  49. #include <linux/watchdog.h>
  50. /* registers */
  51. #define PDC_WDT_SOFT_RESET 0x00
  52. #define PDC_WDT_CONFIG 0x04
  53. #define PDC_WDT_CONFIG_ENABLE BIT(31)
  54. #define PDC_WDT_CONFIG_DELAY_MASK 0x1f
  55. #define PDC_WDT_TICKLE1 0x08
  56. #define PDC_WDT_TICKLE1_MAGIC 0xabcd1234
  57. #define PDC_WDT_TICKLE2 0x0c
  58. #define PDC_WDT_TICKLE2_MAGIC 0x4321dcba
  59. #define PDC_WDT_TICKLE_STATUS_MASK 0x7
  60. #define PDC_WDT_TICKLE_STATUS_SHIFT 0
  61. #define PDC_WDT_TICKLE_STATUS_HRESET 0x0 /* Hard reset */
  62. #define PDC_WDT_TICKLE_STATUS_TIMEOUT 0x1 /* Timeout */
  63. #define PDC_WDT_TICKLE_STATUS_TICKLE 0x2 /* Tickled incorrectly */
  64. #define PDC_WDT_TICKLE_STATUS_SRESET 0x3 /* Soft reset */
  65. #define PDC_WDT_TICKLE_STATUS_USER 0x4 /* User reset */
  66. /* Timeout values are in seconds */
  67. #define PDC_WDT_MIN_TIMEOUT 1
  68. #define PDC_WDT_DEF_TIMEOUT 64
  69. static int heartbeat;
  70. module_param(heartbeat, int, 0);
  71. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds "
  72. "(default=" __MODULE_STRING(PDC_WDT_DEF_TIMEOUT) ")");
  73. static bool nowayout = WATCHDOG_NOWAYOUT;
  74. module_param(nowayout, bool, 0);
  75. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  76. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  77. struct pdc_wdt_dev {
  78. struct watchdog_device wdt_dev;
  79. struct clk *wdt_clk;
  80. struct clk *sys_clk;
  81. void __iomem *base;
  82. };
  83. static int pdc_wdt_keepalive(struct watchdog_device *wdt_dev)
  84. {
  85. struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
  86. writel(PDC_WDT_TICKLE1_MAGIC, wdt->base + PDC_WDT_TICKLE1);
  87. writel(PDC_WDT_TICKLE2_MAGIC, wdt->base + PDC_WDT_TICKLE2);
  88. return 0;
  89. }
  90. static int pdc_wdt_stop(struct watchdog_device *wdt_dev)
  91. {
  92. unsigned int val;
  93. struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
  94. val = readl(wdt->base + PDC_WDT_CONFIG);
  95. val &= ~PDC_WDT_CONFIG_ENABLE;
  96. writel(val, wdt->base + PDC_WDT_CONFIG);
  97. /* Must tickle to finish the stop */
  98. pdc_wdt_keepalive(wdt_dev);
  99. return 0;
  100. }
  101. static void __pdc_wdt_set_timeout(struct pdc_wdt_dev *wdt)
  102. {
  103. unsigned long clk_rate = clk_get_rate(wdt->wdt_clk);
  104. unsigned int val;
  105. val = readl(wdt->base + PDC_WDT_CONFIG) & ~PDC_WDT_CONFIG_DELAY_MASK;
  106. val |= order_base_2(wdt->wdt_dev.timeout * clk_rate) - 1;
  107. writel(val, wdt->base + PDC_WDT_CONFIG);
  108. }
  109. static int pdc_wdt_set_timeout(struct watchdog_device *wdt_dev,
  110. unsigned int new_timeout)
  111. {
  112. struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
  113. wdt->wdt_dev.timeout = new_timeout;
  114. __pdc_wdt_set_timeout(wdt);
  115. return 0;
  116. }
  117. /* Start the watchdog timer (delay should already be set) */
  118. static int pdc_wdt_start(struct watchdog_device *wdt_dev)
  119. {
  120. unsigned int val;
  121. struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
  122. __pdc_wdt_set_timeout(wdt);
  123. val = readl(wdt->base + PDC_WDT_CONFIG);
  124. val |= PDC_WDT_CONFIG_ENABLE;
  125. writel(val, wdt->base + PDC_WDT_CONFIG);
  126. return 0;
  127. }
  128. static int pdc_wdt_restart(struct watchdog_device *wdt_dev,
  129. unsigned long action, void *data)
  130. {
  131. struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
  132. /* Assert SOFT_RESET */
  133. writel(0x1, wdt->base + PDC_WDT_SOFT_RESET);
  134. return 0;
  135. }
  136. static const struct watchdog_info pdc_wdt_info = {
  137. .identity = "IMG PDC Watchdog",
  138. .options = WDIOF_SETTIMEOUT |
  139. WDIOF_KEEPALIVEPING |
  140. WDIOF_MAGICCLOSE,
  141. };
  142. static const struct watchdog_ops pdc_wdt_ops = {
  143. .owner = THIS_MODULE,
  144. .start = pdc_wdt_start,
  145. .stop = pdc_wdt_stop,
  146. .ping = pdc_wdt_keepalive,
  147. .set_timeout = pdc_wdt_set_timeout,
  148. .restart = pdc_wdt_restart,
  149. };
  150. static int pdc_wdt_probe(struct platform_device *pdev)
  151. {
  152. u64 div;
  153. int ret, val;
  154. unsigned long clk_rate;
  155. struct resource *res;
  156. struct pdc_wdt_dev *pdc_wdt;
  157. pdc_wdt = devm_kzalloc(&pdev->dev, sizeof(*pdc_wdt), GFP_KERNEL);
  158. if (!pdc_wdt)
  159. return -ENOMEM;
  160. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  161. pdc_wdt->base = devm_ioremap_resource(&pdev->dev, res);
  162. if (IS_ERR(pdc_wdt->base))
  163. return PTR_ERR(pdc_wdt->base);
  164. pdc_wdt->sys_clk = devm_clk_get(&pdev->dev, "sys");
  165. if (IS_ERR(pdc_wdt->sys_clk)) {
  166. dev_err(&pdev->dev, "failed to get the sys clock\n");
  167. return PTR_ERR(pdc_wdt->sys_clk);
  168. }
  169. pdc_wdt->wdt_clk = devm_clk_get(&pdev->dev, "wdt");
  170. if (IS_ERR(pdc_wdt->wdt_clk)) {
  171. dev_err(&pdev->dev, "failed to get the wdt clock\n");
  172. return PTR_ERR(pdc_wdt->wdt_clk);
  173. }
  174. ret = clk_prepare_enable(pdc_wdt->sys_clk);
  175. if (ret) {
  176. dev_err(&pdev->dev, "could not prepare or enable sys clock\n");
  177. return ret;
  178. }
  179. ret = clk_prepare_enable(pdc_wdt->wdt_clk);
  180. if (ret) {
  181. dev_err(&pdev->dev, "could not prepare or enable wdt clock\n");
  182. goto disable_sys_clk;
  183. }
  184. /* We use the clock rate to calculate the max timeout */
  185. clk_rate = clk_get_rate(pdc_wdt->wdt_clk);
  186. if (clk_rate == 0) {
  187. dev_err(&pdev->dev, "failed to get clock rate\n");
  188. ret = -EINVAL;
  189. goto disable_wdt_clk;
  190. }
  191. if (order_base_2(clk_rate) > PDC_WDT_CONFIG_DELAY_MASK + 1) {
  192. dev_err(&pdev->dev, "invalid clock rate\n");
  193. ret = -EINVAL;
  194. goto disable_wdt_clk;
  195. }
  196. if (order_base_2(clk_rate) == 0)
  197. pdc_wdt->wdt_dev.min_timeout = PDC_WDT_MIN_TIMEOUT + 1;
  198. else
  199. pdc_wdt->wdt_dev.min_timeout = PDC_WDT_MIN_TIMEOUT;
  200. pdc_wdt->wdt_dev.info = &pdc_wdt_info;
  201. pdc_wdt->wdt_dev.ops = &pdc_wdt_ops;
  202. div = 1ULL << (PDC_WDT_CONFIG_DELAY_MASK + 1);
  203. do_div(div, clk_rate);
  204. pdc_wdt->wdt_dev.max_timeout = div;
  205. pdc_wdt->wdt_dev.timeout = PDC_WDT_DEF_TIMEOUT;
  206. pdc_wdt->wdt_dev.parent = &pdev->dev;
  207. watchdog_set_drvdata(&pdc_wdt->wdt_dev, pdc_wdt);
  208. watchdog_init_timeout(&pdc_wdt->wdt_dev, heartbeat, &pdev->dev);
  209. pdc_wdt_stop(&pdc_wdt->wdt_dev);
  210. /* Find what caused the last reset */
  211. val = readl(pdc_wdt->base + PDC_WDT_TICKLE1);
  212. val = (val & PDC_WDT_TICKLE_STATUS_MASK) >> PDC_WDT_TICKLE_STATUS_SHIFT;
  213. switch (val) {
  214. case PDC_WDT_TICKLE_STATUS_TICKLE:
  215. case PDC_WDT_TICKLE_STATUS_TIMEOUT:
  216. pdc_wdt->wdt_dev.bootstatus |= WDIOF_CARDRESET;
  217. dev_info(&pdev->dev,
  218. "watchdog module last reset due to timeout\n");
  219. break;
  220. case PDC_WDT_TICKLE_STATUS_HRESET:
  221. dev_info(&pdev->dev,
  222. "watchdog module last reset due to hard reset\n");
  223. break;
  224. case PDC_WDT_TICKLE_STATUS_SRESET:
  225. dev_info(&pdev->dev,
  226. "watchdog module last reset due to soft reset\n");
  227. break;
  228. case PDC_WDT_TICKLE_STATUS_USER:
  229. dev_info(&pdev->dev,
  230. "watchdog module last reset due to user reset\n");
  231. break;
  232. default:
  233. dev_info(&pdev->dev,
  234. "contains an illegal status code (%08x)\n", val);
  235. break;
  236. }
  237. watchdog_set_nowayout(&pdc_wdt->wdt_dev, nowayout);
  238. watchdog_set_restart_priority(&pdc_wdt->wdt_dev, 128);
  239. platform_set_drvdata(pdev, pdc_wdt);
  240. ret = watchdog_register_device(&pdc_wdt->wdt_dev);
  241. if (ret)
  242. goto disable_wdt_clk;
  243. return 0;
  244. disable_wdt_clk:
  245. clk_disable_unprepare(pdc_wdt->wdt_clk);
  246. disable_sys_clk:
  247. clk_disable_unprepare(pdc_wdt->sys_clk);
  248. return ret;
  249. }
  250. static void pdc_wdt_shutdown(struct platform_device *pdev)
  251. {
  252. struct pdc_wdt_dev *pdc_wdt = platform_get_drvdata(pdev);
  253. pdc_wdt_stop(&pdc_wdt->wdt_dev);
  254. }
  255. static int pdc_wdt_remove(struct platform_device *pdev)
  256. {
  257. struct pdc_wdt_dev *pdc_wdt = platform_get_drvdata(pdev);
  258. pdc_wdt_stop(&pdc_wdt->wdt_dev);
  259. watchdog_unregister_device(&pdc_wdt->wdt_dev);
  260. clk_disable_unprepare(pdc_wdt->wdt_clk);
  261. clk_disable_unprepare(pdc_wdt->sys_clk);
  262. return 0;
  263. }
  264. static const struct of_device_id pdc_wdt_match[] = {
  265. { .compatible = "img,pdc-wdt" },
  266. {}
  267. };
  268. MODULE_DEVICE_TABLE(of, pdc_wdt_match);
  269. static struct platform_driver pdc_wdt_driver = {
  270. .driver = {
  271. .name = "imgpdc-wdt",
  272. .of_match_table = pdc_wdt_match,
  273. },
  274. .probe = pdc_wdt_probe,
  275. .remove = pdc_wdt_remove,
  276. .shutdown = pdc_wdt_shutdown,
  277. };
  278. module_platform_driver(pdc_wdt_driver);
  279. MODULE_AUTHOR("Jude Abraham <Jude.Abraham@imgtec.com>");
  280. MODULE_AUTHOR("Naidu Tellapati <Naidu.Tellapati@imgtec.com>");
  281. MODULE_DESCRIPTION("Imagination Technologies PDC Watchdog Timer Driver");
  282. MODULE_LICENSE("GPL v2");