dw_wdt.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2010-2011 Picochip Ltd., Jamie Iles
  4. * https://www.picochip.com
  5. *
  6. * This file implements a driver for the Synopsys DesignWare watchdog device
  7. * in the many subsystems. The watchdog has 16 different timeout periods
  8. * and these are a function of the input clock frequency.
  9. *
  10. * The DesignWare watchdog cannot be stopped once it has been started so we
  11. * do not implement a stop function. The watchdog core will continue to send
  12. * heartbeat requests after the watchdog device has been closed.
  13. */
  14. #include <linux/bitops.h>
  15. #include <linux/clk.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/delay.h>
  18. #include <linux/err.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/kernel.h>
  22. #include <linux/limits.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/of.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/pm.h>
  28. #include <linux/reset.h>
  29. #include <linux/watchdog.h>
  30. #define WDOG_CONTROL_REG_OFFSET 0x00
  31. #define WDOG_CONTROL_REG_WDT_EN_MASK 0x01
  32. #define WDOG_CONTROL_REG_RESP_MODE_MASK 0x02
  33. #define WDOG_TIMEOUT_RANGE_REG_OFFSET 0x04
  34. #define WDOG_TIMEOUT_RANGE_TOPINIT_SHIFT 4
  35. #define WDOG_CURRENT_COUNT_REG_OFFSET 0x08
  36. #define WDOG_COUNTER_RESTART_REG_OFFSET 0x0c
  37. #define WDOG_COUNTER_RESTART_KICK_VALUE 0x76
  38. #define WDOG_INTERRUPT_STATUS_REG_OFFSET 0x10
  39. #define WDOG_INTERRUPT_CLEAR_REG_OFFSET 0x14
  40. #define WDOG_COMP_PARAMS_5_REG_OFFSET 0xe4
  41. #define WDOG_COMP_PARAMS_4_REG_OFFSET 0xe8
  42. #define WDOG_COMP_PARAMS_3_REG_OFFSET 0xec
  43. #define WDOG_COMP_PARAMS_2_REG_OFFSET 0xf0
  44. #define WDOG_COMP_PARAMS_1_REG_OFFSET 0xf4
  45. #define WDOG_COMP_PARAMS_1_USE_FIX_TOP BIT(6)
  46. #define WDOG_COMP_VERSION_REG_OFFSET 0xf8
  47. #define WDOG_COMP_TYPE_REG_OFFSET 0xfc
  48. /* There are sixteen TOPs (timeout periods) that can be set in the watchdog. */
  49. #define DW_WDT_NUM_TOPS 16
  50. #define DW_WDT_FIX_TOP(_idx) (1U << (16 + _idx))
  51. #define DW_WDT_DEFAULT_SECONDS 30
  52. static const u32 dw_wdt_fix_tops[DW_WDT_NUM_TOPS] = {
  53. DW_WDT_FIX_TOP(0), DW_WDT_FIX_TOP(1), DW_WDT_FIX_TOP(2),
  54. DW_WDT_FIX_TOP(3), DW_WDT_FIX_TOP(4), DW_WDT_FIX_TOP(5),
  55. DW_WDT_FIX_TOP(6), DW_WDT_FIX_TOP(7), DW_WDT_FIX_TOP(8),
  56. DW_WDT_FIX_TOP(9), DW_WDT_FIX_TOP(10), DW_WDT_FIX_TOP(11),
  57. DW_WDT_FIX_TOP(12), DW_WDT_FIX_TOP(13), DW_WDT_FIX_TOP(14),
  58. DW_WDT_FIX_TOP(15)
  59. };
  60. static bool nowayout = WATCHDOG_NOWAYOUT;
  61. module_param(nowayout, bool, 0);
  62. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  63. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  64. enum dw_wdt_rmod {
  65. DW_WDT_RMOD_RESET = 1,
  66. DW_WDT_RMOD_IRQ = 2
  67. };
  68. struct dw_wdt_timeout {
  69. u32 top_val;
  70. unsigned int sec;
  71. unsigned int msec;
  72. };
  73. struct dw_wdt {
  74. void __iomem *regs;
  75. struct clk *clk;
  76. struct clk *pclk;
  77. unsigned long rate;
  78. enum dw_wdt_rmod rmod;
  79. struct dw_wdt_timeout timeouts[DW_WDT_NUM_TOPS];
  80. struct watchdog_device wdd;
  81. struct reset_control *rst;
  82. /* Save/restore */
  83. u32 control;
  84. u32 timeout;
  85. #ifdef CONFIG_DEBUG_FS
  86. struct dentry *dbgfs_dir;
  87. #endif
  88. };
  89. #define to_dw_wdt(wdd) container_of(wdd, struct dw_wdt, wdd)
  90. static inline int dw_wdt_is_enabled(struct dw_wdt *dw_wdt)
  91. {
  92. return readl(dw_wdt->regs + WDOG_CONTROL_REG_OFFSET) &
  93. WDOG_CONTROL_REG_WDT_EN_MASK;
  94. }
  95. static void dw_wdt_update_mode(struct dw_wdt *dw_wdt, enum dw_wdt_rmod rmod)
  96. {
  97. u32 val;
  98. val = readl(dw_wdt->regs + WDOG_CONTROL_REG_OFFSET);
  99. if (rmod == DW_WDT_RMOD_IRQ)
  100. val |= WDOG_CONTROL_REG_RESP_MODE_MASK;
  101. else
  102. val &= ~WDOG_CONTROL_REG_RESP_MODE_MASK;
  103. writel(val, dw_wdt->regs + WDOG_CONTROL_REG_OFFSET);
  104. dw_wdt->rmod = rmod;
  105. }
  106. static unsigned int dw_wdt_find_best_top(struct dw_wdt *dw_wdt,
  107. unsigned int timeout, u32 *top_val)
  108. {
  109. int idx;
  110. /*
  111. * Find a TOP with timeout greater or equal to the requested number.
  112. * Note we'll select a TOP with maximum timeout if the requested
  113. * timeout couldn't be reached.
  114. */
  115. for (idx = 0; idx < DW_WDT_NUM_TOPS; ++idx) {
  116. if (dw_wdt->timeouts[idx].sec >= timeout)
  117. break;
  118. }
  119. if (idx == DW_WDT_NUM_TOPS)
  120. --idx;
  121. *top_val = dw_wdt->timeouts[idx].top_val;
  122. return dw_wdt->timeouts[idx].sec;
  123. }
  124. static unsigned int dw_wdt_get_min_timeout(struct dw_wdt *dw_wdt)
  125. {
  126. int idx;
  127. /*
  128. * We'll find a timeout greater or equal to one second anyway because
  129. * the driver probe would have failed if there was none.
  130. */
  131. for (idx = 0; idx < DW_WDT_NUM_TOPS; ++idx) {
  132. if (dw_wdt->timeouts[idx].sec)
  133. break;
  134. }
  135. return dw_wdt->timeouts[idx].sec;
  136. }
  137. static unsigned int dw_wdt_get_max_timeout_ms(struct dw_wdt *dw_wdt)
  138. {
  139. struct dw_wdt_timeout *timeout = &dw_wdt->timeouts[DW_WDT_NUM_TOPS - 1];
  140. u64 msec;
  141. msec = (u64)timeout->sec * MSEC_PER_SEC + timeout->msec;
  142. return msec < UINT_MAX ? msec : UINT_MAX;
  143. }
  144. static unsigned int dw_wdt_get_timeout(struct dw_wdt *dw_wdt)
  145. {
  146. int top_val = readl(dw_wdt->regs + WDOG_TIMEOUT_RANGE_REG_OFFSET) & 0xF;
  147. int idx;
  148. for (idx = 0; idx < DW_WDT_NUM_TOPS; ++idx) {
  149. if (dw_wdt->timeouts[idx].top_val == top_val)
  150. break;
  151. }
  152. /*
  153. * In IRQ mode due to the two stages counter, the actual timeout is
  154. * twice greater than the TOP setting.
  155. */
  156. return dw_wdt->timeouts[idx].sec * dw_wdt->rmod;
  157. }
  158. static int dw_wdt_ping(struct watchdog_device *wdd)
  159. {
  160. struct dw_wdt *dw_wdt = to_dw_wdt(wdd);
  161. writel(WDOG_COUNTER_RESTART_KICK_VALUE, dw_wdt->regs +
  162. WDOG_COUNTER_RESTART_REG_OFFSET);
  163. return 0;
  164. }
  165. static int dw_wdt_set_timeout(struct watchdog_device *wdd, unsigned int top_s)
  166. {
  167. struct dw_wdt *dw_wdt = to_dw_wdt(wdd);
  168. unsigned int timeout;
  169. u32 top_val;
  170. /*
  171. * Note IRQ mode being enabled means having a non-zero pre-timeout
  172. * setup. In this case we try to find a TOP as close to the half of the
  173. * requested timeout as possible since DW Watchdog IRQ mode is designed
  174. * in two stages way - first timeout rises the pre-timeout interrupt,
  175. * second timeout performs the system reset. So basically the effective
  176. * watchdog-caused reset happens after two watchdog TOPs elapsed.
  177. */
  178. timeout = dw_wdt_find_best_top(dw_wdt, DIV_ROUND_UP(top_s, dw_wdt->rmod),
  179. &top_val);
  180. if (dw_wdt->rmod == DW_WDT_RMOD_IRQ)
  181. wdd->pretimeout = timeout;
  182. else
  183. wdd->pretimeout = 0;
  184. /*
  185. * Set the new value in the watchdog. Some versions of dw_wdt
  186. * have TOPINIT in the TIMEOUT_RANGE register (as per
  187. * CP_WDT_DUAL_TOP in WDT_COMP_PARAMS_1). On those we
  188. * effectively get a pat of the watchdog right here.
  189. */
  190. writel(top_val | top_val << WDOG_TIMEOUT_RANGE_TOPINIT_SHIFT,
  191. dw_wdt->regs + WDOG_TIMEOUT_RANGE_REG_OFFSET);
  192. /* Kick new TOP value into the watchdog counter if activated. */
  193. if (watchdog_active(wdd))
  194. dw_wdt_ping(wdd);
  195. /*
  196. * In case users set bigger timeout value than HW can support,
  197. * kernel(watchdog_dev.c) helps to feed watchdog before
  198. * wdd->max_hw_heartbeat_ms
  199. */
  200. if (top_s * 1000 <= wdd->max_hw_heartbeat_ms)
  201. wdd->timeout = timeout * dw_wdt->rmod;
  202. else
  203. wdd->timeout = top_s;
  204. return 0;
  205. }
  206. static int dw_wdt_set_pretimeout(struct watchdog_device *wdd, unsigned int req)
  207. {
  208. struct dw_wdt *dw_wdt = to_dw_wdt(wdd);
  209. /*
  210. * We ignore actual value of the timeout passed from user-space
  211. * using it as a flag whether the pretimeout functionality is intended
  212. * to be activated.
  213. */
  214. dw_wdt_update_mode(dw_wdt, req ? DW_WDT_RMOD_IRQ : DW_WDT_RMOD_RESET);
  215. dw_wdt_set_timeout(wdd, wdd->timeout);
  216. return 0;
  217. }
  218. static void dw_wdt_arm_system_reset(struct dw_wdt *dw_wdt)
  219. {
  220. u32 val = readl(dw_wdt->regs + WDOG_CONTROL_REG_OFFSET);
  221. /* Disable/enable interrupt mode depending on the RMOD flag. */
  222. if (dw_wdt->rmod == DW_WDT_RMOD_IRQ)
  223. val |= WDOG_CONTROL_REG_RESP_MODE_MASK;
  224. else
  225. val &= ~WDOG_CONTROL_REG_RESP_MODE_MASK;
  226. /* Enable watchdog. */
  227. val |= WDOG_CONTROL_REG_WDT_EN_MASK;
  228. writel(val, dw_wdt->regs + WDOG_CONTROL_REG_OFFSET);
  229. }
  230. static int dw_wdt_start(struct watchdog_device *wdd)
  231. {
  232. struct dw_wdt *dw_wdt = to_dw_wdt(wdd);
  233. dw_wdt_set_timeout(wdd, wdd->timeout);
  234. dw_wdt_ping(&dw_wdt->wdd);
  235. dw_wdt_arm_system_reset(dw_wdt);
  236. return 0;
  237. }
  238. static int dw_wdt_stop(struct watchdog_device *wdd)
  239. {
  240. struct dw_wdt *dw_wdt = to_dw_wdt(wdd);
  241. if (!dw_wdt->rst) {
  242. set_bit(WDOG_HW_RUNNING, &wdd->status);
  243. return 0;
  244. }
  245. reset_control_assert(dw_wdt->rst);
  246. reset_control_deassert(dw_wdt->rst);
  247. return 0;
  248. }
  249. static int dw_wdt_restart(struct watchdog_device *wdd,
  250. unsigned long action, void *data)
  251. {
  252. struct dw_wdt *dw_wdt = to_dw_wdt(wdd);
  253. writel(0, dw_wdt->regs + WDOG_TIMEOUT_RANGE_REG_OFFSET);
  254. dw_wdt_update_mode(dw_wdt, DW_WDT_RMOD_RESET);
  255. if (dw_wdt_is_enabled(dw_wdt))
  256. writel(WDOG_COUNTER_RESTART_KICK_VALUE,
  257. dw_wdt->regs + WDOG_COUNTER_RESTART_REG_OFFSET);
  258. else
  259. dw_wdt_arm_system_reset(dw_wdt);
  260. /* wait for reset to assert... */
  261. mdelay(500);
  262. return 0;
  263. }
  264. static unsigned int dw_wdt_get_timeleft(struct watchdog_device *wdd)
  265. {
  266. struct dw_wdt *dw_wdt = to_dw_wdt(wdd);
  267. unsigned int sec;
  268. u32 val;
  269. val = readl(dw_wdt->regs + WDOG_CURRENT_COUNT_REG_OFFSET);
  270. sec = val / dw_wdt->rate;
  271. if (dw_wdt->rmod == DW_WDT_RMOD_IRQ) {
  272. val = readl(dw_wdt->regs + WDOG_INTERRUPT_STATUS_REG_OFFSET);
  273. if (!val)
  274. sec += wdd->pretimeout;
  275. }
  276. return sec;
  277. }
  278. static const struct watchdog_info dw_wdt_ident = {
  279. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
  280. WDIOF_MAGICCLOSE,
  281. .identity = "Synopsys DesignWare Watchdog",
  282. };
  283. static const struct watchdog_info dw_wdt_pt_ident = {
  284. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
  285. WDIOF_PRETIMEOUT | WDIOF_MAGICCLOSE,
  286. .identity = "Synopsys DesignWare Watchdog",
  287. };
  288. static const struct watchdog_ops dw_wdt_ops = {
  289. .owner = THIS_MODULE,
  290. .start = dw_wdt_start,
  291. .stop = dw_wdt_stop,
  292. .ping = dw_wdt_ping,
  293. .set_timeout = dw_wdt_set_timeout,
  294. .set_pretimeout = dw_wdt_set_pretimeout,
  295. .get_timeleft = dw_wdt_get_timeleft,
  296. .restart = dw_wdt_restart,
  297. };
  298. static irqreturn_t dw_wdt_irq(int irq, void *devid)
  299. {
  300. struct dw_wdt *dw_wdt = devid;
  301. u32 val;
  302. /*
  303. * We don't clear the IRQ status. It's supposed to be done by the
  304. * following ping operations.
  305. */
  306. val = readl(dw_wdt->regs + WDOG_INTERRUPT_STATUS_REG_OFFSET);
  307. if (!val)
  308. return IRQ_NONE;
  309. watchdog_notify_pretimeout(&dw_wdt->wdd);
  310. return IRQ_HANDLED;
  311. }
  312. static int dw_wdt_suspend(struct device *dev)
  313. {
  314. struct dw_wdt *dw_wdt = dev_get_drvdata(dev);
  315. dw_wdt->control = readl(dw_wdt->regs + WDOG_CONTROL_REG_OFFSET);
  316. dw_wdt->timeout = readl(dw_wdt->regs + WDOG_TIMEOUT_RANGE_REG_OFFSET);
  317. clk_disable_unprepare(dw_wdt->pclk);
  318. clk_disable_unprepare(dw_wdt->clk);
  319. return 0;
  320. }
  321. static int dw_wdt_resume(struct device *dev)
  322. {
  323. struct dw_wdt *dw_wdt = dev_get_drvdata(dev);
  324. int err = clk_prepare_enable(dw_wdt->clk);
  325. if (err)
  326. return err;
  327. err = clk_prepare_enable(dw_wdt->pclk);
  328. if (err) {
  329. clk_disable_unprepare(dw_wdt->clk);
  330. return err;
  331. }
  332. writel(dw_wdt->timeout, dw_wdt->regs + WDOG_TIMEOUT_RANGE_REG_OFFSET);
  333. writel(dw_wdt->control, dw_wdt->regs + WDOG_CONTROL_REG_OFFSET);
  334. dw_wdt_ping(&dw_wdt->wdd);
  335. return 0;
  336. }
  337. static DEFINE_SIMPLE_DEV_PM_OPS(dw_wdt_pm_ops, dw_wdt_suspend, dw_wdt_resume);
  338. /*
  339. * In case if DW WDT IP core is synthesized with fixed TOP feature disabled the
  340. * TOPs array can be arbitrary ordered with nearly any sixteen uint numbers
  341. * depending on the system engineer imagination. The next method handles the
  342. * passed TOPs array to pre-calculate the effective timeouts and to sort the
  343. * TOP items out in the ascending order with respect to the timeouts.
  344. */
  345. static void dw_wdt_handle_tops(struct dw_wdt *dw_wdt, const u32 *tops)
  346. {
  347. struct dw_wdt_timeout tout, *dst;
  348. int val, tidx;
  349. u64 msec;
  350. /*
  351. * We walk over the passed TOPs array and calculate corresponding
  352. * timeouts in seconds and milliseconds. The milliseconds granularity
  353. * is needed to distinguish the TOPs with very close timeouts and to
  354. * set the watchdog max heartbeat setting further.
  355. */
  356. for (val = 0; val < DW_WDT_NUM_TOPS; ++val) {
  357. tout.top_val = val;
  358. tout.sec = tops[val] / dw_wdt->rate;
  359. msec = (u64)tops[val] * MSEC_PER_SEC;
  360. do_div(msec, dw_wdt->rate);
  361. tout.msec = msec - ((u64)tout.sec * MSEC_PER_SEC);
  362. /*
  363. * Find a suitable place for the current TOP in the timeouts
  364. * array so that the list is remained in the ascending order.
  365. */
  366. for (tidx = 0; tidx < val; ++tidx) {
  367. dst = &dw_wdt->timeouts[tidx];
  368. if (tout.sec > dst->sec || (tout.sec == dst->sec &&
  369. tout.msec >= dst->msec))
  370. continue;
  371. else
  372. swap(*dst, tout);
  373. }
  374. dw_wdt->timeouts[val] = tout;
  375. }
  376. }
  377. static int dw_wdt_init_timeouts(struct dw_wdt *dw_wdt, struct device *dev)
  378. {
  379. u32 data, of_tops[DW_WDT_NUM_TOPS];
  380. const u32 *tops;
  381. int ret;
  382. /*
  383. * Retrieve custom or fixed counter values depending on the
  384. * WDT_USE_FIX_TOP flag found in the component specific parameters
  385. * #1 register.
  386. */
  387. data = readl(dw_wdt->regs + WDOG_COMP_PARAMS_1_REG_OFFSET);
  388. if (data & WDOG_COMP_PARAMS_1_USE_FIX_TOP) {
  389. tops = dw_wdt_fix_tops;
  390. } else {
  391. ret = of_property_read_variable_u32_array(dev_of_node(dev),
  392. "snps,watchdog-tops", of_tops, DW_WDT_NUM_TOPS,
  393. DW_WDT_NUM_TOPS);
  394. if (ret < 0) {
  395. dev_warn(dev, "No valid TOPs array specified\n");
  396. tops = dw_wdt_fix_tops;
  397. } else {
  398. tops = of_tops;
  399. }
  400. }
  401. /* Convert the specified TOPs into an array of watchdog timeouts. */
  402. dw_wdt_handle_tops(dw_wdt, tops);
  403. if (!dw_wdt->timeouts[DW_WDT_NUM_TOPS - 1].sec) {
  404. dev_err(dev, "No any valid TOP detected\n");
  405. return -EINVAL;
  406. }
  407. return 0;
  408. }
  409. #ifdef CONFIG_DEBUG_FS
  410. #define DW_WDT_DBGFS_REG(_name, _off) \
  411. { \
  412. .name = _name, \
  413. .offset = _off \
  414. }
  415. static const struct debugfs_reg32 dw_wdt_dbgfs_regs[] = {
  416. DW_WDT_DBGFS_REG("cr", WDOG_CONTROL_REG_OFFSET),
  417. DW_WDT_DBGFS_REG("torr", WDOG_TIMEOUT_RANGE_REG_OFFSET),
  418. DW_WDT_DBGFS_REG("ccvr", WDOG_CURRENT_COUNT_REG_OFFSET),
  419. DW_WDT_DBGFS_REG("crr", WDOG_COUNTER_RESTART_REG_OFFSET),
  420. DW_WDT_DBGFS_REG("stat", WDOG_INTERRUPT_STATUS_REG_OFFSET),
  421. DW_WDT_DBGFS_REG("param5", WDOG_COMP_PARAMS_5_REG_OFFSET),
  422. DW_WDT_DBGFS_REG("param4", WDOG_COMP_PARAMS_4_REG_OFFSET),
  423. DW_WDT_DBGFS_REG("param3", WDOG_COMP_PARAMS_3_REG_OFFSET),
  424. DW_WDT_DBGFS_REG("param2", WDOG_COMP_PARAMS_2_REG_OFFSET),
  425. DW_WDT_DBGFS_REG("param1", WDOG_COMP_PARAMS_1_REG_OFFSET),
  426. DW_WDT_DBGFS_REG("version", WDOG_COMP_VERSION_REG_OFFSET),
  427. DW_WDT_DBGFS_REG("type", WDOG_COMP_TYPE_REG_OFFSET)
  428. };
  429. static void dw_wdt_dbgfs_init(struct dw_wdt *dw_wdt)
  430. {
  431. struct device *dev = dw_wdt->wdd.parent;
  432. struct debugfs_regset32 *regset;
  433. regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
  434. if (!regset)
  435. return;
  436. regset->regs = dw_wdt_dbgfs_regs;
  437. regset->nregs = ARRAY_SIZE(dw_wdt_dbgfs_regs);
  438. regset->base = dw_wdt->regs;
  439. dw_wdt->dbgfs_dir = debugfs_create_dir(dev_name(dev), NULL);
  440. debugfs_create_regset32("registers", 0444, dw_wdt->dbgfs_dir, regset);
  441. }
  442. static void dw_wdt_dbgfs_clear(struct dw_wdt *dw_wdt)
  443. {
  444. debugfs_remove_recursive(dw_wdt->dbgfs_dir);
  445. }
  446. #else /* !CONFIG_DEBUG_FS */
  447. static void dw_wdt_dbgfs_init(struct dw_wdt *dw_wdt) {}
  448. static void dw_wdt_dbgfs_clear(struct dw_wdt *dw_wdt) {}
  449. #endif /* !CONFIG_DEBUG_FS */
  450. static int dw_wdt_drv_probe(struct platform_device *pdev)
  451. {
  452. struct device *dev = &pdev->dev;
  453. struct watchdog_device *wdd;
  454. struct dw_wdt *dw_wdt;
  455. int ret;
  456. dw_wdt = devm_kzalloc(dev, sizeof(*dw_wdt), GFP_KERNEL);
  457. if (!dw_wdt)
  458. return -ENOMEM;
  459. dw_wdt->regs = devm_platform_ioremap_resource(pdev, 0);
  460. if (IS_ERR(dw_wdt->regs))
  461. return PTR_ERR(dw_wdt->regs);
  462. /*
  463. * Try to request the watchdog dedicated timer clock source. It must
  464. * be supplied if asynchronous mode is enabled. Otherwise fallback
  465. * to the common timer/bus clocks configuration, in which the very
  466. * first found clock supply both timer and APB signals.
  467. */
  468. dw_wdt->clk = devm_clk_get(dev, "tclk");
  469. if (IS_ERR(dw_wdt->clk)) {
  470. dw_wdt->clk = devm_clk_get(dev, NULL);
  471. if (IS_ERR(dw_wdt->clk))
  472. return PTR_ERR(dw_wdt->clk);
  473. }
  474. ret = clk_prepare_enable(dw_wdt->clk);
  475. if (ret)
  476. return ret;
  477. dw_wdt->rate = clk_get_rate(dw_wdt->clk);
  478. if (dw_wdt->rate == 0) {
  479. ret = -EINVAL;
  480. goto out_disable_clk;
  481. }
  482. /*
  483. * Request APB clock if device is configured with async clocks mode.
  484. * In this case both tclk and pclk clocks are supposed to be specified.
  485. * Alas we can't know for sure whether async mode was really activated,
  486. * so the pclk phandle reference is left optional. If it couldn't be
  487. * found we consider the device configured in synchronous clocks mode.
  488. */
  489. dw_wdt->pclk = devm_clk_get_optional(dev, "pclk");
  490. if (IS_ERR(dw_wdt->pclk)) {
  491. ret = PTR_ERR(dw_wdt->pclk);
  492. goto out_disable_clk;
  493. }
  494. ret = clk_prepare_enable(dw_wdt->pclk);
  495. if (ret)
  496. goto out_disable_clk;
  497. dw_wdt->rst = devm_reset_control_get_optional_shared(&pdev->dev, NULL);
  498. if (IS_ERR(dw_wdt->rst)) {
  499. ret = PTR_ERR(dw_wdt->rst);
  500. goto out_disable_pclk;
  501. }
  502. /* Enable normal reset without pre-timeout by default. */
  503. dw_wdt_update_mode(dw_wdt, DW_WDT_RMOD_RESET);
  504. /*
  505. * Pre-timeout IRQ is optional, since some hardware may lack support
  506. * of it. Note we must request rising-edge IRQ, since the lane is left
  507. * pending either until the next watchdog kick event or up to the
  508. * system reset.
  509. */
  510. ret = platform_get_irq_optional(pdev, 0);
  511. if (ret > 0) {
  512. ret = devm_request_irq(dev, ret, dw_wdt_irq,
  513. IRQF_SHARED | IRQF_TRIGGER_RISING,
  514. pdev->name, dw_wdt);
  515. if (ret)
  516. goto out_disable_pclk;
  517. dw_wdt->wdd.info = &dw_wdt_pt_ident;
  518. } else {
  519. if (ret == -EPROBE_DEFER)
  520. goto out_disable_pclk;
  521. dw_wdt->wdd.info = &dw_wdt_ident;
  522. }
  523. reset_control_deassert(dw_wdt->rst);
  524. ret = dw_wdt_init_timeouts(dw_wdt, dev);
  525. if (ret)
  526. goto out_assert_rst;
  527. wdd = &dw_wdt->wdd;
  528. wdd->ops = &dw_wdt_ops;
  529. wdd->min_timeout = dw_wdt_get_min_timeout(dw_wdt);
  530. wdd->max_hw_heartbeat_ms = dw_wdt_get_max_timeout_ms(dw_wdt);
  531. wdd->parent = dev;
  532. watchdog_set_drvdata(wdd, dw_wdt);
  533. watchdog_set_nowayout(wdd, nowayout);
  534. watchdog_init_timeout(wdd, 0, dev);
  535. /*
  536. * If the watchdog is already running, use its already configured
  537. * timeout. Otherwise use the default or the value provided through
  538. * devicetree.
  539. */
  540. if (dw_wdt_is_enabled(dw_wdt)) {
  541. wdd->timeout = dw_wdt_get_timeout(dw_wdt);
  542. set_bit(WDOG_HW_RUNNING, &wdd->status);
  543. } else {
  544. wdd->timeout = DW_WDT_DEFAULT_SECONDS;
  545. watchdog_init_timeout(wdd, 0, dev);
  546. }
  547. platform_set_drvdata(pdev, dw_wdt);
  548. watchdog_set_restart_priority(wdd, 128);
  549. ret = watchdog_register_device(wdd);
  550. if (ret)
  551. goto out_assert_rst;
  552. dw_wdt_dbgfs_init(dw_wdt);
  553. return 0;
  554. out_assert_rst:
  555. reset_control_assert(dw_wdt->rst);
  556. out_disable_pclk:
  557. clk_disable_unprepare(dw_wdt->pclk);
  558. out_disable_clk:
  559. clk_disable_unprepare(dw_wdt->clk);
  560. return ret;
  561. }
  562. static int dw_wdt_drv_remove(struct platform_device *pdev)
  563. {
  564. struct dw_wdt *dw_wdt = platform_get_drvdata(pdev);
  565. dw_wdt_dbgfs_clear(dw_wdt);
  566. watchdog_unregister_device(&dw_wdt->wdd);
  567. reset_control_assert(dw_wdt->rst);
  568. clk_disable_unprepare(dw_wdt->pclk);
  569. clk_disable_unprepare(dw_wdt->clk);
  570. return 0;
  571. }
  572. #ifdef CONFIG_OF
  573. static const struct of_device_id dw_wdt_of_match[] = {
  574. { .compatible = "snps,dw-wdt", },
  575. { /* sentinel */ }
  576. };
  577. MODULE_DEVICE_TABLE(of, dw_wdt_of_match);
  578. #endif
  579. static struct platform_driver dw_wdt_driver = {
  580. .probe = dw_wdt_drv_probe,
  581. .remove = dw_wdt_drv_remove,
  582. .driver = {
  583. .name = "dw_wdt",
  584. .of_match_table = of_match_ptr(dw_wdt_of_match),
  585. .pm = pm_sleep_ptr(&dw_wdt_pm_ops),
  586. },
  587. };
  588. module_platform_driver(dw_wdt_driver);
  589. MODULE_AUTHOR("Jamie Iles");
  590. MODULE_DESCRIPTION("Synopsys DesignWare Watchdog Driver");
  591. MODULE_LICENSE("GPL");