blob: 07c493679bf021840fbd325f213110551f78b3d9 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +02002/*
3 * (C) Copyright 2008
4 * Sergei Poselenov, Emcraft Systems, sposelenov@emcraft.com.
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +02005 */
6
7#include <common.h>
8
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02009#if defined(CONFIG_SYS_NAND_BASE)
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020010#include <nand.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090011#include <linux/errno.h>
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020012#include <asm/io.h>
13
14static int state;
Marek Vasut169de902011-10-04 00:56:09 +020015static void sc_nand_write_byte(struct mtd_info *mtd, u_char byte);
16static void sc_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
17static u_char sc_nand_read_byte(struct mtd_info *mtd);
18static u16 sc_nand_read_word(struct mtd_info *mtd);
19static void sc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
Marek Vasut169de902011-10-04 00:56:09 +020020static int sc_nand_device_ready(struct mtd_info *mtdinfo);
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020021
22#define FPGA_NAND_CMD_MASK (0x7 << 28)
Scott Wood68cf19a2008-08-13 18:24:05 -050023#define FPGA_NAND_CMD_COMMAND (0x0 << 28)
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020024#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)
Scott Wood68cf19a2008-08-13 18:24:05 -050029#define FPGA_NAND_DATA_SHIFT 16
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020030
31/**
Marek Vasut169de902011-10-04 00:56:09 +020032 * sc_nand_write_byte - write one byte to the chip
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020033 * @mtd: MTD device structure
34 * @byte: pointer to data byte to write
35 */
Marek Vasut169de902011-10-04 00:56:09 +020036static void sc_nand_write_byte(struct mtd_info *mtd, u_char byte)
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020037{
Marek Vasut169de902011-10-04 00:56:09 +020038 sc_nand_write_buf(mtd, (const uchar *)&byte, sizeof(byte));
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020039}
40
41/**
Marek Vasut169de902011-10-04 00:56:09 +020042 * sc_nand_write_buf - write buffer to chip
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020043 * @mtd: MTD device structure
44 * @buf: data buffer
45 * @len: number of bytes to write
46 */
Marek Vasut169de902011-10-04 00:56:09 +020047static void sc_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020048{
49 int i;
Scott Wood17cb4b82016-05-30 13:57:56 -050050 struct nand_chip *this = mtd_to_nand(mtd);
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020051
52 for (i = 0; i < len; i++) {
Scott Wood68cf19a2008-08-13 18:24:05 -050053 out_be32(this->IO_ADDR_W,
54 state | (buf[i] << FPGA_NAND_DATA_SHIFT));
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020055 }
56}
57
58
59/**
Marek Vasut169de902011-10-04 00:56:09 +020060 * sc_nand_read_byte - read one byte from the chip
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020061 * @mtd: MTD device structure
62 */
Marek Vasut169de902011-10-04 00:56:09 +020063static u_char sc_nand_read_byte(struct mtd_info *mtd)
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020064{
65 u8 byte;
Marek Vasut169de902011-10-04 00:56:09 +020066 sc_nand_read_buf(mtd, (uchar *)&byte, sizeof(byte));
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020067 return byte;
68}
69
70/**
Marek Vasut169de902011-10-04 00:56:09 +020071 * sc_nand_read_word - read one word from the chip
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020072 * @mtd: MTD device structure
73 */
Marek Vasut169de902011-10-04 00:56:09 +020074static u16 sc_nand_read_word(struct mtd_info *mtd)
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020075{
76 u16 word;
Marek Vasut169de902011-10-04 00:56:09 +020077 sc_nand_read_buf(mtd, (uchar *)&word, sizeof(word));
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020078 return word;
79}
80
81/**
Marek Vasut169de902011-10-04 00:56:09 +020082 * sc_nand_read_buf - read chip data into buffer
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020083 * @mtd: MTD device structure
84 * @buf: buffer to store date
85 * @len: number of bytes to read
86 */
Marek Vasut169de902011-10-04 00:56:09 +020087static void sc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020088{
89 int i;
Scott Wood17cb4b82016-05-30 13:57:56 -050090 struct nand_chip *this = mtd_to_nand(mtd);
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +020091 int val;
92
93 val = (state & FPGA_NAND_ENABLE) | FPGA_NAND_CMD_READ;
94
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;
98 }
99}
100
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +0200101/**
Marek Vasut169de902011-10-04 00:56:09 +0200102 * sc_nand_device_ready - Check the NAND device is ready for next command.
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +0200103 * @mtd: MTD device structure
104 */
Marek Vasut169de902011-10-04 00:56:09 +0200105static int sc_nand_device_ready(struct mtd_info *mtdinfo)
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +0200106{
Scott Wood17cb4b82016-05-30 13:57:56 -0500107 struct nand_chip *this = mtd_to_nand(mtdinfo);
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +0200108
109 if (in_be32(this->IO_ADDR_W) & FPGA_NAND_BUSY)
110 return 0; /* busy */
111 return 1;
112}
113
114/**
Marek Vasut169de902011-10-04 00:56:09 +0200115 * sc_nand_hwcontrol - NAND control functions wrapper.
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +0200116 * @mtd: MTD device structure
117 * @cmd: Command
118 */
Marek Vasut169de902011-10-04 00:56:09 +0200119static void sc_nand_hwcontrol(struct mtd_info *mtdinfo, int cmd, unsigned int ctrl)
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +0200120{
Scott Wood68cf19a2008-08-13 18:24:05 -0500121 if (ctrl & NAND_CTRL_CHANGE) {
122 state &= ~(FPGA_NAND_CMD_MASK | FPGA_NAND_ENABLE);
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +0200123
Scott Wood68cf19a2008-08-13 18:24:05 -0500124 switch (ctrl & (NAND_ALE | NAND_CLE)) {
125 case 0:
126 state |= FPGA_NAND_CMD_WRITE;
127 break;
128
129 case NAND_ALE:
130 state |= FPGA_NAND_CMD_ADDR;
131 break;
132
133 case NAND_CLE:
134 state |= FPGA_NAND_CMD_COMMAND;
135 break;
136
137 default:
138 printf("%s: unknown ctrl %#x\n", __FUNCTION__, ctrl);
139 }
140
141 if (ctrl & NAND_NCE)
142 state |= FPGA_NAND_ENABLE;
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +0200143 }
Scott Wood68cf19a2008-08-13 18:24:05 -0500144
145 if (cmd != NAND_CMD_NONE)
Marek Vasut169de902011-10-04 00:56:09 +0200146 sc_nand_write_byte(mtdinfo, cmd);
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +0200147}
148
149int board_nand_init(struct nand_chip *nand)
150{
Marek Vasut169de902011-10-04 00:56:09 +0200151 nand->cmd_ctrl = sc_nand_hwcontrol;
Scott Wood68cf19a2008-08-13 18:24:05 -0500152 nand->ecc.mode = NAND_ECC_SOFT;
Marek Vasut169de902011-10-04 00:56:09 +0200153 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;
Sergei Poselenovfd51b0e2008-06-06 15:42:44 +0200158
159 return 0;
160}
161
162#endif