1 /***************************************************************************
2 * Copyright (C) 2009 by Alexei Babich *
3 * Rezonans plc., Chelyabinsk, Russia *
6 * Copyright (C) 2010 by Gaetan CARLIER *
7 * Trump s.a., Belgium *
9 * Copyright (C) 2011 by Erik Ahlen *
10 * Avalon Innovation, Sweden *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
17 * This program is distributed in the hope that it will be useful, *
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20 * GNU General Public License for more details. *
22 * You should have received a copy of the GNU General Public License *
23 * along with this program; if not, write to the *
24 * Free Software Foundation, Inc., *
25 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
26 ***************************************************************************/
29 * Freescale iMX OpenOCD NAND Flash controller support.
30 * based on Freescale iMX2* and iMX3* OpenOCD NAND Flash controller support.
34 * driver tested with Samsung K9F2G08UXA and Numonyx/ST NAND02G-B2D @mxc
35 * tested "nand probe #", "nand erase # 0 #", "nand dump # file 0 #",
36 * "nand write # file 0", "nand verify"
38 * get_next_halfword_from_sram_buffer() not tested
39 * !! all function only tested with 2k page nand device; mxc_write_page
40 * writes the 4 MAIN_BUFFER's and is not compatible with < 2k page
41 * !! oob must be be used due to NFS bug
49 #include <target/target.h>
51 #define nfc_is_v1() (mxc_nf_info->mxc_version == MXC_VERSION_MX27 || \
52 mxc_nf_info->mxc_version == MXC_VERSION_MX31)
53 #define nfc_is_v2() (mxc_nf_info->mxc_version == MXC_VERSION_MX25 || \
54 mxc_nf_info->mxc_version == MXC_VERSION_MX35)
56 /* This permits to print (in LOG_INFO) how much bytes
57 * has been written after a page read or write.
58 * This is useful when OpenOCD is used with a graphical
59 * front-end to estimate progression of the global read/write
61 #undef _MXC_PRINT_STAT
62 /* #define _MXC_PRINT_STAT */
64 static const char target_not_halted_err_msg
[] =
65 "target must be halted to use mxc NAND flash controller";
66 static const char data_block_size_err_msg
[] =
67 "minimal granularity is one half-word, %" PRId32
" is incorrect";
68 static const char sram_buffer_bounds_err_msg
[] =
69 "trying to access out of SRAM buffer bound (addr=0x%" PRIx32
")";
70 static const char get_status_register_err_msg
[] = "can't get NAND status";
71 static uint32_t in_sram_address
;
72 static unsigned char sign_of_sequental_byte_read
;
74 static uint32_t align_address_v2(struct nand_device
*nand
, uint32_t addr
);
75 static int initialize_nf_controller(struct nand_device
*nand
);
76 static int get_next_byte_from_sram_buffer(struct nand_device
*nand
, uint8_t *value
);
77 static int get_next_halfword_from_sram_buffer(struct nand_device
*nand
, uint16_t *value
);
78 static int poll_for_complete_op(struct nand_device
*nand
, const char *text
);
79 static int validate_target_state(struct nand_device
*nand
);
80 static int do_data_output(struct nand_device
*nand
);
82 static int mxc_command(struct nand_device
*nand
, uint8_t command
);
83 static int mxc_address(struct nand_device
*nand
, uint8_t address
);
85 NAND_DEVICE_COMMAND_HANDLER(mxc_nand_device_command
)
87 struct mxc_nf_controller
*mxc_nf_info
;
91 mxc_nf_info
= malloc(sizeof(struct mxc_nf_controller
));
92 if (mxc_nf_info
== NULL
) {
93 LOG_ERROR("no memory for nand controller");
96 nand
->controller_priv
= mxc_nf_info
;
99 LOG_ERROR("use \"nand device mxc target mx25|mx27|mx31|mx35 noecc|hwecc [biswap]\"");
106 if (strcmp(CMD_ARGV
[2], "mx25") == 0) {
107 mxc_nf_info
->mxc_version
= MXC_VERSION_MX25
;
108 mxc_nf_info
->mxc_base_addr
= 0xBB000000;
109 mxc_nf_info
->mxc_regs_addr
= mxc_nf_info
->mxc_base_addr
+ 0x1E00;
110 } else if (strcmp(CMD_ARGV
[2], "mx27") == 0) {
111 mxc_nf_info
->mxc_version
= MXC_VERSION_MX27
;
112 mxc_nf_info
->mxc_base_addr
= 0xD8000000;
113 mxc_nf_info
->mxc_regs_addr
= mxc_nf_info
->mxc_base_addr
+ 0x0E00;
114 } else if (strcmp(CMD_ARGV
[2], "mx31") == 0) {
115 mxc_nf_info
->mxc_version
= MXC_VERSION_MX31
;
116 mxc_nf_info
->mxc_base_addr
= 0xB8000000;
117 mxc_nf_info
->mxc_regs_addr
= mxc_nf_info
->mxc_base_addr
+ 0x0E00;
118 } else if (strcmp(CMD_ARGV
[2], "mx35") == 0) {
119 mxc_nf_info
->mxc_version
= MXC_VERSION_MX35
;
120 mxc_nf_info
->mxc_base_addr
= 0xBB000000;
121 mxc_nf_info
->mxc_regs_addr
= mxc_nf_info
->mxc_base_addr
+ 0x1E00;
125 * check hwecc requirements
127 hwecc_needed
= strcmp(CMD_ARGV
[3], "hwecc");
128 if (hwecc_needed
== 0)
129 mxc_nf_info
->flags
.hw_ecc_enabled
= 1;
131 mxc_nf_info
->flags
.hw_ecc_enabled
= 0;
133 mxc_nf_info
->optype
= MXC_NF_DATAOUT_PAGE
;
134 mxc_nf_info
->fin
= MXC_NF_FIN_NONE
;
135 mxc_nf_info
->flags
.target_little_endian
=
136 (nand
->target
->endianness
== TARGET_LITTLE_ENDIAN
);
139 * should factory bad block indicator be swaped
140 * as a workaround for how the nfc handles pages.
142 if (CMD_ARGC
> 4 && strcmp(CMD_ARGV
[4], "biswap") == 0) {
143 LOG_DEBUG("BI-swap enabled");
144 mxc_nf_info
->flags
.biswap_enabled
= 1;
148 * testing host endianness
151 if (*(char *) &x
== 1)
152 mxc_nf_info
->flags
.host_little_endian
= 1;
154 mxc_nf_info
->flags
.host_little_endian
= 0;
158 COMMAND_HANDLER(handle_mxc_biswap_command
)
160 struct nand_device
*nand
= NULL
;
161 struct mxc_nf_controller
*mxc_nf_info
= NULL
;
163 if (CMD_ARGC
< 1 || CMD_ARGC
> 2)
164 return ERROR_COMMAND_SYNTAX_ERROR
;
166 int retval
= CALL_COMMAND_HANDLER(nand_command_get_device
, 0, &nand
);
167 if (retval
!= ERROR_OK
) {
168 command_print(CMD_CTX
, "invalid nand device number or name: %s", CMD_ARGV
[0]);
169 return ERROR_COMMAND_ARGUMENT_INVALID
;
172 mxc_nf_info
= nand
->controller_priv
;
174 if (strcmp(CMD_ARGV
[1], "enable") == 0)
175 mxc_nf_info
->flags
.biswap_enabled
= true;
177 mxc_nf_info
->flags
.biswap_enabled
= false;
179 if (mxc_nf_info
->flags
.biswap_enabled
)
180 command_print(CMD_CTX
, "BI-swapping enabled on %s", nand
->name
);
182 command_print(CMD_CTX
, "BI-swapping disabled on %s", nand
->name
);
187 static const struct command_registration mxc_sub_command_handlers
[] = {
190 .handler
= handle_mxc_biswap_command
,
191 .help
= "Turns on/off bad block information swaping from main area, "
192 "without parameter query status.",
193 .usage
= "bank_id ['enable'|'disable']",
195 COMMAND_REGISTRATION_DONE
198 static const struct command_registration mxc_nand_command_handler
[] = {
202 .help
= "MXC NAND flash controller commands",
203 .chain
= mxc_sub_command_handlers
205 COMMAND_REGISTRATION_DONE
208 static int mxc_init(struct nand_device
*nand
)
210 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
211 struct target
*target
= nand
->target
;
213 int validate_target_result
;
214 uint16_t buffsize_register_content
;
215 uint32_t sreg_content
;
216 uint32_t SREG
= MX2_FMCR
;
217 uint32_t SEL_16BIT
= MX2_FMCR_NF_16BIT_SEL
;
218 uint32_t SEL_FMS
= MX2_FMCR_NF_FMS
;
220 uint16_t nand_status_content
;
222 * validate target state
224 validate_target_result
= validate_target_state(nand
);
225 if (validate_target_result
!= ERROR_OK
)
226 return validate_target_result
;
229 target_read_u16(target
, MXC_NF_BUFSIZ
, &buffsize_register_content
);
230 mxc_nf_info
->flags
.one_kb_sram
= !(buffsize_register_content
& 0x000f);
232 mxc_nf_info
->flags
.one_kb_sram
= 0;
234 if (mxc_nf_info
->mxc_version
== MXC_VERSION_MX31
) {
236 SEL_16BIT
= MX3_PCSR_NF_16BIT_SEL
;
237 SEL_FMS
= MX3_PCSR_NF_FMS
;
238 } else if (mxc_nf_info
->mxc_version
== MXC_VERSION_MX25
) {
240 SEL_16BIT
= MX25_RCSR_NF_16BIT_SEL
;
241 SEL_FMS
= MX25_RCSR_NF_FMS
;
242 } else if (mxc_nf_info
->mxc_version
== MXC_VERSION_MX35
) {
244 SEL_16BIT
= MX35_RCSR_NF_16BIT_SEL
;
245 SEL_FMS
= MX35_RCSR_NF_FMS
;
248 target_read_u32(target
, SREG
, &sreg_content
);
249 if (!nand
->bus_width
) {
250 /* bus_width not yet defined. Read it from MXC_FMCR */
251 nand
->bus_width
= (sreg_content
& SEL_16BIT
) ? 16 : 8;
253 /* bus_width forced in soft. Sync it to MXC_FMCR */
254 sreg_content
|= ((nand
->bus_width
== 16) ? SEL_16BIT
: 0x00000000);
255 target_write_u32(target
, SREG
, sreg_content
);
257 if (nand
->bus_width
== 16)
258 LOG_DEBUG("MXC_NF : bus is 16-bit width");
260 LOG_DEBUG("MXC_NF : bus is 8-bit width");
262 if (!nand
->page_size
) {
263 nand
->page_size
= (sreg_content
& SEL_FMS
) ? 2048 : 512;
265 sreg_content
|= ((nand
->page_size
== 2048) ? SEL_FMS
: 0x00000000);
266 target_write_u32(target
, SREG
, sreg_content
);
268 if (mxc_nf_info
->flags
.one_kb_sram
&& (nand
->page_size
== 2048)) {
269 LOG_ERROR("NAND controller have only 1 kb SRAM, so "
270 "pagesize 2048 is incompatible with it");
272 LOG_DEBUG("MXC_NF : NAND controller can handle pagesize of 2048");
275 if (nfc_is_v2() && sreg_content
& MX35_RCSR_NF_4K
)
276 LOG_ERROR("MXC driver does not have support for 4k pagesize.");
278 initialize_nf_controller(nand
);
281 retval
|= mxc_command(nand
, NAND_CMD_STATUS
);
282 retval
|= mxc_address(nand
, 0x00);
283 retval
|= do_data_output(nand
);
284 if (retval
!= ERROR_OK
) {
285 LOG_ERROR(get_status_register_err_msg
);
288 target_read_u16(target
, MXC_NF_MAIN_BUFFER0
, &nand_status_content
);
289 if (!(nand_status_content
& 0x0080)) {
290 LOG_INFO("NAND read-only");
291 mxc_nf_info
->flags
.nand_readonly
= 1;
293 mxc_nf_info
->flags
.nand_readonly
= 0;
298 static int mxc_read_data(struct nand_device
*nand
, void *data
)
300 int validate_target_result
;
301 int try_data_output_from_nand_chip
;
303 * validate target state
305 validate_target_result
= validate_target_state(nand
);
306 if (validate_target_result
!= ERROR_OK
)
307 return validate_target_result
;
310 * get data from nand chip
312 try_data_output_from_nand_chip
= do_data_output(nand
);
313 if (try_data_output_from_nand_chip
!= ERROR_OK
) {
314 LOG_ERROR("mxc_read_data : read data failed : '%x'",
315 try_data_output_from_nand_chip
);
316 return try_data_output_from_nand_chip
;
319 if (nand
->bus_width
== 16)
320 get_next_halfword_from_sram_buffer(nand
, data
);
322 get_next_byte_from_sram_buffer(nand
, data
);
327 static int mxc_write_data(struct nand_device
*nand
, uint16_t data
)
329 LOG_ERROR("write_data() not implemented");
330 return ERROR_NAND_OPERATION_FAILED
;
333 static int mxc_reset(struct nand_device
*nand
)
336 * validate target state
338 int validate_target_result
;
339 validate_target_result
= validate_target_state(nand
);
340 if (validate_target_result
!= ERROR_OK
)
341 return validate_target_result
;
342 initialize_nf_controller(nand
);
346 static int mxc_command(struct nand_device
*nand
, uint8_t command
)
348 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
349 struct target
*target
= nand
->target
;
350 int validate_target_result
;
353 * validate target state
355 validate_target_result
= validate_target_state(nand
);
356 if (validate_target_result
!= ERROR_OK
)
357 return validate_target_result
;
360 case NAND_CMD_READOOB
:
361 command
= NAND_CMD_READ0
;
362 /* set read point for data_read() and read_block_data() to
363 * spare area in SRAM buffer
366 in_sram_address
= MXC_NF_V1_SPARE_BUFFER0
;
368 in_sram_address
= MXC_NF_V2_SPARE_BUFFER0
;
371 command
= NAND_CMD_READ0
;
373 * offset == one half of page size
375 in_sram_address
= MXC_NF_MAIN_BUFFER0
+ (nand
->page_size
>> 1);
378 in_sram_address
= MXC_NF_MAIN_BUFFER0
;
382 target_write_u16(target
, MXC_NF_FCMD
, command
);
384 * start command input operation (set MXC_NF_BIT_OP_DONE==0)
386 target_write_u16(target
, MXC_NF_CFG2
, MXC_NF_BIT_OP_FCI
);
387 poll_result
= poll_for_complete_op(nand
, "command");
388 if (poll_result
!= ERROR_OK
)
391 * reset cursor to begin of the buffer
393 sign_of_sequental_byte_read
= 0;
394 /* Handle special read command and adjust NF_CFG2(FDO) */
396 case NAND_CMD_READID
:
397 mxc_nf_info
->optype
= MXC_NF_DATAOUT_NANDID
;
398 mxc_nf_info
->fin
= MXC_NF_FIN_DATAOUT
;
400 case NAND_CMD_STATUS
:
401 mxc_nf_info
->optype
= MXC_NF_DATAOUT_NANDSTATUS
;
402 mxc_nf_info
->fin
= MXC_NF_FIN_DATAOUT
;
403 target_write_u16 (target
, MXC_NF_BUFADDR
, 0);
407 mxc_nf_info
->fin
= MXC_NF_FIN_DATAOUT
;
408 mxc_nf_info
->optype
= MXC_NF_DATAOUT_PAGE
;
411 /* Ohter command use the default 'One page data out' FDO */
412 mxc_nf_info
->optype
= MXC_NF_DATAOUT_PAGE
;
418 static int mxc_address(struct nand_device
*nand
, uint8_t address
)
420 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
421 struct target
*target
= nand
->target
;
422 int validate_target_result
;
425 * validate target state
427 validate_target_result
= validate_target_state(nand
);
428 if (validate_target_result
!= ERROR_OK
)
429 return validate_target_result
;
431 target_write_u16(target
, MXC_NF_FADDR
, address
);
433 * start address input operation (set MXC_NF_BIT_OP_DONE==0)
435 target_write_u16(target
, MXC_NF_CFG2
, MXC_NF_BIT_OP_FAI
);
436 poll_result
= poll_for_complete_op(nand
, "address");
437 if (poll_result
!= ERROR_OK
)
443 static int mxc_nand_ready(struct nand_device
*nand
, int tout
)
445 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
446 struct target
*target
= nand
->target
;
447 uint16_t poll_complete_status
;
448 int validate_target_result
;
451 * validate target state
453 validate_target_result
= validate_target_state(nand
);
454 if (validate_target_result
!= ERROR_OK
)
455 return validate_target_result
;
458 target_read_u16(target
, MXC_NF_CFG2
, &poll_complete_status
);
459 if (poll_complete_status
& MXC_NF_BIT_OP_DONE
)
468 static int mxc_write_page(struct nand_device
*nand
, uint32_t page
,
469 uint8_t *data
, uint32_t data_size
,
470 uint8_t *oob
, uint32_t oob_size
)
472 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
473 struct target
*target
= nand
->target
;
475 uint16_t nand_status_content
;
476 uint16_t swap1
, swap2
, new_swap1
;
481 LOG_ERROR(data_block_size_err_msg
, data_size
);
482 return ERROR_NAND_OPERATION_FAILED
;
485 LOG_ERROR(data_block_size_err_msg
, oob_size
);
486 return ERROR_NAND_OPERATION_FAILED
;
489 LOG_ERROR("nothing to program");
490 return ERROR_NAND_OPERATION_FAILED
;
494 * validate target state
496 retval
= validate_target_state(nand
);
497 if (retval
!= ERROR_OK
)
500 in_sram_address
= MXC_NF_MAIN_BUFFER0
;
501 sign_of_sequental_byte_read
= 0;
503 retval
|= mxc_command(nand
, NAND_CMD_SEQIN
);
504 retval
|= mxc_address(nand
, 0); /* col */
505 retval
|= mxc_address(nand
, 0); /* col */
506 retval
|= mxc_address(nand
, page
& 0xff); /* page address */
507 retval
|= mxc_address(nand
, (page
>> 8) & 0xff); /* page address */
508 retval
|= mxc_address(nand
, (page
>> 16) & 0xff); /* page address */
510 target_write_buffer(target
, MXC_NF_MAIN_BUFFER0
, data_size
, data
);
512 if (mxc_nf_info
->flags
.hw_ecc_enabled
) {
514 * part of spare block will be overrided by hardware
517 LOG_DEBUG("part of spare block will be overrided "
518 "by hardware ECC generator");
521 target_write_buffer(target
, MXC_NF_V1_SPARE_BUFFER0
, oob_size
, oob
);
523 uint32_t addr
= MXC_NF_V2_SPARE_BUFFER0
;
524 while (oob_size
> 0) {
525 uint8_t len
= MIN(oob_size
, MXC_NF_SPARE_BUFFER_LEN
);
526 target_write_buffer(target
, addr
, len
, oob
);
527 addr
= align_address_v2(nand
, addr
+ len
);
534 if (nand
->page_size
> 512 && mxc_nf_info
->flags
.biswap_enabled
) {
535 /* BI-swap - work-around of i.MX NFC for NAND device with page == 2kb*/
536 target_read_u16(target
, MXC_NF_MAIN_BUFFER3
+ 464, &swap1
);
538 LOG_ERROR("Due to NFC Bug, oob is not correctly implemented in mxc driver");
539 return ERROR_NAND_OPERATION_FAILED
;
541 swap2
= 0xffff; /* Spare buffer unused forced to 0xffff */
542 new_swap1
= (swap1
& 0xFF00) | (swap2
>> 8);
543 swap2
= (swap1
<< 8) | (swap2
& 0xFF);
544 target_write_u16(target
, MXC_NF_MAIN_BUFFER3
+ 464, new_swap1
);
546 target_write_u16(target
, MXC_NF_V1_SPARE_BUFFER3
, swap2
);
548 target_write_u16(target
, MXC_NF_V2_SPARE_BUFFER3
, swap2
);
552 * start data input operation (set MXC_NF_BIT_OP_DONE==0)
554 if (nfc_is_v1() && nand
->page_size
> 512)
559 for (uint8_t i
= 0 ; i
< bufs
; ++i
) {
560 target_write_u16(target
, MXC_NF_BUFADDR
, i
);
561 target_write_u16(target
, MXC_NF_CFG2
, MXC_NF_BIT_OP_FDI
);
562 poll_result
= poll_for_complete_op(nand
, "data input");
563 if (poll_result
!= ERROR_OK
)
567 retval
|= mxc_command(nand
, NAND_CMD_PAGEPROG
);
568 if (retval
!= ERROR_OK
)
572 * check status register
575 retval
|= mxc_command(nand
, NAND_CMD_STATUS
);
576 target_write_u16 (target
, MXC_NF_BUFADDR
, 0);
577 mxc_nf_info
->optype
= MXC_NF_DATAOUT_NANDSTATUS
;
578 mxc_nf_info
->fin
= MXC_NF_FIN_DATAOUT
;
579 retval
|= do_data_output(nand
);
580 if (retval
!= ERROR_OK
) {
581 LOG_ERROR(get_status_register_err_msg
);
584 target_read_u16(target
, MXC_NF_MAIN_BUFFER0
, &nand_status_content
);
585 if (nand_status_content
& 0x0001) {
587 * page not correctly written
589 return ERROR_NAND_OPERATION_FAILED
;
591 #ifdef _MXC_PRINT_STAT
592 LOG_INFO("%d bytes newly written", data_size
);
597 static int mxc_read_page(struct nand_device
*nand
, uint32_t page
,
598 uint8_t *data
, uint32_t data_size
,
599 uint8_t *oob
, uint32_t oob_size
)
601 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
602 struct target
*target
= nand
->target
;
605 uint16_t swap1
, swap2
, new_swap1
;
608 LOG_ERROR(data_block_size_err_msg
, data_size
);
609 return ERROR_NAND_OPERATION_FAILED
;
612 LOG_ERROR(data_block_size_err_msg
, oob_size
);
613 return ERROR_NAND_OPERATION_FAILED
;
617 * validate target state
619 retval
= validate_target_state(nand
);
620 if (retval
!= ERROR_OK
) {
623 /* Reset address_cycles before mxc_command ?? */
624 retval
= mxc_command(nand
, NAND_CMD_READ0
);
625 if (retval
!= ERROR_OK
) return retval
;
626 retval
= mxc_address(nand
, 0); /* col */
627 if (retval
!= ERROR_OK
) return retval
;
628 retval
= mxc_address(nand
, 0); /* col */
629 if (retval
!= ERROR_OK
) return retval
;
630 retval
= mxc_address(nand
, page
& 0xff); /* page address */
631 if (retval
!= ERROR_OK
) return retval
;
632 retval
= mxc_address(nand
, (page
>> 8) & 0xff); /* page address */
633 if (retval
!= ERROR_OK
) return retval
;
634 retval
= mxc_address(nand
, (page
>> 16) & 0xff); /* page address */
635 if (retval
!= ERROR_OK
) return retval
;
636 retval
= mxc_command(nand
, NAND_CMD_READSTART
);
637 if (retval
!= ERROR_OK
) return retval
;
639 if (nfc_is_v1() && nand
->page_size
> 512)
644 for (uint8_t i
= 0 ; i
< bufs
; ++i
) {
645 target_write_u16(target
, MXC_NF_BUFADDR
, i
);
646 mxc_nf_info
->fin
= MXC_NF_FIN_DATAOUT
;
647 retval
= do_data_output(nand
);
648 if (retval
!= ERROR_OK
) {
649 LOG_ERROR("MXC_NF : Error reading page %d", i
);
654 if (nand
->page_size
> 512 && mxc_nf_info
->flags
.biswap_enabled
) {
655 uint32_t SPARE_BUFFER3
;
656 /* BI-swap - work-around of mxc NFC for NAND device with page == 2k */
657 target_read_u16(target
, MXC_NF_MAIN_BUFFER3
+ 464, &swap1
);
659 SPARE_BUFFER3
= MXC_NF_V1_SPARE_BUFFER3
;
661 SPARE_BUFFER3
= MXC_NF_V2_SPARE_BUFFER3
;
662 target_read_u16(target
, SPARE_BUFFER3
, &swap2
);
663 new_swap1
= (swap1
& 0xFF00) | (swap2
>> 8);
664 swap2
= (swap1
<< 8) | (swap2
& 0xFF);
665 target_write_u16(target
, MXC_NF_MAIN_BUFFER3
+ 464, new_swap1
);
666 target_write_u16(target
, SPARE_BUFFER3
, swap2
);
670 target_read_buffer(target
, MXC_NF_MAIN_BUFFER0
, data_size
, data
);
673 target_read_buffer(target
, MXC_NF_V1_SPARE_BUFFER0
, oob_size
, oob
);
675 uint32_t addr
= MXC_NF_V2_SPARE_BUFFER0
;
676 while (oob_size
> 0) {
677 uint8_t len
= MIN(oob_size
, MXC_NF_SPARE_BUFFER_LEN
);
678 target_read_buffer(target
, addr
, len
, oob
);
679 addr
= align_address_v2(nand
, addr
+ len
);
686 #ifdef _MXC_PRINT_STAT
688 /* When Operation Status is read (when page is erased),
689 * this function is used but data_size is null.
691 LOG_INFO("%d bytes newly read", data_size
);
697 static uint32_t align_address_v2(struct nand_device
*nand
, uint32_t addr
)
699 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
701 if (addr
> MXC_NF_V2_SPARE_BUFFER0
&&
702 (addr
& 0x1F) == MXC_NF_SPARE_BUFFER_LEN
) {
703 ret
+= MXC_NF_SPARE_BUFFER_MAX
- MXC_NF_SPARE_BUFFER_LEN
;
704 } else if (addr
>= (mxc_nf_info
->mxc_base_addr
+ (uint32_t)nand
->page_size
))
705 ret
= MXC_NF_V2_SPARE_BUFFER0
;
709 static int initialize_nf_controller(struct nand_device
*nand
)
711 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
712 struct target
*target
= nand
->target
;
713 uint16_t work_mode
= 0;
716 * resets NAND flash controller in zero time ? I dont know.
718 target_write_u16(target
, MXC_NF_CFG1
, MXC_NF_BIT_RESET_EN
);
719 if (mxc_nf_info
->mxc_version
== MXC_VERSION_MX27
)
720 work_mode
= MXC_NF_BIT_INT_DIS
; /* disable interrupt */
722 if (target
->endianness
== TARGET_BIG_ENDIAN
) {
723 LOG_DEBUG("MXC_NF : work in Big Endian mode");
724 work_mode
|= MXC_NF_BIT_BE_EN
;
726 LOG_DEBUG("MXC_NF : work in Little Endian mode");
728 if (mxc_nf_info
->flags
.hw_ecc_enabled
) {
729 LOG_DEBUG("MXC_NF : work with ECC mode");
730 work_mode
|= MXC_NF_BIT_ECC_EN
;
732 LOG_DEBUG("MXC_NF : work without ECC mode");
735 if (nand
->page_size
) {
736 uint16_t pages_per_block
= nand
->erase_size
/ nand
->page_size
;
737 work_mode
|= MXC_NF_V2_CFG1_PPB(ffs(pages_per_block
) - 6);
739 work_mode
|= MXC_NF_BIT_ECC_4BIT
;
741 target_write_u16(target
, MXC_NF_CFG1
, work_mode
);
744 * unlock SRAM buffer for write; 2 mean "Unlock", other values means "Lock"
746 target_write_u16(target
, MXC_NF_BUFCFG
, 2);
747 target_read_u16(target
, MXC_NF_FWP
, &temp
);
748 if ((temp
& 0x0007) == 1) {
749 LOG_ERROR("NAND flash is tight-locked, reset needed");
754 * unlock NAND flash for write
757 target_write_u16(target
, MXC_NF_V1_UNLOCKSTART
, 0x0000);
758 target_write_u16(target
, MXC_NF_V1_UNLOCKEND
, 0xFFFF);
760 target_write_u16(target
, MXC_NF_V2_UNLOCKSTART0
, 0x0000);
761 target_write_u16(target
, MXC_NF_V2_UNLOCKSTART1
, 0x0000);
762 target_write_u16(target
, MXC_NF_V2_UNLOCKSTART2
, 0x0000);
763 target_write_u16(target
, MXC_NF_V2_UNLOCKSTART3
, 0x0000);
764 target_write_u16(target
, MXC_NF_V2_UNLOCKEND0
, 0xFFFF);
765 target_write_u16(target
, MXC_NF_V2_UNLOCKEND1
, 0xFFFF);
766 target_write_u16(target
, MXC_NF_V2_UNLOCKEND2
, 0xFFFF);
767 target_write_u16(target
, MXC_NF_V2_UNLOCKEND3
, 0xFFFF);
769 target_write_u16(target
, MXC_NF_FWP
, 4);
772 * 0x0000 means that first SRAM buffer @base_addr will be used
774 target_write_u16(target
, MXC_NF_BUFADDR
, 0x0000);
776 * address of SRAM buffer
778 in_sram_address
= MXC_NF_MAIN_BUFFER0
;
779 sign_of_sequental_byte_read
= 0;
783 static int get_next_byte_from_sram_buffer(struct nand_device
*nand
, uint8_t *value
)
785 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
786 struct target
*target
= nand
->target
;
787 static uint8_t even_byte
= 0;
792 if (sign_of_sequental_byte_read
== 0)
795 if (in_sram_address
>
796 (nfc_is_v1() ? MXC_NF_V1_LAST_BUFFADDR
: MXC_NF_V2_LAST_BUFFADDR
)) {
797 LOG_ERROR(sram_buffer_bounds_err_msg
, in_sram_address
);
799 sign_of_sequental_byte_read
= 0;
801 return ERROR_NAND_OPERATION_FAILED
;
804 in_sram_address
= align_address_v2(nand
, in_sram_address
);
806 target_read_u16(target
, in_sram_address
, &temp
);
810 in_sram_address
+= 2;
812 *value
= temp
& 0xff;
816 sign_of_sequental_byte_read
= 1;
820 static int get_next_halfword_from_sram_buffer(struct nand_device
*nand
, uint16_t *value
)
822 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
823 struct target
*target
= nand
->target
;
825 if (in_sram_address
>
826 (nfc_is_v1() ? MXC_NF_V1_LAST_BUFFADDR
: MXC_NF_V2_LAST_BUFFADDR
)) {
827 LOG_ERROR(sram_buffer_bounds_err_msg
, in_sram_address
);
829 return ERROR_NAND_OPERATION_FAILED
;
832 in_sram_address
= align_address_v2(nand
, in_sram_address
);
834 target_read_u16(target
, in_sram_address
, value
);
835 in_sram_address
+= 2;
840 static int poll_for_complete_op(struct nand_device
*nand
, const char *text
)
842 if (mxc_nand_ready(nand
, 1000) == -1) {
843 LOG_ERROR("%s sending timeout", text
);
844 return ERROR_NAND_OPERATION_FAILED
;
849 static int validate_target_state(struct nand_device
*nand
)
851 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
852 struct target
*target
= nand
->target
;
854 if (target
->state
!= TARGET_HALTED
) {
855 LOG_ERROR(target_not_halted_err_msg
);
856 return ERROR_NAND_OPERATION_FAILED
;
859 if (mxc_nf_info
->flags
.target_little_endian
!=
860 (target
->endianness
== TARGET_LITTLE_ENDIAN
)) {
862 * endianness changed after NAND controller probed
864 return ERROR_NAND_OPERATION_FAILED
;
869 int ecc_status_v1(struct nand_device
*nand
)
871 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
872 struct target
*target
= nand
->target
;
875 target_read_u16(target
, MXC_NF_ECCSTATUS
, &ecc_status
);
876 switch (ecc_status
& 0x000c) {
878 LOG_INFO("main area read with 1 (correctable) error");
881 LOG_INFO("main area read with more than 1 (incorrectable) error");
882 return ERROR_NAND_OPERATION_FAILED
;
885 switch (ecc_status
& 0x0003) {
887 LOG_INFO("spare area read with 1 (correctable) error");
890 LOG_INFO("main area read with more than 1 (incorrectable) error");
891 return ERROR_NAND_OPERATION_FAILED
;
897 int ecc_status_v2(struct nand_device
*nand
)
899 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
900 struct target
*target
= nand
->target
;
905 no_subpages
= nand
->page_size
>> 9;
907 target_read_u16(target
, MXC_NF_ECCSTATUS
, &ecc_status
);
909 err
= ecc_status
& 0xF;
911 LOG_INFO("UnCorrectable RS-ECC Error");
912 return ERROR_NAND_OPERATION_FAILED
;
914 LOG_INFO("%d Symbol Correctable RS-ECC Error", err
);
916 } while (--no_subpages
);
920 static int do_data_output(struct nand_device
*nand
)
922 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
923 struct target
*target
= nand
->target
;
925 switch (mxc_nf_info
->fin
) {
926 case MXC_NF_FIN_DATAOUT
:
928 * start data output operation (set MXC_NF_BIT_OP_DONE==0)
930 target_write_u16(target
, MXC_NF_CFG2
, MXC_NF_BIT_DATAOUT_TYPE(mxc_nf_info
->optype
));
931 poll_result
= poll_for_complete_op(nand
, "data output");
932 if (poll_result
!= ERROR_OK
)
935 mxc_nf_info
->fin
= MXC_NF_FIN_NONE
;
939 if (mxc_nf_info
->optype
== MXC_NF_DATAOUT_PAGE
&&
940 mxc_nf_info
->flags
.hw_ecc_enabled
) {
943 ecc_status
= ecc_status_v1(nand
);
945 ecc_status
= ecc_status_v2(nand
);
946 if (ecc_status
!= ERROR_OK
)
950 case MXC_NF_FIN_NONE
:
956 struct nand_flash_controller mxc_nand_flash_controller
= {
958 .nand_device_command
= &mxc_nand_device_command
,
959 .commands
= mxc_nand_command_handler
,
962 .command
= &mxc_command
,
963 .address
= &mxc_address
,
964 .write_data
= &mxc_write_data
,
965 .read_data
= &mxc_read_data
,
966 .write_page
= &mxc_write_page
,
967 .read_page
= &mxc_read_page
,
968 .nand_ready
= &mxc_nand_ready
,
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)