10 #include <asm/arch/regs-base.h>
11 #include <asm/imx-common/regs-gpmi.h>
12 #include <asm/imx-common/regs-bch.h>
14 #define FCB_START_BLOCK 0
15 #define NUM_FCB_BLOCKS 1
16 #define MAX_FCB_BLOCKS 32768
18 struct mx28_nand_timing {
33 struct mx28_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 rsrvd[6]; /* not used by ROM code */
57 u32 bb_mark_phys_offset;
60 struct mx28_dbbt_header {
75 #define BF_VAL(v, bf) (((v) & bf##_MASK) >> bf##_OFFSET)
77 static nand_info_t *mtd = &nand_info[0];
81 #define BIT(v,n) (((v) >> (n)) & 0x1)
83 static u8 calculate_parity_13_8(u8 d)
87 p |= (BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 3) ^ BIT(d, 2)) << 0;
88 p |= (BIT(d, 7) ^ BIT(d, 5) ^ BIT(d, 4) ^ BIT(d, 2) ^ BIT(d, 1)) << 1;
89 p |= (BIT(d, 7) ^ BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 1) ^ BIT(d, 0)) << 2;
90 p |= (BIT(d, 7) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 0)) << 3;
91 p |= (BIT(d, 6) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 2) ^ BIT(d, 1) ^ BIT(d, 0)) << 4;
95 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
101 for (i = 0; i < size; i++)
102 ecc[i] = calculate_parity_13_8(src[i]);
105 static u32 calc_chksum(void *buf, size_t size)
111 for (i = 0; i < size; i++) {
118 Physical organisation of data in NAND flash:
120 payload chunk 0 (may be empty)
121 ecc for metadata + payload chunk 0
123 ecc for payload chunk 1
126 ecc for payload chunk n
129 static int calc_bb_offset(nand_info_t *mtd, struct mx28_fcb *fcb)
132 int chunk_data_size = fcb->ecc_blockn_size * 8;
133 int chunk_ecc_size = (fcb->ecc_blockn_type << 1) * 13;
134 int chunk_total_size = chunk_data_size + chunk_ecc_size;
135 int bb_mark_chunk, bb_mark_chunk_offs;
137 bb_mark_offset = (mtd->writesize - fcb->metadata_size) * 8;
138 if (fcb->ecc_block0_size == 0)
139 bb_mark_offset -= (fcb->ecc_block0_type << 1) * 13;
141 bb_mark_chunk = bb_mark_offset / chunk_total_size;
142 bb_mark_chunk_offs = bb_mark_offset - (bb_mark_chunk * chunk_total_size);
143 if (bb_mark_chunk_offs > chunk_data_size) {
144 printf("Unsupported ECC layout; BB mark resides in ECC data: %u\n",
148 bb_mark_offset -= bb_mark_chunk * chunk_ecc_size;
149 return bb_mark_offset;
152 static struct mx28_fcb *create_fcb(void *buf, int fw1_start_block,
153 int fw2_start_block, size_t fw_size)
155 struct gpmi_regs *gpmi_base = (void *)GPMI_BASE_ADDRESS;
156 struct bch_regs *bch_base = (void *)BCH_BASE_ADDRESS;
160 int bb_mark_bit_offs;
161 struct mx28_fcb *fcb;
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 /* ROM code assumes GPMI clock of 25 MHz */
185 fcb->timing.data_setup = BF_VAL(t0, GPMI_TIMING0_DATA_SETUP) * 40;
186 fcb->timing.data_hold = BF_VAL(t0, GPMI_TIMING0_DATA_HOLD) * 40;
187 fcb->timing.address_setup = BF_VAL(t0, GPMI_TIMING0_ADDRESS_SETUP) * 40;
189 fcb->page_data_size = mtd->writesize;
190 fcb->total_page_size = mtd->writesize + mtd->oobsize;
191 fcb->sectors_per_block = mtd->erasesize / mtd->writesize;
193 fcb->ecc_block0_type = BF_VAL(fl0, BCH_FLASHLAYOUT0_ECC0);
194 fcb->ecc_block0_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_DATA0_SIZE);
195 fcb->ecc_blockn_type = BF_VAL(fl1, BCH_FLASHLAYOUT1_ECCN);
196 fcb->ecc_blockn_size = BF_VAL(fl1, BCH_FLASHLAYOUT1_DATAN_SIZE);
198 fcb->metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
199 fcb->ecc_blocks_per_page = BF_VAL(fl0, BCH_FLASHLAYOUT0_NBLOCKS);
200 fcb->bch_mode = readl(&bch_base->hw_bch_mode);
203 fcb->patch_sectors = 0;
205 fcb->fw1_start_page = fw1_start_block * mtd->erasesize / mtd->writesize;
206 fcb->fw1_sectors = DIV_ROUND_UP(fw_size, mtd->writesize);
208 if (fw2_start_block != 0 && fw2_start_block < mtd->size / mtd->erasesize) {
209 fcb->fw2_start_page = fw2_start_block * mtd->erasesize / mtd->writesize;
210 fcb->fw2_sectors = fcb->fw1_sectors;
213 fcb->dbbt_search_area = 1;
215 bb_mark_bit_offs = calc_bb_offset(mtd, fcb);
216 if (bb_mark_bit_offs < 0)
217 return ERR_PTR(bb_mark_bit_offs);
218 fcb->bb_mark_byte = bb_mark_bit_offs / 8;
219 fcb->bb_mark_startbit = bb_mark_bit_offs % 8;
220 fcb->bb_mark_phys_offset = mtd->writesize;
222 fcb->checksum = calc_chksum(&fcb->fingerprint, 512 - 4);
226 static int find_fcb(void *ref, int page)
229 struct nand_chip *chip = mtd->priv;
230 void *buf = malloc(mtd->erasesize);
235 chip->select_chip(mtd, 0);
236 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
237 ret = chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
239 printf("Failed to read FCB from page %u: %d\n", page, ret);
242 chip->select_chip(mtd, -1);
243 if (memcmp(buf, ref, mtd->writesize) == 0) {
244 debug("Found FCB in page %u (%08x)\n",
245 page, page * mtd->writesize);
252 static int write_fcb(void *buf, int block)
255 struct nand_chip *chip = mtd->priv;
256 int page = block * mtd->erasesize / mtd->writesize;
258 ret = find_fcb(buf, page);
260 printf("FCB at block %d is up to date\n", block);
264 ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
266 printf("Failed to erase FCB block %u\n", block);
270 printf("Writing FCB to block %d @ %08x\n", block,
271 block * mtd->erasesize);
272 chip->select_chip(mtd, 0);
273 ret = chip->write_page(mtd, chip, buf, 1, page, 0, 1);
275 printf("Failed to write FCB to block %u: %d\n", block, ret);
277 chip->select_chip(mtd, -1);
281 static size_t count_good_blocks(int start, int end)
283 size_t max_len = (end - start + 1);
286 for (block = start; block <= end; block++) {
287 if (nand_block_isbad(mtd, block * mtd->erasesize))
293 #define chk_overlap(a,b) \
294 ((a##_start_block <= b##_end_block && \
295 a##_end_block >= b##_start_block) || \
296 (b##_start_block <= a##_end_block && \
297 b##_end_block >= a##_start_block))
299 #define fail_if_overlap(a,b,m1,m2) do { \
300 if (chk_overlap(a, b)) { \
301 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
302 m1, a##_start_block, a##_end_block, \
303 m2, b##_start_block, b##_end_block); \
308 #ifdef CONFIG_ENV_IS_IN_NAND
309 #ifndef CONFIG_ENV_OFFSET_REDUND
310 #define TOTAL_ENV_SIZE CONFIG_ENV_RANGE
312 #define TOTAL_ENV_SIZE (CONFIG_ENV_RANGE * 2)
316 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
319 int erase_size = mtd->erasesize;
320 int page_size = mtd->writesize;
326 struct mx28_fcb *fcb;
327 unsigned long fcb_start_block = FCB_START_BLOCK;
328 unsigned long num_fcb_blocks = NUM_FCB_BLOCKS;
329 unsigned long fcb_end_block;
330 unsigned long mtd_num_blocks = mtd->size / mtd->erasesize;
331 #ifdef CONFIG_ENV_IS_IN_NAND
332 unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
333 unsigned long env_end_block = env_start_block +
334 DIV_ROUND_UP(TOTAL_ENV_SIZE, mtd->erasesize) - 1;
339 unsigned long fw1_start_block = 0, fw1_end_block;
340 unsigned long fw2_start_block = 0, fw2_end_block;
341 unsigned long fw_num_blocks;
342 unsigned long extra_blocks = 2;
343 nand_erase_options_t erase_opts = { 0, };
344 size_t max_len1, max_len2;
347 for (optind = 1; optind < argc; optind++) {
348 if (strcmp(argv[optind], "-b") == 0) {
349 if (optind >= argc - 1) {
350 printf("Option %s requires an argument\n", argv[optind]);
354 fcb_start_block = simple_strtoul(argv[optind], NULL, 0);
355 if (fcb_start_block >= mtd_num_blocks) {
356 printf("Block number %lu is out of range: 0..%lu\n",
357 fcb_start_block, mtd_num_blocks - 1);
360 } else if (strcmp(argv[optind], "-n") == 0) {
361 if (optind >= argc - 1) {
362 printf("Option %s requires an argument\n", argv[optind]);
366 num_fcb_blocks = simple_strtoul(argv[optind], NULL, 0);
367 if (num_fcb_blocks > MAX_FCB_BLOCKS) {
368 printf("Extraneous number of FCB blocks; max. allowed: %u\n",
372 } else if (strcmp(argv[optind], "-f") == 0) {
373 if (optind >= argc - 1) {
374 printf("Option %s requires an argument\n",
379 fw1_start_block = simple_strtoul(argv[optind], NULL, 0);
380 if (fw1_start_block >= mtd_num_blocks) {
381 printf("Block number %lu is out of range: 0..%lu\n",
382 fw1_start_block, mtd_num_blocks - 1);
386 } else if (strcmp(argv[optind], "-r") == 0) {
387 if (optind < argc - 1 && argv[optind + 1][0] != '-') {
389 fw2_start_block = simple_strtoul(argv[optind],
391 if (fw2_start_block >= mtd_num_blocks) {
392 printf("Block number %lu is out of range: 0..%lu\n",
399 } else if (strcmp(argv[optind], "-e") == 0) {
400 if (optind >= argc - 1) {
401 printf("Option %s requires an argument\n",
406 extra_blocks = simple_strtoul(argv[optind], NULL, 0);
407 if (extra_blocks >= mtd_num_blocks) {
408 printf("Extra block count %lu is out of range: 0..%lu\n",
413 } else if (argv[optind][0] == '-') {
414 printf("Unrecognized option %s\n", argv[optind]);
421 load_addr = getenv("fileaddr");
422 file_size = getenv("filesize");
424 if (argc - optind < 1 && load_addr == NULL) {
425 printf("Load address not specified\n");
428 if (argc - optind < 2 && file_size == NULL) {
429 printf("WARNING: Image size not specified; overwriting whole uboot partition\n");
433 addr = (void *)simple_strtoul(argv[optind], NULL, 16);
438 size = simple_strtoul(argv[optind], NULL, 16);
441 if (load_addr != NULL) {
442 addr = (void *)simple_strtoul(load_addr, NULL, 16);
443 printf("Using default load address %p\n", addr);
445 if (file_size != NULL) {
446 size = simple_strtoul(file_size, NULL, 16);
447 printf("Using default file size %08x\n", size);
449 fcb_end_block = fcb_start_block + num_fcb_blocks - 1;
451 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
453 fw_num_blocks = CONFIG_U_BOOT_IMG_SIZE / mtd->erasesize - extra_blocks;
456 fw1_start_block = fcb_end_block + 1;
457 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
459 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
462 if (fw2_set && fw2_start_block == 0) {
463 fw2_start_block = fw1_end_block + 1;
464 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
466 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
469 #ifdef CONFIG_ENV_IS_IN_NAND
470 fail_if_overlap(fcb, env, "FCB", "Environment");
471 fail_if_overlap(fw1, env, "FW1", "Environment");
473 fail_if_overlap(fcb, fw1, "FCB", "FW1");
475 fail_if_overlap(fcb, fw2, "FCB", "FW2");
476 #ifdef CONFIG_ENV_IS_IN_NAND
477 fail_if_overlap(fw2, env, "FW2", "Environment");
479 fail_if_overlap(fw1, fw2, "FW1", "FW2");
482 buf = malloc(erase_size);
484 printf("Failed to allocate buffer\n");
488 /* search for bad blocks in FW1 block range */
489 max_len1 = count_good_blocks(fw1_start_block, fw1_end_block);
490 printf("%u good blocks in %lu..%lu\n",
491 max_len1, fw1_start_block, fw1_end_block);
492 if (fw_num_blocks > max_len1) {
493 printf("Too many bad blocks in FW1 block range: %lu..%lu; max blocks: %u\n",
494 fw1_end_block + 1 - fw_num_blocks - extra_blocks,
495 fw1_end_block, max_len1);
499 /* search for bad blocks in FW2 block range */
500 max_len2 = count_good_blocks(fw2_start_block, fw2_end_block);
501 if (fw2_start_block > 0 && fw_num_blocks > max_len2) {
502 printf("Too many bad blocks in FW2 block range: %lu..%lu\n",
503 fw2_end_block + 1 - fw_num_blocks - extra_blocks,
508 fcb = create_fcb(buf, fw1_start_block, fw2_start_block,
509 ALIGN(fw_num_blocks * mtd->erasesize, mtd->writesize));
511 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
514 encode_hamming_13_8(fcb, (void *)fcb + 512, 512);
516 ret = write_fcb(buf, fcb_start_block);
518 printf("Failed to write FCB to block %lu\n", fcb_start_block);
522 printf("Programming U-Boot image from %p to block %lu\n",
523 addr, fw1_start_block);
524 if (size & (page_size - 1)) {
525 memset(addr + size, 0xff, size & (page_size - 1));
526 size = ALIGN(size, page_size);
529 erase_opts.offset = fcb->fw1_start_page * page_size;
530 erase_opts.length = (fw1_end_block - fw1_start_block + 1) *
532 erase_opts.quiet = 1;
534 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
535 erase_opts.offset + erase_opts.length - 1);
537 ret = nand_erase_opts(mtd, &erase_opts);
539 printf("Failed to erase flash: %d\n", ret);
543 max_len1 *= mtd->erasesize;
547 printf("Programming flash @ %08x..%08x from %p\n",
548 fcb->fw1_start_page * page_size,
549 fcb->fw1_start_page * page_size + max_len1 - 1, addr);
550 ret = nand_write_skip_bad(mtd, fcb->fw1_start_page * page_size,
551 &max_len1, &actual, erase_opts.length, addr,
553 if (ret || actual < size) {
554 printf("Failed to program flash: %d\n", ret);
557 if (fw2_start_block == 0) {
561 printf("Programming redundant U-Boot image to block %lu\n",
563 erase_opts.offset = fcb->fw2_start_page * page_size;
564 erase_opts.length = (fw2_end_block - fw2_start_block + 1) *
566 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
567 erase_opts.offset + erase_opts.length - 1);
569 ret = nand_erase_opts(mtd, &erase_opts);
571 printf("Failed to erase flash: %d\n", ret);
575 max_len2 *= mtd->erasesize;
578 printf("Programming flash @ %08x..%08x from %p\n",
579 fcb->fw2_start_page * page_size,
580 fcb->fw2_start_page * page_size + max_len2 - 1, addr);
581 ret = nand_write_skip_bad(mtd, fcb->fw2_start_page * page_size,
582 &max_len2, &actual, erase_opts.length, addr,
584 if (ret || actual < size) {
585 printf("Failed to program flash: %d\n", ret);
591 U_BOOT_CMD(romupdate, 11, 0, do_update,
592 "Creates an FCB data structure and writes an U-Boot image to flash",
593 "[-b #] [-n #] [-f #] [-r [#]] [<address>] [<length>]\n"
594 "\t-b #\tfirst FCB block number (default 0)\n"
595 "\t-n #\ttotal number of FCB blocks (default 1)\n"
596 "\t-f #\twrite bootloader image at block #\n"
597 "\t-r\twrite redundant bootloader image at next free block after first image\n"
598 "\t-r #\twrite redundant bootloader image at block #\n"
599 "\t-e #\tspecify number of redundant blocks per boot loader image (default 2)\n"
600 "\t<address>\tRAM address of bootloader image (default: ${fileaddr}\n"
601 "\t<length>\tlength of bootloader image in RAM (default: ${filesize}"