xref: /openbmc/u-boot/cmd/otp.c (revision c947ef08e1975bdcc0fec4b2b83be3ba109792c5)
169d5fd8fSJohnny Huang /*
269d5fd8fSJohnny Huang  *  This program is distributed in the hope that it will be useful,
369d5fd8fSJohnny Huang  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
469d5fd8fSJohnny Huang  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
569d5fd8fSJohnny Huang  *  GNU General Public License for more details.
669d5fd8fSJohnny Huang  *
769d5fd8fSJohnny Huang  *  You should have received a copy of the GNU General Public License
869d5fd8fSJohnny Huang  *  along with this program; if not, write to the Free Software
969d5fd8fSJohnny Huang  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1069d5fd8fSJohnny Huang  */
114c1c9b35SJohnny Huang #include <stdlib.h>
1269d5fd8fSJohnny Huang #include <common.h>
1369d5fd8fSJohnny Huang #include <console.h>
1469d5fd8fSJohnny Huang #include <bootretry.h>
1569d5fd8fSJohnny Huang #include <cli.h>
1669d5fd8fSJohnny Huang #include <command.h>
1769d5fd8fSJohnny Huang #include <console.h>
184c1c9b35SJohnny Huang #include <malloc.h>
1969d5fd8fSJohnny Huang #include <inttypes.h>
2069d5fd8fSJohnny Huang #include <mapmem.h>
2169d5fd8fSJohnny Huang #include <asm/io.h>
2269d5fd8fSJohnny Huang #include <linux/compiler.h>
2369d5fd8fSJohnny Huang 
2469d5fd8fSJohnny Huang DECLARE_GLOBAL_DATA_PTR;
2569d5fd8fSJohnny Huang 
2669d5fd8fSJohnny Huang #define OTP_PASSWD			0x349fe38a
2769d5fd8fSJohnny Huang #define RETRY				3
28a6d0d645SJohnny Huang #define OTP_REGION_STRAP		1
29a6d0d645SJohnny Huang #define OTP_REGION_CONF			2
30a6d0d645SJohnny Huang #define OTP_REGION_DATA			3
31a6d0d645SJohnny Huang #define OTP_REGION_ALL			4
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 
39a8bd6d8cSJohnny Huang #define OTP_REG_RESERVED		-1
40b458cd62SJohnny Huang #define OTP_REG_VALUE			-2
41b458cd62SJohnny Huang #define OTP_REG_VALID_BIT		-3
4276d13988SJohnny Huang 
434c1c9b35SJohnny Huang #define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
444c1c9b35SJohnny Huang #define PBWIDTH 60
454c1c9b35SJohnny Huang 
4666f2f8e5SJohnny Huang struct otpstrap_status {
4769d5fd8fSJohnny Huang 	int value;
4869d5fd8fSJohnny Huang 	int option_array[7];
4969d5fd8fSJohnny Huang 	int remain_times;
5069d5fd8fSJohnny Huang 	int writeable_option;
5169d5fd8fSJohnny Huang 	int protected;
5269d5fd8fSJohnny Huang };
5369d5fd8fSJohnny Huang 
5466f2f8e5SJohnny Huang struct otpconf_parse {
5566f2f8e5SJohnny Huang 	int dw_offset;
5666f2f8e5SJohnny Huang 	int bit;
5766f2f8e5SJohnny Huang 	int length;
5866f2f8e5SJohnny Huang 	int value;
5966f2f8e5SJohnny Huang 	int keep;
6066f2f8e5SJohnny Huang 	char status[80];
6166f2f8e5SJohnny Huang };
6266f2f8e5SJohnny Huang 
63a8bd6d8cSJohnny Huang struct otpstrap_info {
64a8bd6d8cSJohnny Huang 	uint32_t bit_offset;
65a8bd6d8cSJohnny Huang 	uint32_t length;
6676d13988SJohnny Huang 	int value;
67a8bd6d8cSJohnny Huang 	char information[80];
68a8bd6d8cSJohnny Huang };
69a8bd6d8cSJohnny Huang 
70a8bd6d8cSJohnny Huang struct otpconf_info {
71a8bd6d8cSJohnny Huang 	uint32_t dw_offset;
72a8bd6d8cSJohnny Huang 	uint32_t bit_offset;
73a8bd6d8cSJohnny Huang 	uint32_t length;
74a8bd6d8cSJohnny Huang 	int value;
75a8bd6d8cSJohnny Huang 	char information[80];
76a8bd6d8cSJohnny Huang };
77a8bd6d8cSJohnny Huang 
78a8bd6d8cSJohnny Huang void printProgress(int numerator, int denominator, char *format, ...)
79a8bd6d8cSJohnny Huang {
80a8bd6d8cSJohnny Huang 	int val = numerator * 100 / denominator;
81a8bd6d8cSJohnny Huang 	int lpad = numerator * PBWIDTH / denominator;
82a8bd6d8cSJohnny Huang 	int rpad = PBWIDTH - lpad;
83a8bd6d8cSJohnny Huang 	char buffer[256];
84a8bd6d8cSJohnny Huang 	va_list aptr;
85a8bd6d8cSJohnny Huang 
86a8bd6d8cSJohnny Huang 	va_start(aptr, format);
87a8bd6d8cSJohnny Huang 	vsprintf(buffer, format, aptr);
88a8bd6d8cSJohnny Huang 	va_end(aptr);
89a8bd6d8cSJohnny Huang 
90a8bd6d8cSJohnny Huang 	printf("\r%3d%% [%.*s%*s] %s", val, lpad, PBSTR, rpad, "", buffer);
91a8bd6d8cSJohnny Huang 	if (numerator == denominator)
92a8bd6d8cSJohnny Huang 		printf("\n");
93a8bd6d8cSJohnny Huang }
94a8bd6d8cSJohnny Huang 
95a8bd6d8cSJohnny Huang struct otpstrap_info a0_strap_info[] = {
96a8bd6d8cSJohnny Huang 	{
97a8bd6d8cSJohnny Huang 		0, 1, 0, "Disable secure boot"
98a8bd6d8cSJohnny Huang 	}, {
99a8bd6d8cSJohnny Huang 		0, 1, 1, "Enable secure boot"
100a8bd6d8cSJohnny Huang 	}, {
101a8bd6d8cSJohnny Huang 		1, 1, 0, "Disable boot from eMMC"
102a8bd6d8cSJohnny Huang 	}, {
103a8bd6d8cSJohnny Huang 		1, 1, 1, "Enable boot from eMMC"
104a8bd6d8cSJohnny Huang 	}, {
105a8bd6d8cSJohnny Huang 		2, 1, 0, "Disable Boot from debug SPI"
106a8bd6d8cSJohnny Huang 	}, {
107a8bd6d8cSJohnny Huang 		2, 1, 1, "Enable Boot from debug SPI"
108a8bd6d8cSJohnny Huang 	}, {
109a8bd6d8cSJohnny Huang 		3, 1, 0, "Enable ARM CM3"
110a8bd6d8cSJohnny Huang 	}, {
111a8bd6d8cSJohnny Huang 		3, 1, 1, "Disable ARM CM3"
112a8bd6d8cSJohnny Huang 	}, {
113a8bd6d8cSJohnny Huang 		4, 1, 0, "No VGA BISO ROM, VGA BIOS is merged in the system BIOS"
114a8bd6d8cSJohnny Huang 	}, {
115a8bd6d8cSJohnny Huang 		4, 1, 1, "Enable dedicated VGA BIOS ROM"
116a8bd6d8cSJohnny Huang 	}, {
117a8bd6d8cSJohnny Huang 		5, 1, 0, "MAC 1 : RMII/NCSI"
118a8bd6d8cSJohnny Huang 	}, {
119a8bd6d8cSJohnny Huang 		5, 1, 1, "MAC 1 : RGMII"
120a8bd6d8cSJohnny Huang 	}, {
121a8bd6d8cSJohnny Huang 		6, 1, 0, "MAC 2 : RMII/NCSI"
122a8bd6d8cSJohnny Huang 	}, {
123a8bd6d8cSJohnny Huang 		6, 1, 1, "MAC 2 : RGMII"
124a8bd6d8cSJohnny Huang 	}, {
125a8bd6d8cSJohnny Huang 		7, 3, 0, "CPU Frequency : 1GHz"
126a8bd6d8cSJohnny Huang 	}, {
127a8bd6d8cSJohnny Huang 		7, 3, 1, "CPU Frequency : 800MHz"
128a8bd6d8cSJohnny Huang 	}, {
129a8bd6d8cSJohnny Huang 		7, 3, 2, "CPU Frequency : 1.2GHz"
130a8bd6d8cSJohnny Huang 	}, {
131a8bd6d8cSJohnny Huang 		7, 3, 3, "CPU Frequency : 1.4GHz"
132a8bd6d8cSJohnny Huang 	}, {
133a8bd6d8cSJohnny Huang 		10, 2, 0, "HCLK ratio AXI:AHB = 2:1"
134a8bd6d8cSJohnny Huang 	}, {
135a8bd6d8cSJohnny Huang 		10, 2, 1, "HCLK ratio AXI:AHB = 2:1"
136a8bd6d8cSJohnny Huang 	}, {
137a8bd6d8cSJohnny Huang 		10, 2, 2, "HCLK ratio AXI:AHB = 3:1"
138a8bd6d8cSJohnny Huang 	}, {
139a8bd6d8cSJohnny Huang 		10, 2, 3, "HCLK ratio AXI:AHB = 4:1"
140a8bd6d8cSJohnny Huang 	}, {
141a8bd6d8cSJohnny Huang 		12, 2, 0, "VGA memory size : 8MB"
142a8bd6d8cSJohnny Huang 	}, {
143a8bd6d8cSJohnny Huang 		12, 2, 1, "VGA memory size : 16MB"
144a8bd6d8cSJohnny Huang 	}, {
145a8bd6d8cSJohnny Huang 		12, 2, 2, "VGA memory size : 32MB"
146a8bd6d8cSJohnny Huang 	}, {
147a8bd6d8cSJohnny Huang 		12, 2, 3, "VGA memory size : 64MB"
148a8bd6d8cSJohnny Huang 	}, {
149a8bd6d8cSJohnny Huang 		14, 3, OTP_REG_RESERVED, ""
150a8bd6d8cSJohnny Huang 	}, {
151a8bd6d8cSJohnny Huang 		17, 1, 0, "VGA class code : Class Code for video device"
152a8bd6d8cSJohnny Huang 	}, {
153a8bd6d8cSJohnny Huang 		17, 1, 1, "VGA class code : Class Code for VGA device"
154a8bd6d8cSJohnny Huang 	}, {
155a8bd6d8cSJohnny Huang 		18, 1, 0, "Enable debug interfaces 0"
156a8bd6d8cSJohnny Huang 	}, {
157a8bd6d8cSJohnny Huang 		18, 1, 1, "Disable debug interfaces 0"
158a8bd6d8cSJohnny Huang 	}, {
159a8bd6d8cSJohnny Huang 		19, 1, 0, "Boot from emmc mode : High eMMC speed"
160a8bd6d8cSJohnny Huang 	}, {
161a8bd6d8cSJohnny Huang 		19, 1, 1, "Boot from emmc mode : Normal eMMC speed"
162a8bd6d8cSJohnny Huang 	}, {
163a8bd6d8cSJohnny Huang 		20, 1, 0, "Enable Pcie EHCI device"
164a8bd6d8cSJohnny Huang 	}, {
165a8bd6d8cSJohnny Huang 		20, 1, 1, "Disable Pcie EHCI device"
166a8bd6d8cSJohnny Huang 	}, {
167a8bd6d8cSJohnny Huang 		21, 1, 0, "Enable VGA XDMA function"
168a8bd6d8cSJohnny Huang 	}, {
169a8bd6d8cSJohnny Huang 		21, 1, 1, "Disable VGA XDMA function"
170a8bd6d8cSJohnny Huang 	}, {
171a8bd6d8cSJohnny Huang 		22, 1, 0, "Normal BMC mode"
172a8bd6d8cSJohnny Huang 	}, {
173a8bd6d8cSJohnny Huang 		22, 1, 1, "Disable dedicated BMC functions for non-BMC application"
174a8bd6d8cSJohnny Huang 	}, {
175a8bd6d8cSJohnny Huang 		23, 1, 0, "SSPRST# pin is for secondary processor dedicated reset pin"
176a8bd6d8cSJohnny Huang 	}, {
177a8bd6d8cSJohnny Huang 		23, 1, 1, "SSPRST# pin is for PCIE root complex dedicated reset pin"
178a8bd6d8cSJohnny Huang 	}, {
179a8bd6d8cSJohnny Huang 		24, 1, 0, "DRAM types : DDR3"
180a8bd6d8cSJohnny Huang 	}, {
181a8bd6d8cSJohnny Huang 		24, 1, 1, "DRAM types : DDR4"
182a8bd6d8cSJohnny Huang 	}, {
183a8bd6d8cSJohnny Huang 		25, 5, OTP_REG_RESERVED, ""
184a8bd6d8cSJohnny Huang 	}, {
185a8bd6d8cSJohnny Huang 		30, 2, OTP_REG_RESERVED, ""
186a8bd6d8cSJohnny Huang 	}, {
187a8bd6d8cSJohnny Huang 		32, 1, 0, "MAC 3 : RMII/NCSI"
188a8bd6d8cSJohnny Huang 	}, {
189a8bd6d8cSJohnny Huang 		32, 1, 1, "MAC 3 : RGMII"
190a8bd6d8cSJohnny Huang 	}, {
191a8bd6d8cSJohnny Huang 		33, 1, 0, "MAC 4 : RMII/NCSI"
192a8bd6d8cSJohnny Huang 	}, {
193a8bd6d8cSJohnny Huang 		33, 1, 1, "MAC 4 : RGMII"
194a8bd6d8cSJohnny Huang 	}, {
195a8bd6d8cSJohnny Huang 		34, 1, 0, "SuperIO configuration address : 0x2E"
196a8bd6d8cSJohnny Huang 	}, {
197a8bd6d8cSJohnny Huang 		34, 1, 1, "SuperIO configuration address : 0x4E"
198a8bd6d8cSJohnny Huang 	}, {
199a8bd6d8cSJohnny Huang 		35, 1, 0, "Enable LPC to decode SuperIO"
200a8bd6d8cSJohnny Huang 	}, {
201a8bd6d8cSJohnny Huang 		35, 1, 1, "Disable LPC to decode SuperIO"
202a8bd6d8cSJohnny Huang 	}, {
203a8bd6d8cSJohnny Huang 		36, 1, 0, "Enable debug interfaces 1"
204a8bd6d8cSJohnny Huang 	}, {
205a8bd6d8cSJohnny Huang 		36, 1, 1, "Disable debug interfaces 1"
206a8bd6d8cSJohnny Huang 	}, {
207a8bd6d8cSJohnny Huang 		37, 1, 0, "Disable ACPI function"
208a8bd6d8cSJohnny Huang 	}, {
209a8bd6d8cSJohnny Huang 		37, 1, 1, "Enable ACPI function"
210a8bd6d8cSJohnny Huang 	}, {
211a8bd6d8cSJohnny Huang 		38, 1, 0, "Enable eSPI mode"
212a8bd6d8cSJohnny Huang 	}, {
213a8bd6d8cSJohnny Huang 		38, 1, 1, "Enable LPC mode"
214a8bd6d8cSJohnny Huang 	}, {
215a8bd6d8cSJohnny Huang 		39, 1, 0, "Enable SAFS mode"
216a8bd6d8cSJohnny Huang 	}, {
217a8bd6d8cSJohnny Huang 		39, 1, 1, "Enable SAFS mode"
218a8bd6d8cSJohnny Huang 	}, {
219a8bd6d8cSJohnny Huang 		40, 2, OTP_REG_RESERVED, ""
220a8bd6d8cSJohnny Huang 	}, {
221a8bd6d8cSJohnny Huang 		42, 1, 0, "Disable boot SPI 3B/4B address mode auto detection"
222a8bd6d8cSJohnny Huang 	}, {
223a8bd6d8cSJohnny Huang 		42, 1, 1, "Enable boot SPI 3B/4B address mode auto detection"
224a8bd6d8cSJohnny Huang 	}, {
225a8bd6d8cSJohnny Huang 		43, 1, 0, "Disable boot SPI ABR"
226a8bd6d8cSJohnny Huang 	}, {
227a8bd6d8cSJohnny Huang 		43, 1, 1, "Enable boot SPI ABR"
228a8bd6d8cSJohnny Huang 	}, {
229a8bd6d8cSJohnny Huang 		44, 1, 0, "Boot SPI ABR mode : dual SPI flash"
230a8bd6d8cSJohnny Huang 	}, {
231a8bd6d8cSJohnny Huang 		44, 1, 1, "Boot SPI ABR mode : single SPI flash"
232a8bd6d8cSJohnny Huang 	}, {
233a8bd6d8cSJohnny Huang 		45, 3, 0, "Boot SPI flash size : no define size"
234a8bd6d8cSJohnny Huang 	}, {
235a8bd6d8cSJohnny Huang 		45, 3, 1, "Boot SPI flash size : 2MB"
236a8bd6d8cSJohnny Huang 	}, {
237a8bd6d8cSJohnny Huang 		45, 3, 2, "Boot SPI flash size : 4MB"
238a8bd6d8cSJohnny Huang 	}, {
239a8bd6d8cSJohnny Huang 		45, 3, 3, "Boot SPI flash size : 8MB"
240a8bd6d8cSJohnny Huang 	}, {
241a8bd6d8cSJohnny Huang 		45, 3, 4, "Boot SPI flash size : 16MB"
242a8bd6d8cSJohnny Huang 	}, {
243a8bd6d8cSJohnny Huang 		45, 3, 5, "Boot SPI flash size : 32MB"
244a8bd6d8cSJohnny Huang 	}, {
245a8bd6d8cSJohnny Huang 		45, 3, 6, "Boot SPI flash size : 64MB"
246a8bd6d8cSJohnny Huang 	}, {
247a8bd6d8cSJohnny Huang 		45, 3, 7, "Boot SPI flash size : 128MB"
248a8bd6d8cSJohnny Huang 	}, {
249a8bd6d8cSJohnny Huang 		48, 1, 0, "Disable host SPI ABR"
250a8bd6d8cSJohnny Huang 	}, {
251a8bd6d8cSJohnny Huang 		48, 1, 1, "Enable host SPI ABR"
252a8bd6d8cSJohnny Huang 	}, {
253a8bd6d8cSJohnny Huang 		49, 1, 0, "Disable host SPI ABR mode select pin"
254a8bd6d8cSJohnny Huang 	}, {
255a8bd6d8cSJohnny Huang 		49, 1, 1, "Enable host SPI ABR mode select pin"
256a8bd6d8cSJohnny Huang 	}, {
257a8bd6d8cSJohnny Huang 		50, 1, 0, "Host SPI ABR mode : dual SPI flash"
258a8bd6d8cSJohnny Huang 	}, {
259a8bd6d8cSJohnny Huang 		50, 1, 1, "Host SPI ABR mode : single SPI flash"
260a8bd6d8cSJohnny Huang 	}, {
261a8bd6d8cSJohnny Huang 		51, 3, 0, "Host SPI flash size : no define size"
262a8bd6d8cSJohnny Huang 	}, {
263a8bd6d8cSJohnny Huang 		51, 3, 1, "Host SPI flash size : 2MB"
264a8bd6d8cSJohnny Huang 	}, {
265a8bd6d8cSJohnny Huang 		51, 3, 2, "Host SPI flash size : 4MB"
266a8bd6d8cSJohnny Huang 	}, {
267a8bd6d8cSJohnny Huang 		51, 3, 3, "Host SPI flash size : 8MB"
268a8bd6d8cSJohnny Huang 	}, {
269a8bd6d8cSJohnny Huang 		51, 3, 4, "Host SPI flash size : 16MB"
270a8bd6d8cSJohnny Huang 	}, {
271a8bd6d8cSJohnny Huang 		51, 3, 5, "Host SPI flash size : 32MB"
272a8bd6d8cSJohnny Huang 	}, {
273a8bd6d8cSJohnny Huang 		51, 3, 6, "Host SPI flash size : 64MB"
274a8bd6d8cSJohnny Huang 	}, {
275a8bd6d8cSJohnny Huang 		51, 3, 7, "Host SPI flash size : 128MB"
276a8bd6d8cSJohnny Huang 	}, {
277a8bd6d8cSJohnny Huang 		54, 1, 0, "Disable boot SPI auxiliary control pins"
278a8bd6d8cSJohnny Huang 	}, {
279a8bd6d8cSJohnny Huang 		54, 1, 1, "Enable boot SPI auxiliary control pins"
280a8bd6d8cSJohnny Huang 	}, {
281a8bd6d8cSJohnny Huang 		55, 2, 0, "Boot SPI CRTM size : disable CRTM"
282a8bd6d8cSJohnny Huang 	}, {
283a8bd6d8cSJohnny Huang 		55, 2, 1, "Boot SPI CRTM size : 256KB"
284a8bd6d8cSJohnny Huang 	}, {
285a8bd6d8cSJohnny Huang 		55, 2, 2, "Boot SPI CRTM size : 512KB"
286a8bd6d8cSJohnny Huang 	}, {
287a8bd6d8cSJohnny Huang 		55, 2, 3, "Boot SPI CRTM size : 1MB"
288a8bd6d8cSJohnny Huang 	}, {
289a8bd6d8cSJohnny Huang 		57, 2, 0, "Host SPI CRTM size : disable CRTM"
290a8bd6d8cSJohnny Huang 	}, {
291a8bd6d8cSJohnny Huang 		57, 2, 1, "Host SPI CRTM size : 256KB"
292a8bd6d8cSJohnny Huang 	}, {
293a8bd6d8cSJohnny Huang 		57, 2, 2, "Host SPI CRTM size : 512KB"
294a8bd6d8cSJohnny Huang 	}, {
295a8bd6d8cSJohnny Huang 		57, 2, 3, "Host SPI CRTM size : 1MB"
296a8bd6d8cSJohnny Huang 	}, {
297a8bd6d8cSJohnny Huang 		59, 1, 0, "Disable host SPI auxiliary control pins"
298a8bd6d8cSJohnny Huang 	}, {
299a8bd6d8cSJohnny Huang 		59, 1, 1, "Enable host SPI auxiliary control pins"
300a8bd6d8cSJohnny Huang 	}, {
301a8bd6d8cSJohnny Huang 		60, 1, 0, "Disable GPIO pass through"
302a8bd6d8cSJohnny Huang 	}, {
303a8bd6d8cSJohnny Huang 		60, 1, 1, "Enable GPIO pass through"
304a8bd6d8cSJohnny Huang 	}, {
305a8bd6d8cSJohnny Huang 		61, 1, 0, "Enable low security secure boot key"
306a8bd6d8cSJohnny Huang 	}, {
307a8bd6d8cSJohnny Huang 		61, 1, 1, "Disable low security secure boot key"
308a8bd6d8cSJohnny Huang 	}, {
309a8bd6d8cSJohnny Huang 		62, 1, 0, "Disable dedicate GPIO strap pins"
310a8bd6d8cSJohnny Huang 	}, {
311a8bd6d8cSJohnny Huang 		62, 1, 1, "Enable dedicate GPIO strap pins"
312b458cd62SJohnny Huang 	}, {
313b458cd62SJohnny Huang 		63, 1, OTP_REG_RESERVED, ""
314a8bd6d8cSJohnny Huang 	}
31576d13988SJohnny Huang };
316b458cd62SJohnny Huang struct otpconf_info a0_conf_info[] = {
317b458cd62SJohnny Huang 	{
318b458cd62SJohnny Huang 		0, 0, 1, 0, "Enable Secure Region programming"
319b458cd62SJohnny Huang 	}, {
320b458cd62SJohnny Huang 		0, 0, 1, 1, "Disable Secure Region programming"
321b458cd62SJohnny Huang 	}, {
322b458cd62SJohnny Huang 		0, 1, 1, 0, "Disable Secure Boot"
323b458cd62SJohnny Huang 	}, {
324b458cd62SJohnny Huang 		0, 1, 1, 1, "Enable Secure Boot"
325b458cd62SJohnny Huang 	}, {
326b458cd62SJohnny Huang 		0, 2, 1, 0, "Initialization programming not done"
327b458cd62SJohnny Huang 	}, {
328b458cd62SJohnny Huang 		0, 2, 1, 1, "Initialization programming done"
329b458cd62SJohnny Huang 	}, {
330b458cd62SJohnny Huang 		0, 3, 1, 0, "User region ECC disable"
331b458cd62SJohnny Huang 	}, {
332b458cd62SJohnny Huang 		0, 3, 1, 1, "User region ECC enable"
333b458cd62SJohnny Huang 	}, {
334b458cd62SJohnny Huang 		0, 4, 1, 0, "Secure Region ECC disable"
335b458cd62SJohnny Huang 	}, {
336b458cd62SJohnny Huang 		0, 4, 1, 1, "Secure Region ECC enable"
337b458cd62SJohnny Huang 	}, {
338b458cd62SJohnny Huang 		0, 5, 1, 0, "Enable low security key"
339b458cd62SJohnny Huang 	}, {
340b458cd62SJohnny Huang 		0, 5, 1, 1, "Disable low security key"
341b458cd62SJohnny Huang 	}, {
342b458cd62SJohnny Huang 		0, 6, 1, 0, "Do not ignore Secure Boot hardware strap"
343b458cd62SJohnny Huang 	}, {
344b458cd62SJohnny Huang 		0, 6, 1, 1, "Ignore Secure Boot hardware strap"
345b458cd62SJohnny Huang 	}, {
346b458cd62SJohnny Huang 		0, 7, 1, 0, "Secure Boot Mode: 2"
347b458cd62SJohnny Huang 	}, {
348b458cd62SJohnny Huang 		0, 7, 1, 1, "Secure Boot Mode: 1"
349b458cd62SJohnny Huang 	}, {
350b458cd62SJohnny Huang 		0, 8, 2, 0, "Single cell mode (recommended)"
351b458cd62SJohnny Huang 	}, {
352b458cd62SJohnny Huang 		0, 8, 2, 1, "Differnetial mode"
353b458cd62SJohnny Huang 	}, {
354b458cd62SJohnny Huang 		0, 8, 2, 2, "Differential-redundant mode"
355b458cd62SJohnny Huang 	}, {
356b458cd62SJohnny Huang 		0, 10, 2, 0, "RSA mode : RSA1024"
357b458cd62SJohnny Huang 	}, {
358b458cd62SJohnny Huang 		0, 10, 2, 1, "RSA mode : RSA2048"
359b458cd62SJohnny Huang 	}, {
360b458cd62SJohnny Huang 		0, 10, 2, 2, "RSA mode : RSA3072"
361b458cd62SJohnny Huang 	}, {
362b458cd62SJohnny Huang 		0, 10, 2, 3, "RSA mode : RSA4096"
363b458cd62SJohnny Huang 	}, {
364b458cd62SJohnny Huang 		0, 12, 2, 0, "SHA mode : SHA224"
365b458cd62SJohnny Huang 	}, {
366b458cd62SJohnny Huang 		0, 12, 2, 1, "SHA mode : SHA256"
367b458cd62SJohnny Huang 	}, {
368b458cd62SJohnny Huang 		0, 12, 2, 2, "SHA mode : SHA384"
369b458cd62SJohnny Huang 	}, {
370b458cd62SJohnny Huang 		0, 12, 2, 3, "SHA mode : SHA512"
371b458cd62SJohnny Huang 	}, {
372b458cd62SJohnny Huang 		0, 14, 2, OTP_REG_RESERVED, ""
373b458cd62SJohnny Huang 	}, {
374b458cd62SJohnny Huang 		0, 16, 6, OTP_REG_VALUE, "Secure Region size (DW): 0x%x"
375b458cd62SJohnny Huang 	}, {
376b458cd62SJohnny Huang 		0, 22, 1, 0, "Secure Region : Writable"
377b458cd62SJohnny Huang 	}, {
378b458cd62SJohnny Huang 		0, 22, 1, 1, "Secure Region : Write Protect"
379b458cd62SJohnny Huang 	}, {
380b458cd62SJohnny Huang 		0, 23, 1, 0, "User Region : Writable"
381b458cd62SJohnny Huang 	}, {
382b458cd62SJohnny Huang 		0, 23, 1, 1, "User Region : Write Protect"
383b458cd62SJohnny Huang 	}, {
384b458cd62SJohnny Huang 		0, 24, 1, 0, "Configure Region : Writable"
385b458cd62SJohnny Huang 	}, {
386b458cd62SJohnny Huang 		0, 24, 1, 1, "Configure Region : Write Protect"
387b458cd62SJohnny Huang 	}, {
388b458cd62SJohnny Huang 		0, 25, 1, 0, "OTP strap Region : Writable"
389b458cd62SJohnny Huang 	}, {
390b458cd62SJohnny Huang 		0, 25, 1, 1, "OTP strap Region : Write Protect"
391b458cd62SJohnny Huang 	}, {
392b458cd62SJohnny Huang 		0, 26, 1, 0, "Disable Copy Boot Image to Internal SRAM"
393b458cd62SJohnny Huang 	}, {
394b458cd62SJohnny Huang 		0, 26, 1, 1, "Copy Boot Image to Internal SRAM"
395b458cd62SJohnny Huang 	}, {
396b458cd62SJohnny Huang 		0, 27, 1, 0, "Disable image encryption"
397b458cd62SJohnny Huang 	}, {
398b458cd62SJohnny Huang 		0, 27, 1, 1, "Enable image encryption"
399b458cd62SJohnny Huang 	}, {
400b458cd62SJohnny Huang 		0, 28, 1, OTP_REG_RESERVED, ""
401b458cd62SJohnny Huang 	}, {
402b458cd62SJohnny Huang 		0, 29, 1, 0, "OTP key retire Region : Writable"
403b458cd62SJohnny Huang 	}, {
404b458cd62SJohnny Huang 		0, 29, 1, 1, "OTP key retire Region : Write Protect"
405b458cd62SJohnny Huang 	}, {
406b458cd62SJohnny Huang 		0, 30, 1, 0, "SIPROM RED_EN redundancy repair disable"
407b458cd62SJohnny Huang 	}, {
408b458cd62SJohnny Huang 		0, 30, 1, 1, "SIPROM RED_EN redundancy repair enable"
409b458cd62SJohnny Huang 	}, {
410b458cd62SJohnny Huang 		0, 31, 1, 0, "SIPROM Mlock memory lock disable"
411b458cd62SJohnny Huang 	}, {
412b458cd62SJohnny Huang 		0, 31, 1, 1, "SIPROM Mlock memory lock enable"
413b458cd62SJohnny Huang 	}, {
414b458cd62SJohnny Huang 		2, 0, 16, OTP_REG_VALUE, "Vender ID : 0x%x"
415b458cd62SJohnny Huang 	}, {
416b458cd62SJohnny Huang 		2, 16, 16, OTP_REG_VALUE, "Key Revision : 0x%x"
417b458cd62SJohnny Huang 	}, {
418b458cd62SJohnny Huang 		3, 0, 16, OTP_REG_VALUE, "Secure boot header offset : 0x%x"
419b458cd62SJohnny Huang 	}, {
420b458cd62SJohnny Huang 		4, 0, 8, OTP_REG_VALID_BIT, "Keys valid  : %d"
421b458cd62SJohnny Huang 	}, {
422b458cd62SJohnny Huang 		4, 16, 8, OTP_REG_VALID_BIT, "Keys retire  : %d"
423b458cd62SJohnny Huang 	}, {
424b458cd62SJohnny Huang 		5, 0, 32, OTP_REG_VALUE, "User define data, random number low : 0x%x"
425b458cd62SJohnny Huang 	}, {
426b458cd62SJohnny Huang 		6, 0, 32, OTP_REG_VALUE, "User define data, random number high : 0x%x"
427b458cd62SJohnny Huang 	}, {
42807baa4e8SJohnny Huang 		7, 0, 1, 0, "Force enable PCI bus to AHB bus bridge"
42907baa4e8SJohnny Huang 	}, {
43007baa4e8SJohnny Huang 		7, 0, 1, 1, "Force disable PCI bus to AHB bus bridge"
43107baa4e8SJohnny Huang 	}, {
43207baa4e8SJohnny Huang 		7, 1, 1, 0, "Force enable UART5 debug port function"
43307baa4e8SJohnny Huang 	}, {
43407baa4e8SJohnny Huang 		7, 1, 1, 1, "Force disable UART5 debug port function"
43507baa4e8SJohnny Huang 	}, {
43607baa4e8SJohnny Huang 		7, 2, 1, 0, "Force enable XDMA function"
43707baa4e8SJohnny Huang 	}, {
43807baa4e8SJohnny Huang 		7, 2, 1, 1, "Force disable XDMA function"
43907baa4e8SJohnny Huang 	}, {
44007baa4e8SJohnny Huang 		7, 3, 1, 0, "Force enable APB to PCIE device bridge"
44107baa4e8SJohnny Huang 	}, {
44207baa4e8SJohnny Huang 		7, 3, 1, 1, "Force disable APB to PCIE device bridge"
44307baa4e8SJohnny Huang 	}, {
44407baa4e8SJohnny Huang 		7, 4, 1, 0, "Force enable APB to PCIE bridge config access"
44507baa4e8SJohnny Huang 	}, {
44607baa4e8SJohnny Huang 		7, 4, 1, 1, "Force disable APB to PCIE bridge config access"
44707baa4e8SJohnny Huang 	}, {
44807baa4e8SJohnny Huang 		7, 5, 1, 0, "Force enable PCIE bus trace buffer"
44907baa4e8SJohnny Huang 	}, {
45007baa4e8SJohnny Huang 		7, 5, 1, 1, "Force disable PCIE bus trace buffer"
45107baa4e8SJohnny Huang 	}, {
45207baa4e8SJohnny Huang 		7, 6, 1, 0, "Force enable the capability for PCIE device port as a Root Complex"
45307baa4e8SJohnny Huang 	}, {
45407baa4e8SJohnny Huang 		7, 6, 1, 1, "Force disable the capability for PCIE device port as a Root Complex"
45507baa4e8SJohnny Huang 	}, {
45607baa4e8SJohnny Huang 		7, 16, 1, 0, "Force enable ESPI bus to AHB bus bridge"
45707baa4e8SJohnny Huang 	}, {
45807baa4e8SJohnny Huang 		7, 16, 1, 1, "Force disable ESPI bus to AHB bus bridge"
45907baa4e8SJohnny Huang 	}, {
46007baa4e8SJohnny Huang 		7, 17, 1, 0, "Force enable LPC bus to AHB bus bridge1"
46107baa4e8SJohnny Huang 	}, {
46207baa4e8SJohnny Huang 		7, 17, 1, 1, "Force disable LPC bus to AHB bus bridge1"
46307baa4e8SJohnny Huang 	}, {
46407baa4e8SJohnny Huang 		7, 18, 1, 0, "Force enable LPC bus to AHB bus bridge2"
46507baa4e8SJohnny Huang 	}, {
46607baa4e8SJohnny Huang 		7, 18, 1, 1, "Force disable LPC bus to AHB bus bridge2"
46707baa4e8SJohnny Huang 	}, {
46807baa4e8SJohnny Huang 		7, 19, 1, 0, "Force enable UART1 debug port function"
46907baa4e8SJohnny Huang 	}, {
47007baa4e8SJohnny Huang 		7, 19, 1, 1, "Force disable UART1 debug port function"
47107baa4e8SJohnny Huang 	}, {
47207baa4e8SJohnny Huang 		7, 31, 1, 0, "Disable chip security setting"
47307baa4e8SJohnny Huang 	}, {
47407baa4e8SJohnny Huang 		7, 31, 1, 1, "Enable chip security setting"
47507baa4e8SJohnny Huang 	}, {
476b458cd62SJohnny Huang 		8, 0, 32, OTP_REG_VALUE, "Redundancy Repair : 0x%x"
477b458cd62SJohnny Huang 	}, {
478b458cd62SJohnny Huang 		10, 0, 32, OTP_REG_VALUE, "Manifest ID low : 0x%x"
479b458cd62SJohnny Huang 	}, {
480b458cd62SJohnny Huang 		11, 0, 32, OTP_REG_VALUE, "Manifest ID high : 0x%x"
481b458cd62SJohnny Huang 	}
482b458cd62SJohnny Huang };
4832a856b9aSJohnny Huang static void otp_read_data(uint32_t offset, uint32_t *data)
48469d5fd8fSJohnny Huang {
48569d5fd8fSJohnny Huang 	writel(offset, 0x1e6f2010); //Read address
48669d5fd8fSJohnny Huang 	writel(0x23b1e361, 0x1e6f2004); //trigger read
48769d5fd8fSJohnny Huang 	udelay(2);
48869d5fd8fSJohnny Huang 	data[0] = readl(0x1e6f2020);
48969d5fd8fSJohnny Huang 	data[1] = readl(0x1e6f2024);
49069d5fd8fSJohnny Huang }
49169d5fd8fSJohnny Huang 
4922a856b9aSJohnny Huang static void otp_read_config(uint32_t offset, uint32_t *data)
49369d5fd8fSJohnny Huang {
49469d5fd8fSJohnny Huang 	int config_offset;
49569d5fd8fSJohnny Huang 
49669d5fd8fSJohnny Huang 	config_offset = 0x800;
49769d5fd8fSJohnny Huang 	config_offset |= (offset / 8) * 0x200;
49869d5fd8fSJohnny Huang 	config_offset |= (offset % 8) * 0x2;
49969d5fd8fSJohnny Huang 
50069d5fd8fSJohnny Huang 	writel(config_offset, 0x1e6f2010);  //Read address
50169d5fd8fSJohnny Huang 	writel(0x23b1e361, 0x1e6f2004); //trigger read
50269d5fd8fSJohnny Huang 	udelay(2);
50369d5fd8fSJohnny Huang 	data[0] = readl(0x1e6f2020);
50469d5fd8fSJohnny Huang }
50569d5fd8fSJohnny Huang 
50669d5fd8fSJohnny Huang static int otp_print_config(uint32_t offset, int dw_count)
50769d5fd8fSJohnny Huang {
50869d5fd8fSJohnny Huang 	int i;
50969d5fd8fSJohnny Huang 	uint32_t ret[1];
51069d5fd8fSJohnny Huang 
51169d5fd8fSJohnny Huang 	if (offset + dw_count > 32)
5122a856b9aSJohnny Huang 		return OTP_USAGE;
51369d5fd8fSJohnny Huang 	for (i = offset; i < offset + dw_count; i ++) {
51469d5fd8fSJohnny Huang 		otp_read_config(i, ret);
515a6af4a17SJohnny Huang 		printf("OTPCFG%X: %08X\n", i, ret[0]);
51669d5fd8fSJohnny Huang 	}
51769d5fd8fSJohnny Huang 	printf("\n");
5182a856b9aSJohnny Huang 	return OTP_SUCCESS;
51969d5fd8fSJohnny Huang }
52069d5fd8fSJohnny Huang 
52169d5fd8fSJohnny Huang static int otp_print_data(uint32_t offset, int dw_count)
52269d5fd8fSJohnny Huang {
52369d5fd8fSJohnny Huang 	int i;
52469d5fd8fSJohnny Huang 	uint32_t ret[2];
52569d5fd8fSJohnny Huang 
52669d5fd8fSJohnny Huang 	if (offset + dw_count > 2048 || offset % 4 != 0)
5272a856b9aSJohnny Huang 		return OTP_USAGE;
52869d5fd8fSJohnny Huang 	for (i = offset; i < offset + dw_count; i += 2) {
52969d5fd8fSJohnny Huang 		otp_read_data(i, ret);
53069d5fd8fSJohnny Huang 		if (i % 4 == 0)
53169d5fd8fSJohnny Huang 			printf("%03X: %08X %08X ", i * 4, ret[0], ret[1]);
53269d5fd8fSJohnny Huang 		else
53369d5fd8fSJohnny Huang 			printf("%08X %08X\n", ret[0], ret[1]);
53469d5fd8fSJohnny Huang 
53569d5fd8fSJohnny Huang 	}
53669d5fd8fSJohnny Huang 	printf("\n");
5372a856b9aSJohnny Huang 	return OTP_SUCCESS;
53869d5fd8fSJohnny Huang }
53969d5fd8fSJohnny Huang 
54069d5fd8fSJohnny Huang static int otp_compare(uint32_t otp_addr, uint32_t addr)
54169d5fd8fSJohnny Huang {
54269d5fd8fSJohnny Huang 	uint32_t ret;
54369d5fd8fSJohnny Huang 	uint32_t *buf;
54469d5fd8fSJohnny Huang 
54569d5fd8fSJohnny Huang 	buf = map_physmem(addr, 16, MAP_WRBACK);
54669d5fd8fSJohnny Huang 	printf("%08X\n", buf[0]);
54769d5fd8fSJohnny Huang 	printf("%08X\n", buf[1]);
54869d5fd8fSJohnny Huang 	printf("%08X\n", buf[2]);
54969d5fd8fSJohnny Huang 	printf("%08X\n", buf[3]);
55069d5fd8fSJohnny Huang 	writel(otp_addr, 0x1e6f2010); //Compare address
55169d5fd8fSJohnny Huang 	writel(buf[0], 0x1e6f2020); //Compare data 1
55269d5fd8fSJohnny Huang 	writel(buf[1], 0x1e6f2024); //Compare data 2
55369d5fd8fSJohnny Huang 	writel(buf[2], 0x1e6f2028); //Compare data 3
55469d5fd8fSJohnny Huang 	writel(buf[3], 0x1e6f202c); //Compare data 4
55569d5fd8fSJohnny Huang 	writel(0x23b1e363, 0x1e6f2004); //Compare command
55669d5fd8fSJohnny Huang 	udelay(10);
55769d5fd8fSJohnny Huang 	ret = readl(0x1e6f2014); //Compare command
55869d5fd8fSJohnny Huang 	if (ret & 0x1)
55969d5fd8fSJohnny Huang 		return 0;
56069d5fd8fSJohnny Huang 	else
56169d5fd8fSJohnny Huang 		return -1;
56269d5fd8fSJohnny Huang }
56369d5fd8fSJohnny Huang 
56469d5fd8fSJohnny Huang static void otp_write(uint32_t otp_addr, uint32_t data)
56569d5fd8fSJohnny Huang {
56669d5fd8fSJohnny Huang 	writel(otp_addr, 0x1e6f2010); //write address
56769d5fd8fSJohnny Huang 	writel(data, 0x1e6f2020); //write data
56869d5fd8fSJohnny Huang 	writel(0x23b1e362, 0x1e6f2004); //write command
56969d5fd8fSJohnny Huang 	udelay(100);
57069d5fd8fSJohnny Huang }
57169d5fd8fSJohnny Huang 
57269d5fd8fSJohnny Huang static void otp_prog(uint32_t otp_addr, uint32_t prog_bit)
57369d5fd8fSJohnny Huang {
57469d5fd8fSJohnny Huang 	writel(otp_addr, 0x1e6f2010); //write address
57569d5fd8fSJohnny Huang 	writel(prog_bit, 0x1e6f2020); //write data
57669d5fd8fSJohnny Huang 	writel(0x23b1e364, 0x1e6f2004); //write command
57769d5fd8fSJohnny Huang 	udelay(85);
57869d5fd8fSJohnny Huang }
57969d5fd8fSJohnny Huang 
580a6d0d645SJohnny Huang static int verify_bit(uint32_t otp_addr, int bit_offset, int value)
58169d5fd8fSJohnny Huang {
58269d5fd8fSJohnny Huang 	int ret;
58369d5fd8fSJohnny Huang 
58469d5fd8fSJohnny Huang 	writel(otp_addr, 0x1e6f2010); //Read address
58569d5fd8fSJohnny Huang 	writel(0x23b1e361, 0x1e6f2004); //trigger read
58669d5fd8fSJohnny Huang 	udelay(2);
58769d5fd8fSJohnny Huang 	ret = readl(0x1e6f2020);
588a6d0d645SJohnny Huang 	// printf("verify_bit = %x\n", ret);
58969d5fd8fSJohnny Huang 	if (((ret >> bit_offset) & 1) == value)
59069d5fd8fSJohnny Huang 		return 0;
59169d5fd8fSJohnny Huang 	else
59269d5fd8fSJohnny Huang 		return -1;
59369d5fd8fSJohnny Huang }
59469d5fd8fSJohnny Huang 
595d90825e2SJohnny Huang static uint32_t verify_dw(uint32_t otp_addr, uint32_t *value, uint32_t *keep, uint32_t *compare, int size)
5964c1c9b35SJohnny Huang {
5974c1c9b35SJohnny Huang 	uint32_t ret[2];
5984c1c9b35SJohnny Huang 
5994c1c9b35SJohnny Huang 	otp_addr &= ~(1 << 15);
6004c1c9b35SJohnny Huang 
6014c1c9b35SJohnny Huang 	if (otp_addr % 2 == 0)
6024c1c9b35SJohnny Huang 		writel(otp_addr, 0x1e6f2010); //Read address
6034c1c9b35SJohnny Huang 	else
6044c1c9b35SJohnny Huang 		writel(otp_addr - 1, 0x1e6f2010); //Read address
6054c1c9b35SJohnny Huang 	writel(0x23b1e361, 0x1e6f2004); //trigger read
6064c1c9b35SJohnny Huang 	udelay(2);
6074c1c9b35SJohnny Huang 	ret[0] = readl(0x1e6f2020);
6084c1c9b35SJohnny Huang 	ret[1] = readl(0x1e6f2024);
6094c1c9b35SJohnny Huang 	if (size == 1) {
6104c1c9b35SJohnny Huang 		if (otp_addr % 2 == 0) {
6114c1c9b35SJohnny Huang 			// printf("check %x : %x = %x\n", otp_addr, ret[0], value[0]);
612d90825e2SJohnny Huang 			if ((value[0] & ~keep[0]) == (ret[0] & ~keep[0])) {
6134c1c9b35SJohnny Huang 				compare[0] = 0;
6144c1c9b35SJohnny Huang 				return 0;
6154c1c9b35SJohnny Huang 			} else {
6164c1c9b35SJohnny Huang 				compare[0] = value[0] ^ ret[0];
6174c1c9b35SJohnny Huang 				return -1;
6184c1c9b35SJohnny Huang 			}
6194c1c9b35SJohnny Huang 
6204c1c9b35SJohnny Huang 		} else {
6214c1c9b35SJohnny Huang 			// printf("check %x : %x = %x\n", otp_addr, ret[1], value[0]);
622d90825e2SJohnny Huang 			if ((value[0] & ~keep[0]) == (ret[1] & ~keep[0])) {
6234c1c9b35SJohnny Huang 				compare[0] = ~0;
6244c1c9b35SJohnny Huang 				return 0;
6254c1c9b35SJohnny Huang 			} else {
626d90825e2SJohnny Huang 				compare[0] = ~(value[0] ^ ret[1]);
6274c1c9b35SJohnny Huang 				return -1;
6284c1c9b35SJohnny Huang 			}
6294c1c9b35SJohnny Huang 		}
6304c1c9b35SJohnny Huang 	} else if (size == 2) {
6314c1c9b35SJohnny Huang 		// otp_addr should be even
632d90825e2SJohnny Huang 		if ((value[0] & ~keep[0]) == (ret[0] & ~keep[0]) && (value[1] & ~keep[1]) == (ret[1] & ~keep[1])) {
6334c1c9b35SJohnny Huang 			// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
6344c1c9b35SJohnny Huang 			// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
6354c1c9b35SJohnny Huang 			compare[0] = 0;
6364c1c9b35SJohnny Huang 			compare[1] = ~0;
6374c1c9b35SJohnny Huang 			return 0;
6384c1c9b35SJohnny Huang 		} else {
6394c1c9b35SJohnny Huang 			// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
6404c1c9b35SJohnny Huang 			// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
6414c1c9b35SJohnny Huang 			compare[0] = value[0] ^ ret[0];
6424c1c9b35SJohnny Huang 			compare[1] = ~(value[1] ^ ret[1]);
6434c1c9b35SJohnny Huang 			return -1;
6444c1c9b35SJohnny Huang 		}
6454c1c9b35SJohnny Huang 	} else {
6464c1c9b35SJohnny Huang 		return -1;
6474c1c9b35SJohnny Huang 	}
6484c1c9b35SJohnny Huang }
6494c1c9b35SJohnny Huang 
6507e22f42dSJohnny Huang static void otp_soak(int soak)
651d90825e2SJohnny Huang {
652d90825e2SJohnny Huang 	if (soak) {
653d90825e2SJohnny Huang 		otp_write(0x3000, 0x4021); // Write MRA
654d90825e2SJohnny Huang 		otp_write(0x5000, 0x1027); // Write MRB
655d90825e2SJohnny Huang 		otp_write(0x1000, 0x4820); // Write MR
656d90825e2SJohnny Huang 		writel(0x041930d4, 0x1e602008); //soak program
657d90825e2SJohnny Huang 	} else {
658d90825e2SJohnny Huang 		otp_write(0x3000, 0x4061); // Write MRA
659d90825e2SJohnny Huang 		otp_write(0x5000, 0x302f); // Write MRB
660d90825e2SJohnny Huang 		otp_write(0x1000, 0x4020); // Write MR
661d90825e2SJohnny Huang 		writel(0x04190760, 0x1e602008); //normal program
662d90825e2SJohnny Huang 	}
663d90825e2SJohnny Huang }
664d90825e2SJohnny Huang 
665d90825e2SJohnny Huang static void otp_prog_dw(uint32_t value, uint32_t keep, uint32_t prog_address)
666d90825e2SJohnny Huang {
667d90825e2SJohnny Huang 	int j, bit_value, prog_bit;
668d90825e2SJohnny Huang 
669d90825e2SJohnny Huang 	for (j = 0; j < 32; j++) {
670d90825e2SJohnny Huang 		if ((keep >> j) & 0x1)
671d90825e2SJohnny Huang 			continue;
672d90825e2SJohnny Huang 		bit_value = (value >> j) & 0x1;
673d90825e2SJohnny Huang 		if (prog_address % 2 == 0) {
674d90825e2SJohnny Huang 			if (bit_value)
675d90825e2SJohnny Huang 				prog_bit = ~(0x1 << j);
676d90825e2SJohnny Huang 			else
677d90825e2SJohnny Huang 				continue;
678d90825e2SJohnny Huang 		} else {
679d90825e2SJohnny Huang 			prog_address |= 1 << 15;
680d90825e2SJohnny Huang 			if (bit_value)
681d90825e2SJohnny Huang 				continue;
682d90825e2SJohnny Huang 			else
683d90825e2SJohnny Huang 				prog_bit = 0x1 << j;
684d90825e2SJohnny Huang 		}
685d90825e2SJohnny Huang 		otp_prog(prog_address, prog_bit);
686d90825e2SJohnny Huang 	}
687d90825e2SJohnny Huang }
688d90825e2SJohnny Huang 
68976d13988SJohnny Huang 
69076d13988SJohnny Huang static void otp_strp_status(struct otpstrap_status *otpstrap)
69176d13988SJohnny Huang {
69276d13988SJohnny Huang 	uint32_t OTPSTRAP_RAW[2];
69376d13988SJohnny Huang 	int i, j;
69476d13988SJohnny Huang 
69576d13988SJohnny Huang 	for (j = 0; j < 64; j++) {
69676d13988SJohnny Huang 		otpstrap[j].value = 0;
69776d13988SJohnny Huang 		otpstrap[j].remain_times = 7;
69876d13988SJohnny Huang 		otpstrap[j].writeable_option = -1;
69976d13988SJohnny Huang 		otpstrap[j].protected = 0;
70076d13988SJohnny Huang 	}
70176d13988SJohnny Huang 
70276d13988SJohnny Huang 	for (i = 16; i < 30; i += 2) {
70376d13988SJohnny Huang 		int option = (i - 16) / 2;
70476d13988SJohnny Huang 		otp_read_config(i, &OTPSTRAP_RAW[0]);
70576d13988SJohnny Huang 		otp_read_config(i + 1, &OTPSTRAP_RAW[1]);
70676d13988SJohnny Huang 		for (j = 0; j < 32; j++) {
70776d13988SJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[0] >> j) & 0x1);
70876d13988SJohnny Huang 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
70976d13988SJohnny Huang 				otpstrap[j].writeable_option = option;
71076d13988SJohnny Huang 			}
71176d13988SJohnny Huang 			if (bit_value == 1)
71276d13988SJohnny Huang 				otpstrap[j].remain_times --;
71376d13988SJohnny Huang 			otpstrap[j].value ^= bit_value;
71476d13988SJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
71576d13988SJohnny Huang 		}
71676d13988SJohnny Huang 		for (j = 32; j < 64; j++) {
71776d13988SJohnny Huang 			char bit_value = ((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1);
71876d13988SJohnny Huang 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
71976d13988SJohnny Huang 				otpstrap[j].writeable_option = option;
72076d13988SJohnny Huang 			}
72176d13988SJohnny Huang 			if (bit_value == 1)
72276d13988SJohnny Huang 				otpstrap[j].remain_times --;
72376d13988SJohnny Huang 			otpstrap[j].value ^= bit_value;
72476d13988SJohnny Huang 			otpstrap[j].option_array[option] = bit_value;
72576d13988SJohnny Huang 		}
72676d13988SJohnny Huang 	}
72776d13988SJohnny Huang 	otp_read_config(30, &OTPSTRAP_RAW[0]);
72876d13988SJohnny Huang 	otp_read_config(31, &OTPSTRAP_RAW[1]);
72976d13988SJohnny Huang 	for (j = 0; j < 32; j++) {
73076d13988SJohnny Huang 		if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
73176d13988SJohnny Huang 			otpstrap[j].protected = 1;
73276d13988SJohnny Huang 	}
73376d13988SJohnny Huang 	for (j = 32; j < 64; j++) {
73476d13988SJohnny Huang 		if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
73576d13988SJohnny Huang 			otpstrap[j].protected = 1;
73676d13988SJohnny Huang 	}
73776d13988SJohnny Huang }
73876d13988SJohnny Huang 
739b458cd62SJohnny Huang static int otp_print_conf_image(uint32_t *OTPCFG)
74069d5fd8fSJohnny Huang {
741442839bbSJohnny Huang 	uint32_t *OTPCFG_KEEP = &OTPCFG[12];
742b458cd62SJohnny Huang 	uint32_t mask;
743b458cd62SJohnny Huang 	uint32_t dw_offset;
744b458cd62SJohnny Huang 	uint32_t bit_offset;
745b458cd62SJohnny Huang 	uint32_t otp_value;
746b458cd62SJohnny Huang 	uint32_t otp_keep;
747b458cd62SJohnny Huang 	int fail = 0;
748b458cd62SJohnny Huang 	int valid_bit = 0;
74966f2f8e5SJohnny Huang 	int i;
75066f2f8e5SJohnny Huang 
751737ed20bSJohnny Huang 	printf("DW    BIT        Value       Description\n");
75266f2f8e5SJohnny Huang 	printf("__________________________________________________________________________\n");
753b458cd62SJohnny Huang 	for (i = 0; i < ARRAY_SIZE(a0_conf_info); i++) {
754b458cd62SJohnny Huang 		dw_offset = a0_conf_info[i].dw_offset;
755b458cd62SJohnny Huang 		bit_offset = a0_conf_info[i].bit_offset;
756b458cd62SJohnny Huang 		mask = BIT(a0_conf_info[i].length) - 1;
757b458cd62SJohnny Huang 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
758b458cd62SJohnny Huang 		otp_keep = (OTPCFG_KEEP[dw_offset] >> bit_offset) & mask;
759b458cd62SJohnny Huang 
760b458cd62SJohnny Huang 		if (otp_keep == mask) {
761b458cd62SJohnny Huang 			continue;
762b458cd62SJohnny Huang 		} else if (otp_keep != 0) {
763b458cd62SJohnny Huang 			fail = 1;
764b458cd62SJohnny Huang 		}
765b458cd62SJohnny Huang 
766b458cd62SJohnny Huang 		if ((otp_value != a0_conf_info[i].value) &&
767b458cd62SJohnny Huang 		    a0_conf_info[i].value != OTP_REG_RESERVED &&
768b458cd62SJohnny Huang 		    a0_conf_info[i].value != OTP_REG_VALUE &&
769b458cd62SJohnny Huang 		    a0_conf_info[i].value != OTP_REG_VALID_BIT)
770b458cd62SJohnny Huang 			continue;
771b458cd62SJohnny Huang 		printf("0x%-4X", dw_offset);
772b458cd62SJohnny Huang 
773b458cd62SJohnny Huang 		if (a0_conf_info[i].length == 1) {
774b458cd62SJohnny Huang 			printf("0x%-9X", a0_conf_info[i].bit_offset);
77566f2f8e5SJohnny Huang 		} else {
776b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
777b458cd62SJohnny Huang 			       a0_conf_info[i].bit_offset + a0_conf_info[i].length - 1,
778b458cd62SJohnny Huang 			       a0_conf_info[i].bit_offset);
77966f2f8e5SJohnny Huang 		}
780b458cd62SJohnny Huang 		printf("0x%-10x", otp_value);
781b458cd62SJohnny Huang 
782b458cd62SJohnny Huang 		if (fail) {
783b458cd62SJohnny Huang 			printf("Keep mask error\n");
784b458cd62SJohnny Huang 		} else {
785b458cd62SJohnny Huang 			if (a0_conf_info[i].value == OTP_REG_RESERVED) {
786b458cd62SJohnny Huang 				printf("Reserved\n");
787b458cd62SJohnny Huang 			} else if (a0_conf_info[i].value == OTP_REG_VALUE) {
788b458cd62SJohnny Huang 				printf(a0_conf_info[i].information, otp_value);
789b458cd62SJohnny Huang 				printf("\n");
790b458cd62SJohnny Huang 			} else if (a0_conf_info[i].value == OTP_REG_VALID_BIT) {
791b458cd62SJohnny Huang 				if (otp_value != 0) {
792b458cd62SJohnny Huang 					for (i = 0; i < 7; i++) {
793b458cd62SJohnny Huang 						if (otp_value == (1 << i)) {
794b458cd62SJohnny Huang 							valid_bit = i + 1;
79566f2f8e5SJohnny Huang 						}
796b458cd62SJohnny Huang 					}
797b458cd62SJohnny Huang 				} else {
798b458cd62SJohnny Huang 					valid_bit = 0;
799b458cd62SJohnny Huang 				}
800b458cd62SJohnny Huang 				printf(a0_conf_info[i].information, valid_bit);
801b458cd62SJohnny Huang 				printf("\n");
802b458cd62SJohnny Huang 			} else {
803b458cd62SJohnny Huang 				printf("%s\n", a0_conf_info[i].information);
804b458cd62SJohnny Huang 			}
805b458cd62SJohnny Huang 		}
806b458cd62SJohnny Huang 	}
807b458cd62SJohnny Huang 
808b458cd62SJohnny Huang 	if (fail)
809b458cd62SJohnny Huang 		return OTP_FAILURE;
810b458cd62SJohnny Huang 
81166f2f8e5SJohnny Huang 	return OTP_SUCCESS;
81266f2f8e5SJohnny Huang }
81366f2f8e5SJohnny Huang 
8142d4b0742SJohnny Huang static int otp_print_conf_info(int input_offset)
81566f2f8e5SJohnny Huang {
816b458cd62SJohnny Huang 	uint32_t OTPCFG[12];
817b458cd62SJohnny Huang 	uint32_t mask;
818b458cd62SJohnny Huang 	uint32_t dw_offset;
819b458cd62SJohnny Huang 	uint32_t bit_offset;
820b458cd62SJohnny Huang 	uint32_t otp_value;
821b458cd62SJohnny Huang 	int valid_bit = 0;
82266f2f8e5SJohnny Huang 	int i;
82366f2f8e5SJohnny Huang 
82466f2f8e5SJohnny Huang 	for (i = 0; i < 12; i++)
82566f2f8e5SJohnny Huang 		otp_read_config(i, &OTPCFG[i]);
82666f2f8e5SJohnny Huang 
82766f2f8e5SJohnny Huang 
828b458cd62SJohnny Huang 	printf("DW    BIT        Value       Description\n");
829b458cd62SJohnny Huang 	printf("__________________________________________________________________________\n");
830b458cd62SJohnny Huang 	for (i = 0; i < ARRAY_SIZE(a0_conf_info); i++) {
8312d4b0742SJohnny Huang 		if (input_offset != -1 && input_offset != a0_conf_info[i].dw_offset)
8322d4b0742SJohnny Huang 			continue;
833b458cd62SJohnny Huang 		dw_offset = a0_conf_info[i].dw_offset;
834b458cd62SJohnny Huang 		bit_offset = a0_conf_info[i].bit_offset;
835b458cd62SJohnny Huang 		mask = BIT(a0_conf_info[i].length) - 1;
836b458cd62SJohnny Huang 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
837b458cd62SJohnny Huang 
838b458cd62SJohnny Huang 		if ((otp_value != a0_conf_info[i].value) &&
839b458cd62SJohnny Huang 		    a0_conf_info[i].value != OTP_REG_RESERVED &&
840b458cd62SJohnny Huang 		    a0_conf_info[i].value != OTP_REG_VALUE &&
841b458cd62SJohnny Huang 		    a0_conf_info[i].value != OTP_REG_VALID_BIT)
842b458cd62SJohnny Huang 			continue;
843b458cd62SJohnny Huang 		printf("0x%-4X", dw_offset);
844b458cd62SJohnny Huang 
845b458cd62SJohnny Huang 		if (a0_conf_info[i].length == 1) {
846b458cd62SJohnny Huang 			printf("0x%-9X", a0_conf_info[i].bit_offset);
847b458cd62SJohnny Huang 		} else {
848b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
849b458cd62SJohnny Huang 			       a0_conf_info[i].bit_offset + a0_conf_info[i].length - 1,
850b458cd62SJohnny Huang 			       a0_conf_info[i].bit_offset);
851b458cd62SJohnny Huang 		}
852b458cd62SJohnny Huang 		printf("0x%-10x", otp_value);
853b458cd62SJohnny Huang 
854b458cd62SJohnny Huang 		if (a0_conf_info[i].value == OTP_REG_RESERVED) {
855b458cd62SJohnny Huang 			printf("Reserved\n");
856b458cd62SJohnny Huang 		} else if (a0_conf_info[i].value == OTP_REG_VALUE) {
857b458cd62SJohnny Huang 			printf(a0_conf_info[i].information, otp_value);
858b458cd62SJohnny Huang 			printf("\n");
859b458cd62SJohnny Huang 		} else if (a0_conf_info[i].value == OTP_REG_VALID_BIT) {
860b458cd62SJohnny Huang 			if (otp_value != 0) {
861b458cd62SJohnny Huang 				for (i = 0; i < 7; i++) {
862b458cd62SJohnny Huang 					if (otp_value == (1 << i)) {
863b458cd62SJohnny Huang 						valid_bit = i + 1;
864b458cd62SJohnny Huang 					}
865b458cd62SJohnny Huang 				}
866b458cd62SJohnny Huang 			} else {
867b458cd62SJohnny Huang 				valid_bit = 0;
868b458cd62SJohnny Huang 			}
869b458cd62SJohnny Huang 			printf(a0_conf_info[i].information, valid_bit);
870b458cd62SJohnny Huang 			printf("\n");
871b458cd62SJohnny Huang 		} else {
872b458cd62SJohnny Huang 			printf("%s\n", a0_conf_info[i].information);
873b458cd62SJohnny Huang 		}
874b458cd62SJohnny Huang 	}
875b458cd62SJohnny Huang 	return OTP_SUCCESS;
87666f2f8e5SJohnny Huang }
87766f2f8e5SJohnny Huang 
878b458cd62SJohnny Huang static int otp_print_strap_image(uint32_t *OTPSTRAP)
87976d13988SJohnny Huang {
880a8bd6d8cSJohnny Huang 	uint32_t *OTPSTRAP_PRO = &OTPSTRAP[4];
881a8bd6d8cSJohnny Huang 	uint32_t *OTPSTRAP_KEEP = &OTPSTRAP[2];
88276d13988SJohnny Huang 	int i;
883a8bd6d8cSJohnny Huang 	int fail = 0;
884a8bd6d8cSJohnny Huang 	uint32_t bit_offset;
885a8bd6d8cSJohnny Huang 	uint32_t dw_offset;
886a8bd6d8cSJohnny Huang 	uint32_t mask;
887a8bd6d8cSJohnny Huang 	uint32_t otp_value;
888a8bd6d8cSJohnny Huang 	uint32_t otp_protect;
889a8bd6d8cSJohnny Huang 	uint32_t otp_keep;
89076d13988SJohnny Huang 
891a8bd6d8cSJohnny Huang 	printf("BIT(hex)   Value       Protect     Description\n");
892a8bd6d8cSJohnny Huang 	printf("__________________________________________________________________________________________\n");
893b458cd62SJohnny Huang 
894a8bd6d8cSJohnny Huang 	for (i = 0; i < ARRAY_SIZE(a0_strap_info); i++) {
895a8bd6d8cSJohnny Huang 		if (a0_strap_info[i].bit_offset > 32) {
896a8bd6d8cSJohnny Huang 			dw_offset = 1;
897a8bd6d8cSJohnny Huang 			bit_offset = a0_strap_info[i].bit_offset - 32;
898a8bd6d8cSJohnny Huang 		} else {
899a8bd6d8cSJohnny Huang 			dw_offset = 0;
900a8bd6d8cSJohnny Huang 			bit_offset = a0_strap_info[i].bit_offset;
901a8bd6d8cSJohnny Huang 		}
90276d13988SJohnny Huang 
903a8bd6d8cSJohnny Huang 		mask = BIT(a0_strap_info[i].length) - 1;
904a8bd6d8cSJohnny Huang 		otp_value = (OTPSTRAP[dw_offset] >> bit_offset) & mask;
905a8bd6d8cSJohnny Huang 		otp_protect = (OTPSTRAP_PRO[dw_offset] >> bit_offset) & mask;
906a8bd6d8cSJohnny Huang 		otp_keep = (OTPSTRAP_KEEP[dw_offset] >> bit_offset) & mask;
907a8bd6d8cSJohnny Huang 
908a8bd6d8cSJohnny Huang 		if (otp_keep == mask) {
909a8bd6d8cSJohnny Huang 			continue;
910a8bd6d8cSJohnny Huang 		} else if (otp_keep != 0) {
911a8bd6d8cSJohnny Huang 			fail = 1;
912a8bd6d8cSJohnny Huang 		}
913a8bd6d8cSJohnny Huang 
914a8bd6d8cSJohnny Huang 		if ((otp_value != a0_strap_info[i].value) &&
915a8bd6d8cSJohnny Huang 		    a0_strap_info[i].value != OTP_REG_RESERVED)
916a8bd6d8cSJohnny Huang 			continue;
917a8bd6d8cSJohnny Huang 
918a8bd6d8cSJohnny Huang 		if (a0_strap_info[i].length == 1) {
919b458cd62SJohnny Huang 			printf("0x%-9X", a0_strap_info[i].bit_offset);
920a8bd6d8cSJohnny Huang 		} else {
921b458cd62SJohnny Huang 			printf("0x%-2X:0x%-4X",
922a8bd6d8cSJohnny Huang 			       a0_strap_info[i].bit_offset + a0_strap_info[i].length - 1,
923a8bd6d8cSJohnny Huang 			       a0_strap_info[i].bit_offset);
924a8bd6d8cSJohnny Huang 		}
925a8bd6d8cSJohnny Huang 		printf("0x%-10x", otp_value);
926a8bd6d8cSJohnny Huang 		printf("0x%-10x", otp_protect);
927a8bd6d8cSJohnny Huang 
928a8bd6d8cSJohnny Huang 		if (fail) {
929a8bd6d8cSJohnny Huang 			printf("Keep mask error\n");
930a8bd6d8cSJohnny Huang 		} else {
931a8bd6d8cSJohnny Huang 			if (a0_strap_info[i].value != OTP_REG_RESERVED)
932a8bd6d8cSJohnny Huang 				printf("%s\n", a0_strap_info[i].information);
933a8bd6d8cSJohnny Huang 			else
934a8bd6d8cSJohnny Huang 				printf("Reserved\n");
935a8bd6d8cSJohnny Huang 		}
936a8bd6d8cSJohnny Huang 	}
937a8bd6d8cSJohnny Huang 
938a8bd6d8cSJohnny Huang 	if (fail)
93976d13988SJohnny Huang 		return OTP_FAILURE;
94076d13988SJohnny Huang 
94176d13988SJohnny Huang 	return OTP_SUCCESS;
94276d13988SJohnny Huang }
94376d13988SJohnny Huang 
944b458cd62SJohnny Huang static int otp_print_strap_info(int view)
94576d13988SJohnny Huang {
94676d13988SJohnny Huang 	struct otpstrap_status strap_status[64];
94707baa4e8SJohnny Huang 	int i, j;
948b458cd62SJohnny Huang 	int fail = 0;
949b458cd62SJohnny Huang 	uint32_t bit_offset;
950b458cd62SJohnny Huang 	uint32_t length;
951b458cd62SJohnny Huang 	uint32_t otp_value;
952b458cd62SJohnny Huang 	uint32_t otp_protect;
95376d13988SJohnny Huang 
95476d13988SJohnny Huang 	otp_strp_status(strap_status);
95576d13988SJohnny Huang 
956b458cd62SJohnny Huang 	if (view) {
95707baa4e8SJohnny Huang 		// printf("BIT(hex) Value  Option         Protect   Description\n");
95807baa4e8SJohnny Huang 		// printf("                0 1 2 3 4 5 6\n");
95907baa4e8SJohnny Huang 		printf("BIT(hex) Value  Remains  Protect   Description\n");
96007baa4e8SJohnny Huang 		printf("___________________________________________________________________________________________________\n");
961b458cd62SJohnny Huang 	} else {
962b458cd62SJohnny Huang 		printf("BIT(hex)   Value       Description\n");
963b458cd62SJohnny Huang 		printf("________________________________________________________________________________\n");
96476d13988SJohnny Huang 	}
965b458cd62SJohnny Huang 	for (i = 0; i < ARRAY_SIZE(a0_strap_info); i++) {
966b458cd62SJohnny Huang 		otp_value = 0;
967b458cd62SJohnny Huang 		bit_offset = a0_strap_info[i].bit_offset;
968b458cd62SJohnny Huang 		length = a0_strap_info[i].length;
969b458cd62SJohnny Huang 		for (j = 0; j < length; j++) {
970*c947ef08SJohnny Huang 			otp_value |= strap_status[bit_offset + j].value << j;
971*c947ef08SJohnny Huang 			otp_protect |= strap_status[bit_offset + j].protected << j;
972b458cd62SJohnny Huang 		}
973b458cd62SJohnny Huang 		if ((otp_value != a0_strap_info[i].value) &&
974b458cd62SJohnny Huang 		    a0_strap_info[i].value != OTP_REG_RESERVED)
975b458cd62SJohnny Huang 			continue;
976b458cd62SJohnny Huang 		if (view) {
977b458cd62SJohnny Huang 			for (j = 0; j < length; j++) {
978b458cd62SJohnny Huang 				printf("0x%-7X", a0_strap_info[i].bit_offset + j);
979b458cd62SJohnny Huang 				printf("0x%-5X", strap_status[bit_offset + j].value);
98007baa4e8SJohnny Huang 				printf("%-9d", strap_status[bit_offset + j].remain_times);
981b458cd62SJohnny Huang 				printf("0x%-7X", strap_status[bit_offset].protected);
982b458cd62SJohnny Huang 				if (a0_strap_info[i].value == OTP_REG_RESERVED) {
983b458cd62SJohnny Huang 					printf(" Reserved\n");
984b458cd62SJohnny Huang 					continue;
985b458cd62SJohnny Huang 				}
986b458cd62SJohnny Huang 				if (length == 1) {
987b458cd62SJohnny Huang 					printf(" %s\n", a0_strap_info[i].information);
988b458cd62SJohnny Huang 					continue;
98976d13988SJohnny Huang 				}
99076d13988SJohnny Huang 
991b458cd62SJohnny Huang 				if (j == 0)
992b458cd62SJohnny Huang 					printf("/%s\n", a0_strap_info[i].information);
993b458cd62SJohnny Huang 				else if (j == length - 1)
994b458cd62SJohnny Huang 					printf("\\ \"\n");
995b458cd62SJohnny Huang 				else
996b458cd62SJohnny Huang 					printf("| \"\n");
99776d13988SJohnny Huang 			}
998b458cd62SJohnny Huang 		} else {
999*c947ef08SJohnny Huang 			if (length == 1) {
1000b458cd62SJohnny Huang 				printf("0x%-9X", a0_strap_info[i].bit_offset);
1001b458cd62SJohnny Huang 			} else {
1002b458cd62SJohnny Huang 				printf("0x%-2X:0x%-4X",
1003b458cd62SJohnny Huang 				       bit_offset + length - 1, bit_offset);
1004b458cd62SJohnny Huang 			}
1005b458cd62SJohnny Huang 
1006b458cd62SJohnny Huang 			printf("0x%-10X", otp_value);
1007b458cd62SJohnny Huang 
1008b458cd62SJohnny Huang 			if (a0_strap_info[i].value != OTP_REG_RESERVED)
1009b458cd62SJohnny Huang 				printf("%s\n", a0_strap_info[i].information);
1010b458cd62SJohnny Huang 			else
1011b458cd62SJohnny Huang 				printf("Reserved\n");
1012b458cd62SJohnny Huang 		}
1013b458cd62SJohnny Huang 	}
1014b458cd62SJohnny Huang 
1015b458cd62SJohnny Huang 	if (fail)
1016b458cd62SJohnny Huang 		return OTP_FAILURE;
1017b458cd62SJohnny Huang 
1018b458cd62SJohnny Huang 	return OTP_SUCCESS;
1019b458cd62SJohnny Huang }
1020b458cd62SJohnny Huang 
102169d5fd8fSJohnny Huang static void buf_print(char *buf, int len)
102269d5fd8fSJohnny Huang {
102369d5fd8fSJohnny Huang 	int i;
102469d5fd8fSJohnny Huang 	printf("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
102569d5fd8fSJohnny Huang 	for (i = 0; i < len; i++) {
102669d5fd8fSJohnny Huang 		if (i % 16 == 0) {
102769d5fd8fSJohnny Huang 			printf("%04X: ", i);
102869d5fd8fSJohnny Huang 		}
102969d5fd8fSJohnny Huang 		printf("%02X ", buf[i]);
103069d5fd8fSJohnny Huang 		if ((i + 1) % 16 == 0) {
103169d5fd8fSJohnny Huang 			printf("\n");
103269d5fd8fSJohnny Huang 		}
103369d5fd8fSJohnny Huang 	}
103469d5fd8fSJohnny Huang }
103569d5fd8fSJohnny Huang 
10367f795e57SJohnny Huang static int otp_print_data_info(uint32_t *buf)
103769d5fd8fSJohnny Huang {
103869d5fd8fSJohnny Huang 	int key_id, key_offset, last, key_type, key_length, exp_length;
103969d5fd8fSJohnny Huang 	char *byte_buf;
104069d5fd8fSJohnny Huang 	int i = 0, len = 0;
104169d5fd8fSJohnny Huang 	byte_buf = (char *)buf;
104269d5fd8fSJohnny Huang 	while (1) {
104369d5fd8fSJohnny Huang 		key_id = buf[i] & 0x7;
104469d5fd8fSJohnny Huang 		key_offset = buf[i] & 0x1ff8;
104569d5fd8fSJohnny Huang 		last = (buf[i] >> 13) & 1;
104669d5fd8fSJohnny Huang 		key_type = (buf[i] >> 14) & 0xf;
104769d5fd8fSJohnny Huang 		key_length = (buf[i] >> 18) & 0x3;
104869d5fd8fSJohnny Huang 		exp_length = (buf[i] >> 20) & 0xfff;
10497f795e57SJohnny Huang 		printf("\nKey[%d]:\n", i);
105069d5fd8fSJohnny Huang 		printf("Key Type: ");
105169d5fd8fSJohnny Huang 		switch (key_type) {
105269d5fd8fSJohnny Huang 		case 0:
105369d5fd8fSJohnny Huang 			printf("AES-256 as OEM platform key for image encryption/decryption\n");
105469d5fd8fSJohnny Huang 			break;
105569d5fd8fSJohnny Huang 		case 1:
105669d5fd8fSJohnny Huang 			printf("AES-256 as secret vault key\n");
105769d5fd8fSJohnny Huang 			break;
105869d5fd8fSJohnny Huang 		case 4:
105969d5fd8fSJohnny Huang 			printf("HMAC as encrypted OEM HMAC keys in Mode 1\n");
106069d5fd8fSJohnny Huang 			break;
106169d5fd8fSJohnny Huang 		case 8:
106269d5fd8fSJohnny Huang 			printf("RSA-public as OEM DSS public keys in Mode 2\n");
106369d5fd8fSJohnny Huang 			break;
106469d5fd8fSJohnny Huang 		case 9:
106569d5fd8fSJohnny Huang 			printf("RSA-public as SOC public key\n");
106669d5fd8fSJohnny Huang 			break;
106769d5fd8fSJohnny Huang 		case 10:
106869d5fd8fSJohnny Huang 			printf("RSA-public as AES key decryption key\n");
106969d5fd8fSJohnny Huang 			break;
107069d5fd8fSJohnny Huang 		case 13:
107169d5fd8fSJohnny Huang 			printf("RSA-private as SOC private key\n");
107269d5fd8fSJohnny Huang 			break;
107369d5fd8fSJohnny Huang 		case 14:
107469d5fd8fSJohnny Huang 			printf("RSA-private as AES key decryption key\n");
107569d5fd8fSJohnny Huang 			break;
107669d5fd8fSJohnny Huang 		default:
107769d5fd8fSJohnny Huang 			printf("key_type error: %x\n", key_type);
107869d5fd8fSJohnny Huang 			return -1;
107969d5fd8fSJohnny Huang 		}
108069d5fd8fSJohnny Huang 		if (key_type == 4) {
108169d5fd8fSJohnny Huang 			printf("HMAC SHA Type: ");
108269d5fd8fSJohnny Huang 			switch (key_length) {
108369d5fd8fSJohnny Huang 			case 0:
108469d5fd8fSJohnny Huang 				printf("HMAC(SHA224)\n");
108569d5fd8fSJohnny Huang 				break;
108669d5fd8fSJohnny Huang 			case 1:
108769d5fd8fSJohnny Huang 				printf("HMAC(SHA256)\n");
108869d5fd8fSJohnny Huang 				break;
108969d5fd8fSJohnny Huang 			case 2:
109069d5fd8fSJohnny Huang 				printf("HMAC(SHA384)\n");
109169d5fd8fSJohnny Huang 				break;
109269d5fd8fSJohnny Huang 			case 3:
109369d5fd8fSJohnny Huang 				printf("HMAC(SHA512)\n");
109469d5fd8fSJohnny Huang 				break;
109569d5fd8fSJohnny Huang 			}
1096cd1610b4SJohnny Huang 		} else if (key_type != 0 && key_type != 1) {
109769d5fd8fSJohnny Huang 			printf("RSA SHA Type: ");
109869d5fd8fSJohnny Huang 			switch (key_length) {
109969d5fd8fSJohnny Huang 			case 0:
110069d5fd8fSJohnny Huang 				printf("RSA1024\n");
110169d5fd8fSJohnny Huang 				len = 0x100;
110269d5fd8fSJohnny Huang 				break;
110369d5fd8fSJohnny Huang 			case 1:
110469d5fd8fSJohnny Huang 				printf("RSA2048\n");
110569d5fd8fSJohnny Huang 				len = 0x200;
110669d5fd8fSJohnny Huang 				break;
110769d5fd8fSJohnny Huang 			case 2:
110869d5fd8fSJohnny Huang 				printf("RSA3072\n");
110969d5fd8fSJohnny Huang 				len = 0x300;
111069d5fd8fSJohnny Huang 				break;
111169d5fd8fSJohnny Huang 			case 3:
111269d5fd8fSJohnny Huang 				printf("RSA4096\n");
111369d5fd8fSJohnny Huang 				len = 0x400;
111469d5fd8fSJohnny Huang 				break;
111569d5fd8fSJohnny Huang 			}
111669d5fd8fSJohnny Huang 			printf("RSA exponent bit length: %d\n", exp_length);
111769d5fd8fSJohnny Huang 		}
111869d5fd8fSJohnny Huang 		if (key_type == 4 || key_type == 8)
111969d5fd8fSJohnny Huang 			printf("Key Number ID: %d\n", key_id);
112069d5fd8fSJohnny Huang 		printf("Key Value:\n");
112169d5fd8fSJohnny Huang 		if (key_type == 4) {
112269d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], 0x40);
112369d5fd8fSJohnny Huang 		} else if (key_type == 0 || key_type == 1) {
112469d5fd8fSJohnny Huang 			printf("AES Key:\n");
112569d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], 0x20);
112669d5fd8fSJohnny Huang 			printf("AES IV:\n");
112769d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset + 0x20], 0x10);
112869d5fd8fSJohnny Huang 
112969d5fd8fSJohnny Huang 		} else {
113069d5fd8fSJohnny Huang 			printf("RSA mod:\n");
113169d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset], len / 2);
113269d5fd8fSJohnny Huang 			printf("RSA exp:\n");
113369d5fd8fSJohnny Huang 			buf_print(&byte_buf[key_offset + (len / 2)], len / 2);
113469d5fd8fSJohnny Huang 		}
113569d5fd8fSJohnny Huang 		if (last)
113669d5fd8fSJohnny Huang 			break;
113769d5fd8fSJohnny Huang 		i++;
113869d5fd8fSJohnny Huang 	}
113969d5fd8fSJohnny Huang 	return 0;
114069d5fd8fSJohnny Huang }
114169d5fd8fSJohnny Huang 
1142a6d0d645SJohnny Huang static int otp_prog_conf(uint32_t *buf)
114369d5fd8fSJohnny Huang {
1144a6d0d645SJohnny Huang 	int i, k;
1145d90825e2SJohnny Huang 	int pass = 0;
1146d90825e2SJohnny Huang 	int soak = 0;
1147a6d0d645SJohnny Huang 	uint32_t prog_address;
1148a6d0d645SJohnny Huang 	uint32_t data[12];
1149a6d0d645SJohnny Huang 	uint32_t compare[2];
1150d90825e2SJohnny Huang 	uint32_t *buf_keep = &buf[12];
1151d90825e2SJohnny Huang 	uint32_t data_masked;
1152d90825e2SJohnny Huang 	uint32_t buf_masked;
115369d5fd8fSJohnny Huang 
1154a6d0d645SJohnny Huang 	printf("Read OTP Config Region:\n");
1155a6d0d645SJohnny Huang 
1156a6d0d645SJohnny Huang 	printProgress(0, 12, "");
1157a6d0d645SJohnny Huang 	for (i = 0; i < 12 ; i ++) {
1158a6d0d645SJohnny Huang 		printProgress(i + 1, 12, "");
115969d5fd8fSJohnny Huang 		prog_address = 0x800;
1160a6d0d645SJohnny Huang 		prog_address |= (i / 8) * 0x200;
1161a6d0d645SJohnny Huang 		prog_address |= (i % 8) * 0x2;
1162a6d0d645SJohnny Huang 		otp_read_data(prog_address, &data[i]);
1163a6d0d645SJohnny Huang 	}
1164a6d0d645SJohnny Huang 
1165a6d0d645SJohnny Huang 	printf("Check writable...\n");
1166a6d0d645SJohnny Huang 	for (i = 0; i < 12; i++) {
1167d90825e2SJohnny Huang 		data_masked = data[i]  & ~buf_keep[i];
1168d90825e2SJohnny Huang 		buf_masked  = buf[i] & ~buf_keep[i];
1169d90825e2SJohnny Huang 		if (data_masked == buf_masked)
117069d5fd8fSJohnny Huang 			continue;
1171d90825e2SJohnny Huang 		if ((data_masked | buf_masked) == buf_masked) {
1172a6d0d645SJohnny Huang 			continue;
1173a6d0d645SJohnny Huang 		} else {
1174a6d0d645SJohnny Huang 			printf("Input image can't program into OTP, please check.\n");
1175a6af4a17SJohnny Huang 			printf("OTPCFG[%X] = %x\n", i, data[i]);
1176a6af4a17SJohnny Huang 			printf("Input [%X] = %x\n", i, buf[i]);
1177a6af4a17SJohnny Huang 			printf("Mask  [%X] = %x\n", i, ~buf_keep[i]);
11782a856b9aSJohnny Huang 			return OTP_FAILURE;
1179a6d0d645SJohnny Huang 		}
1180a6d0d645SJohnny Huang 	}
1181a6d0d645SJohnny Huang 
1182a6d0d645SJohnny Huang 	printf("Start Programing...\n");
1183a6d0d645SJohnny Huang 	printProgress(0, 12, "");
1184d90825e2SJohnny Huang 	otp_soak(0);
1185a6d0d645SJohnny Huang 	for (i = 0; i < 12; i++) {
1186d90825e2SJohnny Huang 		data_masked = data[i]  & ~buf_keep[i];
1187d90825e2SJohnny Huang 		buf_masked  = buf[i] & ~buf_keep[i];
1188a6d0d645SJohnny Huang 		prog_address = 0x800;
1189a6d0d645SJohnny Huang 		prog_address |= (i / 8) * 0x200;
1190a6d0d645SJohnny Huang 		prog_address |= (i % 8) * 0x2;
1191d90825e2SJohnny Huang 		if (data_masked == buf_masked) {
1192a6d0d645SJohnny Huang 			printProgress(i + 1, 12, "[%03X]=%08X HIT", prog_address, buf[i]);
1193a6d0d645SJohnny Huang 			continue;
1194a6d0d645SJohnny Huang 		}
1195d90825e2SJohnny Huang 		if (soak) {
1196d90825e2SJohnny Huang 			soak = 0;
1197d90825e2SJohnny Huang 			otp_soak(0);
1198d90825e2SJohnny Huang 		}
1199a6d0d645SJohnny Huang 		printProgress(i + 1, 12, "[%03X]=%08X    ", prog_address, buf[i]);
1200a6d0d645SJohnny Huang 
1201d90825e2SJohnny Huang 		otp_prog_dw(buf[i], buf_keep[i], prog_address);
1202a6d0d645SJohnny Huang 
120369d5fd8fSJohnny Huang 		pass = 0;
120469d5fd8fSJohnny Huang 		for (k = 0; k < RETRY; k++) {
1205d90825e2SJohnny Huang 			if (verify_dw(prog_address, &buf[i], &buf_keep[i], compare, 1) != 0) {
1206d90825e2SJohnny Huang 				if (soak == 0) {
1207d90825e2SJohnny Huang 					soak = 1;
1208d90825e2SJohnny Huang 					otp_soak(1);
1209d90825e2SJohnny Huang 				}
1210a6d0d645SJohnny Huang 				otp_prog_dw(compare[0], prog_address, 1);
1211a6d0d645SJohnny Huang 			} else {
121269d5fd8fSJohnny Huang 				pass = 1;
121369d5fd8fSJohnny Huang 				break;
121469d5fd8fSJohnny Huang 			}
121569d5fd8fSJohnny Huang 		}
1216a6d0d645SJohnny Huang 	}
1217a6d0d645SJohnny Huang 
121869d5fd8fSJohnny Huang 	if (!pass)
12192a856b9aSJohnny Huang 		return OTP_FAILURE;
1220a6d0d645SJohnny Huang 
12212a856b9aSJohnny Huang 	return OTP_SUCCESS;
1222d90825e2SJohnny Huang 
122369d5fd8fSJohnny Huang }
122469d5fd8fSJohnny Huang 
122569d5fd8fSJohnny Huang 
122676d13988SJohnny Huang static int otp_strap_image_confirm(uint32_t *buf)
122769d5fd8fSJohnny Huang {
122869d5fd8fSJohnny Huang 	int i;
122969d5fd8fSJohnny Huang 	uint32_t *strap_keep = buf + 2;
123069d5fd8fSJohnny Huang 	uint32_t *strap_protect = buf + 4;
123169d5fd8fSJohnny Huang 	int bit, pbit, kbit;
123269d5fd8fSJohnny Huang 	int fail = 0;
1233a6af4a17SJohnny Huang 	int skip = -1;
123466f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
123569d5fd8fSJohnny Huang 
123669d5fd8fSJohnny Huang 	otp_strp_status(otpstrap);
123769d5fd8fSJohnny Huang 	for (i = 0; i < 64; i++) {
123869d5fd8fSJohnny Huang 		if (i < 32) {
123969d5fd8fSJohnny Huang 			bit = (buf[0] >> i) & 0x1;
124069d5fd8fSJohnny Huang 			kbit = (strap_keep[0] >> i) & 0x1;
124169d5fd8fSJohnny Huang 			pbit = (strap_protect[0] >> i) & 0x1;
124269d5fd8fSJohnny Huang 		} else {
124369d5fd8fSJohnny Huang 			bit = (buf[1] >> (i - 32)) & 0x1;
124469d5fd8fSJohnny Huang 			kbit = (strap_keep[1] >> (i - 32)) & 0x1;
124569d5fd8fSJohnny Huang 			pbit = (strap_protect[1] >> (i - 32)) & 0x1;
124669d5fd8fSJohnny Huang 		}
124769d5fd8fSJohnny Huang 
124869d5fd8fSJohnny Huang 		if (kbit == 1) {
124969d5fd8fSJohnny Huang 			continue;
125069d5fd8fSJohnny Huang 		} else {
1251a6af4a17SJohnny Huang 			printf("OTPSTRAP[%X]:\n", i);
125269d5fd8fSJohnny Huang 		}
125369d5fd8fSJohnny Huang 		if (bit == otpstrap[i].value) {
125469d5fd8fSJohnny Huang 			printf("    The value is same as before, skip it.\n");
1255a6af4a17SJohnny Huang 			if (skip == -1)
1256a6af4a17SJohnny Huang 				skip = 1;
125769d5fd8fSJohnny Huang 			continue;
1258a6af4a17SJohnny Huang 		} else {
1259a6af4a17SJohnny Huang 			skip = 0;
126069d5fd8fSJohnny Huang 		}
126169d5fd8fSJohnny Huang 		if (otpstrap[i].protected == 1) {
126269d5fd8fSJohnny Huang 			printf("    This bit is protected and is not writable\n");
126369d5fd8fSJohnny Huang 			fail = 1;
126469d5fd8fSJohnny Huang 			continue;
126569d5fd8fSJohnny Huang 		}
126669d5fd8fSJohnny Huang 		if (otpstrap[i].remain_times == 0) {
1267a6af4a17SJohnny Huang 			printf("    This bit is no remaining times to write.\n");
126869d5fd8fSJohnny Huang 			fail = 1;
126969d5fd8fSJohnny Huang 			continue;
127069d5fd8fSJohnny Huang 		}
127169d5fd8fSJohnny Huang 		if (pbit == 1) {
127269d5fd8fSJohnny Huang 			printf("    This bit will be protected and become non-writable.\n");
127369d5fd8fSJohnny Huang 		}
1274a6af4a17SJohnny 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);
127569d5fd8fSJohnny Huang 	}
127669d5fd8fSJohnny Huang 	if (fail == 1)
1277a6af4a17SJohnny Huang 		return OTP_FAILURE;
1278a6af4a17SJohnny Huang 	else if (skip == 1)
1279a6af4a17SJohnny Huang 		return OTP_PROG_SKIP;
12807e22f42dSJohnny Huang 
12817e22f42dSJohnny Huang 	return 0;
128269d5fd8fSJohnny Huang }
128369d5fd8fSJohnny Huang 
12842a856b9aSJohnny Huang static int otp_print_strap(int start, int count)
128569d5fd8fSJohnny Huang {
128669d5fd8fSJohnny Huang 	int i, j;
128766f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
128869d5fd8fSJohnny Huang 
12892a856b9aSJohnny Huang 	if (start < 0 || start > 64)
12902a856b9aSJohnny Huang 		return OTP_USAGE;
12912a856b9aSJohnny Huang 
12922a856b9aSJohnny Huang 	if ((start + count) < 0 || (start + count) > 64)
12932a856b9aSJohnny Huang 		return OTP_USAGE;
12942a856b9aSJohnny Huang 
129569d5fd8fSJohnny Huang 	otp_strp_status(otpstrap);
129669d5fd8fSJohnny Huang 
129707baa4e8SJohnny Huang 	printf("BIT(hex)  Value  Option           Status\n");
1298a8bd6d8cSJohnny Huang 	printf("___________________________________________________________________________\n");
1299737ed20bSJohnny Huang 
1300cd1610b4SJohnny Huang 	for (i = start; i < start + count; i++) {
130107baa4e8SJohnny Huang 		printf("0x%-8X", i);
1302737ed20bSJohnny Huang 		printf("%-7d", otpstrap[i].value);
1303737ed20bSJohnny Huang 		for (j = 0; j < 7; j++)
1304737ed20bSJohnny Huang 			printf("%d ", otpstrap[i].option_array[j]);
1305737ed20bSJohnny Huang 		printf("   ");
130669d5fd8fSJohnny Huang 		if (otpstrap[i].protected == 1) {
1307737ed20bSJohnny Huang 			printf("protected and not writable");
130869d5fd8fSJohnny Huang 		} else {
1309737ed20bSJohnny Huang 			printf("not protected ");
131069d5fd8fSJohnny Huang 			if (otpstrap[i].remain_times == 0) {
1311737ed20bSJohnny Huang 				printf("and no remaining times to write.");
131269d5fd8fSJohnny Huang 			} else {
1313737ed20bSJohnny Huang 				printf("and still can write %d times", otpstrap[i].remain_times);
131469d5fd8fSJohnny Huang 			}
131569d5fd8fSJohnny Huang 		}
1316737ed20bSJohnny Huang 		printf("\n");
131769d5fd8fSJohnny Huang 	}
13182a856b9aSJohnny Huang 
13192a856b9aSJohnny Huang 	return OTP_SUCCESS;
132069d5fd8fSJohnny Huang }
132169d5fd8fSJohnny Huang 
132269d5fd8fSJohnny Huang static int otp_prog_strap(uint32_t *buf)
132369d5fd8fSJohnny Huang {
132469d5fd8fSJohnny Huang 	int i, j;
132569d5fd8fSJohnny Huang 	uint32_t *strap_keep = buf + 2;
132669d5fd8fSJohnny Huang 	uint32_t *strap_protect = buf + 4;
132769d5fd8fSJohnny Huang 	uint32_t prog_bit, prog_address;
132869d5fd8fSJohnny Huang 	int bit, pbit, kbit, offset;
132969d5fd8fSJohnny Huang 	int fail = 0;
13307e22f42dSJohnny Huang 	int pass = 0;
13317e22f42dSJohnny Huang 	int soak = 0;
133266f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
133369d5fd8fSJohnny Huang 
13347f795e57SJohnny Huang 	printf("Read OTP Strap Region:\n");
133569d5fd8fSJohnny Huang 	otp_strp_status(otpstrap);
133669d5fd8fSJohnny Huang 
13377f795e57SJohnny Huang 	printf("Check writable...\n");
13387f795e57SJohnny Huang 	if (otp_strap_image_confirm(buf) == OTP_FAILURE) {
13397f795e57SJohnny Huang 		printf("Input image can't program into OTP, please check.\n");
13407f795e57SJohnny Huang 		return OTP_FAILURE;
13417f795e57SJohnny Huang 	}
13427e22f42dSJohnny Huang 
13437f795e57SJohnny Huang 	otp_soak(0);
134469d5fd8fSJohnny Huang 	for (i = 0; i < 64; i++) {
13454c1c9b35SJohnny Huang 		printProgress(i + 1, 64, "");
134669d5fd8fSJohnny Huang 		prog_address = 0x800;
134769d5fd8fSJohnny Huang 		if (i < 32) {
134869d5fd8fSJohnny Huang 			offset = i;
134969d5fd8fSJohnny Huang 			bit = (buf[0] >> offset) & 0x1;
135069d5fd8fSJohnny Huang 			kbit = (strap_keep[0] >> offset) & 0x1;
135169d5fd8fSJohnny Huang 			pbit = (strap_protect[0] >> offset) & 0x1;
135269d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200;
135369d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2;
135469d5fd8fSJohnny Huang 
135569d5fd8fSJohnny Huang 		} else {
135669d5fd8fSJohnny Huang 			offset = (i - 32);
135769d5fd8fSJohnny Huang 			bit = (buf[1] >> offset) & 0x1;
135869d5fd8fSJohnny Huang 			kbit = (strap_keep[1] >> offset) & 0x1;
135969d5fd8fSJohnny Huang 			pbit = (strap_protect[1] >> offset) & 0x1;
136069d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200;
136169d5fd8fSJohnny Huang 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2;
136269d5fd8fSJohnny Huang 		}
136369d5fd8fSJohnny Huang 		prog_bit = ~(0x1 << offset);
136469d5fd8fSJohnny Huang 
136569d5fd8fSJohnny Huang 		if (kbit == 1) {
136669d5fd8fSJohnny Huang 			continue;
136769d5fd8fSJohnny Huang 		}
136869d5fd8fSJohnny Huang 		if (bit == otpstrap[i].value) {
136969d5fd8fSJohnny Huang 			continue;
137069d5fd8fSJohnny Huang 		}
137169d5fd8fSJohnny Huang 		if (otpstrap[i].protected == 1) {
137269d5fd8fSJohnny Huang 			fail = 1;
137369d5fd8fSJohnny Huang 			continue;
137469d5fd8fSJohnny Huang 		}
137569d5fd8fSJohnny Huang 		if (otpstrap[i].remain_times == 0) {
137669d5fd8fSJohnny Huang 			fail = 1;
137769d5fd8fSJohnny Huang 			continue;
137869d5fd8fSJohnny Huang 		}
13797e22f42dSJohnny Huang 
13807e22f42dSJohnny Huang 		if (soak) {
138169d5fd8fSJohnny Huang 			soak = 0;
13827e22f42dSJohnny Huang 			otp_soak(0);
13837e22f42dSJohnny Huang 		}
13847e22f42dSJohnny Huang 
13857e22f42dSJohnny Huang 		otp_prog(prog_address, prog_bit);
13867e22f42dSJohnny Huang 
13877e22f42dSJohnny Huang 		pass = 0;
13887e22f42dSJohnny Huang 
138969d5fd8fSJohnny Huang 		for (j = 0; j < RETRY; j++) {
1390a6d0d645SJohnny Huang 			if (verify_bit(prog_address, offset, 1) == 0) {
139169d5fd8fSJohnny Huang 				pass = 1;
139269d5fd8fSJohnny Huang 				break;
139369d5fd8fSJohnny Huang 			}
13947e22f42dSJohnny Huang 			if (soak == 0) {
139569d5fd8fSJohnny Huang 				soak = 1;
13967e22f42dSJohnny Huang 				otp_soak(1);
13974b65a65dSJohnny Huang 			}
139869d5fd8fSJohnny Huang 			otp_prog(prog_address, prog_bit);
139969d5fd8fSJohnny Huang 		}
140069d5fd8fSJohnny Huang 		if (!pass)
14012a856b9aSJohnny Huang 			return OTP_FAILURE;
140269d5fd8fSJohnny Huang 
140369d5fd8fSJohnny Huang 		if (pbit == 0)
140469d5fd8fSJohnny Huang 			continue;
140569d5fd8fSJohnny Huang 		prog_address = 0x800;
140669d5fd8fSJohnny Huang 		if (i < 32)
140769d5fd8fSJohnny Huang 			prog_address |= 0x60c;
140869d5fd8fSJohnny Huang 		else
140969d5fd8fSJohnny Huang 			prog_address |= 0x60e;
141069d5fd8fSJohnny Huang 
14117e22f42dSJohnny Huang 
14127e22f42dSJohnny Huang 		if (soak) {
14137e22f42dSJohnny Huang 			soak = 0;
14147e22f42dSJohnny Huang 			otp_soak(0);
14157e22f42dSJohnny Huang 		}
14167e22f42dSJohnny Huang 
14177e22f42dSJohnny Huang 		otp_prog(prog_address, prog_bit);
14187e22f42dSJohnny Huang 
14197e22f42dSJohnny Huang 		pass = 0;
14207e22f42dSJohnny Huang 
142169d5fd8fSJohnny Huang 		for (j = 0; j < RETRY; j++) {
14227e22f42dSJohnny Huang 
1423a6d0d645SJohnny Huang 			if (verify_bit(prog_address, offset, 1) == 0) {
142469d5fd8fSJohnny Huang 				pass = 1;
142569d5fd8fSJohnny Huang 				break;
142669d5fd8fSJohnny Huang 			}
14277e22f42dSJohnny Huang 			if (soak == 0) {
142869d5fd8fSJohnny Huang 				soak = 1;
14297e22f42dSJohnny Huang 				otp_soak(1);
143069d5fd8fSJohnny Huang 			}
143169d5fd8fSJohnny Huang 			otp_prog(prog_address, prog_bit);
143269d5fd8fSJohnny Huang 		}
143369d5fd8fSJohnny Huang 		if (!pass)
14342a856b9aSJohnny Huang 			return OTP_FAILURE;
143569d5fd8fSJohnny Huang 
143669d5fd8fSJohnny Huang 	}
143769d5fd8fSJohnny Huang 	if (fail == 1)
14382a856b9aSJohnny Huang 		return OTP_FAILURE;
143969d5fd8fSJohnny Huang 	else
14402a856b9aSJohnny Huang 		return OTP_SUCCESS;
144169d5fd8fSJohnny Huang 
144269d5fd8fSJohnny Huang }
144369d5fd8fSJohnny Huang 
1444cd1610b4SJohnny Huang static void otp_prog_bit(uint32_t value, uint32_t prog_address, uint32_t bit_offset, int soak)
1445cd1610b4SJohnny Huang {
1446cd1610b4SJohnny Huang 	int prog_bit;
1447cd1610b4SJohnny Huang 
14487e22f42dSJohnny Huang 	otp_soak(soak);
14497e22f42dSJohnny Huang 
1450cd1610b4SJohnny Huang 	if (prog_address % 2 == 0) {
1451cd1610b4SJohnny Huang 		if (value)
1452cd1610b4SJohnny Huang 			prog_bit = ~(0x1 << bit_offset);
1453cd1610b4SJohnny Huang 		else
1454cd1610b4SJohnny Huang 			return;
1455cd1610b4SJohnny Huang 	} else {
1456cd1610b4SJohnny Huang 		prog_address |= 1 << 15;
1457cd1610b4SJohnny Huang 		if (!value)
1458cd1610b4SJohnny Huang 			prog_bit = 0x1 << bit_offset;
1459cd1610b4SJohnny Huang 		else
1460cd1610b4SJohnny Huang 			return;
1461cd1610b4SJohnny Huang 	}
1462cd1610b4SJohnny Huang 	otp_prog(prog_address, prog_bit);
1463cd1610b4SJohnny Huang }
1464cd1610b4SJohnny Huang 
1465d90825e2SJohnny Huang static int otp_prog_data(uint32_t *buf)
14664c1c9b35SJohnny Huang {
14674c1c9b35SJohnny Huang 	int i, k;
14684c1c9b35SJohnny Huang 	int pass;
1469d90825e2SJohnny Huang 	int soak = 0;
14704c1c9b35SJohnny Huang 	uint32_t prog_address;
1471d90825e2SJohnny Huang 	uint32_t data[2048];
14724c1c9b35SJohnny Huang 	uint32_t compare[2];
1473d90825e2SJohnny Huang 	uint32_t *buf_keep = &buf[2048];
14744c1c9b35SJohnny Huang 
1475d90825e2SJohnny Huang 	uint32_t data0_masked;
1476d90825e2SJohnny Huang 	uint32_t data1_masked;
1477d90825e2SJohnny Huang 	uint32_t buf0_masked;
1478d90825e2SJohnny Huang 	uint32_t buf1_masked;
14794c1c9b35SJohnny Huang 
14804c1c9b35SJohnny Huang 	printf("Read OTP Data:\n");
14814c1c9b35SJohnny Huang 
1482d90825e2SJohnny Huang 	printProgress(0, 2048, "");
1483d90825e2SJohnny Huang 	for (i = 0; i < 2048 ; i += 2) {
1484d90825e2SJohnny Huang 		printProgress(i + 2, 2048, "");
1485d90825e2SJohnny Huang 		otp_read_data(i, &data[i]);
14864c1c9b35SJohnny Huang 	}
1487d90825e2SJohnny Huang 
14884c1c9b35SJohnny Huang 
14894c1c9b35SJohnny Huang 	printf("Check writable...\n");
1490d90825e2SJohnny Huang 	for (i = 0; i < 2048; i++) {
1491d90825e2SJohnny Huang 		data0_masked = data[i]  & ~buf_keep[i];
1492d90825e2SJohnny Huang 		buf0_masked  = buf[i] & ~buf_keep[i];
1493d90825e2SJohnny Huang 		if (data0_masked == buf0_masked)
14944c1c9b35SJohnny Huang 			continue;
1495d90825e2SJohnny Huang 		if (i % 2 == 0) {
1496d90825e2SJohnny Huang 			if ((data0_masked | buf0_masked) == buf0_masked) {
14974c1c9b35SJohnny Huang 				continue;
14984c1c9b35SJohnny Huang 			} else {
14994c1c9b35SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
1500d90825e2SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
15014c1c9b35SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
1502d90825e2SJohnny Huang 				printf("Mask    [%x] = %x\n", i, ~buf_keep[i]);
15032a856b9aSJohnny Huang 				return OTP_FAILURE;
150469d5fd8fSJohnny Huang 			}
1505d90825e2SJohnny Huang 		} else {
1506d90825e2SJohnny Huang 			if ((data0_masked & buf0_masked) == buf0_masked) {
1507d90825e2SJohnny Huang 				continue;
1508d90825e2SJohnny Huang 			} else {
1509d90825e2SJohnny Huang 				printf("Input image can't program into OTP, please check.\n");
1510d90825e2SJohnny Huang 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
1511d90825e2SJohnny Huang 				printf("Input   [%x] = %x\n", i, buf[i]);
1512d90825e2SJohnny Huang 				printf("Mask    [%x] = %x\n", i, ~buf_keep[i]);
15132a856b9aSJohnny Huang 				return OTP_FAILURE;
1514d90825e2SJohnny Huang 			}
1515d90825e2SJohnny Huang 		}
1516d90825e2SJohnny Huang 	}
151769d5fd8fSJohnny Huang 
1518d90825e2SJohnny Huang 	printf("Start Programing...\n");
1519d90825e2SJohnny Huang 	printProgress(0, 2048, "");
1520d90825e2SJohnny Huang 
1521d90825e2SJohnny Huang 	for (i = 0; i < 2048; i += 2) {
1522d90825e2SJohnny Huang 		prog_address = i;
1523d90825e2SJohnny Huang 		data0_masked = data[i]  & ~buf_keep[i];
1524d90825e2SJohnny Huang 		buf0_masked  = buf[i] & ~buf_keep[i];
1525d90825e2SJohnny Huang 		data1_masked = data[i + 1]  & ~buf_keep[i + 1];
1526d90825e2SJohnny Huang 		buf1_masked  = buf[i + 1] & ~buf_keep[i + 1];
1527d90825e2SJohnny Huang 		if ((data0_masked == buf0_masked) && (data1_masked == buf1_masked)) {
1528d90825e2SJohnny Huang 			printProgress(i + 2, 2048, "[%03X]=%08X HIT;[%03X]=%08X HIT", prog_address, buf[i], prog_address + 1, buf[i + 1]);
1529d90825e2SJohnny Huang 			continue;
1530d90825e2SJohnny Huang 		}
1531d90825e2SJohnny Huang 		if (soak) {
1532d90825e2SJohnny Huang 			soak = 0;
1533d90825e2SJohnny Huang 			otp_soak(0);
1534d90825e2SJohnny Huang 		}
1535d90825e2SJohnny Huang 		if (data1_masked == buf1_masked) {
1536d90825e2SJohnny Huang 			printProgress(i + 2, 2048, "[%03X]=%08X    ;[%03X]=%08X HIT", prog_address, buf[i], prog_address + 1, buf[i + 1]);
1537d90825e2SJohnny Huang 			otp_prog_dw(buf[i], buf_keep[i], prog_address);
1538d90825e2SJohnny Huang 		} else if (data0_masked == buf0_masked) {
1539d90825e2SJohnny Huang 			printProgress(i + 2, 2048, "[%03X]=%08X HIT;[%03X]=%08X    ", prog_address, buf[i], prog_address + 1, buf[i + 1]);
1540d90825e2SJohnny Huang 			otp_prog_dw(buf[i + 1], buf_keep[i + 1], prog_address + 1);
1541d90825e2SJohnny Huang 		} else {
1542d90825e2SJohnny Huang 			printProgress(i + 2, 2048, "[%03X]=%08X    ;[%03X]=%08X    ", prog_address, buf[i], prog_address + 1, buf[i + 1]);
1543d90825e2SJohnny Huang 			otp_prog_dw(buf[i], buf_keep[i], prog_address);
1544d90825e2SJohnny Huang 			otp_prog_dw(buf[i + 1], buf_keep[i + 1], prog_address + 1);
1545d90825e2SJohnny Huang 		}
1546d90825e2SJohnny Huang 
1547d90825e2SJohnny Huang 		pass = 0;
1548d90825e2SJohnny Huang 		for (k = 0; k < RETRY; k++) {
1549d90825e2SJohnny Huang 			if (verify_dw(prog_address, &buf[i], &buf_keep[i], compare, 2) != 0) {
1550d90825e2SJohnny Huang 				if (soak == 0) {
1551d90825e2SJohnny Huang 					soak = 1;
1552d90825e2SJohnny Huang 					otp_soak(1);
1553d90825e2SJohnny Huang 				}
1554d90825e2SJohnny Huang 				if (compare[0] != 0) {
1555d90825e2SJohnny Huang 					otp_prog_dw(compare[0], buf_keep[i], prog_address);
1556d90825e2SJohnny Huang 				}
1557d90825e2SJohnny Huang 				if (compare[1] != ~0) {
1558d90825e2SJohnny Huang 					otp_prog_dw(compare[1], buf_keep[i], prog_address + 1);
1559d90825e2SJohnny Huang 				}
1560d90825e2SJohnny Huang 			} else {
1561d90825e2SJohnny Huang 				pass = 1;
1562d90825e2SJohnny Huang 				break;
1563d90825e2SJohnny Huang 			}
1564d90825e2SJohnny Huang 		}
1565d90825e2SJohnny Huang 
1566d90825e2SJohnny Huang 		if (!pass)
15672a856b9aSJohnny Huang 			return OTP_FAILURE;
1568d90825e2SJohnny Huang 	}
15692a856b9aSJohnny Huang 	return OTP_SUCCESS;
1570d90825e2SJohnny Huang 
1571d90825e2SJohnny Huang }
1572d90825e2SJohnny Huang 
1573d90825e2SJohnny Huang static int do_otp_prog(int addr, int byte_size, int nconfirm)
157469d5fd8fSJohnny Huang {
157569d5fd8fSJohnny Huang 	int ret;
1576d90825e2SJohnny Huang 	int mode;
157769d5fd8fSJohnny Huang 	uint32_t *buf;
1578d90825e2SJohnny Huang 	uint32_t *data_region = NULL;
1579d90825e2SJohnny Huang 	uint32_t *conf_region = NULL;
1580d90825e2SJohnny Huang 	uint32_t *strap_region = NULL;
158169d5fd8fSJohnny Huang 
1582d90825e2SJohnny Huang 	buf = map_physmem(addr, byte_size, MAP_WRBACK);
158369d5fd8fSJohnny Huang 	if (!buf) {
158469d5fd8fSJohnny Huang 		puts("Failed to map physical memory\n");
15852a856b9aSJohnny Huang 		return OTP_FAILURE;
158669d5fd8fSJohnny Huang 	}
1587d90825e2SJohnny Huang 
1588d90825e2SJohnny Huang 	if (((buf[0] >> 29) & 0x7) == 0x7) {
1589d90825e2SJohnny Huang 		mode = OTP_REGION_ALL;
1590d90825e2SJohnny Huang 		conf_region = &buf[1];
1591d90825e2SJohnny Huang 		strap_region = &buf[25];
1592d90825e2SJohnny Huang 		data_region = &buf[31];
1593d90825e2SJohnny Huang 	} else {
1594d90825e2SJohnny Huang 		if (buf[0] & BIT(29)) {
1595d90825e2SJohnny Huang 			mode = OTP_REGION_DATA;
1596d90825e2SJohnny Huang 			data_region = &buf[31];
1597d90825e2SJohnny Huang 		}
1598d90825e2SJohnny Huang 		if (buf[0] & BIT(30)) {
1599d90825e2SJohnny Huang 			mode = OTP_REGION_CONF;
1600d90825e2SJohnny Huang 			strap_region = &buf[25];
1601d90825e2SJohnny Huang 		}
1602d90825e2SJohnny Huang 		if (buf[0] & BIT(31)) {
1603d90825e2SJohnny Huang 			mode = OTP_REGION_STRAP;
1604d90825e2SJohnny Huang 			conf_region = &buf[1];
1605d90825e2SJohnny Huang 		}
1606d90825e2SJohnny Huang 	}
160769d5fd8fSJohnny Huang 	if (!nconfirm) {
1608a6d0d645SJohnny Huang 		if (mode == OTP_REGION_CONF) {
16097f795e57SJohnny Huang 			printf("\nOTP configuration region :\n");
1610b458cd62SJohnny Huang 			if (otp_print_conf_image(conf_region) < 0) {
161169d5fd8fSJohnny Huang 				printf("OTP config error, please check.\n");
16122a856b9aSJohnny Huang 				return OTP_FAILURE;
161369d5fd8fSJohnny Huang 			}
1614a6d0d645SJohnny Huang 		} else if (mode == OTP_REGION_DATA) {
16157f795e57SJohnny Huang 			printf("\nOTP data region :\n");
16167f795e57SJohnny Huang 			if (otp_print_data_info(data_region) < 0) {
161769d5fd8fSJohnny Huang 				printf("OTP data error, please check.\n");
16182a856b9aSJohnny Huang 				return OTP_FAILURE;
161969d5fd8fSJohnny Huang 			}
1620a6d0d645SJohnny Huang 		} else if (mode == OTP_REGION_STRAP) {
16217f795e57SJohnny Huang 			printf("\nOTP strap region :\n");
1622b458cd62SJohnny Huang 			if (otp_print_strap_image(strap_region) < 0) {
162369d5fd8fSJohnny Huang 				printf("OTP strap error, please check.\n");
16242a856b9aSJohnny Huang 				return OTP_FAILURE;
162569d5fd8fSJohnny Huang 			}
1626a6d0d645SJohnny Huang 		} else if (mode == OTP_REGION_ALL) {
16277f795e57SJohnny Huang 			printf("\nOTP configuration region :\n");
1628b458cd62SJohnny Huang 			if (otp_print_conf_image(conf_region) < 0) {
162969d5fd8fSJohnny Huang 				printf("OTP config error, please check.\n");
16302a856b9aSJohnny Huang 				return OTP_FAILURE;
163169d5fd8fSJohnny Huang 			}
16327f795e57SJohnny Huang 			printf("\nOTP strap region :\n");
1633b458cd62SJohnny Huang 			if (otp_print_strap_image(strap_region) < 0) {
163469d5fd8fSJohnny Huang 				printf("OTP strap error, please check.\n");
16352a856b9aSJohnny Huang 				return OTP_FAILURE;
163669d5fd8fSJohnny Huang 			}
16377f795e57SJohnny Huang 			printf("\nOTP data region :\n");
16387f795e57SJohnny Huang 			if (otp_print_data_info(data_region) < 0) {
163969d5fd8fSJohnny Huang 				printf("OTP data error, please check.\n");
16402a856b9aSJohnny Huang 				return OTP_FAILURE;
164169d5fd8fSJohnny Huang 			}
164269d5fd8fSJohnny Huang 		}
164369d5fd8fSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
164469d5fd8fSJohnny Huang 		if (!confirm_yesno()) {
164569d5fd8fSJohnny Huang 			printf(" Aborting\n");
16462a856b9aSJohnny Huang 			return OTP_FAILURE;
164769d5fd8fSJohnny Huang 		}
164869d5fd8fSJohnny Huang 	}
1649a6d0d645SJohnny Huang 	if (mode == OTP_REGION_CONF) {
1650d90825e2SJohnny Huang 		return otp_prog_conf(conf_region);
1651a6d0d645SJohnny Huang 	} else if (mode == OTP_REGION_STRAP) {
1652d90825e2SJohnny Huang 		return otp_prog_strap(strap_region);
1653a6d0d645SJohnny Huang 	} else if (mode == OTP_REGION_DATA) {
1654d90825e2SJohnny Huang 		return otp_prog_data(data_region);
1655a6d0d645SJohnny Huang 	} else if (mode == OTP_REGION_ALL) {
165669d5fd8fSJohnny Huang 		printf("programing data region ... ");
1657d90825e2SJohnny Huang 		ret = otp_prog_data(data_region);
16582a856b9aSJohnny Huang 		if (ret != 0) {
165969d5fd8fSJohnny Huang 			printf("Error\n");
166069d5fd8fSJohnny Huang 			return ret;
166169d5fd8fSJohnny Huang 		} else {
166269d5fd8fSJohnny Huang 			printf("Done\n");
166369d5fd8fSJohnny Huang 		}
166469d5fd8fSJohnny Huang 		printf("programing strap region ... ");
1665d90825e2SJohnny Huang 		ret = otp_prog_strap(strap_region);
16662a856b9aSJohnny Huang 		if (ret != 0) {
166769d5fd8fSJohnny Huang 			printf("Error\n");
166869d5fd8fSJohnny Huang 			return ret;
166969d5fd8fSJohnny Huang 		} else {
167069d5fd8fSJohnny Huang 			printf("Done\n");
167169d5fd8fSJohnny Huang 		}
167269d5fd8fSJohnny Huang 		printf("programing configuration region ... ");
1673d90825e2SJohnny Huang 		ret = otp_prog_conf(conf_region);
16742a856b9aSJohnny Huang 		if (ret != 0) {
167569d5fd8fSJohnny Huang 			printf("Error\n");
167669d5fd8fSJohnny Huang 			return ret;
167769d5fd8fSJohnny Huang 		}
167869d5fd8fSJohnny Huang 		printf("Done\n");
16792a856b9aSJohnny Huang 		return OTP_SUCCESS;
168069d5fd8fSJohnny Huang 	}
1681cd1610b4SJohnny Huang 
16822a856b9aSJohnny Huang 	return OTP_USAGE;
16832a856b9aSJohnny Huang }
16842a856b9aSJohnny Huang 
16852a856b9aSJohnny Huang static int do_otp_prog_bit(int mode, int otp_dw_offset, int bit_offset, int value, int nconfirm)
1686cd1610b4SJohnny Huang {
1687a6af4a17SJohnny Huang 	uint32_t read[2];
1688cd1610b4SJohnny Huang 	uint32_t strap_buf[6];
1689d90825e2SJohnny Huang 	uint32_t prog_address = 0;
169066f2f8e5SJohnny Huang 	struct otpstrap_status otpstrap[64];
1691cd1610b4SJohnny Huang 	int otp_bit;
1692cd1610b4SJohnny Huang 	int i;
1693cd1610b4SJohnny Huang 	int pass;
1694a6af4a17SJohnny Huang 	int ret;
1695cd1610b4SJohnny Huang 
1696cd1610b4SJohnny Huang 	switch (mode) {
1697a6d0d645SJohnny Huang 	case OTP_REGION_CONF:
1698a6af4a17SJohnny Huang 		otp_read_config(otp_dw_offset, read);
1699cd1610b4SJohnny Huang 		prog_address = 0x800;
1700cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset / 8) * 0x200;
1701cd1610b4SJohnny Huang 		prog_address |= (otp_dw_offset % 8) * 0x2;
1702a6af4a17SJohnny Huang 		otp_bit = (read[0] >> bit_offset) & 0x1;
1703cd1610b4SJohnny Huang 		if (otp_bit == value) {
1704a6af4a17SJohnny Huang 			printf("OTPCFG%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
1705cd1610b4SJohnny Huang 			printf("No need to program\n");
17062a856b9aSJohnny Huang 			return OTP_SUCCESS;
1707cd1610b4SJohnny Huang 		}
1708cd1610b4SJohnny Huang 		if (otp_bit == 1 && value == 0) {
1709a6af4a17SJohnny Huang 			printf("OTPCFG%X[%X] = 1\n", otp_dw_offset, bit_offset);
1710cd1610b4SJohnny Huang 			printf("OTP is programed, which can't be clean\n");
17112a856b9aSJohnny Huang 			return OTP_FAILURE;
1712cd1610b4SJohnny Huang 		}
1713a6af4a17SJohnny Huang 		printf("Program OTPCFG%X[%X] to 1\n", otp_dw_offset, bit_offset);
1714cd1610b4SJohnny Huang 		break;
1715a6d0d645SJohnny Huang 	case OTP_REGION_DATA:
1716cd1610b4SJohnny Huang 		prog_address = otp_dw_offset;
1717cd1610b4SJohnny Huang 
1718cd1610b4SJohnny Huang 		if (otp_dw_offset % 2 == 0) {
1719a6af4a17SJohnny Huang 			otp_read_data(otp_dw_offset, read);
1720a6af4a17SJohnny Huang 			otp_bit = (read[0] >> bit_offset) & 0x1;
1721cd1610b4SJohnny Huang 		} else {
1722a6af4a17SJohnny Huang 			otp_read_data(otp_dw_offset - 1, read);
1723a6af4a17SJohnny Huang 			otp_bit = (read[1] >> bit_offset) & 0x1;
1724cd1610b4SJohnny Huang 		}
1725cd1610b4SJohnny Huang 		if (otp_bit == value) {
1726a6af4a17SJohnny Huang 			printf("OTPDATA%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
1727cd1610b4SJohnny Huang 			printf("No need to program\n");
17282a856b9aSJohnny Huang 			return OTP_SUCCESS;
1729cd1610b4SJohnny Huang 		}
1730cd1610b4SJohnny Huang 		if (otp_bit == 1 && value == 0) {
1731a6af4a17SJohnny Huang 			printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
1732cd1610b4SJohnny Huang 			printf("OTP is programed, which can't be clean\n");
17332a856b9aSJohnny Huang 			return OTP_FAILURE;
1734cd1610b4SJohnny Huang 		}
1735a6af4a17SJohnny Huang 		printf("Program OTPDATA%X[%X] to 1\n", otp_dw_offset, bit_offset);
1736cd1610b4SJohnny Huang 		break;
1737a6d0d645SJohnny Huang 	case OTP_REGION_STRAP:
1738cd1610b4SJohnny Huang 		otp_strp_status(otpstrap);
1739cd1610b4SJohnny Huang 		otp_print_strap(bit_offset, 1);
1740cd1610b4SJohnny Huang 		if (bit_offset < 32) {
1741cd1610b4SJohnny Huang 			strap_buf[0] = value << bit_offset;
1742cd1610b4SJohnny Huang 			strap_buf[2] = ~BIT(bit_offset);
1743cd1610b4SJohnny Huang 			strap_buf[3] = ~0;
1744cd1610b4SJohnny Huang 			strap_buf[5] = 0;
17452a856b9aSJohnny Huang 			// if (protect)
17462a856b9aSJohnny Huang 			// 	strap_buf[4] = BIT(bit_offset);
17472a856b9aSJohnny Huang 			// else
17482a856b9aSJohnny Huang 			// 	strap_buf[4] = 0;
1749cd1610b4SJohnny Huang 		} else {
1750cd1610b4SJohnny Huang 			strap_buf[1] = value << (bit_offset - 32);
1751cd1610b4SJohnny Huang 			strap_buf[2] = ~0;
1752cd1610b4SJohnny Huang 			strap_buf[3] = ~BIT(bit_offset - 32);
1753cd1610b4SJohnny Huang 			strap_buf[4] = 0;
17542a856b9aSJohnny Huang 			// if (protect)
17552a856b9aSJohnny Huang 			// 	strap_buf[5] = BIT(bit_offset - 32);
17562a856b9aSJohnny Huang 			// else
17572a856b9aSJohnny Huang 			// 	strap_buf[5] = 0;
1758cd1610b4SJohnny Huang 		}
175976d13988SJohnny Huang 		ret = otp_strap_image_confirm(strap_buf);
1760a6af4a17SJohnny Huang 		if (ret == OTP_FAILURE)
17612a856b9aSJohnny Huang 			return OTP_FAILURE;
1762a6af4a17SJohnny Huang 		else if (ret == OTP_PROG_SKIP)
1763a6af4a17SJohnny Huang 			return OTP_SUCCESS;
1764a6af4a17SJohnny Huang 
1765cd1610b4SJohnny Huang 		break;
1766cd1610b4SJohnny Huang 	}
1767cd1610b4SJohnny Huang 
1768cd1610b4SJohnny Huang 	if (!nconfirm) {
1769cd1610b4SJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
1770cd1610b4SJohnny Huang 		if (!confirm_yesno()) {
1771cd1610b4SJohnny Huang 			printf(" Aborting\n");
17722a856b9aSJohnny Huang 			return OTP_FAILURE;
1773cd1610b4SJohnny Huang 		}
1774cd1610b4SJohnny Huang 	}
1775cd1610b4SJohnny Huang 
1776cd1610b4SJohnny Huang 	switch (mode) {
1777a6d0d645SJohnny Huang 	case OTP_REGION_STRAP:
1778cd1610b4SJohnny Huang 		return otp_prog_strap(strap_buf);
1779a6d0d645SJohnny Huang 	case OTP_REGION_CONF:
1780a6d0d645SJohnny Huang 	case OTP_REGION_DATA:
1781cd1610b4SJohnny Huang 		otp_prog_bit(value, prog_address, bit_offset, 0);
1782cd1610b4SJohnny Huang 		pass = -1;
1783cd1610b4SJohnny Huang 		for (i = 0; i < RETRY; i++) {
1784a6d0d645SJohnny Huang 			if (verify_bit(prog_address, bit_offset, value) != 0) {
1785cd1610b4SJohnny Huang 				otp_prog_bit(value, prog_address, bit_offset, 1);
1786cd1610b4SJohnny Huang 			} else {
1787cd1610b4SJohnny Huang 				pass = 0;
1788cd1610b4SJohnny Huang 				break;
1789cd1610b4SJohnny Huang 			}
1790cd1610b4SJohnny Huang 		}
17912a856b9aSJohnny Huang 		if (pass == 0)
17922a856b9aSJohnny Huang 			return OTP_SUCCESS;
1793cd1610b4SJohnny Huang 	}
1794cd1610b4SJohnny Huang 
17952a856b9aSJohnny Huang 	return OTP_USAGE;
1796cd1610b4SJohnny Huang }
1797cd1610b4SJohnny Huang 
17982a856b9aSJohnny Huang static int do_otpread(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
179969d5fd8fSJohnny Huang {
18002a856b9aSJohnny Huang 	uint32_t offset, count;
18012a856b9aSJohnny Huang 	int ret;
180269d5fd8fSJohnny Huang 
18032a856b9aSJohnny Huang 	if (argc == 4) {
18042a856b9aSJohnny Huang 		offset = simple_strtoul(argv[2], NULL, 16);
18052a856b9aSJohnny Huang 		count = simple_strtoul(argv[3], NULL, 16);
18062a856b9aSJohnny Huang 	} else if (argc == 3) {
18072a856b9aSJohnny Huang 		offset = simple_strtoul(argv[2], NULL, 16);
18082a856b9aSJohnny Huang 		count = 1;
18092a856b9aSJohnny Huang 	} else {
181069d5fd8fSJohnny Huang 		return CMD_RET_USAGE;
181169d5fd8fSJohnny Huang 	}
181269d5fd8fSJohnny Huang 
181369d5fd8fSJohnny Huang 
18142a856b9aSJohnny Huang 	if (!strcmp(argv[1], "conf")) {
181569d5fd8fSJohnny Huang 		writel(OTP_PASSWD, 0x1e6f2000); //password
18162a856b9aSJohnny Huang 		ret = otp_print_config(offset, count);
18172a856b9aSJohnny Huang 	} else if (!strcmp(argv[1], "data")) {
18182a856b9aSJohnny Huang 		writel(OTP_PASSWD, 0x1e6f2000); //password
18192a856b9aSJohnny Huang 		ret = otp_print_data(offset, count);
18202a856b9aSJohnny Huang 	} else if (!strcmp(argv[1], "strap")) {
18212a856b9aSJohnny Huang 		writel(OTP_PASSWD, 0x1e6f2000); //password
18222a856b9aSJohnny Huang 		ret = otp_print_strap(offset, count);
18232a856b9aSJohnny Huang 	} else {
18242a856b9aSJohnny Huang 		return CMD_RET_USAGE;
182569d5fd8fSJohnny Huang 	}
182669d5fd8fSJohnny Huang 
18272a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
18282a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
18292a856b9aSJohnny Huang 	else
18302a856b9aSJohnny Huang 		return CMD_RET_USAGE;
18312a856b9aSJohnny Huang 
18322a856b9aSJohnny Huang }
18332a856b9aSJohnny Huang 
18342a856b9aSJohnny Huang static int do_otpprog(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
18352a856b9aSJohnny Huang {
18362a856b9aSJohnny Huang 	phys_addr_t addr;
18372a856b9aSJohnny Huang 	uint32_t byte_size;
18382a856b9aSJohnny Huang 	int ret;
18392a856b9aSJohnny Huang 
18402a856b9aSJohnny Huang 	if (argc == 4) {
18412a856b9aSJohnny Huang 		if (strcmp(argv[1], "f"))
18422a856b9aSJohnny Huang 			return CMD_RET_USAGE;
18432a856b9aSJohnny Huang 		addr = simple_strtoul(argv[2], NULL, 16);
18442a856b9aSJohnny Huang 		byte_size = simple_strtoul(argv[3], NULL, 16);
184569d5fd8fSJohnny Huang 		writel(OTP_PASSWD, 0x1e6f2000); //password
18462a856b9aSJohnny Huang 		ret = do_otp_prog(addr, byte_size, 1);
18472a856b9aSJohnny Huang 	} else if (argc == 3) {
18482a856b9aSJohnny Huang 		addr = simple_strtoul(argv[1], NULL, 16);
18492a856b9aSJohnny Huang 		byte_size = simple_strtoul(argv[2], NULL, 16);
18502a856b9aSJohnny Huang 		writel(OTP_PASSWD, 0x1e6f2000); //password
18512a856b9aSJohnny Huang 		ret = do_otp_prog(addr, byte_size, 0);
18522a856b9aSJohnny Huang 	} else {
18532a856b9aSJohnny Huang 		return CMD_RET_USAGE;
18542a856b9aSJohnny Huang 	}
18552a856b9aSJohnny Huang 
18562a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
18572a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
18582a856b9aSJohnny Huang 	else if (ret == OTP_FAILURE)
18592a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
18602a856b9aSJohnny Huang 	else
18612a856b9aSJohnny Huang 		return CMD_RET_USAGE;
18622a856b9aSJohnny Huang }
18632a856b9aSJohnny Huang 
18642a856b9aSJohnny Huang static int do_otppb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
18652a856b9aSJohnny Huang {
18662a856b9aSJohnny Huang 	int mode = 0;
18672a856b9aSJohnny Huang 	int nconfirm = 0;
18682a856b9aSJohnny Huang 	int otp_addr = 0;
18692a856b9aSJohnny Huang 	int bit_offset;
18702a856b9aSJohnny Huang 	int value;
18712a856b9aSJohnny Huang 	int ret;
18722a856b9aSJohnny Huang 
18732a856b9aSJohnny Huang 	if (argc != 4 && argc != 5 && argc != 6)
18742a856b9aSJohnny Huang 		return CMD_RET_USAGE;
18752a856b9aSJohnny Huang 
18762a856b9aSJohnny Huang 	/* Drop the pb cmd */
18772a856b9aSJohnny Huang 	argc--;
18782a856b9aSJohnny Huang 	argv++;
18792a856b9aSJohnny Huang 
18802a856b9aSJohnny Huang 	if (!strcmp(argv[0], "conf"))
1881a6d0d645SJohnny Huang 		mode = OTP_REGION_CONF;
18822a856b9aSJohnny Huang 	else if (!strcmp(argv[0], "strap"))
1883a6d0d645SJohnny Huang 		mode = OTP_REGION_STRAP;
18842a856b9aSJohnny Huang 	else if (!strcmp(argv[0], "data"))
1885a6d0d645SJohnny Huang 		mode = OTP_REGION_DATA;
1886cd1610b4SJohnny Huang 	else
18872a856b9aSJohnny Huang 		return CMD_RET_USAGE;
18882a856b9aSJohnny Huang 
18892a856b9aSJohnny Huang 	/* Drop the region cmd */
18902a856b9aSJohnny Huang 	argc--;
18912a856b9aSJohnny Huang 	argv++;
18922a856b9aSJohnny Huang 
18932a856b9aSJohnny Huang 	if (!strcmp(argv[0], "f")) {
1894cd1610b4SJohnny Huang 		nconfirm = 1;
18952a856b9aSJohnny Huang 		/* Drop the force option */
18962a856b9aSJohnny Huang 		argc--;
18972a856b9aSJohnny Huang 		argv++;
18982a856b9aSJohnny Huang 	}
1899cd1610b4SJohnny Huang 
1900a6d0d645SJohnny Huang 	if (mode == OTP_REGION_STRAP) {
19012a856b9aSJohnny Huang 		bit_offset = simple_strtoul(argv[0], NULL, 16);
19022a856b9aSJohnny Huang 		value = simple_strtoul(argv[1], NULL, 16);
1903cd1610b4SJohnny Huang 		if (bit_offset >= 64)
19042a856b9aSJohnny Huang 			return CMD_RET_USAGE;
1905cd1610b4SJohnny Huang 	} else {
19062a856b9aSJohnny Huang 		otp_addr = simple_strtoul(argv[0], NULL, 16);
19072a856b9aSJohnny Huang 		bit_offset = simple_strtoul(argv[1], NULL, 16);
19082a856b9aSJohnny Huang 		value = simple_strtoul(argv[2], NULL, 16);
1909cd1610b4SJohnny Huang 		if (bit_offset >= 32)
19102a856b9aSJohnny Huang 			return CMD_RET_USAGE;
1911cd1610b4SJohnny Huang 	}
1912cd1610b4SJohnny Huang 	if (value != 0 && value != 1)
19132a856b9aSJohnny Huang 		return CMD_RET_USAGE;
1914cd1610b4SJohnny Huang 
1915cd1610b4SJohnny Huang 	writel(OTP_PASSWD, 0x1e6f2000); //password
19162a856b9aSJohnny Huang 	ret = do_otp_prog_bit(mode, otp_addr, bit_offset, value, nconfirm);
19172a856b9aSJohnny Huang 
19182a856b9aSJohnny Huang 	if (ret == OTP_SUCCESS)
19192a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
19202a856b9aSJohnny Huang 	else if (ret == OTP_FAILURE)
19212a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
19222a856b9aSJohnny Huang 	else
19232a856b9aSJohnny Huang 		return CMD_RET_USAGE;
19242a856b9aSJohnny Huang }
19252a856b9aSJohnny Huang 
19262a856b9aSJohnny Huang static int do_otpcmp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
19272a856b9aSJohnny Huang {
19282a856b9aSJohnny Huang 	phys_addr_t addr;
19292a856b9aSJohnny Huang 	int otp_addr = 0;
19302a856b9aSJohnny Huang 
19312a856b9aSJohnny Huang 	if (argc != 3)
19322a856b9aSJohnny Huang 		return CMD_RET_USAGE;
19332a856b9aSJohnny Huang 
193469d5fd8fSJohnny Huang 	writel(OTP_PASSWD, 0x1e6f2000); //password
19352a856b9aSJohnny Huang 	addr = simple_strtoul(argv[1], NULL, 16);
19362a856b9aSJohnny Huang 	otp_addr = simple_strtoul(argv[2], NULL, 16);
19372a856b9aSJohnny Huang 	if (otp_compare(otp_addr, addr) == 0) {
193869d5fd8fSJohnny Huang 		printf("Compare pass\n");
19392a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
194069d5fd8fSJohnny Huang 	} else {
194169d5fd8fSJohnny Huang 		printf("Compare fail\n");
19422a856b9aSJohnny Huang 		return CMD_RET_FAILURE;
194369d5fd8fSJohnny Huang 	}
194469d5fd8fSJohnny Huang }
194569d5fd8fSJohnny Huang 
194666f2f8e5SJohnny Huang static int do_otpinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
194766f2f8e5SJohnny Huang {
1948a8bd6d8cSJohnny Huang 	int view = 0;
19492d4b0742SJohnny Huang 	int input;
1950a8bd6d8cSJohnny Huang 
1951a8bd6d8cSJohnny Huang 	if (argc != 2 && argc != 3)
195266f2f8e5SJohnny Huang 		return CMD_RET_USAGE;
195366f2f8e5SJohnny Huang 
19542d4b0742SJohnny Huang 	if (!strcmp(argv[1], "conf")) {
195566f2f8e5SJohnny Huang 
19562d4b0742SJohnny Huang 		writel(OTP_PASSWD, 0x1e6f2000); //password
19572d4b0742SJohnny Huang 		if (argc == 3) {
19582d4b0742SJohnny Huang 			input = simple_strtoul(argv[2], NULL, 16);
19592d4b0742SJohnny Huang 			otp_print_conf_info(input);
19602d4b0742SJohnny Huang 		} else {
19612d4b0742SJohnny Huang 			otp_print_conf_info(-1);
19622d4b0742SJohnny Huang 		}
19632d4b0742SJohnny Huang 	} else if (!strcmp(argv[1], "strap")) {
19642d4b0742SJohnny Huang 		if (!strcmp(argv[2], "v")) {
1965a8bd6d8cSJohnny Huang 			view = 1;
1966a8bd6d8cSJohnny Huang 			/* Drop the view option */
1967a8bd6d8cSJohnny Huang 			argc--;
1968a8bd6d8cSJohnny Huang 			argv++;
1969a8bd6d8cSJohnny Huang 		}
197066f2f8e5SJohnny Huang 		writel(OTP_PASSWD, 0x1e6f2000); //password
1971b458cd62SJohnny Huang 		otp_print_strap_info(view);
197266f2f8e5SJohnny Huang 	} else {
197366f2f8e5SJohnny Huang 		return CMD_RET_USAGE;
197466f2f8e5SJohnny Huang 	}
19752d4b0742SJohnny Huang 
197666f2f8e5SJohnny Huang 	return CMD_RET_SUCCESS;
197766f2f8e5SJohnny Huang }
197866f2f8e5SJohnny Huang 
1979737ed20bSJohnny Huang static int do_otpprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1980737ed20bSJohnny Huang {
1981737ed20bSJohnny Huang 	int input;
1982737ed20bSJohnny Huang 	int bit_offset;
1983737ed20bSJohnny Huang 	int prog_address;
1984737ed20bSJohnny Huang 	int pass;
1985737ed20bSJohnny Huang 	int i;
1986737ed20bSJohnny Huang 	if (argc != 3 && argc != 2)
1987737ed20bSJohnny Huang 		return CMD_RET_USAGE;
1988737ed20bSJohnny Huang 
1989737ed20bSJohnny Huang 	if (!strcmp(argv[0], "f")) {
1990737ed20bSJohnny Huang 		input = simple_strtoul(argv[2], NULL, 16);
1991737ed20bSJohnny Huang 	} else {
1992737ed20bSJohnny Huang 		input = simple_strtoul(argv[1], NULL, 16);
1993737ed20bSJohnny Huang 		printf("OTPSTRAP[%d] will be protected\n", input);
1994737ed20bSJohnny Huang 		printf("type \"YES\" (no quotes) to continue:\n");
1995737ed20bSJohnny Huang 		if (!confirm_yesno()) {
1996737ed20bSJohnny Huang 			printf(" Aborting\n");
1997737ed20bSJohnny Huang 			return CMD_RET_FAILURE;
1998737ed20bSJohnny Huang 		}
1999737ed20bSJohnny Huang 	}
2000737ed20bSJohnny Huang 
2001737ed20bSJohnny Huang 	prog_address = 0x800;
2002737ed20bSJohnny Huang 	if (input < 32) {
2003737ed20bSJohnny Huang 		bit_offset = input;
2004737ed20bSJohnny Huang 		prog_address |= 0x60c;
2005737ed20bSJohnny Huang 	} else if (input < 64) {
2006737ed20bSJohnny Huang 		bit_offset = input - 32;
2007737ed20bSJohnny Huang 		prog_address |= 0x60e;
2008737ed20bSJohnny Huang 	} else {
2009737ed20bSJohnny Huang 		return CMD_RET_USAGE;
2010737ed20bSJohnny Huang 	}
2011737ed20bSJohnny Huang 
2012737ed20bSJohnny Huang 	if (verify_bit(prog_address, bit_offset, 1) == 0) {
2013737ed20bSJohnny Huang 		printf("OTPSTRAP[%d] already protected\n", input);
2014737ed20bSJohnny Huang 	}
2015737ed20bSJohnny Huang 	otp_prog_bit(1, prog_address, bit_offset, 0);
2016737ed20bSJohnny Huang 	pass = -1;
2017737ed20bSJohnny Huang 	for (i = 0; i < RETRY; i++) {
2018737ed20bSJohnny Huang 		if (verify_bit(prog_address, bit_offset, 1) != 0) {
2019737ed20bSJohnny Huang 			otp_prog_bit(1, prog_address, bit_offset, 1);
2020737ed20bSJohnny Huang 		} else {
2021737ed20bSJohnny Huang 			pass = 0;
2022737ed20bSJohnny Huang 			break;
2023737ed20bSJohnny Huang 		}
2024737ed20bSJohnny Huang 	}
2025737ed20bSJohnny Huang 	if (pass == 0) {
2026737ed20bSJohnny Huang 		printf("OTPSTRAP[%d] is protected\n", input);
2027737ed20bSJohnny Huang 		return CMD_RET_SUCCESS;
2028737ed20bSJohnny Huang 	}
2029737ed20bSJohnny Huang 
2030737ed20bSJohnny Huang 	printf("Protect OTPSTRAP[%d] fail\n", input);
2031737ed20bSJohnny Huang 	return CMD_RET_FAILURE;
2032737ed20bSJohnny Huang 
2033737ed20bSJohnny Huang }
20342a856b9aSJohnny Huang static cmd_tbl_t cmd_otp[] = {
20352a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(read, 4, 0, do_otpread, "", ""),
2036a8bd6d8cSJohnny Huang 	U_BOOT_CMD_MKENT(info, 3, 0, do_otpinfo, "", ""),
20372a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(prog, 4, 0, do_otpprog, "", ""),
20382a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(pb, 6, 0, do_otppb, "", ""),
2039737ed20bSJohnny Huang 	U_BOOT_CMD_MKENT(protect, 3, 0, do_otpprotect, "", ""),
20402a856b9aSJohnny Huang 	U_BOOT_CMD_MKENT(cmp, 3, 0, do_otpcmp, "", ""),
20412a856b9aSJohnny Huang };
20422a856b9aSJohnny Huang 
20432a856b9aSJohnny Huang static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
20442a856b9aSJohnny Huang {
20452a856b9aSJohnny Huang 	cmd_tbl_t *cp;
20462a856b9aSJohnny Huang 
20472a856b9aSJohnny Huang 	cp = find_cmd_tbl(argv[1], cmd_otp, ARRAY_SIZE(cmd_otp));
20482a856b9aSJohnny Huang 
2049737ed20bSJohnny Huang 	/* Drop the otp command */
20502a856b9aSJohnny Huang 	argc--;
20512a856b9aSJohnny Huang 	argv++;
20522a856b9aSJohnny Huang 
20532a856b9aSJohnny Huang 	if (cp == NULL || argc > cp->maxargs)
20542a856b9aSJohnny Huang 		return CMD_RET_USAGE;
20552a856b9aSJohnny Huang 	if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
20562a856b9aSJohnny Huang 		return CMD_RET_SUCCESS;
20572a856b9aSJohnny Huang 
20582a856b9aSJohnny Huang 	return cp->cmd(cmdtp, flag, argc, argv);
205969d5fd8fSJohnny Huang }
206069d5fd8fSJohnny Huang 
206169d5fd8fSJohnny Huang U_BOOT_CMD(
206269d5fd8fSJohnny Huang 	otp, 7, 0,  do_ast_otp,
206369d5fd8fSJohnny Huang 	"ASPEED One-Time-Programmable sub-system",
20642a856b9aSJohnny Huang 	"read conf|data <otp_dw_offset> <dw_count>\n"
20652a856b9aSJohnny Huang 	"otp read strap <strap_bit_offset> <bit_count>\n"
20662d4b0742SJohnny Huang 	"otp info strap [v]\n"
20672d4b0742SJohnny Huang 	"otp info conf [otp_dw_offset]\n"
2068d90825e2SJohnny Huang 	"otp prog [f] <addr> <byte_size>\n"
2069cd1610b4SJohnny Huang 	"otp pb conf|data [f] <otp_dw_offset> <bit_offset> <value>\n"
2070737ed20bSJohnny Huang 	"otp pb strap [f] <bit_offset> <value>\n"
2071737ed20bSJohnny Huang 	"otp protect [f] <bit_offset>\n"
20722a856b9aSJohnny Huang 	"otp cmp <addr> <otp_dw_offset>\n"
207369d5fd8fSJohnny Huang );
2074