Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ye Li | 62862b6 | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Freescale Semiconductor, Inc. |
Ye Li | 62862b6 | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | afb0215 | 2019-12-28 10:45:01 -0700 | [diff] [blame] | 7 | #include <cpu_func.h> |
Ye Li | 62862b6 | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 8 | #include <asm/io.h> |
| 9 | #include <asm/arch/imx-regs.h> |
| 10 | |
| 11 | /* |
| 12 | * MX7ULP WDOG Register Map |
| 13 | */ |
| 14 | struct wdog_regs { |
Breno Lima | 4a68689 | 2021-06-29 10:32:35 +0800 | [diff] [blame] | 15 | u32 cs; |
Ye Li | 62862b6 | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 16 | u32 cnt; |
| 17 | u32 toval; |
| 18 | u32 win; |
| 19 | }; |
| 20 | |
| 21 | #ifndef CONFIG_WATCHDOG_TIMEOUT_MSECS |
| 22 | #define CONFIG_WATCHDOG_TIMEOUT_MSECS 0x1500 |
| 23 | #endif |
| 24 | |
| 25 | #define REFRESH_WORD0 0xA602 /* 1st refresh word */ |
| 26 | #define REFRESH_WORD1 0xB480 /* 2nd refresh word */ |
| 27 | |
| 28 | #define UNLOCK_WORD0 0xC520 /* 1st unlock word */ |
| 29 | #define UNLOCK_WORD1 0xD928 /* 2nd unlock word */ |
| 30 | |
Breno Lima | 4a68689 | 2021-06-29 10:32:35 +0800 | [diff] [blame] | 31 | #define WDGCS_WDGE BIT(7) |
| 32 | #define WDGCS_WDGUPDATE BIT(5) |
Ye Li | 62862b6 | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 33 | |
Breno Lima | 4a68689 | 2021-06-29 10:32:35 +0800 | [diff] [blame] | 34 | #define WDGCS_RCS BIT(10) |
| 35 | #define WDGCS_ULK BIT(11) |
| 36 | #define WDGCS_FLG BIT(14) |
Ye Li | 62862b6 | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 37 | |
| 38 | #define WDG_BUS_CLK (0x0) |
| 39 | #define WDG_LPO_CLK (0x1) |
| 40 | #define WDG_32KHZ_CLK (0x2) |
| 41 | #define WDG_EXT_CLK (0x3) |
| 42 | |
Ye Li | 62862b6 | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 43 | void hw_watchdog_set_timeout(u16 val) |
| 44 | { |
| 45 | /* setting timeout value */ |
| 46 | struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE_ADDR; |
| 47 | |
| 48 | writel(val, &wdog->toval); |
| 49 | } |
| 50 | |
| 51 | void hw_watchdog_reset(void) |
| 52 | { |
| 53 | struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE_ADDR; |
| 54 | |
Breno Lima | 3c5e963 | 2021-06-29 10:32:34 +0800 | [diff] [blame] | 55 | dmb(); |
| 56 | __raw_writel(REFRESH_WORD0, &wdog->cnt); |
| 57 | __raw_writel(REFRESH_WORD1, &wdog->cnt); |
| 58 | dmb(); |
Ye Li | 62862b6 | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void hw_watchdog_init(void) |
| 62 | { |
Ye Li | 62862b6 | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 63 | struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE_ADDR; |
| 64 | |
Breno Lima | 3c5e963 | 2021-06-29 10:32:34 +0800 | [diff] [blame] | 65 | dmb(); |
| 66 | __raw_writel(UNLOCK_WORD0, &wdog->cnt); |
| 67 | __raw_writel(UNLOCK_WORD1, &wdog->cnt); |
| 68 | dmb(); |
Ye Li | 62862b6 | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 69 | |
Breno Lima | 4a68689 | 2021-06-29 10:32:35 +0800 | [diff] [blame] | 70 | /* Wait WDOG Unlock */ |
| 71 | while (!(readl(&wdog->cs) & WDGCS_ULK)) |
| 72 | ; |
Ye Li | 62862b6 | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 73 | |
| 74 | hw_watchdog_set_timeout(CONFIG_WATCHDOG_TIMEOUT_MSECS); |
| 75 | writel(0, &wdog->win); |
| 76 | |
Breno Lima | 4a68689 | 2021-06-29 10:32:35 +0800 | [diff] [blame] | 77 | /* setting 1-kHz clock source, enable counter running, and clear interrupt */ |
| 78 | writel((WDGCS_WDGE | WDGCS_WDGUPDATE |(WDG_LPO_CLK << 8) | WDGCS_FLG), &wdog->cs); |
| 79 | |
| 80 | /* Wait WDOG reconfiguration */ |
| 81 | while (!(readl(&wdog->cs) & WDGCS_RCS)) |
| 82 | ; |
Ye Li | 62862b6 | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 83 | |
| 84 | hw_watchdog_reset(); |
| 85 | } |
| 86 | |
Harald Seiler | 6f14d5f | 2020-12-15 16:47:52 +0100 | [diff] [blame] | 87 | void reset_cpu(void) |
Ye Li | 62862b6 | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 88 | { |
| 89 | struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE_ADDR; |
| 90 | |
Breno Lima | 3c5e963 | 2021-06-29 10:32:34 +0800 | [diff] [blame] | 91 | dmb(); |
| 92 | __raw_writel(UNLOCK_WORD0, &wdog->cnt); |
| 93 | __raw_writel(UNLOCK_WORD1, &wdog->cnt); |
| 94 | dmb(); |
Ye Li | 62862b6 | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 95 | |
Breno Lima | 4a68689 | 2021-06-29 10:32:35 +0800 | [diff] [blame] | 96 | /* Wait WDOG Unlock */ |
| 97 | while (!(readl(&wdog->cs) & WDGCS_ULK)) |
| 98 | ; |
| 99 | |
Ye Li | 62862b6 | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 100 | hw_watchdog_set_timeout(5); /* 5ms timeout */ |
| 101 | writel(0, &wdog->win); |
| 102 | |
Breno Lima | 4a68689 | 2021-06-29 10:32:35 +0800 | [diff] [blame] | 103 | /* enable counter running */ |
| 104 | writel((WDGCS_WDGE | (WDG_LPO_CLK << 8)), &wdog->cs); |
| 105 | |
| 106 | /* Wait WDOG reconfiguration */ |
| 107 | while (!(readl(&wdog->cs) & WDGCS_RCS)) |
| 108 | ; |
Ye Li | 62862b6 | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 109 | |
| 110 | hw_watchdog_reset(); |
| 111 | |
| 112 | while (1); |
| 113 | } |