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