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