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/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)
81 static nand_info_t *mtd = &nand_info[0];
84 #define BIT(v,n) (((v) >> (n)) & 0x1)
86 static u8 calculate_parity_13_8(u8 d)
90 p |= (BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 3) ^ BIT(d, 2)) << 0;
91 p |= (BIT(d, 7) ^ BIT(d, 5) ^ BIT(d, 4) ^ BIT(d, 2) ^ BIT(d, 1)) << 1;
92 p |= (BIT(d, 7) ^ BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 1) ^ BIT(d, 0)) << 2;
93 p |= (BIT(d, 7) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 0)) << 3;
94 p |= (BIT(d, 6) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 2) ^ BIT(d, 1) ^ BIT(d, 0)) << 4;
98 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
104 for (i = 0; i < size; i++)
105 ecc[i] = calculate_parity_13_8(src[i]);
108 static u32 calc_chksum(void *buf, size_t size)
114 for (i = 0; i < size; i++) {
121 * return number of blocks to skip for a contiguous partition
124 static int find_contig_space(int block, int num_blocks, int max_blocks)
128 int last = block + max_blocks;
130 debug("Searching %u contiguous blocks from %d..%d\n",
131 num_blocks, block, block + max_blocks - 1);
132 for (; block < last; block++) {
133 if (nand_block_isbad(mtd, block * mtd->erasesize)) {
136 debug("Skipping %u blocks to %u\n",
140 if (found >= num_blocks) {
141 debug("Found %u good blocks from %d..%d\n",
142 found, block - found + 1, block);
150 #define offset_of(p, m) ((void *)&(p)->m - (void *)(p))
151 #define pr_fcb_val(p, n) debug("%-24s[%02x]=%08x(%d)\n", #n, offset_of(p, n), (p)->n, (p)->n)
153 static struct mx6_fcb *create_fcb(void *buf, int fw1_start_block,
154 int fw2_start_block, int fw_num_blocks)
156 struct gpmi_regs *gpmi_base = (void *)GPMI_BASE_ADDRESS;
157 struct bch_regs *bch_base = (void *)BCH_BASE_ADDRESS;
164 if (gpmi_base == NULL || bch_base == NULL) {
165 return ERR_PTR(-ENOMEM);
168 fl0 = readl(&bch_base->hw_bch_flash0layout0);
169 fl1 = readl(&bch_base->hw_bch_flash0layout1);
170 t0 = readl(&gpmi_base->hw_gpmi_timing0);
172 metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
174 fcb = buf + ALIGN(metadata_size, 4);
175 fcb_offs = (void *)fcb - buf;
177 memset(buf, 0xff, fcb_offs);
178 memset(fcb, 0x00, sizeof(*fcb));
179 memset(fcb + 1, 0xff, mtd->erasesize - fcb_offs - sizeof(*fcb));
181 strncpy((char *)&fcb->fingerprint, "FCB ", 4);
182 fcb->version = cpu_to_be32(1);
184 fcb->disbb_search = 0;
187 /* ROM code assumes GPMI clock of 25 MHz */
188 fcb->timing.data_setup = BF_VAL(t0, GPMI_TIMING0_DATA_SETUP) * 40;
189 fcb->timing.data_hold = BF_VAL(t0, GPMI_TIMING0_DATA_HOLD) * 40;
190 fcb->timing.address_setup = BF_VAL(t0, GPMI_TIMING0_ADDRESS_SETUP) * 40;
192 fcb->page_data_size = mtd->writesize;
193 fcb->total_page_size = mtd->writesize + mtd->oobsize;
194 fcb->sectors_per_block = mtd->erasesize / mtd->writesize;
196 fcb->ecc_block0_type = BF_VAL(fl0, BCH_FLASHLAYOUT0_ECC0);
197 fcb->ecc_block0_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_DATA0_SIZE) * 4;
198 fcb->ecc_blockn_type = BF_VAL(fl1, BCH_FLASHLAYOUT1_ECCN);
199 fcb->ecc_blockn_size = BF_VAL(fl1, BCH_FLASHLAYOUT1_DATAN_SIZE) * 4;
201 pr_fcb_val(fcb, ecc_block0_type);
202 pr_fcb_val(fcb, ecc_blockn_type);
203 pr_fcb_val(fcb, ecc_block0_size);
204 pr_fcb_val(fcb, ecc_blockn_size);
206 fcb->metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
207 fcb->ecc_blocks_per_page = BF_VAL(fl0, BCH_FLASHLAYOUT0_NBLOCKS);
208 fcb->bch_mode = readl(&bch_base->hw_bch_mode);
209 fcb->bch_type = 0; /* BCH20 */
211 fcb->fw1_start_page = fw1_start_block * fcb->sectors_per_block;
212 fcb->fw1_sectors = fw_num_blocks * fcb->sectors_per_block;
213 pr_fcb_val(fcb, fw1_start_page);
214 pr_fcb_val(fcb, fw1_sectors);
216 if (fw2_start_block != 0 &&
217 fw2_start_block < lldiv(mtd->size, mtd->erasesize)) {
218 fcb->fw2_start_page = fw2_start_block * fcb->sectors_per_block;
219 fcb->fw2_sectors = fcb->fw1_sectors;
220 pr_fcb_val(fcb, fw2_start_page);
221 pr_fcb_val(fcb, fw2_sectors);
224 fcb->dbbt_search_area = 0;
226 fcb->checksum = calc_chksum(&fcb->fingerprint, 512 - 4);
230 static int find_fcb(void *ref, int page)
233 struct nand_chip *chip = mtd->priv;
234 void *buf = malloc(mtd->erasesize);
239 chip->select_chip(mtd, 0);
240 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
241 ret = chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
243 printf("Failed to read FCB from page %u: %d\n", page, ret);
246 if (memcmp(buf, ref, mtd->writesize) == 0) {
247 debug("Found FCB in page %u (%08x)\n",
248 page, page * mtd->writesize);
252 chip->select_chip(mtd, -1);
257 static int write_fcb(void *buf, int block)
260 struct nand_chip *chip = mtd->priv;
261 int page = block * mtd->erasesize / mtd->writesize;
263 ret = find_fcb(buf, page);
265 printf("FCB at block %d is up to date\n", block);
270 ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
272 printf("Failed to erase FCB block %u\n", block);
277 printf("Writing FCB to block %d @ %08llx\n", block,
278 (u64)block * mtd->erasesize);
280 chip->select_chip(mtd, 0);
281 ret = chip->write_page(mtd, chip, 0, mtd->writesize,
284 printf("Failed to write FCB to block %u: %d\n", block, ret);
286 chip->select_chip(mtd, -1);
291 #define chk_overlap(a,b) \
292 ((a##_start_block <= b##_end_block && \
293 a##_end_block >= b##_start_block) || \
294 (b##_start_block <= a##_end_block && \
295 b##_end_block >= a##_start_block))
297 #define fail_if_overlap(a,b,m1,m2) do { \
299 printf("check: %s[%lu..%lu] <> %s[%lu..%lu]\n", \
300 m1, a##_start_block, a##_end_block, \
301 m2, b##_start_block, b##_end_block); \
302 if (a##_end_block < a##_start_block) \
303 printf("Invalid start/end block # for %s\n", m1); \
304 if (b##_end_block < b##_start_block) \
305 printf("Invalid start/end block # for %s\n", m2); \
306 if (chk_overlap(a, b)) { \
307 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
308 m1, a##_start_block, a##_end_block, \
309 m2, b##_start_block, b##_end_block); \
314 static int tx6_prog_uboot(void *addr, int start_block, int skip,
315 size_t size, size_t max_len)
318 nand_erase_options_t erase_opts = { 0, };
320 size_t prg_length = max_len - skip * mtd->erasesize;
321 int prg_start = (start_block + skip) * mtd->erasesize;
323 erase_opts.offset = start_block * mtd->erasesize;
324 erase_opts.length = max_len;
325 erase_opts.quiet = 1;
327 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
328 erase_opts.offset + erase_opts.length - 1);
330 ret = nand_erase_opts(mtd, &erase_opts);
332 printf("Failed to erase flash: %d\n", ret);
337 printf("Programming flash @ %08x..%08x from %p\n",
338 prg_start, prg_start + size - 1, addr);
341 ret = nand_write_skip_bad(mtd, prg_start, &actual, NULL,
342 prg_length, addr, WITH_DROP_FFS);
344 printf("Failed to program flash: %d\n", ret);
348 printf("Could only write %u of %u bytes\n", actual, size);
355 #ifdef CONFIG_ENV_IS_IN_NAND
356 #ifndef CONFIG_ENV_OFFSET_REDUND
357 #define TOTAL_ENV_SIZE CONFIG_ENV_RANGE
359 #define TOTAL_ENV_SIZE (CONFIG_ENV_RANGE * 2)
363 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
366 const unsigned long fcb_start_block = 0, fcb_end_block = 0;
367 int erase_size = mtd->erasesize;
374 unsigned long mtd_num_blocks = lldiv(mtd->size, mtd->erasesize);
375 #ifdef CONFIG_ENV_IS_IN_NAND
376 unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
377 unsigned long env_end_block = env_start_block +
378 DIV_ROUND_UP(TOTAL_ENV_SIZE, mtd->erasesize) - 1;
382 unsigned long fw1_start_block = 0, fw1_end_block;
383 unsigned long fw2_start_block = 0, fw2_end_block;
384 unsigned long fw_num_blocks;
385 int fw1_skip, fw2_skip;
386 unsigned long extra_blocks = 0;
387 u64 max_len1, max_len2;
388 struct mtd_device *dev;
389 struct part_info *part_info;
390 struct part_info *redund_part_info;
391 const char *uboot_part = "u-boot";
392 const char *redund_part = NULL;
396 ret = mtdparts_init();
401 for (optind = 1; optind < argc; optind++) {
404 if (strcmp(argv[optind], "-f") == 0) {
405 if (optind >= argc - 1) {
406 printf("Option %s requires an argument\n",
411 fw1_start_block = simple_strtoul(argv[optind], &endp, 0);
413 uboot_part = argv[optind];
417 if (fw1_start_block >= mtd_num_blocks) {
418 printf("Block number %lu is out of range: 0..%lu\n",
419 fw1_start_block, mtd_num_blocks - 1);
422 } else if (strcmp(argv[optind], "-r") == 0) {
424 if (optind < argc - 1 && argv[optind + 1][0] != '-') {
426 fw2_start_block = simple_strtoul(argv[optind],
429 redund_part = argv[optind];
432 if (fw2_start_block >= mtd_num_blocks) {
433 printf("Block number %lu is out of range: 0..%lu\n",
439 } else if (strcmp(argv[optind], "-e") == 0) {
440 if (optind >= argc - 1) {
441 printf("Option %s requires an argument\n",
446 extra_blocks = simple_strtoul(argv[optind], NULL, 0);
447 if (extra_blocks >= mtd_num_blocks) {
448 printf("Extra block count %lu is out of range: 0..%lu\n",
453 } else if (strcmp(argv[optind], "-n") == 0) {
455 } else if (argv[optind][0] == '-') {
456 printf("Unrecognized option %s\n", argv[optind]);
463 load_addr = getenv("fileaddr");
464 file_size = getenv("filesize");
466 if (argc - optind < 1 && load_addr == NULL) {
467 printf("Load address not specified\n");
470 if (argc - optind < 2 && file_size == NULL) {
472 printf("WARNING: Image size not specified; overwriting whole '%s' partition\n",
474 printf("This will only work, if there are no bad blocks inside this partition!\n");
476 printf("ERROR: Image size must be specified\n");
482 addr = (void *)simple_strtoul(argv[optind], NULL, 16);
487 size = simple_strtoul(argv[optind], NULL, 16);
490 if (load_addr != NULL) {
491 addr = (void *)simple_strtoul(load_addr, NULL, 16);
492 printf("Using default load address %p\n", addr);
494 if (file_size != NULL) {
495 size = simple_strtoul(file_size, NULL, 16);
496 printf("Using default file size %08x\n", size);
499 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
504 ret = find_dev_and_part(uboot_part, &dev, &part_num,
507 printf("Failed to find '%s' partition: %d\n",
511 fw1_start_block = lldiv(part_info->offset, mtd->erasesize);
512 max_len1 = part_info->size;
514 fw_num_blocks = lldiv(max_len1, mtd->erasesize);
516 max_len1 = (u64)(fw_num_blocks + extra_blocks) * mtd->erasesize;
520 ret = find_dev_and_part(redund_part, &dev, &redund_part_num,
523 printf("Failed to find '%s' partition: %d\n",
527 fw2_start_block = lldiv(redund_part_info->offset, mtd->erasesize);
528 max_len2 = redund_part_info->size;
529 if (fw2_start_block == fcb_start_block) {
531 max_len2 -= mtd->erasesize;
534 fw_num_blocks = lldiv(max_len2, mtd->erasesize);
535 } else if (fw2_set) {
536 max_len2 = (u64)(fw_num_blocks + extra_blocks) * mtd->erasesize;
541 fw1_skip = find_contig_space(fw1_start_block, fw_num_blocks,
542 lldiv(max_len1, mtd->erasesize));
544 printf("Could not find %lu contiguous good blocks for fw image in blocks %lu..%llu\n",
545 fw_num_blocks, fw1_start_block,
546 fw1_start_block + lldiv(max_len1, mtd->erasesize) - 1);
548 #ifdef CONFIG_ENV_IS_IN_NAND
549 if (part_info->offset <= CONFIG_ENV_OFFSET + TOTAL_ENV_SIZE) {
550 printf("Use a different partition\n");
552 printf("Increase the size of the '%s' partition\n",
556 printf("Increase the size of the '%s' partition\n",
560 printf("Increase the number of spare blocks to use with the '-e' option\n");
565 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
567 fw1_end_block = fw1_start_block + fw_num_blocks + fw1_skip - 1;
569 if (fw2_set && fw2_start_block == 0)
570 fw2_start_block = fw1_end_block + 1;
571 if (fw2_start_block > 0) {
572 fw2_skip = find_contig_space(fw2_start_block, fw_num_blocks,
573 lldiv(max_len2, mtd->erasesize));
575 printf("Could not find %lu contiguous good blocks for redundant fw image in blocks %lu..%llu\n",
576 fw_num_blocks, fw2_start_block,
577 fw2_start_block + lldiv(max_len2, mtd->erasesize) - 1);
579 printf("Increase the size of the '%s' partition or use a different partition\n",
582 printf("Increase the number of spare blocks to use with the '-e' option\n");
590 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
592 fw2_end_block = fw2_start_block + fw_num_blocks + fw2_skip - 1;
594 #ifdef CONFIG_ENV_IS_IN_NAND
595 fail_if_overlap(fcb, env, "FCB", "Environment");
596 fail_if_overlap(fw1, env, "FW1", "Environment");
598 fail_if_overlap(fcb, fw1, "FCB", "FW1");
600 fail_if_overlap(fcb, fw2, "FCB", "FW2");
601 #ifdef CONFIG_ENV_IS_IN_NAND
602 fail_if_overlap(fw2, env, "FW2", "Environment");
604 fail_if_overlap(fw1, fw2, "FW1", "FW2");
606 fw1_start_block += fw1_skip;
607 fw2_start_block += fw2_skip;
609 buf = memalign(SZ_128K, erase_size);
611 printf("Failed to allocate buffer\n");
615 fcb = create_fcb(buf, fw1_start_block,
616 fw2_start_block, fw_num_blocks);
618 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
622 encode_hamming_13_8(fcb, (void *)fcb + 512, 512);
624 ret = write_fcb(buf, fcb_start_block);
626 printf("Failed to write FCB to block %lu\n", fcb_start_block);
630 printf("Programming U-Boot image from %p to block %lu @ %08llx\n",
631 buf, fw1_start_block, (u64)fw1_start_block * mtd->erasesize);
632 ret = tx6_prog_uboot(addr, fw1_start_block, fw1_skip, size,
635 if (ret || fw2_start_block == 0)
638 printf("Programming redundant U-Boot image to block %lu @ %08llx\n",
639 fw2_start_block, (u64)fw2_start_block * mtd->erasesize);
640 ret = tx6_prog_uboot(addr, fw2_start_block, fw2_skip, size,
647 U_BOOT_CMD(romupdate, 11, 0, do_update,
648 "Creates an FCB data structure and writes an U-Boot image to flash",
649 "[-f {<part>|block#}] [-r [{<part>|block#}]] [-e #] [<address>] [<length>]\n"
650 "\t-f <part>\twrite bootloader image to partition <part>\n"
651 "\t-f #\t\twrite bootloader image at block # (decimal)\n"
652 "\t-r\t\twrite redundant bootloader image at next free block after first image\n"
653 "\t-r <part>\twrite redundant bootloader image to partition <part>\n"
654 "\t-r #\t\twrite redundant bootloader image at block # (decimal)\n"
655 "\t-e #\t\tspecify number of redundant blocks per boot loader image\n"
656 "\t\t\t(only valid if -f or -r specify a flash address rather than a partition name)\n"
657 "\t-n\t\tshow what would be done without actually updating the flash\n"
658 "\t<address>\tRAM address of bootloader image (default: ${fileaddr})\n"
659 "\t<length>\tlength of bootloader image in RAM (default: ${filesize})"