2 /***************************************************************************
3 * Copyright (C) 2009 by Alexei Babich *
4 * Rezonans plc., Chelyabinsk, Russia *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
24 * Freescale iMX3* OpenOCD NAND Flash controller support.
26 * Many thanks to Ben Dooks for writing s3c24xx driver.
30 driver tested with STMicro NAND512W3A @imx31
31 tested "nand probe #", "nand erase # 0 #", "nand dump # file 0 #", "nand write # file 0"
32 get_next_halfword_from_sram_buffer() not tested
40 static const char target_not_halted_err_msg[] =
41 "target must be halted to use mx3 NAND flash controller";
42 static const char data_block_size_err_msg[] =
43 "minimal granularity is one half-word, %" PRId32 " is incorrect";
44 static const char sram_buffer_bounds_err_msg[] =
45 "trying to access out of SRAM buffer bound (addr=0x%" PRIx32 ")";
46 static const char get_status_register_err_msg[] = "can't get NAND status";
47 static uint32_t in_sram_address;
48 unsigned char sign_of_sequental_byte_read;
50 static int test_iomux_settings (struct target * target, uint32_t value,
51 uint32_t mask, const char *text);
52 static int initialize_nf_controller (struct nand_device *nand);
53 static int get_next_byte_from_sram_buffer (struct target * target, uint8_t * value);
54 static int get_next_halfword_from_sram_buffer (struct target * target,
56 static int poll_for_complete_op (struct target * target, const char *text);
57 static int validate_target_state (struct nand_device *nand);
58 static int do_data_output (struct nand_device *nand);
60 static int imx31_command (struct nand_device *nand, uint8_t command);
61 static int imx31_address (struct nand_device *nand, uint8_t address);
62 static int imx31_controller_ready (struct nand_device *nand, int tout);
64 NAND_DEVICE_COMMAND_HANDLER(imx31_nand_device_command)
66 struct mx3_nf_controller *mx3_nf_info;
67 mx3_nf_info = malloc (sizeof (struct mx3_nf_controller));
68 if (mx3_nf_info == NULL)
70 LOG_ERROR ("no memory for nand controller");
74 nand->controller_priv = mx3_nf_info;
76 mx3_nf_info->target = get_target (CMD_ARGV[1]);
77 if (mx3_nf_info->target == NULL)
79 LOG_ERROR ("target '%s' not defined", CMD_ARGV[1]);
84 LOG_ERROR ("use \"nand device imx31 target noecc|hwecc\"");
88 * check hwecc requirements
92 hwecc_needed = strcmp (CMD_ARGV[2], "hwecc");
93 if (hwecc_needed == 0)
95 mx3_nf_info->flags.hw_ecc_enabled = 1;
99 mx3_nf_info->flags.hw_ecc_enabled = 0;
103 mx3_nf_info->optype = MX3_NF_DATAOUT_PAGE;
104 mx3_nf_info->fin = MX3_NF_FIN_NONE;
105 mx3_nf_info->flags.target_little_endian =
106 (mx3_nf_info->target->endianness == TARGET_LITTLE_ENDIAN);
108 * testing host endianess
112 if (*(char *) &x == 1)
114 mx3_nf_info->flags.host_little_endian = 1;
118 mx3_nf_info->flags.host_little_endian = 0;
124 static int imx31_init (struct nand_device *nand)
126 struct mx3_nf_controller *mx3_nf_info = nand->controller_priv;
127 struct target *target = mx3_nf_info->target;
131 * validate target state
133 int validate_target_result;
134 validate_target_result = validate_target_state(nand);
135 if (validate_target_result != ERROR_OK)
137 return validate_target_result;
142 uint16_t buffsize_register_content;
143 target_read_u16 (target, MX3_NF_BUFSIZ, &buffsize_register_content);
144 mx3_nf_info->flags.one_kb_sram = !(buffsize_register_content & 0x000f);
148 uint32_t pcsr_register_content;
149 target_read_u32 (target, MX3_PCSR, &pcsr_register_content);
150 if (!nand->bus_width)
153 (pcsr_register_content & 0x80000000) ? 16 : 8;
157 pcsr_register_content |=
158 ((nand->bus_width == 16) ? 0x80000000 : 0x00000000);
159 target_write_u32 (target, MX3_PCSR, pcsr_register_content);
162 if (!nand->page_size)
165 (pcsr_register_content & 0x40000000) ? 2048 : 512;
169 pcsr_register_content |=
170 ((nand->page_size == 2048) ? 0x40000000 : 0x00000000);
171 target_write_u32 (target, MX3_PCSR, pcsr_register_content);
173 if (mx3_nf_info->flags.one_kb_sram && (nand->page_size == 2048))
176 ("NAND controller have only 1 kb SRAM, so pagesize 2048 is incompatible with it");
181 uint32_t cgr_register_content;
182 target_read_u32 (target, MX3_CCM_CGR2, &cgr_register_content);
183 if (!(cgr_register_content & 0x00000300))
185 LOG_ERROR ("clock gating to EMI disabled");
191 uint32_t gpr_register_content;
192 target_read_u32 (target, MX3_GPR, &gpr_register_content);
193 if (gpr_register_content & 0x00000060)
195 LOG_ERROR ("pins mode overrided by GPR");
202 * testing IOMUX settings; must be in "functional-mode output and
203 * functional-mode input" mode
206 test_iomux = ERROR_OK;
208 test_iomux_settings (target, 0x43fac0c0, 0x7f7f7f00, "d0,d1,d2");
210 test_iomux_settings (target, 0x43fac0c4, 0x7f7f7f7f, "d3,d4,d5,d6");
212 test_iomux_settings (target, 0x43fac0c8, 0x0000007f, "d7");
213 if (nand->bus_width == 16)
216 test_iomux_settings (target, 0x43fac0c8, 0x7f7f7f00,
219 test_iomux_settings (target, 0x43fac0cc, 0x7f7f7f7f,
222 test_iomux_settings (target, 0x43fac0d0, 0x0000007f, "d15");
225 test_iomux_settings (target, 0x43fac0d0, 0x7f7f7f00,
228 test_iomux_settings (target, 0x43fac0d4, 0x7f7f7f7f,
229 "nfwe,nfre,nfale,nfcle");
230 if (test_iomux != ERROR_OK)
236 initialize_nf_controller (nand);
240 uint16_t nand_status_content;
242 retval |= imx31_command (nand, NAND_CMD_STATUS);
243 retval |= imx31_address (nand, 0x00);
244 retval |= do_data_output (nand);
245 if (retval != ERROR_OK)
247 LOG_ERROR (get_status_register_err_msg);
250 target_read_u16 (target, MX3_NF_MAIN_BUFFER0, &nand_status_content);
251 if (!(nand_status_content & 0x0080))
254 * is host-big-endian correctly ??
256 LOG_INFO ("NAND read-only");
257 mx3_nf_info->flags.nand_readonly = 1;
261 mx3_nf_info->flags.nand_readonly = 0;
267 static int imx31_read_data (struct nand_device *nand, void *data)
269 struct mx3_nf_controller *mx3_nf_info = nand->controller_priv;
270 struct target *target = mx3_nf_info->target;
273 * validate target state
275 int validate_target_result;
276 validate_target_result = validate_target_state (nand);
277 if (validate_target_result != ERROR_OK)
279 return validate_target_result;
285 * get data from nand chip
287 int try_data_output_from_nand_chip;
288 try_data_output_from_nand_chip = do_data_output (nand);
289 if (try_data_output_from_nand_chip != ERROR_OK)
291 return try_data_output_from_nand_chip;
295 if (nand->bus_width == 16)
297 get_next_halfword_from_sram_buffer (target, data);
301 get_next_byte_from_sram_buffer (target, data);
307 static int imx31_write_data (struct nand_device *nand, uint16_t data)
309 LOG_ERROR ("write_data() not implemented");
310 return ERROR_NAND_OPERATION_FAILED;
313 static int imx31_nand_ready (struct nand_device *nand, int timeout)
315 return imx31_controller_ready (nand, timeout);
318 static int imx31_reset (struct nand_device *nand)
321 * validate target state
323 int validate_target_result;
324 validate_target_result = validate_target_state (nand);
325 if (validate_target_result != ERROR_OK)
327 return validate_target_result;
329 initialize_nf_controller (nand);
333 static int imx31_command (struct nand_device *nand, uint8_t command)
335 struct mx3_nf_controller *mx3_nf_info = nand->controller_priv;
336 struct target *target = mx3_nf_info->target;
339 * validate target state
341 int validate_target_result;
342 validate_target_result = validate_target_state (nand);
343 if (validate_target_result != ERROR_OK)
345 return validate_target_result;
351 case NAND_CMD_READOOB:
352 command = NAND_CMD_READ0;
353 in_sram_address = MX3_NF_SPARE_BUFFER0; /* set read point for
355 * read_block_data() to
360 command = NAND_CMD_READ0;
362 * offset == one half of page size
365 MX3_NF_MAIN_BUFFER0 + (nand->page_size >> 1);
367 in_sram_address = MX3_NF_MAIN_BUFFER0;
370 target_write_u16 (target, MX3_NF_FCMD, command);
372 * start command input operation (set MX3_NF_BIT_OP_DONE==0)
374 target_write_u16 (target, MX3_NF_CFG2, MX3_NF_BIT_OP_FCI);
377 poll_result = poll_for_complete_op (target, "command");
378 if (poll_result != ERROR_OK)
384 * reset cursor to begin of the buffer
386 sign_of_sequental_byte_read = 0;
389 case NAND_CMD_READID:
390 mx3_nf_info->optype = MX3_NF_DATAOUT_NANDID;
391 mx3_nf_info->fin = MX3_NF_FIN_DATAOUT;
393 case NAND_CMD_STATUS:
394 mx3_nf_info->optype = MX3_NF_DATAOUT_NANDSTATUS;
395 mx3_nf_info->fin = MX3_NF_FIN_DATAOUT;
398 mx3_nf_info->fin = MX3_NF_FIN_DATAOUT;
399 mx3_nf_info->optype = MX3_NF_DATAOUT_PAGE;
402 mx3_nf_info->optype = MX3_NF_DATAOUT_PAGE;
407 static int imx31_address (struct nand_device *nand, uint8_t address)
409 struct mx3_nf_controller *mx3_nf_info = nand->controller_priv;
410 struct target *target = mx3_nf_info->target;
413 * validate target state
415 int validate_target_result;
416 validate_target_result = validate_target_state (nand);
417 if (validate_target_result != ERROR_OK)
419 return validate_target_result;
423 target_write_u16 (target, MX3_NF_FADDR, address);
425 * start address input operation (set MX3_NF_BIT_OP_DONE==0)
427 target_write_u16 (target, MX3_NF_CFG2, MX3_NF_BIT_OP_FAI);
430 poll_result = poll_for_complete_op (target, "address");
431 if (poll_result != ERROR_OK)
439 static int imx31_controller_ready (struct nand_device *nand, int tout)
441 uint16_t poll_complete_status;
442 struct mx3_nf_controller *mx3_nf_info = nand->controller_priv;
443 struct target *target = mx3_nf_info->target;
447 * validate target state
449 int validate_target_result;
450 validate_target_result = validate_target_state (nand);
451 if (validate_target_result != ERROR_OK)
453 return validate_target_result;
459 target_read_u16 (target, MX3_NF_CFG2, &poll_complete_status);
460 if (poll_complete_status & MX3_NF_BIT_OP_DONE)
470 static int imx31_write_page (struct nand_device *nand, uint32_t page,
471 uint8_t * data, uint32_t data_size, uint8_t * oob,
474 struct mx3_nf_controller *mx3_nf_info = nand->controller_priv;
475 struct target *target = mx3_nf_info->target;
479 LOG_ERROR (data_block_size_err_msg, data_size);
480 return ERROR_NAND_OPERATION_FAILED;
484 LOG_ERROR (data_block_size_err_msg, oob_size);
485 return ERROR_NAND_OPERATION_FAILED;
489 LOG_ERROR ("nothing to program");
490 return ERROR_NAND_OPERATION_FAILED;
494 * validate target state
497 retval = validate_target_state (nand);
498 if (retval != ERROR_OK)
504 int retval = ERROR_OK;
505 retval |= imx31_command(nand, NAND_CMD_SEQIN);
506 retval |= imx31_address(nand, 0x00);
507 retval |= imx31_address(nand, page & 0xff);
508 retval |= imx31_address(nand, (page >> 8) & 0xff);
509 if (nand->address_cycles >= 4)
511 retval |= imx31_address (nand, (page >> 16) & 0xff);
512 if (nand->address_cycles >= 5)
514 retval |= imx31_address (nand, (page >> 24) & 0xff);
517 target_write_buffer (target, MX3_NF_MAIN_BUFFER0, data_size, data);
520 if (mx3_nf_info->flags.hw_ecc_enabled)
523 * part of spare block will be overrided by hardware
527 ("part of spare block will be overrided by hardware ECC generator");
529 target_write_buffer (target, MX3_NF_SPARE_BUFFER0, oob_size,
533 * start data input operation (set MX3_NF_BIT_OP_DONE==0)
535 target_write_u16 (target, MX3_NF_CFG2, MX3_NF_BIT_OP_FDI);
538 poll_result = poll_for_complete_op (target, "data input");
539 if (poll_result != ERROR_OK)
544 retval |= imx31_command (nand, NAND_CMD_PAGEPROG);
545 if (retval != ERROR_OK)
551 * check status register
554 uint16_t nand_status_content;
556 retval |= imx31_command(nand, NAND_CMD_STATUS);
557 retval |= imx31_address(nand, 0x00);
558 retval |= do_data_output(nand);
559 if (retval != ERROR_OK)
561 LOG_ERROR (get_status_register_err_msg);
564 target_read_u16 (target, MX3_NF_MAIN_BUFFER0, &nand_status_content);
565 if (nand_status_content & 0x0001)
568 * is host-big-endian correctly ??
570 return ERROR_NAND_OPERATION_FAILED;
577 static int imx31_read_page (struct nand_device *nand, uint32_t page,
578 uint8_t * data, uint32_t data_size, uint8_t * oob,
581 struct mx3_nf_controller *mx3_nf_info = nand->controller_priv;
582 struct target *target = mx3_nf_info->target;
586 LOG_ERROR (data_block_size_err_msg, data_size);
587 return ERROR_NAND_OPERATION_FAILED;
591 LOG_ERROR (data_block_size_err_msg, oob_size);
592 return ERROR_NAND_OPERATION_FAILED;
597 * validate target state
600 retval = validate_target_state(nand);
601 if (retval != ERROR_OK)
607 int retval = ERROR_OK;
608 retval |= imx31_command(nand, NAND_CMD_READ0);
609 retval |= imx31_address(nand, 0x00);
610 retval |= imx31_address(nand, page & 0xff);
611 retval |= imx31_address(nand, (page >> 8) & 0xff);
612 if (nand->address_cycles >= 4)
614 retval |= imx31_address(nand, (page >> 16) & 0xff);
615 if (nand->address_cycles >= 5)
617 retval |= imx31_address(nand, (page >> 24) & 0xff);
618 retval |= imx31_command(nand, NAND_CMD_READSTART);
621 retval |= do_data_output (nand);
622 if (retval != ERROR_OK)
629 target_read_buffer (target, MX3_NF_MAIN_BUFFER0, data_size,
634 target_read_buffer (target, MX3_NF_SPARE_BUFFER0, oob_size,
641 static int test_iomux_settings (struct target * target, uint32_t address,
642 uint32_t mask, const char *text)
644 uint32_t register_content;
645 target_read_u32 (target, address, ®ister_content);
646 if ((register_content & mask) != (0x12121212 & mask))
648 LOG_ERROR ("IOMUX for {%s} is bad", text);
654 static int initialize_nf_controller (struct nand_device *nand)
656 struct mx3_nf_controller *mx3_nf_info = nand->controller_priv;
657 struct target *target = mx3_nf_info->target;
659 * resets NAND flash controller in zero time ? I dont know.
661 target_write_u16 (target, MX3_NF_CFG1, MX3_NF_BIT_RESET_EN);
664 work_mode = MX3_NF_BIT_INT_DIS; /* disable interrupt */
665 if (target->endianness == TARGET_BIG_ENDIAN)
667 work_mode |= MX3_NF_BIT_BE_EN;
669 if (mx3_nf_info->flags.hw_ecc_enabled)
671 work_mode |= MX3_NF_BIT_ECC_EN;
673 target_write_u16 (target, MX3_NF_CFG1, work_mode);
676 * unlock SRAM buffer for write; 2 mean "Unlock", other values means "Lock"
678 target_write_u16 (target, MX3_NF_BUFCFG, 2);
681 target_read_u16 (target, MX3_NF_FWP, &temp);
682 if ((temp & 0x0007) == 1)
684 LOG_ERROR ("NAND flash is tight-locked, reset needed");
690 * unlock NAND flash for write
692 target_write_u16 (target, MX3_NF_FWP, 4);
693 target_write_u16 (target, MX3_NF_LOCKSTART, 0x0000);
694 target_write_u16 (target, MX3_NF_LOCKEND, 0xFFFF);
696 * 0x0000 means that first SRAM buffer @0xB800_0000 will be used
698 target_write_u16 (target, MX3_NF_BUFADDR, 0x0000);
700 * address of SRAM buffer
702 in_sram_address = MX3_NF_MAIN_BUFFER0;
703 sign_of_sequental_byte_read = 0;
707 static int get_next_byte_from_sram_buffer (struct target * target, uint8_t * value)
709 static uint8_t even_byte = 0;
713 if (sign_of_sequental_byte_read == 0)
717 if (in_sram_address > MX3_NF_LAST_BUFFER_ADDR)
719 LOG_ERROR (sram_buffer_bounds_err_msg, in_sram_address);
721 sign_of_sequental_byte_read = 0;
723 return ERROR_NAND_OPERATION_FAILED;
728 target_read_u16 (target, in_sram_address, &temp);
733 in_sram_address += 2;
737 *value = temp & 0xff;
741 sign_of_sequental_byte_read = 1;
745 static int get_next_halfword_from_sram_buffer (struct target * target,
748 if (in_sram_address > MX3_NF_LAST_BUFFER_ADDR)
750 LOG_ERROR (sram_buffer_bounds_err_msg, in_sram_address);
752 return ERROR_NAND_OPERATION_FAILED;
756 target_read_u16 (target, in_sram_address, value);
757 in_sram_address += 2;
762 static int poll_for_complete_op (struct target * target, const char *text)
764 uint16_t poll_complete_status;
765 for (int poll_cycle_count = 0; poll_cycle_count < 100; poll_cycle_count++)
768 target_read_u16 (target, MX3_NF_CFG2, &poll_complete_status);
769 if (poll_complete_status & MX3_NF_BIT_OP_DONE)
774 if (!(poll_complete_status & MX3_NF_BIT_OP_DONE))
776 LOG_ERROR ("%s sending timeout", text);
777 return ERROR_NAND_OPERATION_FAILED;
782 static int validate_target_state (struct nand_device *nand)
784 struct mx3_nf_controller *mx3_nf_info = nand->controller_priv;
785 struct target *target = mx3_nf_info->target;
787 if (target->state != TARGET_HALTED)
789 LOG_ERROR (target_not_halted_err_msg);
790 return ERROR_NAND_OPERATION_FAILED;
793 if (mx3_nf_info->flags.target_little_endian !=
794 (target->endianness == TARGET_LITTLE_ENDIAN))
797 * endianness changed after NAND controller probed
799 return ERROR_NAND_OPERATION_FAILED;
804 static int do_data_output (struct nand_device *nand)
806 struct mx3_nf_controller *mx3_nf_info = nand->controller_priv;
807 struct target *target = mx3_nf_info->target;
808 switch (mx3_nf_info->fin)
810 case MX3_NF_FIN_DATAOUT:
812 * start data output operation (set MX3_NF_BIT_OP_DONE==0)
814 target_write_u16 (target, MX3_NF_CFG2,
815 MX3_NF_BIT_DATAOUT_TYPE (mx3_nf_info->
819 poll_result = poll_for_complete_op (target, "data output");
820 if (poll_result != ERROR_OK)
825 mx3_nf_info->fin = MX3_NF_FIN_NONE;
829 if ((mx3_nf_info->optype == MX3_NF_DATAOUT_PAGE)
830 && mx3_nf_info->flags.hw_ecc_enabled)
833 target_read_u16 (target, MX3_NF_ECCSTATUS, &ecc_status);
834 switch (ecc_status & 0x000c)
838 ("main area readed with 1 (correctable) error");
842 ("main area readed with more than 1 (incorrectable) error");
843 return ERROR_NAND_OPERATION_FAILED;
846 switch (ecc_status & 0x0003)
850 ("spare area readed with 1 (correctable) error");
854 ("main area readed with more than 1 (incorrectable) error");
855 return ERROR_NAND_OPERATION_FAILED;
860 case MX3_NF_FIN_NONE:
866 struct nand_flash_controller imx31_nand_flash_controller = {
868 .nand_device_command = &imx31_nand_device_command,
870 .reset = &imx31_reset,
871 .command = &imx31_command,
872 .address = &imx31_address,
873 .write_data = &imx31_write_data,
874 .read_data = &imx31_read_data,
875 .write_page = &imx31_write_page,
876 .read_page = &imx31_read_page,
877 .controller_ready = &imx31_controller_ready,
878 .nand_ready = &imx31_nand_ready,