blob: 7157c9e97999d8c84568bd4d9cea7b0a3f6e9b49 [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)
Ye Li233b0b02020-08-02 23:07:55 -0700278 boot_stream2_address =
279 (loff_t)boot_cfg->secondary_boot_stream_off_in_MB * 1024 * 1024;
Han Xu214b7d52020-05-05 22:04:03 +0800280
281 max_boot_stream_size = boot_stream2_address - boot_stream1_address;
282
283 /* sanity check */
284 if (max_boot_stream_size <= 0) {
285 debug("st1_addr: %llx, st2_addr: %llx, max: %llx\n",
286 boot_stream1_address, boot_stream2_address,
287 max_boot_stream_size);
288 printf("something wrong with firmware address settings\n");
289 return CMD_RET_FAILURE;
290 }
291 boot_cfg->boot_stream1_address = boot_stream1_address;
292 boot_cfg->boot_stream2_address = boot_stream2_address;
293 boot_cfg->max_boot_stream_size = max_boot_stream_size;
294
295 /* set the boot_stream size as the input size now */
296 if (boot_cfg->input_size) {
297 boot_cfg->boot_stream1_size = boot_cfg->input_size;
298 boot_cfg->boot_stream2_size = boot_cfg->input_size;
299 }
300
301 return CMD_RET_SUCCESS;
302}
303
304static int nandbcb_check_space(struct boot_config *boot_cfg)
305{
306 size_t maxsize = boot_cfg->maxsize;
307 size_t max_boot_stream_size = boot_cfg->max_boot_stream_size;
308 loff_t boot_stream2_address = boot_cfg->boot_stream2_address;
309
310 if (boot_cfg->boot_stream1_size &&
311 boot_cfg->boot_stream1_size > max_boot_stream_size) {
312 printf("boot stream1 doesn't fit, check partition size or settings\n");
313 return CMD_RET_FAILURE;
314 }
315
316 if (boot_cfg->boot_stream2_size &&
317 boot_cfg->boot_stream2_size > maxsize - boot_stream2_address) {
318 printf("boot stream2 doesn't fit, check partition size or settings\n");
319 return CMD_RET_FAILURE;
320 }
321
322 return CMD_RET_SUCCESS;
323}
Shyam Saini1d43e242019-06-14 13:05:33 +0530324
Parthiban Nallathambi6aa87492019-10-18 11:46:19 +0200325#if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
326static uint8_t reverse_bit(uint8_t b)
327{
328 b = (b & 0xf0) >> 4 | (b & 0x0f) << 4;
329 b = (b & 0xcc) >> 2 | (b & 0x33) << 2;
330 b = (b & 0xaa) >> 1 | (b & 0x55) << 1;
331
332 return b;
333}
334
335static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits)
336{
337 int i, j, m = 13;
338 int blocksize = 128;
339 int numblocks = 8;
340 int ecc_buf_size = (m * eccbits + 7) / 8;
341 struct bch_control *bch = init_bch(m, eccbits, 0);
342 u8 *ecc_buf = kzalloc(ecc_buf_size, GFP_KERNEL);
343 u8 *tmp_buf = kzalloc(blocksize * numblocks, GFP_KERNEL);
344 u8 *psrc, *pdst;
345
346 /*
347 * The blocks here are bit aligned. If eccbits is a multiple of 8,
348 * we just can copy bytes. Otherwiese we must move the blocks to
349 * the next free bit position.
350 */
351 WARN_ON(eccbits % 8);
352
353 memcpy(tmp_buf, fcb, sizeof(*fcb));
354
355 for (i = 0; i < numblocks; i++) {
356 memset(ecc_buf, 0, ecc_buf_size);
357 psrc = tmp_buf + i * blocksize;
358 pdst = buf + i * (blocksize + ecc_buf_size);
359
360 /* copy data byte aligned to destination buf */
361 memcpy(pdst, psrc, blocksize);
362
363 /*
364 * imx-kobs use a modified encode_bch which reverse the
365 * bit order of the data before calculating bch.
366 * Do this in the buffer and use the bch lib here.
367 */
368 for (j = 0; j < blocksize; j++)
369 psrc[j] = reverse_bit(psrc[j]);
370
371 encode_bch(bch, psrc, blocksize, ecc_buf);
372
373 /* reverse ecc bit */
374 for (j = 0; j < ecc_buf_size; j++)
375 ecc_buf[j] = reverse_bit(ecc_buf[j]);
376
377 /* Here eccbuf is byte aligned and we can just copy it */
378 memcpy(pdst + blocksize, ecc_buf, ecc_buf_size);
379 }
380
381 kfree(ecc_buf);
382 kfree(tmp_buf);
383 free_bch(bch);
384}
385#else
386
Shyam Saini1d43e242019-06-14 13:05:33 +0530387static u8 calculate_parity_13_8(u8 d)
388{
389 u8 p = 0;
390
391 p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0;
392 p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^
393 GETBIT(d, 1)) << 1;
394 p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^
395 GETBIT(d, 0)) << 2;
396 p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3;
397 p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^
398 GETBIT(d, 1) ^ GETBIT(d, 0)) << 4;
399
400 return p;
401}
402
403static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
404{
405 int i;
406 u8 *src = _src;
407 u8 *ecc = _ecc;
408
409 for (i = 0; i < size; i++)
410 ecc[i] = calculate_parity_13_8(src[i]);
411}
Parthiban Nallathambi6aa87492019-10-18 11:46:19 +0200412#endif
Shyam Saini1d43e242019-06-14 13:05:33 +0530413
414static u32 calc_chksum(void *buf, size_t size)
415{
416 u32 chksum = 0;
417 u8 *bp = buf;
418 size_t i;
419
420 for (i = 0; i < size; i++)
421 chksum += bp[i];
422
423 return ~chksum;
424}
425
Han Xu214b7d52020-05-05 22:04:03 +0800426static void fill_fcb(struct fcb_block *fcb, struct boot_config *boot_cfg)
Shyam Saini1d43e242019-06-14 13:05:33 +0530427{
Han Xu214b7d52020-05-05 22:04:03 +0800428 struct mtd_info *mtd = boot_cfg->mtd;
Shyam Saini1d43e242019-06-14 13:05:33 +0530429 struct nand_chip *chip = mtd_to_nand(mtd);
430 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100431 struct mxs_nand_layout l;
432
433 mxs_nand_get_layout(mtd, &l);
Shyam Saini1d43e242019-06-14 13:05:33 +0530434
435 fcb->fingerprint = FCB_FINGERPRINT;
436 fcb->version = FCB_VERSION_1;
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100437
Han Xu214b7d52020-05-05 22:04:03 +0800438 fcb->datasetup = 80;
439 fcb->datahold = 60;
440 fcb->addr_setup = 25;
441 fcb->dsample_time = 6;
442
Shyam Saini1d43e242019-06-14 13:05:33 +0530443 fcb->pagesize = mtd->writesize;
444 fcb->oob_pagesize = mtd->writesize + mtd->oobsize;
445 fcb->sectors = mtd->erasesize / mtd->writesize;
446
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100447 fcb->meta_size = l.meta_size;
448 fcb->nr_blocks = l.nblocks;
449 fcb->ecc_nr = l.data0_size;
450 fcb->ecc_level = l.ecc0;
451 fcb->ecc_size = l.datan_size;
452 fcb->ecc_type = l.eccn;
Han Xuc6ed3502020-05-05 22:03:59 +0800453 fcb->bchtype = l.gf_len;
Shyam Saini1d43e242019-06-14 13:05:33 +0530454
Han Xu214b7d52020-05-05 22:04:03 +0800455 /* DBBT search area starts from the next block after all FCB */
456 fcb->dbbt_start = boot_cfg->search_area_size_in_pages;
Shyam Saini1d43e242019-06-14 13:05:33 +0530457
458 fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset;
459 fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset;
460
461 fcb->phy_offset = mtd->writesize;
462
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100463 fcb->disbbm = 0;
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100464
Han Xu214b7d52020-05-05 22:04:03 +0800465 fcb->fw1_start = CONV_TO_PAGES(boot_cfg->boot_stream1_address);
466 fcb->fw2_start = CONV_TO_PAGES(boot_cfg->boot_stream2_address);
467 fcb->fw1_pages = CONV_TO_PAGES(boot_cfg->boot_stream1_size);
468 fcb->fw2_pages = CONV_TO_PAGES(boot_cfg->boot_stream2_size);
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100469
Shyam Saini1d43e242019-06-14 13:05:33 +0530470 fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
471}
472
Han Xu214b7d52020-05-05 22:04:03 +0800473static int fill_dbbt_data(struct mtd_info *mtd, void *buf, int num_blocks)
Shyam Saini1d43e242019-06-14 13:05:33 +0530474{
475 int n, n_bad_blocks = 0;
476 u32 *bb = buf + 0x8;
477 u32 *n_bad_blocksp = buf + 0x4;
478
479 for (n = 0; n < num_blocks; n++) {
Ye Li983f5e02020-08-02 22:59:43 -0700480 loff_t offset = (loff_t)n * mtd->erasesize;
Shyam Saini1d43e242019-06-14 13:05:33 +0530481 if (mtd_block_isbad(mtd, offset)) {
482 n_bad_blocks++;
483 *bb = n;
484 bb++;
485 }
486 }
487
488 *n_bad_blocksp = n_bad_blocks;
489
490 return n_bad_blocks;
491}
492
Han Xu214b7d52020-05-05 22:04:03 +0800493/*
494 * return 1 - bad block
495 * return 0 - read successfully
496 * return < 0 - read failed
497 */
498static int read_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb,
499 loff_t off)
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100500{
Han Xu214b7d52020-05-05 22:04:03 +0800501 struct mtd_info *mtd;
502 void *fcb_raw_page;
503 size_t size;
504 int ret = 0;
505
506 mtd = boot_cfg->mtd;
Han Xu214b7d52020-05-05 22:04:03 +0800507 if (mtd_block_isbad(mtd, off)) {
508 printf("Block %d is bad, skipped\n", (int)CONV_TO_BLOCKS(off));
509 return 1;
510 }
511
Ye Lif637c402020-08-02 22:43:45 -0700512 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
513 if (!fcb_raw_page) {
514 debug("failed to allocate fcb_raw_page\n");
515 ret = -ENOMEM;
516 return ret;
517 }
518
Han Xu214b7d52020-05-05 22:04:03 +0800519 /*
520 * User BCH hardware to decode ECC for FCB
521 */
522 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
523 size = sizeof(struct fcb_block);
524
525 /* switch nand BCH to FCB compatible settings */
526 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
527 mxs_nand_mode_fcb_62bit(mtd);
528 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
529 mxs_nand_mode_fcb_40bit(mtd);
530
531 ret = nand_read(mtd, off, &size, (u_char *)fcb);
532
533 /* switch BCH back */
534 mxs_nand_mode_normal(mtd);
535 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
536 off, size, ret ? "ERROR" : "OK");
537
538 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
539 /* raw read*/
540 mtd_oob_ops_t ops = {
541 .datbuf = (u8 *)fcb_raw_page,
542 .oobbuf = ((u8 *)fcb_raw_page) + mtd->writesize,
543 .len = mtd->writesize,
544 .ooblen = mtd->oobsize,
545 .mode = MTD_OPS_RAW
546 };
547
548 ret = mtd_read_oob(mtd, off, &ops);
549 printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
550 off, ops.len, ret ? "ERROR" : "OK");
551 }
552
553 if (ret)
554 goto fcb_raw_page_err;
555
556 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
557 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B))
558 memcpy(fcb, fcb_raw_page + 12, sizeof(struct fcb_block));
559
560/* TODO: check if it can pass Hamming check */
561
562fcb_raw_page_err:
563 kfree(fcb_raw_page);
564
565 return ret;
566}
567
568static int write_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb)
569{
570 struct mtd_info *mtd;
571 void *fcb_raw_page = NULL;
Ye Li9dd599a2020-08-02 20:55:17 -0700572 int i, ret = 0;
Han Xu214b7d52020-05-05 22:04:03 +0800573 loff_t off;
574 size_t size;
575
576 mtd = boot_cfg->mtd;
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100577
578 /*
579 * We prepare raw page only for i.MX6, for i.MX7 we
580 * leverage BCH hw module instead
581 */
Han Xu214b7d52020-05-05 22:04:03 +0800582 if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
583 (plat_config.misc_flags & FCB_LAYOUT_RESV_12B)) {
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100584 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize,
585 GFP_KERNEL);
586 if (!fcb_raw_page) {
587 debug("failed to allocate fcb_raw_page\n");
588 ret = -ENOMEM;
589 return ret;
590 }
591
592#if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
593 /* 40 bit BCH, for i.MX6UL(L) */
594 encode_bch_ecc(fcb_raw_page + 32, fcb, 40);
595#else
596 memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block));
597 encode_hamming_13_8(fcb_raw_page + 12,
598 fcb_raw_page + 12 + 512, 512);
599#endif
600 /*
601 * Set the first and second byte of OOB data to 0xFF,
602 * not 0x00. These bytes are used as the Manufacturers Bad
603 * Block Marker (MBBM). Since the FCB is mostly written to
604 * the first page in a block, a scan for
605 * factory bad blocks will detect these blocks as bad, e.g.
606 * when function nand_scan_bbt() is executed to build a new
607 * bad block table.
608 */
609 memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
610 }
Han Xu214b7d52020-05-05 22:04:03 +0800611
612 /* start writing FCB from the very beginning */
613 off = 0;
614
615 for (i = 0; i < g_boot_search_count; i++) {
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100616 if (mtd_block_isbad(mtd, off)) {
617 printf("Block %d is bad, skipped\n", i);
618 continue;
619 }
620
621 /*
Han Xu214b7d52020-05-05 22:04:03 +0800622 * User BCH hardware module to generate ECC for FCB
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100623 */
Han Xu214b7d52020-05-05 22:04:03 +0800624 if (plat_config.misc_flags & FCB_ENCODE_BCH) {
625 size = sizeof(struct fcb_block);
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100626
627 /* switch nand BCH to FCB compatible settings */
Han Xu214b7d52020-05-05 22:04:03 +0800628 if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
629 mxs_nand_mode_fcb_62bit(mtd);
630 else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
631 mxs_nand_mode_fcb_40bit(mtd);
Alice Guo0b103372020-05-05 22:04:01 +0800632
Han Xu214b7d52020-05-05 22:04:03 +0800633 ret = nand_write(mtd, off, &size, (u_char *)fcb);
Alice Guo0b103372020-05-05 22:04:01 +0800634
Han Xu214b7d52020-05-05 22:04:03 +0800635 /* switch BCH back */
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100636 mxs_nand_mode_normal(mtd);
Han Xu214b7d52020-05-05 22:04:03 +0800637 printf("NAND FCB write to 0x%zx offset 0x%llx written: %s\n",
638 size, off, ret ? "ERROR" : "OK");
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100639
Han Xu214b7d52020-05-05 22:04:03 +0800640 } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100641 /* raw write */
642 mtd_oob_ops_t ops = {
643 .datbuf = (u8 *)fcb_raw_page,
644 .oobbuf = ((u8 *)fcb_raw_page) +
645 mtd->writesize,
646 .len = mtd->writesize,
647 .ooblen = mtd->oobsize,
648 .mode = MTD_OPS_RAW
649 };
650
Han Xu214b7d52020-05-05 22:04:03 +0800651 ret = mtd_write_oob(mtd, off, &ops);
652 printf("NAND FCB write to 0x%llxx offset 0x%zx written: %s\n", off, ops.len, ret ? "ERROR" : "OK");
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100653 }
654
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100655 if (ret)
656 goto fcb_raw_page_err;
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100657
Han Xu214b7d52020-05-05 22:04:03 +0800658 /* next writing location */
659 off += g_boot_search_stride;
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100660 }
661
662fcb_raw_page_err:
Han Xu214b7d52020-05-05 22:04:03 +0800663 kfree(fcb_raw_page);
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100664
665 return ret;
666}
667
Han Xu214b7d52020-05-05 22:04:03 +0800668/*
669 * return 1 - bad block
670 * return 0 - read successfully
671 * return < 0 - read failed
672 */
673static int read_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
674 void *dbbt_data_page, loff_t off)
Shyam Saini1d43e242019-06-14 13:05:33 +0530675{
Han Xu214b7d52020-05-05 22:04:03 +0800676 size_t size;
677 struct mtd_info *mtd;
678 loff_t to;
679 int ret;
680
681 mtd = boot_cfg->mtd;
682
683 if (mtd_block_isbad(mtd, off)) {
684 printf("Block %d is bad, skipped\n",
685 (int)CONV_TO_BLOCKS(off));
686 return 1;
687 }
688
689 size = sizeof(struct dbbt_block);
690 ret = nand_read(mtd, off, &size, (u_char *)dbbt);
691 printf("NAND DBBT read from 0x%llx offset 0x%zx read: %s\n",
692 off, size, ret ? "ERROR" : "OK");
693 if (ret)
694 return ret;
695
696 /* dbbtpages == 0 if no bad blocks */
697 if (dbbt->dbbtpages > 0) {
698 to = off + 4 * mtd->writesize;
699 size = mtd->writesize;
700 ret = nand_read(mtd, to, &size, dbbt_data_page);
701 printf("DBBT data read from 0x%llx offset 0x%zx read: %s\n",
702 to, size, ret ? "ERROR" : "OK");
703
704 if (ret)
705 return ret;
706 }
707
708 return 0;
709}
710
711static int write_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
712 void *dbbt_data_page)
713{
714 int i;
715 loff_t off, to;
716 size_t size;
717 struct mtd_info *mtd;
718 int ret;
719
720 mtd = boot_cfg->mtd;
721
722 /* start writing DBBT after all FCBs */
723 off = boot_cfg->search_area_size_in_bytes;
724 size = mtd->writesize;
725
726 for (i = 0; i < g_boot_search_count; i++) {
727 if (mtd_block_isbad(mtd, off)) {
728 printf("Block %d is bad, skipped\n",
729 (int)(i + CONV_TO_BLOCKS(off)));
730 continue;
731 }
732
733 ret = nand_write(mtd, off, &size, (u_char *)dbbt);
734 printf("NAND DBBT write to 0x%llx offset 0x%zx written: %s\n",
735 off, size, ret ? "ERROR" : "OK");
736 if (ret)
737 return ret;
738
739 /* dbbtpages == 0 if no bad blocks */
740 if (dbbt->dbbtpages > 0) {
741 to = off + 4 * mtd->writesize;
742 ret = nand_write(mtd, to, &size, dbbt_data_page);
743 printf("DBBT data write to 0x%llx offset 0x%zx written: %s\n",
744 to, size, ret ? "ERROR" : "OK");
745
746 if (ret)
747 return ret;
748 }
749
750 /* next writing location */
751 off += g_boot_search_stride;
752 }
753
754 return 0;
755}
756
757/* reuse the check_skip_len from nand_util.c with minor change*/
758static int check_skip_length(struct boot_config *boot_cfg, loff_t offset,
759 size_t length, size_t *used)
760{
761 struct mtd_info *mtd = boot_cfg->mtd;
762 size_t maxsize = boot_cfg->maxsize;
763 size_t len_excl_bad = 0;
764 int ret = 0;
765
766 while (len_excl_bad < length) {
767 size_t block_len, block_off;
768 loff_t block_start;
769
770 if (offset >= maxsize)
771 return -1;
772
773 block_start = offset & ~(loff_t)(mtd->erasesize - 1);
774 block_off = offset & (mtd->erasesize - 1);
775 block_len = mtd->erasesize - block_off;
776
777 if (!nand_block_isbad(mtd, block_start))
778 len_excl_bad += block_len;
779 else
780 ret = 1;
781
782 offset += block_len;
783 *used += block_len;
784 }
785
786 /* If the length is not a multiple of block_len, adjust. */
787 if (len_excl_bad > length)
788 *used -= (len_excl_bad - length);
789
790 return ret;
791}
792
793static int nandbcb_get_next_good_blk_addr(struct boot_config *boot_cfg,
794 struct boot_stream_config *bs_cfg)
795{
796 struct mtd_info *mtd = boot_cfg->mtd;
797 loff_t offset = bs_cfg->bs_addr;
798 size_t length = bs_cfg->bs_size;
799 size_t used = 0;
800 int ret;
801
802 ret = check_skip_length(boot_cfg, offset, length, &used);
803
804 if (ret < 0)
805 return ret;
806
807 /* get next image address */
808 bs_cfg->next_bs_addr = (u32)(offset + used + mtd->erasesize - 1)
809 / (u32)mtd->erasesize * mtd->erasesize;
810
811 return ret;
812}
813
814static int nandbcb_write_bs_skip_bad(struct boot_config *boot_cfg,
815 struct boot_stream_config *bs_cfg)
816{
817 struct mtd_info *mtd;
818 void *buf;
819 loff_t offset, maxsize;
820 size_t size;
821 size_t length;
822 int ret;
823 bool padding_flag = false;
824
825 mtd = boot_cfg->mtd;
826 offset = bs_cfg->bs_addr;
827 maxsize = boot_cfg->maxsize;
828 size = bs_cfg->bs_size;
829
830 /* some boot images may need leading offset */
831 if (bs_cfg->need_padding &&
832 (plat_config.misc_flags & FIRMWARE_NEED_PADDING))
833 padding_flag = 1;
834
835 if (padding_flag)
836 length = ALIGN(size + FLASH_OFFSET_STANDARD, mtd->writesize);
837 else
838 length = ALIGN(size, mtd->writesize);
839
840 buf = kzalloc(length, GFP_KERNEL);
841 if (!buf) {
842 printf("failed to allocate buffer for firmware\n");
843 ret = -ENOMEM;
844 return ret;
845 }
846
847 if (padding_flag)
848 memcpy(buf + FLASH_OFFSET_STANDARD, bs_cfg->bs_buf, size);
849 else
850 memcpy(buf, bs_cfg->bs_buf, size);
851
852 ret = nand_write_skip_bad(mtd, offset, &length, NULL, maxsize,
853 (u_char *)buf, WITH_WR_VERIFY);
854 printf("Write %s @0x%llx offset, 0x%zx bytes written: %s\n",
855 bs_cfg->bs_label, offset, length, ret ? "ERROR" : "OK");
856
857 if (ret)
858 /* write image failed, quit */
859 goto err;
860
861 /* get next good blk address if needed */
862 if (bs_cfg->need_padding) {
863 ret = nandbcb_get_next_good_blk_addr(boot_cfg, bs_cfg);
864 if (ret < 0) {
865 printf("Next image cannot fit in NAND partition\n");
866 goto err;
867 }
868 }
869
870 /* now we know how the exact image size written to NAND */
871 bs_cfg->bs_size = length;
872 return 0;
873err:
874 kfree(buf);
875 return ret;
876}
877
878static int nandbcb_write_fw(struct boot_config *boot_cfg, u_char *buf,
879 int index)
880{
881 int i;
882 loff_t offset;
883 size_t size;
884 loff_t next_bs_addr;
885 struct boot_stream_config bs_cfg;
886 int ret;
887
888 for (i = 0; i < 2; ++i) {
889 if (!(FW_INX(i) & index))
890 continue;
891
892 if (i == 0) {
893 offset = boot_cfg->boot_stream1_address;
894 size = boot_cfg->boot_stream1_size;
895 } else {
896 offset = boot_cfg->boot_stream2_address;
897 size = boot_cfg->boot_stream2_size;
898 }
899
900 /* write Firmware*/
901 if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
902 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
903 sprintf(bs_cfg.bs_label, "firmware%d", i);
904 bs_cfg.bs_addr = offset;
905 bs_cfg.bs_size = size;
906 bs_cfg.bs_buf = buf;
907 bs_cfg.need_padding = 1;
908
909 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
910 if (ret)
911 return ret;
912
913 /* update the boot stream size */
914 if (i == 0)
915 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
916 else
917 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
918
919 } else {
920 /* some platforms need extra firmware */
921 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
922 sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 1);
923 bs_cfg.bs_addr = offset;
924 bs_cfg.bs_size = IMX8MQ_HDMI_FW_SZ;
925 bs_cfg.bs_buf = buf;
926 bs_cfg.need_padding = 1;
927
928 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
929 if (ret)
930 return ret;
931
932 /* update the boot stream size */
933 if (i == 0)
934 boot_cfg->boot_stream1_size = bs_cfg.bs_size;
935 else
936 boot_cfg->boot_stream2_size = bs_cfg.bs_size;
937
938 /* get next image address */
939 next_bs_addr = bs_cfg.next_bs_addr;
940
941 memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
942 sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 2);
943 bs_cfg.bs_addr = next_bs_addr;
944 bs_cfg.bs_size = IMX8MQ_SPL_SZ;
945 bs_cfg.bs_buf = (u_char *)(buf + IMX8MQ_HDMI_FW_SZ);
946 bs_cfg.need_padding = 0;
947
948 ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
949 if (ret)
950 return ret;
951 }
952 }
953
954 return 0;
955}
956
957static int nandbcb_init(struct boot_config *boot_cfg, u_char *buf)
958{
959 struct mtd_info *mtd;
Shyam Saini1d43e242019-06-14 13:05:33 +0530960 nand_erase_options_t opts;
961 struct fcb_block *fcb;
962 struct dbbt_block *dbbt;
Han Xu214b7d52020-05-05 22:04:03 +0800963 void *dbbt_page, *dbbt_data_page;
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100964 int ret;
Han Xu214b7d52020-05-05 22:04:03 +0800965 loff_t maxsize, off;
966
967 mtd = boot_cfg->mtd;
968 maxsize = boot_cfg->maxsize;
969 off = boot_cfg->offset;
Shyam Saini1d43e242019-06-14 13:05:33 +0530970
971 /* erase */
972 memset(&opts, 0, sizeof(opts));
973 opts.offset = off;
974 opts.length = maxsize - 1;
975 ret = nand_erase_opts(mtd, &opts);
976 if (ret) {
977 printf("%s: erase failed (ret = %d)\n", __func__, ret);
978 return ret;
979 }
980
981 /*
982 * Reference documentation from i.MX6DQRM section 8.5.2.2
983 *
984 * Nand Boot Control Block(BCB) contains two data structures,
985 * - Firmware Configuration Block(FCB)
986 * - Discovered Bad Block Table(DBBT)
987 *
988 * FCB contains,
989 * - nand timings
990 * - DBBT search page address,
991 * - start page address of primary firmware
992 * - start page address of secondary firmware
993 *
994 * setup fcb:
995 * - number of blocks = mtd partition size / mtd erasesize
996 * - two firmware blocks, primary and secondary
997 * - first 4 block for FCB/DBBT
998 * - rest split in half for primary and secondary firmware
Han Xu214b7d52020-05-05 22:04:03 +0800999 * - same firmware write twice
Shyam Saini1d43e242019-06-14 13:05:33 +05301000 */
Shyam Saini1d43e242019-06-14 13:05:33 +05301001
Han Xu214b7d52020-05-05 22:04:03 +08001002 /* write Firmware*/
1003 ret = nandbcb_write_fw(boot_cfg, buf, FW_ALL);
1004 if (ret)
1005 goto err;
Shyam Saini1d43e242019-06-14 13:05:33 +05301006
1007 /* fill fcb */
1008 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1009 if (!fcb) {
1010 debug("failed to allocate fcb\n");
1011 ret = -ENOMEM;
Han Xu214b7d52020-05-05 22:04:03 +08001012 return ret;
Shyam Saini1d43e242019-06-14 13:05:33 +05301013 }
Han Xu214b7d52020-05-05 22:04:03 +08001014 fill_fcb(fcb, boot_cfg);
Shyam Saini1d43e242019-06-14 13:05:33 +05301015
Han Xu214b7d52020-05-05 22:04:03 +08001016 ret = write_fcb(boot_cfg, fcb);
Alice Guo0b103372020-05-05 22:04:01 +08001017
Shyam Saini1d43e242019-06-14 13:05:33 +05301018 /* fill dbbt */
1019 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1020 if (!dbbt_page) {
1021 debug("failed to allocate dbbt_page\n");
1022 ret = -ENOMEM;
1023 goto fcb_err;
1024 }
1025
1026 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1027 if (!dbbt_data_page) {
1028 debug("failed to allocate dbbt_data_page\n");
1029 ret = -ENOMEM;
1030 goto dbbt_page_err;
1031 }
1032
1033 dbbt = dbbt_page;
1034 dbbt->checksum = 0;
Han Xu214b7d52020-05-05 22:04:03 +08001035 dbbt->fingerprint = DBBT_FINGERPRINT;
Shyam Saini1d43e242019-06-14 13:05:33 +05301036 dbbt->version = DBBT_VERSION_1;
Han Xu214b7d52020-05-05 22:04:03 +08001037 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
Shyam Saini1d43e242019-06-14 13:05:33 +05301038 if (ret < 0)
1039 goto dbbt_data_page_err;
1040 else if (ret > 0)
1041 dbbt->dbbtpages = 1;
1042
Han Xu214b7d52020-05-05 22:04:03 +08001043 /* write dbbt */
1044 ret = write_dbbt(boot_cfg, dbbt, dbbt_data_page);
Igor Opaniuk1b899a82019-11-03 16:49:45 +01001045 if (ret < 0)
1046 printf("failed to write FCB/DBBT\n");
Shyam Saini1d43e242019-06-14 13:05:33 +05301047
Shyam Saini1d43e242019-06-14 13:05:33 +05301048dbbt_data_page_err:
1049 kfree(dbbt_data_page);
1050dbbt_page_err:
1051 kfree(dbbt_page);
1052fcb_err:
1053 kfree(fcb);
Shyam Saini1d43e242019-06-14 13:05:33 +05301054err:
1055 return ret;
1056}
1057
Simon Glass09140112020-05-10 11:40:03 -06001058static int do_nandbcb_bcbonly(int argc, char *const argv[])
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001059{
1060 struct fcb_block *fcb;
1061 struct dbbt_block *dbbt;
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001062 struct mtd_info *mtd;
Han Xu214b7d52020-05-05 22:04:03 +08001063 nand_erase_options_t opts;
1064 size_t maxsize;
1065 loff_t off;
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001066 void *dbbt_page, *dbbt_data_page;
Han Xu214b7d52020-05-05 22:04:03 +08001067 int ret;
1068 struct boot_config cfg;
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001069
Han Xu214b7d52020-05-05 22:04:03 +08001070 if (argc < 4)
1071 return CMD_RET_USAGE;
1072
1073 memset(&cfg, 0, sizeof(struct boot_config));
1074 if (nandbcb_get_info(argc, argv, &cfg))
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001075 return CMD_RET_FAILURE;
Han Xu214b7d52020-05-05 22:04:03 +08001076
1077 /* only get the partition info */
1078 if (nandbcb_get_size(2, argv, 1, &cfg))
1079 return CMD_RET_FAILURE;
1080
1081 if (nandbcb_set_boot_config(argc, argv, &cfg))
1082 return CMD_RET_FAILURE;
1083
1084 mtd = cfg.mtd;
1085
1086 cfg.boot_stream1_address = simple_strtoul(argv[2], NULL, 16);
1087 cfg.boot_stream1_size = simple_strtoul(argv[3], NULL, 16);
1088 cfg.boot_stream1_size = ALIGN(cfg.boot_stream1_size, mtd->writesize);
1089
1090 if (argc > 5) {
1091 cfg.boot_stream2_address = simple_strtoul(argv[4], NULL, 16);
1092 cfg.boot_stream2_size = simple_strtoul(argv[5], NULL, 16);
1093 cfg.boot_stream2_size = ALIGN(cfg.boot_stream2_size,
1094 mtd->writesize);
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001095 }
1096
Han Xu214b7d52020-05-05 22:04:03 +08001097 /* sanity check */
1098 nandbcb_check_space(&cfg);
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001099
Han Xu214b7d52020-05-05 22:04:03 +08001100 maxsize = cfg.maxsize;
1101 off = cfg.offset;
1102
1103 /* erase the previous FCB/DBBT */
1104 memset(&opts, 0, sizeof(opts));
1105 opts.offset = off;
1106 opts.length = g_boot_search_stride * 2;
1107 ret = nand_erase_opts(mtd, &opts);
1108 if (ret) {
1109 printf("%s: erase failed (ret = %d)\n", __func__, ret);
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001110 return CMD_RET_FAILURE;
Han Xu214b7d52020-05-05 22:04:03 +08001111 }
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001112
1113 /* fill fcb */
1114 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
1115 if (!fcb) {
Han Xu214b7d52020-05-05 22:04:03 +08001116 printf("failed to allocate fcb\n");
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001117 ret = -ENOMEM;
1118 return CMD_RET_FAILURE;
1119 }
1120
Han Xu214b7d52020-05-05 22:04:03 +08001121 fill_fcb(fcb, &cfg);
1122
1123 /* write fcb */
1124 ret = write_fcb(&cfg, fcb);
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001125
1126 /* fill dbbt */
1127 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
1128 if (!dbbt_page) {
Han Xu214b7d52020-05-05 22:04:03 +08001129 printf("failed to allocate dbbt_page\n");
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001130 ret = -ENOMEM;
1131 goto fcb_err;
1132 }
1133
1134 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1135 if (!dbbt_data_page) {
Han Xu214b7d52020-05-05 22:04:03 +08001136 printf("failed to allocate dbbt_data_page\n");
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001137 ret = -ENOMEM;
1138 goto dbbt_page_err;
1139 }
1140
1141 dbbt = dbbt_page;
1142 dbbt->checksum = 0;
Han Xu214b7d52020-05-05 22:04:03 +08001143 dbbt->fingerprint = DBBT_FINGERPRINT;
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001144 dbbt->version = DBBT_VERSION_1;
Han Xu214b7d52020-05-05 22:04:03 +08001145 ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001146 if (ret < 0)
1147 goto dbbt_data_page_err;
1148 else if (ret > 0)
1149 dbbt->dbbtpages = 1;
1150
Han Xu214b7d52020-05-05 22:04:03 +08001151 /* write dbbt */
1152 ret = write_dbbt(&cfg, dbbt, dbbt_data_page);
1153
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001154dbbt_data_page_err:
1155 kfree(dbbt_data_page);
1156dbbt_page_err:
1157 kfree(dbbt_page);
1158fcb_err:
1159 kfree(fcb);
1160
1161 if (ret < 0) {
1162 printf("failed to write FCB/DBBT\n");
1163 return CMD_RET_FAILURE;
1164 }
1165
1166 return CMD_RET_SUCCESS;
1167}
1168
Alice Guo0b103372020-05-05 22:04:01 +08001169/* dump data which is read from NAND chip */
Han Xu214b7d52020-05-05 22:04:03 +08001170void dump_structure(struct boot_config *boot_cfg, struct fcb_block *fcb,
1171 struct dbbt_block *dbbt, void *dbbt_data_page)
Alice Guo0b103372020-05-05 22:04:01 +08001172{
Han Xu214b7d52020-05-05 22:04:03 +08001173 int i;
1174 struct mtd_info *mtd = boot_cfg->mtd;
1175
1176 #define P1(x) printf(" %s = 0x%08x\n", #x, fcb->x)
1177 printf("FCB\n");
Alice Guo0b103372020-05-05 22:04:01 +08001178 P1(checksum);
1179 P1(fingerprint);
1180 P1(version);
1181 #undef P1
Han Xu214b7d52020-05-05 22:04:03 +08001182 #define P1(x) printf(" %s = %d\n", #x, fcb->x)
Alice Guo0b103372020-05-05 22:04:01 +08001183 P1(datasetup);
1184 P1(datahold);
1185 P1(addr_setup);
1186 P1(dsample_time);
1187 P1(pagesize);
1188 P1(oob_pagesize);
1189 P1(sectors);
1190 P1(nr_nand);
1191 P1(nr_die);
1192 P1(celltype);
1193 P1(ecc_type);
1194 P1(ecc_nr);
1195 P1(ecc_size);
1196 P1(ecc_level);
1197 P1(meta_size);
1198 P1(nr_blocks);
1199 P1(ecc_type_sdk);
1200 P1(ecc_nr_sdk);
1201 P1(ecc_size_sdk);
1202 P1(ecc_level_sdk);
1203 P1(nr_blocks_sdk);
1204 P1(meta_size_sdk);
1205 P1(erase_th);
1206 P1(bootpatch);
1207 P1(patch_size);
1208 P1(fw1_start);
1209 P1(fw2_start);
1210 P1(fw1_pages);
1211 P1(fw2_pages);
1212 P1(dbbt_start);
1213 P1(bb_byte);
1214 P1(bb_start_bit);
1215 P1(phy_offset);
1216 P1(bchtype);
1217 P1(readlatency);
1218 P1(predelay);
1219 P1(cedelay);
1220 P1(postdelay);
1221 P1(cmdaddpause);
1222 P1(datapause);
1223 P1(tmspeed);
1224 P1(busytimeout);
1225 P1(disbbm);
1226 P1(spare_offset);
Han Xu214b7d52020-05-05 22:04:03 +08001227#if !defined(CONFIG_MX6) || defined(CONFIG_MX6SX) || \
1228 defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
Alice Guo0b103372020-05-05 22:04:01 +08001229 P1(onfi_sync_enable);
1230 P1(onfi_sync_speed);
1231 P1(onfi_sync_nand_data);
1232 P1(disbbm_search);
1233 P1(disbbm_search_limit);
1234 P1(read_retry_enable);
Han Xu214b7d52020-05-05 22:04:03 +08001235#endif
Alice Guo0b103372020-05-05 22:04:01 +08001236 #undef P1
Han Xu214b7d52020-05-05 22:04:03 +08001237 #define P1(x) printf(" %s = 0x%08x\n", #x, dbbt->x)
1238 printf("DBBT :\n");
Alice Guo0b103372020-05-05 22:04:01 +08001239 P1(checksum);
1240 P1(fingerprint);
1241 P1(version);
1242 #undef P1
Han Xu214b7d52020-05-05 22:04:03 +08001243 #define P1(x) printf(" %s = %d\n", #x, dbbt->x)
1244 P1(dbbtpages);
Alice Guo0b103372020-05-05 22:04:01 +08001245 #undef P1
1246
Han Xu214b7d52020-05-05 22:04:03 +08001247 for (i = 0; i < dbbt->dbbtpages; ++i)
1248 printf("%d ", *((u32 *)(dbbt_data_page + i)));
1249
1250 if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
1251 printf("Firmware: image #0 @ 0x%x size 0x%x\n",
1252 fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
1253 printf("Firmware: image #1 @ 0x%x size 0x%x\n",
1254 fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
1255 } else {
1256 printf("Firmware: image #0 @ 0x%x size 0x%x\n",
1257 fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
1258 printf("Firmware: image #1 @ 0x%x size 0x%x\n",
1259 fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
1260 /* TODO: Add extra image information */
Alice Guo0b103372020-05-05 22:04:01 +08001261 }
1262}
1263
Han Xu214b7d52020-05-05 22:04:03 +08001264static bool check_fingerprint(void *data, int fingerprint)
1265{
1266 int off = 4;
1267
1268 return (*(int *)(data + off) == fingerprint);
1269}
1270
Han Xuf797fe82020-05-05 22:04:04 +08001271static int fuse_to_search_count(u32 bank, u32 word, u32 mask, u32 off)
1272{
1273 int err;
1274 u32 val;
1275 int ret;
1276
1277 /* by default, the boot search count from fuse should be 2 */
1278 err = fuse_read(bank, word, &val);
1279 if (err)
1280 return 2;
1281
1282 val = (val & mask) >> off;
1283
1284 switch (val) {
1285 case 0:
1286 ret = 2;
1287 break;
1288 case 1:
1289 case 2:
1290 case 3:
1291 ret = 1 << val;
1292 break;
1293 default:
1294 ret = 2;
1295 }
1296
1297 return ret;
1298}
1299
Han Xu214b7d52020-05-05 22:04:03 +08001300static int nandbcb_dump(struct boot_config *boot_cfg)
1301{
1302 int i;
1303 loff_t off;
1304 struct mtd_info *mtd = boot_cfg->mtd;
1305 struct fcb_block fcb, fcb_copy;
1306 struct dbbt_block dbbt, dbbt_copy;
1307 void *dbbt_data_page, *dbbt_data_page_copy;
1308 bool fcb_not_found, dbbt_not_found;
1309 int ret = 0;
1310
1311 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
1312 if (!dbbt_data_page) {
1313 printf("failed to allocate dbbt_data_page\n");
1314 ret = -ENOMEM;
1315 return ret;
1316 }
1317
1318 dbbt_data_page_copy = kzalloc(mtd->writesize, GFP_KERNEL);
1319 if (!dbbt_data_page_copy) {
1320 printf("failed to allocate dbbt_data_page\n");
1321 ret = -ENOMEM;
1322 goto dbbt_page_err;
1323 }
1324
1325 /* read fcb */
1326 fcb_not_found = 1;
1327 off = 0;
1328 for (i = 0; i < g_boot_search_count; ++i) {
1329 if (fcb_not_found) {
1330 ret = read_fcb(boot_cfg, &fcb, off);
1331
1332 if (ret < 0)
1333 goto dbbt_page_copy_err;
1334 else if (ret == 1)
1335 continue;
1336 else if (ret == 0)
1337 if (check_fingerprint(&fcb, FCB_FINGERPRINT))
1338 fcb_not_found = 0;
1339 } else {
1340 ret = read_fcb(boot_cfg, &fcb_copy, off);
1341
1342 if (ret < 0)
1343 goto dbbt_page_copy_err;
1344 if (memcmp(&fcb, &fcb_copy,
1345 sizeof(struct fcb_block))) {
1346 printf("FCB copies are not identical\n");
1347 ret = -EINVAL;
1348 goto dbbt_page_copy_err;
1349 }
1350 }
1351
1352 /* next read location */
1353 off += g_boot_search_stride;
1354 }
1355
1356 /* read dbbt*/
1357 dbbt_not_found = 1;
1358 off = boot_cfg->search_area_size_in_bytes;
1359 for (i = 0; i < g_boot_search_count; ++i) {
1360 if (dbbt_not_found) {
1361 ret = read_dbbt(boot_cfg, &dbbt, dbbt_data_page, off);
1362
1363 if (ret < 0)
1364 goto dbbt_page_copy_err;
1365 else if (ret == 1)
1366 continue;
1367 else if (ret == 0)
1368 if (check_fingerprint(&dbbt, DBBT_FINGERPRINT))
1369 dbbt_not_found = 0;
1370 } else {
1371 ret = read_dbbt(boot_cfg, &dbbt_copy,
1372 dbbt_data_page_copy, off);
1373
1374 if (ret < 0)
1375 goto dbbt_page_copy_err;
1376 if (memcmp(&dbbt, &dbbt_copy,
1377 sizeof(struct dbbt_block))) {
1378 printf("DBBT copies are not identical\n");
1379 ret = -EINVAL;
1380 goto dbbt_page_copy_err;
1381 }
1382 if (dbbt.dbbtpages > 0 &&
1383 memcmp(dbbt_data_page, dbbt_data_page_copy,
1384 mtd->writesize)) {
1385 printf("DBBT data copies are not identical\n");
1386 ret = -EINVAL;
1387 goto dbbt_page_copy_err;
1388 }
1389 }
1390
1391 /* next read location */
1392 off += g_boot_search_stride;
1393 }
1394
1395 dump_structure(boot_cfg, &fcb, &dbbt, dbbt_data_page);
1396
1397dbbt_page_copy_err:
1398 kfree(dbbt_data_page_copy);
1399dbbt_page_err:
1400 kfree(dbbt_data_page);
1401
1402 return ret;
1403}
1404
Alice Guo0b103372020-05-05 22:04:01 +08001405static int do_nandbcb_dump(int argc, char * const argv[])
1406{
Han Xu214b7d52020-05-05 22:04:03 +08001407 struct boot_config cfg;
1408 int ret;
Alice Guo0b103372020-05-05 22:04:01 +08001409
1410 if (argc != 2)
1411 return CMD_RET_USAGE;
1412
Han Xu214b7d52020-05-05 22:04:03 +08001413 memset(&cfg, 0, sizeof(struct boot_config));
1414 if (nandbcb_get_info(argc, argv, &cfg))
1415 return CMD_RET_FAILURE;
Alice Guo0b103372020-05-05 22:04:01 +08001416
Han Xu214b7d52020-05-05 22:04:03 +08001417 if (nandbcb_get_size(argc, argv, 1, &cfg))
1418 return CMD_RET_FAILURE;
Alice Guo0b103372020-05-05 22:04:01 +08001419
Han Xu214b7d52020-05-05 22:04:03 +08001420 if (nandbcb_set_boot_config(argc, argv, &cfg))
1421 return CMD_RET_FAILURE;
Alice Guo0b103372020-05-05 22:04:01 +08001422
Han Xu214b7d52020-05-05 22:04:03 +08001423 ret = nandbcb_dump(&cfg);
1424 if (ret)
1425 return ret;
Alice Guo0b103372020-05-05 22:04:01 +08001426
Han Xu214b7d52020-05-05 22:04:03 +08001427 return ret;
Alice Guo0b103372020-05-05 22:04:01 +08001428}
1429
Han Xu214b7d52020-05-05 22:04:03 +08001430static int do_nandbcb_init(int argc, char * const argv[])
Shyam Saini1d43e242019-06-14 13:05:33 +05301431{
Shyam Saini1d43e242019-06-14 13:05:33 +05301432 u_char *buf;
Han Xu214b7d52020-05-05 22:04:03 +08001433 size_t size;
1434 loff_t addr;
1435 char *endp;
Shyam Saini1d43e242019-06-14 13:05:33 +05301436 int ret;
Han Xu214b7d52020-05-05 22:04:03 +08001437 struct boot_config cfg;
Shyam Saini1d43e242019-06-14 13:05:33 +05301438
1439 if (argc != 4)
1440 return CMD_RET_USAGE;
1441
Han Xu214b7d52020-05-05 22:04:03 +08001442 memset(&cfg, 0, sizeof(struct boot_config));
1443 if (nandbcb_get_info(argc, argv, &cfg))
Shyam Saini1d43e242019-06-14 13:05:33 +05301444 return CMD_RET_FAILURE;
Han Xu214b7d52020-05-05 22:04:03 +08001445
1446 if (nandbcb_get_size(argc, argv, 2, &cfg))
1447 return CMD_RET_FAILURE;
1448 size = cfg.boot_stream1_size;
1449
1450 if (nandbcb_set_boot_config(argc, argv, &cfg))
1451 return CMD_RET_FAILURE;
Shyam Saini1d43e242019-06-14 13:05:33 +05301452
1453 addr = simple_strtoul(argv[1], &endp, 16);
1454 if (*argv[1] == 0 || *endp != 0)
1455 return CMD_RET_FAILURE;
1456
Shyam Saini1d43e242019-06-14 13:05:33 +05301457 buf = map_physmem(addr, size, MAP_WRBACK);
1458 if (!buf) {
1459 puts("failed to map physical memory\n");
1460 return CMD_RET_FAILURE;
1461 }
1462
Han Xu214b7d52020-05-05 22:04:03 +08001463 ret = nandbcb_init(&cfg, buf);
Shyam Saini1d43e242019-06-14 13:05:33 +05301464
1465 return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
1466}
1467
Simon Glass09140112020-05-10 11:40:03 -06001468static int do_nandbcb(struct cmd_tbl *cmdtp, int flag, int argc,
1469 char *const argv[])
Shyam Saini1d43e242019-06-14 13:05:33 +05301470{
1471 const char *cmd;
1472 int ret = 0;
1473
Alice Guo0b103372020-05-05 22:04:01 +08001474 if (argc < 3)
Shyam Saini1d43e242019-06-14 13:05:33 +05301475 goto usage;
1476
Han Xu214b7d52020-05-05 22:04:03 +08001477 /* check the platform config first */
1478 if (is_mx6sx()) {
1479 plat_config = imx6sx_plat_config;
1480 } else if (is_mx7()) {
1481 plat_config = imx7d_plat_config;
1482 } else if (is_mx6ul() || is_mx6ull()) {
1483 plat_config = imx6ul_plat_config;
1484 } else if (is_mx6() && !is_mx6sx() && !is_mx6ul() && !is_mx6ull()) {
1485 plat_config = imx6qdl_plat_config;
1486 } else if (is_imx8mq()) {
1487 plat_config = imx8mq_plat_config;
1488 } else if (is_imx8mm()) {
1489 plat_config = imx8mm_plat_config;
Han Xu42a49752020-10-10 08:48:49 -05001490 } else if (is_imx8mn() || is_imx8mp()) {
Han Xu214b7d52020-05-05 22:04:03 +08001491 plat_config = imx8mn_plat_config;
1492 } else if (is_imx8qm() || is_imx8qxp()) {
1493 plat_config = imx8q_plat_config;
1494 } else {
1495 printf("ERROR: Unknown platform\n");
1496 return CMD_RET_FAILURE;
1497 }
1498
Han Xu42a49752020-10-10 08:48:49 -05001499 if ((plat_config.misc_flags) & BT_SEARCH_CNT_FROM_FUSE) {
1500 if (is_imx8qxp())
1501 g_boot_search_count = fuse_to_search_count(0, 720, 0xc0, 6);
1502 if (is_imx8mn() || is_imx8mp())
1503 g_boot_search_count = fuse_to_search_count(2, 2, 0x6000, 13);
1504 printf("search count set to %d from fuse\n",
1505 g_boot_search_count);
Han Xuf797fe82020-05-05 22:04:04 +08001506 }
Han Xu214b7d52020-05-05 22:04:03 +08001507
Shyam Saini1d43e242019-06-14 13:05:33 +05301508 cmd = argv[1];
1509 --argc;
1510 ++argv;
1511
Han Xu214b7d52020-05-05 22:04:03 +08001512 if (strcmp(cmd, "init") == 0) {
1513 ret = do_nandbcb_init(argc, argv);
Shyam Saini1d43e242019-06-14 13:05:33 +05301514 goto done;
1515 }
1516
Alice Guo0b103372020-05-05 22:04:01 +08001517 if (strcmp(cmd, "dump") == 0) {
1518 ret = do_nandbcb_dump(argc, argv);
1519 goto done;
1520 }
1521
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001522 if (strcmp(cmd, "bcbonly") == 0) {
1523 ret = do_nandbcb_bcbonly(argc, argv);
1524 goto done;
1525 }
1526
Shyam Saini1d43e242019-06-14 13:05:33 +05301527done:
1528 if (ret != -1)
1529 return ret;
1530usage:
1531 return CMD_RET_USAGE;
1532}
1533
Parthiban Nallathambi4ee0ff12019-08-23 18:35:10 +02001534#ifdef CONFIG_SYS_LONGHELP
Shyam Saini1d43e242019-06-14 13:05:33 +05301535static char nandbcb_help_text[] =
Han Xu214b7d52020-05-05 22:04:03 +08001536 "init addr off|partition len - update 'len' bytes starting at\n"
Igor Opaniukae8a53e2019-11-03 16:49:46 +01001537 " 'off|part' to memory address 'addr', skipping bad blocks\n"
Han Xu214b7d52020-05-05 22:04:03 +08001538 "nandbcb bcbonly off|partition fw1-off fw1-size [fw2-off fw2-size]\n"
1539 " - write BCB only (FCB and DBBT)\n"
1540 " where `fwx-size` is fw sizes in bytes, `fw1-off`\n"
Igor Opaniuk061b63b2019-12-16 14:06:44 +02001541 " and `fw2-off` - firmware offsets\n"
1542 " FIY, BCB isn't erased automatically, so mtd erase should\n"
1543 " be called in advance before writing new BCB:\n"
Alice Guo0b103372020-05-05 22:04:01 +08001544 " > mtd erase mx7-bcb\n"
Han Xu214b7d52020-05-05 22:04:03 +08001545 "nandbcb dump off|partition - dump/verify boot structures\n";
Parthiban Nallathambi4ee0ff12019-08-23 18:35:10 +02001546#endif
Shyam Saini1d43e242019-06-14 13:05:33 +05301547
Han Xu214b7d52020-05-05 22:04:03 +08001548U_BOOT_CMD(nandbcb, 7, 1, do_nandbcb,
1549 "i.MX NAND Boot Control Blocks write",
Shyam Saini1d43e242019-06-14 13:05:33 +05301550 nandbcb_help_text
1551);