blob: 175cf82393efcb39df0af3aa3704a7f35a08de8b [file] [log] [blame]
David Woodhouse5467fb02006-10-06 15:36:29 +01001/*
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 *
4 * Copyright © 2006 Red Hat, Inc.
5 * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
6 */
7
David Woodhouse8dd851d2006-10-20 02:11:40 +01008#define DEBUG
David Woodhouse5467fb02006-10-06 15:36:29 +01009
10#include <linux/device.h>
11#undef DEBUG
12#include <linux/mtd/mtd.h>
13#include <linux/mtd/nand.h>
14#include <linux/pci.h>
15#include <linux/delay.h>
16#include <linux/interrupt.h>
17#include <asm/io.h>
18
19#define CAFE_NAND_CTRL1 0x00
20#define CAFE_NAND_CTRL2 0x04
21#define CAFE_NAND_CTRL3 0x08
22#define CAFE_NAND_STATUS 0x0c
23#define CAFE_NAND_IRQ 0x10
24#define CAFE_NAND_IRQ_MASK 0x14
25#define CAFE_NAND_DATA_LEN 0x18
26#define CAFE_NAND_ADDR1 0x1c
27#define CAFE_NAND_ADDR2 0x20
28#define CAFE_NAND_TIMING1 0x24
29#define CAFE_NAND_TIMING2 0x28
30#define CAFE_NAND_TIMING3 0x2c
31#define CAFE_NAND_NONMEM 0x30
David Woodhouse04459d72006-10-22 02:18:48 +010032#define CAFE_NAND_ECC_RESULT 0x3C
David Woodhousefbad5692006-10-22 15:09:33 +010033#define CAFE_NAND_DMA_CTRL 0x40
34#define CAFE_NAND_DMA_ADDR0 0x44
35#define CAFE_NAND_DMA_ADDR1 0x48
David Woodhouse04459d72006-10-22 02:18:48 +010036#define CAFE_NAND_ECC_SYN01 0x50
37#define CAFE_NAND_ECC_SYN23 0x54
38#define CAFE_NAND_ECC_SYN45 0x58
39#define CAFE_NAND_ECC_SYN67 0x5c
David Woodhouse5467fb02006-10-06 15:36:29 +010040#define CAFE_NAND_READ_DATA 0x1000
41#define CAFE_NAND_WRITE_DATA 0x2000
42
David Woodhouse195a2532006-10-31 12:30:11 +080043#define CAFE_GLOBAL_CTRL 0x3004
44#define CAFE_GLOBAL_IRQ 0x3008
45#define CAFE_GLOBAL_IRQ_MASK 0x300c
46#define CAFE_NAND_RESET 0x3034
47
David Woodhouse04459d72006-10-22 02:18:48 +010048int cafe_correct_ecc(unsigned char *buf,
49 unsigned short *chk_syndrome_list);
50
David Woodhouse5467fb02006-10-06 15:36:29 +010051struct cafe_priv {
52 struct nand_chip nand;
53 struct pci_dev *pdev;
54 void __iomem *mmio;
55 uint32_t ctl1;
56 uint32_t ctl2;
57 int datalen;
58 int nr_data;
59 int data_pos;
60 int page_addr;
61 dma_addr_t dmaaddr;
62 unsigned char *dmabuf;
63
64};
65
David Woodhouseb478c772006-10-27 14:50:04 +030066static int usedma = 1;
David Woodhouse5467fb02006-10-06 15:36:29 +010067module_param(usedma, int, 0644);
68
David Woodhouse8dd851d2006-10-20 02:11:40 +010069static int skipbbt = 0;
70module_param(skipbbt, int, 0644);
71
72static int debug = 0;
73module_param(debug, int, 0644);
74
David Woodhouseb478c772006-10-27 14:50:04 +030075static int checkecc = 1;
David Woodhouse470b0a92006-10-23 14:29:04 +010076module_param(checkecc, int, 0644);
77
David Woodhouseb478c772006-10-27 14:50:04 +030078static int slowtiming = 0;
79module_param(slowtiming, int, 0644);
80
David Woodhouse04459d72006-10-22 02:18:48 +010081/* Hrm. Why isn't this already conditional on something in the struct device? */
David Woodhouse8dd851d2006-10-20 02:11:40 +010082#define cafe_dev_dbg(dev, args...) do { if (debug) dev_dbg(dev, ##args); } while(0)
83
David Woodhouse195a2532006-10-31 12:30:11 +080084/* Make it easier to switch to PIO if we need to */
85#define cafe_readl(cafe, addr) readl((cafe)->mmio + CAFE_##addr)
86#define cafe_writel(cafe, datum, addr) writel(datum, (cafe)->mmio + CAFE_##addr)
David Woodhouse8dd851d2006-10-20 02:11:40 +010087
David Woodhouse5467fb02006-10-06 15:36:29 +010088static int cafe_device_ready(struct mtd_info *mtd)
89{
90 struct cafe_priv *cafe = mtd->priv;
David Woodhouse195a2532006-10-31 12:30:11 +080091 int result = !!(cafe_readl(cafe, NAND_STATUS) | 0x40000000);
92 uint32_t irqs = cafe_readl(cafe, NAND_IRQ);
David Woodhousefbad5692006-10-22 15:09:33 +010093
David Woodhouse195a2532006-10-31 12:30:11 +080094 cafe_writel(cafe, irqs, NAND_IRQ);
David Woodhousefbad5692006-10-22 15:09:33 +010095
David Woodhouse8dd851d2006-10-20 02:11:40 +010096 cafe_dev_dbg(&cafe->pdev->dev, "NAND device is%s ready, IRQ %x (%x) (%x,%x)\n",
David Woodhouse195a2532006-10-31 12:30:11 +080097 result?"":" not", irqs, cafe_readl(cafe, NAND_IRQ),
98 cafe_readl(cafe, GLOBAL_IRQ), cafe_readl(cafe, GLOBAL_IRQ_MASK));
David Woodhousefbad5692006-10-22 15:09:33 +010099
David Woodhouse5467fb02006-10-06 15:36:29 +0100100 return result;
101}
102
103
104static void cafe_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
105{
106 struct cafe_priv *cafe = mtd->priv;
107
108 if (usedma)
109 memcpy(cafe->dmabuf + cafe->datalen, buf, len);
110 else
111 memcpy_toio(cafe->mmio + CAFE_NAND_WRITE_DATA + cafe->datalen, buf, len);
David Woodhousefbad5692006-10-22 15:09:33 +0100112
David Woodhouse5467fb02006-10-06 15:36:29 +0100113 cafe->datalen += len;
114
David Woodhouse8dd851d2006-10-20 02:11:40 +0100115 cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes to write buffer. datalen 0x%x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100116 len, cafe->datalen);
117}
118
119static void cafe_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
120{
121 struct cafe_priv *cafe = mtd->priv;
122
123 if (usedma)
124 memcpy(buf, cafe->dmabuf + cafe->datalen, len);
125 else
126 memcpy_fromio(buf, cafe->mmio + CAFE_NAND_READ_DATA + cafe->datalen, len);
127
David Woodhouse8dd851d2006-10-20 02:11:40 +0100128 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 +0100129 len, cafe->datalen);
130 cafe->datalen += len;
131}
132
133static uint8_t cafe_read_byte(struct mtd_info *mtd)
134{
135 struct cafe_priv *cafe = mtd->priv;
136 uint8_t d;
137
138 cafe_read_buf(mtd, &d, 1);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100139 cafe_dev_dbg(&cafe->pdev->dev, "Read %02x\n", d);
David Woodhouse5467fb02006-10-06 15:36:29 +0100140
141 return d;
142}
143
144static void cafe_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
145 int column, int page_addr)
146{
147 struct cafe_priv *cafe = mtd->priv;
148 int adrbytes = 0;
149 uint32_t ctl1;
150 uint32_t doneint = 0x80000000;
David Woodhouse5467fb02006-10-06 15:36:29 +0100151
David Woodhouse8dd851d2006-10-20 02:11:40 +0100152 cafe_dev_dbg(&cafe->pdev->dev, "cmdfunc %02x, 0x%x, 0x%x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100153 command, column, page_addr);
154
155 if (command == NAND_CMD_ERASE2 || command == NAND_CMD_PAGEPROG) {
156 /* Second half of a command we already calculated */
David Woodhouse195a2532006-10-31 12:30:11 +0800157 cafe_writel(cafe, cafe->ctl2 | 0x100 | command, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100158 ctl1 = cafe->ctl1;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100159 cafe_dev_dbg(&cafe->pdev->dev, "Continue command, ctl1 %08x, #data %d\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100160 cafe->ctl1, cafe->nr_data);
161 goto do_command;
162 }
163 /* Reset ECC engine */
David Woodhouse195a2532006-10-31 12:30:11 +0800164 cafe_writel(cafe, 0, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100165
166 /* Emulate NAND_CMD_READOOB on large-page chips */
167 if (mtd->writesize > 512 &&
168 command == NAND_CMD_READOOB) {
169 column += mtd->writesize;
170 command = NAND_CMD_READ0;
171 }
172
173 /* FIXME: Do we need to send read command before sending data
174 for small-page chips, to position the buffer correctly? */
175
176 if (column != -1) {
David Woodhouse195a2532006-10-31 12:30:11 +0800177 cafe_writel(cafe, column, NAND_ADDR1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100178 adrbytes = 2;
179 if (page_addr != -1)
180 goto write_adr2;
181 } else if (page_addr != -1) {
David Woodhouse195a2532006-10-31 12:30:11 +0800182 cafe_writel(cafe, page_addr & 0xffff, NAND_ADDR1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100183 page_addr >>= 16;
184 write_adr2:
David Woodhouse195a2532006-10-31 12:30:11 +0800185 cafe_writel(cafe, page_addr, NAND_ADDR2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100186 adrbytes += 2;
187 if (mtd->size > mtd->writesize << 16)
188 adrbytes++;
189 }
190
191 cafe->data_pos = cafe->datalen = 0;
192
193 /* Set command valid bit */
194 ctl1 = 0x80000000 | command;
195
196 /* Set RD or WR bits as appropriate */
197 if (command == NAND_CMD_READID || command == NAND_CMD_STATUS) {
198 ctl1 |= (1<<26); /* rd */
199 /* Always 5 bytes, for now */
David Woodhouse8dd851d2006-10-20 02:11:40 +0100200 cafe->datalen = 4;
David Woodhouse5467fb02006-10-06 15:36:29 +0100201 /* And one address cycle -- even for STATUS, since the controller doesn't work without */
202 adrbytes = 1;
203 } else if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
204 command == NAND_CMD_READOOB || command == NAND_CMD_RNDOUT) {
205 ctl1 |= 1<<26; /* rd */
206 /* For now, assume just read to end of page */
207 cafe->datalen = mtd->writesize + mtd->oobsize - column;
208 } else if (command == NAND_CMD_SEQIN)
209 ctl1 |= 1<<25; /* wr */
210
211 /* Set number of address bytes */
212 if (adrbytes)
213 ctl1 |= ((adrbytes-1)|8) << 27;
214
215 if (command == NAND_CMD_SEQIN || command == NAND_CMD_ERASE1) {
216 /* Ignore the first command of a pair; the hardware
217 deals with them both at once, later */
218 cafe->ctl1 = ctl1;
219 cafe->ctl2 = 0;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100220 cafe_dev_dbg(&cafe->pdev->dev, "Setup for delayed command, ctl1 %08x, dlen %x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100221 cafe->ctl1, cafe->datalen);
222 return;
223 }
224 /* RNDOUT and READ0 commands need a following byte */
225 if (command == NAND_CMD_RNDOUT)
David Woodhouse195a2532006-10-31 12:30:11 +0800226 cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_RNDOUTSTART, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100227 else if (command == NAND_CMD_READ0 && mtd->writesize > 512)
David Woodhouse195a2532006-10-31 12:30:11 +0800228 cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_READSTART, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100229
230 do_command:
David Woodhouse470b0a92006-10-23 14:29:04 +0100231#if 0
David Woodhousefbad5692006-10-22 15:09:33 +0100232 /* http://dev.laptop.org/ticket/200
233 ECC on read only works if we read precisely 0x80e bytes */
David Woodhouse04459d72006-10-22 02:18:48 +0100234 if (cafe->datalen == 2112)
235 cafe->datalen = 2062;
236#endif
David Woodhouse8dd851d2006-10-20 02:11:40 +0100237 cafe_dev_dbg(&cafe->pdev->dev, "dlen %x, ctl1 %x, ctl2 %x\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800238 cafe->datalen, ctl1, cafe_readl(cafe, NAND_CTRL2));
David Woodhousefbad5692006-10-22 15:09:33 +0100239
David Woodhouse5467fb02006-10-06 15:36:29 +0100240 /* NB: The datasheet lies -- we really should be subtracting 1 here */
David Woodhouse195a2532006-10-31 12:30:11 +0800241 cafe_writel(cafe, cafe->datalen, NAND_DATA_LEN);
242 cafe_writel(cafe, 0x90000000, NAND_IRQ);
David Woodhouse5467fb02006-10-06 15:36:29 +0100243 if (usedma && (ctl1 & (3<<25))) {
244 uint32_t dmactl = 0xc0000000 + cafe->datalen;
245 /* If WR or RD bits set, set up DMA */
246 if (ctl1 & (1<<26)) {
247 /* It's a read */
248 dmactl |= (1<<29);
249 /* ... so it's done when the DMA is done, not just
250 the command. */
251 doneint = 0x10000000;
252 }
David Woodhouse195a2532006-10-31 12:30:11 +0800253 cafe_writel(cafe, dmactl, NAND_DMA_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100254 }
David Woodhouse5467fb02006-10-06 15:36:29 +0100255 cafe->datalen = 0;
256
257#if 0
David Woodhousefbad5692006-10-22 15:09:33 +0100258 { int i;
David Woodhouse5467fb02006-10-06 15:36:29 +0100259 printk("About to write command %08x\n", ctl1);
260 for (i=0; i< 0x5c; i+=4)
261 printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
David Woodhousefbad5692006-10-22 15:09:33 +0100262 }
David Woodhouse5467fb02006-10-06 15:36:29 +0100263#endif
David Woodhouse195a2532006-10-31 12:30:11 +0800264 cafe_writel(cafe, ctl1, NAND_CTRL1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100265 /* Apply this short delay always to ensure that we do wait tWB in
266 * any case on any machine. */
267 ndelay(100);
268
269 if (1) {
David Woodhouse8dd851d2006-10-20 02:11:40 +0100270 int c = 500000;
David Woodhouse5467fb02006-10-06 15:36:29 +0100271 uint32_t irqs;
272
273 while (c--) {
David Woodhouse195a2532006-10-31 12:30:11 +0800274 irqs = cafe_readl(cafe, NAND_IRQ);
David Woodhouse5467fb02006-10-06 15:36:29 +0100275 if (irqs & doneint)
276 break;
277 udelay(1);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100278 if (!(c % 100000))
279 cafe_dev_dbg(&cafe->pdev->dev, "Wait for ready, IRQ %x\n", irqs);
David Woodhouse5467fb02006-10-06 15:36:29 +0100280 cpu_relax();
281 }
David Woodhouse195a2532006-10-31 12:30:11 +0800282 cafe_writel(cafe, doneint, NAND_IRQ);
David Woodhousea0207272006-10-28 17:08:38 +0300283 cafe_dev_dbg(&cafe->pdev->dev, "Command %x completed after %d usec, irqs %x (%x)\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800284 command, 500000-c, irqs, cafe_readl(cafe, NAND_IRQ));
David Woodhouse5467fb02006-10-06 15:36:29 +0100285 }
286
287
288 cafe->ctl2 &= ~(1<<8);
289 cafe->ctl2 &= ~(1<<30);
290
291 switch (command) {
292
293 case NAND_CMD_CACHEDPROG:
294 case NAND_CMD_PAGEPROG:
295 case NAND_CMD_ERASE1:
296 case NAND_CMD_ERASE2:
297 case NAND_CMD_SEQIN:
298 case NAND_CMD_RNDIN:
299 case NAND_CMD_STATUS:
300 case NAND_CMD_DEPLETE1:
301 case NAND_CMD_RNDOUT:
302 case NAND_CMD_STATUS_ERROR:
303 case NAND_CMD_STATUS_ERROR0:
304 case NAND_CMD_STATUS_ERROR1:
305 case NAND_CMD_STATUS_ERROR2:
306 case NAND_CMD_STATUS_ERROR3:
David Woodhouse195a2532006-10-31 12:30:11 +0800307 cafe_writel(cafe, cafe->ctl2, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100308 return;
309 }
310 nand_wait_ready(mtd);
David Woodhouse195a2532006-10-31 12:30:11 +0800311 cafe_writel(cafe, cafe->ctl2, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100312}
313
314static void cafe_select_chip(struct mtd_info *mtd, int chipnr)
315{
316 //struct cafe_priv *cafe = mtd->priv;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100317 // cafe_dev_dbg(&cafe->pdev->dev, "select_chip %d\n", chipnr);
David Woodhouse5467fb02006-10-06 15:36:29 +0100318}
David Woodhousefbad5692006-10-22 15:09:33 +0100319
David Woodhouse5467fb02006-10-06 15:36:29 +0100320static int cafe_nand_interrupt(int irq, void *id, struct pt_regs *regs)
321{
322 struct mtd_info *mtd = id;
323 struct cafe_priv *cafe = mtd->priv;
David Woodhouse195a2532006-10-31 12:30:11 +0800324 uint32_t irqs = cafe_readl(cafe, NAND_IRQ);
325 cafe_writel(cafe, irqs & ~0x90000000, NAND_IRQ);
David Woodhouse5467fb02006-10-06 15:36:29 +0100326 if (!irqs)
327 return IRQ_NONE;
328
David Woodhouse195a2532006-10-31 12:30:11 +0800329 cafe_dev_dbg(&cafe->pdev->dev, "irq, bits %x (%x)\n", irqs, cafe_readl(cafe, NAND_IRQ));
David Woodhouse5467fb02006-10-06 15:36:29 +0100330 return IRQ_HANDLED;
331}
332
333static void cafe_nand_bug(struct mtd_info *mtd)
334{
335 BUG();
336}
337
338static int cafe_nand_write_oob(struct mtd_info *mtd,
339 struct nand_chip *chip, int page)
340{
341 int status = 0;
342
David Woodhouse5467fb02006-10-06 15:36:29 +0100343 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
344 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
345 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
346 status = chip->waitfunc(mtd, chip);
347
348 return status & NAND_STATUS_FAIL ? -EIO : 0;
349}
350
351/* Don't use -- use nand_read_oob_std for now */
352static int cafe_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
353 int page, int sndcmd)
354{
355 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
356 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
357 return 1;
358}
359/**
360 * cafe_nand_read_page_syndrome - {REPLACABLE] hardware ecc syndrom based page read
361 * @mtd: mtd info structure
362 * @chip: nand chip info structure
363 * @buf: buffer to store read data
364 *
365 * The hw generator calculates the error syndrome automatically. Therefor
366 * we need a special oob layout and handling.
367 */
368static int cafe_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
369 uint8_t *buf)
370{
371 struct cafe_priv *cafe = mtd->priv;
372
David Woodhousefbad5692006-10-22 15:09:33 +0100373 cafe_dev_dbg(&cafe->pdev->dev, "ECC result %08x SYN1,2 %08x\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800374 cafe_readl(cafe, NAND_ECC_RESULT),
375 cafe_readl(cafe, NAND_ECC_SYN01));
David Woodhouse5467fb02006-10-06 15:36:29 +0100376
377 chip->read_buf(mtd, buf, mtd->writesize);
378 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
379
David Woodhouse195a2532006-10-31 12:30:11 +0800380 if (checkecc && cafe_readl(cafe, NAND_ECC_RESULT) & (1<<18)) {
David Woodhouse04459d72006-10-22 02:18:48 +0100381 unsigned short syn[8];
382 int i;
383
384 for (i=0; i<8; i+=2) {
David Woodhouse195a2532006-10-31 12:30:11 +0800385 uint32_t tmp = cafe_readl(cafe, NAND_ECC_SYN01 + (i*2));
David Woodhouse04459d72006-10-22 02:18:48 +0100386 syn[i] = tmp & 0xfff;
387 syn[i+1] = (tmp >> 16) & 0xfff;
388 }
389
David Woodhouse63a14232006-10-27 22:12:02 +0300390 if ((i = cafe_correct_ecc(buf, syn)) < 0) {
David Woodhouse04459d72006-10-22 02:18:48 +0100391 dev_dbg(&cafe->pdev->dev, "Failed to correct ECC\n");
392 mtd->ecc_stats.failed++;
393 } else {
394 dev_dbg(&cafe->pdev->dev, "Corrected %d symbol errors\n", i);
395 mtd->ecc_stats.corrected += i;
396 }
397 }
398
399
David Woodhouse5467fb02006-10-06 15:36:29 +0100400 return 0;
401}
402
David Woodhouse8dd851d2006-10-20 02:11:40 +0100403static struct nand_ecclayout cafe_oobinfo_2048 = {
404 .eccbytes = 14,
405 .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
406 .oobfree = {{14, 50}}
407};
408
409/* Ick. The BBT code really ought to be able to work this bit out
David Woodhousefbad5692006-10-22 15:09:33 +0100410 for itself from the above, at least for the 2KiB case */
411static uint8_t cafe_bbt_pattern_2048[] = { 'B', 'b', 't', '0' };
412static uint8_t cafe_mirror_pattern_2048[] = { '1', 't', 'b', 'B' };
413
414static uint8_t cafe_bbt_pattern_512[] = { 0xBB };
415static uint8_t cafe_mirror_pattern_512[] = { 0xBC };
416
David Woodhouse8dd851d2006-10-20 02:11:40 +0100417
418static struct nand_bbt_descr cafe_bbt_main_descr_2048 = {
419 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
420 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
421 .offs = 14,
422 .len = 4,
423 .veroffs = 18,
424 .maxblocks = 4,
David Woodhousefbad5692006-10-22 15:09:33 +0100425 .pattern = cafe_bbt_pattern_2048
David Woodhouse8dd851d2006-10-20 02:11:40 +0100426};
427
428static struct nand_bbt_descr cafe_bbt_mirror_descr_2048 = {
429 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
430 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
431 .offs = 14,
432 .len = 4,
433 .veroffs = 18,
434 .maxblocks = 4,
David Woodhousefbad5692006-10-22 15:09:33 +0100435 .pattern = cafe_mirror_pattern_2048
David Woodhouse8dd851d2006-10-20 02:11:40 +0100436};
437
438static struct nand_ecclayout cafe_oobinfo_512 = {
439 .eccbytes = 14,
440 .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
441 .oobfree = {{14, 2}}
442};
443
David Woodhousefbad5692006-10-22 15:09:33 +0100444static struct nand_bbt_descr cafe_bbt_main_descr_512 = {
445 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
446 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
447 .offs = 14,
448 .len = 1,
449 .veroffs = 15,
450 .maxblocks = 4,
451 .pattern = cafe_bbt_pattern_512
452};
453
454static struct nand_bbt_descr cafe_bbt_mirror_descr_512 = {
455 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
456 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
457 .offs = 14,
458 .len = 1,
459 .veroffs = 15,
460 .maxblocks = 4,
461 .pattern = cafe_mirror_pattern_512
462};
463
464
David Woodhouse5467fb02006-10-06 15:36:29 +0100465static void cafe_nand_write_page_lowlevel(struct mtd_info *mtd,
466 struct nand_chip *chip, const uint8_t *buf)
467{
468 struct cafe_priv *cafe = mtd->priv;
469
David Woodhouse5467fb02006-10-06 15:36:29 +0100470 chip->write_buf(mtd, buf, mtd->writesize);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100471 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
David Woodhouse5467fb02006-10-06 15:36:29 +0100472
473 /* Set up ECC autogeneration */
474 cafe->ctl2 |= (1<<27) | (1<<30);
475 if (mtd->writesize == 2048)
476 cafe->ctl2 |= (1<<29);
477}
478
479static int cafe_nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
480 const uint8_t *buf, int page, int cached, int raw)
481{
482 int status;
483
David Woodhouse5467fb02006-10-06 15:36:29 +0100484 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
485
486 if (unlikely(raw))
487 chip->ecc.write_page_raw(mtd, chip, buf);
488 else
489 chip->ecc.write_page(mtd, chip, buf);
490
491 /*
492 * Cached progamming disabled for now, Not sure if its worth the
493 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
494 */
495 cached = 0;
496
497 if (!cached || !(chip->options & NAND_CACHEPRG)) {
498
499 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
500 status = chip->waitfunc(mtd, chip);
501 /*
502 * See if operation failed and additional status checks are
503 * available
504 */
505 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
506 status = chip->errstat(mtd, chip, FL_WRITING, status,
507 page);
508
509 if (status & NAND_STATUS_FAIL)
510 return -EIO;
511 } else {
512 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
513 status = chip->waitfunc(mtd, chip);
514 }
515
516#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
517 /* Send command to read back the data */
518 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
519
520 if (chip->verify_buf(mtd, buf, mtd->writesize))
521 return -EIO;
522#endif
523 return 0;
524}
525
David Woodhouse8dd851d2006-10-20 02:11:40 +0100526static int cafe_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
527{
528 return 0;
529}
David Woodhouse5467fb02006-10-06 15:36:29 +0100530
531static int __devinit cafe_nand_probe(struct pci_dev *pdev,
532 const struct pci_device_id *ent)
533{
534 struct mtd_info *mtd;
535 struct cafe_priv *cafe;
536 uint32_t ctrl;
537 int err = 0;
538
539 err = pci_enable_device(pdev);
540 if (err)
541 return err;
542
543 pci_set_master(pdev);
544
545 mtd = kzalloc(sizeof(*mtd) + sizeof(struct cafe_priv), GFP_KERNEL);
546 if (!mtd) {
547 dev_warn(&pdev->dev, "failed to alloc mtd_info\n");
548 return -ENOMEM;
549 }
550 cafe = (void *)(&mtd[1]);
551
552 mtd->priv = cafe;
553 mtd->owner = THIS_MODULE;
554
555 cafe->pdev = pdev;
556 cafe->mmio = pci_iomap(pdev, 0, 0);
557 if (!cafe->mmio) {
558 dev_warn(&pdev->dev, "failed to iomap\n");
559 err = -ENOMEM;
560 goto out_free_mtd;
561 }
562 cafe->dmabuf = dma_alloc_coherent(&cafe->pdev->dev, 2112 + sizeof(struct nand_buffers),
563 &cafe->dmaaddr, GFP_KERNEL);
564 if (!cafe->dmabuf) {
565 err = -ENOMEM;
566 goto out_ior;
567 }
568 cafe->nand.buffers = (void *)cafe->dmabuf + 2112;
569
570 cafe->nand.cmdfunc = cafe_nand_cmdfunc;
571 cafe->nand.dev_ready = cafe_device_ready;
572 cafe->nand.read_byte = cafe_read_byte;
573 cafe->nand.read_buf = cafe_read_buf;
574 cafe->nand.write_buf = cafe_write_buf;
575 cafe->nand.select_chip = cafe_select_chip;
576
577 cafe->nand.chip_delay = 0;
578
579 /* Enable the following for a flash based bad block table */
580 cafe->nand.options = NAND_USE_FLASH_BBT | NAND_NO_AUTOINCR | NAND_OWN_BUFFERS;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100581
582 if (skipbbt) {
583 cafe->nand.options |= NAND_SKIP_BBTSCAN;
584 cafe->nand.block_bad = cafe_nand_block_bad;
585 }
David Woodhouse5467fb02006-10-06 15:36:29 +0100586
David Woodhousedcc41bc2006-10-27 09:55:34 +0300587 /* Start off by resetting the NAND controller completely */
David Woodhouse195a2532006-10-31 12:30:11 +0800588 cafe_writel(cafe, 1, NAND_RESET);
589 cafe_writel(cafe, 0, NAND_RESET);
590
591 cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK);
David Woodhousedcc41bc2006-10-27 09:55:34 +0300592
David Woodhouse5467fb02006-10-06 15:36:29 +0100593 /* Timings from Marvell's test code (not verified or calculated by us) */
David Woodhouseb478c772006-10-27 14:50:04 +0300594 if (!slowtiming) {
David Woodhouse195a2532006-10-31 12:30:11 +0800595 cafe_writel(cafe, 0x01010a0a, NAND_TIMING1);
596 cafe_writel(cafe, 0x24121212, NAND_TIMING2);
597 cafe_writel(cafe, 0x11000000, NAND_TIMING3);
David Woodhouseb478c772006-10-27 14:50:04 +0300598 } else {
David Woodhouse195a2532006-10-31 12:30:11 +0800599 cafe_writel(cafe, 0xffffffff, NAND_TIMING1);
600 cafe_writel(cafe, 0xffffffff, NAND_TIMING2);
601 cafe_writel(cafe, 0xffffffff, NAND_TIMING3);
David Woodhouseb478c772006-10-27 14:50:04 +0300602 }
David Woodhouse195a2532006-10-31 12:30:11 +0800603 cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK);
David Woodhouse5467fb02006-10-06 15:36:29 +0100604 err = request_irq(pdev->irq, &cafe_nand_interrupt, SA_SHIRQ, "CAFE NAND", mtd);
605 if (err) {
606 dev_warn(&pdev->dev, "Could not register IRQ %d\n", pdev->irq);
607
608 goto out_free_dma;
609 }
610#if 1
611 /* Disable master reset, enable NAND clock */
David Woodhouse195a2532006-10-31 12:30:11 +0800612 ctrl = cafe_readl(cafe, GLOBAL_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100613 ctrl &= 0xffffeff0;
614 ctrl |= 0x00007000;
David Woodhouse195a2532006-10-31 12:30:11 +0800615 cafe_writel(cafe, ctrl | 0x05, GLOBAL_CTRL);
616 cafe_writel(cafe, ctrl | 0x0a, GLOBAL_CTRL);
617 cafe_writel(cafe, 0, NAND_DMA_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100618
David Woodhouse195a2532006-10-31 12:30:11 +0800619 cafe_writel(cafe, 0x7006, GLOBAL_CTRL);
620 cafe_writel(cafe, 0x700a, GLOBAL_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100621
622 /* Set up DMA address */
David Woodhouse195a2532006-10-31 12:30:11 +0800623 cafe_writel(cafe, cafe->dmaaddr & 0xffffffff, NAND_DMA_ADDR0);
David Woodhouse5467fb02006-10-06 15:36:29 +0100624 if (sizeof(cafe->dmaaddr) > 4)
David Woodhousefbad5692006-10-22 15:09:33 +0100625 /* Shift in two parts to shut the compiler up */
David Woodhouse195a2532006-10-31 12:30:11 +0800626 cafe_writel(cafe, (cafe->dmaaddr >> 16) >> 16, NAND_DMA_ADDR1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100627 else
David Woodhouse195a2532006-10-31 12:30:11 +0800628 cafe_writel(cafe, 0, NAND_DMA_ADDR1);
David Woodhousefbad5692006-10-22 15:09:33 +0100629
David Woodhouse8dd851d2006-10-20 02:11:40 +0100630 cafe_dev_dbg(&cafe->pdev->dev, "Set DMA address to %x (virt %p)\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800631 cafe_readl(cafe, NAND_DMA_ADDR0), cafe->dmabuf);
David Woodhouse5467fb02006-10-06 15:36:29 +0100632
633 /* Enable NAND IRQ in global IRQ mask register */
David Woodhouse195a2532006-10-31 12:30:11 +0800634 cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100635 cafe_dev_dbg(&cafe->pdev->dev, "Control %x, IRQ mask %x\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800636 cafe_readl(cafe, GLOBAL_CTRL), cafe_readl(cafe, GLOBAL_IRQ_MASK));
David Woodhouse5467fb02006-10-06 15:36:29 +0100637#endif
638#if 1
639 mtd->writesize=2048;
640 mtd->oobsize = 0x40;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100641 memset(cafe->dmabuf, 0x5a, 2112);
David Woodhouse5467fb02006-10-06 15:36:29 +0100642 cafe->nand.cmdfunc(mtd, NAND_CMD_READID, 0, -1);
643 cafe->nand.read_byte(mtd);
644 cafe->nand.read_byte(mtd);
645 cafe->nand.read_byte(mtd);
646 cafe->nand.read_byte(mtd);
647 cafe->nand.read_byte(mtd);
648#endif
649#if 0
650 cafe->nand.cmdfunc(mtd, NAND_CMD_READ0, 0, 0);
651 // nand_wait_ready(mtd);
652 cafe->nand.read_byte(mtd);
653 cafe->nand.read_byte(mtd);
654 cafe->nand.read_byte(mtd);
655 cafe->nand.read_byte(mtd);
656#endif
657#if 0
658 writel(0x84600070, cafe->mmio);
659 udelay(10);
David Woodhouse195a2532006-10-31 12:30:11 +0800660 cafe_dev_dbg(&cafe->pdev->dev, "Status %x\n", cafe_readl(cafe, NAND_NONMEM));
David Woodhouse5467fb02006-10-06 15:36:29 +0100661#endif
662 /* Scan to find existance of the device */
663 if (nand_scan_ident(mtd, 1)) {
664 err = -ENXIO;
665 goto out_irq;
666 }
667
668 cafe->ctl2 = 1<<27; /* Reed-Solomon ECC */
669 if (mtd->writesize == 2048)
670 cafe->ctl2 |= 1<<29; /* 2KiB page size */
671
672 /* Set up ECC according to the type of chip we found */
David Woodhousefbad5692006-10-22 15:09:33 +0100673 if (mtd->writesize == 2048) {
David Woodhouse8dd851d2006-10-20 02:11:40 +0100674 cafe->nand.ecc.layout = &cafe_oobinfo_2048;
675 cafe->nand.bbt_td = &cafe_bbt_main_descr_2048;
676 cafe->nand.bbt_md = &cafe_bbt_mirror_descr_2048;
David Woodhousefbad5692006-10-22 15:09:33 +0100677 } else if (mtd->writesize == 512) {
678 cafe->nand.ecc.layout = &cafe_oobinfo_512;
679 cafe->nand.bbt_td = &cafe_bbt_main_descr_512;
680 cafe->nand.bbt_md = &cafe_bbt_mirror_descr_512;
David Woodhouse5467fb02006-10-06 15:36:29 +0100681 } else {
David Woodhousefbad5692006-10-22 15:09:33 +0100682 printk(KERN_WARNING "Unexpected NAND flash writesize %d. Aborting\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100683 mtd->writesize);
David Woodhousefbad5692006-10-22 15:09:33 +0100684 goto out_irq;
David Woodhouse5467fb02006-10-06 15:36:29 +0100685 }
David Woodhousefbad5692006-10-22 15:09:33 +0100686 cafe->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
687 cafe->nand.ecc.size = mtd->writesize;
688 cafe->nand.ecc.bytes = 14;
689 cafe->nand.ecc.hwctl = (void *)cafe_nand_bug;
690 cafe->nand.ecc.calculate = (void *)cafe_nand_bug;
691 cafe->nand.ecc.correct = (void *)cafe_nand_bug;
692 cafe->nand.write_page = cafe_nand_write_page;
693 cafe->nand.ecc.write_page = cafe_nand_write_page_lowlevel;
694 cafe->nand.ecc.write_oob = cafe_nand_write_oob;
695 cafe->nand.ecc.read_page = cafe_nand_read_page;
696 cafe->nand.ecc.read_oob = cafe_nand_read_oob;
David Woodhouse5467fb02006-10-06 15:36:29 +0100697
698 err = nand_scan_tail(mtd);
699 if (err)
700 goto out_irq;
701
David Woodhouse5467fb02006-10-06 15:36:29 +0100702 pci_set_drvdata(pdev, mtd);
703 add_mtd_device(mtd);
704 goto out;
705
706 out_irq:
707 /* Disable NAND IRQ in global IRQ mask register */
David Woodhouse195a2532006-10-31 12:30:11 +0800708 cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
David Woodhouse5467fb02006-10-06 15:36:29 +0100709 free_irq(pdev->irq, mtd);
710 out_free_dma:
711 dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
712 out_ior:
713 pci_iounmap(pdev, cafe->mmio);
714 out_free_mtd:
715 kfree(mtd);
716 out:
717 return err;
718}
719
720static void __devexit cafe_nand_remove(struct pci_dev *pdev)
721{
722 struct mtd_info *mtd = pci_get_drvdata(pdev);
723 struct cafe_priv *cafe = mtd->priv;
724
725 del_mtd_device(mtd);
726 /* Disable NAND IRQ in global IRQ mask register */
David Woodhouse195a2532006-10-31 12:30:11 +0800727 cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
David Woodhouse5467fb02006-10-06 15:36:29 +0100728 free_irq(pdev->irq, mtd);
729 nand_release(mtd);
730 pci_iounmap(pdev, cafe->mmio);
731 dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
732 kfree(mtd);
733}
734
735static struct pci_device_id cafe_nand_tbl[] = {
736 { 0x11ab, 0x4100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MEMORY_FLASH << 8, 0xFFFF0 }
737};
738
739MODULE_DEVICE_TABLE(pci, cafe_nand_tbl);
740
741static struct pci_driver cafe_nand_pci_driver = {
742 .name = "CAFÉ NAND",
743 .id_table = cafe_nand_tbl,
744 .probe = cafe_nand_probe,
745 .remove = __devexit_p(cafe_nand_remove),
746#ifdef CONFIG_PMx
747 .suspend = cafe_nand_suspend,
748 .resume = cafe_nand_resume,
749#endif
750};
751
752static int cafe_nand_init(void)
753{
754 return pci_register_driver(&cafe_nand_pci_driver);
755}
756
757static void cafe_nand_exit(void)
758{
759 pci_unregister_driver(&cafe_nand_pci_driver);
760}
761module_init(cafe_nand_init);
762module_exit(cafe_nand_exit);
763
764MODULE_LICENSE("GPL");
765MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
766MODULE_DESCRIPTION("NAND flash driver for OLPC CAFE chip");
767
768/* Correct ECC for 2048 bytes of 0xff:
769 41 a0 71 65 54 27 f3 93 ec a9 be ed 0b a1 */
David Woodhouse8dd851d2006-10-20 02:11:40 +0100770
771/* dwmw2's B-test board, in case of completely screwing it:
772Bad eraseblock 2394 at 0x12b40000
773Bad eraseblock 2627 at 0x14860000
774Bad eraseblock 3349 at 0x1a2a0000
775*/