xref: /openbmc/u-boot/cmd/otp.c (revision a6d0d645902b17c89e8e748ed988ac87daf5787a)
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
28*a6d0d645SJohnny Huang #define OTP_REGION_STRAP		1
29*a6d0d645SJohnny Huang #define OTP_REGION_CONF			2
30*a6d0d645SJohnny Huang #define OTP_REGION_DATA			3
31*a6d0d645SJohnny Huang #define OTP_REGION_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 
204*a6d0d645SJohnny Huang static int verify_bit(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);
212*a6d0d645SJohnny Huang 	// printf("verify_bit = %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 
219*a6d0d645SJohnny Huang static uint32_t verify_dw(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 			}
530cd1610b4SJohnny 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 
576*a6d0d645SJohnny Huang static int otp_prog_conf(uint32_t *buf)
57769d5fd8fSJohnny Huang {
578*a6d0d645SJohnny Huang 	int i, k;
579*a6d0d645SJohnny Huang 	int pass;
580*a6d0d645SJohnny Huang 	uint32_t prog_address;
581*a6d0d645SJohnny Huang 	uint32_t data[12];
582*a6d0d645SJohnny Huang 	uint32_t compare[2];
58369d5fd8fSJohnny Huang 
584*a6d0d645SJohnny Huang 	printf("Read OTP Config Region:\n");
585*a6d0d645SJohnny Huang 
586*a6d0d645SJohnny Huang 	printProgress(0, 12, "");
587*a6d0d645SJohnny Huang 	for (i = 0; i < 12 ; i ++) {
588*a6d0d645SJohnny Huang 		printProgress(i + 1, 12, "");
58969d5fd8fSJohnny Huang 		prog_address = 0x800;
590*a6d0d645SJohnny Huang 		prog_address |= (i / 8) * 0x200;
591*a6d0d645SJohnny Huang 		prog_address |= (i % 8) * 0x2;
592*a6d0d645SJohnny Huang 		otp_read_data(prog_address, &data[i]);
593*a6d0d645SJohnny Huang 	}
594*a6d0d645SJohnny Huang 
595*a6d0d645SJohnny Huang 	printf("Check writable...\n");
596*a6d0d645SJohnny Huang 	for (i = 0; i < 12; i++) {
597*a6d0d645SJohnny Huang 		if (data[i] == buf[i])
59869d5fd8fSJohnny Huang 			continue;
599*a6d0d645SJohnny Huang 		if ((data[i] | buf[i]) == buf[i]) {
600*a6d0d645SJohnny Huang 			continue;
601*a6d0d645SJohnny Huang 		} else {
602*a6d0d645SJohnny Huang 			printf("Input image can't program into OTP, please check.\n");
603*a6d0d645SJohnny Huang 			printf("OTPCFG[%d] = %x\n", i, data[i]);
604*a6d0d645SJohnny Huang 			printf("Input [%d] = %x\n", i, buf[i]);
605*a6d0d645SJohnny Huang 			goto fail;
606*a6d0d645SJohnny Huang 		}
607*a6d0d645SJohnny Huang 	}
608*a6d0d645SJohnny Huang 
609*a6d0d645SJohnny Huang 	printf("Start Programing...\n");
610*a6d0d645SJohnny Huang 	printProgress(0, 12, "");
611*a6d0d645SJohnny Huang 	for (i = 0; i < 12; i++) {
612*a6d0d645SJohnny Huang 		prog_address = 0x800;
613*a6d0d645SJohnny Huang 		prog_address |= (i / 8) * 0x200;
614*a6d0d645SJohnny Huang 		prog_address |= (i % 8) * 0x2;
615*a6d0d645SJohnny Huang 		if (data[i] == buf[i]) {
616*a6d0d645SJohnny Huang 			printProgress(i + 1, 12, "[%03X]=%08X HIT", prog_address, buf[i]);
617*a6d0d645SJohnny Huang 			continue;
618*a6d0d645SJohnny Huang 		}
619*a6d0d645SJohnny Huang 
620*a6d0d645SJohnny Huang 		printProgress(i + 1, 12, "[%03X]=%08X    ", prog_address, buf[i]);
621*a6d0d645SJohnny Huang 
622*a6d0d645SJohnny Huang 		otp_prog_dw(buf[i], prog_address, 0);
623*a6d0d645SJohnny Huang 
62469d5fd8fSJohnny Huang 		pass = 0;
62569d5fd8fSJohnny Huang 		for (k = 0; k < RETRY; k++) {
626*a6d0d645SJohnny Huang 			if (verify_dw(prog_address, &buf[i], compare, 1) != 0) {
627*a6d0d645SJohnny Huang 				otp_prog_dw(compare[0], prog_address, 1);
628*a6d0d645SJohnny Huang 			} else {
62969d5fd8fSJohnny Huang 				pass = 1;
63069d5fd8fSJohnny Huang 				break;
63169d5fd8fSJohnny Huang 			}
63269d5fd8fSJohnny Huang 		}
633*a6d0d645SJohnny Huang 	}
634*a6d0d645SJohnny Huang 
63569d5fd8fSJohnny Huang 	if (!pass)
636*a6d0d645SJohnny Huang 		goto fail;
63769d5fd8fSJohnny Huang 	return 0;
638*a6d0d645SJohnny Huang fail:
639*a6d0d645SJohnny Huang 	return -1;
640*a6d0d645SJohnny Huang 
64169d5fd8fSJohnny Huang }
64269d5fd8fSJohnny Huang 
64369d5fd8fSJohnny Huang static void otp_strp_status(struct otpstrap *otpstrap)
64469d5fd8fSJohnny Huang {
64569d5fd8fSJohnny Huang 	uint32_t OTPSTRAP_RAW[2];
64669d5fd8fSJohnny Huang 	int i, j;
64769d5fd8fSJohnny Huang 
64869d5fd8fSJohnny Huang 	for (j = 0; j < 64; j++) {
64969d5fd8fSJohnny Huang 		otpstrap[j].value = 0;
65069d5fd8fSJohnny Huang 		otpstrap[j].remain_times = 7;
65169d5fd8fSJohnny Huang 		otpstrap[j].writeable_option = -1;
65269d5fd8fSJohnny Huang 		otpstrap[j].protected = 0;
65369d5fd8fSJohnny Huang 	}
65469d5fd8fSJohnny Huang 
65569d5fd8fSJohnny Huang 	for (i = 16; i < 30; i += 2) {
65669d5fd8fSJohnny Huang 		int option = (i - 16) / 2;
65769d5fd8fSJohnny Huang 		otp_read_config(i, &OTPSTRAP_RAW[0]);
65869d5fd8fSJohnny Huang 		otp_read_config(i + 1, &OTPSTRAP_RAW[1]);
65969d5fd8fSJohnny Huang 		for (j = 0; j < 32; j++) {
66069d5fd8fSJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[0] >> j) & 0x1);
66169d5fd8fSJohnny Huang 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
66269d5fd8fSJohnny Huang 				otpstrap[j].writeable_option = option;
66369d5fd8fSJohnny Huang 			}
66469d5fd8fSJohnny Huang 			if (bit_value == 1)
66569d5fd8fSJohnny Huang 				otpstrap[j].remain_times --;
66669d5fd8fSJohnny Huang 			otpstrap[j].value ^= bit_value;
66769d5fd8fSJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
66869d5fd8fSJohnny Huang 		}
66969d5fd8fSJohnny Huang 		for (j = 32; j < 64; j++) {
67069d5fd8fSJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1);
67169d5fd8fSJohnny Huang 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
67269d5fd8fSJohnny Huang 				otpstrap[j].writeable_option = option;
67369d5fd8fSJohnny Huang 			}
67469d5fd8fSJohnny Huang 			if (bit_value == 1)
67569d5fd8fSJohnny Huang 				otpstrap[j].remain_times --;
67669d5fd8fSJohnny Huang 			otpstrap[j].value ^= bit_value;
67769d5fd8fSJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
67869d5fd8fSJohnny Huang 		}
67969d5fd8fSJohnny Huang 	}
68069d5fd8fSJohnny Huang 	otp_read_config(30, &OTPSTRAP_RAW[0]);
68169d5fd8fSJohnny Huang 	otp_read_config(31, &OTPSTRAP_RAW[1]);
68269d5fd8fSJohnny Huang 	for (j = 0; j < 32; j++) {
68369d5fd8fSJohnny Huang 		if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
68469d5fd8fSJohnny Huang 			otpstrap[j].protected = 1;
68569d5fd8fSJohnny Huang 	}
68669d5fd8fSJohnny Huang 	for (j = 32; j < 64; j++) {
68769d5fd8fSJohnny Huang 		if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
68869d5fd8fSJohnny Huang 			otpstrap[j].protected = 1;
68969d5fd8fSJohnny Huang 	}
69069d5fd8fSJohnny Huang }
69169d5fd8fSJohnny Huang 
69269d5fd8fSJohnny Huang static int otp_strap_parse(uint32_t *buf)
69369d5fd8fSJohnny Huang {
69469d5fd8fSJohnny Huang 	int i;
69569d5fd8fSJohnny Huang 	uint32_t *strap_keep = buf + 2;
69669d5fd8fSJohnny Huang 	uint32_t *strap_protect = buf + 4;
69769d5fd8fSJohnny Huang 	int bit, pbit, kbit;
69869d5fd8fSJohnny Huang 	int fail = 0;
69969d5fd8fSJohnny Huang 	struct otpstrap otpstrap[64];
70069d5fd8fSJohnny Huang 
70169d5fd8fSJohnny Huang 	otp_strp_status(otpstrap);
70269d5fd8fSJohnny Huang 	for (i = 0; i < 64; i++) {
70369d5fd8fSJohnny Huang 		if (i < 32) {
70469d5fd8fSJohnny Huang 			bit = (buf[0] >> i) & 0x1;
70569d5fd8fSJohnny Huang 			kbit = (strap_keep[0] >> i) & 0x1;
70669d5fd8fSJohnny Huang 			pbit = (strap_protect[0] >> i) & 0x1;
70769d5fd8fSJohnny Huang 		} else {
70869d5fd8fSJohnny Huang 			bit = (buf[1] >> (i - 32)) & 0x1;
70969d5fd8fSJohnny Huang 			kbit = (strap_keep[1] >> (i - 32)) & 0x1;
71069d5fd8fSJohnny Huang 			pbit = (strap_protect[1] >> (i - 32)) & 0x1;
71169d5fd8fSJohnny Huang 		}
71269d5fd8fSJohnny Huang 
71369d5fd8fSJohnny Huang 		if (kbit == 1) {
71469d5fd8fSJohnny Huang 			continue;
71569d5fd8fSJohnny Huang 		} else {
71669d5fd8fSJohnny Huang 			printf("OTPSTRAP[%d]:\n", i);
71769d5fd8fSJohnny Huang 		}
71869d5fd8fSJohnny Huang 		if (bit == otpstrap[i].value) {
71969d5fd8fSJohnny Huang 			printf("    The value is same as before, skip it.\n");
72069d5fd8fSJohnny Huang 			continue;
72169d5fd8fSJohnny Huang 		}
72269d5fd8fSJohnny Huang 		if (otpstrap[i].protected == 1) {
72369d5fd8fSJohnny Huang 			printf("    This bit is protected and is not writable\n");
72469d5fd8fSJohnny Huang 			fail = 1;
72569d5fd8fSJohnny Huang 			continue;
72669d5fd8fSJohnny Huang 		}
72769d5fd8fSJohnny Huang 		if (otpstrap[i].remain_times == 0) {
72869d5fd8fSJohnny Huang 			printf("    This bit is no remaining number of times to write.\n");
72969d5fd8fSJohnny Huang 			fail = 1;
73069d5fd8fSJohnny Huang 			continue;
73169d5fd8fSJohnny Huang 		}
73269d5fd8fSJohnny Huang 		if (pbit == 1) {
73369d5fd8fSJohnny Huang 			printf("    This bit will be protected and become non-writable.\n");
73469d5fd8fSJohnny Huang 		}
735cd1610b4SJohnny 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);
73669d5fd8fSJohnny Huang 	}
73769d5fd8fSJohnny Huang 	if (fail == 1)
73869d5fd8fSJohnny Huang 		return -1;
73969d5fd8fSJohnny Huang 	else
74069d5fd8fSJohnny Huang 		return 0;
74169d5fd8fSJohnny Huang }
74269d5fd8fSJohnny Huang 
743cd1610b4SJohnny Huang static void otp_print_strap(int start, int count)
74469d5fd8fSJohnny Huang {
74569d5fd8fSJohnny Huang 	int i, j;
74669d5fd8fSJohnny Huang 	struct otpstrap otpstrap[64];
74769d5fd8fSJohnny Huang 
74869d5fd8fSJohnny Huang 	otp_strp_status(otpstrap);
74969d5fd8fSJohnny Huang 
750cd1610b4SJohnny Huang 	for (i = start; i < start + count; i++) {
75169d5fd8fSJohnny Huang 		printf("OTPSTRAP[%d]:\n", i);
75269d5fd8fSJohnny Huang 		printf("  OTP Option value: ");
75369d5fd8fSJohnny Huang 		for (j = 1; j <= 7; j++)
75469d5fd8fSJohnny Huang 			printf("[%d]:%d ", j, otpstrap[i].option_array[j - 1]);
75569d5fd8fSJohnny Huang 		printf("\n");
75669d5fd8fSJohnny Huang 		printf("  OTP Value: %d\n", otpstrap[i].value);
75769d5fd8fSJohnny Huang 		printf("  Status:\n");
75869d5fd8fSJohnny Huang 		if (otpstrap[i].protected == 1) {
75969d5fd8fSJohnny Huang 			printf("    OTPSTRAP[%d] is protected and is not writable\n", i);
76069d5fd8fSJohnny Huang 		} else {
76169d5fd8fSJohnny Huang 			printf("    OTPSTRAP[%d] is not protected ", i);
76269d5fd8fSJohnny Huang 			if (otpstrap[i].remain_times == 0) {
76369d5fd8fSJohnny Huang 				printf("and no remaining number of times to write.\n");
76469d5fd8fSJohnny Huang 			} else {
76569d5fd8fSJohnny Huang 				printf("and still can write %d number of times\n", otpstrap[i].remain_times);
76669d5fd8fSJohnny Huang 			}
76769d5fd8fSJohnny Huang 		}
76869d5fd8fSJohnny Huang 	}
76969d5fd8fSJohnny Huang }
77069d5fd8fSJohnny Huang 
77169d5fd8fSJohnny Huang static int otp_prog_strap(uint32_t *buf)
77269d5fd8fSJohnny Huang {
77369d5fd8fSJohnny Huang 	int i, j;
77469d5fd8fSJohnny Huang 	uint32_t *strap_keep = buf + 2;
77569d5fd8fSJohnny Huang 	uint32_t *strap_protect = buf + 4;
77669d5fd8fSJohnny Huang 	uint32_t prog_bit, prog_address;
77769d5fd8fSJohnny Huang 	int bit, pbit, kbit, offset;
77869d5fd8fSJohnny Huang 	int fail = 0;
77969d5fd8fSJohnny Huang 	int pass, soak;
78069d5fd8fSJohnny Huang 	struct otpstrap otpstrap[64];
78169d5fd8fSJohnny Huang 
78269d5fd8fSJohnny Huang 	otp_strp_status(otpstrap);
78369d5fd8fSJohnny Huang 
78469d5fd8fSJohnny Huang 	otp_write(0x3000, 0x4061); // Write MRA
78569d5fd8fSJohnny Huang 	otp_write(0x5000, 0x302f); // Write MRB
78669d5fd8fSJohnny Huang 	otp_write(0x1000, 0x4020); // Write MR
78769d5fd8fSJohnny Huang 	for (i = 0; i < 64; i++) {
7884c1c9b35SJohnny Huang 		printProgress(i + 1, 64, "");
78969d5fd8fSJohnny Huang 		prog_address = 0x800;
79069d5fd8fSJohnny Huang 		if (i < 32) {
79169d5fd8fSJohnny Huang 			offset = i;
79269d5fd8fSJohnny Huang 			bit = (buf[0] >> offset) & 0x1;
79369d5fd8fSJohnny Huang 			kbit = (strap_keep[0] >> offset) & 0x1;
79469d5fd8fSJohnny Huang 			pbit = (strap_protect[0] >> offset) & 0x1;
79569d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200;
79669d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2;
79769d5fd8fSJohnny Huang 
79869d5fd8fSJohnny Huang 		} else {
79969d5fd8fSJohnny Huang 			offset = (i - 32);
80069d5fd8fSJohnny Huang 			bit = (buf[1] >> offset) & 0x1;
80169d5fd8fSJohnny Huang 			kbit = (strap_keep[1] >> offset) & 0x1;
80269d5fd8fSJohnny Huang 			pbit = (strap_protect[1] >> offset) & 0x1;
80369d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200;
80469d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2;
80569d5fd8fSJohnny Huang 		}
80669d5fd8fSJohnny Huang 		prog_bit = ~(0x1 << offset);
80769d5fd8fSJohnny Huang 
80869d5fd8fSJohnny Huang 		if (kbit == 1) {
80969d5fd8fSJohnny Huang 			continue;
81069d5fd8fSJohnny Huang 		}
81169d5fd8fSJohnny Huang 		if (bit == otpstrap[i].value) {
81269d5fd8fSJohnny Huang 			continue;
81369d5fd8fSJohnny Huang 		}
81469d5fd8fSJohnny Huang 		if (otpstrap[i].protected == 1) {
81569d5fd8fSJohnny Huang 			fail = 1;
81669d5fd8fSJohnny Huang 			continue;
81769d5fd8fSJohnny Huang 		}
81869d5fd8fSJohnny Huang 		if (otpstrap[i].remain_times == 0) {
81969d5fd8fSJohnny Huang 			fail = 1;
82069d5fd8fSJohnny Huang 			continue;
82169d5fd8fSJohnny Huang 		}
82269d5fd8fSJohnny Huang 		pass = 0;
82369d5fd8fSJohnny Huang 		soak = 0;
8244b65a65dSJohnny Huang 		otp_write(0x3000, 0x4061); // Write MRA
8254b65a65dSJohnny Huang 		otp_write(0x5000, 0x302f); // Write MRB
8264b65a65dSJohnny Huang 		otp_write(0x1000, 0x4020); // Write MR
8274b65a65dSJohnny Huang 		writel(0x04190760, 0x1e602008); //normal program
82869d5fd8fSJohnny Huang 		for (j = 0; j < RETRY; j++) {
82969d5fd8fSJohnny Huang 			if (!soak) {
83069d5fd8fSJohnny Huang 				otp_prog(prog_address, prog_bit);
831*a6d0d645SJohnny Huang 				if (verify_bit(prog_address, offset, 1) == 0) {
83269d5fd8fSJohnny Huang 					pass = 1;
83369d5fd8fSJohnny Huang 					break;
83469d5fd8fSJohnny Huang 				}
83569d5fd8fSJohnny Huang 				soak = 1;
8364b65a65dSJohnny Huang 				otp_write(0x3000, 0x4021); // Write MRA
8374b65a65dSJohnny Huang 				otp_write(0x5000, 0x1027); // Write MRB
8384b65a65dSJohnny Huang 				otp_write(0x1000, 0x4820); // Write MR
83969d5fd8fSJohnny Huang 				writel(0x041930d4, 0x1e602008); //soak program
8404b65a65dSJohnny Huang 			}
84169d5fd8fSJohnny Huang 			otp_prog(prog_address, prog_bit);
842*a6d0d645SJohnny Huang 			if (verify_bit(prog_address, offset, 1) == 0) {
84369d5fd8fSJohnny Huang 				pass = 1;
84469d5fd8fSJohnny Huang 				break;
84569d5fd8fSJohnny Huang 			}
84669d5fd8fSJohnny Huang 		}
84769d5fd8fSJohnny Huang 		if (!pass)
84869d5fd8fSJohnny Huang 			return -1;
84969d5fd8fSJohnny Huang 
85069d5fd8fSJohnny Huang 		if (pbit == 0)
85169d5fd8fSJohnny Huang 			continue;
85269d5fd8fSJohnny Huang 		prog_address = 0x800;
85369d5fd8fSJohnny Huang 		if (i < 32)
85469d5fd8fSJohnny Huang 			prog_address |= 0x60c;
85569d5fd8fSJohnny Huang 		else
85669d5fd8fSJohnny Huang 			prog_address |= 0x60e;
85769d5fd8fSJohnny Huang 
85869d5fd8fSJohnny Huang 		for (j = 0; j < RETRY; j++) {
85969d5fd8fSJohnny Huang 			if (!soak) {
86069d5fd8fSJohnny Huang 				writel(0x04190760, 0x1e602008); //normal program
86169d5fd8fSJohnny Huang 				otp_prog(prog_address, prog_bit);
862*a6d0d645SJohnny Huang 				if (verify_bit(prog_address, offset, 1) == 0) {
86369d5fd8fSJohnny Huang 					pass = 1;
86469d5fd8fSJohnny Huang 					break;
86569d5fd8fSJohnny Huang 				}
86669d5fd8fSJohnny Huang 				soak = 1;
86769d5fd8fSJohnny Huang 			}
86869d5fd8fSJohnny Huang 			writel(0x041930d4, 0x1e602008); //soak program
86969d5fd8fSJohnny Huang 			otp_prog(prog_address, prog_bit);
870*a6d0d645SJohnny Huang 			if (verify_bit(prog_address, offset, 1) == 0) {
87169d5fd8fSJohnny Huang 				pass = 1;
87269d5fd8fSJohnny Huang 				break;
87369d5fd8fSJohnny Huang 			}
87469d5fd8fSJohnny Huang 		}
87569d5fd8fSJohnny Huang 		if (!pass)
87669d5fd8fSJohnny Huang 			return -1;
87769d5fd8fSJohnny Huang 
87869d5fd8fSJohnny Huang 	}
87969d5fd8fSJohnny Huang 	if (fail == 1)
88069d5fd8fSJohnny Huang 		return -1;
88169d5fd8fSJohnny Huang 	else
88269d5fd8fSJohnny Huang 		return 0;
88369d5fd8fSJohnny Huang 
88469d5fd8fSJohnny Huang }
88569d5fd8fSJohnny Huang 
8864c1c9b35SJohnny Huang static void otp_prog_dw(uint32_t value, uint32_t prog_address, int soak)
88769d5fd8fSJohnny Huang {
8884c1c9b35SJohnny Huang 	int j, bit_value, prog_bit;
88969d5fd8fSJohnny Huang 
8904c1c9b35SJohnny Huang 	if (soak) {
8914c1c9b35SJohnny Huang 		otp_write(0x3000, 0x4021); // Write MRA
8924c1c9b35SJohnny Huang 		otp_write(0x5000, 0x1027); // Write MRB
8934c1c9b35SJohnny Huang 		otp_write(0x1000, 0x4820); // Write MR
8944c1c9b35SJohnny Huang 		writel(0x041930d4, 0x1e602008); //soak program
8954c1c9b35SJohnny Huang 	} else {
8964c1c9b35SJohnny Huang 		otp_write(0x3000, 0x4061); // Write MRA
8974c1c9b35SJohnny Huang 		otp_write(0x5000, 0x302f); // Write MRB
8984c1c9b35SJohnny Huang 		otp_write(0x1000, 0x4020); // Write MR
8994c1c9b35SJohnny Huang 		writel(0x04190760, 0x1e602008); //normal program
9004c1c9b35SJohnny Huang 	}
9014c1c9b35SJohnny Huang 
90269d5fd8fSJohnny Huang 	for (j = 0; j < 32; j++) {
9034c1c9b35SJohnny Huang 		bit_value = (value >> j) & 0x1;
90469d5fd8fSJohnny Huang 		if (prog_address % 2 == 0) {
90569d5fd8fSJohnny Huang 			if (bit_value)
90669d5fd8fSJohnny Huang 				prog_bit = ~(0x1 << j);
90769d5fd8fSJohnny Huang 			else
90869d5fd8fSJohnny Huang 				continue;
90969d5fd8fSJohnny Huang 		} else {
91069d5fd8fSJohnny Huang 			prog_address |= 1 << 15;
91169d5fd8fSJohnny Huang 			if (bit_value)
91269d5fd8fSJohnny Huang 				continue;
91369d5fd8fSJohnny Huang 			else
91469d5fd8fSJohnny Huang 				prog_bit = 0x1 << j;
91569d5fd8fSJohnny Huang 		}
9164c1c9b35SJohnny Huang 		otp_prog(prog_address, prog_bit);
9174c1c9b35SJohnny Huang 	}
9184c1c9b35SJohnny Huang }
9194c1c9b35SJohnny Huang 
920cd1610b4SJohnny Huang static void otp_prog_bit(uint32_t value, uint32_t prog_address, uint32_t bit_offset, int soak)
921cd1610b4SJohnny Huang {
922cd1610b4SJohnny Huang 	int prog_bit;
923cd1610b4SJohnny Huang 
924cd1610b4SJohnny Huang 	if (soak) {
925cd1610b4SJohnny Huang 		otp_write(0x3000, 0x4021); // Write MRA
926cd1610b4SJohnny Huang 		otp_write(0x5000, 0x1027); // Write MRB
927cd1610b4SJohnny Huang 		otp_write(0x1000, 0x4820); // Write MR
928cd1610b4SJohnny Huang 		writel(0x041930d4, 0x1e602008); //soak program
929cd1610b4SJohnny Huang 	} else {
930cd1610b4SJohnny Huang 		otp_write(0x3000, 0x4061); // Write MRA
931cd1610b4SJohnny Huang 		otp_write(0x5000, 0x302f); // Write MRB
932cd1610b4SJohnny Huang 		otp_write(0x1000, 0x4020); // Write MR
933cd1610b4SJohnny Huang 		writel(0x04190760, 0x1e602008); //normal program
934cd1610b4SJohnny Huang 	}
935cd1610b4SJohnny Huang 	if (prog_address % 2 == 0) {
936cd1610b4SJohnny Huang 		if (value)
937cd1610b4SJohnny Huang 			prog_bit = ~(0x1 << bit_offset);
938cd1610b4SJohnny Huang 		else
939cd1610b4SJohnny Huang 			return;
940cd1610b4SJohnny Huang 	} else {
941cd1610b4SJohnny Huang 		prog_address |= 1 << 15;
942cd1610b4SJohnny Huang 		if (!value)
943cd1610b4SJohnny Huang 			prog_bit = 0x1 << bit_offset;
944cd1610b4SJohnny Huang 		else
945cd1610b4SJohnny Huang 			return;
946cd1610b4SJohnny Huang 	}
947cd1610b4SJohnny Huang 	otp_prog(prog_address, prog_bit);
948cd1610b4SJohnny Huang }
949cd1610b4SJohnny Huang 
9504c1c9b35SJohnny Huang static int otp_prog_data(uint32_t *buf, int otp_addr, int dw_count)
9514c1c9b35SJohnny Huang {
9524c1c9b35SJohnny Huang 	int i, k;
9534c1c9b35SJohnny Huang 	int pass;
9544c1c9b35SJohnny Huang 	int addr_odd, count_odd;
9554c1c9b35SJohnny Huang 	int d_size;
9564c1c9b35SJohnny Huang 	uint32_t prog_address;
9574c1c9b35SJohnny Huang 	uint32_t *data;
9584c1c9b35SJohnny Huang 	uint32_t compare[2];
9594c1c9b35SJohnny Huang 
9604c1c9b35SJohnny Huang 	count_odd = dw_count % 2;
9614c1c9b35SJohnny Huang 	addr_odd = otp_addr % 2;
9624c1c9b35SJohnny Huang 	d_size = dw_count + (count_odd ^ addr_odd);
9634c1c9b35SJohnny Huang 	data = malloc(d_size * 4);
9644c1c9b35SJohnny Huang 
9654c1c9b35SJohnny Huang 	printf("Read OTP Data:\n");
9664c1c9b35SJohnny Huang 
9674c1c9b35SJohnny Huang 	printProgress(0, d_size, "");
9684c1c9b35SJohnny Huang 	if (!addr_odd) {
9694c1c9b35SJohnny Huang 		for (i = 0; i < d_size ; i += 2) {
9704c1c9b35SJohnny Huang 			printProgress(i + 2, d_size, "");
9714c1c9b35SJohnny Huang 			otp_read_data(i + otp_addr, &data[i]);
9724c1c9b35SJohnny Huang 		}
9734c1c9b35SJohnny Huang 	} else {
9744c1c9b35SJohnny Huang 		otp_read_data(otp_addr - 1, &data[0]);
9754c1c9b35SJohnny Huang 		data[0] = data[1];
9764c1c9b35SJohnny Huang 		for (i = 1; i < d_size ; i += 2) {
9774c1c9b35SJohnny Huang 			printProgress(i + 2, d_size, "");
9784c1c9b35SJohnny Huang 			otp_read_data(i + otp_addr, &data[i]);
9794c1c9b35SJohnny Huang 		}
9804c1c9b35SJohnny Huang 	}
9814c1c9b35SJohnny Huang 
9824c1c9b35SJohnny Huang 	printf("Check writable...\n");
9834c1c9b35SJohnny Huang 	for (i = 0; i < dw_count; i++) {
9844c1c9b35SJohnny Huang 		if (data[i] == buf[i])
9854c1c9b35SJohnny Huang 			continue;
9864c1c9b35SJohnny Huang 		if ((i + otp_addr) % 2 == 0) {
9874c1c9b35SJohnny Huang 			if ((data[i] | buf[i]) == buf[i]) {
9884c1c9b35SJohnny Huang 				continue;
9894c1c9b35SJohnny Huang 			} else {
9904c1c9b35SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
9914c1c9b35SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i + otp_addr, data[i]);
9924c1c9b35SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
9934c1c9b35SJohnny Huang 				goto fail;
9944c1c9b35SJohnny Huang 			}
9954c1c9b35SJohnny Huang 		} else {
9964c1c9b35SJohnny Huang 			if ((data[i] & buf[i]) == buf[i]) {
9974c1c9b35SJohnny Huang 				continue;
9984c1c9b35SJohnny Huang 			} else {
9994c1c9b35SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
10004c1c9b35SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i + otp_addr, data[i]);
10014c1c9b35SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
10024c1c9b35SJohnny Huang 				goto fail;
10034c1c9b35SJohnny Huang 			}
10044c1c9b35SJohnny Huang 		}
10054c1c9b35SJohnny Huang 	}
10064c1c9b35SJohnny Huang 
1007cd1610b4SJohnny Huang #if 1
10084c1c9b35SJohnny Huang 	printf("Start Programing...\n");
10094c1c9b35SJohnny Huang 	printProgress(0, dw_count, "");
10104c1c9b35SJohnny Huang 	if (addr_odd) {
10114c1c9b35SJohnny Huang 		if (data[0] != buf[0]) {
101269d5fd8fSJohnny Huang 			pass = 0;
10134c1c9b35SJohnny Huang 			printProgress(1, dw_count, "[%08X] = %08X                   ", otp_addr, buf[0]);
10144c1c9b35SJohnny Huang 			otp_prog_dw(buf[0], otp_addr, 0);
101569d5fd8fSJohnny Huang 			for (k = 0; k < RETRY; k++) {
1016*a6d0d645SJohnny Huang 				if (verify_dw(otp_addr, &buf[0], compare, 1) != 0) {
10174c1c9b35SJohnny Huang 					otp_prog_dw(compare[0], otp_addr, 1);
10184c1c9b35SJohnny Huang 				} else {
101969d5fd8fSJohnny Huang 					pass = 1;
102069d5fd8fSJohnny Huang 					break;
102169d5fd8fSJohnny Huang 				}
102269d5fd8fSJohnny Huang 			}
102369d5fd8fSJohnny Huang 			if (!pass)
10244c1c9b35SJohnny Huang 				goto fail;
10254c1c9b35SJohnny Huang 		} else {
10264c1c9b35SJohnny Huang 			printProgress(1, dw_count, "[%08X] = %08X HIT               ", otp_addr, buf[0]);
102769d5fd8fSJohnny Huang 		}
102869d5fd8fSJohnny Huang 	}
10294c1c9b35SJohnny Huang 	/* if otp_addr is odd, then begin from 1. */
10304c1c9b35SJohnny Huang 	/* if dw_count is odd, then program the last dw seperately. */
10314c1c9b35SJohnny Huang 	for (i = 0 + addr_odd; i < dw_count - (addr_odd ^ count_odd); i += 2) {
10324c1c9b35SJohnny Huang 		prog_address = i + otp_addr;
10334c1c9b35SJohnny Huang 
10344c1c9b35SJohnny Huang 		if ((data[i] == buf[i]) && (data[i + 1] == buf[i + 1])) {
10354c1c9b35SJohnny Huang 			printProgress(i + 2, dw_count, "[%03X]=%08X HIT;[%03X]=%08X HIT", prog_address, buf[i], prog_address + 1, buf[i + 1]);
10364c1c9b35SJohnny Huang 			continue;
10374c1c9b35SJohnny Huang 		}
10384c1c9b35SJohnny Huang 		if (data[i + 1] == buf[i + 1]) {
10394c1c9b35SJohnny Huang 			printProgress(i + 2, dw_count, "[%03X]=%08X    ;[%03X]=%08X HIT", prog_address, buf[i], prog_address + 1, buf[i + 1]);
10404c1c9b35SJohnny Huang 			otp_prog_dw(buf[i], prog_address, 0);
10414c1c9b35SJohnny Huang 		} else if (data[i] == buf[i]) {
10424c1c9b35SJohnny Huang 			printProgress(i + 2, dw_count, "[%03X]=%08X HIT;[%03X]=%08X    ", prog_address, buf[i], prog_address + 1, buf[i + 1]);
10434c1c9b35SJohnny Huang 			otp_prog_dw(buf[i + 1], prog_address + 1, 0);
10444c1c9b35SJohnny Huang 		} else {
10454c1c9b35SJohnny Huang 			printProgress(i + 2, dw_count, "[%03X]=%08X    ;[%03X]=%08X    ", prog_address, buf[i], prog_address + 1, buf[i + 1]);
10464c1c9b35SJohnny Huang 			otp_prog_dw(buf[i], prog_address, 0);
10474c1c9b35SJohnny Huang 			otp_prog_dw(buf[i + 1], prog_address + 1, 0);
10484c1c9b35SJohnny Huang 		}
10494c1c9b35SJohnny Huang 
10504c1c9b35SJohnny Huang 		pass = 0;
10514c1c9b35SJohnny Huang 		for (k = 0; k < RETRY; k++) {
1052*a6d0d645SJohnny Huang 			if (verify_dw(prog_address, &buf[i], compare, 2) != 0) {
10534c1c9b35SJohnny Huang 				if (compare[0] != 0) {
10544c1c9b35SJohnny Huang 					otp_prog_dw(compare[0], prog_address, 1);
10554c1c9b35SJohnny Huang 				}
10564c1c9b35SJohnny Huang 				if (compare[1] != ~0) {
10574c1c9b35SJohnny Huang 					otp_prog_dw(compare[1], prog_address + 1, 1);
10584c1c9b35SJohnny Huang 				}
10594c1c9b35SJohnny Huang 			} else {
10604c1c9b35SJohnny Huang 				pass = 1;
10614c1c9b35SJohnny Huang 				break;
10624c1c9b35SJohnny Huang 			}
10634c1c9b35SJohnny Huang 		}
10644c1c9b35SJohnny Huang 
10654c1c9b35SJohnny Huang 		if (!pass)
10664c1c9b35SJohnny Huang 			goto fail;
10674c1c9b35SJohnny Huang 	}
10684c1c9b35SJohnny Huang 
10694c1c9b35SJohnny Huang 	if (!(addr_odd ^ count_odd))
10704c1c9b35SJohnny Huang 		goto pass;
10714c1c9b35SJohnny Huang 
10724c1c9b35SJohnny Huang 	prog_address = otp_addr + dw_count - 1;
10734c1c9b35SJohnny Huang 	if (data[dw_count - 1] != buf[dw_count - 1]) {
10744c1c9b35SJohnny Huang 		printProgress(dw_count, dw_count, "[%08X] = %08X                   ", prog_address, buf[dw_count - 1]);
10754c1c9b35SJohnny Huang 		otp_prog_dw(buf[dw_count - 1], prog_address, 0);
10764c1c9b35SJohnny Huang 		for (k = 0; k < RETRY; k++) {
1077*a6d0d645SJohnny Huang 			if (verify_dw(prog_address, &buf[dw_count - 1], compare, 1) != 0) {
10784c1c9b35SJohnny Huang 				otp_prog_dw(compare[0], prog_address, 1);
10794c1c9b35SJohnny Huang 			} else {
10804c1c9b35SJohnny Huang 				pass = 1;
10814c1c9b35SJohnny Huang 				break;
10824c1c9b35SJohnny Huang 			}
10834c1c9b35SJohnny Huang 		}
10844c1c9b35SJohnny Huang 		if (!pass)
10854c1c9b35SJohnny Huang 			goto fail;
10864c1c9b35SJohnny Huang 	} else {
10874c1c9b35SJohnny Huang 		printProgress(dw_count, dw_count, "[%08X] = %08X HIT               ", prog_address, buf[dw_count - 1]);
10884c1c9b35SJohnny Huang 	}
1089cd1610b4SJohnny Huang #endif
10904c1c9b35SJohnny Huang pass:
10914c1c9b35SJohnny Huang 	free(data);
109269d5fd8fSJohnny Huang 	return 0;
10934c1c9b35SJohnny Huang 
10944c1c9b35SJohnny Huang fail:
10954c1c9b35SJohnny Huang 	free(data);
10964c1c9b35SJohnny Huang 	return -1;
109769d5fd8fSJohnny Huang }
109869d5fd8fSJohnny Huang 
109969d5fd8fSJohnny Huang static int do_otp_prog(int mode, int addr, int otp_addr, int dw_count, int nconfirm)
110069d5fd8fSJohnny Huang {
110169d5fd8fSJohnny Huang 	int ret;
110269d5fd8fSJohnny Huang 	uint32_t *buf;
110369d5fd8fSJohnny Huang 
110469d5fd8fSJohnny Huang 	buf = map_physmem(addr, dw_count * 4, MAP_WRBACK);
110569d5fd8fSJohnny Huang 	if (!buf) {
110669d5fd8fSJohnny Huang 		puts("Failed to map physical memory\n");
110769d5fd8fSJohnny Huang 		return 1;
110869d5fd8fSJohnny Huang 	}
110969d5fd8fSJohnny Huang 	if (!nconfirm) {
1110*a6d0d645SJohnny Huang 		if (mode == OTP_REGION_CONF) {
111169d5fd8fSJohnny Huang 			if (otp_conf_parse(buf) < 0) {
111269d5fd8fSJohnny Huang 				printf("OTP config error, please check.\n");
111369d5fd8fSJohnny Huang 				return -1;
111469d5fd8fSJohnny Huang 			}
1115*a6d0d645SJohnny Huang 		} else if (mode == OTP_REGION_DATA) {
111669d5fd8fSJohnny Huang 			if (otp_data_parse(buf, dw_count) < 0) {
111769d5fd8fSJohnny Huang 				printf("OTP data error, please check.\n");
111869d5fd8fSJohnny Huang 				return -1;
111969d5fd8fSJohnny Huang 			}
1120*a6d0d645SJohnny Huang 		} else if (mode == OTP_REGION_STRAP) {
112169d5fd8fSJohnny Huang 			if (otp_strap_parse(buf) < 0) {
112269d5fd8fSJohnny Huang 				printf("OTP strap error, please check.\n");
112369d5fd8fSJohnny Huang 				return -1;
112469d5fd8fSJohnny Huang 			}
1125*a6d0d645SJohnny Huang 		} else if (mode == OTP_REGION_ALL) {
112669d5fd8fSJohnny Huang 			if (otp_conf_parse(buf) < 0) {
112769d5fd8fSJohnny Huang 				printf("OTP config error, please check.\n");
112869d5fd8fSJohnny Huang 				return -1;
112969d5fd8fSJohnny Huang 			}
113069d5fd8fSJohnny Huang 			if (otp_strap_parse(&buf[12]) < 0) {
113169d5fd8fSJohnny Huang 				printf("OTP strap error, please check.\n");
113269d5fd8fSJohnny Huang 				return -1;
113369d5fd8fSJohnny Huang 			}
113469d5fd8fSJohnny Huang 			if (otp_data_parse(&buf[18], dw_count - 18) < 0) {
113569d5fd8fSJohnny Huang 				printf("OTP data error, please check.\n");
113669d5fd8fSJohnny Huang 				return -1;
113769d5fd8fSJohnny Huang 			}
113869d5fd8fSJohnny Huang 		}
113969d5fd8fSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
114069d5fd8fSJohnny Huang 		if (!confirm_yesno()) {
114169d5fd8fSJohnny Huang 			printf(" Aborting\n");
114269d5fd8fSJohnny Huang 			return 1;
114369d5fd8fSJohnny Huang 		}
114469d5fd8fSJohnny Huang 	}
1145*a6d0d645SJohnny Huang 	if (mode == OTP_REGION_CONF) {
1146*a6d0d645SJohnny Huang 		return otp_prog_conf(buf);
1147*a6d0d645SJohnny Huang 	} else if (mode == OTP_REGION_STRAP) {
114869d5fd8fSJohnny Huang 		return otp_prog_strap(buf);
1149*a6d0d645SJohnny Huang 	} else if (mode == OTP_REGION_DATA) {
115069d5fd8fSJohnny Huang 		return otp_prog_data(buf, otp_addr, dw_count);
1151*a6d0d645SJohnny Huang 	} else if (mode == OTP_REGION_ALL) {
115269d5fd8fSJohnny Huang 		printf("programing data region ... ");
115369d5fd8fSJohnny Huang 		ret = otp_prog_data(&buf[16], 0, dw_count - 18);
115469d5fd8fSJohnny Huang 		if (ret < 0) {
115569d5fd8fSJohnny Huang 			printf("Error\n");
115669d5fd8fSJohnny Huang 			return ret;
115769d5fd8fSJohnny Huang 		} else {
115869d5fd8fSJohnny Huang 			printf("Done\n");
115969d5fd8fSJohnny Huang 		}
116069d5fd8fSJohnny Huang 		printf("programing strap region ... ");
116169d5fd8fSJohnny Huang 		ret = otp_prog_strap(&buf[12]);
116269d5fd8fSJohnny Huang 		if (ret < 0) {
116369d5fd8fSJohnny Huang 			printf("Error\n");
116469d5fd8fSJohnny Huang 			return ret;
116569d5fd8fSJohnny Huang 		} else {
116669d5fd8fSJohnny Huang 			printf("Done\n");
116769d5fd8fSJohnny Huang 		}
116869d5fd8fSJohnny Huang 		printf("programing configuration region ... ");
1169*a6d0d645SJohnny Huang 		ret = otp_prog_conf(buf);
117069d5fd8fSJohnny Huang 		if (ret < 0) {
117169d5fd8fSJohnny Huang 			printf("Error\n");
117269d5fd8fSJohnny Huang 			return ret;
117369d5fd8fSJohnny Huang 		}
117469d5fd8fSJohnny Huang 		printf("Done\n");
117569d5fd8fSJohnny Huang 		return ret;
117669d5fd8fSJohnny Huang 	}
117769d5fd8fSJohnny Huang 	return 0;
117869d5fd8fSJohnny Huang }
1179cd1610b4SJohnny Huang 
1180cd1610b4SJohnny Huang static int do_otp_prog_bit(int mode, int otp_dw_offset, int bit_offset, int value, int protect, int nconfirm)
1181cd1610b4SJohnny Huang {
1182cd1610b4SJohnny Huang 	uint32_t ret[2];
1183cd1610b4SJohnny Huang 	uint32_t strap_buf[6];
1184cd1610b4SJohnny Huang 	uint32_t prog_address;
1185cd1610b4SJohnny Huang 	struct otpstrap otpstrap[64];
1186cd1610b4SJohnny Huang 	int otp_bit;
1187cd1610b4SJohnny Huang 	int i;
1188cd1610b4SJohnny Huang 	int pass;
1189cd1610b4SJohnny Huang 
1190cd1610b4SJohnny Huang 	switch (mode) {
1191*a6d0d645SJohnny Huang 	case OTP_REGION_CONF:
1192cd1610b4SJohnny Huang 		otp_read_config(otp_dw_offset, ret);
1193cd1610b4SJohnny Huang 		prog_address = 0x800;
1194cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset / 8) * 0x200;
1195cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset % 8) * 0x2;
1196cd1610b4SJohnny Huang 		otp_bit = (ret[0] >> bit_offset) & 0x1;
1197cd1610b4SJohnny Huang 		if (otp_bit == value) {
1198cd1610b4SJohnny Huang 			printf("OTPCFG%X[%d] = %d\n", otp_dw_offset, bit_offset, value);
1199cd1610b4SJohnny Huang 			printf("No need to program\n");
1200cd1610b4SJohnny Huang 			return 0;
1201cd1610b4SJohnny Huang 		}
1202cd1610b4SJohnny Huang 		if (otp_bit == 1 && value == 0) {
1203cd1610b4SJohnny Huang 			printf("OTPCFG%X[%d] = 1\n", otp_dw_offset, bit_offset);
1204cd1610b4SJohnny Huang 			printf("OTP is programed, which can't be clean\n");
1205cd1610b4SJohnny Huang 			return -1;
1206cd1610b4SJohnny Huang 		}
1207cd1610b4SJohnny Huang 		printf("Program OTPCFG%X[%d] to 1\n", otp_dw_offset, bit_offset);
1208cd1610b4SJohnny Huang 		break;
1209*a6d0d645SJohnny Huang 	case OTP_REGION_DATA:
1210cd1610b4SJohnny Huang 		prog_address = otp_dw_offset;
1211cd1610b4SJohnny Huang 
1212cd1610b4SJohnny Huang 		if (otp_dw_offset % 2 == 0) {
1213cd1610b4SJohnny Huang 			otp_read_data(otp_dw_offset, ret);
1214cd1610b4SJohnny Huang 			otp_bit = (ret[0] >> bit_offset) & 0x1;
1215cd1610b4SJohnny Huang 		} else {
1216cd1610b4SJohnny Huang 			otp_read_data(otp_dw_offset - 1, ret);
1217cd1610b4SJohnny Huang 			otp_bit = (ret[1] >> bit_offset) & 0x1;
1218cd1610b4SJohnny Huang 		}
1219cd1610b4SJohnny Huang 		if (otp_bit == value) {
1220cd1610b4SJohnny Huang 			printf("OTPDATA%X[%d] = %d\n", otp_dw_offset, bit_offset, value);
1221cd1610b4SJohnny Huang 			printf("No need to program\n");
1222cd1610b4SJohnny Huang 			return 0;
1223cd1610b4SJohnny Huang 		}
1224cd1610b4SJohnny Huang 		if (otp_bit == 1 && value == 0) {
1225cd1610b4SJohnny Huang 			printf("OTPDATA%X[%d] = 1\n", otp_dw_offset, bit_offset);
1226cd1610b4SJohnny Huang 			printf("OTP is programed, which can't be clean\n");
1227cd1610b4SJohnny Huang 			return -1;
1228cd1610b4SJohnny Huang 		}
1229cd1610b4SJohnny Huang 		printf("Program OTPDATA%X[%d] to 1\n", otp_dw_offset, bit_offset);
1230cd1610b4SJohnny Huang 		break;
1231*a6d0d645SJohnny Huang 	case OTP_REGION_STRAP:
1232cd1610b4SJohnny Huang 		otp_strp_status(otpstrap);
1233cd1610b4SJohnny Huang 		otp_print_strap(bit_offset, 1);
1234cd1610b4SJohnny Huang 		if (bit_offset < 32) {
1235cd1610b4SJohnny Huang 			strap_buf[0] = value << bit_offset;
1236cd1610b4SJohnny Huang 			strap_buf[2] = ~BIT(bit_offset);
1237cd1610b4SJohnny Huang 			strap_buf[3] = ~0;
1238cd1610b4SJohnny Huang 			strap_buf[5] = 0;
1239cd1610b4SJohnny Huang 			if (protect)
1240cd1610b4SJohnny Huang 				strap_buf[4] = BIT(bit_offset);
1241cd1610b4SJohnny Huang 			else
1242cd1610b4SJohnny Huang 				strap_buf[4] = 0;
1243cd1610b4SJohnny Huang 		} else {
1244cd1610b4SJohnny Huang 			strap_buf[1] = value << (bit_offset - 32);
1245cd1610b4SJohnny Huang 			strap_buf[2] = ~0;
1246cd1610b4SJohnny Huang 			strap_buf[3] = ~BIT(bit_offset - 32);
1247cd1610b4SJohnny Huang 			strap_buf[4] = 0;
1248cd1610b4SJohnny Huang 			if (protect)
1249cd1610b4SJohnny Huang 				strap_buf[5] = BIT(bit_offset - 32);
1250cd1610b4SJohnny Huang 			else
1251cd1610b4SJohnny Huang 				strap_buf[5] = 0;
1252cd1610b4SJohnny Huang 		}
1253cd1610b4SJohnny Huang 		if (otp_strap_parse(strap_buf) < 0)
1254cd1610b4SJohnny Huang 			return -1;
1255cd1610b4SJohnny Huang 		break;
1256cd1610b4SJohnny Huang 	}
1257cd1610b4SJohnny Huang 
1258cd1610b4SJohnny Huang 	if (!nconfirm) {
1259cd1610b4SJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
1260cd1610b4SJohnny Huang 		if (!confirm_yesno()) {
1261cd1610b4SJohnny Huang 			printf(" Aborting\n");
1262cd1610b4SJohnny Huang 			return 1;
1263cd1610b4SJohnny Huang 		}
1264cd1610b4SJohnny Huang 	}
1265cd1610b4SJohnny Huang 
1266cd1610b4SJohnny Huang 	switch (mode) {
1267*a6d0d645SJohnny Huang 	case OTP_REGION_STRAP:
1268cd1610b4SJohnny Huang 		return otp_prog_strap(strap_buf);
1269*a6d0d645SJohnny Huang 	case OTP_REGION_CONF:
1270*a6d0d645SJohnny Huang 	case OTP_REGION_DATA:
1271cd1610b4SJohnny Huang 		otp_prog_bit(value, prog_address, bit_offset, 0);
1272cd1610b4SJohnny Huang 		pass = -1;
1273cd1610b4SJohnny Huang 		for (i = 0; i < RETRY; i++) {
1274*a6d0d645SJohnny Huang 			if (verify_bit(prog_address, bit_offset, value) != 0) {
1275cd1610b4SJohnny Huang 				otp_prog_bit(value, prog_address, bit_offset, 1);
1276cd1610b4SJohnny Huang 			} else {
1277cd1610b4SJohnny Huang 				pass = 0;
1278cd1610b4SJohnny Huang 				break;
1279cd1610b4SJohnny Huang 			}
1280cd1610b4SJohnny Huang 		}
1281cd1610b4SJohnny Huang 		return pass;
1282cd1610b4SJohnny Huang 	}
1283cd1610b4SJohnny Huang 
1284cd1610b4SJohnny Huang 	return -1;
1285cd1610b4SJohnny Huang }
1286cd1610b4SJohnny Huang 
128769d5fd8fSJohnny Huang static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc,
128869d5fd8fSJohnny Huang 		      char *const argv[])
128969d5fd8fSJohnny Huang {
129069d5fd8fSJohnny Huang 	char *cmd;
129169d5fd8fSJohnny Huang 	int mode = 0;
129269d5fd8fSJohnny Huang 	int nconfirm = 0;
1293cd1610b4SJohnny Huang 	uint32_t addr = 0;
1294cd1610b4SJohnny Huang 	uint32_t otp_addr = 0;
1295cd1610b4SJohnny Huang 	int dw_count = 0;
1296cd1610b4SJohnny Huang 	int bit_offset = 0;
1297cd1610b4SJohnny Huang 	int value = 0;
1298cd1610b4SJohnny Huang 	int protect = 0;
129969d5fd8fSJohnny Huang 
130069d5fd8fSJohnny Huang 
130169d5fd8fSJohnny Huang 
130269d5fd8fSJohnny Huang 	if (argc < 2) {
130369d5fd8fSJohnny Huang usage:
130469d5fd8fSJohnny Huang 		return CMD_RET_USAGE;
130569d5fd8fSJohnny Huang 	}
130669d5fd8fSJohnny Huang 
130769d5fd8fSJohnny Huang 	cmd = argv[1];
130869d5fd8fSJohnny Huang 	if (!strcmp(cmd, "read")) {
130969d5fd8fSJohnny Huang 		if (!strcmp(argv[2], "conf"))
1310*a6d0d645SJohnny Huang 			mode = OTP_REGION_CONF;
131169d5fd8fSJohnny Huang 		else if (!strcmp(argv[2], "data"))
1312*a6d0d645SJohnny Huang 			mode = OTP_REGION_DATA;
131369d5fd8fSJohnny Huang 		else if (!strcmp(argv[2], "strap"))
1314*a6d0d645SJohnny Huang 			mode = OTP_REGION_STRAP;
131569d5fd8fSJohnny Huang 		else
131669d5fd8fSJohnny Huang 			goto usage;
131769d5fd8fSJohnny Huang 
131869d5fd8fSJohnny Huang 		writel(OTP_PASSWD, 0x1e6f2000); //password
131969d5fd8fSJohnny Huang 		otp_addr = simple_strtoul(argv[3], NULL, 16);
132069d5fd8fSJohnny Huang 		dw_count = simple_strtoul(argv[4], NULL, 16);
1321*a6d0d645SJohnny Huang 		if (mode == OTP_REGION_CONF) {
132269d5fd8fSJohnny Huang 			otp_print_config(otp_addr, dw_count);
1323*a6d0d645SJohnny Huang 		} else if (mode == OTP_REGION_DATA) {
132469d5fd8fSJohnny Huang 			otp_print_data(otp_addr, dw_count);
1325*a6d0d645SJohnny Huang 		} else if (mode == OTP_REGION_STRAP) {
1326cd1610b4SJohnny Huang 			otp_print_strap(0, 64);
132769d5fd8fSJohnny Huang 		}
132869d5fd8fSJohnny Huang 	} else if (!strcmp(cmd, "prog")) {
132969d5fd8fSJohnny Huang 		if (!strcmp(argv[2], "conf"))
1330*a6d0d645SJohnny Huang 			mode = OTP_REGION_CONF;
133169d5fd8fSJohnny Huang 		else if (!strcmp(argv[2], "strap"))
1332*a6d0d645SJohnny Huang 			mode = OTP_REGION_STRAP;
133369d5fd8fSJohnny Huang 		else if (!strcmp(argv[2], "data"))
1334*a6d0d645SJohnny Huang 			mode = OTP_REGION_DATA;
133569d5fd8fSJohnny Huang 		else if (!strcmp(argv[2], "all"))
1336*a6d0d645SJohnny Huang 			mode = OTP_REGION_ALL;
133769d5fd8fSJohnny Huang 		else
133869d5fd8fSJohnny Huang 			goto usage;
133969d5fd8fSJohnny Huang 
134069d5fd8fSJohnny Huang 		if (!strcmp(argv[3], "f"))
134169d5fd8fSJohnny Huang 			nconfirm = 1;
134269d5fd8fSJohnny Huang 		writel(OTP_PASSWD, 0x1e6f2000); //password
134369d5fd8fSJohnny Huang 		addr = simple_strtoul(argv[3 + nconfirm], NULL, 16);
134469d5fd8fSJohnny Huang 		otp_addr = simple_strtoul(argv[4 + nconfirm], NULL, 16);
134569d5fd8fSJohnny Huang 		dw_count = simple_strtoul(argv[5 + nconfirm], NULL, 16);
134669d5fd8fSJohnny Huang 		return do_otp_prog(mode, addr, otp_addr, dw_count, nconfirm);
1347cd1610b4SJohnny Huang 	} else if (!strcmp(cmd, "pb")) {
1348cd1610b4SJohnny Huang 		if (!strcmp(argv[2], "conf"))
1349*a6d0d645SJohnny Huang 			mode = OTP_REGION_CONF;
1350cd1610b4SJohnny Huang 		else if (!strcmp(argv[2], "strap"))
1351*a6d0d645SJohnny Huang 			mode = OTP_REGION_STRAP;
1352cd1610b4SJohnny Huang 		else if (!strcmp(argv[2], "data"))
1353*a6d0d645SJohnny Huang 			mode = OTP_REGION_DATA;
1354cd1610b4SJohnny Huang 		else
1355cd1610b4SJohnny Huang 			goto usage;
1356cd1610b4SJohnny Huang 		if (!strcmp(argv[3], "f"))
1357cd1610b4SJohnny Huang 			nconfirm = 1;
1358cd1610b4SJohnny Huang 
1359*a6d0d645SJohnny Huang 		if (mode == OTP_REGION_STRAP) {
1360cd1610b4SJohnny Huang 			bit_offset = simple_strtoul(argv[3 + nconfirm], NULL, 16);
1361cd1610b4SJohnny Huang 			value = simple_strtoul(argv[4 + nconfirm], NULL, 16);
1362cd1610b4SJohnny Huang 			protect = simple_strtoul(argv[5 + nconfirm], NULL, 16);
1363cd1610b4SJohnny Huang 			if (bit_offset >= 64)
1364cd1610b4SJohnny Huang 				return -1;
1365cd1610b4SJohnny Huang 		} else {
1366cd1610b4SJohnny Huang 			otp_addr = simple_strtoul(argv[3 + nconfirm], NULL, 16);
1367cd1610b4SJohnny Huang 			bit_offset = simple_strtoul(argv[4 + nconfirm], NULL, 16);
1368cd1610b4SJohnny Huang 			value = simple_strtoul(argv[5 + nconfirm], NULL, 16);
1369cd1610b4SJohnny Huang 			if (bit_offset >= 32)
1370cd1610b4SJohnny Huang 				return -1;
1371cd1610b4SJohnny Huang 		}
1372cd1610b4SJohnny Huang 		if (value != 0 && value != 1)
1373cd1610b4SJohnny Huang 			return -1;
1374cd1610b4SJohnny Huang 
1375cd1610b4SJohnny Huang 		writel(OTP_PASSWD, 0x1e6f2000); //password
1376cd1610b4SJohnny Huang 		return do_otp_prog_bit(mode, otp_addr, bit_offset, value, protect, nconfirm);
137769d5fd8fSJohnny Huang 	} else if (!strcmp(cmd, "comp")) {
137869d5fd8fSJohnny Huang 		writel(OTP_PASSWD, 0x1e6f2000); //password
137969d5fd8fSJohnny Huang 		addr = simple_strtoul(argv[2], NULL, 16);
138069d5fd8fSJohnny Huang 		otp_addr = simple_strtoul(argv[3], NULL, 16);
138169d5fd8fSJohnny Huang 		if (otp_compare(otp_addr, addr) >= 0) {
138269d5fd8fSJohnny Huang 			printf("Compare pass\n");
138369d5fd8fSJohnny Huang 		} else {
138469d5fd8fSJohnny Huang 			printf("Compare fail\n");
138569d5fd8fSJohnny Huang 		}
138669d5fd8fSJohnny Huang 	} else {
138769d5fd8fSJohnny Huang 		goto usage;
138869d5fd8fSJohnny Huang 	}
138969d5fd8fSJohnny Huang 
139069d5fd8fSJohnny Huang 
139169d5fd8fSJohnny Huang 	return 0;
139269d5fd8fSJohnny Huang }
139369d5fd8fSJohnny Huang 
139469d5fd8fSJohnny Huang 
139569d5fd8fSJohnny Huang U_BOOT_CMD(
139669d5fd8fSJohnny Huang 	otp, 7, 0,  do_ast_otp,
139769d5fd8fSJohnny Huang 	"ASPEED One-Time-Programmable sub-system",
1398cd1610b4SJohnny Huang 	"read conf|strap|data <otp_dw_offset> <dw_count>\n"
1399cd1610b4SJohnny Huang 	"otp prog conf|strap|data|all [f] <addr> <otp_dw_offset> <dw_count>\n"
1400cd1610b4SJohnny Huang 	"otp pb conf|data [f] <otp_dw_offset> <bit_offset> <value>\n"
1401cd1610b4SJohnny Huang 	"otp pb strap [f] <bit_offset> <value> <protect>\n"
1402cd1610b4SJohnny Huang 	"otp comp <addr> <otp_dw_offset>\n"
140369d5fd8fSJohnny Huang );
1404