10 #include <asm/arch/imx-regs.h>
11 #include <asm/arch/regs-gpmi.h>
12 #include <asm/arch/regs-bch.h>
14 #define FCB_START_BLOCK 0
15 #define NUM_FCB_BLOCKS 1
16 #define MAX_FCB_BLOCKS 32768
18 #if CONFIG_SYS_NAND_U_BOOT_OFFS < 0x20000
19 #error CONFIG_SYS_NAND_U_BOOT_OFFS must be >= 128kIB
22 struct mx6_nand_timing {
37 struct mx6_nand_timing timing;
40 u32 sectors_per_block;
41 u32 number_of_nands; /* not used by ROM code */
42 u32 total_internal_die; /* not used by ROM code */
43 u32 cell_type; /* not used by ROM code */
49 u32 ecc_blocks_per_page;
50 u32 rsrvd1[6]; /* not used by ROM code */
51 u32 bch_mode; /* erase_threshold */
60 u32 bb_mark_phys_offset;
62 u32 rsrvd3[8]; /* Toggle NAND timing parameters */
64 u32 bb_mark_spare_offset;
65 u32 rsrvd4[9]; /* ONFI NAND parameters */
69 struct mx6_dbbt_header {
84 #define BF_VAL(v, bf) (((v) & bf##_MASK) >> bf##_OFFSET)
86 static nand_info_t *mtd = &nand_info[0];
90 #define BIT(v,n) (((v) >> (n)) & 0x1)
92 static inline void memdump(const void *addr, size_t len)
94 const char *buf = addr;
97 for (i = 0; i < len; i++) {
101 printf("%p:", &buf[i]);
103 printf(" %02x", buf[i]);
108 static u8 calculate_parity_13_8(u8 d)
112 p |= (BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 3) ^ BIT(d, 2)) << 0;
113 p |= (BIT(d, 7) ^ BIT(d, 5) ^ BIT(d, 4) ^ BIT(d, 2) ^ BIT(d, 1)) << 1;
114 p |= (BIT(d, 7) ^ BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 1) ^ BIT(d, 0)) << 2;
115 p |= (BIT(d, 7) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 0)) << 3;
116 p |= (BIT(d, 6) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 2) ^ BIT(d, 1) ^ BIT(d, 0)) << 4;
120 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
126 for (i = 0; i < size; i++)
127 ecc[i] = calculate_parity_13_8(src[i]);
130 static u32 calc_chksum(void *buf, size_t size)
136 for (i = 0; i < size; i++) {
143 Physical organisation of data in NAND flash:
145 payload chunk 0 (may be empty)
146 ecc for metadata + payload chunk 0
148 ecc for payload chunk 1
151 ecc for payload chunk n
154 static int calc_bb_offset(nand_info_t *mtd, struct mx6_fcb *fcb)
157 int chunk_data_size = fcb->ecc_blockn_size * 8;
158 int chunk_ecc_size = (fcb->ecc_blockn_type << 1) * 13;
159 int chunk_total_size = chunk_data_size + chunk_ecc_size;
160 int bb_mark_chunk, bb_mark_chunk_offs;
162 bb_mark_offset = (mtd->writesize - fcb->metadata_size) * 8;
163 if (fcb->ecc_block0_size == 0)
164 bb_mark_offset -= (fcb->ecc_block0_type << 1) * 13;
166 bb_mark_chunk = bb_mark_offset / chunk_total_size;
167 bb_mark_chunk_offs = bb_mark_offset - (bb_mark_chunk * chunk_total_size);
168 if (bb_mark_chunk_offs > chunk_data_size) {
169 printf("Unsupported ECC layout; BB mark resides in ECC data: %u\n",
173 bb_mark_offset -= bb_mark_chunk * chunk_ecc_size;
174 return bb_mark_offset;
177 #define pr_fcb_val(p, n) debug("%s=%08x(%d)\n", #n, (p)->n, (p)->n)
179 static struct mx6_fcb *create_fcb(void *buf, int fw1_start_block,
180 int fw2_start_block, size_t fw_size)
182 struct gpmi_regs *gpmi_base = (struct gpmi_regs *)GPMI_BASE_ADDRESS;
183 struct bch_regs *bch_base = (struct bch_regs *)BCH_BASE_ADDRESS;
187 int bb_mark_bit_offs;
191 if (gpmi_base == NULL || bch_base == NULL) {
192 return ERR_PTR(-ENOMEM);
195 fl0 = readl(&bch_base->hw_bch_flash0layout0);
196 fl1 = readl(&bch_base->hw_bch_flash0layout1);
197 t0 = readl(&gpmi_base->hw_gpmi_timing0);
198 // t1 = readl(&gpmi_base->hw_gpmi_timing1);
200 metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
202 fcb = buf + ALIGN(metadata_size, 4);
203 fcb_offs = (void *)fcb - buf;
205 memset(buf, 0xff, fcb_offs);
206 memset(fcb, 0x00, sizeof(*fcb));
207 memset(fcb + 1, 0xff, mtd->erasesize - fcb_offs - sizeof(*fcb));
209 memset(buf, 0, fcb_offs);
210 memset(fcb, 0x00, sizeof(*fcb));
211 memset(fcb + 1, 0, mtd->erasesize - fcb_offs - sizeof(*fcb));
213 strncpy((char *)&fcb->fingerprint, "FCB ", 4);
214 fcb->version = cpu_to_be32(1);
216 fcb->disbb_search = 1;
219 fcb->timing.data_setup = BF_VAL(t0, GPMI_TIMING0_DATA_SETUP);
220 fcb->timing.data_hold = BF_VAL(t0, GPMI_TIMING0_DATA_HOLD);
221 fcb->timing.address_setup = BF_VAL(t0, GPMI_TIMING0_ADDRESS_SETUP);
223 fcb->timing.data_setup = 80;
224 fcb->timing.data_hold = 60;
225 fcb->timing.address_setup = 25;
226 fcb->timing.dsample_time = 6;
228 fcb->page_data_size = mtd->writesize;
229 fcb->total_page_size = mtd->writesize + mtd->oobsize;
230 fcb->sectors_per_block = mtd->erasesize / mtd->writesize;
232 fcb->ecc_block0_type = BF_VAL(fl0, BCH_FLASHLAYOUT0_ECC0);
233 fcb->ecc_block0_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_DATA0_SIZE) * 4;
234 fcb->ecc_blockn_type = BF_VAL(fl1, BCH_FLASHLAYOUT1_ECCN);
235 fcb->ecc_blockn_size = BF_VAL(fl1, BCH_FLASHLAYOUT1_DATAN_SIZE) * 4;
237 pr_fcb_val(fcb, ecc_block0_type);
238 pr_fcb_val(fcb, ecc_blockn_type);
239 pr_fcb_val(fcb, ecc_block0_size);
240 pr_fcb_val(fcb, ecc_blockn_size);
242 fcb->metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
243 fcb->ecc_blocks_per_page = BF_VAL(fl0, BCH_FLASHLAYOUT0_NBLOCKS);
244 fcb->bch_mode = readl(&bch_base->hw_bch_mode);
245 fcb->bch_type = 0; /* BCH20 */
247 fcb->fw1_start_page = fw1_start_block * mtd->erasesize / mtd->writesize;
248 fcb->fw1_sectors = DIV_ROUND_UP(fw_size, mtd->writesize);
250 if (fw2_start_block != 0 && fw2_start_block < mtd->size / mtd->erasesize) {
251 fcb->fw2_start_page = fw2_start_block * mtd->erasesize / mtd->writesize;
252 fcb->fw2_sectors = fcb->fw1_sectors;
255 fcb->dbbt_search_area = 1;
257 bb_mark_bit_offs = calc_bb_offset(mtd, fcb);
258 if (bb_mark_bit_offs < 0)
259 return ERR_PTR(bb_mark_bit_offs);
260 fcb->bb_mark_byte = bb_mark_bit_offs / 8;
261 fcb->bb_mark_startbit = bb_mark_bit_offs % 8;
262 fcb->bb_mark_phys_offset = mtd->writesize;
264 pr_fcb_val(fcb, bb_mark_byte);
265 pr_fcb_val(fcb, bb_mark_startbit);
266 pr_fcb_val(fcb, bb_mark_phys_offset);
268 fcb->checksum = calc_chksum(&fcb->fingerprint, 512 - 4);
272 static inline int find_fcb(void *ref, int page)
275 struct nand_chip *chip = mtd->priv;
276 void *buf = malloc(mtd->erasesize);
281 chip->select_chip(mtd, 0);
282 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
283 ret = chip->ecc.read_page_raw(mtd, chip, buf, page);
285 printf("Failed to read FCB from page %u: %d\n", page, ret);
288 chip->select_chip(mtd, -1);
289 if (memcmp(buf, ref, mtd->writesize) == 0) {
290 debug("Found FCB in page %u (%08x)\n",
291 page, page * mtd->writesize);
298 static int write_fcb(void *buf, int block)
301 struct nand_chip *chip = mtd->priv;
302 int page = block * mtd->erasesize / mtd->writesize;
304 ret = find_fcb(buf, page);
306 printf("FCB at block %d is up to date\n", block);
310 ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
312 printf("Failed to erase FCB block %u\n", block);
316 printf("Writing FCB to block %d @ %08x\n", block,
317 block * mtd->erasesize);
318 chip->select_chip(mtd, 0);
319 ret = chip->write_page(mtd, chip, buf, page, 0, 1);
321 printf("Failed to write FCB to block %u: %d\n", block, ret);
323 chip->select_chip(mtd, -1);
339 struct mx6_boot_data {
345 static int find_ivt(void *buf)
347 struct mx6_ivt *ivt_hdr = buf + 0x400;
349 if ((ivt_hdr->magic & 0xff0000ff) != 0x400000d1)
355 static inline void *reloc(void *dst, void *base, void *ptr)
357 return dst + (ptr - base);
360 static int patch_ivt(void *buf, size_t fsize)
362 struct mx6_ivt *ivt_hdr = buf + 0x400;
363 struct mx6_boot_data *boot_data;
365 if (!find_ivt(buf)) {
366 printf("No IVT found in image at %p\n", buf);
369 boot_data = reloc(ivt_hdr, ivt_hdr->self, ivt_hdr->boot_data);
370 boot_data->length = fsize;
375 #define chk_overlap(a,b) \
376 ((a##_start_block <= b##_end_block && \
377 a##_end_block >= b##_start_block) || \
378 (b##_start_block <= a##_end_block && \
379 b##_end_block >= a##_start_block))
381 #define fail_if_overlap(a,b,m1,m2) do { \
382 if (chk_overlap(a, b)) { \
383 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
384 m1, a##_start_block, a##_end_block, \
385 m2, b##_start_block, b##_end_block); \
386 /*return -EINVAL;*/ \
390 #ifndef CONFIG_ENV_OFFSET_REDUND
391 #define TOTAL_ENV_SIZE CONFIG_ENV_RANGE
393 #define TOTAL_ENV_SIZE (CONFIG_ENV_RANGE * 2)
396 #define pr_fcb_offset(n) printf("%s: %04x (%d)\n", #n, \
397 offsetof(struct mx6_fcb, n), offsetof(struct mx6_fcb, n))
399 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
403 int erase_size = mtd->erasesize;
404 int page_size = mtd->writesize;
411 unsigned long fcb_start_block = FCB_START_BLOCK;
412 unsigned long num_fcb_blocks = NUM_FCB_BLOCKS;
413 unsigned long fcb_end_block;
414 unsigned long mtd_num_blocks = mtd->size / mtd->erasesize;
415 unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
416 unsigned long env_end_block = env_start_block +
417 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, };
428 load_addr = getenv("fileaddr");
429 file_size = getenv("filesize");
431 if (argc < 2 && load_addr == NULL) {
432 printf("Load address not specified\n");
435 if (argc < 3 && file_size == NULL) {
436 printf("Image size not specified\n");
440 for (optind = 1; optind < argc; optind++) {
441 if (strcmp(argv[optind], "-b") == 0) {
442 if (optind >= argc - 1) {
443 printf("Option %s requires an argument\n",
448 fcb_start_block = simple_strtoul(argv[optind], NULL, 0);
449 if (fcb_start_block >= mtd_num_blocks) {
450 printf("Block number %lu is out of range: 0..%lu\n",
451 fcb_start_block, mtd_num_blocks - 1);
454 } else if (strcmp(argv[optind], "-n") == 0) {
455 if (optind >= argc - 1) {
456 printf("Option %s requires an argument\n",
461 num_fcb_blocks = simple_strtoul(argv[optind], NULL, 0);
462 if (num_fcb_blocks == 0) {
463 printf("Number of FCB blocks must be non-zero\n");
465 } else if (num_fcb_blocks > MAX_FCB_BLOCKS) {
466 printf("Extraneous number of FCB blocks; max. allowed: %u\n",
470 } else if (strcmp(argv[optind], "-f") == 0) {
471 if (optind >= argc - 1) {
472 printf("Option %s requires an argument\n",
477 fw1_start_block = simple_strtoul(argv[optind], NULL, 0);
478 if (fw1_start_block >= mtd_num_blocks) {
479 printf("Block number %lu is out of range: 0..%lu\n",
480 fw1_start_block, mtd_num_blocks - 1);
484 } else if (strcmp(argv[optind], "-r") == 0) {
485 if (optind < argc - 1 && argv[optind + 1][0] != '-') {
487 fw2_start_block = simple_strtoul(argv[optind],
489 if (fw2_start_block >= mtd_num_blocks) {
490 printf("Block number %lu is out of range: 0..%lu\n",
497 } else if (strcmp(argv[optind], "-e") == 0) {
498 if (optind >= argc - 1) {
499 printf("Option %s requires an argument\n",
504 extra_blocks = simple_strtoul(argv[optind], NULL, 0);
505 if (extra_blocks >= mtd_num_blocks) {
506 printf("Extra block count %lu is out of range: 0..%lu\n",
511 } else if (argv[optind][0] == '-') {
512 printf("Unrecognized option %s\n", argv[optind]);
521 addr = (void *)simple_strtoul(argv[optind], NULL, 16);
526 size = simple_strtoul(argv[optind], NULL, 16);
529 if (load_addr != NULL) {
530 addr = (void *)simple_strtoul(load_addr, NULL, 16);
531 printf("Using default load address %p\n", addr);
533 if (file_size != NULL) {
534 size = simple_strtoul(file_size, NULL, 16);
535 printf("Using default file size %08x\n", size);
537 fcb_end_block = fcb_start_block + num_fcb_blocks - 1;
538 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
541 fw1_start_block = fcb_end_block + CONFIG_SYS_NAND_U_BOOT_OFFS / mtd->erasesize;
542 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
543 if (chk_overlap(fw1, env)) {
544 fw1_start_block = env_end_block + 1;
545 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
548 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
551 if (fw2_set && fw2_start_block == 0) {
552 fw2_start_block = fw1_end_block + 1;
553 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
554 if (chk_overlap(fw2, env)) {
555 fw2_start_block = env_end_block + 1;
556 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
559 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
562 fail_if_overlap(fcb, env, "FCB", "Environment");
563 fail_if_overlap(fcb, fw1, "FCB", "FW1");
564 fail_if_overlap(fw1, env, "FW1", "Environment");
566 fail_if_overlap(fcb, fw2, "FCB", "FW2");
567 fail_if_overlap(fw2, env, "FW2", "Environment");
568 fail_if_overlap(fw1, fw2, "FW1", "FW2");
571 buf = malloc(erase_size);
573 printf("Failed to allocate buffer\n");
577 /* search for first non-bad block in FW1 block range */
578 while (fw1_start_block <= fw1_end_block) {
579 if (!nand_block_isbad(mtd, fw1_start_block * mtd->erasesize))
583 if (fw1_end_block - fw1_start_block + 1 < fw_num_blocks) {
584 printf("Too many bad blocks in FW1 block range: %lu..%lu\n",
585 fw1_end_block + 1 - fw_num_blocks - extra_blocks,
590 /* search for first non-bad block in FW2 block range */
591 while (fw2_set && fw2_start_block <= fw2_end_block) {
592 if (!nand_block_isbad(mtd, fw2_start_block * mtd->erasesize))
596 if (fw2_end_block - fw2_start_block + 1 < fw_num_blocks) {
597 printf("Too many bad blocks in FW2 area %08lx..%08lx\n",
598 fw2_end_block + 1 - fw_num_blocks - extra_blocks,
603 fcb = create_fcb(buf, fw1_start_block, fw2_start_block,
604 ALIGN(size, mtd->writesize));
606 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
609 encode_hamming_13_8(fcb, (void *)fcb + 512, 512);
611 for (block = fcb_start_block; block <= fcb_end_block; block++) {
612 if (nand_block_isbad(mtd, block * mtd->erasesize)) {
613 if (block == fcb_start_block)
617 ret = write_fcb(buf, block);
619 printf("Failed to write FCB to block %u\n", block);
626 printf("Could not write FCB to flash\n");
630 ret = patch_ivt(addr, size);
635 printf("Programming U-Boot image from %p to block %lu\n",
636 addr, fw1_start_block);
637 if (size & (page_size - 1)) {
638 memset(addr + size, 0xff, size & (page_size - 1));
639 size = ALIGN(size, page_size);
642 erase_opts.offset = fcb->fw1_start_page * page_size;
643 erase_opts.length = ALIGN(size, erase_size) +
644 extra_blocks * mtd->erasesize;
645 erase_opts.quiet = 1;
647 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
648 erase_opts.offset + erase_opts.length - 1);
650 ret = nand_erase_opts(mtd, &erase_opts);
652 printf("Failed to erase flash: %d\n", ret);
655 printf("Programming flash @ %08x..%08x from %p\n",
656 fcb->fw1_start_page * page_size,
657 fcb->fw1_start_page * page_size + size, addr);
658 ret = nand_write_skip_bad(mtd, fcb->fw1_start_page * page_size,
659 &size, addr, WITH_DROP_FFS);
661 printf("Failed to program flash: %d\n", ret);
664 if (fw2_start_block == 0) {
668 printf("Programming redundant U-Boot image to block %lu\n",
670 erase_opts.offset = fcb->fw2_start_page * page_size;
671 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
672 erase_opts.offset + erase_opts.length - 1);
674 ret = nand_erase_opts(mtd, &erase_opts);
676 printf("Failed to erase flash: %d\n", ret);
679 printf("Programming flash @ %08x..%08x from %p\n",
680 fcb->fw2_start_page * page_size,
681 fcb->fw2_start_page * page_size + size, addr);
682 ret = nand_write_skip_bad(mtd, fcb->fw2_start_page * page_size,
683 &size, addr, WITH_DROP_FFS);
685 printf("Failed to program flash: %d\n", ret);
691 U_BOOT_CMD(romupdate, 11, 0, do_update,
692 "Creates an FCB data structure and writes an U-Boot image to flash\n",
693 "[-b #] [-n #] [-f #] [-r [#]] [<address>] [<length>]\n"
694 "\t-b #\tfirst FCB block number (default 0)\n"
695 "\t-n #\ttotal number of FCB blocks (default 1)\n"
696 "\t-f #\twrite bootloader image at block #\n"
697 "\t-r\twrite redundant bootloader image at next free block after first image\n"
698 "\t-r #\twrite redundant bootloader image at block #\n"
699 "\t-e #\tspecify number of redundant blocks per boot loader image\n"
700 "\t<address>\tRAM address of bootloader image (default: ${fileaddr}\n"
701 "\t<length>\tlength of bootloader image in RAM (default: ${filesize}"