10 #include <asm/arch/imx-regs.h>
11 #include <asm/arch/regs-gpmi.h>
12 #include <asm/arch/regs-bch.h>
14 #if CONFIG_SYS_NAND_U_BOOT_OFFS < 0x20000
15 #error CONFIG_SYS_NAND_U_BOOT_OFFS must be >= 128kIB
18 struct mx6_nand_timing {
33 struct mx6_nand_timing timing;
36 u32 sectors_per_block;
37 u32 number_of_nands; /* not used by ROM code */
38 u32 total_internal_die; /* not used by ROM code */
39 u32 cell_type; /* not used by ROM code */
45 u32 ecc_blocks_per_page;
46 u32 rsrvd1[6]; /* not used by ROM code */
47 u32 bch_mode; /* erase_threshold */
56 u32 bb_mark_phys_offset;
58 u32 rsrvd3[8]; /* Toggle NAND timing parameters */
60 u32 bb_mark_spare_offset;
61 u32 rsrvd4[9]; /* ONFI NAND parameters */
65 struct mx6_dbbt_header {
80 #define BF_VAL(v, bf) (((v) & bf##_MASK) >> bf##_OFFSET)
82 static nand_info_t *mtd = &nand_info[0];
86 #define BIT(v,n) (((v) >> (n)) & 0x1)
88 static inline void memdump(const void *addr, size_t len)
90 const char *buf = addr;
93 for (i = 0; i < len; i++) {
97 printf("%p:", &buf[i]);
99 printf(" %02x", buf[i]);
104 static u8 calculate_parity_13_8(u8 d)
108 p |= (BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 3) ^ BIT(d, 2)) << 0;
109 p |= (BIT(d, 7) ^ BIT(d, 5) ^ BIT(d, 4) ^ BIT(d, 2) ^ BIT(d, 1)) << 1;
110 p |= (BIT(d, 7) ^ BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 1) ^ BIT(d, 0)) << 2;
111 p |= (BIT(d, 7) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 0)) << 3;
112 p |= (BIT(d, 6) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 2) ^ BIT(d, 1) ^ BIT(d, 0)) << 4;
116 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
122 for (i = 0; i < size; i++)
123 ecc[i] = calculate_parity_13_8(src[i]);
126 static u32 calc_chksum(void *buf, size_t size)
132 for (i = 0; i < size; i++) {
139 Physical organisation of data in NAND flash:
141 payload chunk 0 (may be empty)
142 ecc for metadata + payload chunk 0
144 ecc for payload chunk 1
147 ecc for payload chunk n
150 static int calc_bb_offset(nand_info_t *mtd, struct mx6_fcb *fcb)
153 int chunk_data_size = fcb->ecc_blockn_size * 8;
154 int chunk_ecc_size = (fcb->ecc_blockn_type << 1) * 13;
155 int chunk_total_size = chunk_data_size + chunk_ecc_size;
156 int bb_mark_chunk, bb_mark_chunk_offs;
158 bb_mark_offset = (mtd->writesize - fcb->metadata_size) * 8;
159 if (fcb->ecc_block0_size == 0)
160 bb_mark_offset -= (fcb->ecc_block0_type << 1) * 13;
162 bb_mark_chunk = bb_mark_offset / chunk_total_size;
163 bb_mark_chunk_offs = bb_mark_offset - (bb_mark_chunk * chunk_total_size);
164 if (bb_mark_chunk_offs > chunk_data_size) {
165 printf("Unsupported ECC layout; BB mark resides in ECC data: %u\n",
169 bb_mark_offset -= bb_mark_chunk * chunk_ecc_size;
170 return bb_mark_offset;
173 #define pr_fcb_val(p, n) debug("%s=%08x(%d)\n", #n, (p)->n, (p)->n)
175 static struct mx6_fcb *create_fcb(void *buf, int fw1_start_block,
176 int fw2_start_block, size_t fw_size)
178 struct gpmi_regs *gpmi_base = (struct gpmi_regs *)GPMI_BASE_ADDRESS;
179 struct bch_regs *bch_base = (struct bch_regs *)BCH_BASE_ADDRESS;
183 int bb_mark_bit_offs;
187 if (gpmi_base == NULL || bch_base == NULL) {
188 return ERR_PTR(-ENOMEM);
191 fl0 = readl(&bch_base->hw_bch_flash0layout0);
192 fl1 = readl(&bch_base->hw_bch_flash0layout1);
193 t0 = readl(&gpmi_base->hw_gpmi_timing0);
195 metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
197 fcb = buf + ALIGN(metadata_size, 4);
198 fcb_offs = (void *)fcb - buf;
200 memset(buf, 0xff, fcb_offs);
201 memset(fcb, 0x00, sizeof(*fcb));
202 memset(fcb + 1, 0xff, mtd->erasesize - fcb_offs - sizeof(*fcb));
204 strncpy((char *)&fcb->fingerprint, "FCB ", 4);
205 fcb->version = cpu_to_be32(1);
207 fcb->disbb_search = 1;
210 /* ROM code assumes GPMI clock of 25 MHz */
211 fcb->timing.data_setup = BF_VAL(t0, GPMI_TIMING0_DATA_SETUP) * 40;
212 fcb->timing.data_hold = BF_VAL(t0, GPMI_TIMING0_DATA_HOLD) * 40;
213 fcb->timing.address_setup = BF_VAL(t0, GPMI_TIMING0_ADDRESS_SETUP) * 40;
215 fcb->page_data_size = mtd->writesize;
216 fcb->total_page_size = mtd->writesize + mtd->oobsize;
217 fcb->sectors_per_block = mtd->erasesize / mtd->writesize;
219 fcb->ecc_block0_type = BF_VAL(fl0, BCH_FLASHLAYOUT0_ECC0);
220 fcb->ecc_block0_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_DATA0_SIZE) * 4;
221 fcb->ecc_blockn_type = BF_VAL(fl1, BCH_FLASHLAYOUT1_ECCN);
222 fcb->ecc_blockn_size = BF_VAL(fl1, BCH_FLASHLAYOUT1_DATAN_SIZE) * 4;
224 pr_fcb_val(fcb, ecc_block0_type);
225 pr_fcb_val(fcb, ecc_blockn_type);
226 pr_fcb_val(fcb, ecc_block0_size);
227 pr_fcb_val(fcb, ecc_blockn_size);
229 fcb->metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
230 fcb->ecc_blocks_per_page = BF_VAL(fl0, BCH_FLASHLAYOUT0_NBLOCKS);
231 fcb->bch_mode = readl(&bch_base->hw_bch_mode);
232 fcb->bch_type = 0; /* BCH20 */
234 fcb->fw1_start_page = fw1_start_block * mtd->erasesize / mtd->writesize;
235 fcb->fw1_sectors = DIV_ROUND_UP(fw_size, mtd->writesize);
237 if (fw2_start_block != 0 && fw2_start_block < mtd->size / mtd->erasesize) {
238 fcb->fw2_start_page = fw2_start_block * mtd->erasesize / mtd->writesize;
239 fcb->fw2_sectors = fcb->fw1_sectors;
242 fcb->dbbt_search_area = 1;
244 bb_mark_bit_offs = calc_bb_offset(mtd, fcb);
245 if (bb_mark_bit_offs < 0)
246 return ERR_PTR(bb_mark_bit_offs);
247 fcb->bb_mark_byte = bb_mark_bit_offs / 8;
248 fcb->bb_mark_startbit = bb_mark_bit_offs % 8;
249 fcb->bb_mark_phys_offset = mtd->writesize;
251 pr_fcb_val(fcb, bb_mark_byte);
252 pr_fcb_val(fcb, bb_mark_startbit);
253 pr_fcb_val(fcb, bb_mark_phys_offset);
255 fcb->checksum = calc_chksum(&fcb->fingerprint, 512 - 4);
259 static inline int find_fcb(void *ref, int page)
262 struct nand_chip *chip = mtd->priv;
263 void *buf = malloc(mtd->erasesize);
268 chip->select_chip(mtd, 0);
269 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
270 ret = chip->ecc.read_page_raw(mtd, chip, buf, page);
272 printf("Failed to read FCB from page %u: %d\n", page, ret);
275 chip->select_chip(mtd, -1);
276 if (memcmp(buf, ref, mtd->writesize) == 0) {
277 debug("Found FCB in page %u (%08x)\n",
278 page, page * mtd->writesize);
285 static int write_fcb(void *buf, int block)
288 struct nand_chip *chip = mtd->priv;
289 int page = block * mtd->erasesize / mtd->writesize;
291 ret = find_fcb(buf, page);
293 printf("FCB at block %d is up to date\n", block);
297 ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
299 printf("Failed to erase FCB block %u\n", block);
303 printf("Writing FCB to block %d @ %08x\n", block,
304 block * mtd->erasesize);
305 chip->select_chip(mtd, 0);
306 ret = chip->write_page(mtd, chip, buf, page, 0, 1);
308 printf("Failed to write FCB to block %u: %d\n", block, ret);
310 chip->select_chip(mtd, -1);
326 struct mx6_boot_data {
332 static size_t count_good_blocks(int start, int end)
334 size_t max_len = (end - start + 1);
337 for (block = start; block <= end; block++) {
338 if (nand_block_isbad(mtd, block * mtd->erasesize))
344 static int find_ivt(void *buf)
346 struct mx6_ivt *ivt_hdr = buf + 0x400;
348 if ((ivt_hdr->magic & 0xff0000ff) != 0x400000d1)
354 static inline void *reloc(void *dst, void *base, void *ptr)
356 return dst + (ptr - base);
359 static int patch_ivt(void *buf, size_t fsize)
361 struct mx6_ivt *ivt_hdr = buf + 0x400;
362 struct mx6_boot_data *boot_data;
364 if (!find_ivt(buf)) {
365 printf("No IVT found in image at %p\n", buf);
368 boot_data = reloc(ivt_hdr, ivt_hdr->self, ivt_hdr->boot_data);
369 boot_data->length = fsize;
374 #define chk_overlap(a,b) \
375 ((a##_start_block <= b##_end_block && \
376 a##_end_block >= b##_start_block) || \
377 (b##_start_block <= a##_end_block && \
378 b##_end_block >= a##_start_block))
380 #define fail_if_overlap(a,b,m1,m2) do { \
381 if (chk_overlap(a, b)) { \
382 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
383 m1, a##_start_block, a##_end_block, \
384 m2, b##_start_block, b##_end_block); \
389 #ifdef CONFIG_ENV_IS_IN_NAND
390 #ifndef CONFIG_ENV_OFFSET_REDUND
391 #define TOTAL_ENV_SIZE CONFIG_ENV_RANGE
393 #define TOTAL_ENV_SIZE (CONFIG_ENV_RANGE * 2)
397 #define pr_fcb_offset(n) printf("%s: %04x (%d)\n", #n, \
398 offsetof(struct mx6_fcb, n), offsetof(struct mx6_fcb, n))
400 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
403 const unsigned long fcb_start_block = 0, fcb_end_block = 0;
404 int erase_size = mtd->erasesize;
405 int page_size = mtd->writesize;
412 unsigned long mtd_num_blocks = mtd->size / mtd->erasesize;
413 #ifdef CONFIG_ENV_IS_IN_NAND
414 unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
415 unsigned long env_end_block = env_start_block +
416 DIV_ROUND_UP(TOTAL_ENV_SIZE, mtd->erasesize) - 1;
421 unsigned long fw1_start_block = 0, fw1_end_block;
422 unsigned long fw2_start_block = 0, fw2_end_block;
423 unsigned long fw_num_blocks;
424 unsigned long extra_blocks = 2;
425 nand_erase_options_t erase_opts = { 0, };
426 size_t max_len1, max_len2;
428 for (optind = 1; optind < argc; optind++) {
429 if (strcmp(argv[optind], "-f") == 0) {
430 if (optind >= argc - 1) {
431 printf("Option %s requires an argument\n",
436 fw1_start_block = simple_strtoul(argv[optind], NULL, 0);
437 if (fw1_start_block >= mtd_num_blocks) {
438 printf("Block number %lu is out of range: 0..%lu\n",
439 fw1_start_block, mtd_num_blocks - 1);
443 } else if (strcmp(argv[optind], "-r") == 0) {
444 if (optind < argc - 1 && argv[optind + 1][0] != '-') {
446 fw2_start_block = simple_strtoul(argv[optind],
448 if (fw2_start_block >= mtd_num_blocks) {
449 printf("Block number %lu is out of range: 0..%lu\n",
456 } else if (strcmp(argv[optind], "-e") == 0) {
457 if (optind >= argc - 1) {
458 printf("Option %s requires an argument\n",
463 extra_blocks = simple_strtoul(argv[optind], NULL, 0);
464 if (extra_blocks >= mtd_num_blocks) {
465 printf("Extra block count %lu is out of range: 0..%lu\n",
470 } else if (argv[optind][0] == '-') {
471 printf("Unrecognized option %s\n", argv[optind]);
478 load_addr = getenv("fileaddr");
479 file_size = getenv("filesize");
481 if (argc - optind < 1 && load_addr == NULL) {
482 printf("Load address not specified\n");
485 if (argc - optind < 2 && file_size == NULL) {
486 printf("WARNING: Image size not specified; overwriting whole uboot partition\n");
490 addr = (void *)simple_strtoul(argv[optind], NULL, 16);
495 size = simple_strtoul(argv[optind], NULL, 16);
498 if (load_addr != NULL) {
499 addr = (void *)simple_strtoul(load_addr, NULL, 16);
500 printf("Using default load address %p\n", addr);
502 if (file_size != NULL) {
503 size = simple_strtoul(file_size, NULL, 16);
504 printf("Using default file size %08x\n", size);
507 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
509 fw_num_blocks = CONFIG_U_BOOT_IMG_SIZE / mtd->erasesize - extra_blocks;
512 fw1_start_block = CONFIG_SYS_NAND_U_BOOT_OFFS / mtd->erasesize;
513 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
515 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
518 if (fw2_set && fw2_start_block == 0) {
519 fw2_start_block = fw1_end_block + 1;
520 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
522 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
525 #ifdef CONFIG_ENV_IS_IN_NAND
526 fail_if_overlap(fcb, env, "FCB", "Environment");
527 fail_if_overlap(fw1, env, "FW1", "Environment");
529 fail_if_overlap(fcb, fw1, "FCB", "FW1");
531 fail_if_overlap(fcb, fw2, "FCB", "FW2");
532 #ifdef CONFIG_ENV_IS_IN_NAND
533 fail_if_overlap(fw2, env, "FW2", "Environment");
535 fail_if_overlap(fw1, fw2, "FW1", "FW2");
538 buf = malloc(erase_size);
540 printf("Failed to allocate buffer\n");
544 /* search for bad blocks in FW1 block range */
545 max_len1 = count_good_blocks(fw1_start_block, fw1_end_block);
546 printf("%u good blocks in %lu..%lu\n",
547 max_len1, fw1_start_block, fw1_end_block);
548 if (fw_num_blocks > max_len1) {
549 printf("Too many bad blocks in FW1 block range: %lu..%lu; max blocks: %u\n",
550 fw1_end_block + 1 - fw_num_blocks - extra_blocks,
551 fw1_end_block, max_len1);
555 /* search for bad blocks in FW2 block range */
556 max_len2 = count_good_blocks(fw2_start_block, fw2_end_block);
557 if (fw2_start_block > 0 && fw_num_blocks > max_len2) {
558 printf("Too many bad blocks in FW2 block range: %lu..%lu\n",
559 fw2_end_block + 1 - fw_num_blocks - extra_blocks,
564 fcb = create_fcb(buf, fw1_start_block, fw2_start_block,
565 ALIGN(fw_num_blocks * mtd->erasesize, mtd->writesize));
567 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
570 encode_hamming_13_8(fcb, (void *)fcb + 512, 512);
572 ret = write_fcb(buf, fcb_start_block);
574 printf("Failed to write FCB to block %lu\n", fcb_start_block);
581 ret = patch_ivt(addr, size ?: fw_num_blocks * mtd->erasesize);
586 printf("Programming U-Boot image from %p to block %lu\n",
587 addr, fw1_start_block);
588 if (size & (page_size - 1)) {
589 memset(addr + size, 0xff, size & (page_size - 1));
590 size = ALIGN(size, page_size);
593 erase_opts.offset = fcb->fw1_start_page * page_size;
594 erase_opts.length = (fw1_end_block - fw1_start_block + 1) *
596 erase_opts.quiet = 1;
598 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
599 erase_opts.offset + erase_opts.length - 1);
601 ret = nand_erase_opts(mtd, &erase_opts);
603 printf("Failed to erase flash: %d\n", ret);
607 max_len1 *= mtd->erasesize;
611 printf("Programming flash @ %08x..%08x from %p\n",
612 fcb->fw1_start_page * page_size,
613 fcb->fw1_start_page * page_size + max_len1 - 1, addr);
614 ret = nand_write_skip_bad(mtd, fcb->fw1_start_page * page_size,
615 &max_len1, addr, WITH_DROP_FFS);
617 printf("Failed to program flash: %d\n", ret);
620 if (fw2_start_block == 0) {
624 printf("Programming redundant U-Boot image to block %lu\n",
626 erase_opts.offset = fcb->fw2_start_page * page_size;
627 erase_opts.length = (fw2_end_block - fw2_start_block + 1) *
629 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
630 erase_opts.offset + erase_opts.length - 1);
632 ret = nand_erase_opts(mtd, &erase_opts);
634 printf("Failed to erase flash: %d\n", ret);
638 max_len2 *= mtd->erasesize;
641 printf("Programming flash @ %08x..%08x from %p\n",
642 fcb->fw2_start_page * page_size,
643 fcb->fw2_start_page * page_size + max_len2 - 1, addr);
644 ret = nand_write_skip_bad(mtd, fcb->fw2_start_page * page_size,
645 &max_len2, addr, WITH_DROP_FFS);
647 printf("Failed to program flash: %d\n", ret);
653 U_BOOT_CMD(romupdate, 11, 0, do_update,
654 "Creates an FCB data structure and writes an U-Boot image to flash\n",
655 "[-f #] [-r [#]] [-e #] [<address>] [<length>]\n"
656 "\t-f #\twrite bootloader image at block #\n"
657 "\t-r\twrite redundant bootloader image at next free block after first image\n"
658 "\t-r #\twrite redundant bootloader image at block #\n"
659 "\t-e #\tspecify number of redundant blocks per boot loader image (default 2)\n"
660 "\t<address>\tRAM address of bootloader image (default: ${fileaddr}\n"
661 "\t<length>\tlength of bootloader image in RAM (default: ${filesize}"