uboot/board/toradex/common/tdx-cfg-block.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * Copyright (c) 2016-2020 Toradex
   4 */
   5
   6#include <common.h>
   7#include <asm/global_data.h>
   8#include "tdx-cfg-block.h"
   9#include "tdx-eeprom.h"
  10
  11#include <command.h>
  12#include <asm/cache.h>
  13
  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>
  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>
  33#include <env.h>
  34#include <flash.h>
  35#include <malloc.h>
  36#include <mmc.h>
  37#include <nand.h>
  38#include <asm/mach-types.h>
  39
  40DECLARE_GLOBAL_DATA_PTR;
  41
  42#define TAG_VALID       0xcf01
  43#define TAG_MAC         0x0000
  44#define TAG_CAR_SERIAL  0x0021
  45#define TAG_HW          0x0008
  46#define TAG_INVALID     0xffff
  47
  48#define TAG_FLAG_VALID  0x1
  49
  50#define TDX_EEPROM_ID_MODULE            0
  51#define TDX_EEPROM_ID_CARRIER           1
  52
  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
  61#else
  62#error Toradex config block location not set
  63#endif
  64
  65#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
  66#define TDX_CFG_BLOCK_EXTRA_MAX_SIZE 64
  67#endif
  68
  69struct 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;
  79#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
  80u32 tdx_car_serial;
  81bool valid_cfgblock_carrier;
  82struct toradex_hw tdx_car_hw_tag;
  83#endif
  84
  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",
 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",
 153};
 154
 155const 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
 167#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        }
 184        if (mmc_init(mmc)) {
 185                puts("MMC init failed\n");
 186                return -EINVAL;
 187        }
 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");
 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                }
 206        } 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;
 227        struct mtd_info *mtd = get_nand_dev_by_index(0);
 228
 229        if (!mtd)
 230                return -ENODEV;
 231
 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,
 235                                  config_block);
 236}
 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 */
 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);
 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
 267#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
 281int 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);
 304#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
 305        ret = read_tdx_cfg_block_from_eeprom(config_block);
 306#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
 322        /*
 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) {
 328                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 */
 353        if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) /
 354                                  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';
 367        char wb = 'n';
 368        int len = 0;
 369
 370        /* Unknown module by default */
 371        tdx_hw_tag.prodid = 0;
 372
 373        if (cpu_is_pxa27x())
 374                sprintf(message, "Is the module the 312 MHz version? [y/N] ");
 375        else
 376                sprintf(message, "Is the module an IT version? [y/N] ");
 377
 378        len = cli_readline(message);
 379        it = console_buffer[0];
 380
 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];
 390#endif
 391
 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;
 398                        else
 399                                tdx_hw_tag.prodid = APALIS_IMX6D_IT;
 400                } else {
 401                        if (is_cpu_type(MXC_CPU_MX6Q))
 402                                tdx_hw_tag.prodid = APALIS_IMX6Q;
 403                        else
 404                                tdx_hw_tag.prodid = APALIS_IMX6D;
 405                }
 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;
 412                } else {
 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;
 417                }
 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;
 422                        else
 423                                if (gd->ram_size == 0x20000000)
 424                                        tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT;
 425                                else
 426                                        tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT_EMMC;
 427                } 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
 434        } else if (!strcmp("imx7d", soc))
 435                if (gd->ram_size == 0x20000000)
 436                        tdx_hw_tag.prodid = COLIBRI_IMX7D;
 437                else
 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;
 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)) {
 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;
 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
 464                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                }
 475#endif
 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;
 479                else
 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;
 484                else
 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;
 496                        else
 497                                tdx_hw_tag.prodid = VERDIN_IMX8MPQ_WIFI_BT_IT;
 498                else
 499                        if (it == 'y' || it == 'Y')
 500                                tdx_hw_tag.prodid = VERDIN_IMX8MPQ_IT;
 501                        else
 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;
 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;
 519        }
 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;
 525                else
 526                        if (gd->ram_size == 0x40000000)
 527                                tdx_hw_tag.prodid = APALIS_T30_1GB;
 528                        else
 529                                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
 536        }
 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;
 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;
 550        }
 551
 552        if (!tdx_hw_tag.prodid) {
 553                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
 566        if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1)
 567                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
 575        tdx_serial = dectoul(console_buffer, NULL);
 576
 577        return 0;
 578}
 579
 580static int get_cfgblock_barcode(char *barcode, struct toradex_hw *tag,
 581                                u32 *serial)
 582{
 583        char revision[3] = {barcode[6], barcode[7], '\0'};
 584
 585        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 */
 591        tag->ver_major = barcode[4] - '0';
 592        tag->ver_minor = barcode[5] - '0';
 593        tag->ver_assembly = dectoul(revision, NULL);
 594
 595        barcode[4] = '\0';
 596        tag->prodid = dectoul(barcode, NULL);
 597
 598        /* Parse second part of the barcode (serial number */
 599        barcode += 8;
 600        *serial = dectoul(barcode, NULL);
 601
 602        return 0;
 603}
 604
 605static int write_tag(u8 *config_block, int *offset, int tag_id,
 606                     u8 *tag_data, size_t tag_data_size)
 607{
 608        struct toradex_tag *tag;
 609
 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
 689int 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';
 737        tdx_car_hw_tag.prodid = dectoul(pid8, NULL);
 738
 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
 767static 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);
 781        tdx_car_hw_tag.prodid = dectoul(console_buffer, NULL);
 782
 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
 797        tdx_car_serial = dectoul(console_buffer, NULL);
 798
 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;
 807        int offset = 0;
 808        int ret = CMD_RET_SUCCESS;
 809        int err;
 810        int force_overwrite = 0;
 811
 812        if (argc >= 3) {
 813                if (argv[2][0] == '-' && argv[2][1] == 'y')
 814                        force_overwrite = 1;
 815        }
 816
 817        /* 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);
 825        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;
 894
 895        if (argc >= 3) {
 896#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 */
 901                if (argv[2][0] == '-' && argv[2][1] == 'y')
 902                        force_overwrite = 1;
 903        }
 904
 905        /* 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
 914        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                 */
 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);
 924                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                 */
 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);
 932                goto out;
 933#else
 934                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] ");
 939
 940                        if (!cli_readline(message))
 941                                goto out;
 942
 943                        if (console_buffer[0] != 'y' &&
 944                            console_buffer[0] != 'Y')
 945                                goto out;
 946                }
 947#endif
 948        }
 949
 950        /* Parse new Toradex config block data... */
 951        if (argc < 3 || (force_overwrite && argc < 4)) {
 952                err = get_cfgblock_interactive();
 953        } else {
 954                if (force_overwrite)
 955                        err = get_cfgblock_barcode(argv[3], &tdx_hw_tag,
 956                                                   &tdx_serial);
 957                else
 958                        err = get_cfgblock_barcode(argv[2], &tdx_hw_tag,
 959                                                   &tdx_serial);
 960        }
 961        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 */
 971        write_tag(config_block, &offset, TAG_VALID, NULL, 0);
 972
 973        /* Product Tag */
 974        write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_hw_tag,
 975                  sizeof(tdx_hw_tag));
 976
 977        /* MAC Tag */
 978        write_tag(config_block, &offset, TAG_MAC, (u8 *)&tdx_eth_addr,
 979                  sizeof(tdx_eth_addr));
 980
 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);
 990#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
1006static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc,
1007                       char *const argv[])
1008{
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
1029U_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"
1035);
1036