/*
* Copyright (c) 2003-2013 Broadcom Corporation
*
* Copyright (c) 2009-2010 Micron Technology, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
#include <linux/mtd/nand.h>
#include <linux/spi/spi.h>
#include "mt29f_spinand.h"
#define BUFSIZE (10 * 64 * 2048)
#define CACHE_BUF 2112
/*
* OOB area specification layout: Total 32 available free bytes.
*/
static inline struct spinand_state *mtd_to_state(struct mtd_info *mtd)
{
struct nand_chip *chip = (struct nand_chip *)mtd->priv;
struct spinand_info *info = (struct spinand_info *)chip->priv;
struct spinand_state *state = (struct spinand_state *)info->priv;
return state;
}
#ifdef CONFIG_MTD_SPINAND_ONDIEECC
static int enable_hw_ecc;
static int enable_read_hw_ecc;
static struct nand_ecclayout spinand_oob_64 = {
.eccbytes = 24,
.eccpos = {
1, 2, 3, 4, 5, 6,
17, 18, 19, 20, 21, 22,
33, 34, 35, 36, 37, 38,
49, 50, 51, 52, 53, 54, },
.oobavail = 32,
.oobfree = {
{.offset = 8,
.length = 8},
{.offset = 24,
.length = 8},
{.offset = 40,
.length = 8},
{.offset = 56,
.length = 8},
}
};
#endif
/*
* spinand_cmd - to process a command to send to the SPI Nand
* Description:
* Set up the command buffer to send to the SPI controller.
* The command buffer has to initialized to 0.
*/
static int spinand_cmd(struct spi_device *spi, struct spinand_cmd *cmd)
{
struct spi_message message;
struct spi_transfer x[4];
u8 dummy = 0xff;
spi_message_init(&message);
memset(x, 0, sizeof(x));
x[0].len = 1;
x[0].tx_buf = &cmd->cmd;
spi_message_add_tail(&x[0], &message);
if (cmd->n_addr) {
x[1].len = cmd->n_addr;
x[1].tx_buf = cmd->addr;
spi_message_add_tail(&x[1], &message);
}
if (cmd->n_dummy) {
x[2].len = cmd->n_dummy;
x[2].tx_buf = &dummy;
spi_message_add_tail(&x[2], &message);
}
if (cmd->n_tx) {
x[3].len = cmd->n_tx;
x[3].tx_buf = cmd->tx_buf;
spi_message_add_tail(&x[3], &message);
}
if (cmd->n_rx) {
x[3].len = cmd->n_rx;
x[3].rx_buf = cmd->rx_buf;
spi_message_add_tail(&x[3], &message);
}
return spi_sync(spi, &message);
}
/*
* spinand_read_id- Read SPI Nand ID
* Description:
* Read ID: read two ID bytes from the SPI Nand device
*/
static int spinand_read_id(struct spi_device *spi_nand, u8 *id)
{
int retval;
u8 nand_id[3];
struct spinand_cmd cmd = {0};
cmd.cmd = CMD_READ_ID;
cmd.n_rx = 3;
cmd.rx_buf = &nand_id[0];
retval = spinand_cmd(spi_nand, &cmd);
if (retval < 0) {
dev_err(&spi_nand->dev, "error %d reading id\n", retval);
return retval;
}
id[0] = nand_id[1];
id[1] = nand_id[2];
return retval;
}
/*
* spinand_read_status- send command 0xf to the SPI Nand status register
* Description:
* After read, write, or erase, the Nand device is expected to set the
* busy status.
* This function is to allow reading the status of the command: read,
* write, and erase.
* Once the status turns to be ready, the other status bits also are
* valid status bits.
*/
static int spinand_read_status(struct spi_device *spi_nand, uint8_t *status)
{
struct spinand_cmd cmd = {0};
int ret;
cmd.cmd = CMD_READ_REG;
cmd.n_addr = 1;
cmd.addr[0] = REG_STATUS;
cmd.n_rx = 1;
cmd.rx_buf = status;
ret = spinand_cmd(spi_nand, &cmd);
if (ret < 0)
dev_err(