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>
8 #include <dm.h>
9 #include <asm/io.h>
10 #include <wdt.h>
11 #include <watchdog.h>
12 #include <asm/arch/imx-regs.h>
13 #ifdef CONFIG_FSL_LSCH2
14 #include <asm/arch/immap_lsch2.h>
15 #endif
16 #include <fsl_wdog.h>
17
imx_watchdog_expire_now(struct watchdog_regs * wdog,bool ext_reset)18 static void imx_watchdog_expire_now(struct watchdog_regs *wdog, bool ext_reset)
19 {
20 u16 wcr = WCR_WDE;
21
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
32 while (1) {
33 /*
34 * spin before reset
35 */
36 }
37 }
38
39 #if !defined(CONFIG_IMX_WATCHDOG) || \
40 (defined(CONFIG_IMX_WATCHDOG) && !CONFIG_IS_ENABLED(WDT))
reset_cpu(ulong addr)41 void __attribute__((weak)) reset_cpu(ulong addr)
42 {
43 struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
44
45 imx_watchdog_expire_now(wdog, true);
46 }
47 #endif
48
49 #if defined(CONFIG_IMX_WATCHDOG)
imx_watchdog_reset(struct watchdog_regs * wdog)50 static void imx_watchdog_reset(struct watchdog_regs *wdog)
51 {
52 #ifndef CONFIG_WATCHDOG_RESET_DISABLE
53 writew(0x5555, &wdog->wsr);
54 writew(0xaaaa, &wdog->wsr);
55 #endif /* CONFIG_WATCHDOG_RESET_DISABLE*/
56 }
57
imx_watchdog_init(struct watchdog_regs * wdog,bool ext_reset)58 static void imx_watchdog_init(struct watchdog_regs *wdog, bool ext_reset)
59 {
60 u16 timeout;
61 u16 wcr;
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;
72 #ifdef CONFIG_FSL_LSCH2
73 wcr = (WCR_WDA | WCR_SRS | WCR_WDE) << 8 | timeout;
74 #else
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;
79 #endif /* CONFIG_FSL_LSCH2*/
80 writew(wcr, &wdog->wcr);
81 imx_watchdog_reset(wdog);
82 }
83
84 #if !CONFIG_IS_ENABLED(WDT)
hw_watchdog_reset(void)85 void hw_watchdog_reset(void)
86 {
87 struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
88
89 imx_watchdog_reset(wdog);
90 }
91
hw_watchdog_init(void)92 void hw_watchdog_init(void)
93 {
94 struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
95
96 imx_watchdog_init(wdog, true);
97 }
98 #else
99 struct imx_wdt_priv {
100 void __iomem *base;
101 bool ext_reset;
102 };
103
imx_wdt_reset(struct udevice * dev)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
imx_wdt_expire_now(struct udevice * dev,ulong flags)113 static int imx_wdt_expire_now(struct udevice *dev, ulong flags)
114 {
115 struct imx_wdt_priv *priv = dev_get_priv(dev);
116
117 imx_watchdog_expire_now(priv->base, priv->ext_reset);
118 hang();
119
120 return 0;
121 }
122
imx_wdt_start(struct udevice * dev,u64 timeout,ulong flags)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
127 imx_watchdog_init(priv->base, priv->ext_reset);
128
129 return 0;
130 }
131
imx_wdt_probe(struct udevice * dev)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
140 priv->ext_reset = dev_read_bool(dev, "fsl,ext-reset-output");
141
142 return 0;
143 }
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
167