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