2 * Copyright (C) 2012,2013 Lothar Waßmann <LW@KARO-electronics.de>
4 * See file CREDITS for list of people who contributed to this
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
23 #include <linux/err.h>
26 #include <asm/sizes.h>
27 #include <asm/arch/imx-regs.h>
28 #include <asm/imx-common/regs-gpmi.h>
29 #include <asm/imx-common/regs-bch.h>
31 #if CONFIG_SYS_NAND_U_BOOT_OFFS < 0x20000
32 #error CONFIG_SYS_NAND_U_BOOT_OFFS must be >= 128kIB
35 struct mx6_nand_timing {
50 struct mx6_nand_timing timing;
53 u32 sectors_per_block;
54 u32 number_of_nands; /* not used by ROM code */
55 u32 total_internal_die; /* not used by ROM code */
56 u32 cell_type; /* not used by ROM code */
62 u32 ecc_blocks_per_page;
63 u32 rsrvd1[6]; /* not used by ROM code */
64 u32 bch_mode; /* erase_threshold */
73 u32 bb_mark_phys_offset;
75 u32 rsrvd3[8]; /* Toggle NAND timing parameters */
77 u32 bb_mark_spare_offset;
78 u32 rsrvd4[9]; /* ONFI NAND parameters */
82 struct mx6_dbbt_header {
97 #define BF_VAL(v, bf) (((v) & bf##_MASK) >> bf##_OFFSET)
99 static nand_info_t *mtd = &nand_info[0];
103 #define BIT(v,n) (((v) >> (n)) & 0x1)
105 static inline void memdump(const void *addr, size_t len)
107 const char *buf = addr;
110 for (i = 0; i < len; i++) {
114 printf("%p:", &buf[i]);
116 printf(" %02x", buf[i]);
121 static u8 calculate_parity_13_8(u8 d)
125 p |= (BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 3) ^ BIT(d, 2)) << 0;
126 p |= (BIT(d, 7) ^ BIT(d, 5) ^ BIT(d, 4) ^ BIT(d, 2) ^ BIT(d, 1)) << 1;
127 p |= (BIT(d, 7) ^ BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 1) ^ BIT(d, 0)) << 2;
128 p |= (BIT(d, 7) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 0)) << 3;
129 p |= (BIT(d, 6) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 2) ^ BIT(d, 1) ^ BIT(d, 0)) << 4;
133 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
139 for (i = 0; i < size; i++)
140 ecc[i] = calculate_parity_13_8(src[i]);
143 static u32 calc_chksum(void *buf, size_t size)
149 for (i = 0; i < size; i++) {
156 Physical organisation of data in NAND flash:
158 payload chunk 0 (may be empty)
159 ecc for metadata + payload chunk 0
161 ecc for payload chunk 1
164 ecc for payload chunk n
167 static int calc_bb_offset(nand_info_t *mtd, struct mx6_fcb *fcb)
170 int chunk_data_size = fcb->ecc_blockn_size * 8;
171 int chunk_ecc_size = (fcb->ecc_blockn_type << 1) * 13;
172 int chunk_total_size = chunk_data_size + chunk_ecc_size;
173 int bb_mark_chunk, bb_mark_chunk_offs;
175 bb_mark_offset = (mtd->writesize - fcb->metadata_size) * 8;
176 if (fcb->ecc_block0_size == 0)
177 bb_mark_offset -= (fcb->ecc_block0_type << 1) * 13;
179 bb_mark_chunk = bb_mark_offset / chunk_total_size;
180 bb_mark_chunk_offs = bb_mark_offset - (bb_mark_chunk * chunk_total_size);
181 if (bb_mark_chunk_offs > chunk_data_size) {
182 printf("Unsupported ECC layout; BB mark resides in ECC data: %u\n",
186 bb_mark_offset -= bb_mark_chunk * chunk_ecc_size;
187 return bb_mark_offset;
190 #define pr_fcb_val(p, n) debug("%s=%08x(%d)\n", #n, (p)->n, (p)->n)
192 static struct mx6_fcb *create_fcb(void *buf, int fw1_start_block,
193 int fw2_start_block, size_t fw_size)
195 struct gpmi_regs *gpmi_base = (void *)GPMI_BASE_ADDRESS;
196 struct bch_regs *bch_base = (void *)BCH_BASE_ADDRESS;
200 int bb_mark_bit_offs;
204 if (gpmi_base == NULL || bch_base == NULL) {
205 return ERR_PTR(-ENOMEM);
208 fl0 = readl(&bch_base->hw_bch_flash0layout0);
209 fl1 = readl(&bch_base->hw_bch_flash0layout1);
210 t0 = readl(&gpmi_base->hw_gpmi_timing0);
212 metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
214 fcb = buf + ALIGN(metadata_size, 4);
215 fcb_offs = (void *)fcb - buf;
217 memset(buf, 0xff, fcb_offs);
218 memset(fcb, 0x00, sizeof(*fcb));
219 memset(fcb + 1, 0xff, mtd->erasesize - fcb_offs - sizeof(*fcb));
221 strncpy((char *)&fcb->fingerprint, "FCB ", 4);
222 fcb->version = cpu_to_be32(1);
224 fcb->disbb_search = 1;
227 /* ROM code assumes GPMI clock of 25 MHz */
228 fcb->timing.data_setup = BF_VAL(t0, GPMI_TIMING0_DATA_SETUP) * 40;
229 fcb->timing.data_hold = BF_VAL(t0, GPMI_TIMING0_DATA_HOLD) * 40;
230 fcb->timing.address_setup = BF_VAL(t0, GPMI_TIMING0_ADDRESS_SETUP) * 40;
232 fcb->page_data_size = mtd->writesize;
233 fcb->total_page_size = mtd->writesize + mtd->oobsize;
234 fcb->sectors_per_block = mtd->erasesize / mtd->writesize;
236 fcb->ecc_block0_type = BF_VAL(fl0, BCH_FLASHLAYOUT0_ECC0);
237 fcb->ecc_block0_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_DATA0_SIZE) * 4;
238 fcb->ecc_blockn_type = BF_VAL(fl1, BCH_FLASHLAYOUT1_ECCN);
239 fcb->ecc_blockn_size = BF_VAL(fl1, BCH_FLASHLAYOUT1_DATAN_SIZE) * 4;
241 pr_fcb_val(fcb, ecc_block0_type);
242 pr_fcb_val(fcb, ecc_blockn_type);
243 pr_fcb_val(fcb, ecc_block0_size);
244 pr_fcb_val(fcb, ecc_blockn_size);
246 fcb->metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
247 fcb->ecc_blocks_per_page = BF_VAL(fl0, BCH_FLASHLAYOUT0_NBLOCKS);
248 fcb->bch_mode = readl(&bch_base->hw_bch_mode);
249 fcb->bch_type = 0; /* BCH20 */
251 fcb->fw1_start_page = fw1_start_block * mtd->erasesize / mtd->writesize;
252 fcb->fw1_sectors = DIV_ROUND_UP(fw_size, mtd->writesize);
254 if (fw2_start_block != 0 && fw2_start_block < mtd->size / mtd->erasesize) {
255 fcb->fw2_start_page = fw2_start_block * mtd->erasesize / mtd->writesize;
256 fcb->fw2_sectors = fcb->fw1_sectors;
259 fcb->dbbt_search_area = 1;
261 bb_mark_bit_offs = calc_bb_offset(mtd, fcb);
262 if (bb_mark_bit_offs < 0)
263 return ERR_PTR(bb_mark_bit_offs);
264 fcb->bb_mark_byte = bb_mark_bit_offs / 8;
265 fcb->bb_mark_startbit = bb_mark_bit_offs % 8;
266 fcb->bb_mark_phys_offset = mtd->writesize;
268 pr_fcb_val(fcb, bb_mark_byte);
269 pr_fcb_val(fcb, bb_mark_startbit);
270 pr_fcb_val(fcb, bb_mark_phys_offset);
272 fcb->checksum = calc_chksum(&fcb->fingerprint, 512 - 4);
276 static int find_fcb(void *ref, int page)
279 struct nand_chip *chip = mtd->priv;
280 void *buf = malloc(mtd->erasesize);
285 chip->select_chip(mtd, 0);
286 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
287 ret = chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
289 printf("Failed to read FCB from page %u: %d\n", page, ret);
292 chip->select_chip(mtd, -1);
293 if (memcmp(buf, ref, mtd->writesize) == 0) {
294 debug("Found FCB in page %u (%08x)\n",
295 page, page * mtd->writesize);
302 static int write_fcb(void *buf, int block)
305 struct nand_chip *chip = mtd->priv;
306 int page = block * mtd->erasesize / mtd->writesize;
308 ret = find_fcb(buf, page);
310 printf("FCB at block %d is up to date\n", block);
314 ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
316 printf("Failed to erase FCB block %u\n", block);
320 printf("Writing FCB to block %d @ %08x\n", block,
321 block * mtd->erasesize);
322 chip->select_chip(mtd, 0);
323 ret = chip->write_page(mtd, chip, buf, 1, page, 0, 1);
325 printf("Failed to write FCB to block %u: %d\n", block, ret);
327 chip->select_chip(mtd, -1);
342 struct mx6_boot_data {
348 static size_t count_good_blocks(int start, int end)
350 size_t max_len = (end - start + 1);
353 for (block = start; block <= end; block++) {
354 if (nand_block_isbad(mtd, block * mtd->erasesize))
360 static int find_ivt(void *buf)
362 struct mx6_ivt *ivt_hdr = buf + 0x400;
364 if ((ivt_hdr->magic & 0xff0000ff) != 0x400000d1)
370 static inline void *reloc(void *dst, void *base, void *ptr)
372 return dst + (ptr - base);
375 static int patch_ivt(void *buf, size_t fsize)
377 struct mx6_ivt *ivt_hdr = buf + 0x400;
378 struct mx6_boot_data *boot_data;
380 if (!find_ivt(buf)) {
381 printf("No IVT found in image at %p\n", buf);
384 boot_data = reloc(ivt_hdr, ivt_hdr->self, ivt_hdr->boot_data);
385 boot_data->length = fsize;
390 #define chk_overlap(a,b) \
391 ((a##_start_block <= b##_end_block && \
392 a##_end_block >= b##_start_block) || \
393 (b##_start_block <= a##_end_block && \
394 b##_end_block >= a##_start_block))
396 #define fail_if_overlap(a,b,m1,m2) do { \
397 if (chk_overlap(a, b)) { \
398 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
399 m1, a##_start_block, a##_end_block, \
400 m2, b##_start_block, b##_end_block); \
405 #ifdef CONFIG_ENV_IS_IN_NAND
406 #ifndef CONFIG_ENV_OFFSET_REDUND
407 #define TOTAL_ENV_SIZE CONFIG_ENV_RANGE
409 #define TOTAL_ENV_SIZE (CONFIG_ENV_RANGE * 2)
413 #define pr_fcb_offset(n) printf("%s: %04x (%d)\n", #n, \
414 offsetof(struct mx6_fcb, n), offsetof(struct mx6_fcb, n))
416 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
419 const unsigned long fcb_start_block = 0, fcb_end_block = 0;
420 int erase_size = mtd->erasesize;
421 int page_size = mtd->writesize;
428 unsigned long mtd_num_blocks = mtd->size / mtd->erasesize;
429 #ifdef CONFIG_ENV_IS_IN_NAND
430 unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
431 unsigned long env_end_block = env_start_block +
432 DIV_ROUND_UP(TOTAL_ENV_SIZE, mtd->erasesize) - 1;
437 unsigned long fw1_start_block = 0, fw1_end_block;
438 unsigned long fw2_start_block = 0, fw2_end_block;
439 unsigned long fw_num_blocks;
440 unsigned long extra_blocks = 2;
441 nand_erase_options_t erase_opts = { 0, };
442 size_t max_len1, max_len2;
445 for (optind = 1; optind < argc; optind++) {
446 if (strcmp(argv[optind], "-f") == 0) {
447 if (optind >= argc - 1) {
448 printf("Option %s requires an argument\n",
453 fw1_start_block = simple_strtoul(argv[optind], NULL, 0);
454 if (fw1_start_block >= mtd_num_blocks) {
455 printf("Block number %lu is out of range: 0..%lu\n",
456 fw1_start_block, mtd_num_blocks - 1);
460 } else if (strcmp(argv[optind], "-r") == 0) {
461 if (optind < argc - 1 && argv[optind + 1][0] != '-') {
463 fw2_start_block = simple_strtoul(argv[optind],
465 if (fw2_start_block >= mtd_num_blocks) {
466 printf("Block number %lu is out of range: 0..%lu\n",
473 } else if (strcmp(argv[optind], "-e") == 0) {
474 if (optind >= argc - 1) {
475 printf("Option %s requires an argument\n",
480 extra_blocks = simple_strtoul(argv[optind], NULL, 0);
481 if (extra_blocks >= mtd_num_blocks) {
482 printf("Extra block count %lu is out of range: 0..%lu\n",
487 } else if (argv[optind][0] == '-') {
488 printf("Unrecognized option %s\n", argv[optind]);
495 load_addr = getenv("fileaddr");
496 file_size = getenv("filesize");
498 if (argc - optind < 1 && load_addr == NULL) {
499 printf("Load address not specified\n");
502 if (argc - optind < 2 && file_size == NULL) {
503 printf("WARNING: Image size not specified; overwriting whole uboot partition\n");
507 addr = (void *)simple_strtoul(argv[optind], NULL, 16);
512 size = simple_strtoul(argv[optind], NULL, 16);
515 if (load_addr != NULL) {
516 addr = (void *)simple_strtoul(load_addr, NULL, 16);
517 printf("Using default load address %p\n", addr);
519 if (file_size != NULL) {
520 size = simple_strtoul(file_size, NULL, 16);
521 printf("Using default file size %08x\n", size);
524 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
526 fw_num_blocks = CONFIG_U_BOOT_IMG_SIZE / mtd->erasesize - extra_blocks;
529 fw1_start_block = CONFIG_SYS_NAND_U_BOOT_OFFS / mtd->erasesize;
530 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
532 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
535 if (fw2_set && fw2_start_block == 0) {
536 fw2_start_block = fw1_end_block + 1;
537 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
539 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
542 #ifdef CONFIG_ENV_IS_IN_NAND
543 fail_if_overlap(fcb, env, "FCB", "Environment");
544 fail_if_overlap(fw1, env, "FW1", "Environment");
546 fail_if_overlap(fcb, fw1, "FCB", "FW1");
548 fail_if_overlap(fcb, fw2, "FCB", "FW2");
549 #ifdef CONFIG_ENV_IS_IN_NAND
550 fail_if_overlap(fw2, env, "FW2", "Environment");
552 fail_if_overlap(fw1, fw2, "FW1", "FW2");
555 buf = malloc(erase_size);
557 printf("Failed to allocate buffer\n");
561 /* search for bad blocks in FW1 block range */
562 max_len1 = count_good_blocks(fw1_start_block, fw1_end_block);
563 printf("%u good blocks in %lu..%lu\n",
564 max_len1, fw1_start_block, fw1_end_block);
565 if (fw_num_blocks > max_len1) {
566 printf("Too many bad blocks in FW1 block range: %lu..%lu; max blocks: %u\n",
567 fw1_end_block + 1 - fw_num_blocks - extra_blocks,
568 fw1_end_block, max_len1);
572 /* search for bad blocks in FW2 block range */
573 max_len2 = count_good_blocks(fw2_start_block, fw2_end_block);
574 if (fw2_start_block > 0 && fw_num_blocks > max_len2) {
575 printf("Too many bad blocks in FW2 block range: %lu..%lu\n",
576 fw2_end_block + 1 - fw_num_blocks - extra_blocks,
581 fcb = create_fcb(buf, fw1_start_block, fw2_start_block,
582 ALIGN(fw_num_blocks * mtd->erasesize, mtd->writesize));
584 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
587 encode_hamming_13_8(fcb, (void *)fcb + 512, 512);
589 ret = write_fcb(buf, fcb_start_block);
591 printf("Failed to write FCB to block %lu\n", fcb_start_block);
595 ret = patch_ivt(addr, size ?: fw_num_blocks * mtd->erasesize);
600 printf("Programming U-Boot image from %p to block %lu\n",
601 addr, fw1_start_block);
602 if (size & (page_size - 1)) {
603 memset(addr + size, 0xff, size & (page_size - 1));
604 size = ALIGN(size, page_size);
607 erase_opts.offset = fcb->fw1_start_page * page_size;
608 erase_opts.length = (fw1_end_block - fw1_start_block + 1) *
610 erase_opts.quiet = 1;
612 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
613 erase_opts.offset + erase_opts.length - 1);
615 ret = nand_erase_opts(mtd, &erase_opts);
617 printf("Failed to erase flash: %d\n", ret);
621 max_len1 *= mtd->erasesize;
625 printf("Programming flash @ %08x..%08x from %p\n",
626 fcb->fw1_start_page * page_size,
627 fcb->fw1_start_page * page_size + max_len1 - 1, addr);
628 ret = nand_write_skip_bad(mtd, fcb->fw1_start_page * page_size,
629 &max_len1, &actual, erase_opts.length, addr,
631 if (ret || actual < size) {
632 printf("Failed to program flash: %d\n", ret);
635 if (fw2_start_block == 0) {
639 printf("Programming redundant U-Boot image to block %lu\n",
641 erase_opts.offset = fcb->fw2_start_page * page_size;
642 erase_opts.length = (fw2_end_block - fw2_start_block + 1) *
644 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
645 erase_opts.offset + erase_opts.length - 1);
647 ret = nand_erase_opts(mtd, &erase_opts);
649 printf("Failed to erase flash: %d\n", ret);
653 max_len2 *= mtd->erasesize;
656 printf("Programming flash @ %08x..%08x from %p\n",
657 fcb->fw2_start_page * page_size,
658 fcb->fw2_start_page * page_size + max_len2 - 1, addr);
659 ret = nand_write_skip_bad(mtd, fcb->fw2_start_page * page_size,
660 &max_len2, &actual, erase_opts.length, addr,
662 if (ret || actual < size) {
663 printf("Failed to program flash: %d\n", ret);
669 U_BOOT_CMD(romupdate, 11, 0, do_update,
670 "Creates an FCB data structure and writes an U-Boot image to flash",
671 "[-f #] [-r [#]] [-e #] [<address>] [<length>]\n"
672 "\t-f #\twrite bootloader image at block #\n"
673 "\t-r\twrite redundant bootloader image at next free block after first image\n"
674 "\t-r #\twrite redundant bootloader image at block #\n"
675 "\t-e #\tspecify number of redundant blocks per boot loader image (default 2)\n"
676 "\t<address>\tRAM address of bootloader image (default: ${fileaddr}\n"
677 "\t<length>\tlength of bootloader image in RAM (default: ${filesize}"