1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright 2017-2019 NXP 4 * 5 * Peng Fan <peng.fan@nxp.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright notice, 11 * this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright notice, 14 * this list of conditions and the following disclaimer in the documentation 15 * and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <assert.h> 31 #include <drivers/imx_wdog.h> 32 #include <io.h> 33 #include <keep.h> 34 #include <kernel/dt.h> 35 #include <kernel/boot.h> 36 #include <kernel/panic.h> 37 #include <libfdt.h> 38 #include <mm/core_memprot.h> 39 #include <mm/core_mmu.h> 40 #include <util.h> 41 42 static bool ext_reset; 43 static vaddr_t wdog_base; 44 45 static const char * const dt_wdog_match_table[] = { 46 "fsl,imx21-wdt", 47 "fsl,imx7ulp-wdt", 48 }; 49 50 void imx_wdog_restart(void) 51 { 52 uint32_t val = 0; 53 54 if (!wdog_base) { 55 EMSG("No wdog mapped\n"); 56 panic(); 57 } 58 59 #ifdef CFG_MX7ULP 60 val = io_read32(wdog_base + WDOG_CS); 61 62 io_write32(wdog_base + WDOG_CNT, UNLOCK); 63 /* Enable wdog */ 64 io_write32(wdog_base + WDOG_CS, val | WDOG_CS_EN); 65 66 io_write32(wdog_base + WDOG_CNT, UNLOCK); 67 io_write32(wdog_base + WDOG_TOVAL, 1000); 68 io_write32(wdog_base + WDOG_CNT, REFRESH); 69 #else 70 if (ext_reset) 71 val = 0x14; 72 else 73 val = 0x24; 74 75 DMSG("val %x\n", val); 76 77 io_write16(wdog_base + WDT_WCR, val); 78 dsb(); 79 80 if (io_read16(wdog_base + WDT_WCR) & WDT_WCR_WDE) { 81 io_write16(wdog_base + WDT_WSR, WDT_SEQ1); 82 io_write16(wdog_base + WDT_WSR, WDT_SEQ2); 83 } 84 85 io_write16(wdog_base + WDT_WCR, val); 86 io_write16(wdog_base + WDT_WCR, val); 87 #endif 88 89 while (1) 90 ; 91 } 92 DECLARE_KEEP_PAGER(imx_wdog_restart); 93 94 #if defined(CFG_DT) && !defined(CFG_EXTERNAL_DTB_OVERLAY) 95 static TEE_Result imx_wdog_base(vaddr_t *wdog_vbase) 96 { 97 const char *match = NULL; 98 void *fdt = NULL; 99 vaddr_t vbase = 0; 100 int found_off = 0; 101 size_t sz = 0; 102 int off = 0; 103 int st = 0; 104 uint32_t i = 0; 105 106 fdt = get_dt(); 107 if (!fdt) { 108 EMSG("No DTB\n"); 109 return TEE_ERROR_NOT_SUPPORTED; 110 } 111 112 /* search the first usable wdog */ 113 for (i = 0; i < ARRAY_SIZE(dt_wdog_match_table); i++) { 114 match = dt_wdog_match_table[i]; 115 off = 0; 116 while (off >= 0) { 117 off = fdt_node_offset_by_compatible(fdt, off, match); 118 if (off > 0) { 119 st = _fdt_get_status(fdt, off); 120 if (st & DT_STATUS_OK_SEC) { 121 DMSG("Wdog found at %u", off); 122 found_off = off; 123 break; 124 } 125 } 126 } 127 if (found_off) 128 break; 129 else 130 DMSG("%s not found in DTB", dt_wdog_match_table[i]); 131 } 132 133 if (!found_off) { 134 EMSG("No Watchdog found in DTB\n"); 135 return TEE_ERROR_ITEM_NOT_FOUND; 136 } 137 138 ext_reset = dt_have_prop(fdt, found_off, "fsl,ext-reset-output"); 139 140 if (dt_map_dev(fdt, found_off, &vbase, &sz) < 0) { 141 EMSG("Failed to map Watchdog\n"); 142 return TEE_ERROR_ITEM_NOT_FOUND; 143 } 144 145 *wdog_vbase = vbase; 146 147 return TEE_SUCCESS; 148 } 149 #else 150 register_phys_mem_pgdir(MEM_AREA_IO_SEC, WDOG_BASE, CORE_MMU_PGDIR_SIZE); 151 static TEE_Result imx_wdog_base(vaddr_t *wdog_vbase) 152 { 153 *wdog_vbase = (vaddr_t)phys_to_virt(WDOG_BASE, MEM_AREA_IO_SEC); 154 #if defined(CFG_IMX_WDOG_EXT_RESET) 155 ext_reset = true; 156 #endif 157 return TEE_SUCCESS; 158 } 159 #endif 160 161 static TEE_Result imx_wdog_init(void) 162 { 163 #if defined(PLATFORM_FLAVOR_mx7dsabresd) || \ 164 defined(PLATFORM_FLAVOR_mx7dclsom) 165 166 ext_reset = true; 167 #endif 168 return imx_wdog_base(&wdog_base); 169 } 170 driver_init(imx_wdog_init); 171