blob: 853ab78bbf167cb5e832a39991d1150953f0179c [file] [log] [blame]
Patrick Delaunay2a350372019-07-05 17:20:16 +02001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
Patrick Delaunay52163d92020-03-24 09:05:00 +01004 *
Patrick Delaunay6ed1cf62023-01-06 13:20:18 +01005 * the command stboard supports the STMicroelectronics board identification
6 * saved in OTP_BOARD.
Patrick Delaunay52163d92020-03-24 09:05:00 +01007 *
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
Patrick Delaunay6ed1cf62023-01-06 13:20:18 +010021 * in OTP_BOARD (59 for STM32MP15x or 60 for STM32MP13x), with:
Patrick Delaunay52163d92020-03-24 09:05:00 +010022 * 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>
Patrick Delaunay2a350372019-07-05 17:20:16 +020030 */
31
Patrick Delaunay2a57bcb2020-01-13 15:17:40 +010032#ifndef CONFIG_SPL_BUILD
Patrick Delaunay2a350372019-07-05 17:20:16 +020033#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -060034#include <command.h>
Patrick Delaunay2a350372019-07-05 17:20:16 +020035#include <console.h>
36#include <misc.h>
Patrick Delaunayd5388292023-01-06 13:20:15 +010037#include <asm/arch/bsec.h>
Patrick Delaunay2a350372019-07-05 17:20:16 +020038#include <dm/device.h>
39#include <dm/uclass.h>
40
41static bool check_stboard(u16 board)
42{
43 unsigned int i;
Patrick Delaunay52163d92020-03-24 09:05:00 +010044 /* list of supported ST boards */
Patrick Delaunay2a350372019-07-05 17:20:16 +020045 const u16 st_board_id[] = {
46 0x1272,
47 0x1263,
48 0x1264,
49 0x1298,
50 0x1341,
51 0x1497,
Patrick Delaunay6ed1cf62023-01-06 13:20:18 +010052 0x1635,
Patrick Delaunay2a350372019-07-05 17:20:16 +020053 };
54
55 for (i = 0; i < ARRAY_SIZE(st_board_id); i++)
56 if (board == st_board_id[i])
57 return true;
58
59 return false;
60}
61
62static void display_stboard(u32 otp)
63{
Patrick Delaunay52163d92020-03-24 09:05:00 +010064 /* display board indentification with OPT coding */
65 printf("Board: MB%04x Var%d.%d Rev.%c-%02d\n",
Patrick Delaunay2a350372019-07-05 17:20:16 +020066 otp >> 16,
67 (otp >> 12) & 0xF,
Patrick Delaunay52163d92020-03-24 09:05:00 +010068 (otp >> 4) & 0xF,
Patrick Delaunay2a350372019-07-05 17:20:16 +020069 ((otp >> 8) & 0xF) - 1 + 'A',
70 otp & 0xF);
71}
72
Simon Glassed38aef2020-05-10 11:40:03 -060073static int do_stboard(struct cmd_tbl *cmdtp, int flag, int argc,
74 char *const argv[])
Patrick Delaunay2a350372019-07-05 17:20:16 +020075{
76 int ret;
Patrick Delaunay3085ee32020-02-12 19:37:39 +010077 u32 otp, lock;
Patrick Delaunay2a350372019-07-05 17:20:16 +020078 u8 revision;
Patrick Delaunay52163d92020-03-24 09:05:00 +010079 unsigned long board, var_cpn, var_fg, bom;
Patrick Delaunay2a350372019-07-05 17:20:16 +020080 struct udevice *dev;
Patrick Delaunay52163d92020-03-24 09:05:00 +010081 int confirmed = argc == 7 && !strcmp(argv[1], "-y");
Patrick Delaunay2a350372019-07-05 17:20:16 +020082
83 argc -= 1 + confirmed;
84 argv += 1 + confirmed;
85
Patrick Delaunay52163d92020-03-24 09:05:00 +010086 if (argc != 0 && argc != 5)
Patrick Delaunay2a350372019-07-05 17:20:16 +020087 return CMD_RET_USAGE;
88
89 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -070090 DM_DRIVER_GET(stm32mp_bsec),
Patrick Delaunay2a350372019-07-05 17:20:16 +020091 &dev);
92
Patrick Delaunayb7ce0152020-02-12 19:37:36 +010093 ret = misc_read(dev, STM32_BSEC_OTP(BSEC_OTP_BOARD),
Patrick Delaunay2a350372019-07-05 17:20:16 +020094 &otp, sizeof(otp));
95
Patrick Delaunayf6baeb52022-02-01 14:37:19 +010096 if (ret != sizeof(otp)) {
Patrick Delaunay0262c962022-04-15 11:46:50 +020097 puts("OTP read error\n");
Patrick Delaunay2a350372019-07-05 17:20:16 +020098 return CMD_RET_FAILURE;
99 }
100
Patrick Delaunay3085ee32020-02-12 19:37:39 +0100101 ret = misc_read(dev, STM32_BSEC_LOCK(BSEC_OTP_BOARD),
102 &lock, sizeof(lock));
Patrick Delaunayf6baeb52022-02-01 14:37:19 +0100103 if (ret != sizeof(lock)) {
Patrick Delaunay0262c962022-04-15 11:46:50 +0200104 puts("LOCK read error\n");
Patrick Delaunay3085ee32020-02-12 19:37:39 +0100105 return CMD_RET_FAILURE;
106 }
107
Patrick Delaunay2a350372019-07-05 17:20:16 +0200108 if (argc == 0) {
109 if (!otp)
110 puts("Board : OTP board FREE\n");
111 else
112 display_stboard(otp);
Patrick Delaunay3085ee32020-02-12 19:37:39 +0100113 printf(" OTP %d %s locked !\n", BSEC_OTP_BOARD,
Patrick Delaunayd5388292023-01-06 13:20:15 +0100114 lock & BSEC_LOCK_PERM ? "" : "NOT");
Patrick Delaunay2a350372019-07-05 17:20:16 +0200115 return CMD_RET_SUCCESS;
116 }
117
118 if (otp) {
119 display_stboard(otp);
120 printf("ERROR: OTP board not FREE\n");
121 return CMD_RET_FAILURE;
122 }
123
124 if (strict_strtoul(argv[0], 16, &board) < 0 ||
125 board == 0 || board > 0xFFFF) {
126 printf("argument %d invalid: %s\n", 1, argv[0]);
127 return CMD_RET_USAGE;
128 }
129
Patrick Delaunay52163d92020-03-24 09:05:00 +0100130 if (strict_strtoul(argv[1], 10, &var_cpn) < 0 ||
131 var_cpn == 0 || var_cpn > 15) {
Patrick Delaunay2a350372019-07-05 17:20:16 +0200132 printf("argument %d invalid: %s\n", 2, argv[1]);
133 return CMD_RET_USAGE;
134 }
135
136 revision = argv[2][0] - 'A' + 1;
137 if (strlen(argv[2]) > 1 || revision == 0 || revision > 15) {
138 printf("argument %d invalid: %s\n", 3, argv[2]);
139 return CMD_RET_USAGE;
140 }
141
Patrick Delaunay52163d92020-03-24 09:05:00 +0100142 if (strict_strtoul(argv[3], 10, &var_fg) < 0 ||
143 var_fg > 15) {
144 printf("argument %d invalid: %s\n", 4, argv[3]);
145 return CMD_RET_USAGE;
146 }
147
148 if (strict_strtoul(argv[4], 10, &bom) < 0 ||
Patrick Delaunay2a350372019-07-05 17:20:16 +0200149 bom == 0 || bom > 15) {
150 printf("argument %d invalid: %s\n", 4, argv[3]);
151 return CMD_RET_USAGE;
152 }
153
Patrick Delaunay52163d92020-03-24 09:05:00 +0100154 /* st board indentification value */
155 otp = (board << 16) | (var_cpn << 12) | (revision << 8) |
156 (var_fg << 4) | bom;
Patrick Delaunay2a350372019-07-05 17:20:16 +0200157 display_stboard(otp);
158 printf("=> OTP[%d] = %08X\n", BSEC_OTP_BOARD, otp);
159
160 if (!check_stboard((u16)board)) {
161 printf("Unknown board MB%04x\n", (u16)board);
162 return CMD_RET_FAILURE;
163 }
164 if (!confirmed) {
165 printf("Warning: Programming BOARD in OTP is irreversible!\n");
166 printf("Really perform this OTP programming? <y/N>\n");
167
168 if (!confirm_yesno()) {
169 puts("BOARD programming aborted\n");
170 return CMD_RET_FAILURE;
171 }
172 }
173
174 ret = misc_write(dev, STM32_BSEC_OTP(BSEC_OTP_BOARD),
175 &otp, sizeof(otp));
176
Patrick Delaunayf6baeb52022-02-01 14:37:19 +0100177 if (ret != sizeof(otp)) {
Patrick Delaunay2a350372019-07-05 17:20:16 +0200178 puts("BOARD programming error\n");
179 return CMD_RET_FAILURE;
180 }
Patrick Delaunay3085ee32020-02-12 19:37:39 +0100181
182 /* write persistent lock */
Patrick Delaunayd5388292023-01-06 13:20:15 +0100183 otp = BSEC_LOCK_PERM;
Patrick Delaunay3085ee32020-02-12 19:37:39 +0100184 ret = misc_write(dev, STM32_BSEC_LOCK(BSEC_OTP_BOARD),
185 &otp, sizeof(otp));
Patrick Delaunayf6baeb52022-02-01 14:37:19 +0100186 if (ret != sizeof(otp)) {
Patrick Delaunay3085ee32020-02-12 19:37:39 +0100187 puts("BOARD lock error\n");
188 return CMD_RET_FAILURE;
189 }
190
Patrick Delaunay2a350372019-07-05 17:20:16 +0200191 puts("BOARD programming done\n");
192
193 return CMD_RET_SUCCESS;
194}
195
Patrick Delaunay52163d92020-03-24 09:05:00 +0100196U_BOOT_CMD(stboard, 7, 0, do_stboard,
Patrick Delaunay2a350372019-07-05 17:20:16 +0200197 "read/write board reference in OTP",
198 "\n"
199 " Print current board information\n"
Patrick Delaunay52163d92020-03-24 09:05:00 +0100200 "stboard [-y] <Board> <VarCPN> <Revision> <VarFG> <BOM>\n"
Patrick Delaunay2a350372019-07-05 17:20:16 +0200201 " Write board information\n"
202 " - Board: xxxx, example 1264 for MB1264\n"
Patrick Delaunay52163d92020-03-24 09:05:00 +0100203 " - VarCPN: 1...15\n"
Patrick Delaunay2a350372019-07-05 17:20:16 +0200204 " - Revision: A...O\n"
Patrick Delaunay52163d92020-03-24 09:05:00 +0100205 " - VarFG: 0...15\n"
Patrick Delaunay2a350372019-07-05 17:20:16 +0200206 " - BOM: 1...15\n");
Patrick Delaunay2a57bcb2020-01-13 15:17:40 +0100207
208#endif