1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (C) 2016 Freescale Semiconductor, Inc.
4  * Copyright 2017-2019, 2021 NXP
5  *
6  * Peng Fan <peng.fan@nxp.com>
7  */
8 
9 #include <config.h>
10 #include <console.h>
11 #include <io.h>
12 #include <imx.h>
13 #include <mm/core_mmu.h>
14 #include <mm/core_memprot.h>
15 #include <platform_config.h>
16 
17 #define SOC_TYPE(reg)	       (((reg) & (0x00FF0000)) >> 16)
18 #define SOC_REV_MAJOR(reg)     (((reg) & (0x0000FF00)) >> 8)
19 #define SOC_REV_MINOR(reg)     ((reg) & (0x0000000F))
20 #define SOC_REV_MINOR_MX7(reg) ((reg) & (0x000000FF))
21 
22 static uint32_t imx_digprog;
23 
24 #ifdef ANATOP_BASE
imx_get_digprog(void)25 uint32_t imx_get_digprog(void)
26 {
27 	vaddr_t addr = 0;
28 
29 	if (imx_digprog)
30 		return imx_digprog;
31 
32 	addr = core_mmu_get_va(ANATOP_BASE, MEM_AREA_IO_SEC, 0x1000);
33 	if (!addr)
34 		return 0;
35 
36 	imx_digprog = io_read32(addr + DIGPROG_OFFSET);
37 
38 #ifdef CFG_MX8MQ
39 	/*
40 	 * On the i.MX8MQ, the minor revision number must be updated to make
41 	 * the difference between B0 chip and the newer chips.
42 	 */
43 	addr = core_mmu_get_va(OCOTP_BASE, MEM_AREA_IO_SEC, OCOTP_SIZE);
44 	if (!addr)
45 		return 0;
46 
47 	if (io_read32(addr + OCOTP_SW_INFO_B1) == OCOTP_SW_MAGIC_B1)
48 		imx_digprog |= BIT32(0);
49 #endif /* CFG_MX8MQ */
50 
51 	return imx_digprog;
52 }
53 #else  /* ANATOP_BASE */
imx_get_digprog(void)54 uint32_t imx_get_digprog(void)
55 {
56 	if (imx_digprog)
57 		return imx_digprog;
58 
59 	if (IS_ENABLED(CFG_MX7ULP))
60 		imx_digprog = SOC_MX7ULP << 16;
61 	else if (IS_ENABLED(CFG_MX8QX))
62 		imx_digprog = SOC_MX8QX << 16;
63 	else if (IS_ENABLED(CFG_MX8QM))
64 		imx_digprog = SOC_MX8QM << 16;
65 	else if (IS_ENABLED(CFG_MX8DXL))
66 		imx_digprog = SOC_MX8DXL << 16;
67 	else if (IS_ENABLED(CFG_MX8ULP))
68 		imx_digprog = SOC_MX8ULP << 16;
69 	else if (IS_ENABLED(CFG_MX93))
70 		imx_digprog = SOC_MX93 << 16;
71 	else if (IS_ENABLED(CFG_MX91))
72 		imx_digprog = SOC_MX91 << 16;
73 	else if (IS_ENABLED(CFG_MX95))
74 		imx_digprog = SOC_MX95 << 16;
75 
76 	return imx_digprog;
77 }
78 #endif /* ANATOP_BASE */
79 
imx_soc_rev_major(void)80 uint32_t imx_soc_rev_major(void)
81 {
82 	if (imx_digprog == 0)
83 		imx_get_digprog();
84 
85 	return SOC_REV_MAJOR(imx_digprog);
86 }
87 
imx_soc_rev_minor(void)88 uint32_t imx_soc_rev_minor(void)
89 {
90 	if (imx_digprog == 0)
91 		imx_get_digprog();
92 
93 	if (IS_ENABLED(CFG_MX7))
94 		return SOC_REV_MINOR_MX7(imx_digprog);
95 	else
96 		return SOC_REV_MINOR(imx_digprog);
97 }
98 
imx_soc_type(void)99 uint32_t imx_soc_type(void)
100 {
101 	if (imx_digprog == 0)
102 		imx_get_digprog();
103 
104 	return SOC_TYPE(imx_digprog);
105 }
106 
soc_is_imx6sl(void)107 bool soc_is_imx6sl(void)
108 {
109 	return imx_soc_type() == SOC_MX6SL;
110 }
111 
soc_is_imx6sll(void)112 bool soc_is_imx6sll(void)
113 {
114 	return imx_soc_type() == SOC_MX6SLL;
115 }
116 
soc_is_imx6sx(void)117 bool soc_is_imx6sx(void)
118 {
119 	return imx_soc_type() == SOC_MX6SX;
120 }
121 
soc_is_imx6ul(void)122 bool soc_is_imx6ul(void)
123 {
124 	return imx_soc_type() == SOC_MX6UL;
125 }
126 
soc_is_imx6ull(void)127 bool soc_is_imx6ull(void)
128 {
129 	return imx_soc_type() == SOC_MX6ULL;
130 }
131 
soc_is_imx6sdl(void)132 bool soc_is_imx6sdl(void)
133 {
134 	return imx_soc_type() == SOC_MX6DL;
135 }
136 
soc_is_imx6dq(void)137 bool soc_is_imx6dq(void)
138 {
139 	return (imx_soc_type() == SOC_MX6Q) && (imx_soc_rev_major() == 0);
140 }
141 
soc_is_imx6dqp(void)142 bool soc_is_imx6dqp(void)
143 {
144 	return (imx_soc_type() == SOC_MX6Q) && (imx_soc_rev_major() == 1);
145 }
146 
soc_is_imx6(void)147 bool soc_is_imx6(void)
148 {
149 	uint32_t soc = imx_soc_type();
150 
151 	return (soc == SOC_MX6SLL) || (soc == SOC_MX6SL) ||
152 	       (soc == SOC_MX6D) || (soc == SOC_MX6SX) ||
153 	       (soc == SOC_MX6UL) || (soc == SOC_MX6ULL) ||
154 	       (soc == SOC_MX6DL) || (soc == SOC_MX6Q);
155 }
156 
soc_is_imx7ds(void)157 bool soc_is_imx7ds(void)
158 {
159 	return imx_soc_type() == SOC_MX7D;
160 }
161 
soc_is_imx7ulp(void)162 bool soc_is_imx7ulp(void)
163 {
164 	return imx_soc_type() == SOC_MX7ULP;
165 }
166 
soc_is_imx8mq(void)167 bool soc_is_imx8mq(void)
168 {
169 	return imx_soc_type() == SOC_MX8M && imx_soc_rev_major() == 0x40;
170 }
171 
soc_is_imx8mm(void)172 bool soc_is_imx8mm(void)
173 {
174 	return imx_soc_type() == SOC_MX8M && imx_soc_rev_major() == 0x41;
175 }
176 
soc_is_imx8mn(void)177 bool soc_is_imx8mn(void)
178 {
179 	return imx_soc_type() == SOC_MX8M && imx_soc_rev_major() == 0x42;
180 }
181 
soc_is_imx8mp(void)182 bool soc_is_imx8mp(void)
183 {
184 	return imx_soc_type() == SOC_MX8M && imx_soc_rev_major() == 0x43;
185 }
186 
soc_is_imx8m(void)187 bool soc_is_imx8m(void)
188 {
189 	return soc_is_imx8mq() || soc_is_imx8mm() || soc_is_imx8mn() ||
190 	       soc_is_imx8mp();
191 }
192 
soc_is_imx8mq_b0_layer(void)193 bool soc_is_imx8mq_b0_layer(void)
194 {
195 	if (soc_is_imx8mq() && imx_soc_rev_minor() == 0x0)
196 		return true;
197 	else
198 		return false;
199 }
200