blob: 11a56df89eab3831f747dfd322bfcacd62e526bc [file] [log] [blame]
David Woodhousec9ac5972006-11-30 08:17:38 +00001/*
David Woodhousefbad5692006-10-22 15:09:33 +01002 * Driver for One Laptop Per Child ‘CAFÉ’ controller, aka Marvell 88ALP01
David Woodhouse5467fb02006-10-06 15:36:29 +01003 *
David Woodhouse514fca42008-09-03 09:47:17 +01004 * The data sheet for this device can be found at:
Justin P. Mattock631dd1a2010-10-18 11:03:14 +02005 * http://wiki.laptop.org/go/Datasheets
David Woodhouse514fca42008-09-03 09:47:17 +01006 *
David Woodhouse5467fb02006-10-06 15:36:29 +01007 * Copyright © 2006 Red Hat, Inc.
8 * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
9 */
10
David Woodhouse8dd851d2006-10-20 02:11:40 +010011#define DEBUG
David Woodhouse5467fb02006-10-06 15:36:29 +010012
13#include <linux/device.h>
14#undef DEBUG
15#include <linux/mtd/mtd.h>
16#include <linux/mtd/nand.h>
David Woodhouse9c37f332007-10-28 21:56:39 -040017#include <linux/mtd/partitions.h>
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +020018#include <linux/rslib.h>
David Woodhouse5467fb02006-10-06 15:36:29 +010019#include <linux/pci.h>
20#include <linux/delay.h>
21#include <linux/interrupt.h>
Al Viroa1274302007-01-30 13:23:30 +000022#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
David Woodhouse5467fb02006-10-06 15:36:29 +010024#include <asm/io.h>
25
26#define CAFE_NAND_CTRL1 0x00
27#define CAFE_NAND_CTRL2 0x04
28#define CAFE_NAND_CTRL3 0x08
29#define CAFE_NAND_STATUS 0x0c
30#define CAFE_NAND_IRQ 0x10
31#define CAFE_NAND_IRQ_MASK 0x14
32#define CAFE_NAND_DATA_LEN 0x18
33#define CAFE_NAND_ADDR1 0x1c
34#define CAFE_NAND_ADDR2 0x20
35#define CAFE_NAND_TIMING1 0x24
36#define CAFE_NAND_TIMING2 0x28
37#define CAFE_NAND_TIMING3 0x2c
38#define CAFE_NAND_NONMEM 0x30
David Woodhouse04459d72006-10-22 02:18:48 +010039#define CAFE_NAND_ECC_RESULT 0x3C
David Woodhousefbad5692006-10-22 15:09:33 +010040#define CAFE_NAND_DMA_CTRL 0x40
41#define CAFE_NAND_DMA_ADDR0 0x44
42#define CAFE_NAND_DMA_ADDR1 0x48
David Woodhouse04459d72006-10-22 02:18:48 +010043#define CAFE_NAND_ECC_SYN01 0x50
44#define CAFE_NAND_ECC_SYN23 0x54
45#define CAFE_NAND_ECC_SYN45 0x58
46#define CAFE_NAND_ECC_SYN67 0x5c
David Woodhouse5467fb02006-10-06 15:36:29 +010047#define CAFE_NAND_READ_DATA 0x1000
48#define CAFE_NAND_WRITE_DATA 0x2000
49
David Woodhouse195a2532006-10-31 12:30:11 +080050#define CAFE_GLOBAL_CTRL 0x3004
51#define CAFE_GLOBAL_IRQ 0x3008
52#define CAFE_GLOBAL_IRQ_MASK 0x300c
53#define CAFE_NAND_RESET 0x3034
54
David Woodhouse048c37b2007-05-02 12:26:37 +010055/* Missing from the datasheet: bit 19 of CTRL1 sets CE0 vs. CE1 */
56#define CTRL1_CHIPSELECT (1<<19)
57
David Woodhouse5467fb02006-10-06 15:36:29 +010058struct cafe_priv {
59 struct nand_chip nand;
60 struct pci_dev *pdev;
61 void __iomem *mmio;
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +020062 struct rs_control *rs;
David Woodhouse5467fb02006-10-06 15:36:29 +010063 uint32_t ctl1;
64 uint32_t ctl2;
65 int datalen;
66 int nr_data;
67 int data_pos;
68 int page_addr;
69 dma_addr_t dmaaddr;
70 unsigned char *dmabuf;
David Woodhouse5467fb02006-10-06 15:36:29 +010071};
72
David Woodhouseb478c772006-10-27 14:50:04 +030073static int usedma = 1;
David Woodhouse5467fb02006-10-06 15:36:29 +010074module_param(usedma, int, 0644);
75
David Woodhouse8dd851d2006-10-20 02:11:40 +010076static int skipbbt = 0;
77module_param(skipbbt, int, 0644);
78
79static int debug = 0;
80module_param(debug, int, 0644);
81
David Woodhousebe8444b2006-10-31 12:36:04 +080082static int regdebug = 0;
83module_param(regdebug, int, 0644);
84
David Woodhouseb478c772006-10-27 14:50:04 +030085static int checkecc = 1;
David Woodhouse470b0a92006-10-23 14:29:04 +010086module_param(checkecc, int, 0644);
87
Al Viro64a6f952007-10-14 19:35:30 +010088static unsigned int numtimings;
David Woodhouse527a4f42007-01-23 15:35:27 +080089static int timing[3];
90module_param_array(timing, int, &numtimings, 0644);
David Woodhouseb478c772006-10-27 14:50:04 +030091
Philip Rakity68874412008-10-08 16:08:20 -070092static const char *part_probes[] = { "cmdlinepart", "RedBoot", NULL };
David Woodhouse9c37f332007-10-28 21:56:39 -040093
David Woodhouse04459d72006-10-22 02:18:48 +010094/* Hrm. Why isn't this already conditional on something in the struct device? */
David Woodhouse8dd851d2006-10-20 02:11:40 +010095#define cafe_dev_dbg(dev, args...) do { if (debug) dev_dbg(dev, ##args); } while(0)
96
David Woodhouse195a2532006-10-31 12:30:11 +080097/* Make it easier to switch to PIO if we need to */
98#define cafe_readl(cafe, addr) readl((cafe)->mmio + CAFE_##addr)
99#define cafe_writel(cafe, datum, addr) writel(datum, (cafe)->mmio + CAFE_##addr)
David Woodhouse8dd851d2006-10-20 02:11:40 +0100100
David Woodhouse5467fb02006-10-06 15:36:29 +0100101static int cafe_device_ready(struct mtd_info *mtd)
102{
103 struct cafe_priv *cafe = mtd->priv;
David Woodhouse195a2532006-10-31 12:30:11 +0800104 int result = !!(cafe_readl(cafe, NAND_STATUS) | 0x40000000);
105 uint32_t irqs = cafe_readl(cafe, NAND_IRQ);
David Woodhousefbad5692006-10-22 15:09:33 +0100106
David Woodhouse195a2532006-10-31 12:30:11 +0800107 cafe_writel(cafe, irqs, NAND_IRQ);
David Woodhousefbad5692006-10-22 15:09:33 +0100108
David Woodhouse8dd851d2006-10-20 02:11:40 +0100109 cafe_dev_dbg(&cafe->pdev->dev, "NAND device is%s ready, IRQ %x (%x) (%x,%x)\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800110 result?"":" not", irqs, cafe_readl(cafe, NAND_IRQ),
111 cafe_readl(cafe, GLOBAL_IRQ), cafe_readl(cafe, GLOBAL_IRQ_MASK));
David Woodhousefbad5692006-10-22 15:09:33 +0100112
David Woodhouse5467fb02006-10-06 15:36:29 +0100113 return result;
114}
115
116
117static void cafe_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
118{
119 struct cafe_priv *cafe = mtd->priv;
120
121 if (usedma)
122 memcpy(cafe->dmabuf + cafe->datalen, buf, len);
123 else
124 memcpy_toio(cafe->mmio + CAFE_NAND_WRITE_DATA + cafe->datalen, buf, len);
David Woodhousefbad5692006-10-22 15:09:33 +0100125
David Woodhouse5467fb02006-10-06 15:36:29 +0100126 cafe->datalen += len;
127
David Woodhouse8dd851d2006-10-20 02:11:40 +0100128 cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes to write buffer. datalen 0x%x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100129 len, cafe->datalen);
130}
131
132static void cafe_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
133{
134 struct cafe_priv *cafe = mtd->priv;
135
136 if (usedma)
137 memcpy(buf, cafe->dmabuf + cafe->datalen, len);
138 else
139 memcpy_fromio(buf, cafe->mmio + CAFE_NAND_READ_DATA + cafe->datalen, len);
140
David Woodhouse8dd851d2006-10-20 02:11:40 +0100141 cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes from position 0x%x in read buffer.\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100142 len, cafe->datalen);
143 cafe->datalen += len;
144}
145
146static uint8_t cafe_read_byte(struct mtd_info *mtd)
147{
148 struct cafe_priv *cafe = mtd->priv;
149 uint8_t d;
150
151 cafe_read_buf(mtd, &d, 1);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100152 cafe_dev_dbg(&cafe->pdev->dev, "Read %02x\n", d);
David Woodhouse5467fb02006-10-06 15:36:29 +0100153
154 return d;
155}
156
157static void cafe_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
158 int column, int page_addr)
159{
160 struct cafe_priv *cafe = mtd->priv;
161 int adrbytes = 0;
162 uint32_t ctl1;
163 uint32_t doneint = 0x80000000;
David Woodhouse5467fb02006-10-06 15:36:29 +0100164
David Woodhouse8dd851d2006-10-20 02:11:40 +0100165 cafe_dev_dbg(&cafe->pdev->dev, "cmdfunc %02x, 0x%x, 0x%x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100166 command, column, page_addr);
167
168 if (command == NAND_CMD_ERASE2 || command == NAND_CMD_PAGEPROG) {
169 /* Second half of a command we already calculated */
David Woodhouse195a2532006-10-31 12:30:11 +0800170 cafe_writel(cafe, cafe->ctl2 | 0x100 | command, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100171 ctl1 = cafe->ctl1;
David Woodhousecad40652006-11-01 08:19:20 +0800172 cafe->ctl2 &= ~(1<<30);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100173 cafe_dev_dbg(&cafe->pdev->dev, "Continue command, ctl1 %08x, #data %d\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100174 cafe->ctl1, cafe->nr_data);
175 goto do_command;
176 }
177 /* Reset ECC engine */
David Woodhouse195a2532006-10-31 12:30:11 +0800178 cafe_writel(cafe, 0, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100179
180 /* Emulate NAND_CMD_READOOB on large-page chips */
181 if (mtd->writesize > 512 &&
182 command == NAND_CMD_READOOB) {
183 column += mtd->writesize;
184 command = NAND_CMD_READ0;
185 }
186
187 /* FIXME: Do we need to send read command before sending data
188 for small-page chips, to position the buffer correctly? */
189
190 if (column != -1) {
David Woodhouse195a2532006-10-31 12:30:11 +0800191 cafe_writel(cafe, column, NAND_ADDR1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100192 adrbytes = 2;
193 if (page_addr != -1)
194 goto write_adr2;
195 } else if (page_addr != -1) {
David Woodhouse195a2532006-10-31 12:30:11 +0800196 cafe_writel(cafe, page_addr & 0xffff, NAND_ADDR1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100197 page_addr >>= 16;
198 write_adr2:
David Woodhouse195a2532006-10-31 12:30:11 +0800199 cafe_writel(cafe, page_addr, NAND_ADDR2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100200 adrbytes += 2;
201 if (mtd->size > mtd->writesize << 16)
202 adrbytes++;
203 }
204
205 cafe->data_pos = cafe->datalen = 0;
206
David Woodhouse048c37b2007-05-02 12:26:37 +0100207 /* Set command valid bit, mask in the chip select bit */
208 ctl1 = 0x80000000 | command | (cafe->ctl1 & CTRL1_CHIPSELECT);
David Woodhouse5467fb02006-10-06 15:36:29 +0100209
210 /* Set RD or WR bits as appropriate */
211 if (command == NAND_CMD_READID || command == NAND_CMD_STATUS) {
212 ctl1 |= (1<<26); /* rd */
213 /* Always 5 bytes, for now */
David Woodhouse8dd851d2006-10-20 02:11:40 +0100214 cafe->datalen = 4;
David Woodhouse5467fb02006-10-06 15:36:29 +0100215 /* And one address cycle -- even for STATUS, since the controller doesn't work without */
216 adrbytes = 1;
217 } else if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
218 command == NAND_CMD_READOOB || command == NAND_CMD_RNDOUT) {
219 ctl1 |= 1<<26; /* rd */
220 /* For now, assume just read to end of page */
221 cafe->datalen = mtd->writesize + mtd->oobsize - column;
222 } else if (command == NAND_CMD_SEQIN)
223 ctl1 |= 1<<25; /* wr */
224
225 /* Set number of address bytes */
226 if (adrbytes)
227 ctl1 |= ((adrbytes-1)|8) << 27;
228
229 if (command == NAND_CMD_SEQIN || command == NAND_CMD_ERASE1) {
David Woodhousec9ac5972006-11-30 08:17:38 +0000230 /* Ignore the first command of a pair; the hardware
David Woodhouse5467fb02006-10-06 15:36:29 +0100231 deals with them both at once, later */
232 cafe->ctl1 = ctl1;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100233 cafe_dev_dbg(&cafe->pdev->dev, "Setup for delayed command, ctl1 %08x, dlen %x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100234 cafe->ctl1, cafe->datalen);
235 return;
236 }
237 /* RNDOUT and READ0 commands need a following byte */
238 if (command == NAND_CMD_RNDOUT)
David Woodhouse195a2532006-10-31 12:30:11 +0800239 cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_RNDOUTSTART, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100240 else if (command == NAND_CMD_READ0 && mtd->writesize > 512)
David Woodhouse195a2532006-10-31 12:30:11 +0800241 cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_READSTART, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100242
243 do_command:
David Woodhousec9ac5972006-11-30 08:17:38 +0000244 cafe_dev_dbg(&cafe->pdev->dev, "dlen %x, ctl1 %x, ctl2 %x\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800245 cafe->datalen, ctl1, cafe_readl(cafe, NAND_CTRL2));
David Woodhousefbad5692006-10-22 15:09:33 +0100246
David Woodhouse5467fb02006-10-06 15:36:29 +0100247 /* NB: The datasheet lies -- we really should be subtracting 1 here */
David Woodhouse195a2532006-10-31 12:30:11 +0800248 cafe_writel(cafe, cafe->datalen, NAND_DATA_LEN);
249 cafe_writel(cafe, 0x90000000, NAND_IRQ);
David Woodhouse5467fb02006-10-06 15:36:29 +0100250 if (usedma && (ctl1 & (3<<25))) {
251 uint32_t dmactl = 0xc0000000 + cafe->datalen;
252 /* If WR or RD bits set, set up DMA */
253 if (ctl1 & (1<<26)) {
254 /* It's a read */
255 dmactl |= (1<<29);
256 /* ... so it's done when the DMA is done, not just
257 the command. */
258 doneint = 0x10000000;
259 }
David Woodhouse195a2532006-10-31 12:30:11 +0800260 cafe_writel(cafe, dmactl, NAND_DMA_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100261 }
David Woodhouse5467fb02006-10-06 15:36:29 +0100262 cafe->datalen = 0;
263
David Woodhousebe8444b2006-10-31 12:36:04 +0800264 if (unlikely(regdebug)) {
265 int i;
266 printk("About to write command %08x to register 0\n", ctl1);
267 for (i=4; i< 0x5c; i+=4)
268 printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
David Woodhousefbad5692006-10-22 15:09:33 +0100269 }
David Woodhousebe8444b2006-10-31 12:36:04 +0800270
David Woodhouse195a2532006-10-31 12:30:11 +0800271 cafe_writel(cafe, ctl1, NAND_CTRL1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100272 /* Apply this short delay always to ensure that we do wait tWB in
273 * any case on any machine. */
274 ndelay(100);
275
276 if (1) {
Andrew Morton2a7295b2007-02-17 16:02:11 -0800277 int c;
David Woodhouse5467fb02006-10-06 15:36:29 +0100278 uint32_t irqs;
279
Andrew Morton2a7295b2007-02-17 16:02:11 -0800280 for (c = 500000; c != 0; c--) {
David Woodhouse195a2532006-10-31 12:30:11 +0800281 irqs = cafe_readl(cafe, NAND_IRQ);
David Woodhouse5467fb02006-10-06 15:36:29 +0100282 if (irqs & doneint)
283 break;
284 udelay(1);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100285 if (!(c % 100000))
286 cafe_dev_dbg(&cafe->pdev->dev, "Wait for ready, IRQ %x\n", irqs);
David Woodhouse5467fb02006-10-06 15:36:29 +0100287 cpu_relax();
288 }
David Woodhouse195a2532006-10-31 12:30:11 +0800289 cafe_writel(cafe, doneint, NAND_IRQ);
David Woodhousea0207272006-10-28 17:08:38 +0300290 cafe_dev_dbg(&cafe->pdev->dev, "Command %x completed after %d usec, irqs %x (%x)\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800291 command, 500000-c, irqs, cafe_readl(cafe, NAND_IRQ));
David Woodhouse5467fb02006-10-06 15:36:29 +0100292 }
293
David Woodhousecad40652006-11-01 08:19:20 +0800294 WARN_ON(cafe->ctl2 & (1<<30));
David Woodhouse5467fb02006-10-06 15:36:29 +0100295
296 switch (command) {
297
298 case NAND_CMD_CACHEDPROG:
299 case NAND_CMD_PAGEPROG:
300 case NAND_CMD_ERASE1:
301 case NAND_CMD_ERASE2:
302 case NAND_CMD_SEQIN:
303 case NAND_CMD_RNDIN:
304 case NAND_CMD_STATUS:
305 case NAND_CMD_DEPLETE1:
306 case NAND_CMD_RNDOUT:
307 case NAND_CMD_STATUS_ERROR:
308 case NAND_CMD_STATUS_ERROR0:
309 case NAND_CMD_STATUS_ERROR1:
310 case NAND_CMD_STATUS_ERROR2:
311 case NAND_CMD_STATUS_ERROR3:
David Woodhouse195a2532006-10-31 12:30:11 +0800312 cafe_writel(cafe, cafe->ctl2, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100313 return;
314 }
315 nand_wait_ready(mtd);
David Woodhouse195a2532006-10-31 12:30:11 +0800316 cafe_writel(cafe, cafe->ctl2, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100317}
318
319static void cafe_select_chip(struct mtd_info *mtd, int chipnr)
320{
David Woodhouse048c37b2007-05-02 12:26:37 +0100321 struct cafe_priv *cafe = mtd->priv;
322
323 cafe_dev_dbg(&cafe->pdev->dev, "select_chip %d\n", chipnr);
324
325 /* Mask the appropriate bit into the stored value of ctl1
326 which will be used by cafe_nand_cmdfunc() */
327 if (chipnr)
328 cafe->ctl1 |= CTRL1_CHIPSELECT;
329 else
330 cafe->ctl1 &= ~CTRL1_CHIPSELECT;
David Woodhouse5467fb02006-10-06 15:36:29 +0100331}
David Woodhousefbad5692006-10-22 15:09:33 +0100332
Alan Cox67cd7242009-04-22 15:02:23 +0100333static irqreturn_t cafe_nand_interrupt(int irq, void *id)
David Woodhouse5467fb02006-10-06 15:36:29 +0100334{
335 struct mtd_info *mtd = id;
336 struct cafe_priv *cafe = mtd->priv;
David Woodhouse195a2532006-10-31 12:30:11 +0800337 uint32_t irqs = cafe_readl(cafe, NAND_IRQ);
338 cafe_writel(cafe, irqs & ~0x90000000, NAND_IRQ);
David Woodhouse5467fb02006-10-06 15:36:29 +0100339 if (!irqs)
340 return IRQ_NONE;
341
David Woodhouse195a2532006-10-31 12:30:11 +0800342 cafe_dev_dbg(&cafe->pdev->dev, "irq, bits %x (%x)\n", irqs, cafe_readl(cafe, NAND_IRQ));
David Woodhouse5467fb02006-10-06 15:36:29 +0100343 return IRQ_HANDLED;
344}
345
346static void cafe_nand_bug(struct mtd_info *mtd)
347{
348 BUG();
349}
350
351static int cafe_nand_write_oob(struct mtd_info *mtd,
352 struct nand_chip *chip, int page)
353{
354 int status = 0;
355
David Woodhouse5467fb02006-10-06 15:36:29 +0100356 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
357 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
358 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
359 status = chip->waitfunc(mtd, chip);
360
361 return status & NAND_STATUS_FAIL ? -EIO : 0;
362}
363
364/* Don't use -- use nand_read_oob_std for now */
365static int cafe_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
366 int page, int sndcmd)
367{
368 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
369 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
370 return 1;
371}
372/**
Brian Norris7854d3f2011-06-23 14:12:08 -0700373 * cafe_nand_read_page_syndrome - [REPLACEABLE] hardware ecc syndrome based page read
David Woodhouse5467fb02006-10-06 15:36:29 +0100374 * @mtd: mtd info structure
375 * @chip: nand chip info structure
376 * @buf: buffer to store read data
377 *
378 * The hw generator calculates the error syndrome automatically. Therefor
379 * we need a special oob layout and handling.
380 */
381static int cafe_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700382 uint8_t *buf, int page)
David Woodhouse5467fb02006-10-06 15:36:29 +0100383{
384 struct cafe_priv *cafe = mtd->priv;
385
David Woodhousefbad5692006-10-22 15:09:33 +0100386 cafe_dev_dbg(&cafe->pdev->dev, "ECC result %08x SYN1,2 %08x\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800387 cafe_readl(cafe, NAND_ECC_RESULT),
388 cafe_readl(cafe, NAND_ECC_SYN01));
David Woodhouse5467fb02006-10-06 15:36:29 +0100389
390 chip->read_buf(mtd, buf, mtd->writesize);
391 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
392
David Woodhouse195a2532006-10-31 12:30:11 +0800393 if (checkecc && cafe_readl(cafe, NAND_ECC_RESULT) & (1<<18)) {
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200394 unsigned short syn[8], pat[4];
395 int pos[4];
396 u8 *oob = chip->oob_poi;
397 int i, n;
David Woodhouse04459d72006-10-22 02:18:48 +0100398
399 for (i=0; i<8; i+=2) {
David Woodhouse195a2532006-10-31 12:30:11 +0800400 uint32_t tmp = cafe_readl(cafe, NAND_ECC_SYN01 + (i*2));
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200401 syn[i] = cafe->rs->index_of[tmp & 0xfff];
402 syn[i+1] = cafe->rs->index_of[(tmp >> 16) & 0xfff];
David Woodhousec9ac5972006-11-30 08:17:38 +0000403 }
David Woodhouse04459d72006-10-22 02:18:48 +0100404
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200405 n = decode_rs16(cafe->rs, NULL, NULL, 1367, syn, 0, pos, 0,
406 pat);
407
408 for (i = 0; i < n; i++) {
409 int p = pos[i];
410
411 /* The 12-bit symbols are mapped to bytes here */
412
413 if (p > 1374) {
414 /* out of range */
415 n = -1374;
416 } else if (p == 0) {
417 /* high four bits do not correspond to data */
418 if (pat[i] > 0xff)
419 n = -2048;
420 else
421 buf[0] ^= pat[i];
422 } else if (p == 1365) {
423 buf[2047] ^= pat[i] >> 4;
424 oob[0] ^= pat[i] << 4;
425 } else if (p > 1365) {
426 if ((p & 1) == 1) {
427 oob[3*p/2 - 2048] ^= pat[i] >> 4;
428 oob[3*p/2 - 2047] ^= pat[i] << 4;
429 } else {
430 oob[3*p/2 - 2049] ^= pat[i] >> 8;
431 oob[3*p/2 - 2048] ^= pat[i];
432 }
433 } else if ((p & 1) == 1) {
434 buf[3*p/2] ^= pat[i] >> 4;
435 buf[3*p/2 + 1] ^= pat[i] << 4;
436 } else {
437 buf[3*p/2 - 1] ^= pat[i] >> 8;
438 buf[3*p/2] ^= pat[i];
439 }
440 }
441
442 if (n < 0) {
David Woodhousebe8444b2006-10-31 12:36:04 +0800443 dev_dbg(&cafe->pdev->dev, "Failed to correct ECC at %08x\n",
444 cafe_readl(cafe, NAND_ADDR2) * 2048);
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200445 for (i = 0; i < 0x5c; i += 4)
David Woodhousebe8444b2006-10-31 12:36:04 +0800446 printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
David Woodhouse04459d72006-10-22 02:18:48 +0100447 mtd->ecc_stats.failed++;
448 } else {
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200449 dev_dbg(&cafe->pdev->dev, "Corrected %d symbol errors\n", n);
450 mtd->ecc_stats.corrected += n;
David Woodhouse04459d72006-10-22 02:18:48 +0100451 }
452 }
453
David Woodhouse5467fb02006-10-06 15:36:29 +0100454 return 0;
455}
456
David Woodhouse8dd851d2006-10-20 02:11:40 +0100457static struct nand_ecclayout cafe_oobinfo_2048 = {
458 .eccbytes = 14,
459 .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
460 .oobfree = {{14, 50}}
461};
462
David Woodhousec9ac5972006-11-30 08:17:38 +0000463/* Ick. The BBT code really ought to be able to work this bit out
David Woodhousefbad5692006-10-22 15:09:33 +0100464 for itself from the above, at least for the 2KiB case */
465static uint8_t cafe_bbt_pattern_2048[] = { 'B', 'b', 't', '0' };
466static uint8_t cafe_mirror_pattern_2048[] = { '1', 't', 'b', 'B' };
467
468static uint8_t cafe_bbt_pattern_512[] = { 0xBB };
469static uint8_t cafe_mirror_pattern_512[] = { 0xBC };
470
David Woodhouse8dd851d2006-10-20 02:11:40 +0100471
472static struct nand_bbt_descr cafe_bbt_main_descr_2048 = {
473 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
David Woodhouse048c37b2007-05-02 12:26:37 +0100474 | NAND_BBT_2BIT | NAND_BBT_VERSION,
David Woodhouse8dd851d2006-10-20 02:11:40 +0100475 .offs = 14,
476 .len = 4,
477 .veroffs = 18,
478 .maxblocks = 4,
David Woodhousefbad5692006-10-22 15:09:33 +0100479 .pattern = cafe_bbt_pattern_2048
David Woodhouse8dd851d2006-10-20 02:11:40 +0100480};
481
482static struct nand_bbt_descr cafe_bbt_mirror_descr_2048 = {
483 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
David Woodhouse048c37b2007-05-02 12:26:37 +0100484 | NAND_BBT_2BIT | NAND_BBT_VERSION,
David Woodhouse8dd851d2006-10-20 02:11:40 +0100485 .offs = 14,
486 .len = 4,
487 .veroffs = 18,
488 .maxblocks = 4,
David Woodhousefbad5692006-10-22 15:09:33 +0100489 .pattern = cafe_mirror_pattern_2048
David Woodhouse8dd851d2006-10-20 02:11:40 +0100490};
491
492static struct nand_ecclayout cafe_oobinfo_512 = {
493 .eccbytes = 14,
494 .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
495 .oobfree = {{14, 2}}
496};
497
David Woodhousefbad5692006-10-22 15:09:33 +0100498static struct nand_bbt_descr cafe_bbt_main_descr_512 = {
499 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
David Woodhouse048c37b2007-05-02 12:26:37 +0100500 | NAND_BBT_2BIT | NAND_BBT_VERSION,
David Woodhousefbad5692006-10-22 15:09:33 +0100501 .offs = 14,
502 .len = 1,
503 .veroffs = 15,
504 .maxblocks = 4,
505 .pattern = cafe_bbt_pattern_512
506};
507
508static struct nand_bbt_descr cafe_bbt_mirror_descr_512 = {
509 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
David Woodhouse048c37b2007-05-02 12:26:37 +0100510 | NAND_BBT_2BIT | NAND_BBT_VERSION,
David Woodhousefbad5692006-10-22 15:09:33 +0100511 .offs = 14,
512 .len = 1,
513 .veroffs = 15,
514 .maxblocks = 4,
515 .pattern = cafe_mirror_pattern_512
516};
517
518
David Woodhouse5467fb02006-10-06 15:36:29 +0100519static void cafe_nand_write_page_lowlevel(struct mtd_info *mtd,
520 struct nand_chip *chip, const uint8_t *buf)
521{
522 struct cafe_priv *cafe = mtd->priv;
523
David Woodhouse5467fb02006-10-06 15:36:29 +0100524 chip->write_buf(mtd, buf, mtd->writesize);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100525 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
David Woodhouse5467fb02006-10-06 15:36:29 +0100526
527 /* Set up ECC autogeneration */
David Woodhousecad40652006-11-01 08:19:20 +0800528 cafe->ctl2 |= (1<<30);
David Woodhouse5467fb02006-10-06 15:36:29 +0100529}
530
531static int cafe_nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
532 const uint8_t *buf, int page, int cached, int raw)
533{
534 int status;
535
David Woodhouse5467fb02006-10-06 15:36:29 +0100536 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
537
538 if (unlikely(raw))
539 chip->ecc.write_page_raw(mtd, chip, buf);
540 else
541 chip->ecc.write_page(mtd, chip, buf);
542
543 /*
544 * Cached progamming disabled for now, Not sure if its worth the
545 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
546 */
547 cached = 0;
548
549 if (!cached || !(chip->options & NAND_CACHEPRG)) {
550
551 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
552 status = chip->waitfunc(mtd, chip);
553 /*
554 * See if operation failed and additional status checks are
555 * available
556 */
557 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
558 status = chip->errstat(mtd, chip, FL_WRITING, status,
559 page);
560
561 if (status & NAND_STATUS_FAIL)
562 return -EIO;
563 } else {
564 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
565 status = chip->waitfunc(mtd, chip);
566 }
567
568#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
569 /* Send command to read back the data */
570 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
571
572 if (chip->verify_buf(mtd, buf, mtd->writesize))
573 return -EIO;
574#endif
575 return 0;
576}
577
David Woodhouse8dd851d2006-10-20 02:11:40 +0100578static int cafe_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
579{
580 return 0;
581}
David Woodhouse5467fb02006-10-06 15:36:29 +0100582
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200583/* F_2[X]/(X**6+X+1) */
584static unsigned short __devinit gf64_mul(u8 a, u8 b)
585{
586 u8 c;
587 unsigned int i;
588
589 c = 0;
590 for (i = 0; i < 6; i++) {
591 if (a & 1)
592 c ^= b;
593 a >>= 1;
594 b <<= 1;
595 if ((b & 0x40) != 0)
596 b ^= 0x43;
597 }
598
599 return c;
600}
601
602/* F_64[X]/(X**2+X+A**-1) with A the generator of F_64[X] */
603static u16 __devinit gf4096_mul(u16 a, u16 b)
604{
605 u8 ah, al, bh, bl, ch, cl;
606
607 ah = a >> 6;
608 al = a & 0x3f;
609 bh = b >> 6;
610 bl = b & 0x3f;
611
612 ch = gf64_mul(ah ^ al, bh ^ bl) ^ gf64_mul(al, bl);
613 cl = gf64_mul(gf64_mul(ah, bh), 0x21) ^ gf64_mul(al, bl);
614
615 return (ch << 6) ^ cl;
616}
617
618static int __devinit cafe_mul(int x)
619{
620 if (x == 0)
621 return 1;
622 return gf4096_mul(x, 0xe01);
623}
624
David Woodhouse5467fb02006-10-06 15:36:29 +0100625static int __devinit cafe_nand_probe(struct pci_dev *pdev,
626 const struct pci_device_id *ent)
627{
628 struct mtd_info *mtd;
629 struct cafe_priv *cafe;
630 uint32_t ctrl;
631 int err = 0;
632
David Woodhouse06ed24e2007-10-06 14:44:12 -0400633 /* Very old versions shared the same PCI ident for all three
634 functions on the chip. Verify the class too... */
635 if ((pdev->class >> 8) != PCI_CLASS_MEMORY_FLASH)
636 return -ENODEV;
637
David Woodhouse5467fb02006-10-06 15:36:29 +0100638 err = pci_enable_device(pdev);
639 if (err)
640 return err;
641
642 pci_set_master(pdev);
643
644 mtd = kzalloc(sizeof(*mtd) + sizeof(struct cafe_priv), GFP_KERNEL);
645 if (!mtd) {
646 dev_warn(&pdev->dev, "failed to alloc mtd_info\n");
647 return -ENOMEM;
648 }
649 cafe = (void *)(&mtd[1]);
650
David Woodhousec451c7c2009-04-04 15:27:45 +0100651 mtd->dev.parent = &pdev->dev;
David Woodhouse5467fb02006-10-06 15:36:29 +0100652 mtd->priv = cafe;
653 mtd->owner = THIS_MODULE;
654
655 cafe->pdev = pdev;
656 cafe->mmio = pci_iomap(pdev, 0, 0);
657 if (!cafe->mmio) {
658 dev_warn(&pdev->dev, "failed to iomap\n");
659 err = -ENOMEM;
660 goto out_free_mtd;
661 }
662 cafe->dmabuf = dma_alloc_coherent(&cafe->pdev->dev, 2112 + sizeof(struct nand_buffers),
663 &cafe->dmaaddr, GFP_KERNEL);
664 if (!cafe->dmabuf) {
665 err = -ENOMEM;
666 goto out_ior;
667 }
668 cafe->nand.buffers = (void *)cafe->dmabuf + 2112;
669
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200670 cafe->rs = init_rs_non_canonical(12, &cafe_mul, 0, 1, 8);
671 if (!cafe->rs) {
672 err = -ENOMEM;
673 goto out_ior;
674 }
675
David Woodhouse5467fb02006-10-06 15:36:29 +0100676 cafe->nand.cmdfunc = cafe_nand_cmdfunc;
677 cafe->nand.dev_ready = cafe_device_ready;
678 cafe->nand.read_byte = cafe_read_byte;
679 cafe->nand.read_buf = cafe_read_buf;
680 cafe->nand.write_buf = cafe_write_buf;
681 cafe->nand.select_chip = cafe_select_chip;
682
683 cafe->nand.chip_delay = 0;
684
685 /* Enable the following for a flash based bad block table */
Brian Norrisbb9ebd42011-05-31 16:31:23 -0700686 cafe->nand.bbt_options = NAND_BBT_USE_FLASH;
Brian Norrisa40f7342011-05-31 16:31:22 -0700687 cafe->nand.options = NAND_NO_AUTOINCR | NAND_OWN_BUFFERS;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100688
689 if (skipbbt) {
690 cafe->nand.options |= NAND_SKIP_BBTSCAN;
691 cafe->nand.block_bad = cafe_nand_block_bad;
692 }
David Woodhousec9ac5972006-11-30 08:17:38 +0000693
David Woodhouse527a4f42007-01-23 15:35:27 +0800694 if (numtimings && numtimings != 3) {
695 dev_warn(&cafe->pdev->dev, "%d timing register values ignored; precisely three are required\n", numtimings);
696 }
697
698 if (numtimings == 3) {
David Woodhouse527a4f42007-01-23 15:35:27 +0800699 cafe_dev_dbg(&cafe->pdev->dev, "Using provided timings (%08x %08x %08x)\n",
David Woodhouse8e5368a2007-03-23 10:40:04 +0000700 timing[0], timing[1], timing[2]);
David Woodhouse527a4f42007-01-23 15:35:27 +0800701 } else {
David Woodhouse8e5368a2007-03-23 10:40:04 +0000702 timing[0] = cafe_readl(cafe, NAND_TIMING1);
703 timing[1] = cafe_readl(cafe, NAND_TIMING2);
704 timing[2] = cafe_readl(cafe, NAND_TIMING3);
David Woodhouse527a4f42007-01-23 15:35:27 +0800705
David Woodhouse8e5368a2007-03-23 10:40:04 +0000706 if (timing[0] | timing[1] | timing[2]) {
707 cafe_dev_dbg(&cafe->pdev->dev, "Timing registers already set (%08x %08x %08x)\n",
708 timing[0], timing[1], timing[2]);
David Woodhouse527a4f42007-01-23 15:35:27 +0800709 } else {
710 dev_warn(&cafe->pdev->dev, "Timing registers unset; using most conservative defaults\n");
David Woodhouse8e5368a2007-03-23 10:40:04 +0000711 timing[0] = timing[1] = timing[2] = 0xffffffff;
David Woodhouse527a4f42007-01-23 15:35:27 +0800712 }
713 }
714
David Woodhousedcc41bc2006-10-27 09:55:34 +0300715 /* Start off by resetting the NAND controller completely */
David Woodhouse195a2532006-10-31 12:30:11 +0800716 cafe_writel(cafe, 1, NAND_RESET);
717 cafe_writel(cafe, 0, NAND_RESET);
718
David Woodhouse8e5368a2007-03-23 10:40:04 +0000719 cafe_writel(cafe, timing[0], NAND_TIMING1);
720 cafe_writel(cafe, timing[1], NAND_TIMING2);
721 cafe_writel(cafe, timing[2], NAND_TIMING3);
David Woodhousedcc41bc2006-10-27 09:55:34 +0300722
David Woodhouse195a2532006-10-31 12:30:11 +0800723 cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK);
Thomas Gleixner2db63462007-02-14 00:33:20 -0800724 err = request_irq(pdev->irq, &cafe_nand_interrupt, IRQF_SHARED,
725 "CAFE NAND", mtd);
David Woodhouse5467fb02006-10-06 15:36:29 +0100726 if (err) {
727 dev_warn(&pdev->dev, "Could not register IRQ %d\n", pdev->irq);
David Woodhouse5467fb02006-10-06 15:36:29 +0100728 goto out_free_dma;
729 }
David Woodhousef7c37d72007-01-23 15:44:10 +0800730
David Woodhouse5467fb02006-10-06 15:36:29 +0100731 /* Disable master reset, enable NAND clock */
David Woodhouse195a2532006-10-31 12:30:11 +0800732 ctrl = cafe_readl(cafe, GLOBAL_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100733 ctrl &= 0xffffeff0;
734 ctrl |= 0x00007000;
David Woodhouse195a2532006-10-31 12:30:11 +0800735 cafe_writel(cafe, ctrl | 0x05, GLOBAL_CTRL);
736 cafe_writel(cafe, ctrl | 0x0a, GLOBAL_CTRL);
737 cafe_writel(cafe, 0, NAND_DMA_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100738
David Woodhouse195a2532006-10-31 12:30:11 +0800739 cafe_writel(cafe, 0x7006, GLOBAL_CTRL);
740 cafe_writel(cafe, 0x700a, GLOBAL_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100741
742 /* Set up DMA address */
David Woodhouse195a2532006-10-31 12:30:11 +0800743 cafe_writel(cafe, cafe->dmaaddr & 0xffffffff, NAND_DMA_ADDR0);
David Woodhouse5467fb02006-10-06 15:36:29 +0100744 if (sizeof(cafe->dmaaddr) > 4)
David Woodhousefbad5692006-10-22 15:09:33 +0100745 /* Shift in two parts to shut the compiler up */
David Woodhouse195a2532006-10-31 12:30:11 +0800746 cafe_writel(cafe, (cafe->dmaaddr >> 16) >> 16, NAND_DMA_ADDR1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100747 else
David Woodhouse195a2532006-10-31 12:30:11 +0800748 cafe_writel(cafe, 0, NAND_DMA_ADDR1);
David Woodhousefbad5692006-10-22 15:09:33 +0100749
David Woodhouse8dd851d2006-10-20 02:11:40 +0100750 cafe_dev_dbg(&cafe->pdev->dev, "Set DMA address to %x (virt %p)\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800751 cafe_readl(cafe, NAND_DMA_ADDR0), cafe->dmabuf);
David Woodhouse5467fb02006-10-06 15:36:29 +0100752
753 /* Enable NAND IRQ in global IRQ mask register */
David Woodhouse195a2532006-10-31 12:30:11 +0800754 cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100755 cafe_dev_dbg(&cafe->pdev->dev, "Control %x, IRQ mask %x\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800756 cafe_readl(cafe, GLOBAL_CTRL), cafe_readl(cafe, GLOBAL_IRQ_MASK));
David Woodhousef7c37d72007-01-23 15:44:10 +0800757
758 /* Scan to find existence of the device */
David Woodhouse5e81e882010-02-26 18:32:56 +0000759 if (nand_scan_ident(mtd, 2, NULL)) {
David Woodhouse5467fb02006-10-06 15:36:29 +0100760 err = -ENXIO;
761 goto out_irq;
762 }
763
764 cafe->ctl2 = 1<<27; /* Reed-Solomon ECC */
765 if (mtd->writesize == 2048)
766 cafe->ctl2 |= 1<<29; /* 2KiB page size */
767
768 /* Set up ECC according to the type of chip we found */
David Woodhousefbad5692006-10-22 15:09:33 +0100769 if (mtd->writesize == 2048) {
David Woodhouse8dd851d2006-10-20 02:11:40 +0100770 cafe->nand.ecc.layout = &cafe_oobinfo_2048;
771 cafe->nand.bbt_td = &cafe_bbt_main_descr_2048;
772 cafe->nand.bbt_md = &cafe_bbt_mirror_descr_2048;
David Woodhousefbad5692006-10-22 15:09:33 +0100773 } else if (mtd->writesize == 512) {
774 cafe->nand.ecc.layout = &cafe_oobinfo_512;
775 cafe->nand.bbt_td = &cafe_bbt_main_descr_512;
776 cafe->nand.bbt_md = &cafe_bbt_mirror_descr_512;
David Woodhouse5467fb02006-10-06 15:36:29 +0100777 } else {
David Woodhousefbad5692006-10-22 15:09:33 +0100778 printk(KERN_WARNING "Unexpected NAND flash writesize %d. Aborting\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100779 mtd->writesize);
David Woodhousefbad5692006-10-22 15:09:33 +0100780 goto out_irq;
David Woodhouse5467fb02006-10-06 15:36:29 +0100781 }
David Woodhousefbad5692006-10-22 15:09:33 +0100782 cafe->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
783 cafe->nand.ecc.size = mtd->writesize;
784 cafe->nand.ecc.bytes = 14;
785 cafe->nand.ecc.hwctl = (void *)cafe_nand_bug;
786 cafe->nand.ecc.calculate = (void *)cafe_nand_bug;
787 cafe->nand.ecc.correct = (void *)cafe_nand_bug;
788 cafe->nand.write_page = cafe_nand_write_page;
789 cafe->nand.ecc.write_page = cafe_nand_write_page_lowlevel;
790 cafe->nand.ecc.write_oob = cafe_nand_write_oob;
791 cafe->nand.ecc.read_page = cafe_nand_read_page;
792 cafe->nand.ecc.read_oob = cafe_nand_read_oob;
David Woodhouse5467fb02006-10-06 15:36:29 +0100793
794 err = nand_scan_tail(mtd);
795 if (err)
796 goto out_irq;
797
David Woodhouse5467fb02006-10-06 15:36:29 +0100798 pci_set_drvdata(pdev, mtd);
David Woodhouse9c37f332007-10-28 21:56:39 -0400799
Philip Rakity68874412008-10-08 16:08:20 -0700800 mtd->name = "cafe_nand";
Dmitry Eremin-Solenikov4d32de82011-06-02 18:00:29 +0400801 mtd_device_parse_register(mtd, part_probes, 0, NULL, 0);
802
David Woodhouse5467fb02006-10-06 15:36:29 +0100803 goto out;
804
805 out_irq:
806 /* Disable NAND IRQ in global IRQ mask register */
David Woodhouse195a2532006-10-31 12:30:11 +0800807 cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
David Woodhouse5467fb02006-10-06 15:36:29 +0100808 free_irq(pdev->irq, mtd);
809 out_free_dma:
810 dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
811 out_ior:
812 pci_iounmap(pdev, cafe->mmio);
813 out_free_mtd:
814 kfree(mtd);
815 out:
816 return err;
817}
818
819static void __devexit cafe_nand_remove(struct pci_dev *pdev)
820{
821 struct mtd_info *mtd = pci_get_drvdata(pdev);
822 struct cafe_priv *cafe = mtd->priv;
823
David Woodhouse5467fb02006-10-06 15:36:29 +0100824 /* Disable NAND IRQ in global IRQ mask register */
David Woodhouse195a2532006-10-31 12:30:11 +0800825 cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
David Woodhouse5467fb02006-10-06 15:36:29 +0100826 free_irq(pdev->irq, mtd);
827 nand_release(mtd);
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200828 free_rs(cafe->rs);
David Woodhouse5467fb02006-10-06 15:36:29 +0100829 pci_iounmap(pdev, cafe->mmio);
830 dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
831 kfree(mtd);
832}
833
Márton Németh377ace02010-01-09 15:10:34 +0100834static const struct pci_device_id cafe_nand_tbl[] = {
David Woodhouse514fca42008-09-03 09:47:17 +0100835 { PCI_VENDOR_ID_MARVELL, PCI_DEVICE_ID_MARVELL_88ALP01_NAND,
836 PCI_ANY_ID, PCI_ANY_ID },
David Woodhouse06ed24e2007-10-06 14:44:12 -0400837 { }
David Woodhouse5467fb02006-10-06 15:36:29 +0100838};
839
840MODULE_DEVICE_TABLE(pci, cafe_nand_tbl);
841
David Woodhouse1fcf8ce2007-10-06 14:59:32 -0400842static int cafe_nand_resume(struct pci_dev *pdev)
843{
844 uint32_t ctrl;
845 struct mtd_info *mtd = pci_get_drvdata(pdev);
846 struct cafe_priv *cafe = mtd->priv;
847
848 /* Start off by resetting the NAND controller completely */
849 cafe_writel(cafe, 1, NAND_RESET);
850 cafe_writel(cafe, 0, NAND_RESET);
851 cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK);
852
853 /* Restore timing configuration */
854 cafe_writel(cafe, timing[0], NAND_TIMING1);
855 cafe_writel(cafe, timing[1], NAND_TIMING2);
856 cafe_writel(cafe, timing[2], NAND_TIMING3);
857
858 /* Disable master reset, enable NAND clock */
859 ctrl = cafe_readl(cafe, GLOBAL_CTRL);
860 ctrl &= 0xffffeff0;
861 ctrl |= 0x00007000;
862 cafe_writel(cafe, ctrl | 0x05, GLOBAL_CTRL);
863 cafe_writel(cafe, ctrl | 0x0a, GLOBAL_CTRL);
864 cafe_writel(cafe, 0, NAND_DMA_CTRL);
865 cafe_writel(cafe, 0x7006, GLOBAL_CTRL);
866 cafe_writel(cafe, 0x700a, GLOBAL_CTRL);
867
868 /* Set up DMA address */
869 cafe_writel(cafe, cafe->dmaaddr & 0xffffffff, NAND_DMA_ADDR0);
870 if (sizeof(cafe->dmaaddr) > 4)
871 /* Shift in two parts to shut the compiler up */
872 cafe_writel(cafe, (cafe->dmaaddr >> 16) >> 16, NAND_DMA_ADDR1);
873 else
874 cafe_writel(cafe, 0, NAND_DMA_ADDR1);
875
876 /* Enable NAND IRQ in global IRQ mask register */
877 cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK);
878 return 0;
879}
880
David Woodhouse5467fb02006-10-06 15:36:29 +0100881static struct pci_driver cafe_nand_pci_driver = {
882 .name = "CAFÉ NAND",
883 .id_table = cafe_nand_tbl,
884 .probe = cafe_nand_probe,
885 .remove = __devexit_p(cafe_nand_remove),
David Woodhouse5467fb02006-10-06 15:36:29 +0100886 .resume = cafe_nand_resume,
David Woodhouse5467fb02006-10-06 15:36:29 +0100887};
888
Peter Huewe627df232009-06-11 02:23:33 +0200889static int __init cafe_nand_init(void)
David Woodhouse5467fb02006-10-06 15:36:29 +0100890{
891 return pci_register_driver(&cafe_nand_pci_driver);
892}
893
Peter Huewe627df232009-06-11 02:23:33 +0200894static void __exit cafe_nand_exit(void)
David Woodhouse5467fb02006-10-06 15:36:29 +0100895{
896 pci_unregister_driver(&cafe_nand_pci_driver);
897}
898module_init(cafe_nand_init);
899module_exit(cafe_nand_exit);
900
901MODULE_LICENSE("GPL");
902MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
David Woodhousef7c37d72007-01-23 15:44:10 +0800903MODULE_DESCRIPTION("NAND flash driver for OLPC CAFÉ chip");