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 |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <console.h> |
| 8 | #include <misc.h> |
| 9 | #include <dm/device.h> |
| 10 | #include <dm/uclass.h> |
| 11 | |
| 12 | static bool check_stboard(u16 board) |
| 13 | { |
| 14 | unsigned int i; |
| 15 | const u16 st_board_id[] = { |
| 16 | 0x1272, |
| 17 | 0x1263, |
| 18 | 0x1264, |
| 19 | 0x1298, |
| 20 | 0x1341, |
| 21 | 0x1497, |
| 22 | }; |
| 23 | |
| 24 | for (i = 0; i < ARRAY_SIZE(st_board_id); i++) |
| 25 | if (board == st_board_id[i]) |
| 26 | return true; |
| 27 | |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | static void display_stboard(u32 otp) |
| 32 | { |
| 33 | printf("Board: MB%04x Var%d Rev.%c-%02d\n", |
| 34 | otp >> 16, |
| 35 | (otp >> 12) & 0xF, |
| 36 | ((otp >> 8) & 0xF) - 1 + 'A', |
| 37 | otp & 0xF); |
| 38 | } |
| 39 | |
| 40 | static int do_stboard(cmd_tbl_t *cmdtp, int flag, int argc, |
| 41 | char * const argv[]) |
| 42 | { |
| 43 | int ret; |
| 44 | u32 otp; |
| 45 | u8 revision; |
| 46 | unsigned long board, variant, bom; |
| 47 | struct udevice *dev; |
| 48 | int confirmed = argc == 6 && !strcmp(argv[1], "-y"); |
| 49 | |
| 50 | argc -= 1 + confirmed; |
| 51 | argv += 1 + confirmed; |
| 52 | |
| 53 | if (argc != 0 && argc != 4) |
| 54 | return CMD_RET_USAGE; |
| 55 | |
| 56 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 57 | DM_GET_DRIVER(stm32mp_bsec), |
| 58 | &dev); |
| 59 | |
| 60 | ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD), |
| 61 | &otp, sizeof(otp)); |
| 62 | |
| 63 | if (ret) { |
| 64 | puts("OTP read error"); |
| 65 | return CMD_RET_FAILURE; |
| 66 | } |
| 67 | |
| 68 | if (argc == 0) { |
| 69 | if (!otp) |
| 70 | puts("Board : OTP board FREE\n"); |
| 71 | else |
| 72 | display_stboard(otp); |
| 73 | return CMD_RET_SUCCESS; |
| 74 | } |
| 75 | |
| 76 | if (otp) { |
| 77 | display_stboard(otp); |
| 78 | printf("ERROR: OTP board not FREE\n"); |
| 79 | return CMD_RET_FAILURE; |
| 80 | } |
| 81 | |
| 82 | if (strict_strtoul(argv[0], 16, &board) < 0 || |
| 83 | board == 0 || board > 0xFFFF) { |
| 84 | printf("argument %d invalid: %s\n", 1, argv[0]); |
| 85 | return CMD_RET_USAGE; |
| 86 | } |
| 87 | |
| 88 | if (strict_strtoul(argv[1], 10, &variant) < 0 || |
| 89 | variant == 0 || variant > 15) { |
| 90 | printf("argument %d invalid: %s\n", 2, argv[1]); |
| 91 | return CMD_RET_USAGE; |
| 92 | } |
| 93 | |
| 94 | revision = argv[2][0] - 'A' + 1; |
| 95 | if (strlen(argv[2]) > 1 || revision == 0 || revision > 15) { |
| 96 | printf("argument %d invalid: %s\n", 3, argv[2]); |
| 97 | return CMD_RET_USAGE; |
| 98 | } |
| 99 | |
| 100 | if (strict_strtoul(argv[3], 10, &bom) < 0 || |
| 101 | bom == 0 || bom > 15) { |
| 102 | printf("argument %d invalid: %s\n", 4, argv[3]); |
| 103 | return CMD_RET_USAGE; |
| 104 | } |
| 105 | |
| 106 | otp = (board << 16) | (variant << 12) | (revision << 8) | bom; |
| 107 | display_stboard(otp); |
| 108 | printf("=> OTP[%d] = %08X\n", BSEC_OTP_BOARD, otp); |
| 109 | |
| 110 | if (!check_stboard((u16)board)) { |
| 111 | printf("Unknown board MB%04x\n", (u16)board); |
| 112 | return CMD_RET_FAILURE; |
| 113 | } |
| 114 | if (!confirmed) { |
| 115 | printf("Warning: Programming BOARD in OTP is irreversible!\n"); |
| 116 | printf("Really perform this OTP programming? <y/N>\n"); |
| 117 | |
| 118 | if (!confirm_yesno()) { |
| 119 | puts("BOARD programming aborted\n"); |
| 120 | return CMD_RET_FAILURE; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | ret = misc_write(dev, STM32_BSEC_OTP(BSEC_OTP_BOARD), |
| 125 | &otp, sizeof(otp)); |
| 126 | |
| 127 | if (ret) { |
| 128 | puts("BOARD programming error\n"); |
| 129 | return CMD_RET_FAILURE; |
| 130 | } |
| 131 | puts("BOARD programming done\n"); |
| 132 | |
| 133 | return CMD_RET_SUCCESS; |
| 134 | } |
| 135 | |
| 136 | U_BOOT_CMD(stboard, 6, 0, do_stboard, |
| 137 | "read/write board reference in OTP", |
| 138 | "\n" |
| 139 | " Print current board information\n" |
| 140 | "stboard [-y] <Board> <Variant> <Revision> <BOM>\n" |
| 141 | " Write board information\n" |
| 142 | " - Board: xxxx, example 1264 for MB1264\n" |
| 143 | " - Variant: 1 ... 15\n" |
| 144 | " - Revision: A...O\n" |
| 145 | " - BOM: 1...15\n"); |