xref: /optee_os/core/drivers/imx_wdog.c (revision 1bb929836182ecb96d2d9d268daa807c67596396)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright 2017 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/generic_boot.h>
36 #include <kernel/panic.h>
37 #include <libfdt.h>
38 #include <mm/core_mmu.h>
39 #include <mm/core_memprot.h>
40 #include <util.h>
41 
42 static bool ext_reset;
43 static vaddr_t wdog_base;
44 
45 void imx_wdog_restart(void)
46 {
47 	uint32_t val;
48 
49 	if (!wdog_base) {
50 		EMSG("No wdog mapped\n");
51 		panic();
52 	}
53 
54 	if (ext_reset)
55 		val = 0x14;
56 	else
57 		val = 0x24;
58 
59 	DMSG("val %x\n", val);
60 
61 	write16(val, wdog_base + WCR_OFF);
62 	dsb();
63 
64 	if (read16(wdog_base + WDT_WCR) & WDT_WCR_WDE) {
65 		write16(WDT_SEQ1, wdog_base + WDT_WSR);
66 		write16(WDT_SEQ2, wdog_base + WDT_WSR);
67 	}
68 
69 	write16(val, wdog_base + WCR_OFF);
70 	write16(val, wdog_base + WCR_OFF);
71 
72 	while (1)
73 		;
74 }
75 KEEP_PAGER(imx_wdog_restart);
76 
77 static TEE_Result imx_wdog_init(void)
78 {
79 	enum teecore_memtypes mtype;
80 	void *fdt;
81 	paddr_t pbase;
82 	vaddr_t vbase;
83 	ssize_t sz;
84 	int off;
85 	int st;
86 	uint32_t i;
87 
88 #ifdef CFG_MX7
89 	static const char * const wdog_path[] = {
90 		"/soc/aips-bus@30000000/wdog@30280000",
91 		"/soc/aips-bus@30000000/wdog@30290000",
92 		"/soc/aips-bus@30000000/wdog@302a0000",
93 		"/soc/aips-bus@30000000/wdog@302b0000",
94 	};
95 #else
96 	static const char * const wdog_path[] = {
97 		"/soc/aips-bus@02000000/wdog@020bc000",
98 		"/soc/aips-bus@02000000/wdog@020c0000",
99 	};
100 #endif
101 
102 	fdt = get_dt_blob();
103 	if (!fdt) {
104 		EMSG("No DTB\n");
105 		return TEE_ERROR_NOT_SUPPORTED;
106 	}
107 
108 	/* search the first usable wdog */
109 	for (i = 0; i < ARRAY_SIZE(wdog_path); i++) {
110 		off = fdt_path_offset(fdt, wdog_path[i]);
111 		if (off < 0)
112 			continue;
113 
114 		st = _fdt_get_status(fdt, off);
115 		if (st & DT_STATUS_OK_SEC)
116 			break;
117 	}
118 
119 	if (i == ARRAY_SIZE(wdog_path))
120 		return TEE_ERROR_ITEM_NOT_FOUND;
121 
122 	DMSG("path: %s\n", wdog_path[i]);
123 
124 	ext_reset = dt_have_prop(fdt, off, "fsl,ext-reset-output");
125 
126 	pbase = _fdt_reg_base_address(fdt, off);
127 	if (pbase == (paddr_t)-1)
128 		return TEE_ERROR_ITEM_NOT_FOUND;
129 
130 	sz = _fdt_reg_size(fdt, off);
131 	if (sz < 0)
132 		return TEE_ERROR_ITEM_NOT_FOUND;
133 
134 	if ((st & DT_STATUS_OK_SEC) && !(st & DT_STATUS_OK_NSEC))
135 		mtype = MEM_AREA_IO_SEC;
136 	else
137 		mtype = MEM_AREA_IO_NSEC;
138 
139 	/*
140 	 * Check to see whether it has been mapped using
141 	 * register_phys_mem or not.
142 	 */
143 	vbase = (vaddr_t)phys_to_virt(pbase, mtype);
144 	if (!vbase) {
145 		if (!core_mmu_add_mapping(mtype, pbase, sz)) {
146 			EMSG("Failed to map %zu bytes at PA 0x%"PRIxPA,
147 			     (size_t)sz, pbase);
148 			return TEE_ERROR_GENERIC;
149 		}
150 	}
151 
152 	vbase = (vaddr_t)phys_to_virt(pbase, mtype);
153 	if (!vbase) {
154 		EMSG("Failed to get VA for PA 0x%"PRIxPA, pbase);
155 		return TEE_ERROR_GENERIC;
156 	}
157 
158 	wdog_base = vbase;
159 
160 	return TEE_SUCCESS;
161 }
162 driver_init(imx_wdog_init);
163