tegra: Convert MMC to use driver model for operations
[oweals/u-boot.git] / 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
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                 /* Flush cache after read */
132                 flush_cache((ulong)(unsigned char *)config_block, 512);
133         } else {
134                 /* Just writing one 512 byte block */
135                 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
136                                (unsigned char *)config_block) != 1) {
137                         ret = -EIO;
138                         goto out;
139                 }
140         }
141
142 out:
143         /* Switch back to regular eMMC user partition */
144         blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
145
146         return ret;
147 }
148 #endif
149
150 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
151 static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
152 {
153         size_t size = TDX_CFG_BLOCK_MAX_SIZE;
154
155         /* Read production parameter config block from NAND page */
156         return nand_read_skip_bad(nand_info[0], CONFIG_TDX_CFG_BLOCK_OFFSET,
157                          &size, NULL, TDX_CFG_BLOCK_MAX_SIZE, 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(nand_info[0], CONFIG_TDX_CFG_BLOCK_OFFSET,
166                                    &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
167                                    config_block, WITH_WR_VERIFY);
168 }
169 #endif
170
171 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
172 static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
173 {
174         /* Read production parameter config block from NOR flash */
175         memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
176                TDX_CFG_BLOCK_MAX_SIZE);
177         return 0;
178 }
179
180 static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
181 {
182         /* Write production parameter config block to NOR flash */
183         return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
184                            TDX_CFG_BLOCK_MAX_SIZE);
185 }
186 #endif
187
188 int read_tdx_cfg_block(void)
189 {
190         int ret = 0;
191         u8 *config_block = NULL;
192         struct toradex_tag *tag;
193         size_t size = TDX_CFG_BLOCK_MAX_SIZE;
194         int offset;
195
196         /* Allocate RAM area for config block */
197         config_block = memalign(ARCH_DMA_MINALIGN, size);
198         if (!config_block) {
199                 printf("Not enough malloc space available!\n");
200                 return -ENOMEM;
201         }
202
203         memset(config_block, 0, size);
204
205 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
206         ret = tdx_cfg_block_mmc_storage(config_block, 0);
207 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
208         ret = read_tdx_cfg_block_from_nand(config_block);
209 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
210         ret = read_tdx_cfg_block_from_nor(config_block);
211 #else
212         ret = -EINVAL;
213 #endif
214         if (ret)
215                 goto out;
216
217         /* Expect a valid tag first */
218         tag = (struct toradex_tag *)config_block;
219         if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
220                 valid_cfgblock = false;
221                 ret = -EINVAL;
222                 goto out;
223         }
224         valid_cfgblock = true;
225         offset = 4;
226
227         while (offset < TDX_CFG_BLOCK_MAX_SIZE) {
228                 tag = (struct toradex_tag *)(config_block + offset);
229                 offset += 4;
230                 if (tag->id == TAG_INVALID)
231                         break;
232
233                 if (tag->flags == TAG_FLAG_VALID) {
234                         switch (tag->id) {
235                         case TAG_MAC:
236                                 memcpy(&tdx_eth_addr, config_block + offset,
237                                        6);
238
239                                 /* NIC part of MAC address is serial number */
240                                 tdx_serial = ntohl(tdx_eth_addr.nic) >> 8;
241                                 break;
242                         case TAG_HW:
243                                 memcpy(&tdx_hw_tag, config_block + offset, 8);
244                                 break;
245                         }
246                 }
247
248                 /* Get to next tag according to current tags length */
249                 offset += tag->len * 4;
250         }
251
252         /* Cap product id to avoid issues with a yet unknown one */
253         if (tdx_hw_tag.prodid > (sizeof(toradex_modules) /
254                                   sizeof(toradex_modules[0])))
255                 tdx_hw_tag.prodid = 0;
256
257 out:
258         free(config_block);
259         return ret;
260 }
261
262 static int get_cfgblock_interactive(void)
263 {
264         char message[CONFIG_SYS_CBSIZE];
265         char *soc;
266         char it = 'n';
267         int len;
268
269         if (cpu_is_pxa27x())
270                 sprintf(message, "Is the module the 312 MHz version? [y/N] ");
271         else
272                 sprintf(message, "Is the module an IT version? [y/N] ");
273
274         len = cli_readline(message);
275         it = console_buffer[0];
276
277         soc = getenv("soc");
278         if (!strcmp("mx6", soc)) {
279 #ifdef CONFIG_MACH_TYPE
280                 if (it == 'y' || it == 'Y')
281                         if (is_cpu_type(MXC_CPU_MX6Q))
282                                 tdx_hw_tag.prodid = APALIS_IMX6Q_IT;
283                         else
284                                 tdx_hw_tag.prodid = APALIS_IMX6D_IT;
285                 else
286                         if (is_cpu_type(MXC_CPU_MX6Q))
287                                 tdx_hw_tag.prodid = APALIS_IMX6Q;
288                         else
289                                 tdx_hw_tag.prodid = APALIS_IMX6D;
290 #else
291                 if (it == 'y' || it == 'Y')
292                         if (is_cpu_type(MXC_CPU_MX6DL))
293                                 tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT;
294                         else
295                                 tdx_hw_tag.prodid = COLIBRI_IMX6S_IT;
296                 else
297                         if (is_cpu_type(MXC_CPU_MX6DL))
298                                 tdx_hw_tag.prodid = COLIBRI_IMX6DL;
299                         else
300                                 tdx_hw_tag.prodid = COLIBRI_IMX6S;
301 #endif /* CONFIG_MACH_TYPE */
302         } else if (!strcmp("imx7d", soc)) {
303                 tdx_hw_tag.prodid = COLIBRI_IMX7D;
304         } else if (!strcmp("imx7s", soc)) {
305                 tdx_hw_tag.prodid = COLIBRI_IMX7S;
306         } else if (!strcmp("tegra20", soc)) {
307                 if (it == 'y' || it == 'Y')
308                         if (gd->ram_size == 0x10000000)
309                                 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
310                         else
311                                 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
312                 else
313                         if (gd->ram_size == 0x10000000)
314                                 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
315                         else
316                                 tdx_hw_tag.prodid = COLIBRI_T20_512MB;
317         } else if (cpu_is_pxa27x()) {
318                 if (it == 'y' || it == 'Y')
319                         tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ;
320                 else
321                         tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
322 #ifdef CONFIG_MACH_TYPE
323         } else if (!strcmp("tegra30", soc)) {
324                 if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) {
325                         if (it == 'y' || it == 'Y')
326                                 tdx_hw_tag.prodid = APALIS_T30_IT;
327                         else
328                                 if (gd->ram_size == 0x40000000)
329                                         tdx_hw_tag.prodid = APALIS_T30_1GB;
330                                 else
331                                         tdx_hw_tag.prodid = APALIS_T30_2GB;
332                 } else {
333                         if (it == 'y' || it == 'Y')
334                                 tdx_hw_tag.prodid = COLIBRI_T30_IT;
335                         else
336                                 tdx_hw_tag.prodid = COLIBRI_T30;
337                 }
338 #endif /* CONFIG_MACH_TYPE */
339         } else if (!strcmp("tegra124", soc)) {
340                 tdx_hw_tag.prodid = APALIS_TK1_2GB;
341         } else if (!strcmp("vf500", soc)) {
342                 if (it == 'y' || it == 'Y')
343                         tdx_hw_tag.prodid = COLIBRI_VF50_IT;
344                 else
345                         tdx_hw_tag.prodid = COLIBRI_VF50;
346         } else if (!strcmp("vf610", soc)) {
347                 if (it == 'y' || it == 'Y')
348                         tdx_hw_tag.prodid = COLIBRI_VF61_IT;
349                 else
350                         tdx_hw_tag.prodid = COLIBRI_VF61;
351         } else {
352                 printf("Module type not detectable due to unknown SoC\n");
353                 return -1;
354         }
355
356         while (len < 4) {
357                 sprintf(message, "Enter the module version (e.g. V1.1B): V");
358                 len = cli_readline(message);
359         }
360
361         tdx_hw_tag.ver_major = console_buffer[0] - '0';
362         tdx_hw_tag.ver_minor = console_buffer[2] - '0';
363         tdx_hw_tag.ver_assembly = console_buffer[3] - 'A';
364
365         if (cpu_is_pxa27x() && (tdx_hw_tag.ver_major == 1))
366                 tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
367                                        COLIBRI_PXA270_V1_312MHZ);
368
369         while (len < 8) {
370                 sprintf(message, "Enter module serial number: ");
371                 len = cli_readline(message);
372         }
373
374         tdx_serial = simple_strtoul(console_buffer, NULL, 10);
375
376         return 0;
377 }
378
379 static int get_cfgblock_barcode(char *barcode)
380 {
381         if (strlen(barcode) < 16) {
382                 printf("Argument too short, barcode is 16 chars long\n");
383                 return -1;
384         }
385
386         /* Get hardware information from the first 8 digits */
387         tdx_hw_tag.ver_major = barcode[4] - '0';
388         tdx_hw_tag.ver_minor = barcode[5] - '0';
389         tdx_hw_tag.ver_assembly = barcode[7] - '0';
390
391         barcode[4] = '\0';
392         tdx_hw_tag.prodid = simple_strtoul(barcode, NULL, 10);
393
394         /* Parse second part of the barcode (serial number */
395         barcode += 8;
396         tdx_serial = simple_strtoul(barcode, NULL, 10);
397
398         return 0;
399 }
400
401 static int do_cfgblock_create(cmd_tbl_t *cmdtp, int flag, int argc,
402                               char * const argv[])
403 {
404         u8 *config_block;
405         struct toradex_tag *tag;
406         size_t size = TDX_CFG_BLOCK_MAX_SIZE;
407         int offset = 0;
408         int ret = CMD_RET_SUCCESS;
409         int err;
410
411         /* Allocate RAM area for config block */
412         config_block = memalign(ARCH_DMA_MINALIGN, size);
413         if (!config_block) {
414                 printf("Not enough malloc space available!\n");
415                 return CMD_RET_FAILURE;
416         }
417
418         memset(config_block, 0xff, size);
419
420         read_tdx_cfg_block();
421         if (valid_cfgblock) {
422 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
423                 /*
424                  * On NAND devices, recreation is only allowed if the page is
425                  * empty (config block invalid...)
426                  */
427                 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
428                        CONFIG_TDX_CFG_BLOCK_OFFSET / nand_info[0]->erasesize);
429                 goto out;
430 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
431                 /*
432                  * On NOR devices, recreation is only allowed if the sector is
433                  * empty and write protection is off (config block invalid...)
434                  */
435                 printf("NOR sector at offset 0x%02x need to be erased and unprotected before creating a Toradex config block\n",
436                        CONFIG_TDX_CFG_BLOCK_OFFSET);
437                 goto out;
438 #else
439                 char message[CONFIG_SYS_CBSIZE];
440                 sprintf(message,
441                         "A valid Toradex config block is present, still recreate? [y/N] ");
442
443                 if (!cli_readline(message))
444                         goto out;
445
446                 if (console_buffer[0] != 'y' && console_buffer[0] != 'Y')
447                         goto out;
448 #endif
449         }
450
451         /* Parse new Toradex config block data... */
452         if (argc < 3)
453                 err = get_cfgblock_interactive();
454         else
455                 err = get_cfgblock_barcode(argv[2]);
456
457         if (err) {
458                 ret = CMD_RET_FAILURE;
459                 goto out;
460         }
461
462         /* Convert serial number to MAC address (the storage format) */
463         tdx_eth_addr.oui = htonl(0x00142dUL << 8);
464         tdx_eth_addr.nic = htonl(tdx_serial << 8);
465
466         /* Valid Tag */
467         tag = (struct toradex_tag *)config_block;
468         tag->id = TAG_VALID;
469         tag->flags = TAG_FLAG_VALID;
470         tag->len = 0;
471         offset += 4;
472
473         /* Product Tag */
474         tag = (struct toradex_tag *)(config_block + offset);
475         tag->id = TAG_HW;
476         tag->flags = TAG_FLAG_VALID;
477         tag->len = 2;
478         offset += 4;
479
480         memcpy(config_block + offset, &tdx_hw_tag, 8);
481         offset += 8;
482
483         /* MAC Tag */
484         tag = (struct toradex_tag *)(config_block + offset);
485         tag->id = TAG_MAC;
486         tag->flags = TAG_FLAG_VALID;
487         tag->len = 2;
488         offset += 4;
489
490         memcpy(config_block + offset, &tdx_eth_addr, 6);
491         offset += 6;
492         memset(config_block + offset, 0, 32 - offset);
493
494 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
495         err = tdx_cfg_block_mmc_storage(config_block, 1);
496 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
497         err = write_tdx_cfg_block_to_nand(config_block);
498 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
499         err = write_tdx_cfg_block_to_nor(config_block);
500 #else
501         err = -EINVAL;
502 #endif
503         if (err) {
504                 printf("Failed to write Toradex config block: %d\n", ret);
505                 ret = CMD_RET_FAILURE;
506                 goto out;
507         }
508
509         printf("Toradex config block successfully written\n");
510
511 out:
512         free(config_block);
513         return ret;
514 }
515
516 static int do_cfgblock(cmd_tbl_t *cmdtp, int flag, int argc,
517                        char * const argv[])
518 {
519         int ret;
520
521         if (argc < 2)
522                 return CMD_RET_USAGE;
523
524         if (!strcmp(argv[1], "create")) {
525                 return do_cfgblock_create(cmdtp, flag, argc, argv);
526         } else if (!strcmp(argv[1], "reload")) {
527                 ret = read_tdx_cfg_block();
528                 if (ret) {
529                         printf("Failed to reload Toradex config block: %d\n",
530                                ret);
531                         return CMD_RET_FAILURE;
532                 }
533                 return CMD_RET_SUCCESS;
534         }
535
536         return CMD_RET_USAGE;
537 }
538
539 U_BOOT_CMD(
540         cfgblock, 3, 0, do_cfgblock,
541         "Toradex config block handling commands",
542         "create [barcode] - (Re-)create Toradex config block\n"
543         "cfgblock reload - Reload Toradex config block from flash"
544 );