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>
24 #include <jffs2/load_kernel.h>
27 #include <asm/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 #if CONFIG_SYS_NAND_U_BOOT_OFFS < 0x20000
33 #error CONFIG_SYS_NAND_U_BOOT_OFFS must be >= 128kIB
36 struct mx6_nand_timing {
51 struct mx6_nand_timing timing;
54 u32 sectors_per_block;
55 u32 number_of_nands; /* not used by ROM code */
56 u32 total_internal_die; /* not used by ROM code */
57 u32 cell_type; /* not used by ROM code */
63 u32 ecc_blocks_per_page;
64 u32 rsrvd1[6]; /* not used by ROM code */
65 u32 bch_mode; /* erase_threshold */
74 u32 bb_mark_phys_offset;
76 u32 rsrvd3[8]; /* Toggle NAND timing parameters */
78 u32 bb_mark_spare_offset;
79 u32 rsrvd4[9]; /* ONFI NAND parameters */
83 struct mx6_dbbt_header {
98 #define BF_VAL(v, bf) (((v) & bf##_MASK) >> bf##_OFFSET)
100 static nand_info_t *mtd = &nand_info[0];
104 #define BIT(v,n) (((v) >> (n)) & 0x1)
106 static inline void memdump(const void *addr, size_t len)
108 const char *buf = addr;
111 for (i = 0; i < len; i++) {
115 printf("%p:", &buf[i]);
117 printf(" %02x", buf[i]);
122 static u8 calculate_parity_13_8(u8 d)
126 p |= (BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 3) ^ BIT(d, 2)) << 0;
127 p |= (BIT(d, 7) ^ BIT(d, 5) ^ BIT(d, 4) ^ BIT(d, 2) ^ BIT(d, 1)) << 1;
128 p |= (BIT(d, 7) ^ BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 1) ^ BIT(d, 0)) << 2;
129 p |= (BIT(d, 7) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 0)) << 3;
130 p |= (BIT(d, 6) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 2) ^ BIT(d, 1) ^ BIT(d, 0)) << 4;
134 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
140 for (i = 0; i < size; i++)
141 ecc[i] = calculate_parity_13_8(src[i]);
144 static u32 calc_chksum(void *buf, size_t size)
150 for (i = 0; i < size; i++) {
157 Physical organisation of data in NAND flash:
159 payload chunk 0 (may be empty)
160 ecc for metadata + payload chunk 0
162 ecc for payload chunk 1
165 ecc for payload chunk n
168 static inline int calc_bb_offset(nand_info_t *mtd, struct mx6_fcb *fcb)
171 int chunk_data_size = fcb->ecc_blockn_size * 8;
172 int chunk_ecc_size = (fcb->ecc_blockn_type << 1) * 13;
173 int chunk_total_size = chunk_data_size + chunk_ecc_size;
174 int bb_mark_chunk, bb_mark_chunk_offs;
176 bb_mark_offset = (mtd->writesize - fcb->metadata_size) * 8;
177 if (fcb->ecc_block0_size == 0)
178 bb_mark_offset -= (fcb->ecc_block0_type << 1) * 13;
180 bb_mark_chunk = bb_mark_offset / chunk_total_size;
181 bb_mark_chunk_offs = bb_mark_offset - (bb_mark_chunk * chunk_total_size);
182 if (bb_mark_chunk_offs > chunk_data_size) {
183 printf("Unsupported ECC layout; BB mark resides in ECC data: %u\n",
187 bb_mark_offset -= bb_mark_chunk * chunk_ecc_size;
188 return bb_mark_offset;
191 static int find_contig_space(int block, int num_blocks, int max_blocks)
196 for (; block < block + max_blocks; block++) {
197 if (nand_block_isbad(mtd, block * mtd->erasesize)) {
199 printf("Adjusting start block from %u to %u\n",
204 if (found >= num_blocks)
211 #define pr_fcb_val(p, n) debug("%s=%08x(%d)\n", #n, (p)->n, (p)->n)
213 static struct mx6_fcb *create_fcb(void *buf, int fw1_start_block,
214 int fw2_start_block, int fw_num_blocks,
217 struct gpmi_regs *gpmi_base = (void *)GPMI_BASE_ADDRESS;
218 struct bch_regs *bch_base = (void *)BCH_BASE_ADDRESS;
222 int bb_mark_bit_offs;
226 if (gpmi_base == NULL || bch_base == NULL) {
227 return ERR_PTR(-ENOMEM);
230 fl0 = readl(&bch_base->hw_bch_flash0layout0);
231 fl1 = readl(&bch_base->hw_bch_flash0layout1);
232 t0 = readl(&gpmi_base->hw_gpmi_timing0);
234 metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
236 fcb = buf + ALIGN(metadata_size, 4);
237 fcb_offs = (void *)fcb - buf;
239 memset(buf, 0xff, fcb_offs);
240 memset(fcb, 0x00, sizeof(*fcb));
241 memset(fcb + 1, 0xff, mtd->erasesize - fcb_offs - sizeof(*fcb));
243 strncpy((char *)&fcb->fingerprint, "FCB ", 4);
244 fcb->version = cpu_to_be32(1);
246 fcb->disbb_search = 0;
249 /* ROM code assumes GPMI clock of 25 MHz */
250 fcb->timing.data_setup = BF_VAL(t0, GPMI_TIMING0_DATA_SETUP) * 40;
251 fcb->timing.data_hold = BF_VAL(t0, GPMI_TIMING0_DATA_HOLD) * 40;
252 fcb->timing.address_setup = BF_VAL(t0, GPMI_TIMING0_ADDRESS_SETUP) * 40;
254 fcb->page_data_size = mtd->writesize;
255 fcb->total_page_size = mtd->writesize + mtd->oobsize;
256 fcb->sectors_per_block = mtd->erasesize / mtd->writesize;
258 fcb->ecc_block0_type = BF_VAL(fl0, BCH_FLASHLAYOUT0_ECC0);
259 fcb->ecc_block0_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_DATA0_SIZE) * 4;
260 fcb->ecc_blockn_type = BF_VAL(fl1, BCH_FLASHLAYOUT1_ECCN);
261 fcb->ecc_blockn_size = BF_VAL(fl1, BCH_FLASHLAYOUT1_DATAN_SIZE) * 4;
263 pr_fcb_val(fcb, ecc_block0_type);
264 pr_fcb_val(fcb, ecc_blockn_type);
265 pr_fcb_val(fcb, ecc_block0_size);
266 pr_fcb_val(fcb, ecc_blockn_size);
268 fcb->metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
269 fcb->ecc_blocks_per_page = BF_VAL(fl0, BCH_FLASHLAYOUT0_NBLOCKS);
270 fcb->bch_mode = readl(&bch_base->hw_bch_mode);
271 fcb->bch_type = 0; /* BCH20 */
273 fcb->fw1_start_page = fw1_start_block * fcb->sectors_per_block;
274 fcb->fw1_sectors = fw_num_blocks * fcb->sectors_per_block;
275 pr_fcb_val(fcb, fw1_start_page);
276 pr_fcb_val(fcb, fw1_sectors);
278 if (fw2_start_block != 0 && fw2_start_block < mtd->size / mtd->erasesize) {
279 fcb->fw2_start_page = fw2_start_block * fcb->sectors_per_block;
280 fcb->fw2_sectors = fw_num_blocks * fcb->sectors_per_block;
281 pr_fcb_val(fcb, fw2_start_page);
282 pr_fcb_val(fcb, fw2_sectors);
285 fcb->dbbt_search_area = 0;
287 bb_mark_bit_offs = calc_bb_offset(mtd, fcb);
288 if (bb_mark_bit_offs < 0)
289 return ERR_PTR(bb_mark_bit_offs);
290 fcb->bb_mark_byte = bb_mark_bit_offs / 8;
291 fcb->bb_mark_startbit = bb_mark_bit_offs % 8;
292 fcb->bb_mark_phys_offset = mtd->writesize;
294 pr_fcb_val(fcb, bb_mark_byte);
295 pr_fcb_val(fcb, bb_mark_startbit);
296 pr_fcb_val(fcb, bb_mark_phys_offset);
298 fcb->checksum = calc_chksum(&fcb->fingerprint, 512 - 4);
302 static int find_fcb(void *ref, int page)
305 struct nand_chip *chip = mtd->priv;
306 void *buf = malloc(mtd->erasesize);
311 chip->select_chip(mtd, 0);
312 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
313 ret = chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
315 printf("Failed to read FCB from page %u: %d\n", page, ret);
318 chip->select_chip(mtd, -1);
319 if (memcmp(buf, ref, mtd->writesize) == 0) {
320 debug("Found FCB in page %u (%08x)\n",
321 page, page * mtd->writesize);
329 static int write_fcb(void *buf, int block)
332 struct nand_chip *chip = mtd->priv;
333 int page = block * mtd->erasesize / mtd->writesize;
335 ret = find_fcb(buf, page);
337 printf("FCB at block %d is up to date\n", block);
341 ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
343 printf("Failed to erase FCB block %u\n", block);
347 printf("Writing FCB to block %d @ %08x\n", block,
348 block * mtd->erasesize);
349 chip->select_chip(mtd, 0);
350 ret = chip->write_page(mtd, chip, buf, 1, page, 0, 1);
352 printf("Failed to write FCB to block %u: %d\n", block, ret);
354 chip->select_chip(mtd, -1);
369 struct mx6_boot_data {
375 static int find_ivt(void *buf)
377 struct mx6_ivt *ivt_hdr = buf + 0x400;
379 if ((ivt_hdr->magic & 0xff0000ff) != 0x400000d1)
385 static inline void *reloc(void *dst, void *base, void *ptr)
387 return dst + (ptr - base);
390 static int patch_ivt(void *buf, size_t fsize)
392 struct mx6_ivt *ivt_hdr = buf + 0x400;
393 struct mx6_boot_data *boot_data;
395 if (!find_ivt(buf)) {
396 printf("No IVT found in image at %p\n", buf);
399 boot_data = reloc(ivt_hdr, ivt_hdr->self, ivt_hdr->boot_data);
400 boot_data->length = fsize;
405 #define chk_overlap(a,b) \
406 ((a##_start_block <= b##_end_block && \
407 a##_end_block >= b##_start_block) || \
408 (b##_start_block <= a##_end_block && \
409 b##_end_block >= a##_start_block))
411 #define fail_if_overlap(a,b,m1,m2) do { \
412 if (chk_overlap(a, b)) { \
413 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
414 m1, a##_start_block, a##_end_block, \
415 m2, b##_start_block, b##_end_block); \
420 #ifdef CONFIG_ENV_IS_IN_NAND
421 #ifndef CONFIG_ENV_OFFSET_REDUND
422 #define TOTAL_ENV_SIZE CONFIG_ENV_RANGE
424 #define TOTAL_ENV_SIZE (CONFIG_ENV_RANGE * 2)
428 #define pr_fcb_offset(n) printf("%s: %04x (%d)\n", #n, \
429 offsetof(struct mx6_fcb, n), offsetof(struct mx6_fcb, n))
431 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
434 const unsigned long fcb_start_block = 0, fcb_end_block = 0;
435 int erase_size = mtd->erasesize;
436 int page_size = mtd->writesize;
443 unsigned long mtd_num_blocks = mtd->size / mtd->erasesize;
444 #ifdef CONFIG_ENV_IS_IN_NAND
445 unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
446 unsigned long env_end_block = env_start_block +
447 DIV_ROUND_UP(TOTAL_ENV_SIZE, mtd->erasesize) - 1;
452 unsigned long fw1_start_block = 0, fw1_end_block;
453 unsigned long fw2_start_block = 0, fw2_end_block;
454 unsigned long fw_num_blocks;
455 unsigned long extra_blocks = 2;
456 nand_erase_options_t erase_opts = { 0, };
457 size_t max_len1, max_len2;
458 struct mtd_device *dev;
459 struct part_info *part_info;
463 ret = mtdparts_init();
467 ret = find_dev_and_part("u-boot", &dev, &part_num,
470 printf("Failed to find u-boot partition: %d\n", ret);
474 for (optind = 1; optind < argc; optind++) {
475 if (strcmp(argv[optind], "-f") == 0) {
476 if (optind >= argc - 1) {
477 printf("Option %s requires an argument\n",
482 fw1_start_block = simple_strtoul(argv[optind], NULL, 0);
483 if (fw1_start_block >= mtd_num_blocks) {
484 printf("Block number %lu is out of range: 0..%lu\n",
485 fw1_start_block, mtd_num_blocks - 1);
489 } else if (strcmp(argv[optind], "-r") == 0) {
490 if (optind < argc - 1 && argv[optind + 1][0] != '-') {
492 fw2_start_block = simple_strtoul(argv[optind],
494 if (fw2_start_block >= mtd_num_blocks) {
495 printf("Block number %lu is out of range: 0..%lu\n",
502 } else if (strcmp(argv[optind], "-e") == 0) {
503 if (optind >= argc - 1) {
504 printf("Option %s requires an argument\n",
509 extra_blocks = simple_strtoul(argv[optind], NULL, 0);
510 if (extra_blocks >= mtd_num_blocks) {
511 printf("Extra block count %lu is out of range: 0..%lu\n",
516 } else if (argv[optind][0] == '-') {
517 printf("Unrecognized option %s\n", argv[optind]);
524 load_addr = getenv("fileaddr");
525 file_size = getenv("filesize");
527 if (argc - optind < 1 && load_addr == NULL) {
528 printf("Load address not specified\n");
531 if (argc - optind < 2 && file_size == NULL) {
532 printf("WARNING: Image size not specified; overwriting whole uboot partition\n");
536 addr = (void *)simple_strtoul(argv[optind], NULL, 16);
541 size = simple_strtoul(argv[optind], NULL, 16);
544 if (load_addr != NULL) {
545 addr = (void *)simple_strtoul(load_addr, NULL, 16);
546 printf("Using default load address %p\n", addr);
548 if (file_size != NULL) {
549 size = simple_strtoul(file_size, NULL, 16);
550 printf("Using default file size %08x\n", size);
553 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
555 fw_num_blocks = part_info->size / mtd->erasesize - extra_blocks;
556 size = fw_num_blocks * mtd->erasesize;
559 fw1_start_block = part_info->offset / mtd->erasesize;
561 fw1_start_block = find_contig_space(fw1_start_block,
563 size / mtd->erasesize);
564 if (fw1_start_block < 0) {
565 printf("Could not find %lu contiguous good blocks for fw image\n",
569 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
571 if (fw2_set && fw2_start_block == 0)
572 fw2_start_block = fw1_end_block + 1;
573 if (fw2_start_block > 0) {
574 fw2_start_block = find_contig_space(fw2_start_block,
576 size / mtd->erasesize);
577 if (fw2_start_block < 0) {
578 printf("Could not find %lu contiguous good blocks for redundant fw image\n",
583 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
585 #ifdef CONFIG_ENV_IS_IN_NAND
586 fail_if_overlap(fcb, env, "FCB", "Environment");
587 fail_if_overlap(fw1, env, "FW1", "Environment");
589 fail_if_overlap(fcb, fw1, "FCB", "FW1");
591 fail_if_overlap(fcb, fw2, "FCB", "FW2");
592 #ifdef CONFIG_ENV_IS_IN_NAND
593 fail_if_overlap(fw2, env, "FW2", "Environment");
595 fail_if_overlap(fw1, fw2, "FW1", "FW2");
598 buf = malloc(erase_size);
600 printf("Failed to allocate buffer\n");
604 fcb = create_fcb(buf, fw1_start_block, fw2_start_block, fw_num_blocks,
605 size / mtd->erasesize);
607 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
611 encode_hamming_13_8(fcb, (void *)fcb + 512, 512);
613 ret = write_fcb(buf, fcb_start_block);
616 printf("Failed to write FCB to block %lu\n", fcb_start_block);
619 ret = patch_ivt(addr, size);
624 printf("Programming U-Boot image from %p to block %lu\n",
625 addr, fw1_start_block);
626 if (size & (page_size - 1)) {
627 memset(addr + size, 0xff, size & (page_size - 1));
628 size = ALIGN(size, page_size);
631 erase_opts.offset = fcb->fw1_start_page * page_size;
632 erase_opts.length = (fw1_end_block - fw1_start_block + 1) *
634 erase_opts.quiet = 1;
636 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
637 erase_opts.offset + erase_opts.length - 1);
639 ret = nand_erase_opts(mtd, &erase_opts);
641 printf("Failed to erase flash: %d\n", ret);
647 printf("max_len1=%u\n", max_len1);
648 printf("Programming flash @ %08x..%08x from %p\n",
649 fcb->fw1_start_page * page_size,
650 fcb->fw1_start_page * page_size + max_len1 - 1, addr);
651 ret = nand_write_skip_bad(mtd, fcb->fw1_start_page * page_size,
652 &max_len1, &actual, erase_opts.length, addr,
654 if (ret || actual < size) {
655 printf("Failed to program flash: %d\n", ret);
658 if (fw2_start_block == 0) {
662 printf("Programming redundant U-Boot image to block %lu\n",
664 erase_opts.offset = fcb->fw2_start_page * page_size;
665 erase_opts.length = (fw2_end_block - fw2_start_block + 1) *
667 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
668 erase_opts.offset + erase_opts.length - 1);
670 ret = nand_erase_opts(mtd, &erase_opts);
672 printf("Failed to erase flash: %d\n", ret);
678 printf("max_len2=%u\n", max_len2);
679 printf("Programming flash @ %08x..%08x from %p\n",
680 fcb->fw2_start_page * page_size,
681 fcb->fw2_start_page * page_size + max_len2 - 1, addr);
682 ret = nand_write_skip_bad(mtd, fcb->fw2_start_page * page_size,
683 &max_len2, &actual, erase_opts.length, addr,
685 if (ret || actual < size) {
686 printf("Failed to program flash: %d\n", ret);
692 U_BOOT_CMD(romupdate, 11, 0, do_update,
693 "Creates an FCB data structure and writes an U-Boot image to flash",
694 "[-f #] [-r [#]] [-e #] [<address>] [<length>]\n"
695 "\t-f #\twrite bootloader image at block #\n"
696 "\t-r\twrite redundant bootloader image at next free block after first image\n"
697 "\t-r #\twrite redundant bootloader image at block #\n"
698 "\t-e #\tspecify number of redundant blocks per boot loader image (default 2)\n"
699 "\t<address>\tRAM address of bootloader image (default: ${fileaddr}\n"
700 "\t<length>\tlength of bootloader image in RAM (default: ${filesize}"