10 #include <asm/arch/mxs_gpmi-regs.h>
11 #include <asm/arch/mxs_gpmi-bch-regs.h>
13 #define FCB_START_BLOCK 0
14 #define NUM_FCB_BLOCKS 1
15 #define MAX_FCB_BLOCKS 32768
17 struct mx28_nand_timing {
32 struct mx28_nand_timing timing;
35 u32 sectors_per_block;
36 u32 number_of_nands; /* not used by ROM code */
37 u32 total_internal_die; /* not used by ROM code */
38 u32 cell_type; /* not used by ROM code */
44 u32 ecc_blocks_per_page;
45 u32 rsrvd[6]; /* not used by ROM code */
56 u32 bb_mark_phys_offset;
59 struct mx28_dbbt_header {
74 #define BF_VAL(v, bf) (((v) & BM_##bf) >> BP_##bf)
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;
151 static struct mx28_fcb *create_fcb(void *buf, int fw1_start_block, int fw2_start_block,
154 volatile void *gpmi_base = __ioremap(GPMI_BASE_ADDR, SZ_4K, 1);
155 volatile void *bch_base = __ioremap(BCH_BASE_ADDR, SZ_4K, 1);
159 int bb_mark_bit_offs;
160 struct mx28_fcb *fcb;
163 if (gpmi_base == NULL || bch_base == NULL) {
164 return ERR_PTR(-ENOMEM);
167 fl0 = readl(bch_base + HW_BCH_FLASH0LAYOUT0);
168 fl1 = readl(bch_base + HW_BCH_FLASH0LAYOUT1);
169 t0 = readl(gpmi_base + HW_GPMI_TIMING0);
170 t1 = readl(gpmi_base + HW_GPMI_TIMING1);
172 metadata_size = BF_VAL(fl0, BCH_FLASH0LAYOUT0_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 fcb->timing.data_setup = BF_VAL(t0, GPMI_TIMING0_DATA_SETUP);
185 fcb->timing.data_hold = BF_VAL(t0, GPMI_TIMING0_DATA_HOLD);
186 fcb->timing.address_setup = BF_VAL(t0, GPMI_TIMING0_ADDRESS_SETUP);
188 fcb->page_data_size = mtd->writesize;
189 fcb->total_page_size = mtd->writesize + mtd->oobsize;
190 fcb->sectors_per_block = mtd->erasesize / mtd->writesize;
192 fcb->ecc_block0_type = BF_VAL(fl0, BCH_FLASH0LAYOUT0_ECC0);
193 fcb->ecc_block0_size = BF_VAL(fl0, BCH_FLASH0LAYOUT0_DATA0_SIZE);
194 fcb->ecc_blockn_type = BF_VAL(fl1, BCH_FLASH0LAYOUT1_ECCN);
195 fcb->ecc_blockn_size = BF_VAL(fl1, BCH_FLASH0LAYOUT1_DATAN_SIZE);
197 fcb->metadata_size = BF_VAL(fl0, BCH_FLASH0LAYOUT0_META_SIZE);
198 fcb->ecc_blocks_per_page = BF_VAL(fl0, BCH_FLASH0LAYOUT0_NBLOCKS);
199 fcb->bch_mode = readl(bch_base + HW_BCH_MODE);
202 fcb->patch_sectors = 0;
204 fcb->fw1_start_page = fw1_start_block * mtd->erasesize / mtd->writesize;
205 fcb->fw1_sectors = DIV_ROUND_UP(fw_size, mtd->writesize);
207 if (fw2_start_block != 0 && fw2_start_block < mtd->size / mtd->erasesize) {
208 fcb->fw2_start_page = fw2_start_block * mtd->erasesize / mtd->writesize;
209 fcb->fw2_sectors = fcb->fw1_sectors;
212 fcb->dbbt_search_area = 1;
214 bb_mark_bit_offs = calc_bb_offset(mtd, fcb);
215 if (bb_mark_bit_offs < 0)
216 return ERR_PTR(bb_mark_bit_offs);
217 fcb->bb_mark_byte = bb_mark_bit_offs / 8;
218 fcb->bb_mark_startbit = bb_mark_bit_offs % 8;
219 fcb->bb_mark_phys_offset = mtd->writesize;
221 fcb->checksum = calc_chksum(&fcb->fingerprint, 512 - 4);
225 static int find_fcb(void *ref, int page)
228 struct nand_chip *chip = mtd->priv;
229 void *buf = malloc(mtd->erasesize);
234 chip->select_chip(mtd, 0);
235 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
236 ret = chip->ecc.read_page_raw(mtd, chip, buf);
238 printf("Failed to read FCB from page %u: %d\n", page, ret);
241 chip->select_chip(mtd, -1);
242 if (memcmp(buf, ref, mtd->writesize) == 0) {
243 printf("%s: Found FCB in page %u (%08x)\n", __func__,
244 page, page * mtd->writesize);
251 static int write_fcb(void *buf, int block)
254 struct nand_chip *chip = mtd->priv;
255 int page = block * mtd->erasesize / mtd->writesize;
257 ret = find_fcb(buf, page);
259 printf("FCB at block %d is up to date\n", block);
263 ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
265 printf("Failed to erase FCB block %u\n", block);
269 printf("Writing FCB to block %d @ %08x\n", block,
270 block * mtd->erasesize);
271 chip->select_chip(mtd, 0);
272 ret = chip->write_page(mtd, chip, buf, page, 0, 1);
274 printf("Failed to write FCB to block %u: %d\n", block, ret);
276 chip->select_chip(mtd, -1);
280 #define chk_overlap(a,b) \
281 ((a##_start_block <= b##_end_block && \
282 a##_end_block >= b##_start_block) || \
283 (b##_start_block <= a##_end_block && \
284 b##_end_block >= a##_start_block))
286 #define fail_if_overlap(a,b,m1,m2) do { \
287 if (chk_overlap(a, b)) { \
288 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
289 m1, a##_start_block, a##_end_block, \
290 m2, b##_start_block, b##_end_block); \
295 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
299 int erase_size = mtd->erasesize;
300 int page_size = mtd->writesize;
306 struct mx28_fcb *fcb;
307 unsigned long fcb_start_block = FCB_START_BLOCK;
308 unsigned long num_fcb_blocks = NUM_FCB_BLOCKS;
309 unsigned long fcb_end_block;
310 unsigned long mtd_num_blocks = mtd->size / mtd->erasesize;
311 unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
312 unsigned long env_end_block = env_start_block +
313 DIV_ROUND_UP(CONFIG_ENV_SIZE, mtd->erasesize) - 1;
317 unsigned long fw1_start_block = 0, fw1_end_block;
318 unsigned long fw2_start_block = 0, fw2_end_block;
319 unsigned long fw_num_blocks;
320 unsigned long extra_blocks = 2;
321 nand_erase_options_t erase_opts = { 0, };
324 load_addr = getenv("fileaddr");
325 file_size = getenv("filesize");
327 if (argc < 2 && load_addr == NULL) {
328 printf("Load address not specified\n");
331 if (argc < 3 && file_size == NULL) {
332 printf("Image size not specified\n");
336 for (optind = 1; optind < argc; optind++) {
337 if (strcmp(argv[optind], "-b") == 0) {
338 if (optind >= argc - 1) {
339 printf("Option %s requires an argument\n", argv[optind]);
343 fcb_start_block = simple_strtoul(argv[optind], NULL, 0);
344 if (fcb_start_block >= mtd_num_blocks) {
345 printf("Block number %lu is out of range: 0..%lu\n",
346 fcb_start_block, mtd_num_blocks - 1);
349 } else if (strcmp(argv[optind], "-n") == 0) {
350 if (optind >= argc - 1) {
351 printf("Option %s requires an argument\n", argv[optind]);
355 num_fcb_blocks = simple_strtoul(argv[optind], NULL, 0);
356 if (num_fcb_blocks > MAX_FCB_BLOCKS) {
357 printf("Extraneous number of FCB blocks; max. allowed: %u\n",
361 } else if (strcmp(argv[optind], "-f") == 0) {
362 if (optind >= argc - 1) {
363 printf("Option %s requires an argument\n", argv[optind]);
367 fw1_start_block = simple_strtoul(argv[optind], NULL, 0);
368 if (fw1_start_block >= mtd_num_blocks) {
369 printf("Block number %lu is out of range: 0..%lu\n",
375 } else if (strcmp(argv[optind], "-r") == 0) {
376 if (optind < argc - 1 && argv[optind + 1][0] != '-') {
378 fw2_start_block = simple_strtoul(argv[optind], NULL, 0);
379 if (fw2_start_block >= mtd_num_blocks) {
380 printf("Block number %lu is out of range: 0..%lu\n",
387 } else if (strcmp(argv[optind], "-e") == 0) {
388 if (optind >= argc - 1) {
389 printf("Option %s requires an argument\n", argv[optind]);
393 extra_blocks = simple_strtoul(argv[optind], NULL, 0);
394 if (extra_blocks >= mtd_num_blocks) {
395 printf("Extra block count %lu is out of range: 0..%lu\n",
400 } else if (argv[optind][0] == '-') {
401 printf("Unrecognized option %s\n", argv[optind]);
407 addr = (void *)simple_strtoul(argv[optind], NULL, 0);
412 size = simple_strtoul(argv[optind], NULL, 0);
415 if (load_addr != NULL) {
416 addr = (void *)simple_strtoul(load_addr, NULL, 16);
417 printf("Using default load address %p\n", addr);
419 if (file_size != NULL) {
420 size = simple_strtoul(file_size, NULL, 16);
421 printf("Using default file size %08x\n", size);
423 fcb_end_block = fcb_start_block + num_fcb_blocks - 1;
424 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
427 fw1_start_block = fcb_end_block + 1;
428 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
429 if (chk_overlap(fw1, env)) {
430 fw1_start_block = env_end_block + 1;
431 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
434 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
437 if (fw2_set && fw2_start_block == 0) {
438 fw2_start_block = fw1_end_block + 1;
439 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
440 if (chk_overlap(fw2, env)) {
441 fw2_start_block = env_end_block + 1;
442 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
445 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
448 fail_if_overlap(fcb, env, "FCB", "Environment");
449 fail_if_overlap(fcb, fw1, "FCB", "FW1");
450 fail_if_overlap(fw1, env, "FW1", "Environment");
452 fail_if_overlap(fcb, fw2, "FCB", "FW2");
453 fail_if_overlap(fw2, env, "FW2", "Environment");
454 fail_if_overlap(fw1, fw2, "FW1", "FW2");
457 buf = malloc(erase_size);
459 printf("Failed to allocate buffer\n");
462 /* search for first non-bad block in FW1 block range */
463 while (fw1_start_block <= fw1_end_block) {
464 if (!nand_block_isbad(mtd, fw1_start_block * mtd->erasesize))
468 if (fw1_end_block - fw1_start_block + 1 < fw_num_blocks) {
469 printf("Too many bad blocks in FW1 block range: %lu..%lu\n",
470 fw1_end_block + 1 - fw_num_blocks - extra_blocks,
475 /* search for first non-bad block in FW2 block range */
476 while (fw2_set && fw2_start_block <= fw2_end_block) {
477 if (!nand_block_isbad(mtd, fw2_start_block * mtd->erasesize))
481 if (fw2_end_block - fw2_start_block + 1 < fw_num_blocks) {
482 printf("Too many bad blocks in FW2 area %08lx..%08lx\n",
483 fw2_end_block + 1 - fw_num_blocks - extra_blocks,
488 fcb = create_fcb(buf, fw1_start_block, fw2_start_block,
489 (fw_num_blocks + extra_blocks) * mtd->erasesize);
491 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
494 encode_hamming_13_8(fcb, (void *)fcb + 512, 512);
496 for (block = fcb_start_block; block < fcb_start_block + num_fcb_blocks;
498 if (nand_block_isbad(mtd, block * mtd->erasesize)) {
499 if (block == fcb_start_block)
503 ret = write_fcb(buf, block);
505 printf("Failed to write FCB to block %u\n", block);
512 printf("Could not write FCB to flash\n");
516 printf("Programming U-Boot image from %p to block %lu\n",
517 addr, fw1_start_block);
518 if (size & (page_size - 1)) {
519 memset(addr + size, 0xff, size & (page_size - 1));
520 size = ALIGN(size, page_size);
523 erase_opts.offset = fcb->fw1_start_page * page_size;
524 erase_opts.length = ALIGN(size, erase_size) +
525 extra_blocks * mtd->erasesize;
526 erase_opts.quiet = 1;
528 printf("Erasing flash @ %08lx..%08lx\n", erase_opts.offset,
529 erase_opts.offset + erase_opts.length - 1);
531 ret = nand_erase_opts(mtd, &erase_opts);
533 printf("Failed to erase flash: %d\n", ret);
536 printf("Programming flash @ %08x..%08x from %p\n",
537 fcb->fw1_start_page * page_size,
538 fcb->fw1_start_page * page_size + size, addr);
539 ret = nand_write_skip_bad(mtd, fcb->fw1_start_page * page_size,
542 printf("Failed to program flash: %d\n", ret);
545 if (fw2_start_block == 0) {
549 printf("Programming redundant U-Boot image to block %lu\n",
551 erase_opts.offset = fcb->fw2_start_page * page_size;
552 printf("Erasing flash @ %08lx..%08lx\n", erase_opts.offset,
553 erase_opts.offset + erase_opts.length - 1);
555 ret = nand_erase_opts(mtd, &erase_opts);
557 printf("Failed to erase flash: %d\n", ret);
560 printf("Programming flash @ %08x..%08x from %p\n",
561 fcb->fw2_start_page * page_size,
562 fcb->fw2_start_page * page_size + size, addr);
563 ret = nand_write_skip_bad(mtd, fcb->fw2_start_page * page_size,
566 printf("Failed to program flash: %d\n", ret);
572 U_BOOT_CMD(romupdate, 11, 0, do_update,
573 "Creates an FCB data structure and writes an U-Boot image to flash\n",
574 "[-b #] [-n #] [-f #] [-r [#]] [<address>] [<length>]\n"
575 "\t-b #\tfirst FCB block number (default 0)\n"
576 "\t-n #\ttotal number of FCB blocks (default 1)\n"
577 "\t-f #\twrite bootloader image at block #\n"
578 "\t-r\twrite redundant bootloader image at next free block after first image\n"
579 "\t-r #\twrite redundant bootloader image at block #\n"
580 "\t-e #\tspecify number of redundant blocks per boot loader image\n"
581 "\t<address>\tRAM address of bootloader image (default: ${fileaddr}\n"
582 "\t<length>\tlength of bootloader image in RAM (default: ${filesize}"