Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 2 | /* |
Gerard Salvatella | 08f8055 | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 3 | * Copyright (c) 2016-2019 Toradex, Inc. |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include "tdx-cfg-block.h" |
| 8 | |
Marcel Ziswiler | 4adc9fc | 2019-04-09 17:25:32 +0200 | [diff] [blame] | 9 | #if defined(CONFIG_TARGET_APALIS_IMX6) || \ |
Marcel Ziswiler | 9d63c7d | 2019-07-12 12:35:08 +0200 | [diff] [blame^] | 10 | defined(CONFIG_TARGET_APALIS_IMX8) || \ |
Marcel Ziswiler | 4adc9fc | 2019-04-09 17:25:32 +0200 | [diff] [blame] | 11 | defined(CONFIG_TARGET_COLIBRI_IMX6) || \ |
Marcel Ziswiler | 6c297ee | 2019-07-12 12:35:07 +0200 | [diff] [blame] | 12 | defined(CONFIG_TARGET_COLIBRI_IMX8X) |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 13 | #include <asm/arch/sys_proto.h> |
| 14 | #else |
| 15 | #define is_cpu_type(cpu) (0) |
| 16 | #endif |
| 17 | #if defined(CONFIG_CPU_PXA27X) |
| 18 | #include <asm/arch-pxa/pxa.h> |
| 19 | #else |
| 20 | #define cpu_is_pxa27x(cpu) (0) |
| 21 | #endif |
| 22 | #include <cli.h> |
| 23 | #include <console.h> |
| 24 | #include <flash.h> |
| 25 | #include <malloc.h> |
| 26 | #include <mmc.h> |
| 27 | #include <nand.h> |
Simon Glass | c62db35 | 2017-05-31 19:47:48 -0600 | [diff] [blame] | 28 | #include <asm/mach-types.h> |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 29 | |
| 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
| 32 | #define TAG_VALID 0xcf01 |
| 33 | #define TAG_MAC 0x0000 |
| 34 | #define TAG_HW 0x0008 |
| 35 | #define TAG_INVALID 0xffff |
| 36 | |
| 37 | #define TAG_FLAG_VALID 0x1 |
| 38 | |
| 39 | #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC) |
| 40 | #define TDX_CFG_BLOCK_MAX_SIZE 512 |
| 41 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND) |
| 42 | #define TDX_CFG_BLOCK_MAX_SIZE 64 |
| 43 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR) |
| 44 | #define TDX_CFG_BLOCK_MAX_SIZE 64 |
| 45 | #else |
| 46 | #error Toradex config block location not set |
| 47 | #endif |
| 48 | |
| 49 | struct toradex_tag { |
| 50 | u32 len:14; |
| 51 | u32 flags:2; |
| 52 | u32 id:16; |
| 53 | }; |
| 54 | |
| 55 | bool valid_cfgblock; |
| 56 | struct toradex_hw tdx_hw_tag; |
| 57 | struct toradex_eth_addr tdx_eth_addr; |
| 58 | u32 tdx_serial; |
| 59 | |
| 60 | const char * const toradex_modules[] = { |
| 61 | [0] = "UNKNOWN MODULE", |
| 62 | [1] = "Colibri PXA270 312MHz", |
| 63 | [2] = "Colibri PXA270 520MHz", |
| 64 | [3] = "Colibri PXA320 806MHz", |
| 65 | [4] = "Colibri PXA300 208MHz", |
| 66 | [5] = "Colibri PXA310 624MHz", |
| 67 | [6] = "Colibri PXA320 806MHz IT", |
| 68 | [7] = "Colibri PXA300 208MHz XT", |
| 69 | [8] = "Colibri PXA270 312MHz", |
| 70 | [9] = "Colibri PXA270 520MHz", |
| 71 | [10] = "Colibri VF50 128MB", /* not currently on sale */ |
| 72 | [11] = "Colibri VF61 256MB", |
| 73 | [12] = "Colibri VF61 256MB IT", |
| 74 | [13] = "Colibri VF50 128MB IT", |
| 75 | [14] = "Colibri iMX6 Solo 256MB", |
| 76 | [15] = "Colibri iMX6 DualLite 512MB", |
| 77 | [16] = "Colibri iMX6 Solo 256MB IT", |
| 78 | [17] = "Colibri iMX6 DualLite 512MB IT", |
| 79 | [18] = "UNKNOWN MODULE", |
| 80 | [19] = "UNKNOWN MODULE", |
| 81 | [20] = "Colibri T20 256MB", |
| 82 | [21] = "Colibri T20 512MB", |
| 83 | [22] = "Colibri T20 512MB IT", |
| 84 | [23] = "Colibri T30 1GB", |
| 85 | [24] = "Colibri T20 256MB IT", |
| 86 | [25] = "Apalis T30 2GB", |
| 87 | [26] = "Apalis T30 1GB", |
| 88 | [27] = "Apalis iMX6 Quad 1GB", |
| 89 | [28] = "Apalis iMX6 Quad 2GB IT", |
| 90 | [29] = "Apalis iMX6 Dual 512MB", |
| 91 | [30] = "Colibri T30 1GB IT", |
| 92 | [31] = "Apalis T30 1GB IT", |
| 93 | [32] = "Colibri iMX7 Solo 256MB", |
| 94 | [33] = "Colibri iMX7 Dual 512MB", |
| 95 | [34] = "Apalis TK1 2GB", |
| 96 | [35] = "Apalis iMX6 Dual 1GB IT", |
Stefan Agner | d826b87 | 2018-05-30 19:01:47 +0200 | [diff] [blame] | 97 | [36] = "Colibri iMX6ULL 256MB", |
Marcel Ziswiler | 4adc9fc | 2019-04-09 17:25:32 +0200 | [diff] [blame] | 98 | [37] = "Apalis iMX8 QuadMax 4GB Wi-Fi / BT IT", |
| 99 | [38] = "Colibri iMX8 QuadXPlus 2GB Wi-Fi / BT IT", |
Stefan Agner | d826b87 | 2018-05-30 19:01:47 +0200 | [diff] [blame] | 100 | [39] = "Colibri iMX7 Dual 1GB (eMMC)", |
Marcel Ziswiler | 4adc9fc | 2019-04-09 17:25:32 +0200 | [diff] [blame] | 101 | [40] = "Colibri iMX6ULL 512MB Wi-Fi / BT IT", |
Stefan Agner | d826b87 | 2018-05-30 19:01:47 +0200 | [diff] [blame] | 102 | [41] = "Colibri iMX7 Dual 512MB EPDC", |
| 103 | [42] = "Apalis TK1 4GB", |
Gerard Salvatella | 08f8055 | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 104 | [43] = "Colibri T20 512MB IT SETEK", |
| 105 | [44] = "Colibri iMX6ULL 512MB IT", |
| 106 | [45] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth", |
Marcel Ziswiler | 6988a3a | 2019-04-09 17:25:33 +0200 | [diff] [blame] | 107 | [46] = "Apalis iMX8 QuadXPlus 2GB Wi-Fi / BT IT", |
| 108 | [47] = "Apalis iMX8 QuadMax 4GB IT", |
| 109 | [48] = "Apalis iMX8 QuadPlus 2GB Wi-Fi / BT", |
| 110 | [49] = "Apalis iMX8 QuadPlus 2GB", |
| 111 | [50] = "Colibri iMX8 QuadXPlus 2GB IT", |
| 112 | [51] = "Colibri iMX8 DualX 1GB Wi-Fi / Bluetooth", |
| 113 | [52] = "Colibri iMX8 DualX 1GB", |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC |
| 117 | static int tdx_cfg_block_mmc_storage(u8 *config_block, int write) |
| 118 | { |
| 119 | struct mmc *mmc; |
| 120 | int dev = CONFIG_TDX_CFG_BLOCK_DEV; |
| 121 | int offset = CONFIG_TDX_CFG_BLOCK_OFFSET; |
| 122 | uint part = CONFIG_TDX_CFG_BLOCK_PART; |
| 123 | uint blk_start; |
| 124 | int ret = 0; |
| 125 | |
| 126 | /* Read production parameter config block from eMMC */ |
| 127 | mmc = find_mmc_device(dev); |
| 128 | if (!mmc) { |
| 129 | puts("No MMC card found\n"); |
| 130 | ret = -ENODEV; |
| 131 | goto out; |
| 132 | } |
Stefan Agner | 42a4f18 | 2019-07-12 12:35:05 +0200 | [diff] [blame] | 133 | if (mmc_init(mmc)) { |
| 134 | puts("MMC init failed\n"); |
| 135 | return -EINVAL; |
| 136 | } |
Simon Glass | 0e513e7 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 137 | if (part != mmc_get_blk_desc(mmc)->hwpart) { |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 138 | if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) { |
| 139 | puts("MMC partition switch failed\n"); |
| 140 | ret = -ENODEV; |
| 141 | goto out; |
| 142 | } |
| 143 | } |
| 144 | if (offset < 0) |
| 145 | offset += mmc->capacity; |
| 146 | blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len; |
| 147 | |
| 148 | if (!write) { |
| 149 | /* Careful reads a whole block of 512 bytes into config_block */ |
| 150 | if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1, |
| 151 | (unsigned char *)config_block) != 1) { |
| 152 | ret = -EIO; |
| 153 | goto out; |
| 154 | } |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 155 | } else { |
| 156 | /* Just writing one 512 byte block */ |
| 157 | if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1, |
| 158 | (unsigned char *)config_block) != 1) { |
| 159 | ret = -EIO; |
| 160 | goto out; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | out: |
| 165 | /* Switch back to regular eMMC user partition */ |
| 166 | blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0); |
| 167 | |
| 168 | return ret; |
| 169 | } |
| 170 | #endif |
| 171 | |
| 172 | #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND |
| 173 | static int read_tdx_cfg_block_from_nand(unsigned char *config_block) |
| 174 | { |
| 175 | size_t size = TDX_CFG_BLOCK_MAX_SIZE; |
Stefan Agner | bc53fb1 | 2018-08-06 09:19:18 +0200 | [diff] [blame] | 176 | struct mtd_info *mtd = get_nand_dev_by_index(0); |
| 177 | |
| 178 | if (!mtd) |
| 179 | return -ENODEV; |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 180 | |
| 181 | /* Read production parameter config block from NAND page */ |
Stefan Agner | bc53fb1 | 2018-08-06 09:19:18 +0200 | [diff] [blame] | 182 | return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET, |
Grygorii Strashko | bf264cd | 2017-06-26 19:13:06 -0500 | [diff] [blame] | 183 | &size, NULL, TDX_CFG_BLOCK_MAX_SIZE, |
| 184 | config_block); |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static int write_tdx_cfg_block_to_nand(unsigned char *config_block) |
| 188 | { |
| 189 | size_t size = TDX_CFG_BLOCK_MAX_SIZE; |
| 190 | |
| 191 | /* Write production parameter config block to NAND page */ |
Grygorii Strashko | bf264cd | 2017-06-26 19:13:06 -0500 | [diff] [blame] | 192 | return nand_write_skip_bad(get_nand_dev_by_index(0), |
| 193 | CONFIG_TDX_CFG_BLOCK_OFFSET, |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 194 | &size, NULL, TDX_CFG_BLOCK_MAX_SIZE, |
| 195 | config_block, WITH_WR_VERIFY); |
| 196 | } |
| 197 | #endif |
| 198 | |
| 199 | #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR |
| 200 | static int read_tdx_cfg_block_from_nor(unsigned char *config_block) |
| 201 | { |
| 202 | /* Read production parameter config block from NOR flash */ |
| 203 | memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET, |
| 204 | TDX_CFG_BLOCK_MAX_SIZE); |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static int write_tdx_cfg_block_to_nor(unsigned char *config_block) |
| 209 | { |
| 210 | /* Write production parameter config block to NOR flash */ |
| 211 | return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET, |
| 212 | TDX_CFG_BLOCK_MAX_SIZE); |
| 213 | } |
| 214 | #endif |
| 215 | |
| 216 | int read_tdx_cfg_block(void) |
| 217 | { |
| 218 | int ret = 0; |
| 219 | u8 *config_block = NULL; |
| 220 | struct toradex_tag *tag; |
| 221 | size_t size = TDX_CFG_BLOCK_MAX_SIZE; |
| 222 | int offset; |
| 223 | |
| 224 | /* Allocate RAM area for config block */ |
| 225 | config_block = memalign(ARCH_DMA_MINALIGN, size); |
| 226 | if (!config_block) { |
| 227 | printf("Not enough malloc space available!\n"); |
| 228 | return -ENOMEM; |
| 229 | } |
| 230 | |
| 231 | memset(config_block, 0, size); |
| 232 | |
| 233 | #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC) |
| 234 | ret = tdx_cfg_block_mmc_storage(config_block, 0); |
| 235 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND) |
| 236 | ret = read_tdx_cfg_block_from_nand(config_block); |
| 237 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR) |
| 238 | ret = read_tdx_cfg_block_from_nor(config_block); |
| 239 | #else |
| 240 | ret = -EINVAL; |
| 241 | #endif |
| 242 | if (ret) |
| 243 | goto out; |
| 244 | |
| 245 | /* Expect a valid tag first */ |
| 246 | tag = (struct toradex_tag *)config_block; |
| 247 | if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) { |
| 248 | valid_cfgblock = false; |
| 249 | ret = -EINVAL; |
| 250 | goto out; |
| 251 | } |
| 252 | valid_cfgblock = true; |
| 253 | offset = 4; |
| 254 | |
| 255 | while (offset < TDX_CFG_BLOCK_MAX_SIZE) { |
| 256 | tag = (struct toradex_tag *)(config_block + offset); |
| 257 | offset += 4; |
| 258 | if (tag->id == TAG_INVALID) |
| 259 | break; |
| 260 | |
| 261 | if (tag->flags == TAG_FLAG_VALID) { |
| 262 | switch (tag->id) { |
| 263 | case TAG_MAC: |
| 264 | memcpy(&tdx_eth_addr, config_block + offset, |
| 265 | 6); |
| 266 | |
| 267 | /* NIC part of MAC address is serial number */ |
| 268 | tdx_serial = ntohl(tdx_eth_addr.nic) >> 8; |
| 269 | break; |
| 270 | case TAG_HW: |
| 271 | memcpy(&tdx_hw_tag, config_block + offset, 8); |
| 272 | break; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /* Get to next tag according to current tags length */ |
| 277 | offset += tag->len * 4; |
| 278 | } |
| 279 | |
| 280 | /* Cap product id to avoid issues with a yet unknown one */ |
Marcel Ziswiler | ccdd371 | 2019-03-25 17:18:29 +0100 | [diff] [blame] | 281 | if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) / |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 282 | sizeof(toradex_modules[0]))) |
| 283 | tdx_hw_tag.prodid = 0; |
| 284 | |
| 285 | out: |
| 286 | free(config_block); |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | static int get_cfgblock_interactive(void) |
| 291 | { |
| 292 | char message[CONFIG_SYS_CBSIZE]; |
| 293 | char *soc; |
| 294 | char it = 'n'; |
Marcel Ziswiler | d1aa1444 | 2019-07-12 12:35:06 +0200 | [diff] [blame] | 295 | char wb = 'n'; |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 296 | int len; |
| 297 | |
Stefan Agner | 89315f3 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 298 | /* Unknown module by default */ |
| 299 | tdx_hw_tag.prodid = 0; |
| 300 | |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 301 | if (cpu_is_pxa27x()) |
| 302 | sprintf(message, "Is the module the 312 MHz version? [y/N] "); |
| 303 | else |
| 304 | sprintf(message, "Is the module an IT version? [y/N] "); |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 305 | len = cli_readline(message); |
| 306 | it = console_buffer[0]; |
| 307 | |
Marcel Ziswiler | d1aa1444 | 2019-07-12 12:35:06 +0200 | [diff] [blame] | 308 | #if defined(CONFIG_TARGET_APALIS_IMX8) || \ |
| 309 | defined(CONFIG_TARGET_COLIBRI_IMX6ULL) || \ |
| 310 | defined(CONFIG_TARGET_COLIBRI_IMX8X) |
| 311 | sprintf(message, "Does the module have Wi-Fi / Bluetooth? [y/N] "); |
| 312 | len = cli_readline(message); |
| 313 | wb = console_buffer[0]; |
| 314 | #endif |
| 315 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 316 | soc = env_get("soc"); |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 317 | if (!strcmp("mx6", soc)) { |
Stefan Agner | 89315f3 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 318 | #ifdef CONFIG_TARGET_APALIS_IMX6 |
| 319 | if (it == 'y' || it == 'Y') { |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 320 | if (is_cpu_type(MXC_CPU_MX6Q)) |
| 321 | tdx_hw_tag.prodid = APALIS_IMX6Q_IT; |
| 322 | else |
| 323 | tdx_hw_tag.prodid = APALIS_IMX6D_IT; |
Stefan Agner | 89315f3 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 324 | } else { |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 325 | if (is_cpu_type(MXC_CPU_MX6Q)) |
| 326 | tdx_hw_tag.prodid = APALIS_IMX6Q; |
| 327 | else |
| 328 | tdx_hw_tag.prodid = APALIS_IMX6D; |
Stefan Agner | 89315f3 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 329 | } |
| 330 | #elif CONFIG_TARGET_COLIBRI_IMX6 |
| 331 | if (it == 'y' || it == 'Y') { |
| 332 | if (is_cpu_type(MXC_CPU_MX6DL)) |
| 333 | tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT; |
| 334 | else if (is_cpu_type(MXC_CPU_MX6SOLO)) |
| 335 | tdx_hw_tag.prodid = COLIBRI_IMX6S_IT; |
| 336 | } else { |
| 337 | if (is_cpu_type(MXC_CPU_MX6DL)) |
| 338 | tdx_hw_tag.prodid = COLIBRI_IMX6DL; |
| 339 | else if (is_cpu_type(MXC_CPU_MX6SOLO)) |
| 340 | tdx_hw_tag.prodid = COLIBRI_IMX6S; |
| 341 | } |
| 342 | #elif CONFIG_TARGET_COLIBRI_IMX6ULL |
Gerard Salvatella | 08f8055 | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 343 | if (it == 'y' || it == 'Y') { |
Stefan Agner | 89315f3 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 344 | if (wb == 'y' || wb == 'Y') |
| 345 | tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT; |
| 346 | else |
| 347 | tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT; |
Gerard Salvatella | 08f8055 | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 348 | } else { |
Stefan Agner | 89315f3 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 349 | if (wb == 'y' || wb == 'Y') |
| 350 | tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT; |
| 351 | else |
| 352 | tdx_hw_tag.prodid = COLIBRI_IMX6ULL; |
Gerard Salvatella | 08f8055 | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 353 | } |
Stefan Agner | 89315f3 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 354 | #endif |
Gerard Salvatella | 08f8055 | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 355 | } else if (!strcmp("imx7d", soc)) |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 356 | tdx_hw_tag.prodid = COLIBRI_IMX7D; |
Gerard Salvatella | 08f8055 | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 357 | else if (!strcmp("imx7s", soc)) |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 358 | tdx_hw_tag.prodid = COLIBRI_IMX7S; |
Marcel Ziswiler | 9d63c7d | 2019-07-12 12:35:08 +0200 | [diff] [blame^] | 359 | else if (is_cpu_type(MXC_CPU_IMX8QM)) { |
| 360 | if (it == 'y' || it == 'Y') { |
| 361 | if (wb == 'y' || wb == 'Y') |
| 362 | tdx_hw_tag.prodid = APALIS_IMX8QM_WIFI_BT_IT; |
| 363 | else |
| 364 | tdx_hw_tag.prodid = APALIS_IMX8QM_IT; |
| 365 | } else { |
| 366 | if (wb == 'y' || wb == 'Y') |
| 367 | tdx_hw_tag.prodid = APALIS_IMX8QP_WIFI_BT; |
| 368 | else |
| 369 | tdx_hw_tag.prodid = APALIS_IMX8QP; |
| 370 | } |
| 371 | } else if (is_cpu_type(MXC_CPU_IMX8QXP)) { |
Marcel Ziswiler | 6c297ee | 2019-07-12 12:35:07 +0200 | [diff] [blame] | 372 | if (it == 'y' || it == 'Y') { |
| 373 | if (wb == 'y' || wb == 'Y') |
| 374 | tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT; |
| 375 | else |
| 376 | tdx_hw_tag.prodid = COLIBRI_IMX8QXP_IT; |
| 377 | } else { |
| 378 | if (wb == 'y' || wb == 'Y') |
| 379 | tdx_hw_tag.prodid = COLIBRI_IMX8DX_WIFI_BT; |
| 380 | else |
| 381 | tdx_hw_tag.prodid = COLIBRI_IMX8DX; |
| 382 | } |
| 383 | } else if (!strcmp("tegra20", soc)) { |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 384 | if (it == 'y' || it == 'Y') |
| 385 | if (gd->ram_size == 0x10000000) |
| 386 | tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT; |
| 387 | else |
| 388 | tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT; |
| 389 | else |
| 390 | if (gd->ram_size == 0x10000000) |
| 391 | tdx_hw_tag.prodid = COLIBRI_T20_256MB; |
| 392 | else |
| 393 | tdx_hw_tag.prodid = COLIBRI_T20_512MB; |
| 394 | } else if (cpu_is_pxa27x()) { |
| 395 | if (it == 'y' || it == 'Y') |
| 396 | tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ; |
| 397 | else |
| 398 | tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ; |
Gerard Salvatella | 08f8055 | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 399 | } |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 400 | #ifdef CONFIG_MACH_TYPE |
Gerard Salvatella | 08f8055 | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 401 | else if (!strcmp("tegra30", soc)) { |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 402 | if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) { |
| 403 | if (it == 'y' || it == 'Y') |
| 404 | tdx_hw_tag.prodid = APALIS_T30_IT; |
| 405 | else |
| 406 | if (gd->ram_size == 0x40000000) |
| 407 | tdx_hw_tag.prodid = APALIS_T30_1GB; |
| 408 | else |
| 409 | tdx_hw_tag.prodid = APALIS_T30_2GB; |
| 410 | } else { |
| 411 | if (it == 'y' || it == 'Y') |
| 412 | tdx_hw_tag.prodid = COLIBRI_T30_IT; |
| 413 | else |
| 414 | tdx_hw_tag.prodid = COLIBRI_T30; |
| 415 | } |
Gerard Salvatella | 08f8055 | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 416 | } |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 417 | #endif /* CONFIG_MACH_TYPE */ |
Gerard Salvatella | 08f8055 | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 418 | else if (!strcmp("tegra124", soc)) { |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 419 | tdx_hw_tag.prodid = APALIS_TK1_2GB; |
| 420 | } else if (!strcmp("vf500", soc)) { |
| 421 | if (it == 'y' || it == 'Y') |
| 422 | tdx_hw_tag.prodid = COLIBRI_VF50_IT; |
| 423 | else |
| 424 | tdx_hw_tag.prodid = COLIBRI_VF50; |
| 425 | } else if (!strcmp("vf610", soc)) { |
| 426 | if (it == 'y' || it == 'Y') |
| 427 | tdx_hw_tag.prodid = COLIBRI_VF61_IT; |
| 428 | else |
| 429 | tdx_hw_tag.prodid = COLIBRI_VF61; |
Stefan Agner | 89315f3 | 2019-04-09 17:24:08 +0200 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | if (!tdx_hw_tag.prodid) { |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 433 | printf("Module type not detectable due to unknown SoC\n"); |
| 434 | return -1; |
| 435 | } |
| 436 | |
| 437 | while (len < 4) { |
| 438 | sprintf(message, "Enter the module version (e.g. V1.1B): V"); |
| 439 | len = cli_readline(message); |
| 440 | } |
| 441 | |
| 442 | tdx_hw_tag.ver_major = console_buffer[0] - '0'; |
| 443 | tdx_hw_tag.ver_minor = console_buffer[2] - '0'; |
| 444 | tdx_hw_tag.ver_assembly = console_buffer[3] - 'A'; |
| 445 | |
Gerard Salvatella | 08f8055 | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 446 | if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1) |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 447 | tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ - |
| 448 | COLIBRI_PXA270_V1_312MHZ); |
| 449 | |
| 450 | while (len < 8) { |
| 451 | sprintf(message, "Enter module serial number: "); |
| 452 | len = cli_readline(message); |
| 453 | } |
| 454 | |
| 455 | tdx_serial = simple_strtoul(console_buffer, NULL, 10); |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | static int get_cfgblock_barcode(char *barcode) |
| 461 | { |
| 462 | if (strlen(barcode) < 16) { |
| 463 | printf("Argument too short, barcode is 16 chars long\n"); |
| 464 | return -1; |
| 465 | } |
| 466 | |
| 467 | /* Get hardware information from the first 8 digits */ |
| 468 | tdx_hw_tag.ver_major = barcode[4] - '0'; |
| 469 | tdx_hw_tag.ver_minor = barcode[5] - '0'; |
| 470 | tdx_hw_tag.ver_assembly = barcode[7] - '0'; |
| 471 | |
| 472 | barcode[4] = '\0'; |
| 473 | tdx_hw_tag.prodid = simple_strtoul(barcode, NULL, 10); |
| 474 | |
| 475 | /* Parse second part of the barcode (serial number */ |
| 476 | barcode += 8; |
| 477 | tdx_serial = simple_strtoul(barcode, NULL, 10); |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | static int do_cfgblock_create(cmd_tbl_t *cmdtp, int flag, int argc, |
| 483 | char * const argv[]) |
| 484 | { |
| 485 | u8 *config_block; |
| 486 | struct toradex_tag *tag; |
| 487 | size_t size = TDX_CFG_BLOCK_MAX_SIZE; |
| 488 | int offset = 0; |
| 489 | int ret = CMD_RET_SUCCESS; |
| 490 | int err; |
Dominik Sliwa | 587b13c | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 491 | int force_overwrite = 0; |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 492 | |
| 493 | /* Allocate RAM area for config block */ |
| 494 | config_block = memalign(ARCH_DMA_MINALIGN, size); |
| 495 | if (!config_block) { |
| 496 | printf("Not enough malloc space available!\n"); |
| 497 | return CMD_RET_FAILURE; |
| 498 | } |
| 499 | |
| 500 | memset(config_block, 0xff, size); |
| 501 | |
Dominik Sliwa | 587b13c | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 502 | if (argc >= 3) { |
| 503 | if (argv[2][0] == '-' && argv[2][1] == 'y') |
| 504 | force_overwrite = 1; |
| 505 | } |
| 506 | |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 507 | read_tdx_cfg_block(); |
| 508 | if (valid_cfgblock) { |
| 509 | #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND) |
| 510 | /* |
| 511 | * On NAND devices, recreation is only allowed if the page is |
| 512 | * empty (config block invalid...) |
| 513 | */ |
Gerard Salvatella | 08f8055 | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 514 | printf("NAND erase block %d need to be erased before creating" \ |
| 515 | " a Toradex config block\n", |
Grygorii Strashko | bf264cd | 2017-06-26 19:13:06 -0500 | [diff] [blame] | 516 | CONFIG_TDX_CFG_BLOCK_OFFSET / |
| 517 | get_nand_dev_by_index(0)->erasesize); |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 518 | goto out; |
| 519 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR) |
| 520 | /* |
| 521 | * On NOR devices, recreation is only allowed if the sector is |
| 522 | * empty and write protection is off (config block invalid...) |
| 523 | */ |
Gerard Salvatella | 08f8055 | 2019-04-09 17:24:07 +0200 | [diff] [blame] | 524 | printf("NOR sector at offset 0x%02x need to be erased and " \ |
| 525 | "unprotected before creating a Toradex config block\n", |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 526 | CONFIG_TDX_CFG_BLOCK_OFFSET); |
| 527 | goto out; |
| 528 | #else |
Dominik Sliwa | 587b13c | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 529 | if (!force_overwrite) { |
| 530 | char message[CONFIG_SYS_CBSIZE]; |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 531 | |
Dominik Sliwa | 587b13c | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 532 | sprintf(message, |
| 533 | "A valid Toradex config block is present, still recreate? [y/N] "); |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 534 | |
Dominik Sliwa | 587b13c | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 535 | if (!cli_readline(message)) |
| 536 | goto out; |
| 537 | |
| 538 | if (console_buffer[0] != 'y' && |
| 539 | console_buffer[0] != 'Y') |
| 540 | goto out; |
| 541 | } |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 542 | #endif |
| 543 | } |
| 544 | |
| 545 | /* Parse new Toradex config block data... */ |
Dominik Sliwa | 587b13c | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 546 | if (argc < 3 || (force_overwrite && argc < 4)) { |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 547 | err = get_cfgblock_interactive(); |
Dominik Sliwa | 587b13c | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 548 | } else { |
| 549 | if (force_overwrite) |
| 550 | err = get_cfgblock_barcode(argv[3]); |
| 551 | else |
| 552 | err = get_cfgblock_barcode(argv[2]); |
| 553 | } |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 554 | if (err) { |
| 555 | ret = CMD_RET_FAILURE; |
| 556 | goto out; |
| 557 | } |
| 558 | |
| 559 | /* Convert serial number to MAC address (the storage format) */ |
| 560 | tdx_eth_addr.oui = htonl(0x00142dUL << 8); |
| 561 | tdx_eth_addr.nic = htonl(tdx_serial << 8); |
| 562 | |
| 563 | /* Valid Tag */ |
| 564 | tag = (struct toradex_tag *)config_block; |
| 565 | tag->id = TAG_VALID; |
| 566 | tag->flags = TAG_FLAG_VALID; |
| 567 | tag->len = 0; |
| 568 | offset += 4; |
| 569 | |
| 570 | /* Product Tag */ |
| 571 | tag = (struct toradex_tag *)(config_block + offset); |
| 572 | tag->id = TAG_HW; |
| 573 | tag->flags = TAG_FLAG_VALID; |
| 574 | tag->len = 2; |
| 575 | offset += 4; |
| 576 | |
| 577 | memcpy(config_block + offset, &tdx_hw_tag, 8); |
| 578 | offset += 8; |
| 579 | |
| 580 | /* MAC Tag */ |
| 581 | tag = (struct toradex_tag *)(config_block + offset); |
| 582 | tag->id = TAG_MAC; |
| 583 | tag->flags = TAG_FLAG_VALID; |
| 584 | tag->len = 2; |
| 585 | offset += 4; |
| 586 | |
| 587 | memcpy(config_block + offset, &tdx_eth_addr, 6); |
| 588 | offset += 6; |
| 589 | memset(config_block + offset, 0, 32 - offset); |
| 590 | |
| 591 | #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC) |
| 592 | err = tdx_cfg_block_mmc_storage(config_block, 1); |
| 593 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND) |
| 594 | err = write_tdx_cfg_block_to_nand(config_block); |
| 595 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR) |
| 596 | err = write_tdx_cfg_block_to_nor(config_block); |
| 597 | #else |
| 598 | err = -EINVAL; |
| 599 | #endif |
| 600 | if (err) { |
| 601 | printf("Failed to write Toradex config block: %d\n", ret); |
| 602 | ret = CMD_RET_FAILURE; |
| 603 | goto out; |
| 604 | } |
| 605 | |
| 606 | printf("Toradex config block successfully written\n"); |
| 607 | |
| 608 | out: |
| 609 | free(config_block); |
| 610 | return ret; |
| 611 | } |
| 612 | |
| 613 | static int do_cfgblock(cmd_tbl_t *cmdtp, int flag, int argc, |
| 614 | char * const argv[]) |
| 615 | { |
| 616 | int ret; |
| 617 | |
| 618 | if (argc < 2) |
| 619 | return CMD_RET_USAGE; |
| 620 | |
| 621 | if (!strcmp(argv[1], "create")) { |
| 622 | return do_cfgblock_create(cmdtp, flag, argc, argv); |
| 623 | } else if (!strcmp(argv[1], "reload")) { |
| 624 | ret = read_tdx_cfg_block(); |
| 625 | if (ret) { |
| 626 | printf("Failed to reload Toradex config block: %d\n", |
| 627 | ret); |
| 628 | return CMD_RET_FAILURE; |
| 629 | } |
| 630 | return CMD_RET_SUCCESS; |
| 631 | } |
| 632 | |
| 633 | return CMD_RET_USAGE; |
| 634 | } |
| 635 | |
| 636 | U_BOOT_CMD( |
Dominik Sliwa | 587b13c | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 637 | cfgblock, 4, 0, do_cfgblock, |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 638 | "Toradex config block handling commands", |
Dominik Sliwa | 587b13c | 2019-03-25 17:18:27 +0100 | [diff] [blame] | 639 | "create [-y] [barcode] - (Re-)create Toradex config block\n" |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 640 | "cfgblock reload - Reload Toradex config block from flash" |
| 641 | ); |