3 * Sergei Poselenov, Emcraft Systems, sposelenov@emcraft.com.
5 * SPDX-License-Identifier: GPL-2.0+
10 #if defined(CONFIG_SYS_NAND_BASE)
12 #include <asm/errno.h>
16 static void sc_nand_write_byte(struct mtd_info *mtd, u_char byte);
17 static void sc_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
18 static u_char sc_nand_read_byte(struct mtd_info *mtd);
19 static u16 sc_nand_read_word(struct mtd_info *mtd);
20 static void sc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
21 #if defined(CONFIG_MTD_NAND_VERIFY_WRITE)
22 static int sc_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len);
24 static int sc_nand_device_ready(struct mtd_info *mtdinfo);
26 #define FPGA_NAND_CMD_MASK (0x7 << 28)
27 #define FPGA_NAND_CMD_COMMAND (0x0 << 28)
28 #define FPGA_NAND_CMD_ADDR (0x1 << 28)
29 #define FPGA_NAND_CMD_READ (0x2 << 28)
30 #define FPGA_NAND_CMD_WRITE (0x3 << 28)
31 #define FPGA_NAND_BUSY (0x1 << 15)
32 #define FPGA_NAND_ENABLE (0x1 << 31)
33 #define FPGA_NAND_DATA_SHIFT 16
36 * sc_nand_write_byte - write one byte to the chip
37 * @mtd: MTD device structure
38 * @byte: pointer to data byte to write
40 static void sc_nand_write_byte(struct mtd_info *mtd, u_char byte)
42 sc_nand_write_buf(mtd, (const uchar *)&byte, sizeof(byte));
46 * sc_nand_write_buf - write buffer to chip
47 * @mtd: MTD device structure
49 * @len: number of bytes to write
51 static void sc_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
54 struct nand_chip *this = mtd->priv;
56 for (i = 0; i < len; i++) {
57 out_be32(this->IO_ADDR_W,
58 state | (buf[i] << FPGA_NAND_DATA_SHIFT));
64 * sc_nand_read_byte - read one byte from the chip
65 * @mtd: MTD device structure
67 static u_char sc_nand_read_byte(struct mtd_info *mtd)
70 sc_nand_read_buf(mtd, (uchar *)&byte, sizeof(byte));
75 * sc_nand_read_word - read one word from the chip
76 * @mtd: MTD device structure
78 static u16 sc_nand_read_word(struct mtd_info *mtd)
81 sc_nand_read_buf(mtd, (uchar *)&word, sizeof(word));
86 * sc_nand_read_buf - read chip data into buffer
87 * @mtd: MTD device structure
88 * @buf: buffer to store date
89 * @len: number of bytes to read
91 static void sc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
94 struct nand_chip *this = mtd->priv;
97 val = (state & FPGA_NAND_ENABLE) | FPGA_NAND_CMD_READ;
99 out_be32(this->IO_ADDR_W, val);
100 for (i = 0; i < len; i++) {
101 buf[i] = (in_be32(this->IO_ADDR_R) >> FPGA_NAND_DATA_SHIFT) & 0xff;
105 #if defined(CONFIG_MTD_NAND_VERIFY_WRITE)
107 * sc_nand_verify_buf - Verify chip data against buffer
108 * @mtd: MTD device structure
109 * @buf: buffer containing the data to compare
110 * @len: number of bytes to compare
112 static int sc_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
116 for (i = 0; i < len; i++) {
117 if (buf[i] != sc_nand_read_byte(mtd));
125 * sc_nand_device_ready - Check the NAND device is ready for next command.
126 * @mtd: MTD device structure
128 static int sc_nand_device_ready(struct mtd_info *mtdinfo)
130 struct nand_chip *this = mtdinfo->priv;
132 if (in_be32(this->IO_ADDR_W) & FPGA_NAND_BUSY)
138 * sc_nand_hwcontrol - NAND control functions wrapper.
139 * @mtd: MTD device structure
142 static void sc_nand_hwcontrol(struct mtd_info *mtdinfo, int cmd, unsigned int ctrl)
144 if (ctrl & NAND_CTRL_CHANGE) {
145 state &= ~(FPGA_NAND_CMD_MASK | FPGA_NAND_ENABLE);
147 switch (ctrl & (NAND_ALE | NAND_CLE)) {
149 state |= FPGA_NAND_CMD_WRITE;
153 state |= FPGA_NAND_CMD_ADDR;
157 state |= FPGA_NAND_CMD_COMMAND;
161 printf("%s: unknown ctrl %#x\n", __FUNCTION__, ctrl);
165 state |= FPGA_NAND_ENABLE;
168 if (cmd != NAND_CMD_NONE)
169 sc_nand_write_byte(mtdinfo, cmd);
172 int board_nand_init(struct nand_chip *nand)
174 nand->cmd_ctrl = sc_nand_hwcontrol;
175 nand->ecc.mode = NAND_ECC_SOFT;
176 nand->dev_ready = sc_nand_device_ready;
177 nand->read_byte = sc_nand_read_byte;
178 nand->read_word = sc_nand_read_word;
179 nand->write_buf = sc_nand_write_buf;
180 nand->read_buf = sc_nand_read_buf;
181 #if defined(CONFIG_MTD_NAND_VERIFY_WRITE)
182 nand->verify_buf = sc_nand_verify_buf;