xref: /openbmc/u-boot/cmd/otp.c (revision 83655e91b4ef6ca5981be7135f5e4d12d437fd67)
169d5fd8fSJohnny Huang /*
269d5fd8fSJohnny Huang  *  This program is distributed in the hope that it will be useful,
369d5fd8fSJohnny Huang  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
469d5fd8fSJohnny Huang  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
569d5fd8fSJohnny Huang  *  GNU General Public License for more details.
669d5fd8fSJohnny Huang  *
769d5fd8fSJohnny Huang  *  You should have received a copy of the GNU General Public License
869d5fd8fSJohnny Huang  *  along with this program; if not, write to the Free Software
969d5fd8fSJohnny Huang  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1069d5fd8fSJohnny Huang  */
114c1c9b35SJohnny Huang #include <stdlib.h>
1269d5fd8fSJohnny Huang #include <common.h>
1369d5fd8fSJohnny Huang #include <console.h>
1469d5fd8fSJohnny Huang #include <bootretry.h>
1569d5fd8fSJohnny Huang #include <cli.h>
1669d5fd8fSJohnny Huang #include <command.h>
1769d5fd8fSJohnny Huang #include <console.h>
184c1c9b35SJohnny Huang #include <malloc.h>
1969d5fd8fSJohnny Huang #include <inttypes.h>
2069d5fd8fSJohnny Huang #include <mapmem.h>
2169d5fd8fSJohnny Huang #include <asm/io.h>
2269d5fd8fSJohnny Huang #include <linux/compiler.h>
23696656c6SJohnny Huang #include <u-boot/sha256.h>
2469d5fd8fSJohnny Huang 
2569d5fd8fSJohnny Huang DECLARE_GLOBAL_DATA_PTR;
2669d5fd8fSJohnny Huang 
2769d5fd8fSJohnny Huang #define OTP_PASSWD			0x349fe38a
2869d5fd8fSJohnny Huang #define RETRY				3
297332532cSJohnny Huang #define OTP_REGION_STRAP		BIT(0)
307332532cSJohnny Huang #define OTP_REGION_CONF			BIT(1)
317332532cSJohnny Huang #define OTP_REGION_DATA			BIT(2)
3269d5fd8fSJohnny Huang 
332a856b9aSJohnny Huang #define OTP_USAGE			-1
342a856b9aSJohnny Huang #define OTP_FAILURE			-2
352a856b9aSJohnny Huang #define OTP_SUCCESS			0
362a856b9aSJohnny Huang 
37a6af4a17SJohnny Huang #define OTP_PROG_SKIP			1
38a6af4a17SJohnny Huang 
399a4fe690SJohnny Huang #define OTP_KEY_TYPE_RSA		1
409a4fe690SJohnny Huang #define OTP_KEY_TYPE_AES		2
419a4fe690SJohnny Huang #define OTP_KEY_TYPE_VAULT		3
429a4fe690SJohnny Huang #define OTP_KEY_TYPE_HMAC		4
439a4fe690SJohnny Huang 
449a4fe690SJohnny Huang 
45a8bd6d8cSJohnny Huang #define OTP_REG_RESERVED		-1
46b458cd62SJohnny Huang #define OTP_REG_VALUE			-2
47b458cd62SJohnny Huang #define OTP_REG_VALID_BIT		-3
4876d13988SJohnny Huang 
494c1c9b35SJohnny Huang #define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
504c1c9b35SJohnny Huang #define PBWIDTH 60
514c1c9b35SJohnny Huang 
523d3688adSJohnny Huang #define OTP_BASE		0x1e6f2000
533d3688adSJohnny Huang #define OTP_PROTECT_KEY		OTP_BASE
543d3688adSJohnny Huang #define OTP_COMMAND		OTP_BASE + 0x4
553d3688adSJohnny Huang #define OTP_TIMING		OTP_BASE + 0x8
563d3688adSJohnny Huang #define OTP_ADDR		OTP_BASE + 0x10
573d3688adSJohnny Huang #define OTP_STATUS		OTP_BASE + 0x14
583d3688adSJohnny Huang #define OTP_COMPARE_1		OTP_BASE + 0x20
593d3688adSJohnny Huang #define OTP_COMPARE_2		OTP_BASE + 0x24
603d3688adSJohnny Huang #define OTP_COMPARE_3		OTP_BASE + 0x28
613d3688adSJohnny Huang #define OTP_COMPARE_4		OTP_BASE + 0x2c
623d3688adSJohnny Huang 
63696656c6SJohnny Huang #define OTP_MAGIC		"SOCOTP"
64696656c6SJohnny Huang #define CHECKSUM_LEN		32
65696656c6SJohnny Huang #define OTP_INC_DATA		(1 << 31)
66696656c6SJohnny Huang #define OTP_INC_CONFIG		(1 << 30)
67696656c6SJohnny Huang #define OTP_INC_STRAP		(1 << 29)
68696656c6SJohnny Huang #define OTP_ECC_EN		(1 << 28)
69696656c6SJohnny Huang #define OTP_REGION_SIZE(info)	((info >> 16) & 0xffff)
70696656c6SJohnny Huang #define OTP_REGION_OFFSET(info)	(info & 0xffff)
71696656c6SJohnny Huang #define OTP_IMAGE_SIZE(info)	(info & 0xffff)
72696656c6SJohnny Huang 
73696656c6SJohnny Huang #define OTP_AST2600A0		0
74696656c6SJohnny Huang #define OTP_AST2600A1		1
75696656c6SJohnny Huang 
76696656c6SJohnny Huang struct otp_header {
77696656c6SJohnny Huang 	u8	otp_magic[8];
78696656c6SJohnny Huang 	u8	otp_version[8];
79696656c6SJohnny Huang 	u32	image_info;
80696656c6SJohnny Huang 	u32	data_info;
81696656c6SJohnny Huang 	u32	config_info;
82696656c6SJohnny Huang 	u32	strap_info;
83696656c6SJohnny Huang 	u32	checksum_offset;
84696656c6SJohnny Huang } __attribute__((packed));
85696656c6SJohnny Huang 
8666f2f8e5SJohnny Huang struct otpstrap_status {
8769d5fd8fSJohnny Huang 	int value;
8869d5fd8fSJohnny Huang 	int option_array[7];
8969d5fd8fSJohnny Huang 	int remain_times;
9069d5fd8fSJohnny Huang 	int writeable_option;
915010032bSJohnny Huang 	int reg_protected;
9269d5fd8fSJohnny Huang 	int protected;
9369d5fd8fSJohnny Huang };
9469d5fd8fSJohnny Huang 
9566f2f8e5SJohnny Huang struct otpconf_parse {
9666f2f8e5SJohnny Huang 	int dw_offset;
9766f2f8e5SJohnny Huang 	int bit;
9866f2f8e5SJohnny Huang 	int length;
9966f2f8e5SJohnny Huang 	int value;
100696656c6SJohnny Huang 	int ignore;
10166f2f8e5SJohnny Huang 	char status[80];
10266f2f8e5SJohnny Huang };
10366f2f8e5SJohnny Huang 
104a8bd6d8cSJohnny Huang struct otpstrap_info {
10579e42a59SJoel Stanley 	int8_t bit_offset;
10679e42a59SJoel Stanley 	int8_t length;
10779e42a59SJoel Stanley 	int8_t value;
10879e42a59SJoel Stanley 	char *information;
109a8bd6d8cSJohnny Huang };
110a8bd6d8cSJohnny Huang 
111a8bd6d8cSJohnny Huang struct otpconf_info {
11279e42a59SJoel Stanley 	int8_t dw_offset;
11379e42a59SJoel Stanley 	int8_t bit_offset;
11479e42a59SJoel Stanley 	int8_t length;
11579e42a59SJoel Stanley 	int8_t value;
11679e42a59SJoel Stanley 	char *information;
117a8bd6d8cSJohnny Huang };
118a8bd6d8cSJohnny Huang 
1199a4fe690SJohnny Huang struct otpkey_type {
1209a4fe690SJohnny Huang 	int value;
1219a4fe690SJohnny Huang 	int key_type;
1229a4fe690SJohnny Huang 	int need_id;
1239a4fe690SJohnny Huang 	char information[110];
1249a4fe690SJohnny Huang };
1259a4fe690SJohnny Huang 
1269a4fe690SJohnny Huang struct otp_info_cb {
1279a4fe690SJohnny Huang 	int version;
12879e42a59SJoel Stanley 	const struct otpstrap_info *strap_info;
1299a4fe690SJohnny Huang 	int strap_info_len;
13079e42a59SJoel Stanley 	const struct otpconf_info *conf_info;
1319a4fe690SJohnny Huang 	int conf_info_len;
13279e42a59SJoel Stanley 	const struct otpkey_type *key_info;
1339a4fe690SJohnny Huang 	int key_info_len;
1345010032bSJohnny Huang 
1359a4fe690SJohnny Huang };
1369a4fe690SJohnny Huang 
137696656c6SJohnny Huang struct otp_image_layout {
1385010032bSJohnny Huang 	int data_length;
1395010032bSJohnny Huang 	int conf_length;
1405010032bSJohnny Huang 	int strap_length;
141696656c6SJohnny Huang 	uint8_t *data;
142696656c6SJohnny Huang 	uint8_t *data_ignore;
143696656c6SJohnny Huang 	uint8_t *conf;
144696656c6SJohnny Huang 	uint8_t *conf_ignore;
145696656c6SJohnny Huang 	uint8_t *strap;
146696656c6SJohnny Huang 	uint8_t *strap_reg_pro;
147696656c6SJohnny Huang 	uint8_t *strap_pro;
148696656c6SJohnny Huang 	uint8_t *strap_ignore;
149696656c6SJohnny Huang };
150696656c6SJohnny Huang 
1519a4fe690SJohnny Huang static struct otp_info_cb info_cb;
1529a4fe690SJohnny Huang 
15379e42a59SJoel Stanley static const struct otpstrap_info a0_strap_info[] = {
15491448c03SJohnny Huang 	{ 0, 1, 0, "Disable secure boot" },
15591448c03SJohnny Huang 	{ 0, 1, 1, "Enable secure boot"	},
15691448c03SJohnny Huang 	{ 1, 1, 0, "Disable boot from eMMC" },
15791448c03SJohnny Huang 	{ 1, 1, 1, "Enable boot from eMMC" },
15891448c03SJohnny Huang 	{ 2, 1, 0, "Disable Boot from debug SPI" },
15991448c03SJohnny Huang 	{ 2, 1, 1, "Enable Boot from debug SPI" },
16091448c03SJohnny Huang 	{ 3, 1, 0, "Enable ARM CM3" },
16191448c03SJohnny Huang 	{ 3, 1, 1, "Disable ARM CM3" },
162541eb887SJohnny Huang 	{ 4, 1, 0, "No VGA BIOS ROM, VGA BIOS is merged in the system BIOS" },
16391448c03SJohnny Huang 	{ 4, 1, 1, "Enable dedicated VGA BIOS ROM" },
16491448c03SJohnny Huang 	{ 5, 1, 0, "MAC 1 : RMII/NCSI" },
16591448c03SJohnny Huang 	{ 5, 1, 1, "MAC 1 : RGMII" },
16691448c03SJohnny Huang 	{ 6, 1, 0, "MAC 2 : RMII/NCSI" },
16791448c03SJohnny Huang 	{ 6, 1, 1, "MAC 2 : RGMII" },
16891448c03SJohnny Huang 	{ 7, 3, 0, "CPU Frequency : 1GHz" },
16991448c03SJohnny Huang 	{ 7, 3, 1, "CPU Frequency : 800MHz" },
17091448c03SJohnny Huang 	{ 7, 3, 2, "CPU Frequency : 1.2GHz" },
17191448c03SJohnny Huang 	{ 7, 3, 3, "CPU Frequency : 1.4GHz" },
17291448c03SJohnny Huang 	{ 10, 2, 0, "HCLK ratio AXI:AHB = 2:1" },
17391448c03SJohnny Huang 	{ 10, 2, 1, "HCLK ratio AXI:AHB = 2:1" },
17491448c03SJohnny Huang 	{ 10, 2, 2, "HCLK ratio AXI:AHB = 3:1" },
17591448c03SJohnny Huang 	{ 10, 2, 3, "HCLK ratio AXI:AHB = 4:1" },
17691448c03SJohnny Huang 	{ 12, 2, 0, "VGA memory size : 8MB" },
17791448c03SJohnny Huang 	{ 12, 2, 1, "VGA memory size : 16MB" },
17891448c03SJohnny Huang 	{ 12, 2, 2, "VGA memory size : 32MB" },
17991448c03SJohnny Huang 	{ 12, 2, 3, "VGA memory size : 64MB" },
18091448c03SJohnny Huang 	{ 14, 3, OTP_REG_RESERVED, "" },
18191448c03SJohnny Huang 	{ 17, 1, 0, "VGA class code : Class Code for video device" },
18291448c03SJohnny Huang 	{ 17, 1, 1, "VGA class code : Class Code for VGA device" },
18391448c03SJohnny Huang 	{ 18, 1, 0, "Enable debug interfaces 0" },
18491448c03SJohnny Huang 	{ 18, 1, 1, "Disable debug interfaces 0" },
18591448c03SJohnny Huang 	{ 19, 1, 0, "Boot from emmc mode : High eMMC speed" },
18691448c03SJohnny Huang 	{ 19, 1, 1, "Boot from emmc mode : Normal eMMC speed" },
18791448c03SJohnny Huang 	{ 20, 1, 0, "Enable Pcie EHCI device" },
18891448c03SJohnny Huang 	{ 20, 1, 1, "Disable Pcie EHCI device" },
18991448c03SJohnny Huang 	{ 21, 1, 0, "Enable VGA XDMA function" },
19091448c03SJohnny Huang 	{ 21, 1, 1, "Disable VGA XDMA function" },
19191448c03SJohnny Huang 	{ 22, 1, 0, "Normal BMC mode" },
19291448c03SJohnny Huang 	{ 22, 1, 1, "Disable dedicated BMC functions for non-BMC application" },
19391448c03SJohnny Huang 	{ 23, 1, 0, "SSPRST# pin is for secondary processor dedicated reset pin" },
19491448c03SJohnny Huang 	{ 23, 1, 1, "SSPRST# pin is for PCIE root complex dedicated reset pin" },
19591448c03SJohnny Huang 	{ 24, 1, 0, "DRAM types : DDR4" },
19691448c03SJohnny Huang 	{ 24, 1, 1, "DRAM types : DDR3" },
19791448c03SJohnny Huang 	{ 25, 5, OTP_REG_RESERVED, "" },
19891448c03SJohnny Huang 	{ 30, 2, OTP_REG_RESERVED, "" },
19991448c03SJohnny Huang 	{ 32, 1, 0, "MAC 3 : RMII/NCSI" },
20091448c03SJohnny Huang 	{ 32, 1, 1, "MAC 3 : RGMII" },
20191448c03SJohnny Huang 	{ 33, 1, 0, "MAC 4 : RMII/NCSI" },
20291448c03SJohnny Huang 	{ 33, 1, 1, "MAC 4 : RGMII" },
20391448c03SJohnny Huang 	{ 34, 1, 0, "SuperIO configuration address : 0x2E" },
20491448c03SJohnny Huang 	{ 34, 1, 1, "SuperIO configuration address : 0x4E" },
20591448c03SJohnny Huang 	{ 35, 1, 0, "Enable LPC to decode SuperIO" },
20691448c03SJohnny Huang 	{ 35, 1, 1, "Disable LPC to decode SuperIO" },
20791448c03SJohnny Huang 	{ 36, 1, 0, "Enable debug interfaces 1" },
20891448c03SJohnny Huang 	{ 36, 1, 1, "Disable debug interfaces 1" },
20991448c03SJohnny Huang 	{ 37, 1, 0, "Disable ACPI function" },
21091448c03SJohnny Huang 	{ 37, 1, 1, "Enable ACPI function" },
21191448c03SJohnny Huang 	{ 38, 1, 0, "Enable eSPI mode" },
21291448c03SJohnny Huang 	{ 38, 1, 1, "Enable LPC mode" },
21391448c03SJohnny Huang 	{ 39, 1, 0, "Enable SAFS mode" },
21491448c03SJohnny Huang 	{ 39, 1, 1, "Enable SAFS mode" },
21591448c03SJohnny Huang 	{ 40, 2, OTP_REG_RESERVED, "" },
21691448c03SJohnny Huang 	{ 42, 1, 0, "Disable boot SPI 3B/4B address mode auto detection" },
21791448c03SJohnny Huang 	{ 42, 1, 1, "Enable boot SPI 3B/4B address mode auto detection" },
21891448c03SJohnny Huang 	{ 43, 1, 0, "Disable boot SPI ABR" },
21991448c03SJohnny Huang 	{ 43, 1, 1, "Enable boot SPI ABR" },
22091448c03SJohnny Huang 	{ 44, 1, 0, "Boot SPI ABR mode : dual SPI flash" },
22191448c03SJohnny Huang 	{ 44, 1, 1, "Boot SPI ABR mode : single SPI flash" },
22291448c03SJohnny Huang 	{ 45, 3, 0, "Boot SPI flash size : no define size" },
22391448c03SJohnny Huang 	{ 45, 3, 1, "Boot SPI flash size : 2MB" },
22491448c03SJohnny Huang 	{ 45, 3, 2, "Boot SPI flash size : 4MB" },
22591448c03SJohnny Huang 	{ 45, 3, 3, "Boot SPI flash size : 8MB" },
22691448c03SJohnny Huang 	{ 45, 3, 4, "Boot SPI flash size : 16MB" },
22791448c03SJohnny Huang 	{ 45, 3, 5, "Boot SPI flash size : 32MB" },
22891448c03SJohnny Huang 	{ 45, 3, 6, "Boot SPI flash size : 64MB" },
22991448c03SJohnny Huang 	{ 45, 3, 7, "Boot SPI flash size : 128MB" },
23091448c03SJohnny Huang 	{ 48, 1, 0, "Disable host SPI ABR" },
23191448c03SJohnny Huang 	{ 48, 1, 1, "Enable host SPI ABR" },
23291448c03SJohnny Huang 	{ 49, 1, 0, "Disable host SPI ABR mode select pin" },
23391448c03SJohnny Huang 	{ 49, 1, 1, "Enable host SPI ABR mode select pin" },
23491448c03SJohnny Huang 	{ 50, 1, 0, "Host SPI ABR mode : dual SPI flash" },
23591448c03SJohnny Huang 	{ 50, 1, 1, "Host SPI ABR mode : single SPI flash" },
23691448c03SJohnny Huang 	{ 51, 3, 0, "Host SPI flash size : no define size" },
23791448c03SJohnny Huang 	{ 51, 3, 1, "Host SPI flash size : 2MB" },
23891448c03SJohnny Huang 	{ 51, 3, 2, "Host SPI flash size : 4MB" },
23991448c03SJohnny Huang 	{ 51, 3, 3, "Host SPI flash size : 8MB" },
24091448c03SJohnny Huang 	{ 51, 3, 4, "Host SPI flash size : 16MB" },
24191448c03SJohnny Huang 	{ 51, 3, 5, "Host SPI flash size : 32MB" },
24291448c03SJohnny Huang 	{ 51, 3, 6, "Host SPI flash size : 64MB" },
24391448c03SJohnny Huang 	{ 51, 3, 7, "Host SPI flash size : 128MB" },
24491448c03SJohnny Huang 	{ 54, 1, 0, "Disable boot SPI auxiliary control pins" },
24591448c03SJohnny Huang 	{ 54, 1, 1, "Enable boot SPI auxiliary control pins" },
24691448c03SJohnny Huang 	{ 55, 2, 0, "Boot SPI CRTM size : disable CRTM" },
24791448c03SJohnny Huang 	{ 55, 2, 1, "Boot SPI CRTM size : 256KB" },
24891448c03SJohnny Huang 	{ 55, 2, 2, "Boot SPI CRTM size : 512KB" },
24991448c03SJohnny Huang 	{ 55, 2, 3, "Boot SPI CRTM size : 1MB" },
25091448c03SJohnny Huang 	{ 57, 2, 0, "Host SPI CRTM size : disable CRTM" },
25191448c03SJohnny Huang 	{ 57, 2, 1, "Host SPI CRTM size : 256KB" },
25291448c03SJohnny Huang 	{ 57, 2, 2, "Host SPI CRTM size : 512KB" },
25391448c03SJohnny Huang 	{ 57, 2, 3, "Host SPI CRTM size : 1MB" },
25491448c03SJohnny Huang 	{ 59, 1, 0, "Disable host SPI auxiliary control pins" },
25591448c03SJohnny Huang 	{ 59, 1, 1, "Enable host SPI auxiliary control pins" },
25691448c03SJohnny Huang 	{ 60, 1, 0, "Disable GPIO pass through" },
25791448c03SJohnny Huang 	{ 60, 1, 1, "Enable GPIO pass through" },
25891448c03SJohnny Huang 	{ 61, 1, 0, "Enable low security secure boot key" },
25991448c03SJohnny Huang 	{ 61, 1, 1, "Disable low security secure boot key" },
26091448c03SJohnny Huang 	{ 62, 1, 0, "Disable dedicate GPIO strap pins" },
26191448c03SJohnny Huang 	{ 62, 1, 1, "Enable dedicate GPIO strap pins" },
26291448c03SJohnny Huang 	{ 63, 1, OTP_REG_RESERVED, "" }
26376d13988SJohnny Huang };
2649a4fe690SJohnny Huang 
26579e42a59SJoel Stanley static const struct otpstrap_info a1_strap_info[] = {
2663cb28812SJohnny Huang 	{ 0, 1, 0, "Disable secure boot" },
2673cb28812SJohnny Huang 	{ 0, 1, 1, "Enable secure boot"	},
2683cb28812SJohnny Huang 	{ 1, 1, 0, "Disable boot from eMMC" },
2693cb28812SJohnny Huang 	{ 1, 1, 1, "Enable boot from eMMC" },
2703cb28812SJohnny Huang 	{ 2, 1, 0, "Disable Boot from debug SPI" },
2713cb28812SJohnny Huang 	{ 2, 1, 1, "Enable Boot from debug SPI" },
2723cb28812SJohnny Huang 	{ 3, 1, 0, "Enable ARM CM3" },
2733cb28812SJohnny Huang 	{ 3, 1, 1, "Disable ARM CM3" },
2743cb28812SJohnny Huang 	{ 4, 1, 0, "No VGA BIOS ROM, VGA BIOS is merged in the system BIOS" },
2753cb28812SJohnny Huang 	{ 4, 1, 1, "Enable dedicated VGA BIOS ROM" },
2763cb28812SJohnny Huang 	{ 5, 1, 0, "MAC 1 : RMII/NCSI" },
2773cb28812SJohnny Huang 	{ 5, 1, 1, "MAC 1 : RGMII" },
2783cb28812SJohnny Huang 	{ 6, 1, 0, "MAC 2 : RMII/NCSI" },
2793cb28812SJohnny Huang 	{ 6, 1, 1, "MAC 2 : RGMII" },
2803cb28812SJohnny Huang 	{ 7, 3, 0, "CPU Frequency : 1GHz" },
2813cb28812SJohnny Huang 	{ 7, 3, 1, "CPU Frequency : 800MHz" },
2823cb28812SJohnny Huang 	{ 7, 3, 2, "CPU Frequency : 1.2GHz" },
2833cb28812SJohnny Huang 	{ 7, 3, 3, "CPU Frequency : 1.4GHz" },
2843cb28812SJohnny Huang 	{ 10, 2, 0, "HCLK ratio AXI:AHB = 2:1" },
2853cb28812SJohnny Huang 	{ 10, 2, 1, "HCLK ratio AXI:AHB = 2:1" },
2863cb28812SJohnny Huang 	{ 10, 2, 2, "HCLK ratio AXI:AHB = 3:1" },
2873cb28812SJohnny Huang 	{ 10, 2, 3, "HCLK ratio AXI:AHB = 4:1" },
2883cb28812SJohnny Huang 	{ 12, 2, 0, "VGA memory size : 8MB" },
2893cb28812SJohnny Huang 	{ 12, 2, 1, "VGA memory size : 16MB" },
2903cb28812SJohnny Huang 	{ 12, 2, 2, "VGA memory size : 32MB" },
2913cb28812SJohnny Huang 	{ 12, 2, 3, "VGA memory size : 64MB" },
2923cb28812SJohnny Huang 	{ 14, 3, OTP_REG_RESERVED, "" },
2933cb28812SJohnny Huang 	{ 17, 1, 0, "VGA class code : Class Code for video device" },
2943cb28812SJohnny Huang 	{ 17, 1, 1, "VGA class code : Class Code for VGA device" },
2953cb28812SJohnny Huang 	{ 18, 1, 0, "Enable debug interfaces 0" },
2963cb28812SJohnny Huang 	{ 18, 1, 1, "Disable debug interfaces 0" },
2973cb28812SJohnny Huang 	{ 19, 1, 0, "Boot from emmc mode : High eMMC speed" },
2983cb28812SJohnny Huang 	{ 19, 1, 1, "Boot from emmc mode : Normal eMMC speed" },
2993cb28812SJohnny Huang 	{ 20, 1, 0, "Disable Pcie EHCI device" },
3003cb28812SJohnny Huang 	{ 20, 1, 1, "Enable Pcie EHCI device" },
3013cb28812SJohnny Huang 	{ 21, 1, 0, "Enable VGA XDMA function" },
3023cb28812SJohnny Huang 	{ 21, 1, 1, "Disable VGA XDMA function" },
3033cb28812SJohnny Huang 	{ 22, 1, 0, "Normal BMC mode" },
3043cb28812SJohnny Huang 	{ 22, 1, 1, "Disable dedicated BMC functions for non-BMC application" },
3053cb28812SJohnny Huang 	{ 23, 1, 0, "SSPRST# pin is for secondary processor dedicated reset pin" },
3063cb28812SJohnny Huang 	{ 23, 1, 1, "SSPRST# pin is for PCIE root complex dedicated reset pin" },
3073cb28812SJohnny Huang 	{ 24, 1, 0, "Enable watchdog to reset full chip" },
3083cb28812SJohnny Huang 	{ 24, 1, 1, "Disable watchdog to reset full chip" },
3093cb28812SJohnny Huang 	{ 25, 5, OTP_REG_RESERVED, "" },
3103cb28812SJohnny Huang 	{ 30, 2, OTP_REG_RESERVED, "" },
3113cb28812SJohnny Huang 	{ 32, 1, 0, "MAC 3 : RMII/NCSI" },
3123cb28812SJohnny Huang 	{ 32, 1, 1, "MAC 3 : RGMII" },
3133cb28812SJohnny Huang 	{ 33, 1, 0, "MAC 4 : RMII/NCSI" },
3143cb28812SJohnny Huang 	{ 33, 1, 1, "MAC 4 : RGMII" },
3153cb28812SJohnny Huang 	{ 34, 1, 0, "SuperIO configuration address : 0x2E" },
3163cb28812SJohnny Huang 	{ 34, 1, 1, "SuperIO configuration address : 0x4E" },
3173cb28812SJohnny Huang 	{ 35, 1, 0, "Enable LPC to decode SuperIO" },
3183cb28812SJohnny Huang 	{ 35, 1, 1, "Disable LPC to decode SuperIO" },
3193cb28812SJohnny Huang 	{ 36, 1, 0, "Enable debug interfaces 1" },
3203cb28812SJohnny Huang 	{ 36, 1, 1, "Disable debug interfaces 1" },
3213cb28812SJohnny Huang 	{ 37, 1, 0, "Disable ACPI function" },
3223cb28812SJohnny Huang 	{ 37, 1, 1, "Enable ACPI function" },
3233cb28812SJohnny Huang 	{ 38, 1, 0, "Enable eSPI mode" },
3243cb28812SJohnny Huang 	{ 38, 1, 1, "Enable LPC mode" },
3253cb28812SJohnny Huang 	{ 39, 1, 0, "Enable SAFS mode" },
3263cb28812SJohnny Huang 	{ 39, 1, 1, "Enable SAFS mode" },
3273cb28812SJohnny Huang 	{ 40, 2, OTP_REG_RESERVED, "" },
3283cb28812SJohnny Huang 	{ 42, 1, 0, "Disable boot SPI 3B/4B address mode auto detection" },
3293cb28812SJohnny Huang 	{ 42, 1, 1, "Enable boot SPI 3B/4B address mode auto detection" },
3303cb28812SJohnny Huang 	{ 43, 1, 0, "Disable boot SPI ABR" },
3313cb28812SJohnny Huang 	{ 43, 1, 1, "Enable boot SPI ABR" },
3323cb28812SJohnny Huang 	{ 44, 1, 0, "Boot SPI ABR mode : dual SPI flash" },
3333cb28812SJohnny Huang 	{ 44, 1, 1, "Boot SPI ABR mode : single SPI flash" },
3343cb28812SJohnny Huang 	{ 45, 3, 0, "Boot SPI flash size : no define size" },
3353cb28812SJohnny Huang 	{ 45, 3, 1, "Boot SPI flash size : 2MB" },
3363cb28812SJohnny Huang 	{ 45, 3, 2, "Boot SPI flash size : 4MB" },
3373cb28812SJohnny Huang 	{ 45, 3, 3, "Boot SPI flash size : 8MB" },
3383cb28812SJohnny Huang 	{ 45, 3, 4, "Boot SPI flash size : 16MB" },
3393cb28812SJohnny Huang 	{ 45, 3, 5, "Boot SPI flash size : 32MB" },
3403cb28812SJohnny Huang 	{ 45, 3, 6, "Boot SPI flash size : 64MB" },
3413cb28812SJohnny Huang 	{ 45, 3, 7, "Boot SPI flash size : 128MB" },
3423cb28812SJohnny Huang 	{ 48, 1, 0, "Disable host SPI ABR" },
3433cb28812SJohnny Huang 	{ 48, 1, 1, "Enable host SPI ABR" },
3443cb28812SJohnny Huang 	{ 49, 1, 0, "Disable host SPI ABR mode select pin" },
3453cb28812SJohnny Huang 	{ 49, 1, 1, "Enable host SPI ABR mode select pin" },
3463cb28812SJohnny Huang 	{ 50, 1, 0, "Host SPI ABR mode : dual SPI flash" },
3473cb28812SJohnny Huang 	{ 50, 1, 1, "Host SPI ABR mode : single SPI flash" },
3483cb28812SJohnny Huang 	{ 51, 3, 0, "Host SPI flash size : no define size" },
3493cb28812SJohnny Huang 	{ 51, 3, 1, "Host SPI flash size : 2MB" },
3503cb28812SJohnny Huang 	{ 51, 3, 2, "Host SPI flash size : 4MB" },
3513cb28812SJohnny Huang 	{ 51, 3, 3, "Host SPI flash size : 8MB" },
3523cb28812SJohnny Huang 	{ 51, 3, 4, "Host SPI flash size : 16MB" },
3533cb28812SJohnny Huang 	{ 51, 3, 5, "Host SPI flash size : 32MB" },
3543cb28812SJohnny Huang 	{ 51, 3, 6, "Host SPI flash size : 64MB" },
3553cb28812SJohnny Huang 	{ 51, 3, 7, "Host SPI flash size : 128MB" },
3563cb28812SJohnny Huang 	{ 54, 1, 0, "Disable boot SPI auxiliary control pins" },
3573cb28812SJohnny Huang 	{ 54, 1, 1, "Enable boot SPI auxiliary control pins" },
3583cb28812SJohnny Huang 	{ 55, 2, 0, "Boot SPI CRTM size : disable CRTM" },
3593cb28812SJohnny Huang 	{ 55, 2, 1, "Boot SPI CRTM size : 256KB" },
3603cb28812SJohnny Huang 	{ 55, 2, 2, "Boot SPI CRTM size : 512KB" },
3613cb28812SJohnny Huang 	{ 55, 2, 3, "Boot SPI CRTM size : 1MB" },
3623cb28812SJohnny Huang 	{ 57, 2, 0, "Host SPI CRTM size : disable CRTM" },
3633cb28812SJohnny Huang 	{ 57, 2, 1, "Host SPI CRTM size : 256KB" },
3643cb28812SJohnny Huang 	{ 57, 2, 2, "Host SPI CRTM size : 512KB" },
3653cb28812SJohnny Huang 	{ 57, 2, 3, "Host SPI CRTM size : 1MB" },
3663cb28812SJohnny Huang 	{ 59, 1, 0, "Disable host SPI auxiliary control pins" },
3673cb28812SJohnny Huang 	{ 59, 1, 1, "Enable host SPI auxiliary control pins" },
3683cb28812SJohnny Huang 	{ 60, 1, 0, "Disable GPIO pass through" },
3693cb28812SJohnny Huang 	{ 60, 1, 1, "Enable GPIO pass through" },
3703cb28812SJohnny Huang 	{ 61, 1, 0, "Enable low security secure boot key" },
3713cb28812SJohnny Huang 	{ 61, 1, 1, "Disable low security secure boot key" },
3723cb28812SJohnny Huang 	{ 62, 1, 0, "Disable dedicate GPIO strap pins" },
3733cb28812SJohnny Huang 	{ 62, 1, 1, "Enable dedicate GPIO strap pins" },
3743cb28812SJohnny Huang 	{ 63, 1, OTP_REG_RESERVED, "" }
3753cb28812SJohnny Huang };
3763cb28812SJohnny Huang 
37779e42a59SJoel Stanley static const struct otpconf_info a0_conf_info[] = {
37891448c03SJohnny Huang 	{ 0, 0,  1,  0, "Enable Secure Region programming" },
37991448c03SJohnny Huang 	{ 0, 0,  1,  1, "Disable Secure Region programming" },
38091448c03SJohnny Huang 	{ 0, 1,  1,  0, "Disable Secure Boot" },
38191448c03SJohnny Huang 	{ 0, 1,  1,  1, "Enable Secure Boot" },
38291448c03SJohnny Huang 	{ 0, 2,  1,  0, "Initialization programming not done" },
38391448c03SJohnny Huang 	{ 0, 2,  1,  1, "Initialization programming done" },
38491448c03SJohnny Huang 	{ 0, 3,  1,  0, "User region ECC disable" },
38591448c03SJohnny Huang 	{ 0, 3,  1,  1, "User region ECC enable" },
38691448c03SJohnny Huang 	{ 0, 4,  1,  0, "Secure Region ECC disable" },
38791448c03SJohnny Huang 	{ 0, 4,  1,  1, "Secure Region ECC enable" },
38891448c03SJohnny Huang 	{ 0, 5,  1,  0, "Enable low security key" },
38991448c03SJohnny Huang 	{ 0, 5,  1,  1, "Disable low security key" },
39091448c03SJohnny Huang 	{ 0, 6,  1,  0, "Do not ignore Secure Boot hardware strap" },
39191448c03SJohnny Huang 	{ 0, 6,  1,  1, "Ignore Secure Boot hardware strap" },
39291448c03SJohnny Huang 	{ 0, 7,  1,  0, "Secure Boot Mode: 1" },
39391448c03SJohnny Huang 	{ 0, 7,  1,  1, "Secure Boot Mode: 2" },
39491448c03SJohnny Huang 	{ 0, 8,  2,  0, "Single cell mode (recommended)" },
395541eb887SJohnny Huang 	{ 0, 8,  2,  1, "Differential mode" },
39691448c03SJohnny Huang 	{ 0, 8,  2,  2, "Differential-redundant mode" },
39791448c03SJohnny Huang 	{ 0, 10, 2,  0, "RSA mode : RSA1024" },
39891448c03SJohnny Huang 	{ 0, 10, 2,  1, "RSA mode : RSA2048" },
39991448c03SJohnny Huang 	{ 0, 10, 2,  2, "RSA mode : RSA3072" },
40091448c03SJohnny Huang 	{ 0, 10, 2,  3, "RSA mode : RSA4096" },
40191448c03SJohnny Huang 	{ 0, 12, 2,  0, "SHA mode : SHA224" },
40291448c03SJohnny Huang 	{ 0, 12, 2,  1, "SHA mode : SHA256" },
40391448c03SJohnny Huang 	{ 0, 12, 2,  2, "SHA mode : SHA384" },
40491448c03SJohnny Huang 	{ 0, 12, 2,  3, "SHA mode : SHA512" },
40591448c03SJohnny Huang 	{ 0, 14, 2,  OTP_REG_RESERVED, "" },
40691448c03SJohnny Huang 	{ 0, 16, 6,  OTP_REG_VALUE, "Secure Region size (DW): 0x%x" },
40791448c03SJohnny Huang 	{ 0, 22, 1,  0, "Secure Region : Writable" },
40891448c03SJohnny Huang 	{ 0, 22, 1,  1, "Secure Region : Write Protect" },
40991448c03SJohnny Huang 	{ 0, 23, 1,  0, "User Region : Writable" },
41091448c03SJohnny Huang 	{ 0, 23, 1,  1, "User Region : Write Protect" },
41191448c03SJohnny Huang 	{ 0, 24, 1,  0, "Configure Region : Writable" },
41291448c03SJohnny Huang 	{ 0, 24, 1,  1, "Configure Region : Write Protect" },
41391448c03SJohnny Huang 	{ 0, 25, 1,  0, "OTP strap Region : Writable" },
41491448c03SJohnny Huang 	{ 0, 25, 1,  1, "OTP strap Region : Write Protect" },
41591448c03SJohnny Huang 	{ 0, 26, 1,  0, "Disable Copy Boot Image to Internal SRAM" },
41691448c03SJohnny Huang 	{ 0, 26, 1,  1, "Copy Boot Image to Internal SRAM" },
41791448c03SJohnny Huang 	{ 0, 27, 1,  0, "Disable image encryption" },
41891448c03SJohnny Huang 	{ 0, 27, 1,  1, "Enable image encryption" },
41991448c03SJohnny Huang 	{ 0, 28, 1,  OTP_REG_RESERVED, "" },
42091448c03SJohnny Huang 	{ 0, 29, 1,  0, "OTP key retire Region : Writable" },
42191448c03SJohnny Huang 	{ 0, 29, 1,  1, "OTP key retire Region : Write Protect" },
4223cb28812SJohnny Huang 	{ 0, 30, 1,  0, "Data region redundancy repair disable" },
4233cb28812SJohnny Huang 	{ 0, 30, 1,  1, "Data region redundancy repair enable" },
4243cb28812SJohnny Huang 	{ 0, 31, 1,  0, "OTP memory lock disable" },
4253cb28812SJohnny Huang 	{ 0, 31, 1,  1, "OTP memory lock enable" },
4263cb28812SJohnny Huang 	{ 2, 0,  16, OTP_REG_VALUE, "Vender ID : 0x%x" },
4273cb28812SJohnny Huang 	{ 2, 16, 16, OTP_REG_VALUE, "Key Revision : 0x%x" },
4283cb28812SJohnny Huang 	{ 3, 0,  16, OTP_REG_VALUE, "Secure boot header offset : 0x%x" },
4293cb28812SJohnny Huang 	{ 4, 0,  8,  OTP_REG_VALID_BIT, "Keys valid  : %s" },
4303cb28812SJohnny Huang 	{ 4, 16, 8,  OTP_REG_VALID_BIT, "Keys retire  : %s" },
4313cb28812SJohnny Huang 	{ 5, 0,  32, OTP_REG_VALUE, "User define data, random number low : 0x%x" },
4323cb28812SJohnny Huang 	{ 6, 0,  32, OTP_REG_VALUE, "User define data, random number high : 0x%x" },
4333cb28812SJohnny Huang 	{ 7, 0,  1,  0, "Force enable PCI bus to AHB bus bridge" },
4343cb28812SJohnny Huang 	{ 7, 0,  1,  1, "Force disable PCI bus to AHB bus bridge" },
4353cb28812SJohnny Huang 	{ 7, 1,  1,  0, "Force enable UART5 debug port function" },
4363cb28812SJohnny Huang 	{ 7, 1,  1,  1, "Force disable UART5 debug port function" },
4373cb28812SJohnny Huang 	{ 7, 2,  1,  0, "Force enable XDMA function" },
4383cb28812SJohnny Huang 	{ 7, 2,  1,  1, "Force disable XDMA function" },
4393cb28812SJohnny Huang 	{ 7, 3,  1,  0, "Force enable APB to PCIE device bridge" },
4403cb28812SJohnny Huang 	{ 7, 3,  1,  1, "Force disable APB to PCIE device bridge" },
4413cb28812SJohnny Huang 	{ 7, 4,  1,  0, "Force enable APB to PCIE bridge config access" },
4423cb28812SJohnny Huang 	{ 7, 4,  1,  1, "Force disable APB to PCIE bridge config access" },
4433cb28812SJohnny Huang 	{ 7, 5,  1,  0, "Force enable PCIE bus trace buffer" },
4443cb28812SJohnny Huang 	{ 7, 5,  1,  1, "Force disable PCIE bus trace buffer" },
4453cb28812SJohnny Huang 	{ 7, 6,  1,  0, "Force enable the capability for PCIE device port as a Root Complex" },
4463cb28812SJohnny Huang 	{ 7, 6,  1,  1, "Force disable the capability for PCIE device port as a Root Complex" },
4473cb28812SJohnny Huang 	{ 7, 16, 1,  0, "Force enable ESPI bus to AHB bus bridge" },
4483cb28812SJohnny Huang 	{ 7, 16, 1,  1, "Force disable ESPI bus to AHB bus bridge" },
4493cb28812SJohnny Huang 	{ 7, 17, 1,  0, "Force enable LPC bus to AHB bus bridge1" },
4503cb28812SJohnny Huang 	{ 7, 17, 1,  1, "Force disable LPC bus to AHB bus bridge1" },
4513cb28812SJohnny Huang 	{ 7, 18, 1,  0, "Force enable LPC bus to AHB bus bridge2" },
4523cb28812SJohnny Huang 	{ 7, 18, 1,  1, "Force disable LPC bus to AHB bus bridge2" },
4533cb28812SJohnny Huang 	{ 7, 19, 1,  0, "Force enable UART1 debug port function" },
4543cb28812SJohnny Huang 	{ 7, 19, 1,  1, "Force disable UART1 debug port function" },
4553cb28812SJohnny Huang 	{ 7, 31, 1,  0, "Disable chip security setting" },
4563cb28812SJohnny Huang 	{ 7, 31, 1,  1, "Enable chip security setting" },
4573cb28812SJohnny Huang 	{ 8, 0,  32, OTP_REG_VALUE, "Redundancy Repair : 0x%x" },
4583cb28812SJohnny Huang 	{ 10, 0, 32, OTP_REG_VALUE, "Manifest ID low : 0x%x" },
4593cb28812SJohnny Huang 	{ 11, 0, 32, OTP_REG_VALUE, "Manifest ID high : 0x%x" }
4603cb28812SJohnny Huang };
4613cb28812SJohnny Huang 
46279e42a59SJoel Stanley static const struct otpconf_info a1_conf_info[] = {
4633cb28812SJohnny Huang 	{ 0, 0,  1,  OTP_REG_RESERVED, "" },
4643cb28812SJohnny Huang 	{ 0, 1,  1,  0, "Disable Secure Boot" },
4653cb28812SJohnny Huang 	{ 0, 1,  1,  1, "Enable Secure Boot" },
4663cb28812SJohnny Huang 	{ 0, 2,  1,  0, "Initialization programming not done" },
4673cb28812SJohnny Huang 	{ 0, 2,  1,  1, "Initialization programming done" },
4683cb28812SJohnny Huang 	{ 0, 3,  1,  0, "User region ECC disable" },
4693cb28812SJohnny Huang 	{ 0, 3,  1,  1, "User region ECC enable" },
4703cb28812SJohnny Huang 	{ 0, 4,  1,  0, "Secure Region ECC disable" },
4713cb28812SJohnny Huang 	{ 0, 4,  1,  1, "Secure Region ECC enable" },
4723cb28812SJohnny Huang 	{ 0, 5,  1,  0, "Enable low security key" },
4733cb28812SJohnny Huang 	{ 0, 5,  1,  1, "Disable low security key" },
4743cb28812SJohnny Huang 	{ 0, 6,  1,  0, "Do not ignore Secure Boot hardware strap" },
4753cb28812SJohnny Huang 	{ 0, 6,  1,  1, "Ignore Secure Boot hardware strap" },
4763cb28812SJohnny Huang 	{ 0, 7,  1,  0, "Secure Boot Mode: GCM" },
4773cb28812SJohnny Huang 	{ 0, 7,  1,  1, "Secure Boot Mode: 2" },
4783cb28812SJohnny Huang 	{ 0, 8,  2,  0, "Single cell mode (recommended)" },
4793cb28812SJohnny Huang 	{ 0, 8,  2,  1, "Differential mode" },
4803cb28812SJohnny Huang 	{ 0, 8,  2,  2, "Differential-redundant mode" },
4813cb28812SJohnny Huang 	{ 0, 10, 2,  0, "RSA mode : RSA1024" },
4823cb28812SJohnny Huang 	{ 0, 10, 2,  1, "RSA mode : RSA2048" },
4833cb28812SJohnny Huang 	{ 0, 10, 2,  2, "RSA mode : RSA3072" },
4843cb28812SJohnny Huang 	{ 0, 10, 2,  3, "RSA mode : RSA4096" },
4853cb28812SJohnny Huang 	{ 0, 12, 2,  0, "SHA mode : SHA224" },
4863cb28812SJohnny Huang 	{ 0, 12, 2,  1, "SHA mode : SHA256" },
4873cb28812SJohnny Huang 	{ 0, 12, 2,  2, "SHA mode : SHA384" },
4883cb28812SJohnny Huang 	{ 0, 12, 2,  3, "SHA mode : SHA512" },
489bb34a7bfSJohnny Huang 	{ 0, 14, 1,  0, "Disable Patch code" },
490bb34a7bfSJohnny Huang 	{ 0, 14, 1,  1, "Enable Patch code" },
491bb34a7bfSJohnny Huang 	{ 0, 15, 1,  OTP_REG_RESERVED, "" },
4923cb28812SJohnny Huang 	{ 0, 16, 6,  OTP_REG_VALUE, "Secure Region size (DW): 0x%x" },
4933cb28812SJohnny Huang 	{ 0, 22, 1,  0, "Secure Region : Writable" },
4943cb28812SJohnny Huang 	{ 0, 22, 1,  1, "Secure Region : Write Protect" },
4953cb28812SJohnny Huang 	{ 0, 23, 1,  0, "User Region : Writable" },
4963cb28812SJohnny Huang 	{ 0, 23, 1,  1, "User Region : Write Protect" },
4973cb28812SJohnny Huang 	{ 0, 24, 1,  0, "Configure Region : Writable" },
4983cb28812SJohnny Huang 	{ 0, 24, 1,  1, "Configure Region : Write Protect" },
4993cb28812SJohnny Huang 	{ 0, 25, 1,  0, "OTP strap Region : Writable" },
5003cb28812SJohnny Huang 	{ 0, 25, 1,  1, "OTP strap Region : Write Protect" },
5013cb28812SJohnny Huang 	{ 0, 26, 1,  0, "Disable Copy Boot Image to Internal SRAM" },
5023cb28812SJohnny Huang 	{ 0, 26, 1,  1, "Copy Boot Image to Internal SRAM" },
5033cb28812SJohnny Huang 	{ 0, 27, 1,  0, "Disable image encryption" },
5043cb28812SJohnny Huang 	{ 0, 27, 1,  1, "Enable image encryption" },
5053cb28812SJohnny Huang 	{ 0, 28, 1,  OTP_REG_RESERVED, "" },
5063cb28812SJohnny Huang 	{ 0, 29, 1,  0, "OTP key retire Region : Writable" },
5073cb28812SJohnny Huang 	{ 0, 29, 1,  1, "OTP key retire Region : Write Protect" },
5083cb28812SJohnny Huang 	{ 0, 30, 1,  0, "Data region redundancy repair disable" },
5093cb28812SJohnny Huang 	{ 0, 30, 1,  1, "Data region redundancy repair enable" },
5103cb28812SJohnny Huang 	{ 0, 31, 1,  0, "OTP memory lock disable" },
5113cb28812SJohnny Huang 	{ 0, 31, 1,  1, "OTP memory lock enable" },
51291448c03SJohnny Huang 	{ 2, 0,  16, OTP_REG_VALUE, "Vender ID : 0x%x" },
51391448c03SJohnny Huang 	{ 2, 16, 16, OTP_REG_VALUE, "Key Revision : 0x%x" },
51491448c03SJohnny Huang 	{ 3, 0,  16, OTP_REG_VALUE, "Secure boot header offset : 0x%x" },
51573f11549SJohnny Huang 	{ 4, 0,  8,  OTP_REG_VALID_BIT, "Keys valid  : %s" },
51673f11549SJohnny Huang 	{ 4, 16, 8,  OTP_REG_VALID_BIT, "Keys retire  : %s" },
51791448c03SJohnny Huang 	{ 5, 0,  32, OTP_REG_VALUE, "User define data, random number low : 0x%x" },
51891448c03SJohnny Huang 	{ 6, 0,  32, OTP_REG_VALUE, "User define data, random number high : 0x%x" },
51991448c03SJohnny Huang 	{ 7, 0,  1,  0, "Force enable PCI bus to AHB bus bridge" },
52091448c03SJohnny Huang 	{ 7, 0,  1,  1, "Force disable PCI bus to AHB bus bridge" },
52191448c03SJohnny Huang 	{ 7, 1,  1,  0, "Force enable UART5 debug port function" },
52291448c03SJohnny Huang 	{ 7, 1,  1,  1, "Force disable UART5 debug port function" },
52391448c03SJohnny Huang 	{ 7, 2,  1,  0, "Force enable XDMA function" },
52491448c03SJohnny Huang 	{ 7, 2,  1,  1, "Force disable XDMA function" },
52591448c03SJohnny Huang 	{ 7, 3,  1,  0, "Force enable APB to PCIE device bridge" },
52691448c03SJohnny Huang 	{ 7, 3,  1,  1, "Force disable APB to PCIE device bridge" },
52791448c03SJohnny Huang 	{ 7, 4,  1,  0, "Force enable APB to PCIE bridge config access" },
52891448c03SJohnny Huang 	{ 7, 4,  1,  1, "Force disable APB to PCIE bridge config access" },
52991448c03SJohnny Huang 	{ 7, 5,  1,  0, "Force enable PCIE bus trace buffer" },
53091448c03SJohnny Huang 	{ 7, 5,  1,  1, "Force disable PCIE bus trace buffer" },
53191448c03SJohnny Huang 	{ 7, 6,  1,  0, "Force enable the capability for PCIE device port as a Root Complex" },
53291448c03SJohnny Huang 	{ 7, 6,  1,  1, "Force disable the capability for PCIE device port as a Root Complex" },
53391448c03SJohnny Huang 	{ 7, 16, 1,  0, "Force enable ESPI bus to AHB bus bridge" },
53491448c03SJohnny Huang 	{ 7, 16, 1,  1, "Force disable ESPI bus to AHB bus bridge" },
53591448c03SJohnny Huang 	{ 7, 17, 1,  0, "Force enable LPC bus to AHB bus bridge1" },
53691448c03SJohnny Huang 	{ 7, 17, 1,  1, "Force disable LPC bus to AHB bus bridge1" },
53791448c03SJohnny Huang 	{ 7, 18, 1,  0, "Force enable LPC bus to AHB bus bridge2" },
53891448c03SJohnny Huang 	{ 7, 18, 1,  1, "Force disable LPC bus to AHB bus bridge2" },
53991448c03SJohnny Huang 	{ 7, 19, 1,  0, "Force enable UART1 debug port function" },
54091448c03SJohnny Huang 	{ 7, 19, 1,  1, "Force disable UART1 debug port function" },
54191448c03SJohnny Huang 	{ 7, 31, 1,  0, "Disable chip security setting" },
54291448c03SJohnny Huang 	{ 7, 31, 1,  1, "Enable chip security setting" },
54391448c03SJohnny Huang 	{ 8, 0,  32, OTP_REG_VALUE, "Redundancy Repair : 0x%x" },
54491448c03SJohnny Huang 	{ 10, 0, 32, OTP_REG_VALUE, "Manifest ID low : 0x%x" },
545bb34a7bfSJohnny Huang 	{ 11, 0, 32, OTP_REG_VALUE, "Manifest ID high : 0x%x" },
546bb34a7bfSJohnny Huang 	{ 14, 0, 11, OTP_REG_VALUE, "Patch code location (DW): 0x%x" },
547bb34a7bfSJohnny Huang 	{ 14, 11, 6, OTP_REG_VALUE, "Patch code size (DW): 0x%x" }
548b458cd62SJohnny Huang };
5499a4fe690SJohnny Huang 
55079e42a59SJoel Stanley static const struct otpkey_type a0_key_type[] = {
5519a4fe690SJohnny Huang 	{0, OTP_KEY_TYPE_AES,   0, "AES-256 as OEM platform key for image encryption/decryption"},
5529a4fe690SJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
5539a4fe690SJohnny Huang 	{4, OTP_KEY_TYPE_HMAC,  1, "HMAC as encrypted OEM HMAC keys in Mode 1"},
5549a4fe690SJohnny Huang 	{8, OTP_KEY_TYPE_RSA,   1, "RSA-public as OEM DSS public keys in Mode 2"},
5559a4fe690SJohnny Huang 	{9, OTP_KEY_TYPE_RSA,   0, "RSA-public as SOC public key"},
5569a4fe690SJohnny Huang 	{10, OTP_KEY_TYPE_RSA,  0, "RSA-public as AES key decryption key"},
5579a4fe690SJohnny Huang 	{13, OTP_KEY_TYPE_RSA,  0, "RSA-private as SOC private key"},
5589a4fe690SJohnny Huang 	{14, OTP_KEY_TYPE_RSA,  0, "RSA-private as AES key decryption key"},
5599a4fe690SJohnny Huang };
5609a4fe690SJohnny Huang 
56179e42a59SJoel Stanley static const struct otpkey_type a1_key_type[] = {
5629a4fe690SJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
5639a4fe690SJohnny Huang 	{2, OTP_KEY_TYPE_AES,   1, "AES-256 as OEM platform key for image encryption/decryption in Mode 2 or AES-256 as OEM DSS keys for Mode GCM"},
5649a4fe690SJohnny Huang 	{8, OTP_KEY_TYPE_RSA,   1, "RSA-public as OEM DSS public keys in Mode 2"},
5659a4fe690SJohnny Huang 	{10, OTP_KEY_TYPE_RSA,  0, "RSA-public as AES key decryption key"},
5669a4fe690SJohnny Huang 	{14, OTP_KEY_TYPE_RSA,  0, "RSA-private as AES key decryption key"},
5679a4fe690SJohnny Huang };
5689a4fe690SJohnny Huang 
5699a4fe690SJohnny Huang static uint32_t chip_version(void)
5709a4fe690SJohnny Huang {
5719a4fe690SJohnny Huang 	uint32_t rev_id;
5729a4fe690SJohnny Huang 
5739a4fe690SJohnny Huang 	rev_id = (readl(0x1e6e2004) >> 16) & 0xff ;
5749a4fe690SJohnny Huang 
5759a4fe690SJohnny Huang 	return rev_id;
5769a4fe690SJohnny Huang }
5779a4fe690SJohnny Huang 
5783d3688adSJohnny Huang static void wait_complete(void)
5793d3688adSJohnny Huang {
5803d3688adSJohnny Huang 	int reg;
5813d3688adSJohnny Huang 
5823d3688adSJohnny Huang 	do {
5833d3688adSJohnny Huang 		reg = readl(OTP_STATUS);
5843d3688adSJohnny Huang 	} while ((reg & 0x6) != 0x6);
5853d3688adSJohnny Huang }
5863d3688adSJohnny Huang 
5872a856b9aSJohnny Huang static void otp_read_data(uint32_t offset, uint32_t *data)
58869d5fd8fSJohnny Huang {
5893d3688adSJohnny Huang 	writel(offset, OTP_ADDR); //Read address
5903d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
5913d3688adSJohnny Huang 	wait_complete();
5923d3688adSJohnny Huang 	data[0] = readl(OTP_COMPARE_1);
5933d3688adSJohnny Huang 	data[1] = readl(OTP_COMPARE_2);
59469d5fd8fSJohnny Huang }
59569d5fd8fSJohnny Huang 
5962a856b9aSJohnny Huang static void otp_read_config(uint32_t offset, uint32_t *data)
59769d5fd8fSJohnny Huang {
59869d5fd8fSJohnny Huang 	int config_offset;
59969d5fd8fSJohnny Huang 
60069d5fd8fSJohnny Huang 	config_offset = 0x800;
60169d5fd8fSJohnny Huang 	config_offset |= (offset / 8) * 0x200;
60269d5fd8fSJohnny Huang 	config_offset |= (offset % 8) * 0x2;
60369d5fd8fSJohnny Huang 
6043d3688adSJohnny Huang 	writel(config_offset, OTP_ADDR);  //Read address
6053d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
6063d3688adSJohnny Huang 	wait_complete();
6073d3688adSJohnny Huang 	data[0] = readl(OTP_COMPARE_1);
60869d5fd8fSJohnny Huang }
60969d5fd8fSJohnny Huang 
61069d5fd8fSJohnny Huang static int otp_print_config(uint32_t offset, int dw_count)
61169d5fd8fSJohnny Huang {
61269d5fd8fSJohnny Huang 	int i;
61369d5fd8fSJohnny Huang 	uint32_t ret[1];
61469d5fd8fSJohnny Huang 
61569d5fd8fSJohnny Huang 	if (offset + dw_count > 32)
6162a856b9aSJohnny Huang 		return OTP_USAGE;
61769d5fd8fSJohnny Huang 	for (i = offset; i < offset + dw_count; i ++) {
61869d5fd8fSJohnny Huang 		otp_read_config(i, ret);
619a6af4a17SJohnny Huang 		printf("OTPCFG%X: %08X\n", i, ret[0]);
62069d5fd8fSJohnny Huang 	}
62169d5fd8fSJohnny Huang 	printf("\n");
6222a856b9aSJohnny Huang 	return OTP_SUCCESS;
62369d5fd8fSJohnny Huang }
62469d5fd8fSJohnny Huang 
62569d5fd8fSJohnny Huang static int otp_print_data(uint32_t offset, int dw_count)
62669d5fd8fSJohnny Huang {
62769d5fd8fSJohnny Huang 	int i;
62869d5fd8fSJohnny Huang 	uint32_t ret[2];
62969d5fd8fSJohnny Huang 
63069d5fd8fSJohnny Huang 	if (offset + dw_count > 2048 || offset % 4 != 0)
6312a856b9aSJohnny Huang 		return OTP_USAGE;
63269d5fd8fSJohnny Huang 	for (i = offset; i < offset + dw_count; i += 2) {
63369d5fd8fSJohnny Huang 		otp_read_data(i, ret);
63469d5fd8fSJohnny Huang 		if (i % 4 == 0)
63569d5fd8fSJohnny Huang 			printf("%03X: %08X %08X ", i * 4, ret[0], ret[1]);
63669d5fd8fSJohnny Huang 		else
63769d5fd8fSJohnny Huang 			printf("%08X %08X\n", ret[0], ret[1]);
63869d5fd8fSJohnny Huang 
63969d5fd8fSJohnny Huang 	}
64069d5fd8fSJohnny Huang 	printf("\n");
6412a856b9aSJohnny Huang 	return OTP_SUCCESS;
64269d5fd8fSJohnny Huang }
64369d5fd8fSJohnny Huang 
64469d5fd8fSJohnny Huang static int otp_compare(uint32_t otp_addr, uint32_t addr)
64569d5fd8fSJohnny Huang {
64669d5fd8fSJohnny Huang 	uint32_t ret;
64769d5fd8fSJohnny Huang 	uint32_t *buf;
64869d5fd8fSJohnny Huang 
64969d5fd8fSJohnny Huang 	buf = map_physmem(addr, 16, MAP_WRBACK);
65069d5fd8fSJohnny Huang 	printf("%08X\n", buf[0]);
65169d5fd8fSJohnny Huang 	printf("%08X\n", buf[1]);
65269d5fd8fSJohnny Huang 	printf("%08X\n", buf[2]);
65369d5fd8fSJohnny Huang 	printf("%08X\n", buf[3]);
6543d3688adSJohnny Huang 	writel(otp_addr, OTP_ADDR); //Compare address
6553d3688adSJohnny Huang 	writel(buf[0], OTP_COMPARE_1); //Compare data 1
6563d3688adSJohnny Huang 	writel(buf[1], OTP_COMPARE_2); //Compare data 2
6573d3688adSJohnny Huang 	writel(buf[2], OTP_COMPARE_3); //Compare data 3
6583d3688adSJohnny Huang 	writel(buf[3], OTP_COMPARE_4); //Compare data 4
6593d3688adSJohnny Huang 	writel(0x23b1e363, OTP_COMMAND); //Compare command
6603d3688adSJohnny Huang 	wait_complete();
6613d3688adSJohnny Huang 	ret = readl(OTP_STATUS); //Compare command
66269d5fd8fSJohnny Huang 	if (ret & 0x1)
66369d5fd8fSJohnny Huang 		return 0;
66469d5fd8fSJohnny Huang 	else
66569d5fd8fSJohnny Huang 		return -1;
66669d5fd8fSJohnny Huang }
66769d5fd8fSJohnny Huang 
66869d5fd8fSJohnny Huang static void otp_write(uint32_t otp_addr, uint32_t data)
66969d5fd8fSJohnny Huang {
6703d3688adSJohnny Huang 	writel(otp_addr, OTP_ADDR); //write address
6713d3688adSJohnny Huang 	writel(data, OTP_COMPARE_1); //write data
6723d3688adSJohnny Huang 	writel(0x23b1e362, OTP_COMMAND); //write command
6733d3688adSJohnny Huang 	wait_complete();
67469d5fd8fSJohnny Huang }
67569d5fd8fSJohnny Huang 
676a6d0d645SJohnny Huang static int verify_bit(uint32_t otp_addr, int bit_offset, int value)
67769d5fd8fSJohnny Huang {
67830a8c590SJohnny Huang 	uint32_t ret[2];
67969d5fd8fSJohnny Huang 
68030a8c590SJohnny Huang 	if (otp_addr % 2 == 0)
6813d3688adSJohnny Huang 		writel(otp_addr, OTP_ADDR); //Read address
68230a8c590SJohnny Huang 	else
6833d3688adSJohnny Huang 		writel(otp_addr - 1, OTP_ADDR); //Read address
68430a8c590SJohnny Huang 
6853d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
6863d3688adSJohnny Huang 	wait_complete();
6873d3688adSJohnny Huang 	ret[0] = readl(OTP_COMPARE_1);
6883d3688adSJohnny Huang 	ret[1] = readl(OTP_COMPARE_2);
689*83655e91SJohnny Huang 
69030a8c590SJohnny Huang 	if (otp_addr % 2 == 0) {
69130a8c590SJohnny Huang 		if (((ret[0] >> bit_offset) & 1) == value)
69269d5fd8fSJohnny Huang 			return 0;
69369d5fd8fSJohnny Huang 		else
69469d5fd8fSJohnny Huang 			return -1;
69530a8c590SJohnny Huang 	} else {
69630a8c590SJohnny Huang 		if (((ret[1] >> bit_offset) & 1) == value)
69730a8c590SJohnny Huang 			return 0;
69830a8c590SJohnny Huang 		else
69930a8c590SJohnny Huang 			return -1;
70030a8c590SJohnny Huang 	}
70130a8c590SJohnny Huang 
70269d5fd8fSJohnny Huang }
70369d5fd8fSJohnny Huang 
704696656c6SJohnny Huang static uint32_t verify_dw(uint32_t otp_addr, uint32_t *value, uint32_t *ignore, uint32_t *compare, int size)
7054c1c9b35SJohnny Huang {
7064c1c9b35SJohnny Huang 	uint32_t ret[2];
7074c1c9b35SJohnny Huang 
7084c1c9b35SJohnny Huang 	otp_addr &= ~(1 << 15);
7094c1c9b35SJohnny Huang 
7104c1c9b35SJohnny Huang 	if (otp_addr % 2 == 0)
7113d3688adSJohnny Huang 		writel(otp_addr, OTP_ADDR); //Read address
7124c1c9b35SJohnny Huang 	else
7133d3688adSJohnny Huang 		writel(otp_addr - 1, OTP_ADDR); //Read address
7143d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
7153d3688adSJohnny Huang 	wait_complete();
7163d3688adSJohnny Huang 	ret[0] = readl(OTP_COMPARE_1);
7173d3688adSJohnny Huang 	ret[1] = readl(OTP_COMPARE_2);
7184c1c9b35SJohnny Huang 	if (size == 1) {
7194c1c9b35SJohnny Huang 		if (otp_addr % 2 == 0) {
7204c1c9b35SJohnny Huang 			// printf("check %x : %x = %x\n", otp_addr, ret[0], value[0]);
721696656c6SJohnny Huang 			if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0])) {
7224c1c9b35SJohnny Huang 				compare[0] = 0;
7234c1c9b35SJohnny Huang 				return 0;
7244c1c9b35SJohnny Huang 			} else {
7254c1c9b35SJohnny Huang 				compare[0] = value[0] ^ ret[0];
7264c1c9b35SJohnny Huang 				return -1;
7274c1c9b35SJohnny Huang 			}
7284c1c9b35SJohnny Huang 
7294c1c9b35SJohnny Huang 		} else {
7304c1c9b35SJohnny Huang 			// printf("check %x : %x = %x\n", otp_addr, ret[1], value[0]);
731696656c6SJohnny Huang 			if ((value[0] & ~ignore[0]) == (ret[1] & ~ignore[0])) {
7324c1c9b35SJohnny Huang 				compare[0] = ~0;
7334c1c9b35SJohnny Huang 				return 0;
7344c1c9b35SJohnny Huang 			} else {
735d90825e2SJohnny Huang 				compare[0] = ~(value[0] ^ ret[1]);
7364c1c9b35SJohnny Huang 				return -1;
7374c1c9b35SJohnny Huang 			}
7384c1c9b35SJohnny Huang 		}
7394c1c9b35SJohnny Huang 	} else if (size == 2) {
7404c1c9b35SJohnny Huang 		// otp_addr should be even
741696656c6SJohnny Huang 		if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0]) && (value[1] & ~ignore[1]) == (ret[1] & ~ignore[1])) {
7424c1c9b35SJohnny Huang 			// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
7434c1c9b35SJohnny Huang 			// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
7444c1c9b35SJohnny Huang 			compare[0] = 0;
7454c1c9b35SJohnny Huang 			compare[1] = ~0;
7464c1c9b35SJohnny Huang 			return 0;
7474c1c9b35SJohnny Huang 		} else {
7484c1c9b35SJohnny Huang 			// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
7494c1c9b35SJohnny Huang 			// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
7504c1c9b35SJohnny Huang 			compare[0] = value[0] ^ ret[0];
7514c1c9b35SJohnny Huang 			compare[1] = ~(value[1] ^ ret[1]);
7524c1c9b35SJohnny Huang 			return -1;
7534c1c9b35SJohnny Huang 		}
7544c1c9b35SJohnny Huang 	} else {
7554c1c9b35SJohnny Huang 		return -1;
7564c1c9b35SJohnny Huang 	}
7574c1c9b35SJohnny Huang }
7584c1c9b35SJohnny Huang 
7597e22f42dSJohnny Huang static void otp_soak(int soak)
760d90825e2SJohnny Huang {
761de6fbf1cSJohnny Huang 	switch (soak) {
762de6fbf1cSJohnny Huang 	case 0: //default
763de6fbf1cSJohnny Huang 		otp_write(0x3000, 0x0); // Write MRA
764de6fbf1cSJohnny Huang 		otp_write(0x5000, 0x0); // Write MRB
765de6fbf1cSJohnny Huang 		otp_write(0x1000, 0x0); // Write MR
766de6fbf1cSJohnny Huang 		break;
767de6fbf1cSJohnny Huang 	case 1: //normal program
768de6fbf1cSJohnny Huang 		otp_write(0x3000, 0x4021); // Write MRA
769de6fbf1cSJohnny Huang 		otp_write(0x5000, 0x302f); // Write MRB
770de6fbf1cSJohnny Huang 		otp_write(0x1000, 0x4020); // Write MR
771de6fbf1cSJohnny Huang 		writel(0x04190760, OTP_TIMING);
772de6fbf1cSJohnny Huang 		break;
773de6fbf1cSJohnny Huang 	case 2: //soak program
774d90825e2SJohnny Huang 		otp_write(0x3000, 0x4021); // Write MRA
775d90825e2SJohnny Huang 		otp_write(0x5000, 0x1027); // Write MRB
776d90825e2SJohnny Huang 		otp_write(0x1000, 0x4820); // Write MR
777de6fbf1cSJohnny Huang 		writel(0x041930d4, OTP_TIMING);
778de6fbf1cSJohnny Huang 		break;
779d90825e2SJohnny Huang 	}
780de6fbf1cSJohnny Huang 
7813d3688adSJohnny Huang 	wait_complete();
782d90825e2SJohnny Huang }
783d90825e2SJohnny Huang 
784*83655e91SJohnny Huang static void otp_prog(uint32_t otp_addr, uint32_t prog_bit)
785*83655e91SJohnny Huang {
786*83655e91SJohnny Huang 	writel(otp_addr, OTP_ADDR); //write address
787*83655e91SJohnny Huang 	writel(prog_bit, OTP_COMPARE_1); //write data
788*83655e91SJohnny Huang 	writel(0x23b1e364, OTP_COMMAND); //write command
789*83655e91SJohnny Huang 	wait_complete();
790*83655e91SJohnny Huang }
791*83655e91SJohnny Huang 
792*83655e91SJohnny Huang static void _otp_prog_bit(uint32_t value, uint32_t prog_address, uint32_t bit_offset)
793*83655e91SJohnny Huang {
794*83655e91SJohnny Huang 	int prog_bit;
795*83655e91SJohnny Huang 
796*83655e91SJohnny Huang 	if (prog_address % 2 == 0) {
797*83655e91SJohnny Huang 		if (value)
798*83655e91SJohnny Huang 			prog_bit = ~(0x1 << bit_offset);
799*83655e91SJohnny Huang 		else
800*83655e91SJohnny Huang 			return;
801*83655e91SJohnny Huang 	} else {
802*83655e91SJohnny Huang 		prog_address |= 1 << 15;
803*83655e91SJohnny Huang 		if (!value)
804*83655e91SJohnny Huang 			prog_bit = 0x1 << bit_offset;
805*83655e91SJohnny Huang 		else
806*83655e91SJohnny Huang 			return;
807*83655e91SJohnny Huang 	}
808*83655e91SJohnny Huang 	otp_prog(prog_address, prog_bit);
809*83655e91SJohnny Huang }
810*83655e91SJohnny Huang 
811*83655e91SJohnny Huang static int otp_prog_bit(uint32_t value, uint32_t prog_address, uint32_t bit_offset)
812*83655e91SJohnny Huang {
813*83655e91SJohnny Huang 	int pass;
814*83655e91SJohnny Huang 	int i;
815*83655e91SJohnny Huang 
816*83655e91SJohnny Huang 	otp_soak(1);
817*83655e91SJohnny Huang 	_otp_prog_bit(value, prog_address, bit_offset);
818*83655e91SJohnny Huang 	pass = 0;
819*83655e91SJohnny Huang 
820*83655e91SJohnny Huang 	for (i = 0; i < RETRY; i++) {
821*83655e91SJohnny Huang 		if (verify_bit(prog_address, bit_offset, value) != 0) {
822*83655e91SJohnny Huang 			otp_soak(2);
823*83655e91SJohnny Huang 			_otp_prog_bit(value, prog_address, bit_offset);
824*83655e91SJohnny Huang 			if (verify_bit(prog_address, bit_offset, value) != 0) {
825*83655e91SJohnny Huang 				otp_soak(1);
826*83655e91SJohnny Huang 			} else {
827*83655e91SJohnny Huang 				pass = 1;
828*83655e91SJohnny Huang 				break;
829*83655e91SJohnny Huang 			}
830*83655e91SJohnny Huang 		} else {
831*83655e91SJohnny Huang 			pass = 1;
832*83655e91SJohnny Huang 			break;
833*83655e91SJohnny Huang 		}
834*83655e91SJohnny Huang 	}
835*83655e91SJohnny Huang 
836*83655e91SJohnny Huang 	return pass;
837*83655e91SJohnny Huang }
838*83655e91SJohnny Huang 
839696656c6SJohnny Huang static void otp_prog_dw(uint32_t value, uint32_t ignore, uint32_t prog_address)
840d90825e2SJohnny Huang {
841d90825e2SJohnny Huang 	int j, bit_value, prog_bit;
842d90825e2SJohnny Huang 
843d90825e2SJohnny Huang 	for (j = 0; j < 32; j++) {
844696656c6SJohnny Huang 		if ((ignore >> j) & 0x1)
845d90825e2SJohnny Huang 			continue;
846d90825e2SJohnny Huang 		bit_value = (value >> j) & 0x1;
847d90825e2SJohnny Huang 		if (prog_address % 2 == 0) {
848d90825e2SJohnny Huang 			if (bit_value)
849d90825e2SJohnny Huang 				prog_bit = ~(0x1 << j);
850d90825e2SJohnny Huang 			else
851d90825e2SJohnny Huang 				continue;
852d90825e2SJohnny Huang 		} else {
853d90825e2SJohnny Huang 			prog_address |= 1 << 15;
854d90825e2SJohnny Huang 			if (bit_value)
855d90825e2SJohnny Huang 				continue;
856d90825e2SJohnny Huang 			else
857d90825e2SJohnny Huang 				prog_bit = 0x1 << j;
858d90825e2SJohnny Huang 		}
859d90825e2SJohnny Huang 		otp_prog(prog_address, prog_bit);
860d90825e2SJohnny Huang 	}
861d90825e2SJohnny Huang }
862d90825e2SJohnny Huang 
86354552c69SJohnny Huang static int otp_prog_verify_2dw(uint32_t *data, uint32_t *buf, uint32_t *ignore_mask, uint32_t prog_address)
86454552c69SJohnny Huang {
86554552c69SJohnny Huang 	int pass;
86654552c69SJohnny Huang 	int i;
86754552c69SJohnny Huang 	uint32_t data0_masked;
86854552c69SJohnny Huang 	uint32_t data1_masked;
86954552c69SJohnny Huang 	uint32_t buf0_masked;
87054552c69SJohnny Huang 	uint32_t buf1_masked;
87154552c69SJohnny Huang 	uint32_t compare[2];
87254552c69SJohnny Huang 
87354552c69SJohnny Huang 	data0_masked = data[0]  & ~ignore_mask[0];
87454552c69SJohnny Huang 	buf0_masked  = buf[0] & ~ignore_mask[0];
87554552c69SJohnny Huang 	data1_masked = data[1]  & ~ignore_mask[1];
87654552c69SJohnny Huang 	buf1_masked  = buf[1] & ~ignore_mask[1];
87754552c69SJohnny Huang 	if ((data0_masked == buf0_masked) && (data1_masked == buf1_masked))
87854552c69SJohnny Huang 		return 0;
87954552c69SJohnny Huang 
88054552c69SJohnny Huang 	otp_soak(1);
88154552c69SJohnny Huang 	if (data0_masked != buf0_masked)
88254552c69SJohnny Huang 		otp_prog_dw(buf[0], ignore_mask[0], prog_address);
88354552c69SJohnny Huang 	if (data1_masked != buf1_masked)
88454552c69SJohnny Huang 		otp_prog_dw(buf[1], ignore_mask[1], prog_address + 1);
88554552c69SJohnny Huang 
88654552c69SJohnny Huang 	pass = 0;
88754552c69SJohnny Huang 	for (i = 0; i < RETRY; i++) {
88854552c69SJohnny Huang 		if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) {
88954552c69SJohnny Huang 			otp_soak(2);
89054552c69SJohnny Huang 			if (compare[0] != 0) {
89154552c69SJohnny Huang 				otp_prog_dw(compare[0], ignore_mask[0], prog_address);
89254552c69SJohnny Huang 			}
89354552c69SJohnny Huang 			if (compare[1] != ~0) {
89454552c69SJohnny Huang 				otp_prog_dw(compare[1], ignore_mask[0], prog_address + 1);
89554552c69SJohnny Huang 			}
89654552c69SJohnny Huang 			if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) {
89754552c69SJohnny Huang 				otp_soak(1);
89854552c69SJohnny Huang 			} else {
89954552c69SJohnny Huang 				pass = 1;
90054552c69SJohnny Huang 				break;
90154552c69SJohnny Huang 			}
90254552c69SJohnny Huang 		} else {
90354552c69SJohnny Huang 			pass = 1;
90454552c69SJohnny Huang 			break;
90554552c69SJohnny Huang 		}
90654552c69SJohnny Huang 	}
90754552c69SJohnny Huang 
90854552c69SJohnny Huang 	if (!pass) {
90954552c69SJohnny Huang 		otp_soak(0);
91054552c69SJohnny Huang 		return OTP_FAILURE;
91154552c69SJohnny Huang 	}
91254552c69SJohnny Huang 	return OTP_SUCCESS;
91354552c69SJohnny Huang }
91454552c69SJohnny Huang 
915541eb887SJohnny Huang static void otp_strap_status(struct otpstrap_status *otpstrap)
91676d13988SJohnny Huang {
91776d13988SJohnny Huang 	uint32_t OTPSTRAP_RAW[2];
9185010032bSJohnny Huang 	int strap_end;
91976d13988SJohnny Huang 	int i, j;
92076d13988SJohnny Huang 
9215010032bSJohnny Huang 	if (info_cb.version == OTP_AST2600A0) {
92276d13988SJohnny Huang 		for (j = 0; j < 64; j++) {
92376d13988SJohnny Huang 			otpstrap[j].value = 0;
92476d13988SJohnny Huang 			otpstrap[j].remain_times = 7;
92576d13988SJohnny Huang 			otpstrap[j].writeable_option = -1;
92676d13988SJohnny Huang 			otpstrap[j].protected = 0;
92776d13988SJohnny Huang 		}
9285010032bSJohnny Huang 		strap_end = 30;
9295010032bSJohnny Huang 	} else {
9305010032bSJohnny Huang 		for (j = 0; j < 64; j++) {
9315010032bSJohnny Huang 			otpstrap[j].value = 0;
9325010032bSJohnny Huang 			otpstrap[j].remain_times = 6;
9335010032bSJohnny Huang 			otpstrap[j].writeable_option = -1;
9345010032bSJohnny Huang 			otpstrap[j].reg_protected = 0;
9355010032bSJohnny Huang 			otpstrap[j].protected = 0;
9365010032bSJohnny Huang 		}
9375010032bSJohnny Huang 		strap_end = 28;
9385010032bSJohnny Huang 	}
93976d13988SJohnny Huang 
9405010032bSJohnny Huang 	for (i = 16; i < strap_end; i += 2) {
94176d13988SJohnny Huang 		int option = (i - 16) / 2;
94276d13988SJohnny Huang 		otp_read_config(i, &OTPSTRAP_RAW[0]);
94376d13988SJohnny Huang 		otp_read_config(i + 1, &OTPSTRAP_RAW[1]);
94476d13988SJohnny Huang 		for (j = 0; j < 32; j++) {
94576d13988SJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[0] >> j) & 0x1);
94676d13988SJohnny Huang 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
94776d13988SJohnny Huang 				otpstrap[j].writeable_option = option;
94876d13988SJohnny Huang 			}
94976d13988SJohnny Huang 			if (bit_value == 1)
95076d13988SJohnny Huang 				otpstrap[j].remain_times --;
95176d13988SJohnny Huang 			otpstrap[j].value ^= bit_value;
95276d13988SJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
95376d13988SJohnny Huang 		}
95476d13988SJohnny Huang 		for (j = 32; j < 64; j++) {
95576d13988SJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1);
95676d13988SJohnny Huang 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
95776d13988SJohnny Huang 				otpstrap[j].writeable_option = option;
95876d13988SJohnny Huang 			}
95976d13988SJohnny Huang 			if (bit_value == 1)
96076d13988SJohnny Huang 				otpstrap[j].remain_times --;
96176d13988SJohnny Huang 			otpstrap[j].value ^= bit_value;
96276d13988SJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
96376d13988SJohnny Huang 		}
96476d13988SJohnny Huang 	}
9655010032bSJohnny Huang 
9665010032bSJohnny Huang 	if (info_cb.version != OTP_AST2600A0) {
9675010032bSJohnny Huang 		otp_read_config(28, &OTPSTRAP_RAW[0]);
9685010032bSJohnny Huang 		otp_read_config(29, &OTPSTRAP_RAW[1]);
9695010032bSJohnny Huang 		for (j = 0; j < 32; j++) {
9705010032bSJohnny Huang 			if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
9715010032bSJohnny Huang 				otpstrap[j].reg_protected = 1;
9725010032bSJohnny Huang 		}
9735010032bSJohnny Huang 		for (j = 32; j < 64; j++) {
9745010032bSJohnny Huang 			if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
9755010032bSJohnny Huang 				otpstrap[j].reg_protected = 1;
9765010032bSJohnny Huang 		}
9775010032bSJohnny Huang 
9785010032bSJohnny Huang 	}
9795010032bSJohnny Huang 
98076d13988SJohnny Huang 	otp_read_config(30, &OTPSTRAP_RAW[0]);
98176d13988SJohnny Huang 	otp_read_config(31, &OTPSTRAP_RAW[1]);
98276d13988SJohnny Huang 	for (j = 0; j < 32; j++) {
98376d13988SJohnny Huang 		if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
98476d13988SJohnny Huang 			otpstrap[j].protected = 1;
98576d13988SJohnny Huang 	}
98676d13988SJohnny Huang 	for (j = 32; j < 64; j++) {
98776d13988SJohnny Huang 		if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
98876d13988SJohnny Huang 			otpstrap[j].protected = 1;
98976d13988SJohnny Huang 	}
99076d13988SJohnny Huang }
99176d13988SJohnny Huang 
992696656c6SJohnny Huang static int otp_print_conf_image(struct otp_image_layout *image_layout)
99369d5fd8fSJohnny Huang {
99479e42a59SJoel Stanley 	const struct otpconf_info *conf_info = info_cb.conf_info;
995696656c6SJohnny Huang 	uint32_t *OTPCFG = (uint32_t *)image_layout->conf;
996696656c6SJohnny Huang 	uint32_t *OTPCFG_IGNORE = (uint32_t *)image_layout->conf_ignore;
997b458cd62SJohnny Huang 	uint32_t mask;
998b458cd62SJohnny Huang 	uint32_t dw_offset;
999b458cd62SJohnny Huang 	uint32_t bit_offset;
1000b458cd62SJohnny Huang 	uint32_t otp_value;
1001696656c6SJohnny Huang 	uint32_t otp_ignore;
1002b458cd62SJohnny Huang 	int fail = 0;
100373f11549SJohnny Huang 	char valid_bit[20];
100466f2f8e5SJohnny Huang 	int i;
100573f11549SJohnny Huang 	int j;
100666f2f8e5SJohnny Huang 
1007737ed20bSJohnny Huang 	printf("DW    BIT        Value       Description\n");
100866f2f8e5SJohnny Huang 	printf("__________________________________________________________________________\n");
10093cb28812SJohnny Huang 	for (i = 0; i < info_cb.conf_info_len; i++) {
10103cb28812SJohnny Huang 		dw_offset = conf_info[i].dw_offset;
10113cb28812SJohnny Huang 		bit_offset = conf_info[i].bit_offset;
10123cb28812SJohnny Huang 		mask = BIT(conf_info[i].length) - 1;
1013b458cd62SJohnny Huang 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
1014696656c6SJohnny Huang 		otp_ignore = (OTPCFG_IGNORE[dw_offset] >> bit_offset) & mask;
1015b458cd62SJohnny Huang 
1016696656c6SJohnny Huang 		if (otp_ignore == mask) {
1017b458cd62SJohnny Huang 			continue;
1018696656c6SJohnny Huang 		} else if (otp_ignore != 0) {
1019b458cd62SJohnny Huang 			fail = 1;
1020b458cd62SJohnny Huang 		}
1021b458cd62SJohnny Huang 
10223cb28812SJohnny Huang 		if ((otp_value != conf_info[i].value) &&
10233cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_RESERVED &&
10243cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALUE &&
10253cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALID_BIT)
1026b458cd62SJohnny Huang 			continue;
1027b458cd62SJohnny Huang 		printf("0x%-4X", dw_offset);
1028b458cd62SJohnny Huang 
10293cb28812SJohnny Huang 		if (conf_info[i].length == 1) {
10303cb28812SJohnny Huang 			printf("0x%-9X", conf_info[i].bit_offset);
103166f2f8e5SJohnny Huang 		} else {
1032b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
10333cb28812SJohnny Huang 			       conf_info[i].bit_offset + conf_info[i].length - 1,
10343cb28812SJohnny Huang 			       conf_info[i].bit_offset);
103566f2f8e5SJohnny Huang 		}
1036b458cd62SJohnny Huang 		printf("0x%-10x", otp_value);
1037b458cd62SJohnny Huang 
1038b458cd62SJohnny Huang 		if (fail) {
1039696656c6SJohnny Huang 			printf("Ignore mask error\n");
1040b458cd62SJohnny Huang 		} else {
10413cb28812SJohnny Huang 			if (conf_info[i].value == OTP_REG_RESERVED) {
1042b458cd62SJohnny Huang 				printf("Reserved\n");
10433cb28812SJohnny Huang 			} else if (conf_info[i].value == OTP_REG_VALUE) {
10443cb28812SJohnny Huang 				printf(conf_info[i].information, otp_value);
1045b458cd62SJohnny Huang 				printf("\n");
10463cb28812SJohnny Huang 			} else if (conf_info[i].value == OTP_REG_VALID_BIT) {
1047b458cd62SJohnny Huang 				if (otp_value != 0) {
104873f11549SJohnny Huang 					for (j = 0; j < 7; j++) {
104973f11549SJohnny Huang 						if (otp_value == (1 << j)) {
105073f11549SJohnny Huang 							valid_bit[j * 2] = '1';
1051b458cd62SJohnny Huang 						} else {
105273f11549SJohnny Huang 							valid_bit[j * 2] = '0';
105373f11549SJohnny Huang 						}
105473f11549SJohnny Huang 						valid_bit[j * 2 + 1] = ' ';
105573f11549SJohnny Huang 					}
105673f11549SJohnny Huang 					valid_bit[15] = 0;
105773f11549SJohnny Huang 				} else {
105873f11549SJohnny Huang 					strcpy(valid_bit, "0 0 0 0 0 0 0 0\0");
1059b458cd62SJohnny Huang 				}
10603cb28812SJohnny Huang 				printf(conf_info[i].information, valid_bit);
1061b458cd62SJohnny Huang 				printf("\n");
1062b458cd62SJohnny Huang 			} else {
10633cb28812SJohnny Huang 				printf("%s\n", conf_info[i].information);
1064b458cd62SJohnny Huang 			}
1065b458cd62SJohnny Huang 		}
1066b458cd62SJohnny Huang 	}
1067b458cd62SJohnny Huang 
1068b458cd62SJohnny Huang 	if (fail)
1069b458cd62SJohnny Huang 		return OTP_FAILURE;
1070b458cd62SJohnny Huang 
107166f2f8e5SJohnny Huang 	return OTP_SUCCESS;
107266f2f8e5SJohnny Huang }
107366f2f8e5SJohnny Huang 
10742d4b0742SJohnny Huang static int otp_print_conf_info(int input_offset)
107566f2f8e5SJohnny Huang {
107679e42a59SJoel Stanley 	const struct otpconf_info *conf_info = info_cb.conf_info;
1077bb34a7bfSJohnny Huang 	uint32_t OTPCFG[16];
1078b458cd62SJohnny Huang 	uint32_t mask;
1079b458cd62SJohnny Huang 	uint32_t dw_offset;
1080b458cd62SJohnny Huang 	uint32_t bit_offset;
1081b458cd62SJohnny Huang 	uint32_t otp_value;
108273f11549SJohnny Huang 	char valid_bit[20];
108366f2f8e5SJohnny Huang 	int i;
108473f11549SJohnny Huang 	int j;
108566f2f8e5SJohnny Huang 
1086bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++)
108766f2f8e5SJohnny Huang 		otp_read_config(i, &OTPCFG[i]);
108866f2f8e5SJohnny Huang 
108966f2f8e5SJohnny Huang 
1090b458cd62SJohnny Huang 	printf("DW    BIT        Value       Description\n");
1091b458cd62SJohnny Huang 	printf("__________________________________________________________________________\n");
10923cb28812SJohnny Huang 	for (i = 0; i < info_cb.conf_info_len; i++) {
10933cb28812SJohnny Huang 		if (input_offset != -1 && input_offset != conf_info[i].dw_offset)
10942d4b0742SJohnny Huang 			continue;
10953cb28812SJohnny Huang 		dw_offset = conf_info[i].dw_offset;
10963cb28812SJohnny Huang 		bit_offset = conf_info[i].bit_offset;
10973cb28812SJohnny Huang 		mask = BIT(conf_info[i].length) - 1;
1098b458cd62SJohnny Huang 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
1099b458cd62SJohnny Huang 
11003cb28812SJohnny Huang 		if ((otp_value != conf_info[i].value) &&
11013cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_RESERVED &&
11023cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALUE &&
11033cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALID_BIT)
1104b458cd62SJohnny Huang 			continue;
1105b458cd62SJohnny Huang 		printf("0x%-4X", dw_offset);
1106b458cd62SJohnny Huang 
11073cb28812SJohnny Huang 		if (conf_info[i].length == 1) {
11083cb28812SJohnny Huang 			printf("0x%-9X", conf_info[i].bit_offset);
1109b458cd62SJohnny Huang 		} else {
1110b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
11113cb28812SJohnny Huang 			       conf_info[i].bit_offset + conf_info[i].length - 1,
11123cb28812SJohnny Huang 			       conf_info[i].bit_offset);
1113b458cd62SJohnny Huang 		}
1114b458cd62SJohnny Huang 		printf("0x%-10x", otp_value);
1115b458cd62SJohnny Huang 
11163cb28812SJohnny Huang 		if (conf_info[i].value == OTP_REG_RESERVED) {
1117b458cd62SJohnny Huang 			printf("Reserved\n");
11183cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALUE) {
11193cb28812SJohnny Huang 			printf(conf_info[i].information, otp_value);
1120b458cd62SJohnny Huang 			printf("\n");
11213cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALID_BIT) {
1122b458cd62SJohnny Huang 			if (otp_value != 0) {
112373f11549SJohnny Huang 				for (j = 0; j < 7; j++) {
112473f11549SJohnny Huang 					if (otp_value == (1 << j)) {
112573f11549SJohnny Huang 						valid_bit[j * 2] = '1';
1126b458cd62SJohnny Huang 					} else {
112773f11549SJohnny Huang 						valid_bit[j * 2] = '0';
112873f11549SJohnny Huang 					}
112973f11549SJohnny Huang 					valid_bit[j * 2 + 1] = ' ';
113073f11549SJohnny Huang 				}
113173f11549SJohnny Huang 				valid_bit[15] = 0;
113273f11549SJohnny Huang 			} else {
113373f11549SJohnny Huang 				strcpy(valid_bit, "0 0 0 0 0 0 0 0\0");
1134b458cd62SJohnny Huang 			}
11353cb28812SJohnny Huang 			printf(conf_info[i].information, valid_bit);
1136b458cd62SJohnny Huang 			printf("\n");
1137b458cd62SJohnny Huang 		} else {
11383cb28812SJohnny Huang 			printf("%s\n", conf_info[i].information);
1139b458cd62SJohnny Huang 		}
1140b458cd62SJohnny Huang 	}
1141b458cd62SJohnny Huang 	return OTP_SUCCESS;
114266f2f8e5SJohnny Huang }
114366f2f8e5SJohnny Huang 
11445010032bSJohnny Huang static int otp_print_strap_image(struct otp_image_layout *image_layout)
114576d13988SJohnny Huang {
114679e42a59SJoel Stanley 	const struct otpstrap_info *strap_info = info_cb.strap_info;
1147696656c6SJohnny Huang 	uint32_t *OTPSTRAP;
1148696656c6SJohnny Huang 	uint32_t *OTPSTRAP_REG_PRO;
1149696656c6SJohnny Huang 	uint32_t *OTPSTRAP_PRO;
1150696656c6SJohnny Huang 	uint32_t *OTPSTRAP_IGNORE;
115176d13988SJohnny Huang 	int i;
1152a8bd6d8cSJohnny Huang 	int fail = 0;
1153a8bd6d8cSJohnny Huang 	uint32_t bit_offset;
1154a8bd6d8cSJohnny Huang 	uint32_t dw_offset;
1155a8bd6d8cSJohnny Huang 	uint32_t mask;
1156a8bd6d8cSJohnny Huang 	uint32_t otp_value;
1157696656c6SJohnny Huang 	uint32_t otp_reg_protect;
1158a8bd6d8cSJohnny Huang 	uint32_t otp_protect;
1159696656c6SJohnny Huang 	uint32_t otp_ignore;
116076d13988SJohnny Huang 
1161696656c6SJohnny Huang 	OTPSTRAP = (uint32_t *)image_layout->strap;
1162696656c6SJohnny Huang 	OTPSTRAP_PRO = (uint32_t *)image_layout->strap_pro;
1163696656c6SJohnny Huang 	OTPSTRAP_IGNORE = (uint32_t *)image_layout->strap_ignore;
11645010032bSJohnny Huang 	if (info_cb.version == OTP_AST2600A0) {
1165696656c6SJohnny Huang 		OTPSTRAP_REG_PRO = NULL;
1166a8bd6d8cSJohnny Huang 		printf("BIT(hex)   Value       Protect     Description\n");
1167696656c6SJohnny Huang 	} else {
1168696656c6SJohnny Huang 		OTPSTRAP_REG_PRO = (uint32_t *)image_layout->strap_reg_pro;
1169de6b0cc4SJohnny Huang 		printf("BIT(hex)   Value       Reg_Protect Protect     Description\n");
1170696656c6SJohnny Huang 	}
1171de6b0cc4SJohnny Huang 	printf("__________________________________________________________________________________________\n");
1172b458cd62SJohnny Huang 
11733cb28812SJohnny Huang 	for (i = 0; i < info_cb.strap_info_len; i++) {
1174696656c6SJohnny Huang 		if (strap_info[i].bit_offset > 31) {
1175a8bd6d8cSJohnny Huang 			dw_offset = 1;
11763cb28812SJohnny Huang 			bit_offset = strap_info[i].bit_offset - 32;
1177a8bd6d8cSJohnny Huang 		} else {
1178a8bd6d8cSJohnny Huang 			dw_offset = 0;
11793cb28812SJohnny Huang 			bit_offset = strap_info[i].bit_offset;
1180a8bd6d8cSJohnny Huang 		}
118176d13988SJohnny Huang 
11823cb28812SJohnny Huang 		mask = BIT(strap_info[i].length) - 1;
1183a8bd6d8cSJohnny Huang 		otp_value = (OTPSTRAP[dw_offset] >> bit_offset) & mask;
1184a8bd6d8cSJohnny Huang 		otp_protect = (OTPSTRAP_PRO[dw_offset] >> bit_offset) & mask;
1185696656c6SJohnny Huang 		otp_ignore = (OTPSTRAP_IGNORE[dw_offset] >> bit_offset) & mask;
1186a8bd6d8cSJohnny Huang 
11875010032bSJohnny Huang 		if (info_cb.version != OTP_AST2600A0)
1188696656c6SJohnny Huang 			otp_reg_protect = (OTPSTRAP_REG_PRO[dw_offset] >> bit_offset) & mask;
11895010032bSJohnny Huang 		else
11905010032bSJohnny Huang 			otp_reg_protect = 0;
1191696656c6SJohnny Huang 
1192696656c6SJohnny Huang 		if (otp_ignore == mask) {
1193a8bd6d8cSJohnny Huang 			continue;
1194696656c6SJohnny Huang 		} else if (otp_ignore != 0) {
1195a8bd6d8cSJohnny Huang 			fail = 1;
1196a8bd6d8cSJohnny Huang 		}
1197a8bd6d8cSJohnny Huang 
11983cb28812SJohnny Huang 		if ((otp_value != strap_info[i].value) &&
11993cb28812SJohnny Huang 		    strap_info[i].value != OTP_REG_RESERVED)
1200a8bd6d8cSJohnny Huang 			continue;
1201a8bd6d8cSJohnny Huang 
12023cb28812SJohnny Huang 		if (strap_info[i].length == 1) {
12033cb28812SJohnny Huang 			printf("0x%-9X", strap_info[i].bit_offset);
1204a8bd6d8cSJohnny Huang 		} else {
1205b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
12063cb28812SJohnny Huang 			       strap_info[i].bit_offset + strap_info[i].length - 1,
12073cb28812SJohnny Huang 			       strap_info[i].bit_offset);
1208a8bd6d8cSJohnny Huang 		}
1209a8bd6d8cSJohnny Huang 		printf("0x%-10x", otp_value);
12105010032bSJohnny Huang 		if (info_cb.version != OTP_AST2600A0)
1211696656c6SJohnny Huang 			printf("0x%-10x", otp_reg_protect);
1212a8bd6d8cSJohnny Huang 		printf("0x%-10x", otp_protect);
1213a8bd6d8cSJohnny Huang 
1214a8bd6d8cSJohnny Huang 		if (fail) {
1215696656c6SJohnny Huang 			printf("Ignore mask error\n");
1216a8bd6d8cSJohnny Huang 		} else {
12173cb28812SJohnny Huang 			if (strap_info[i].value != OTP_REG_RESERVED)
12183cb28812SJohnny Huang 				printf("%s\n", strap_info[i].information);
1219a8bd6d8cSJohnny Huang 			else
1220a8bd6d8cSJohnny Huang 				printf("Reserved\n");
1221a8bd6d8cSJohnny Huang 		}
1222a8bd6d8cSJohnny Huang 	}
1223a8bd6d8cSJohnny Huang 
1224a8bd6d8cSJohnny Huang 	if (fail)
122576d13988SJohnny Huang 		return OTP_FAILURE;
122676d13988SJohnny Huang 
122776d13988SJohnny Huang 	return OTP_SUCCESS;
122876d13988SJohnny Huang }
122976d13988SJohnny Huang 
1230b458cd62SJohnny Huang static int otp_print_strap_info(int view)
123176d13988SJohnny Huang {
123279e42a59SJoel Stanley 	const struct otpstrap_info *strap_info = info_cb.strap_info;
123376d13988SJohnny Huang 	struct otpstrap_status strap_status[64];
123407baa4e8SJohnny Huang 	int i, j;
1235b458cd62SJohnny Huang 	int fail = 0;
1236b458cd62SJohnny Huang 	uint32_t bit_offset;
1237b458cd62SJohnny Huang 	uint32_t length;
1238b458cd62SJohnny Huang 	uint32_t otp_value;
1239b458cd62SJohnny Huang 	uint32_t otp_protect;
124076d13988SJohnny Huang 
1241541eb887SJohnny Huang 	otp_strap_status(strap_status);
124276d13988SJohnny Huang 
1243b458cd62SJohnny Huang 	if (view) {
1244*83655e91SJohnny Huang 		if (info_cb.version == OTP_AST2600A0)
124507baa4e8SJohnny Huang 			printf("BIT(hex) Value  Remains  Protect   Description\n");
1246*83655e91SJohnny Huang 		else
1247*83655e91SJohnny Huang 			printf("BIT(hex) Value  Remains  Reg_Protect Protect   Description\n");
124807baa4e8SJohnny Huang 		printf("___________________________________________________________________________________________________\n");
1249b458cd62SJohnny Huang 	} else {
1250b458cd62SJohnny Huang 		printf("BIT(hex)   Value       Description\n");
1251b458cd62SJohnny Huang 		printf("________________________________________________________________________________\n");
125276d13988SJohnny Huang 	}
12533cb28812SJohnny Huang 	for (i = 0; i < info_cb.strap_info_len; i++) {
1254b458cd62SJohnny Huang 		otp_value = 0;
12553cb28812SJohnny Huang 		bit_offset = strap_info[i].bit_offset;
12563cb28812SJohnny Huang 		length = strap_info[i].length;
1257b458cd62SJohnny Huang 		for (j = 0; j < length; j++) {
1258c947ef08SJohnny Huang 			otp_value |= strap_status[bit_offset + j].value << j;
1259c947ef08SJohnny Huang 			otp_protect |= strap_status[bit_offset + j].protected << j;
1260b458cd62SJohnny Huang 		}
12613cb28812SJohnny Huang 		if ((otp_value != strap_info[i].value) &&
12623cb28812SJohnny Huang 		    strap_info[i].value != OTP_REG_RESERVED)
1263b458cd62SJohnny Huang 			continue;
1264b458cd62SJohnny Huang 		if (view) {
1265b458cd62SJohnny Huang 			for (j = 0; j < length; j++) {
12663cb28812SJohnny Huang 				printf("0x%-7X", strap_info[i].bit_offset + j);
1267b458cd62SJohnny Huang 				printf("0x%-5X", strap_status[bit_offset + j].value);
126807baa4e8SJohnny Huang 				printf("%-9d", strap_status[bit_offset + j].remain_times);
1269*83655e91SJohnny Huang 				if (info_cb.version != OTP_AST2600A0)
1270*83655e91SJohnny Huang 					printf("0x%-10X", strap_status[bit_offset].reg_protected);
1271b458cd62SJohnny Huang 				printf("0x%-7X", strap_status[bit_offset].protected);
12723cb28812SJohnny Huang 				if (strap_info[i].value == OTP_REG_RESERVED) {
1273b458cd62SJohnny Huang 					printf(" Reserved\n");
1274b458cd62SJohnny Huang 					continue;
1275b458cd62SJohnny Huang 				}
1276b458cd62SJohnny Huang 				if (length == 1) {
12773cb28812SJohnny Huang 					printf(" %s\n", strap_info[i].information);
1278b458cd62SJohnny Huang 					continue;
127976d13988SJohnny Huang 				}
128076d13988SJohnny Huang 
1281b458cd62SJohnny Huang 				if (j == 0)
12823cb28812SJohnny Huang 					printf("/%s\n", strap_info[i].information);
1283b458cd62SJohnny Huang 				else if (j == length - 1)
1284b458cd62SJohnny Huang 					printf("\\ \"\n");
1285b458cd62SJohnny Huang 				else
1286b458cd62SJohnny Huang 					printf("| \"\n");
128776d13988SJohnny Huang 			}
1288b458cd62SJohnny Huang 		} else {
1289c947ef08SJohnny Huang 			if (length == 1) {
12903cb28812SJohnny Huang 				printf("0x%-9X", strap_info[i].bit_offset);
1291b458cd62SJohnny Huang 			} else {
1292b458cd62SJohnny Huang 				printf("0x%-2X:0x%-4X",
1293b458cd62SJohnny Huang 				       bit_offset + length - 1, bit_offset);
1294b458cd62SJohnny Huang 			}
1295b458cd62SJohnny Huang 
1296b458cd62SJohnny Huang 			printf("0x%-10X", otp_value);
1297b458cd62SJohnny Huang 
12983cb28812SJohnny Huang 			if (strap_info[i].value != OTP_REG_RESERVED)
12993cb28812SJohnny Huang 				printf("%s\n", strap_info[i].information);
1300b458cd62SJohnny Huang 			else
1301b458cd62SJohnny Huang 				printf("Reserved\n");
1302b458cd62SJohnny Huang 		}
1303b458cd62SJohnny Huang 	}
1304b458cd62SJohnny Huang 
1305b458cd62SJohnny Huang 	if (fail)
1306b458cd62SJohnny Huang 		return OTP_FAILURE;
1307b458cd62SJohnny Huang 
1308b458cd62SJohnny Huang 	return OTP_SUCCESS;
1309b458cd62SJohnny Huang }
1310b458cd62SJohnny Huang 
1311696656c6SJohnny Huang static void buf_print(uint8_t *buf, int len)
131269d5fd8fSJohnny Huang {
131369d5fd8fSJohnny Huang 	int i;
131469d5fd8fSJohnny Huang 	printf("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
131569d5fd8fSJohnny Huang 	for (i = 0; i < len; i++) {
131669d5fd8fSJohnny Huang 		if (i % 16 == 0) {
131769d5fd8fSJohnny Huang 			printf("%04X: ", i);
131869d5fd8fSJohnny Huang 		}
131969d5fd8fSJohnny Huang 		printf("%02X ", buf[i]);
132069d5fd8fSJohnny Huang 		if ((i + 1) % 16 == 0) {
132169d5fd8fSJohnny Huang 			printf("\n");
132269d5fd8fSJohnny Huang 		}
132369d5fd8fSJohnny Huang 	}
132469d5fd8fSJohnny Huang }
132569d5fd8fSJohnny Huang 
1326696656c6SJohnny Huang static int otp_print_data_info(struct otp_image_layout *image_layout)
132769d5fd8fSJohnny Huang {
132869d5fd8fSJohnny Huang 	int key_id, key_offset, last, key_type, key_length, exp_length;
132979e42a59SJoel Stanley 	const struct otpkey_type *key_info_array = info_cb.key_info;
13309a4fe690SJohnny Huang 	struct otpkey_type key_info;
1331696656c6SJohnny Huang 	uint32_t *buf;
1332696656c6SJohnny Huang 	uint8_t *byte_buf;
13339d998018SJohnny Huang 	char empty = 1;
133469d5fd8fSJohnny Huang 	int i = 0, len = 0;
13359a4fe690SJohnny Huang 	int j;
133654552c69SJohnny Huang 
1337696656c6SJohnny Huang 	byte_buf = image_layout->data;
1338696656c6SJohnny Huang 	buf = (uint32_t *)byte_buf;
13399d998018SJohnny Huang 
13409d998018SJohnny Huang 	for (i = 0; i < 16; i++) {
13419d998018SJohnny Huang 		if (buf[i] != 0) {
13429d998018SJohnny Huang 			empty = 0;
13439d998018SJohnny Huang 		}
13449d998018SJohnny Huang 	}
13459d998018SJohnny Huang 	if (empty)
13469d998018SJohnny Huang 		return 0;
13479d998018SJohnny Huang 
13489d998018SJohnny Huang 	i = 0;
134969d5fd8fSJohnny Huang 	while (1) {
135069d5fd8fSJohnny Huang 		key_id = buf[i] & 0x7;
135169d5fd8fSJohnny Huang 		key_offset = buf[i] & 0x1ff8;
135269d5fd8fSJohnny Huang 		last = (buf[i] >> 13) & 1;
135369d5fd8fSJohnny Huang 		key_type = (buf[i] >> 14) & 0xf;
135469d5fd8fSJohnny Huang 		key_length = (buf[i] >> 18) & 0x3;
135569d5fd8fSJohnny Huang 		exp_length = (buf[i] >> 20) & 0xfff;
13569a4fe690SJohnny Huang 
13579a4fe690SJohnny Huang 		for (j = 0; j < info_cb.key_info_len; j++) {
13589a4fe690SJohnny Huang 			if (key_type == key_info_array[j].value) {
13599a4fe690SJohnny Huang 				key_info = key_info_array[j];
13609a4fe690SJohnny Huang 				break;
13619a4fe690SJohnny Huang 			}
13629a4fe690SJohnny Huang 		}
13639a4fe690SJohnny Huang 
13647f795e57SJohnny Huang 		printf("\nKey[%d]:\n", i);
136569d5fd8fSJohnny Huang 		printf("Key Type: ");
13669a4fe690SJohnny Huang 		printf("%s\n", key_info.information);
13679a4fe690SJohnny Huang 
13689a4fe690SJohnny Huang 		if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
136969d5fd8fSJohnny Huang 			printf("HMAC SHA Type: ");
137069d5fd8fSJohnny Huang 			switch (key_length) {
137169d5fd8fSJohnny Huang 			case 0:
137269d5fd8fSJohnny Huang 				printf("HMAC(SHA224)\n");
137369d5fd8fSJohnny Huang 				break;
137469d5fd8fSJohnny Huang 			case 1:
137569d5fd8fSJohnny Huang 				printf("HMAC(SHA256)\n");
137669d5fd8fSJohnny Huang 				break;
137769d5fd8fSJohnny Huang 			case 2:
137869d5fd8fSJohnny Huang 				printf("HMAC(SHA384)\n");
137969d5fd8fSJohnny Huang 				break;
138069d5fd8fSJohnny Huang 			case 3:
138169d5fd8fSJohnny Huang 				printf("HMAC(SHA512)\n");
138269d5fd8fSJohnny Huang 				break;
138369d5fd8fSJohnny Huang 			}
13849a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA) {
138569d5fd8fSJohnny Huang 			printf("RSA SHA Type: ");
138669d5fd8fSJohnny Huang 			switch (key_length) {
138769d5fd8fSJohnny Huang 			case 0:
138869d5fd8fSJohnny Huang 				printf("RSA1024\n");
138969d5fd8fSJohnny Huang 				len = 0x100;
139069d5fd8fSJohnny Huang 				break;
139169d5fd8fSJohnny Huang 			case 1:
139269d5fd8fSJohnny Huang 				printf("RSA2048\n");
139369d5fd8fSJohnny Huang 				len = 0x200;
139469d5fd8fSJohnny Huang 				break;
139569d5fd8fSJohnny Huang 			case 2:
139669d5fd8fSJohnny Huang 				printf("RSA3072\n");
139769d5fd8fSJohnny Huang 				len = 0x300;
139869d5fd8fSJohnny Huang 				break;
139969d5fd8fSJohnny Huang 			case 3:
140069d5fd8fSJohnny Huang 				printf("RSA4096\n");
140169d5fd8fSJohnny Huang 				len = 0x400;
140269d5fd8fSJohnny Huang 				break;
140369d5fd8fSJohnny Huang 			}
140469d5fd8fSJohnny Huang 			printf("RSA exponent bit length: %d\n", exp_length);
140569d5fd8fSJohnny Huang 		}
14069a4fe690SJohnny Huang 		if (key_info.need_id)
140769d5fd8fSJohnny Huang 			printf("Key Number ID: %d\n", key_id);
140869d5fd8fSJohnny Huang 		printf("Key Value:\n");
14099a4fe690SJohnny Huang 		if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
141069d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], 0x40);
14119a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_AES) {
14129a4fe690SJohnny Huang 			printf("AES Key:\n");
14139a4fe690SJohnny Huang 			buf_print(&byte_buf[key_offset], 0x20);
14149a4fe690SJohnny Huang 			if (info_cb.version == 0) {
14159a4fe690SJohnny Huang 				printf("AES IV:\n");
14169a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x10);
14179a4fe690SJohnny Huang 			}
14189a4fe690SJohnny Huang 
14199a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_VAULT) {
14209a4fe690SJohnny Huang 			if (info_cb.version == 0) {
142169d5fd8fSJohnny Huang 				printf("AES Key:\n");
142269d5fd8fSJohnny Huang 				buf_print(&byte_buf[key_offset], 0x20);
142369d5fd8fSJohnny Huang 				printf("AES IV:\n");
142469d5fd8fSJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x10);
14259a4fe690SJohnny Huang 			} else if (info_cb.version == 1) {
14269a4fe690SJohnny Huang 				printf("AES Key 1:\n");
14279a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset], 0x20);
14289a4fe690SJohnny Huang 				printf("AES Key 2:\n");
14299a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x20);
14309a4fe690SJohnny Huang 			}
143169d5fd8fSJohnny Huang 
14329a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA) {
143369d5fd8fSJohnny Huang 			printf("RSA mod:\n");
143469d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], len / 2);
143569d5fd8fSJohnny Huang 			printf("RSA exp:\n");
143669d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset + (len / 2)], len / 2);
143769d5fd8fSJohnny Huang 		}
143869d5fd8fSJohnny Huang 		if (last)
143969d5fd8fSJohnny Huang 			break;
144069d5fd8fSJohnny Huang 		i++;
144169d5fd8fSJohnny Huang 	}
144269d5fd8fSJohnny Huang 	return 0;
144369d5fd8fSJohnny Huang }
144469d5fd8fSJohnny Huang 
14455010032bSJohnny Huang static int otp_prog_conf(struct otp_image_layout *image_layout)
144669d5fd8fSJohnny Huang {
1447a6d0d645SJohnny Huang 	int i, k;
1448d90825e2SJohnny Huang 	int pass = 0;
1449a6d0d645SJohnny Huang 	uint32_t prog_address;
1450bb34a7bfSJohnny Huang 	uint32_t data[16];
1451a6d0d645SJohnny Huang 	uint32_t compare[2];
14525010032bSJohnny Huang 	uint32_t *conf = (uint32_t *)image_layout->conf;
14535010032bSJohnny Huang 	uint32_t *conf_ignore = (uint32_t *)image_layout->conf_ignore;
1454d90825e2SJohnny Huang 	uint32_t data_masked;
1455d90825e2SJohnny Huang 	uint32_t buf_masked;
145669d5fd8fSJohnny Huang 
1457a6d0d645SJohnny Huang 	printf("Read OTP Config Region:\n");
1458a6d0d645SJohnny Huang 
1459bb34a7bfSJohnny Huang 	for (i = 0; i < 16 ; i ++) {
146069d5fd8fSJohnny Huang 		prog_address = 0x800;
1461a6d0d645SJohnny Huang 		prog_address |= (i / 8) * 0x200;
1462a6d0d645SJohnny Huang 		prog_address |= (i % 8) * 0x2;
1463a6d0d645SJohnny Huang 		otp_read_data(prog_address, &data[i]);
1464a6d0d645SJohnny Huang 	}
1465a6d0d645SJohnny Huang 
1466a6d0d645SJohnny Huang 	printf("Check writable...\n");
1467bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++) {
14685010032bSJohnny Huang 		data_masked = data[i]  & ~conf_ignore[i];
14695010032bSJohnny Huang 		buf_masked  = conf[i] & ~conf_ignore[i];
1470d90825e2SJohnny Huang 		if (data_masked == buf_masked)
147169d5fd8fSJohnny Huang 			continue;
1472d90825e2SJohnny Huang 		if ((data_masked | buf_masked) == buf_masked) {
1473a6d0d645SJohnny Huang 			continue;
1474a6d0d645SJohnny Huang 		} else {
1475a6d0d645SJohnny Huang 			printf("Input image can't program into OTP, please check.\n");
1476a6af4a17SJohnny Huang 			printf("OTPCFG[%X] = %x\n", i, data[i]);
14775010032bSJohnny Huang 			printf("Input [%X] = %x\n", i, conf[i]);
14785010032bSJohnny Huang 			printf("Mask  [%X] = %x\n", i, ~conf_ignore[i]);
14792a856b9aSJohnny Huang 			return OTP_FAILURE;
1480a6d0d645SJohnny Huang 		}
1481a6d0d645SJohnny Huang 	}
1482a6d0d645SJohnny Huang 
1483a6d0d645SJohnny Huang 	printf("Start Programing...\n");
1484d90825e2SJohnny Huang 	otp_soak(0);
1485bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++) {
14865010032bSJohnny Huang 		data_masked = data[i]  & ~conf_ignore[i];
14875010032bSJohnny Huang 		buf_masked  = conf[i] & ~conf_ignore[i];
1488a6d0d645SJohnny Huang 		prog_address = 0x800;
1489a6d0d645SJohnny Huang 		prog_address |= (i / 8) * 0x200;
1490a6d0d645SJohnny Huang 		prog_address |= (i % 8) * 0x2;
1491bb34a7bfSJohnny Huang 		if (data_masked == buf_masked) {
1492bb34a7bfSJohnny Huang 			pass = 1;
1493a6d0d645SJohnny Huang 			continue;
1494bb34a7bfSJohnny Huang 		}
1495de6fbf1cSJohnny Huang 
1496a6d0d645SJohnny Huang 
1497de6fbf1cSJohnny Huang 		otp_soak(1);
14985010032bSJohnny Huang 		otp_prog_dw(conf[i], conf_ignore[i], prog_address);
1499a6d0d645SJohnny Huang 
150069d5fd8fSJohnny Huang 		pass = 0;
150169d5fd8fSJohnny Huang 		for (k = 0; k < RETRY; k++) {
15025010032bSJohnny Huang 			if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) {
1503de6fbf1cSJohnny Huang 				otp_soak(2);
1504a6d0d645SJohnny Huang 				otp_prog_dw(compare[0], prog_address, 1);
15055010032bSJohnny Huang 				if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) {
1506de6fbf1cSJohnny Huang 					otp_soak(1);
1507de6fbf1cSJohnny Huang 				} else {
1508de6fbf1cSJohnny Huang 					pass = 1;
1509de6fbf1cSJohnny Huang 					break;
1510de6fbf1cSJohnny Huang 				}
1511a6d0d645SJohnny Huang 			} else {
151269d5fd8fSJohnny Huang 				pass = 1;
151369d5fd8fSJohnny Huang 				break;
151469d5fd8fSJohnny Huang 			}
151569d5fd8fSJohnny Huang 		}
1516bb34a7bfSJohnny Huang 		if (pass == 0) {
1517bb34a7bfSJohnny Huang 			printf("address: %08x, data: %08x, buffer: %08x, mask: %08x\n",
15185010032bSJohnny Huang 			       i, data[i], conf[i], conf_ignore[i]);
1519bb34a7bfSJohnny Huang 			break;
1520bb34a7bfSJohnny Huang 		}
1521a6d0d645SJohnny Huang 	}
1522a6d0d645SJohnny Huang 
1523de6fbf1cSJohnny Huang 	otp_soak(0);
152469d5fd8fSJohnny Huang 	if (!pass)
15252a856b9aSJohnny Huang 		return OTP_FAILURE;
1526a6d0d645SJohnny Huang 
15272a856b9aSJohnny Huang 	return OTP_SUCCESS;
1528d90825e2SJohnny Huang 
152969d5fd8fSJohnny Huang }
153069d5fd8fSJohnny Huang 
1531eda10d61SJohnny Huang static int otp_strap_bit_confirm(struct otpstrap_status *otpstrap, int offset, int ibit, int bit, int pbit, int rpbit)
1532eda10d61SJohnny Huang {
1533eda10d61SJohnny Huang 	if (ibit == 1) {
1534eda10d61SJohnny Huang 		return OTP_SUCCESS;
1535eda10d61SJohnny Huang 	} else {
1536eda10d61SJohnny Huang 		printf("OTPSTRAP[%X]:\n", offset);
1537eda10d61SJohnny Huang 	}
1538eda10d61SJohnny Huang 	if (bit == otpstrap->value) {
1539eda10d61SJohnny Huang 		printf("    The value is same as before, skip it.\n");
1540eda10d61SJohnny Huang 		return OTP_PROG_SKIP;
1541eda10d61SJohnny Huang 	}
1542eda10d61SJohnny Huang 	if (otpstrap->protected == 1) {
1543eda10d61SJohnny Huang 		printf("    This bit is protected and is not writable\n");
1544eda10d61SJohnny Huang 		return OTP_FAILURE;
1545eda10d61SJohnny Huang 	}
1546eda10d61SJohnny Huang 	if (otpstrap->remain_times == 0) {
1547eda10d61SJohnny Huang 		printf("    This bit is no remaining times to write.\n");
1548eda10d61SJohnny Huang 		return OTP_FAILURE;
1549eda10d61SJohnny Huang 	}
1550eda10d61SJohnny Huang 	if (pbit == 1) {
1551eda10d61SJohnny Huang 		printf("    This bit will be protected and become non-writable.\n");
1552eda10d61SJohnny Huang 	}
1553eda10d61SJohnny Huang 	if (rpbit == 1 && info_cb.version != OTP_AST2600A0) {
1554eda10d61SJohnny Huang 		printf("    The relative register will be protected.\n");
1555eda10d61SJohnny Huang 	}
1556eda10d61SJohnny Huang 	printf("    Write 1 to OTPSTRAP[%X] OPTION[%X], that value becomes from %d to %d.\n", offset, otpstrap->writeable_option + 1, otpstrap->value, otpstrap->value ^ 1);
1557eda10d61SJohnny Huang 	return OTP_SUCCESS;
1558eda10d61SJohnny Huang }
1559eda10d61SJohnny Huang 
15605010032bSJohnny Huang static int otp_strap_image_confirm(struct otp_image_layout *image_layout)
156169d5fd8fSJohnny Huang {
156269d5fd8fSJohnny Huang 	int i;
15635010032bSJohnny Huang 	uint32_t *strap;
15645010032bSJohnny Huang 	uint32_t *strap_ignore;
15655010032bSJohnny Huang 	uint32_t *strap_reg_protect;
15665010032bSJohnny Huang 	uint32_t *strap_pro;
1567eda10d61SJohnny Huang 	int bit, pbit, ibit, rpbit;
156869d5fd8fSJohnny Huang 	int fail = 0;
1569a6af4a17SJohnny Huang 	int skip = -1;
1570eda10d61SJohnny Huang 	int ret;
157166f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
157269d5fd8fSJohnny Huang 
15735010032bSJohnny Huang 	strap = (uint32_t *)image_layout->strap;
15745010032bSJohnny Huang 	strap_pro = (uint32_t *)image_layout->strap_pro;
15755010032bSJohnny Huang 	strap_ignore = (uint32_t *)image_layout->strap_ignore;
15765010032bSJohnny Huang 	strap_reg_protect = (uint32_t *)image_layout->strap_reg_pro;
15775010032bSJohnny Huang 
1578541eb887SJohnny Huang 	otp_strap_status(otpstrap);
157969d5fd8fSJohnny Huang 	for (i = 0; i < 64; i++) {
158069d5fd8fSJohnny Huang 		if (i < 32) {
15815010032bSJohnny Huang 			bit = (strap[0] >> i) & 0x1;
1582eda10d61SJohnny Huang 			ibit = (strap_ignore[0] >> i) & 0x1;
15835010032bSJohnny Huang 			pbit = (strap_pro[0] >> i) & 0x1;
158469d5fd8fSJohnny Huang 		} else {
15855010032bSJohnny Huang 			bit = (strap[1] >> (i - 32)) & 0x1;
1586eda10d61SJohnny Huang 			ibit = (strap_ignore[1] >> (i - 32)) & 0x1;
15875010032bSJohnny Huang 			pbit = (strap_pro[1] >> (i - 32)) & 0x1;
15885010032bSJohnny Huang 		}
15895010032bSJohnny Huang 
15905010032bSJohnny Huang 		if (info_cb.version != OTP_AST2600A0) {
15915010032bSJohnny Huang 			if (i < 32) {
15925010032bSJohnny Huang 				rpbit = (strap_reg_protect[0] >> i) & 0x1;
15935010032bSJohnny Huang 			} else {
15945010032bSJohnny Huang 				rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1;
15955010032bSJohnny Huang 			}
15965010032bSJohnny Huang 		} else {
15975010032bSJohnny Huang 			rpbit = 0;
159869d5fd8fSJohnny Huang 		}
1599eda10d61SJohnny Huang 		ret = otp_strap_bit_confirm(&otpstrap[i], i, ibit, bit, pbit, rpbit);
1600eda10d61SJohnny Huang 		if (ret == OTP_PROG_SKIP) {
1601a6af4a17SJohnny Huang 			if (skip == -1)
1602a6af4a17SJohnny Huang 				skip = 1;
160369d5fd8fSJohnny Huang 			continue;
1604a6af4a17SJohnny Huang 		} else {
1605eda10d61SJohnny Huang 			skip = 1;
160669d5fd8fSJohnny Huang 		}
1607eda10d61SJohnny Huang 
1608eda10d61SJohnny Huang 		if (ret == OTP_FAILURE)
160969d5fd8fSJohnny Huang 			fail = 1;
161069d5fd8fSJohnny Huang 	}
161169d5fd8fSJohnny Huang 	if (fail == 1)
1612a6af4a17SJohnny Huang 		return OTP_FAILURE;
1613a6af4a17SJohnny Huang 	else if (skip == 1)
1614a6af4a17SJohnny Huang 		return OTP_PROG_SKIP;
16157e22f42dSJohnny Huang 
1616eda10d61SJohnny Huang 	return OTP_SUCCESS;
161769d5fd8fSJohnny Huang }
161869d5fd8fSJohnny Huang 
16192a856b9aSJohnny Huang static int otp_print_strap(int start, int count)
162069d5fd8fSJohnny Huang {
162169d5fd8fSJohnny Huang 	int i, j;
1622de6b0cc4SJohnny Huang 	int remains;
162366f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
162469d5fd8fSJohnny Huang 
16252a856b9aSJohnny Huang 	if (start < 0 || start > 64)
16262a856b9aSJohnny Huang 		return OTP_USAGE;
16272a856b9aSJohnny Huang 
16282a856b9aSJohnny Huang 	if ((start + count) < 0 || (start + count) > 64)
16292a856b9aSJohnny Huang 		return OTP_USAGE;
16302a856b9aSJohnny Huang 
1631541eb887SJohnny Huang 	otp_strap_status(otpstrap);
163269d5fd8fSJohnny Huang 
1633de6b0cc4SJohnny Huang 	if (info_cb.version == OTP_AST2600A0) {
1634de6b0cc4SJohnny Huang 		remains = 7;
163507baa4e8SJohnny Huang 		printf("BIT(hex)  Value  Option           Status\n");
1636de6b0cc4SJohnny Huang 	} else {
1637de6b0cc4SJohnny Huang 		remains = 6;
1638de6b0cc4SJohnny Huang 		printf("BIT(hex)  Value  Option         Reg_Protect Status\n");
1639de6b0cc4SJohnny Huang 	}
1640de6b0cc4SJohnny Huang 	printf("______________________________________________________________________________\n");
1641737ed20bSJohnny Huang 
1642cd1610b4SJohnny Huang 	for (i = start; i < start + count; i++) {
164307baa4e8SJohnny Huang 		printf("0x%-8X", i);
1644737ed20bSJohnny Huang 		printf("%-7d", otpstrap[i].value);
1645de6b0cc4SJohnny Huang 		for (j = 0; j < remains; j++)
1646737ed20bSJohnny Huang 			printf("%d ", otpstrap[i].option_array[j]);
1647737ed20bSJohnny Huang 		printf("   ");
1648de6b0cc4SJohnny Huang 		if (info_cb.version != OTP_AST2600A0) {
1649de6b0cc4SJohnny Huang 			printf("%d           ", otpstrap[i].reg_protected);
1650de6b0cc4SJohnny Huang 		}
165169d5fd8fSJohnny Huang 		if (otpstrap[i].protected == 1) {
1652737ed20bSJohnny Huang 			printf("protected and not writable");
165369d5fd8fSJohnny Huang 		} else {
1654737ed20bSJohnny Huang 			printf("not protected ");
165569d5fd8fSJohnny Huang 			if (otpstrap[i].remain_times == 0) {
1656737ed20bSJohnny Huang 				printf("and no remaining times to write.");
165769d5fd8fSJohnny Huang 			} else {
1658737ed20bSJohnny Huang 				printf("and still can write %d times", otpstrap[i].remain_times);
165969d5fd8fSJohnny Huang 			}
166069d5fd8fSJohnny Huang 		}
1661737ed20bSJohnny Huang 		printf("\n");
166269d5fd8fSJohnny Huang 	}
16632a856b9aSJohnny Huang 
16642a856b9aSJohnny Huang 	return OTP_SUCCESS;
166569d5fd8fSJohnny Huang }
166669d5fd8fSJohnny Huang 
16678848d5dcSJohnny Huang static int otp_prog_strap_bit(int bit_offset, int value)
16688848d5dcSJohnny Huang {
16698848d5dcSJohnny Huang 	struct otpstrap_status otpstrap[64];
1670*83655e91SJohnny Huang 	uint32_t prog_address;
16718848d5dcSJohnny Huang 	int offset;
16728848d5dcSJohnny Huang 	int ret;
16738848d5dcSJohnny Huang 
16748848d5dcSJohnny Huang 
16758848d5dcSJohnny Huang 	otp_strap_status(otpstrap);
16768848d5dcSJohnny Huang 
16778848d5dcSJohnny Huang 	ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0, 0);
16788848d5dcSJohnny Huang 
16798848d5dcSJohnny Huang 	if (ret != OTP_SUCCESS) {
16808848d5dcSJohnny Huang 		return ret;
16818848d5dcSJohnny Huang 	}
16828848d5dcSJohnny Huang 
16838848d5dcSJohnny Huang 	prog_address = 0x800;
16848848d5dcSJohnny Huang 	if (bit_offset < 32) {
16858848d5dcSJohnny Huang 		offset = bit_offset;
16868848d5dcSJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) / 8) * 0x200;
16878848d5dcSJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) % 8) * 0x2;
16888848d5dcSJohnny Huang 
16898848d5dcSJohnny Huang 	} else {
16908848d5dcSJohnny Huang 		offset = (bit_offset - 32);
16918848d5dcSJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) / 8) * 0x200;
16928848d5dcSJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) % 8) * 0x2;
16938848d5dcSJohnny Huang 	}
16948848d5dcSJohnny Huang 
16958848d5dcSJohnny Huang 
1696*83655e91SJohnny Huang 	return otp_prog_bit(1, prog_address, offset);
16978848d5dcSJohnny Huang }
16988848d5dcSJohnny Huang 
16995010032bSJohnny Huang static int otp_prog_strap(struct otp_image_layout *image_layout)
170069d5fd8fSJohnny Huang {
17015010032bSJohnny Huang 	uint32_t *strap;
17025010032bSJohnny Huang 	uint32_t *strap_ignore;
17035010032bSJohnny Huang 	uint32_t *strap_pro;
17045010032bSJohnny Huang 	uint32_t *strap_reg_protect;
1705*83655e91SJohnny Huang 	uint32_t prog_address;
1706*83655e91SJohnny Huang 	int i;
1707eda10d61SJohnny Huang 	int bit, pbit, ibit, offset, rpbit;
170869d5fd8fSJohnny Huang 	int fail = 0;
1709*83655e91SJohnny Huang 	int ret;
171066f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
171169d5fd8fSJohnny Huang 
17125010032bSJohnny Huang 	strap = (uint32_t *)image_layout->strap;
17135010032bSJohnny Huang 	strap_pro = (uint32_t *)image_layout->strap_pro;
17145010032bSJohnny Huang 	strap_ignore = (uint32_t *)image_layout->strap_ignore;
17155010032bSJohnny Huang 	strap_reg_protect = (uint32_t *)image_layout->strap_reg_pro;
17165010032bSJohnny Huang 
17177f795e57SJohnny Huang 	printf("Read OTP Strap Region:\n");
1718541eb887SJohnny Huang 	otp_strap_status(otpstrap);
171969d5fd8fSJohnny Huang 
17207f795e57SJohnny Huang 	printf("Check writable...\n");
17215010032bSJohnny Huang 	if (otp_strap_image_confirm(image_layout) == OTP_FAILURE) {
17227f795e57SJohnny Huang 		printf("Input image can't program into OTP, please check.\n");
17237f795e57SJohnny Huang 		return OTP_FAILURE;
17247f795e57SJohnny Huang 	}
17257e22f42dSJohnny Huang 
172669d5fd8fSJohnny Huang 	for (i = 0; i < 64; i++) {
172769d5fd8fSJohnny Huang 		prog_address = 0x800;
172869d5fd8fSJohnny Huang 		if (i < 32) {
172969d5fd8fSJohnny Huang 			offset = i;
17305010032bSJohnny Huang 			bit = (strap[0] >> offset) & 0x1;
1731eda10d61SJohnny Huang 			ibit = (strap_ignore[0] >> offset) & 0x1;
17325010032bSJohnny Huang 			pbit = (strap_pro[0] >> offset) & 0x1;
173369d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200;
173469d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2;
173569d5fd8fSJohnny Huang 
173669d5fd8fSJohnny Huang 		} else {
173769d5fd8fSJohnny Huang 			offset = (i - 32);
17385010032bSJohnny Huang 			bit = (strap[1] >> offset) & 0x1;
1739eda10d61SJohnny Huang 			ibit = (strap_ignore[1] >> offset) & 0x1;
17405010032bSJohnny Huang 			pbit = (strap_pro[1] >> offset) & 0x1;
174169d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200;
174269d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2;
174369d5fd8fSJohnny Huang 		}
17445010032bSJohnny Huang 		if (info_cb.version != OTP_AST2600A0) {
17455010032bSJohnny Huang 			if (i < 32) {
17465010032bSJohnny Huang 				rpbit = (strap_reg_protect[0] >> i) & 0x1;
17475010032bSJohnny Huang 			} else {
17485010032bSJohnny Huang 				rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1;
17495010032bSJohnny Huang 			}
17505010032bSJohnny Huang 		} else {
17515010032bSJohnny Huang 			rpbit = 0;
17525010032bSJohnny Huang 		}
175369d5fd8fSJohnny Huang 
1754eda10d61SJohnny Huang 		if (ibit == 1) {
175569d5fd8fSJohnny Huang 			continue;
175669d5fd8fSJohnny Huang 		}
175769d5fd8fSJohnny Huang 		if (bit == otpstrap[i].value) {
175869d5fd8fSJohnny Huang 			continue;
175969d5fd8fSJohnny Huang 		}
176069d5fd8fSJohnny Huang 		if (otpstrap[i].protected == 1) {
176169d5fd8fSJohnny Huang 			fail = 1;
176269d5fd8fSJohnny Huang 			continue;
176369d5fd8fSJohnny Huang 		}
176469d5fd8fSJohnny Huang 		if (otpstrap[i].remain_times == 0) {
176569d5fd8fSJohnny Huang 			fail = 1;
176669d5fd8fSJohnny Huang 			continue;
176769d5fd8fSJohnny Huang 		}
17687e22f42dSJohnny Huang 
1769*83655e91SJohnny Huang 		ret = otp_prog_bit(1, prog_address, offset);
1770*83655e91SJohnny Huang 		if (!ret)
17712a856b9aSJohnny Huang 			return OTP_FAILURE;
177269d5fd8fSJohnny Huang 
17735010032bSJohnny Huang 		if (rpbit == 1 && info_cb.version != OTP_AST2600A0) {
177469d5fd8fSJohnny Huang 			prog_address = 0x800;
177569d5fd8fSJohnny Huang 			if (i < 32)
17765010032bSJohnny Huang 				prog_address |= 0x608;
177769d5fd8fSJohnny Huang 			else
17785010032bSJohnny Huang 				prog_address |= 0x60a;
17797e22f42dSJohnny Huang 
1780*83655e91SJohnny Huang 			ret = otp_prog_bit(1, prog_address, offset);
1781*83655e91SJohnny Huang 			if (!ret)
17822a856b9aSJohnny Huang 				return OTP_FAILURE;
17835010032bSJohnny Huang 		}
17845010032bSJohnny Huang 
17855010032bSJohnny Huang 		if (pbit != 0) {
17865010032bSJohnny Huang 			prog_address = 0x800;
17875010032bSJohnny Huang 			if (i < 32)
17885010032bSJohnny Huang 				prog_address |= 0x60c;
17895010032bSJohnny Huang 			else
17905010032bSJohnny Huang 				prog_address |= 0x60e;
17915010032bSJohnny Huang 
1792*83655e91SJohnny Huang 			ret = otp_prog_bit(1, prog_address, offset);
1793*83655e91SJohnny Huang 			if (!ret)
17945010032bSJohnny Huang 				return OTP_FAILURE;
17955010032bSJohnny Huang 		}
179669d5fd8fSJohnny Huang 
179769d5fd8fSJohnny Huang 	}
1798de6fbf1cSJohnny Huang 	otp_soak(0);
179969d5fd8fSJohnny Huang 	if (fail == 1)
18002a856b9aSJohnny Huang 		return OTP_FAILURE;
180169d5fd8fSJohnny Huang 	else
18022a856b9aSJohnny Huang 		return OTP_SUCCESS;
180369d5fd8fSJohnny Huang 
180469d5fd8fSJohnny Huang }
180569d5fd8fSJohnny Huang 
18065010032bSJohnny Huang static int otp_prog_data(struct otp_image_layout *image_layout)
18074c1c9b35SJohnny Huang {
180854552c69SJohnny Huang 	int i;
180954552c69SJohnny Huang 	int ret;
18105010032bSJohnny Huang 	int data_dw;
1811d90825e2SJohnny Huang 	uint32_t data[2048];
18125010032bSJohnny Huang 	uint32_t *buf;
18135010032bSJohnny Huang 	uint32_t *buf_ignore;
18144c1c9b35SJohnny Huang 
181554552c69SJohnny Huang 	uint32_t data_masked;
181654552c69SJohnny Huang 	uint32_t buf_masked;
18174c1c9b35SJohnny Huang 
18185010032bSJohnny Huang 	buf = (uint32_t *)image_layout->data;
18195010032bSJohnny Huang 	buf_ignore = (uint32_t *)image_layout->data_ignore;
18205010032bSJohnny Huang 
18215010032bSJohnny Huang 	data_dw = image_layout->data_length / 4;
18225010032bSJohnny Huang 
18234c1c9b35SJohnny Huang 	printf("Read OTP Data:\n");
18244c1c9b35SJohnny Huang 
18255010032bSJohnny Huang 	for (i = 0; i < data_dw - 2 ; i += 2) {
1826d90825e2SJohnny Huang 		otp_read_data(i, &data[i]);
18274c1c9b35SJohnny Huang 	}
1828d90825e2SJohnny Huang 
18294c1c9b35SJohnny Huang 	printf("Check writable...\n");
183054552c69SJohnny Huang 	// ignore last two dw, the last two dw is used for slt otp write check.
18315010032bSJohnny Huang 	for (i = 0; i < data_dw - 2; i++) {
1832696656c6SJohnny Huang 		data_masked = data[i]  & ~buf_ignore[i];
1833696656c6SJohnny Huang 		buf_masked  = buf[i] & ~buf_ignore[i];
183454552c69SJohnny Huang 		if (data_masked == buf_masked)
18354c1c9b35SJohnny Huang 			continue;
1836d90825e2SJohnny Huang 		if (i % 2 == 0) {
183754552c69SJohnny Huang 			if ((data_masked | buf_masked) == buf_masked) {
18384c1c9b35SJohnny Huang 				continue;
18394c1c9b35SJohnny Huang 			} else {
18404c1c9b35SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
1841d90825e2SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
18424c1c9b35SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
1843696656c6SJohnny Huang 				printf("Mask    [%x] = %x\n", i, ~buf_ignore[i]);
18442a856b9aSJohnny Huang 				return OTP_FAILURE;
184569d5fd8fSJohnny Huang 			}
1846d90825e2SJohnny Huang 		} else {
184754552c69SJohnny Huang 			if ((data_masked & buf_masked) == buf_masked) {
1848d90825e2SJohnny Huang 				continue;
1849d90825e2SJohnny Huang 			} else {
1850d90825e2SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
1851d90825e2SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
1852d90825e2SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
1853696656c6SJohnny Huang 				printf("Mask    [%x] = %x\n", i, ~buf_ignore[i]);
18542a856b9aSJohnny Huang 				return OTP_FAILURE;
1855d90825e2SJohnny Huang 			}
1856d90825e2SJohnny Huang 		}
1857d90825e2SJohnny Huang 	}
185869d5fd8fSJohnny Huang 
1859d90825e2SJohnny Huang 	printf("Start Programing...\n");
1860d90825e2SJohnny Huang 
186154552c69SJohnny Huang 	// programing ecc region first
186254552c69SJohnny Huang 	for (i = 1792; i < 2046; i += 2) {
1863696656c6SJohnny Huang 		ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i);
186454552c69SJohnny Huang 		if (ret != OTP_SUCCESS) {
186554552c69SJohnny Huang 			printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n",
1866696656c6SJohnny Huang 			       i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]);
186754552c69SJohnny Huang 			return ret;
1868d90825e2SJohnny Huang 		}
1869d90825e2SJohnny Huang 	}
1870d90825e2SJohnny Huang 
187154552c69SJohnny Huang 	for (i = 0; i < 1792; i += 2) {
1872696656c6SJohnny Huang 		ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i);
187354552c69SJohnny Huang 		if (ret != OTP_SUCCESS) {
187454552c69SJohnny Huang 			printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n",
1875696656c6SJohnny Huang 			       i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]);
187654552c69SJohnny Huang 			return ret;
1877d90825e2SJohnny Huang 		}
1878de6fbf1cSJohnny Huang 	}
1879de6fbf1cSJohnny Huang 	otp_soak(0);
18802a856b9aSJohnny Huang 	return OTP_SUCCESS;
1881d90825e2SJohnny Huang 
1882d90825e2SJohnny Huang }
1883d90825e2SJohnny Huang 
1884696656c6SJohnny Huang static int otp_image_verify(uint8_t *src_buf, uint32_t length, uint8_t *digest_buf)
1885696656c6SJohnny Huang {
1886696656c6SJohnny Huang 	sha256_context ctx;
1887696656c6SJohnny Huang 	u8 digest_ret[CHECKSUM_LEN];
1888696656c6SJohnny Huang 
1889696656c6SJohnny Huang 	sha256_starts(&ctx);
1890696656c6SJohnny Huang 	sha256_update(&ctx, src_buf, length);
1891696656c6SJohnny Huang 	sha256_finish(&ctx, digest_ret);
1892696656c6SJohnny Huang 
1893696656c6SJohnny Huang 	if (!memcmp(digest_buf, digest_ret, CHECKSUM_LEN))
1894696656c6SJohnny Huang 		return 0;
1895696656c6SJohnny Huang 	else
1896696656c6SJohnny Huang 		return -1;
1897696656c6SJohnny Huang 
1898696656c6SJohnny Huang }
1899696656c6SJohnny Huang 
1900de6b0cc4SJohnny Huang static int do_otp_prog(int addr, int nconfirm)
190169d5fd8fSJohnny Huang {
190269d5fd8fSJohnny Huang 	int ret;
19039a4fe690SJohnny Huang 	int image_version = 0;
1904696656c6SJohnny Huang 	struct otp_header *otp_header;
1905696656c6SJohnny Huang 	struct otp_image_layout image_layout;
1906696656c6SJohnny Huang 	int image_size;
1907696656c6SJohnny Huang 	uint8_t *buf;
1908696656c6SJohnny Huang 	uint8_t *checksum;
190969d5fd8fSJohnny Huang 
1910696656c6SJohnny Huang 	otp_header = map_physmem(addr, sizeof(struct otp_header), MAP_WRBACK);
1911696656c6SJohnny Huang 	if (!otp_header) {
191269d5fd8fSJohnny Huang 		puts("Failed to map physical memory\n");
19132a856b9aSJohnny Huang 		return OTP_FAILURE;
191469d5fd8fSJohnny Huang 	}
1915d90825e2SJohnny Huang 
1916696656c6SJohnny Huang 	image_size = OTP_IMAGE_SIZE(otp_header->image_info);
1917696656c6SJohnny Huang 	unmap_physmem(otp_header, MAP_WRBACK);
1918696656c6SJohnny Huang 
1919696656c6SJohnny Huang 	buf = map_physmem(addr, image_size + CHECKSUM_LEN, MAP_WRBACK);
1920696656c6SJohnny Huang 
1921696656c6SJohnny Huang 	if (!buf) {
1922696656c6SJohnny Huang 		puts("Failed to map physical memory\n");
1923696656c6SJohnny Huang 		return OTP_FAILURE;
1924696656c6SJohnny Huang 	}
1925696656c6SJohnny Huang 	otp_header = (struct otp_header *) buf;
1926696656c6SJohnny Huang 	checksum = buf + otp_header->checksum_offset;
1927696656c6SJohnny Huang 
1928696656c6SJohnny Huang 	if (strcmp(OTP_MAGIC, (char *)otp_header->otp_magic) != 0) {
1929696656c6SJohnny Huang 		puts("Image is invalid\n");
1930696656c6SJohnny Huang 		return OTP_FAILURE;
1931696656c6SJohnny Huang 	}
1932696656c6SJohnny Huang 
1933696656c6SJohnny Huang 
19345010032bSJohnny Huang 	image_layout.data_length = (int)(OTP_REGION_SIZE(otp_header->data_info) / 2);
19355010032bSJohnny Huang 	image_layout.data = buf + OTP_REGION_OFFSET(otp_header->data_info);
19365010032bSJohnny Huang 	image_layout.data_ignore = image_layout.data + image_layout.data_length;
19375010032bSJohnny Huang 
19385010032bSJohnny Huang 	image_layout.conf_length = (int)(OTP_REGION_SIZE(otp_header->config_info) / 2);
1939696656c6SJohnny Huang 	image_layout.conf = buf + OTP_REGION_OFFSET(otp_header->config_info);
19405010032bSJohnny Huang 	image_layout.conf_ignore = image_layout.conf + image_layout.conf_length;
1941696656c6SJohnny Huang 
1942696656c6SJohnny Huang 	image_layout.strap = buf + OTP_REGION_OFFSET(otp_header->strap_info);
1943696656c6SJohnny Huang 
1944696656c6SJohnny Huang 	if (!strcmp("A0", (char *)otp_header->otp_version)) {
1945696656c6SJohnny Huang 		image_version = OTP_AST2600A0;
19465010032bSJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 3);
19475010032bSJohnny Huang 		image_layout.strap_pro = image_layout.strap + image_layout.strap_length;
19485010032bSJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 2 * image_layout.strap_length;
1949696656c6SJohnny Huang 	} else if (!strcmp("A1", (char *)otp_header->otp_version)) {
1950696656c6SJohnny Huang 		image_version = OTP_AST2600A1;
19515010032bSJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4);
19525010032bSJohnny Huang 		image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length;
19535010032bSJohnny Huang 		image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length;
19545010032bSJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length;
1955696656c6SJohnny Huang 	} else {
1956696656c6SJohnny Huang 		puts("Version is not supported\n");
1957696656c6SJohnny Huang 		return OTP_FAILURE;
1958696656c6SJohnny Huang 	}
1959696656c6SJohnny Huang 
19609a4fe690SJohnny Huang 	if (image_version != info_cb.version) {
19619a4fe690SJohnny Huang 		puts("Version is not match\n");
19629a4fe690SJohnny Huang 		return OTP_FAILURE;
19639a4fe690SJohnny Huang 	}
19649a4fe690SJohnny Huang 
1965696656c6SJohnny Huang 	if (otp_image_verify(buf, image_size, checksum)) {
1966696656c6SJohnny Huang 		puts("checksum is invalid\n");
1967696656c6SJohnny Huang 		return OTP_FAILURE;
1968d90825e2SJohnny Huang 	}
19697332532cSJohnny Huang 
197069d5fd8fSJohnny Huang 	if (!nconfirm) {
1971696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_DATA) {
19727f795e57SJohnny Huang 			printf("\nOTP data region :\n");
1973696656c6SJohnny Huang 			if (otp_print_data_info(&image_layout) < 0) {
197469d5fd8fSJohnny Huang 				printf("OTP data error, please check.\n");
19752a856b9aSJohnny Huang 				return OTP_FAILURE;
197669d5fd8fSJohnny Huang 			}
197769d5fd8fSJohnny Huang 		}
1978696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_STRAP) {
19797332532cSJohnny Huang 			printf("\nOTP strap region :\n");
19805010032bSJohnny Huang 			if (otp_print_strap_image(&image_layout) < 0) {
19817332532cSJohnny Huang 				printf("OTP strap error, please check.\n");
19827332532cSJohnny Huang 				return OTP_FAILURE;
19837332532cSJohnny Huang 			}
19847332532cSJohnny Huang 		}
1985696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_CONFIG) {
19867332532cSJohnny Huang 			printf("\nOTP configuration region :\n");
1987696656c6SJohnny Huang 			if (otp_print_conf_image(&image_layout) < 0) {
19887332532cSJohnny Huang 				printf("OTP config error, please check.\n");
19897332532cSJohnny Huang 				return OTP_FAILURE;
19907332532cSJohnny Huang 			}
19917332532cSJohnny Huang 		}
19927332532cSJohnny Huang 
199369d5fd8fSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
199469d5fd8fSJohnny Huang 		if (!confirm_yesno()) {
199569d5fd8fSJohnny Huang 			printf(" Aborting\n");
19962a856b9aSJohnny Huang 			return OTP_FAILURE;
199769d5fd8fSJohnny Huang 		}
199869d5fd8fSJohnny Huang 	}
19997332532cSJohnny Huang 
20005010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_DATA) {
20015010032bSJohnny Huang 		printf("programing data region ...\n");
20025010032bSJohnny Huang 		ret = otp_prog_data(&image_layout);
20035010032bSJohnny Huang 		if (ret != 0) {
20045010032bSJohnny Huang 			printf("Error\n");
20055010032bSJohnny Huang 			return ret;
20065010032bSJohnny Huang 		} else {
20075010032bSJohnny Huang 			printf("Done\n");
20085010032bSJohnny Huang 		}
20095010032bSJohnny Huang 	}
20105010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_STRAP) {
20115010032bSJohnny Huang 		printf("programing strap region ...\n");
20125010032bSJohnny Huang 		ret = otp_prog_strap(&image_layout);
20135010032bSJohnny Huang 		if (ret != 0) {
20145010032bSJohnny Huang 			printf("Error\n");
20155010032bSJohnny Huang 			return ret;
20165010032bSJohnny Huang 		} else {
20175010032bSJohnny Huang 			printf("Done\n");
20185010032bSJohnny Huang 		}
20195010032bSJohnny Huang 	}
20205010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_CONFIG) {
20215010032bSJohnny Huang 		printf("programing configuration region ...\n");
20225010032bSJohnny Huang 		ret = otp_prog_conf(&image_layout);
20235010032bSJohnny Huang 		if (ret != 0) {
20245010032bSJohnny Huang 			printf("Error\n");
20255010032bSJohnny Huang 			return ret;
20265010032bSJohnny Huang 		}
20275010032bSJohnny Huang 		printf("Done\n");
20285010032bSJohnny Huang 	}
2029cd1610b4SJohnny Huang 
20307332532cSJohnny Huang 	return OTP_SUCCESS;
20312a856b9aSJohnny Huang }
20322a856b9aSJohnny Huang 
20332a856b9aSJohnny Huang static int do_otp_prog_bit(int mode, int otp_dw_offset, int bit_offset, int value, int nconfirm)
2034cd1610b4SJohnny Huang {
2035a6af4a17SJohnny Huang 	uint32_t read[2];
2036d90825e2SJohnny Huang 	uint32_t prog_address = 0;
203766f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
2038cd1610b4SJohnny Huang 	int otp_bit;
2039*83655e91SJohnny Huang 	int ret = 0;
2040cd1610b4SJohnny Huang 
2041cd1610b4SJohnny Huang 	switch (mode) {
2042a6d0d645SJohnny Huang 	case OTP_REGION_CONF:
2043a6af4a17SJohnny Huang 		otp_read_config(otp_dw_offset, read);
2044cd1610b4SJohnny Huang 		prog_address = 0x800;
2045cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset / 8) * 0x200;
2046cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset % 8) * 0x2;
2047a6af4a17SJohnny Huang 		otp_bit = (read[0] >> bit_offset) & 0x1;
2048cd1610b4SJohnny Huang 		if (otp_bit == value) {
2049a6af4a17SJohnny Huang 			printf("OTPCFG%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
2050cd1610b4SJohnny Huang 			printf("No need to program\n");
20512a856b9aSJohnny Huang 			return OTP_SUCCESS;
2052cd1610b4SJohnny Huang 		}
2053cd1610b4SJohnny Huang 		if (otp_bit == 1 && value == 0) {
2054a6af4a17SJohnny Huang 			printf("OTPCFG%X[%X] = 1\n", otp_dw_offset, bit_offset);
2055cd1610b4SJohnny Huang 			printf("OTP is programed, which can't be clean\n");
20562a856b9aSJohnny Huang 			return OTP_FAILURE;
2057cd1610b4SJohnny Huang 		}
2058a6af4a17SJohnny Huang 		printf("Program OTPCFG%X[%X] to 1\n", otp_dw_offset, bit_offset);
2059cd1610b4SJohnny Huang 		break;
2060a6d0d645SJohnny Huang 	case OTP_REGION_DATA:
2061cd1610b4SJohnny Huang 		prog_address = otp_dw_offset;
2062cd1610b4SJohnny Huang 
2063cd1610b4SJohnny Huang 		if (otp_dw_offset % 2 == 0) {
2064a6af4a17SJohnny Huang 			otp_read_data(otp_dw_offset, read);
2065a6af4a17SJohnny Huang 			otp_bit = (read[0] >> bit_offset) & 0x1;
2066643b9cfdSJohnny Huang 
2067643b9cfdSJohnny Huang 			if (otp_bit == 1 && value == 0) {
2068643b9cfdSJohnny Huang 				printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
2069643b9cfdSJohnny Huang 				printf("OTP is programed, which can't be cleaned\n");
2070643b9cfdSJohnny Huang 				return OTP_FAILURE;
2071643b9cfdSJohnny Huang 			}
2072cd1610b4SJohnny Huang 		} else {
2073a6af4a17SJohnny Huang 			otp_read_data(otp_dw_offset - 1, read);
2074a6af4a17SJohnny Huang 			otp_bit = (read[1] >> bit_offset) & 0x1;
2075643b9cfdSJohnny Huang 
2076643b9cfdSJohnny Huang 			if (otp_bit == 0 && value == 1) {
2077643b9cfdSJohnny Huang 				printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
2078643b9cfdSJohnny Huang 				printf("OTP is programed, which can't be writen\n");
2079643b9cfdSJohnny Huang 				return OTP_FAILURE;
2080643b9cfdSJohnny Huang 			}
2081cd1610b4SJohnny Huang 		}
2082cd1610b4SJohnny Huang 		if (otp_bit == value) {
2083a6af4a17SJohnny Huang 			printf("OTPDATA%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
2084cd1610b4SJohnny Huang 			printf("No need to program\n");
20852a856b9aSJohnny Huang 			return OTP_SUCCESS;
2086cd1610b4SJohnny Huang 		}
2087643b9cfdSJohnny Huang 
2088a6af4a17SJohnny Huang 		printf("Program OTPDATA%X[%X] to 1\n", otp_dw_offset, bit_offset);
2089cd1610b4SJohnny Huang 		break;
2090a6d0d645SJohnny Huang 	case OTP_REGION_STRAP:
20918848d5dcSJohnny Huang 		otp_strap_status(otpstrap);
20928848d5dcSJohnny Huang 		otp_print_strap(bit_offset, 1);
20938848d5dcSJohnny Huang 		ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0, 0);
20948848d5dcSJohnny Huang 		if (ret == OTP_FAILURE)
20958848d5dcSJohnny Huang 			return OTP_FAILURE;
20968848d5dcSJohnny Huang 		else if (ret == OTP_PROG_SKIP)
20978848d5dcSJohnny Huang 			return OTP_SUCCESS;
2098a6af4a17SJohnny Huang 
2099cd1610b4SJohnny Huang 		break;
2100cd1610b4SJohnny Huang 	}
2101cd1610b4SJohnny Huang 
2102cd1610b4SJohnny Huang 	if (!nconfirm) {
2103cd1610b4SJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
2104cd1610b4SJohnny Huang 		if (!confirm_yesno()) {
2105cd1610b4SJohnny Huang 			printf(" Aborting\n");
21062a856b9aSJohnny Huang 			return OTP_FAILURE;
2107cd1610b4SJohnny Huang 		}
2108cd1610b4SJohnny Huang 	}
2109cd1610b4SJohnny Huang 
2110cd1610b4SJohnny Huang 	switch (mode) {
2111a6d0d645SJohnny Huang 	case OTP_REGION_STRAP:
2112*83655e91SJohnny Huang 		ret =  otp_prog_strap_bit(bit_offset, value);
2113*83655e91SJohnny Huang 		break;
2114a6d0d645SJohnny Huang 	case OTP_REGION_CONF:
2115a6d0d645SJohnny Huang 	case OTP_REGION_DATA:
2116*83655e91SJohnny Huang 		ret = otp_prog_bit(value, prog_address, bit_offset);
2117de6fbf1cSJohnny Huang 		break;
2118de6fbf1cSJohnny Huang 	}
2119de6fbf1cSJohnny Huang 	otp_soak(0);
2120*83655e91SJohnny Huang 	if (ret) {
21219009c25dSJohnny Huang 		printf("SUCCESS\n");
21222a856b9aSJohnny Huang 		return OTP_SUCCESS;
21239009c25dSJohnny Huang 	} else {
21249009c25dSJohnny Huang 		printf("OTP cannot be programed\n");
21259009c25dSJohnny Huang 		printf("FAILED\n");
21269009c25dSJohnny Huang 		return OTP_FAILURE;
21279009c25dSJohnny Huang 	}
2128cd1610b4SJohnny Huang 
21292a856b9aSJohnny Huang 	return OTP_USAGE;
2130cd1610b4SJohnny Huang }
2131cd1610b4SJohnny Huang 
21322a856b9aSJohnny Huang static int do_otpread(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
213369d5fd8fSJohnny Huang {
21342a856b9aSJohnny Huang 	uint32_t offset, count;
21352a856b9aSJohnny Huang 	int ret;
213669d5fd8fSJohnny Huang 
21372a856b9aSJohnny Huang 	if (argc == 4) {
21382a856b9aSJohnny Huang 		offset = simple_strtoul(argv[2], NULL, 16);
21392a856b9aSJohnny Huang 		count = simple_strtoul(argv[3], NULL, 16);
21402a856b9aSJohnny Huang 	} else if (argc == 3) {
21412a856b9aSJohnny Huang 		offset = simple_strtoul(argv[2], NULL, 16);
21422a856b9aSJohnny Huang 		count = 1;
21432a856b9aSJohnny Huang 	} else {
214469d5fd8fSJohnny Huang 		return CMD_RET_USAGE;
214569d5fd8fSJohnny Huang 	}
214669d5fd8fSJohnny Huang 
214769d5fd8fSJohnny Huang 
21482a856b9aSJohnny Huang 	if (!strcmp(argv[1], "conf")) {
21493d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
21502a856b9aSJohnny Huang 		ret = otp_print_config(offset, count);
21512a856b9aSJohnny Huang 	} else if (!strcmp(argv[1], "data")) {
21523d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
21532a856b9aSJohnny Huang 		ret = otp_print_data(offset, count);
21542a856b9aSJohnny Huang 	} else if (!strcmp(argv[1], "strap")) {
21553d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
21562a856b9aSJohnny Huang 		ret = otp_print_strap(offset, count);
21572a856b9aSJohnny Huang 	} else {
21582a856b9aSJohnny Huang 		return CMD_RET_USAGE;
215969d5fd8fSJohnny Huang 	}
216069d5fd8fSJohnny Huang 
21612a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
21622a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
21632a856b9aSJohnny Huang 	else
21642a856b9aSJohnny Huang 		return CMD_RET_USAGE;
21652a856b9aSJohnny Huang 
21662a856b9aSJohnny Huang }
21672a856b9aSJohnny Huang 
21682a856b9aSJohnny Huang static int do_otpprog(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
21692a856b9aSJohnny Huang {
21702a856b9aSJohnny Huang 	phys_addr_t addr;
21712a856b9aSJohnny Huang 	int ret;
21722a856b9aSJohnny Huang 
2173de6b0cc4SJohnny Huang 	if (argc == 3) {
2174ed071a2bSJohnny Huang 		if (strcmp(argv[1], "o"))
21752a856b9aSJohnny Huang 			return CMD_RET_USAGE;
21762a856b9aSJohnny Huang 		addr = simple_strtoul(argv[2], NULL, 16);
21773d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2178de6b0cc4SJohnny Huang 		ret = do_otp_prog(addr, 1);
2179de6b0cc4SJohnny Huang 	} else if (argc == 2) {
21802a856b9aSJohnny Huang 		addr = simple_strtoul(argv[1], NULL, 16);
21813d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2182de6b0cc4SJohnny Huang 		ret = do_otp_prog(addr, 0);
21832a856b9aSJohnny Huang 	} else {
21842a856b9aSJohnny Huang 		return CMD_RET_USAGE;
21852a856b9aSJohnny Huang 	}
21862a856b9aSJohnny Huang 
21872a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
21882a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
21892a856b9aSJohnny Huang 	else if (ret == OTP_FAILURE)
21902a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
21912a856b9aSJohnny Huang 	else
21922a856b9aSJohnny Huang 		return CMD_RET_USAGE;
21932a856b9aSJohnny Huang }
21942a856b9aSJohnny Huang 
21952a856b9aSJohnny Huang static int do_otppb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
21962a856b9aSJohnny Huang {
21972a856b9aSJohnny Huang 	int mode = 0;
21982a856b9aSJohnny Huang 	int nconfirm = 0;
21992a856b9aSJohnny Huang 	int otp_addr = 0;
22002a856b9aSJohnny Huang 	int bit_offset;
22012a856b9aSJohnny Huang 	int value;
22022a856b9aSJohnny Huang 	int ret;
22032a856b9aSJohnny Huang 
22042a856b9aSJohnny Huang 	if (argc != 4 && argc != 5 && argc != 6)
22052a856b9aSJohnny Huang 		return CMD_RET_USAGE;
22062a856b9aSJohnny Huang 
22072a856b9aSJohnny Huang 	/* Drop the pb cmd */
22082a856b9aSJohnny Huang 	argc--;
22092a856b9aSJohnny Huang 	argv++;
22102a856b9aSJohnny Huang 
22112a856b9aSJohnny Huang 	if (!strcmp(argv[0], "conf"))
2212a6d0d645SJohnny Huang 		mode = OTP_REGION_CONF;
22132a856b9aSJohnny Huang 	else if (!strcmp(argv[0], "strap"))
2214a6d0d645SJohnny Huang 		mode = OTP_REGION_STRAP;
22152a856b9aSJohnny Huang 	else if (!strcmp(argv[0], "data"))
2216a6d0d645SJohnny Huang 		mode = OTP_REGION_DATA;
2217cd1610b4SJohnny Huang 	else
22182a856b9aSJohnny Huang 		return CMD_RET_USAGE;
22192a856b9aSJohnny Huang 
22202a856b9aSJohnny Huang 	/* Drop the region cmd */
22212a856b9aSJohnny Huang 	argc--;
22222a856b9aSJohnny Huang 	argv++;
22232a856b9aSJohnny Huang 
2224ed071a2bSJohnny Huang 	if (!strcmp(argv[0], "o")) {
2225cd1610b4SJohnny Huang 		nconfirm = 1;
22262a856b9aSJohnny Huang 		/* Drop the force option */
22272a856b9aSJohnny Huang 		argc--;
22282a856b9aSJohnny Huang 		argv++;
22292a856b9aSJohnny Huang 	}
2230cd1610b4SJohnny Huang 
2231a6d0d645SJohnny Huang 	if (mode == OTP_REGION_STRAP) {
22322a856b9aSJohnny Huang 		bit_offset = simple_strtoul(argv[0], NULL, 16);
22332a856b9aSJohnny Huang 		value = simple_strtoul(argv[1], NULL, 16);
22340808cc55SJohnny Huang 		if (bit_offset >= 64 || (value != 0 && value != 1))
22352a856b9aSJohnny Huang 			return CMD_RET_USAGE;
2236cd1610b4SJohnny Huang 	} else {
22372a856b9aSJohnny Huang 		otp_addr = simple_strtoul(argv[0], NULL, 16);
22382a856b9aSJohnny Huang 		bit_offset = simple_strtoul(argv[1], NULL, 16);
22392a856b9aSJohnny Huang 		value = simple_strtoul(argv[2], NULL, 16);
22400808cc55SJohnny Huang 		if (bit_offset >= 32 || (value != 0 && value != 1))
22412a856b9aSJohnny Huang 			return CMD_RET_USAGE;
22420808cc55SJohnny Huang 		if (mode == OTP_REGION_DATA) {
224378855207SJohnny Huang 			if (otp_addr >= 0x800)
22440808cc55SJohnny Huang 				return CMD_RET_USAGE;
22450808cc55SJohnny Huang 		} else {
224678855207SJohnny Huang 			if (otp_addr >= 0x20)
22470808cc55SJohnny Huang 				return CMD_RET_USAGE;
22480808cc55SJohnny Huang 		}
2249cd1610b4SJohnny Huang 	}
2250cd1610b4SJohnny Huang 	if (value != 0 && value != 1)
22512a856b9aSJohnny Huang 		return CMD_RET_USAGE;
2252cd1610b4SJohnny Huang 
22533d3688adSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
22542a856b9aSJohnny Huang 	ret = do_otp_prog_bit(mode, otp_addr, bit_offset, value, nconfirm);
22552a856b9aSJohnny Huang 
22562a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
22572a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
22582a856b9aSJohnny Huang 	else if (ret == OTP_FAILURE)
22592a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
22602a856b9aSJohnny Huang 	else
22612a856b9aSJohnny Huang 		return CMD_RET_USAGE;
22622a856b9aSJohnny Huang }
22632a856b9aSJohnny Huang 
22642a856b9aSJohnny Huang static int do_otpcmp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
22652a856b9aSJohnny Huang {
22662a856b9aSJohnny Huang 	phys_addr_t addr;
22672a856b9aSJohnny Huang 	int otp_addr = 0;
22682a856b9aSJohnny Huang 
22692a856b9aSJohnny Huang 	if (argc != 3)
22702a856b9aSJohnny Huang 		return CMD_RET_USAGE;
22712a856b9aSJohnny Huang 
22723d3688adSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
22732a856b9aSJohnny Huang 	addr = simple_strtoul(argv[1], NULL, 16);
22742a856b9aSJohnny Huang 	otp_addr = simple_strtoul(argv[2], NULL, 16);
22752a856b9aSJohnny Huang 	if (otp_compare(otp_addr, addr) == 0) {
227669d5fd8fSJohnny Huang 		printf("Compare pass\n");
22772a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
227869d5fd8fSJohnny Huang 	} else {
227969d5fd8fSJohnny Huang 		printf("Compare fail\n");
22802a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
228169d5fd8fSJohnny Huang 	}
228269d5fd8fSJohnny Huang }
228369d5fd8fSJohnny Huang 
228466f2f8e5SJohnny Huang static int do_otpinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
228566f2f8e5SJohnny Huang {
2286a8bd6d8cSJohnny Huang 	int view = 0;
22872d4b0742SJohnny Huang 	int input;
2288a8bd6d8cSJohnny Huang 
2289a8bd6d8cSJohnny Huang 	if (argc != 2 && argc != 3)
229066f2f8e5SJohnny Huang 		return CMD_RET_USAGE;
229166f2f8e5SJohnny Huang 
22922d4b0742SJohnny Huang 	if (!strcmp(argv[1], "conf")) {
229366f2f8e5SJohnny Huang 
22943d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
22952d4b0742SJohnny Huang 		if (argc == 3) {
22962d4b0742SJohnny Huang 			input = simple_strtoul(argv[2], NULL, 16);
22972d4b0742SJohnny Huang 			otp_print_conf_info(input);
22982d4b0742SJohnny Huang 		} else {
22992d4b0742SJohnny Huang 			otp_print_conf_info(-1);
23002d4b0742SJohnny Huang 		}
23012d4b0742SJohnny Huang 	} else if (!strcmp(argv[1], "strap")) {
23022d4b0742SJohnny Huang 		if (!strcmp(argv[2], "v")) {
2303a8bd6d8cSJohnny Huang 			view = 1;
2304a8bd6d8cSJohnny Huang 			/* Drop the view option */
2305a8bd6d8cSJohnny Huang 			argc--;
2306a8bd6d8cSJohnny Huang 			argv++;
2307a8bd6d8cSJohnny Huang 		}
23083d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2309b458cd62SJohnny Huang 		otp_print_strap_info(view);
231066f2f8e5SJohnny Huang 	} else {
231166f2f8e5SJohnny Huang 		return CMD_RET_USAGE;
231266f2f8e5SJohnny Huang 	}
23132d4b0742SJohnny Huang 
231466f2f8e5SJohnny Huang 	return CMD_RET_SUCCESS;
231566f2f8e5SJohnny Huang }
231666f2f8e5SJohnny Huang 
2317737ed20bSJohnny Huang static int do_otpprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2318737ed20bSJohnny Huang {
2319737ed20bSJohnny Huang 	int input;
2320737ed20bSJohnny Huang 	int bit_offset;
2321737ed20bSJohnny Huang 	int prog_address;
2322*83655e91SJohnny Huang 	int ret;
2323737ed20bSJohnny Huang 	if (argc != 3 && argc != 2)
2324737ed20bSJohnny Huang 		return CMD_RET_USAGE;
2325737ed20bSJohnny Huang 
2326ed071a2bSJohnny Huang 	if (!strcmp(argv[0], "o")) {
2327737ed20bSJohnny Huang 		input = simple_strtoul(argv[2], NULL, 16);
2328737ed20bSJohnny Huang 	} else {
2329737ed20bSJohnny Huang 		input = simple_strtoul(argv[1], NULL, 16);
2330737ed20bSJohnny Huang 		printf("OTPSTRAP[%d] will be protected\n", input);
2331737ed20bSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
2332737ed20bSJohnny Huang 		if (!confirm_yesno()) {
2333737ed20bSJohnny Huang 			printf(" Aborting\n");
2334737ed20bSJohnny Huang 			return CMD_RET_FAILURE;
2335737ed20bSJohnny Huang 		}
2336737ed20bSJohnny Huang 	}
2337737ed20bSJohnny Huang 
2338737ed20bSJohnny Huang 	prog_address = 0x800;
2339737ed20bSJohnny Huang 	if (input < 32) {
2340737ed20bSJohnny Huang 		bit_offset = input;
2341737ed20bSJohnny Huang 		prog_address |= 0x60c;
2342737ed20bSJohnny Huang 	} else if (input < 64) {
2343737ed20bSJohnny Huang 		bit_offset = input - 32;
2344737ed20bSJohnny Huang 		prog_address |= 0x60e;
2345737ed20bSJohnny Huang 	} else {
2346737ed20bSJohnny Huang 		return CMD_RET_USAGE;
2347737ed20bSJohnny Huang 	}
2348737ed20bSJohnny Huang 
2349737ed20bSJohnny Huang 	if (verify_bit(prog_address, bit_offset, 1) == 0) {
2350737ed20bSJohnny Huang 		printf("OTPSTRAP[%d] already protected\n", input);
2351737ed20bSJohnny Huang 	}
2352de6fbf1cSJohnny Huang 
2353*83655e91SJohnny Huang 	ret = otp_prog_bit(1, prog_address, bit_offset);
2354de6fbf1cSJohnny Huang 	otp_soak(0);
2355*83655e91SJohnny Huang 
2356*83655e91SJohnny Huang 	if (ret) {
2357737ed20bSJohnny Huang 		printf("OTPSTRAP[%d] is protected\n", input);
2358737ed20bSJohnny Huang 		return CMD_RET_SUCCESS;
2359737ed20bSJohnny Huang 	}
2360737ed20bSJohnny Huang 
2361737ed20bSJohnny Huang 	printf("Protect OTPSTRAP[%d] fail\n", input);
2362737ed20bSJohnny Huang 	return CMD_RET_FAILURE;
2363737ed20bSJohnny Huang 
2364737ed20bSJohnny Huang }
23659a4fe690SJohnny Huang 
23662a856b9aSJohnny Huang static cmd_tbl_t cmd_otp[] = {
23672a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(read, 4, 0, do_otpread, "", ""),
2368a8bd6d8cSJohnny Huang 	U_BOOT_CMD_MKENT(info, 3, 0, do_otpinfo, "", ""),
2369de6b0cc4SJohnny Huang 	U_BOOT_CMD_MKENT(prog, 3, 0, do_otpprog, "", ""),
23702a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(pb, 6, 0, do_otppb, "", ""),
2371737ed20bSJohnny Huang 	U_BOOT_CMD_MKENT(protect, 3, 0, do_otpprotect, "", ""),
23722a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(cmp, 3, 0, do_otpcmp, "", ""),
23732a856b9aSJohnny Huang };
23742a856b9aSJohnny Huang 
23752a856b9aSJohnny Huang static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
23762a856b9aSJohnny Huang {
23772a856b9aSJohnny Huang 	cmd_tbl_t *cp;
23782a856b9aSJohnny Huang 
23792a856b9aSJohnny Huang 	cp = find_cmd_tbl(argv[1], cmd_otp, ARRAY_SIZE(cmd_otp));
23802a856b9aSJohnny Huang 
2381737ed20bSJohnny Huang 	/* Drop the otp command */
23822a856b9aSJohnny Huang 	argc--;
23832a856b9aSJohnny Huang 	argv++;
23842a856b9aSJohnny Huang 
23852a856b9aSJohnny Huang 	if (cp == NULL || argc > cp->maxargs)
23862a856b9aSJohnny Huang 		return CMD_RET_USAGE;
23872a856b9aSJohnny Huang 	if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
23882a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
23892a856b9aSJohnny Huang 
2390696656c6SJohnny Huang 	if (chip_version() == OTP_AST2600A0) {
2391696656c6SJohnny Huang 		info_cb.version = OTP_AST2600A0;
23929a4fe690SJohnny Huang 		info_cb.conf_info = a0_conf_info;
23939a4fe690SJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a0_conf_info);
23949a4fe690SJohnny Huang 		info_cb.strap_info = a0_strap_info;
23959a4fe690SJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a0_strap_info);
23969a4fe690SJohnny Huang 		info_cb.key_info = a0_key_type;
23979a4fe690SJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a0_key_type);
2398696656c6SJohnny Huang 	} else if (chip_version() == OTP_AST2600A1) {
2399696656c6SJohnny Huang 		info_cb.version = OTP_AST2600A1;
24003cb28812SJohnny Huang 		info_cb.conf_info = a1_conf_info;
24013cb28812SJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a1_conf_info);
24023cb28812SJohnny Huang 		info_cb.strap_info = a1_strap_info;
24033cb28812SJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a1_strap_info);
24049a4fe690SJohnny Huang 		info_cb.key_info = a1_key_type;
24059a4fe690SJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a1_key_type);
24069a4fe690SJohnny Huang 	}
24079a4fe690SJohnny Huang 
24082a856b9aSJohnny Huang 	return cp->cmd(cmdtp, flag, argc, argv);
240969d5fd8fSJohnny Huang }
241069d5fd8fSJohnny Huang 
241169d5fd8fSJohnny Huang U_BOOT_CMD(
241269d5fd8fSJohnny Huang 	otp, 7, 0,  do_ast_otp,
241369d5fd8fSJohnny Huang 	"ASPEED One-Time-Programmable sub-system",
24142a856b9aSJohnny Huang 	"read conf|data <otp_dw_offset> <dw_count>\n"
24152a856b9aSJohnny Huang 	"otp read strap <strap_bit_offset> <bit_count>\n"
24162d4b0742SJohnny Huang 	"otp info strap [v]\n"
24172d4b0742SJohnny Huang 	"otp info conf [otp_dw_offset]\n"
2418de6b0cc4SJohnny Huang 	"otp prog [o] <addr>\n"
2419ed071a2bSJohnny Huang 	"otp pb conf|data [o] <otp_dw_offset> <bit_offset> <value>\n"
2420ed071a2bSJohnny Huang 	"otp pb strap [o] <bit_offset> <value>\n"
2421ed071a2bSJohnny Huang 	"otp protect [o] <bit_offset>\n"
24222a856b9aSJohnny Huang 	"otp cmp <addr> <otp_dw_offset>\n"
242369d5fd8fSJohnny Huang );
2424