xref: /openbmc/u-boot/cmd/otp.c (revision 5010032b7c04f83eccae83392709c61b76893fc1)
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;
91*5010032bSJohnny 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;
134*5010032bSJohnny Huang 
1359a4fe690SJohnny Huang };
1369a4fe690SJohnny Huang 
137696656c6SJohnny Huang struct otp_image_layout {
138*5010032bSJohnny Huang 	int data_length;
139*5010032bSJohnny Huang 	int conf_length;
140*5010032bSJohnny Huang 	int strap_length;
141696656c6SJohnny Huang 	uint8_t *data;
142696656c6SJohnny Huang 	uint8_t *data_ignore;
143696656c6SJohnny Huang 	uint8_t *conf;
144696656c6SJohnny Huang 	uint8_t *conf_ignore;
145696656c6SJohnny Huang 	uint8_t *strap;
146696656c6SJohnny Huang 	uint8_t *strap_reg_pro;
147696656c6SJohnny Huang 	uint8_t *strap_pro;
148696656c6SJohnny Huang 	uint8_t *strap_ignore;
149696656c6SJohnny Huang };
150696656c6SJohnny Huang 
1519a4fe690SJohnny Huang static struct otp_info_cb info_cb;
1529a4fe690SJohnny Huang 
15379e42a59SJoel Stanley static const struct otpstrap_info a0_strap_info[] = {
15491448c03SJohnny Huang 	{ 0, 1, 0, "Disable secure boot" },
15591448c03SJohnny Huang 	{ 0, 1, 1, "Enable secure boot"	},
15691448c03SJohnny Huang 	{ 1, 1, 0, "Disable boot from eMMC" },
15791448c03SJohnny Huang 	{ 1, 1, 1, "Enable boot from eMMC" },
15891448c03SJohnny Huang 	{ 2, 1, 0, "Disable Boot from debug SPI" },
15991448c03SJohnny Huang 	{ 2, 1, 1, "Enable Boot from debug SPI" },
16091448c03SJohnny Huang 	{ 3, 1, 0, "Enable ARM CM3" },
16191448c03SJohnny Huang 	{ 3, 1, 1, "Disable ARM CM3" },
162541eb887SJohnny Huang 	{ 4, 1, 0, "No VGA BIOS ROM, VGA BIOS is merged in the system BIOS" },
16391448c03SJohnny Huang 	{ 4, 1, 1, "Enable dedicated VGA BIOS ROM" },
16491448c03SJohnny Huang 	{ 5, 1, 0, "MAC 1 : RMII/NCSI" },
16591448c03SJohnny Huang 	{ 5, 1, 1, "MAC 1 : RGMII" },
16691448c03SJohnny Huang 	{ 6, 1, 0, "MAC 2 : RMII/NCSI" },
16791448c03SJohnny Huang 	{ 6, 1, 1, "MAC 2 : RGMII" },
16891448c03SJohnny Huang 	{ 7, 3, 0, "CPU Frequency : 1GHz" },
16991448c03SJohnny Huang 	{ 7, 3, 1, "CPU Frequency : 800MHz" },
17091448c03SJohnny Huang 	{ 7, 3, 2, "CPU Frequency : 1.2GHz" },
17191448c03SJohnny Huang 	{ 7, 3, 3, "CPU Frequency : 1.4GHz" },
17291448c03SJohnny Huang 	{ 10, 2, 0, "HCLK ratio AXI:AHB = 2:1" },
17391448c03SJohnny Huang 	{ 10, 2, 1, "HCLK ratio AXI:AHB = 2:1" },
17491448c03SJohnny Huang 	{ 10, 2, 2, "HCLK ratio AXI:AHB = 3:1" },
17591448c03SJohnny Huang 	{ 10, 2, 3, "HCLK ratio AXI:AHB = 4:1" },
17691448c03SJohnny Huang 	{ 12, 2, 0, "VGA memory size : 8MB" },
17791448c03SJohnny Huang 	{ 12, 2, 1, "VGA memory size : 16MB" },
17891448c03SJohnny Huang 	{ 12, 2, 2, "VGA memory size : 32MB" },
17991448c03SJohnny Huang 	{ 12, 2, 3, "VGA memory size : 64MB" },
18091448c03SJohnny Huang 	{ 14, 3, OTP_REG_RESERVED, "" },
18191448c03SJohnny Huang 	{ 17, 1, 0, "VGA class code : Class Code for video device" },
18291448c03SJohnny Huang 	{ 17, 1, 1, "VGA class code : Class Code for VGA device" },
18391448c03SJohnny Huang 	{ 18, 1, 0, "Enable debug interfaces 0" },
18491448c03SJohnny Huang 	{ 18, 1, 1, "Disable debug interfaces 0" },
18591448c03SJohnny Huang 	{ 19, 1, 0, "Boot from emmc mode : High eMMC speed" },
18691448c03SJohnny Huang 	{ 19, 1, 1, "Boot from emmc mode : Normal eMMC speed" },
18791448c03SJohnny Huang 	{ 20, 1, 0, "Enable Pcie EHCI device" },
18891448c03SJohnny Huang 	{ 20, 1, 1, "Disable Pcie EHCI device" },
18991448c03SJohnny Huang 	{ 21, 1, 0, "Enable VGA XDMA function" },
19091448c03SJohnny Huang 	{ 21, 1, 1, "Disable VGA XDMA function" },
19191448c03SJohnny Huang 	{ 22, 1, 0, "Normal BMC mode" },
19291448c03SJohnny Huang 	{ 22, 1, 1, "Disable dedicated BMC functions for non-BMC application" },
19391448c03SJohnny Huang 	{ 23, 1, 0, "SSPRST# pin is for secondary processor dedicated reset pin" },
19491448c03SJohnny Huang 	{ 23, 1, 1, "SSPRST# pin is for PCIE root complex dedicated reset pin" },
19591448c03SJohnny Huang 	{ 24, 1, 0, "DRAM types : DDR4" },
19691448c03SJohnny Huang 	{ 24, 1, 1, "DRAM types : DDR3" },
19791448c03SJohnny Huang 	{ 25, 5, OTP_REG_RESERVED, "" },
19891448c03SJohnny Huang 	{ 30, 2, OTP_REG_RESERVED, "" },
19991448c03SJohnny Huang 	{ 32, 1, 0, "MAC 3 : RMII/NCSI" },
20091448c03SJohnny Huang 	{ 32, 1, 1, "MAC 3 : RGMII" },
20191448c03SJohnny Huang 	{ 33, 1, 0, "MAC 4 : RMII/NCSI" },
20291448c03SJohnny Huang 	{ 33, 1, 1, "MAC 4 : RGMII" },
20391448c03SJohnny Huang 	{ 34, 1, 0, "SuperIO configuration address : 0x2E" },
20491448c03SJohnny Huang 	{ 34, 1, 1, "SuperIO configuration address : 0x4E" },
20591448c03SJohnny Huang 	{ 35, 1, 0, "Enable LPC to decode SuperIO" },
20691448c03SJohnny Huang 	{ 35, 1, 1, "Disable LPC to decode SuperIO" },
20791448c03SJohnny Huang 	{ 36, 1, 0, "Enable debug interfaces 1" },
20891448c03SJohnny Huang 	{ 36, 1, 1, "Disable debug interfaces 1" },
20991448c03SJohnny Huang 	{ 37, 1, 0, "Disable ACPI function" },
21091448c03SJohnny Huang 	{ 37, 1, 1, "Enable ACPI function" },
21191448c03SJohnny Huang 	{ 38, 1, 0, "Enable eSPI mode" },
21291448c03SJohnny Huang 	{ 38, 1, 1, "Enable LPC mode" },
21391448c03SJohnny Huang 	{ 39, 1, 0, "Enable SAFS mode" },
21491448c03SJohnny Huang 	{ 39, 1, 1, "Enable SAFS mode" },
21591448c03SJohnny Huang 	{ 40, 2, OTP_REG_RESERVED, "" },
21691448c03SJohnny Huang 	{ 42, 1, 0, "Disable boot SPI 3B/4B address mode auto detection" },
21791448c03SJohnny Huang 	{ 42, 1, 1, "Enable boot SPI 3B/4B address mode auto detection" },
21891448c03SJohnny Huang 	{ 43, 1, 0, "Disable boot SPI ABR" },
21991448c03SJohnny Huang 	{ 43, 1, 1, "Enable boot SPI ABR" },
22091448c03SJohnny Huang 	{ 44, 1, 0, "Boot SPI ABR mode : dual SPI flash" },
22191448c03SJohnny Huang 	{ 44, 1, 1, "Boot SPI ABR mode : single SPI flash" },
22291448c03SJohnny Huang 	{ 45, 3, 0, "Boot SPI flash size : no define size" },
22391448c03SJohnny Huang 	{ 45, 3, 1, "Boot SPI flash size : 2MB" },
22491448c03SJohnny Huang 	{ 45, 3, 2, "Boot SPI flash size : 4MB" },
22591448c03SJohnny Huang 	{ 45, 3, 3, "Boot SPI flash size : 8MB" },
22691448c03SJohnny Huang 	{ 45, 3, 4, "Boot SPI flash size : 16MB" },
22791448c03SJohnny Huang 	{ 45, 3, 5, "Boot SPI flash size : 32MB" },
22891448c03SJohnny Huang 	{ 45, 3, 6, "Boot SPI flash size : 64MB" },
22991448c03SJohnny Huang 	{ 45, 3, 7, "Boot SPI flash size : 128MB" },
23091448c03SJohnny Huang 	{ 48, 1, 0, "Disable host SPI ABR" },
23191448c03SJohnny Huang 	{ 48, 1, 1, "Enable host SPI ABR" },
23291448c03SJohnny Huang 	{ 49, 1, 0, "Disable host SPI ABR mode select pin" },
23391448c03SJohnny Huang 	{ 49, 1, 1, "Enable host SPI ABR mode select pin" },
23491448c03SJohnny Huang 	{ 50, 1, 0, "Host SPI ABR mode : dual SPI flash" },
23591448c03SJohnny Huang 	{ 50, 1, 1, "Host SPI ABR mode : single SPI flash" },
23691448c03SJohnny Huang 	{ 51, 3, 0, "Host SPI flash size : no define size" },
23791448c03SJohnny Huang 	{ 51, 3, 1, "Host SPI flash size : 2MB" },
23891448c03SJohnny Huang 	{ 51, 3, 2, "Host SPI flash size : 4MB" },
23991448c03SJohnny Huang 	{ 51, 3, 3, "Host SPI flash size : 8MB" },
24091448c03SJohnny Huang 	{ 51, 3, 4, "Host SPI flash size : 16MB" },
24191448c03SJohnny Huang 	{ 51, 3, 5, "Host SPI flash size : 32MB" },
24291448c03SJohnny Huang 	{ 51, 3, 6, "Host SPI flash size : 64MB" },
24391448c03SJohnny Huang 	{ 51, 3, 7, "Host SPI flash size : 128MB" },
24491448c03SJohnny Huang 	{ 54, 1, 0, "Disable boot SPI auxiliary control pins" },
24591448c03SJohnny Huang 	{ 54, 1, 1, "Enable boot SPI auxiliary control pins" },
24691448c03SJohnny Huang 	{ 55, 2, 0, "Boot SPI CRTM size : disable CRTM" },
24791448c03SJohnny Huang 	{ 55, 2, 1, "Boot SPI CRTM size : 256KB" },
24891448c03SJohnny Huang 	{ 55, 2, 2, "Boot SPI CRTM size : 512KB" },
24991448c03SJohnny Huang 	{ 55, 2, 3, "Boot SPI CRTM size : 1MB" },
25091448c03SJohnny Huang 	{ 57, 2, 0, "Host SPI CRTM size : disable CRTM" },
25191448c03SJohnny Huang 	{ 57, 2, 1, "Host SPI CRTM size : 256KB" },
25291448c03SJohnny Huang 	{ 57, 2, 2, "Host SPI CRTM size : 512KB" },
25391448c03SJohnny Huang 	{ 57, 2, 3, "Host SPI CRTM size : 1MB" },
25491448c03SJohnny Huang 	{ 59, 1, 0, "Disable host SPI auxiliary control pins" },
25591448c03SJohnny Huang 	{ 59, 1, 1, "Enable host SPI auxiliary control pins" },
25691448c03SJohnny Huang 	{ 60, 1, 0, "Disable GPIO pass through" },
25791448c03SJohnny Huang 	{ 60, 1, 1, "Enable GPIO pass through" },
25891448c03SJohnny Huang 	{ 61, 1, 0, "Enable low security secure boot key" },
25991448c03SJohnny Huang 	{ 61, 1, 1, "Disable low security secure boot key" },
26091448c03SJohnny Huang 	{ 62, 1, 0, "Disable dedicate GPIO strap pins" },
26191448c03SJohnny Huang 	{ 62, 1, 1, "Enable dedicate GPIO strap pins" },
26291448c03SJohnny Huang 	{ 63, 1, OTP_REG_RESERVED, "" }
26376d13988SJohnny Huang };
2649a4fe690SJohnny Huang 
26579e42a59SJoel Stanley static const struct otpstrap_info a1_strap_info[] = {
2663cb28812SJohnny Huang 	{ 0, 1, 0, "Disable secure boot" },
2673cb28812SJohnny Huang 	{ 0, 1, 1, "Enable secure boot"	},
2683cb28812SJohnny Huang 	{ 1, 1, 0, "Disable boot from eMMC" },
2693cb28812SJohnny Huang 	{ 1, 1, 1, "Enable boot from eMMC" },
2703cb28812SJohnny Huang 	{ 2, 1, 0, "Disable Boot from debug SPI" },
2713cb28812SJohnny Huang 	{ 2, 1, 1, "Enable Boot from debug SPI" },
2723cb28812SJohnny Huang 	{ 3, 1, 0, "Enable ARM CM3" },
2733cb28812SJohnny Huang 	{ 3, 1, 1, "Disable ARM CM3" },
2743cb28812SJohnny Huang 	{ 4, 1, 0, "No VGA BIOS ROM, VGA BIOS is merged in the system BIOS" },
2753cb28812SJohnny Huang 	{ 4, 1, 1, "Enable dedicated VGA BIOS ROM" },
2763cb28812SJohnny Huang 	{ 5, 1, 0, "MAC 1 : RMII/NCSI" },
2773cb28812SJohnny Huang 	{ 5, 1, 1, "MAC 1 : RGMII" },
2783cb28812SJohnny Huang 	{ 6, 1, 0, "MAC 2 : RMII/NCSI" },
2793cb28812SJohnny Huang 	{ 6, 1, 1, "MAC 2 : RGMII" },
2803cb28812SJohnny Huang 	{ 7, 3, 0, "CPU Frequency : 1GHz" },
2813cb28812SJohnny Huang 	{ 7, 3, 1, "CPU Frequency : 800MHz" },
2823cb28812SJohnny Huang 	{ 7, 3, 2, "CPU Frequency : 1.2GHz" },
2833cb28812SJohnny Huang 	{ 7, 3, 3, "CPU Frequency : 1.4GHz" },
2843cb28812SJohnny Huang 	{ 10, 2, 0, "HCLK ratio AXI:AHB = 2:1" },
2853cb28812SJohnny Huang 	{ 10, 2, 1, "HCLK ratio AXI:AHB = 2:1" },
2863cb28812SJohnny Huang 	{ 10, 2, 2, "HCLK ratio AXI:AHB = 3:1" },
2873cb28812SJohnny Huang 	{ 10, 2, 3, "HCLK ratio AXI:AHB = 4:1" },
2883cb28812SJohnny Huang 	{ 12, 2, 0, "VGA memory size : 8MB" },
2893cb28812SJohnny Huang 	{ 12, 2, 1, "VGA memory size : 16MB" },
2903cb28812SJohnny Huang 	{ 12, 2, 2, "VGA memory size : 32MB" },
2913cb28812SJohnny Huang 	{ 12, 2, 3, "VGA memory size : 64MB" },
2923cb28812SJohnny Huang 	{ 14, 3, OTP_REG_RESERVED, "" },
2933cb28812SJohnny Huang 	{ 17, 1, 0, "VGA class code : Class Code for video device" },
2943cb28812SJohnny Huang 	{ 17, 1, 1, "VGA class code : Class Code for VGA device" },
2953cb28812SJohnny Huang 	{ 18, 1, 0, "Enable debug interfaces 0" },
2963cb28812SJohnny Huang 	{ 18, 1, 1, "Disable debug interfaces 0" },
2973cb28812SJohnny Huang 	{ 19, 1, 0, "Boot from emmc mode : High eMMC speed" },
2983cb28812SJohnny Huang 	{ 19, 1, 1, "Boot from emmc mode : Normal eMMC speed" },
2993cb28812SJohnny Huang 	{ 20, 1, 0, "Disable Pcie EHCI device" },
3003cb28812SJohnny Huang 	{ 20, 1, 1, "Enable Pcie EHCI device" },
3013cb28812SJohnny Huang 	{ 21, 1, 0, "Enable VGA XDMA function" },
3023cb28812SJohnny Huang 	{ 21, 1, 1, "Disable VGA XDMA function" },
3033cb28812SJohnny Huang 	{ 22, 1, 0, "Normal BMC mode" },
3043cb28812SJohnny Huang 	{ 22, 1, 1, "Disable dedicated BMC functions for non-BMC application" },
3053cb28812SJohnny Huang 	{ 23, 1, 0, "SSPRST# pin is for secondary processor dedicated reset pin" },
3063cb28812SJohnny Huang 	{ 23, 1, 1, "SSPRST# pin is for PCIE root complex dedicated reset pin" },
3073cb28812SJohnny Huang 	{ 24, 1, 0, "Enable watchdog to reset full chip" },
3083cb28812SJohnny Huang 	{ 24, 1, 1, "Disable watchdog to reset full chip" },
3093cb28812SJohnny Huang 	{ 25, 5, OTP_REG_RESERVED, "" },
3103cb28812SJohnny Huang 	{ 30, 2, OTP_REG_RESERVED, "" },
3113cb28812SJohnny Huang 	{ 32, 1, 0, "MAC 3 : RMII/NCSI" },
3123cb28812SJohnny Huang 	{ 32, 1, 1, "MAC 3 : RGMII" },
3133cb28812SJohnny Huang 	{ 33, 1, 0, "MAC 4 : RMII/NCSI" },
3143cb28812SJohnny Huang 	{ 33, 1, 1, "MAC 4 : RGMII" },
3153cb28812SJohnny Huang 	{ 34, 1, 0, "SuperIO configuration address : 0x2E" },
3163cb28812SJohnny Huang 	{ 34, 1, 1, "SuperIO configuration address : 0x4E" },
3173cb28812SJohnny Huang 	{ 35, 1, 0, "Enable LPC to decode SuperIO" },
3183cb28812SJohnny Huang 	{ 35, 1, 1, "Disable LPC to decode SuperIO" },
3193cb28812SJohnny Huang 	{ 36, 1, 0, "Enable debug interfaces 1" },
3203cb28812SJohnny Huang 	{ 36, 1, 1, "Disable debug interfaces 1" },
3213cb28812SJohnny Huang 	{ 37, 1, 0, "Disable ACPI function" },
3223cb28812SJohnny Huang 	{ 37, 1, 1, "Enable ACPI function" },
3233cb28812SJohnny Huang 	{ 38, 1, 0, "Enable eSPI mode" },
3243cb28812SJohnny Huang 	{ 38, 1, 1, "Enable LPC mode" },
3253cb28812SJohnny Huang 	{ 39, 1, 0, "Enable SAFS mode" },
3263cb28812SJohnny Huang 	{ 39, 1, 1, "Enable SAFS mode" },
3273cb28812SJohnny Huang 	{ 40, 2, OTP_REG_RESERVED, "" },
3283cb28812SJohnny Huang 	{ 42, 1, 0, "Disable boot SPI 3B/4B address mode auto detection" },
3293cb28812SJohnny Huang 	{ 42, 1, 1, "Enable boot SPI 3B/4B address mode auto detection" },
3303cb28812SJohnny Huang 	{ 43, 1, 0, "Disable boot SPI ABR" },
3313cb28812SJohnny Huang 	{ 43, 1, 1, "Enable boot SPI ABR" },
3323cb28812SJohnny Huang 	{ 44, 1, 0, "Boot SPI ABR mode : dual SPI flash" },
3333cb28812SJohnny Huang 	{ 44, 1, 1, "Boot SPI ABR mode : single SPI flash" },
3343cb28812SJohnny Huang 	{ 45, 3, 0, "Boot SPI flash size : no define size" },
3353cb28812SJohnny Huang 	{ 45, 3, 1, "Boot SPI flash size : 2MB" },
3363cb28812SJohnny Huang 	{ 45, 3, 2, "Boot SPI flash size : 4MB" },
3373cb28812SJohnny Huang 	{ 45, 3, 3, "Boot SPI flash size : 8MB" },
3383cb28812SJohnny Huang 	{ 45, 3, 4, "Boot SPI flash size : 16MB" },
3393cb28812SJohnny Huang 	{ 45, 3, 5, "Boot SPI flash size : 32MB" },
3403cb28812SJohnny Huang 	{ 45, 3, 6, "Boot SPI flash size : 64MB" },
3413cb28812SJohnny Huang 	{ 45, 3, 7, "Boot SPI flash size : 128MB" },
3423cb28812SJohnny Huang 	{ 48, 1, 0, "Disable host SPI ABR" },
3433cb28812SJohnny Huang 	{ 48, 1, 1, "Enable host SPI ABR" },
3443cb28812SJohnny Huang 	{ 49, 1, 0, "Disable host SPI ABR mode select pin" },
3453cb28812SJohnny Huang 	{ 49, 1, 1, "Enable host SPI ABR mode select pin" },
3463cb28812SJohnny Huang 	{ 50, 1, 0, "Host SPI ABR mode : dual SPI flash" },
3473cb28812SJohnny Huang 	{ 50, 1, 1, "Host SPI ABR mode : single SPI flash" },
3483cb28812SJohnny Huang 	{ 51, 3, 0, "Host SPI flash size : no define size" },
3493cb28812SJohnny Huang 	{ 51, 3, 1, "Host SPI flash size : 2MB" },
3503cb28812SJohnny Huang 	{ 51, 3, 2, "Host SPI flash size : 4MB" },
3513cb28812SJohnny Huang 	{ 51, 3, 3, "Host SPI flash size : 8MB" },
3523cb28812SJohnny Huang 	{ 51, 3, 4, "Host SPI flash size : 16MB" },
3533cb28812SJohnny Huang 	{ 51, 3, 5, "Host SPI flash size : 32MB" },
3543cb28812SJohnny Huang 	{ 51, 3, 6, "Host SPI flash size : 64MB" },
3553cb28812SJohnny Huang 	{ 51, 3, 7, "Host SPI flash size : 128MB" },
3563cb28812SJohnny Huang 	{ 54, 1, 0, "Disable boot SPI auxiliary control pins" },
3573cb28812SJohnny Huang 	{ 54, 1, 1, "Enable boot SPI auxiliary control pins" },
3583cb28812SJohnny Huang 	{ 55, 2, 0, "Boot SPI CRTM size : disable CRTM" },
3593cb28812SJohnny Huang 	{ 55, 2, 1, "Boot SPI CRTM size : 256KB" },
3603cb28812SJohnny Huang 	{ 55, 2, 2, "Boot SPI CRTM size : 512KB" },
3613cb28812SJohnny Huang 	{ 55, 2, 3, "Boot SPI CRTM size : 1MB" },
3623cb28812SJohnny Huang 	{ 57, 2, 0, "Host SPI CRTM size : disable CRTM" },
3633cb28812SJohnny Huang 	{ 57, 2, 1, "Host SPI CRTM size : 256KB" },
3643cb28812SJohnny Huang 	{ 57, 2, 2, "Host SPI CRTM size : 512KB" },
3653cb28812SJohnny Huang 	{ 57, 2, 3, "Host SPI CRTM size : 1MB" },
3663cb28812SJohnny Huang 	{ 59, 1, 0, "Disable host SPI auxiliary control pins" },
3673cb28812SJohnny Huang 	{ 59, 1, 1, "Enable host SPI auxiliary control pins" },
3683cb28812SJohnny Huang 	{ 60, 1, 0, "Disable GPIO pass through" },
3693cb28812SJohnny Huang 	{ 60, 1, 1, "Enable GPIO pass through" },
3703cb28812SJohnny Huang 	{ 61, 1, 0, "Enable low security secure boot key" },
3713cb28812SJohnny Huang 	{ 61, 1, 1, "Disable low security secure boot key" },
3723cb28812SJohnny Huang 	{ 62, 1, 0, "Disable dedicate GPIO strap pins" },
3733cb28812SJohnny Huang 	{ 62, 1, 1, "Enable dedicate GPIO strap pins" },
3743cb28812SJohnny Huang 	{ 63, 1, OTP_REG_RESERVED, "" }
3753cb28812SJohnny Huang };
3763cb28812SJohnny Huang 
37779e42a59SJoel Stanley static const struct otpconf_info a0_conf_info[] = {
37891448c03SJohnny Huang 	{ 0, 0,  1,  0, "Enable Secure Region programming" },
37991448c03SJohnny Huang 	{ 0, 0,  1,  1, "Disable Secure Region programming" },
38091448c03SJohnny Huang 	{ 0, 1,  1,  0, "Disable Secure Boot" },
38191448c03SJohnny Huang 	{ 0, 1,  1,  1, "Enable Secure Boot" },
38291448c03SJohnny Huang 	{ 0, 2,  1,  0, "Initialization programming not done" },
38391448c03SJohnny Huang 	{ 0, 2,  1,  1, "Initialization programming done" },
38491448c03SJohnny Huang 	{ 0, 3,  1,  0, "User region ECC disable" },
38591448c03SJohnny Huang 	{ 0, 3,  1,  1, "User region ECC enable" },
38691448c03SJohnny Huang 	{ 0, 4,  1,  0, "Secure Region ECC disable" },
38791448c03SJohnny Huang 	{ 0, 4,  1,  1, "Secure Region ECC enable" },
38891448c03SJohnny Huang 	{ 0, 5,  1,  0, "Enable low security key" },
38991448c03SJohnny Huang 	{ 0, 5,  1,  1, "Disable low security key" },
39091448c03SJohnny Huang 	{ 0, 6,  1,  0, "Do not ignore Secure Boot hardware strap" },
39191448c03SJohnny Huang 	{ 0, 6,  1,  1, "Ignore Secure Boot hardware strap" },
39291448c03SJohnny Huang 	{ 0, 7,  1,  0, "Secure Boot Mode: 1" },
39391448c03SJohnny Huang 	{ 0, 7,  1,  1, "Secure Boot Mode: 2" },
39491448c03SJohnny Huang 	{ 0, 8,  2,  0, "Single cell mode (recommended)" },
395541eb887SJohnny Huang 	{ 0, 8,  2,  1, "Differential mode" },
39691448c03SJohnny Huang 	{ 0, 8,  2,  2, "Differential-redundant mode" },
39791448c03SJohnny Huang 	{ 0, 10, 2,  0, "RSA mode : RSA1024" },
39891448c03SJohnny Huang 	{ 0, 10, 2,  1, "RSA mode : RSA2048" },
39991448c03SJohnny Huang 	{ 0, 10, 2,  2, "RSA mode : RSA3072" },
40091448c03SJohnny Huang 	{ 0, 10, 2,  3, "RSA mode : RSA4096" },
40191448c03SJohnny Huang 	{ 0, 12, 2,  0, "SHA mode : SHA224" },
40291448c03SJohnny Huang 	{ 0, 12, 2,  1, "SHA mode : SHA256" },
40391448c03SJohnny Huang 	{ 0, 12, 2,  2, "SHA mode : SHA384" },
40491448c03SJohnny Huang 	{ 0, 12, 2,  3, "SHA mode : SHA512" },
40591448c03SJohnny Huang 	{ 0, 14, 2,  OTP_REG_RESERVED, "" },
40691448c03SJohnny Huang 	{ 0, 16, 6,  OTP_REG_VALUE, "Secure Region size (DW): 0x%x" },
40791448c03SJohnny Huang 	{ 0, 22, 1,  0, "Secure Region : Writable" },
40891448c03SJohnny Huang 	{ 0, 22, 1,  1, "Secure Region : Write Protect" },
40991448c03SJohnny Huang 	{ 0, 23, 1,  0, "User Region : Writable" },
41091448c03SJohnny Huang 	{ 0, 23, 1,  1, "User Region : Write Protect" },
41191448c03SJohnny Huang 	{ 0, 24, 1,  0, "Configure Region : Writable" },
41291448c03SJohnny Huang 	{ 0, 24, 1,  1, "Configure Region : Write Protect" },
41391448c03SJohnny Huang 	{ 0, 25, 1,  0, "OTP strap Region : Writable" },
41491448c03SJohnny Huang 	{ 0, 25, 1,  1, "OTP strap Region : Write Protect" },
41591448c03SJohnny Huang 	{ 0, 26, 1,  0, "Disable Copy Boot Image to Internal SRAM" },
41691448c03SJohnny Huang 	{ 0, 26, 1,  1, "Copy Boot Image to Internal SRAM" },
41791448c03SJohnny Huang 	{ 0, 27, 1,  0, "Disable image encryption" },
41891448c03SJohnny Huang 	{ 0, 27, 1,  1, "Enable image encryption" },
41991448c03SJohnny Huang 	{ 0, 28, 1,  OTP_REG_RESERVED, "" },
42091448c03SJohnny Huang 	{ 0, 29, 1,  0, "OTP key retire Region : Writable" },
42191448c03SJohnny Huang 	{ 0, 29, 1,  1, "OTP key retire Region : Write Protect" },
4223cb28812SJohnny Huang 	{ 0, 30, 1,  0, "Data region redundancy repair disable" },
4233cb28812SJohnny Huang 	{ 0, 30, 1,  1, "Data region redundancy repair enable" },
4243cb28812SJohnny Huang 	{ 0, 31, 1,  0, "OTP memory lock disable" },
4253cb28812SJohnny Huang 	{ 0, 31, 1,  1, "OTP memory lock enable" },
4263cb28812SJohnny Huang 	{ 2, 0,  16, OTP_REG_VALUE, "Vender ID : 0x%x" },
4273cb28812SJohnny Huang 	{ 2, 16, 16, OTP_REG_VALUE, "Key Revision : 0x%x" },
4283cb28812SJohnny Huang 	{ 3, 0,  16, OTP_REG_VALUE, "Secure boot header offset : 0x%x" },
4293cb28812SJohnny Huang 	{ 4, 0,  8,  OTP_REG_VALID_BIT, "Keys valid  : %s" },
4303cb28812SJohnny Huang 	{ 4, 16, 8,  OTP_REG_VALID_BIT, "Keys retire  : %s" },
4313cb28812SJohnny Huang 	{ 5, 0,  32, OTP_REG_VALUE, "User define data, random number low : 0x%x" },
4323cb28812SJohnny Huang 	{ 6, 0,  32, OTP_REG_VALUE, "User define data, random number high : 0x%x" },
4333cb28812SJohnny Huang 	{ 7, 0,  1,  0, "Force enable PCI bus to AHB bus bridge" },
4343cb28812SJohnny Huang 	{ 7, 0,  1,  1, "Force disable PCI bus to AHB bus bridge" },
4353cb28812SJohnny Huang 	{ 7, 1,  1,  0, "Force enable UART5 debug port function" },
4363cb28812SJohnny Huang 	{ 7, 1,  1,  1, "Force disable UART5 debug port function" },
4373cb28812SJohnny Huang 	{ 7, 2,  1,  0, "Force enable XDMA function" },
4383cb28812SJohnny Huang 	{ 7, 2,  1,  1, "Force disable XDMA function" },
4393cb28812SJohnny Huang 	{ 7, 3,  1,  0, "Force enable APB to PCIE device bridge" },
4403cb28812SJohnny Huang 	{ 7, 3,  1,  1, "Force disable APB to PCIE device bridge" },
4413cb28812SJohnny Huang 	{ 7, 4,  1,  0, "Force enable APB to PCIE bridge config access" },
4423cb28812SJohnny Huang 	{ 7, 4,  1,  1, "Force disable APB to PCIE bridge config access" },
4433cb28812SJohnny Huang 	{ 7, 5,  1,  0, "Force enable PCIE bus trace buffer" },
4443cb28812SJohnny Huang 	{ 7, 5,  1,  1, "Force disable PCIE bus trace buffer" },
4453cb28812SJohnny Huang 	{ 7, 6,  1,  0, "Force enable the capability for PCIE device port as a Root Complex" },
4463cb28812SJohnny Huang 	{ 7, 6,  1,  1, "Force disable the capability for PCIE device port as a Root Complex" },
4473cb28812SJohnny Huang 	{ 7, 16, 1,  0, "Force enable ESPI bus to AHB bus bridge" },
4483cb28812SJohnny Huang 	{ 7, 16, 1,  1, "Force disable ESPI bus to AHB bus bridge" },
4493cb28812SJohnny Huang 	{ 7, 17, 1,  0, "Force enable LPC bus to AHB bus bridge1" },
4503cb28812SJohnny Huang 	{ 7, 17, 1,  1, "Force disable LPC bus to AHB bus bridge1" },
4513cb28812SJohnny Huang 	{ 7, 18, 1,  0, "Force enable LPC bus to AHB bus bridge2" },
4523cb28812SJohnny Huang 	{ 7, 18, 1,  1, "Force disable LPC bus to AHB bus bridge2" },
4533cb28812SJohnny Huang 	{ 7, 19, 1,  0, "Force enable UART1 debug port function" },
4543cb28812SJohnny Huang 	{ 7, 19, 1,  1, "Force disable UART1 debug port function" },
4553cb28812SJohnny Huang 	{ 7, 31, 1,  0, "Disable chip security setting" },
4563cb28812SJohnny Huang 	{ 7, 31, 1,  1, "Enable chip security setting" },
4573cb28812SJohnny Huang 	{ 8, 0,  32, OTP_REG_VALUE, "Redundancy Repair : 0x%x" },
4583cb28812SJohnny Huang 	{ 10, 0, 32, OTP_REG_VALUE, "Manifest ID low : 0x%x" },
4593cb28812SJohnny Huang 	{ 11, 0, 32, OTP_REG_VALUE, "Manifest ID high : 0x%x" }
4603cb28812SJohnny Huang };
4613cb28812SJohnny Huang 
46279e42a59SJoel Stanley static const struct otpconf_info a1_conf_info[] = {
4633cb28812SJohnny Huang 	{ 0, 0,  1,  OTP_REG_RESERVED, "" },
4643cb28812SJohnny Huang 	{ 0, 1,  1,  0, "Disable Secure Boot" },
4653cb28812SJohnny Huang 	{ 0, 1,  1,  1, "Enable Secure Boot" },
4663cb28812SJohnny Huang 	{ 0, 2,  1,  0, "Initialization programming not done" },
4673cb28812SJohnny Huang 	{ 0, 2,  1,  1, "Initialization programming done" },
4683cb28812SJohnny Huang 	{ 0, 3,  1,  0, "User region ECC disable" },
4693cb28812SJohnny Huang 	{ 0, 3,  1,  1, "User region ECC enable" },
4703cb28812SJohnny Huang 	{ 0, 4,  1,  0, "Secure Region ECC disable" },
4713cb28812SJohnny Huang 	{ 0, 4,  1,  1, "Secure Region ECC enable" },
4723cb28812SJohnny Huang 	{ 0, 5,  1,  0, "Enable low security key" },
4733cb28812SJohnny Huang 	{ 0, 5,  1,  1, "Disable low security key" },
4743cb28812SJohnny Huang 	{ 0, 6,  1,  0, "Do not ignore Secure Boot hardware strap" },
4753cb28812SJohnny Huang 	{ 0, 6,  1,  1, "Ignore Secure Boot hardware strap" },
4763cb28812SJohnny Huang 	{ 0, 7,  1,  0, "Secure Boot Mode: GCM" },
4773cb28812SJohnny Huang 	{ 0, 7,  1,  1, "Secure Boot Mode: 2" },
4783cb28812SJohnny Huang 	{ 0, 8,  2,  0, "Single cell mode (recommended)" },
4793cb28812SJohnny Huang 	{ 0, 8,  2,  1, "Differential mode" },
4803cb28812SJohnny Huang 	{ 0, 8,  2,  2, "Differential-redundant mode" },
4813cb28812SJohnny Huang 	{ 0, 10, 2,  0, "RSA mode : RSA1024" },
4823cb28812SJohnny Huang 	{ 0, 10, 2,  1, "RSA mode : RSA2048" },
4833cb28812SJohnny Huang 	{ 0, 10, 2,  2, "RSA mode : RSA3072" },
4843cb28812SJohnny Huang 	{ 0, 10, 2,  3, "RSA mode : RSA4096" },
4853cb28812SJohnny Huang 	{ 0, 12, 2,  0, "SHA mode : SHA224" },
4863cb28812SJohnny Huang 	{ 0, 12, 2,  1, "SHA mode : SHA256" },
4873cb28812SJohnny Huang 	{ 0, 12, 2,  2, "SHA mode : SHA384" },
4883cb28812SJohnny Huang 	{ 0, 12, 2,  3, "SHA mode : SHA512" },
489bb34a7bfSJohnny Huang 	{ 0, 14, 1,  0, "Disable Patch code" },
490bb34a7bfSJohnny Huang 	{ 0, 14, 1,  1, "Enable Patch code" },
491bb34a7bfSJohnny Huang 	{ 0, 15, 1,  OTP_REG_RESERVED, "" },
4923cb28812SJohnny Huang 	{ 0, 16, 6,  OTP_REG_VALUE, "Secure Region size (DW): 0x%x" },
4933cb28812SJohnny Huang 	{ 0, 22, 1,  0, "Secure Region : Writable" },
4943cb28812SJohnny Huang 	{ 0, 22, 1,  1, "Secure Region : Write Protect" },
4953cb28812SJohnny Huang 	{ 0, 23, 1,  0, "User Region : Writable" },
4963cb28812SJohnny Huang 	{ 0, 23, 1,  1, "User Region : Write Protect" },
4973cb28812SJohnny Huang 	{ 0, 24, 1,  0, "Configure Region : Writable" },
4983cb28812SJohnny Huang 	{ 0, 24, 1,  1, "Configure Region : Write Protect" },
4993cb28812SJohnny Huang 	{ 0, 25, 1,  0, "OTP strap Region : Writable" },
5003cb28812SJohnny Huang 	{ 0, 25, 1,  1, "OTP strap Region : Write Protect" },
5013cb28812SJohnny Huang 	{ 0, 26, 1,  0, "Disable Copy Boot Image to Internal SRAM" },
5023cb28812SJohnny Huang 	{ 0, 26, 1,  1, "Copy Boot Image to Internal SRAM" },
5033cb28812SJohnny Huang 	{ 0, 27, 1,  0, "Disable image encryption" },
5043cb28812SJohnny Huang 	{ 0, 27, 1,  1, "Enable image encryption" },
5053cb28812SJohnny Huang 	{ 0, 28, 1,  OTP_REG_RESERVED, "" },
5063cb28812SJohnny Huang 	{ 0, 29, 1,  0, "OTP key retire Region : Writable" },
5073cb28812SJohnny Huang 	{ 0, 29, 1,  1, "OTP key retire Region : Write Protect" },
5083cb28812SJohnny Huang 	{ 0, 30, 1,  0, "Data region redundancy repair disable" },
5093cb28812SJohnny Huang 	{ 0, 30, 1,  1, "Data region redundancy repair enable" },
5103cb28812SJohnny Huang 	{ 0, 31, 1,  0, "OTP memory lock disable" },
5113cb28812SJohnny Huang 	{ 0, 31, 1,  1, "OTP memory lock enable" },
51291448c03SJohnny Huang 	{ 2, 0,  16, OTP_REG_VALUE, "Vender ID : 0x%x" },
51391448c03SJohnny Huang 	{ 2, 16, 16, OTP_REG_VALUE, "Key Revision : 0x%x" },
51491448c03SJohnny Huang 	{ 3, 0,  16, OTP_REG_VALUE, "Secure boot header offset : 0x%x" },
51573f11549SJohnny Huang 	{ 4, 0,  8,  OTP_REG_VALID_BIT, "Keys valid  : %s" },
51673f11549SJohnny Huang 	{ 4, 16, 8,  OTP_REG_VALID_BIT, "Keys retire  : %s" },
51791448c03SJohnny Huang 	{ 5, 0,  32, OTP_REG_VALUE, "User define data, random number low : 0x%x" },
51891448c03SJohnny Huang 	{ 6, 0,  32, OTP_REG_VALUE, "User define data, random number high : 0x%x" },
51991448c03SJohnny Huang 	{ 7, 0,  1,  0, "Force enable PCI bus to AHB bus bridge" },
52091448c03SJohnny Huang 	{ 7, 0,  1,  1, "Force disable PCI bus to AHB bus bridge" },
52191448c03SJohnny Huang 	{ 7, 1,  1,  0, "Force enable UART5 debug port function" },
52291448c03SJohnny Huang 	{ 7, 1,  1,  1, "Force disable UART5 debug port function" },
52391448c03SJohnny Huang 	{ 7, 2,  1,  0, "Force enable XDMA function" },
52491448c03SJohnny Huang 	{ 7, 2,  1,  1, "Force disable XDMA function" },
52591448c03SJohnny Huang 	{ 7, 3,  1,  0, "Force enable APB to PCIE device bridge" },
52691448c03SJohnny Huang 	{ 7, 3,  1,  1, "Force disable APB to PCIE device bridge" },
52791448c03SJohnny Huang 	{ 7, 4,  1,  0, "Force enable APB to PCIE bridge config access" },
52891448c03SJohnny Huang 	{ 7, 4,  1,  1, "Force disable APB to PCIE bridge config access" },
52991448c03SJohnny Huang 	{ 7, 5,  1,  0, "Force enable PCIE bus trace buffer" },
53091448c03SJohnny Huang 	{ 7, 5,  1,  1, "Force disable PCIE bus trace buffer" },
53191448c03SJohnny Huang 	{ 7, 6,  1,  0, "Force enable the capability for PCIE device port as a Root Complex" },
53291448c03SJohnny Huang 	{ 7, 6,  1,  1, "Force disable the capability for PCIE device port as a Root Complex" },
53391448c03SJohnny Huang 	{ 7, 16, 1,  0, "Force enable ESPI bus to AHB bus bridge" },
53491448c03SJohnny Huang 	{ 7, 16, 1,  1, "Force disable ESPI bus to AHB bus bridge" },
53591448c03SJohnny Huang 	{ 7, 17, 1,  0, "Force enable LPC bus to AHB bus bridge1" },
53691448c03SJohnny Huang 	{ 7, 17, 1,  1, "Force disable LPC bus to AHB bus bridge1" },
53791448c03SJohnny Huang 	{ 7, 18, 1,  0, "Force enable LPC bus to AHB bus bridge2" },
53891448c03SJohnny Huang 	{ 7, 18, 1,  1, "Force disable LPC bus to AHB bus bridge2" },
53991448c03SJohnny Huang 	{ 7, 19, 1,  0, "Force enable UART1 debug port function" },
54091448c03SJohnny Huang 	{ 7, 19, 1,  1, "Force disable UART1 debug port function" },
54191448c03SJohnny Huang 	{ 7, 31, 1,  0, "Disable chip security setting" },
54291448c03SJohnny Huang 	{ 7, 31, 1,  1, "Enable chip security setting" },
54391448c03SJohnny Huang 	{ 8, 0,  32, OTP_REG_VALUE, "Redundancy Repair : 0x%x" },
54491448c03SJohnny Huang 	{ 10, 0, 32, OTP_REG_VALUE, "Manifest ID low : 0x%x" },
545bb34a7bfSJohnny Huang 	{ 11, 0, 32, OTP_REG_VALUE, "Manifest ID high : 0x%x" },
546bb34a7bfSJohnny Huang 	{ 14, 0, 11, OTP_REG_VALUE, "Patch code location (DW): 0x%x" },
547bb34a7bfSJohnny Huang 	{ 14, 11, 6, OTP_REG_VALUE, "Patch code size (DW): 0x%x" }
548b458cd62SJohnny Huang };
5499a4fe690SJohnny Huang 
55079e42a59SJoel Stanley static const struct otpkey_type a0_key_type[] = {
5519a4fe690SJohnny Huang 	{0, OTP_KEY_TYPE_AES,   0, "AES-256 as OEM platform key for image encryption/decryption"},
5529a4fe690SJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
5539a4fe690SJohnny Huang 	{4, OTP_KEY_TYPE_HMAC,  1, "HMAC as encrypted OEM HMAC keys in Mode 1"},
5549a4fe690SJohnny Huang 	{8, OTP_KEY_TYPE_RSA,   1, "RSA-public as OEM DSS public keys in Mode 2"},
5559a4fe690SJohnny Huang 	{9, OTP_KEY_TYPE_RSA,   0, "RSA-public as SOC public key"},
5569a4fe690SJohnny Huang 	{10, OTP_KEY_TYPE_RSA,  0, "RSA-public as AES key decryption key"},
5579a4fe690SJohnny Huang 	{13, OTP_KEY_TYPE_RSA,  0, "RSA-private as SOC private key"},
5589a4fe690SJohnny Huang 	{14, OTP_KEY_TYPE_RSA,  0, "RSA-private as AES key decryption key"},
5599a4fe690SJohnny Huang };
5609a4fe690SJohnny Huang 
56179e42a59SJoel Stanley static const struct otpkey_type a1_key_type[] = {
5629a4fe690SJohnny Huang 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
5639a4fe690SJohnny Huang 	{2, OTP_KEY_TYPE_AES,   1, "AES-256 as OEM platform key for image encryption/decryption in Mode 2 or AES-256 as OEM DSS keys for Mode GCM"},
5649a4fe690SJohnny Huang 	{8, OTP_KEY_TYPE_RSA,   1, "RSA-public as OEM DSS public keys in Mode 2"},
5659a4fe690SJohnny Huang 	{10, OTP_KEY_TYPE_RSA,  0, "RSA-public as AES key decryption key"},
5669a4fe690SJohnny Huang 	{14, OTP_KEY_TYPE_RSA,  0, "RSA-private as AES key decryption key"},
5679a4fe690SJohnny Huang };
5689a4fe690SJohnny Huang 
5699a4fe690SJohnny Huang static uint32_t chip_version(void)
5709a4fe690SJohnny Huang {
5719a4fe690SJohnny Huang 	uint32_t rev_id;
5729a4fe690SJohnny Huang 
5739a4fe690SJohnny Huang 	rev_id = (readl(0x1e6e2004) >> 16) & 0xff ;
5749a4fe690SJohnny Huang 
5759a4fe690SJohnny Huang 	return rev_id;
5769a4fe690SJohnny Huang }
5779a4fe690SJohnny Huang 
5783d3688adSJohnny Huang static void wait_complete(void)
5793d3688adSJohnny Huang {
5803d3688adSJohnny Huang 	int reg;
5813d3688adSJohnny Huang 
5823d3688adSJohnny Huang 	do {
5833d3688adSJohnny Huang 		reg = readl(OTP_STATUS);
5843d3688adSJohnny Huang 	} while ((reg & 0x6) != 0x6);
5853d3688adSJohnny Huang }
5863d3688adSJohnny Huang 
5872a856b9aSJohnny Huang static void otp_read_data(uint32_t offset, uint32_t *data)
58869d5fd8fSJohnny Huang {
5893d3688adSJohnny Huang 	writel(offset, OTP_ADDR); //Read address
5903d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
5913d3688adSJohnny Huang 	wait_complete();
5923d3688adSJohnny Huang 	data[0] = readl(OTP_COMPARE_1);
5933d3688adSJohnny Huang 	data[1] = readl(OTP_COMPARE_2);
59469d5fd8fSJohnny Huang }
59569d5fd8fSJohnny Huang 
5962a856b9aSJohnny Huang static void otp_read_config(uint32_t offset, uint32_t *data)
59769d5fd8fSJohnny Huang {
59869d5fd8fSJohnny Huang 	int config_offset;
59969d5fd8fSJohnny Huang 
60069d5fd8fSJohnny Huang 	config_offset = 0x800;
60169d5fd8fSJohnny Huang 	config_offset |= (offset / 8) * 0x200;
60269d5fd8fSJohnny Huang 	config_offset |= (offset % 8) * 0x2;
60369d5fd8fSJohnny Huang 
6043d3688adSJohnny Huang 	writel(config_offset, OTP_ADDR);  //Read address
6053d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
6063d3688adSJohnny Huang 	wait_complete();
6073d3688adSJohnny Huang 	data[0] = readl(OTP_COMPARE_1);
60869d5fd8fSJohnny Huang }
60969d5fd8fSJohnny Huang 
61069d5fd8fSJohnny Huang static int otp_print_config(uint32_t offset, int dw_count)
61169d5fd8fSJohnny Huang {
61269d5fd8fSJohnny Huang 	int i;
61369d5fd8fSJohnny Huang 	uint32_t ret[1];
61469d5fd8fSJohnny Huang 
61569d5fd8fSJohnny Huang 	if (offset + dw_count > 32)
6162a856b9aSJohnny Huang 		return OTP_USAGE;
61769d5fd8fSJohnny Huang 	for (i = offset; i < offset + dw_count; i ++) {
61869d5fd8fSJohnny Huang 		otp_read_config(i, ret);
619a6af4a17SJohnny Huang 		printf("OTPCFG%X: %08X\n", i, ret[0]);
62069d5fd8fSJohnny Huang 	}
62169d5fd8fSJohnny Huang 	printf("\n");
6222a856b9aSJohnny Huang 	return OTP_SUCCESS;
62369d5fd8fSJohnny Huang }
62469d5fd8fSJohnny Huang 
62569d5fd8fSJohnny Huang static int otp_print_data(uint32_t offset, int dw_count)
62669d5fd8fSJohnny Huang {
62769d5fd8fSJohnny Huang 	int i;
62869d5fd8fSJohnny Huang 	uint32_t ret[2];
62969d5fd8fSJohnny Huang 
63069d5fd8fSJohnny Huang 	if (offset + dw_count > 2048 || offset % 4 != 0)
6312a856b9aSJohnny Huang 		return OTP_USAGE;
63269d5fd8fSJohnny Huang 	for (i = offset; i < offset + dw_count; i += 2) {
63369d5fd8fSJohnny Huang 		otp_read_data(i, ret);
63469d5fd8fSJohnny Huang 		if (i % 4 == 0)
63569d5fd8fSJohnny Huang 			printf("%03X: %08X %08X ", i * 4, ret[0], ret[1]);
63669d5fd8fSJohnny Huang 		else
63769d5fd8fSJohnny Huang 			printf("%08X %08X\n", ret[0], ret[1]);
63869d5fd8fSJohnny Huang 
63969d5fd8fSJohnny Huang 	}
64069d5fd8fSJohnny Huang 	printf("\n");
6412a856b9aSJohnny Huang 	return OTP_SUCCESS;
64269d5fd8fSJohnny Huang }
64369d5fd8fSJohnny Huang 
64469d5fd8fSJohnny Huang static int otp_compare(uint32_t otp_addr, uint32_t addr)
64569d5fd8fSJohnny Huang {
64669d5fd8fSJohnny Huang 	uint32_t ret;
64769d5fd8fSJohnny Huang 	uint32_t *buf;
64869d5fd8fSJohnny Huang 
64969d5fd8fSJohnny Huang 	buf = map_physmem(addr, 16, MAP_WRBACK);
65069d5fd8fSJohnny Huang 	printf("%08X\n", buf[0]);
65169d5fd8fSJohnny Huang 	printf("%08X\n", buf[1]);
65269d5fd8fSJohnny Huang 	printf("%08X\n", buf[2]);
65369d5fd8fSJohnny Huang 	printf("%08X\n", buf[3]);
6543d3688adSJohnny Huang 	writel(otp_addr, OTP_ADDR); //Compare address
6553d3688adSJohnny Huang 	writel(buf[0], OTP_COMPARE_1); //Compare data 1
6563d3688adSJohnny Huang 	writel(buf[1], OTP_COMPARE_2); //Compare data 2
6573d3688adSJohnny Huang 	writel(buf[2], OTP_COMPARE_3); //Compare data 3
6583d3688adSJohnny Huang 	writel(buf[3], OTP_COMPARE_4); //Compare data 4
6593d3688adSJohnny Huang 	writel(0x23b1e363, OTP_COMMAND); //Compare command
6603d3688adSJohnny Huang 	wait_complete();
6613d3688adSJohnny Huang 	ret = readl(OTP_STATUS); //Compare command
66269d5fd8fSJohnny Huang 	if (ret & 0x1)
66369d5fd8fSJohnny Huang 		return 0;
66469d5fd8fSJohnny Huang 	else
66569d5fd8fSJohnny Huang 		return -1;
66669d5fd8fSJohnny Huang }
66769d5fd8fSJohnny Huang 
66869d5fd8fSJohnny Huang static void otp_write(uint32_t otp_addr, uint32_t data)
66969d5fd8fSJohnny Huang {
6703d3688adSJohnny Huang 	writel(otp_addr, OTP_ADDR); //write address
6713d3688adSJohnny Huang 	writel(data, OTP_COMPARE_1); //write data
6723d3688adSJohnny Huang 	writel(0x23b1e362, OTP_COMMAND); //write command
6733d3688adSJohnny Huang 	wait_complete();
67469d5fd8fSJohnny Huang }
67569d5fd8fSJohnny Huang 
67669d5fd8fSJohnny Huang static void otp_prog(uint32_t otp_addr, uint32_t prog_bit)
67769d5fd8fSJohnny Huang {
6783d3688adSJohnny Huang 	writel(otp_addr, OTP_ADDR); //write address
6793d3688adSJohnny Huang 	writel(prog_bit, OTP_COMPARE_1); //write data
6803d3688adSJohnny Huang 	writel(0x23b1e364, OTP_COMMAND); //write command
6813d3688adSJohnny Huang 	wait_complete();
68269d5fd8fSJohnny Huang }
68369d5fd8fSJohnny Huang 
684a6d0d645SJohnny Huang static int verify_bit(uint32_t otp_addr, int bit_offset, int value)
68569d5fd8fSJohnny Huang {
68630a8c590SJohnny Huang 	uint32_t ret[2];
68769d5fd8fSJohnny Huang 
68830a8c590SJohnny Huang 	if (otp_addr % 2 == 0)
6893d3688adSJohnny Huang 		writel(otp_addr, OTP_ADDR); //Read address
69030a8c590SJohnny Huang 	else
6913d3688adSJohnny Huang 		writel(otp_addr - 1, OTP_ADDR); //Read address
69230a8c590SJohnny Huang 
6933d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
6943d3688adSJohnny Huang 	wait_complete();
6953d3688adSJohnny Huang 	ret[0] = readl(OTP_COMPARE_1);
6963d3688adSJohnny Huang 	ret[1] = readl(OTP_COMPARE_2);
697a6d0d645SJohnny Huang 	// printf("verify_bit = %x\n", ret);
69830a8c590SJohnny Huang 	if (otp_addr % 2 == 0) {
69930a8c590SJohnny Huang 		if (((ret[0] >> bit_offset) & 1) == value)
70069d5fd8fSJohnny Huang 			return 0;
70169d5fd8fSJohnny Huang 		else
70269d5fd8fSJohnny Huang 			return -1;
70330a8c590SJohnny Huang 	} else {
70430a8c590SJohnny Huang 		if (((ret[1] >> bit_offset) & 1) == value)
70530a8c590SJohnny Huang 			return 0;
70630a8c590SJohnny Huang 		else
70730a8c590SJohnny Huang 			return -1;
70830a8c590SJohnny Huang 	}
70930a8c590SJohnny Huang 
71069d5fd8fSJohnny Huang }
71169d5fd8fSJohnny Huang 
712696656c6SJohnny Huang static uint32_t verify_dw(uint32_t otp_addr, uint32_t *value, uint32_t *ignore, uint32_t *compare, int size)
7134c1c9b35SJohnny Huang {
7144c1c9b35SJohnny Huang 	uint32_t ret[2];
7154c1c9b35SJohnny Huang 
7164c1c9b35SJohnny Huang 	otp_addr &= ~(1 << 15);
7174c1c9b35SJohnny Huang 
7184c1c9b35SJohnny Huang 	if (otp_addr % 2 == 0)
7193d3688adSJohnny Huang 		writel(otp_addr, OTP_ADDR); //Read address
7204c1c9b35SJohnny Huang 	else
7213d3688adSJohnny Huang 		writel(otp_addr - 1, OTP_ADDR); //Read address
7223d3688adSJohnny Huang 	writel(0x23b1e361, OTP_COMMAND); //trigger read
7233d3688adSJohnny Huang 	wait_complete();
7243d3688adSJohnny Huang 	ret[0] = readl(OTP_COMPARE_1);
7253d3688adSJohnny Huang 	ret[1] = readl(OTP_COMPARE_2);
7264c1c9b35SJohnny Huang 	if (size == 1) {
7274c1c9b35SJohnny Huang 		if (otp_addr % 2 == 0) {
7284c1c9b35SJohnny Huang 			// printf("check %x : %x = %x\n", otp_addr, ret[0], value[0]);
729696656c6SJohnny Huang 			if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0])) {
7304c1c9b35SJohnny Huang 				compare[0] = 0;
7314c1c9b35SJohnny Huang 				return 0;
7324c1c9b35SJohnny Huang 			} else {
7334c1c9b35SJohnny Huang 				compare[0] = value[0] ^ ret[0];
7344c1c9b35SJohnny Huang 				return -1;
7354c1c9b35SJohnny Huang 			}
7364c1c9b35SJohnny Huang 
7374c1c9b35SJohnny Huang 		} else {
7384c1c9b35SJohnny Huang 			// printf("check %x : %x = %x\n", otp_addr, ret[1], value[0]);
739696656c6SJohnny Huang 			if ((value[0] & ~ignore[0]) == (ret[1] & ~ignore[0])) {
7404c1c9b35SJohnny Huang 				compare[0] = ~0;
7414c1c9b35SJohnny Huang 				return 0;
7424c1c9b35SJohnny Huang 			} else {
743d90825e2SJohnny Huang 				compare[0] = ~(value[0] ^ ret[1]);
7444c1c9b35SJohnny Huang 				return -1;
7454c1c9b35SJohnny Huang 			}
7464c1c9b35SJohnny Huang 		}
7474c1c9b35SJohnny Huang 	} else if (size == 2) {
7484c1c9b35SJohnny Huang 		// otp_addr should be even
749696656c6SJohnny Huang 		if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0]) && (value[1] & ~ignore[1]) == (ret[1] & ~ignore[1])) {
7504c1c9b35SJohnny Huang 			// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
7514c1c9b35SJohnny Huang 			// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
7524c1c9b35SJohnny Huang 			compare[0] = 0;
7534c1c9b35SJohnny Huang 			compare[1] = ~0;
7544c1c9b35SJohnny Huang 			return 0;
7554c1c9b35SJohnny Huang 		} else {
7564c1c9b35SJohnny Huang 			// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
7574c1c9b35SJohnny Huang 			// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
7584c1c9b35SJohnny Huang 			compare[0] = value[0] ^ ret[0];
7594c1c9b35SJohnny Huang 			compare[1] = ~(value[1] ^ ret[1]);
7604c1c9b35SJohnny Huang 			return -1;
7614c1c9b35SJohnny Huang 		}
7624c1c9b35SJohnny Huang 	} else {
7634c1c9b35SJohnny Huang 		return -1;
7644c1c9b35SJohnny Huang 	}
7654c1c9b35SJohnny Huang }
7664c1c9b35SJohnny Huang 
7677e22f42dSJohnny Huang static void otp_soak(int soak)
768d90825e2SJohnny Huang {
769de6fbf1cSJohnny Huang 	switch (soak) {
770de6fbf1cSJohnny Huang 	case 0: //default
771de6fbf1cSJohnny Huang 		otp_write(0x3000, 0x0); // Write MRA
772de6fbf1cSJohnny Huang 		otp_write(0x5000, 0x0); // Write MRB
773de6fbf1cSJohnny Huang 		otp_write(0x1000, 0x0); // Write MR
774de6fbf1cSJohnny Huang 		break;
775de6fbf1cSJohnny Huang 	case 1: //normal program
776de6fbf1cSJohnny Huang 		otp_write(0x3000, 0x4021); // Write MRA
777de6fbf1cSJohnny Huang 		otp_write(0x5000, 0x302f); // Write MRB
778de6fbf1cSJohnny Huang 		otp_write(0x1000, 0x4020); // Write MR
779de6fbf1cSJohnny Huang 		writel(0x04190760, OTP_TIMING);
780de6fbf1cSJohnny Huang 		break;
781de6fbf1cSJohnny Huang 	case 2: //soak program
782d90825e2SJohnny Huang 		otp_write(0x3000, 0x4021); // Write MRA
783d90825e2SJohnny Huang 		otp_write(0x5000, 0x1027); // Write MRB
784d90825e2SJohnny Huang 		otp_write(0x1000, 0x4820); // Write MR
785de6fbf1cSJohnny Huang 		writel(0x041930d4, OTP_TIMING);
786de6fbf1cSJohnny Huang 		break;
787d90825e2SJohnny Huang 	}
788de6fbf1cSJohnny Huang 
7893d3688adSJohnny Huang 	wait_complete();
790d90825e2SJohnny Huang }
791d90825e2SJohnny Huang 
792696656c6SJohnny Huang static void otp_prog_dw(uint32_t value, uint32_t ignore, uint32_t prog_address)
793d90825e2SJohnny Huang {
794d90825e2SJohnny Huang 	int j, bit_value, prog_bit;
795d90825e2SJohnny Huang 
796d90825e2SJohnny Huang 	for (j = 0; j < 32; j++) {
797696656c6SJohnny Huang 		if ((ignore >> j) & 0x1)
798d90825e2SJohnny Huang 			continue;
799d90825e2SJohnny Huang 		bit_value = (value >> j) & 0x1;
800d90825e2SJohnny Huang 		if (prog_address % 2 == 0) {
801d90825e2SJohnny Huang 			if (bit_value)
802d90825e2SJohnny Huang 				prog_bit = ~(0x1 << j);
803d90825e2SJohnny Huang 			else
804d90825e2SJohnny Huang 				continue;
805d90825e2SJohnny Huang 		} else {
806d90825e2SJohnny Huang 			prog_address |= 1 << 15;
807d90825e2SJohnny Huang 			if (bit_value)
808d90825e2SJohnny Huang 				continue;
809d90825e2SJohnny Huang 			else
810d90825e2SJohnny Huang 				prog_bit = 0x1 << j;
811d90825e2SJohnny Huang 		}
812d90825e2SJohnny Huang 		otp_prog(prog_address, prog_bit);
813d90825e2SJohnny Huang 	}
814d90825e2SJohnny Huang }
815d90825e2SJohnny Huang 
81654552c69SJohnny Huang static int otp_prog_verify_2dw(uint32_t *data, uint32_t *buf, uint32_t *ignore_mask, uint32_t prog_address)
81754552c69SJohnny Huang {
81854552c69SJohnny Huang 	int pass;
81954552c69SJohnny Huang 	int i;
82054552c69SJohnny Huang 	uint32_t data0_masked;
82154552c69SJohnny Huang 	uint32_t data1_masked;
82254552c69SJohnny Huang 	uint32_t buf0_masked;
82354552c69SJohnny Huang 	uint32_t buf1_masked;
82454552c69SJohnny Huang 	uint32_t compare[2];
82554552c69SJohnny Huang 
82654552c69SJohnny Huang 	data0_masked = data[0]  & ~ignore_mask[0];
82754552c69SJohnny Huang 	buf0_masked  = buf[0] & ~ignore_mask[0];
82854552c69SJohnny Huang 	data1_masked = data[1]  & ~ignore_mask[1];
82954552c69SJohnny Huang 	buf1_masked  = buf[1] & ~ignore_mask[1];
83054552c69SJohnny Huang 	if ((data0_masked == buf0_masked) && (data1_masked == buf1_masked))
83154552c69SJohnny Huang 		return 0;
83254552c69SJohnny Huang 
83354552c69SJohnny Huang 	otp_soak(1);
83454552c69SJohnny Huang 	if (data0_masked != buf0_masked)
83554552c69SJohnny Huang 		otp_prog_dw(buf[0], ignore_mask[0], prog_address);
83654552c69SJohnny Huang 	if (data1_masked != buf1_masked)
83754552c69SJohnny Huang 		otp_prog_dw(buf[1], ignore_mask[1], prog_address + 1);
83854552c69SJohnny Huang 
83954552c69SJohnny Huang 	pass = 0;
84054552c69SJohnny Huang 	for (i = 0; i < RETRY; i++) {
84154552c69SJohnny Huang 		if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) {
84254552c69SJohnny Huang 			otp_soak(2);
84354552c69SJohnny Huang 			if (compare[0] != 0) {
84454552c69SJohnny Huang 				otp_prog_dw(compare[0], ignore_mask[0], prog_address);
84554552c69SJohnny Huang 			}
84654552c69SJohnny Huang 			if (compare[1] != ~0) {
84754552c69SJohnny Huang 				otp_prog_dw(compare[1], ignore_mask[0], prog_address + 1);
84854552c69SJohnny Huang 			}
84954552c69SJohnny Huang 			if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) {
85054552c69SJohnny Huang 				otp_soak(1);
85154552c69SJohnny Huang 			} else {
85254552c69SJohnny Huang 				pass = 1;
85354552c69SJohnny Huang 				break;
85454552c69SJohnny Huang 			}
85554552c69SJohnny Huang 		} else {
85654552c69SJohnny Huang 			pass = 1;
85754552c69SJohnny Huang 			break;
85854552c69SJohnny Huang 		}
85954552c69SJohnny Huang 	}
86054552c69SJohnny Huang 
86154552c69SJohnny Huang 	if (!pass) {
86254552c69SJohnny Huang 		otp_soak(0);
86354552c69SJohnny Huang 		return OTP_FAILURE;
86454552c69SJohnny Huang 	}
86554552c69SJohnny Huang 	return OTP_SUCCESS;
86654552c69SJohnny Huang }
86754552c69SJohnny Huang 
86876d13988SJohnny Huang 
869541eb887SJohnny Huang static void otp_strap_status(struct otpstrap_status *otpstrap)
87076d13988SJohnny Huang {
87176d13988SJohnny Huang 	uint32_t OTPSTRAP_RAW[2];
872*5010032bSJohnny Huang 	int strap_end;
87376d13988SJohnny Huang 	int i, j;
87476d13988SJohnny Huang 
875*5010032bSJohnny Huang 	if (info_cb.version == OTP_AST2600A0) {
87676d13988SJohnny Huang 		for (j = 0; j < 64; j++) {
87776d13988SJohnny Huang 			otpstrap[j].value = 0;
87876d13988SJohnny Huang 			otpstrap[j].remain_times = 7;
87976d13988SJohnny Huang 			otpstrap[j].writeable_option = -1;
88076d13988SJohnny Huang 			otpstrap[j].protected = 0;
88176d13988SJohnny Huang 		}
882*5010032bSJohnny Huang 		strap_end = 30;
883*5010032bSJohnny Huang 	} else {
884*5010032bSJohnny Huang 		for (j = 0; j < 64; j++) {
885*5010032bSJohnny Huang 			otpstrap[j].value = 0;
886*5010032bSJohnny Huang 			otpstrap[j].remain_times = 6;
887*5010032bSJohnny Huang 			otpstrap[j].writeable_option = -1;
888*5010032bSJohnny Huang 			otpstrap[j].reg_protected = 0;
889*5010032bSJohnny Huang 			otpstrap[j].protected = 0;
890*5010032bSJohnny Huang 		}
891*5010032bSJohnny Huang 		strap_end = 28;
892*5010032bSJohnny Huang 	}
89376d13988SJohnny Huang 
894*5010032bSJohnny Huang 	for (i = 16; i < strap_end; i += 2) {
89576d13988SJohnny Huang 		int option = (i - 16) / 2;
89676d13988SJohnny Huang 		otp_read_config(i, &OTPSTRAP_RAW[0]);
89776d13988SJohnny Huang 		otp_read_config(i + 1, &OTPSTRAP_RAW[1]);
89876d13988SJohnny Huang 		for (j = 0; j < 32; j++) {
89976d13988SJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[0] >> j) & 0x1);
90076d13988SJohnny Huang 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
90176d13988SJohnny Huang 				otpstrap[j].writeable_option = option;
90276d13988SJohnny Huang 			}
90376d13988SJohnny Huang 			if (bit_value == 1)
90476d13988SJohnny Huang 				otpstrap[j].remain_times --;
90576d13988SJohnny Huang 			otpstrap[j].value ^= bit_value;
90676d13988SJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
90776d13988SJohnny Huang 		}
90876d13988SJohnny Huang 		for (j = 32; j < 64; j++) {
90976d13988SJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1);
91076d13988SJohnny Huang 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
91176d13988SJohnny Huang 				otpstrap[j].writeable_option = option;
91276d13988SJohnny Huang 			}
91376d13988SJohnny Huang 			if (bit_value == 1)
91476d13988SJohnny Huang 				otpstrap[j].remain_times --;
91576d13988SJohnny Huang 			otpstrap[j].value ^= bit_value;
91676d13988SJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
91776d13988SJohnny Huang 		}
91876d13988SJohnny Huang 	}
919*5010032bSJohnny Huang 
920*5010032bSJohnny Huang 	if (info_cb.version != OTP_AST2600A0) {
921*5010032bSJohnny Huang 		otp_read_config(28, &OTPSTRAP_RAW[0]);
922*5010032bSJohnny Huang 		otp_read_config(29, &OTPSTRAP_RAW[1]);
923*5010032bSJohnny Huang 		for (j = 0; j < 32; j++) {
924*5010032bSJohnny Huang 			if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
925*5010032bSJohnny Huang 				otpstrap[j].reg_protected = 1;
926*5010032bSJohnny Huang 		}
927*5010032bSJohnny Huang 		for (j = 32; j < 64; j++) {
928*5010032bSJohnny Huang 			if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
929*5010032bSJohnny Huang 				otpstrap[j].reg_protected = 1;
930*5010032bSJohnny Huang 		}
931*5010032bSJohnny Huang 
932*5010032bSJohnny Huang 	}
933*5010032bSJohnny Huang 
93476d13988SJohnny Huang 	otp_read_config(30, &OTPSTRAP_RAW[0]);
93576d13988SJohnny Huang 	otp_read_config(31, &OTPSTRAP_RAW[1]);
93676d13988SJohnny Huang 	for (j = 0; j < 32; j++) {
93776d13988SJohnny Huang 		if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
93876d13988SJohnny Huang 			otpstrap[j].protected = 1;
93976d13988SJohnny Huang 	}
94076d13988SJohnny Huang 	for (j = 32; j < 64; j++) {
94176d13988SJohnny Huang 		if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
94276d13988SJohnny Huang 			otpstrap[j].protected = 1;
94376d13988SJohnny Huang 	}
94476d13988SJohnny Huang }
94576d13988SJohnny Huang 
946696656c6SJohnny Huang static int otp_print_conf_image(struct otp_image_layout *image_layout)
94769d5fd8fSJohnny Huang {
94879e42a59SJoel Stanley 	const struct otpconf_info *conf_info = info_cb.conf_info;
949696656c6SJohnny Huang 	uint32_t *OTPCFG = (uint32_t *)image_layout->conf;
950696656c6SJohnny Huang 	uint32_t *OTPCFG_IGNORE = (uint32_t *)image_layout->conf_ignore;
951b458cd62SJohnny Huang 	uint32_t mask;
952b458cd62SJohnny Huang 	uint32_t dw_offset;
953b458cd62SJohnny Huang 	uint32_t bit_offset;
954b458cd62SJohnny Huang 	uint32_t otp_value;
955696656c6SJohnny Huang 	uint32_t otp_ignore;
956b458cd62SJohnny Huang 	int fail = 0;
95773f11549SJohnny Huang 	char valid_bit[20];
95866f2f8e5SJohnny Huang 	int i;
95973f11549SJohnny Huang 	int j;
96066f2f8e5SJohnny Huang 
961737ed20bSJohnny Huang 	printf("DW    BIT        Value       Description\n");
96266f2f8e5SJohnny Huang 	printf("__________________________________________________________________________\n");
9633cb28812SJohnny Huang 	for (i = 0; i < info_cb.conf_info_len; i++) {
9643cb28812SJohnny Huang 		dw_offset = conf_info[i].dw_offset;
9653cb28812SJohnny Huang 		bit_offset = conf_info[i].bit_offset;
9663cb28812SJohnny Huang 		mask = BIT(conf_info[i].length) - 1;
967b458cd62SJohnny Huang 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
968696656c6SJohnny Huang 		otp_ignore = (OTPCFG_IGNORE[dw_offset] >> bit_offset) & mask;
969b458cd62SJohnny Huang 
970696656c6SJohnny Huang 		if (otp_ignore == mask) {
971b458cd62SJohnny Huang 			continue;
972696656c6SJohnny Huang 		} else if (otp_ignore != 0) {
973b458cd62SJohnny Huang 			fail = 1;
974b458cd62SJohnny Huang 		}
975b458cd62SJohnny Huang 
9763cb28812SJohnny Huang 		if ((otp_value != conf_info[i].value) &&
9773cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_RESERVED &&
9783cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALUE &&
9793cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALID_BIT)
980b458cd62SJohnny Huang 			continue;
981b458cd62SJohnny Huang 		printf("0x%-4X", dw_offset);
982b458cd62SJohnny Huang 
9833cb28812SJohnny Huang 		if (conf_info[i].length == 1) {
9843cb28812SJohnny Huang 			printf("0x%-9X", conf_info[i].bit_offset);
98566f2f8e5SJohnny Huang 		} else {
986b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
9873cb28812SJohnny Huang 			       conf_info[i].bit_offset + conf_info[i].length - 1,
9883cb28812SJohnny Huang 			       conf_info[i].bit_offset);
98966f2f8e5SJohnny Huang 		}
990b458cd62SJohnny Huang 		printf("0x%-10x", otp_value);
991b458cd62SJohnny Huang 
992b458cd62SJohnny Huang 		if (fail) {
993696656c6SJohnny Huang 			printf("Ignore mask error\n");
994b458cd62SJohnny Huang 		} else {
9953cb28812SJohnny Huang 			if (conf_info[i].value == OTP_REG_RESERVED) {
996b458cd62SJohnny Huang 				printf("Reserved\n");
9973cb28812SJohnny Huang 			} else if (conf_info[i].value == OTP_REG_VALUE) {
9983cb28812SJohnny Huang 				printf(conf_info[i].information, otp_value);
999b458cd62SJohnny Huang 				printf("\n");
10003cb28812SJohnny Huang 			} else if (conf_info[i].value == OTP_REG_VALID_BIT) {
1001b458cd62SJohnny Huang 				if (otp_value != 0) {
100273f11549SJohnny Huang 					for (j = 0; j < 7; j++) {
100373f11549SJohnny Huang 						if (otp_value == (1 << j)) {
100473f11549SJohnny Huang 							valid_bit[j * 2] = '1';
1005b458cd62SJohnny Huang 						} else {
100673f11549SJohnny Huang 							valid_bit[j * 2] = '0';
100773f11549SJohnny Huang 						}
100873f11549SJohnny Huang 						valid_bit[j * 2 + 1] = ' ';
100973f11549SJohnny Huang 					}
101073f11549SJohnny Huang 					valid_bit[15] = 0;
101173f11549SJohnny Huang 				} else {
101273f11549SJohnny Huang 					strcpy(valid_bit, "0 0 0 0 0 0 0 0\0");
1013b458cd62SJohnny Huang 				}
10143cb28812SJohnny Huang 				printf(conf_info[i].information, valid_bit);
1015b458cd62SJohnny Huang 				printf("\n");
1016b458cd62SJohnny Huang 			} else {
10173cb28812SJohnny Huang 				printf("%s\n", conf_info[i].information);
1018b458cd62SJohnny Huang 			}
1019b458cd62SJohnny Huang 		}
1020b458cd62SJohnny Huang 	}
1021b458cd62SJohnny Huang 
1022b458cd62SJohnny Huang 	if (fail)
1023b458cd62SJohnny Huang 		return OTP_FAILURE;
1024b458cd62SJohnny Huang 
102566f2f8e5SJohnny Huang 	return OTP_SUCCESS;
102666f2f8e5SJohnny Huang }
102766f2f8e5SJohnny Huang 
10282d4b0742SJohnny Huang static int otp_print_conf_info(int input_offset)
102966f2f8e5SJohnny Huang {
103079e42a59SJoel Stanley 	const struct otpconf_info *conf_info = info_cb.conf_info;
1031bb34a7bfSJohnny Huang 	uint32_t OTPCFG[16];
1032b458cd62SJohnny Huang 	uint32_t mask;
1033b458cd62SJohnny Huang 	uint32_t dw_offset;
1034b458cd62SJohnny Huang 	uint32_t bit_offset;
1035b458cd62SJohnny Huang 	uint32_t otp_value;
103673f11549SJohnny Huang 	char valid_bit[20];
103766f2f8e5SJohnny Huang 	int i;
103873f11549SJohnny Huang 	int j;
103966f2f8e5SJohnny Huang 
1040bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++)
104166f2f8e5SJohnny Huang 		otp_read_config(i, &OTPCFG[i]);
104266f2f8e5SJohnny Huang 
104366f2f8e5SJohnny Huang 
1044b458cd62SJohnny Huang 	printf("DW    BIT        Value       Description\n");
1045b458cd62SJohnny Huang 	printf("__________________________________________________________________________\n");
10463cb28812SJohnny Huang 	for (i = 0; i < info_cb.conf_info_len; i++) {
10473cb28812SJohnny Huang 		if (input_offset != -1 && input_offset != conf_info[i].dw_offset)
10482d4b0742SJohnny Huang 			continue;
10493cb28812SJohnny Huang 		dw_offset = conf_info[i].dw_offset;
10503cb28812SJohnny Huang 		bit_offset = conf_info[i].bit_offset;
10513cb28812SJohnny Huang 		mask = BIT(conf_info[i].length) - 1;
1052b458cd62SJohnny Huang 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
1053b458cd62SJohnny Huang 
10543cb28812SJohnny Huang 		if ((otp_value != conf_info[i].value) &&
10553cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_RESERVED &&
10563cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALUE &&
10573cb28812SJohnny Huang 		    conf_info[i].value != OTP_REG_VALID_BIT)
1058b458cd62SJohnny Huang 			continue;
1059b458cd62SJohnny Huang 		printf("0x%-4X", dw_offset);
1060b458cd62SJohnny Huang 
10613cb28812SJohnny Huang 		if (conf_info[i].length == 1) {
10623cb28812SJohnny Huang 			printf("0x%-9X", conf_info[i].bit_offset);
1063b458cd62SJohnny Huang 		} else {
1064b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
10653cb28812SJohnny Huang 			       conf_info[i].bit_offset + conf_info[i].length - 1,
10663cb28812SJohnny Huang 			       conf_info[i].bit_offset);
1067b458cd62SJohnny Huang 		}
1068b458cd62SJohnny Huang 		printf("0x%-10x", otp_value);
1069b458cd62SJohnny Huang 
10703cb28812SJohnny Huang 		if (conf_info[i].value == OTP_REG_RESERVED) {
1071b458cd62SJohnny Huang 			printf("Reserved\n");
10723cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALUE) {
10733cb28812SJohnny Huang 			printf(conf_info[i].information, otp_value);
1074b458cd62SJohnny Huang 			printf("\n");
10753cb28812SJohnny Huang 		} else if (conf_info[i].value == OTP_REG_VALID_BIT) {
1076b458cd62SJohnny Huang 			if (otp_value != 0) {
107773f11549SJohnny Huang 				for (j = 0; j < 7; j++) {
107873f11549SJohnny Huang 					if (otp_value == (1 << j)) {
107973f11549SJohnny Huang 						valid_bit[j * 2] = '1';
1080b458cd62SJohnny Huang 					} else {
108173f11549SJohnny Huang 						valid_bit[j * 2] = '0';
108273f11549SJohnny Huang 					}
108373f11549SJohnny Huang 					valid_bit[j * 2 + 1] = ' ';
108473f11549SJohnny Huang 				}
108573f11549SJohnny Huang 				valid_bit[15] = 0;
108673f11549SJohnny Huang 			} else {
108773f11549SJohnny Huang 				strcpy(valid_bit, "0 0 0 0 0 0 0 0\0");
1088b458cd62SJohnny Huang 			}
10893cb28812SJohnny Huang 			printf(conf_info[i].information, valid_bit);
1090b458cd62SJohnny Huang 			printf("\n");
1091b458cd62SJohnny Huang 		} else {
10923cb28812SJohnny Huang 			printf("%s\n", conf_info[i].information);
1093b458cd62SJohnny Huang 		}
1094b458cd62SJohnny Huang 	}
1095b458cd62SJohnny Huang 	return OTP_SUCCESS;
109666f2f8e5SJohnny Huang }
109766f2f8e5SJohnny Huang 
1098*5010032bSJohnny Huang static int otp_print_strap_image(struct otp_image_layout *image_layout)
109976d13988SJohnny Huang {
110079e42a59SJoel Stanley 	const struct otpstrap_info *strap_info = info_cb.strap_info;
1101696656c6SJohnny Huang 	uint32_t *OTPSTRAP;
1102696656c6SJohnny Huang 	uint32_t *OTPSTRAP_REG_PRO;
1103696656c6SJohnny Huang 	uint32_t *OTPSTRAP_PRO;
1104696656c6SJohnny Huang 	uint32_t *OTPSTRAP_IGNORE;
110576d13988SJohnny Huang 	int i;
1106a8bd6d8cSJohnny Huang 	int fail = 0;
1107a8bd6d8cSJohnny Huang 	uint32_t bit_offset;
1108a8bd6d8cSJohnny Huang 	uint32_t dw_offset;
1109a8bd6d8cSJohnny Huang 	uint32_t mask;
1110a8bd6d8cSJohnny Huang 	uint32_t otp_value;
1111696656c6SJohnny Huang 	uint32_t otp_reg_protect;
1112a8bd6d8cSJohnny Huang 	uint32_t otp_protect;
1113696656c6SJohnny Huang 	uint32_t otp_ignore;
111476d13988SJohnny Huang 
1115696656c6SJohnny Huang 	OTPSTRAP = (uint32_t *)image_layout->strap;
1116696656c6SJohnny Huang 	OTPSTRAP_PRO = (uint32_t *)image_layout->strap_pro;
1117696656c6SJohnny Huang 	OTPSTRAP_IGNORE = (uint32_t *)image_layout->strap_ignore;
1118*5010032bSJohnny Huang 	if (info_cb.version == OTP_AST2600A0) {
1119696656c6SJohnny Huang 		OTPSTRAP_REG_PRO = NULL;
1120a8bd6d8cSJohnny Huang 		printf("BIT(hex)   Value       Protect     Description\n");
1121a8bd6d8cSJohnny Huang 		printf("__________________________________________________________________________________________\n");
1122696656c6SJohnny Huang 	} else {
1123696656c6SJohnny Huang 		OTPSTRAP_REG_PRO = (uint32_t *)image_layout->strap_reg_pro;
1124696656c6SJohnny Huang 		printf("BIT(hex)   Value       REG_Protect Protect     Description\n");
1125696656c6SJohnny Huang 		printf("__________________________________________________________________________________________\n");
1126696656c6SJohnny Huang 	}
1127b458cd62SJohnny Huang 
11283cb28812SJohnny Huang 	for (i = 0; i < info_cb.strap_info_len; i++) {
1129696656c6SJohnny Huang 		if (strap_info[i].bit_offset > 31) {
1130a8bd6d8cSJohnny Huang 			dw_offset = 1;
11313cb28812SJohnny Huang 			bit_offset = strap_info[i].bit_offset - 32;
1132a8bd6d8cSJohnny Huang 		} else {
1133a8bd6d8cSJohnny Huang 			dw_offset = 0;
11343cb28812SJohnny Huang 			bit_offset = strap_info[i].bit_offset;
1135a8bd6d8cSJohnny Huang 		}
113676d13988SJohnny Huang 
11373cb28812SJohnny Huang 		mask = BIT(strap_info[i].length) - 1;
1138a8bd6d8cSJohnny Huang 		otp_value = (OTPSTRAP[dw_offset] >> bit_offset) & mask;
1139a8bd6d8cSJohnny Huang 		otp_protect = (OTPSTRAP_PRO[dw_offset] >> bit_offset) & mask;
1140696656c6SJohnny Huang 		otp_ignore = (OTPSTRAP_IGNORE[dw_offset] >> bit_offset) & mask;
1141a8bd6d8cSJohnny Huang 
1142*5010032bSJohnny Huang 		if (info_cb.version != OTP_AST2600A0)
1143696656c6SJohnny Huang 			otp_reg_protect = (OTPSTRAP_REG_PRO[dw_offset] >> bit_offset) & mask;
1144*5010032bSJohnny Huang 		else
1145*5010032bSJohnny Huang 			otp_reg_protect = 0;
1146696656c6SJohnny Huang 
1147696656c6SJohnny Huang 		if (otp_ignore == mask) {
1148a8bd6d8cSJohnny Huang 			continue;
1149696656c6SJohnny Huang 		} else if (otp_ignore != 0) {
1150a8bd6d8cSJohnny Huang 			fail = 1;
1151a8bd6d8cSJohnny Huang 		}
1152a8bd6d8cSJohnny Huang 
11533cb28812SJohnny Huang 		if ((otp_value != strap_info[i].value) &&
11543cb28812SJohnny Huang 		    strap_info[i].value != OTP_REG_RESERVED)
1155a8bd6d8cSJohnny Huang 			continue;
1156a8bd6d8cSJohnny Huang 
11573cb28812SJohnny Huang 		if (strap_info[i].length == 1) {
11583cb28812SJohnny Huang 			printf("0x%-9X", strap_info[i].bit_offset);
1159a8bd6d8cSJohnny Huang 		} else {
1160b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
11613cb28812SJohnny Huang 			       strap_info[i].bit_offset + strap_info[i].length - 1,
11623cb28812SJohnny Huang 			       strap_info[i].bit_offset);
1163a8bd6d8cSJohnny Huang 		}
1164a8bd6d8cSJohnny Huang 		printf("0x%-10x", otp_value);
1165*5010032bSJohnny Huang 		if (info_cb.version != OTP_AST2600A0)
1166696656c6SJohnny Huang 			printf("0x%-10x", otp_reg_protect);
1167a8bd6d8cSJohnny Huang 		printf("0x%-10x", otp_protect);
1168a8bd6d8cSJohnny Huang 
1169a8bd6d8cSJohnny Huang 		if (fail) {
1170696656c6SJohnny Huang 			printf("Ignore mask error\n");
1171a8bd6d8cSJohnny Huang 		} else {
11723cb28812SJohnny Huang 			if (strap_info[i].value != OTP_REG_RESERVED)
11733cb28812SJohnny Huang 				printf("%s\n", strap_info[i].information);
1174a8bd6d8cSJohnny Huang 			else
1175a8bd6d8cSJohnny Huang 				printf("Reserved\n");
1176a8bd6d8cSJohnny Huang 		}
1177a8bd6d8cSJohnny Huang 	}
1178a8bd6d8cSJohnny Huang 
1179a8bd6d8cSJohnny Huang 	if (fail)
118076d13988SJohnny Huang 		return OTP_FAILURE;
118176d13988SJohnny Huang 
118276d13988SJohnny Huang 	return OTP_SUCCESS;
118376d13988SJohnny Huang }
118476d13988SJohnny Huang 
1185b458cd62SJohnny Huang static int otp_print_strap_info(int view)
118676d13988SJohnny Huang {
118779e42a59SJoel Stanley 	const struct otpstrap_info *strap_info = info_cb.strap_info;
118876d13988SJohnny Huang 	struct otpstrap_status strap_status[64];
118907baa4e8SJohnny Huang 	int i, j;
1190b458cd62SJohnny Huang 	int fail = 0;
1191b458cd62SJohnny Huang 	uint32_t bit_offset;
1192b458cd62SJohnny Huang 	uint32_t length;
1193b458cd62SJohnny Huang 	uint32_t otp_value;
1194b458cd62SJohnny Huang 	uint32_t otp_protect;
119576d13988SJohnny Huang 
1196541eb887SJohnny Huang 	otp_strap_status(strap_status);
119776d13988SJohnny Huang 
1198b458cd62SJohnny Huang 	if (view) {
119907baa4e8SJohnny Huang 		// printf("BIT(hex) Value  Option         Protect   Description\n");
120007baa4e8SJohnny Huang 		// printf("                0 1 2 3 4 5 6\n");
120107baa4e8SJohnny Huang 		printf("BIT(hex) Value  Remains  Protect   Description\n");
120207baa4e8SJohnny Huang 		printf("___________________________________________________________________________________________________\n");
1203b458cd62SJohnny Huang 	} else {
1204b458cd62SJohnny Huang 		printf("BIT(hex)   Value       Description\n");
1205b458cd62SJohnny Huang 		printf("________________________________________________________________________________\n");
120676d13988SJohnny Huang 	}
12073cb28812SJohnny Huang 	for (i = 0; i < info_cb.strap_info_len; i++) {
1208b458cd62SJohnny Huang 		otp_value = 0;
12093cb28812SJohnny Huang 		bit_offset = strap_info[i].bit_offset;
12103cb28812SJohnny Huang 		length = strap_info[i].length;
1211b458cd62SJohnny Huang 		for (j = 0; j < length; j++) {
1212c947ef08SJohnny Huang 			otp_value |= strap_status[bit_offset + j].value << j;
1213c947ef08SJohnny Huang 			otp_protect |= strap_status[bit_offset + j].protected << j;
1214b458cd62SJohnny Huang 		}
12153cb28812SJohnny Huang 		if ((otp_value != strap_info[i].value) &&
12163cb28812SJohnny Huang 		    strap_info[i].value != OTP_REG_RESERVED)
1217b458cd62SJohnny Huang 			continue;
1218b458cd62SJohnny Huang 		if (view) {
1219b458cd62SJohnny Huang 			for (j = 0; j < length; j++) {
12203cb28812SJohnny Huang 				printf("0x%-7X", strap_info[i].bit_offset + j);
1221b458cd62SJohnny Huang 				printf("0x%-5X", strap_status[bit_offset + j].value);
122207baa4e8SJohnny Huang 				printf("%-9d", strap_status[bit_offset + j].remain_times);
1223b458cd62SJohnny Huang 				printf("0x%-7X", strap_status[bit_offset].protected);
12243cb28812SJohnny Huang 				if (strap_info[i].value == OTP_REG_RESERVED) {
1225b458cd62SJohnny Huang 					printf(" Reserved\n");
1226b458cd62SJohnny Huang 					continue;
1227b458cd62SJohnny Huang 				}
1228b458cd62SJohnny Huang 				if (length == 1) {
12293cb28812SJohnny Huang 					printf(" %s\n", strap_info[i].information);
1230b458cd62SJohnny Huang 					continue;
123176d13988SJohnny Huang 				}
123276d13988SJohnny Huang 
1233b458cd62SJohnny Huang 				if (j == 0)
12343cb28812SJohnny Huang 					printf("/%s\n", strap_info[i].information);
1235b458cd62SJohnny Huang 				else if (j == length - 1)
1236b458cd62SJohnny Huang 					printf("\\ \"\n");
1237b458cd62SJohnny Huang 				else
1238b458cd62SJohnny Huang 					printf("| \"\n");
123976d13988SJohnny Huang 			}
1240b458cd62SJohnny Huang 		} else {
1241c947ef08SJohnny Huang 			if (length == 1) {
12423cb28812SJohnny Huang 				printf("0x%-9X", strap_info[i].bit_offset);
1243b458cd62SJohnny Huang 			} else {
1244b458cd62SJohnny Huang 				printf("0x%-2X:0x%-4X",
1245b458cd62SJohnny Huang 				       bit_offset + length - 1, bit_offset);
1246b458cd62SJohnny Huang 			}
1247b458cd62SJohnny Huang 
1248b458cd62SJohnny Huang 			printf("0x%-10X", otp_value);
1249b458cd62SJohnny Huang 
12503cb28812SJohnny Huang 			if (strap_info[i].value != OTP_REG_RESERVED)
12513cb28812SJohnny Huang 				printf("%s\n", strap_info[i].information);
1252b458cd62SJohnny Huang 			else
1253b458cd62SJohnny Huang 				printf("Reserved\n");
1254b458cd62SJohnny Huang 		}
1255b458cd62SJohnny Huang 	}
1256b458cd62SJohnny Huang 
1257b458cd62SJohnny Huang 	if (fail)
1258b458cd62SJohnny Huang 		return OTP_FAILURE;
1259b458cd62SJohnny Huang 
1260b458cd62SJohnny Huang 	return OTP_SUCCESS;
1261b458cd62SJohnny Huang }
1262b458cd62SJohnny Huang 
1263696656c6SJohnny Huang static void buf_print(uint8_t *buf, int len)
126469d5fd8fSJohnny Huang {
126569d5fd8fSJohnny Huang 	int i;
126669d5fd8fSJohnny Huang 	printf("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
126769d5fd8fSJohnny Huang 	for (i = 0; i < len; i++) {
126869d5fd8fSJohnny Huang 		if (i % 16 == 0) {
126969d5fd8fSJohnny Huang 			printf("%04X: ", i);
127069d5fd8fSJohnny Huang 		}
127169d5fd8fSJohnny Huang 		printf("%02X ", buf[i]);
127269d5fd8fSJohnny Huang 		if ((i + 1) % 16 == 0) {
127369d5fd8fSJohnny Huang 			printf("\n");
127469d5fd8fSJohnny Huang 		}
127569d5fd8fSJohnny Huang 	}
127669d5fd8fSJohnny Huang }
127769d5fd8fSJohnny Huang 
1278696656c6SJohnny Huang static int otp_print_data_info(struct otp_image_layout *image_layout)
127969d5fd8fSJohnny Huang {
128069d5fd8fSJohnny Huang 	int key_id, key_offset, last, key_type, key_length, exp_length;
128179e42a59SJoel Stanley 	const struct otpkey_type *key_info_array = info_cb.key_info;
12829a4fe690SJohnny Huang 	struct otpkey_type key_info;
1283696656c6SJohnny Huang 	uint32_t *buf;
1284696656c6SJohnny Huang 	uint8_t *byte_buf;
12859d998018SJohnny Huang 	char empty = 1;
128669d5fd8fSJohnny Huang 	int i = 0, len = 0;
12879a4fe690SJohnny Huang 	int j;
128854552c69SJohnny Huang 
1289696656c6SJohnny Huang 	byte_buf = image_layout->data;
1290696656c6SJohnny Huang 	buf = (uint32_t *)byte_buf;
12919d998018SJohnny Huang 
12929d998018SJohnny Huang 	for (i = 0; i < 16; i++) {
12939d998018SJohnny Huang 		if (buf[i] != 0) {
12949d998018SJohnny Huang 			empty = 0;
12959d998018SJohnny Huang 		}
12969d998018SJohnny Huang 	}
12979d998018SJohnny Huang 	if (empty)
12989d998018SJohnny Huang 		return 0;
12999d998018SJohnny Huang 
13009d998018SJohnny Huang 	i = 0;
130169d5fd8fSJohnny Huang 	while (1) {
130269d5fd8fSJohnny Huang 		key_id = buf[i] & 0x7;
130369d5fd8fSJohnny Huang 		key_offset = buf[i] & 0x1ff8;
130469d5fd8fSJohnny Huang 		last = (buf[i] >> 13) & 1;
130569d5fd8fSJohnny Huang 		key_type = (buf[i] >> 14) & 0xf;
130669d5fd8fSJohnny Huang 		key_length = (buf[i] >> 18) & 0x3;
130769d5fd8fSJohnny Huang 		exp_length = (buf[i] >> 20) & 0xfff;
13089a4fe690SJohnny Huang 
13099a4fe690SJohnny Huang 		for (j = 0; j < info_cb.key_info_len; j++) {
13109a4fe690SJohnny Huang 			if (key_type == key_info_array[j].value) {
13119a4fe690SJohnny Huang 				key_info = key_info_array[j];
13129a4fe690SJohnny Huang 				break;
13139a4fe690SJohnny Huang 			}
13149a4fe690SJohnny Huang 		}
13159a4fe690SJohnny Huang 
13167f795e57SJohnny Huang 		printf("\nKey[%d]:\n", i);
131769d5fd8fSJohnny Huang 		printf("Key Type: ");
13189a4fe690SJohnny Huang 		printf("%s\n", key_info.information);
13199a4fe690SJohnny Huang 
13209a4fe690SJohnny Huang 		if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
132169d5fd8fSJohnny Huang 			printf("HMAC SHA Type: ");
132269d5fd8fSJohnny Huang 			switch (key_length) {
132369d5fd8fSJohnny Huang 			case 0:
132469d5fd8fSJohnny Huang 				printf("HMAC(SHA224)\n");
132569d5fd8fSJohnny Huang 				break;
132669d5fd8fSJohnny Huang 			case 1:
132769d5fd8fSJohnny Huang 				printf("HMAC(SHA256)\n");
132869d5fd8fSJohnny Huang 				break;
132969d5fd8fSJohnny Huang 			case 2:
133069d5fd8fSJohnny Huang 				printf("HMAC(SHA384)\n");
133169d5fd8fSJohnny Huang 				break;
133269d5fd8fSJohnny Huang 			case 3:
133369d5fd8fSJohnny Huang 				printf("HMAC(SHA512)\n");
133469d5fd8fSJohnny Huang 				break;
133569d5fd8fSJohnny Huang 			}
13369a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA) {
133769d5fd8fSJohnny Huang 			printf("RSA SHA Type: ");
133869d5fd8fSJohnny Huang 			switch (key_length) {
133969d5fd8fSJohnny Huang 			case 0:
134069d5fd8fSJohnny Huang 				printf("RSA1024\n");
134169d5fd8fSJohnny Huang 				len = 0x100;
134269d5fd8fSJohnny Huang 				break;
134369d5fd8fSJohnny Huang 			case 1:
134469d5fd8fSJohnny Huang 				printf("RSA2048\n");
134569d5fd8fSJohnny Huang 				len = 0x200;
134669d5fd8fSJohnny Huang 				break;
134769d5fd8fSJohnny Huang 			case 2:
134869d5fd8fSJohnny Huang 				printf("RSA3072\n");
134969d5fd8fSJohnny Huang 				len = 0x300;
135069d5fd8fSJohnny Huang 				break;
135169d5fd8fSJohnny Huang 			case 3:
135269d5fd8fSJohnny Huang 				printf("RSA4096\n");
135369d5fd8fSJohnny Huang 				len = 0x400;
135469d5fd8fSJohnny Huang 				break;
135569d5fd8fSJohnny Huang 			}
135669d5fd8fSJohnny Huang 			printf("RSA exponent bit length: %d\n", exp_length);
135769d5fd8fSJohnny Huang 		}
13589a4fe690SJohnny Huang 		if (key_info.need_id)
135969d5fd8fSJohnny Huang 			printf("Key Number ID: %d\n", key_id);
136069d5fd8fSJohnny Huang 		printf("Key Value:\n");
13619a4fe690SJohnny Huang 		if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
136269d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], 0x40);
13639a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_AES) {
13649a4fe690SJohnny Huang 			printf("AES Key:\n");
13659a4fe690SJohnny Huang 			buf_print(&byte_buf[key_offset], 0x20);
13669a4fe690SJohnny Huang 			if (info_cb.version == 0) {
13679a4fe690SJohnny Huang 				printf("AES IV:\n");
13689a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x10);
13699a4fe690SJohnny Huang 			}
13709a4fe690SJohnny Huang 
13719a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_VAULT) {
13729a4fe690SJohnny Huang 			if (info_cb.version == 0) {
137369d5fd8fSJohnny Huang 				printf("AES Key:\n");
137469d5fd8fSJohnny Huang 				buf_print(&byte_buf[key_offset], 0x20);
137569d5fd8fSJohnny Huang 				printf("AES IV:\n");
137669d5fd8fSJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x10);
13779a4fe690SJohnny Huang 			} else if (info_cb.version == 1) {
13789a4fe690SJohnny Huang 				printf("AES Key 1:\n");
13799a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset], 0x20);
13809a4fe690SJohnny Huang 				printf("AES Key 2:\n");
13819a4fe690SJohnny Huang 				buf_print(&byte_buf[key_offset + 0x20], 0x20);
13829a4fe690SJohnny Huang 			}
138369d5fd8fSJohnny Huang 
13849a4fe690SJohnny Huang 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA) {
138569d5fd8fSJohnny Huang 			printf("RSA mod:\n");
138669d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], len / 2);
138769d5fd8fSJohnny Huang 			printf("RSA exp:\n");
138869d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset + (len / 2)], len / 2);
138969d5fd8fSJohnny Huang 		}
139069d5fd8fSJohnny Huang 		if (last)
139169d5fd8fSJohnny Huang 			break;
139269d5fd8fSJohnny Huang 		i++;
139369d5fd8fSJohnny Huang 	}
139469d5fd8fSJohnny Huang 	return 0;
139569d5fd8fSJohnny Huang }
139669d5fd8fSJohnny Huang 
1397*5010032bSJohnny Huang static int otp_prog_conf(struct otp_image_layout *image_layout)
139869d5fd8fSJohnny Huang {
1399a6d0d645SJohnny Huang 	int i, k;
1400d90825e2SJohnny Huang 	int pass = 0;
1401a6d0d645SJohnny Huang 	uint32_t prog_address;
1402bb34a7bfSJohnny Huang 	uint32_t data[16];
1403a6d0d645SJohnny Huang 	uint32_t compare[2];
1404*5010032bSJohnny Huang 	uint32_t *conf = (uint32_t *)image_layout->conf;
1405*5010032bSJohnny Huang 	uint32_t *conf_ignore = (uint32_t *)image_layout->conf_ignore;
1406d90825e2SJohnny Huang 	uint32_t data_masked;
1407d90825e2SJohnny Huang 	uint32_t buf_masked;
140869d5fd8fSJohnny Huang 
1409a6d0d645SJohnny Huang 	printf("Read OTP Config Region:\n");
1410a6d0d645SJohnny Huang 
1411bb34a7bfSJohnny Huang 	for (i = 0; i < 16 ; i ++) {
141269d5fd8fSJohnny Huang 		prog_address = 0x800;
1413a6d0d645SJohnny Huang 		prog_address |= (i / 8) * 0x200;
1414a6d0d645SJohnny Huang 		prog_address |= (i % 8) * 0x2;
1415a6d0d645SJohnny Huang 		otp_read_data(prog_address, &data[i]);
1416a6d0d645SJohnny Huang 	}
1417a6d0d645SJohnny Huang 
1418a6d0d645SJohnny Huang 	printf("Check writable...\n");
1419bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++) {
1420*5010032bSJohnny Huang 		data_masked = data[i]  & ~conf_ignore[i];
1421*5010032bSJohnny Huang 		buf_masked  = conf[i] & ~conf_ignore[i];
1422d90825e2SJohnny Huang 		if (data_masked == buf_masked)
142369d5fd8fSJohnny Huang 			continue;
1424d90825e2SJohnny Huang 		if ((data_masked | buf_masked) == buf_masked) {
1425a6d0d645SJohnny Huang 			continue;
1426a6d0d645SJohnny Huang 		} else {
1427a6d0d645SJohnny Huang 			printf("Input image can't program into OTP, please check.\n");
1428a6af4a17SJohnny Huang 			printf("OTPCFG[%X] = %x\n", i, data[i]);
1429*5010032bSJohnny Huang 			printf("Input [%X] = %x\n", i, conf[i]);
1430*5010032bSJohnny Huang 			printf("Mask  [%X] = %x\n", i, ~conf_ignore[i]);
14312a856b9aSJohnny Huang 			return OTP_FAILURE;
1432a6d0d645SJohnny Huang 		}
1433a6d0d645SJohnny Huang 	}
1434a6d0d645SJohnny Huang 
1435a6d0d645SJohnny Huang 	printf("Start Programing...\n");
1436d90825e2SJohnny Huang 	otp_soak(0);
1437bb34a7bfSJohnny Huang 	for (i = 0; i < 16; i++) {
1438*5010032bSJohnny Huang 		data_masked = data[i]  & ~conf_ignore[i];
1439*5010032bSJohnny Huang 		buf_masked  = conf[i] & ~conf_ignore[i];
1440a6d0d645SJohnny Huang 		prog_address = 0x800;
1441a6d0d645SJohnny Huang 		prog_address |= (i / 8) * 0x200;
1442a6d0d645SJohnny Huang 		prog_address |= (i % 8) * 0x2;
1443bb34a7bfSJohnny Huang 		if (data_masked == buf_masked) {
1444bb34a7bfSJohnny Huang 			pass = 1;
1445a6d0d645SJohnny Huang 			continue;
1446bb34a7bfSJohnny Huang 		}
1447de6fbf1cSJohnny Huang 
1448a6d0d645SJohnny Huang 
1449de6fbf1cSJohnny Huang 		otp_soak(1);
1450*5010032bSJohnny Huang 		otp_prog_dw(conf[i], conf_ignore[i], prog_address);
1451a6d0d645SJohnny Huang 
145269d5fd8fSJohnny Huang 		pass = 0;
145369d5fd8fSJohnny Huang 		for (k = 0; k < RETRY; k++) {
1454*5010032bSJohnny Huang 			if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) {
1455de6fbf1cSJohnny Huang 				otp_soak(2);
1456a6d0d645SJohnny Huang 				otp_prog_dw(compare[0], prog_address, 1);
1457*5010032bSJohnny Huang 				if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) {
1458de6fbf1cSJohnny Huang 					otp_soak(1);
1459de6fbf1cSJohnny Huang 				} else {
1460de6fbf1cSJohnny Huang 					pass = 1;
1461de6fbf1cSJohnny Huang 					break;
1462de6fbf1cSJohnny Huang 				}
1463a6d0d645SJohnny Huang 			} else {
146469d5fd8fSJohnny Huang 				pass = 1;
146569d5fd8fSJohnny Huang 				break;
146669d5fd8fSJohnny Huang 			}
146769d5fd8fSJohnny Huang 		}
1468bb34a7bfSJohnny Huang 		if (pass == 0) {
1469bb34a7bfSJohnny Huang 			printf("address: %08x, data: %08x, buffer: %08x, mask: %08x\n",
1470*5010032bSJohnny Huang 			       i, data[i], conf[i], conf_ignore[i]);
1471bb34a7bfSJohnny Huang 			break;
1472bb34a7bfSJohnny Huang 		}
1473a6d0d645SJohnny Huang 	}
1474a6d0d645SJohnny Huang 
1475de6fbf1cSJohnny Huang 	otp_soak(0);
147669d5fd8fSJohnny Huang 	if (!pass)
14772a856b9aSJohnny Huang 		return OTP_FAILURE;
1478a6d0d645SJohnny Huang 
14792a856b9aSJohnny Huang 	return OTP_SUCCESS;
1480d90825e2SJohnny Huang 
148169d5fd8fSJohnny Huang }
148269d5fd8fSJohnny Huang 
148369d5fd8fSJohnny Huang 
1484*5010032bSJohnny Huang static int otp_strap_image_confirm(struct otp_image_layout *image_layout)
148569d5fd8fSJohnny Huang {
148669d5fd8fSJohnny Huang 	int i;
1487*5010032bSJohnny Huang 	uint32_t *strap;
1488*5010032bSJohnny Huang 	uint32_t *strap_ignore;
1489*5010032bSJohnny Huang 	uint32_t *strap_reg_protect;
1490*5010032bSJohnny Huang 	uint32_t *strap_pro;
1491*5010032bSJohnny Huang 	int bit, pbit, kbit, rpbit;
149269d5fd8fSJohnny Huang 	int fail = 0;
1493a6af4a17SJohnny Huang 	int skip = -1;
149466f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
149569d5fd8fSJohnny Huang 
1496*5010032bSJohnny Huang 	strap = (uint32_t *)image_layout->strap;
1497*5010032bSJohnny Huang 	strap_pro = (uint32_t *)image_layout->strap_pro;
1498*5010032bSJohnny Huang 	strap_ignore = (uint32_t *)image_layout->strap_ignore;
1499*5010032bSJohnny Huang 	strap_reg_protect = (uint32_t *)image_layout->strap_reg_pro;
1500*5010032bSJohnny Huang 
1501541eb887SJohnny Huang 	otp_strap_status(otpstrap);
150269d5fd8fSJohnny Huang 	for (i = 0; i < 64; i++) {
150369d5fd8fSJohnny Huang 		if (i < 32) {
1504*5010032bSJohnny Huang 			bit = (strap[0] >> i) & 0x1;
1505696656c6SJohnny Huang 			kbit = (strap_ignore[0] >> i) & 0x1;
1506*5010032bSJohnny Huang 			pbit = (strap_pro[0] >> i) & 0x1;
150769d5fd8fSJohnny Huang 		} else {
1508*5010032bSJohnny Huang 			bit = (strap[1] >> (i - 32)) & 0x1;
1509696656c6SJohnny Huang 			kbit = (strap_ignore[1] >> (i - 32)) & 0x1;
1510*5010032bSJohnny Huang 			pbit = (strap_pro[1] >> (i - 32)) & 0x1;
1511*5010032bSJohnny Huang 		}
1512*5010032bSJohnny Huang 
1513*5010032bSJohnny Huang 		if (info_cb.version != OTP_AST2600A0) {
1514*5010032bSJohnny Huang 			if (i < 32) {
1515*5010032bSJohnny Huang 				rpbit = (strap_reg_protect[0] >> i) & 0x1;
1516*5010032bSJohnny Huang 			} else {
1517*5010032bSJohnny Huang 				rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1;
1518*5010032bSJohnny Huang 			}
1519*5010032bSJohnny Huang 		} else {
1520*5010032bSJohnny Huang 			rpbit = 0;
152169d5fd8fSJohnny Huang 		}
152269d5fd8fSJohnny Huang 
152369d5fd8fSJohnny Huang 		if (kbit == 1) {
152469d5fd8fSJohnny Huang 			continue;
152569d5fd8fSJohnny Huang 		} else {
1526a6af4a17SJohnny Huang 			printf("OTPSTRAP[%X]:\n", i);
152769d5fd8fSJohnny Huang 		}
152869d5fd8fSJohnny Huang 		if (bit == otpstrap[i].value) {
152969d5fd8fSJohnny Huang 			printf("    The value is same as before, skip it.\n");
1530a6af4a17SJohnny Huang 			if (skip == -1)
1531a6af4a17SJohnny Huang 				skip = 1;
153269d5fd8fSJohnny Huang 			continue;
1533a6af4a17SJohnny Huang 		} else {
1534a6af4a17SJohnny Huang 			skip = 0;
153569d5fd8fSJohnny Huang 		}
153669d5fd8fSJohnny Huang 		if (otpstrap[i].protected == 1) {
153769d5fd8fSJohnny Huang 			printf("    This bit is protected and is not writable\n");
153869d5fd8fSJohnny Huang 			fail = 1;
153969d5fd8fSJohnny Huang 			continue;
154069d5fd8fSJohnny Huang 		}
154169d5fd8fSJohnny Huang 		if (otpstrap[i].remain_times == 0) {
1542a6af4a17SJohnny Huang 			printf("    This bit is no remaining times to write.\n");
154369d5fd8fSJohnny Huang 			fail = 1;
154469d5fd8fSJohnny Huang 			continue;
154569d5fd8fSJohnny Huang 		}
154669d5fd8fSJohnny Huang 		if (pbit == 1) {
154769d5fd8fSJohnny Huang 			printf("    This bit will be protected and become non-writable.\n");
154869d5fd8fSJohnny Huang 		}
1549*5010032bSJohnny Huang 		if (rpbit == 1 && info_cb.version != OTP_AST2600A0) {
1550*5010032bSJohnny Huang 			printf("    The relative register will be protected.\n");
1551*5010032bSJohnny Huang 		}
1552a6af4a17SJohnny Huang 		printf("    Write 1 to OTPSTRAP[%X] OPTION[%X], that value becomes from %d to %d.\n", i, otpstrap[i].writeable_option + 1, otpstrap[i].value, otpstrap[i].value ^ 1);
155369d5fd8fSJohnny Huang 	}
155469d5fd8fSJohnny Huang 	if (fail == 1)
1555a6af4a17SJohnny Huang 		return OTP_FAILURE;
1556a6af4a17SJohnny Huang 	else if (skip == 1)
1557a6af4a17SJohnny Huang 		return OTP_PROG_SKIP;
15587e22f42dSJohnny Huang 
15597e22f42dSJohnny Huang 	return 0;
156069d5fd8fSJohnny Huang }
156169d5fd8fSJohnny Huang 
15622a856b9aSJohnny Huang static int otp_print_strap(int start, int count)
156369d5fd8fSJohnny Huang {
156469d5fd8fSJohnny Huang 	int i, j;
156566f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
156669d5fd8fSJohnny Huang 
15672a856b9aSJohnny Huang 	if (start < 0 || start > 64)
15682a856b9aSJohnny Huang 		return OTP_USAGE;
15692a856b9aSJohnny Huang 
15702a856b9aSJohnny Huang 	if ((start + count) < 0 || (start + count) > 64)
15712a856b9aSJohnny Huang 		return OTP_USAGE;
15722a856b9aSJohnny Huang 
1573541eb887SJohnny Huang 	otp_strap_status(otpstrap);
157469d5fd8fSJohnny Huang 
157507baa4e8SJohnny Huang 	printf("BIT(hex)  Value  Option           Status\n");
1576a8bd6d8cSJohnny Huang 	printf("___________________________________________________________________________\n");
1577737ed20bSJohnny Huang 
1578cd1610b4SJohnny Huang 	for (i = start; i < start + count; i++) {
157907baa4e8SJohnny Huang 		printf("0x%-8X", i);
1580737ed20bSJohnny Huang 		printf("%-7d", otpstrap[i].value);
1581737ed20bSJohnny Huang 		for (j = 0; j < 7; j++)
1582737ed20bSJohnny Huang 			printf("%d ", otpstrap[i].option_array[j]);
1583737ed20bSJohnny Huang 		printf("   ");
158469d5fd8fSJohnny Huang 		if (otpstrap[i].protected == 1) {
1585737ed20bSJohnny Huang 			printf("protected and not writable");
158669d5fd8fSJohnny Huang 		} else {
1587737ed20bSJohnny Huang 			printf("not protected ");
158869d5fd8fSJohnny Huang 			if (otpstrap[i].remain_times == 0) {
1589737ed20bSJohnny Huang 				printf("and no remaining times to write.");
159069d5fd8fSJohnny Huang 			} else {
1591737ed20bSJohnny Huang 				printf("and still can write %d times", otpstrap[i].remain_times);
159269d5fd8fSJohnny Huang 			}
159369d5fd8fSJohnny Huang 		}
1594737ed20bSJohnny Huang 		printf("\n");
159569d5fd8fSJohnny Huang 	}
15962a856b9aSJohnny Huang 
15972a856b9aSJohnny Huang 	return OTP_SUCCESS;
159869d5fd8fSJohnny Huang }
159969d5fd8fSJohnny Huang 
1600*5010032bSJohnny Huang static int otp_prog_strap(struct otp_image_layout *image_layout)
160169d5fd8fSJohnny Huang {
160269d5fd8fSJohnny Huang 	int i, j;
1603*5010032bSJohnny Huang 	uint32_t *strap;
1604*5010032bSJohnny Huang 	uint32_t *strap_ignore;
1605*5010032bSJohnny Huang 	uint32_t *strap_pro;
1606*5010032bSJohnny Huang 	uint32_t *strap_reg_protect;
160769d5fd8fSJohnny Huang 	uint32_t prog_bit, prog_address;
1608*5010032bSJohnny Huang 	int bit, pbit, kbit, offset, rpbit;
160969d5fd8fSJohnny Huang 	int fail = 0;
16107e22f42dSJohnny Huang 	int pass = 0;
161166f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
161269d5fd8fSJohnny Huang 
1613*5010032bSJohnny Huang 	strap = (uint32_t *)image_layout->strap;
1614*5010032bSJohnny Huang 	strap_pro = (uint32_t *)image_layout->strap_pro;
1615*5010032bSJohnny Huang 	strap_ignore = (uint32_t *)image_layout->strap_ignore;
1616*5010032bSJohnny Huang 	strap_reg_protect = (uint32_t *)image_layout->strap_reg_pro;
1617*5010032bSJohnny Huang 
16187f795e57SJohnny Huang 	printf("Read OTP Strap Region:\n");
1619541eb887SJohnny Huang 	otp_strap_status(otpstrap);
162069d5fd8fSJohnny Huang 
16217f795e57SJohnny Huang 	printf("Check writable...\n");
1622*5010032bSJohnny Huang 	if (otp_strap_image_confirm(image_layout) == OTP_FAILURE) {
16237f795e57SJohnny Huang 		printf("Input image can't program into OTP, please check.\n");
16247f795e57SJohnny Huang 		return OTP_FAILURE;
16257f795e57SJohnny Huang 	}
16267e22f42dSJohnny Huang 
162769d5fd8fSJohnny Huang 	for (i = 0; i < 64; i++) {
162869d5fd8fSJohnny Huang 		prog_address = 0x800;
162969d5fd8fSJohnny Huang 		if (i < 32) {
163069d5fd8fSJohnny Huang 			offset = i;
1631*5010032bSJohnny Huang 			bit = (strap[0] >> offset) & 0x1;
1632696656c6SJohnny Huang 			kbit = (strap_ignore[0] >> offset) & 0x1;
1633*5010032bSJohnny Huang 			pbit = (strap_pro[0] >> offset) & 0x1;
163469d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200;
163569d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2;
163669d5fd8fSJohnny Huang 
163769d5fd8fSJohnny Huang 		} else {
163869d5fd8fSJohnny Huang 			offset = (i - 32);
1639*5010032bSJohnny Huang 			bit = (strap[1] >> offset) & 0x1;
1640696656c6SJohnny Huang 			kbit = (strap_ignore[1] >> offset) & 0x1;
1641*5010032bSJohnny Huang 			pbit = (strap_pro[1] >> offset) & 0x1;
164269d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200;
164369d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2;
164469d5fd8fSJohnny Huang 		}
1645*5010032bSJohnny Huang 		if (info_cb.version != OTP_AST2600A0) {
1646*5010032bSJohnny Huang 			if (i < 32) {
1647*5010032bSJohnny Huang 				rpbit = (strap_reg_protect[0] >> i) & 0x1;
1648*5010032bSJohnny Huang 			} else {
1649*5010032bSJohnny Huang 				rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1;
1650*5010032bSJohnny Huang 			}
1651*5010032bSJohnny Huang 		} else {
1652*5010032bSJohnny Huang 			rpbit = 0;
1653*5010032bSJohnny Huang 		}
165469d5fd8fSJohnny Huang 		prog_bit = ~(0x1 << offset);
165569d5fd8fSJohnny Huang 
165669d5fd8fSJohnny Huang 		if (kbit == 1) {
165769d5fd8fSJohnny Huang 			continue;
165869d5fd8fSJohnny Huang 		}
165969d5fd8fSJohnny Huang 		if (bit == otpstrap[i].value) {
166069d5fd8fSJohnny Huang 			continue;
166169d5fd8fSJohnny Huang 		}
166269d5fd8fSJohnny Huang 		if (otpstrap[i].protected == 1) {
166369d5fd8fSJohnny Huang 			fail = 1;
166469d5fd8fSJohnny Huang 			continue;
166569d5fd8fSJohnny Huang 		}
166669d5fd8fSJohnny Huang 		if (otpstrap[i].remain_times == 0) {
166769d5fd8fSJohnny Huang 			fail = 1;
166869d5fd8fSJohnny Huang 			continue;
166969d5fd8fSJohnny Huang 		}
16707e22f42dSJohnny Huang 
16717e22f42dSJohnny Huang 
1672de6fbf1cSJohnny Huang 		otp_soak(1);
16737e22f42dSJohnny Huang 		otp_prog(prog_address, prog_bit);
16747e22f42dSJohnny Huang 
16757e22f42dSJohnny Huang 		pass = 0;
167669d5fd8fSJohnny Huang 		for (j = 0; j < RETRY; j++) {
1677de6fbf1cSJohnny Huang 			if (verify_bit(prog_address, offset, 1) != 0) {
1678de6fbf1cSJohnny Huang 				otp_soak(2);
1679de6fbf1cSJohnny Huang 				otp_prog(prog_address, prog_bit);
1680de6fbf1cSJohnny Huang 				if (verify_bit(prog_address, offset, 1) != 0) {
1681de6fbf1cSJohnny Huang 					otp_soak(1);
1682de6fbf1cSJohnny Huang 				} else {
168369d5fd8fSJohnny Huang 					pass = 1;
168469d5fd8fSJohnny Huang 					break;
168569d5fd8fSJohnny Huang 				}
1686de6fbf1cSJohnny Huang 			} else {
1687de6fbf1cSJohnny Huang 				pass = 1;
1688de6fbf1cSJohnny Huang 				break;
16894b65a65dSJohnny Huang 			}
169069d5fd8fSJohnny Huang 		}
169169d5fd8fSJohnny Huang 		if (!pass)
16922a856b9aSJohnny Huang 			return OTP_FAILURE;
169369d5fd8fSJohnny Huang 
1694*5010032bSJohnny Huang 		if (rpbit == 1 && info_cb.version != OTP_AST2600A0) {
169569d5fd8fSJohnny Huang 			prog_address = 0x800;
169669d5fd8fSJohnny Huang 			if (i < 32)
1697*5010032bSJohnny Huang 				prog_address |= 0x608;
169869d5fd8fSJohnny Huang 			else
1699*5010032bSJohnny Huang 				prog_address |= 0x60a;
17007e22f42dSJohnny Huang 
1701de6fbf1cSJohnny Huang 			otp_soak(1);
17027e22f42dSJohnny Huang 			otp_prog(prog_address, prog_bit);
17037e22f42dSJohnny Huang 
17047e22f42dSJohnny Huang 			pass = 0;
170569d5fd8fSJohnny Huang 			for (j = 0; j < RETRY; j++) {
1706de6fbf1cSJohnny Huang 				if (verify_bit(prog_address, offset, 1) != 0) {
1707de6fbf1cSJohnny Huang 					otp_soak(2);
1708de6fbf1cSJohnny Huang 					otp_prog(prog_address, prog_bit);
1709de6fbf1cSJohnny Huang 					if (verify_bit(prog_address, offset, 1) != 0) {
1710de6fbf1cSJohnny Huang 						otp_soak(1);
1711de6fbf1cSJohnny Huang 					} else {
171269d5fd8fSJohnny Huang 						pass = 1;
171369d5fd8fSJohnny Huang 						break;
171469d5fd8fSJohnny Huang 					}
1715de6fbf1cSJohnny Huang 				} else {
1716de6fbf1cSJohnny Huang 					pass = 1;
1717de6fbf1cSJohnny Huang 					break;
171869d5fd8fSJohnny Huang 				}
171969d5fd8fSJohnny Huang 			}
172069d5fd8fSJohnny Huang 			if (!pass)
17212a856b9aSJohnny Huang 				return OTP_FAILURE;
1722*5010032bSJohnny Huang 		}
1723*5010032bSJohnny Huang 
1724*5010032bSJohnny Huang 		if (pbit != 0) {
1725*5010032bSJohnny Huang 			prog_address = 0x800;
1726*5010032bSJohnny Huang 			if (i < 32)
1727*5010032bSJohnny Huang 				prog_address |= 0x60c;
1728*5010032bSJohnny Huang 			else
1729*5010032bSJohnny Huang 				prog_address |= 0x60e;
1730*5010032bSJohnny Huang 
1731*5010032bSJohnny Huang 			otp_soak(1);
1732*5010032bSJohnny Huang 			otp_prog(prog_address, prog_bit);
1733*5010032bSJohnny Huang 
1734*5010032bSJohnny Huang 			pass = 0;
1735*5010032bSJohnny Huang 			for (j = 0; j < RETRY; j++) {
1736*5010032bSJohnny Huang 				if (verify_bit(prog_address, offset, 1) != 0) {
1737*5010032bSJohnny Huang 					otp_soak(2);
1738*5010032bSJohnny Huang 					otp_prog(prog_address, prog_bit);
1739*5010032bSJohnny Huang 					if (verify_bit(prog_address, offset, 1) != 0) {
1740*5010032bSJohnny Huang 						otp_soak(1);
1741*5010032bSJohnny Huang 					} else {
1742*5010032bSJohnny Huang 						pass = 1;
1743*5010032bSJohnny Huang 						break;
1744*5010032bSJohnny Huang 					}
1745*5010032bSJohnny Huang 				} else {
1746*5010032bSJohnny Huang 					pass = 1;
1747*5010032bSJohnny Huang 					break;
1748*5010032bSJohnny Huang 				}
1749*5010032bSJohnny Huang 			}
1750*5010032bSJohnny Huang 			if (!pass)
1751*5010032bSJohnny Huang 				return OTP_FAILURE;
1752*5010032bSJohnny Huang 		}
175369d5fd8fSJohnny Huang 
175469d5fd8fSJohnny Huang 	}
1755de6fbf1cSJohnny Huang 	otp_soak(0);
175669d5fd8fSJohnny Huang 	if (fail == 1)
17572a856b9aSJohnny Huang 		return OTP_FAILURE;
175869d5fd8fSJohnny Huang 	else
17592a856b9aSJohnny Huang 		return OTP_SUCCESS;
176069d5fd8fSJohnny Huang 
176169d5fd8fSJohnny Huang }
176269d5fd8fSJohnny Huang 
1763de6fbf1cSJohnny Huang static void otp_prog_bit(uint32_t value, uint32_t prog_address, uint32_t bit_offset)
1764cd1610b4SJohnny Huang {
1765cd1610b4SJohnny Huang 	int prog_bit;
1766cd1610b4SJohnny Huang 
1767cd1610b4SJohnny Huang 	if (prog_address % 2 == 0) {
1768cd1610b4SJohnny Huang 		if (value)
1769cd1610b4SJohnny Huang 			prog_bit = ~(0x1 << bit_offset);
1770cd1610b4SJohnny Huang 		else
1771cd1610b4SJohnny Huang 			return;
1772cd1610b4SJohnny Huang 	} else {
1773cd1610b4SJohnny Huang 		prog_address |= 1 << 15;
1774cd1610b4SJohnny Huang 		if (!value)
1775cd1610b4SJohnny Huang 			prog_bit = 0x1 << bit_offset;
1776cd1610b4SJohnny Huang 		else
1777cd1610b4SJohnny Huang 			return;
1778cd1610b4SJohnny Huang 	}
1779cd1610b4SJohnny Huang 	otp_prog(prog_address, prog_bit);
1780cd1610b4SJohnny Huang }
1781cd1610b4SJohnny Huang 
1782*5010032bSJohnny Huang static int otp_prog_data(struct otp_image_layout *image_layout)
17834c1c9b35SJohnny Huang {
178454552c69SJohnny Huang 	int i;
178554552c69SJohnny Huang 	int ret;
1786*5010032bSJohnny Huang 	int data_dw;
1787d90825e2SJohnny Huang 	uint32_t data[2048];
1788*5010032bSJohnny Huang 	uint32_t *buf;
1789*5010032bSJohnny Huang 	uint32_t *buf_ignore;
17904c1c9b35SJohnny Huang 
179154552c69SJohnny Huang 	uint32_t data_masked;
179254552c69SJohnny Huang 	uint32_t buf_masked;
17934c1c9b35SJohnny Huang 
1794*5010032bSJohnny Huang 	buf = (uint32_t *)image_layout->data;
1795*5010032bSJohnny Huang 	buf_ignore = (uint32_t *)image_layout->data_ignore;
1796*5010032bSJohnny Huang 
1797*5010032bSJohnny Huang 	data_dw = image_layout->data_length / 4;
1798*5010032bSJohnny Huang 
17994c1c9b35SJohnny Huang 	printf("Read OTP Data:\n");
18004c1c9b35SJohnny Huang 
1801*5010032bSJohnny Huang 	for (i = 0; i < data_dw - 2 ; i += 2) {
1802d90825e2SJohnny Huang 		otp_read_data(i, &data[i]);
18034c1c9b35SJohnny Huang 	}
1804d90825e2SJohnny Huang 
18054c1c9b35SJohnny Huang 	printf("Check writable...\n");
180654552c69SJohnny Huang 	// ignore last two dw, the last two dw is used for slt otp write check.
1807*5010032bSJohnny Huang 	for (i = 0; i < data_dw - 2; i++) {
1808696656c6SJohnny Huang 		data_masked = data[i]  & ~buf_ignore[i];
1809696656c6SJohnny Huang 		buf_masked  = buf[i] & ~buf_ignore[i];
181054552c69SJohnny Huang 		if (data_masked == buf_masked)
18114c1c9b35SJohnny Huang 			continue;
1812d90825e2SJohnny Huang 		if (i % 2 == 0) {
181354552c69SJohnny Huang 			if ((data_masked | buf_masked) == buf_masked) {
18144c1c9b35SJohnny Huang 				continue;
18154c1c9b35SJohnny Huang 			} else {
18164c1c9b35SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
1817d90825e2SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
18184c1c9b35SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
1819696656c6SJohnny Huang 				printf("Mask    [%x] = %x\n", i, ~buf_ignore[i]);
18202a856b9aSJohnny Huang 				return OTP_FAILURE;
182169d5fd8fSJohnny Huang 			}
1822d90825e2SJohnny Huang 		} else {
182354552c69SJohnny Huang 			if ((data_masked & buf_masked) == buf_masked) {
1824d90825e2SJohnny Huang 				continue;
1825d90825e2SJohnny Huang 			} else {
1826d90825e2SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
1827d90825e2SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
1828d90825e2SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
1829696656c6SJohnny Huang 				printf("Mask    [%x] = %x\n", i, ~buf_ignore[i]);
18302a856b9aSJohnny Huang 				return OTP_FAILURE;
1831d90825e2SJohnny Huang 			}
1832d90825e2SJohnny Huang 		}
1833d90825e2SJohnny Huang 	}
183469d5fd8fSJohnny Huang 
1835d90825e2SJohnny Huang 	printf("Start Programing...\n");
1836d90825e2SJohnny Huang 
183754552c69SJohnny Huang 	// programing ecc region first
183854552c69SJohnny Huang 	for (i = 1792; i < 2046; i += 2) {
1839696656c6SJohnny Huang 		ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i);
184054552c69SJohnny Huang 		if (ret != OTP_SUCCESS) {
184154552c69SJohnny Huang 			printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n",
1842696656c6SJohnny Huang 			       i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]);
184354552c69SJohnny Huang 			return ret;
1844d90825e2SJohnny Huang 		}
1845d90825e2SJohnny Huang 	}
1846d90825e2SJohnny Huang 
184754552c69SJohnny Huang 	for (i = 0; i < 1792; i += 2) {
1848696656c6SJohnny Huang 		ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i);
184954552c69SJohnny Huang 		if (ret != OTP_SUCCESS) {
185054552c69SJohnny Huang 			printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n",
1851696656c6SJohnny Huang 			       i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]);
185254552c69SJohnny Huang 			return ret;
1853d90825e2SJohnny Huang 		}
1854de6fbf1cSJohnny Huang 	}
1855de6fbf1cSJohnny Huang 	otp_soak(0);
18562a856b9aSJohnny Huang 	return OTP_SUCCESS;
1857d90825e2SJohnny Huang 
1858d90825e2SJohnny Huang }
1859d90825e2SJohnny Huang 
1860696656c6SJohnny Huang static int otp_image_verify(uint8_t *src_buf, uint32_t length, uint8_t *digest_buf)
1861696656c6SJohnny Huang {
1862696656c6SJohnny Huang 	sha256_context ctx;
1863696656c6SJohnny Huang 	u8 digest_ret[CHECKSUM_LEN];
1864696656c6SJohnny Huang 
1865696656c6SJohnny Huang 	sha256_starts(&ctx);
1866696656c6SJohnny Huang 	sha256_update(&ctx, src_buf, length);
1867696656c6SJohnny Huang 	sha256_finish(&ctx, digest_ret);
1868696656c6SJohnny Huang 
1869696656c6SJohnny Huang 	if (!memcmp(digest_buf, digest_ret, CHECKSUM_LEN))
1870696656c6SJohnny Huang 		return 0;
1871696656c6SJohnny Huang 	else
1872696656c6SJohnny Huang 		return -1;
1873696656c6SJohnny Huang 
1874696656c6SJohnny Huang }
1875696656c6SJohnny Huang 
1876d90825e2SJohnny Huang static int do_otp_prog(int addr, int byte_size, int nconfirm)
187769d5fd8fSJohnny Huang {
187869d5fd8fSJohnny Huang 	int ret;
18799a4fe690SJohnny Huang 	int image_version = 0;
1880696656c6SJohnny Huang 	struct otp_header *otp_header;
1881696656c6SJohnny Huang 	struct otp_image_layout image_layout;
1882696656c6SJohnny Huang 	int image_size;
1883696656c6SJohnny Huang 	uint8_t *buf;
1884696656c6SJohnny Huang 	uint8_t *checksum;
188569d5fd8fSJohnny Huang 
1886696656c6SJohnny Huang 	otp_header = map_physmem(addr, sizeof(struct otp_header), MAP_WRBACK);
1887696656c6SJohnny Huang 	if (!otp_header) {
188869d5fd8fSJohnny Huang 		puts("Failed to map physical memory\n");
18892a856b9aSJohnny Huang 		return OTP_FAILURE;
189069d5fd8fSJohnny Huang 	}
1891d90825e2SJohnny Huang 
1892696656c6SJohnny Huang 	image_size = OTP_IMAGE_SIZE(otp_header->image_info);
1893696656c6SJohnny Huang 	unmap_physmem(otp_header, MAP_WRBACK);
1894696656c6SJohnny Huang 
1895696656c6SJohnny Huang 	buf = map_physmem(addr, image_size + CHECKSUM_LEN, MAP_WRBACK);
1896696656c6SJohnny Huang 
1897696656c6SJohnny Huang 	if (!buf) {
1898696656c6SJohnny Huang 		puts("Failed to map physical memory\n");
1899696656c6SJohnny Huang 		return OTP_FAILURE;
1900696656c6SJohnny Huang 	}
1901696656c6SJohnny Huang 	otp_header = (struct otp_header *) buf;
1902696656c6SJohnny Huang 	checksum = buf + otp_header->checksum_offset;
1903696656c6SJohnny Huang 
1904696656c6SJohnny Huang 	if (strcmp(OTP_MAGIC, (char *)otp_header->otp_magic) != 0) {
1905696656c6SJohnny Huang 		puts("Image is invalid\n");
1906696656c6SJohnny Huang 		return OTP_FAILURE;
1907696656c6SJohnny Huang 	}
1908696656c6SJohnny Huang 
1909696656c6SJohnny Huang 
1910*5010032bSJohnny Huang 	image_layout.data_length = (int)(OTP_REGION_SIZE(otp_header->data_info) / 2);
1911*5010032bSJohnny Huang 	image_layout.data = buf + OTP_REGION_OFFSET(otp_header->data_info);
1912*5010032bSJohnny Huang 	image_layout.data_ignore = image_layout.data + image_layout.data_length;
1913*5010032bSJohnny Huang 
1914*5010032bSJohnny Huang 	image_layout.conf_length = (int)(OTP_REGION_SIZE(otp_header->config_info) / 2);
1915696656c6SJohnny Huang 	image_layout.conf = buf + OTP_REGION_OFFSET(otp_header->config_info);
1916*5010032bSJohnny Huang 	image_layout.conf_ignore = image_layout.conf + image_layout.conf_length;
1917696656c6SJohnny Huang 
1918696656c6SJohnny Huang 	image_layout.strap = buf + OTP_REGION_OFFSET(otp_header->strap_info);
1919696656c6SJohnny Huang 
1920696656c6SJohnny Huang 	if (!strcmp("A0", (char *)otp_header->otp_version)) {
1921696656c6SJohnny Huang 		image_version = OTP_AST2600A0;
1922*5010032bSJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 3);
1923*5010032bSJohnny Huang 		image_layout.strap_pro = image_layout.strap + image_layout.strap_length;
1924*5010032bSJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 2 * image_layout.strap_length;
1925696656c6SJohnny Huang 	} else if (!strcmp("A1", (char *)otp_header->otp_version)) {
1926696656c6SJohnny Huang 		image_version = OTP_AST2600A1;
1927*5010032bSJohnny Huang 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4);
1928*5010032bSJohnny Huang 		image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length;
1929*5010032bSJohnny Huang 		image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length;
1930*5010032bSJohnny Huang 		image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length;
1931696656c6SJohnny Huang 	} else {
1932696656c6SJohnny Huang 		puts("Version is not supported\n");
1933696656c6SJohnny Huang 		return OTP_FAILURE;
1934696656c6SJohnny Huang 	}
1935696656c6SJohnny Huang 
19369a4fe690SJohnny Huang 	if (image_version != info_cb.version) {
19379a4fe690SJohnny Huang 		puts("Version is not match\n");
19389a4fe690SJohnny Huang 		return OTP_FAILURE;
19399a4fe690SJohnny Huang 	}
19409a4fe690SJohnny Huang 
1941696656c6SJohnny Huang 	if (otp_image_verify(buf, image_size, checksum)) {
1942696656c6SJohnny Huang 		puts("checksum is invalid\n");
1943696656c6SJohnny Huang 		return OTP_FAILURE;
1944d90825e2SJohnny Huang 	}
19457332532cSJohnny Huang 
194669d5fd8fSJohnny Huang 	if (!nconfirm) {
1947696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_DATA) {
19487f795e57SJohnny Huang 			printf("\nOTP data region :\n");
1949696656c6SJohnny Huang 			if (otp_print_data_info(&image_layout) < 0) {
195069d5fd8fSJohnny Huang 				printf("OTP data error, please check.\n");
19512a856b9aSJohnny Huang 				return OTP_FAILURE;
195269d5fd8fSJohnny Huang 			}
195369d5fd8fSJohnny Huang 		}
1954696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_STRAP) {
19557332532cSJohnny Huang 			printf("\nOTP strap region :\n");
1956*5010032bSJohnny Huang 			if (otp_print_strap_image(&image_layout) < 0) {
19577332532cSJohnny Huang 				printf("OTP strap error, please check.\n");
19587332532cSJohnny Huang 				return OTP_FAILURE;
19597332532cSJohnny Huang 			}
19607332532cSJohnny Huang 		}
1961696656c6SJohnny Huang 		if (otp_header->image_info & OTP_INC_CONFIG) {
19627332532cSJohnny Huang 			printf("\nOTP configuration region :\n");
1963696656c6SJohnny Huang 			if (otp_print_conf_image(&image_layout) < 0) {
19647332532cSJohnny Huang 				printf("OTP config error, please check.\n");
19657332532cSJohnny Huang 				return OTP_FAILURE;
19667332532cSJohnny Huang 			}
19677332532cSJohnny Huang 		}
19687332532cSJohnny Huang 
196969d5fd8fSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
197069d5fd8fSJohnny Huang 		if (!confirm_yesno()) {
197169d5fd8fSJohnny Huang 			printf(" Aborting\n");
19722a856b9aSJohnny Huang 			return OTP_FAILURE;
197369d5fd8fSJohnny Huang 		}
197469d5fd8fSJohnny Huang 	}
19757332532cSJohnny Huang 
1976*5010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_DATA) {
1977*5010032bSJohnny Huang 		printf("programing data region ...\n");
1978*5010032bSJohnny Huang 		ret = otp_prog_data(&image_layout);
1979*5010032bSJohnny Huang 		if (ret != 0) {
1980*5010032bSJohnny Huang 			printf("Error\n");
1981*5010032bSJohnny Huang 			return ret;
1982*5010032bSJohnny Huang 		} else {
1983*5010032bSJohnny Huang 			printf("Done\n");
1984*5010032bSJohnny Huang 		}
1985*5010032bSJohnny Huang 	}
1986*5010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_STRAP) {
1987*5010032bSJohnny Huang 		printf("programing strap region ...\n");
1988*5010032bSJohnny Huang 		ret = otp_prog_strap(&image_layout);
1989*5010032bSJohnny Huang 		if (ret != 0) {
1990*5010032bSJohnny Huang 			printf("Error\n");
1991*5010032bSJohnny Huang 			return ret;
1992*5010032bSJohnny Huang 		} else {
1993*5010032bSJohnny Huang 			printf("Done\n");
1994*5010032bSJohnny Huang 		}
1995*5010032bSJohnny Huang 	}
1996*5010032bSJohnny Huang 	if (otp_header->image_info & OTP_INC_CONFIG) {
1997*5010032bSJohnny Huang 		printf("programing configuration region ...\n");
1998*5010032bSJohnny Huang 		ret = otp_prog_conf(&image_layout);
1999*5010032bSJohnny Huang 		if (ret != 0) {
2000*5010032bSJohnny Huang 			printf("Error\n");
2001*5010032bSJohnny Huang 			return ret;
2002*5010032bSJohnny Huang 		}
2003*5010032bSJohnny Huang 		printf("Done\n");
2004*5010032bSJohnny Huang 	}
2005cd1610b4SJohnny Huang 
20067332532cSJohnny Huang 	return OTP_SUCCESS;
20072a856b9aSJohnny Huang }
20082a856b9aSJohnny Huang 
20092a856b9aSJohnny Huang static int do_otp_prog_bit(int mode, int otp_dw_offset, int bit_offset, int value, int nconfirm)
2010cd1610b4SJohnny Huang {
2011a6af4a17SJohnny Huang 	uint32_t read[2];
2012cd1610b4SJohnny Huang 	uint32_t strap_buf[6];
2013d90825e2SJohnny Huang 	uint32_t prog_address = 0;
201466f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
2015cd1610b4SJohnny Huang 	int otp_bit;
2016cd1610b4SJohnny Huang 	int i;
2017cd1610b4SJohnny Huang 	int pass;
2018a6af4a17SJohnny Huang 	int ret;
2019cd1610b4SJohnny Huang 
2020cd1610b4SJohnny Huang 	switch (mode) {
2021a6d0d645SJohnny Huang 	case OTP_REGION_CONF:
2022a6af4a17SJohnny Huang 		otp_read_config(otp_dw_offset, read);
2023cd1610b4SJohnny Huang 		prog_address = 0x800;
2024cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset / 8) * 0x200;
2025cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset % 8) * 0x2;
2026a6af4a17SJohnny Huang 		otp_bit = (read[0] >> bit_offset) & 0x1;
2027cd1610b4SJohnny Huang 		if (otp_bit == value) {
2028a6af4a17SJohnny Huang 			printf("OTPCFG%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
2029cd1610b4SJohnny Huang 			printf("No need to program\n");
20302a856b9aSJohnny Huang 			return OTP_SUCCESS;
2031cd1610b4SJohnny Huang 		}
2032cd1610b4SJohnny Huang 		if (otp_bit == 1 && value == 0) {
2033a6af4a17SJohnny Huang 			printf("OTPCFG%X[%X] = 1\n", otp_dw_offset, bit_offset);
2034cd1610b4SJohnny Huang 			printf("OTP is programed, which can't be clean\n");
20352a856b9aSJohnny Huang 			return OTP_FAILURE;
2036cd1610b4SJohnny Huang 		}
2037a6af4a17SJohnny Huang 		printf("Program OTPCFG%X[%X] to 1\n", otp_dw_offset, bit_offset);
2038cd1610b4SJohnny Huang 		break;
2039a6d0d645SJohnny Huang 	case OTP_REGION_DATA:
2040cd1610b4SJohnny Huang 		prog_address = otp_dw_offset;
2041cd1610b4SJohnny Huang 
2042cd1610b4SJohnny Huang 		if (otp_dw_offset % 2 == 0) {
2043a6af4a17SJohnny Huang 			otp_read_data(otp_dw_offset, read);
2044a6af4a17SJohnny Huang 			otp_bit = (read[0] >> bit_offset) & 0x1;
2045643b9cfdSJohnny Huang 
2046643b9cfdSJohnny Huang 			if (otp_bit == 1 && value == 0) {
2047643b9cfdSJohnny Huang 				printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
2048643b9cfdSJohnny Huang 				printf("OTP is programed, which can't be cleaned\n");
2049643b9cfdSJohnny Huang 				return OTP_FAILURE;
2050643b9cfdSJohnny Huang 			}
2051cd1610b4SJohnny Huang 		} else {
2052a6af4a17SJohnny Huang 			otp_read_data(otp_dw_offset - 1, read);
2053a6af4a17SJohnny Huang 			otp_bit = (read[1] >> bit_offset) & 0x1;
2054643b9cfdSJohnny Huang 
2055643b9cfdSJohnny Huang 			if (otp_bit == 0 && value == 1) {
2056643b9cfdSJohnny Huang 				printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
2057643b9cfdSJohnny Huang 				printf("OTP is programed, which can't be writen\n");
2058643b9cfdSJohnny Huang 				return OTP_FAILURE;
2059643b9cfdSJohnny Huang 			}
2060cd1610b4SJohnny Huang 		}
2061cd1610b4SJohnny Huang 		if (otp_bit == value) {
2062a6af4a17SJohnny Huang 			printf("OTPDATA%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
2063cd1610b4SJohnny Huang 			printf("No need to program\n");
20642a856b9aSJohnny Huang 			return OTP_SUCCESS;
2065cd1610b4SJohnny Huang 		}
2066643b9cfdSJohnny Huang 
2067a6af4a17SJohnny Huang 		printf("Program OTPDATA%X[%X] to 1\n", otp_dw_offset, bit_offset);
2068cd1610b4SJohnny Huang 		break;
2069a6d0d645SJohnny Huang 	case OTP_REGION_STRAP:
2070*5010032bSJohnny Huang 		// otp_strap_status(otpstrap);
2071*5010032bSJohnny Huang 		// otp_print_strap(bit_offset, 1);
2072*5010032bSJohnny Huang 		// if (bit_offset < 32) {
2073*5010032bSJohnny Huang 		// 	strap_buf[0] = value << bit_offset;
2074*5010032bSJohnny Huang 		// 	strap_buf[1] = 0;
2075*5010032bSJohnny Huang 		// 	strap_buf[2] = ~BIT(bit_offset);
2076*5010032bSJohnny Huang 		// 	strap_buf[3] = ~0;
2077*5010032bSJohnny Huang 		// 	strap_buf[4] = 0;
2078*5010032bSJohnny Huang 		// 	strap_buf[5] = 0;
2079*5010032bSJohnny Huang 		// } else {
2080*5010032bSJohnny Huang 		// 	strap_buf[0] = 0;
2081*5010032bSJohnny Huang 		// 	strap_buf[1] = value << (bit_offset - 32);
2082*5010032bSJohnny Huang 		// 	strap_buf[2] = ~0;
2083*5010032bSJohnny Huang 		// 	strap_buf[3] = ~BIT(bit_offset - 32);
2084*5010032bSJohnny Huang 		// 	strap_buf[4] = 0;
2085*5010032bSJohnny Huang 		// 	strap_buf[5] = 0;
2086*5010032bSJohnny Huang 		// }
2087*5010032bSJohnny Huang 		// ret = otp_strap_image_confirm(strap_buf);
2088*5010032bSJohnny Huang 		// if (ret == OTP_FAILURE)
2089*5010032bSJohnny Huang 		// 	return OTP_FAILURE;
2090*5010032bSJohnny Huang 		// else if (ret == OTP_PROG_SKIP)
2091*5010032bSJohnny Huang 		// 	return OTP_SUCCESS;
2092a6af4a17SJohnny Huang 
2093cd1610b4SJohnny Huang 		break;
2094cd1610b4SJohnny Huang 	}
2095cd1610b4SJohnny Huang 
2096cd1610b4SJohnny Huang 	if (!nconfirm) {
2097cd1610b4SJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
2098cd1610b4SJohnny Huang 		if (!confirm_yesno()) {
2099cd1610b4SJohnny Huang 			printf(" Aborting\n");
21002a856b9aSJohnny Huang 			return OTP_FAILURE;
2101cd1610b4SJohnny Huang 		}
2102cd1610b4SJohnny Huang 	}
2103cd1610b4SJohnny Huang 
2104cd1610b4SJohnny Huang 	switch (mode) {
2105a6d0d645SJohnny Huang 	case OTP_REGION_STRAP:
2106*5010032bSJohnny Huang 		// return otp_prog_strap(strap_buf);
2107*5010032bSJohnny Huang 		return OTP_SUCCESS;
2108a6d0d645SJohnny Huang 	case OTP_REGION_CONF:
2109a6d0d645SJohnny Huang 	case OTP_REGION_DATA:
2110de6fbf1cSJohnny Huang 		otp_soak(1);
2111de6fbf1cSJohnny Huang 		otp_prog_bit(value, prog_address, bit_offset);
2112de6fbf1cSJohnny Huang 		pass = 0;
2113de6fbf1cSJohnny Huang 
2114cd1610b4SJohnny Huang 		for (i = 0; i < RETRY; i++) {
2115a6d0d645SJohnny Huang 			if (verify_bit(prog_address, bit_offset, value) != 0) {
2116de6fbf1cSJohnny Huang 				otp_soak(2);
2117de6fbf1cSJohnny Huang 				otp_prog_bit(value, prog_address, bit_offset);
2118de6fbf1cSJohnny Huang 				if (verify_bit(prog_address, bit_offset, value) != 0) {
2119de6fbf1cSJohnny Huang 					otp_soak(1);
2120cd1610b4SJohnny Huang 				} else {
2121de6fbf1cSJohnny Huang 					pass = 1;
2122de6fbf1cSJohnny Huang 					break;
2123de6fbf1cSJohnny Huang 				}
2124de6fbf1cSJohnny Huang 			} else {
2125de6fbf1cSJohnny Huang 				pass = 1;
2126cd1610b4SJohnny Huang 				break;
2127cd1610b4SJohnny Huang 			}
2128cd1610b4SJohnny Huang 		}
2129de6fbf1cSJohnny Huang 
2130de6fbf1cSJohnny Huang 		otp_soak(0);
2131de6fbf1cSJohnny Huang 		if (pass) {
21329009c25dSJohnny Huang 			printf("SUCCESS\n");
21332a856b9aSJohnny Huang 			return OTP_SUCCESS;
21349009c25dSJohnny Huang 		} else {
21359009c25dSJohnny Huang 			printf("OTP cannot be programed\n");
21369009c25dSJohnny Huang 			printf("FAILED\n");
21379009c25dSJohnny Huang 			return OTP_FAILURE;
21389009c25dSJohnny Huang 		}
2139cd1610b4SJohnny Huang 	}
2140cd1610b4SJohnny Huang 
21412a856b9aSJohnny Huang 	return OTP_USAGE;
2142cd1610b4SJohnny Huang }
2143cd1610b4SJohnny Huang 
21442a856b9aSJohnny Huang static int do_otpread(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
214569d5fd8fSJohnny Huang {
21462a856b9aSJohnny Huang 	uint32_t offset, count;
21472a856b9aSJohnny Huang 	int ret;
214869d5fd8fSJohnny Huang 
21492a856b9aSJohnny Huang 	if (argc == 4) {
21502a856b9aSJohnny Huang 		offset = simple_strtoul(argv[2], NULL, 16);
21512a856b9aSJohnny Huang 		count = simple_strtoul(argv[3], NULL, 16);
21522a856b9aSJohnny Huang 	} else if (argc == 3) {
21532a856b9aSJohnny Huang 		offset = simple_strtoul(argv[2], NULL, 16);
21542a856b9aSJohnny Huang 		count = 1;
21552a856b9aSJohnny Huang 	} else {
215669d5fd8fSJohnny Huang 		return CMD_RET_USAGE;
215769d5fd8fSJohnny Huang 	}
215869d5fd8fSJohnny Huang 
215969d5fd8fSJohnny Huang 
21602a856b9aSJohnny Huang 	if (!strcmp(argv[1], "conf")) {
21613d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
21622a856b9aSJohnny Huang 		ret = otp_print_config(offset, count);
21632a856b9aSJohnny Huang 	} else if (!strcmp(argv[1], "data")) {
21643d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
21652a856b9aSJohnny Huang 		ret = otp_print_data(offset, count);
21662a856b9aSJohnny Huang 	} else if (!strcmp(argv[1], "strap")) {
21673d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
21682a856b9aSJohnny Huang 		ret = otp_print_strap(offset, count);
21692a856b9aSJohnny Huang 	} else {
21702a856b9aSJohnny Huang 		return CMD_RET_USAGE;
217169d5fd8fSJohnny Huang 	}
217269d5fd8fSJohnny Huang 
21732a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
21742a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
21752a856b9aSJohnny Huang 	else
21762a856b9aSJohnny Huang 		return CMD_RET_USAGE;
21772a856b9aSJohnny Huang 
21782a856b9aSJohnny Huang }
21792a856b9aSJohnny Huang 
21802a856b9aSJohnny Huang static int do_otpprog(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
21812a856b9aSJohnny Huang {
21822a856b9aSJohnny Huang 	phys_addr_t addr;
21832a856b9aSJohnny Huang 	uint32_t byte_size;
21842a856b9aSJohnny Huang 	int ret;
21852a856b9aSJohnny Huang 
21862a856b9aSJohnny Huang 	if (argc == 4) {
2187ed071a2bSJohnny Huang 		if (strcmp(argv[1], "o"))
21882a856b9aSJohnny Huang 			return CMD_RET_USAGE;
21892a856b9aSJohnny Huang 		addr = simple_strtoul(argv[2], NULL, 16);
21902a856b9aSJohnny Huang 		byte_size = simple_strtoul(argv[3], NULL, 16);
21913d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
21922a856b9aSJohnny Huang 		ret = do_otp_prog(addr, byte_size, 1);
21932a856b9aSJohnny Huang 	} else if (argc == 3) {
21942a856b9aSJohnny Huang 		addr = simple_strtoul(argv[1], NULL, 16);
21952a856b9aSJohnny Huang 		byte_size = simple_strtoul(argv[2], NULL, 16);
21963d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
21972a856b9aSJohnny Huang 		ret = do_otp_prog(addr, byte_size, 0);
21982a856b9aSJohnny Huang 	} else {
21992a856b9aSJohnny Huang 		return CMD_RET_USAGE;
22002a856b9aSJohnny Huang 	}
22012a856b9aSJohnny Huang 
22022a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
22032a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
22042a856b9aSJohnny Huang 	else if (ret == OTP_FAILURE)
22052a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
22062a856b9aSJohnny Huang 	else
22072a856b9aSJohnny Huang 		return CMD_RET_USAGE;
22082a856b9aSJohnny Huang }
22092a856b9aSJohnny Huang 
22102a856b9aSJohnny Huang static int do_otppb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
22112a856b9aSJohnny Huang {
22122a856b9aSJohnny Huang 	int mode = 0;
22132a856b9aSJohnny Huang 	int nconfirm = 0;
22142a856b9aSJohnny Huang 	int otp_addr = 0;
22152a856b9aSJohnny Huang 	int bit_offset;
22162a856b9aSJohnny Huang 	int value;
22172a856b9aSJohnny Huang 	int ret;
22182a856b9aSJohnny Huang 
22192a856b9aSJohnny Huang 	if (argc != 4 && argc != 5 && argc != 6)
22202a856b9aSJohnny Huang 		return CMD_RET_USAGE;
22212a856b9aSJohnny Huang 
22222a856b9aSJohnny Huang 	/* Drop the pb cmd */
22232a856b9aSJohnny Huang 	argc--;
22242a856b9aSJohnny Huang 	argv++;
22252a856b9aSJohnny Huang 
22262a856b9aSJohnny Huang 	if (!strcmp(argv[0], "conf"))
2227a6d0d645SJohnny Huang 		mode = OTP_REGION_CONF;
22282a856b9aSJohnny Huang 	else if (!strcmp(argv[0], "strap"))
2229a6d0d645SJohnny Huang 		mode = OTP_REGION_STRAP;
22302a856b9aSJohnny Huang 	else if (!strcmp(argv[0], "data"))
2231a6d0d645SJohnny Huang 		mode = OTP_REGION_DATA;
2232cd1610b4SJohnny Huang 	else
22332a856b9aSJohnny Huang 		return CMD_RET_USAGE;
22342a856b9aSJohnny Huang 
22352a856b9aSJohnny Huang 	/* Drop the region cmd */
22362a856b9aSJohnny Huang 	argc--;
22372a856b9aSJohnny Huang 	argv++;
22382a856b9aSJohnny Huang 
2239ed071a2bSJohnny Huang 	if (!strcmp(argv[0], "o")) {
2240cd1610b4SJohnny Huang 		nconfirm = 1;
22412a856b9aSJohnny Huang 		/* Drop the force option */
22422a856b9aSJohnny Huang 		argc--;
22432a856b9aSJohnny Huang 		argv++;
22442a856b9aSJohnny Huang 	}
2245cd1610b4SJohnny Huang 
2246a6d0d645SJohnny Huang 	if (mode == OTP_REGION_STRAP) {
22472a856b9aSJohnny Huang 		bit_offset = simple_strtoul(argv[0], NULL, 16);
22482a856b9aSJohnny Huang 		value = simple_strtoul(argv[1], NULL, 16);
22490808cc55SJohnny Huang 		if (bit_offset >= 64 || (value != 0 && value != 1))
22502a856b9aSJohnny Huang 			return CMD_RET_USAGE;
2251cd1610b4SJohnny Huang 	} else {
22522a856b9aSJohnny Huang 		otp_addr = simple_strtoul(argv[0], NULL, 16);
22532a856b9aSJohnny Huang 		bit_offset = simple_strtoul(argv[1], NULL, 16);
22542a856b9aSJohnny Huang 		value = simple_strtoul(argv[2], NULL, 16);
22550808cc55SJohnny Huang 		if (bit_offset >= 32 || (value != 0 && value != 1))
22562a856b9aSJohnny Huang 			return CMD_RET_USAGE;
22570808cc55SJohnny Huang 		if (mode == OTP_REGION_DATA) {
225878855207SJohnny Huang 			if (otp_addr >= 0x800)
22590808cc55SJohnny Huang 				return CMD_RET_USAGE;
22600808cc55SJohnny Huang 		} else {
226178855207SJohnny Huang 			if (otp_addr >= 0x20)
22620808cc55SJohnny Huang 				return CMD_RET_USAGE;
22630808cc55SJohnny Huang 		}
2264cd1610b4SJohnny Huang 	}
2265cd1610b4SJohnny Huang 	if (value != 0 && value != 1)
22662a856b9aSJohnny Huang 		return CMD_RET_USAGE;
2267cd1610b4SJohnny Huang 
22683d3688adSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
22692a856b9aSJohnny Huang 	ret = do_otp_prog_bit(mode, otp_addr, bit_offset, value, nconfirm);
22702a856b9aSJohnny Huang 
22712a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
22722a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
22732a856b9aSJohnny Huang 	else if (ret == OTP_FAILURE)
22742a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
22752a856b9aSJohnny Huang 	else
22762a856b9aSJohnny Huang 		return CMD_RET_USAGE;
22772a856b9aSJohnny Huang }
22782a856b9aSJohnny Huang 
22792a856b9aSJohnny Huang static int do_otpcmp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
22802a856b9aSJohnny Huang {
22812a856b9aSJohnny Huang 	phys_addr_t addr;
22822a856b9aSJohnny Huang 	int otp_addr = 0;
22832a856b9aSJohnny Huang 
22842a856b9aSJohnny Huang 	if (argc != 3)
22852a856b9aSJohnny Huang 		return CMD_RET_USAGE;
22862a856b9aSJohnny Huang 
22873d3688adSJohnny Huang 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
22882a856b9aSJohnny Huang 	addr = simple_strtoul(argv[1], NULL, 16);
22892a856b9aSJohnny Huang 	otp_addr = simple_strtoul(argv[2], NULL, 16);
22902a856b9aSJohnny Huang 	if (otp_compare(otp_addr, addr) == 0) {
229169d5fd8fSJohnny Huang 		printf("Compare pass\n");
22922a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
229369d5fd8fSJohnny Huang 	} else {
229469d5fd8fSJohnny Huang 		printf("Compare fail\n");
22952a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
229669d5fd8fSJohnny Huang 	}
229769d5fd8fSJohnny Huang }
229869d5fd8fSJohnny Huang 
229966f2f8e5SJohnny Huang static int do_otpinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
230066f2f8e5SJohnny Huang {
2301a8bd6d8cSJohnny Huang 	int view = 0;
23022d4b0742SJohnny Huang 	int input;
2303a8bd6d8cSJohnny Huang 
2304a8bd6d8cSJohnny Huang 	if (argc != 2 && argc != 3)
230566f2f8e5SJohnny Huang 		return CMD_RET_USAGE;
230666f2f8e5SJohnny Huang 
23072d4b0742SJohnny Huang 	if (!strcmp(argv[1], "conf")) {
230866f2f8e5SJohnny Huang 
23093d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
23102d4b0742SJohnny Huang 		if (argc == 3) {
23112d4b0742SJohnny Huang 			input = simple_strtoul(argv[2], NULL, 16);
23122d4b0742SJohnny Huang 			otp_print_conf_info(input);
23132d4b0742SJohnny Huang 		} else {
23142d4b0742SJohnny Huang 			otp_print_conf_info(-1);
23152d4b0742SJohnny Huang 		}
23162d4b0742SJohnny Huang 	} else if (!strcmp(argv[1], "strap")) {
23172d4b0742SJohnny Huang 		if (!strcmp(argv[2], "v")) {
2318a8bd6d8cSJohnny Huang 			view = 1;
2319a8bd6d8cSJohnny Huang 			/* Drop the view option */
2320a8bd6d8cSJohnny Huang 			argc--;
2321a8bd6d8cSJohnny Huang 			argv++;
2322a8bd6d8cSJohnny Huang 		}
23233d3688adSJohnny Huang 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2324b458cd62SJohnny Huang 		otp_print_strap_info(view);
232566f2f8e5SJohnny Huang 	} else {
232666f2f8e5SJohnny Huang 		return CMD_RET_USAGE;
232766f2f8e5SJohnny Huang 	}
23282d4b0742SJohnny Huang 
232966f2f8e5SJohnny Huang 	return CMD_RET_SUCCESS;
233066f2f8e5SJohnny Huang }
233166f2f8e5SJohnny Huang 
2332737ed20bSJohnny Huang static int do_otpprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2333737ed20bSJohnny Huang {
2334737ed20bSJohnny Huang 	int input;
2335737ed20bSJohnny Huang 	int bit_offset;
2336737ed20bSJohnny Huang 	int prog_address;
2337737ed20bSJohnny Huang 	int pass;
2338737ed20bSJohnny Huang 	int i;
2339737ed20bSJohnny Huang 	if (argc != 3 && argc != 2)
2340737ed20bSJohnny Huang 		return CMD_RET_USAGE;
2341737ed20bSJohnny Huang 
2342ed071a2bSJohnny Huang 	if (!strcmp(argv[0], "o")) {
2343737ed20bSJohnny Huang 		input = simple_strtoul(argv[2], NULL, 16);
2344737ed20bSJohnny Huang 	} else {
2345737ed20bSJohnny Huang 		input = simple_strtoul(argv[1], NULL, 16);
2346737ed20bSJohnny Huang 		printf("OTPSTRAP[%d] will be protected\n", input);
2347737ed20bSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
2348737ed20bSJohnny Huang 		if (!confirm_yesno()) {
2349737ed20bSJohnny Huang 			printf(" Aborting\n");
2350737ed20bSJohnny Huang 			return CMD_RET_FAILURE;
2351737ed20bSJohnny Huang 		}
2352737ed20bSJohnny Huang 	}
2353737ed20bSJohnny Huang 
2354737ed20bSJohnny Huang 	prog_address = 0x800;
2355737ed20bSJohnny Huang 	if (input < 32) {
2356737ed20bSJohnny Huang 		bit_offset = input;
2357737ed20bSJohnny Huang 		prog_address |= 0x60c;
2358737ed20bSJohnny Huang 	} else if (input < 64) {
2359737ed20bSJohnny Huang 		bit_offset = input - 32;
2360737ed20bSJohnny Huang 		prog_address |= 0x60e;
2361737ed20bSJohnny Huang 	} else {
2362737ed20bSJohnny Huang 		return CMD_RET_USAGE;
2363737ed20bSJohnny Huang 	}
2364737ed20bSJohnny Huang 
2365737ed20bSJohnny Huang 	if (verify_bit(prog_address, bit_offset, 1) == 0) {
2366737ed20bSJohnny Huang 		printf("OTPSTRAP[%d] already protected\n", input);
2367737ed20bSJohnny Huang 	}
2368de6fbf1cSJohnny Huang 
2369de6fbf1cSJohnny Huang 	otp_soak(1);
2370de6fbf1cSJohnny Huang 	otp_prog_bit(1, prog_address, bit_offset);
2371de6fbf1cSJohnny Huang 	pass = 0;
2372de6fbf1cSJohnny Huang 
2373737ed20bSJohnny Huang 	for (i = 0; i < RETRY; i++) {
2374737ed20bSJohnny Huang 		if (verify_bit(prog_address, bit_offset, 1) != 0) {
2375de6fbf1cSJohnny Huang 			otp_soak(2);
2376de6fbf1cSJohnny Huang 			otp_prog_bit(1, prog_address, bit_offset);
2377de6fbf1cSJohnny Huang 			if (verify_bit(prog_address, bit_offset, 1) != 0) {
2378de6fbf1cSJohnny Huang 				otp_soak(1);
2379737ed20bSJohnny Huang 			} else {
2380de6fbf1cSJohnny Huang 				pass = 1;
2381de6fbf1cSJohnny Huang 				break;
2382de6fbf1cSJohnny Huang 			}
2383de6fbf1cSJohnny Huang 		} else {
2384de6fbf1cSJohnny Huang 			pass = 1;
2385737ed20bSJohnny Huang 			break;
2386737ed20bSJohnny Huang 		}
2387737ed20bSJohnny Huang 	}
2388de6fbf1cSJohnny Huang 	otp_soak(0);
2389de6fbf1cSJohnny Huang 	if (pass) {
2390737ed20bSJohnny Huang 		printf("OTPSTRAP[%d] is protected\n", input);
2391737ed20bSJohnny Huang 		return CMD_RET_SUCCESS;
2392737ed20bSJohnny Huang 	}
2393737ed20bSJohnny Huang 
2394737ed20bSJohnny Huang 	printf("Protect OTPSTRAP[%d] fail\n", input);
2395737ed20bSJohnny Huang 	return CMD_RET_FAILURE;
2396737ed20bSJohnny Huang 
2397737ed20bSJohnny Huang }
23989a4fe690SJohnny Huang 
23992a856b9aSJohnny Huang static cmd_tbl_t cmd_otp[] = {
24002a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(read, 4, 0, do_otpread, "", ""),
2401a8bd6d8cSJohnny Huang 	U_BOOT_CMD_MKENT(info, 3, 0, do_otpinfo, "", ""),
24022a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(prog, 4, 0, do_otpprog, "", ""),
24032a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(pb, 6, 0, do_otppb, "", ""),
2404737ed20bSJohnny Huang 	U_BOOT_CMD_MKENT(protect, 3, 0, do_otpprotect, "", ""),
24052a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(cmp, 3, 0, do_otpcmp, "", ""),
24062a856b9aSJohnny Huang };
24072a856b9aSJohnny Huang 
24082a856b9aSJohnny Huang static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
24092a856b9aSJohnny Huang {
24102a856b9aSJohnny Huang 	cmd_tbl_t *cp;
24112a856b9aSJohnny Huang 
24122a856b9aSJohnny Huang 	cp = find_cmd_tbl(argv[1], cmd_otp, ARRAY_SIZE(cmd_otp));
24132a856b9aSJohnny Huang 
2414737ed20bSJohnny Huang 	/* Drop the otp command */
24152a856b9aSJohnny Huang 	argc--;
24162a856b9aSJohnny Huang 	argv++;
24172a856b9aSJohnny Huang 
24182a856b9aSJohnny Huang 	if (cp == NULL || argc > cp->maxargs)
24192a856b9aSJohnny Huang 		return CMD_RET_USAGE;
24202a856b9aSJohnny Huang 	if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
24212a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
24222a856b9aSJohnny Huang 
2423696656c6SJohnny Huang 	if (chip_version() == OTP_AST2600A0) {
2424696656c6SJohnny Huang 		info_cb.version = OTP_AST2600A0;
24259a4fe690SJohnny Huang 		info_cb.conf_info = a0_conf_info;
24269a4fe690SJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a0_conf_info);
24279a4fe690SJohnny Huang 		info_cb.strap_info = a0_strap_info;
24289a4fe690SJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a0_strap_info);
24299a4fe690SJohnny Huang 		info_cb.key_info = a0_key_type;
24309a4fe690SJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a0_key_type);
2431696656c6SJohnny Huang 	} else if (chip_version() == OTP_AST2600A1) {
2432696656c6SJohnny Huang 		info_cb.version = OTP_AST2600A1;
24333cb28812SJohnny Huang 		info_cb.conf_info = a1_conf_info;
24343cb28812SJohnny Huang 		info_cb.conf_info_len = ARRAY_SIZE(a1_conf_info);
24353cb28812SJohnny Huang 		info_cb.strap_info = a1_strap_info;
24363cb28812SJohnny Huang 		info_cb.strap_info_len = ARRAY_SIZE(a1_strap_info);
24379a4fe690SJohnny Huang 		info_cb.key_info = a1_key_type;
24389a4fe690SJohnny Huang 		info_cb.key_info_len = ARRAY_SIZE(a1_key_type);
24399a4fe690SJohnny Huang 	}
24409a4fe690SJohnny Huang 
24412a856b9aSJohnny Huang 	return cp->cmd(cmdtp, flag, argc, argv);
244269d5fd8fSJohnny Huang }
244369d5fd8fSJohnny Huang 
244469d5fd8fSJohnny Huang U_BOOT_CMD(
244569d5fd8fSJohnny Huang 	otp, 7, 0,  do_ast_otp,
244669d5fd8fSJohnny Huang 	"ASPEED One-Time-Programmable sub-system",
24472a856b9aSJohnny Huang 	"read conf|data <otp_dw_offset> <dw_count>\n"
24482a856b9aSJohnny Huang 	"otp read strap <strap_bit_offset> <bit_count>\n"
24492d4b0742SJohnny Huang 	"otp info strap [v]\n"
24502d4b0742SJohnny Huang 	"otp info conf [otp_dw_offset]\n"
2451ed071a2bSJohnny Huang 	"otp prog [o] <addr> <byte_size>\n"
2452ed071a2bSJohnny Huang 	"otp pb conf|data [o] <otp_dw_offset> <bit_offset> <value>\n"
2453ed071a2bSJohnny Huang 	"otp pb strap [o] <bit_offset> <value>\n"
2454ed071a2bSJohnny Huang 	"otp protect [o] <bit_offset>\n"
24552a856b9aSJohnny Huang 	"otp cmp <addr> <otp_dw_offset>\n"
245669d5fd8fSJohnny Huang );
2457