marvell_gti_wdt.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Marvell GTI Watchdog driver
  3. *
  4. * Copyright (C) 2023 Marvell.
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/io.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/of.h>
  12. #include <linux/watchdog.h>
  13. /*
  14. * Hardware supports following mode of operation:
  15. * 1) Interrupt Only:
  16. * This will generate the interrupt to arm core whenever timeout happens.
  17. *
  18. * 2) Interrupt + del3t (Interrupt to firmware (SCP processor)).
  19. * This will generate interrupt to arm core on 1st timeout happens
  20. * This will generate interrupt to SCP processor on 2nd timeout happens
  21. *
  22. * 3) Interrupt + Interrupt to SCP processor (called delt3t) + reboot.
  23. * This will generate interrupt to arm core on 1st timeout happens
  24. * Will generate interrupt to SCP processor on 2nd timeout happens,
  25. * if interrupt is configured.
  26. * Reboot on 3rd timeout.
  27. *
  28. * Driver will use hardware in mode-3 above so that system can reboot in case
  29. * a hardware hang. Also h/w is configured not to generate SCP interrupt, so
  30. * effectively 2nd timeout is ignored within hardware.
  31. *
  32. * First timeout is effectively watchdog pretimeout.
  33. */
  34. /* GTI CWD Watchdog (GTI_CWD_WDOG) Register */
  35. #define GTI_CWD_WDOG(reg_offset) (0x8 * (reg_offset))
  36. #define GTI_CWD_WDOG_MODE_INT_DEL3T_RST 0x3
  37. #define GTI_CWD_WDOG_MODE_MASK GENMASK_ULL(1, 0)
  38. #define GTI_CWD_WDOG_LEN_SHIFT 4
  39. #define GTI_CWD_WDOG_LEN_MASK GENMASK_ULL(19, 4)
  40. #define GTI_CWD_WDOG_CNT_SHIFT 20
  41. #define GTI_CWD_WDOG_CNT_MASK GENMASK_ULL(43, 20)
  42. /* GTI CWD Watchdog Interrupt (GTI_CWD_INT) Register */
  43. #define GTI_CWD_INT 0x200
  44. #define GTI_CWD_INT_PENDING_STATUS(bit) BIT_ULL(bit)
  45. /* GTI CWD Watchdog Interrupt Enable Clear (GTI_CWD_INT_ENA_CLR) Register */
  46. #define GTI_CWD_INT_ENA_CLR 0x210
  47. #define GTI_CWD_INT_ENA_CLR_VAL(bit) BIT_ULL(bit)
  48. /* GTI CWD Watchdog Interrupt Enable Set (GTI_CWD_INT_ENA_SET) Register */
  49. #define GTI_CWD_INT_ENA_SET 0x218
  50. #define GTI_CWD_INT_ENA_SET_VAL(bit) BIT_ULL(bit)
  51. /* GTI CWD Watchdog Poke (GTI_CWD_POKE) Registers */
  52. #define GTI_CWD_POKE(reg_offset) (0x10000 + 0x8 * (reg_offset))
  53. #define GTI_CWD_POKE_VAL 1
  54. struct gti_match_data {
  55. u32 gti_num_timers;
  56. };
  57. static const struct gti_match_data match_data_octeontx2 = {
  58. .gti_num_timers = 54,
  59. };
  60. static const struct gti_match_data match_data_cn10k = {
  61. .gti_num_timers = 64,
  62. };
  63. struct gti_wdt_priv {
  64. struct watchdog_device wdev;
  65. void __iomem *base;
  66. u32 clock_freq;
  67. struct clk *sclk;
  68. /* wdt_timer_idx used for timer to be used for system watchdog */
  69. u32 wdt_timer_idx;
  70. const struct gti_match_data *data;
  71. };
  72. static irqreturn_t gti_wdt_interrupt(int irq, void *data)
  73. {
  74. struct watchdog_device *wdev = data;
  75. struct gti_wdt_priv *priv = watchdog_get_drvdata(wdev);
  76. /* Clear Interrupt Pending Status */
  77. writeq(GTI_CWD_INT_PENDING_STATUS(priv->wdt_timer_idx),
  78. priv->base + GTI_CWD_INT);
  79. watchdog_notify_pretimeout(wdev);
  80. return IRQ_HANDLED;
  81. }
  82. static int gti_wdt_ping(struct watchdog_device *wdev)
  83. {
  84. struct gti_wdt_priv *priv = watchdog_get_drvdata(wdev);
  85. writeq(GTI_CWD_POKE_VAL,
  86. priv->base + GTI_CWD_POKE(priv->wdt_timer_idx));
  87. return 0;
  88. }
  89. static int gti_wdt_start(struct watchdog_device *wdev)
  90. {
  91. struct gti_wdt_priv *priv = watchdog_get_drvdata(wdev);
  92. u64 regval;
  93. if (!wdev->pretimeout)
  94. return -EINVAL;
  95. set_bit(WDOG_HW_RUNNING, &wdev->status);
  96. /* Clear any pending interrupt */
  97. writeq(GTI_CWD_INT_PENDING_STATUS(priv->wdt_timer_idx),
  98. priv->base + GTI_CWD_INT);
  99. /* Enable Interrupt */
  100. writeq(GTI_CWD_INT_ENA_SET_VAL(priv->wdt_timer_idx),
  101. priv->base + GTI_CWD_INT_ENA_SET);
  102. /* Set (Interrupt + SCP interrupt (DEL3T) + core domain reset) Mode */
  103. regval = readq(priv->base + GTI_CWD_WDOG(priv->wdt_timer_idx));
  104. regval |= GTI_CWD_WDOG_MODE_INT_DEL3T_RST;
  105. writeq(regval, priv->base + GTI_CWD_WDOG(priv->wdt_timer_idx));
  106. return 0;
  107. }
  108. static int gti_wdt_stop(struct watchdog_device *wdev)
  109. {
  110. struct gti_wdt_priv *priv = watchdog_get_drvdata(wdev);
  111. u64 regval;
  112. /* Disable Interrupt */
  113. writeq(GTI_CWD_INT_ENA_CLR_VAL(priv->wdt_timer_idx),
  114. priv->base + GTI_CWD_INT_ENA_CLR);
  115. /* Set GTI_CWD_WDOG.Mode = 0 to stop the timer */
  116. regval = readq(priv->base + GTI_CWD_WDOG(priv->wdt_timer_idx));
  117. regval &= ~GTI_CWD_WDOG_MODE_MASK;
  118. writeq(regval, priv->base + GTI_CWD_WDOG(priv->wdt_timer_idx));
  119. return 0;
  120. }
  121. static int gti_wdt_settimeout(struct watchdog_device *wdev,
  122. unsigned int timeout)
  123. {
  124. struct gti_wdt_priv *priv = watchdog_get_drvdata(wdev);
  125. u64 timeout_wdog, regval;
  126. /* Update new timeout */
  127. wdev->timeout = timeout;
  128. /* Pretimeout is 1/3 of timeout */
  129. wdev->pretimeout = timeout / 3;
  130. /* Get clock cycles from pretimeout */
  131. timeout_wdog = (u64)priv->clock_freq * wdev->pretimeout;
  132. /* Watchdog counts in 1024 cycle steps */
  133. timeout_wdog = timeout_wdog >> 10;
  134. /* GTI_CWD_WDOG.CNT: reload counter is 16-bit */
  135. timeout_wdog = (timeout_wdog + 0xff) >> 8;
  136. if (timeout_wdog >= 0x10000)
  137. timeout_wdog = 0xffff;
  138. /*
  139. * GTI_CWD_WDOG.LEN is 24bit, lower 8-bits should be zero and
  140. * upper 16-bits are same as GTI_CWD_WDOG.CNT
  141. */
  142. regval = readq(priv->base + GTI_CWD_WDOG(priv->wdt_timer_idx));
  143. regval &= GTI_CWD_WDOG_MODE_MASK;
  144. regval |= (timeout_wdog << (GTI_CWD_WDOG_CNT_SHIFT + 8)) |
  145. (timeout_wdog << GTI_CWD_WDOG_LEN_SHIFT);
  146. writeq(regval, priv->base + GTI_CWD_WDOG(priv->wdt_timer_idx));
  147. return 0;
  148. }
  149. static int gti_wdt_set_pretimeout(struct watchdog_device *wdev,
  150. unsigned int timeout)
  151. {
  152. struct gti_wdt_priv *priv = watchdog_get_drvdata(wdev);
  153. struct watchdog_device *wdog_dev = &priv->wdev;
  154. if (!timeout) {
  155. /* Disable Interrupt */
  156. writeq(GTI_CWD_INT_ENA_CLR_VAL(priv->wdt_timer_idx),
  157. priv->base + GTI_CWD_INT_ENA_CLR);
  158. return 0;
  159. }
  160. /* pretimeout should 1/3 of max_timeout */
  161. if (timeout * 3 <= wdog_dev->max_timeout)
  162. return gti_wdt_settimeout(wdev, timeout * 3);
  163. return -EINVAL;
  164. }
  165. static void gti_clk_disable_unprepare(void *data)
  166. {
  167. clk_disable_unprepare(data);
  168. }
  169. static int gti_wdt_get_cntfrq(struct platform_device *pdev,
  170. struct gti_wdt_priv *priv)
  171. {
  172. int err;
  173. priv->sclk = devm_clk_get_enabled(&pdev->dev, NULL);
  174. if (IS_ERR(priv->sclk))
  175. return PTR_ERR(priv->sclk);
  176. err = devm_add_action_or_reset(&pdev->dev,
  177. gti_clk_disable_unprepare, priv->sclk);
  178. if (err)
  179. return err;
  180. priv->clock_freq = clk_get_rate(priv->sclk);
  181. if (!priv->clock_freq)
  182. return -EINVAL;
  183. return 0;
  184. }
  185. static const struct watchdog_info gti_wdt_ident = {
  186. .identity = "Marvell GTI watchdog",
  187. .options = WDIOF_SETTIMEOUT | WDIOF_PRETIMEOUT | WDIOF_KEEPALIVEPING |
  188. WDIOF_MAGICCLOSE | WDIOF_CARDRESET,
  189. };
  190. static const struct watchdog_ops gti_wdt_ops = {
  191. .owner = THIS_MODULE,
  192. .start = gti_wdt_start,
  193. .stop = gti_wdt_stop,
  194. .ping = gti_wdt_ping,
  195. .set_timeout = gti_wdt_settimeout,
  196. .set_pretimeout = gti_wdt_set_pretimeout,
  197. };
  198. static int gti_wdt_probe(struct platform_device *pdev)
  199. {
  200. struct gti_wdt_priv *priv;
  201. struct device *dev = &pdev->dev;
  202. struct watchdog_device *wdog_dev;
  203. u64 max_pretimeout;
  204. u32 wdt_idx;
  205. int irq;
  206. int err;
  207. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  208. if (!priv)
  209. return -ENOMEM;
  210. priv->base = devm_platform_ioremap_resource(pdev, 0);
  211. if (IS_ERR(priv->base))
  212. return dev_err_probe(&pdev->dev, PTR_ERR(priv->base),
  213. "reg property not valid/found\n");
  214. err = gti_wdt_get_cntfrq(pdev, priv);
  215. if (err)
  216. return dev_err_probe(&pdev->dev, err,
  217. "GTI clock frequency not valid/found");
  218. priv->data = of_device_get_match_data(dev);
  219. /* default use last timer for watchdog */
  220. priv->wdt_timer_idx = priv->data->gti_num_timers - 1;
  221. err = of_property_read_u32(dev->of_node, "marvell,wdt-timer-index",
  222. &wdt_idx);
  223. if (!err) {
  224. if (wdt_idx >= priv->data->gti_num_timers)
  225. return dev_err_probe(&pdev->dev, -EINVAL,
  226. "GTI wdog timer index not valid");
  227. priv->wdt_timer_idx = wdt_idx;
  228. }
  229. wdog_dev = &priv->wdev;
  230. wdog_dev->info = &gti_wdt_ident;
  231. wdog_dev->ops = &gti_wdt_ops;
  232. wdog_dev->parent = dev;
  233. /*
  234. * Watchdog counter is 24 bit where lower 8 bits are zeros
  235. * This counter decrements every 1024 clock cycles.
  236. */
  237. max_pretimeout = (GTI_CWD_WDOG_CNT_MASK >> GTI_CWD_WDOG_CNT_SHIFT);
  238. max_pretimeout &= ~0xFFUL;
  239. max_pretimeout = (max_pretimeout * 1024) / priv->clock_freq;
  240. wdog_dev->pretimeout = max_pretimeout;
  241. /* Maximum timeout is 3 times the pretimeout */
  242. wdog_dev->max_timeout = max_pretimeout * 3;
  243. wdog_dev->max_hw_heartbeat_ms = max_pretimeout * 1000;
  244. /* Minimum first timeout (pretimeout) is 1, so min_timeout as 3 */
  245. wdog_dev->min_timeout = 3;
  246. wdog_dev->timeout = wdog_dev->pretimeout;
  247. watchdog_set_drvdata(wdog_dev, priv);
  248. platform_set_drvdata(pdev, priv);
  249. gti_wdt_settimeout(wdog_dev, wdog_dev->timeout);
  250. watchdog_stop_on_reboot(wdog_dev);
  251. watchdog_stop_on_unregister(wdog_dev);
  252. err = devm_watchdog_register_device(dev, wdog_dev);
  253. if (err)
  254. return err;
  255. irq = platform_get_irq(pdev, 0);
  256. if (irq < 0)
  257. return irq;
  258. err = devm_request_irq(dev, irq, gti_wdt_interrupt, 0,
  259. pdev->name, &priv->wdev);
  260. if (err)
  261. return dev_err_probe(dev, err, "Failed to register interrupt handler\n");
  262. dev_info(dev, "Watchdog enabled (timeout=%d sec)\n", wdog_dev->timeout);
  263. return 0;
  264. }
  265. static const struct of_device_id gti_wdt_of_match[] = {
  266. { .compatible = "marvell,cn9670-wdt", .data = &match_data_octeontx2},
  267. { .compatible = "marvell,cn10624-wdt", .data = &match_data_cn10k},
  268. { },
  269. };
  270. MODULE_DEVICE_TABLE(of, gti_wdt_of_match);
  271. static struct platform_driver gti_wdt_driver = {
  272. .driver = {
  273. .name = "gti-wdt",
  274. .of_match_table = gti_wdt_of_match,
  275. },
  276. .probe = gti_wdt_probe,
  277. };
  278. module_platform_driver(gti_wdt_driver);
  279. MODULE_AUTHOR("Bharat Bhushan <bbhushan2@marvell.com>");
  280. MODULE_DESCRIPTION("Marvell GTI watchdog driver");
  281. MODULE_LICENSE("GPL");