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 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU General Public License for more details. *
19 * You should have received a copy of the GNU General Public License *
20 * along with this program; if not, write to the *
21 * Free Software Foundation, Inc., *
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23 ***************************************************************************/
26 * Freescale iMX OpenOCD NAND Flash controller support.
27 * based on Freescale iMX2* and iMX3* OpenOCD NAND Flash controller support.
31 * driver tested with Samsung K9F2G08UXA and Numonyx/ST NAND02G-B2D @mxc
32 * tested "nand probe #", "nand erase # 0 #", "nand dump # file 0 #",
33 * "nand write # file 0", "nand verify"
35 * get_next_halfword_from_sram_buffer() not tested
36 * !! all function only tested with 2k page nand device; mxc_write_page
37 * writes the 4 MAIN_BUFFER's and is not compatible with < 2k page
38 * !! oob must be be used due to NFS bug
46 #include <target/target.h>
48 /* This permits to print (in LOG_INFO) how much bytes
49 * has been written after a page read or write.
50 * This is useful when OpenOCD is used with a graphical
51 * front-end to estimate progression of the global read/write
53 #undef _MXC_PRINT_STAT
54 /* #define _MXC_PRINT_STAT */
56 static const char target_not_halted_err_msg
[] =
57 "target must be halted to use mxc NAND flash controller";
58 static const char data_block_size_err_msg
[] =
59 "minimal granularity is one half-word, %" PRId32
" is incorrect";
60 static const char sram_buffer_bounds_err_msg
[] =
61 "trying to access out of SRAM buffer bound (addr=0x%" PRIx32
")";
62 static const char get_status_register_err_msg
[] = "can't get NAND status";
63 static uint32_t in_sram_address
;
64 static unsigned char sign_of_sequental_byte_read
;
66 static int initialize_nf_controller(struct nand_device
*nand
);
67 static int get_next_byte_from_sram_buffer(struct nand_device
*nand
, uint8_t *value
);
68 static int get_next_halfword_from_sram_buffer(struct nand_device
*nand
, uint16_t *value
);
69 static int poll_for_complete_op(struct nand_device
*nand
, const char *text
);
70 static int validate_target_state(struct nand_device
*nand
);
71 static int do_data_output(struct nand_device
*nand
);
73 static int mxc_command(struct nand_device
*nand
, uint8_t command
);
74 static int mxc_address(struct nand_device
*nand
, uint8_t address
);
76 NAND_DEVICE_COMMAND_HANDLER(mxc_nand_device_command
)
78 struct mxc_nf_controller
*mxc_nf_info
;
82 mxc_nf_info
= malloc(sizeof(struct mxc_nf_controller
));
83 if (mxc_nf_info
== NULL
) {
84 LOG_ERROR("no memory for nand controller");
87 nand
->controller_priv
= mxc_nf_info
;
90 LOG_ERROR("use \"nand device mxc target mx27|mx31|mx35 noecc|hwecc [biswap]\"");
97 if (strcmp(CMD_ARGV
[2], "mx27") == 0)
98 mxc_nf_info
->mxc_base_addr
= 0xD8000000;
99 else if (strcmp(CMD_ARGV
[2], "mx31") == 0)
100 mxc_nf_info
->mxc_base_addr
= 0xB8000000;
101 else if (strcmp(CMD_ARGV
[2], "mx35") == 0)
102 mxc_nf_info
->mxc_base_addr
= 0xBB000000;
105 * check hwecc requirements
107 hwecc_needed
= strcmp(CMD_ARGV
[3], "hwecc");
108 if (hwecc_needed
== 0)
109 mxc_nf_info
->flags
.hw_ecc_enabled
= 1;
111 mxc_nf_info
->flags
.hw_ecc_enabled
= 0;
113 mxc_nf_info
->optype
= MXC_NF_DATAOUT_PAGE
;
114 mxc_nf_info
->fin
= MXC_NF_FIN_NONE
;
115 mxc_nf_info
->flags
.target_little_endian
=
116 (nand
->target
->endianness
== TARGET_LITTLE_ENDIAN
);
119 * should factory bad block indicator be swaped
120 * as a workaround for how the nfc handles pages.
122 if (CMD_ARGC
> 4 && strcmp(CMD_ARGV
[4], "biswap") == 0) {
123 LOG_DEBUG("BI-swap enabled");
124 mxc_nf_info
->flags
.biswap_enabled
= 1;
128 * testing host endianness
131 if (*(char *) &x
== 1)
132 mxc_nf_info
->flags
.host_little_endian
= 1;
134 mxc_nf_info
->flags
.host_little_endian
= 0;
138 COMMAND_HANDLER(handle_mxc_biswap_command
)
140 struct nand_device
*nand
= NULL
;
141 struct mxc_nf_controller
*mxc_nf_info
= NULL
;
143 if (CMD_ARGC
< 1 || CMD_ARGC
> 2)
144 return ERROR_COMMAND_SYNTAX_ERROR
;
146 int retval
= CALL_COMMAND_HANDLER(nand_command_get_device
, 0, &nand
);
147 if (retval
!= ERROR_OK
) {
148 command_print(CMD_CTX
, "invalid nand device number or name: %s", CMD_ARGV
[0]);
149 return ERROR_COMMAND_ARGUMENT_INVALID
;
152 mxc_nf_info
= nand
->controller_priv
;
154 if (strcmp(CMD_ARGV
[1], "enable") == 0)
155 mxc_nf_info
->flags
.biswap_enabled
= true;
157 mxc_nf_info
->flags
.biswap_enabled
= false;
159 if (mxc_nf_info
->flags
.biswap_enabled
)
160 command_print(CMD_CTX
, "BI-swapping enabled on %s", nand
->name
);
162 command_print(CMD_CTX
, "BI-swapping disabled on %s", nand
->name
);
167 static const struct command_registration mxc_sub_command_handlers
[] = {
170 .handler
= handle_mxc_biswap_command
,
171 .help
= "Turns on/off bad block information swaping from main area, "
172 "without parameter query status.",
173 .usage
= "bank_id ['enable'|'disable']",
175 COMMAND_REGISTRATION_DONE
178 static const struct command_registration mxc_nand_command_handler
[] = {
182 .help
= "MXC NAND flash controller commands",
183 .chain
= mxc_sub_command_handlers
185 COMMAND_REGISTRATION_DONE
188 static int mxc_init(struct nand_device
*nand
)
190 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
191 struct target
*target
= nand
->target
;
193 int validate_target_result
;
194 uint16_t buffsize_register_content
;
195 uint32_t pcsr_register_content
;
197 uint16_t nand_status_content
;
199 * validate target state
201 validate_target_result
= validate_target_state(nand
);
202 if (validate_target_result
!= ERROR_OK
)
203 return validate_target_result
;
205 target_read_u16(target
, MXC_NF_BUFSIZ
, &buffsize_register_content
);
206 mxc_nf_info
->flags
.one_kb_sram
= !(buffsize_register_content
& 0x000f);
208 target_read_u32(target
, MXC_FMCR
, &pcsr_register_content
);
209 if (!nand
->bus_width
) {
210 /* bus_width not yet defined. Read it from MXC_FMCR */
212 (pcsr_register_content
& MXC_FMCR_NF_16BIT_SEL
) ? 16 : 8;
214 /* bus_width forced in soft. Sync it to MXC_FMCR */
215 pcsr_register_content
|=
216 ((nand
->bus_width
== 16) ? MXC_FMCR_NF_16BIT_SEL
: 0x00000000);
217 target_write_u32(target
, MXC_FMCR
, pcsr_register_content
);
219 if (nand
->bus_width
== 16)
220 LOG_DEBUG("MXC_NF : bus is 16-bit width");
222 LOG_DEBUG("MXC_NF : bus is 8-bit width");
224 if (!nand
->page_size
) {
225 nand
->page_size
= (pcsr_register_content
& MXC_FMCR_NF_FMS
) ? 2048 : 512;
227 pcsr_register_content
|=
228 ((nand
->page_size
== 2048) ? MXC_FMCR_NF_FMS
: 0x00000000);
229 target_write_u32(target
, MXC_FMCR
, pcsr_register_content
);
231 if (mxc_nf_info
->flags
.one_kb_sram
&& (nand
->page_size
== 2048)) {
232 LOG_ERROR("NAND controller have only 1 kb SRAM, so "
233 "pagesize 2048 is incompatible with it");
235 LOG_DEBUG("MXC_NF : NAND controller can handle pagesize of 2048");
238 initialize_nf_controller(nand
);
241 retval
|= mxc_command(nand
, NAND_CMD_STATUS
);
242 retval
|= mxc_address(nand
, 0x00);
243 retval
|= do_data_output(nand
);
244 if (retval
!= ERROR_OK
) {
245 LOG_ERROR(get_status_register_err_msg
);
248 target_read_u16(target
, MXC_NF_MAIN_BUFFER0
, &nand_status_content
);
249 if (!(nand_status_content
& 0x0080)) {
250 LOG_INFO("NAND read-only");
251 mxc_nf_info
->flags
.nand_readonly
= 1;
253 mxc_nf_info
->flags
.nand_readonly
= 0;
258 static int mxc_read_data(struct nand_device
*nand
, void *data
)
260 int validate_target_result
;
261 int try_data_output_from_nand_chip
;
263 * validate target state
265 validate_target_result
= validate_target_state(nand
);
266 if (validate_target_result
!= ERROR_OK
)
267 return validate_target_result
;
270 * get data from nand chip
272 try_data_output_from_nand_chip
= do_data_output(nand
);
273 if (try_data_output_from_nand_chip
!= ERROR_OK
) {
274 LOG_ERROR("mxc_read_data : read data failed : '%x'",
275 try_data_output_from_nand_chip
);
276 return try_data_output_from_nand_chip
;
279 if (nand
->bus_width
== 16)
280 get_next_halfword_from_sram_buffer(nand
, data
);
282 get_next_byte_from_sram_buffer(nand
, data
);
287 static int mxc_write_data(struct nand_device
*nand
, uint16_t data
)
289 LOG_ERROR("write_data() not implemented");
290 return ERROR_NAND_OPERATION_FAILED
;
293 static int mxc_reset(struct nand_device
*nand
)
296 * validate target state
298 int validate_target_result
;
299 validate_target_result
= validate_target_state(nand
);
300 if (validate_target_result
!= ERROR_OK
)
301 return validate_target_result
;
302 initialize_nf_controller(nand
);
306 static int mxc_command(struct nand_device
*nand
, uint8_t command
)
308 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
309 struct target
*target
= nand
->target
;
310 int validate_target_result
;
313 * validate target state
315 validate_target_result
= validate_target_state(nand
);
316 if (validate_target_result
!= ERROR_OK
)
317 return validate_target_result
;
320 case NAND_CMD_READOOB
:
321 command
= NAND_CMD_READ0
;
322 /* set read point for data_read() and read_block_data() to
323 * spare area in SRAM buffer
325 in_sram_address
= MXC_NF_SPARE_BUFFER0
;
328 command
= NAND_CMD_READ0
;
330 * offset == one half of page size
332 in_sram_address
= MXC_NF_MAIN_BUFFER0
+ (nand
->page_size
>> 1);
335 in_sram_address
= MXC_NF_MAIN_BUFFER0
;
339 target_write_u16(target
, MXC_NF_FCMD
, command
);
341 * start command input operation (set MXC_NF_BIT_OP_DONE==0)
343 target_write_u16(target
, MXC_NF_CFG2
, MXC_NF_BIT_OP_FCI
);
344 poll_result
= poll_for_complete_op(nand
, "command");
345 if (poll_result
!= ERROR_OK
)
348 * reset cursor to begin of the buffer
350 sign_of_sequental_byte_read
= 0;
351 /* Handle special read command and adjust NF_CFG2(FDO) */
353 case NAND_CMD_READID
:
354 mxc_nf_info
->optype
= MXC_NF_DATAOUT_NANDID
;
355 mxc_nf_info
->fin
= MXC_NF_FIN_DATAOUT
;
357 case NAND_CMD_STATUS
:
358 mxc_nf_info
->optype
= MXC_NF_DATAOUT_NANDSTATUS
;
359 mxc_nf_info
->fin
= MXC_NF_FIN_DATAOUT
;
360 target_write_u16 (target
, MXC_NF_BUFADDR
, 0);
364 mxc_nf_info
->fin
= MXC_NF_FIN_DATAOUT
;
365 mxc_nf_info
->optype
= MXC_NF_DATAOUT_PAGE
;
368 /* Ohter command use the default 'One page data out' FDO */
369 mxc_nf_info
->optype
= MXC_NF_DATAOUT_PAGE
;
375 static int mxc_address(struct nand_device
*nand
, uint8_t address
)
377 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
378 struct target
*target
= nand
->target
;
379 int validate_target_result
;
382 * validate target state
384 validate_target_result
= validate_target_state(nand
);
385 if (validate_target_result
!= ERROR_OK
)
386 return validate_target_result
;
388 target_write_u16(target
, MXC_NF_FADDR
, address
);
390 * start address input operation (set MXC_NF_BIT_OP_DONE==0)
392 target_write_u16(target
, MXC_NF_CFG2
, MXC_NF_BIT_OP_FAI
);
393 poll_result
= poll_for_complete_op(nand
, "address");
394 if (poll_result
!= ERROR_OK
)
400 static int mxc_nand_ready(struct nand_device
*nand
, int tout
)
402 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
403 struct target
*target
= nand
->target
;
404 uint16_t poll_complete_status
;
405 int validate_target_result
;
408 * validate target state
410 validate_target_result
= validate_target_state(nand
);
411 if (validate_target_result
!= ERROR_OK
)
412 return validate_target_result
;
415 target_read_u16(target
, MXC_NF_CFG2
, &poll_complete_status
);
416 if (poll_complete_status
& MXC_NF_BIT_OP_DONE
)
425 static int mxc_write_page(struct nand_device
*nand
, uint32_t page
,
426 uint8_t *data
, uint32_t data_size
,
427 uint8_t *oob
, uint32_t oob_size
)
429 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
430 struct target
*target
= nand
->target
;
432 uint16_t nand_status_content
;
433 uint16_t swap1
, swap2
, new_swap1
;
436 LOG_ERROR(data_block_size_err_msg
, data_size
);
437 return ERROR_NAND_OPERATION_FAILED
;
440 LOG_ERROR(data_block_size_err_msg
, oob_size
);
441 return ERROR_NAND_OPERATION_FAILED
;
444 LOG_ERROR("nothing to program");
445 return ERROR_NAND_OPERATION_FAILED
;
448 * validate target state
450 retval
= validate_target_state(nand
);
451 if (retval
!= ERROR_OK
)
454 in_sram_address
= MXC_NF_MAIN_BUFFER0
;
455 sign_of_sequental_byte_read
= 0;
457 retval
|= mxc_command(nand
, NAND_CMD_SEQIN
);
458 retval
|= mxc_address(nand
, 0); /* col */
459 retval
|= mxc_address(nand
, 0); /* col */
460 retval
|= mxc_address(nand
, page
& 0xff); /* page address */
461 retval
|= mxc_address(nand
, (page
>> 8) & 0xff); /* page address */
462 retval
|= mxc_address(nand
, (page
>> 16) & 0xff); /* page address */
464 target_write_buffer(target
, MXC_NF_MAIN_BUFFER0
, data_size
, data
);
466 if (mxc_nf_info
->flags
.hw_ecc_enabled
) {
468 * part of spare block will be overrided by hardware
471 LOG_DEBUG("part of spare block will be overrided "
472 "by hardware ECC generator");
474 target_write_buffer(target
, MXC_NF_SPARE_BUFFER0
, oob_size
, oob
);
477 if (nand
->page_size
> 512 && mxc_nf_info
->flags
.biswap_enabled
) {
478 /* BI-swap - work-around of i.MX NFC for NAND device with page == 2kb*/
479 target_read_u16(target
, MXC_NF_MAIN_BUFFER3
+ 464, &swap1
);
481 LOG_ERROR("Due to NFC Bug, oob is not correctly implemented in mxc driver");
482 return ERROR_NAND_OPERATION_FAILED
;
484 swap2
= 0xffff; /* Spare buffer unused forced to 0xffff */
485 new_swap1
= (swap1
& 0xFF00) | (swap2
>> 8);
486 swap2
= (swap1
<< 8) | (swap2
& 0xFF);
487 target_write_u16(target
, MXC_NF_MAIN_BUFFER3
+ 464, new_swap1
);
488 target_write_u16(target
, MXC_NF_SPARE_BUFFER3
+ 4, swap2
);
492 * start data input operation (set MXC_NF_BIT_OP_DONE==0)
494 target_write_u16(target
, MXC_NF_BUFADDR
, 0);
495 target_write_u16(target
, MXC_NF_CFG2
, MXC_NF_BIT_OP_FDI
);
496 poll_result
= poll_for_complete_op(nand
, "data input");
497 if (poll_result
!= ERROR_OK
)
500 target_write_u16(target
, MXC_NF_BUFADDR
, 1);
501 target_write_u16(target
, MXC_NF_CFG2
, MXC_NF_BIT_OP_FDI
);
502 poll_result
= poll_for_complete_op(nand
, "data input");
503 if (poll_result
!= ERROR_OK
)
506 target_write_u16(target
, MXC_NF_BUFADDR
, 2);
507 target_write_u16(target
, MXC_NF_CFG2
, MXC_NF_BIT_OP_FDI
);
508 poll_result
= poll_for_complete_op(nand
, "data input");
509 if (poll_result
!= ERROR_OK
)
512 target_write_u16(target
, MXC_NF_BUFADDR
, 3);
513 target_write_u16(target
, MXC_NF_CFG2
, MXC_NF_BIT_OP_FDI
);
514 poll_result
= poll_for_complete_op(nand
, "data input");
515 if (poll_result
!= ERROR_OK
)
518 retval
|= mxc_command(nand
, NAND_CMD_PAGEPROG
);
519 if (retval
!= ERROR_OK
)
523 * check status register
526 retval
|= mxc_command(nand
, NAND_CMD_STATUS
);
527 target_write_u16 (target
, MXC_NF_BUFADDR
, 0);
528 mxc_nf_info
->optype
= MXC_NF_DATAOUT_NANDSTATUS
;
529 mxc_nf_info
->fin
= MXC_NF_FIN_DATAOUT
;
530 retval
|= do_data_output(nand
);
531 if (retval
!= ERROR_OK
) {
532 LOG_ERROR(get_status_register_err_msg
);
535 target_read_u16(target
, MXC_NF_MAIN_BUFFER0
, &nand_status_content
);
536 if (nand_status_content
& 0x0001) {
538 * page not correctly written
540 return ERROR_NAND_OPERATION_FAILED
;
542 #ifdef _MXC_PRINT_STAT
543 LOG_INFO("%d bytes newly written", data_size
);
548 static int mxc_read_page(struct nand_device
*nand
, uint32_t page
,
549 uint8_t *data
, uint32_t data_size
,
550 uint8_t *oob
, uint32_t oob_size
)
552 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
553 struct target
*target
= nand
->target
;
555 uint16_t swap1
, swap2
, new_swap1
;
558 LOG_ERROR(data_block_size_err_msg
, data_size
);
559 return ERROR_NAND_OPERATION_FAILED
;
562 LOG_ERROR(data_block_size_err_msg
, oob_size
);
563 return ERROR_NAND_OPERATION_FAILED
;
567 * validate target state
569 retval
= validate_target_state(nand
);
570 if (retval
!= ERROR_OK
) {
573 /* Reset address_cycles before mxc_command ?? */
574 retval
= mxc_command(nand
, NAND_CMD_READ0
);
575 if (retval
!= ERROR_OK
) return retval
;
576 retval
= mxc_address(nand
, 0); /* col */
577 if (retval
!= ERROR_OK
) return retval
;
578 retval
= mxc_address(nand
, 0); /* col */
579 if (retval
!= ERROR_OK
) return retval
;
580 retval
= mxc_address(nand
, page
& 0xff); /* page address */
581 if (retval
!= ERROR_OK
) return retval
;
582 retval
= mxc_address(nand
, (page
>> 8) & 0xff); /* page address */
583 if (retval
!= ERROR_OK
) return retval
;
584 retval
= mxc_address(nand
, (page
>> 16) & 0xff); /* page address */
585 if (retval
!= ERROR_OK
) return retval
;
586 retval
= mxc_command(nand
, NAND_CMD_READSTART
);
587 if (retval
!= ERROR_OK
) return retval
;
589 target_write_u16(target
, MXC_NF_BUFADDR
, 0);
590 mxc_nf_info
->fin
= MXC_NF_FIN_DATAOUT
;
591 retval
= do_data_output(nand
);
592 if (retval
!= ERROR_OK
) {
593 LOG_ERROR("MXC_NF : Error reading page 0");
596 /* Test nand page size to know how much MAIN_BUFFER must be written */
597 target_write_u16(target
, MXC_NF_BUFADDR
, 1);
598 mxc_nf_info
->fin
= MXC_NF_FIN_DATAOUT
;
599 retval
= do_data_output(nand
);
600 if (retval
!= ERROR_OK
) {
601 LOG_ERROR("MXC_NF : Error reading page 1");
604 target_write_u16(target
, MXC_NF_BUFADDR
, 2);
605 mxc_nf_info
->fin
= MXC_NF_FIN_DATAOUT
;
606 retval
= do_data_output(nand
);
607 if (retval
!= ERROR_OK
) {
608 LOG_ERROR("MXC_NF : Error reading page 2");
611 target_write_u16(target
, MXC_NF_BUFADDR
, 3);
612 mxc_nf_info
->fin
= MXC_NF_FIN_DATAOUT
;
613 retval
= do_data_output(nand
);
614 if (retval
!= ERROR_OK
) {
615 LOG_ERROR("MXC_NF : Error reading page 3");
619 if (nand
->page_size
> 512 && mxc_nf_info
->flags
.biswap_enabled
) {
620 /* BI-swap - work-around of mxc NFC for NAND device with page == 2k */
621 target_read_u16(target
, MXC_NF_MAIN_BUFFER3
+ 464, &swap1
);
622 target_read_u16(target
, MXC_NF_SPARE_BUFFER3
+ 4, &swap2
);
623 new_swap1
= (swap1
& 0xFF00) | (swap2
>> 8);
624 swap2
= (swap1
<< 8) | (swap2
& 0xFF);
625 target_write_u16(target
, MXC_NF_MAIN_BUFFER3
+ 464, new_swap1
);
626 target_write_u16(target
, MXC_NF_SPARE_BUFFER3
+ 4, swap2
);
630 target_read_buffer(target
, MXC_NF_MAIN_BUFFER0
, data_size
, data
);
632 target_read_buffer(target
, MXC_NF_SPARE_BUFFER0
, oob_size
, oob
);
634 #ifdef _MXC_PRINT_STAT
636 /* When Operation Status is read (when page is erased),
637 * this function is used but data_size is null.
639 LOG_INFO("%d bytes newly read", data_size
);
645 static int initialize_nf_controller(struct nand_device
*nand
)
647 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
648 struct target
*target
= nand
->target
;
652 * resets NAND flash controller in zero time ? I dont know.
654 target_write_u16(target
, MXC_NF_CFG1
, MXC_NF_BIT_RESET_EN
);
655 work_mode
= MXC_NF_BIT_INT_DIS
; /* disable interrupt */
656 if (target
->endianness
== TARGET_BIG_ENDIAN
) {
657 LOG_DEBUG("MXC_NF : work in Big Endian mode");
658 work_mode
|= MXC_NF_BIT_BE_EN
;
660 LOG_DEBUG("MXC_NF : work in Little Endian mode");
662 if (mxc_nf_info
->flags
.hw_ecc_enabled
) {
663 LOG_DEBUG("MXC_NF : work with ECC mode");
664 work_mode
|= MXC_NF_BIT_ECC_EN
;
666 LOG_DEBUG("MXC_NF : work without ECC mode");
668 target_write_u16(target
, MXC_NF_CFG1
, work_mode
);
670 * unlock SRAM buffer for write; 2 mean "Unlock", other values means "Lock"
672 target_write_u16(target
, MXC_NF_BUFCFG
, 2);
673 target_read_u16(target
, MXC_NF_FWP
, &temp
);
674 if ((temp
& 0x0007) == 1) {
675 LOG_ERROR("NAND flash is tight-locked, reset needed");
680 * unlock NAND flash for write
682 target_write_u16(target
, MXC_NF_FWP
, 4);
683 target_write_u16(target
, MXC_NF_LOCKSTART
, 0x0000);
684 target_write_u16(target
, MXC_NF_LOCKEND
, 0xFFFF);
686 * 0x0000 means that first SRAM buffer @0xD800_0000 will be used
688 target_write_u16(target
, MXC_NF_BUFADDR
, 0x0000);
690 * address of SRAM buffer
692 in_sram_address
= MXC_NF_MAIN_BUFFER0
;
693 sign_of_sequental_byte_read
= 0;
697 static int get_next_byte_from_sram_buffer(struct nand_device
*nand
, uint8_t *value
)
699 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
700 struct target
*target
= nand
->target
;
701 static uint8_t even_byte
= 0;
706 if (sign_of_sequental_byte_read
== 0)
709 if (in_sram_address
> MXC_NF_LAST_BUFFER_ADDR
) {
710 LOG_ERROR(sram_buffer_bounds_err_msg
, in_sram_address
);
712 sign_of_sequental_byte_read
= 0;
714 return ERROR_NAND_OPERATION_FAILED
;
716 target_read_u16(target
, in_sram_address
, &temp
);
720 in_sram_address
+= 2;
722 *value
= temp
& 0xff;
726 sign_of_sequental_byte_read
= 1;
730 static int get_next_halfword_from_sram_buffer(struct nand_device
*nand
, uint16_t *value
)
732 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
733 struct target
*target
= nand
->target
;
735 if (in_sram_address
> MXC_NF_LAST_BUFFER_ADDR
) {
736 LOG_ERROR(sram_buffer_bounds_err_msg
, in_sram_address
);
738 return ERROR_NAND_OPERATION_FAILED
;
740 target_read_u16(target
, in_sram_address
, value
);
741 in_sram_address
+= 2;
746 static int poll_for_complete_op(struct nand_device
*nand
, const char *text
)
748 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
749 struct target
*target
= nand
->target
;
750 uint16_t poll_complete_status
;
752 for (int poll_cycle_count
= 0; poll_cycle_count
< 100; poll_cycle_count
++) {
753 target_read_u16(target
, MXC_NF_CFG2
, &poll_complete_status
);
754 if (poll_complete_status
& MXC_NF_BIT_OP_DONE
)
759 if (!(poll_complete_status
& MXC_NF_BIT_OP_DONE
)) {
760 LOG_ERROR("%s sending timeout", text
);
761 return ERROR_NAND_OPERATION_FAILED
;
766 static int validate_target_state(struct nand_device
*nand
)
768 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
769 struct target
*target
= nand
->target
;
771 if (target
->state
!= TARGET_HALTED
) {
772 LOG_ERROR(target_not_halted_err_msg
);
773 return ERROR_NAND_OPERATION_FAILED
;
776 if (mxc_nf_info
->flags
.target_little_endian
!=
777 (target
->endianness
== TARGET_LITTLE_ENDIAN
)) {
779 * endianness changed after NAND controller probed
781 return ERROR_NAND_OPERATION_FAILED
;
786 static int do_data_output(struct nand_device
*nand
)
788 struct mxc_nf_controller
*mxc_nf_info
= nand
->controller_priv
;
789 struct target
*target
= nand
->target
;
792 switch (mxc_nf_info
->fin
) {
793 case MXC_NF_FIN_DATAOUT
:
795 * start data output operation (set MXC_NF_BIT_OP_DONE==0)
797 target_write_u16(target
, MXC_NF_CFG2
, MXC_NF_BIT_DATAOUT_TYPE(mxc_nf_info
->optype
));
798 poll_result
= poll_for_complete_op(nand
, "data output");
799 if (poll_result
!= ERROR_OK
)
802 mxc_nf_info
->fin
= MXC_NF_FIN_NONE
;
806 if ((mxc_nf_info
->optype
== MXC_NF_DATAOUT_PAGE
) &&
807 mxc_nf_info
->flags
.hw_ecc_enabled
) {
808 target_read_u16(target
, MXC_NF_ECCSTATUS
, &ecc_status
);
809 switch (ecc_status
& 0x000c) {
811 LOG_INFO("main area readed with 1 (correctable) error");
814 LOG_INFO("main area readed with more than 1 (incorrectable) error");
815 return ERROR_NAND_OPERATION_FAILED
;
818 switch (ecc_status
& 0x0003) {
820 LOG_INFO("spare area readed with 1 (correctable) error");
823 LOG_INFO("main area readed with more than 1 (incorrectable) error");
824 return ERROR_NAND_OPERATION_FAILED
;
829 case MXC_NF_FIN_NONE
:
835 struct nand_flash_controller mxc_nand_flash_controller
= {
837 .nand_device_command
= &mxc_nand_device_command
,
838 .commands
= mxc_nand_command_handler
,
841 .command
= &mxc_command
,
842 .address
= &mxc_address
,
843 .write_data
= &mxc_write_data
,
844 .read_data
= &mxc_read_data
,
845 .write_page
= &mxc_write_page
,
846 .read_page
= &mxc_read_page
,
847 .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)