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