2 * Copyright (C) 2012-2015 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>
24 #include <jffs2/load_kernel.h>
27 #include <linux/sizes.h>
28 #include <asm/arch/imx-regs.h>
29 #include <asm/imx-common/regs-gpmi.h>
30 #include <asm/imx-common/regs-bch.h>
32 struct mx6_nand_timing {
47 struct mx6_nand_timing timing;
50 u32 sectors_per_block;
51 u32 number_of_nands; /* not used by ROM code */
52 u32 total_internal_die; /* not used by ROM code */
53 u32 cell_type; /* not used by ROM code */
59 u32 ecc_blocks_per_page;
60 u32 rsrvd1[6]; /* not used by ROM code */
61 u32 bch_mode; /* erase_threshold */
70 u32 bb_mark_phys_offset;
72 u32 rsrvd3[8]; /* Toggle NAND timing parameters */
74 u32 bb_mark_spare_offset;
75 u32 rsrvd4[9]; /* ONFI NAND parameters */
79 struct mx6_dbbt_header {
105 struct mx6_boot_data {
111 #define BF_VAL(v, bf) (((v) & bf##_MASK) >> bf##_OFFSET)
113 static nand_info_t *mtd = &nand_info[0];
116 #define BIT(v,n) (((v) >> (n)) & 0x1)
118 static u8 calculate_parity_13_8(u8 d)
122 p |= (BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 3) ^ BIT(d, 2)) << 0;
123 p |= (BIT(d, 7) ^ BIT(d, 5) ^ BIT(d, 4) ^ BIT(d, 2) ^ BIT(d, 1)) << 1;
124 p |= (BIT(d, 7) ^ BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 1) ^ BIT(d, 0)) << 2;
125 p |= (BIT(d, 7) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 0)) << 3;
126 p |= (BIT(d, 6) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 2) ^ BIT(d, 1) ^ BIT(d, 0)) << 4;
130 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
136 for (i = 0; i < size; i++)
137 ecc[i] = calculate_parity_13_8(src[i]);
140 static u32 calc_chksum(void *buf, size_t size)
146 for (i = 0; i < size; i++) {
153 Physical organisation of data in NAND flash:
155 payload chunk 0 (may be empty)
156 ecc for metadata + payload chunk 0
158 ecc for payload chunk 1
161 ecc for payload chunk n
164 static inline int calc_bb_offset(nand_info_t *mtd, struct mx6_fcb *fcb)
167 int chunk_data_size = fcb->ecc_blockn_size * 8;
168 int chunk_ecc_size = (fcb->ecc_blockn_type << 1) * 13;
169 int chunk_total_size = chunk_data_size + chunk_ecc_size;
170 int bb_mark_chunk, bb_mark_chunk_offs;
172 bb_mark_offset = (mtd->writesize - fcb->metadata_size) * 8;
173 if (fcb->ecc_block0_size == 0)
174 bb_mark_offset -= (fcb->ecc_block0_type << 1) * 13;
176 bb_mark_chunk = bb_mark_offset / chunk_total_size;
177 bb_mark_chunk_offs = bb_mark_offset - (bb_mark_chunk * chunk_total_size);
178 if (bb_mark_chunk_offs > chunk_data_size) {
179 printf("Unsupported ECC layout; BB mark resides in ECC data: %u\n",
183 bb_mark_offset -= bb_mark_chunk * chunk_ecc_size;
184 return bb_mark_offset;
188 * return number of blocks to skip for a contiguous partition
191 static int find_contig_space(int block, int num_blocks, int max_blocks)
195 int last = block + max_blocks;
197 debug("Searching %u contiguous blocks from %d..%d\n",
198 num_blocks, block, block + max_blocks - 1);
199 for (; block < last; block++) {
200 if (nand_block_isbad(mtd, block * mtd->erasesize)) {
203 debug("Skipping %u blocks to %u\n",
207 if (found >= num_blocks) {
208 debug("Found %u good blocks from %d..%d\n",
209 found, block - found + 1, block);
217 #define offset_of(p, m) ((void *)&(p)->m - (void *)(p))
218 #define pr_fcb_val(p, n) debug("%-24s[%02x]=%08x(%d)\n", #n, offset_of(p, n), (p)->n, (p)->n)
220 static struct mx6_fcb *create_fcb(void *buf, int fw1_start_block,
221 int fw2_start_block, int fw_num_blocks)
223 struct gpmi_regs *gpmi_base = (void *)GPMI_BASE_ADDRESS;
224 struct bch_regs *bch_base = (void *)BCH_BASE_ADDRESS;
228 int bb_mark_bit_offs;
232 if (gpmi_base == NULL || bch_base == NULL) {
233 return ERR_PTR(-ENOMEM);
236 fl0 = readl(&bch_base->hw_bch_flash0layout0);
237 fl1 = readl(&bch_base->hw_bch_flash0layout1);
238 t0 = readl(&gpmi_base->hw_gpmi_timing0);
240 metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
242 fcb = buf + ALIGN(metadata_size, 4);
243 fcb_offs = (void *)fcb - buf;
245 memset(buf, 0xff, fcb_offs);
246 memset(fcb, 0x00, sizeof(*fcb));
247 memset(fcb + 1, 0xff, mtd->erasesize - fcb_offs - sizeof(*fcb));
249 strncpy((char *)&fcb->fingerprint, "FCB ", 4);
250 fcb->version = cpu_to_be32(1);
252 fcb->disbb_search = 0;
255 /* ROM code assumes GPMI clock of 25 MHz */
256 fcb->timing.data_setup = BF_VAL(t0, GPMI_TIMING0_DATA_SETUP) * 40;
257 fcb->timing.data_hold = BF_VAL(t0, GPMI_TIMING0_DATA_HOLD) * 40;
258 fcb->timing.address_setup = BF_VAL(t0, GPMI_TIMING0_ADDRESS_SETUP) * 40;
260 fcb->page_data_size = mtd->writesize;
261 fcb->total_page_size = mtd->writesize + mtd->oobsize;
262 fcb->sectors_per_block = mtd->erasesize / mtd->writesize;
264 fcb->ecc_block0_type = BF_VAL(fl0, BCH_FLASHLAYOUT0_ECC0);
265 fcb->ecc_block0_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_DATA0_SIZE) * 4;
266 fcb->ecc_blockn_type = BF_VAL(fl1, BCH_FLASHLAYOUT1_ECCN);
267 fcb->ecc_blockn_size = BF_VAL(fl1, BCH_FLASHLAYOUT1_DATAN_SIZE) * 4;
269 pr_fcb_val(fcb, ecc_block0_type);
270 pr_fcb_val(fcb, ecc_blockn_type);
271 pr_fcb_val(fcb, ecc_block0_size);
272 pr_fcb_val(fcb, ecc_blockn_size);
274 fcb->metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
275 fcb->ecc_blocks_per_page = BF_VAL(fl0, BCH_FLASHLAYOUT0_NBLOCKS);
276 fcb->bch_mode = readl(&bch_base->hw_bch_mode);
277 fcb->bch_type = 0; /* BCH20 */
279 fcb->fw1_start_page = fw1_start_block * fcb->sectors_per_block;
280 fcb->fw1_sectors = fw_num_blocks * fcb->sectors_per_block;
281 pr_fcb_val(fcb, fw1_start_page);
282 pr_fcb_val(fcb, fw1_sectors);
284 if (fw2_start_block != 0 && fw2_start_block < mtd->size / mtd->erasesize) {
285 fcb->fw2_start_page = fw2_start_block * fcb->sectors_per_block;
286 fcb->fw2_sectors = fcb->fw1_sectors;
287 pr_fcb_val(fcb, fw2_start_page);
288 pr_fcb_val(fcb, fw2_sectors);
291 fcb->dbbt_search_area = 0;
293 bb_mark_bit_offs = calc_bb_offset(mtd, fcb);
294 if (bb_mark_bit_offs < 0)
295 return ERR_PTR(bb_mark_bit_offs);
296 fcb->bb_mark_byte = bb_mark_bit_offs / 8;
297 fcb->bb_mark_startbit = bb_mark_bit_offs % 8;
298 fcb->bb_mark_phys_offset = mtd->writesize;
300 pr_fcb_val(fcb, bb_mark_byte);
301 pr_fcb_val(fcb, bb_mark_startbit);
302 pr_fcb_val(fcb, bb_mark_phys_offset);
304 fcb->checksum = calc_chksum(&fcb->fingerprint, 512 - 4);
308 static int find_fcb(void *ref, int page)
311 struct nand_chip *chip = mtd->priv;
312 void *buf = malloc(mtd->erasesize);
317 chip->select_chip(mtd, 0);
318 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
319 ret = chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
321 printf("Failed to read FCB from page %u: %d\n", page, ret);
324 if (memcmp(buf, ref, mtd->writesize) == 0) {
325 debug("Found FCB in page %u (%08x)\n",
326 page, page * mtd->writesize);
330 chip->select_chip(mtd, -1);
335 static int write_fcb(void *buf, int block)
338 struct nand_chip *chip = mtd->priv;
339 int page = block * mtd->erasesize / mtd->writesize;
341 ret = find_fcb(buf, page);
343 printf("FCB at block %d is up to date\n", block);
348 ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
350 printf("Failed to erase FCB block %u\n", block);
355 printf("Writing FCB to block %d @ %08llx\n", block,
356 (u64)block * mtd->erasesize);
358 chip->select_chip(mtd, 0);
359 ret = chip->write_page(mtd, chip, 0, mtd->writesize,
362 printf("Failed to write FCB to block %u: %d\n", block, ret);
364 chip->select_chip(mtd, -1);
369 #define chk_overlap(a,b) \
370 ((a##_start_block <= b##_end_block && \
371 a##_end_block >= b##_start_block) || \
372 (b##_start_block <= a##_end_block && \
373 b##_end_block >= a##_start_block))
375 #define fail_if_overlap(a,b,m1,m2) do { \
377 printf("check: %s[%lu..%lu] <> %s[%lu..%lu]\n", \
378 m1, a##_start_block, a##_end_block, \
379 m2, b##_start_block, b##_end_block); \
380 if (a##_end_block < a##_start_block) \
381 printf("Invalid start/end block # for %s\n", m1); \
382 if (b##_end_block < b##_start_block) \
383 printf("Invalid start/end block # for %s\n", m2); \
384 if (chk_overlap(a, b)) { \
385 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
386 m1, a##_start_block, a##_end_block, \
387 m2, b##_start_block, b##_end_block); \
392 static int tx6_prog_uboot(void *addr, int start_block, int skip,
393 size_t size, size_t max_len)
396 nand_erase_options_t erase_opts = { 0, };
398 size_t prg_length = max_len - skip * mtd->erasesize;
399 int prg_start = (start_block + skip) * mtd->erasesize;
401 erase_opts.offset = start_block * mtd->erasesize;
402 erase_opts.length = max_len;
403 erase_opts.quiet = 1;
405 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
406 erase_opts.offset + erase_opts.length - 1);
408 ret = nand_erase_opts(mtd, &erase_opts);
410 printf("Failed to erase flash: %d\n", ret);
415 printf("Programming flash @ %08x..%08x from %p\n",
416 prg_start, prg_start + size - 1, addr);
419 ret = nand_write_skip_bad(mtd, prg_start, &actual, NULL,
420 prg_length, addr, WITH_DROP_FFS);
422 printf("Failed to program flash: %d\n", ret);
426 printf("Could only write %u of %u bytes\n", actual, size);
433 #ifdef CONFIG_ENV_IS_IN_NAND
434 #ifndef CONFIG_ENV_OFFSET_REDUND
435 #define TOTAL_ENV_SIZE CONFIG_ENV_RANGE
437 #define TOTAL_ENV_SIZE (CONFIG_ENV_RANGE * 2)
441 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
444 const unsigned long fcb_start_block = 0, fcb_end_block = 0;
445 int erase_size = mtd->erasesize;
452 unsigned long mtd_num_blocks = mtd->size / mtd->erasesize;
453 #ifdef CONFIG_ENV_IS_IN_NAND
454 unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
455 unsigned long env_end_block = env_start_block +
456 DIV_ROUND_UP(TOTAL_ENV_SIZE, mtd->erasesize) - 1;
460 unsigned long fw1_start_block = 0, fw1_end_block;
461 unsigned long fw2_start_block = 0, fw2_end_block;
462 unsigned long fw_num_blocks;
463 int fw1_skip, fw2_skip;
464 unsigned long extra_blocks = 0;
465 size_t max_len1, max_len2;
466 struct mtd_device *dev;
467 struct part_info *part_info;
468 struct part_info *redund_part_info;
469 const char *uboot_part = "u-boot";
470 const char *redund_part = NULL;
474 ret = mtdparts_init();
479 for (optind = 1; optind < argc; optind++) {
482 if (strcmp(argv[optind], "-f") == 0) {
483 if (optind >= argc - 1) {
484 printf("Option %s requires an argument\n",
489 fw1_start_block = simple_strtoul(argv[optind], &endp, 0);
491 uboot_part = argv[optind];
495 if (fw1_start_block >= mtd_num_blocks) {
496 printf("Block number %lu is out of range: 0..%lu\n",
497 fw1_start_block, mtd_num_blocks - 1);
500 } else if (strcmp(argv[optind], "-r") == 0) {
502 if (optind < argc - 1 && argv[optind + 1][0] != '-') {
504 fw2_start_block = simple_strtoul(argv[optind],
507 redund_part = argv[optind];
510 if (fw2_start_block >= mtd_num_blocks) {
511 printf("Block number %lu is out of range: 0..%lu\n",
517 } else if (strcmp(argv[optind], "-e") == 0) {
518 if (optind >= argc - 1) {
519 printf("Option %s requires an argument\n",
524 extra_blocks = simple_strtoul(argv[optind], NULL, 0);
525 if (extra_blocks >= mtd_num_blocks) {
526 printf("Extra block count %lu is out of range: 0..%lu\n",
531 } else if (strcmp(argv[optind], "-n") == 0) {
533 } else if (argv[optind][0] == '-') {
534 printf("Unrecognized option %s\n", argv[optind]);
541 load_addr = getenv("fileaddr");
542 file_size = getenv("filesize");
544 if (argc - optind < 1 && load_addr == NULL) {
545 printf("Load address not specified\n");
548 if (argc - optind < 2 && file_size == NULL) {
550 printf("WARNING: Image size not specified; overwriting whole '%s' partition\n",
552 printf("This will only work, if there are no bad blocks inside this partition!\n");
554 printf("ERROR: Image size must be specified\n");
560 addr = (void *)simple_strtoul(argv[optind], NULL, 16);
565 size = simple_strtoul(argv[optind], NULL, 16);
568 if (load_addr != NULL) {
569 addr = (void *)simple_strtoul(load_addr, NULL, 16);
570 printf("Using default load address %p\n", addr);
572 if (file_size != NULL) {
573 size = simple_strtoul(file_size, NULL, 16);
574 printf("Using default file size %08x\n", size);
577 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
582 ret = find_dev_and_part(uboot_part, &dev, &part_num,
585 printf("Failed to find '%s' partition: %d\n",
589 fw1_start_block = part_info->offset / mtd->erasesize;
590 max_len1 = part_info->size;
592 fw_num_blocks = max_len1 / mtd->erasesize;
594 max_len1 = (fw_num_blocks + extra_blocks) * mtd->erasesize;
598 ret = find_dev_and_part(redund_part, &dev, &redund_part_num,
601 printf("Failed to find '%s' partition: %d\n",
605 fw2_start_block = redund_part_info->offset / mtd->erasesize;
606 max_len2 = redund_part_info->size;
607 if (fw2_start_block == fcb_start_block) {
609 max_len2 -= mtd->erasesize;
612 fw_num_blocks = max_len2 / mtd->erasesize;
613 } else if (fw2_set) {
614 max_len2 = (fw_num_blocks + extra_blocks) * mtd->erasesize;
619 fw1_skip = find_contig_space(fw1_start_block, fw_num_blocks,
620 max_len1 / mtd->erasesize);
622 printf("Could not find %lu contiguous good blocks for fw image in blocks %lu..%lu\n",
623 fw_num_blocks, fw1_start_block,
624 fw1_start_block + max_len1 / mtd->erasesize - 1);
626 #ifdef CONFIG_ENV_IS_IN_NAND
627 if (part_info->offset <= CONFIG_ENV_OFFSET + TOTAL_ENV_SIZE) {
628 printf("Use a different partition\n");
630 printf("Increase the size of the '%s' partition\n",
634 printf("Increase the size of the '%s' partition\n",
638 printf("Increase the number of spare blocks to use with the '-e' option\n");
643 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
645 fw1_end_block = fw1_start_block + fw_num_blocks + fw1_skip - 1;
647 if (fw2_set && fw2_start_block == 0)
648 fw2_start_block = fw1_end_block + 1;
649 if (fw2_start_block > 0) {
650 fw2_skip = find_contig_space(fw2_start_block, fw_num_blocks,
651 max_len2 / mtd->erasesize);
653 printf("Could not find %lu contiguous good blocks for redundant fw image in blocks %lu..%lu\n",
654 fw_num_blocks, fw2_start_block,
655 fw2_start_block + max_len2 / mtd->erasesize - 1);
657 printf("Increase the size of the '%s' partition or use a different partition\n",
660 printf("Increase the number of spare blocks to use with the '-e' option\n");
668 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
670 fw2_end_block = fw2_start_block + fw_num_blocks + fw2_skip - 1;
672 #ifdef CONFIG_ENV_IS_IN_NAND
673 fail_if_overlap(fcb, env, "FCB", "Environment");
674 fail_if_overlap(fw1, env, "FW1", "Environment");
676 fail_if_overlap(fcb, fw1, "FCB", "FW1");
678 fail_if_overlap(fcb, fw2, "FCB", "FW2");
679 #ifdef CONFIG_ENV_IS_IN_NAND
680 fail_if_overlap(fw2, env, "FW2", "Environment");
682 fail_if_overlap(fw1, fw2, "FW1", "FW2");
684 fw1_start_block += fw1_skip;
685 fw2_start_block += fw2_skip;
687 buf = memalign(SZ_128K, erase_size);
689 printf("Failed to allocate buffer\n");
693 fcb = create_fcb(buf, fw1_start_block,
694 fw2_start_block, fw_num_blocks);
696 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
700 encode_hamming_13_8(fcb, (void *)fcb + 512, 512);
702 ret = write_fcb(buf, fcb_start_block);
704 printf("Failed to write FCB to block %lu\n", fcb_start_block);
708 printf("Programming U-Boot image from %p to block %lu @ %08llx\n",
709 buf, fw1_start_block, (u64)fw1_start_block * mtd->erasesize);
710 ret = tx6_prog_uboot(addr, fw1_start_block, fw1_skip, size,
713 if (ret || fw2_start_block == 0)
716 printf("Programming redundant U-Boot image to block %lu @ %08llx\n",
717 fw2_start_block, (u64)fw2_start_block * mtd->erasesize);
718 ret = tx6_prog_uboot(addr, fw2_start_block, fw2_skip, size,
725 U_BOOT_CMD(romupdate, 11, 0, do_update,
726 "Creates an FCB data structure and writes an U-Boot image to flash",
727 "[-f {<part>|block#}] [-r [{<part>|block#}]] [-e #] [<address>] [<length>]\n"
728 "\t-f <part>\twrite bootloader image to partition <part>\n"
729 "\t-f #\t\twrite bootloader image at block # (decimal)\n"
730 "\t-r\t\twrite redundant bootloader image at next free block after first image\n"
731 "\t-r <part>\twrite redundant bootloader image to partition <part>\n"
732 "\t-r #\t\twrite redundant bootloader image at block # (decimal)\n"
733 "\t-e #\t\tspecify number of redundant blocks per boot loader image\n"
734 "\t\t\t(only valid if -f or -r specify a flash address rather than a partition name)\n"
735 "\t-n\t\tshow what would be done without actually updating the flash\n"
736 "\t<address>\tRAM address of bootloader image (default: ${fileaddr})\n"
737 "\t<length>\tlength of bootloader image in RAM (default: ${filesize})"