Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * watchdog.c - driver for i.mx on-chip watchdog |
| 3 | * |
| 4 | * Licensed under the GPL-2 or later. |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | afb0215 | 2019-12-28 10:45:01 -0700 | [diff] [blame] | 8 | #include <cpu_func.h> |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | f11478f | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 10 | #include <hang.h> |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 11 | #include <asm/io.h> |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 12 | #include <wdt.h> |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 13 | #include <watchdog.h> |
| 14 | #include <asm/arch/imx-regs.h> |
Xiaoliang Yang | a6657ad | 2018-10-18 18:27:45 +0800 | [diff] [blame] | 15 | #ifdef CONFIG_FSL_LSCH2 |
| 16 | #include <asm/arch/immap_lsch2.h> |
| 17 | #endif |
Fabio Estevam | cd847ab | 2015-10-03 14:20:59 -0300 | [diff] [blame] | 18 | #include <fsl_wdog.h> |
Mo, Yuezhang | c65adaa | 2020-07-01 09:15:28 +0000 | [diff] [blame] | 19 | #include <div64.h> |
| 20 | |
| 21 | #define TIMEOUT_MAX 128000 |
| 22 | #define TIMEOUT_MIN 500 |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 23 | |
Robert Hancock | edc1d15 | 2019-08-06 11:05:30 -0600 | [diff] [blame] | 24 | static void imx_watchdog_expire_now(struct watchdog_regs *wdog, bool ext_reset) |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 25 | { |
Robert Hancock | edc1d15 | 2019-08-06 11:05:30 -0600 | [diff] [blame] | 26 | u16 wcr = WCR_WDE; |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 27 | |
Robert Hancock | edc1d15 | 2019-08-06 11:05:30 -0600 | [diff] [blame] | 28 | if (ext_reset) |
| 29 | wcr |= WCR_SRS; /* do not assert internal reset */ |
| 30 | else |
| 31 | wcr |= WCR_WDA; /* do not assert external reset */ |
| 32 | |
| 33 | /* Write 3 times to ensure it works, due to IMX6Q errata ERR004346 */ |
| 34 | writew(wcr, &wdog->wcr); |
| 35 | writew(wcr, &wdog->wcr); |
| 36 | writew(wcr, &wdog->wcr); |
| 37 | |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 38 | while (1) { |
| 39 | /* |
Robert Hancock | edc1d15 | 2019-08-06 11:05:30 -0600 | [diff] [blame] | 40 | * spin before reset |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 41 | */ |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | #if !defined(CONFIG_IMX_WATCHDOG) || \ |
| 46 | (defined(CONFIG_IMX_WATCHDOG) && !CONFIG_IS_ENABLED(WDT)) |
Harald Seiler | 6f14d5f | 2020-12-15 16:47:52 +0100 | [diff] [blame] | 47 | void __attribute__((weak)) reset_cpu(void) |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 48 | { |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 49 | struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR; |
| 50 | |
Robert Hancock | edc1d15 | 2019-08-06 11:05:30 -0600 | [diff] [blame] | 51 | imx_watchdog_expire_now(wdog, true); |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 52 | } |
| 53 | #endif |
| 54 | |
| 55 | #if defined(CONFIG_IMX_WATCHDOG) |
| 56 | static void imx_watchdog_reset(struct watchdog_regs *wdog) |
| 57 | { |
| 58 | #ifndef CONFIG_WATCHDOG_RESET_DISABLE |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 59 | writew(0x5555, &wdog->wsr); |
| 60 | writew(0xaaaa, &wdog->wsr); |
Xiaoliang Yang | 09e9213 | 2018-10-18 18:27:46 +0800 | [diff] [blame] | 61 | #endif /* CONFIG_WATCHDOG_RESET_DISABLE*/ |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Mo, Yuezhang | c65adaa | 2020-07-01 09:15:28 +0000 | [diff] [blame] | 64 | static void imx_watchdog_init(struct watchdog_regs *wdog, bool ext_reset, |
| 65 | u64 timeout) |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 66 | { |
Robert Hancock | d00a0b1 | 2019-08-06 11:05:29 -0600 | [diff] [blame] | 67 | u16 wcr; |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * The timer watchdog can be set between |
| 71 | * 0.5 and 128 Seconds. If not defined |
| 72 | * in configuration file, sets 128 Seconds |
| 73 | */ |
| 74 | #ifndef CONFIG_WATCHDOG_TIMEOUT_MSECS |
| 75 | #define CONFIG_WATCHDOG_TIMEOUT_MSECS 128000 |
| 76 | #endif |
Mo, Yuezhang | c65adaa | 2020-07-01 09:15:28 +0000 | [diff] [blame] | 77 | |
| 78 | timeout = max_t(u64, timeout, TIMEOUT_MIN); |
| 79 | timeout = min_t(u64, timeout, TIMEOUT_MAX); |
| 80 | timeout = lldiv(timeout, 500) - 1; |
| 81 | |
Xiaoliang Yang | a6657ad | 2018-10-18 18:27:45 +0800 | [diff] [blame] | 82 | #ifdef CONFIG_FSL_LSCH2 |
Robert Hancock | d00a0b1 | 2019-08-06 11:05:29 -0600 | [diff] [blame] | 83 | wcr = (WCR_WDA | WCR_SRS | WCR_WDE) << 8 | timeout; |
Xiaoliang Yang | a6657ad | 2018-10-18 18:27:45 +0800 | [diff] [blame] | 84 | #else |
Robert Hancock | d00a0b1 | 2019-08-06 11:05:29 -0600 | [diff] [blame] | 85 | wcr = WCR_WDZST | WCR_WDBG | WCR_WDE | WCR_SRS | |
| 86 | WCR_WDA | SET_WCR_WT(timeout); |
| 87 | if (ext_reset) |
| 88 | wcr |= WCR_WDT; |
Xiaoliang Yang | a6657ad | 2018-10-18 18:27:45 +0800 | [diff] [blame] | 89 | #endif /* CONFIG_FSL_LSCH2*/ |
Robert Hancock | d00a0b1 | 2019-08-06 11:05:29 -0600 | [diff] [blame] | 90 | writew(wcr, &wdog->wcr); |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 91 | imx_watchdog_reset(wdog); |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 92 | } |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 93 | |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 94 | #if !CONFIG_IS_ENABLED(WDT) |
| 95 | void hw_watchdog_reset(void) |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 96 | { |
| 97 | struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR; |
| 98 | |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 99 | imx_watchdog_reset(wdog); |
| 100 | } |
Peng Fan | 838cf7b | 2015-09-14 13:34:44 +0800 | [diff] [blame] | 101 | |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 102 | void hw_watchdog_init(void) |
| 103 | { |
| 104 | struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR; |
| 105 | |
Mo, Yuezhang | c65adaa | 2020-07-01 09:15:28 +0000 | [diff] [blame] | 106 | imx_watchdog_init(wdog, true, CONFIG_WATCHDOG_TIMEOUT_MSECS); |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 107 | } |
| 108 | #else |
| 109 | struct imx_wdt_priv { |
| 110 | void __iomem *base; |
Robert Hancock | d00a0b1 | 2019-08-06 11:05:29 -0600 | [diff] [blame] | 111 | bool ext_reset; |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | static int imx_wdt_reset(struct udevice *dev) |
| 115 | { |
| 116 | struct imx_wdt_priv *priv = dev_get_priv(dev); |
| 117 | |
| 118 | imx_watchdog_reset(priv->base); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int imx_wdt_expire_now(struct udevice *dev, ulong flags) |
| 124 | { |
| 125 | struct imx_wdt_priv *priv = dev_get_priv(dev); |
| 126 | |
Robert Hancock | edc1d15 | 2019-08-06 11:05:30 -0600 | [diff] [blame] | 127 | imx_watchdog_expire_now(priv->base, priv->ext_reset); |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 128 | hang(); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static int imx_wdt_start(struct udevice *dev, u64 timeout, ulong flags) |
| 134 | { |
| 135 | struct imx_wdt_priv *priv = dev_get_priv(dev); |
| 136 | |
Mo, Yuezhang | c65adaa | 2020-07-01 09:15:28 +0000 | [diff] [blame] | 137 | imx_watchdog_init(priv->base, priv->ext_reset, timeout); |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int imx_wdt_probe(struct udevice *dev) |
| 143 | { |
| 144 | struct imx_wdt_priv *priv = dev_get_priv(dev); |
| 145 | |
| 146 | priv->base = dev_read_addr_ptr(dev); |
| 147 | if (!priv->base) |
| 148 | return -ENOENT; |
| 149 | |
Robert Hancock | d00a0b1 | 2019-08-06 11:05:29 -0600 | [diff] [blame] | 150 | priv->ext_reset = dev_read_bool(dev, "fsl,ext-reset-output"); |
| 151 | |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 152 | return 0; |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 153 | } |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 154 | |
| 155 | static const struct wdt_ops imx_wdt_ops = { |
| 156 | .start = imx_wdt_start, |
| 157 | .reset = imx_wdt_reset, |
| 158 | .expire_now = imx_wdt_expire_now, |
| 159 | }; |
| 160 | |
| 161 | static const struct udevice_id imx_wdt_ids[] = { |
| 162 | { .compatible = "fsl,imx21-wdt" }, |
| 163 | {} |
| 164 | }; |
| 165 | |
| 166 | U_BOOT_DRIVER(imx_wdt) = { |
| 167 | .name = "imx_wdt", |
| 168 | .id = UCLASS_WDT, |
| 169 | .of_match = imx_wdt_ids, |
| 170 | .probe = imx_wdt_probe, |
| 171 | .ops = &imx_wdt_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 172 | .priv_auto = sizeof(struct imx_wdt_priv), |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 173 | .flags = DM_FLAG_PRE_RELOC, |
| 174 | }; |
| 175 | #endif |
| 176 | #endif |