xref: /optee_os/core/drivers/imx_wdog.c (revision b1469ba0bfd0371eb52bd50f5c52eeda7a8f5f1e)
1 /*
2  * Copyright 2017 NXP
3  *
4  * Peng Fan <peng.fan@nxp.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <assert.h>
30 #include <drivers/imx_wdog.h>
31 #include <io.h>
32 #include <keep.h>
33 #include <kernel/dt.h>
34 #include <kernel/generic_boot.h>
35 #include <kernel/panic.h>
36 #include <libfdt.h>
37 #include <mm/core_mmu.h>
38 #include <mm/core_memprot.h>
39 #include <util.h>
40 
41 static bool ext_reset;
42 static vaddr_t wdog_base;
43 
44 void imx_wdog_restart(void)
45 {
46 	uint32_t val;
47 
48 	if (!wdog_base) {
49 		EMSG("No wdog mapped\n");
50 		panic();
51 	}
52 
53 	if (ext_reset)
54 		val = 0x14;
55 	else
56 		val = 0x24;
57 
58 	DMSG("val %x\n", val);
59 
60 	write16(val, wdog_base + WCR_OFF);
61 	dsb();
62 
63 	if (read32(wdog_base + WDT_WCR) & WDT_WCR_WDE) {
64 		write32(WDT_SEQ1, wdog_base + WDT_WSR);
65 		write32(WDT_SEQ2, wdog_base + WDT_WSR);
66 	}
67 
68 	write16(val, wdog_base + WCR_OFF);
69 	write16(val, wdog_base + WCR_OFF);
70 
71 	while (1)
72 		;
73 }
74 KEEP_PAGER(imx_wdog_restart);
75 
76 static TEE_Result imx_wdog_init(void)
77 {
78 	enum teecore_memtypes mtype;
79 	void *fdt;
80 	paddr_t pbase;
81 	vaddr_t vbase;
82 	ssize_t sz;
83 	int off;
84 	int st;
85 	uint32_t i;
86 
87 #ifdef CFG_MX7
88 	static const char * const wdog_path[] = {
89 		"/soc/aips-bus@30000000/wdog@30280000",
90 		"/soc/aips-bus@30000000/wdog@30290000",
91 		"/soc/aips-bus@30000000/wdog@302a0000",
92 		"/soc/aips-bus@30000000/wdog@302b0000",
93 	};
94 #else
95 	static const char * const wdog_path[] = {
96 		"/soc/aips-bus@02000000/wdog@020bc000",
97 		"/soc/aips-bus@02000000/wdog@020c0000",
98 	};
99 #endif
100 
101 	fdt = get_dt_blob();
102 	if (!fdt) {
103 		EMSG("No DTB\n");
104 		return TEE_ERROR_NOT_SUPPORTED;
105 	}
106 
107 	/* search the first usable wdog */
108 	for (i = 0; i < ARRAY_SIZE(wdog_path); i++) {
109 		off = fdt_path_offset(fdt, wdog_path[i]);
110 		if (off < 0)
111 			continue;
112 
113 		st = _fdt_get_status(fdt, off);
114 		if (st & DT_STATUS_OK_SEC)
115 			break;
116 	}
117 
118 	if (i == ARRAY_SIZE(wdog_path))
119 		return TEE_ERROR_ITEM_NOT_FOUND;
120 
121 	DMSG("path: %s\n", wdog_path[i]);
122 
123 	ext_reset = dt_have_prop(fdt, off, "fsl,ext-reset-output");
124 
125 	pbase = _fdt_reg_base_address(fdt, off);
126 	if (pbase == (paddr_t)-1)
127 		return TEE_ERROR_ITEM_NOT_FOUND;
128 
129 	sz = _fdt_reg_size(fdt, off);
130 	if (sz < 0)
131 		return TEE_ERROR_ITEM_NOT_FOUND;
132 
133 	if ((st & DT_STATUS_OK_SEC) && !(st & DT_STATUS_OK_NSEC))
134 		mtype = MEM_AREA_IO_SEC;
135 	else
136 		mtype = MEM_AREA_IO_NSEC;
137 
138 	/*
139 	 * Check to see whether it has been mapped using
140 	 * register_phys_mem or not.
141 	 */
142 	vbase = (vaddr_t)phys_to_virt(pbase, mtype);
143 	if (!vbase) {
144 		if (!core_mmu_add_mapping(mtype, pbase, sz)) {
145 			EMSG("Failed to map %zu bytes at PA 0x%"PRIxPA,
146 			     (size_t)sz, pbase);
147 			return TEE_ERROR_GENERIC;
148 		}
149 	}
150 
151 	vbase = (vaddr_t)phys_to_virt(pbase, mtype);
152 	if (!vbase) {
153 		EMSG("Failed to get VA for PA 0x%"PRIxPA, pbase);
154 		return TEE_ERROR_GENERIC;
155 	}
156 
157 	wdog_base = vbase;
158 
159 	return TEE_SUCCESS;
160 }
161 driver_init(imx_wdog_init);
162