xref: /openbmc/u-boot/cmd/otp.c (revision 9ab23354)
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  : %d" },
258 	{ 4, 16, 8,  OTP_REG_VALID_BIT, "Keys retire  : %d" },
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 	int valid_bit = 0;
555 	int i;
556 
557 	printf("DW    BIT        Value       Description\n");
558 	printf("__________________________________________________________________________\n");
559 	for (i = 0; i < ARRAY_SIZE(a0_conf_info); i++) {
560 		dw_offset = a0_conf_info[i].dw_offset;
561 		bit_offset = a0_conf_info[i].bit_offset;
562 		mask = BIT(a0_conf_info[i].length) - 1;
563 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
564 		otp_keep = (OTPCFG_KEEP[dw_offset] >> bit_offset) & mask;
565 
566 		if (otp_keep == mask) {
567 			continue;
568 		} else if (otp_keep != 0) {
569 			fail = 1;
570 		}
571 
572 		if ((otp_value != a0_conf_info[i].value) &&
573 		    a0_conf_info[i].value != OTP_REG_RESERVED &&
574 		    a0_conf_info[i].value != OTP_REG_VALUE &&
575 		    a0_conf_info[i].value != OTP_REG_VALID_BIT)
576 			continue;
577 		printf("0x%-4X", dw_offset);
578 
579 		if (a0_conf_info[i].length == 1) {
580 			printf("0x%-9X", a0_conf_info[i].bit_offset);
581 		} else {
582 			printf("0x%-2X:0x%-4X",
583 			       a0_conf_info[i].bit_offset + a0_conf_info[i].length - 1,
584 			       a0_conf_info[i].bit_offset);
585 		}
586 		printf("0x%-10x", otp_value);
587 
588 		if (fail) {
589 			printf("Keep mask error\n");
590 		} else {
591 			if (a0_conf_info[i].value == OTP_REG_RESERVED) {
592 				printf("Reserved\n");
593 			} else if (a0_conf_info[i].value == OTP_REG_VALUE) {
594 				printf(a0_conf_info[i].information, otp_value);
595 				printf("\n");
596 			} else if (a0_conf_info[i].value == OTP_REG_VALID_BIT) {
597 				if (otp_value != 0) {
598 					for (i = 0; i < 7; i++) {
599 						if (otp_value == (1 << i)) {
600 							valid_bit = i + 1;
601 						}
602 					}
603 				} else {
604 					valid_bit = 0;
605 				}
606 				printf(a0_conf_info[i].information, valid_bit);
607 				printf("\n");
608 			} else {
609 				printf("%s\n", a0_conf_info[i].information);
610 			}
611 		}
612 	}
613 
614 	if (fail)
615 		return OTP_FAILURE;
616 
617 	return OTP_SUCCESS;
618 }
619 
620 static int otp_print_conf_info(int input_offset)
621 {
622 	uint32_t OTPCFG[12];
623 	uint32_t mask;
624 	uint32_t dw_offset;
625 	uint32_t bit_offset;
626 	uint32_t otp_value;
627 	int valid_bit = 0;
628 	int i;
629 
630 	for (i = 0; i < 12; i++)
631 		otp_read_config(i, &OTPCFG[i]);
632 
633 
634 	printf("DW    BIT        Value       Description\n");
635 	printf("__________________________________________________________________________\n");
636 	for (i = 0; i < ARRAY_SIZE(a0_conf_info); i++) {
637 		if (input_offset != -1 && input_offset != a0_conf_info[i].dw_offset)
638 			continue;
639 		dw_offset = a0_conf_info[i].dw_offset;
640 		bit_offset = a0_conf_info[i].bit_offset;
641 		mask = BIT(a0_conf_info[i].length) - 1;
642 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
643 
644 		if ((otp_value != a0_conf_info[i].value) &&
645 		    a0_conf_info[i].value != OTP_REG_RESERVED &&
646 		    a0_conf_info[i].value != OTP_REG_VALUE &&
647 		    a0_conf_info[i].value != OTP_REG_VALID_BIT)
648 			continue;
649 		printf("0x%-4X", dw_offset);
650 
651 		if (a0_conf_info[i].length == 1) {
652 			printf("0x%-9X", a0_conf_info[i].bit_offset);
653 		} else {
654 			printf("0x%-2X:0x%-4X",
655 			       a0_conf_info[i].bit_offset + a0_conf_info[i].length - 1,
656 			       a0_conf_info[i].bit_offset);
657 		}
658 		printf("0x%-10x", otp_value);
659 
660 		if (a0_conf_info[i].value == OTP_REG_RESERVED) {
661 			printf("Reserved\n");
662 		} else if (a0_conf_info[i].value == OTP_REG_VALUE) {
663 			printf(a0_conf_info[i].information, otp_value);
664 			printf("\n");
665 		} else if (a0_conf_info[i].value == OTP_REG_VALID_BIT) {
666 			if (otp_value != 0) {
667 				for (i = 0; i < 7; i++) {
668 					if (otp_value == (1 << i)) {
669 						valid_bit = i + 1;
670 					}
671 				}
672 			} else {
673 				valid_bit = 0;
674 			}
675 			printf(a0_conf_info[i].information, valid_bit);
676 			printf("\n");
677 		} else {
678 			printf("%s\n", a0_conf_info[i].information);
679 		}
680 	}
681 	return OTP_SUCCESS;
682 }
683 
684 static int otp_print_strap_image(uint32_t *OTPSTRAP)
685 {
686 	uint32_t *OTPSTRAP_PRO = &OTPSTRAP[4];
687 	uint32_t *OTPSTRAP_KEEP = &OTPSTRAP[2];
688 	int i;
689 	int fail = 0;
690 	uint32_t bit_offset;
691 	uint32_t dw_offset;
692 	uint32_t mask;
693 	uint32_t otp_value;
694 	uint32_t otp_protect;
695 	uint32_t otp_keep;
696 
697 	printf("BIT(hex)   Value       Protect     Description\n");
698 	printf("__________________________________________________________________________________________\n");
699 
700 	for (i = 0; i < ARRAY_SIZE(a0_strap_info); i++) {
701 		if (a0_strap_info[i].bit_offset > 32) {
702 			dw_offset = 1;
703 			bit_offset = a0_strap_info[i].bit_offset - 32;
704 		} else {
705 			dw_offset = 0;
706 			bit_offset = a0_strap_info[i].bit_offset;
707 		}
708 
709 		mask = BIT(a0_strap_info[i].length) - 1;
710 		otp_value = (OTPSTRAP[dw_offset] >> bit_offset) & mask;
711 		otp_protect = (OTPSTRAP_PRO[dw_offset] >> bit_offset) & mask;
712 		otp_keep = (OTPSTRAP_KEEP[dw_offset] >> bit_offset) & mask;
713 
714 		if (otp_keep == mask) {
715 			continue;
716 		} else if (otp_keep != 0) {
717 			fail = 1;
718 		}
719 
720 		if ((otp_value != a0_strap_info[i].value) &&
721 		    a0_strap_info[i].value != OTP_REG_RESERVED)
722 			continue;
723 
724 		if (a0_strap_info[i].length == 1) {
725 			printf("0x%-9X", a0_strap_info[i].bit_offset);
726 		} else {
727 			printf("0x%-2X:0x%-4X",
728 			       a0_strap_info[i].bit_offset + a0_strap_info[i].length - 1,
729 			       a0_strap_info[i].bit_offset);
730 		}
731 		printf("0x%-10x", otp_value);
732 		printf("0x%-10x", otp_protect);
733 
734 		if (fail) {
735 			printf("Keep mask error\n");
736 		} else {
737 			if (a0_strap_info[i].value != OTP_REG_RESERVED)
738 				printf("%s\n", a0_strap_info[i].information);
739 			else
740 				printf("Reserved\n");
741 		}
742 	}
743 
744 	if (fail)
745 		return OTP_FAILURE;
746 
747 	return OTP_SUCCESS;
748 }
749 
750 static int otp_print_strap_info(int view)
751 {
752 	struct otpstrap_status strap_status[64];
753 	int i, j;
754 	int fail = 0;
755 	uint32_t bit_offset;
756 	uint32_t length;
757 	uint32_t otp_value;
758 	uint32_t otp_protect;
759 
760 	otp_strp_status(strap_status);
761 
762 	if (view) {
763 		// printf("BIT(hex) Value  Option         Protect   Description\n");
764 		// printf("                0 1 2 3 4 5 6\n");
765 		printf("BIT(hex) Value  Remains  Protect   Description\n");
766 		printf("___________________________________________________________________________________________________\n");
767 	} else {
768 		printf("BIT(hex)   Value       Description\n");
769 		printf("________________________________________________________________________________\n");
770 	}
771 	for (i = 0; i < ARRAY_SIZE(a0_strap_info); i++) {
772 		otp_value = 0;
773 		bit_offset = a0_strap_info[i].bit_offset;
774 		length = a0_strap_info[i].length;
775 		for (j = 0; j < length; j++) {
776 			otp_value |= strap_status[bit_offset + j].value << j;
777 			otp_protect |= strap_status[bit_offset + j].protected << j;
778 		}
779 		if ((otp_value != a0_strap_info[i].value) &&
780 		    a0_strap_info[i].value != OTP_REG_RESERVED)
781 			continue;
782 		if (view) {
783 			for (j = 0; j < length; j++) {
784 				printf("0x%-7X", a0_strap_info[i].bit_offset + j);
785 				printf("0x%-5X", strap_status[bit_offset + j].value);
786 				printf("%-9d", strap_status[bit_offset + j].remain_times);
787 				printf("0x%-7X", strap_status[bit_offset].protected);
788 				if (a0_strap_info[i].value == OTP_REG_RESERVED) {
789 					printf(" Reserved\n");
790 					continue;
791 				}
792 				if (length == 1) {
793 					printf(" %s\n", a0_strap_info[i].information);
794 					continue;
795 				}
796 
797 				if (j == 0)
798 					printf("/%s\n", a0_strap_info[i].information);
799 				else if (j == length - 1)
800 					printf("\\ \"\n");
801 				else
802 					printf("| \"\n");
803 			}
804 		} else {
805 			if (length == 1) {
806 				printf("0x%-9X", a0_strap_info[i].bit_offset);
807 			} else {
808 				printf("0x%-2X:0x%-4X",
809 				       bit_offset + length - 1, bit_offset);
810 			}
811 
812 			printf("0x%-10X", otp_value);
813 
814 			if (a0_strap_info[i].value != OTP_REG_RESERVED)
815 				printf("%s\n", a0_strap_info[i].information);
816 			else
817 				printf("Reserved\n");
818 		}
819 	}
820 
821 	if (fail)
822 		return OTP_FAILURE;
823 
824 	return OTP_SUCCESS;
825 }
826 
827 static void buf_print(char *buf, int len)
828 {
829 	int i;
830 	printf("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
831 	for (i = 0; i < len; i++) {
832 		if (i % 16 == 0) {
833 			printf("%04X: ", i);
834 		}
835 		printf("%02X ", buf[i]);
836 		if ((i + 1) % 16 == 0) {
837 			printf("\n");
838 		}
839 	}
840 }
841 
842 static int otp_print_data_info(uint32_t *buf)
843 {
844 	int key_id, key_offset, last, key_type, key_length, exp_length;
845 	char *byte_buf;
846 	int i = 0, len = 0;
847 	byte_buf = (char *)buf;
848 	while (1) {
849 		key_id = buf[i] & 0x7;
850 		key_offset = buf[i] & 0x1ff8;
851 		last = (buf[i] >> 13) & 1;
852 		key_type = (buf[i] >> 14) & 0xf;
853 		key_length = (buf[i] >> 18) & 0x3;
854 		exp_length = (buf[i] >> 20) & 0xfff;
855 		printf("\nKey[%d]:\n", i);
856 		printf("Key Type: ");
857 		switch (key_type) {
858 		case 0:
859 			printf("AES-256 as OEM platform key for image encryption/decryption\n");
860 			break;
861 		case 1:
862 			printf("AES-256 as secret vault key\n");
863 			break;
864 		case 4:
865 			printf("HMAC as encrypted OEM HMAC keys in Mode 1\n");
866 			break;
867 		case 8:
868 			printf("RSA-public as OEM DSS public keys in Mode 2\n");
869 			break;
870 		case 9:
871 			printf("RSA-public as SOC public key\n");
872 			break;
873 		case 10:
874 			printf("RSA-public as AES key decryption key\n");
875 			break;
876 		case 13:
877 			printf("RSA-private as SOC private key\n");
878 			break;
879 		case 14:
880 			printf("RSA-private as AES key decryption key\n");
881 			break;
882 		default:
883 			printf("key_type error: %x\n", key_type);
884 			return -1;
885 		}
886 		if (key_type == 4) {
887 			printf("HMAC SHA Type: ");
888 			switch (key_length) {
889 			case 0:
890 				printf("HMAC(SHA224)\n");
891 				break;
892 			case 1:
893 				printf("HMAC(SHA256)\n");
894 				break;
895 			case 2:
896 				printf("HMAC(SHA384)\n");
897 				break;
898 			case 3:
899 				printf("HMAC(SHA512)\n");
900 				break;
901 			}
902 		} else if (key_type != 0 && key_type != 1) {
903 			printf("RSA SHA Type: ");
904 			switch (key_length) {
905 			case 0:
906 				printf("RSA1024\n");
907 				len = 0x100;
908 				break;
909 			case 1:
910 				printf("RSA2048\n");
911 				len = 0x200;
912 				break;
913 			case 2:
914 				printf("RSA3072\n");
915 				len = 0x300;
916 				break;
917 			case 3:
918 				printf("RSA4096\n");
919 				len = 0x400;
920 				break;
921 			}
922 			printf("RSA exponent bit length: %d\n", exp_length);
923 		}
924 		if (key_type == 4 || key_type == 8)
925 			printf("Key Number ID: %d\n", key_id);
926 		printf("Key Value:\n");
927 		if (key_type == 4) {
928 			buf_print(&byte_buf[key_offset], 0x40);
929 		} else if (key_type == 0 || key_type == 1) {
930 			printf("AES Key:\n");
931 			buf_print(&byte_buf[key_offset], 0x20);
932 			printf("AES IV:\n");
933 			buf_print(&byte_buf[key_offset + 0x20], 0x10);
934 
935 		} else {
936 			printf("RSA mod:\n");
937 			buf_print(&byte_buf[key_offset], len / 2);
938 			printf("RSA exp:\n");
939 			buf_print(&byte_buf[key_offset + (len / 2)], len / 2);
940 		}
941 		if (last)
942 			break;
943 		i++;
944 	}
945 	return 0;
946 }
947 
948 static int otp_prog_conf(uint32_t *buf)
949 {
950 	int i, k;
951 	int pass = 0;
952 	int soak = 0;
953 	uint32_t prog_address;
954 	uint32_t data[12];
955 	uint32_t compare[2];
956 	uint32_t *buf_keep = &buf[12];
957 	uint32_t data_masked;
958 	uint32_t buf_masked;
959 
960 	printf("Read OTP Config Region:\n");
961 
962 	printProgress(0, 12, "");
963 	for (i = 0; i < 12 ; i ++) {
964 		printProgress(i + 1, 12, "");
965 		prog_address = 0x800;
966 		prog_address |= (i / 8) * 0x200;
967 		prog_address |= (i % 8) * 0x2;
968 		otp_read_data(prog_address, &data[i]);
969 	}
970 
971 	printf("Check writable...\n");
972 	for (i = 0; i < 12; i++) {
973 		data_masked = data[i]  & ~buf_keep[i];
974 		buf_masked  = buf[i] & ~buf_keep[i];
975 		if (data_masked == buf_masked)
976 			continue;
977 		if ((data_masked | buf_masked) == buf_masked) {
978 			continue;
979 		} else {
980 			printf("Input image can't program into OTP, please check.\n");
981 			printf("OTPCFG[%X] = %x\n", i, data[i]);
982 			printf("Input [%X] = %x\n", i, buf[i]);
983 			printf("Mask  [%X] = %x\n", i, ~buf_keep[i]);
984 			return OTP_FAILURE;
985 		}
986 	}
987 
988 	printf("Start Programing...\n");
989 	printProgress(0, 12, "");
990 	otp_soak(0);
991 	for (i = 0; i < 12; i++) {
992 		data_masked = data[i]  & ~buf_keep[i];
993 		buf_masked  = buf[i] & ~buf_keep[i];
994 		prog_address = 0x800;
995 		prog_address |= (i / 8) * 0x200;
996 		prog_address |= (i % 8) * 0x2;
997 		if (data_masked == buf_masked) {
998 			printProgress(i + 1, 12, "[%03X]=%08X HIT", prog_address, buf[i]);
999 			continue;
1000 		}
1001 		if (soak) {
1002 			soak = 0;
1003 			otp_soak(0);
1004 		}
1005 		printProgress(i + 1, 12, "[%03X]=%08X    ", prog_address, buf[i]);
1006 
1007 		otp_prog_dw(buf[i], buf_keep[i], prog_address);
1008 
1009 		pass = 0;
1010 		for (k = 0; k < RETRY; k++) {
1011 			if (verify_dw(prog_address, &buf[i], &buf_keep[i], compare, 1) != 0) {
1012 				if (soak == 0) {
1013 					soak = 1;
1014 					otp_soak(1);
1015 				}
1016 				otp_prog_dw(compare[0], prog_address, 1);
1017 			} else {
1018 				pass = 1;
1019 				break;
1020 			}
1021 		}
1022 	}
1023 
1024 	if (!pass)
1025 		return OTP_FAILURE;
1026 
1027 	return OTP_SUCCESS;
1028 
1029 }
1030 
1031 
1032 static int otp_strap_image_confirm(uint32_t *buf)
1033 {
1034 	int i;
1035 	uint32_t *strap_keep = buf + 2;
1036 	uint32_t *strap_protect = buf + 4;
1037 	int bit, pbit, kbit;
1038 	int fail = 0;
1039 	int skip = -1;
1040 	struct otpstrap_status otpstrap[64];
1041 
1042 	otp_strp_status(otpstrap);
1043 	for (i = 0; i < 64; i++) {
1044 		if (i < 32) {
1045 			bit = (buf[0] >> i) & 0x1;
1046 			kbit = (strap_keep[0] >> i) & 0x1;
1047 			pbit = (strap_protect[0] >> i) & 0x1;
1048 		} else {
1049 			bit = (buf[1] >> (i - 32)) & 0x1;
1050 			kbit = (strap_keep[1] >> (i - 32)) & 0x1;
1051 			pbit = (strap_protect[1] >> (i - 32)) & 0x1;
1052 		}
1053 
1054 		if (kbit == 1) {
1055 			continue;
1056 		} else {
1057 			printf("OTPSTRAP[%X]:\n", i);
1058 		}
1059 		if (bit == otpstrap[i].value) {
1060 			printf("    The value is same as before, skip it.\n");
1061 			if (skip == -1)
1062 				skip = 1;
1063 			continue;
1064 		} else {
1065 			skip = 0;
1066 		}
1067 		if (otpstrap[i].protected == 1) {
1068 			printf("    This bit is protected and is not writable\n");
1069 			fail = 1;
1070 			continue;
1071 		}
1072 		if (otpstrap[i].remain_times == 0) {
1073 			printf("    This bit is no remaining times to write.\n");
1074 			fail = 1;
1075 			continue;
1076 		}
1077 		if (pbit == 1) {
1078 			printf("    This bit will be protected and become non-writable.\n");
1079 		}
1080 		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);
1081 	}
1082 	if (fail == 1)
1083 		return OTP_FAILURE;
1084 	else if (skip == 1)
1085 		return OTP_PROG_SKIP;
1086 
1087 	return 0;
1088 }
1089 
1090 static int otp_print_strap(int start, int count)
1091 {
1092 	int i, j;
1093 	struct otpstrap_status otpstrap[64];
1094 
1095 	if (start < 0 || start > 64)
1096 		return OTP_USAGE;
1097 
1098 	if ((start + count) < 0 || (start + count) > 64)
1099 		return OTP_USAGE;
1100 
1101 	otp_strp_status(otpstrap);
1102 
1103 	printf("BIT(hex)  Value  Option           Status\n");
1104 	printf("___________________________________________________________________________\n");
1105 
1106 	for (i = start; i < start + count; i++) {
1107 		printf("0x%-8X", i);
1108 		printf("%-7d", otpstrap[i].value);
1109 		for (j = 0; j < 7; j++)
1110 			printf("%d ", otpstrap[i].option_array[j]);
1111 		printf("   ");
1112 		if (otpstrap[i].protected == 1) {
1113 			printf("protected and not writable");
1114 		} else {
1115 			printf("not protected ");
1116 			if (otpstrap[i].remain_times == 0) {
1117 				printf("and no remaining times to write.");
1118 			} else {
1119 				printf("and still can write %d times", otpstrap[i].remain_times);
1120 			}
1121 		}
1122 		printf("\n");
1123 	}
1124 
1125 	return OTP_SUCCESS;
1126 }
1127 
1128 static int otp_prog_strap(uint32_t *buf)
1129 {
1130 	int i, j;
1131 	uint32_t *strap_keep = buf + 2;
1132 	uint32_t *strap_protect = buf + 4;
1133 	uint32_t prog_bit, prog_address;
1134 	int bit, pbit, kbit, offset;
1135 	int fail = 0;
1136 	int pass = 0;
1137 	int soak = 0;
1138 	struct otpstrap_status otpstrap[64];
1139 
1140 	printf("Read OTP Strap Region:\n");
1141 	otp_strp_status(otpstrap);
1142 
1143 	printf("Check writable...\n");
1144 	if (otp_strap_image_confirm(buf) == OTP_FAILURE) {
1145 		printf("Input image can't program into OTP, please check.\n");
1146 		return OTP_FAILURE;
1147 	}
1148 
1149 	otp_soak(0);
1150 	for (i = 0; i < 64; i++) {
1151 		printProgress(i + 1, 64, "");
1152 		prog_address = 0x800;
1153 		if (i < 32) {
1154 			offset = i;
1155 			bit = (buf[0] >> offset) & 0x1;
1156 			kbit = (strap_keep[0] >> offset) & 0x1;
1157 			pbit = (strap_protect[0] >> offset) & 0x1;
1158 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200;
1159 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2;
1160 
1161 		} else {
1162 			offset = (i - 32);
1163 			bit = (buf[1] >> offset) & 0x1;
1164 			kbit = (strap_keep[1] >> offset) & 0x1;
1165 			pbit = (strap_protect[1] >> offset) & 0x1;
1166 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200;
1167 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2;
1168 		}
1169 		prog_bit = ~(0x1 << offset);
1170 
1171 		if (kbit == 1) {
1172 			continue;
1173 		}
1174 		if (bit == otpstrap[i].value) {
1175 			continue;
1176 		}
1177 		if (otpstrap[i].protected == 1) {
1178 			fail = 1;
1179 			continue;
1180 		}
1181 		if (otpstrap[i].remain_times == 0) {
1182 			fail = 1;
1183 			continue;
1184 		}
1185 
1186 		if (soak) {
1187 			soak = 0;
1188 			otp_soak(0);
1189 		}
1190 
1191 		otp_prog(prog_address, prog_bit);
1192 
1193 		pass = 0;
1194 
1195 		for (j = 0; j < RETRY; j++) {
1196 			if (verify_bit(prog_address, offset, 1) == 0) {
1197 				pass = 1;
1198 				break;
1199 			}
1200 			if (soak == 0) {
1201 				soak = 1;
1202 				otp_soak(1);
1203 			}
1204 			otp_prog(prog_address, prog_bit);
1205 		}
1206 		if (!pass)
1207 			return OTP_FAILURE;
1208 
1209 		if (pbit == 0)
1210 			continue;
1211 		prog_address = 0x800;
1212 		if (i < 32)
1213 			prog_address |= 0x60c;
1214 		else
1215 			prog_address |= 0x60e;
1216 
1217 
1218 		if (soak) {
1219 			soak = 0;
1220 			otp_soak(0);
1221 		}
1222 
1223 		otp_prog(prog_address, prog_bit);
1224 
1225 		pass = 0;
1226 
1227 		for (j = 0; j < RETRY; j++) {
1228 
1229 			if (verify_bit(prog_address, offset, 1) == 0) {
1230 				pass = 1;
1231 				break;
1232 			}
1233 			if (soak == 0) {
1234 				soak = 1;
1235 				otp_soak(1);
1236 			}
1237 			otp_prog(prog_address, prog_bit);
1238 		}
1239 		if (!pass)
1240 			return OTP_FAILURE;
1241 
1242 	}
1243 	if (fail == 1)
1244 		return OTP_FAILURE;
1245 	else
1246 		return OTP_SUCCESS;
1247 
1248 }
1249 
1250 static void otp_prog_bit(uint32_t value, uint32_t prog_address, uint32_t bit_offset, int soak)
1251 {
1252 	int prog_bit;
1253 
1254 	otp_soak(soak);
1255 
1256 	if (prog_address % 2 == 0) {
1257 		if (value)
1258 			prog_bit = ~(0x1 << bit_offset);
1259 		else
1260 			return;
1261 	} else {
1262 		prog_address |= 1 << 15;
1263 		if (!value)
1264 			prog_bit = 0x1 << bit_offset;
1265 		else
1266 			return;
1267 	}
1268 	otp_prog(prog_address, prog_bit);
1269 }
1270 
1271 static int otp_prog_data(uint32_t *buf)
1272 {
1273 	int i, k;
1274 	int pass;
1275 	int soak = 0;
1276 	uint32_t prog_address;
1277 	uint32_t data[2048];
1278 	uint32_t compare[2];
1279 	uint32_t *buf_keep = &buf[2048];
1280 
1281 	uint32_t data0_masked;
1282 	uint32_t data1_masked;
1283 	uint32_t buf0_masked;
1284 	uint32_t buf1_masked;
1285 
1286 	printf("Read OTP Data:\n");
1287 
1288 	printProgress(0, 2048, "");
1289 	for (i = 0; i < 2048 ; i += 2) {
1290 		printProgress(i + 2, 2048, "");
1291 		otp_read_data(i, &data[i]);
1292 	}
1293 
1294 
1295 	printf("Check writable...\n");
1296 	for (i = 0; i < 2048; i++) {
1297 		data0_masked = data[i]  & ~buf_keep[i];
1298 		buf0_masked  = buf[i] & ~buf_keep[i];
1299 		if (data0_masked == buf0_masked)
1300 			continue;
1301 		if (i % 2 == 0) {
1302 			if ((data0_masked | buf0_masked) == buf0_masked) {
1303 				continue;
1304 			} else {
1305 				printf("Input image can't program into OTP, please check.\n");
1306 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
1307 				printf("Input   [%x] = %x\n", i, buf[i]);
1308 				printf("Mask    [%x] = %x\n", i, ~buf_keep[i]);
1309 				return OTP_FAILURE;
1310 			}
1311 		} else {
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 		}
1322 	}
1323 
1324 	printf("Start Programing...\n");
1325 	printProgress(0, 2048, "");
1326 
1327 	for (i = 0; i < 2048; i += 2) {
1328 		prog_address = i;
1329 		data0_masked = data[i]  & ~buf_keep[i];
1330 		buf0_masked  = buf[i] & ~buf_keep[i];
1331 		data1_masked = data[i + 1]  & ~buf_keep[i + 1];
1332 		buf1_masked  = buf[i + 1] & ~buf_keep[i + 1];
1333 		if ((data0_masked == buf0_masked) && (data1_masked == buf1_masked)) {
1334 			printProgress(i + 2, 2048, "[%03X]=%08X HIT;[%03X]=%08X HIT", prog_address, buf[i], prog_address + 1, buf[i + 1]);
1335 			continue;
1336 		}
1337 		if (soak) {
1338 			soak = 0;
1339 			otp_soak(0);
1340 		}
1341 		if (data1_masked == buf1_masked) {
1342 			printProgress(i + 2, 2048, "[%03X]=%08X    ;[%03X]=%08X HIT", prog_address, buf[i], prog_address + 1, buf[i + 1]);
1343 			otp_prog_dw(buf[i], buf_keep[i], prog_address);
1344 		} else if (data0_masked == buf0_masked) {
1345 			printProgress(i + 2, 2048, "[%03X]=%08X HIT;[%03X]=%08X    ", prog_address, buf[i], prog_address + 1, buf[i + 1]);
1346 			otp_prog_dw(buf[i + 1], buf_keep[i + 1], prog_address + 1);
1347 		} else {
1348 			printProgress(i + 2, 2048, "[%03X]=%08X    ;[%03X]=%08X    ", prog_address, buf[i], prog_address + 1, buf[i + 1]);
1349 			otp_prog_dw(buf[i], buf_keep[i], prog_address);
1350 			otp_prog_dw(buf[i + 1], buf_keep[i + 1], prog_address + 1);
1351 		}
1352 
1353 		pass = 0;
1354 		for (k = 0; k < RETRY; k++) {
1355 			if (verify_dw(prog_address, &buf[i], &buf_keep[i], compare, 2) != 0) {
1356 				if (soak == 0) {
1357 					soak = 1;
1358 					otp_soak(1);
1359 				}
1360 				if (compare[0] != 0) {
1361 					otp_prog_dw(compare[0], buf_keep[i], prog_address);
1362 				}
1363 				if (compare[1] != ~0) {
1364 					otp_prog_dw(compare[1], buf_keep[i], prog_address + 1);
1365 				}
1366 			} else {
1367 				pass = 1;
1368 				break;
1369 			}
1370 		}
1371 
1372 		if (!pass)
1373 			return OTP_FAILURE;
1374 	}
1375 	return OTP_SUCCESS;
1376 
1377 }
1378 
1379 static int do_otp_prog(int addr, int byte_size, int nconfirm)
1380 {
1381 	int ret;
1382 	int mode = 0;
1383 	uint32_t *buf;
1384 	uint32_t *data_region = NULL;
1385 	uint32_t *conf_region = NULL;
1386 	uint32_t *strap_region = NULL;
1387 
1388 	buf = map_physmem(addr, byte_size, MAP_WRBACK);
1389 	if (!buf) {
1390 		puts("Failed to map physical memory\n");
1391 		return OTP_FAILURE;
1392 	}
1393 
1394 	if (buf[0] & BIT(29)) {
1395 		mode |= OTP_REGION_DATA;
1396 		data_region = &buf[36];
1397 	}
1398 	if (buf[0] & BIT(30)) {
1399 		mode |= OTP_REGION_CONF;
1400 		conf_region = &buf[12];
1401 	}
1402 	if (buf[0] & BIT(31)) {
1403 		mode |= OTP_REGION_STRAP;
1404 		strap_region = &buf[4];
1405 	}
1406 
1407 	if (!nconfirm) {
1408 		if (mode & OTP_REGION_DATA) {
1409 			printf("\nOTP data region :\n");
1410 			if (otp_print_data_info(data_region) < 0) {
1411 				printf("OTP data error, please check.\n");
1412 				return OTP_FAILURE;
1413 			}
1414 		}
1415 		if (mode & OTP_REGION_STRAP) {
1416 			printf("\nOTP strap region :\n");
1417 			if (otp_print_strap_image(strap_region) < 0) {
1418 				printf("OTP strap error, please check.\n");
1419 				return OTP_FAILURE;
1420 			}
1421 		}
1422 		if (mode & OTP_REGION_CONF) {
1423 			printf("\nOTP configuration region :\n");
1424 			if (otp_print_conf_image(conf_region) < 0) {
1425 				printf("OTP config error, please check.\n");
1426 				return OTP_FAILURE;
1427 			}
1428 		}
1429 
1430 		printf("type \"YES\" (no quotes) to continue:\n");
1431 		if (!confirm_yesno()) {
1432 			printf(" Aborting\n");
1433 			return OTP_FAILURE;
1434 		}
1435 	}
1436 
1437 	if (mode & OTP_REGION_DATA) {
1438 		printf("programing data region ...\n");
1439 		ret = otp_prog_data(data_region);
1440 		if (ret != 0) {
1441 			printf("Error\n");
1442 			return ret;
1443 		} else {
1444 			printf("Done\n");
1445 		}
1446 	}
1447 	if (mode & OTP_REGION_STRAP) {
1448 		printf("programing strap region ...\n");
1449 		ret = otp_prog_strap(strap_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_CONF) {
1458 		printf("programing configuration region ...\n");
1459 		ret = otp_prog_conf(conf_region);
1460 		if (ret != 0) {
1461 			printf("Error\n");
1462 			return ret;
1463 		}
1464 		printf("Done\n");
1465 	}
1466 
1467 	return OTP_SUCCESS;
1468 }
1469 
1470 static int do_otp_prog_bit(int mode, int otp_dw_offset, int bit_offset, int value, int nconfirm)
1471 {
1472 	uint32_t read[2];
1473 	uint32_t strap_buf[6];
1474 	uint32_t prog_address = 0;
1475 	struct otpstrap_status otpstrap[64];
1476 	int otp_bit;
1477 	int i;
1478 	int pass;
1479 	int ret;
1480 
1481 	switch (mode) {
1482 	case OTP_REGION_CONF:
1483 		otp_read_config(otp_dw_offset, read);
1484 		prog_address = 0x800;
1485 		prog_address |= (otp_dw_offset / 8) * 0x200;
1486 		prog_address |= (otp_dw_offset % 8) * 0x2;
1487 		otp_bit = (read[0] >> bit_offset) & 0x1;
1488 		if (otp_bit == value) {
1489 			printf("OTPCFG%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
1490 			printf("No need to program\n");
1491 			return OTP_SUCCESS;
1492 		}
1493 		if (otp_bit == 1 && value == 0) {
1494 			printf("OTPCFG%X[%X] = 1\n", otp_dw_offset, bit_offset);
1495 			printf("OTP is programed, which can't be clean\n");
1496 			return OTP_FAILURE;
1497 		}
1498 		printf("Program OTPCFG%X[%X] to 1\n", otp_dw_offset, bit_offset);
1499 		break;
1500 	case OTP_REGION_DATA:
1501 		prog_address = otp_dw_offset;
1502 
1503 		if (otp_dw_offset % 2 == 0) {
1504 			otp_read_data(otp_dw_offset, read);
1505 			otp_bit = (read[0] >> bit_offset) & 0x1;
1506 		} else {
1507 			otp_read_data(otp_dw_offset - 1, read);
1508 			otp_bit = (read[1] >> bit_offset) & 0x1;
1509 		}
1510 		if (otp_bit == value) {
1511 			printf("OTPDATA%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
1512 			printf("No need to program\n");
1513 			return OTP_SUCCESS;
1514 		}
1515 		if (otp_bit == 1 && value == 0) {
1516 			printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
1517 			printf("OTP is programed, which can't be clean\n");
1518 			return OTP_FAILURE;
1519 		}
1520 		printf("Program OTPDATA%X[%X] to 1\n", otp_dw_offset, bit_offset);
1521 		break;
1522 	case OTP_REGION_STRAP:
1523 		otp_strp_status(otpstrap);
1524 		otp_print_strap(bit_offset, 1);
1525 		if (bit_offset < 32) {
1526 			strap_buf[0] = value << bit_offset;
1527 			strap_buf[2] = ~BIT(bit_offset);
1528 			strap_buf[3] = ~0;
1529 			strap_buf[5] = 0;
1530 			// if (protect)
1531 			// 	strap_buf[4] = BIT(bit_offset);
1532 			// else
1533 			// 	strap_buf[4] = 0;
1534 		} else {
1535 			strap_buf[1] = value << (bit_offset - 32);
1536 			strap_buf[2] = ~0;
1537 			strap_buf[3] = ~BIT(bit_offset - 32);
1538 			strap_buf[4] = 0;
1539 			// if (protect)
1540 			// 	strap_buf[5] = BIT(bit_offset - 32);
1541 			// else
1542 			// 	strap_buf[5] = 0;
1543 		}
1544 		ret = otp_strap_image_confirm(strap_buf);
1545 		if (ret == OTP_FAILURE)
1546 			return OTP_FAILURE;
1547 		else if (ret == OTP_PROG_SKIP)
1548 			return OTP_SUCCESS;
1549 
1550 		break;
1551 	}
1552 
1553 	if (!nconfirm) {
1554 		printf("type \"YES\" (no quotes) to continue:\n");
1555 		if (!confirm_yesno()) {
1556 			printf(" Aborting\n");
1557 			return OTP_FAILURE;
1558 		}
1559 	}
1560 
1561 	switch (mode) {
1562 	case OTP_REGION_STRAP:
1563 		return otp_prog_strap(strap_buf);
1564 	case OTP_REGION_CONF:
1565 	case OTP_REGION_DATA:
1566 		otp_prog_bit(value, prog_address, bit_offset, 0);
1567 		pass = -1;
1568 		for (i = 0; i < RETRY; i++) {
1569 			if (verify_bit(prog_address, bit_offset, value) != 0) {
1570 				otp_prog_bit(value, prog_address, bit_offset, 1);
1571 			} else {
1572 				pass = 0;
1573 				break;
1574 			}
1575 		}
1576 		if (pass == 0)
1577 			return OTP_SUCCESS;
1578 	}
1579 
1580 	return OTP_USAGE;
1581 }
1582 
1583 static int do_otpread(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1584 {
1585 	uint32_t offset, count;
1586 	int ret;
1587 
1588 	if (argc == 4) {
1589 		offset = simple_strtoul(argv[2], NULL, 16);
1590 		count = simple_strtoul(argv[3], NULL, 16);
1591 	} else if (argc == 3) {
1592 		offset = simple_strtoul(argv[2], NULL, 16);
1593 		count = 1;
1594 	} else {
1595 		return CMD_RET_USAGE;
1596 	}
1597 
1598 
1599 	if (!strcmp(argv[1], "conf")) {
1600 		writel(OTP_PASSWD, 0x1e6f2000); //password
1601 		ret = otp_print_config(offset, count);
1602 	} else if (!strcmp(argv[1], "data")) {
1603 		writel(OTP_PASSWD, 0x1e6f2000); //password
1604 		ret = otp_print_data(offset, count);
1605 	} else if (!strcmp(argv[1], "strap")) {
1606 		writel(OTP_PASSWD, 0x1e6f2000); //password
1607 		ret = otp_print_strap(offset, count);
1608 	} else {
1609 		return CMD_RET_USAGE;
1610 	}
1611 
1612 	if (ret == OTP_SUCCESS)
1613 		return CMD_RET_SUCCESS;
1614 	else
1615 		return CMD_RET_USAGE;
1616 
1617 }
1618 
1619 static int do_otpprog(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1620 {
1621 	phys_addr_t addr;
1622 	uint32_t byte_size;
1623 	int ret;
1624 
1625 	if (argc == 4) {
1626 		if (strcmp(argv[1], "f"))
1627 			return CMD_RET_USAGE;
1628 		addr = simple_strtoul(argv[2], NULL, 16);
1629 		byte_size = simple_strtoul(argv[3], NULL, 16);
1630 		writel(OTP_PASSWD, 0x1e6f2000); //password
1631 		ret = do_otp_prog(addr, byte_size, 1);
1632 	} else if (argc == 3) {
1633 		addr = simple_strtoul(argv[1], NULL, 16);
1634 		byte_size = simple_strtoul(argv[2], NULL, 16);
1635 		writel(OTP_PASSWD, 0x1e6f2000); //password
1636 		ret = do_otp_prog(addr, byte_size, 0);
1637 	} else {
1638 		return CMD_RET_USAGE;
1639 	}
1640 
1641 	if (ret == OTP_SUCCESS)
1642 		return CMD_RET_SUCCESS;
1643 	else if (ret == OTP_FAILURE)
1644 		return CMD_RET_FAILURE;
1645 	else
1646 		return CMD_RET_USAGE;
1647 }
1648 
1649 static int do_otppb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1650 {
1651 	int mode = 0;
1652 	int nconfirm = 0;
1653 	int otp_addr = 0;
1654 	int bit_offset;
1655 	int value;
1656 	int ret;
1657 
1658 	if (argc != 4 && argc != 5 && argc != 6)
1659 		return CMD_RET_USAGE;
1660 
1661 	/* Drop the pb cmd */
1662 	argc--;
1663 	argv++;
1664 
1665 	if (!strcmp(argv[0], "conf"))
1666 		mode = OTP_REGION_CONF;
1667 	else if (!strcmp(argv[0], "strap"))
1668 		mode = OTP_REGION_STRAP;
1669 	else if (!strcmp(argv[0], "data"))
1670 		mode = OTP_REGION_DATA;
1671 	else
1672 		return CMD_RET_USAGE;
1673 
1674 	/* Drop the region cmd */
1675 	argc--;
1676 	argv++;
1677 
1678 	if (!strcmp(argv[0], "f")) {
1679 		nconfirm = 1;
1680 		/* Drop the force option */
1681 		argc--;
1682 		argv++;
1683 	}
1684 
1685 	if (mode == OTP_REGION_STRAP) {
1686 		bit_offset = simple_strtoul(argv[0], NULL, 16);
1687 		value = simple_strtoul(argv[1], NULL, 16);
1688 		if (bit_offset >= 64)
1689 			return CMD_RET_USAGE;
1690 	} else {
1691 		otp_addr = simple_strtoul(argv[0], NULL, 16);
1692 		bit_offset = simple_strtoul(argv[1], NULL, 16);
1693 		value = simple_strtoul(argv[2], NULL, 16);
1694 		if (bit_offset >= 32)
1695 			return CMD_RET_USAGE;
1696 	}
1697 	if (value != 0 && value != 1)
1698 		return CMD_RET_USAGE;
1699 
1700 	writel(OTP_PASSWD, 0x1e6f2000); //password
1701 	ret = do_otp_prog_bit(mode, otp_addr, bit_offset, value, nconfirm);
1702 
1703 	if (ret == OTP_SUCCESS)
1704 		return CMD_RET_SUCCESS;
1705 	else if (ret == OTP_FAILURE)
1706 		return CMD_RET_FAILURE;
1707 	else
1708 		return CMD_RET_USAGE;
1709 }
1710 
1711 static int do_otpcmp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1712 {
1713 	phys_addr_t addr;
1714 	int otp_addr = 0;
1715 
1716 	if (argc != 3)
1717 		return CMD_RET_USAGE;
1718 
1719 	writel(OTP_PASSWD, 0x1e6f2000); //password
1720 	addr = simple_strtoul(argv[1], NULL, 16);
1721 	otp_addr = simple_strtoul(argv[2], NULL, 16);
1722 	if (otp_compare(otp_addr, addr) == 0) {
1723 		printf("Compare pass\n");
1724 		return CMD_RET_SUCCESS;
1725 	} else {
1726 		printf("Compare fail\n");
1727 		return CMD_RET_FAILURE;
1728 	}
1729 }
1730 
1731 static int do_otpinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1732 {
1733 	int view = 0;
1734 	int input;
1735 
1736 	if (argc != 2 && argc != 3)
1737 		return CMD_RET_USAGE;
1738 
1739 	if (!strcmp(argv[1], "conf")) {
1740 
1741 		writel(OTP_PASSWD, 0x1e6f2000); //password
1742 		if (argc == 3) {
1743 			input = simple_strtoul(argv[2], NULL, 16);
1744 			otp_print_conf_info(input);
1745 		} else {
1746 			otp_print_conf_info(-1);
1747 		}
1748 	} else if (!strcmp(argv[1], "strap")) {
1749 		if (!strcmp(argv[2], "v")) {
1750 			view = 1;
1751 			/* Drop the view option */
1752 			argc--;
1753 			argv++;
1754 		}
1755 		writel(OTP_PASSWD, 0x1e6f2000); //password
1756 		otp_print_strap_info(view);
1757 	} else {
1758 		return CMD_RET_USAGE;
1759 	}
1760 
1761 	return CMD_RET_SUCCESS;
1762 }
1763 
1764 static int do_otpprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1765 {
1766 	int input;
1767 	int bit_offset;
1768 	int prog_address;
1769 	int pass;
1770 	int i;
1771 	if (argc != 3 && argc != 2)
1772 		return CMD_RET_USAGE;
1773 
1774 	if (!strcmp(argv[0], "f")) {
1775 		input = simple_strtoul(argv[2], NULL, 16);
1776 	} else {
1777 		input = simple_strtoul(argv[1], NULL, 16);
1778 		printf("OTPSTRAP[%d] will be protected\n", input);
1779 		printf("type \"YES\" (no quotes) to continue:\n");
1780 		if (!confirm_yesno()) {
1781 			printf(" Aborting\n");
1782 			return CMD_RET_FAILURE;
1783 		}
1784 	}
1785 
1786 	prog_address = 0x800;
1787 	if (input < 32) {
1788 		bit_offset = input;
1789 		prog_address |= 0x60c;
1790 	} else if (input < 64) {
1791 		bit_offset = input - 32;
1792 		prog_address |= 0x60e;
1793 	} else {
1794 		return CMD_RET_USAGE;
1795 	}
1796 
1797 	if (verify_bit(prog_address, bit_offset, 1) == 0) {
1798 		printf("OTPSTRAP[%d] already protected\n", input);
1799 	}
1800 	otp_prog_bit(1, prog_address, bit_offset, 0);
1801 	pass = -1;
1802 	for (i = 0; i < RETRY; i++) {
1803 		if (verify_bit(prog_address, bit_offset, 1) != 0) {
1804 			otp_prog_bit(1, prog_address, bit_offset, 1);
1805 		} else {
1806 			pass = 0;
1807 			break;
1808 		}
1809 	}
1810 	if (pass == 0) {
1811 		printf("OTPSTRAP[%d] is protected\n", input);
1812 		return CMD_RET_SUCCESS;
1813 	}
1814 
1815 	printf("Protect OTPSTRAP[%d] fail\n", input);
1816 	return CMD_RET_FAILURE;
1817 
1818 }
1819 static cmd_tbl_t cmd_otp[] = {
1820 	U_BOOT_CMD_MKENT(read, 4, 0, do_otpread, "", ""),
1821 	U_BOOT_CMD_MKENT(info, 3, 0, do_otpinfo, "", ""),
1822 	U_BOOT_CMD_MKENT(prog, 4, 0, do_otpprog, "", ""),
1823 	U_BOOT_CMD_MKENT(pb, 6, 0, do_otppb, "", ""),
1824 	U_BOOT_CMD_MKENT(protect, 3, 0, do_otpprotect, "", ""),
1825 	U_BOOT_CMD_MKENT(cmp, 3, 0, do_otpcmp, "", ""),
1826 };
1827 
1828 static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1829 {
1830 	cmd_tbl_t *cp;
1831 
1832 	cp = find_cmd_tbl(argv[1], cmd_otp, ARRAY_SIZE(cmd_otp));
1833 
1834 	/* Drop the otp command */
1835 	argc--;
1836 	argv++;
1837 
1838 	if (cp == NULL || argc > cp->maxargs)
1839 		return CMD_RET_USAGE;
1840 	if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
1841 		return CMD_RET_SUCCESS;
1842 
1843 	return cp->cmd(cmdtp, flag, argc, argv);
1844 }
1845 
1846 U_BOOT_CMD(
1847 	otp, 7, 0,  do_ast_otp,
1848 	"ASPEED One-Time-Programmable sub-system",
1849 	"read conf|data <otp_dw_offset> <dw_count>\n"
1850 	"otp read strap <strap_bit_offset> <bit_count>\n"
1851 	"otp info strap [v]\n"
1852 	"otp info conf [otp_dw_offset]\n"
1853 	"otp prog [f] <addr> <byte_size>\n"
1854 	"otp pb conf|data [f] <otp_dw_offset> <bit_offset> <value>\n"
1855 	"otp pb strap [f] <bit_offset> <value>\n"
1856 	"otp protect [f] <bit_offset>\n"
1857 	"otp cmp <addr> <otp_dw_offset>\n"
1858 );
1859