adab0a080209e6346b8718accc329abf12ad8cf9
[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_IMX8QM)) {
427                 if (it == 'y' || it == 'Y') {
428                         if (wb == 'y' || wb == 'Y')
429                                 tdx_hw_tag.prodid = APALIS_IMX8QM_WIFI_BT_IT;
430                         else
431                                 tdx_hw_tag.prodid = APALIS_IMX8QM_IT;
432                 } else {
433                         if (wb == 'y' || wb == 'Y')
434                                 tdx_hw_tag.prodid = APALIS_IMX8QP_WIFI_BT;
435                         else
436                                 tdx_hw_tag.prodid = APALIS_IMX8QP;
437                 }
438         } else if (is_cpu_type(MXC_CPU_IMX8QXP)) {
439 #ifdef CONFIG_TARGET_APALIS_IMX8X
440                 if (it == 'y' || it == 'Y' || wb == 'y' || wb == 'Y') {
441                                 tdx_hw_tag.prodid = APALIS_IMX8QXP_WIFI_BT_IT;
442                 } else {
443                         if (gd->ram_size == 0x40000000)
444                                 tdx_hw_tag.prodid = APALIS_IMX8DXP;
445                         else
446                                 tdx_hw_tag.prodid = APALIS_IMX8QXP;
447                 }
448 #elif CONFIG_TARGET_COLIBRI_IMX8X
449                 if (it == 'y' || it == 'Y') {
450                         if (wb == 'y' || wb == 'Y')
451                                 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT;
452                         else
453                                 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_IT;
454                 } else {
455                         if (wb == 'y' || wb == 'Y')
456                                 tdx_hw_tag.prodid = COLIBRI_IMX8DX_WIFI_BT;
457                         else
458                                 tdx_hw_tag.prodid = COLIBRI_IMX8DX;
459                 }
460 #endif
461         } else if (is_cpu_type(MXC_CPU_IMX8MMDL)) {
462                 if (wb == 'y' || wb == 'Y')
463                         tdx_hw_tag.prodid = VERDIN_IMX8MMDL_WIFI_BT_IT;
464                 else
465                         tdx_hw_tag.prodid = VERDIN_IMX8MMDL;
466         } else if (is_cpu_type(MXC_CPU_IMX8MM)) {
467                 if (wb == 'y' || wb == 'Y')
468                         tdx_hw_tag.prodid = VERDIN_IMX8MMQ_WIFI_BT_IT;
469                 else
470                         tdx_hw_tag.prodid = VERDIN_IMX8MMQ_IT;
471         } else if (is_cpu_type(MXC_CPU_IMX8MN)) {
472                 tdx_hw_tag.prodid = VERDIN_IMX8MNQ_WIFI_BT;
473         } else if (is_cpu_type(MXC_CPU_IMX8MP)) {
474                 if (wb == 'y' || wb == 'Y')
475                         tdx_hw_tag.prodid = VERDIN_IMX8MPQ_WIFI_BT_IT;
476                 else
477                         tdx_hw_tag.prodid = VERDIN_IMX8MPQ;
478         } else if (!strcmp("tegra20", soc)) {
479                 if (it == 'y' || it == 'Y')
480                         if (gd->ram_size == 0x10000000)
481                                 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
482                         else
483                                 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
484                 else
485                         if (gd->ram_size == 0x10000000)
486                                 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
487                         else
488                                 tdx_hw_tag.prodid = COLIBRI_T20_512MB;
489         } else if (cpu_is_pxa27x()) {
490                 if (it == 'y' || it == 'Y')
491                         tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ;
492                 else
493                         tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
494         }
495 #ifdef CONFIG_MACH_TYPE
496         else if (!strcmp("tegra30", soc)) {
497                 if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) {
498                         if (it == 'y' || it == 'Y')
499                                 tdx_hw_tag.prodid = APALIS_T30_IT;
500                         else
501                                 if (gd->ram_size == 0x40000000)
502                                         tdx_hw_tag.prodid = APALIS_T30_1GB;
503                                 else
504                                         tdx_hw_tag.prodid = APALIS_T30_2GB;
505                 } else {
506                         if (it == 'y' || it == 'Y')
507                                 tdx_hw_tag.prodid = COLIBRI_T30_IT;
508                         else
509                                 tdx_hw_tag.prodid = COLIBRI_T30;
510                 }
511         }
512 #endif /* CONFIG_MACH_TYPE */
513         else if (!strcmp("tegra124", soc)) {
514                 tdx_hw_tag.prodid = APALIS_TK1_2GB;
515         } else if (!strcmp("vf500", soc)) {
516                 if (it == 'y' || it == 'Y')
517                         tdx_hw_tag.prodid = COLIBRI_VF50_IT;
518                 else
519                         tdx_hw_tag.prodid = COLIBRI_VF50;
520         } else if (!strcmp("vf610", soc)) {
521                 if (it == 'y' || it == 'Y')
522                         tdx_hw_tag.prodid = COLIBRI_VF61_IT;
523                 else
524                         tdx_hw_tag.prodid = COLIBRI_VF61;
525         }
526
527         if (!tdx_hw_tag.prodid) {
528                 printf("Module type not detectable due to unknown SoC\n");
529                 return -1;
530         }
531
532         while (len < 4) {
533                 sprintf(message, "Enter the module version (e.g. V1.1B): V");
534                 len = cli_readline(message);
535         }
536
537         tdx_hw_tag.ver_major = console_buffer[0] - '0';
538         tdx_hw_tag.ver_minor = console_buffer[2] - '0';
539         tdx_hw_tag.ver_assembly = console_buffer[3] - 'A';
540
541         if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1)
542                 tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
543                                        COLIBRI_PXA270_V1_312MHZ);
544
545         while (len < 8) {
546                 sprintf(message, "Enter module serial number: ");
547                 len = cli_readline(message);
548         }
549
550         tdx_serial = simple_strtoul(console_buffer, NULL, 10);
551
552         return 0;
553 }
554
555 static int get_cfgblock_barcode(char *barcode, struct toradex_hw *tag,
556                                 u32 *serial)
557 {
558         if (strlen(barcode) < 16) {
559                 printf("Argument too short, barcode is 16 chars long\n");
560                 return -1;
561         }
562
563         /* Get hardware information from the first 8 digits */
564         tag->ver_major = barcode[4] - '0';
565         tag->ver_minor = barcode[5] - '0';
566         tag->ver_assembly = barcode[7] - '0';
567
568         barcode[4] = '\0';
569         tag->prodid = simple_strtoul(barcode, NULL, 10);
570
571         /* Parse second part of the barcode (serial number */
572         barcode += 8;
573         *serial = simple_strtoul(barcode, NULL, 10);
574
575         return 0;
576 }
577
578 static int write_tag(u8 *config_block, int *offset, int tag_id,
579                      u8 *tag_data, size_t tag_data_size)
580 {
581         struct toradex_tag *tag;
582
583         if (!offset || !config_block)
584                 return -EINVAL;
585
586         tag = (struct toradex_tag *)(config_block + *offset);
587         tag->id = tag_id;
588         tag->flags = TAG_FLAG_VALID;
589         /* len is provided as number of 32bit values after the tag */
590         tag->len = (tag_data_size + sizeof(u32) - 1) / sizeof(u32);
591         *offset += sizeof(struct toradex_tag);
592         if (tag_data && tag_data_size) {
593                 memcpy(config_block + *offset, tag_data,
594                        tag_data_size);
595                 *offset += tag_data_size;
596         }
597
598         return 0;
599 }
600
601 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
602 int read_tdx_cfg_block_carrier(void)
603 {
604         int ret = 0;
605         u8 *config_block = NULL;
606         struct toradex_tag *tag;
607         size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
608         int offset;
609
610         /* Allocate RAM area for carrier config block */
611         config_block = memalign(ARCH_DMA_MINALIGN, size);
612         if (!config_block) {
613                 printf("Not enough malloc space available!\n");
614                 return -ENOMEM;
615         }
616
617         memset(config_block, 0, size);
618
619         ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
620                                    size);
621         if (ret)
622                 return ret;
623
624         /* Expect a valid tag first */
625         tag = (struct toradex_tag *)config_block;
626         if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
627                 valid_cfgblock_carrier = false;
628                 ret = -EINVAL;
629                 goto out;
630         }
631         valid_cfgblock_carrier = true;
632         offset = 4;
633
634         while (offset + sizeof(struct toradex_tag) +
635                sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
636                 tag = (struct toradex_tag *)(config_block + offset);
637                 offset += 4;
638                 if (tag->id == TAG_INVALID)
639                         break;
640
641                 if (tag->flags == TAG_FLAG_VALID) {
642                         switch (tag->id) {
643                         case TAG_CAR_SERIAL:
644                                 memcpy(&tdx_car_serial, config_block + offset,
645                                        sizeof(tdx_car_serial));
646                                 break;
647                         case TAG_HW:
648                                 memcpy(&tdx_car_hw_tag, config_block +
649                                        offset, 8);
650                                 break;
651                         }
652                 }
653
654                 /* Get to next tag according to current tags length */
655                 offset += tag->len * 4;
656         }
657 out:
658         free(config_block);
659         return ret;
660 }
661
662 int check_pid8_sanity(char *pid8)
663 {
664         char s_carrierid_verdin_dev[5];
665         char s_carrierid_dahlia[5];
666
667         sprintf(s_carrierid_verdin_dev, "0%d", VERDIN_DEVELOPMENT_BOARD);
668         sprintf(s_carrierid_dahlia, "0%d", DAHLIA);
669
670         /* sane value check, first 4 chars which represent carrier id */
671         if (!strncmp(pid8, s_carrierid_verdin_dev, 4))
672                 return 0;
673
674         if (!strncmp(pid8, s_carrierid_dahlia, 4))
675                 return 0;
676
677         return -EINVAL;
678 }
679
680 int try_migrate_tdx_cfg_block_carrier(void)
681 {
682         char pid8[8];
683         int offset = 0;
684         int ret = CMD_RET_SUCCESS;
685         size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
686         u8 *config_block;
687
688         memset(pid8, 0x0, 8);
689         ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, (u8 *)pid8, 8);
690         if (ret)
691                 return ret;
692
693         if (check_pid8_sanity(pid8))
694                 return -EINVAL;
695
696         /* Allocate RAM area for config block */
697         config_block = memalign(ARCH_DMA_MINALIGN, size);
698         if (!config_block) {
699                 printf("Not enough malloc space available!\n");
700                 return CMD_RET_FAILURE;
701         }
702
703         memset(config_block, 0xff, size);
704         /* we try parse PID8 concatenating zeroed serial number */
705         tdx_car_hw_tag.ver_major = pid8[4] - '0';
706         tdx_car_hw_tag.ver_minor = pid8[5] - '0';
707         tdx_car_hw_tag.ver_assembly = pid8[7] - '0';
708
709         pid8[4] = '\0';
710         tdx_car_hw_tag.prodid = simple_strtoul(pid8, NULL, 10);
711
712         /* Valid Tag */
713         write_tag(config_block, &offset, TAG_VALID, NULL, 0);
714
715         /* Product Tag */
716         write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
717                   sizeof(tdx_car_hw_tag));
718
719         /* Serial Tag */
720         write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
721                   sizeof(tdx_car_serial));
722
723         memset(config_block + offset, 0, 32 - offset);
724         ret = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
725                                     size);
726         if (ret) {
727                 printf("Failed to write Toradex Extra config block: %d\n",
728                        ret);
729                 ret = CMD_RET_FAILURE;
730                 goto out;
731         }
732
733         printf("Successfully migrated to Toradex Config Block from PID8\n");
734
735 out:
736         free(config_block);
737         return ret;
738 }
739
740 static int get_cfgblock_carrier_interactive(void)
741 {
742         char message[CONFIG_SYS_CBSIZE];
743         int len;
744
745         printf("Supported carrier boards:\n");
746         printf("CARRIER BOARD NAME\t\t [ID]\n");
747         for (int i = 0; i < sizeof(toradex_carrier_boards) /
748                             sizeof(toradex_carrier_boards[0]); i++)
749                 if (toradex_carrier_boards[i])
750                         printf("%s \t\t [%d]\n", toradex_carrier_boards[i], i);
751
752         sprintf(message, "Choose your carrier board (provide ID): ");
753         len = cli_readline(message);
754         tdx_car_hw_tag.prodid = simple_strtoul(console_buffer, NULL, 10);
755
756         do {
757                 sprintf(message, "Enter carrier board version (e.g. V1.1B): V");
758                 len = cli_readline(message);
759         } while (len < 4);
760
761         tdx_car_hw_tag.ver_major = console_buffer[0] - '0';
762         tdx_car_hw_tag.ver_minor = console_buffer[2] - '0';
763         tdx_car_hw_tag.ver_assembly = console_buffer[3] - 'A';
764
765         while (len < 8) {
766                 sprintf(message, "Enter carrier board serial number: ");
767                 len = cli_readline(message);
768         }
769
770         tdx_car_serial = simple_strtoul(console_buffer, NULL, 10);
771
772         return 0;
773 }
774
775 static int do_cfgblock_carrier_create(struct cmd_tbl *cmdtp, int flag, int argc,
776                                       char * const argv[])
777 {
778         u8 *config_block;
779         size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
780         int offset = 0;
781         int ret = CMD_RET_SUCCESS;
782         int err;
783         int force_overwrite = 0;
784
785         if (argc >= 3) {
786                 if (argv[2][0] == '-' && argv[2][1] == 'y')
787                         force_overwrite = 1;
788         }
789
790         /* Allocate RAM area for config block */
791         config_block = memalign(ARCH_DMA_MINALIGN, size);
792         if (!config_block) {
793                 printf("Not enough malloc space available!\n");
794                 return CMD_RET_FAILURE;
795         }
796
797         memset(config_block, 0xff, size);
798         read_tdx_cfg_block_carrier();
799         if (valid_cfgblock_carrier && !force_overwrite) {
800                 char message[CONFIG_SYS_CBSIZE];
801
802                 sprintf(message, "A valid Toradex Carrier config block is present, still recreate? [y/N] ");
803
804                 if (!cli_readline(message))
805                         goto out;
806
807                 if (console_buffer[0] != 'y' &&
808                     console_buffer[0] != 'Y')
809                         goto out;
810         }
811
812         if (argc < 3 || (force_overwrite && argc < 4)) {
813                 err = get_cfgblock_carrier_interactive();
814         } else {
815                 if (force_overwrite)
816                         err = get_cfgblock_barcode(argv[3], &tdx_car_hw_tag,
817                                                    &tdx_car_serial);
818                 else
819                         err = get_cfgblock_barcode(argv[2], &tdx_car_hw_tag,
820                                                    &tdx_car_serial);
821         }
822
823         if (err) {
824                 ret = CMD_RET_FAILURE;
825                 goto out;
826         }
827
828         /* Valid Tag */
829         write_tag(config_block, &offset, TAG_VALID, NULL, 0);
830
831         /* Product Tag */
832         write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
833                   sizeof(tdx_car_hw_tag));
834
835         /* Serial Tag */
836         write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
837                   sizeof(tdx_car_serial));
838
839         memset(config_block + offset, 0, 32 - offset);
840         err = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
841                                     size);
842         if (err) {
843                 printf("Failed to write Toradex Extra config block: %d\n",
844                        ret);
845                 ret = CMD_RET_FAILURE;
846                 goto out;
847         }
848
849         printf("Toradex Extra config block successfully written\n");
850
851 out:
852         free(config_block);
853         return ret;
854 }
855
856 #endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
857
858 static int do_cfgblock_create(struct cmd_tbl *cmdtp, int flag, int argc,
859                               char * const argv[])
860 {
861         u8 *config_block;
862         size_t size = TDX_CFG_BLOCK_MAX_SIZE;
863         int offset = 0;
864         int ret = CMD_RET_SUCCESS;
865         int err;
866         int force_overwrite = 0;
867
868         if (argc >= 3) {
869 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
870                 if (!strcmp(argv[2], "carrier"))
871                         return do_cfgblock_carrier_create(cmdtp, flag,
872                                                           --argc, ++argv);
873 #endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
874                 if (argv[2][0] == '-' && argv[2][1] == 'y')
875                         force_overwrite = 1;
876         }
877
878         /* Allocate RAM area for config block */
879         config_block = memalign(ARCH_DMA_MINALIGN, size);
880         if (!config_block) {
881                 printf("Not enough malloc space available!\n");
882                 return CMD_RET_FAILURE;
883         }
884
885         memset(config_block, 0xff, size);
886
887         read_tdx_cfg_block();
888         if (valid_cfgblock) {
889 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
890                 /*
891                  * On NAND devices, recreation is only allowed if the page is
892                  * empty (config block invalid...)
893                  */
894                 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
895                        CONFIG_TDX_CFG_BLOCK_OFFSET /
896                        get_nand_dev_by_index(0)->erasesize);
897                 goto out;
898 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
899                 /*
900                  * On NOR devices, recreation is only allowed if the sector is
901                  * empty and write protection is off (config block invalid...)
902                  */
903                 printf("NOR sector at offset 0x%02x need to be erased and unprotected before creating a Toradex config block\n",
904                        CONFIG_TDX_CFG_BLOCK_OFFSET);
905                 goto out;
906 #else
907                 if (!force_overwrite) {
908                         char message[CONFIG_SYS_CBSIZE];
909
910                         sprintf(message,
911                                 "A valid Toradex config block is present, still recreate? [y/N] ");
912
913                         if (!cli_readline(message))
914                                 goto out;
915
916                         if (console_buffer[0] != 'y' &&
917                             console_buffer[0] != 'Y')
918                                 goto out;
919                 }
920 #endif
921         }
922
923         /* Parse new Toradex config block data... */
924         if (argc < 3 || (force_overwrite && argc < 4)) {
925                 err = get_cfgblock_interactive();
926         } else {
927                 if (force_overwrite)
928                         err = get_cfgblock_barcode(argv[3], &tdx_hw_tag,
929                                                    &tdx_serial);
930                 else
931                         err = get_cfgblock_barcode(argv[2], &tdx_hw_tag,
932                                                    &tdx_serial);
933         }
934         if (err) {
935                 ret = CMD_RET_FAILURE;
936                 goto out;
937         }
938
939         /* Convert serial number to MAC address (the storage format) */
940         tdx_eth_addr.oui = htonl(0x00142dUL << 8);
941         tdx_eth_addr.nic = htonl(tdx_serial << 8);
942
943         /* Valid Tag */
944         write_tag(config_block, &offset, TAG_VALID, NULL, 0);
945
946         /* Product Tag */
947         write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_hw_tag,
948                   sizeof(tdx_hw_tag));
949
950         /* MAC Tag */
951         write_tag(config_block, &offset, TAG_MAC, (u8 *)&tdx_eth_addr,
952                   sizeof(tdx_eth_addr));
953
954         memset(config_block + offset, 0, 32 - offset);
955 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
956         err = tdx_cfg_block_mmc_storage(config_block, 1);
957 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
958         err = write_tdx_cfg_block_to_nand(config_block);
959 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
960         err = write_tdx_cfg_block_to_nor(config_block);
961 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
962         err = write_tdx_cfg_block_to_eeprom(config_block);
963 #else
964         err = -EINVAL;
965 #endif
966         if (err) {
967                 printf("Failed to write Toradex config block: %d\n", ret);
968                 ret = CMD_RET_FAILURE;
969                 goto out;
970         }
971
972         printf("Toradex config block successfully written\n");
973
974 out:
975         free(config_block);
976         return ret;
977 }
978
979 static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc,
980                        char *const argv[])
981 {
982         int ret;
983
984         if (argc < 2)
985                 return CMD_RET_USAGE;
986
987         if (!strcmp(argv[1], "create")) {
988                 return do_cfgblock_create(cmdtp, flag, argc, argv);
989         } else if (!strcmp(argv[1], "reload")) {
990                 ret = read_tdx_cfg_block();
991                 if (ret) {
992                         printf("Failed to reload Toradex config block: %d\n",
993                                ret);
994                         return CMD_RET_FAILURE;
995                 }
996                 return CMD_RET_SUCCESS;
997         }
998
999         return CMD_RET_USAGE;
1000 }
1001
1002 U_BOOT_CMD(
1003         cfgblock, 5, 0, do_cfgblock,
1004         "Toradex config block handling commands",
1005         "create [-y] [barcode] - (Re-)create Toradex config block\n"
1006         "create carrier [-y] [barcode] - (Re-)create Toradex Carrier config block\n"
1007         "cfgblock reload - Reload Toradex config block from flash"
1008 );