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 | |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 18 | static void imx_watchdog_expire_now(struct watchdog_regs *wdog) |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 19 | { |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 20 | clrsetbits_le16(&wdog->wcr, WCR_WT_MSK, WCR_WDE); |
| 21 | |
| 22 | writew(0x5555, &wdog->wsr); |
| 23 | writew(0xaaaa, &wdog->wsr); /* load minimum 1/2 second timeout */ |
| 24 | while (1) { |
| 25 | /* |
| 26 | * spin for .5 seconds before reset |
| 27 | */ |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | #if !defined(CONFIG_IMX_WATCHDOG) || \ |
| 32 | (defined(CONFIG_IMX_WATCHDOG) && !CONFIG_IS_ENABLED(WDT)) |
| 33 | void __attribute__((weak)) reset_cpu(ulong addr) |
| 34 | { |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 35 | struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR; |
| 36 | |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 37 | imx_watchdog_expire_now(wdog); |
| 38 | } |
| 39 | #endif |
| 40 | |
| 41 | #if defined(CONFIG_IMX_WATCHDOG) |
| 42 | static void imx_watchdog_reset(struct watchdog_regs *wdog) |
| 43 | { |
| 44 | #ifndef CONFIG_WATCHDOG_RESET_DISABLE |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 45 | writew(0x5555, &wdog->wsr); |
| 46 | writew(0xaaaa, &wdog->wsr); |
Xiaoliang Yang | 09e9213 | 2018-10-18 18:27:46 +0800 | [diff] [blame] | 47 | #endif /* CONFIG_WATCHDOG_RESET_DISABLE*/ |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 50 | static void imx_watchdog_init(struct watchdog_regs *wdog) |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 51 | { |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 52 | u16 timeout; |
| 53 | |
| 54 | /* |
| 55 | * The timer watchdog can be set between |
| 56 | * 0.5 and 128 Seconds. If not defined |
| 57 | * in configuration file, sets 128 Seconds |
| 58 | */ |
| 59 | #ifndef CONFIG_WATCHDOG_TIMEOUT_MSECS |
| 60 | #define CONFIG_WATCHDOG_TIMEOUT_MSECS 128000 |
| 61 | #endif |
| 62 | timeout = (CONFIG_WATCHDOG_TIMEOUT_MSECS / 500) - 1; |
Xiaoliang Yang | a6657ad | 2018-10-18 18:27:45 +0800 | [diff] [blame] | 63 | #ifdef CONFIG_FSL_LSCH2 |
| 64 | writew((WCR_WDA | WCR_SRS | WCR_WDE) << 8 | timeout, &wdog->wcr); |
| 65 | #else |
Anatolij Gustschin | ca31000 | 2013-09-30 12:52:38 +0200 | [diff] [blame] | 66 | writew(WCR_WDZST | WCR_WDBG | WCR_WDE | WCR_WDT | WCR_SRS | |
Ross Parker | feaada1 | 2016-08-02 08:08:07 +0000 | [diff] [blame] | 67 | WCR_WDA | SET_WCR_WT(timeout), &wdog->wcr); |
Xiaoliang Yang | a6657ad | 2018-10-18 18:27:45 +0800 | [diff] [blame] | 68 | #endif /* CONFIG_FSL_LSCH2*/ |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 69 | imx_watchdog_reset(wdog); |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 70 | } |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 71 | |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 72 | #if !CONFIG_IS_ENABLED(WDT) |
| 73 | void hw_watchdog_reset(void) |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 74 | { |
| 75 | struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR; |
| 76 | |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 77 | imx_watchdog_reset(wdog); |
| 78 | } |
Peng Fan | 838cf7b | 2015-09-14 13:34:44 +0800 | [diff] [blame] | 79 | |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 80 | void hw_watchdog_init(void) |
| 81 | { |
| 82 | struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR; |
| 83 | |
| 84 | imx_watchdog_init(wdog); |
| 85 | } |
| 86 | #else |
| 87 | struct imx_wdt_priv { |
| 88 | void __iomem *base; |
| 89 | }; |
| 90 | |
| 91 | static int imx_wdt_reset(struct udevice *dev) |
| 92 | { |
| 93 | struct imx_wdt_priv *priv = dev_get_priv(dev); |
| 94 | |
| 95 | imx_watchdog_reset(priv->base); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static int imx_wdt_expire_now(struct udevice *dev, ulong flags) |
| 101 | { |
| 102 | struct imx_wdt_priv *priv = dev_get_priv(dev); |
| 103 | |
| 104 | imx_watchdog_expire_now(priv->base); |
| 105 | hang(); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int imx_wdt_start(struct udevice *dev, u64 timeout, ulong flags) |
| 111 | { |
| 112 | struct imx_wdt_priv *priv = dev_get_priv(dev); |
| 113 | |
| 114 | imx_watchdog_init(priv->base); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static int imx_wdt_probe(struct udevice *dev) |
| 120 | { |
| 121 | struct imx_wdt_priv *priv = dev_get_priv(dev); |
| 122 | |
| 123 | priv->base = dev_read_addr_ptr(dev); |
| 124 | if (!priv->base) |
| 125 | return -ENOENT; |
| 126 | |
| 127 | return 0; |
Troy Kisky | 4b7c602 | 2012-10-22 15:19:01 +0000 | [diff] [blame] | 128 | } |
Marek Vasut | f7fc5c1 | 2019-06-09 03:46:22 +0200 | [diff] [blame] | 129 | |
| 130 | static const struct wdt_ops imx_wdt_ops = { |
| 131 | .start = imx_wdt_start, |
| 132 | .reset = imx_wdt_reset, |
| 133 | .expire_now = imx_wdt_expire_now, |
| 134 | }; |
| 135 | |
| 136 | static const struct udevice_id imx_wdt_ids[] = { |
| 137 | { .compatible = "fsl,imx21-wdt" }, |
| 138 | {} |
| 139 | }; |
| 140 | |
| 141 | U_BOOT_DRIVER(imx_wdt) = { |
| 142 | .name = "imx_wdt", |
| 143 | .id = UCLASS_WDT, |
| 144 | .of_match = imx_wdt_ids, |
| 145 | .probe = imx_wdt_probe, |
| 146 | .ops = &imx_wdt_ops, |
| 147 | .priv_auto_alloc_size = sizeof(struct imx_wdt_priv), |
| 148 | .flags = DM_FLAG_PRE_RELOC, |
| 149 | }; |
| 150 | #endif |
| 151 | #endif |