2 * Copyright (C) 2011-2014 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/regs-base.h>
29 #include <asm/imx-common/regs-gpmi.h>
30 #include <asm/imx-common/regs-bch.h>
32 struct mx28_nand_timing {
47 struct mx28_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 rsrvd[6]; /* not used by ROM code */
71 u32 bb_mark_phys_offset;
74 #define BF_VAL(v, bf) (((v) & bf##_MASK) >> bf##_OFFSET)
76 static nand_info_t *mtd = &nand_info[0];
80 #define BIT(v,n) (((v) >> (n)) & 0x1)
82 static u8 calculate_parity_13_8(u8 d)
86 p |= (BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 3) ^ BIT(d, 2)) << 0;
87 p |= (BIT(d, 7) ^ BIT(d, 5) ^ BIT(d, 4) ^ BIT(d, 2) ^ BIT(d, 1)) << 1;
88 p |= (BIT(d, 7) ^ BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 1) ^ BIT(d, 0)) << 2;
89 p |= (BIT(d, 7) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 0)) << 3;
90 p |= (BIT(d, 6) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 2) ^ BIT(d, 1) ^ BIT(d, 0)) << 4;
94 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
100 for (i = 0; i < size; i++)
101 ecc[i] = calculate_parity_13_8(src[i]);
104 static u32 calc_chksum(void *buf, size_t size)
110 for (i = 0; i < size; i++) {
117 Physical organisation of data in NAND flash:
119 payload chunk 0 (may be empty)
120 ecc for metadata + payload chunk 0
122 ecc for payload chunk 1
125 ecc for payload chunk n
128 static int calc_bb_offset(nand_info_t *mtd, struct mx28_fcb *fcb)
131 int chunk_data_size = fcb->ecc_blockn_size * 8;
132 int chunk_ecc_size = (fcb->ecc_blockn_type << 1) * 13;
133 int chunk_total_size = chunk_data_size + chunk_ecc_size;
134 int bb_mark_chunk, bb_mark_chunk_offs;
136 bb_mark_offset = (mtd->writesize - fcb->metadata_size) * 8;
137 if (fcb->ecc_block0_size == 0)
138 bb_mark_offset -= (fcb->ecc_block0_type << 1) * 13;
140 bb_mark_chunk = bb_mark_offset / chunk_total_size;
141 bb_mark_chunk_offs = bb_mark_offset - (bb_mark_chunk * chunk_total_size);
142 if (bb_mark_chunk_offs > chunk_data_size) {
143 printf("Unsupported ECC layout; BB mark resides in ECC data: %u\n",
147 bb_mark_offset -= bb_mark_chunk * chunk_ecc_size;
148 return bb_mark_offset;
152 * return number of blocks to skip for a contiguous partition
155 static int find_contig_space(int block, int num_blocks, int max_blocks)
159 int last = block + max_blocks;
161 debug("Searching %u contiguous blocks from %d..%d\n",
162 num_blocks, block, block + max_blocks - 1);
163 for (; block < last; block++) {
164 if (nand_block_isbad(mtd, block * mtd->erasesize)) {
167 debug("Skipping %u blocks to %u\n",
171 if (found >= num_blocks) {
172 debug("Found %u good blocks from %d..%d\n",
173 found, block - found + 1, block);
181 #define pr_fcb_val(p, n) debug("%s=%08x(%d)\n", #n, (p)->n, (p)->n)
183 static struct mx28_fcb *create_fcb(void *buf, int fw1_start_block,
184 int fw2_start_block, int fw_num_blocks)
186 struct gpmi_regs *gpmi_base = (void *)GPMI_BASE_ADDRESS;
187 struct bch_regs *bch_base = (void *)BCH_BASE_ADDRESS;
191 int bb_mark_bit_offs;
192 struct mx28_fcb *fcb;
195 if (gpmi_base == NULL || bch_base == NULL) {
196 return ERR_PTR(-ENOMEM);
199 fl0 = readl(&bch_base->hw_bch_flash0layout0);
200 fl1 = readl(&bch_base->hw_bch_flash0layout1);
201 t0 = readl(&gpmi_base->hw_gpmi_timing0);
203 metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
205 fcb = buf + ALIGN(metadata_size, 4);
206 fcb_offs = (void *)fcb - buf;
208 memset(buf, 0xff, fcb_offs);
209 memset(fcb, 0x00, sizeof(*fcb));
210 memset(fcb + 1, 0xff, mtd->erasesize - fcb_offs - sizeof(*fcb));
212 strncpy((char *)&fcb->fingerprint, "FCB ", 4);
213 fcb->version = cpu_to_be32(1);
215 /* ROM code assumes GPMI clock of 25 MHz */
216 fcb->timing.data_setup = BF_VAL(t0, GPMI_TIMING0_DATA_SETUP) * 40;
217 fcb->timing.data_hold = BF_VAL(t0, GPMI_TIMING0_DATA_HOLD) * 40;
218 fcb->timing.address_setup = BF_VAL(t0, GPMI_TIMING0_ADDRESS_SETUP) * 40;
220 fcb->page_data_size = mtd->writesize;
221 fcb->total_page_size = mtd->writesize + mtd->oobsize;
222 fcb->sectors_per_block = mtd->erasesize / mtd->writesize;
224 fcb->ecc_block0_type = BF_VAL(fl0, BCH_FLASHLAYOUT0_ECC0);
225 fcb->ecc_block0_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_DATA0_SIZE);
226 fcb->ecc_blockn_type = BF_VAL(fl1, BCH_FLASHLAYOUT1_ECCN);
227 fcb->ecc_blockn_size = BF_VAL(fl1, BCH_FLASHLAYOUT1_DATAN_SIZE);
229 pr_fcb_val(fcb, ecc_block0_type);
230 pr_fcb_val(fcb, ecc_blockn_type);
231 pr_fcb_val(fcb, ecc_block0_size);
232 pr_fcb_val(fcb, ecc_blockn_size);
234 fcb->metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
235 fcb->ecc_blocks_per_page = BF_VAL(fl0, BCH_FLASHLAYOUT0_NBLOCKS);
236 fcb->bch_mode = readl(&bch_base->hw_bch_mode);
238 fcb->fw1_start_page = fw1_start_block * fcb->sectors_per_block;
239 fcb->fw1_sectors = fw_num_blocks * fcb->sectors_per_block;
240 pr_fcb_val(fcb, fw1_start_page);
241 pr_fcb_val(fcb, fw1_sectors);
243 if (fw2_start_block != 0 && fw2_start_block < mtd->size / mtd->erasesize) {
244 fcb->fw2_start_page = fw2_start_block * fcb->sectors_per_block;
245 fcb->fw2_sectors = fcb->fw1_sectors;
246 pr_fcb_val(fcb, fw2_start_page);
247 pr_fcb_val(fcb, fw2_sectors);
250 fcb->dbbt_search_area = 1;
252 bb_mark_bit_offs = calc_bb_offset(mtd, fcb);
253 if (bb_mark_bit_offs < 0)
254 return ERR_PTR(bb_mark_bit_offs);
255 fcb->bb_mark_byte = bb_mark_bit_offs / 8;
256 fcb->bb_mark_startbit = bb_mark_bit_offs % 8;
257 fcb->bb_mark_phys_offset = mtd->writesize;
259 pr_fcb_val(fcb, bb_mark_byte);
260 pr_fcb_val(fcb, bb_mark_startbit);
261 pr_fcb_val(fcb, bb_mark_phys_offset);
263 fcb->checksum = calc_chksum(&fcb->fingerprint, 512 - 4);
267 static int find_fcb(void *ref, int page)
270 struct nand_chip *chip = mtd->priv;
271 void *buf = malloc(mtd->erasesize);
276 chip->select_chip(mtd, 0);
277 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
278 ret = chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
280 printf("Failed to read FCB from page %u: %d\n", page, ret);
283 if (memcmp(buf, ref, mtd->writesize) == 0) {
284 debug("Found FCB in page %u (%08x)\n",
285 page, page * mtd->writesize);
289 chip->select_chip(mtd, -1);
294 static int write_fcb(void *buf, int block)
297 struct nand_chip *chip = mtd->priv;
298 int page = block * mtd->erasesize / mtd->writesize;
300 ret = find_fcb(buf, page);
302 printf("FCB at block %d is up to date\n", block);
306 ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
308 printf("Failed to erase FCB block %u\n", block);
312 printf("Writing FCB to block %d @ %08llx\n", block,
313 (u64)block * mtd->erasesize);
314 chip->select_chip(mtd, 0);
315 ret = chip->write_page(mtd, chip, buf, 1, page, 0, 1);
317 printf("Failed to write FCB to block %u: %d\n", block, ret);
319 chip->select_chip(mtd, -1);
323 #define chk_overlap(a,b) \
324 ((a##_start_block <= b##_end_block && \
325 a##_end_block >= b##_start_block) || \
326 (b##_start_block <= a##_end_block && \
327 b##_end_block >= a##_start_block))
329 #define fail_if_overlap(a,b,m1,m2) do { \
330 if (chk_overlap(a, b)) { \
331 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
332 m1, a##_start_block, a##_end_block, \
333 m2, b##_start_block, b##_end_block); \
338 static int tx28_prog_uboot(void *addr, int start_block, int skip,
339 size_t size, size_t max_len)
342 nand_erase_options_t erase_opts = { 0, };
344 size_t prg_length = max_len - skip * mtd->erasesize;
345 int prg_start = (start_block + skip) * mtd->erasesize;
347 erase_opts.offset = start_block * mtd->erasesize;
348 erase_opts.length = max_len;
349 erase_opts.quiet = 1;
351 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
352 erase_opts.offset + erase_opts.length - 1);
353 ret = nand_erase_opts(mtd, &erase_opts);
355 printf("Failed to erase flash: %d\n", ret);
359 printf("Programming flash @ %08llx..%08llx from %p\n",
360 (u64)start_block * mtd->erasesize,
361 (u64)start_block * mtd->erasesize + size - 1, addr);
363 ret = nand_write_skip_bad(mtd, prg_start, &actual, NULL,
364 prg_length, addr, WITH_DROP_FFS);
366 printf("Failed to program flash: %d\n", ret);
370 printf("Could only write %u of %u bytes\n", actual, size);
376 #ifdef CONFIG_ENV_IS_IN_NAND
377 #ifndef CONFIG_ENV_OFFSET_REDUND
378 #define TOTAL_ENV_SIZE CONFIG_ENV_RANGE
380 #define TOTAL_ENV_SIZE (CONFIG_ENV_RANGE * 2)
384 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
387 const unsigned long fcb_start_block = 0, fcb_end_block = 0;
388 int erase_size = mtd->erasesize;
389 int page_size = mtd->writesize;
395 struct mx28_fcb *fcb;
396 unsigned long mtd_num_blocks = mtd->size / mtd->erasesize;
397 #ifdef CONFIG_ENV_IS_IN_NAND
398 unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
399 unsigned long env_end_block = env_start_block +
400 DIV_ROUND_UP(TOTAL_ENV_SIZE, mtd->erasesize) - 1;
404 unsigned long fw1_start_block = 0, fw1_end_block;
405 unsigned long fw2_start_block = 0, fw2_end_block;
406 unsigned long fw_num_blocks;
407 int fw1_skip, fw2_skip;
408 unsigned long extra_blocks = 0;
409 size_t max_len1, max_len2;
410 struct mtd_device *dev;
411 struct part_info *part_info;
412 struct part_info *redund_part_info;
413 const char *uboot_part = "u-boot";
414 const char *redund_part = NULL;
418 ret = mtdparts_init();
422 for (optind = 1; optind < argc; optind++) {
425 if (strcmp(argv[optind], "-f") == 0) {
426 if (optind >= argc - 1) {
427 printf("Option %s requires an argument\n",
432 fw1_start_block = simple_strtoul(argv[optind], &endp, 0);
434 uboot_part = argv[optind];
438 if (fw1_start_block >= mtd_num_blocks) {
439 printf("Block number %lu is out of range: 0..%lu\n",
440 fw1_start_block, mtd_num_blocks - 1);
443 } else if (strcmp(argv[optind], "-r") == 0) {
445 if (optind < argc - 1 && argv[optind + 1][0] != '-') {
447 fw2_start_block = simple_strtoul(argv[optind],
450 redund_part = argv[optind];
453 if (fw2_start_block >= mtd_num_blocks) {
454 printf("Block number %lu is out of range: 0..%lu\n",
460 } else if (strcmp(argv[optind], "-e") == 0) {
461 if (optind >= argc - 1) {
462 printf("Option %s requires an argument\n",
467 extra_blocks = simple_strtoul(argv[optind], NULL, 0);
468 if (extra_blocks >= mtd_num_blocks) {
469 printf("Extra block count %lu is out of range: 0..%lu\n",
474 } else if (argv[optind][0] == '-') {
475 printf("Unrecognized option %s\n", argv[optind]);
482 load_addr = getenv("fileaddr");
483 file_size = getenv("filesize");
485 if (argc - optind < 1 && load_addr == NULL) {
486 printf("Load address not specified\n");
489 if (argc - optind < 2 && file_size == NULL) {
490 printf("WARNING: Image size not specified; overwriting whole uboot partition\n");
494 addr = (void *)simple_strtoul(argv[optind], NULL, 16);
499 size = simple_strtoul(argv[optind], NULL, 16);
502 if (load_addr != NULL) {
503 addr = (void *)simple_strtoul(load_addr, NULL, 16);
504 printf("Using default load address %p\n", addr);
506 if (file_size != NULL) {
507 size = simple_strtoul(file_size, NULL, 16);
508 printf("Using default file size %08x\n", size);
511 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
513 fw_num_blocks = part_info->size / mtd->erasesize -
515 size = fw_num_blocks * mtd->erasesize;
519 ret = find_dev_and_part(uboot_part, &dev, &part_num,
522 printf("Failed to find '%s' partition: %d\n",
526 fw1_start_block = part_info->offset / mtd->erasesize;
527 max_len1 = part_info->size;
529 max_len1 = (fw_num_blocks + extra_blocks) * mtd->erasesize;
533 ret = find_dev_and_part(redund_part, &dev, &redund_part_num,
536 printf("Failed to find '%s' partition: %d\n",
540 fw2_start_block = redund_part_info->offset / mtd->erasesize;
541 max_len2 = redund_part_info->size;
542 } else if (fw2_set) {
543 max_len2 = (fw_num_blocks + extra_blocks) * mtd->erasesize;
548 fw1_skip = find_contig_space(fw1_start_block, fw_num_blocks,
549 max_len1 / mtd->erasesize);
551 printf("Could not find %lu contiguous good blocks for fw image\n",
554 #ifdef CONFIG_ENV_IS_IN_NAND
555 if (part_info->offset <= CONFIG_ENV_OFFSET + TOTAL_ENV_SIZE) {
556 printf("Use a different partition\n");
558 printf("Increase the size of the '%s' partition\n",
562 printf("Increase the size of the '%s' partition\n",
566 printf("Increase the number of spare blocks to use with the '-e' option\n");
570 fw1_end_block = fw1_start_block + fw1_skip + fw_num_blocks - 1;
572 if (fw2_set && fw2_start_block == 0)
573 fw2_start_block = fw1_end_block + 1;
574 if (fw2_start_block > 0) {
575 fw2_skip = find_contig_space(fw2_start_block, fw_num_blocks,
576 max_len2 / mtd->erasesize);
578 printf("Could not find %lu contiguous good blocks for redundant fw image\n",
581 printf("Increase the size of the '%s' partition or use a different partition\n",
584 printf("Increase the number of spare blocks to use with the '-e' option\n");
591 fw2_end_block = fw2_start_block + fw2_skip + fw_num_blocks - 1;
593 #ifdef CONFIG_ENV_IS_IN_NAND
594 fail_if_overlap(fcb, env, "FCB", "Environment");
595 fail_if_overlap(fw1, env, "FW1", "Environment");
597 fail_if_overlap(fcb, fw1, "FCB", "FW1");
599 fail_if_overlap(fcb, fw2, "FCB", "FW2");
600 #ifdef CONFIG_ENV_IS_IN_NAND
601 fail_if_overlap(fw2, env, "FW2", "Environment");
603 fail_if_overlap(fw1, fw2, "FW1", "FW2");
606 buf = malloc(erase_size);
608 printf("Failed to allocate buffer\n");
612 fcb = create_fcb(buf, fw1_start_block + fw1_skip,
613 fw2_start_block + fw2_skip, fw_num_blocks);
615 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
619 encode_hamming_13_8(fcb, (void *)fcb + 512, 512);
621 ret = write_fcb(buf, fcb_start_block);
624 printf("Failed to write FCB to block %lu\n", fcb_start_block);
628 if (size & (page_size - 1)) {
629 memset(addr + size, 0xff, size & (page_size - 1));
630 size = ALIGN(size, page_size);
633 printf("Programming U-Boot image from %p to block %lu @ %08llx\n",
634 addr, fw1_start_block + fw1_skip,
635 (u64)(fw1_start_block + fw1_skip) * mtd->erasesize);
636 ret = tx28_prog_uboot(addr, fw1_start_block, fw1_skip, size,
639 if (fw2_start_block == 0) {
643 printf("Programming redundant U-Boot image to block %lu @ %08llx\n",
644 fw2_start_block + fw2_skip,
645 (u64)(fw2_start_block + fw2_skip) * mtd->erasesize);
646 ret = tx28_prog_uboot(addr, fw2_start_block, fw2_skip, fw_num_blocks,
651 U_BOOT_CMD(romupdate, 11, 0, do_update,
652 "Creates an FCB data structure and writes an U-Boot image to flash",
653 "[-f {<part>|block#}] [-r [{<part>|block#}]] [-e #] [<address>] [<length>]\n"
654 "\t-f <part>\twrite bootloader image to partition <part>\n"
655 "\t-f #\twrite bootloader image at block # (decimal)\n"
656 "\t-r\twrite redundant bootloader image at next free block after first image\n"
657 "\t-r <part>\twrite redundant bootloader image to partition <part>\n"
658 "\t-r #\twrite redundant bootloader image at block # (decimal)\n"
659 "\t-e #\tspecify number of redundant blocks per boot loader image\n"
660 "\t\tonly valid if -f or -r specify a flash address rather than a partition name\n"
661 "\t<address>\tRAM address of bootloader image (default: ${fileaddr}\n"
662 "\t<length>\tlength of bootloader image in RAM (default: ${filesize}"