xref: /openbmc/u-boot/cmd/otp.c (revision f347c284e58a9293389eb87f95e50cccaf1cf6d5)
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 
23*f347c284SJohnny 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
563d3688adSJohnny Huang 
57696656c6SJohnny Huang #define OTP_MAGIC		"SOCOTP"
58696656c6SJohnny Huang #define CHECKSUM_LEN		32
59a219f6deSJohnny Huang #define OTP_INC_DATA		BIT(31)
60a219f6deSJohnny Huang #define OTP_INC_CONFIG		BIT(30)
61a219f6deSJohnny Huang #define OTP_INC_STRAP		BIT(29)
62a219f6deSJohnny Huang #define OTP_ECC_EN		BIT(28)
63696656c6SJohnny Huang #define OTP_REGION_SIZE(info)	((info >> 16) & 0xffff)
64696656c6SJohnny Huang #define OTP_REGION_OFFSET(info)	(info & 0xffff)
65696656c6SJohnny Huang #define OTP_IMAGE_SIZE(info)	(info & 0xffff)
66696656c6SJohnny Huang 
67e417205bSJohnny Huang #define OTP_A0		0
68e417205bSJohnny Huang #define OTP_A1		1
69e417205bSJohnny Huang #define OTP_A2		2
70e417205bSJohnny Huang #define OTP_A3		3
71e417205bSJohnny Huang 
72e417205bSJohnny Huang #define ID0_AST2600A0	0x05000303
73e417205bSJohnny Huang #define ID1_AST2600A0	0x05000303
74e417205bSJohnny Huang #define ID0_AST2600A1	0x05010303
75e417205bSJohnny Huang #define ID1_AST2600A1	0x05010203
76e417205bSJohnny Huang #define ID0_AST2600A2	0x05010303
77e417205bSJohnny Huang #define ID1_AST2600A2	0x05020303
78e417205bSJohnny Huang #define ID0_AST2600A3	0x05030303
79e417205bSJohnny Huang #define ID1_AST2600A3	0x05030303
80e417205bSJohnny Huang #define ID0_AST2620A1	0x05010203
81e417205bSJohnny Huang #define ID1_AST2620A1	0x05010203
82e417205bSJohnny Huang #define ID0_AST2620A2	0x05010203
83e417205bSJohnny Huang #define ID1_AST2620A2	0x05020203
84e417205bSJohnny Huang #define ID0_AST2620A3	0x05030203
85e417205bSJohnny Huang #define ID1_AST2620A3	0x05030203
86e417205bSJohnny Huang #define ID0_AST2620A3	0x05030203
87e417205bSJohnny Huang #define ID1_AST2620A3	0x05030203
88e417205bSJohnny Huang #define ID0_AST2605A2	0x05010103
89e417205bSJohnny Huang #define ID1_AST2605A2	0x05020103
90e417205bSJohnny Huang #define ID0_AST2605A3	0x05030103
91e417205bSJohnny Huang #define ID1_AST2605A3	0x05030103
92e417205bSJohnny Huang #define ID0_AST2625A3	0x05030403
93e417205bSJohnny Huang #define ID1_AST2625A3	0x05030403
94696656c6SJohnny Huang 
95696656c6SJohnny Huang struct otp_header {
96696656c6SJohnny Huang 	u8	otp_magic[8];
97696656c6SJohnny Huang 	u8	otp_version[8];
98696656c6SJohnny Huang 	u32	image_info;
99696656c6SJohnny Huang 	u32	data_info;
100696656c6SJohnny Huang 	u32	config_info;
101696656c6SJohnny Huang 	u32	strap_info;
102696656c6SJohnny Huang 	u32	checksum_offset;
103a219f6deSJohnny Huang } __packed;
104696656c6SJohnny Huang 
10566f2f8e5SJohnny Huang struct otpstrap_status {
10669d5fd8fSJohnny Huang 	int value;
10769d5fd8fSJohnny Huang 	int option_array[7];
10869d5fd8fSJohnny Huang 	int remain_times;
10969d5fd8fSJohnny Huang 	int writeable_option;
1105010032bSJohnny Huang 	int reg_protected;
11169d5fd8fSJohnny Huang 	int protected;
11269d5fd8fSJohnny Huang };
11369d5fd8fSJohnny Huang 
11466f2f8e5SJohnny Huang struct otpconf_parse {
11566f2f8e5SJohnny Huang 	int dw_offset;
11666f2f8e5SJohnny Huang 	int bit;
11766f2f8e5SJohnny Huang 	int length;
11866f2f8e5SJohnny Huang 	int value;
119696656c6SJohnny Huang 	int ignore;
12066f2f8e5SJohnny Huang 	char status[80];
12166f2f8e5SJohnny Huang };
12266f2f8e5SJohnny Huang 
1239a4fe690SJohnny Huang struct otpkey_type {
1249a4fe690SJohnny Huang 	int value;
1259a4fe690SJohnny Huang 	int key_type;
1269a4fe690SJohnny Huang 	int need_id;
1279a4fe690SJohnny Huang 	char information[110];
1289a4fe690SJohnny Huang };
1299a4fe690SJohnny Huang 
1309a4fe690SJohnny Huang struct otp_info_cb {
1319a4fe690SJohnny Huang 	int version;
132e417205bSJohnny Huang 	char ver_name[3];
13379e42a59SJoel Stanley 	const struct otpstrap_info *strap_info;
1349a4fe690SJohnny Huang 	int strap_info_len;
13579e42a59SJoel Stanley 	const struct otpconf_info *conf_info;
1369a4fe690SJohnny Huang 	int conf_info_len;
13779e42a59SJoel Stanley 	const struct otpkey_type *key_info;
1389a4fe690SJohnny Huang 	int key_info_len;
1399a4fe690SJohnny Huang };
1409a4fe690SJohnny Huang 
141696656c6SJohnny Huang struct otp_image_layout {
1425010032bSJohnny Huang 	int data_length;
1435010032bSJohnny Huang 	int conf_length;
1445010032bSJohnny Huang 	int strap_length;
145a219f6deSJohnny Huang 	u8 *data;
146a219f6deSJohnny Huang 	u8 *data_ignore;
147a219f6deSJohnny Huang 	u8 *conf;
148a219f6deSJohnny Huang 	u8 *conf_ignore;
149a219f6deSJohnny Huang 	u8 *strap;
150a219f6deSJohnny Huang 	u8 *strap_reg_pro;
151a219f6deSJohnny Huang 	u8 *strap_pro;
152a219f6deSJohnny Huang 	u8 *strap_ignore;
153696656c6SJohnny Huang };
154696656c6SJohnny Huang 
1559a4fe690SJohnny Huang static struct otp_info_cb info_cb;
1569a4fe690SJohnny Huang 
15779e42a59SJoel Stanley static const struct otpkey_type a0_key_type[] = {
1589a4fe690SJohnny Huang 	{0, OTP_KEY_TYPE_AES,   0, "AES-256 as OEM platform key for image encryption/decryption"},
1599a4fe690SJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
1609a4fe690SJohnny Huang 	{4, OTP_KEY_TYPE_HMAC,  1, "HMAC as encrypted OEM HMAC keys in Mode 1"},
161181f72d8SJohnny Huang 	{8, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2"},
162181f72d8SJohnny Huang 	{9, OTP_KEY_TYPE_RSA_PUB,   0, "RSA-public as SOC public key"},
163181f72d8SJohnny Huang 	{10, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key"},
164181f72d8SJohnny Huang 	{13, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as SOC private key"},
165181f72d8SJohnny Huang 	{14, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key"},
1669a4fe690SJohnny Huang };
1679a4fe690SJohnny Huang 
16879e42a59SJoel Stanley static const struct otpkey_type a1_key_type[] = {
1699a4fe690SJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
1709a4fe690SJohnny 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"},
171181f72d8SJohnny Huang 	{8, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2"},
172181f72d8SJohnny Huang 	{10, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key"},
173181f72d8SJohnny Huang 	{14, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key"},
1749a4fe690SJohnny Huang };
1759a4fe690SJohnny Huang 
1765fdde29fSJohnny Huang static const struct otpkey_type a2_key_type[] = {
1775fdde29fSJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
1785fdde29fSJohnny 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"},
179181f72d8SJohnny Huang 	{8, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2"},
180181f72d8SJohnny Huang 	{10, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key"},
181181f72d8SJohnny Huang 	{14, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key"},
182181f72d8SJohnny Huang };
183181f72d8SJohnny Huang 
184181f72d8SJohnny Huang static const struct otpkey_type a3_key_type[] = {
185181f72d8SJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
186181f72d8SJohnny 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"},
187181f72d8SJohnny Huang 	{8, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2"},
188181f72d8SJohnny Huang 	{9, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2(big endian)"},
189181f72d8SJohnny Huang 	{10, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key"},
190181f72d8SJohnny Huang 	{11, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key(big endian)"},
191181f72d8SJohnny Huang 	{12, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key"},
192181f72d8SJohnny Huang 	{13, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key(big endian)"},
1935fdde29fSJohnny Huang };
1945fdde29fSJohnny Huang 
195*f347c284SJohnny Huang static void buf_print(u8 *buf, int len)
196*f347c284SJohnny Huang {
197*f347c284SJohnny Huang 	int i;
198*f347c284SJohnny Huang 
199*f347c284SJohnny Huang 	printf("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
200*f347c284SJohnny Huang 	for (i = 0; i < len; i++) {
201*f347c284SJohnny Huang 		if (i % 16 == 0)
202*f347c284SJohnny Huang 			printf("%04X: ", i);
203*f347c284SJohnny Huang 		printf("%02X ", buf[i]);
204*f347c284SJohnny Huang 		if ((i + 1) % 16 == 0)
205*f347c284SJohnny Huang 			printf("\n");
206*f347c284SJohnny Huang 	}
207*f347c284SJohnny Huang }
208*f347c284SJohnny Huang 
209794e27ecSJohnny Huang static int get_dw_bit(u32 *rid, int offset)
210794e27ecSJohnny Huang {
211794e27ecSJohnny Huang 	int bit_offset;
212794e27ecSJohnny Huang 	int i;
213794e27ecSJohnny Huang 
214794e27ecSJohnny Huang 	if (offset < 32) {
215794e27ecSJohnny Huang 		i = 0;
216794e27ecSJohnny Huang 		bit_offset = offset;
217794e27ecSJohnny Huang 	} else {
218794e27ecSJohnny Huang 		i = 1;
219794e27ecSJohnny Huang 		bit_offset = offset - 32;
220794e27ecSJohnny Huang 	}
221794e27ecSJohnny Huang 	if ((rid[i] >> bit_offset) & 0x1)
222794e27ecSJohnny Huang 		return 1;
223794e27ecSJohnny Huang 	else
224794e27ecSJohnny Huang 		return 0;
225794e27ecSJohnny Huang }
226794e27ecSJohnny Huang 
227794e27ecSJohnny Huang static int get_rid_num(u32 *rid)
228794e27ecSJohnny Huang {
229794e27ecSJohnny Huang 	int i;
230794e27ecSJohnny Huang 	int fz = 0;
231794e27ecSJohnny Huang 	int rid_num = 0;
232794e27ecSJohnny Huang 	int ret = 0;
233794e27ecSJohnny Huang 
234794e27ecSJohnny Huang 	for (i = 0; i < 64; i++) {
235794e27ecSJohnny Huang 		if (get_dw_bit(rid, i) == 0) {
236794e27ecSJohnny Huang 			if (!fz)
237794e27ecSJohnny Huang 				fz = 1;
238794e27ecSJohnny Huang 
239794e27ecSJohnny Huang 		} else {
240794e27ecSJohnny Huang 			rid_num++;
241794e27ecSJohnny Huang 			if (fz)
242794e27ecSJohnny Huang 				ret = OTP_FAILURE;
243794e27ecSJohnny Huang 		}
244794e27ecSJohnny Huang 	}
245794e27ecSJohnny Huang 	if (ret)
246794e27ecSJohnny Huang 		return ret;
247794e27ecSJohnny Huang 
248794e27ecSJohnny Huang 	return rid_num;
249794e27ecSJohnny Huang }
250794e27ecSJohnny Huang 
251a219f6deSJohnny Huang static u32 chip_version(void)
2529a4fe690SJohnny Huang {
253e417205bSJohnny Huang 	u32 revid0, revid1;
2549a4fe690SJohnny Huang 
255e417205bSJohnny Huang 	revid0 = readl(ASPEED_REVISION_ID0);
256e417205bSJohnny Huang 	revid1 = readl(ASPEED_REVISION_ID1);
2579a4fe690SJohnny Huang 
258e417205bSJohnny Huang 	if (revid0 == ID0_AST2600A0 && revid1 == ID1_AST2600A0) {
259badd21c2SJohnny Huang 		/* AST2600-A0 */
260e417205bSJohnny Huang 		return OTP_A0;
261e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2600A1 && revid1 == ID1_AST2600A1) {
262badd21c2SJohnny Huang 		/* AST2600-A1 */
263e417205bSJohnny Huang 		return OTP_A1;
264e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2600A2 && revid1 == ID1_AST2600A2) {
265badd21c2SJohnny Huang 		/* AST2600-A2 */
266e417205bSJohnny Huang 		return OTP_A2;
267e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2600A3 && revid1 == ID1_AST2600A3) {
26864b66712SJohnny Huang 		/* AST2600-A3 */
269e417205bSJohnny Huang 		return OTP_A3;
270e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2620A1 && revid1 == ID1_AST2620A1) {
271e417205bSJohnny Huang 		/* AST2620-A1 */
272e417205bSJohnny Huang 		return OTP_A1;
273e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2620A2 && revid1 == ID1_AST2620A2) {
274e417205bSJohnny Huang 		/* AST2620-A2 */
275e417205bSJohnny Huang 		return OTP_A2;
276e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2620A3 && revid1 == ID1_AST2620A3) {
27764b66712SJohnny Huang 		/* AST2620-A3 */
278e417205bSJohnny Huang 		return OTP_A3;
279e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2605A2 && revid1 == ID1_AST2605A2) {
280e417205bSJohnny Huang 		/* AST2605-A2 */
281e417205bSJohnny Huang 		return OTP_A2;
282e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2605A3 && revid1 == ID1_AST2605A3) {
283e417205bSJohnny Huang 		/* AST2605-A3 */
284e417205bSJohnny Huang 		return OTP_A3;
285e417205bSJohnny Huang 	} else if (revid0 == ID0_AST2625A3 && revid1 == ID1_AST2625A3) {
286e417205bSJohnny Huang 		/* AST2605-A3 */
287e417205bSJohnny Huang 		return OTP_A3;
2880dae9d52SJohnny Huang 	}
289*f347c284SJohnny Huang 	return OTP_FAILURE;
2909a4fe690SJohnny Huang }
2919a4fe690SJohnny Huang 
2923d3688adSJohnny Huang static void wait_complete(void)
2933d3688adSJohnny Huang {
2943d3688adSJohnny Huang 	int reg;
2953d3688adSJohnny Huang 
2963d3688adSJohnny Huang 	do {
2973d3688adSJohnny Huang 		reg = readl(OTP_STATUS);
2983d3688adSJohnny Huang 	} while ((reg & 0x6) != 0x6);
2993d3688adSJohnny Huang }
3003d3688adSJohnny Huang 
301a219f6deSJohnny Huang static void otp_write(u32 otp_addr, u32 data)
302dacbba92SJohnny Huang {
303dacbba92SJohnny Huang 	writel(otp_addr, OTP_ADDR); //write address
304dacbba92SJohnny Huang 	writel(data, OTP_COMPARE_1); //write data
305dacbba92SJohnny Huang 	writel(0x23b1e362, OTP_COMMAND); //write command
306dacbba92SJohnny Huang 	wait_complete();
307dacbba92SJohnny Huang }
308dacbba92SJohnny Huang 
309dacbba92SJohnny Huang static void otp_soak(int soak)
310dacbba92SJohnny Huang {
311e417205bSJohnny Huang 	if (info_cb.version == OTP_A2 || info_cb.version == OTP_A3) {
312dacbba92SJohnny Huang 		switch (soak) {
313dacbba92SJohnny Huang 		case 0: //default
314dacbba92SJohnny Huang 			otp_write(0x3000, 0x0210); // Write MRA
315dacbba92SJohnny Huang 			otp_write(0x5000, 0x2000); // Write MRB
316dacbba92SJohnny Huang 			otp_write(0x1000, 0x0); // Write MR
317dacbba92SJohnny Huang 			break;
318dacbba92SJohnny Huang 		case 1: //normal program
319dacbba92SJohnny Huang 			otp_write(0x3000, 0x1200); // Write MRA
320feea3fdfSJohnny Huang 			otp_write(0x5000, 0x107F); // Write MRB
321dacbba92SJohnny Huang 			otp_write(0x1000, 0x1024); // Write MR
322feea3fdfSJohnny Huang 			writel(0x04191388, OTP_TIMING); // 200us
323dacbba92SJohnny Huang 			break;
324dacbba92SJohnny Huang 		case 2: //soak program
325dacbba92SJohnny Huang 			otp_write(0x3000, 0x1220); // Write MRA
326feea3fdfSJohnny Huang 			otp_write(0x5000, 0x2074); // Write MRB
327dacbba92SJohnny Huang 			otp_write(0x1000, 0x08a4); // Write MR
328feea3fdfSJohnny Huang 			writel(0x04193a98, OTP_TIMING); // 600us
329dacbba92SJohnny Huang 			break;
330dacbba92SJohnny Huang 		}
331dacbba92SJohnny Huang 	} else {
332dacbba92SJohnny Huang 		switch (soak) {
333dacbba92SJohnny Huang 		case 0: //default
334dacbba92SJohnny Huang 			otp_write(0x3000, 0x0); // Write MRA
335dacbba92SJohnny Huang 			otp_write(0x5000, 0x0); // Write MRB
336dacbba92SJohnny Huang 			otp_write(0x1000, 0x0); // Write MR
337dacbba92SJohnny Huang 			break;
338dacbba92SJohnny Huang 		case 1: //normal program
339dacbba92SJohnny Huang 			otp_write(0x3000, 0x4021); // Write MRA
340dacbba92SJohnny Huang 			otp_write(0x5000, 0x302f); // Write MRB
341dacbba92SJohnny Huang 			otp_write(0x1000, 0x4020); // Write MR
342feea3fdfSJohnny Huang 			writel(0x04190760, OTP_TIMING); // 75us
343dacbba92SJohnny Huang 			break;
344dacbba92SJohnny Huang 		case 2: //soak program
345dacbba92SJohnny Huang 			otp_write(0x3000, 0x4021); // Write MRA
346dacbba92SJohnny Huang 			otp_write(0x5000, 0x1027); // Write MRB
347dacbba92SJohnny Huang 			otp_write(0x1000, 0x4820); // Write MR
348feea3fdfSJohnny Huang 			writel(0x041930d4, OTP_TIMING); // 500us
349dacbba92SJohnny Huang 			break;
350dacbba92SJohnny Huang 		}
351dacbba92SJohnny Huang 	}
352dacbba92SJohnny Huang 
353dacbba92SJohnny Huang 	wait_complete();
354dacbba92SJohnny Huang }
355dacbba92SJohnny Huang 
356a219f6deSJohnny Huang static void otp_read_data(u32 offset, u32 *data)
35769d5fd8fSJohnny Huang {
3583d3688adSJohnny Huang 	writel(offset, OTP_ADDR); //Read address
3593d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
3603d3688adSJohnny Huang 	wait_complete();
3613d3688adSJohnny Huang 	data[0] = readl(OTP_COMPARE_1);
3623d3688adSJohnny Huang 	data[1] = readl(OTP_COMPARE_2);
36369d5fd8fSJohnny Huang }
36469d5fd8fSJohnny Huang 
365*f347c284SJohnny Huang static void otp_read_conf(u32 offset, u32 *data)
36669d5fd8fSJohnny Huang {
36769d5fd8fSJohnny Huang 	int config_offset;
36869d5fd8fSJohnny Huang 
36969d5fd8fSJohnny Huang 	config_offset = 0x800;
37069d5fd8fSJohnny Huang 	config_offset |= (offset / 8) * 0x200;
37169d5fd8fSJohnny Huang 	config_offset |= (offset % 8) * 0x2;
37269d5fd8fSJohnny Huang 
3733d3688adSJohnny Huang 	writel(config_offset, OTP_ADDR);  //Read address
3743d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
3753d3688adSJohnny Huang 	wait_complete();
3763d3688adSJohnny Huang 	data[0] = readl(OTP_COMPARE_1);
37769d5fd8fSJohnny Huang }
37869d5fd8fSJohnny Huang 
379a219f6deSJohnny Huang static int otp_compare(u32 otp_addr, u32 addr)
38069d5fd8fSJohnny Huang {
381a219f6deSJohnny Huang 	u32 ret;
382a219f6deSJohnny Huang 	u32 *buf;
38369d5fd8fSJohnny Huang 
38469d5fd8fSJohnny Huang 	buf = map_physmem(addr, 16, MAP_WRBACK);
38569d5fd8fSJohnny Huang 	printf("%08X\n", buf[0]);
38669d5fd8fSJohnny Huang 	printf("%08X\n", buf[1]);
38769d5fd8fSJohnny Huang 	printf("%08X\n", buf[2]);
38869d5fd8fSJohnny Huang 	printf("%08X\n", buf[3]);
3893d3688adSJohnny Huang 	writel(otp_addr, OTP_ADDR); //Compare address
3903d3688adSJohnny Huang 	writel(buf[0], OTP_COMPARE_1); //Compare data 1
3913d3688adSJohnny Huang 	writel(buf[1], OTP_COMPARE_2); //Compare data 2
3923d3688adSJohnny Huang 	writel(buf[2], OTP_COMPARE_3); //Compare data 3
3933d3688adSJohnny Huang 	writel(buf[3], OTP_COMPARE_4); //Compare data 4
3943d3688adSJohnny Huang 	writel(0x23b1e363, OTP_COMMAND); //Compare command
3953d3688adSJohnny Huang 	wait_complete();
3963d3688adSJohnny Huang 	ret = readl(OTP_STATUS); //Compare command
39769d5fd8fSJohnny Huang 	if (ret & 0x1)
398*f347c284SJohnny Huang 		return OTP_SUCCESS;
39969d5fd8fSJohnny Huang 	else
400*f347c284SJohnny Huang 		return OTP_FAILURE;
40169d5fd8fSJohnny Huang }
40269d5fd8fSJohnny Huang 
403a219f6deSJohnny Huang static int verify_bit(u32 otp_addr, int bit_offset, int value)
40469d5fd8fSJohnny Huang {
405a219f6deSJohnny Huang 	u32 ret[2];
40669d5fd8fSJohnny Huang 
40730a8c590SJohnny Huang 	if (otp_addr % 2 == 0)
4083d3688adSJohnny Huang 		writel(otp_addr, OTP_ADDR); //Read address
40930a8c590SJohnny Huang 	else
4103d3688adSJohnny Huang 		writel(otp_addr - 1, OTP_ADDR); //Read address
41130a8c590SJohnny Huang 
4123d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
4133d3688adSJohnny Huang 	wait_complete();
4143d3688adSJohnny Huang 	ret[0] = readl(OTP_COMPARE_1);
4153d3688adSJohnny Huang 	ret[1] = readl(OTP_COMPARE_2);
41683655e91SJohnny Huang 
41730a8c590SJohnny Huang 	if (otp_addr % 2 == 0) {
41830a8c590SJohnny Huang 		if (((ret[0] >> bit_offset) & 1) == value)
419*f347c284SJohnny Huang 			return OTP_SUCCESS;
42069d5fd8fSJohnny Huang 		else
421*f347c284SJohnny Huang 			return OTP_FAILURE;
42230a8c590SJohnny Huang 	} else {
42330a8c590SJohnny Huang 		if (((ret[1] >> bit_offset) & 1) == value)
424*f347c284SJohnny Huang 			return OTP_SUCCESS;
42530a8c590SJohnny Huang 		else
426*f347c284SJohnny Huang 			return OTP_FAILURE;
42730a8c590SJohnny Huang 	}
42869d5fd8fSJohnny Huang }
42969d5fd8fSJohnny Huang 
430a219f6deSJohnny Huang static u32 verify_dw(u32 otp_addr, u32 *value, u32 *ignore, u32 *compare, int size)
4314c1c9b35SJohnny Huang {
432a219f6deSJohnny Huang 	u32 ret[2];
4334c1c9b35SJohnny Huang 
4344c1c9b35SJohnny Huang 	otp_addr &= ~(1 << 15);
4354c1c9b35SJohnny Huang 
4364c1c9b35SJohnny Huang 	if (otp_addr % 2 == 0)
4373d3688adSJohnny Huang 		writel(otp_addr, OTP_ADDR); //Read address
4384c1c9b35SJohnny Huang 	else
4393d3688adSJohnny Huang 		writel(otp_addr - 1, OTP_ADDR); //Read address
4403d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
4413d3688adSJohnny Huang 	wait_complete();
4423d3688adSJohnny Huang 	ret[0] = readl(OTP_COMPARE_1);
4433d3688adSJohnny Huang 	ret[1] = readl(OTP_COMPARE_2);
4444c1c9b35SJohnny Huang 	if (size == 1) {
4454c1c9b35SJohnny Huang 		if (otp_addr % 2 == 0) {
4464c1c9b35SJohnny Huang 			// printf("check %x : %x = %x\n", otp_addr, ret[0], value[0]);
447696656c6SJohnny Huang 			if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0])) {
4484c1c9b35SJohnny Huang 				compare[0] = 0;
449*f347c284SJohnny Huang 				return OTP_SUCCESS;
450a219f6deSJohnny Huang 			}
4514c1c9b35SJohnny Huang 			compare[0] = value[0] ^ ret[0];
452*f347c284SJohnny Huang 			return OTP_FAILURE;
4534c1c9b35SJohnny Huang 
4544c1c9b35SJohnny Huang 		} else {
4554c1c9b35SJohnny Huang 			// printf("check %x : %x = %x\n", otp_addr, ret[1], value[0]);
456696656c6SJohnny Huang 			if ((value[0] & ~ignore[0]) == (ret[1] & ~ignore[0])) {
4574c1c9b35SJohnny Huang 				compare[0] = ~0;
458*f347c284SJohnny Huang 				return OTP_SUCCESS;
459a219f6deSJohnny Huang 			}
460d90825e2SJohnny Huang 			compare[0] = ~(value[0] ^ ret[1]);
461*f347c284SJohnny Huang 			return OTP_FAILURE;
4624c1c9b35SJohnny Huang 		}
4634c1c9b35SJohnny Huang 	} else if (size == 2) {
4644c1c9b35SJohnny Huang 		// otp_addr should be even
465696656c6SJohnny Huang 		if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0]) && (value[1] & ~ignore[1]) == (ret[1] & ~ignore[1])) {
4664c1c9b35SJohnny Huang 			// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
4674c1c9b35SJohnny Huang 			// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
4684c1c9b35SJohnny Huang 			compare[0] = 0;
4694c1c9b35SJohnny Huang 			compare[1] = ~0;
470*f347c284SJohnny Huang 			return OTP_SUCCESS;
471a219f6deSJohnny Huang 		}
4724c1c9b35SJohnny Huang 		// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
4734c1c9b35SJohnny Huang 		// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
4744c1c9b35SJohnny Huang 		compare[0] = value[0] ^ ret[0];
4754c1c9b35SJohnny Huang 		compare[1] = ~(value[1] ^ ret[1]);
476*f347c284SJohnny Huang 		return OTP_FAILURE;
4774c1c9b35SJohnny Huang 	} else {
478*f347c284SJohnny Huang 		return OTP_FAILURE;
4794c1c9b35SJohnny Huang 	}
4804c1c9b35SJohnny Huang }
4814c1c9b35SJohnny Huang 
482a219f6deSJohnny Huang static void otp_prog(u32 otp_addr, u32 prog_bit)
48383655e91SJohnny Huang {
48490965bb3SJohnny Huang 	otp_write(0x0, prog_bit);
48583655e91SJohnny Huang 	writel(otp_addr, OTP_ADDR); //write address
48683655e91SJohnny Huang 	writel(prog_bit, OTP_COMPARE_1); //write data
48783655e91SJohnny Huang 	writel(0x23b1e364, OTP_COMMAND); //write command
48883655e91SJohnny Huang 	wait_complete();
48983655e91SJohnny Huang }
49083655e91SJohnny Huang 
491a219f6deSJohnny Huang static void _otp_prog_bit(u32 value, u32 prog_address, u32 bit_offset)
49283655e91SJohnny Huang {
49383655e91SJohnny Huang 	int prog_bit;
49483655e91SJohnny Huang 
49583655e91SJohnny Huang 	if (prog_address % 2 == 0) {
49683655e91SJohnny Huang 		if (value)
49783655e91SJohnny Huang 			prog_bit = ~(0x1 << bit_offset);
49883655e91SJohnny Huang 		else
49983655e91SJohnny Huang 			return;
50083655e91SJohnny Huang 	} else {
501e417205bSJohnny Huang 		if (info_cb.version != OTP_A3)
50283655e91SJohnny Huang 			prog_address |= 1 << 15;
50383655e91SJohnny Huang 		if (!value)
50483655e91SJohnny Huang 			prog_bit = 0x1 << bit_offset;
50583655e91SJohnny Huang 		else
50683655e91SJohnny Huang 			return;
50783655e91SJohnny Huang 	}
50883655e91SJohnny Huang 	otp_prog(prog_address, prog_bit);
50983655e91SJohnny Huang }
51083655e91SJohnny Huang 
511*f347c284SJohnny Huang static int otp_prog_dc_b(u32 value, u32 prog_address, u32 bit_offset)
51283655e91SJohnny Huang {
51383655e91SJohnny Huang 	int pass;
51483655e91SJohnny Huang 	int i;
51583655e91SJohnny Huang 
51683655e91SJohnny Huang 	otp_soak(1);
51783655e91SJohnny Huang 	_otp_prog_bit(value, prog_address, bit_offset);
51883655e91SJohnny Huang 	pass = 0;
51983655e91SJohnny Huang 
52083655e91SJohnny Huang 	for (i = 0; i < RETRY; i++) {
52183655e91SJohnny Huang 		if (verify_bit(prog_address, bit_offset, value) != 0) {
52283655e91SJohnny Huang 			otp_soak(2);
52383655e91SJohnny Huang 			_otp_prog_bit(value, prog_address, bit_offset);
52483655e91SJohnny Huang 			if (verify_bit(prog_address, bit_offset, value) != 0) {
52583655e91SJohnny Huang 				otp_soak(1);
52683655e91SJohnny Huang 			} else {
52783655e91SJohnny Huang 				pass = 1;
52883655e91SJohnny Huang 				break;
52983655e91SJohnny Huang 			}
53083655e91SJohnny Huang 		} else {
53183655e91SJohnny Huang 			pass = 1;
53283655e91SJohnny Huang 			break;
53383655e91SJohnny Huang 		}
53483655e91SJohnny Huang 	}
535794e27ecSJohnny Huang 	if (pass)
536794e27ecSJohnny Huang 		return OTP_SUCCESS;
53783655e91SJohnny Huang 
538794e27ecSJohnny Huang 	return OTP_FAILURE;
53983655e91SJohnny Huang }
54083655e91SJohnny Huang 
541a219f6deSJohnny Huang static void otp_prog_dw(u32 value, u32 ignore, u32 prog_address)
542d90825e2SJohnny Huang {
543d90825e2SJohnny Huang 	int j, bit_value, prog_bit;
544d90825e2SJohnny Huang 
545d90825e2SJohnny Huang 	for (j = 0; j < 32; j++) {
546696656c6SJohnny Huang 		if ((ignore >> j) & 0x1)
547d90825e2SJohnny Huang 			continue;
548d90825e2SJohnny Huang 		bit_value = (value >> j) & 0x1;
549d90825e2SJohnny Huang 		if (prog_address % 2 == 0) {
550d90825e2SJohnny Huang 			if (bit_value)
551d90825e2SJohnny Huang 				prog_bit = ~(0x1 << j);
552d90825e2SJohnny Huang 			else
553d90825e2SJohnny Huang 				continue;
554d90825e2SJohnny Huang 		} else {
555e417205bSJohnny Huang 			if (info_cb.version != OTP_A3)
556d90825e2SJohnny Huang 				prog_address |= 1 << 15;
557d90825e2SJohnny Huang 			if (bit_value)
558d90825e2SJohnny Huang 				continue;
559d90825e2SJohnny Huang 			else
560d90825e2SJohnny Huang 				prog_bit = 0x1 << j;
561d90825e2SJohnny Huang 		}
562d90825e2SJohnny Huang 		otp_prog(prog_address, prog_bit);
563d90825e2SJohnny Huang 	}
564d90825e2SJohnny Huang }
565d90825e2SJohnny Huang 
566a219f6deSJohnny Huang static int otp_prog_verify_2dw(u32 *data, u32 *buf, u32 *ignore_mask, u32 prog_address)
56754552c69SJohnny Huang {
56854552c69SJohnny Huang 	int pass;
56954552c69SJohnny Huang 	int i;
570a219f6deSJohnny Huang 	u32 data0_masked;
571a219f6deSJohnny Huang 	u32 data1_masked;
572a219f6deSJohnny Huang 	u32 buf0_masked;
573a219f6deSJohnny Huang 	u32 buf1_masked;
574a219f6deSJohnny Huang 	u32 compare[2];
57554552c69SJohnny Huang 
57654552c69SJohnny Huang 	data0_masked = data[0]  & ~ignore_mask[0];
57754552c69SJohnny Huang 	buf0_masked  = buf[0] & ~ignore_mask[0];
57854552c69SJohnny Huang 	data1_masked = data[1]  & ~ignore_mask[1];
57954552c69SJohnny Huang 	buf1_masked  = buf[1] & ~ignore_mask[1];
580a219f6deSJohnny Huang 	if (data0_masked == buf0_masked && data1_masked == buf1_masked)
581*f347c284SJohnny Huang 		return OTP_SUCCESS;
58254552c69SJohnny Huang 
58354552c69SJohnny Huang 	otp_soak(1);
58454552c69SJohnny Huang 	if (data0_masked != buf0_masked)
58554552c69SJohnny Huang 		otp_prog_dw(buf[0], ignore_mask[0], prog_address);
58654552c69SJohnny Huang 	if (data1_masked != buf1_masked)
58754552c69SJohnny Huang 		otp_prog_dw(buf[1], ignore_mask[1], prog_address + 1);
58854552c69SJohnny Huang 
58954552c69SJohnny Huang 	pass = 0;
59054552c69SJohnny Huang 	for (i = 0; i < RETRY; i++) {
59154552c69SJohnny Huang 		if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) {
59254552c69SJohnny Huang 			otp_soak(2);
593a219f6deSJohnny Huang 			if (compare[0] != 0)
59454552c69SJohnny Huang 				otp_prog_dw(compare[0], ignore_mask[0], prog_address);
595a219f6deSJohnny Huang 			if (compare[1] != ~0)
5965537bc72SJohnny Huang 				otp_prog_dw(compare[1], ignore_mask[1], prog_address + 1);
59754552c69SJohnny Huang 			if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) {
59854552c69SJohnny Huang 				otp_soak(1);
59954552c69SJohnny Huang 			} else {
60054552c69SJohnny Huang 				pass = 1;
60154552c69SJohnny Huang 				break;
60254552c69SJohnny Huang 			}
60354552c69SJohnny Huang 		} else {
60454552c69SJohnny Huang 			pass = 1;
60554552c69SJohnny Huang 			break;
60654552c69SJohnny Huang 		}
60754552c69SJohnny Huang 	}
60854552c69SJohnny Huang 
60954552c69SJohnny Huang 	if (!pass) {
61054552c69SJohnny Huang 		otp_soak(0);
61154552c69SJohnny Huang 		return OTP_FAILURE;
61254552c69SJohnny Huang 	}
61354552c69SJohnny Huang 	return OTP_SUCCESS;
61454552c69SJohnny Huang }
61554552c69SJohnny Huang 
616541eb887SJohnny Huang static void otp_strap_status(struct otpstrap_status *otpstrap)
61776d13988SJohnny Huang {
618a219f6deSJohnny Huang 	u32 OTPSTRAP_RAW[2];
6195010032bSJohnny Huang 	int strap_end;
62076d13988SJohnny Huang 	int i, j;
62176d13988SJohnny Huang 
622e417205bSJohnny Huang 	if (info_cb.version == OTP_A0) {
62376d13988SJohnny Huang 		for (j = 0; j < 64; j++) {
62476d13988SJohnny Huang 			otpstrap[j].value = 0;
62576d13988SJohnny Huang 			otpstrap[j].remain_times = 7;
62676d13988SJohnny Huang 			otpstrap[j].writeable_option = -1;
62776d13988SJohnny Huang 			otpstrap[j].protected = 0;
62876d13988SJohnny Huang 		}
6295010032bSJohnny Huang 		strap_end = 30;
6305010032bSJohnny Huang 	} else {
6315010032bSJohnny Huang 		for (j = 0; j < 64; j++) {
6325010032bSJohnny Huang 			otpstrap[j].value = 0;
6335010032bSJohnny Huang 			otpstrap[j].remain_times = 6;
6345010032bSJohnny Huang 			otpstrap[j].writeable_option = -1;
6355010032bSJohnny Huang 			otpstrap[j].reg_protected = 0;
6365010032bSJohnny Huang 			otpstrap[j].protected = 0;
6375010032bSJohnny Huang 		}
6385010032bSJohnny Huang 		strap_end = 28;
6395010032bSJohnny Huang 	}
64076d13988SJohnny Huang 
641dacbba92SJohnny Huang 	otp_soak(0);
6425010032bSJohnny Huang 	for (i = 16; i < strap_end; i += 2) {
64376d13988SJohnny Huang 		int option = (i - 16) / 2;
644a219f6deSJohnny Huang 
645*f347c284SJohnny Huang 		otp_read_conf(i, &OTPSTRAP_RAW[0]);
646*f347c284SJohnny Huang 		otp_read_conf(i + 1, &OTPSTRAP_RAW[1]);
64776d13988SJohnny Huang 		for (j = 0; j < 32; j++) {
64876d13988SJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[0] >> j) & 0x1);
649a219f6deSJohnny Huang 
650a219f6deSJohnny Huang 			if (bit_value == 0 && otpstrap[j].writeable_option == -1)
65176d13988SJohnny Huang 				otpstrap[j].writeable_option = option;
65276d13988SJohnny Huang 			if (bit_value == 1)
65376d13988SJohnny Huang 				otpstrap[j].remain_times--;
65476d13988SJohnny Huang 			otpstrap[j].value ^= bit_value;
65576d13988SJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
65676d13988SJohnny Huang 		}
65776d13988SJohnny Huang 		for (j = 32; j < 64; j++) {
65876d13988SJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1);
659a219f6deSJohnny Huang 
660a219f6deSJohnny Huang 			if (bit_value == 0 && otpstrap[j].writeable_option == -1)
66176d13988SJohnny Huang 				otpstrap[j].writeable_option = option;
66276d13988SJohnny Huang 			if (bit_value == 1)
66376d13988SJohnny Huang 				otpstrap[j].remain_times--;
66476d13988SJohnny Huang 			otpstrap[j].value ^= bit_value;
66576d13988SJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
66676d13988SJohnny Huang 		}
66776d13988SJohnny Huang 	}
6685010032bSJohnny Huang 
669e417205bSJohnny Huang 	if (info_cb.version != OTP_A0) {
670*f347c284SJohnny Huang 		otp_read_conf(28, &OTPSTRAP_RAW[0]);
671*f347c284SJohnny Huang 		otp_read_conf(29, &OTPSTRAP_RAW[1]);
6725010032bSJohnny Huang 		for (j = 0; j < 32; j++) {
6735010032bSJohnny Huang 			if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
6745010032bSJohnny Huang 				otpstrap[j].reg_protected = 1;
6755010032bSJohnny Huang 		}
6765010032bSJohnny Huang 		for (j = 32; j < 64; j++) {
6775010032bSJohnny Huang 			if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
6785010032bSJohnny Huang 				otpstrap[j].reg_protected = 1;
6795010032bSJohnny Huang 		}
6805010032bSJohnny Huang 	}
6815010032bSJohnny Huang 
682*f347c284SJohnny Huang 	otp_read_conf(30, &OTPSTRAP_RAW[0]);
683*f347c284SJohnny Huang 	otp_read_conf(31, &OTPSTRAP_RAW[1]);
68476d13988SJohnny Huang 	for (j = 0; j < 32; j++) {
68576d13988SJohnny Huang 		if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
68676d13988SJohnny Huang 			otpstrap[j].protected = 1;
68776d13988SJohnny Huang 	}
68876d13988SJohnny Huang 	for (j = 32; j < 64; j++) {
68976d13988SJohnny Huang 		if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
69076d13988SJohnny Huang 			otpstrap[j].protected = 1;
69176d13988SJohnny Huang 	}
69276d13988SJohnny Huang }
69376d13988SJohnny Huang 
694*f347c284SJohnny Huang static int otp_strap_bit_confirm(struct otpstrap_status *otpstrap, int offset, int ibit, int bit, int pbit, int rpbit)
695*f347c284SJohnny Huang {
696*f347c284SJohnny Huang 	int prog_flag = 0;
697*f347c284SJohnny Huang 
698*f347c284SJohnny Huang 	// ignore this bit
699*f347c284SJohnny Huang 	if (ibit == 1)
700*f347c284SJohnny Huang 		return OTP_SUCCESS;
701*f347c284SJohnny Huang 	printf("OTPSTRAP[%X]:\n", offset);
702*f347c284SJohnny Huang 
703*f347c284SJohnny Huang 	if (bit == otpstrap->value) {
704*f347c284SJohnny Huang 		if (!pbit && !rpbit) {
705*f347c284SJohnny Huang 			printf("    The value is same as before, skip it.\n");
706*f347c284SJohnny Huang 			return OTP_PROG_SKIP;
707*f347c284SJohnny Huang 		}
708*f347c284SJohnny Huang 		printf("    The value is same as before.\n");
709*f347c284SJohnny Huang 	} else {
710*f347c284SJohnny Huang 		prog_flag = 1;
711*f347c284SJohnny Huang 	}
712*f347c284SJohnny Huang 	if (otpstrap->protected == 1 && prog_flag) {
713*f347c284SJohnny Huang 		printf("    This bit is protected and is not writable\n");
714*f347c284SJohnny Huang 		return OTP_FAILURE;
715*f347c284SJohnny Huang 	}
716*f347c284SJohnny Huang 	if (otpstrap->remain_times == 0 && prog_flag) {
717*f347c284SJohnny Huang 		printf("    This bit is no remaining times to write.\n");
718*f347c284SJohnny Huang 		return OTP_FAILURE;
719*f347c284SJohnny Huang 	}
720*f347c284SJohnny Huang 	if (pbit == 1)
721*f347c284SJohnny Huang 		printf("    This bit will be protected and become non-writable.\n");
722*f347c284SJohnny Huang 	if (rpbit == 1 && info_cb.version != OTP_A0)
723*f347c284SJohnny Huang 		printf("    The relative register will be protected.\n");
724*f347c284SJohnny Huang 	if (prog_flag)
725*f347c284SJohnny 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);
726*f347c284SJohnny Huang 
727*f347c284SJohnny Huang 	return OTP_SUCCESS;
728*f347c284SJohnny Huang }
729*f347c284SJohnny Huang 
730*f347c284SJohnny Huang static int otp_prog_strap_b(int bit_offset, int value)
731*f347c284SJohnny Huang {
732*f347c284SJohnny Huang 	struct otpstrap_status otpstrap[64];
733*f347c284SJohnny Huang 	u32 prog_address;
734*f347c284SJohnny Huang 	int offset;
735*f347c284SJohnny Huang 	int ret;
736*f347c284SJohnny Huang 
737*f347c284SJohnny Huang 	otp_strap_status(otpstrap);
738*f347c284SJohnny Huang 
739*f347c284SJohnny Huang 	ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0, 0);
740*f347c284SJohnny Huang 
741*f347c284SJohnny Huang 	if (ret != OTP_SUCCESS)
742*f347c284SJohnny Huang 		return ret;
743*f347c284SJohnny Huang 
744*f347c284SJohnny Huang 	prog_address = 0x800;
745*f347c284SJohnny Huang 	if (bit_offset < 32) {
746*f347c284SJohnny Huang 		offset = bit_offset;
747*f347c284SJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) / 8) * 0x200;
748*f347c284SJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) % 8) * 0x2;
749*f347c284SJohnny Huang 
750*f347c284SJohnny Huang 	} else {
751*f347c284SJohnny Huang 		offset = (bit_offset - 32);
752*f347c284SJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) / 8) * 0x200;
753*f347c284SJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) % 8) * 0x2;
754*f347c284SJohnny Huang 	}
755*f347c284SJohnny Huang 
756*f347c284SJohnny Huang 	return otp_prog_dc_b(1, prog_address, offset);
757*f347c284SJohnny Huang }
758*f347c284SJohnny Huang 
759*f347c284SJohnny Huang static int otp_print_conf(u32 offset, int dw_count)
760*f347c284SJohnny Huang {
761*f347c284SJohnny Huang 	int i;
762*f347c284SJohnny Huang 	u32 ret[1];
763*f347c284SJohnny Huang 
764*f347c284SJohnny Huang 	if (offset + dw_count > 32)
765*f347c284SJohnny Huang 		return OTP_USAGE;
766*f347c284SJohnny Huang 	otp_soak(0);
767*f347c284SJohnny Huang 	for (i = offset; i < offset + dw_count; i++) {
768*f347c284SJohnny Huang 		otp_read_conf(i, ret);
769*f347c284SJohnny Huang 		printf("OTPCFG%X: %08X\n", i, ret[0]);
770*f347c284SJohnny Huang 	}
771*f347c284SJohnny Huang 	printf("\n");
772*f347c284SJohnny Huang 	return OTP_SUCCESS;
773*f347c284SJohnny Huang }
774*f347c284SJohnny Huang 
775*f347c284SJohnny Huang static int otp_print_data(u32 offset, int dw_count)
776*f347c284SJohnny Huang {
777*f347c284SJohnny Huang 	int i;
778*f347c284SJohnny Huang 	u32 ret[2];
779*f347c284SJohnny Huang 
780*f347c284SJohnny Huang 	if (offset + dw_count > 2048 || offset % 4 != 0)
781*f347c284SJohnny Huang 		return OTP_USAGE;
782*f347c284SJohnny Huang 	otp_soak(0);
783*f347c284SJohnny Huang 	for (i = offset; i < offset + dw_count; i += 2) {
784*f347c284SJohnny Huang 		otp_read_data(i, ret);
785*f347c284SJohnny Huang 		if (i % 4 == 0)
786*f347c284SJohnny Huang 			printf("%03X: %08X %08X ", i * 4, ret[0], ret[1]);
787*f347c284SJohnny Huang 		else
788*f347c284SJohnny Huang 			printf("%08X %08X\n", ret[0], ret[1]);
789*f347c284SJohnny Huang 	}
790*f347c284SJohnny Huang 	printf("\n");
791*f347c284SJohnny Huang 	return OTP_SUCCESS;
792*f347c284SJohnny Huang }
793*f347c284SJohnny Huang 
794*f347c284SJohnny Huang static int otp_print_strap(int start, int count)
795*f347c284SJohnny Huang {
796*f347c284SJohnny Huang 	int i, j;
797*f347c284SJohnny Huang 	int remains;
798*f347c284SJohnny Huang 	struct otpstrap_status otpstrap[64];
799*f347c284SJohnny Huang 
800*f347c284SJohnny Huang 	if (start < 0 || start > 64)
801*f347c284SJohnny Huang 		return OTP_USAGE;
802*f347c284SJohnny Huang 
803*f347c284SJohnny Huang 	if ((start + count) < 0 || (start + count) > 64)
804*f347c284SJohnny Huang 		return OTP_USAGE;
805*f347c284SJohnny Huang 
806*f347c284SJohnny Huang 	otp_strap_status(otpstrap);
807*f347c284SJohnny Huang 
808*f347c284SJohnny Huang 	if (info_cb.version == OTP_A0) {
809*f347c284SJohnny Huang 		remains = 7;
810*f347c284SJohnny Huang 		printf("BIT(hex)  Value  Option           Status\n");
811*f347c284SJohnny Huang 	} else {
812*f347c284SJohnny Huang 		remains = 6;
813*f347c284SJohnny Huang 		printf("BIT(hex)  Value  Option         Reg_Protect Status\n");
814*f347c284SJohnny Huang 	}
815*f347c284SJohnny Huang 	printf("______________________________________________________________________________\n");
816*f347c284SJohnny Huang 
817*f347c284SJohnny Huang 	for (i = start; i < start + count; i++) {
818*f347c284SJohnny Huang 		printf("0x%-8X", i);
819*f347c284SJohnny Huang 		printf("%-7d", otpstrap[i].value);
820*f347c284SJohnny Huang 		for (j = 0; j < remains; j++)
821*f347c284SJohnny Huang 			printf("%d ", otpstrap[i].option_array[j]);
822*f347c284SJohnny Huang 		printf("   ");
823*f347c284SJohnny Huang 		if (info_cb.version != OTP_A0)
824*f347c284SJohnny Huang 			printf("%d           ", otpstrap[i].reg_protected);
825*f347c284SJohnny Huang 		if (otpstrap[i].protected == 1) {
826*f347c284SJohnny Huang 			printf("protected and not writable");
827*f347c284SJohnny Huang 		} else {
828*f347c284SJohnny Huang 			printf("not protected ");
829*f347c284SJohnny Huang 			if (otpstrap[i].remain_times == 0)
830*f347c284SJohnny Huang 				printf("and no remaining times to write.");
831*f347c284SJohnny Huang 			else
832*f347c284SJohnny Huang 				printf("and still can write %d times", otpstrap[i].remain_times);
833*f347c284SJohnny Huang 		}
834*f347c284SJohnny Huang 		printf("\n");
835*f347c284SJohnny Huang 	}
836*f347c284SJohnny Huang 
837*f347c284SJohnny Huang 	return OTP_SUCCESS;
838*f347c284SJohnny Huang }
839*f347c284SJohnny Huang 
840794e27ecSJohnny Huang static void otp_print_revid(u32 *rid)
841794e27ecSJohnny Huang {
842794e27ecSJohnny Huang 	int bit_offset;
843794e27ecSJohnny Huang 	int i, j;
844794e27ecSJohnny Huang 
845794e27ecSJohnny Huang 	printf("     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f\n");
846794e27ecSJohnny Huang 	printf("___________________________________________________\n");
847794e27ecSJohnny Huang 	for (i = 0; i < 64; i++) {
848794e27ecSJohnny Huang 		if (i < 32) {
849794e27ecSJohnny Huang 			j = 0;
850794e27ecSJohnny Huang 			bit_offset = i;
851794e27ecSJohnny Huang 		} else {
852794e27ecSJohnny Huang 			j = 1;
853794e27ecSJohnny Huang 			bit_offset = i - 32;
854794e27ecSJohnny Huang 		}
855794e27ecSJohnny Huang 		if (i % 16 == 0)
856794e27ecSJohnny Huang 			printf("%2x | ", i);
857794e27ecSJohnny Huang 		printf("%d  ", (rid[j] >> bit_offset) & 0x1);
858794e27ecSJohnny Huang 		if ((i + 1) % 16 == 0)
859794e27ecSJohnny Huang 			printf("\n");
860794e27ecSJohnny Huang 	}
861794e27ecSJohnny Huang }
862794e27ecSJohnny Huang 
863696656c6SJohnny Huang static int otp_print_conf_image(struct otp_image_layout *image_layout)
86469d5fd8fSJohnny Huang {
86579e42a59SJoel Stanley 	const struct otpconf_info *conf_info = info_cb.conf_info;
866a219f6deSJohnny Huang 	u32 *OTPCFG = (u32 *)image_layout->conf;
867a219f6deSJohnny Huang 	u32 *OTPCFG_IGNORE = (u32 *)image_layout->conf_ignore;
868a219f6deSJohnny Huang 	u32 mask;
869a219f6deSJohnny Huang 	u32 dw_offset;
870a219f6deSJohnny Huang 	u32 bit_offset;
871a219f6deSJohnny Huang 	u32 otp_value;
872a219f6deSJohnny Huang 	u32 otp_ignore;
873b458cd62SJohnny Huang 	int fail = 0;
874794e27ecSJohnny Huang 	int rid_num = 0;
87573f11549SJohnny Huang 	char valid_bit[20];
876794e27ecSJohnny Huang 	int fz;
87766f2f8e5SJohnny Huang 	int i;
87873f11549SJohnny Huang 	int j;
87966f2f8e5SJohnny Huang 
880737ed20bSJohnny Huang 	printf("DW    BIT        Value       Description\n");
88166f2f8e5SJohnny Huang 	printf("__________________________________________________________________________\n");
8823cb28812SJohnny Huang 	for (i = 0; i < info_cb.conf_info_len; i++) {
8833cb28812SJohnny Huang 		dw_offset = conf_info[i].dw_offset;
8843cb28812SJohnny Huang 		bit_offset = conf_info[i].bit_offset;
8853cb28812SJohnny Huang 		mask = BIT(conf_info[i].length) - 1;
886b458cd62SJohnny Huang 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
887696656c6SJohnny Huang 		otp_ignore = (OTPCFG_IGNORE[dw_offset] >> bit_offset) & mask;
888b458cd62SJohnny Huang 
889a219f6deSJohnny Huang 		if (otp_ignore == mask)
890b458cd62SJohnny Huang 			continue;
891a219f6deSJohnny Huang 		else if (otp_ignore != 0)
892b458cd62SJohnny Huang 			fail = 1;
893b458cd62SJohnny Huang 
894a219f6deSJohnny Huang 		if (otp_value != conf_info[i].value &&
8953cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_RESERVED &&
8963cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALUE &&
8973cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALID_BIT)
898b458cd62SJohnny Huang 			continue;
899b458cd62SJohnny Huang 		printf("0x%-4X", dw_offset);
900b458cd62SJohnny Huang 
9013cb28812SJohnny Huang 		if (conf_info[i].length == 1) {
9023cb28812SJohnny Huang 			printf("0x%-9X", conf_info[i].bit_offset);
90366f2f8e5SJohnny Huang 		} else {
904b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
9053cb28812SJohnny Huang 			       conf_info[i].bit_offset + conf_info[i].length - 1,
9063cb28812SJohnny Huang 			       conf_info[i].bit_offset);
90766f2f8e5SJohnny Huang 		}
908b458cd62SJohnny Huang 		printf("0x%-10x", otp_value);
909b458cd62SJohnny Huang 
910b458cd62SJohnny Huang 		if (fail) {
911696656c6SJohnny Huang 			printf("Ignore mask error\n");
912a219f6deSJohnny Huang 			continue;
913a219f6deSJohnny Huang 		}
9143cb28812SJohnny Huang 		if (conf_info[i].value == OTP_REG_RESERVED) {
915b458cd62SJohnny Huang 			printf("Reserved\n");
9163cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALUE) {
9173cb28812SJohnny Huang 			printf(conf_info[i].information, otp_value);
918b458cd62SJohnny Huang 			printf("\n");
9193cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALID_BIT) {
920b458cd62SJohnny Huang 			if (otp_value != 0) {
92173f11549SJohnny Huang 				for (j = 0; j < 7; j++) {
922a219f6deSJohnny Huang 					if (otp_value == (1 << j))
92373f11549SJohnny Huang 						valid_bit[j * 2] = '1';
924a219f6deSJohnny Huang 					else
92573f11549SJohnny Huang 						valid_bit[j * 2] = '0';
92673f11549SJohnny Huang 					valid_bit[j * 2 + 1] = ' ';
92773f11549SJohnny Huang 				}
92873f11549SJohnny Huang 				valid_bit[15] = 0;
92973f11549SJohnny Huang 			} else {
93073f11549SJohnny Huang 				strcpy(valid_bit, "0 0 0 0 0 0 0 0\0");
931b458cd62SJohnny Huang 			}
9323cb28812SJohnny Huang 			printf(conf_info[i].information, valid_bit);
933b458cd62SJohnny Huang 			printf("\n");
934b458cd62SJohnny Huang 		} else {
9353cb28812SJohnny Huang 			printf("%s\n", conf_info[i].information);
936b458cd62SJohnny Huang 		}
937b458cd62SJohnny Huang 	}
938b458cd62SJohnny Huang 
939794e27ecSJohnny Huang 	if (OTPCFG[0xa] != 0 || OTPCFG[0xb] != 0) {
940794e27ecSJohnny Huang 		if (OTPCFG_IGNORE[0xa] != 0 && OTPCFG_IGNORE[0xb] != 0) {
941794e27ecSJohnny Huang 			printf("OTP revision ID is invalid.\n");
942794e27ecSJohnny Huang 			fail = 1;
943794e27ecSJohnny Huang 		} else {
944794e27ecSJohnny Huang 			fz = 0;
945794e27ecSJohnny Huang 			for (i = 0; i < 64; i++) {
946794e27ecSJohnny Huang 				if (get_dw_bit(&OTPCFG[0xa], i) == 0) {
947794e27ecSJohnny Huang 					if (!fz)
948794e27ecSJohnny Huang 						fz = 1;
949794e27ecSJohnny Huang 				} else {
950794e27ecSJohnny Huang 					rid_num++;
951794e27ecSJohnny Huang 					if (fz) {
952794e27ecSJohnny Huang 						printf("OTP revision ID is invalid.\n");
953794e27ecSJohnny Huang 						fail = 1;
954794e27ecSJohnny Huang 						break;
955794e27ecSJohnny Huang 					}
956794e27ecSJohnny Huang 				}
957794e27ecSJohnny Huang 			}
958794e27ecSJohnny Huang 		}
959794e27ecSJohnny Huang 		if (fail)
960794e27ecSJohnny Huang 			printf("OTP revision ID\n");
961794e27ecSJohnny Huang 		else
962794e27ecSJohnny Huang 			printf("OTP revision ID: 0x%x\n", rid_num);
963794e27ecSJohnny Huang 		otp_print_revid(&OTPCFG[0xa]);
964794e27ecSJohnny Huang 	}
965794e27ecSJohnny Huang 
966b458cd62SJohnny Huang 	if (fail)
967b458cd62SJohnny Huang 		return OTP_FAILURE;
968b458cd62SJohnny Huang 
96966f2f8e5SJohnny Huang 	return OTP_SUCCESS;
97066f2f8e5SJohnny Huang }
97166f2f8e5SJohnny Huang 
9722d4b0742SJohnny Huang static int otp_print_conf_info(int input_offset)
97366f2f8e5SJohnny Huang {
97479e42a59SJoel Stanley 	const struct otpconf_info *conf_info = info_cb.conf_info;
975a219f6deSJohnny Huang 	u32 OTPCFG[16];
976a219f6deSJohnny Huang 	u32 mask;
977a219f6deSJohnny Huang 	u32 dw_offset;
978a219f6deSJohnny Huang 	u32 bit_offset;
979a219f6deSJohnny Huang 	u32 otp_value;
98073f11549SJohnny Huang 	char valid_bit[20];
98166f2f8e5SJohnny Huang 	int i;
98273f11549SJohnny Huang 	int j;
98366f2f8e5SJohnny Huang 
984dacbba92SJohnny Huang 	otp_soak(0);
985bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++)
986*f347c284SJohnny Huang 		otp_read_conf(i, &OTPCFG[i]);
98766f2f8e5SJohnny Huang 
988b458cd62SJohnny Huang 	printf("DW    BIT        Value       Description\n");
989b458cd62SJohnny Huang 	printf("__________________________________________________________________________\n");
9903cb28812SJohnny Huang 	for (i = 0; i < info_cb.conf_info_len; i++) {
9913cb28812SJohnny Huang 		if (input_offset != -1 && input_offset != conf_info[i].dw_offset)
9922d4b0742SJohnny Huang 			continue;
9933cb28812SJohnny Huang 		dw_offset = conf_info[i].dw_offset;
9943cb28812SJohnny Huang 		bit_offset = conf_info[i].bit_offset;
9953cb28812SJohnny Huang 		mask = BIT(conf_info[i].length) - 1;
996b458cd62SJohnny Huang 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
997b458cd62SJohnny Huang 
998a219f6deSJohnny Huang 		if (otp_value != conf_info[i].value &&
9993cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_RESERVED &&
10003cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALUE &&
10013cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALID_BIT)
1002b458cd62SJohnny Huang 			continue;
1003b458cd62SJohnny Huang 		printf("0x%-4X", dw_offset);
1004b458cd62SJohnny Huang 
10053cb28812SJohnny Huang 		if (conf_info[i].length == 1) {
10063cb28812SJohnny Huang 			printf("0x%-9X", conf_info[i].bit_offset);
1007b458cd62SJohnny Huang 		} else {
1008b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
10093cb28812SJohnny Huang 			       conf_info[i].bit_offset + conf_info[i].length - 1,
10103cb28812SJohnny Huang 			       conf_info[i].bit_offset);
1011b458cd62SJohnny Huang 		}
1012b458cd62SJohnny Huang 		printf("0x%-10x", otp_value);
1013b458cd62SJohnny Huang 
10143cb28812SJohnny Huang 		if (conf_info[i].value == OTP_REG_RESERVED) {
1015b458cd62SJohnny Huang 			printf("Reserved\n");
10163cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALUE) {
10173cb28812SJohnny Huang 			printf(conf_info[i].information, otp_value);
1018b458cd62SJohnny Huang 			printf("\n");
10193cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALID_BIT) {
1020b458cd62SJohnny Huang 			if (otp_value != 0) {
102173f11549SJohnny Huang 				for (j = 0; j < 7; j++) {
1022a219f6deSJohnny Huang 					if (otp_value == (1 << j))
102373f11549SJohnny Huang 						valid_bit[j * 2] = '1';
1024a219f6deSJohnny Huang 					else
102573f11549SJohnny Huang 						valid_bit[j * 2] = '0';
102673f11549SJohnny Huang 					valid_bit[j * 2 + 1] = ' ';
102773f11549SJohnny Huang 				}
102873f11549SJohnny Huang 				valid_bit[15] = 0;
102973f11549SJohnny Huang 			} else {
103073f11549SJohnny Huang 				strcpy(valid_bit, "0 0 0 0 0 0 0 0\0");
1031b458cd62SJohnny Huang 			}
10323cb28812SJohnny Huang 			printf(conf_info[i].information, valid_bit);
1033b458cd62SJohnny Huang 			printf("\n");
1034b458cd62SJohnny Huang 		} else {
10353cb28812SJohnny Huang 			printf("%s\n", conf_info[i].information);
1036b458cd62SJohnny Huang 		}
1037b458cd62SJohnny Huang 	}
1038b458cd62SJohnny Huang 	return OTP_SUCCESS;
103966f2f8e5SJohnny Huang }
104066f2f8e5SJohnny Huang 
10415010032bSJohnny Huang static int otp_print_strap_image(struct otp_image_layout *image_layout)
104276d13988SJohnny Huang {
104379e42a59SJoel Stanley 	const struct otpstrap_info *strap_info = info_cb.strap_info;
1044a219f6deSJohnny Huang 	u32 *OTPSTRAP;
1045a219f6deSJohnny Huang 	u32 *OTPSTRAP_REG_PRO;
1046a219f6deSJohnny Huang 	u32 *OTPSTRAP_PRO;
1047a219f6deSJohnny Huang 	u32 *OTPSTRAP_IGNORE;
104876d13988SJohnny Huang 	int i;
1049a8bd6d8cSJohnny Huang 	int fail = 0;
1050a219f6deSJohnny Huang 	u32 bit_offset;
1051a219f6deSJohnny Huang 	u32 dw_offset;
1052a219f6deSJohnny Huang 	u32 mask;
1053a219f6deSJohnny Huang 	u32 otp_value;
1054a219f6deSJohnny Huang 	u32 otp_reg_protect;
1055a219f6deSJohnny Huang 	u32 otp_protect;
1056a219f6deSJohnny Huang 	u32 otp_ignore;
105776d13988SJohnny Huang 
1058a219f6deSJohnny Huang 	OTPSTRAP = (u32 *)image_layout->strap;
1059a219f6deSJohnny Huang 	OTPSTRAP_PRO = (u32 *)image_layout->strap_pro;
1060a219f6deSJohnny Huang 	OTPSTRAP_IGNORE = (u32 *)image_layout->strap_ignore;
1061e417205bSJohnny Huang 	if (info_cb.version == OTP_A0) {
1062696656c6SJohnny Huang 		OTPSTRAP_REG_PRO = NULL;
1063a8bd6d8cSJohnny Huang 		printf("BIT(hex)   Value       Protect     Description\n");
1064696656c6SJohnny Huang 	} else {
1065a219f6deSJohnny Huang 		OTPSTRAP_REG_PRO = (u32 *)image_layout->strap_reg_pro;
1066de6b0cc4SJohnny Huang 		printf("BIT(hex)   Value       Reg_Protect Protect     Description\n");
1067696656c6SJohnny Huang 	}
1068de6b0cc4SJohnny Huang 	printf("__________________________________________________________________________________________\n");
1069b458cd62SJohnny Huang 
10703cb28812SJohnny Huang 	for (i = 0; i < info_cb.strap_info_len; i++) {
1071696656c6SJohnny Huang 		if (strap_info[i].bit_offset > 31) {
1072a8bd6d8cSJohnny Huang 			dw_offset = 1;
10733cb28812SJohnny Huang 			bit_offset = strap_info[i].bit_offset - 32;
1074a8bd6d8cSJohnny Huang 		} else {
1075a8bd6d8cSJohnny Huang 			dw_offset = 0;
10763cb28812SJohnny Huang 			bit_offset = strap_info[i].bit_offset;
1077a8bd6d8cSJohnny Huang 		}
107876d13988SJohnny Huang 
10793cb28812SJohnny Huang 		mask = BIT(strap_info[i].length) - 1;
1080a8bd6d8cSJohnny Huang 		otp_value = (OTPSTRAP[dw_offset] >> bit_offset) & mask;
1081a8bd6d8cSJohnny Huang 		otp_protect = (OTPSTRAP_PRO[dw_offset] >> bit_offset) & mask;
1082696656c6SJohnny Huang 		otp_ignore = (OTPSTRAP_IGNORE[dw_offset] >> bit_offset) & mask;
1083a8bd6d8cSJohnny Huang 
1084e417205bSJohnny Huang 		if (info_cb.version != OTP_A0)
1085696656c6SJohnny Huang 			otp_reg_protect = (OTPSTRAP_REG_PRO[dw_offset] >> bit_offset) & mask;
10865010032bSJohnny Huang 		else
10875010032bSJohnny Huang 			otp_reg_protect = 0;
1088696656c6SJohnny Huang 
1089a219f6deSJohnny Huang 		if (otp_ignore == mask)
1090a8bd6d8cSJohnny Huang 			continue;
1091a219f6deSJohnny Huang 		else if (otp_ignore != 0)
1092a8bd6d8cSJohnny Huang 			fail = 1;
1093a8bd6d8cSJohnny Huang 
1094a219f6deSJohnny Huang 		if (otp_value != strap_info[i].value &&
10953cb28812SJohnny Huang 		    strap_info[i].value != OTP_REG_RESERVED)
1096a8bd6d8cSJohnny Huang 			continue;
1097a8bd6d8cSJohnny Huang 
10983cb28812SJohnny Huang 		if (strap_info[i].length == 1) {
10993cb28812SJohnny Huang 			printf("0x%-9X", strap_info[i].bit_offset);
1100a8bd6d8cSJohnny Huang 		} else {
1101b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
11023cb28812SJohnny Huang 			       strap_info[i].bit_offset + strap_info[i].length - 1,
11033cb28812SJohnny Huang 			       strap_info[i].bit_offset);
1104a8bd6d8cSJohnny Huang 		}
1105a8bd6d8cSJohnny Huang 		printf("0x%-10x", otp_value);
1106e417205bSJohnny Huang 		if (info_cb.version != OTP_A0)
1107696656c6SJohnny Huang 			printf("0x%-10x", otp_reg_protect);
1108a8bd6d8cSJohnny Huang 		printf("0x%-10x", otp_protect);
1109a8bd6d8cSJohnny Huang 
1110a8bd6d8cSJohnny Huang 		if (fail) {
1111696656c6SJohnny Huang 			printf("Ignore mask error\n");
1112a8bd6d8cSJohnny Huang 		} else {
11133cb28812SJohnny Huang 			if (strap_info[i].value != OTP_REG_RESERVED)
11143cb28812SJohnny Huang 				printf("%s\n", strap_info[i].information);
1115a8bd6d8cSJohnny Huang 			else
1116a8bd6d8cSJohnny Huang 				printf("Reserved\n");
1117a8bd6d8cSJohnny Huang 		}
1118a8bd6d8cSJohnny Huang 	}
1119a8bd6d8cSJohnny Huang 
1120a8bd6d8cSJohnny Huang 	if (fail)
112176d13988SJohnny Huang 		return OTP_FAILURE;
112276d13988SJohnny Huang 
112376d13988SJohnny Huang 	return OTP_SUCCESS;
112476d13988SJohnny Huang }
112576d13988SJohnny Huang 
1126b458cd62SJohnny Huang static int otp_print_strap_info(int view)
112776d13988SJohnny Huang {
112879e42a59SJoel Stanley 	const struct otpstrap_info *strap_info = info_cb.strap_info;
112976d13988SJohnny Huang 	struct otpstrap_status strap_status[64];
113007baa4e8SJohnny Huang 	int i, j;
1131b458cd62SJohnny Huang 	int fail = 0;
1132a219f6deSJohnny Huang 	u32 bit_offset;
1133a219f6deSJohnny Huang 	u32 length;
1134a219f6deSJohnny Huang 	u32 otp_value;
1135a219f6deSJohnny Huang 	u32 otp_protect;
113676d13988SJohnny Huang 
1137541eb887SJohnny Huang 	otp_strap_status(strap_status);
113876d13988SJohnny Huang 
1139b458cd62SJohnny Huang 	if (view) {
1140e417205bSJohnny Huang 		if (info_cb.version == OTP_A0)
114107baa4e8SJohnny Huang 			printf("BIT(hex) Value  Remains  Protect   Description\n");
114283655e91SJohnny Huang 		else
114383655e91SJohnny Huang 			printf("BIT(hex) Value  Remains  Reg_Protect Protect   Description\n");
114407baa4e8SJohnny Huang 		printf("___________________________________________________________________________________________________\n");
1145b458cd62SJohnny Huang 	} else {
1146b458cd62SJohnny Huang 		printf("BIT(hex)   Value       Description\n");
1147b458cd62SJohnny Huang 		printf("________________________________________________________________________________\n");
114876d13988SJohnny Huang 	}
11493cb28812SJohnny Huang 	for (i = 0; i < info_cb.strap_info_len; i++) {
1150b458cd62SJohnny Huang 		otp_value = 0;
11513cb28812SJohnny Huang 		bit_offset = strap_info[i].bit_offset;
11523cb28812SJohnny Huang 		length = strap_info[i].length;
1153b458cd62SJohnny Huang 		for (j = 0; j < length; j++) {
1154c947ef08SJohnny Huang 			otp_value |= strap_status[bit_offset + j].value << j;
1155c947ef08SJohnny Huang 			otp_protect |= strap_status[bit_offset + j].protected << j;
1156b458cd62SJohnny Huang 		}
1157a219f6deSJohnny Huang 		if (otp_value != strap_info[i].value &&
11583cb28812SJohnny Huang 		    strap_info[i].value != OTP_REG_RESERVED)
1159b458cd62SJohnny Huang 			continue;
1160b458cd62SJohnny Huang 		if (view) {
1161b458cd62SJohnny Huang 			for (j = 0; j < length; j++) {
11623cb28812SJohnny Huang 				printf("0x%-7X", strap_info[i].bit_offset + j);
1163b458cd62SJohnny Huang 				printf("0x%-5X", strap_status[bit_offset + j].value);
116407baa4e8SJohnny Huang 				printf("%-9d", strap_status[bit_offset + j].remain_times);
1165e417205bSJohnny Huang 				if (info_cb.version != OTP_A0)
1166e1a7245eSJohnny Huang 					printf("0x%-10X", strap_status[bit_offset + j].reg_protected);
1167e1a7245eSJohnny Huang 				printf("0x%-7X", strap_status[bit_offset + j].protected);
11683cb28812SJohnny Huang 				if (strap_info[i].value == OTP_REG_RESERVED) {
1169b458cd62SJohnny Huang 					printf(" Reserved\n");
1170b458cd62SJohnny Huang 					continue;
1171b458cd62SJohnny Huang 				}
1172b458cd62SJohnny Huang 				if (length == 1) {
11733cb28812SJohnny Huang 					printf(" %s\n", strap_info[i].information);
1174b458cd62SJohnny Huang 					continue;
117576d13988SJohnny Huang 				}
117676d13988SJohnny Huang 
1177b458cd62SJohnny Huang 				if (j == 0)
11783cb28812SJohnny Huang 					printf("/%s\n", strap_info[i].information);
1179b458cd62SJohnny Huang 				else if (j == length - 1)
1180b458cd62SJohnny Huang 					printf("\\ \"\n");
1181b458cd62SJohnny Huang 				else
1182b458cd62SJohnny Huang 					printf("| \"\n");
118376d13988SJohnny Huang 			}
1184b458cd62SJohnny Huang 		} else {
1185c947ef08SJohnny Huang 			if (length == 1) {
11863cb28812SJohnny Huang 				printf("0x%-9X", strap_info[i].bit_offset);
1187b458cd62SJohnny Huang 			} else {
1188b458cd62SJohnny Huang 				printf("0x%-2X:0x%-4X",
1189b458cd62SJohnny Huang 				       bit_offset + length - 1, bit_offset);
1190b458cd62SJohnny Huang 			}
1191b458cd62SJohnny Huang 
1192b458cd62SJohnny Huang 			printf("0x%-10X", otp_value);
1193b458cd62SJohnny Huang 
11943cb28812SJohnny Huang 			if (strap_info[i].value != OTP_REG_RESERVED)
11953cb28812SJohnny Huang 				printf("%s\n", strap_info[i].information);
1196b458cd62SJohnny Huang 			else
1197b458cd62SJohnny Huang 				printf("Reserved\n");
1198b458cd62SJohnny Huang 		}
1199b458cd62SJohnny Huang 	}
1200b458cd62SJohnny Huang 
1201b458cd62SJohnny Huang 	if (fail)
1202b458cd62SJohnny Huang 		return OTP_FAILURE;
1203b458cd62SJohnny Huang 
1204b458cd62SJohnny Huang 	return OTP_SUCCESS;
1205b458cd62SJohnny Huang }
1206b458cd62SJohnny Huang 
1207*f347c284SJohnny Huang static int otp_print_data_image(struct otp_image_layout *image_layout)
120869d5fd8fSJohnny Huang {
120969d5fd8fSJohnny Huang 	int key_id, key_offset, last, key_type, key_length, exp_length;
121079e42a59SJoel Stanley 	const struct otpkey_type *key_info_array = info_cb.key_info;
12119a4fe690SJohnny Huang 	struct otpkey_type key_info;
1212a219f6deSJohnny Huang 	u32 *buf;
1213a219f6deSJohnny Huang 	u8 *byte_buf;
12149d998018SJohnny Huang 	char empty = 1;
121569d5fd8fSJohnny Huang 	int i = 0, len = 0;
12169a4fe690SJohnny Huang 	int j;
121754552c69SJohnny Huang 
1218696656c6SJohnny Huang 	byte_buf = image_layout->data;
1219a219f6deSJohnny Huang 	buf = (u32 *)byte_buf;
12209d998018SJohnny Huang 
12219d998018SJohnny Huang 	for (i = 0; i < 16; i++) {
1222a219f6deSJohnny Huang 		if (buf[i] != 0)
12239d998018SJohnny Huang 			empty = 0;
12249d998018SJohnny Huang 	}
12259d998018SJohnny Huang 	if (empty)
1226*f347c284SJohnny Huang 		return OTP_SUCCESS;
12279d998018SJohnny Huang 
12289d998018SJohnny Huang 	i = 0;
122969d5fd8fSJohnny Huang 	while (1) {
123069d5fd8fSJohnny Huang 		key_id = buf[i] & 0x7;
123169d5fd8fSJohnny Huang 		key_offset = buf[i] & 0x1ff8;
123269d5fd8fSJohnny Huang 		last = (buf[i] >> 13) & 1;
123369d5fd8fSJohnny Huang 		key_type = (buf[i] >> 14) & 0xf;
123469d5fd8fSJohnny Huang 		key_length = (buf[i] >> 18) & 0x3;
123569d5fd8fSJohnny Huang 		exp_length = (buf[i] >> 20) & 0xfff;
12369a4fe690SJohnny Huang 
12379a4fe690SJohnny Huang 		for (j = 0; j < info_cb.key_info_len; j++) {
12389a4fe690SJohnny Huang 			if (key_type == key_info_array[j].value) {
12399a4fe690SJohnny Huang 				key_info = key_info_array[j];
12409a4fe690SJohnny Huang 				break;
12419a4fe690SJohnny Huang 			}
12429a4fe690SJohnny Huang 		}
12439a4fe690SJohnny Huang 
12447f795e57SJohnny Huang 		printf("\nKey[%d]:\n", i);
124569d5fd8fSJohnny Huang 		printf("Key Type: ");
12469a4fe690SJohnny Huang 		printf("%s\n", key_info.information);
12479a4fe690SJohnny Huang 
12489a4fe690SJohnny Huang 		if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
124969d5fd8fSJohnny Huang 			printf("HMAC SHA Type: ");
125069d5fd8fSJohnny Huang 			switch (key_length) {
125169d5fd8fSJohnny Huang 			case 0:
125269d5fd8fSJohnny Huang 				printf("HMAC(SHA224)\n");
125369d5fd8fSJohnny Huang 				break;
125469d5fd8fSJohnny Huang 			case 1:
125569d5fd8fSJohnny Huang 				printf("HMAC(SHA256)\n");
125669d5fd8fSJohnny Huang 				break;
125769d5fd8fSJohnny Huang 			case 2:
125869d5fd8fSJohnny Huang 				printf("HMAC(SHA384)\n");
125969d5fd8fSJohnny Huang 				break;
126069d5fd8fSJohnny Huang 			case 3:
126169d5fd8fSJohnny Huang 				printf("HMAC(SHA512)\n");
126269d5fd8fSJohnny Huang 				break;
126369d5fd8fSJohnny Huang 			}
1264181f72d8SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA_PRIV ||
1265181f72d8SJohnny Huang 			   key_info.key_type == OTP_KEY_TYPE_RSA_PUB) {
126669d5fd8fSJohnny Huang 			printf("RSA SHA Type: ");
126769d5fd8fSJohnny Huang 			switch (key_length) {
126869d5fd8fSJohnny Huang 			case 0:
126969d5fd8fSJohnny Huang 				printf("RSA1024\n");
127069d5fd8fSJohnny Huang 				len = 0x100;
127169d5fd8fSJohnny Huang 				break;
127269d5fd8fSJohnny Huang 			case 1:
127369d5fd8fSJohnny Huang 				printf("RSA2048\n");
127469d5fd8fSJohnny Huang 				len = 0x200;
127569d5fd8fSJohnny Huang 				break;
127669d5fd8fSJohnny Huang 			case 2:
127769d5fd8fSJohnny Huang 				printf("RSA3072\n");
127869d5fd8fSJohnny Huang 				len = 0x300;
127969d5fd8fSJohnny Huang 				break;
128069d5fd8fSJohnny Huang 			case 3:
128169d5fd8fSJohnny Huang 				printf("RSA4096\n");
128269d5fd8fSJohnny Huang 				len = 0x400;
128369d5fd8fSJohnny Huang 				break;
128469d5fd8fSJohnny Huang 			}
128569d5fd8fSJohnny Huang 			printf("RSA exponent bit length: %d\n", exp_length);
128669d5fd8fSJohnny Huang 		}
12879a4fe690SJohnny Huang 		if (key_info.need_id)
128869d5fd8fSJohnny Huang 			printf("Key Number ID: %d\n", key_id);
128969d5fd8fSJohnny Huang 		printf("Key Value:\n");
12909a4fe690SJohnny Huang 		if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
129169d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], 0x40);
12929a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_AES) {
12939a4fe690SJohnny Huang 			printf("AES Key:\n");
12949a4fe690SJohnny Huang 			buf_print(&byte_buf[key_offset], 0x20);
1295e417205bSJohnny Huang 			if (info_cb.version == OTP_A0) {
12969a4fe690SJohnny Huang 				printf("AES IV:\n");
12979a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x10);
12989a4fe690SJohnny Huang 			}
12999a4fe690SJohnny Huang 
13009a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_VAULT) {
1301e417205bSJohnny Huang 			if (info_cb.version == OTP_A0) {
130269d5fd8fSJohnny Huang 				printf("AES Key:\n");
130369d5fd8fSJohnny Huang 				buf_print(&byte_buf[key_offset], 0x20);
130469d5fd8fSJohnny Huang 				printf("AES IV:\n");
130569d5fd8fSJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x10);
13065fdde29fSJohnny Huang 			} else {
13079a4fe690SJohnny Huang 				printf("AES Key 1:\n");
13089a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset], 0x20);
13099a4fe690SJohnny Huang 				printf("AES Key 2:\n");
13109a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x20);
13119a4fe690SJohnny Huang 			}
1312181f72d8SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA_PRIV) {
131369d5fd8fSJohnny Huang 			printf("RSA mod:\n");
131469d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], len / 2);
131569d5fd8fSJohnny Huang 			printf("RSA exp:\n");
131669d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset + (len / 2)], len / 2);
1317181f72d8SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA_PUB) {
1318181f72d8SJohnny Huang 			printf("RSA mod:\n");
1319181f72d8SJohnny Huang 			buf_print(&byte_buf[key_offset], len / 2);
1320181f72d8SJohnny Huang 			printf("RSA exp:\n");
1321a219f6deSJohnny Huang 			buf_print((u8 *)"\x01\x00\x01", 3);
132269d5fd8fSJohnny Huang 		}
132369d5fd8fSJohnny Huang 		if (last)
132469d5fd8fSJohnny Huang 			break;
132569d5fd8fSJohnny Huang 		i++;
132669d5fd8fSJohnny Huang 	}
1327*f347c284SJohnny Huang 	return OTP_SUCCESS;
1328*f347c284SJohnny Huang }
1329*f347c284SJohnny Huang 
1330*f347c284SJohnny Huang static int otp_strap_image_confirm(struct otp_image_layout *image_layout)
1331*f347c284SJohnny Huang {
1332*f347c284SJohnny Huang 	int i;
1333*f347c284SJohnny Huang 	u32 *strap;
1334*f347c284SJohnny Huang 	u32 *strap_ignore;
1335*f347c284SJohnny Huang 	u32 *strap_reg_protect;
1336*f347c284SJohnny Huang 	u32 *strap_pro;
1337*f347c284SJohnny Huang 	int bit, pbit, ibit, rpbit;
1338*f347c284SJohnny Huang 	int fail = 0;
1339*f347c284SJohnny Huang 	int ret;
1340*f347c284SJohnny Huang 	struct otpstrap_status otpstrap[64];
1341*f347c284SJohnny Huang 
1342*f347c284SJohnny Huang 	strap = (u32 *)image_layout->strap;
1343*f347c284SJohnny Huang 	strap_pro = (u32 *)image_layout->strap_pro;
1344*f347c284SJohnny Huang 	strap_ignore = (u32 *)image_layout->strap_ignore;
1345*f347c284SJohnny Huang 	strap_reg_protect = (u32 *)image_layout->strap_reg_pro;
1346*f347c284SJohnny Huang 
1347*f347c284SJohnny Huang 	otp_strap_status(otpstrap);
1348*f347c284SJohnny Huang 	for (i = 0; i < 64; i++) {
1349*f347c284SJohnny Huang 		if (i < 32) {
1350*f347c284SJohnny Huang 			bit = (strap[0] >> i) & 0x1;
1351*f347c284SJohnny Huang 			ibit = (strap_ignore[0] >> i) & 0x1;
1352*f347c284SJohnny Huang 			pbit = (strap_pro[0] >> i) & 0x1;
1353*f347c284SJohnny Huang 		} else {
1354*f347c284SJohnny Huang 			bit = (strap[1] >> (i - 32)) & 0x1;
1355*f347c284SJohnny Huang 			ibit = (strap_ignore[1] >> (i - 32)) & 0x1;
1356*f347c284SJohnny Huang 			pbit = (strap_pro[1] >> (i - 32)) & 0x1;
1357*f347c284SJohnny Huang 		}
1358*f347c284SJohnny Huang 
1359*f347c284SJohnny Huang 		if (info_cb.version != OTP_A0) {
1360*f347c284SJohnny Huang 			if (i < 32)
1361*f347c284SJohnny Huang 				rpbit = (strap_reg_protect[0] >> i) & 0x1;
1362*f347c284SJohnny Huang 			else
1363*f347c284SJohnny Huang 				rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1;
1364*f347c284SJohnny Huang 		} else {
1365*f347c284SJohnny Huang 			rpbit = 0;
1366*f347c284SJohnny Huang 		}
1367*f347c284SJohnny Huang 		ret = otp_strap_bit_confirm(&otpstrap[i], i, ibit, bit, pbit, rpbit);
1368*f347c284SJohnny Huang 
1369*f347c284SJohnny Huang 		if (ret == OTP_FAILURE)
1370*f347c284SJohnny Huang 			fail = 1;
1371*f347c284SJohnny Huang 	}
1372*f347c284SJohnny Huang 	if (fail == 1)
1373*f347c284SJohnny Huang 		return OTP_FAILURE;
1374*f347c284SJohnny Huang 	else
1375*f347c284SJohnny Huang 		return OTP_SUCCESS;
1376*f347c284SJohnny Huang }
1377*f347c284SJohnny Huang 
1378*f347c284SJohnny Huang static int otp_prog_data(struct otp_image_layout *image_layout)
1379*f347c284SJohnny Huang {
1380*f347c284SJohnny Huang 	int i;
1381*f347c284SJohnny Huang 	int ret;
1382*f347c284SJohnny Huang 	int data_dw;
1383*f347c284SJohnny Huang 	u32 data[2048];
1384*f347c284SJohnny Huang 	u32 *buf;
1385*f347c284SJohnny Huang 	u32 *buf_ignore;
1386*f347c284SJohnny Huang 	u32 data_masked;
1387*f347c284SJohnny Huang 	u32 buf_masked;
1388*f347c284SJohnny Huang 
1389*f347c284SJohnny Huang 	buf = (u32 *)image_layout->data;
1390*f347c284SJohnny Huang 	buf_ignore = (u32 *)image_layout->data_ignore;
1391*f347c284SJohnny Huang 
1392*f347c284SJohnny Huang 	data_dw = image_layout->data_length / 4;
1393*f347c284SJohnny Huang 
1394*f347c284SJohnny Huang 	printf("Read OTP Data:\n");
1395*f347c284SJohnny Huang 
1396*f347c284SJohnny Huang 	for (i = 0; i < data_dw - 2 ; i += 2)
1397*f347c284SJohnny Huang 		otp_read_data(i, &data[i]);
1398*f347c284SJohnny Huang 
1399*f347c284SJohnny Huang 	printf("Check writable...\n");
1400*f347c284SJohnny Huang 	// ignore last two dw, the last two dw is used for slt otp write check.
1401*f347c284SJohnny Huang 	for (i = 0; i < data_dw - 2; i++) {
1402*f347c284SJohnny Huang 		data_masked = data[i]  & ~buf_ignore[i];
1403*f347c284SJohnny Huang 		buf_masked  = buf[i] & ~buf_ignore[i];
1404*f347c284SJohnny Huang 		if (data_masked == buf_masked)
1405*f347c284SJohnny Huang 			continue;
1406*f347c284SJohnny Huang 		if (i % 2 == 0) {
1407*f347c284SJohnny Huang 			if ((data_masked | buf_masked) == buf_masked) {
1408*f347c284SJohnny Huang 				continue;
1409*f347c284SJohnny Huang 			} else {
1410*f347c284SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
1411*f347c284SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
1412*f347c284SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
1413*f347c284SJohnny Huang 				printf("Mask    [%x] = %x\n", i, ~buf_ignore[i]);
1414*f347c284SJohnny Huang 				return OTP_FAILURE;
1415*f347c284SJohnny Huang 			}
1416*f347c284SJohnny Huang 		} else {
1417*f347c284SJohnny Huang 			if ((data_masked & buf_masked) == buf_masked) {
1418*f347c284SJohnny Huang 				continue;
1419*f347c284SJohnny Huang 			} else {
1420*f347c284SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
1421*f347c284SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
1422*f347c284SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
1423*f347c284SJohnny Huang 				printf("Mask    [%x] = %x\n", i, ~buf_ignore[i]);
1424*f347c284SJohnny Huang 				return OTP_FAILURE;
1425*f347c284SJohnny Huang 			}
1426*f347c284SJohnny Huang 		}
1427*f347c284SJohnny Huang 	}
1428*f347c284SJohnny Huang 
1429*f347c284SJohnny Huang 	printf("Start Programing...\n");
1430*f347c284SJohnny Huang 
1431*f347c284SJohnny Huang 	// programing ecc region first
1432*f347c284SJohnny Huang 	for (i = 1792; i < 2046; i += 2) {
1433*f347c284SJohnny Huang 		ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i);
1434*f347c284SJohnny Huang 		if (ret != OTP_SUCCESS) {
1435*f347c284SJohnny Huang 			printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n",
1436*f347c284SJohnny Huang 			       i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]);
1437*f347c284SJohnny Huang 			return ret;
1438*f347c284SJohnny Huang 		}
1439*f347c284SJohnny Huang 	}
1440*f347c284SJohnny Huang 
1441*f347c284SJohnny Huang 	for (i = 0; i < 1792; i += 2) {
1442*f347c284SJohnny Huang 		ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i);
1443*f347c284SJohnny Huang 		if (ret != OTP_SUCCESS) {
1444*f347c284SJohnny Huang 			printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n",
1445*f347c284SJohnny Huang 			       i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]);
1446*f347c284SJohnny Huang 			return ret;
1447*f347c284SJohnny Huang 		}
1448*f347c284SJohnny Huang 	}
1449*f347c284SJohnny Huang 	otp_soak(0);
1450*f347c284SJohnny Huang 	return OTP_SUCCESS;
1451*f347c284SJohnny Huang }
1452*f347c284SJohnny Huang 
1453*f347c284SJohnny Huang static int otp_prog_strap(struct otp_image_layout *image_layout)
1454*f347c284SJohnny Huang {
1455*f347c284SJohnny Huang 	u32 *strap;
1456*f347c284SJohnny Huang 	u32 *strap_ignore;
1457*f347c284SJohnny Huang 	u32 *strap_pro;
1458*f347c284SJohnny Huang 	u32 *strap_reg_protect;
1459*f347c284SJohnny Huang 	u32 prog_address;
1460*f347c284SJohnny Huang 	int i;
1461*f347c284SJohnny Huang 	int bit, pbit, ibit, offset, rpbit;
1462*f347c284SJohnny Huang 	int fail = 0;
1463*f347c284SJohnny Huang 	int ret;
1464*f347c284SJohnny Huang 	int prog_flag = 0;
1465*f347c284SJohnny Huang 	struct otpstrap_status otpstrap[64];
1466*f347c284SJohnny Huang 
1467*f347c284SJohnny Huang 	strap = (u32 *)image_layout->strap;
1468*f347c284SJohnny Huang 	strap_pro = (u32 *)image_layout->strap_pro;
1469*f347c284SJohnny Huang 	strap_ignore = (u32 *)image_layout->strap_ignore;
1470*f347c284SJohnny Huang 	strap_reg_protect = (u32 *)image_layout->strap_reg_pro;
1471*f347c284SJohnny Huang 
1472*f347c284SJohnny Huang 	printf("Read OTP Strap Region:\n");
1473*f347c284SJohnny Huang 	otp_strap_status(otpstrap);
1474*f347c284SJohnny Huang 
1475*f347c284SJohnny Huang 	printf("Check writable...\n");
1476*f347c284SJohnny Huang 	if (otp_strap_image_confirm(image_layout) == OTP_FAILURE) {
1477*f347c284SJohnny Huang 		printf("Input image can't program into OTP, please check.\n");
1478*f347c284SJohnny Huang 		return OTP_FAILURE;
1479*f347c284SJohnny Huang 	}
1480*f347c284SJohnny Huang 
1481*f347c284SJohnny Huang 	for (i = 0; i < 64; i++) {
1482*f347c284SJohnny Huang 		prog_address = 0x800;
1483*f347c284SJohnny Huang 		if (i < 32) {
1484*f347c284SJohnny Huang 			offset = i;
1485*f347c284SJohnny Huang 			bit = (strap[0] >> offset) & 0x1;
1486*f347c284SJohnny Huang 			ibit = (strap_ignore[0] >> offset) & 0x1;
1487*f347c284SJohnny Huang 			pbit = (strap_pro[0] >> offset) & 0x1;
1488*f347c284SJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200;
1489*f347c284SJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2;
1490*f347c284SJohnny Huang 
1491*f347c284SJohnny Huang 		} else {
1492*f347c284SJohnny Huang 			offset = (i - 32);
1493*f347c284SJohnny Huang 			bit = (strap[1] >> offset) & 0x1;
1494*f347c284SJohnny Huang 			ibit = (strap_ignore[1] >> offset) & 0x1;
1495*f347c284SJohnny Huang 			pbit = (strap_pro[1] >> offset) & 0x1;
1496*f347c284SJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200;
1497*f347c284SJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2;
1498*f347c284SJohnny Huang 		}
1499*f347c284SJohnny Huang 		if (info_cb.version != OTP_A0) {
1500*f347c284SJohnny Huang 			if (i < 32)
1501*f347c284SJohnny Huang 				rpbit = (strap_reg_protect[0] >> i) & 0x1;
1502*f347c284SJohnny Huang 			else
1503*f347c284SJohnny Huang 				rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1;
1504*f347c284SJohnny Huang 		} else {
1505*f347c284SJohnny Huang 			rpbit = 0;
1506*f347c284SJohnny Huang 		}
1507*f347c284SJohnny Huang 
1508*f347c284SJohnny Huang 		if (ibit == 1)
1509*f347c284SJohnny Huang 			continue;
1510*f347c284SJohnny Huang 		if (bit == otpstrap[i].value)
1511*f347c284SJohnny Huang 			prog_flag = 0;
1512*f347c284SJohnny Huang 		else
1513*f347c284SJohnny Huang 			prog_flag = 1;
1514*f347c284SJohnny Huang 
1515*f347c284SJohnny Huang 		if (otpstrap[i].protected == 1 && prog_flag) {
1516*f347c284SJohnny Huang 			fail = 1;
1517*f347c284SJohnny Huang 			continue;
1518*f347c284SJohnny Huang 		}
1519*f347c284SJohnny Huang 		if (otpstrap[i].remain_times == 0 && prog_flag) {
1520*f347c284SJohnny Huang 			fail = 1;
1521*f347c284SJohnny Huang 			continue;
1522*f347c284SJohnny Huang 		}
1523*f347c284SJohnny Huang 
1524*f347c284SJohnny Huang 		if (prog_flag) {
1525*f347c284SJohnny Huang 			ret = otp_prog_dc_b(1, prog_address, offset);
1526*f347c284SJohnny Huang 			if (ret)
1527*f347c284SJohnny Huang 				return OTP_FAILURE;
1528*f347c284SJohnny Huang 		}
1529*f347c284SJohnny Huang 
1530*f347c284SJohnny Huang 		if (rpbit == 1 && info_cb.version != OTP_A0) {
1531*f347c284SJohnny Huang 			prog_address = 0x800;
1532*f347c284SJohnny Huang 			if (i < 32)
1533*f347c284SJohnny Huang 				prog_address |= 0x608;
1534*f347c284SJohnny Huang 			else
1535*f347c284SJohnny Huang 				prog_address |= 0x60a;
1536*f347c284SJohnny Huang 
1537*f347c284SJohnny Huang 			ret = otp_prog_dc_b(1, prog_address, offset);
1538*f347c284SJohnny Huang 			if (ret)
1539*f347c284SJohnny Huang 				return OTP_FAILURE;
1540*f347c284SJohnny Huang 		}
1541*f347c284SJohnny Huang 
1542*f347c284SJohnny Huang 		if (pbit != 0) {
1543*f347c284SJohnny Huang 			prog_address = 0x800;
1544*f347c284SJohnny Huang 			if (i < 32)
1545*f347c284SJohnny Huang 				prog_address |= 0x60c;
1546*f347c284SJohnny Huang 			else
1547*f347c284SJohnny Huang 				prog_address |= 0x60e;
1548*f347c284SJohnny Huang 
1549*f347c284SJohnny Huang 			ret = otp_prog_dc_b(1, prog_address, offset);
1550*f347c284SJohnny Huang 			if (ret)
1551*f347c284SJohnny Huang 				return OTP_FAILURE;
1552*f347c284SJohnny Huang 		}
1553*f347c284SJohnny Huang 	}
1554*f347c284SJohnny Huang 	otp_soak(0);
1555*f347c284SJohnny Huang 	if (fail == 1)
1556*f347c284SJohnny Huang 		return OTP_FAILURE;
1557*f347c284SJohnny Huang 	return OTP_SUCCESS;
155869d5fd8fSJohnny Huang }
155969d5fd8fSJohnny Huang 
15605010032bSJohnny Huang static int otp_prog_conf(struct otp_image_layout *image_layout)
156169d5fd8fSJohnny Huang {
1562a6d0d645SJohnny Huang 	int i, k;
1563d90825e2SJohnny Huang 	int pass = 0;
1564a219f6deSJohnny Huang 	u32 prog_address;
1565a219f6deSJohnny Huang 	u32 data[16];
1566a219f6deSJohnny Huang 	u32 compare[2];
1567a219f6deSJohnny Huang 	u32 *conf = (u32 *)image_layout->conf;
1568a219f6deSJohnny Huang 	u32 *conf_ignore = (u32 *)image_layout->conf_ignore;
1569a219f6deSJohnny Huang 	u32 data_masked;
1570a219f6deSJohnny Huang 	u32 buf_masked;
157169d5fd8fSJohnny Huang 
1572a6d0d645SJohnny Huang 	printf("Read OTP Config Region:\n");
1573a6d0d645SJohnny Huang 
1574bb34a7bfSJohnny Huang 	for (i = 0; i < 16 ; i++) {
157569d5fd8fSJohnny Huang 		prog_address = 0x800;
1576a6d0d645SJohnny Huang 		prog_address |= (i / 8) * 0x200;
1577a6d0d645SJohnny Huang 		prog_address |= (i % 8) * 0x2;
1578a6d0d645SJohnny Huang 		otp_read_data(prog_address, &data[i]);
1579a6d0d645SJohnny Huang 	}
1580a6d0d645SJohnny Huang 
1581a6d0d645SJohnny Huang 	printf("Check writable...\n");
1582bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++) {
15835010032bSJohnny Huang 		data_masked = data[i]  & ~conf_ignore[i];
15845010032bSJohnny Huang 		buf_masked  = conf[i] & ~conf_ignore[i];
1585d90825e2SJohnny Huang 		if (data_masked == buf_masked)
158669d5fd8fSJohnny Huang 			continue;
1587d90825e2SJohnny Huang 		if ((data_masked | buf_masked) == buf_masked) {
1588a6d0d645SJohnny Huang 			continue;
1589a6d0d645SJohnny Huang 		} else {
1590a6d0d645SJohnny Huang 			printf("Input image can't program into OTP, please check.\n");
1591a6af4a17SJohnny Huang 			printf("OTPCFG[%X] = %x\n", i, data[i]);
15925010032bSJohnny Huang 			printf("Input [%X] = %x\n", i, conf[i]);
15935010032bSJohnny Huang 			printf("Mask  [%X] = %x\n", i, ~conf_ignore[i]);
15942a856b9aSJohnny Huang 			return OTP_FAILURE;
1595a6d0d645SJohnny Huang 		}
1596a6d0d645SJohnny Huang 	}
1597a6d0d645SJohnny Huang 
1598a6d0d645SJohnny Huang 	printf("Start Programing...\n");
1599d90825e2SJohnny Huang 	otp_soak(0);
1600bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++) {
16015010032bSJohnny Huang 		data_masked = data[i]  & ~conf_ignore[i];
16025010032bSJohnny Huang 		buf_masked  = conf[i] & ~conf_ignore[i];
1603a6d0d645SJohnny Huang 		prog_address = 0x800;
1604a6d0d645SJohnny Huang 		prog_address |= (i / 8) * 0x200;
1605a6d0d645SJohnny Huang 		prog_address |= (i % 8) * 0x2;
1606bb34a7bfSJohnny Huang 		if (data_masked == buf_masked) {
1607bb34a7bfSJohnny Huang 			pass = 1;
1608a6d0d645SJohnny Huang 			continue;
1609bb34a7bfSJohnny Huang 		}
1610de6fbf1cSJohnny Huang 
1611de6fbf1cSJohnny Huang 		otp_soak(1);
16125010032bSJohnny Huang 		otp_prog_dw(conf[i], conf_ignore[i], prog_address);
1613a6d0d645SJohnny Huang 
161469d5fd8fSJohnny Huang 		pass = 0;
161569d5fd8fSJohnny Huang 		for (k = 0; k < RETRY; k++) {
16165010032bSJohnny Huang 			if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) {
1617de6fbf1cSJohnny Huang 				otp_soak(2);
1618feea3fdfSJohnny Huang 				otp_prog_dw(compare[0], conf_ignore[i], prog_address);
16195010032bSJohnny Huang 				if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) {
1620de6fbf1cSJohnny Huang 					otp_soak(1);
1621de6fbf1cSJohnny Huang 				} else {
1622de6fbf1cSJohnny Huang 					pass = 1;
1623de6fbf1cSJohnny Huang 					break;
1624de6fbf1cSJohnny Huang 				}
1625a6d0d645SJohnny Huang 			} else {
162669d5fd8fSJohnny Huang 				pass = 1;
162769d5fd8fSJohnny Huang 				break;
162869d5fd8fSJohnny Huang 			}
162969d5fd8fSJohnny Huang 		}
1630bb34a7bfSJohnny Huang 		if (pass == 0) {
1631bb34a7bfSJohnny Huang 			printf("address: %08x, data: %08x, buffer: %08x, mask: %08x\n",
16325010032bSJohnny Huang 			       i, data[i], conf[i], conf_ignore[i]);
1633bb34a7bfSJohnny Huang 			break;
1634bb34a7bfSJohnny Huang 		}
1635a6d0d645SJohnny Huang 	}
1636a6d0d645SJohnny Huang 
1637de6fbf1cSJohnny Huang 	otp_soak(0);
163869d5fd8fSJohnny Huang 	if (!pass)
16392a856b9aSJohnny Huang 		return OTP_FAILURE;
1640a6d0d645SJohnny Huang 
16412a856b9aSJohnny Huang 	return OTP_SUCCESS;
164269d5fd8fSJohnny Huang }
164369d5fd8fSJohnny Huang 
1644*f347c284SJohnny Huang static int otp_verify_image(u8 *src_buf, u32 length, u8 *digest_buf)
1645696656c6SJohnny Huang {
1646696656c6SJohnny Huang 	sha256_context ctx;
1647696656c6SJohnny Huang 	u8 digest_ret[CHECKSUM_LEN];
1648696656c6SJohnny Huang 
1649696656c6SJohnny Huang 	sha256_starts(&ctx);
1650696656c6SJohnny Huang 	sha256_update(&ctx, src_buf, length);
1651696656c6SJohnny Huang 	sha256_finish(&ctx, digest_ret);
1652696656c6SJohnny Huang 
1653696656c6SJohnny Huang 	if (!memcmp(digest_buf, digest_ret, CHECKSUM_LEN))
1654*f347c284SJohnny Huang 		return OTP_SUCCESS;
1655*f347c284SJohnny Huang 	return OTP_FAILURE;
1656696656c6SJohnny Huang }
1657696656c6SJohnny Huang 
1658*f347c284SJohnny Huang static int otp_prog_image(int addr, int nconfirm)
165969d5fd8fSJohnny Huang {
166069d5fd8fSJohnny Huang 	int ret;
16619a4fe690SJohnny Huang 	int image_version = 0;
1662696656c6SJohnny Huang 	struct otp_header *otp_header;
1663696656c6SJohnny Huang 	struct otp_image_layout image_layout;
1664696656c6SJohnny Huang 	int image_size;
1665a219f6deSJohnny Huang 	u8 *buf;
1666a219f6deSJohnny Huang 	u8 *checksum;
166769d5fd8fSJohnny Huang 
1668696656c6SJohnny Huang 	otp_header = map_physmem(addr, sizeof(struct otp_header), MAP_WRBACK);
1669696656c6SJohnny Huang 	if (!otp_header) {
167069d5fd8fSJohnny Huang 		puts("Failed to map physical memory\n");
16712a856b9aSJohnny Huang 		return OTP_FAILURE;
167269d5fd8fSJohnny Huang 	}
1673d90825e2SJohnny Huang 
1674696656c6SJohnny Huang 	image_size = OTP_IMAGE_SIZE(otp_header->image_info);
1675696656c6SJohnny Huang 	unmap_physmem(otp_header, MAP_WRBACK);
1676696656c6SJohnny Huang 
1677696656c6SJohnny Huang 	buf = map_physmem(addr, image_size + CHECKSUM_LEN, MAP_WRBACK);
1678696656c6SJohnny Huang 
1679696656c6SJohnny Huang 	if (!buf) {
1680696656c6SJohnny Huang 		puts("Failed to map physical memory\n");
1681696656c6SJohnny Huang 		return OTP_FAILURE;
1682696656c6SJohnny Huang 	}
1683696656c6SJohnny Huang 	otp_header = (struct otp_header *)buf;
1684696656c6SJohnny Huang 	checksum = buf + otp_header->checksum_offset;
1685696656c6SJohnny Huang 
1686696656c6SJohnny Huang 	if (strcmp(OTP_MAGIC, (char *)otp_header->otp_magic) != 0) {
1687696656c6SJohnny Huang 		puts("Image is invalid\n");
1688696656c6SJohnny Huang 		return OTP_FAILURE;
1689696656c6SJohnny Huang 	}
1690696656c6SJohnny Huang 
16915010032bSJohnny Huang 	image_layout.data_length = (int)(OTP_REGION_SIZE(otp_header->data_info) / 2);
16925010032bSJohnny Huang 	image_layout.data = buf + OTP_REGION_OFFSET(otp_header->data_info);
16935010032bSJohnny Huang 	image_layout.data_ignore = image_layout.data + image_layout.data_length;
16945010032bSJohnny Huang 
16955010032bSJohnny Huang 	image_layout.conf_length = (int)(OTP_REGION_SIZE(otp_header->config_info) / 2);
1696696656c6SJohnny Huang 	image_layout.conf = buf + OTP_REGION_OFFSET(otp_header->config_info);
16975010032bSJohnny Huang 	image_layout.conf_ignore = image_layout.conf + image_layout.conf_length;
1698696656c6SJohnny Huang 
1699696656c6SJohnny Huang 	image_layout.strap = buf + OTP_REGION_OFFSET(otp_header->strap_info);
1700696656c6SJohnny Huang 
1701696656c6SJohnny Huang 	if (!strcmp("A0", (char *)otp_header->otp_version)) {
1702e417205bSJohnny Huang 		image_version = OTP_A0;
17035010032bSJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 3);
17045010032bSJohnny Huang 		image_layout.strap_pro = image_layout.strap + image_layout.strap_length;
17055010032bSJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 2 * image_layout.strap_length;
1706696656c6SJohnny Huang 	} else if (!strcmp("A1", (char *)otp_header->otp_version)) {
1707e417205bSJohnny Huang 		image_version = OTP_A1;
17085010032bSJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4);
17095010032bSJohnny Huang 		image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length;
17105010032bSJohnny Huang 		image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length;
17115010032bSJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length;
17125fdde29fSJohnny Huang 	} else if (!strcmp("A2", (char *)otp_header->otp_version)) {
1713e417205bSJohnny Huang 		image_version = OTP_A2;
17145fdde29fSJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4);
17155fdde29fSJohnny Huang 		image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length;
17165fdde29fSJohnny Huang 		image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length;
17175fdde29fSJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length;
171864b66712SJohnny Huang 	} else if (!strcmp("A3", (char *)otp_header->otp_version)) {
1719e417205bSJohnny Huang 		image_version = OTP_A3;
172064b66712SJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4);
172164b66712SJohnny Huang 		image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length;
172264b66712SJohnny Huang 		image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length;
172364b66712SJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length;
1724696656c6SJohnny Huang 	} else {
1725696656c6SJohnny Huang 		puts("Version is not supported\n");
1726696656c6SJohnny Huang 		return OTP_FAILURE;
1727696656c6SJohnny Huang 	}
1728696656c6SJohnny Huang 
17299a4fe690SJohnny Huang 	if (image_version != info_cb.version) {
17309a4fe690SJohnny Huang 		puts("Version is not match\n");
17319a4fe690SJohnny Huang 		return OTP_FAILURE;
17329a4fe690SJohnny Huang 	}
17339a4fe690SJohnny Huang 
1734*f347c284SJohnny Huang 	if (otp_verify_image(buf, image_size, checksum)) {
1735696656c6SJohnny Huang 		puts("checksum is invalid\n");
1736696656c6SJohnny Huang 		return OTP_FAILURE;
1737d90825e2SJohnny Huang 	}
17387332532cSJohnny Huang 
173969d5fd8fSJohnny Huang 	if (!nconfirm) {
1740696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_DATA) {
17417f795e57SJohnny Huang 			printf("\nOTP data region :\n");
1742*f347c284SJohnny Huang 			if (otp_print_data_image(&image_layout) < 0) {
174369d5fd8fSJohnny Huang 				printf("OTP data error, please check.\n");
17442a856b9aSJohnny Huang 				return OTP_FAILURE;
174569d5fd8fSJohnny Huang 			}
174669d5fd8fSJohnny Huang 		}
1747696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_STRAP) {
17487332532cSJohnny Huang 			printf("\nOTP strap region :\n");
17495010032bSJohnny Huang 			if (otp_print_strap_image(&image_layout) < 0) {
17507332532cSJohnny Huang 				printf("OTP strap error, please check.\n");
17517332532cSJohnny Huang 				return OTP_FAILURE;
17527332532cSJohnny Huang 			}
17537332532cSJohnny Huang 		}
1754696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_CONFIG) {
17557332532cSJohnny Huang 			printf("\nOTP configuration region :\n");
1756696656c6SJohnny Huang 			if (otp_print_conf_image(&image_layout) < 0) {
17577332532cSJohnny Huang 				printf("OTP config error, please check.\n");
17587332532cSJohnny Huang 				return OTP_FAILURE;
17597332532cSJohnny Huang 			}
17607332532cSJohnny Huang 		}
17617332532cSJohnny Huang 
176269d5fd8fSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
176369d5fd8fSJohnny Huang 		if (!confirm_yesno()) {
176469d5fd8fSJohnny Huang 			printf(" Aborting\n");
17652a856b9aSJohnny Huang 			return OTP_FAILURE;
176669d5fd8fSJohnny Huang 		}
176769d5fd8fSJohnny Huang 	}
17687332532cSJohnny Huang 
17695010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_DATA) {
17705010032bSJohnny Huang 		printf("programing data region ...\n");
17715010032bSJohnny Huang 		ret = otp_prog_data(&image_layout);
17725010032bSJohnny Huang 		if (ret != 0) {
17735010032bSJohnny Huang 			printf("Error\n");
17745010032bSJohnny Huang 			return ret;
17755010032bSJohnny Huang 		}
1776a219f6deSJohnny Huang 		printf("Done\n");
17775010032bSJohnny Huang 	}
17785010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_STRAP) {
17795010032bSJohnny Huang 		printf("programing strap region ...\n");
17805010032bSJohnny Huang 		ret = otp_prog_strap(&image_layout);
17815010032bSJohnny Huang 		if (ret != 0) {
17825010032bSJohnny Huang 			printf("Error\n");
17835010032bSJohnny Huang 			return ret;
17845010032bSJohnny Huang 		}
1785a219f6deSJohnny Huang 		printf("Done\n");
17865010032bSJohnny Huang 	}
17875010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_CONFIG) {
17885010032bSJohnny Huang 		printf("programing configuration region ...\n");
17895010032bSJohnny Huang 		ret = otp_prog_conf(&image_layout);
17905010032bSJohnny Huang 		if (ret != 0) {
17915010032bSJohnny Huang 			printf("Error\n");
17925010032bSJohnny Huang 			return ret;
17935010032bSJohnny Huang 		}
17945010032bSJohnny Huang 		printf("Done\n");
17955010032bSJohnny Huang 	}
1796cd1610b4SJohnny Huang 
17977332532cSJohnny Huang 	return OTP_SUCCESS;
17982a856b9aSJohnny Huang }
17992a856b9aSJohnny Huang 
1800*f347c284SJohnny Huang static int otp_prog_bit(int mode, int otp_dw_offset, int bit_offset, int value, int nconfirm)
1801cd1610b4SJohnny Huang {
1802a219f6deSJohnny Huang 	u32 read[2];
1803a219f6deSJohnny Huang 	u32 prog_address = 0;
180466f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
1805cd1610b4SJohnny Huang 	int otp_bit;
180683655e91SJohnny Huang 	int ret = 0;
1807cd1610b4SJohnny Huang 
1808dacbba92SJohnny Huang 	otp_soak(0);
1809cd1610b4SJohnny Huang 	switch (mode) {
1810a6d0d645SJohnny Huang 	case OTP_REGION_CONF:
1811*f347c284SJohnny Huang 		otp_read_conf(otp_dw_offset, read);
1812cd1610b4SJohnny Huang 		prog_address = 0x800;
1813cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset / 8) * 0x200;
1814cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset % 8) * 0x2;
1815a6af4a17SJohnny Huang 		otp_bit = (read[0] >> bit_offset) & 0x1;
1816cd1610b4SJohnny Huang 		if (otp_bit == value) {
1817a6af4a17SJohnny Huang 			printf("OTPCFG%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
1818cd1610b4SJohnny Huang 			printf("No need to program\n");
18192a856b9aSJohnny Huang 			return OTP_SUCCESS;
1820cd1610b4SJohnny Huang 		}
1821cd1610b4SJohnny Huang 		if (otp_bit == 1 && value == 0) {
1822a6af4a17SJohnny Huang 			printf("OTPCFG%X[%X] = 1\n", otp_dw_offset, bit_offset);
1823cd1610b4SJohnny Huang 			printf("OTP is programed, which can't be clean\n");
18242a856b9aSJohnny Huang 			return OTP_FAILURE;
1825cd1610b4SJohnny Huang 		}
1826a6af4a17SJohnny Huang 		printf("Program OTPCFG%X[%X] to 1\n", otp_dw_offset, bit_offset);
1827cd1610b4SJohnny Huang 		break;
1828a6d0d645SJohnny Huang 	case OTP_REGION_DATA:
1829cd1610b4SJohnny Huang 		prog_address = otp_dw_offset;
1830cd1610b4SJohnny Huang 
1831cd1610b4SJohnny Huang 		if (otp_dw_offset % 2 == 0) {
1832a6af4a17SJohnny Huang 			otp_read_data(otp_dw_offset, read);
1833a6af4a17SJohnny Huang 			otp_bit = (read[0] >> bit_offset) & 0x1;
1834643b9cfdSJohnny Huang 
1835643b9cfdSJohnny Huang 			if (otp_bit == 1 && value == 0) {
1836643b9cfdSJohnny Huang 				printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
1837643b9cfdSJohnny Huang 				printf("OTP is programed, which can't be cleaned\n");
1838643b9cfdSJohnny Huang 				return OTP_FAILURE;
1839643b9cfdSJohnny Huang 			}
1840cd1610b4SJohnny Huang 		} else {
1841a6af4a17SJohnny Huang 			otp_read_data(otp_dw_offset - 1, read);
1842a6af4a17SJohnny Huang 			otp_bit = (read[1] >> bit_offset) & 0x1;
1843643b9cfdSJohnny Huang 
1844643b9cfdSJohnny Huang 			if (otp_bit == 0 && value == 1) {
1845643b9cfdSJohnny Huang 				printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
1846643b9cfdSJohnny Huang 				printf("OTP is programed, which can't be writen\n");
1847643b9cfdSJohnny Huang 				return OTP_FAILURE;
1848643b9cfdSJohnny Huang 			}
1849cd1610b4SJohnny Huang 		}
1850cd1610b4SJohnny Huang 		if (otp_bit == value) {
1851a6af4a17SJohnny Huang 			printf("OTPDATA%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
1852cd1610b4SJohnny Huang 			printf("No need to program\n");
18532a856b9aSJohnny Huang 			return OTP_SUCCESS;
1854cd1610b4SJohnny Huang 		}
1855643b9cfdSJohnny Huang 
1856a6af4a17SJohnny Huang 		printf("Program OTPDATA%X[%X] to 1\n", otp_dw_offset, bit_offset);
1857cd1610b4SJohnny Huang 		break;
1858a6d0d645SJohnny Huang 	case OTP_REGION_STRAP:
18598848d5dcSJohnny Huang 		otp_strap_status(otpstrap);
18608848d5dcSJohnny Huang 		otp_print_strap(bit_offset, 1);
18618848d5dcSJohnny Huang 		ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0, 0);
18628848d5dcSJohnny Huang 		if (ret == OTP_FAILURE)
18638848d5dcSJohnny Huang 			return OTP_FAILURE;
18648848d5dcSJohnny Huang 		else if (ret == OTP_PROG_SKIP)
18658848d5dcSJohnny Huang 			return OTP_SUCCESS;
1866a6af4a17SJohnny Huang 
1867cd1610b4SJohnny Huang 		break;
1868cd1610b4SJohnny Huang 	}
1869cd1610b4SJohnny Huang 
1870cd1610b4SJohnny Huang 	if (!nconfirm) {
1871cd1610b4SJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
1872cd1610b4SJohnny Huang 		if (!confirm_yesno()) {
1873cd1610b4SJohnny Huang 			printf(" Aborting\n");
18742a856b9aSJohnny Huang 			return OTP_FAILURE;
1875cd1610b4SJohnny Huang 		}
1876cd1610b4SJohnny Huang 	}
1877cd1610b4SJohnny Huang 
1878cd1610b4SJohnny Huang 	switch (mode) {
1879a6d0d645SJohnny Huang 	case OTP_REGION_STRAP:
1880*f347c284SJohnny Huang 		ret =  otp_prog_strap_b(bit_offset, value);
188183655e91SJohnny Huang 		break;
1882a6d0d645SJohnny Huang 	case OTP_REGION_CONF:
1883a6d0d645SJohnny Huang 	case OTP_REGION_DATA:
1884*f347c284SJohnny Huang 		ret = otp_prog_dc_b(value, prog_address, bit_offset);
1885de6fbf1cSJohnny Huang 		break;
1886de6fbf1cSJohnny Huang 	}
1887de6fbf1cSJohnny Huang 	otp_soak(0);
188883655e91SJohnny Huang 	if (ret) {
1889794e27ecSJohnny Huang 		printf("OTP cannot be programed\n");
1890794e27ecSJohnny Huang 		printf("FAILURE\n");
1891794e27ecSJohnny Huang 		return OTP_FAILURE;
1892794e27ecSJohnny Huang 	}
1893794e27ecSJohnny Huang 
18949009c25dSJohnny Huang 	printf("SUCCESS\n");
18952a856b9aSJohnny Huang 	return OTP_SUCCESS;
1896a219f6deSJohnny Huang }
1897a219f6deSJohnny Huang 
1898794e27ecSJohnny Huang static int otp_update_rid(u32 update_num, int force)
1899794e27ecSJohnny Huang {
1900794e27ecSJohnny Huang 	u32 otp_rid[2];
1901794e27ecSJohnny Huang 	int rid_num = 0;
1902794e27ecSJohnny Huang 	int bit_offset;
1903794e27ecSJohnny Huang 	int dw_offset;
1904794e27ecSJohnny Huang 	int i;
1905794e27ecSJohnny Huang 	int ret;
1906794e27ecSJohnny Huang 
1907*f347c284SJohnny Huang 	otp_read_conf(10, &otp_rid[0]);
1908*f347c284SJohnny Huang 	otp_read_conf(11, &otp_rid[1]);
1909794e27ecSJohnny Huang 
1910794e27ecSJohnny Huang 	rid_num = get_rid_num(otp_rid);
1911794e27ecSJohnny Huang 
1912794e27ecSJohnny Huang 	if (rid_num < 0) {
1913794e27ecSJohnny Huang 		printf("Currennt OTP revision ID cannot handle by this command,\n"
1914794e27ecSJohnny Huang 		       "plase use 'otp pb' command to update it manually\n");
1915794e27ecSJohnny Huang 		otp_print_revid(otp_rid);
19169009c25dSJohnny Huang 		return OTP_FAILURE;
19179009c25dSJohnny Huang 	}
1918cd1610b4SJohnny Huang 
1919794e27ecSJohnny Huang 	printf("current OTP revision ID: 0x%x\n", rid_num);
1920794e27ecSJohnny Huang 	otp_print_revid(otp_rid);
1921794e27ecSJohnny Huang 	printf("input update number: 0x%x\n", update_num);
1922794e27ecSJohnny Huang 
1923794e27ecSJohnny Huang 	if (rid_num >= update_num) {
1924794e27ecSJohnny Huang 		printf("OTP rev_id is bigger than 0x%x\n", update_num);
1925794e27ecSJohnny Huang 		printf("Skip\n");
1926794e27ecSJohnny Huang 		return OTP_FAILURE;
1927794e27ecSJohnny Huang 	}
1928794e27ecSJohnny Huang 
1929794e27ecSJohnny Huang 	for (i = rid_num; i < update_num; i++) {
1930794e27ecSJohnny Huang 		if (i < 32) {
1931794e27ecSJohnny Huang 			dw_offset = 0xa;
1932794e27ecSJohnny Huang 			bit_offset = i;
1933794e27ecSJohnny Huang 		} else {
1934794e27ecSJohnny Huang 			dw_offset = 0xb;
1935794e27ecSJohnny Huang 			bit_offset = i - 32;
1936794e27ecSJohnny Huang 		}
1937794e27ecSJohnny Huang 		printf("OTPCFG%X[%d]", dw_offset, bit_offset);
1938794e27ecSJohnny Huang 		if (i + 1 != update_num)
1939794e27ecSJohnny Huang 			printf(", ");
1940794e27ecSJohnny Huang 	}
1941794e27ecSJohnny Huang 
1942794e27ecSJohnny Huang 	printf(" will be programmed\n");
1943794e27ecSJohnny Huang 	if (force == 0) {
1944794e27ecSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
1945794e27ecSJohnny Huang 		if (!confirm_yesno()) {
1946794e27ecSJohnny Huang 			printf(" Aborting\n");
1947794e27ecSJohnny Huang 			return OTP_FAILURE;
1948794e27ecSJohnny Huang 		}
1949794e27ecSJohnny Huang 	}
1950794e27ecSJohnny Huang 
1951794e27ecSJohnny Huang 	ret = 0;
1952794e27ecSJohnny Huang 	for (i = rid_num; i < update_num; i++) {
1953794e27ecSJohnny Huang 		if (i < 32) {
1954794e27ecSJohnny Huang 			dw_offset = 0xa04;
1955794e27ecSJohnny Huang 			bit_offset = i;
1956794e27ecSJohnny Huang 		} else {
1957794e27ecSJohnny Huang 			dw_offset = 0xa06;
1958794e27ecSJohnny Huang 			bit_offset = i - 32;
1959794e27ecSJohnny Huang 		}
1960*f347c284SJohnny Huang 		if (otp_prog_dc_b(1, dw_offset, bit_offset)) {
1961794e27ecSJohnny Huang 			printf("OTPCFG%X[%d] programming failed\n", dw_offset, bit_offset);
1962794e27ecSJohnny Huang 			ret = OTP_FAILURE;
1963794e27ecSJohnny Huang 			break;
1964794e27ecSJohnny Huang 		}
1965794e27ecSJohnny Huang 	}
1966794e27ecSJohnny Huang 
1967*f347c284SJohnny Huang 	otp_read_conf(10, &otp_rid[0]);
1968*f347c284SJohnny Huang 	otp_read_conf(11, &otp_rid[1]);
1969794e27ecSJohnny Huang 	rid_num = get_rid_num(otp_rid);
1970794e27ecSJohnny Huang 	if (rid_num >= 0)
1971794e27ecSJohnny Huang 		printf("OTP revision ID: 0x%x\n", rid_num);
1972794e27ecSJohnny Huang 	else
1973794e27ecSJohnny Huang 		printf("OTP revision ID\n");
1974794e27ecSJohnny Huang 	otp_print_revid(otp_rid);
1975794e27ecSJohnny Huang 	if (!ret)
1976794e27ecSJohnny Huang 		printf("SUCCESS\n");
1977794e27ecSJohnny Huang 	else
1978794e27ecSJohnny Huang 		printf("FAILED\n");
1979794e27ecSJohnny Huang 	return ret;
1980794e27ecSJohnny Huang }
1981794e27ecSJohnny Huang 
19822a856b9aSJohnny Huang static int do_otpread(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
198369d5fd8fSJohnny Huang {
1984a219f6deSJohnny Huang 	u32 offset, count;
19852a856b9aSJohnny Huang 	int ret;
198669d5fd8fSJohnny Huang 
19872a856b9aSJohnny Huang 	if (argc == 4) {
19882a856b9aSJohnny Huang 		offset = simple_strtoul(argv[2], NULL, 16);
19892a856b9aSJohnny Huang 		count = simple_strtoul(argv[3], NULL, 16);
19902a856b9aSJohnny Huang 	} else if (argc == 3) {
19912a856b9aSJohnny Huang 		offset = simple_strtoul(argv[2], NULL, 16);
19922a856b9aSJohnny Huang 		count = 1;
19932a856b9aSJohnny Huang 	} else {
199469d5fd8fSJohnny Huang 		return CMD_RET_USAGE;
199569d5fd8fSJohnny Huang 	}
199669d5fd8fSJohnny Huang 
19972a856b9aSJohnny Huang 	if (!strcmp(argv[1], "conf")) {
19983d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
1999*f347c284SJohnny Huang 		ret = otp_print_conf(offset, count);
20002a856b9aSJohnny Huang 	} else if (!strcmp(argv[1], "data")) {
20013d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
20022a856b9aSJohnny Huang 		ret = otp_print_data(offset, count);
20032a856b9aSJohnny Huang 	} else if (!strcmp(argv[1], "strap")) {
20043d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
20052a856b9aSJohnny Huang 		ret = otp_print_strap(offset, count);
20062a856b9aSJohnny Huang 	} else {
20072a856b9aSJohnny Huang 		return CMD_RET_USAGE;
200869d5fd8fSJohnny Huang 	}
200969d5fd8fSJohnny Huang 
20102a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
20112a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
20122a856b9aSJohnny Huang 	return CMD_RET_USAGE;
20132a856b9aSJohnny Huang }
20142a856b9aSJohnny Huang 
20152a856b9aSJohnny Huang static int do_otpprog(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
20162a856b9aSJohnny Huang {
20172a856b9aSJohnny Huang 	phys_addr_t addr;
20182a856b9aSJohnny Huang 	int ret;
20192a856b9aSJohnny Huang 
2020de6b0cc4SJohnny Huang 	if (argc == 3) {
2021ed071a2bSJohnny Huang 		if (strcmp(argv[1], "o"))
20222a856b9aSJohnny Huang 			return CMD_RET_USAGE;
20232a856b9aSJohnny Huang 		addr = simple_strtoul(argv[2], NULL, 16);
20243d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2025*f347c284SJohnny Huang 		ret = otp_prog_image(addr, 1);
2026de6b0cc4SJohnny Huang 	} else if (argc == 2) {
20272a856b9aSJohnny Huang 		addr = simple_strtoul(argv[1], NULL, 16);
20283d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2029*f347c284SJohnny Huang 		ret = otp_prog_image(addr, 0);
20302a856b9aSJohnny Huang 	} else {
20312a856b9aSJohnny Huang 		return CMD_RET_USAGE;
20322a856b9aSJohnny Huang 	}
20332a856b9aSJohnny Huang 
20342a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
20352a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
20362a856b9aSJohnny Huang 	else if (ret == OTP_FAILURE)
20372a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
20382a856b9aSJohnny Huang 	else
20392a856b9aSJohnny Huang 		return CMD_RET_USAGE;
20402a856b9aSJohnny Huang }
20412a856b9aSJohnny Huang 
20422a856b9aSJohnny Huang static int do_otppb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
20432a856b9aSJohnny Huang {
20442a856b9aSJohnny Huang 	int mode = 0;
20452a856b9aSJohnny Huang 	int nconfirm = 0;
20462a856b9aSJohnny Huang 	int otp_addr = 0;
20472a856b9aSJohnny Huang 	int bit_offset;
20482a856b9aSJohnny Huang 	int value;
20492a856b9aSJohnny Huang 	int ret;
20502a856b9aSJohnny Huang 
20512a856b9aSJohnny Huang 	if (argc != 4 && argc != 5 && argc != 6)
20522a856b9aSJohnny Huang 		return CMD_RET_USAGE;
20532a856b9aSJohnny Huang 
20542a856b9aSJohnny Huang 	/* Drop the pb cmd */
20552a856b9aSJohnny Huang 	argc--;
20562a856b9aSJohnny Huang 	argv++;
20572a856b9aSJohnny Huang 
20582a856b9aSJohnny Huang 	if (!strcmp(argv[0], "conf"))
2059a6d0d645SJohnny Huang 		mode = OTP_REGION_CONF;
20602a856b9aSJohnny Huang 	else if (!strcmp(argv[0], "strap"))
2061a6d0d645SJohnny Huang 		mode = OTP_REGION_STRAP;
20622a856b9aSJohnny Huang 	else if (!strcmp(argv[0], "data"))
2063a6d0d645SJohnny Huang 		mode = OTP_REGION_DATA;
2064cd1610b4SJohnny Huang 	else
20652a856b9aSJohnny Huang 		return CMD_RET_USAGE;
20662a856b9aSJohnny Huang 
20672a856b9aSJohnny Huang 	/* Drop the region cmd */
20682a856b9aSJohnny Huang 	argc--;
20692a856b9aSJohnny Huang 	argv++;
20702a856b9aSJohnny Huang 
2071ed071a2bSJohnny Huang 	if (!strcmp(argv[0], "o")) {
2072cd1610b4SJohnny Huang 		nconfirm = 1;
20732a856b9aSJohnny Huang 		/* Drop the force option */
20742a856b9aSJohnny Huang 		argc--;
20752a856b9aSJohnny Huang 		argv++;
20762a856b9aSJohnny Huang 	}
2077cd1610b4SJohnny Huang 
2078a6d0d645SJohnny Huang 	if (mode == OTP_REGION_STRAP) {
20792a856b9aSJohnny Huang 		bit_offset = simple_strtoul(argv[0], NULL, 16);
20802a856b9aSJohnny Huang 		value = simple_strtoul(argv[1], NULL, 16);
20810808cc55SJohnny Huang 		if (bit_offset >= 64 || (value != 0 && value != 1))
20822a856b9aSJohnny Huang 			return CMD_RET_USAGE;
2083cd1610b4SJohnny Huang 	} else {
20842a856b9aSJohnny Huang 		otp_addr = simple_strtoul(argv[0], NULL, 16);
20852a856b9aSJohnny Huang 		bit_offset = simple_strtoul(argv[1], NULL, 16);
20862a856b9aSJohnny Huang 		value = simple_strtoul(argv[2], NULL, 16);
20870808cc55SJohnny Huang 		if (bit_offset >= 32 || (value != 0 && value != 1))
20882a856b9aSJohnny Huang 			return CMD_RET_USAGE;
20890808cc55SJohnny Huang 		if (mode == OTP_REGION_DATA) {
209078855207SJohnny Huang 			if (otp_addr >= 0x800)
20910808cc55SJohnny Huang 				return CMD_RET_USAGE;
20920808cc55SJohnny Huang 		} else {
209378855207SJohnny Huang 			if (otp_addr >= 0x20)
20940808cc55SJohnny Huang 				return CMD_RET_USAGE;
20950808cc55SJohnny Huang 		}
2096cd1610b4SJohnny Huang 	}
2097cd1610b4SJohnny Huang 	if (value != 0 && value != 1)
20982a856b9aSJohnny Huang 		return CMD_RET_USAGE;
2099cd1610b4SJohnny Huang 
21003d3688adSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2101*f347c284SJohnny Huang 	ret = otp_prog_bit(mode, otp_addr, bit_offset, value, nconfirm);
21022a856b9aSJohnny Huang 
21032a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
21042a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
21052a856b9aSJohnny Huang 	else if (ret == OTP_FAILURE)
21062a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
21072a856b9aSJohnny Huang 	else
21082a856b9aSJohnny Huang 		return CMD_RET_USAGE;
21092a856b9aSJohnny Huang }
21102a856b9aSJohnny Huang 
21112a856b9aSJohnny Huang static int do_otpcmp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
21122a856b9aSJohnny Huang {
21132a856b9aSJohnny Huang 	phys_addr_t addr;
21142a856b9aSJohnny Huang 	int otp_addr = 0;
21152a856b9aSJohnny Huang 
21162a856b9aSJohnny Huang 	if (argc != 3)
21172a856b9aSJohnny Huang 		return CMD_RET_USAGE;
21182a856b9aSJohnny Huang 
21193d3688adSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
21202a856b9aSJohnny Huang 	addr = simple_strtoul(argv[1], NULL, 16);
21212a856b9aSJohnny Huang 	otp_addr = simple_strtoul(argv[2], NULL, 16);
21222a856b9aSJohnny Huang 	if (otp_compare(otp_addr, addr) == 0) {
212369d5fd8fSJohnny Huang 		printf("Compare pass\n");
21242a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
2125a219f6deSJohnny Huang 	}
212669d5fd8fSJohnny Huang 	printf("Compare fail\n");
21272a856b9aSJohnny Huang 	return CMD_RET_FAILURE;
212869d5fd8fSJohnny Huang }
212969d5fd8fSJohnny Huang 
213066f2f8e5SJohnny Huang static int do_otpinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
213166f2f8e5SJohnny Huang {
2132a8bd6d8cSJohnny Huang 	int view = 0;
21332d4b0742SJohnny Huang 	int input;
2134a8bd6d8cSJohnny Huang 
2135a8bd6d8cSJohnny Huang 	if (argc != 2 && argc != 3)
213666f2f8e5SJohnny Huang 		return CMD_RET_USAGE;
213766f2f8e5SJohnny Huang 
21382d4b0742SJohnny Huang 	if (!strcmp(argv[1], "conf")) {
21393d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
21402d4b0742SJohnny Huang 		if (argc == 3) {
21412d4b0742SJohnny Huang 			input = simple_strtoul(argv[2], NULL, 16);
21422d4b0742SJohnny Huang 			otp_print_conf_info(input);
21432d4b0742SJohnny Huang 		} else {
21442d4b0742SJohnny Huang 			otp_print_conf_info(-1);
21452d4b0742SJohnny Huang 		}
21462d4b0742SJohnny Huang 	} else if (!strcmp(argv[1], "strap")) {
21472d4b0742SJohnny Huang 		if (!strcmp(argv[2], "v")) {
2148a8bd6d8cSJohnny Huang 			view = 1;
2149a8bd6d8cSJohnny Huang 			/* Drop the view option */
2150a8bd6d8cSJohnny Huang 			argc--;
2151a8bd6d8cSJohnny Huang 			argv++;
2152a8bd6d8cSJohnny Huang 		}
21533d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2154b458cd62SJohnny Huang 		otp_print_strap_info(view);
215566f2f8e5SJohnny Huang 	} else {
215666f2f8e5SJohnny Huang 		return CMD_RET_USAGE;
215766f2f8e5SJohnny Huang 	}
21582d4b0742SJohnny Huang 
215966f2f8e5SJohnny Huang 	return CMD_RET_SUCCESS;
216066f2f8e5SJohnny Huang }
216166f2f8e5SJohnny Huang 
2162e14b073cSJohnny Huang static int _do_otpprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[], int preg)
2163737ed20bSJohnny Huang {
2164737ed20bSJohnny Huang 	int input;
2165737ed20bSJohnny Huang 	int bit_offset;
2166e14b073cSJohnny Huang 	u32 prog_address;
216783655e91SJohnny Huang 	int ret;
2168e14b073cSJohnny Huang 	char info[10];
2169e14b073cSJohnny Huang 
2170e14b073cSJohnny Huang 	if (preg) {
2171e14b073cSJohnny Huang 		sprintf(info, "register ");
2172e14b073cSJohnny Huang 		prog_address = 0xe08;
2173e14b073cSJohnny Huang 	} else {
2174e14b073cSJohnny Huang 		info[0] = 0;
2175e14b073cSJohnny Huang 		prog_address = 0xe0c;
2176e14b073cSJohnny Huang 	}
2177a219f6deSJohnny Huang 
2178737ed20bSJohnny Huang 	if (argc != 3 && argc != 2)
2179737ed20bSJohnny Huang 		return CMD_RET_USAGE;
2180737ed20bSJohnny Huang 
2181e14b073cSJohnny Huang 	if (!strcmp(argv[1], "o")) {
2182737ed20bSJohnny Huang 		input = simple_strtoul(argv[2], NULL, 16);
2183737ed20bSJohnny Huang 	} else {
2184737ed20bSJohnny Huang 		input = simple_strtoul(argv[1], NULL, 16);
2185e14b073cSJohnny Huang 		printf("OTPSTRAP[%d] %swill be protected\n", input, info);
2186737ed20bSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
2187737ed20bSJohnny Huang 		if (!confirm_yesno()) {
2188737ed20bSJohnny Huang 			printf(" Aborting\n");
2189737ed20bSJohnny Huang 			return CMD_RET_FAILURE;
2190737ed20bSJohnny Huang 		}
2191737ed20bSJohnny Huang 	}
2192737ed20bSJohnny Huang 
2193737ed20bSJohnny Huang 	if (input < 32) {
2194737ed20bSJohnny Huang 		bit_offset = input;
2195737ed20bSJohnny Huang 	} else if (input < 64) {
2196737ed20bSJohnny Huang 		bit_offset = input - 32;
2197e14b073cSJohnny Huang 		prog_address += 2;
2198737ed20bSJohnny Huang 	} else {
2199737ed20bSJohnny Huang 		return CMD_RET_USAGE;
2200737ed20bSJohnny Huang 	}
2201737ed20bSJohnny Huang 
2202e14b073cSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2203e14b073cSJohnny Huang 	if (verify_bit(prog_address, bit_offset, 1) == 0) {
2204e14b073cSJohnny Huang 		printf("OTPSTRAP[%d] %salready protected\n", input, info);
2205e14b073cSJohnny Huang 		return CMD_RET_SUCCESS;
2206e14b073cSJohnny Huang 	}
2207de6fbf1cSJohnny Huang 
2208*f347c284SJohnny Huang 	ret = otp_prog_dc_b(1, prog_address, bit_offset);
2209de6fbf1cSJohnny Huang 	otp_soak(0);
221083655e91SJohnny Huang 
221183655e91SJohnny Huang 	if (ret) {
2212e14b073cSJohnny Huang 		printf("Protect OTPSTRAP[%d] %sfail\n", input, info);
2213737ed20bSJohnny Huang 		return CMD_RET_FAILURE;
2214737ed20bSJohnny Huang 	}
22159a4fe690SJohnny Huang 
2216794e27ecSJohnny Huang 	printf("OTPSTRAP[%d] %sis protected\n", input, info);
2217794e27ecSJohnny Huang 	return CMD_RET_SUCCESS;
2218794e27ecSJohnny Huang }
2219794e27ecSJohnny Huang 
2220e14b073cSJohnny Huang static int do_otpprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2221e14b073cSJohnny Huang {
2222e14b073cSJohnny Huang 	return _do_otpprotect(cmdtp, flag, argc, argv, 0);
2223e14b073cSJohnny Huang }
2224e14b073cSJohnny Huang 
2225e14b073cSJohnny Huang static int do_otprprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2226e14b073cSJohnny Huang {
2227e14b073cSJohnny Huang 	return _do_otpprotect(cmdtp, flag, argc, argv, 1);
2228e14b073cSJohnny Huang }
2229e14b073cSJohnny Huang 
2230f67375f7SJohnny Huang static int do_otpver(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2231f67375f7SJohnny Huang {
2232e417205bSJohnny Huang 	printf("SOC OTP version: %s\n", info_cb.ver_name);
2233f67375f7SJohnny Huang 	printf("OTP tool version: %s\n", OTP_VER);
2234f67375f7SJohnny Huang 	printf("OTP info version: %s\n", OTP_INFO_VER);
2235f67375f7SJohnny Huang 
2236f67375f7SJohnny Huang 	return CMD_RET_SUCCESS;
2237f67375f7SJohnny Huang }
2238f67375f7SJohnny Huang 
2239794e27ecSJohnny Huang static int do_otpupdate(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2240794e27ecSJohnny Huang {
2241794e27ecSJohnny Huang 	u32 update_num;
2242794e27ecSJohnny Huang 	int force = 0;
2243794e27ecSJohnny Huang 	int ret;
2244794e27ecSJohnny Huang 
2245794e27ecSJohnny Huang 	if (argc == 3) {
2246794e27ecSJohnny Huang 		if (strcmp(argv[1], "o"))
2247794e27ecSJohnny Huang 			return CMD_RET_USAGE;
2248794e27ecSJohnny Huang 		force = 1;
2249794e27ecSJohnny Huang 		update_num = simple_strtoul(argv[2], NULL, 16);
2250794e27ecSJohnny Huang 	} else if (argc == 2) {
2251794e27ecSJohnny Huang 		update_num = simple_strtoul(argv[1], NULL, 16);
2252794e27ecSJohnny Huang 	} else {
2253794e27ecSJohnny Huang 		return CMD_RET_USAGE;
2254794e27ecSJohnny Huang 	}
2255794e27ecSJohnny Huang 
2256794e27ecSJohnny Huang 	if (update_num > 64)
2257794e27ecSJohnny Huang 		return CMD_RET_USAGE;
2258794e27ecSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2259794e27ecSJohnny Huang 	ret = otp_update_rid(update_num, force);
2260794e27ecSJohnny Huang 	if (ret)
2261794e27ecSJohnny Huang 		return CMD_RET_FAILURE;
2262794e27ecSJohnny Huang 	return CMD_RET_SUCCESS;
2263794e27ecSJohnny Huang }
2264794e27ecSJohnny Huang 
2265794e27ecSJohnny Huang static int do_otprid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2266794e27ecSJohnny Huang {
2267794e27ecSJohnny Huang 	u32 otp_rid[2];
2268794e27ecSJohnny Huang 	int rid_num = 0;
2269794e27ecSJohnny Huang 	int ret;
2270794e27ecSJohnny Huang 
2271794e27ecSJohnny Huang 	if (argc != 1)
2272794e27ecSJohnny Huang 		return CMD_RET_USAGE;
2273794e27ecSJohnny Huang 
2274794e27ecSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2275*f347c284SJohnny Huang 	otp_read_conf(10, &otp_rid[0]);
2276*f347c284SJohnny Huang 	otp_read_conf(11, &otp_rid[1]);
2277794e27ecSJohnny Huang 
2278794e27ecSJohnny Huang 	rid_num = get_rid_num(otp_rid);
2279794e27ecSJohnny Huang 
2280794e27ecSJohnny Huang 	if (rid_num >= 0) {
2281794e27ecSJohnny Huang 		printf("current OTP revision ID: 0x%x\n", rid_num);
2282794e27ecSJohnny Huang 		ret = CMD_RET_SUCCESS;
2283794e27ecSJohnny Huang 	} else {
2284794e27ecSJohnny Huang 		printf("Currennt OTP revision ID cannot handle by 'otp update',\n"
2285794e27ecSJohnny Huang 		       "plase use 'otp pb' command to update it manually\n"
2286794e27ecSJohnny Huang 		       "current OTP revision ID\n");
2287794e27ecSJohnny Huang 		ret = CMD_RET_FAILURE;
2288794e27ecSJohnny Huang 	}
2289794e27ecSJohnny Huang 	otp_print_revid(otp_rid);
2290794e27ecSJohnny Huang 
2291794e27ecSJohnny Huang 	return ret;
2292794e27ecSJohnny Huang }
2293794e27ecSJohnny Huang 
22942a856b9aSJohnny Huang static cmd_tbl_t cmd_otp[] = {
2295f67375f7SJohnny Huang 	U_BOOT_CMD_MKENT(version, 1, 0, do_otpver, "", ""),
22962a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(read, 4, 0, do_otpread, "", ""),
2297a8bd6d8cSJohnny Huang 	U_BOOT_CMD_MKENT(info, 3, 0, do_otpinfo, "", ""),
2298de6b0cc4SJohnny Huang 	U_BOOT_CMD_MKENT(prog, 3, 0, do_otpprog, "", ""),
22992a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(pb, 6, 0, do_otppb, "", ""),
2300737ed20bSJohnny Huang 	U_BOOT_CMD_MKENT(protect, 3, 0, do_otpprotect, "", ""),
2301e14b073cSJohnny Huang 	U_BOOT_CMD_MKENT(rprotect, 3, 0, do_otprprotect, "", ""),
23022a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(cmp, 3, 0, do_otpcmp, "", ""),
2303794e27ecSJohnny Huang 	U_BOOT_CMD_MKENT(update, 3, 0, do_otpupdate, "", ""),
2304794e27ecSJohnny Huang 	U_BOOT_CMD_MKENT(rid, 1, 0, do_otprid, "", ""),
23052a856b9aSJohnny Huang };
23062a856b9aSJohnny Huang 
23072a856b9aSJohnny Huang static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
23082a856b9aSJohnny Huang {
23092a856b9aSJohnny Huang 	cmd_tbl_t *cp;
2310a219f6deSJohnny Huang 	u32 ver;
2311e14b073cSJohnny Huang 	int ret;
23122a856b9aSJohnny Huang 
23132a856b9aSJohnny Huang 	cp = find_cmd_tbl(argv[1], cmd_otp, ARRAY_SIZE(cmd_otp));
23142a856b9aSJohnny Huang 
2315737ed20bSJohnny Huang 	/* Drop the otp command */
23162a856b9aSJohnny Huang 	argc--;
23172a856b9aSJohnny Huang 	argv++;
23182a856b9aSJohnny Huang 
2319a219f6deSJohnny Huang 	if (!cp || argc > cp->maxargs)
23202a856b9aSJohnny Huang 		return CMD_RET_USAGE;
23212a856b9aSJohnny Huang 	if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
23222a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
23232a856b9aSJohnny Huang 
23240dae9d52SJohnny Huang 	ver = chip_version();
23250dae9d52SJohnny Huang 	switch (ver) {
2326e417205bSJohnny Huang 	case OTP_A0:
2327e417205bSJohnny Huang 		info_cb.version = OTP_A0;
23289a4fe690SJohnny Huang 		info_cb.conf_info = a0_conf_info;
23299a4fe690SJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a0_conf_info);
23309a4fe690SJohnny Huang 		info_cb.strap_info = a0_strap_info;
23319a4fe690SJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a0_strap_info);
23329a4fe690SJohnny Huang 		info_cb.key_info = a0_key_type;
23339a4fe690SJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a0_key_type);
2334e417205bSJohnny Huang 		sprintf(info_cb.ver_name, "A0");
23350dae9d52SJohnny Huang 		break;
2336e417205bSJohnny Huang 	case OTP_A1:
2337e417205bSJohnny Huang 		info_cb.version = OTP_A1;
23383cb28812SJohnny Huang 		info_cb.conf_info = a1_conf_info;
23393cb28812SJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a1_conf_info);
23403cb28812SJohnny Huang 		info_cb.strap_info = a1_strap_info;
23413cb28812SJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a1_strap_info);
23429a4fe690SJohnny Huang 		info_cb.key_info = a1_key_type;
23439a4fe690SJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a1_key_type);
2344e417205bSJohnny Huang 		sprintf(info_cb.ver_name, "A1");
23450dae9d52SJohnny Huang 		break;
2346e417205bSJohnny Huang 	case OTP_A2:
2347e417205bSJohnny Huang 		info_cb.version = OTP_A2;
23485fdde29fSJohnny Huang 		info_cb.conf_info = a2_conf_info;
23495fdde29fSJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a2_conf_info);
23505fdde29fSJohnny Huang 		info_cb.strap_info = a2_strap_info;
23515fdde29fSJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a2_strap_info);
23525fdde29fSJohnny Huang 		info_cb.key_info = a2_key_type;
23535fdde29fSJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a2_key_type);
2354e417205bSJohnny Huang 		sprintf(info_cb.ver_name, "A2");
23550dae9d52SJohnny Huang 		break;
2356e417205bSJohnny Huang 	case OTP_A3:
2357e417205bSJohnny Huang 		info_cb.version = OTP_A3;
235864b66712SJohnny Huang 		info_cb.conf_info = a2_conf_info;
235964b66712SJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a2_conf_info);
236064b66712SJohnny Huang 		info_cb.strap_info = a2_strap_info;
236164b66712SJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a2_strap_info);
2362181f72d8SJohnny Huang 		info_cb.key_info = a3_key_type;
2363181f72d8SJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a3_key_type);
2364e417205bSJohnny Huang 		sprintf(info_cb.ver_name, "A3");
236564b66712SJohnny Huang 		break;
23660dae9d52SJohnny Huang 	default:
2367f1be5099SJohnny Huang 		printf("SOC is not supported\n");
23680dae9d52SJohnny Huang 		return CMD_RET_FAILURE;
23699a4fe690SJohnny Huang 	}
23709a4fe690SJohnny Huang 
2371e14b073cSJohnny Huang 	ret = cp->cmd(cmdtp, flag, argc, argv);
2372e14b073cSJohnny Huang 	writel(1, OTP_PROTECT_KEY); //password
2373e14b073cSJohnny Huang 
2374e14b073cSJohnny Huang 	return ret;
237569d5fd8fSJohnny Huang }
237669d5fd8fSJohnny Huang 
2377a219f6deSJohnny Huang U_BOOT_CMD(otp, 7, 0,  do_ast_otp,
237869d5fd8fSJohnny Huang 	   "ASPEED One-Time-Programmable sub-system",
2379f67375f7SJohnny Huang 	   "version\n"
2380f67375f7SJohnny Huang 	   "otp read conf|data <otp_dw_offset> <dw_count>\n"
23812a856b9aSJohnny Huang 	   "otp read strap <strap_bit_offset> <bit_count>\n"
23822d4b0742SJohnny Huang 	   "otp info strap [v]\n"
23832d4b0742SJohnny Huang 	   "otp info conf [otp_dw_offset]\n"
2384de6b0cc4SJohnny Huang 	   "otp prog [o] <addr>\n"
2385ed071a2bSJohnny Huang 	   "otp pb conf|data [o] <otp_dw_offset> <bit_offset> <value>\n"
2386ed071a2bSJohnny Huang 	   "otp pb strap [o] <bit_offset> <value>\n"
2387ed071a2bSJohnny Huang 	   "otp protect [o] <bit_offset>\n"
2388e14b073cSJohnny Huang 	   "otp rprotect [o] <bit_offset>\n"
2389794e27ecSJohnny Huang 	   "otp update [o] <revision_id>\n"
2390794e27ecSJohnny Huang 	   "otp rid\n"
239169d5fd8fSJohnny Huang 	  );
2392