Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2019, STMicroelectronics - All Rights Reserved |
Patrick Delaunay | 52163d9 | 2020-03-24 09:05:00 +0100 | [diff] [blame] | 4 | * |
Patrick Delaunay | 6ed1cf6 | 2023-01-06 13:20:18 +0100 | [diff] [blame] | 5 | * the command stboard supports the STMicroelectronics board identification |
| 6 | * saved in OTP_BOARD. |
Patrick Delaunay | 52163d9 | 2020-03-24 09:05:00 +0100 | [diff] [blame] | 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): |
Michal Simek | cc046dc | 2024-04-16 08:55:19 +0200 | [diff] [blame^] | 12 | * effective content of the product without chip STM32MP1xx (LCD, Wifi,...) |
Patrick Delaunay | 52163d9 | 2020-03-24 09:05:00 +0100 | [diff] [blame] | 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 Delaunay | 6ed1cf6 | 2023-01-06 13:20:18 +0100 | [diff] [blame] | 21 | * in OTP_BOARD (59 for STM32MP15x or 60 for STM32MP13x), with: |
Patrick Delaunay | 52163d9 | 2020-03-24 09:05:00 +0100 | [diff] [blame] | 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> |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 30 | */ |
| 31 | |
Patrick Delaunay | 2a57bcb | 2020-01-13 15:17:40 +0100 | [diff] [blame] | 32 | #ifndef CONFIG_SPL_BUILD |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 33 | #include <common.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 34 | #include <command.h> |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 35 | #include <console.h> |
| 36 | #include <misc.h> |
Patrick Delaunay | d538829 | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 37 | #include <asm/arch/bsec.h> |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 38 | #include <dm/device.h> |
| 39 | #include <dm/uclass.h> |
| 40 | |
| 41 | static bool check_stboard(u16 board) |
| 42 | { |
| 43 | unsigned int i; |
Patrick Delaunay | 52163d9 | 2020-03-24 09:05:00 +0100 | [diff] [blame] | 44 | /* list of supported ST boards */ |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 45 | const u16 st_board_id[] = { |
| 46 | 0x1272, |
| 47 | 0x1263, |
| 48 | 0x1264, |
| 49 | 0x1298, |
| 50 | 0x1341, |
| 51 | 0x1497, |
Patrick Delaunay | a6c078c | 2024-01-15 15:05:53 +0100 | [diff] [blame] | 52 | 0x1605, /* stm32mp25xx-dk */ |
Patrick Delaunay | 6ed1cf6 | 2023-01-06 13:20:18 +0100 | [diff] [blame] | 53 | 0x1635, |
Patrick Delaunay | a6c078c | 2024-01-15 15:05:53 +0100 | [diff] [blame] | 54 | 0x1936, /* stm32mp25xx-ev1 */ |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | for (i = 0; i < ARRAY_SIZE(st_board_id); i++) |
| 58 | if (board == st_board_id[i]) |
| 59 | return true; |
| 60 | |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | static void display_stboard(u32 otp) |
| 65 | { |
Patrick Delaunay | 52163d9 | 2020-03-24 09:05:00 +0100 | [diff] [blame] | 66 | /* display board indentification with OPT coding */ |
| 67 | printf("Board: MB%04x Var%d.%d Rev.%c-%02d\n", |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 68 | otp >> 16, |
| 69 | (otp >> 12) & 0xF, |
Patrick Delaunay | 52163d9 | 2020-03-24 09:05:00 +0100 | [diff] [blame] | 70 | (otp >> 4) & 0xF, |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 71 | ((otp >> 8) & 0xF) - 1 + 'A', |
| 72 | otp & 0xF); |
| 73 | } |
| 74 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 75 | static int do_stboard(struct cmd_tbl *cmdtp, int flag, int argc, |
| 76 | char *const argv[]) |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 77 | { |
| 78 | int ret; |
Patrick Delaunay | 3085ee3 | 2020-02-12 19:37:39 +0100 | [diff] [blame] | 79 | u32 otp, lock; |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 80 | u8 revision; |
Patrick Delaunay | 52163d9 | 2020-03-24 09:05:00 +0100 | [diff] [blame] | 81 | unsigned long board, var_cpn, var_fg, bom; |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 82 | struct udevice *dev; |
Patrick Delaunay | 52163d9 | 2020-03-24 09:05:00 +0100 | [diff] [blame] | 83 | int confirmed = argc == 7 && !strcmp(argv[1], "-y"); |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 84 | |
| 85 | argc -= 1 + confirmed; |
| 86 | argv += 1 + confirmed; |
| 87 | |
Patrick Delaunay | 52163d9 | 2020-03-24 09:05:00 +0100 | [diff] [blame] | 88 | if (argc != 0 && argc != 5) |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 89 | return CMD_RET_USAGE; |
| 90 | |
| 91 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 92 | DM_DRIVER_GET(stm32mp_bsec), |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 93 | &dev); |
| 94 | |
Patrick Delaunay | b7ce015 | 2020-02-12 19:37:36 +0100 | [diff] [blame] | 95 | ret = misc_read(dev, STM32_BSEC_OTP(BSEC_OTP_BOARD), |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 96 | &otp, sizeof(otp)); |
| 97 | |
Patrick Delaunay | f6baeb5 | 2022-02-01 14:37:19 +0100 | [diff] [blame] | 98 | if (ret != sizeof(otp)) { |
Patrick Delaunay | 0262c96 | 2022-04-15 11:46:50 +0200 | [diff] [blame] | 99 | puts("OTP read error\n"); |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 100 | return CMD_RET_FAILURE; |
| 101 | } |
| 102 | |
Patrick Delaunay | 3085ee3 | 2020-02-12 19:37:39 +0100 | [diff] [blame] | 103 | ret = misc_read(dev, STM32_BSEC_LOCK(BSEC_OTP_BOARD), |
| 104 | &lock, sizeof(lock)); |
Patrick Delaunay | f6baeb5 | 2022-02-01 14:37:19 +0100 | [diff] [blame] | 105 | if (ret != sizeof(lock)) { |
Patrick Delaunay | 0262c96 | 2022-04-15 11:46:50 +0200 | [diff] [blame] | 106 | puts("LOCK read error\n"); |
Patrick Delaunay | 3085ee3 | 2020-02-12 19:37:39 +0100 | [diff] [blame] | 107 | return CMD_RET_FAILURE; |
| 108 | } |
| 109 | |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 110 | if (argc == 0) { |
| 111 | if (!otp) |
| 112 | puts("Board : OTP board FREE\n"); |
| 113 | else |
| 114 | display_stboard(otp); |
Patrick Delaunay | 3085ee3 | 2020-02-12 19:37:39 +0100 | [diff] [blame] | 115 | printf(" OTP %d %s locked !\n", BSEC_OTP_BOARD, |
Patrick Delaunay | d538829 | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 116 | lock & BSEC_LOCK_PERM ? "" : "NOT"); |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 117 | return CMD_RET_SUCCESS; |
| 118 | } |
| 119 | |
| 120 | if (otp) { |
| 121 | display_stboard(otp); |
| 122 | printf("ERROR: OTP board not FREE\n"); |
| 123 | return CMD_RET_FAILURE; |
| 124 | } |
| 125 | |
| 126 | if (strict_strtoul(argv[0], 16, &board) < 0 || |
| 127 | board == 0 || board > 0xFFFF) { |
| 128 | printf("argument %d invalid: %s\n", 1, argv[0]); |
| 129 | return CMD_RET_USAGE; |
| 130 | } |
| 131 | |
Patrick Delaunay | 52163d9 | 2020-03-24 09:05:00 +0100 | [diff] [blame] | 132 | if (strict_strtoul(argv[1], 10, &var_cpn) < 0 || |
| 133 | var_cpn == 0 || var_cpn > 15) { |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 134 | printf("argument %d invalid: %s\n", 2, argv[1]); |
| 135 | return CMD_RET_USAGE; |
| 136 | } |
| 137 | |
| 138 | revision = argv[2][0] - 'A' + 1; |
| 139 | if (strlen(argv[2]) > 1 || revision == 0 || revision > 15) { |
| 140 | printf("argument %d invalid: %s\n", 3, argv[2]); |
| 141 | return CMD_RET_USAGE; |
| 142 | } |
| 143 | |
Patrick Delaunay | 52163d9 | 2020-03-24 09:05:00 +0100 | [diff] [blame] | 144 | if (strict_strtoul(argv[3], 10, &var_fg) < 0 || |
| 145 | var_fg > 15) { |
| 146 | printf("argument %d invalid: %s\n", 4, argv[3]); |
| 147 | return CMD_RET_USAGE; |
| 148 | } |
| 149 | |
| 150 | if (strict_strtoul(argv[4], 10, &bom) < 0 || |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 151 | bom == 0 || bom > 15) { |
| 152 | printf("argument %d invalid: %s\n", 4, argv[3]); |
| 153 | return CMD_RET_USAGE; |
| 154 | } |
| 155 | |
Patrick Delaunay | 52163d9 | 2020-03-24 09:05:00 +0100 | [diff] [blame] | 156 | /* st board indentification value */ |
| 157 | otp = (board << 16) | (var_cpn << 12) | (revision << 8) | |
| 158 | (var_fg << 4) | bom; |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 159 | display_stboard(otp); |
| 160 | printf("=> OTP[%d] = %08X\n", BSEC_OTP_BOARD, otp); |
| 161 | |
| 162 | if (!check_stboard((u16)board)) { |
| 163 | printf("Unknown board MB%04x\n", (u16)board); |
| 164 | return CMD_RET_FAILURE; |
| 165 | } |
| 166 | if (!confirmed) { |
| 167 | printf("Warning: Programming BOARD in OTP is irreversible!\n"); |
| 168 | printf("Really perform this OTP programming? <y/N>\n"); |
| 169 | |
| 170 | if (!confirm_yesno()) { |
| 171 | puts("BOARD programming aborted\n"); |
| 172 | return CMD_RET_FAILURE; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | ret = misc_write(dev, STM32_BSEC_OTP(BSEC_OTP_BOARD), |
| 177 | &otp, sizeof(otp)); |
| 178 | |
Patrick Delaunay | f6baeb5 | 2022-02-01 14:37:19 +0100 | [diff] [blame] | 179 | if (ret != sizeof(otp)) { |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 180 | puts("BOARD programming error\n"); |
| 181 | return CMD_RET_FAILURE; |
| 182 | } |
Patrick Delaunay | 3085ee3 | 2020-02-12 19:37:39 +0100 | [diff] [blame] | 183 | |
| 184 | /* write persistent lock */ |
Patrick Delaunay | d538829 | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 185 | otp = BSEC_LOCK_PERM; |
Patrick Delaunay | 3085ee3 | 2020-02-12 19:37:39 +0100 | [diff] [blame] | 186 | ret = misc_write(dev, STM32_BSEC_LOCK(BSEC_OTP_BOARD), |
| 187 | &otp, sizeof(otp)); |
Patrick Delaunay | f6baeb5 | 2022-02-01 14:37:19 +0100 | [diff] [blame] | 188 | if (ret != sizeof(otp)) { |
Patrick Delaunay | 3085ee3 | 2020-02-12 19:37:39 +0100 | [diff] [blame] | 189 | puts("BOARD lock error\n"); |
| 190 | return CMD_RET_FAILURE; |
| 191 | } |
| 192 | |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 193 | puts("BOARD programming done\n"); |
| 194 | |
| 195 | return CMD_RET_SUCCESS; |
| 196 | } |
| 197 | |
Patrick Delaunay | 52163d9 | 2020-03-24 09:05:00 +0100 | [diff] [blame] | 198 | U_BOOT_CMD(stboard, 7, 0, do_stboard, |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 199 | "read/write board reference in OTP", |
| 200 | "\n" |
| 201 | " Print current board information\n" |
Patrick Delaunay | 52163d9 | 2020-03-24 09:05:00 +0100 | [diff] [blame] | 202 | "stboard [-y] <Board> <VarCPN> <Revision> <VarFG> <BOM>\n" |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 203 | " Write board information\n" |
| 204 | " - Board: xxxx, example 1264 for MB1264\n" |
Patrick Delaunay | 52163d9 | 2020-03-24 09:05:00 +0100 | [diff] [blame] | 205 | " - VarCPN: 1...15\n" |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 206 | " - Revision: A...O\n" |
Patrick Delaunay | 52163d9 | 2020-03-24 09:05:00 +0100 | [diff] [blame] | 207 | " - VarFG: 0...15\n" |
Patrick Delaunay | 2a35037 | 2019-07-05 17:20:16 +0200 | [diff] [blame] | 208 | " - BOM: 1...15\n"); |
Patrick Delaunay | 2a57bcb | 2020-01-13 15:17:40 +0100 | [diff] [blame] | 209 | |
| 210 | #endif |