3 * Platform independend driver for NDFC (NanD Flash Controller)
4 * integrated into EP440 cores
6 * (C) Copyright 2006-2007
7 * Stefan Roese, DENX Software Engineering, sr@denx.de.
9 * Based on original work by
13 * See file CREDITS for list of people who contributed to this
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 #if defined(CONFIG_CMD_NAND) && !defined(CFG_NAND_LEGACY) && \
35 (defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
36 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
37 defined(CONFIG_405EZ))
40 #include <linux/mtd/ndfc.h>
41 #include <linux/mtd/nand_ecc.h>
42 #include <asm/processor.h>
48 static void ndfc_hwcontrol(struct mtd_info *mtdinfo, int cmd)
69 static void ndfc_write_byte(struct mtd_info *mtdinfo, u_char byte)
71 struct nand_chip *this = mtdinfo->priv;
72 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
75 out_8((u8 *)(base + NDFC_CMD), byte);
77 out_8((u8 *)(base + NDFC_ALE), byte);
79 out_8((u8 *)(base + NDFC_DATA), byte);
82 static u_char ndfc_read_byte(struct mtd_info *mtdinfo)
84 struct nand_chip *this = mtdinfo->priv;
85 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
87 return (in_8((u8 *)(base + NDFC_DATA)));
90 static int ndfc_dev_ready(struct mtd_info *mtdinfo)
92 struct nand_chip *this = mtdinfo->priv;
93 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
95 while (!(in_be32((u32 *)(base + NDFC_STAT)) & NDFC_STAT_IS_READY))
101 static void ndfc_enable_hwecc(struct mtd_info *mtdinfo, int mode)
103 struct nand_chip *this = mtdinfo->priv;
104 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
107 ccr = in_be32((u32 *)(base + NDFC_CCR));
108 ccr |= NDFC_CCR_RESET_ECC;
109 out_be32((u32 *)(base + NDFC_CCR), ccr);
112 static int ndfc_calculate_ecc(struct mtd_info *mtdinfo,
113 const u_char *dat, u_char *ecc_code)
115 struct nand_chip *this = mtdinfo->priv;
116 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
120 ecc = in_be32((u32 *)(base + NDFC_ECC));
122 /* The NDFC uses Smart Media (SMC) bytes order
132 * Speedups for buffer read/write/verify
134 * NDFC allows 32bit read/write of data. So we can speed up the buffer
135 * functions. No further checking, as nand_base will always read/write
138 static void ndfc_read_buf(struct mtd_info *mtdinfo, uint8_t *buf, int len)
140 struct nand_chip *this = mtdinfo->priv;
141 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
142 uint32_t *p = (uint32_t *) buf;
144 for (;len > 0; len -= 4)
145 *p++ = in_be32((u32 *)(base + NDFC_DATA));
148 #ifndef CONFIG_NAND_SPL
150 * Don't use these speedup functions in NAND boot image, since the image
151 * has to fit into 4kByte.
153 static void ndfc_write_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
155 struct nand_chip *this = mtdinfo->priv;
156 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
157 uint32_t *p = (uint32_t *) buf;
159 for (; len > 0; len -= 4)
160 out_be32((u32 *)(base + NDFC_DATA), *p++);
163 static int ndfc_verify_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
165 struct nand_chip *this = mtdinfo->priv;
166 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
167 uint32_t *p = (uint32_t *) buf;
169 for (; len > 0; len -= 4)
170 if (*p++ != in_be32((u32 *)(base + NDFC_DATA)))
175 #endif /* #ifndef CONFIG_NAND_SPL */
177 void board_nand_select_device(struct nand_chip *nand, int chip)
180 * Don't use "chip" to address the NAND device,
181 * generate the cs from the address where it is encoded.
183 int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
184 ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc;
186 /* Set NandFlash Core Configuration Register */
188 out_be32((u32 *)(base + NDFC_CCR), 0x00000000 | (cs << 24));
191 int board_nand_init(struct nand_chip *nand)
193 int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
194 ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc;
196 nand->hwcontrol = ndfc_hwcontrol;
197 nand->read_byte = ndfc_read_byte;
198 nand->read_buf = ndfc_read_buf;
199 nand->write_byte = ndfc_write_byte;
200 nand->dev_ready = ndfc_dev_ready;
202 nand->eccmode = NAND_ECC_HW3_256;
203 nand->enable_hwecc = ndfc_enable_hwecc;
204 nand->calculate_ecc = ndfc_calculate_ecc;
205 nand->correct_data = nand_correct_data;
207 #ifndef CONFIG_NAND_SPL
208 nand->write_buf = ndfc_write_buf;
209 nand->verify_buf = ndfc_verify_buf;
212 * Setup EBC (CS0 only right now)
214 mtebc(EBC0_CFG, 0xb8400000);
216 mtebc(pb0cr, CFG_EBC_PB0CR);
217 mtebc(pb0ap, CFG_EBC_PB0AP);
221 * Select required NAND chip in NDFC
223 board_nand_select_device(nand, cs);
224 out_be32((u32 *)(base + NDFC_BCFG0 + (cs << 2)), 0x80002222);