xref: /openbmc/u-boot/cmd/otp.c (revision cd1610b4)
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>
2369d5fd8fSJohnny Huang 
2469d5fd8fSJohnny Huang DECLARE_GLOBAL_DATA_PTR;
2569d5fd8fSJohnny Huang 
2669d5fd8fSJohnny Huang #define OTP_PASSWD	0x349fe38a
2769d5fd8fSJohnny Huang #define RETRY		3
2869d5fd8fSJohnny Huang #define MODE_CONF	1
2969d5fd8fSJohnny Huang #define MODE_STRAP	2
3069d5fd8fSJohnny Huang #define MODE_DATA	3
3169d5fd8fSJohnny Huang #define MODE_ALL	4
3269d5fd8fSJohnny Huang 
33e1f9e54eSJohnny Huang #define DISABLE_SECREG_PROG		BIT(0)
34e1f9e54eSJohnny Huang #define ENABLE_SEC_BOOT			BIT(1)
35e1f9e54eSJohnny Huang #define INIT_PROG_DONE			BIT(2)
36e1f9e54eSJohnny Huang #define ENABLE_USERREG_ECC		BIT(3)
37e1f9e54eSJohnny Huang #define ENABLE_SECREG_ECC		BIT(4)
38e1f9e54eSJohnny Huang #define DISABLE_LOW_SEC_KEY		BIT(5)
39e1f9e54eSJohnny Huang #define IGNORE_SEC_BOOT_HWSTRAP		BIT(6)
40e1f9e54eSJohnny Huang #define SEC_BOOT_MDOES(x)		(x >> 7)
41e1f9e54eSJohnny Huang #define   SEC_MODE1			0x0
42e1f9e54eSJohnny Huang #define   SEC_MODE2			0x1
43e1f9e54eSJohnny Huang #define OTP_BIT_CELL_MODES(x)		((x >> 8) & 0x3)
44e1f9e54eSJohnny Huang #define   SINGLE_CELL_MODE		0x0
45e1f9e54eSJohnny Huang #define   DIFFERENTIAL_MODE		0x1
46e1f9e54eSJohnny Huang #define   DIFFERENTIAL_REDUDANT_MODE	0x2
47e1f9e54eSJohnny Huang #define CRYPTO_MODES(x)			((x >> 10) & 0x3)
48e1f9e54eSJohnny Huang #define   CRYPTO_RSA1024		0x0
49e1f9e54eSJohnny Huang #define   CRYPTO_RSA2048		0x1
50e1f9e54eSJohnny Huang #define   CRYPTO_RSA3072		0x2
51e1f9e54eSJohnny Huang #define   CRYPTO_RSA4096		0x3
52e1f9e54eSJohnny Huang #define HASH_MODES(x)			((x >> 12) & 0x3)
53e1f9e54eSJohnny Huang #define   HASH_SAH224			0x0
54e1f9e54eSJohnny Huang #define   HASH_SAH256			0x1
55e1f9e54eSJohnny Huang #define   HASH_SAH384			0x2
56e1f9e54eSJohnny Huang #define   HASH_SAH512			0x3
57e1f9e54eSJohnny Huang #define SECREG_SIZE(x)			((x >> 16) & 0x3f)
58e1f9e54eSJohnny Huang #define WRITE_PROTECT_SECREG		BIT(22)
59e1f9e54eSJohnny Huang #define WRITE_PROTECT_USERREG		BIT(23)
60e1f9e54eSJohnny Huang #define WRITE_PROTECT_CONFREG		BIT(24)
61e1f9e54eSJohnny Huang #define WRITE_PROTECT_STRAPREG		BIT(25)
62e1f9e54eSJohnny Huang #define ENABLE_COPY_TO_SRAM		BIT(26)
63e1f9e54eSJohnny Huang #define ENABLE_IMAGE_ENC		BIT(27)
64e1f9e54eSJohnny Huang #define WRITE_PROTECT_KEY_RETIRE	BIT(29)
65e1f9e54eSJohnny Huang #define ENABLE_SIPROM_RED		BIT(30)
66e1f9e54eSJohnny Huang #define ENABLE_SIPROM_MLOCK		BIT(31)
67e1f9e54eSJohnny Huang 
68e1f9e54eSJohnny Huang #define VENDER_ID(x) 			(x & 0xFFFF)
69e1f9e54eSJohnny Huang #define KEY_REVISION(x)			((x >> 16) & 0xFFFF)
70e1f9e54eSJohnny Huang 
71e1f9e54eSJohnny Huang #define SEC_BOOT_HEADER_OFFSET(x)	(x & 0xFFFF)
72e1f9e54eSJohnny Huang 
73e1f9e54eSJohnny Huang #define KEYS_VALID_BITS(x)		(x & 0xff)
74e1f9e54eSJohnny Huang #define KEYS_RETIRE_BITS(x)		((x >> 16) & 0xff)
754c1c9b35SJohnny Huang 
764c1c9b35SJohnny Huang #define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
774c1c9b35SJohnny Huang #define PBWIDTH 60
784c1c9b35SJohnny Huang 
794c1c9b35SJohnny Huang void printProgress(int numerator, int denominator, char *format, ...)
804c1c9b35SJohnny Huang {
814c1c9b35SJohnny Huang 	int val = numerator * 100 / denominator;
824c1c9b35SJohnny Huang 	int lpad = numerator * PBWIDTH / denominator;
834c1c9b35SJohnny Huang 	int rpad = PBWIDTH - lpad;
844c1c9b35SJohnny Huang 	char buffer[256];
854c1c9b35SJohnny Huang 	va_list aptr;
864c1c9b35SJohnny Huang 
874c1c9b35SJohnny Huang 	va_start(aptr, format);
884c1c9b35SJohnny Huang 	vsprintf(buffer, format, aptr);
894c1c9b35SJohnny Huang 	va_end(aptr);
904c1c9b35SJohnny Huang 
914c1c9b35SJohnny Huang 	printf("\r%3d%% [%.*s%*s] %s", val, lpad, PBSTR, rpad, "", buffer);
924c1c9b35SJohnny Huang 	if (numerator == denominator)
934c1c9b35SJohnny Huang 		printf("\n");
944c1c9b35SJohnny Huang }
954c1c9b35SJohnny Huang 
9669d5fd8fSJohnny Huang struct otpstrap {
9769d5fd8fSJohnny Huang 	int value;
9869d5fd8fSJohnny Huang 	int option_array[7];
9969d5fd8fSJohnny Huang 	int remain_times;
10069d5fd8fSJohnny Huang 	int writeable_option;
10169d5fd8fSJohnny Huang 	int protected;
10269d5fd8fSJohnny Huang };
10369d5fd8fSJohnny Huang 
10469d5fd8fSJohnny Huang static int otp_read_data(uint32_t offset, uint32_t *data)
10569d5fd8fSJohnny Huang {
10669d5fd8fSJohnny Huang 	writel(offset, 0x1e6f2010); //Read address
10769d5fd8fSJohnny Huang 	writel(0x23b1e361, 0x1e6f2004); //trigger read
10869d5fd8fSJohnny Huang 	udelay(2);
10969d5fd8fSJohnny Huang 	data[0] = readl(0x1e6f2020);
11069d5fd8fSJohnny Huang 	data[1] = readl(0x1e6f2024);
11169d5fd8fSJohnny Huang 	return 1;
11269d5fd8fSJohnny Huang }
11369d5fd8fSJohnny Huang 
11469d5fd8fSJohnny Huang static int otp_read_config(uint32_t offset, uint32_t *data)
11569d5fd8fSJohnny Huang {
11669d5fd8fSJohnny Huang 	int config_offset;
11769d5fd8fSJohnny Huang 
11869d5fd8fSJohnny Huang 	config_offset = 0x800;
11969d5fd8fSJohnny Huang 	config_offset |= (offset / 8) * 0x200;
12069d5fd8fSJohnny Huang 	config_offset |= (offset % 8) * 0x2;
12169d5fd8fSJohnny Huang 
12269d5fd8fSJohnny Huang 	writel(config_offset, 0x1e6f2010);  //Read address
12369d5fd8fSJohnny Huang 	writel(0x23b1e361, 0x1e6f2004); //trigger read
12469d5fd8fSJohnny Huang 	udelay(2);
12569d5fd8fSJohnny Huang 	data[0] = readl(0x1e6f2020);
12669d5fd8fSJohnny Huang 
12769d5fd8fSJohnny Huang 	return 1;
12869d5fd8fSJohnny Huang }
12969d5fd8fSJohnny Huang 
13069d5fd8fSJohnny Huang static int otp_print_config(uint32_t offset, int dw_count)
13169d5fd8fSJohnny Huang {
13269d5fd8fSJohnny Huang 	int i;
13369d5fd8fSJohnny Huang 	uint32_t ret[1];
13469d5fd8fSJohnny Huang 
13569d5fd8fSJohnny Huang 	if (offset + dw_count > 32)
13669d5fd8fSJohnny Huang 		return -1;
13769d5fd8fSJohnny Huang 	for (i = offset; i < offset + dw_count; i ++) {
13869d5fd8fSJohnny Huang 		otp_read_config(i, ret);
13969d5fd8fSJohnny Huang 		printf("OTPCFG%d: %08X\n", i, ret[0]);
14069d5fd8fSJohnny Huang 	}
14169d5fd8fSJohnny Huang 	printf("\n");
14269d5fd8fSJohnny Huang 	return 1;
14369d5fd8fSJohnny Huang }
14469d5fd8fSJohnny Huang 
14569d5fd8fSJohnny Huang static int otp_print_data(uint32_t offset, int dw_count)
14669d5fd8fSJohnny Huang {
14769d5fd8fSJohnny Huang 	int i;
14869d5fd8fSJohnny Huang 	uint32_t ret[2];
14969d5fd8fSJohnny Huang 
15069d5fd8fSJohnny Huang 	if (offset + dw_count > 2048 || offset % 4 != 0)
15169d5fd8fSJohnny Huang 		return -1;
15269d5fd8fSJohnny Huang 	for (i = offset; i < offset + dw_count; i += 2) {
15369d5fd8fSJohnny Huang 		otp_read_data(i, ret);
15469d5fd8fSJohnny Huang 		if (i % 4 == 0)
15569d5fd8fSJohnny Huang 			printf("%03X: %08X %08X ", i * 4, ret[0], ret[1]);
15669d5fd8fSJohnny Huang 		else
15769d5fd8fSJohnny Huang 			printf("%08X %08X\n", ret[0], ret[1]);
15869d5fd8fSJohnny Huang 
15969d5fd8fSJohnny Huang 	}
16069d5fd8fSJohnny Huang 	printf("\n");
16169d5fd8fSJohnny Huang 	return 1;
16269d5fd8fSJohnny Huang }
16369d5fd8fSJohnny Huang 
16469d5fd8fSJohnny Huang static int otp_compare(uint32_t otp_addr, uint32_t addr)
16569d5fd8fSJohnny Huang {
16669d5fd8fSJohnny Huang 	uint32_t ret;
16769d5fd8fSJohnny Huang 	uint32_t *buf;
16869d5fd8fSJohnny Huang 
16969d5fd8fSJohnny Huang 	buf = map_physmem(addr, 16, MAP_WRBACK);
17069d5fd8fSJohnny Huang 	printf("%08X\n", buf[0]);
17169d5fd8fSJohnny Huang 	printf("%08X\n", buf[1]);
17269d5fd8fSJohnny Huang 	printf("%08X\n", buf[2]);
17369d5fd8fSJohnny Huang 	printf("%08X\n", buf[3]);
17469d5fd8fSJohnny Huang 	writel(otp_addr, 0x1e6f2010); //Compare address
17569d5fd8fSJohnny Huang 	writel(buf[0], 0x1e6f2020); //Compare data 1
17669d5fd8fSJohnny Huang 	writel(buf[1], 0x1e6f2024); //Compare data 2
17769d5fd8fSJohnny Huang 	writel(buf[2], 0x1e6f2028); //Compare data 3
17869d5fd8fSJohnny Huang 	writel(buf[3], 0x1e6f202c); //Compare data 4
17969d5fd8fSJohnny Huang 	writel(0x23b1e363, 0x1e6f2004); //Compare command
18069d5fd8fSJohnny Huang 	udelay(10);
18169d5fd8fSJohnny Huang 	ret = readl(0x1e6f2014); //Compare command
18269d5fd8fSJohnny Huang 	if (ret & 0x1)
18369d5fd8fSJohnny Huang 		return 0;
18469d5fd8fSJohnny Huang 	else
18569d5fd8fSJohnny Huang 		return -1;
18669d5fd8fSJohnny Huang }
18769d5fd8fSJohnny Huang 
18869d5fd8fSJohnny Huang static void otp_write(uint32_t otp_addr, uint32_t data)
18969d5fd8fSJohnny Huang {
19069d5fd8fSJohnny Huang 	writel(otp_addr, 0x1e6f2010); //write address
19169d5fd8fSJohnny Huang 	writel(data, 0x1e6f2020); //write data
19269d5fd8fSJohnny Huang 	writel(0x23b1e362, 0x1e6f2004); //write command
19369d5fd8fSJohnny Huang 	udelay(100);
19469d5fd8fSJohnny Huang }
19569d5fd8fSJohnny Huang 
19669d5fd8fSJohnny Huang static void otp_prog(uint32_t otp_addr, uint32_t prog_bit)
19769d5fd8fSJohnny Huang {
19869d5fd8fSJohnny Huang 	writel(otp_addr, 0x1e6f2010); //write address
19969d5fd8fSJohnny Huang 	writel(prog_bit, 0x1e6f2020); //write data
20069d5fd8fSJohnny Huang 	writel(0x23b1e364, 0x1e6f2004); //write command
20169d5fd8fSJohnny Huang 	udelay(85);
20269d5fd8fSJohnny Huang }
20369d5fd8fSJohnny Huang 
2044c1c9b35SJohnny Huang static int prog_conf_verify(uint32_t otp_addr, int bit_offset, int value)
20569d5fd8fSJohnny Huang {
20669d5fd8fSJohnny Huang 	int ret;
20769d5fd8fSJohnny Huang 
20869d5fd8fSJohnny Huang 	writel(otp_addr, 0x1e6f2010); //Read address
20969d5fd8fSJohnny Huang 	writel(0x23b1e361, 0x1e6f2004); //trigger read
21069d5fd8fSJohnny Huang 	udelay(2);
21169d5fd8fSJohnny Huang 	ret = readl(0x1e6f2020);
2124c1c9b35SJohnny Huang 	// printf("prog_conf_verify = %x\n", ret);
21369d5fd8fSJohnny Huang 	if (((ret >> bit_offset) & 1) == value)
21469d5fd8fSJohnny Huang 		return 0;
21569d5fd8fSJohnny Huang 	else
21669d5fd8fSJohnny Huang 		return -1;
21769d5fd8fSJohnny Huang }
21869d5fd8fSJohnny Huang 
2194c1c9b35SJohnny Huang static uint32_t prog_data_dw_verify(uint32_t otp_addr, uint32_t *value, uint32_t *compare, int size)
2204c1c9b35SJohnny Huang {
2214c1c9b35SJohnny Huang 	uint32_t ret[2];
2224c1c9b35SJohnny Huang 
2234c1c9b35SJohnny Huang 	otp_addr &= ~(1 << 15);
2244c1c9b35SJohnny Huang 
2254c1c9b35SJohnny Huang 	if (otp_addr % 2 == 0)
2264c1c9b35SJohnny Huang 		writel(otp_addr, 0x1e6f2010); //Read address
2274c1c9b35SJohnny Huang 	else
2284c1c9b35SJohnny Huang 		writel(otp_addr - 1, 0x1e6f2010); //Read address
2294c1c9b35SJohnny Huang 	writel(0x23b1e361, 0x1e6f2004); //trigger read
2304c1c9b35SJohnny Huang 	udelay(2);
2314c1c9b35SJohnny Huang 	ret[0] = readl(0x1e6f2020);
2324c1c9b35SJohnny Huang 	ret[1] = readl(0x1e6f2024);
2334c1c9b35SJohnny Huang 	if (size == 1) {
2344c1c9b35SJohnny Huang 		if (otp_addr % 2 == 0) {
2354c1c9b35SJohnny Huang 			// printf("check %x : %x = %x\n", otp_addr, ret[0], value[0]);
2364c1c9b35SJohnny Huang 			if (value[0] == ret[0]) {
2374c1c9b35SJohnny Huang 				compare[0] = 0;
2384c1c9b35SJohnny Huang 				return 0;
2394c1c9b35SJohnny Huang 			} else {
2404c1c9b35SJohnny Huang 				compare[0] = value[0] ^ ret[0];
2414c1c9b35SJohnny Huang 				return -1;
2424c1c9b35SJohnny Huang 			}
2434c1c9b35SJohnny Huang 
2444c1c9b35SJohnny Huang 		} else {
2454c1c9b35SJohnny Huang 			// printf("check %x : %x = %x\n", otp_addr, ret[1], value[0]);
2464c1c9b35SJohnny Huang 			if (value[0] == ret[1]) {
2474c1c9b35SJohnny Huang 				compare[0] = ~0;
2484c1c9b35SJohnny Huang 				return 0;
2494c1c9b35SJohnny Huang 			} else {
2504c1c9b35SJohnny Huang 				compare[0] = ~(value[0] ^ ret[0]);
2514c1c9b35SJohnny Huang 				return -1;
2524c1c9b35SJohnny Huang 			}
2534c1c9b35SJohnny Huang 		}
2544c1c9b35SJohnny Huang 	} else if (size == 2) {
2554c1c9b35SJohnny Huang 		// otp_addr should be even
2564c1c9b35SJohnny Huang 		if ((value[0] == ret[0]) && (value[1] == ret[1])) {
2574c1c9b35SJohnny Huang 			// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
2584c1c9b35SJohnny Huang 			// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
2594c1c9b35SJohnny Huang 			compare[0] = 0;
2604c1c9b35SJohnny Huang 			compare[1] = ~0;
2614c1c9b35SJohnny Huang 			return 0;
2624c1c9b35SJohnny Huang 		} else {
2634c1c9b35SJohnny Huang 			// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
2644c1c9b35SJohnny Huang 			// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
2654c1c9b35SJohnny Huang 			compare[0] = value[0] ^ ret[0];
2664c1c9b35SJohnny Huang 			compare[1] = ~(value[1] ^ ret[1]);
2674c1c9b35SJohnny Huang 			return -1;
2684c1c9b35SJohnny Huang 		}
2694c1c9b35SJohnny Huang 	} else {
2704c1c9b35SJohnny Huang 		return -1;
2714c1c9b35SJohnny Huang 	}
2724c1c9b35SJohnny Huang }
2734c1c9b35SJohnny Huang 
27469d5fd8fSJohnny Huang static int otp_conf_parse(uint32_t *OTPCFG)
27569d5fd8fSJohnny Huang {
2764c1c9b35SJohnny Huang 	int tmp, i;
2774c1c9b35SJohnny Huang 	int pass = 0;
27869d5fd8fSJohnny Huang 
27969d5fd8fSJohnny Huang 	printf("OTPCFG0-D[0]\n");
280e1f9e54eSJohnny Huang 	if (OTPCFG[0] & DISABLE_SECREG_PROG)
28169d5fd8fSJohnny Huang 		printf("  Disable Secure Region programming\n");
28269d5fd8fSJohnny Huang 	else
28369d5fd8fSJohnny Huang 		printf("  Enable Secure Region programming\n");
28469d5fd8fSJohnny Huang 	printf("OTPCFG0-D[1]\n");
285e1f9e54eSJohnny Huang 	if (OTPCFG[0] & ENABLE_SEC_BOOT)
28669d5fd8fSJohnny Huang 		printf("  Enable Secure Boot\n");
28769d5fd8fSJohnny Huang 	else
28869d5fd8fSJohnny Huang 		printf("  Disable Secure Boot\n");
28969d5fd8fSJohnny Huang 	printf("OTPCFG0-D[3]\n");
290e1f9e54eSJohnny Huang 	if (OTPCFG[0] & ENABLE_USERREG_ECC)
29169d5fd8fSJohnny Huang 		printf("  User region ECC enable\n");
29269d5fd8fSJohnny Huang 	else
29369d5fd8fSJohnny Huang 		printf("  User region ECC disable\n");
29469d5fd8fSJohnny Huang 	printf("OTPCFG0-D[4]\n");
295e1f9e54eSJohnny Huang 	if (OTPCFG[0] & ENABLE_SECREG_ECC)
29669d5fd8fSJohnny Huang 		printf("  Secure Region ECC enable\n");
29769d5fd8fSJohnny Huang 	else
29869d5fd8fSJohnny Huang 		printf("  Secure Region ECC disable\n");
29969d5fd8fSJohnny Huang 	printf("OTPCFG0-D[5]\n");
300e1f9e54eSJohnny Huang 	if (OTPCFG[0] & DISABLE_LOW_SEC_KEY)
30169d5fd8fSJohnny Huang 		printf("  Disable low security key\n");
30269d5fd8fSJohnny Huang 	else
30369d5fd8fSJohnny Huang 		printf("  Enable low security key\n");
30469d5fd8fSJohnny Huang 	printf("OTPCFG0-D[6]\n");
305e1f9e54eSJohnny Huang 	if (OTPCFG[0] & IGNORE_SEC_BOOT_HWSTRAP)
30669d5fd8fSJohnny Huang 		printf("  Ignore Secure Boot hardware strap\n");
30769d5fd8fSJohnny Huang 	else
30869d5fd8fSJohnny Huang 		printf("  Do not ignore Secure Boot hardware strap\n");
30969d5fd8fSJohnny Huang 	printf("OTPCFG0-D[7]\n");
310e1f9e54eSJohnny Huang 	if (SEC_BOOT_MDOES(OTPCFG[0]) == SEC_MODE1)
311e1f9e54eSJohnny Huang 		printf("  Secure Boot Mode: 1\n");
312e1f9e54eSJohnny Huang 	else
313e1f9e54eSJohnny Huang 		printf("  Secure Boot Mode: 2\n");
31469d5fd8fSJohnny Huang 	printf("OTPCFG0-D[9:8]\n");
31569d5fd8fSJohnny Huang 	printf("  OTP bit cell mode : ");
316e1f9e54eSJohnny Huang 	tmp = OTP_BIT_CELL_MODES(OTPCFG[0]);
317e1f9e54eSJohnny Huang 	if (tmp == SINGLE_CELL_MODE) {
31869d5fd8fSJohnny Huang 		printf("Single cell mode (recommended)\n");
319e1f9e54eSJohnny Huang 	} else if (tmp == DIFFERENTIAL_MODE) {
32069d5fd8fSJohnny Huang 		printf("Differnetial mode\n");
321e1f9e54eSJohnny Huang 	} else if (tmp == DIFFERENTIAL_REDUDANT_MODE) {
32269d5fd8fSJohnny Huang 		printf("Differential-redundant mode\n");
32369d5fd8fSJohnny Huang 	} else {
32469d5fd8fSJohnny Huang 		printf("Value error\n");
32569d5fd8fSJohnny Huang 		return -1;
32669d5fd8fSJohnny Huang 	}
32769d5fd8fSJohnny Huang 	printf("OTPCFG0-D[11:10]\n");
32869d5fd8fSJohnny Huang 	printf("  RSA mode : ");
329e1f9e54eSJohnny Huang 	tmp = CRYPTO_MODES(OTPCFG[0]);
330e1f9e54eSJohnny Huang 	if (tmp == CRYPTO_RSA1024) {
33169d5fd8fSJohnny Huang 		printf("RSA1024\n");
332e1f9e54eSJohnny Huang 	} else if (tmp == CRYPTO_RSA2048) {
33369d5fd8fSJohnny Huang 		printf("RSA2048\n");
334e1f9e54eSJohnny Huang 	} else if (tmp == CRYPTO_RSA3072) {
33569d5fd8fSJohnny Huang 		printf("RSA3072\n");
33669d5fd8fSJohnny Huang 	} else {
33769d5fd8fSJohnny Huang 		printf("RSA4096\n");
33869d5fd8fSJohnny Huang 	}
33969d5fd8fSJohnny Huang 	printf("OTPCFG0-D[13:12]\n");
34069d5fd8fSJohnny Huang 	printf("  SHA mode : ");
341e1f9e54eSJohnny Huang 	tmp = HASH_MODES(OTPCFG[0]);
342e1f9e54eSJohnny Huang 	if (tmp == HASH_SAH224) {
34369d5fd8fSJohnny Huang 		printf("SHA224\n");
344e1f9e54eSJohnny Huang 	} else if (tmp == HASH_SAH256) {
34569d5fd8fSJohnny Huang 		printf("SHA256\n");
346e1f9e54eSJohnny Huang 	} else if (tmp == HASH_SAH384) {
34769d5fd8fSJohnny Huang 		printf("SHA384\n");
34869d5fd8fSJohnny Huang 	} else {
34969d5fd8fSJohnny Huang 		printf("SHA512\n");
35069d5fd8fSJohnny Huang 	}
35169d5fd8fSJohnny Huang 
35269d5fd8fSJohnny Huang 	printf("OTPCFG0-D[21:16]\n");
353e1f9e54eSJohnny Huang 	printf("  Secure Region size (DW): %x\n", SECREG_SIZE(OTPCFG[0]));
35469d5fd8fSJohnny Huang 
35569d5fd8fSJohnny Huang 	printf("OTPCFG0-D[22]\n");
356e1f9e54eSJohnny Huang 	if (OTPCFG[0] & WRITE_PROTECT_SECREG)
35769d5fd8fSJohnny Huang 		printf("  Secure Region : Write Protect\n");
35869d5fd8fSJohnny Huang 	else
35969d5fd8fSJohnny Huang 		printf("  Secure Region : Writable\n");
36069d5fd8fSJohnny Huang 	printf("OTPCFG0-D[23]\n");
361e1f9e54eSJohnny Huang 	if (OTPCFG[0] & WRITE_PROTECT_USERREG)
36269d5fd8fSJohnny Huang 		printf("  User Region : Write Protect\n");
36369d5fd8fSJohnny Huang 	else
36469d5fd8fSJohnny Huang 		printf("  User Region : Writable\n");
36569d5fd8fSJohnny Huang 	printf("OTPCFG0-D[24]\n");
366e1f9e54eSJohnny Huang 	if (OTPCFG[0] & WRITE_PROTECT_CONFREG)
36769d5fd8fSJohnny Huang 		printf("  Configure Region : Write Protect\n");
36869d5fd8fSJohnny Huang 	else
36969d5fd8fSJohnny Huang 		printf("  Configure Region : Writable\n");
37069d5fd8fSJohnny Huang 	printf("OTPCFG0-D[25]\n");
371e1f9e54eSJohnny Huang 	if (OTPCFG[0] & WRITE_PROTECT_STRAPREG)
37269d5fd8fSJohnny Huang 		printf("  OTP strap Region : Write Protect\n");
37369d5fd8fSJohnny Huang 	else
37469d5fd8fSJohnny Huang 		printf("  OTP strap Region : Writable\n");
37569d5fd8fSJohnny Huang 	printf("OTPCFG0-D[26]\n");
376e1f9e54eSJohnny Huang 	if (OTPCFG[0] & ENABLE_COPY_TO_SRAM)
37769d5fd8fSJohnny Huang 		printf("  Copy Boot Image to Internal SRAM\n");
37869d5fd8fSJohnny Huang 	else
37969d5fd8fSJohnny Huang 		printf("  Disable Copy Boot Image to Internal SRAM\n");
38069d5fd8fSJohnny Huang 	printf("OTPCFG0-D[27]\n");
381e1f9e54eSJohnny Huang 	if (OTPCFG[0] & ENABLE_IMAGE_ENC)
38269d5fd8fSJohnny Huang 		printf("  Enable image encryption\n");
38369d5fd8fSJohnny Huang 	else
38469d5fd8fSJohnny Huang 		printf("  Disable image encryption\n");
38569d5fd8fSJohnny Huang 	printf("OTPCFG0-D[29]\n");
386e1f9e54eSJohnny Huang 	if (OTPCFG[0] & WRITE_PROTECT_KEY_RETIRE)
38769d5fd8fSJohnny Huang 		printf("  OTP key retire Region : Write Protect\n");
38869d5fd8fSJohnny Huang 	else
38969d5fd8fSJohnny Huang 		printf("  OTP key retire Region : Writable\n");
39069d5fd8fSJohnny Huang 	printf("OTPCFG0-D[30]\n");
391e1f9e54eSJohnny Huang 	if (OTPCFG[0] & ENABLE_SIPROM_RED)
39269d5fd8fSJohnny Huang 		printf("  SIPROM RED_EN redundancy repair enable\n");
39369d5fd8fSJohnny Huang 	else
39469d5fd8fSJohnny Huang 		printf("  SIPROM RED_EN redundancy repair disable\n");
39569d5fd8fSJohnny Huang 	printf("OTPCFG0-D[31]\n");
396e1f9e54eSJohnny Huang 	if (OTPCFG[0] & ENABLE_SIPROM_MLOCK)
39769d5fd8fSJohnny Huang 		printf("  SIPROM Mlock memory lock enable\n");
39869d5fd8fSJohnny Huang 	else
39969d5fd8fSJohnny Huang 		printf("  SIPROM Mlock memory lock disable\n");
40069d5fd8fSJohnny Huang 
40169d5fd8fSJohnny Huang 	printf("OTPCFG2-D[15:0]\n");
402e1f9e54eSJohnny Huang 	printf("  Vender ID : %x\n", VENDER_ID(OTPCFG[2]));
40369d5fd8fSJohnny Huang 
40469d5fd8fSJohnny Huang 	printf("OTPCFG2-D[31:16]\n");
405e1f9e54eSJohnny Huang 	printf("  Key Revision : %x\n", KEY_REVISION(OTPCFG[2]));
40669d5fd8fSJohnny Huang 
40769d5fd8fSJohnny Huang 	printf("OTPCFG3-D[15:0]\n");
408e1f9e54eSJohnny Huang 	printf("  Secure boot header offset : %x\n",
409e1f9e54eSJohnny Huang 	       SEC_BOOT_HEADER_OFFSET(OTPCFG[3]));
41069d5fd8fSJohnny Huang 
41169d5fd8fSJohnny Huang 	printf("OTPCFG4-D[7:0]\n");
412e1f9e54eSJohnny Huang 	tmp = KEYS_VALID_BITS(OTPCFG[4]);
413e1f9e54eSJohnny Huang 	if (tmp != 0) {
41469d5fd8fSJohnny Huang 		for (i = 0; i < 7; i++) {
41569d5fd8fSJohnny Huang 			if (tmp == (1 << i)) {
416e1f9e54eSJohnny Huang 				pass = i + 1;
41769d5fd8fSJohnny Huang 			}
41869d5fd8fSJohnny Huang 		}
419e1f9e54eSJohnny Huang 	} else {
420e1f9e54eSJohnny Huang 		pass = 0;
421e1f9e54eSJohnny Huang 	}
422e1f9e54eSJohnny Huang 	printf("  Keys valid  : %d\n", pass);
42369d5fd8fSJohnny Huang 
42469d5fd8fSJohnny Huang 	printf("OTPCFG4-D[23:16]\n");
425e1f9e54eSJohnny Huang 	tmp = KEYS_RETIRE_BITS(OTPCFG[4]);
426e1f9e54eSJohnny Huang 	if (tmp != 0) {
42769d5fd8fSJohnny Huang 		for (i = 0; i < 7; i++) {
42869d5fd8fSJohnny Huang 			if (tmp == (1 << i)) {
429e1f9e54eSJohnny Huang 				pass = i + 1;
43069d5fd8fSJohnny Huang 			}
43169d5fd8fSJohnny Huang 		}
432e1f9e54eSJohnny Huang 	} else {
433e1f9e54eSJohnny Huang 		pass = 0;
43469d5fd8fSJohnny Huang 	}
43569d5fd8fSJohnny Huang 	printf("  Keys Retire ID : %d\n", pass);
43669d5fd8fSJohnny Huang 
43769d5fd8fSJohnny Huang 	printf("OTPCFG5-D[31:0]\n");
43869d5fd8fSJohnny Huang 	printf("  User define data, random number low : %x\n", OTPCFG[5]);
43969d5fd8fSJohnny Huang 
44069d5fd8fSJohnny Huang 	printf("OTPCFG6-D[31:0]\n");
44169d5fd8fSJohnny Huang 	printf("  User define data, random number high : %x\n", OTPCFG[6]);
44269d5fd8fSJohnny Huang 
44369d5fd8fSJohnny Huang 	printf("OTPCFG8-D[31:0]\n");
44469d5fd8fSJohnny Huang 	printf("  Redundancy Repair : %x\n", OTPCFG[8]);
44569d5fd8fSJohnny Huang 
44669d5fd8fSJohnny Huang 	printf("OTPCFG10-D[31:0]\n");
44769d5fd8fSJohnny Huang 	printf("  Manifest ID low : %x\n", OTPCFG[10]);
44869d5fd8fSJohnny Huang 
44969d5fd8fSJohnny Huang 	printf("OTPCFG11-D[31:0]\n");
45069d5fd8fSJohnny Huang 	printf("  Manifest ID high : %x\n", OTPCFG[11]);
45169d5fd8fSJohnny Huang 	return 0;
45269d5fd8fSJohnny Huang 
45369d5fd8fSJohnny Huang }
45469d5fd8fSJohnny Huang 
45569d5fd8fSJohnny Huang static void buf_print(char *buf, int len)
45669d5fd8fSJohnny Huang {
45769d5fd8fSJohnny Huang 	int i;
45869d5fd8fSJohnny Huang 	printf("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
45969d5fd8fSJohnny Huang 	for (i = 0; i < len; i++) {
46069d5fd8fSJohnny Huang 		if (i % 16 == 0) {
46169d5fd8fSJohnny Huang 			printf("%04X: ", i);
46269d5fd8fSJohnny Huang 		}
46369d5fd8fSJohnny Huang 		printf("%02X ", buf[i]);
46469d5fd8fSJohnny Huang 		if ((i + 1) % 16 == 0) {
46569d5fd8fSJohnny Huang 			printf("\n");
46669d5fd8fSJohnny Huang 		}
46769d5fd8fSJohnny Huang 	}
46869d5fd8fSJohnny Huang }
46969d5fd8fSJohnny Huang 
47069d5fd8fSJohnny Huang static int otp_data_parse(uint32_t *buf, int dw_count)
47169d5fd8fSJohnny Huang {
47269d5fd8fSJohnny Huang 	int key_id, key_offset, last, key_type, key_length, exp_length;
47369d5fd8fSJohnny Huang 	char *byte_buf;
47469d5fd8fSJohnny Huang 	int i = 0, len = 0;
47569d5fd8fSJohnny Huang 	byte_buf = (char *)buf;
47669d5fd8fSJohnny Huang 	while (1) {
47769d5fd8fSJohnny Huang 		key_id = buf[i] & 0x7;
47869d5fd8fSJohnny Huang 		key_offset = buf[i] & 0x1ff8;
47969d5fd8fSJohnny Huang 		last = (buf[i] >> 13) & 1;
48069d5fd8fSJohnny Huang 		key_type = (buf[i] >> 14) & 0xf;
48169d5fd8fSJohnny Huang 		key_length = (buf[i] >> 18) & 0x3;
48269d5fd8fSJohnny Huang 		exp_length = (buf[i] >> 20) & 0xfff;
48369d5fd8fSJohnny Huang 		printf("Key[%d]:\n", i);
48469d5fd8fSJohnny Huang 		printf("Key Type: ");
48569d5fd8fSJohnny Huang 		switch (key_type) {
48669d5fd8fSJohnny Huang 		case 0:
48769d5fd8fSJohnny Huang 			printf("AES-256 as OEM platform key for image encryption/decryption\n");
48869d5fd8fSJohnny Huang 			break;
48969d5fd8fSJohnny Huang 		case 1:
49069d5fd8fSJohnny Huang 			printf("AES-256 as secret vault key\n");
49169d5fd8fSJohnny Huang 			break;
49269d5fd8fSJohnny Huang 		case 4:
49369d5fd8fSJohnny Huang 			printf("HMAC as encrypted OEM HMAC keys in Mode 1\n");
49469d5fd8fSJohnny Huang 			break;
49569d5fd8fSJohnny Huang 		case 8:
49669d5fd8fSJohnny Huang 			printf("RSA-public as OEM DSS public keys in Mode 2\n");
49769d5fd8fSJohnny Huang 			break;
49869d5fd8fSJohnny Huang 		case 9:
49969d5fd8fSJohnny Huang 			printf("RSA-public as SOC public key\n");
50069d5fd8fSJohnny Huang 			break;
50169d5fd8fSJohnny Huang 		case 10:
50269d5fd8fSJohnny Huang 			printf("RSA-public as AES key decryption key\n");
50369d5fd8fSJohnny Huang 			break;
50469d5fd8fSJohnny Huang 		case 13:
50569d5fd8fSJohnny Huang 			printf("RSA-private as SOC private key\n");
50669d5fd8fSJohnny Huang 			break;
50769d5fd8fSJohnny Huang 		case 14:
50869d5fd8fSJohnny Huang 			printf("RSA-private as AES key decryption key\n");
50969d5fd8fSJohnny Huang 			break;
51069d5fd8fSJohnny Huang 		default:
51169d5fd8fSJohnny Huang 			printf("key_type error: %x\n", key_type);
51269d5fd8fSJohnny Huang 			return -1;
51369d5fd8fSJohnny Huang 		}
51469d5fd8fSJohnny Huang 		if (key_type == 4) {
51569d5fd8fSJohnny Huang 			printf("HMAC SHA Type: ");
51669d5fd8fSJohnny Huang 			switch (key_length) {
51769d5fd8fSJohnny Huang 			case 0:
51869d5fd8fSJohnny Huang 				printf("HMAC(SHA224)\n");
51969d5fd8fSJohnny Huang 				break;
52069d5fd8fSJohnny Huang 			case 1:
52169d5fd8fSJohnny Huang 				printf("HMAC(SHA256)\n");
52269d5fd8fSJohnny Huang 				break;
52369d5fd8fSJohnny Huang 			case 2:
52469d5fd8fSJohnny Huang 				printf("HMAC(SHA384)\n");
52569d5fd8fSJohnny Huang 				break;
52669d5fd8fSJohnny Huang 			case 3:
52769d5fd8fSJohnny Huang 				printf("HMAC(SHA512)\n");
52869d5fd8fSJohnny Huang 				break;
52969d5fd8fSJohnny Huang 			}
530*cd1610b4SJohnny Huang 		} else if (key_type != 0 && key_type != 1) {
53169d5fd8fSJohnny Huang 			printf("RSA SHA Type: ");
53269d5fd8fSJohnny Huang 			switch (key_length) {
53369d5fd8fSJohnny Huang 			case 0:
53469d5fd8fSJohnny Huang 				printf("RSA1024\n");
53569d5fd8fSJohnny Huang 				len = 0x100;
53669d5fd8fSJohnny Huang 				break;
53769d5fd8fSJohnny Huang 			case 1:
53869d5fd8fSJohnny Huang 				printf("RSA2048\n");
53969d5fd8fSJohnny Huang 				len = 0x200;
54069d5fd8fSJohnny Huang 				break;
54169d5fd8fSJohnny Huang 			case 2:
54269d5fd8fSJohnny Huang 				printf("RSA3072\n");
54369d5fd8fSJohnny Huang 				len = 0x300;
54469d5fd8fSJohnny Huang 				break;
54569d5fd8fSJohnny Huang 			case 3:
54669d5fd8fSJohnny Huang 				printf("RSA4096\n");
54769d5fd8fSJohnny Huang 				len = 0x400;
54869d5fd8fSJohnny Huang 				break;
54969d5fd8fSJohnny Huang 			}
55069d5fd8fSJohnny Huang 			printf("RSA exponent bit length: %d\n", exp_length);
55169d5fd8fSJohnny Huang 		}
55269d5fd8fSJohnny Huang 		if (key_type == 4 || key_type == 8)
55369d5fd8fSJohnny Huang 			printf("Key Number ID: %d\n", key_id);
55469d5fd8fSJohnny Huang 		printf("Key Value:\n");
55569d5fd8fSJohnny Huang 		if (key_type == 4) {
55669d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], 0x40);
55769d5fd8fSJohnny Huang 		} else if (key_type == 0 || key_type == 1) {
55869d5fd8fSJohnny Huang 			printf("AES Key:\n");
55969d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], 0x20);
56069d5fd8fSJohnny Huang 			printf("AES IV:\n");
56169d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset + 0x20], 0x10);
56269d5fd8fSJohnny Huang 
56369d5fd8fSJohnny Huang 		} else {
56469d5fd8fSJohnny Huang 			printf("RSA mod:\n");
56569d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], len / 2);
56669d5fd8fSJohnny Huang 			printf("RSA exp:\n");
56769d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset + (len / 2)], len / 2);
56869d5fd8fSJohnny Huang 		}
56969d5fd8fSJohnny Huang 		if (last)
57069d5fd8fSJohnny Huang 			break;
57169d5fd8fSJohnny Huang 		i++;
57269d5fd8fSJohnny Huang 	}
57369d5fd8fSJohnny Huang 	return 0;
57469d5fd8fSJohnny Huang }
57569d5fd8fSJohnny Huang 
57669d5fd8fSJohnny Huang static int otp_prog_conf(uint32_t *buf, int otp_addr, int dw_count)
57769d5fd8fSJohnny Huang {
57869d5fd8fSJohnny Huang 	int i, j, k, bit_value;
57969d5fd8fSJohnny Huang 	int pass, soak;
58069d5fd8fSJohnny Huang 	uint32_t prog_bit, prog_address;
58169d5fd8fSJohnny Huang 
58269d5fd8fSJohnny Huang 	for (i = 0; i < dw_count; i++) {
5834c1c9b35SJohnny Huang 		printProgress(i + 1, dw_count, "");
58469d5fd8fSJohnny Huang 		prog_address = 0x800;
58569d5fd8fSJohnny Huang 		prog_address |= ((i + otp_addr) / 8) * 0x200;
58669d5fd8fSJohnny Huang 		prog_address |= ((i + otp_addr) % 8) * 0x2;
58769d5fd8fSJohnny Huang 		for (j = 0; j < 32; j++) {
58869d5fd8fSJohnny Huang 			bit_value = (buf[i] >> j) & 0x1;
58969d5fd8fSJohnny Huang 			if (bit_value)
59069d5fd8fSJohnny Huang 				prog_bit = ~(0x1 << j);
59169d5fd8fSJohnny Huang 			else
59269d5fd8fSJohnny Huang 				continue;
59369d5fd8fSJohnny Huang 			pass = 0;
59469d5fd8fSJohnny Huang 			soak = 0;
5954b65a65dSJohnny Huang 			otp_write(0x3000, 0x4061); // Write MRA
5964b65a65dSJohnny Huang 			otp_write(0x5000, 0x302f); // Write MRB
5974b65a65dSJohnny Huang 			otp_write(0x1000, 0x4020); // Write MR
5984b65a65dSJohnny Huang 			writel(0x04190760, 0x1e602008); //normal program
59969d5fd8fSJohnny Huang 			for (k = 0; k < RETRY; k++) {
60069d5fd8fSJohnny Huang 				if (!soak) {
60169d5fd8fSJohnny Huang 					otp_prog(prog_address, prog_bit);
6024c1c9b35SJohnny Huang 					if (prog_conf_verify(prog_address, j, bit_value) == 0) {
60369d5fd8fSJohnny Huang 						pass = 1;
60469d5fd8fSJohnny Huang 						break;
60569d5fd8fSJohnny Huang 					}
60669d5fd8fSJohnny Huang 					soak = 1;
6074b65a65dSJohnny Huang 					otp_write(0x3000, 0x4021); // Write MRA
6084b65a65dSJohnny Huang 					otp_write(0x5000, 0x1027); // Write MRB
6094b65a65dSJohnny Huang 					otp_write(0x1000, 0x4820); // Write MR
61069d5fd8fSJohnny Huang 					writel(0x041930d4, 0x1e602008); //soak program
6114b65a65dSJohnny Huang 				}
61269d5fd8fSJohnny Huang 				otp_prog(prog_address, prog_bit);
6134c1c9b35SJohnny Huang 				if (prog_conf_verify(prog_address, j, bit_value) == 0) {
61469d5fd8fSJohnny Huang 					pass = 1;
61569d5fd8fSJohnny Huang 					break;
61669d5fd8fSJohnny Huang 				}
61769d5fd8fSJohnny Huang 			}
61869d5fd8fSJohnny Huang 			if (!pass)
61969d5fd8fSJohnny Huang 				return -1;
62069d5fd8fSJohnny Huang 		}
62169d5fd8fSJohnny Huang 	}
62269d5fd8fSJohnny Huang 	return 0;
62369d5fd8fSJohnny Huang }
62469d5fd8fSJohnny Huang 
62569d5fd8fSJohnny Huang static void otp_strp_status(struct otpstrap *otpstrap)
62669d5fd8fSJohnny Huang {
62769d5fd8fSJohnny Huang 	uint32_t OTPSTRAP_RAW[2];
62869d5fd8fSJohnny Huang 	int i, j;
62969d5fd8fSJohnny Huang 
63069d5fd8fSJohnny Huang 	for (j = 0; j < 64; j++) {
63169d5fd8fSJohnny Huang 		otpstrap[j].value = 0;
63269d5fd8fSJohnny Huang 		otpstrap[j].remain_times = 7;
63369d5fd8fSJohnny Huang 		otpstrap[j].writeable_option = -1;
63469d5fd8fSJohnny Huang 		otpstrap[j].protected = 0;
63569d5fd8fSJohnny Huang 	}
63669d5fd8fSJohnny Huang 
63769d5fd8fSJohnny Huang 	for (i = 16; i < 30; i += 2) {
63869d5fd8fSJohnny Huang 		int option = (i - 16) / 2;
63969d5fd8fSJohnny Huang 		otp_read_config(i, &OTPSTRAP_RAW[0]);
64069d5fd8fSJohnny Huang 		otp_read_config(i + 1, &OTPSTRAP_RAW[1]);
64169d5fd8fSJohnny Huang 		for (j = 0; j < 32; j++) {
64269d5fd8fSJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[0] >> j) & 0x1);
64369d5fd8fSJohnny Huang 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
64469d5fd8fSJohnny Huang 				otpstrap[j].writeable_option = option;
64569d5fd8fSJohnny Huang 			}
64669d5fd8fSJohnny Huang 			if (bit_value == 1)
64769d5fd8fSJohnny Huang 				otpstrap[j].remain_times --;
64869d5fd8fSJohnny Huang 			otpstrap[j].value ^= bit_value;
64969d5fd8fSJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
65069d5fd8fSJohnny Huang 		}
65169d5fd8fSJohnny Huang 		for (j = 32; j < 64; j++) {
65269d5fd8fSJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1);
65369d5fd8fSJohnny Huang 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
65469d5fd8fSJohnny Huang 				otpstrap[j].writeable_option = option;
65569d5fd8fSJohnny Huang 			}
65669d5fd8fSJohnny Huang 			if (bit_value == 1)
65769d5fd8fSJohnny Huang 				otpstrap[j].remain_times --;
65869d5fd8fSJohnny Huang 			otpstrap[j].value ^= bit_value;
65969d5fd8fSJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
66069d5fd8fSJohnny Huang 		}
66169d5fd8fSJohnny Huang 	}
66269d5fd8fSJohnny Huang 	otp_read_config(30, &OTPSTRAP_RAW[0]);
66369d5fd8fSJohnny Huang 	otp_read_config(31, &OTPSTRAP_RAW[1]);
66469d5fd8fSJohnny Huang 	for (j = 0; j < 32; j++) {
66569d5fd8fSJohnny Huang 		if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
66669d5fd8fSJohnny Huang 			otpstrap[j].protected = 1;
66769d5fd8fSJohnny Huang 	}
66869d5fd8fSJohnny Huang 	for (j = 32; j < 64; j++) {
66969d5fd8fSJohnny Huang 		if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
67069d5fd8fSJohnny Huang 			otpstrap[j].protected = 1;
67169d5fd8fSJohnny Huang 	}
67269d5fd8fSJohnny Huang }
67369d5fd8fSJohnny Huang 
67469d5fd8fSJohnny Huang static int otp_strap_parse(uint32_t *buf)
67569d5fd8fSJohnny Huang {
67669d5fd8fSJohnny Huang 	int i;
67769d5fd8fSJohnny Huang 	uint32_t *strap_keep = buf + 2;
67869d5fd8fSJohnny Huang 	uint32_t *strap_protect = buf + 4;
67969d5fd8fSJohnny Huang 	int bit, pbit, kbit;
68069d5fd8fSJohnny Huang 	int fail = 0;
68169d5fd8fSJohnny Huang 	struct otpstrap otpstrap[64];
68269d5fd8fSJohnny Huang 
68369d5fd8fSJohnny Huang 	otp_strp_status(otpstrap);
68469d5fd8fSJohnny Huang 	for (i = 0; i < 64; i++) {
68569d5fd8fSJohnny Huang 		if (i < 32) {
68669d5fd8fSJohnny Huang 			bit = (buf[0] >> i) & 0x1;
68769d5fd8fSJohnny Huang 			kbit = (strap_keep[0] >> i) & 0x1;
68869d5fd8fSJohnny Huang 			pbit = (strap_protect[0] >> i) & 0x1;
68969d5fd8fSJohnny Huang 		} else {
69069d5fd8fSJohnny Huang 			bit = (buf[1] >> (i - 32)) & 0x1;
69169d5fd8fSJohnny Huang 			kbit = (strap_keep[1] >> (i - 32)) & 0x1;
69269d5fd8fSJohnny Huang 			pbit = (strap_protect[1] >> (i - 32)) & 0x1;
69369d5fd8fSJohnny Huang 		}
69469d5fd8fSJohnny Huang 
69569d5fd8fSJohnny Huang 		if (kbit == 1) {
69669d5fd8fSJohnny Huang 			continue;
69769d5fd8fSJohnny Huang 		} else {
69869d5fd8fSJohnny Huang 			printf("OTPSTRAP[%d]:\n", i);
69969d5fd8fSJohnny Huang 		}
70069d5fd8fSJohnny Huang 		if (bit == otpstrap[i].value) {
70169d5fd8fSJohnny Huang 			printf("    The value is same as before, skip it.\n");
70269d5fd8fSJohnny Huang 			continue;
70369d5fd8fSJohnny Huang 		}
70469d5fd8fSJohnny Huang 		if (otpstrap[i].protected == 1) {
70569d5fd8fSJohnny Huang 			printf("    This bit is protected and is not writable\n");
70669d5fd8fSJohnny Huang 			fail = 1;
70769d5fd8fSJohnny Huang 			continue;
70869d5fd8fSJohnny Huang 		}
70969d5fd8fSJohnny Huang 		if (otpstrap[i].remain_times == 0) {
71069d5fd8fSJohnny Huang 			printf("    This bit is no remaining number of times to write.\n");
71169d5fd8fSJohnny Huang 			fail = 1;
71269d5fd8fSJohnny Huang 			continue;
71369d5fd8fSJohnny Huang 		}
71469d5fd8fSJohnny Huang 		if (pbit == 1) {
71569d5fd8fSJohnny Huang 			printf("    This bit will be protected and become non-writable.\n");
71669d5fd8fSJohnny Huang 		}
717*cd1610b4SJohnny Huang 		printf("    Write 1 to OTPSTRAP[%d] OPTION[%d], that value becomes from %d to %d.\n", i, otpstrap[i].writeable_option + 1, otpstrap[i].value, otpstrap[i].value ^ 1);
71869d5fd8fSJohnny Huang 	}
71969d5fd8fSJohnny Huang 	if (fail == 1)
72069d5fd8fSJohnny Huang 		return -1;
72169d5fd8fSJohnny Huang 	else
72269d5fd8fSJohnny Huang 		return 0;
72369d5fd8fSJohnny Huang }
72469d5fd8fSJohnny Huang 
725*cd1610b4SJohnny Huang static void otp_print_strap(int start, int count)
72669d5fd8fSJohnny Huang {
72769d5fd8fSJohnny Huang 	int i, j;
72869d5fd8fSJohnny Huang 	struct otpstrap otpstrap[64];
72969d5fd8fSJohnny Huang 
73069d5fd8fSJohnny Huang 	otp_strp_status(otpstrap);
73169d5fd8fSJohnny Huang 
732*cd1610b4SJohnny Huang 	for (i = start; i < start + count; i++) {
73369d5fd8fSJohnny Huang 		printf("OTPSTRAP[%d]:\n", i);
73469d5fd8fSJohnny Huang 		printf("  OTP Option value: ");
73569d5fd8fSJohnny Huang 		for (j = 1; j <= 7; j++)
73669d5fd8fSJohnny Huang 			printf("[%d]:%d ", j, otpstrap[i].option_array[j - 1]);
73769d5fd8fSJohnny Huang 		printf("\n");
73869d5fd8fSJohnny Huang 		printf("  OTP Value: %d\n", otpstrap[i].value);
73969d5fd8fSJohnny Huang 		printf("  Status:\n");
74069d5fd8fSJohnny Huang 		if (otpstrap[i].protected == 1) {
74169d5fd8fSJohnny Huang 			printf("    OTPSTRAP[%d] is protected and is not writable\n", i);
74269d5fd8fSJohnny Huang 		} else {
74369d5fd8fSJohnny Huang 			printf("    OTPSTRAP[%d] is not protected ", i);
74469d5fd8fSJohnny Huang 			if (otpstrap[i].remain_times == 0) {
74569d5fd8fSJohnny Huang 				printf("and no remaining number of times to write.\n");
74669d5fd8fSJohnny Huang 			} else {
74769d5fd8fSJohnny Huang 				printf("and still can write %d number of times\n", otpstrap[i].remain_times);
74869d5fd8fSJohnny Huang 			}
74969d5fd8fSJohnny Huang 		}
75069d5fd8fSJohnny Huang 	}
75169d5fd8fSJohnny Huang }
75269d5fd8fSJohnny Huang 
75369d5fd8fSJohnny Huang static int otp_prog_strap(uint32_t *buf)
75469d5fd8fSJohnny Huang {
75569d5fd8fSJohnny Huang 	int i, j;
75669d5fd8fSJohnny Huang 	uint32_t *strap_keep = buf + 2;
75769d5fd8fSJohnny Huang 	uint32_t *strap_protect = buf + 4;
75869d5fd8fSJohnny Huang 	uint32_t prog_bit, prog_address;
75969d5fd8fSJohnny Huang 	int bit, pbit, kbit, offset;
76069d5fd8fSJohnny Huang 	int fail = 0;
76169d5fd8fSJohnny Huang 	int pass, soak;
76269d5fd8fSJohnny Huang 	struct otpstrap otpstrap[64];
76369d5fd8fSJohnny Huang 
76469d5fd8fSJohnny Huang 	otp_strp_status(otpstrap);
76569d5fd8fSJohnny Huang 
76669d5fd8fSJohnny Huang 	otp_write(0x3000, 0x4061); // Write MRA
76769d5fd8fSJohnny Huang 	otp_write(0x5000, 0x302f); // Write MRB
76869d5fd8fSJohnny Huang 	otp_write(0x1000, 0x4020); // Write MR
76969d5fd8fSJohnny Huang 	for (i = 0; i < 64; i++) {
7704c1c9b35SJohnny Huang 		printProgress(i + 1, 64, "");
77169d5fd8fSJohnny Huang 		prog_address = 0x800;
77269d5fd8fSJohnny Huang 		if (i < 32) {
77369d5fd8fSJohnny Huang 			offset = i;
77469d5fd8fSJohnny Huang 			bit = (buf[0] >> offset) & 0x1;
77569d5fd8fSJohnny Huang 			kbit = (strap_keep[0] >> offset) & 0x1;
77669d5fd8fSJohnny Huang 			pbit = (strap_protect[0] >> offset) & 0x1;
77769d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200;
77869d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2;
77969d5fd8fSJohnny Huang 
78069d5fd8fSJohnny Huang 		} else {
78169d5fd8fSJohnny Huang 			offset = (i - 32);
78269d5fd8fSJohnny Huang 			bit = (buf[1] >> offset) & 0x1;
78369d5fd8fSJohnny Huang 			kbit = (strap_keep[1] >> offset) & 0x1;
78469d5fd8fSJohnny Huang 			pbit = (strap_protect[1] >> offset) & 0x1;
78569d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200;
78669d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2;
78769d5fd8fSJohnny Huang 		}
78869d5fd8fSJohnny Huang 		prog_bit = ~(0x1 << offset);
78969d5fd8fSJohnny Huang 
79069d5fd8fSJohnny Huang 		if (kbit == 1) {
79169d5fd8fSJohnny Huang 			continue;
79269d5fd8fSJohnny Huang 		}
79369d5fd8fSJohnny Huang 		if (bit == otpstrap[i].value) {
79469d5fd8fSJohnny Huang 			continue;
79569d5fd8fSJohnny Huang 		}
79669d5fd8fSJohnny Huang 		if (otpstrap[i].protected == 1) {
79769d5fd8fSJohnny Huang 			fail = 1;
79869d5fd8fSJohnny Huang 			continue;
79969d5fd8fSJohnny Huang 		}
80069d5fd8fSJohnny Huang 		if (otpstrap[i].remain_times == 0) {
80169d5fd8fSJohnny Huang 			fail = 1;
80269d5fd8fSJohnny Huang 			continue;
80369d5fd8fSJohnny Huang 		}
80469d5fd8fSJohnny Huang 		pass = 0;
80569d5fd8fSJohnny Huang 		soak = 0;
8064b65a65dSJohnny Huang 		otp_write(0x3000, 0x4061); // Write MRA
8074b65a65dSJohnny Huang 		otp_write(0x5000, 0x302f); // Write MRB
8084b65a65dSJohnny Huang 		otp_write(0x1000, 0x4020); // Write MR
8094b65a65dSJohnny Huang 		writel(0x04190760, 0x1e602008); //normal program
81069d5fd8fSJohnny Huang 		for (j = 0; j < RETRY; j++) {
81169d5fd8fSJohnny Huang 			if (!soak) {
81269d5fd8fSJohnny Huang 				otp_prog(prog_address, prog_bit);
8134c1c9b35SJohnny Huang 				if (prog_conf_verify(prog_address, offset, 1) == 0) {
81469d5fd8fSJohnny Huang 					pass = 1;
81569d5fd8fSJohnny Huang 					break;
81669d5fd8fSJohnny Huang 				}
81769d5fd8fSJohnny Huang 				soak = 1;
8184b65a65dSJohnny Huang 				otp_write(0x3000, 0x4021); // Write MRA
8194b65a65dSJohnny Huang 				otp_write(0x5000, 0x1027); // Write MRB
8204b65a65dSJohnny Huang 				otp_write(0x1000, 0x4820); // Write MR
82169d5fd8fSJohnny Huang 				writel(0x041930d4, 0x1e602008); //soak program
8224b65a65dSJohnny Huang 			}
82369d5fd8fSJohnny Huang 			otp_prog(prog_address, prog_bit);
8244c1c9b35SJohnny Huang 			if (prog_conf_verify(prog_address, offset, 1) == 0) {
82569d5fd8fSJohnny Huang 				pass = 1;
82669d5fd8fSJohnny Huang 				break;
82769d5fd8fSJohnny Huang 			}
82869d5fd8fSJohnny Huang 		}
82969d5fd8fSJohnny Huang 		if (!pass)
83069d5fd8fSJohnny Huang 			return -1;
83169d5fd8fSJohnny Huang 
83269d5fd8fSJohnny Huang 		if (pbit == 0)
83369d5fd8fSJohnny Huang 			continue;
83469d5fd8fSJohnny Huang 		prog_address = 0x800;
83569d5fd8fSJohnny Huang 		if (i < 32)
83669d5fd8fSJohnny Huang 			prog_address |= 0x60c;
83769d5fd8fSJohnny Huang 		else
83869d5fd8fSJohnny Huang 			prog_address |= 0x60e;
83969d5fd8fSJohnny Huang 
84069d5fd8fSJohnny Huang 		for (j = 0; j < RETRY; j++) {
84169d5fd8fSJohnny Huang 			if (!soak) {
84269d5fd8fSJohnny Huang 				writel(0x04190760, 0x1e602008); //normal program
84369d5fd8fSJohnny Huang 				otp_prog(prog_address, prog_bit);
8444c1c9b35SJohnny Huang 				if (prog_conf_verify(prog_address, offset, 1) == 0) {
84569d5fd8fSJohnny Huang 					pass = 1;
84669d5fd8fSJohnny Huang 					break;
84769d5fd8fSJohnny Huang 				}
84869d5fd8fSJohnny Huang 				soak = 1;
84969d5fd8fSJohnny Huang 			}
85069d5fd8fSJohnny Huang 			writel(0x041930d4, 0x1e602008); //soak program
85169d5fd8fSJohnny Huang 			otp_prog(prog_address, prog_bit);
8524c1c9b35SJohnny Huang 			if (prog_conf_verify(prog_address, offset, 1) == 0) {
85369d5fd8fSJohnny Huang 				pass = 1;
85469d5fd8fSJohnny Huang 				break;
85569d5fd8fSJohnny Huang 			}
85669d5fd8fSJohnny Huang 		}
85769d5fd8fSJohnny Huang 		if (!pass)
85869d5fd8fSJohnny Huang 			return -1;
85969d5fd8fSJohnny Huang 
86069d5fd8fSJohnny Huang 	}
86169d5fd8fSJohnny Huang 	if (fail == 1)
86269d5fd8fSJohnny Huang 		return -1;
86369d5fd8fSJohnny Huang 	else
86469d5fd8fSJohnny Huang 		return 0;
86569d5fd8fSJohnny Huang 
86669d5fd8fSJohnny Huang }
86769d5fd8fSJohnny Huang 
8684c1c9b35SJohnny Huang static void otp_prog_dw(uint32_t value, uint32_t prog_address, int soak)
86969d5fd8fSJohnny Huang {
8704c1c9b35SJohnny Huang 	int j, bit_value, prog_bit;
87169d5fd8fSJohnny Huang 
8724c1c9b35SJohnny Huang 	if (soak) {
8734c1c9b35SJohnny Huang 		otp_write(0x3000, 0x4021); // Write MRA
8744c1c9b35SJohnny Huang 		otp_write(0x5000, 0x1027); // Write MRB
8754c1c9b35SJohnny Huang 		otp_write(0x1000, 0x4820); // Write MR
8764c1c9b35SJohnny Huang 		writel(0x041930d4, 0x1e602008); //soak program
8774c1c9b35SJohnny Huang 	} else {
8784c1c9b35SJohnny Huang 		otp_write(0x3000, 0x4061); // Write MRA
8794c1c9b35SJohnny Huang 		otp_write(0x5000, 0x302f); // Write MRB
8804c1c9b35SJohnny Huang 		otp_write(0x1000, 0x4020); // Write MR
8814c1c9b35SJohnny Huang 		writel(0x04190760, 0x1e602008); //normal program
8824c1c9b35SJohnny Huang 	}
8834c1c9b35SJohnny Huang 
88469d5fd8fSJohnny Huang 	for (j = 0; j < 32; j++) {
8854c1c9b35SJohnny Huang 		bit_value = (value >> j) & 0x1;
88669d5fd8fSJohnny Huang 		if (prog_address % 2 == 0) {
88769d5fd8fSJohnny Huang 			if (bit_value)
88869d5fd8fSJohnny Huang 				prog_bit = ~(0x1 << j);
88969d5fd8fSJohnny Huang 			else
89069d5fd8fSJohnny Huang 				continue;
89169d5fd8fSJohnny Huang 		} else {
89269d5fd8fSJohnny Huang 			prog_address |= 1 << 15;
89369d5fd8fSJohnny Huang 			if (bit_value)
89469d5fd8fSJohnny Huang 				continue;
89569d5fd8fSJohnny Huang 			else
89669d5fd8fSJohnny Huang 				prog_bit = 0x1 << j;
89769d5fd8fSJohnny Huang 		}
8984c1c9b35SJohnny Huang 		otp_prog(prog_address, prog_bit);
8994c1c9b35SJohnny Huang 	}
9004c1c9b35SJohnny Huang }
9014c1c9b35SJohnny Huang 
902*cd1610b4SJohnny Huang static void otp_prog_bit(uint32_t value, uint32_t prog_address, uint32_t bit_offset, int soak)
903*cd1610b4SJohnny Huang {
904*cd1610b4SJohnny Huang 	int prog_bit;
905*cd1610b4SJohnny Huang 
906*cd1610b4SJohnny Huang 	if (soak) {
907*cd1610b4SJohnny Huang 		otp_write(0x3000, 0x4021); // Write MRA
908*cd1610b4SJohnny Huang 		otp_write(0x5000, 0x1027); // Write MRB
909*cd1610b4SJohnny Huang 		otp_write(0x1000, 0x4820); // Write MR
910*cd1610b4SJohnny Huang 		writel(0x041930d4, 0x1e602008); //soak program
911*cd1610b4SJohnny Huang 	} else {
912*cd1610b4SJohnny Huang 		otp_write(0x3000, 0x4061); // Write MRA
913*cd1610b4SJohnny Huang 		otp_write(0x5000, 0x302f); // Write MRB
914*cd1610b4SJohnny Huang 		otp_write(0x1000, 0x4020); // Write MR
915*cd1610b4SJohnny Huang 		writel(0x04190760, 0x1e602008); //normal program
916*cd1610b4SJohnny Huang 	}
917*cd1610b4SJohnny Huang 	if (prog_address % 2 == 0) {
918*cd1610b4SJohnny Huang 		if (value)
919*cd1610b4SJohnny Huang 			prog_bit = ~(0x1 << bit_offset);
920*cd1610b4SJohnny Huang 		else
921*cd1610b4SJohnny Huang 			return;
922*cd1610b4SJohnny Huang 	} else {
923*cd1610b4SJohnny Huang 		prog_address |= 1 << 15;
924*cd1610b4SJohnny Huang 		if (!value)
925*cd1610b4SJohnny Huang 			prog_bit = 0x1 << bit_offset;
926*cd1610b4SJohnny Huang 		else
927*cd1610b4SJohnny Huang 			return;
928*cd1610b4SJohnny Huang 	}
929*cd1610b4SJohnny Huang 	otp_prog(prog_address, prog_bit);
930*cd1610b4SJohnny Huang }
931*cd1610b4SJohnny Huang 
9324c1c9b35SJohnny Huang static int otp_prog_data(uint32_t *buf, int otp_addr, int dw_count)
9334c1c9b35SJohnny Huang {
9344c1c9b35SJohnny Huang 	int i, k;
9354c1c9b35SJohnny Huang 	int pass;
9364c1c9b35SJohnny Huang 	int addr_odd, count_odd;
9374c1c9b35SJohnny Huang 	int d_size;
9384c1c9b35SJohnny Huang 	uint32_t prog_address;
9394c1c9b35SJohnny Huang 	uint32_t *data;
9404c1c9b35SJohnny Huang 	uint32_t compare[2];
9414c1c9b35SJohnny Huang 
9424c1c9b35SJohnny Huang 	count_odd = dw_count % 2;
9434c1c9b35SJohnny Huang 	addr_odd = otp_addr % 2;
9444c1c9b35SJohnny Huang 	d_size = dw_count + (count_odd ^ addr_odd);
9454c1c9b35SJohnny Huang 	data = malloc(d_size * 4);
9464c1c9b35SJohnny Huang 
9474c1c9b35SJohnny Huang 	printf("Read OTP Data:\n");
9484c1c9b35SJohnny Huang 
9494c1c9b35SJohnny Huang 	printProgress(0, d_size, "");
9504c1c9b35SJohnny Huang 	if (!addr_odd) {
9514c1c9b35SJohnny Huang 		for (i = 0; i < d_size ; i += 2) {
9524c1c9b35SJohnny Huang 			printProgress(i + 2, d_size, "");
9534c1c9b35SJohnny Huang 			otp_read_data(i + otp_addr, &data[i]);
9544c1c9b35SJohnny Huang 		}
9554c1c9b35SJohnny Huang 	} else {
9564c1c9b35SJohnny Huang 		otp_read_data(otp_addr - 1, &data[0]);
9574c1c9b35SJohnny Huang 		data[0] = data[1];
9584c1c9b35SJohnny Huang 		for (i = 1; i < d_size ; i += 2) {
9594c1c9b35SJohnny Huang 			printProgress(i + 2, d_size, "");
9604c1c9b35SJohnny Huang 			otp_read_data(i + otp_addr, &data[i]);
9614c1c9b35SJohnny Huang 		}
9624c1c9b35SJohnny Huang 	}
9634c1c9b35SJohnny Huang 
9644c1c9b35SJohnny Huang 	printf("Check writable...\n");
9654c1c9b35SJohnny Huang 	for (i = 0; i < dw_count; i++) {
9664c1c9b35SJohnny Huang 		if (data[i] == buf[i])
9674c1c9b35SJohnny Huang 			continue;
9684c1c9b35SJohnny Huang 		if ((i + otp_addr) % 2 == 0) {
9694c1c9b35SJohnny Huang 			if ((data[i] | buf[i]) == buf[i]) {
9704c1c9b35SJohnny Huang 				continue;
9714c1c9b35SJohnny Huang 			} else {
9724c1c9b35SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
9734c1c9b35SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i + otp_addr, data[i]);
9744c1c9b35SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
9754c1c9b35SJohnny Huang 				goto fail;
9764c1c9b35SJohnny Huang 			}
9774c1c9b35SJohnny Huang 		} else {
9784c1c9b35SJohnny Huang 			if ((data[i] & buf[i]) == buf[i]) {
9794c1c9b35SJohnny Huang 				continue;
9804c1c9b35SJohnny Huang 			} else {
9814c1c9b35SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
9824c1c9b35SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i + otp_addr, data[i]);
9834c1c9b35SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
9844c1c9b35SJohnny Huang 				goto fail;
9854c1c9b35SJohnny Huang 			}
9864c1c9b35SJohnny Huang 		}
9874c1c9b35SJohnny Huang 	}
9884c1c9b35SJohnny Huang 
989*cd1610b4SJohnny Huang #if 1
9904c1c9b35SJohnny Huang 	printf("Start Programing...\n");
9914c1c9b35SJohnny Huang 	printProgress(0, dw_count, "");
9924c1c9b35SJohnny Huang 	if (addr_odd) {
9934c1c9b35SJohnny Huang 		if (data[0] != buf[0]) {
99469d5fd8fSJohnny Huang 			pass = 0;
9954c1c9b35SJohnny Huang 			printProgress(1, dw_count, "[%08X] = %08X                   ", otp_addr, buf[0]);
9964c1c9b35SJohnny Huang 			otp_prog_dw(buf[0], otp_addr, 0);
99769d5fd8fSJohnny Huang 			for (k = 0; k < RETRY; k++) {
9984c1c9b35SJohnny Huang 				if (prog_data_dw_verify(otp_addr, &buf[0], compare, 1) != 0) {
9994c1c9b35SJohnny Huang 					otp_prog_dw(compare[0], otp_addr, 1);
10004c1c9b35SJohnny Huang 				} else {
100169d5fd8fSJohnny Huang 					pass = 1;
100269d5fd8fSJohnny Huang 					break;
100369d5fd8fSJohnny Huang 				}
100469d5fd8fSJohnny Huang 			}
100569d5fd8fSJohnny Huang 			if (!pass)
10064c1c9b35SJohnny Huang 				goto fail;
10074c1c9b35SJohnny Huang 		} else {
10084c1c9b35SJohnny Huang 			printProgress(1, dw_count, "[%08X] = %08X HIT               ", otp_addr, buf[0]);
100969d5fd8fSJohnny Huang 		}
101069d5fd8fSJohnny Huang 	}
10114c1c9b35SJohnny Huang 	/* if otp_addr is odd, then begin from 1. */
10124c1c9b35SJohnny Huang 	/* if dw_count is odd, then program the last dw seperately. */
10134c1c9b35SJohnny Huang 	for (i = 0 + addr_odd; i < dw_count - (addr_odd ^ count_odd); i += 2) {
10144c1c9b35SJohnny Huang 		prog_address = i + otp_addr;
10154c1c9b35SJohnny Huang 
10164c1c9b35SJohnny Huang 		if ((data[i] == buf[i]) && (data[i + 1] == buf[i + 1])) {
10174c1c9b35SJohnny Huang 			printProgress(i + 2, dw_count, "[%03X]=%08X HIT;[%03X]=%08X HIT", prog_address, buf[i], prog_address + 1, buf[i + 1]);
10184c1c9b35SJohnny Huang 			continue;
10194c1c9b35SJohnny Huang 		}
10204c1c9b35SJohnny Huang 		if (data[i + 1] == buf[i + 1]) {
10214c1c9b35SJohnny Huang 			printProgress(i + 2, dw_count, "[%03X]=%08X    ;[%03X]=%08X HIT", prog_address, buf[i], prog_address + 1, buf[i + 1]);
10224c1c9b35SJohnny Huang 			otp_prog_dw(buf[i], prog_address, 0);
10234c1c9b35SJohnny Huang 		} else if (data[i] == buf[i]) {
10244c1c9b35SJohnny Huang 			printProgress(i + 2, dw_count, "[%03X]=%08X HIT;[%03X]=%08X    ", prog_address, buf[i], prog_address + 1, buf[i + 1]);
10254c1c9b35SJohnny Huang 			otp_prog_dw(buf[i + 1], prog_address + 1, 0);
10264c1c9b35SJohnny Huang 		} else {
10274c1c9b35SJohnny Huang 			printProgress(i + 2, dw_count, "[%03X]=%08X    ;[%03X]=%08X    ", prog_address, buf[i], prog_address + 1, buf[i + 1]);
10284c1c9b35SJohnny Huang 			otp_prog_dw(buf[i], prog_address, 0);
10294c1c9b35SJohnny Huang 			otp_prog_dw(buf[i + 1], prog_address + 1, 0);
10304c1c9b35SJohnny Huang 		}
10314c1c9b35SJohnny Huang 
10324c1c9b35SJohnny Huang 		pass = 0;
10334c1c9b35SJohnny Huang 		for (k = 0; k < RETRY; k++) {
10344c1c9b35SJohnny Huang 			if (prog_data_dw_verify(prog_address, &buf[i], compare, 2) != 0) {
10354c1c9b35SJohnny Huang 				if (compare[0] != 0) {
10364c1c9b35SJohnny Huang 					otp_prog_dw(compare[0], prog_address, 1);
10374c1c9b35SJohnny Huang 				}
10384c1c9b35SJohnny Huang 				if (compare[1] != ~0) {
10394c1c9b35SJohnny Huang 					otp_prog_dw(compare[1], prog_address + 1, 1);
10404c1c9b35SJohnny Huang 				}
10414c1c9b35SJohnny Huang 			} else {
10424c1c9b35SJohnny Huang 				pass = 1;
10434c1c9b35SJohnny Huang 				break;
10444c1c9b35SJohnny Huang 			}
10454c1c9b35SJohnny Huang 		}
10464c1c9b35SJohnny Huang 
10474c1c9b35SJohnny Huang 		if (!pass)
10484c1c9b35SJohnny Huang 			goto fail;
10494c1c9b35SJohnny Huang 	}
10504c1c9b35SJohnny Huang 
10514c1c9b35SJohnny Huang 	if (!(addr_odd ^ count_odd))
10524c1c9b35SJohnny Huang 		goto pass;
10534c1c9b35SJohnny Huang 
10544c1c9b35SJohnny Huang 	prog_address = otp_addr + dw_count - 1;
10554c1c9b35SJohnny Huang 	if (data[dw_count - 1] != buf[dw_count - 1]) {
10564c1c9b35SJohnny Huang 		printProgress(dw_count, dw_count, "[%08X] = %08X                   ", prog_address, buf[dw_count - 1]);
10574c1c9b35SJohnny Huang 		otp_prog_dw(buf[dw_count - 1], prog_address, 0);
10584c1c9b35SJohnny Huang 		for (k = 0; k < RETRY; k++) {
10594c1c9b35SJohnny Huang 			if (prog_data_dw_verify(prog_address, &buf[dw_count - 1], compare, 1) != 0) {
10604c1c9b35SJohnny Huang 				otp_prog_dw(compare[0], prog_address, 1);
10614c1c9b35SJohnny Huang 			} else {
10624c1c9b35SJohnny Huang 				pass = 1;
10634c1c9b35SJohnny Huang 				break;
10644c1c9b35SJohnny Huang 			}
10654c1c9b35SJohnny Huang 		}
10664c1c9b35SJohnny Huang 		if (!pass)
10674c1c9b35SJohnny Huang 			goto fail;
10684c1c9b35SJohnny Huang 	} else {
10694c1c9b35SJohnny Huang 		printProgress(dw_count, dw_count, "[%08X] = %08X HIT               ", prog_address, buf[dw_count - 1]);
10704c1c9b35SJohnny Huang 	}
1071*cd1610b4SJohnny Huang #endif
10724c1c9b35SJohnny Huang pass:
10734c1c9b35SJohnny Huang 	free(data);
107469d5fd8fSJohnny Huang 	return 0;
10754c1c9b35SJohnny Huang 
10764c1c9b35SJohnny Huang fail:
10774c1c9b35SJohnny Huang 	free(data);
10784c1c9b35SJohnny Huang 	return -1;
107969d5fd8fSJohnny Huang }
108069d5fd8fSJohnny Huang 
108169d5fd8fSJohnny Huang static int do_otp_prog(int mode, int addr, int otp_addr, int dw_count, int nconfirm)
108269d5fd8fSJohnny Huang {
108369d5fd8fSJohnny Huang 	int ret;
108469d5fd8fSJohnny Huang 	uint32_t *buf;
108569d5fd8fSJohnny Huang 
108669d5fd8fSJohnny Huang 	buf = map_physmem(addr, dw_count * 4, MAP_WRBACK);
108769d5fd8fSJohnny Huang 	if (!buf) {
108869d5fd8fSJohnny Huang 		puts("Failed to map physical memory\n");
108969d5fd8fSJohnny Huang 		return 1;
109069d5fd8fSJohnny Huang 	}
109169d5fd8fSJohnny Huang 	if (!nconfirm) {
109269d5fd8fSJohnny Huang 		if (mode == MODE_CONF) {
109369d5fd8fSJohnny Huang 			if (otp_conf_parse(buf) < 0) {
109469d5fd8fSJohnny Huang 				printf("OTP config error, please check.\n");
109569d5fd8fSJohnny Huang 				return -1;
109669d5fd8fSJohnny Huang 			}
109769d5fd8fSJohnny Huang 		} else if (mode == MODE_DATA) {
109869d5fd8fSJohnny Huang 			if (otp_data_parse(buf, dw_count) < 0) {
109969d5fd8fSJohnny Huang 				printf("OTP data error, please check.\n");
110069d5fd8fSJohnny Huang 				return -1;
110169d5fd8fSJohnny Huang 			}
110269d5fd8fSJohnny Huang 		} else if (mode == MODE_STRAP) {
110369d5fd8fSJohnny Huang 			if (otp_strap_parse(buf) < 0) {
110469d5fd8fSJohnny Huang 				printf("OTP strap error, please check.\n");
110569d5fd8fSJohnny Huang 				return -1;
110669d5fd8fSJohnny Huang 			}
110769d5fd8fSJohnny Huang 		} else if (mode == MODE_ALL) {
110869d5fd8fSJohnny Huang 			if (otp_conf_parse(buf) < 0) {
110969d5fd8fSJohnny Huang 				printf("OTP config error, please check.\n");
111069d5fd8fSJohnny Huang 				return -1;
111169d5fd8fSJohnny Huang 			}
111269d5fd8fSJohnny Huang 			if (otp_strap_parse(&buf[12]) < 0) {
111369d5fd8fSJohnny Huang 				printf("OTP strap error, please check.\n");
111469d5fd8fSJohnny Huang 				return -1;
111569d5fd8fSJohnny Huang 			}
111669d5fd8fSJohnny Huang 			if (otp_data_parse(&buf[18], dw_count - 18) < 0) {
111769d5fd8fSJohnny Huang 				printf("OTP data error, please check.\n");
111869d5fd8fSJohnny Huang 				return -1;
111969d5fd8fSJohnny Huang 			}
112069d5fd8fSJohnny Huang 		}
112169d5fd8fSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
112269d5fd8fSJohnny Huang 		if (!confirm_yesno()) {
112369d5fd8fSJohnny Huang 			printf(" Aborting\n");
112469d5fd8fSJohnny Huang 			return 1;
112569d5fd8fSJohnny Huang 		}
112669d5fd8fSJohnny Huang 	}
112769d5fd8fSJohnny Huang 	if (mode == MODE_CONF) {
112869d5fd8fSJohnny Huang 		return otp_prog_conf(buf, otp_addr, dw_count);
112969d5fd8fSJohnny Huang 	} else if (mode == MODE_STRAP) {
113069d5fd8fSJohnny Huang 		return otp_prog_strap(buf);
113169d5fd8fSJohnny Huang 	} else if (mode == MODE_DATA) {
113269d5fd8fSJohnny Huang 		return otp_prog_data(buf, otp_addr, dw_count);
113369d5fd8fSJohnny Huang 	} else if (mode == MODE_ALL) {
113469d5fd8fSJohnny Huang 		printf("programing data region ... ");
113569d5fd8fSJohnny Huang 		ret = otp_prog_data(&buf[16], 0, dw_count - 18);
113669d5fd8fSJohnny Huang 		if (ret < 0) {
113769d5fd8fSJohnny Huang 			printf("Error\n");
113869d5fd8fSJohnny Huang 			return ret;
113969d5fd8fSJohnny Huang 		} else {
114069d5fd8fSJohnny Huang 			printf("Done\n");
114169d5fd8fSJohnny Huang 		}
114269d5fd8fSJohnny Huang 		printf("programing strap region ... ");
114369d5fd8fSJohnny Huang 		ret = otp_prog_strap(&buf[12]);
114469d5fd8fSJohnny Huang 		if (ret < 0) {
114569d5fd8fSJohnny Huang 			printf("Error\n");
114669d5fd8fSJohnny Huang 			return ret;
114769d5fd8fSJohnny Huang 		} else {
114869d5fd8fSJohnny Huang 			printf("Done\n");
114969d5fd8fSJohnny Huang 		}
115069d5fd8fSJohnny Huang 		printf("programing configuration region ... ");
115169d5fd8fSJohnny Huang 		ret = otp_prog_conf(buf, 0, 12);
115269d5fd8fSJohnny Huang 		if (ret < 0) {
115369d5fd8fSJohnny Huang 			printf("Error\n");
115469d5fd8fSJohnny Huang 			return ret;
115569d5fd8fSJohnny Huang 		}
115669d5fd8fSJohnny Huang 		printf("Done\n");
115769d5fd8fSJohnny Huang 		return ret;
115869d5fd8fSJohnny Huang 	}
115969d5fd8fSJohnny Huang 	return 0;
116069d5fd8fSJohnny Huang }
1161*cd1610b4SJohnny Huang 
1162*cd1610b4SJohnny Huang static int do_otp_prog_bit(int mode, int otp_dw_offset, int bit_offset, int value, int protect, int nconfirm)
1163*cd1610b4SJohnny Huang {
1164*cd1610b4SJohnny Huang 	uint32_t ret[2];
1165*cd1610b4SJohnny Huang 	uint32_t strap_buf[6];
1166*cd1610b4SJohnny Huang 	uint32_t prog_address;
1167*cd1610b4SJohnny Huang 	struct otpstrap otpstrap[64];
1168*cd1610b4SJohnny Huang 	int otp_bit;
1169*cd1610b4SJohnny Huang 	int i;
1170*cd1610b4SJohnny Huang 	int pass;
1171*cd1610b4SJohnny Huang 
1172*cd1610b4SJohnny Huang 	switch (mode) {
1173*cd1610b4SJohnny Huang 	case MODE_CONF:
1174*cd1610b4SJohnny Huang 		otp_read_config(otp_dw_offset, ret);
1175*cd1610b4SJohnny Huang 		prog_address = 0x800;
1176*cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset / 8) * 0x200;
1177*cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset % 8) * 0x2;
1178*cd1610b4SJohnny Huang 		otp_bit = (ret[0] >> bit_offset) & 0x1;
1179*cd1610b4SJohnny Huang 		if (otp_bit == value) {
1180*cd1610b4SJohnny Huang 			printf("OTPCFG%X[%d] = %d\n", otp_dw_offset, bit_offset, value);
1181*cd1610b4SJohnny Huang 			printf("No need to program\n");
1182*cd1610b4SJohnny Huang 			return 0;
1183*cd1610b4SJohnny Huang 		}
1184*cd1610b4SJohnny Huang 		if (otp_bit == 1 && value == 0) {
1185*cd1610b4SJohnny Huang 			printf("OTPCFG%X[%d] = 1\n", otp_dw_offset, bit_offset);
1186*cd1610b4SJohnny Huang 			printf("OTP is programed, which can't be clean\n");
1187*cd1610b4SJohnny Huang 			return -1;
1188*cd1610b4SJohnny Huang 		}
1189*cd1610b4SJohnny Huang 		printf("Program OTPCFG%X[%d] to 1\n", otp_dw_offset, bit_offset);
1190*cd1610b4SJohnny Huang 		break;
1191*cd1610b4SJohnny Huang 	case MODE_DATA:
1192*cd1610b4SJohnny Huang 		prog_address = otp_dw_offset;
1193*cd1610b4SJohnny Huang 
1194*cd1610b4SJohnny Huang 		if (otp_dw_offset % 2 == 0) {
1195*cd1610b4SJohnny Huang 			otp_read_data(otp_dw_offset, ret);
1196*cd1610b4SJohnny Huang 			otp_bit = (ret[0] >> bit_offset) & 0x1;
1197*cd1610b4SJohnny Huang 		} else {
1198*cd1610b4SJohnny Huang 			otp_read_data(otp_dw_offset - 1, ret);
1199*cd1610b4SJohnny Huang 			otp_bit = (ret[1] >> bit_offset) & 0x1;
1200*cd1610b4SJohnny Huang 		}
1201*cd1610b4SJohnny Huang 		if (otp_bit == value) {
1202*cd1610b4SJohnny Huang 			printf("OTPDATA%X[%d] = %d\n", otp_dw_offset, bit_offset, value);
1203*cd1610b4SJohnny Huang 			printf("No need to program\n");
1204*cd1610b4SJohnny Huang 			return 0;
1205*cd1610b4SJohnny Huang 		}
1206*cd1610b4SJohnny Huang 		if (otp_bit == 1 && value == 0) {
1207*cd1610b4SJohnny Huang 			printf("OTPDATA%X[%d] = 1\n", otp_dw_offset, bit_offset);
1208*cd1610b4SJohnny Huang 			printf("OTP is programed, which can't be clean\n");
1209*cd1610b4SJohnny Huang 			return -1;
1210*cd1610b4SJohnny Huang 		}
1211*cd1610b4SJohnny Huang 		printf("Program OTPDATA%X[%d] to 1\n", otp_dw_offset, bit_offset);
1212*cd1610b4SJohnny Huang 		break;
1213*cd1610b4SJohnny Huang 	case MODE_STRAP:
1214*cd1610b4SJohnny Huang 		otp_strp_status(otpstrap);
1215*cd1610b4SJohnny Huang 		otp_print_strap(bit_offset, 1);
1216*cd1610b4SJohnny Huang 		if (bit_offset < 32) {
1217*cd1610b4SJohnny Huang 			strap_buf[0] = value << bit_offset;
1218*cd1610b4SJohnny Huang 			strap_buf[2] = ~BIT(bit_offset);
1219*cd1610b4SJohnny Huang 			strap_buf[3] = ~0;
1220*cd1610b4SJohnny Huang 			strap_buf[5] = 0;
1221*cd1610b4SJohnny Huang 			if (protect)
1222*cd1610b4SJohnny Huang 				strap_buf[4] = BIT(bit_offset);
1223*cd1610b4SJohnny Huang 			else
1224*cd1610b4SJohnny Huang 				strap_buf[4] = 0;
1225*cd1610b4SJohnny Huang 		} else {
1226*cd1610b4SJohnny Huang 			strap_buf[1] = value << (bit_offset - 32);
1227*cd1610b4SJohnny Huang 			strap_buf[2] = ~0;
1228*cd1610b4SJohnny Huang 			strap_buf[3] = ~BIT(bit_offset - 32);
1229*cd1610b4SJohnny Huang 			strap_buf[4] = 0;
1230*cd1610b4SJohnny Huang 			if (protect)
1231*cd1610b4SJohnny Huang 				strap_buf[5] = BIT(bit_offset - 32);
1232*cd1610b4SJohnny Huang 			else
1233*cd1610b4SJohnny Huang 				strap_buf[5] = 0;
1234*cd1610b4SJohnny Huang 		}
1235*cd1610b4SJohnny Huang 		if (otp_strap_parse(strap_buf) < 0)
1236*cd1610b4SJohnny Huang 			return -1;
1237*cd1610b4SJohnny Huang 		break;
1238*cd1610b4SJohnny Huang 	}
1239*cd1610b4SJohnny Huang 
1240*cd1610b4SJohnny Huang 	if (!nconfirm) {
1241*cd1610b4SJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
1242*cd1610b4SJohnny Huang 		if (!confirm_yesno()) {
1243*cd1610b4SJohnny Huang 			printf(" Aborting\n");
1244*cd1610b4SJohnny Huang 			return 1;
1245*cd1610b4SJohnny Huang 		}
1246*cd1610b4SJohnny Huang 	}
1247*cd1610b4SJohnny Huang 
1248*cd1610b4SJohnny Huang 	switch (mode) {
1249*cd1610b4SJohnny Huang 	case MODE_STRAP:
1250*cd1610b4SJohnny Huang 		return otp_prog_strap(strap_buf);
1251*cd1610b4SJohnny Huang 	case MODE_CONF:
1252*cd1610b4SJohnny Huang 	case MODE_DATA:
1253*cd1610b4SJohnny Huang 		otp_prog_bit(value, prog_address, bit_offset, 0);
1254*cd1610b4SJohnny Huang 		pass = -1;
1255*cd1610b4SJohnny Huang 		for (i = 0; i < RETRY; i++) {
1256*cd1610b4SJohnny Huang 			if (prog_conf_verify(prog_address, bit_offset, value) != 0) {
1257*cd1610b4SJohnny Huang 				otp_prog_bit(value, prog_address, bit_offset, 1);
1258*cd1610b4SJohnny Huang 			} else {
1259*cd1610b4SJohnny Huang 				pass = 0;
1260*cd1610b4SJohnny Huang 				break;
1261*cd1610b4SJohnny Huang 			}
1262*cd1610b4SJohnny Huang 		}
1263*cd1610b4SJohnny Huang 		return pass;
1264*cd1610b4SJohnny Huang 	}
1265*cd1610b4SJohnny Huang 
1266*cd1610b4SJohnny Huang 	return -1;
1267*cd1610b4SJohnny Huang }
1268*cd1610b4SJohnny Huang 
126969d5fd8fSJohnny Huang static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc,
127069d5fd8fSJohnny Huang 		      char *const argv[])
127169d5fd8fSJohnny Huang {
127269d5fd8fSJohnny Huang 	char *cmd;
127369d5fd8fSJohnny Huang 	int mode = 0;
127469d5fd8fSJohnny Huang 	int nconfirm = 0;
1275*cd1610b4SJohnny Huang 	uint32_t addr = 0;
1276*cd1610b4SJohnny Huang 	uint32_t otp_addr = 0;
1277*cd1610b4SJohnny Huang 	int dw_count = 0;
1278*cd1610b4SJohnny Huang 	int bit_offset = 0;
1279*cd1610b4SJohnny Huang 	int value = 0;
1280*cd1610b4SJohnny Huang 	int protect = 0;
128169d5fd8fSJohnny Huang 
128269d5fd8fSJohnny Huang 
128369d5fd8fSJohnny Huang 
128469d5fd8fSJohnny Huang 	if (argc < 2) {
128569d5fd8fSJohnny Huang usage:
128669d5fd8fSJohnny Huang 		return CMD_RET_USAGE;
128769d5fd8fSJohnny Huang 	}
128869d5fd8fSJohnny Huang 
128969d5fd8fSJohnny Huang 	cmd = argv[1];
129069d5fd8fSJohnny Huang 	if (!strcmp(cmd, "read")) {
129169d5fd8fSJohnny Huang 		if (!strcmp(argv[2], "conf"))
129269d5fd8fSJohnny Huang 			mode = MODE_CONF;
129369d5fd8fSJohnny Huang 		else if (!strcmp(argv[2], "data"))
129469d5fd8fSJohnny Huang 			mode = MODE_DATA;
129569d5fd8fSJohnny Huang 		else if (!strcmp(argv[2], "strap"))
129669d5fd8fSJohnny Huang 			mode = MODE_STRAP;
129769d5fd8fSJohnny Huang 		else
129869d5fd8fSJohnny Huang 			goto usage;
129969d5fd8fSJohnny Huang 
130069d5fd8fSJohnny Huang 		writel(OTP_PASSWD, 0x1e6f2000); //password
130169d5fd8fSJohnny Huang 		otp_addr = simple_strtoul(argv[3], NULL, 16);
130269d5fd8fSJohnny Huang 		dw_count = simple_strtoul(argv[4], NULL, 16);
130369d5fd8fSJohnny Huang 		if (mode == MODE_CONF) {
130469d5fd8fSJohnny Huang 			otp_print_config(otp_addr, dw_count);
130569d5fd8fSJohnny Huang 		} else if (mode == MODE_DATA) {
130669d5fd8fSJohnny Huang 			otp_print_data(otp_addr, dw_count);
130769d5fd8fSJohnny Huang 		} else if (mode == MODE_STRAP) {
1308*cd1610b4SJohnny Huang 			otp_print_strap(0, 64);
130969d5fd8fSJohnny Huang 		}
131069d5fd8fSJohnny Huang 	} else if (!strcmp(cmd, "prog")) {
131169d5fd8fSJohnny Huang 		if (!strcmp(argv[2], "conf"))
131269d5fd8fSJohnny Huang 			mode = MODE_CONF;
131369d5fd8fSJohnny Huang 		else if (!strcmp(argv[2], "strap"))
131469d5fd8fSJohnny Huang 			mode = MODE_STRAP;
131569d5fd8fSJohnny Huang 		else if (!strcmp(argv[2], "data"))
131669d5fd8fSJohnny Huang 			mode = MODE_DATA;
131769d5fd8fSJohnny Huang 		else if (!strcmp(argv[2], "all"))
131869d5fd8fSJohnny Huang 			mode = MODE_ALL;
131969d5fd8fSJohnny Huang 		else
132069d5fd8fSJohnny Huang 			goto usage;
132169d5fd8fSJohnny Huang 
132269d5fd8fSJohnny Huang 		if (!strcmp(argv[3], "f"))
132369d5fd8fSJohnny Huang 			nconfirm = 1;
132469d5fd8fSJohnny Huang 		writel(OTP_PASSWD, 0x1e6f2000); //password
132569d5fd8fSJohnny Huang 		addr = simple_strtoul(argv[3 + nconfirm], NULL, 16);
132669d5fd8fSJohnny Huang 		otp_addr = simple_strtoul(argv[4 + nconfirm], NULL, 16);
132769d5fd8fSJohnny Huang 		dw_count = simple_strtoul(argv[5 + nconfirm], NULL, 16);
132869d5fd8fSJohnny Huang 		return do_otp_prog(mode, addr, otp_addr, dw_count, nconfirm);
1329*cd1610b4SJohnny Huang 	} else if (!strcmp(cmd, "pb")) {
1330*cd1610b4SJohnny Huang 		if (!strcmp(argv[2], "conf"))
1331*cd1610b4SJohnny Huang 			mode = MODE_CONF;
1332*cd1610b4SJohnny Huang 		else if (!strcmp(argv[2], "strap"))
1333*cd1610b4SJohnny Huang 			mode = MODE_STRAP;
1334*cd1610b4SJohnny Huang 		else if (!strcmp(argv[2], "data"))
1335*cd1610b4SJohnny Huang 			mode = MODE_DATA;
1336*cd1610b4SJohnny Huang 		else
1337*cd1610b4SJohnny Huang 			goto usage;
1338*cd1610b4SJohnny Huang 		if (!strcmp(argv[3], "f"))
1339*cd1610b4SJohnny Huang 			nconfirm = 1;
1340*cd1610b4SJohnny Huang 
1341*cd1610b4SJohnny Huang 		if (mode == MODE_STRAP) {
1342*cd1610b4SJohnny Huang 			bit_offset = simple_strtoul(argv[3 + nconfirm], NULL, 16);
1343*cd1610b4SJohnny Huang 			value = simple_strtoul(argv[4 + nconfirm], NULL, 16);
1344*cd1610b4SJohnny Huang 			protect = simple_strtoul(argv[5 + nconfirm], NULL, 16);
1345*cd1610b4SJohnny Huang 			if (bit_offset >= 64)
1346*cd1610b4SJohnny Huang 				return -1;
1347*cd1610b4SJohnny Huang 		} else {
1348*cd1610b4SJohnny Huang 			otp_addr = simple_strtoul(argv[3 + nconfirm], NULL, 16);
1349*cd1610b4SJohnny Huang 			bit_offset = simple_strtoul(argv[4 + nconfirm], NULL, 16);
1350*cd1610b4SJohnny Huang 			value = simple_strtoul(argv[5 + nconfirm], NULL, 16);
1351*cd1610b4SJohnny Huang 			if (bit_offset >= 32)
1352*cd1610b4SJohnny Huang 				return -1;
1353*cd1610b4SJohnny Huang 		}
1354*cd1610b4SJohnny Huang 		if (value != 0 && value != 1)
1355*cd1610b4SJohnny Huang 			return -1;
1356*cd1610b4SJohnny Huang 
1357*cd1610b4SJohnny Huang 		writel(OTP_PASSWD, 0x1e6f2000); //password
1358*cd1610b4SJohnny Huang 		return do_otp_prog_bit(mode, otp_addr, bit_offset, value, protect, nconfirm);
135969d5fd8fSJohnny Huang 	} else if (!strcmp(cmd, "comp")) {
136069d5fd8fSJohnny Huang 		writel(OTP_PASSWD, 0x1e6f2000); //password
136169d5fd8fSJohnny Huang 		addr = simple_strtoul(argv[2], NULL, 16);
136269d5fd8fSJohnny Huang 		otp_addr = simple_strtoul(argv[3], NULL, 16);
136369d5fd8fSJohnny Huang 		if (otp_compare(otp_addr, addr) >= 0) {
136469d5fd8fSJohnny Huang 			printf("Compare pass\n");
136569d5fd8fSJohnny Huang 		} else {
136669d5fd8fSJohnny Huang 			printf("Compare fail\n");
136769d5fd8fSJohnny Huang 		}
136869d5fd8fSJohnny Huang 	} else {
136969d5fd8fSJohnny Huang 		goto usage;
137069d5fd8fSJohnny Huang 	}
137169d5fd8fSJohnny Huang 
137269d5fd8fSJohnny Huang 
137369d5fd8fSJohnny Huang 	return 0;
137469d5fd8fSJohnny Huang }
137569d5fd8fSJohnny Huang 
137669d5fd8fSJohnny Huang 
137769d5fd8fSJohnny Huang U_BOOT_CMD(
137869d5fd8fSJohnny Huang 	otp, 7, 0,  do_ast_otp,
137969d5fd8fSJohnny Huang 	"ASPEED One-Time-Programmable sub-system",
1380*cd1610b4SJohnny Huang 	"read conf|strap|data <otp_dw_offset> <dw_count>\n"
1381*cd1610b4SJohnny Huang 	"otp prog conf|strap|data|all [f] <addr> <otp_dw_offset> <dw_count>\n"
1382*cd1610b4SJohnny Huang 	"otp pb conf|data [f] <otp_dw_offset> <bit_offset> <value>\n"
1383*cd1610b4SJohnny Huang 	"otp pb strap [f] <bit_offset> <value> <protect>\n"
1384*cd1610b4SJohnny Huang 	"otp comp <addr> <otp_dw_offset>\n"
138569d5fd8fSJohnny Huang );
1386