aspeed: Watchdog Timer Driver
[oweals/u-boot.git] / arch / arm / mach-aspeed / ast_wdt.c
1 /*
2  * (C) Copyright 2016 Google, Inc
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6
7 #include <common.h>
8 #include <asm/io.h>
9 #include <asm/arch/wdt.h>
10 #include <linux/err.h>
11
12 u32 ast_reset_mode_from_flags(ulong flags)
13 {
14         return flags & WDT_CTRL_RESET_MASK;
15 }
16
17 u32 ast_reset_mask_from_flags(ulong flags)
18 {
19         return flags >> 2;
20 }
21
22 ulong ast_flags_from_reset_mode_mask(u32 reset_mode, u32 reset_mask)
23 {
24         ulong ret = reset_mode & WDT_CTRL_RESET_MASK;
25
26         if (ret == WDT_CTRL_RESET_SOC)
27                 ret |= (reset_mask << 2);
28
29         return ret;
30 }
31
32 #ifndef CONFIG_WDT
33 void wdt_stop(struct ast_wdt *wdt)
34 {
35         clrbits_le32(&wdt->ctrl, WDT_CTRL_EN);
36 }
37
38 void wdt_start(struct ast_wdt *wdt, u32 timeout)
39 {
40         writel(timeout, &wdt->counter_reload_val);
41         writel(WDT_COUNTER_RESTART_VAL, &wdt->counter_restart);
42         /*
43          * Setting CLK1MHZ bit is just for compatibility with ast2400 part.
44          * On ast2500 watchdog timer clock is fixed at 1MHz and the bit is
45          * read-only
46          */
47         setbits_le32(&wdt->ctrl,
48                      WDT_CTRL_EN | WDT_CTRL_RESET | WDT_CTRL_CLK1MHZ);
49 }
50 #endif  /* CONFIG_WDT */
51
52 int ast_wdt_reset_masked(struct ast_wdt *wdt, u32 mask)
53 {
54 #ifdef CONFIG_ASPEED_AST2500
55         if (!mask)
56                 return -EINVAL;
57
58         writel(mask, &wdt->reset_mask);
59         clrbits_le32(&wdt->ctrl,
60                      WDT_CTRL_RESET_MASK << WDT_CTRL_RESET_MODE_SHIFT);
61         wdt_start(wdt, 1);
62
63         /* Wait for WDT to reset */
64         while (readl(&wdt->ctrl) & WDT_CTRL_EN)
65                 ;
66         wdt_stop(wdt);
67
68         return 0;
69 #else
70         return -EINVAL;
71 #endif
72 }
73
74 struct ast_wdt *ast_get_wdt(u8 wdt_number)
75 {
76         if (wdt_number > CONFIG_WDT_NUM - 1)
77                 return ERR_PTR(-EINVAL);
78
79         return (struct ast_wdt *)(WDT_BASE +
80                                   sizeof(struct ast_wdt) * wdt_number);
81 }