blob: 7cadd05c0a780196e542f867dfdec9e6409d5aa9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01002/*
Marcel Ziswiler75b93272020-01-28 14:42:23 +01003 * Copyright (c) 2016-2020 Toradex
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01004 */
5
6#include <common.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06007#include <asm/global_data.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01008#include "tdx-cfg-block.h"
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +03009#include "tdx-eeprom.h"
10
Simon Glassed38aef2020-05-10 11:40:03 -060011#include <command.h>
Simon Glass274e0b02020-05-10 11:39:56 -060012#include <asm/cache.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010013
Marcel Ziswiler2658c8e2019-04-09 17:25:32 +020014#if defined(CONFIG_TARGET_APALIS_IMX6) || \
Marcel Ziswiler00320612019-07-12 12:35:08 +020015 defined(CONFIG_TARGET_APALIS_IMX8) || \
Marcel Ziswiler75b93272020-01-28 14:42:23 +010016 defined(CONFIG_TARGET_APALIS_IMX8X) || \
Marcel Ziswiler2658c8e2019-04-09 17:25:32 +020017 defined(CONFIG_TARGET_COLIBRI_IMX6) || \
Marcel Ziswilereca26ba2020-01-28 14:42:24 +010018 defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
19 defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +020020 defined(CONFIG_TARGET_VERDIN_IMX8MN) || \
21 defined(CONFIG_TARGET_VERDIN_IMX8MP)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010022#include <asm/arch/sys_proto.h>
23#else
24#define is_cpu_type(cpu) (0)
25#endif
26#if defined(CONFIG_CPU_PXA27X)
27#include <asm/arch-pxa/pxa.h>
28#else
29#define cpu_is_pxa27x(cpu) (0)
30#endif
31#include <cli.h>
32#include <console.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060033#include <env.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010034#include <flash.h>
35#include <malloc.h>
36#include <mmc.h>
37#include <nand.h>
Simon Glass0ffb9d62017-05-31 19:47:48 -060038#include <asm/mach-types.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010039
40DECLARE_GLOBAL_DATA_PTR;
41
42#define TAG_VALID 0xcf01
43#define TAG_MAC 0x0000
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030044#define TAG_CAR_SERIAL 0x0021
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010045#define TAG_HW 0x0008
46#define TAG_INVALID 0xffff
47
48#define TAG_FLAG_VALID 0x1
49
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030050#define TDX_EEPROM_ID_MODULE 0
51#define TDX_EEPROM_ID_CARRIER 1
52
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010053#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
54#define TDX_CFG_BLOCK_MAX_SIZE 512
55#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
56#define TDX_CFG_BLOCK_MAX_SIZE 64
57#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
58#define TDX_CFG_BLOCK_MAX_SIZE 64
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030059#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
60#define TDX_CFG_BLOCK_MAX_SIZE 64
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010061#else
62#error Toradex config block location not set
63#endif
64
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030065#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
66#define TDX_CFG_BLOCK_EXTRA_MAX_SIZE 64
67#endif
68
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010069struct toradex_tag {
70 u32 len:14;
71 u32 flags:2;
72 u32 id:16;
73};
74
75bool valid_cfgblock;
76struct toradex_hw tdx_hw_tag;
77struct toradex_eth_addr tdx_eth_addr;
78u32 tdx_serial;
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030079#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
80u32 tdx_car_serial;
81bool valid_cfgblock_carrier;
82struct toradex_hw tdx_car_hw_tag;
83#endif
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010084
85const char * const toradex_modules[] = {
86 [0] = "UNKNOWN MODULE",
87 [1] = "Colibri PXA270 312MHz",
88 [2] = "Colibri PXA270 520MHz",
89 [3] = "Colibri PXA320 806MHz",
90 [4] = "Colibri PXA300 208MHz",
91 [5] = "Colibri PXA310 624MHz",
92 [6] = "Colibri PXA320 806MHz IT",
93 [7] = "Colibri PXA300 208MHz XT",
94 [8] = "Colibri PXA270 312MHz",
95 [9] = "Colibri PXA270 520MHz",
96 [10] = "Colibri VF50 128MB", /* not currently on sale */
97 [11] = "Colibri VF61 256MB",
98 [12] = "Colibri VF61 256MB IT",
99 [13] = "Colibri VF50 128MB IT",
100 [14] = "Colibri iMX6 Solo 256MB",
101 [15] = "Colibri iMX6 DualLite 512MB",
102 [16] = "Colibri iMX6 Solo 256MB IT",
103 [17] = "Colibri iMX6 DualLite 512MB IT",
104 [18] = "UNKNOWN MODULE",
105 [19] = "UNKNOWN MODULE",
106 [20] = "Colibri T20 256MB",
107 [21] = "Colibri T20 512MB",
108 [22] = "Colibri T20 512MB IT",
109 [23] = "Colibri T30 1GB",
110 [24] = "Colibri T20 256MB IT",
111 [25] = "Apalis T30 2GB",
112 [26] = "Apalis T30 1GB",
113 [27] = "Apalis iMX6 Quad 1GB",
114 [28] = "Apalis iMX6 Quad 2GB IT",
115 [29] = "Apalis iMX6 Dual 512MB",
116 [30] = "Colibri T30 1GB IT",
117 [31] = "Apalis T30 1GB IT",
118 [32] = "Colibri iMX7 Solo 256MB",
119 [33] = "Colibri iMX7 Dual 512MB",
120 [34] = "Apalis TK1 2GB",
121 [35] = "Apalis iMX6 Dual 1GB IT",
Stefan Agner01875e92018-05-30 19:01:47 +0200122 [36] = "Colibri iMX6ULL 256MB",
Marcel Ziswiler2658c8e2019-04-09 17:25:32 +0200123 [37] = "Apalis iMX8 QuadMax 4GB Wi-Fi / BT IT",
124 [38] = "Colibri iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
Stefan Agner01875e92018-05-30 19:01:47 +0200125 [39] = "Colibri iMX7 Dual 1GB (eMMC)",
Marcel Ziswiler2658c8e2019-04-09 17:25:32 +0200126 [40] = "Colibri iMX6ULL 512MB Wi-Fi / BT IT",
Stefan Agner01875e92018-05-30 19:01:47 +0200127 [41] = "Colibri iMX7 Dual 512MB EPDC",
128 [42] = "Apalis TK1 4GB",
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200129 [43] = "Colibri T20 512MB IT SETEK",
130 [44] = "Colibri iMX6ULL 512MB IT",
131 [45] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth",
Marcel Ziswilerf60ffec2019-04-09 17:25:33 +0200132 [46] = "Apalis iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
133 [47] = "Apalis iMX8 QuadMax 4GB IT",
134 [48] = "Apalis iMX8 QuadPlus 2GB Wi-Fi / BT",
135 [49] = "Apalis iMX8 QuadPlus 2GB",
136 [50] = "Colibri iMX8 QuadXPlus 2GB IT",
137 [51] = "Colibri iMX8 DualX 1GB Wi-Fi / Bluetooth",
138 [52] = "Colibri iMX8 DualX 1GB",
Marcel Ziswiler75b93272020-01-28 14:42:23 +0100139 [53] = "Apalis iMX8 QuadXPlus 2GB ECC IT",
140 [54] = "Apalis iMX8 DualXPlus 1GB",
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100141 [55] = "Verdin iMX8M Mini Quad 2GB Wi-Fi / BT IT",
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200142 [56] = "Verdin iMX8M Nano Quad 1GB Wi-Fi / BT", /* not currently on sale */
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100143 [57] = "Verdin iMX8M Mini DualLite 1GB",
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200144 [58] = "Verdin iMX8M Plus Quad 4GB Wi-Fi / BT IT",
145 [59] = "Verdin iMX8M Mini Quad 2GB IT",
146 [60] = "Verdin iMX8M Mini DualLite 1GB WB IT",
147 [61] = "Verdin iMX8M Plus Quad 2GB",
Marcel Ziswilerc066a442021-10-06 18:55:34 +0200148 [62] = "Colibri iMX6ULL 1GB IT (eMMC)",
149 [63] = "Verdin iMX8M Plus Quad 4GB IT",
150 [64] = "Verdin iMX8M Plus Quad 2GB Wi-Fi / BT IT",
151 [65] = "Verdin iMX8M Plus QuadLite 1GB IT",
152 [66] = "Verdin iMX8M Plus Quad 8GB Wi-Fi / BT",
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100153};
154
Igor Opaniuk414df352020-07-15 13:30:54 +0300155const char * const toradex_carrier_boards[] = {
156 [0] = "UNKNOWN CARRIER BOARD",
157 [155] = "Dahlia",
158 [156] = "Verdin Development Board",
159};
160
161const char * const toradex_display_adapters[] = {
162 [0] = "UNKNOWN DISPLAY ADAPTER",
163 [157] = "Verdin DSI to HDMI Adapter",
164 [159] = "Verdin DSI to LVDS Adapter",
165};
166
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100167#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
168static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
169{
170 struct mmc *mmc;
171 int dev = CONFIG_TDX_CFG_BLOCK_DEV;
172 int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
173 uint part = CONFIG_TDX_CFG_BLOCK_PART;
174 uint blk_start;
175 int ret = 0;
176
177 /* Read production parameter config block from eMMC */
178 mmc = find_mmc_device(dev);
179 if (!mmc) {
180 puts("No MMC card found\n");
181 ret = -ENODEV;
182 goto out;
183 }
Stefan Agnerdd202342019-07-12 12:35:05 +0200184 if (mmc_init(mmc)) {
185 puts("MMC init failed\n");
186 return -EINVAL;
187 }
Simon Glass8c4c5c82017-04-23 20:02:11 -0600188 if (part != mmc_get_blk_desc(mmc)->hwpart) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100189 if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) {
190 puts("MMC partition switch failed\n");
191 ret = -ENODEV;
192 goto out;
193 }
194 }
195 if (offset < 0)
196 offset += mmc->capacity;
197 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
198
199 if (!write) {
200 /* Careful reads a whole block of 512 bytes into config_block */
201 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
202 (unsigned char *)config_block) != 1) {
203 ret = -EIO;
204 goto out;
205 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100206 } else {
207 /* Just writing one 512 byte block */
208 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
209 (unsigned char *)config_block) != 1) {
210 ret = -EIO;
211 goto out;
212 }
213 }
214
215out:
216 /* Switch back to regular eMMC user partition */
217 blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
218
219 return ret;
220}
221#endif
222
223#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
224static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
225{
226 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
Stefan Agner8843b6d2018-08-06 09:19:18 +0200227 struct mtd_info *mtd = get_nand_dev_by_index(0);
228
229 if (!mtd)
230 return -ENODEV;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100231
232 /* Read production parameter config block from NAND page */
Stefan Agner8843b6d2018-08-06 09:19:18 +0200233 return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
Grygorii Strashkobb314622017-06-26 19:13:06 -0500234 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
235 config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100236}
237
238static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
239{
240 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
241
242 /* Write production parameter config block to NAND page */
Grygorii Strashkobb314622017-06-26 19:13:06 -0500243 return nand_write_skip_bad(get_nand_dev_by_index(0),
244 CONFIG_TDX_CFG_BLOCK_OFFSET,
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100245 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
246 config_block, WITH_WR_VERIFY);
247}
248#endif
249
250#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
251static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
252{
253 /* Read production parameter config block from NOR flash */
254 memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
255 TDX_CFG_BLOCK_MAX_SIZE);
256 return 0;
257}
258
259static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
260{
261 /* Write production parameter config block to NOR flash */
262 return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
263 TDX_CFG_BLOCK_MAX_SIZE);
264}
265#endif
266
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300267#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM
268static int read_tdx_cfg_block_from_eeprom(unsigned char *config_block)
269{
270 return read_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
271 TDX_CFG_BLOCK_MAX_SIZE);
272}
273
274static int write_tdx_cfg_block_to_eeprom(unsigned char *config_block)
275{
276 return write_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
277 TDX_CFG_BLOCK_MAX_SIZE);
278}
279#endif
280
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100281int read_tdx_cfg_block(void)
282{
283 int ret = 0;
284 u8 *config_block = NULL;
285 struct toradex_tag *tag;
286 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
287 int offset;
288
289 /* Allocate RAM area for config block */
290 config_block = memalign(ARCH_DMA_MINALIGN, size);
291 if (!config_block) {
292 printf("Not enough malloc space available!\n");
293 return -ENOMEM;
294 }
295
296 memset(config_block, 0, size);
297
298#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
299 ret = tdx_cfg_block_mmc_storage(config_block, 0);
300#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
301 ret = read_tdx_cfg_block_from_nand(config_block);
302#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
303 ret = read_tdx_cfg_block_from_nor(config_block);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300304#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
305 ret = read_tdx_cfg_block_from_eeprom(config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100306#else
307 ret = -EINVAL;
308#endif
309 if (ret)
310 goto out;
311
312 /* Expect a valid tag first */
313 tag = (struct toradex_tag *)config_block;
314 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
315 valid_cfgblock = false;
316 ret = -EINVAL;
317 goto out;
318 }
319 valid_cfgblock = true;
320 offset = 4;
321
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300322 /*
323 * check if there is enough space for storing tag and value of the
324 * biggest element
325 */
326 while (offset + sizeof(struct toradex_tag) +
327 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100328 tag = (struct toradex_tag *)(config_block + offset);
329 offset += 4;
330 if (tag->id == TAG_INVALID)
331 break;
332
333 if (tag->flags == TAG_FLAG_VALID) {
334 switch (tag->id) {
335 case TAG_MAC:
336 memcpy(&tdx_eth_addr, config_block + offset,
337 6);
338
339 /* NIC part of MAC address is serial number */
340 tdx_serial = ntohl(tdx_eth_addr.nic) >> 8;
341 break;
342 case TAG_HW:
343 memcpy(&tdx_hw_tag, config_block + offset, 8);
344 break;
345 }
346 }
347
348 /* Get to next tag according to current tags length */
349 offset += tag->len * 4;
350 }
351
352 /* Cap product id to avoid issues with a yet unknown one */
Marcel Ziswiler8c9127c2019-03-25 17:18:29 +0100353 if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) /
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100354 sizeof(toradex_modules[0])))
355 tdx_hw_tag.prodid = 0;
356
357out:
358 free(config_block);
359 return ret;
360}
361
362static int get_cfgblock_interactive(void)
363{
364 char message[CONFIG_SYS_CBSIZE];
365 char *soc;
366 char it = 'n';
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200367 char wb = 'n';
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100368 int len = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100369
Stefan Agner68f58782019-04-09 17:24:08 +0200370 /* Unknown module by default */
371 tdx_hw_tag.prodid = 0;
372
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100373 if (cpu_is_pxa27x())
374 sprintf(message, "Is the module the 312 MHz version? [y/N] ");
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100375 else
Denys Drozdov8f6eaa92021-10-06 18:55:33 +0200376 sprintf(message, "Is the module an IT version? [y/N] ");
377
378 len = cli_readline(message);
379 it = console_buffer[0];
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100380
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200381#if defined(CONFIG_TARGET_APALIS_IMX8) || \
Marcel Ziswiler75b93272020-01-28 14:42:23 +0100382 defined(CONFIG_TARGET_APALIS_IMX8X) || \
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200383 defined(CONFIG_TARGET_COLIBRI_IMX6ULL) || \
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200384 defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
385 defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
386 defined(CONFIG_TARGET_VERDIN_IMX8MP)
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200387 sprintf(message, "Does the module have Wi-Fi / Bluetooth? [y/N] ");
388 len = cli_readline(message);
389 wb = console_buffer[0];
390#endif
391
Simon Glass64b723f2017-08-03 12:22:12 -0600392 soc = env_get("soc");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100393 if (!strcmp("mx6", soc)) {
Stefan Agner68f58782019-04-09 17:24:08 +0200394#ifdef CONFIG_TARGET_APALIS_IMX6
395 if (it == 'y' || it == 'Y') {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100396 if (is_cpu_type(MXC_CPU_MX6Q))
397 tdx_hw_tag.prodid = APALIS_IMX6Q_IT;
398 else
399 tdx_hw_tag.prodid = APALIS_IMX6D_IT;
Stefan Agner68f58782019-04-09 17:24:08 +0200400 } else {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100401 if (is_cpu_type(MXC_CPU_MX6Q))
402 tdx_hw_tag.prodid = APALIS_IMX6Q;
403 else
404 tdx_hw_tag.prodid = APALIS_IMX6D;
Stefan Agner68f58782019-04-09 17:24:08 +0200405 }
406#elif CONFIG_TARGET_COLIBRI_IMX6
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200407 if (it == 'y' || it == 'Y') {
Stefan Agner68f58782019-04-09 17:24:08 +0200408 if (is_cpu_type(MXC_CPU_MX6DL))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100409 tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT;
Stefan Agner68f58782019-04-09 17:24:08 +0200410 else if (is_cpu_type(MXC_CPU_MX6SOLO))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100411 tdx_hw_tag.prodid = COLIBRI_IMX6S_IT;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200412 } else {
Stefan Agner68f58782019-04-09 17:24:08 +0200413 if (is_cpu_type(MXC_CPU_MX6DL))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100414 tdx_hw_tag.prodid = COLIBRI_IMX6DL;
Stefan Agner68f58782019-04-09 17:24:08 +0200415 else if (is_cpu_type(MXC_CPU_MX6SOLO))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100416 tdx_hw_tag.prodid = COLIBRI_IMX6S;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200417 }
Stefan Agner68f58782019-04-09 17:24:08 +0200418#elif CONFIG_TARGET_COLIBRI_IMX6ULL
Stefan Agner68f58782019-04-09 17:24:08 +0200419 if (it == 'y' || it == 'Y') {
420 if (wb == 'y' || wb == 'Y')
421 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT;
422 else
Marcel Ziswilerc066a442021-10-06 18:55:34 +0200423 if (gd->ram_size == 0x20000000)
424 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT;
425 else
426 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT_EMMC;
Stefan Agner68f58782019-04-09 17:24:08 +0200427 } else {
428 if (wb == 'y' || wb == 'Y')
429 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT;
430 else
431 tdx_hw_tag.prodid = COLIBRI_IMX6ULL;
432 }
433#endif
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200434 } else if (!strcmp("imx7d", soc))
Marcel Ziswilerc066a442021-10-06 18:55:34 +0200435 if (gd->ram_size == 0x20000000)
436 tdx_hw_tag.prodid = COLIBRI_IMX7D;
437 else
438 tdx_hw_tag.prodid = COLIBRI_IMX7D_EMMC;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200439 else if (!strcmp("imx7s", soc))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100440 tdx_hw_tag.prodid = COLIBRI_IMX7S;
Marcel Ziswiler00320612019-07-12 12:35:08 +0200441 else if (is_cpu_type(MXC_CPU_IMX8QM)) {
442 if (it == 'y' || it == 'Y') {
443 if (wb == 'y' || wb == 'Y')
444 tdx_hw_tag.prodid = APALIS_IMX8QM_WIFI_BT_IT;
445 else
446 tdx_hw_tag.prodid = APALIS_IMX8QM_IT;
447 } else {
448 if (wb == 'y' || wb == 'Y')
449 tdx_hw_tag.prodid = APALIS_IMX8QP_WIFI_BT;
450 else
451 tdx_hw_tag.prodid = APALIS_IMX8QP;
452 }
453 } else if (is_cpu_type(MXC_CPU_IMX8QXP)) {
Marcel Ziswiler75b93272020-01-28 14:42:23 +0100454#ifdef CONFIG_TARGET_APALIS_IMX8X
455 if (it == 'y' || it == 'Y' || wb == 'y' || wb == 'Y') {
456 tdx_hw_tag.prodid = APALIS_IMX8QXP_WIFI_BT_IT;
457 } else {
458 if (gd->ram_size == 0x40000000)
459 tdx_hw_tag.prodid = APALIS_IMX8DXP;
460 else
461 tdx_hw_tag.prodid = APALIS_IMX8QXP;
462 }
463#elif CONFIG_TARGET_COLIBRI_IMX8X
Marcel Ziswiler7e2954f2019-07-12 12:35:07 +0200464 if (it == 'y' || it == 'Y') {
465 if (wb == 'y' || wb == 'Y')
466 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT;
467 else
468 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_IT;
469 } else {
470 if (wb == 'y' || wb == 'Y')
471 tdx_hw_tag.prodid = COLIBRI_IMX8DX_WIFI_BT;
472 else
473 tdx_hw_tag.prodid = COLIBRI_IMX8DX;
474 }
Marcel Ziswiler75b93272020-01-28 14:42:23 +0100475#endif
Marcel Ziswiler3834f662020-10-28 11:58:15 +0200476 } else if (is_cpu_type(MXC_CPU_IMX8MMDL)) {
477 if (wb == 'y' || wb == 'Y')
478 tdx_hw_tag.prodid = VERDIN_IMX8MMDL_WIFI_BT_IT;
479 else
480 tdx_hw_tag.prodid = VERDIN_IMX8MMDL;
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200481 } else if (is_cpu_type(MXC_CPU_IMX8MM)) {
Marcel Ziswiler3834f662020-10-28 11:58:15 +0200482 if (wb == 'y' || wb == 'Y')
483 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_WIFI_BT_IT;
484 else
485 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_IT;
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200486 } else if (is_cpu_type(MXC_CPU_IMX8MN)) {
487 tdx_hw_tag.prodid = VERDIN_IMX8MNQ_WIFI_BT;
Marcel Ziswilerc066a442021-10-06 18:55:34 +0200488 } else if (is_cpu_type(MXC_CPU_IMX8MPL)) {
489 tdx_hw_tag.prodid = VERDIN_IMX8MPQL_IT;
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200490 } else if (is_cpu_type(MXC_CPU_IMX8MP)) {
491 if (wb == 'y' || wb == 'Y')
Marcel Ziswilerc066a442021-10-06 18:55:34 +0200492 if (gd->ram_size == 0x80000000)
493 tdx_hw_tag.prodid = VERDIN_IMX8MPQ_2GB_WIFI_BT_IT;
494 else if (gd->ram_size == 0x200000000)
495 tdx_hw_tag.prodid = VERDIN_IMX8MPQ_8GB_WIFI_BT;
496 else
497 tdx_hw_tag.prodid = VERDIN_IMX8MPQ_WIFI_BT_IT;
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200498 else
Marcel Ziswilerc066a442021-10-06 18:55:34 +0200499 if (it == 'y' || it == 'Y')
500 tdx_hw_tag.prodid = VERDIN_IMX8MPQ_IT;
501 else
502 tdx_hw_tag.prodid = VERDIN_IMX8MPQ;
Marcel Ziswiler7e2954f2019-07-12 12:35:07 +0200503 } else if (!strcmp("tegra20", soc)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100504 if (it == 'y' || it == 'Y')
505 if (gd->ram_size == 0x10000000)
506 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
507 else
508 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
509 else
510 if (gd->ram_size == 0x10000000)
511 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
512 else
513 tdx_hw_tag.prodid = COLIBRI_T20_512MB;
514 } else if (cpu_is_pxa27x()) {
515 if (it == 'y' || it == 'Y')
516 tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ;
517 else
518 tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200519 }
Tom Rinieb6aadc2021-08-30 09:16:32 -0400520#if defined(CONFIG_TARGET_APALIS_T30) || defined(CONFIG_TARGET_COLIBRI_T30)
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200521 else if (!strcmp("tegra30", soc)) {
Tom Rinieb6aadc2021-08-30 09:16:32 -0400522#ifdef CONFIG_TARGET_APALIS_T30
523 if (it == 'y' || it == 'Y')
524 tdx_hw_tag.prodid = APALIS_T30_IT;
525 else
526 if (gd->ram_size == 0x40000000)
527 tdx_hw_tag.prodid = APALIS_T30_1GB;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100528 else
Tom Rinieb6aadc2021-08-30 09:16:32 -0400529 tdx_hw_tag.prodid = APALIS_T30_2GB;
530#else
531 if (it == 'y' || it == 'Y')
532 tdx_hw_tag.prodid = COLIBRI_T30_IT;
533 else
534 tdx_hw_tag.prodid = COLIBRI_T30;
535#endif
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200536 }
Tom Rinieb6aadc2021-08-30 09:16:32 -0400537#endif /* CONFIG_TARGET_APALIS_T30 || CONFIG_TARGET_COLIBRI_T30 */
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200538 else if (!strcmp("tegra124", soc)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100539 tdx_hw_tag.prodid = APALIS_TK1_2GB;
540 } else if (!strcmp("vf500", soc)) {
541 if (it == 'y' || it == 'Y')
542 tdx_hw_tag.prodid = COLIBRI_VF50_IT;
543 else
544 tdx_hw_tag.prodid = COLIBRI_VF50;
545 } else if (!strcmp("vf610", soc)) {
546 if (it == 'y' || it == 'Y')
547 tdx_hw_tag.prodid = COLIBRI_VF61_IT;
548 else
549 tdx_hw_tag.prodid = COLIBRI_VF61;
Stefan Agner68f58782019-04-09 17:24:08 +0200550 }
551
552 if (!tdx_hw_tag.prodid) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100553 printf("Module type not detectable due to unknown SoC\n");
554 return -1;
555 }
556
557 while (len < 4) {
558 sprintf(message, "Enter the module version (e.g. V1.1B): V");
559 len = cli_readline(message);
560 }
561
562 tdx_hw_tag.ver_major = console_buffer[0] - '0';
563 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
564 tdx_hw_tag.ver_assembly = console_buffer[3] - 'A';
565
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200566 if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100567 tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
568 COLIBRI_PXA270_V1_312MHZ);
569
570 while (len < 8) {
571 sprintf(message, "Enter module serial number: ");
572 len = cli_readline(message);
573 }
574
Simon Glassff9b9032021-07-24 09:03:30 -0600575 tdx_serial = dectoul(console_buffer, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100576
577 return 0;
578}
579
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300580static int get_cfgblock_barcode(char *barcode, struct toradex_hw *tag,
581 u32 *serial)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100582{
Denys Drozdov3a1c6572021-04-07 15:28:24 +0300583 char revision[3] = {barcode[6], barcode[7], '\0'};
584
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100585 if (strlen(barcode) < 16) {
586 printf("Argument too short, barcode is 16 chars long\n");
587 return -1;
588 }
589
590 /* Get hardware information from the first 8 digits */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300591 tag->ver_major = barcode[4] - '0';
592 tag->ver_minor = barcode[5] - '0';
Simon Glassff9b9032021-07-24 09:03:30 -0600593 tag->ver_assembly = dectoul(revision, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100594
595 barcode[4] = '\0';
Simon Glassff9b9032021-07-24 09:03:30 -0600596 tag->prodid = dectoul(barcode, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100597
598 /* Parse second part of the barcode (serial number */
599 barcode += 8;
Simon Glassff9b9032021-07-24 09:03:30 -0600600 *serial = dectoul(barcode, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100601
602 return 0;
603}
604
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300605static int write_tag(u8 *config_block, int *offset, int tag_id,
606 u8 *tag_data, size_t tag_data_size)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100607{
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100608 struct toradex_tag *tag;
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300609
610 if (!offset || !config_block)
611 return -EINVAL;
612
613 tag = (struct toradex_tag *)(config_block + *offset);
614 tag->id = tag_id;
615 tag->flags = TAG_FLAG_VALID;
616 /* len is provided as number of 32bit values after the tag */
617 tag->len = (tag_data_size + sizeof(u32) - 1) / sizeof(u32);
618 *offset += sizeof(struct toradex_tag);
619 if (tag_data && tag_data_size) {
620 memcpy(config_block + *offset, tag_data,
621 tag_data_size);
622 *offset += tag_data_size;
623 }
624
625 return 0;
626}
627
628#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
629int read_tdx_cfg_block_carrier(void)
630{
631 int ret = 0;
632 u8 *config_block = NULL;
633 struct toradex_tag *tag;
634 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
635 int offset;
636
637 /* Allocate RAM area for carrier config block */
638 config_block = memalign(ARCH_DMA_MINALIGN, size);
639 if (!config_block) {
640 printf("Not enough malloc space available!\n");
641 return -ENOMEM;
642 }
643
644 memset(config_block, 0, size);
645
646 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
647 size);
648 if (ret)
649 return ret;
650
651 /* Expect a valid tag first */
652 tag = (struct toradex_tag *)config_block;
653 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
654 valid_cfgblock_carrier = false;
655 ret = -EINVAL;
656 goto out;
657 }
658 valid_cfgblock_carrier = true;
659 offset = 4;
660
661 while (offset + sizeof(struct toradex_tag) +
662 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
663 tag = (struct toradex_tag *)(config_block + offset);
664 offset += 4;
665 if (tag->id == TAG_INVALID)
666 break;
667
668 if (tag->flags == TAG_FLAG_VALID) {
669 switch (tag->id) {
670 case TAG_CAR_SERIAL:
671 memcpy(&tdx_car_serial, config_block + offset,
672 sizeof(tdx_car_serial));
673 break;
674 case TAG_HW:
675 memcpy(&tdx_car_hw_tag, config_block +
676 offset, 8);
677 break;
678 }
679 }
680
681 /* Get to next tag according to current tags length */
682 offset += tag->len * 4;
683 }
684out:
685 free(config_block);
686 return ret;
687}
688
Igor Opaniuke9ad67a2020-07-15 13:30:56 +0300689int check_pid8_sanity(char *pid8)
690{
691 char s_carrierid_verdin_dev[5];
692 char s_carrierid_dahlia[5];
693
694 sprintf(s_carrierid_verdin_dev, "0%d", VERDIN_DEVELOPMENT_BOARD);
695 sprintf(s_carrierid_dahlia, "0%d", DAHLIA);
696
697 /* sane value check, first 4 chars which represent carrier id */
698 if (!strncmp(pid8, s_carrierid_verdin_dev, 4))
699 return 0;
700
701 if (!strncmp(pid8, s_carrierid_dahlia, 4))
702 return 0;
703
704 return -EINVAL;
705}
706
707int try_migrate_tdx_cfg_block_carrier(void)
708{
709 char pid8[8];
710 int offset = 0;
711 int ret = CMD_RET_SUCCESS;
712 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
713 u8 *config_block;
714
715 memset(pid8, 0x0, 8);
716 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, (u8 *)pid8, 8);
717 if (ret)
718 return ret;
719
720 if (check_pid8_sanity(pid8))
721 return -EINVAL;
722
723 /* Allocate RAM area for config block */
724 config_block = memalign(ARCH_DMA_MINALIGN, size);
725 if (!config_block) {
726 printf("Not enough malloc space available!\n");
727 return CMD_RET_FAILURE;
728 }
729
730 memset(config_block, 0xff, size);
731 /* we try parse PID8 concatenating zeroed serial number */
732 tdx_car_hw_tag.ver_major = pid8[4] - '0';
733 tdx_car_hw_tag.ver_minor = pid8[5] - '0';
734 tdx_car_hw_tag.ver_assembly = pid8[7] - '0';
735
736 pid8[4] = '\0';
Simon Glassff9b9032021-07-24 09:03:30 -0600737 tdx_car_hw_tag.prodid = dectoul(pid8, NULL);
Igor Opaniuke9ad67a2020-07-15 13:30:56 +0300738
739 /* Valid Tag */
740 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
741
742 /* Product Tag */
743 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
744 sizeof(tdx_car_hw_tag));
745
746 /* Serial Tag */
747 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
748 sizeof(tdx_car_serial));
749
750 memset(config_block + offset, 0, 32 - offset);
751 ret = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
752 size);
753 if (ret) {
754 printf("Failed to write Toradex Extra config block: %d\n",
755 ret);
756 ret = CMD_RET_FAILURE;
757 goto out;
758 }
759
760 printf("Successfully migrated to Toradex Config Block from PID8\n");
761
762out:
763 free(config_block);
764 return ret;
765}
766
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300767static int get_cfgblock_carrier_interactive(void)
768{
769 char message[CONFIG_SYS_CBSIZE];
770 int len;
771
772 printf("Supported carrier boards:\n");
773 printf("CARRIER BOARD NAME\t\t [ID]\n");
774 for (int i = 0; i < sizeof(toradex_carrier_boards) /
775 sizeof(toradex_carrier_boards[0]); i++)
776 if (toradex_carrier_boards[i])
777 printf("%s \t\t [%d]\n", toradex_carrier_boards[i], i);
778
779 sprintf(message, "Choose your carrier board (provide ID): ");
780 len = cli_readline(message);
Simon Glassff9b9032021-07-24 09:03:30 -0600781 tdx_car_hw_tag.prodid = dectoul(console_buffer, NULL);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300782
783 do {
784 sprintf(message, "Enter carrier board version (e.g. V1.1B): V");
785 len = cli_readline(message);
786 } while (len < 4);
787
788 tdx_car_hw_tag.ver_major = console_buffer[0] - '0';
789 tdx_car_hw_tag.ver_minor = console_buffer[2] - '0';
790 tdx_car_hw_tag.ver_assembly = console_buffer[3] - 'A';
791
792 while (len < 8) {
793 sprintf(message, "Enter carrier board serial number: ");
794 len = cli_readline(message);
795 }
796
Simon Glassff9b9032021-07-24 09:03:30 -0600797 tdx_car_serial = dectoul(console_buffer, NULL);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300798
799 return 0;
800}
801
802static int do_cfgblock_carrier_create(struct cmd_tbl *cmdtp, int flag, int argc,
803 char * const argv[])
804{
805 u8 *config_block;
806 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100807 int offset = 0;
808 int ret = CMD_RET_SUCCESS;
809 int err;
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100810 int force_overwrite = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100811
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300812 if (argc >= 3) {
813 if (argv[2][0] == '-' && argv[2][1] == 'y')
814 force_overwrite = 1;
815 }
816
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100817 /* Allocate RAM area for config block */
818 config_block = memalign(ARCH_DMA_MINALIGN, size);
819 if (!config_block) {
820 printf("Not enough malloc space available!\n");
821 return CMD_RET_FAILURE;
822 }
823
824 memset(config_block, 0xff, size);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300825 read_tdx_cfg_block_carrier();
826 if (valid_cfgblock_carrier && !force_overwrite) {
827 char message[CONFIG_SYS_CBSIZE];
828
829 sprintf(message, "A valid Toradex Carrier config block is present, still recreate? [y/N] ");
830
831 if (!cli_readline(message))
832 goto out;
833
834 if (console_buffer[0] != 'y' &&
835 console_buffer[0] != 'Y')
836 goto out;
837 }
838
839 if (argc < 3 || (force_overwrite && argc < 4)) {
840 err = get_cfgblock_carrier_interactive();
841 } else {
842 if (force_overwrite)
843 err = get_cfgblock_barcode(argv[3], &tdx_car_hw_tag,
844 &tdx_car_serial);
845 else
846 err = get_cfgblock_barcode(argv[2], &tdx_car_hw_tag,
847 &tdx_car_serial);
848 }
849
850 if (err) {
851 ret = CMD_RET_FAILURE;
852 goto out;
853 }
854
855 /* Valid Tag */
856 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
857
858 /* Product Tag */
859 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
860 sizeof(tdx_car_hw_tag));
861
862 /* Serial Tag */
863 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
864 sizeof(tdx_car_serial));
865
866 memset(config_block + offset, 0, 32 - offset);
867 err = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
868 size);
869 if (err) {
870 printf("Failed to write Toradex Extra config block: %d\n",
871 ret);
872 ret = CMD_RET_FAILURE;
873 goto out;
874 }
875
876 printf("Toradex Extra config block successfully written\n");
877
878out:
879 free(config_block);
880 return ret;
881}
882
883#endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
884
885static int do_cfgblock_create(struct cmd_tbl *cmdtp, int flag, int argc,
886 char * const argv[])
887{
888 u8 *config_block;
889 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
890 int offset = 0;
891 int ret = CMD_RET_SUCCESS;
892 int err;
893 int force_overwrite = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100894
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100895 if (argc >= 3) {
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300896#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
897 if (!strcmp(argv[2], "carrier"))
898 return do_cfgblock_carrier_create(cmdtp, flag,
899 --argc, ++argv);
900#endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100901 if (argv[2][0] == '-' && argv[2][1] == 'y')
902 force_overwrite = 1;
903 }
904
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300905 /* Allocate RAM area for config block */
906 config_block = memalign(ARCH_DMA_MINALIGN, size);
907 if (!config_block) {
908 printf("Not enough malloc space available!\n");
909 return CMD_RET_FAILURE;
910 }
911
912 memset(config_block, 0xff, size);
913
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100914 read_tdx_cfg_block();
915 if (valid_cfgblock) {
916#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
917 /*
918 * On NAND devices, recreation is only allowed if the page is
919 * empty (config block invalid...)
920 */
Marcel Ziswiler6cf2f782019-07-12 12:35:09 +0200921 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
Grygorii Strashkobb314622017-06-26 19:13:06 -0500922 CONFIG_TDX_CFG_BLOCK_OFFSET /
923 get_nand_dev_by_index(0)->erasesize);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100924 goto out;
925#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
926 /*
927 * On NOR devices, recreation is only allowed if the sector is
928 * empty and write protection is off (config block invalid...)
929 */
Marcel Ziswiler6cf2f782019-07-12 12:35:09 +0200930 printf("NOR sector at offset 0x%02x need to be erased and unprotected before creating a Toradex config block\n",
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100931 CONFIG_TDX_CFG_BLOCK_OFFSET);
932 goto out;
933#else
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100934 if (!force_overwrite) {
935 char message[CONFIG_SYS_CBSIZE];
936
937 sprintf(message,
938 "A valid Toradex config block is present, still recreate? [y/N] ");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100939
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100940 if (!cli_readline(message))
941 goto out;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100942
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100943 if (console_buffer[0] != 'y' &&
944 console_buffer[0] != 'Y')
945 goto out;
946 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100947#endif
948 }
949
950 /* Parse new Toradex config block data... */
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100951 if (argc < 3 || (force_overwrite && argc < 4)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100952 err = get_cfgblock_interactive();
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100953 } else {
954 if (force_overwrite)
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300955 err = get_cfgblock_barcode(argv[3], &tdx_hw_tag,
956 &tdx_serial);
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100957 else
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300958 err = get_cfgblock_barcode(argv[2], &tdx_hw_tag,
959 &tdx_serial);
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100960 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100961 if (err) {
962 ret = CMD_RET_FAILURE;
963 goto out;
964 }
965
966 /* Convert serial number to MAC address (the storage format) */
967 tdx_eth_addr.oui = htonl(0x00142dUL << 8);
968 tdx_eth_addr.nic = htonl(tdx_serial << 8);
969
970 /* Valid Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300971 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100972
973 /* Product Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300974 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_hw_tag,
975 sizeof(tdx_hw_tag));
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100976
977 /* MAC Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300978 write_tag(config_block, &offset, TAG_MAC, (u8 *)&tdx_eth_addr,
979 sizeof(tdx_eth_addr));
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100980
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100981 memset(config_block + offset, 0, 32 - offset);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100982#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
983 err = tdx_cfg_block_mmc_storage(config_block, 1);
984#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
985 err = write_tdx_cfg_block_to_nand(config_block);
986#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
987 err = write_tdx_cfg_block_to_nor(config_block);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300988#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
989 err = write_tdx_cfg_block_to_eeprom(config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100990#else
991 err = -EINVAL;
992#endif
993 if (err) {
994 printf("Failed to write Toradex config block: %d\n", ret);
995 ret = CMD_RET_FAILURE;
996 goto out;
997 }
998
999 printf("Toradex config block successfully written\n");
1000
1001out:
1002 free(config_block);
1003 return ret;
1004}
1005
Simon Glassed38aef2020-05-10 11:40:03 -06001006static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc,
1007 char *const argv[])
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01001008{
1009 int ret;
1010
1011 if (argc < 2)
1012 return CMD_RET_USAGE;
1013
1014 if (!strcmp(argv[1], "create")) {
1015 return do_cfgblock_create(cmdtp, flag, argc, argv);
1016 } else if (!strcmp(argv[1], "reload")) {
1017 ret = read_tdx_cfg_block();
1018 if (ret) {
1019 printf("Failed to reload Toradex config block: %d\n",
1020 ret);
1021 return CMD_RET_FAILURE;
1022 }
1023 return CMD_RET_SUCCESS;
1024 }
1025
1026 return CMD_RET_USAGE;
1027}
1028
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +03001029U_BOOT_CMD(
1030 cfgblock, 5, 0, do_cfgblock,
1031 "Toradex config block handling commands",
1032 "create [-y] [barcode] - (Re-)create Toradex config block\n"
1033 "create carrier [-y] [barcode] - (Re-)create Toradex Carrier config block\n"
1034 "cfgblock reload - Reload Toradex config block from flash"
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01001035);