xref: /openbmc/u-boot/cmd/otp.c (revision e1a7245e442b73a8d1b926db9c3f8d0bf309b4de)
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" },
280*e1a7245eSJohnny Huang 	{ 7, 3, 0, "CPU Frequency : 1.2GHz" },
281*e1a7245eSJohnny Huang 	{ 7, 3, 1, "CPU Frequency : 1.6MHz" },
2823cb28812SJohnny Huang 	{ 7, 3, 2, "CPU Frequency : 1.2GHz" },
283*e1a7245eSJohnny Huang 	{ 7, 3, 3, "CPU Frequency : 1.6GHz" },
284*e1a7245eSJohnny Huang 	{ 7, 3, 4, "CPU Frequency : 800MHz" },
285*e1a7245eSJohnny Huang 	{ 7, 3, 5, "CPU Frequency : 800MHz" },
286*e1a7245eSJohnny Huang 	{ 7, 3, 6, "CPU Frequency : 800MHz" },
287*e1a7245eSJohnny Huang 	{ 7, 3, 7, "CPU Frequency : 800MHz" },
2883cb28812SJohnny Huang 	{ 10, 2, 0, "HCLK ratio AXI:AHB = 2:1" },
2893cb28812SJohnny Huang 	{ 10, 2, 1, "HCLK ratio AXI:AHB = 2:1" },
2903cb28812SJohnny Huang 	{ 10, 2, 2, "HCLK ratio AXI:AHB = 3:1" },
2913cb28812SJohnny Huang 	{ 10, 2, 3, "HCLK ratio AXI:AHB = 4:1" },
2923cb28812SJohnny Huang 	{ 12, 2, 0, "VGA memory size : 8MB" },
2933cb28812SJohnny Huang 	{ 12, 2, 1, "VGA memory size : 16MB" },
2943cb28812SJohnny Huang 	{ 12, 2, 2, "VGA memory size : 32MB" },
2953cb28812SJohnny Huang 	{ 12, 2, 3, "VGA memory size : 64MB" },
2963cb28812SJohnny Huang 	{ 14, 3, OTP_REG_RESERVED, "" },
2973cb28812SJohnny Huang 	{ 17, 1, 0, "VGA class code : Class Code for video device" },
2983cb28812SJohnny Huang 	{ 17, 1, 1, "VGA class code : Class Code for VGA device" },
2993cb28812SJohnny Huang 	{ 18, 1, 0, "Enable debug interfaces 0" },
3003cb28812SJohnny Huang 	{ 18, 1, 1, "Disable debug interfaces 0" },
3013cb28812SJohnny Huang 	{ 19, 1, 0, "Boot from emmc mode : High eMMC speed" },
3023cb28812SJohnny Huang 	{ 19, 1, 1, "Boot from emmc mode : Normal eMMC speed" },
3033cb28812SJohnny Huang 	{ 20, 1, 0, "Disable Pcie EHCI device" },
3043cb28812SJohnny Huang 	{ 20, 1, 1, "Enable Pcie EHCI device" },
3053cb28812SJohnny Huang 	{ 21, 1, 0, "Enable VGA XDMA function" },
3063cb28812SJohnny Huang 	{ 21, 1, 1, "Disable VGA XDMA function" },
3073cb28812SJohnny Huang 	{ 22, 1, 0, "Normal BMC mode" },
3083cb28812SJohnny Huang 	{ 22, 1, 1, "Disable dedicated BMC functions for non-BMC application" },
3093cb28812SJohnny Huang 	{ 23, 1, 0, "SSPRST# pin is for secondary processor dedicated reset pin" },
3103cb28812SJohnny Huang 	{ 23, 1, 1, "SSPRST# pin is for PCIE root complex dedicated reset pin" },
3113cb28812SJohnny Huang 	{ 24, 1, 0, "Enable watchdog to reset full chip" },
3123cb28812SJohnny Huang 	{ 24, 1, 1, "Disable watchdog to reset full chip" },
3133cb28812SJohnny Huang 	{ 25, 5, OTP_REG_RESERVED, "" },
3143cb28812SJohnny Huang 	{ 30, 2, OTP_REG_RESERVED, "" },
3153cb28812SJohnny Huang 	{ 32, 1, 0, "MAC 3 : RMII/NCSI" },
3163cb28812SJohnny Huang 	{ 32, 1, 1, "MAC 3 : RGMII" },
3173cb28812SJohnny Huang 	{ 33, 1, 0, "MAC 4 : RMII/NCSI" },
3183cb28812SJohnny Huang 	{ 33, 1, 1, "MAC 4 : RGMII" },
3193cb28812SJohnny Huang 	{ 34, 1, 0, "SuperIO configuration address : 0x2E" },
3203cb28812SJohnny Huang 	{ 34, 1, 1, "SuperIO configuration address : 0x4E" },
3213cb28812SJohnny Huang 	{ 35, 1, 0, "Enable LPC to decode SuperIO" },
3223cb28812SJohnny Huang 	{ 35, 1, 1, "Disable LPC to decode SuperIO" },
3233cb28812SJohnny Huang 	{ 36, 1, 0, "Enable debug interfaces 1" },
3243cb28812SJohnny Huang 	{ 36, 1, 1, "Disable debug interfaces 1" },
3253cb28812SJohnny Huang 	{ 37, 1, 0, "Disable ACPI function" },
3263cb28812SJohnny Huang 	{ 37, 1, 1, "Enable ACPI function" },
3273cb28812SJohnny Huang 	{ 38, 1, 0, "Enable eSPI mode" },
3283cb28812SJohnny Huang 	{ 38, 1, 1, "Enable LPC mode" },
3293cb28812SJohnny Huang 	{ 39, 1, 0, "Enable SAFS mode" },
3303cb28812SJohnny Huang 	{ 39, 1, 1, "Enable SAFS mode" },
3313cb28812SJohnny Huang 	{ 40, 2, OTP_REG_RESERVED, "" },
3323cb28812SJohnny Huang 	{ 42, 1, 0, "Disable boot SPI 3B/4B address mode auto detection" },
3333cb28812SJohnny Huang 	{ 42, 1, 1, "Enable boot SPI 3B/4B address mode auto detection" },
3343cb28812SJohnny Huang 	{ 43, 1, 0, "Disable boot SPI ABR" },
3353cb28812SJohnny Huang 	{ 43, 1, 1, "Enable boot SPI ABR" },
3363cb28812SJohnny Huang 	{ 44, 1, 0, "Boot SPI ABR mode : dual SPI flash" },
3373cb28812SJohnny Huang 	{ 44, 1, 1, "Boot SPI ABR mode : single SPI flash" },
3383cb28812SJohnny Huang 	{ 45, 3, 0, "Boot SPI flash size : no define size" },
3393cb28812SJohnny Huang 	{ 45, 3, 1, "Boot SPI flash size : 2MB" },
3403cb28812SJohnny Huang 	{ 45, 3, 2, "Boot SPI flash size : 4MB" },
3413cb28812SJohnny Huang 	{ 45, 3, 3, "Boot SPI flash size : 8MB" },
3423cb28812SJohnny Huang 	{ 45, 3, 4, "Boot SPI flash size : 16MB" },
3433cb28812SJohnny Huang 	{ 45, 3, 5, "Boot SPI flash size : 32MB" },
3443cb28812SJohnny Huang 	{ 45, 3, 6, "Boot SPI flash size : 64MB" },
3453cb28812SJohnny Huang 	{ 45, 3, 7, "Boot SPI flash size : 128MB" },
3463cb28812SJohnny Huang 	{ 48, 1, 0, "Disable host SPI ABR" },
3473cb28812SJohnny Huang 	{ 48, 1, 1, "Enable host SPI ABR" },
3483cb28812SJohnny Huang 	{ 49, 1, 0, "Disable host SPI ABR mode select pin" },
3493cb28812SJohnny Huang 	{ 49, 1, 1, "Enable host SPI ABR mode select pin" },
3503cb28812SJohnny Huang 	{ 50, 1, 0, "Host SPI ABR mode : dual SPI flash" },
3513cb28812SJohnny Huang 	{ 50, 1, 1, "Host SPI ABR mode : single SPI flash" },
3523cb28812SJohnny Huang 	{ 51, 3, 0, "Host SPI flash size : no define size" },
3533cb28812SJohnny Huang 	{ 51, 3, 1, "Host SPI flash size : 2MB" },
3543cb28812SJohnny Huang 	{ 51, 3, 2, "Host SPI flash size : 4MB" },
3553cb28812SJohnny Huang 	{ 51, 3, 3, "Host SPI flash size : 8MB" },
3563cb28812SJohnny Huang 	{ 51, 3, 4, "Host SPI flash size : 16MB" },
3573cb28812SJohnny Huang 	{ 51, 3, 5, "Host SPI flash size : 32MB" },
3583cb28812SJohnny Huang 	{ 51, 3, 6, "Host SPI flash size : 64MB" },
3593cb28812SJohnny Huang 	{ 51, 3, 7, "Host SPI flash size : 128MB" },
3603cb28812SJohnny Huang 	{ 54, 1, 0, "Disable boot SPI auxiliary control pins" },
3613cb28812SJohnny Huang 	{ 54, 1, 1, "Enable boot SPI auxiliary control pins" },
3623cb28812SJohnny Huang 	{ 55, 2, 0, "Boot SPI CRTM size : disable CRTM" },
3633cb28812SJohnny Huang 	{ 55, 2, 1, "Boot SPI CRTM size : 256KB" },
3643cb28812SJohnny Huang 	{ 55, 2, 2, "Boot SPI CRTM size : 512KB" },
3653cb28812SJohnny Huang 	{ 55, 2, 3, "Boot SPI CRTM size : 1MB" },
3663cb28812SJohnny Huang 	{ 57, 2, 0, "Host SPI CRTM size : disable CRTM" },
3673cb28812SJohnny Huang 	{ 57, 2, 1, "Host SPI CRTM size : 256KB" },
3683cb28812SJohnny Huang 	{ 57, 2, 2, "Host SPI CRTM size : 512KB" },
3693cb28812SJohnny Huang 	{ 57, 2, 3, "Host SPI CRTM size : 1MB" },
3703cb28812SJohnny Huang 	{ 59, 1, 0, "Disable host SPI auxiliary control pins" },
3713cb28812SJohnny Huang 	{ 59, 1, 1, "Enable host SPI auxiliary control pins" },
3723cb28812SJohnny Huang 	{ 60, 1, 0, "Disable GPIO pass through" },
3733cb28812SJohnny Huang 	{ 60, 1, 1, "Enable GPIO pass through" },
3743cb28812SJohnny Huang 	{ 61, 1, 0, "Enable low security secure boot key" },
3753cb28812SJohnny Huang 	{ 61, 1, 1, "Disable low security secure boot key" },
3763cb28812SJohnny Huang 	{ 62, 1, 0, "Disable dedicate GPIO strap pins" },
3773cb28812SJohnny Huang 	{ 62, 1, 1, "Enable dedicate GPIO strap pins" },
3783cb28812SJohnny Huang 	{ 63, 1, OTP_REG_RESERVED, "" }
3793cb28812SJohnny Huang };
3803cb28812SJohnny Huang 
38179e42a59SJoel Stanley static const struct otpconf_info a0_conf_info[] = {
38291448c03SJohnny Huang 	{ 0, 0,  1,  0, "Enable Secure Region programming" },
38391448c03SJohnny Huang 	{ 0, 0,  1,  1, "Disable Secure Region programming" },
38491448c03SJohnny Huang 	{ 0, 1,  1,  0, "Disable Secure Boot" },
38591448c03SJohnny Huang 	{ 0, 1,  1,  1, "Enable Secure Boot" },
38691448c03SJohnny Huang 	{ 0, 2,  1,  0, "Initialization programming not done" },
38791448c03SJohnny Huang 	{ 0, 2,  1,  1, "Initialization programming done" },
38891448c03SJohnny Huang 	{ 0, 3,  1,  0, "User region ECC disable" },
38991448c03SJohnny Huang 	{ 0, 3,  1,  1, "User region ECC enable" },
39091448c03SJohnny Huang 	{ 0, 4,  1,  0, "Secure Region ECC disable" },
39191448c03SJohnny Huang 	{ 0, 4,  1,  1, "Secure Region ECC enable" },
39291448c03SJohnny Huang 	{ 0, 5,  1,  0, "Enable low security key" },
39391448c03SJohnny Huang 	{ 0, 5,  1,  1, "Disable low security key" },
39491448c03SJohnny Huang 	{ 0, 6,  1,  0, "Do not ignore Secure Boot hardware strap" },
39591448c03SJohnny Huang 	{ 0, 6,  1,  1, "Ignore Secure Boot hardware strap" },
39691448c03SJohnny Huang 	{ 0, 7,  1,  0, "Secure Boot Mode: 1" },
39791448c03SJohnny Huang 	{ 0, 7,  1,  1, "Secure Boot Mode: 2" },
39891448c03SJohnny Huang 	{ 0, 8,  2,  0, "Single cell mode (recommended)" },
399541eb887SJohnny Huang 	{ 0, 8,  2,  1, "Differential mode" },
40091448c03SJohnny Huang 	{ 0, 8,  2,  2, "Differential-redundant mode" },
40191448c03SJohnny Huang 	{ 0, 10, 2,  0, "RSA mode : RSA1024" },
40291448c03SJohnny Huang 	{ 0, 10, 2,  1, "RSA mode : RSA2048" },
40391448c03SJohnny Huang 	{ 0, 10, 2,  2, "RSA mode : RSA3072" },
40491448c03SJohnny Huang 	{ 0, 10, 2,  3, "RSA mode : RSA4096" },
40591448c03SJohnny Huang 	{ 0, 12, 2,  0, "SHA mode : SHA224" },
40691448c03SJohnny Huang 	{ 0, 12, 2,  1, "SHA mode : SHA256" },
40791448c03SJohnny Huang 	{ 0, 12, 2,  2, "SHA mode : SHA384" },
40891448c03SJohnny Huang 	{ 0, 12, 2,  3, "SHA mode : SHA512" },
40991448c03SJohnny Huang 	{ 0, 14, 2,  OTP_REG_RESERVED, "" },
41091448c03SJohnny Huang 	{ 0, 16, 6,  OTP_REG_VALUE, "Secure Region size (DW): 0x%x" },
41191448c03SJohnny Huang 	{ 0, 22, 1,  0, "Secure Region : Writable" },
41291448c03SJohnny Huang 	{ 0, 22, 1,  1, "Secure Region : Write Protect" },
41391448c03SJohnny Huang 	{ 0, 23, 1,  0, "User Region : Writable" },
41491448c03SJohnny Huang 	{ 0, 23, 1,  1, "User Region : Write Protect" },
41591448c03SJohnny Huang 	{ 0, 24, 1,  0, "Configure Region : Writable" },
41691448c03SJohnny Huang 	{ 0, 24, 1,  1, "Configure Region : Write Protect" },
41791448c03SJohnny Huang 	{ 0, 25, 1,  0, "OTP strap Region : Writable" },
41891448c03SJohnny Huang 	{ 0, 25, 1,  1, "OTP strap Region : Write Protect" },
41991448c03SJohnny Huang 	{ 0, 26, 1,  0, "Disable Copy Boot Image to Internal SRAM" },
42091448c03SJohnny Huang 	{ 0, 26, 1,  1, "Copy Boot Image to Internal SRAM" },
42191448c03SJohnny Huang 	{ 0, 27, 1,  0, "Disable image encryption" },
42291448c03SJohnny Huang 	{ 0, 27, 1,  1, "Enable image encryption" },
42391448c03SJohnny Huang 	{ 0, 28, 1,  OTP_REG_RESERVED, "" },
42491448c03SJohnny Huang 	{ 0, 29, 1,  0, "OTP key retire Region : Writable" },
42591448c03SJohnny Huang 	{ 0, 29, 1,  1, "OTP key retire Region : Write Protect" },
4263cb28812SJohnny Huang 	{ 0, 30, 1,  0, "Data region redundancy repair disable" },
4273cb28812SJohnny Huang 	{ 0, 30, 1,  1, "Data region redundancy repair enable" },
4283cb28812SJohnny Huang 	{ 0, 31, 1,  0, "OTP memory lock disable" },
4293cb28812SJohnny Huang 	{ 0, 31, 1,  1, "OTP memory lock enable" },
4303cb28812SJohnny Huang 	{ 2, 0,  16, OTP_REG_VALUE, "Vender ID : 0x%x" },
4313cb28812SJohnny Huang 	{ 2, 16, 16, OTP_REG_VALUE, "Key Revision : 0x%x" },
4323cb28812SJohnny Huang 	{ 3, 0,  16, OTP_REG_VALUE, "Secure boot header offset : 0x%x" },
4333cb28812SJohnny Huang 	{ 4, 0,  8,  OTP_REG_VALID_BIT, "Keys valid  : %s" },
4343cb28812SJohnny Huang 	{ 4, 16, 8,  OTP_REG_VALID_BIT, "Keys retire  : %s" },
4353cb28812SJohnny Huang 	{ 5, 0,  32, OTP_REG_VALUE, "User define data, random number low : 0x%x" },
4363cb28812SJohnny Huang 	{ 6, 0,  32, OTP_REG_VALUE, "User define data, random number high : 0x%x" },
4373cb28812SJohnny Huang 	{ 7, 0,  1,  0, "Force enable PCI bus to AHB bus bridge" },
4383cb28812SJohnny Huang 	{ 7, 0,  1,  1, "Force disable PCI bus to AHB bus bridge" },
4393cb28812SJohnny Huang 	{ 7, 1,  1,  0, "Force enable UART5 debug port function" },
4403cb28812SJohnny Huang 	{ 7, 1,  1,  1, "Force disable UART5 debug port function" },
4413cb28812SJohnny Huang 	{ 7, 2,  1,  0, "Force enable XDMA function" },
4423cb28812SJohnny Huang 	{ 7, 2,  1,  1, "Force disable XDMA function" },
4433cb28812SJohnny Huang 	{ 7, 3,  1,  0, "Force enable APB to PCIE device bridge" },
4443cb28812SJohnny Huang 	{ 7, 3,  1,  1, "Force disable APB to PCIE device bridge" },
4453cb28812SJohnny Huang 	{ 7, 4,  1,  0, "Force enable APB to PCIE bridge config access" },
4463cb28812SJohnny Huang 	{ 7, 4,  1,  1, "Force disable APB to PCIE bridge config access" },
4473cb28812SJohnny Huang 	{ 7, 5,  1,  0, "Force enable PCIE bus trace buffer" },
4483cb28812SJohnny Huang 	{ 7, 5,  1,  1, "Force disable PCIE bus trace buffer" },
4493cb28812SJohnny Huang 	{ 7, 6,  1,  0, "Force enable the capability for PCIE device port as a Root Complex" },
4503cb28812SJohnny Huang 	{ 7, 6,  1,  1, "Force disable the capability for PCIE device port as a Root Complex" },
4513cb28812SJohnny Huang 	{ 7, 16, 1,  0, "Force enable ESPI bus to AHB bus bridge" },
4523cb28812SJohnny Huang 	{ 7, 16, 1,  1, "Force disable ESPI bus to AHB bus bridge" },
4533cb28812SJohnny Huang 	{ 7, 17, 1,  0, "Force enable LPC bus to AHB bus bridge1" },
4543cb28812SJohnny Huang 	{ 7, 17, 1,  1, "Force disable LPC bus to AHB bus bridge1" },
4553cb28812SJohnny Huang 	{ 7, 18, 1,  0, "Force enable LPC bus to AHB bus bridge2" },
4563cb28812SJohnny Huang 	{ 7, 18, 1,  1, "Force disable LPC bus to AHB bus bridge2" },
4573cb28812SJohnny Huang 	{ 7, 19, 1,  0, "Force enable UART1 debug port function" },
4583cb28812SJohnny Huang 	{ 7, 19, 1,  1, "Force disable UART1 debug port function" },
4593cb28812SJohnny Huang 	{ 7, 31, 1,  0, "Disable chip security setting" },
4603cb28812SJohnny Huang 	{ 7, 31, 1,  1, "Enable chip security setting" },
4613cb28812SJohnny Huang 	{ 8, 0,  32, OTP_REG_VALUE, "Redundancy Repair : 0x%x" },
4623cb28812SJohnny Huang 	{ 10, 0, 32, OTP_REG_VALUE, "Manifest ID low : 0x%x" },
4633cb28812SJohnny Huang 	{ 11, 0, 32, OTP_REG_VALUE, "Manifest ID high : 0x%x" }
4643cb28812SJohnny Huang };
4653cb28812SJohnny Huang 
46679e42a59SJoel Stanley static const struct otpconf_info a1_conf_info[] = {
4673cb28812SJohnny Huang 	{ 0, 0,  1,  OTP_REG_RESERVED, "" },
4683cb28812SJohnny Huang 	{ 0, 1,  1,  0, "Disable Secure Boot" },
4693cb28812SJohnny Huang 	{ 0, 1,  1,  1, "Enable Secure Boot" },
4703cb28812SJohnny Huang 	{ 0, 2,  1,  0, "Initialization programming not done" },
4713cb28812SJohnny Huang 	{ 0, 2,  1,  1, "Initialization programming done" },
4723cb28812SJohnny Huang 	{ 0, 3,  1,  0, "User region ECC disable" },
4733cb28812SJohnny Huang 	{ 0, 3,  1,  1, "User region ECC enable" },
4743cb28812SJohnny Huang 	{ 0, 4,  1,  0, "Secure Region ECC disable" },
4753cb28812SJohnny Huang 	{ 0, 4,  1,  1, "Secure Region ECC enable" },
4763cb28812SJohnny Huang 	{ 0, 5,  1,  0, "Enable low security key" },
4773cb28812SJohnny Huang 	{ 0, 5,  1,  1, "Disable low security key" },
4783cb28812SJohnny Huang 	{ 0, 6,  1,  0, "Do not ignore Secure Boot hardware strap" },
4793cb28812SJohnny Huang 	{ 0, 6,  1,  1, "Ignore Secure Boot hardware strap" },
4803cb28812SJohnny Huang 	{ 0, 7,  1,  0, "Secure Boot Mode: GCM" },
4813cb28812SJohnny Huang 	{ 0, 7,  1,  1, "Secure Boot Mode: 2" },
4823cb28812SJohnny Huang 	{ 0, 8,  2,  0, "Single cell mode (recommended)" },
4833cb28812SJohnny Huang 	{ 0, 8,  2,  1, "Differential mode" },
4843cb28812SJohnny Huang 	{ 0, 8,  2,  2, "Differential-redundant mode" },
4853cb28812SJohnny Huang 	{ 0, 10, 2,  0, "RSA mode : RSA1024" },
4863cb28812SJohnny Huang 	{ 0, 10, 2,  1, "RSA mode : RSA2048" },
4873cb28812SJohnny Huang 	{ 0, 10, 2,  2, "RSA mode : RSA3072" },
4883cb28812SJohnny Huang 	{ 0, 10, 2,  3, "RSA mode : RSA4096" },
4893cb28812SJohnny Huang 	{ 0, 12, 2,  0, "SHA mode : SHA224" },
4903cb28812SJohnny Huang 	{ 0, 12, 2,  1, "SHA mode : SHA256" },
4913cb28812SJohnny Huang 	{ 0, 12, 2,  2, "SHA mode : SHA384" },
4923cb28812SJohnny Huang 	{ 0, 12, 2,  3, "SHA mode : SHA512" },
493bb34a7bfSJohnny Huang 	{ 0, 14, 1,  0, "Disable Patch code" },
494bb34a7bfSJohnny Huang 	{ 0, 14, 1,  1, "Enable Patch code" },
495bb34a7bfSJohnny Huang 	{ 0, 15, 1,  OTP_REG_RESERVED, "" },
4963cb28812SJohnny Huang 	{ 0, 16, 6,  OTP_REG_VALUE, "Secure Region size (DW): 0x%x" },
4973cb28812SJohnny Huang 	{ 0, 22, 1,  0, "Secure Region : Writable" },
4983cb28812SJohnny Huang 	{ 0, 22, 1,  1, "Secure Region : Write Protect" },
4993cb28812SJohnny Huang 	{ 0, 23, 1,  0, "User Region : Writable" },
5003cb28812SJohnny Huang 	{ 0, 23, 1,  1, "User Region : Write Protect" },
5013cb28812SJohnny Huang 	{ 0, 24, 1,  0, "Configure Region : Writable" },
5023cb28812SJohnny Huang 	{ 0, 24, 1,  1, "Configure Region : Write Protect" },
5033cb28812SJohnny Huang 	{ 0, 25, 1,  0, "OTP strap Region : Writable" },
5043cb28812SJohnny Huang 	{ 0, 25, 1,  1, "OTP strap Region : Write Protect" },
5053cb28812SJohnny Huang 	{ 0, 26, 1,  0, "Disable Copy Boot Image to Internal SRAM" },
5063cb28812SJohnny Huang 	{ 0, 26, 1,  1, "Copy Boot Image to Internal SRAM" },
5073cb28812SJohnny Huang 	{ 0, 27, 1,  0, "Disable image encryption" },
5083cb28812SJohnny Huang 	{ 0, 27, 1,  1, "Enable image encryption" },
5093cb28812SJohnny Huang 	{ 0, 28, 1,  OTP_REG_RESERVED, "" },
5103cb28812SJohnny Huang 	{ 0, 29, 1,  0, "OTP key retire Region : Writable" },
5113cb28812SJohnny Huang 	{ 0, 29, 1,  1, "OTP key retire Region : Write Protect" },
5123cb28812SJohnny Huang 	{ 0, 30, 1,  0, "Data region redundancy repair disable" },
5133cb28812SJohnny Huang 	{ 0, 30, 1,  1, "Data region redundancy repair enable" },
5143cb28812SJohnny Huang 	{ 0, 31, 1,  0, "OTP memory lock disable" },
5153cb28812SJohnny Huang 	{ 0, 31, 1,  1, "OTP memory lock enable" },
51691448c03SJohnny Huang 	{ 2, 0,  16, OTP_REG_VALUE, "Vender ID : 0x%x" },
51791448c03SJohnny Huang 	{ 2, 16, 16, OTP_REG_VALUE, "Key Revision : 0x%x" },
51891448c03SJohnny Huang 	{ 3, 0,  16, OTP_REG_VALUE, "Secure boot header offset : 0x%x" },
51973f11549SJohnny Huang 	{ 4, 0,  8,  OTP_REG_VALID_BIT, "Keys valid  : %s" },
52073f11549SJohnny Huang 	{ 4, 16, 8,  OTP_REG_VALID_BIT, "Keys retire  : %s" },
52191448c03SJohnny Huang 	{ 5, 0,  32, OTP_REG_VALUE, "User define data, random number low : 0x%x" },
52291448c03SJohnny Huang 	{ 6, 0,  32, OTP_REG_VALUE, "User define data, random number high : 0x%x" },
52391448c03SJohnny Huang 	{ 7, 0,  1,  0, "Force enable PCI bus to AHB bus bridge" },
52491448c03SJohnny Huang 	{ 7, 0,  1,  1, "Force disable PCI bus to AHB bus bridge" },
52591448c03SJohnny Huang 	{ 7, 1,  1,  0, "Force enable UART5 debug port function" },
52691448c03SJohnny Huang 	{ 7, 1,  1,  1, "Force disable UART5 debug port function" },
52791448c03SJohnny Huang 	{ 7, 2,  1,  0, "Force enable XDMA function" },
52891448c03SJohnny Huang 	{ 7, 2,  1,  1, "Force disable XDMA function" },
52991448c03SJohnny Huang 	{ 7, 3,  1,  0, "Force enable APB to PCIE device bridge" },
53091448c03SJohnny Huang 	{ 7, 3,  1,  1, "Force disable APB to PCIE device bridge" },
53191448c03SJohnny Huang 	{ 7, 4,  1,  0, "Force enable APB to PCIE bridge config access" },
53291448c03SJohnny Huang 	{ 7, 4,  1,  1, "Force disable APB to PCIE bridge config access" },
53391448c03SJohnny Huang 	{ 7, 5,  1,  0, "Force enable PCIE bus trace buffer" },
53491448c03SJohnny Huang 	{ 7, 5,  1,  1, "Force disable PCIE bus trace buffer" },
53591448c03SJohnny Huang 	{ 7, 6,  1,  0, "Force enable the capability for PCIE device port as a Root Complex" },
53691448c03SJohnny Huang 	{ 7, 6,  1,  1, "Force disable the capability for PCIE device port as a Root Complex" },
53791448c03SJohnny Huang 	{ 7, 16, 1,  0, "Force enable ESPI bus to AHB bus bridge" },
53891448c03SJohnny Huang 	{ 7, 16, 1,  1, "Force disable ESPI bus to AHB bus bridge" },
53991448c03SJohnny Huang 	{ 7, 17, 1,  0, "Force enable LPC bus to AHB bus bridge1" },
54091448c03SJohnny Huang 	{ 7, 17, 1,  1, "Force disable LPC bus to AHB bus bridge1" },
54191448c03SJohnny Huang 	{ 7, 18, 1,  0, "Force enable LPC bus to AHB bus bridge2" },
54291448c03SJohnny Huang 	{ 7, 18, 1,  1, "Force disable LPC bus to AHB bus bridge2" },
54391448c03SJohnny Huang 	{ 7, 19, 1,  0, "Force enable UART1 debug port function" },
54491448c03SJohnny Huang 	{ 7, 19, 1,  1, "Force disable UART1 debug port function" },
54591448c03SJohnny Huang 	{ 7, 31, 1,  0, "Disable chip security setting" },
54691448c03SJohnny Huang 	{ 7, 31, 1,  1, "Enable chip security setting" },
54791448c03SJohnny Huang 	{ 8, 0,  32, OTP_REG_VALUE, "Redundancy Repair : 0x%x" },
54891448c03SJohnny Huang 	{ 10, 0, 32, OTP_REG_VALUE, "Manifest ID low : 0x%x" },
549bb34a7bfSJohnny Huang 	{ 11, 0, 32, OTP_REG_VALUE, "Manifest ID high : 0x%x" },
550bb34a7bfSJohnny Huang 	{ 14, 0, 11, OTP_REG_VALUE, "Patch code location (DW): 0x%x" },
551bb34a7bfSJohnny Huang 	{ 14, 11, 6, OTP_REG_VALUE, "Patch code size (DW): 0x%x" }
552b458cd62SJohnny Huang };
5539a4fe690SJohnny Huang 
55479e42a59SJoel Stanley static const struct otpkey_type a0_key_type[] = {
5559a4fe690SJohnny Huang 	{0, OTP_KEY_TYPE_AES,   0, "AES-256 as OEM platform key for image encryption/decryption"},
5569a4fe690SJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
5579a4fe690SJohnny Huang 	{4, OTP_KEY_TYPE_HMAC,  1, "HMAC as encrypted OEM HMAC keys in Mode 1"},
5589a4fe690SJohnny Huang 	{8, OTP_KEY_TYPE_RSA,   1, "RSA-public as OEM DSS public keys in Mode 2"},
5599a4fe690SJohnny Huang 	{9, OTP_KEY_TYPE_RSA,   0, "RSA-public as SOC public key"},
5609a4fe690SJohnny Huang 	{10, OTP_KEY_TYPE_RSA,  0, "RSA-public as AES key decryption key"},
5619a4fe690SJohnny Huang 	{13, OTP_KEY_TYPE_RSA,  0, "RSA-private as SOC private key"},
5629a4fe690SJohnny Huang 	{14, OTP_KEY_TYPE_RSA,  0, "RSA-private as AES key decryption key"},
5639a4fe690SJohnny Huang };
5649a4fe690SJohnny Huang 
56579e42a59SJoel Stanley static const struct otpkey_type a1_key_type[] = {
5669a4fe690SJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
5679a4fe690SJohnny 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"},
5689a4fe690SJohnny Huang 	{8, OTP_KEY_TYPE_RSA,   1, "RSA-public as OEM DSS public keys in Mode 2"},
5699a4fe690SJohnny Huang 	{10, OTP_KEY_TYPE_RSA,  0, "RSA-public as AES key decryption key"},
5709a4fe690SJohnny Huang 	{14, OTP_KEY_TYPE_RSA,  0, "RSA-private as AES key decryption key"},
5719a4fe690SJohnny Huang };
5729a4fe690SJohnny Huang 
5739a4fe690SJohnny Huang static uint32_t chip_version(void)
5749a4fe690SJohnny Huang {
5759a4fe690SJohnny Huang 	uint32_t rev_id;
5769a4fe690SJohnny Huang 
5779a4fe690SJohnny Huang 	rev_id = (readl(0x1e6e2004) >> 16) & 0xff ;
5789a4fe690SJohnny Huang 
5799a4fe690SJohnny Huang 	return rev_id;
5809a4fe690SJohnny Huang }
5819a4fe690SJohnny Huang 
5823d3688adSJohnny Huang static void wait_complete(void)
5833d3688adSJohnny Huang {
5843d3688adSJohnny Huang 	int reg;
5853d3688adSJohnny Huang 
5863d3688adSJohnny Huang 	do {
5873d3688adSJohnny Huang 		reg = readl(OTP_STATUS);
5883d3688adSJohnny Huang 	} while ((reg & 0x6) != 0x6);
5893d3688adSJohnny Huang }
5903d3688adSJohnny Huang 
5912a856b9aSJohnny Huang static void otp_read_data(uint32_t offset, uint32_t *data)
59269d5fd8fSJohnny Huang {
5933d3688adSJohnny Huang 	writel(offset, OTP_ADDR); //Read address
5943d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
5953d3688adSJohnny Huang 	wait_complete();
5963d3688adSJohnny Huang 	data[0] = readl(OTP_COMPARE_1);
5973d3688adSJohnny Huang 	data[1] = readl(OTP_COMPARE_2);
59869d5fd8fSJohnny Huang }
59969d5fd8fSJohnny Huang 
6002a856b9aSJohnny Huang static void otp_read_config(uint32_t offset, uint32_t *data)
60169d5fd8fSJohnny Huang {
60269d5fd8fSJohnny Huang 	int config_offset;
60369d5fd8fSJohnny Huang 
60469d5fd8fSJohnny Huang 	config_offset = 0x800;
60569d5fd8fSJohnny Huang 	config_offset |= (offset / 8) * 0x200;
60669d5fd8fSJohnny Huang 	config_offset |= (offset % 8) * 0x2;
60769d5fd8fSJohnny Huang 
6083d3688adSJohnny Huang 	writel(config_offset, OTP_ADDR);  //Read address
6093d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
6103d3688adSJohnny Huang 	wait_complete();
6113d3688adSJohnny Huang 	data[0] = readl(OTP_COMPARE_1);
61269d5fd8fSJohnny Huang }
61369d5fd8fSJohnny Huang 
61469d5fd8fSJohnny Huang static int otp_print_config(uint32_t offset, int dw_count)
61569d5fd8fSJohnny Huang {
61669d5fd8fSJohnny Huang 	int i;
61769d5fd8fSJohnny Huang 	uint32_t ret[1];
61869d5fd8fSJohnny Huang 
61969d5fd8fSJohnny Huang 	if (offset + dw_count > 32)
6202a856b9aSJohnny Huang 		return OTP_USAGE;
62169d5fd8fSJohnny Huang 	for (i = offset; i < offset + dw_count; i ++) {
62269d5fd8fSJohnny Huang 		otp_read_config(i, ret);
623a6af4a17SJohnny Huang 		printf("OTPCFG%X: %08X\n", i, ret[0]);
62469d5fd8fSJohnny Huang 	}
62569d5fd8fSJohnny Huang 	printf("\n");
6262a856b9aSJohnny Huang 	return OTP_SUCCESS;
62769d5fd8fSJohnny Huang }
62869d5fd8fSJohnny Huang 
62969d5fd8fSJohnny Huang static int otp_print_data(uint32_t offset, int dw_count)
63069d5fd8fSJohnny Huang {
63169d5fd8fSJohnny Huang 	int i;
63269d5fd8fSJohnny Huang 	uint32_t ret[2];
63369d5fd8fSJohnny Huang 
63469d5fd8fSJohnny Huang 	if (offset + dw_count > 2048 || offset % 4 != 0)
6352a856b9aSJohnny Huang 		return OTP_USAGE;
63669d5fd8fSJohnny Huang 	for (i = offset; i < offset + dw_count; i += 2) {
63769d5fd8fSJohnny Huang 		otp_read_data(i, ret);
63869d5fd8fSJohnny Huang 		if (i % 4 == 0)
63969d5fd8fSJohnny Huang 			printf("%03X: %08X %08X ", i * 4, ret[0], ret[1]);
64069d5fd8fSJohnny Huang 		else
64169d5fd8fSJohnny Huang 			printf("%08X %08X\n", ret[0], ret[1]);
64269d5fd8fSJohnny Huang 
64369d5fd8fSJohnny Huang 	}
64469d5fd8fSJohnny Huang 	printf("\n");
6452a856b9aSJohnny Huang 	return OTP_SUCCESS;
64669d5fd8fSJohnny Huang }
64769d5fd8fSJohnny Huang 
64869d5fd8fSJohnny Huang static int otp_compare(uint32_t otp_addr, uint32_t addr)
64969d5fd8fSJohnny Huang {
65069d5fd8fSJohnny Huang 	uint32_t ret;
65169d5fd8fSJohnny Huang 	uint32_t *buf;
65269d5fd8fSJohnny Huang 
65369d5fd8fSJohnny Huang 	buf = map_physmem(addr, 16, MAP_WRBACK);
65469d5fd8fSJohnny Huang 	printf("%08X\n", buf[0]);
65569d5fd8fSJohnny Huang 	printf("%08X\n", buf[1]);
65669d5fd8fSJohnny Huang 	printf("%08X\n", buf[2]);
65769d5fd8fSJohnny Huang 	printf("%08X\n", buf[3]);
6583d3688adSJohnny Huang 	writel(otp_addr, OTP_ADDR); //Compare address
6593d3688adSJohnny Huang 	writel(buf[0], OTP_COMPARE_1); //Compare data 1
6603d3688adSJohnny Huang 	writel(buf[1], OTP_COMPARE_2); //Compare data 2
6613d3688adSJohnny Huang 	writel(buf[2], OTP_COMPARE_3); //Compare data 3
6623d3688adSJohnny Huang 	writel(buf[3], OTP_COMPARE_4); //Compare data 4
6633d3688adSJohnny Huang 	writel(0x23b1e363, OTP_COMMAND); //Compare command
6643d3688adSJohnny Huang 	wait_complete();
6653d3688adSJohnny Huang 	ret = readl(OTP_STATUS); //Compare command
66669d5fd8fSJohnny Huang 	if (ret & 0x1)
66769d5fd8fSJohnny Huang 		return 0;
66869d5fd8fSJohnny Huang 	else
66969d5fd8fSJohnny Huang 		return -1;
67069d5fd8fSJohnny Huang }
67169d5fd8fSJohnny Huang 
67269d5fd8fSJohnny Huang static void otp_write(uint32_t otp_addr, uint32_t data)
67369d5fd8fSJohnny Huang {
6743d3688adSJohnny Huang 	writel(otp_addr, OTP_ADDR); //write address
6753d3688adSJohnny Huang 	writel(data, OTP_COMPARE_1); //write data
6763d3688adSJohnny Huang 	writel(0x23b1e362, OTP_COMMAND); //write command
6773d3688adSJohnny Huang 	wait_complete();
67869d5fd8fSJohnny Huang }
67969d5fd8fSJohnny Huang 
680a6d0d645SJohnny Huang static int verify_bit(uint32_t otp_addr, int bit_offset, int value)
68169d5fd8fSJohnny Huang {
68230a8c590SJohnny Huang 	uint32_t ret[2];
68369d5fd8fSJohnny Huang 
68430a8c590SJohnny Huang 	if (otp_addr % 2 == 0)
6853d3688adSJohnny Huang 		writel(otp_addr, OTP_ADDR); //Read address
68630a8c590SJohnny Huang 	else
6873d3688adSJohnny Huang 		writel(otp_addr - 1, OTP_ADDR); //Read address
68830a8c590SJohnny Huang 
6893d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
6903d3688adSJohnny Huang 	wait_complete();
6913d3688adSJohnny Huang 	ret[0] = readl(OTP_COMPARE_1);
6923d3688adSJohnny Huang 	ret[1] = readl(OTP_COMPARE_2);
69383655e91SJohnny Huang 
69430a8c590SJohnny Huang 	if (otp_addr % 2 == 0) {
69530a8c590SJohnny Huang 		if (((ret[0] >> bit_offset) & 1) == value)
69669d5fd8fSJohnny Huang 			return 0;
69769d5fd8fSJohnny Huang 		else
69869d5fd8fSJohnny Huang 			return -1;
69930a8c590SJohnny Huang 	} else {
70030a8c590SJohnny Huang 		if (((ret[1] >> bit_offset) & 1) == value)
70130a8c590SJohnny Huang 			return 0;
70230a8c590SJohnny Huang 		else
70330a8c590SJohnny Huang 			return -1;
70430a8c590SJohnny Huang 	}
70530a8c590SJohnny Huang 
70669d5fd8fSJohnny Huang }
70769d5fd8fSJohnny Huang 
708696656c6SJohnny Huang static uint32_t verify_dw(uint32_t otp_addr, uint32_t *value, uint32_t *ignore, uint32_t *compare, int size)
7094c1c9b35SJohnny Huang {
7104c1c9b35SJohnny Huang 	uint32_t ret[2];
7114c1c9b35SJohnny Huang 
7124c1c9b35SJohnny Huang 	otp_addr &= ~(1 << 15);
7134c1c9b35SJohnny Huang 
7144c1c9b35SJohnny Huang 	if (otp_addr % 2 == 0)
7153d3688adSJohnny Huang 		writel(otp_addr, OTP_ADDR); //Read address
7164c1c9b35SJohnny Huang 	else
7173d3688adSJohnny Huang 		writel(otp_addr - 1, OTP_ADDR); //Read address
7183d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
7193d3688adSJohnny Huang 	wait_complete();
7203d3688adSJohnny Huang 	ret[0] = readl(OTP_COMPARE_1);
7213d3688adSJohnny Huang 	ret[1] = readl(OTP_COMPARE_2);
7224c1c9b35SJohnny Huang 	if (size == 1) {
7234c1c9b35SJohnny Huang 		if (otp_addr % 2 == 0) {
7244c1c9b35SJohnny Huang 			// printf("check %x : %x = %x\n", otp_addr, ret[0], value[0]);
725696656c6SJohnny Huang 			if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0])) {
7264c1c9b35SJohnny Huang 				compare[0] = 0;
7274c1c9b35SJohnny Huang 				return 0;
7284c1c9b35SJohnny Huang 			} else {
7294c1c9b35SJohnny Huang 				compare[0] = value[0] ^ ret[0];
7304c1c9b35SJohnny Huang 				return -1;
7314c1c9b35SJohnny Huang 			}
7324c1c9b35SJohnny Huang 
7334c1c9b35SJohnny Huang 		} else {
7344c1c9b35SJohnny Huang 			// printf("check %x : %x = %x\n", otp_addr, ret[1], value[0]);
735696656c6SJohnny Huang 			if ((value[0] & ~ignore[0]) == (ret[1] & ~ignore[0])) {
7364c1c9b35SJohnny Huang 				compare[0] = ~0;
7374c1c9b35SJohnny Huang 				return 0;
7384c1c9b35SJohnny Huang 			} else {
739d90825e2SJohnny Huang 				compare[0] = ~(value[0] ^ ret[1]);
7404c1c9b35SJohnny Huang 				return -1;
7414c1c9b35SJohnny Huang 			}
7424c1c9b35SJohnny Huang 		}
7434c1c9b35SJohnny Huang 	} else if (size == 2) {
7444c1c9b35SJohnny Huang 		// otp_addr should be even
745696656c6SJohnny Huang 		if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0]) && (value[1] & ~ignore[1]) == (ret[1] & ~ignore[1])) {
7464c1c9b35SJohnny Huang 			// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
7474c1c9b35SJohnny Huang 			// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
7484c1c9b35SJohnny Huang 			compare[0] = 0;
7494c1c9b35SJohnny Huang 			compare[1] = ~0;
7504c1c9b35SJohnny Huang 			return 0;
7514c1c9b35SJohnny Huang 		} else {
7524c1c9b35SJohnny Huang 			// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
7534c1c9b35SJohnny Huang 			// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
7544c1c9b35SJohnny Huang 			compare[0] = value[0] ^ ret[0];
7554c1c9b35SJohnny Huang 			compare[1] = ~(value[1] ^ ret[1]);
7564c1c9b35SJohnny Huang 			return -1;
7574c1c9b35SJohnny Huang 		}
7584c1c9b35SJohnny Huang 	} else {
7594c1c9b35SJohnny Huang 		return -1;
7604c1c9b35SJohnny Huang 	}
7614c1c9b35SJohnny Huang }
7624c1c9b35SJohnny Huang 
7637e22f42dSJohnny Huang static void otp_soak(int soak)
764d90825e2SJohnny Huang {
765de6fbf1cSJohnny Huang 	switch (soak) {
766de6fbf1cSJohnny Huang 	case 0: //default
767de6fbf1cSJohnny Huang 		otp_write(0x3000, 0x0); // Write MRA
768de6fbf1cSJohnny Huang 		otp_write(0x5000, 0x0); // Write MRB
769de6fbf1cSJohnny Huang 		otp_write(0x1000, 0x0); // Write MR
770de6fbf1cSJohnny Huang 		break;
771de6fbf1cSJohnny Huang 	case 1: //normal program
772de6fbf1cSJohnny Huang 		otp_write(0x3000, 0x4021); // Write MRA
773de6fbf1cSJohnny Huang 		otp_write(0x5000, 0x302f); // Write MRB
774de6fbf1cSJohnny Huang 		otp_write(0x1000, 0x4020); // Write MR
775de6fbf1cSJohnny Huang 		writel(0x04190760, OTP_TIMING);
776de6fbf1cSJohnny Huang 		break;
777de6fbf1cSJohnny Huang 	case 2: //soak program
778d90825e2SJohnny Huang 		otp_write(0x3000, 0x4021); // Write MRA
779d90825e2SJohnny Huang 		otp_write(0x5000, 0x1027); // Write MRB
780d90825e2SJohnny Huang 		otp_write(0x1000, 0x4820); // Write MR
781de6fbf1cSJohnny Huang 		writel(0x041930d4, OTP_TIMING);
782de6fbf1cSJohnny Huang 		break;
783d90825e2SJohnny Huang 	}
784de6fbf1cSJohnny Huang 
7853d3688adSJohnny Huang 	wait_complete();
786d90825e2SJohnny Huang }
787d90825e2SJohnny Huang 
78883655e91SJohnny Huang static void otp_prog(uint32_t otp_addr, uint32_t prog_bit)
78983655e91SJohnny Huang {
79083655e91SJohnny Huang 	writel(otp_addr, OTP_ADDR); //write address
79183655e91SJohnny Huang 	writel(prog_bit, OTP_COMPARE_1); //write data
79283655e91SJohnny Huang 	writel(0x23b1e364, OTP_COMMAND); //write command
79383655e91SJohnny Huang 	wait_complete();
79483655e91SJohnny Huang }
79583655e91SJohnny Huang 
79683655e91SJohnny Huang static void _otp_prog_bit(uint32_t value, uint32_t prog_address, uint32_t bit_offset)
79783655e91SJohnny Huang {
79883655e91SJohnny Huang 	int prog_bit;
79983655e91SJohnny Huang 
80083655e91SJohnny Huang 	if (prog_address % 2 == 0) {
80183655e91SJohnny Huang 		if (value)
80283655e91SJohnny Huang 			prog_bit = ~(0x1 << bit_offset);
80383655e91SJohnny Huang 		else
80483655e91SJohnny Huang 			return;
80583655e91SJohnny Huang 	} else {
80683655e91SJohnny Huang 		prog_address |= 1 << 15;
80783655e91SJohnny Huang 		if (!value)
80883655e91SJohnny Huang 			prog_bit = 0x1 << bit_offset;
80983655e91SJohnny Huang 		else
81083655e91SJohnny Huang 			return;
81183655e91SJohnny Huang 	}
81283655e91SJohnny Huang 	otp_prog(prog_address, prog_bit);
81383655e91SJohnny Huang }
81483655e91SJohnny Huang 
81583655e91SJohnny Huang static int otp_prog_bit(uint32_t value, uint32_t prog_address, uint32_t bit_offset)
81683655e91SJohnny Huang {
81783655e91SJohnny Huang 	int pass;
81883655e91SJohnny Huang 	int i;
81983655e91SJohnny Huang 
82083655e91SJohnny Huang 	otp_soak(1);
82183655e91SJohnny Huang 	_otp_prog_bit(value, prog_address, bit_offset);
82283655e91SJohnny Huang 	pass = 0;
82383655e91SJohnny Huang 
82483655e91SJohnny Huang 	for (i = 0; i < RETRY; i++) {
82583655e91SJohnny Huang 		if (verify_bit(prog_address, bit_offset, value) != 0) {
82683655e91SJohnny Huang 			otp_soak(2);
82783655e91SJohnny Huang 			_otp_prog_bit(value, prog_address, bit_offset);
82883655e91SJohnny Huang 			if (verify_bit(prog_address, bit_offset, value) != 0) {
82983655e91SJohnny Huang 				otp_soak(1);
83083655e91SJohnny Huang 			} else {
83183655e91SJohnny Huang 				pass = 1;
83283655e91SJohnny Huang 				break;
83383655e91SJohnny Huang 			}
83483655e91SJohnny Huang 		} else {
83583655e91SJohnny Huang 			pass = 1;
83683655e91SJohnny Huang 			break;
83783655e91SJohnny Huang 		}
83883655e91SJohnny Huang 	}
83983655e91SJohnny Huang 
84083655e91SJohnny Huang 	return pass;
84183655e91SJohnny Huang }
84283655e91SJohnny Huang 
843696656c6SJohnny Huang static void otp_prog_dw(uint32_t value, uint32_t ignore, uint32_t prog_address)
844d90825e2SJohnny Huang {
845d90825e2SJohnny Huang 	int j, bit_value, prog_bit;
846d90825e2SJohnny Huang 
847d90825e2SJohnny Huang 	for (j = 0; j < 32; j++) {
848696656c6SJohnny Huang 		if ((ignore >> j) & 0x1)
849d90825e2SJohnny Huang 			continue;
850d90825e2SJohnny Huang 		bit_value = (value >> j) & 0x1;
851d90825e2SJohnny Huang 		if (prog_address % 2 == 0) {
852d90825e2SJohnny Huang 			if (bit_value)
853d90825e2SJohnny Huang 				prog_bit = ~(0x1 << j);
854d90825e2SJohnny Huang 			else
855d90825e2SJohnny Huang 				continue;
856d90825e2SJohnny Huang 		} else {
857d90825e2SJohnny Huang 			prog_address |= 1 << 15;
858d90825e2SJohnny Huang 			if (bit_value)
859d90825e2SJohnny Huang 				continue;
860d90825e2SJohnny Huang 			else
861d90825e2SJohnny Huang 				prog_bit = 0x1 << j;
862d90825e2SJohnny Huang 		}
863d90825e2SJohnny Huang 		otp_prog(prog_address, prog_bit);
864d90825e2SJohnny Huang 	}
865d90825e2SJohnny Huang }
866d90825e2SJohnny Huang 
86754552c69SJohnny Huang static int otp_prog_verify_2dw(uint32_t *data, uint32_t *buf, uint32_t *ignore_mask, uint32_t prog_address)
86854552c69SJohnny Huang {
86954552c69SJohnny Huang 	int pass;
87054552c69SJohnny Huang 	int i;
87154552c69SJohnny Huang 	uint32_t data0_masked;
87254552c69SJohnny Huang 	uint32_t data1_masked;
87354552c69SJohnny Huang 	uint32_t buf0_masked;
87454552c69SJohnny Huang 	uint32_t buf1_masked;
87554552c69SJohnny Huang 	uint32_t compare[2];
87654552c69SJohnny Huang 
87754552c69SJohnny Huang 	data0_masked = data[0]  & ~ignore_mask[0];
87854552c69SJohnny Huang 	buf0_masked  = buf[0] & ~ignore_mask[0];
87954552c69SJohnny Huang 	data1_masked = data[1]  & ~ignore_mask[1];
88054552c69SJohnny Huang 	buf1_masked  = buf[1] & ~ignore_mask[1];
88154552c69SJohnny Huang 	if ((data0_masked == buf0_masked) && (data1_masked == buf1_masked))
88254552c69SJohnny Huang 		return 0;
88354552c69SJohnny Huang 
88454552c69SJohnny Huang 	otp_soak(1);
88554552c69SJohnny Huang 	if (data0_masked != buf0_masked)
88654552c69SJohnny Huang 		otp_prog_dw(buf[0], ignore_mask[0], prog_address);
88754552c69SJohnny Huang 	if (data1_masked != buf1_masked)
88854552c69SJohnny Huang 		otp_prog_dw(buf[1], ignore_mask[1], prog_address + 1);
88954552c69SJohnny Huang 
89054552c69SJohnny Huang 	pass = 0;
89154552c69SJohnny Huang 	for (i = 0; i < RETRY; i++) {
89254552c69SJohnny Huang 		if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) {
89354552c69SJohnny Huang 			otp_soak(2);
89454552c69SJohnny Huang 			if (compare[0] != 0) {
89554552c69SJohnny Huang 				otp_prog_dw(compare[0], ignore_mask[0], prog_address);
89654552c69SJohnny Huang 			}
89754552c69SJohnny Huang 			if (compare[1] != ~0) {
89854552c69SJohnny Huang 				otp_prog_dw(compare[1], ignore_mask[0], prog_address + 1);
89954552c69SJohnny Huang 			}
90054552c69SJohnny Huang 			if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) {
90154552c69SJohnny Huang 				otp_soak(1);
90254552c69SJohnny Huang 			} else {
90354552c69SJohnny Huang 				pass = 1;
90454552c69SJohnny Huang 				break;
90554552c69SJohnny Huang 			}
90654552c69SJohnny Huang 		} else {
90754552c69SJohnny Huang 			pass = 1;
90854552c69SJohnny Huang 			break;
90954552c69SJohnny Huang 		}
91054552c69SJohnny Huang 	}
91154552c69SJohnny Huang 
91254552c69SJohnny Huang 	if (!pass) {
91354552c69SJohnny Huang 		otp_soak(0);
91454552c69SJohnny Huang 		return OTP_FAILURE;
91554552c69SJohnny Huang 	}
91654552c69SJohnny Huang 	return OTP_SUCCESS;
91754552c69SJohnny Huang }
91854552c69SJohnny Huang 
919541eb887SJohnny Huang static void otp_strap_status(struct otpstrap_status *otpstrap)
92076d13988SJohnny Huang {
92176d13988SJohnny Huang 	uint32_t OTPSTRAP_RAW[2];
9225010032bSJohnny Huang 	int strap_end;
92376d13988SJohnny Huang 	int i, j;
92476d13988SJohnny Huang 
9255010032bSJohnny Huang 	if (info_cb.version == OTP_AST2600A0) {
92676d13988SJohnny Huang 		for (j = 0; j < 64; j++) {
92776d13988SJohnny Huang 			otpstrap[j].value = 0;
92876d13988SJohnny Huang 			otpstrap[j].remain_times = 7;
92976d13988SJohnny Huang 			otpstrap[j].writeable_option = -1;
93076d13988SJohnny Huang 			otpstrap[j].protected = 0;
93176d13988SJohnny Huang 		}
9325010032bSJohnny Huang 		strap_end = 30;
9335010032bSJohnny Huang 	} else {
9345010032bSJohnny Huang 		for (j = 0; j < 64; j++) {
9355010032bSJohnny Huang 			otpstrap[j].value = 0;
9365010032bSJohnny Huang 			otpstrap[j].remain_times = 6;
9375010032bSJohnny Huang 			otpstrap[j].writeable_option = -1;
9385010032bSJohnny Huang 			otpstrap[j].reg_protected = 0;
9395010032bSJohnny Huang 			otpstrap[j].protected = 0;
9405010032bSJohnny Huang 		}
9415010032bSJohnny Huang 		strap_end = 28;
9425010032bSJohnny Huang 	}
94376d13988SJohnny Huang 
9445010032bSJohnny Huang 	for (i = 16; i < strap_end; i += 2) {
94576d13988SJohnny Huang 		int option = (i - 16) / 2;
94676d13988SJohnny Huang 		otp_read_config(i, &OTPSTRAP_RAW[0]);
94776d13988SJohnny Huang 		otp_read_config(i + 1, &OTPSTRAP_RAW[1]);
94876d13988SJohnny Huang 		for (j = 0; j < 32; j++) {
94976d13988SJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[0] >> j) & 0x1);
95076d13988SJohnny Huang 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
95176d13988SJohnny Huang 				otpstrap[j].writeable_option = option;
95276d13988SJohnny Huang 			}
95376d13988SJohnny Huang 			if (bit_value == 1)
95476d13988SJohnny Huang 				otpstrap[j].remain_times --;
95576d13988SJohnny Huang 			otpstrap[j].value ^= bit_value;
95676d13988SJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
95776d13988SJohnny Huang 		}
95876d13988SJohnny Huang 		for (j = 32; j < 64; j++) {
95976d13988SJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1);
96076d13988SJohnny Huang 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
96176d13988SJohnny Huang 				otpstrap[j].writeable_option = option;
96276d13988SJohnny Huang 			}
96376d13988SJohnny Huang 			if (bit_value == 1)
96476d13988SJohnny Huang 				otpstrap[j].remain_times --;
96576d13988SJohnny Huang 			otpstrap[j].value ^= bit_value;
96676d13988SJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
96776d13988SJohnny Huang 		}
96876d13988SJohnny Huang 	}
9695010032bSJohnny Huang 
9705010032bSJohnny Huang 	if (info_cb.version != OTP_AST2600A0) {
9715010032bSJohnny Huang 		otp_read_config(28, &OTPSTRAP_RAW[0]);
9725010032bSJohnny Huang 		otp_read_config(29, &OTPSTRAP_RAW[1]);
9735010032bSJohnny Huang 		for (j = 0; j < 32; j++) {
9745010032bSJohnny Huang 			if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
9755010032bSJohnny Huang 				otpstrap[j].reg_protected = 1;
9765010032bSJohnny Huang 		}
9775010032bSJohnny Huang 		for (j = 32; j < 64; j++) {
9785010032bSJohnny Huang 			if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
9795010032bSJohnny Huang 				otpstrap[j].reg_protected = 1;
9805010032bSJohnny Huang 		}
9815010032bSJohnny Huang 
9825010032bSJohnny Huang 	}
9835010032bSJohnny Huang 
98476d13988SJohnny Huang 	otp_read_config(30, &OTPSTRAP_RAW[0]);
98576d13988SJohnny Huang 	otp_read_config(31, &OTPSTRAP_RAW[1]);
98676d13988SJohnny Huang 	for (j = 0; j < 32; j++) {
98776d13988SJohnny Huang 		if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
98876d13988SJohnny Huang 			otpstrap[j].protected = 1;
98976d13988SJohnny Huang 	}
99076d13988SJohnny Huang 	for (j = 32; j < 64; j++) {
99176d13988SJohnny Huang 		if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
99276d13988SJohnny Huang 			otpstrap[j].protected = 1;
99376d13988SJohnny Huang 	}
99476d13988SJohnny Huang }
99576d13988SJohnny Huang 
996696656c6SJohnny Huang static int otp_print_conf_image(struct otp_image_layout *image_layout)
99769d5fd8fSJohnny Huang {
99879e42a59SJoel Stanley 	const struct otpconf_info *conf_info = info_cb.conf_info;
999696656c6SJohnny Huang 	uint32_t *OTPCFG = (uint32_t *)image_layout->conf;
1000696656c6SJohnny Huang 	uint32_t *OTPCFG_IGNORE = (uint32_t *)image_layout->conf_ignore;
1001b458cd62SJohnny Huang 	uint32_t mask;
1002b458cd62SJohnny Huang 	uint32_t dw_offset;
1003b458cd62SJohnny Huang 	uint32_t bit_offset;
1004b458cd62SJohnny Huang 	uint32_t otp_value;
1005696656c6SJohnny Huang 	uint32_t otp_ignore;
1006b458cd62SJohnny Huang 	int fail = 0;
100773f11549SJohnny Huang 	char valid_bit[20];
100866f2f8e5SJohnny Huang 	int i;
100973f11549SJohnny Huang 	int j;
101066f2f8e5SJohnny Huang 
1011737ed20bSJohnny Huang 	printf("DW    BIT        Value       Description\n");
101266f2f8e5SJohnny Huang 	printf("__________________________________________________________________________\n");
10133cb28812SJohnny Huang 	for (i = 0; i < info_cb.conf_info_len; i++) {
10143cb28812SJohnny Huang 		dw_offset = conf_info[i].dw_offset;
10153cb28812SJohnny Huang 		bit_offset = conf_info[i].bit_offset;
10163cb28812SJohnny Huang 		mask = BIT(conf_info[i].length) - 1;
1017b458cd62SJohnny Huang 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
1018696656c6SJohnny Huang 		otp_ignore = (OTPCFG_IGNORE[dw_offset] >> bit_offset) & mask;
1019b458cd62SJohnny Huang 
1020696656c6SJohnny Huang 		if (otp_ignore == mask) {
1021b458cd62SJohnny Huang 			continue;
1022696656c6SJohnny Huang 		} else if (otp_ignore != 0) {
1023b458cd62SJohnny Huang 			fail = 1;
1024b458cd62SJohnny Huang 		}
1025b458cd62SJohnny Huang 
10263cb28812SJohnny Huang 		if ((otp_value != conf_info[i].value) &&
10273cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_RESERVED &&
10283cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALUE &&
10293cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALID_BIT)
1030b458cd62SJohnny Huang 			continue;
1031b458cd62SJohnny Huang 		printf("0x%-4X", dw_offset);
1032b458cd62SJohnny Huang 
10333cb28812SJohnny Huang 		if (conf_info[i].length == 1) {
10343cb28812SJohnny Huang 			printf("0x%-9X", conf_info[i].bit_offset);
103566f2f8e5SJohnny Huang 		} else {
1036b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
10373cb28812SJohnny Huang 			       conf_info[i].bit_offset + conf_info[i].length - 1,
10383cb28812SJohnny Huang 			       conf_info[i].bit_offset);
103966f2f8e5SJohnny Huang 		}
1040b458cd62SJohnny Huang 		printf("0x%-10x", otp_value);
1041b458cd62SJohnny Huang 
1042b458cd62SJohnny Huang 		if (fail) {
1043696656c6SJohnny Huang 			printf("Ignore mask error\n");
1044b458cd62SJohnny Huang 		} else {
10453cb28812SJohnny Huang 			if (conf_info[i].value == OTP_REG_RESERVED) {
1046b458cd62SJohnny Huang 				printf("Reserved\n");
10473cb28812SJohnny Huang 			} else if (conf_info[i].value == OTP_REG_VALUE) {
10483cb28812SJohnny Huang 				printf(conf_info[i].information, otp_value);
1049b458cd62SJohnny Huang 				printf("\n");
10503cb28812SJohnny Huang 			} else if (conf_info[i].value == OTP_REG_VALID_BIT) {
1051b458cd62SJohnny Huang 				if (otp_value != 0) {
105273f11549SJohnny Huang 					for (j = 0; j < 7; j++) {
105373f11549SJohnny Huang 						if (otp_value == (1 << j)) {
105473f11549SJohnny Huang 							valid_bit[j * 2] = '1';
1055b458cd62SJohnny Huang 						} else {
105673f11549SJohnny Huang 							valid_bit[j * 2] = '0';
105773f11549SJohnny Huang 						}
105873f11549SJohnny Huang 						valid_bit[j * 2 + 1] = ' ';
105973f11549SJohnny Huang 					}
106073f11549SJohnny Huang 					valid_bit[15] = 0;
106173f11549SJohnny Huang 				} else {
106273f11549SJohnny Huang 					strcpy(valid_bit, "0 0 0 0 0 0 0 0\0");
1063b458cd62SJohnny Huang 				}
10643cb28812SJohnny Huang 				printf(conf_info[i].information, valid_bit);
1065b458cd62SJohnny Huang 				printf("\n");
1066b458cd62SJohnny Huang 			} else {
10673cb28812SJohnny Huang 				printf("%s\n", conf_info[i].information);
1068b458cd62SJohnny Huang 			}
1069b458cd62SJohnny Huang 		}
1070b458cd62SJohnny Huang 	}
1071b458cd62SJohnny Huang 
1072b458cd62SJohnny Huang 	if (fail)
1073b458cd62SJohnny Huang 		return OTP_FAILURE;
1074b458cd62SJohnny Huang 
107566f2f8e5SJohnny Huang 	return OTP_SUCCESS;
107666f2f8e5SJohnny Huang }
107766f2f8e5SJohnny Huang 
10782d4b0742SJohnny Huang static int otp_print_conf_info(int input_offset)
107966f2f8e5SJohnny Huang {
108079e42a59SJoel Stanley 	const struct otpconf_info *conf_info = info_cb.conf_info;
1081bb34a7bfSJohnny Huang 	uint32_t OTPCFG[16];
1082b458cd62SJohnny Huang 	uint32_t mask;
1083b458cd62SJohnny Huang 	uint32_t dw_offset;
1084b458cd62SJohnny Huang 	uint32_t bit_offset;
1085b458cd62SJohnny Huang 	uint32_t otp_value;
108673f11549SJohnny Huang 	char valid_bit[20];
108766f2f8e5SJohnny Huang 	int i;
108873f11549SJohnny Huang 	int j;
108966f2f8e5SJohnny Huang 
1090bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++)
109166f2f8e5SJohnny Huang 		otp_read_config(i, &OTPCFG[i]);
109266f2f8e5SJohnny Huang 
109366f2f8e5SJohnny Huang 
1094b458cd62SJohnny Huang 	printf("DW    BIT        Value       Description\n");
1095b458cd62SJohnny Huang 	printf("__________________________________________________________________________\n");
10963cb28812SJohnny Huang 	for (i = 0; i < info_cb.conf_info_len; i++) {
10973cb28812SJohnny Huang 		if (input_offset != -1 && input_offset != conf_info[i].dw_offset)
10982d4b0742SJohnny Huang 			continue;
10993cb28812SJohnny Huang 		dw_offset = conf_info[i].dw_offset;
11003cb28812SJohnny Huang 		bit_offset = conf_info[i].bit_offset;
11013cb28812SJohnny Huang 		mask = BIT(conf_info[i].length) - 1;
1102b458cd62SJohnny Huang 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
1103b458cd62SJohnny Huang 
11043cb28812SJohnny Huang 		if ((otp_value != conf_info[i].value) &&
11053cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_RESERVED &&
11063cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALUE &&
11073cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALID_BIT)
1108b458cd62SJohnny Huang 			continue;
1109b458cd62SJohnny Huang 		printf("0x%-4X", dw_offset);
1110b458cd62SJohnny Huang 
11113cb28812SJohnny Huang 		if (conf_info[i].length == 1) {
11123cb28812SJohnny Huang 			printf("0x%-9X", conf_info[i].bit_offset);
1113b458cd62SJohnny Huang 		} else {
1114b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
11153cb28812SJohnny Huang 			       conf_info[i].bit_offset + conf_info[i].length - 1,
11163cb28812SJohnny Huang 			       conf_info[i].bit_offset);
1117b458cd62SJohnny Huang 		}
1118b458cd62SJohnny Huang 		printf("0x%-10x", otp_value);
1119b458cd62SJohnny Huang 
11203cb28812SJohnny Huang 		if (conf_info[i].value == OTP_REG_RESERVED) {
1121b458cd62SJohnny Huang 			printf("Reserved\n");
11223cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALUE) {
11233cb28812SJohnny Huang 			printf(conf_info[i].information, otp_value);
1124b458cd62SJohnny Huang 			printf("\n");
11253cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALID_BIT) {
1126b458cd62SJohnny Huang 			if (otp_value != 0) {
112773f11549SJohnny Huang 				for (j = 0; j < 7; j++) {
112873f11549SJohnny Huang 					if (otp_value == (1 << j)) {
112973f11549SJohnny Huang 						valid_bit[j * 2] = '1';
1130b458cd62SJohnny Huang 					} else {
113173f11549SJohnny Huang 						valid_bit[j * 2] = '0';
113273f11549SJohnny Huang 					}
113373f11549SJohnny Huang 					valid_bit[j * 2 + 1] = ' ';
113473f11549SJohnny Huang 				}
113573f11549SJohnny Huang 				valid_bit[15] = 0;
113673f11549SJohnny Huang 			} else {
113773f11549SJohnny Huang 				strcpy(valid_bit, "0 0 0 0 0 0 0 0\0");
1138b458cd62SJohnny Huang 			}
11393cb28812SJohnny Huang 			printf(conf_info[i].information, valid_bit);
1140b458cd62SJohnny Huang 			printf("\n");
1141b458cd62SJohnny Huang 		} else {
11423cb28812SJohnny Huang 			printf("%s\n", conf_info[i].information);
1143b458cd62SJohnny Huang 		}
1144b458cd62SJohnny Huang 	}
1145b458cd62SJohnny Huang 	return OTP_SUCCESS;
114666f2f8e5SJohnny Huang }
114766f2f8e5SJohnny Huang 
11485010032bSJohnny Huang static int otp_print_strap_image(struct otp_image_layout *image_layout)
114976d13988SJohnny Huang {
115079e42a59SJoel Stanley 	const struct otpstrap_info *strap_info = info_cb.strap_info;
1151696656c6SJohnny Huang 	uint32_t *OTPSTRAP;
1152696656c6SJohnny Huang 	uint32_t *OTPSTRAP_REG_PRO;
1153696656c6SJohnny Huang 	uint32_t *OTPSTRAP_PRO;
1154696656c6SJohnny Huang 	uint32_t *OTPSTRAP_IGNORE;
115576d13988SJohnny Huang 	int i;
1156a8bd6d8cSJohnny Huang 	int fail = 0;
1157a8bd6d8cSJohnny Huang 	uint32_t bit_offset;
1158a8bd6d8cSJohnny Huang 	uint32_t dw_offset;
1159a8bd6d8cSJohnny Huang 	uint32_t mask;
1160a8bd6d8cSJohnny Huang 	uint32_t otp_value;
1161696656c6SJohnny Huang 	uint32_t otp_reg_protect;
1162a8bd6d8cSJohnny Huang 	uint32_t otp_protect;
1163696656c6SJohnny Huang 	uint32_t otp_ignore;
116476d13988SJohnny Huang 
1165696656c6SJohnny Huang 	OTPSTRAP = (uint32_t *)image_layout->strap;
1166696656c6SJohnny Huang 	OTPSTRAP_PRO = (uint32_t *)image_layout->strap_pro;
1167696656c6SJohnny Huang 	OTPSTRAP_IGNORE = (uint32_t *)image_layout->strap_ignore;
11685010032bSJohnny Huang 	if (info_cb.version == OTP_AST2600A0) {
1169696656c6SJohnny Huang 		OTPSTRAP_REG_PRO = NULL;
1170a8bd6d8cSJohnny Huang 		printf("BIT(hex)   Value       Protect     Description\n");
1171696656c6SJohnny Huang 	} else {
1172696656c6SJohnny Huang 		OTPSTRAP_REG_PRO = (uint32_t *)image_layout->strap_reg_pro;
1173de6b0cc4SJohnny Huang 		printf("BIT(hex)   Value       Reg_Protect Protect     Description\n");
1174696656c6SJohnny Huang 	}
1175de6b0cc4SJohnny Huang 	printf("__________________________________________________________________________________________\n");
1176b458cd62SJohnny Huang 
11773cb28812SJohnny Huang 	for (i = 0; i < info_cb.strap_info_len; i++) {
1178696656c6SJohnny Huang 		if (strap_info[i].bit_offset > 31) {
1179a8bd6d8cSJohnny Huang 			dw_offset = 1;
11803cb28812SJohnny Huang 			bit_offset = strap_info[i].bit_offset - 32;
1181a8bd6d8cSJohnny Huang 		} else {
1182a8bd6d8cSJohnny Huang 			dw_offset = 0;
11833cb28812SJohnny Huang 			bit_offset = strap_info[i].bit_offset;
1184a8bd6d8cSJohnny Huang 		}
118576d13988SJohnny Huang 
11863cb28812SJohnny Huang 		mask = BIT(strap_info[i].length) - 1;
1187a8bd6d8cSJohnny Huang 		otp_value = (OTPSTRAP[dw_offset] >> bit_offset) & mask;
1188a8bd6d8cSJohnny Huang 		otp_protect = (OTPSTRAP_PRO[dw_offset] >> bit_offset) & mask;
1189696656c6SJohnny Huang 		otp_ignore = (OTPSTRAP_IGNORE[dw_offset] >> bit_offset) & mask;
1190a8bd6d8cSJohnny Huang 
11915010032bSJohnny Huang 		if (info_cb.version != OTP_AST2600A0)
1192696656c6SJohnny Huang 			otp_reg_protect = (OTPSTRAP_REG_PRO[dw_offset] >> bit_offset) & mask;
11935010032bSJohnny Huang 		else
11945010032bSJohnny Huang 			otp_reg_protect = 0;
1195696656c6SJohnny Huang 
1196696656c6SJohnny Huang 		if (otp_ignore == mask) {
1197a8bd6d8cSJohnny Huang 			continue;
1198696656c6SJohnny Huang 		} else if (otp_ignore != 0) {
1199a8bd6d8cSJohnny Huang 			fail = 1;
1200a8bd6d8cSJohnny Huang 		}
1201a8bd6d8cSJohnny Huang 
12023cb28812SJohnny Huang 		if ((otp_value != strap_info[i].value) &&
12033cb28812SJohnny Huang 		    strap_info[i].value != OTP_REG_RESERVED)
1204a8bd6d8cSJohnny Huang 			continue;
1205a8bd6d8cSJohnny Huang 
12063cb28812SJohnny Huang 		if (strap_info[i].length == 1) {
12073cb28812SJohnny Huang 			printf("0x%-9X", strap_info[i].bit_offset);
1208a8bd6d8cSJohnny Huang 		} else {
1209b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
12103cb28812SJohnny Huang 			       strap_info[i].bit_offset + strap_info[i].length - 1,
12113cb28812SJohnny Huang 			       strap_info[i].bit_offset);
1212a8bd6d8cSJohnny Huang 		}
1213a8bd6d8cSJohnny Huang 		printf("0x%-10x", otp_value);
12145010032bSJohnny Huang 		if (info_cb.version != OTP_AST2600A0)
1215696656c6SJohnny Huang 			printf("0x%-10x", otp_reg_protect);
1216a8bd6d8cSJohnny Huang 		printf("0x%-10x", otp_protect);
1217a8bd6d8cSJohnny Huang 
1218a8bd6d8cSJohnny Huang 		if (fail) {
1219696656c6SJohnny Huang 			printf("Ignore mask error\n");
1220a8bd6d8cSJohnny Huang 		} else {
12213cb28812SJohnny Huang 			if (strap_info[i].value != OTP_REG_RESERVED)
12223cb28812SJohnny Huang 				printf("%s\n", strap_info[i].information);
1223a8bd6d8cSJohnny Huang 			else
1224a8bd6d8cSJohnny Huang 				printf("Reserved\n");
1225a8bd6d8cSJohnny Huang 		}
1226a8bd6d8cSJohnny Huang 	}
1227a8bd6d8cSJohnny Huang 
1228a8bd6d8cSJohnny Huang 	if (fail)
122976d13988SJohnny Huang 		return OTP_FAILURE;
123076d13988SJohnny Huang 
123176d13988SJohnny Huang 	return OTP_SUCCESS;
123276d13988SJohnny Huang }
123376d13988SJohnny Huang 
1234b458cd62SJohnny Huang static int otp_print_strap_info(int view)
123576d13988SJohnny Huang {
123679e42a59SJoel Stanley 	const struct otpstrap_info *strap_info = info_cb.strap_info;
123776d13988SJohnny Huang 	struct otpstrap_status strap_status[64];
123807baa4e8SJohnny Huang 	int i, j;
1239b458cd62SJohnny Huang 	int fail = 0;
1240b458cd62SJohnny Huang 	uint32_t bit_offset;
1241b458cd62SJohnny Huang 	uint32_t length;
1242b458cd62SJohnny Huang 	uint32_t otp_value;
1243b458cd62SJohnny Huang 	uint32_t otp_protect;
124476d13988SJohnny Huang 
1245541eb887SJohnny Huang 	otp_strap_status(strap_status);
124676d13988SJohnny Huang 
1247b458cd62SJohnny Huang 	if (view) {
124883655e91SJohnny Huang 		if (info_cb.version == OTP_AST2600A0)
124907baa4e8SJohnny Huang 			printf("BIT(hex) Value  Remains  Protect   Description\n");
125083655e91SJohnny Huang 		else
125183655e91SJohnny Huang 			printf("BIT(hex) Value  Remains  Reg_Protect Protect   Description\n");
125207baa4e8SJohnny Huang 		printf("___________________________________________________________________________________________________\n");
1253b458cd62SJohnny Huang 	} else {
1254b458cd62SJohnny Huang 		printf("BIT(hex)   Value       Description\n");
1255b458cd62SJohnny Huang 		printf("________________________________________________________________________________\n");
125676d13988SJohnny Huang 	}
12573cb28812SJohnny Huang 	for (i = 0; i < info_cb.strap_info_len; i++) {
1258b458cd62SJohnny Huang 		otp_value = 0;
12593cb28812SJohnny Huang 		bit_offset = strap_info[i].bit_offset;
12603cb28812SJohnny Huang 		length = strap_info[i].length;
1261b458cd62SJohnny Huang 		for (j = 0; j < length; j++) {
1262c947ef08SJohnny Huang 			otp_value |= strap_status[bit_offset + j].value << j;
1263c947ef08SJohnny Huang 			otp_protect |= strap_status[bit_offset + j].protected << j;
1264b458cd62SJohnny Huang 		}
12653cb28812SJohnny Huang 		if ((otp_value != strap_info[i].value) &&
12663cb28812SJohnny Huang 		    strap_info[i].value != OTP_REG_RESERVED)
1267b458cd62SJohnny Huang 			continue;
1268b458cd62SJohnny Huang 		if (view) {
1269b458cd62SJohnny Huang 			for (j = 0; j < length; j++) {
12703cb28812SJohnny Huang 				printf("0x%-7X", strap_info[i].bit_offset + j);
1271b458cd62SJohnny Huang 				printf("0x%-5X", strap_status[bit_offset + j].value);
127207baa4e8SJohnny Huang 				printf("%-9d", strap_status[bit_offset + j].remain_times);
127383655e91SJohnny Huang 				if (info_cb.version != OTP_AST2600A0)
1274*e1a7245eSJohnny Huang 					printf("0x%-10X", strap_status[bit_offset + j].reg_protected);
1275*e1a7245eSJohnny Huang 				printf("0x%-7X", strap_status[bit_offset + j].protected);
12763cb28812SJohnny Huang 				if (strap_info[i].value == OTP_REG_RESERVED) {
1277b458cd62SJohnny Huang 					printf(" Reserved\n");
1278b458cd62SJohnny Huang 					continue;
1279b458cd62SJohnny Huang 				}
1280b458cd62SJohnny Huang 				if (length == 1) {
12813cb28812SJohnny Huang 					printf(" %s\n", strap_info[i].information);
1282b458cd62SJohnny Huang 					continue;
128376d13988SJohnny Huang 				}
128476d13988SJohnny Huang 
1285b458cd62SJohnny Huang 				if (j == 0)
12863cb28812SJohnny Huang 					printf("/%s\n", strap_info[i].information);
1287b458cd62SJohnny Huang 				else if (j == length - 1)
1288b458cd62SJohnny Huang 					printf("\\ \"\n");
1289b458cd62SJohnny Huang 				else
1290b458cd62SJohnny Huang 					printf("| \"\n");
129176d13988SJohnny Huang 			}
1292b458cd62SJohnny Huang 		} else {
1293c947ef08SJohnny Huang 			if (length == 1) {
12943cb28812SJohnny Huang 				printf("0x%-9X", strap_info[i].bit_offset);
1295b458cd62SJohnny Huang 			} else {
1296b458cd62SJohnny Huang 				printf("0x%-2X:0x%-4X",
1297b458cd62SJohnny Huang 				       bit_offset + length - 1, bit_offset);
1298b458cd62SJohnny Huang 			}
1299b458cd62SJohnny Huang 
1300b458cd62SJohnny Huang 			printf("0x%-10X", otp_value);
1301b458cd62SJohnny Huang 
13023cb28812SJohnny Huang 			if (strap_info[i].value != OTP_REG_RESERVED)
13033cb28812SJohnny Huang 				printf("%s\n", strap_info[i].information);
1304b458cd62SJohnny Huang 			else
1305b458cd62SJohnny Huang 				printf("Reserved\n");
1306b458cd62SJohnny Huang 		}
1307b458cd62SJohnny Huang 	}
1308b458cd62SJohnny Huang 
1309b458cd62SJohnny Huang 	if (fail)
1310b458cd62SJohnny Huang 		return OTP_FAILURE;
1311b458cd62SJohnny Huang 
1312b458cd62SJohnny Huang 	return OTP_SUCCESS;
1313b458cd62SJohnny Huang }
1314b458cd62SJohnny Huang 
1315696656c6SJohnny Huang static void buf_print(uint8_t *buf, int len)
131669d5fd8fSJohnny Huang {
131769d5fd8fSJohnny Huang 	int i;
131869d5fd8fSJohnny Huang 	printf("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
131969d5fd8fSJohnny Huang 	for (i = 0; i < len; i++) {
132069d5fd8fSJohnny Huang 		if (i % 16 == 0) {
132169d5fd8fSJohnny Huang 			printf("%04X: ", i);
132269d5fd8fSJohnny Huang 		}
132369d5fd8fSJohnny Huang 		printf("%02X ", buf[i]);
132469d5fd8fSJohnny Huang 		if ((i + 1) % 16 == 0) {
132569d5fd8fSJohnny Huang 			printf("\n");
132669d5fd8fSJohnny Huang 		}
132769d5fd8fSJohnny Huang 	}
132869d5fd8fSJohnny Huang }
132969d5fd8fSJohnny Huang 
1330696656c6SJohnny Huang static int otp_print_data_info(struct otp_image_layout *image_layout)
133169d5fd8fSJohnny Huang {
133269d5fd8fSJohnny Huang 	int key_id, key_offset, last, key_type, key_length, exp_length;
133379e42a59SJoel Stanley 	const struct otpkey_type *key_info_array = info_cb.key_info;
13349a4fe690SJohnny Huang 	struct otpkey_type key_info;
1335696656c6SJohnny Huang 	uint32_t *buf;
1336696656c6SJohnny Huang 	uint8_t *byte_buf;
13379d998018SJohnny Huang 	char empty = 1;
133869d5fd8fSJohnny Huang 	int i = 0, len = 0;
13399a4fe690SJohnny Huang 	int j;
134054552c69SJohnny Huang 
1341696656c6SJohnny Huang 	byte_buf = image_layout->data;
1342696656c6SJohnny Huang 	buf = (uint32_t *)byte_buf;
13439d998018SJohnny Huang 
13449d998018SJohnny Huang 	for (i = 0; i < 16; i++) {
13459d998018SJohnny Huang 		if (buf[i] != 0) {
13469d998018SJohnny Huang 			empty = 0;
13479d998018SJohnny Huang 		}
13489d998018SJohnny Huang 	}
13499d998018SJohnny Huang 	if (empty)
13509d998018SJohnny Huang 		return 0;
13519d998018SJohnny Huang 
13529d998018SJohnny Huang 	i = 0;
135369d5fd8fSJohnny Huang 	while (1) {
135469d5fd8fSJohnny Huang 		key_id = buf[i] & 0x7;
135569d5fd8fSJohnny Huang 		key_offset = buf[i] & 0x1ff8;
135669d5fd8fSJohnny Huang 		last = (buf[i] >> 13) & 1;
135769d5fd8fSJohnny Huang 		key_type = (buf[i] >> 14) & 0xf;
135869d5fd8fSJohnny Huang 		key_length = (buf[i] >> 18) & 0x3;
135969d5fd8fSJohnny Huang 		exp_length = (buf[i] >> 20) & 0xfff;
13609a4fe690SJohnny Huang 
13619a4fe690SJohnny Huang 		for (j = 0; j < info_cb.key_info_len; j++) {
13629a4fe690SJohnny Huang 			if (key_type == key_info_array[j].value) {
13639a4fe690SJohnny Huang 				key_info = key_info_array[j];
13649a4fe690SJohnny Huang 				break;
13659a4fe690SJohnny Huang 			}
13669a4fe690SJohnny Huang 		}
13679a4fe690SJohnny Huang 
13687f795e57SJohnny Huang 		printf("\nKey[%d]:\n", i);
136969d5fd8fSJohnny Huang 		printf("Key Type: ");
13709a4fe690SJohnny Huang 		printf("%s\n", key_info.information);
13719a4fe690SJohnny Huang 
13729a4fe690SJohnny Huang 		if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
137369d5fd8fSJohnny Huang 			printf("HMAC SHA Type: ");
137469d5fd8fSJohnny Huang 			switch (key_length) {
137569d5fd8fSJohnny Huang 			case 0:
137669d5fd8fSJohnny Huang 				printf("HMAC(SHA224)\n");
137769d5fd8fSJohnny Huang 				break;
137869d5fd8fSJohnny Huang 			case 1:
137969d5fd8fSJohnny Huang 				printf("HMAC(SHA256)\n");
138069d5fd8fSJohnny Huang 				break;
138169d5fd8fSJohnny Huang 			case 2:
138269d5fd8fSJohnny Huang 				printf("HMAC(SHA384)\n");
138369d5fd8fSJohnny Huang 				break;
138469d5fd8fSJohnny Huang 			case 3:
138569d5fd8fSJohnny Huang 				printf("HMAC(SHA512)\n");
138669d5fd8fSJohnny Huang 				break;
138769d5fd8fSJohnny Huang 			}
13889a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA) {
138969d5fd8fSJohnny Huang 			printf("RSA SHA Type: ");
139069d5fd8fSJohnny Huang 			switch (key_length) {
139169d5fd8fSJohnny Huang 			case 0:
139269d5fd8fSJohnny Huang 				printf("RSA1024\n");
139369d5fd8fSJohnny Huang 				len = 0x100;
139469d5fd8fSJohnny Huang 				break;
139569d5fd8fSJohnny Huang 			case 1:
139669d5fd8fSJohnny Huang 				printf("RSA2048\n");
139769d5fd8fSJohnny Huang 				len = 0x200;
139869d5fd8fSJohnny Huang 				break;
139969d5fd8fSJohnny Huang 			case 2:
140069d5fd8fSJohnny Huang 				printf("RSA3072\n");
140169d5fd8fSJohnny Huang 				len = 0x300;
140269d5fd8fSJohnny Huang 				break;
140369d5fd8fSJohnny Huang 			case 3:
140469d5fd8fSJohnny Huang 				printf("RSA4096\n");
140569d5fd8fSJohnny Huang 				len = 0x400;
140669d5fd8fSJohnny Huang 				break;
140769d5fd8fSJohnny Huang 			}
140869d5fd8fSJohnny Huang 			printf("RSA exponent bit length: %d\n", exp_length);
140969d5fd8fSJohnny Huang 		}
14109a4fe690SJohnny Huang 		if (key_info.need_id)
141169d5fd8fSJohnny Huang 			printf("Key Number ID: %d\n", key_id);
141269d5fd8fSJohnny Huang 		printf("Key Value:\n");
14139a4fe690SJohnny Huang 		if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
141469d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], 0x40);
14159a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_AES) {
14169a4fe690SJohnny Huang 			printf("AES Key:\n");
14179a4fe690SJohnny Huang 			buf_print(&byte_buf[key_offset], 0x20);
14189a4fe690SJohnny Huang 			if (info_cb.version == 0) {
14199a4fe690SJohnny Huang 				printf("AES IV:\n");
14209a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x10);
14219a4fe690SJohnny Huang 			}
14229a4fe690SJohnny Huang 
14239a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_VAULT) {
14249a4fe690SJohnny Huang 			if (info_cb.version == 0) {
142569d5fd8fSJohnny Huang 				printf("AES Key:\n");
142669d5fd8fSJohnny Huang 				buf_print(&byte_buf[key_offset], 0x20);
142769d5fd8fSJohnny Huang 				printf("AES IV:\n");
142869d5fd8fSJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x10);
14299a4fe690SJohnny Huang 			} else if (info_cb.version == 1) {
14309a4fe690SJohnny Huang 				printf("AES Key 1:\n");
14319a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset], 0x20);
14329a4fe690SJohnny Huang 				printf("AES Key 2:\n");
14339a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x20);
14349a4fe690SJohnny Huang 			}
143569d5fd8fSJohnny Huang 
14369a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA) {
143769d5fd8fSJohnny Huang 			printf("RSA mod:\n");
143869d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], len / 2);
143969d5fd8fSJohnny Huang 			printf("RSA exp:\n");
144069d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset + (len / 2)], len / 2);
144169d5fd8fSJohnny Huang 		}
144269d5fd8fSJohnny Huang 		if (last)
144369d5fd8fSJohnny Huang 			break;
144469d5fd8fSJohnny Huang 		i++;
144569d5fd8fSJohnny Huang 	}
144669d5fd8fSJohnny Huang 	return 0;
144769d5fd8fSJohnny Huang }
144869d5fd8fSJohnny Huang 
14495010032bSJohnny Huang static int otp_prog_conf(struct otp_image_layout *image_layout)
145069d5fd8fSJohnny Huang {
1451a6d0d645SJohnny Huang 	int i, k;
1452d90825e2SJohnny Huang 	int pass = 0;
1453a6d0d645SJohnny Huang 	uint32_t prog_address;
1454bb34a7bfSJohnny Huang 	uint32_t data[16];
1455a6d0d645SJohnny Huang 	uint32_t compare[2];
14565010032bSJohnny Huang 	uint32_t *conf = (uint32_t *)image_layout->conf;
14575010032bSJohnny Huang 	uint32_t *conf_ignore = (uint32_t *)image_layout->conf_ignore;
1458d90825e2SJohnny Huang 	uint32_t data_masked;
1459d90825e2SJohnny Huang 	uint32_t buf_masked;
146069d5fd8fSJohnny Huang 
1461a6d0d645SJohnny Huang 	printf("Read OTP Config Region:\n");
1462a6d0d645SJohnny Huang 
1463bb34a7bfSJohnny Huang 	for (i = 0; i < 16 ; i ++) {
146469d5fd8fSJohnny Huang 		prog_address = 0x800;
1465a6d0d645SJohnny Huang 		prog_address |= (i / 8) * 0x200;
1466a6d0d645SJohnny Huang 		prog_address |= (i % 8) * 0x2;
1467a6d0d645SJohnny Huang 		otp_read_data(prog_address, &data[i]);
1468a6d0d645SJohnny Huang 	}
1469a6d0d645SJohnny Huang 
1470a6d0d645SJohnny Huang 	printf("Check writable...\n");
1471bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++) {
14725010032bSJohnny Huang 		data_masked = data[i]  & ~conf_ignore[i];
14735010032bSJohnny Huang 		buf_masked  = conf[i] & ~conf_ignore[i];
1474d90825e2SJohnny Huang 		if (data_masked == buf_masked)
147569d5fd8fSJohnny Huang 			continue;
1476d90825e2SJohnny Huang 		if ((data_masked | buf_masked) == buf_masked) {
1477a6d0d645SJohnny Huang 			continue;
1478a6d0d645SJohnny Huang 		} else {
1479a6d0d645SJohnny Huang 			printf("Input image can't program into OTP, please check.\n");
1480a6af4a17SJohnny Huang 			printf("OTPCFG[%X] = %x\n", i, data[i]);
14815010032bSJohnny Huang 			printf("Input [%X] = %x\n", i, conf[i]);
14825010032bSJohnny Huang 			printf("Mask  [%X] = %x\n", i, ~conf_ignore[i]);
14832a856b9aSJohnny Huang 			return OTP_FAILURE;
1484a6d0d645SJohnny Huang 		}
1485a6d0d645SJohnny Huang 	}
1486a6d0d645SJohnny Huang 
1487a6d0d645SJohnny Huang 	printf("Start Programing...\n");
1488d90825e2SJohnny Huang 	otp_soak(0);
1489bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++) {
14905010032bSJohnny Huang 		data_masked = data[i]  & ~conf_ignore[i];
14915010032bSJohnny Huang 		buf_masked  = conf[i] & ~conf_ignore[i];
1492a6d0d645SJohnny Huang 		prog_address = 0x800;
1493a6d0d645SJohnny Huang 		prog_address |= (i / 8) * 0x200;
1494a6d0d645SJohnny Huang 		prog_address |= (i % 8) * 0x2;
1495bb34a7bfSJohnny Huang 		if (data_masked == buf_masked) {
1496bb34a7bfSJohnny Huang 			pass = 1;
1497a6d0d645SJohnny Huang 			continue;
1498bb34a7bfSJohnny Huang 		}
1499de6fbf1cSJohnny Huang 
1500a6d0d645SJohnny Huang 
1501de6fbf1cSJohnny Huang 		otp_soak(1);
15025010032bSJohnny Huang 		otp_prog_dw(conf[i], conf_ignore[i], prog_address);
1503a6d0d645SJohnny Huang 
150469d5fd8fSJohnny Huang 		pass = 0;
150569d5fd8fSJohnny Huang 		for (k = 0; k < RETRY; k++) {
15065010032bSJohnny Huang 			if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) {
1507de6fbf1cSJohnny Huang 				otp_soak(2);
1508a6d0d645SJohnny Huang 				otp_prog_dw(compare[0], prog_address, 1);
15095010032bSJohnny Huang 				if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) {
1510de6fbf1cSJohnny Huang 					otp_soak(1);
1511de6fbf1cSJohnny Huang 				} else {
1512de6fbf1cSJohnny Huang 					pass = 1;
1513de6fbf1cSJohnny Huang 					break;
1514de6fbf1cSJohnny Huang 				}
1515a6d0d645SJohnny Huang 			} else {
151669d5fd8fSJohnny Huang 				pass = 1;
151769d5fd8fSJohnny Huang 				break;
151869d5fd8fSJohnny Huang 			}
151969d5fd8fSJohnny Huang 		}
1520bb34a7bfSJohnny Huang 		if (pass == 0) {
1521bb34a7bfSJohnny Huang 			printf("address: %08x, data: %08x, buffer: %08x, mask: %08x\n",
15225010032bSJohnny Huang 			       i, data[i], conf[i], conf_ignore[i]);
1523bb34a7bfSJohnny Huang 			break;
1524bb34a7bfSJohnny Huang 		}
1525a6d0d645SJohnny Huang 	}
1526a6d0d645SJohnny Huang 
1527de6fbf1cSJohnny Huang 	otp_soak(0);
152869d5fd8fSJohnny Huang 	if (!pass)
15292a856b9aSJohnny Huang 		return OTP_FAILURE;
1530a6d0d645SJohnny Huang 
15312a856b9aSJohnny Huang 	return OTP_SUCCESS;
1532d90825e2SJohnny Huang 
153369d5fd8fSJohnny Huang }
153469d5fd8fSJohnny Huang 
1535eda10d61SJohnny Huang static int otp_strap_bit_confirm(struct otpstrap_status *otpstrap, int offset, int ibit, int bit, int pbit, int rpbit)
1536eda10d61SJohnny Huang {
1537eda10d61SJohnny Huang 	if (ibit == 1) {
1538eda10d61SJohnny Huang 		return OTP_SUCCESS;
1539eda10d61SJohnny Huang 	} else {
1540eda10d61SJohnny Huang 		printf("OTPSTRAP[%X]:\n", offset);
1541eda10d61SJohnny Huang 	}
1542eda10d61SJohnny Huang 	if (bit == otpstrap->value) {
1543eda10d61SJohnny Huang 		printf("    The value is same as before, skip it.\n");
1544eda10d61SJohnny Huang 		return OTP_PROG_SKIP;
1545eda10d61SJohnny Huang 	}
1546eda10d61SJohnny Huang 	if (otpstrap->protected == 1) {
1547eda10d61SJohnny Huang 		printf("    This bit is protected and is not writable\n");
1548eda10d61SJohnny Huang 		return OTP_FAILURE;
1549eda10d61SJohnny Huang 	}
1550eda10d61SJohnny Huang 	if (otpstrap->remain_times == 0) {
1551eda10d61SJohnny Huang 		printf("    This bit is no remaining times to write.\n");
1552eda10d61SJohnny Huang 		return OTP_FAILURE;
1553eda10d61SJohnny Huang 	}
1554eda10d61SJohnny Huang 	if (pbit == 1) {
1555eda10d61SJohnny Huang 		printf("    This bit will be protected and become non-writable.\n");
1556eda10d61SJohnny Huang 	}
1557eda10d61SJohnny Huang 	if (rpbit == 1 && info_cb.version != OTP_AST2600A0) {
1558eda10d61SJohnny Huang 		printf("    The relative register will be protected.\n");
1559eda10d61SJohnny Huang 	}
1560eda10d61SJohnny 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);
1561eda10d61SJohnny Huang 	return OTP_SUCCESS;
1562eda10d61SJohnny Huang }
1563eda10d61SJohnny Huang 
15645010032bSJohnny Huang static int otp_strap_image_confirm(struct otp_image_layout *image_layout)
156569d5fd8fSJohnny Huang {
156669d5fd8fSJohnny Huang 	int i;
15675010032bSJohnny Huang 	uint32_t *strap;
15685010032bSJohnny Huang 	uint32_t *strap_ignore;
15695010032bSJohnny Huang 	uint32_t *strap_reg_protect;
15705010032bSJohnny Huang 	uint32_t *strap_pro;
1571eda10d61SJohnny Huang 	int bit, pbit, ibit, rpbit;
157269d5fd8fSJohnny Huang 	int fail = 0;
1573a6af4a17SJohnny Huang 	int skip = -1;
1574eda10d61SJohnny Huang 	int ret;
157566f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
157669d5fd8fSJohnny Huang 
15775010032bSJohnny Huang 	strap = (uint32_t *)image_layout->strap;
15785010032bSJohnny Huang 	strap_pro = (uint32_t *)image_layout->strap_pro;
15795010032bSJohnny Huang 	strap_ignore = (uint32_t *)image_layout->strap_ignore;
15805010032bSJohnny Huang 	strap_reg_protect = (uint32_t *)image_layout->strap_reg_pro;
15815010032bSJohnny Huang 
1582541eb887SJohnny Huang 	otp_strap_status(otpstrap);
158369d5fd8fSJohnny Huang 	for (i = 0; i < 64; i++) {
158469d5fd8fSJohnny Huang 		if (i < 32) {
15855010032bSJohnny Huang 			bit = (strap[0] >> i) & 0x1;
1586eda10d61SJohnny Huang 			ibit = (strap_ignore[0] >> i) & 0x1;
15875010032bSJohnny Huang 			pbit = (strap_pro[0] >> i) & 0x1;
158869d5fd8fSJohnny Huang 		} else {
15895010032bSJohnny Huang 			bit = (strap[1] >> (i - 32)) & 0x1;
1590eda10d61SJohnny Huang 			ibit = (strap_ignore[1] >> (i - 32)) & 0x1;
15915010032bSJohnny Huang 			pbit = (strap_pro[1] >> (i - 32)) & 0x1;
15925010032bSJohnny Huang 		}
15935010032bSJohnny Huang 
15945010032bSJohnny Huang 		if (info_cb.version != OTP_AST2600A0) {
15955010032bSJohnny Huang 			if (i < 32) {
15965010032bSJohnny Huang 				rpbit = (strap_reg_protect[0] >> i) & 0x1;
15975010032bSJohnny Huang 			} else {
15985010032bSJohnny Huang 				rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1;
15995010032bSJohnny Huang 			}
16005010032bSJohnny Huang 		} else {
16015010032bSJohnny Huang 			rpbit = 0;
160269d5fd8fSJohnny Huang 		}
1603eda10d61SJohnny Huang 		ret = otp_strap_bit_confirm(&otpstrap[i], i, ibit, bit, pbit, rpbit);
1604eda10d61SJohnny Huang 		if (ret == OTP_PROG_SKIP) {
1605a6af4a17SJohnny Huang 			if (skip == -1)
1606a6af4a17SJohnny Huang 				skip = 1;
160769d5fd8fSJohnny Huang 			continue;
1608a6af4a17SJohnny Huang 		} else {
1609eda10d61SJohnny Huang 			skip = 1;
161069d5fd8fSJohnny Huang 		}
1611eda10d61SJohnny Huang 
1612eda10d61SJohnny Huang 		if (ret == OTP_FAILURE)
161369d5fd8fSJohnny Huang 			fail = 1;
161469d5fd8fSJohnny Huang 	}
161569d5fd8fSJohnny Huang 	if (fail == 1)
1616a6af4a17SJohnny Huang 		return OTP_FAILURE;
1617a6af4a17SJohnny Huang 	else if (skip == 1)
1618a6af4a17SJohnny Huang 		return OTP_PROG_SKIP;
16197e22f42dSJohnny Huang 
1620eda10d61SJohnny Huang 	return OTP_SUCCESS;
162169d5fd8fSJohnny Huang }
162269d5fd8fSJohnny Huang 
16232a856b9aSJohnny Huang static int otp_print_strap(int start, int count)
162469d5fd8fSJohnny Huang {
162569d5fd8fSJohnny Huang 	int i, j;
1626de6b0cc4SJohnny Huang 	int remains;
162766f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
162869d5fd8fSJohnny Huang 
16292a856b9aSJohnny Huang 	if (start < 0 || start > 64)
16302a856b9aSJohnny Huang 		return OTP_USAGE;
16312a856b9aSJohnny Huang 
16322a856b9aSJohnny Huang 	if ((start + count) < 0 || (start + count) > 64)
16332a856b9aSJohnny Huang 		return OTP_USAGE;
16342a856b9aSJohnny Huang 
1635541eb887SJohnny Huang 	otp_strap_status(otpstrap);
163669d5fd8fSJohnny Huang 
1637de6b0cc4SJohnny Huang 	if (info_cb.version == OTP_AST2600A0) {
1638de6b0cc4SJohnny Huang 		remains = 7;
163907baa4e8SJohnny Huang 		printf("BIT(hex)  Value  Option           Status\n");
1640de6b0cc4SJohnny Huang 	} else {
1641de6b0cc4SJohnny Huang 		remains = 6;
1642de6b0cc4SJohnny Huang 		printf("BIT(hex)  Value  Option         Reg_Protect Status\n");
1643de6b0cc4SJohnny Huang 	}
1644de6b0cc4SJohnny Huang 	printf("______________________________________________________________________________\n");
1645737ed20bSJohnny Huang 
1646cd1610b4SJohnny Huang 	for (i = start; i < start + count; i++) {
164707baa4e8SJohnny Huang 		printf("0x%-8X", i);
1648737ed20bSJohnny Huang 		printf("%-7d", otpstrap[i].value);
1649de6b0cc4SJohnny Huang 		for (j = 0; j < remains; j++)
1650737ed20bSJohnny Huang 			printf("%d ", otpstrap[i].option_array[j]);
1651737ed20bSJohnny Huang 		printf("   ");
1652de6b0cc4SJohnny Huang 		if (info_cb.version != OTP_AST2600A0) {
1653de6b0cc4SJohnny Huang 			printf("%d           ", otpstrap[i].reg_protected);
1654de6b0cc4SJohnny Huang 		}
165569d5fd8fSJohnny Huang 		if (otpstrap[i].protected == 1) {
1656737ed20bSJohnny Huang 			printf("protected and not writable");
165769d5fd8fSJohnny Huang 		} else {
1658737ed20bSJohnny Huang 			printf("not protected ");
165969d5fd8fSJohnny Huang 			if (otpstrap[i].remain_times == 0) {
1660737ed20bSJohnny Huang 				printf("and no remaining times to write.");
166169d5fd8fSJohnny Huang 			} else {
1662737ed20bSJohnny Huang 				printf("and still can write %d times", otpstrap[i].remain_times);
166369d5fd8fSJohnny Huang 			}
166469d5fd8fSJohnny Huang 		}
1665737ed20bSJohnny Huang 		printf("\n");
166669d5fd8fSJohnny Huang 	}
16672a856b9aSJohnny Huang 
16682a856b9aSJohnny Huang 	return OTP_SUCCESS;
166969d5fd8fSJohnny Huang }
167069d5fd8fSJohnny Huang 
16718848d5dcSJohnny Huang static int otp_prog_strap_bit(int bit_offset, int value)
16728848d5dcSJohnny Huang {
16738848d5dcSJohnny Huang 	struct otpstrap_status otpstrap[64];
167483655e91SJohnny Huang 	uint32_t prog_address;
16758848d5dcSJohnny Huang 	int offset;
16768848d5dcSJohnny Huang 	int ret;
16778848d5dcSJohnny Huang 
16788848d5dcSJohnny Huang 
16798848d5dcSJohnny Huang 	otp_strap_status(otpstrap);
16808848d5dcSJohnny Huang 
16818848d5dcSJohnny Huang 	ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0, 0);
16828848d5dcSJohnny Huang 
16838848d5dcSJohnny Huang 	if (ret != OTP_SUCCESS) {
16848848d5dcSJohnny Huang 		return ret;
16858848d5dcSJohnny Huang 	}
16868848d5dcSJohnny Huang 
16878848d5dcSJohnny Huang 	prog_address = 0x800;
16888848d5dcSJohnny Huang 	if (bit_offset < 32) {
16898848d5dcSJohnny Huang 		offset = bit_offset;
16908848d5dcSJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) / 8) * 0x200;
16918848d5dcSJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) % 8) * 0x2;
16928848d5dcSJohnny Huang 
16938848d5dcSJohnny Huang 	} else {
16948848d5dcSJohnny Huang 		offset = (bit_offset - 32);
16958848d5dcSJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) / 8) * 0x200;
16968848d5dcSJohnny Huang 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) % 8) * 0x2;
16978848d5dcSJohnny Huang 	}
16988848d5dcSJohnny Huang 
16998848d5dcSJohnny Huang 
170083655e91SJohnny Huang 	return otp_prog_bit(1, prog_address, offset);
17018848d5dcSJohnny Huang }
17028848d5dcSJohnny Huang 
17035010032bSJohnny Huang static int otp_prog_strap(struct otp_image_layout *image_layout)
170469d5fd8fSJohnny Huang {
17055010032bSJohnny Huang 	uint32_t *strap;
17065010032bSJohnny Huang 	uint32_t *strap_ignore;
17075010032bSJohnny Huang 	uint32_t *strap_pro;
17085010032bSJohnny Huang 	uint32_t *strap_reg_protect;
170983655e91SJohnny Huang 	uint32_t prog_address;
171083655e91SJohnny Huang 	int i;
1711eda10d61SJohnny Huang 	int bit, pbit, ibit, offset, rpbit;
171269d5fd8fSJohnny Huang 	int fail = 0;
171383655e91SJohnny Huang 	int ret;
171466f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
171569d5fd8fSJohnny Huang 
17165010032bSJohnny Huang 	strap = (uint32_t *)image_layout->strap;
17175010032bSJohnny Huang 	strap_pro = (uint32_t *)image_layout->strap_pro;
17185010032bSJohnny Huang 	strap_ignore = (uint32_t *)image_layout->strap_ignore;
17195010032bSJohnny Huang 	strap_reg_protect = (uint32_t *)image_layout->strap_reg_pro;
17205010032bSJohnny Huang 
17217f795e57SJohnny Huang 	printf("Read OTP Strap Region:\n");
1722541eb887SJohnny Huang 	otp_strap_status(otpstrap);
172369d5fd8fSJohnny Huang 
17247f795e57SJohnny Huang 	printf("Check writable...\n");
17255010032bSJohnny Huang 	if (otp_strap_image_confirm(image_layout) == OTP_FAILURE) {
17267f795e57SJohnny Huang 		printf("Input image can't program into OTP, please check.\n");
17277f795e57SJohnny Huang 		return OTP_FAILURE;
17287f795e57SJohnny Huang 	}
17297e22f42dSJohnny Huang 
173069d5fd8fSJohnny Huang 	for (i = 0; i < 64; i++) {
173169d5fd8fSJohnny Huang 		prog_address = 0x800;
173269d5fd8fSJohnny Huang 		if (i < 32) {
173369d5fd8fSJohnny Huang 			offset = i;
17345010032bSJohnny Huang 			bit = (strap[0] >> offset) & 0x1;
1735eda10d61SJohnny Huang 			ibit = (strap_ignore[0] >> offset) & 0x1;
17365010032bSJohnny Huang 			pbit = (strap_pro[0] >> offset) & 0x1;
173769d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200;
173869d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2;
173969d5fd8fSJohnny Huang 
174069d5fd8fSJohnny Huang 		} else {
174169d5fd8fSJohnny Huang 			offset = (i - 32);
17425010032bSJohnny Huang 			bit = (strap[1] >> offset) & 0x1;
1743eda10d61SJohnny Huang 			ibit = (strap_ignore[1] >> offset) & 0x1;
17445010032bSJohnny Huang 			pbit = (strap_pro[1] >> offset) & 0x1;
174569d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200;
174669d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2;
174769d5fd8fSJohnny Huang 		}
17485010032bSJohnny Huang 		if (info_cb.version != OTP_AST2600A0) {
17495010032bSJohnny Huang 			if (i < 32) {
17505010032bSJohnny Huang 				rpbit = (strap_reg_protect[0] >> i) & 0x1;
17515010032bSJohnny Huang 			} else {
17525010032bSJohnny Huang 				rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1;
17535010032bSJohnny Huang 			}
17545010032bSJohnny Huang 		} else {
17555010032bSJohnny Huang 			rpbit = 0;
17565010032bSJohnny Huang 		}
175769d5fd8fSJohnny Huang 
1758eda10d61SJohnny Huang 		if (ibit == 1) {
175969d5fd8fSJohnny Huang 			continue;
176069d5fd8fSJohnny Huang 		}
176169d5fd8fSJohnny Huang 		if (bit == otpstrap[i].value) {
176269d5fd8fSJohnny Huang 			continue;
176369d5fd8fSJohnny Huang 		}
176469d5fd8fSJohnny Huang 		if (otpstrap[i].protected == 1) {
176569d5fd8fSJohnny Huang 			fail = 1;
176669d5fd8fSJohnny Huang 			continue;
176769d5fd8fSJohnny Huang 		}
176869d5fd8fSJohnny Huang 		if (otpstrap[i].remain_times == 0) {
176969d5fd8fSJohnny Huang 			fail = 1;
177069d5fd8fSJohnny Huang 			continue;
177169d5fd8fSJohnny Huang 		}
17727e22f42dSJohnny Huang 
177383655e91SJohnny Huang 		ret = otp_prog_bit(1, prog_address, offset);
177483655e91SJohnny Huang 		if (!ret)
17752a856b9aSJohnny Huang 			return OTP_FAILURE;
177669d5fd8fSJohnny Huang 
17775010032bSJohnny Huang 		if (rpbit == 1 && info_cb.version != OTP_AST2600A0) {
177869d5fd8fSJohnny Huang 			prog_address = 0x800;
177969d5fd8fSJohnny Huang 			if (i < 32)
17805010032bSJohnny Huang 				prog_address |= 0x608;
178169d5fd8fSJohnny Huang 			else
17825010032bSJohnny Huang 				prog_address |= 0x60a;
17837e22f42dSJohnny Huang 
178483655e91SJohnny Huang 			ret = otp_prog_bit(1, prog_address, offset);
178583655e91SJohnny Huang 			if (!ret)
17862a856b9aSJohnny Huang 				return OTP_FAILURE;
17875010032bSJohnny Huang 		}
17885010032bSJohnny Huang 
17895010032bSJohnny Huang 		if (pbit != 0) {
17905010032bSJohnny Huang 			prog_address = 0x800;
17915010032bSJohnny Huang 			if (i < 32)
17925010032bSJohnny Huang 				prog_address |= 0x60c;
17935010032bSJohnny Huang 			else
17945010032bSJohnny Huang 				prog_address |= 0x60e;
17955010032bSJohnny Huang 
179683655e91SJohnny Huang 			ret = otp_prog_bit(1, prog_address, offset);
179783655e91SJohnny Huang 			if (!ret)
17985010032bSJohnny Huang 				return OTP_FAILURE;
17995010032bSJohnny Huang 		}
180069d5fd8fSJohnny Huang 
180169d5fd8fSJohnny Huang 	}
1802de6fbf1cSJohnny Huang 	otp_soak(0);
180369d5fd8fSJohnny Huang 	if (fail == 1)
18042a856b9aSJohnny Huang 		return OTP_FAILURE;
180569d5fd8fSJohnny Huang 	else
18062a856b9aSJohnny Huang 		return OTP_SUCCESS;
180769d5fd8fSJohnny Huang 
180869d5fd8fSJohnny Huang }
180969d5fd8fSJohnny Huang 
18105010032bSJohnny Huang static int otp_prog_data(struct otp_image_layout *image_layout)
18114c1c9b35SJohnny Huang {
181254552c69SJohnny Huang 	int i;
181354552c69SJohnny Huang 	int ret;
18145010032bSJohnny Huang 	int data_dw;
1815d90825e2SJohnny Huang 	uint32_t data[2048];
18165010032bSJohnny Huang 	uint32_t *buf;
18175010032bSJohnny Huang 	uint32_t *buf_ignore;
18184c1c9b35SJohnny Huang 
181954552c69SJohnny Huang 	uint32_t data_masked;
182054552c69SJohnny Huang 	uint32_t buf_masked;
18214c1c9b35SJohnny Huang 
18225010032bSJohnny Huang 	buf = (uint32_t *)image_layout->data;
18235010032bSJohnny Huang 	buf_ignore = (uint32_t *)image_layout->data_ignore;
18245010032bSJohnny Huang 
18255010032bSJohnny Huang 	data_dw = image_layout->data_length / 4;
18265010032bSJohnny Huang 
18274c1c9b35SJohnny Huang 	printf("Read OTP Data:\n");
18284c1c9b35SJohnny Huang 
18295010032bSJohnny Huang 	for (i = 0; i < data_dw - 2 ; i += 2) {
1830d90825e2SJohnny Huang 		otp_read_data(i, &data[i]);
18314c1c9b35SJohnny Huang 	}
1832d90825e2SJohnny Huang 
18334c1c9b35SJohnny Huang 	printf("Check writable...\n");
183454552c69SJohnny Huang 	// ignore last two dw, the last two dw is used for slt otp write check.
18355010032bSJohnny Huang 	for (i = 0; i < data_dw - 2; i++) {
1836696656c6SJohnny Huang 		data_masked = data[i]  & ~buf_ignore[i];
1837696656c6SJohnny Huang 		buf_masked  = buf[i] & ~buf_ignore[i];
183854552c69SJohnny Huang 		if (data_masked == buf_masked)
18394c1c9b35SJohnny Huang 			continue;
1840d90825e2SJohnny Huang 		if (i % 2 == 0) {
184154552c69SJohnny Huang 			if ((data_masked | buf_masked) == buf_masked) {
18424c1c9b35SJohnny Huang 				continue;
18434c1c9b35SJohnny Huang 			} else {
18444c1c9b35SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
1845d90825e2SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
18464c1c9b35SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
1847696656c6SJohnny Huang 				printf("Mask    [%x] = %x\n", i, ~buf_ignore[i]);
18482a856b9aSJohnny Huang 				return OTP_FAILURE;
184969d5fd8fSJohnny Huang 			}
1850d90825e2SJohnny Huang 		} else {
185154552c69SJohnny Huang 			if ((data_masked & buf_masked) == buf_masked) {
1852d90825e2SJohnny Huang 				continue;
1853d90825e2SJohnny Huang 			} else {
1854d90825e2SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
1855d90825e2SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
1856d90825e2SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
1857696656c6SJohnny Huang 				printf("Mask    [%x] = %x\n", i, ~buf_ignore[i]);
18582a856b9aSJohnny Huang 				return OTP_FAILURE;
1859d90825e2SJohnny Huang 			}
1860d90825e2SJohnny Huang 		}
1861d90825e2SJohnny Huang 	}
186269d5fd8fSJohnny Huang 
1863d90825e2SJohnny Huang 	printf("Start Programing...\n");
1864d90825e2SJohnny Huang 
186554552c69SJohnny Huang 	// programing ecc region first
186654552c69SJohnny Huang 	for (i = 1792; i < 2046; i += 2) {
1867696656c6SJohnny Huang 		ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i);
186854552c69SJohnny Huang 		if (ret != OTP_SUCCESS) {
186954552c69SJohnny Huang 			printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n",
1870696656c6SJohnny Huang 			       i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]);
187154552c69SJohnny Huang 			return ret;
1872d90825e2SJohnny Huang 		}
1873d90825e2SJohnny Huang 	}
1874d90825e2SJohnny Huang 
187554552c69SJohnny Huang 	for (i = 0; i < 1792; i += 2) {
1876696656c6SJohnny Huang 		ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i);
187754552c69SJohnny Huang 		if (ret != OTP_SUCCESS) {
187854552c69SJohnny Huang 			printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n",
1879696656c6SJohnny Huang 			       i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]);
188054552c69SJohnny Huang 			return ret;
1881d90825e2SJohnny Huang 		}
1882de6fbf1cSJohnny Huang 	}
1883de6fbf1cSJohnny Huang 	otp_soak(0);
18842a856b9aSJohnny Huang 	return OTP_SUCCESS;
1885d90825e2SJohnny Huang 
1886d90825e2SJohnny Huang }
1887d90825e2SJohnny Huang 
1888696656c6SJohnny Huang static int otp_image_verify(uint8_t *src_buf, uint32_t length, uint8_t *digest_buf)
1889696656c6SJohnny Huang {
1890696656c6SJohnny Huang 	sha256_context ctx;
1891696656c6SJohnny Huang 	u8 digest_ret[CHECKSUM_LEN];
1892696656c6SJohnny Huang 
1893696656c6SJohnny Huang 	sha256_starts(&ctx);
1894696656c6SJohnny Huang 	sha256_update(&ctx, src_buf, length);
1895696656c6SJohnny Huang 	sha256_finish(&ctx, digest_ret);
1896696656c6SJohnny Huang 
1897696656c6SJohnny Huang 	if (!memcmp(digest_buf, digest_ret, CHECKSUM_LEN))
1898696656c6SJohnny Huang 		return 0;
1899696656c6SJohnny Huang 	else
1900696656c6SJohnny Huang 		return -1;
1901696656c6SJohnny Huang 
1902696656c6SJohnny Huang }
1903696656c6SJohnny Huang 
1904de6b0cc4SJohnny Huang static int do_otp_prog(int addr, int nconfirm)
190569d5fd8fSJohnny Huang {
190669d5fd8fSJohnny Huang 	int ret;
19079a4fe690SJohnny Huang 	int image_version = 0;
1908696656c6SJohnny Huang 	struct otp_header *otp_header;
1909696656c6SJohnny Huang 	struct otp_image_layout image_layout;
1910696656c6SJohnny Huang 	int image_size;
1911696656c6SJohnny Huang 	uint8_t *buf;
1912696656c6SJohnny Huang 	uint8_t *checksum;
191369d5fd8fSJohnny Huang 
1914696656c6SJohnny Huang 	otp_header = map_physmem(addr, sizeof(struct otp_header), MAP_WRBACK);
1915696656c6SJohnny Huang 	if (!otp_header) {
191669d5fd8fSJohnny Huang 		puts("Failed to map physical memory\n");
19172a856b9aSJohnny Huang 		return OTP_FAILURE;
191869d5fd8fSJohnny Huang 	}
1919d90825e2SJohnny Huang 
1920696656c6SJohnny Huang 	image_size = OTP_IMAGE_SIZE(otp_header->image_info);
1921696656c6SJohnny Huang 	unmap_physmem(otp_header, MAP_WRBACK);
1922696656c6SJohnny Huang 
1923696656c6SJohnny Huang 	buf = map_physmem(addr, image_size + CHECKSUM_LEN, MAP_WRBACK);
1924696656c6SJohnny Huang 
1925696656c6SJohnny Huang 	if (!buf) {
1926696656c6SJohnny Huang 		puts("Failed to map physical memory\n");
1927696656c6SJohnny Huang 		return OTP_FAILURE;
1928696656c6SJohnny Huang 	}
1929696656c6SJohnny Huang 	otp_header = (struct otp_header *) buf;
1930696656c6SJohnny Huang 	checksum = buf + otp_header->checksum_offset;
1931696656c6SJohnny Huang 
1932696656c6SJohnny Huang 	if (strcmp(OTP_MAGIC, (char *)otp_header->otp_magic) != 0) {
1933696656c6SJohnny Huang 		puts("Image is invalid\n");
1934696656c6SJohnny Huang 		return OTP_FAILURE;
1935696656c6SJohnny Huang 	}
1936696656c6SJohnny Huang 
1937696656c6SJohnny Huang 
19385010032bSJohnny Huang 	image_layout.data_length = (int)(OTP_REGION_SIZE(otp_header->data_info) / 2);
19395010032bSJohnny Huang 	image_layout.data = buf + OTP_REGION_OFFSET(otp_header->data_info);
19405010032bSJohnny Huang 	image_layout.data_ignore = image_layout.data + image_layout.data_length;
19415010032bSJohnny Huang 
19425010032bSJohnny Huang 	image_layout.conf_length = (int)(OTP_REGION_SIZE(otp_header->config_info) / 2);
1943696656c6SJohnny Huang 	image_layout.conf = buf + OTP_REGION_OFFSET(otp_header->config_info);
19445010032bSJohnny Huang 	image_layout.conf_ignore = image_layout.conf + image_layout.conf_length;
1945696656c6SJohnny Huang 
1946696656c6SJohnny Huang 	image_layout.strap = buf + OTP_REGION_OFFSET(otp_header->strap_info);
1947696656c6SJohnny Huang 
1948696656c6SJohnny Huang 	if (!strcmp("A0", (char *)otp_header->otp_version)) {
1949696656c6SJohnny Huang 		image_version = OTP_AST2600A0;
19505010032bSJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 3);
19515010032bSJohnny Huang 		image_layout.strap_pro = image_layout.strap + image_layout.strap_length;
19525010032bSJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 2 * image_layout.strap_length;
1953696656c6SJohnny Huang 	} else if (!strcmp("A1", (char *)otp_header->otp_version)) {
1954696656c6SJohnny Huang 		image_version = OTP_AST2600A1;
19555010032bSJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4);
19565010032bSJohnny Huang 		image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length;
19575010032bSJohnny Huang 		image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length;
19585010032bSJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length;
1959696656c6SJohnny Huang 	} else {
1960696656c6SJohnny Huang 		puts("Version is not supported\n");
1961696656c6SJohnny Huang 		return OTP_FAILURE;
1962696656c6SJohnny Huang 	}
1963696656c6SJohnny Huang 
19649a4fe690SJohnny Huang 	if (image_version != info_cb.version) {
19659a4fe690SJohnny Huang 		puts("Version is not match\n");
19669a4fe690SJohnny Huang 		return OTP_FAILURE;
19679a4fe690SJohnny Huang 	}
19689a4fe690SJohnny Huang 
1969696656c6SJohnny Huang 	if (otp_image_verify(buf, image_size, checksum)) {
1970696656c6SJohnny Huang 		puts("checksum is invalid\n");
1971696656c6SJohnny Huang 		return OTP_FAILURE;
1972d90825e2SJohnny Huang 	}
19737332532cSJohnny Huang 
197469d5fd8fSJohnny Huang 	if (!nconfirm) {
1975696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_DATA) {
19767f795e57SJohnny Huang 			printf("\nOTP data region :\n");
1977696656c6SJohnny Huang 			if (otp_print_data_info(&image_layout) < 0) {
197869d5fd8fSJohnny Huang 				printf("OTP data error, please check.\n");
19792a856b9aSJohnny Huang 				return OTP_FAILURE;
198069d5fd8fSJohnny Huang 			}
198169d5fd8fSJohnny Huang 		}
1982696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_STRAP) {
19837332532cSJohnny Huang 			printf("\nOTP strap region :\n");
19845010032bSJohnny Huang 			if (otp_print_strap_image(&image_layout) < 0) {
19857332532cSJohnny Huang 				printf("OTP strap error, please check.\n");
19867332532cSJohnny Huang 				return OTP_FAILURE;
19877332532cSJohnny Huang 			}
19887332532cSJohnny Huang 		}
1989696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_CONFIG) {
19907332532cSJohnny Huang 			printf("\nOTP configuration region :\n");
1991696656c6SJohnny Huang 			if (otp_print_conf_image(&image_layout) < 0) {
19927332532cSJohnny Huang 				printf("OTP config error, please check.\n");
19937332532cSJohnny Huang 				return OTP_FAILURE;
19947332532cSJohnny Huang 			}
19957332532cSJohnny Huang 		}
19967332532cSJohnny Huang 
199769d5fd8fSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
199869d5fd8fSJohnny Huang 		if (!confirm_yesno()) {
199969d5fd8fSJohnny Huang 			printf(" Aborting\n");
20002a856b9aSJohnny Huang 			return OTP_FAILURE;
200169d5fd8fSJohnny Huang 		}
200269d5fd8fSJohnny Huang 	}
20037332532cSJohnny Huang 
20045010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_DATA) {
20055010032bSJohnny Huang 		printf("programing data region ...\n");
20065010032bSJohnny Huang 		ret = otp_prog_data(&image_layout);
20075010032bSJohnny Huang 		if (ret != 0) {
20085010032bSJohnny Huang 			printf("Error\n");
20095010032bSJohnny Huang 			return ret;
20105010032bSJohnny Huang 		} else {
20115010032bSJohnny Huang 			printf("Done\n");
20125010032bSJohnny Huang 		}
20135010032bSJohnny Huang 	}
20145010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_STRAP) {
20155010032bSJohnny Huang 		printf("programing strap region ...\n");
20165010032bSJohnny Huang 		ret = otp_prog_strap(&image_layout);
20175010032bSJohnny Huang 		if (ret != 0) {
20185010032bSJohnny Huang 			printf("Error\n");
20195010032bSJohnny Huang 			return ret;
20205010032bSJohnny Huang 		} else {
20215010032bSJohnny Huang 			printf("Done\n");
20225010032bSJohnny Huang 		}
20235010032bSJohnny Huang 	}
20245010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_CONFIG) {
20255010032bSJohnny Huang 		printf("programing configuration region ...\n");
20265010032bSJohnny Huang 		ret = otp_prog_conf(&image_layout);
20275010032bSJohnny Huang 		if (ret != 0) {
20285010032bSJohnny Huang 			printf("Error\n");
20295010032bSJohnny Huang 			return ret;
20305010032bSJohnny Huang 		}
20315010032bSJohnny Huang 		printf("Done\n");
20325010032bSJohnny Huang 	}
2033cd1610b4SJohnny Huang 
20347332532cSJohnny Huang 	return OTP_SUCCESS;
20352a856b9aSJohnny Huang }
20362a856b9aSJohnny Huang 
20372a856b9aSJohnny Huang static int do_otp_prog_bit(int mode, int otp_dw_offset, int bit_offset, int value, int nconfirm)
2038cd1610b4SJohnny Huang {
2039a6af4a17SJohnny Huang 	uint32_t read[2];
2040d90825e2SJohnny Huang 	uint32_t prog_address = 0;
204166f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
2042cd1610b4SJohnny Huang 	int otp_bit;
204383655e91SJohnny Huang 	int ret = 0;
2044cd1610b4SJohnny Huang 
2045cd1610b4SJohnny Huang 	switch (mode) {
2046a6d0d645SJohnny Huang 	case OTP_REGION_CONF:
2047a6af4a17SJohnny Huang 		otp_read_config(otp_dw_offset, read);
2048cd1610b4SJohnny Huang 		prog_address = 0x800;
2049cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset / 8) * 0x200;
2050cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset % 8) * 0x2;
2051a6af4a17SJohnny Huang 		otp_bit = (read[0] >> bit_offset) & 0x1;
2052cd1610b4SJohnny Huang 		if (otp_bit == value) {
2053a6af4a17SJohnny Huang 			printf("OTPCFG%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
2054cd1610b4SJohnny Huang 			printf("No need to program\n");
20552a856b9aSJohnny Huang 			return OTP_SUCCESS;
2056cd1610b4SJohnny Huang 		}
2057cd1610b4SJohnny Huang 		if (otp_bit == 1 && value == 0) {
2058a6af4a17SJohnny Huang 			printf("OTPCFG%X[%X] = 1\n", otp_dw_offset, bit_offset);
2059cd1610b4SJohnny Huang 			printf("OTP is programed, which can't be clean\n");
20602a856b9aSJohnny Huang 			return OTP_FAILURE;
2061cd1610b4SJohnny Huang 		}
2062a6af4a17SJohnny Huang 		printf("Program OTPCFG%X[%X] to 1\n", otp_dw_offset, bit_offset);
2063cd1610b4SJohnny Huang 		break;
2064a6d0d645SJohnny Huang 	case OTP_REGION_DATA:
2065cd1610b4SJohnny Huang 		prog_address = otp_dw_offset;
2066cd1610b4SJohnny Huang 
2067cd1610b4SJohnny Huang 		if (otp_dw_offset % 2 == 0) {
2068a6af4a17SJohnny Huang 			otp_read_data(otp_dw_offset, read);
2069a6af4a17SJohnny Huang 			otp_bit = (read[0] >> bit_offset) & 0x1;
2070643b9cfdSJohnny Huang 
2071643b9cfdSJohnny Huang 			if (otp_bit == 1 && value == 0) {
2072643b9cfdSJohnny Huang 				printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
2073643b9cfdSJohnny Huang 				printf("OTP is programed, which can't be cleaned\n");
2074643b9cfdSJohnny Huang 				return OTP_FAILURE;
2075643b9cfdSJohnny Huang 			}
2076cd1610b4SJohnny Huang 		} else {
2077a6af4a17SJohnny Huang 			otp_read_data(otp_dw_offset - 1, read);
2078a6af4a17SJohnny Huang 			otp_bit = (read[1] >> bit_offset) & 0x1;
2079643b9cfdSJohnny Huang 
2080643b9cfdSJohnny Huang 			if (otp_bit == 0 && value == 1) {
2081643b9cfdSJohnny Huang 				printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
2082643b9cfdSJohnny Huang 				printf("OTP is programed, which can't be writen\n");
2083643b9cfdSJohnny Huang 				return OTP_FAILURE;
2084643b9cfdSJohnny Huang 			}
2085cd1610b4SJohnny Huang 		}
2086cd1610b4SJohnny Huang 		if (otp_bit == value) {
2087a6af4a17SJohnny Huang 			printf("OTPDATA%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
2088cd1610b4SJohnny Huang 			printf("No need to program\n");
20892a856b9aSJohnny Huang 			return OTP_SUCCESS;
2090cd1610b4SJohnny Huang 		}
2091643b9cfdSJohnny Huang 
2092a6af4a17SJohnny Huang 		printf("Program OTPDATA%X[%X] to 1\n", otp_dw_offset, bit_offset);
2093cd1610b4SJohnny Huang 		break;
2094a6d0d645SJohnny Huang 	case OTP_REGION_STRAP:
20958848d5dcSJohnny Huang 		otp_strap_status(otpstrap);
20968848d5dcSJohnny Huang 		otp_print_strap(bit_offset, 1);
20978848d5dcSJohnny Huang 		ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0, 0);
20988848d5dcSJohnny Huang 		if (ret == OTP_FAILURE)
20998848d5dcSJohnny Huang 			return OTP_FAILURE;
21008848d5dcSJohnny Huang 		else if (ret == OTP_PROG_SKIP)
21018848d5dcSJohnny Huang 			return OTP_SUCCESS;
2102a6af4a17SJohnny Huang 
2103cd1610b4SJohnny Huang 		break;
2104cd1610b4SJohnny Huang 	}
2105cd1610b4SJohnny Huang 
2106cd1610b4SJohnny Huang 	if (!nconfirm) {
2107cd1610b4SJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
2108cd1610b4SJohnny Huang 		if (!confirm_yesno()) {
2109cd1610b4SJohnny Huang 			printf(" Aborting\n");
21102a856b9aSJohnny Huang 			return OTP_FAILURE;
2111cd1610b4SJohnny Huang 		}
2112cd1610b4SJohnny Huang 	}
2113cd1610b4SJohnny Huang 
2114cd1610b4SJohnny Huang 	switch (mode) {
2115a6d0d645SJohnny Huang 	case OTP_REGION_STRAP:
211683655e91SJohnny Huang 		ret =  otp_prog_strap_bit(bit_offset, value);
211783655e91SJohnny Huang 		break;
2118a6d0d645SJohnny Huang 	case OTP_REGION_CONF:
2119a6d0d645SJohnny Huang 	case OTP_REGION_DATA:
212083655e91SJohnny Huang 		ret = otp_prog_bit(value, prog_address, bit_offset);
2121de6fbf1cSJohnny Huang 		break;
2122de6fbf1cSJohnny Huang 	}
2123de6fbf1cSJohnny Huang 	otp_soak(0);
212483655e91SJohnny Huang 	if (ret) {
21259009c25dSJohnny Huang 		printf("SUCCESS\n");
21262a856b9aSJohnny Huang 		return OTP_SUCCESS;
21279009c25dSJohnny Huang 	} else {
21289009c25dSJohnny Huang 		printf("OTP cannot be programed\n");
21299009c25dSJohnny Huang 		printf("FAILED\n");
21309009c25dSJohnny Huang 		return OTP_FAILURE;
21319009c25dSJohnny Huang 	}
2132cd1610b4SJohnny Huang 
21332a856b9aSJohnny Huang 	return OTP_USAGE;
2134cd1610b4SJohnny Huang }
2135cd1610b4SJohnny Huang 
21362a856b9aSJohnny Huang static int do_otpread(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
213769d5fd8fSJohnny Huang {
21382a856b9aSJohnny Huang 	uint32_t offset, count;
21392a856b9aSJohnny Huang 	int ret;
214069d5fd8fSJohnny Huang 
21412a856b9aSJohnny Huang 	if (argc == 4) {
21422a856b9aSJohnny Huang 		offset = simple_strtoul(argv[2], NULL, 16);
21432a856b9aSJohnny Huang 		count = simple_strtoul(argv[3], NULL, 16);
21442a856b9aSJohnny Huang 	} else if (argc == 3) {
21452a856b9aSJohnny Huang 		offset = simple_strtoul(argv[2], NULL, 16);
21462a856b9aSJohnny Huang 		count = 1;
21472a856b9aSJohnny Huang 	} else {
214869d5fd8fSJohnny Huang 		return CMD_RET_USAGE;
214969d5fd8fSJohnny Huang 	}
215069d5fd8fSJohnny Huang 
215169d5fd8fSJohnny Huang 
21522a856b9aSJohnny Huang 	if (!strcmp(argv[1], "conf")) {
21533d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
21542a856b9aSJohnny Huang 		ret = otp_print_config(offset, count);
21552a856b9aSJohnny Huang 	} else if (!strcmp(argv[1], "data")) {
21563d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
21572a856b9aSJohnny Huang 		ret = otp_print_data(offset, count);
21582a856b9aSJohnny Huang 	} else if (!strcmp(argv[1], "strap")) {
21593d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
21602a856b9aSJohnny Huang 		ret = otp_print_strap(offset, count);
21612a856b9aSJohnny Huang 	} else {
21622a856b9aSJohnny Huang 		return CMD_RET_USAGE;
216369d5fd8fSJohnny Huang 	}
216469d5fd8fSJohnny Huang 
21652a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
21662a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
21672a856b9aSJohnny Huang 	else
21682a856b9aSJohnny Huang 		return CMD_RET_USAGE;
21692a856b9aSJohnny Huang 
21702a856b9aSJohnny Huang }
21712a856b9aSJohnny Huang 
21722a856b9aSJohnny Huang static int do_otpprog(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
21732a856b9aSJohnny Huang {
21742a856b9aSJohnny Huang 	phys_addr_t addr;
21752a856b9aSJohnny Huang 	int ret;
21762a856b9aSJohnny Huang 
2177de6b0cc4SJohnny Huang 	if (argc == 3) {
2178ed071a2bSJohnny Huang 		if (strcmp(argv[1], "o"))
21792a856b9aSJohnny Huang 			return CMD_RET_USAGE;
21802a856b9aSJohnny Huang 		addr = simple_strtoul(argv[2], NULL, 16);
21813d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2182de6b0cc4SJohnny Huang 		ret = do_otp_prog(addr, 1);
2183de6b0cc4SJohnny Huang 	} else if (argc == 2) {
21842a856b9aSJohnny Huang 		addr = simple_strtoul(argv[1], NULL, 16);
21853d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2186de6b0cc4SJohnny Huang 		ret = do_otp_prog(addr, 0);
21872a856b9aSJohnny Huang 	} else {
21882a856b9aSJohnny Huang 		return CMD_RET_USAGE;
21892a856b9aSJohnny Huang 	}
21902a856b9aSJohnny Huang 
21912a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
21922a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
21932a856b9aSJohnny Huang 	else if (ret == OTP_FAILURE)
21942a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
21952a856b9aSJohnny Huang 	else
21962a856b9aSJohnny Huang 		return CMD_RET_USAGE;
21972a856b9aSJohnny Huang }
21982a856b9aSJohnny Huang 
21992a856b9aSJohnny Huang static int do_otppb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
22002a856b9aSJohnny Huang {
22012a856b9aSJohnny Huang 	int mode = 0;
22022a856b9aSJohnny Huang 	int nconfirm = 0;
22032a856b9aSJohnny Huang 	int otp_addr = 0;
22042a856b9aSJohnny Huang 	int bit_offset;
22052a856b9aSJohnny Huang 	int value;
22062a856b9aSJohnny Huang 	int ret;
22072a856b9aSJohnny Huang 
22082a856b9aSJohnny Huang 	if (argc != 4 && argc != 5 && argc != 6)
22092a856b9aSJohnny Huang 		return CMD_RET_USAGE;
22102a856b9aSJohnny Huang 
22112a856b9aSJohnny Huang 	/* Drop the pb cmd */
22122a856b9aSJohnny Huang 	argc--;
22132a856b9aSJohnny Huang 	argv++;
22142a856b9aSJohnny Huang 
22152a856b9aSJohnny Huang 	if (!strcmp(argv[0], "conf"))
2216a6d0d645SJohnny Huang 		mode = OTP_REGION_CONF;
22172a856b9aSJohnny Huang 	else if (!strcmp(argv[0], "strap"))
2218a6d0d645SJohnny Huang 		mode = OTP_REGION_STRAP;
22192a856b9aSJohnny Huang 	else if (!strcmp(argv[0], "data"))
2220a6d0d645SJohnny Huang 		mode = OTP_REGION_DATA;
2221cd1610b4SJohnny Huang 	else
22222a856b9aSJohnny Huang 		return CMD_RET_USAGE;
22232a856b9aSJohnny Huang 
22242a856b9aSJohnny Huang 	/* Drop the region cmd */
22252a856b9aSJohnny Huang 	argc--;
22262a856b9aSJohnny Huang 	argv++;
22272a856b9aSJohnny Huang 
2228ed071a2bSJohnny Huang 	if (!strcmp(argv[0], "o")) {
2229cd1610b4SJohnny Huang 		nconfirm = 1;
22302a856b9aSJohnny Huang 		/* Drop the force option */
22312a856b9aSJohnny Huang 		argc--;
22322a856b9aSJohnny Huang 		argv++;
22332a856b9aSJohnny Huang 	}
2234cd1610b4SJohnny Huang 
2235a6d0d645SJohnny Huang 	if (mode == OTP_REGION_STRAP) {
22362a856b9aSJohnny Huang 		bit_offset = simple_strtoul(argv[0], NULL, 16);
22372a856b9aSJohnny Huang 		value = simple_strtoul(argv[1], NULL, 16);
22380808cc55SJohnny Huang 		if (bit_offset >= 64 || (value != 0 && value != 1))
22392a856b9aSJohnny Huang 			return CMD_RET_USAGE;
2240cd1610b4SJohnny Huang 	} else {
22412a856b9aSJohnny Huang 		otp_addr = simple_strtoul(argv[0], NULL, 16);
22422a856b9aSJohnny Huang 		bit_offset = simple_strtoul(argv[1], NULL, 16);
22432a856b9aSJohnny Huang 		value = simple_strtoul(argv[2], NULL, 16);
22440808cc55SJohnny Huang 		if (bit_offset >= 32 || (value != 0 && value != 1))
22452a856b9aSJohnny Huang 			return CMD_RET_USAGE;
22460808cc55SJohnny Huang 		if (mode == OTP_REGION_DATA) {
224778855207SJohnny Huang 			if (otp_addr >= 0x800)
22480808cc55SJohnny Huang 				return CMD_RET_USAGE;
22490808cc55SJohnny Huang 		} else {
225078855207SJohnny Huang 			if (otp_addr >= 0x20)
22510808cc55SJohnny Huang 				return CMD_RET_USAGE;
22520808cc55SJohnny Huang 		}
2253cd1610b4SJohnny Huang 	}
2254cd1610b4SJohnny Huang 	if (value != 0 && value != 1)
22552a856b9aSJohnny Huang 		return CMD_RET_USAGE;
2256cd1610b4SJohnny Huang 
22573d3688adSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
22582a856b9aSJohnny Huang 	ret = do_otp_prog_bit(mode, otp_addr, bit_offset, value, nconfirm);
22592a856b9aSJohnny Huang 
22602a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
22612a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
22622a856b9aSJohnny Huang 	else if (ret == OTP_FAILURE)
22632a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
22642a856b9aSJohnny Huang 	else
22652a856b9aSJohnny Huang 		return CMD_RET_USAGE;
22662a856b9aSJohnny Huang }
22672a856b9aSJohnny Huang 
22682a856b9aSJohnny Huang static int do_otpcmp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
22692a856b9aSJohnny Huang {
22702a856b9aSJohnny Huang 	phys_addr_t addr;
22712a856b9aSJohnny Huang 	int otp_addr = 0;
22722a856b9aSJohnny Huang 
22732a856b9aSJohnny Huang 	if (argc != 3)
22742a856b9aSJohnny Huang 		return CMD_RET_USAGE;
22752a856b9aSJohnny Huang 
22763d3688adSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
22772a856b9aSJohnny Huang 	addr = simple_strtoul(argv[1], NULL, 16);
22782a856b9aSJohnny Huang 	otp_addr = simple_strtoul(argv[2], NULL, 16);
22792a856b9aSJohnny Huang 	if (otp_compare(otp_addr, addr) == 0) {
228069d5fd8fSJohnny Huang 		printf("Compare pass\n");
22812a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
228269d5fd8fSJohnny Huang 	} else {
228369d5fd8fSJohnny Huang 		printf("Compare fail\n");
22842a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
228569d5fd8fSJohnny Huang 	}
228669d5fd8fSJohnny Huang }
228769d5fd8fSJohnny Huang 
228866f2f8e5SJohnny Huang static int do_otpinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
228966f2f8e5SJohnny Huang {
2290a8bd6d8cSJohnny Huang 	int view = 0;
22912d4b0742SJohnny Huang 	int input;
2292a8bd6d8cSJohnny Huang 
2293a8bd6d8cSJohnny Huang 	if (argc != 2 && argc != 3)
229466f2f8e5SJohnny Huang 		return CMD_RET_USAGE;
229566f2f8e5SJohnny Huang 
22962d4b0742SJohnny Huang 	if (!strcmp(argv[1], "conf")) {
229766f2f8e5SJohnny Huang 
22983d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
22992d4b0742SJohnny Huang 		if (argc == 3) {
23002d4b0742SJohnny Huang 			input = simple_strtoul(argv[2], NULL, 16);
23012d4b0742SJohnny Huang 			otp_print_conf_info(input);
23022d4b0742SJohnny Huang 		} else {
23032d4b0742SJohnny Huang 			otp_print_conf_info(-1);
23042d4b0742SJohnny Huang 		}
23052d4b0742SJohnny Huang 	} else if (!strcmp(argv[1], "strap")) {
23062d4b0742SJohnny Huang 		if (!strcmp(argv[2], "v")) {
2307a8bd6d8cSJohnny Huang 			view = 1;
2308a8bd6d8cSJohnny Huang 			/* Drop the view option */
2309a8bd6d8cSJohnny Huang 			argc--;
2310a8bd6d8cSJohnny Huang 			argv++;
2311a8bd6d8cSJohnny Huang 		}
23123d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2313b458cd62SJohnny Huang 		otp_print_strap_info(view);
231466f2f8e5SJohnny Huang 	} else {
231566f2f8e5SJohnny Huang 		return CMD_RET_USAGE;
231666f2f8e5SJohnny Huang 	}
23172d4b0742SJohnny Huang 
231866f2f8e5SJohnny Huang 	return CMD_RET_SUCCESS;
231966f2f8e5SJohnny Huang }
232066f2f8e5SJohnny Huang 
2321737ed20bSJohnny Huang static int do_otpprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2322737ed20bSJohnny Huang {
2323737ed20bSJohnny Huang 	int input;
2324737ed20bSJohnny Huang 	int bit_offset;
2325737ed20bSJohnny Huang 	int prog_address;
232683655e91SJohnny Huang 	int ret;
2327737ed20bSJohnny Huang 	if (argc != 3 && argc != 2)
2328737ed20bSJohnny Huang 		return CMD_RET_USAGE;
2329737ed20bSJohnny Huang 
2330ed071a2bSJohnny Huang 	if (!strcmp(argv[0], "o")) {
2331737ed20bSJohnny Huang 		input = simple_strtoul(argv[2], NULL, 16);
2332737ed20bSJohnny Huang 	} else {
2333737ed20bSJohnny Huang 		input = simple_strtoul(argv[1], NULL, 16);
2334737ed20bSJohnny Huang 		printf("OTPSTRAP[%d] will be protected\n", input);
2335737ed20bSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
2336737ed20bSJohnny Huang 		if (!confirm_yesno()) {
2337737ed20bSJohnny Huang 			printf(" Aborting\n");
2338737ed20bSJohnny Huang 			return CMD_RET_FAILURE;
2339737ed20bSJohnny Huang 		}
2340737ed20bSJohnny Huang 	}
2341737ed20bSJohnny Huang 
2342737ed20bSJohnny Huang 	prog_address = 0x800;
2343737ed20bSJohnny Huang 	if (input < 32) {
2344737ed20bSJohnny Huang 		bit_offset = input;
2345737ed20bSJohnny Huang 		prog_address |= 0x60c;
2346737ed20bSJohnny Huang 	} else if (input < 64) {
2347737ed20bSJohnny Huang 		bit_offset = input - 32;
2348737ed20bSJohnny Huang 		prog_address |= 0x60e;
2349737ed20bSJohnny Huang 	} else {
2350737ed20bSJohnny Huang 		return CMD_RET_USAGE;
2351737ed20bSJohnny Huang 	}
2352737ed20bSJohnny Huang 
2353737ed20bSJohnny Huang 	if (verify_bit(prog_address, bit_offset, 1) == 0) {
2354737ed20bSJohnny Huang 		printf("OTPSTRAP[%d] already protected\n", input);
2355737ed20bSJohnny Huang 	}
2356de6fbf1cSJohnny Huang 
235783655e91SJohnny Huang 	ret = otp_prog_bit(1, prog_address, bit_offset);
2358de6fbf1cSJohnny Huang 	otp_soak(0);
235983655e91SJohnny Huang 
236083655e91SJohnny Huang 	if (ret) {
2361737ed20bSJohnny Huang 		printf("OTPSTRAP[%d] is protected\n", input);
2362737ed20bSJohnny Huang 		return CMD_RET_SUCCESS;
2363737ed20bSJohnny Huang 	}
2364737ed20bSJohnny Huang 
2365737ed20bSJohnny Huang 	printf("Protect OTPSTRAP[%d] fail\n", input);
2366737ed20bSJohnny Huang 	return CMD_RET_FAILURE;
2367737ed20bSJohnny Huang 
2368737ed20bSJohnny Huang }
23699a4fe690SJohnny Huang 
23702a856b9aSJohnny Huang static cmd_tbl_t cmd_otp[] = {
23712a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(read, 4, 0, do_otpread, "", ""),
2372a8bd6d8cSJohnny Huang 	U_BOOT_CMD_MKENT(info, 3, 0, do_otpinfo, "", ""),
2373de6b0cc4SJohnny Huang 	U_BOOT_CMD_MKENT(prog, 3, 0, do_otpprog, "", ""),
23742a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(pb, 6, 0, do_otppb, "", ""),
2375737ed20bSJohnny Huang 	U_BOOT_CMD_MKENT(protect, 3, 0, do_otpprotect, "", ""),
23762a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(cmp, 3, 0, do_otpcmp, "", ""),
23772a856b9aSJohnny Huang };
23782a856b9aSJohnny Huang 
23792a856b9aSJohnny Huang static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
23802a856b9aSJohnny Huang {
23812a856b9aSJohnny Huang 	cmd_tbl_t *cp;
23822a856b9aSJohnny Huang 
23832a856b9aSJohnny Huang 	cp = find_cmd_tbl(argv[1], cmd_otp, ARRAY_SIZE(cmd_otp));
23842a856b9aSJohnny Huang 
2385737ed20bSJohnny Huang 	/* Drop the otp command */
23862a856b9aSJohnny Huang 	argc--;
23872a856b9aSJohnny Huang 	argv++;
23882a856b9aSJohnny Huang 
23892a856b9aSJohnny Huang 	if (cp == NULL || argc > cp->maxargs)
23902a856b9aSJohnny Huang 		return CMD_RET_USAGE;
23912a856b9aSJohnny Huang 	if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
23922a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
23932a856b9aSJohnny Huang 
2394696656c6SJohnny Huang 	if (chip_version() == OTP_AST2600A0) {
2395696656c6SJohnny Huang 		info_cb.version = OTP_AST2600A0;
23969a4fe690SJohnny Huang 		info_cb.conf_info = a0_conf_info;
23979a4fe690SJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a0_conf_info);
23989a4fe690SJohnny Huang 		info_cb.strap_info = a0_strap_info;
23999a4fe690SJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a0_strap_info);
24009a4fe690SJohnny Huang 		info_cb.key_info = a0_key_type;
24019a4fe690SJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a0_key_type);
2402696656c6SJohnny Huang 	} else if (chip_version() == OTP_AST2600A1) {
2403696656c6SJohnny Huang 		info_cb.version = OTP_AST2600A1;
24043cb28812SJohnny Huang 		info_cb.conf_info = a1_conf_info;
24053cb28812SJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a1_conf_info);
24063cb28812SJohnny Huang 		info_cb.strap_info = a1_strap_info;
24073cb28812SJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a1_strap_info);
24089a4fe690SJohnny Huang 		info_cb.key_info = a1_key_type;
24099a4fe690SJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a1_key_type);
24109a4fe690SJohnny Huang 	}
24119a4fe690SJohnny Huang 
24122a856b9aSJohnny Huang 	return cp->cmd(cmdtp, flag, argc, argv);
241369d5fd8fSJohnny Huang }
241469d5fd8fSJohnny Huang 
241569d5fd8fSJohnny Huang U_BOOT_CMD(
241669d5fd8fSJohnny Huang 	otp, 7, 0,  do_ast_otp,
241769d5fd8fSJohnny Huang 	"ASPEED One-Time-Programmable sub-system",
24182a856b9aSJohnny Huang 	"read conf|data <otp_dw_offset> <dw_count>\n"
24192a856b9aSJohnny Huang 	"otp read strap <strap_bit_offset> <bit_count>\n"
24202d4b0742SJohnny Huang 	"otp info strap [v]\n"
24212d4b0742SJohnny Huang 	"otp info conf [otp_dw_offset]\n"
2422de6b0cc4SJohnny Huang 	"otp prog [o] <addr>\n"
2423ed071a2bSJohnny Huang 	"otp pb conf|data [o] <otp_dw_offset> <bit_offset> <value>\n"
2424ed071a2bSJohnny Huang 	"otp pb strap [o] <bit_offset> <value>\n"
2425ed071a2bSJohnny Huang 	"otp protect [o] <bit_offset>\n"
24262a856b9aSJohnny Huang 	"otp cmp <addr> <otp_dw_offset>\n"
242769d5fd8fSJohnny Huang );
2428