xref: /openbmc/u-boot/cmd/otp.c (revision 21a8cfcea044e751281e1e0d56618e0a943dbd8c)
1a219f6deSJohnny Huang // SPDX-License-Identifier: GPL-2.0+
269d5fd8fSJohnny Huang /*
3a219f6deSJohnny Huang  * Copyright 2021 Aspeed Technology Inc.
469d5fd8fSJohnny Huang  */
5e417205bSJohnny Huang 
64c1c9b35SJohnny Huang #include <stdlib.h>
769d5fd8fSJohnny Huang #include <common.h>
869d5fd8fSJohnny Huang #include <console.h>
969d5fd8fSJohnny Huang #include <bootretry.h>
1069d5fd8fSJohnny Huang #include <cli.h>
1169d5fd8fSJohnny Huang #include <command.h>
1269d5fd8fSJohnny Huang #include <console.h>
134c1c9b35SJohnny Huang #include <malloc.h>
1469d5fd8fSJohnny Huang #include <inttypes.h>
1569d5fd8fSJohnny Huang #include <mapmem.h>
1669d5fd8fSJohnny Huang #include <asm/io.h>
1769d5fd8fSJohnny Huang #include <linux/compiler.h>
18696656c6SJohnny Huang #include <u-boot/sha256.h>
190cee9a95SJohnny Huang #include "otp_info.h"
2069d5fd8fSJohnny Huang 
2169d5fd8fSJohnny Huang DECLARE_GLOBAL_DATA_PTR;
2269d5fd8fSJohnny Huang 
23f347c284SJohnny Huang #define OTP_VER				"1.1.0"
24f67375f7SJohnny Huang 
2569d5fd8fSJohnny Huang #define OTP_PASSWD			0x349fe38a
26dacbba92SJohnny Huang #define RETRY				20
277332532cSJohnny Huang #define OTP_REGION_STRAP		BIT(0)
287332532cSJohnny Huang #define OTP_REGION_CONF			BIT(1)
297332532cSJohnny Huang #define OTP_REGION_DATA			BIT(2)
3069d5fd8fSJohnny Huang 
312a856b9aSJohnny Huang #define OTP_USAGE			-1
322a856b9aSJohnny Huang #define OTP_FAILURE			-2
332a856b9aSJohnny Huang #define OTP_SUCCESS			0
342a856b9aSJohnny Huang 
35a6af4a17SJohnny Huang #define OTP_PROG_SKIP			1
36a6af4a17SJohnny Huang 
37181f72d8SJohnny Huang #define OTP_KEY_TYPE_RSA_PUB		1
38181f72d8SJohnny Huang #define OTP_KEY_TYPE_RSA_PRIV		2
39181f72d8SJohnny Huang #define OTP_KEY_TYPE_AES		3
40181f72d8SJohnny Huang #define OTP_KEY_TYPE_VAULT		4
41181f72d8SJohnny Huang #define OTP_KEY_TYPE_HMAC		5
429a4fe690SJohnny Huang 
434c1c9b35SJohnny Huang #define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
444c1c9b35SJohnny Huang #define PBWIDTH 60
454c1c9b35SJohnny Huang 
463d3688adSJohnny Huang #define OTP_BASE		0x1e6f2000
473d3688adSJohnny Huang #define OTP_PROTECT_KEY		OTP_BASE
483d3688adSJohnny Huang #define OTP_COMMAND		OTP_BASE + 0x4
493d3688adSJohnny Huang #define OTP_TIMING		OTP_BASE + 0x8
503d3688adSJohnny Huang #define OTP_ADDR		OTP_BASE + 0x10
513d3688adSJohnny Huang #define OTP_STATUS		OTP_BASE + 0x14
523d3688adSJohnny Huang #define OTP_COMPARE_1		OTP_BASE + 0x20
533d3688adSJohnny Huang #define OTP_COMPARE_2		OTP_BASE + 0x24
543d3688adSJohnny Huang #define OTP_COMPARE_3		OTP_BASE + 0x28
553d3688adSJohnny Huang #define OTP_COMPARE_4		OTP_BASE + 0x2c
56a8789b47SJohnny Huang #define SW_REV_ID0		OTP_BASE + 0x68
57a8789b47SJohnny Huang #define SW_REV_ID1		OTP_BASE + 0x6c
583d3688adSJohnny Huang 
59696656c6SJohnny Huang #define OTP_MAGIC		"SOCOTP"
60696656c6SJohnny Huang #define CHECKSUM_LEN		32
61a219f6deSJohnny Huang #define OTP_INC_DATA		BIT(31)
62a219f6deSJohnny Huang #define OTP_INC_CONFIG		BIT(30)
63a219f6deSJohnny Huang #define OTP_INC_STRAP		BIT(29)
64a219f6deSJohnny Huang #define OTP_ECC_EN		BIT(28)
65696656c6SJohnny Huang #define OTP_REGION_SIZE(info)	((info >> 16) & 0xffff)
66696656c6SJohnny Huang #define OTP_REGION_OFFSET(info)	(info & 0xffff)
67696656c6SJohnny Huang #define OTP_IMAGE_SIZE(info)	(info & 0xffff)
68696656c6SJohnny Huang 
69e417205bSJohnny Huang #define OTP_A0		0
70e417205bSJohnny Huang #define OTP_A1		1
71e417205bSJohnny Huang #define OTP_A2		2
72e417205bSJohnny Huang #define OTP_A3		3
73e417205bSJohnny Huang 
74e417205bSJohnny Huang #define ID0_AST2600A0	0x05000303
75e417205bSJohnny Huang #define ID1_AST2600A0	0x05000303
76e417205bSJohnny Huang #define ID0_AST2600A1	0x05010303
77*21a8cfceSJohnny Huang #define ID1_AST2600A1	0x05010303
78e417205bSJohnny Huang #define ID0_AST2600A2	0x05010303
79e417205bSJohnny Huang #define ID1_AST2600A2	0x05020303
80e417205bSJohnny Huang #define ID0_AST2600A3	0x05030303
81e417205bSJohnny Huang #define ID1_AST2600A3	0x05030303
82e417205bSJohnny Huang #define ID0_AST2620A1	0x05010203
83e417205bSJohnny Huang #define ID1_AST2620A1	0x05010203
84e417205bSJohnny Huang #define ID0_AST2620A2	0x05010203
85e417205bSJohnny Huang #define ID1_AST2620A2	0x05020203
86e417205bSJohnny Huang #define ID0_AST2620A3	0x05030203
87e417205bSJohnny Huang #define ID1_AST2620A3	0x05030203
88e417205bSJohnny Huang #define ID0_AST2620A3	0x05030203
89e417205bSJohnny Huang #define ID1_AST2620A3	0x05030203
90e417205bSJohnny Huang #define ID0_AST2605A2	0x05010103
91e417205bSJohnny Huang #define ID1_AST2605A2	0x05020103
92e417205bSJohnny Huang #define ID0_AST2605A3	0x05030103
93e417205bSJohnny Huang #define ID1_AST2605A3	0x05030103
94e417205bSJohnny Huang #define ID0_AST2625A3	0x05030403
95e417205bSJohnny Huang #define ID1_AST2625A3	0x05030403
96696656c6SJohnny Huang 
97696656c6SJohnny Huang struct otp_header {
98696656c6SJohnny Huang 	u8	otp_magic[8];
99696656c6SJohnny Huang 	u8	otp_version[8];
100696656c6SJohnny Huang 	u32	image_info;
101696656c6SJohnny Huang 	u32	data_info;
102696656c6SJohnny Huang 	u32	config_info;
103696656c6SJohnny Huang 	u32	strap_info;
104696656c6SJohnny Huang 	u32	checksum_offset;
105a219f6deSJohnny Huang } __packed;
106696656c6SJohnny Huang 
10766f2f8e5SJohnny Huang struct otpstrap_status {
10869d5fd8fSJohnny Huang 	int value;
10969d5fd8fSJohnny Huang 	int option_array[7];
11069d5fd8fSJohnny Huang 	int remain_times;
11169d5fd8fSJohnny Huang 	int writeable_option;
1125010032bSJohnny Huang 	int reg_protected;
11369d5fd8fSJohnny Huang 	int protected;
11469d5fd8fSJohnny Huang };
11569d5fd8fSJohnny Huang 
11666f2f8e5SJohnny Huang struct otpconf_parse {
11766f2f8e5SJohnny Huang 	int dw_offset;
11866f2f8e5SJohnny Huang 	int bit;
11966f2f8e5SJohnny Huang 	int length;
12066f2f8e5SJohnny Huang 	int value;
121696656c6SJohnny Huang 	int ignore;
12266f2f8e5SJohnny Huang 	char status[80];
12366f2f8e5SJohnny Huang };
12466f2f8e5SJohnny Huang 
1259a4fe690SJohnny Huang struct otpkey_type {
1269a4fe690SJohnny Huang 	int value;
1279a4fe690SJohnny Huang 	int key_type;
1289a4fe690SJohnny Huang 	int need_id;
1299a4fe690SJohnny Huang 	char information[110];
1309a4fe690SJohnny Huang };
1319a4fe690SJohnny Huang 
1329a4fe690SJohnny Huang struct otp_info_cb {
1339a4fe690SJohnny Huang 	int version;
134e417205bSJohnny Huang 	char ver_name[3];
13579e42a59SJoel Stanley 	const struct otpstrap_info *strap_info;
1369a4fe690SJohnny Huang 	int strap_info_len;
13779e42a59SJoel Stanley 	const struct otpconf_info *conf_info;
1389a4fe690SJohnny Huang 	int conf_info_len;
13979e42a59SJoel Stanley 	const struct otpkey_type *key_info;
1409a4fe690SJohnny Huang 	int key_info_len;
1419a4fe690SJohnny Huang };
1429a4fe690SJohnny Huang 
143696656c6SJohnny Huang struct otp_image_layout {
1445010032bSJohnny Huang 	int data_length;
1455010032bSJohnny Huang 	int conf_length;
1465010032bSJohnny Huang 	int strap_length;
147a219f6deSJohnny Huang 	u8 *data;
148a219f6deSJohnny Huang 	u8 *data_ignore;
149a219f6deSJohnny Huang 	u8 *conf;
150a219f6deSJohnny Huang 	u8 *conf_ignore;
151a219f6deSJohnny Huang 	u8 *strap;
152a219f6deSJohnny Huang 	u8 *strap_reg_pro;
153a219f6deSJohnny Huang 	u8 *strap_pro;
154a219f6deSJohnny Huang 	u8 *strap_ignore;
155696656c6SJohnny Huang };
156696656c6SJohnny Huang 
1579a4fe690SJohnny Huang static struct otp_info_cb info_cb;
1589a4fe690SJohnny Huang 
15979e42a59SJoel Stanley static const struct otpkey_type a0_key_type[] = {
1609a4fe690SJohnny Huang 	{0, OTP_KEY_TYPE_AES,   0, "AES-256 as OEM platform key for image encryption/decryption"},
1619a4fe690SJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
1629a4fe690SJohnny Huang 	{4, OTP_KEY_TYPE_HMAC,  1, "HMAC as encrypted OEM HMAC keys in Mode 1"},
163181f72d8SJohnny Huang 	{8, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2"},
164181f72d8SJohnny Huang 	{9, OTP_KEY_TYPE_RSA_PUB,   0, "RSA-public as SOC public key"},
165181f72d8SJohnny Huang 	{10, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key"},
166181f72d8SJohnny Huang 	{13, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as SOC private key"},
167181f72d8SJohnny Huang 	{14, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key"},
1689a4fe690SJohnny Huang };
1699a4fe690SJohnny Huang 
17079e42a59SJoel Stanley static const struct otpkey_type a1_key_type[] = {
1719a4fe690SJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
1729a4fe690SJohnny Huang 	{2, OTP_KEY_TYPE_AES,   1, "AES-256 as OEM platform key for image encryption/decryption in Mode 2 or AES-256 as OEM DSS keys for Mode GCM"},
173181f72d8SJohnny Huang 	{8, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2"},
174181f72d8SJohnny Huang 	{10, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key"},
175181f72d8SJohnny Huang 	{14, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key"},
1769a4fe690SJohnny Huang };
1779a4fe690SJohnny Huang 
1785fdde29fSJohnny Huang static const struct otpkey_type a2_key_type[] = {
1795fdde29fSJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
1805fdde29fSJohnny Huang 	{2, OTP_KEY_TYPE_AES,   1, "AES-256 as OEM platform key for image encryption/decryption in Mode 2 or AES-256 as OEM DSS keys for Mode GCM"},
181181f72d8SJohnny Huang 	{8, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2"},
182181f72d8SJohnny Huang 	{10, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key"},
183181f72d8SJohnny Huang 	{14, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key"},
184181f72d8SJohnny Huang };
185181f72d8SJohnny Huang 
186181f72d8SJohnny Huang static const struct otpkey_type a3_key_type[] = {
187181f72d8SJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
188181f72d8SJohnny Huang 	{2, OTP_KEY_TYPE_AES,   1, "AES-256 as OEM platform key for image encryption/decryption in Mode 2 or AES-256 as OEM DSS keys for Mode GCM"},
189181f72d8SJohnny Huang 	{8, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2"},
190181f72d8SJohnny Huang 	{9, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2(big endian)"},
191181f72d8SJohnny Huang 	{10, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key"},
192181f72d8SJohnny Huang 	{11, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key(big endian)"},
193181f72d8SJohnny Huang 	{12, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key"},
194181f72d8SJohnny Huang 	{13, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key(big endian)"},
1955fdde29fSJohnny Huang };
1965fdde29fSJohnny Huang 
197f347c284SJohnny Huang static void buf_print(u8 *buf, int len)
198f347c284SJohnny Huang {
199f347c284SJohnny Huang 	int i;
200f347c284SJohnny Huang 
201f347c284SJohnny Huang 	printf("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
202f347c284SJohnny Huang 	for (i = 0; i < len; i++) {
203f347c284SJohnny Huang 		if (i % 16 == 0)
204f347c284SJohnny Huang 			printf("%04X: ", i);
205f347c284SJohnny Huang 		printf("%02X ", buf[i]);
206f347c284SJohnny Huang 		if ((i + 1) % 16 == 0)
207f347c284SJohnny Huang 			printf("\n");
208f347c284SJohnny Huang 	}
209f347c284SJohnny Huang }
210f347c284SJohnny Huang 
211794e27ecSJohnny Huang static int get_dw_bit(u32 *rid, int offset)
212794e27ecSJohnny Huang {
213794e27ecSJohnny Huang 	int bit_offset;
214794e27ecSJohnny Huang 	int i;
215794e27ecSJohnny Huang 
216794e27ecSJohnny Huang 	if (offset < 32) {
217794e27ecSJohnny Huang 		i = 0;
218794e27ecSJohnny Huang 		bit_offset = offset;
219794e27ecSJohnny Huang 	} else {
220794e27ecSJohnny Huang 		i = 1;
221794e27ecSJohnny Huang 		bit_offset = offset - 32;
222794e27ecSJohnny Huang 	}
223794e27ecSJohnny Huang 	if ((rid[i] >> bit_offset) & 0x1)
224794e27ecSJohnny Huang 		return 1;
225794e27ecSJohnny Huang 	else
226794e27ecSJohnny Huang 		return 0;
227794e27ecSJohnny Huang }
228794e27ecSJohnny Huang 
229794e27ecSJohnny Huang static int get_rid_num(u32 *rid)
230794e27ecSJohnny Huang {
231794e27ecSJohnny Huang 	int i;
232794e27ecSJohnny Huang 	int fz = 0;
233794e27ecSJohnny Huang 	int rid_num = 0;
234794e27ecSJohnny Huang 	int ret = 0;
235794e27ecSJohnny Huang 
236794e27ecSJohnny Huang 	for (i = 0; i < 64; i++) {
237794e27ecSJohnny Huang 		if (get_dw_bit(rid, i) == 0) {
238794e27ecSJohnny Huang 			if (!fz)
239794e27ecSJohnny Huang 				fz = 1;
240794e27ecSJohnny Huang 
241794e27ecSJohnny Huang 		} else {
242794e27ecSJohnny Huang 			rid_num++;
243794e27ecSJohnny Huang 			if (fz)
244794e27ecSJohnny Huang 				ret = OTP_FAILURE;
245794e27ecSJohnny Huang 		}
246794e27ecSJohnny Huang 	}
247794e27ecSJohnny Huang 	if (ret)
248794e27ecSJohnny Huang 		return ret;
249794e27ecSJohnny Huang 
250794e27ecSJohnny Huang 	return rid_num;
251794e27ecSJohnny Huang }
252794e27ecSJohnny Huang 
253a219f6deSJohnny Huang static u32 chip_version(void)
2549a4fe690SJohnny Huang {
255e417205bSJohnny Huang 	u32 revid0, revid1;
2569a4fe690SJohnny Huang 
257e417205bSJohnny Huang 	revid0 = readl(ASPEED_REVISION_ID0);
258e417205bSJohnny Huang 	revid1 = readl(ASPEED_REVISION_ID1);
2599a4fe690SJohnny Huang 
260e417205bSJohnny Huang 	if (revid0 == ID0_AST2600A0 && revid1 == ID1_AST2600A0) {
261badd21c2SJohnny Huang 		/* AST2600-A0 */
262e417205bSJohnny Huang 		return OTP_A0;
263e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2600A1 && revid1 == ID1_AST2600A1) {
264badd21c2SJohnny Huang 		/* AST2600-A1 */
265e417205bSJohnny Huang 		return OTP_A1;
266e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2600A2 && revid1 == ID1_AST2600A2) {
267badd21c2SJohnny Huang 		/* AST2600-A2 */
268e417205bSJohnny Huang 		return OTP_A2;
269e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2600A3 && revid1 == ID1_AST2600A3) {
27064b66712SJohnny Huang 		/* AST2600-A3 */
271e417205bSJohnny Huang 		return OTP_A3;
272e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2620A1 && revid1 == ID1_AST2620A1) {
273e417205bSJohnny Huang 		/* AST2620-A1 */
274e417205bSJohnny Huang 		return OTP_A1;
275e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2620A2 && revid1 == ID1_AST2620A2) {
276e417205bSJohnny Huang 		/* AST2620-A2 */
277e417205bSJohnny Huang 		return OTP_A2;
278e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2620A3 && revid1 == ID1_AST2620A3) {
27964b66712SJohnny Huang 		/* AST2620-A3 */
280e417205bSJohnny Huang 		return OTP_A3;
281e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2605A2 && revid1 == ID1_AST2605A2) {
282e417205bSJohnny Huang 		/* AST2605-A2 */
283e417205bSJohnny Huang 		return OTP_A2;
284e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2605A3 && revid1 == ID1_AST2605A3) {
285e417205bSJohnny Huang 		/* AST2605-A3 */
286e417205bSJohnny Huang 		return OTP_A3;
287e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2625A3 && revid1 == ID1_AST2625A3) {
288e417205bSJohnny Huang 		/* AST2605-A3 */
289e417205bSJohnny Huang 		return OTP_A3;
2900dae9d52SJohnny Huang 	}
291f347c284SJohnny Huang 	return OTP_FAILURE;
2929a4fe690SJohnny Huang }
2939a4fe690SJohnny Huang 
2943d3688adSJohnny Huang static void wait_complete(void)
2953d3688adSJohnny Huang {
2963d3688adSJohnny Huang 	int reg;
2973d3688adSJohnny Huang 
2983d3688adSJohnny Huang 	do {
2993d3688adSJohnny Huang 		reg = readl(OTP_STATUS);
3003d3688adSJohnny Huang 	} while ((reg & 0x6) != 0x6);
3013d3688adSJohnny Huang }
3023d3688adSJohnny Huang 
303a219f6deSJohnny Huang static void otp_write(u32 otp_addr, u32 data)
304dacbba92SJohnny Huang {
305dacbba92SJohnny Huang 	writel(otp_addr, OTP_ADDR); //write address
306dacbba92SJohnny Huang 	writel(data, OTP_COMPARE_1); //write data
307dacbba92SJohnny Huang 	writel(0x23b1e362, OTP_COMMAND); //write command
308dacbba92SJohnny Huang 	wait_complete();
309dacbba92SJohnny Huang }
310dacbba92SJohnny Huang 
311dacbba92SJohnny Huang static void otp_soak(int soak)
312dacbba92SJohnny Huang {
313e417205bSJohnny Huang 	if (info_cb.version == OTP_A2 || info_cb.version == OTP_A3) {
314dacbba92SJohnny Huang 		switch (soak) {
315dacbba92SJohnny Huang 		case 0: //default
316dacbba92SJohnny Huang 			otp_write(0x3000, 0x0210); // Write MRA
317dacbba92SJohnny Huang 			otp_write(0x5000, 0x2000); // Write MRB
318dacbba92SJohnny Huang 			otp_write(0x1000, 0x0); // Write MR
319dacbba92SJohnny Huang 			break;
320dacbba92SJohnny Huang 		case 1: //normal program
321dacbba92SJohnny Huang 			otp_write(0x3000, 0x1200); // Write MRA
322feea3fdfSJohnny Huang 			otp_write(0x5000, 0x107F); // Write MRB
323dacbba92SJohnny Huang 			otp_write(0x1000, 0x1024); // Write MR
324feea3fdfSJohnny Huang 			writel(0x04191388, OTP_TIMING); // 200us
325dacbba92SJohnny Huang 			break;
326dacbba92SJohnny Huang 		case 2: //soak program
327dacbba92SJohnny Huang 			otp_write(0x3000, 0x1220); // Write MRA
328feea3fdfSJohnny Huang 			otp_write(0x5000, 0x2074); // Write MRB
329dacbba92SJohnny Huang 			otp_write(0x1000, 0x08a4); // Write MR
330feea3fdfSJohnny Huang 			writel(0x04193a98, OTP_TIMING); // 600us
331dacbba92SJohnny Huang 			break;
332dacbba92SJohnny Huang 		}
333dacbba92SJohnny Huang 	} else {
334dacbba92SJohnny Huang 		switch (soak) {
335dacbba92SJohnny Huang 		case 0: //default
336dacbba92SJohnny Huang 			otp_write(0x3000, 0x0); // Write MRA
337dacbba92SJohnny Huang 			otp_write(0x5000, 0x0); // Write MRB
338dacbba92SJohnny Huang 			otp_write(0x1000, 0x0); // Write MR
339dacbba92SJohnny Huang 			break;
340dacbba92SJohnny Huang 		case 1: //normal program
341dacbba92SJohnny Huang 			otp_write(0x3000, 0x4021); // Write MRA
342dacbba92SJohnny Huang 			otp_write(0x5000, 0x302f); // Write MRB
343dacbba92SJohnny Huang 			otp_write(0x1000, 0x4020); // Write MR
344feea3fdfSJohnny Huang 			writel(0x04190760, OTP_TIMING); // 75us
345dacbba92SJohnny Huang 			break;
346dacbba92SJohnny Huang 		case 2: //soak program
347dacbba92SJohnny Huang 			otp_write(0x3000, 0x4021); // Write MRA
348dacbba92SJohnny Huang 			otp_write(0x5000, 0x1027); // Write MRB
349dacbba92SJohnny Huang 			otp_write(0x1000, 0x4820); // Write MR
350feea3fdfSJohnny Huang 			writel(0x041930d4, OTP_TIMING); // 500us
351dacbba92SJohnny Huang 			break;
352dacbba92SJohnny Huang 		}
353dacbba92SJohnny Huang 	}
354dacbba92SJohnny Huang 
355dacbba92SJohnny Huang 	wait_complete();
356dacbba92SJohnny Huang }
357dacbba92SJohnny Huang 
358a219f6deSJohnny Huang static void otp_read_data(u32 offset, u32 *data)
35969d5fd8fSJohnny Huang {
3603d3688adSJohnny Huang 	writel(offset, OTP_ADDR); //Read address
3613d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
3623d3688adSJohnny Huang 	wait_complete();
3633d3688adSJohnny Huang 	data[0] = readl(OTP_COMPARE_1);
3643d3688adSJohnny Huang 	data[1] = readl(OTP_COMPARE_2);
36569d5fd8fSJohnny Huang }
36669d5fd8fSJohnny Huang 
367f347c284SJohnny Huang static void otp_read_conf(u32 offset, u32 *data)
36869d5fd8fSJohnny Huang {
36969d5fd8fSJohnny Huang 	int config_offset;
37069d5fd8fSJohnny Huang 
37169d5fd8fSJohnny Huang 	config_offset = 0x800;
37269d5fd8fSJohnny Huang 	config_offset |= (offset / 8) * 0x200;
37369d5fd8fSJohnny Huang 	config_offset |= (offset % 8) * 0x2;
37469d5fd8fSJohnny Huang 
3753d3688adSJohnny Huang 	writel(config_offset, OTP_ADDR);  //Read address
3763d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
3773d3688adSJohnny Huang 	wait_complete();
3783d3688adSJohnny Huang 	data[0] = readl(OTP_COMPARE_1);
37969d5fd8fSJohnny Huang }
38069d5fd8fSJohnny Huang 
381a219f6deSJohnny Huang static int otp_compare(u32 otp_addr, u32 addr)
38269d5fd8fSJohnny Huang {
383a219f6deSJohnny Huang 	u32 ret;
384a219f6deSJohnny Huang 	u32 *buf;
38569d5fd8fSJohnny Huang 
38669d5fd8fSJohnny Huang 	buf = map_physmem(addr, 16, MAP_WRBACK);
38769d5fd8fSJohnny Huang 	printf("%08X\n", buf[0]);
38869d5fd8fSJohnny Huang 	printf("%08X\n", buf[1]);
38969d5fd8fSJohnny Huang 	printf("%08X\n", buf[2]);
39069d5fd8fSJohnny Huang 	printf("%08X\n", buf[3]);
3913d3688adSJohnny Huang 	writel(otp_addr, OTP_ADDR); //Compare address
3923d3688adSJohnny Huang 	writel(buf[0], OTP_COMPARE_1); //Compare data 1
3933d3688adSJohnny Huang 	writel(buf[1], OTP_COMPARE_2); //Compare data 2
3943d3688adSJohnny Huang 	writel(buf[2], OTP_COMPARE_3); //Compare data 3
3953d3688adSJohnny Huang 	writel(buf[3], OTP_COMPARE_4); //Compare data 4
3963d3688adSJohnny Huang 	writel(0x23b1e363, OTP_COMMAND); //Compare command
3973d3688adSJohnny Huang 	wait_complete();
3983d3688adSJohnny Huang 	ret = readl(OTP_STATUS); //Compare command
39969d5fd8fSJohnny Huang 	if (ret & 0x1)
400f347c284SJohnny Huang 		return OTP_SUCCESS;
40169d5fd8fSJohnny Huang 	else
402f347c284SJohnny Huang 		return OTP_FAILURE;
40369d5fd8fSJohnny Huang }
40469d5fd8fSJohnny Huang 
405a219f6deSJohnny Huang static int verify_bit(u32 otp_addr, int bit_offset, int value)
40669d5fd8fSJohnny Huang {
407a219f6deSJohnny Huang 	u32 ret[2];
40869d5fd8fSJohnny Huang 
40930a8c590SJohnny Huang 	if (otp_addr % 2 == 0)
4103d3688adSJohnny Huang 		writel(otp_addr, OTP_ADDR); //Read address
41130a8c590SJohnny Huang 	else
4123d3688adSJohnny Huang 		writel(otp_addr - 1, OTP_ADDR); //Read address
41330a8c590SJohnny Huang 
4143d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
4153d3688adSJohnny Huang 	wait_complete();
4163d3688adSJohnny Huang 	ret[0] = readl(OTP_COMPARE_1);
4173d3688adSJohnny Huang 	ret[1] = readl(OTP_COMPARE_2);
41883655e91SJohnny Huang 
41930a8c590SJohnny Huang 	if (otp_addr % 2 == 0) {
42030a8c590SJohnny Huang 		if (((ret[0] >> bit_offset) & 1) == value)
421f347c284SJohnny Huang 			return OTP_SUCCESS;
42269d5fd8fSJohnny Huang 		else
423f347c284SJohnny Huang 			return OTP_FAILURE;
42430a8c590SJohnny Huang 	} else {
42530a8c590SJohnny Huang 		if (((ret[1] >> bit_offset) & 1) == value)
426f347c284SJohnny Huang 			return OTP_SUCCESS;
42730a8c590SJohnny Huang 		else
428f347c284SJohnny Huang 			return OTP_FAILURE;
42930a8c590SJohnny Huang 	}
43069d5fd8fSJohnny Huang }
43169d5fd8fSJohnny Huang 
432a219f6deSJohnny Huang static u32 verify_dw(u32 otp_addr, u32 *value, u32 *ignore, u32 *compare, int size)
4334c1c9b35SJohnny Huang {
434a219f6deSJohnny Huang 	u32 ret[2];
4354c1c9b35SJohnny Huang 
4364c1c9b35SJohnny Huang 	otp_addr &= ~(1 << 15);
4374c1c9b35SJohnny Huang 
4384c1c9b35SJohnny Huang 	if (otp_addr % 2 == 0)
4393d3688adSJohnny Huang 		writel(otp_addr, OTP_ADDR); //Read address
4404c1c9b35SJohnny Huang 	else
4413d3688adSJohnny Huang 		writel(otp_addr - 1, OTP_ADDR); //Read address
4423d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
4433d3688adSJohnny Huang 	wait_complete();
4443d3688adSJohnny Huang 	ret[0] = readl(OTP_COMPARE_1);
4453d3688adSJohnny Huang 	ret[1] = readl(OTP_COMPARE_2);
4464c1c9b35SJohnny Huang 	if (size == 1) {
4474c1c9b35SJohnny Huang 		if (otp_addr % 2 == 0) {
4484c1c9b35SJohnny Huang 			// printf("check %x : %x = %x\n", otp_addr, ret[0], value[0]);
449696656c6SJohnny Huang 			if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0])) {
4504c1c9b35SJohnny Huang 				compare[0] = 0;
451f347c284SJohnny Huang 				return OTP_SUCCESS;
452a219f6deSJohnny Huang 			}
4534c1c9b35SJohnny Huang 			compare[0] = value[0] ^ ret[0];
454f347c284SJohnny Huang 			return OTP_FAILURE;
4554c1c9b35SJohnny Huang 
4564c1c9b35SJohnny Huang 		} else {
4574c1c9b35SJohnny Huang 			// printf("check %x : %x = %x\n", otp_addr, ret[1], value[0]);
458696656c6SJohnny Huang 			if ((value[0] & ~ignore[0]) == (ret[1] & ~ignore[0])) {
4594c1c9b35SJohnny Huang 				compare[0] = ~0;
460f347c284SJohnny Huang 				return OTP_SUCCESS;
461a219f6deSJohnny Huang 			}
462d90825e2SJohnny Huang 			compare[0] = ~(value[0] ^ ret[1]);
463f347c284SJohnny Huang 			return OTP_FAILURE;
4644c1c9b35SJohnny Huang 		}
4654c1c9b35SJohnny Huang 	} else if (size == 2) {
4664c1c9b35SJohnny Huang 		// otp_addr should be even
467696656c6SJohnny Huang 		if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0]) && (value[1] & ~ignore[1]) == (ret[1] & ~ignore[1])) {
4684c1c9b35SJohnny Huang 			// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
4694c1c9b35SJohnny Huang 			// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
4704c1c9b35SJohnny Huang 			compare[0] = 0;
4714c1c9b35SJohnny Huang 			compare[1] = ~0;
472f347c284SJohnny Huang 			return OTP_SUCCESS;
473a219f6deSJohnny Huang 		}
4744c1c9b35SJohnny Huang 		// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
4754c1c9b35SJohnny Huang 		// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
4764c1c9b35SJohnny Huang 		compare[0] = value[0] ^ ret[0];
4774c1c9b35SJohnny Huang 		compare[1] = ~(value[1] ^ ret[1]);
478f347c284SJohnny Huang 		return OTP_FAILURE;
4794c1c9b35SJohnny Huang 	} else {
480f347c284SJohnny Huang 		return OTP_FAILURE;
4814c1c9b35SJohnny Huang 	}
4824c1c9b35SJohnny Huang }
4834c1c9b35SJohnny Huang 
484a219f6deSJohnny Huang static void otp_prog(u32 otp_addr, u32 prog_bit)
48583655e91SJohnny Huang {
48690965bb3SJohnny Huang 	otp_write(0x0, prog_bit);
48783655e91SJohnny Huang 	writel(otp_addr, OTP_ADDR); //write address
48883655e91SJohnny Huang 	writel(prog_bit, OTP_COMPARE_1); //write data
48983655e91SJohnny Huang 	writel(0x23b1e364, OTP_COMMAND); //write command
49083655e91SJohnny Huang 	wait_complete();
49183655e91SJohnny Huang }
49283655e91SJohnny Huang 
493a219f6deSJohnny Huang static void _otp_prog_bit(u32 value, u32 prog_address, u32 bit_offset)
49483655e91SJohnny Huang {
49583655e91SJohnny Huang 	int prog_bit;
49683655e91SJohnny Huang 
49783655e91SJohnny Huang 	if (prog_address % 2 == 0) {
49883655e91SJohnny Huang 		if (value)
49983655e91SJohnny Huang 			prog_bit = ~(0x1 << bit_offset);
50083655e91SJohnny Huang 		else
50183655e91SJohnny Huang 			return;
50283655e91SJohnny Huang 	} else {
503e417205bSJohnny Huang 		if (info_cb.version != OTP_A3)
50483655e91SJohnny Huang 			prog_address |= 1 << 15;
50583655e91SJohnny Huang 		if (!value)
50683655e91SJohnny Huang 			prog_bit = 0x1 << bit_offset;
50783655e91SJohnny Huang 		else
50883655e91SJohnny Huang 			return;
50983655e91SJohnny Huang 	}
51083655e91SJohnny Huang 	otp_prog(prog_address, prog_bit);
51183655e91SJohnny Huang }
51283655e91SJohnny Huang 
513f347c284SJohnny Huang static int otp_prog_dc_b(u32 value, u32 prog_address, u32 bit_offset)
51483655e91SJohnny Huang {
51583655e91SJohnny Huang 	int pass;
51683655e91SJohnny Huang 	int i;
51783655e91SJohnny Huang 
51883655e91SJohnny Huang 	otp_soak(1);
51983655e91SJohnny Huang 	_otp_prog_bit(value, prog_address, bit_offset);
52083655e91SJohnny Huang 	pass = 0;
52183655e91SJohnny Huang 
52283655e91SJohnny Huang 	for (i = 0; i < RETRY; i++) {
52383655e91SJohnny Huang 		if (verify_bit(prog_address, bit_offset, value) != 0) {
52483655e91SJohnny Huang 			otp_soak(2);
52583655e91SJohnny Huang 			_otp_prog_bit(value, prog_address, bit_offset);
52683655e91SJohnny Huang 			if (verify_bit(prog_address, bit_offset, value) != 0) {
52783655e91SJohnny Huang 				otp_soak(1);
52883655e91SJohnny Huang 			} else {
52983655e91SJohnny Huang 				pass = 1;
53083655e91SJohnny Huang 				break;
53183655e91SJohnny Huang 			}
53283655e91SJohnny Huang 		} else {
53383655e91SJohnny Huang 			pass = 1;
53483655e91SJohnny Huang 			break;
53583655e91SJohnny Huang 		}
53683655e91SJohnny Huang 	}
537794e27ecSJohnny Huang 	if (pass)
538794e27ecSJohnny Huang 		return OTP_SUCCESS;
53983655e91SJohnny Huang 
540794e27ecSJohnny Huang 	return OTP_FAILURE;
54183655e91SJohnny Huang }
54283655e91SJohnny Huang 
543a219f6deSJohnny Huang static void otp_prog_dw(u32 value, u32 ignore, u32 prog_address)
544d90825e2SJohnny Huang {
545d90825e2SJohnny Huang 	int j, bit_value, prog_bit;
546d90825e2SJohnny Huang 
547d90825e2SJohnny Huang 	for (j = 0; j < 32; j++) {
548696656c6SJohnny Huang 		if ((ignore >> j) & 0x1)
549d90825e2SJohnny Huang 			continue;
550d90825e2SJohnny Huang 		bit_value = (value >> j) & 0x1;
551d90825e2SJohnny Huang 		if (prog_address % 2 == 0) {
552d90825e2SJohnny Huang 			if (bit_value)
553d90825e2SJohnny Huang 				prog_bit = ~(0x1 << j);
554d90825e2SJohnny Huang 			else
555d90825e2SJohnny Huang 				continue;
556d90825e2SJohnny Huang 		} else {
557e417205bSJohnny Huang 			if (info_cb.version != OTP_A3)
558d90825e2SJohnny Huang 				prog_address |= 1 << 15;
559d90825e2SJohnny Huang 			if (bit_value)
560d90825e2SJohnny Huang 				continue;
561d90825e2SJohnny Huang 			else
562d90825e2SJohnny Huang 				prog_bit = 0x1 << j;
563d90825e2SJohnny Huang 		}
564d90825e2SJohnny Huang 		otp_prog(prog_address, prog_bit);
565d90825e2SJohnny Huang 	}
566d90825e2SJohnny Huang }
567d90825e2SJohnny Huang 
568a219f6deSJohnny Huang static int otp_prog_verify_2dw(u32 *data, u32 *buf, u32 *ignore_mask, u32 prog_address)
56954552c69SJohnny Huang {
57054552c69SJohnny Huang 	int pass;
57154552c69SJohnny Huang 	int i;
572a219f6deSJohnny Huang 	u32 data0_masked;
573a219f6deSJohnny Huang 	u32 data1_masked;
574a219f6deSJohnny Huang 	u32 buf0_masked;
575a219f6deSJohnny Huang 	u32 buf1_masked;
576a219f6deSJohnny Huang 	u32 compare[2];
57754552c69SJohnny Huang 
57854552c69SJohnny Huang 	data0_masked = data[0]  & ~ignore_mask[0];
57954552c69SJohnny Huang 	buf0_masked  = buf[0] & ~ignore_mask[0];
58054552c69SJohnny Huang 	data1_masked = data[1]  & ~ignore_mask[1];
58154552c69SJohnny Huang 	buf1_masked  = buf[1] & ~ignore_mask[1];
582a219f6deSJohnny Huang 	if (data0_masked == buf0_masked && data1_masked == buf1_masked)
583f347c284SJohnny Huang 		return OTP_SUCCESS;
58454552c69SJohnny Huang 
58554552c69SJohnny Huang 	otp_soak(1);
58654552c69SJohnny Huang 	if (data0_masked != buf0_masked)
58754552c69SJohnny Huang 		otp_prog_dw(buf[0], ignore_mask[0], prog_address);
58854552c69SJohnny Huang 	if (data1_masked != buf1_masked)
58954552c69SJohnny Huang 		otp_prog_dw(buf[1], ignore_mask[1], prog_address + 1);
59054552c69SJohnny Huang 
59154552c69SJohnny Huang 	pass = 0;
59254552c69SJohnny Huang 	for (i = 0; i < RETRY; i++) {
59354552c69SJohnny Huang 		if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) {
59454552c69SJohnny Huang 			otp_soak(2);
595a219f6deSJohnny Huang 			if (compare[0] != 0)
59654552c69SJohnny Huang 				otp_prog_dw(compare[0], ignore_mask[0], prog_address);
597a219f6deSJohnny Huang 			if (compare[1] != ~0)
5985537bc72SJohnny Huang 				otp_prog_dw(compare[1], ignore_mask[1], prog_address + 1);
59954552c69SJohnny Huang 			if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) {
60054552c69SJohnny Huang 				otp_soak(1);
60154552c69SJohnny Huang 			} else {
60254552c69SJohnny Huang 				pass = 1;
60354552c69SJohnny Huang 				break;
60454552c69SJohnny Huang 			}
60554552c69SJohnny Huang 		} else {
60654552c69SJohnny Huang 			pass = 1;
60754552c69SJohnny Huang 			break;
60854552c69SJohnny Huang 		}
60954552c69SJohnny Huang 	}
61054552c69SJohnny Huang 
61154552c69SJohnny Huang 	if (!pass) {
61254552c69SJohnny Huang 		otp_soak(0);
61354552c69SJohnny Huang 		return OTP_FAILURE;
61454552c69SJohnny Huang 	}
61554552c69SJohnny Huang 	return OTP_SUCCESS;
61654552c69SJohnny Huang }
61754552c69SJohnny Huang 
618541eb887SJohnny Huang static void otp_strap_status(struct otpstrap_status *otpstrap)
61976d13988SJohnny Huang {
620a219f6deSJohnny Huang 	u32 OTPSTRAP_RAW[2];
6215010032bSJohnny Huang 	int strap_end;
62276d13988SJohnny Huang 	int i, j;
62376d13988SJohnny Huang 
624e417205bSJohnny Huang 	if (info_cb.version == OTP_A0) {
62576d13988SJohnny Huang 		for (j = 0; j < 64; j++) {
62676d13988SJohnny Huang 			otpstrap[j].value = 0;
62776d13988SJohnny Huang 			otpstrap[j].remain_times = 7;
62876d13988SJohnny Huang 			otpstrap[j].writeable_option = -1;
62976d13988SJohnny Huang 			otpstrap[j].protected = 0;
63076d13988SJohnny Huang 		}
6315010032bSJohnny Huang 		strap_end = 30;
6325010032bSJohnny Huang 	} else {
6335010032bSJohnny Huang 		for (j = 0; j < 64; j++) {
6345010032bSJohnny Huang 			otpstrap[j].value = 0;
6355010032bSJohnny Huang 			otpstrap[j].remain_times = 6;
6365010032bSJohnny Huang 			otpstrap[j].writeable_option = -1;
6375010032bSJohnny Huang 			otpstrap[j].reg_protected = 0;
6385010032bSJohnny Huang 			otpstrap[j].protected = 0;
6395010032bSJohnny Huang 		}
6405010032bSJohnny Huang 		strap_end = 28;
6415010032bSJohnny Huang 	}
64276d13988SJohnny Huang 
643dacbba92SJohnny Huang 	otp_soak(0);
6445010032bSJohnny Huang 	for (i = 16; i < strap_end; i += 2) {
64576d13988SJohnny Huang 		int option = (i - 16) / 2;
646a219f6deSJohnny Huang 
647f347c284SJohnny Huang 		otp_read_conf(i, &OTPSTRAP_RAW[0]);
648f347c284SJohnny Huang 		otp_read_conf(i + 1, &OTPSTRAP_RAW[1]);
64976d13988SJohnny Huang 		for (j = 0; j < 32; j++) {
65076d13988SJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[0] >> j) & 0x1);
651a219f6deSJohnny Huang 
652a219f6deSJohnny Huang 			if (bit_value == 0 && otpstrap[j].writeable_option == -1)
65376d13988SJohnny Huang 				otpstrap[j].writeable_option = option;
65476d13988SJohnny Huang 			if (bit_value == 1)
65576d13988SJohnny Huang 				otpstrap[j].remain_times--;
65676d13988SJohnny Huang 			otpstrap[j].value ^= bit_value;
65776d13988SJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
65876d13988SJohnny Huang 		}
65976d13988SJohnny Huang 		for (j = 32; j < 64; j++) {
66076d13988SJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1);
661a219f6deSJohnny Huang 
662a219f6deSJohnny Huang 			if (bit_value == 0 && otpstrap[j].writeable_option == -1)
66376d13988SJohnny Huang 				otpstrap[j].writeable_option = option;
66476d13988SJohnny Huang 			if (bit_value == 1)
66576d13988SJohnny Huang 				otpstrap[j].remain_times--;
66676d13988SJohnny Huang 			otpstrap[j].value ^= bit_value;
66776d13988SJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
66876d13988SJohnny Huang 		}
66976d13988SJohnny Huang 	}
6705010032bSJohnny Huang 
671e417205bSJohnny Huang 	if (info_cb.version != OTP_A0) {
672f347c284SJohnny Huang 		otp_read_conf(28, &OTPSTRAP_RAW[0]);
673f347c284SJohnny Huang 		otp_read_conf(29, &OTPSTRAP_RAW[1]);
6745010032bSJohnny Huang 		for (j = 0; j < 32; j++) {
6755010032bSJohnny Huang 			if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
6765010032bSJohnny Huang 				otpstrap[j].reg_protected = 1;
6775010032bSJohnny Huang 		}
6785010032bSJohnny Huang 		for (j = 32; j < 64; j++) {
6795010032bSJohnny Huang 			if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
6805010032bSJohnny Huang 				otpstrap[j].reg_protected = 1;
6815010032bSJohnny Huang 		}
6825010032bSJohnny Huang 	}
6835010032bSJohnny Huang 
684f347c284SJohnny Huang 	otp_read_conf(30, &OTPSTRAP_RAW[0]);
685f347c284SJohnny Huang 	otp_read_conf(31, &OTPSTRAP_RAW[1]);
68676d13988SJohnny Huang 	for (j = 0; j < 32; j++) {
68776d13988SJohnny Huang 		if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
68876d13988SJohnny Huang 			otpstrap[j].protected = 1;
68976d13988SJohnny Huang 	}
69076d13988SJohnny Huang 	for (j = 32; j < 64; j++) {
69176d13988SJohnny Huang 		if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
69276d13988SJohnny Huang 			otpstrap[j].protected = 1;
69376d13988SJohnny Huang 	}
69476d13988SJohnny Huang }
69576d13988SJohnny Huang 
696f347c284SJohnny Huang static int otp_strap_bit_confirm(struct otpstrap_status *otpstrap, int offset, int ibit, int bit, int pbit, int rpbit)
697f347c284SJohnny Huang {
698f347c284SJohnny Huang 	int prog_flag = 0;
699f347c284SJohnny Huang 
700f347c284SJohnny Huang 	// ignore this bit
701f347c284SJohnny Huang 	if (ibit == 1)
702f347c284SJohnny Huang 		return OTP_SUCCESS;
703f347c284SJohnny Huang 	printf("OTPSTRAP[%X]:\n", offset);
704f347c284SJohnny Huang 
705f347c284SJohnny Huang 	if (bit == otpstrap->value) {
706f347c284SJohnny Huang 		if (!pbit && !rpbit) {
707f347c284SJohnny Huang 			printf("    The value is same as before, skip it.\n");
708f347c284SJohnny Huang 			return OTP_PROG_SKIP;
709f347c284SJohnny Huang 		}
710f347c284SJohnny Huang 		printf("    The value is same as before.\n");
711f347c284SJohnny Huang 	} else {
712f347c284SJohnny Huang 		prog_flag = 1;
713f347c284SJohnny Huang 	}
714f347c284SJohnny Huang 	if (otpstrap->protected == 1 && prog_flag) {
715f347c284SJohnny Huang 		printf("    This bit is protected and is not writable\n");
716f347c284SJohnny Huang 		return OTP_FAILURE;
717f347c284SJohnny Huang 	}
718f347c284SJohnny Huang 	if (otpstrap->remain_times == 0 && prog_flag) {
719f347c284SJohnny Huang 		printf("    This bit is no remaining times to write.\n");
720f347c284SJohnny Huang 		return OTP_FAILURE;
721f347c284SJohnny Huang 	}
722f347c284SJohnny Huang 	if (pbit == 1)
723f347c284SJohnny Huang 		printf("    This bit will be protected and become non-writable.\n");
724f347c284SJohnny Huang 	if (rpbit == 1 && info_cb.version != OTP_A0)
725f347c284SJohnny Huang 		printf("    The relative register will be protected.\n");
726f347c284SJohnny Huang 	if (prog_flag)
727f347c284SJohnny Huang 		printf("    Write 1 to OTPSTRAP[%X] OPTION[%X], that value becomes from %d to %d.\n", offset, otpstrap->writeable_option + 1, otpstrap->value, otpstrap->value ^ 1);
728f347c284SJohnny Huang 
729f347c284SJohnny Huang 	return OTP_SUCCESS;
730f347c284SJohnny Huang }
731f347c284SJohnny Huang 
732f347c284SJohnny Huang static int otp_prog_strap_b(int bit_offset, int value)
733f347c284SJohnny Huang {
734f347c284SJohnny Huang 	struct otpstrap_status otpstrap[64];
735f347c284SJohnny Huang 	u32 prog_address;
736f347c284SJohnny Huang 	int offset;
737f347c284SJohnny Huang 	int ret;
738f347c284SJohnny Huang 
739f347c284SJohnny Huang 	otp_strap_status(otpstrap);
740f347c284SJohnny Huang 
741f347c284SJohnny Huang 	ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0, 0);
742f347c284SJohnny Huang 
743f347c284SJohnny Huang 	if (ret != OTP_SUCCESS)
744f347c284SJohnny Huang 		return ret;
745f347c284SJohnny Huang 
746f347c284SJohnny Huang 	prog_address = 0x800;
747f347c284SJohnny Huang 	if (bit_offset < 32) {
748f347c284SJohnny Huang 		offset = bit_offset;
749f347c284SJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) / 8) * 0x200;
750f347c284SJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) % 8) * 0x2;
751f347c284SJohnny Huang 
752f347c284SJohnny Huang 	} else {
753f347c284SJohnny Huang 		offset = (bit_offset - 32);
754f347c284SJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) / 8) * 0x200;
755f347c284SJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) % 8) * 0x2;
756f347c284SJohnny Huang 	}
757f347c284SJohnny Huang 
758f347c284SJohnny Huang 	return otp_prog_dc_b(1, prog_address, offset);
759f347c284SJohnny Huang }
760f347c284SJohnny Huang 
761f347c284SJohnny Huang static int otp_print_conf(u32 offset, int dw_count)
762f347c284SJohnny Huang {
763f347c284SJohnny Huang 	int i;
764f347c284SJohnny Huang 	u32 ret[1];
765f347c284SJohnny Huang 
766f347c284SJohnny Huang 	if (offset + dw_count > 32)
767f347c284SJohnny Huang 		return OTP_USAGE;
768f347c284SJohnny Huang 	otp_soak(0);
769f347c284SJohnny Huang 	for (i = offset; i < offset + dw_count; i++) {
770f347c284SJohnny Huang 		otp_read_conf(i, ret);
771f347c284SJohnny Huang 		printf("OTPCFG%X: %08X\n", i, ret[0]);
772f347c284SJohnny Huang 	}
773f347c284SJohnny Huang 	printf("\n");
774f347c284SJohnny Huang 	return OTP_SUCCESS;
775f347c284SJohnny Huang }
776f347c284SJohnny Huang 
777f347c284SJohnny Huang static int otp_print_data(u32 offset, int dw_count)
778f347c284SJohnny Huang {
779f347c284SJohnny Huang 	int i;
780f347c284SJohnny Huang 	u32 ret[2];
781f347c284SJohnny Huang 
782f347c284SJohnny Huang 	if (offset + dw_count > 2048 || offset % 4 != 0)
783f347c284SJohnny Huang 		return OTP_USAGE;
784f347c284SJohnny Huang 	otp_soak(0);
785f347c284SJohnny Huang 	for (i = offset; i < offset + dw_count; i += 2) {
786f347c284SJohnny Huang 		otp_read_data(i, ret);
787f347c284SJohnny Huang 		if (i % 4 == 0)
788f347c284SJohnny Huang 			printf("%03X: %08X %08X ", i * 4, ret[0], ret[1]);
789f347c284SJohnny Huang 		else
790f347c284SJohnny Huang 			printf("%08X %08X\n", ret[0], ret[1]);
791f347c284SJohnny Huang 	}
792f347c284SJohnny Huang 	printf("\n");
793f347c284SJohnny Huang 	return OTP_SUCCESS;
794f347c284SJohnny Huang }
795f347c284SJohnny Huang 
796f347c284SJohnny Huang static int otp_print_strap(int start, int count)
797f347c284SJohnny Huang {
798f347c284SJohnny Huang 	int i, j;
799f347c284SJohnny Huang 	int remains;
800f347c284SJohnny Huang 	struct otpstrap_status otpstrap[64];
801f347c284SJohnny Huang 
802f347c284SJohnny Huang 	if (start < 0 || start > 64)
803f347c284SJohnny Huang 		return OTP_USAGE;
804f347c284SJohnny Huang 
805f347c284SJohnny Huang 	if ((start + count) < 0 || (start + count) > 64)
806f347c284SJohnny Huang 		return OTP_USAGE;
807f347c284SJohnny Huang 
808f347c284SJohnny Huang 	otp_strap_status(otpstrap);
809f347c284SJohnny Huang 
810f347c284SJohnny Huang 	if (info_cb.version == OTP_A0) {
811f347c284SJohnny Huang 		remains = 7;
812f347c284SJohnny Huang 		printf("BIT(hex)  Value  Option           Status\n");
813f347c284SJohnny Huang 	} else {
814f347c284SJohnny Huang 		remains = 6;
815f347c284SJohnny Huang 		printf("BIT(hex)  Value  Option         Reg_Protect Status\n");
816f347c284SJohnny Huang 	}
817f347c284SJohnny Huang 	printf("______________________________________________________________________________\n");
818f347c284SJohnny Huang 
819f347c284SJohnny Huang 	for (i = start; i < start + count; i++) {
820f347c284SJohnny Huang 		printf("0x%-8X", i);
821f347c284SJohnny Huang 		printf("%-7d", otpstrap[i].value);
822f347c284SJohnny Huang 		for (j = 0; j < remains; j++)
823f347c284SJohnny Huang 			printf("%d ", otpstrap[i].option_array[j]);
824f347c284SJohnny Huang 		printf("   ");
825f347c284SJohnny Huang 		if (info_cb.version != OTP_A0)
826f347c284SJohnny Huang 			printf("%d           ", otpstrap[i].reg_protected);
827f347c284SJohnny Huang 		if (otpstrap[i].protected == 1) {
828f347c284SJohnny Huang 			printf("protected and not writable");
829f347c284SJohnny Huang 		} else {
830f347c284SJohnny Huang 			printf("not protected ");
831f347c284SJohnny Huang 			if (otpstrap[i].remain_times == 0)
832f347c284SJohnny Huang 				printf("and no remaining times to write.");
833f347c284SJohnny Huang 			else
834f347c284SJohnny Huang 				printf("and still can write %d times", otpstrap[i].remain_times);
835f347c284SJohnny Huang 		}
836f347c284SJohnny Huang 		printf("\n");
837f347c284SJohnny Huang 	}
838f347c284SJohnny Huang 
839f347c284SJohnny Huang 	return OTP_SUCCESS;
840f347c284SJohnny Huang }
841f347c284SJohnny Huang 
842794e27ecSJohnny Huang static void otp_print_revid(u32 *rid)
843794e27ecSJohnny Huang {
844794e27ecSJohnny Huang 	int bit_offset;
845794e27ecSJohnny Huang 	int i, j;
846794e27ecSJohnny Huang 
847794e27ecSJohnny Huang 	printf("     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f\n");
848794e27ecSJohnny Huang 	printf("___________________________________________________\n");
849794e27ecSJohnny Huang 	for (i = 0; i < 64; i++) {
850794e27ecSJohnny Huang 		if (i < 32) {
851794e27ecSJohnny Huang 			j = 0;
852794e27ecSJohnny Huang 			bit_offset = i;
853794e27ecSJohnny Huang 		} else {
854794e27ecSJohnny Huang 			j = 1;
855794e27ecSJohnny Huang 			bit_offset = i - 32;
856794e27ecSJohnny Huang 		}
857794e27ecSJohnny Huang 		if (i % 16 == 0)
858794e27ecSJohnny Huang 			printf("%2x | ", i);
859794e27ecSJohnny Huang 		printf("%d  ", (rid[j] >> bit_offset) & 0x1);
860794e27ecSJohnny Huang 		if ((i + 1) % 16 == 0)
861794e27ecSJohnny Huang 			printf("\n");
862794e27ecSJohnny Huang 	}
863794e27ecSJohnny Huang }
864794e27ecSJohnny Huang 
865696656c6SJohnny Huang static int otp_print_conf_image(struct otp_image_layout *image_layout)
86669d5fd8fSJohnny Huang {
86779e42a59SJoel Stanley 	const struct otpconf_info *conf_info = info_cb.conf_info;
868a219f6deSJohnny Huang 	u32 *OTPCFG = (u32 *)image_layout->conf;
869a219f6deSJohnny Huang 	u32 *OTPCFG_IGNORE = (u32 *)image_layout->conf_ignore;
870a219f6deSJohnny Huang 	u32 mask;
871a219f6deSJohnny Huang 	u32 dw_offset;
872a219f6deSJohnny Huang 	u32 bit_offset;
873a219f6deSJohnny Huang 	u32 otp_value;
874a219f6deSJohnny Huang 	u32 otp_ignore;
875b458cd62SJohnny Huang 	int fail = 0;
8767adec5f6SJohnny Huang 	int mask_err;
877794e27ecSJohnny Huang 	int rid_num = 0;
87873f11549SJohnny Huang 	char valid_bit[20];
879794e27ecSJohnny Huang 	int fz;
88066f2f8e5SJohnny Huang 	int i;
88173f11549SJohnny Huang 	int j;
88266f2f8e5SJohnny Huang 
883737ed20bSJohnny Huang 	printf("DW    BIT        Value       Description\n");
88466f2f8e5SJohnny Huang 	printf("__________________________________________________________________________\n");
8853cb28812SJohnny Huang 	for (i = 0; i < info_cb.conf_info_len; i++) {
8867adec5f6SJohnny Huang 		mask_err = 0;
8873cb28812SJohnny Huang 		dw_offset = conf_info[i].dw_offset;
8883cb28812SJohnny Huang 		bit_offset = conf_info[i].bit_offset;
8893cb28812SJohnny Huang 		mask = BIT(conf_info[i].length) - 1;
890b458cd62SJohnny Huang 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
891696656c6SJohnny Huang 		otp_ignore = (OTPCFG_IGNORE[dw_offset] >> bit_offset) & mask;
892b458cd62SJohnny Huang 
8937adec5f6SJohnny Huang 		if (conf_info[i].value == OTP_REG_VALID_BIT) {
8947adec5f6SJohnny Huang 			if (((otp_value + otp_ignore) & mask) != mask) {
895b458cd62SJohnny Huang 				fail = 1;
8967adec5f6SJohnny Huang 				mask_err = 1;
8977adec5f6SJohnny Huang 			}
8987adec5f6SJohnny Huang 		} else {
8997adec5f6SJohnny Huang 			if (otp_ignore == mask) {
9007adec5f6SJohnny Huang 				continue;
9017adec5f6SJohnny Huang 			} else if (otp_ignore != 0) {
9027adec5f6SJohnny Huang 				fail = 1;
9037adec5f6SJohnny Huang 				mask_err = 1;
9047adec5f6SJohnny Huang 			}
9057adec5f6SJohnny Huang 		}
906b458cd62SJohnny Huang 
907a219f6deSJohnny Huang 		if (otp_value != conf_info[i].value &&
9083cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_RESERVED &&
9093cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALUE &&
9103cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALID_BIT)
911b458cd62SJohnny Huang 			continue;
912b458cd62SJohnny Huang 		printf("0x%-4X", dw_offset);
913b458cd62SJohnny Huang 
9143cb28812SJohnny Huang 		if (conf_info[i].length == 1) {
9153cb28812SJohnny Huang 			printf("0x%-9X", conf_info[i].bit_offset);
91666f2f8e5SJohnny Huang 		} else {
917b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
9183cb28812SJohnny Huang 			       conf_info[i].bit_offset + conf_info[i].length - 1,
9193cb28812SJohnny Huang 			       conf_info[i].bit_offset);
92066f2f8e5SJohnny Huang 		}
921b458cd62SJohnny Huang 		printf("0x%-10x", otp_value);
922b458cd62SJohnny Huang 
9237adec5f6SJohnny Huang 		if (mask_err) {
9247adec5f6SJohnny Huang 			printf("Ignore, mask error\n");
925a219f6deSJohnny Huang 			continue;
926a219f6deSJohnny Huang 		}
9273cb28812SJohnny Huang 		if (conf_info[i].value == OTP_REG_RESERVED) {
928b458cd62SJohnny Huang 			printf("Reserved\n");
9293cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALUE) {
9303cb28812SJohnny Huang 			printf(conf_info[i].information, otp_value);
931b458cd62SJohnny Huang 			printf("\n");
9323cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALID_BIT) {
933b458cd62SJohnny Huang 			if (otp_value != 0) {
93473f11549SJohnny Huang 				for (j = 0; j < 7; j++) {
935a219f6deSJohnny Huang 					if (otp_value == (1 << j))
93673f11549SJohnny Huang 						valid_bit[j * 2] = '1';
937a219f6deSJohnny Huang 					else
93873f11549SJohnny Huang 						valid_bit[j * 2] = '0';
93973f11549SJohnny Huang 					valid_bit[j * 2 + 1] = ' ';
94073f11549SJohnny Huang 				}
94173f11549SJohnny Huang 				valid_bit[15] = 0;
94273f11549SJohnny Huang 			} else {
94373f11549SJohnny Huang 				strcpy(valid_bit, "0 0 0 0 0 0 0 0\0");
944b458cd62SJohnny Huang 			}
9453cb28812SJohnny Huang 			printf(conf_info[i].information, valid_bit);
946b458cd62SJohnny Huang 			printf("\n");
947b458cd62SJohnny Huang 		} else {
9483cb28812SJohnny Huang 			printf("%s\n", conf_info[i].information);
949b458cd62SJohnny Huang 		}
950b458cd62SJohnny Huang 	}
951b458cd62SJohnny Huang 
952794e27ecSJohnny Huang 	if (OTPCFG[0xa] != 0 || OTPCFG[0xb] != 0) {
953794e27ecSJohnny Huang 		if (OTPCFG_IGNORE[0xa] != 0 && OTPCFG_IGNORE[0xb] != 0) {
954794e27ecSJohnny Huang 			printf("OTP revision ID is invalid.\n");
955794e27ecSJohnny Huang 			fail = 1;
956794e27ecSJohnny Huang 		} else {
957794e27ecSJohnny Huang 			fz = 0;
958794e27ecSJohnny Huang 			for (i = 0; i < 64; i++) {
959794e27ecSJohnny Huang 				if (get_dw_bit(&OTPCFG[0xa], i) == 0) {
960794e27ecSJohnny Huang 					if (!fz)
961794e27ecSJohnny Huang 						fz = 1;
962794e27ecSJohnny Huang 				} else {
963794e27ecSJohnny Huang 					rid_num++;
964794e27ecSJohnny Huang 					if (fz) {
965794e27ecSJohnny Huang 						printf("OTP revision ID is invalid.\n");
966794e27ecSJohnny Huang 						fail = 1;
967794e27ecSJohnny Huang 						break;
968794e27ecSJohnny Huang 					}
969794e27ecSJohnny Huang 				}
970794e27ecSJohnny Huang 			}
971794e27ecSJohnny Huang 		}
972794e27ecSJohnny Huang 		if (fail)
973794e27ecSJohnny Huang 			printf("OTP revision ID\n");
974794e27ecSJohnny Huang 		else
975794e27ecSJohnny Huang 			printf("OTP revision ID: 0x%x\n", rid_num);
976794e27ecSJohnny Huang 		otp_print_revid(&OTPCFG[0xa]);
977794e27ecSJohnny Huang 	}
978794e27ecSJohnny Huang 
979b458cd62SJohnny Huang 	if (fail)
980b458cd62SJohnny Huang 		return OTP_FAILURE;
981b458cd62SJohnny Huang 
98266f2f8e5SJohnny Huang 	return OTP_SUCCESS;
98366f2f8e5SJohnny Huang }
98466f2f8e5SJohnny Huang 
9852d4b0742SJohnny Huang static int otp_print_conf_info(int input_offset)
98666f2f8e5SJohnny Huang {
98779e42a59SJoel Stanley 	const struct otpconf_info *conf_info = info_cb.conf_info;
988a219f6deSJohnny Huang 	u32 OTPCFG[16];
989a219f6deSJohnny Huang 	u32 mask;
990a219f6deSJohnny Huang 	u32 dw_offset;
991a219f6deSJohnny Huang 	u32 bit_offset;
992a219f6deSJohnny Huang 	u32 otp_value;
99373f11549SJohnny Huang 	char valid_bit[20];
99466f2f8e5SJohnny Huang 	int i;
99573f11549SJohnny Huang 	int j;
99666f2f8e5SJohnny Huang 
997dacbba92SJohnny Huang 	otp_soak(0);
998bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++)
999f347c284SJohnny Huang 		otp_read_conf(i, &OTPCFG[i]);
100066f2f8e5SJohnny Huang 
1001b458cd62SJohnny Huang 	printf("DW    BIT        Value       Description\n");
1002b458cd62SJohnny Huang 	printf("__________________________________________________________________________\n");
10033cb28812SJohnny Huang 	for (i = 0; i < info_cb.conf_info_len; i++) {
10043cb28812SJohnny Huang 		if (input_offset != -1 && input_offset != conf_info[i].dw_offset)
10052d4b0742SJohnny Huang 			continue;
10063cb28812SJohnny Huang 		dw_offset = conf_info[i].dw_offset;
10073cb28812SJohnny Huang 		bit_offset = conf_info[i].bit_offset;
10083cb28812SJohnny Huang 		mask = BIT(conf_info[i].length) - 1;
1009b458cd62SJohnny Huang 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
1010b458cd62SJohnny Huang 
1011a219f6deSJohnny Huang 		if (otp_value != conf_info[i].value &&
10123cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_RESERVED &&
10133cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALUE &&
10143cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALID_BIT)
1015b458cd62SJohnny Huang 			continue;
1016b458cd62SJohnny Huang 		printf("0x%-4X", dw_offset);
1017b458cd62SJohnny Huang 
10183cb28812SJohnny Huang 		if (conf_info[i].length == 1) {
10193cb28812SJohnny Huang 			printf("0x%-9X", conf_info[i].bit_offset);
1020b458cd62SJohnny Huang 		} else {
1021b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
10223cb28812SJohnny Huang 			       conf_info[i].bit_offset + conf_info[i].length - 1,
10233cb28812SJohnny Huang 			       conf_info[i].bit_offset);
1024b458cd62SJohnny Huang 		}
1025b458cd62SJohnny Huang 		printf("0x%-10x", otp_value);
1026b458cd62SJohnny Huang 
10273cb28812SJohnny Huang 		if (conf_info[i].value == OTP_REG_RESERVED) {
1028b458cd62SJohnny Huang 			printf("Reserved\n");
10293cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALUE) {
10303cb28812SJohnny Huang 			printf(conf_info[i].information, otp_value);
1031b458cd62SJohnny Huang 			printf("\n");
10323cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALID_BIT) {
1033b458cd62SJohnny Huang 			if (otp_value != 0) {
103473f11549SJohnny Huang 				for (j = 0; j < 7; j++) {
1035a219f6deSJohnny Huang 					if (otp_value == (1 << j))
103673f11549SJohnny Huang 						valid_bit[j * 2] = '1';
1037a219f6deSJohnny Huang 					else
103873f11549SJohnny Huang 						valid_bit[j * 2] = '0';
103973f11549SJohnny Huang 					valid_bit[j * 2 + 1] = ' ';
104073f11549SJohnny Huang 				}
104173f11549SJohnny Huang 				valid_bit[15] = 0;
104273f11549SJohnny Huang 			} else {
104373f11549SJohnny Huang 				strcpy(valid_bit, "0 0 0 0 0 0 0 0\0");
1044b458cd62SJohnny Huang 			}
10453cb28812SJohnny Huang 			printf(conf_info[i].information, valid_bit);
1046b458cd62SJohnny Huang 			printf("\n");
1047b458cd62SJohnny Huang 		} else {
10483cb28812SJohnny Huang 			printf("%s\n", conf_info[i].information);
1049b458cd62SJohnny Huang 		}
1050b458cd62SJohnny Huang 	}
1051b458cd62SJohnny Huang 	return OTP_SUCCESS;
105266f2f8e5SJohnny Huang }
105366f2f8e5SJohnny Huang 
10545010032bSJohnny Huang static int otp_print_strap_image(struct otp_image_layout *image_layout)
105576d13988SJohnny Huang {
105679e42a59SJoel Stanley 	const struct otpstrap_info *strap_info = info_cb.strap_info;
1057a219f6deSJohnny Huang 	u32 *OTPSTRAP;
1058a219f6deSJohnny Huang 	u32 *OTPSTRAP_REG_PRO;
1059a219f6deSJohnny Huang 	u32 *OTPSTRAP_PRO;
1060a219f6deSJohnny Huang 	u32 *OTPSTRAP_IGNORE;
106176d13988SJohnny Huang 	int i;
1062a8bd6d8cSJohnny Huang 	int fail = 0;
1063a219f6deSJohnny Huang 	u32 bit_offset;
1064a219f6deSJohnny Huang 	u32 dw_offset;
1065a219f6deSJohnny Huang 	u32 mask;
1066a219f6deSJohnny Huang 	u32 otp_value;
1067a219f6deSJohnny Huang 	u32 otp_reg_protect;
1068a219f6deSJohnny Huang 	u32 otp_protect;
1069a219f6deSJohnny Huang 	u32 otp_ignore;
107076d13988SJohnny Huang 
1071a219f6deSJohnny Huang 	OTPSTRAP = (u32 *)image_layout->strap;
1072a219f6deSJohnny Huang 	OTPSTRAP_PRO = (u32 *)image_layout->strap_pro;
1073a219f6deSJohnny Huang 	OTPSTRAP_IGNORE = (u32 *)image_layout->strap_ignore;
1074e417205bSJohnny Huang 	if (info_cb.version == OTP_A0) {
1075696656c6SJohnny Huang 		OTPSTRAP_REG_PRO = NULL;
1076a8bd6d8cSJohnny Huang 		printf("BIT(hex)   Value       Protect     Description\n");
1077696656c6SJohnny Huang 	} else {
1078a219f6deSJohnny Huang 		OTPSTRAP_REG_PRO = (u32 *)image_layout->strap_reg_pro;
1079de6b0cc4SJohnny Huang 		printf("BIT(hex)   Value       Reg_Protect Protect     Description\n");
1080696656c6SJohnny Huang 	}
1081de6b0cc4SJohnny Huang 	printf("__________________________________________________________________________________________\n");
1082b458cd62SJohnny Huang 
10833cb28812SJohnny Huang 	for (i = 0; i < info_cb.strap_info_len; i++) {
10847adec5f6SJohnny Huang 		fail = 0;
1085696656c6SJohnny Huang 		if (strap_info[i].bit_offset > 31) {
1086a8bd6d8cSJohnny Huang 			dw_offset = 1;
10873cb28812SJohnny Huang 			bit_offset = strap_info[i].bit_offset - 32;
1088a8bd6d8cSJohnny Huang 		} else {
1089a8bd6d8cSJohnny Huang 			dw_offset = 0;
10903cb28812SJohnny Huang 			bit_offset = strap_info[i].bit_offset;
1091a8bd6d8cSJohnny Huang 		}
109276d13988SJohnny Huang 
10933cb28812SJohnny Huang 		mask = BIT(strap_info[i].length) - 1;
1094a8bd6d8cSJohnny Huang 		otp_value = (OTPSTRAP[dw_offset] >> bit_offset) & mask;
1095a8bd6d8cSJohnny Huang 		otp_protect = (OTPSTRAP_PRO[dw_offset] >> bit_offset) & mask;
1096696656c6SJohnny Huang 		otp_ignore = (OTPSTRAP_IGNORE[dw_offset] >> bit_offset) & mask;
1097a8bd6d8cSJohnny Huang 
1098e417205bSJohnny Huang 		if (info_cb.version != OTP_A0)
1099696656c6SJohnny Huang 			otp_reg_protect = (OTPSTRAP_REG_PRO[dw_offset] >> bit_offset) & mask;
11005010032bSJohnny Huang 		else
11015010032bSJohnny Huang 			otp_reg_protect = 0;
1102696656c6SJohnny Huang 
1103a219f6deSJohnny Huang 		if (otp_ignore == mask)
1104a8bd6d8cSJohnny Huang 			continue;
1105a219f6deSJohnny Huang 		else if (otp_ignore != 0)
1106a8bd6d8cSJohnny Huang 			fail = 1;
1107a8bd6d8cSJohnny Huang 
1108a219f6deSJohnny Huang 		if (otp_value != strap_info[i].value &&
11093cb28812SJohnny Huang 		    strap_info[i].value != OTP_REG_RESERVED)
1110a8bd6d8cSJohnny Huang 			continue;
1111a8bd6d8cSJohnny Huang 
11123cb28812SJohnny Huang 		if (strap_info[i].length == 1) {
11133cb28812SJohnny Huang 			printf("0x%-9X", strap_info[i].bit_offset);
1114a8bd6d8cSJohnny Huang 		} else {
1115b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
11163cb28812SJohnny Huang 			       strap_info[i].bit_offset + strap_info[i].length - 1,
11173cb28812SJohnny Huang 			       strap_info[i].bit_offset);
1118a8bd6d8cSJohnny Huang 		}
1119a8bd6d8cSJohnny Huang 		printf("0x%-10x", otp_value);
1120e417205bSJohnny Huang 		if (info_cb.version != OTP_A0)
1121696656c6SJohnny Huang 			printf("0x%-10x", otp_reg_protect);
1122a8bd6d8cSJohnny Huang 		printf("0x%-10x", otp_protect);
1123a8bd6d8cSJohnny Huang 
1124a8bd6d8cSJohnny Huang 		if (fail) {
1125696656c6SJohnny Huang 			printf("Ignore mask error\n");
1126a8bd6d8cSJohnny Huang 		} else {
11273cb28812SJohnny Huang 			if (strap_info[i].value != OTP_REG_RESERVED)
11283cb28812SJohnny Huang 				printf("%s\n", strap_info[i].information);
1129a8bd6d8cSJohnny Huang 			else
1130a8bd6d8cSJohnny Huang 				printf("Reserved\n");
1131a8bd6d8cSJohnny Huang 		}
1132a8bd6d8cSJohnny Huang 	}
1133a8bd6d8cSJohnny Huang 
1134a8bd6d8cSJohnny Huang 	if (fail)
113576d13988SJohnny Huang 		return OTP_FAILURE;
113676d13988SJohnny Huang 
113776d13988SJohnny Huang 	return OTP_SUCCESS;
113876d13988SJohnny Huang }
113976d13988SJohnny Huang 
1140b458cd62SJohnny Huang static int otp_print_strap_info(int view)
114176d13988SJohnny Huang {
114279e42a59SJoel Stanley 	const struct otpstrap_info *strap_info = info_cb.strap_info;
114376d13988SJohnny Huang 	struct otpstrap_status strap_status[64];
114407baa4e8SJohnny Huang 	int i, j;
1145b458cd62SJohnny Huang 	int fail = 0;
1146a219f6deSJohnny Huang 	u32 bit_offset;
1147a219f6deSJohnny Huang 	u32 length;
1148a219f6deSJohnny Huang 	u32 otp_value;
1149a219f6deSJohnny Huang 	u32 otp_protect;
115076d13988SJohnny Huang 
1151541eb887SJohnny Huang 	otp_strap_status(strap_status);
115276d13988SJohnny Huang 
1153b458cd62SJohnny Huang 	if (view) {
1154e417205bSJohnny Huang 		if (info_cb.version == OTP_A0)
115507baa4e8SJohnny Huang 			printf("BIT(hex) Value  Remains  Protect   Description\n");
115683655e91SJohnny Huang 		else
115783655e91SJohnny Huang 			printf("BIT(hex) Value  Remains  Reg_Protect Protect   Description\n");
115807baa4e8SJohnny Huang 		printf("___________________________________________________________________________________________________\n");
1159b458cd62SJohnny Huang 	} else {
1160b458cd62SJohnny Huang 		printf("BIT(hex)   Value       Description\n");
1161b458cd62SJohnny Huang 		printf("________________________________________________________________________________\n");
116276d13988SJohnny Huang 	}
11633cb28812SJohnny Huang 	for (i = 0; i < info_cb.strap_info_len; i++) {
1164b458cd62SJohnny Huang 		otp_value = 0;
11653cb28812SJohnny Huang 		bit_offset = strap_info[i].bit_offset;
11663cb28812SJohnny Huang 		length = strap_info[i].length;
1167b458cd62SJohnny Huang 		for (j = 0; j < length; j++) {
1168c947ef08SJohnny Huang 			otp_value |= strap_status[bit_offset + j].value << j;
1169c947ef08SJohnny Huang 			otp_protect |= strap_status[bit_offset + j].protected << j;
1170b458cd62SJohnny Huang 		}
1171a219f6deSJohnny Huang 		if (otp_value != strap_info[i].value &&
11723cb28812SJohnny Huang 		    strap_info[i].value != OTP_REG_RESERVED)
1173b458cd62SJohnny Huang 			continue;
1174b458cd62SJohnny Huang 		if (view) {
1175b458cd62SJohnny Huang 			for (j = 0; j < length; j++) {
11763cb28812SJohnny Huang 				printf("0x%-7X", strap_info[i].bit_offset + j);
1177b458cd62SJohnny Huang 				printf("0x%-5X", strap_status[bit_offset + j].value);
117807baa4e8SJohnny Huang 				printf("%-9d", strap_status[bit_offset + j].remain_times);
1179e417205bSJohnny Huang 				if (info_cb.version != OTP_A0)
1180e1a7245eSJohnny Huang 					printf("0x%-10X", strap_status[bit_offset + j].reg_protected);
1181e1a7245eSJohnny Huang 				printf("0x%-7X", strap_status[bit_offset + j].protected);
11823cb28812SJohnny Huang 				if (strap_info[i].value == OTP_REG_RESERVED) {
1183b458cd62SJohnny Huang 					printf(" Reserved\n");
1184b458cd62SJohnny Huang 					continue;
1185b458cd62SJohnny Huang 				}
1186b458cd62SJohnny Huang 				if (length == 1) {
11873cb28812SJohnny Huang 					printf(" %s\n", strap_info[i].information);
1188b458cd62SJohnny Huang 					continue;
118976d13988SJohnny Huang 				}
119076d13988SJohnny Huang 
1191b458cd62SJohnny Huang 				if (j == 0)
11923cb28812SJohnny Huang 					printf("/%s\n", strap_info[i].information);
1193b458cd62SJohnny Huang 				else if (j == length - 1)
1194b458cd62SJohnny Huang 					printf("\\ \"\n");
1195b458cd62SJohnny Huang 				else
1196b458cd62SJohnny Huang 					printf("| \"\n");
119776d13988SJohnny Huang 			}
1198b458cd62SJohnny Huang 		} else {
1199c947ef08SJohnny Huang 			if (length == 1) {
12003cb28812SJohnny Huang 				printf("0x%-9X", strap_info[i].bit_offset);
1201b458cd62SJohnny Huang 			} else {
1202b458cd62SJohnny Huang 				printf("0x%-2X:0x%-4X",
1203b458cd62SJohnny Huang 				       bit_offset + length - 1, bit_offset);
1204b458cd62SJohnny Huang 			}
1205b458cd62SJohnny Huang 
1206b458cd62SJohnny Huang 			printf("0x%-10X", otp_value);
1207b458cd62SJohnny Huang 
12083cb28812SJohnny Huang 			if (strap_info[i].value != OTP_REG_RESERVED)
12093cb28812SJohnny Huang 				printf("%s\n", strap_info[i].information);
1210b458cd62SJohnny Huang 			else
1211b458cd62SJohnny Huang 				printf("Reserved\n");
1212b458cd62SJohnny Huang 		}
1213b458cd62SJohnny Huang 	}
1214b458cd62SJohnny Huang 
1215b458cd62SJohnny Huang 	if (fail)
1216b458cd62SJohnny Huang 		return OTP_FAILURE;
1217b458cd62SJohnny Huang 
1218b458cd62SJohnny Huang 	return OTP_SUCCESS;
1219b458cd62SJohnny Huang }
1220b458cd62SJohnny Huang 
1221f347c284SJohnny Huang static int otp_print_data_image(struct otp_image_layout *image_layout)
122269d5fd8fSJohnny Huang {
122369d5fd8fSJohnny Huang 	int key_id, key_offset, last, key_type, key_length, exp_length;
122479e42a59SJoel Stanley 	const struct otpkey_type *key_info_array = info_cb.key_info;
12259a4fe690SJohnny Huang 	struct otpkey_type key_info;
1226a219f6deSJohnny Huang 	u32 *buf;
1227a219f6deSJohnny Huang 	u8 *byte_buf;
12289d998018SJohnny Huang 	char empty = 1;
122969d5fd8fSJohnny Huang 	int i = 0, len = 0;
12309a4fe690SJohnny Huang 	int j;
123154552c69SJohnny Huang 
1232696656c6SJohnny Huang 	byte_buf = image_layout->data;
1233a219f6deSJohnny Huang 	buf = (u32 *)byte_buf;
12349d998018SJohnny Huang 
12359d998018SJohnny Huang 	for (i = 0; i < 16; i++) {
1236a219f6deSJohnny Huang 		if (buf[i] != 0)
12379d998018SJohnny Huang 			empty = 0;
12389d998018SJohnny Huang 	}
12399d998018SJohnny Huang 	if (empty)
1240f347c284SJohnny Huang 		return OTP_SUCCESS;
12419d998018SJohnny Huang 
12429d998018SJohnny Huang 	i = 0;
124369d5fd8fSJohnny Huang 	while (1) {
124469d5fd8fSJohnny Huang 		key_id = buf[i] & 0x7;
124569d5fd8fSJohnny Huang 		key_offset = buf[i] & 0x1ff8;
124669d5fd8fSJohnny Huang 		last = (buf[i] >> 13) & 1;
124769d5fd8fSJohnny Huang 		key_type = (buf[i] >> 14) & 0xf;
124869d5fd8fSJohnny Huang 		key_length = (buf[i] >> 18) & 0x3;
124969d5fd8fSJohnny Huang 		exp_length = (buf[i] >> 20) & 0xfff;
12509a4fe690SJohnny Huang 
12519a4fe690SJohnny Huang 		for (j = 0; j < info_cb.key_info_len; j++) {
12529a4fe690SJohnny Huang 			if (key_type == key_info_array[j].value) {
12539a4fe690SJohnny Huang 				key_info = key_info_array[j];
12549a4fe690SJohnny Huang 				break;
12559a4fe690SJohnny Huang 			}
12569a4fe690SJohnny Huang 		}
12579a4fe690SJohnny Huang 
12587f795e57SJohnny Huang 		printf("\nKey[%d]:\n", i);
125969d5fd8fSJohnny Huang 		printf("Key Type: ");
12609a4fe690SJohnny Huang 		printf("%s\n", key_info.information);
12619a4fe690SJohnny Huang 
12629a4fe690SJohnny Huang 		if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
126369d5fd8fSJohnny Huang 			printf("HMAC SHA Type: ");
126469d5fd8fSJohnny Huang 			switch (key_length) {
126569d5fd8fSJohnny Huang 			case 0:
126669d5fd8fSJohnny Huang 				printf("HMAC(SHA224)\n");
126769d5fd8fSJohnny Huang 				break;
126869d5fd8fSJohnny Huang 			case 1:
126969d5fd8fSJohnny Huang 				printf("HMAC(SHA256)\n");
127069d5fd8fSJohnny Huang 				break;
127169d5fd8fSJohnny Huang 			case 2:
127269d5fd8fSJohnny Huang 				printf("HMAC(SHA384)\n");
127369d5fd8fSJohnny Huang 				break;
127469d5fd8fSJohnny Huang 			case 3:
127569d5fd8fSJohnny Huang 				printf("HMAC(SHA512)\n");
127669d5fd8fSJohnny Huang 				break;
127769d5fd8fSJohnny Huang 			}
1278181f72d8SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA_PRIV ||
1279181f72d8SJohnny Huang 			   key_info.key_type == OTP_KEY_TYPE_RSA_PUB) {
128069d5fd8fSJohnny Huang 			printf("RSA SHA Type: ");
128169d5fd8fSJohnny Huang 			switch (key_length) {
128269d5fd8fSJohnny Huang 			case 0:
128369d5fd8fSJohnny Huang 				printf("RSA1024\n");
128469d5fd8fSJohnny Huang 				len = 0x100;
128569d5fd8fSJohnny Huang 				break;
128669d5fd8fSJohnny Huang 			case 1:
128769d5fd8fSJohnny Huang 				printf("RSA2048\n");
128869d5fd8fSJohnny Huang 				len = 0x200;
128969d5fd8fSJohnny Huang 				break;
129069d5fd8fSJohnny Huang 			case 2:
129169d5fd8fSJohnny Huang 				printf("RSA3072\n");
129269d5fd8fSJohnny Huang 				len = 0x300;
129369d5fd8fSJohnny Huang 				break;
129469d5fd8fSJohnny Huang 			case 3:
129569d5fd8fSJohnny Huang 				printf("RSA4096\n");
129669d5fd8fSJohnny Huang 				len = 0x400;
129769d5fd8fSJohnny Huang 				break;
129869d5fd8fSJohnny Huang 			}
129969d5fd8fSJohnny Huang 			printf("RSA exponent bit length: %d\n", exp_length);
130069d5fd8fSJohnny Huang 		}
13019a4fe690SJohnny Huang 		if (key_info.need_id)
130269d5fd8fSJohnny Huang 			printf("Key Number ID: %d\n", key_id);
130369d5fd8fSJohnny Huang 		printf("Key Value:\n");
13049a4fe690SJohnny Huang 		if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
130569d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], 0x40);
13069a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_AES) {
13079a4fe690SJohnny Huang 			printf("AES Key:\n");
13089a4fe690SJohnny Huang 			buf_print(&byte_buf[key_offset], 0x20);
1309e417205bSJohnny Huang 			if (info_cb.version == OTP_A0) {
13109a4fe690SJohnny Huang 				printf("AES IV:\n");
13119a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x10);
13129a4fe690SJohnny Huang 			}
13139a4fe690SJohnny Huang 
13149a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_VAULT) {
1315e417205bSJohnny Huang 			if (info_cb.version == OTP_A0) {
131669d5fd8fSJohnny Huang 				printf("AES Key:\n");
131769d5fd8fSJohnny Huang 				buf_print(&byte_buf[key_offset], 0x20);
131869d5fd8fSJohnny Huang 				printf("AES IV:\n");
131969d5fd8fSJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x10);
13205fdde29fSJohnny Huang 			} else {
13219a4fe690SJohnny Huang 				printf("AES Key 1:\n");
13229a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset], 0x20);
13239a4fe690SJohnny Huang 				printf("AES Key 2:\n");
13249a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x20);
13259a4fe690SJohnny Huang 			}
1326181f72d8SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA_PRIV) {
132769d5fd8fSJohnny Huang 			printf("RSA mod:\n");
132869d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], len / 2);
132969d5fd8fSJohnny Huang 			printf("RSA exp:\n");
133069d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset + (len / 2)], len / 2);
1331181f72d8SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA_PUB) {
1332181f72d8SJohnny Huang 			printf("RSA mod:\n");
1333181f72d8SJohnny Huang 			buf_print(&byte_buf[key_offset], len / 2);
1334181f72d8SJohnny Huang 			printf("RSA exp:\n");
1335a219f6deSJohnny Huang 			buf_print((u8 *)"\x01\x00\x01", 3);
133669d5fd8fSJohnny Huang 		}
133769d5fd8fSJohnny Huang 		if (last)
133869d5fd8fSJohnny Huang 			break;
133969d5fd8fSJohnny Huang 		i++;
134069d5fd8fSJohnny Huang 	}
1341f347c284SJohnny Huang 	return OTP_SUCCESS;
1342f347c284SJohnny Huang }
1343f347c284SJohnny Huang 
1344f347c284SJohnny Huang static int otp_strap_image_confirm(struct otp_image_layout *image_layout)
1345f347c284SJohnny Huang {
1346f347c284SJohnny Huang 	int i;
1347f347c284SJohnny Huang 	u32 *strap;
1348f347c284SJohnny Huang 	u32 *strap_ignore;
1349f347c284SJohnny Huang 	u32 *strap_reg_protect;
1350f347c284SJohnny Huang 	u32 *strap_pro;
1351f347c284SJohnny Huang 	int bit, pbit, ibit, rpbit;
1352f347c284SJohnny Huang 	int fail = 0;
1353f347c284SJohnny Huang 	int ret;
1354f347c284SJohnny Huang 	struct otpstrap_status otpstrap[64];
1355f347c284SJohnny Huang 
1356f347c284SJohnny Huang 	strap = (u32 *)image_layout->strap;
1357f347c284SJohnny Huang 	strap_pro = (u32 *)image_layout->strap_pro;
1358f347c284SJohnny Huang 	strap_ignore = (u32 *)image_layout->strap_ignore;
1359f347c284SJohnny Huang 	strap_reg_protect = (u32 *)image_layout->strap_reg_pro;
1360f347c284SJohnny Huang 
1361f347c284SJohnny Huang 	otp_strap_status(otpstrap);
1362f347c284SJohnny Huang 	for (i = 0; i < 64; i++) {
1363f347c284SJohnny Huang 		if (i < 32) {
1364f347c284SJohnny Huang 			bit = (strap[0] >> i) & 0x1;
1365f347c284SJohnny Huang 			ibit = (strap_ignore[0] >> i) & 0x1;
1366f347c284SJohnny Huang 			pbit = (strap_pro[0] >> i) & 0x1;
1367f347c284SJohnny Huang 		} else {
1368f347c284SJohnny Huang 			bit = (strap[1] >> (i - 32)) & 0x1;
1369f347c284SJohnny Huang 			ibit = (strap_ignore[1] >> (i - 32)) & 0x1;
1370f347c284SJohnny Huang 			pbit = (strap_pro[1] >> (i - 32)) & 0x1;
1371f347c284SJohnny Huang 		}
1372f347c284SJohnny Huang 
1373f347c284SJohnny Huang 		if (info_cb.version != OTP_A0) {
1374f347c284SJohnny Huang 			if (i < 32)
1375f347c284SJohnny Huang 				rpbit = (strap_reg_protect[0] >> i) & 0x1;
1376f347c284SJohnny Huang 			else
1377f347c284SJohnny Huang 				rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1;
1378f347c284SJohnny Huang 		} else {
1379f347c284SJohnny Huang 			rpbit = 0;
1380f347c284SJohnny Huang 		}
1381f347c284SJohnny Huang 		ret = otp_strap_bit_confirm(&otpstrap[i], i, ibit, bit, pbit, rpbit);
1382f347c284SJohnny Huang 
1383f347c284SJohnny Huang 		if (ret == OTP_FAILURE)
1384f347c284SJohnny Huang 			fail = 1;
1385f347c284SJohnny Huang 	}
1386f347c284SJohnny Huang 	if (fail == 1)
1387f347c284SJohnny Huang 		return OTP_FAILURE;
1388f347c284SJohnny Huang 	else
1389f347c284SJohnny Huang 		return OTP_SUCCESS;
1390f347c284SJohnny Huang }
1391f347c284SJohnny Huang 
1392f347c284SJohnny Huang static int otp_prog_data(struct otp_image_layout *image_layout)
1393f347c284SJohnny Huang {
1394f347c284SJohnny Huang 	int i;
1395f347c284SJohnny Huang 	int ret;
1396f347c284SJohnny Huang 	int data_dw;
1397f347c284SJohnny Huang 	u32 data[2048];
1398f347c284SJohnny Huang 	u32 *buf;
1399f347c284SJohnny Huang 	u32 *buf_ignore;
1400f347c284SJohnny Huang 	u32 data_masked;
1401f347c284SJohnny Huang 	u32 buf_masked;
1402f347c284SJohnny Huang 
1403f347c284SJohnny Huang 	buf = (u32 *)image_layout->data;
1404f347c284SJohnny Huang 	buf_ignore = (u32 *)image_layout->data_ignore;
1405f347c284SJohnny Huang 
1406f347c284SJohnny Huang 	data_dw = image_layout->data_length / 4;
1407f347c284SJohnny Huang 
1408f347c284SJohnny Huang 	printf("Read OTP Data:\n");
1409f347c284SJohnny Huang 
1410f347c284SJohnny Huang 	for (i = 0; i < data_dw - 2 ; i += 2)
1411f347c284SJohnny Huang 		otp_read_data(i, &data[i]);
1412f347c284SJohnny Huang 
1413f347c284SJohnny Huang 	printf("Check writable...\n");
1414f347c284SJohnny Huang 	// ignore last two dw, the last two dw is used for slt otp write check.
1415f347c284SJohnny Huang 	for (i = 0; i < data_dw - 2; i++) {
1416f347c284SJohnny Huang 		data_masked = data[i]  & ~buf_ignore[i];
1417f347c284SJohnny Huang 		buf_masked  = buf[i] & ~buf_ignore[i];
1418f347c284SJohnny Huang 		if (data_masked == buf_masked)
1419f347c284SJohnny Huang 			continue;
1420f347c284SJohnny Huang 		if (i % 2 == 0) {
1421f347c284SJohnny Huang 			if ((data_masked | buf_masked) == buf_masked) {
1422f347c284SJohnny Huang 				continue;
1423f347c284SJohnny Huang 			} else {
1424f347c284SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
1425f347c284SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
1426f347c284SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
1427f347c284SJohnny Huang 				printf("Mask    [%x] = %x\n", i, ~buf_ignore[i]);
1428f347c284SJohnny Huang 				return OTP_FAILURE;
1429f347c284SJohnny Huang 			}
1430f347c284SJohnny Huang 		} else {
1431f347c284SJohnny Huang 			if ((data_masked & buf_masked) == buf_masked) {
1432f347c284SJohnny Huang 				continue;
1433f347c284SJohnny Huang 			} else {
1434f347c284SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
1435f347c284SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
1436f347c284SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
1437f347c284SJohnny Huang 				printf("Mask    [%x] = %x\n", i, ~buf_ignore[i]);
1438f347c284SJohnny Huang 				return OTP_FAILURE;
1439f347c284SJohnny Huang 			}
1440f347c284SJohnny Huang 		}
1441f347c284SJohnny Huang 	}
1442f347c284SJohnny Huang 
1443f347c284SJohnny Huang 	printf("Start Programing...\n");
1444f347c284SJohnny Huang 
1445f347c284SJohnny Huang 	// programing ecc region first
1446f347c284SJohnny Huang 	for (i = 1792; i < 2046; i += 2) {
1447f347c284SJohnny Huang 		ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i);
1448f347c284SJohnny Huang 		if (ret != OTP_SUCCESS) {
1449f347c284SJohnny Huang 			printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n",
1450f347c284SJohnny Huang 			       i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]);
1451f347c284SJohnny Huang 			return ret;
1452f347c284SJohnny Huang 		}
1453f347c284SJohnny Huang 	}
1454f347c284SJohnny Huang 
1455f347c284SJohnny Huang 	for (i = 0; i < 1792; i += 2) {
1456f347c284SJohnny Huang 		ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i);
1457f347c284SJohnny Huang 		if (ret != OTP_SUCCESS) {
1458f347c284SJohnny Huang 			printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n",
1459f347c284SJohnny Huang 			       i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]);
1460f347c284SJohnny Huang 			return ret;
1461f347c284SJohnny Huang 		}
1462f347c284SJohnny Huang 	}
1463f347c284SJohnny Huang 	otp_soak(0);
1464f347c284SJohnny Huang 	return OTP_SUCCESS;
1465f347c284SJohnny Huang }
1466f347c284SJohnny Huang 
1467f347c284SJohnny Huang static int otp_prog_strap(struct otp_image_layout *image_layout)
1468f347c284SJohnny Huang {
1469f347c284SJohnny Huang 	u32 *strap;
1470f347c284SJohnny Huang 	u32 *strap_ignore;
1471f347c284SJohnny Huang 	u32 *strap_pro;
1472f347c284SJohnny Huang 	u32 *strap_reg_protect;
1473f347c284SJohnny Huang 	u32 prog_address;
1474f347c284SJohnny Huang 	int i;
1475f347c284SJohnny Huang 	int bit, pbit, ibit, offset, rpbit;
1476f347c284SJohnny Huang 	int fail = 0;
1477f347c284SJohnny Huang 	int ret;
1478f347c284SJohnny Huang 	int prog_flag = 0;
1479f347c284SJohnny Huang 	struct otpstrap_status otpstrap[64];
1480f347c284SJohnny Huang 
1481f347c284SJohnny Huang 	strap = (u32 *)image_layout->strap;
1482f347c284SJohnny Huang 	strap_pro = (u32 *)image_layout->strap_pro;
1483f347c284SJohnny Huang 	strap_ignore = (u32 *)image_layout->strap_ignore;
1484f347c284SJohnny Huang 	strap_reg_protect = (u32 *)image_layout->strap_reg_pro;
1485f347c284SJohnny Huang 
1486f347c284SJohnny Huang 	printf("Read OTP Strap Region:\n");
1487f347c284SJohnny Huang 	otp_strap_status(otpstrap);
1488f347c284SJohnny Huang 
1489f347c284SJohnny Huang 	printf("Check writable...\n");
1490f347c284SJohnny Huang 	if (otp_strap_image_confirm(image_layout) == OTP_FAILURE) {
1491f347c284SJohnny Huang 		printf("Input image can't program into OTP, please check.\n");
1492f347c284SJohnny Huang 		return OTP_FAILURE;
1493f347c284SJohnny Huang 	}
1494f347c284SJohnny Huang 
1495f347c284SJohnny Huang 	for (i = 0; i < 64; i++) {
1496f347c284SJohnny Huang 		prog_address = 0x800;
1497f347c284SJohnny Huang 		if (i < 32) {
1498f347c284SJohnny Huang 			offset = i;
1499f347c284SJohnny Huang 			bit = (strap[0] >> offset) & 0x1;
1500f347c284SJohnny Huang 			ibit = (strap_ignore[0] >> offset) & 0x1;
1501f347c284SJohnny Huang 			pbit = (strap_pro[0] >> offset) & 0x1;
1502f347c284SJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200;
1503f347c284SJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2;
1504f347c284SJohnny Huang 
1505f347c284SJohnny Huang 		} else {
1506f347c284SJohnny Huang 			offset = (i - 32);
1507f347c284SJohnny Huang 			bit = (strap[1] >> offset) & 0x1;
1508f347c284SJohnny Huang 			ibit = (strap_ignore[1] >> offset) & 0x1;
1509f347c284SJohnny Huang 			pbit = (strap_pro[1] >> offset) & 0x1;
1510f347c284SJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200;
1511f347c284SJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2;
1512f347c284SJohnny Huang 		}
1513f347c284SJohnny Huang 		if (info_cb.version != OTP_A0) {
1514f347c284SJohnny Huang 			if (i < 32)
1515f347c284SJohnny Huang 				rpbit = (strap_reg_protect[0] >> i) & 0x1;
1516f347c284SJohnny Huang 			else
1517f347c284SJohnny Huang 				rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1;
1518f347c284SJohnny Huang 		} else {
1519f347c284SJohnny Huang 			rpbit = 0;
1520f347c284SJohnny Huang 		}
1521f347c284SJohnny Huang 
1522f347c284SJohnny Huang 		if (ibit == 1)
1523f347c284SJohnny Huang 			continue;
1524f347c284SJohnny Huang 		if (bit == otpstrap[i].value)
1525f347c284SJohnny Huang 			prog_flag = 0;
1526f347c284SJohnny Huang 		else
1527f347c284SJohnny Huang 			prog_flag = 1;
1528f347c284SJohnny Huang 
1529f347c284SJohnny Huang 		if (otpstrap[i].protected == 1 && prog_flag) {
1530f347c284SJohnny Huang 			fail = 1;
1531f347c284SJohnny Huang 			continue;
1532f347c284SJohnny Huang 		}
1533f347c284SJohnny Huang 		if (otpstrap[i].remain_times == 0 && prog_flag) {
1534f347c284SJohnny Huang 			fail = 1;
1535f347c284SJohnny Huang 			continue;
1536f347c284SJohnny Huang 		}
1537f347c284SJohnny Huang 
1538f347c284SJohnny Huang 		if (prog_flag) {
1539f347c284SJohnny Huang 			ret = otp_prog_dc_b(1, prog_address, offset);
1540f347c284SJohnny Huang 			if (ret)
1541f347c284SJohnny Huang 				return OTP_FAILURE;
1542f347c284SJohnny Huang 		}
1543f347c284SJohnny Huang 
1544f347c284SJohnny Huang 		if (rpbit == 1 && info_cb.version != OTP_A0) {
1545f347c284SJohnny Huang 			prog_address = 0x800;
1546f347c284SJohnny Huang 			if (i < 32)
1547f347c284SJohnny Huang 				prog_address |= 0x608;
1548f347c284SJohnny Huang 			else
1549f347c284SJohnny Huang 				prog_address |= 0x60a;
1550f347c284SJohnny Huang 
1551f347c284SJohnny Huang 			ret = otp_prog_dc_b(1, prog_address, offset);
1552f347c284SJohnny Huang 			if (ret)
1553f347c284SJohnny Huang 				return OTP_FAILURE;
1554f347c284SJohnny Huang 		}
1555f347c284SJohnny Huang 
1556f347c284SJohnny Huang 		if (pbit != 0) {
1557f347c284SJohnny Huang 			prog_address = 0x800;
1558f347c284SJohnny Huang 			if (i < 32)
1559f347c284SJohnny Huang 				prog_address |= 0x60c;
1560f347c284SJohnny Huang 			else
1561f347c284SJohnny Huang 				prog_address |= 0x60e;
1562f347c284SJohnny Huang 
1563f347c284SJohnny Huang 			ret = otp_prog_dc_b(1, prog_address, offset);
1564f347c284SJohnny Huang 			if (ret)
1565f347c284SJohnny Huang 				return OTP_FAILURE;
1566f347c284SJohnny Huang 		}
1567f347c284SJohnny Huang 	}
1568f347c284SJohnny Huang 	otp_soak(0);
1569f347c284SJohnny Huang 	if (fail == 1)
1570f347c284SJohnny Huang 		return OTP_FAILURE;
1571f347c284SJohnny Huang 	return OTP_SUCCESS;
157269d5fd8fSJohnny Huang }
157369d5fd8fSJohnny Huang 
15745010032bSJohnny Huang static int otp_prog_conf(struct otp_image_layout *image_layout)
157569d5fd8fSJohnny Huang {
1576a6d0d645SJohnny Huang 	int i, k;
1577d90825e2SJohnny Huang 	int pass = 0;
1578a219f6deSJohnny Huang 	u32 prog_address;
1579a219f6deSJohnny Huang 	u32 data[16];
1580a219f6deSJohnny Huang 	u32 compare[2];
1581a219f6deSJohnny Huang 	u32 *conf = (u32 *)image_layout->conf;
1582a219f6deSJohnny Huang 	u32 *conf_ignore = (u32 *)image_layout->conf_ignore;
1583a219f6deSJohnny Huang 	u32 data_masked;
1584a219f6deSJohnny Huang 	u32 buf_masked;
158569d5fd8fSJohnny Huang 
1586a6d0d645SJohnny Huang 	printf("Read OTP Config Region:\n");
1587a6d0d645SJohnny Huang 
1588bb34a7bfSJohnny Huang 	for (i = 0; i < 16 ; i++) {
158969d5fd8fSJohnny Huang 		prog_address = 0x800;
1590a6d0d645SJohnny Huang 		prog_address |= (i / 8) * 0x200;
1591a6d0d645SJohnny Huang 		prog_address |= (i % 8) * 0x2;
1592a6d0d645SJohnny Huang 		otp_read_data(prog_address, &data[i]);
1593a6d0d645SJohnny Huang 	}
1594a6d0d645SJohnny Huang 
1595a6d0d645SJohnny Huang 	printf("Check writable...\n");
1596bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++) {
15975010032bSJohnny Huang 		data_masked = data[i]  & ~conf_ignore[i];
15985010032bSJohnny Huang 		buf_masked  = conf[i] & ~conf_ignore[i];
1599d90825e2SJohnny Huang 		if (data_masked == buf_masked)
160069d5fd8fSJohnny Huang 			continue;
1601d90825e2SJohnny Huang 		if ((data_masked | buf_masked) == buf_masked) {
1602a6d0d645SJohnny Huang 			continue;
1603a6d0d645SJohnny Huang 		} else {
1604a6d0d645SJohnny Huang 			printf("Input image can't program into OTP, please check.\n");
1605a6af4a17SJohnny Huang 			printf("OTPCFG[%X] = %x\n", i, data[i]);
16065010032bSJohnny Huang 			printf("Input [%X] = %x\n", i, conf[i]);
16075010032bSJohnny Huang 			printf("Mask  [%X] = %x\n", i, ~conf_ignore[i]);
16082a856b9aSJohnny Huang 			return OTP_FAILURE;
1609a6d0d645SJohnny Huang 		}
1610a6d0d645SJohnny Huang 	}
1611a6d0d645SJohnny Huang 
1612a6d0d645SJohnny Huang 	printf("Start Programing...\n");
1613d90825e2SJohnny Huang 	otp_soak(0);
1614bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++) {
16155010032bSJohnny Huang 		data_masked = data[i]  & ~conf_ignore[i];
16165010032bSJohnny Huang 		buf_masked  = conf[i] & ~conf_ignore[i];
1617a6d0d645SJohnny Huang 		prog_address = 0x800;
1618a6d0d645SJohnny Huang 		prog_address |= (i / 8) * 0x200;
1619a6d0d645SJohnny Huang 		prog_address |= (i % 8) * 0x2;
1620bb34a7bfSJohnny Huang 		if (data_masked == buf_masked) {
1621bb34a7bfSJohnny Huang 			pass = 1;
1622a6d0d645SJohnny Huang 			continue;
1623bb34a7bfSJohnny Huang 		}
1624de6fbf1cSJohnny Huang 
1625de6fbf1cSJohnny Huang 		otp_soak(1);
16265010032bSJohnny Huang 		otp_prog_dw(conf[i], conf_ignore[i], prog_address);
1627a6d0d645SJohnny Huang 
162869d5fd8fSJohnny Huang 		pass = 0;
162969d5fd8fSJohnny Huang 		for (k = 0; k < RETRY; k++) {
16305010032bSJohnny Huang 			if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) {
1631de6fbf1cSJohnny Huang 				otp_soak(2);
1632feea3fdfSJohnny Huang 				otp_prog_dw(compare[0], conf_ignore[i], prog_address);
16335010032bSJohnny Huang 				if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) {
1634de6fbf1cSJohnny Huang 					otp_soak(1);
1635de6fbf1cSJohnny Huang 				} else {
1636de6fbf1cSJohnny Huang 					pass = 1;
1637de6fbf1cSJohnny Huang 					break;
1638de6fbf1cSJohnny Huang 				}
1639a6d0d645SJohnny Huang 			} else {
164069d5fd8fSJohnny Huang 				pass = 1;
164169d5fd8fSJohnny Huang 				break;
164269d5fd8fSJohnny Huang 			}
164369d5fd8fSJohnny Huang 		}
1644bb34a7bfSJohnny Huang 		if (pass == 0) {
1645bb34a7bfSJohnny Huang 			printf("address: %08x, data: %08x, buffer: %08x, mask: %08x\n",
16465010032bSJohnny Huang 			       i, data[i], conf[i], conf_ignore[i]);
1647bb34a7bfSJohnny Huang 			break;
1648bb34a7bfSJohnny Huang 		}
1649a6d0d645SJohnny Huang 	}
1650a6d0d645SJohnny Huang 
1651de6fbf1cSJohnny Huang 	otp_soak(0);
165269d5fd8fSJohnny Huang 	if (!pass)
16532a856b9aSJohnny Huang 		return OTP_FAILURE;
1654a6d0d645SJohnny Huang 
16552a856b9aSJohnny Huang 	return OTP_SUCCESS;
165669d5fd8fSJohnny Huang }
165769d5fd8fSJohnny Huang 
1658f347c284SJohnny Huang static int otp_verify_image(u8 *src_buf, u32 length, u8 *digest_buf)
1659696656c6SJohnny Huang {
1660696656c6SJohnny Huang 	sha256_context ctx;
1661696656c6SJohnny Huang 	u8 digest_ret[CHECKSUM_LEN];
1662696656c6SJohnny Huang 
1663696656c6SJohnny Huang 	sha256_starts(&ctx);
1664696656c6SJohnny Huang 	sha256_update(&ctx, src_buf, length);
1665696656c6SJohnny Huang 	sha256_finish(&ctx, digest_ret);
1666696656c6SJohnny Huang 
1667696656c6SJohnny Huang 	if (!memcmp(digest_buf, digest_ret, CHECKSUM_LEN))
1668f347c284SJohnny Huang 		return OTP_SUCCESS;
1669f347c284SJohnny Huang 	return OTP_FAILURE;
1670696656c6SJohnny Huang }
1671696656c6SJohnny Huang 
1672f347c284SJohnny Huang static int otp_prog_image(int addr, int nconfirm)
167369d5fd8fSJohnny Huang {
167469d5fd8fSJohnny Huang 	int ret;
16759a4fe690SJohnny Huang 	int image_version = 0;
1676696656c6SJohnny Huang 	struct otp_header *otp_header;
1677696656c6SJohnny Huang 	struct otp_image_layout image_layout;
1678696656c6SJohnny Huang 	int image_size;
1679a219f6deSJohnny Huang 	u8 *buf;
1680a219f6deSJohnny Huang 	u8 *checksum;
168169d5fd8fSJohnny Huang 
1682696656c6SJohnny Huang 	otp_header = map_physmem(addr, sizeof(struct otp_header), MAP_WRBACK);
1683696656c6SJohnny Huang 	if (!otp_header) {
168469d5fd8fSJohnny Huang 		puts("Failed to map physical memory\n");
16852a856b9aSJohnny Huang 		return OTP_FAILURE;
168669d5fd8fSJohnny Huang 	}
1687d90825e2SJohnny Huang 
1688696656c6SJohnny Huang 	image_size = OTP_IMAGE_SIZE(otp_header->image_info);
1689696656c6SJohnny Huang 	unmap_physmem(otp_header, MAP_WRBACK);
1690696656c6SJohnny Huang 
1691696656c6SJohnny Huang 	buf = map_physmem(addr, image_size + CHECKSUM_LEN, MAP_WRBACK);
1692696656c6SJohnny Huang 
1693696656c6SJohnny Huang 	if (!buf) {
1694696656c6SJohnny Huang 		puts("Failed to map physical memory\n");
1695696656c6SJohnny Huang 		return OTP_FAILURE;
1696696656c6SJohnny Huang 	}
1697696656c6SJohnny Huang 	otp_header = (struct otp_header *)buf;
1698696656c6SJohnny Huang 	checksum = buf + otp_header->checksum_offset;
1699696656c6SJohnny Huang 
1700696656c6SJohnny Huang 	if (strcmp(OTP_MAGIC, (char *)otp_header->otp_magic) != 0) {
1701696656c6SJohnny Huang 		puts("Image is invalid\n");
1702696656c6SJohnny Huang 		return OTP_FAILURE;
1703696656c6SJohnny Huang 	}
1704696656c6SJohnny Huang 
17055010032bSJohnny Huang 	image_layout.data_length = (int)(OTP_REGION_SIZE(otp_header->data_info) / 2);
17065010032bSJohnny Huang 	image_layout.data = buf + OTP_REGION_OFFSET(otp_header->data_info);
17075010032bSJohnny Huang 	image_layout.data_ignore = image_layout.data + image_layout.data_length;
17085010032bSJohnny Huang 
17095010032bSJohnny Huang 	image_layout.conf_length = (int)(OTP_REGION_SIZE(otp_header->config_info) / 2);
1710696656c6SJohnny Huang 	image_layout.conf = buf + OTP_REGION_OFFSET(otp_header->config_info);
17115010032bSJohnny Huang 	image_layout.conf_ignore = image_layout.conf + image_layout.conf_length;
1712696656c6SJohnny Huang 
1713696656c6SJohnny Huang 	image_layout.strap = buf + OTP_REGION_OFFSET(otp_header->strap_info);
1714696656c6SJohnny Huang 
1715696656c6SJohnny Huang 	if (!strcmp("A0", (char *)otp_header->otp_version)) {
1716e417205bSJohnny Huang 		image_version = OTP_A0;
17175010032bSJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 3);
17185010032bSJohnny Huang 		image_layout.strap_pro = image_layout.strap + image_layout.strap_length;
17195010032bSJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 2 * image_layout.strap_length;
1720696656c6SJohnny Huang 	} else if (!strcmp("A1", (char *)otp_header->otp_version)) {
1721e417205bSJohnny Huang 		image_version = OTP_A1;
17225010032bSJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4);
17235010032bSJohnny Huang 		image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length;
17245010032bSJohnny Huang 		image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length;
17255010032bSJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length;
17265fdde29fSJohnny Huang 	} else if (!strcmp("A2", (char *)otp_header->otp_version)) {
1727e417205bSJohnny Huang 		image_version = OTP_A2;
17285fdde29fSJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4);
17295fdde29fSJohnny Huang 		image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length;
17305fdde29fSJohnny Huang 		image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length;
17315fdde29fSJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length;
173264b66712SJohnny Huang 	} else if (!strcmp("A3", (char *)otp_header->otp_version)) {
1733e417205bSJohnny Huang 		image_version = OTP_A3;
173464b66712SJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4);
173564b66712SJohnny Huang 		image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length;
173664b66712SJohnny Huang 		image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length;
173764b66712SJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length;
1738696656c6SJohnny Huang 	} else {
1739696656c6SJohnny Huang 		puts("Version is not supported\n");
1740696656c6SJohnny Huang 		return OTP_FAILURE;
1741696656c6SJohnny Huang 	}
1742696656c6SJohnny Huang 
17439a4fe690SJohnny Huang 	if (image_version != info_cb.version) {
17449a4fe690SJohnny Huang 		puts("Version is not match\n");
17459a4fe690SJohnny Huang 		return OTP_FAILURE;
17469a4fe690SJohnny Huang 	}
17479a4fe690SJohnny Huang 
1748f347c284SJohnny Huang 	if (otp_verify_image(buf, image_size, checksum)) {
1749696656c6SJohnny Huang 		puts("checksum is invalid\n");
1750696656c6SJohnny Huang 		return OTP_FAILURE;
1751d90825e2SJohnny Huang 	}
17527332532cSJohnny Huang 
175369d5fd8fSJohnny Huang 	if (!nconfirm) {
1754696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_DATA) {
17557f795e57SJohnny Huang 			printf("\nOTP data region :\n");
1756f347c284SJohnny Huang 			if (otp_print_data_image(&image_layout) < 0) {
175769d5fd8fSJohnny Huang 				printf("OTP data error, please check.\n");
17582a856b9aSJohnny Huang 				return OTP_FAILURE;
175969d5fd8fSJohnny Huang 			}
176069d5fd8fSJohnny Huang 		}
1761696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_CONFIG) {
17627332532cSJohnny Huang 			printf("\nOTP configuration region :\n");
1763696656c6SJohnny Huang 			if (otp_print_conf_image(&image_layout) < 0) {
17647332532cSJohnny Huang 				printf("OTP config error, please check.\n");
17657332532cSJohnny Huang 				return OTP_FAILURE;
17667332532cSJohnny Huang 			}
17677332532cSJohnny Huang 		}
17687adec5f6SJohnny Huang 		if (otp_header->image_info & OTP_INC_STRAP) {
17697adec5f6SJohnny Huang 			printf("\nOTP strap region :\n");
17707adec5f6SJohnny Huang 			if (otp_print_strap_image(&image_layout) < 0) {
17717adec5f6SJohnny Huang 				printf("OTP strap error, please check.\n");
17727adec5f6SJohnny Huang 				return OTP_FAILURE;
17737adec5f6SJohnny Huang 			}
17747adec5f6SJohnny Huang 		}
17757332532cSJohnny Huang 
177669d5fd8fSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
177769d5fd8fSJohnny Huang 		if (!confirm_yesno()) {
177869d5fd8fSJohnny Huang 			printf(" Aborting\n");
17792a856b9aSJohnny Huang 			return OTP_FAILURE;
178069d5fd8fSJohnny Huang 		}
178169d5fd8fSJohnny Huang 	}
17827332532cSJohnny Huang 
17835010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_DATA) {
17845010032bSJohnny Huang 		printf("programing data region ...\n");
17855010032bSJohnny Huang 		ret = otp_prog_data(&image_layout);
17865010032bSJohnny Huang 		if (ret != 0) {
17875010032bSJohnny Huang 			printf("Error\n");
17885010032bSJohnny Huang 			return ret;
17895010032bSJohnny Huang 		}
1790a219f6deSJohnny Huang 		printf("Done\n");
17915010032bSJohnny Huang 	}
17925010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_STRAP) {
17935010032bSJohnny Huang 		printf("programing strap region ...\n");
17945010032bSJohnny Huang 		ret = otp_prog_strap(&image_layout);
17955010032bSJohnny Huang 		if (ret != 0) {
17965010032bSJohnny Huang 			printf("Error\n");
17975010032bSJohnny Huang 			return ret;
17985010032bSJohnny Huang 		}
1799a219f6deSJohnny Huang 		printf("Done\n");
18005010032bSJohnny Huang 	}
18015010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_CONFIG) {
18025010032bSJohnny Huang 		printf("programing configuration region ...\n");
18035010032bSJohnny Huang 		ret = otp_prog_conf(&image_layout);
18045010032bSJohnny Huang 		if (ret != 0) {
18055010032bSJohnny Huang 			printf("Error\n");
18065010032bSJohnny Huang 			return ret;
18075010032bSJohnny Huang 		}
18085010032bSJohnny Huang 		printf("Done\n");
18095010032bSJohnny Huang 	}
1810cd1610b4SJohnny Huang 
18117332532cSJohnny Huang 	return OTP_SUCCESS;
18122a856b9aSJohnny Huang }
18132a856b9aSJohnny Huang 
1814f347c284SJohnny Huang static int otp_prog_bit(int mode, int otp_dw_offset, int bit_offset, int value, int nconfirm)
1815cd1610b4SJohnny Huang {
1816a219f6deSJohnny Huang 	u32 read[2];
1817a219f6deSJohnny Huang 	u32 prog_address = 0;
181866f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
1819cd1610b4SJohnny Huang 	int otp_bit;
182083655e91SJohnny Huang 	int ret = 0;
1821cd1610b4SJohnny Huang 
1822dacbba92SJohnny Huang 	otp_soak(0);
1823cd1610b4SJohnny Huang 	switch (mode) {
1824a6d0d645SJohnny Huang 	case OTP_REGION_CONF:
1825f347c284SJohnny Huang 		otp_read_conf(otp_dw_offset, read);
1826cd1610b4SJohnny Huang 		prog_address = 0x800;
1827cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset / 8) * 0x200;
1828cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset % 8) * 0x2;
1829a6af4a17SJohnny Huang 		otp_bit = (read[0] >> bit_offset) & 0x1;
1830cd1610b4SJohnny Huang 		if (otp_bit == value) {
1831a6af4a17SJohnny Huang 			printf("OTPCFG%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
1832cd1610b4SJohnny Huang 			printf("No need to program\n");
18332a856b9aSJohnny Huang 			return OTP_SUCCESS;
1834cd1610b4SJohnny Huang 		}
1835cd1610b4SJohnny Huang 		if (otp_bit == 1 && value == 0) {
1836a6af4a17SJohnny Huang 			printf("OTPCFG%X[%X] = 1\n", otp_dw_offset, bit_offset);
1837cd1610b4SJohnny Huang 			printf("OTP is programed, which can't be clean\n");
18382a856b9aSJohnny Huang 			return OTP_FAILURE;
1839cd1610b4SJohnny Huang 		}
1840a6af4a17SJohnny Huang 		printf("Program OTPCFG%X[%X] to 1\n", otp_dw_offset, bit_offset);
1841cd1610b4SJohnny Huang 		break;
1842a6d0d645SJohnny Huang 	case OTP_REGION_DATA:
1843cd1610b4SJohnny Huang 		prog_address = otp_dw_offset;
1844cd1610b4SJohnny Huang 
1845cd1610b4SJohnny Huang 		if (otp_dw_offset % 2 == 0) {
1846a6af4a17SJohnny Huang 			otp_read_data(otp_dw_offset, read);
1847a6af4a17SJohnny Huang 			otp_bit = (read[0] >> bit_offset) & 0x1;
1848643b9cfdSJohnny Huang 
1849643b9cfdSJohnny Huang 			if (otp_bit == 1 && value == 0) {
1850643b9cfdSJohnny Huang 				printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
1851643b9cfdSJohnny Huang 				printf("OTP is programed, which can't be cleaned\n");
1852643b9cfdSJohnny Huang 				return OTP_FAILURE;
1853643b9cfdSJohnny Huang 			}
1854cd1610b4SJohnny Huang 		} else {
1855a6af4a17SJohnny Huang 			otp_read_data(otp_dw_offset - 1, read);
1856a6af4a17SJohnny Huang 			otp_bit = (read[1] >> bit_offset) & 0x1;
1857643b9cfdSJohnny Huang 
1858643b9cfdSJohnny Huang 			if (otp_bit == 0 && value == 1) {
1859643b9cfdSJohnny Huang 				printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
1860643b9cfdSJohnny Huang 				printf("OTP is programed, which can't be writen\n");
1861643b9cfdSJohnny Huang 				return OTP_FAILURE;
1862643b9cfdSJohnny Huang 			}
1863cd1610b4SJohnny Huang 		}
1864cd1610b4SJohnny Huang 		if (otp_bit == value) {
1865a6af4a17SJohnny Huang 			printf("OTPDATA%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
1866cd1610b4SJohnny Huang 			printf("No need to program\n");
18672a856b9aSJohnny Huang 			return OTP_SUCCESS;
1868cd1610b4SJohnny Huang 		}
1869643b9cfdSJohnny Huang 
1870a6af4a17SJohnny Huang 		printf("Program OTPDATA%X[%X] to 1\n", otp_dw_offset, bit_offset);
1871cd1610b4SJohnny Huang 		break;
1872a6d0d645SJohnny Huang 	case OTP_REGION_STRAP:
18738848d5dcSJohnny Huang 		otp_strap_status(otpstrap);
18748848d5dcSJohnny Huang 		otp_print_strap(bit_offset, 1);
18758848d5dcSJohnny Huang 		ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0, 0);
18768848d5dcSJohnny Huang 		if (ret == OTP_FAILURE)
18778848d5dcSJohnny Huang 			return OTP_FAILURE;
18788848d5dcSJohnny Huang 		else if (ret == OTP_PROG_SKIP)
18798848d5dcSJohnny Huang 			return OTP_SUCCESS;
1880a6af4a17SJohnny Huang 
1881cd1610b4SJohnny Huang 		break;
1882cd1610b4SJohnny Huang 	}
1883cd1610b4SJohnny Huang 
1884cd1610b4SJohnny Huang 	if (!nconfirm) {
1885cd1610b4SJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
1886cd1610b4SJohnny Huang 		if (!confirm_yesno()) {
1887cd1610b4SJohnny Huang 			printf(" Aborting\n");
18882a856b9aSJohnny Huang 			return OTP_FAILURE;
1889cd1610b4SJohnny Huang 		}
1890cd1610b4SJohnny Huang 	}
1891cd1610b4SJohnny Huang 
1892cd1610b4SJohnny Huang 	switch (mode) {
1893a6d0d645SJohnny Huang 	case OTP_REGION_STRAP:
1894f347c284SJohnny Huang 		ret =  otp_prog_strap_b(bit_offset, value);
189583655e91SJohnny Huang 		break;
1896a6d0d645SJohnny Huang 	case OTP_REGION_CONF:
1897a6d0d645SJohnny Huang 	case OTP_REGION_DATA:
1898f347c284SJohnny Huang 		ret = otp_prog_dc_b(value, prog_address, bit_offset);
1899de6fbf1cSJohnny Huang 		break;
1900de6fbf1cSJohnny Huang 	}
1901de6fbf1cSJohnny Huang 	otp_soak(0);
190283655e91SJohnny Huang 	if (ret) {
1903794e27ecSJohnny Huang 		printf("OTP cannot be programed\n");
1904794e27ecSJohnny Huang 		printf("FAILURE\n");
1905794e27ecSJohnny Huang 		return OTP_FAILURE;
1906794e27ecSJohnny Huang 	}
1907794e27ecSJohnny Huang 
19089009c25dSJohnny Huang 	printf("SUCCESS\n");
19092a856b9aSJohnny Huang 	return OTP_SUCCESS;
1910a219f6deSJohnny Huang }
1911a219f6deSJohnny Huang 
1912794e27ecSJohnny Huang static int otp_update_rid(u32 update_num, int force)
1913794e27ecSJohnny Huang {
1914794e27ecSJohnny Huang 	u32 otp_rid[2];
1915a8789b47SJohnny Huang 	u32 sw_rid[2];
1916794e27ecSJohnny Huang 	int rid_num = 0;
1917a8789b47SJohnny Huang 	int sw_rid_num = 0;
1918794e27ecSJohnny Huang 	int bit_offset;
1919794e27ecSJohnny Huang 	int dw_offset;
1920794e27ecSJohnny Huang 	int i;
1921794e27ecSJohnny Huang 	int ret;
1922794e27ecSJohnny Huang 
1923f347c284SJohnny Huang 	otp_read_conf(10, &otp_rid[0]);
1924f347c284SJohnny Huang 	otp_read_conf(11, &otp_rid[1]);
1925794e27ecSJohnny Huang 
1926a8789b47SJohnny Huang 	sw_rid[0] = readl(SW_REV_ID0);
1927a8789b47SJohnny Huang 	sw_rid[1] = readl(SW_REV_ID1);
1928a8789b47SJohnny Huang 
1929794e27ecSJohnny Huang 	rid_num = get_rid_num(otp_rid);
1930a8789b47SJohnny Huang 	sw_rid_num = get_rid_num(sw_rid);
1931a8789b47SJohnny Huang 
1932a8789b47SJohnny Huang 	if (sw_rid_num < 0) {
1933a8789b47SJohnny Huang 		printf("SW revision id is invalid, please check.\n");
1934a8789b47SJohnny Huang 		return OTP_FAILURE;
1935a8789b47SJohnny Huang 	}
1936a8789b47SJohnny Huang 
1937a8789b47SJohnny Huang 	if (update_num > sw_rid_num) {
1938a8789b47SJohnny Huang 		printf("current SW revision ID: 0x%x\n", sw_rid_num);
1939a8789b47SJohnny Huang 		printf("update number could not bigger than current SW revision id\n");
1940a8789b47SJohnny Huang 		return OTP_FAILURE;
1941a8789b47SJohnny Huang 	}
1942794e27ecSJohnny Huang 
1943794e27ecSJohnny Huang 	if (rid_num < 0) {
1944794e27ecSJohnny Huang 		printf("Currennt OTP revision ID cannot handle by this command,\n"
1945794e27ecSJohnny Huang 		       "plase use 'otp pb' command to update it manually\n");
1946794e27ecSJohnny Huang 		otp_print_revid(otp_rid);
19479009c25dSJohnny Huang 		return OTP_FAILURE;
19489009c25dSJohnny Huang 	}
1949cd1610b4SJohnny Huang 
1950794e27ecSJohnny Huang 	printf("current OTP revision ID: 0x%x\n", rid_num);
1951794e27ecSJohnny Huang 	otp_print_revid(otp_rid);
1952794e27ecSJohnny Huang 	printf("input update number: 0x%x\n", update_num);
1953794e27ecSJohnny Huang 
1954a8789b47SJohnny Huang 	if (rid_num > update_num) {
1955a8789b47SJohnny Huang 		printf("OTP rev_id is bigger than 0x%X\n", update_num);
1956a8789b47SJohnny Huang 		printf("Skip\n");
1957a8789b47SJohnny Huang 		return OTP_FAILURE;
1958a8789b47SJohnny Huang 	} else if (rid_num == update_num) {
1959a8789b47SJohnny Huang 		printf("OTP rev_id is same as input\n");
1960794e27ecSJohnny Huang 		printf("Skip\n");
1961794e27ecSJohnny Huang 		return OTP_FAILURE;
1962794e27ecSJohnny Huang 	}
1963794e27ecSJohnny Huang 
1964794e27ecSJohnny Huang 	for (i = rid_num; i < update_num; i++) {
1965794e27ecSJohnny Huang 		if (i < 32) {
1966794e27ecSJohnny Huang 			dw_offset = 0xa;
1967794e27ecSJohnny Huang 			bit_offset = i;
1968794e27ecSJohnny Huang 		} else {
1969794e27ecSJohnny Huang 			dw_offset = 0xb;
1970794e27ecSJohnny Huang 			bit_offset = i - 32;
1971794e27ecSJohnny Huang 		}
1972794e27ecSJohnny Huang 		printf("OTPCFG%X[%d]", dw_offset, bit_offset);
1973794e27ecSJohnny Huang 		if (i + 1 != update_num)
1974794e27ecSJohnny Huang 			printf(", ");
1975794e27ecSJohnny Huang 	}
1976794e27ecSJohnny Huang 
1977794e27ecSJohnny Huang 	printf(" will be programmed\n");
1978794e27ecSJohnny Huang 	if (force == 0) {
1979794e27ecSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
1980794e27ecSJohnny Huang 		if (!confirm_yesno()) {
1981794e27ecSJohnny Huang 			printf(" Aborting\n");
1982794e27ecSJohnny Huang 			return OTP_FAILURE;
1983794e27ecSJohnny Huang 		}
1984794e27ecSJohnny Huang 	}
1985794e27ecSJohnny Huang 
1986794e27ecSJohnny Huang 	ret = 0;
1987794e27ecSJohnny Huang 	for (i = rid_num; i < update_num; i++) {
1988794e27ecSJohnny Huang 		if (i < 32) {
1989794e27ecSJohnny Huang 			dw_offset = 0xa04;
1990794e27ecSJohnny Huang 			bit_offset = i;
1991794e27ecSJohnny Huang 		} else {
1992794e27ecSJohnny Huang 			dw_offset = 0xa06;
1993794e27ecSJohnny Huang 			bit_offset = i - 32;
1994794e27ecSJohnny Huang 		}
1995f347c284SJohnny Huang 		if (otp_prog_dc_b(1, dw_offset, bit_offset)) {
1996794e27ecSJohnny Huang 			printf("OTPCFG%X[%d] programming failed\n", dw_offset, bit_offset);
1997794e27ecSJohnny Huang 			ret = OTP_FAILURE;
1998794e27ecSJohnny Huang 			break;
1999794e27ecSJohnny Huang 		}
2000794e27ecSJohnny Huang 	}
2001794e27ecSJohnny Huang 
2002f347c284SJohnny Huang 	otp_read_conf(10, &otp_rid[0]);
2003f347c284SJohnny Huang 	otp_read_conf(11, &otp_rid[1]);
2004794e27ecSJohnny Huang 	rid_num = get_rid_num(otp_rid);
2005794e27ecSJohnny Huang 	if (rid_num >= 0)
2006794e27ecSJohnny Huang 		printf("OTP revision ID: 0x%x\n", rid_num);
2007794e27ecSJohnny Huang 	else
2008794e27ecSJohnny Huang 		printf("OTP revision ID\n");
2009794e27ecSJohnny Huang 	otp_print_revid(otp_rid);
2010794e27ecSJohnny Huang 	if (!ret)
2011794e27ecSJohnny Huang 		printf("SUCCESS\n");
2012794e27ecSJohnny Huang 	else
2013794e27ecSJohnny Huang 		printf("FAILED\n");
2014794e27ecSJohnny Huang 	return ret;
2015794e27ecSJohnny Huang }
2016794e27ecSJohnny Huang 
20172a856b9aSJohnny Huang static int do_otpread(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
201869d5fd8fSJohnny Huang {
2019a219f6deSJohnny Huang 	u32 offset, count;
20202a856b9aSJohnny Huang 	int ret;
202169d5fd8fSJohnny Huang 
20222a856b9aSJohnny Huang 	if (argc == 4) {
20232a856b9aSJohnny Huang 		offset = simple_strtoul(argv[2], NULL, 16);
20242a856b9aSJohnny Huang 		count = simple_strtoul(argv[3], NULL, 16);
20252a856b9aSJohnny Huang 	} else if (argc == 3) {
20262a856b9aSJohnny Huang 		offset = simple_strtoul(argv[2], NULL, 16);
20272a856b9aSJohnny Huang 		count = 1;
20282a856b9aSJohnny Huang 	} else {
202969d5fd8fSJohnny Huang 		return CMD_RET_USAGE;
203069d5fd8fSJohnny Huang 	}
203169d5fd8fSJohnny Huang 
20322a856b9aSJohnny Huang 	if (!strcmp(argv[1], "conf")) {
20333d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2034f347c284SJohnny Huang 		ret = otp_print_conf(offset, count);
20352a856b9aSJohnny Huang 	} else if (!strcmp(argv[1], "data")) {
20363d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
20372a856b9aSJohnny Huang 		ret = otp_print_data(offset, count);
20382a856b9aSJohnny Huang 	} else if (!strcmp(argv[1], "strap")) {
20393d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
20402a856b9aSJohnny Huang 		ret = otp_print_strap(offset, count);
20412a856b9aSJohnny Huang 	} else {
20422a856b9aSJohnny Huang 		return CMD_RET_USAGE;
204369d5fd8fSJohnny Huang 	}
204469d5fd8fSJohnny Huang 
20452a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
20462a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
20472a856b9aSJohnny Huang 	return CMD_RET_USAGE;
20482a856b9aSJohnny Huang }
20492a856b9aSJohnny Huang 
20502a856b9aSJohnny Huang static int do_otpprog(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
20512a856b9aSJohnny Huang {
20522a856b9aSJohnny Huang 	phys_addr_t addr;
20532a856b9aSJohnny Huang 	int ret;
20542a856b9aSJohnny Huang 
2055de6b0cc4SJohnny Huang 	if (argc == 3) {
2056ed071a2bSJohnny Huang 		if (strcmp(argv[1], "o"))
20572a856b9aSJohnny Huang 			return CMD_RET_USAGE;
20582a856b9aSJohnny Huang 		addr = simple_strtoul(argv[2], NULL, 16);
20593d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2060f347c284SJohnny Huang 		ret = otp_prog_image(addr, 1);
2061de6b0cc4SJohnny Huang 	} else if (argc == 2) {
20622a856b9aSJohnny Huang 		addr = simple_strtoul(argv[1], NULL, 16);
20633d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2064f347c284SJohnny Huang 		ret = otp_prog_image(addr, 0);
20652a856b9aSJohnny Huang 	} else {
20662a856b9aSJohnny Huang 		return CMD_RET_USAGE;
20672a856b9aSJohnny Huang 	}
20682a856b9aSJohnny Huang 
20692a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
20702a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
20712a856b9aSJohnny Huang 	else if (ret == OTP_FAILURE)
20722a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
20732a856b9aSJohnny Huang 	else
20742a856b9aSJohnny Huang 		return CMD_RET_USAGE;
20752a856b9aSJohnny Huang }
20762a856b9aSJohnny Huang 
20772a856b9aSJohnny Huang static int do_otppb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
20782a856b9aSJohnny Huang {
20792a856b9aSJohnny Huang 	int mode = 0;
20802a856b9aSJohnny Huang 	int nconfirm = 0;
20812a856b9aSJohnny Huang 	int otp_addr = 0;
20822a856b9aSJohnny Huang 	int bit_offset;
20832a856b9aSJohnny Huang 	int value;
20842a856b9aSJohnny Huang 	int ret;
20852a856b9aSJohnny Huang 
20862a856b9aSJohnny Huang 	if (argc != 4 && argc != 5 && argc != 6)
20872a856b9aSJohnny Huang 		return CMD_RET_USAGE;
20882a856b9aSJohnny Huang 
20892a856b9aSJohnny Huang 	/* Drop the pb cmd */
20902a856b9aSJohnny Huang 	argc--;
20912a856b9aSJohnny Huang 	argv++;
20922a856b9aSJohnny Huang 
20932a856b9aSJohnny Huang 	if (!strcmp(argv[0], "conf"))
2094a6d0d645SJohnny Huang 		mode = OTP_REGION_CONF;
20952a856b9aSJohnny Huang 	else if (!strcmp(argv[0], "strap"))
2096a6d0d645SJohnny Huang 		mode = OTP_REGION_STRAP;
20972a856b9aSJohnny Huang 	else if (!strcmp(argv[0], "data"))
2098a6d0d645SJohnny Huang 		mode = OTP_REGION_DATA;
2099cd1610b4SJohnny Huang 	else
21002a856b9aSJohnny Huang 		return CMD_RET_USAGE;
21012a856b9aSJohnny Huang 
21022a856b9aSJohnny Huang 	/* Drop the region cmd */
21032a856b9aSJohnny Huang 	argc--;
21042a856b9aSJohnny Huang 	argv++;
21052a856b9aSJohnny Huang 
2106ed071a2bSJohnny Huang 	if (!strcmp(argv[0], "o")) {
2107cd1610b4SJohnny Huang 		nconfirm = 1;
21082a856b9aSJohnny Huang 		/* Drop the force option */
21092a856b9aSJohnny Huang 		argc--;
21102a856b9aSJohnny Huang 		argv++;
21112a856b9aSJohnny Huang 	}
2112cd1610b4SJohnny Huang 
2113a6d0d645SJohnny Huang 	if (mode == OTP_REGION_STRAP) {
21142a856b9aSJohnny Huang 		bit_offset = simple_strtoul(argv[0], NULL, 16);
21152a856b9aSJohnny Huang 		value = simple_strtoul(argv[1], NULL, 16);
21160808cc55SJohnny Huang 		if (bit_offset >= 64 || (value != 0 && value != 1))
21172a856b9aSJohnny Huang 			return CMD_RET_USAGE;
2118cd1610b4SJohnny Huang 	} else {
21192a856b9aSJohnny Huang 		otp_addr = simple_strtoul(argv[0], NULL, 16);
21202a856b9aSJohnny Huang 		bit_offset = simple_strtoul(argv[1], NULL, 16);
21212a856b9aSJohnny Huang 		value = simple_strtoul(argv[2], NULL, 16);
21220808cc55SJohnny Huang 		if (bit_offset >= 32 || (value != 0 && value != 1))
21232a856b9aSJohnny Huang 			return CMD_RET_USAGE;
21240808cc55SJohnny Huang 		if (mode == OTP_REGION_DATA) {
212578855207SJohnny Huang 			if (otp_addr >= 0x800)
21260808cc55SJohnny Huang 				return CMD_RET_USAGE;
21270808cc55SJohnny Huang 		} else {
212878855207SJohnny Huang 			if (otp_addr >= 0x20)
21290808cc55SJohnny Huang 				return CMD_RET_USAGE;
21300808cc55SJohnny Huang 		}
2131cd1610b4SJohnny Huang 	}
2132cd1610b4SJohnny Huang 	if (value != 0 && value != 1)
21332a856b9aSJohnny Huang 		return CMD_RET_USAGE;
2134cd1610b4SJohnny Huang 
21353d3688adSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2136f347c284SJohnny Huang 	ret = otp_prog_bit(mode, otp_addr, bit_offset, value, nconfirm);
21372a856b9aSJohnny Huang 
21382a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
21392a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
21402a856b9aSJohnny Huang 	else if (ret == OTP_FAILURE)
21412a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
21422a856b9aSJohnny Huang 	else
21432a856b9aSJohnny Huang 		return CMD_RET_USAGE;
21442a856b9aSJohnny Huang }
21452a856b9aSJohnny Huang 
21462a856b9aSJohnny Huang static int do_otpcmp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
21472a856b9aSJohnny Huang {
21482a856b9aSJohnny Huang 	phys_addr_t addr;
21492a856b9aSJohnny Huang 	int otp_addr = 0;
21502a856b9aSJohnny Huang 
21512a856b9aSJohnny Huang 	if (argc != 3)
21522a856b9aSJohnny Huang 		return CMD_RET_USAGE;
21532a856b9aSJohnny Huang 
21543d3688adSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
21552a856b9aSJohnny Huang 	addr = simple_strtoul(argv[1], NULL, 16);
21562a856b9aSJohnny Huang 	otp_addr = simple_strtoul(argv[2], NULL, 16);
21572a856b9aSJohnny Huang 	if (otp_compare(otp_addr, addr) == 0) {
215869d5fd8fSJohnny Huang 		printf("Compare pass\n");
21592a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
2160a219f6deSJohnny Huang 	}
216169d5fd8fSJohnny Huang 	printf("Compare fail\n");
21622a856b9aSJohnny Huang 	return CMD_RET_FAILURE;
216369d5fd8fSJohnny Huang }
216469d5fd8fSJohnny Huang 
216566f2f8e5SJohnny Huang static int do_otpinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
216666f2f8e5SJohnny Huang {
2167a8bd6d8cSJohnny Huang 	int view = 0;
21682d4b0742SJohnny Huang 	int input;
2169a8bd6d8cSJohnny Huang 
2170a8bd6d8cSJohnny Huang 	if (argc != 2 && argc != 3)
217166f2f8e5SJohnny Huang 		return CMD_RET_USAGE;
217266f2f8e5SJohnny Huang 
21732d4b0742SJohnny Huang 	if (!strcmp(argv[1], "conf")) {
21743d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
21752d4b0742SJohnny Huang 		if (argc == 3) {
21762d4b0742SJohnny Huang 			input = simple_strtoul(argv[2], NULL, 16);
21772d4b0742SJohnny Huang 			otp_print_conf_info(input);
21782d4b0742SJohnny Huang 		} else {
21792d4b0742SJohnny Huang 			otp_print_conf_info(-1);
21802d4b0742SJohnny Huang 		}
21812d4b0742SJohnny Huang 	} else if (!strcmp(argv[1], "strap")) {
21822d4b0742SJohnny Huang 		if (!strcmp(argv[2], "v")) {
2183a8bd6d8cSJohnny Huang 			view = 1;
2184a8bd6d8cSJohnny Huang 			/* Drop the view option */
2185a8bd6d8cSJohnny Huang 			argc--;
2186a8bd6d8cSJohnny Huang 			argv++;
2187a8bd6d8cSJohnny Huang 		}
21883d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2189b458cd62SJohnny Huang 		otp_print_strap_info(view);
219066f2f8e5SJohnny Huang 	} else {
219166f2f8e5SJohnny Huang 		return CMD_RET_USAGE;
219266f2f8e5SJohnny Huang 	}
21932d4b0742SJohnny Huang 
219466f2f8e5SJohnny Huang 	return CMD_RET_SUCCESS;
219566f2f8e5SJohnny Huang }
219666f2f8e5SJohnny Huang 
2197e14b073cSJohnny Huang static int _do_otpprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[], int preg)
2198737ed20bSJohnny Huang {
2199737ed20bSJohnny Huang 	int input;
2200737ed20bSJohnny Huang 	int bit_offset;
2201e14b073cSJohnny Huang 	u32 prog_address;
220283655e91SJohnny Huang 	int ret;
2203e14b073cSJohnny Huang 	char info[10];
2204e14b073cSJohnny Huang 
2205e14b073cSJohnny Huang 	if (preg) {
2206e14b073cSJohnny Huang 		sprintf(info, "register ");
2207e14b073cSJohnny Huang 		prog_address = 0xe08;
2208e14b073cSJohnny Huang 	} else {
2209e14b073cSJohnny Huang 		info[0] = 0;
2210e14b073cSJohnny Huang 		prog_address = 0xe0c;
2211e14b073cSJohnny Huang 	}
2212a219f6deSJohnny Huang 
2213737ed20bSJohnny Huang 	if (argc != 3 && argc != 2)
2214737ed20bSJohnny Huang 		return CMD_RET_USAGE;
2215737ed20bSJohnny Huang 
2216e14b073cSJohnny Huang 	if (!strcmp(argv[1], "o")) {
2217737ed20bSJohnny Huang 		input = simple_strtoul(argv[2], NULL, 16);
2218737ed20bSJohnny Huang 	} else {
2219737ed20bSJohnny Huang 		input = simple_strtoul(argv[1], NULL, 16);
2220e14b073cSJohnny Huang 		printf("OTPSTRAP[%d] %swill be protected\n", input, info);
2221737ed20bSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
2222737ed20bSJohnny Huang 		if (!confirm_yesno()) {
2223737ed20bSJohnny Huang 			printf(" Aborting\n");
2224737ed20bSJohnny Huang 			return CMD_RET_FAILURE;
2225737ed20bSJohnny Huang 		}
2226737ed20bSJohnny Huang 	}
2227737ed20bSJohnny Huang 
2228737ed20bSJohnny Huang 	if (input < 32) {
2229737ed20bSJohnny Huang 		bit_offset = input;
2230737ed20bSJohnny Huang 	} else if (input < 64) {
2231737ed20bSJohnny Huang 		bit_offset = input - 32;
2232e14b073cSJohnny Huang 		prog_address += 2;
2233737ed20bSJohnny Huang 	} else {
2234737ed20bSJohnny Huang 		return CMD_RET_USAGE;
2235737ed20bSJohnny Huang 	}
2236737ed20bSJohnny Huang 
2237e14b073cSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2238e14b073cSJohnny Huang 	if (verify_bit(prog_address, bit_offset, 1) == 0) {
2239e14b073cSJohnny Huang 		printf("OTPSTRAP[%d] %salready protected\n", input, info);
2240e14b073cSJohnny Huang 		return CMD_RET_SUCCESS;
2241e14b073cSJohnny Huang 	}
2242de6fbf1cSJohnny Huang 
2243f347c284SJohnny Huang 	ret = otp_prog_dc_b(1, prog_address, bit_offset);
2244de6fbf1cSJohnny Huang 	otp_soak(0);
224583655e91SJohnny Huang 
224683655e91SJohnny Huang 	if (ret) {
2247e14b073cSJohnny Huang 		printf("Protect OTPSTRAP[%d] %sfail\n", input, info);
2248737ed20bSJohnny Huang 		return CMD_RET_FAILURE;
2249737ed20bSJohnny Huang 	}
22509a4fe690SJohnny Huang 
2251794e27ecSJohnny Huang 	printf("OTPSTRAP[%d] %sis protected\n", input, info);
2252794e27ecSJohnny Huang 	return CMD_RET_SUCCESS;
2253794e27ecSJohnny Huang }
2254794e27ecSJohnny Huang 
2255e14b073cSJohnny Huang static int do_otpprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2256e14b073cSJohnny Huang {
2257e14b073cSJohnny Huang 	return _do_otpprotect(cmdtp, flag, argc, argv, 0);
2258e14b073cSJohnny Huang }
2259e14b073cSJohnny Huang 
2260e14b073cSJohnny Huang static int do_otprprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2261e14b073cSJohnny Huang {
2262e14b073cSJohnny Huang 	return _do_otpprotect(cmdtp, flag, argc, argv, 1);
2263e14b073cSJohnny Huang }
2264e14b073cSJohnny Huang 
2265f67375f7SJohnny Huang static int do_otpver(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2266f67375f7SJohnny Huang {
2267e417205bSJohnny Huang 	printf("SOC OTP version: %s\n", info_cb.ver_name);
2268f67375f7SJohnny Huang 	printf("OTP tool version: %s\n", OTP_VER);
2269f67375f7SJohnny Huang 	printf("OTP info version: %s\n", OTP_INFO_VER);
2270f67375f7SJohnny Huang 
2271f67375f7SJohnny Huang 	return CMD_RET_SUCCESS;
2272f67375f7SJohnny Huang }
2273f67375f7SJohnny Huang 
2274794e27ecSJohnny Huang static int do_otpupdate(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2275794e27ecSJohnny Huang {
2276794e27ecSJohnny Huang 	u32 update_num;
2277794e27ecSJohnny Huang 	int force = 0;
2278794e27ecSJohnny Huang 	int ret;
2279794e27ecSJohnny Huang 
2280794e27ecSJohnny Huang 	if (argc == 3) {
2281794e27ecSJohnny Huang 		if (strcmp(argv[1], "o"))
2282794e27ecSJohnny Huang 			return CMD_RET_USAGE;
2283794e27ecSJohnny Huang 		force = 1;
2284794e27ecSJohnny Huang 		update_num = simple_strtoul(argv[2], NULL, 16);
2285794e27ecSJohnny Huang 	} else if (argc == 2) {
2286794e27ecSJohnny Huang 		update_num = simple_strtoul(argv[1], NULL, 16);
2287794e27ecSJohnny Huang 	} else {
2288794e27ecSJohnny Huang 		return CMD_RET_USAGE;
2289794e27ecSJohnny Huang 	}
2290794e27ecSJohnny Huang 
2291794e27ecSJohnny Huang 	if (update_num > 64)
2292794e27ecSJohnny Huang 		return CMD_RET_USAGE;
2293794e27ecSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2294794e27ecSJohnny Huang 	ret = otp_update_rid(update_num, force);
2295794e27ecSJohnny Huang 	if (ret)
2296794e27ecSJohnny Huang 		return CMD_RET_FAILURE;
2297794e27ecSJohnny Huang 	return CMD_RET_SUCCESS;
2298794e27ecSJohnny Huang }
2299794e27ecSJohnny Huang 
2300794e27ecSJohnny Huang static int do_otprid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2301794e27ecSJohnny Huang {
2302794e27ecSJohnny Huang 	u32 otp_rid[2];
2303a8789b47SJohnny Huang 	u32 sw_rid[2];
2304794e27ecSJohnny Huang 	int rid_num = 0;
2305a8789b47SJohnny Huang 	int sw_rid_num = 0;
2306794e27ecSJohnny Huang 	int ret;
2307794e27ecSJohnny Huang 
2308794e27ecSJohnny Huang 	if (argc != 1)
2309794e27ecSJohnny Huang 		return CMD_RET_USAGE;
2310794e27ecSJohnny Huang 
2311794e27ecSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2312f347c284SJohnny Huang 	otp_read_conf(10, &otp_rid[0]);
2313f347c284SJohnny Huang 	otp_read_conf(11, &otp_rid[1]);
2314794e27ecSJohnny Huang 
2315a8789b47SJohnny Huang 	sw_rid[0] = readl(SW_REV_ID0);
2316a8789b47SJohnny Huang 	sw_rid[1] = readl(SW_REV_ID1);
2317794e27ecSJohnny Huang 
2318a8789b47SJohnny Huang 	rid_num = get_rid_num(otp_rid);
2319a8789b47SJohnny Huang 	sw_rid_num = get_rid_num(sw_rid);
2320a8789b47SJohnny Huang 
2321a8789b47SJohnny Huang 	printf("current SW revision ID: 0x%x\n", sw_rid_num);
2322794e27ecSJohnny Huang 	if (rid_num >= 0) {
2323794e27ecSJohnny Huang 		printf("current OTP revision ID: 0x%x\n", rid_num);
2324794e27ecSJohnny Huang 		ret = CMD_RET_SUCCESS;
2325794e27ecSJohnny Huang 	} else {
2326794e27ecSJohnny Huang 		printf("Currennt OTP revision ID cannot handle by 'otp update',\n"
2327794e27ecSJohnny Huang 		       "plase use 'otp pb' command to update it manually\n"
2328794e27ecSJohnny Huang 		       "current OTP revision ID\n");
2329794e27ecSJohnny Huang 		ret = CMD_RET_FAILURE;
2330794e27ecSJohnny Huang 	}
2331794e27ecSJohnny Huang 	otp_print_revid(otp_rid);
2332794e27ecSJohnny Huang 
2333794e27ecSJohnny Huang 	return ret;
2334794e27ecSJohnny Huang }
2335794e27ecSJohnny Huang 
23362a856b9aSJohnny Huang static cmd_tbl_t cmd_otp[] = {
2337f67375f7SJohnny Huang 	U_BOOT_CMD_MKENT(version, 1, 0, do_otpver, "", ""),
23382a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(read, 4, 0, do_otpread, "", ""),
2339a8bd6d8cSJohnny Huang 	U_BOOT_CMD_MKENT(info, 3, 0, do_otpinfo, "", ""),
2340de6b0cc4SJohnny Huang 	U_BOOT_CMD_MKENT(prog, 3, 0, do_otpprog, "", ""),
23412a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(pb, 6, 0, do_otppb, "", ""),
2342737ed20bSJohnny Huang 	U_BOOT_CMD_MKENT(protect, 3, 0, do_otpprotect, "", ""),
2343e14b073cSJohnny Huang 	U_BOOT_CMD_MKENT(rprotect, 3, 0, do_otprprotect, "", ""),
23442a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(cmp, 3, 0, do_otpcmp, "", ""),
2345794e27ecSJohnny Huang 	U_BOOT_CMD_MKENT(update, 3, 0, do_otpupdate, "", ""),
2346794e27ecSJohnny Huang 	U_BOOT_CMD_MKENT(rid, 1, 0, do_otprid, "", ""),
23472a856b9aSJohnny Huang };
23482a856b9aSJohnny Huang 
23492a856b9aSJohnny Huang static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
23502a856b9aSJohnny Huang {
23512a856b9aSJohnny Huang 	cmd_tbl_t *cp;
2352a219f6deSJohnny Huang 	u32 ver;
2353e14b073cSJohnny Huang 	int ret;
23542a856b9aSJohnny Huang 
23552a856b9aSJohnny Huang 	cp = find_cmd_tbl(argv[1], cmd_otp, ARRAY_SIZE(cmd_otp));
23562a856b9aSJohnny Huang 
2357737ed20bSJohnny Huang 	/* Drop the otp command */
23582a856b9aSJohnny Huang 	argc--;
23592a856b9aSJohnny Huang 	argv++;
23602a856b9aSJohnny Huang 
2361a219f6deSJohnny Huang 	if (!cp || argc > cp->maxargs)
23622a856b9aSJohnny Huang 		return CMD_RET_USAGE;
23632a856b9aSJohnny Huang 	if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
23642a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
23652a856b9aSJohnny Huang 
23660dae9d52SJohnny Huang 	ver = chip_version();
23670dae9d52SJohnny Huang 	switch (ver) {
2368e417205bSJohnny Huang 	case OTP_A0:
2369e417205bSJohnny Huang 		info_cb.version = OTP_A0;
23709a4fe690SJohnny Huang 		info_cb.conf_info = a0_conf_info;
23719a4fe690SJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a0_conf_info);
23729a4fe690SJohnny Huang 		info_cb.strap_info = a0_strap_info;
23739a4fe690SJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a0_strap_info);
23749a4fe690SJohnny Huang 		info_cb.key_info = a0_key_type;
23759a4fe690SJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a0_key_type);
2376e417205bSJohnny Huang 		sprintf(info_cb.ver_name, "A0");
23770dae9d52SJohnny Huang 		break;
2378e417205bSJohnny Huang 	case OTP_A1:
2379e417205bSJohnny Huang 		info_cb.version = OTP_A1;
23803cb28812SJohnny Huang 		info_cb.conf_info = a1_conf_info;
23813cb28812SJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a1_conf_info);
23823cb28812SJohnny Huang 		info_cb.strap_info = a1_strap_info;
23833cb28812SJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a1_strap_info);
23849a4fe690SJohnny Huang 		info_cb.key_info = a1_key_type;
23859a4fe690SJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a1_key_type);
2386e417205bSJohnny Huang 		sprintf(info_cb.ver_name, "A1");
23870dae9d52SJohnny Huang 		break;
2388e417205bSJohnny Huang 	case OTP_A2:
2389e417205bSJohnny Huang 		info_cb.version = OTP_A2;
23905fdde29fSJohnny Huang 		info_cb.conf_info = a2_conf_info;
23915fdde29fSJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a2_conf_info);
23925fdde29fSJohnny Huang 		info_cb.strap_info = a2_strap_info;
23935fdde29fSJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a2_strap_info);
23945fdde29fSJohnny Huang 		info_cb.key_info = a2_key_type;
23955fdde29fSJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a2_key_type);
2396e417205bSJohnny Huang 		sprintf(info_cb.ver_name, "A2");
23970dae9d52SJohnny Huang 		break;
2398e417205bSJohnny Huang 	case OTP_A3:
2399e417205bSJohnny Huang 		info_cb.version = OTP_A3;
2400b63af886SJohnny Huang 		info_cb.conf_info = a3_conf_info;
2401b63af886SJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a3_conf_info);
2402b63af886SJohnny Huang 		info_cb.strap_info = a3_strap_info;
2403b63af886SJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a3_strap_info);
2404181f72d8SJohnny Huang 		info_cb.key_info = a3_key_type;
2405181f72d8SJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a3_key_type);
2406e417205bSJohnny Huang 		sprintf(info_cb.ver_name, "A3");
240764b66712SJohnny Huang 		break;
24080dae9d52SJohnny Huang 	default:
2409f1be5099SJohnny Huang 		printf("SOC is not supported\n");
24100dae9d52SJohnny Huang 		return CMD_RET_FAILURE;
24119a4fe690SJohnny Huang 	}
24129a4fe690SJohnny Huang 
2413e14b073cSJohnny Huang 	ret = cp->cmd(cmdtp, flag, argc, argv);
2414e14b073cSJohnny Huang 	writel(1, OTP_PROTECT_KEY); //password
2415e14b073cSJohnny Huang 
2416e14b073cSJohnny Huang 	return ret;
241769d5fd8fSJohnny Huang }
241869d5fd8fSJohnny Huang 
2419a219f6deSJohnny Huang U_BOOT_CMD(otp, 7, 0,  do_ast_otp,
242069d5fd8fSJohnny Huang 	   "ASPEED One-Time-Programmable sub-system",
2421f67375f7SJohnny Huang 	   "version\n"
2422f67375f7SJohnny Huang 	   "otp read conf|data <otp_dw_offset> <dw_count>\n"
24232a856b9aSJohnny Huang 	   "otp read strap <strap_bit_offset> <bit_count>\n"
24242d4b0742SJohnny Huang 	   "otp info strap [v]\n"
24252d4b0742SJohnny Huang 	   "otp info conf [otp_dw_offset]\n"
2426de6b0cc4SJohnny Huang 	   "otp prog [o] <addr>\n"
2427ed071a2bSJohnny Huang 	   "otp pb conf|data [o] <otp_dw_offset> <bit_offset> <value>\n"
2428ed071a2bSJohnny Huang 	   "otp pb strap [o] <bit_offset> <value>\n"
2429ed071a2bSJohnny Huang 	   "otp protect [o] <bit_offset>\n"
2430e14b073cSJohnny Huang 	   "otp rprotect [o] <bit_offset>\n"
2431794e27ecSJohnny Huang 	   "otp update [o] <revision_id>\n"
2432794e27ecSJohnny Huang 	   "otp rid\n"
243369d5fd8fSJohnny Huang 	  );
2434