1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2016-2020 Toradex
7 #include <asm/global_data.h>
8 #include "tdx-cfg-block.h"
9 #include "tdx-eeprom.h"
12 #include <asm/cache.h>
14 #if defined(CONFIG_TARGET_APALIS_IMX6) || \
15 defined(CONFIG_TARGET_APALIS_IMX8) || \
16 defined(CONFIG_TARGET_APALIS_IMX8X) || \
17 defined(CONFIG_TARGET_COLIBRI_IMX6) || \
18 defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
19 defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
20 defined(CONFIG_TARGET_VERDIN_IMX8MN) || \
21 defined(CONFIG_TARGET_VERDIN_IMX8MP)
22 #include <asm/arch/sys_proto.h>
24 #define is_cpu_type(cpu) (0)
26 #if defined(CONFIG_CPU_PXA27X)
27 #include <asm/arch-pxa/pxa.h>
29 #define cpu_is_pxa27x(cpu) (0)
38 #include <asm/mach-types.h>
40 DECLARE_GLOBAL_DATA_PTR;
42 #define TAG_VALID 0xcf01
43 #define TAG_MAC 0x0000
44 #define TAG_CAR_SERIAL 0x0021
46 #define TAG_INVALID 0xffff
48 #define TAG_FLAG_VALID 0x1
50 #define TDX_EEPROM_ID_MODULE 0
51 #define TDX_EEPROM_ID_CARRIER 1
53 #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
59 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
60 #define TDX_CFG_BLOCK_MAX_SIZE 64
62 #error Toradex config block location not set
65 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
66 #define TDX_CFG_BLOCK_EXTRA_MAX_SIZE 64
76 struct toradex_hw tdx_hw_tag;
77 struct toradex_eth_addr tdx_eth_addr;
79 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
81 bool valid_cfgblock_carrier;
82 struct toradex_hw tdx_car_hw_tag;
85 const 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",
122 [36] = "Colibri iMX6ULL 256MB",
123 [37] = "Apalis iMX8 QuadMax 4GB Wi-Fi / BT IT",
124 [38] = "Colibri iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
125 [39] = "Colibri iMX7 Dual 1GB (eMMC)",
126 [40] = "Colibri iMX6ULL 512MB Wi-Fi / BT IT",
127 [41] = "Colibri iMX7 Dual 512MB EPDC",
128 [42] = "Apalis TK1 4GB",
129 [43] = "Colibri T20 512MB IT SETEK",
130 [44] = "Colibri iMX6ULL 512MB IT",
131 [45] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth",
132 [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",
139 [53] = "Apalis iMX8 QuadXPlus 2GB ECC IT",
140 [54] = "Apalis iMX8 DualXPlus 1GB",
141 [55] = "Verdin iMX8M Mini Quad 2GB Wi-Fi / BT IT",
142 [56] = "Verdin iMX8M Nano Quad 1GB Wi-Fi / BT", /* not currently on sale */
143 [57] = "Verdin iMX8M Mini DualLite 1GB",
144 [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",
148 [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",
155 const char * const toradex_carrier_boards[] = {
156 [0] = "UNKNOWN CARRIER BOARD",
158 [156] = "Verdin Development Board",
161 const char * const toradex_display_adapters[] = {
162 [0] = "UNKNOWN DISPLAY ADAPTER",
163 [157] = "Verdin DSI to HDMI Adapter",
164 [159] = "Verdin DSI to LVDS Adapter",
167 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
168 static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
171 int dev = CONFIG_TDX_CFG_BLOCK_DEV;
172 int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
173 uint part = CONFIG_TDX_CFG_BLOCK_PART;
177 /* Read production parameter config block from eMMC */
178 mmc = find_mmc_device(dev);
180 puts("No MMC card found\n");
185 puts("MMC init failed\n");
188 if (part != mmc_get_blk_desc(mmc)->hwpart) {
189 if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) {
190 puts("MMC partition switch failed\n");
196 offset += mmc->capacity;
197 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
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) {
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) {
216 /* Switch back to regular eMMC user partition */
217 blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
223 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
224 static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
226 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
227 struct mtd_info *mtd = get_nand_dev_by_index(0);
232 /* Read production parameter config block from NAND page */
233 return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
234 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
238 static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
240 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
242 /* Write production parameter config block to NAND page */
243 return nand_write_skip_bad(get_nand_dev_by_index(0),
244 CONFIG_TDX_CFG_BLOCK_OFFSET,
245 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
246 config_block, WITH_WR_VERIFY);
250 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
251 static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
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);
259 static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
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);
267 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM
268 static int read_tdx_cfg_block_from_eeprom(unsigned char *config_block)
270 return read_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
271 TDX_CFG_BLOCK_MAX_SIZE);
274 static int write_tdx_cfg_block_to_eeprom(unsigned char *config_block)
276 return write_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
277 TDX_CFG_BLOCK_MAX_SIZE);
281 int read_tdx_cfg_block(void)
284 u8 *config_block = NULL;
285 struct toradex_tag *tag;
286 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
289 /* Allocate RAM area for config block */
290 config_block = memalign(ARCH_DMA_MINALIGN, size);
292 printf("Not enough malloc space available!\n");
296 memset(config_block, 0, size);
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);
304 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
305 ret = read_tdx_cfg_block_from_eeprom(config_block);
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;
319 valid_cfgblock = true;
323 * check if there is enough space for storing tag and value of the
326 while (offset + sizeof(struct toradex_tag) +
327 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
328 tag = (struct toradex_tag *)(config_block + offset);
330 if (tag->id == TAG_INVALID)
333 if (tag->flags == TAG_FLAG_VALID) {
336 memcpy(&tdx_eth_addr, config_block + offset,
339 /* NIC part of MAC address is serial number */
340 tdx_serial = ntohl(tdx_eth_addr.nic) >> 8;
343 memcpy(&tdx_hw_tag, config_block + offset, 8);
348 /* Get to next tag according to current tags length */
349 offset += tag->len * 4;
352 /* Cap product id to avoid issues with a yet unknown one */
353 if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) /
354 sizeof(toradex_modules[0])))
355 tdx_hw_tag.prodid = 0;
362 static int get_cfgblock_interactive(void)
364 char message[CONFIG_SYS_CBSIZE];
370 /* Unknown module by default */
371 tdx_hw_tag.prodid = 0;
374 sprintf(message, "Is the module the 312 MHz version? [y/N] ");
376 sprintf(message, "Is the module an IT version? [y/N] ");
378 len = cli_readline(message);
379 it = console_buffer[0];
381 #if defined(CONFIG_TARGET_APALIS_IMX8) || \
382 defined(CONFIG_TARGET_APALIS_IMX8X) || \
383 defined(CONFIG_TARGET_COLIBRI_IMX6ULL) || \
384 defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
385 defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
386 defined(CONFIG_TARGET_VERDIN_IMX8MP)
387 sprintf(message, "Does the module have Wi-Fi / Bluetooth? [y/N] ");
388 len = cli_readline(message);
389 wb = console_buffer[0];
392 soc = env_get("soc");
393 if (!strcmp("mx6", soc)) {
394 #ifdef CONFIG_TARGET_APALIS_IMX6
395 if (it == 'y' || it == 'Y') {
396 if (is_cpu_type(MXC_CPU_MX6Q))
397 tdx_hw_tag.prodid = APALIS_IMX6Q_IT;
399 tdx_hw_tag.prodid = APALIS_IMX6D_IT;
401 if (is_cpu_type(MXC_CPU_MX6Q))
402 tdx_hw_tag.prodid = APALIS_IMX6Q;
404 tdx_hw_tag.prodid = APALIS_IMX6D;
406 #elif CONFIG_TARGET_COLIBRI_IMX6
407 if (it == 'y' || it == 'Y') {
408 if (is_cpu_type(MXC_CPU_MX6DL))
409 tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT;
410 else if (is_cpu_type(MXC_CPU_MX6SOLO))
411 tdx_hw_tag.prodid = COLIBRI_IMX6S_IT;
413 if (is_cpu_type(MXC_CPU_MX6DL))
414 tdx_hw_tag.prodid = COLIBRI_IMX6DL;
415 else if (is_cpu_type(MXC_CPU_MX6SOLO))
416 tdx_hw_tag.prodid = COLIBRI_IMX6S;
418 #elif CONFIG_TARGET_COLIBRI_IMX6ULL
419 if (it == 'y' || it == 'Y') {
420 if (wb == 'y' || wb == 'Y')
421 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT;
423 if (gd->ram_size == 0x20000000)
424 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT;
426 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT_EMMC;
428 if (wb == 'y' || wb == 'Y')
429 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT;
431 tdx_hw_tag.prodid = COLIBRI_IMX6ULL;
434 } else if (!strcmp("imx7d", soc))
435 if (gd->ram_size == 0x20000000)
436 tdx_hw_tag.prodid = COLIBRI_IMX7D;
438 tdx_hw_tag.prodid = COLIBRI_IMX7D_EMMC;
439 else if (!strcmp("imx7s", soc))
440 tdx_hw_tag.prodid = COLIBRI_IMX7S;
441 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;
446 tdx_hw_tag.prodid = APALIS_IMX8QM_IT;
448 if (wb == 'y' || wb == 'Y')
449 tdx_hw_tag.prodid = APALIS_IMX8QP_WIFI_BT;
451 tdx_hw_tag.prodid = APALIS_IMX8QP;
453 } else if (is_cpu_type(MXC_CPU_IMX8QXP)) {
454 #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;
458 if (gd->ram_size == 0x40000000)
459 tdx_hw_tag.prodid = APALIS_IMX8DXP;
461 tdx_hw_tag.prodid = APALIS_IMX8QXP;
463 #elif CONFIG_TARGET_COLIBRI_IMX8X
464 if (it == 'y' || it == 'Y') {
465 if (wb == 'y' || wb == 'Y')
466 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT;
468 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_IT;
470 if (wb == 'y' || wb == 'Y')
471 tdx_hw_tag.prodid = COLIBRI_IMX8DX_WIFI_BT;
473 tdx_hw_tag.prodid = COLIBRI_IMX8DX;
476 } else if (is_cpu_type(MXC_CPU_IMX8MMDL)) {
477 if (wb == 'y' || wb == 'Y')
478 tdx_hw_tag.prodid = VERDIN_IMX8MMDL_WIFI_BT_IT;
480 tdx_hw_tag.prodid = VERDIN_IMX8MMDL;
481 } else if (is_cpu_type(MXC_CPU_IMX8MM)) {
482 if (wb == 'y' || wb == 'Y')
483 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_WIFI_BT_IT;
485 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_IT;
486 } else if (is_cpu_type(MXC_CPU_IMX8MN)) {
487 tdx_hw_tag.prodid = VERDIN_IMX8MNQ_WIFI_BT;
488 } else if (is_cpu_type(MXC_CPU_IMX8MPL)) {
489 tdx_hw_tag.prodid = VERDIN_IMX8MPQL_IT;
490 } else if (is_cpu_type(MXC_CPU_IMX8MP)) {
491 if (wb == 'y' || wb == 'Y')
492 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;
497 tdx_hw_tag.prodid = VERDIN_IMX8MPQ_WIFI_BT_IT;
499 if (it == 'y' || it == 'Y')
500 tdx_hw_tag.prodid = VERDIN_IMX8MPQ_IT;
502 tdx_hw_tag.prodid = VERDIN_IMX8MPQ;
503 } else if (!strcmp("tegra20", soc)) {
504 if (it == 'y' || it == 'Y')
505 if (gd->ram_size == 0x10000000)
506 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
508 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
510 if (gd->ram_size == 0x10000000)
511 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
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;
518 tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
520 #if defined(CONFIG_TARGET_APALIS_T30) || defined(CONFIG_TARGET_COLIBRI_T30)
521 else if (!strcmp("tegra30", soc)) {
522 #ifdef CONFIG_TARGET_APALIS_T30
523 if (it == 'y' || it == 'Y')
524 tdx_hw_tag.prodid = APALIS_T30_IT;
526 if (gd->ram_size == 0x40000000)
527 tdx_hw_tag.prodid = APALIS_T30_1GB;
529 tdx_hw_tag.prodid = APALIS_T30_2GB;
531 if (it == 'y' || it == 'Y')
532 tdx_hw_tag.prodid = COLIBRI_T30_IT;
534 tdx_hw_tag.prodid = COLIBRI_T30;
537 #endif /* CONFIG_TARGET_APALIS_T30 || CONFIG_TARGET_COLIBRI_T30 */
538 else if (!strcmp("tegra124", soc)) {
539 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;
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;
549 tdx_hw_tag.prodid = COLIBRI_VF61;
552 if (!tdx_hw_tag.prodid) {
553 printf("Module type not detectable due to unknown SoC\n");
558 sprintf(message, "Enter the module version (e.g. V1.1B): V");
559 len = cli_readline(message);
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';
566 if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1)
567 tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
568 COLIBRI_PXA270_V1_312MHZ);
571 sprintf(message, "Enter module serial number: ");
572 len = cli_readline(message);
575 tdx_serial = dectoul(console_buffer, NULL);
580 static int get_cfgblock_barcode(char *barcode, struct toradex_hw *tag,
583 char revision[3] = {barcode[6], barcode[7], '\0'};
585 if (strlen(barcode) < 16) {
586 printf("Argument too short, barcode is 16 chars long\n");
590 /* Get hardware information from the first 8 digits */
591 tag->ver_major = barcode[4] - '0';
592 tag->ver_minor = barcode[5] - '0';
593 tag->ver_assembly = dectoul(revision, NULL);
596 tag->prodid = dectoul(barcode, NULL);
598 /* Parse second part of the barcode (serial number */
600 *serial = dectoul(barcode, NULL);
605 static int write_tag(u8 *config_block, int *offset, int tag_id,
606 u8 *tag_data, size_t tag_data_size)
608 struct toradex_tag *tag;
610 if (!offset || !config_block)
613 tag = (struct toradex_tag *)(config_block + *offset);
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,
622 *offset += tag_data_size;
628 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
629 int read_tdx_cfg_block_carrier(void)
632 u8 *config_block = NULL;
633 struct toradex_tag *tag;
634 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
637 /* Allocate RAM area for carrier config block */
638 config_block = memalign(ARCH_DMA_MINALIGN, size);
640 printf("Not enough malloc space available!\n");
644 memset(config_block, 0, size);
646 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
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;
658 valid_cfgblock_carrier = true;
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);
665 if (tag->id == TAG_INVALID)
668 if (tag->flags == TAG_FLAG_VALID) {
671 memcpy(&tdx_car_serial, config_block + offset,
672 sizeof(tdx_car_serial));
675 memcpy(&tdx_car_hw_tag, config_block +
681 /* Get to next tag according to current tags length */
682 offset += tag->len * 4;
689 int check_pid8_sanity(char *pid8)
691 char s_carrierid_verdin_dev[5];
692 char s_carrierid_dahlia[5];
694 sprintf(s_carrierid_verdin_dev, "0%d", VERDIN_DEVELOPMENT_BOARD);
695 sprintf(s_carrierid_dahlia, "0%d", DAHLIA);
697 /* sane value check, first 4 chars which represent carrier id */
698 if (!strncmp(pid8, s_carrierid_verdin_dev, 4))
701 if (!strncmp(pid8, s_carrierid_dahlia, 4))
707 int try_migrate_tdx_cfg_block_carrier(void)
711 int ret = CMD_RET_SUCCESS;
712 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
715 memset(pid8, 0x0, 8);
716 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, (u8 *)pid8, 8);
720 if (check_pid8_sanity(pid8))
723 /* Allocate RAM area for config block */
724 config_block = memalign(ARCH_DMA_MINALIGN, size);
726 printf("Not enough malloc space available!\n");
727 return CMD_RET_FAILURE;
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';
737 tdx_car_hw_tag.prodid = dectoul(pid8, NULL);
740 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
743 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
744 sizeof(tdx_car_hw_tag));
747 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
748 sizeof(tdx_car_serial));
750 memset(config_block + offset, 0, 32 - offset);
751 ret = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
754 printf("Failed to write Toradex Extra config block: %d\n",
756 ret = CMD_RET_FAILURE;
760 printf("Successfully migrated to Toradex Config Block from PID8\n");
767 static int get_cfgblock_carrier_interactive(void)
769 char message[CONFIG_SYS_CBSIZE];
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);
779 sprintf(message, "Choose your carrier board (provide ID): ");
780 len = cli_readline(message);
781 tdx_car_hw_tag.prodid = dectoul(console_buffer, NULL);
784 sprintf(message, "Enter carrier board version (e.g. V1.1B): V");
785 len = cli_readline(message);
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';
793 sprintf(message, "Enter carrier board serial number: ");
794 len = cli_readline(message);
797 tdx_car_serial = dectoul(console_buffer, NULL);
802 static int do_cfgblock_carrier_create(struct cmd_tbl *cmdtp, int flag, int argc,
806 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
808 int ret = CMD_RET_SUCCESS;
810 int force_overwrite = 0;
813 if (argv[2][0] == '-' && argv[2][1] == 'y')
817 /* Allocate RAM area for config block */
818 config_block = memalign(ARCH_DMA_MINALIGN, size);
820 printf("Not enough malloc space available!\n");
821 return CMD_RET_FAILURE;
824 memset(config_block, 0xff, size);
825 read_tdx_cfg_block_carrier();
826 if (valid_cfgblock_carrier && !force_overwrite) {
827 char message[CONFIG_SYS_CBSIZE];
829 sprintf(message, "A valid Toradex Carrier config block is present, still recreate? [y/N] ");
831 if (!cli_readline(message))
834 if (console_buffer[0] != 'y' &&
835 console_buffer[0] != 'Y')
839 if (argc < 3 || (force_overwrite && argc < 4)) {
840 err = get_cfgblock_carrier_interactive();
843 err = get_cfgblock_barcode(argv[3], &tdx_car_hw_tag,
846 err = get_cfgblock_barcode(argv[2], &tdx_car_hw_tag,
851 ret = CMD_RET_FAILURE;
856 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
859 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
860 sizeof(tdx_car_hw_tag));
863 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
864 sizeof(tdx_car_serial));
866 memset(config_block + offset, 0, 32 - offset);
867 err = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
870 printf("Failed to write Toradex Extra config block: %d\n",
872 ret = CMD_RET_FAILURE;
876 printf("Toradex Extra config block successfully written\n");
883 #endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
885 static int do_cfgblock_create(struct cmd_tbl *cmdtp, int flag, int argc,
889 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
891 int ret = CMD_RET_SUCCESS;
893 int force_overwrite = 0;
896 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
897 if (!strcmp(argv[2], "carrier"))
898 return do_cfgblock_carrier_create(cmdtp, flag,
900 #endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
901 if (argv[2][0] == '-' && argv[2][1] == 'y')
905 /* Allocate RAM area for config block */
906 config_block = memalign(ARCH_DMA_MINALIGN, size);
908 printf("Not enough malloc space available!\n");
909 return CMD_RET_FAILURE;
912 memset(config_block, 0xff, size);
914 read_tdx_cfg_block();
915 if (valid_cfgblock) {
916 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
918 * On NAND devices, recreation is only allowed if the page is
919 * empty (config block invalid...)
921 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
922 CONFIG_TDX_CFG_BLOCK_OFFSET /
923 get_nand_dev_by_index(0)->erasesize);
925 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
927 * On NOR devices, recreation is only allowed if the sector is
928 * empty and write protection is off (config block invalid...)
930 printf("NOR sector at offset 0x%02x need to be erased and unprotected before creating a Toradex config block\n",
931 CONFIG_TDX_CFG_BLOCK_OFFSET);
934 if (!force_overwrite) {
935 char message[CONFIG_SYS_CBSIZE];
938 "A valid Toradex config block is present, still recreate? [y/N] ");
940 if (!cli_readline(message))
943 if (console_buffer[0] != 'y' &&
944 console_buffer[0] != 'Y')
950 /* Parse new Toradex config block data... */
951 if (argc < 3 || (force_overwrite && argc < 4)) {
952 err = get_cfgblock_interactive();
955 err = get_cfgblock_barcode(argv[3], &tdx_hw_tag,
958 err = get_cfgblock_barcode(argv[2], &tdx_hw_tag,
962 ret = CMD_RET_FAILURE;
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);
971 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
974 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_hw_tag,
978 write_tag(config_block, &offset, TAG_MAC, (u8 *)&tdx_eth_addr,
979 sizeof(tdx_eth_addr));
981 memset(config_block + offset, 0, 32 - offset);
982 #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);
988 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
989 err = write_tdx_cfg_block_to_eeprom(config_block);
994 printf("Failed to write Toradex config block: %d\n", ret);
995 ret = CMD_RET_FAILURE;
999 printf("Toradex config block successfully written\n");
1006 static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc,
1012 return CMD_RET_USAGE;
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();
1019 printf("Failed to reload Toradex config block: %d\n",
1021 return CMD_RET_FAILURE;
1023 return CMD_RET_SUCCESS;
1026 return CMD_RET_USAGE;
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"