blob: ea4e147da873a3e575e687989d73f423bc6f5e78 [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>
Simon Glasseb41d8a2020-05-10 11:40:08 -060020#include <linux/bug.h>
Shyam Saini1d43e242019-06-14 13:05:33 +053021
22#include <asm/io.h>
23#include <jffs2/jffs2.h>
Parthiban Nallathambi6aa87492019-10-18 11:46:19 +020024#include <linux/bch.h>
Shyam Saini1d43e242019-06-14 13:05:33 +053025#include <linux/mtd/mtd.h>
26
Igor Opaniukdad30dd2019-11-03 16:49:44 +010027#include <asm/arch/sys_proto.h>
Shyam Saini1d43e242019-06-14 13:05:33 +053028#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>
Han Xuf797fe82020-05-05 22:04:04 +080033#include <fuse.h>
Shyam Saini1d43e242019-06-14 13:05:33 +053034
Miquel Raynaleb446ef2019-10-25 19:39:29 +020035#include "../../../cmd/legacy-mtd-utils.h"
36
Han Xu214b7d52020-05-05 22:04:03 +080037/* 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
64struct platform_config {
65 int misc_flags;
66};
67
68static struct platform_config plat_config;
69
70/* imx6q/dl/solo */
71static struct platform_config imx6qdl_plat_config = {
72 .misc_flags = FCB_LAYOUT_RESV_12B |
73 FCB_ENCODE_HAMMING |
74 FIRMWARE_NEED_PADDING,
75};
76
77static 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
84static 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 */
92static struct platform_config imx6ul_plat_config = {
93 .misc_flags = FCB_LAYOUT_META_32B |
94 FCB_ENCODE_BCH_40b |
95 FIRMWARE_NEED_PADDING,
96};
97
98static 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 */
107static 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 */
115static 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 */
124static 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 */
133static int g_boot_search_count = 4;
134static int g_boot_search_stride;
135static int g_pages_per_stride;
136
137/* mtd config structure */
138struct 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 */
156struct 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
Shyam Saini1d43e242019-06-14 13:05:33 +0530175#define GETBIT(v, n) (((v) >> (n)) & 0x1)
Alice Guo66dbd9c2020-05-05 22:04:00 +0800176#define IMX8MQ_SPL_SZ 0x3e000
177#define IMX8MQ_HDMI_FW_SZ 0x19c00
Alice Guo0b103372020-05-05 22:04:01 +0800178
Han Xu214b7d52020-05-05 22:04:03 +0800179static 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
203static 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
229static 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 = boot_cfg->secondary_boot_stream_off_in_MB * 1024 * 1024;
279
280 max_boot_stream_size = boot_stream2_address - boot_stream1_address;
281
282 /* sanity check */
283 if (max_boot_stream_size <= 0) {
284 debug("st1_addr: %llx, st2_addr: %llx, max: %llx\n",
285 boot_stream1_address, boot_stream2_address,
286 max_boot_stream_size);
287 printf("something wrong with firmware address settings\n");
288 return CMD_RET_FAILURE;
289 }
290 boot_cfg->boot_stream1_address = boot_stream1_address;
291 boot_cfg->boot_stream2_address = boot_stream2_address;
292 boot_cfg->max_boot_stream_size = max_boot_stream_size;
293
294 /* set the boot_stream size as the input size now */
295 if (boot_cfg->input_size) {
296 boot_cfg->boot_stream1_size = boot_cfg->input_size;
297 boot_cfg->boot_stream2_size = boot_cfg->input_size;
298 }
299
300 return CMD_RET_SUCCESS;
301}
302
303static int nandbcb_check_space(struct boot_config *boot_cfg)
304{
305 size_t maxsize = boot_cfg->maxsize;
306 size_t max_boot_stream_size = boot_cfg->max_boot_stream_size;
307 loff_t boot_stream2_address = boot_cfg->boot_stream2_address;
308
309 if (boot_cfg->boot_stream1_size &&
310 boot_cfg->boot_stream1_size > max_boot_stream_size) {
311 printf("boot stream1 doesn't fit, check partition size or settings\n");
312 return CMD_RET_FAILURE;
313 }
314
315 if (boot_cfg->boot_stream2_size &&
316 boot_cfg->boot_stream2_size > maxsize - boot_stream2_address) {
317 printf("boot stream2 doesn't fit, check partition size or settings\n");
318 return CMD_RET_FAILURE;
319 }
320
321 return CMD_RET_SUCCESS;
322}
Shyam Saini1d43e242019-06-14 13:05:33 +0530323
Parthiban Nallathambi6aa87492019-10-18 11:46:19 +0200324#if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
325static uint8_t reverse_bit(uint8_t b)
326{
327 b = (b & 0xf0) >> 4 | (b & 0x0f) << 4;
328 b = (b & 0xcc) >> 2 | (b & 0x33) << 2;
329 b = (b & 0xaa) >> 1 | (b & 0x55) << 1;
330
331 return b;
332}
333
334static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits)
335{
336 int i, j, m = 13;
337 int blocksize = 128;
338 int numblocks = 8;
339 int ecc_buf_size = (m * eccbits + 7) / 8;
340 struct bch_control *bch = init_bch(m, eccbits, 0);
341 u8 *ecc_buf = kzalloc(ecc_buf_size, GFP_KERNEL);
342 u8 *tmp_buf = kzalloc(blocksize * numblocks, GFP_KERNEL);
343 u8 *psrc, *pdst;
344
345 /*
346 * The blocks here are bit aligned. If eccbits is a multiple of 8,
347 * we just can copy bytes. Otherwiese we must move the blocks to
348 * the next free bit position.
349 */
350 WARN_ON(eccbits % 8);
351
352 memcpy(tmp_buf, fcb, sizeof(*fcb));
353
354 for (i = 0; i < numblocks; i++) {
355 memset(ecc_buf, 0, ecc_buf_size);
356 psrc = tmp_buf + i * blocksize;
357 pdst = buf + i * (blocksize + ecc_buf_size);
358
359 /* copy data byte aligned to destination buf */
360 memcpy(pdst, psrc, blocksize);
361
362 /*
363 * imx-kobs use a modified encode_bch which reverse the
364 * bit order of the data before calculating bch.
365 * Do this in the buffer and use the bch lib here.
366 */
367 for (j = 0; j < blocksize; j++)
368 psrc[j] = reverse_bit(psrc[j]);
369
370 encode_bch(bch, psrc, blocksize, ecc_buf);
371
372 /* reverse ecc bit */
373 for (j = 0; j < ecc_buf_size; j++)
374 ecc_buf[j] = reverse_bit(ecc_buf[j]);
375
376 /* Here eccbuf is byte aligned and we can just copy it */
377 memcpy(pdst + blocksize, ecc_buf, ecc_buf_size);
378 }
379
380 kfree(ecc_buf);
381 kfree(tmp_buf);
382 free_bch(bch);
383}
384#else
385
Shyam Saini1d43e242019-06-14 13:05:33 +0530386static u8 calculate_parity_13_8(u8 d)
387{
388 u8 p = 0;
389
390 p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0;
391 p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^
392 GETBIT(d, 1)) << 1;
393 p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^
394 GETBIT(d, 0)) << 2;
395 p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3;
396 p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^
397 GETBIT(d, 1) ^ GETBIT(d, 0)) << 4;
398
399 return p;
400}
401
402static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
403{
404 int i;
405 u8 *src = _src;
406 u8 *ecc = _ecc;
407
408 for (i = 0; i < size; i++)
409 ecc[i] = calculate_parity_13_8(src[i]);
410}
Parthiban Nallathambi6aa87492019-10-18 11:46:19 +0200411#endif
Shyam Saini1d43e242019-06-14 13:05:33 +0530412
413static u32 calc_chksum(void *buf, size_t size)
414{
415 u32 chksum = 0;
416 u8 *bp = buf;
417 size_t i;
418
419 for (i = 0; i < size; i++)
420 chksum += bp[i];
421
422 return ~chksum;
423}
424
Han Xu214b7d52020-05-05 22:04:03 +0800425static void fill_fcb(struct fcb_block *fcb, struct boot_config *boot_cfg)
Shyam Saini1d43e242019-06-14 13:05:33 +0530426{
Han Xu214b7d52020-05-05 22:04:03 +0800427 struct mtd_info *mtd = boot_cfg->mtd;
Shyam Saini1d43e242019-06-14 13:05:33 +0530428 struct nand_chip *chip = mtd_to_nand(mtd);
429 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100430 struct mxs_nand_layout l;
431
432 mxs_nand_get_layout(mtd, &l);
Shyam Saini1d43e242019-06-14 13:05:33 +0530433
434 fcb->fingerprint = FCB_FINGERPRINT;
435 fcb->version = FCB_VERSION_1;
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100436
Han Xu214b7d52020-05-05 22:04:03 +0800437 fcb->datasetup = 80;
438 fcb->datahold = 60;
439 fcb->addr_setup = 25;
440 fcb->dsample_time = 6;
441
Shyam Saini1d43e242019-06-14 13:05:33 +0530442 fcb->pagesize = mtd->writesize;
443 fcb->oob_pagesize = mtd->writesize + mtd->oobsize;
444 fcb->sectors = mtd->erasesize / mtd->writesize;
445
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100446 fcb->meta_size = l.meta_size;
447 fcb->nr_blocks = l.nblocks;
448 fcb->ecc_nr = l.data0_size;
449 fcb->ecc_level = l.ecc0;
450 fcb->ecc_size = l.datan_size;
451 fcb->ecc_type = l.eccn;
Han Xuc6ed3502020-05-05 22:03:59 +0800452 fcb->bchtype = l.gf_len;
Shyam Saini1d43e242019-06-14 13:05:33 +0530453
Han Xu214b7d52020-05-05 22:04:03 +0800454 /* DBBT search area starts from the next block after all FCB */
455 fcb->dbbt_start = boot_cfg->search_area_size_in_pages;
Shyam Saini1d43e242019-06-14 13:05:33 +0530456
457 fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset;
458 fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset;
459
460 fcb->phy_offset = mtd->writesize;
461
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100462 fcb->disbbm = 0;
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100463
Han Xu214b7d52020-05-05 22:04:03 +0800464 fcb->fw1_start = CONV_TO_PAGES(boot_cfg->boot_stream1_address);
465 fcb->fw2_start = CONV_TO_PAGES(boot_cfg->boot_stream2_address);
466 fcb->fw1_pages = CONV_TO_PAGES(boot_cfg->boot_stream1_size);
467 fcb->fw2_pages = CONV_TO_PAGES(boot_cfg->boot_stream2_size);
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100468
Shyam Saini1d43e242019-06-14 13:05:33 +0530469 fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
470}
471
Han Xu214b7d52020-05-05 22:04:03 +0800472static int fill_dbbt_data(struct mtd_info *mtd, void *buf, int num_blocks)
Shyam Saini1d43e242019-06-14 13:05:33 +0530473{
474 int n, n_bad_blocks = 0;
475 u32 *bb = buf + 0x8;
476 u32 *n_bad_blocksp = buf + 0x4;
477
478 for (n = 0; n < num_blocks; n++) {
Ye Li983f5e02020-08-02 22:59:43 -0700479 loff_t offset = (loff_t)n * mtd->erasesize;
Shyam Saini1d43e242019-06-14 13:05:33 +0530480 if (mtd_block_isbad(mtd, offset)) {
481 n_bad_blocks++;
482 *bb = n;
483 bb++;
484 }
485 }
486
487 *n_bad_blocksp = n_bad_blocks;
488
489 return n_bad_blocks;
490}
491
Han Xu214b7d52020-05-05 22:04:03 +0800492/*
493 * return 1 - bad block
494 * return 0 - read successfully
495 * return < 0 - read failed
496 */
497static int read_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb,
498 loff_t off)
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100499{
Han Xu214b7d52020-05-05 22:04:03 +0800500 struct mtd_info *mtd;
501 void *fcb_raw_page;
502 size_t size;
503 int ret = 0;
504
505 mtd = boot_cfg->mtd;
Han Xu214b7d52020-05-05 22:04:03 +0800506 if (mtd_block_isbad(mtd, off)) {
507 printf("Block %d is bad, skipped\n", (int)CONV_TO_BLOCKS(off));
508 return 1;
509 }
510
Ye Lif637c402020-08-02 22:43:45 -0700511 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
512 if (!fcb_raw_page) {
513 debug("failed to allocate fcb_raw_page\n");
514 ret = -ENOMEM;
515 return ret;
516 }
517
Han Xu214b7d52020-05-05 22:04:03 +0800518 /*
519 * User BCH hardware to decode ECC for FCB
520 */
521 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
522 size = sizeof(struct fcb_block);
523
524 /* switch nand BCH to FCB compatible settings */
525 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
526 mxs_nand_mode_fcb_62bit(mtd);
527 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
528 mxs_nand_mode_fcb_40bit(mtd);
529
530 ret = nand_read(mtd, off, &size, (u_char *)fcb);
531
532 /* switch BCH back */
533 mxs_nand_mode_normal(mtd);
534 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
535 off, size, ret ? "ERROR" : "OK");
536
537 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
538 /* raw read*/
539 mtd_oob_ops_t ops = {
540 .datbuf = (u8 *)fcb_raw_page,
541 .oobbuf = ((u8 *)fcb_raw_page) + mtd->writesize,
542 .len = mtd->writesize,
543 .ooblen = mtd->oobsize,
544 .mode = MTD_OPS_RAW
545 };
546
547 ret = mtd_read_oob(mtd, off, &ops);
548 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
549 off, ops.len, ret ? "ERROR" : "OK");
550 }
551
552 if (ret)
553 goto fcb_raw_page_err;
554
555 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
556 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B))
557 memcpy(fcb, fcb_raw_page + 12, sizeof(struct fcb_block));
558
559/* TODO: check if it can pass Hamming check */
560
561fcb_raw_page_err:
562 kfree(fcb_raw_page);
563
564 return ret;
565}
566
567static int write_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb)
568{
569 struct mtd_info *mtd;
570 void *fcb_raw_page = NULL;
Ye Li9dd599a2020-08-02 20:55:17 -0700571 int i, ret = 0;
Han Xu214b7d52020-05-05 22:04:03 +0800572 loff_t off;
573 size_t size;
574
575 mtd = boot_cfg->mtd;
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100576
577 /*
578 * We prepare raw page only for i.MX6, for i.MX7 we
579 * leverage BCH hw module instead
580 */
Han Xu214b7d52020-05-05 22:04:03 +0800581 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
582 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B)) {
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100583 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize,
584 GFP_KERNEL);
585 if (!fcb_raw_page) {
586 debug("failed to allocate fcb_raw_page\n");
587 ret = -ENOMEM;
588 return ret;
589 }
590
591#if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
592 /* 40 bit BCH, for i.MX6UL(L) */
593 encode_bch_ecc(fcb_raw_page + 32, fcb, 40);
594#else
595 memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block));
596 encode_hamming_13_8(fcb_raw_page + 12,
597 fcb_raw_page + 12 + 512, 512);
598#endif
599 /*
600 * Set the first and second byte of OOB data to 0xFF,
601 * not 0x00. These bytes are used as the Manufacturers Bad
602 * Block Marker (MBBM). Since the FCB is mostly written to
603 * the first page in a block, a scan for
604 * factory bad blocks will detect these blocks as bad, e.g.
605 * when function nand_scan_bbt() is executed to build a new
606 * bad block table.
607 */
608 memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
609 }
Han Xu214b7d52020-05-05 22:04:03 +0800610
611 /* start writing FCB from the very beginning */
612 off = 0;
613
614 for (i = 0; i < g_boot_search_count; i++) {
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100615 if (mtd_block_isbad(mtd, off)) {
616 printf("Block %d is bad, skipped\n", i);
617 continue;
618 }
619
620 /*
Han Xu214b7d52020-05-05 22:04:03 +0800621 * User BCH hardware module to generate ECC for FCB
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100622 */
Han Xu214b7d52020-05-05 22:04:03 +0800623 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
624 size = sizeof(struct fcb_block);
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100625
626 /* switch nand BCH to FCB compatible settings */
Han Xu214b7d52020-05-05 22:04:03 +0800627 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
628 mxs_nand_mode_fcb_62bit(mtd);
629 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
630 mxs_nand_mode_fcb_40bit(mtd);
Alice Guo0b103372020-05-05 22:04:01 +0800631
Han Xu214b7d52020-05-05 22:04:03 +0800632 ret = nand_write(mtd, off, &size, (u_char *)fcb);
Alice Guo0b103372020-05-05 22:04:01 +0800633
Han Xu214b7d52020-05-05 22:04:03 +0800634 /* switch BCH back */
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100635 mxs_nand_mode_normal(mtd);
Han Xu214b7d52020-05-05 22:04:03 +0800636 printf("NAND FCB write to 0x%zx offset 0x%llx written: %s\n",
637 size, off, ret ? "ERROR" : "OK");
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100638
Han Xu214b7d52020-05-05 22:04:03 +0800639 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100640 /* raw write */
641 mtd_oob_ops_t ops = {
642 .datbuf = (u8 *)fcb_raw_page,
643 .oobbuf = ((u8 *)fcb_raw_page) +
644 mtd->writesize,
645 .len = mtd->writesize,
646 .ooblen = mtd->oobsize,
647 .mode = MTD_OPS_RAW
648 };
649
Han Xu214b7d52020-05-05 22:04:03 +0800650 ret = mtd_write_oob(mtd, off, &ops);
651 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 +0100652 }
653
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100654 if (ret)
655 goto fcb_raw_page_err;
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100656
Han Xu214b7d52020-05-05 22:04:03 +0800657 /* next writing location */
658 off += g_boot_search_stride;
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100659 }
660
661fcb_raw_page_err:
Han Xu214b7d52020-05-05 22:04:03 +0800662 kfree(fcb_raw_page);
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100663
664 return ret;
665}
666
Han Xu214b7d52020-05-05 22:04:03 +0800667/*
668 * return 1 - bad block
669 * return 0 - read successfully
670 * return < 0 - read failed
671 */
672static int read_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
673 void *dbbt_data_page, loff_t off)
Shyam Saini1d43e242019-06-14 13:05:33 +0530674{
Han Xu214b7d52020-05-05 22:04:03 +0800675 size_t size;
676 struct mtd_info *mtd;
677 loff_t to;
678 int ret;
679
680 mtd = boot_cfg->mtd;
681
682 if (mtd_block_isbad(mtd, off)) {
683 printf("Block %d is bad, skipped\n",
684 (int)CONV_TO_BLOCKS(off));
685 return 1;
686 }
687
688 size = sizeof(struct dbbt_block);
689 ret = nand_read(mtd, off, &size, (u_char *)dbbt);
690 printf("NAND DBBT read from 0x%llx offset 0x%zx read: %s\n",
691 off, size, ret ? "ERROR" : "OK");
692 if (ret)
693 return ret;
694
695 /* dbbtpages == 0 if no bad blocks */
696 if (dbbt->dbbtpages > 0) {
697 to = off + 4 * mtd->writesize;
698 size = mtd->writesize;
699 ret = nand_read(mtd, to, &size, dbbt_data_page);
700 printf("DBBT data read from 0x%llx offset 0x%zx read: %s\n",
701 to, size, ret ? "ERROR" : "OK");
702
703 if (ret)
704 return ret;
705 }
706
707 return 0;
708}
709
710static int write_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
711 void *dbbt_data_page)
712{
713 int i;
714 loff_t off, to;
715 size_t size;
716 struct mtd_info *mtd;
717 int ret;
718
719 mtd = boot_cfg->mtd;
720
721 /* start writing DBBT after all FCBs */
722 off = boot_cfg->search_area_size_in_bytes;
723 size = mtd->writesize;
724
725 for (i = 0; i < g_boot_search_count; i++) {
726 if (mtd_block_isbad(mtd, off)) {
727 printf("Block %d is bad, skipped\n",
728 (int)(i + CONV_TO_BLOCKS(off)));
729 continue;
730 }
731
732 ret = nand_write(mtd, off, &size, (u_char *)dbbt);
733 printf("NAND DBBT write to 0x%llx offset 0x%zx written: %s\n",
734 off, size, ret ? "ERROR" : "OK");
735 if (ret)
736 return ret;
737
738 /* dbbtpages == 0 if no bad blocks */
739 if (dbbt->dbbtpages > 0) {
740 to = off + 4 * mtd->writesize;
741 ret = nand_write(mtd, to, &size, dbbt_data_page);
742 printf("DBBT data write to 0x%llx offset 0x%zx written: %s\n",
743 to, size, ret ? "ERROR" : "OK");
744
745 if (ret)
746 return ret;
747 }
748
749 /* next writing location */
750 off += g_boot_search_stride;
751 }
752
753 return 0;
754}
755
756/* reuse the check_skip_len from nand_util.c with minor change*/
757static int check_skip_length(struct boot_config *boot_cfg, loff_t offset,
758 size_t length, size_t *used)
759{
760 struct mtd_info *mtd = boot_cfg->mtd;
761 size_t maxsize = boot_cfg->maxsize;
762 size_t len_excl_bad = 0;
763 int ret = 0;
764
765 while (len_excl_bad < length) {
766 size_t block_len, block_off;
767 loff_t block_start;
768
769 if (offset >= maxsize)
770 return -1;
771
772 block_start = offset & ~(loff_t)(mtd->erasesize - 1);
773 block_off = offset & (mtd->erasesize - 1);
774 block_len = mtd->erasesize - block_off;
775
776 if (!nand_block_isbad(mtd, block_start))
777 len_excl_bad += block_len;
778 else
779 ret = 1;
780
781 offset += block_len;
782 *used += block_len;
783 }
784
785 /* If the length is not a multiple of block_len, adjust. */
786 if (len_excl_bad > length)
787 *used -= (len_excl_bad - length);
788
789 return ret;
790}
791
792static int nandbcb_get_next_good_blk_addr(struct boot_config *boot_cfg,
793 struct boot_stream_config *bs_cfg)
794{
795 struct mtd_info *mtd = boot_cfg->mtd;
796 loff_t offset = bs_cfg->bs_addr;
797 size_t length = bs_cfg->bs_size;
798 size_t used = 0;
799 int ret;
800
801 ret = check_skip_length(boot_cfg, offset, length, &used);
802
803 if (ret < 0)
804 return ret;
805
806 /* get next image address */
807 bs_cfg->next_bs_addr = (u32)(offset + used + mtd->erasesize - 1)
808 / (u32)mtd->erasesize * mtd->erasesize;
809
810 return ret;
811}
812
813static int nandbcb_write_bs_skip_bad(struct boot_config *boot_cfg,
814 struct boot_stream_config *bs_cfg)
815{
816 struct mtd_info *mtd;
817 void *buf;
818 loff_t offset, maxsize;
819 size_t size;
820 size_t length;
821 int ret;
822 bool padding_flag = false;
823
824 mtd = boot_cfg->mtd;
825 offset = bs_cfg->bs_addr;
826 maxsize = boot_cfg->maxsize;
827 size = bs_cfg->bs_size;
828
829 /* some boot images may need leading offset */
830 if (bs_cfg->need_padding &&
831 (plat_config.misc_flags & FIRMWARE_NEED_PADDING))
832 padding_flag = 1;
833
834 if (padding_flag)
835 length = ALIGN(size + FLASH_OFFSET_STANDARD, mtd->writesize);
836 else
837 length = ALIGN(size, mtd->writesize);
838
839 buf = kzalloc(length, GFP_KERNEL);
840 if (!buf) {
841 printf("failed to allocate buffer for firmware\n");
842 ret = -ENOMEM;
843 return ret;
844 }
845
846 if (padding_flag)
847 memcpy(buf + FLASH_OFFSET_STANDARD, bs_cfg->bs_buf, size);
848 else
849 memcpy(buf, bs_cfg->bs_buf, size);
850
851 ret = nand_write_skip_bad(mtd, offset, &length, NULL, maxsize,
852 (u_char *)buf, WITH_WR_VERIFY);
853 printf("Write %s @0x%llx offset, 0x%zx bytes written: %s\n",
854 bs_cfg->bs_label, offset, length, ret ? "ERROR" : "OK");
855
856 if (ret)
857 /* write image failed, quit */
858 goto err;
859
860 /* get next good blk address if needed */
861 if (bs_cfg->need_padding) {
862 ret = nandbcb_get_next_good_blk_addr(boot_cfg, bs_cfg);
863 if (ret < 0) {
864 printf("Next image cannot fit in NAND partition\n");
865 goto err;
866 }
867 }
868
869 /* now we know how the exact image size written to NAND */
870 bs_cfg->bs_size = length;
871 return 0;
872err:
873 kfree(buf);
874 return ret;
875}
876
877static int nandbcb_write_fw(struct boot_config *boot_cfg, u_char *buf,
878 int index)
879{
880 int i;
881 loff_t offset;
882 size_t size;
883 loff_t next_bs_addr;
884 struct boot_stream_config bs_cfg;
885 int ret;
886
887 for (i = 0; i < 2; ++i) {
888 if (!(FW_INX(i) & index))
889 continue;
890
891 if (i == 0) {
892 offset = boot_cfg->boot_stream1_address;
893 size = boot_cfg->boot_stream1_size;
894 } else {
895 offset = boot_cfg->boot_stream2_address;
896 size = boot_cfg->boot_stream2_size;
897 }
898
899 /* write Firmware*/
900 if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
901 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
902 sprintf(bs_cfg.bs_label, "firmware%d", i);
903 bs_cfg.bs_addr = offset;
904 bs_cfg.bs_size = size;
905 bs_cfg.bs_buf = buf;
906 bs_cfg.need_padding = 1;
907
908 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
909 if (ret)
910 return ret;
911
912 /* update the boot stream size */
913 if (i == 0)
914 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
915 else
916 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
917
918 } else {
919 /* some platforms need extra firmware */
920 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
921 sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 1);
922 bs_cfg.bs_addr = offset;
923 bs_cfg.bs_size = IMX8MQ_HDMI_FW_SZ;
924 bs_cfg.bs_buf = buf;
925 bs_cfg.need_padding = 1;
926
927 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
928 if (ret)
929 return ret;
930
931 /* update the boot stream size */
932 if (i == 0)
933 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
934 else
935 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
936
937 /* get next image address */
938 next_bs_addr = bs_cfg.next_bs_addr;
939
940 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
941 sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 2);
942 bs_cfg.bs_addr = next_bs_addr;
943 bs_cfg.bs_size = IMX8MQ_SPL_SZ;
944 bs_cfg.bs_buf = (u_char *)(buf + IMX8MQ_HDMI_FW_SZ);
945 bs_cfg.need_padding = 0;
946
947 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
948 if (ret)
949 return ret;
950 }
951 }
952
953 return 0;
954}
955
956static int nandbcb_init(struct boot_config *boot_cfg, u_char *buf)
957{
958 struct mtd_info *mtd;
Shyam Saini1d43e242019-06-14 13:05:33 +0530959 nand_erase_options_t opts;
960 struct fcb_block *fcb;
961 struct dbbt_block *dbbt;
Han Xu214b7d52020-05-05 22:04:03 +0800962 void *dbbt_page, *dbbt_data_page;
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100963 int ret;
Han Xu214b7d52020-05-05 22:04:03 +0800964 loff_t maxsize, off;
965
966 mtd = boot_cfg->mtd;
967 maxsize = boot_cfg->maxsize;
968 off = boot_cfg->offset;
Shyam Saini1d43e242019-06-14 13:05:33 +0530969
970 /* erase */
971 memset(&opts, 0, sizeof(opts));
972 opts.offset = off;
973 opts.length = maxsize - 1;
974 ret = nand_erase_opts(mtd, &opts);
975 if (ret) {
976 printf("%s: erase failed (ret = %d)\n", __func__, ret);
977 return ret;
978 }
979
980 /*
981 * Reference documentation from i.MX6DQRM section 8.5.2.2
982 *
983 * Nand Boot Control Block(BCB) contains two data structures,
984 * - Firmware Configuration Block(FCB)
985 * - Discovered Bad Block Table(DBBT)
986 *
987 * FCB contains,
988 * - nand timings
989 * - DBBT search page address,
990 * - start page address of primary firmware
991 * - start page address of secondary firmware
992 *
993 * setup fcb:
994 * - number of blocks = mtd partition size / mtd erasesize
995 * - two firmware blocks, primary and secondary
996 * - first 4 block for FCB/DBBT
997 * - rest split in half for primary and secondary firmware
Han Xu214b7d52020-05-05 22:04:03 +0800998 * - same firmware write twice
Shyam Saini1d43e242019-06-14 13:05:33 +0530999 */
Shyam Saini1d43e242019-06-14 13:05:33 +05301000
Han Xu214b7d52020-05-05 22:04:03 +08001001 /* write Firmware*/
1002 ret = nandbcb_write_fw(boot_cfg, buf, FW_ALL);
1003 if (ret)
1004 goto err;
Shyam Saini1d43e242019-06-14 13:05:33 +05301005
1006 /* fill fcb */
1007 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1008 if (!fcb) {
1009 debug("failed to allocate fcb\n");
1010 ret = -ENOMEM;
Han Xu214b7d52020-05-05 22:04:03 +08001011 return ret;
Shyam Saini1d43e242019-06-14 13:05:33 +05301012 }
Han Xu214b7d52020-05-05 22:04:03 +08001013 fill_fcb(fcb, boot_cfg);
Shyam Saini1d43e242019-06-14 13:05:33 +05301014
Han Xu214b7d52020-05-05 22:04:03 +08001015 ret = write_fcb(boot_cfg, fcb);
Alice Guo0b103372020-05-05 22:04:01 +08001016
Shyam Saini1d43e242019-06-14 13:05:33 +05301017 /* fill dbbt */
1018 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1019 if (!dbbt_page) {
1020 debug("failed to allocate dbbt_page\n");
1021 ret = -ENOMEM;
1022 goto fcb_err;
1023 }
1024
1025 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1026 if (!dbbt_data_page) {
1027 debug("failed to allocate dbbt_data_page\n");
1028 ret = -ENOMEM;
1029 goto dbbt_page_err;
1030 }
1031
1032 dbbt = dbbt_page;
1033 dbbt->checksum = 0;
Han Xu214b7d52020-05-05 22:04:03 +08001034 dbbt->fingerprint = DBBT_FINGERPRINT;
Shyam Saini1d43e242019-06-14 13:05:33 +05301035 dbbt->version = DBBT_VERSION_1;
Han Xu214b7d52020-05-05 22:04:03 +08001036 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
Shyam Saini1d43e242019-06-14 13:05:33 +05301037 if (ret < 0)
1038 goto dbbt_data_page_err;
1039 else if (ret > 0)
1040 dbbt->dbbtpages = 1;
1041
Han Xu214b7d52020-05-05 22:04:03 +08001042 /* write dbbt */
1043 ret = write_dbbt(boot_cfg, dbbt, dbbt_data_page);
Igor Opaniuk1b899a82019-11-03 16:49:45 +01001044 if (ret < 0)
1045 printf("failed to write FCB/DBBT\n");
Shyam Saini1d43e242019-06-14 13:05:33 +05301046
Shyam Saini1d43e242019-06-14 13:05:33 +05301047dbbt_data_page_err:
1048 kfree(dbbt_data_page);
1049dbbt_page_err:
1050 kfree(dbbt_page);
1051fcb_err:
1052 kfree(fcb);
Shyam Saini1d43e242019-06-14 13:05:33 +05301053err:
1054 return ret;
1055}
1056
Simon Glass09140112020-05-10 11:40:03 -06001057static int do_nandbcb_bcbonly(int argc, char *const argv[])
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001058{
1059 struct fcb_block *fcb;
1060 struct dbbt_block *dbbt;
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001061 struct mtd_info *mtd;
Han Xu214b7d52020-05-05 22:04:03 +08001062 nand_erase_options_t opts;
1063 size_t maxsize;
1064 loff_t off;
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001065 void *dbbt_page, *dbbt_data_page;
Han Xu214b7d52020-05-05 22:04:03 +08001066 int ret;
1067 struct boot_config cfg;
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001068
Han Xu214b7d52020-05-05 22:04:03 +08001069 if (argc < 4)
1070 return CMD_RET_USAGE;
1071
1072 memset(&cfg, 0, sizeof(struct boot_config));
1073 if (nandbcb_get_info(argc, argv, &cfg))
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001074 return CMD_RET_FAILURE;
Han Xu214b7d52020-05-05 22:04:03 +08001075
1076 /* only get the partition info */
1077 if (nandbcb_get_size(2, argv, 1, &cfg))
1078 return CMD_RET_FAILURE;
1079
1080 if (nandbcb_set_boot_config(argc, argv, &cfg))
1081 return CMD_RET_FAILURE;
1082
1083 mtd = cfg.mtd;
1084
1085 cfg.boot_stream1_address = simple_strtoul(argv[2], NULL, 16);
1086 cfg.boot_stream1_size = simple_strtoul(argv[3], NULL, 16);
1087 cfg.boot_stream1_size = ALIGN(cfg.boot_stream1_size, mtd->writesize);
1088
1089 if (argc > 5) {
1090 cfg.boot_stream2_address = simple_strtoul(argv[4], NULL, 16);
1091 cfg.boot_stream2_size = simple_strtoul(argv[5], NULL, 16);
1092 cfg.boot_stream2_size = ALIGN(cfg.boot_stream2_size,
1093 mtd->writesize);
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001094 }
1095
Han Xu214b7d52020-05-05 22:04:03 +08001096 /* sanity check */
1097 nandbcb_check_space(&cfg);
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001098
Han Xu214b7d52020-05-05 22:04:03 +08001099 maxsize = cfg.maxsize;
1100 off = cfg.offset;
1101
1102 /* erase the previous FCB/DBBT */
1103 memset(&opts, 0, sizeof(opts));
1104 opts.offset = off;
1105 opts.length = g_boot_search_stride * 2;
1106 ret = nand_erase_opts(mtd, &opts);
1107 if (ret) {
1108 printf("%s: erase failed (ret = %d)\n", __func__, ret);
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001109 return CMD_RET_FAILURE;
Han Xu214b7d52020-05-05 22:04:03 +08001110 }
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001111
1112 /* fill fcb */
1113 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1114 if (!fcb) {
Han Xu214b7d52020-05-05 22:04:03 +08001115 printf("failed to allocate fcb\n");
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001116 ret = -ENOMEM;
1117 return CMD_RET_FAILURE;
1118 }
1119
Han Xu214b7d52020-05-05 22:04:03 +08001120 fill_fcb(fcb, &cfg);
1121
1122 /* write fcb */
1123 ret = write_fcb(&cfg, fcb);
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001124
1125 /* fill dbbt */
1126 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1127 if (!dbbt_page) {
Han Xu214b7d52020-05-05 22:04:03 +08001128 printf("failed to allocate dbbt_page\n");
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001129 ret = -ENOMEM;
1130 goto fcb_err;
1131 }
1132
1133 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1134 if (!dbbt_data_page) {
Han Xu214b7d52020-05-05 22:04:03 +08001135 printf("failed to allocate dbbt_data_page\n");
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001136 ret = -ENOMEM;
1137 goto dbbt_page_err;
1138 }
1139
1140 dbbt = dbbt_page;
1141 dbbt->checksum = 0;
Han Xu214b7d52020-05-05 22:04:03 +08001142 dbbt->fingerprint = DBBT_FINGERPRINT;
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001143 dbbt->version = DBBT_VERSION_1;
Han Xu214b7d52020-05-05 22:04:03 +08001144 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001145 if (ret < 0)
1146 goto dbbt_data_page_err;
1147 else if (ret > 0)
1148 dbbt->dbbtpages = 1;
1149
Han Xu214b7d52020-05-05 22:04:03 +08001150 /* write dbbt */
1151 ret = write_dbbt(&cfg, dbbt, dbbt_data_page);
1152
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001153dbbt_data_page_err:
1154 kfree(dbbt_data_page);
1155dbbt_page_err:
1156 kfree(dbbt_page);
1157fcb_err:
1158 kfree(fcb);
1159
1160 if (ret < 0) {
1161 printf("failed to write FCB/DBBT\n");
1162 return CMD_RET_FAILURE;
1163 }
1164
1165 return CMD_RET_SUCCESS;
1166}
1167
Alice Guo0b103372020-05-05 22:04:01 +08001168/* dump data which is read from NAND chip */
Han Xu214b7d52020-05-05 22:04:03 +08001169void dump_structure(struct boot_config *boot_cfg, struct fcb_block *fcb,
1170 struct dbbt_block *dbbt, void *dbbt_data_page)
Alice Guo0b103372020-05-05 22:04:01 +08001171{
Han Xu214b7d52020-05-05 22:04:03 +08001172 int i;
1173 struct mtd_info *mtd = boot_cfg->mtd;
1174
1175 #define P1(x) printf(" %s = 0x%08x\n", #x, fcb->x)
1176 printf("FCB\n");
Alice Guo0b103372020-05-05 22:04:01 +08001177 P1(checksum);
1178 P1(fingerprint);
1179 P1(version);
1180 #undef P1
Han Xu214b7d52020-05-05 22:04:03 +08001181 #define P1(x) printf(" %s = %d\n", #x, fcb->x)
Alice Guo0b103372020-05-05 22:04:01 +08001182 P1(datasetup);
1183 P1(datahold);
1184 P1(addr_setup);
1185 P1(dsample_time);
1186 P1(pagesize);
1187 P1(oob_pagesize);
1188 P1(sectors);
1189 P1(nr_nand);
1190 P1(nr_die);
1191 P1(celltype);
1192 P1(ecc_type);
1193 P1(ecc_nr);
1194 P1(ecc_size);
1195 P1(ecc_level);
1196 P1(meta_size);
1197 P1(nr_blocks);
1198 P1(ecc_type_sdk);
1199 P1(ecc_nr_sdk);
1200 P1(ecc_size_sdk);
1201 P1(ecc_level_sdk);
1202 P1(nr_blocks_sdk);
1203 P1(meta_size_sdk);
1204 P1(erase_th);
1205 P1(bootpatch);
1206 P1(patch_size);
1207 P1(fw1_start);
1208 P1(fw2_start);
1209 P1(fw1_pages);
1210 P1(fw2_pages);
1211 P1(dbbt_start);
1212 P1(bb_byte);
1213 P1(bb_start_bit);
1214 P1(phy_offset);
1215 P1(bchtype);
1216 P1(readlatency);
1217 P1(predelay);
1218 P1(cedelay);
1219 P1(postdelay);
1220 P1(cmdaddpause);
1221 P1(datapause);
1222 P1(tmspeed);
1223 P1(busytimeout);
1224 P1(disbbm);
1225 P1(spare_offset);
Han Xu214b7d52020-05-05 22:04:03 +08001226#if !defined(CONFIG_MX6) || defined(CONFIG_MX6SX) || \
1227 defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
Alice Guo0b103372020-05-05 22:04:01 +08001228 P1(onfi_sync_enable);
1229 P1(onfi_sync_speed);
1230 P1(onfi_sync_nand_data);
1231 P1(disbbm_search);
1232 P1(disbbm_search_limit);
1233 P1(read_retry_enable);
Han Xu214b7d52020-05-05 22:04:03 +08001234#endif
Alice Guo0b103372020-05-05 22:04:01 +08001235 #undef P1
Han Xu214b7d52020-05-05 22:04:03 +08001236 #define P1(x) printf(" %s = 0x%08x\n", #x, dbbt->x)
1237 printf("DBBT :\n");
Alice Guo0b103372020-05-05 22:04:01 +08001238 P1(checksum);
1239 P1(fingerprint);
1240 P1(version);
1241 #undef P1
Han Xu214b7d52020-05-05 22:04:03 +08001242 #define P1(x) printf(" %s = %d\n", #x, dbbt->x)
1243 P1(dbbtpages);
Alice Guo0b103372020-05-05 22:04:01 +08001244 #undef P1
1245
Han Xu214b7d52020-05-05 22:04:03 +08001246 for (i = 0; i < dbbt->dbbtpages; ++i)
1247 printf("%d ", *((u32 *)(dbbt_data_page + i)));
1248
1249 if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
1250 printf("Firmware: image #0 @ 0x%x size 0x%x\n",
1251 fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
1252 printf("Firmware: image #1 @ 0x%x size 0x%x\n",
1253 fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
1254 } else {
1255 printf("Firmware: image #0 @ 0x%x size 0x%x\n",
1256 fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
1257 printf("Firmware: image #1 @ 0x%x size 0x%x\n",
1258 fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
1259 /* TODO: Add extra image information */
Alice Guo0b103372020-05-05 22:04:01 +08001260 }
1261}
1262
Han Xu214b7d52020-05-05 22:04:03 +08001263static bool check_fingerprint(void *data, int fingerprint)
1264{
1265 int off = 4;
1266
1267 return (*(int *)(data + off) == fingerprint);
1268}
1269
Han Xuf797fe82020-05-05 22:04:04 +08001270static int fuse_to_search_count(u32 bank, u32 word, u32 mask, u32 off)
1271{
1272 int err;
1273 u32 val;
1274 int ret;
1275
1276 /* by default, the boot search count from fuse should be 2 */
1277 err = fuse_read(bank, word, &val);
1278 if (err)
1279 return 2;
1280
1281 val = (val & mask) >> off;
1282
1283 switch (val) {
1284 case 0:
1285 ret = 2;
1286 break;
1287 case 1:
1288 case 2:
1289 case 3:
1290 ret = 1 << val;
1291 break;
1292 default:
1293 ret = 2;
1294 }
1295
1296 return ret;
1297}
1298
Han Xu214b7d52020-05-05 22:04:03 +08001299static int nandbcb_dump(struct boot_config *boot_cfg)
1300{
1301 int i;
1302 loff_t off;
1303 struct mtd_info *mtd = boot_cfg->mtd;
1304 struct fcb_block fcb, fcb_copy;
1305 struct dbbt_block dbbt, dbbt_copy;
1306 void *dbbt_data_page, *dbbt_data_page_copy;
1307 bool fcb_not_found, dbbt_not_found;
1308 int ret = 0;
1309
1310 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1311 if (!dbbt_data_page) {
1312 printf("failed to allocate dbbt_data_page\n");
1313 ret = -ENOMEM;
1314 return ret;
1315 }
1316
1317 dbbt_data_page_copy = kzalloc(mtd->writesize, GFP_KERNEL);
1318 if (!dbbt_data_page_copy) {
1319 printf("failed to allocate dbbt_data_page\n");
1320 ret = -ENOMEM;
1321 goto dbbt_page_err;
1322 }
1323
1324 /* read fcb */
1325 fcb_not_found = 1;
1326 off = 0;
1327 for (i = 0; i < g_boot_search_count; ++i) {
1328 if (fcb_not_found) {
1329 ret = read_fcb(boot_cfg, &fcb, off);
1330
1331 if (ret < 0)
1332 goto dbbt_page_copy_err;
1333 else if (ret == 1)
1334 continue;
1335 else if (ret == 0)
1336 if (check_fingerprint(&fcb, FCB_FINGERPRINT))
1337 fcb_not_found = 0;
1338 } else {
1339 ret = read_fcb(boot_cfg, &fcb_copy, off);
1340
1341 if (ret < 0)
1342 goto dbbt_page_copy_err;
1343 if (memcmp(&fcb, &fcb_copy,
1344 sizeof(struct fcb_block))) {
1345 printf("FCB copies are not identical\n");
1346 ret = -EINVAL;
1347 goto dbbt_page_copy_err;
1348 }
1349 }
1350
1351 /* next read location */
1352 off += g_boot_search_stride;
1353 }
1354
1355 /* read dbbt*/
1356 dbbt_not_found = 1;
1357 off = boot_cfg->search_area_size_in_bytes;
1358 for (i = 0; i < g_boot_search_count; ++i) {
1359 if (dbbt_not_found) {
1360 ret = read_dbbt(boot_cfg, &dbbt, dbbt_data_page, off);
1361
1362 if (ret < 0)
1363 goto dbbt_page_copy_err;
1364 else if (ret == 1)
1365 continue;
1366 else if (ret == 0)
1367 if (check_fingerprint(&dbbt, DBBT_FINGERPRINT))
1368 dbbt_not_found = 0;
1369 } else {
1370 ret = read_dbbt(boot_cfg, &dbbt_copy,
1371 dbbt_data_page_copy, off);
1372
1373 if (ret < 0)
1374 goto dbbt_page_copy_err;
1375 if (memcmp(&dbbt, &dbbt_copy,
1376 sizeof(struct dbbt_block))) {
1377 printf("DBBT copies are not identical\n");
1378 ret = -EINVAL;
1379 goto dbbt_page_copy_err;
1380 }
1381 if (dbbt.dbbtpages > 0 &&
1382 memcmp(dbbt_data_page, dbbt_data_page_copy,
1383 mtd->writesize)) {
1384 printf("DBBT data copies are not identical\n");
1385 ret = -EINVAL;
1386 goto dbbt_page_copy_err;
1387 }
1388 }
1389
1390 /* next read location */
1391 off += g_boot_search_stride;
1392 }
1393
1394 dump_structure(boot_cfg, &fcb, &dbbt, dbbt_data_page);
1395
1396dbbt_page_copy_err:
1397 kfree(dbbt_data_page_copy);
1398dbbt_page_err:
1399 kfree(dbbt_data_page);
1400
1401 return ret;
1402}
1403
Alice Guo0b103372020-05-05 22:04:01 +08001404static int do_nandbcb_dump(int argc, char * const argv[])
1405{
Han Xu214b7d52020-05-05 22:04:03 +08001406 struct boot_config cfg;
1407 int ret;
Alice Guo0b103372020-05-05 22:04:01 +08001408
1409 if (argc != 2)
1410 return CMD_RET_USAGE;
1411
Han Xu214b7d52020-05-05 22:04:03 +08001412 memset(&cfg, 0, sizeof(struct boot_config));
1413 if (nandbcb_get_info(argc, argv, &cfg))
1414 return CMD_RET_FAILURE;
Alice Guo0b103372020-05-05 22:04:01 +08001415
Han Xu214b7d52020-05-05 22:04:03 +08001416 if (nandbcb_get_size(argc, argv, 1, &cfg))
1417 return CMD_RET_FAILURE;
Alice Guo0b103372020-05-05 22:04:01 +08001418
Han Xu214b7d52020-05-05 22:04:03 +08001419 if (nandbcb_set_boot_config(argc, argv, &cfg))
1420 return CMD_RET_FAILURE;
Alice Guo0b103372020-05-05 22:04:01 +08001421
Han Xu214b7d52020-05-05 22:04:03 +08001422 ret = nandbcb_dump(&cfg);
1423 if (ret)
1424 return ret;
Alice Guo0b103372020-05-05 22:04:01 +08001425
Han Xu214b7d52020-05-05 22:04:03 +08001426 return ret;
Alice Guo0b103372020-05-05 22:04:01 +08001427}
1428
Han Xu214b7d52020-05-05 22:04:03 +08001429static int do_nandbcb_init(int argc, char * const argv[])
Shyam Saini1d43e242019-06-14 13:05:33 +05301430{
Shyam Saini1d43e242019-06-14 13:05:33 +05301431 u_char *buf;
Han Xu214b7d52020-05-05 22:04:03 +08001432 size_t size;
1433 loff_t addr;
1434 char *endp;
Shyam Saini1d43e242019-06-14 13:05:33 +05301435 int ret;
Han Xu214b7d52020-05-05 22:04:03 +08001436 struct boot_config cfg;
Shyam Saini1d43e242019-06-14 13:05:33 +05301437
1438 if (argc != 4)
1439 return CMD_RET_USAGE;
1440
Han Xu214b7d52020-05-05 22:04:03 +08001441 memset(&cfg, 0, sizeof(struct boot_config));
1442 if (nandbcb_get_info(argc, argv, &cfg))
Shyam Saini1d43e242019-06-14 13:05:33 +05301443 return CMD_RET_FAILURE;
Han Xu214b7d52020-05-05 22:04:03 +08001444
1445 if (nandbcb_get_size(argc, argv, 2, &cfg))
1446 return CMD_RET_FAILURE;
1447 size = cfg.boot_stream1_size;
1448
1449 if (nandbcb_set_boot_config(argc, argv, &cfg))
1450 return CMD_RET_FAILURE;
Shyam Saini1d43e242019-06-14 13:05:33 +05301451
1452 addr = simple_strtoul(argv[1], &endp, 16);
1453 if (*argv[1] == 0 || *endp != 0)
1454 return CMD_RET_FAILURE;
1455
Shyam Saini1d43e242019-06-14 13:05:33 +05301456 buf = map_physmem(addr, size, MAP_WRBACK);
1457 if (!buf) {
1458 puts("failed to map physical memory\n");
1459 return CMD_RET_FAILURE;
1460 }
1461
Han Xu214b7d52020-05-05 22:04:03 +08001462 ret = nandbcb_init(&cfg, buf);
Shyam Saini1d43e242019-06-14 13:05:33 +05301463
1464 return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
1465}
1466
Simon Glass09140112020-05-10 11:40:03 -06001467static int do_nandbcb(struct cmd_tbl *cmdtp, int flag, int argc,
1468 char *const argv[])
Shyam Saini1d43e242019-06-14 13:05:33 +05301469{
1470 const char *cmd;
1471 int ret = 0;
1472
Alice Guo0b103372020-05-05 22:04:01 +08001473 if (argc < 3)
Shyam Saini1d43e242019-06-14 13:05:33 +05301474 goto usage;
1475
Han Xu214b7d52020-05-05 22:04:03 +08001476 /* check the platform config first */
1477 if (is_mx6sx()) {
1478 plat_config = imx6sx_plat_config;
1479 } else if (is_mx7()) {
1480 plat_config = imx7d_plat_config;
1481 } else if (is_mx6ul() || is_mx6ull()) {
1482 plat_config = imx6ul_plat_config;
1483 } else if (is_mx6() && !is_mx6sx() && !is_mx6ul() && !is_mx6ull()) {
1484 plat_config = imx6qdl_plat_config;
1485 } else if (is_imx8mq()) {
1486 plat_config = imx8mq_plat_config;
1487 } else if (is_imx8mm()) {
1488 plat_config = imx8mm_plat_config;
1489 } else if (is_imx8mn()) {
1490 plat_config = imx8mn_plat_config;
1491 } else if (is_imx8qm() || is_imx8qxp()) {
1492 plat_config = imx8q_plat_config;
1493 } else {
1494 printf("ERROR: Unknown platform\n");
1495 return CMD_RET_FAILURE;
1496 }
1497
Han Xuf797fe82020-05-05 22:04:04 +08001498 if (plat_config.misc_flags & BT_SEARCH_CNT_FROM_FUSE) {
1499 if (is_imx8qxp()) {
1500 g_boot_search_count = fuse_to_search_count(0, 720,
1501 0xc0, 6);
1502 printf("search count set to %d from fuse\n",
1503 g_boot_search_count);
1504 }
1505 }
Han Xu214b7d52020-05-05 22:04:03 +08001506
Shyam Saini1d43e242019-06-14 13:05:33 +05301507 cmd = argv[1];
1508 --argc;
1509 ++argv;
1510
Han Xu214b7d52020-05-05 22:04:03 +08001511 if (strcmp(cmd, "init") == 0) {
1512 ret = do_nandbcb_init(argc, argv);
Shyam Saini1d43e242019-06-14 13:05:33 +05301513 goto done;
1514 }
1515
Alice Guo0b103372020-05-05 22:04:01 +08001516 if (strcmp(cmd, "dump") == 0) {
1517 ret = do_nandbcb_dump(argc, argv);
1518 goto done;
1519 }
1520
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001521 if (strcmp(cmd, "bcbonly") == 0) {
1522 ret = do_nandbcb_bcbonly(argc, argv);
1523 goto done;
1524 }
1525
Shyam Saini1d43e242019-06-14 13:05:33 +05301526done:
1527 if (ret != -1)
1528 return ret;
1529usage:
1530 return CMD_RET_USAGE;
1531}
1532
Parthiban Nallathambi4ee0ff12019-08-23 18:35:10 +02001533#ifdef CONFIG_SYS_LONGHELP
Shyam Saini1d43e242019-06-14 13:05:33 +05301534static char nandbcb_help_text[] =
Han Xu214b7d52020-05-05 22:04:03 +08001535 "init addr off|partition len - update 'len' bytes starting at\n"
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001536 " 'off|part' to memory address 'addr', skipping bad blocks\n"
Han Xu214b7d52020-05-05 22:04:03 +08001537 "nandbcb bcbonly off|partition fw1-off fw1-size [fw2-off fw2-size]\n"
1538 " - write BCB only (FCB and DBBT)\n"
1539 " where `fwx-size` is fw sizes in bytes, `fw1-off`\n"
Igor Opaniuk061b63b2019-12-16 14:06:44 +02001540 " and `fw2-off` - firmware offsets\n"
1541 " FIY, BCB isn't erased automatically, so mtd erase should\n"
1542 " be called in advance before writing new BCB:\n"
Alice Guo0b103372020-05-05 22:04:01 +08001543 " > mtd erase mx7-bcb\n"
Han Xu214b7d52020-05-05 22:04:03 +08001544 "nandbcb dump off|partition - dump/verify boot structures\n";
Parthiban Nallathambi4ee0ff12019-08-23 18:35:10 +02001545#endif
Shyam Saini1d43e242019-06-14 13:05:33 +05301546
Han Xu214b7d52020-05-05 22:04:03 +08001547U_BOOT_CMD(nandbcb, 7, 1, do_nandbcb,
1548 "i.MX NAND Boot Control Blocks write",
Shyam Saini1d43e242019-06-14 13:05:33 +05301549 nandbcb_help_text
1550);