2 * Copyright (C) 2012-2016 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/sys_proto.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 #define BF_VAL(v, bf) (((v) & bf##_MASK) >> bf##_OFFSET)
80 #define BF_SET_VAL(r, v, bf) r = ((r) & ~bf##_MASK) | (((v) << bf##_OFFSET) & bf##_MASK)
82 static nand_info_t *mtd = &nand_info[0];
85 #define BIT(v,n) (((v) >> (n)) & 0x1)
87 static u8 calculate_parity_13_8(u8 d)
91 p |= (BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 3) ^ BIT(d, 2)) << 0;
92 p |= (BIT(d, 7) ^ BIT(d, 5) ^ BIT(d, 4) ^ BIT(d, 2) ^ BIT(d, 1)) << 1;
93 p |= (BIT(d, 7) ^ BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 1) ^ BIT(d, 0)) << 2;
94 p |= (BIT(d, 7) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 0)) << 3;
95 p |= (BIT(d, 6) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 2) ^ BIT(d, 1) ^ BIT(d, 0)) << 4;
99 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
105 for (i = 0; i < size; i++)
106 ecc[i] = calculate_parity_13_8(src[i]);
109 static struct bch_regs bch_save;
110 static struct bch_regs *bch_base = (void *)BCH_BASE_ADDRESS;
113 * Reprogram BCH engine for 40bit ECC on chunks of 128 byte
114 * and 32 byte of metadata as required by the i.MX6UL ROM code.
116 static void tx6_init_bch(void)
118 u32 fl0 = readl(&bch_base->hw_bch_flash0layout0);
119 u32 fl1 = readl(&bch_base->hw_bch_flash0layout1);
121 bch_save.hw_bch_flash0layout0 = fl0;
122 bch_save.hw_bch_flash0layout1 = fl1;
124 BF_SET_VAL(fl0, 32, BCH_FLASHLAYOUT0_META_SIZE);
125 BF_SET_VAL(fl0, 7, BCH_FLASHLAYOUT0_NBLOCKS);
127 BF_SET_VAL(fl0, 0x14, BCH_FLASHLAYOUT0_ECC0);
128 BF_SET_VAL(fl0, 128 / 4, BCH_FLASHLAYOUT0_DATA0_SIZE);
130 BF_SET_VAL(fl1, 0x14, BCH_FLASHLAYOUT1_ECCN);
131 BF_SET_VAL(fl1, 128 / 4, BCH_FLASHLAYOUT1_DATAN_SIZE);
133 writel(fl0, &bch_base->hw_bch_flash0layout0);
134 writel(fl1, &bch_base->hw_bch_flash0layout1);
137 static void tx6_restore_bch(void)
139 writel(bch_save.hw_bch_flash0layout0, &bch_base->hw_bch_flash0layout0);
140 writel(bch_save.hw_bch_flash0layout1, &bch_base->hw_bch_flash0layout1);
143 static u32 calc_chksum(void *buf, size_t size)
149 for (i = 0; i < size; i++) {
156 * return number of blocks to skip for a contiguous partition
159 static int find_contig_space(int block, int num_blocks, int max_blocks)
163 int last = block + max_blocks;
165 debug("Searching %u contiguous blocks from %d..%d\n",
166 num_blocks, block, block + max_blocks - 1);
167 for (; block < last; block++) {
168 if (nand_block_isbad(mtd, block * mtd->erasesize)) {
171 debug("Skipping %u blocks to %u\n",
175 if (found >= num_blocks) {
176 debug("Found %u good blocks from %d..%d\n",
177 found, block - found + 1, block);
185 #define offset_of(p, m) ((void *)&(p)->m - (void *)(p))
186 #define pr_fcb_val(p, n) debug("%-24s[%02x]=%08x(%d)\n", #n, offset_of(p, n), (p)->n, (p)->n)
188 static struct mx6_fcb *create_fcb(void *buf, int fw1_start_block,
189 int fw2_start_block, int fw_num_blocks)
191 struct gpmi_regs *gpmi_base = (void *)GPMI_BASE_ADDRESS;
197 if (gpmi_base == NULL || bch_base == NULL) {
198 return ERR_PTR(-ENOMEM);
201 fl0 = readl(&bch_base->hw_bch_flash0layout0);
202 fl1 = readl(&bch_base->hw_bch_flash0layout1);
203 t0 = readl(&gpmi_base->hw_gpmi_timing0);
205 if (!is_cpu_type(MXC_CPU_MX6UL)) {
206 int metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
208 fcb = buf + ALIGN(metadata_size, 4);
209 fcb_offs = (void *)fcb - buf;
211 memset(buf, 0xff, fcb_offs);
217 memset(fcb, 0, mtd->erasesize - fcb_offs);
219 strncpy((char *)&fcb->fingerprint, "FCB ", 4);
220 fcb->version = cpu_to_be32(1);
222 fcb->disbb_search = 0;
225 /* ROM code assumes GPMI clock of 25 MHz */
226 fcb->timing.data_setup = BF_VAL(t0, GPMI_TIMING0_DATA_SETUP) * 40;
227 fcb->timing.data_hold = BF_VAL(t0, GPMI_TIMING0_DATA_HOLD) * 40;
228 fcb->timing.address_setup = BF_VAL(t0, GPMI_TIMING0_ADDRESS_SETUP) * 40;
230 fcb->page_data_size = mtd->writesize;
231 fcb->total_page_size = mtd->writesize + mtd->oobsize;
232 fcb->sectors_per_block = mtd->erasesize / mtd->writesize;
234 fcb->ecc_block0_type = BF_VAL(fl0, BCH_FLASHLAYOUT0_ECC0);
235 fcb->ecc_block0_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_DATA0_SIZE) * 4;
236 fcb->ecc_blockn_type = BF_VAL(fl1, BCH_FLASHLAYOUT1_ECCN);
237 fcb->ecc_blockn_size = BF_VAL(fl1, BCH_FLASHLAYOUT1_DATAN_SIZE) * 4;
239 pr_fcb_val(fcb, ecc_block0_type);
240 pr_fcb_val(fcb, ecc_blockn_type);
241 pr_fcb_val(fcb, ecc_block0_size);
242 pr_fcb_val(fcb, ecc_blockn_size);
244 fcb->metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
245 fcb->ecc_blocks_per_page = BF_VAL(fl0, BCH_FLASHLAYOUT0_NBLOCKS);
246 fcb->bch_mode = readl(&bch_base->hw_bch_mode);
247 fcb->bch_type = 0; /* BCH20 */
249 fcb->fw1_start_page = fw1_start_block * fcb->sectors_per_block;
250 fcb->fw1_sectors = fw_num_blocks * fcb->sectors_per_block;
251 pr_fcb_val(fcb, fw1_start_page);
252 pr_fcb_val(fcb, fw1_sectors);
254 if (fw2_start_block != 0 &&
255 fw2_start_block < lldiv(mtd->size, mtd->erasesize)) {
256 fcb->fw2_start_page = fw2_start_block * fcb->sectors_per_block;
257 fcb->fw2_sectors = fcb->fw1_sectors;
258 pr_fcb_val(fcb, fw2_start_page);
259 pr_fcb_val(fcb, fw2_sectors);
262 fcb->dbbt_search_area = 0;
264 fcb->checksum = calc_chksum(&fcb->fingerprint, 512 - 4);
268 static int find_fcb(void *ref, int page)
271 struct nand_chip *chip = mtd->priv;
272 void *buf = malloc(mtd->erasesize);
277 chip->select_chip(mtd, 0);
278 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
279 ret = chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
281 printf("Failed to read FCB from page %u: %d\n", page, ret);
284 if (memcmp(buf, ref, mtd->writesize) == 0) {
285 debug("Found FCB in page %u (%08x)\n",
286 page, page * mtd->writesize);
290 chip->select_chip(mtd, -1);
295 static int write_fcb(void *buf, int block)
298 struct nand_chip *chip = mtd->priv;
299 int page = block * mtd->erasesize / mtd->writesize;
301 ret = find_fcb(buf, page);
303 printf("FCB at block %d is up to date\n", block);
308 ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
310 printf("Failed to erase FCB block %u\n", block);
315 printf("Writing FCB to block %d @ %08llx\n", block,
316 (u64)block * mtd->erasesize);
318 if (is_cpu_type(MXC_CPU_MX6UL)) {
319 size_t len = mtd->writesize;
322 printf("writing block %u from buffer %p\n", block, buf);
323 ret = nand_write(mtd, block * mtd->erasesize, &len, buf);
326 chip->select_chip(mtd, 0);
327 ret = chip->write_page(mtd, chip, 0, mtd->writesize,
331 printf("Failed to write FCB to block %u: %d\n", block, ret);
333 chip->select_chip(mtd, -1);
338 #define chk_overlap(a,b) \
339 ((a##_start_block <= b##_end_block && \
340 a##_end_block >= b##_start_block) || \
341 (b##_start_block <= a##_end_block && \
342 b##_end_block >= a##_start_block))
344 #define fail_if_overlap(a,b,m1,m2) do { \
346 printf("check: %s[%lu..%lu] <> %s[%lu..%lu]\n", \
347 m1, a##_start_block, a##_end_block, \
348 m2, b##_start_block, b##_end_block); \
349 if (a##_end_block < a##_start_block) \
350 printf("Invalid start/end block # for %s\n", m1); \
351 if (b##_end_block < b##_start_block) \
352 printf("Invalid start/end block # for %s\n", m2); \
353 if (chk_overlap(a, b)) { \
354 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
355 m1, a##_start_block, a##_end_block, \
356 m2, b##_start_block, b##_end_block); \
361 static int tx6_prog_uboot(void *addr, int start_block, int skip,
362 size_t size, size_t max_len)
365 nand_erase_options_t erase_opts = { 0, };
367 size_t prg_length = max_len - skip * mtd->erasesize;
368 int prg_start = start_block * mtd->erasesize;
370 erase_opts.offset = (start_block - skip) * mtd->erasesize;
371 erase_opts.length = max_len;
372 erase_opts.quiet = 1;
374 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
375 erase_opts.offset + erase_opts.length - 1);
377 ret = nand_erase_opts(mtd, &erase_opts);
379 printf("Failed to erase flash: %d\n", ret);
384 printf("Programming flash @ %08x..%08x from %p\n",
385 prg_start, prg_start + size - 1, addr);
388 ret = nand_write_skip_bad(mtd, prg_start, &actual, NULL,
389 prg_length, addr, 0);
391 printf("Failed to program flash: %d\n", ret);
395 printf("Could only write %u of %u bytes\n", actual, size);
402 #ifdef CONFIG_ENV_IS_IN_NAND
403 #ifndef CONFIG_ENV_OFFSET_REDUND
404 #define TOTAL_ENV_SIZE CONFIG_ENV_RANGE
406 #define TOTAL_ENV_SIZE (CONFIG_ENV_RANGE * 2)
410 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
413 const unsigned long fcb_start_block = 0, fcb_end_block = 0;
414 int erase_size = mtd->erasesize;
421 unsigned long mtd_num_blocks = lldiv(mtd->size, mtd->erasesize);
422 #ifdef CONFIG_ENV_IS_IN_NAND
423 unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
424 unsigned long env_end_block = env_start_block +
425 DIV_ROUND_UP(TOTAL_ENV_SIZE, mtd->erasesize) - 1;
429 unsigned long fw1_start_block = 0, fw1_end_block;
430 unsigned long fw2_start_block = 0, fw2_end_block;
431 unsigned long fw_num_blocks;
432 int fw1_skip, fw2_skip;
433 unsigned long extra_blocks = 0;
434 u64 max_len1, max_len2;
435 struct mtd_device *dev;
436 struct part_info *part_info;
437 struct part_info *redund_part_info;
438 const char *uboot_part = "u-boot";
439 const char *redund_part = NULL;
443 ret = mtdparts_init();
448 for (optind = 1; optind < argc; optind++) {
451 if (strcmp(argv[optind], "-f") == 0) {
452 if (optind >= argc - 1) {
453 printf("Option %s requires an argument\n",
458 fw1_start_block = simple_strtoul(argv[optind], &endp, 0);
460 uboot_part = argv[optind];
464 if (fw1_start_block >= mtd_num_blocks) {
465 printf("Block number %lu is out of range: 0..%lu\n",
466 fw1_start_block, mtd_num_blocks - 1);
469 } else if (strcmp(argv[optind], "-r") == 0) {
471 if (optind < argc - 1 && argv[optind + 1][0] != '-') {
473 fw2_start_block = simple_strtoul(argv[optind],
476 redund_part = argv[optind];
479 if (fw2_start_block >= mtd_num_blocks) {
480 printf("Block number %lu is out of range: 0..%lu\n",
486 } else if (strcmp(argv[optind], "-e") == 0) {
487 if (optind >= argc - 1) {
488 printf("Option %s requires an argument\n",
493 extra_blocks = simple_strtoul(argv[optind], NULL, 0);
494 if (extra_blocks >= mtd_num_blocks) {
495 printf("Extra block count %lu is out of range: 0..%lu\n",
500 } else if (strcmp(argv[optind], "-n") == 0) {
502 } else if (argv[optind][0] == '-') {
503 printf("Unrecognized option %s\n", argv[optind]);
510 load_addr = getenv("fileaddr");
511 file_size = getenv("filesize");
513 if (argc - optind < 1 && load_addr == NULL) {
514 printf("Load address not specified\n");
517 if (argc - optind < 2 && file_size == NULL) {
519 printf("WARNING: Image size not specified; overwriting whole '%s' partition\n",
521 printf("This will only work, if there are no bad blocks inside this partition!\n");
523 printf("ERROR: Image size must be specified\n");
529 addr = (void *)simple_strtoul(argv[optind], NULL, 16);
534 size = simple_strtoul(argv[optind], NULL, 16);
537 if (load_addr != NULL) {
538 addr = (void *)simple_strtoul(load_addr, NULL, 16);
539 printf("Using default load address %p\n", addr);
541 if (file_size != NULL) {
542 size = simple_strtoul(file_size, NULL, 16);
543 printf("Using default file size %08x\n", size);
546 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
551 ret = find_dev_and_part(uboot_part, &dev, &part_num,
554 printf("Failed to find '%s' partition: %d\n",
558 fw1_start_block = lldiv(part_info->offset, mtd->erasesize);
559 max_len1 = part_info->size;
561 fw_num_blocks = lldiv(max_len1, mtd->erasesize);
563 max_len1 = (u64)(fw_num_blocks + extra_blocks) * mtd->erasesize;
567 ret = find_dev_and_part(redund_part, &dev, &redund_part_num,
570 printf("Failed to find '%s' partition: %d\n",
574 fw2_start_block = lldiv(redund_part_info->offset, mtd->erasesize);
575 max_len2 = redund_part_info->size;
576 if (fw2_start_block == fcb_start_block) {
578 max_len2 -= mtd->erasesize;
581 fw_num_blocks = lldiv(max_len2, mtd->erasesize);
582 } else if (fw2_set) {
583 max_len2 = (u64)(fw_num_blocks + extra_blocks) * mtd->erasesize;
588 fw1_skip = find_contig_space(fw1_start_block, fw_num_blocks,
589 lldiv(max_len1, mtd->erasesize));
591 printf("Could not find %lu contiguous good blocks for fw image in blocks %lu..%llu\n",
592 fw_num_blocks, fw1_start_block,
593 fw1_start_block + lldiv(max_len1, mtd->erasesize) - 1);
595 #ifdef CONFIG_ENV_IS_IN_NAND
596 if (part_info->offset <= CONFIG_ENV_OFFSET + TOTAL_ENV_SIZE) {
597 printf("Use a different partition\n");
599 printf("Increase the size of the '%s' partition\n",
603 printf("Increase the size of the '%s' partition\n",
607 printf("Increase the number of spare blocks to use with the '-e' option\n");
612 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
614 fw1_end_block = fw1_start_block + fw_num_blocks + fw1_skip - 1;
616 if (fw2_set && fw2_start_block == 0)
617 fw2_start_block = fw1_end_block + 1;
618 if (fw2_start_block > 0) {
619 fw2_skip = find_contig_space(fw2_start_block, fw_num_blocks,
620 lldiv(max_len2, mtd->erasesize));
622 printf("Could not find %lu contiguous good blocks for redundant fw image in blocks %lu..%llu\n",
623 fw_num_blocks, fw2_start_block,
624 fw2_start_block + lldiv(max_len2, mtd->erasesize) - 1);
626 printf("Increase the size of the '%s' partition or use a different partition\n",
629 printf("Increase the number of spare blocks to use with the '-e' option\n");
637 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
639 fw2_end_block = fw2_start_block + fw_num_blocks + fw2_skip - 1;
641 #ifdef CONFIG_ENV_IS_IN_NAND
642 fail_if_overlap(fcb, env, "FCB", "Environment");
643 fail_if_overlap(fw1, env, "FW1", "Environment");
645 fail_if_overlap(fcb, fw1, "FCB", "FW1");
647 fail_if_overlap(fcb, fw2, "FCB", "FW2");
648 #ifdef CONFIG_ENV_IS_IN_NAND
649 fail_if_overlap(fw2, env, "FW2", "Environment");
651 fail_if_overlap(fw1, fw2, "FW1", "FW2");
653 fw1_start_block += fw1_skip;
654 fw2_start_block += fw2_skip;
656 buf = memalign(SZ_128K, erase_size);
658 printf("Failed to allocate buffer\n");
662 fcb = create_fcb(buf, fw1_start_block,
663 fw2_start_block, fw_num_blocks);
665 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
669 if (!is_cpu_type(MXC_CPU_MX6UL))
670 encode_hamming_13_8(fcb, (void *)fcb + 512, 512);
672 ret = write_fcb(buf, fcb_start_block);
674 printf("Failed to write FCB to block %lu\n", fcb_start_block);
678 printf("Programming U-Boot image from %p to block %lu @ %08llx\n",
679 addr, fw1_start_block, (u64)fw1_start_block * mtd->erasesize);
680 ret = tx6_prog_uboot(addr, fw1_start_block, fw1_skip, size,
683 if (ret || fw2_start_block == 0)
686 printf("Programming redundant U-Boot image to block %lu @ %08llx\n",
687 fw2_start_block, (u64)fw2_start_block * mtd->erasesize);
688 ret = tx6_prog_uboot(addr, fw2_start_block, fw2_skip, size,
695 U_BOOT_CMD(romupdate, 11, 0, do_update,
696 "Creates an FCB data structure and writes an U-Boot image to flash",
697 "[-f {<part>|block#}] [-r [{<part>|block#}]] [-e #] [<address>] [<length>]\n"
698 "\t-f <part>\twrite bootloader image to partition <part>\n"
699 "\t-f #\t\twrite bootloader image at block # (decimal)\n"
700 "\t-r\t\twrite redundant bootloader image at next free block after first image\n"
701 "\t-r <part>\twrite redundant bootloader image to partition <part>\n"
702 "\t-r #\t\twrite redundant bootloader image at block # (decimal)\n"
703 "\t-e #\t\tspecify number of redundant blocks per boot loader image\n"
704 "\t\t\t(only valid if -f or -r specify a flash address rather than a partition name)\n"
705 "\t-n\t\tshow what would be done without actually updating the flash\n"
706 "\t<address>\tRAM address of bootloader image (default: ${fileaddr})\n"
707 "\t<length>\tlength of bootloader image in RAM (default: ${filesize})"