475abf78a7ba018e4499c798cceb5b72c117f18d
[platform/kernel/u-boot.git] / board / toradex / common / tdx-cfg-block.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2016-2020 Toradex
4  */
5
6 #include <common.h>
7 #include "tdx-cfg-block.h"
8 #include "tdx-eeprom.h"
9
10 #include <command.h>
11 #include <asm/cache.h>
12
13 #if defined(CONFIG_TARGET_APALIS_IMX6) || \
14         defined(CONFIG_TARGET_APALIS_IMX8) || \
15         defined(CONFIG_TARGET_APALIS_IMX8X) || \
16         defined(CONFIG_TARGET_COLIBRI_IMX6) || \
17         defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
18         defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
19         defined(CONFIG_TARGET_VERDIN_IMX8MN) || \
20         defined(CONFIG_TARGET_VERDIN_IMX8MP)
21 #include <asm/arch/sys_proto.h>
22 #else
23 #define is_cpu_type(cpu) (0)
24 #endif
25 #if defined(CONFIG_CPU_PXA27X)
26 #include <asm/arch-pxa/pxa.h>
27 #else
28 #define cpu_is_pxa27x(cpu) (0)
29 #endif
30 #include <cli.h>
31 #include <console.h>
32 #include <env.h>
33 #include <flash.h>
34 #include <malloc.h>
35 #include <mmc.h>
36 #include <nand.h>
37 #include <asm/mach-types.h>
38
39 DECLARE_GLOBAL_DATA_PTR;
40
41 #define TAG_VALID       0xcf01
42 #define TAG_MAC         0x0000
43 #define TAG_CAR_SERIAL  0x0021
44 #define TAG_HW          0x0008
45 #define TAG_INVALID     0xffff
46
47 #define TAG_FLAG_VALID  0x1
48
49 #define TDX_EEPROM_ID_MODULE            0
50 #define TDX_EEPROM_ID_CARRIER           1
51
52 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
53 #define TDX_CFG_BLOCK_MAX_SIZE 512
54 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
55 #define TDX_CFG_BLOCK_MAX_SIZE 64
56 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
57 #define TDX_CFG_BLOCK_MAX_SIZE 64
58 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
59 #define TDX_CFG_BLOCK_MAX_SIZE 64
60 #else
61 #error Toradex config block location not set
62 #endif
63
64 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
65 #define TDX_CFG_BLOCK_EXTRA_MAX_SIZE 64
66 #endif
67
68 struct toradex_tag {
69         u32 len:14;
70         u32 flags:2;
71         u32 id:16;
72 };
73
74 bool valid_cfgblock;
75 struct toradex_hw tdx_hw_tag;
76 struct toradex_eth_addr tdx_eth_addr;
77 u32 tdx_serial;
78 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
79 u32 tdx_car_serial;
80 bool valid_cfgblock_carrier;
81 struct toradex_hw tdx_car_hw_tag;
82 #endif
83
84 const char * const toradex_modules[] = {
85          [0] = "UNKNOWN MODULE",
86          [1] = "Colibri PXA270 312MHz",
87          [2] = "Colibri PXA270 520MHz",
88          [3] = "Colibri PXA320 806MHz",
89          [4] = "Colibri PXA300 208MHz",
90          [5] = "Colibri PXA310 624MHz",
91          [6] = "Colibri PXA320 806MHz IT",
92          [7] = "Colibri PXA300 208MHz XT",
93          [8] = "Colibri PXA270 312MHz",
94          [9] = "Colibri PXA270 520MHz",
95         [10] = "Colibri VF50 128MB", /* not currently on sale */
96         [11] = "Colibri VF61 256MB",
97         [12] = "Colibri VF61 256MB IT",
98         [13] = "Colibri VF50 128MB IT",
99         [14] = "Colibri iMX6 Solo 256MB",
100         [15] = "Colibri iMX6 DualLite 512MB",
101         [16] = "Colibri iMX6 Solo 256MB IT",
102         [17] = "Colibri iMX6 DualLite 512MB IT",
103         [18] = "UNKNOWN MODULE",
104         [19] = "UNKNOWN MODULE",
105         [20] = "Colibri T20 256MB",
106         [21] = "Colibri T20 512MB",
107         [22] = "Colibri T20 512MB IT",
108         [23] = "Colibri T30 1GB",
109         [24] = "Colibri T20 256MB IT",
110         [25] = "Apalis T30 2GB",
111         [26] = "Apalis T30 1GB",
112         [27] = "Apalis iMX6 Quad 1GB",
113         [28] = "Apalis iMX6 Quad 2GB IT",
114         [29] = "Apalis iMX6 Dual 512MB",
115         [30] = "Colibri T30 1GB IT",
116         [31] = "Apalis T30 1GB IT",
117         [32] = "Colibri iMX7 Solo 256MB",
118         [33] = "Colibri iMX7 Dual 512MB",
119         [34] = "Apalis TK1 2GB",
120         [35] = "Apalis iMX6 Dual 1GB IT",
121         [36] = "Colibri iMX6ULL 256MB",
122         [37] = "Apalis iMX8 QuadMax 4GB Wi-Fi / BT IT",
123         [38] = "Colibri iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
124         [39] = "Colibri iMX7 Dual 1GB (eMMC)",
125         [40] = "Colibri iMX6ULL 512MB Wi-Fi / BT IT",
126         [41] = "Colibri iMX7 Dual 512MB EPDC",
127         [42] = "Apalis TK1 4GB",
128         [43] = "Colibri T20 512MB IT SETEK",
129         [44] = "Colibri iMX6ULL 512MB IT",
130         [45] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth",
131         [46] = "Apalis iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
132         [47] = "Apalis iMX8 QuadMax 4GB IT",
133         [48] = "Apalis iMX8 QuadPlus 2GB Wi-Fi / BT",
134         [49] = "Apalis iMX8 QuadPlus 2GB",
135         [50] = "Colibri iMX8 QuadXPlus 2GB IT",
136         [51] = "Colibri iMX8 DualX 1GB Wi-Fi / Bluetooth",
137         [52] = "Colibri iMX8 DualX 1GB",
138         [53] = "Apalis iMX8 QuadXPlus 2GB ECC IT",
139         [54] = "Apalis iMX8 DualXPlus 1GB",
140         [55] = "Verdin iMX8M Mini Quad 2GB Wi-Fi / BT IT",
141         [56] = "Verdin iMX8M Nano Quad 1GB Wi-Fi / BT", /* not currently on sale */
142         [57] = "Verdin iMX8M Mini DualLite 1GB",
143         [58] = "Verdin iMX8M Plus Quad 4GB Wi-Fi / BT IT",
144         [59] = "Verdin iMX8M Mini Quad 2GB IT",
145         [60] = "Verdin iMX8M Mini DualLite 1GB WB IT",
146         [61] = "Verdin iMX8M Plus Quad 2GB",
147 };
148
149 const char * const toradex_carrier_boards[] = {
150         [0] = "UNKNOWN CARRIER BOARD",
151         [155] = "Dahlia",
152         [156] = "Verdin Development Board",
153 };
154
155 const char * const toradex_display_adapters[] = {
156         [0] = "UNKNOWN DISPLAY ADAPTER",
157         [157] = "Verdin DSI to HDMI Adapter",
158         [159] = "Verdin DSI to LVDS Adapter",
159 };
160
161 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
162 static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
163 {
164         struct mmc *mmc;
165         int dev = CONFIG_TDX_CFG_BLOCK_DEV;
166         int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
167         uint part = CONFIG_TDX_CFG_BLOCK_PART;
168         uint blk_start;
169         int ret = 0;
170
171         /* Read production parameter config block from eMMC */
172         mmc = find_mmc_device(dev);
173         if (!mmc) {
174                 puts("No MMC card found\n");
175                 ret = -ENODEV;
176                 goto out;
177         }
178         if (mmc_init(mmc)) {
179                 puts("MMC init failed\n");
180                 return -EINVAL;
181         }
182         if (part != mmc_get_blk_desc(mmc)->hwpart) {
183                 if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) {
184                         puts("MMC partition switch failed\n");
185                         ret = -ENODEV;
186                         goto out;
187                 }
188         }
189         if (offset < 0)
190                 offset += mmc->capacity;
191         blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
192
193         if (!write) {
194                 /* Careful reads a whole block of 512 bytes into config_block */
195                 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
196                               (unsigned char *)config_block) != 1) {
197                         ret = -EIO;
198                         goto out;
199                 }
200         } else {
201                 /* Just writing one 512 byte block */
202                 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
203                                (unsigned char *)config_block) != 1) {
204                         ret = -EIO;
205                         goto out;
206                 }
207         }
208
209 out:
210         /* Switch back to regular eMMC user partition */
211         blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
212
213         return ret;
214 }
215 #endif
216
217 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
218 static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
219 {
220         size_t size = TDX_CFG_BLOCK_MAX_SIZE;
221         struct mtd_info *mtd = get_nand_dev_by_index(0);
222
223         if (!mtd)
224                 return -ENODEV;
225
226         /* Read production parameter config block from NAND page */
227         return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
228                                   &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
229                                   config_block);
230 }
231
232 static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
233 {
234         size_t size = TDX_CFG_BLOCK_MAX_SIZE;
235
236         /* Write production parameter config block to NAND page */
237         return nand_write_skip_bad(get_nand_dev_by_index(0),
238                                    CONFIG_TDX_CFG_BLOCK_OFFSET,
239                                    &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
240                                    config_block, WITH_WR_VERIFY);
241 }
242 #endif
243
244 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
245 static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
246 {
247         /* Read production parameter config block from NOR flash */
248         memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
249                TDX_CFG_BLOCK_MAX_SIZE);
250         return 0;
251 }
252
253 static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
254 {
255         /* Write production parameter config block to NOR flash */
256         return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
257                            TDX_CFG_BLOCK_MAX_SIZE);
258 }
259 #endif
260
261 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM
262 static int read_tdx_cfg_block_from_eeprom(unsigned char *config_block)
263 {
264         return read_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
265                                     TDX_CFG_BLOCK_MAX_SIZE);
266 }
267
268 static int write_tdx_cfg_block_to_eeprom(unsigned char *config_block)
269 {
270         return write_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
271                                      TDX_CFG_BLOCK_MAX_SIZE);
272 }
273 #endif
274
275 int read_tdx_cfg_block(void)
276 {
277         int ret = 0;
278         u8 *config_block = NULL;
279         struct toradex_tag *tag;
280         size_t size = TDX_CFG_BLOCK_MAX_SIZE;
281         int offset;
282
283         /* Allocate RAM area for config block */
284         config_block = memalign(ARCH_DMA_MINALIGN, size);
285         if (!config_block) {
286                 printf("Not enough malloc space available!\n");
287                 return -ENOMEM;
288         }
289
290         memset(config_block, 0, size);
291
292 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
293         ret = tdx_cfg_block_mmc_storage(config_block, 0);
294 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
295         ret = read_tdx_cfg_block_from_nand(config_block);
296 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
297         ret = read_tdx_cfg_block_from_nor(config_block);
298 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
299         ret = read_tdx_cfg_block_from_eeprom(config_block);
300 #else
301         ret = -EINVAL;
302 #endif
303         if (ret)
304                 goto out;
305
306         /* Expect a valid tag first */
307         tag = (struct toradex_tag *)config_block;
308         if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
309                 valid_cfgblock = false;
310                 ret = -EINVAL;
311                 goto out;
312         }
313         valid_cfgblock = true;
314         offset = 4;
315
316         /*
317          * check if there is enough space for storing tag and value of the
318          * biggest element
319          */
320         while (offset + sizeof(struct toradex_tag) +
321                sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
322                 tag = (struct toradex_tag *)(config_block + offset);
323                 offset += 4;
324                 if (tag->id == TAG_INVALID)
325                         break;
326
327                 if (tag->flags == TAG_FLAG_VALID) {
328                         switch (tag->id) {
329                         case TAG_MAC:
330                                 memcpy(&tdx_eth_addr, config_block + offset,
331                                        6);
332
333                                 /* NIC part of MAC address is serial number */
334                                 tdx_serial = ntohl(tdx_eth_addr.nic) >> 8;
335                                 break;
336                         case TAG_HW:
337                                 memcpy(&tdx_hw_tag, config_block + offset, 8);
338                                 break;
339                         }
340                 }
341
342                 /* Get to next tag according to current tags length */
343                 offset += tag->len * 4;
344         }
345
346         /* Cap product id to avoid issues with a yet unknown one */
347         if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) /
348                                   sizeof(toradex_modules[0])))
349                 tdx_hw_tag.prodid = 0;
350
351 out:
352         free(config_block);
353         return ret;
354 }
355
356 static int get_cfgblock_interactive(void)
357 {
358         char message[CONFIG_SYS_CBSIZE];
359         char *soc;
360         char it = 'n';
361         char wb = 'n';
362         int len = 0;
363
364         /* Unknown module by default */
365         tdx_hw_tag.prodid = 0;
366
367         if (cpu_is_pxa27x())
368                 sprintf(message, "Is the module the 312 MHz version? [y/N] ");
369         else
370                 it = 'y';
371
372 #if defined(CONFIG_TARGET_APALIS_IMX8) || \
373                 defined(CONFIG_TARGET_APALIS_IMX8X) || \
374                 defined(CONFIG_TARGET_COLIBRI_IMX6ULL) || \
375                 defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
376                 defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
377                 defined(CONFIG_TARGET_VERDIN_IMX8MP)
378         sprintf(message, "Does the module have Wi-Fi / Bluetooth? [y/N] ");
379         len = cli_readline(message);
380         wb = console_buffer[0];
381 #endif
382
383         soc = env_get("soc");
384         if (!strcmp("mx6", soc)) {
385 #ifdef CONFIG_TARGET_APALIS_IMX6
386                 if (it == 'y' || it == 'Y') {
387                         if (is_cpu_type(MXC_CPU_MX6Q))
388                                 tdx_hw_tag.prodid = APALIS_IMX6Q_IT;
389                         else
390                                 tdx_hw_tag.prodid = APALIS_IMX6D_IT;
391                 } else {
392                         if (is_cpu_type(MXC_CPU_MX6Q))
393                                 tdx_hw_tag.prodid = APALIS_IMX6Q;
394                         else
395                                 tdx_hw_tag.prodid = APALIS_IMX6D;
396                 }
397 #elif CONFIG_TARGET_COLIBRI_IMX6
398                 if (it == 'y' || it == 'Y') {
399                         if (is_cpu_type(MXC_CPU_MX6DL))
400                                 tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT;
401                         else if (is_cpu_type(MXC_CPU_MX6SOLO))
402                                 tdx_hw_tag.prodid = COLIBRI_IMX6S_IT;
403                 } else {
404                         if (is_cpu_type(MXC_CPU_MX6DL))
405                                 tdx_hw_tag.prodid = COLIBRI_IMX6DL;
406                         else if (is_cpu_type(MXC_CPU_MX6SOLO))
407                                 tdx_hw_tag.prodid = COLIBRI_IMX6S;
408                 }
409 #elif CONFIG_TARGET_COLIBRI_IMX6ULL
410                 if (it == 'y' || it == 'Y') {
411                         if (wb == 'y' || wb == 'Y')
412                                 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT;
413                         else
414                                 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT;
415                 } else {
416                         if (wb == 'y' || wb == 'Y')
417                                 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT;
418                         else
419                                 tdx_hw_tag.prodid = COLIBRI_IMX6ULL;
420                 }
421 #endif
422         } else if (!strcmp("imx7d", soc))
423                 tdx_hw_tag.prodid = COLIBRI_IMX7D;
424         else if (!strcmp("imx7s", soc))
425                 tdx_hw_tag.prodid = COLIBRI_IMX7S;
426         else if (is_cpu_type(MXC_CPU_IMX8MM))
427                 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_WIFI_BT_IT;
428         else if (is_cpu_type(MXC_CPU_IMX8MMDL))
429                 tdx_hw_tag.prodid = VERDIN_IMX8MMDL;
430         else if (is_cpu_type(MXC_CPU_IMX8MN))
431                 tdx_hw_tag.prodid = VERDIN_IMX8MNQ_WIFI_BT;
432         else if (is_cpu_type(MXC_CPU_IMX8QM)) {
433                 if (it == 'y' || it == 'Y') {
434                         if (wb == 'y' || wb == 'Y')
435                                 tdx_hw_tag.prodid = APALIS_IMX8QM_WIFI_BT_IT;
436                         else
437                                 tdx_hw_tag.prodid = APALIS_IMX8QM_IT;
438                 } else {
439                         if (wb == 'y' || wb == 'Y')
440                                 tdx_hw_tag.prodid = APALIS_IMX8QP_WIFI_BT;
441                         else
442                                 tdx_hw_tag.prodid = APALIS_IMX8QP;
443                 }
444         } else if (is_cpu_type(MXC_CPU_IMX8QXP)) {
445 #ifdef CONFIG_TARGET_APALIS_IMX8X
446                 if (it == 'y' || it == 'Y' || wb == 'y' || wb == 'Y') {
447                                 tdx_hw_tag.prodid = APALIS_IMX8QXP_WIFI_BT_IT;
448                 } else {
449                         if (gd->ram_size == 0x40000000)
450                                 tdx_hw_tag.prodid = APALIS_IMX8DXP;
451                         else
452                                 tdx_hw_tag.prodid = APALIS_IMX8QXP;
453                 }
454 #elif CONFIG_TARGET_COLIBRI_IMX8X
455                 if (it == 'y' || it == 'Y') {
456                         if (wb == 'y' || wb == 'Y')
457                                 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT;
458                         else
459                                 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_IT;
460                 } else {
461                         if (wb == 'y' || wb == 'Y')
462                                 tdx_hw_tag.prodid = COLIBRI_IMX8DX_WIFI_BT;
463                         else
464                                 tdx_hw_tag.prodid = COLIBRI_IMX8DX;
465                 }
466 #endif
467         } else if (is_cpu_type(MXC_CPU_IMX8MM)) {
468                 if (is_cpu_type(MXC_CPU_IMX8MMDL)) {
469                         if (wb == 'y' || wb == 'Y')
470                                 tdx_hw_tag.prodid = VERDIN_IMX8MMDL_WIFI_BT_IT;
471                         else
472                                 tdx_hw_tag.prodid = VERDIN_IMX8MMDL;
473                 } else {
474                         if (wb == 'y' || wb == 'Y')
475                                 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_WIFI_BT_IT;
476                         else
477                                 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_IT;
478                 }
479         } else if (is_cpu_type(MXC_CPU_IMX8MN)) {
480                 tdx_hw_tag.prodid = VERDIN_IMX8MNQ_WIFI_BT;
481         } else if (is_cpu_type(MXC_CPU_IMX8MP)) {
482                 if (wb == 'y' || wb == 'Y')
483                         tdx_hw_tag.prodid = VERDIN_IMX8MPQ_WIFI_BT_IT;
484                 else
485                         tdx_hw_tag.prodid = VERDIN_IMX8MPQ;
486         } else if (!strcmp("tegra20", soc)) {
487                 if (it == 'y' || it == 'Y')
488                         if (gd->ram_size == 0x10000000)
489                                 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
490                         else
491                                 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
492                 else
493                         if (gd->ram_size == 0x10000000)
494                                 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
495                         else
496                                 tdx_hw_tag.prodid = COLIBRI_T20_512MB;
497         } else if (cpu_is_pxa27x()) {
498                 if (it == 'y' || it == 'Y')
499                         tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ;
500                 else
501                         tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
502         }
503 #ifdef CONFIG_MACH_TYPE
504         else if (!strcmp("tegra30", soc)) {
505                 if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) {
506                         if (it == 'y' || it == 'Y')
507                                 tdx_hw_tag.prodid = APALIS_T30_IT;
508                         else
509                                 if (gd->ram_size == 0x40000000)
510                                         tdx_hw_tag.prodid = APALIS_T30_1GB;
511                                 else
512                                         tdx_hw_tag.prodid = APALIS_T30_2GB;
513                 } else {
514                         if (it == 'y' || it == 'Y')
515                                 tdx_hw_tag.prodid = COLIBRI_T30_IT;
516                         else
517                                 tdx_hw_tag.prodid = COLIBRI_T30;
518                 }
519         }
520 #endif /* CONFIG_MACH_TYPE */
521         else if (!strcmp("tegra124", soc)) {
522                 tdx_hw_tag.prodid = APALIS_TK1_2GB;
523         } else if (!strcmp("vf500", soc)) {
524                 if (it == 'y' || it == 'Y')
525                         tdx_hw_tag.prodid = COLIBRI_VF50_IT;
526                 else
527                         tdx_hw_tag.prodid = COLIBRI_VF50;
528         } else if (!strcmp("vf610", soc)) {
529                 if (it == 'y' || it == 'Y')
530                         tdx_hw_tag.prodid = COLIBRI_VF61_IT;
531                 else
532                         tdx_hw_tag.prodid = COLIBRI_VF61;
533         }
534
535         if (!tdx_hw_tag.prodid) {
536                 printf("Module type not detectable due to unknown SoC\n");
537                 return -1;
538         }
539
540         while (len < 4) {
541                 sprintf(message, "Enter the module version (e.g. V1.1B): V");
542                 len = cli_readline(message);
543         }
544
545         tdx_hw_tag.ver_major = console_buffer[0] - '0';
546         tdx_hw_tag.ver_minor = console_buffer[2] - '0';
547         tdx_hw_tag.ver_assembly = console_buffer[3] - 'A';
548
549         if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1)
550                 tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
551                                        COLIBRI_PXA270_V1_312MHZ);
552
553         while (len < 8) {
554                 sprintf(message, "Enter module serial number: ");
555                 len = cli_readline(message);
556         }
557
558         tdx_serial = simple_strtoul(console_buffer, NULL, 10);
559
560         return 0;
561 }
562
563 static int get_cfgblock_barcode(char *barcode, struct toradex_hw *tag,
564                                 u32 *serial)
565 {
566         if (strlen(barcode) < 16) {
567                 printf("Argument too short, barcode is 16 chars long\n");
568                 return -1;
569         }
570
571         /* Get hardware information from the first 8 digits */
572         tag->ver_major = barcode[4] - '0';
573         tag->ver_minor = barcode[5] - '0';
574         tag->ver_assembly = barcode[7] - '0';
575
576         barcode[4] = '\0';
577         tag->prodid = simple_strtoul(barcode, NULL, 10);
578
579         /* Parse second part of the barcode (serial number */
580         barcode += 8;
581         *serial = simple_strtoul(barcode, NULL, 10);
582
583         return 0;
584 }
585
586 static int write_tag(u8 *config_block, int *offset, int tag_id,
587                      u8 *tag_data, size_t tag_data_size)
588 {
589         struct toradex_tag *tag;
590
591         if (!offset || !config_block)
592                 return -EINVAL;
593
594         tag = (struct toradex_tag *)(config_block + *offset);
595         tag->id = tag_id;
596         tag->flags = TAG_FLAG_VALID;
597         /* len is provided as number of 32bit values after the tag */
598         tag->len = (tag_data_size + sizeof(u32) - 1) / sizeof(u32);
599         *offset += sizeof(struct toradex_tag);
600         if (tag_data && tag_data_size) {
601                 memcpy(config_block + *offset, tag_data,
602                        tag_data_size);
603                 *offset += tag_data_size;
604         }
605
606         return 0;
607 }
608
609 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
610 int read_tdx_cfg_block_carrier(void)
611 {
612         int ret = 0;
613         u8 *config_block = NULL;
614         struct toradex_tag *tag;
615         size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
616         int offset;
617
618         /* Allocate RAM area for carrier config block */
619         config_block = memalign(ARCH_DMA_MINALIGN, size);
620         if (!config_block) {
621                 printf("Not enough malloc space available!\n");
622                 return -ENOMEM;
623         }
624
625         memset(config_block, 0, size);
626
627         ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
628                                    size);
629         if (ret)
630                 return ret;
631
632         /* Expect a valid tag first */
633         tag = (struct toradex_tag *)config_block;
634         if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
635                 valid_cfgblock_carrier = false;
636                 ret = -EINVAL;
637                 goto out;
638         }
639         valid_cfgblock_carrier = true;
640         offset = 4;
641
642         while (offset + sizeof(struct toradex_tag) +
643                sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
644                 tag = (struct toradex_tag *)(config_block + offset);
645                 offset += 4;
646                 if (tag->id == TAG_INVALID)
647                         break;
648
649                 if (tag->flags == TAG_FLAG_VALID) {
650                         switch (tag->id) {
651                         case TAG_CAR_SERIAL:
652                                 memcpy(&tdx_car_serial, config_block + offset,
653                                        sizeof(tdx_car_serial));
654                                 break;
655                         case TAG_HW:
656                                 memcpy(&tdx_car_hw_tag, config_block +
657                                        offset, 8);
658                                 break;
659                         }
660                 }
661
662                 /* Get to next tag according to current tags length */
663                 offset += tag->len * 4;
664         }
665 out:
666         free(config_block);
667         return ret;
668 }
669
670 int check_pid8_sanity(char *pid8)
671 {
672         char s_carrierid_verdin_dev[5];
673         char s_carrierid_dahlia[5];
674
675         sprintf(s_carrierid_verdin_dev, "0%d", VERDIN_DEVELOPMENT_BOARD);
676         sprintf(s_carrierid_dahlia, "0%d", DAHLIA);
677
678         /* sane value check, first 4 chars which represent carrier id */
679         if (!strncmp(pid8, s_carrierid_verdin_dev, 4))
680                 return 0;
681
682         if (!strncmp(pid8, s_carrierid_dahlia, 4))
683                 return 0;
684
685         return -EINVAL;
686 }
687
688 int try_migrate_tdx_cfg_block_carrier(void)
689 {
690         char pid8[8];
691         int offset = 0;
692         int ret = CMD_RET_SUCCESS;
693         size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
694         u8 *config_block;
695
696         memset(pid8, 0x0, 8);
697         ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, (u8 *)pid8, 8);
698         if (ret)
699                 return ret;
700
701         if (check_pid8_sanity(pid8))
702                 return -EINVAL;
703
704         /* Allocate RAM area for config block */
705         config_block = memalign(ARCH_DMA_MINALIGN, size);
706         if (!config_block) {
707                 printf("Not enough malloc space available!\n");
708                 return CMD_RET_FAILURE;
709         }
710
711         memset(config_block, 0xff, size);
712         /* we try parse PID8 concatenating zeroed serial number */
713         tdx_car_hw_tag.ver_major = pid8[4] - '0';
714         tdx_car_hw_tag.ver_minor = pid8[5] - '0';
715         tdx_car_hw_tag.ver_assembly = pid8[7] - '0';
716
717         pid8[4] = '\0';
718         tdx_car_hw_tag.prodid = simple_strtoul(pid8, NULL, 10);
719
720         /* Valid Tag */
721         write_tag(config_block, &offset, TAG_VALID, NULL, 0);
722
723         /* Product Tag */
724         write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
725                   sizeof(tdx_car_hw_tag));
726
727         /* Serial Tag */
728         write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
729                   sizeof(tdx_car_serial));
730
731         memset(config_block + offset, 0, 32 - offset);
732         ret = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
733                                     size);
734         if (ret) {
735                 printf("Failed to write Toradex Extra config block: %d\n",
736                        ret);
737                 ret = CMD_RET_FAILURE;
738                 goto out;
739         }
740
741         printf("Successfully migrated to Toradex Config Block from PID8\n");
742
743 out:
744         free(config_block);
745         return ret;
746 }
747
748 static int get_cfgblock_carrier_interactive(void)
749 {
750         char message[CONFIG_SYS_CBSIZE];
751         int len;
752
753         printf("Supported carrier boards:\n");
754         printf("CARRIER BOARD NAME\t\t [ID]\n");
755         for (int i = 0; i < sizeof(toradex_carrier_boards) /
756                             sizeof(toradex_carrier_boards[0]); i++)
757                 if (toradex_carrier_boards[i])
758                         printf("%s \t\t [%d]\n", toradex_carrier_boards[i], i);
759
760         sprintf(message, "Choose your carrier board (provide ID): ");
761         len = cli_readline(message);
762         tdx_car_hw_tag.prodid = simple_strtoul(console_buffer, NULL, 10);
763
764         do {
765                 sprintf(message, "Enter carrier board version (e.g. V1.1B): V");
766                 len = cli_readline(message);
767         } while (len < 4);
768
769         tdx_car_hw_tag.ver_major = console_buffer[0] - '0';
770         tdx_car_hw_tag.ver_minor = console_buffer[2] - '0';
771         tdx_car_hw_tag.ver_assembly = console_buffer[3] - 'A';
772
773         while (len < 8) {
774                 sprintf(message, "Enter carrier board serial number: ");
775                 len = cli_readline(message);
776         }
777
778         tdx_car_serial = simple_strtoul(console_buffer, NULL, 10);
779
780         return 0;
781 }
782
783 static int do_cfgblock_carrier_create(struct cmd_tbl *cmdtp, int flag, int argc,
784                                       char * const argv[])
785 {
786         u8 *config_block;
787         size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
788         int offset = 0;
789         int ret = CMD_RET_SUCCESS;
790         int err;
791         int force_overwrite = 0;
792
793         if (argc >= 3) {
794                 if (argv[2][0] == '-' && argv[2][1] == 'y')
795                         force_overwrite = 1;
796         }
797
798         /* Allocate RAM area for config block */
799         config_block = memalign(ARCH_DMA_MINALIGN, size);
800         if (!config_block) {
801                 printf("Not enough malloc space available!\n");
802                 return CMD_RET_FAILURE;
803         }
804
805         memset(config_block, 0xff, size);
806         read_tdx_cfg_block_carrier();
807         if (valid_cfgblock_carrier && !force_overwrite) {
808                 char message[CONFIG_SYS_CBSIZE];
809
810                 sprintf(message, "A valid Toradex Carrier config block is present, still recreate? [y/N] ");
811
812                 if (!cli_readline(message))
813                         goto out;
814
815                 if (console_buffer[0] != 'y' &&
816                     console_buffer[0] != 'Y')
817                         goto out;
818         }
819
820         if (argc < 3 || (force_overwrite && argc < 4)) {
821                 err = get_cfgblock_carrier_interactive();
822         } else {
823                 if (force_overwrite)
824                         err = get_cfgblock_barcode(argv[3], &tdx_car_hw_tag,
825                                                    &tdx_car_serial);
826                 else
827                         err = get_cfgblock_barcode(argv[2], &tdx_car_hw_tag,
828                                                    &tdx_car_serial);
829         }
830
831         if (err) {
832                 ret = CMD_RET_FAILURE;
833                 goto out;
834         }
835
836         /* Valid Tag */
837         write_tag(config_block, &offset, TAG_VALID, NULL, 0);
838
839         /* Product Tag */
840         write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
841                   sizeof(tdx_car_hw_tag));
842
843         /* Serial Tag */
844         write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
845                   sizeof(tdx_car_serial));
846
847         memset(config_block + offset, 0, 32 - offset);
848         err = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
849                                     size);
850         if (err) {
851                 printf("Failed to write Toradex Extra config block: %d\n",
852                        ret);
853                 ret = CMD_RET_FAILURE;
854                 goto out;
855         }
856
857         printf("Toradex Extra config block successfully written\n");
858
859 out:
860         free(config_block);
861         return ret;
862 }
863
864 #endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
865
866 static int do_cfgblock_create(struct cmd_tbl *cmdtp, int flag, int argc,
867                               char * const argv[])
868 {
869         u8 *config_block;
870         size_t size = TDX_CFG_BLOCK_MAX_SIZE;
871         int offset = 0;
872         int ret = CMD_RET_SUCCESS;
873         int err;
874         int force_overwrite = 0;
875
876         if (argc >= 3) {
877 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
878                 if (!strcmp(argv[2], "carrier"))
879                         return do_cfgblock_carrier_create(cmdtp, flag,
880                                                           --argc, ++argv);
881 #endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
882                 if (argv[2][0] == '-' && argv[2][1] == 'y')
883                         force_overwrite = 1;
884         }
885
886         /* Allocate RAM area for config block */
887         config_block = memalign(ARCH_DMA_MINALIGN, size);
888         if (!config_block) {
889                 printf("Not enough malloc space available!\n");
890                 return CMD_RET_FAILURE;
891         }
892
893         memset(config_block, 0xff, size);
894
895         read_tdx_cfg_block();
896         if (valid_cfgblock) {
897 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
898                 /*
899                  * On NAND devices, recreation is only allowed if the page is
900                  * empty (config block invalid...)
901                  */
902                 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
903                        CONFIG_TDX_CFG_BLOCK_OFFSET /
904                        get_nand_dev_by_index(0)->erasesize);
905                 goto out;
906 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
907                 /*
908                  * On NOR devices, recreation is only allowed if the sector is
909                  * empty and write protection is off (config block invalid...)
910                  */
911                 printf("NOR sector at offset 0x%02x need to be erased and unprotected before creating a Toradex config block\n",
912                        CONFIG_TDX_CFG_BLOCK_OFFSET);
913                 goto out;
914 #else
915                 if (!force_overwrite) {
916                         char message[CONFIG_SYS_CBSIZE];
917
918                         sprintf(message,
919                                 "A valid Toradex config block is present, still recreate? [y/N] ");
920
921                         if (!cli_readline(message))
922                                 goto out;
923
924                         if (console_buffer[0] != 'y' &&
925                             console_buffer[0] != 'Y')
926                                 goto out;
927                 }
928 #endif
929         }
930
931         /* Parse new Toradex config block data... */
932         if (argc < 3 || (force_overwrite && argc < 4)) {
933                 err = get_cfgblock_interactive();
934         } else {
935                 if (force_overwrite)
936                         err = get_cfgblock_barcode(argv[3], &tdx_hw_tag,
937                                                    &tdx_serial);
938                 else
939                         err = get_cfgblock_barcode(argv[2], &tdx_hw_tag,
940                                                    &tdx_serial);
941         }
942         if (err) {
943                 ret = CMD_RET_FAILURE;
944                 goto out;
945         }
946
947         /* Convert serial number to MAC address (the storage format) */
948         tdx_eth_addr.oui = htonl(0x00142dUL << 8);
949         tdx_eth_addr.nic = htonl(tdx_serial << 8);
950
951         /* Valid Tag */
952         write_tag(config_block, &offset, TAG_VALID, NULL, 0);
953
954         /* Product Tag */
955         write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_hw_tag,
956                   sizeof(tdx_hw_tag));
957
958         /* MAC Tag */
959         write_tag(config_block, &offset, TAG_MAC, (u8 *)&tdx_eth_addr,
960                   sizeof(tdx_eth_addr));
961
962         memset(config_block + offset, 0, 32 - offset);
963 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
964         err = tdx_cfg_block_mmc_storage(config_block, 1);
965 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
966         err = write_tdx_cfg_block_to_nand(config_block);
967 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
968         err = write_tdx_cfg_block_to_nor(config_block);
969 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
970         err = write_tdx_cfg_block_to_eeprom(config_block);
971 #else
972         err = -EINVAL;
973 #endif
974         if (err) {
975                 printf("Failed to write Toradex config block: %d\n", ret);
976                 ret = CMD_RET_FAILURE;
977                 goto out;
978         }
979
980         printf("Toradex config block successfully written\n");
981
982 out:
983         free(config_block);
984         return ret;
985 }
986
987 static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc,
988                        char *const argv[])
989 {
990         int ret;
991
992         if (argc < 2)
993                 return CMD_RET_USAGE;
994
995         if (!strcmp(argv[1], "create")) {
996                 return do_cfgblock_create(cmdtp, flag, argc, argv);
997         } else if (!strcmp(argv[1], "reload")) {
998                 ret = read_tdx_cfg_block();
999                 if (ret) {
1000                         printf("Failed to reload Toradex config block: %d\n",
1001                                ret);
1002                         return CMD_RET_FAILURE;
1003                 }
1004                 return CMD_RET_SUCCESS;
1005         }
1006
1007         return CMD_RET_USAGE;
1008 }
1009
1010 U_BOOT_CMD(
1011         cfgblock, 5, 0, do_cfgblock,
1012         "Toradex config block handling commands",
1013         "create [-y] [barcode] - (Re-)create Toradex config block\n"
1014         "create carrier [-y] [barcode] - (Re-)create Toradex Carrier config block\n"
1015         "cfgblock reload - Reload Toradex config block from flash"
1016 );