]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/mtd/nand/cafe_nand.c
long vs. unsigned long - low-hanging fruits in drivers
[net-next-2.6.git] / drivers / mtd / nand / cafe_nand.c
CommitLineData
c9ac5977 1/*
fbad5696 2 * Driver for One Laptop Per Child ‘CAFÉ’ controller, aka Marvell 88ALP01
5467fb02
DW
3 *
4 * Copyright © 2006 Red Hat, Inc.
5 * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
6 */
7
8dd851de 8#define DEBUG
5467fb02
DW
9
10#include <linux/device.h>
11#undef DEBUG
12#include <linux/mtd/mtd.h>
13#include <linux/mtd/nand.h>
8c61b7a7 14#include <linux/rslib.h>
5467fb02
DW
15#include <linux/pci.h>
16#include <linux/delay.h>
17#include <linux/interrupt.h>
a1274302 18#include <linux/dma-mapping.h>
5467fb02
DW
19#include <asm/io.h>
20
21#define CAFE_NAND_CTRL1 0x00
22#define CAFE_NAND_CTRL2 0x04
23#define CAFE_NAND_CTRL3 0x08
24#define CAFE_NAND_STATUS 0x0c
25#define CAFE_NAND_IRQ 0x10
26#define CAFE_NAND_IRQ_MASK 0x14
27#define CAFE_NAND_DATA_LEN 0x18
28#define CAFE_NAND_ADDR1 0x1c
29#define CAFE_NAND_ADDR2 0x20
30#define CAFE_NAND_TIMING1 0x24
31#define CAFE_NAND_TIMING2 0x28
32#define CAFE_NAND_TIMING3 0x2c
33#define CAFE_NAND_NONMEM 0x30
04459d7c 34#define CAFE_NAND_ECC_RESULT 0x3C
fbad5696
DW
35#define CAFE_NAND_DMA_CTRL 0x40
36#define CAFE_NAND_DMA_ADDR0 0x44
37#define CAFE_NAND_DMA_ADDR1 0x48
04459d7c
DW
38#define CAFE_NAND_ECC_SYN01 0x50
39#define CAFE_NAND_ECC_SYN23 0x54
40#define CAFE_NAND_ECC_SYN45 0x58
41#define CAFE_NAND_ECC_SYN67 0x5c
5467fb02
DW
42#define CAFE_NAND_READ_DATA 0x1000
43#define CAFE_NAND_WRITE_DATA 0x2000
44
195a253b
DW
45#define CAFE_GLOBAL_CTRL 0x3004
46#define CAFE_GLOBAL_IRQ 0x3008
47#define CAFE_GLOBAL_IRQ_MASK 0x300c
48#define CAFE_NAND_RESET 0x3034
49
048c37b4
DW
50/* Missing from the datasheet: bit 19 of CTRL1 sets CE0 vs. CE1 */
51#define CTRL1_CHIPSELECT (1<<19)
52
5467fb02
DW
53struct cafe_priv {
54 struct nand_chip nand;
55 struct pci_dev *pdev;
56 void __iomem *mmio;
8c61b7a7 57 struct rs_control *rs;
5467fb02
DW
58 uint32_t ctl1;
59 uint32_t ctl2;
60 int datalen;
61 int nr_data;
62 int data_pos;
63 int page_addr;
64 dma_addr_t dmaaddr;
65 unsigned char *dmabuf;
5467fb02
DW
66};
67
b478c775 68static int usedma = 1;
5467fb02
DW
69module_param(usedma, int, 0644);
70
8dd851de
DW
71static int skipbbt = 0;
72module_param(skipbbt, int, 0644);
73
74static int debug = 0;
75module_param(debug, int, 0644);
76
be8444bd
DW
77static int regdebug = 0;
78module_param(regdebug, int, 0644);
79
b478c775 80static int checkecc = 1;
470b0a90
DW
81module_param(checkecc, int, 0644);
82
527a4f45
DW
83static int numtimings;
84static int timing[3];
85module_param_array(timing, int, &numtimings, 0644);
b478c775 86
04459d7c 87/* Hrm. Why isn't this already conditional on something in the struct device? */
8dd851de
DW
88#define cafe_dev_dbg(dev, args...) do { if (debug) dev_dbg(dev, ##args); } while(0)
89
195a253b
DW
90/* Make it easier to switch to PIO if we need to */
91#define cafe_readl(cafe, addr) readl((cafe)->mmio + CAFE_##addr)
92#define cafe_writel(cafe, datum, addr) writel(datum, (cafe)->mmio + CAFE_##addr)
8dd851de 93
5467fb02
DW
94static int cafe_device_ready(struct mtd_info *mtd)
95{
96 struct cafe_priv *cafe = mtd->priv;
195a253b
DW
97 int result = !!(cafe_readl(cafe, NAND_STATUS) | 0x40000000);
98 uint32_t irqs = cafe_readl(cafe, NAND_IRQ);
fbad5696 99
195a253b 100 cafe_writel(cafe, irqs, NAND_IRQ);
fbad5696 101
8dd851de 102 cafe_dev_dbg(&cafe->pdev->dev, "NAND device is%s ready, IRQ %x (%x) (%x,%x)\n",
195a253b
DW
103 result?"":" not", irqs, cafe_readl(cafe, NAND_IRQ),
104 cafe_readl(cafe, GLOBAL_IRQ), cafe_readl(cafe, GLOBAL_IRQ_MASK));
fbad5696 105
5467fb02
DW
106 return result;
107}
108
109
110static void cafe_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
111{
112 struct cafe_priv *cafe = mtd->priv;
113
114 if (usedma)
115 memcpy(cafe->dmabuf + cafe->datalen, buf, len);
116 else
117 memcpy_toio(cafe->mmio + CAFE_NAND_WRITE_DATA + cafe->datalen, buf, len);
fbad5696 118
5467fb02
DW
119 cafe->datalen += len;
120
8dd851de 121 cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes to write buffer. datalen 0x%x\n",
5467fb02
DW
122 len, cafe->datalen);
123}
124
125static void cafe_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
126{
127 struct cafe_priv *cafe = mtd->priv;
128
129 if (usedma)
130 memcpy(buf, cafe->dmabuf + cafe->datalen, len);
131 else
132 memcpy_fromio(buf, cafe->mmio + CAFE_NAND_READ_DATA + cafe->datalen, len);
133
8dd851de 134 cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes from position 0x%x in read buffer.\n",
5467fb02
DW
135 len, cafe->datalen);
136 cafe->datalen += len;
137}
138
139static uint8_t cafe_read_byte(struct mtd_info *mtd)
140{
141 struct cafe_priv *cafe = mtd->priv;
142 uint8_t d;
143
144 cafe_read_buf(mtd, &d, 1);
8dd851de 145 cafe_dev_dbg(&cafe->pdev->dev, "Read %02x\n", d);
5467fb02
DW
146
147 return d;
148}
149
150static void cafe_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
151 int column, int page_addr)
152{
153 struct cafe_priv *cafe = mtd->priv;
154 int adrbytes = 0;
155 uint32_t ctl1;
156 uint32_t doneint = 0x80000000;
5467fb02 157
8dd851de 158 cafe_dev_dbg(&cafe->pdev->dev, "cmdfunc %02x, 0x%x, 0x%x\n",
5467fb02
DW
159 command, column, page_addr);
160
161 if (command == NAND_CMD_ERASE2 || command == NAND_CMD_PAGEPROG) {
162 /* Second half of a command we already calculated */
195a253b 163 cafe_writel(cafe, cafe->ctl2 | 0x100 | command, NAND_CTRL2);
5467fb02 164 ctl1 = cafe->ctl1;
cad40654 165 cafe->ctl2 &= ~(1<<30);
8dd851de 166 cafe_dev_dbg(&cafe->pdev->dev, "Continue command, ctl1 %08x, #data %d\n",
5467fb02
DW
167 cafe->ctl1, cafe->nr_data);
168 goto do_command;
169 }
170 /* Reset ECC engine */
195a253b 171 cafe_writel(cafe, 0, NAND_CTRL2);
5467fb02
DW
172
173 /* Emulate NAND_CMD_READOOB on large-page chips */
174 if (mtd->writesize > 512 &&
175 command == NAND_CMD_READOOB) {
176 column += mtd->writesize;
177 command = NAND_CMD_READ0;
178 }
179
180 /* FIXME: Do we need to send read command before sending data
181 for small-page chips, to position the buffer correctly? */
182
183 if (column != -1) {
195a253b 184 cafe_writel(cafe, column, NAND_ADDR1);
5467fb02
DW
185 adrbytes = 2;
186 if (page_addr != -1)
187 goto write_adr2;
188 } else if (page_addr != -1) {
195a253b 189 cafe_writel(cafe, page_addr & 0xffff, NAND_ADDR1);
5467fb02
DW
190 page_addr >>= 16;
191 write_adr2:
195a253b 192 cafe_writel(cafe, page_addr, NAND_ADDR2);
5467fb02
DW
193 adrbytes += 2;
194 if (mtd->size > mtd->writesize << 16)
195 adrbytes++;
196 }
197
198 cafe->data_pos = cafe->datalen = 0;
199
048c37b4
DW
200 /* Set command valid bit, mask in the chip select bit */
201 ctl1 = 0x80000000 | command | (cafe->ctl1 & CTRL1_CHIPSELECT);
5467fb02
DW
202
203 /* Set RD or WR bits as appropriate */
204 if (command == NAND_CMD_READID || command == NAND_CMD_STATUS) {
205 ctl1 |= (1<<26); /* rd */
206 /* Always 5 bytes, for now */
8dd851de 207 cafe->datalen = 4;
5467fb02
DW
208 /* And one address cycle -- even for STATUS, since the controller doesn't work without */
209 adrbytes = 1;
210 } else if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
211 command == NAND_CMD_READOOB || command == NAND_CMD_RNDOUT) {
212 ctl1 |= 1<<26; /* rd */
213 /* For now, assume just read to end of page */
214 cafe->datalen = mtd->writesize + mtd->oobsize - column;
215 } else if (command == NAND_CMD_SEQIN)
216 ctl1 |= 1<<25; /* wr */
217
218 /* Set number of address bytes */
219 if (adrbytes)
220 ctl1 |= ((adrbytes-1)|8) << 27;
221
222 if (command == NAND_CMD_SEQIN || command == NAND_CMD_ERASE1) {
c9ac5977 223 /* Ignore the first command of a pair; the hardware
5467fb02
DW
224 deals with them both at once, later */
225 cafe->ctl1 = ctl1;
8dd851de 226 cafe_dev_dbg(&cafe->pdev->dev, "Setup for delayed command, ctl1 %08x, dlen %x\n",
5467fb02
DW
227 cafe->ctl1, cafe->datalen);
228 return;
229 }
230 /* RNDOUT and READ0 commands need a following byte */
231 if (command == NAND_CMD_RNDOUT)
195a253b 232 cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_RNDOUTSTART, NAND_CTRL2);
5467fb02 233 else if (command == NAND_CMD_READ0 && mtd->writesize > 512)
195a253b 234 cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_READSTART, NAND_CTRL2);
5467fb02
DW
235
236 do_command:
c9ac5977 237 cafe_dev_dbg(&cafe->pdev->dev, "dlen %x, ctl1 %x, ctl2 %x\n",
195a253b 238 cafe->datalen, ctl1, cafe_readl(cafe, NAND_CTRL2));
fbad5696 239
5467fb02 240 /* NB: The datasheet lies -- we really should be subtracting 1 here */
195a253b
DW
241 cafe_writel(cafe, cafe->datalen, NAND_DATA_LEN);
242 cafe_writel(cafe, 0x90000000, NAND_IRQ);
5467fb02
DW
243 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 }
195a253b 253 cafe_writel(cafe, dmactl, NAND_DMA_CTRL);
5467fb02 254 }
5467fb02
DW
255 cafe->datalen = 0;
256
be8444bd
DW
257 if (unlikely(regdebug)) {
258 int i;
259 printk("About to write command %08x to register 0\n", ctl1);
260 for (i=4; i< 0x5c; i+=4)
261 printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
fbad5696 262 }
be8444bd 263
195a253b 264 cafe_writel(cafe, ctl1, NAND_CTRL1);
5467fb02
DW
265 /* 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) {
2a7295b2 270 int c;
5467fb02
DW
271 uint32_t irqs;
272
2a7295b2 273 for (c = 500000; c != 0; c--) {
195a253b 274 irqs = cafe_readl(cafe, NAND_IRQ);
5467fb02
DW
275 if (irqs & doneint)
276 break;
277 udelay(1);
8dd851de
DW
278 if (!(c % 100000))
279 cafe_dev_dbg(&cafe->pdev->dev, "Wait for ready, IRQ %x\n", irqs);
5467fb02
DW
280 cpu_relax();
281 }
195a253b 282 cafe_writel(cafe, doneint, NAND_IRQ);
a020727b 283 cafe_dev_dbg(&cafe->pdev->dev, "Command %x completed after %d usec, irqs %x (%x)\n",
195a253b 284 command, 500000-c, irqs, cafe_readl(cafe, NAND_IRQ));
5467fb02
DW
285 }
286
cad40654 287 WARN_ON(cafe->ctl2 & (1<<30));
5467fb02
DW
288
289 switch (command) {
290
291 case NAND_CMD_CACHEDPROG:
292 case NAND_CMD_PAGEPROG:
293 case NAND_CMD_ERASE1:
294 case NAND_CMD_ERASE2:
295 case NAND_CMD_SEQIN:
296 case NAND_CMD_RNDIN:
297 case NAND_CMD_STATUS:
298 case NAND_CMD_DEPLETE1:
299 case NAND_CMD_RNDOUT:
300 case NAND_CMD_STATUS_ERROR:
301 case NAND_CMD_STATUS_ERROR0:
302 case NAND_CMD_STATUS_ERROR1:
303 case NAND_CMD_STATUS_ERROR2:
304 case NAND_CMD_STATUS_ERROR3:
195a253b 305 cafe_writel(cafe, cafe->ctl2, NAND_CTRL2);
5467fb02
DW
306 return;
307 }
308 nand_wait_ready(mtd);
195a253b 309 cafe_writel(cafe, cafe->ctl2, NAND_CTRL2);
5467fb02
DW
310}
311
312static void cafe_select_chip(struct mtd_info *mtd, int chipnr)
313{
048c37b4
DW
314 struct cafe_priv *cafe = mtd->priv;
315
316 cafe_dev_dbg(&cafe->pdev->dev, "select_chip %d\n", chipnr);
317
318 /* Mask the appropriate bit into the stored value of ctl1
319 which will be used by cafe_nand_cmdfunc() */
320 if (chipnr)
321 cafe->ctl1 |= CTRL1_CHIPSELECT;
322 else
323 cafe->ctl1 &= ~CTRL1_CHIPSELECT;
5467fb02 324}
fbad5696 325
28bdd4a7 326static int cafe_nand_interrupt(int irq, void *id)
5467fb02
DW
327{
328 struct mtd_info *mtd = id;
329 struct cafe_priv *cafe = mtd->priv;
195a253b
DW
330 uint32_t irqs = cafe_readl(cafe, NAND_IRQ);
331 cafe_writel(cafe, irqs & ~0x90000000, NAND_IRQ);
5467fb02
DW
332 if (!irqs)
333 return IRQ_NONE;
334
195a253b 335 cafe_dev_dbg(&cafe->pdev->dev, "irq, bits %x (%x)\n", irqs, cafe_readl(cafe, NAND_IRQ));
5467fb02
DW
336 return IRQ_HANDLED;
337}
338
339static void cafe_nand_bug(struct mtd_info *mtd)
340{
341 BUG();
342}
343
344static int cafe_nand_write_oob(struct mtd_info *mtd,
345 struct nand_chip *chip, int page)
346{
347 int status = 0;
348
5467fb02
DW
349 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
350 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
351 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
352 status = chip->waitfunc(mtd, chip);
353
354 return status & NAND_STATUS_FAIL ? -EIO : 0;
355}
356
357/* Don't use -- use nand_read_oob_std for now */
358static int cafe_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
359 int page, int sndcmd)
360{
361 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
362 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
363 return 1;
364}
365/**
366 * cafe_nand_read_page_syndrome - {REPLACABLE] hardware ecc syndrom based page read
367 * @mtd: mtd info structure
368 * @chip: nand chip info structure
369 * @buf: buffer to store read data
370 *
371 * The hw generator calculates the error syndrome automatically. Therefor
372 * we need a special oob layout and handling.
373 */
374static int cafe_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
375 uint8_t *buf)
376{
377 struct cafe_priv *cafe = mtd->priv;
378
fbad5696 379 cafe_dev_dbg(&cafe->pdev->dev, "ECC result %08x SYN1,2 %08x\n",
195a253b
DW
380 cafe_readl(cafe, NAND_ECC_RESULT),
381 cafe_readl(cafe, NAND_ECC_SYN01));
5467fb02
DW
382
383 chip->read_buf(mtd, buf, mtd->writesize);
384 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
385
195a253b 386 if (checkecc && cafe_readl(cafe, NAND_ECC_RESULT) & (1<<18)) {
8c61b7a7
SB
387 unsigned short syn[8], pat[4];
388 int pos[4];
389 u8 *oob = chip->oob_poi;
390 int i, n;
04459d7c
DW
391
392 for (i=0; i<8; i+=2) {
195a253b 393 uint32_t tmp = cafe_readl(cafe, NAND_ECC_SYN01 + (i*2));
8c61b7a7
SB
394 syn[i] = cafe->rs->index_of[tmp & 0xfff];
395 syn[i+1] = cafe->rs->index_of[(tmp >> 16) & 0xfff];
396 }
397
398 n = decode_rs16(cafe->rs, NULL, NULL, 1367, syn, 0, pos, 0,
399 pat);
400
401 for (i = 0; i < n; i++) {
402 int p = pos[i];
403
404 /* The 12-bit symbols are mapped to bytes here */
405
406 if (p > 1374) {
407 /* out of range */
408 n = -1374;
409 } else if (p == 0) {
410 /* high four bits do not correspond to data */
411 if (pat[i] > 0xff)
412 n = -2048;
413 else
414 buf[0] ^= pat[i];
415 } else if (p == 1365) {
416 buf[2047] ^= pat[i] >> 4;
417 oob[0] ^= pat[i] << 4;
418 } else if (p > 1365) {
419 if ((p & 1) == 1) {
420 oob[3*p/2 - 2048] ^= pat[i] >> 4;
421 oob[3*p/2 - 2047] ^= pat[i] << 4;
422 } else {
423 oob[3*p/2 - 2049] ^= pat[i] >> 8;
424 oob[3*p/2 - 2048] ^= pat[i];
425 }
426 } else if ((p & 1) == 1) {
427 buf[3*p/2] ^= pat[i] >> 4;
428 buf[3*p/2 + 1] ^= pat[i] << 4;
429 } else {
430 buf[3*p/2 - 1] ^= pat[i] >> 8;
431 buf[3*p/2] ^= pat[i];
432 }
c9ac5977 433 }
04459d7c 434
8c61b7a7 435 if (n < 0) {
be8444bd
DW
436 dev_dbg(&cafe->pdev->dev, "Failed to correct ECC at %08x\n",
437 cafe_readl(cafe, NAND_ADDR2) * 2048);
8c61b7a7 438 for (i = 0; i < 0x5c; i += 4)
be8444bd 439 printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
04459d7c
DW
440 mtd->ecc_stats.failed++;
441 } else {
8c61b7a7
SB
442 dev_dbg(&cafe->pdev->dev, "Corrected %d symbol errors\n", n);
443 mtd->ecc_stats.corrected += n;
04459d7c
DW
444 }
445 }
446
5467fb02
DW
447 return 0;
448}
449
8dd851de
DW
450static struct nand_ecclayout cafe_oobinfo_2048 = {
451 .eccbytes = 14,
452 .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
453 .oobfree = {{14, 50}}
454};
455
c9ac5977 456/* Ick. The BBT code really ought to be able to work this bit out
fbad5696
DW
457 for itself from the above, at least for the 2KiB case */
458static uint8_t cafe_bbt_pattern_2048[] = { 'B', 'b', 't', '0' };
459static uint8_t cafe_mirror_pattern_2048[] = { '1', 't', 'b', 'B' };
460
461static uint8_t cafe_bbt_pattern_512[] = { 0xBB };
462static uint8_t cafe_mirror_pattern_512[] = { 0xBC };
463
8dd851de
DW
464
465static struct nand_bbt_descr cafe_bbt_main_descr_2048 = {
466 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
048c37b4 467 | NAND_BBT_2BIT | NAND_BBT_VERSION,
8dd851de
DW
468 .offs = 14,
469 .len = 4,
470 .veroffs = 18,
471 .maxblocks = 4,
fbad5696 472 .pattern = cafe_bbt_pattern_2048
8dd851de
DW
473};
474
475static struct nand_bbt_descr cafe_bbt_mirror_descr_2048 = {
476 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
048c37b4 477 | NAND_BBT_2BIT | NAND_BBT_VERSION,
8dd851de
DW
478 .offs = 14,
479 .len = 4,
480 .veroffs = 18,
481 .maxblocks = 4,
fbad5696 482 .pattern = cafe_mirror_pattern_2048
8dd851de
DW
483};
484
485static struct nand_ecclayout cafe_oobinfo_512 = {
486 .eccbytes = 14,
487 .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
488 .oobfree = {{14, 2}}
489};
490
fbad5696
DW
491static struct nand_bbt_descr cafe_bbt_main_descr_512 = {
492 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
048c37b4 493 | NAND_BBT_2BIT | NAND_BBT_VERSION,
fbad5696
DW
494 .offs = 14,
495 .len = 1,
496 .veroffs = 15,
497 .maxblocks = 4,
498 .pattern = cafe_bbt_pattern_512
499};
500
501static struct nand_bbt_descr cafe_bbt_mirror_descr_512 = {
502 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
048c37b4 503 | NAND_BBT_2BIT | NAND_BBT_VERSION,
fbad5696
DW
504 .offs = 14,
505 .len = 1,
506 .veroffs = 15,
507 .maxblocks = 4,
508 .pattern = cafe_mirror_pattern_512
509};
510
511
5467fb02
DW
512static void cafe_nand_write_page_lowlevel(struct mtd_info *mtd,
513 struct nand_chip *chip, const uint8_t *buf)
514{
515 struct cafe_priv *cafe = mtd->priv;
516
5467fb02 517 chip->write_buf(mtd, buf, mtd->writesize);
8dd851de 518 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
5467fb02
DW
519
520 /* Set up ECC autogeneration */
cad40654 521 cafe->ctl2 |= (1<<30);
5467fb02
DW
522}
523
524static int cafe_nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
525 const uint8_t *buf, int page, int cached, int raw)
526{
527 int status;
528
5467fb02
DW
529 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
530
531 if (unlikely(raw))
532 chip->ecc.write_page_raw(mtd, chip, buf);
533 else
534 chip->ecc.write_page(mtd, chip, buf);
535
536 /*
537 * Cached progamming disabled for now, Not sure if its worth the
538 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
539 */
540 cached = 0;
541
542 if (!cached || !(chip->options & NAND_CACHEPRG)) {
543
544 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
545 status = chip->waitfunc(mtd, chip);
546 /*
547 * See if operation failed and additional status checks are
548 * available
549 */
550 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
551 status = chip->errstat(mtd, chip, FL_WRITING, status,
552 page);
553
554 if (status & NAND_STATUS_FAIL)
555 return -EIO;
556 } else {
557 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
558 status = chip->waitfunc(mtd, chip);
559 }
560
561#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
562 /* Send command to read back the data */
563 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
564
565 if (chip->verify_buf(mtd, buf, mtd->writesize))
566 return -EIO;
567#endif
568 return 0;
569}
570
8dd851de
DW
571static int cafe_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
572{
573 return 0;
574}
5467fb02 575
8c61b7a7
SB
576/* F_2[X]/(X**6+X+1) */
577static unsigned short __devinit gf64_mul(u8 a, u8 b)
578{
579 u8 c;
580 unsigned int i;
581
582 c = 0;
583 for (i = 0; i < 6; i++) {
584 if (a & 1)
585 c ^= b;
586 a >>= 1;
587 b <<= 1;
588 if ((b & 0x40) != 0)
589 b ^= 0x43;
590 }
591
592 return c;
593}
594
595/* F_64[X]/(X**2+X+A**-1) with A the generator of F_64[X] */
596static u16 __devinit gf4096_mul(u16 a, u16 b)
597{
598 u8 ah, al, bh, bl, ch, cl;
599
600 ah = a >> 6;
601 al = a & 0x3f;
602 bh = b >> 6;
603 bl = b & 0x3f;
604
605 ch = gf64_mul(ah ^ al, bh ^ bl) ^ gf64_mul(al, bl);
606 cl = gf64_mul(gf64_mul(ah, bh), 0x21) ^ gf64_mul(al, bl);
607
608 return (ch << 6) ^ cl;
609}
610
611static int __devinit cafe_mul(int x)
612{
613 if (x == 0)
614 return 1;
615 return gf4096_mul(x, 0xe01);
616}
617
5467fb02
DW
618static int __devinit cafe_nand_probe(struct pci_dev *pdev,
619 const struct pci_device_id *ent)
620{
621 struct mtd_info *mtd;
622 struct cafe_priv *cafe;
623 uint32_t ctrl;
624 int err = 0;
625
06ed24e5
DW
626 /* Very old versions shared the same PCI ident for all three
627 functions on the chip. Verify the class too... */
628 if ((pdev->class >> 8) != PCI_CLASS_MEMORY_FLASH)
629 return -ENODEV;
630
5467fb02
DW
631 err = pci_enable_device(pdev);
632 if (err)
633 return err;
634
635 pci_set_master(pdev);
636
637 mtd = kzalloc(sizeof(*mtd) + sizeof(struct cafe_priv), GFP_KERNEL);
638 if (!mtd) {
639 dev_warn(&pdev->dev, "failed to alloc mtd_info\n");
640 return -ENOMEM;
641 }
642 cafe = (void *)(&mtd[1]);
643
644 mtd->priv = cafe;
645 mtd->owner = THIS_MODULE;
646
647 cafe->pdev = pdev;
648 cafe->mmio = pci_iomap(pdev, 0, 0);
649 if (!cafe->mmio) {
650 dev_warn(&pdev->dev, "failed to iomap\n");
651 err = -ENOMEM;
652 goto out_free_mtd;
653 }
654 cafe->dmabuf = dma_alloc_coherent(&cafe->pdev->dev, 2112 + sizeof(struct nand_buffers),
655 &cafe->dmaaddr, GFP_KERNEL);
656 if (!cafe->dmabuf) {
657 err = -ENOMEM;
658 goto out_ior;
659 }
660 cafe->nand.buffers = (void *)cafe->dmabuf + 2112;
661
8c61b7a7
SB
662 cafe->rs = init_rs_non_canonical(12, &cafe_mul, 0, 1, 8);
663 if (!cafe->rs) {
664 err = -ENOMEM;
665 goto out_ior;
666 }
667
5467fb02
DW
668 cafe->nand.cmdfunc = cafe_nand_cmdfunc;
669 cafe->nand.dev_ready = cafe_device_ready;
670 cafe->nand.read_byte = cafe_read_byte;
671 cafe->nand.read_buf = cafe_read_buf;
672 cafe->nand.write_buf = cafe_write_buf;
673 cafe->nand.select_chip = cafe_select_chip;
674
675 cafe->nand.chip_delay = 0;
676
677 /* Enable the following for a flash based bad block table */
678 cafe->nand.options = NAND_USE_FLASH_BBT | NAND_NO_AUTOINCR | NAND_OWN_BUFFERS;
8dd851de
DW
679
680 if (skipbbt) {
681 cafe->nand.options |= NAND_SKIP_BBTSCAN;
682 cafe->nand.block_bad = cafe_nand_block_bad;
683 }
c9ac5977 684
527a4f45
DW
685 if (numtimings && numtimings != 3) {
686 dev_warn(&cafe->pdev->dev, "%d timing register values ignored; precisely three are required\n", numtimings);
687 }
688
689 if (numtimings == 3) {
527a4f45 690 cafe_dev_dbg(&cafe->pdev->dev, "Using provided timings (%08x %08x %08x)\n",
8e5368a1 691 timing[0], timing[1], timing[2]);
527a4f45 692 } else {
8e5368a1
DW
693 timing[0] = cafe_readl(cafe, NAND_TIMING1);
694 timing[1] = cafe_readl(cafe, NAND_TIMING2);
695 timing[2] = cafe_readl(cafe, NAND_TIMING3);
527a4f45 696
8e5368a1
DW
697 if (timing[0] | timing[1] | timing[2]) {
698 cafe_dev_dbg(&cafe->pdev->dev, "Timing registers already set (%08x %08x %08x)\n",
699 timing[0], timing[1], timing[2]);
527a4f45
DW
700 } else {
701 dev_warn(&cafe->pdev->dev, "Timing registers unset; using most conservative defaults\n");
8e5368a1 702 timing[0] = timing[1] = timing[2] = 0xffffffff;
527a4f45
DW
703 }
704 }
705
dcc41bc8 706 /* Start off by resetting the NAND controller completely */
195a253b
DW
707 cafe_writel(cafe, 1, NAND_RESET);
708 cafe_writel(cafe, 0, NAND_RESET);
dcc41bc8 709
8e5368a1
DW
710 cafe_writel(cafe, timing[0], NAND_TIMING1);
711 cafe_writel(cafe, timing[1], NAND_TIMING2);
712 cafe_writel(cafe, timing[2], NAND_TIMING3);
b478c775 713
195a253b 714 cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK);
2db6346f
TG
715 err = request_irq(pdev->irq, &cafe_nand_interrupt, IRQF_SHARED,
716 "CAFE NAND", mtd);
5467fb02
DW
717 if (err) {
718 dev_warn(&pdev->dev, "Could not register IRQ %d\n", pdev->irq);
5467fb02
DW
719 goto out_free_dma;
720 }
f7c37d7b 721
5467fb02 722 /* Disable master reset, enable NAND clock */
195a253b 723 ctrl = cafe_readl(cafe, GLOBAL_CTRL);
5467fb02
DW
724 ctrl &= 0xffffeff0;
725 ctrl |= 0x00007000;
195a253b
DW
726 cafe_writel(cafe, ctrl | 0x05, GLOBAL_CTRL);
727 cafe_writel(cafe, ctrl | 0x0a, GLOBAL_CTRL);
728 cafe_writel(cafe, 0, NAND_DMA_CTRL);
5467fb02 729
195a253b
DW
730 cafe_writel(cafe, 0x7006, GLOBAL_CTRL);
731 cafe_writel(cafe, 0x700a, GLOBAL_CTRL);
5467fb02
DW
732
733 /* Set up DMA address */
195a253b 734 cafe_writel(cafe, cafe->dmaaddr & 0xffffffff, NAND_DMA_ADDR0);
5467fb02 735 if (sizeof(cafe->dmaaddr) > 4)
fbad5696 736 /* Shift in two parts to shut the compiler up */
195a253b 737 cafe_writel(cafe, (cafe->dmaaddr >> 16) >> 16, NAND_DMA_ADDR1);
5467fb02 738 else
195a253b 739 cafe_writel(cafe, 0, NAND_DMA_ADDR1);
fbad5696 740
8dd851de 741 cafe_dev_dbg(&cafe->pdev->dev, "Set DMA address to %x (virt %p)\n",
195a253b 742 cafe_readl(cafe, NAND_DMA_ADDR0), cafe->dmabuf);
5467fb02
DW
743
744 /* Enable NAND IRQ in global IRQ mask register */
195a253b 745 cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK);
8dd851de 746 cafe_dev_dbg(&cafe->pdev->dev, "Control %x, IRQ mask %x\n",
195a253b 747 cafe_readl(cafe, GLOBAL_CTRL), cafe_readl(cafe, GLOBAL_IRQ_MASK));
f7c37d7b
DW
748
749 /* Scan to find existence of the device */
048c37b4 750 if (nand_scan_ident(mtd, 2)) {
5467fb02
DW
751 err = -ENXIO;
752 goto out_irq;
753 }
754
755 cafe->ctl2 = 1<<27; /* Reed-Solomon ECC */
756 if (mtd->writesize == 2048)
757 cafe->ctl2 |= 1<<29; /* 2KiB page size */
758
759 /* Set up ECC according to the type of chip we found */
fbad5696 760 if (mtd->writesize == 2048) {
8dd851de
DW
761 cafe->nand.ecc.layout = &cafe_oobinfo_2048;
762 cafe->nand.bbt_td = &cafe_bbt_main_descr_2048;
763 cafe->nand.bbt_md = &cafe_bbt_mirror_descr_2048;
fbad5696
DW
764 } else if (mtd->writesize == 512) {
765 cafe->nand.ecc.layout = &cafe_oobinfo_512;
766 cafe->nand.bbt_td = &cafe_bbt_main_descr_512;
767 cafe->nand.bbt_md = &cafe_bbt_mirror_descr_512;
5467fb02 768 } else {
fbad5696 769 printk(KERN_WARNING "Unexpected NAND flash writesize %d. Aborting\n",
5467fb02 770 mtd->writesize);
fbad5696 771 goto out_irq;
5467fb02 772 }
fbad5696
DW
773 cafe->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
774 cafe->nand.ecc.size = mtd->writesize;
775 cafe->nand.ecc.bytes = 14;
776 cafe->nand.ecc.hwctl = (void *)cafe_nand_bug;
777 cafe->nand.ecc.calculate = (void *)cafe_nand_bug;
778 cafe->nand.ecc.correct = (void *)cafe_nand_bug;
779 cafe->nand.write_page = cafe_nand_write_page;
780 cafe->nand.ecc.write_page = cafe_nand_write_page_lowlevel;
781 cafe->nand.ecc.write_oob = cafe_nand_write_oob;
782 cafe->nand.ecc.read_page = cafe_nand_read_page;
783 cafe->nand.ecc.read_oob = cafe_nand_read_oob;
5467fb02
DW
784
785 err = nand_scan_tail(mtd);
786 if (err)
787 goto out_irq;
788
5467fb02
DW
789 pci_set_drvdata(pdev, mtd);
790 add_mtd_device(mtd);
791 goto out;
792
793 out_irq:
794 /* Disable NAND IRQ in global IRQ mask register */
195a253b 795 cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
5467fb02
DW
796 free_irq(pdev->irq, mtd);
797 out_free_dma:
798 dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
799 out_ior:
800 pci_iounmap(pdev, cafe->mmio);
801 out_free_mtd:
802 kfree(mtd);
803 out:
804 return err;
805}
806
807static void __devexit cafe_nand_remove(struct pci_dev *pdev)
808{
809 struct mtd_info *mtd = pci_get_drvdata(pdev);
810 struct cafe_priv *cafe = mtd->priv;
811
812 del_mtd_device(mtd);
813 /* Disable NAND IRQ in global IRQ mask register */
195a253b 814 cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
5467fb02
DW
815 free_irq(pdev->irq, mtd);
816 nand_release(mtd);
8c61b7a7 817 free_rs(cafe->rs);
5467fb02
DW
818 pci_iounmap(pdev, cafe->mmio);
819 dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
820 kfree(mtd);
821}
822
823static struct pci_device_id cafe_nand_tbl[] = {
06ed24e5
DW
824 { 0x11ab, 0x4100, PCI_ANY_ID, PCI_ANY_ID },
825 { }
5467fb02
DW
826};
827
828MODULE_DEVICE_TABLE(pci, cafe_nand_tbl);
829
1fcf8ce5
DW
830static int cafe_nand_resume(struct pci_dev *pdev)
831{
832 uint32_t ctrl;
833 struct mtd_info *mtd = pci_get_drvdata(pdev);
834 struct cafe_priv *cafe = mtd->priv;
835
836 /* Start off by resetting the NAND controller completely */
837 cafe_writel(cafe, 1, NAND_RESET);
838 cafe_writel(cafe, 0, NAND_RESET);
839 cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK);
840
841 /* Restore timing configuration */
842 cafe_writel(cafe, timing[0], NAND_TIMING1);
843 cafe_writel(cafe, timing[1], NAND_TIMING2);
844 cafe_writel(cafe, timing[2], NAND_TIMING3);
845
846 /* Disable master reset, enable NAND clock */
847 ctrl = cafe_readl(cafe, GLOBAL_CTRL);
848 ctrl &= 0xffffeff0;
849 ctrl |= 0x00007000;
850 cafe_writel(cafe, ctrl | 0x05, GLOBAL_CTRL);
851 cafe_writel(cafe, ctrl | 0x0a, GLOBAL_CTRL);
852 cafe_writel(cafe, 0, NAND_DMA_CTRL);
853 cafe_writel(cafe, 0x7006, GLOBAL_CTRL);
854 cafe_writel(cafe, 0x700a, GLOBAL_CTRL);
855
856 /* Set up DMA address */
857 cafe_writel(cafe, cafe->dmaaddr & 0xffffffff, NAND_DMA_ADDR0);
858 if (sizeof(cafe->dmaaddr) > 4)
859 /* Shift in two parts to shut the compiler up */
860 cafe_writel(cafe, (cafe->dmaaddr >> 16) >> 16, NAND_DMA_ADDR1);
861 else
862 cafe_writel(cafe, 0, NAND_DMA_ADDR1);
863
864 /* Enable NAND IRQ in global IRQ mask register */
865 cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK);
866 return 0;
867}
868
5467fb02
DW
869static struct pci_driver cafe_nand_pci_driver = {
870 .name = "CAFÉ NAND",
871 .id_table = cafe_nand_tbl,
872 .probe = cafe_nand_probe,
873 .remove = __devexit_p(cafe_nand_remove),
5467fb02 874 .resume = cafe_nand_resume,
5467fb02
DW
875};
876
877static int cafe_nand_init(void)
878{
879 return pci_register_driver(&cafe_nand_pci_driver);
880}
881
882static void cafe_nand_exit(void)
883{
884 pci_unregister_driver(&cafe_nand_pci_driver);
885}
886module_init(cafe_nand_init);
887module_exit(cafe_nand_exit);
888
889MODULE_LICENSE("GPL");
890MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
f7c37d7b 891MODULE_DESCRIPTION("NAND flash driver for OLPC CAFÉ chip");