xref: /openbmc/u-boot/cmd/otp.c (revision 61a6cda7)
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
7721a8cfceSJohnny 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 
97*61a6cda7SJohnny Huang #define SOC_AST2600A0	0
98*61a6cda7SJohnny Huang #define SOC_AST2600A1	1
99*61a6cda7SJohnny Huang #define SOC_AST2600A2	2
100*61a6cda7SJohnny Huang #define SOC_AST2600A3	3
101*61a6cda7SJohnny Huang 
102*61a6cda7SJohnny Huang #define OTPTOOL_VERSION(a, b, c) (((a) << 24) + ((b) << 12) + (c))
103*61a6cda7SJohnny Huang 
104696656c6SJohnny Huang struct otp_header {
105696656c6SJohnny Huang 	u8	otp_magic[8];
106*61a6cda7SJohnny Huang 	u32	soc_ver;
107*61a6cda7SJohnny Huang 	u32	otptool_ver;
108696656c6SJohnny Huang 	u32	image_info;
109696656c6SJohnny Huang 	u32	data_info;
110696656c6SJohnny Huang 	u32	config_info;
111696656c6SJohnny Huang 	u32	strap_info;
112696656c6SJohnny Huang 	u32	checksum_offset;
113a219f6deSJohnny Huang } __packed;
114696656c6SJohnny Huang 
11566f2f8e5SJohnny Huang struct otpstrap_status {
11669d5fd8fSJohnny Huang 	int value;
11769d5fd8fSJohnny Huang 	int option_array[7];
11869d5fd8fSJohnny Huang 	int remain_times;
11969d5fd8fSJohnny Huang 	int writeable_option;
1205010032bSJohnny Huang 	int reg_protected;
12169d5fd8fSJohnny Huang 	int protected;
12269d5fd8fSJohnny Huang };
12369d5fd8fSJohnny Huang 
1249a4fe690SJohnny Huang struct otpkey_type {
1259a4fe690SJohnny Huang 	int value;
1269a4fe690SJohnny Huang 	int key_type;
1279a4fe690SJohnny Huang 	int need_id;
1289a4fe690SJohnny Huang 	char information[110];
1299a4fe690SJohnny Huang };
1309a4fe690SJohnny Huang 
1319a4fe690SJohnny Huang struct otp_info_cb {
1329a4fe690SJohnny Huang 	int version;
133e417205bSJohnny Huang 	char ver_name[3];
13479e42a59SJoel Stanley 	const struct otpstrap_info *strap_info;
1359a4fe690SJohnny Huang 	int strap_info_len;
13679e42a59SJoel Stanley 	const struct otpconf_info *conf_info;
1379a4fe690SJohnny Huang 	int conf_info_len;
13879e42a59SJoel Stanley 	const struct otpkey_type *key_info;
1399a4fe690SJohnny Huang 	int key_info_len;
1409a4fe690SJohnny Huang };
1419a4fe690SJohnny Huang 
142696656c6SJohnny Huang struct otp_image_layout {
1435010032bSJohnny Huang 	int data_length;
1445010032bSJohnny Huang 	int conf_length;
1455010032bSJohnny Huang 	int strap_length;
146a219f6deSJohnny Huang 	u8 *data;
147a219f6deSJohnny Huang 	u8 *data_ignore;
148a219f6deSJohnny Huang 	u8 *conf;
149a219f6deSJohnny Huang 	u8 *conf_ignore;
150a219f6deSJohnny Huang 	u8 *strap;
151a219f6deSJohnny Huang 	u8 *strap_reg_pro;
152a219f6deSJohnny Huang 	u8 *strap_pro;
153a219f6deSJohnny Huang 	u8 *strap_ignore;
154696656c6SJohnny Huang };
155696656c6SJohnny Huang 
1569a4fe690SJohnny Huang static struct otp_info_cb info_cb;
1579a4fe690SJohnny Huang 
15879e42a59SJoel Stanley static const struct otpkey_type a0_key_type[] = {
1599a4fe690SJohnny Huang 	{0, OTP_KEY_TYPE_AES,   0, "AES-256 as OEM platform key for image encryption/decryption"},
1609a4fe690SJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
1619a4fe690SJohnny Huang 	{4, OTP_KEY_TYPE_HMAC,  1, "HMAC as encrypted OEM HMAC keys in Mode 1"},
162181f72d8SJohnny Huang 	{8, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2"},
163181f72d8SJohnny Huang 	{9, OTP_KEY_TYPE_RSA_PUB,   0, "RSA-public as SOC public key"},
164181f72d8SJohnny Huang 	{10, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key"},
165181f72d8SJohnny Huang 	{13, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as SOC private key"},
166181f72d8SJohnny Huang 	{14, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key"},
1679a4fe690SJohnny Huang };
1689a4fe690SJohnny Huang 
16979e42a59SJoel Stanley static const struct otpkey_type a1_key_type[] = {
1709a4fe690SJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
1719a4fe690SJohnny 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"},
172181f72d8SJohnny Huang 	{8, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2"},
173181f72d8SJohnny Huang 	{10, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key"},
174181f72d8SJohnny Huang 	{14, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key"},
1759a4fe690SJohnny Huang };
1769a4fe690SJohnny Huang 
1775fdde29fSJohnny Huang static const struct otpkey_type a2_key_type[] = {
1785fdde29fSJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
1795fdde29fSJohnny 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"},
180181f72d8SJohnny Huang 	{8, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2"},
181181f72d8SJohnny Huang 	{10, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key"},
182181f72d8SJohnny Huang 	{14, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key"},
183181f72d8SJohnny Huang };
184181f72d8SJohnny Huang 
185181f72d8SJohnny Huang static const struct otpkey_type a3_key_type[] = {
186181f72d8SJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
187181f72d8SJohnny 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"},
188181f72d8SJohnny Huang 	{8, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2"},
189181f72d8SJohnny Huang 	{9, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2(big endian)"},
190181f72d8SJohnny Huang 	{10, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key"},
191181f72d8SJohnny Huang 	{11, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key(big endian)"},
192181f72d8SJohnny Huang 	{12, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key"},
193181f72d8SJohnny Huang 	{13, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key(big endian)"},
1945fdde29fSJohnny Huang };
1955fdde29fSJohnny Huang 
196f347c284SJohnny Huang static void buf_print(u8 *buf, int len)
197f347c284SJohnny Huang {
198f347c284SJohnny Huang 	int i;
199f347c284SJohnny Huang 
200f347c284SJohnny Huang 	printf("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
201f347c284SJohnny Huang 	for (i = 0; i < len; i++) {
202f347c284SJohnny Huang 		if (i % 16 == 0)
203f347c284SJohnny Huang 			printf("%04X: ", i);
204f347c284SJohnny Huang 		printf("%02X ", buf[i]);
205f347c284SJohnny Huang 		if ((i + 1) % 16 == 0)
206f347c284SJohnny Huang 			printf("\n");
207f347c284SJohnny Huang 	}
208f347c284SJohnny Huang }
209f347c284SJohnny Huang 
210794e27ecSJohnny Huang static int get_dw_bit(u32 *rid, int offset)
211794e27ecSJohnny Huang {
212794e27ecSJohnny Huang 	int bit_offset;
213794e27ecSJohnny Huang 	int i;
214794e27ecSJohnny Huang 
215794e27ecSJohnny Huang 	if (offset < 32) {
216794e27ecSJohnny Huang 		i = 0;
217794e27ecSJohnny Huang 		bit_offset = offset;
218794e27ecSJohnny Huang 	} else {
219794e27ecSJohnny Huang 		i = 1;
220794e27ecSJohnny Huang 		bit_offset = offset - 32;
221794e27ecSJohnny Huang 	}
222794e27ecSJohnny Huang 	if ((rid[i] >> bit_offset) & 0x1)
223794e27ecSJohnny Huang 		return 1;
224794e27ecSJohnny Huang 	else
225794e27ecSJohnny Huang 		return 0;
226794e27ecSJohnny Huang }
227794e27ecSJohnny Huang 
228794e27ecSJohnny Huang static int get_rid_num(u32 *rid)
229794e27ecSJohnny Huang {
230794e27ecSJohnny Huang 	int i;
231794e27ecSJohnny Huang 	int fz = 0;
232794e27ecSJohnny Huang 	int rid_num = 0;
233794e27ecSJohnny Huang 	int ret = 0;
234794e27ecSJohnny Huang 
235794e27ecSJohnny Huang 	for (i = 0; i < 64; i++) {
236794e27ecSJohnny Huang 		if (get_dw_bit(rid, i) == 0) {
237794e27ecSJohnny Huang 			if (!fz)
238794e27ecSJohnny Huang 				fz = 1;
239794e27ecSJohnny Huang 
240794e27ecSJohnny Huang 		} else {
241794e27ecSJohnny Huang 			rid_num++;
242794e27ecSJohnny Huang 			if (fz)
243794e27ecSJohnny Huang 				ret = OTP_FAILURE;
244794e27ecSJohnny Huang 		}
245794e27ecSJohnny Huang 	}
246794e27ecSJohnny Huang 	if (ret)
247794e27ecSJohnny Huang 		return ret;
248794e27ecSJohnny Huang 
249794e27ecSJohnny Huang 	return rid_num;
250794e27ecSJohnny Huang }
251794e27ecSJohnny Huang 
252a219f6deSJohnny Huang static u32 chip_version(void)
2539a4fe690SJohnny Huang {
254e417205bSJohnny Huang 	u32 revid0, revid1;
2559a4fe690SJohnny Huang 
256e417205bSJohnny Huang 	revid0 = readl(ASPEED_REVISION_ID0);
257e417205bSJohnny Huang 	revid1 = readl(ASPEED_REVISION_ID1);
2589a4fe690SJohnny Huang 
259e417205bSJohnny Huang 	if (revid0 == ID0_AST2600A0 && revid1 == ID1_AST2600A0) {
260badd21c2SJohnny Huang 		/* AST2600-A0 */
261e417205bSJohnny Huang 		return OTP_A0;
262e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2600A1 && revid1 == ID1_AST2600A1) {
263badd21c2SJohnny Huang 		/* AST2600-A1 */
264e417205bSJohnny Huang 		return OTP_A1;
265e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2600A2 && revid1 == ID1_AST2600A2) {
266badd21c2SJohnny Huang 		/* AST2600-A2 */
267e417205bSJohnny Huang 		return OTP_A2;
268e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2600A3 && revid1 == ID1_AST2600A3) {
26964b66712SJohnny Huang 		/* AST2600-A3 */
270e417205bSJohnny Huang 		return OTP_A3;
271e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2620A1 && revid1 == ID1_AST2620A1) {
272e417205bSJohnny Huang 		/* AST2620-A1 */
273e417205bSJohnny Huang 		return OTP_A1;
274e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2620A2 && revid1 == ID1_AST2620A2) {
275e417205bSJohnny Huang 		/* AST2620-A2 */
276e417205bSJohnny Huang 		return OTP_A2;
277e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2620A3 && revid1 == ID1_AST2620A3) {
27864b66712SJohnny Huang 		/* AST2620-A3 */
279e417205bSJohnny Huang 		return OTP_A3;
280e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2605A2 && revid1 == ID1_AST2605A2) {
281e417205bSJohnny Huang 		/* AST2605-A2 */
282e417205bSJohnny Huang 		return OTP_A2;
283e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2605A3 && revid1 == ID1_AST2605A3) {
284e417205bSJohnny Huang 		/* AST2605-A3 */
285e417205bSJohnny Huang 		return OTP_A3;
286e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2625A3 && revid1 == ID1_AST2625A3) {
287e417205bSJohnny Huang 		/* AST2605-A3 */
288e417205bSJohnny Huang 		return OTP_A3;
2890dae9d52SJohnny Huang 	}
290f347c284SJohnny Huang 	return OTP_FAILURE;
2919a4fe690SJohnny Huang }
2929a4fe690SJohnny Huang 
2933d3688adSJohnny Huang static void wait_complete(void)
2943d3688adSJohnny Huang {
2953d3688adSJohnny Huang 	int reg;
2963d3688adSJohnny Huang 
2973d3688adSJohnny Huang 	do {
2983d3688adSJohnny Huang 		reg = readl(OTP_STATUS);
2993d3688adSJohnny Huang 	} while ((reg & 0x6) != 0x6);
3003d3688adSJohnny Huang }
3013d3688adSJohnny Huang 
302a219f6deSJohnny Huang static void otp_write(u32 otp_addr, u32 data)
303dacbba92SJohnny Huang {
304dacbba92SJohnny Huang 	writel(otp_addr, OTP_ADDR); //write address
305dacbba92SJohnny Huang 	writel(data, OTP_COMPARE_1); //write data
306dacbba92SJohnny Huang 	writel(0x23b1e362, OTP_COMMAND); //write command
307dacbba92SJohnny Huang 	wait_complete();
308dacbba92SJohnny Huang }
309dacbba92SJohnny Huang 
310dacbba92SJohnny Huang static void otp_soak(int soak)
311dacbba92SJohnny Huang {
312e417205bSJohnny Huang 	if (info_cb.version == OTP_A2 || info_cb.version == OTP_A3) {
313dacbba92SJohnny Huang 		switch (soak) {
314dacbba92SJohnny Huang 		case 0: //default
315377f8cd7SJohnny Huang 			otp_write(0x3000, 0x0); // Write MRA
316377f8cd7SJohnny Huang 			otp_write(0x5000, 0x0); // Write MRB
317dacbba92SJohnny Huang 			otp_write(0x1000, 0x0); // Write MR
318dacbba92SJohnny Huang 			break;
319dacbba92SJohnny Huang 		case 1: //normal program
320377f8cd7SJohnny Huang 			otp_write(0x3000, 0x1320); // Write MRA
321377f8cd7SJohnny Huang 			otp_write(0x5000, 0x1008); // Write MRB
322377f8cd7SJohnny Huang 			otp_write(0x1000, 0x0024); // Write MR
323feea3fdfSJohnny Huang 			writel(0x04191388, OTP_TIMING); // 200us
324dacbba92SJohnny Huang 			break;
325dacbba92SJohnny Huang 		case 2: //soak program
326377f8cd7SJohnny Huang 			otp_write(0x3000, 0x1320); // Write MRA
327377f8cd7SJohnny Huang 			otp_write(0x5000, 0x0007); // Write MRB
328377f8cd7SJohnny Huang 			otp_write(0x1000, 0x0100); // Write MR
329feea3fdfSJohnny Huang 			writel(0x04193a98, OTP_TIMING); // 600us
330dacbba92SJohnny Huang 			break;
331dacbba92SJohnny Huang 		}
332dacbba92SJohnny Huang 	} else {
333dacbba92SJohnny Huang 		switch (soak) {
334dacbba92SJohnny Huang 		case 0: //default
335dacbba92SJohnny Huang 			otp_write(0x3000, 0x0); // Write MRA
336dacbba92SJohnny Huang 			otp_write(0x5000, 0x0); // Write MRB
337dacbba92SJohnny Huang 			otp_write(0x1000, 0x0); // Write MR
338dacbba92SJohnny Huang 			break;
339dacbba92SJohnny Huang 		case 1: //normal program
340dacbba92SJohnny Huang 			otp_write(0x3000, 0x4021); // Write MRA
341dacbba92SJohnny Huang 			otp_write(0x5000, 0x302f); // Write MRB
342dacbba92SJohnny Huang 			otp_write(0x1000, 0x4020); // Write MR
343feea3fdfSJohnny Huang 			writel(0x04190760, OTP_TIMING); // 75us
344dacbba92SJohnny Huang 			break;
345dacbba92SJohnny Huang 		case 2: //soak program
346dacbba92SJohnny Huang 			otp_write(0x3000, 0x4021); // Write MRA
347dacbba92SJohnny Huang 			otp_write(0x5000, 0x1027); // Write MRB
348dacbba92SJohnny Huang 			otp_write(0x1000, 0x4820); // Write MR
349feea3fdfSJohnny Huang 			writel(0x041930d4, OTP_TIMING); // 500us
350dacbba92SJohnny Huang 			break;
351dacbba92SJohnny Huang 		}
352dacbba92SJohnny Huang 	}
353dacbba92SJohnny Huang 
354dacbba92SJohnny Huang 	wait_complete();
355dacbba92SJohnny Huang }
356dacbba92SJohnny Huang 
357a219f6deSJohnny Huang static void otp_read_data(u32 offset, u32 *data)
35869d5fd8fSJohnny Huang {
3593d3688adSJohnny Huang 	writel(offset, OTP_ADDR); //Read address
3603d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
3613d3688adSJohnny Huang 	wait_complete();
3623d3688adSJohnny Huang 	data[0] = readl(OTP_COMPARE_1);
3633d3688adSJohnny Huang 	data[1] = readl(OTP_COMPARE_2);
36469d5fd8fSJohnny Huang }
36569d5fd8fSJohnny Huang 
366f347c284SJohnny Huang static void otp_read_conf(u32 offset, u32 *data)
36769d5fd8fSJohnny Huang {
36869d5fd8fSJohnny Huang 	int config_offset;
36969d5fd8fSJohnny Huang 
37069d5fd8fSJohnny Huang 	config_offset = 0x800;
37169d5fd8fSJohnny Huang 	config_offset |= (offset / 8) * 0x200;
37269d5fd8fSJohnny Huang 	config_offset |= (offset % 8) * 0x2;
37369d5fd8fSJohnny Huang 
3743d3688adSJohnny Huang 	writel(config_offset, OTP_ADDR);  //Read address
3753d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
3763d3688adSJohnny Huang 	wait_complete();
3773d3688adSJohnny Huang 	data[0] = readl(OTP_COMPARE_1);
37869d5fd8fSJohnny Huang }
37969d5fd8fSJohnny Huang 
380a219f6deSJohnny Huang static int otp_compare(u32 otp_addr, u32 addr)
38169d5fd8fSJohnny Huang {
382a219f6deSJohnny Huang 	u32 ret;
383a219f6deSJohnny Huang 	u32 *buf;
38469d5fd8fSJohnny Huang 
38569d5fd8fSJohnny Huang 	buf = map_physmem(addr, 16, MAP_WRBACK);
38669d5fd8fSJohnny Huang 	printf("%08X\n", buf[0]);
38769d5fd8fSJohnny Huang 	printf("%08X\n", buf[1]);
38869d5fd8fSJohnny Huang 	printf("%08X\n", buf[2]);
38969d5fd8fSJohnny Huang 	printf("%08X\n", buf[3]);
3903d3688adSJohnny Huang 	writel(otp_addr, OTP_ADDR); //Compare address
3913d3688adSJohnny Huang 	writel(buf[0], OTP_COMPARE_1); //Compare data 1
3923d3688adSJohnny Huang 	writel(buf[1], OTP_COMPARE_2); //Compare data 2
3933d3688adSJohnny Huang 	writel(buf[2], OTP_COMPARE_3); //Compare data 3
3943d3688adSJohnny Huang 	writel(buf[3], OTP_COMPARE_4); //Compare data 4
3953d3688adSJohnny Huang 	writel(0x23b1e363, OTP_COMMAND); //Compare command
3963d3688adSJohnny Huang 	wait_complete();
3973d3688adSJohnny Huang 	ret = readl(OTP_STATUS); //Compare command
39869d5fd8fSJohnny Huang 	if (ret & 0x1)
399f347c284SJohnny Huang 		return OTP_SUCCESS;
40069d5fd8fSJohnny Huang 	else
401f347c284SJohnny Huang 		return OTP_FAILURE;
40269d5fd8fSJohnny Huang }
40369d5fd8fSJohnny Huang 
404a219f6deSJohnny Huang static int verify_bit(u32 otp_addr, int bit_offset, int value)
40569d5fd8fSJohnny Huang {
406a219f6deSJohnny Huang 	u32 ret[2];
40769d5fd8fSJohnny Huang 
40830a8c590SJohnny Huang 	if (otp_addr % 2 == 0)
4093d3688adSJohnny Huang 		writel(otp_addr, OTP_ADDR); //Read address
41030a8c590SJohnny Huang 	else
4113d3688adSJohnny Huang 		writel(otp_addr - 1, OTP_ADDR); //Read address
41230a8c590SJohnny Huang 
4133d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
4143d3688adSJohnny Huang 	wait_complete();
4153d3688adSJohnny Huang 	ret[0] = readl(OTP_COMPARE_1);
4163d3688adSJohnny Huang 	ret[1] = readl(OTP_COMPARE_2);
41783655e91SJohnny Huang 
41830a8c590SJohnny Huang 	if (otp_addr % 2 == 0) {
41930a8c590SJohnny Huang 		if (((ret[0] >> bit_offset) & 1) == value)
420f347c284SJohnny Huang 			return OTP_SUCCESS;
42169d5fd8fSJohnny Huang 		else
422f347c284SJohnny Huang 			return OTP_FAILURE;
42330a8c590SJohnny Huang 	} else {
42430a8c590SJohnny Huang 		if (((ret[1] >> bit_offset) & 1) == value)
425f347c284SJohnny Huang 			return OTP_SUCCESS;
42630a8c590SJohnny Huang 		else
427f347c284SJohnny Huang 			return OTP_FAILURE;
42830a8c590SJohnny Huang 	}
42969d5fd8fSJohnny Huang }
43069d5fd8fSJohnny Huang 
431a219f6deSJohnny Huang static u32 verify_dw(u32 otp_addr, u32 *value, u32 *ignore, u32 *compare, int size)
4324c1c9b35SJohnny Huang {
433a219f6deSJohnny Huang 	u32 ret[2];
4344c1c9b35SJohnny Huang 
4354c1c9b35SJohnny Huang 	otp_addr &= ~(1 << 15);
4364c1c9b35SJohnny Huang 
4374c1c9b35SJohnny Huang 	if (otp_addr % 2 == 0)
4383d3688adSJohnny Huang 		writel(otp_addr, OTP_ADDR); //Read address
4394c1c9b35SJohnny Huang 	else
4403d3688adSJohnny Huang 		writel(otp_addr - 1, OTP_ADDR); //Read address
4413d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
4423d3688adSJohnny Huang 	wait_complete();
4433d3688adSJohnny Huang 	ret[0] = readl(OTP_COMPARE_1);
4443d3688adSJohnny Huang 	ret[1] = readl(OTP_COMPARE_2);
4454c1c9b35SJohnny Huang 	if (size == 1) {
4464c1c9b35SJohnny Huang 		if (otp_addr % 2 == 0) {
4474c1c9b35SJohnny Huang 			// printf("check %x : %x = %x\n", otp_addr, ret[0], value[0]);
448696656c6SJohnny Huang 			if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0])) {
4494c1c9b35SJohnny Huang 				compare[0] = 0;
450f347c284SJohnny Huang 				return OTP_SUCCESS;
451a219f6deSJohnny Huang 			}
4524c1c9b35SJohnny Huang 			compare[0] = value[0] ^ ret[0];
453f347c284SJohnny Huang 			return OTP_FAILURE;
4544c1c9b35SJohnny Huang 
4554c1c9b35SJohnny Huang 		} else {
4564c1c9b35SJohnny Huang 			// printf("check %x : %x = %x\n", otp_addr, ret[1], value[0]);
457696656c6SJohnny Huang 			if ((value[0] & ~ignore[0]) == (ret[1] & ~ignore[0])) {
4584c1c9b35SJohnny Huang 				compare[0] = ~0;
459f347c284SJohnny Huang 				return OTP_SUCCESS;
460a219f6deSJohnny Huang 			}
461d90825e2SJohnny Huang 			compare[0] = ~(value[0] ^ ret[1]);
462f347c284SJohnny Huang 			return OTP_FAILURE;
4634c1c9b35SJohnny Huang 		}
4644c1c9b35SJohnny Huang 	} else if (size == 2) {
4654c1c9b35SJohnny Huang 		// otp_addr should be even
466696656c6SJohnny Huang 		if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0]) && (value[1] & ~ignore[1]) == (ret[1] & ~ignore[1])) {
4674c1c9b35SJohnny Huang 			// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
4684c1c9b35SJohnny Huang 			// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
4694c1c9b35SJohnny Huang 			compare[0] = 0;
4704c1c9b35SJohnny Huang 			compare[1] = ~0;
471f347c284SJohnny Huang 			return OTP_SUCCESS;
472a219f6deSJohnny Huang 		}
4734c1c9b35SJohnny Huang 		// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
4744c1c9b35SJohnny Huang 		// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
4754c1c9b35SJohnny Huang 		compare[0] = value[0] ^ ret[0];
4764c1c9b35SJohnny Huang 		compare[1] = ~(value[1] ^ ret[1]);
477f347c284SJohnny Huang 		return OTP_FAILURE;
4784c1c9b35SJohnny Huang 	} else {
479f347c284SJohnny Huang 		return OTP_FAILURE;
4804c1c9b35SJohnny Huang 	}
4814c1c9b35SJohnny Huang }
4824c1c9b35SJohnny Huang 
483a219f6deSJohnny Huang static void otp_prog(u32 otp_addr, u32 prog_bit)
48483655e91SJohnny Huang {
48590965bb3SJohnny Huang 	otp_write(0x0, prog_bit);
48683655e91SJohnny Huang 	writel(otp_addr, OTP_ADDR); //write address
48783655e91SJohnny Huang 	writel(prog_bit, OTP_COMPARE_1); //write data
48883655e91SJohnny Huang 	writel(0x23b1e364, OTP_COMMAND); //write command
48983655e91SJohnny Huang 	wait_complete();
49083655e91SJohnny Huang }
49183655e91SJohnny Huang 
492a219f6deSJohnny Huang static void _otp_prog_bit(u32 value, u32 prog_address, u32 bit_offset)
49383655e91SJohnny Huang {
49483655e91SJohnny Huang 	int prog_bit;
49583655e91SJohnny Huang 
49683655e91SJohnny Huang 	if (prog_address % 2 == 0) {
49783655e91SJohnny Huang 		if (value)
49883655e91SJohnny Huang 			prog_bit = ~(0x1 << bit_offset);
49983655e91SJohnny Huang 		else
50083655e91SJohnny Huang 			return;
50183655e91SJohnny Huang 	} else {
502e417205bSJohnny Huang 		if (info_cb.version != OTP_A3)
50383655e91SJohnny Huang 			prog_address |= 1 << 15;
50483655e91SJohnny Huang 		if (!value)
50583655e91SJohnny Huang 			prog_bit = 0x1 << bit_offset;
50683655e91SJohnny Huang 		else
50783655e91SJohnny Huang 			return;
50883655e91SJohnny Huang 	}
50983655e91SJohnny Huang 	otp_prog(prog_address, prog_bit);
51083655e91SJohnny Huang }
51183655e91SJohnny Huang 
512f347c284SJohnny Huang static int otp_prog_dc_b(u32 value, u32 prog_address, u32 bit_offset)
51383655e91SJohnny Huang {
51483655e91SJohnny Huang 	int pass;
51583655e91SJohnny Huang 	int i;
51683655e91SJohnny Huang 
51783655e91SJohnny Huang 	otp_soak(1);
51883655e91SJohnny Huang 	_otp_prog_bit(value, prog_address, bit_offset);
51983655e91SJohnny Huang 	pass = 0;
52083655e91SJohnny Huang 
52183655e91SJohnny Huang 	for (i = 0; i < RETRY; i++) {
52283655e91SJohnny Huang 		if (verify_bit(prog_address, bit_offset, value) != 0) {
52383655e91SJohnny Huang 			otp_soak(2);
52483655e91SJohnny Huang 			_otp_prog_bit(value, prog_address, bit_offset);
52583655e91SJohnny Huang 			if (verify_bit(prog_address, bit_offset, value) != 0) {
52683655e91SJohnny Huang 				otp_soak(1);
52783655e91SJohnny Huang 			} else {
52883655e91SJohnny Huang 				pass = 1;
52983655e91SJohnny Huang 				break;
53083655e91SJohnny Huang 			}
53183655e91SJohnny Huang 		} else {
53283655e91SJohnny Huang 			pass = 1;
53383655e91SJohnny Huang 			break;
53483655e91SJohnny Huang 		}
53583655e91SJohnny Huang 	}
536794e27ecSJohnny Huang 	if (pass)
537794e27ecSJohnny Huang 		return OTP_SUCCESS;
53883655e91SJohnny Huang 
539794e27ecSJohnny Huang 	return OTP_FAILURE;
54083655e91SJohnny Huang }
54183655e91SJohnny Huang 
542a219f6deSJohnny Huang static void otp_prog_dw(u32 value, u32 ignore, u32 prog_address)
543d90825e2SJohnny Huang {
544d90825e2SJohnny Huang 	int j, bit_value, prog_bit;
545d90825e2SJohnny Huang 
546d90825e2SJohnny Huang 	for (j = 0; j < 32; j++) {
547696656c6SJohnny Huang 		if ((ignore >> j) & 0x1)
548d90825e2SJohnny Huang 			continue;
549d90825e2SJohnny Huang 		bit_value = (value >> j) & 0x1;
550d90825e2SJohnny Huang 		if (prog_address % 2 == 0) {
551d90825e2SJohnny Huang 			if (bit_value)
552d90825e2SJohnny Huang 				prog_bit = ~(0x1 << j);
553d90825e2SJohnny Huang 			else
554d90825e2SJohnny Huang 				continue;
555d90825e2SJohnny Huang 		} else {
556e417205bSJohnny Huang 			if (info_cb.version != OTP_A3)
557d90825e2SJohnny Huang 				prog_address |= 1 << 15;
558d90825e2SJohnny Huang 			if (bit_value)
559d90825e2SJohnny Huang 				continue;
560d90825e2SJohnny Huang 			else
561d90825e2SJohnny Huang 				prog_bit = 0x1 << j;
562d90825e2SJohnny Huang 		}
563d90825e2SJohnny Huang 		otp_prog(prog_address, prog_bit);
564d90825e2SJohnny Huang 	}
565d90825e2SJohnny Huang }
566d90825e2SJohnny Huang 
567a219f6deSJohnny Huang static int otp_prog_verify_2dw(u32 *data, u32 *buf, u32 *ignore_mask, u32 prog_address)
56854552c69SJohnny Huang {
56954552c69SJohnny Huang 	int pass;
57054552c69SJohnny Huang 	int i;
571a219f6deSJohnny Huang 	u32 data0_masked;
572a219f6deSJohnny Huang 	u32 data1_masked;
573a219f6deSJohnny Huang 	u32 buf0_masked;
574a219f6deSJohnny Huang 	u32 buf1_masked;
575a219f6deSJohnny Huang 	u32 compare[2];
57654552c69SJohnny Huang 
57754552c69SJohnny Huang 	data0_masked = data[0]  & ~ignore_mask[0];
57854552c69SJohnny Huang 	buf0_masked  = buf[0] & ~ignore_mask[0];
57954552c69SJohnny Huang 	data1_masked = data[1]  & ~ignore_mask[1];
58054552c69SJohnny Huang 	buf1_masked  = buf[1] & ~ignore_mask[1];
581a219f6deSJohnny Huang 	if (data0_masked == buf0_masked && data1_masked == buf1_masked)
582f347c284SJohnny Huang 		return OTP_SUCCESS;
58354552c69SJohnny Huang 
58454552c69SJohnny Huang 	otp_soak(1);
58554552c69SJohnny Huang 	if (data0_masked != buf0_masked)
58654552c69SJohnny Huang 		otp_prog_dw(buf[0], ignore_mask[0], prog_address);
58754552c69SJohnny Huang 	if (data1_masked != buf1_masked)
58854552c69SJohnny Huang 		otp_prog_dw(buf[1], ignore_mask[1], prog_address + 1);
58954552c69SJohnny Huang 
59054552c69SJohnny Huang 	pass = 0;
59154552c69SJohnny Huang 	for (i = 0; i < RETRY; i++) {
59254552c69SJohnny Huang 		if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) {
59354552c69SJohnny Huang 			otp_soak(2);
594a219f6deSJohnny Huang 			if (compare[0] != 0)
59554552c69SJohnny Huang 				otp_prog_dw(compare[0], ignore_mask[0], prog_address);
596a219f6deSJohnny Huang 			if (compare[1] != ~0)
5975537bc72SJohnny Huang 				otp_prog_dw(compare[1], ignore_mask[1], prog_address + 1);
59854552c69SJohnny Huang 			if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) {
59954552c69SJohnny Huang 				otp_soak(1);
60054552c69SJohnny Huang 			} else {
60154552c69SJohnny Huang 				pass = 1;
60254552c69SJohnny Huang 				break;
60354552c69SJohnny Huang 			}
60454552c69SJohnny Huang 		} else {
60554552c69SJohnny Huang 			pass = 1;
60654552c69SJohnny Huang 			break;
60754552c69SJohnny Huang 		}
60854552c69SJohnny Huang 	}
60954552c69SJohnny Huang 
61054552c69SJohnny Huang 	if (!pass) {
61154552c69SJohnny Huang 		otp_soak(0);
61254552c69SJohnny Huang 		return OTP_FAILURE;
61354552c69SJohnny Huang 	}
61454552c69SJohnny Huang 	return OTP_SUCCESS;
61554552c69SJohnny Huang }
61654552c69SJohnny Huang 
617541eb887SJohnny Huang static void otp_strap_status(struct otpstrap_status *otpstrap)
61876d13988SJohnny Huang {
619a219f6deSJohnny Huang 	u32 OTPSTRAP_RAW[2];
6205010032bSJohnny Huang 	int strap_end;
62176d13988SJohnny Huang 	int i, j;
62276d13988SJohnny Huang 
623e417205bSJohnny Huang 	if (info_cb.version == OTP_A0) {
62476d13988SJohnny Huang 		for (j = 0; j < 64; j++) {
62576d13988SJohnny Huang 			otpstrap[j].value = 0;
62676d13988SJohnny Huang 			otpstrap[j].remain_times = 7;
62776d13988SJohnny Huang 			otpstrap[j].writeable_option = -1;
62876d13988SJohnny Huang 			otpstrap[j].protected = 0;
62976d13988SJohnny Huang 		}
6305010032bSJohnny Huang 		strap_end = 30;
6315010032bSJohnny Huang 	} else {
6325010032bSJohnny Huang 		for (j = 0; j < 64; j++) {
6335010032bSJohnny Huang 			otpstrap[j].value = 0;
6345010032bSJohnny Huang 			otpstrap[j].remain_times = 6;
6355010032bSJohnny Huang 			otpstrap[j].writeable_option = -1;
6365010032bSJohnny Huang 			otpstrap[j].reg_protected = 0;
6375010032bSJohnny Huang 			otpstrap[j].protected = 0;
6385010032bSJohnny Huang 		}
6395010032bSJohnny Huang 		strap_end = 28;
6405010032bSJohnny Huang 	}
64176d13988SJohnny Huang 
642dacbba92SJohnny Huang 	otp_soak(0);
6435010032bSJohnny Huang 	for (i = 16; i < strap_end; i += 2) {
64476d13988SJohnny Huang 		int option = (i - 16) / 2;
645a219f6deSJohnny Huang 
646f347c284SJohnny Huang 		otp_read_conf(i, &OTPSTRAP_RAW[0]);
647f347c284SJohnny Huang 		otp_read_conf(i + 1, &OTPSTRAP_RAW[1]);
64876d13988SJohnny Huang 		for (j = 0; j < 32; j++) {
64976d13988SJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[0] >> j) & 0x1);
650a219f6deSJohnny Huang 
651a219f6deSJohnny Huang 			if (bit_value == 0 && otpstrap[j].writeable_option == -1)
65276d13988SJohnny Huang 				otpstrap[j].writeable_option = option;
65376d13988SJohnny Huang 			if (bit_value == 1)
65476d13988SJohnny Huang 				otpstrap[j].remain_times--;
65576d13988SJohnny Huang 			otpstrap[j].value ^= bit_value;
65676d13988SJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
65776d13988SJohnny Huang 		}
65876d13988SJohnny Huang 		for (j = 32; j < 64; j++) {
65976d13988SJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1);
660a219f6deSJohnny Huang 
661a219f6deSJohnny Huang 			if (bit_value == 0 && otpstrap[j].writeable_option == -1)
66276d13988SJohnny Huang 				otpstrap[j].writeable_option = option;
66376d13988SJohnny Huang 			if (bit_value == 1)
66476d13988SJohnny Huang 				otpstrap[j].remain_times--;
66576d13988SJohnny Huang 			otpstrap[j].value ^= bit_value;
66676d13988SJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
66776d13988SJohnny Huang 		}
66876d13988SJohnny Huang 	}
6695010032bSJohnny Huang 
670e417205bSJohnny Huang 	if (info_cb.version != OTP_A0) {
671f347c284SJohnny Huang 		otp_read_conf(28, &OTPSTRAP_RAW[0]);
672f347c284SJohnny Huang 		otp_read_conf(29, &OTPSTRAP_RAW[1]);
6735010032bSJohnny Huang 		for (j = 0; j < 32; j++) {
6745010032bSJohnny Huang 			if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
6755010032bSJohnny Huang 				otpstrap[j].reg_protected = 1;
6765010032bSJohnny Huang 		}
6775010032bSJohnny Huang 		for (j = 32; j < 64; j++) {
6785010032bSJohnny Huang 			if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
6795010032bSJohnny Huang 				otpstrap[j].reg_protected = 1;
6805010032bSJohnny Huang 		}
6815010032bSJohnny Huang 	}
6825010032bSJohnny Huang 
683f347c284SJohnny Huang 	otp_read_conf(30, &OTPSTRAP_RAW[0]);
684f347c284SJohnny Huang 	otp_read_conf(31, &OTPSTRAP_RAW[1]);
68576d13988SJohnny Huang 	for (j = 0; j < 32; j++) {
68676d13988SJohnny Huang 		if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
68776d13988SJohnny Huang 			otpstrap[j].protected = 1;
68876d13988SJohnny Huang 	}
68976d13988SJohnny Huang 	for (j = 32; j < 64; j++) {
69076d13988SJohnny Huang 		if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
69176d13988SJohnny Huang 			otpstrap[j].protected = 1;
69276d13988SJohnny Huang 	}
69376d13988SJohnny Huang }
69476d13988SJohnny Huang 
695f347c284SJohnny Huang static int otp_strap_bit_confirm(struct otpstrap_status *otpstrap, int offset, int ibit, int bit, int pbit, int rpbit)
696f347c284SJohnny Huang {
697f347c284SJohnny Huang 	int prog_flag = 0;
698f347c284SJohnny Huang 
699f347c284SJohnny Huang 	// ignore this bit
700f347c284SJohnny Huang 	if (ibit == 1)
701f347c284SJohnny Huang 		return OTP_SUCCESS;
702f347c284SJohnny Huang 	printf("OTPSTRAP[%X]:\n", offset);
703f347c284SJohnny Huang 
704f347c284SJohnny Huang 	if (bit == otpstrap->value) {
705f347c284SJohnny Huang 		if (!pbit && !rpbit) {
706f347c284SJohnny Huang 			printf("    The value is same as before, skip it.\n");
707f347c284SJohnny Huang 			return OTP_PROG_SKIP;
708f347c284SJohnny Huang 		}
709f347c284SJohnny Huang 		printf("    The value is same as before.\n");
710f347c284SJohnny Huang 	} else {
711f347c284SJohnny Huang 		prog_flag = 1;
712f347c284SJohnny Huang 	}
713f347c284SJohnny Huang 	if (otpstrap->protected == 1 && prog_flag) {
714f347c284SJohnny Huang 		printf("    This bit is protected and is not writable\n");
715f347c284SJohnny Huang 		return OTP_FAILURE;
716f347c284SJohnny Huang 	}
717f347c284SJohnny Huang 	if (otpstrap->remain_times == 0 && prog_flag) {
718f347c284SJohnny Huang 		printf("    This bit is no remaining times to write.\n");
719f347c284SJohnny Huang 		return OTP_FAILURE;
720f347c284SJohnny Huang 	}
721f347c284SJohnny Huang 	if (pbit == 1)
722f347c284SJohnny Huang 		printf("    This bit will be protected and become non-writable.\n");
723f347c284SJohnny Huang 	if (rpbit == 1 && info_cb.version != OTP_A0)
724f347c284SJohnny Huang 		printf("    The relative register will be protected.\n");
725f347c284SJohnny Huang 	if (prog_flag)
726f347c284SJohnny 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);
727f347c284SJohnny Huang 
728f347c284SJohnny Huang 	return OTP_SUCCESS;
729f347c284SJohnny Huang }
730f347c284SJohnny Huang 
731f347c284SJohnny Huang static int otp_prog_strap_b(int bit_offset, int value)
732f347c284SJohnny Huang {
733f347c284SJohnny Huang 	struct otpstrap_status otpstrap[64];
734f347c284SJohnny Huang 	u32 prog_address;
735f347c284SJohnny Huang 	int offset;
736f347c284SJohnny Huang 	int ret;
737f347c284SJohnny Huang 
738f347c284SJohnny Huang 	otp_strap_status(otpstrap);
739f347c284SJohnny Huang 
740f347c284SJohnny Huang 	ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0, 0);
741f347c284SJohnny Huang 
742f347c284SJohnny Huang 	if (ret != OTP_SUCCESS)
743f347c284SJohnny Huang 		return ret;
744f347c284SJohnny Huang 
745f347c284SJohnny Huang 	prog_address = 0x800;
746f347c284SJohnny Huang 	if (bit_offset < 32) {
747f347c284SJohnny Huang 		offset = bit_offset;
748f347c284SJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) / 8) * 0x200;
749f347c284SJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) % 8) * 0x2;
750f347c284SJohnny Huang 
751f347c284SJohnny Huang 	} else {
752f347c284SJohnny Huang 		offset = (bit_offset - 32);
753f347c284SJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) / 8) * 0x200;
754f347c284SJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) % 8) * 0x2;
755f347c284SJohnny Huang 	}
756f347c284SJohnny Huang 
757f347c284SJohnny Huang 	return otp_prog_dc_b(1, prog_address, offset);
758f347c284SJohnny Huang }
759f347c284SJohnny Huang 
760f347c284SJohnny Huang static int otp_print_conf(u32 offset, int dw_count)
761f347c284SJohnny Huang {
762f347c284SJohnny Huang 	int i;
763f347c284SJohnny Huang 	u32 ret[1];
764f347c284SJohnny Huang 
765f347c284SJohnny Huang 	if (offset + dw_count > 32)
766f347c284SJohnny Huang 		return OTP_USAGE;
767f347c284SJohnny Huang 	otp_soak(0);
768f347c284SJohnny Huang 	for (i = offset; i < offset + dw_count; i++) {
769f347c284SJohnny Huang 		otp_read_conf(i, ret);
770f347c284SJohnny Huang 		printf("OTPCFG%X: %08X\n", i, ret[0]);
771f347c284SJohnny Huang 	}
772f347c284SJohnny Huang 	printf("\n");
773f347c284SJohnny Huang 	return OTP_SUCCESS;
774f347c284SJohnny Huang }
775f347c284SJohnny Huang 
776f347c284SJohnny Huang static int otp_print_data(u32 offset, int dw_count)
777f347c284SJohnny Huang {
778f347c284SJohnny Huang 	int i;
779f347c284SJohnny Huang 	u32 ret[2];
780f347c284SJohnny Huang 
781f347c284SJohnny Huang 	if (offset + dw_count > 2048 || offset % 4 != 0)
782f347c284SJohnny Huang 		return OTP_USAGE;
783f347c284SJohnny Huang 	otp_soak(0);
784f347c284SJohnny Huang 	for (i = offset; i < offset + dw_count; i += 2) {
785f347c284SJohnny Huang 		otp_read_data(i, ret);
786f347c284SJohnny Huang 		if (i % 4 == 0)
787f347c284SJohnny Huang 			printf("%03X: %08X %08X ", i * 4, ret[0], ret[1]);
788f347c284SJohnny Huang 		else
789f347c284SJohnny Huang 			printf("%08X %08X\n", ret[0], ret[1]);
790f347c284SJohnny Huang 	}
791f347c284SJohnny Huang 	printf("\n");
792f347c284SJohnny Huang 	return OTP_SUCCESS;
793f347c284SJohnny Huang }
794f347c284SJohnny Huang 
795f347c284SJohnny Huang static int otp_print_strap(int start, int count)
796f347c284SJohnny Huang {
797f347c284SJohnny Huang 	int i, j;
798f347c284SJohnny Huang 	int remains;
799f347c284SJohnny Huang 	struct otpstrap_status otpstrap[64];
800f347c284SJohnny Huang 
801f347c284SJohnny Huang 	if (start < 0 || start > 64)
802f347c284SJohnny Huang 		return OTP_USAGE;
803f347c284SJohnny Huang 
804f347c284SJohnny Huang 	if ((start + count) < 0 || (start + count) > 64)
805f347c284SJohnny Huang 		return OTP_USAGE;
806f347c284SJohnny Huang 
807f347c284SJohnny Huang 	otp_strap_status(otpstrap);
808f347c284SJohnny Huang 
809f347c284SJohnny Huang 	if (info_cb.version == OTP_A0) {
810f347c284SJohnny Huang 		remains = 7;
811f347c284SJohnny Huang 		printf("BIT(hex)  Value  Option           Status\n");
812f347c284SJohnny Huang 	} else {
813f347c284SJohnny Huang 		remains = 6;
814f347c284SJohnny Huang 		printf("BIT(hex)  Value  Option         Reg_Protect Status\n");
815f347c284SJohnny Huang 	}
816f347c284SJohnny Huang 	printf("______________________________________________________________________________\n");
817f347c284SJohnny Huang 
818f347c284SJohnny Huang 	for (i = start; i < start + count; i++) {
819f347c284SJohnny Huang 		printf("0x%-8X", i);
820f347c284SJohnny Huang 		printf("%-7d", otpstrap[i].value);
821f347c284SJohnny Huang 		for (j = 0; j < remains; j++)
822f347c284SJohnny Huang 			printf("%d ", otpstrap[i].option_array[j]);
823f347c284SJohnny Huang 		printf("   ");
824f347c284SJohnny Huang 		if (info_cb.version != OTP_A0)
825f347c284SJohnny Huang 			printf("%d           ", otpstrap[i].reg_protected);
826f347c284SJohnny Huang 		if (otpstrap[i].protected == 1) {
827f347c284SJohnny Huang 			printf("protected and not writable");
828f347c284SJohnny Huang 		} else {
829f347c284SJohnny Huang 			printf("not protected ");
830f347c284SJohnny Huang 			if (otpstrap[i].remain_times == 0)
831f347c284SJohnny Huang 				printf("and no remaining times to write.");
832f347c284SJohnny Huang 			else
833f347c284SJohnny Huang 				printf("and still can write %d times", otpstrap[i].remain_times);
834f347c284SJohnny Huang 		}
835f347c284SJohnny Huang 		printf("\n");
836f347c284SJohnny Huang 	}
837f347c284SJohnny Huang 
838f347c284SJohnny Huang 	return OTP_SUCCESS;
839f347c284SJohnny Huang }
840f347c284SJohnny Huang 
841794e27ecSJohnny Huang static void otp_print_revid(u32 *rid)
842794e27ecSJohnny Huang {
843794e27ecSJohnny Huang 	int bit_offset;
844794e27ecSJohnny Huang 	int i, j;
845794e27ecSJohnny Huang 
846794e27ecSJohnny Huang 	printf("     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f\n");
847794e27ecSJohnny Huang 	printf("___________________________________________________\n");
848794e27ecSJohnny Huang 	for (i = 0; i < 64; i++) {
849794e27ecSJohnny Huang 		if (i < 32) {
850794e27ecSJohnny Huang 			j = 0;
851794e27ecSJohnny Huang 			bit_offset = i;
852794e27ecSJohnny Huang 		} else {
853794e27ecSJohnny Huang 			j = 1;
854794e27ecSJohnny Huang 			bit_offset = i - 32;
855794e27ecSJohnny Huang 		}
856794e27ecSJohnny Huang 		if (i % 16 == 0)
857794e27ecSJohnny Huang 			printf("%2x | ", i);
858794e27ecSJohnny Huang 		printf("%d  ", (rid[j] >> bit_offset) & 0x1);
859794e27ecSJohnny Huang 		if ((i + 1) % 16 == 0)
860794e27ecSJohnny Huang 			printf("\n");
861794e27ecSJohnny Huang 	}
862794e27ecSJohnny Huang }
863794e27ecSJohnny Huang 
864696656c6SJohnny Huang static int otp_print_conf_image(struct otp_image_layout *image_layout)
86569d5fd8fSJohnny Huang {
86679e42a59SJoel Stanley 	const struct otpconf_info *conf_info = info_cb.conf_info;
867a219f6deSJohnny Huang 	u32 *OTPCFG = (u32 *)image_layout->conf;
868a219f6deSJohnny Huang 	u32 *OTPCFG_IGNORE = (u32 *)image_layout->conf_ignore;
869a219f6deSJohnny Huang 	u32 mask;
870a219f6deSJohnny Huang 	u32 dw_offset;
871a219f6deSJohnny Huang 	u32 bit_offset;
872a219f6deSJohnny Huang 	u32 otp_value;
873a219f6deSJohnny Huang 	u32 otp_ignore;
874b458cd62SJohnny Huang 	int fail = 0;
8757adec5f6SJohnny Huang 	int mask_err;
876794e27ecSJohnny Huang 	int rid_num = 0;
87773f11549SJohnny Huang 	char valid_bit[20];
878794e27ecSJohnny Huang 	int fz;
87966f2f8e5SJohnny Huang 	int i;
88073f11549SJohnny Huang 	int j;
88166f2f8e5SJohnny Huang 
882737ed20bSJohnny Huang 	printf("DW    BIT        Value       Description\n");
88366f2f8e5SJohnny Huang 	printf("__________________________________________________________________________\n");
8843cb28812SJohnny Huang 	for (i = 0; i < info_cb.conf_info_len; i++) {
8857adec5f6SJohnny Huang 		mask_err = 0;
8863cb28812SJohnny Huang 		dw_offset = conf_info[i].dw_offset;
8873cb28812SJohnny Huang 		bit_offset = conf_info[i].bit_offset;
8883cb28812SJohnny Huang 		mask = BIT(conf_info[i].length) - 1;
889b458cd62SJohnny Huang 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
890696656c6SJohnny Huang 		otp_ignore = (OTPCFG_IGNORE[dw_offset] >> bit_offset) & mask;
891b458cd62SJohnny Huang 
8927adec5f6SJohnny Huang 		if (conf_info[i].value == OTP_REG_VALID_BIT) {
8937adec5f6SJohnny Huang 			if (((otp_value + otp_ignore) & mask) != mask) {
894b458cd62SJohnny Huang 				fail = 1;
8957adec5f6SJohnny Huang 				mask_err = 1;
8967adec5f6SJohnny Huang 			}
8977adec5f6SJohnny Huang 		} else {
8987adec5f6SJohnny Huang 			if (otp_ignore == mask) {
8997adec5f6SJohnny Huang 				continue;
9007adec5f6SJohnny Huang 			} else if (otp_ignore != 0) {
9017adec5f6SJohnny Huang 				fail = 1;
9027adec5f6SJohnny Huang 				mask_err = 1;
9037adec5f6SJohnny Huang 			}
9047adec5f6SJohnny Huang 		}
905b458cd62SJohnny Huang 
906a219f6deSJohnny Huang 		if (otp_value != conf_info[i].value &&
9073cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_RESERVED &&
9083cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALUE &&
9093cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALID_BIT)
910b458cd62SJohnny Huang 			continue;
911b458cd62SJohnny Huang 		printf("0x%-4X", dw_offset);
912b458cd62SJohnny Huang 
9133cb28812SJohnny Huang 		if (conf_info[i].length == 1) {
9143cb28812SJohnny Huang 			printf("0x%-9X", conf_info[i].bit_offset);
91566f2f8e5SJohnny Huang 		} else {
916b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
9173cb28812SJohnny Huang 			       conf_info[i].bit_offset + conf_info[i].length - 1,
9183cb28812SJohnny Huang 			       conf_info[i].bit_offset);
91966f2f8e5SJohnny Huang 		}
920b458cd62SJohnny Huang 		printf("0x%-10x", otp_value);
921b458cd62SJohnny Huang 
9227adec5f6SJohnny Huang 		if (mask_err) {
9237adec5f6SJohnny Huang 			printf("Ignore, mask error\n");
924a219f6deSJohnny Huang 			continue;
925a219f6deSJohnny Huang 		}
9263cb28812SJohnny Huang 		if (conf_info[i].value == OTP_REG_RESERVED) {
927b458cd62SJohnny Huang 			printf("Reserved\n");
9283cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALUE) {
9293cb28812SJohnny Huang 			printf(conf_info[i].information, otp_value);
930b458cd62SJohnny Huang 			printf("\n");
9313cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALID_BIT) {
932b458cd62SJohnny Huang 			if (otp_value != 0) {
93373f11549SJohnny Huang 				for (j = 0; j < 7; j++) {
934a219f6deSJohnny Huang 					if (otp_value == (1 << j))
93573f11549SJohnny Huang 						valid_bit[j * 2] = '1';
936a219f6deSJohnny Huang 					else
93773f11549SJohnny Huang 						valid_bit[j * 2] = '0';
93873f11549SJohnny Huang 					valid_bit[j * 2 + 1] = ' ';
93973f11549SJohnny Huang 				}
94073f11549SJohnny Huang 				valid_bit[15] = 0;
94173f11549SJohnny Huang 			} else {
94273f11549SJohnny Huang 				strcpy(valid_bit, "0 0 0 0 0 0 0 0\0");
943b458cd62SJohnny Huang 			}
9443cb28812SJohnny Huang 			printf(conf_info[i].information, valid_bit);
945b458cd62SJohnny Huang 			printf("\n");
946b458cd62SJohnny Huang 		} else {
9473cb28812SJohnny Huang 			printf("%s\n", conf_info[i].information);
948b458cd62SJohnny Huang 		}
949b458cd62SJohnny Huang 	}
950b458cd62SJohnny Huang 
951794e27ecSJohnny Huang 	if (OTPCFG[0xa] != 0 || OTPCFG[0xb] != 0) {
952794e27ecSJohnny Huang 		if (OTPCFG_IGNORE[0xa] != 0 && OTPCFG_IGNORE[0xb] != 0) {
953794e27ecSJohnny Huang 			printf("OTP revision ID is invalid.\n");
954794e27ecSJohnny Huang 			fail = 1;
955794e27ecSJohnny Huang 		} else {
956794e27ecSJohnny Huang 			fz = 0;
957794e27ecSJohnny Huang 			for (i = 0; i < 64; i++) {
958794e27ecSJohnny Huang 				if (get_dw_bit(&OTPCFG[0xa], i) == 0) {
959794e27ecSJohnny Huang 					if (!fz)
960794e27ecSJohnny Huang 						fz = 1;
961794e27ecSJohnny Huang 				} else {
962794e27ecSJohnny Huang 					rid_num++;
963794e27ecSJohnny Huang 					if (fz) {
964794e27ecSJohnny Huang 						printf("OTP revision ID is invalid.\n");
965794e27ecSJohnny Huang 						fail = 1;
966794e27ecSJohnny Huang 						break;
967794e27ecSJohnny Huang 					}
968794e27ecSJohnny Huang 				}
969794e27ecSJohnny Huang 			}
970794e27ecSJohnny Huang 		}
971794e27ecSJohnny Huang 		if (fail)
972794e27ecSJohnny Huang 			printf("OTP revision ID\n");
973794e27ecSJohnny Huang 		else
974794e27ecSJohnny Huang 			printf("OTP revision ID: 0x%x\n", rid_num);
975794e27ecSJohnny Huang 		otp_print_revid(&OTPCFG[0xa]);
976794e27ecSJohnny Huang 	}
977794e27ecSJohnny Huang 
978b458cd62SJohnny Huang 	if (fail)
979b458cd62SJohnny Huang 		return OTP_FAILURE;
980b458cd62SJohnny Huang 
98166f2f8e5SJohnny Huang 	return OTP_SUCCESS;
98266f2f8e5SJohnny Huang }
98366f2f8e5SJohnny Huang 
9842d4b0742SJohnny Huang static int otp_print_conf_info(int input_offset)
98566f2f8e5SJohnny Huang {
98679e42a59SJoel Stanley 	const struct otpconf_info *conf_info = info_cb.conf_info;
987a219f6deSJohnny Huang 	u32 OTPCFG[16];
988a219f6deSJohnny Huang 	u32 mask;
989a219f6deSJohnny Huang 	u32 dw_offset;
990a219f6deSJohnny Huang 	u32 bit_offset;
991a219f6deSJohnny Huang 	u32 otp_value;
99273f11549SJohnny Huang 	char valid_bit[20];
99366f2f8e5SJohnny Huang 	int i;
99473f11549SJohnny Huang 	int j;
99566f2f8e5SJohnny Huang 
996dacbba92SJohnny Huang 	otp_soak(0);
997bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++)
998f347c284SJohnny Huang 		otp_read_conf(i, &OTPCFG[i]);
99966f2f8e5SJohnny Huang 
1000b458cd62SJohnny Huang 	printf("DW    BIT        Value       Description\n");
1001b458cd62SJohnny Huang 	printf("__________________________________________________________________________\n");
10023cb28812SJohnny Huang 	for (i = 0; i < info_cb.conf_info_len; i++) {
10033cb28812SJohnny Huang 		if (input_offset != -1 && input_offset != conf_info[i].dw_offset)
10042d4b0742SJohnny Huang 			continue;
10053cb28812SJohnny Huang 		dw_offset = conf_info[i].dw_offset;
10063cb28812SJohnny Huang 		bit_offset = conf_info[i].bit_offset;
10073cb28812SJohnny Huang 		mask = BIT(conf_info[i].length) - 1;
1008b458cd62SJohnny Huang 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
1009b458cd62SJohnny Huang 
1010a219f6deSJohnny Huang 		if (otp_value != conf_info[i].value &&
10113cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_RESERVED &&
10123cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALUE &&
10133cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALID_BIT)
1014b458cd62SJohnny Huang 			continue;
1015b458cd62SJohnny Huang 		printf("0x%-4X", dw_offset);
1016b458cd62SJohnny Huang 
10173cb28812SJohnny Huang 		if (conf_info[i].length == 1) {
10183cb28812SJohnny Huang 			printf("0x%-9X", conf_info[i].bit_offset);
1019b458cd62SJohnny Huang 		} else {
1020b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
10213cb28812SJohnny Huang 			       conf_info[i].bit_offset + conf_info[i].length - 1,
10223cb28812SJohnny Huang 			       conf_info[i].bit_offset);
1023b458cd62SJohnny Huang 		}
1024b458cd62SJohnny Huang 		printf("0x%-10x", otp_value);
1025b458cd62SJohnny Huang 
10263cb28812SJohnny Huang 		if (conf_info[i].value == OTP_REG_RESERVED) {
1027b458cd62SJohnny Huang 			printf("Reserved\n");
10283cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALUE) {
10293cb28812SJohnny Huang 			printf(conf_info[i].information, otp_value);
1030b458cd62SJohnny Huang 			printf("\n");
10313cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALID_BIT) {
1032b458cd62SJohnny Huang 			if (otp_value != 0) {
103373f11549SJohnny Huang 				for (j = 0; j < 7; j++) {
1034a219f6deSJohnny Huang 					if (otp_value == (1 << j))
103573f11549SJohnny Huang 						valid_bit[j * 2] = '1';
1036a219f6deSJohnny Huang 					else
103773f11549SJohnny Huang 						valid_bit[j * 2] = '0';
103873f11549SJohnny Huang 					valid_bit[j * 2 + 1] = ' ';
103973f11549SJohnny Huang 				}
104073f11549SJohnny Huang 				valid_bit[15] = 0;
104173f11549SJohnny Huang 			} else {
104273f11549SJohnny Huang 				strcpy(valid_bit, "0 0 0 0 0 0 0 0\0");
1043b458cd62SJohnny Huang 			}
10443cb28812SJohnny Huang 			printf(conf_info[i].information, valid_bit);
1045b458cd62SJohnny Huang 			printf("\n");
1046b458cd62SJohnny Huang 		} else {
10473cb28812SJohnny Huang 			printf("%s\n", conf_info[i].information);
1048b458cd62SJohnny Huang 		}
1049b458cd62SJohnny Huang 	}
1050b458cd62SJohnny Huang 	return OTP_SUCCESS;
105166f2f8e5SJohnny Huang }
105266f2f8e5SJohnny Huang 
10535010032bSJohnny Huang static int otp_print_strap_image(struct otp_image_layout *image_layout)
105476d13988SJohnny Huang {
105579e42a59SJoel Stanley 	const struct otpstrap_info *strap_info = info_cb.strap_info;
1056a219f6deSJohnny Huang 	u32 *OTPSTRAP;
1057a219f6deSJohnny Huang 	u32 *OTPSTRAP_REG_PRO;
1058a219f6deSJohnny Huang 	u32 *OTPSTRAP_PRO;
1059a219f6deSJohnny Huang 	u32 *OTPSTRAP_IGNORE;
106076d13988SJohnny Huang 	int i;
1061a8bd6d8cSJohnny Huang 	int fail = 0;
1062a219f6deSJohnny Huang 	u32 bit_offset;
1063a219f6deSJohnny Huang 	u32 dw_offset;
1064a219f6deSJohnny Huang 	u32 mask;
1065a219f6deSJohnny Huang 	u32 otp_value;
1066a219f6deSJohnny Huang 	u32 otp_reg_protect;
1067a219f6deSJohnny Huang 	u32 otp_protect;
1068a219f6deSJohnny Huang 	u32 otp_ignore;
106976d13988SJohnny Huang 
1070a219f6deSJohnny Huang 	OTPSTRAP = (u32 *)image_layout->strap;
1071a219f6deSJohnny Huang 	OTPSTRAP_PRO = (u32 *)image_layout->strap_pro;
1072a219f6deSJohnny Huang 	OTPSTRAP_IGNORE = (u32 *)image_layout->strap_ignore;
1073e417205bSJohnny Huang 	if (info_cb.version == OTP_A0) {
1074696656c6SJohnny Huang 		OTPSTRAP_REG_PRO = NULL;
1075a8bd6d8cSJohnny Huang 		printf("BIT(hex)   Value       Protect     Description\n");
1076696656c6SJohnny Huang 	} else {
1077a219f6deSJohnny Huang 		OTPSTRAP_REG_PRO = (u32 *)image_layout->strap_reg_pro;
1078de6b0cc4SJohnny Huang 		printf("BIT(hex)   Value       Reg_Protect Protect     Description\n");
1079696656c6SJohnny Huang 	}
1080de6b0cc4SJohnny Huang 	printf("__________________________________________________________________________________________\n");
1081b458cd62SJohnny Huang 
10823cb28812SJohnny Huang 	for (i = 0; i < info_cb.strap_info_len; i++) {
10837adec5f6SJohnny Huang 		fail = 0;
1084696656c6SJohnny Huang 		if (strap_info[i].bit_offset > 31) {
1085a8bd6d8cSJohnny Huang 			dw_offset = 1;
10863cb28812SJohnny Huang 			bit_offset = strap_info[i].bit_offset - 32;
1087a8bd6d8cSJohnny Huang 		} else {
1088a8bd6d8cSJohnny Huang 			dw_offset = 0;
10893cb28812SJohnny Huang 			bit_offset = strap_info[i].bit_offset;
1090a8bd6d8cSJohnny Huang 		}
109176d13988SJohnny Huang 
10923cb28812SJohnny Huang 		mask = BIT(strap_info[i].length) - 1;
1093a8bd6d8cSJohnny Huang 		otp_value = (OTPSTRAP[dw_offset] >> bit_offset) & mask;
1094a8bd6d8cSJohnny Huang 		otp_protect = (OTPSTRAP_PRO[dw_offset] >> bit_offset) & mask;
1095696656c6SJohnny Huang 		otp_ignore = (OTPSTRAP_IGNORE[dw_offset] >> bit_offset) & mask;
1096a8bd6d8cSJohnny Huang 
1097e417205bSJohnny Huang 		if (info_cb.version != OTP_A0)
1098696656c6SJohnny Huang 			otp_reg_protect = (OTPSTRAP_REG_PRO[dw_offset] >> bit_offset) & mask;
10995010032bSJohnny Huang 		else
11005010032bSJohnny Huang 			otp_reg_protect = 0;
1101696656c6SJohnny Huang 
1102a219f6deSJohnny Huang 		if (otp_ignore == mask)
1103a8bd6d8cSJohnny Huang 			continue;
1104a219f6deSJohnny Huang 		else if (otp_ignore != 0)
1105a8bd6d8cSJohnny Huang 			fail = 1;
1106a8bd6d8cSJohnny Huang 
1107a219f6deSJohnny Huang 		if (otp_value != strap_info[i].value &&
11083cb28812SJohnny Huang 		    strap_info[i].value != OTP_REG_RESERVED)
1109a8bd6d8cSJohnny Huang 			continue;
1110a8bd6d8cSJohnny Huang 
11113cb28812SJohnny Huang 		if (strap_info[i].length == 1) {
11123cb28812SJohnny Huang 			printf("0x%-9X", strap_info[i].bit_offset);
1113a8bd6d8cSJohnny Huang 		} else {
1114b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
11153cb28812SJohnny Huang 			       strap_info[i].bit_offset + strap_info[i].length - 1,
11163cb28812SJohnny Huang 			       strap_info[i].bit_offset);
1117a8bd6d8cSJohnny Huang 		}
1118a8bd6d8cSJohnny Huang 		printf("0x%-10x", otp_value);
1119e417205bSJohnny Huang 		if (info_cb.version != OTP_A0)
1120696656c6SJohnny Huang 			printf("0x%-10x", otp_reg_protect);
1121a8bd6d8cSJohnny Huang 		printf("0x%-10x", otp_protect);
1122a8bd6d8cSJohnny Huang 
1123a8bd6d8cSJohnny Huang 		if (fail) {
1124696656c6SJohnny Huang 			printf("Ignore mask error\n");
1125a8bd6d8cSJohnny Huang 		} else {
11263cb28812SJohnny Huang 			if (strap_info[i].value != OTP_REG_RESERVED)
11273cb28812SJohnny Huang 				printf("%s\n", strap_info[i].information);
1128a8bd6d8cSJohnny Huang 			else
1129a8bd6d8cSJohnny Huang 				printf("Reserved\n");
1130a8bd6d8cSJohnny Huang 		}
1131a8bd6d8cSJohnny Huang 	}
1132a8bd6d8cSJohnny Huang 
1133a8bd6d8cSJohnny Huang 	if (fail)
113476d13988SJohnny Huang 		return OTP_FAILURE;
113576d13988SJohnny Huang 
113676d13988SJohnny Huang 	return OTP_SUCCESS;
113776d13988SJohnny Huang }
113876d13988SJohnny Huang 
1139b458cd62SJohnny Huang static int otp_print_strap_info(int view)
114076d13988SJohnny Huang {
114179e42a59SJoel Stanley 	const struct otpstrap_info *strap_info = info_cb.strap_info;
114276d13988SJohnny Huang 	struct otpstrap_status strap_status[64];
114307baa4e8SJohnny Huang 	int i, j;
1144b458cd62SJohnny Huang 	int fail = 0;
1145a219f6deSJohnny Huang 	u32 bit_offset;
1146a219f6deSJohnny Huang 	u32 length;
1147a219f6deSJohnny Huang 	u32 otp_value;
1148a219f6deSJohnny Huang 	u32 otp_protect;
114976d13988SJohnny Huang 
1150541eb887SJohnny Huang 	otp_strap_status(strap_status);
115176d13988SJohnny Huang 
1152b458cd62SJohnny Huang 	if (view) {
1153e417205bSJohnny Huang 		if (info_cb.version == OTP_A0)
115407baa4e8SJohnny Huang 			printf("BIT(hex) Value  Remains  Protect   Description\n");
115583655e91SJohnny Huang 		else
115683655e91SJohnny Huang 			printf("BIT(hex) Value  Remains  Reg_Protect Protect   Description\n");
115707baa4e8SJohnny Huang 		printf("___________________________________________________________________________________________________\n");
1158b458cd62SJohnny Huang 	} else {
1159b458cd62SJohnny Huang 		printf("BIT(hex)   Value       Description\n");
1160b458cd62SJohnny Huang 		printf("________________________________________________________________________________\n");
116176d13988SJohnny Huang 	}
11623cb28812SJohnny Huang 	for (i = 0; i < info_cb.strap_info_len; i++) {
1163b458cd62SJohnny Huang 		otp_value = 0;
11643cb28812SJohnny Huang 		bit_offset = strap_info[i].bit_offset;
11653cb28812SJohnny Huang 		length = strap_info[i].length;
1166b458cd62SJohnny Huang 		for (j = 0; j < length; j++) {
1167c947ef08SJohnny Huang 			otp_value |= strap_status[bit_offset + j].value << j;
1168c947ef08SJohnny Huang 			otp_protect |= strap_status[bit_offset + j].protected << j;
1169b458cd62SJohnny Huang 		}
1170a219f6deSJohnny Huang 		if (otp_value != strap_info[i].value &&
11713cb28812SJohnny Huang 		    strap_info[i].value != OTP_REG_RESERVED)
1172b458cd62SJohnny Huang 			continue;
1173b458cd62SJohnny Huang 		if (view) {
1174b458cd62SJohnny Huang 			for (j = 0; j < length; j++) {
11753cb28812SJohnny Huang 				printf("0x%-7X", strap_info[i].bit_offset + j);
1176b458cd62SJohnny Huang 				printf("0x%-5X", strap_status[bit_offset + j].value);
117707baa4e8SJohnny Huang 				printf("%-9d", strap_status[bit_offset + j].remain_times);
1178e417205bSJohnny Huang 				if (info_cb.version != OTP_A0)
1179e1a7245eSJohnny Huang 					printf("0x%-10X", strap_status[bit_offset + j].reg_protected);
1180e1a7245eSJohnny Huang 				printf("0x%-7X", strap_status[bit_offset + j].protected);
11813cb28812SJohnny Huang 				if (strap_info[i].value == OTP_REG_RESERVED) {
1182b458cd62SJohnny Huang 					printf(" Reserved\n");
1183b458cd62SJohnny Huang 					continue;
1184b458cd62SJohnny Huang 				}
1185b458cd62SJohnny Huang 				if (length == 1) {
11863cb28812SJohnny Huang 					printf(" %s\n", strap_info[i].information);
1187b458cd62SJohnny Huang 					continue;
118876d13988SJohnny Huang 				}
118976d13988SJohnny Huang 
1190b458cd62SJohnny Huang 				if (j == 0)
11913cb28812SJohnny Huang 					printf("/%s\n", strap_info[i].information);
1192b458cd62SJohnny Huang 				else if (j == length - 1)
1193b458cd62SJohnny Huang 					printf("\\ \"\n");
1194b458cd62SJohnny Huang 				else
1195b458cd62SJohnny Huang 					printf("| \"\n");
119676d13988SJohnny Huang 			}
1197b458cd62SJohnny Huang 		} else {
1198c947ef08SJohnny Huang 			if (length == 1) {
11993cb28812SJohnny Huang 				printf("0x%-9X", strap_info[i].bit_offset);
1200b458cd62SJohnny Huang 			} else {
1201b458cd62SJohnny Huang 				printf("0x%-2X:0x%-4X",
1202b458cd62SJohnny Huang 				       bit_offset + length - 1, bit_offset);
1203b458cd62SJohnny Huang 			}
1204b458cd62SJohnny Huang 
1205b458cd62SJohnny Huang 			printf("0x%-10X", otp_value);
1206b458cd62SJohnny Huang 
12073cb28812SJohnny Huang 			if (strap_info[i].value != OTP_REG_RESERVED)
12083cb28812SJohnny Huang 				printf("%s\n", strap_info[i].information);
1209b458cd62SJohnny Huang 			else
1210b458cd62SJohnny Huang 				printf("Reserved\n");
1211b458cd62SJohnny Huang 		}
1212b458cd62SJohnny Huang 	}
1213b458cd62SJohnny Huang 
1214b458cd62SJohnny Huang 	if (fail)
1215b458cd62SJohnny Huang 		return OTP_FAILURE;
1216b458cd62SJohnny Huang 
1217b458cd62SJohnny Huang 	return OTP_SUCCESS;
1218b458cd62SJohnny Huang }
1219b458cd62SJohnny Huang 
1220f347c284SJohnny Huang static int otp_print_data_image(struct otp_image_layout *image_layout)
122169d5fd8fSJohnny Huang {
122269d5fd8fSJohnny Huang 	int key_id, key_offset, last, key_type, key_length, exp_length;
122379e42a59SJoel Stanley 	const struct otpkey_type *key_info_array = info_cb.key_info;
12249a4fe690SJohnny Huang 	struct otpkey_type key_info;
1225a219f6deSJohnny Huang 	u32 *buf;
1226a219f6deSJohnny Huang 	u8 *byte_buf;
12279d998018SJohnny Huang 	char empty = 1;
122869d5fd8fSJohnny Huang 	int i = 0, len = 0;
12299a4fe690SJohnny Huang 	int j;
123054552c69SJohnny Huang 
1231696656c6SJohnny Huang 	byte_buf = image_layout->data;
1232a219f6deSJohnny Huang 	buf = (u32 *)byte_buf;
12339d998018SJohnny Huang 
12349d998018SJohnny Huang 	for (i = 0; i < 16; i++) {
1235a219f6deSJohnny Huang 		if (buf[i] != 0)
12369d998018SJohnny Huang 			empty = 0;
12379d998018SJohnny Huang 	}
12389d998018SJohnny Huang 	if (empty)
1239f347c284SJohnny Huang 		return OTP_SUCCESS;
12409d998018SJohnny Huang 
12419d998018SJohnny Huang 	i = 0;
124269d5fd8fSJohnny Huang 	while (1) {
124369d5fd8fSJohnny Huang 		key_id = buf[i] & 0x7;
124469d5fd8fSJohnny Huang 		key_offset = buf[i] & 0x1ff8;
124569d5fd8fSJohnny Huang 		last = (buf[i] >> 13) & 1;
124669d5fd8fSJohnny Huang 		key_type = (buf[i] >> 14) & 0xf;
124769d5fd8fSJohnny Huang 		key_length = (buf[i] >> 18) & 0x3;
124869d5fd8fSJohnny Huang 		exp_length = (buf[i] >> 20) & 0xfff;
12499a4fe690SJohnny Huang 
12509a4fe690SJohnny Huang 		for (j = 0; j < info_cb.key_info_len; j++) {
12519a4fe690SJohnny Huang 			if (key_type == key_info_array[j].value) {
12529a4fe690SJohnny Huang 				key_info = key_info_array[j];
12539a4fe690SJohnny Huang 				break;
12549a4fe690SJohnny Huang 			}
12559a4fe690SJohnny Huang 		}
12569a4fe690SJohnny Huang 
12577f795e57SJohnny Huang 		printf("\nKey[%d]:\n", i);
125869d5fd8fSJohnny Huang 		printf("Key Type: ");
12599a4fe690SJohnny Huang 		printf("%s\n", key_info.information);
12609a4fe690SJohnny Huang 
12619a4fe690SJohnny Huang 		if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
126269d5fd8fSJohnny Huang 			printf("HMAC SHA Type: ");
126369d5fd8fSJohnny Huang 			switch (key_length) {
126469d5fd8fSJohnny Huang 			case 0:
126569d5fd8fSJohnny Huang 				printf("HMAC(SHA224)\n");
126669d5fd8fSJohnny Huang 				break;
126769d5fd8fSJohnny Huang 			case 1:
126869d5fd8fSJohnny Huang 				printf("HMAC(SHA256)\n");
126969d5fd8fSJohnny Huang 				break;
127069d5fd8fSJohnny Huang 			case 2:
127169d5fd8fSJohnny Huang 				printf("HMAC(SHA384)\n");
127269d5fd8fSJohnny Huang 				break;
127369d5fd8fSJohnny Huang 			case 3:
127469d5fd8fSJohnny Huang 				printf("HMAC(SHA512)\n");
127569d5fd8fSJohnny Huang 				break;
127669d5fd8fSJohnny Huang 			}
1277181f72d8SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA_PRIV ||
1278181f72d8SJohnny Huang 			   key_info.key_type == OTP_KEY_TYPE_RSA_PUB) {
127969d5fd8fSJohnny Huang 			printf("RSA SHA Type: ");
128069d5fd8fSJohnny Huang 			switch (key_length) {
128169d5fd8fSJohnny Huang 			case 0:
128269d5fd8fSJohnny Huang 				printf("RSA1024\n");
128369d5fd8fSJohnny Huang 				len = 0x100;
128469d5fd8fSJohnny Huang 				break;
128569d5fd8fSJohnny Huang 			case 1:
128669d5fd8fSJohnny Huang 				printf("RSA2048\n");
128769d5fd8fSJohnny Huang 				len = 0x200;
128869d5fd8fSJohnny Huang 				break;
128969d5fd8fSJohnny Huang 			case 2:
129069d5fd8fSJohnny Huang 				printf("RSA3072\n");
129169d5fd8fSJohnny Huang 				len = 0x300;
129269d5fd8fSJohnny Huang 				break;
129369d5fd8fSJohnny Huang 			case 3:
129469d5fd8fSJohnny Huang 				printf("RSA4096\n");
129569d5fd8fSJohnny Huang 				len = 0x400;
129669d5fd8fSJohnny Huang 				break;
129769d5fd8fSJohnny Huang 			}
129869d5fd8fSJohnny Huang 			printf("RSA exponent bit length: %d\n", exp_length);
129969d5fd8fSJohnny Huang 		}
13009a4fe690SJohnny Huang 		if (key_info.need_id)
130169d5fd8fSJohnny Huang 			printf("Key Number ID: %d\n", key_id);
130269d5fd8fSJohnny Huang 		printf("Key Value:\n");
13039a4fe690SJohnny Huang 		if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
130469d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], 0x40);
13059a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_AES) {
13069a4fe690SJohnny Huang 			printf("AES Key:\n");
13079a4fe690SJohnny Huang 			buf_print(&byte_buf[key_offset], 0x20);
1308e417205bSJohnny Huang 			if (info_cb.version == OTP_A0) {
13099a4fe690SJohnny Huang 				printf("AES IV:\n");
13109a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x10);
13119a4fe690SJohnny Huang 			}
13129a4fe690SJohnny Huang 
13139a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_VAULT) {
1314e417205bSJohnny Huang 			if (info_cb.version == OTP_A0) {
131569d5fd8fSJohnny Huang 				printf("AES Key:\n");
131669d5fd8fSJohnny Huang 				buf_print(&byte_buf[key_offset], 0x20);
131769d5fd8fSJohnny Huang 				printf("AES IV:\n");
131869d5fd8fSJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x10);
13195fdde29fSJohnny Huang 			} else {
13209a4fe690SJohnny Huang 				printf("AES Key 1:\n");
13219a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset], 0x20);
13229a4fe690SJohnny Huang 				printf("AES Key 2:\n");
13239a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x20);
13249a4fe690SJohnny Huang 			}
1325181f72d8SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA_PRIV) {
132669d5fd8fSJohnny Huang 			printf("RSA mod:\n");
132769d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], len / 2);
132869d5fd8fSJohnny Huang 			printf("RSA exp:\n");
132969d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset + (len / 2)], len / 2);
1330181f72d8SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA_PUB) {
1331181f72d8SJohnny Huang 			printf("RSA mod:\n");
1332181f72d8SJohnny Huang 			buf_print(&byte_buf[key_offset], len / 2);
1333181f72d8SJohnny Huang 			printf("RSA exp:\n");
1334a219f6deSJohnny Huang 			buf_print((u8 *)"\x01\x00\x01", 3);
133569d5fd8fSJohnny Huang 		}
133669d5fd8fSJohnny Huang 		if (last)
133769d5fd8fSJohnny Huang 			break;
133869d5fd8fSJohnny Huang 		i++;
133969d5fd8fSJohnny Huang 	}
1340f347c284SJohnny Huang 	return OTP_SUCCESS;
1341f347c284SJohnny Huang }
1342f347c284SJohnny Huang 
1343f347c284SJohnny Huang static int otp_strap_image_confirm(struct otp_image_layout *image_layout)
1344f347c284SJohnny Huang {
1345f347c284SJohnny Huang 	int i;
1346f347c284SJohnny Huang 	u32 *strap;
1347f347c284SJohnny Huang 	u32 *strap_ignore;
1348f347c284SJohnny Huang 	u32 *strap_reg_protect;
1349f347c284SJohnny Huang 	u32 *strap_pro;
1350f347c284SJohnny Huang 	int bit, pbit, ibit, rpbit;
1351f347c284SJohnny Huang 	int fail = 0;
1352f347c284SJohnny Huang 	int ret;
1353f347c284SJohnny Huang 	struct otpstrap_status otpstrap[64];
1354f347c284SJohnny Huang 
1355f347c284SJohnny Huang 	strap = (u32 *)image_layout->strap;
1356f347c284SJohnny Huang 	strap_pro = (u32 *)image_layout->strap_pro;
1357f347c284SJohnny Huang 	strap_ignore = (u32 *)image_layout->strap_ignore;
1358f347c284SJohnny Huang 	strap_reg_protect = (u32 *)image_layout->strap_reg_pro;
1359f347c284SJohnny Huang 
1360f347c284SJohnny Huang 	otp_strap_status(otpstrap);
1361f347c284SJohnny Huang 	for (i = 0; i < 64; i++) {
1362f347c284SJohnny Huang 		if (i < 32) {
1363f347c284SJohnny Huang 			bit = (strap[0] >> i) & 0x1;
1364f347c284SJohnny Huang 			ibit = (strap_ignore[0] >> i) & 0x1;
1365f347c284SJohnny Huang 			pbit = (strap_pro[0] >> i) & 0x1;
1366f347c284SJohnny Huang 		} else {
1367f347c284SJohnny Huang 			bit = (strap[1] >> (i - 32)) & 0x1;
1368f347c284SJohnny Huang 			ibit = (strap_ignore[1] >> (i - 32)) & 0x1;
1369f347c284SJohnny Huang 			pbit = (strap_pro[1] >> (i - 32)) & 0x1;
1370f347c284SJohnny Huang 		}
1371f347c284SJohnny Huang 
1372f347c284SJohnny Huang 		if (info_cb.version != OTP_A0) {
1373f347c284SJohnny Huang 			if (i < 32)
1374f347c284SJohnny Huang 				rpbit = (strap_reg_protect[0] >> i) & 0x1;
1375f347c284SJohnny Huang 			else
1376f347c284SJohnny Huang 				rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1;
1377f347c284SJohnny Huang 		} else {
1378f347c284SJohnny Huang 			rpbit = 0;
1379f347c284SJohnny Huang 		}
1380f347c284SJohnny Huang 		ret = otp_strap_bit_confirm(&otpstrap[i], i, ibit, bit, pbit, rpbit);
1381f347c284SJohnny Huang 
1382f347c284SJohnny Huang 		if (ret == OTP_FAILURE)
1383f347c284SJohnny Huang 			fail = 1;
1384f347c284SJohnny Huang 	}
1385f347c284SJohnny Huang 	if (fail == 1)
1386f347c284SJohnny Huang 		return OTP_FAILURE;
1387f347c284SJohnny Huang 	else
1388f347c284SJohnny Huang 		return OTP_SUCCESS;
1389f347c284SJohnny Huang }
1390f347c284SJohnny Huang 
1391f347c284SJohnny Huang static int otp_prog_data(struct otp_image_layout *image_layout)
1392f347c284SJohnny Huang {
1393f347c284SJohnny Huang 	int i;
1394f347c284SJohnny Huang 	int ret;
1395f347c284SJohnny Huang 	int data_dw;
1396f347c284SJohnny Huang 	u32 data[2048];
1397f347c284SJohnny Huang 	u32 *buf;
1398f347c284SJohnny Huang 	u32 *buf_ignore;
1399f347c284SJohnny Huang 	u32 data_masked;
1400f347c284SJohnny Huang 	u32 buf_masked;
1401f347c284SJohnny Huang 
1402f347c284SJohnny Huang 	buf = (u32 *)image_layout->data;
1403f347c284SJohnny Huang 	buf_ignore = (u32 *)image_layout->data_ignore;
1404f347c284SJohnny Huang 
1405f347c284SJohnny Huang 	data_dw = image_layout->data_length / 4;
1406f347c284SJohnny Huang 
1407f347c284SJohnny Huang 	printf("Read OTP Data:\n");
1408f347c284SJohnny Huang 
1409f347c284SJohnny Huang 	for (i = 0; i < data_dw - 2 ; i += 2)
1410f347c284SJohnny Huang 		otp_read_data(i, &data[i]);
1411f347c284SJohnny Huang 
1412f347c284SJohnny Huang 	printf("Check writable...\n");
1413f347c284SJohnny Huang 	// ignore last two dw, the last two dw is used for slt otp write check.
1414f347c284SJohnny Huang 	for (i = 0; i < data_dw - 2; i++) {
1415f347c284SJohnny Huang 		data_masked = data[i]  & ~buf_ignore[i];
1416f347c284SJohnny Huang 		buf_masked  = buf[i] & ~buf_ignore[i];
1417f347c284SJohnny Huang 		if (data_masked == buf_masked)
1418f347c284SJohnny Huang 			continue;
1419f347c284SJohnny Huang 		if (i % 2 == 0) {
1420f347c284SJohnny Huang 			if ((data_masked | buf_masked) == buf_masked) {
1421f347c284SJohnny Huang 				continue;
1422f347c284SJohnny Huang 			} else {
1423f347c284SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
1424f347c284SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
1425f347c284SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
1426f347c284SJohnny Huang 				printf("Mask    [%x] = %x\n", i, ~buf_ignore[i]);
1427f347c284SJohnny Huang 				return OTP_FAILURE;
1428f347c284SJohnny Huang 			}
1429f347c284SJohnny Huang 		} else {
1430f347c284SJohnny Huang 			if ((data_masked & buf_masked) == buf_masked) {
1431f347c284SJohnny Huang 				continue;
1432f347c284SJohnny Huang 			} else {
1433f347c284SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
1434f347c284SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
1435f347c284SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
1436f347c284SJohnny Huang 				printf("Mask    [%x] = %x\n", i, ~buf_ignore[i]);
1437f347c284SJohnny Huang 				return OTP_FAILURE;
1438f347c284SJohnny Huang 			}
1439f347c284SJohnny Huang 		}
1440f347c284SJohnny Huang 	}
1441f347c284SJohnny Huang 
1442f347c284SJohnny Huang 	printf("Start Programing...\n");
1443f347c284SJohnny Huang 
1444f347c284SJohnny Huang 	// programing ecc region first
1445f347c284SJohnny Huang 	for (i = 1792; i < 2046; i += 2) {
1446f347c284SJohnny Huang 		ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i);
1447f347c284SJohnny Huang 		if (ret != OTP_SUCCESS) {
1448f347c284SJohnny Huang 			printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n",
1449f347c284SJohnny Huang 			       i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]);
1450f347c284SJohnny Huang 			return ret;
1451f347c284SJohnny Huang 		}
1452f347c284SJohnny Huang 	}
1453f347c284SJohnny Huang 
1454f347c284SJohnny Huang 	for (i = 0; i < 1792; i += 2) {
1455f347c284SJohnny Huang 		ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i);
1456f347c284SJohnny Huang 		if (ret != OTP_SUCCESS) {
1457f347c284SJohnny Huang 			printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n",
1458f347c284SJohnny Huang 			       i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]);
1459f347c284SJohnny Huang 			return ret;
1460f347c284SJohnny Huang 		}
1461f347c284SJohnny Huang 	}
1462f347c284SJohnny Huang 	otp_soak(0);
1463f347c284SJohnny Huang 	return OTP_SUCCESS;
1464f347c284SJohnny Huang }
1465f347c284SJohnny Huang 
1466f347c284SJohnny Huang static int otp_prog_strap(struct otp_image_layout *image_layout)
1467f347c284SJohnny Huang {
1468f347c284SJohnny Huang 	u32 *strap;
1469f347c284SJohnny Huang 	u32 *strap_ignore;
1470f347c284SJohnny Huang 	u32 *strap_pro;
1471f347c284SJohnny Huang 	u32 *strap_reg_protect;
1472f347c284SJohnny Huang 	u32 prog_address;
1473f347c284SJohnny Huang 	int i;
1474f347c284SJohnny Huang 	int bit, pbit, ibit, offset, rpbit;
1475f347c284SJohnny Huang 	int fail = 0;
1476f347c284SJohnny Huang 	int ret;
1477f347c284SJohnny Huang 	int prog_flag = 0;
1478f347c284SJohnny Huang 	struct otpstrap_status otpstrap[64];
1479f347c284SJohnny Huang 
1480f347c284SJohnny Huang 	strap = (u32 *)image_layout->strap;
1481f347c284SJohnny Huang 	strap_pro = (u32 *)image_layout->strap_pro;
1482f347c284SJohnny Huang 	strap_ignore = (u32 *)image_layout->strap_ignore;
1483f347c284SJohnny Huang 	strap_reg_protect = (u32 *)image_layout->strap_reg_pro;
1484f347c284SJohnny Huang 
1485f347c284SJohnny Huang 	printf("Read OTP Strap Region:\n");
1486f347c284SJohnny Huang 	otp_strap_status(otpstrap);
1487f347c284SJohnny Huang 
1488f347c284SJohnny Huang 	printf("Check writable...\n");
1489f347c284SJohnny Huang 	if (otp_strap_image_confirm(image_layout) == OTP_FAILURE) {
1490f347c284SJohnny Huang 		printf("Input image can't program into OTP, please check.\n");
1491f347c284SJohnny Huang 		return OTP_FAILURE;
1492f347c284SJohnny Huang 	}
1493f347c284SJohnny Huang 
1494f347c284SJohnny Huang 	for (i = 0; i < 64; i++) {
1495f347c284SJohnny Huang 		prog_address = 0x800;
1496f347c284SJohnny Huang 		if (i < 32) {
1497f347c284SJohnny Huang 			offset = i;
1498f347c284SJohnny Huang 			bit = (strap[0] >> offset) & 0x1;
1499f347c284SJohnny Huang 			ibit = (strap_ignore[0] >> offset) & 0x1;
1500f347c284SJohnny Huang 			pbit = (strap_pro[0] >> offset) & 0x1;
1501f347c284SJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200;
1502f347c284SJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2;
1503f347c284SJohnny Huang 
1504f347c284SJohnny Huang 		} else {
1505f347c284SJohnny Huang 			offset = (i - 32);
1506f347c284SJohnny Huang 			bit = (strap[1] >> offset) & 0x1;
1507f347c284SJohnny Huang 			ibit = (strap_ignore[1] >> offset) & 0x1;
1508f347c284SJohnny Huang 			pbit = (strap_pro[1] >> offset) & 0x1;
1509f347c284SJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200;
1510f347c284SJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2;
1511f347c284SJohnny Huang 		}
1512f347c284SJohnny Huang 		if (info_cb.version != OTP_A0) {
1513f347c284SJohnny Huang 			if (i < 32)
1514f347c284SJohnny Huang 				rpbit = (strap_reg_protect[0] >> i) & 0x1;
1515f347c284SJohnny Huang 			else
1516f347c284SJohnny Huang 				rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1;
1517f347c284SJohnny Huang 		} else {
1518f347c284SJohnny Huang 			rpbit = 0;
1519f347c284SJohnny Huang 		}
1520f347c284SJohnny Huang 
1521f347c284SJohnny Huang 		if (ibit == 1)
1522f347c284SJohnny Huang 			continue;
1523f347c284SJohnny Huang 		if (bit == otpstrap[i].value)
1524f347c284SJohnny Huang 			prog_flag = 0;
1525f347c284SJohnny Huang 		else
1526f347c284SJohnny Huang 			prog_flag = 1;
1527f347c284SJohnny Huang 
1528f347c284SJohnny Huang 		if (otpstrap[i].protected == 1 && prog_flag) {
1529f347c284SJohnny Huang 			fail = 1;
1530f347c284SJohnny Huang 			continue;
1531f347c284SJohnny Huang 		}
1532f347c284SJohnny Huang 		if (otpstrap[i].remain_times == 0 && prog_flag) {
1533f347c284SJohnny Huang 			fail = 1;
1534f347c284SJohnny Huang 			continue;
1535f347c284SJohnny Huang 		}
1536f347c284SJohnny Huang 
1537f347c284SJohnny Huang 		if (prog_flag) {
1538f347c284SJohnny Huang 			ret = otp_prog_dc_b(1, prog_address, offset);
1539f347c284SJohnny Huang 			if (ret)
1540f347c284SJohnny Huang 				return OTP_FAILURE;
1541f347c284SJohnny Huang 		}
1542f347c284SJohnny Huang 
1543f347c284SJohnny Huang 		if (rpbit == 1 && info_cb.version != OTP_A0) {
1544f347c284SJohnny Huang 			prog_address = 0x800;
1545f347c284SJohnny Huang 			if (i < 32)
1546f347c284SJohnny Huang 				prog_address |= 0x608;
1547f347c284SJohnny Huang 			else
1548f347c284SJohnny Huang 				prog_address |= 0x60a;
1549f347c284SJohnny Huang 
1550f347c284SJohnny Huang 			ret = otp_prog_dc_b(1, prog_address, offset);
1551f347c284SJohnny Huang 			if (ret)
1552f347c284SJohnny Huang 				return OTP_FAILURE;
1553f347c284SJohnny Huang 		}
1554f347c284SJohnny Huang 
1555f347c284SJohnny Huang 		if (pbit != 0) {
1556f347c284SJohnny Huang 			prog_address = 0x800;
1557f347c284SJohnny Huang 			if (i < 32)
1558f347c284SJohnny Huang 				prog_address |= 0x60c;
1559f347c284SJohnny Huang 			else
1560f347c284SJohnny Huang 				prog_address |= 0x60e;
1561f347c284SJohnny Huang 
1562f347c284SJohnny Huang 			ret = otp_prog_dc_b(1, prog_address, offset);
1563f347c284SJohnny Huang 			if (ret)
1564f347c284SJohnny Huang 				return OTP_FAILURE;
1565f347c284SJohnny Huang 		}
1566f347c284SJohnny Huang 	}
1567f347c284SJohnny Huang 	otp_soak(0);
1568f347c284SJohnny Huang 	if (fail == 1)
1569f347c284SJohnny Huang 		return OTP_FAILURE;
1570f347c284SJohnny Huang 	return OTP_SUCCESS;
157169d5fd8fSJohnny Huang }
157269d5fd8fSJohnny Huang 
15735010032bSJohnny Huang static int otp_prog_conf(struct otp_image_layout *image_layout)
157469d5fd8fSJohnny Huang {
1575a6d0d645SJohnny Huang 	int i, k;
1576d90825e2SJohnny Huang 	int pass = 0;
1577a219f6deSJohnny Huang 	u32 prog_address;
1578a219f6deSJohnny Huang 	u32 data[16];
1579a219f6deSJohnny Huang 	u32 compare[2];
1580a219f6deSJohnny Huang 	u32 *conf = (u32 *)image_layout->conf;
1581a219f6deSJohnny Huang 	u32 *conf_ignore = (u32 *)image_layout->conf_ignore;
1582a219f6deSJohnny Huang 	u32 data_masked;
1583a219f6deSJohnny Huang 	u32 buf_masked;
158469d5fd8fSJohnny Huang 
1585a6d0d645SJohnny Huang 	printf("Read OTP Config Region:\n");
1586a6d0d645SJohnny Huang 
1587bb34a7bfSJohnny Huang 	for (i = 0; i < 16 ; i++) {
158869d5fd8fSJohnny Huang 		prog_address = 0x800;
1589a6d0d645SJohnny Huang 		prog_address |= (i / 8) * 0x200;
1590a6d0d645SJohnny Huang 		prog_address |= (i % 8) * 0x2;
1591a6d0d645SJohnny Huang 		otp_read_data(prog_address, &data[i]);
1592a6d0d645SJohnny Huang 	}
1593a6d0d645SJohnny Huang 
1594a6d0d645SJohnny Huang 	printf("Check writable...\n");
1595bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++) {
15965010032bSJohnny Huang 		data_masked = data[i]  & ~conf_ignore[i];
15975010032bSJohnny Huang 		buf_masked  = conf[i] & ~conf_ignore[i];
1598d90825e2SJohnny Huang 		if (data_masked == buf_masked)
159969d5fd8fSJohnny Huang 			continue;
1600d90825e2SJohnny Huang 		if ((data_masked | buf_masked) == buf_masked) {
1601a6d0d645SJohnny Huang 			continue;
1602a6d0d645SJohnny Huang 		} else {
1603a6d0d645SJohnny Huang 			printf("Input image can't program into OTP, please check.\n");
1604a6af4a17SJohnny Huang 			printf("OTPCFG[%X] = %x\n", i, data[i]);
16055010032bSJohnny Huang 			printf("Input [%X] = %x\n", i, conf[i]);
16065010032bSJohnny Huang 			printf("Mask  [%X] = %x\n", i, ~conf_ignore[i]);
16072a856b9aSJohnny Huang 			return OTP_FAILURE;
1608a6d0d645SJohnny Huang 		}
1609a6d0d645SJohnny Huang 	}
1610a6d0d645SJohnny Huang 
1611a6d0d645SJohnny Huang 	printf("Start Programing...\n");
1612d90825e2SJohnny Huang 	otp_soak(0);
1613bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++) {
16145010032bSJohnny Huang 		data_masked = data[i]  & ~conf_ignore[i];
16155010032bSJohnny Huang 		buf_masked  = conf[i] & ~conf_ignore[i];
1616a6d0d645SJohnny Huang 		prog_address = 0x800;
1617a6d0d645SJohnny Huang 		prog_address |= (i / 8) * 0x200;
1618a6d0d645SJohnny Huang 		prog_address |= (i % 8) * 0x2;
1619bb34a7bfSJohnny Huang 		if (data_masked == buf_masked) {
1620bb34a7bfSJohnny Huang 			pass = 1;
1621a6d0d645SJohnny Huang 			continue;
1622bb34a7bfSJohnny Huang 		}
1623de6fbf1cSJohnny Huang 
1624de6fbf1cSJohnny Huang 		otp_soak(1);
16255010032bSJohnny Huang 		otp_prog_dw(conf[i], conf_ignore[i], prog_address);
1626a6d0d645SJohnny Huang 
162769d5fd8fSJohnny Huang 		pass = 0;
162869d5fd8fSJohnny Huang 		for (k = 0; k < RETRY; k++) {
16295010032bSJohnny Huang 			if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) {
1630de6fbf1cSJohnny Huang 				otp_soak(2);
1631feea3fdfSJohnny Huang 				otp_prog_dw(compare[0], conf_ignore[i], prog_address);
16325010032bSJohnny Huang 				if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) {
1633de6fbf1cSJohnny Huang 					otp_soak(1);
1634de6fbf1cSJohnny Huang 				} else {
1635de6fbf1cSJohnny Huang 					pass = 1;
1636de6fbf1cSJohnny Huang 					break;
1637de6fbf1cSJohnny Huang 				}
1638a6d0d645SJohnny Huang 			} else {
163969d5fd8fSJohnny Huang 				pass = 1;
164069d5fd8fSJohnny Huang 				break;
164169d5fd8fSJohnny Huang 			}
164269d5fd8fSJohnny Huang 		}
1643bb34a7bfSJohnny Huang 		if (pass == 0) {
1644bb34a7bfSJohnny Huang 			printf("address: %08x, data: %08x, buffer: %08x, mask: %08x\n",
16455010032bSJohnny Huang 			       i, data[i], conf[i], conf_ignore[i]);
1646bb34a7bfSJohnny Huang 			break;
1647bb34a7bfSJohnny Huang 		}
1648a6d0d645SJohnny Huang 	}
1649a6d0d645SJohnny Huang 
1650de6fbf1cSJohnny Huang 	otp_soak(0);
165169d5fd8fSJohnny Huang 	if (!pass)
16522a856b9aSJohnny Huang 		return OTP_FAILURE;
1653a6d0d645SJohnny Huang 
16542a856b9aSJohnny Huang 	return OTP_SUCCESS;
165569d5fd8fSJohnny Huang }
165669d5fd8fSJohnny Huang 
1657f347c284SJohnny Huang static int otp_verify_image(u8 *src_buf, u32 length, u8 *digest_buf)
1658696656c6SJohnny Huang {
1659696656c6SJohnny Huang 	sha256_context ctx;
1660696656c6SJohnny Huang 	u8 digest_ret[CHECKSUM_LEN];
1661696656c6SJohnny Huang 
1662696656c6SJohnny Huang 	sha256_starts(&ctx);
1663696656c6SJohnny Huang 	sha256_update(&ctx, src_buf, length);
1664696656c6SJohnny Huang 	sha256_finish(&ctx, digest_ret);
1665696656c6SJohnny Huang 
1666696656c6SJohnny Huang 	if (!memcmp(digest_buf, digest_ret, CHECKSUM_LEN))
1667f347c284SJohnny Huang 		return OTP_SUCCESS;
1668f347c284SJohnny Huang 	return OTP_FAILURE;
1669696656c6SJohnny Huang }
1670696656c6SJohnny Huang 
1671f347c284SJohnny Huang static int otp_prog_image(int addr, int nconfirm)
167269d5fd8fSJohnny Huang {
167369d5fd8fSJohnny Huang 	int ret;
1674*61a6cda7SJohnny Huang 	int image_soc_ver = 0;
1675696656c6SJohnny Huang 	struct otp_header *otp_header;
1676696656c6SJohnny Huang 	struct otp_image_layout image_layout;
1677696656c6SJohnny Huang 	int image_size;
1678a219f6deSJohnny Huang 	u8 *buf;
1679a219f6deSJohnny Huang 	u8 *checksum;
168069d5fd8fSJohnny Huang 
1681696656c6SJohnny Huang 	otp_header = map_physmem(addr, sizeof(struct otp_header), MAP_WRBACK);
1682696656c6SJohnny Huang 	if (!otp_header) {
168369d5fd8fSJohnny Huang 		puts("Failed to map physical memory\n");
16842a856b9aSJohnny Huang 		return OTP_FAILURE;
168569d5fd8fSJohnny Huang 	}
1686d90825e2SJohnny Huang 
1687696656c6SJohnny Huang 	image_size = OTP_IMAGE_SIZE(otp_header->image_info);
1688696656c6SJohnny Huang 	unmap_physmem(otp_header, MAP_WRBACK);
1689696656c6SJohnny Huang 
1690696656c6SJohnny Huang 	buf = map_physmem(addr, image_size + CHECKSUM_LEN, MAP_WRBACK);
1691696656c6SJohnny Huang 
1692696656c6SJohnny Huang 	if (!buf) {
1693696656c6SJohnny Huang 		puts("Failed to map physical memory\n");
1694696656c6SJohnny Huang 		return OTP_FAILURE;
1695696656c6SJohnny Huang 	}
1696696656c6SJohnny Huang 	otp_header = (struct otp_header *)buf;
1697696656c6SJohnny Huang 	checksum = buf + otp_header->checksum_offset;
1698696656c6SJohnny Huang 
1699696656c6SJohnny Huang 	if (strcmp(OTP_MAGIC, (char *)otp_header->otp_magic) != 0) {
1700696656c6SJohnny Huang 		puts("Image is invalid\n");
1701696656c6SJohnny Huang 		return OTP_FAILURE;
1702696656c6SJohnny Huang 	}
1703696656c6SJohnny Huang 
17045010032bSJohnny Huang 	image_layout.data_length = (int)(OTP_REGION_SIZE(otp_header->data_info) / 2);
17055010032bSJohnny Huang 	image_layout.data = buf + OTP_REGION_OFFSET(otp_header->data_info);
17065010032bSJohnny Huang 	image_layout.data_ignore = image_layout.data + image_layout.data_length;
17075010032bSJohnny Huang 
17085010032bSJohnny Huang 	image_layout.conf_length = (int)(OTP_REGION_SIZE(otp_header->config_info) / 2);
1709696656c6SJohnny Huang 	image_layout.conf = buf + OTP_REGION_OFFSET(otp_header->config_info);
17105010032bSJohnny Huang 	image_layout.conf_ignore = image_layout.conf + image_layout.conf_length;
1711696656c6SJohnny Huang 
1712696656c6SJohnny Huang 	image_layout.strap = buf + OTP_REGION_OFFSET(otp_header->strap_info);
1713*61a6cda7SJohnny Huang 	if (otp_header->soc_ver == SOC_AST2600A0) {
1714*61a6cda7SJohnny Huang 		image_soc_ver = OTP_A0;
17155010032bSJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 3);
17165010032bSJohnny Huang 		image_layout.strap_pro = image_layout.strap + image_layout.strap_length;
17175010032bSJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 2 * image_layout.strap_length;
1718*61a6cda7SJohnny Huang 	} else if (otp_header->soc_ver == SOC_AST2600A1) {
1719*61a6cda7SJohnny Huang 		image_soc_ver = OTP_A1;
17205010032bSJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4);
17215010032bSJohnny Huang 		image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length;
17225010032bSJohnny Huang 		image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length;
17235010032bSJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length;
1724*61a6cda7SJohnny Huang 	} else if (otp_header->soc_ver == SOC_AST2600A2) {
1725*61a6cda7SJohnny Huang 		image_soc_ver = OTP_A2;
17265fdde29fSJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4);
17275fdde29fSJohnny Huang 		image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length;
17285fdde29fSJohnny Huang 		image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length;
17295fdde29fSJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length;
1730*61a6cda7SJohnny Huang 	} else if (otp_header->soc_ver == SOC_AST2600A3) {
1731*61a6cda7SJohnny Huang 		image_soc_ver = OTP_A3;
173264b66712SJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4);
173364b66712SJohnny Huang 		image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length;
173464b66712SJohnny Huang 		image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length;
173564b66712SJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length;
1736696656c6SJohnny Huang 	} else {
1737*61a6cda7SJohnny Huang 		puts("Image SOC Version is not supported\n");
1738696656c6SJohnny Huang 		return OTP_FAILURE;
1739696656c6SJohnny Huang 	}
1740696656c6SJohnny Huang 
1741*61a6cda7SJohnny Huang 	if (image_soc_ver != info_cb.version) {
17429a4fe690SJohnny Huang 		puts("Version is not match\n");
17439a4fe690SJohnny Huang 		return OTP_FAILURE;
17449a4fe690SJohnny Huang 	}
17459a4fe690SJohnny Huang 
1746*61a6cda7SJohnny Huang 	if (otp_header->otptool_ver != OTPTOOL_VERSION(1, 0, 0)) {
1747*61a6cda7SJohnny Huang 		puts("OTP image is not generated by otptool v1.0.0\n");
1748*61a6cda7SJohnny Huang 		return OTP_FAILURE;
1749*61a6cda7SJohnny Huang 	}
1750*61a6cda7SJohnny Huang 
1751f347c284SJohnny Huang 	if (otp_verify_image(buf, image_size, checksum)) {
1752696656c6SJohnny Huang 		puts("checksum is invalid\n");
1753696656c6SJohnny Huang 		return OTP_FAILURE;
1754d90825e2SJohnny Huang 	}
17557332532cSJohnny Huang 
175669d5fd8fSJohnny Huang 	if (!nconfirm) {
1757696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_DATA) {
17587f795e57SJohnny Huang 			printf("\nOTP data region :\n");
1759f347c284SJohnny Huang 			if (otp_print_data_image(&image_layout) < 0) {
176069d5fd8fSJohnny Huang 				printf("OTP data error, please check.\n");
17612a856b9aSJohnny Huang 				return OTP_FAILURE;
176269d5fd8fSJohnny Huang 			}
176369d5fd8fSJohnny Huang 		}
1764696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_CONFIG) {
17657332532cSJohnny Huang 			printf("\nOTP configuration region :\n");
1766696656c6SJohnny Huang 			if (otp_print_conf_image(&image_layout) < 0) {
17677332532cSJohnny Huang 				printf("OTP config error, please check.\n");
17687332532cSJohnny Huang 				return OTP_FAILURE;
17697332532cSJohnny Huang 			}
17707332532cSJohnny Huang 		}
17717adec5f6SJohnny Huang 		if (otp_header->image_info & OTP_INC_STRAP) {
17727adec5f6SJohnny Huang 			printf("\nOTP strap region :\n");
17737adec5f6SJohnny Huang 			if (otp_print_strap_image(&image_layout) < 0) {
17747adec5f6SJohnny Huang 				printf("OTP strap error, please check.\n");
17757adec5f6SJohnny Huang 				return OTP_FAILURE;
17767adec5f6SJohnny Huang 			}
17777adec5f6SJohnny Huang 		}
17787332532cSJohnny Huang 
177969d5fd8fSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
178069d5fd8fSJohnny Huang 		if (!confirm_yesno()) {
178169d5fd8fSJohnny Huang 			printf(" Aborting\n");
17822a856b9aSJohnny Huang 			return OTP_FAILURE;
178369d5fd8fSJohnny Huang 		}
178469d5fd8fSJohnny Huang 	}
17857332532cSJohnny Huang 
17865010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_DATA) {
17875010032bSJohnny Huang 		printf("programing data region ...\n");
17885010032bSJohnny Huang 		ret = otp_prog_data(&image_layout);
17895010032bSJohnny Huang 		if (ret != 0) {
17905010032bSJohnny Huang 			printf("Error\n");
17915010032bSJohnny Huang 			return ret;
17925010032bSJohnny Huang 		}
1793a219f6deSJohnny Huang 		printf("Done\n");
17945010032bSJohnny Huang 	}
17955010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_STRAP) {
17965010032bSJohnny Huang 		printf("programing strap region ...\n");
17975010032bSJohnny Huang 		ret = otp_prog_strap(&image_layout);
17985010032bSJohnny Huang 		if (ret != 0) {
17995010032bSJohnny Huang 			printf("Error\n");
18005010032bSJohnny Huang 			return ret;
18015010032bSJohnny Huang 		}
1802a219f6deSJohnny Huang 		printf("Done\n");
18035010032bSJohnny Huang 	}
18045010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_CONFIG) {
18055010032bSJohnny Huang 		printf("programing configuration region ...\n");
18065010032bSJohnny Huang 		ret = otp_prog_conf(&image_layout);
18075010032bSJohnny Huang 		if (ret != 0) {
18085010032bSJohnny Huang 			printf("Error\n");
18095010032bSJohnny Huang 			return ret;
18105010032bSJohnny Huang 		}
18115010032bSJohnny Huang 		printf("Done\n");
18125010032bSJohnny Huang 	}
1813cd1610b4SJohnny Huang 
18147332532cSJohnny Huang 	return OTP_SUCCESS;
18152a856b9aSJohnny Huang }
18162a856b9aSJohnny Huang 
1817f347c284SJohnny Huang static int otp_prog_bit(int mode, int otp_dw_offset, int bit_offset, int value, int nconfirm)
1818cd1610b4SJohnny Huang {
1819a219f6deSJohnny Huang 	u32 read[2];
1820a219f6deSJohnny Huang 	u32 prog_address = 0;
182166f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
1822cd1610b4SJohnny Huang 	int otp_bit;
182383655e91SJohnny Huang 	int ret = 0;
1824cd1610b4SJohnny Huang 
1825dacbba92SJohnny Huang 	otp_soak(0);
1826cd1610b4SJohnny Huang 	switch (mode) {
1827a6d0d645SJohnny Huang 	case OTP_REGION_CONF:
1828f347c284SJohnny Huang 		otp_read_conf(otp_dw_offset, read);
1829cd1610b4SJohnny Huang 		prog_address = 0x800;
1830cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset / 8) * 0x200;
1831cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset % 8) * 0x2;
1832a6af4a17SJohnny Huang 		otp_bit = (read[0] >> bit_offset) & 0x1;
1833cd1610b4SJohnny Huang 		if (otp_bit == value) {
1834a6af4a17SJohnny Huang 			printf("OTPCFG%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
1835cd1610b4SJohnny Huang 			printf("No need to program\n");
18362a856b9aSJohnny Huang 			return OTP_SUCCESS;
1837cd1610b4SJohnny Huang 		}
1838cd1610b4SJohnny Huang 		if (otp_bit == 1 && value == 0) {
1839a6af4a17SJohnny Huang 			printf("OTPCFG%X[%X] = 1\n", otp_dw_offset, bit_offset);
1840cd1610b4SJohnny Huang 			printf("OTP is programed, which can't be clean\n");
18412a856b9aSJohnny Huang 			return OTP_FAILURE;
1842cd1610b4SJohnny Huang 		}
1843a6af4a17SJohnny Huang 		printf("Program OTPCFG%X[%X] to 1\n", otp_dw_offset, bit_offset);
1844cd1610b4SJohnny Huang 		break;
1845a6d0d645SJohnny Huang 	case OTP_REGION_DATA:
1846cd1610b4SJohnny Huang 		prog_address = otp_dw_offset;
1847cd1610b4SJohnny Huang 
1848cd1610b4SJohnny Huang 		if (otp_dw_offset % 2 == 0) {
1849a6af4a17SJohnny Huang 			otp_read_data(otp_dw_offset, read);
1850a6af4a17SJohnny Huang 			otp_bit = (read[0] >> bit_offset) & 0x1;
1851643b9cfdSJohnny Huang 
1852643b9cfdSJohnny Huang 			if (otp_bit == 1 && value == 0) {
1853643b9cfdSJohnny Huang 				printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
1854643b9cfdSJohnny Huang 				printf("OTP is programed, which can't be cleaned\n");
1855643b9cfdSJohnny Huang 				return OTP_FAILURE;
1856643b9cfdSJohnny Huang 			}
1857cd1610b4SJohnny Huang 		} else {
1858a6af4a17SJohnny Huang 			otp_read_data(otp_dw_offset - 1, read);
1859a6af4a17SJohnny Huang 			otp_bit = (read[1] >> bit_offset) & 0x1;
1860643b9cfdSJohnny Huang 
1861643b9cfdSJohnny Huang 			if (otp_bit == 0 && value == 1) {
1862643b9cfdSJohnny Huang 				printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
1863643b9cfdSJohnny Huang 				printf("OTP is programed, which can't be writen\n");
1864643b9cfdSJohnny Huang 				return OTP_FAILURE;
1865643b9cfdSJohnny Huang 			}
1866cd1610b4SJohnny Huang 		}
1867cd1610b4SJohnny Huang 		if (otp_bit == value) {
1868a6af4a17SJohnny Huang 			printf("OTPDATA%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
1869cd1610b4SJohnny Huang 			printf("No need to program\n");
18702a856b9aSJohnny Huang 			return OTP_SUCCESS;
1871cd1610b4SJohnny Huang 		}
1872643b9cfdSJohnny Huang 
1873a6af4a17SJohnny Huang 		printf("Program OTPDATA%X[%X] to 1\n", otp_dw_offset, bit_offset);
1874cd1610b4SJohnny Huang 		break;
1875a6d0d645SJohnny Huang 	case OTP_REGION_STRAP:
18768848d5dcSJohnny Huang 		otp_strap_status(otpstrap);
18778848d5dcSJohnny Huang 		otp_print_strap(bit_offset, 1);
18788848d5dcSJohnny Huang 		ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0, 0);
18798848d5dcSJohnny Huang 		if (ret == OTP_FAILURE)
18808848d5dcSJohnny Huang 			return OTP_FAILURE;
18818848d5dcSJohnny Huang 		else if (ret == OTP_PROG_SKIP)
18828848d5dcSJohnny Huang 			return OTP_SUCCESS;
1883a6af4a17SJohnny Huang 
1884cd1610b4SJohnny Huang 		break;
1885cd1610b4SJohnny Huang 	}
1886cd1610b4SJohnny Huang 
1887cd1610b4SJohnny Huang 	if (!nconfirm) {
1888cd1610b4SJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
1889cd1610b4SJohnny Huang 		if (!confirm_yesno()) {
1890cd1610b4SJohnny Huang 			printf(" Aborting\n");
18912a856b9aSJohnny Huang 			return OTP_FAILURE;
1892cd1610b4SJohnny Huang 		}
1893cd1610b4SJohnny Huang 	}
1894cd1610b4SJohnny Huang 
1895cd1610b4SJohnny Huang 	switch (mode) {
1896a6d0d645SJohnny Huang 	case OTP_REGION_STRAP:
1897f347c284SJohnny Huang 		ret =  otp_prog_strap_b(bit_offset, value);
189883655e91SJohnny Huang 		break;
1899a6d0d645SJohnny Huang 	case OTP_REGION_CONF:
1900a6d0d645SJohnny Huang 	case OTP_REGION_DATA:
1901f347c284SJohnny Huang 		ret = otp_prog_dc_b(value, prog_address, bit_offset);
1902de6fbf1cSJohnny Huang 		break;
1903de6fbf1cSJohnny Huang 	}
1904de6fbf1cSJohnny Huang 	otp_soak(0);
190583655e91SJohnny Huang 	if (ret) {
1906794e27ecSJohnny Huang 		printf("OTP cannot be programed\n");
1907794e27ecSJohnny Huang 		printf("FAILURE\n");
1908794e27ecSJohnny Huang 		return OTP_FAILURE;
1909794e27ecSJohnny Huang 	}
1910794e27ecSJohnny Huang 
19119009c25dSJohnny Huang 	printf("SUCCESS\n");
19122a856b9aSJohnny Huang 	return OTP_SUCCESS;
1913a219f6deSJohnny Huang }
1914a219f6deSJohnny Huang 
1915794e27ecSJohnny Huang static int otp_update_rid(u32 update_num, int force)
1916794e27ecSJohnny Huang {
1917794e27ecSJohnny Huang 	u32 otp_rid[2];
1918a8789b47SJohnny Huang 	u32 sw_rid[2];
1919794e27ecSJohnny Huang 	int rid_num = 0;
1920a8789b47SJohnny Huang 	int sw_rid_num = 0;
1921794e27ecSJohnny Huang 	int bit_offset;
1922794e27ecSJohnny Huang 	int dw_offset;
1923794e27ecSJohnny Huang 	int i;
1924794e27ecSJohnny Huang 	int ret;
1925794e27ecSJohnny Huang 
1926f347c284SJohnny Huang 	otp_read_conf(10, &otp_rid[0]);
1927f347c284SJohnny Huang 	otp_read_conf(11, &otp_rid[1]);
1928794e27ecSJohnny Huang 
1929a8789b47SJohnny Huang 	sw_rid[0] = readl(SW_REV_ID0);
1930a8789b47SJohnny Huang 	sw_rid[1] = readl(SW_REV_ID1);
1931a8789b47SJohnny Huang 
1932794e27ecSJohnny Huang 	rid_num = get_rid_num(otp_rid);
1933a8789b47SJohnny Huang 	sw_rid_num = get_rid_num(sw_rid);
1934a8789b47SJohnny Huang 
1935a8789b47SJohnny Huang 	if (sw_rid_num < 0) {
1936a8789b47SJohnny Huang 		printf("SW revision id is invalid, please check.\n");
1937a8789b47SJohnny Huang 		return OTP_FAILURE;
1938a8789b47SJohnny Huang 	}
1939a8789b47SJohnny Huang 
1940a8789b47SJohnny Huang 	if (update_num > sw_rid_num) {
1941a8789b47SJohnny Huang 		printf("current SW revision ID: 0x%x\n", sw_rid_num);
1942a8789b47SJohnny Huang 		printf("update number could not bigger than current SW revision id\n");
1943a8789b47SJohnny Huang 		return OTP_FAILURE;
1944a8789b47SJohnny Huang 	}
1945794e27ecSJohnny Huang 
1946794e27ecSJohnny Huang 	if (rid_num < 0) {
1947794e27ecSJohnny Huang 		printf("Currennt OTP revision ID cannot handle by this command,\n"
1948794e27ecSJohnny Huang 		       "plase use 'otp pb' command to update it manually\n");
1949794e27ecSJohnny Huang 		otp_print_revid(otp_rid);
19509009c25dSJohnny Huang 		return OTP_FAILURE;
19519009c25dSJohnny Huang 	}
1952cd1610b4SJohnny Huang 
1953794e27ecSJohnny Huang 	printf("current OTP revision ID: 0x%x\n", rid_num);
1954794e27ecSJohnny Huang 	otp_print_revid(otp_rid);
1955794e27ecSJohnny Huang 	printf("input update number: 0x%x\n", update_num);
1956794e27ecSJohnny Huang 
1957a8789b47SJohnny Huang 	if (rid_num > update_num) {
1958a8789b47SJohnny Huang 		printf("OTP rev_id is bigger than 0x%X\n", update_num);
1959a8789b47SJohnny Huang 		printf("Skip\n");
1960a8789b47SJohnny Huang 		return OTP_FAILURE;
1961a8789b47SJohnny Huang 	} else if (rid_num == update_num) {
1962a8789b47SJohnny Huang 		printf("OTP rev_id is same as input\n");
1963794e27ecSJohnny Huang 		printf("Skip\n");
1964794e27ecSJohnny Huang 		return OTP_FAILURE;
1965794e27ecSJohnny Huang 	}
1966794e27ecSJohnny Huang 
1967794e27ecSJohnny Huang 	for (i = rid_num; i < update_num; i++) {
1968794e27ecSJohnny Huang 		if (i < 32) {
1969794e27ecSJohnny Huang 			dw_offset = 0xa;
1970794e27ecSJohnny Huang 			bit_offset = i;
1971794e27ecSJohnny Huang 		} else {
1972794e27ecSJohnny Huang 			dw_offset = 0xb;
1973794e27ecSJohnny Huang 			bit_offset = i - 32;
1974794e27ecSJohnny Huang 		}
1975794e27ecSJohnny Huang 		printf("OTPCFG%X[%d]", dw_offset, bit_offset);
1976794e27ecSJohnny Huang 		if (i + 1 != update_num)
1977794e27ecSJohnny Huang 			printf(", ");
1978794e27ecSJohnny Huang 	}
1979794e27ecSJohnny Huang 
1980794e27ecSJohnny Huang 	printf(" will be programmed\n");
1981794e27ecSJohnny Huang 	if (force == 0) {
1982794e27ecSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
1983794e27ecSJohnny Huang 		if (!confirm_yesno()) {
1984794e27ecSJohnny Huang 			printf(" Aborting\n");
1985794e27ecSJohnny Huang 			return OTP_FAILURE;
1986794e27ecSJohnny Huang 		}
1987794e27ecSJohnny Huang 	}
1988794e27ecSJohnny Huang 
1989794e27ecSJohnny Huang 	ret = 0;
1990794e27ecSJohnny Huang 	for (i = rid_num; i < update_num; i++) {
1991794e27ecSJohnny Huang 		if (i < 32) {
1992794e27ecSJohnny Huang 			dw_offset = 0xa04;
1993794e27ecSJohnny Huang 			bit_offset = i;
1994794e27ecSJohnny Huang 		} else {
1995794e27ecSJohnny Huang 			dw_offset = 0xa06;
1996794e27ecSJohnny Huang 			bit_offset = i - 32;
1997794e27ecSJohnny Huang 		}
1998f347c284SJohnny Huang 		if (otp_prog_dc_b(1, dw_offset, bit_offset)) {
1999794e27ecSJohnny Huang 			printf("OTPCFG%X[%d] programming failed\n", dw_offset, bit_offset);
2000794e27ecSJohnny Huang 			ret = OTP_FAILURE;
2001794e27ecSJohnny Huang 			break;
2002794e27ecSJohnny Huang 		}
2003794e27ecSJohnny Huang 	}
2004794e27ecSJohnny Huang 
2005f347c284SJohnny Huang 	otp_read_conf(10, &otp_rid[0]);
2006f347c284SJohnny Huang 	otp_read_conf(11, &otp_rid[1]);
2007794e27ecSJohnny Huang 	rid_num = get_rid_num(otp_rid);
2008794e27ecSJohnny Huang 	if (rid_num >= 0)
2009794e27ecSJohnny Huang 		printf("OTP revision ID: 0x%x\n", rid_num);
2010794e27ecSJohnny Huang 	else
2011794e27ecSJohnny Huang 		printf("OTP revision ID\n");
2012794e27ecSJohnny Huang 	otp_print_revid(otp_rid);
2013794e27ecSJohnny Huang 	if (!ret)
2014794e27ecSJohnny Huang 		printf("SUCCESS\n");
2015794e27ecSJohnny Huang 	else
2016794e27ecSJohnny Huang 		printf("FAILED\n");
2017794e27ecSJohnny Huang 	return ret;
2018794e27ecSJohnny Huang }
2019794e27ecSJohnny Huang 
20202a856b9aSJohnny Huang static int do_otpread(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
202169d5fd8fSJohnny Huang {
2022a219f6deSJohnny Huang 	u32 offset, count;
20232a856b9aSJohnny Huang 	int ret;
202469d5fd8fSJohnny Huang 
20252a856b9aSJohnny Huang 	if (argc == 4) {
20262a856b9aSJohnny Huang 		offset = simple_strtoul(argv[2], NULL, 16);
20272a856b9aSJohnny Huang 		count = simple_strtoul(argv[3], NULL, 16);
20282a856b9aSJohnny Huang 	} else if (argc == 3) {
20292a856b9aSJohnny Huang 		offset = simple_strtoul(argv[2], NULL, 16);
20302a856b9aSJohnny Huang 		count = 1;
20312a856b9aSJohnny Huang 	} else {
203269d5fd8fSJohnny Huang 		return CMD_RET_USAGE;
203369d5fd8fSJohnny Huang 	}
203469d5fd8fSJohnny Huang 
20352a856b9aSJohnny Huang 	if (!strcmp(argv[1], "conf")) {
20363d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2037f347c284SJohnny Huang 		ret = otp_print_conf(offset, count);
20382a856b9aSJohnny Huang 	} else if (!strcmp(argv[1], "data")) {
20393d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
20402a856b9aSJohnny Huang 		ret = otp_print_data(offset, count);
20412a856b9aSJohnny Huang 	} else if (!strcmp(argv[1], "strap")) {
20423d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
20432a856b9aSJohnny Huang 		ret = otp_print_strap(offset, count);
20442a856b9aSJohnny Huang 	} else {
20452a856b9aSJohnny Huang 		return CMD_RET_USAGE;
204669d5fd8fSJohnny Huang 	}
204769d5fd8fSJohnny Huang 
20482a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
20492a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
20502a856b9aSJohnny Huang 	return CMD_RET_USAGE;
20512a856b9aSJohnny Huang }
20522a856b9aSJohnny Huang 
20532a856b9aSJohnny Huang static int do_otpprog(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
20542a856b9aSJohnny Huang {
20552a856b9aSJohnny Huang 	phys_addr_t addr;
20562a856b9aSJohnny Huang 	int ret;
20572a856b9aSJohnny Huang 
2058de6b0cc4SJohnny Huang 	if (argc == 3) {
2059ed071a2bSJohnny Huang 		if (strcmp(argv[1], "o"))
20602a856b9aSJohnny Huang 			return CMD_RET_USAGE;
20612a856b9aSJohnny Huang 		addr = simple_strtoul(argv[2], NULL, 16);
20623d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2063f347c284SJohnny Huang 		ret = otp_prog_image(addr, 1);
2064de6b0cc4SJohnny Huang 	} else if (argc == 2) {
20652a856b9aSJohnny Huang 		addr = simple_strtoul(argv[1], NULL, 16);
20663d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2067f347c284SJohnny Huang 		ret = otp_prog_image(addr, 0);
20682a856b9aSJohnny Huang 	} else {
20692a856b9aSJohnny Huang 		return CMD_RET_USAGE;
20702a856b9aSJohnny Huang 	}
20712a856b9aSJohnny Huang 
20722a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
20732a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
20742a856b9aSJohnny Huang 	else if (ret == OTP_FAILURE)
20752a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
20762a856b9aSJohnny Huang 	else
20772a856b9aSJohnny Huang 		return CMD_RET_USAGE;
20782a856b9aSJohnny Huang }
20792a856b9aSJohnny Huang 
20802a856b9aSJohnny Huang static int do_otppb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
20812a856b9aSJohnny Huang {
20822a856b9aSJohnny Huang 	int mode = 0;
20832a856b9aSJohnny Huang 	int nconfirm = 0;
20842a856b9aSJohnny Huang 	int otp_addr = 0;
20852a856b9aSJohnny Huang 	int bit_offset;
20862a856b9aSJohnny Huang 	int value;
20872a856b9aSJohnny Huang 	int ret;
20882a856b9aSJohnny Huang 
20892a856b9aSJohnny Huang 	if (argc != 4 && argc != 5 && argc != 6)
20902a856b9aSJohnny Huang 		return CMD_RET_USAGE;
20912a856b9aSJohnny Huang 
20922a856b9aSJohnny Huang 	/* Drop the pb cmd */
20932a856b9aSJohnny Huang 	argc--;
20942a856b9aSJohnny Huang 	argv++;
20952a856b9aSJohnny Huang 
20962a856b9aSJohnny Huang 	if (!strcmp(argv[0], "conf"))
2097a6d0d645SJohnny Huang 		mode = OTP_REGION_CONF;
20982a856b9aSJohnny Huang 	else if (!strcmp(argv[0], "strap"))
2099a6d0d645SJohnny Huang 		mode = OTP_REGION_STRAP;
21002a856b9aSJohnny Huang 	else if (!strcmp(argv[0], "data"))
2101a6d0d645SJohnny Huang 		mode = OTP_REGION_DATA;
2102cd1610b4SJohnny Huang 	else
21032a856b9aSJohnny Huang 		return CMD_RET_USAGE;
21042a856b9aSJohnny Huang 
21052a856b9aSJohnny Huang 	/* Drop the region cmd */
21062a856b9aSJohnny Huang 	argc--;
21072a856b9aSJohnny Huang 	argv++;
21082a856b9aSJohnny Huang 
2109ed071a2bSJohnny Huang 	if (!strcmp(argv[0], "o")) {
2110cd1610b4SJohnny Huang 		nconfirm = 1;
21112a856b9aSJohnny Huang 		/* Drop the force option */
21122a856b9aSJohnny Huang 		argc--;
21132a856b9aSJohnny Huang 		argv++;
21142a856b9aSJohnny Huang 	}
2115cd1610b4SJohnny Huang 
2116a6d0d645SJohnny Huang 	if (mode == OTP_REGION_STRAP) {
21172a856b9aSJohnny Huang 		bit_offset = simple_strtoul(argv[0], NULL, 16);
21182a856b9aSJohnny Huang 		value = simple_strtoul(argv[1], NULL, 16);
21190808cc55SJohnny Huang 		if (bit_offset >= 64 || (value != 0 && value != 1))
21202a856b9aSJohnny Huang 			return CMD_RET_USAGE;
2121cd1610b4SJohnny Huang 	} else {
21222a856b9aSJohnny Huang 		otp_addr = simple_strtoul(argv[0], NULL, 16);
21232a856b9aSJohnny Huang 		bit_offset = simple_strtoul(argv[1], NULL, 16);
21242a856b9aSJohnny Huang 		value = simple_strtoul(argv[2], NULL, 16);
21250808cc55SJohnny Huang 		if (bit_offset >= 32 || (value != 0 && value != 1))
21262a856b9aSJohnny Huang 			return CMD_RET_USAGE;
21270808cc55SJohnny Huang 		if (mode == OTP_REGION_DATA) {
212878855207SJohnny Huang 			if (otp_addr >= 0x800)
21290808cc55SJohnny Huang 				return CMD_RET_USAGE;
21300808cc55SJohnny Huang 		} else {
213178855207SJohnny Huang 			if (otp_addr >= 0x20)
21320808cc55SJohnny Huang 				return CMD_RET_USAGE;
21330808cc55SJohnny Huang 		}
2134cd1610b4SJohnny Huang 	}
2135cd1610b4SJohnny Huang 	if (value != 0 && value != 1)
21362a856b9aSJohnny Huang 		return CMD_RET_USAGE;
2137cd1610b4SJohnny Huang 
21383d3688adSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2139f347c284SJohnny Huang 	ret = otp_prog_bit(mode, otp_addr, bit_offset, value, nconfirm);
21402a856b9aSJohnny Huang 
21412a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
21422a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
21432a856b9aSJohnny Huang 	else if (ret == OTP_FAILURE)
21442a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
21452a856b9aSJohnny Huang 	else
21462a856b9aSJohnny Huang 		return CMD_RET_USAGE;
21472a856b9aSJohnny Huang }
21482a856b9aSJohnny Huang 
21492a856b9aSJohnny Huang static int do_otpcmp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
21502a856b9aSJohnny Huang {
21512a856b9aSJohnny Huang 	phys_addr_t addr;
21522a856b9aSJohnny Huang 	int otp_addr = 0;
21532a856b9aSJohnny Huang 
21542a856b9aSJohnny Huang 	if (argc != 3)
21552a856b9aSJohnny Huang 		return CMD_RET_USAGE;
21562a856b9aSJohnny Huang 
21573d3688adSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
21582a856b9aSJohnny Huang 	addr = simple_strtoul(argv[1], NULL, 16);
21592a856b9aSJohnny Huang 	otp_addr = simple_strtoul(argv[2], NULL, 16);
21602a856b9aSJohnny Huang 	if (otp_compare(otp_addr, addr) == 0) {
216169d5fd8fSJohnny Huang 		printf("Compare pass\n");
21622a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
2163a219f6deSJohnny Huang 	}
216469d5fd8fSJohnny Huang 	printf("Compare fail\n");
21652a856b9aSJohnny Huang 	return CMD_RET_FAILURE;
216669d5fd8fSJohnny Huang }
216769d5fd8fSJohnny Huang 
216866f2f8e5SJohnny Huang static int do_otpinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
216966f2f8e5SJohnny Huang {
2170a8bd6d8cSJohnny Huang 	int view = 0;
21712d4b0742SJohnny Huang 	int input;
2172a8bd6d8cSJohnny Huang 
2173a8bd6d8cSJohnny Huang 	if (argc != 2 && argc != 3)
217466f2f8e5SJohnny Huang 		return CMD_RET_USAGE;
217566f2f8e5SJohnny Huang 
21762d4b0742SJohnny Huang 	if (!strcmp(argv[1], "conf")) {
21773d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
21782d4b0742SJohnny Huang 		if (argc == 3) {
21792d4b0742SJohnny Huang 			input = simple_strtoul(argv[2], NULL, 16);
21802d4b0742SJohnny Huang 			otp_print_conf_info(input);
21812d4b0742SJohnny Huang 		} else {
21822d4b0742SJohnny Huang 			otp_print_conf_info(-1);
21832d4b0742SJohnny Huang 		}
21842d4b0742SJohnny Huang 	} else if (!strcmp(argv[1], "strap")) {
21852d4b0742SJohnny Huang 		if (!strcmp(argv[2], "v")) {
2186a8bd6d8cSJohnny Huang 			view = 1;
2187a8bd6d8cSJohnny Huang 			/* Drop the view option */
2188a8bd6d8cSJohnny Huang 			argc--;
2189a8bd6d8cSJohnny Huang 			argv++;
2190a8bd6d8cSJohnny Huang 		}
21913d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2192b458cd62SJohnny Huang 		otp_print_strap_info(view);
219366f2f8e5SJohnny Huang 	} else {
219466f2f8e5SJohnny Huang 		return CMD_RET_USAGE;
219566f2f8e5SJohnny Huang 	}
21962d4b0742SJohnny Huang 
219766f2f8e5SJohnny Huang 	return CMD_RET_SUCCESS;
219866f2f8e5SJohnny Huang }
219966f2f8e5SJohnny Huang 
2200e14b073cSJohnny Huang static int _do_otpprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[], int preg)
2201737ed20bSJohnny Huang {
2202737ed20bSJohnny Huang 	int input;
2203737ed20bSJohnny Huang 	int bit_offset;
2204e14b073cSJohnny Huang 	u32 prog_address;
220583655e91SJohnny Huang 	int ret;
2206e14b073cSJohnny Huang 	char info[10];
2207e14b073cSJohnny Huang 
2208e14b073cSJohnny Huang 	if (preg) {
2209e14b073cSJohnny Huang 		sprintf(info, "register ");
2210e14b073cSJohnny Huang 		prog_address = 0xe08;
2211e14b073cSJohnny Huang 	} else {
2212e14b073cSJohnny Huang 		info[0] = 0;
2213e14b073cSJohnny Huang 		prog_address = 0xe0c;
2214e14b073cSJohnny Huang 	}
2215a219f6deSJohnny Huang 
2216737ed20bSJohnny Huang 	if (argc != 3 && argc != 2)
2217737ed20bSJohnny Huang 		return CMD_RET_USAGE;
2218737ed20bSJohnny Huang 
2219e14b073cSJohnny Huang 	if (!strcmp(argv[1], "o")) {
2220737ed20bSJohnny Huang 		input = simple_strtoul(argv[2], NULL, 16);
2221737ed20bSJohnny Huang 	} else {
2222737ed20bSJohnny Huang 		input = simple_strtoul(argv[1], NULL, 16);
2223e14b073cSJohnny Huang 		printf("OTPSTRAP[%d] %swill be protected\n", input, info);
2224737ed20bSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
2225737ed20bSJohnny Huang 		if (!confirm_yesno()) {
2226737ed20bSJohnny Huang 			printf(" Aborting\n");
2227737ed20bSJohnny Huang 			return CMD_RET_FAILURE;
2228737ed20bSJohnny Huang 		}
2229737ed20bSJohnny Huang 	}
2230737ed20bSJohnny Huang 
2231737ed20bSJohnny Huang 	if (input < 32) {
2232737ed20bSJohnny Huang 		bit_offset = input;
2233737ed20bSJohnny Huang 	} else if (input < 64) {
2234737ed20bSJohnny Huang 		bit_offset = input - 32;
2235e14b073cSJohnny Huang 		prog_address += 2;
2236737ed20bSJohnny Huang 	} else {
2237737ed20bSJohnny Huang 		return CMD_RET_USAGE;
2238737ed20bSJohnny Huang 	}
2239737ed20bSJohnny Huang 
2240e14b073cSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2241e14b073cSJohnny Huang 	if (verify_bit(prog_address, bit_offset, 1) == 0) {
2242e14b073cSJohnny Huang 		printf("OTPSTRAP[%d] %salready protected\n", input, info);
2243e14b073cSJohnny Huang 		return CMD_RET_SUCCESS;
2244e14b073cSJohnny Huang 	}
2245de6fbf1cSJohnny Huang 
2246f347c284SJohnny Huang 	ret = otp_prog_dc_b(1, prog_address, bit_offset);
2247de6fbf1cSJohnny Huang 	otp_soak(0);
224883655e91SJohnny Huang 
224983655e91SJohnny Huang 	if (ret) {
2250e14b073cSJohnny Huang 		printf("Protect OTPSTRAP[%d] %sfail\n", input, info);
2251737ed20bSJohnny Huang 		return CMD_RET_FAILURE;
2252737ed20bSJohnny Huang 	}
22539a4fe690SJohnny Huang 
2254794e27ecSJohnny Huang 	printf("OTPSTRAP[%d] %sis protected\n", input, info);
2255794e27ecSJohnny Huang 	return CMD_RET_SUCCESS;
2256794e27ecSJohnny Huang }
2257794e27ecSJohnny Huang 
2258e14b073cSJohnny Huang static int do_otpprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2259e14b073cSJohnny Huang {
2260e14b073cSJohnny Huang 	return _do_otpprotect(cmdtp, flag, argc, argv, 0);
2261e14b073cSJohnny Huang }
2262e14b073cSJohnny Huang 
2263e14b073cSJohnny Huang static int do_otprprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2264e14b073cSJohnny Huang {
2265e14b073cSJohnny Huang 	return _do_otpprotect(cmdtp, flag, argc, argv, 1);
2266e14b073cSJohnny Huang }
2267e14b073cSJohnny Huang 
2268f67375f7SJohnny Huang static int do_otpver(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2269f67375f7SJohnny Huang {
2270e417205bSJohnny Huang 	printf("SOC OTP version: %s\n", info_cb.ver_name);
2271f67375f7SJohnny Huang 	printf("OTP tool version: %s\n", OTP_VER);
2272f67375f7SJohnny Huang 	printf("OTP info version: %s\n", OTP_INFO_VER);
2273f67375f7SJohnny Huang 
2274f67375f7SJohnny Huang 	return CMD_RET_SUCCESS;
2275f67375f7SJohnny Huang }
2276f67375f7SJohnny Huang 
2277794e27ecSJohnny Huang static int do_otpupdate(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2278794e27ecSJohnny Huang {
2279794e27ecSJohnny Huang 	u32 update_num;
2280794e27ecSJohnny Huang 	int force = 0;
2281794e27ecSJohnny Huang 	int ret;
2282794e27ecSJohnny Huang 
2283794e27ecSJohnny Huang 	if (argc == 3) {
2284794e27ecSJohnny Huang 		if (strcmp(argv[1], "o"))
2285794e27ecSJohnny Huang 			return CMD_RET_USAGE;
2286794e27ecSJohnny Huang 		force = 1;
2287794e27ecSJohnny Huang 		update_num = simple_strtoul(argv[2], NULL, 16);
2288794e27ecSJohnny Huang 	} else if (argc == 2) {
2289794e27ecSJohnny Huang 		update_num = simple_strtoul(argv[1], NULL, 16);
2290794e27ecSJohnny Huang 	} else {
2291794e27ecSJohnny Huang 		return CMD_RET_USAGE;
2292794e27ecSJohnny Huang 	}
2293794e27ecSJohnny Huang 
2294794e27ecSJohnny Huang 	if (update_num > 64)
2295794e27ecSJohnny Huang 		return CMD_RET_USAGE;
2296794e27ecSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2297794e27ecSJohnny Huang 	ret = otp_update_rid(update_num, force);
2298794e27ecSJohnny Huang 	if (ret)
2299794e27ecSJohnny Huang 		return CMD_RET_FAILURE;
2300794e27ecSJohnny Huang 	return CMD_RET_SUCCESS;
2301794e27ecSJohnny Huang }
2302794e27ecSJohnny Huang 
2303794e27ecSJohnny Huang static int do_otprid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2304794e27ecSJohnny Huang {
2305794e27ecSJohnny Huang 	u32 otp_rid[2];
2306a8789b47SJohnny Huang 	u32 sw_rid[2];
2307794e27ecSJohnny Huang 	int rid_num = 0;
2308a8789b47SJohnny Huang 	int sw_rid_num = 0;
2309794e27ecSJohnny Huang 	int ret;
2310794e27ecSJohnny Huang 
2311794e27ecSJohnny Huang 	if (argc != 1)
2312794e27ecSJohnny Huang 		return CMD_RET_USAGE;
2313794e27ecSJohnny Huang 
2314794e27ecSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2315f347c284SJohnny Huang 	otp_read_conf(10, &otp_rid[0]);
2316f347c284SJohnny Huang 	otp_read_conf(11, &otp_rid[1]);
2317794e27ecSJohnny Huang 
2318a8789b47SJohnny Huang 	sw_rid[0] = readl(SW_REV_ID0);
2319a8789b47SJohnny Huang 	sw_rid[1] = readl(SW_REV_ID1);
2320794e27ecSJohnny Huang 
2321a8789b47SJohnny Huang 	rid_num = get_rid_num(otp_rid);
2322a8789b47SJohnny Huang 	sw_rid_num = get_rid_num(sw_rid);
2323a8789b47SJohnny Huang 
2324a8789b47SJohnny Huang 	printf("current SW revision ID: 0x%x\n", sw_rid_num);
2325794e27ecSJohnny Huang 	if (rid_num >= 0) {
2326794e27ecSJohnny Huang 		printf("current OTP revision ID: 0x%x\n", rid_num);
2327794e27ecSJohnny Huang 		ret = CMD_RET_SUCCESS;
2328794e27ecSJohnny Huang 	} else {
2329794e27ecSJohnny Huang 		printf("Currennt OTP revision ID cannot handle by 'otp update',\n"
2330794e27ecSJohnny Huang 		       "plase use 'otp pb' command to update it manually\n"
2331794e27ecSJohnny Huang 		       "current OTP revision ID\n");
2332794e27ecSJohnny Huang 		ret = CMD_RET_FAILURE;
2333794e27ecSJohnny Huang 	}
2334794e27ecSJohnny Huang 	otp_print_revid(otp_rid);
2335794e27ecSJohnny Huang 
2336794e27ecSJohnny Huang 	return ret;
2337794e27ecSJohnny Huang }
2338794e27ecSJohnny Huang 
23392a856b9aSJohnny Huang static cmd_tbl_t cmd_otp[] = {
2340f67375f7SJohnny Huang 	U_BOOT_CMD_MKENT(version, 1, 0, do_otpver, "", ""),
23412a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(read, 4, 0, do_otpread, "", ""),
2342a8bd6d8cSJohnny Huang 	U_BOOT_CMD_MKENT(info, 3, 0, do_otpinfo, "", ""),
2343de6b0cc4SJohnny Huang 	U_BOOT_CMD_MKENT(prog, 3, 0, do_otpprog, "", ""),
23442a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(pb, 6, 0, do_otppb, "", ""),
2345737ed20bSJohnny Huang 	U_BOOT_CMD_MKENT(protect, 3, 0, do_otpprotect, "", ""),
2346e14b073cSJohnny Huang 	U_BOOT_CMD_MKENT(rprotect, 3, 0, do_otprprotect, "", ""),
23472a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(cmp, 3, 0, do_otpcmp, "", ""),
2348794e27ecSJohnny Huang 	U_BOOT_CMD_MKENT(update, 3, 0, do_otpupdate, "", ""),
2349794e27ecSJohnny Huang 	U_BOOT_CMD_MKENT(rid, 1, 0, do_otprid, "", ""),
23502a856b9aSJohnny Huang };
23512a856b9aSJohnny Huang 
23522a856b9aSJohnny Huang static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
23532a856b9aSJohnny Huang {
23542a856b9aSJohnny Huang 	cmd_tbl_t *cp;
2355a219f6deSJohnny Huang 	u32 ver;
2356e14b073cSJohnny Huang 	int ret;
23572a856b9aSJohnny Huang 
23582a856b9aSJohnny Huang 	cp = find_cmd_tbl(argv[1], cmd_otp, ARRAY_SIZE(cmd_otp));
23592a856b9aSJohnny Huang 
2360737ed20bSJohnny Huang 	/* Drop the otp command */
23612a856b9aSJohnny Huang 	argc--;
23622a856b9aSJohnny Huang 	argv++;
23632a856b9aSJohnny Huang 
2364a219f6deSJohnny Huang 	if (!cp || argc > cp->maxargs)
23652a856b9aSJohnny Huang 		return CMD_RET_USAGE;
23662a856b9aSJohnny Huang 	if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
23672a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
23682a856b9aSJohnny Huang 
23690dae9d52SJohnny Huang 	ver = chip_version();
23700dae9d52SJohnny Huang 	switch (ver) {
2371e417205bSJohnny Huang 	case OTP_A0:
2372e417205bSJohnny Huang 		info_cb.version = OTP_A0;
23739a4fe690SJohnny Huang 		info_cb.conf_info = a0_conf_info;
23749a4fe690SJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a0_conf_info);
23759a4fe690SJohnny Huang 		info_cb.strap_info = a0_strap_info;
23769a4fe690SJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a0_strap_info);
23779a4fe690SJohnny Huang 		info_cb.key_info = a0_key_type;
23789a4fe690SJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a0_key_type);
2379e417205bSJohnny Huang 		sprintf(info_cb.ver_name, "A0");
23800dae9d52SJohnny Huang 		break;
2381e417205bSJohnny Huang 	case OTP_A1:
2382e417205bSJohnny Huang 		info_cb.version = OTP_A1;
23833cb28812SJohnny Huang 		info_cb.conf_info = a1_conf_info;
23843cb28812SJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a1_conf_info);
23853cb28812SJohnny Huang 		info_cb.strap_info = a1_strap_info;
23863cb28812SJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a1_strap_info);
23879a4fe690SJohnny Huang 		info_cb.key_info = a1_key_type;
23889a4fe690SJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a1_key_type);
2389e417205bSJohnny Huang 		sprintf(info_cb.ver_name, "A1");
23900dae9d52SJohnny Huang 		break;
2391e417205bSJohnny Huang 	case OTP_A2:
2392e417205bSJohnny Huang 		info_cb.version = OTP_A2;
23935fdde29fSJohnny Huang 		info_cb.conf_info = a2_conf_info;
23945fdde29fSJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a2_conf_info);
2395fed30023SJohnny Huang 		info_cb.strap_info = a1_strap_info;
2396fed30023SJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a1_strap_info);
23975fdde29fSJohnny Huang 		info_cb.key_info = a2_key_type;
23985fdde29fSJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a2_key_type);
2399e417205bSJohnny Huang 		sprintf(info_cb.ver_name, "A2");
24000dae9d52SJohnny Huang 		break;
2401e417205bSJohnny Huang 	case OTP_A3:
2402e417205bSJohnny Huang 		info_cb.version = OTP_A3;
2403b63af886SJohnny Huang 		info_cb.conf_info = a3_conf_info;
2404b63af886SJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a3_conf_info);
2405fed30023SJohnny Huang 		info_cb.strap_info = a1_strap_info;
2406fed30023SJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a1_strap_info);
2407181f72d8SJohnny Huang 		info_cb.key_info = a3_key_type;
2408181f72d8SJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a3_key_type);
2409e417205bSJohnny Huang 		sprintf(info_cb.ver_name, "A3");
241064b66712SJohnny Huang 		break;
24110dae9d52SJohnny Huang 	default:
2412f1be5099SJohnny Huang 		printf("SOC is not supported\n");
24130dae9d52SJohnny Huang 		return CMD_RET_FAILURE;
24149a4fe690SJohnny Huang 	}
24159a4fe690SJohnny Huang 
2416e14b073cSJohnny Huang 	ret = cp->cmd(cmdtp, flag, argc, argv);
2417e14b073cSJohnny Huang 	writel(1, OTP_PROTECT_KEY); //password
2418e14b073cSJohnny Huang 
2419e14b073cSJohnny Huang 	return ret;
242069d5fd8fSJohnny Huang }
242169d5fd8fSJohnny Huang 
2422a219f6deSJohnny Huang U_BOOT_CMD(otp, 7, 0,  do_ast_otp,
242369d5fd8fSJohnny Huang 	   "ASPEED One-Time-Programmable sub-system",
2424f67375f7SJohnny Huang 	   "version\n"
2425f67375f7SJohnny Huang 	   "otp read conf|data <otp_dw_offset> <dw_count>\n"
24262a856b9aSJohnny Huang 	   "otp read strap <strap_bit_offset> <bit_count>\n"
24272d4b0742SJohnny Huang 	   "otp info strap [v]\n"
24282d4b0742SJohnny Huang 	   "otp info conf [otp_dw_offset]\n"
2429de6b0cc4SJohnny Huang 	   "otp prog [o] <addr>\n"
2430ed071a2bSJohnny Huang 	   "otp pb conf|data [o] <otp_dw_offset> <bit_offset> <value>\n"
2431ed071a2bSJohnny Huang 	   "otp pb strap [o] <bit_offset> <value>\n"
2432ed071a2bSJohnny Huang 	   "otp protect [o] <bit_offset>\n"
2433e14b073cSJohnny Huang 	   "otp rprotect [o] <bit_offset>\n"
2434794e27ecSJohnny Huang 	   "otp update [o] <revision_id>\n"
2435794e27ecSJohnny Huang 	   "otp rid\n"
243669d5fd8fSJohnny Huang 	  );
2437