2008-06-06 13:42:44 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2008
|
|
|
|
* Sergei Poselenov, Emcraft Systems, sposelenov@emcraft.com.
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2008-06-06 13:42:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
|
2008-10-16 13:01:15 +00:00
|
|
|
#if defined(CONFIG_SYS_NAND_BASE)
|
2008-06-06 13:42:44 +00:00
|
|
|
#include <nand.h>
|
|
|
|
#include <asm/errno.h>
|
|
|
|
#include <asm/io.h>
|
|
|
|
|
|
|
|
static int state;
|
2011-10-03 22:56:09 +00:00
|
|
|
static void sc_nand_write_byte(struct mtd_info *mtd, u_char byte);
|
|
|
|
static void sc_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
|
|
|
|
static u_char sc_nand_read_byte(struct mtd_info *mtd);
|
|
|
|
static u16 sc_nand_read_word(struct mtd_info *mtd);
|
|
|
|
static void sc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
|
|
|
|
static int sc_nand_device_ready(struct mtd_info *mtdinfo);
|
2008-06-06 13:42:44 +00:00
|
|
|
|
|
|
|
#define FPGA_NAND_CMD_MASK (0x7 << 28)
|
2008-08-13 23:24:05 +00:00
|
|
|
#define FPGA_NAND_CMD_COMMAND (0x0 << 28)
|
2008-06-06 13:42:44 +00:00
|
|
|
#define FPGA_NAND_CMD_ADDR (0x1 << 28)
|
|
|
|
#define FPGA_NAND_CMD_READ (0x2 << 28)
|
|
|
|
#define FPGA_NAND_CMD_WRITE (0x3 << 28)
|
|
|
|
#define FPGA_NAND_BUSY (0x1 << 15)
|
|
|
|
#define FPGA_NAND_ENABLE (0x1 << 31)
|
2008-08-13 23:24:05 +00:00
|
|
|
#define FPGA_NAND_DATA_SHIFT 16
|
2008-06-06 13:42:44 +00:00
|
|
|
|
|
|
|
/**
|
2011-10-03 22:56:09 +00:00
|
|
|
* sc_nand_write_byte - write one byte to the chip
|
2008-06-06 13:42:44 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
* @byte: pointer to data byte to write
|
|
|
|
*/
|
2011-10-03 22:56:09 +00:00
|
|
|
static void sc_nand_write_byte(struct mtd_info *mtd, u_char byte)
|
2008-06-06 13:42:44 +00:00
|
|
|
{
|
2011-10-03 22:56:09 +00:00
|
|
|
sc_nand_write_buf(mtd, (const uchar *)&byte, sizeof(byte));
|
2008-06-06 13:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-03 22:56:09 +00:00
|
|
|
* sc_nand_write_buf - write buffer to chip
|
2008-06-06 13:42:44 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
* @buf: data buffer
|
|
|
|
* @len: number of bytes to write
|
|
|
|
*/
|
2011-10-03 22:56:09 +00:00
|
|
|
static void sc_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
|
2008-06-06 13:42:44 +00:00
|
|
|
{
|
|
|
|
int i;
|
2016-05-30 18:57:56 +00:00
|
|
|
struct nand_chip *this = mtd_to_nand(mtd);
|
2008-06-06 13:42:44 +00:00
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
2008-08-13 23:24:05 +00:00
|
|
|
out_be32(this->IO_ADDR_W,
|
|
|
|
state | (buf[i] << FPGA_NAND_DATA_SHIFT));
|
2008-06-06 13:42:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-10-03 22:56:09 +00:00
|
|
|
* sc_nand_read_byte - read one byte from the chip
|
2008-06-06 13:42:44 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
*/
|
2011-10-03 22:56:09 +00:00
|
|
|
static u_char sc_nand_read_byte(struct mtd_info *mtd)
|
2008-06-06 13:42:44 +00:00
|
|
|
{
|
|
|
|
u8 byte;
|
2011-10-03 22:56:09 +00:00
|
|
|
sc_nand_read_buf(mtd, (uchar *)&byte, sizeof(byte));
|
2008-06-06 13:42:44 +00:00
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-03 22:56:09 +00:00
|
|
|
* sc_nand_read_word - read one word from the chip
|
2008-06-06 13:42:44 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
*/
|
2011-10-03 22:56:09 +00:00
|
|
|
static u16 sc_nand_read_word(struct mtd_info *mtd)
|
2008-06-06 13:42:44 +00:00
|
|
|
{
|
|
|
|
u16 word;
|
2011-10-03 22:56:09 +00:00
|
|
|
sc_nand_read_buf(mtd, (uchar *)&word, sizeof(word));
|
2008-06-06 13:42:44 +00:00
|
|
|
return word;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-03 22:56:09 +00:00
|
|
|
* sc_nand_read_buf - read chip data into buffer
|
2008-06-06 13:42:44 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
* @buf: buffer to store date
|
|
|
|
* @len: number of bytes to read
|
|
|
|
*/
|
2011-10-03 22:56:09 +00:00
|
|
|
static void sc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
|
2008-06-06 13:42:44 +00:00
|
|
|
{
|
|
|
|
int i;
|
2016-05-30 18:57:56 +00:00
|
|
|
struct nand_chip *this = mtd_to_nand(mtd);
|
2008-06-06 13:42:44 +00:00
|
|
|
int val;
|
|
|
|
|
|
|
|
val = (state & FPGA_NAND_ENABLE) | FPGA_NAND_CMD_READ;
|
|
|
|
|
|
|
|
out_be32(this->IO_ADDR_W, val);
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
buf[i] = (in_be32(this->IO_ADDR_R) >> FPGA_NAND_DATA_SHIFT) & 0xff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-03 22:56:09 +00:00
|
|
|
* sc_nand_device_ready - Check the NAND device is ready for next command.
|
2008-06-06 13:42:44 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
*/
|
2011-10-03 22:56:09 +00:00
|
|
|
static int sc_nand_device_ready(struct mtd_info *mtdinfo)
|
2008-06-06 13:42:44 +00:00
|
|
|
{
|
2016-05-30 18:57:56 +00:00
|
|
|
struct nand_chip *this = mtd_to_nand(mtdinfo);
|
2008-06-06 13:42:44 +00:00
|
|
|
|
|
|
|
if (in_be32(this->IO_ADDR_W) & FPGA_NAND_BUSY)
|
|
|
|
return 0; /* busy */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-03 22:56:09 +00:00
|
|
|
* sc_nand_hwcontrol - NAND control functions wrapper.
|
2008-06-06 13:42:44 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
* @cmd: Command
|
|
|
|
*/
|
2011-10-03 22:56:09 +00:00
|
|
|
static void sc_nand_hwcontrol(struct mtd_info *mtdinfo, int cmd, unsigned int ctrl)
|
2008-06-06 13:42:44 +00:00
|
|
|
{
|
2008-08-13 23:24:05 +00:00
|
|
|
if (ctrl & NAND_CTRL_CHANGE) {
|
|
|
|
state &= ~(FPGA_NAND_CMD_MASK | FPGA_NAND_ENABLE);
|
|
|
|
|
|
|
|
switch (ctrl & (NAND_ALE | NAND_CLE)) {
|
|
|
|
case 0:
|
|
|
|
state |= FPGA_NAND_CMD_WRITE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NAND_ALE:
|
|
|
|
state |= FPGA_NAND_CMD_ADDR;
|
|
|
|
break;
|
2008-06-06 13:42:44 +00:00
|
|
|
|
2008-08-13 23:24:05 +00:00
|
|
|
case NAND_CLE:
|
|
|
|
state |= FPGA_NAND_CMD_COMMAND;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("%s: unknown ctrl %#x\n", __FUNCTION__, ctrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctrl & NAND_NCE)
|
|
|
|
state |= FPGA_NAND_ENABLE;
|
2008-06-06 13:42:44 +00:00
|
|
|
}
|
2008-08-13 23:24:05 +00:00
|
|
|
|
|
|
|
if (cmd != NAND_CMD_NONE)
|
2011-10-03 22:56:09 +00:00
|
|
|
sc_nand_write_byte(mtdinfo, cmd);
|
2008-06-06 13:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int board_nand_init(struct nand_chip *nand)
|
|
|
|
{
|
2011-10-03 22:56:09 +00:00
|
|
|
nand->cmd_ctrl = sc_nand_hwcontrol;
|
2008-08-13 23:24:05 +00:00
|
|
|
nand->ecc.mode = NAND_ECC_SOFT;
|
2011-10-03 22:56:09 +00:00
|
|
|
nand->dev_ready = sc_nand_device_ready;
|
|
|
|
nand->read_byte = sc_nand_read_byte;
|
|
|
|
nand->read_word = sc_nand_read_word;
|
|
|
|
nand->write_buf = sc_nand_write_buf;
|
|
|
|
nand->read_buf = sc_nand_read_buf;
|
2008-06-06 13:42:44 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|