xref: /openbmc/u-boot/cmd/otp.c (revision f1be50992958e0e4ac886e99a6a4a55d6e71157e)
169d5fd8fSJohnny Huang /*
269d5fd8fSJohnny Huang  *  This program is distributed in the hope that it will be useful,
369d5fd8fSJohnny Huang  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
469d5fd8fSJohnny Huang  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
569d5fd8fSJohnny Huang  *  GNU General Public License for more details.
669d5fd8fSJohnny Huang  *
769d5fd8fSJohnny Huang  *  You should have received a copy of the GNU General Public License
869d5fd8fSJohnny Huang  *  along with this program; if not, write to the Free Software
969d5fd8fSJohnny Huang  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1069d5fd8fSJohnny Huang  */
114c1c9b35SJohnny Huang #include <stdlib.h>
1269d5fd8fSJohnny Huang #include <common.h>
1369d5fd8fSJohnny Huang #include <console.h>
1469d5fd8fSJohnny Huang #include <bootretry.h>
1569d5fd8fSJohnny Huang #include <cli.h>
1669d5fd8fSJohnny Huang #include <command.h>
1769d5fd8fSJohnny Huang #include <console.h>
184c1c9b35SJohnny Huang #include <malloc.h>
1969d5fd8fSJohnny Huang #include <inttypes.h>
2069d5fd8fSJohnny Huang #include <mapmem.h>
2169d5fd8fSJohnny Huang #include <asm/io.h>
2269d5fd8fSJohnny Huang #include <linux/compiler.h>
23696656c6SJohnny Huang #include <u-boot/sha256.h>
240cee9a95SJohnny Huang #include "otp_info.h"
2569d5fd8fSJohnny Huang 
2669d5fd8fSJohnny Huang DECLARE_GLOBAL_DATA_PTR;
2769d5fd8fSJohnny Huang 
280dae9d52SJohnny Huang #define OTP_VER				"1.0.1"
29f67375f7SJohnny Huang 
3069d5fd8fSJohnny Huang #define OTP_PASSWD			0x349fe38a
31dacbba92SJohnny Huang #define RETRY				20
327332532cSJohnny Huang #define OTP_REGION_STRAP		BIT(0)
337332532cSJohnny Huang #define OTP_REGION_CONF			BIT(1)
347332532cSJohnny Huang #define OTP_REGION_DATA			BIT(2)
3569d5fd8fSJohnny Huang 
362a856b9aSJohnny Huang #define OTP_USAGE			-1
372a856b9aSJohnny Huang #define OTP_FAILURE			-2
382a856b9aSJohnny Huang #define OTP_SUCCESS			0
392a856b9aSJohnny Huang 
40a6af4a17SJohnny Huang #define OTP_PROG_SKIP			1
41a6af4a17SJohnny Huang 
429a4fe690SJohnny Huang #define OTP_KEY_TYPE_RSA		1
439a4fe690SJohnny Huang #define OTP_KEY_TYPE_AES		2
449a4fe690SJohnny Huang #define OTP_KEY_TYPE_VAULT		3
459a4fe690SJohnny Huang #define OTP_KEY_TYPE_HMAC		4
469a4fe690SJohnny Huang 
474c1c9b35SJohnny Huang #define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
484c1c9b35SJohnny Huang #define PBWIDTH 60
494c1c9b35SJohnny Huang 
503d3688adSJohnny Huang #define OTP_BASE		0x1e6f2000
513d3688adSJohnny Huang #define OTP_PROTECT_KEY		OTP_BASE
523d3688adSJohnny Huang #define OTP_COMMAND		OTP_BASE + 0x4
533d3688adSJohnny Huang #define OTP_TIMING		OTP_BASE + 0x8
543d3688adSJohnny Huang #define OTP_ADDR		OTP_BASE + 0x10
553d3688adSJohnny Huang #define OTP_STATUS		OTP_BASE + 0x14
563d3688adSJohnny Huang #define OTP_COMPARE_1		OTP_BASE + 0x20
573d3688adSJohnny Huang #define OTP_COMPARE_2		OTP_BASE + 0x24
583d3688adSJohnny Huang #define OTP_COMPARE_3		OTP_BASE + 0x28
593d3688adSJohnny Huang #define OTP_COMPARE_4		OTP_BASE + 0x2c
603d3688adSJohnny Huang 
61696656c6SJohnny Huang #define OTP_MAGIC		"SOCOTP"
62696656c6SJohnny Huang #define CHECKSUM_LEN		32
63696656c6SJohnny Huang #define OTP_INC_DATA		(1 << 31)
64696656c6SJohnny Huang #define OTP_INC_CONFIG		(1 << 30)
65696656c6SJohnny Huang #define OTP_INC_STRAP		(1 << 29)
66696656c6SJohnny Huang #define OTP_ECC_EN		(1 << 28)
67696656c6SJohnny Huang #define OTP_REGION_SIZE(info)	((info >> 16) & 0xffff)
68696656c6SJohnny Huang #define OTP_REGION_OFFSET(info)	(info & 0xffff)
69696656c6SJohnny Huang #define OTP_IMAGE_SIZE(info)	(info & 0xffff)
70696656c6SJohnny Huang 
71696656c6SJohnny Huang #define OTP_AST2600A0		0
72696656c6SJohnny Huang #define OTP_AST2600A1		1
730dae9d52SJohnny Huang #define OTP_AST2600A2		2
74696656c6SJohnny Huang 
75696656c6SJohnny Huang struct otp_header {
76696656c6SJohnny Huang 	u8	otp_magic[8];
77696656c6SJohnny Huang 	u8	otp_version[8];
78696656c6SJohnny Huang 	u32	image_info;
79696656c6SJohnny Huang 	u32	data_info;
80696656c6SJohnny Huang 	u32	config_info;
81696656c6SJohnny Huang 	u32	strap_info;
82696656c6SJohnny Huang 	u32	checksum_offset;
83696656c6SJohnny Huang } __attribute__((packed));
84696656c6SJohnny Huang 
8566f2f8e5SJohnny Huang struct otpstrap_status {
8669d5fd8fSJohnny Huang 	int value;
8769d5fd8fSJohnny Huang 	int option_array[7];
8869d5fd8fSJohnny Huang 	int remain_times;
8969d5fd8fSJohnny Huang 	int writeable_option;
905010032bSJohnny Huang 	int reg_protected;
9169d5fd8fSJohnny Huang 	int protected;
9269d5fd8fSJohnny Huang };
9369d5fd8fSJohnny Huang 
9466f2f8e5SJohnny Huang struct otpconf_parse {
9566f2f8e5SJohnny Huang 	int dw_offset;
9666f2f8e5SJohnny Huang 	int bit;
9766f2f8e5SJohnny Huang 	int length;
9866f2f8e5SJohnny Huang 	int value;
99696656c6SJohnny Huang 	int ignore;
10066f2f8e5SJohnny Huang 	char status[80];
10166f2f8e5SJohnny Huang };
10266f2f8e5SJohnny Huang 
1039a4fe690SJohnny Huang struct otpkey_type {
1049a4fe690SJohnny Huang 	int value;
1059a4fe690SJohnny Huang 	int key_type;
1069a4fe690SJohnny Huang 	int need_id;
1079a4fe690SJohnny Huang 	char information[110];
1089a4fe690SJohnny Huang };
1099a4fe690SJohnny Huang 
1109a4fe690SJohnny Huang struct otp_info_cb {
1119a4fe690SJohnny Huang 	int version;
11279e42a59SJoel Stanley 	const struct otpstrap_info *strap_info;
1139a4fe690SJohnny Huang 	int strap_info_len;
11479e42a59SJoel Stanley 	const struct otpconf_info *conf_info;
1159a4fe690SJohnny Huang 	int conf_info_len;
11679e42a59SJoel Stanley 	const struct otpkey_type *key_info;
1179a4fe690SJohnny Huang 	int key_info_len;
1185010032bSJohnny Huang 
1199a4fe690SJohnny Huang };
1209a4fe690SJohnny Huang 
121696656c6SJohnny Huang struct otp_image_layout {
1225010032bSJohnny Huang 	int data_length;
1235010032bSJohnny Huang 	int conf_length;
1245010032bSJohnny Huang 	int strap_length;
125696656c6SJohnny Huang 	uint8_t *data;
126696656c6SJohnny Huang 	uint8_t *data_ignore;
127696656c6SJohnny Huang 	uint8_t *conf;
128696656c6SJohnny Huang 	uint8_t *conf_ignore;
129696656c6SJohnny Huang 	uint8_t *strap;
130696656c6SJohnny Huang 	uint8_t *strap_reg_pro;
131696656c6SJohnny Huang 	uint8_t *strap_pro;
132696656c6SJohnny Huang 	uint8_t *strap_ignore;
133696656c6SJohnny Huang };
134696656c6SJohnny Huang 
1359a4fe690SJohnny Huang static struct otp_info_cb info_cb;
1369a4fe690SJohnny Huang 
13779e42a59SJoel Stanley static const struct otpkey_type a0_key_type[] = {
1389a4fe690SJohnny Huang 	{0, OTP_KEY_TYPE_AES,   0, "AES-256 as OEM platform key for image encryption/decryption"},
1399a4fe690SJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
1409a4fe690SJohnny Huang 	{4, OTP_KEY_TYPE_HMAC,  1, "HMAC as encrypted OEM HMAC keys in Mode 1"},
1419a4fe690SJohnny Huang 	{8, OTP_KEY_TYPE_RSA,   1, "RSA-public as OEM DSS public keys in Mode 2"},
1429a4fe690SJohnny Huang 	{9, OTP_KEY_TYPE_RSA,   0, "RSA-public as SOC public key"},
1439a4fe690SJohnny Huang 	{10, OTP_KEY_TYPE_RSA,  0, "RSA-public as AES key decryption key"},
1449a4fe690SJohnny Huang 	{13, OTP_KEY_TYPE_RSA,  0, "RSA-private as SOC private key"},
1459a4fe690SJohnny Huang 	{14, OTP_KEY_TYPE_RSA,  0, "RSA-private as AES key decryption key"},
1469a4fe690SJohnny Huang };
1479a4fe690SJohnny Huang 
14879e42a59SJoel Stanley static const struct otpkey_type a1_key_type[] = {
1499a4fe690SJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
1509a4fe690SJohnny 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"},
1519a4fe690SJohnny Huang 	{8, OTP_KEY_TYPE_RSA,   1, "RSA-public as OEM DSS public keys in Mode 2"},
1529a4fe690SJohnny Huang 	{10, OTP_KEY_TYPE_RSA,  0, "RSA-public as AES key decryption key"},
1539a4fe690SJohnny Huang 	{14, OTP_KEY_TYPE_RSA,  0, "RSA-private as AES key decryption key"},
1549a4fe690SJohnny Huang };
1559a4fe690SJohnny Huang 
1565fdde29fSJohnny Huang static const struct otpkey_type a2_key_type[] = {
1575fdde29fSJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
1585fdde29fSJohnny 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"},
1595fdde29fSJohnny Huang 	{8, OTP_KEY_TYPE_RSA,   1, "RSA-public as OEM DSS public keys in Mode 2"},
1605fdde29fSJohnny Huang 	{10, OTP_KEY_TYPE_RSA,  0, "RSA-public as AES key decryption key"},
1615fdde29fSJohnny Huang 	{14, OTP_KEY_TYPE_RSA,  0, "RSA-private as AES key decryption key"},
1625fdde29fSJohnny Huang };
1635fdde29fSJohnny Huang 
1649a4fe690SJohnny Huang static uint32_t  chip_version(void)
1659a4fe690SJohnny Huang {
166badd21c2SJohnny Huang 	u64 rev_id;
1679a4fe690SJohnny Huang 
168badd21c2SJohnny Huang 	rev_id = readl(ASPEED_REVISION_ID0);
169badd21c2SJohnny Huang 	rev_id = ((u64)readl(ASPEED_REVISION_ID1) << 32) | rev_id;
1709a4fe690SJohnny Huang 
171badd21c2SJohnny Huang 	if (rev_id == 0x0500030305000303) {
172badd21c2SJohnny Huang 		/* AST2600-A0 */
1730dae9d52SJohnny Huang 		return OTP_AST2600A0;
174badd21c2SJohnny Huang 	} else if (rev_id == 0x0501030305010303) {
175badd21c2SJohnny Huang 		/* AST2600-A1 */
1760dae9d52SJohnny Huang 		return OTP_AST2600A1;
177badd21c2SJohnny Huang 	} else if (rev_id == 0x0501020305010203) {
178badd21c2SJohnny Huang 		/* AST2620-A1 */
179badd21c2SJohnny Huang 		return OTP_AST2600A1;
180badd21c2SJohnny Huang 	} else if (rev_id == 0x0502030305010303) {
181badd21c2SJohnny Huang 		/* AST2600-A2 */
1820dae9d52SJohnny Huang 		return OTP_AST2600A2;
183badd21c2SJohnny Huang 	} else if (rev_id == 0x0502020305010203) {
184badd21c2SJohnny Huang 		/* AST2620-A2 */
185badd21c2SJohnny Huang 		return OTP_AST2600A2;
186badd21c2SJohnny Huang 	} else if (rev_id == 0x0502010305010103) {
187badd21c2SJohnny Huang 		/* AST2605-A2 */
1880dae9d52SJohnny Huang 		return OTP_AST2600A2;
1890dae9d52SJohnny Huang 	}
1900dae9d52SJohnny Huang 
1915fdde29fSJohnny Huang 	return -1;
1929a4fe690SJohnny Huang }
1939a4fe690SJohnny Huang 
1943d3688adSJohnny Huang static void wait_complete(void)
1953d3688adSJohnny Huang {
1963d3688adSJohnny Huang 	int reg;
1973d3688adSJohnny Huang 
1983d3688adSJohnny Huang 	do {
1993d3688adSJohnny Huang 		reg = readl(OTP_STATUS);
2003d3688adSJohnny Huang 	} while ((reg & 0x6) != 0x6);
2013d3688adSJohnny Huang }
2023d3688adSJohnny Huang 
203dacbba92SJohnny Huang static void otp_write(uint32_t otp_addr, uint32_t data)
204dacbba92SJohnny Huang {
205dacbba92SJohnny Huang 	writel(otp_addr, OTP_ADDR); //write address
206dacbba92SJohnny Huang 	writel(data, OTP_COMPARE_1); //write data
207dacbba92SJohnny Huang 	writel(0x23b1e362, OTP_COMMAND); //write command
208dacbba92SJohnny Huang 	wait_complete();
209dacbba92SJohnny Huang }
210dacbba92SJohnny Huang 
211dacbba92SJohnny Huang static void otp_soak(int soak)
212dacbba92SJohnny Huang {
213dacbba92SJohnny Huang 	if (info_cb.version == OTP_AST2600A2) {
214dacbba92SJohnny Huang 		switch (soak) {
215dacbba92SJohnny Huang 		case 0: //default
216dacbba92SJohnny Huang 			otp_write(0x3000, 0x0210); // Write MRA
217dacbba92SJohnny Huang 			otp_write(0x5000, 0x2000); // Write MRB
218dacbba92SJohnny Huang 			otp_write(0x1000, 0x0); // Write MR
219dacbba92SJohnny Huang 			break;
220dacbba92SJohnny Huang 		case 1: //normal program
221dacbba92SJohnny Huang 			otp_write(0x3000, 0x1200); // Write MRA
222feea3fdfSJohnny Huang 			otp_write(0x5000, 0x107F); // Write MRB
223dacbba92SJohnny Huang 			otp_write(0x1000, 0x1024); // Write MR
224feea3fdfSJohnny Huang 			writel(0x04191388, OTP_TIMING); // 200us
225dacbba92SJohnny Huang 			break;
226dacbba92SJohnny Huang 		case 2: //soak program
227dacbba92SJohnny Huang 			otp_write(0x3000, 0x1220); // Write MRA
228feea3fdfSJohnny Huang 			otp_write(0x5000, 0x2074); // Write MRB
229dacbba92SJohnny Huang 			otp_write(0x1000, 0x08a4); // Write MR
230feea3fdfSJohnny Huang 			writel(0x04193a98, OTP_TIMING); // 600us
231dacbba92SJohnny Huang 			break;
232dacbba92SJohnny Huang 		}
233dacbba92SJohnny Huang 	} else {
234dacbba92SJohnny Huang 		switch (soak) {
235dacbba92SJohnny Huang 		case 0: //default
236dacbba92SJohnny Huang 			otp_write(0x3000, 0x0); // Write MRA
237dacbba92SJohnny Huang 			otp_write(0x5000, 0x0); // Write MRB
238dacbba92SJohnny Huang 			otp_write(0x1000, 0x0); // Write MR
239dacbba92SJohnny Huang 			break;
240dacbba92SJohnny Huang 		case 1: //normal program
241dacbba92SJohnny Huang 			otp_write(0x3000, 0x4021); // Write MRA
242dacbba92SJohnny Huang 			otp_write(0x5000, 0x302f); // Write MRB
243dacbba92SJohnny Huang 			otp_write(0x1000, 0x4020); // Write MR
244feea3fdfSJohnny Huang 			writel(0x04190760, OTP_TIMING); // 75us
245dacbba92SJohnny Huang 			break;
246dacbba92SJohnny Huang 		case 2: //soak program
247dacbba92SJohnny Huang 			otp_write(0x3000, 0x4021); // Write MRA
248dacbba92SJohnny Huang 			otp_write(0x5000, 0x1027); // Write MRB
249dacbba92SJohnny Huang 			otp_write(0x1000, 0x4820); // Write MR
250feea3fdfSJohnny Huang 			writel(0x041930d4, OTP_TIMING); // 500us
251dacbba92SJohnny Huang 			break;
252dacbba92SJohnny Huang 		}
253dacbba92SJohnny Huang 	}
254dacbba92SJohnny Huang 
255dacbba92SJohnny Huang 	wait_complete();
256dacbba92SJohnny Huang }
257dacbba92SJohnny Huang 
2582a856b9aSJohnny Huang static void otp_read_data(uint32_t offset, uint32_t *data)
25969d5fd8fSJohnny Huang {
2603d3688adSJohnny Huang 	writel(offset, OTP_ADDR); //Read address
2613d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
2623d3688adSJohnny Huang 	wait_complete();
2633d3688adSJohnny Huang 	data[0] = readl(OTP_COMPARE_1);
2643d3688adSJohnny Huang 	data[1] = readl(OTP_COMPARE_2);
26569d5fd8fSJohnny Huang }
26669d5fd8fSJohnny Huang 
2672a856b9aSJohnny Huang static void otp_read_config(uint32_t offset, uint32_t *data)
26869d5fd8fSJohnny Huang {
26969d5fd8fSJohnny Huang 	int config_offset;
27069d5fd8fSJohnny Huang 
27169d5fd8fSJohnny Huang 	config_offset = 0x800;
27269d5fd8fSJohnny Huang 	config_offset |= (offset / 8) * 0x200;
27369d5fd8fSJohnny Huang 	config_offset |= (offset % 8) * 0x2;
27469d5fd8fSJohnny Huang 
2753d3688adSJohnny Huang 	writel(config_offset, OTP_ADDR);  //Read address
2763d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
2773d3688adSJohnny Huang 	wait_complete();
2783d3688adSJohnny Huang 	data[0] = readl(OTP_COMPARE_1);
27969d5fd8fSJohnny Huang }
28069d5fd8fSJohnny Huang 
28169d5fd8fSJohnny Huang static int otp_print_config(uint32_t offset, int dw_count)
28269d5fd8fSJohnny Huang {
28369d5fd8fSJohnny Huang 	int i;
28469d5fd8fSJohnny Huang 	uint32_t ret[1];
28569d5fd8fSJohnny Huang 
28669d5fd8fSJohnny Huang 	if (offset + dw_count > 32)
2872a856b9aSJohnny Huang 		return OTP_USAGE;
288dacbba92SJohnny Huang 	otp_soak(0);
28969d5fd8fSJohnny Huang 	for (i = offset; i < offset + dw_count; i ++) {
29069d5fd8fSJohnny Huang 		otp_read_config(i, ret);
291a6af4a17SJohnny Huang 		printf("OTPCFG%X: %08X\n", i, ret[0]);
29269d5fd8fSJohnny Huang 	}
29369d5fd8fSJohnny Huang 	printf("\n");
2942a856b9aSJohnny Huang 	return OTP_SUCCESS;
29569d5fd8fSJohnny Huang }
29669d5fd8fSJohnny Huang 
29769d5fd8fSJohnny Huang static int otp_print_data(uint32_t offset, int dw_count)
29869d5fd8fSJohnny Huang {
29969d5fd8fSJohnny Huang 	int i;
30069d5fd8fSJohnny Huang 	uint32_t ret[2];
30169d5fd8fSJohnny Huang 
30269d5fd8fSJohnny Huang 	if (offset + dw_count > 2048 || offset % 4 != 0)
3032a856b9aSJohnny Huang 		return OTP_USAGE;
304dacbba92SJohnny Huang 	otp_soak(0);
30569d5fd8fSJohnny Huang 	for (i = offset; i < offset + dw_count; i += 2) {
30669d5fd8fSJohnny Huang 		otp_read_data(i, ret);
30769d5fd8fSJohnny Huang 		if (i % 4 == 0)
30869d5fd8fSJohnny Huang 			printf("%03X: %08X %08X ", i * 4, ret[0], ret[1]);
30969d5fd8fSJohnny Huang 		else
31069d5fd8fSJohnny Huang 			printf("%08X %08X\n", ret[0], ret[1]);
31169d5fd8fSJohnny Huang 
31269d5fd8fSJohnny Huang 	}
31369d5fd8fSJohnny Huang 	printf("\n");
3142a856b9aSJohnny Huang 	return OTP_SUCCESS;
31569d5fd8fSJohnny Huang }
31669d5fd8fSJohnny Huang 
31769d5fd8fSJohnny Huang static int otp_compare(uint32_t otp_addr, uint32_t addr)
31869d5fd8fSJohnny Huang {
31969d5fd8fSJohnny Huang 	uint32_t ret;
32069d5fd8fSJohnny Huang 	uint32_t *buf;
32169d5fd8fSJohnny Huang 
32269d5fd8fSJohnny Huang 	buf = map_physmem(addr, 16, MAP_WRBACK);
32369d5fd8fSJohnny Huang 	printf("%08X\n", buf[0]);
32469d5fd8fSJohnny Huang 	printf("%08X\n", buf[1]);
32569d5fd8fSJohnny Huang 	printf("%08X\n", buf[2]);
32669d5fd8fSJohnny Huang 	printf("%08X\n", buf[3]);
3273d3688adSJohnny Huang 	writel(otp_addr, OTP_ADDR); //Compare address
3283d3688adSJohnny Huang 	writel(buf[0], OTP_COMPARE_1); //Compare data 1
3293d3688adSJohnny Huang 	writel(buf[1], OTP_COMPARE_2); //Compare data 2
3303d3688adSJohnny Huang 	writel(buf[2], OTP_COMPARE_3); //Compare data 3
3313d3688adSJohnny Huang 	writel(buf[3], OTP_COMPARE_4); //Compare data 4
3323d3688adSJohnny Huang 	writel(0x23b1e363, OTP_COMMAND); //Compare command
3333d3688adSJohnny Huang 	wait_complete();
3343d3688adSJohnny Huang 	ret = readl(OTP_STATUS); //Compare command
33569d5fd8fSJohnny Huang 	if (ret & 0x1)
33669d5fd8fSJohnny Huang 		return 0;
33769d5fd8fSJohnny Huang 	else
33869d5fd8fSJohnny Huang 		return -1;
33969d5fd8fSJohnny Huang }
34069d5fd8fSJohnny Huang 
341a6d0d645SJohnny Huang static int verify_bit(uint32_t otp_addr, int bit_offset, int value)
34269d5fd8fSJohnny Huang {
34330a8c590SJohnny Huang 	uint32_t ret[2];
34469d5fd8fSJohnny Huang 
34530a8c590SJohnny Huang 	if (otp_addr % 2 == 0)
3463d3688adSJohnny Huang 		writel(otp_addr, OTP_ADDR); //Read address
34730a8c590SJohnny Huang 	else
3483d3688adSJohnny Huang 		writel(otp_addr - 1, OTP_ADDR); //Read address
34930a8c590SJohnny Huang 
3503d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
3513d3688adSJohnny Huang 	wait_complete();
3523d3688adSJohnny Huang 	ret[0] = readl(OTP_COMPARE_1);
3533d3688adSJohnny Huang 	ret[1] = readl(OTP_COMPARE_2);
35483655e91SJohnny Huang 
35530a8c590SJohnny Huang 	if (otp_addr % 2 == 0) {
35630a8c590SJohnny Huang 		if (((ret[0] >> bit_offset) & 1) == value)
35769d5fd8fSJohnny Huang 			return 0;
35869d5fd8fSJohnny Huang 		else
35969d5fd8fSJohnny Huang 			return -1;
36030a8c590SJohnny Huang 	} else {
36130a8c590SJohnny Huang 		if (((ret[1] >> bit_offset) & 1) == value)
36230a8c590SJohnny Huang 			return 0;
36330a8c590SJohnny Huang 		else
36430a8c590SJohnny Huang 			return -1;
36530a8c590SJohnny Huang 	}
36630a8c590SJohnny Huang 
36769d5fd8fSJohnny Huang }
36869d5fd8fSJohnny Huang 
369696656c6SJohnny Huang static uint32_t verify_dw(uint32_t otp_addr, uint32_t *value, uint32_t *ignore, uint32_t *compare, int size)
3704c1c9b35SJohnny Huang {
3714c1c9b35SJohnny Huang 	uint32_t ret[2];
3724c1c9b35SJohnny Huang 
3734c1c9b35SJohnny Huang 	otp_addr &= ~(1 << 15);
3744c1c9b35SJohnny Huang 
3754c1c9b35SJohnny Huang 	if (otp_addr % 2 == 0)
3763d3688adSJohnny Huang 		writel(otp_addr, OTP_ADDR); //Read address
3774c1c9b35SJohnny Huang 	else
3783d3688adSJohnny Huang 		writel(otp_addr - 1, OTP_ADDR); //Read address
3793d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
3803d3688adSJohnny Huang 	wait_complete();
3813d3688adSJohnny Huang 	ret[0] = readl(OTP_COMPARE_1);
3823d3688adSJohnny Huang 	ret[1] = readl(OTP_COMPARE_2);
3834c1c9b35SJohnny Huang 	if (size == 1) {
3844c1c9b35SJohnny Huang 		if (otp_addr % 2 == 0) {
3854c1c9b35SJohnny Huang 			// printf("check %x : %x = %x\n", otp_addr, ret[0], value[0]);
386696656c6SJohnny Huang 			if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0])) {
3874c1c9b35SJohnny Huang 				compare[0] = 0;
3884c1c9b35SJohnny Huang 				return 0;
3894c1c9b35SJohnny Huang 			} else {
3904c1c9b35SJohnny Huang 				compare[0] = value[0] ^ ret[0];
3914c1c9b35SJohnny Huang 				return -1;
3924c1c9b35SJohnny Huang 			}
3934c1c9b35SJohnny Huang 
3944c1c9b35SJohnny Huang 		} else {
3954c1c9b35SJohnny Huang 			// printf("check %x : %x = %x\n", otp_addr, ret[1], value[0]);
396696656c6SJohnny Huang 			if ((value[0] & ~ignore[0]) == (ret[1] & ~ignore[0])) {
3974c1c9b35SJohnny Huang 				compare[0] = ~0;
3984c1c9b35SJohnny Huang 				return 0;
3994c1c9b35SJohnny Huang 			} else {
400d90825e2SJohnny Huang 				compare[0] = ~(value[0] ^ ret[1]);
4014c1c9b35SJohnny Huang 				return -1;
4024c1c9b35SJohnny Huang 			}
4034c1c9b35SJohnny Huang 		}
4044c1c9b35SJohnny Huang 	} else if (size == 2) {
4054c1c9b35SJohnny Huang 		// otp_addr should be even
406696656c6SJohnny Huang 		if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0]) && (value[1] & ~ignore[1]) == (ret[1] & ~ignore[1])) {
4074c1c9b35SJohnny Huang 			// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
4084c1c9b35SJohnny Huang 			// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
4094c1c9b35SJohnny Huang 			compare[0] = 0;
4104c1c9b35SJohnny Huang 			compare[1] = ~0;
4114c1c9b35SJohnny Huang 			return 0;
4124c1c9b35SJohnny Huang 		} else {
4134c1c9b35SJohnny Huang 			// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
4144c1c9b35SJohnny Huang 			// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
4154c1c9b35SJohnny Huang 			compare[0] = value[0] ^ ret[0];
4164c1c9b35SJohnny Huang 			compare[1] = ~(value[1] ^ ret[1]);
4174c1c9b35SJohnny Huang 			return -1;
4184c1c9b35SJohnny Huang 		}
4194c1c9b35SJohnny Huang 	} else {
4204c1c9b35SJohnny Huang 		return -1;
4214c1c9b35SJohnny Huang 	}
4224c1c9b35SJohnny Huang }
4234c1c9b35SJohnny Huang 
42483655e91SJohnny Huang static void otp_prog(uint32_t otp_addr, uint32_t prog_bit)
42583655e91SJohnny Huang {
42683655e91SJohnny Huang 	writel(otp_addr, OTP_ADDR); //write address
42783655e91SJohnny Huang 	writel(prog_bit, OTP_COMPARE_1); //write data
42883655e91SJohnny Huang 	writel(0x23b1e364, OTP_COMMAND); //write command
42983655e91SJohnny Huang 	wait_complete();
43083655e91SJohnny Huang }
43183655e91SJohnny Huang 
43283655e91SJohnny Huang static void _otp_prog_bit(uint32_t value, uint32_t prog_address, uint32_t bit_offset)
43383655e91SJohnny Huang {
43483655e91SJohnny Huang 	int prog_bit;
43583655e91SJohnny Huang 
43683655e91SJohnny Huang 	if (prog_address % 2 == 0) {
43783655e91SJohnny Huang 		if (value)
43883655e91SJohnny Huang 			prog_bit = ~(0x1 << bit_offset);
43983655e91SJohnny Huang 		else
44083655e91SJohnny Huang 			return;
44183655e91SJohnny Huang 	} else {
44283655e91SJohnny Huang 		prog_address |= 1 << 15;
44383655e91SJohnny Huang 		if (!value)
44483655e91SJohnny Huang 			prog_bit = 0x1 << bit_offset;
44583655e91SJohnny Huang 		else
44683655e91SJohnny Huang 			return;
44783655e91SJohnny Huang 	}
44883655e91SJohnny Huang 	otp_prog(prog_address, prog_bit);
44983655e91SJohnny Huang }
45083655e91SJohnny Huang 
45183655e91SJohnny Huang static int otp_prog_bit(uint32_t value, uint32_t prog_address, uint32_t bit_offset)
45283655e91SJohnny Huang {
45383655e91SJohnny Huang 	int pass;
45483655e91SJohnny Huang 	int i;
45583655e91SJohnny Huang 
45683655e91SJohnny Huang 	otp_soak(1);
45783655e91SJohnny Huang 	_otp_prog_bit(value, prog_address, bit_offset);
45883655e91SJohnny Huang 	pass = 0;
45983655e91SJohnny Huang 
46083655e91SJohnny Huang 	for (i = 0; i < RETRY; i++) {
46183655e91SJohnny Huang 		if (verify_bit(prog_address, bit_offset, value) != 0) {
46283655e91SJohnny Huang 			otp_soak(2);
46383655e91SJohnny Huang 			_otp_prog_bit(value, prog_address, bit_offset);
46483655e91SJohnny Huang 			if (verify_bit(prog_address, bit_offset, value) != 0) {
46583655e91SJohnny Huang 				otp_soak(1);
46683655e91SJohnny Huang 			} else {
46783655e91SJohnny Huang 				pass = 1;
46883655e91SJohnny Huang 				break;
46983655e91SJohnny Huang 			}
47083655e91SJohnny Huang 		} else {
47183655e91SJohnny Huang 			pass = 1;
47283655e91SJohnny Huang 			break;
47383655e91SJohnny Huang 		}
47483655e91SJohnny Huang 	}
47583655e91SJohnny Huang 
47683655e91SJohnny Huang 	return pass;
47783655e91SJohnny Huang }
47883655e91SJohnny Huang 
479696656c6SJohnny Huang static void otp_prog_dw(uint32_t value, uint32_t ignore, uint32_t prog_address)
480d90825e2SJohnny Huang {
481d90825e2SJohnny Huang 	int j, bit_value, prog_bit;
482d90825e2SJohnny Huang 
483d90825e2SJohnny Huang 	for (j = 0; j < 32; j++) {
484696656c6SJohnny Huang 		if ((ignore >> j) & 0x1)
485d90825e2SJohnny Huang 			continue;
486d90825e2SJohnny Huang 		bit_value = (value >> j) & 0x1;
487d90825e2SJohnny Huang 		if (prog_address % 2 == 0) {
488d90825e2SJohnny Huang 			if (bit_value)
489d90825e2SJohnny Huang 				prog_bit = ~(0x1 << j);
490d90825e2SJohnny Huang 			else
491d90825e2SJohnny Huang 				continue;
492d90825e2SJohnny Huang 		} else {
493d90825e2SJohnny Huang 			prog_address |= 1 << 15;
494d90825e2SJohnny Huang 			if (bit_value)
495d90825e2SJohnny Huang 				continue;
496d90825e2SJohnny Huang 			else
497d90825e2SJohnny Huang 				prog_bit = 0x1 << j;
498d90825e2SJohnny Huang 		}
499d90825e2SJohnny Huang 		otp_prog(prog_address, prog_bit);
500d90825e2SJohnny Huang 	}
501d90825e2SJohnny Huang }
502d90825e2SJohnny Huang 
50354552c69SJohnny Huang static int otp_prog_verify_2dw(uint32_t *data, uint32_t *buf, uint32_t *ignore_mask, uint32_t prog_address)
50454552c69SJohnny Huang {
50554552c69SJohnny Huang 	int pass;
50654552c69SJohnny Huang 	int i;
50754552c69SJohnny Huang 	uint32_t data0_masked;
50854552c69SJohnny Huang 	uint32_t data1_masked;
50954552c69SJohnny Huang 	uint32_t buf0_masked;
51054552c69SJohnny Huang 	uint32_t buf1_masked;
51154552c69SJohnny Huang 	uint32_t compare[2];
51254552c69SJohnny Huang 
51354552c69SJohnny Huang 	data0_masked = data[0]  & ~ignore_mask[0];
51454552c69SJohnny Huang 	buf0_masked  = buf[0] & ~ignore_mask[0];
51554552c69SJohnny Huang 	data1_masked = data[1]  & ~ignore_mask[1];
51654552c69SJohnny Huang 	buf1_masked  = buf[1] & ~ignore_mask[1];
51754552c69SJohnny Huang 	if ((data0_masked == buf0_masked) && (data1_masked == buf1_masked))
51854552c69SJohnny Huang 		return 0;
51954552c69SJohnny Huang 
52054552c69SJohnny Huang 	otp_soak(1);
52154552c69SJohnny Huang 	if (data0_masked != buf0_masked)
52254552c69SJohnny Huang 		otp_prog_dw(buf[0], ignore_mask[0], prog_address);
52354552c69SJohnny Huang 	if (data1_masked != buf1_masked)
52454552c69SJohnny Huang 		otp_prog_dw(buf[1], ignore_mask[1], prog_address + 1);
52554552c69SJohnny Huang 
52654552c69SJohnny Huang 	pass = 0;
52754552c69SJohnny Huang 	for (i = 0; i < RETRY; i++) {
52854552c69SJohnny Huang 		if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) {
52954552c69SJohnny Huang 			otp_soak(2);
53054552c69SJohnny Huang 			if (compare[0] != 0) {
53154552c69SJohnny Huang 				otp_prog_dw(compare[0], ignore_mask[0], prog_address);
53254552c69SJohnny Huang 			}
53354552c69SJohnny Huang 			if (compare[1] != ~0) {
5345537bc72SJohnny Huang 				otp_prog_dw(compare[1], ignore_mask[1], prog_address + 1);
53554552c69SJohnny Huang 			}
53654552c69SJohnny Huang 			if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) {
53754552c69SJohnny Huang 				otp_soak(1);
53854552c69SJohnny Huang 			} else {
53954552c69SJohnny Huang 				pass = 1;
54054552c69SJohnny Huang 				break;
54154552c69SJohnny Huang 			}
54254552c69SJohnny Huang 		} else {
54354552c69SJohnny Huang 			pass = 1;
54454552c69SJohnny Huang 			break;
54554552c69SJohnny Huang 		}
54654552c69SJohnny Huang 	}
54754552c69SJohnny Huang 
54854552c69SJohnny Huang 	if (!pass) {
54954552c69SJohnny Huang 		otp_soak(0);
55054552c69SJohnny Huang 		return OTP_FAILURE;
55154552c69SJohnny Huang 	}
55254552c69SJohnny Huang 	return OTP_SUCCESS;
55354552c69SJohnny Huang }
55454552c69SJohnny Huang 
555541eb887SJohnny Huang static void otp_strap_status(struct otpstrap_status *otpstrap)
55676d13988SJohnny Huang {
55776d13988SJohnny Huang 	uint32_t OTPSTRAP_RAW[2];
5585010032bSJohnny Huang 	int strap_end;
55976d13988SJohnny Huang 	int i, j;
56076d13988SJohnny Huang 
5615010032bSJohnny Huang 	if (info_cb.version == OTP_AST2600A0) {
56276d13988SJohnny Huang 		for (j = 0; j < 64; j++) {
56376d13988SJohnny Huang 			otpstrap[j].value = 0;
56476d13988SJohnny Huang 			otpstrap[j].remain_times = 7;
56576d13988SJohnny Huang 			otpstrap[j].writeable_option = -1;
56676d13988SJohnny Huang 			otpstrap[j].protected = 0;
56776d13988SJohnny Huang 		}
5685010032bSJohnny Huang 		strap_end = 30;
5695010032bSJohnny Huang 	} else {
5705010032bSJohnny Huang 		for (j = 0; j < 64; j++) {
5715010032bSJohnny Huang 			otpstrap[j].value = 0;
5725010032bSJohnny Huang 			otpstrap[j].remain_times = 6;
5735010032bSJohnny Huang 			otpstrap[j].writeable_option = -1;
5745010032bSJohnny Huang 			otpstrap[j].reg_protected = 0;
5755010032bSJohnny Huang 			otpstrap[j].protected = 0;
5765010032bSJohnny Huang 		}
5775010032bSJohnny Huang 		strap_end = 28;
5785010032bSJohnny Huang 	}
57976d13988SJohnny Huang 
580dacbba92SJohnny Huang 	otp_soak(0);
5815010032bSJohnny Huang 	for (i = 16; i < strap_end; i += 2) {
58276d13988SJohnny Huang 		int option = (i - 16) / 2;
58376d13988SJohnny Huang 		otp_read_config(i, &OTPSTRAP_RAW[0]);
58476d13988SJohnny Huang 		otp_read_config(i + 1, &OTPSTRAP_RAW[1]);
58576d13988SJohnny Huang 		for (j = 0; j < 32; j++) {
58676d13988SJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[0] >> j) & 0x1);
58776d13988SJohnny Huang 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
58876d13988SJohnny Huang 				otpstrap[j].writeable_option = option;
58976d13988SJohnny Huang 			}
59076d13988SJohnny Huang 			if (bit_value == 1)
59176d13988SJohnny Huang 				otpstrap[j].remain_times --;
59276d13988SJohnny Huang 			otpstrap[j].value ^= bit_value;
59376d13988SJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
59476d13988SJohnny Huang 		}
59576d13988SJohnny Huang 		for (j = 32; j < 64; j++) {
59676d13988SJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1);
59776d13988SJohnny Huang 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
59876d13988SJohnny Huang 				otpstrap[j].writeable_option = option;
59976d13988SJohnny Huang 			}
60076d13988SJohnny Huang 			if (bit_value == 1)
60176d13988SJohnny Huang 				otpstrap[j].remain_times --;
60276d13988SJohnny Huang 			otpstrap[j].value ^= bit_value;
60376d13988SJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
60476d13988SJohnny Huang 		}
60576d13988SJohnny Huang 	}
6065010032bSJohnny Huang 
6075010032bSJohnny Huang 	if (info_cb.version != OTP_AST2600A0) {
6085010032bSJohnny Huang 		otp_read_config(28, &OTPSTRAP_RAW[0]);
6095010032bSJohnny Huang 		otp_read_config(29, &OTPSTRAP_RAW[1]);
6105010032bSJohnny Huang 		for (j = 0; j < 32; j++) {
6115010032bSJohnny Huang 			if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
6125010032bSJohnny Huang 				otpstrap[j].reg_protected = 1;
6135010032bSJohnny Huang 		}
6145010032bSJohnny Huang 		for (j = 32; j < 64; j++) {
6155010032bSJohnny Huang 			if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
6165010032bSJohnny Huang 				otpstrap[j].reg_protected = 1;
6175010032bSJohnny Huang 		}
6185010032bSJohnny Huang 
6195010032bSJohnny Huang 	}
6205010032bSJohnny Huang 
62176d13988SJohnny Huang 	otp_read_config(30, &OTPSTRAP_RAW[0]);
62276d13988SJohnny Huang 	otp_read_config(31, &OTPSTRAP_RAW[1]);
62376d13988SJohnny Huang 	for (j = 0; j < 32; j++) {
62476d13988SJohnny Huang 		if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
62576d13988SJohnny Huang 			otpstrap[j].protected = 1;
62676d13988SJohnny Huang 	}
62776d13988SJohnny Huang 	for (j = 32; j < 64; j++) {
62876d13988SJohnny Huang 		if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
62976d13988SJohnny Huang 			otpstrap[j].protected = 1;
63076d13988SJohnny Huang 	}
63176d13988SJohnny Huang }
63276d13988SJohnny Huang 
633696656c6SJohnny Huang static int otp_print_conf_image(struct otp_image_layout *image_layout)
63469d5fd8fSJohnny Huang {
63579e42a59SJoel Stanley 	const struct otpconf_info *conf_info = info_cb.conf_info;
636696656c6SJohnny Huang 	uint32_t *OTPCFG = (uint32_t *)image_layout->conf;
637696656c6SJohnny Huang 	uint32_t *OTPCFG_IGNORE = (uint32_t *)image_layout->conf_ignore;
638b458cd62SJohnny Huang 	uint32_t mask;
639b458cd62SJohnny Huang 	uint32_t dw_offset;
640b458cd62SJohnny Huang 	uint32_t bit_offset;
641b458cd62SJohnny Huang 	uint32_t otp_value;
642696656c6SJohnny Huang 	uint32_t otp_ignore;
643b458cd62SJohnny Huang 	int fail = 0;
64473f11549SJohnny Huang 	char valid_bit[20];
64566f2f8e5SJohnny Huang 	int i;
64673f11549SJohnny Huang 	int j;
64766f2f8e5SJohnny Huang 
648737ed20bSJohnny Huang 	printf("DW    BIT        Value       Description\n");
64966f2f8e5SJohnny Huang 	printf("__________________________________________________________________________\n");
6503cb28812SJohnny Huang 	for (i = 0; i < info_cb.conf_info_len; i++) {
6513cb28812SJohnny Huang 		dw_offset = conf_info[i].dw_offset;
6523cb28812SJohnny Huang 		bit_offset = conf_info[i].bit_offset;
6533cb28812SJohnny Huang 		mask = BIT(conf_info[i].length) - 1;
654b458cd62SJohnny Huang 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
655696656c6SJohnny Huang 		otp_ignore = (OTPCFG_IGNORE[dw_offset] >> bit_offset) & mask;
656b458cd62SJohnny Huang 
657696656c6SJohnny Huang 		if (otp_ignore == mask) {
658b458cd62SJohnny Huang 			continue;
659696656c6SJohnny Huang 		} else if (otp_ignore != 0) {
660b458cd62SJohnny Huang 			fail = 1;
661b458cd62SJohnny Huang 		}
662b458cd62SJohnny Huang 
6633cb28812SJohnny Huang 		if ((otp_value != conf_info[i].value) &&
6643cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_RESERVED &&
6653cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALUE &&
6663cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALID_BIT)
667b458cd62SJohnny Huang 			continue;
668b458cd62SJohnny Huang 		printf("0x%-4X", dw_offset);
669b458cd62SJohnny Huang 
6703cb28812SJohnny Huang 		if (conf_info[i].length == 1) {
6713cb28812SJohnny Huang 			printf("0x%-9X", conf_info[i].bit_offset);
67266f2f8e5SJohnny Huang 		} else {
673b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
6743cb28812SJohnny Huang 			       conf_info[i].bit_offset + conf_info[i].length - 1,
6753cb28812SJohnny Huang 			       conf_info[i].bit_offset);
67666f2f8e5SJohnny Huang 		}
677b458cd62SJohnny Huang 		printf("0x%-10x", otp_value);
678b458cd62SJohnny Huang 
679b458cd62SJohnny Huang 		if (fail) {
680696656c6SJohnny Huang 			printf("Ignore mask error\n");
681b458cd62SJohnny Huang 		} else {
6823cb28812SJohnny Huang 			if (conf_info[i].value == OTP_REG_RESERVED) {
683b458cd62SJohnny Huang 				printf("Reserved\n");
6843cb28812SJohnny Huang 			} else if (conf_info[i].value == OTP_REG_VALUE) {
6853cb28812SJohnny Huang 				printf(conf_info[i].information, otp_value);
686b458cd62SJohnny Huang 				printf("\n");
6873cb28812SJohnny Huang 			} else if (conf_info[i].value == OTP_REG_VALID_BIT) {
688b458cd62SJohnny Huang 				if (otp_value != 0) {
68973f11549SJohnny Huang 					for (j = 0; j < 7; j++) {
69073f11549SJohnny Huang 						if (otp_value == (1 << j)) {
69173f11549SJohnny Huang 							valid_bit[j * 2] = '1';
692b458cd62SJohnny Huang 						} else {
69373f11549SJohnny Huang 							valid_bit[j * 2] = '0';
69473f11549SJohnny Huang 						}
69573f11549SJohnny Huang 						valid_bit[j * 2 + 1] = ' ';
69673f11549SJohnny Huang 					}
69773f11549SJohnny Huang 					valid_bit[15] = 0;
69873f11549SJohnny Huang 				} else {
69973f11549SJohnny Huang 					strcpy(valid_bit, "0 0 0 0 0 0 0 0\0");
700b458cd62SJohnny Huang 				}
7013cb28812SJohnny Huang 				printf(conf_info[i].information, valid_bit);
702b458cd62SJohnny Huang 				printf("\n");
703b458cd62SJohnny Huang 			} else {
7043cb28812SJohnny Huang 				printf("%s\n", conf_info[i].information);
705b458cd62SJohnny Huang 			}
706b458cd62SJohnny Huang 		}
707b458cd62SJohnny Huang 	}
708b458cd62SJohnny Huang 
709b458cd62SJohnny Huang 	if (fail)
710b458cd62SJohnny Huang 		return OTP_FAILURE;
711b458cd62SJohnny Huang 
71266f2f8e5SJohnny Huang 	return OTP_SUCCESS;
71366f2f8e5SJohnny Huang }
71466f2f8e5SJohnny Huang 
7152d4b0742SJohnny Huang static int otp_print_conf_info(int input_offset)
71666f2f8e5SJohnny Huang {
71779e42a59SJoel Stanley 	const struct otpconf_info *conf_info = info_cb.conf_info;
718bb34a7bfSJohnny Huang 	uint32_t OTPCFG[16];
719b458cd62SJohnny Huang 	uint32_t mask;
720b458cd62SJohnny Huang 	uint32_t dw_offset;
721b458cd62SJohnny Huang 	uint32_t bit_offset;
722b458cd62SJohnny Huang 	uint32_t otp_value;
72373f11549SJohnny Huang 	char valid_bit[20];
72466f2f8e5SJohnny Huang 	int i;
72573f11549SJohnny Huang 	int j;
72666f2f8e5SJohnny Huang 
727dacbba92SJohnny Huang 	otp_soak(0);
728bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++)
72966f2f8e5SJohnny Huang 		otp_read_config(i, &OTPCFG[i]);
73066f2f8e5SJohnny Huang 
73166f2f8e5SJohnny Huang 
732b458cd62SJohnny Huang 	printf("DW    BIT        Value       Description\n");
733b458cd62SJohnny Huang 	printf("__________________________________________________________________________\n");
7343cb28812SJohnny Huang 	for (i = 0; i < info_cb.conf_info_len; i++) {
7353cb28812SJohnny Huang 		if (input_offset != -1 && input_offset != conf_info[i].dw_offset)
7362d4b0742SJohnny Huang 			continue;
7373cb28812SJohnny Huang 		dw_offset = conf_info[i].dw_offset;
7383cb28812SJohnny Huang 		bit_offset = conf_info[i].bit_offset;
7393cb28812SJohnny Huang 		mask = BIT(conf_info[i].length) - 1;
740b458cd62SJohnny Huang 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
741b458cd62SJohnny Huang 
7423cb28812SJohnny Huang 		if ((otp_value != conf_info[i].value) &&
7433cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_RESERVED &&
7443cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALUE &&
7453cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALID_BIT)
746b458cd62SJohnny Huang 			continue;
747b458cd62SJohnny Huang 		printf("0x%-4X", dw_offset);
748b458cd62SJohnny Huang 
7493cb28812SJohnny Huang 		if (conf_info[i].length == 1) {
7503cb28812SJohnny Huang 			printf("0x%-9X", conf_info[i].bit_offset);
751b458cd62SJohnny Huang 		} else {
752b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
7533cb28812SJohnny Huang 			       conf_info[i].bit_offset + conf_info[i].length - 1,
7543cb28812SJohnny Huang 			       conf_info[i].bit_offset);
755b458cd62SJohnny Huang 		}
756b458cd62SJohnny Huang 		printf("0x%-10x", otp_value);
757b458cd62SJohnny Huang 
7583cb28812SJohnny Huang 		if (conf_info[i].value == OTP_REG_RESERVED) {
759b458cd62SJohnny Huang 			printf("Reserved\n");
7603cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALUE) {
7613cb28812SJohnny Huang 			printf(conf_info[i].information, otp_value);
762b458cd62SJohnny Huang 			printf("\n");
7633cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALID_BIT) {
764b458cd62SJohnny Huang 			if (otp_value != 0) {
76573f11549SJohnny Huang 				for (j = 0; j < 7; j++) {
76673f11549SJohnny Huang 					if (otp_value == (1 << j)) {
76773f11549SJohnny Huang 						valid_bit[j * 2] = '1';
768b458cd62SJohnny Huang 					} else {
76973f11549SJohnny Huang 						valid_bit[j * 2] = '0';
77073f11549SJohnny Huang 					}
77173f11549SJohnny Huang 					valid_bit[j * 2 + 1] = ' ';
77273f11549SJohnny Huang 				}
77373f11549SJohnny Huang 				valid_bit[15] = 0;
77473f11549SJohnny Huang 			} else {
77573f11549SJohnny Huang 				strcpy(valid_bit, "0 0 0 0 0 0 0 0\0");
776b458cd62SJohnny Huang 			}
7773cb28812SJohnny Huang 			printf(conf_info[i].information, valid_bit);
778b458cd62SJohnny Huang 			printf("\n");
779b458cd62SJohnny Huang 		} else {
7803cb28812SJohnny Huang 			printf("%s\n", conf_info[i].information);
781b458cd62SJohnny Huang 		}
782b458cd62SJohnny Huang 	}
783b458cd62SJohnny Huang 	return OTP_SUCCESS;
78466f2f8e5SJohnny Huang }
78566f2f8e5SJohnny Huang 
7865010032bSJohnny Huang static int otp_print_strap_image(struct otp_image_layout *image_layout)
78776d13988SJohnny Huang {
78879e42a59SJoel Stanley 	const struct otpstrap_info *strap_info = info_cb.strap_info;
789696656c6SJohnny Huang 	uint32_t *OTPSTRAP;
790696656c6SJohnny Huang 	uint32_t *OTPSTRAP_REG_PRO;
791696656c6SJohnny Huang 	uint32_t *OTPSTRAP_PRO;
792696656c6SJohnny Huang 	uint32_t *OTPSTRAP_IGNORE;
79376d13988SJohnny Huang 	int i;
794a8bd6d8cSJohnny Huang 	int fail = 0;
795a8bd6d8cSJohnny Huang 	uint32_t bit_offset;
796a8bd6d8cSJohnny Huang 	uint32_t dw_offset;
797a8bd6d8cSJohnny Huang 	uint32_t mask;
798a8bd6d8cSJohnny Huang 	uint32_t otp_value;
799696656c6SJohnny Huang 	uint32_t otp_reg_protect;
800a8bd6d8cSJohnny Huang 	uint32_t otp_protect;
801696656c6SJohnny Huang 	uint32_t otp_ignore;
80276d13988SJohnny Huang 
803696656c6SJohnny Huang 	OTPSTRAP = (uint32_t *)image_layout->strap;
804696656c6SJohnny Huang 	OTPSTRAP_PRO = (uint32_t *)image_layout->strap_pro;
805696656c6SJohnny Huang 	OTPSTRAP_IGNORE = (uint32_t *)image_layout->strap_ignore;
8065010032bSJohnny Huang 	if (info_cb.version == OTP_AST2600A0) {
807696656c6SJohnny Huang 		OTPSTRAP_REG_PRO = NULL;
808a8bd6d8cSJohnny Huang 		printf("BIT(hex)   Value       Protect     Description\n");
809696656c6SJohnny Huang 	} else {
810696656c6SJohnny Huang 		OTPSTRAP_REG_PRO = (uint32_t *)image_layout->strap_reg_pro;
811de6b0cc4SJohnny Huang 		printf("BIT(hex)   Value       Reg_Protect Protect     Description\n");
812696656c6SJohnny Huang 	}
813de6b0cc4SJohnny Huang 	printf("__________________________________________________________________________________________\n");
814b458cd62SJohnny Huang 
8153cb28812SJohnny Huang 	for (i = 0; i < info_cb.strap_info_len; i++) {
816696656c6SJohnny Huang 		if (strap_info[i].bit_offset > 31) {
817a8bd6d8cSJohnny Huang 			dw_offset = 1;
8183cb28812SJohnny Huang 			bit_offset = strap_info[i].bit_offset - 32;
819a8bd6d8cSJohnny Huang 		} else {
820a8bd6d8cSJohnny Huang 			dw_offset = 0;
8213cb28812SJohnny Huang 			bit_offset = strap_info[i].bit_offset;
822a8bd6d8cSJohnny Huang 		}
82376d13988SJohnny Huang 
8243cb28812SJohnny Huang 		mask = BIT(strap_info[i].length) - 1;
825a8bd6d8cSJohnny Huang 		otp_value = (OTPSTRAP[dw_offset] >> bit_offset) & mask;
826a8bd6d8cSJohnny Huang 		otp_protect = (OTPSTRAP_PRO[dw_offset] >> bit_offset) & mask;
827696656c6SJohnny Huang 		otp_ignore = (OTPSTRAP_IGNORE[dw_offset] >> bit_offset) & mask;
828a8bd6d8cSJohnny Huang 
8295010032bSJohnny Huang 		if (info_cb.version != OTP_AST2600A0)
830696656c6SJohnny Huang 			otp_reg_protect = (OTPSTRAP_REG_PRO[dw_offset] >> bit_offset) & mask;
8315010032bSJohnny Huang 		else
8325010032bSJohnny Huang 			otp_reg_protect = 0;
833696656c6SJohnny Huang 
834696656c6SJohnny Huang 		if (otp_ignore == mask) {
835a8bd6d8cSJohnny Huang 			continue;
836696656c6SJohnny Huang 		} else if (otp_ignore != 0) {
837a8bd6d8cSJohnny Huang 			fail = 1;
838a8bd6d8cSJohnny Huang 		}
839a8bd6d8cSJohnny Huang 
8403cb28812SJohnny Huang 		if ((otp_value != strap_info[i].value) &&
8413cb28812SJohnny Huang 		    strap_info[i].value != OTP_REG_RESERVED)
842a8bd6d8cSJohnny Huang 			continue;
843a8bd6d8cSJohnny Huang 
8443cb28812SJohnny Huang 		if (strap_info[i].length == 1) {
8453cb28812SJohnny Huang 			printf("0x%-9X", strap_info[i].bit_offset);
846a8bd6d8cSJohnny Huang 		} else {
847b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
8483cb28812SJohnny Huang 			       strap_info[i].bit_offset + strap_info[i].length - 1,
8493cb28812SJohnny Huang 			       strap_info[i].bit_offset);
850a8bd6d8cSJohnny Huang 		}
851a8bd6d8cSJohnny Huang 		printf("0x%-10x", otp_value);
8525010032bSJohnny Huang 		if (info_cb.version != OTP_AST2600A0)
853696656c6SJohnny Huang 			printf("0x%-10x", otp_reg_protect);
854a8bd6d8cSJohnny Huang 		printf("0x%-10x", otp_protect);
855a8bd6d8cSJohnny Huang 
856a8bd6d8cSJohnny Huang 		if (fail) {
857696656c6SJohnny Huang 			printf("Ignore mask error\n");
858a8bd6d8cSJohnny Huang 		} else {
8593cb28812SJohnny Huang 			if (strap_info[i].value != OTP_REG_RESERVED)
8603cb28812SJohnny Huang 				printf("%s\n", strap_info[i].information);
861a8bd6d8cSJohnny Huang 			else
862a8bd6d8cSJohnny Huang 				printf("Reserved\n");
863a8bd6d8cSJohnny Huang 		}
864a8bd6d8cSJohnny Huang 	}
865a8bd6d8cSJohnny Huang 
866a8bd6d8cSJohnny Huang 	if (fail)
86776d13988SJohnny Huang 		return OTP_FAILURE;
86876d13988SJohnny Huang 
86976d13988SJohnny Huang 	return OTP_SUCCESS;
87076d13988SJohnny Huang }
87176d13988SJohnny Huang 
872b458cd62SJohnny Huang static int otp_print_strap_info(int view)
87376d13988SJohnny Huang {
87479e42a59SJoel Stanley 	const struct otpstrap_info *strap_info = info_cb.strap_info;
87576d13988SJohnny Huang 	struct otpstrap_status strap_status[64];
87607baa4e8SJohnny Huang 	int i, j;
877b458cd62SJohnny Huang 	int fail = 0;
878b458cd62SJohnny Huang 	uint32_t bit_offset;
879b458cd62SJohnny Huang 	uint32_t length;
880b458cd62SJohnny Huang 	uint32_t otp_value;
881b458cd62SJohnny Huang 	uint32_t otp_protect;
88276d13988SJohnny Huang 
883541eb887SJohnny Huang 	otp_strap_status(strap_status);
88476d13988SJohnny Huang 
885b458cd62SJohnny Huang 	if (view) {
88683655e91SJohnny Huang 		if (info_cb.version == OTP_AST2600A0)
88707baa4e8SJohnny Huang 			printf("BIT(hex) Value  Remains  Protect   Description\n");
88883655e91SJohnny Huang 		else
88983655e91SJohnny Huang 			printf("BIT(hex) Value  Remains  Reg_Protect Protect   Description\n");
89007baa4e8SJohnny Huang 		printf("___________________________________________________________________________________________________\n");
891b458cd62SJohnny Huang 	} else {
892b458cd62SJohnny Huang 		printf("BIT(hex)   Value       Description\n");
893b458cd62SJohnny Huang 		printf("________________________________________________________________________________\n");
89476d13988SJohnny Huang 	}
8953cb28812SJohnny Huang 	for (i = 0; i < info_cb.strap_info_len; i++) {
896b458cd62SJohnny Huang 		otp_value = 0;
8973cb28812SJohnny Huang 		bit_offset = strap_info[i].bit_offset;
8983cb28812SJohnny Huang 		length = strap_info[i].length;
899b458cd62SJohnny Huang 		for (j = 0; j < length; j++) {
900c947ef08SJohnny Huang 			otp_value |= strap_status[bit_offset + j].value << j;
901c947ef08SJohnny Huang 			otp_protect |= strap_status[bit_offset + j].protected << j;
902b458cd62SJohnny Huang 		}
9033cb28812SJohnny Huang 		if ((otp_value != strap_info[i].value) &&
9043cb28812SJohnny Huang 		    strap_info[i].value != OTP_REG_RESERVED)
905b458cd62SJohnny Huang 			continue;
906b458cd62SJohnny Huang 		if (view) {
907b458cd62SJohnny Huang 			for (j = 0; j < length; j++) {
9083cb28812SJohnny Huang 				printf("0x%-7X", strap_info[i].bit_offset + j);
909b458cd62SJohnny Huang 				printf("0x%-5X", strap_status[bit_offset + j].value);
91007baa4e8SJohnny Huang 				printf("%-9d", strap_status[bit_offset + j].remain_times);
91183655e91SJohnny Huang 				if (info_cb.version != OTP_AST2600A0)
912e1a7245eSJohnny Huang 					printf("0x%-10X", strap_status[bit_offset + j].reg_protected);
913e1a7245eSJohnny Huang 				printf("0x%-7X", strap_status[bit_offset + j].protected);
9143cb28812SJohnny Huang 				if (strap_info[i].value == OTP_REG_RESERVED) {
915b458cd62SJohnny Huang 					printf(" Reserved\n");
916b458cd62SJohnny Huang 					continue;
917b458cd62SJohnny Huang 				}
918b458cd62SJohnny Huang 				if (length == 1) {
9193cb28812SJohnny Huang 					printf(" %s\n", strap_info[i].information);
920b458cd62SJohnny Huang 					continue;
92176d13988SJohnny Huang 				}
92276d13988SJohnny Huang 
923b458cd62SJohnny Huang 				if (j == 0)
9243cb28812SJohnny Huang 					printf("/%s\n", strap_info[i].information);
925b458cd62SJohnny Huang 				else if (j == length - 1)
926b458cd62SJohnny Huang 					printf("\\ \"\n");
927b458cd62SJohnny Huang 				else
928b458cd62SJohnny Huang 					printf("| \"\n");
92976d13988SJohnny Huang 			}
930b458cd62SJohnny Huang 		} else {
931c947ef08SJohnny Huang 			if (length == 1) {
9323cb28812SJohnny Huang 				printf("0x%-9X", strap_info[i].bit_offset);
933b458cd62SJohnny Huang 			} else {
934b458cd62SJohnny Huang 				printf("0x%-2X:0x%-4X",
935b458cd62SJohnny Huang 				       bit_offset + length - 1, bit_offset);
936b458cd62SJohnny Huang 			}
937b458cd62SJohnny Huang 
938b458cd62SJohnny Huang 			printf("0x%-10X", otp_value);
939b458cd62SJohnny Huang 
9403cb28812SJohnny Huang 			if (strap_info[i].value != OTP_REG_RESERVED)
9413cb28812SJohnny Huang 				printf("%s\n", strap_info[i].information);
942b458cd62SJohnny Huang 			else
943b458cd62SJohnny Huang 				printf("Reserved\n");
944b458cd62SJohnny Huang 		}
945b458cd62SJohnny Huang 	}
946b458cd62SJohnny Huang 
947b458cd62SJohnny Huang 	if (fail)
948b458cd62SJohnny Huang 		return OTP_FAILURE;
949b458cd62SJohnny Huang 
950b458cd62SJohnny Huang 	return OTP_SUCCESS;
951b458cd62SJohnny Huang }
952b458cd62SJohnny Huang 
953696656c6SJohnny Huang static void buf_print(uint8_t *buf, int len)
95469d5fd8fSJohnny Huang {
95569d5fd8fSJohnny Huang 	int i;
95669d5fd8fSJohnny Huang 	printf("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
95769d5fd8fSJohnny Huang 	for (i = 0; i < len; i++) {
95869d5fd8fSJohnny Huang 		if (i % 16 == 0) {
95969d5fd8fSJohnny Huang 			printf("%04X: ", i);
96069d5fd8fSJohnny Huang 		}
96169d5fd8fSJohnny Huang 		printf("%02X ", buf[i]);
96269d5fd8fSJohnny Huang 		if ((i + 1) % 16 == 0) {
96369d5fd8fSJohnny Huang 			printf("\n");
96469d5fd8fSJohnny Huang 		}
96569d5fd8fSJohnny Huang 	}
96669d5fd8fSJohnny Huang }
96769d5fd8fSJohnny Huang 
968696656c6SJohnny Huang static int otp_print_data_info(struct otp_image_layout *image_layout)
96969d5fd8fSJohnny Huang {
97069d5fd8fSJohnny Huang 	int key_id, key_offset, last, key_type, key_length, exp_length;
97179e42a59SJoel Stanley 	const struct otpkey_type *key_info_array = info_cb.key_info;
9729a4fe690SJohnny Huang 	struct otpkey_type key_info;
973696656c6SJohnny Huang 	uint32_t *buf;
974696656c6SJohnny Huang 	uint8_t *byte_buf;
9759d998018SJohnny Huang 	char empty = 1;
97669d5fd8fSJohnny Huang 	int i = 0, len = 0;
9779a4fe690SJohnny Huang 	int j;
97854552c69SJohnny Huang 
979696656c6SJohnny Huang 	byte_buf = image_layout->data;
980696656c6SJohnny Huang 	buf = (uint32_t *)byte_buf;
9819d998018SJohnny Huang 
9829d998018SJohnny Huang 	for (i = 0; i < 16; i++) {
9839d998018SJohnny Huang 		if (buf[i] != 0) {
9849d998018SJohnny Huang 			empty = 0;
9859d998018SJohnny Huang 		}
9869d998018SJohnny Huang 	}
9879d998018SJohnny Huang 	if (empty)
9889d998018SJohnny Huang 		return 0;
9899d998018SJohnny Huang 
9909d998018SJohnny Huang 	i = 0;
99169d5fd8fSJohnny Huang 	while (1) {
99269d5fd8fSJohnny Huang 		key_id = buf[i] & 0x7;
99369d5fd8fSJohnny Huang 		key_offset = buf[i] & 0x1ff8;
99469d5fd8fSJohnny Huang 		last = (buf[i] >> 13) & 1;
99569d5fd8fSJohnny Huang 		key_type = (buf[i] >> 14) & 0xf;
99669d5fd8fSJohnny Huang 		key_length = (buf[i] >> 18) & 0x3;
99769d5fd8fSJohnny Huang 		exp_length = (buf[i] >> 20) & 0xfff;
9989a4fe690SJohnny Huang 
9999a4fe690SJohnny Huang 		for (j = 0; j < info_cb.key_info_len; j++) {
10009a4fe690SJohnny Huang 			if (key_type == key_info_array[j].value) {
10019a4fe690SJohnny Huang 				key_info = key_info_array[j];
10029a4fe690SJohnny Huang 				break;
10039a4fe690SJohnny Huang 			}
10049a4fe690SJohnny Huang 		}
10059a4fe690SJohnny Huang 
10067f795e57SJohnny Huang 		printf("\nKey[%d]:\n", i);
100769d5fd8fSJohnny Huang 		printf("Key Type: ");
10089a4fe690SJohnny Huang 		printf("%s\n", key_info.information);
10099a4fe690SJohnny Huang 
10109a4fe690SJohnny Huang 		if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
101169d5fd8fSJohnny Huang 			printf("HMAC SHA Type: ");
101269d5fd8fSJohnny Huang 			switch (key_length) {
101369d5fd8fSJohnny Huang 			case 0:
101469d5fd8fSJohnny Huang 				printf("HMAC(SHA224)\n");
101569d5fd8fSJohnny Huang 				break;
101669d5fd8fSJohnny Huang 			case 1:
101769d5fd8fSJohnny Huang 				printf("HMAC(SHA256)\n");
101869d5fd8fSJohnny Huang 				break;
101969d5fd8fSJohnny Huang 			case 2:
102069d5fd8fSJohnny Huang 				printf("HMAC(SHA384)\n");
102169d5fd8fSJohnny Huang 				break;
102269d5fd8fSJohnny Huang 			case 3:
102369d5fd8fSJohnny Huang 				printf("HMAC(SHA512)\n");
102469d5fd8fSJohnny Huang 				break;
102569d5fd8fSJohnny Huang 			}
10269a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA) {
102769d5fd8fSJohnny Huang 			printf("RSA SHA Type: ");
102869d5fd8fSJohnny Huang 			switch (key_length) {
102969d5fd8fSJohnny Huang 			case 0:
103069d5fd8fSJohnny Huang 				printf("RSA1024\n");
103169d5fd8fSJohnny Huang 				len = 0x100;
103269d5fd8fSJohnny Huang 				break;
103369d5fd8fSJohnny Huang 			case 1:
103469d5fd8fSJohnny Huang 				printf("RSA2048\n");
103569d5fd8fSJohnny Huang 				len = 0x200;
103669d5fd8fSJohnny Huang 				break;
103769d5fd8fSJohnny Huang 			case 2:
103869d5fd8fSJohnny Huang 				printf("RSA3072\n");
103969d5fd8fSJohnny Huang 				len = 0x300;
104069d5fd8fSJohnny Huang 				break;
104169d5fd8fSJohnny Huang 			case 3:
104269d5fd8fSJohnny Huang 				printf("RSA4096\n");
104369d5fd8fSJohnny Huang 				len = 0x400;
104469d5fd8fSJohnny Huang 				break;
104569d5fd8fSJohnny Huang 			}
104669d5fd8fSJohnny Huang 			printf("RSA exponent bit length: %d\n", exp_length);
104769d5fd8fSJohnny Huang 		}
10489a4fe690SJohnny Huang 		if (key_info.need_id)
104969d5fd8fSJohnny Huang 			printf("Key Number ID: %d\n", key_id);
105069d5fd8fSJohnny Huang 		printf("Key Value:\n");
10519a4fe690SJohnny Huang 		if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
105269d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], 0x40);
10539a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_AES) {
10549a4fe690SJohnny Huang 			printf("AES Key:\n");
10559a4fe690SJohnny Huang 			buf_print(&byte_buf[key_offset], 0x20);
10565fdde29fSJohnny Huang 			if (info_cb.version == OTP_AST2600A0) {
10579a4fe690SJohnny Huang 				printf("AES IV:\n");
10589a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x10);
10599a4fe690SJohnny Huang 			}
10609a4fe690SJohnny Huang 
10619a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_VAULT) {
10625fdde29fSJohnny Huang 			if (info_cb.version == OTP_AST2600A0) {
106369d5fd8fSJohnny Huang 				printf("AES Key:\n");
106469d5fd8fSJohnny Huang 				buf_print(&byte_buf[key_offset], 0x20);
106569d5fd8fSJohnny Huang 				printf("AES IV:\n");
106669d5fd8fSJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x10);
10675fdde29fSJohnny Huang 			} else {
10689a4fe690SJohnny Huang 				printf("AES Key 1:\n");
10699a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset], 0x20);
10709a4fe690SJohnny Huang 				printf("AES Key 2:\n");
10719a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x20);
10729a4fe690SJohnny Huang 			}
107369d5fd8fSJohnny Huang 
10749a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA) {
107569d5fd8fSJohnny Huang 			printf("RSA mod:\n");
107669d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], len / 2);
107769d5fd8fSJohnny Huang 			printf("RSA exp:\n");
107869d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset + (len / 2)], len / 2);
107969d5fd8fSJohnny Huang 		}
108069d5fd8fSJohnny Huang 		if (last)
108169d5fd8fSJohnny Huang 			break;
108269d5fd8fSJohnny Huang 		i++;
108369d5fd8fSJohnny Huang 	}
108469d5fd8fSJohnny Huang 	return 0;
108569d5fd8fSJohnny Huang }
108669d5fd8fSJohnny Huang 
10875010032bSJohnny Huang static int otp_prog_conf(struct otp_image_layout *image_layout)
108869d5fd8fSJohnny Huang {
1089a6d0d645SJohnny Huang 	int i, k;
1090d90825e2SJohnny Huang 	int pass = 0;
1091a6d0d645SJohnny Huang 	uint32_t prog_address;
1092bb34a7bfSJohnny Huang 	uint32_t data[16];
1093a6d0d645SJohnny Huang 	uint32_t compare[2];
10945010032bSJohnny Huang 	uint32_t *conf = (uint32_t *)image_layout->conf;
10955010032bSJohnny Huang 	uint32_t *conf_ignore = (uint32_t *)image_layout->conf_ignore;
1096d90825e2SJohnny Huang 	uint32_t data_masked;
1097d90825e2SJohnny Huang 	uint32_t buf_masked;
109869d5fd8fSJohnny Huang 
1099a6d0d645SJohnny Huang 	printf("Read OTP Config Region:\n");
1100a6d0d645SJohnny Huang 
1101bb34a7bfSJohnny Huang 	for (i = 0; i < 16 ; i ++) {
110269d5fd8fSJohnny Huang 		prog_address = 0x800;
1103a6d0d645SJohnny Huang 		prog_address |= (i / 8) * 0x200;
1104a6d0d645SJohnny Huang 		prog_address |= (i % 8) * 0x2;
1105a6d0d645SJohnny Huang 		otp_read_data(prog_address, &data[i]);
1106a6d0d645SJohnny Huang 	}
1107a6d0d645SJohnny Huang 
1108a6d0d645SJohnny Huang 	printf("Check writable...\n");
1109bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++) {
11105010032bSJohnny Huang 		data_masked = data[i]  & ~conf_ignore[i];
11115010032bSJohnny Huang 		buf_masked  = conf[i] & ~conf_ignore[i];
1112d90825e2SJohnny Huang 		if (data_masked == buf_masked)
111369d5fd8fSJohnny Huang 			continue;
1114d90825e2SJohnny Huang 		if ((data_masked | buf_masked) == buf_masked) {
1115a6d0d645SJohnny Huang 			continue;
1116a6d0d645SJohnny Huang 		} else {
1117a6d0d645SJohnny Huang 			printf("Input image can't program into OTP, please check.\n");
1118a6af4a17SJohnny Huang 			printf("OTPCFG[%X] = %x\n", i, data[i]);
11195010032bSJohnny Huang 			printf("Input [%X] = %x\n", i, conf[i]);
11205010032bSJohnny Huang 			printf("Mask  [%X] = %x\n", i, ~conf_ignore[i]);
11212a856b9aSJohnny Huang 			return OTP_FAILURE;
1122a6d0d645SJohnny Huang 		}
1123a6d0d645SJohnny Huang 	}
1124a6d0d645SJohnny Huang 
1125a6d0d645SJohnny Huang 	printf("Start Programing...\n");
1126d90825e2SJohnny Huang 	otp_soak(0);
1127bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++) {
11285010032bSJohnny Huang 		data_masked = data[i]  & ~conf_ignore[i];
11295010032bSJohnny Huang 		buf_masked  = conf[i] & ~conf_ignore[i];
1130a6d0d645SJohnny Huang 		prog_address = 0x800;
1131a6d0d645SJohnny Huang 		prog_address |= (i / 8) * 0x200;
1132a6d0d645SJohnny Huang 		prog_address |= (i % 8) * 0x2;
1133bb34a7bfSJohnny Huang 		if (data_masked == buf_masked) {
1134bb34a7bfSJohnny Huang 			pass = 1;
1135a6d0d645SJohnny Huang 			continue;
1136bb34a7bfSJohnny Huang 		}
1137de6fbf1cSJohnny Huang 
1138a6d0d645SJohnny Huang 
1139de6fbf1cSJohnny Huang 		otp_soak(1);
11405010032bSJohnny Huang 		otp_prog_dw(conf[i], conf_ignore[i], prog_address);
1141a6d0d645SJohnny Huang 
114269d5fd8fSJohnny Huang 		pass = 0;
114369d5fd8fSJohnny Huang 		for (k = 0; k < RETRY; k++) {
11445010032bSJohnny Huang 			if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) {
1145de6fbf1cSJohnny Huang 				otp_soak(2);
1146feea3fdfSJohnny Huang 				otp_prog_dw(compare[0], conf_ignore[i], prog_address);
11475010032bSJohnny Huang 				if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) {
1148de6fbf1cSJohnny Huang 					otp_soak(1);
1149de6fbf1cSJohnny Huang 				} else {
1150de6fbf1cSJohnny Huang 					pass = 1;
1151de6fbf1cSJohnny Huang 					break;
1152de6fbf1cSJohnny Huang 				}
1153a6d0d645SJohnny Huang 			} else {
115469d5fd8fSJohnny Huang 				pass = 1;
115569d5fd8fSJohnny Huang 				break;
115669d5fd8fSJohnny Huang 			}
115769d5fd8fSJohnny Huang 		}
1158bb34a7bfSJohnny Huang 		if (pass == 0) {
1159bb34a7bfSJohnny Huang 			printf("address: %08x, data: %08x, buffer: %08x, mask: %08x\n",
11605010032bSJohnny Huang 			       i, data[i], conf[i], conf_ignore[i]);
1161bb34a7bfSJohnny Huang 			break;
1162bb34a7bfSJohnny Huang 		}
1163a6d0d645SJohnny Huang 	}
1164a6d0d645SJohnny Huang 
1165de6fbf1cSJohnny Huang 	otp_soak(0);
116669d5fd8fSJohnny Huang 	if (!pass)
11672a856b9aSJohnny Huang 		return OTP_FAILURE;
1168a6d0d645SJohnny Huang 
11692a856b9aSJohnny Huang 	return OTP_SUCCESS;
1170d90825e2SJohnny Huang 
117169d5fd8fSJohnny Huang }
117269d5fd8fSJohnny Huang 
1173eda10d61SJohnny Huang static int otp_strap_bit_confirm(struct otpstrap_status *otpstrap, int offset, int ibit, int bit, int pbit, int rpbit)
1174eda10d61SJohnny Huang {
1175eda10d61SJohnny Huang 	if (ibit == 1) {
1176eda10d61SJohnny Huang 		return OTP_SUCCESS;
1177eda10d61SJohnny Huang 	} else {
1178eda10d61SJohnny Huang 		printf("OTPSTRAP[%X]:\n", offset);
1179eda10d61SJohnny Huang 	}
1180eda10d61SJohnny Huang 	if (bit == otpstrap->value) {
1181eda10d61SJohnny Huang 		printf("    The value is same as before, skip it.\n");
1182eda10d61SJohnny Huang 		return OTP_PROG_SKIP;
1183eda10d61SJohnny Huang 	}
1184eda10d61SJohnny Huang 	if (otpstrap->protected == 1) {
1185eda10d61SJohnny Huang 		printf("    This bit is protected and is not writable\n");
1186eda10d61SJohnny Huang 		return OTP_FAILURE;
1187eda10d61SJohnny Huang 	}
1188eda10d61SJohnny Huang 	if (otpstrap->remain_times == 0) {
1189eda10d61SJohnny Huang 		printf("    This bit is no remaining times to write.\n");
1190eda10d61SJohnny Huang 		return OTP_FAILURE;
1191eda10d61SJohnny Huang 	}
1192eda10d61SJohnny Huang 	if (pbit == 1) {
1193eda10d61SJohnny Huang 		printf("    This bit will be protected and become non-writable.\n");
1194eda10d61SJohnny Huang 	}
1195eda10d61SJohnny Huang 	if (rpbit == 1 && info_cb.version != OTP_AST2600A0) {
1196eda10d61SJohnny Huang 		printf("    The relative register will be protected.\n");
1197eda10d61SJohnny Huang 	}
1198eda10d61SJohnny 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);
1199eda10d61SJohnny Huang 	return OTP_SUCCESS;
1200eda10d61SJohnny Huang }
1201eda10d61SJohnny Huang 
12025010032bSJohnny Huang static int otp_strap_image_confirm(struct otp_image_layout *image_layout)
120369d5fd8fSJohnny Huang {
120469d5fd8fSJohnny Huang 	int i;
12055010032bSJohnny Huang 	uint32_t *strap;
12065010032bSJohnny Huang 	uint32_t *strap_ignore;
12075010032bSJohnny Huang 	uint32_t *strap_reg_protect;
12085010032bSJohnny Huang 	uint32_t *strap_pro;
1209eda10d61SJohnny Huang 	int bit, pbit, ibit, rpbit;
121069d5fd8fSJohnny Huang 	int fail = 0;
1211a6af4a17SJohnny Huang 	int skip = -1;
1212eda10d61SJohnny Huang 	int ret;
121366f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
121469d5fd8fSJohnny Huang 
12155010032bSJohnny Huang 	strap = (uint32_t *)image_layout->strap;
12165010032bSJohnny Huang 	strap_pro = (uint32_t *)image_layout->strap_pro;
12175010032bSJohnny Huang 	strap_ignore = (uint32_t *)image_layout->strap_ignore;
12185010032bSJohnny Huang 	strap_reg_protect = (uint32_t *)image_layout->strap_reg_pro;
12195010032bSJohnny Huang 
1220541eb887SJohnny Huang 	otp_strap_status(otpstrap);
122169d5fd8fSJohnny Huang 	for (i = 0; i < 64; i++) {
122269d5fd8fSJohnny Huang 		if (i < 32) {
12235010032bSJohnny Huang 			bit = (strap[0] >> i) & 0x1;
1224eda10d61SJohnny Huang 			ibit = (strap_ignore[0] >> i) & 0x1;
12255010032bSJohnny Huang 			pbit = (strap_pro[0] >> i) & 0x1;
122669d5fd8fSJohnny Huang 		} else {
12275010032bSJohnny Huang 			bit = (strap[1] >> (i - 32)) & 0x1;
1228eda10d61SJohnny Huang 			ibit = (strap_ignore[1] >> (i - 32)) & 0x1;
12295010032bSJohnny Huang 			pbit = (strap_pro[1] >> (i - 32)) & 0x1;
12305010032bSJohnny Huang 		}
12315010032bSJohnny Huang 
12325010032bSJohnny Huang 		if (info_cb.version != OTP_AST2600A0) {
12335010032bSJohnny Huang 			if (i < 32) {
12345010032bSJohnny Huang 				rpbit = (strap_reg_protect[0] >> i) & 0x1;
12355010032bSJohnny Huang 			} else {
12365010032bSJohnny Huang 				rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1;
12375010032bSJohnny Huang 			}
12385010032bSJohnny Huang 		} else {
12395010032bSJohnny Huang 			rpbit = 0;
124069d5fd8fSJohnny Huang 		}
1241eda10d61SJohnny Huang 		ret = otp_strap_bit_confirm(&otpstrap[i], i, ibit, bit, pbit, rpbit);
1242eda10d61SJohnny Huang 		if (ret == OTP_PROG_SKIP) {
1243a6af4a17SJohnny Huang 			if (skip == -1)
1244a6af4a17SJohnny Huang 				skip = 1;
124569d5fd8fSJohnny Huang 			continue;
1246a6af4a17SJohnny Huang 		} else {
1247eda10d61SJohnny Huang 			skip = 1;
124869d5fd8fSJohnny Huang 		}
1249eda10d61SJohnny Huang 
1250eda10d61SJohnny Huang 		if (ret == OTP_FAILURE)
125169d5fd8fSJohnny Huang 			fail = 1;
125269d5fd8fSJohnny Huang 	}
125369d5fd8fSJohnny Huang 	if (fail == 1)
1254a6af4a17SJohnny Huang 		return OTP_FAILURE;
1255a6af4a17SJohnny Huang 	else if (skip == 1)
1256a6af4a17SJohnny Huang 		return OTP_PROG_SKIP;
12577e22f42dSJohnny Huang 
1258eda10d61SJohnny Huang 	return OTP_SUCCESS;
125969d5fd8fSJohnny Huang }
126069d5fd8fSJohnny Huang 
12612a856b9aSJohnny Huang static int otp_print_strap(int start, int count)
126269d5fd8fSJohnny Huang {
126369d5fd8fSJohnny Huang 	int i, j;
1264de6b0cc4SJohnny Huang 	int remains;
126566f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
126669d5fd8fSJohnny Huang 
12672a856b9aSJohnny Huang 	if (start < 0 || start > 64)
12682a856b9aSJohnny Huang 		return OTP_USAGE;
12692a856b9aSJohnny Huang 
12702a856b9aSJohnny Huang 	if ((start + count) < 0 || (start + count) > 64)
12712a856b9aSJohnny Huang 		return OTP_USAGE;
12722a856b9aSJohnny Huang 
1273541eb887SJohnny Huang 	otp_strap_status(otpstrap);
127469d5fd8fSJohnny Huang 
1275de6b0cc4SJohnny Huang 	if (info_cb.version == OTP_AST2600A0) {
1276de6b0cc4SJohnny Huang 		remains = 7;
127707baa4e8SJohnny Huang 		printf("BIT(hex)  Value  Option           Status\n");
1278de6b0cc4SJohnny Huang 	} else {
1279de6b0cc4SJohnny Huang 		remains = 6;
1280de6b0cc4SJohnny Huang 		printf("BIT(hex)  Value  Option         Reg_Protect Status\n");
1281de6b0cc4SJohnny Huang 	}
1282de6b0cc4SJohnny Huang 	printf("______________________________________________________________________________\n");
1283737ed20bSJohnny Huang 
1284cd1610b4SJohnny Huang 	for (i = start; i < start + count; i++) {
128507baa4e8SJohnny Huang 		printf("0x%-8X", i);
1286737ed20bSJohnny Huang 		printf("%-7d", otpstrap[i].value);
1287de6b0cc4SJohnny Huang 		for (j = 0; j < remains; j++)
1288737ed20bSJohnny Huang 			printf("%d ", otpstrap[i].option_array[j]);
1289737ed20bSJohnny Huang 		printf("   ");
1290de6b0cc4SJohnny Huang 		if (info_cb.version != OTP_AST2600A0) {
1291de6b0cc4SJohnny Huang 			printf("%d           ", otpstrap[i].reg_protected);
1292de6b0cc4SJohnny Huang 		}
129369d5fd8fSJohnny Huang 		if (otpstrap[i].protected == 1) {
1294737ed20bSJohnny Huang 			printf("protected and not writable");
129569d5fd8fSJohnny Huang 		} else {
1296737ed20bSJohnny Huang 			printf("not protected ");
129769d5fd8fSJohnny Huang 			if (otpstrap[i].remain_times == 0) {
1298737ed20bSJohnny Huang 				printf("and no remaining times to write.");
129969d5fd8fSJohnny Huang 			} else {
1300737ed20bSJohnny Huang 				printf("and still can write %d times", otpstrap[i].remain_times);
130169d5fd8fSJohnny Huang 			}
130269d5fd8fSJohnny Huang 		}
1303737ed20bSJohnny Huang 		printf("\n");
130469d5fd8fSJohnny Huang 	}
13052a856b9aSJohnny Huang 
13062a856b9aSJohnny Huang 	return OTP_SUCCESS;
130769d5fd8fSJohnny Huang }
130869d5fd8fSJohnny Huang 
13098848d5dcSJohnny Huang static int otp_prog_strap_bit(int bit_offset, int value)
13108848d5dcSJohnny Huang {
13118848d5dcSJohnny Huang 	struct otpstrap_status otpstrap[64];
131283655e91SJohnny Huang 	uint32_t prog_address;
13138848d5dcSJohnny Huang 	int offset;
13148848d5dcSJohnny Huang 	int ret;
13158848d5dcSJohnny Huang 
13168848d5dcSJohnny Huang 
13178848d5dcSJohnny Huang 	otp_strap_status(otpstrap);
13188848d5dcSJohnny Huang 
13198848d5dcSJohnny Huang 	ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0, 0);
13208848d5dcSJohnny Huang 
13218848d5dcSJohnny Huang 	if (ret != OTP_SUCCESS) {
13228848d5dcSJohnny Huang 		return ret;
13238848d5dcSJohnny Huang 	}
13248848d5dcSJohnny Huang 
13258848d5dcSJohnny Huang 	prog_address = 0x800;
13268848d5dcSJohnny Huang 	if (bit_offset < 32) {
13278848d5dcSJohnny Huang 		offset = bit_offset;
13288848d5dcSJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) / 8) * 0x200;
13298848d5dcSJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) % 8) * 0x2;
13308848d5dcSJohnny Huang 
13318848d5dcSJohnny Huang 	} else {
13328848d5dcSJohnny Huang 		offset = (bit_offset - 32);
13338848d5dcSJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) / 8) * 0x200;
13348848d5dcSJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) % 8) * 0x2;
13358848d5dcSJohnny Huang 	}
13368848d5dcSJohnny Huang 
13378848d5dcSJohnny Huang 
133883655e91SJohnny Huang 	return otp_prog_bit(1, prog_address, offset);
13398848d5dcSJohnny Huang }
13408848d5dcSJohnny Huang 
13415010032bSJohnny Huang static int otp_prog_strap(struct otp_image_layout *image_layout)
134269d5fd8fSJohnny Huang {
13435010032bSJohnny Huang 	uint32_t *strap;
13445010032bSJohnny Huang 	uint32_t *strap_ignore;
13455010032bSJohnny Huang 	uint32_t *strap_pro;
13465010032bSJohnny Huang 	uint32_t *strap_reg_protect;
134783655e91SJohnny Huang 	uint32_t prog_address;
134883655e91SJohnny Huang 	int i;
1349eda10d61SJohnny Huang 	int bit, pbit, ibit, offset, rpbit;
135069d5fd8fSJohnny Huang 	int fail = 0;
135183655e91SJohnny Huang 	int ret;
135266f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
135369d5fd8fSJohnny Huang 
13545010032bSJohnny Huang 	strap = (uint32_t *)image_layout->strap;
13555010032bSJohnny Huang 	strap_pro = (uint32_t *)image_layout->strap_pro;
13565010032bSJohnny Huang 	strap_ignore = (uint32_t *)image_layout->strap_ignore;
13575010032bSJohnny Huang 	strap_reg_protect = (uint32_t *)image_layout->strap_reg_pro;
13585010032bSJohnny Huang 
13597f795e57SJohnny Huang 	printf("Read OTP Strap Region:\n");
1360541eb887SJohnny Huang 	otp_strap_status(otpstrap);
136169d5fd8fSJohnny Huang 
13627f795e57SJohnny Huang 	printf("Check writable...\n");
13635010032bSJohnny Huang 	if (otp_strap_image_confirm(image_layout) == OTP_FAILURE) {
13647f795e57SJohnny Huang 		printf("Input image can't program into OTP, please check.\n");
13657f795e57SJohnny Huang 		return OTP_FAILURE;
13667f795e57SJohnny Huang 	}
13677e22f42dSJohnny Huang 
136869d5fd8fSJohnny Huang 	for (i = 0; i < 64; i++) {
136969d5fd8fSJohnny Huang 		prog_address = 0x800;
137069d5fd8fSJohnny Huang 		if (i < 32) {
137169d5fd8fSJohnny Huang 			offset = i;
13725010032bSJohnny Huang 			bit = (strap[0] >> offset) & 0x1;
1373eda10d61SJohnny Huang 			ibit = (strap_ignore[0] >> offset) & 0x1;
13745010032bSJohnny Huang 			pbit = (strap_pro[0] >> offset) & 0x1;
137569d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200;
137669d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2;
137769d5fd8fSJohnny Huang 
137869d5fd8fSJohnny Huang 		} else {
137969d5fd8fSJohnny Huang 			offset = (i - 32);
13805010032bSJohnny Huang 			bit = (strap[1] >> offset) & 0x1;
1381eda10d61SJohnny Huang 			ibit = (strap_ignore[1] >> offset) & 0x1;
13825010032bSJohnny Huang 			pbit = (strap_pro[1] >> offset) & 0x1;
138369d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200;
138469d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2;
138569d5fd8fSJohnny Huang 		}
13865010032bSJohnny Huang 		if (info_cb.version != OTP_AST2600A0) {
13875010032bSJohnny Huang 			if (i < 32) {
13885010032bSJohnny Huang 				rpbit = (strap_reg_protect[0] >> i) & 0x1;
13895010032bSJohnny Huang 			} else {
13905010032bSJohnny Huang 				rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1;
13915010032bSJohnny Huang 			}
13925010032bSJohnny Huang 		} else {
13935010032bSJohnny Huang 			rpbit = 0;
13945010032bSJohnny Huang 		}
139569d5fd8fSJohnny Huang 
1396eda10d61SJohnny Huang 		if (ibit == 1) {
139769d5fd8fSJohnny Huang 			continue;
139869d5fd8fSJohnny Huang 		}
139969d5fd8fSJohnny Huang 		if (bit == otpstrap[i].value) {
140069d5fd8fSJohnny Huang 			continue;
140169d5fd8fSJohnny Huang 		}
140269d5fd8fSJohnny Huang 		if (otpstrap[i].protected == 1) {
140369d5fd8fSJohnny Huang 			fail = 1;
140469d5fd8fSJohnny Huang 			continue;
140569d5fd8fSJohnny Huang 		}
140669d5fd8fSJohnny Huang 		if (otpstrap[i].remain_times == 0) {
140769d5fd8fSJohnny Huang 			fail = 1;
140869d5fd8fSJohnny Huang 			continue;
140969d5fd8fSJohnny Huang 		}
14107e22f42dSJohnny Huang 
141183655e91SJohnny Huang 		ret = otp_prog_bit(1, prog_address, offset);
141283655e91SJohnny Huang 		if (!ret)
14132a856b9aSJohnny Huang 			return OTP_FAILURE;
141469d5fd8fSJohnny Huang 
14155010032bSJohnny Huang 		if (rpbit == 1 && info_cb.version != OTP_AST2600A0) {
141669d5fd8fSJohnny Huang 			prog_address = 0x800;
141769d5fd8fSJohnny Huang 			if (i < 32)
14185010032bSJohnny Huang 				prog_address |= 0x608;
141969d5fd8fSJohnny Huang 			else
14205010032bSJohnny Huang 				prog_address |= 0x60a;
14217e22f42dSJohnny Huang 
142283655e91SJohnny Huang 			ret = otp_prog_bit(1, prog_address, offset);
142383655e91SJohnny Huang 			if (!ret)
14242a856b9aSJohnny Huang 				return OTP_FAILURE;
14255010032bSJohnny Huang 		}
14265010032bSJohnny Huang 
14275010032bSJohnny Huang 		if (pbit != 0) {
14285010032bSJohnny Huang 			prog_address = 0x800;
14295010032bSJohnny Huang 			if (i < 32)
14305010032bSJohnny Huang 				prog_address |= 0x60c;
14315010032bSJohnny Huang 			else
14325010032bSJohnny Huang 				prog_address |= 0x60e;
14335010032bSJohnny Huang 
143483655e91SJohnny Huang 			ret = otp_prog_bit(1, prog_address, offset);
143583655e91SJohnny Huang 			if (!ret)
14365010032bSJohnny Huang 				return OTP_FAILURE;
14375010032bSJohnny Huang 		}
143869d5fd8fSJohnny Huang 
143969d5fd8fSJohnny Huang 	}
1440de6fbf1cSJohnny Huang 	otp_soak(0);
144169d5fd8fSJohnny Huang 	if (fail == 1)
14422a856b9aSJohnny Huang 		return OTP_FAILURE;
144369d5fd8fSJohnny Huang 	else
14442a856b9aSJohnny Huang 		return OTP_SUCCESS;
144569d5fd8fSJohnny Huang 
144669d5fd8fSJohnny Huang }
144769d5fd8fSJohnny Huang 
14485010032bSJohnny Huang static int otp_prog_data(struct otp_image_layout *image_layout)
14494c1c9b35SJohnny Huang {
145054552c69SJohnny Huang 	int i;
145154552c69SJohnny Huang 	int ret;
14525010032bSJohnny Huang 	int data_dw;
1453d90825e2SJohnny Huang 	uint32_t data[2048];
14545010032bSJohnny Huang 	uint32_t *buf;
14555010032bSJohnny Huang 	uint32_t *buf_ignore;
14564c1c9b35SJohnny Huang 
145754552c69SJohnny Huang 	uint32_t data_masked;
145854552c69SJohnny Huang 	uint32_t buf_masked;
14594c1c9b35SJohnny Huang 
14605010032bSJohnny Huang 	buf = (uint32_t *)image_layout->data;
14615010032bSJohnny Huang 	buf_ignore = (uint32_t *)image_layout->data_ignore;
14625010032bSJohnny Huang 
14635010032bSJohnny Huang 	data_dw = image_layout->data_length / 4;
14645010032bSJohnny Huang 
14654c1c9b35SJohnny Huang 	printf("Read OTP Data:\n");
14664c1c9b35SJohnny Huang 
14675010032bSJohnny Huang 	for (i = 0; i < data_dw - 2 ; i += 2) {
1468d90825e2SJohnny Huang 		otp_read_data(i, &data[i]);
14694c1c9b35SJohnny Huang 	}
1470d90825e2SJohnny Huang 
14714c1c9b35SJohnny Huang 	printf("Check writable...\n");
147254552c69SJohnny Huang 	// ignore last two dw, the last two dw is used for slt otp write check.
14735010032bSJohnny Huang 	for (i = 0; i < data_dw - 2; i++) {
1474696656c6SJohnny Huang 		data_masked = data[i]  & ~buf_ignore[i];
1475696656c6SJohnny Huang 		buf_masked  = buf[i] & ~buf_ignore[i];
147654552c69SJohnny Huang 		if (data_masked == buf_masked)
14774c1c9b35SJohnny Huang 			continue;
1478d90825e2SJohnny Huang 		if (i % 2 == 0) {
147954552c69SJohnny Huang 			if ((data_masked | buf_masked) == buf_masked) {
14804c1c9b35SJohnny Huang 				continue;
14814c1c9b35SJohnny Huang 			} else {
14824c1c9b35SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
1483d90825e2SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
14844c1c9b35SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
1485696656c6SJohnny Huang 				printf("Mask    [%x] = %x\n", i, ~buf_ignore[i]);
14862a856b9aSJohnny Huang 				return OTP_FAILURE;
148769d5fd8fSJohnny Huang 			}
1488d90825e2SJohnny Huang 		} else {
148954552c69SJohnny Huang 			if ((data_masked & buf_masked) == buf_masked) {
1490d90825e2SJohnny Huang 				continue;
1491d90825e2SJohnny Huang 			} else {
1492d90825e2SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
1493d90825e2SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
1494d90825e2SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
1495696656c6SJohnny Huang 				printf("Mask    [%x] = %x\n", i, ~buf_ignore[i]);
14962a856b9aSJohnny Huang 				return OTP_FAILURE;
1497d90825e2SJohnny Huang 			}
1498d90825e2SJohnny Huang 		}
1499d90825e2SJohnny Huang 	}
150069d5fd8fSJohnny Huang 
1501d90825e2SJohnny Huang 	printf("Start Programing...\n");
1502d90825e2SJohnny Huang 
150354552c69SJohnny Huang 	// programing ecc region first
150454552c69SJohnny Huang 	for (i = 1792; i < 2046; i += 2) {
1505696656c6SJohnny Huang 		ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i);
150654552c69SJohnny Huang 		if (ret != OTP_SUCCESS) {
150754552c69SJohnny Huang 			printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n",
1508696656c6SJohnny Huang 			       i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]);
150954552c69SJohnny Huang 			return ret;
1510d90825e2SJohnny Huang 		}
1511d90825e2SJohnny Huang 	}
1512d90825e2SJohnny Huang 
151354552c69SJohnny Huang 	for (i = 0; i < 1792; i += 2) {
1514696656c6SJohnny Huang 		ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i);
151554552c69SJohnny Huang 		if (ret != OTP_SUCCESS) {
151654552c69SJohnny Huang 			printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n",
1517696656c6SJohnny Huang 			       i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]);
151854552c69SJohnny Huang 			return ret;
1519d90825e2SJohnny Huang 		}
1520de6fbf1cSJohnny Huang 	}
1521de6fbf1cSJohnny Huang 	otp_soak(0);
15222a856b9aSJohnny Huang 	return OTP_SUCCESS;
1523d90825e2SJohnny Huang 
1524d90825e2SJohnny Huang }
1525d90825e2SJohnny Huang 
1526696656c6SJohnny Huang static int otp_image_verify(uint8_t *src_buf, uint32_t length, uint8_t *digest_buf)
1527696656c6SJohnny Huang {
1528696656c6SJohnny Huang 	sha256_context ctx;
1529696656c6SJohnny Huang 	u8 digest_ret[CHECKSUM_LEN];
1530696656c6SJohnny Huang 
1531696656c6SJohnny Huang 	sha256_starts(&ctx);
1532696656c6SJohnny Huang 	sha256_update(&ctx, src_buf, length);
1533696656c6SJohnny Huang 	sha256_finish(&ctx, digest_ret);
1534696656c6SJohnny Huang 
1535696656c6SJohnny Huang 	if (!memcmp(digest_buf, digest_ret, CHECKSUM_LEN))
1536696656c6SJohnny Huang 		return 0;
1537696656c6SJohnny Huang 	else
1538696656c6SJohnny Huang 		return -1;
1539696656c6SJohnny Huang 
1540696656c6SJohnny Huang }
1541696656c6SJohnny Huang 
1542de6b0cc4SJohnny Huang static int do_otp_prog(int addr, int nconfirm)
154369d5fd8fSJohnny Huang {
154469d5fd8fSJohnny Huang 	int ret;
15459a4fe690SJohnny Huang 	int image_version = 0;
1546696656c6SJohnny Huang 	struct otp_header *otp_header;
1547696656c6SJohnny Huang 	struct otp_image_layout image_layout;
1548696656c6SJohnny Huang 	int image_size;
1549696656c6SJohnny Huang 	uint8_t *buf;
1550696656c6SJohnny Huang 	uint8_t *checksum;
155169d5fd8fSJohnny Huang 
1552696656c6SJohnny Huang 	otp_header = map_physmem(addr, sizeof(struct otp_header), MAP_WRBACK);
1553696656c6SJohnny Huang 	if (!otp_header) {
155469d5fd8fSJohnny Huang 		puts("Failed to map physical memory\n");
15552a856b9aSJohnny Huang 		return OTP_FAILURE;
155669d5fd8fSJohnny Huang 	}
1557d90825e2SJohnny Huang 
1558696656c6SJohnny Huang 	image_size = OTP_IMAGE_SIZE(otp_header->image_info);
1559696656c6SJohnny Huang 	unmap_physmem(otp_header, MAP_WRBACK);
1560696656c6SJohnny Huang 
1561696656c6SJohnny Huang 	buf = map_physmem(addr, image_size + CHECKSUM_LEN, MAP_WRBACK);
1562696656c6SJohnny Huang 
1563696656c6SJohnny Huang 	if (!buf) {
1564696656c6SJohnny Huang 		puts("Failed to map physical memory\n");
1565696656c6SJohnny Huang 		return OTP_FAILURE;
1566696656c6SJohnny Huang 	}
1567696656c6SJohnny Huang 	otp_header = (struct otp_header *) buf;
1568696656c6SJohnny Huang 	checksum = buf + otp_header->checksum_offset;
1569696656c6SJohnny Huang 
1570696656c6SJohnny Huang 	if (strcmp(OTP_MAGIC, (char *)otp_header->otp_magic) != 0) {
1571696656c6SJohnny Huang 		puts("Image is invalid\n");
1572696656c6SJohnny Huang 		return OTP_FAILURE;
1573696656c6SJohnny Huang 	}
1574696656c6SJohnny Huang 
1575696656c6SJohnny Huang 
15765010032bSJohnny Huang 	image_layout.data_length = (int)(OTP_REGION_SIZE(otp_header->data_info) / 2);
15775010032bSJohnny Huang 	image_layout.data = buf + OTP_REGION_OFFSET(otp_header->data_info);
15785010032bSJohnny Huang 	image_layout.data_ignore = image_layout.data + image_layout.data_length;
15795010032bSJohnny Huang 
15805010032bSJohnny Huang 	image_layout.conf_length = (int)(OTP_REGION_SIZE(otp_header->config_info) / 2);
1581696656c6SJohnny Huang 	image_layout.conf = buf + OTP_REGION_OFFSET(otp_header->config_info);
15825010032bSJohnny Huang 	image_layout.conf_ignore = image_layout.conf + image_layout.conf_length;
1583696656c6SJohnny Huang 
1584696656c6SJohnny Huang 	image_layout.strap = buf + OTP_REGION_OFFSET(otp_header->strap_info);
1585696656c6SJohnny Huang 
1586696656c6SJohnny Huang 	if (!strcmp("A0", (char *)otp_header->otp_version)) {
1587696656c6SJohnny Huang 		image_version = OTP_AST2600A0;
15885010032bSJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 3);
15895010032bSJohnny Huang 		image_layout.strap_pro = image_layout.strap + image_layout.strap_length;
15905010032bSJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 2 * image_layout.strap_length;
1591696656c6SJohnny Huang 	} else if (!strcmp("A1", (char *)otp_header->otp_version)) {
1592696656c6SJohnny Huang 		image_version = OTP_AST2600A1;
15935010032bSJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4);
15945010032bSJohnny Huang 		image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length;
15955010032bSJohnny Huang 		image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length;
15965010032bSJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length;
15975fdde29fSJohnny Huang 	} else if (!strcmp("A2", (char *)otp_header->otp_version)) {
15985fdde29fSJohnny Huang 		image_version = OTP_AST2600A2;
15995fdde29fSJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4);
16005fdde29fSJohnny Huang 		image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length;
16015fdde29fSJohnny Huang 		image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length;
16025fdde29fSJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length;
1603696656c6SJohnny Huang 	} else {
1604696656c6SJohnny Huang 		puts("Version is not supported\n");
1605696656c6SJohnny Huang 		return OTP_FAILURE;
1606696656c6SJohnny Huang 	}
1607696656c6SJohnny Huang 
16089a4fe690SJohnny Huang 	if (image_version != info_cb.version) {
16099a4fe690SJohnny Huang 		puts("Version is not match\n");
16109a4fe690SJohnny Huang 		return OTP_FAILURE;
16119a4fe690SJohnny Huang 	}
16129a4fe690SJohnny Huang 
1613696656c6SJohnny Huang 	if (otp_image_verify(buf, image_size, checksum)) {
1614696656c6SJohnny Huang 		puts("checksum is invalid\n");
1615696656c6SJohnny Huang 		return OTP_FAILURE;
1616d90825e2SJohnny Huang 	}
16177332532cSJohnny Huang 
161869d5fd8fSJohnny Huang 	if (!nconfirm) {
1619696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_DATA) {
16207f795e57SJohnny Huang 			printf("\nOTP data region :\n");
1621696656c6SJohnny Huang 			if (otp_print_data_info(&image_layout) < 0) {
162269d5fd8fSJohnny Huang 				printf("OTP data error, please check.\n");
16232a856b9aSJohnny Huang 				return OTP_FAILURE;
162469d5fd8fSJohnny Huang 			}
162569d5fd8fSJohnny Huang 		}
1626696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_STRAP) {
16277332532cSJohnny Huang 			printf("\nOTP strap region :\n");
16285010032bSJohnny Huang 			if (otp_print_strap_image(&image_layout) < 0) {
16297332532cSJohnny Huang 				printf("OTP strap error, please check.\n");
16307332532cSJohnny Huang 				return OTP_FAILURE;
16317332532cSJohnny Huang 			}
16327332532cSJohnny Huang 		}
1633696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_CONFIG) {
16347332532cSJohnny Huang 			printf("\nOTP configuration region :\n");
1635696656c6SJohnny Huang 			if (otp_print_conf_image(&image_layout) < 0) {
16367332532cSJohnny Huang 				printf("OTP config error, please check.\n");
16377332532cSJohnny Huang 				return OTP_FAILURE;
16387332532cSJohnny Huang 			}
16397332532cSJohnny Huang 		}
16407332532cSJohnny Huang 
164169d5fd8fSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
164269d5fd8fSJohnny Huang 		if (!confirm_yesno()) {
164369d5fd8fSJohnny Huang 			printf(" Aborting\n");
16442a856b9aSJohnny Huang 			return OTP_FAILURE;
164569d5fd8fSJohnny Huang 		}
164669d5fd8fSJohnny Huang 	}
16477332532cSJohnny Huang 
16485010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_DATA) {
16495010032bSJohnny Huang 		printf("programing data region ...\n");
16505010032bSJohnny Huang 		ret = otp_prog_data(&image_layout);
16515010032bSJohnny Huang 		if (ret != 0) {
16525010032bSJohnny Huang 			printf("Error\n");
16535010032bSJohnny Huang 			return ret;
16545010032bSJohnny Huang 		} else {
16555010032bSJohnny Huang 			printf("Done\n");
16565010032bSJohnny Huang 		}
16575010032bSJohnny Huang 	}
16585010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_STRAP) {
16595010032bSJohnny Huang 		printf("programing strap region ...\n");
16605010032bSJohnny Huang 		ret = otp_prog_strap(&image_layout);
16615010032bSJohnny Huang 		if (ret != 0) {
16625010032bSJohnny Huang 			printf("Error\n");
16635010032bSJohnny Huang 			return ret;
16645010032bSJohnny Huang 		} else {
16655010032bSJohnny Huang 			printf("Done\n");
16665010032bSJohnny Huang 		}
16675010032bSJohnny Huang 	}
16685010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_CONFIG) {
16695010032bSJohnny Huang 		printf("programing configuration region ...\n");
16705010032bSJohnny Huang 		ret = otp_prog_conf(&image_layout);
16715010032bSJohnny Huang 		if (ret != 0) {
16725010032bSJohnny Huang 			printf("Error\n");
16735010032bSJohnny Huang 			return ret;
16745010032bSJohnny Huang 		}
16755010032bSJohnny Huang 		printf("Done\n");
16765010032bSJohnny Huang 	}
1677cd1610b4SJohnny Huang 
16787332532cSJohnny Huang 	return OTP_SUCCESS;
16792a856b9aSJohnny Huang }
16802a856b9aSJohnny Huang 
16812a856b9aSJohnny Huang static int do_otp_prog_bit(int mode, int otp_dw_offset, int bit_offset, int value, int nconfirm)
1682cd1610b4SJohnny Huang {
1683a6af4a17SJohnny Huang 	uint32_t read[2];
1684d90825e2SJohnny Huang 	uint32_t prog_address = 0;
168566f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
1686cd1610b4SJohnny Huang 	int otp_bit;
168783655e91SJohnny Huang 	int ret = 0;
1688cd1610b4SJohnny Huang 
1689dacbba92SJohnny Huang 	otp_soak(0);
1690cd1610b4SJohnny Huang 	switch (mode) {
1691a6d0d645SJohnny Huang 	case OTP_REGION_CONF:
1692a6af4a17SJohnny Huang 		otp_read_config(otp_dw_offset, read);
1693cd1610b4SJohnny Huang 		prog_address = 0x800;
1694cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset / 8) * 0x200;
1695cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset % 8) * 0x2;
1696a6af4a17SJohnny Huang 		otp_bit = (read[0] >> bit_offset) & 0x1;
1697cd1610b4SJohnny Huang 		if (otp_bit == value) {
1698a6af4a17SJohnny Huang 			printf("OTPCFG%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
1699cd1610b4SJohnny Huang 			printf("No need to program\n");
17002a856b9aSJohnny Huang 			return OTP_SUCCESS;
1701cd1610b4SJohnny Huang 		}
1702cd1610b4SJohnny Huang 		if (otp_bit == 1 && value == 0) {
1703a6af4a17SJohnny Huang 			printf("OTPCFG%X[%X] = 1\n", otp_dw_offset, bit_offset);
1704cd1610b4SJohnny Huang 			printf("OTP is programed, which can't be clean\n");
17052a856b9aSJohnny Huang 			return OTP_FAILURE;
1706cd1610b4SJohnny Huang 		}
1707a6af4a17SJohnny Huang 		printf("Program OTPCFG%X[%X] to 1\n", otp_dw_offset, bit_offset);
1708cd1610b4SJohnny Huang 		break;
1709a6d0d645SJohnny Huang 	case OTP_REGION_DATA:
1710cd1610b4SJohnny Huang 		prog_address = otp_dw_offset;
1711cd1610b4SJohnny Huang 
1712cd1610b4SJohnny Huang 		if (otp_dw_offset % 2 == 0) {
1713a6af4a17SJohnny Huang 			otp_read_data(otp_dw_offset, read);
1714a6af4a17SJohnny Huang 			otp_bit = (read[0] >> bit_offset) & 0x1;
1715643b9cfdSJohnny Huang 
1716643b9cfdSJohnny Huang 			if (otp_bit == 1 && value == 0) {
1717643b9cfdSJohnny Huang 				printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
1718643b9cfdSJohnny Huang 				printf("OTP is programed, which can't be cleaned\n");
1719643b9cfdSJohnny Huang 				return OTP_FAILURE;
1720643b9cfdSJohnny Huang 			}
1721cd1610b4SJohnny Huang 		} else {
1722a6af4a17SJohnny Huang 			otp_read_data(otp_dw_offset - 1, read);
1723a6af4a17SJohnny Huang 			otp_bit = (read[1] >> bit_offset) & 0x1;
1724643b9cfdSJohnny Huang 
1725643b9cfdSJohnny Huang 			if (otp_bit == 0 && value == 1) {
1726643b9cfdSJohnny Huang 				printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
1727643b9cfdSJohnny Huang 				printf("OTP is programed, which can't be writen\n");
1728643b9cfdSJohnny Huang 				return OTP_FAILURE;
1729643b9cfdSJohnny Huang 			}
1730cd1610b4SJohnny Huang 		}
1731cd1610b4SJohnny Huang 		if (otp_bit == value) {
1732a6af4a17SJohnny Huang 			printf("OTPDATA%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
1733cd1610b4SJohnny Huang 			printf("No need to program\n");
17342a856b9aSJohnny Huang 			return OTP_SUCCESS;
1735cd1610b4SJohnny Huang 		}
1736643b9cfdSJohnny Huang 
1737a6af4a17SJohnny Huang 		printf("Program OTPDATA%X[%X] to 1\n", otp_dw_offset, bit_offset);
1738cd1610b4SJohnny Huang 		break;
1739a6d0d645SJohnny Huang 	case OTP_REGION_STRAP:
17408848d5dcSJohnny Huang 		otp_strap_status(otpstrap);
17418848d5dcSJohnny Huang 		otp_print_strap(bit_offset, 1);
17428848d5dcSJohnny Huang 		ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0, 0);
17438848d5dcSJohnny Huang 		if (ret == OTP_FAILURE)
17448848d5dcSJohnny Huang 			return OTP_FAILURE;
17458848d5dcSJohnny Huang 		else if (ret == OTP_PROG_SKIP)
17468848d5dcSJohnny Huang 			return OTP_SUCCESS;
1747a6af4a17SJohnny Huang 
1748cd1610b4SJohnny Huang 		break;
1749cd1610b4SJohnny Huang 	}
1750cd1610b4SJohnny Huang 
1751cd1610b4SJohnny Huang 	if (!nconfirm) {
1752cd1610b4SJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
1753cd1610b4SJohnny Huang 		if (!confirm_yesno()) {
1754cd1610b4SJohnny Huang 			printf(" Aborting\n");
17552a856b9aSJohnny Huang 			return OTP_FAILURE;
1756cd1610b4SJohnny Huang 		}
1757cd1610b4SJohnny Huang 	}
1758cd1610b4SJohnny Huang 
1759cd1610b4SJohnny Huang 	switch (mode) {
1760a6d0d645SJohnny Huang 	case OTP_REGION_STRAP:
176183655e91SJohnny Huang 		ret =  otp_prog_strap_bit(bit_offset, value);
176283655e91SJohnny Huang 		break;
1763a6d0d645SJohnny Huang 	case OTP_REGION_CONF:
1764a6d0d645SJohnny Huang 	case OTP_REGION_DATA:
176583655e91SJohnny Huang 		ret = otp_prog_bit(value, prog_address, bit_offset);
1766de6fbf1cSJohnny Huang 		break;
1767de6fbf1cSJohnny Huang 	}
1768de6fbf1cSJohnny Huang 	otp_soak(0);
176983655e91SJohnny Huang 	if (ret) {
17709009c25dSJohnny Huang 		printf("SUCCESS\n");
17712a856b9aSJohnny Huang 		return OTP_SUCCESS;
17729009c25dSJohnny Huang 	} else {
17739009c25dSJohnny Huang 		printf("OTP cannot be programed\n");
17749009c25dSJohnny Huang 		printf("FAILED\n");
17759009c25dSJohnny Huang 		return OTP_FAILURE;
17769009c25dSJohnny Huang 	}
1777cd1610b4SJohnny Huang 
17782a856b9aSJohnny Huang 	return OTP_USAGE;
1779cd1610b4SJohnny Huang }
1780cd1610b4SJohnny Huang 
17812a856b9aSJohnny Huang static int do_otpread(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
178269d5fd8fSJohnny Huang {
17832a856b9aSJohnny Huang 	uint32_t offset, count;
17842a856b9aSJohnny Huang 	int ret;
178569d5fd8fSJohnny Huang 
17862a856b9aSJohnny Huang 	if (argc == 4) {
17872a856b9aSJohnny Huang 		offset = simple_strtoul(argv[2], NULL, 16);
17882a856b9aSJohnny Huang 		count = simple_strtoul(argv[3], NULL, 16);
17892a856b9aSJohnny Huang 	} else if (argc == 3) {
17902a856b9aSJohnny Huang 		offset = simple_strtoul(argv[2], NULL, 16);
17912a856b9aSJohnny Huang 		count = 1;
17922a856b9aSJohnny Huang 	} else {
179369d5fd8fSJohnny Huang 		return CMD_RET_USAGE;
179469d5fd8fSJohnny Huang 	}
179569d5fd8fSJohnny Huang 
179669d5fd8fSJohnny Huang 
17972a856b9aSJohnny Huang 	if (!strcmp(argv[1], "conf")) {
17983d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
17992a856b9aSJohnny Huang 		ret = otp_print_config(offset, count);
18002a856b9aSJohnny Huang 	} else if (!strcmp(argv[1], "data")) {
18013d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
18022a856b9aSJohnny Huang 		ret = otp_print_data(offset, count);
18032a856b9aSJohnny Huang 	} else if (!strcmp(argv[1], "strap")) {
18043d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
18052a856b9aSJohnny Huang 		ret = otp_print_strap(offset, count);
18062a856b9aSJohnny Huang 	} else {
18072a856b9aSJohnny Huang 		return CMD_RET_USAGE;
180869d5fd8fSJohnny Huang 	}
180969d5fd8fSJohnny Huang 
18102a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
18112a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
18122a856b9aSJohnny Huang 	else
18132a856b9aSJohnny Huang 		return CMD_RET_USAGE;
18142a856b9aSJohnny Huang 
18152a856b9aSJohnny Huang }
18162a856b9aSJohnny Huang 
18172a856b9aSJohnny Huang static int do_otpprog(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
18182a856b9aSJohnny Huang {
18192a856b9aSJohnny Huang 	phys_addr_t addr;
18202a856b9aSJohnny Huang 	int ret;
18212a856b9aSJohnny Huang 
1822de6b0cc4SJohnny Huang 	if (argc == 3) {
1823ed071a2bSJohnny Huang 		if (strcmp(argv[1], "o"))
18242a856b9aSJohnny Huang 			return CMD_RET_USAGE;
18252a856b9aSJohnny Huang 		addr = simple_strtoul(argv[2], NULL, 16);
18263d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
1827de6b0cc4SJohnny Huang 		ret = do_otp_prog(addr, 1);
1828de6b0cc4SJohnny Huang 	} else if (argc == 2) {
18292a856b9aSJohnny Huang 		addr = simple_strtoul(argv[1], NULL, 16);
18303d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
1831de6b0cc4SJohnny Huang 		ret = do_otp_prog(addr, 0);
18322a856b9aSJohnny Huang 	} else {
18332a856b9aSJohnny Huang 		return CMD_RET_USAGE;
18342a856b9aSJohnny Huang 	}
18352a856b9aSJohnny Huang 
18362a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
18372a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
18382a856b9aSJohnny Huang 	else if (ret == OTP_FAILURE)
18392a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
18402a856b9aSJohnny Huang 	else
18412a856b9aSJohnny Huang 		return CMD_RET_USAGE;
18422a856b9aSJohnny Huang }
18432a856b9aSJohnny Huang 
18442a856b9aSJohnny Huang static int do_otppb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
18452a856b9aSJohnny Huang {
18462a856b9aSJohnny Huang 	int mode = 0;
18472a856b9aSJohnny Huang 	int nconfirm = 0;
18482a856b9aSJohnny Huang 	int otp_addr = 0;
18492a856b9aSJohnny Huang 	int bit_offset;
18502a856b9aSJohnny Huang 	int value;
18512a856b9aSJohnny Huang 	int ret;
18522a856b9aSJohnny Huang 
18532a856b9aSJohnny Huang 	if (argc != 4 && argc != 5 && argc != 6)
18542a856b9aSJohnny Huang 		return CMD_RET_USAGE;
18552a856b9aSJohnny Huang 
18562a856b9aSJohnny Huang 	/* Drop the pb cmd */
18572a856b9aSJohnny Huang 	argc--;
18582a856b9aSJohnny Huang 	argv++;
18592a856b9aSJohnny Huang 
18602a856b9aSJohnny Huang 	if (!strcmp(argv[0], "conf"))
1861a6d0d645SJohnny Huang 		mode = OTP_REGION_CONF;
18622a856b9aSJohnny Huang 	else if (!strcmp(argv[0], "strap"))
1863a6d0d645SJohnny Huang 		mode = OTP_REGION_STRAP;
18642a856b9aSJohnny Huang 	else if (!strcmp(argv[0], "data"))
1865a6d0d645SJohnny Huang 		mode = OTP_REGION_DATA;
1866cd1610b4SJohnny Huang 	else
18672a856b9aSJohnny Huang 		return CMD_RET_USAGE;
18682a856b9aSJohnny Huang 
18692a856b9aSJohnny Huang 	/* Drop the region cmd */
18702a856b9aSJohnny Huang 	argc--;
18712a856b9aSJohnny Huang 	argv++;
18722a856b9aSJohnny Huang 
1873ed071a2bSJohnny Huang 	if (!strcmp(argv[0], "o")) {
1874cd1610b4SJohnny Huang 		nconfirm = 1;
18752a856b9aSJohnny Huang 		/* Drop the force option */
18762a856b9aSJohnny Huang 		argc--;
18772a856b9aSJohnny Huang 		argv++;
18782a856b9aSJohnny Huang 	}
1879cd1610b4SJohnny Huang 
1880a6d0d645SJohnny Huang 	if (mode == OTP_REGION_STRAP) {
18812a856b9aSJohnny Huang 		bit_offset = simple_strtoul(argv[0], NULL, 16);
18822a856b9aSJohnny Huang 		value = simple_strtoul(argv[1], NULL, 16);
18830808cc55SJohnny Huang 		if (bit_offset >= 64 || (value != 0 && value != 1))
18842a856b9aSJohnny Huang 			return CMD_RET_USAGE;
1885cd1610b4SJohnny Huang 	} else {
18862a856b9aSJohnny Huang 		otp_addr = simple_strtoul(argv[0], NULL, 16);
18872a856b9aSJohnny Huang 		bit_offset = simple_strtoul(argv[1], NULL, 16);
18882a856b9aSJohnny Huang 		value = simple_strtoul(argv[2], NULL, 16);
18890808cc55SJohnny Huang 		if (bit_offset >= 32 || (value != 0 && value != 1))
18902a856b9aSJohnny Huang 			return CMD_RET_USAGE;
18910808cc55SJohnny Huang 		if (mode == OTP_REGION_DATA) {
189278855207SJohnny Huang 			if (otp_addr >= 0x800)
18930808cc55SJohnny Huang 				return CMD_RET_USAGE;
18940808cc55SJohnny Huang 		} else {
189578855207SJohnny Huang 			if (otp_addr >= 0x20)
18960808cc55SJohnny Huang 				return CMD_RET_USAGE;
18970808cc55SJohnny Huang 		}
1898cd1610b4SJohnny Huang 	}
1899cd1610b4SJohnny Huang 	if (value != 0 && value != 1)
19002a856b9aSJohnny Huang 		return CMD_RET_USAGE;
1901cd1610b4SJohnny Huang 
19023d3688adSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
19032a856b9aSJohnny Huang 	ret = do_otp_prog_bit(mode, otp_addr, bit_offset, value, nconfirm);
19042a856b9aSJohnny Huang 
19052a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
19062a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
19072a856b9aSJohnny Huang 	else if (ret == OTP_FAILURE)
19082a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
19092a856b9aSJohnny Huang 	else
19102a856b9aSJohnny Huang 		return CMD_RET_USAGE;
19112a856b9aSJohnny Huang }
19122a856b9aSJohnny Huang 
19132a856b9aSJohnny Huang static int do_otpcmp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
19142a856b9aSJohnny Huang {
19152a856b9aSJohnny Huang 	phys_addr_t addr;
19162a856b9aSJohnny Huang 	int otp_addr = 0;
19172a856b9aSJohnny Huang 
19182a856b9aSJohnny Huang 	if (argc != 3)
19192a856b9aSJohnny Huang 		return CMD_RET_USAGE;
19202a856b9aSJohnny Huang 
19213d3688adSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
19222a856b9aSJohnny Huang 	addr = simple_strtoul(argv[1], NULL, 16);
19232a856b9aSJohnny Huang 	otp_addr = simple_strtoul(argv[2], NULL, 16);
19242a856b9aSJohnny Huang 	if (otp_compare(otp_addr, addr) == 0) {
192569d5fd8fSJohnny Huang 		printf("Compare pass\n");
19262a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
192769d5fd8fSJohnny Huang 	} else {
192869d5fd8fSJohnny Huang 		printf("Compare fail\n");
19292a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
193069d5fd8fSJohnny Huang 	}
193169d5fd8fSJohnny Huang }
193269d5fd8fSJohnny Huang 
193366f2f8e5SJohnny Huang static int do_otpinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
193466f2f8e5SJohnny Huang {
1935a8bd6d8cSJohnny Huang 	int view = 0;
19362d4b0742SJohnny Huang 	int input;
1937a8bd6d8cSJohnny Huang 
1938a8bd6d8cSJohnny Huang 	if (argc != 2 && argc != 3)
193966f2f8e5SJohnny Huang 		return CMD_RET_USAGE;
194066f2f8e5SJohnny Huang 
19412d4b0742SJohnny Huang 	if (!strcmp(argv[1], "conf")) {
194266f2f8e5SJohnny Huang 
19433d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
19442d4b0742SJohnny Huang 		if (argc == 3) {
19452d4b0742SJohnny Huang 			input = simple_strtoul(argv[2], NULL, 16);
19462d4b0742SJohnny Huang 			otp_print_conf_info(input);
19472d4b0742SJohnny Huang 		} else {
19482d4b0742SJohnny Huang 			otp_print_conf_info(-1);
19492d4b0742SJohnny Huang 		}
19502d4b0742SJohnny Huang 	} else if (!strcmp(argv[1], "strap")) {
19512d4b0742SJohnny Huang 		if (!strcmp(argv[2], "v")) {
1952a8bd6d8cSJohnny Huang 			view = 1;
1953a8bd6d8cSJohnny Huang 			/* Drop the view option */
1954a8bd6d8cSJohnny Huang 			argc--;
1955a8bd6d8cSJohnny Huang 			argv++;
1956a8bd6d8cSJohnny Huang 		}
19573d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
1958b458cd62SJohnny Huang 		otp_print_strap_info(view);
195966f2f8e5SJohnny Huang 	} else {
196066f2f8e5SJohnny Huang 		return CMD_RET_USAGE;
196166f2f8e5SJohnny Huang 	}
19622d4b0742SJohnny Huang 
196366f2f8e5SJohnny Huang 	return CMD_RET_SUCCESS;
196466f2f8e5SJohnny Huang }
196566f2f8e5SJohnny Huang 
1966737ed20bSJohnny Huang static int do_otpprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1967737ed20bSJohnny Huang {
1968737ed20bSJohnny Huang 	int input;
1969737ed20bSJohnny Huang 	int bit_offset;
1970737ed20bSJohnny Huang 	int prog_address;
197183655e91SJohnny Huang 	int ret;
1972737ed20bSJohnny Huang 	if (argc != 3 && argc != 2)
1973737ed20bSJohnny Huang 		return CMD_RET_USAGE;
1974737ed20bSJohnny Huang 
1975ed071a2bSJohnny Huang 	if (!strcmp(argv[0], "o")) {
1976737ed20bSJohnny Huang 		input = simple_strtoul(argv[2], NULL, 16);
1977737ed20bSJohnny Huang 	} else {
1978737ed20bSJohnny Huang 		input = simple_strtoul(argv[1], NULL, 16);
1979737ed20bSJohnny Huang 		printf("OTPSTRAP[%d] will be protected\n", input);
1980737ed20bSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
1981737ed20bSJohnny Huang 		if (!confirm_yesno()) {
1982737ed20bSJohnny Huang 			printf(" Aborting\n");
1983737ed20bSJohnny Huang 			return CMD_RET_FAILURE;
1984737ed20bSJohnny Huang 		}
1985737ed20bSJohnny Huang 	}
1986737ed20bSJohnny Huang 
1987737ed20bSJohnny Huang 	prog_address = 0x800;
1988737ed20bSJohnny Huang 	if (input < 32) {
1989737ed20bSJohnny Huang 		bit_offset = input;
1990737ed20bSJohnny Huang 		prog_address |= 0x60c;
1991737ed20bSJohnny Huang 	} else if (input < 64) {
1992737ed20bSJohnny Huang 		bit_offset = input - 32;
1993737ed20bSJohnny Huang 		prog_address |= 0x60e;
1994737ed20bSJohnny Huang 	} else {
1995737ed20bSJohnny Huang 		return CMD_RET_USAGE;
1996737ed20bSJohnny Huang 	}
1997737ed20bSJohnny Huang 
1998737ed20bSJohnny Huang 	if (verify_bit(prog_address, bit_offset, 1) == 0) {
1999737ed20bSJohnny Huang 		printf("OTPSTRAP[%d] already protected\n", input);
2000737ed20bSJohnny Huang 	}
2001de6fbf1cSJohnny Huang 
200283655e91SJohnny Huang 	ret = otp_prog_bit(1, prog_address, bit_offset);
2003de6fbf1cSJohnny Huang 	otp_soak(0);
200483655e91SJohnny Huang 
200583655e91SJohnny Huang 	if (ret) {
2006737ed20bSJohnny Huang 		printf("OTPSTRAP[%d] is protected\n", input);
2007737ed20bSJohnny Huang 		return CMD_RET_SUCCESS;
2008737ed20bSJohnny Huang 	}
2009737ed20bSJohnny Huang 
2010737ed20bSJohnny Huang 	printf("Protect OTPSTRAP[%d] fail\n", input);
2011737ed20bSJohnny Huang 	return CMD_RET_FAILURE;
2012737ed20bSJohnny Huang 
2013737ed20bSJohnny Huang }
20149a4fe690SJohnny Huang 
2015f67375f7SJohnny Huang static int do_otpver(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2016f67375f7SJohnny Huang {
2017f67375f7SJohnny Huang 	printf("OTP tool version: %s\n", OTP_VER);
2018f67375f7SJohnny Huang 	printf("OTP info version: %s\n", OTP_INFO_VER);
2019f67375f7SJohnny Huang 
2020f67375f7SJohnny Huang 	return CMD_RET_SUCCESS;
2021f67375f7SJohnny Huang }
2022f67375f7SJohnny Huang 
20232a856b9aSJohnny Huang static cmd_tbl_t cmd_otp[] = {
2024f67375f7SJohnny Huang 	U_BOOT_CMD_MKENT(version, 1, 0, do_otpver, "", ""),
20252a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(read, 4, 0, do_otpread, "", ""),
2026a8bd6d8cSJohnny Huang 	U_BOOT_CMD_MKENT(info, 3, 0, do_otpinfo, "", ""),
2027de6b0cc4SJohnny Huang 	U_BOOT_CMD_MKENT(prog, 3, 0, do_otpprog, "", ""),
20282a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(pb, 6, 0, do_otppb, "", ""),
2029737ed20bSJohnny Huang 	U_BOOT_CMD_MKENT(protect, 3, 0, do_otpprotect, "", ""),
20302a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(cmp, 3, 0, do_otpcmp, "", ""),
20312a856b9aSJohnny Huang };
20322a856b9aSJohnny Huang 
20332a856b9aSJohnny Huang static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
20342a856b9aSJohnny Huang {
20352a856b9aSJohnny Huang 	cmd_tbl_t *cp;
20360dae9d52SJohnny Huang 	uint32_t ver;
20372a856b9aSJohnny Huang 
20382a856b9aSJohnny Huang 	cp = find_cmd_tbl(argv[1], cmd_otp, ARRAY_SIZE(cmd_otp));
20392a856b9aSJohnny Huang 
2040737ed20bSJohnny Huang 	/* Drop the otp command */
20412a856b9aSJohnny Huang 	argc--;
20422a856b9aSJohnny Huang 	argv++;
20432a856b9aSJohnny Huang 
20442a856b9aSJohnny Huang 	if (cp == NULL || argc > cp->maxargs)
20452a856b9aSJohnny Huang 		return CMD_RET_USAGE;
20462a856b9aSJohnny Huang 	if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
20472a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
20482a856b9aSJohnny Huang 
20490dae9d52SJohnny Huang 	ver = chip_version();
20500dae9d52SJohnny Huang 	switch (ver) {
20510dae9d52SJohnny Huang 	case OTP_AST2600A0:
2052696656c6SJohnny Huang 		info_cb.version = OTP_AST2600A0;
20539a4fe690SJohnny Huang 		info_cb.conf_info = a0_conf_info;
20549a4fe690SJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a0_conf_info);
20559a4fe690SJohnny Huang 		info_cb.strap_info = a0_strap_info;
20569a4fe690SJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a0_strap_info);
20579a4fe690SJohnny Huang 		info_cb.key_info = a0_key_type;
20589a4fe690SJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a0_key_type);
20590dae9d52SJohnny Huang 		break;
20600dae9d52SJohnny Huang 	case OTP_AST2600A1:
2061696656c6SJohnny Huang 		info_cb.version = OTP_AST2600A1;
20623cb28812SJohnny Huang 		info_cb.conf_info = a1_conf_info;
20633cb28812SJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a1_conf_info);
20643cb28812SJohnny Huang 		info_cb.strap_info = a1_strap_info;
20653cb28812SJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a1_strap_info);
20669a4fe690SJohnny Huang 		info_cb.key_info = a1_key_type;
20679a4fe690SJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a1_key_type);
20680dae9d52SJohnny Huang 		break;
20690dae9d52SJohnny Huang 	case OTP_AST2600A2:
20705fdde29fSJohnny Huang 		info_cb.version = OTP_AST2600A2;
20715fdde29fSJohnny Huang 		info_cb.conf_info = a2_conf_info;
20725fdde29fSJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a2_conf_info);
20735fdde29fSJohnny Huang 		info_cb.strap_info = a2_strap_info;
20745fdde29fSJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a2_strap_info);
20755fdde29fSJohnny Huang 		info_cb.key_info = a2_key_type;
20765fdde29fSJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a2_key_type);
20770dae9d52SJohnny Huang 		break;
20780dae9d52SJohnny Huang 	default:
2079*f1be5099SJohnny Huang 		printf("SOC is not supported\n");
20800dae9d52SJohnny Huang 		return CMD_RET_FAILURE;
20819a4fe690SJohnny Huang 	}
20829a4fe690SJohnny Huang 
20832a856b9aSJohnny Huang 	return cp->cmd(cmdtp, flag, argc, argv);
208469d5fd8fSJohnny Huang }
208569d5fd8fSJohnny Huang 
208669d5fd8fSJohnny Huang U_BOOT_CMD(
208769d5fd8fSJohnny Huang 	otp, 7, 0,  do_ast_otp,
208869d5fd8fSJohnny Huang 	"ASPEED One-Time-Programmable sub-system",
2089f67375f7SJohnny Huang 	"version\n"
2090f67375f7SJohnny Huang 	"otp read conf|data <otp_dw_offset> <dw_count>\n"
20912a856b9aSJohnny Huang 	"otp read strap <strap_bit_offset> <bit_count>\n"
20922d4b0742SJohnny Huang 	"otp info strap [v]\n"
20932d4b0742SJohnny Huang 	"otp info conf [otp_dw_offset]\n"
2094de6b0cc4SJohnny Huang 	"otp prog [o] <addr>\n"
2095ed071a2bSJohnny Huang 	"otp pb conf|data [o] <otp_dw_offset> <bit_offset> <value>\n"
2096ed071a2bSJohnny Huang 	"otp pb strap [o] <bit_offset> <value>\n"
2097ed071a2bSJohnny Huang 	"otp protect [o] <bit_offset>\n"
20982a856b9aSJohnny Huang 	"otp cmp <addr> <otp_dw_offset>\n"
209969d5fd8fSJohnny Huang );
2100