blob: 3802c7ec7abcd6bd2ff314c90d2adc450edc991a [file] [log] [blame]
Sergei Poselenov30115a02008-06-06 15:42:44 +02001/*
2 * (C) Copyright 2008
3 * Sergei Poselenov, Emcraft Systems, sposelenov@emcraft.com.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Sergei Poselenov30115a02008-06-06 15:42:44 +02006 */
7
8#include <common.h>
9
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020010#if defined(CONFIG_SYS_NAND_BASE)
Sergei Poselenov30115a02008-06-06 15:42:44 +020011#include <nand.h>
12#include <asm/errno.h>
13#include <asm/io.h>
14
15static int state;
Marek Vasut67450a32011-10-04 00:56:09 +020016static void sc_nand_write_byte(struct mtd_info *mtd, u_char byte);
17static void sc_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
18static u_char sc_nand_read_byte(struct mtd_info *mtd);
19static u16 sc_nand_read_word(struct mtd_info *mtd);
20static void sc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
21static int sc_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len);
22static int sc_nand_device_ready(struct mtd_info *mtdinfo);
Sergei Poselenov30115a02008-06-06 15:42:44 +020023
24#define FPGA_NAND_CMD_MASK (0x7 << 28)
Scott Wood81cb8082008-08-13 18:24:05 -050025#define FPGA_NAND_CMD_COMMAND (0x0 << 28)
Sergei Poselenov30115a02008-06-06 15:42:44 +020026#define FPGA_NAND_CMD_ADDR (0x1 << 28)
27#define FPGA_NAND_CMD_READ (0x2 << 28)
28#define FPGA_NAND_CMD_WRITE (0x3 << 28)
29#define FPGA_NAND_BUSY (0x1 << 15)
30#define FPGA_NAND_ENABLE (0x1 << 31)
Scott Wood81cb8082008-08-13 18:24:05 -050031#define FPGA_NAND_DATA_SHIFT 16
Sergei Poselenov30115a02008-06-06 15:42:44 +020032
33/**
Marek Vasut67450a32011-10-04 00:56:09 +020034 * sc_nand_write_byte - write one byte to the chip
Sergei Poselenov30115a02008-06-06 15:42:44 +020035 * @mtd: MTD device structure
36 * @byte: pointer to data byte to write
37 */
Marek Vasut67450a32011-10-04 00:56:09 +020038static void sc_nand_write_byte(struct mtd_info *mtd, u_char byte)
Sergei Poselenov30115a02008-06-06 15:42:44 +020039{
Marek Vasut67450a32011-10-04 00:56:09 +020040 sc_nand_write_buf(mtd, (const uchar *)&byte, sizeof(byte));
Sergei Poselenov30115a02008-06-06 15:42:44 +020041}
42
43/**
Marek Vasut67450a32011-10-04 00:56:09 +020044 * sc_nand_write_buf - write buffer to chip
Sergei Poselenov30115a02008-06-06 15:42:44 +020045 * @mtd: MTD device structure
46 * @buf: data buffer
47 * @len: number of bytes to write
48 */
Marek Vasut67450a32011-10-04 00:56:09 +020049static void sc_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
Sergei Poselenov30115a02008-06-06 15:42:44 +020050{
51 int i;
52 struct nand_chip *this = mtd->priv;
Sergei Poselenov30115a02008-06-06 15:42:44 +020053
54 for (i = 0; i < len; i++) {
Scott Wood81cb8082008-08-13 18:24:05 -050055 out_be32(this->IO_ADDR_W,
56 state | (buf[i] << FPGA_NAND_DATA_SHIFT));
Sergei Poselenov30115a02008-06-06 15:42:44 +020057 }
58}
59
60
61/**
Marek Vasut67450a32011-10-04 00:56:09 +020062 * sc_nand_read_byte - read one byte from the chip
Sergei Poselenov30115a02008-06-06 15:42:44 +020063 * @mtd: MTD device structure
64 */
Marek Vasut67450a32011-10-04 00:56:09 +020065static u_char sc_nand_read_byte(struct mtd_info *mtd)
Sergei Poselenov30115a02008-06-06 15:42:44 +020066{
67 u8 byte;
Marek Vasut67450a32011-10-04 00:56:09 +020068 sc_nand_read_buf(mtd, (uchar *)&byte, sizeof(byte));
Sergei Poselenov30115a02008-06-06 15:42:44 +020069 return byte;
70}
71
72/**
Marek Vasut67450a32011-10-04 00:56:09 +020073 * sc_nand_read_word - read one word from the chip
Sergei Poselenov30115a02008-06-06 15:42:44 +020074 * @mtd: MTD device structure
75 */
Marek Vasut67450a32011-10-04 00:56:09 +020076static u16 sc_nand_read_word(struct mtd_info *mtd)
Sergei Poselenov30115a02008-06-06 15:42:44 +020077{
78 u16 word;
Marek Vasut67450a32011-10-04 00:56:09 +020079 sc_nand_read_buf(mtd, (uchar *)&word, sizeof(word));
Sergei Poselenov30115a02008-06-06 15:42:44 +020080 return word;
81}
82
83/**
Marek Vasut67450a32011-10-04 00:56:09 +020084 * sc_nand_read_buf - read chip data into buffer
Sergei Poselenov30115a02008-06-06 15:42:44 +020085 * @mtd: MTD device structure
86 * @buf: buffer to store date
87 * @len: number of bytes to read
88 */
Marek Vasut67450a32011-10-04 00:56:09 +020089static void sc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
Sergei Poselenov30115a02008-06-06 15:42:44 +020090{
91 int i;
92 struct nand_chip *this = mtd->priv;
93 int val;
94
95 val = (state & FPGA_NAND_ENABLE) | FPGA_NAND_CMD_READ;
96
97 out_be32(this->IO_ADDR_W, val);
98 for (i = 0; i < len; i++) {
99 buf[i] = (in_be32(this->IO_ADDR_R) >> FPGA_NAND_DATA_SHIFT) & 0xff;
100 }
101}
102
103/**
Marek Vasut67450a32011-10-04 00:56:09 +0200104 * sc_nand_verify_buf - Verify chip data against buffer
Sergei Poselenov30115a02008-06-06 15:42:44 +0200105 * @mtd: MTD device structure
106 * @buf: buffer containing the data to compare
107 * @len: number of bytes to compare
108 */
Marek Vasut67450a32011-10-04 00:56:09 +0200109static int sc_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
Sergei Poselenov30115a02008-06-06 15:42:44 +0200110{
111 int i;
112
113 for (i = 0; i < len; i++) {
Marek Vasut67450a32011-10-04 00:56:09 +0200114 if (buf[i] != sc_nand_read_byte(mtd));
Scott Wood81cb8082008-08-13 18:24:05 -0500115 return -EFAULT;
Sergei Poselenov30115a02008-06-06 15:42:44 +0200116 }
117 return 0;
118}
119
120/**
Marek Vasut67450a32011-10-04 00:56:09 +0200121 * sc_nand_device_ready - Check the NAND device is ready for next command.
Sergei Poselenov30115a02008-06-06 15:42:44 +0200122 * @mtd: MTD device structure
123 */
Marek Vasut67450a32011-10-04 00:56:09 +0200124static int sc_nand_device_ready(struct mtd_info *mtdinfo)
Sergei Poselenov30115a02008-06-06 15:42:44 +0200125{
126 struct nand_chip *this = mtdinfo->priv;
127
128 if (in_be32(this->IO_ADDR_W) & FPGA_NAND_BUSY)
129 return 0; /* busy */
130 return 1;
131}
132
133/**
Marek Vasut67450a32011-10-04 00:56:09 +0200134 * sc_nand_hwcontrol - NAND control functions wrapper.
Sergei Poselenov30115a02008-06-06 15:42:44 +0200135 * @mtd: MTD device structure
136 * @cmd: Command
137 */
Marek Vasut67450a32011-10-04 00:56:09 +0200138static void sc_nand_hwcontrol(struct mtd_info *mtdinfo, int cmd, unsigned int ctrl)
Sergei Poselenov30115a02008-06-06 15:42:44 +0200139{
Scott Wood81cb8082008-08-13 18:24:05 -0500140 if (ctrl & NAND_CTRL_CHANGE) {
141 state &= ~(FPGA_NAND_CMD_MASK | FPGA_NAND_ENABLE);
142
143 switch (ctrl & (NAND_ALE | NAND_CLE)) {
144 case 0:
145 state |= FPGA_NAND_CMD_WRITE;
146 break;
147
148 case NAND_ALE:
149 state |= FPGA_NAND_CMD_ADDR;
150 break;
Sergei Poselenov30115a02008-06-06 15:42:44 +0200151
Scott Wood81cb8082008-08-13 18:24:05 -0500152 case NAND_CLE:
153 state |= FPGA_NAND_CMD_COMMAND;
154 break;
155
156 default:
157 printf("%s: unknown ctrl %#x\n", __FUNCTION__, ctrl);
158 }
159
160 if (ctrl & NAND_NCE)
161 state |= FPGA_NAND_ENABLE;
Sergei Poselenov30115a02008-06-06 15:42:44 +0200162 }
Scott Wood81cb8082008-08-13 18:24:05 -0500163
164 if (cmd != NAND_CMD_NONE)
Marek Vasut67450a32011-10-04 00:56:09 +0200165 sc_nand_write_byte(mtdinfo, cmd);
Sergei Poselenov30115a02008-06-06 15:42:44 +0200166}
167
168int board_nand_init(struct nand_chip *nand)
169{
Marek Vasut67450a32011-10-04 00:56:09 +0200170 nand->cmd_ctrl = sc_nand_hwcontrol;
Scott Wood81cb8082008-08-13 18:24:05 -0500171 nand->ecc.mode = NAND_ECC_SOFT;
Marek Vasut67450a32011-10-04 00:56:09 +0200172 nand->dev_ready = sc_nand_device_ready;
173 nand->read_byte = sc_nand_read_byte;
174 nand->read_word = sc_nand_read_word;
175 nand->write_buf = sc_nand_write_buf;
176 nand->read_buf = sc_nand_read_buf;
177 nand->verify_buf = sc_nand_verify_buf;
Sergei Poselenov30115a02008-06-06 15:42:44 +0200178
179 return 0;
180}
181
182#endif