blob: 3346aab275c7ac6298a97bf1ec45775d3919be15 [file] [log] [blame]
David Woodhousec9ac5972006-11-30 08:17:38 +00001/*
David Woodhousefbad5692006-10-22 15:09:33 +01002 * Driver for One Laptop Per Child ‘CAFÉ’ controller, aka Marvell 88ALP01
David Woodhouse5467fb02006-10-06 15:36:29 +01003 *
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;
David Woodhouse5467fb02006-10-06 15:36:29 +010063};
64
David Woodhouseb478c772006-10-27 14:50:04 +030065static int usedma = 1;
David Woodhouse5467fb02006-10-06 15:36:29 +010066module_param(usedma, int, 0644);
67
David Woodhouse8dd851d2006-10-20 02:11:40 +010068static int skipbbt = 0;
69module_param(skipbbt, int, 0644);
70
71static int debug = 0;
72module_param(debug, int, 0644);
73
David Woodhousebe8444b2006-10-31 12:36:04 +080074static int regdebug = 0;
75module_param(regdebug, int, 0644);
76
David Woodhouseb478c772006-10-27 14:50:04 +030077static int checkecc = 1;
David Woodhouse470b0a92006-10-23 14:29:04 +010078module_param(checkecc, int, 0644);
79
David Woodhouse527a4f42007-01-23 15:35:27 +080080static int numtimings;
81static int timing[3];
82module_param_array(timing, int, &numtimings, 0644);
David Woodhouseb478c772006-10-27 14:50:04 +030083
David Woodhouse04459d72006-10-22 02:18:48 +010084/* Hrm. Why isn't this already conditional on something in the struct device? */
David Woodhouse8dd851d2006-10-20 02:11:40 +010085#define cafe_dev_dbg(dev, args...) do { if (debug) dev_dbg(dev, ##args); } while(0)
86
David Woodhouse195a2532006-10-31 12:30:11 +080087/* Make it easier to switch to PIO if we need to */
88#define cafe_readl(cafe, addr) readl((cafe)->mmio + CAFE_##addr)
89#define cafe_writel(cafe, datum, addr) writel(datum, (cafe)->mmio + CAFE_##addr)
David Woodhouse8dd851d2006-10-20 02:11:40 +010090
David Woodhouse5467fb02006-10-06 15:36:29 +010091static int cafe_device_ready(struct mtd_info *mtd)
92{
93 struct cafe_priv *cafe = mtd->priv;
David Woodhouse195a2532006-10-31 12:30:11 +080094 int result = !!(cafe_readl(cafe, NAND_STATUS) | 0x40000000);
95 uint32_t irqs = cafe_readl(cafe, NAND_IRQ);
David Woodhousefbad5692006-10-22 15:09:33 +010096
David Woodhouse195a2532006-10-31 12:30:11 +080097 cafe_writel(cafe, irqs, NAND_IRQ);
David Woodhousefbad5692006-10-22 15:09:33 +010098
David Woodhouse8dd851d2006-10-20 02:11:40 +010099 cafe_dev_dbg(&cafe->pdev->dev, "NAND device is%s ready, IRQ %x (%x) (%x,%x)\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800100 result?"":" not", irqs, cafe_readl(cafe, NAND_IRQ),
101 cafe_readl(cafe, GLOBAL_IRQ), cafe_readl(cafe, GLOBAL_IRQ_MASK));
David Woodhousefbad5692006-10-22 15:09:33 +0100102
David Woodhouse5467fb02006-10-06 15:36:29 +0100103 return result;
104}
105
106
107static void cafe_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
108{
109 struct cafe_priv *cafe = mtd->priv;
110
111 if (usedma)
112 memcpy(cafe->dmabuf + cafe->datalen, buf, len);
113 else
114 memcpy_toio(cafe->mmio + CAFE_NAND_WRITE_DATA + cafe->datalen, buf, len);
David Woodhousefbad5692006-10-22 15:09:33 +0100115
David Woodhouse5467fb02006-10-06 15:36:29 +0100116 cafe->datalen += len;
117
David Woodhouse8dd851d2006-10-20 02:11:40 +0100118 cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes to write buffer. datalen 0x%x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100119 len, cafe->datalen);
120}
121
122static void cafe_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
123{
124 struct cafe_priv *cafe = mtd->priv;
125
126 if (usedma)
127 memcpy(buf, cafe->dmabuf + cafe->datalen, len);
128 else
129 memcpy_fromio(buf, cafe->mmio + CAFE_NAND_READ_DATA + cafe->datalen, len);
130
David Woodhouse8dd851d2006-10-20 02:11:40 +0100131 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 +0100132 len, cafe->datalen);
133 cafe->datalen += len;
134}
135
136static uint8_t cafe_read_byte(struct mtd_info *mtd)
137{
138 struct cafe_priv *cafe = mtd->priv;
139 uint8_t d;
140
141 cafe_read_buf(mtd, &d, 1);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100142 cafe_dev_dbg(&cafe->pdev->dev, "Read %02x\n", d);
David Woodhouse5467fb02006-10-06 15:36:29 +0100143
144 return d;
145}
146
147static void cafe_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
148 int column, int page_addr)
149{
150 struct cafe_priv *cafe = mtd->priv;
151 int adrbytes = 0;
152 uint32_t ctl1;
153 uint32_t doneint = 0x80000000;
David Woodhouse5467fb02006-10-06 15:36:29 +0100154
David Woodhouse8dd851d2006-10-20 02:11:40 +0100155 cafe_dev_dbg(&cafe->pdev->dev, "cmdfunc %02x, 0x%x, 0x%x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100156 command, column, page_addr);
157
158 if (command == NAND_CMD_ERASE2 || command == NAND_CMD_PAGEPROG) {
159 /* Second half of a command we already calculated */
David Woodhouse195a2532006-10-31 12:30:11 +0800160 cafe_writel(cafe, cafe->ctl2 | 0x100 | command, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100161 ctl1 = cafe->ctl1;
David Woodhousecad40652006-11-01 08:19:20 +0800162 cafe->ctl2 &= ~(1<<30);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100163 cafe_dev_dbg(&cafe->pdev->dev, "Continue command, ctl1 %08x, #data %d\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100164 cafe->ctl1, cafe->nr_data);
165 goto do_command;
166 }
167 /* Reset ECC engine */
David Woodhouse195a2532006-10-31 12:30:11 +0800168 cafe_writel(cafe, 0, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100169
170 /* Emulate NAND_CMD_READOOB on large-page chips */
171 if (mtd->writesize > 512 &&
172 command == NAND_CMD_READOOB) {
173 column += mtd->writesize;
174 command = NAND_CMD_READ0;
175 }
176
177 /* FIXME: Do we need to send read command before sending data
178 for small-page chips, to position the buffer correctly? */
179
180 if (column != -1) {
David Woodhouse195a2532006-10-31 12:30:11 +0800181 cafe_writel(cafe, column, NAND_ADDR1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100182 adrbytes = 2;
183 if (page_addr != -1)
184 goto write_adr2;
185 } else if (page_addr != -1) {
David Woodhouse195a2532006-10-31 12:30:11 +0800186 cafe_writel(cafe, page_addr & 0xffff, NAND_ADDR1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100187 page_addr >>= 16;
188 write_adr2:
David Woodhouse195a2532006-10-31 12:30:11 +0800189 cafe_writel(cafe, page_addr, NAND_ADDR2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100190 adrbytes += 2;
191 if (mtd->size > mtd->writesize << 16)
192 adrbytes++;
193 }
194
195 cafe->data_pos = cafe->datalen = 0;
196
197 /* Set command valid bit */
198 ctl1 = 0x80000000 | command;
199
200 /* Set RD or WR bits as appropriate */
201 if (command == NAND_CMD_READID || command == NAND_CMD_STATUS) {
202 ctl1 |= (1<<26); /* rd */
203 /* Always 5 bytes, for now */
David Woodhouse8dd851d2006-10-20 02:11:40 +0100204 cafe->datalen = 4;
David Woodhouse5467fb02006-10-06 15:36:29 +0100205 /* And one address cycle -- even for STATUS, since the controller doesn't work without */
206 adrbytes = 1;
207 } else if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
208 command == NAND_CMD_READOOB || command == NAND_CMD_RNDOUT) {
209 ctl1 |= 1<<26; /* rd */
210 /* For now, assume just read to end of page */
211 cafe->datalen = mtd->writesize + mtd->oobsize - column;
212 } else if (command == NAND_CMD_SEQIN)
213 ctl1 |= 1<<25; /* wr */
214
215 /* Set number of address bytes */
216 if (adrbytes)
217 ctl1 |= ((adrbytes-1)|8) << 27;
218
219 if (command == NAND_CMD_SEQIN || command == NAND_CMD_ERASE1) {
David Woodhousec9ac5972006-11-30 08:17:38 +0000220 /* Ignore the first command of a pair; the hardware
David Woodhouse5467fb02006-10-06 15:36:29 +0100221 deals with them both at once, later */
222 cafe->ctl1 = ctl1;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100223 cafe_dev_dbg(&cafe->pdev->dev, "Setup for delayed command, ctl1 %08x, dlen %x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100224 cafe->ctl1, cafe->datalen);
225 return;
226 }
227 /* RNDOUT and READ0 commands need a following byte */
228 if (command == NAND_CMD_RNDOUT)
David Woodhouse195a2532006-10-31 12:30:11 +0800229 cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_RNDOUTSTART, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100230 else if (command == NAND_CMD_READ0 && mtd->writesize > 512)
David Woodhouse195a2532006-10-31 12:30:11 +0800231 cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_READSTART, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100232
233 do_command:
David Woodhousec9ac5972006-11-30 08:17:38 +0000234 cafe_dev_dbg(&cafe->pdev->dev, "dlen %x, ctl1 %x, ctl2 %x\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800235 cafe->datalen, ctl1, cafe_readl(cafe, NAND_CTRL2));
David Woodhousefbad5692006-10-22 15:09:33 +0100236
David Woodhouse5467fb02006-10-06 15:36:29 +0100237 /* NB: The datasheet lies -- we really should be subtracting 1 here */
David Woodhouse195a2532006-10-31 12:30:11 +0800238 cafe_writel(cafe, cafe->datalen, NAND_DATA_LEN);
239 cafe_writel(cafe, 0x90000000, NAND_IRQ);
David Woodhouse5467fb02006-10-06 15:36:29 +0100240 if (usedma && (ctl1 & (3<<25))) {
241 uint32_t dmactl = 0xc0000000 + cafe->datalen;
242 /* If WR or RD bits set, set up DMA */
243 if (ctl1 & (1<<26)) {
244 /* It's a read */
245 dmactl |= (1<<29);
246 /* ... so it's done when the DMA is done, not just
247 the command. */
248 doneint = 0x10000000;
249 }
David Woodhouse195a2532006-10-31 12:30:11 +0800250 cafe_writel(cafe, dmactl, NAND_DMA_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100251 }
David Woodhouse5467fb02006-10-06 15:36:29 +0100252 cafe->datalen = 0;
253
David Woodhousebe8444b2006-10-31 12:36:04 +0800254 if (unlikely(regdebug)) {
255 int i;
256 printk("About to write command %08x to register 0\n", ctl1);
257 for (i=4; i< 0x5c; i+=4)
258 printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
David Woodhousefbad5692006-10-22 15:09:33 +0100259 }
David Woodhousebe8444b2006-10-31 12:36:04 +0800260
David Woodhouse195a2532006-10-31 12:30:11 +0800261 cafe_writel(cafe, ctl1, NAND_CTRL1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100262 /* Apply this short delay always to ensure that we do wait tWB in
263 * any case on any machine. */
264 ndelay(100);
265
266 if (1) {
David Woodhouse8dd851d2006-10-20 02:11:40 +0100267 int c = 500000;
David Woodhouse5467fb02006-10-06 15:36:29 +0100268 uint32_t irqs;
269
270 while (c--) {
David Woodhouse195a2532006-10-31 12:30:11 +0800271 irqs = cafe_readl(cafe, NAND_IRQ);
David Woodhouse5467fb02006-10-06 15:36:29 +0100272 if (irqs & doneint)
273 break;
274 udelay(1);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100275 if (!(c % 100000))
276 cafe_dev_dbg(&cafe->pdev->dev, "Wait for ready, IRQ %x\n", irqs);
David Woodhouse5467fb02006-10-06 15:36:29 +0100277 cpu_relax();
278 }
David Woodhouse195a2532006-10-31 12:30:11 +0800279 cafe_writel(cafe, doneint, NAND_IRQ);
David Woodhousea0207272006-10-28 17:08:38 +0300280 cafe_dev_dbg(&cafe->pdev->dev, "Command %x completed after %d usec, irqs %x (%x)\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800281 command, 500000-c, irqs, cafe_readl(cafe, NAND_IRQ));
David Woodhouse5467fb02006-10-06 15:36:29 +0100282 }
283
David Woodhousecad40652006-11-01 08:19:20 +0800284 WARN_ON(cafe->ctl2 & (1<<30));
David Woodhouse5467fb02006-10-06 15:36:29 +0100285
286 switch (command) {
287
288 case NAND_CMD_CACHEDPROG:
289 case NAND_CMD_PAGEPROG:
290 case NAND_CMD_ERASE1:
291 case NAND_CMD_ERASE2:
292 case NAND_CMD_SEQIN:
293 case NAND_CMD_RNDIN:
294 case NAND_CMD_STATUS:
295 case NAND_CMD_DEPLETE1:
296 case NAND_CMD_RNDOUT:
297 case NAND_CMD_STATUS_ERROR:
298 case NAND_CMD_STATUS_ERROR0:
299 case NAND_CMD_STATUS_ERROR1:
300 case NAND_CMD_STATUS_ERROR2:
301 case NAND_CMD_STATUS_ERROR3:
David Woodhouse195a2532006-10-31 12:30:11 +0800302 cafe_writel(cafe, cafe->ctl2, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100303 return;
304 }
305 nand_wait_ready(mtd);
David Woodhouse195a2532006-10-31 12:30:11 +0800306 cafe_writel(cafe, cafe->ctl2, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100307}
308
309static void cafe_select_chip(struct mtd_info *mtd, int chipnr)
310{
311 //struct cafe_priv *cafe = mtd->priv;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100312 // cafe_dev_dbg(&cafe->pdev->dev, "select_chip %d\n", chipnr);
David Woodhouse5467fb02006-10-06 15:36:29 +0100313}
David Woodhousefbad5692006-10-22 15:09:33 +0100314
David Woodhouse28bdd4a2006-11-29 00:04:59 +0000315static int cafe_nand_interrupt(int irq, void *id)
David Woodhouse5467fb02006-10-06 15:36:29 +0100316{
317 struct mtd_info *mtd = id;
318 struct cafe_priv *cafe = mtd->priv;
David Woodhouse195a2532006-10-31 12:30:11 +0800319 uint32_t irqs = cafe_readl(cafe, NAND_IRQ);
320 cafe_writel(cafe, irqs & ~0x90000000, NAND_IRQ);
David Woodhouse5467fb02006-10-06 15:36:29 +0100321 if (!irqs)
322 return IRQ_NONE;
323
David Woodhouse195a2532006-10-31 12:30:11 +0800324 cafe_dev_dbg(&cafe->pdev->dev, "irq, bits %x (%x)\n", irqs, cafe_readl(cafe, NAND_IRQ));
David Woodhouse5467fb02006-10-06 15:36:29 +0100325 return IRQ_HANDLED;
326}
327
328static void cafe_nand_bug(struct mtd_info *mtd)
329{
330 BUG();
331}
332
333static int cafe_nand_write_oob(struct mtd_info *mtd,
334 struct nand_chip *chip, int page)
335{
336 int status = 0;
337
David Woodhouse5467fb02006-10-06 15:36:29 +0100338 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
339 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
340 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
341 status = chip->waitfunc(mtd, chip);
342
343 return status & NAND_STATUS_FAIL ? -EIO : 0;
344}
345
346/* Don't use -- use nand_read_oob_std for now */
347static int cafe_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
348 int page, int sndcmd)
349{
350 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
351 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
352 return 1;
353}
354/**
355 * cafe_nand_read_page_syndrome - {REPLACABLE] hardware ecc syndrom based page read
356 * @mtd: mtd info structure
357 * @chip: nand chip info structure
358 * @buf: buffer to store read data
359 *
360 * The hw generator calculates the error syndrome automatically. Therefor
361 * we need a special oob layout and handling.
362 */
363static int cafe_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
364 uint8_t *buf)
365{
366 struct cafe_priv *cafe = mtd->priv;
367
David Woodhousefbad5692006-10-22 15:09:33 +0100368 cafe_dev_dbg(&cafe->pdev->dev, "ECC result %08x SYN1,2 %08x\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800369 cafe_readl(cafe, NAND_ECC_RESULT),
370 cafe_readl(cafe, NAND_ECC_SYN01));
David Woodhouse5467fb02006-10-06 15:36:29 +0100371
372 chip->read_buf(mtd, buf, mtd->writesize);
373 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
374
David Woodhouse195a2532006-10-31 12:30:11 +0800375 if (checkecc && cafe_readl(cafe, NAND_ECC_RESULT) & (1<<18)) {
David Woodhouse04459d72006-10-22 02:18:48 +0100376 unsigned short syn[8];
377 int i;
378
379 for (i=0; i<8; i+=2) {
David Woodhouse195a2532006-10-31 12:30:11 +0800380 uint32_t tmp = cafe_readl(cafe, NAND_ECC_SYN01 + (i*2));
David Woodhouse04459d72006-10-22 02:18:48 +0100381 syn[i] = tmp & 0xfff;
382 syn[i+1] = (tmp >> 16) & 0xfff;
David Woodhousec9ac5972006-11-30 08:17:38 +0000383 }
David Woodhouse04459d72006-10-22 02:18:48 +0100384
David Woodhouse63a14232006-10-27 22:12:02 +0300385 if ((i = cafe_correct_ecc(buf, syn)) < 0) {
David Woodhousebe8444b2006-10-31 12:36:04 +0800386 dev_dbg(&cafe->pdev->dev, "Failed to correct ECC at %08x\n",
387 cafe_readl(cafe, NAND_ADDR2) * 2048);
388 for (i=0; i< 0x5c; i+=4)
389 printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
David Woodhouse04459d72006-10-22 02:18:48 +0100390 mtd->ecc_stats.failed++;
391 } else {
392 dev_dbg(&cafe->pdev->dev, "Corrected %d symbol errors\n", i);
393 mtd->ecc_stats.corrected += i;
394 }
395 }
396
397
David Woodhouse5467fb02006-10-06 15:36:29 +0100398 return 0;
399}
400
David Woodhouse8dd851d2006-10-20 02:11:40 +0100401static struct nand_ecclayout cafe_oobinfo_2048 = {
402 .eccbytes = 14,
403 .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
404 .oobfree = {{14, 50}}
405};
406
David Woodhousec9ac5972006-11-30 08:17:38 +0000407/* Ick. The BBT code really ought to be able to work this bit out
David Woodhousefbad5692006-10-22 15:09:33 +0100408 for itself from the above, at least for the 2KiB case */
409static uint8_t cafe_bbt_pattern_2048[] = { 'B', 'b', 't', '0' };
410static uint8_t cafe_mirror_pattern_2048[] = { '1', 't', 'b', 'B' };
411
412static uint8_t cafe_bbt_pattern_512[] = { 0xBB };
413static uint8_t cafe_mirror_pattern_512[] = { 0xBC };
414
David Woodhouse8dd851d2006-10-20 02:11:40 +0100415
416static struct nand_bbt_descr cafe_bbt_main_descr_2048 = {
417 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
418 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
419 .offs = 14,
420 .len = 4,
421 .veroffs = 18,
422 .maxblocks = 4,
David Woodhousefbad5692006-10-22 15:09:33 +0100423 .pattern = cafe_bbt_pattern_2048
David Woodhouse8dd851d2006-10-20 02:11:40 +0100424};
425
426static struct nand_bbt_descr cafe_bbt_mirror_descr_2048 = {
427 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
428 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
429 .offs = 14,
430 .len = 4,
431 .veroffs = 18,
432 .maxblocks = 4,
David Woodhousefbad5692006-10-22 15:09:33 +0100433 .pattern = cafe_mirror_pattern_2048
David Woodhouse8dd851d2006-10-20 02:11:40 +0100434};
435
436static struct nand_ecclayout cafe_oobinfo_512 = {
437 .eccbytes = 14,
438 .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
439 .oobfree = {{14, 2}}
440};
441
David Woodhousefbad5692006-10-22 15:09:33 +0100442static struct nand_bbt_descr cafe_bbt_main_descr_512 = {
443 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
444 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
445 .offs = 14,
446 .len = 1,
447 .veroffs = 15,
448 .maxblocks = 4,
449 .pattern = cafe_bbt_pattern_512
450};
451
452static struct nand_bbt_descr cafe_bbt_mirror_descr_512 = {
453 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
454 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
455 .offs = 14,
456 .len = 1,
457 .veroffs = 15,
458 .maxblocks = 4,
459 .pattern = cafe_mirror_pattern_512
460};
461
462
David Woodhouse5467fb02006-10-06 15:36:29 +0100463static void cafe_nand_write_page_lowlevel(struct mtd_info *mtd,
464 struct nand_chip *chip, const uint8_t *buf)
465{
466 struct cafe_priv *cafe = mtd->priv;
467
David Woodhouse5467fb02006-10-06 15:36:29 +0100468 chip->write_buf(mtd, buf, mtd->writesize);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100469 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
David Woodhouse5467fb02006-10-06 15:36:29 +0100470
471 /* Set up ECC autogeneration */
David Woodhousecad40652006-11-01 08:19:20 +0800472 cafe->ctl2 |= (1<<30);
David Woodhouse5467fb02006-10-06 15:36:29 +0100473}
474
475static int cafe_nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
476 const uint8_t *buf, int page, int cached, int raw)
477{
478 int status;
479
David Woodhouse5467fb02006-10-06 15:36:29 +0100480 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
481
482 if (unlikely(raw))
483 chip->ecc.write_page_raw(mtd, chip, buf);
484 else
485 chip->ecc.write_page(mtd, chip, buf);
486
487 /*
488 * Cached progamming disabled for now, Not sure if its worth the
489 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
490 */
491 cached = 0;
492
493 if (!cached || !(chip->options & NAND_CACHEPRG)) {
494
495 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
496 status = chip->waitfunc(mtd, chip);
497 /*
498 * See if operation failed and additional status checks are
499 * available
500 */
501 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
502 status = chip->errstat(mtd, chip, FL_WRITING, status,
503 page);
504
505 if (status & NAND_STATUS_FAIL)
506 return -EIO;
507 } else {
508 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
509 status = chip->waitfunc(mtd, chip);
510 }
511
512#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
513 /* Send command to read back the data */
514 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
515
516 if (chip->verify_buf(mtd, buf, mtd->writesize))
517 return -EIO;
518#endif
519 return 0;
520}
521
David Woodhouse8dd851d2006-10-20 02:11:40 +0100522static int cafe_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
523{
524 return 0;
525}
David Woodhouse5467fb02006-10-06 15:36:29 +0100526
527static int __devinit cafe_nand_probe(struct pci_dev *pdev,
528 const struct pci_device_id *ent)
529{
530 struct mtd_info *mtd;
531 struct cafe_priv *cafe;
David Woodhouse527a4f42007-01-23 15:35:27 +0800532 uint32_t timing1, timing2, timing3;
David Woodhouse5467fb02006-10-06 15:36:29 +0100533 uint32_t ctrl;
534 int err = 0;
535
536 err = pci_enable_device(pdev);
537 if (err)
538 return err;
539
540 pci_set_master(pdev);
541
542 mtd = kzalloc(sizeof(*mtd) + sizeof(struct cafe_priv), GFP_KERNEL);
543 if (!mtd) {
544 dev_warn(&pdev->dev, "failed to alloc mtd_info\n");
545 return -ENOMEM;
546 }
547 cafe = (void *)(&mtd[1]);
548
549 mtd->priv = cafe;
550 mtd->owner = THIS_MODULE;
551
552 cafe->pdev = pdev;
553 cafe->mmio = pci_iomap(pdev, 0, 0);
554 if (!cafe->mmio) {
555 dev_warn(&pdev->dev, "failed to iomap\n");
556 err = -ENOMEM;
557 goto out_free_mtd;
558 }
559 cafe->dmabuf = dma_alloc_coherent(&cafe->pdev->dev, 2112 + sizeof(struct nand_buffers),
560 &cafe->dmaaddr, GFP_KERNEL);
561 if (!cafe->dmabuf) {
562 err = -ENOMEM;
563 goto out_ior;
564 }
565 cafe->nand.buffers = (void *)cafe->dmabuf + 2112;
566
567 cafe->nand.cmdfunc = cafe_nand_cmdfunc;
568 cafe->nand.dev_ready = cafe_device_ready;
569 cafe->nand.read_byte = cafe_read_byte;
570 cafe->nand.read_buf = cafe_read_buf;
571 cafe->nand.write_buf = cafe_write_buf;
572 cafe->nand.select_chip = cafe_select_chip;
573
574 cafe->nand.chip_delay = 0;
575
576 /* Enable the following for a flash based bad block table */
577 cafe->nand.options = NAND_USE_FLASH_BBT | NAND_NO_AUTOINCR | NAND_OWN_BUFFERS;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100578
579 if (skipbbt) {
580 cafe->nand.options |= NAND_SKIP_BBTSCAN;
581 cafe->nand.block_bad = cafe_nand_block_bad;
582 }
David Woodhousec9ac5972006-11-30 08:17:38 +0000583
David Woodhouse527a4f42007-01-23 15:35:27 +0800584 if (numtimings && numtimings != 3) {
585 dev_warn(&cafe->pdev->dev, "%d timing register values ignored; precisely three are required\n", numtimings);
586 }
587
588 if (numtimings == 3) {
589 timing1 = timing[0];
590 timing2 = timing[1];
591 timing3 = timing[2];
592 cafe_dev_dbg(&cafe->pdev->dev, "Using provided timings (%08x %08x %08x)\n",
593 timing1, timing2, timing3);
594 } else {
595 timing1 = cafe_readl(cafe, NAND_TIMING1);
596 timing2 = cafe_readl(cafe, NAND_TIMING2);
597 timing3 = cafe_readl(cafe, NAND_TIMING3);
598
599 if (timing1 | timing2 | timing3) {
600 cafe_dev_dbg(&cafe->pdev->dev, "Timing registers already set (%08x %08x %08x)\n", timing1, timing2, timing3);
601 } else {
602 dev_warn(&cafe->pdev->dev, "Timing registers unset; using most conservative defaults\n");
603 timing1 = timing2 = timing3 = 0xffffffff;
604 }
605 }
606
David Woodhousedcc41bc2006-10-27 09:55:34 +0300607 /* Start off by resetting the NAND controller completely */
David Woodhouse195a2532006-10-31 12:30:11 +0800608 cafe_writel(cafe, 1, NAND_RESET);
609 cafe_writel(cafe, 0, NAND_RESET);
610
David Woodhouse527a4f42007-01-23 15:35:27 +0800611 cafe_writel(cafe, timing1, NAND_TIMING1);
612 cafe_writel(cafe, timing2, NAND_TIMING2);
613 cafe_writel(cafe, timing3, NAND_TIMING3);
David Woodhousedcc41bc2006-10-27 09:55:34 +0300614
David Woodhouse195a2532006-10-31 12:30:11 +0800615 cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK);
David Woodhouse5467fb02006-10-06 15:36:29 +0100616 err = request_irq(pdev->irq, &cafe_nand_interrupt, SA_SHIRQ, "CAFE NAND", mtd);
617 if (err) {
618 dev_warn(&pdev->dev, "Could not register IRQ %d\n", pdev->irq);
David Woodhouse5467fb02006-10-06 15:36:29 +0100619 goto out_free_dma;
620 }
621#if 1
622 /* Disable master reset, enable NAND clock */
David Woodhouse195a2532006-10-31 12:30:11 +0800623 ctrl = cafe_readl(cafe, GLOBAL_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100624 ctrl &= 0xffffeff0;
625 ctrl |= 0x00007000;
David Woodhouse195a2532006-10-31 12:30:11 +0800626 cafe_writel(cafe, ctrl | 0x05, GLOBAL_CTRL);
627 cafe_writel(cafe, ctrl | 0x0a, GLOBAL_CTRL);
628 cafe_writel(cafe, 0, NAND_DMA_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100629
David Woodhouse195a2532006-10-31 12:30:11 +0800630 cafe_writel(cafe, 0x7006, GLOBAL_CTRL);
631 cafe_writel(cafe, 0x700a, GLOBAL_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100632
633 /* Set up DMA address */
David Woodhouse195a2532006-10-31 12:30:11 +0800634 cafe_writel(cafe, cafe->dmaaddr & 0xffffffff, NAND_DMA_ADDR0);
David Woodhouse5467fb02006-10-06 15:36:29 +0100635 if (sizeof(cafe->dmaaddr) > 4)
David Woodhousefbad5692006-10-22 15:09:33 +0100636 /* Shift in two parts to shut the compiler up */
David Woodhouse195a2532006-10-31 12:30:11 +0800637 cafe_writel(cafe, (cafe->dmaaddr >> 16) >> 16, NAND_DMA_ADDR1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100638 else
David Woodhouse195a2532006-10-31 12:30:11 +0800639 cafe_writel(cafe, 0, NAND_DMA_ADDR1);
David Woodhousefbad5692006-10-22 15:09:33 +0100640
David Woodhouse8dd851d2006-10-20 02:11:40 +0100641 cafe_dev_dbg(&cafe->pdev->dev, "Set DMA address to %x (virt %p)\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800642 cafe_readl(cafe, NAND_DMA_ADDR0), cafe->dmabuf);
David Woodhouse5467fb02006-10-06 15:36:29 +0100643
644 /* Enable NAND IRQ in global IRQ mask register */
David Woodhouse195a2532006-10-31 12:30:11 +0800645 cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100646 cafe_dev_dbg(&cafe->pdev->dev, "Control %x, IRQ mask %x\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800647 cafe_readl(cafe, GLOBAL_CTRL), cafe_readl(cafe, GLOBAL_IRQ_MASK));
David Woodhouse5467fb02006-10-06 15:36:29 +0100648#endif
649#if 1
650 mtd->writesize=2048;
651 mtd->oobsize = 0x40;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100652 memset(cafe->dmabuf, 0x5a, 2112);
David Woodhouse5467fb02006-10-06 15:36:29 +0100653 cafe->nand.cmdfunc(mtd, NAND_CMD_READID, 0, -1);
654 cafe->nand.read_byte(mtd);
655 cafe->nand.read_byte(mtd);
656 cafe->nand.read_byte(mtd);
657 cafe->nand.read_byte(mtd);
658 cafe->nand.read_byte(mtd);
659#endif
660#if 0
661 cafe->nand.cmdfunc(mtd, NAND_CMD_READ0, 0, 0);
662 // nand_wait_ready(mtd);
663 cafe->nand.read_byte(mtd);
664 cafe->nand.read_byte(mtd);
665 cafe->nand.read_byte(mtd);
666 cafe->nand.read_byte(mtd);
667#endif
668#if 0
669 writel(0x84600070, cafe->mmio);
670 udelay(10);
David Woodhouse195a2532006-10-31 12:30:11 +0800671 cafe_dev_dbg(&cafe->pdev->dev, "Status %x\n", cafe_readl(cafe, NAND_NONMEM));
David Woodhousec9ac5972006-11-30 08:17:38 +0000672#endif
David Woodhouse5467fb02006-10-06 15:36:29 +0100673 /* Scan to find existance of the device */
674 if (nand_scan_ident(mtd, 1)) {
675 err = -ENXIO;
676 goto out_irq;
677 }
678
679 cafe->ctl2 = 1<<27; /* Reed-Solomon ECC */
680 if (mtd->writesize == 2048)
681 cafe->ctl2 |= 1<<29; /* 2KiB page size */
682
683 /* Set up ECC according to the type of chip we found */
David Woodhousefbad5692006-10-22 15:09:33 +0100684 if (mtd->writesize == 2048) {
David Woodhouse8dd851d2006-10-20 02:11:40 +0100685 cafe->nand.ecc.layout = &cafe_oobinfo_2048;
686 cafe->nand.bbt_td = &cafe_bbt_main_descr_2048;
687 cafe->nand.bbt_md = &cafe_bbt_mirror_descr_2048;
David Woodhousefbad5692006-10-22 15:09:33 +0100688 } else if (mtd->writesize == 512) {
689 cafe->nand.ecc.layout = &cafe_oobinfo_512;
690 cafe->nand.bbt_td = &cafe_bbt_main_descr_512;
691 cafe->nand.bbt_md = &cafe_bbt_mirror_descr_512;
David Woodhouse5467fb02006-10-06 15:36:29 +0100692 } else {
David Woodhousefbad5692006-10-22 15:09:33 +0100693 printk(KERN_WARNING "Unexpected NAND flash writesize %d. Aborting\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100694 mtd->writesize);
David Woodhousefbad5692006-10-22 15:09:33 +0100695 goto out_irq;
David Woodhouse5467fb02006-10-06 15:36:29 +0100696 }
David Woodhousefbad5692006-10-22 15:09:33 +0100697 cafe->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
698 cafe->nand.ecc.size = mtd->writesize;
699 cafe->nand.ecc.bytes = 14;
700 cafe->nand.ecc.hwctl = (void *)cafe_nand_bug;
701 cafe->nand.ecc.calculate = (void *)cafe_nand_bug;
702 cafe->nand.ecc.correct = (void *)cafe_nand_bug;
703 cafe->nand.write_page = cafe_nand_write_page;
704 cafe->nand.ecc.write_page = cafe_nand_write_page_lowlevel;
705 cafe->nand.ecc.write_oob = cafe_nand_write_oob;
706 cafe->nand.ecc.read_page = cafe_nand_read_page;
707 cafe->nand.ecc.read_oob = cafe_nand_read_oob;
David Woodhouse5467fb02006-10-06 15:36:29 +0100708
709 err = nand_scan_tail(mtd);
710 if (err)
711 goto out_irq;
712
David Woodhouse5467fb02006-10-06 15:36:29 +0100713 pci_set_drvdata(pdev, mtd);
714 add_mtd_device(mtd);
715 goto out;
716
717 out_irq:
718 /* Disable NAND IRQ in global IRQ mask register */
David Woodhouse195a2532006-10-31 12:30:11 +0800719 cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
David Woodhouse5467fb02006-10-06 15:36:29 +0100720 free_irq(pdev->irq, mtd);
721 out_free_dma:
722 dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
723 out_ior:
724 pci_iounmap(pdev, cafe->mmio);
725 out_free_mtd:
726 kfree(mtd);
727 out:
728 return err;
729}
730
731static void __devexit cafe_nand_remove(struct pci_dev *pdev)
732{
733 struct mtd_info *mtd = pci_get_drvdata(pdev);
734 struct cafe_priv *cafe = mtd->priv;
735
736 del_mtd_device(mtd);
737 /* Disable NAND IRQ in global IRQ mask register */
David Woodhouse195a2532006-10-31 12:30:11 +0800738 cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
David Woodhouse5467fb02006-10-06 15:36:29 +0100739 free_irq(pdev->irq, mtd);
740 nand_release(mtd);
741 pci_iounmap(pdev, cafe->mmio);
742 dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
743 kfree(mtd);
744}
745
746static struct pci_device_id cafe_nand_tbl[] = {
747 { 0x11ab, 0x4100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MEMORY_FLASH << 8, 0xFFFF0 }
748};
749
750MODULE_DEVICE_TABLE(pci, cafe_nand_tbl);
751
752static struct pci_driver cafe_nand_pci_driver = {
753 .name = "CAFÉ NAND",
754 .id_table = cafe_nand_tbl,
755 .probe = cafe_nand_probe,
756 .remove = __devexit_p(cafe_nand_remove),
757#ifdef CONFIG_PMx
758 .suspend = cafe_nand_suspend,
759 .resume = cafe_nand_resume,
760#endif
761};
762
763static int cafe_nand_init(void)
764{
765 return pci_register_driver(&cafe_nand_pci_driver);
766}
767
768static void cafe_nand_exit(void)
769{
770 pci_unregister_driver(&cafe_nand_pci_driver);
771}
772module_init(cafe_nand_init);
773module_exit(cafe_nand_exit);
774
775MODULE_LICENSE("GPL");
776MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
777MODULE_DESCRIPTION("NAND flash driver for OLPC CAFE chip");
778
779/* Correct ECC for 2048 bytes of 0xff:
780 41 a0 71 65 54 27 f3 93 ec a9 be ed 0b a1 */
David Woodhouse8dd851d2006-10-20 02:11:40 +0100781
782/* dwmw2's B-test board, in case of completely screwing it:
783Bad eraseblock 2394 at 0x12b40000
784Bad eraseblock 2627 at 0x14860000
785Bad eraseblock 3349 at 0x1a2a0000
786*/