blob: 2d1c22dc88c15394fe7d1d621f5c32fdd1d937a4 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
David Woodhousec9ac59772006-11-30 08:17:38 +00002/*
David Woodhousefbad5692006-10-22 15:09:33 +01003 * Driver for One Laptop Per Child ‘CAFÉ’ controller, aka Marvell 88ALP01
David Woodhouse5467fb02006-10-06 15:36:29 +01004 *
David Woodhouse514fca42008-09-03 09:47:17 +01005 * The data sheet for this device can be found at:
Justin P. Mattock631dd1a2010-10-18 11:03:14 +02006 * http://wiki.laptop.org/go/Datasheets
David Woodhouse514fca42008-09-03 09:47:17 +01007 *
David Woodhouse5467fb02006-10-06 15:36:29 +01008 * Copyright © 2006 Red Hat, Inc.
9 * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
10 */
11
David Woodhouse8dd851d2006-10-20 02:11:40 +010012#define DEBUG
David Woodhouse5467fb02006-10-06 15:36:29 +010013
14#include <linux/device.h>
15#undef DEBUG
16#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020017#include <linux/mtd/rawnand.h>
David Woodhouse9c37f332007-10-28 21:56:39 -040018#include <linux/mtd/partitions.h>
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +020019#include <linux/rslib.h>
David Woodhouse5467fb02006-10-06 15:36:29 +010020#include <linux/pci.h>
21#include <linux/delay.h>
22#include <linux/interrupt.h>
Al Viroa1274302007-01-30 13:23:30 +000023#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Paul Gortmakera0e5cc52011-07-03 15:17:31 -040025#include <linux/module.h>
David Woodhouse5467fb02006-10-06 15:36:29 +010026#include <asm/io.h>
27
28#define CAFE_NAND_CTRL1 0x00
29#define CAFE_NAND_CTRL2 0x04
30#define CAFE_NAND_CTRL3 0x08
31#define CAFE_NAND_STATUS 0x0c
32#define CAFE_NAND_IRQ 0x10
33#define CAFE_NAND_IRQ_MASK 0x14
34#define CAFE_NAND_DATA_LEN 0x18
35#define CAFE_NAND_ADDR1 0x1c
36#define CAFE_NAND_ADDR2 0x20
37#define CAFE_NAND_TIMING1 0x24
38#define CAFE_NAND_TIMING2 0x28
39#define CAFE_NAND_TIMING3 0x2c
40#define CAFE_NAND_NONMEM 0x30
David Woodhouse04459d72006-10-22 02:18:48 +010041#define CAFE_NAND_ECC_RESULT 0x3C
David Woodhousefbad5692006-10-22 15:09:33 +010042#define CAFE_NAND_DMA_CTRL 0x40
43#define CAFE_NAND_DMA_ADDR0 0x44
44#define CAFE_NAND_DMA_ADDR1 0x48
David Woodhouse04459d72006-10-22 02:18:48 +010045#define CAFE_NAND_ECC_SYN01 0x50
46#define CAFE_NAND_ECC_SYN23 0x54
47#define CAFE_NAND_ECC_SYN45 0x58
48#define CAFE_NAND_ECC_SYN67 0x5c
David Woodhouse5467fb02006-10-06 15:36:29 +010049#define CAFE_NAND_READ_DATA 0x1000
50#define CAFE_NAND_WRITE_DATA 0x2000
51
David Woodhouse195a2532006-10-31 12:30:11 +080052#define CAFE_GLOBAL_CTRL 0x3004
53#define CAFE_GLOBAL_IRQ 0x3008
54#define CAFE_GLOBAL_IRQ_MASK 0x300c
55#define CAFE_NAND_RESET 0x3034
56
David Woodhouse048c37b2007-05-02 12:26:37 +010057/* Missing from the datasheet: bit 19 of CTRL1 sets CE0 vs. CE1 */
58#define CTRL1_CHIPSELECT (1<<19)
59
David Woodhouse5467fb02006-10-06 15:36:29 +010060struct cafe_priv {
61 struct nand_chip nand;
62 struct pci_dev *pdev;
63 void __iomem *mmio;
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +020064 struct rs_control *rs;
David Woodhouse5467fb02006-10-06 15:36:29 +010065 uint32_t ctl1;
66 uint32_t ctl2;
67 int datalen;
68 int nr_data;
69 int data_pos;
70 int page_addr;
Miquel Raynal73a27db2018-07-25 15:31:37 +020071 bool usedma;
David Woodhouse5467fb02006-10-06 15:36:29 +010072 dma_addr_t dmaaddr;
73 unsigned char *dmabuf;
David Woodhouse5467fb02006-10-06 15:36:29 +010074};
75
David Woodhouseb478c772006-10-27 14:50:04 +030076static int usedma = 1;
David Woodhouse5467fb02006-10-06 15:36:29 +010077module_param(usedma, int, 0644);
78
David Woodhouse8dd851d2006-10-20 02:11:40 +010079static int skipbbt = 0;
80module_param(skipbbt, int, 0644);
81
82static int debug = 0;
83module_param(debug, int, 0644);
84
David Woodhousebe8444b2006-10-31 12:36:04 +080085static int regdebug = 0;
86module_param(regdebug, int, 0644);
87
David Woodhouseb478c772006-10-27 14:50:04 +030088static int checkecc = 1;
David Woodhouse470b0a92006-10-23 14:29:04 +010089module_param(checkecc, int, 0644);
90
Al Viro64a6f952007-10-14 19:35:30 +010091static unsigned int numtimings;
David Woodhouse527a4f42007-01-23 15:35:27 +080092static int timing[3];
93module_param_array(timing, int, &numtimings, 0644);
David Woodhouseb478c772006-10-27 14:50:04 +030094
Philip Rakity68874412008-10-08 16:08:20 -070095static const char *part_probes[] = { "cmdlinepart", "RedBoot", NULL };
David Woodhouse9c37f332007-10-28 21:56:39 -040096
David Woodhouse04459d72006-10-22 02:18:48 +010097/* Hrm. Why isn't this already conditional on something in the struct device? */
David Woodhouse8dd851d2006-10-20 02:11:40 +010098#define cafe_dev_dbg(dev, args...) do { if (debug) dev_dbg(dev, ##args); } while(0)
99
David Woodhouse195a2532006-10-31 12:30:11 +0800100/* Make it easier to switch to PIO if we need to */
101#define cafe_readl(cafe, addr) readl((cafe)->mmio + CAFE_##addr)
102#define cafe_writel(cafe, datum, addr) writel(datum, (cafe)->mmio + CAFE_##addr)
David Woodhouse8dd851d2006-10-20 02:11:40 +0100103
Boris Brezillon50a487e2018-09-06 14:05:27 +0200104static int cafe_device_ready(struct nand_chip *chip)
David Woodhouse5467fb02006-10-06 15:36:29 +0100105{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100106 struct cafe_priv *cafe = nand_get_controller_data(chip);
Dan Carpenter48f8b642012-06-09 19:08:25 +0300107 int result = !!(cafe_readl(cafe, NAND_STATUS) & 0x40000000);
David Woodhouse195a2532006-10-31 12:30:11 +0800108 uint32_t irqs = cafe_readl(cafe, NAND_IRQ);
David Woodhousefbad5692006-10-22 15:09:33 +0100109
David Woodhouse195a2532006-10-31 12:30:11 +0800110 cafe_writel(cafe, irqs, NAND_IRQ);
David Woodhousefbad5692006-10-22 15:09:33 +0100111
David Woodhouse8dd851d2006-10-20 02:11:40 +0100112 cafe_dev_dbg(&cafe->pdev->dev, "NAND device is%s ready, IRQ %x (%x) (%x,%x)\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800113 result?"":" not", irqs, cafe_readl(cafe, NAND_IRQ),
114 cafe_readl(cafe, GLOBAL_IRQ), cafe_readl(cafe, GLOBAL_IRQ_MASK));
David Woodhousefbad5692006-10-22 15:09:33 +0100115
David Woodhouse5467fb02006-10-06 15:36:29 +0100116 return result;
117}
118
119
Boris Brezillonc0739d82018-09-06 14:05:23 +0200120static void cafe_write_buf(struct nand_chip *chip, const uint8_t *buf, int len)
David Woodhouse5467fb02006-10-06 15:36:29 +0100121{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100122 struct cafe_priv *cafe = nand_get_controller_data(chip);
David Woodhouse5467fb02006-10-06 15:36:29 +0100123
Miquel Raynal73a27db2018-07-25 15:31:37 +0200124 if (cafe->usedma)
David Woodhouse5467fb02006-10-06 15:36:29 +0100125 memcpy(cafe->dmabuf + cafe->datalen, buf, len);
126 else
127 memcpy_toio(cafe->mmio + CAFE_NAND_WRITE_DATA + cafe->datalen, buf, len);
David Woodhousefbad5692006-10-22 15:09:33 +0100128
David Woodhouse5467fb02006-10-06 15:36:29 +0100129 cafe->datalen += len;
130
David Woodhouse8dd851d2006-10-20 02:11:40 +0100131 cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes to write buffer. datalen 0x%x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100132 len, cafe->datalen);
133}
134
Boris Brezillon7e534322018-09-06 14:05:22 +0200135static void cafe_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
David Woodhouse5467fb02006-10-06 15:36:29 +0100136{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100137 struct cafe_priv *cafe = nand_get_controller_data(chip);
David Woodhouse5467fb02006-10-06 15:36:29 +0100138
Miquel Raynal73a27db2018-07-25 15:31:37 +0200139 if (cafe->usedma)
David Woodhouse5467fb02006-10-06 15:36:29 +0100140 memcpy(buf, cafe->dmabuf + cafe->datalen, len);
141 else
142 memcpy_fromio(buf, cafe->mmio + CAFE_NAND_READ_DATA + cafe->datalen, len);
143
David Woodhouse8dd851d2006-10-20 02:11:40 +0100144 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 +0100145 len, cafe->datalen);
146 cafe->datalen += len;
147}
148
Boris Brezillon7e534322018-09-06 14:05:22 +0200149static uint8_t cafe_read_byte(struct nand_chip *chip)
David Woodhouse5467fb02006-10-06 15:36:29 +0100150{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100151 struct cafe_priv *cafe = nand_get_controller_data(chip);
David Woodhouse5467fb02006-10-06 15:36:29 +0100152 uint8_t d;
153
Boris Brezillon7e534322018-09-06 14:05:22 +0200154 cafe_read_buf(chip, &d, 1);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100155 cafe_dev_dbg(&cafe->pdev->dev, "Read %02x\n", d);
David Woodhouse5467fb02006-10-06 15:36:29 +0100156
157 return d;
158}
159
Boris Brezillon5295cf22018-09-06 14:05:28 +0200160static void cafe_nand_cmdfunc(struct nand_chip *chip, unsigned command,
David Woodhouse5467fb02006-10-06 15:36:29 +0100161 int column, int page_addr)
162{
Boris Brezillon5295cf22018-09-06 14:05:28 +0200163 struct mtd_info *mtd = nand_to_mtd(chip);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100164 struct cafe_priv *cafe = nand_get_controller_data(chip);
David Woodhouse5467fb02006-10-06 15:36:29 +0100165 int adrbytes = 0;
166 uint32_t ctl1;
167 uint32_t doneint = 0x80000000;
David Woodhouse5467fb02006-10-06 15:36:29 +0100168
David Woodhouse8dd851d2006-10-20 02:11:40 +0100169 cafe_dev_dbg(&cafe->pdev->dev, "cmdfunc %02x, 0x%x, 0x%x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100170 command, column, page_addr);
171
172 if (command == NAND_CMD_ERASE2 || command == NAND_CMD_PAGEPROG) {
173 /* Second half of a command we already calculated */
David Woodhouse195a2532006-10-31 12:30:11 +0800174 cafe_writel(cafe, cafe->ctl2 | 0x100 | command, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100175 ctl1 = cafe->ctl1;
David Woodhousecad40652006-11-01 08:19:20 +0800176 cafe->ctl2 &= ~(1<<30);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100177 cafe_dev_dbg(&cafe->pdev->dev, "Continue command, ctl1 %08x, #data %d\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100178 cafe->ctl1, cafe->nr_data);
179 goto do_command;
180 }
181 /* Reset ECC engine */
David Woodhouse195a2532006-10-31 12:30:11 +0800182 cafe_writel(cafe, 0, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100183
184 /* Emulate NAND_CMD_READOOB on large-page chips */
185 if (mtd->writesize > 512 &&
186 command == NAND_CMD_READOOB) {
187 column += mtd->writesize;
188 command = NAND_CMD_READ0;
189 }
190
191 /* FIXME: Do we need to send read command before sending data
192 for small-page chips, to position the buffer correctly? */
193
194 if (column != -1) {
David Woodhouse195a2532006-10-31 12:30:11 +0800195 cafe_writel(cafe, column, NAND_ADDR1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100196 adrbytes = 2;
197 if (page_addr != -1)
198 goto write_adr2;
199 } else if (page_addr != -1) {
David Woodhouse195a2532006-10-31 12:30:11 +0800200 cafe_writel(cafe, page_addr & 0xffff, NAND_ADDR1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100201 page_addr >>= 16;
202 write_adr2:
David Woodhouse195a2532006-10-31 12:30:11 +0800203 cafe_writel(cafe, page_addr, NAND_ADDR2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100204 adrbytes += 2;
205 if (mtd->size > mtd->writesize << 16)
206 adrbytes++;
207 }
208
209 cafe->data_pos = cafe->datalen = 0;
210
David Woodhouse048c37b2007-05-02 12:26:37 +0100211 /* Set command valid bit, mask in the chip select bit */
212 ctl1 = 0x80000000 | command | (cafe->ctl1 & CTRL1_CHIPSELECT);
David Woodhouse5467fb02006-10-06 15:36:29 +0100213
214 /* Set RD or WR bits as appropriate */
215 if (command == NAND_CMD_READID || command == NAND_CMD_STATUS) {
216 ctl1 |= (1<<26); /* rd */
217 /* Always 5 bytes, for now */
David Woodhouse8dd851d2006-10-20 02:11:40 +0100218 cafe->datalen = 4;
David Woodhouse5467fb02006-10-06 15:36:29 +0100219 /* And one address cycle -- even for STATUS, since the controller doesn't work without */
220 adrbytes = 1;
221 } else if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
222 command == NAND_CMD_READOOB || command == NAND_CMD_RNDOUT) {
223 ctl1 |= 1<<26; /* rd */
224 /* For now, assume just read to end of page */
225 cafe->datalen = mtd->writesize + mtd->oobsize - column;
226 } else if (command == NAND_CMD_SEQIN)
227 ctl1 |= 1<<25; /* wr */
228
229 /* Set number of address bytes */
230 if (adrbytes)
231 ctl1 |= ((adrbytes-1)|8) << 27;
232
233 if (command == NAND_CMD_SEQIN || command == NAND_CMD_ERASE1) {
David Woodhousec9ac59772006-11-30 08:17:38 +0000234 /* Ignore the first command of a pair; the hardware
David Woodhouse5467fb02006-10-06 15:36:29 +0100235 deals with them both at once, later */
236 cafe->ctl1 = ctl1;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100237 cafe_dev_dbg(&cafe->pdev->dev, "Setup for delayed command, ctl1 %08x, dlen %x\n",
David Woodhouse5467fb02006-10-06 15:36:29 +0100238 cafe->ctl1, cafe->datalen);
239 return;
240 }
241 /* RNDOUT and READ0 commands need a following byte */
242 if (command == NAND_CMD_RNDOUT)
David Woodhouse195a2532006-10-31 12:30:11 +0800243 cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_RNDOUTSTART, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100244 else if (command == NAND_CMD_READ0 && mtd->writesize > 512)
David Woodhouse195a2532006-10-31 12:30:11 +0800245 cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_READSTART, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100246
247 do_command:
David Woodhousec9ac59772006-11-30 08:17:38 +0000248 cafe_dev_dbg(&cafe->pdev->dev, "dlen %x, ctl1 %x, ctl2 %x\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800249 cafe->datalen, ctl1, cafe_readl(cafe, NAND_CTRL2));
David Woodhousefbad5692006-10-22 15:09:33 +0100250
David Woodhouse5467fb02006-10-06 15:36:29 +0100251 /* NB: The datasheet lies -- we really should be subtracting 1 here */
David Woodhouse195a2532006-10-31 12:30:11 +0800252 cafe_writel(cafe, cafe->datalen, NAND_DATA_LEN);
253 cafe_writel(cafe, 0x90000000, NAND_IRQ);
Miquel Raynal73a27db2018-07-25 15:31:37 +0200254 if (cafe->usedma && (ctl1 & (3<<25))) {
David Woodhouse5467fb02006-10-06 15:36:29 +0100255 uint32_t dmactl = 0xc0000000 + cafe->datalen;
256 /* If WR or RD bits set, set up DMA */
257 if (ctl1 & (1<<26)) {
258 /* It's a read */
259 dmactl |= (1<<29);
260 /* ... so it's done when the DMA is done, not just
261 the command. */
262 doneint = 0x10000000;
263 }
David Woodhouse195a2532006-10-31 12:30:11 +0800264 cafe_writel(cafe, dmactl, NAND_DMA_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100265 }
David Woodhouse5467fb02006-10-06 15:36:29 +0100266 cafe->datalen = 0;
267
David Woodhousebe8444b2006-10-31 12:36:04 +0800268 if (unlikely(regdebug)) {
269 int i;
270 printk("About to write command %08x to register 0\n", ctl1);
271 for (i=4; i< 0x5c; i+=4)
272 printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
David Woodhousefbad5692006-10-22 15:09:33 +0100273 }
David Woodhousebe8444b2006-10-31 12:36:04 +0800274
David Woodhouse195a2532006-10-31 12:30:11 +0800275 cafe_writel(cafe, ctl1, NAND_CTRL1);
David Woodhouse5467fb02006-10-06 15:36:29 +0100276 /* Apply this short delay always to ensure that we do wait tWB in
277 * any case on any machine. */
278 ndelay(100);
279
280 if (1) {
Andrew Morton2a7295b22007-02-17 16:02:11 -0800281 int c;
David Woodhouse5467fb02006-10-06 15:36:29 +0100282 uint32_t irqs;
283
Andrew Morton2a7295b22007-02-17 16:02:11 -0800284 for (c = 500000; c != 0; c--) {
David Woodhouse195a2532006-10-31 12:30:11 +0800285 irqs = cafe_readl(cafe, NAND_IRQ);
David Woodhouse5467fb02006-10-06 15:36:29 +0100286 if (irqs & doneint)
287 break;
288 udelay(1);
David Woodhouse8dd851d2006-10-20 02:11:40 +0100289 if (!(c % 100000))
290 cafe_dev_dbg(&cafe->pdev->dev, "Wait for ready, IRQ %x\n", irqs);
David Woodhouse5467fb02006-10-06 15:36:29 +0100291 cpu_relax();
292 }
David Woodhouse195a2532006-10-31 12:30:11 +0800293 cafe_writel(cafe, doneint, NAND_IRQ);
David Woodhousea0207272006-10-28 17:08:38 +0300294 cafe_dev_dbg(&cafe->pdev->dev, "Command %x completed after %d usec, irqs %x (%x)\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800295 command, 500000-c, irqs, cafe_readl(cafe, NAND_IRQ));
David Woodhouse5467fb02006-10-06 15:36:29 +0100296 }
297
David Woodhousecad40652006-11-01 08:19:20 +0800298 WARN_ON(cafe->ctl2 & (1<<30));
David Woodhouse5467fb02006-10-06 15:36:29 +0100299
300 switch (command) {
301
302 case NAND_CMD_CACHEDPROG:
303 case NAND_CMD_PAGEPROG:
304 case NAND_CMD_ERASE1:
305 case NAND_CMD_ERASE2:
306 case NAND_CMD_SEQIN:
307 case NAND_CMD_RNDIN:
308 case NAND_CMD_STATUS:
David Woodhouse5467fb02006-10-06 15:36:29 +0100309 case NAND_CMD_RNDOUT:
David Woodhouse195a2532006-10-31 12:30:11 +0800310 cafe_writel(cafe, cafe->ctl2, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100311 return;
312 }
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200313 nand_wait_ready(chip);
David Woodhouse195a2532006-10-31 12:30:11 +0800314 cafe_writel(cafe, cafe->ctl2, NAND_CTRL2);
David Woodhouse5467fb02006-10-06 15:36:29 +0100315}
316
Boris Brezillon758b56f2018-09-06 14:05:24 +0200317static void cafe_select_chip(struct nand_chip *chip, int chipnr)
David Woodhouse5467fb02006-10-06 15:36:29 +0100318{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100319 struct cafe_priv *cafe = nand_get_controller_data(chip);
David Woodhouse048c37b2007-05-02 12:26:37 +0100320
321 cafe_dev_dbg(&cafe->pdev->dev, "select_chip %d\n", chipnr);
322
323 /* Mask the appropriate bit into the stored value of ctl1
324 which will be used by cafe_nand_cmdfunc() */
325 if (chipnr)
326 cafe->ctl1 |= CTRL1_CHIPSELECT;
327 else
328 cafe->ctl1 &= ~CTRL1_CHIPSELECT;
David Woodhouse5467fb02006-10-06 15:36:29 +0100329}
David Woodhousefbad5692006-10-22 15:09:33 +0100330
Alan Cox67cd7242009-04-22 15:02:23 +0100331static irqreturn_t cafe_nand_interrupt(int irq, void *id)
David Woodhouse5467fb02006-10-06 15:36:29 +0100332{
333 struct mtd_info *mtd = id;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100334 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100335 struct cafe_priv *cafe = nand_get_controller_data(chip);
David Woodhouse195a2532006-10-31 12:30:11 +0800336 uint32_t irqs = cafe_readl(cafe, NAND_IRQ);
337 cafe_writel(cafe, irqs & ~0x90000000, NAND_IRQ);
David Woodhouse5467fb02006-10-06 15:36:29 +0100338 if (!irqs)
339 return IRQ_NONE;
340
David Woodhouse195a2532006-10-31 12:30:11 +0800341 cafe_dev_dbg(&cafe->pdev->dev, "irq, bits %x (%x)\n", irqs, cafe_readl(cafe, NAND_IRQ));
David Woodhouse5467fb02006-10-06 15:36:29 +0100342 return IRQ_HANDLED;
343}
344
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200345static int cafe_nand_write_oob(struct nand_chip *chip, int page)
David Woodhouse5467fb02006-10-06 15:36:29 +0100346{
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200347 struct mtd_info *mtd = nand_to_mtd(chip);
348
Boris Brezillon97d90da2017-11-30 18:01:29 +0100349 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
350 mtd->oobsize);
David Woodhouse5467fb02006-10-06 15:36:29 +0100351}
352
353/* Don't use -- use nand_read_oob_std for now */
Boris Brezillonb9761682018-09-06 14:05:20 +0200354static int cafe_nand_read_oob(struct nand_chip *chip, int page)
David Woodhouse5467fb02006-10-06 15:36:29 +0100355{
Boris Brezillonb9761682018-09-06 14:05:20 +0200356 struct mtd_info *mtd = nand_to_mtd(chip);
357
Boris Brezillon97d90da2017-11-30 18:01:29 +0100358 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
David Woodhouse5467fb02006-10-06 15:36:29 +0100359}
360/**
Brian Norris7854d3f2011-06-23 14:12:08 -0700361 * cafe_nand_read_page_syndrome - [REPLACEABLE] hardware ecc syndrome based page read
David Woodhouse5467fb02006-10-06 15:36:29 +0100362 * @mtd: mtd info structure
363 * @chip: nand chip info structure
364 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -0700365 * @oob_required: caller expects OOB data read to chip->oob_poi
David Woodhouse5467fb02006-10-06 15:36:29 +0100366 *
Brian Norrisb9bc8152012-05-11 13:30:34 -0700367 * The hw generator calculates the error syndrome automatically. Therefore
David Woodhouse5467fb02006-10-06 15:36:29 +0100368 * we need a special oob layout and handling.
369 */
Boris Brezillonb9761682018-09-06 14:05:20 +0200370static int cafe_nand_read_page(struct nand_chip *chip, uint8_t *buf,
371 int oob_required, int page)
David Woodhouse5467fb02006-10-06 15:36:29 +0100372{
Boris Brezillonb9761682018-09-06 14:05:20 +0200373 struct mtd_info *mtd = nand_to_mtd(chip);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100374 struct cafe_priv *cafe = nand_get_controller_data(chip);
Mike Dunn3f91e942012-04-25 12:06:09 -0700375 unsigned int max_bitflips = 0;
David Woodhouse5467fb02006-10-06 15:36:29 +0100376
David Woodhousefbad5692006-10-22 15:09:33 +0100377 cafe_dev_dbg(&cafe->pdev->dev, "ECC result %08x SYN1,2 %08x\n",
David Woodhouse195a2532006-10-31 12:30:11 +0800378 cafe_readl(cafe, NAND_ECC_RESULT),
379 cafe_readl(cafe, NAND_ECC_SYN01));
David Woodhouse5467fb02006-10-06 15:36:29 +0100380
Boris Brezillon25f815f2017-11-30 18:01:30 +0100381 nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon716bbba2018-09-07 00:38:35 +0200382 chip->legacy.read_buf(chip, chip->oob_poi, mtd->oobsize);
David Woodhouse5467fb02006-10-06 15:36:29 +0100383
David Woodhouse195a2532006-10-31 12:30:11 +0800384 if (checkecc && cafe_readl(cafe, NAND_ECC_RESULT) & (1<<18)) {
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200385 unsigned short syn[8], pat[4];
386 int pos[4];
387 u8 *oob = chip->oob_poi;
388 int i, n;
David Woodhouse04459d72006-10-22 02:18:48 +0100389
390 for (i=0; i<8; i+=2) {
David Woodhouse195a2532006-10-31 12:30:11 +0800391 uint32_t tmp = cafe_readl(cafe, NAND_ECC_SYN01 + (i*2));
Thomas Gleixner21633982018-04-22 18:23:53 +0200392
393 syn[i] = cafe->rs->codec->index_of[tmp & 0xfff];
394 syn[i+1] = cafe->rs->codec->index_of[(tmp >> 16) & 0xfff];
David Woodhousec9ac59772006-11-30 08:17:38 +0000395 }
David Woodhouse04459d72006-10-22 02:18:48 +0100396
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200397 n = decode_rs16(cafe->rs, NULL, NULL, 1367, syn, 0, pos, 0,
Thomas Gleixner21633982018-04-22 18:23:53 +0200398 pat);
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200399
400 for (i = 0; i < n; i++) {
401 int p = pos[i];
402
403 /* The 12-bit symbols are mapped to bytes here */
404
405 if (p > 1374) {
406 /* out of range */
407 n = -1374;
408 } else if (p == 0) {
409 /* high four bits do not correspond to data */
410 if (pat[i] > 0xff)
411 n = -2048;
412 else
413 buf[0] ^= pat[i];
414 } else if (p == 1365) {
415 buf[2047] ^= pat[i] >> 4;
416 oob[0] ^= pat[i] << 4;
417 } else if (p > 1365) {
418 if ((p & 1) == 1) {
419 oob[3*p/2 - 2048] ^= pat[i] >> 4;
420 oob[3*p/2 - 2047] ^= pat[i] << 4;
421 } else {
422 oob[3*p/2 - 2049] ^= pat[i] >> 8;
423 oob[3*p/2 - 2048] ^= pat[i];
424 }
425 } else if ((p & 1) == 1) {
426 buf[3*p/2] ^= pat[i] >> 4;
427 buf[3*p/2 + 1] ^= pat[i] << 4;
428 } else {
429 buf[3*p/2 - 1] ^= pat[i] >> 8;
430 buf[3*p/2] ^= pat[i];
431 }
432 }
433
434 if (n < 0) {
David Woodhousebe8444b2006-10-31 12:36:04 +0800435 dev_dbg(&cafe->pdev->dev, "Failed to correct ECC at %08x\n",
436 cafe_readl(cafe, NAND_ADDR2) * 2048);
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200437 for (i = 0; i < 0x5c; i += 4)
David Woodhousebe8444b2006-10-31 12:36:04 +0800438 printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
David Woodhouse04459d72006-10-22 02:18:48 +0100439 mtd->ecc_stats.failed++;
440 } else {
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200441 dev_dbg(&cafe->pdev->dev, "Corrected %d symbol errors\n", n);
442 mtd->ecc_stats.corrected += n;
Mike Dunn3f91e942012-04-25 12:06:09 -0700443 max_bitflips = max_t(unsigned int, max_bitflips, n);
David Woodhouse04459d72006-10-22 02:18:48 +0100444 }
445 }
446
Mike Dunn3f91e942012-04-25 12:06:09 -0700447 return max_bitflips;
David Woodhouse5467fb02006-10-06 15:36:29 +0100448}
449
Boris Brezillona8ed6e62016-02-03 19:59:47 +0100450static int cafe_ooblayout_ecc(struct mtd_info *mtd, int section,
451 struct mtd_oob_region *oobregion)
452{
453 struct nand_chip *chip = mtd_to_nand(mtd);
454
455 if (section)
456 return -ERANGE;
457
458 oobregion->offset = 0;
459 oobregion->length = chip->ecc.total;
460
461 return 0;
462}
463
464static int cafe_ooblayout_free(struct mtd_info *mtd, int section,
465 struct mtd_oob_region *oobregion)
466{
467 struct nand_chip *chip = mtd_to_nand(mtd);
468
469 if (section)
470 return -ERANGE;
471
472 oobregion->offset = chip->ecc.total;
473 oobregion->length = mtd->oobsize - chip->ecc.total;
474
475 return 0;
476}
477
478static const struct mtd_ooblayout_ops cafe_ooblayout_ops = {
479 .ecc = cafe_ooblayout_ecc,
480 .free = cafe_ooblayout_free,
David Woodhouse8dd851d2006-10-20 02:11:40 +0100481};
482
David Woodhousec9ac59772006-11-30 08:17:38 +0000483/* Ick. The BBT code really ought to be able to work this bit out
David Woodhousefbad5692006-10-22 15:09:33 +0100484 for itself from the above, at least for the 2KiB case */
485static uint8_t cafe_bbt_pattern_2048[] = { 'B', 'b', 't', '0' };
486static uint8_t cafe_mirror_pattern_2048[] = { '1', 't', 'b', 'B' };
487
488static uint8_t cafe_bbt_pattern_512[] = { 0xBB };
489static uint8_t cafe_mirror_pattern_512[] = { 0xBC };
490
David Woodhouse8dd851d2006-10-20 02:11:40 +0100491
492static struct nand_bbt_descr cafe_bbt_main_descr_2048 = {
493 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
David Woodhouse048c37b2007-05-02 12:26:37 +0100494 | NAND_BBT_2BIT | NAND_BBT_VERSION,
David Woodhouse8dd851d2006-10-20 02:11:40 +0100495 .offs = 14,
496 .len = 4,
497 .veroffs = 18,
498 .maxblocks = 4,
David Woodhousefbad5692006-10-22 15:09:33 +0100499 .pattern = cafe_bbt_pattern_2048
David Woodhouse8dd851d2006-10-20 02:11:40 +0100500};
501
502static struct nand_bbt_descr cafe_bbt_mirror_descr_2048 = {
503 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
David Woodhouse048c37b2007-05-02 12:26:37 +0100504 | NAND_BBT_2BIT | NAND_BBT_VERSION,
David Woodhouse8dd851d2006-10-20 02:11:40 +0100505 .offs = 14,
506 .len = 4,
507 .veroffs = 18,
508 .maxblocks = 4,
David Woodhousefbad5692006-10-22 15:09:33 +0100509 .pattern = cafe_mirror_pattern_2048
David Woodhouse8dd851d2006-10-20 02:11:40 +0100510};
511
David Woodhousefbad5692006-10-22 15:09:33 +0100512static struct nand_bbt_descr cafe_bbt_main_descr_512 = {
513 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
David Woodhouse048c37b2007-05-02 12:26:37 +0100514 | NAND_BBT_2BIT | NAND_BBT_VERSION,
David Woodhousefbad5692006-10-22 15:09:33 +0100515 .offs = 14,
516 .len = 1,
517 .veroffs = 15,
518 .maxblocks = 4,
519 .pattern = cafe_bbt_pattern_512
520};
521
522static struct nand_bbt_descr cafe_bbt_mirror_descr_512 = {
523 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
David Woodhouse048c37b2007-05-02 12:26:37 +0100524 | NAND_BBT_2BIT | NAND_BBT_VERSION,
David Woodhousefbad5692006-10-22 15:09:33 +0100525 .offs = 14,
526 .len = 1,
527 .veroffs = 15,
528 .maxblocks = 4,
529 .pattern = cafe_mirror_pattern_512
530};
531
532
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200533static int cafe_nand_write_page_lowlevel(struct nand_chip *chip,
534 const uint8_t *buf, int oob_required,
535 int page)
David Woodhouse5467fb02006-10-06 15:36:29 +0100536{
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200537 struct mtd_info *mtd = nand_to_mtd(chip);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100538 struct cafe_priv *cafe = nand_get_controller_data(chip);
David Woodhouse5467fb02006-10-06 15:36:29 +0100539
Boris Brezillon25f815f2017-11-30 18:01:30 +0100540 nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon716bbba2018-09-07 00:38:35 +0200541 chip->legacy.write_buf(chip, chip->oob_poi, mtd->oobsize);
David Woodhouse5467fb02006-10-06 15:36:29 +0100542
543 /* Set up ECC autogeneration */
David Woodhousecad40652006-11-01 08:19:20 +0800544 cafe->ctl2 |= (1<<30);
Josh Wufdbad98d2012-06-25 18:07:45 +0800545
Boris Brezillon25f815f2017-11-30 18:01:30 +0100546 return nand_prog_page_end_op(chip);
David Woodhouse5467fb02006-10-06 15:36:29 +0100547}
548
Boris Brezillonc17556f2018-09-06 14:05:25 +0200549static int cafe_nand_block_bad(struct nand_chip *chip, loff_t ofs)
David Woodhouse8dd851d2006-10-20 02:11:40 +0100550{
551 return 0;
552}
David Woodhouse5467fb02006-10-06 15:36:29 +0100553
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200554/* F_2[X]/(X**6+X+1) */
Bill Pemberton06f25512012-11-19 13:23:07 -0500555static unsigned short gf64_mul(u8 a, u8 b)
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200556{
557 u8 c;
558 unsigned int i;
559
560 c = 0;
561 for (i = 0; i < 6; i++) {
562 if (a & 1)
563 c ^= b;
564 a >>= 1;
565 b <<= 1;
566 if ((b & 0x40) != 0)
567 b ^= 0x43;
568 }
569
570 return c;
571}
572
573/* F_64[X]/(X**2+X+A**-1) with A the generator of F_64[X] */
Bill Pemberton06f25512012-11-19 13:23:07 -0500574static u16 gf4096_mul(u16 a, u16 b)
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200575{
576 u8 ah, al, bh, bl, ch, cl;
577
578 ah = a >> 6;
579 al = a & 0x3f;
580 bh = b >> 6;
581 bl = b & 0x3f;
582
583 ch = gf64_mul(ah ^ al, bh ^ bl) ^ gf64_mul(al, bl);
584 cl = gf64_mul(gf64_mul(ah, bh), 0x21) ^ gf64_mul(al, bl);
585
586 return (ch << 6) ^ cl;
587}
588
Bill Pemberton06f25512012-11-19 13:23:07 -0500589static int cafe_mul(int x)
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200590{
591 if (x == 0)
592 return 1;
593 return gf4096_mul(x, 0xe01);
594}
595
Miquel Raynal73a27db2018-07-25 15:31:37 +0200596static int cafe_nand_attach_chip(struct nand_chip *chip)
597{
598 struct mtd_info *mtd = nand_to_mtd(chip);
599 struct cafe_priv *cafe = nand_get_controller_data(chip);
600 int err = 0;
601
602 cafe->dmabuf = dma_alloc_coherent(&cafe->pdev->dev, 2112,
603 &cafe->dmaaddr, GFP_KERNEL);
604 if (!cafe->dmabuf)
605 return -ENOMEM;
606
607 /* Set up DMA address */
608 cafe_writel(cafe, lower_32_bits(cafe->dmaaddr), NAND_DMA_ADDR0);
609 cafe_writel(cafe, upper_32_bits(cafe->dmaaddr), NAND_DMA_ADDR1);
610
611 cafe_dev_dbg(&cafe->pdev->dev, "Set DMA address to %x (virt %p)\n",
612 cafe_readl(cafe, NAND_DMA_ADDR0), cafe->dmabuf);
613
614 /* Restore the DMA flag */
615 cafe->usedma = usedma;
616
617 cafe->ctl2 = BIT(27); /* Reed-Solomon ECC */
618 if (mtd->writesize == 2048)
619 cafe->ctl2 |= BIT(29); /* 2KiB page size */
620
621 /* Set up ECC according to the type of chip we found */
622 mtd_set_ooblayout(mtd, &cafe_ooblayout_ops);
623 if (mtd->writesize == 2048) {
624 cafe->nand.bbt_td = &cafe_bbt_main_descr_2048;
625 cafe->nand.bbt_md = &cafe_bbt_mirror_descr_2048;
626 } else if (mtd->writesize == 512) {
627 cafe->nand.bbt_td = &cafe_bbt_main_descr_512;
628 cafe->nand.bbt_md = &cafe_bbt_mirror_descr_512;
629 } else {
630 dev_warn(&cafe->pdev->dev,
631 "Unexpected NAND flash writesize %d. Aborting\n",
632 mtd->writesize);
633 err = -ENOTSUPP;
634 goto out_free_dma;
635 }
636
637 cafe->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
638 cafe->nand.ecc.size = mtd->writesize;
639 cafe->nand.ecc.bytes = 14;
640 cafe->nand.ecc.strength = 4;
641 cafe->nand.ecc.write_page = cafe_nand_write_page_lowlevel;
642 cafe->nand.ecc.write_oob = cafe_nand_write_oob;
643 cafe->nand.ecc.read_page = cafe_nand_read_page;
644 cafe->nand.ecc.read_oob = cafe_nand_read_oob;
645
646 return 0;
647
648 out_free_dma:
649 dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
650
651 return err;
652}
653
654static void cafe_nand_detach_chip(struct nand_chip *chip)
655{
656 struct cafe_priv *cafe = nand_get_controller_data(chip);
657
658 dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
659}
660
661static const struct nand_controller_ops cafe_nand_controller_ops = {
662 .attach_chip = cafe_nand_attach_chip,
663 .detach_chip = cafe_nand_detach_chip,
664};
665
Bill Pemberton06f25512012-11-19 13:23:07 -0500666static int cafe_nand_probe(struct pci_dev *pdev,
David Woodhouse5467fb02006-10-06 15:36:29 +0100667 const struct pci_device_id *ent)
668{
669 struct mtd_info *mtd;
670 struct cafe_priv *cafe;
671 uint32_t ctrl;
672 int err = 0;
673
David Woodhouse06ed24e2007-10-06 14:44:12 -0400674 /* Very old versions shared the same PCI ident for all three
675 functions on the chip. Verify the class too... */
676 if ((pdev->class >> 8) != PCI_CLASS_MEMORY_FLASH)
677 return -ENODEV;
678
David Woodhouse5467fb02006-10-06 15:36:29 +0100679 err = pci_enable_device(pdev);
680 if (err)
681 return err;
682
683 pci_set_master(pdev);
684
Boris BREZILLONe787dfd2015-12-10 08:59:55 +0100685 cafe = kzalloc(sizeof(*cafe), GFP_KERNEL);
686 if (!cafe)
David Woodhouse5467fb02006-10-06 15:36:29 +0100687 return -ENOMEM;
David Woodhouse5467fb02006-10-06 15:36:29 +0100688
Boris BREZILLONe787dfd2015-12-10 08:59:55 +0100689 mtd = nand_to_mtd(&cafe->nand);
David Woodhousec451c7c2009-04-04 15:27:45 +0100690 mtd->dev.parent = &pdev->dev;
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100691 nand_set_controller_data(&cafe->nand, cafe);
David Woodhouse5467fb02006-10-06 15:36:29 +0100692
693 cafe->pdev = pdev;
694 cafe->mmio = pci_iomap(pdev, 0, 0);
695 if (!cafe->mmio) {
696 dev_warn(&pdev->dev, "failed to iomap\n");
697 err = -ENOMEM;
698 goto out_free_mtd;
699 }
David Woodhouse5467fb02006-10-06 15:36:29 +0100700
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200701 cafe->rs = init_rs_non_canonical(12, &cafe_mul, 0, 1, 8);
702 if (!cafe->rs) {
703 err = -ENOMEM;
704 goto out_ior;
705 }
706
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200707 cafe->nand.legacy.cmdfunc = cafe_nand_cmdfunc;
Boris Brezillon8395b752018-09-07 00:38:37 +0200708 cafe->nand.legacy.dev_ready = cafe_device_ready;
Boris Brezillon716bbba2018-09-07 00:38:35 +0200709 cafe->nand.legacy.read_byte = cafe_read_byte;
710 cafe->nand.legacy.read_buf = cafe_read_buf;
711 cafe->nand.legacy.write_buf = cafe_write_buf;
Boris Brezillon7d6c37e2018-11-11 08:55:22 +0100712 cafe->nand.legacy.select_chip = cafe_select_chip;
Boris Brezillon45240362018-09-07 00:38:40 +0200713 cafe->nand.legacy.set_features = nand_get_set_features_notsupp;
714 cafe->nand.legacy.get_features = nand_get_set_features_notsupp;
David Woodhouse5467fb02006-10-06 15:36:29 +0100715
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200716 cafe->nand.legacy.chip_delay = 0;
David Woodhouse5467fb02006-10-06 15:36:29 +0100717
718 /* Enable the following for a flash based bad block table */
Brian Norrisbb9ebd42011-05-31 16:31:23 -0700719 cafe->nand.bbt_options = NAND_BBT_USE_FLASH;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100720
721 if (skipbbt) {
722 cafe->nand.options |= NAND_SKIP_BBTSCAN;
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200723 cafe->nand.legacy.block_bad = cafe_nand_block_bad;
David Woodhouse8dd851d2006-10-20 02:11:40 +0100724 }
David Woodhousec9ac59772006-11-30 08:17:38 +0000725
David Woodhouse527a4f42007-01-23 15:35:27 +0800726 if (numtimings && numtimings != 3) {
727 dev_warn(&cafe->pdev->dev, "%d timing register values ignored; precisely three are required\n", numtimings);
728 }
729
730 if (numtimings == 3) {
David Woodhouse527a4f42007-01-23 15:35:27 +0800731 cafe_dev_dbg(&cafe->pdev->dev, "Using provided timings (%08x %08x %08x)\n",
David Woodhouse8e5368a2007-03-23 10:40:04 +0000732 timing[0], timing[1], timing[2]);
David Woodhouse527a4f42007-01-23 15:35:27 +0800733 } else {
David Woodhouse8e5368a2007-03-23 10:40:04 +0000734 timing[0] = cafe_readl(cafe, NAND_TIMING1);
735 timing[1] = cafe_readl(cafe, NAND_TIMING2);
736 timing[2] = cafe_readl(cafe, NAND_TIMING3);
David Woodhouse527a4f42007-01-23 15:35:27 +0800737
David Woodhouse8e5368a2007-03-23 10:40:04 +0000738 if (timing[0] | timing[1] | timing[2]) {
739 cafe_dev_dbg(&cafe->pdev->dev, "Timing registers already set (%08x %08x %08x)\n",
740 timing[0], timing[1], timing[2]);
David Woodhouse527a4f42007-01-23 15:35:27 +0800741 } else {
742 dev_warn(&cafe->pdev->dev, "Timing registers unset; using most conservative defaults\n");
David Woodhouse8e5368a2007-03-23 10:40:04 +0000743 timing[0] = timing[1] = timing[2] = 0xffffffff;
David Woodhouse527a4f42007-01-23 15:35:27 +0800744 }
745 }
746
David Woodhousedcc41bc2006-10-27 09:55:34 +0300747 /* Start off by resetting the NAND controller completely */
David Woodhouse195a2532006-10-31 12:30:11 +0800748 cafe_writel(cafe, 1, NAND_RESET);
749 cafe_writel(cafe, 0, NAND_RESET);
750
David Woodhouse8e5368a2007-03-23 10:40:04 +0000751 cafe_writel(cafe, timing[0], NAND_TIMING1);
752 cafe_writel(cafe, timing[1], NAND_TIMING2);
753 cafe_writel(cafe, timing[2], NAND_TIMING3);
David Woodhousedcc41bc2006-10-27 09:55:34 +0300754
David Woodhouse195a2532006-10-31 12:30:11 +0800755 cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK);
Thomas Gleixner2db63462007-02-14 00:33:20 -0800756 err = request_irq(pdev->irq, &cafe_nand_interrupt, IRQF_SHARED,
757 "CAFE NAND", mtd);
David Woodhouse5467fb02006-10-06 15:36:29 +0100758 if (err) {
759 dev_warn(&pdev->dev, "Could not register IRQ %d\n", pdev->irq);
Huang Shijief02ea4e2014-01-13 14:27:12 +0800760 goto out_ior;
David Woodhouse5467fb02006-10-06 15:36:29 +0100761 }
David Woodhousef7c37d72007-01-23 15:44:10 +0800762
David Woodhouse5467fb02006-10-06 15:36:29 +0100763 /* Disable master reset, enable NAND clock */
David Woodhouse195a2532006-10-31 12:30:11 +0800764 ctrl = cafe_readl(cafe, GLOBAL_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100765 ctrl &= 0xffffeff0;
766 ctrl |= 0x00007000;
David Woodhouse195a2532006-10-31 12:30:11 +0800767 cafe_writel(cafe, ctrl | 0x05, GLOBAL_CTRL);
768 cafe_writel(cafe, ctrl | 0x0a, GLOBAL_CTRL);
769 cafe_writel(cafe, 0, NAND_DMA_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100770
David Woodhouse195a2532006-10-31 12:30:11 +0800771 cafe_writel(cafe, 0x7006, GLOBAL_CTRL);
772 cafe_writel(cafe, 0x700a, GLOBAL_CTRL);
David Woodhouse5467fb02006-10-06 15:36:29 +0100773
Huang Shijief02ea4e2014-01-13 14:27:12 +0800774 /* Enable NAND IRQ in global IRQ mask register */
775 cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK);
776 cafe_dev_dbg(&cafe->pdev->dev, "Control %x, IRQ mask %x\n",
777 cafe_readl(cafe, GLOBAL_CTRL),
778 cafe_readl(cafe, GLOBAL_IRQ_MASK));
779
Miquel Raynal73a27db2018-07-25 15:31:37 +0200780 /* Do not use the DMA during the NAND identification */
781 cafe->usedma = 0;
Huang Shijief02ea4e2014-01-13 14:27:12 +0800782
783 /* Scan to find existence of the device */
Boris Brezillon7b6a9b22018-11-20 10:02:39 +0100784 cafe->nand.legacy.dummy_controller.ops = &cafe_nand_controller_ops;
Boris Brezillon00ad3782018-09-06 14:05:14 +0200785 err = nand_scan(&cafe->nand, 2);
Masahiro Yamada72480e42016-11-04 19:43:06 +0900786 if (err)
Huang Shijief02ea4e2014-01-13 14:27:12 +0800787 goto out_irq;
Huang Shijief02ea4e2014-01-13 14:27:12 +0800788
David Woodhouse5467fb02006-10-06 15:36:29 +0100789 pci_set_drvdata(pdev, mtd);
David Woodhouse9c37f332007-10-28 21:56:39 -0400790
Philip Rakity68874412008-10-08 16:08:20 -0700791 mtd->name = "cafe_nand";
Miquel Raynala446c992018-03-21 14:01:43 +0100792 err = mtd_device_parse_register(mtd, part_probes, NULL, NULL, 0);
793 if (err)
794 goto out_cleanup_nand;
Dmitry Eremin-Solenikov4d32de82011-06-02 18:00:29 +0400795
David Woodhouse5467fb02006-10-06 15:36:29 +0100796 goto out;
797
Miquel Raynala446c992018-03-21 14:01:43 +0100798 out_cleanup_nand:
799 nand_cleanup(&cafe->nand);
David Woodhouse5467fb02006-10-06 15:36:29 +0100800 out_irq:
801 /* Disable NAND IRQ in global IRQ mask register */
David Woodhouse195a2532006-10-31 12:30:11 +0800802 cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
David Woodhouse5467fb02006-10-06 15:36:29 +0100803 free_irq(pdev->irq, mtd);
David Woodhouse5467fb02006-10-06 15:36:29 +0100804 out_ior:
805 pci_iounmap(pdev, cafe->mmio);
806 out_free_mtd:
Boris BREZILLONe787dfd2015-12-10 08:59:55 +0100807 kfree(cafe);
David Woodhouse5467fb02006-10-06 15:36:29 +0100808 out:
809 return err;
810}
811
Bill Pemberton810b7e02012-11-19 13:26:04 -0500812static void cafe_nand_remove(struct pci_dev *pdev)
David Woodhouse5467fb02006-10-06 15:36:29 +0100813{
814 struct mtd_info *mtd = pci_get_drvdata(pdev);
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100815 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100816 struct cafe_priv *cafe = nand_get_controller_data(chip);
David Woodhouse5467fb02006-10-06 15:36:29 +0100817
David Woodhouse5467fb02006-10-06 15:36:29 +0100818 /* Disable NAND IRQ in global IRQ mask register */
David Woodhouse195a2532006-10-31 12:30:11 +0800819 cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
David Woodhouse5467fb02006-10-06 15:36:29 +0100820 free_irq(pdev->irq, mtd);
Boris Brezillon59ac2762018-09-06 14:05:15 +0200821 nand_release(chip);
Segher Boessenkool8c61b7a2007-05-02 12:18:49 +0200822 free_rs(cafe->rs);
David Woodhouse5467fb02006-10-06 15:36:29 +0100823 pci_iounmap(pdev, cafe->mmio);
Masahiro Yamadaf880b072017-12-05 17:47:14 +0900824 dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
Boris BREZILLONe787dfd2015-12-10 08:59:55 +0100825 kfree(cafe);
David Woodhouse5467fb02006-10-06 15:36:29 +0100826}
827
Márton Németh377ace02010-01-09 15:10:34 +0100828static const struct pci_device_id cafe_nand_tbl[] = {
David Woodhouse514fca42008-09-03 09:47:17 +0100829 { PCI_VENDOR_ID_MARVELL, PCI_DEVICE_ID_MARVELL_88ALP01_NAND,
830 PCI_ANY_ID, PCI_ANY_ID },
David Woodhouse06ed24e2007-10-06 14:44:12 -0400831 { }
David Woodhouse5467fb02006-10-06 15:36:29 +0100832};
833
834MODULE_DEVICE_TABLE(pci, cafe_nand_tbl);
835
David Woodhouse1fcf8ce2007-10-06 14:59:32 -0400836static int cafe_nand_resume(struct pci_dev *pdev)
837{
838 uint32_t ctrl;
839 struct mtd_info *mtd = pci_get_drvdata(pdev);
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100840 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100841 struct cafe_priv *cafe = nand_get_controller_data(chip);
David Woodhouse1fcf8ce2007-10-06 14:59:32 -0400842
843 /* Start off by resetting the NAND controller completely */
844 cafe_writel(cafe, 1, NAND_RESET);
845 cafe_writel(cafe, 0, NAND_RESET);
846 cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK);
847
848 /* Restore timing configuration */
849 cafe_writel(cafe, timing[0], NAND_TIMING1);
850 cafe_writel(cafe, timing[1], NAND_TIMING2);
851 cafe_writel(cafe, timing[2], NAND_TIMING3);
852
853 /* Disable master reset, enable NAND clock */
854 ctrl = cafe_readl(cafe, GLOBAL_CTRL);
855 ctrl &= 0xffffeff0;
856 ctrl |= 0x00007000;
857 cafe_writel(cafe, ctrl | 0x05, GLOBAL_CTRL);
858 cafe_writel(cafe, ctrl | 0x0a, GLOBAL_CTRL);
859 cafe_writel(cafe, 0, NAND_DMA_CTRL);
860 cafe_writel(cafe, 0x7006, GLOBAL_CTRL);
861 cafe_writel(cafe, 0x700a, GLOBAL_CTRL);
862
863 /* Set up DMA address */
864 cafe_writel(cafe, cafe->dmaaddr & 0xffffffff, NAND_DMA_ADDR0);
865 if (sizeof(cafe->dmaaddr) > 4)
866 /* Shift in two parts to shut the compiler up */
867 cafe_writel(cafe, (cafe->dmaaddr >> 16) >> 16, NAND_DMA_ADDR1);
868 else
869 cafe_writel(cafe, 0, NAND_DMA_ADDR1);
870
871 /* Enable NAND IRQ in global IRQ mask register */
872 cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK);
873 return 0;
874}
875
David Woodhouse5467fb02006-10-06 15:36:29 +0100876static struct pci_driver cafe_nand_pci_driver = {
877 .name = "CAFÉ NAND",
878 .id_table = cafe_nand_tbl,
879 .probe = cafe_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -0500880 .remove = cafe_nand_remove,
David Woodhouse5467fb02006-10-06 15:36:29 +0100881 .resume = cafe_nand_resume,
David Woodhouse5467fb02006-10-06 15:36:29 +0100882};
883
Axel Lin4d16cd62012-04-03 09:59:44 +0800884module_pci_driver(cafe_nand_pci_driver);
David Woodhouse5467fb02006-10-06 15:36:29 +0100885
886MODULE_LICENSE("GPL");
887MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
David Woodhousef7c37d72007-01-23 15:44:10 +0800888MODULE_DESCRIPTION("NAND flash driver for OLPC CAFÉ chip");