2 * (C) Copyright 2004 Texas Instruments
3 * Jian Zhang <jzhang@ti.com>
5 * Samsung K9F5616Q0C NAND chip driver for an OMAP16xx board
7 * This file is based on the following u-boot file:
10 * See file CREDITS for list of people who contributed to this
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 #ifdef CFG_NAND_K9F5616
33 #define K9F5616_MFR 0xec
34 #define K9F5616_ID 0x45
38 #define ADDR_COLUMN_PAGE 3
42 static int nand_read_page(u_char *buf, ulong page_addr);
43 static int nand_read_oob(u_char * buf, ulong page_addr);
45 /* JFFS2 512-byte-page ECC layout */
46 static u_char ecc_pos[] = {0,1,2,3,6,7};
47 static u_char eccvalid_pos = 4;
49 /* NanD_Command: Send a flash command to the flash chip */
50 static int NanD_Command(unsigned char command)
52 NAND_CTL_SETCLE(NAND_ADDR);
53 WRITE_NAND_COMMAND(command, NAND_ADDR);
54 NAND_CTL_CLRCLE(NAND_ADDR);
56 if(command == NAND_CMD_RESET){
57 unsigned char ret_val;
58 NanD_Command(NAND_CMD_STATUS);
60 ret_val = READ_NAND(NAND_ADDR);/* wait till ready */
61 } while((ret_val & 0x40) != 0x40);
68 /* NanD_Address: Set the current address for the flash chip */
69 static int NanD_Address(int numbytes, unsigned long ofs)
73 NAND_CTL_SETALE(NAND_ADDR);
75 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE)
76 WRITE_NAND_ADDRESS(ofs, NAND_ADDR);
80 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE)
81 for (i = 0; i < 2; i++, ofs = ofs >> 8)
82 WRITE_NAND_ADDRESS(ofs, NAND_ADDR);
84 NAND_CTL_CLRALE(NAND_ADDR);
90 /* read chip mfr and id
91 * return 0 if they match board config
100 if (NanD_Command(NAND_CMD_RESET)) {
101 printf("Err: RESET\n");
106 if (NanD_Command(NAND_CMD_READID)) {
107 printf("Err: READID\n");
112 NanD_Address(ADDR_COLUMN, 0);
114 mfr = READ_NAND(NAND_ADDR);
115 id = READ_NAND(NAND_ADDR);
119 return (mfr != K9F5616_MFR || id != K9F5616_ID);
122 /* read a block data to buf
123 * return 1 if the block is bad or ECC error can't be corrected for any page
126 int nand_read_block(unsigned char *buf, ulong block_addr)
131 /* check bad block */
132 /* 0th and 5th words need be 0xffff */
133 if (nand_read_oob(oob_buf, block_addr) ||
134 // oob_buf[0] != 0xff || oob_buf[1] != 0xff ||
135 // oob_buf[10] != 0xff || oob_buf[11] != 0xff ){
137 printf("Skipped bad block at 0x%x\n", block_addr);
138 return 1; /* skip bad block */
141 /* read the block page by page*/
142 for (i=0; i<32; i++){
143 if (nand_read_page(buf+offset, block_addr + offset))
151 /* read a page with ECC */
152 static int nand_read_page(u_char *buf, ulong page_addr)
162 NanD_Command(NAND_CMD_READ0);
163 NanD_Address(ADDR_COLUMN_PAGE, page_addr>>1);
167 for (cntr = 0; cntr < 256; cntr++){
168 val = READ_NAND(NAND_ADDR);
174 for (cntr = 0; cntr < 8; cntr++){
175 val = READ_NAND(NAND_ADDR);
179 NAND_DISABLE_CE(); /* set pin high */
181 /* Pick the ECC bytes out of the oob data */
182 for (cntr = 0; cntr < 6; cntr++)
183 ecc_code[cntr] = oob_buf[ecc_pos[cntr]];
186 if ((oob_buf[eccvalid_pos] & 0x0f) != 0x0f) {
187 nand_calculate_ecc (buf, &ecc_calc[0]);
188 if (nand_correct_data (buf, &ecc_code[0], &ecc_calc[0]) == -1) {
189 printf ("ECC Failed, page 0x%08x\n", page_addr);
194 if ((oob_buf[eccvalid_pos] & 0xf0) != 0xf0) {
195 nand_calculate_ecc (buf + 256, &ecc_calc[0]);
196 if (nand_correct_data (buf + 256, &ecc_code[3], &ecc_calc[0]) == -1) {
197 printf ("ECC Failed, page 0x%08x\n", page_addr+0x100);
205 /* read from the 16 bytes of oob data that correspond to a 512 byte page.
207 static int nand_read_oob(u_char *buf, ulong page_addr)
212 NAND_ENABLE_CE(); /* set pin low */
213 NanD_Command(NAND_CMD_READOOB);
214 NanD_Address(ADDR_COLUMN_PAGE, page_addr>>1);
217 for (cntr = 0; cntr < 8; cntr++){
218 val = READ_NAND(NAND_ADDR);
224 NAND_DISABLE_CE(); /* set pin high */