2019-05-19 20:08:20 +08:00
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2006-11-30 16:17:38 +08:00
|
|
|
|
/*
|
2006-10-22 22:09:33 +08:00
|
|
|
|
* Driver for One Laptop Per Child ‘CAFÉ’ controller, aka Marvell 88ALP01
|
2006-10-06 22:36:29 +08:00
|
|
|
|
*
|
2008-09-03 16:47:17 +08:00
|
|
|
|
* The data sheet for this device can be found at:
|
2010-10-18 17:03:14 +08:00
|
|
|
|
* http://wiki.laptop.org/go/Datasheets
|
2008-09-03 16:47:17 +08:00
|
|
|
|
*
|
2006-10-06 22:36:29 +08:00
|
|
|
|
* Copyright © 2006 Red Hat, Inc.
|
|
|
|
|
* Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
|
|
|
|
|
*/
|
|
|
|
|
|
2006-10-20 09:11:40 +08:00
|
|
|
|
#define DEBUG
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
|
|
|
|
#include <linux/device.h>
|
|
|
|
|
#undef DEBUG
|
|
|
|
|
#include <linux/mtd/mtd.h>
|
2017-08-04 23:29:10 +08:00
|
|
|
|
#include <linux/mtd/rawnand.h>
|
2007-10-29 09:56:39 +08:00
|
|
|
|
#include <linux/mtd/partitions.h>
|
2007-05-02 18:18:49 +08:00
|
|
|
|
#include <linux/rslib.h>
|
2006-10-06 22:36:29 +08:00
|
|
|
|
#include <linux/pci.h>
|
|
|
|
|
#include <linux/delay.h>
|
|
|
|
|
#include <linux/interrupt.h>
|
2007-01-30 21:23:30 +08:00
|
|
|
|
#include <linux/dma-mapping.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
|
#include <linux/slab.h>
|
2011-07-04 03:17:31 +08:00
|
|
|
|
#include <linux/module.h>
|
2006-10-06 22:36:29 +08:00
|
|
|
|
#include <asm/io.h>
|
|
|
|
|
|
|
|
|
|
#define CAFE_NAND_CTRL1 0x00
|
|
|
|
|
#define CAFE_NAND_CTRL2 0x04
|
|
|
|
|
#define CAFE_NAND_CTRL3 0x08
|
|
|
|
|
#define CAFE_NAND_STATUS 0x0c
|
|
|
|
|
#define CAFE_NAND_IRQ 0x10
|
|
|
|
|
#define CAFE_NAND_IRQ_MASK 0x14
|
|
|
|
|
#define CAFE_NAND_DATA_LEN 0x18
|
|
|
|
|
#define CAFE_NAND_ADDR1 0x1c
|
|
|
|
|
#define CAFE_NAND_ADDR2 0x20
|
|
|
|
|
#define CAFE_NAND_TIMING1 0x24
|
|
|
|
|
#define CAFE_NAND_TIMING2 0x28
|
|
|
|
|
#define CAFE_NAND_TIMING3 0x2c
|
|
|
|
|
#define CAFE_NAND_NONMEM 0x30
|
2006-10-22 09:18:48 +08:00
|
|
|
|
#define CAFE_NAND_ECC_RESULT 0x3C
|
2006-10-22 22:09:33 +08:00
|
|
|
|
#define CAFE_NAND_DMA_CTRL 0x40
|
|
|
|
|
#define CAFE_NAND_DMA_ADDR0 0x44
|
|
|
|
|
#define CAFE_NAND_DMA_ADDR1 0x48
|
2006-10-22 09:18:48 +08:00
|
|
|
|
#define CAFE_NAND_ECC_SYN01 0x50
|
|
|
|
|
#define CAFE_NAND_ECC_SYN23 0x54
|
|
|
|
|
#define CAFE_NAND_ECC_SYN45 0x58
|
|
|
|
|
#define CAFE_NAND_ECC_SYN67 0x5c
|
2006-10-06 22:36:29 +08:00
|
|
|
|
#define CAFE_NAND_READ_DATA 0x1000
|
|
|
|
|
#define CAFE_NAND_WRITE_DATA 0x2000
|
|
|
|
|
|
2006-10-31 12:30:11 +08:00
|
|
|
|
#define CAFE_GLOBAL_CTRL 0x3004
|
|
|
|
|
#define CAFE_GLOBAL_IRQ 0x3008
|
|
|
|
|
#define CAFE_GLOBAL_IRQ_MASK 0x300c
|
|
|
|
|
#define CAFE_NAND_RESET 0x3034
|
|
|
|
|
|
2007-05-02 19:26:37 +08:00
|
|
|
|
/* Missing from the datasheet: bit 19 of CTRL1 sets CE0 vs. CE1 */
|
|
|
|
|
#define CTRL1_CHIPSELECT (1<<19)
|
|
|
|
|
|
2006-10-06 22:36:29 +08:00
|
|
|
|
struct cafe_priv {
|
|
|
|
|
struct nand_chip nand;
|
|
|
|
|
struct pci_dev *pdev;
|
|
|
|
|
void __iomem *mmio;
|
2007-05-02 18:18:49 +08:00
|
|
|
|
struct rs_control *rs;
|
2006-10-06 22:36:29 +08:00
|
|
|
|
uint32_t ctl1;
|
|
|
|
|
uint32_t ctl2;
|
|
|
|
|
int datalen;
|
|
|
|
|
int nr_data;
|
|
|
|
|
int data_pos;
|
|
|
|
|
int page_addr;
|
2018-07-25 21:31:37 +08:00
|
|
|
|
bool usedma;
|
2006-10-06 22:36:29 +08:00
|
|
|
|
dma_addr_t dmaaddr;
|
|
|
|
|
unsigned char *dmabuf;
|
|
|
|
|
};
|
|
|
|
|
|
2006-10-27 19:50:04 +08:00
|
|
|
|
static int usedma = 1;
|
2006-10-06 22:36:29 +08:00
|
|
|
|
module_param(usedma, int, 0644);
|
|
|
|
|
|
2006-10-20 09:11:40 +08:00
|
|
|
|
static int skipbbt = 0;
|
|
|
|
|
module_param(skipbbt, int, 0644);
|
|
|
|
|
|
|
|
|
|
static int debug = 0;
|
|
|
|
|
module_param(debug, int, 0644);
|
|
|
|
|
|
2006-10-31 12:36:04 +08:00
|
|
|
|
static int regdebug = 0;
|
|
|
|
|
module_param(regdebug, int, 0644);
|
|
|
|
|
|
2006-10-27 19:50:04 +08:00
|
|
|
|
static int checkecc = 1;
|
2006-10-23 21:29:04 +08:00
|
|
|
|
module_param(checkecc, int, 0644);
|
|
|
|
|
|
2007-10-15 02:35:30 +08:00
|
|
|
|
static unsigned int numtimings;
|
2007-01-23 15:35:27 +08:00
|
|
|
|
static int timing[3];
|
|
|
|
|
module_param_array(timing, int, &numtimings, 0644);
|
2006-10-27 19:50:04 +08:00
|
|
|
|
|
2008-10-09 07:08:20 +08:00
|
|
|
|
static const char *part_probes[] = { "cmdlinepart", "RedBoot", NULL };
|
2007-10-29 09:56:39 +08:00
|
|
|
|
|
2006-10-22 09:18:48 +08:00
|
|
|
|
/* Hrm. Why isn't this already conditional on something in the struct device? */
|
2006-10-20 09:11:40 +08:00
|
|
|
|
#define cafe_dev_dbg(dev, args...) do { if (debug) dev_dbg(dev, ##args); } while(0)
|
|
|
|
|
|
2006-10-31 12:30:11 +08:00
|
|
|
|
/* Make it easier to switch to PIO if we need to */
|
|
|
|
|
#define cafe_readl(cafe, addr) readl((cafe)->mmio + CAFE_##addr)
|
|
|
|
|
#define cafe_writel(cafe, datum, addr) writel(datum, (cafe)->mmio + CAFE_##addr)
|
2006-10-20 09:11:40 +08:00
|
|
|
|
|
2018-09-06 20:05:27 +08:00
|
|
|
|
static int cafe_device_ready(struct nand_chip *chip)
|
2006-10-06 22:36:29 +08:00
|
|
|
|
{
|
2015-12-10 16:00:41 +08:00
|
|
|
|
struct cafe_priv *cafe = nand_get_controller_data(chip);
|
2012-06-10 00:08:25 +08:00
|
|
|
|
int result = !!(cafe_readl(cafe, NAND_STATUS) & 0x40000000);
|
2006-10-31 12:30:11 +08:00
|
|
|
|
uint32_t irqs = cafe_readl(cafe, NAND_IRQ);
|
2006-10-22 22:09:33 +08:00
|
|
|
|
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, irqs, NAND_IRQ);
|
2006-10-22 22:09:33 +08:00
|
|
|
|
|
2006-10-20 09:11:40 +08:00
|
|
|
|
cafe_dev_dbg(&cafe->pdev->dev, "NAND device is%s ready, IRQ %x (%x) (%x,%x)\n",
|
2006-10-31 12:30:11 +08:00
|
|
|
|
result?"":" not", irqs, cafe_readl(cafe, NAND_IRQ),
|
|
|
|
|
cafe_readl(cafe, GLOBAL_IRQ), cafe_readl(cafe, GLOBAL_IRQ_MASK));
|
2006-10-22 22:09:33 +08:00
|
|
|
|
|
2006-10-06 22:36:29 +08:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-09-06 20:05:23 +08:00
|
|
|
|
static void cafe_write_buf(struct nand_chip *chip, const uint8_t *buf, int len)
|
2006-10-06 22:36:29 +08:00
|
|
|
|
{
|
2015-12-10 16:00:41 +08:00
|
|
|
|
struct cafe_priv *cafe = nand_get_controller_data(chip);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
2018-07-25 21:31:37 +08:00
|
|
|
|
if (cafe->usedma)
|
2006-10-06 22:36:29 +08:00
|
|
|
|
memcpy(cafe->dmabuf + cafe->datalen, buf, len);
|
|
|
|
|
else
|
|
|
|
|
memcpy_toio(cafe->mmio + CAFE_NAND_WRITE_DATA + cafe->datalen, buf, len);
|
2006-10-22 22:09:33 +08:00
|
|
|
|
|
2006-10-06 22:36:29 +08:00
|
|
|
|
cafe->datalen += len;
|
|
|
|
|
|
2006-10-20 09:11:40 +08:00
|
|
|
|
cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes to write buffer. datalen 0x%x\n",
|
2006-10-06 22:36:29 +08:00
|
|
|
|
len, cafe->datalen);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 20:05:22 +08:00
|
|
|
|
static void cafe_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
|
2006-10-06 22:36:29 +08:00
|
|
|
|
{
|
2015-12-10 16:00:41 +08:00
|
|
|
|
struct cafe_priv *cafe = nand_get_controller_data(chip);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
2018-07-25 21:31:37 +08:00
|
|
|
|
if (cafe->usedma)
|
2006-10-06 22:36:29 +08:00
|
|
|
|
memcpy(buf, cafe->dmabuf + cafe->datalen, len);
|
|
|
|
|
else
|
|
|
|
|
memcpy_fromio(buf, cafe->mmio + CAFE_NAND_READ_DATA + cafe->datalen, len);
|
|
|
|
|
|
2006-10-20 09:11:40 +08:00
|
|
|
|
cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes from position 0x%x in read buffer.\n",
|
2006-10-06 22:36:29 +08:00
|
|
|
|
len, cafe->datalen);
|
|
|
|
|
cafe->datalen += len;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 20:05:22 +08:00
|
|
|
|
static uint8_t cafe_read_byte(struct nand_chip *chip)
|
2006-10-06 22:36:29 +08:00
|
|
|
|
{
|
2015-12-10 16:00:41 +08:00
|
|
|
|
struct cafe_priv *cafe = nand_get_controller_data(chip);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
uint8_t d;
|
|
|
|
|
|
2018-09-06 20:05:22 +08:00
|
|
|
|
cafe_read_buf(chip, &d, 1);
|
2006-10-20 09:11:40 +08:00
|
|
|
|
cafe_dev_dbg(&cafe->pdev->dev, "Read %02x\n", d);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
|
|
|
|
return d;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 20:05:28 +08:00
|
|
|
|
static void cafe_nand_cmdfunc(struct nand_chip *chip, unsigned command,
|
2006-10-06 22:36:29 +08:00
|
|
|
|
int column, int page_addr)
|
|
|
|
|
{
|
2018-09-06 20:05:28 +08:00
|
|
|
|
struct mtd_info *mtd = nand_to_mtd(chip);
|
2015-12-10 16:00:41 +08:00
|
|
|
|
struct cafe_priv *cafe = nand_get_controller_data(chip);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
int adrbytes = 0;
|
|
|
|
|
uint32_t ctl1;
|
|
|
|
|
uint32_t doneint = 0x80000000;
|
|
|
|
|
|
2006-10-20 09:11:40 +08:00
|
|
|
|
cafe_dev_dbg(&cafe->pdev->dev, "cmdfunc %02x, 0x%x, 0x%x\n",
|
2006-10-06 22:36:29 +08:00
|
|
|
|
command, column, page_addr);
|
|
|
|
|
|
|
|
|
|
if (command == NAND_CMD_ERASE2 || command == NAND_CMD_PAGEPROG) {
|
|
|
|
|
/* Second half of a command we already calculated */
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, cafe->ctl2 | 0x100 | command, NAND_CTRL2);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
ctl1 = cafe->ctl1;
|
2006-11-01 08:19:20 +08:00
|
|
|
|
cafe->ctl2 &= ~(1<<30);
|
2006-10-20 09:11:40 +08:00
|
|
|
|
cafe_dev_dbg(&cafe->pdev->dev, "Continue command, ctl1 %08x, #data %d\n",
|
2006-10-06 22:36:29 +08:00
|
|
|
|
cafe->ctl1, cafe->nr_data);
|
|
|
|
|
goto do_command;
|
|
|
|
|
}
|
|
|
|
|
/* Reset ECC engine */
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, 0, NAND_CTRL2);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
|
|
|
|
/* Emulate NAND_CMD_READOOB on large-page chips */
|
|
|
|
|
if (mtd->writesize > 512 &&
|
|
|
|
|
command == NAND_CMD_READOOB) {
|
|
|
|
|
column += mtd->writesize;
|
|
|
|
|
command = NAND_CMD_READ0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* FIXME: Do we need to send read command before sending data
|
|
|
|
|
for small-page chips, to position the buffer correctly? */
|
|
|
|
|
|
|
|
|
|
if (column != -1) {
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, column, NAND_ADDR1);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
adrbytes = 2;
|
|
|
|
|
if (page_addr != -1)
|
|
|
|
|
goto write_adr2;
|
|
|
|
|
} else if (page_addr != -1) {
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, page_addr & 0xffff, NAND_ADDR1);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
page_addr >>= 16;
|
|
|
|
|
write_adr2:
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, page_addr, NAND_ADDR2);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
adrbytes += 2;
|
|
|
|
|
if (mtd->size > mtd->writesize << 16)
|
|
|
|
|
adrbytes++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cafe->data_pos = cafe->datalen = 0;
|
|
|
|
|
|
2007-05-02 19:26:37 +08:00
|
|
|
|
/* Set command valid bit, mask in the chip select bit */
|
|
|
|
|
ctl1 = 0x80000000 | command | (cafe->ctl1 & CTRL1_CHIPSELECT);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
|
|
|
|
/* Set RD or WR bits as appropriate */
|
|
|
|
|
if (command == NAND_CMD_READID || command == NAND_CMD_STATUS) {
|
|
|
|
|
ctl1 |= (1<<26); /* rd */
|
|
|
|
|
/* Always 5 bytes, for now */
|
2006-10-20 09:11:40 +08:00
|
|
|
|
cafe->datalen = 4;
|
2006-10-06 22:36:29 +08:00
|
|
|
|
/* And one address cycle -- even for STATUS, since the controller doesn't work without */
|
|
|
|
|
adrbytes = 1;
|
|
|
|
|
} else if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
|
|
|
|
|
command == NAND_CMD_READOOB || command == NAND_CMD_RNDOUT) {
|
|
|
|
|
ctl1 |= 1<<26; /* rd */
|
|
|
|
|
/* For now, assume just read to end of page */
|
|
|
|
|
cafe->datalen = mtd->writesize + mtd->oobsize - column;
|
|
|
|
|
} else if (command == NAND_CMD_SEQIN)
|
|
|
|
|
ctl1 |= 1<<25; /* wr */
|
|
|
|
|
|
|
|
|
|
/* Set number of address bytes */
|
|
|
|
|
if (adrbytes)
|
|
|
|
|
ctl1 |= ((adrbytes-1)|8) << 27;
|
|
|
|
|
|
|
|
|
|
if (command == NAND_CMD_SEQIN || command == NAND_CMD_ERASE1) {
|
2006-11-30 16:17:38 +08:00
|
|
|
|
/* Ignore the first command of a pair; the hardware
|
2006-10-06 22:36:29 +08:00
|
|
|
|
deals with them both at once, later */
|
|
|
|
|
cafe->ctl1 = ctl1;
|
2006-10-20 09:11:40 +08:00
|
|
|
|
cafe_dev_dbg(&cafe->pdev->dev, "Setup for delayed command, ctl1 %08x, dlen %x\n",
|
2006-10-06 22:36:29 +08:00
|
|
|
|
cafe->ctl1, cafe->datalen);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
/* RNDOUT and READ0 commands need a following byte */
|
|
|
|
|
if (command == NAND_CMD_RNDOUT)
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_RNDOUTSTART, NAND_CTRL2);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
else if (command == NAND_CMD_READ0 && mtd->writesize > 512)
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_READSTART, NAND_CTRL2);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
|
|
|
|
do_command:
|
2006-11-30 16:17:38 +08:00
|
|
|
|
cafe_dev_dbg(&cafe->pdev->dev, "dlen %x, ctl1 %x, ctl2 %x\n",
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe->datalen, ctl1, cafe_readl(cafe, NAND_CTRL2));
|
2006-10-22 22:09:33 +08:00
|
|
|
|
|
2006-10-06 22:36:29 +08:00
|
|
|
|
/* NB: The datasheet lies -- we really should be subtracting 1 here */
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, cafe->datalen, NAND_DATA_LEN);
|
|
|
|
|
cafe_writel(cafe, 0x90000000, NAND_IRQ);
|
2018-07-25 21:31:37 +08:00
|
|
|
|
if (cafe->usedma && (ctl1 & (3<<25))) {
|
2006-10-06 22:36:29 +08:00
|
|
|
|
uint32_t dmactl = 0xc0000000 + cafe->datalen;
|
|
|
|
|
/* If WR or RD bits set, set up DMA */
|
|
|
|
|
if (ctl1 & (1<<26)) {
|
|
|
|
|
/* It's a read */
|
|
|
|
|
dmactl |= (1<<29);
|
|
|
|
|
/* ... so it's done when the DMA is done, not just
|
|
|
|
|
the command. */
|
|
|
|
|
doneint = 0x10000000;
|
|
|
|
|
}
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, dmactl, NAND_DMA_CTRL);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
}
|
|
|
|
|
cafe->datalen = 0;
|
|
|
|
|
|
2006-10-31 12:36:04 +08:00
|
|
|
|
if (unlikely(regdebug)) {
|
|
|
|
|
int i;
|
|
|
|
|
printk("About to write command %08x to register 0\n", ctl1);
|
|
|
|
|
for (i=4; i< 0x5c; i+=4)
|
|
|
|
|
printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
|
2006-10-22 22:09:33 +08:00
|
|
|
|
}
|
2006-10-31 12:36:04 +08:00
|
|
|
|
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, ctl1, NAND_CTRL1);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
/* Apply this short delay always to ensure that we do wait tWB in
|
|
|
|
|
* any case on any machine. */
|
|
|
|
|
ndelay(100);
|
|
|
|
|
|
|
|
|
|
if (1) {
|
2007-02-18 08:02:11 +08:00
|
|
|
|
int c;
|
2006-10-06 22:36:29 +08:00
|
|
|
|
uint32_t irqs;
|
|
|
|
|
|
2007-02-18 08:02:11 +08:00
|
|
|
|
for (c = 500000; c != 0; c--) {
|
2006-10-31 12:30:11 +08:00
|
|
|
|
irqs = cafe_readl(cafe, NAND_IRQ);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
if (irqs & doneint)
|
|
|
|
|
break;
|
|
|
|
|
udelay(1);
|
2006-10-20 09:11:40 +08:00
|
|
|
|
if (!(c % 100000))
|
|
|
|
|
cafe_dev_dbg(&cafe->pdev->dev, "Wait for ready, IRQ %x\n", irqs);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
cpu_relax();
|
|
|
|
|
}
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, doneint, NAND_IRQ);
|
2006-10-28 22:08:38 +08:00
|
|
|
|
cafe_dev_dbg(&cafe->pdev->dev, "Command %x completed after %d usec, irqs %x (%x)\n",
|
2006-10-31 12:30:11 +08:00
|
|
|
|
command, 500000-c, irqs, cafe_readl(cafe, NAND_IRQ));
|
2006-10-06 22:36:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2006-11-01 08:19:20 +08:00
|
|
|
|
WARN_ON(cafe->ctl2 & (1<<30));
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
|
|
|
|
switch (command) {
|
|
|
|
|
|
|
|
|
|
case NAND_CMD_CACHEDPROG:
|
|
|
|
|
case NAND_CMD_PAGEPROG:
|
|
|
|
|
case NAND_CMD_ERASE1:
|
|
|
|
|
case NAND_CMD_ERASE2:
|
|
|
|
|
case NAND_CMD_SEQIN:
|
|
|
|
|
case NAND_CMD_RNDIN:
|
|
|
|
|
case NAND_CMD_STATUS:
|
|
|
|
|
case NAND_CMD_RNDOUT:
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, cafe->ctl2, NAND_CTRL2);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2018-09-06 20:05:16 +08:00
|
|
|
|
nand_wait_ready(chip);
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, cafe->ctl2, NAND_CTRL2);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 20:05:24 +08:00
|
|
|
|
static void cafe_select_chip(struct nand_chip *chip, int chipnr)
|
2006-10-06 22:36:29 +08:00
|
|
|
|
{
|
2015-12-10 16:00:41 +08:00
|
|
|
|
struct cafe_priv *cafe = nand_get_controller_data(chip);
|
2007-05-02 19:26:37 +08:00
|
|
|
|
|
|
|
|
|
cafe_dev_dbg(&cafe->pdev->dev, "select_chip %d\n", chipnr);
|
|
|
|
|
|
|
|
|
|
/* Mask the appropriate bit into the stored value of ctl1
|
|
|
|
|
which will be used by cafe_nand_cmdfunc() */
|
|
|
|
|
if (chipnr)
|
|
|
|
|
cafe->ctl1 |= CTRL1_CHIPSELECT;
|
|
|
|
|
else
|
|
|
|
|
cafe->ctl1 &= ~CTRL1_CHIPSELECT;
|
2006-10-06 22:36:29 +08:00
|
|
|
|
}
|
2006-10-22 22:09:33 +08:00
|
|
|
|
|
2009-04-22 22:02:23 +08:00
|
|
|
|
static irqreturn_t cafe_nand_interrupt(int irq, void *id)
|
2006-10-06 22:36:29 +08:00
|
|
|
|
{
|
|
|
|
|
struct mtd_info *mtd = id;
|
2015-12-01 19:03:04 +08:00
|
|
|
|
struct nand_chip *chip = mtd_to_nand(mtd);
|
2015-12-10 16:00:41 +08:00
|
|
|
|
struct cafe_priv *cafe = nand_get_controller_data(chip);
|
2006-10-31 12:30:11 +08:00
|
|
|
|
uint32_t irqs = cafe_readl(cafe, NAND_IRQ);
|
|
|
|
|
cafe_writel(cafe, irqs & ~0x90000000, NAND_IRQ);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
if (!irqs)
|
|
|
|
|
return IRQ_NONE;
|
|
|
|
|
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_dev_dbg(&cafe->pdev->dev, "irq, bits %x (%x)\n", irqs, cafe_readl(cafe, NAND_IRQ));
|
2006-10-06 22:36:29 +08:00
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 20:05:21 +08:00
|
|
|
|
static int cafe_nand_write_oob(struct nand_chip *chip, int page)
|
2006-10-06 22:36:29 +08:00
|
|
|
|
{
|
2018-09-06 20:05:21 +08:00
|
|
|
|
struct mtd_info *mtd = nand_to_mtd(chip);
|
|
|
|
|
|
2017-12-01 01:01:29 +08:00
|
|
|
|
return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
|
|
|
|
|
mtd->oobsize);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Don't use -- use nand_read_oob_std for now */
|
2018-09-06 20:05:20 +08:00
|
|
|
|
static int cafe_nand_read_oob(struct nand_chip *chip, int page)
|
2006-10-06 22:36:29 +08:00
|
|
|
|
{
|
2018-09-06 20:05:20 +08:00
|
|
|
|
struct mtd_info *mtd = nand_to_mtd(chip);
|
|
|
|
|
|
2017-12-01 01:01:29 +08:00
|
|
|
|
return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
}
|
|
|
|
|
/**
|
2011-06-24 05:12:08 +08:00
|
|
|
|
* cafe_nand_read_page_syndrome - [REPLACEABLE] hardware ecc syndrome based page read
|
2006-10-06 22:36:29 +08:00
|
|
|
|
* @chip: nand chip info structure
|
|
|
|
|
* @buf: buffer to store read data
|
2012-05-03 01:14:55 +08:00
|
|
|
|
* @oob_required: caller expects OOB data read to chip->oob_poi
|
2020-11-10 02:21:59 +08:00
|
|
|
|
* @page: page number to read
|
2006-10-06 22:36:29 +08:00
|
|
|
|
*
|
2012-05-12 04:30:34 +08:00
|
|
|
|
* The hw generator calculates the error syndrome automatically. Therefore
|
2006-10-06 22:36:29 +08:00
|
|
|
|
* we need a special oob layout and handling.
|
|
|
|
|
*/
|
2018-09-06 20:05:20 +08:00
|
|
|
|
static int cafe_nand_read_page(struct nand_chip *chip, uint8_t *buf,
|
|
|
|
|
int oob_required, int page)
|
2006-10-06 22:36:29 +08:00
|
|
|
|
{
|
2018-09-06 20:05:20 +08:00
|
|
|
|
struct mtd_info *mtd = nand_to_mtd(chip);
|
2015-12-10 16:00:41 +08:00
|
|
|
|
struct cafe_priv *cafe = nand_get_controller_data(chip);
|
2012-04-26 03:06:09 +08:00
|
|
|
|
unsigned int max_bitflips = 0;
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
2006-10-22 22:09:33 +08:00
|
|
|
|
cafe_dev_dbg(&cafe->pdev->dev, "ECC result %08x SYN1,2 %08x\n",
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_readl(cafe, NAND_ECC_RESULT),
|
|
|
|
|
cafe_readl(cafe, NAND_ECC_SYN01));
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
2017-12-01 01:01:30 +08:00
|
|
|
|
nand_read_page_op(chip, page, 0, buf, mtd->writesize);
|
2018-09-07 06:38:35 +08:00
|
|
|
|
chip->legacy.read_buf(chip, chip->oob_poi, mtd->oobsize);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
2006-10-31 12:30:11 +08:00
|
|
|
|
if (checkecc && cafe_readl(cafe, NAND_ECC_RESULT) & (1<<18)) {
|
2007-05-02 18:18:49 +08:00
|
|
|
|
unsigned short syn[8], pat[4];
|
|
|
|
|
int pos[4];
|
|
|
|
|
u8 *oob = chip->oob_poi;
|
|
|
|
|
int i, n;
|
2006-10-22 09:18:48 +08:00
|
|
|
|
|
|
|
|
|
for (i=0; i<8; i+=2) {
|
2006-10-31 12:30:11 +08:00
|
|
|
|
uint32_t tmp = cafe_readl(cafe, NAND_ECC_SYN01 + (i*2));
|
2018-04-23 00:23:53 +08:00
|
|
|
|
|
|
|
|
|
syn[i] = cafe->rs->codec->index_of[tmp & 0xfff];
|
|
|
|
|
syn[i+1] = cafe->rs->codec->index_of[(tmp >> 16) & 0xfff];
|
2007-05-02 18:18:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
n = decode_rs16(cafe->rs, NULL, NULL, 1367, syn, 0, pos, 0,
|
2018-04-23 00:23:53 +08:00
|
|
|
|
pat);
|
2007-05-02 18:18:49 +08:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
|
int p = pos[i];
|
|
|
|
|
|
|
|
|
|
/* The 12-bit symbols are mapped to bytes here */
|
|
|
|
|
|
|
|
|
|
if (p > 1374) {
|
|
|
|
|
/* out of range */
|
|
|
|
|
n = -1374;
|
|
|
|
|
} else if (p == 0) {
|
|
|
|
|
/* high four bits do not correspond to data */
|
|
|
|
|
if (pat[i] > 0xff)
|
|
|
|
|
n = -2048;
|
|
|
|
|
else
|
|
|
|
|
buf[0] ^= pat[i];
|
|
|
|
|
} else if (p == 1365) {
|
|
|
|
|
buf[2047] ^= pat[i] >> 4;
|
|
|
|
|
oob[0] ^= pat[i] << 4;
|
|
|
|
|
} else if (p > 1365) {
|
|
|
|
|
if ((p & 1) == 1) {
|
|
|
|
|
oob[3*p/2 - 2048] ^= pat[i] >> 4;
|
|
|
|
|
oob[3*p/2 - 2047] ^= pat[i] << 4;
|
|
|
|
|
} else {
|
|
|
|
|
oob[3*p/2 - 2049] ^= pat[i] >> 8;
|
|
|
|
|
oob[3*p/2 - 2048] ^= pat[i];
|
|
|
|
|
}
|
|
|
|
|
} else if ((p & 1) == 1) {
|
|
|
|
|
buf[3*p/2] ^= pat[i] >> 4;
|
|
|
|
|
buf[3*p/2 + 1] ^= pat[i] << 4;
|
|
|
|
|
} else {
|
|
|
|
|
buf[3*p/2 - 1] ^= pat[i] >> 8;
|
|
|
|
|
buf[3*p/2] ^= pat[i];
|
|
|
|
|
}
|
2006-11-30 16:17:38 +08:00
|
|
|
|
}
|
2006-10-22 09:18:48 +08:00
|
|
|
|
|
2007-05-02 18:18:49 +08:00
|
|
|
|
if (n < 0) {
|
2006-10-31 12:36:04 +08:00
|
|
|
|
dev_dbg(&cafe->pdev->dev, "Failed to correct ECC at %08x\n",
|
|
|
|
|
cafe_readl(cafe, NAND_ADDR2) * 2048);
|
2007-05-02 18:18:49 +08:00
|
|
|
|
for (i = 0; i < 0x5c; i += 4)
|
2006-10-31 12:36:04 +08:00
|
|
|
|
printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
|
2006-10-22 09:18:48 +08:00
|
|
|
|
mtd->ecc_stats.failed++;
|
|
|
|
|
} else {
|
2007-05-02 18:18:49 +08:00
|
|
|
|
dev_dbg(&cafe->pdev->dev, "Corrected %d symbol errors\n", n);
|
|
|
|
|
mtd->ecc_stats.corrected += n;
|
2012-04-26 03:06:09 +08:00
|
|
|
|
max_bitflips = max_t(unsigned int, max_bitflips, n);
|
2006-10-22 09:18:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-26 03:06:09 +08:00
|
|
|
|
return max_bitflips;
|
2006-10-06 22:36:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-04 02:59:47 +08:00
|
|
|
|
static int cafe_ooblayout_ecc(struct mtd_info *mtd, int section,
|
|
|
|
|
struct mtd_oob_region *oobregion)
|
|
|
|
|
{
|
|
|
|
|
struct nand_chip *chip = mtd_to_nand(mtd);
|
|
|
|
|
|
|
|
|
|
if (section)
|
|
|
|
|
return -ERANGE;
|
|
|
|
|
|
|
|
|
|
oobregion->offset = 0;
|
|
|
|
|
oobregion->length = chip->ecc.total;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int cafe_ooblayout_free(struct mtd_info *mtd, int section,
|
|
|
|
|
struct mtd_oob_region *oobregion)
|
|
|
|
|
{
|
|
|
|
|
struct nand_chip *chip = mtd_to_nand(mtd);
|
|
|
|
|
|
|
|
|
|
if (section)
|
|
|
|
|
return -ERANGE;
|
|
|
|
|
|
|
|
|
|
oobregion->offset = chip->ecc.total;
|
|
|
|
|
oobregion->length = mtd->oobsize - chip->ecc.total;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct mtd_ooblayout_ops cafe_ooblayout_ops = {
|
|
|
|
|
.ecc = cafe_ooblayout_ecc,
|
|
|
|
|
.free = cafe_ooblayout_free,
|
2006-10-20 09:11:40 +08:00
|
|
|
|
};
|
|
|
|
|
|
2006-11-30 16:17:38 +08:00
|
|
|
|
/* Ick. The BBT code really ought to be able to work this bit out
|
2006-10-22 22:09:33 +08:00
|
|
|
|
for itself from the above, at least for the 2KiB case */
|
|
|
|
|
static uint8_t cafe_bbt_pattern_2048[] = { 'B', 'b', 't', '0' };
|
|
|
|
|
static uint8_t cafe_mirror_pattern_2048[] = { '1', 't', 'b', 'B' };
|
|
|
|
|
|
|
|
|
|
static uint8_t cafe_bbt_pattern_512[] = { 0xBB };
|
|
|
|
|
static uint8_t cafe_mirror_pattern_512[] = { 0xBC };
|
|
|
|
|
|
2006-10-20 09:11:40 +08:00
|
|
|
|
|
|
|
|
|
static struct nand_bbt_descr cafe_bbt_main_descr_2048 = {
|
|
|
|
|
.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
|
2007-05-02 19:26:37 +08:00
|
|
|
|
| NAND_BBT_2BIT | NAND_BBT_VERSION,
|
2006-10-20 09:11:40 +08:00
|
|
|
|
.offs = 14,
|
|
|
|
|
.len = 4,
|
|
|
|
|
.veroffs = 18,
|
|
|
|
|
.maxblocks = 4,
|
2006-10-22 22:09:33 +08:00
|
|
|
|
.pattern = cafe_bbt_pattern_2048
|
2006-10-20 09:11:40 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct nand_bbt_descr cafe_bbt_mirror_descr_2048 = {
|
|
|
|
|
.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
|
2007-05-02 19:26:37 +08:00
|
|
|
|
| NAND_BBT_2BIT | NAND_BBT_VERSION,
|
2006-10-20 09:11:40 +08:00
|
|
|
|
.offs = 14,
|
|
|
|
|
.len = 4,
|
|
|
|
|
.veroffs = 18,
|
|
|
|
|
.maxblocks = 4,
|
2006-10-22 22:09:33 +08:00
|
|
|
|
.pattern = cafe_mirror_pattern_2048
|
2006-10-20 09:11:40 +08:00
|
|
|
|
};
|
|
|
|
|
|
2006-10-22 22:09:33 +08:00
|
|
|
|
static struct nand_bbt_descr cafe_bbt_main_descr_512 = {
|
|
|
|
|
.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
|
2007-05-02 19:26:37 +08:00
|
|
|
|
| NAND_BBT_2BIT | NAND_BBT_VERSION,
|
2006-10-22 22:09:33 +08:00
|
|
|
|
.offs = 14,
|
|
|
|
|
.len = 1,
|
|
|
|
|
.veroffs = 15,
|
|
|
|
|
.maxblocks = 4,
|
|
|
|
|
.pattern = cafe_bbt_pattern_512
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct nand_bbt_descr cafe_bbt_mirror_descr_512 = {
|
|
|
|
|
.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
|
2007-05-02 19:26:37 +08:00
|
|
|
|
| NAND_BBT_2BIT | NAND_BBT_VERSION,
|
2006-10-22 22:09:33 +08:00
|
|
|
|
.offs = 14,
|
|
|
|
|
.len = 1,
|
|
|
|
|
.veroffs = 15,
|
|
|
|
|
.maxblocks = 4,
|
|
|
|
|
.pattern = cafe_mirror_pattern_512
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2018-09-06 20:05:21 +08:00
|
|
|
|
static int cafe_nand_write_page_lowlevel(struct nand_chip *chip,
|
|
|
|
|
const uint8_t *buf, int oob_required,
|
|
|
|
|
int page)
|
2006-10-06 22:36:29 +08:00
|
|
|
|
{
|
2018-09-06 20:05:21 +08:00
|
|
|
|
struct mtd_info *mtd = nand_to_mtd(chip);
|
2015-12-10 16:00:41 +08:00
|
|
|
|
struct cafe_priv *cafe = nand_get_controller_data(chip);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
2017-12-01 01:01:30 +08:00
|
|
|
|
nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
|
2018-09-07 06:38:35 +08:00
|
|
|
|
chip->legacy.write_buf(chip, chip->oob_poi, mtd->oobsize);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
|
|
|
|
/* Set up ECC autogeneration */
|
2006-11-01 08:19:20 +08:00
|
|
|
|
cafe->ctl2 |= (1<<30);
|
2012-06-25 18:07:45 +08:00
|
|
|
|
|
2017-12-01 01:01:30 +08:00
|
|
|
|
return nand_prog_page_end_op(chip);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2007-05-02 18:18:49 +08:00
|
|
|
|
/* F_2[X]/(X**6+X+1) */
|
2012-11-20 02:23:07 +08:00
|
|
|
|
static unsigned short gf64_mul(u8 a, u8 b)
|
2007-05-02 18:18:49 +08:00
|
|
|
|
{
|
|
|
|
|
u8 c;
|
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
|
|
c = 0;
|
|
|
|
|
for (i = 0; i < 6; i++) {
|
|
|
|
|
if (a & 1)
|
|
|
|
|
c ^= b;
|
|
|
|
|
a >>= 1;
|
|
|
|
|
b <<= 1;
|
|
|
|
|
if ((b & 0x40) != 0)
|
|
|
|
|
b ^= 0x43;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* F_64[X]/(X**2+X+A**-1) with A the generator of F_64[X] */
|
2012-11-20 02:23:07 +08:00
|
|
|
|
static u16 gf4096_mul(u16 a, u16 b)
|
2007-05-02 18:18:49 +08:00
|
|
|
|
{
|
|
|
|
|
u8 ah, al, bh, bl, ch, cl;
|
|
|
|
|
|
|
|
|
|
ah = a >> 6;
|
|
|
|
|
al = a & 0x3f;
|
|
|
|
|
bh = b >> 6;
|
|
|
|
|
bl = b & 0x3f;
|
|
|
|
|
|
|
|
|
|
ch = gf64_mul(ah ^ al, bh ^ bl) ^ gf64_mul(al, bl);
|
|
|
|
|
cl = gf64_mul(gf64_mul(ah, bh), 0x21) ^ gf64_mul(al, bl);
|
|
|
|
|
|
|
|
|
|
return (ch << 6) ^ cl;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-20 02:23:07 +08:00
|
|
|
|
static int cafe_mul(int x)
|
2007-05-02 18:18:49 +08:00
|
|
|
|
{
|
|
|
|
|
if (x == 0)
|
|
|
|
|
return 1;
|
|
|
|
|
return gf4096_mul(x, 0xe01);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-25 21:31:37 +08:00
|
|
|
|
static int cafe_nand_attach_chip(struct nand_chip *chip)
|
|
|
|
|
{
|
|
|
|
|
struct mtd_info *mtd = nand_to_mtd(chip);
|
|
|
|
|
struct cafe_priv *cafe = nand_get_controller_data(chip);
|
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
|
|
cafe->dmabuf = dma_alloc_coherent(&cafe->pdev->dev, 2112,
|
|
|
|
|
&cafe->dmaaddr, GFP_KERNEL);
|
|
|
|
|
if (!cafe->dmabuf)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
/* Set up DMA address */
|
|
|
|
|
cafe_writel(cafe, lower_32_bits(cafe->dmaaddr), NAND_DMA_ADDR0);
|
|
|
|
|
cafe_writel(cafe, upper_32_bits(cafe->dmaaddr), NAND_DMA_ADDR1);
|
|
|
|
|
|
|
|
|
|
cafe_dev_dbg(&cafe->pdev->dev, "Set DMA address to %x (virt %p)\n",
|
|
|
|
|
cafe_readl(cafe, NAND_DMA_ADDR0), cafe->dmabuf);
|
|
|
|
|
|
|
|
|
|
/* Restore the DMA flag */
|
|
|
|
|
cafe->usedma = usedma;
|
|
|
|
|
|
|
|
|
|
cafe->ctl2 = BIT(27); /* Reed-Solomon ECC */
|
|
|
|
|
if (mtd->writesize == 2048)
|
|
|
|
|
cafe->ctl2 |= BIT(29); /* 2KiB page size */
|
|
|
|
|
|
|
|
|
|
/* Set up ECC according to the type of chip we found */
|
|
|
|
|
mtd_set_ooblayout(mtd, &cafe_ooblayout_ops);
|
|
|
|
|
if (mtd->writesize == 2048) {
|
|
|
|
|
cafe->nand.bbt_td = &cafe_bbt_main_descr_2048;
|
|
|
|
|
cafe->nand.bbt_md = &cafe_bbt_mirror_descr_2048;
|
|
|
|
|
} else if (mtd->writesize == 512) {
|
|
|
|
|
cafe->nand.bbt_td = &cafe_bbt_main_descr_512;
|
|
|
|
|
cafe->nand.bbt_md = &cafe_bbt_mirror_descr_512;
|
|
|
|
|
} else {
|
|
|
|
|
dev_warn(&cafe->pdev->dev,
|
|
|
|
|
"Unexpected NAND flash writesize %d. Aborting\n",
|
|
|
|
|
mtd->writesize);
|
|
|
|
|
err = -ENOTSUPP;
|
|
|
|
|
goto out_free_dma;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 16:51:58 +08:00
|
|
|
|
cafe->nand.ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
|
2020-08-27 16:51:57 +08:00
|
|
|
|
cafe->nand.ecc.placement = NAND_ECC_PLACEMENT_INTERLEAVED;
|
2018-07-25 21:31:37 +08:00
|
|
|
|
cafe->nand.ecc.size = mtd->writesize;
|
|
|
|
|
cafe->nand.ecc.bytes = 14;
|
|
|
|
|
cafe->nand.ecc.strength = 4;
|
|
|
|
|
cafe->nand.ecc.write_page = cafe_nand_write_page_lowlevel;
|
|
|
|
|
cafe->nand.ecc.write_oob = cafe_nand_write_oob;
|
|
|
|
|
cafe->nand.ecc.read_page = cafe_nand_read_page;
|
|
|
|
|
cafe->nand.ecc.read_oob = cafe_nand_read_oob;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
out_free_dma:
|
|
|
|
|
dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void cafe_nand_detach_chip(struct nand_chip *chip)
|
|
|
|
|
{
|
|
|
|
|
struct cafe_priv *cafe = nand_get_controller_data(chip);
|
|
|
|
|
|
|
|
|
|
dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct nand_controller_ops cafe_nand_controller_ops = {
|
|
|
|
|
.attach_chip = cafe_nand_attach_chip,
|
|
|
|
|
.detach_chip = cafe_nand_detach_chip,
|
|
|
|
|
};
|
|
|
|
|
|
2012-11-20 02:23:07 +08:00
|
|
|
|
static int cafe_nand_probe(struct pci_dev *pdev,
|
2006-10-06 22:36:29 +08:00
|
|
|
|
const struct pci_device_id *ent)
|
|
|
|
|
{
|
|
|
|
|
struct mtd_info *mtd;
|
|
|
|
|
struct cafe_priv *cafe;
|
|
|
|
|
uint32_t ctrl;
|
|
|
|
|
int err = 0;
|
|
|
|
|
|
2007-10-07 02:44:12 +08:00
|
|
|
|
/* Very old versions shared the same PCI ident for all three
|
|
|
|
|
functions on the chip. Verify the class too... */
|
|
|
|
|
if ((pdev->class >> 8) != PCI_CLASS_MEMORY_FLASH)
|
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
2006-10-06 22:36:29 +08:00
|
|
|
|
err = pci_enable_device(pdev);
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
pci_set_master(pdev);
|
|
|
|
|
|
2015-12-10 15:59:55 +08:00
|
|
|
|
cafe = kzalloc(sizeof(*cafe), GFP_KERNEL);
|
|
|
|
|
if (!cafe)
|
2006-10-06 22:36:29 +08:00
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
2015-12-10 15:59:55 +08:00
|
|
|
|
mtd = nand_to_mtd(&cafe->nand);
|
2009-04-04 22:27:45 +08:00
|
|
|
|
mtd->dev.parent = &pdev->dev;
|
2015-12-10 16:00:41 +08:00
|
|
|
|
nand_set_controller_data(&cafe->nand, cafe);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
|
|
|
|
cafe->pdev = pdev;
|
|
|
|
|
cafe->mmio = pci_iomap(pdev, 0, 0);
|
|
|
|
|
if (!cafe->mmio) {
|
|
|
|
|
dev_warn(&pdev->dev, "failed to iomap\n");
|
|
|
|
|
err = -ENOMEM;
|
|
|
|
|
goto out_free_mtd;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-02 18:18:49 +08:00
|
|
|
|
cafe->rs = init_rs_non_canonical(12, &cafe_mul, 0, 1, 8);
|
|
|
|
|
if (!cafe->rs) {
|
|
|
|
|
err = -ENOMEM;
|
|
|
|
|
goto out_ior;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-07 06:38:36 +08:00
|
|
|
|
cafe->nand.legacy.cmdfunc = cafe_nand_cmdfunc;
|
2018-09-07 06:38:37 +08:00
|
|
|
|
cafe->nand.legacy.dev_ready = cafe_device_ready;
|
2018-09-07 06:38:35 +08:00
|
|
|
|
cafe->nand.legacy.read_byte = cafe_read_byte;
|
|
|
|
|
cafe->nand.legacy.read_buf = cafe_read_buf;
|
|
|
|
|
cafe->nand.legacy.write_buf = cafe_write_buf;
|
2018-11-11 15:55:22 +08:00
|
|
|
|
cafe->nand.legacy.select_chip = cafe_select_chip;
|
2018-09-07 06:38:40 +08:00
|
|
|
|
cafe->nand.legacy.set_features = nand_get_set_features_notsupp;
|
|
|
|
|
cafe->nand.legacy.get_features = nand_get_set_features_notsupp;
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
2018-09-07 06:38:41 +08:00
|
|
|
|
cafe->nand.legacy.chip_delay = 0;
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
|
|
|
|
/* Enable the following for a flash based bad block table */
|
2011-06-01 07:31:23 +08:00
|
|
|
|
cafe->nand.bbt_options = NAND_BBT_USE_FLASH;
|
2006-10-20 09:11:40 +08:00
|
|
|
|
|
2020-05-11 14:49:16 +08:00
|
|
|
|
if (skipbbt)
|
|
|
|
|
cafe->nand.options |= NAND_SKIP_BBTSCAN | NAND_NO_BBM_QUIRK;
|
2006-11-30 16:17:38 +08:00
|
|
|
|
|
2007-01-23 15:35:27 +08:00
|
|
|
|
if (numtimings && numtimings != 3) {
|
|
|
|
|
dev_warn(&cafe->pdev->dev, "%d timing register values ignored; precisely three are required\n", numtimings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (numtimings == 3) {
|
|
|
|
|
cafe_dev_dbg(&cafe->pdev->dev, "Using provided timings (%08x %08x %08x)\n",
|
2007-03-23 18:40:04 +08:00
|
|
|
|
timing[0], timing[1], timing[2]);
|
2007-01-23 15:35:27 +08:00
|
|
|
|
} else {
|
2007-03-23 18:40:04 +08:00
|
|
|
|
timing[0] = cafe_readl(cafe, NAND_TIMING1);
|
|
|
|
|
timing[1] = cafe_readl(cafe, NAND_TIMING2);
|
|
|
|
|
timing[2] = cafe_readl(cafe, NAND_TIMING3);
|
2007-01-23 15:35:27 +08:00
|
|
|
|
|
2007-03-23 18:40:04 +08:00
|
|
|
|
if (timing[0] | timing[1] | timing[2]) {
|
|
|
|
|
cafe_dev_dbg(&cafe->pdev->dev, "Timing registers already set (%08x %08x %08x)\n",
|
|
|
|
|
timing[0], timing[1], timing[2]);
|
2007-01-23 15:35:27 +08:00
|
|
|
|
} else {
|
|
|
|
|
dev_warn(&cafe->pdev->dev, "Timing registers unset; using most conservative defaults\n");
|
2007-03-23 18:40:04 +08:00
|
|
|
|
timing[0] = timing[1] = timing[2] = 0xffffffff;
|
2007-01-23 15:35:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-27 14:55:34 +08:00
|
|
|
|
/* Start off by resetting the NAND controller completely */
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, 1, NAND_RESET);
|
|
|
|
|
cafe_writel(cafe, 0, NAND_RESET);
|
2006-10-27 14:55:34 +08:00
|
|
|
|
|
2007-03-23 18:40:04 +08:00
|
|
|
|
cafe_writel(cafe, timing[0], NAND_TIMING1);
|
|
|
|
|
cafe_writel(cafe, timing[1], NAND_TIMING2);
|
|
|
|
|
cafe_writel(cafe, timing[2], NAND_TIMING3);
|
2006-10-27 19:50:04 +08:00
|
|
|
|
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK);
|
2007-02-14 16:33:20 +08:00
|
|
|
|
err = request_irq(pdev->irq, &cafe_nand_interrupt, IRQF_SHARED,
|
|
|
|
|
"CAFE NAND", mtd);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
if (err) {
|
|
|
|
|
dev_warn(&pdev->dev, "Could not register IRQ %d\n", pdev->irq);
|
2021-08-21 15:58:45 +08:00
|
|
|
|
goto out_free_rs;
|
2006-10-06 22:36:29 +08:00
|
|
|
|
}
|
2007-01-23 15:44:10 +08:00
|
|
|
|
|
2006-10-06 22:36:29 +08:00
|
|
|
|
/* Disable master reset, enable NAND clock */
|
2006-10-31 12:30:11 +08:00
|
|
|
|
ctrl = cafe_readl(cafe, GLOBAL_CTRL);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
ctrl &= 0xffffeff0;
|
|
|
|
|
ctrl |= 0x00007000;
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, ctrl | 0x05, GLOBAL_CTRL);
|
|
|
|
|
cafe_writel(cafe, ctrl | 0x0a, GLOBAL_CTRL);
|
|
|
|
|
cafe_writel(cafe, 0, NAND_DMA_CTRL);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, 0x7006, GLOBAL_CTRL);
|
|
|
|
|
cafe_writel(cafe, 0x700a, GLOBAL_CTRL);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
2014-01-13 14:27:12 +08:00
|
|
|
|
/* Enable NAND IRQ in global IRQ mask register */
|
|
|
|
|
cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK);
|
|
|
|
|
cafe_dev_dbg(&cafe->pdev->dev, "Control %x, IRQ mask %x\n",
|
|
|
|
|
cafe_readl(cafe, GLOBAL_CTRL),
|
|
|
|
|
cafe_readl(cafe, GLOBAL_IRQ_MASK));
|
|
|
|
|
|
2018-07-25 21:31:37 +08:00
|
|
|
|
/* Do not use the DMA during the NAND identification */
|
|
|
|
|
cafe->usedma = 0;
|
2014-01-13 14:27:12 +08:00
|
|
|
|
|
|
|
|
|
/* Scan to find existence of the device */
|
2018-11-20 17:02:39 +08:00
|
|
|
|
cafe->nand.legacy.dummy_controller.ops = &cafe_nand_controller_ops;
|
2018-09-06 20:05:14 +08:00
|
|
|
|
err = nand_scan(&cafe->nand, 2);
|
2016-11-04 18:43:06 +08:00
|
|
|
|
if (err)
|
2014-01-13 14:27:12 +08:00
|
|
|
|
goto out_irq;
|
|
|
|
|
|
2006-10-06 22:36:29 +08:00
|
|
|
|
pci_set_drvdata(pdev, mtd);
|
2007-10-29 09:56:39 +08:00
|
|
|
|
|
2008-10-09 07:08:20 +08:00
|
|
|
|
mtd->name = "cafe_nand";
|
2018-03-21 21:01:43 +08:00
|
|
|
|
err = mtd_device_parse_register(mtd, part_probes, NULL, NULL, 0);
|
|
|
|
|
if (err)
|
|
|
|
|
goto out_cleanup_nand;
|
2011-06-02 22:00:29 +08:00
|
|
|
|
|
2006-10-06 22:36:29 +08:00
|
|
|
|
goto out;
|
|
|
|
|
|
2018-03-21 21:01:43 +08:00
|
|
|
|
out_cleanup_nand:
|
|
|
|
|
nand_cleanup(&cafe->nand);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
out_irq:
|
|
|
|
|
/* Disable NAND IRQ in global IRQ mask register */
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
free_irq(pdev->irq, mtd);
|
2021-08-21 15:58:45 +08:00
|
|
|
|
out_free_rs:
|
|
|
|
|
free_rs(cafe->rs);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
out_ior:
|
|
|
|
|
pci_iounmap(pdev, cafe->mmio);
|
|
|
|
|
out_free_mtd:
|
2015-12-10 15:59:55 +08:00
|
|
|
|
kfree(cafe);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
out:
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-20 02:26:04 +08:00
|
|
|
|
static void cafe_nand_remove(struct pci_dev *pdev)
|
2006-10-06 22:36:29 +08:00
|
|
|
|
{
|
|
|
|
|
struct mtd_info *mtd = pci_get_drvdata(pdev);
|
2015-12-01 19:03:04 +08:00
|
|
|
|
struct nand_chip *chip = mtd_to_nand(mtd);
|
2015-12-10 16:00:41 +08:00
|
|
|
|
struct cafe_priv *cafe = nand_get_controller_data(chip);
|
2020-05-19 20:59:39 +08:00
|
|
|
|
int ret;
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
|
|
|
|
/* Disable NAND IRQ in global IRQ mask register */
|
2006-10-31 12:30:11 +08:00
|
|
|
|
cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
free_irq(pdev->irq, mtd);
|
2020-05-19 20:59:39 +08:00
|
|
|
|
ret = mtd_device_unregister(mtd);
|
|
|
|
|
WARN_ON(ret);
|
|
|
|
|
nand_cleanup(chip);
|
2007-05-02 18:18:49 +08:00
|
|
|
|
free_rs(cafe->rs);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
pci_iounmap(pdev, cafe->mmio);
|
2017-12-05 16:47:14 +08:00
|
|
|
|
dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
|
2015-12-10 15:59:55 +08:00
|
|
|
|
kfree(cafe);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-09 22:10:34 +08:00
|
|
|
|
static const struct pci_device_id cafe_nand_tbl[] = {
|
2008-09-03 16:47:17 +08:00
|
|
|
|
{ PCI_VENDOR_ID_MARVELL, PCI_DEVICE_ID_MARVELL_88ALP01_NAND,
|
|
|
|
|
PCI_ANY_ID, PCI_ANY_ID },
|
2007-10-07 02:44:12 +08:00
|
|
|
|
{ }
|
2006-10-06 22:36:29 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(pci, cafe_nand_tbl);
|
|
|
|
|
|
2007-10-07 02:59:32 +08:00
|
|
|
|
static int cafe_nand_resume(struct pci_dev *pdev)
|
|
|
|
|
{
|
|
|
|
|
uint32_t ctrl;
|
|
|
|
|
struct mtd_info *mtd = pci_get_drvdata(pdev);
|
2015-12-01 19:03:04 +08:00
|
|
|
|
struct nand_chip *chip = mtd_to_nand(mtd);
|
2015-12-10 16:00:41 +08:00
|
|
|
|
struct cafe_priv *cafe = nand_get_controller_data(chip);
|
2007-10-07 02:59:32 +08:00
|
|
|
|
|
|
|
|
|
/* Start off by resetting the NAND controller completely */
|
|
|
|
|
cafe_writel(cafe, 1, NAND_RESET);
|
|
|
|
|
cafe_writel(cafe, 0, NAND_RESET);
|
|
|
|
|
cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK);
|
|
|
|
|
|
|
|
|
|
/* Restore timing configuration */
|
|
|
|
|
cafe_writel(cafe, timing[0], NAND_TIMING1);
|
|
|
|
|
cafe_writel(cafe, timing[1], NAND_TIMING2);
|
|
|
|
|
cafe_writel(cafe, timing[2], NAND_TIMING3);
|
|
|
|
|
|
|
|
|
|
/* Disable master reset, enable NAND clock */
|
|
|
|
|
ctrl = cafe_readl(cafe, GLOBAL_CTRL);
|
|
|
|
|
ctrl &= 0xffffeff0;
|
|
|
|
|
ctrl |= 0x00007000;
|
|
|
|
|
cafe_writel(cafe, ctrl | 0x05, GLOBAL_CTRL);
|
|
|
|
|
cafe_writel(cafe, ctrl | 0x0a, GLOBAL_CTRL);
|
|
|
|
|
cafe_writel(cafe, 0, NAND_DMA_CTRL);
|
|
|
|
|
cafe_writel(cafe, 0x7006, GLOBAL_CTRL);
|
|
|
|
|
cafe_writel(cafe, 0x700a, GLOBAL_CTRL);
|
|
|
|
|
|
|
|
|
|
/* Set up DMA address */
|
|
|
|
|
cafe_writel(cafe, cafe->dmaaddr & 0xffffffff, NAND_DMA_ADDR0);
|
|
|
|
|
if (sizeof(cafe->dmaaddr) > 4)
|
|
|
|
|
/* Shift in two parts to shut the compiler up */
|
|
|
|
|
cafe_writel(cafe, (cafe->dmaaddr >> 16) >> 16, NAND_DMA_ADDR1);
|
|
|
|
|
else
|
|
|
|
|
cafe_writel(cafe, 0, NAND_DMA_ADDR1);
|
|
|
|
|
|
|
|
|
|
/* Enable NAND IRQ in global IRQ mask register */
|
|
|
|
|
cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-06 22:36:29 +08:00
|
|
|
|
static struct pci_driver cafe_nand_pci_driver = {
|
|
|
|
|
.name = "CAFÉ NAND",
|
|
|
|
|
.id_table = cafe_nand_tbl,
|
|
|
|
|
.probe = cafe_nand_probe,
|
2012-11-20 02:21:24 +08:00
|
|
|
|
.remove = cafe_nand_remove,
|
2006-10-06 22:36:29 +08:00
|
|
|
|
.resume = cafe_nand_resume,
|
|
|
|
|
};
|
|
|
|
|
|
2012-04-03 09:59:44 +08:00
|
|
|
|
module_pci_driver(cafe_nand_pci_driver);
|
2006-10-06 22:36:29 +08:00
|
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
|
2007-01-23 15:44:10 +08:00
|
|
|
|
MODULE_DESCRIPTION("NAND flash driver for OLPC CAFÉ chip");
|