1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2016-2019 Toradex, Inc.
7 #include "tdx-cfg-block.h"
9 #if defined(CONFIG_TARGET_APALIS_IMX6) || \
10 defined(CONFIG_TARGET_COLIBRI_IMX6) || \
11 defined(CONFIG_TARGET_COLIBRI_IMX8QXP)
12 #include <asm/arch/sys_proto.h>
14 #define is_cpu_type(cpu) (0)
16 #if defined(CONFIG_CPU_PXA27X)
17 #include <asm/arch-pxa/pxa.h>
19 #define cpu_is_pxa27x(cpu) (0)
27 #include <asm/mach-types.h>
29 DECLARE_GLOBAL_DATA_PTR;
31 #define TAG_VALID 0xcf01
32 #define TAG_MAC 0x0000
34 #define TAG_INVALID 0xffff
36 #define TAG_FLAG_VALID 0x1
38 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
39 #define TDX_CFG_BLOCK_MAX_SIZE 512
40 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
41 #define TDX_CFG_BLOCK_MAX_SIZE 64
42 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
43 #define TDX_CFG_BLOCK_MAX_SIZE 64
45 #error Toradex config block location not set
55 struct toradex_hw tdx_hw_tag;
56 struct toradex_eth_addr tdx_eth_addr;
59 const char * const toradex_modules[] = {
60 [0] = "UNKNOWN MODULE",
61 [1] = "Colibri PXA270 312MHz",
62 [2] = "Colibri PXA270 520MHz",
63 [3] = "Colibri PXA320 806MHz",
64 [4] = "Colibri PXA300 208MHz",
65 [5] = "Colibri PXA310 624MHz",
66 [6] = "Colibri PXA320 806MHz IT",
67 [7] = "Colibri PXA300 208MHz XT",
68 [8] = "Colibri PXA270 312MHz",
69 [9] = "Colibri PXA270 520MHz",
70 [10] = "Colibri VF50 128MB", /* not currently on sale */
71 [11] = "Colibri VF61 256MB",
72 [12] = "Colibri VF61 256MB IT",
73 [13] = "Colibri VF50 128MB IT",
74 [14] = "Colibri iMX6 Solo 256MB",
75 [15] = "Colibri iMX6 DualLite 512MB",
76 [16] = "Colibri iMX6 Solo 256MB IT",
77 [17] = "Colibri iMX6 DualLite 512MB IT",
78 [18] = "UNKNOWN MODULE",
79 [19] = "UNKNOWN MODULE",
80 [20] = "Colibri T20 256MB",
81 [21] = "Colibri T20 512MB",
82 [22] = "Colibri T20 512MB IT",
83 [23] = "Colibri T30 1GB",
84 [24] = "Colibri T20 256MB IT",
85 [25] = "Apalis T30 2GB",
86 [26] = "Apalis T30 1GB",
87 [27] = "Apalis iMX6 Quad 1GB",
88 [28] = "Apalis iMX6 Quad 2GB IT",
89 [29] = "Apalis iMX6 Dual 512MB",
90 [30] = "Colibri T30 1GB IT",
91 [31] = "Apalis T30 1GB IT",
92 [32] = "Colibri iMX7 Solo 256MB",
93 [33] = "Colibri iMX7 Dual 512MB",
94 [34] = "Apalis TK1 2GB",
95 [35] = "Apalis iMX6 Dual 1GB IT",
96 [36] = "Colibri iMX6ULL 256MB",
97 [37] = "Apalis iMX8 QuadMax 4GB Wi-Fi / BT IT",
98 [38] = "Colibri iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
99 [39] = "Colibri iMX7 Dual 1GB (eMMC)",
100 [40] = "Colibri iMX6ULL 512MB Wi-Fi / BT IT",
101 [41] = "Colibri iMX7 Dual 512MB EPDC",
102 [42] = "Apalis TK1 4GB",
103 [43] = "Colibri T20 512MB IT SETEK",
104 [44] = "Colibri iMX6ULL 512MB IT",
105 [45] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth",
106 [46] = "Apalis iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
107 [47] = "Apalis iMX8 QuadMax 4GB IT",
108 [48] = "Apalis iMX8 QuadPlus 2GB Wi-Fi / BT",
109 [49] = "Apalis iMX8 QuadPlus 2GB",
110 [50] = "Colibri iMX8 QuadXPlus 2GB IT",
111 [51] = "Colibri iMX8 DualX 1GB Wi-Fi / Bluetooth",
112 [52] = "Colibri iMX8 DualX 1GB",
115 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
116 static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
119 int dev = CONFIG_TDX_CFG_BLOCK_DEV;
120 int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
121 uint part = CONFIG_TDX_CFG_BLOCK_PART;
125 /* Read production parameter config block from eMMC */
126 mmc = find_mmc_device(dev);
128 puts("No MMC card found\n");
133 puts("MMC init failed\n");
136 if (part != mmc_get_blk_desc(mmc)->hwpart) {
137 if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) {
138 puts("MMC partition switch failed\n");
144 offset += mmc->capacity;
145 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
148 /* Careful reads a whole block of 512 bytes into config_block */
149 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
150 (unsigned char *)config_block) != 1) {
155 /* Just writing one 512 byte block */
156 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
157 (unsigned char *)config_block) != 1) {
164 /* Switch back to regular eMMC user partition */
165 blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
171 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
172 static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
174 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
175 struct mtd_info *mtd = get_nand_dev_by_index(0);
180 /* Read production parameter config block from NAND page */
181 return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
182 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
186 static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
188 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
190 /* Write production parameter config block to NAND page */
191 return nand_write_skip_bad(get_nand_dev_by_index(0),
192 CONFIG_TDX_CFG_BLOCK_OFFSET,
193 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
194 config_block, WITH_WR_VERIFY);
198 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
199 static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
201 /* Read production parameter config block from NOR flash */
202 memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
203 TDX_CFG_BLOCK_MAX_SIZE);
207 static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
209 /* Write production parameter config block to NOR flash */
210 return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
211 TDX_CFG_BLOCK_MAX_SIZE);
215 int read_tdx_cfg_block(void)
218 u8 *config_block = NULL;
219 struct toradex_tag *tag;
220 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
223 /* Allocate RAM area for config block */
224 config_block = memalign(ARCH_DMA_MINALIGN, size);
226 printf("Not enough malloc space available!\n");
230 memset(config_block, 0, size);
232 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
233 ret = tdx_cfg_block_mmc_storage(config_block, 0);
234 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
235 ret = read_tdx_cfg_block_from_nand(config_block);
236 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
237 ret = read_tdx_cfg_block_from_nor(config_block);
244 /* Expect a valid tag first */
245 tag = (struct toradex_tag *)config_block;
246 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
247 valid_cfgblock = false;
251 valid_cfgblock = true;
254 while (offset < TDX_CFG_BLOCK_MAX_SIZE) {
255 tag = (struct toradex_tag *)(config_block + offset);
257 if (tag->id == TAG_INVALID)
260 if (tag->flags == TAG_FLAG_VALID) {
263 memcpy(&tdx_eth_addr, config_block + offset,
266 /* NIC part of MAC address is serial number */
267 tdx_serial = ntohl(tdx_eth_addr.nic) >> 8;
270 memcpy(&tdx_hw_tag, config_block + offset, 8);
275 /* Get to next tag according to current tags length */
276 offset += tag->len * 4;
279 /* Cap product id to avoid issues with a yet unknown one */
280 if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) /
281 sizeof(toradex_modules[0])))
282 tdx_hw_tag.prodid = 0;
289 static int get_cfgblock_interactive(void)
291 char message[CONFIG_SYS_CBSIZE];
296 /* Unknown module by default */
297 tdx_hw_tag.prodid = 0;
300 sprintf(message, "Is the module the 312 MHz version? [y/N] ");
302 sprintf(message, "Is the module an IT version? [y/N] ");
304 len = cli_readline(message);
305 it = console_buffer[0];
307 soc = env_get("soc");
308 if (!strcmp("mx6", soc)) {
309 #ifdef CONFIG_TARGET_APALIS_IMX6
310 if (it == 'y' || it == 'Y') {
311 if (is_cpu_type(MXC_CPU_MX6Q))
312 tdx_hw_tag.prodid = APALIS_IMX6Q_IT;
314 tdx_hw_tag.prodid = APALIS_IMX6D_IT;
316 if (is_cpu_type(MXC_CPU_MX6Q))
317 tdx_hw_tag.prodid = APALIS_IMX6Q;
319 tdx_hw_tag.prodid = APALIS_IMX6D;
321 #elif CONFIG_TARGET_COLIBRI_IMX6
322 if (it == 'y' || it == 'Y') {
323 if (is_cpu_type(MXC_CPU_MX6DL))
324 tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT;
325 else if (is_cpu_type(MXC_CPU_MX6SOLO))
326 tdx_hw_tag.prodid = COLIBRI_IMX6S_IT;
328 if (is_cpu_type(MXC_CPU_MX6DL))
329 tdx_hw_tag.prodid = COLIBRI_IMX6DL;
330 else if (is_cpu_type(MXC_CPU_MX6SOLO))
331 tdx_hw_tag.prodid = COLIBRI_IMX6S;
333 #elif CONFIG_TARGET_COLIBRI_IMX6ULL
336 sprintf(message, "Does the module have Wi-Fi / Bluetooth? " \
338 len = cli_readline(message);
339 wb = console_buffer[0];
340 if (it == 'y' || it == 'Y') {
341 if (wb == 'y' || wb == 'Y')
342 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT;
344 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT;
346 if (wb == 'y' || wb == 'Y')
347 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT;
349 tdx_hw_tag.prodid = COLIBRI_IMX6ULL;
352 } else if (!strcmp("imx7d", soc))
353 tdx_hw_tag.prodid = COLIBRI_IMX7D;
354 else if (!strcmp("imx7s", soc))
355 tdx_hw_tag.prodid = COLIBRI_IMX7S;
356 else if (is_cpu_type(MXC_CPU_IMX8QXP))
357 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT;
358 else if (!strcmp("tegra20", soc)) {
359 if (it == 'y' || it == 'Y')
360 if (gd->ram_size == 0x10000000)
361 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
363 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
365 if (gd->ram_size == 0x10000000)
366 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
368 tdx_hw_tag.prodid = COLIBRI_T20_512MB;
369 } else if (cpu_is_pxa27x()) {
370 if (it == 'y' || it == 'Y')
371 tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ;
373 tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
375 #ifdef CONFIG_MACH_TYPE
376 else if (!strcmp("tegra30", soc)) {
377 if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) {
378 if (it == 'y' || it == 'Y')
379 tdx_hw_tag.prodid = APALIS_T30_IT;
381 if (gd->ram_size == 0x40000000)
382 tdx_hw_tag.prodid = APALIS_T30_1GB;
384 tdx_hw_tag.prodid = APALIS_T30_2GB;
386 if (it == 'y' || it == 'Y')
387 tdx_hw_tag.prodid = COLIBRI_T30_IT;
389 tdx_hw_tag.prodid = COLIBRI_T30;
392 #endif /* CONFIG_MACH_TYPE */
393 else if (!strcmp("tegra124", soc)) {
394 tdx_hw_tag.prodid = APALIS_TK1_2GB;
395 } else if (!strcmp("vf500", soc)) {
396 if (it == 'y' || it == 'Y')
397 tdx_hw_tag.prodid = COLIBRI_VF50_IT;
399 tdx_hw_tag.prodid = COLIBRI_VF50;
400 } else if (!strcmp("vf610", soc)) {
401 if (it == 'y' || it == 'Y')
402 tdx_hw_tag.prodid = COLIBRI_VF61_IT;
404 tdx_hw_tag.prodid = COLIBRI_VF61;
407 if (!tdx_hw_tag.prodid) {
408 printf("Module type not detectable due to unknown SoC\n");
413 sprintf(message, "Enter the module version (e.g. V1.1B): V");
414 len = cli_readline(message);
417 tdx_hw_tag.ver_major = console_buffer[0] - '0';
418 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
419 tdx_hw_tag.ver_assembly = console_buffer[3] - 'A';
421 if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1)
422 tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
423 COLIBRI_PXA270_V1_312MHZ);
426 sprintf(message, "Enter module serial number: ");
427 len = cli_readline(message);
430 tdx_serial = simple_strtoul(console_buffer, NULL, 10);
435 static int get_cfgblock_barcode(char *barcode)
437 if (strlen(barcode) < 16) {
438 printf("Argument too short, barcode is 16 chars long\n");
442 /* Get hardware information from the first 8 digits */
443 tdx_hw_tag.ver_major = barcode[4] - '0';
444 tdx_hw_tag.ver_minor = barcode[5] - '0';
445 tdx_hw_tag.ver_assembly = barcode[7] - '0';
448 tdx_hw_tag.prodid = simple_strtoul(barcode, NULL, 10);
450 /* Parse second part of the barcode (serial number */
452 tdx_serial = simple_strtoul(barcode, NULL, 10);
457 static int do_cfgblock_create(cmd_tbl_t *cmdtp, int flag, int argc,
461 struct toradex_tag *tag;
462 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
464 int ret = CMD_RET_SUCCESS;
466 int force_overwrite = 0;
468 /* Allocate RAM area for config block */
469 config_block = memalign(ARCH_DMA_MINALIGN, size);
471 printf("Not enough malloc space available!\n");
472 return CMD_RET_FAILURE;
475 memset(config_block, 0xff, size);
478 if (argv[2][0] == '-' && argv[2][1] == 'y')
482 read_tdx_cfg_block();
483 if (valid_cfgblock) {
484 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
486 * On NAND devices, recreation is only allowed if the page is
487 * empty (config block invalid...)
489 printf("NAND erase block %d need to be erased before creating" \
490 " a Toradex config block\n",
491 CONFIG_TDX_CFG_BLOCK_OFFSET /
492 get_nand_dev_by_index(0)->erasesize);
494 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
496 * On NOR devices, recreation is only allowed if the sector is
497 * empty and write protection is off (config block invalid...)
499 printf("NOR sector at offset 0x%02x need to be erased and " \
500 "unprotected before creating a Toradex config block\n",
501 CONFIG_TDX_CFG_BLOCK_OFFSET);
504 if (!force_overwrite) {
505 char message[CONFIG_SYS_CBSIZE];
508 "A valid Toradex config block is present, still recreate? [y/N] ");
510 if (!cli_readline(message))
513 if (console_buffer[0] != 'y' &&
514 console_buffer[0] != 'Y')
520 /* Parse new Toradex config block data... */
521 if (argc < 3 || (force_overwrite && argc < 4)) {
522 err = get_cfgblock_interactive();
525 err = get_cfgblock_barcode(argv[3]);
527 err = get_cfgblock_barcode(argv[2]);
530 ret = CMD_RET_FAILURE;
534 /* Convert serial number to MAC address (the storage format) */
535 tdx_eth_addr.oui = htonl(0x00142dUL << 8);
536 tdx_eth_addr.nic = htonl(tdx_serial << 8);
539 tag = (struct toradex_tag *)config_block;
541 tag->flags = TAG_FLAG_VALID;
546 tag = (struct toradex_tag *)(config_block + offset);
548 tag->flags = TAG_FLAG_VALID;
552 memcpy(config_block + offset, &tdx_hw_tag, 8);
556 tag = (struct toradex_tag *)(config_block + offset);
558 tag->flags = TAG_FLAG_VALID;
562 memcpy(config_block + offset, &tdx_eth_addr, 6);
564 memset(config_block + offset, 0, 32 - offset);
566 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
567 err = tdx_cfg_block_mmc_storage(config_block, 1);
568 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
569 err = write_tdx_cfg_block_to_nand(config_block);
570 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
571 err = write_tdx_cfg_block_to_nor(config_block);
576 printf("Failed to write Toradex config block: %d\n", ret);
577 ret = CMD_RET_FAILURE;
581 printf("Toradex config block successfully written\n");
588 static int do_cfgblock(cmd_tbl_t *cmdtp, int flag, int argc,
594 return CMD_RET_USAGE;
596 if (!strcmp(argv[1], "create")) {
597 return do_cfgblock_create(cmdtp, flag, argc, argv);
598 } else if (!strcmp(argv[1], "reload")) {
599 ret = read_tdx_cfg_block();
601 printf("Failed to reload Toradex config block: %d\n",
603 return CMD_RET_FAILURE;
605 return CMD_RET_SUCCESS;
608 return CMD_RET_USAGE;
612 cfgblock, 4, 0, do_cfgblock,
613 "Toradex config block handling commands",
614 "create [-y] [barcode] - (Re-)create Toradex config block\n"
615 "cfgblock reload - Reload Toradex config block from flash"