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_output __maybe_unused; 43 static vaddr_t wdog_base; 44 45 void imx_wdog_restart(bool external_reset __maybe_unused) 46 { 47 uint32_t val = 0; 48 49 if (!wdog_base) { 50 EMSG("No wdog mapped\n"); 51 panic(); 52 } 53 54 #ifdef CFG_MX7ULP 55 val = io_read32(wdog_base + WDOG_CS); 56 57 io_write32(wdog_base + WDOG_CNT, UNLOCK); 58 /* Enable wdog */ 59 io_write32(wdog_base + WDOG_CS, val | WDOG_CS_EN); 60 61 io_write32(wdog_base + WDOG_CNT, UNLOCK); 62 io_write32(wdog_base + WDOG_TOVAL, 1000); 63 io_write32(wdog_base + WDOG_CNT, REFRESH); 64 #else 65 if (external_reset && ext_reset_output) 66 val = 0x14; 67 else 68 val = 0x24; 69 70 DMSG("val %x\n", val); 71 72 io_write16(wdog_base + WDT_WCR, val); 73 dsb(); 74 75 if (io_read16(wdog_base + WDT_WCR) & WDT_WCR_WDE) { 76 io_write16(wdog_base + WDT_WSR, WDT_SEQ1); 77 io_write16(wdog_base + WDT_WSR, WDT_SEQ2); 78 } 79 80 io_write16(wdog_base + WDT_WCR, val); 81 io_write16(wdog_base + WDT_WCR, val); 82 #endif 83 84 while (1) 85 ; 86 } 87 DECLARE_KEEP_PAGER(imx_wdog_restart); 88 89 #if defined(CFG_DT) && !defined(CFG_EXTERNAL_DTB_OVERLAY) 90 static const char * const dt_wdog_match_table[] = { 91 "fsl,imx21-wdt", 92 "fsl,imx7ulp-wdt", 93 }; 94 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_output = dt_have_prop(fdt, found_off, 139 "fsl,ext-reset-output"); 140 141 if (dt_map_dev(fdt, found_off, &vbase, &sz) < 0) { 142 EMSG("Failed to map Watchdog\n"); 143 return TEE_ERROR_ITEM_NOT_FOUND; 144 } 145 146 *wdog_vbase = vbase; 147 148 return TEE_SUCCESS; 149 } 150 #else 151 register_phys_mem_pgdir(MEM_AREA_IO_SEC, WDOG_BASE, CORE_MMU_PGDIR_SIZE); 152 static TEE_Result imx_wdog_base(vaddr_t *wdog_vbase) 153 { 154 *wdog_vbase = (vaddr_t)phys_to_virt(WDOG_BASE, MEM_AREA_IO_SEC, 1); 155 #if defined(CFG_IMX_WDOG_EXT_RESET) 156 ext_reset_output = true; 157 #endif 158 return TEE_SUCCESS; 159 } 160 #endif 161 162 static TEE_Result imx_wdog_init(void) 163 { 164 return imx_wdog_base(&wdog_base); 165 } 166 driver_init(imx_wdog_init); 167