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>
17 /* Taken from <linux/kernel.h> */
18 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
19 #define round_down(x, y) ((x) & ~__round_mask(x, y))
24 * TWEAK this if you have blown any OCOTP fuses.
26 #define STRIDE_PAGES 64
27 #define STRIDE_COUNT 4
30 * Layout for 256Mb big NAND with 2048b page size, 64b OOB size and
33 * TWEAK this if you have different kind of NAND chip.
35 static uint32_t nand_writesize
= 2048;
36 static uint32_t nand_oobsize
= 64;
37 static uint32_t nand_erasesize
= 128 * 1024;
40 * Sector on which the SigmaTel boot partition (0x53) starts.
42 static uint32_t sd_sector
= 2048;
45 * Each of the U-Boot bootstreams is at maximum 1MB big.
47 * TWEAK this if, for some wild reason, you need to boot bigger image.
49 #define MAX_BOOTSTREAM_SIZE (1 * 1024 * 1024)
51 /* i.MX28 NAND controller-specific constants. DO NOT TWEAK! */
52 #define MXS_NAND_DMA_DESCRIPTOR_COUNT 4
53 #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE 512
54 #define MXS_NAND_METADATA_SIZE 10
55 #define MXS_NAND_BITS_PER_ECC_LEVEL 13
56 #define MXS_NAND_COMMAND_BUFFER_SIZE 32
58 struct mx28_nand_fcb
{
65 uint8_t address_setup
;
67 uint8_t nand_timing_state
;
72 uint32_t page_data_size
;
73 uint32_t total_page_size
;
74 uint32_t sectors_per_block
;
75 uint32_t number_of_nands
; /* Ignored */
76 uint32_t total_internal_die
; /* Ignored */
77 uint32_t cell_type
; /* Ignored */
78 uint32_t ecc_block_n_ecc_type
;
79 uint32_t ecc_block_0_size
;
80 uint32_t ecc_block_n_size
;
81 uint32_t ecc_block_0_ecc_type
;
82 uint32_t metadata_bytes
;
83 uint32_t num_ecc_blocks_per_page
;
84 uint32_t ecc_block_n_ecc_level_sdk
; /* Ignored */
85 uint32_t ecc_block_0_size_sdk
; /* Ignored */
86 uint32_t ecc_block_n_size_sdk
; /* Ignored */
87 uint32_t ecc_block_0_ecc_level_sdk
; /* Ignored */
88 uint32_t num_ecc_blocks_per_page_sdk
; /* Ignored */
89 uint32_t metadata_bytes_sdk
; /* Ignored */
90 uint32_t erase_threshold
;
92 uint32_t patch_sectors
;
93 uint32_t firmware1_starting_sector
;
94 uint32_t firmware2_starting_sector
;
95 uint32_t sectors_in_firmware1
;
96 uint32_t sectors_in_firmware2
;
97 uint32_t dbbt_search_area_start_address
;
98 uint32_t badblock_marker_byte
;
99 uint32_t badblock_marker_start_bit
;
100 uint32_t bb_marker_physical_offset
;
103 struct mx28_nand_dbbt
{
105 uint32_t fingerprint
;
108 uint32_t number_2k_pages_bb
;
111 struct mx28_nand_bbt
{
114 uint32_t badblock
[510];
117 struct mx28_sd_drive_info
{
121 uint32_t first_sector_number
;
122 uint32_t sector_count
;
125 struct mx28_sd_config_block
{
127 uint32_t primary_boot_tag
;
128 uint32_t secondary_boot_tag
;
130 struct mx28_sd_drive_info drv_info
[1];
133 static inline uint32_t mx28_nand_ecc_chunk_cnt(uint32_t page_data_size
)
135 return page_data_size
/ MXS_NAND_CHUNK_DATA_CHUNK_SIZE
;
138 static inline uint32_t mx28_nand_ecc_size_in_bits(uint32_t ecc_strength
)
140 return ecc_strength
* MXS_NAND_BITS_PER_ECC_LEVEL
;
143 static inline uint32_t mx28_nand_get_ecc_strength(uint32_t page_data_size
,
144 uint32_t page_oob_size
)
149 * Determine the ECC layout with the formula:
150 * ECC bits per chunk = (total page spare data bits) /
151 * (bits per ECC level) / (chunks per page)
153 * total page spare data bits =
154 * (page oob size - meta data size) * (bits per byte)
156 ecc_strength
= ((page_oob_size
- MXS_NAND_METADATA_SIZE
) * 8)
157 / (MXS_NAND_BITS_PER_ECC_LEVEL
*
158 mx28_nand_ecc_chunk_cnt(page_data_size
));
160 return round_down(ecc_strength
, 2);
163 static inline uint32_t mx28_nand_get_mark_offset(uint32_t page_data_size
,
164 uint32_t ecc_strength
)
166 uint32_t chunk_data_size_in_bits
;
167 uint32_t chunk_ecc_size_in_bits
;
168 uint32_t chunk_total_size_in_bits
;
169 uint32_t block_mark_chunk_number
;
170 uint32_t block_mark_chunk_bit_offset
;
171 uint32_t block_mark_bit_offset
;
173 chunk_data_size_in_bits
= MXS_NAND_CHUNK_DATA_CHUNK_SIZE
* 8;
174 chunk_ecc_size_in_bits
= mx28_nand_ecc_size_in_bits(ecc_strength
);
176 chunk_total_size_in_bits
=
177 chunk_data_size_in_bits
+ chunk_ecc_size_in_bits
;
179 /* Compute the bit offset of the block mark within the physical page. */
180 block_mark_bit_offset
= page_data_size
* 8;
182 /* Subtract the metadata bits. */
183 block_mark_bit_offset
-= MXS_NAND_METADATA_SIZE
* 8;
186 * Compute the chunk number (starting at zero) in which the block mark
189 block_mark_chunk_number
=
190 block_mark_bit_offset
/ chunk_total_size_in_bits
;
193 * Compute the bit offset of the block mark within its chunk, and
196 block_mark_chunk_bit_offset
= block_mark_bit_offset
-
197 (block_mark_chunk_number
* chunk_total_size_in_bits
);
199 if (block_mark_chunk_bit_offset
> chunk_data_size_in_bits
)
203 * Now that we know the chunk number in which the block mark appears,
204 * we can subtract all the ECC bits that appear before it.
206 block_mark_bit_offset
-=
207 block_mark_chunk_number
* chunk_ecc_size_in_bits
;
209 return block_mark_bit_offset
;
212 static inline uint32_t mx28_nand_mark_byte_offset(void)
214 uint32_t ecc_strength
;
215 ecc_strength
= mx28_nand_get_ecc_strength(nand_writesize
, nand_oobsize
);
216 return mx28_nand_get_mark_offset(nand_writesize
, ecc_strength
) >> 3;
219 static inline uint32_t mx28_nand_mark_bit_offset(void)
221 uint32_t ecc_strength
;
222 ecc_strength
= mx28_nand_get_ecc_strength(nand_writesize
, nand_oobsize
);
223 return mx28_nand_get_mark_offset(nand_writesize
, ecc_strength
) & 0x7;
226 static uint32_t mx28_nand_block_csum(uint8_t *block
, uint32_t size
)
231 for (i
= 0; i
< size
; i
++)
234 return csum
^ 0xffffffff;
237 static struct mx28_nand_fcb
*mx28_nand_get_fcb(uint32_t size
)
239 struct mx28_nand_fcb
*fcb
;
240 uint32_t bcb_size_bytes
;
241 uint32_t stride_size_bytes
;
242 uint32_t bootstream_size_pages
;
243 uint32_t fw1_start_page
;
244 uint32_t fw2_start_page
;
246 fcb
= malloc(nand_writesize
);
248 printf("MX28 NAND: Unable to allocate FCB\n");
252 memset(fcb
, 0, nand_writesize
);
254 fcb
->fingerprint
= 0x20424346;
255 fcb
->version
= 0x01000000;
258 * FIXME: These here are default values as found in kobs-ng. We should
259 * probably retrieve the data from NAND or something.
261 fcb
->timing
.data_setup
= 80;
262 fcb
->timing
.data_hold
= 60;
263 fcb
->timing
.address_setup
= 25;
264 fcb
->timing
.dsample_time
= 6;
266 fcb
->page_data_size
= nand_writesize
;
267 fcb
->total_page_size
= nand_writesize
+ nand_oobsize
;
268 fcb
->sectors_per_block
= nand_erasesize
/ nand_writesize
;
270 fcb
->num_ecc_blocks_per_page
= (nand_writesize
/ 512) - 1;
271 fcb
->ecc_block_0_size
= 512;
272 fcb
->ecc_block_n_size
= 512;
273 fcb
->metadata_bytes
= 10;
275 if (nand_writesize
== 2048) {
276 fcb
->ecc_block_n_ecc_type
= 4;
277 fcb
->ecc_block_0_ecc_type
= 4;
278 } else if (nand_writesize
== 4096) {
279 if (nand_oobsize
== 128) {
280 fcb
->ecc_block_n_ecc_type
= 4;
281 fcb
->ecc_block_0_ecc_type
= 4;
282 } else if (nand_oobsize
== 218) {
283 fcb
->ecc_block_n_ecc_type
= 8;
284 fcb
->ecc_block_0_ecc_type
= 8;
285 } else if (nand_oobsize
== 224) {
286 fcb
->ecc_block_n_ecc_type
= 8;
287 fcb
->ecc_block_0_ecc_type
= 8;
291 if (fcb
->ecc_block_n_ecc_type
== 0) {
292 printf("MX28 NAND: Unsupported NAND geometry\n");
297 fcb
->patch_sectors
= 0;
299 fcb
->badblock_marker_byte
= mx28_nand_mark_byte_offset();
300 fcb
->badblock_marker_start_bit
= mx28_nand_mark_bit_offset();
301 fcb
->bb_marker_physical_offset
= nand_writesize
;
303 stride_size_bytes
= STRIDE_PAGES
* nand_writesize
;
304 bcb_size_bytes
= stride_size_bytes
* STRIDE_COUNT
;
306 bootstream_size_pages
= (size
+ (nand_writesize
- 1)) /
309 fw1_start_page
= 2 * bcb_size_bytes
/ nand_writesize
;
310 fw2_start_page
= (2 * bcb_size_bytes
+ MAX_BOOTSTREAM_SIZE
) /
313 fcb
->firmware1_starting_sector
= fw1_start_page
;
314 fcb
->firmware2_starting_sector
= fw2_start_page
;
315 fcb
->sectors_in_firmware1
= bootstream_size_pages
;
316 fcb
->sectors_in_firmware2
= bootstream_size_pages
;
318 fcb
->dbbt_search_area_start_address
= STRIDE_PAGES
* STRIDE_COUNT
;
327 static struct mx28_nand_dbbt
*mx28_nand_get_dbbt(void)
329 struct mx28_nand_dbbt
*dbbt
;
331 dbbt
= malloc(nand_writesize
);
333 printf("MX28 NAND: Unable to allocate DBBT\n");
337 memset(dbbt
, 0, nand_writesize
);
339 dbbt
->fingerprint
= 0x54424244;
345 static inline uint8_t mx28_nand_parity_13_8(const uint8_t b
)
347 uint32_t parity
= 0, tmp
;
349 tmp
= ((b
>> 6) ^ (b
>> 5) ^ (b
>> 3) ^ (b
>> 2)) & 1;
352 tmp
= ((b
>> 7) ^ (b
>> 5) ^ (b
>> 4) ^ (b
>> 2) ^ (b
>> 1)) & 1;
355 tmp
= ((b
>> 7) ^ (b
>> 6) ^ (b
>> 5) ^ (b
>> 1) ^ (b
>> 0)) & 1;
358 tmp
= ((b
>> 7) ^ (b
>> 4) ^ (b
>> 3) ^ (b
>> 0)) & 1;
361 tmp
= ((b
>> 6) ^ (b
>> 4) ^ (b
>> 3) ^
362 (b
>> 2) ^ (b
>> 1) ^ (b
>> 0)) & 1;
368 static uint8_t *mx28_nand_fcb_block(struct mx28_nand_fcb
*fcb
)
374 block
= malloc(nand_writesize
+ nand_oobsize
);
376 printf("MX28 NAND: Unable to allocate FCB block\n");
380 memset(block
, 0, nand_writesize
+ nand_oobsize
);
382 /* Update the FCB checksum */
383 fcb
->checksum
= mx28_nand_block_csum(((uint8_t *)fcb
) + 4, 508);
385 /* Figure 12-11. in iMX28RM, rev. 1, says FCB is at offset 12 */
386 memcpy(block
+ 12, fcb
, sizeof(struct mx28_nand_fcb
));
388 /* ECC is at offset 12 + 512 */
389 ecc
= block
+ 12 + 512;
391 /* Compute the ECC parity */
392 for (i
= 0; i
< sizeof(struct mx28_nand_fcb
); i
++)
393 ecc
[i
] = mx28_nand_parity_13_8(block
[i
+ 12]);
398 static int mx28_nand_write_fcb(struct mx28_nand_fcb
*fcb
, uint8_t *buf
)
405 fcbblock
= mx28_nand_fcb_block(fcb
);
409 for (i
= 0; i
< STRIDE_PAGES
* STRIDE_COUNT
; i
+= STRIDE_PAGES
) {
410 offset
= i
* nand_writesize
;
411 memcpy(buf
+ offset
, fcbblock
, nand_writesize
+ nand_oobsize
);
412 /* Mark the NAND page is OK. */
413 buf
[offset
+ nand_writesize
] = 0xff;
420 static int mx28_nand_write_dbbt(struct mx28_nand_dbbt
*dbbt
, uint8_t *buf
)
423 int i
= STRIDE_PAGES
* STRIDE_COUNT
;
425 for (; i
< 2 * STRIDE_PAGES
* STRIDE_COUNT
; i
+= STRIDE_PAGES
) {
426 offset
= i
* nand_writesize
;
427 memcpy(buf
+ offset
, dbbt
, sizeof(struct mx28_nand_dbbt
));
433 static int mx28_nand_write_firmware(struct mx28_nand_fcb
*fcb
, int infd
,
438 uint32_t offset1
, offset2
;
440 size
= lseek(infd
, 0, SEEK_END
);
441 lseek(infd
, 0, SEEK_SET
);
443 offset1
= fcb
->firmware1_starting_sector
* nand_writesize
;
444 offset2
= fcb
->firmware2_starting_sector
* nand_writesize
;
446 ret
= read(infd
, buf
+ offset1
, size
);
450 memcpy(buf
+ offset2
, buf
+ offset1
, size
);
455 static void usage(void)
458 "Usage: mxsboot [ops] <type> <infile> <outfile>\n"
459 "Augment BootStream file with a proper header for i.MX28 boot\n"
461 " <type> type of image:\n"
462 " \"nand\" for NAND image\n"
463 " \"sd\" for SD image\n"
464 " <infile> input file, the u-boot.sb bootstream\n"
465 " <outfile> output file, the bootable image\n"
468 "For NAND boot, these options are accepted:\n"
469 " -w <size> NAND page size\n"
470 " -o <size> NAND OOB size\n"
471 " -e <size> NAND erase size\n"
473 "For SD boot, these options are accepted:\n"
474 " -p <sector> Sector where the SGTL partition starts\n"
478 static int mx28_create_nand_image(int infd
, int outfd
)
480 struct mx28_nand_fcb
*fcb
;
481 struct mx28_nand_dbbt
*dbbt
;
487 size
= nand_writesize
* 512 + 2 * MAX_BOOTSTREAM_SIZE
;
491 printf("Can not allocate output buffer of %d bytes\n", size
);
495 memset(buf
, 0, size
);
497 fcb
= mx28_nand_get_fcb(MAX_BOOTSTREAM_SIZE
);
499 printf("Unable to compile FCB\n");
503 dbbt
= mx28_nand_get_dbbt();
505 printf("Unable to compile DBBT\n");
509 ret
= mx28_nand_write_fcb(fcb
, buf
);
511 printf("Unable to write FCB to buffer\n");
515 ret
= mx28_nand_write_dbbt(dbbt
, buf
);
517 printf("Unable to write DBBT to buffer\n");
521 ret
= mx28_nand_write_firmware(fcb
, infd
, buf
);
523 printf("Unable to write firmware to buffer\n");
527 wr_size
= write(outfd
, buf
, size
);
528 if (wr_size
!= size
) {
545 static int mx28_create_sd_image(int infd
, int outfd
)
552 struct mx28_sd_config_block
*cb
;
554 fsize
= lseek(infd
, 0, SEEK_END
);
555 lseek(infd
, 0, SEEK_SET
);
556 size
= fsize
+ 4 * 512;
560 printf("Can not allocate output buffer of %d bytes\n", size
);
564 ret
= read(infd
, (uint8_t *)buf
+ 4 * 512, fsize
);
570 cb
= (struct mx28_sd_config_block
*)buf
;
572 cb
->signature
= 0x00112233;
573 cb
->primary_boot_tag
= 0x1;
574 cb
->secondary_boot_tag
= 0x1;
576 cb
->drv_info
[0].chip_num
= 0x0;
577 cb
->drv_info
[0].drive_type
= 0x0;
578 cb
->drv_info
[0].tag
= 0x1;
579 cb
->drv_info
[0].first_sector_number
= sd_sector
+ 4;
580 cb
->drv_info
[0].sector_count
= (size
- 4) / 512;
582 wr_size
= write(outfd
, buf
, size
);
583 if (wr_size
!= size
) {
596 static int parse_ops(int argc
, char **argv
)
614 for (i
= 1; i
< argc
; i
++) {
615 if (!strncmp(argv
[i
], "-w", 2))
617 else if (!strncmp(argv
[i
], "-o", 2))
619 else if (!strncmp(argv
[i
], "-e", 2))
621 else if (!strncmp(argv
[i
], "-p", 2))
626 tmp
= strtol(argv
[++i
], &end
, 10);
632 if (type
== PARAM_WRITE
)
633 nand_writesize
= tmp
;
634 if (type
== PARAM_OOB
)
636 if (type
== PARAM_ERASE
)
637 nand_erasesize
= tmp
;
638 if (type
== PARAM_PART
)
642 if (strcmp(argv
[i
], "sd") && strcmp(argv
[i
], "nand"))
651 int main(int argc
, char **argv
)
657 offset
= parse_ops(argc
, argv
);
664 infd
= open(argv
[offset
+ 1], O_RDONLY
);
666 printf("Input BootStream file can not be opened\n");
671 outfd
= open(argv
[offset
+ 2], O_CREAT
| O_TRUNC
| O_WRONLY
,
674 printf("Output file can not be created\n");
679 if (!strcmp(argv
[offset
], "sd"))
680 ret
= mx28_create_sd_image(infd
, outfd
);
681 else if (!strcmp(argv
[offset
], "nand"))
682 ret
= mx28_create_nand_image(infd
, outfd
);