1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2016-2020 Toradex
7 #include "tdx-cfg-block.h"
9 #if defined(CONFIG_TARGET_APALIS_IMX6) || \
10 defined(CONFIG_TARGET_APALIS_IMX8) || \
11 defined(CONFIG_TARGET_APALIS_IMX8X) || \
12 defined(CONFIG_TARGET_COLIBRI_IMX6) || \
13 defined(CONFIG_TARGET_COLIBRI_IMX8X)
14 #include <asm/arch/sys_proto.h>
16 #define is_cpu_type(cpu) (0)
18 #if defined(CONFIG_CPU_PXA27X)
19 #include <asm/arch-pxa/pxa.h>
21 #define cpu_is_pxa27x(cpu) (0)
30 #include <asm/mach-types.h>
32 DECLARE_GLOBAL_DATA_PTR;
34 #define TAG_VALID 0xcf01
35 #define TAG_MAC 0x0000
37 #define TAG_INVALID 0xffff
39 #define TAG_FLAG_VALID 0x1
41 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
42 #define TDX_CFG_BLOCK_MAX_SIZE 512
43 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
44 #define TDX_CFG_BLOCK_MAX_SIZE 64
45 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
46 #define TDX_CFG_BLOCK_MAX_SIZE 64
48 #error Toradex config block location not set
58 struct toradex_hw tdx_hw_tag;
59 struct toradex_eth_addr tdx_eth_addr;
62 const char * const toradex_modules[] = {
63 [0] = "UNKNOWN MODULE",
64 [1] = "Colibri PXA270 312MHz",
65 [2] = "Colibri PXA270 520MHz",
66 [3] = "Colibri PXA320 806MHz",
67 [4] = "Colibri PXA300 208MHz",
68 [5] = "Colibri PXA310 624MHz",
69 [6] = "Colibri PXA320 806MHz IT",
70 [7] = "Colibri PXA300 208MHz XT",
71 [8] = "Colibri PXA270 312MHz",
72 [9] = "Colibri PXA270 520MHz",
73 [10] = "Colibri VF50 128MB", /* not currently on sale */
74 [11] = "Colibri VF61 256MB",
75 [12] = "Colibri VF61 256MB IT",
76 [13] = "Colibri VF50 128MB IT",
77 [14] = "Colibri iMX6 Solo 256MB",
78 [15] = "Colibri iMX6 DualLite 512MB",
79 [16] = "Colibri iMX6 Solo 256MB IT",
80 [17] = "Colibri iMX6 DualLite 512MB IT",
81 [18] = "UNKNOWN MODULE",
82 [19] = "UNKNOWN MODULE",
83 [20] = "Colibri T20 256MB",
84 [21] = "Colibri T20 512MB",
85 [22] = "Colibri T20 512MB IT",
86 [23] = "Colibri T30 1GB",
87 [24] = "Colibri T20 256MB IT",
88 [25] = "Apalis T30 2GB",
89 [26] = "Apalis T30 1GB",
90 [27] = "Apalis iMX6 Quad 1GB",
91 [28] = "Apalis iMX6 Quad 2GB IT",
92 [29] = "Apalis iMX6 Dual 512MB",
93 [30] = "Colibri T30 1GB IT",
94 [31] = "Apalis T30 1GB IT",
95 [32] = "Colibri iMX7 Solo 256MB",
96 [33] = "Colibri iMX7 Dual 512MB",
97 [34] = "Apalis TK1 2GB",
98 [35] = "Apalis iMX6 Dual 1GB IT",
99 [36] = "Colibri iMX6ULL 256MB",
100 [37] = "Apalis iMX8 QuadMax 4GB Wi-Fi / BT IT",
101 [38] = "Colibri iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
102 [39] = "Colibri iMX7 Dual 1GB (eMMC)",
103 [40] = "Colibri iMX6ULL 512MB Wi-Fi / BT IT",
104 [41] = "Colibri iMX7 Dual 512MB EPDC",
105 [42] = "Apalis TK1 4GB",
106 [43] = "Colibri T20 512MB IT SETEK",
107 [44] = "Colibri iMX6ULL 512MB IT",
108 [45] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth",
109 [46] = "Apalis iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
110 [47] = "Apalis iMX8 QuadMax 4GB IT",
111 [48] = "Apalis iMX8 QuadPlus 2GB Wi-Fi / BT",
112 [49] = "Apalis iMX8 QuadPlus 2GB",
113 [50] = "Colibri iMX8 QuadXPlus 2GB IT",
114 [51] = "Colibri iMX8 DualX 1GB Wi-Fi / Bluetooth",
115 [52] = "Colibri iMX8 DualX 1GB",
116 [53] = "Apalis iMX8 QuadXPlus 2GB ECC IT",
117 [54] = "Apalis iMX8 DualXPlus 1GB",
120 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
121 static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
124 int dev = CONFIG_TDX_CFG_BLOCK_DEV;
125 int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
126 uint part = CONFIG_TDX_CFG_BLOCK_PART;
130 /* Read production parameter config block from eMMC */
131 mmc = find_mmc_device(dev);
133 puts("No MMC card found\n");
138 puts("MMC init failed\n");
141 if (part != mmc_get_blk_desc(mmc)->hwpart) {
142 if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) {
143 puts("MMC partition switch failed\n");
149 offset += mmc->capacity;
150 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
153 /* Careful reads a whole block of 512 bytes into config_block */
154 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
155 (unsigned char *)config_block) != 1) {
160 /* Just writing one 512 byte block */
161 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
162 (unsigned char *)config_block) != 1) {
169 /* Switch back to regular eMMC user partition */
170 blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
176 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
177 static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
179 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
180 struct mtd_info *mtd = get_nand_dev_by_index(0);
185 /* Read production parameter config block from NAND page */
186 return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
187 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
191 static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
193 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
195 /* Write production parameter config block to NAND page */
196 return nand_write_skip_bad(get_nand_dev_by_index(0),
197 CONFIG_TDX_CFG_BLOCK_OFFSET,
198 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
199 config_block, WITH_WR_VERIFY);
203 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
204 static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
206 /* Read production parameter config block from NOR flash */
207 memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
208 TDX_CFG_BLOCK_MAX_SIZE);
212 static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
214 /* Write production parameter config block to NOR flash */
215 return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
216 TDX_CFG_BLOCK_MAX_SIZE);
220 int read_tdx_cfg_block(void)
223 u8 *config_block = NULL;
224 struct toradex_tag *tag;
225 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
228 /* Allocate RAM area for config block */
229 config_block = memalign(ARCH_DMA_MINALIGN, size);
231 printf("Not enough malloc space available!\n");
235 memset(config_block, 0, size);
237 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
238 ret = tdx_cfg_block_mmc_storage(config_block, 0);
239 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
240 ret = read_tdx_cfg_block_from_nand(config_block);
241 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
242 ret = read_tdx_cfg_block_from_nor(config_block);
249 /* Expect a valid tag first */
250 tag = (struct toradex_tag *)config_block;
251 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
252 valid_cfgblock = false;
256 valid_cfgblock = true;
259 while (offset < TDX_CFG_BLOCK_MAX_SIZE) {
260 tag = (struct toradex_tag *)(config_block + offset);
262 if (tag->id == TAG_INVALID)
265 if (tag->flags == TAG_FLAG_VALID) {
268 memcpy(&tdx_eth_addr, config_block + offset,
271 /* NIC part of MAC address is serial number */
272 tdx_serial = ntohl(tdx_eth_addr.nic) >> 8;
275 memcpy(&tdx_hw_tag, config_block + offset, 8);
280 /* Get to next tag according to current tags length */
281 offset += tag->len * 4;
284 /* Cap product id to avoid issues with a yet unknown one */
285 if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) /
286 sizeof(toradex_modules[0])))
287 tdx_hw_tag.prodid = 0;
294 static int get_cfgblock_interactive(void)
296 char message[CONFIG_SYS_CBSIZE];
302 /* Unknown module by default */
303 tdx_hw_tag.prodid = 0;
306 sprintf(message, "Is the module the 312 MHz version? [y/N] ");
308 sprintf(message, "Is the module an IT version? [y/N] ");
309 len = cli_readline(message);
310 it = console_buffer[0];
312 #if defined(CONFIG_TARGET_APALIS_IMX8) || \
313 defined(CONFIG_TARGET_APALIS_IMX8X) || \
314 defined(CONFIG_TARGET_COLIBRI_IMX6ULL) || \
315 defined(CONFIG_TARGET_COLIBRI_IMX8X)
316 sprintf(message, "Does the module have Wi-Fi / Bluetooth? [y/N] ");
317 len = cli_readline(message);
318 wb = console_buffer[0];
321 soc = env_get("soc");
322 if (!strcmp("mx6", soc)) {
323 #ifdef CONFIG_TARGET_APALIS_IMX6
324 if (it == 'y' || it == 'Y') {
325 if (is_cpu_type(MXC_CPU_MX6Q))
326 tdx_hw_tag.prodid = APALIS_IMX6Q_IT;
328 tdx_hw_tag.prodid = APALIS_IMX6D_IT;
330 if (is_cpu_type(MXC_CPU_MX6Q))
331 tdx_hw_tag.prodid = APALIS_IMX6Q;
333 tdx_hw_tag.prodid = APALIS_IMX6D;
335 #elif CONFIG_TARGET_COLIBRI_IMX6
336 if (it == 'y' || it == 'Y') {
337 if (is_cpu_type(MXC_CPU_MX6DL))
338 tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT;
339 else if (is_cpu_type(MXC_CPU_MX6SOLO))
340 tdx_hw_tag.prodid = COLIBRI_IMX6S_IT;
342 if (is_cpu_type(MXC_CPU_MX6DL))
343 tdx_hw_tag.prodid = COLIBRI_IMX6DL;
344 else if (is_cpu_type(MXC_CPU_MX6SOLO))
345 tdx_hw_tag.prodid = COLIBRI_IMX6S;
347 #elif CONFIG_TARGET_COLIBRI_IMX6ULL
348 if (it == 'y' || it == 'Y') {
349 if (wb == 'y' || wb == 'Y')
350 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT;
352 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT;
354 if (wb == 'y' || wb == 'Y')
355 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT;
357 tdx_hw_tag.prodid = COLIBRI_IMX6ULL;
360 } else if (!strcmp("imx7d", soc))
361 tdx_hw_tag.prodid = COLIBRI_IMX7D;
362 else if (!strcmp("imx7s", soc))
363 tdx_hw_tag.prodid = COLIBRI_IMX7S;
364 else if (is_cpu_type(MXC_CPU_IMX8QM)) {
365 if (it == 'y' || it == 'Y') {
366 if (wb == 'y' || wb == 'Y')
367 tdx_hw_tag.prodid = APALIS_IMX8QM_WIFI_BT_IT;
369 tdx_hw_tag.prodid = APALIS_IMX8QM_IT;
371 if (wb == 'y' || wb == 'Y')
372 tdx_hw_tag.prodid = APALIS_IMX8QP_WIFI_BT;
374 tdx_hw_tag.prodid = APALIS_IMX8QP;
376 } else if (is_cpu_type(MXC_CPU_IMX8QXP)) {
377 #ifdef CONFIG_TARGET_APALIS_IMX8X
378 if (it == 'y' || it == 'Y' || wb == 'y' || wb == 'Y') {
379 tdx_hw_tag.prodid = APALIS_IMX8QXP_WIFI_BT_IT;
381 if (gd->ram_size == 0x40000000)
382 tdx_hw_tag.prodid = APALIS_IMX8DXP;
384 tdx_hw_tag.prodid = APALIS_IMX8QXP;
386 #elif CONFIG_TARGET_COLIBRI_IMX8X
387 if (it == 'y' || it == 'Y') {
388 if (wb == 'y' || wb == 'Y')
389 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT;
391 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_IT;
393 if (wb == 'y' || wb == 'Y')
394 tdx_hw_tag.prodid = COLIBRI_IMX8DX_WIFI_BT;
396 tdx_hw_tag.prodid = COLIBRI_IMX8DX;
399 } else if (!strcmp("tegra20", soc)) {
400 if (it == 'y' || it == 'Y')
401 if (gd->ram_size == 0x10000000)
402 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
404 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
406 if (gd->ram_size == 0x10000000)
407 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
409 tdx_hw_tag.prodid = COLIBRI_T20_512MB;
410 } else if (cpu_is_pxa27x()) {
411 if (it == 'y' || it == 'Y')
412 tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ;
414 tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
416 #ifdef CONFIG_MACH_TYPE
417 else if (!strcmp("tegra30", soc)) {
418 if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) {
419 if (it == 'y' || it == 'Y')
420 tdx_hw_tag.prodid = APALIS_T30_IT;
422 if (gd->ram_size == 0x40000000)
423 tdx_hw_tag.prodid = APALIS_T30_1GB;
425 tdx_hw_tag.prodid = APALIS_T30_2GB;
427 if (it == 'y' || it == 'Y')
428 tdx_hw_tag.prodid = COLIBRI_T30_IT;
430 tdx_hw_tag.prodid = COLIBRI_T30;
433 #endif /* CONFIG_MACH_TYPE */
434 else if (!strcmp("tegra124", soc)) {
435 tdx_hw_tag.prodid = APALIS_TK1_2GB;
436 } else if (!strcmp("vf500", soc)) {
437 if (it == 'y' || it == 'Y')
438 tdx_hw_tag.prodid = COLIBRI_VF50_IT;
440 tdx_hw_tag.prodid = COLIBRI_VF50;
441 } else if (!strcmp("vf610", soc)) {
442 if (it == 'y' || it == 'Y')
443 tdx_hw_tag.prodid = COLIBRI_VF61_IT;
445 tdx_hw_tag.prodid = COLIBRI_VF61;
448 if (!tdx_hw_tag.prodid) {
449 printf("Module type not detectable due to unknown SoC\n");
454 sprintf(message, "Enter the module version (e.g. V1.1B): V");
455 len = cli_readline(message);
458 tdx_hw_tag.ver_major = console_buffer[0] - '0';
459 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
460 tdx_hw_tag.ver_assembly = console_buffer[3] - 'A';
462 if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1)
463 tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
464 COLIBRI_PXA270_V1_312MHZ);
467 sprintf(message, "Enter module serial number: ");
468 len = cli_readline(message);
471 tdx_serial = simple_strtoul(console_buffer, NULL, 10);
476 static int get_cfgblock_barcode(char *barcode)
478 if (strlen(barcode) < 16) {
479 printf("Argument too short, barcode is 16 chars long\n");
483 /* Get hardware information from the first 8 digits */
484 tdx_hw_tag.ver_major = barcode[4] - '0';
485 tdx_hw_tag.ver_minor = barcode[5] - '0';
486 tdx_hw_tag.ver_assembly = barcode[7] - '0';
489 tdx_hw_tag.prodid = simple_strtoul(barcode, NULL, 10);
491 /* Parse second part of the barcode (serial number */
493 tdx_serial = simple_strtoul(barcode, NULL, 10);
498 static int do_cfgblock_create(cmd_tbl_t *cmdtp, int flag, int argc,
502 struct toradex_tag *tag;
503 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
505 int ret = CMD_RET_SUCCESS;
507 int force_overwrite = 0;
509 /* Allocate RAM area for config block */
510 config_block = memalign(ARCH_DMA_MINALIGN, size);
512 printf("Not enough malloc space available!\n");
513 return CMD_RET_FAILURE;
516 memset(config_block, 0xff, size);
519 if (argv[2][0] == '-' && argv[2][1] == 'y')
523 read_tdx_cfg_block();
524 if (valid_cfgblock) {
525 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
527 * On NAND devices, recreation is only allowed if the page is
528 * empty (config block invalid...)
530 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
531 CONFIG_TDX_CFG_BLOCK_OFFSET /
532 get_nand_dev_by_index(0)->erasesize);
534 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
536 * On NOR devices, recreation is only allowed if the sector is
537 * empty and write protection is off (config block invalid...)
539 printf("NOR sector at offset 0x%02x need to be erased and unprotected before creating a Toradex config block\n",
540 CONFIG_TDX_CFG_BLOCK_OFFSET);
543 if (!force_overwrite) {
544 char message[CONFIG_SYS_CBSIZE];
547 "A valid Toradex config block is present, still recreate? [y/N] ");
549 if (!cli_readline(message))
552 if (console_buffer[0] != 'y' &&
553 console_buffer[0] != 'Y')
559 /* Parse new Toradex config block data... */
560 if (argc < 3 || (force_overwrite && argc < 4)) {
561 err = get_cfgblock_interactive();
564 err = get_cfgblock_barcode(argv[3]);
566 err = get_cfgblock_barcode(argv[2]);
569 ret = CMD_RET_FAILURE;
573 /* Convert serial number to MAC address (the storage format) */
574 tdx_eth_addr.oui = htonl(0x00142dUL << 8);
575 tdx_eth_addr.nic = htonl(tdx_serial << 8);
578 tag = (struct toradex_tag *)config_block;
580 tag->flags = TAG_FLAG_VALID;
585 tag = (struct toradex_tag *)(config_block + offset);
587 tag->flags = TAG_FLAG_VALID;
591 memcpy(config_block + offset, &tdx_hw_tag, 8);
595 tag = (struct toradex_tag *)(config_block + offset);
597 tag->flags = TAG_FLAG_VALID;
601 memcpy(config_block + offset, &tdx_eth_addr, 6);
603 memset(config_block + offset, 0, 32 - offset);
605 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
606 err = tdx_cfg_block_mmc_storage(config_block, 1);
607 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
608 err = write_tdx_cfg_block_to_nand(config_block);
609 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
610 err = write_tdx_cfg_block_to_nor(config_block);
615 printf("Failed to write Toradex config block: %d\n", ret);
616 ret = CMD_RET_FAILURE;
620 printf("Toradex config block successfully written\n");
627 static int do_cfgblock(cmd_tbl_t *cmdtp, int flag, int argc,
633 return CMD_RET_USAGE;
635 if (!strcmp(argv[1], "create")) {
636 return do_cfgblock_create(cmdtp, flag, argc, argv);
637 } else if (!strcmp(argv[1], "reload")) {
638 ret = read_tdx_cfg_block();
640 printf("Failed to reload Toradex config block: %d\n",
642 return CMD_RET_FAILURE;
644 return CMD_RET_SUCCESS;
647 return CMD_RET_USAGE;
650 U_BOOT_CMD(cfgblock, 4, 0, do_cfgblock,
651 "Toradex config block handling commands",
652 "create [-y] [barcode] - (Re-)create Toradex config block\n"
653 "cfgblock reload - Reload Toradex config block from flash"