2 * Freescale i.MX28 image generator
4 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
5 * on behalf of DENX Software Engineering GmbH
7 * SPDX-License-Identifier: GPL-2.0+
12 #include <sys/types.h>
20 * TWEAK this if you have blown any OCOTP fuses.
22 #define STRIDE_PAGES 64
23 #define STRIDE_COUNT 4
26 * Layout for 256Mb big NAND with 2048b page size, 64b OOB size and
29 * TWEAK this if you have different kind of NAND chip.
31 static uint32_t nand_writesize = 2048;
32 static uint32_t nand_oobsize = 64;
33 static uint32_t nand_erasesize = 128 * 1024;
36 * Sector on which the SigmaTel boot partition (0x53) starts.
38 static uint32_t sd_sector = 2048;
41 * Each of the U-Boot bootstreams is at maximum 1MB big.
43 * TWEAK this if, for some wild reason, you need to boot bigger image.
45 #define MAX_BOOTSTREAM_SIZE (1 * 1024 * 1024)
47 /* i.MX28 NAND controller-specific constants. DO NOT TWEAK! */
48 #define MXS_NAND_DMA_DESCRIPTOR_COUNT 4
49 #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE 512
50 #define MXS_NAND_METADATA_SIZE 10
51 #define MXS_NAND_BITS_PER_ECC_LEVEL 13
52 #define MXS_NAND_COMMAND_BUFFER_SIZE 32
54 struct mx28_nand_fcb {
61 uint8_t address_setup;
63 uint8_t nand_timing_state;
68 uint32_t page_data_size;
69 uint32_t total_page_size;
70 uint32_t sectors_per_block;
71 uint32_t number_of_nands; /* Ignored */
72 uint32_t total_internal_die; /* Ignored */
73 uint32_t cell_type; /* Ignored */
74 uint32_t ecc_block_n_ecc_type;
75 uint32_t ecc_block_0_size;
76 uint32_t ecc_block_n_size;
77 uint32_t ecc_block_0_ecc_type;
78 uint32_t metadata_bytes;
79 uint32_t num_ecc_blocks_per_page;
80 uint32_t ecc_block_n_ecc_level_sdk; /* Ignored */
81 uint32_t ecc_block_0_size_sdk; /* Ignored */
82 uint32_t ecc_block_n_size_sdk; /* Ignored */
83 uint32_t ecc_block_0_ecc_level_sdk; /* Ignored */
84 uint32_t num_ecc_blocks_per_page_sdk; /* Ignored */
85 uint32_t metadata_bytes_sdk; /* Ignored */
86 uint32_t erase_threshold;
88 uint32_t patch_sectors;
89 uint32_t firmware1_starting_sector;
90 uint32_t firmware2_starting_sector;
91 uint32_t sectors_in_firmware1;
92 uint32_t sectors_in_firmware2;
93 uint32_t dbbt_search_area_start_address;
94 uint32_t badblock_marker_byte;
95 uint32_t badblock_marker_start_bit;
96 uint32_t bb_marker_physical_offset;
99 struct mx28_nand_dbbt {
101 uint32_t fingerprint;
104 uint32_t number_2k_pages_bb;
107 struct mx28_nand_bbt {
110 uint32_t badblock[510];
113 struct mx28_sd_drive_info {
117 uint32_t first_sector_number;
118 uint32_t sector_count;
121 struct mx28_sd_config_block {
123 uint32_t primary_boot_tag;
124 uint32_t secondary_boot_tag;
126 struct mx28_sd_drive_info drv_info[1];
129 static inline uint32_t mx28_nand_ecc_size_in_bits(uint32_t ecc_strength)
131 return ecc_strength * MXS_NAND_BITS_PER_ECC_LEVEL;
134 static inline uint32_t mx28_nand_get_ecc_strength(uint32_t page_data_size,
135 uint32_t page_oob_size)
137 if (page_data_size == 2048)
140 if (page_data_size == 4096) {
141 if (page_oob_size == 128)
144 if (page_oob_size == 218)
147 if (page_oob_size == 224)
154 static inline uint32_t mx28_nand_get_mark_offset(uint32_t page_data_size,
155 uint32_t ecc_strength)
157 uint32_t chunk_data_size_in_bits;
158 uint32_t chunk_ecc_size_in_bits;
159 uint32_t chunk_total_size_in_bits;
160 uint32_t block_mark_chunk_number;
161 uint32_t block_mark_chunk_bit_offset;
162 uint32_t block_mark_bit_offset;
164 chunk_data_size_in_bits = MXS_NAND_CHUNK_DATA_CHUNK_SIZE * 8;
165 chunk_ecc_size_in_bits = mx28_nand_ecc_size_in_bits(ecc_strength);
167 chunk_total_size_in_bits =
168 chunk_data_size_in_bits + chunk_ecc_size_in_bits;
170 /* Compute the bit offset of the block mark within the physical page. */
171 block_mark_bit_offset = page_data_size * 8;
173 /* Subtract the metadata bits. */
174 block_mark_bit_offset -= MXS_NAND_METADATA_SIZE * 8;
177 * Compute the chunk number (starting at zero) in which the block mark
180 block_mark_chunk_number =
181 block_mark_bit_offset / chunk_total_size_in_bits;
184 * Compute the bit offset of the block mark within its chunk, and
187 block_mark_chunk_bit_offset = block_mark_bit_offset -
188 (block_mark_chunk_number * chunk_total_size_in_bits);
190 if (block_mark_chunk_bit_offset > chunk_data_size_in_bits)
194 * Now that we know the chunk number in which the block mark appears,
195 * we can subtract all the ECC bits that appear before it.
197 block_mark_bit_offset -=
198 block_mark_chunk_number * chunk_ecc_size_in_bits;
200 return block_mark_bit_offset;
203 static inline uint32_t mx28_nand_mark_byte_offset(void)
205 uint32_t ecc_strength;
206 ecc_strength = mx28_nand_get_ecc_strength(nand_writesize, nand_oobsize);
207 return mx28_nand_get_mark_offset(nand_writesize, ecc_strength) >> 3;
210 static inline uint32_t mx28_nand_mark_bit_offset(void)
212 uint32_t ecc_strength;
213 ecc_strength = mx28_nand_get_ecc_strength(nand_writesize, nand_oobsize);
214 return mx28_nand_get_mark_offset(nand_writesize, ecc_strength) & 0x7;
217 static uint32_t mx28_nand_block_csum(uint8_t *block, uint32_t size)
222 for (i = 0; i < size; i++)
225 return csum ^ 0xffffffff;
228 static struct mx28_nand_fcb *mx28_nand_get_fcb(uint32_t size)
230 struct mx28_nand_fcb *fcb;
231 uint32_t bcb_size_bytes;
232 uint32_t stride_size_bytes;
233 uint32_t bootstream_size_pages;
234 uint32_t fw1_start_page;
235 uint32_t fw2_start_page;
237 fcb = malloc(nand_writesize);
239 printf("MX28 NAND: Unable to allocate FCB\n");
243 memset(fcb, 0, nand_writesize);
245 fcb->fingerprint = 0x20424346;
246 fcb->version = 0x01000000;
249 * FIXME: These here are default values as found in kobs-ng. We should
250 * probably retrieve the data from NAND or something.
252 fcb->timing.data_setup = 80;
253 fcb->timing.data_hold = 60;
254 fcb->timing.address_setup = 25;
255 fcb->timing.dsample_time = 6;
257 fcb->page_data_size = nand_writesize;
258 fcb->total_page_size = nand_writesize + nand_oobsize;
259 fcb->sectors_per_block = nand_erasesize / nand_writesize;
261 fcb->num_ecc_blocks_per_page = (nand_writesize / 512) - 1;
262 fcb->ecc_block_0_size = 512;
263 fcb->ecc_block_n_size = 512;
264 fcb->metadata_bytes = 10;
266 if (nand_writesize == 2048) {
267 fcb->ecc_block_n_ecc_type = 4;
268 fcb->ecc_block_0_ecc_type = 4;
269 } else if (nand_writesize == 4096) {
270 if (nand_oobsize == 128) {
271 fcb->ecc_block_n_ecc_type = 4;
272 fcb->ecc_block_0_ecc_type = 4;
273 } else if (nand_oobsize == 218) {
274 fcb->ecc_block_n_ecc_type = 8;
275 fcb->ecc_block_0_ecc_type = 8;
276 } else if (nand_oobsize == 224) {
277 fcb->ecc_block_n_ecc_type = 8;
278 fcb->ecc_block_0_ecc_type = 8;
282 if (fcb->ecc_block_n_ecc_type == 0) {
283 printf("MX28 NAND: Unsupported NAND geometry\n");
288 fcb->patch_sectors = 0;
290 fcb->badblock_marker_byte = mx28_nand_mark_byte_offset();
291 fcb->badblock_marker_start_bit = mx28_nand_mark_bit_offset();
292 fcb->bb_marker_physical_offset = nand_writesize;
294 stride_size_bytes = STRIDE_PAGES * nand_writesize;
295 bcb_size_bytes = stride_size_bytes * STRIDE_COUNT;
297 bootstream_size_pages = (size + (nand_writesize - 1)) /
300 fw1_start_page = 2 * bcb_size_bytes / nand_writesize;
301 fw2_start_page = (2 * bcb_size_bytes + MAX_BOOTSTREAM_SIZE) /
304 fcb->firmware1_starting_sector = fw1_start_page;
305 fcb->firmware2_starting_sector = fw2_start_page;
306 fcb->sectors_in_firmware1 = bootstream_size_pages;
307 fcb->sectors_in_firmware2 = bootstream_size_pages;
309 fcb->dbbt_search_area_start_address = STRIDE_PAGES * STRIDE_COUNT;
318 static struct mx28_nand_dbbt *mx28_nand_get_dbbt(void)
320 struct mx28_nand_dbbt *dbbt;
322 dbbt = malloc(nand_writesize);
324 printf("MX28 NAND: Unable to allocate DBBT\n");
328 memset(dbbt, 0, nand_writesize);
330 dbbt->fingerprint = 0x54424244;
336 static inline uint8_t mx28_nand_parity_13_8(const uint8_t b)
338 uint32_t parity = 0, tmp;
340 tmp = ((b >> 6) ^ (b >> 5) ^ (b >> 3) ^ (b >> 2)) & 1;
343 tmp = ((b >> 7) ^ (b >> 5) ^ (b >> 4) ^ (b >> 2) ^ (b >> 1)) & 1;
346 tmp = ((b >> 7) ^ (b >> 6) ^ (b >> 5) ^ (b >> 1) ^ (b >> 0)) & 1;
349 tmp = ((b >> 7) ^ (b >> 4) ^ (b >> 3) ^ (b >> 0)) & 1;
352 tmp = ((b >> 6) ^ (b >> 4) ^ (b >> 3) ^
353 (b >> 2) ^ (b >> 1) ^ (b >> 0)) & 1;
359 static uint8_t *mx28_nand_fcb_block(struct mx28_nand_fcb *fcb)
365 block = malloc(nand_writesize + nand_oobsize);
367 printf("MX28 NAND: Unable to allocate FCB block\n");
371 memset(block, 0, nand_writesize + nand_oobsize);
373 /* Update the FCB checksum */
374 fcb->checksum = mx28_nand_block_csum(((uint8_t *)fcb) + 4, 508);
376 /* Figure 12-11. in iMX28RM, rev. 1, says FCB is at offset 12 */
377 memcpy(block + 12, fcb, sizeof(struct mx28_nand_fcb));
379 /* ECC is at offset 12 + 512 */
380 ecc = block + 12 + 512;
382 /* Compute the ECC parity */
383 for (i = 0; i < sizeof(struct mx28_nand_fcb); i++)
384 ecc[i] = mx28_nand_parity_13_8(block[i + 12]);
389 static int mx28_nand_write_fcb(struct mx28_nand_fcb *fcb, uint8_t *buf)
396 fcbblock = mx28_nand_fcb_block(fcb);
400 for (i = 0; i < STRIDE_PAGES * STRIDE_COUNT; i += STRIDE_PAGES) {
401 offset = i * nand_writesize;
402 memcpy(buf + offset, fcbblock, nand_writesize + nand_oobsize);
403 /* Mark the NAND page is OK. */
404 buf[offset + nand_writesize] = 0xff;
411 static int mx28_nand_write_dbbt(struct mx28_nand_dbbt *dbbt, uint8_t *buf)
414 int i = STRIDE_PAGES * STRIDE_COUNT;
416 for (; i < 2 * STRIDE_PAGES * STRIDE_COUNT; i += STRIDE_PAGES) {
417 offset = i * nand_writesize;
418 memcpy(buf + offset, dbbt, sizeof(struct mx28_nand_dbbt));
424 static int mx28_nand_write_firmware(struct mx28_nand_fcb *fcb, int infd,
429 uint32_t offset1, offset2;
431 size = lseek(infd, 0, SEEK_END);
432 lseek(infd, 0, SEEK_SET);
434 offset1 = fcb->firmware1_starting_sector * nand_writesize;
435 offset2 = fcb->firmware2_starting_sector * nand_writesize;
437 ret = read(infd, buf + offset1, size);
441 memcpy(buf + offset2, buf + offset1, size);
446 static void usage(void)
449 "Usage: mxsboot [ops] <type> <infile> <outfile>\n"
450 "Augment BootStream file with a proper header for i.MX28 boot\n"
452 " <type> type of image:\n"
453 " \"nand\" for NAND image\n"
454 " \"sd\" for SD image\n"
455 " <infile> input file, the u-boot.sb bootstream\n"
456 " <outfile> output file, the bootable image\n"
459 "For NAND boot, these options are accepted:\n"
460 " -w <size> NAND page size\n"
461 " -o <size> NAND OOB size\n"
462 " -e <size> NAND erase size\n"
464 "For SD boot, these options are accepted:\n"
465 " -p <sector> Sector where the SGTL partition starts\n"
469 static int mx28_create_nand_image(int infd, int outfd)
471 struct mx28_nand_fcb *fcb;
472 struct mx28_nand_dbbt *dbbt;
478 size = nand_writesize * 512 + 2 * MAX_BOOTSTREAM_SIZE;
482 printf("Can not allocate output buffer of %d bytes\n", size);
486 memset(buf, 0, size);
488 fcb = mx28_nand_get_fcb(MAX_BOOTSTREAM_SIZE);
490 printf("Unable to compile FCB\n");
494 dbbt = mx28_nand_get_dbbt();
496 printf("Unable to compile DBBT\n");
500 ret = mx28_nand_write_fcb(fcb, buf);
502 printf("Unable to write FCB to buffer\n");
506 ret = mx28_nand_write_dbbt(dbbt, buf);
508 printf("Unable to write DBBT to buffer\n");
512 ret = mx28_nand_write_firmware(fcb, infd, buf);
514 printf("Unable to write firmware to buffer\n");
518 wr_size = write(outfd, buf, size);
519 if (wr_size != size) {
536 static int mx28_create_sd_image(int infd, int outfd)
543 struct mx28_sd_config_block *cb;
545 fsize = lseek(infd, 0, SEEK_END);
546 lseek(infd, 0, SEEK_SET);
547 size = fsize + 4 * 512;
551 printf("Can not allocate output buffer of %d bytes\n", size);
555 ret = read(infd, (uint8_t *)buf + 4 * 512, fsize);
561 cb = (struct mx28_sd_config_block *)buf;
563 cb->signature = 0x00112233;
564 cb->primary_boot_tag = 0x1;
565 cb->secondary_boot_tag = 0x1;
567 cb->drv_info[0].chip_num = 0x0;
568 cb->drv_info[0].drive_type = 0x0;
569 cb->drv_info[0].tag = 0x1;
570 cb->drv_info[0].first_sector_number = sd_sector + 4;
571 cb->drv_info[0].sector_count = (size - 4) / 512;
573 wr_size = write(outfd, buf, size);
574 if (wr_size != size) {
587 static int parse_ops(int argc, char **argv)
605 for (i = 1; i < argc; i++) {
606 if (!strncmp(argv[i], "-w", 2))
608 else if (!strncmp(argv[i], "-o", 2))
610 else if (!strncmp(argv[i], "-e", 2))
612 else if (!strncmp(argv[i], "-p", 2))
617 tmp = strtol(argv[++i], &end, 10);
623 if (type == PARAM_WRITE)
624 nand_writesize = tmp;
625 if (type == PARAM_OOB)
627 if (type == PARAM_ERASE)
628 nand_erasesize = tmp;
629 if (type == PARAM_PART)
633 if (strcmp(argv[i], "sd") && strcmp(argv[i], "nand"))
642 int main(int argc, char **argv)
648 offset = parse_ops(argc, argv);
655 infd = open(argv[offset + 1], O_RDONLY);
657 printf("Input BootStream file can not be opened\n");
662 outfd = open(argv[offset + 2], O_CREAT | O_TRUNC | O_WRONLY,
665 printf("Output file can not be created\n");
670 if (!strcmp(argv[offset], "sd"))
671 ret = mx28_create_sd_image(infd, outfd);
672 else if (!strcmp(argv[offset], "nand"))
673 ret = mx28_create_nand_image(infd, outfd);