usb: Enforce DM_USB migration for USB_HOST devices.
[platform/kernel/u-boot.git] / arch / arm / mach-imx / cmd_nandbcb.c
1 /*
2  * i.MX nand boot control block(bcb).
3  *
4  * Based on the common/imx-bbu-nand-fcb.c from barebox and imx kobs-ng
5  *
6  * Copyright (C) 2017 Jagan Teki <jagan@amarulasolutions.com>
7  * Copyright (C) 2016 Sergey Kubushyn <ksi@koi8.net>
8  *
9  * Reconstucted by Han Xu <han.xu@nxp.com>
10  *
11  * SPDX-License-Identifier:     GPL-2.0+
12  */
13
14 #include <common.h>
15 #include <command.h>
16 #include <log.h>
17 #include <malloc.h>
18 #include <nand.h>
19 #include <dm/devres.h>
20 #include <linux/bug.h>
21
22 #include <asm/io.h>
23 #include <jffs2/jffs2.h>
24 #include <linux/bch.h>
25 #include <linux/mtd/mtd.h>
26
27 #include <asm/arch/sys_proto.h>
28 #include <asm/mach-imx/imx-nandbcb.h>
29 #include <asm/mach-imx/imximage.cfg>
30 #include <mxs_nand.h>
31 #include <linux/mtd/mtd.h>
32 #include <nand.h>
33 #include <fuse.h>
34
35 #include "../../../cmd/legacy-mtd-utils.h"
36
37 /* FCB related flags */
38 /* FCB layout with leading 12B reserved */
39 #define FCB_LAYOUT_RESV_12B             BIT(0)
40 /* FCB layout with leading 32B meta data */
41 #define FCB_LAYOUT_META_32B             BIT(1)
42 /* FCB encrypted by Hamming code */
43 #define FCB_ENCODE_HAMMING              BIT(2)
44 /* FCB encrypted by 40bit BCH */
45 #define FCB_ENCODE_BCH_40b              BIT(3)
46 /* FCB encrypted by 62bit BCH */
47 #define FCB_ENCODE_BCH_62b              BIT(4)
48 /* FCB encrypted by BCH */
49 #define FCB_ENCODE_BCH                  (FCB_ENCODE_BCH_40b | FCB_ENCODE_BCH_62b)
50 /* FCB data was randomized */
51 #define FCB_RANDON_ENABLED              BIT(5)
52
53 /* Firmware related flags */
54 /* No 1K padding */
55 #define FIRMWARE_NEED_PADDING           BIT(8)
56 /* Extra firmware*/
57 #define FIRMWARE_EXTRA_ONE              BIT(9)
58 /* Secondary firmware on fixed address */
59 #define FIRMWARE_SECONDARY_FIXED_ADDR   BIT(10)
60
61 /* Boot search related flags */
62 #define BT_SEARCH_CNT_FROM_FUSE         BIT(16)
63
64 struct platform_config {
65         int misc_flags;
66 };
67
68 static struct platform_config plat_config;
69
70 /* imx6q/dl/solo */
71 static struct platform_config imx6qdl_plat_config = {
72         .misc_flags = FCB_LAYOUT_RESV_12B |
73                      FCB_ENCODE_HAMMING |
74                      FIRMWARE_NEED_PADDING,
75 };
76
77 static struct platform_config imx6sx_plat_config = {
78         .misc_flags = FCB_LAYOUT_META_32B |
79                      FCB_ENCODE_BCH_62b |
80                      FIRMWARE_NEED_PADDING |
81                      FCB_RANDON_ENABLED,
82 };
83
84 static struct platform_config imx7d_plat_config = {
85         .misc_flags = FCB_LAYOUT_META_32B |
86                      FCB_ENCODE_BCH_62b |
87                      FIRMWARE_NEED_PADDING |
88                      FCB_RANDON_ENABLED,
89 };
90
91 /* imx6ul/ull/ulz */
92 static struct platform_config imx6ul_plat_config = {
93         .misc_flags = FCB_LAYOUT_META_32B |
94                      FCB_ENCODE_BCH_40b |
95                      FIRMWARE_NEED_PADDING,
96 };
97
98 static struct platform_config imx8mq_plat_config = {
99         .misc_flags = FCB_LAYOUT_META_32B |
100                      FCB_ENCODE_BCH_62b |
101                      FIRMWARE_NEED_PADDING |
102                      FCB_RANDON_ENABLED |
103                      FIRMWARE_EXTRA_ONE,
104 };
105
106 /* all other imx8mm */
107 static struct platform_config imx8mm_plat_config = {
108         .misc_flags = FCB_LAYOUT_META_32B |
109                      FCB_ENCODE_BCH_62b |
110                      FIRMWARE_NEED_PADDING |
111                      FCB_RANDON_ENABLED,
112 };
113
114 /* imx8mn */
115 static struct platform_config imx8mn_plat_config = {
116         .misc_flags = FCB_LAYOUT_META_32B |
117                      FCB_ENCODE_BCH_62b |
118                      FCB_RANDON_ENABLED |
119                      FIRMWARE_SECONDARY_FIXED_ADDR |
120                      BT_SEARCH_CNT_FROM_FUSE,
121 };
122
123 /* imx8qx/qm */
124 static struct platform_config imx8q_plat_config = {
125         .misc_flags = FCB_LAYOUT_META_32B |
126                      FCB_ENCODE_BCH_62b |
127                      FCB_RANDON_ENABLED |
128                      FIRMWARE_SECONDARY_FIXED_ADDR |
129                      BT_SEARCH_CNT_FROM_FUSE,
130 };
131
132 /* boot search related variables and definitions */
133 static int g_boot_search_count = 4;
134 static int g_boot_search_stride;
135 static int g_pages_per_stride;
136
137 /* mtd config structure */
138 struct boot_config {
139         int dev;
140         struct mtd_info *mtd;
141         loff_t maxsize;
142         loff_t input_size;
143         loff_t offset;
144         loff_t boot_stream1_address;
145         loff_t boot_stream2_address;
146         size_t boot_stream1_size;
147         size_t boot_stream2_size;
148         size_t max_boot_stream_size;
149         int stride_size_in_byte;
150         int search_area_size_in_bytes;
151         int search_area_size_in_pages;
152         int secondary_boot_stream_off_in_MB;
153 };
154
155 /* boot_stream config structure */
156 struct boot_stream_config {
157         char bs_label[32];
158         loff_t bs_addr;
159         size_t bs_size;
160         void *bs_buf;
161         loff_t next_bs_addr;
162         bool need_padding;
163 };
164
165 /* FW index */
166 #define FW1_ONLY        1
167 #define FW2_ONLY        2
168 #define FW_ALL          FW1_ONLY | FW2_ONLY
169 #define FW_INX(x)       (1 << (x))
170
171 /* NAND convert macros */
172 #define CONV_TO_PAGES(x)        ((u32)(x) / (u32)(mtd->writesize))
173 #define CONV_TO_BLOCKS(x)       ((u32)(x) / (u32)(mtd->erasesize))
174
175 #define GETBIT(v, n)            (((v) >> (n)) & 0x1)
176 #define IMX8MQ_SPL_SZ 0x3e000
177 #define IMX8MQ_HDMI_FW_SZ 0x19c00
178
179 static int nandbcb_get_info(int argc, char * const argv[],
180                             struct boot_config *boot_cfg)
181 {
182         int dev;
183         struct mtd_info *mtd;
184
185         dev = nand_curr_device;
186         if (dev < 0) {
187                 printf("failed to get nand_curr_device, run nand device\n");
188                 return CMD_RET_FAILURE;
189         }
190
191         mtd = get_nand_dev_by_index(dev);
192         if (!mtd) {
193                 printf("failed to get mtd info\n");
194                 return CMD_RET_FAILURE;
195         }
196
197         boot_cfg->dev = dev;
198         boot_cfg->mtd = mtd;
199
200         return CMD_RET_SUCCESS;
201 }
202
203 static int nandbcb_get_size(int argc, char * const argv[], int num,
204                             struct boot_config *boot_cfg)
205 {
206         int dev;
207         loff_t offset, size, maxsize;
208         struct mtd_info *mtd;
209
210         dev = boot_cfg->dev;
211         mtd = boot_cfg->mtd;
212         size = 0;
213
214         if (mtd_arg_off_size(argc - num, argv + num, &dev, &offset, &size,
215                              &maxsize, MTD_DEV_TYPE_NAND, mtd->size))
216                 return CMD_RET_FAILURE;
217
218         boot_cfg->maxsize = maxsize;
219         boot_cfg->offset = offset;
220
221         debug("max: %llx, offset: %llx\n", maxsize, offset);
222
223         if (size && size != maxsize)
224                 boot_cfg->input_size = size;
225
226         return CMD_RET_SUCCESS;
227 }
228
229 static int nandbcb_set_boot_config(int argc, char * const argv[],
230                                    struct boot_config *boot_cfg)
231 {
232         struct mtd_info *mtd;
233         loff_t maxsize;
234         loff_t boot_stream1_address, boot_stream2_address, max_boot_stream_size;
235
236         if (!boot_cfg->mtd) {
237                 printf("Didn't get the mtd info, quit\n");
238                 return CMD_RET_FAILURE;
239         }
240         mtd = boot_cfg->mtd;
241
242         /*
243          * By default
244          * set the search count as 4
245          * set each FCB/DBBT/Firmware offset at the beginning of blocks
246          * customers may change the value as needed
247          */
248
249         /* if need more compact layout, change these values */
250         /* g_boot_search_count was set as 4 at the definition*/
251         /* g_pages_per_stride was set as block size */
252
253         g_pages_per_stride = mtd->erasesize / mtd->writesize;
254
255         g_boot_search_stride = mtd->writesize * g_pages_per_stride;
256
257         boot_cfg->stride_size_in_byte = g_boot_search_stride * mtd->writesize;
258         boot_cfg->search_area_size_in_bytes =
259                 g_boot_search_count * g_boot_search_stride;
260         boot_cfg->search_area_size_in_pages =
261                 boot_cfg->search_area_size_in_bytes / mtd->writesize;
262
263         /* after FCB/DBBT, split the rest of area for two Firmwares */
264         if (!boot_cfg->maxsize) {
265                 printf("Didn't get the maxsize, quit\n");
266                 return CMD_RET_FAILURE;
267         }
268         maxsize = boot_cfg->maxsize;
269         /* align to page boundary */
270         maxsize = ((u32)(maxsize + mtd->writesize - 1)) / (u32)mtd->writesize
271                         * mtd->writesize;
272
273         boot_stream1_address = 2 * boot_cfg->search_area_size_in_bytes;
274         boot_stream2_address = ((maxsize - boot_stream1_address) / 2 +
275                                boot_stream1_address);
276
277         if (boot_cfg->secondary_boot_stream_off_in_MB)
278                 boot_stream2_address =
279                         (loff_t)boot_cfg->secondary_boot_stream_off_in_MB * 1024 * 1024;
280
281         max_boot_stream_size = boot_stream2_address - boot_stream1_address;
282
283         /* sanity check */
284         if (max_boot_stream_size <= 0) {
285                 debug("st1_addr: %llx, st2_addr: %llx, max: %llx\n",
286                       boot_stream1_address, boot_stream2_address,
287                       max_boot_stream_size);
288                 printf("something wrong with firmware address settings\n");
289                 return CMD_RET_FAILURE;
290         }
291         boot_cfg->boot_stream1_address = boot_stream1_address;
292         boot_cfg->boot_stream2_address = boot_stream2_address;
293         boot_cfg->max_boot_stream_size = max_boot_stream_size;
294
295         /* set the boot_stream size as the input size now */
296         if (boot_cfg->input_size) {
297                 boot_cfg->boot_stream1_size = boot_cfg->input_size;
298                 boot_cfg->boot_stream2_size = boot_cfg->input_size;
299         }
300
301         return CMD_RET_SUCCESS;
302 }
303
304 static int nandbcb_check_space(struct boot_config *boot_cfg)
305 {
306         size_t maxsize = boot_cfg->maxsize;
307         size_t max_boot_stream_size = boot_cfg->max_boot_stream_size;
308         loff_t boot_stream2_address = boot_cfg->boot_stream2_address;
309
310         if (boot_cfg->boot_stream1_size &&
311             boot_cfg->boot_stream1_size > max_boot_stream_size) {
312                 printf("boot stream1 doesn't fit, check partition size or settings\n");
313                 return CMD_RET_FAILURE;
314         }
315
316         if (boot_cfg->boot_stream2_size &&
317             boot_cfg->boot_stream2_size > maxsize - boot_stream2_address) {
318                 printf("boot stream2 doesn't fit, check partition size or settings\n");
319                 return CMD_RET_FAILURE;
320         }
321
322         return CMD_RET_SUCCESS;
323 }
324
325 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
326 static uint8_t reverse_bit(uint8_t b)
327 {
328         b = (b & 0xf0) >> 4 | (b & 0x0f) << 4;
329         b = (b & 0xcc) >> 2 | (b & 0x33) << 2;
330         b = (b & 0xaa) >> 1 | (b & 0x55) << 1;
331
332         return b;
333 }
334
335 static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits)
336 {
337         int i, j, m = 13;
338         int blocksize = 128;
339         int numblocks = 8;
340         int ecc_buf_size = (m * eccbits + 7) / 8;
341         struct bch_control *bch = init_bch(m, eccbits, 0);
342         u8 *ecc_buf = kzalloc(ecc_buf_size, GFP_KERNEL);
343         u8 *tmp_buf = kzalloc(blocksize * numblocks, GFP_KERNEL);
344         u8 *psrc, *pdst;
345
346         /*
347          * The blocks here are bit aligned. If eccbits is a multiple of 8,
348          * we just can copy bytes. Otherwiese we must move the blocks to
349          * the next free bit position.
350          */
351         WARN_ON(eccbits % 8);
352
353         memcpy(tmp_buf, fcb, sizeof(*fcb));
354
355         for (i = 0; i < numblocks; i++) {
356                 memset(ecc_buf, 0, ecc_buf_size);
357                 psrc = tmp_buf + i * blocksize;
358                 pdst = buf + i * (blocksize + ecc_buf_size);
359
360                 /* copy data byte aligned to destination buf */
361                 memcpy(pdst, psrc, blocksize);
362
363                 /*
364                  * imx-kobs use a modified encode_bch which reverse the
365                  * bit order of the data before calculating bch.
366                  * Do this in the buffer and use the bch lib here.
367                  */
368                 for (j = 0; j < blocksize; j++)
369                         psrc[j] = reverse_bit(psrc[j]);
370
371                 encode_bch(bch, psrc, blocksize, ecc_buf);
372
373                 /* reverse ecc bit */
374                 for (j = 0; j < ecc_buf_size; j++)
375                         ecc_buf[j] = reverse_bit(ecc_buf[j]);
376
377                 /* Here eccbuf is byte aligned and we can just copy it */
378                 memcpy(pdst + blocksize, ecc_buf, ecc_buf_size);
379         }
380
381         kfree(ecc_buf);
382         kfree(tmp_buf);
383         free_bch(bch);
384 }
385 #else
386
387 static u8 calculate_parity_13_8(u8 d)
388 {
389         u8 p = 0;
390
391         p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0;
392         p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^
393               GETBIT(d, 1)) << 1;
394         p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^
395               GETBIT(d, 0)) << 2;
396         p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3;
397         p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^
398               GETBIT(d, 1) ^ GETBIT(d, 0)) << 4;
399
400         return p;
401 }
402
403 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
404 {
405         int i;
406         u8 *src = _src;
407         u8 *ecc = _ecc;
408
409         for (i = 0; i < size; i++)
410                 ecc[i] = calculate_parity_13_8(src[i]);
411 }
412 #endif
413
414 static u32 calc_chksum(void *buf, size_t size)
415 {
416         u32 chksum = 0;
417         u8 *bp = buf;
418         size_t i;
419
420         for (i = 0; i < size; i++)
421                 chksum += bp[i];
422
423         return ~chksum;
424 }
425
426 static void fill_fcb(struct fcb_block *fcb, struct boot_config *boot_cfg)
427 {
428         struct mtd_info *mtd = boot_cfg->mtd;
429         struct nand_chip *chip = mtd_to_nand(mtd);
430         struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
431         struct mxs_nand_layout l;
432
433         mxs_nand_get_layout(mtd, &l);
434
435         fcb->fingerprint = FCB_FINGERPRINT;
436         fcb->version = FCB_VERSION_1;
437
438         fcb->datasetup = 80;
439         fcb->datahold = 60;
440         fcb->addr_setup = 25;
441         fcb->dsample_time = 6;
442
443         fcb->pagesize = mtd->writesize;
444         fcb->oob_pagesize = mtd->writesize + mtd->oobsize;
445         fcb->sectors = mtd->erasesize / mtd->writesize;
446
447         fcb->meta_size = l.meta_size;
448         fcb->nr_blocks = l.nblocks;
449         fcb->ecc_nr = l.data0_size;
450         fcb->ecc_level = l.ecc0;
451         fcb->ecc_size = l.datan_size;
452         fcb->ecc_type = l.eccn;
453         fcb->bchtype = l.gf_len;
454
455         /* DBBT search area starts from the next block after all FCB */
456         fcb->dbbt_start = boot_cfg->search_area_size_in_pages;
457
458         fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset;
459         fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset;
460
461         fcb->phy_offset = mtd->writesize;
462
463         fcb->disbbm = 0;
464
465         fcb->fw1_start = CONV_TO_PAGES(boot_cfg->boot_stream1_address);
466         fcb->fw2_start = CONV_TO_PAGES(boot_cfg->boot_stream2_address);
467         fcb->fw1_pages = CONV_TO_PAGES(boot_cfg->boot_stream1_size);
468         fcb->fw2_pages = CONV_TO_PAGES(boot_cfg->boot_stream2_size);
469
470         fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
471 }
472
473 static int fill_dbbt_data(struct mtd_info *mtd, void *buf, int num_blocks)
474 {
475         int n, n_bad_blocks = 0;
476         u32 *bb = buf + 0x8;
477         u32 *n_bad_blocksp = buf + 0x4;
478
479         for (n = 0; n < num_blocks; n++) {
480                 loff_t offset = (loff_t)n * mtd->erasesize;
481                         if (mtd_block_isbad(mtd, offset)) {
482                                 n_bad_blocks++;
483                                 *bb = n;
484                                 bb++;
485                 }
486         }
487
488         *n_bad_blocksp = n_bad_blocks;
489
490         return n_bad_blocks;
491 }
492
493 /*
494  * return 1     - bad block
495  * return 0     - read successfully
496  * return < 0   - read failed
497  */
498 static int read_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb,
499                     loff_t off)
500 {
501         struct mtd_info *mtd;
502         void *fcb_raw_page;
503         size_t size;
504         int ret = 0;
505
506         mtd = boot_cfg->mtd;
507         if (mtd_block_isbad(mtd, off)) {
508                 printf("Block %d is bad, skipped\n", (int)CONV_TO_BLOCKS(off));
509                 return 1;
510         }
511
512         fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
513         if (!fcb_raw_page) {
514                 debug("failed to allocate fcb_raw_page\n");
515                 ret = -ENOMEM;
516                 return ret;
517         }
518
519         /*
520          * User BCH hardware to decode ECC for FCB
521          */
522         if (plat_config.misc_flags & FCB_ENCODE_BCH) {
523                 size = sizeof(struct fcb_block);
524
525                 /* switch nand BCH to FCB compatible settings */
526                 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
527                         mxs_nand_mode_fcb_62bit(mtd);
528                 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
529                         mxs_nand_mode_fcb_40bit(mtd);
530
531                 ret = nand_read(mtd, off, &size, (u_char *)fcb);
532
533                 /* switch BCH back */
534                 mxs_nand_mode_normal(mtd);
535                 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
536                        off, size, ret ? "ERROR" : "OK");
537
538         } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
539                 /* raw read*/
540                 mtd_oob_ops_t ops = {
541                         .datbuf = (u8 *)fcb_raw_page,
542                         .oobbuf = ((u8 *)fcb_raw_page) + mtd->writesize,
543                         .len = mtd->writesize,
544                         .ooblen = mtd->oobsize,
545                         .mode = MTD_OPS_RAW
546                         };
547
548                 ret = mtd_read_oob(mtd, off, &ops);
549                 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
550                        off, ops.len, ret ? "ERROR" : "OK");
551         }
552
553         if (ret)
554                 goto fcb_raw_page_err;
555
556         if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
557             (plat_config.misc_flags & FCB_LAYOUT_RESV_12B))
558                 memcpy(fcb, fcb_raw_page + 12, sizeof(struct fcb_block));
559
560 /* TODO: check if it can pass Hamming check */
561
562 fcb_raw_page_err:
563         kfree(fcb_raw_page);
564
565         return ret;
566 }
567
568 static int write_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb)
569 {
570         struct mtd_info *mtd;
571         void *fcb_raw_page = NULL;
572         int i, ret = 0;
573         loff_t off;
574         size_t size;
575
576         mtd = boot_cfg->mtd;
577
578         /*
579          * We prepare raw page only for i.MX6, for i.MX7 we
580          * leverage BCH hw module instead
581          */
582         if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
583             (plat_config.misc_flags & FCB_LAYOUT_RESV_12B)) {
584                 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize,
585                                        GFP_KERNEL);
586                 if (!fcb_raw_page) {
587                         debug("failed to allocate fcb_raw_page\n");
588                         ret = -ENOMEM;
589                         return ret;
590                 }
591
592 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
593                 /* 40 bit BCH, for i.MX6UL(L) */
594                 encode_bch_ecc(fcb_raw_page + 32, fcb, 40);
595 #else
596                 memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block));
597                 encode_hamming_13_8(fcb_raw_page + 12,
598                                     fcb_raw_page + 12 + 512, 512);
599 #endif
600                 /*
601                  * Set the first and second byte of OOB data to 0xFF,
602                  * not 0x00. These bytes are used as the Manufacturers Bad
603                  * Block Marker (MBBM). Since the FCB is mostly written to
604                  * the first page in a block, a scan for
605                  * factory bad blocks will detect these blocks as bad, e.g.
606                  * when function nand_scan_bbt() is executed to build a new
607                  * bad block table.
608                  */
609                 memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
610         }
611
612         /* start writing FCB from the very beginning */
613         off = 0;
614
615         for (i = 0; i < g_boot_search_count; i++) {
616                 if (mtd_block_isbad(mtd, off)) {
617                         printf("Block %d is bad, skipped\n", i);
618                         continue;
619                 }
620
621                 /*
622                  * User BCH hardware module to generate ECC for FCB
623                  */
624                 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
625                         size = sizeof(struct fcb_block);
626
627                         /* switch nand BCH to FCB compatible settings */
628                         if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
629                                 mxs_nand_mode_fcb_62bit(mtd);
630                         else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
631                                 mxs_nand_mode_fcb_40bit(mtd);
632
633                         ret = nand_write(mtd, off, &size, (u_char *)fcb);
634
635                         /* switch BCH back */
636                         mxs_nand_mode_normal(mtd);
637                         printf("NAND FCB write to 0x%zx offset 0x%llx written: %s\n",
638                                size, off, ret ? "ERROR" : "OK");
639
640                 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
641                         /* raw write */
642                         mtd_oob_ops_t ops = {
643                                 .datbuf = (u8 *)fcb_raw_page,
644                                 .oobbuf = ((u8 *)fcb_raw_page) +
645                                           mtd->writesize,
646                                 .len = mtd->writesize,
647                                 .ooblen = mtd->oobsize,
648                                 .mode = MTD_OPS_RAW
649                         };
650
651                         ret = mtd_write_oob(mtd, off, &ops);
652                         printf("NAND FCB write to 0x%llxx offset 0x%zx written: %s\n", off, ops.len, ret ? "ERROR" : "OK");
653                 }
654
655                 if (ret)
656                         goto fcb_raw_page_err;
657
658                 /* next writing location */
659                 off += g_boot_search_stride;
660         }
661
662 fcb_raw_page_err:
663         kfree(fcb_raw_page);
664
665         return ret;
666 }
667
668 /*
669  * return 1     - bad block
670  * return 0     - read successfully
671  * return < 0   - read failed
672  */
673 static int read_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
674                       void *dbbt_data_page, loff_t off)
675 {
676         size_t size;
677         struct mtd_info *mtd;
678         loff_t to;
679         int ret;
680
681         mtd = boot_cfg->mtd;
682
683         if (mtd_block_isbad(mtd, off)) {
684                 printf("Block %d is bad, skipped\n",
685                        (int)CONV_TO_BLOCKS(off));
686                 return 1;
687         }
688
689         size = sizeof(struct dbbt_block);
690         ret = nand_read(mtd, off, &size, (u_char *)dbbt);
691         printf("NAND DBBT read from 0x%llx offset 0x%zx read: %s\n",
692                off, size, ret ? "ERROR" : "OK");
693         if (ret)
694                 return ret;
695
696         /* dbbtpages == 0 if no bad blocks */
697         if (dbbt->dbbtpages > 0) {
698                 to = off + 4 * mtd->writesize;
699                 size = mtd->writesize;
700                 ret = nand_read(mtd, to, &size, dbbt_data_page);
701                 printf("DBBT data read from 0x%llx offset 0x%zx read: %s\n",
702                        to, size, ret ? "ERROR" : "OK");
703
704                 if (ret)
705                         return ret;
706         }
707
708         return 0;
709 }
710
711 static int write_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
712                       void *dbbt_data_page)
713 {
714         int i;
715         loff_t off, to;
716         size_t size;
717         struct mtd_info *mtd;
718         int ret;
719
720         mtd = boot_cfg->mtd;
721
722         /* start writing DBBT after all FCBs */
723         off = boot_cfg->search_area_size_in_bytes;
724         size = mtd->writesize;
725
726         for (i = 0; i < g_boot_search_count; i++) {
727                 if (mtd_block_isbad(mtd, off)) {
728                         printf("Block %d is bad, skipped\n",
729                                (int)(i + CONV_TO_BLOCKS(off)));
730                         continue;
731                 }
732
733                 ret = nand_write(mtd, off, &size, (u_char *)dbbt);
734                 printf("NAND DBBT write to 0x%llx offset 0x%zx written: %s\n",
735                        off, size, ret ? "ERROR" : "OK");
736                 if (ret)
737                         return ret;
738
739                 /* dbbtpages == 0 if no bad blocks */
740                 if (dbbt->dbbtpages > 0) {
741                         to = off + 4 * mtd->writesize;
742                         ret = nand_write(mtd, to, &size, dbbt_data_page);
743                         printf("DBBT data write to 0x%llx offset 0x%zx written: %s\n",
744                                to, size, ret ? "ERROR" : "OK");
745
746                 if (ret)
747                         return ret;
748                 }
749
750                 /* next writing location */
751                 off += g_boot_search_stride;
752         }
753
754         return 0;
755 }
756
757 /* reuse the check_skip_len from nand_util.c with minor change*/
758 static int check_skip_length(struct boot_config *boot_cfg, loff_t offset,
759                              size_t length, size_t *used)
760 {
761         struct mtd_info *mtd = boot_cfg->mtd;
762         size_t maxsize = boot_cfg->maxsize;
763         size_t len_excl_bad = 0;
764         int ret = 0;
765
766         while (len_excl_bad < length) {
767                 size_t block_len, block_off;
768                 loff_t block_start;
769
770                 if (offset >= maxsize)
771                         return -1;
772
773                 block_start = offset & ~(loff_t)(mtd->erasesize - 1);
774                 block_off = offset & (mtd->erasesize - 1);
775                 block_len = mtd->erasesize - block_off;
776
777                 if (!nand_block_isbad(mtd, block_start))
778                         len_excl_bad += block_len;
779                 else
780                         ret = 1;
781
782                 offset += block_len;
783                 *used += block_len;
784         }
785
786         /* If the length is not a multiple of block_len, adjust. */
787         if (len_excl_bad > length)
788                 *used -= (len_excl_bad - length);
789
790         return ret;
791 }
792
793 static int nandbcb_get_next_good_blk_addr(struct boot_config *boot_cfg,
794                                           struct boot_stream_config *bs_cfg)
795 {
796         struct mtd_info *mtd = boot_cfg->mtd;
797         loff_t offset = bs_cfg->bs_addr;
798         size_t length = bs_cfg->bs_size;
799         size_t used = 0;
800         int ret;
801
802         ret = check_skip_length(boot_cfg, offset, length, &used);
803
804         if (ret < 0)
805                 return ret;
806
807         /* get next image address */
808         bs_cfg->next_bs_addr = (u32)(offset + used + mtd->erasesize - 1)
809                                  / (u32)mtd->erasesize * mtd->erasesize;
810
811         return ret;
812 }
813
814 static int nandbcb_write_bs_skip_bad(struct boot_config *boot_cfg,
815                                      struct boot_stream_config *bs_cfg)
816 {
817         struct mtd_info *mtd;
818         void *buf;
819         loff_t offset, maxsize;
820         size_t size;
821         size_t length;
822         int ret;
823         bool padding_flag = false;
824
825         mtd = boot_cfg->mtd;
826         offset = bs_cfg->bs_addr;
827         maxsize = boot_cfg->maxsize;
828         size = bs_cfg->bs_size;
829
830         /* some boot images may need leading offset */
831         if (bs_cfg->need_padding &&
832             (plat_config.misc_flags & FIRMWARE_NEED_PADDING))
833                 padding_flag = 1;
834
835         if (padding_flag)
836                 length = ALIGN(size + FLASH_OFFSET_STANDARD, mtd->writesize);
837         else
838                 length = ALIGN(size, mtd->writesize);
839
840         buf = kzalloc(length, GFP_KERNEL);
841         if (!buf) {
842                 printf("failed to allocate buffer for firmware\n");
843                 ret = -ENOMEM;
844                 return ret;
845         }
846
847         if (padding_flag)
848                 memcpy(buf + FLASH_OFFSET_STANDARD, bs_cfg->bs_buf, size);
849         else
850                 memcpy(buf, bs_cfg->bs_buf, size);
851
852         ret = nand_write_skip_bad(mtd, offset, &length, NULL, maxsize,
853                                   (u_char *)buf, WITH_WR_VERIFY);
854         printf("Write %s @0x%llx offset, 0x%zx bytes written: %s\n",
855                bs_cfg->bs_label, offset, length, ret ? "ERROR" : "OK");
856
857         if (ret)
858                 /* write image failed, quit */
859                 goto err;
860
861         /* get next good blk address if needed */
862         if (bs_cfg->need_padding) {
863                 ret = nandbcb_get_next_good_blk_addr(boot_cfg, bs_cfg);
864                 if (ret < 0) {
865                         printf("Next image cannot fit in NAND partition\n");
866                         goto err;
867                 }
868         }
869
870         /* now we know how the exact image size written to NAND */
871         bs_cfg->bs_size = length;
872         return 0;
873 err:
874         kfree(buf);
875         return ret;
876 }
877
878 static int nandbcb_write_fw(struct boot_config *boot_cfg, u_char *buf,
879                             int index)
880 {
881         int i;
882         loff_t offset;
883         size_t size;
884         loff_t next_bs_addr;
885         struct boot_stream_config bs_cfg;
886         int ret;
887
888         for (i = 0; i < 2; ++i) {
889                 if (!(FW_INX(i) & index))
890                         continue;
891
892                 if (i == 0) {
893                         offset = boot_cfg->boot_stream1_address;
894                         size = boot_cfg->boot_stream1_size;
895                 } else {
896                         offset = boot_cfg->boot_stream2_address;
897                         size = boot_cfg->boot_stream2_size;
898                 }
899
900                 /* write Firmware*/
901                 if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
902                         memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
903                         sprintf(bs_cfg.bs_label, "firmware%d", i);
904                         bs_cfg.bs_addr = offset;
905                         bs_cfg.bs_size = size;
906                         bs_cfg.bs_buf = buf;
907                         bs_cfg.need_padding = 1;
908
909                         ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
910                         if (ret)
911                                 return ret;
912
913                         /* update the boot stream size */
914                         if (i == 0)
915                                 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
916                         else
917                                 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
918
919                 } else {
920                 /* some platforms need extra firmware */
921                         memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
922                         sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 1);
923                         bs_cfg.bs_addr = offset;
924                         bs_cfg.bs_size = IMX8MQ_HDMI_FW_SZ;
925                         bs_cfg.bs_buf = buf;
926                         bs_cfg.need_padding = 1;
927
928                         ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
929                         if (ret)
930                                 return ret;
931
932                         /* update the boot stream size */
933                         if (i == 0)
934                                 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
935                         else
936                                 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
937
938                         /* get next image address */
939                         next_bs_addr = bs_cfg.next_bs_addr;
940
941                         memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
942                         sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 2);
943                         bs_cfg.bs_addr = next_bs_addr;
944                         bs_cfg.bs_size = IMX8MQ_SPL_SZ;
945                         bs_cfg.bs_buf = (u_char *)(buf + IMX8MQ_HDMI_FW_SZ);
946                         bs_cfg.need_padding = 0;
947
948                         ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
949                         if (ret)
950                                 return ret;
951                 }
952         }
953
954         return 0;
955 }
956
957 static int nandbcb_init(struct boot_config *boot_cfg, u_char *buf)
958 {
959         struct mtd_info *mtd;
960         nand_erase_options_t opts;
961         struct fcb_block *fcb;
962         struct dbbt_block *dbbt;
963         void *dbbt_page, *dbbt_data_page;
964         int ret;
965         loff_t maxsize, off;
966
967         mtd = boot_cfg->mtd;
968         maxsize = boot_cfg->maxsize;
969         off = boot_cfg->offset;
970
971         /* erase */
972         memset(&opts, 0, sizeof(opts));
973         opts.offset = off;
974         opts.length = maxsize - 1;
975         ret = nand_erase_opts(mtd, &opts);
976         if (ret) {
977                 printf("%s: erase failed (ret = %d)\n", __func__, ret);
978                 return ret;
979         }
980
981         /*
982          * Reference documentation from i.MX6DQRM section 8.5.2.2
983          *
984          * Nand Boot Control Block(BCB) contains two data structures,
985          * - Firmware Configuration Block(FCB)
986          * - Discovered Bad Block Table(DBBT)
987          *
988          * FCB contains,
989          * - nand timings
990          * - DBBT search page address,
991          * - start page address of primary firmware
992          * - start page address of secondary firmware
993          *
994          * setup fcb:
995          * - number of blocks = mtd partition size / mtd erasesize
996          * - two firmware blocks, primary and secondary
997          * - first 4 block for FCB/DBBT
998          * - rest split in half for primary and secondary firmware
999          * - same firmware write twice
1000          */
1001
1002         /* write Firmware*/
1003         ret = nandbcb_write_fw(boot_cfg, buf, FW_ALL);
1004         if (ret)
1005                 goto err;
1006
1007         /* fill fcb */
1008         fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1009         if (!fcb) {
1010                 debug("failed to allocate fcb\n");
1011                 ret = -ENOMEM;
1012                 return ret;
1013         }
1014         fill_fcb(fcb, boot_cfg);
1015
1016         ret = write_fcb(boot_cfg, fcb);
1017
1018         /* fill dbbt */
1019         dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1020         if (!dbbt_page) {
1021                 debug("failed to allocate dbbt_page\n");
1022                 ret = -ENOMEM;
1023                 goto fcb_err;
1024         }
1025
1026         dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1027         if (!dbbt_data_page) {
1028                 debug("failed to allocate dbbt_data_page\n");
1029                 ret = -ENOMEM;
1030                 goto dbbt_page_err;
1031         }
1032
1033         dbbt = dbbt_page;
1034         dbbt->checksum = 0;
1035         dbbt->fingerprint = DBBT_FINGERPRINT;
1036         dbbt->version = DBBT_VERSION_1;
1037         ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
1038         if (ret < 0)
1039                 goto dbbt_data_page_err;
1040         else if (ret > 0)
1041                 dbbt->dbbtpages = 1;
1042
1043         /* write dbbt */
1044         ret = write_dbbt(boot_cfg, dbbt, dbbt_data_page);
1045         if (ret < 0)
1046                 printf("failed to write FCB/DBBT\n");
1047
1048 dbbt_data_page_err:
1049         kfree(dbbt_data_page);
1050 dbbt_page_err:
1051         kfree(dbbt_page);
1052 fcb_err:
1053         kfree(fcb);
1054 err:
1055         return ret;
1056 }
1057
1058 static int do_nandbcb_bcbonly(int argc, char *const argv[])
1059 {
1060         struct fcb_block *fcb;
1061         struct dbbt_block *dbbt;
1062         struct mtd_info *mtd;
1063         nand_erase_options_t opts;
1064         size_t maxsize;
1065         loff_t off;
1066         void *dbbt_page, *dbbt_data_page;
1067         int ret;
1068         struct boot_config cfg;
1069
1070         if (argc < 4)
1071                 return CMD_RET_USAGE;
1072
1073         memset(&cfg, 0, sizeof(struct boot_config));
1074         if (nandbcb_get_info(argc, argv, &cfg))
1075                 return CMD_RET_FAILURE;
1076
1077         /* only get the partition info */
1078         if (nandbcb_get_size(2, argv, 1, &cfg))
1079                 return CMD_RET_FAILURE;
1080
1081         if (nandbcb_set_boot_config(argc, argv, &cfg))
1082                 return CMD_RET_FAILURE;
1083
1084         mtd = cfg.mtd;
1085
1086         cfg.boot_stream1_address = simple_strtoul(argv[2], NULL, 16);
1087         cfg.boot_stream1_size = simple_strtoul(argv[3], NULL, 16);
1088         cfg.boot_stream1_size = ALIGN(cfg.boot_stream1_size, mtd->writesize);
1089
1090         if (argc > 5) {
1091                 cfg.boot_stream2_address = simple_strtoul(argv[4], NULL, 16);
1092                 cfg.boot_stream2_size = simple_strtoul(argv[5], NULL, 16);
1093                 cfg.boot_stream2_size = ALIGN(cfg.boot_stream2_size,
1094                                               mtd->writesize);
1095         }
1096
1097         /* sanity check */
1098         nandbcb_check_space(&cfg);
1099
1100         maxsize = cfg.maxsize;
1101         off = cfg.offset;
1102
1103         /* erase the previous FCB/DBBT */
1104         memset(&opts, 0, sizeof(opts));
1105         opts.offset = off;
1106         opts.length = g_boot_search_stride * 2;
1107         ret = nand_erase_opts(mtd, &opts);
1108         if (ret) {
1109                 printf("%s: erase failed (ret = %d)\n", __func__, ret);
1110                 return CMD_RET_FAILURE;
1111         }
1112
1113         /* fill fcb */
1114         fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1115         if (!fcb) {
1116                 printf("failed to allocate fcb\n");
1117                 ret = -ENOMEM;
1118                 return CMD_RET_FAILURE;
1119         }
1120
1121         fill_fcb(fcb, &cfg);
1122
1123         /* write fcb */
1124         ret = write_fcb(&cfg, fcb);
1125
1126         /* fill dbbt */
1127         dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1128         if (!dbbt_page) {
1129                 printf("failed to allocate dbbt_page\n");
1130                 ret = -ENOMEM;
1131                 goto fcb_err;
1132         }
1133
1134         dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1135         if (!dbbt_data_page) {
1136                 printf("failed to allocate dbbt_data_page\n");
1137                 ret = -ENOMEM;
1138                 goto dbbt_page_err;
1139         }
1140
1141         dbbt = dbbt_page;
1142         dbbt->checksum = 0;
1143         dbbt->fingerprint = DBBT_FINGERPRINT;
1144         dbbt->version = DBBT_VERSION_1;
1145         ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
1146         if (ret < 0)
1147                 goto dbbt_data_page_err;
1148         else if (ret > 0)
1149                 dbbt->dbbtpages = 1;
1150
1151         /* write dbbt */
1152         ret = write_dbbt(&cfg, dbbt, dbbt_data_page);
1153
1154 dbbt_data_page_err:
1155         kfree(dbbt_data_page);
1156 dbbt_page_err:
1157         kfree(dbbt_page);
1158 fcb_err:
1159         kfree(fcb);
1160
1161         if (ret < 0) {
1162                 printf("failed to write FCB/DBBT\n");
1163                 return CMD_RET_FAILURE;
1164         }
1165
1166         return CMD_RET_SUCCESS;
1167 }
1168
1169 /* dump data which is read from NAND chip */
1170 void dump_structure(struct boot_config *boot_cfg, struct fcb_block *fcb,
1171                     struct dbbt_block *dbbt, void *dbbt_data_page)
1172 {
1173         int i;
1174         struct mtd_info *mtd = boot_cfg->mtd;
1175
1176         #define P1(x) printf("  %s = 0x%08x\n", #x, fcb->x)
1177                 printf("FCB\n");
1178                 P1(checksum);
1179                 P1(fingerprint);
1180                 P1(version);
1181         #undef P1
1182         #define P1(x)   printf("  %s = %d\n", #x, fcb->x)
1183                 P1(datasetup);
1184                 P1(datahold);
1185                 P1(addr_setup);
1186                 P1(dsample_time);
1187                 P1(pagesize);
1188                 P1(oob_pagesize);
1189                 P1(sectors);
1190                 P1(nr_nand);
1191                 P1(nr_die);
1192                 P1(celltype);
1193                 P1(ecc_type);
1194                 P1(ecc_nr);
1195                 P1(ecc_size);
1196                 P1(ecc_level);
1197                 P1(meta_size);
1198                 P1(nr_blocks);
1199                 P1(ecc_type_sdk);
1200                 P1(ecc_nr_sdk);
1201                 P1(ecc_size_sdk);
1202                 P1(ecc_level_sdk);
1203                 P1(nr_blocks_sdk);
1204                 P1(meta_size_sdk);
1205                 P1(erase_th);
1206                 P1(bootpatch);
1207                 P1(patch_size);
1208                 P1(fw1_start);
1209                 P1(fw2_start);
1210                 P1(fw1_pages);
1211                 P1(fw2_pages);
1212                 P1(dbbt_start);
1213                 P1(bb_byte);
1214                 P1(bb_start_bit);
1215                 P1(phy_offset);
1216                 P1(bchtype);
1217                 P1(readlatency);
1218                 P1(predelay);
1219                 P1(cedelay);
1220                 P1(postdelay);
1221                 P1(cmdaddpause);
1222                 P1(datapause);
1223                 P1(tmspeed);
1224                 P1(busytimeout);
1225                 P1(disbbm);
1226                 P1(spare_offset);
1227 #if !defined(CONFIG_MX6) || defined(CONFIG_MX6SX) || \
1228         defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
1229                 P1(onfi_sync_enable);
1230                 P1(onfi_sync_speed);
1231                 P1(onfi_sync_nand_data);
1232                 P1(disbbm_search);
1233                 P1(disbbm_search_limit);
1234                 P1(read_retry_enable);
1235 #endif
1236         #undef P1
1237         #define P1(x)   printf("  %s = 0x%08x\n", #x, dbbt->x)
1238                 printf("DBBT :\n");
1239                 P1(checksum);
1240                 P1(fingerprint);
1241                 P1(version);
1242         #undef P1
1243         #define P1(x)   printf("  %s = %d\n", #x, dbbt->x)
1244                 P1(dbbtpages);
1245         #undef P1
1246
1247         for (i = 0; i < dbbt->dbbtpages; ++i)
1248                 printf("%d ", *((u32 *)(dbbt_data_page + i)));
1249
1250         if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
1251                 printf("Firmware: image #0 @ 0x%x size 0x%x\n",
1252                        fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
1253                 printf("Firmware: image #1 @ 0x%x size 0x%x\n",
1254                        fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
1255         } else {
1256                 printf("Firmware: image #0 @ 0x%x size 0x%x\n",
1257                        fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
1258                 printf("Firmware: image #1 @ 0x%x size 0x%x\n",
1259                        fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
1260                 /* TODO: Add extra image information */
1261         }
1262 }
1263
1264 static bool check_fingerprint(void *data, int fingerprint)
1265 {
1266         int off = 4;
1267
1268         return (*(int *)(data + off) == fingerprint);
1269 }
1270
1271 static int fuse_to_search_count(u32 bank, u32 word, u32 mask, u32 off)
1272 {
1273         int err;
1274         u32 val;
1275         int ret;
1276
1277         /* by default, the boot search count from fuse should be 2 */
1278         err = fuse_read(bank, word, &val);
1279         if (err)
1280                 return 2;
1281
1282         val = (val & mask) >> off;
1283
1284         switch (val) {
1285                 case 0:
1286                         ret = 2;
1287                         break;
1288                 case 1:
1289                 case 2:
1290                 case 3:
1291                         ret = 1 << val;
1292                         break;
1293                 default:
1294                         ret = 2;
1295         }
1296
1297         return ret;
1298 }
1299
1300 static int nandbcb_dump(struct boot_config *boot_cfg)
1301 {
1302         int i;
1303         loff_t off;
1304         struct mtd_info *mtd = boot_cfg->mtd;
1305         struct fcb_block fcb, fcb_copy;
1306         struct dbbt_block dbbt, dbbt_copy;
1307         void *dbbt_data_page, *dbbt_data_page_copy;
1308         bool fcb_not_found, dbbt_not_found;
1309         int ret = 0;
1310
1311         dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1312         if (!dbbt_data_page) {
1313                 printf("failed to allocate dbbt_data_page\n");
1314                 ret = -ENOMEM;
1315                 return ret;
1316         }
1317
1318         dbbt_data_page_copy = kzalloc(mtd->writesize, GFP_KERNEL);
1319         if (!dbbt_data_page_copy) {
1320                 printf("failed to allocate dbbt_data_page\n");
1321                 ret = -ENOMEM;
1322                 goto dbbt_page_err;
1323         }
1324
1325         /* read fcb */
1326         fcb_not_found = 1;
1327         off = 0;
1328         for (i = 0; i < g_boot_search_count; ++i) {
1329                 if (fcb_not_found) {
1330                         ret = read_fcb(boot_cfg, &fcb, off);
1331
1332                         if (ret < 0)
1333                                 goto dbbt_page_copy_err;
1334                         else if (ret == 1)
1335                                 continue;
1336                         else if (ret == 0)
1337                                 if (check_fingerprint(&fcb, FCB_FINGERPRINT))
1338                                         fcb_not_found = 0;
1339                 } else {
1340                         ret = read_fcb(boot_cfg, &fcb_copy, off);
1341
1342                         if (ret < 0)
1343                                 goto dbbt_page_copy_err;
1344                         if (memcmp(&fcb, &fcb_copy,
1345                                    sizeof(struct fcb_block))) {
1346                                 printf("FCB copies are not identical\n");
1347                                 ret = -EINVAL;
1348                                 goto dbbt_page_copy_err;
1349                         }
1350                 }
1351
1352                 /* next read location */
1353                 off += g_boot_search_stride;
1354         }
1355
1356         /* read dbbt*/
1357         dbbt_not_found = 1;
1358         off = boot_cfg->search_area_size_in_bytes;
1359         for (i = 0; i < g_boot_search_count; ++i) {
1360                 if (dbbt_not_found) {
1361                         ret = read_dbbt(boot_cfg, &dbbt, dbbt_data_page, off);
1362
1363                         if (ret < 0)
1364                                 goto dbbt_page_copy_err;
1365                         else if (ret == 1)
1366                                 continue;
1367                         else if (ret == 0)
1368                                 if (check_fingerprint(&dbbt, DBBT_FINGERPRINT))
1369                                         dbbt_not_found = 0;
1370                 } else {
1371                         ret = read_dbbt(boot_cfg, &dbbt_copy,
1372                                         dbbt_data_page_copy, off);
1373
1374                         if (ret < 0)
1375                                 goto dbbt_page_copy_err;
1376                         if (memcmp(&dbbt, &dbbt_copy,
1377                                    sizeof(struct dbbt_block))) {
1378                                 printf("DBBT copies are not identical\n");
1379                                 ret = -EINVAL;
1380                                 goto dbbt_page_copy_err;
1381                         }
1382                         if (dbbt.dbbtpages > 0 &&
1383                             memcmp(dbbt_data_page, dbbt_data_page_copy,
1384                                    mtd->writesize)) {
1385                                 printf("DBBT data copies are not identical\n");
1386                                 ret = -EINVAL;
1387                                 goto dbbt_page_copy_err;
1388                         }
1389                 }
1390
1391                 /* next read location */
1392                 off += g_boot_search_stride;
1393         }
1394
1395         dump_structure(boot_cfg, &fcb, &dbbt, dbbt_data_page);
1396
1397 dbbt_page_copy_err:
1398         kfree(dbbt_data_page_copy);
1399 dbbt_page_err:
1400         kfree(dbbt_data_page);
1401
1402         return ret;
1403 }
1404
1405 static int do_nandbcb_dump(int argc, char * const argv[])
1406 {
1407         struct boot_config cfg;
1408         int ret;
1409
1410         if (argc != 2)
1411                 return CMD_RET_USAGE;
1412
1413         memset(&cfg, 0, sizeof(struct boot_config));
1414         if (nandbcb_get_info(argc, argv, &cfg))
1415                 return CMD_RET_FAILURE;
1416
1417         if (nandbcb_get_size(argc, argv, 1, &cfg))
1418                 return CMD_RET_FAILURE;
1419
1420         if (nandbcb_set_boot_config(argc, argv, &cfg))
1421                 return CMD_RET_FAILURE;
1422
1423         ret = nandbcb_dump(&cfg);
1424         if (ret)
1425                 return ret;
1426
1427         return ret;
1428 }
1429
1430 static int do_nandbcb_init(int argc, char * const argv[])
1431 {
1432         u_char *buf;
1433         size_t size;
1434         loff_t addr;
1435         char *endp;
1436         int ret;
1437         struct boot_config cfg;
1438
1439         if (argc != 4)
1440                 return CMD_RET_USAGE;
1441
1442         memset(&cfg, 0, sizeof(struct boot_config));
1443         if (nandbcb_get_info(argc, argv, &cfg))
1444                 return CMD_RET_FAILURE;
1445
1446         if (nandbcb_get_size(argc, argv, 2, &cfg))
1447                 return CMD_RET_FAILURE;
1448         size = cfg.boot_stream1_size;
1449
1450         if (nandbcb_set_boot_config(argc, argv, &cfg))
1451                 return CMD_RET_FAILURE;
1452
1453         addr = simple_strtoul(argv[1], &endp, 16);
1454         if (*argv[1] == 0 || *endp != 0)
1455                 return CMD_RET_FAILURE;
1456
1457         buf = map_physmem(addr, size, MAP_WRBACK);
1458         if (!buf) {
1459                 puts("failed to map physical memory\n");
1460                 return CMD_RET_FAILURE;
1461         }
1462
1463         ret = nandbcb_init(&cfg, buf);
1464
1465         return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
1466 }
1467
1468 static int do_nandbcb(struct cmd_tbl *cmdtp, int flag, int argc,
1469                       char *const argv[])
1470 {
1471         const char *cmd;
1472         int ret = 0;
1473
1474         if (argc < 3)
1475                 goto usage;
1476
1477         /* check the platform config first */
1478         if (is_mx6sx()) {
1479                 plat_config = imx6sx_plat_config;
1480         } else if (is_mx7()) {
1481                 plat_config = imx7d_plat_config;
1482         } else if (is_mx6ul() || is_mx6ull()) {
1483                 plat_config = imx6ul_plat_config;
1484         } else if (is_mx6() && !is_mx6sx() && !is_mx6ul() && !is_mx6ull()) {
1485                 plat_config = imx6qdl_plat_config;
1486         } else if (is_imx8mq()) {
1487                 plat_config = imx8mq_plat_config;
1488         } else if (is_imx8mm()) {
1489                 plat_config = imx8mm_plat_config;
1490         } else if (is_imx8mn() || is_imx8mp()) {
1491                 plat_config = imx8mn_plat_config;
1492         } else if (is_imx8qm() || is_imx8qxp()) {
1493                 plat_config = imx8q_plat_config;
1494         } else {
1495                 printf("ERROR: Unknown platform\n");
1496                 return CMD_RET_FAILURE;
1497         }
1498
1499         if ((plat_config.misc_flags) & BT_SEARCH_CNT_FROM_FUSE) {
1500                 if (is_imx8qxp())
1501                         g_boot_search_count = fuse_to_search_count(0, 720, 0xc0, 6);
1502                 if (is_imx8mn() || is_imx8mp())
1503                         g_boot_search_count = fuse_to_search_count(2, 2, 0x6000, 13);
1504                 printf("search count set to %d from fuse\n",
1505                        g_boot_search_count);
1506         }
1507
1508         cmd = argv[1];
1509         --argc;
1510         ++argv;
1511
1512         if (strcmp(cmd, "init") == 0) {
1513                 ret = do_nandbcb_init(argc, argv);
1514                 goto done;
1515         }
1516
1517         if (strcmp(cmd, "dump") == 0) {
1518                 ret = do_nandbcb_dump(argc, argv);
1519                 goto done;
1520         }
1521
1522         if (strcmp(cmd, "bcbonly") == 0) {
1523                 ret = do_nandbcb_bcbonly(argc, argv);
1524                 goto done;
1525         }
1526
1527 done:
1528         if (ret != -1)
1529                 return ret;
1530 usage:
1531         return CMD_RET_USAGE;
1532 }
1533
1534 #ifdef CONFIG_SYS_LONGHELP
1535 static char nandbcb_help_text[] =
1536         "init addr off|partition len - update 'len' bytes starting at\n"
1537         "       'off|part' to memory address 'addr', skipping  bad blocks\n"
1538         "nandbcb bcbonly off|partition fw1-off fw1-size [fw2-off fw2-size]\n"
1539         "           - write BCB only (FCB and DBBT)\n"
1540         "       where `fwx-size` is fw sizes in bytes, `fw1-off`\n"
1541         "       and `fw2-off` - firmware offsets\n"
1542         "       FIY, BCB isn't erased automatically, so mtd erase should\n"
1543         "       be called in advance before writing new BCB:\n"
1544         "           > mtd erase mx7-bcb\n"
1545         "nandbcb dump off|partition - dump/verify boot structures\n";
1546 #endif
1547
1548 U_BOOT_CMD(nandbcb, 7, 1, do_nandbcb,
1549            "i.MX NAND Boot Control Blocks write",
1550            nandbcb_help_text
1551 );