3 * Sergei Poselenov, Emcraft Systems, sposelenov@emcraft.com.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 #if defined(CFG_NAND_BASE)
28 #include <asm/errno.h>
32 static void nand_write_byte(struct mtd_info *mtd, u_char byte);
33 static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
34 static u_char nand_read_byte(struct mtd_info *mtd);
35 static u16 nand_read_word(struct mtd_info *mtd);
36 static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
37 static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len);
38 static int nand_device_ready(struct mtd_info *mtdinfo);
40 #define FPGA_NAND_CMD_MASK (0x7 << 28)
41 #define FPGA_NAND_CMD_COMMAND (0x0 << 28)
42 #define FPGA_NAND_CMD_ADDR (0x1 << 28)
43 #define FPGA_NAND_CMD_READ (0x2 << 28)
44 #define FPGA_NAND_CMD_WRITE (0x3 << 28)
45 #define FPGA_NAND_BUSY (0x1 << 15)
46 #define FPGA_NAND_ENABLE (0x1 << 31)
47 #define FPGA_NAND_DATA_SHIFT 16
50 * nand_write_byte - write one byte to the chip
51 * @mtd: MTD device structure
52 * @byte: pointer to data byte to write
54 static void nand_write_byte(struct mtd_info *mtd, u_char byte)
56 nand_write_buf(mtd, (const uchar *)&byte, sizeof(byte));
60 * nand_write_buf - write buffer to chip
61 * @mtd: MTD device structure
63 * @len: number of bytes to write
65 static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
68 struct nand_chip *this = mtd->priv;
70 for (i = 0; i < len; i++) {
71 out_be32(this->IO_ADDR_W,
72 state | (buf[i] << FPGA_NAND_DATA_SHIFT));
78 * nand_read_byte - read one byte from the chip
79 * @mtd: MTD device structure
81 static u_char nand_read_byte(struct mtd_info *mtd)
84 nand_read_buf(mtd, (uchar *)&byte, sizeof(byte));
89 * nand_read_word - read one word from the chip
90 * @mtd: MTD device structure
92 static u16 nand_read_word(struct mtd_info *mtd)
95 nand_read_buf(mtd, (uchar *)&word, sizeof(word));
100 * nand_read_buf - read chip data into buffer
101 * @mtd: MTD device structure
102 * @buf: buffer to store date
103 * @len: number of bytes to read
105 static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
108 struct nand_chip *this = mtd->priv;
111 val = (state & FPGA_NAND_ENABLE) | FPGA_NAND_CMD_READ;
113 out_be32(this->IO_ADDR_W, val);
114 for (i = 0; i < len; i++) {
115 buf[i] = (in_be32(this->IO_ADDR_R) >> FPGA_NAND_DATA_SHIFT) & 0xff;
120 * nand_verify_buf - Verify chip data against buffer
121 * @mtd: MTD device structure
122 * @buf: buffer containing the data to compare
123 * @len: number of bytes to compare
125 static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
129 for (i = 0; i < len; i++) {
130 if (buf[i] != nand_read_byte(mtd));
137 * nand_device_ready - Check the NAND device is ready for next command.
138 * @mtd: MTD device structure
140 static int nand_device_ready(struct mtd_info *mtdinfo)
142 struct nand_chip *this = mtdinfo->priv;
144 if (in_be32(this->IO_ADDR_W) & FPGA_NAND_BUSY)
150 * nand_hwcontrol - NAND control functions wrapper.
151 * @mtd: MTD device structure
154 static void nand_hwcontrol(struct mtd_info *mtdinfo, int cmd, unsigned int ctrl)
156 if (ctrl & NAND_CTRL_CHANGE) {
157 state &= ~(FPGA_NAND_CMD_MASK | FPGA_NAND_ENABLE);
159 switch (ctrl & (NAND_ALE | NAND_CLE)) {
161 state |= FPGA_NAND_CMD_WRITE;
165 state |= FPGA_NAND_CMD_ADDR;
169 state |= FPGA_NAND_CMD_COMMAND;
173 printf("%s: unknown ctrl %#x\n", __FUNCTION__, ctrl);
177 state |= FPGA_NAND_ENABLE;
180 if (cmd != NAND_CMD_NONE)
181 nand_write_byte(mtdinfo, cmd);
184 int board_nand_init(struct nand_chip *nand)
186 nand->cmd_ctrl = nand_hwcontrol;
187 nand->ecc.mode = NAND_ECC_SOFT;
188 nand->dev_ready = nand_device_ready;
189 nand->read_byte = nand_read_byte;
190 nand->read_word = nand_read_word;
191 nand->write_buf = nand_write_buf;
192 nand->read_buf = nand_read_buf;
193 nand->verify_buf = nand_verify_buf;