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