blob: 10132efd05883d90adcac4d803fa32152eaf4a11 [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 Woodhouse04459d72006-10-22 02:18:48 +010043int cafe_correct_ecc(unsigned char *buf,
44 unsigned short *chk_syndrome_list);
45
David Woodhouse5467fb02006-10-06 15:36:29 +010046struct cafe_priv {
47 struct nand_chip nand;
48 struct pci_dev *pdev;
49 void __iomem *mmio;
50 uint32_t ctl1;
51 uint32_t ctl2;
52 int datalen;
53 int nr_data;
54 int data_pos;
55 int page_addr;
56 dma_addr_t dmaaddr;
57 unsigned char *dmabuf;
58
59};
60
David Woodhouse04459d72006-10-22 02:18:48 +010061static int usedma = 0;
David Woodhouse5467fb02006-10-06 15:36:29 +010062module_param(usedma, int, 0644);
63
David Woodhouse8dd851d2006-10-20 02:11:40 +010064static int skipbbt = 0;
65module_param(skipbbt, int, 0644);
66
67static int debug = 0;
68module_param(debug, int, 0644);
69
David Woodhouse04459d72006-10-22 02:18:48 +010070/* Hrm. Why isn't this already conditional on something in the struct device? */
David Woodhouse8dd851d2006-10-20 02:11:40 +010071#define cafe_dev_dbg(dev, args...) do { if (debug) dev_dbg(dev, ##args); } while(0)
72
73
David Woodhouse5467fb02006-10-06 15:36:29 +010074static int cafe_device_ready(struct mtd_info *mtd)
75{
76 struct cafe_priv *cafe = mtd->priv;
77 int result = !!(readl(cafe->mmio + CAFE_NAND_STATUS) | 0x40000000);
David Woodhouse8dd851d2006-10-20 02:11:40 +010078 uint32_t irqs = readl(cafe->mmio + CAFE_NAND_IRQ);
David Woodhousefbad5692006-10-22 15:09:33 +010079
David Woodhouse8dd851d2006-10-20 02:11:40 +010080 writel(irqs, cafe->mmio+CAFE_NAND_IRQ);
David Woodhousefbad5692006-10-22 15:09:33 +010081
David Woodhouse8dd851d2006-10-20 02:11:40 +010082 cafe_dev_dbg(&cafe->pdev->dev, "NAND device is%s ready, IRQ %x (%x) (%x,%x)\n",
83 result?"":" not", irqs, readl(cafe->mmio + CAFE_NAND_IRQ),
David Woodhouse5467fb02006-10-06 15:36:29 +010084 readl(cafe->mmio + 0x3008), readl(cafe->mmio + 0x300c));
David Woodhousefbad5692006-10-22 15:09:33 +010085
David Woodhouse5467fb02006-10-06 15:36:29 +010086 return result;
87}
88
89
90static void cafe_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
91{
92 struct cafe_priv *cafe = mtd->priv;
93
94 if (usedma)
95 memcpy(cafe->dmabuf + cafe->datalen, buf, len);
96 else
97 memcpy_toio(cafe->mmio + CAFE_NAND_WRITE_DATA + cafe->datalen, buf, len);
David Woodhousefbad5692006-10-22 15:09:33 +010098
David Woodhouse5467fb02006-10-06 15:36:29 +010099 cafe->datalen += len;
100
David Woodhouse8dd851d2006-10-20 02:11:40 +0100101 cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes to write buffer. datalen 0x%x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100102 len, cafe->datalen);
103}
104
105static void cafe_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
106{
107 struct cafe_priv *cafe = mtd->priv;
108
109 if (usedma)
110 memcpy(buf, cafe->dmabuf + cafe->datalen, len);
111 else
112 memcpy_fromio(buf, cafe->mmio + CAFE_NAND_READ_DATA + cafe->datalen, len);
113
David Woodhouse8dd851d2006-10-20 02:11:40 +0100114 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 +0100115 len, cafe->datalen);
116 cafe->datalen += len;
117}
118
119static uint8_t cafe_read_byte(struct mtd_info *mtd)
120{
121 struct cafe_priv *cafe = mtd->priv;
122 uint8_t d;
123
124 cafe_read_buf(mtd, &d, 1);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100125 cafe_dev_dbg(&cafe->pdev->dev, "Read %02x\n", d);
David Woodhouse5467fb02006-10-06 15:36:29 +0100126
127 return d;
128}
129
130static void cafe_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
131 int column, int page_addr)
132{
133 struct cafe_priv *cafe = mtd->priv;
134 int adrbytes = 0;
135 uint32_t ctl1;
136 uint32_t doneint = 0x80000000;
David Woodhouse5467fb02006-10-06 15:36:29 +0100137
David Woodhouse8dd851d2006-10-20 02:11:40 +0100138 cafe_dev_dbg(&cafe->pdev->dev, "cmdfunc %02x, 0x%x, 0x%x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100139 command, column, page_addr);
140
141 if (command == NAND_CMD_ERASE2 || command == NAND_CMD_PAGEPROG) {
142 /* Second half of a command we already calculated */
David Woodhousefbad5692006-10-22 15:09:33 +0100143 writel(cafe->ctl2 | 0x100 | command, cafe->mmio + CAFE_NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100144 ctl1 = cafe->ctl1;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100145 cafe_dev_dbg(&cafe->pdev->dev, "Continue command, ctl1 %08x, #data %d\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100146 cafe->ctl1, cafe->nr_data);
147 goto do_command;
148 }
149 /* Reset ECC engine */
150 writel(0, cafe->mmio + CAFE_NAND_CTRL2);
151
152 /* Emulate NAND_CMD_READOOB on large-page chips */
153 if (mtd->writesize > 512 &&
154 command == NAND_CMD_READOOB) {
155 column += mtd->writesize;
156 command = NAND_CMD_READ0;
157 }
158
159 /* FIXME: Do we need to send read command before sending data
160 for small-page chips, to position the buffer correctly? */
161
162 if (column != -1) {
David Woodhousefbad5692006-10-22 15:09:33 +0100163 writel(column, cafe->mmio + CAFE_NAND_ADDR1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100164 adrbytes = 2;
165 if (page_addr != -1)
166 goto write_adr2;
167 } else if (page_addr != -1) {
David Woodhousefbad5692006-10-22 15:09:33 +0100168 writel(page_addr & 0xffff, cafe->mmio + CAFE_NAND_ADDR1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100169 page_addr >>= 16;
170 write_adr2:
171 writel(page_addr, cafe->mmio+0x20);
172 adrbytes += 2;
173 if (mtd->size > mtd->writesize << 16)
174 adrbytes++;
175 }
176
177 cafe->data_pos = cafe->datalen = 0;
178
179 /* Set command valid bit */
180 ctl1 = 0x80000000 | command;
181
182 /* Set RD or WR bits as appropriate */
183 if (command == NAND_CMD_READID || command == NAND_CMD_STATUS) {
184 ctl1 |= (1<<26); /* rd */
185 /* Always 5 bytes, for now */
David Woodhouse8dd851d2006-10-20 02:11:40 +0100186 cafe->datalen = 4;
David Woodhouse5467fb02006-10-06 15:36:29 +0100187 /* And one address cycle -- even for STATUS, since the controller doesn't work without */
188 adrbytes = 1;
189 } else if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
190 command == NAND_CMD_READOOB || command == NAND_CMD_RNDOUT) {
191 ctl1 |= 1<<26; /* rd */
192 /* For now, assume just read to end of page */
193 cafe->datalen = mtd->writesize + mtd->oobsize - column;
194 } else if (command == NAND_CMD_SEQIN)
195 ctl1 |= 1<<25; /* wr */
196
197 /* Set number of address bytes */
198 if (adrbytes)
199 ctl1 |= ((adrbytes-1)|8) << 27;
200
201 if (command == NAND_CMD_SEQIN || command == NAND_CMD_ERASE1) {
202 /* Ignore the first command of a pair; the hardware
203 deals with them both at once, later */
204 cafe->ctl1 = ctl1;
205 cafe->ctl2 = 0;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100206 cafe_dev_dbg(&cafe->pdev->dev, "Setup for delayed command, ctl1 %08x, dlen %x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100207 cafe->ctl1, cafe->datalen);
208 return;
209 }
210 /* RNDOUT and READ0 commands need a following byte */
211 if (command == NAND_CMD_RNDOUT)
212 writel(cafe->ctl2 | 0x100 | NAND_CMD_RNDOUTSTART, cafe->mmio + CAFE_NAND_CTRL2);
213 else if (command == NAND_CMD_READ0 && mtd->writesize > 512)
214 writel(cafe->ctl2 | 0x100 | NAND_CMD_READSTART, cafe->mmio + CAFE_NAND_CTRL2);
215
216 do_command:
David Woodhousefbad5692006-10-22 15:09:33 +0100217#if 1
218 /* http://dev.laptop.org/ticket/200
219 ECC on read only works if we read precisely 0x80e bytes */
David Woodhouse04459d72006-10-22 02:18:48 +0100220 if (cafe->datalen == 2112)
221 cafe->datalen = 2062;
222#endif
David Woodhouse8dd851d2006-10-20 02:11:40 +0100223 cafe_dev_dbg(&cafe->pdev->dev, "dlen %x, ctl1 %x, ctl2 %x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100224 cafe->datalen, ctl1, readl(cafe->mmio+CAFE_NAND_CTRL2));
David Woodhousefbad5692006-10-22 15:09:33 +0100225
David Woodhouse5467fb02006-10-06 15:36:29 +0100226 /* NB: The datasheet lies -- we really should be subtracting 1 here */
227 writel(cafe->datalen, cafe->mmio + CAFE_NAND_DATA_LEN);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100228 writel(0x90000000, cafe->mmio + CAFE_NAND_IRQ);
David Woodhouse5467fb02006-10-06 15:36:29 +0100229 if (usedma && (ctl1 & (3<<25))) {
230 uint32_t dmactl = 0xc0000000 + cafe->datalen;
231 /* If WR or RD bits set, set up DMA */
232 if (ctl1 & (1<<26)) {
233 /* It's a read */
234 dmactl |= (1<<29);
235 /* ... so it's done when the DMA is done, not just
236 the command. */
237 doneint = 0x10000000;
238 }
David Woodhousefbad5692006-10-22 15:09:33 +0100239 writel(dmactl, cafe->mmio + CAFE_NAND_DMA_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100240 }
David Woodhouse5467fb02006-10-06 15:36:29 +0100241 cafe->datalen = 0;
242
243#if 0
David Woodhousefbad5692006-10-22 15:09:33 +0100244 { int i;
David Woodhouse5467fb02006-10-06 15:36:29 +0100245 printk("About to write command %08x\n", ctl1);
246 for (i=0; i< 0x5c; i+=4)
247 printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
David Woodhousefbad5692006-10-22 15:09:33 +0100248 }
David Woodhouse5467fb02006-10-06 15:36:29 +0100249#endif
250 writel(ctl1, cafe->mmio + CAFE_NAND_CTRL1);
251 /* Apply this short delay always to ensure that we do wait tWB in
252 * any case on any machine. */
253 ndelay(100);
254
255 if (1) {
David Woodhouse8dd851d2006-10-20 02:11:40 +0100256 int c = 500000;
David Woodhouse5467fb02006-10-06 15:36:29 +0100257 uint32_t irqs;
258
259 while (c--) {
David Woodhouse8dd851d2006-10-20 02:11:40 +0100260 irqs = readl(cafe->mmio + CAFE_NAND_IRQ);
David Woodhouse5467fb02006-10-06 15:36:29 +0100261 if (irqs & doneint)
262 break;
263 udelay(1);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100264 if (!(c % 100000))
265 cafe_dev_dbg(&cafe->pdev->dev, "Wait for ready, IRQ %x\n", irqs);
David Woodhouse5467fb02006-10-06 15:36:29 +0100266 cpu_relax();
267 }
David Woodhouse8dd851d2006-10-20 02:11:40 +0100268 writel(doneint, cafe->mmio + CAFE_NAND_IRQ);
269 cafe_dev_dbg(&cafe->pdev->dev, "Command %x completed after %d usec, irqs %x (%x)\n", command, 50000-c, irqs, readl(cafe->mmio + CAFE_NAND_IRQ));
David Woodhouse5467fb02006-10-06 15:36:29 +0100270 }
271
272
273 cafe->ctl2 &= ~(1<<8);
274 cafe->ctl2 &= ~(1<<30);
275
276 switch (command) {
277
278 case NAND_CMD_CACHEDPROG:
279 case NAND_CMD_PAGEPROG:
280 case NAND_CMD_ERASE1:
281 case NAND_CMD_ERASE2:
282 case NAND_CMD_SEQIN:
283 case NAND_CMD_RNDIN:
284 case NAND_CMD_STATUS:
285 case NAND_CMD_DEPLETE1:
286 case NAND_CMD_RNDOUT:
287 case NAND_CMD_STATUS_ERROR:
288 case NAND_CMD_STATUS_ERROR0:
289 case NAND_CMD_STATUS_ERROR1:
290 case NAND_CMD_STATUS_ERROR2:
291 case NAND_CMD_STATUS_ERROR3:
292 writel(cafe->ctl2, cafe->mmio + CAFE_NAND_CTRL2);
293 return;
294 }
295 nand_wait_ready(mtd);
296 writel(cafe->ctl2, cafe->mmio + CAFE_NAND_CTRL2);
297}
298
299static void cafe_select_chip(struct mtd_info *mtd, int chipnr)
300{
301 //struct cafe_priv *cafe = mtd->priv;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100302 // cafe_dev_dbg(&cafe->pdev->dev, "select_chip %d\n", chipnr);
David Woodhouse5467fb02006-10-06 15:36:29 +0100303}
David Woodhousefbad5692006-10-22 15:09:33 +0100304
David Woodhouse5467fb02006-10-06 15:36:29 +0100305static int cafe_nand_interrupt(int irq, void *id, struct pt_regs *regs)
306{
307 struct mtd_info *mtd = id;
308 struct cafe_priv *cafe = mtd->priv;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100309 uint32_t irqs = readl(cafe->mmio + CAFE_NAND_IRQ);
310 writel(irqs & ~0x90000000, cafe->mmio + CAFE_NAND_IRQ);
David Woodhouse5467fb02006-10-06 15:36:29 +0100311 if (!irqs)
312 return IRQ_NONE;
313
David Woodhouse8dd851d2006-10-20 02:11:40 +0100314 cafe_dev_dbg(&cafe->pdev->dev, "irq, bits %x (%x)\n", irqs, readl(cafe->mmio + CAFE_NAND_IRQ));
David Woodhouse5467fb02006-10-06 15:36:29 +0100315 return IRQ_HANDLED;
316}
317
318static void cafe_nand_bug(struct mtd_info *mtd)
319{
320 BUG();
321}
322
323static int cafe_nand_write_oob(struct mtd_info *mtd,
324 struct nand_chip *chip, int page)
325{
326 int status = 0;
327
David Woodhouse5467fb02006-10-06 15:36:29 +0100328 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
329 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
330 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
331 status = chip->waitfunc(mtd, chip);
332
333 return status & NAND_STATUS_FAIL ? -EIO : 0;
334}
335
336/* Don't use -- use nand_read_oob_std for now */
337static int cafe_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
338 int page, int sndcmd)
339{
340 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
341 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
342 return 1;
343}
344/**
345 * cafe_nand_read_page_syndrome - {REPLACABLE] hardware ecc syndrom based page read
346 * @mtd: mtd info structure
347 * @chip: nand chip info structure
348 * @buf: buffer to store read data
349 *
350 * The hw generator calculates the error syndrome automatically. Therefor
351 * we need a special oob layout and handling.
352 */
David Woodhousefbad5692006-10-22 15:09:33 +0100353
354static unsigned short cafe_empty_syndromes[8] = { 4095, 748, 2629, 2920, 875, 1454, 51, 1456 };
355
356static int is_all_ff(unsigned char *buf, int len)
357{
358 unsigned long *lbuf = (void *)buf;
359 int i;
360
361 for (i=0; i < (len/sizeof(long)); i++) {
362 if (lbuf[i] != ~0UL)
363 return 0;
364 }
365 i *= sizeof(long);
366 for (; i< len; i++) {
367 if (buf[i] != 0xff)
368 return 0;
369 }
370 return 1;
371}
372
David Woodhouse5467fb02006-10-06 15:36:29 +0100373static int cafe_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
374 uint8_t *buf)
375{
376 struct cafe_priv *cafe = mtd->priv;
377
David Woodhousefbad5692006-10-22 15:09:33 +0100378 cafe_dev_dbg(&cafe->pdev->dev, "ECC result %08x SYN1,2 %08x\n",
379 readl(cafe->mmio + CAFE_NAND_ECC_RESULT),
380 readl(cafe->mmio + CAFE_NAND_ECC_SYN01));
David Woodhouse5467fb02006-10-06 15:36:29 +0100381
382 chip->read_buf(mtd, buf, mtd->writesize);
383 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
384
David Woodhouse04459d72006-10-22 02:18:48 +0100385 if (readl(cafe->mmio + CAFE_NAND_ECC_RESULT) & (1<<18)) {
386 unsigned short syn[8];
387 int i;
388
389 for (i=0; i<8; i+=2) {
390 uint32_t tmp = readl(cafe->mmio + CAFE_NAND_ECC_SYN01 + (i*2));
391 syn[i] = tmp & 0xfff;
392 syn[i+1] = (tmp >> 16) & 0xfff;
393 }
394
David Woodhousefbad5692006-10-22 15:09:33 +0100395 /* FIXME: http://dev.laptop.org/ticket/215 */
396 if (!memcmp(syn, cafe_empty_syndromes, sizeof(syn))
397 && is_all_ff(chip->oob_poi, 14)
398 && is_all_ff(buf, mtd->writesize)) {
399 dev_dbg(&cafe->pdev->dev, "ECC error reported on empty block\n");
400 /* It was an empty block. Nothing to fix here except the hardware */
401 } else if ((i = cafe_correct_ecc(buf, syn)) < 0) {
David Woodhouse04459d72006-10-22 02:18:48 +0100402 dev_dbg(&cafe->pdev->dev, "Failed to correct ECC\n");
403 mtd->ecc_stats.failed++;
404 } else {
405 dev_dbg(&cafe->pdev->dev, "Corrected %d symbol errors\n", i);
406 mtd->ecc_stats.corrected += i;
407 }
408 }
409
410
David Woodhouse5467fb02006-10-06 15:36:29 +0100411 return 0;
412}
413
David Woodhouse8dd851d2006-10-20 02:11:40 +0100414static struct nand_ecclayout cafe_oobinfo_2048 = {
415 .eccbytes = 14,
416 .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
417 .oobfree = {{14, 50}}
418};
419
420/* Ick. The BBT code really ought to be able to work this bit out
David Woodhousefbad5692006-10-22 15:09:33 +0100421 for itself from the above, at least for the 2KiB case */
422static uint8_t cafe_bbt_pattern_2048[] = { 'B', 'b', 't', '0' };
423static uint8_t cafe_mirror_pattern_2048[] = { '1', 't', 'b', 'B' };
424
425static uint8_t cafe_bbt_pattern_512[] = { 0xBB };
426static uint8_t cafe_mirror_pattern_512[] = { 0xBC };
427
David Woodhouse8dd851d2006-10-20 02:11:40 +0100428
429static struct nand_bbt_descr cafe_bbt_main_descr_2048 = {
430 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
431 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
432 .offs = 14,
433 .len = 4,
434 .veroffs = 18,
435 .maxblocks = 4,
David Woodhousefbad5692006-10-22 15:09:33 +0100436 .pattern = cafe_bbt_pattern_2048
David Woodhouse8dd851d2006-10-20 02:11:40 +0100437};
438
439static struct nand_bbt_descr cafe_bbt_mirror_descr_2048 = {
440 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
441 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
442 .offs = 14,
443 .len = 4,
444 .veroffs = 18,
445 .maxblocks = 4,
David Woodhousefbad5692006-10-22 15:09:33 +0100446 .pattern = cafe_mirror_pattern_2048
David Woodhouse8dd851d2006-10-20 02:11:40 +0100447};
448
449static struct nand_ecclayout cafe_oobinfo_512 = {
450 .eccbytes = 14,
451 .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
452 .oobfree = {{14, 2}}
453};
454
David Woodhousefbad5692006-10-22 15:09:33 +0100455static struct nand_bbt_descr cafe_bbt_main_descr_512 = {
456 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
457 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
458 .offs = 14,
459 .len = 1,
460 .veroffs = 15,
461 .maxblocks = 4,
462 .pattern = cafe_bbt_pattern_512
463};
464
465static struct nand_bbt_descr cafe_bbt_mirror_descr_512 = {
466 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
467 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
468 .offs = 14,
469 .len = 1,
470 .veroffs = 15,
471 .maxblocks = 4,
472 .pattern = cafe_mirror_pattern_512
473};
474
475
David Woodhouse5467fb02006-10-06 15:36:29 +0100476static void cafe_nand_write_page_lowlevel(struct mtd_info *mtd,
477 struct nand_chip *chip, const uint8_t *buf)
478{
479 struct cafe_priv *cafe = mtd->priv;
480
David Woodhouse5467fb02006-10-06 15:36:29 +0100481 chip->write_buf(mtd, buf, mtd->writesize);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100482 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
David Woodhouse5467fb02006-10-06 15:36:29 +0100483
484 /* Set up ECC autogeneration */
485 cafe->ctl2 |= (1<<27) | (1<<30);
486 if (mtd->writesize == 2048)
487 cafe->ctl2 |= (1<<29);
488}
489
490static int cafe_nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
491 const uint8_t *buf, int page, int cached, int raw)
492{
493 int status;
494
David Woodhouse5467fb02006-10-06 15:36:29 +0100495 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
496
497 if (unlikely(raw))
498 chip->ecc.write_page_raw(mtd, chip, buf);
499 else
500 chip->ecc.write_page(mtd, chip, buf);
501
502 /*
503 * Cached progamming disabled for now, Not sure if its worth the
504 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
505 */
506 cached = 0;
507
508 if (!cached || !(chip->options & NAND_CACHEPRG)) {
509
510 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
511 status = chip->waitfunc(mtd, chip);
512 /*
513 * See if operation failed and additional status checks are
514 * available
515 */
516 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
517 status = chip->errstat(mtd, chip, FL_WRITING, status,
518 page);
519
520 if (status & NAND_STATUS_FAIL)
521 return -EIO;
522 } else {
523 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
524 status = chip->waitfunc(mtd, chip);
525 }
526
527#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
528 /* Send command to read back the data */
529 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
530
531 if (chip->verify_buf(mtd, buf, mtd->writesize))
532 return -EIO;
533#endif
534 return 0;
535}
536
David Woodhouse8dd851d2006-10-20 02:11:40 +0100537static int cafe_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
538{
539 return 0;
540}
David Woodhouse5467fb02006-10-06 15:36:29 +0100541
542static int __devinit cafe_nand_probe(struct pci_dev *pdev,
543 const struct pci_device_id *ent)
544{
545 struct mtd_info *mtd;
546 struct cafe_priv *cafe;
547 uint32_t ctrl;
548 int err = 0;
549
550 err = pci_enable_device(pdev);
551 if (err)
552 return err;
553
554 pci_set_master(pdev);
555
556 mtd = kzalloc(sizeof(*mtd) + sizeof(struct cafe_priv), GFP_KERNEL);
557 if (!mtd) {
558 dev_warn(&pdev->dev, "failed to alloc mtd_info\n");
559 return -ENOMEM;
560 }
561 cafe = (void *)(&mtd[1]);
562
563 mtd->priv = cafe;
564 mtd->owner = THIS_MODULE;
565
566 cafe->pdev = pdev;
567 cafe->mmio = pci_iomap(pdev, 0, 0);
568 if (!cafe->mmio) {
569 dev_warn(&pdev->dev, "failed to iomap\n");
570 err = -ENOMEM;
571 goto out_free_mtd;
572 }
573 cafe->dmabuf = dma_alloc_coherent(&cafe->pdev->dev, 2112 + sizeof(struct nand_buffers),
574 &cafe->dmaaddr, GFP_KERNEL);
575 if (!cafe->dmabuf) {
576 err = -ENOMEM;
577 goto out_ior;
578 }
579 cafe->nand.buffers = (void *)cafe->dmabuf + 2112;
580
581 cafe->nand.cmdfunc = cafe_nand_cmdfunc;
582 cafe->nand.dev_ready = cafe_device_ready;
583 cafe->nand.read_byte = cafe_read_byte;
584 cafe->nand.read_buf = cafe_read_buf;
585 cafe->nand.write_buf = cafe_write_buf;
586 cafe->nand.select_chip = cafe_select_chip;
587
588 cafe->nand.chip_delay = 0;
589
590 /* Enable the following for a flash based bad block table */
591 cafe->nand.options = NAND_USE_FLASH_BBT | NAND_NO_AUTOINCR | NAND_OWN_BUFFERS;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100592
593 if (skipbbt) {
594 cafe->nand.options |= NAND_SKIP_BBTSCAN;
595 cafe->nand.block_bad = cafe_nand_block_bad;
596 }
David Woodhouse5467fb02006-10-06 15:36:29 +0100597
598 /* Timings from Marvell's test code (not verified or calculated by us) */
599 writel(0xffffffff, cafe->mmio + CAFE_NAND_IRQ_MASK);
600#if 1
601 writel(0x01010a0a, cafe->mmio + CAFE_NAND_TIMING1);
602 writel(0x24121212, cafe->mmio + CAFE_NAND_TIMING2);
603 writel(0x11000000, cafe->mmio + CAFE_NAND_TIMING3);
604#else
605 writel(0xffffffff, cafe->mmio + CAFE_NAND_TIMING1);
606 writel(0xffffffff, cafe->mmio + CAFE_NAND_TIMING2);
607 writel(0xffffffff, cafe->mmio + CAFE_NAND_TIMING3);
608#endif
David Woodhouse8dd851d2006-10-20 02:11:40 +0100609 writel(0xffffffff, cafe->mmio + CAFE_NAND_IRQ_MASK);
David Woodhouse5467fb02006-10-06 15:36:29 +0100610 err = request_irq(pdev->irq, &cafe_nand_interrupt, SA_SHIRQ, "CAFE NAND", mtd);
611 if (err) {
612 dev_warn(&pdev->dev, "Could not register IRQ %d\n", pdev->irq);
613
614 goto out_free_dma;
615 }
616#if 1
617 /* Disable master reset, enable NAND clock */
618 ctrl = readl(cafe->mmio + 0x3004);
619 ctrl &= 0xffffeff0;
620 ctrl |= 0x00007000;
621 writel(ctrl | 0x05, cafe->mmio + 0x3004);
622 writel(ctrl | 0x0a, cafe->mmio + 0x3004);
David Woodhousefbad5692006-10-22 15:09:33 +0100623 writel(0, cafe->mmio + CAFE_NAND_DMA_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100624
625 writel(0x7006, cafe->mmio + 0x3004);
626 writel(0x700a, cafe->mmio + 0x3004);
627
628 /* Set up DMA address */
David Woodhousefbad5692006-10-22 15:09:33 +0100629 writel(cafe->dmaaddr & 0xffffffff, cafe->mmio + CAFE_NAND_DMA_ADDR0);
David Woodhouse5467fb02006-10-06 15:36:29 +0100630 if (sizeof(cafe->dmaaddr) > 4)
David Woodhousefbad5692006-10-22 15:09:33 +0100631 /* Shift in two parts to shut the compiler up */
632 writel((cafe->dmaaddr >> 16) >> 16, cafe->mmio + CAFE_NAND_DMA_ADDR1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100633 else
David Woodhousefbad5692006-10-22 15:09:33 +0100634 writel(0, cafe->mmio + CAFE_NAND_DMA_ADDR1);
635
David Woodhouse8dd851d2006-10-20 02:11:40 +0100636 cafe_dev_dbg(&cafe->pdev->dev, "Set DMA address to %x (virt %p)\n",
David Woodhousefbad5692006-10-22 15:09:33 +0100637 readl(cafe->mmio + CAFE_NAND_DMA_ADDR0), cafe->dmabuf);
David Woodhouse5467fb02006-10-06 15:36:29 +0100638
639 /* Enable NAND IRQ in global IRQ mask register */
640 writel(0x80000007, cafe->mmio + 0x300c);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100641 cafe_dev_dbg(&cafe->pdev->dev, "Control %x, IRQ mask %x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100642 readl(cafe->mmio + 0x3004), readl(cafe->mmio + 0x300c));
643#endif
644#if 1
645 mtd->writesize=2048;
646 mtd->oobsize = 0x40;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100647 memset(cafe->dmabuf, 0x5a, 2112);
David Woodhouse5467fb02006-10-06 15:36:29 +0100648 cafe->nand.cmdfunc(mtd, NAND_CMD_READID, 0, -1);
649 cafe->nand.read_byte(mtd);
650 cafe->nand.read_byte(mtd);
651 cafe->nand.read_byte(mtd);
652 cafe->nand.read_byte(mtd);
653 cafe->nand.read_byte(mtd);
654#endif
655#if 0
656 cafe->nand.cmdfunc(mtd, NAND_CMD_READ0, 0, 0);
657 // nand_wait_ready(mtd);
658 cafe->nand.read_byte(mtd);
659 cafe->nand.read_byte(mtd);
660 cafe->nand.read_byte(mtd);
661 cafe->nand.read_byte(mtd);
662#endif
663#if 0
664 writel(0x84600070, cafe->mmio);
665 udelay(10);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100666 cafe_dev_dbg(&cafe->pdev->dev, "Status %x\n", readl(cafe->mmio + 0x30));
David Woodhouse5467fb02006-10-06 15:36:29 +0100667#endif
668 /* Scan to find existance of the device */
669 if (nand_scan_ident(mtd, 1)) {
670 err = -ENXIO;
671 goto out_irq;
672 }
673
674 cafe->ctl2 = 1<<27; /* Reed-Solomon ECC */
675 if (mtd->writesize == 2048)
676 cafe->ctl2 |= 1<<29; /* 2KiB page size */
677
678 /* Set up ECC according to the type of chip we found */
David Woodhousefbad5692006-10-22 15:09:33 +0100679 if (mtd->writesize == 2048) {
David Woodhouse8dd851d2006-10-20 02:11:40 +0100680 cafe->nand.ecc.layout = &cafe_oobinfo_2048;
681 cafe->nand.bbt_td = &cafe_bbt_main_descr_2048;
682 cafe->nand.bbt_md = &cafe_bbt_mirror_descr_2048;
David Woodhousefbad5692006-10-22 15:09:33 +0100683 } else if (mtd->writesize == 512) {
684 cafe->nand.ecc.layout = &cafe_oobinfo_512;
685 cafe->nand.bbt_td = &cafe_bbt_main_descr_512;
686 cafe->nand.bbt_md = &cafe_bbt_mirror_descr_512;
David Woodhouse5467fb02006-10-06 15:36:29 +0100687 } else {
David Woodhousefbad5692006-10-22 15:09:33 +0100688 printk(KERN_WARNING "Unexpected NAND flash writesize %d. Aborting\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100689 mtd->writesize);
David Woodhousefbad5692006-10-22 15:09:33 +0100690 goto out_irq;
David Woodhouse5467fb02006-10-06 15:36:29 +0100691 }
David Woodhousefbad5692006-10-22 15:09:33 +0100692 cafe->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
693 cafe->nand.ecc.size = mtd->writesize;
694 cafe->nand.ecc.bytes = 14;
695 cafe->nand.ecc.hwctl = (void *)cafe_nand_bug;
696 cafe->nand.ecc.calculate = (void *)cafe_nand_bug;
697 cafe->nand.ecc.correct = (void *)cafe_nand_bug;
698 cafe->nand.write_page = cafe_nand_write_page;
699 cafe->nand.ecc.write_page = cafe_nand_write_page_lowlevel;
700 cafe->nand.ecc.write_oob = cafe_nand_write_oob;
701 cafe->nand.ecc.read_page = cafe_nand_read_page;
702 cafe->nand.ecc.read_oob = cafe_nand_read_oob;
David Woodhouse5467fb02006-10-06 15:36:29 +0100703
704 err = nand_scan_tail(mtd);
705 if (err)
706 goto out_irq;
707
David Woodhouse5467fb02006-10-06 15:36:29 +0100708 pci_set_drvdata(pdev, mtd);
709 add_mtd_device(mtd);
710 goto out;
711
712 out_irq:
713 /* Disable NAND IRQ in global IRQ mask register */
714 writel(~1 & readl(cafe->mmio + 0x300c), cafe->mmio + 0x300c);
715 free_irq(pdev->irq, mtd);
716 out_free_dma:
717 dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
718 out_ior:
719 pci_iounmap(pdev, cafe->mmio);
720 out_free_mtd:
721 kfree(mtd);
722 out:
723 return err;
724}
725
726static void __devexit cafe_nand_remove(struct pci_dev *pdev)
727{
728 struct mtd_info *mtd = pci_get_drvdata(pdev);
729 struct cafe_priv *cafe = mtd->priv;
730
731 del_mtd_device(mtd);
732 /* Disable NAND IRQ in global IRQ mask register */
733 writel(~1 & readl(cafe->mmio + 0x300c), cafe->mmio + 0x300c);
734 free_irq(pdev->irq, mtd);
735 nand_release(mtd);
736 pci_iounmap(pdev, cafe->mmio);
737 dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
738 kfree(mtd);
739}
740
741static struct pci_device_id cafe_nand_tbl[] = {
742 { 0x11ab, 0x4100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MEMORY_FLASH << 8, 0xFFFF0 }
743};
744
745MODULE_DEVICE_TABLE(pci, cafe_nand_tbl);
746
747static struct pci_driver cafe_nand_pci_driver = {
748 .name = "CAFÉ NAND",
749 .id_table = cafe_nand_tbl,
750 .probe = cafe_nand_probe,
751 .remove = __devexit_p(cafe_nand_remove),
752#ifdef CONFIG_PMx
753 .suspend = cafe_nand_suspend,
754 .resume = cafe_nand_resume,
755#endif
756};
757
758static int cafe_nand_init(void)
759{
760 return pci_register_driver(&cafe_nand_pci_driver);
761}
762
763static void cafe_nand_exit(void)
764{
765 pci_unregister_driver(&cafe_nand_pci_driver);
766}
767module_init(cafe_nand_init);
768module_exit(cafe_nand_exit);
769
770MODULE_LICENSE("GPL");
771MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
772MODULE_DESCRIPTION("NAND flash driver for OLPC CAFE chip");
773
774/* Correct ECC for 2048 bytes of 0xff:
775 41 a0 71 65 54 27 f3 93 ec a9 be ed 0b a1 */
David Woodhouse8dd851d2006-10-20 02:11:40 +0100776
777/* dwmw2's B-test board, in case of completely screwing it:
778Bad eraseblock 2394 at 0x12b40000
779Bad eraseblock 2627 at 0x14860000
780Bad eraseblock 3349 at 0x1a2a0000
781*/