1 // SPDX-License-Identifier: GPL-2.0+
4 * Sergei Poselenov, Emcraft Systems, sposelenov@emcraft.com.
9 #if defined(CONFIG_SYS_NAND_BASE)
11 #include <linux/errno.h>
15 static void sc_nand_write_byte(struct mtd_info *mtd, u_char byte);
16 static void sc_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
17 static u_char sc_nand_read_byte(struct mtd_info *mtd);
18 static u16 sc_nand_read_word(struct mtd_info *mtd);
19 static void sc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
20 static int sc_nand_device_ready(struct mtd_info *mtdinfo);
22 #define FPGA_NAND_CMD_MASK (0x7 << 28)
23 #define FPGA_NAND_CMD_COMMAND (0x0 << 28)
24 #define FPGA_NAND_CMD_ADDR (0x1 << 28)
25 #define FPGA_NAND_CMD_READ (0x2 << 28)
26 #define FPGA_NAND_CMD_WRITE (0x3 << 28)
27 #define FPGA_NAND_BUSY (0x1 << 15)
28 #define FPGA_NAND_ENABLE (0x1 << 31)
29 #define FPGA_NAND_DATA_SHIFT 16
32 * sc_nand_write_byte - write one byte to the chip
33 * @mtd: MTD device structure
34 * @byte: pointer to data byte to write
36 static void sc_nand_write_byte(struct mtd_info *mtd, u_char byte)
38 sc_nand_write_buf(mtd, (const uchar *)&byte, sizeof(byte));
42 * sc_nand_write_buf - write buffer to chip
43 * @mtd: MTD device structure
45 * @len: number of bytes to write
47 static void sc_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
50 struct nand_chip *this = mtd_to_nand(mtd);
52 for (i = 0; i < len; i++) {
53 out_be32(this->IO_ADDR_W,
54 state | (buf[i] << FPGA_NAND_DATA_SHIFT));
60 * sc_nand_read_byte - read one byte from the chip
61 * @mtd: MTD device structure
63 static u_char sc_nand_read_byte(struct mtd_info *mtd)
66 sc_nand_read_buf(mtd, (uchar *)&byte, sizeof(byte));
71 * sc_nand_read_word - read one word from the chip
72 * @mtd: MTD device structure
74 static u16 sc_nand_read_word(struct mtd_info *mtd)
77 sc_nand_read_buf(mtd, (uchar *)&word, sizeof(word));
82 * sc_nand_read_buf - read chip data into buffer
83 * @mtd: MTD device structure
84 * @buf: buffer to store date
85 * @len: number of bytes to read
87 static void sc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
90 struct nand_chip *this = mtd_to_nand(mtd);
93 val = (state & FPGA_NAND_ENABLE) | FPGA_NAND_CMD_READ;
95 out_be32(this->IO_ADDR_W, val);
96 for (i = 0; i < len; i++) {
97 buf[i] = (in_be32(this->IO_ADDR_R) >> FPGA_NAND_DATA_SHIFT) & 0xff;
102 * sc_nand_device_ready - Check the NAND device is ready for next command.
103 * @mtd: MTD device structure
105 static int sc_nand_device_ready(struct mtd_info *mtdinfo)
107 struct nand_chip *this = mtd_to_nand(mtdinfo);
109 if (in_be32(this->IO_ADDR_W) & FPGA_NAND_BUSY)
115 * sc_nand_hwcontrol - NAND control functions wrapper.
116 * @mtd: MTD device structure
119 static void sc_nand_hwcontrol(struct mtd_info *mtdinfo, int cmd, unsigned int ctrl)
121 if (ctrl & NAND_CTRL_CHANGE) {
122 state &= ~(FPGA_NAND_CMD_MASK | FPGA_NAND_ENABLE);
124 switch (ctrl & (NAND_ALE | NAND_CLE)) {
126 state |= FPGA_NAND_CMD_WRITE;
130 state |= FPGA_NAND_CMD_ADDR;
134 state |= FPGA_NAND_CMD_COMMAND;
138 printf("%s: unknown ctrl %#x\n", __FUNCTION__, ctrl);
142 state |= FPGA_NAND_ENABLE;
145 if (cmd != NAND_CMD_NONE)
146 sc_nand_write_byte(mtdinfo, cmd);
149 int board_nand_init(struct nand_chip *nand)
151 nand->cmd_ctrl = sc_nand_hwcontrol;
152 nand->ecc.mode = NAND_ECC_SOFT;
153 nand->dev_ready = sc_nand_device_ready;
154 nand->read_byte = sc_nand_read_byte;
155 nand->read_word = sc_nand_read_word;
156 nand->write_buf = sc_nand_write_buf;
157 nand->read_buf = sc_nand_read_buf;