blob: d894c7286aab75742624edfc8aad24ee95074c7d [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 Woodhouseb478c772006-10-27 14:50:04 +030061static int usedma = 1;
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 Woodhouseb478c772006-10-27 14:50:04 +030070static int checkecc = 1;
David Woodhouse470b0a92006-10-23 14:29:04 +010071module_param(checkecc, int, 0644);
72
David Woodhouseb478c772006-10-27 14:50:04 +030073static int slowtiming = 0;
74module_param(slowtiming, int, 0644);
75
David Woodhouse04459d72006-10-22 02:18:48 +010076/* Hrm. Why isn't this already conditional on something in the struct device? */
David Woodhouse8dd851d2006-10-20 02:11:40 +010077#define cafe_dev_dbg(dev, args...) do { if (debug) dev_dbg(dev, ##args); } while(0)
78
79
David Woodhouse5467fb02006-10-06 15:36:29 +010080static int cafe_device_ready(struct mtd_info *mtd)
81{
82 struct cafe_priv *cafe = mtd->priv;
83 int result = !!(readl(cafe->mmio + CAFE_NAND_STATUS) | 0x40000000);
David Woodhouse8dd851d2006-10-20 02:11:40 +010084 uint32_t irqs = readl(cafe->mmio + CAFE_NAND_IRQ);
David Woodhousefbad5692006-10-22 15:09:33 +010085
David Woodhouse8dd851d2006-10-20 02:11:40 +010086 writel(irqs, cafe->mmio+CAFE_NAND_IRQ);
David Woodhousefbad5692006-10-22 15:09:33 +010087
David Woodhouse8dd851d2006-10-20 02:11:40 +010088 cafe_dev_dbg(&cafe->pdev->dev, "NAND device is%s ready, IRQ %x (%x) (%x,%x)\n",
89 result?"":" not", irqs, readl(cafe->mmio + CAFE_NAND_IRQ),
David Woodhouse5467fb02006-10-06 15:36:29 +010090 readl(cafe->mmio + 0x3008), readl(cafe->mmio + 0x300c));
David Woodhousefbad5692006-10-22 15:09:33 +010091
David Woodhouse5467fb02006-10-06 15:36:29 +010092 return result;
93}
94
95
96static void cafe_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
97{
98 struct cafe_priv *cafe = mtd->priv;
99
100 if (usedma)
101 memcpy(cafe->dmabuf + cafe->datalen, buf, len);
102 else
103 memcpy_toio(cafe->mmio + CAFE_NAND_WRITE_DATA + cafe->datalen, buf, len);
David Woodhousefbad5692006-10-22 15:09:33 +0100104
David Woodhouse5467fb02006-10-06 15:36:29 +0100105 cafe->datalen += len;
106
David Woodhouse8dd851d2006-10-20 02:11:40 +0100107 cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes to write buffer. datalen 0x%x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100108 len, cafe->datalen);
109}
110
111static void cafe_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
112{
113 struct cafe_priv *cafe = mtd->priv;
114
115 if (usedma)
116 memcpy(buf, cafe->dmabuf + cafe->datalen, len);
117 else
118 memcpy_fromio(buf, cafe->mmio + CAFE_NAND_READ_DATA + cafe->datalen, len);
119
David Woodhouse8dd851d2006-10-20 02:11:40 +0100120 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 +0100121 len, cafe->datalen);
122 cafe->datalen += len;
123}
124
125static uint8_t cafe_read_byte(struct mtd_info *mtd)
126{
127 struct cafe_priv *cafe = mtd->priv;
128 uint8_t d;
129
130 cafe_read_buf(mtd, &d, 1);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100131 cafe_dev_dbg(&cafe->pdev->dev, "Read %02x\n", d);
David Woodhouse5467fb02006-10-06 15:36:29 +0100132
133 return d;
134}
135
136static void cafe_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
137 int column, int page_addr)
138{
139 struct cafe_priv *cafe = mtd->priv;
140 int adrbytes = 0;
141 uint32_t ctl1;
142 uint32_t doneint = 0x80000000;
David Woodhouse5467fb02006-10-06 15:36:29 +0100143
David Woodhouse8dd851d2006-10-20 02:11:40 +0100144 cafe_dev_dbg(&cafe->pdev->dev, "cmdfunc %02x, 0x%x, 0x%x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100145 command, column, page_addr);
146
147 if (command == NAND_CMD_ERASE2 || command == NAND_CMD_PAGEPROG) {
148 /* Second half of a command we already calculated */
David Woodhousefbad5692006-10-22 15:09:33 +0100149 writel(cafe->ctl2 | 0x100 | command, cafe->mmio + CAFE_NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100150 ctl1 = cafe->ctl1;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100151 cafe_dev_dbg(&cafe->pdev->dev, "Continue command, ctl1 %08x, #data %d\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100152 cafe->ctl1, cafe->nr_data);
153 goto do_command;
154 }
155 /* Reset ECC engine */
156 writel(0, cafe->mmio + CAFE_NAND_CTRL2);
157
158 /* Emulate NAND_CMD_READOOB on large-page chips */
159 if (mtd->writesize > 512 &&
160 command == NAND_CMD_READOOB) {
161 column += mtd->writesize;
162 command = NAND_CMD_READ0;
163 }
164
165 /* FIXME: Do we need to send read command before sending data
166 for small-page chips, to position the buffer correctly? */
167
168 if (column != -1) {
David Woodhousefbad5692006-10-22 15:09:33 +0100169 writel(column, cafe->mmio + CAFE_NAND_ADDR1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100170 adrbytes = 2;
171 if (page_addr != -1)
172 goto write_adr2;
173 } else if (page_addr != -1) {
David Woodhousefbad5692006-10-22 15:09:33 +0100174 writel(page_addr & 0xffff, cafe->mmio + CAFE_NAND_ADDR1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100175 page_addr >>= 16;
176 write_adr2:
177 writel(page_addr, cafe->mmio+0x20);
178 adrbytes += 2;
179 if (mtd->size > mtd->writesize << 16)
180 adrbytes++;
181 }
182
183 cafe->data_pos = cafe->datalen = 0;
184
185 /* Set command valid bit */
186 ctl1 = 0x80000000 | command;
187
188 /* Set RD or WR bits as appropriate */
189 if (command == NAND_CMD_READID || command == NAND_CMD_STATUS) {
190 ctl1 |= (1<<26); /* rd */
191 /* Always 5 bytes, for now */
David Woodhouse8dd851d2006-10-20 02:11:40 +0100192 cafe->datalen = 4;
David Woodhouse5467fb02006-10-06 15:36:29 +0100193 /* And one address cycle -- even for STATUS, since the controller doesn't work without */
194 adrbytes = 1;
195 } else if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
196 command == NAND_CMD_READOOB || command == NAND_CMD_RNDOUT) {
197 ctl1 |= 1<<26; /* rd */
198 /* For now, assume just read to end of page */
199 cafe->datalen = mtd->writesize + mtd->oobsize - column;
200 } else if (command == NAND_CMD_SEQIN)
201 ctl1 |= 1<<25; /* wr */
202
203 /* Set number of address bytes */
204 if (adrbytes)
205 ctl1 |= ((adrbytes-1)|8) << 27;
206
207 if (command == NAND_CMD_SEQIN || command == NAND_CMD_ERASE1) {
208 /* Ignore the first command of a pair; the hardware
209 deals with them both at once, later */
210 cafe->ctl1 = ctl1;
211 cafe->ctl2 = 0;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100212 cafe_dev_dbg(&cafe->pdev->dev, "Setup for delayed command, ctl1 %08x, dlen %x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100213 cafe->ctl1, cafe->datalen);
214 return;
215 }
216 /* RNDOUT and READ0 commands need a following byte */
217 if (command == NAND_CMD_RNDOUT)
218 writel(cafe->ctl2 | 0x100 | NAND_CMD_RNDOUTSTART, cafe->mmio + CAFE_NAND_CTRL2);
219 else if (command == NAND_CMD_READ0 && mtd->writesize > 512)
220 writel(cafe->ctl2 | 0x100 | NAND_CMD_READSTART, cafe->mmio + CAFE_NAND_CTRL2);
221
222 do_command:
David Woodhouse470b0a92006-10-23 14:29:04 +0100223#if 0
David Woodhousefbad5692006-10-22 15:09:33 +0100224 /* http://dev.laptop.org/ticket/200
225 ECC on read only works if we read precisely 0x80e bytes */
David Woodhouse04459d72006-10-22 02:18:48 +0100226 if (cafe->datalen == 2112)
227 cafe->datalen = 2062;
228#endif
David Woodhouse8dd851d2006-10-20 02:11:40 +0100229 cafe_dev_dbg(&cafe->pdev->dev, "dlen %x, ctl1 %x, ctl2 %x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100230 cafe->datalen, ctl1, readl(cafe->mmio+CAFE_NAND_CTRL2));
David Woodhousefbad5692006-10-22 15:09:33 +0100231
David Woodhouse5467fb02006-10-06 15:36:29 +0100232 /* NB: The datasheet lies -- we really should be subtracting 1 here */
233 writel(cafe->datalen, cafe->mmio + CAFE_NAND_DATA_LEN);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100234 writel(0x90000000, cafe->mmio + CAFE_NAND_IRQ);
David Woodhouse5467fb02006-10-06 15:36:29 +0100235 if (usedma && (ctl1 & (3<<25))) {
236 uint32_t dmactl = 0xc0000000 + cafe->datalen;
237 /* If WR or RD bits set, set up DMA */
238 if (ctl1 & (1<<26)) {
239 /* It's a read */
240 dmactl |= (1<<29);
241 /* ... so it's done when the DMA is done, not just
242 the command. */
243 doneint = 0x10000000;
244 }
David Woodhousefbad5692006-10-22 15:09:33 +0100245 writel(dmactl, cafe->mmio + CAFE_NAND_DMA_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100246 }
David Woodhouse5467fb02006-10-06 15:36:29 +0100247 cafe->datalen = 0;
248
249#if 0
David Woodhousefbad5692006-10-22 15:09:33 +0100250 { int i;
David Woodhouse5467fb02006-10-06 15:36:29 +0100251 printk("About to write command %08x\n", ctl1);
252 for (i=0; i< 0x5c; i+=4)
253 printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
David Woodhousefbad5692006-10-22 15:09:33 +0100254 }
David Woodhouse5467fb02006-10-06 15:36:29 +0100255#endif
256 writel(ctl1, cafe->mmio + CAFE_NAND_CTRL1);
257 /* Apply this short delay always to ensure that we do wait tWB in
258 * any case on any machine. */
259 ndelay(100);
260
261 if (1) {
David Woodhouse8dd851d2006-10-20 02:11:40 +0100262 int c = 500000;
David Woodhouse5467fb02006-10-06 15:36:29 +0100263 uint32_t irqs;
264
265 while (c--) {
David Woodhouse8dd851d2006-10-20 02:11:40 +0100266 irqs = readl(cafe->mmio + CAFE_NAND_IRQ);
David Woodhouse5467fb02006-10-06 15:36:29 +0100267 if (irqs & doneint)
268 break;
269 udelay(1);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100270 if (!(c % 100000))
271 cafe_dev_dbg(&cafe->pdev->dev, "Wait for ready, IRQ %x\n", irqs);
David Woodhouse5467fb02006-10-06 15:36:29 +0100272 cpu_relax();
273 }
David Woodhouse8dd851d2006-10-20 02:11:40 +0100274 writel(doneint, cafe->mmio + CAFE_NAND_IRQ);
275 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 +0100276 }
277
278
279 cafe->ctl2 &= ~(1<<8);
280 cafe->ctl2 &= ~(1<<30);
281
282 switch (command) {
283
284 case NAND_CMD_CACHEDPROG:
285 case NAND_CMD_PAGEPROG:
286 case NAND_CMD_ERASE1:
287 case NAND_CMD_ERASE2:
288 case NAND_CMD_SEQIN:
289 case NAND_CMD_RNDIN:
290 case NAND_CMD_STATUS:
291 case NAND_CMD_DEPLETE1:
292 case NAND_CMD_RNDOUT:
293 case NAND_CMD_STATUS_ERROR:
294 case NAND_CMD_STATUS_ERROR0:
295 case NAND_CMD_STATUS_ERROR1:
296 case NAND_CMD_STATUS_ERROR2:
297 case NAND_CMD_STATUS_ERROR3:
298 writel(cafe->ctl2, cafe->mmio + CAFE_NAND_CTRL2);
299 return;
300 }
301 nand_wait_ready(mtd);
302 writel(cafe->ctl2, cafe->mmio + CAFE_NAND_CTRL2);
303}
304
305static void cafe_select_chip(struct mtd_info *mtd, int chipnr)
306{
307 //struct cafe_priv *cafe = mtd->priv;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100308 // cafe_dev_dbg(&cafe->pdev->dev, "select_chip %d\n", chipnr);
David Woodhouse5467fb02006-10-06 15:36:29 +0100309}
David Woodhousefbad5692006-10-22 15:09:33 +0100310
David Woodhouse5467fb02006-10-06 15:36:29 +0100311static int cafe_nand_interrupt(int irq, void *id, struct pt_regs *regs)
312{
313 struct mtd_info *mtd = id;
314 struct cafe_priv *cafe = mtd->priv;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100315 uint32_t irqs = readl(cafe->mmio + CAFE_NAND_IRQ);
316 writel(irqs & ~0x90000000, cafe->mmio + CAFE_NAND_IRQ);
David Woodhouse5467fb02006-10-06 15:36:29 +0100317 if (!irqs)
318 return IRQ_NONE;
319
David Woodhouse8dd851d2006-10-20 02:11:40 +0100320 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 +0100321 return IRQ_HANDLED;
322}
323
324static void cafe_nand_bug(struct mtd_info *mtd)
325{
326 BUG();
327}
328
329static int cafe_nand_write_oob(struct mtd_info *mtd,
330 struct nand_chip *chip, int page)
331{
332 int status = 0;
333
David Woodhouse5467fb02006-10-06 15:36:29 +0100334 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
335 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
336 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
337 status = chip->waitfunc(mtd, chip);
338
339 return status & NAND_STATUS_FAIL ? -EIO : 0;
340}
341
342/* Don't use -- use nand_read_oob_std for now */
343static int cafe_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
344 int page, int sndcmd)
345{
346 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
347 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
348 return 1;
349}
350/**
351 * cafe_nand_read_page_syndrome - {REPLACABLE] hardware ecc syndrom based page read
352 * @mtd: mtd info structure
353 * @chip: nand chip info structure
354 * @buf: buffer to store read data
355 *
356 * The hw generator calculates the error syndrome automatically. Therefor
357 * we need a special oob layout and handling.
358 */
David Woodhousefbad5692006-10-22 15:09:33 +0100359
360static unsigned short cafe_empty_syndromes[8] = { 4095, 748, 2629, 2920, 875, 1454, 51, 1456 };
361
362static int is_all_ff(unsigned char *buf, int len)
363{
364 unsigned long *lbuf = (void *)buf;
365 int i;
366
367 for (i=0; i < (len/sizeof(long)); i++) {
368 if (lbuf[i] != ~0UL)
369 return 0;
370 }
371 i *= sizeof(long);
372 for (; i< len; i++) {
373 if (buf[i] != 0xff)
374 return 0;
375 }
376 return 1;
377}
378
David Woodhouse5467fb02006-10-06 15:36:29 +0100379static int cafe_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
380 uint8_t *buf)
381{
382 struct cafe_priv *cafe = mtd->priv;
383
David Woodhousefbad5692006-10-22 15:09:33 +0100384 cafe_dev_dbg(&cafe->pdev->dev, "ECC result %08x SYN1,2 %08x\n",
385 readl(cafe->mmio + CAFE_NAND_ECC_RESULT),
386 readl(cafe->mmio + CAFE_NAND_ECC_SYN01));
David Woodhouse5467fb02006-10-06 15:36:29 +0100387
388 chip->read_buf(mtd, buf, mtd->writesize);
389 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
390
David Woodhouse470b0a92006-10-23 14:29:04 +0100391 if (checkecc && readl(cafe->mmio + CAFE_NAND_ECC_RESULT) & (1<<18)) {
David Woodhouse04459d72006-10-22 02:18:48 +0100392 unsigned short syn[8];
393 int i;
394
395 for (i=0; i<8; i+=2) {
396 uint32_t tmp = readl(cafe->mmio + CAFE_NAND_ECC_SYN01 + (i*2));
397 syn[i] = tmp & 0xfff;
398 syn[i+1] = (tmp >> 16) & 0xfff;
399 }
400
David Woodhousefbad5692006-10-22 15:09:33 +0100401 /* FIXME: http://dev.laptop.org/ticket/215 */
402 if (!memcmp(syn, cafe_empty_syndromes, sizeof(syn))
403 && is_all_ff(chip->oob_poi, 14)
404 && is_all_ff(buf, mtd->writesize)) {
405 dev_dbg(&cafe->pdev->dev, "ECC error reported on empty block\n");
406 /* It was an empty block. Nothing to fix here except the hardware */
407 } else if ((i = cafe_correct_ecc(buf, syn)) < 0) {
David Woodhouse04459d72006-10-22 02:18:48 +0100408 dev_dbg(&cafe->pdev->dev, "Failed to correct ECC\n");
409 mtd->ecc_stats.failed++;
410 } else {
411 dev_dbg(&cafe->pdev->dev, "Corrected %d symbol errors\n", i);
412 mtd->ecc_stats.corrected += i;
413 }
414 }
415
416
David Woodhouse5467fb02006-10-06 15:36:29 +0100417 return 0;
418}
419
David Woodhouse8dd851d2006-10-20 02:11:40 +0100420static struct nand_ecclayout cafe_oobinfo_2048 = {
421 .eccbytes = 14,
422 .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
423 .oobfree = {{14, 50}}
424};
425
426/* Ick. The BBT code really ought to be able to work this bit out
David Woodhousefbad5692006-10-22 15:09:33 +0100427 for itself from the above, at least for the 2KiB case */
428static uint8_t cafe_bbt_pattern_2048[] = { 'B', 'b', 't', '0' };
429static uint8_t cafe_mirror_pattern_2048[] = { '1', 't', 'b', 'B' };
430
431static uint8_t cafe_bbt_pattern_512[] = { 0xBB };
432static uint8_t cafe_mirror_pattern_512[] = { 0xBC };
433
David Woodhouse8dd851d2006-10-20 02:11:40 +0100434
435static struct nand_bbt_descr cafe_bbt_main_descr_2048 = {
436 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
437 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
438 .offs = 14,
439 .len = 4,
440 .veroffs = 18,
441 .maxblocks = 4,
David Woodhousefbad5692006-10-22 15:09:33 +0100442 .pattern = cafe_bbt_pattern_2048
David Woodhouse8dd851d2006-10-20 02:11:40 +0100443};
444
445static struct nand_bbt_descr cafe_bbt_mirror_descr_2048 = {
446 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
447 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
448 .offs = 14,
449 .len = 4,
450 .veroffs = 18,
451 .maxblocks = 4,
David Woodhousefbad5692006-10-22 15:09:33 +0100452 .pattern = cafe_mirror_pattern_2048
David Woodhouse8dd851d2006-10-20 02:11:40 +0100453};
454
455static struct nand_ecclayout cafe_oobinfo_512 = {
456 .eccbytes = 14,
457 .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
458 .oobfree = {{14, 2}}
459};
460
David Woodhousefbad5692006-10-22 15:09:33 +0100461static struct nand_bbt_descr cafe_bbt_main_descr_512 = {
462 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
463 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
464 .offs = 14,
465 .len = 1,
466 .veroffs = 15,
467 .maxblocks = 4,
468 .pattern = cafe_bbt_pattern_512
469};
470
471static struct nand_bbt_descr cafe_bbt_mirror_descr_512 = {
472 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
473 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
474 .offs = 14,
475 .len = 1,
476 .veroffs = 15,
477 .maxblocks = 4,
478 .pattern = cafe_mirror_pattern_512
479};
480
481
David Woodhouse5467fb02006-10-06 15:36:29 +0100482static void cafe_nand_write_page_lowlevel(struct mtd_info *mtd,
483 struct nand_chip *chip, const uint8_t *buf)
484{
485 struct cafe_priv *cafe = mtd->priv;
486
David Woodhouse5467fb02006-10-06 15:36:29 +0100487 chip->write_buf(mtd, buf, mtd->writesize);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100488 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
David Woodhouse5467fb02006-10-06 15:36:29 +0100489
490 /* Set up ECC autogeneration */
491 cafe->ctl2 |= (1<<27) | (1<<30);
492 if (mtd->writesize == 2048)
493 cafe->ctl2 |= (1<<29);
494}
495
496static int cafe_nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
497 const uint8_t *buf, int page, int cached, int raw)
498{
499 int status;
500
David Woodhouse5467fb02006-10-06 15:36:29 +0100501 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
502
503 if (unlikely(raw))
504 chip->ecc.write_page_raw(mtd, chip, buf);
505 else
506 chip->ecc.write_page(mtd, chip, buf);
507
508 /*
509 * Cached progamming disabled for now, Not sure if its worth the
510 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
511 */
512 cached = 0;
513
514 if (!cached || !(chip->options & NAND_CACHEPRG)) {
515
516 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
517 status = chip->waitfunc(mtd, chip);
518 /*
519 * See if operation failed and additional status checks are
520 * available
521 */
522 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
523 status = chip->errstat(mtd, chip, FL_WRITING, status,
524 page);
525
526 if (status & NAND_STATUS_FAIL)
527 return -EIO;
528 } else {
529 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
530 status = chip->waitfunc(mtd, chip);
531 }
532
533#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
534 /* Send command to read back the data */
535 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
536
537 if (chip->verify_buf(mtd, buf, mtd->writesize))
538 return -EIO;
539#endif
540 return 0;
541}
542
David Woodhouse8dd851d2006-10-20 02:11:40 +0100543static int cafe_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
544{
545 return 0;
546}
David Woodhouse5467fb02006-10-06 15:36:29 +0100547
548static int __devinit cafe_nand_probe(struct pci_dev *pdev,
549 const struct pci_device_id *ent)
550{
551 struct mtd_info *mtd;
552 struct cafe_priv *cafe;
553 uint32_t ctrl;
554 int err = 0;
555
556 err = pci_enable_device(pdev);
557 if (err)
558 return err;
559
560 pci_set_master(pdev);
561
562 mtd = kzalloc(sizeof(*mtd) + sizeof(struct cafe_priv), GFP_KERNEL);
563 if (!mtd) {
564 dev_warn(&pdev->dev, "failed to alloc mtd_info\n");
565 return -ENOMEM;
566 }
567 cafe = (void *)(&mtd[1]);
568
569 mtd->priv = cafe;
570 mtd->owner = THIS_MODULE;
571
572 cafe->pdev = pdev;
573 cafe->mmio = pci_iomap(pdev, 0, 0);
574 if (!cafe->mmio) {
575 dev_warn(&pdev->dev, "failed to iomap\n");
576 err = -ENOMEM;
577 goto out_free_mtd;
578 }
579 cafe->dmabuf = dma_alloc_coherent(&cafe->pdev->dev, 2112 + sizeof(struct nand_buffers),
580 &cafe->dmaaddr, GFP_KERNEL);
581 if (!cafe->dmabuf) {
582 err = -ENOMEM;
583 goto out_ior;
584 }
585 cafe->nand.buffers = (void *)cafe->dmabuf + 2112;
586
587 cafe->nand.cmdfunc = cafe_nand_cmdfunc;
588 cafe->nand.dev_ready = cafe_device_ready;
589 cafe->nand.read_byte = cafe_read_byte;
590 cafe->nand.read_buf = cafe_read_buf;
591 cafe->nand.write_buf = cafe_write_buf;
592 cafe->nand.select_chip = cafe_select_chip;
593
594 cafe->nand.chip_delay = 0;
595
596 /* Enable the following for a flash based bad block table */
597 cafe->nand.options = NAND_USE_FLASH_BBT | NAND_NO_AUTOINCR | NAND_OWN_BUFFERS;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100598
599 if (skipbbt) {
600 cafe->nand.options |= NAND_SKIP_BBTSCAN;
601 cafe->nand.block_bad = cafe_nand_block_bad;
602 }
David Woodhouse5467fb02006-10-06 15:36:29 +0100603
David Woodhousedcc41bc2006-10-27 09:55:34 +0300604 /* Start off by resetting the NAND controller completely */
605 writel(1, cafe->mmio + 0x3034);
606 writel(0, cafe->mmio + 0x3034);
607
David Woodhouse5467fb02006-10-06 15:36:29 +0100608 /* Timings from Marvell's test code (not verified or calculated by us) */
609 writel(0xffffffff, cafe->mmio + CAFE_NAND_IRQ_MASK);
David Woodhouseb478c772006-10-27 14:50:04 +0300610
611 if (!slowtiming) {
612 writel(0x01010a0a, cafe->mmio + CAFE_NAND_TIMING1);
613 writel(0x24121212, cafe->mmio + CAFE_NAND_TIMING2);
614 writel(0x11000000, cafe->mmio + CAFE_NAND_TIMING3);
615 } else {
616 writel(0xffffffff, cafe->mmio + CAFE_NAND_TIMING1);
617 writel(0xffffffff, cafe->mmio + CAFE_NAND_TIMING2);
618 writel(0xffffffff, cafe->mmio + CAFE_NAND_TIMING3);
619 }
David Woodhouse8dd851d2006-10-20 02:11:40 +0100620 writel(0xffffffff, cafe->mmio + CAFE_NAND_IRQ_MASK);
David Woodhouse5467fb02006-10-06 15:36:29 +0100621 err = request_irq(pdev->irq, &cafe_nand_interrupt, SA_SHIRQ, "CAFE NAND", mtd);
622 if (err) {
623 dev_warn(&pdev->dev, "Could not register IRQ %d\n", pdev->irq);
624
625 goto out_free_dma;
626 }
627#if 1
628 /* Disable master reset, enable NAND clock */
629 ctrl = readl(cafe->mmio + 0x3004);
630 ctrl &= 0xffffeff0;
631 ctrl |= 0x00007000;
632 writel(ctrl | 0x05, cafe->mmio + 0x3004);
633 writel(ctrl | 0x0a, cafe->mmio + 0x3004);
David Woodhousefbad5692006-10-22 15:09:33 +0100634 writel(0, cafe->mmio + CAFE_NAND_DMA_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100635
636 writel(0x7006, cafe->mmio + 0x3004);
637 writel(0x700a, cafe->mmio + 0x3004);
638
639 /* Set up DMA address */
David Woodhousefbad5692006-10-22 15:09:33 +0100640 writel(cafe->dmaaddr & 0xffffffff, cafe->mmio + CAFE_NAND_DMA_ADDR0);
David Woodhouse5467fb02006-10-06 15:36:29 +0100641 if (sizeof(cafe->dmaaddr) > 4)
David Woodhousefbad5692006-10-22 15:09:33 +0100642 /* Shift in two parts to shut the compiler up */
643 writel((cafe->dmaaddr >> 16) >> 16, cafe->mmio + CAFE_NAND_DMA_ADDR1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100644 else
David Woodhousefbad5692006-10-22 15:09:33 +0100645 writel(0, cafe->mmio + CAFE_NAND_DMA_ADDR1);
646
David Woodhouse8dd851d2006-10-20 02:11:40 +0100647 cafe_dev_dbg(&cafe->pdev->dev, "Set DMA address to %x (virt %p)\n",
David Woodhousefbad5692006-10-22 15:09:33 +0100648 readl(cafe->mmio + CAFE_NAND_DMA_ADDR0), cafe->dmabuf);
David Woodhouse5467fb02006-10-06 15:36:29 +0100649
650 /* Enable NAND IRQ in global IRQ mask register */
651 writel(0x80000007, cafe->mmio + 0x300c);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100652 cafe_dev_dbg(&cafe->pdev->dev, "Control %x, IRQ mask %x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100653 readl(cafe->mmio + 0x3004), readl(cafe->mmio + 0x300c));
654#endif
655#if 1
656 mtd->writesize=2048;
657 mtd->oobsize = 0x40;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100658 memset(cafe->dmabuf, 0x5a, 2112);
David Woodhouse5467fb02006-10-06 15:36:29 +0100659 cafe->nand.cmdfunc(mtd, NAND_CMD_READID, 0, -1);
660 cafe->nand.read_byte(mtd);
661 cafe->nand.read_byte(mtd);
662 cafe->nand.read_byte(mtd);
663 cafe->nand.read_byte(mtd);
664 cafe->nand.read_byte(mtd);
665#endif
666#if 0
667 cafe->nand.cmdfunc(mtd, NAND_CMD_READ0, 0, 0);
668 // nand_wait_ready(mtd);
669 cafe->nand.read_byte(mtd);
670 cafe->nand.read_byte(mtd);
671 cafe->nand.read_byte(mtd);
672 cafe->nand.read_byte(mtd);
673#endif
674#if 0
675 writel(0x84600070, cafe->mmio);
676 udelay(10);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100677 cafe_dev_dbg(&cafe->pdev->dev, "Status %x\n", readl(cafe->mmio + 0x30));
David Woodhouse5467fb02006-10-06 15:36:29 +0100678#endif
679 /* Scan to find existance of the device */
680 if (nand_scan_ident(mtd, 1)) {
681 err = -ENXIO;
682 goto out_irq;
683 }
684
685 cafe->ctl2 = 1<<27; /* Reed-Solomon ECC */
686 if (mtd->writesize == 2048)
687 cafe->ctl2 |= 1<<29; /* 2KiB page size */
688
689 /* Set up ECC according to the type of chip we found */
David Woodhousefbad5692006-10-22 15:09:33 +0100690 if (mtd->writesize == 2048) {
David Woodhouse8dd851d2006-10-20 02:11:40 +0100691 cafe->nand.ecc.layout = &cafe_oobinfo_2048;
692 cafe->nand.bbt_td = &cafe_bbt_main_descr_2048;
693 cafe->nand.bbt_md = &cafe_bbt_mirror_descr_2048;
David Woodhousefbad5692006-10-22 15:09:33 +0100694 } else if (mtd->writesize == 512) {
695 cafe->nand.ecc.layout = &cafe_oobinfo_512;
696 cafe->nand.bbt_td = &cafe_bbt_main_descr_512;
697 cafe->nand.bbt_md = &cafe_bbt_mirror_descr_512;
David Woodhouse5467fb02006-10-06 15:36:29 +0100698 } else {
David Woodhousefbad5692006-10-22 15:09:33 +0100699 printk(KERN_WARNING "Unexpected NAND flash writesize %d. Aborting\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100700 mtd->writesize);
David Woodhousefbad5692006-10-22 15:09:33 +0100701 goto out_irq;
David Woodhouse5467fb02006-10-06 15:36:29 +0100702 }
David Woodhousefbad5692006-10-22 15:09:33 +0100703 cafe->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
704 cafe->nand.ecc.size = mtd->writesize;
705 cafe->nand.ecc.bytes = 14;
706 cafe->nand.ecc.hwctl = (void *)cafe_nand_bug;
707 cafe->nand.ecc.calculate = (void *)cafe_nand_bug;
708 cafe->nand.ecc.correct = (void *)cafe_nand_bug;
709 cafe->nand.write_page = cafe_nand_write_page;
710 cafe->nand.ecc.write_page = cafe_nand_write_page_lowlevel;
711 cafe->nand.ecc.write_oob = cafe_nand_write_oob;
712 cafe->nand.ecc.read_page = cafe_nand_read_page;
713 cafe->nand.ecc.read_oob = cafe_nand_read_oob;
David Woodhouse5467fb02006-10-06 15:36:29 +0100714
715 err = nand_scan_tail(mtd);
716 if (err)
717 goto out_irq;
718
David Woodhouse5467fb02006-10-06 15:36:29 +0100719 pci_set_drvdata(pdev, mtd);
720 add_mtd_device(mtd);
721 goto out;
722
723 out_irq:
724 /* Disable NAND IRQ in global IRQ mask register */
725 writel(~1 & readl(cafe->mmio + 0x300c), cafe->mmio + 0x300c);
726 free_irq(pdev->irq, mtd);
727 out_free_dma:
728 dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
729 out_ior:
730 pci_iounmap(pdev, cafe->mmio);
731 out_free_mtd:
732 kfree(mtd);
733 out:
734 return err;
735}
736
737static void __devexit cafe_nand_remove(struct pci_dev *pdev)
738{
739 struct mtd_info *mtd = pci_get_drvdata(pdev);
740 struct cafe_priv *cafe = mtd->priv;
741
742 del_mtd_device(mtd);
743 /* Disable NAND IRQ in global IRQ mask register */
744 writel(~1 & readl(cafe->mmio + 0x300c), cafe->mmio + 0x300c);
745 free_irq(pdev->irq, mtd);
746 nand_release(mtd);
747 pci_iounmap(pdev, cafe->mmio);
748 dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
749 kfree(mtd);
750}
751
752static struct pci_device_id cafe_nand_tbl[] = {
753 { 0x11ab, 0x4100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MEMORY_FLASH << 8, 0xFFFF0 }
754};
755
756MODULE_DEVICE_TABLE(pci, cafe_nand_tbl);
757
758static struct pci_driver cafe_nand_pci_driver = {
759 .name = "CAFÉ NAND",
760 .id_table = cafe_nand_tbl,
761 .probe = cafe_nand_probe,
762 .remove = __devexit_p(cafe_nand_remove),
763#ifdef CONFIG_PMx
764 .suspend = cafe_nand_suspend,
765 .resume = cafe_nand_resume,
766#endif
767};
768
769static int cafe_nand_init(void)
770{
771 return pci_register_driver(&cafe_nand_pci_driver);
772}
773
774static void cafe_nand_exit(void)
775{
776 pci_unregister_driver(&cafe_nand_pci_driver);
777}
778module_init(cafe_nand_init);
779module_exit(cafe_nand_exit);
780
781MODULE_LICENSE("GPL");
782MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
783MODULE_DESCRIPTION("NAND flash driver for OLPC CAFE chip");
784
785/* Correct ECC for 2048 bytes of 0xff:
786 41 a0 71 65 54 27 f3 93 ec a9 be ed 0b a1 */
David Woodhouse8dd851d2006-10-20 02:11:40 +0100787
788/* dwmw2's B-test board, in case of completely screwing it:
789Bad eraseblock 2394 at 0x12b40000
790Bad eraseblock 2627 at 0x14860000
791Bad eraseblock 3349 at 0x1a2a0000
792*/