blob: 75216ecf6d3979acf14ad5e67e54e32872f77d92 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01002/*
Marcel Ziswiler75b93272020-01-28 14:42:23 +01003 * Copyright (c) 2016-2020 Toradex
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01004 */
5
6#include <common.h>
7#include "tdx-cfg-block.h"
Simon Glassed38aef2020-05-10 11:40:03 -06008#include <command.h>
Simon Glass274e0b02020-05-10 11:39:56 -06009#include <asm/cache.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010010
Marcel Ziswiler2658c8e2019-04-09 17:25:32 +020011#if defined(CONFIG_TARGET_APALIS_IMX6) || \
Marcel Ziswiler00320612019-07-12 12:35:08 +020012 defined(CONFIG_TARGET_APALIS_IMX8) || \
Marcel Ziswiler75b93272020-01-28 14:42:23 +010013 defined(CONFIG_TARGET_APALIS_IMX8X) || \
Marcel Ziswiler2658c8e2019-04-09 17:25:32 +020014 defined(CONFIG_TARGET_COLIBRI_IMX6) || \
Marcel Ziswilereca26ba2020-01-28 14:42:24 +010015 defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
16 defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
17 defined(CONFIG_TARGET_VERDIN_IMX8MN)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010018#include <asm/arch/sys_proto.h>
19#else
20#define is_cpu_type(cpu) (0)
21#endif
22#if defined(CONFIG_CPU_PXA27X)
23#include <asm/arch-pxa/pxa.h>
24#else
25#define cpu_is_pxa27x(cpu) (0)
26#endif
27#include <cli.h>
28#include <console.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060029#include <env.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010030#include <flash.h>
31#include <malloc.h>
32#include <mmc.h>
33#include <nand.h>
Simon Glass0ffb9d62017-05-31 19:47:48 -060034#include <asm/mach-types.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010035
36DECLARE_GLOBAL_DATA_PTR;
37
38#define TAG_VALID 0xcf01
39#define TAG_MAC 0x0000
40#define TAG_HW 0x0008
41#define TAG_INVALID 0xffff
42
43#define TAG_FLAG_VALID 0x1
44
45#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
46#define TDX_CFG_BLOCK_MAX_SIZE 512
47#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
48#define TDX_CFG_BLOCK_MAX_SIZE 64
49#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
50#define TDX_CFG_BLOCK_MAX_SIZE 64
51#else
52#error Toradex config block location not set
53#endif
54
55struct toradex_tag {
56 u32 len:14;
57 u32 flags:2;
58 u32 id:16;
59};
60
61bool valid_cfgblock;
62struct toradex_hw tdx_hw_tag;
63struct toradex_eth_addr tdx_eth_addr;
64u32 tdx_serial;
65
66const char * const toradex_modules[] = {
67 [0] = "UNKNOWN MODULE",
68 [1] = "Colibri PXA270 312MHz",
69 [2] = "Colibri PXA270 520MHz",
70 [3] = "Colibri PXA320 806MHz",
71 [4] = "Colibri PXA300 208MHz",
72 [5] = "Colibri PXA310 624MHz",
73 [6] = "Colibri PXA320 806MHz IT",
74 [7] = "Colibri PXA300 208MHz XT",
75 [8] = "Colibri PXA270 312MHz",
76 [9] = "Colibri PXA270 520MHz",
77 [10] = "Colibri VF50 128MB", /* not currently on sale */
78 [11] = "Colibri VF61 256MB",
79 [12] = "Colibri VF61 256MB IT",
80 [13] = "Colibri VF50 128MB IT",
81 [14] = "Colibri iMX6 Solo 256MB",
82 [15] = "Colibri iMX6 DualLite 512MB",
83 [16] = "Colibri iMX6 Solo 256MB IT",
84 [17] = "Colibri iMX6 DualLite 512MB IT",
85 [18] = "UNKNOWN MODULE",
86 [19] = "UNKNOWN MODULE",
87 [20] = "Colibri T20 256MB",
88 [21] = "Colibri T20 512MB",
89 [22] = "Colibri T20 512MB IT",
90 [23] = "Colibri T30 1GB",
91 [24] = "Colibri T20 256MB IT",
92 [25] = "Apalis T30 2GB",
93 [26] = "Apalis T30 1GB",
94 [27] = "Apalis iMX6 Quad 1GB",
95 [28] = "Apalis iMX6 Quad 2GB IT",
96 [29] = "Apalis iMX6 Dual 512MB",
97 [30] = "Colibri T30 1GB IT",
98 [31] = "Apalis T30 1GB IT",
99 [32] = "Colibri iMX7 Solo 256MB",
100 [33] = "Colibri iMX7 Dual 512MB",
101 [34] = "Apalis TK1 2GB",
102 [35] = "Apalis iMX6 Dual 1GB IT",
Stefan Agner01875e92018-05-30 19:01:47 +0200103 [36] = "Colibri iMX6ULL 256MB",
Marcel Ziswiler2658c8e2019-04-09 17:25:32 +0200104 [37] = "Apalis iMX8 QuadMax 4GB Wi-Fi / BT IT",
105 [38] = "Colibri iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
Stefan Agner01875e92018-05-30 19:01:47 +0200106 [39] = "Colibri iMX7 Dual 1GB (eMMC)",
Marcel Ziswiler2658c8e2019-04-09 17:25:32 +0200107 [40] = "Colibri iMX6ULL 512MB Wi-Fi / BT IT",
Stefan Agner01875e92018-05-30 19:01:47 +0200108 [41] = "Colibri iMX7 Dual 512MB EPDC",
109 [42] = "Apalis TK1 4GB",
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200110 [43] = "Colibri T20 512MB IT SETEK",
111 [44] = "Colibri iMX6ULL 512MB IT",
112 [45] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth",
Marcel Ziswilerf60ffec2019-04-09 17:25:33 +0200113 [46] = "Apalis iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
114 [47] = "Apalis iMX8 QuadMax 4GB IT",
115 [48] = "Apalis iMX8 QuadPlus 2GB Wi-Fi / BT",
116 [49] = "Apalis iMX8 QuadPlus 2GB",
117 [50] = "Colibri iMX8 QuadXPlus 2GB IT",
118 [51] = "Colibri iMX8 DualX 1GB Wi-Fi / Bluetooth",
119 [52] = "Colibri iMX8 DualX 1GB",
Marcel Ziswiler75b93272020-01-28 14:42:23 +0100120 [53] = "Apalis iMX8 QuadXPlus 2GB ECC IT",
121 [54] = "Apalis iMX8 DualXPlus 1GB",
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100122 [55] = "Verdin iMX8M Mini Quad 2GB Wi-Fi / BT IT",
123 [56] = "Verdin iMX8M Nano SoloLite 1GB", /* not currently on sale */
124 [57] = "Verdin iMX8M Mini DualLite 1GB",
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100125};
126
127#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
128static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
129{
130 struct mmc *mmc;
131 int dev = CONFIG_TDX_CFG_BLOCK_DEV;
132 int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
133 uint part = CONFIG_TDX_CFG_BLOCK_PART;
134 uint blk_start;
135 int ret = 0;
136
137 /* Read production parameter config block from eMMC */
138 mmc = find_mmc_device(dev);
139 if (!mmc) {
140 puts("No MMC card found\n");
141 ret = -ENODEV;
142 goto out;
143 }
Stefan Agnerdd202342019-07-12 12:35:05 +0200144 if (mmc_init(mmc)) {
145 puts("MMC init failed\n");
146 return -EINVAL;
147 }
Simon Glass8c4c5c82017-04-23 20:02:11 -0600148 if (part != mmc_get_blk_desc(mmc)->hwpart) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100149 if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) {
150 puts("MMC partition switch failed\n");
151 ret = -ENODEV;
152 goto out;
153 }
154 }
155 if (offset < 0)
156 offset += mmc->capacity;
157 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
158
159 if (!write) {
160 /* Careful reads a whole block of 512 bytes into config_block */
161 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
162 (unsigned char *)config_block) != 1) {
163 ret = -EIO;
164 goto out;
165 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100166 } else {
167 /* Just writing one 512 byte block */
168 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
169 (unsigned char *)config_block) != 1) {
170 ret = -EIO;
171 goto out;
172 }
173 }
174
175out:
176 /* Switch back to regular eMMC user partition */
177 blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
178
179 return ret;
180}
181#endif
182
183#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
184static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
185{
186 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
Stefan Agner8843b6d2018-08-06 09:19:18 +0200187 struct mtd_info *mtd = get_nand_dev_by_index(0);
188
189 if (!mtd)
190 return -ENODEV;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100191
192 /* Read production parameter config block from NAND page */
Stefan Agner8843b6d2018-08-06 09:19:18 +0200193 return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
Grygorii Strashkobb314622017-06-26 19:13:06 -0500194 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
195 config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100196}
197
198static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
199{
200 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
201
202 /* Write production parameter config block to NAND page */
Grygorii Strashkobb314622017-06-26 19:13:06 -0500203 return nand_write_skip_bad(get_nand_dev_by_index(0),
204 CONFIG_TDX_CFG_BLOCK_OFFSET,
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100205 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
206 config_block, WITH_WR_VERIFY);
207}
208#endif
209
210#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
211static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
212{
213 /* Read production parameter config block from NOR flash */
214 memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
215 TDX_CFG_BLOCK_MAX_SIZE);
216 return 0;
217}
218
219static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
220{
221 /* Write production parameter config block to NOR flash */
222 return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
223 TDX_CFG_BLOCK_MAX_SIZE);
224}
225#endif
226
227int read_tdx_cfg_block(void)
228{
229 int ret = 0;
230 u8 *config_block = NULL;
231 struct toradex_tag *tag;
232 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
233 int offset;
234
235 /* Allocate RAM area for config block */
236 config_block = memalign(ARCH_DMA_MINALIGN, size);
237 if (!config_block) {
238 printf("Not enough malloc space available!\n");
239 return -ENOMEM;
240 }
241
242 memset(config_block, 0, size);
243
244#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
245 ret = tdx_cfg_block_mmc_storage(config_block, 0);
246#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
247 ret = read_tdx_cfg_block_from_nand(config_block);
248#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
249 ret = read_tdx_cfg_block_from_nor(config_block);
250#else
251 ret = -EINVAL;
252#endif
253 if (ret)
254 goto out;
255
256 /* Expect a valid tag first */
257 tag = (struct toradex_tag *)config_block;
258 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
259 valid_cfgblock = false;
260 ret = -EINVAL;
261 goto out;
262 }
263 valid_cfgblock = true;
264 offset = 4;
265
266 while (offset < TDX_CFG_BLOCK_MAX_SIZE) {
267 tag = (struct toradex_tag *)(config_block + offset);
268 offset += 4;
269 if (tag->id == TAG_INVALID)
270 break;
271
272 if (tag->flags == TAG_FLAG_VALID) {
273 switch (tag->id) {
274 case TAG_MAC:
275 memcpy(&tdx_eth_addr, config_block + offset,
276 6);
277
278 /* NIC part of MAC address is serial number */
279 tdx_serial = ntohl(tdx_eth_addr.nic) >> 8;
280 break;
281 case TAG_HW:
282 memcpy(&tdx_hw_tag, config_block + offset, 8);
283 break;
284 }
285 }
286
287 /* Get to next tag according to current tags length */
288 offset += tag->len * 4;
289 }
290
291 /* Cap product id to avoid issues with a yet unknown one */
Marcel Ziswiler8c9127c2019-03-25 17:18:29 +0100292 if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) /
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100293 sizeof(toradex_modules[0])))
294 tdx_hw_tag.prodid = 0;
295
296out:
297 free(config_block);
298 return ret;
299}
300
301static int get_cfgblock_interactive(void)
302{
303 char message[CONFIG_SYS_CBSIZE];
304 char *soc;
305 char it = 'n';
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200306 char wb = 'n';
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100307 int len = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100308
Stefan Agner68f58782019-04-09 17:24:08 +0200309 /* Unknown module by default */
310 tdx_hw_tag.prodid = 0;
311
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100312 if (cpu_is_pxa27x())
313 sprintf(message, "Is the module the 312 MHz version? [y/N] ");
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100314#if !defined(CONFIG_TARGET_VERDIN_IMX8MM) || !defined(CONFIG_TARGET_VERDIN_IMX8MN)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100315 else
316 sprintf(message, "Is the module an IT version? [y/N] ");
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100317
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100318 len = cli_readline(message);
319 it = console_buffer[0];
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100320#else
321 else
322 it = 'y';
323#endif
324
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100325
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200326#if defined(CONFIG_TARGET_APALIS_IMX8) || \
Marcel Ziswiler75b93272020-01-28 14:42:23 +0100327 defined(CONFIG_TARGET_APALIS_IMX8X) || \
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200328 defined(CONFIG_TARGET_COLIBRI_IMX6ULL) || \
329 defined(CONFIG_TARGET_COLIBRI_IMX8X)
330 sprintf(message, "Does the module have Wi-Fi / Bluetooth? [y/N] ");
331 len = cli_readline(message);
332 wb = console_buffer[0];
333#endif
334
Simon Glass64b723f2017-08-03 12:22:12 -0600335 soc = env_get("soc");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100336 if (!strcmp("mx6", soc)) {
Stefan Agner68f58782019-04-09 17:24:08 +0200337#ifdef CONFIG_TARGET_APALIS_IMX6
338 if (it == 'y' || it == 'Y') {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100339 if (is_cpu_type(MXC_CPU_MX6Q))
340 tdx_hw_tag.prodid = APALIS_IMX6Q_IT;
341 else
342 tdx_hw_tag.prodid = APALIS_IMX6D_IT;
Stefan Agner68f58782019-04-09 17:24:08 +0200343 } else {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100344 if (is_cpu_type(MXC_CPU_MX6Q))
345 tdx_hw_tag.prodid = APALIS_IMX6Q;
346 else
347 tdx_hw_tag.prodid = APALIS_IMX6D;
Stefan Agner68f58782019-04-09 17:24:08 +0200348 }
349#elif CONFIG_TARGET_COLIBRI_IMX6
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200350 if (it == 'y' || it == 'Y') {
Stefan Agner68f58782019-04-09 17:24:08 +0200351 if (is_cpu_type(MXC_CPU_MX6DL))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100352 tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT;
Stefan Agner68f58782019-04-09 17:24:08 +0200353 else if (is_cpu_type(MXC_CPU_MX6SOLO))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100354 tdx_hw_tag.prodid = COLIBRI_IMX6S_IT;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200355 } else {
Stefan Agner68f58782019-04-09 17:24:08 +0200356 if (is_cpu_type(MXC_CPU_MX6DL))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100357 tdx_hw_tag.prodid = COLIBRI_IMX6DL;
Stefan Agner68f58782019-04-09 17:24:08 +0200358 else if (is_cpu_type(MXC_CPU_MX6SOLO))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100359 tdx_hw_tag.prodid = COLIBRI_IMX6S;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200360 }
Stefan Agner68f58782019-04-09 17:24:08 +0200361#elif CONFIG_TARGET_COLIBRI_IMX6ULL
Stefan Agner68f58782019-04-09 17:24:08 +0200362 if (it == 'y' || it == 'Y') {
363 if (wb == 'y' || wb == 'Y')
364 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT;
365 else
366 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT;
367 } else {
368 if (wb == 'y' || wb == 'Y')
369 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT;
370 else
371 tdx_hw_tag.prodid = COLIBRI_IMX6ULL;
372 }
373#endif
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200374 } else if (!strcmp("imx7d", soc))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100375 tdx_hw_tag.prodid = COLIBRI_IMX7D;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200376 else if (!strcmp("imx7s", soc))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100377 tdx_hw_tag.prodid = COLIBRI_IMX7S;
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100378 else if (is_cpu_type(MXC_CPU_IMX8MM))
379 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_WIFI_BT_IT;
380 else if (is_cpu_type(MXC_CPU_IMX8MMDL))
381 tdx_hw_tag.prodid = VERDIN_IMX8MMDL;
382 else if (is_cpu_type(MXC_CPU_IMX8MN))
383 tdx_hw_tag.prodid = VERDIN_IMX8MNSL;
Marcel Ziswiler00320612019-07-12 12:35:08 +0200384 else if (is_cpu_type(MXC_CPU_IMX8QM)) {
385 if (it == 'y' || it == 'Y') {
386 if (wb == 'y' || wb == 'Y')
387 tdx_hw_tag.prodid = APALIS_IMX8QM_WIFI_BT_IT;
388 else
389 tdx_hw_tag.prodid = APALIS_IMX8QM_IT;
390 } else {
391 if (wb == 'y' || wb == 'Y')
392 tdx_hw_tag.prodid = APALIS_IMX8QP_WIFI_BT;
393 else
394 tdx_hw_tag.prodid = APALIS_IMX8QP;
395 }
396 } else if (is_cpu_type(MXC_CPU_IMX8QXP)) {
Marcel Ziswiler75b93272020-01-28 14:42:23 +0100397#ifdef CONFIG_TARGET_APALIS_IMX8X
398 if (it == 'y' || it == 'Y' || wb == 'y' || wb == 'Y') {
399 tdx_hw_tag.prodid = APALIS_IMX8QXP_WIFI_BT_IT;
400 } else {
401 if (gd->ram_size == 0x40000000)
402 tdx_hw_tag.prodid = APALIS_IMX8DXP;
403 else
404 tdx_hw_tag.prodid = APALIS_IMX8QXP;
405 }
406#elif CONFIG_TARGET_COLIBRI_IMX8X
Marcel Ziswiler7e2954f2019-07-12 12:35:07 +0200407 if (it == 'y' || it == 'Y') {
408 if (wb == 'y' || wb == 'Y')
409 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT;
410 else
411 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_IT;
412 } else {
413 if (wb == 'y' || wb == 'Y')
414 tdx_hw_tag.prodid = COLIBRI_IMX8DX_WIFI_BT;
415 else
416 tdx_hw_tag.prodid = COLIBRI_IMX8DX;
417 }
Marcel Ziswiler75b93272020-01-28 14:42:23 +0100418#endif
Marcel Ziswiler7e2954f2019-07-12 12:35:07 +0200419 } else if (!strcmp("tegra20", soc)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100420 if (it == 'y' || it == 'Y')
421 if (gd->ram_size == 0x10000000)
422 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
423 else
424 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
425 else
426 if (gd->ram_size == 0x10000000)
427 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
428 else
429 tdx_hw_tag.prodid = COLIBRI_T20_512MB;
430 } else if (cpu_is_pxa27x()) {
431 if (it == 'y' || it == 'Y')
432 tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ;
433 else
434 tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200435 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100436#ifdef CONFIG_MACH_TYPE
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200437 else if (!strcmp("tegra30", soc)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100438 if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) {
439 if (it == 'y' || it == 'Y')
440 tdx_hw_tag.prodid = APALIS_T30_IT;
441 else
442 if (gd->ram_size == 0x40000000)
443 tdx_hw_tag.prodid = APALIS_T30_1GB;
444 else
445 tdx_hw_tag.prodid = APALIS_T30_2GB;
446 } else {
447 if (it == 'y' || it == 'Y')
448 tdx_hw_tag.prodid = COLIBRI_T30_IT;
449 else
450 tdx_hw_tag.prodid = COLIBRI_T30;
451 }
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200452 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100453#endif /* CONFIG_MACH_TYPE */
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200454 else if (!strcmp("tegra124", soc)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100455 tdx_hw_tag.prodid = APALIS_TK1_2GB;
456 } else if (!strcmp("vf500", soc)) {
457 if (it == 'y' || it == 'Y')
458 tdx_hw_tag.prodid = COLIBRI_VF50_IT;
459 else
460 tdx_hw_tag.prodid = COLIBRI_VF50;
461 } else if (!strcmp("vf610", soc)) {
462 if (it == 'y' || it == 'Y')
463 tdx_hw_tag.prodid = COLIBRI_VF61_IT;
464 else
465 tdx_hw_tag.prodid = COLIBRI_VF61;
Stefan Agner68f58782019-04-09 17:24:08 +0200466 }
467
468 if (!tdx_hw_tag.prodid) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100469 printf("Module type not detectable due to unknown SoC\n");
470 return -1;
471 }
472
473 while (len < 4) {
474 sprintf(message, "Enter the module version (e.g. V1.1B): V");
475 len = cli_readline(message);
476 }
477
478 tdx_hw_tag.ver_major = console_buffer[0] - '0';
479 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
480 tdx_hw_tag.ver_assembly = console_buffer[3] - 'A';
481
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200482 if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100483 tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
484 COLIBRI_PXA270_V1_312MHZ);
485
486 while (len < 8) {
487 sprintf(message, "Enter module serial number: ");
488 len = cli_readline(message);
489 }
490
491 tdx_serial = simple_strtoul(console_buffer, NULL, 10);
492
493 return 0;
494}
495
496static int get_cfgblock_barcode(char *barcode)
497{
498 if (strlen(barcode) < 16) {
499 printf("Argument too short, barcode is 16 chars long\n");
500 return -1;
501 }
502
503 /* Get hardware information from the first 8 digits */
504 tdx_hw_tag.ver_major = barcode[4] - '0';
505 tdx_hw_tag.ver_minor = barcode[5] - '0';
506 tdx_hw_tag.ver_assembly = barcode[7] - '0';
507
508 barcode[4] = '\0';
509 tdx_hw_tag.prodid = simple_strtoul(barcode, NULL, 10);
510
511 /* Parse second part of the barcode (serial number */
512 barcode += 8;
513 tdx_serial = simple_strtoul(barcode, NULL, 10);
514
515 return 0;
516}
517
Simon Glassed38aef2020-05-10 11:40:03 -0600518static int do_cfgblock_create(struct cmd_tbl *cmdtp, int flag, int argc,
519 char *const argv[])
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100520{
521 u8 *config_block;
522 struct toradex_tag *tag;
523 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
524 int offset = 0;
525 int ret = CMD_RET_SUCCESS;
526 int err;
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100527 int force_overwrite = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100528
529 /* Allocate RAM area for config block */
530 config_block = memalign(ARCH_DMA_MINALIGN, size);
531 if (!config_block) {
532 printf("Not enough malloc space available!\n");
533 return CMD_RET_FAILURE;
534 }
535
536 memset(config_block, 0xff, size);
537
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100538 if (argc >= 3) {
539 if (argv[2][0] == '-' && argv[2][1] == 'y')
540 force_overwrite = 1;
541 }
542
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100543 read_tdx_cfg_block();
544 if (valid_cfgblock) {
545#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
546 /*
547 * On NAND devices, recreation is only allowed if the page is
548 * empty (config block invalid...)
549 */
Marcel Ziswiler6cf2f782019-07-12 12:35:09 +0200550 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
Grygorii Strashkobb314622017-06-26 19:13:06 -0500551 CONFIG_TDX_CFG_BLOCK_OFFSET /
552 get_nand_dev_by_index(0)->erasesize);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100553 goto out;
554#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
555 /*
556 * On NOR devices, recreation is only allowed if the sector is
557 * empty and write protection is off (config block invalid...)
558 */
Marcel Ziswiler6cf2f782019-07-12 12:35:09 +0200559 printf("NOR sector at offset 0x%02x need to be erased and unprotected before creating a Toradex config block\n",
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100560 CONFIG_TDX_CFG_BLOCK_OFFSET);
561 goto out;
562#else
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100563 if (!force_overwrite) {
564 char message[CONFIG_SYS_CBSIZE];
565
566 sprintf(message,
567 "A valid Toradex config block is present, still recreate? [y/N] ");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100568
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100569 if (!cli_readline(message))
570 goto out;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100571
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100572 if (console_buffer[0] != 'y' &&
573 console_buffer[0] != 'Y')
574 goto out;
575 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100576#endif
577 }
578
579 /* Parse new Toradex config block data... */
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100580 if (argc < 3 || (force_overwrite && argc < 4)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100581 err = get_cfgblock_interactive();
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100582 } else {
583 if (force_overwrite)
584 err = get_cfgblock_barcode(argv[3]);
585 else
586 err = get_cfgblock_barcode(argv[2]);
587 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100588 if (err) {
589 ret = CMD_RET_FAILURE;
590 goto out;
591 }
592
593 /* Convert serial number to MAC address (the storage format) */
594 tdx_eth_addr.oui = htonl(0x00142dUL << 8);
595 tdx_eth_addr.nic = htonl(tdx_serial << 8);
596
597 /* Valid Tag */
598 tag = (struct toradex_tag *)config_block;
599 tag->id = TAG_VALID;
600 tag->flags = TAG_FLAG_VALID;
601 tag->len = 0;
602 offset += 4;
603
604 /* Product Tag */
605 tag = (struct toradex_tag *)(config_block + offset);
606 tag->id = TAG_HW;
607 tag->flags = TAG_FLAG_VALID;
608 tag->len = 2;
609 offset += 4;
610
611 memcpy(config_block + offset, &tdx_hw_tag, 8);
612 offset += 8;
613
614 /* MAC Tag */
615 tag = (struct toradex_tag *)(config_block + offset);
616 tag->id = TAG_MAC;
617 tag->flags = TAG_FLAG_VALID;
618 tag->len = 2;
619 offset += 4;
620
621 memcpy(config_block + offset, &tdx_eth_addr, 6);
622 offset += 6;
623 memset(config_block + offset, 0, 32 - offset);
624
625#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
626 err = tdx_cfg_block_mmc_storage(config_block, 1);
627#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
628 err = write_tdx_cfg_block_to_nand(config_block);
629#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
630 err = write_tdx_cfg_block_to_nor(config_block);
631#else
632 err = -EINVAL;
633#endif
634 if (err) {
635 printf("Failed to write Toradex config block: %d\n", ret);
636 ret = CMD_RET_FAILURE;
637 goto out;
638 }
639
640 printf("Toradex config block successfully written\n");
641
642out:
643 free(config_block);
644 return ret;
645}
646
Simon Glassed38aef2020-05-10 11:40:03 -0600647static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc,
648 char *const argv[])
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100649{
650 int ret;
651
652 if (argc < 2)
653 return CMD_RET_USAGE;
654
655 if (!strcmp(argv[1], "create")) {
656 return do_cfgblock_create(cmdtp, flag, argc, argv);
657 } else if (!strcmp(argv[1], "reload")) {
658 ret = read_tdx_cfg_block();
659 if (ret) {
660 printf("Failed to reload Toradex config block: %d\n",
661 ret);
662 return CMD_RET_FAILURE;
663 }
664 return CMD_RET_SUCCESS;
665 }
666
667 return CMD_RET_USAGE;
668}
669
Marcel Ziswiler6cf2f782019-07-12 12:35:09 +0200670U_BOOT_CMD(cfgblock, 4, 0, do_cfgblock,
671 "Toradex config block handling commands",
672 "create [-y] [barcode] - (Re-)create Toradex config block\n"
673 "cfgblock reload - Reload Toradex config block from flash"
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100674);