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