2 * i.MX6 nand boot control block(bcb).
4 * Based on the common/imx-bbu-nand-fcb.c from barebox and imx kobs-ng
6 * Copyright (C) 2017 Jagan Teki <jagan@amarulasolutions.com>
7 * Copyright (C) 2016 Sergey Kubushyn <ksi@koi8.net>
9 * SPDX-License-Identifier: GPL-2.0+
15 #include <dm/devres.h>
18 #include <jffs2/jffs2.h>
19 #include <linux/bch.h>
20 #include <linux/mtd/mtd.h>
22 #include <asm/arch/sys_proto.h>
23 #include <asm/mach-imx/imx-nandbcb.h>
24 #include <asm/mach-imx/imximage.cfg>
26 #include <linux/mtd/mtd.h>
29 #include "../../../cmd/legacy-mtd-utils.h"
31 #define BF_VAL(v, bf) (((v) & bf##_MASK) >> bf##_OFFSET)
32 #define GETBIT(v, n) (((v) >> (n)) & 0x1)
34 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
35 static uint8_t reverse_bit(uint8_t b)
37 b = (b & 0xf0) >> 4 | (b & 0x0f) << 4;
38 b = (b & 0xcc) >> 2 | (b & 0x33) << 2;
39 b = (b & 0xaa) >> 1 | (b & 0x55) << 1;
44 static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits)
49 int ecc_buf_size = (m * eccbits + 7) / 8;
50 struct bch_control *bch = init_bch(m, eccbits, 0);
51 u8 *ecc_buf = kzalloc(ecc_buf_size, GFP_KERNEL);
52 u8 *tmp_buf = kzalloc(blocksize * numblocks, GFP_KERNEL);
56 * The blocks here are bit aligned. If eccbits is a multiple of 8,
57 * we just can copy bytes. Otherwiese we must move the blocks to
58 * the next free bit position.
62 memcpy(tmp_buf, fcb, sizeof(*fcb));
64 for (i = 0; i < numblocks; i++) {
65 memset(ecc_buf, 0, ecc_buf_size);
66 psrc = tmp_buf + i * blocksize;
67 pdst = buf + i * (blocksize + ecc_buf_size);
69 /* copy data byte aligned to destination buf */
70 memcpy(pdst, psrc, blocksize);
73 * imx-kobs use a modified encode_bch which reverse the
74 * bit order of the data before calculating bch.
75 * Do this in the buffer and use the bch lib here.
77 for (j = 0; j < blocksize; j++)
78 psrc[j] = reverse_bit(psrc[j]);
80 encode_bch(bch, psrc, blocksize, ecc_buf);
83 for (j = 0; j < ecc_buf_size; j++)
84 ecc_buf[j] = reverse_bit(ecc_buf[j]);
86 /* Here eccbuf is byte aligned and we can just copy it */
87 memcpy(pdst + blocksize, ecc_buf, ecc_buf_size);
96 static u8 calculate_parity_13_8(u8 d)
100 p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0;
101 p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^
103 p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^
105 p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3;
106 p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^
107 GETBIT(d, 1) ^ GETBIT(d, 0)) << 4;
112 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
118 for (i = 0; i < size; i++)
119 ecc[i] = calculate_parity_13_8(src[i]);
123 static u32 calc_chksum(void *buf, size_t size)
129 for (i = 0; i < size; i++)
135 static void fill_fcb(struct fcb_block *fcb, struct mtd_info *mtd,
136 u32 fw1_start, u32 fw2_start, u32 fw_pages)
138 struct nand_chip *chip = mtd_to_nand(mtd);
139 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
140 struct mxs_nand_layout l;
142 mxs_nand_get_layout(mtd, &l);
144 fcb->fingerprint = FCB_FINGERPRINT;
145 fcb->version = FCB_VERSION_1;
147 fcb->pagesize = mtd->writesize;
148 fcb->oob_pagesize = mtd->writesize + mtd->oobsize;
149 fcb->sectors = mtd->erasesize / mtd->writesize;
151 fcb->meta_size = l.meta_size;
152 fcb->nr_blocks = l.nblocks;
153 fcb->ecc_nr = l.data0_size;
154 fcb->ecc_level = l.ecc0;
155 fcb->ecc_size = l.datan_size;
156 fcb->ecc_type = l.eccn;
158 /* Also hardcoded in kobs-ng */
162 fcb->addr_setup = 25;
163 fcb->dsample_time = 6;
164 } else if (is_mx7()) {
167 fcb->addr_setup = 15;
168 fcb->dsample_time = 6;
171 /* DBBT search area starts at second page on first block */
174 fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset;
175 fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset;
177 fcb->phy_offset = mtd->writesize;
179 fcb->nr_blocks = mtd->writesize / fcb->ecc_nr - 1;
182 fcb->disbbm_search = 0;
184 fcb->fw1_start = fw1_start; /* Firmware image starts on this sector */
185 fcb->fw2_start = fw2_start; /* Secondary FW Image starting Sector */
186 fcb->fw1_pages = fw_pages; /* Number of sectors in firmware image */
187 fcb->fw2_pages = fw_pages; /* Number of sector in secondary FW image */
189 fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
192 static int dbbt_fill_data(struct mtd_info *mtd, void *buf, int num_blocks)
194 int n, n_bad_blocks = 0;
196 u32 *n_bad_blocksp = buf + 0x4;
198 for (n = 0; n < num_blocks; n++) {
199 loff_t offset = n * mtd->erasesize;
200 if (mtd_block_isbad(mtd, offset)) {
207 *n_bad_blocksp = n_bad_blocks;
212 static int write_fcb_dbbt(struct mtd_info *mtd, struct fcb_block *fcb,
213 struct dbbt_block *dbbt, void *dbbt_data_page,
216 void *fcb_raw_page = 0;
221 * We prepare raw page only for i.MX6, for i.MX7 we
222 * leverage BCH hw module instead
226 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize,
229 debug("failed to allocate fcb_raw_page\n");
234 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
235 /* 40 bit BCH, for i.MX6UL(L) */
236 encode_bch_ecc(fcb_raw_page + 32, fcb, 40);
238 memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block));
239 encode_hamming_13_8(fcb_raw_page + 12,
240 fcb_raw_page + 12 + 512, 512);
243 * Set the first and second byte of OOB data to 0xFF,
244 * not 0x00. These bytes are used as the Manufacturers Bad
245 * Block Marker (MBBM). Since the FCB is mostly written to
246 * the first page in a block, a scan for
247 * factory bad blocks will detect these blocks as bad, e.g.
248 * when function nand_scan_bbt() is executed to build a new
251 memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
253 for (i = 0; i < 2; i++) {
254 if (mtd_block_isbad(mtd, off)) {
255 printf("Block %d is bad, skipped\n", i);
260 * User BCH ECC hardware module for i.MX7
263 u32 off = i * mtd->erasesize;
264 size_t rwsize = sizeof(*fcb);
266 printf("Writing %d bytes to 0x%x: ", rwsize, off);
268 /* switch nand BCH to FCB compatible settings */
269 mxs_nand_mode_fcb(mtd);
270 ret = nand_write(mtd, off, &rwsize,
271 (unsigned char *)fcb);
272 mxs_nand_mode_normal(mtd);
274 printf("%s\n", ret ? "ERROR" : "OK");
275 } else if (is_mx6()) {
277 mtd_oob_ops_t ops = {
278 .datbuf = (u8 *)fcb_raw_page,
279 .oobbuf = ((u8 *)fcb_raw_page) +
281 .len = mtd->writesize,
282 .ooblen = mtd->oobsize,
286 ret = mtd_write_oob(mtd, mtd->erasesize * i, &ops);
288 goto fcb_raw_page_err;
289 debug("NAND fcb write: 0x%x offset 0x%x written: %s\n",
290 mtd->erasesize * i, ops.len, ret ?
294 ret = mtd_write(mtd, mtd->erasesize * i + mtd->writesize,
295 mtd->writesize, &dummy, (void *)dbbt);
297 goto fcb_raw_page_err;
298 debug("NAND dbbt write: 0x%x offset, 0x%x bytes written: %s\n",
299 mtd->erasesize * i + mtd->writesize, dummy,
300 ret ? "ERROR" : "OK");
302 /* dbbtpages == 0 if no bad blocks */
303 if (dbbt->dbbtpages > 0) {
304 loff_t to = (mtd->erasesize * i + mtd->writesize * 5);
306 ret = mtd_write(mtd, to, mtd->writesize, &dummy,
309 goto fcb_raw_page_err;
320 static int nandbcb_update(struct mtd_info *mtd, loff_t off, size_t size,
321 size_t maxsize, const u_char *buf)
323 nand_erase_options_t opts;
324 struct fcb_block *fcb;
325 struct dbbt_block *dbbt;
327 void *fwbuf, *dbbt_page, *dbbt_data_page;
328 u32 fw1_start, fw1_pages;
329 int nr_blks, nr_blks_fcb, fw1_blk;
334 memset(&opts, 0, sizeof(opts));
336 opts.length = maxsize - 1;
337 ret = nand_erase_opts(mtd, &opts);
339 printf("%s: erase failed (ret = %d)\n", __func__, ret);
344 * Reference documentation from i.MX6DQRM section 8.5.2.2
346 * Nand Boot Control Block(BCB) contains two data structures,
347 * - Firmware Configuration Block(FCB)
348 * - Discovered Bad Block Table(DBBT)
352 * - DBBT search page address,
353 * - start page address of primary firmware
354 * - start page address of secondary firmware
357 * - number of blocks = mtd partition size / mtd erasesize
358 * - two firmware blocks, primary and secondary
359 * - first 4 block for FCB/DBBT
360 * - rest split in half for primary and secondary firmware
361 * - same firmware will write two times
364 nr_blks = maxsize / mtd->erasesize;
365 fw1_blk = nr_blks_fcb;
368 fwsize = ALIGN(size + FLASH_OFFSET_STANDARD + mtd->writesize,
370 fwbuf = kzalloc(fwsize, GFP_KERNEL);
372 debug("failed to allocate fwbuf\n");
377 memcpy(fwbuf + FLASH_OFFSET_STANDARD, buf, size);
378 fw1_off = fw1_blk * mtd->erasesize;
379 ret = nand_write_skip_bad(mtd, fw1_off, &fwsize, NULL, maxsize,
380 (u_char *)fwbuf, WITH_WR_VERIFY);
381 printf("NAND fw write: 0x%llx offset, 0x%x bytes written: %s\n",
382 fw1_off, fwsize, ret ? "ERROR" : "OK");
387 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
389 debug("failed to allocate fcb\n");
394 fw1_start = (fw1_blk * mtd->erasesize) / mtd->writesize;
395 fw1_pages = size / mtd->writesize + 1;
396 fill_fcb(fcb, mtd, fw1_start, 0, fw1_pages);
399 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
401 debug("failed to allocate dbbt_page\n");
406 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
407 if (!dbbt_data_page) {
408 debug("failed to allocate dbbt_data_page\n");
415 dbbt->fingerprint = DBBT_FINGERPRINT2;
416 dbbt->version = DBBT_VERSION_1;
417 ret = dbbt_fill_data(mtd, dbbt_data_page, nr_blks);
419 goto dbbt_data_page_err;
423 /* write fcb and dbbt to nand */
424 ret = write_fcb_dbbt(mtd, fcb, dbbt, dbbt_data_page, off);
426 printf("failed to write FCB/DBBT\n");
429 kfree(dbbt_data_page);
440 static int do_nandbcb_bcbonly(int argc, char * const argv[])
442 struct fcb_block *fcb;
443 struct dbbt_block *dbbt;
444 u32 fw_len, fw1_off, fw2_off;
445 struct mtd_info *mtd;
446 void *dbbt_page, *dbbt_data_page;
449 dev = nand_curr_device;
450 if ((dev < 0) || (dev >= CONFIG_SYS_MAX_NAND_DEVICE) ||
451 (!get_nand_dev_by_index(dev))) {
452 puts("No devices available\n");
453 return CMD_RET_FAILURE;
456 mtd = get_nand_dev_by_index(dev);
459 return CMD_RET_FAILURE;
461 fw_len = simple_strtoul(argv[1], NULL, 16);
462 fw1_off = simple_strtoul(argv[2], NULL, 16);
465 fw2_off = simple_strtoul(argv[3], NULL, 16);
470 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
472 debug("failed to allocate fcb\n");
474 return CMD_RET_FAILURE;
477 fill_fcb(fcb, mtd, fw1_off / mtd->writesize,
478 fw2_off / mtd->writesize, fw_len / mtd->writesize);
481 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
483 debug("failed to allocate dbbt_page\n");
488 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
489 if (!dbbt_data_page) {
490 debug("failed to allocate dbbt_data_page\n");
497 dbbt->fingerprint = DBBT_FINGERPRINT2;
498 dbbt->version = DBBT_VERSION_1;
499 ret = dbbt_fill_data(mtd, dbbt_data_page, 0);
501 goto dbbt_data_page_err;
505 /* write fcb and dbbt to nand */
506 ret = write_fcb_dbbt(mtd, fcb, dbbt, dbbt_data_page, 0);
508 kfree(dbbt_data_page);
515 printf("failed to write FCB/DBBT\n");
516 return CMD_RET_FAILURE;
519 return CMD_RET_SUCCESS;
522 static int do_nandbcb_update(int argc, char * const argv[])
524 struct mtd_info *mtd;
525 loff_t addr, offset, size, maxsize;
532 return CMD_RET_USAGE;
534 dev = nand_curr_device;
536 printf("failed to get nand_curr_device, run nand device\n");
537 return CMD_RET_FAILURE;
540 addr = simple_strtoul(argv[1], &endp, 16);
541 if (*argv[1] == 0 || *endp != 0)
542 return CMD_RET_FAILURE;
544 mtd = get_nand_dev_by_index(dev);
545 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &offset, &size,
546 &maxsize, MTD_DEV_TYPE_NAND, mtd->size))
547 return CMD_RET_FAILURE;
549 buf = map_physmem(addr, size, MAP_WRBACK);
551 puts("failed to map physical memory\n");
552 return CMD_RET_FAILURE;
555 ret = nandbcb_update(mtd, offset, size, maxsize, buf);
557 return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
560 static int do_nandbcb(cmd_tbl_t *cmdtp, int flag, int argc,
573 if (strcmp(cmd, "update") == 0) {
574 ret = do_nandbcb_update(argc, argv);
578 if (strcmp(cmd, "bcbonly") == 0) {
579 ret = do_nandbcb_bcbonly(argc, argv);
587 return CMD_RET_USAGE;
590 #ifdef CONFIG_SYS_LONGHELP
591 static char nandbcb_help_text[] =
592 "update addr off|partition len - update 'len' bytes starting at\n"
593 " 'off|part' to memory address 'addr', skipping bad blocks\n"
594 "bcbonly fw-size fw1-off [fw2-off] - write only BCB (FCB and DBBT)\n"
595 " where `fw-size` is fw sizes in bytes, `fw1-off`\n"
596 " and `fw2-off` - firmware offsets\n"
597 " FIY, BCB isn't erased automatically, so mtd erase should\n"
598 " be called in advance before writing new BCB:\n"
599 " > mtd erase mx7-bcb";
602 U_BOOT_CMD(nandbcb, 5, 1, do_nandbcb,
603 "i.MX6/i.MX7 NAND Boot Control Blocks write",