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