1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2014-2016, Toradex AG
4  */
5 
6 /*
7  * Helpers for i.MX OTP fusing during module production
8 */
9 
10 #include <common.h>
11 #ifndef CONFIG_SPL_BUILD
12 #include <command.h>
13 #include <console.h>
14 #include <fuse.h>
15 
mfgr_fuse(void)16 static int mfgr_fuse(void)
17 {
18 	unsigned val, val6;
19 
20 	fuse_sense(0, 5, &val);
21 	printf("Fuse 0, 5: %8x\n", val);
22 	fuse_sense(0, 6, &val6);
23 	printf("Fuse 0, 6: %8x\n", val6);
24 	fuse_sense(4, 3, &val);
25 	printf("Fuse 4, 3: %8x\n", val);
26 	fuse_sense(4, 2, &val);
27 	printf("Fuse 4, 2: %8x\n", val);
28 	if (val6 & 0x10) {
29 		puts("BT_FUSE_SEL already fused, will do nothing\n");
30 		return CMD_RET_FAILURE;
31 	}
32 	/* boot cfg */
33 	fuse_prog(0, 5, 0x00005062);
34 	/* BT_FUSE_SEL */
35 	fuse_prog(0, 6, 0x00000010);
36 	return CMD_RET_SUCCESS;
37 }
38 
do_mfgr_fuse(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])39 int do_mfgr_fuse(struct cmd_tbl *cmdtp, int flag, int argc,
40 		 char *const argv[])
41 {
42 	int ret;
43 	puts("Fusing...\n");
44 	ret = mfgr_fuse();
45 	if (ret == CMD_RET_SUCCESS)
46 		puts("done.\n");
47 	else
48 		puts("failed.\n");
49 	return ret;
50 }
51 
do_updt_fuse(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])52 int do_updt_fuse(struct cmd_tbl *cmdtp, int flag, int argc,
53 		 char *const argv[])
54 {
55 	unsigned val;
56 	int ret;
57 	int confirmed = argc >= 1 && !strcmp(argv[1], "-y");
58 
59 	/* can be used in scripts for command availability check */
60 	if (argc >= 1 && !strcmp(argv[1], "-n"))
61 		return CMD_RET_SUCCESS;
62 
63 	/* boot cfg */
64 	fuse_sense(0, 5, &val);
65 	printf("Fuse 0, 5: %8x\n", val);
66 	if (val & 0x10) {
67 		puts("Fast boot mode already fused, no need to fuse\n");
68 		return CMD_RET_SUCCESS;
69 	}
70 	if (!confirmed) {
71 		puts("Warning: Programming fuses is an irreversible operation!\n"
72 				"         Updating to fast boot mode prevents easy\n"
73 				"         downgrading to previous BSP versions.\n"
74 				"\nReally perform this fuse programming? <y/N>\n");
75 		if (!confirm_yesno())
76 			return CMD_RET_FAILURE;
77 	}
78 	puts("Fusing fast boot mode...\n");
79 	ret = fuse_prog(0, 5, 0x00005072);
80 	if (ret == CMD_RET_SUCCESS)
81 		puts("done.\n");
82 	else
83 		puts("failed.\n");
84 	return ret;
85 }
86 
87 U_BOOT_CMD(
88 	mfgr_fuse, 1, 0, do_mfgr_fuse,
89 	"OTP fusing during module production",
90 	""
91 );
92 
93 U_BOOT_CMD(
94 	updt_fuse, 2, 0, do_updt_fuse,
95 	"OTP fusing during module update",
96 	"updt_fuse [-n] [-y] - boot cfg fast boot mode fusing"
97 );
98 #endif /* CONFIG_SPL_BUILD */
99