xref: /openbmc/u-boot/cmd/otp.c (revision 57efeb04)
1 /*
2  *  This program is distributed in the hope that it will be useful,
3  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
4  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5  *  GNU General Public License for more details.
6  *
7  *  You should have received a copy of the GNU General Public License
8  *  along with this program; if not, write to the Free Software
9  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
10  */
11 #include <stdlib.h>
12 #include <common.h>
13 #include <console.h>
14 #include <bootretry.h>
15 #include <cli.h>
16 #include <command.h>
17 #include <console.h>
18 #include <malloc.h>
19 #include <inttypes.h>
20 #include <mapmem.h>
21 #include <asm/io.h>
22 #include <linux/compiler.h>
23 
24 DECLARE_GLOBAL_DATA_PTR;
25 
26 #define OTP_PASSWD			0x349fe38a
27 #define RETRY				3
28 #define OTP_REGION_STRAP		BIT(0)
29 #define OTP_REGION_CONF			BIT(1)
30 #define OTP_REGION_DATA			BIT(2)
31 
32 #define OTP_USAGE			-1
33 #define OTP_FAILURE			-2
34 #define OTP_SUCCESS			0
35 
36 #define OTP_PROG_SKIP			1
37 
38 #define OTP_REG_RESERVED		-1
39 #define OTP_REG_VALUE			-2
40 #define OTP_REG_VALID_BIT		-3
41 
42 #define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
43 #define PBWIDTH 60
44 
45 struct otpstrap_status {
46 	int value;
47 	int option_array[7];
48 	int remain_times;
49 	int writeable_option;
50 	int protected;
51 };
52 
53 struct otpconf_parse {
54 	int dw_offset;
55 	int bit;
56 	int length;
57 	int value;
58 	int keep;
59 	char status[80];
60 };
61 
62 struct otpstrap_info {
63 	uint32_t bit_offset;
64 	uint32_t length;
65 	int value;
66 	char information[80];
67 };
68 
69 struct otpconf_info {
70 	uint32_t dw_offset;
71 	uint32_t bit_offset;
72 	uint32_t length;
73 	int value;
74 	char information[80];
75 };
76 
77 void printProgress(int numerator, int denominator, char *format, ...)
78 {
79 	int val = numerator * 100 / denominator;
80 	int lpad = numerator * PBWIDTH / denominator;
81 	int rpad = PBWIDTH - lpad;
82 	char buffer[256];
83 	va_list aptr;
84 
85 	va_start(aptr, format);
86 	vsprintf(buffer, format, aptr);
87 	va_end(aptr);
88 
89 	printf("\r%3d%% [%.*s%*s] %s", val, lpad, PBSTR, rpad, "", buffer);
90 	if (numerator == denominator)
91 		printf("\n");
92 }
93 
94 struct otpstrap_info a0_strap_info[] = {
95 	{ 0, 1, 0, "Disable secure boot" },
96 	{ 0, 1, 1, "Enable secure boot"	},
97 	{ 1, 1, 0, "Disable boot from eMMC" },
98 	{ 1, 1, 1, "Enable boot from eMMC" },
99 	{ 2, 1, 0, "Disable Boot from debug SPI" },
100 	{ 2, 1, 1, "Enable Boot from debug SPI" },
101 	{ 3, 1, 0, "Enable ARM CM3" },
102 	{ 3, 1, 1, "Disable ARM CM3" },
103 	{ 4, 1, 0, "No VGA BISO ROM, VGA BIOS is merged in the system BIOS" },
104 	{ 4, 1, 1, "Enable dedicated VGA BIOS ROM" },
105 	{ 5, 1, 0, "MAC 1 : RMII/NCSI" },
106 	{ 5, 1, 1, "MAC 1 : RGMII" },
107 	{ 6, 1, 0, "MAC 2 : RMII/NCSI" },
108 	{ 6, 1, 1, "MAC 2 : RGMII" },
109 	{ 7, 3, 0, "CPU Frequency : 1GHz" },
110 	{ 7, 3, 1, "CPU Frequency : 800MHz" },
111 	{ 7, 3, 2, "CPU Frequency : 1.2GHz" },
112 	{ 7, 3, 3, "CPU Frequency : 1.4GHz" },
113 	{ 10, 2, 0, "HCLK ratio AXI:AHB = 2:1" },
114 	{ 10, 2, 1, "HCLK ratio AXI:AHB = 2:1" },
115 	{ 10, 2, 2, "HCLK ratio AXI:AHB = 3:1" },
116 	{ 10, 2, 3, "HCLK ratio AXI:AHB = 4:1" },
117 	{ 12, 2, 0, "VGA memory size : 8MB" },
118 	{ 12, 2, 1, "VGA memory size : 16MB" },
119 	{ 12, 2, 2, "VGA memory size : 32MB" },
120 	{ 12, 2, 3, "VGA memory size : 64MB" },
121 	{ 14, 3, OTP_REG_RESERVED, "" },
122 	{ 17, 1, 0, "VGA class code : Class Code for video device" },
123 	{ 17, 1, 1, "VGA class code : Class Code for VGA device" },
124 	{ 18, 1, 0, "Enable debug interfaces 0" },
125 	{ 18, 1, 1, "Disable debug interfaces 0" },
126 	{ 19, 1, 0, "Boot from emmc mode : High eMMC speed" },
127 	{ 19, 1, 1, "Boot from emmc mode : Normal eMMC speed" },
128 	{ 20, 1, 0, "Enable Pcie EHCI device" },
129 	{ 20, 1, 1, "Disable Pcie EHCI device" },
130 	{ 21, 1, 0, "Enable VGA XDMA function" },
131 	{ 21, 1, 1, "Disable VGA XDMA function" },
132 	{ 22, 1, 0, "Normal BMC mode" },
133 	{ 22, 1, 1, "Disable dedicated BMC functions for non-BMC application" },
134 	{ 23, 1, 0, "SSPRST# pin is for secondary processor dedicated reset pin" },
135 	{ 23, 1, 1, "SSPRST# pin is for PCIE root complex dedicated reset pin" },
136 	{ 24, 1, 0, "DRAM types : DDR4" },
137 	{ 24, 1, 1, "DRAM types : DDR3" },
138 	{ 25, 5, OTP_REG_RESERVED, "" },
139 	{ 30, 2, OTP_REG_RESERVED, "" },
140 	{ 32, 1, 0, "MAC 3 : RMII/NCSI" },
141 	{ 32, 1, 1, "MAC 3 : RGMII" },
142 	{ 33, 1, 0, "MAC 4 : RMII/NCSI" },
143 	{ 33, 1, 1, "MAC 4 : RGMII" },
144 	{ 34, 1, 0, "SuperIO configuration address : 0x2E" },
145 	{ 34, 1, 1, "SuperIO configuration address : 0x4E" },
146 	{ 35, 1, 0, "Enable LPC to decode SuperIO" },
147 	{ 35, 1, 1, "Disable LPC to decode SuperIO" },
148 	{ 36, 1, 0, "Enable debug interfaces 1" },
149 	{ 36, 1, 1, "Disable debug interfaces 1" },
150 	{ 37, 1, 0, "Disable ACPI function" },
151 	{ 37, 1, 1, "Enable ACPI function" },
152 	{ 38, 1, 0, "Enable eSPI mode" },
153 	{ 38, 1, 1, "Enable LPC mode" },
154 	{ 39, 1, 0, "Enable SAFS mode" },
155 	{ 39, 1, 1, "Enable SAFS mode" },
156 	{ 40, 2, OTP_REG_RESERVED, "" },
157 	{ 42, 1, 0, "Disable boot SPI 3B/4B address mode auto detection" },
158 	{ 42, 1, 1, "Enable boot SPI 3B/4B address mode auto detection" },
159 	{ 43, 1, 0, "Disable boot SPI ABR" },
160 	{ 43, 1, 1, "Enable boot SPI ABR" },
161 	{ 44, 1, 0, "Boot SPI ABR mode : dual SPI flash" },
162 	{ 44, 1, 1, "Boot SPI ABR mode : single SPI flash" },
163 	{ 45, 3, 0, "Boot SPI flash size : no define size" },
164 	{ 45, 3, 1, "Boot SPI flash size : 2MB" },
165 	{ 45, 3, 2, "Boot SPI flash size : 4MB" },
166 	{ 45, 3, 3, "Boot SPI flash size : 8MB" },
167 	{ 45, 3, 4, "Boot SPI flash size : 16MB" },
168 	{ 45, 3, 5, "Boot SPI flash size : 32MB" },
169 	{ 45, 3, 6, "Boot SPI flash size : 64MB" },
170 	{ 45, 3, 7, "Boot SPI flash size : 128MB" },
171 	{ 48, 1, 0, "Disable host SPI ABR" },
172 	{ 48, 1, 1, "Enable host SPI ABR" },
173 	{ 49, 1, 0, "Disable host SPI ABR mode select pin" },
174 	{ 49, 1, 1, "Enable host SPI ABR mode select pin" },
175 	{ 50, 1, 0, "Host SPI ABR mode : dual SPI flash" },
176 	{ 50, 1, 1, "Host SPI ABR mode : single SPI flash" },
177 	{ 51, 3, 0, "Host SPI flash size : no define size" },
178 	{ 51, 3, 1, "Host SPI flash size : 2MB" },
179 	{ 51, 3, 2, "Host SPI flash size : 4MB" },
180 	{ 51, 3, 3, "Host SPI flash size : 8MB" },
181 	{ 51, 3, 4, "Host SPI flash size : 16MB" },
182 	{ 51, 3, 5, "Host SPI flash size : 32MB" },
183 	{ 51, 3, 6, "Host SPI flash size : 64MB" },
184 	{ 51, 3, 7, "Host SPI flash size : 128MB" },
185 	{ 54, 1, 0, "Disable boot SPI auxiliary control pins" },
186 	{ 54, 1, 1, "Enable boot SPI auxiliary control pins" },
187 	{ 55, 2, 0, "Boot SPI CRTM size : disable CRTM" },
188 	{ 55, 2, 1, "Boot SPI CRTM size : 256KB" },
189 	{ 55, 2, 2, "Boot SPI CRTM size : 512KB" },
190 	{ 55, 2, 3, "Boot SPI CRTM size : 1MB" },
191 	{ 57, 2, 0, "Host SPI CRTM size : disable CRTM" },
192 	{ 57, 2, 1, "Host SPI CRTM size : 256KB" },
193 	{ 57, 2, 2, "Host SPI CRTM size : 512KB" },
194 	{ 57, 2, 3, "Host SPI CRTM size : 1MB" },
195 	{ 59, 1, 0, "Disable host SPI auxiliary control pins" },
196 	{ 59, 1, 1, "Enable host SPI auxiliary control pins" },
197 	{ 60, 1, 0, "Disable GPIO pass through" },
198 	{ 60, 1, 1, "Enable GPIO pass through" },
199 	{ 61, 1, 0, "Enable low security secure boot key" },
200 	{ 61, 1, 1, "Disable low security secure boot key" },
201 	{ 62, 1, 0, "Disable dedicate GPIO strap pins" },
202 	{ 62, 1, 1, "Enable dedicate GPIO strap pins" },
203 	{ 63, 1, OTP_REG_RESERVED, "" }
204 };
205 struct otpconf_info a0_conf_info[] = {
206 	{ 0, 0,  1,  0, "Enable Secure Region programming" },
207 	{ 0, 0,  1,  1, "Disable Secure Region programming" },
208 	{ 0, 1,  1,  0, "Disable Secure Boot" },
209 	{ 0, 1,  1,  1, "Enable Secure Boot" },
210 	{ 0, 2,  1,  0, "Initialization programming not done" },
211 	{ 0, 2,  1,  1, "Initialization programming done" },
212 	{ 0, 3,  1,  0, "User region ECC disable" },
213 	{ 0, 3,  1,  1, "User region ECC enable" },
214 	{ 0, 4,  1,  0, "Secure Region ECC disable" },
215 	{ 0, 4,  1,  1, "Secure Region ECC enable" },
216 	{ 0, 5,  1,  0, "Enable low security key" },
217 	{ 0, 5,  1,  1, "Disable low security key" },
218 	{ 0, 6,  1,  0, "Do not ignore Secure Boot hardware strap" },
219 	{ 0, 6,  1,  1, "Ignore Secure Boot hardware strap" },
220 	{ 0, 7,  1,  0, "Secure Boot Mode: 1" },
221 	{ 0, 7,  1,  1, "Secure Boot Mode: 2" },
222 	{ 0, 8,  2,  0, "Single cell mode (recommended)" },
223 	{ 0, 8,  2,  1, "Differnetial mode" },
224 	{ 0, 8,  2,  2, "Differential-redundant mode" },
225 	{ 0, 10, 2,  0, "RSA mode : RSA1024" },
226 	{ 0, 10, 2,  1, "RSA mode : RSA2048" },
227 	{ 0, 10, 2,  2, "RSA mode : RSA3072" },
228 	{ 0, 10, 2,  3, "RSA mode : RSA4096" },
229 	{ 0, 12, 2,  0, "SHA mode : SHA224" },
230 	{ 0, 12, 2,  1, "SHA mode : SHA256" },
231 	{ 0, 12, 2,  2, "SHA mode : SHA384" },
232 	{ 0, 12, 2,  3, "SHA mode : SHA512" },
233 	{ 0, 14, 2,  OTP_REG_RESERVED, "" },
234 	{ 0, 16, 6,  OTP_REG_VALUE, "Secure Region size (DW): 0x%x" },
235 	{ 0, 22, 1,  0, "Secure Region : Writable" },
236 	{ 0, 22, 1,  1, "Secure Region : Write Protect" },
237 	{ 0, 23, 1,  0, "User Region : Writable" },
238 	{ 0, 23, 1,  1, "User Region : Write Protect" },
239 	{ 0, 24, 1,  0, "Configure Region : Writable" },
240 	{ 0, 24, 1,  1, "Configure Region : Write Protect" },
241 	{ 0, 25, 1,  0, "OTP strap Region : Writable" },
242 	{ 0, 25, 1,  1, "OTP strap Region : Write Protect" },
243 	{ 0, 26, 1,  0, "Disable Copy Boot Image to Internal SRAM" },
244 	{ 0, 26, 1,  1, "Copy Boot Image to Internal SRAM" },
245 	{ 0, 27, 1,  0, "Disable image encryption" },
246 	{ 0, 27, 1,  1, "Enable image encryption" },
247 	{ 0, 28, 1,  OTP_REG_RESERVED, "" },
248 	{ 0, 29, 1,  0, "OTP key retire Region : Writable" },
249 	{ 0, 29, 1,  1, "OTP key retire Region : Write Protect" },
250 	{ 0, 30, 1,  0, "SIPROM RED_EN redundancy repair disable" },
251 	{ 0, 30, 1,  1, "SIPROM RED_EN redundancy repair enable" },
252 	{ 0, 31, 1,  0, "SIPROM Mlock memory lock disable" },
253 	{ 0, 31, 1,  1, "SIPROM Mlock memory lock enable" },
254 	{ 2, 0,  16, OTP_REG_VALUE, "Vender ID : 0x%x" },
255 	{ 2, 16, 16, OTP_REG_VALUE, "Key Revision : 0x%x" },
256 	{ 3, 0,  16, OTP_REG_VALUE, "Secure boot header offset : 0x%x" },
257 	{ 4, 0,  8,  OTP_REG_VALID_BIT, "Keys valid  : %s" },
258 	{ 4, 16, 8,  OTP_REG_VALID_BIT, "Keys retire  : %s" },
259 	{ 5, 0,  32, OTP_REG_VALUE, "User define data, random number low : 0x%x" },
260 	{ 6, 0,  32, OTP_REG_VALUE, "User define data, random number high : 0x%x" },
261 	{ 7, 0,  1,  0, "Force enable PCI bus to AHB bus bridge" },
262 	{ 7, 0,  1,  1, "Force disable PCI bus to AHB bus bridge" },
263 	{ 7, 1,  1,  0, "Force enable UART5 debug port function" },
264 	{ 7, 1,  1,  1, "Force disable UART5 debug port function" },
265 	{ 7, 2,  1,  0, "Force enable XDMA function" },
266 	{ 7, 2,  1,  1, "Force disable XDMA function" },
267 	{ 7, 3,  1,  0, "Force enable APB to PCIE device bridge" },
268 	{ 7, 3,  1,  1, "Force disable APB to PCIE device bridge" },
269 	{ 7, 4,  1,  0, "Force enable APB to PCIE bridge config access" },
270 	{ 7, 4,  1,  1, "Force disable APB to PCIE bridge config access" },
271 	{ 7, 5,  1,  0, "Force enable PCIE bus trace buffer" },
272 	{ 7, 5,  1,  1, "Force disable PCIE bus trace buffer" },
273 	{ 7, 6,  1,  0, "Force enable the capability for PCIE device port as a Root Complex" },
274 	{ 7, 6,  1,  1, "Force disable the capability for PCIE device port as a Root Complex" },
275 	{ 7, 16, 1,  0, "Force enable ESPI bus to AHB bus bridge" },
276 	{ 7, 16, 1,  1, "Force disable ESPI bus to AHB bus bridge" },
277 	{ 7, 17, 1,  0, "Force enable LPC bus to AHB bus bridge1" },
278 	{ 7, 17, 1,  1, "Force disable LPC bus to AHB bus bridge1" },
279 	{ 7, 18, 1,  0, "Force enable LPC bus to AHB bus bridge2" },
280 	{ 7, 18, 1,  1, "Force disable LPC bus to AHB bus bridge2" },
281 	{ 7, 19, 1,  0, "Force enable UART1 debug port function" },
282 	{ 7, 19, 1,  1, "Force disable UART1 debug port function" },
283 	{ 7, 31, 1,  0, "Disable chip security setting" },
284 	{ 7, 31, 1,  1, "Enable chip security setting" },
285 	{ 8, 0,  32, OTP_REG_VALUE, "Redundancy Repair : 0x%x" },
286 	{ 10, 0, 32, OTP_REG_VALUE, "Manifest ID low : 0x%x" },
287 	{ 11, 0, 32, OTP_REG_VALUE, "Manifest ID high : 0x%x" }
288 };
289 static void otp_read_data(uint32_t offset, uint32_t *data)
290 {
291 	writel(offset, 0x1e6f2010); //Read address
292 	writel(0x23b1e361, 0x1e6f2004); //trigger read
293 	udelay(2);
294 	data[0] = readl(0x1e6f2020);
295 	data[1] = readl(0x1e6f2024);
296 }
297 
298 static void otp_read_config(uint32_t offset, uint32_t *data)
299 {
300 	int config_offset;
301 
302 	config_offset = 0x800;
303 	config_offset |= (offset / 8) * 0x200;
304 	config_offset |= (offset % 8) * 0x2;
305 
306 	writel(config_offset, 0x1e6f2010);  //Read address
307 	writel(0x23b1e361, 0x1e6f2004); //trigger read
308 	udelay(2);
309 	data[0] = readl(0x1e6f2020);
310 }
311 
312 static int otp_print_config(uint32_t offset, int dw_count)
313 {
314 	int i;
315 	uint32_t ret[1];
316 
317 	if (offset + dw_count > 32)
318 		return OTP_USAGE;
319 	for (i = offset; i < offset + dw_count; i ++) {
320 		otp_read_config(i, ret);
321 		printf("OTPCFG%X: %08X\n", i, ret[0]);
322 	}
323 	printf("\n");
324 	return OTP_SUCCESS;
325 }
326 
327 static int otp_print_data(uint32_t offset, int dw_count)
328 {
329 	int i;
330 	uint32_t ret[2];
331 
332 	if (offset + dw_count > 2048 || offset % 4 != 0)
333 		return OTP_USAGE;
334 	for (i = offset; i < offset + dw_count; i += 2) {
335 		otp_read_data(i, ret);
336 		if (i % 4 == 0)
337 			printf("%03X: %08X %08X ", i * 4, ret[0], ret[1]);
338 		else
339 			printf("%08X %08X\n", ret[0], ret[1]);
340 
341 	}
342 	printf("\n");
343 	return OTP_SUCCESS;
344 }
345 
346 static int otp_compare(uint32_t otp_addr, uint32_t addr)
347 {
348 	uint32_t ret;
349 	uint32_t *buf;
350 
351 	buf = map_physmem(addr, 16, MAP_WRBACK);
352 	printf("%08X\n", buf[0]);
353 	printf("%08X\n", buf[1]);
354 	printf("%08X\n", buf[2]);
355 	printf("%08X\n", buf[3]);
356 	writel(otp_addr, 0x1e6f2010); //Compare address
357 	writel(buf[0], 0x1e6f2020); //Compare data 1
358 	writel(buf[1], 0x1e6f2024); //Compare data 2
359 	writel(buf[2], 0x1e6f2028); //Compare data 3
360 	writel(buf[3], 0x1e6f202c); //Compare data 4
361 	writel(0x23b1e363, 0x1e6f2004); //Compare command
362 	udelay(10);
363 	ret = readl(0x1e6f2014); //Compare command
364 	if (ret & 0x1)
365 		return 0;
366 	else
367 		return -1;
368 }
369 
370 static void otp_write(uint32_t otp_addr, uint32_t data)
371 {
372 	writel(otp_addr, 0x1e6f2010); //write address
373 	writel(data, 0x1e6f2020); //write data
374 	writel(0x23b1e362, 0x1e6f2004); //write command
375 	udelay(100);
376 }
377 
378 static void otp_prog(uint32_t otp_addr, uint32_t prog_bit)
379 {
380 	writel(otp_addr, 0x1e6f2010); //write address
381 	writel(prog_bit, 0x1e6f2020); //write data
382 	writel(0x23b1e364, 0x1e6f2004); //write command
383 	udelay(85);
384 }
385 
386 static int verify_bit(uint32_t otp_addr, int bit_offset, int value)
387 {
388 	int ret;
389 
390 	writel(otp_addr, 0x1e6f2010); //Read address
391 	writel(0x23b1e361, 0x1e6f2004); //trigger read
392 	udelay(2);
393 	ret = readl(0x1e6f2020);
394 	// printf("verify_bit = %x\n", ret);
395 	if (((ret >> bit_offset) & 1) == value)
396 		return 0;
397 	else
398 		return -1;
399 }
400 
401 static uint32_t verify_dw(uint32_t otp_addr, uint32_t *value, uint32_t *keep, uint32_t *compare, int size)
402 {
403 	uint32_t ret[2];
404 
405 	otp_addr &= ~(1 << 15);
406 
407 	if (otp_addr % 2 == 0)
408 		writel(otp_addr, 0x1e6f2010); //Read address
409 	else
410 		writel(otp_addr - 1, 0x1e6f2010); //Read address
411 	writel(0x23b1e361, 0x1e6f2004); //trigger read
412 	udelay(2);
413 	ret[0] = readl(0x1e6f2020);
414 	ret[1] = readl(0x1e6f2024);
415 	if (size == 1) {
416 		if (otp_addr % 2 == 0) {
417 			// printf("check %x : %x = %x\n", otp_addr, ret[0], value[0]);
418 			if ((value[0] & ~keep[0]) == (ret[0] & ~keep[0])) {
419 				compare[0] = 0;
420 				return 0;
421 			} else {
422 				compare[0] = value[0] ^ ret[0];
423 				return -1;
424 			}
425 
426 		} else {
427 			// printf("check %x : %x = %x\n", otp_addr, ret[1], value[0]);
428 			if ((value[0] & ~keep[0]) == (ret[1] & ~keep[0])) {
429 				compare[0] = ~0;
430 				return 0;
431 			} else {
432 				compare[0] = ~(value[0] ^ ret[1]);
433 				return -1;
434 			}
435 		}
436 	} else if (size == 2) {
437 		// otp_addr should be even
438 		if ((value[0] & ~keep[0]) == (ret[0] & ~keep[0]) && (value[1] & ~keep[1]) == (ret[1] & ~keep[1])) {
439 			// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
440 			// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
441 			compare[0] = 0;
442 			compare[1] = ~0;
443 			return 0;
444 		} else {
445 			// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
446 			// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
447 			compare[0] = value[0] ^ ret[0];
448 			compare[1] = ~(value[1] ^ ret[1]);
449 			return -1;
450 		}
451 	} else {
452 		return -1;
453 	}
454 }
455 
456 static void otp_soak(int soak)
457 {
458 	if (soak) {
459 		otp_write(0x3000, 0x4021); // Write MRA
460 		otp_write(0x5000, 0x1027); // Write MRB
461 		otp_write(0x1000, 0x4820); // Write MR
462 		writel(0x041930d4, 0x1e602008); //soak program
463 	} else {
464 		otp_write(0x3000, 0x4061); // Write MRA
465 		otp_write(0x5000, 0x302f); // Write MRB
466 		otp_write(0x1000, 0x4020); // Write MR
467 		writel(0x04190760, 0x1e602008); //normal program
468 	}
469 }
470 
471 static void otp_prog_dw(uint32_t value, uint32_t keep, uint32_t prog_address)
472 {
473 	int j, bit_value, prog_bit;
474 
475 	for (j = 0; j < 32; j++) {
476 		if ((keep >> j) & 0x1)
477 			continue;
478 		bit_value = (value >> j) & 0x1;
479 		if (prog_address % 2 == 0) {
480 			if (bit_value)
481 				prog_bit = ~(0x1 << j);
482 			else
483 				continue;
484 		} else {
485 			prog_address |= 1 << 15;
486 			if (bit_value)
487 				continue;
488 			else
489 				prog_bit = 0x1 << j;
490 		}
491 		otp_prog(prog_address, prog_bit);
492 	}
493 }
494 
495 
496 static void otp_strp_status(struct otpstrap_status *otpstrap)
497 {
498 	uint32_t OTPSTRAP_RAW[2];
499 	int i, j;
500 
501 	for (j = 0; j < 64; j++) {
502 		otpstrap[j].value = 0;
503 		otpstrap[j].remain_times = 7;
504 		otpstrap[j].writeable_option = -1;
505 		otpstrap[j].protected = 0;
506 	}
507 
508 	for (i = 16; i < 30; i += 2) {
509 		int option = (i - 16) / 2;
510 		otp_read_config(i, &OTPSTRAP_RAW[0]);
511 		otp_read_config(i + 1, &OTPSTRAP_RAW[1]);
512 		for (j = 0; j < 32; j++) {
513 			char bit_value = ((OTPSTRAP_RAW[0] >> j) & 0x1);
514 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
515 				otpstrap[j].writeable_option = option;
516 			}
517 			if (bit_value == 1)
518 				otpstrap[j].remain_times --;
519 			otpstrap[j].value ^= bit_value;
520 			otpstrap[j].option_array[option] = bit_value;
521 		}
522 		for (j = 32; j < 64; j++) {
523 			char bit_value = ((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1);
524 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
525 				otpstrap[j].writeable_option = option;
526 			}
527 			if (bit_value == 1)
528 				otpstrap[j].remain_times --;
529 			otpstrap[j].value ^= bit_value;
530 			otpstrap[j].option_array[option] = bit_value;
531 		}
532 	}
533 	otp_read_config(30, &OTPSTRAP_RAW[0]);
534 	otp_read_config(31, &OTPSTRAP_RAW[1]);
535 	for (j = 0; j < 32; j++) {
536 		if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
537 			otpstrap[j].protected = 1;
538 	}
539 	for (j = 32; j < 64; j++) {
540 		if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
541 			otpstrap[j].protected = 1;
542 	}
543 }
544 
545 static int otp_print_conf_image(uint32_t *OTPCFG)
546 {
547 	uint32_t *OTPCFG_KEEP = &OTPCFG[12];
548 	uint32_t mask;
549 	uint32_t dw_offset;
550 	uint32_t bit_offset;
551 	uint32_t otp_value;
552 	uint32_t otp_keep;
553 	int fail = 0;
554 	char valid_bit[20];
555 	int i;
556 	int j;
557 
558 	printf("DW    BIT        Value       Description\n");
559 	printf("__________________________________________________________________________\n");
560 	for (i = 0; i < ARRAY_SIZE(a0_conf_info); i++) {
561 		dw_offset = a0_conf_info[i].dw_offset;
562 		bit_offset = a0_conf_info[i].bit_offset;
563 		mask = BIT(a0_conf_info[i].length) - 1;
564 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
565 		otp_keep = (OTPCFG_KEEP[dw_offset] >> bit_offset) & mask;
566 
567 		if (otp_keep == mask) {
568 			continue;
569 		} else if (otp_keep != 0) {
570 			fail = 1;
571 		}
572 
573 		if ((otp_value != a0_conf_info[i].value) &&
574 		    a0_conf_info[i].value != OTP_REG_RESERVED &&
575 		    a0_conf_info[i].value != OTP_REG_VALUE &&
576 		    a0_conf_info[i].value != OTP_REG_VALID_BIT)
577 			continue;
578 		printf("0x%-4X", dw_offset);
579 
580 		if (a0_conf_info[i].length == 1) {
581 			printf("0x%-9X", a0_conf_info[i].bit_offset);
582 		} else {
583 			printf("0x%-2X:0x%-4X",
584 			       a0_conf_info[i].bit_offset + a0_conf_info[i].length - 1,
585 			       a0_conf_info[i].bit_offset);
586 		}
587 		printf("0x%-10x", otp_value);
588 
589 		if (fail) {
590 			printf("Keep mask error\n");
591 		} else {
592 			if (a0_conf_info[i].value == OTP_REG_RESERVED) {
593 				printf("Reserved\n");
594 			} else if (a0_conf_info[i].value == OTP_REG_VALUE) {
595 				printf(a0_conf_info[i].information, otp_value);
596 				printf("\n");
597 			} else if (a0_conf_info[i].value == OTP_REG_VALID_BIT) {
598 				if (otp_value != 0) {
599 					for (j = 0; j < 7; j++) {
600 						if (otp_value == (1 << j)) {
601 							valid_bit[j * 2] = '1';
602 						} else {
603 							valid_bit[j * 2] = '0';
604 						}
605 						valid_bit[j * 2 + 1] = ' ';
606 					}
607 					valid_bit[15] = 0;
608 				} else {
609 					strcpy(valid_bit, "0 0 0 0 0 0 0 0\0");
610 				}
611 				printf(a0_conf_info[i].information, valid_bit);
612 				printf("\n");
613 			} else {
614 				printf("%s\n", a0_conf_info[i].information);
615 			}
616 		}
617 	}
618 
619 	if (fail)
620 		return OTP_FAILURE;
621 
622 	return OTP_SUCCESS;
623 }
624 
625 static int otp_print_conf_info(int input_offset)
626 {
627 	uint32_t OTPCFG[12];
628 	uint32_t mask;
629 	uint32_t dw_offset;
630 	uint32_t bit_offset;
631 	uint32_t otp_value;
632 	char valid_bit[20];
633 	int i;
634 	int j;
635 
636 	for (i = 0; i < 12; i++)
637 		otp_read_config(i, &OTPCFG[i]);
638 
639 
640 	printf("DW    BIT        Value       Description\n");
641 	printf("__________________________________________________________________________\n");
642 	for (i = 0; i < ARRAY_SIZE(a0_conf_info); i++) {
643 		if (input_offset != -1 && input_offset != a0_conf_info[i].dw_offset)
644 			continue;
645 		dw_offset = a0_conf_info[i].dw_offset;
646 		bit_offset = a0_conf_info[i].bit_offset;
647 		mask = BIT(a0_conf_info[i].length) - 1;
648 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
649 
650 		if ((otp_value != a0_conf_info[i].value) &&
651 		    a0_conf_info[i].value != OTP_REG_RESERVED &&
652 		    a0_conf_info[i].value != OTP_REG_VALUE &&
653 		    a0_conf_info[i].value != OTP_REG_VALID_BIT)
654 			continue;
655 		printf("0x%-4X", dw_offset);
656 
657 		if (a0_conf_info[i].length == 1) {
658 			printf("0x%-9X", a0_conf_info[i].bit_offset);
659 		} else {
660 			printf("0x%-2X:0x%-4X",
661 			       a0_conf_info[i].bit_offset + a0_conf_info[i].length - 1,
662 			       a0_conf_info[i].bit_offset);
663 		}
664 		printf("0x%-10x", otp_value);
665 
666 		if (a0_conf_info[i].value == OTP_REG_RESERVED) {
667 			printf("Reserved\n");
668 		} else if (a0_conf_info[i].value == OTP_REG_VALUE) {
669 			printf(a0_conf_info[i].information, otp_value);
670 			printf("\n");
671 		} else if (a0_conf_info[i].value == OTP_REG_VALID_BIT) {
672 			if (otp_value != 0) {
673 				for (j = 0; j < 7; j++) {
674 					if (otp_value == (1 << j)) {
675 						valid_bit[j * 2] = '1';
676 					} else {
677 						valid_bit[j * 2] = '0';
678 					}
679 					valid_bit[j * 2 + 1] = ' ';
680 				}
681 				valid_bit[15] = 0;
682 			} else {
683 				strcpy(valid_bit, "0 0 0 0 0 0 0 0\0");
684 			}
685 			printf(a0_conf_info[i].information, valid_bit);
686 			printf("\n");
687 		} else {
688 			printf("%s\n", a0_conf_info[i].information);
689 		}
690 	}
691 	return OTP_SUCCESS;
692 }
693 
694 static int otp_print_strap_image(uint32_t *OTPSTRAP)
695 {
696 	uint32_t *OTPSTRAP_PRO = &OTPSTRAP[4];
697 	uint32_t *OTPSTRAP_KEEP = &OTPSTRAP[2];
698 	int i;
699 	int fail = 0;
700 	uint32_t bit_offset;
701 	uint32_t dw_offset;
702 	uint32_t mask;
703 	uint32_t otp_value;
704 	uint32_t otp_protect;
705 	uint32_t otp_keep;
706 
707 	printf("BIT(hex)   Value       Protect     Description\n");
708 	printf("__________________________________________________________________________________________\n");
709 
710 	for (i = 0; i < ARRAY_SIZE(a0_strap_info); i++) {
711 		if (a0_strap_info[i].bit_offset > 32) {
712 			dw_offset = 1;
713 			bit_offset = a0_strap_info[i].bit_offset - 32;
714 		} else {
715 			dw_offset = 0;
716 			bit_offset = a0_strap_info[i].bit_offset;
717 		}
718 
719 		mask = BIT(a0_strap_info[i].length) - 1;
720 		otp_value = (OTPSTRAP[dw_offset] >> bit_offset) & mask;
721 		otp_protect = (OTPSTRAP_PRO[dw_offset] >> bit_offset) & mask;
722 		otp_keep = (OTPSTRAP_KEEP[dw_offset] >> bit_offset) & mask;
723 
724 		if (otp_keep == mask) {
725 			continue;
726 		} else if (otp_keep != 0) {
727 			fail = 1;
728 		}
729 
730 		if ((otp_value != a0_strap_info[i].value) &&
731 		    a0_strap_info[i].value != OTP_REG_RESERVED)
732 			continue;
733 
734 		if (a0_strap_info[i].length == 1) {
735 			printf("0x%-9X", a0_strap_info[i].bit_offset);
736 		} else {
737 			printf("0x%-2X:0x%-4X",
738 			       a0_strap_info[i].bit_offset + a0_strap_info[i].length - 1,
739 			       a0_strap_info[i].bit_offset);
740 		}
741 		printf("0x%-10x", otp_value);
742 		printf("0x%-10x", otp_protect);
743 
744 		if (fail) {
745 			printf("Keep mask error\n");
746 		} else {
747 			if (a0_strap_info[i].value != OTP_REG_RESERVED)
748 				printf("%s\n", a0_strap_info[i].information);
749 			else
750 				printf("Reserved\n");
751 		}
752 	}
753 
754 	if (fail)
755 		return OTP_FAILURE;
756 
757 	return OTP_SUCCESS;
758 }
759 
760 static int otp_print_strap_info(int view)
761 {
762 	struct otpstrap_status strap_status[64];
763 	int i, j;
764 	int fail = 0;
765 	uint32_t bit_offset;
766 	uint32_t length;
767 	uint32_t otp_value;
768 	uint32_t otp_protect;
769 
770 	otp_strp_status(strap_status);
771 
772 	if (view) {
773 		// printf("BIT(hex) Value  Option         Protect   Description\n");
774 		// printf("                0 1 2 3 4 5 6\n");
775 		printf("BIT(hex) Value  Remains  Protect   Description\n");
776 		printf("___________________________________________________________________________________________________\n");
777 	} else {
778 		printf("BIT(hex)   Value       Description\n");
779 		printf("________________________________________________________________________________\n");
780 	}
781 	for (i = 0; i < ARRAY_SIZE(a0_strap_info); i++) {
782 		otp_value = 0;
783 		bit_offset = a0_strap_info[i].bit_offset;
784 		length = a0_strap_info[i].length;
785 		for (j = 0; j < length; j++) {
786 			otp_value |= strap_status[bit_offset + j].value << j;
787 			otp_protect |= strap_status[bit_offset + j].protected << j;
788 		}
789 		if ((otp_value != a0_strap_info[i].value) &&
790 		    a0_strap_info[i].value != OTP_REG_RESERVED)
791 			continue;
792 		if (view) {
793 			for (j = 0; j < length; j++) {
794 				printf("0x%-7X", a0_strap_info[i].bit_offset + j);
795 				printf("0x%-5X", strap_status[bit_offset + j].value);
796 				printf("%-9d", strap_status[bit_offset + j].remain_times);
797 				printf("0x%-7X", strap_status[bit_offset].protected);
798 				if (a0_strap_info[i].value == OTP_REG_RESERVED) {
799 					printf(" Reserved\n");
800 					continue;
801 				}
802 				if (length == 1) {
803 					printf(" %s\n", a0_strap_info[i].information);
804 					continue;
805 				}
806 
807 				if (j == 0)
808 					printf("/%s\n", a0_strap_info[i].information);
809 				else if (j == length - 1)
810 					printf("\\ \"\n");
811 				else
812 					printf("| \"\n");
813 			}
814 		} else {
815 			if (length == 1) {
816 				printf("0x%-9X", a0_strap_info[i].bit_offset);
817 			} else {
818 				printf("0x%-2X:0x%-4X",
819 				       bit_offset + length - 1, bit_offset);
820 			}
821 
822 			printf("0x%-10X", otp_value);
823 
824 			if (a0_strap_info[i].value != OTP_REG_RESERVED)
825 				printf("%s\n", a0_strap_info[i].information);
826 			else
827 				printf("Reserved\n");
828 		}
829 	}
830 
831 	if (fail)
832 		return OTP_FAILURE;
833 
834 	return OTP_SUCCESS;
835 }
836 
837 static void buf_print(char *buf, int len)
838 {
839 	int i;
840 	printf("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
841 	for (i = 0; i < len; i++) {
842 		if (i % 16 == 0) {
843 			printf("%04X: ", i);
844 		}
845 		printf("%02X ", buf[i]);
846 		if ((i + 1) % 16 == 0) {
847 			printf("\n");
848 		}
849 	}
850 }
851 
852 static int otp_print_data_info(uint32_t *buf)
853 {
854 	int key_id, key_offset, last, key_type, key_length, exp_length;
855 	char *byte_buf;
856 	int i = 0, len = 0;
857 	byte_buf = (char *)buf;
858 	while (1) {
859 		key_id = buf[i] & 0x7;
860 		key_offset = buf[i] & 0x1ff8;
861 		last = (buf[i] >> 13) & 1;
862 		key_type = (buf[i] >> 14) & 0xf;
863 		key_length = (buf[i] >> 18) & 0x3;
864 		exp_length = (buf[i] >> 20) & 0xfff;
865 		printf("\nKey[%d]:\n", i);
866 		printf("Key Type: ");
867 		switch (key_type) {
868 		case 0:
869 			printf("AES-256 as OEM platform key for image encryption/decryption\n");
870 			break;
871 		case 1:
872 			printf("AES-256 as secret vault key\n");
873 			break;
874 		case 4:
875 			printf("HMAC as encrypted OEM HMAC keys in Mode 1\n");
876 			break;
877 		case 8:
878 			printf("RSA-public as OEM DSS public keys in Mode 2\n");
879 			break;
880 		case 9:
881 			printf("RSA-public as SOC public key\n");
882 			break;
883 		case 10:
884 			printf("RSA-public as AES key decryption key\n");
885 			break;
886 		case 13:
887 			printf("RSA-private as SOC private key\n");
888 			break;
889 		case 14:
890 			printf("RSA-private as AES key decryption key\n");
891 			break;
892 		default:
893 			printf("key_type error: %x\n", key_type);
894 			return -1;
895 		}
896 		if (key_type == 4) {
897 			printf("HMAC SHA Type: ");
898 			switch (key_length) {
899 			case 0:
900 				printf("HMAC(SHA224)\n");
901 				break;
902 			case 1:
903 				printf("HMAC(SHA256)\n");
904 				break;
905 			case 2:
906 				printf("HMAC(SHA384)\n");
907 				break;
908 			case 3:
909 				printf("HMAC(SHA512)\n");
910 				break;
911 			}
912 		} else if (key_type != 0 && key_type != 1) {
913 			printf("RSA SHA Type: ");
914 			switch (key_length) {
915 			case 0:
916 				printf("RSA1024\n");
917 				len = 0x100;
918 				break;
919 			case 1:
920 				printf("RSA2048\n");
921 				len = 0x200;
922 				break;
923 			case 2:
924 				printf("RSA3072\n");
925 				len = 0x300;
926 				break;
927 			case 3:
928 				printf("RSA4096\n");
929 				len = 0x400;
930 				break;
931 			}
932 			printf("RSA exponent bit length: %d\n", exp_length);
933 		}
934 		if (key_type == 4 || key_type == 8)
935 			printf("Key Number ID: %d\n", key_id);
936 		printf("Key Value:\n");
937 		if (key_type == 4) {
938 			buf_print(&byte_buf[key_offset], 0x40);
939 		} else if (key_type == 0 || key_type == 1) {
940 			printf("AES Key:\n");
941 			buf_print(&byte_buf[key_offset], 0x20);
942 			printf("AES IV:\n");
943 			buf_print(&byte_buf[key_offset + 0x20], 0x10);
944 
945 		} else {
946 			printf("RSA mod:\n");
947 			buf_print(&byte_buf[key_offset], len / 2);
948 			printf("RSA exp:\n");
949 			buf_print(&byte_buf[key_offset + (len / 2)], len / 2);
950 		}
951 		if (last)
952 			break;
953 		i++;
954 	}
955 	return 0;
956 }
957 
958 static int otp_prog_conf(uint32_t *buf)
959 {
960 	int i, k;
961 	int pass = 0;
962 	int soak = 0;
963 	uint32_t prog_address;
964 	uint32_t data[12];
965 	uint32_t compare[2];
966 	uint32_t *buf_keep = &buf[12];
967 	uint32_t data_masked;
968 	uint32_t buf_masked;
969 
970 	printf("Read OTP Config Region:\n");
971 
972 	printProgress(0, 12, "");
973 	for (i = 0; i < 12 ; i ++) {
974 		printProgress(i + 1, 12, "");
975 		prog_address = 0x800;
976 		prog_address |= (i / 8) * 0x200;
977 		prog_address |= (i % 8) * 0x2;
978 		otp_read_data(prog_address, &data[i]);
979 	}
980 
981 	printf("Check writable...\n");
982 	for (i = 0; i < 12; i++) {
983 		data_masked = data[i]  & ~buf_keep[i];
984 		buf_masked  = buf[i] & ~buf_keep[i];
985 		if (data_masked == buf_masked)
986 			continue;
987 		if ((data_masked | buf_masked) == buf_masked) {
988 			continue;
989 		} else {
990 			printf("Input image can't program into OTP, please check.\n");
991 			printf("OTPCFG[%X] = %x\n", i, data[i]);
992 			printf("Input [%X] = %x\n", i, buf[i]);
993 			printf("Mask  [%X] = %x\n", i, ~buf_keep[i]);
994 			return OTP_FAILURE;
995 		}
996 	}
997 
998 	printf("Start Programing...\n");
999 	printProgress(0, 12, "");
1000 	otp_soak(0);
1001 	for (i = 0; i < 12; i++) {
1002 		data_masked = data[i]  & ~buf_keep[i];
1003 		buf_masked  = buf[i] & ~buf_keep[i];
1004 		prog_address = 0x800;
1005 		prog_address |= (i / 8) * 0x200;
1006 		prog_address |= (i % 8) * 0x2;
1007 		if (data_masked == buf_masked) {
1008 			printProgress(i + 1, 12, "[%03X]=%08X HIT", prog_address, buf[i]);
1009 			continue;
1010 		}
1011 		if (soak) {
1012 			soak = 0;
1013 			otp_soak(0);
1014 		}
1015 		printProgress(i + 1, 12, "[%03X]=%08X    ", prog_address, buf[i]);
1016 
1017 		otp_prog_dw(buf[i], buf_keep[i], prog_address);
1018 
1019 		pass = 0;
1020 		for (k = 0; k < RETRY; k++) {
1021 			if (verify_dw(prog_address, &buf[i], &buf_keep[i], compare, 1) != 0) {
1022 				if (soak == 0) {
1023 					soak = 1;
1024 					otp_soak(1);
1025 				}
1026 				otp_prog_dw(compare[0], prog_address, 1);
1027 			} else {
1028 				pass = 1;
1029 				break;
1030 			}
1031 		}
1032 	}
1033 
1034 	if (!pass)
1035 		return OTP_FAILURE;
1036 
1037 	return OTP_SUCCESS;
1038 
1039 }
1040 
1041 
1042 static int otp_strap_image_confirm(uint32_t *buf)
1043 {
1044 	int i;
1045 	uint32_t *strap_keep = buf + 2;
1046 	uint32_t *strap_protect = buf + 4;
1047 	int bit, pbit, kbit;
1048 	int fail = 0;
1049 	int skip = -1;
1050 	struct otpstrap_status otpstrap[64];
1051 
1052 	otp_strp_status(otpstrap);
1053 	for (i = 0; i < 64; i++) {
1054 		if (i < 32) {
1055 			bit = (buf[0] >> i) & 0x1;
1056 			kbit = (strap_keep[0] >> i) & 0x1;
1057 			pbit = (strap_protect[0] >> i) & 0x1;
1058 		} else {
1059 			bit = (buf[1] >> (i - 32)) & 0x1;
1060 			kbit = (strap_keep[1] >> (i - 32)) & 0x1;
1061 			pbit = (strap_protect[1] >> (i - 32)) & 0x1;
1062 		}
1063 
1064 		if (kbit == 1) {
1065 			continue;
1066 		} else {
1067 			printf("OTPSTRAP[%X]:\n", i);
1068 		}
1069 		if (bit == otpstrap[i].value) {
1070 			printf("    The value is same as before, skip it.\n");
1071 			if (skip == -1)
1072 				skip = 1;
1073 			continue;
1074 		} else {
1075 			skip = 0;
1076 		}
1077 		if (otpstrap[i].protected == 1) {
1078 			printf("    This bit is protected and is not writable\n");
1079 			fail = 1;
1080 			continue;
1081 		}
1082 		if (otpstrap[i].remain_times == 0) {
1083 			printf("    This bit is no remaining times to write.\n");
1084 			fail = 1;
1085 			continue;
1086 		}
1087 		if (pbit == 1) {
1088 			printf("    This bit will be protected and become non-writable.\n");
1089 		}
1090 		printf("    Write 1 to OTPSTRAP[%X] OPTION[%X], that value becomes from %d to %d.\n", i, otpstrap[i].writeable_option + 1, otpstrap[i].value, otpstrap[i].value ^ 1);
1091 	}
1092 	if (fail == 1)
1093 		return OTP_FAILURE;
1094 	else if (skip == 1)
1095 		return OTP_PROG_SKIP;
1096 
1097 	return 0;
1098 }
1099 
1100 static int otp_print_strap(int start, int count)
1101 {
1102 	int i, j;
1103 	struct otpstrap_status otpstrap[64];
1104 
1105 	if (start < 0 || start > 64)
1106 		return OTP_USAGE;
1107 
1108 	if ((start + count) < 0 || (start + count) > 64)
1109 		return OTP_USAGE;
1110 
1111 	otp_strp_status(otpstrap);
1112 
1113 	printf("BIT(hex)  Value  Option           Status\n");
1114 	printf("___________________________________________________________________________\n");
1115 
1116 	for (i = start; i < start + count; i++) {
1117 		printf("0x%-8X", i);
1118 		printf("%-7d", otpstrap[i].value);
1119 		for (j = 0; j < 7; j++)
1120 			printf("%d ", otpstrap[i].option_array[j]);
1121 		printf("   ");
1122 		if (otpstrap[i].protected == 1) {
1123 			printf("protected and not writable");
1124 		} else {
1125 			printf("not protected ");
1126 			if (otpstrap[i].remain_times == 0) {
1127 				printf("and no remaining times to write.");
1128 			} else {
1129 				printf("and still can write %d times", otpstrap[i].remain_times);
1130 			}
1131 		}
1132 		printf("\n");
1133 	}
1134 
1135 	return OTP_SUCCESS;
1136 }
1137 
1138 static int otp_prog_strap(uint32_t *buf)
1139 {
1140 	int i, j;
1141 	uint32_t *strap_keep = buf + 2;
1142 	uint32_t *strap_protect = buf + 4;
1143 	uint32_t prog_bit, prog_address;
1144 	int bit, pbit, kbit, offset;
1145 	int fail = 0;
1146 	int pass = 0;
1147 	int soak = 0;
1148 	struct otpstrap_status otpstrap[64];
1149 
1150 	printf("Read OTP Strap Region:\n");
1151 	otp_strp_status(otpstrap);
1152 
1153 	printf("Check writable...\n");
1154 	if (otp_strap_image_confirm(buf) == OTP_FAILURE) {
1155 		printf("Input image can't program into OTP, please check.\n");
1156 		return OTP_FAILURE;
1157 	}
1158 
1159 	otp_soak(0);
1160 	for (i = 0; i < 64; i++) {
1161 		printProgress(i + 1, 64, "");
1162 		prog_address = 0x800;
1163 		if (i < 32) {
1164 			offset = i;
1165 			bit = (buf[0] >> offset) & 0x1;
1166 			kbit = (strap_keep[0] >> offset) & 0x1;
1167 			pbit = (strap_protect[0] >> offset) & 0x1;
1168 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200;
1169 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2;
1170 
1171 		} else {
1172 			offset = (i - 32);
1173 			bit = (buf[1] >> offset) & 0x1;
1174 			kbit = (strap_keep[1] >> offset) & 0x1;
1175 			pbit = (strap_protect[1] >> offset) & 0x1;
1176 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200;
1177 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2;
1178 		}
1179 		prog_bit = ~(0x1 << offset);
1180 
1181 		if (kbit == 1) {
1182 			continue;
1183 		}
1184 		if (bit == otpstrap[i].value) {
1185 			continue;
1186 		}
1187 		if (otpstrap[i].protected == 1) {
1188 			fail = 1;
1189 			continue;
1190 		}
1191 		if (otpstrap[i].remain_times == 0) {
1192 			fail = 1;
1193 			continue;
1194 		}
1195 
1196 		if (soak) {
1197 			soak = 0;
1198 			otp_soak(0);
1199 		}
1200 
1201 		otp_prog(prog_address, prog_bit);
1202 
1203 		pass = 0;
1204 
1205 		for (j = 0; j < RETRY; j++) {
1206 			if (verify_bit(prog_address, offset, 1) == 0) {
1207 				pass = 1;
1208 				break;
1209 			}
1210 			if (soak == 0) {
1211 				soak = 1;
1212 				otp_soak(1);
1213 			}
1214 			otp_prog(prog_address, prog_bit);
1215 		}
1216 		if (!pass)
1217 			return OTP_FAILURE;
1218 
1219 		if (pbit == 0)
1220 			continue;
1221 		prog_address = 0x800;
1222 		if (i < 32)
1223 			prog_address |= 0x60c;
1224 		else
1225 			prog_address |= 0x60e;
1226 
1227 
1228 		if (soak) {
1229 			soak = 0;
1230 			otp_soak(0);
1231 		}
1232 
1233 		otp_prog(prog_address, prog_bit);
1234 
1235 		pass = 0;
1236 
1237 		for (j = 0; j < RETRY; j++) {
1238 
1239 			if (verify_bit(prog_address, offset, 1) == 0) {
1240 				pass = 1;
1241 				break;
1242 			}
1243 			if (soak == 0) {
1244 				soak = 1;
1245 				otp_soak(1);
1246 			}
1247 			otp_prog(prog_address, prog_bit);
1248 		}
1249 		if (!pass)
1250 			return OTP_FAILURE;
1251 
1252 	}
1253 	if (fail == 1)
1254 		return OTP_FAILURE;
1255 	else
1256 		return OTP_SUCCESS;
1257 
1258 }
1259 
1260 static void otp_prog_bit(uint32_t value, uint32_t prog_address, uint32_t bit_offset, int soak)
1261 {
1262 	int prog_bit;
1263 
1264 	otp_soak(soak);
1265 
1266 	if (prog_address % 2 == 0) {
1267 		if (value)
1268 			prog_bit = ~(0x1 << bit_offset);
1269 		else
1270 			return;
1271 	} else {
1272 		prog_address |= 1 << 15;
1273 		if (!value)
1274 			prog_bit = 0x1 << bit_offset;
1275 		else
1276 			return;
1277 	}
1278 	otp_prog(prog_address, prog_bit);
1279 }
1280 
1281 static int otp_prog_data(uint32_t *buf)
1282 {
1283 	int i, k;
1284 	int pass;
1285 	int soak = 0;
1286 	uint32_t prog_address;
1287 	uint32_t data[2048];
1288 	uint32_t compare[2];
1289 	uint32_t *buf_keep = &buf[2048];
1290 
1291 	uint32_t data0_masked;
1292 	uint32_t data1_masked;
1293 	uint32_t buf0_masked;
1294 	uint32_t buf1_masked;
1295 
1296 	printf("Read OTP Data:\n");
1297 
1298 	printProgress(0, 2048, "");
1299 	for (i = 0; i < 2048 ; i += 2) {
1300 		printProgress(i + 2, 2048, "");
1301 		otp_read_data(i, &data[i]);
1302 	}
1303 
1304 
1305 	printf("Check writable...\n");
1306 	for (i = 0; i < 2048; i++) {
1307 		data0_masked = data[i]  & ~buf_keep[i];
1308 		buf0_masked  = buf[i] & ~buf_keep[i];
1309 		if (data0_masked == buf0_masked)
1310 			continue;
1311 		if (i % 2 == 0) {
1312 			if ((data0_masked | buf0_masked) == buf0_masked) {
1313 				continue;
1314 			} else {
1315 				printf("Input image can't program into OTP, please check.\n");
1316 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
1317 				printf("Input   [%x] = %x\n", i, buf[i]);
1318 				printf("Mask    [%x] = %x\n", i, ~buf_keep[i]);
1319 				return OTP_FAILURE;
1320 			}
1321 		} else {
1322 			if ((data0_masked & buf0_masked) == buf0_masked) {
1323 				continue;
1324 			} else {
1325 				printf("Input image can't program into OTP, please check.\n");
1326 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
1327 				printf("Input   [%x] = %x\n", i, buf[i]);
1328 				printf("Mask    [%x] = %x\n", i, ~buf_keep[i]);
1329 				return OTP_FAILURE;
1330 			}
1331 		}
1332 	}
1333 
1334 	printf("Start Programing...\n");
1335 	printProgress(0, 2048, "");
1336 
1337 	for (i = 0; i < 2048; i += 2) {
1338 		prog_address = i;
1339 		data0_masked = data[i]  & ~buf_keep[i];
1340 		buf0_masked  = buf[i] & ~buf_keep[i];
1341 		data1_masked = data[i + 1]  & ~buf_keep[i + 1];
1342 		buf1_masked  = buf[i + 1] & ~buf_keep[i + 1];
1343 		if ((data0_masked == buf0_masked) && (data1_masked == buf1_masked)) {
1344 			printProgress(i + 2, 2048, "[%03X]=%08X HIT;[%03X]=%08X HIT", prog_address, buf[i], prog_address + 1, buf[i + 1]);
1345 			continue;
1346 		}
1347 		if (soak) {
1348 			soak = 0;
1349 			otp_soak(0);
1350 		}
1351 		if (data1_masked == buf1_masked) {
1352 			printProgress(i + 2, 2048, "[%03X]=%08X    ;[%03X]=%08X HIT", prog_address, buf[i], prog_address + 1, buf[i + 1]);
1353 			otp_prog_dw(buf[i], buf_keep[i], prog_address);
1354 		} else if (data0_masked == buf0_masked) {
1355 			printProgress(i + 2, 2048, "[%03X]=%08X HIT;[%03X]=%08X    ", prog_address, buf[i], prog_address + 1, buf[i + 1]);
1356 			otp_prog_dw(buf[i + 1], buf_keep[i + 1], prog_address + 1);
1357 		} else {
1358 			printProgress(i + 2, 2048, "[%03X]=%08X    ;[%03X]=%08X    ", prog_address, buf[i], prog_address + 1, buf[i + 1]);
1359 			otp_prog_dw(buf[i], buf_keep[i], prog_address);
1360 			otp_prog_dw(buf[i + 1], buf_keep[i + 1], prog_address + 1);
1361 		}
1362 
1363 		pass = 0;
1364 		for (k = 0; k < RETRY; k++) {
1365 			if (verify_dw(prog_address, &buf[i], &buf_keep[i], compare, 2) != 0) {
1366 				if (soak == 0) {
1367 					soak = 1;
1368 					otp_soak(1);
1369 				}
1370 				if (compare[0] != 0) {
1371 					otp_prog_dw(compare[0], buf_keep[i], prog_address);
1372 				}
1373 				if (compare[1] != ~0) {
1374 					otp_prog_dw(compare[1], buf_keep[i], prog_address + 1);
1375 				}
1376 			} else {
1377 				pass = 1;
1378 				break;
1379 			}
1380 		}
1381 
1382 		if (!pass)
1383 			return OTP_FAILURE;
1384 	}
1385 	return OTP_SUCCESS;
1386 
1387 }
1388 
1389 static int do_otp_prog(int addr, int byte_size, int nconfirm)
1390 {
1391 	int ret;
1392 	int mode = 0;
1393 	uint32_t *buf;
1394 	uint32_t *data_region = NULL;
1395 	uint32_t *conf_region = NULL;
1396 	uint32_t *strap_region = NULL;
1397 
1398 	buf = map_physmem(addr, byte_size, MAP_WRBACK);
1399 	if (!buf) {
1400 		puts("Failed to map physical memory\n");
1401 		return OTP_FAILURE;
1402 	}
1403 
1404 	if (buf[0] & BIT(29)) {
1405 		mode |= OTP_REGION_DATA;
1406 		data_region = &buf[36];
1407 	}
1408 	if (buf[0] & BIT(30)) {
1409 		mode |= OTP_REGION_CONF;
1410 		conf_region = &buf[12];
1411 	}
1412 	if (buf[0] & BIT(31)) {
1413 		mode |= OTP_REGION_STRAP;
1414 		strap_region = &buf[4];
1415 	}
1416 
1417 	if (!nconfirm) {
1418 		if (mode & OTP_REGION_DATA) {
1419 			printf("\nOTP data region :\n");
1420 			if (otp_print_data_info(data_region) < 0) {
1421 				printf("OTP data error, please check.\n");
1422 				return OTP_FAILURE;
1423 			}
1424 		}
1425 		if (mode & OTP_REGION_STRAP) {
1426 			printf("\nOTP strap region :\n");
1427 			if (otp_print_strap_image(strap_region) < 0) {
1428 				printf("OTP strap error, please check.\n");
1429 				return OTP_FAILURE;
1430 			}
1431 		}
1432 		if (mode & OTP_REGION_CONF) {
1433 			printf("\nOTP configuration region :\n");
1434 			if (otp_print_conf_image(conf_region) < 0) {
1435 				printf("OTP config error, please check.\n");
1436 				return OTP_FAILURE;
1437 			}
1438 		}
1439 
1440 		printf("type \"YES\" (no quotes) to continue:\n");
1441 		if (!confirm_yesno()) {
1442 			printf(" Aborting\n");
1443 			return OTP_FAILURE;
1444 		}
1445 	}
1446 
1447 	if (mode & OTP_REGION_DATA) {
1448 		printf("programing data region ...\n");
1449 		ret = otp_prog_data(data_region);
1450 		if (ret != 0) {
1451 			printf("Error\n");
1452 			return ret;
1453 		} else {
1454 			printf("Done\n");
1455 		}
1456 	}
1457 	if (mode & OTP_REGION_STRAP) {
1458 		printf("programing strap region ...\n");
1459 		ret = otp_prog_strap(strap_region);
1460 		if (ret != 0) {
1461 			printf("Error\n");
1462 			return ret;
1463 		} else {
1464 			printf("Done\n");
1465 		}
1466 	}
1467 	if (mode & OTP_REGION_CONF) {
1468 		printf("programing configuration region ...\n");
1469 		ret = otp_prog_conf(conf_region);
1470 		if (ret != 0) {
1471 			printf("Error\n");
1472 			return ret;
1473 		}
1474 		printf("Done\n");
1475 	}
1476 
1477 	return OTP_SUCCESS;
1478 }
1479 
1480 static int do_otp_prog_bit(int mode, int otp_dw_offset, int bit_offset, int value, int nconfirm)
1481 {
1482 	uint32_t read[2];
1483 	uint32_t strap_buf[6];
1484 	uint32_t prog_address = 0;
1485 	struct otpstrap_status otpstrap[64];
1486 	int otp_bit;
1487 	int i;
1488 	int pass;
1489 	int ret;
1490 
1491 	switch (mode) {
1492 	case OTP_REGION_CONF:
1493 		otp_read_config(otp_dw_offset, read);
1494 		prog_address = 0x800;
1495 		prog_address |= (otp_dw_offset / 8) * 0x200;
1496 		prog_address |= (otp_dw_offset % 8) * 0x2;
1497 		otp_bit = (read[0] >> bit_offset) & 0x1;
1498 		if (otp_bit == value) {
1499 			printf("OTPCFG%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
1500 			printf("No need to program\n");
1501 			return OTP_SUCCESS;
1502 		}
1503 		if (otp_bit == 1 && value == 0) {
1504 			printf("OTPCFG%X[%X] = 1\n", otp_dw_offset, bit_offset);
1505 			printf("OTP is programed, which can't be clean\n");
1506 			return OTP_FAILURE;
1507 		}
1508 		printf("Program OTPCFG%X[%X] to 1\n", otp_dw_offset, bit_offset);
1509 		break;
1510 	case OTP_REGION_DATA:
1511 		prog_address = otp_dw_offset;
1512 
1513 		if (otp_dw_offset % 2 == 0) {
1514 			otp_read_data(otp_dw_offset, read);
1515 			otp_bit = (read[0] >> bit_offset) & 0x1;
1516 		} else {
1517 			otp_read_data(otp_dw_offset - 1, read);
1518 			otp_bit = (read[1] >> bit_offset) & 0x1;
1519 		}
1520 		if (otp_bit == value) {
1521 			printf("OTPDATA%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
1522 			printf("No need to program\n");
1523 			return OTP_SUCCESS;
1524 		}
1525 		if (otp_bit == 1 && value == 0) {
1526 			printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
1527 			printf("OTP is programed, which can't be clean\n");
1528 			return OTP_FAILURE;
1529 		}
1530 		printf("Program OTPDATA%X[%X] to 1\n", otp_dw_offset, bit_offset);
1531 		break;
1532 	case OTP_REGION_STRAP:
1533 		otp_strp_status(otpstrap);
1534 		otp_print_strap(bit_offset, 1);
1535 		if (bit_offset < 32) {
1536 			strap_buf[0] = value << bit_offset;
1537 			strap_buf[2] = ~BIT(bit_offset);
1538 			strap_buf[3] = ~0;
1539 			strap_buf[5] = 0;
1540 			// if (protect)
1541 			// 	strap_buf[4] = BIT(bit_offset);
1542 			// else
1543 			// 	strap_buf[4] = 0;
1544 		} else {
1545 			strap_buf[1] = value << (bit_offset - 32);
1546 			strap_buf[2] = ~0;
1547 			strap_buf[3] = ~BIT(bit_offset - 32);
1548 			strap_buf[4] = 0;
1549 			// if (protect)
1550 			// 	strap_buf[5] = BIT(bit_offset - 32);
1551 			// else
1552 			// 	strap_buf[5] = 0;
1553 		}
1554 		ret = otp_strap_image_confirm(strap_buf);
1555 		if (ret == OTP_FAILURE)
1556 			return OTP_FAILURE;
1557 		else if (ret == OTP_PROG_SKIP)
1558 			return OTP_SUCCESS;
1559 
1560 		break;
1561 	}
1562 
1563 	if (!nconfirm) {
1564 		printf("type \"YES\" (no quotes) to continue:\n");
1565 		if (!confirm_yesno()) {
1566 			printf(" Aborting\n");
1567 			return OTP_FAILURE;
1568 		}
1569 	}
1570 
1571 	switch (mode) {
1572 	case OTP_REGION_STRAP:
1573 		return otp_prog_strap(strap_buf);
1574 	case OTP_REGION_CONF:
1575 	case OTP_REGION_DATA:
1576 		otp_prog_bit(value, prog_address, bit_offset, 0);
1577 		pass = -1;
1578 		for (i = 0; i < RETRY; i++) {
1579 			if (verify_bit(prog_address, bit_offset, value) != 0) {
1580 				otp_prog_bit(value, prog_address, bit_offset, 1);
1581 			} else {
1582 				pass = 0;
1583 				break;
1584 			}
1585 		}
1586 		if (pass == 0)
1587 			return OTP_SUCCESS;
1588 	}
1589 
1590 	return OTP_USAGE;
1591 }
1592 
1593 static int do_otpread(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1594 {
1595 	uint32_t offset, count;
1596 	int ret;
1597 
1598 	if (argc == 4) {
1599 		offset = simple_strtoul(argv[2], NULL, 16);
1600 		count = simple_strtoul(argv[3], NULL, 16);
1601 	} else if (argc == 3) {
1602 		offset = simple_strtoul(argv[2], NULL, 16);
1603 		count = 1;
1604 	} else {
1605 		return CMD_RET_USAGE;
1606 	}
1607 
1608 
1609 	if (!strcmp(argv[1], "conf")) {
1610 		writel(OTP_PASSWD, 0x1e6f2000); //password
1611 		ret = otp_print_config(offset, count);
1612 	} else if (!strcmp(argv[1], "data")) {
1613 		writel(OTP_PASSWD, 0x1e6f2000); //password
1614 		ret = otp_print_data(offset, count);
1615 	} else if (!strcmp(argv[1], "strap")) {
1616 		writel(OTP_PASSWD, 0x1e6f2000); //password
1617 		ret = otp_print_strap(offset, count);
1618 	} else {
1619 		return CMD_RET_USAGE;
1620 	}
1621 
1622 	if (ret == OTP_SUCCESS)
1623 		return CMD_RET_SUCCESS;
1624 	else
1625 		return CMD_RET_USAGE;
1626 
1627 }
1628 
1629 static int do_otpprog(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1630 {
1631 	phys_addr_t addr;
1632 	uint32_t byte_size;
1633 	int ret;
1634 
1635 	if (argc == 4) {
1636 		if (strcmp(argv[1], "f"))
1637 			return CMD_RET_USAGE;
1638 		addr = simple_strtoul(argv[2], NULL, 16);
1639 		byte_size = simple_strtoul(argv[3], NULL, 16);
1640 		writel(OTP_PASSWD, 0x1e6f2000); //password
1641 		ret = do_otp_prog(addr, byte_size, 1);
1642 	} else if (argc == 3) {
1643 		addr = simple_strtoul(argv[1], NULL, 16);
1644 		byte_size = simple_strtoul(argv[2], NULL, 16);
1645 		writel(OTP_PASSWD, 0x1e6f2000); //password
1646 		ret = do_otp_prog(addr, byte_size, 0);
1647 	} else {
1648 		return CMD_RET_USAGE;
1649 	}
1650 
1651 	if (ret == OTP_SUCCESS)
1652 		return CMD_RET_SUCCESS;
1653 	else if (ret == OTP_FAILURE)
1654 		return CMD_RET_FAILURE;
1655 	else
1656 		return CMD_RET_USAGE;
1657 }
1658 
1659 static int do_otppb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1660 {
1661 	int mode = 0;
1662 	int nconfirm = 0;
1663 	int otp_addr = 0;
1664 	int bit_offset;
1665 	int value;
1666 	int ret;
1667 
1668 	if (argc != 4 && argc != 5 && argc != 6)
1669 		return CMD_RET_USAGE;
1670 
1671 	/* Drop the pb cmd */
1672 	argc--;
1673 	argv++;
1674 
1675 	if (!strcmp(argv[0], "conf"))
1676 		mode = OTP_REGION_CONF;
1677 	else if (!strcmp(argv[0], "strap"))
1678 		mode = OTP_REGION_STRAP;
1679 	else if (!strcmp(argv[0], "data"))
1680 		mode = OTP_REGION_DATA;
1681 	else
1682 		return CMD_RET_USAGE;
1683 
1684 	/* Drop the region cmd */
1685 	argc--;
1686 	argv++;
1687 
1688 	if (!strcmp(argv[0], "f")) {
1689 		nconfirm = 1;
1690 		/* Drop the force option */
1691 		argc--;
1692 		argv++;
1693 	}
1694 
1695 	if (mode == OTP_REGION_STRAP) {
1696 		bit_offset = simple_strtoul(argv[0], NULL, 16);
1697 		value = simple_strtoul(argv[1], NULL, 16);
1698 		if (bit_offset >= 64)
1699 			return CMD_RET_USAGE;
1700 	} else {
1701 		otp_addr = simple_strtoul(argv[0], NULL, 16);
1702 		bit_offset = simple_strtoul(argv[1], NULL, 16);
1703 		value = simple_strtoul(argv[2], NULL, 16);
1704 		if (bit_offset >= 32)
1705 			return CMD_RET_USAGE;
1706 	}
1707 	if (value != 0 && value != 1)
1708 		return CMD_RET_USAGE;
1709 
1710 	writel(OTP_PASSWD, 0x1e6f2000); //password
1711 	ret = do_otp_prog_bit(mode, otp_addr, bit_offset, value, nconfirm);
1712 
1713 	if (ret == OTP_SUCCESS)
1714 		return CMD_RET_SUCCESS;
1715 	else if (ret == OTP_FAILURE)
1716 		return CMD_RET_FAILURE;
1717 	else
1718 		return CMD_RET_USAGE;
1719 }
1720 
1721 static int do_otpcmp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1722 {
1723 	phys_addr_t addr;
1724 	int otp_addr = 0;
1725 
1726 	if (argc != 3)
1727 		return CMD_RET_USAGE;
1728 
1729 	writel(OTP_PASSWD, 0x1e6f2000); //password
1730 	addr = simple_strtoul(argv[1], NULL, 16);
1731 	otp_addr = simple_strtoul(argv[2], NULL, 16);
1732 	if (otp_compare(otp_addr, addr) == 0) {
1733 		printf("Compare pass\n");
1734 		return CMD_RET_SUCCESS;
1735 	} else {
1736 		printf("Compare fail\n");
1737 		return CMD_RET_FAILURE;
1738 	}
1739 }
1740 
1741 static int do_otpinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1742 {
1743 	int view = 0;
1744 	int input;
1745 
1746 	if (argc != 2 && argc != 3)
1747 		return CMD_RET_USAGE;
1748 
1749 	if (!strcmp(argv[1], "conf")) {
1750 
1751 		writel(OTP_PASSWD, 0x1e6f2000); //password
1752 		if (argc == 3) {
1753 			input = simple_strtoul(argv[2], NULL, 16);
1754 			otp_print_conf_info(input);
1755 		} else {
1756 			otp_print_conf_info(-1);
1757 		}
1758 	} else if (!strcmp(argv[1], "strap")) {
1759 		if (!strcmp(argv[2], "v")) {
1760 			view = 1;
1761 			/* Drop the view option */
1762 			argc--;
1763 			argv++;
1764 		}
1765 		writel(OTP_PASSWD, 0x1e6f2000); //password
1766 		otp_print_strap_info(view);
1767 	} else {
1768 		return CMD_RET_USAGE;
1769 	}
1770 
1771 	return CMD_RET_SUCCESS;
1772 }
1773 
1774 static int do_otpprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1775 {
1776 	int input;
1777 	int bit_offset;
1778 	int prog_address;
1779 	int pass;
1780 	int i;
1781 	if (argc != 3 && argc != 2)
1782 		return CMD_RET_USAGE;
1783 
1784 	if (!strcmp(argv[0], "f")) {
1785 		input = simple_strtoul(argv[2], NULL, 16);
1786 	} else {
1787 		input = simple_strtoul(argv[1], NULL, 16);
1788 		printf("OTPSTRAP[%d] will be protected\n", input);
1789 		printf("type \"YES\" (no quotes) to continue:\n");
1790 		if (!confirm_yesno()) {
1791 			printf(" Aborting\n");
1792 			return CMD_RET_FAILURE;
1793 		}
1794 	}
1795 
1796 	prog_address = 0x800;
1797 	if (input < 32) {
1798 		bit_offset = input;
1799 		prog_address |= 0x60c;
1800 	} else if (input < 64) {
1801 		bit_offset = input - 32;
1802 		prog_address |= 0x60e;
1803 	} else {
1804 		return CMD_RET_USAGE;
1805 	}
1806 
1807 	if (verify_bit(prog_address, bit_offset, 1) == 0) {
1808 		printf("OTPSTRAP[%d] already protected\n", input);
1809 	}
1810 	otp_prog_bit(1, prog_address, bit_offset, 0);
1811 	pass = -1;
1812 	for (i = 0; i < RETRY; i++) {
1813 		if (verify_bit(prog_address, bit_offset, 1) != 0) {
1814 			otp_prog_bit(1, prog_address, bit_offset, 1);
1815 		} else {
1816 			pass = 0;
1817 			break;
1818 		}
1819 	}
1820 	if (pass == 0) {
1821 		printf("OTPSTRAP[%d] is protected\n", input);
1822 		return CMD_RET_SUCCESS;
1823 	}
1824 
1825 	printf("Protect OTPSTRAP[%d] fail\n", input);
1826 	return CMD_RET_FAILURE;
1827 
1828 }
1829 static cmd_tbl_t cmd_otp[] = {
1830 	U_BOOT_CMD_MKENT(read, 4, 0, do_otpread, "", ""),
1831 	U_BOOT_CMD_MKENT(info, 3, 0, do_otpinfo, "", ""),
1832 	U_BOOT_CMD_MKENT(prog, 4, 0, do_otpprog, "", ""),
1833 	U_BOOT_CMD_MKENT(pb, 6, 0, do_otppb, "", ""),
1834 	U_BOOT_CMD_MKENT(protect, 3, 0, do_otpprotect, "", ""),
1835 	U_BOOT_CMD_MKENT(cmp, 3, 0, do_otpcmp, "", ""),
1836 };
1837 
1838 static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1839 {
1840 	cmd_tbl_t *cp;
1841 
1842 	cp = find_cmd_tbl(argv[1], cmd_otp, ARRAY_SIZE(cmd_otp));
1843 
1844 	/* Drop the otp command */
1845 	argc--;
1846 	argv++;
1847 
1848 	if (cp == NULL || argc > cp->maxargs)
1849 		return CMD_RET_USAGE;
1850 	if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
1851 		return CMD_RET_SUCCESS;
1852 
1853 	return cp->cmd(cmdtp, flag, argc, argv);
1854 }
1855 
1856 U_BOOT_CMD(
1857 	otp, 7, 0,  do_ast_otp,
1858 	"ASPEED One-Time-Programmable sub-system",
1859 	"read conf|data <otp_dw_offset> <dw_count>\n"
1860 	"otp read strap <strap_bit_offset> <bit_count>\n"
1861 	"otp info strap [v]\n"
1862 	"otp info conf [otp_dw_offset]\n"
1863 	"otp prog [f] <addr> <byte_size>\n"
1864 	"otp pb conf|data [f] <otp_dw_offset> <bit_offset> <value>\n"
1865 	"otp pb strap [f] <bit_offset> <value>\n"
1866 	"otp protect [f] <bit_offset>\n"
1867 	"otp cmp <addr> <otp_dw_offset>\n"
1868 );
1869