1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2 /*
3  * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
4  *
5  * the command stboard supports the STMicroelectronics board identification
6  * saved in OTP_BOARD.
7  *
8  * The ST product codification have several element
9  * - "Commercial Product Name" (CPN): type of product board (DKX, EVX)
10  *   associated to the board ID "MBxxxx"
11  * - "Finished Good" or "Finish Good" (FG):
12  *   effective content of the product without chip STM32MP1xx (LCD, Wifi,...)
13  * - BOM: cost variant for same FG (for example, several provider of the same
14  *   component)
15  *
16  * For example
17  * - commercial product = STM32MP157C-EV1 for board MB1263
18  * - Finished Good = EVA32MP157A1$AU1
19  *
20  * Both information are written on board and these information are also saved
21  * in OTP_BOARD (59 for STM32MP15x or 60 for STM32MP13x), with:
22  * bit [31:16] (hex) => Board id, MBxxxx
23  * bit [15:12] (dec) => Variant CPN (1....15)
24  * bit [11:8]  (dec) => Revision board (index with A = 1, Z = 26)
25  * bit [7:4]   (dec) => Variant FG : finished good index
26  * bit [3:0]   (dec) => BOM (01, .... 255)
27  *
28  * and displayed with the format:
29  *   Board: MB<Board> Var<VarCPN>.<VarFG> Rev.<Revision>-<BOM>
30  */
31 
32 #ifndef CONFIG_XPL_BUILD
33 #include <command.h>
34 #include <console.h>
35 #include <misc.h>
36 #include <asm/arch/bsec.h>
37 #include <dm/device.h>
38 #include <dm/uclass.h>
39 
check_stboard(u16 board)40 static bool check_stboard(u16 board)
41 {
42 	unsigned int i;
43 	/* list of supported ST boards */
44 	const u16 st_board_id[] = {
45 		0x1272,
46 		0x1263,
47 		0x1264,
48 		0x1298,
49 		0x1341,
50 		0x1497,
51 		0x1605, /* stm32mp25xx-dk */
52 		0x1635,
53 		0x1936, /* stm32mp25xx-ev1 */
54 	};
55 
56 	for (i = 0; i < ARRAY_SIZE(st_board_id); i++)
57 		if (board == st_board_id[i])
58 			return true;
59 
60 	return false;
61 }
62 
display_stboard(u32 otp)63 static void display_stboard(u32 otp)
64 {
65 	/* display board indentification with OPT coding */
66 	printf("Board: MB%04x Var%d.%d Rev.%c-%02d\n",
67 	       otp >> 16,
68 	       (otp >> 12) & 0xF,
69 	       (otp >> 4) & 0xF,
70 	       ((otp >> 8) & 0xF) - 1 + 'A',
71 	       otp & 0xF);
72 }
73 
do_stboard(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])74 static int do_stboard(struct cmd_tbl *cmdtp, int flag, int argc,
75 		      char *const argv[])
76 {
77 	int ret;
78 	u32 otp, lock;
79 	u8 revision;
80 	unsigned long board, var_cpn, var_fg, bom;
81 	struct udevice *dev;
82 	int confirmed = argc == 7 && !strcmp(argv[1], "-y");
83 
84 	argc -= 1 + confirmed;
85 	argv += 1 + confirmed;
86 
87 	if (argc != 0 && argc != 5)
88 		return CMD_RET_USAGE;
89 
90 	ret = uclass_get_device_by_driver(UCLASS_MISC,
91 					  DM_DRIVER_GET(stm32mp_bsec),
92 					  &dev);
93 
94 	ret = misc_read(dev, STM32_BSEC_OTP(BSEC_OTP_BOARD),
95 			&otp, sizeof(otp));
96 
97 	if (ret != sizeof(otp)) {
98 		puts("OTP read error\n");
99 		return CMD_RET_FAILURE;
100 	}
101 
102 	ret = misc_read(dev, STM32_BSEC_LOCK(BSEC_OTP_BOARD),
103 			&lock, sizeof(lock));
104 	if (ret != sizeof(lock)) {
105 		puts("LOCK read error\n");
106 		return CMD_RET_FAILURE;
107 	}
108 
109 	if (argc == 0) {
110 		if (!otp)
111 			puts("Board : OTP board FREE\n");
112 		else
113 			display_stboard(otp);
114 		printf("      OTP %d %s locked !\n", BSEC_OTP_BOARD,
115 		       lock & BSEC_LOCK_PERM ? "" : "NOT");
116 		return CMD_RET_SUCCESS;
117 	}
118 
119 	if (otp) {
120 		display_stboard(otp);
121 		printf("ERROR: OTP board not FREE\n");
122 		return CMD_RET_FAILURE;
123 	}
124 
125 	if (strict_strtoul(argv[0], 16, &board) < 0 ||
126 	    board == 0 || board > 0xFFFF) {
127 		printf("argument %d invalid: %s\n", 1, argv[0]);
128 		return CMD_RET_USAGE;
129 	}
130 
131 	if (strict_strtoul(argv[1], 10, &var_cpn) < 0 ||
132 	    var_cpn == 0 || var_cpn > 15) {
133 		printf("argument %d invalid: %s\n", 2, argv[1]);
134 		return CMD_RET_USAGE;
135 	}
136 
137 	revision = argv[2][0] - 'A' + 1;
138 	if (strlen(argv[2]) > 1 || revision == 0 || revision > 15) {
139 		printf("argument %d invalid: %s\n", 3, argv[2]);
140 		return CMD_RET_USAGE;
141 	}
142 
143 	if (strict_strtoul(argv[3], 10, &var_fg) < 0 ||
144 	    var_fg > 15) {
145 		printf("argument %d invalid: %s\n", 4, argv[3]);
146 		return CMD_RET_USAGE;
147 	}
148 
149 	if (strict_strtoul(argv[4], 10, &bom) < 0 ||
150 	    bom == 0 || bom > 15) {
151 		printf("argument %d invalid: %s\n", 4, argv[3]);
152 		return CMD_RET_USAGE;
153 	}
154 
155 	/* st board indentification value */
156 	otp = (board << 16) | (var_cpn << 12) | (revision << 8) |
157 	      (var_fg << 4) | bom;
158 	display_stboard(otp);
159 	printf("=> OTP[%d] = %08X\n", BSEC_OTP_BOARD, otp);
160 
161 	if (!check_stboard((u16)board)) {
162 		printf("Unknown board MB%04x\n", (u16)board);
163 		return CMD_RET_FAILURE;
164 	}
165 	if (!confirmed) {
166 		printf("Warning: Programming BOARD in OTP is irreversible!\n");
167 		printf("Really perform this OTP programming? <y/N>\n");
168 
169 		if (!confirm_yesno()) {
170 			puts("BOARD programming aborted\n");
171 			return CMD_RET_FAILURE;
172 		}
173 	}
174 
175 	ret = misc_write(dev, STM32_BSEC_OTP(BSEC_OTP_BOARD),
176 			 &otp, sizeof(otp));
177 
178 	if (ret != sizeof(otp)) {
179 		puts("BOARD programming error\n");
180 		return CMD_RET_FAILURE;
181 	}
182 
183 	/* write persistent lock */
184 	otp = BSEC_LOCK_PERM;
185 	ret = misc_write(dev, STM32_BSEC_LOCK(BSEC_OTP_BOARD),
186 			 &otp, sizeof(otp));
187 	if (ret != sizeof(otp)) {
188 		puts("BOARD lock error\n");
189 		return CMD_RET_FAILURE;
190 	}
191 
192 	puts("BOARD programming done\n");
193 
194 	return CMD_RET_SUCCESS;
195 }
196 
197 U_BOOT_CMD(stboard, 7, 0, do_stboard,
198 	   "read/write board reference in OTP",
199 	   "\n"
200 	   "  Print current board information\n"
201 	   "stboard [-y] <Board> <VarCPN> <Revision> <VarFG> <BOM>\n"
202 	   "  Write board information\n"
203 	   "  - Board: xxxx, example 1264 for MB1264\n"
204 	   "  - VarCPN: 1...15\n"
205 	   "  - Revision: A...O\n"
206 	   "  - VarFG: 0...15\n"
207 	   "  - BOM: 1...15\n");
208 
209 #endif
210