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