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