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