blob: c8c1d06b35ab79ba9556b0c5ad71367f01b35ac2 [file] [log] [blame]
Roland Stigge2944a442012-06-07 12:22:15 +02001/*
2 * NXP LPC32XX NAND SLC driver
3 *
4 * Authors:
5 * Kevin Wells <kevin.wells@nxp.com>
6 * Roland Stigge <stigge@antcom.de>
7 *
8 * Copyright © 2011 NXP Semiconductors
9 * Copyright © 2012 Roland Stigge
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 */
21
22#include <linux/slab.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <linux/mtd/mtd.h>
26#include <linux/mtd/nand.h>
27#include <linux/mtd/partitions.h>
28#include <linux/clk.h>
29#include <linux/err.h>
30#include <linux/delay.h>
31#include <linux/io.h>
32#include <linux/mm.h>
33#include <linux/dma-mapping.h>
34#include <linux/dmaengine.h>
35#include <linux/mtd/nand_ecc.h>
36#include <linux/gpio.h>
37#include <linux/of.h>
38#include <linux/of_mtd.h>
39#include <linux/of_gpio.h>
40#include <linux/amba/pl08x.h>
41
42#define LPC32XX_MODNAME "lpc32xx-nand"
43
44/**********************************************************************
45* SLC NAND controller register offsets
46**********************************************************************/
47
48#define SLC_DATA(x) (x + 0x000)
49#define SLC_ADDR(x) (x + 0x004)
50#define SLC_CMD(x) (x + 0x008)
51#define SLC_STOP(x) (x + 0x00C)
52#define SLC_CTRL(x) (x + 0x010)
53#define SLC_CFG(x) (x + 0x014)
54#define SLC_STAT(x) (x + 0x018)
55#define SLC_INT_STAT(x) (x + 0x01C)
56#define SLC_IEN(x) (x + 0x020)
57#define SLC_ISR(x) (x + 0x024)
58#define SLC_ICR(x) (x + 0x028)
59#define SLC_TAC(x) (x + 0x02C)
60#define SLC_TC(x) (x + 0x030)
61#define SLC_ECC(x) (x + 0x034)
62#define SLC_DMA_DATA(x) (x + 0x038)
63
64/**********************************************************************
65* slc_ctrl register definitions
66**********************************************************************/
67#define SLCCTRL_SW_RESET (1 << 2) /* Reset the NAND controller bit */
68#define SLCCTRL_ECC_CLEAR (1 << 1) /* Reset ECC bit */
69#define SLCCTRL_DMA_START (1 << 0) /* Start DMA channel bit */
70
71/**********************************************************************
72* slc_cfg register definitions
73**********************************************************************/
74#define SLCCFG_CE_LOW (1 << 5) /* Force CE low bit */
75#define SLCCFG_DMA_ECC (1 << 4) /* Enable DMA ECC bit */
76#define SLCCFG_ECC_EN (1 << 3) /* ECC enable bit */
77#define SLCCFG_DMA_BURST (1 << 2) /* DMA burst bit */
78#define SLCCFG_DMA_DIR (1 << 1) /* DMA write(0)/read(1) bit */
79#define SLCCFG_WIDTH (1 << 0) /* External device width, 0=8bit */
80
81/**********************************************************************
82* slc_stat register definitions
83**********************************************************************/
84#define SLCSTAT_DMA_FIFO (1 << 2) /* DMA FIFO has data bit */
85#define SLCSTAT_SLC_FIFO (1 << 1) /* SLC FIFO has data bit */
86#define SLCSTAT_NAND_READY (1 << 0) /* NAND device is ready bit */
87
88/**********************************************************************
89* slc_int_stat, slc_ien, slc_isr, and slc_icr register definitions
90**********************************************************************/
91#define SLCSTAT_INT_TC (1 << 1) /* Transfer count bit */
92#define SLCSTAT_INT_RDY_EN (1 << 0) /* Ready interrupt bit */
93
94/**********************************************************************
95* slc_tac register definitions
96**********************************************************************/
97/* Clock setting for RDY write sample wait time in 2*n clocks */
98#define SLCTAC_WDR(n) (((n) & 0xF) << 28)
99/* Write pulse width in clock cycles, 1 to 16 clocks */
100#define SLCTAC_WWIDTH(n) (((n) & 0xF) << 24)
101/* Write hold time of control and data signals, 1 to 16 clocks */
102#define SLCTAC_WHOLD(n) (((n) & 0xF) << 20)
103/* Write setup time of control and data signals, 1 to 16 clocks */
104#define SLCTAC_WSETUP(n) (((n) & 0xF) << 16)
105/* Clock setting for RDY read sample wait time in 2*n clocks */
106#define SLCTAC_RDR(n) (((n) & 0xF) << 12)
107/* Read pulse width in clock cycles, 1 to 16 clocks */
108#define SLCTAC_RWIDTH(n) (((n) & 0xF) << 8)
109/* Read hold time of control and data signals, 1 to 16 clocks */
110#define SLCTAC_RHOLD(n) (((n) & 0xF) << 4)
111/* Read setup time of control and data signals, 1 to 16 clocks */
112#define SLCTAC_RSETUP(n) (((n) & 0xF) << 0)
113
114/**********************************************************************
115* slc_ecc register definitions
116**********************************************************************/
117/* ECC line party fetch macro */
118#define SLCECC_TO_LINEPAR(n) (((n) >> 6) & 0x7FFF)
119#define SLCECC_TO_COLPAR(n) ((n) & 0x3F)
120
121/*
122 * DMA requires storage space for the DMA local buffer and the hardware ECC
123 * storage area. The DMA local buffer is only used if DMA mapping fails
124 * during runtime.
125 */
126#define LPC32XX_DMA_DATA_SIZE 4096
127#define LPC32XX_ECC_SAVE_SIZE ((4096 / 256) * 4)
128
129/* Number of bytes used for ECC stored in NAND per 256 bytes */
130#define LPC32XX_SLC_DEV_ECC_BYTES 3
131
132/*
133 * If the NAND base clock frequency can't be fetched, this frequency will be
134 * used instead as the base. This rate is used to setup the timing registers
135 * used for NAND accesses.
136 */
137#define LPC32XX_DEF_BUS_RATE 133250000
138
139/* Milliseconds for DMA FIFO timeout (unlikely anyway) */
140#define LPC32XX_DMA_TIMEOUT 100
141
142/*
143 * NAND ECC Layout for small page NAND devices
144 * Note: For large and huge page devices, the default layouts are used
145 */
146static struct nand_ecclayout lpc32xx_nand_oob_16 = {
147 .eccbytes = 6,
148 .eccpos = {10, 11, 12, 13, 14, 15},
149 .oobfree = {
150 { .offset = 0, .length = 4 },
151 { .offset = 6, .length = 4 },
152 },
153};
154
155static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
156static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
157
158/*
159 * Small page FLASH BBT descriptors, marker at offset 0, version at offset 6
160 * Note: Large page devices used the default layout
161 */
162static struct nand_bbt_descr bbt_smallpage_main_descr = {
163 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
164 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
165 .offs = 0,
166 .len = 4,
167 .veroffs = 6,
168 .maxblocks = 4,
169 .pattern = bbt_pattern
170};
171
172static struct nand_bbt_descr bbt_smallpage_mirror_descr = {
173 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
174 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
175 .offs = 0,
176 .len = 4,
177 .veroffs = 6,
178 .maxblocks = 4,
179 .pattern = mirror_pattern
180};
181
182/*
183 * NAND platform configuration structure
184 */
185struct lpc32xx_nand_cfg_slc {
186 uint32_t wdr_clks;
187 uint32_t wwidth;
188 uint32_t whold;
189 uint32_t wsetup;
190 uint32_t rdr_clks;
191 uint32_t rwidth;
192 uint32_t rhold;
193 uint32_t rsetup;
194 bool use_bbt;
Alexandre Pereira da Silvadf63fe72012-06-27 17:51:13 +0200195 int wp_gpio;
Roland Stigge2944a442012-06-07 12:22:15 +0200196 struct mtd_partition *parts;
197 unsigned num_parts;
198};
199
200struct lpc32xx_nand_host {
201 struct nand_chip nand_chip;
202 struct clk *clk;
203 struct mtd_info mtd;
204 void __iomem *io_base;
205 struct lpc32xx_nand_cfg_slc *ncfg;
206
207 struct completion comp;
208 struct dma_chan *dma_chan;
209 uint32_t dma_buf_len;
210 struct dma_slave_config dma_slave_config;
211 struct scatterlist sgl;
212
213 /*
214 * DMA and CPU addresses of ECC work area and data buffer
215 */
216 uint32_t *ecc_buf;
217 uint8_t *data_buf;
218 dma_addr_t io_base_dma;
219};
220
221static void lpc32xx_nand_setup(struct lpc32xx_nand_host *host)
222{
223 uint32_t clkrate, tmp;
224
225 /* Reset SLC controller */
226 writel(SLCCTRL_SW_RESET, SLC_CTRL(host->io_base));
227 udelay(1000);
228
229 /* Basic setup */
230 writel(0, SLC_CFG(host->io_base));
231 writel(0, SLC_IEN(host->io_base));
232 writel((SLCSTAT_INT_TC | SLCSTAT_INT_RDY_EN),
233 SLC_ICR(host->io_base));
234
235 /* Get base clock for SLC block */
236 clkrate = clk_get_rate(host->clk);
237 if (clkrate == 0)
238 clkrate = LPC32XX_DEF_BUS_RATE;
239
240 /* Compute clock setup values */
241 tmp = SLCTAC_WDR(host->ncfg->wdr_clks) |
242 SLCTAC_WWIDTH(1 + (clkrate / host->ncfg->wwidth)) |
243 SLCTAC_WHOLD(1 + (clkrate / host->ncfg->whold)) |
244 SLCTAC_WSETUP(1 + (clkrate / host->ncfg->wsetup)) |
245 SLCTAC_RDR(host->ncfg->rdr_clks) |
246 SLCTAC_RWIDTH(1 + (clkrate / host->ncfg->rwidth)) |
247 SLCTAC_RHOLD(1 + (clkrate / host->ncfg->rhold)) |
248 SLCTAC_RSETUP(1 + (clkrate / host->ncfg->rsetup));
249 writel(tmp, SLC_TAC(host->io_base));
250}
251
252/*
253 * Hardware specific access to control lines
254 */
255static void lpc32xx_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
256 unsigned int ctrl)
257{
258 uint32_t tmp;
259 struct nand_chip *chip = mtd->priv;
260 struct lpc32xx_nand_host *host = chip->priv;
261
262 /* Does CE state need to be changed? */
263 tmp = readl(SLC_CFG(host->io_base));
264 if (ctrl & NAND_NCE)
265 tmp |= SLCCFG_CE_LOW;
266 else
267 tmp &= ~SLCCFG_CE_LOW;
268 writel(tmp, SLC_CFG(host->io_base));
269
270 if (cmd != NAND_CMD_NONE) {
271 if (ctrl & NAND_CLE)
272 writel(cmd, SLC_CMD(host->io_base));
273 else
274 writel(cmd, SLC_ADDR(host->io_base));
275 }
276}
277
278/*
279 * Read the Device Ready pin
280 */
281static int lpc32xx_nand_device_ready(struct mtd_info *mtd)
282{
283 struct nand_chip *chip = mtd->priv;
284 struct lpc32xx_nand_host *host = chip->priv;
285 int rdy = 0;
286
287 if ((readl(SLC_STAT(host->io_base)) & SLCSTAT_NAND_READY) != 0)
288 rdy = 1;
289
290 return rdy;
291}
292
293/*
294 * Enable NAND write protect
295 */
296static void lpc32xx_wp_enable(struct lpc32xx_nand_host *host)
297{
Alexandre Pereira da Silvadf63fe72012-06-27 17:51:13 +0200298 if (gpio_is_valid(host->ncfg->wp_gpio))
299 gpio_set_value(host->ncfg->wp_gpio, 0);
Roland Stigge2944a442012-06-07 12:22:15 +0200300}
301
302/*
303 * Disable NAND write protect
304 */
305static void lpc32xx_wp_disable(struct lpc32xx_nand_host *host)
306{
Alexandre Pereira da Silvadf63fe72012-06-27 17:51:13 +0200307 if (gpio_is_valid(host->ncfg->wp_gpio))
308 gpio_set_value(host->ncfg->wp_gpio, 1);
Roland Stigge2944a442012-06-07 12:22:15 +0200309}
310
311/*
312 * Prepares SLC for transfers with H/W ECC enabled
313 */
314static void lpc32xx_nand_ecc_enable(struct mtd_info *mtd, int mode)
315{
316 /* Hardware ECC is enabled automatically in hardware as needed */
317}
318
319/*
320 * Calculates the ECC for the data
321 */
322static int lpc32xx_nand_ecc_calculate(struct mtd_info *mtd,
323 const unsigned char *buf,
324 unsigned char *code)
325{
326 /*
327 * ECC is calculated automatically in hardware during syndrome read
328 * and write operations, so it doesn't need to be calculated here.
329 */
330 return 0;
331}
332
333/*
334 * Read a single byte from NAND device
335 */
336static uint8_t lpc32xx_nand_read_byte(struct mtd_info *mtd)
337{
338 struct nand_chip *chip = mtd->priv;
339 struct lpc32xx_nand_host *host = chip->priv;
340
341 return (uint8_t)readl(SLC_DATA(host->io_base));
342}
343
344/*
345 * Simple device read without ECC
346 */
347static void lpc32xx_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
348{
349 struct nand_chip *chip = mtd->priv;
350 struct lpc32xx_nand_host *host = chip->priv;
351
352 /* Direct device read with no ECC */
353 while (len-- > 0)
354 *buf++ = (uint8_t)readl(SLC_DATA(host->io_base));
355}
356
357/*
358 * Simple device write without ECC
359 */
360static void lpc32xx_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
361{
362 struct nand_chip *chip = mtd->priv;
363 struct lpc32xx_nand_host *host = chip->priv;
364
365 /* Direct device write with no ECC */
366 while (len-- > 0)
367 writel((uint32_t)*buf++, SLC_DATA(host->io_base));
368}
369
370/*
371 * Verify data in buffer to data on device
372 */
373static int lpc32xx_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
374{
375 struct nand_chip *chip = mtd->priv;
376 struct lpc32xx_nand_host *host = chip->priv;
377 int i;
378
379 /* DATA register must be read as 32 bits or it will fail */
380 for (i = 0; i < len; i++) {
381 if (buf[i] != (uint8_t)readl(SLC_DATA(host->io_base)))
382 return -EFAULT;
383 }
384
385 return 0;
386}
387
388/*
389 * Read the OOB data from the device without ECC using FIFO method
390 */
391static int lpc32xx_nand_read_oob_syndrome(struct mtd_info *mtd,
392 struct nand_chip *chip, int page)
393{
394 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
395 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
396
397 return 0;
398}
399
400/*
401 * Write the OOB data to the device without ECC using FIFO method
402 */
403static int lpc32xx_nand_write_oob_syndrome(struct mtd_info *mtd,
404 struct nand_chip *chip, int page)
405{
406 int status;
407
408 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
409 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
410
411 /* Send command to program the OOB data */
412 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
413
414 status = chip->waitfunc(mtd, chip);
415
416 return status & NAND_STATUS_FAIL ? -EIO : 0;
417}
418
419/*
420 * Fills in the ECC fields in the OOB buffer with the hardware generated ECC
421 */
422static void lpc32xx_slc_ecc_copy(uint8_t *spare, const uint32_t *ecc, int count)
423{
424 int i;
425
426 for (i = 0; i < (count * 3); i += 3) {
427 uint32_t ce = ecc[i / 3];
428 ce = ~(ce << 2) & 0xFFFFFF;
429 spare[i + 2] = (uint8_t)(ce & 0xFF);
430 ce >>= 8;
431 spare[i + 1] = (uint8_t)(ce & 0xFF);
432 ce >>= 8;
433 spare[i] = (uint8_t)(ce & 0xFF);
434 }
435}
436
437static void lpc32xx_dma_complete_func(void *completion)
438{
439 complete(completion);
440}
441
442static int lpc32xx_xmit_dma(struct mtd_info *mtd, dma_addr_t dma,
443 void *mem, int len, enum dma_transfer_direction dir)
444{
445 struct nand_chip *chip = mtd->priv;
446 struct lpc32xx_nand_host *host = chip->priv;
447 struct dma_async_tx_descriptor *desc;
448 int flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
449 int res;
450
451 host->dma_slave_config.direction = dir;
452 host->dma_slave_config.src_addr = dma;
453 host->dma_slave_config.dst_addr = dma;
454 host->dma_slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
455 host->dma_slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
456 host->dma_slave_config.src_maxburst = 4;
457 host->dma_slave_config.dst_maxburst = 4;
458 /* DMA controller does flow control: */
459 host->dma_slave_config.device_fc = false;
460 if (dmaengine_slave_config(host->dma_chan, &host->dma_slave_config)) {
461 dev_err(mtd->dev.parent, "Failed to setup DMA slave\n");
462 return -ENXIO;
463 }
464
465 sg_init_one(&host->sgl, mem, len);
466
467 res = dma_map_sg(host->dma_chan->device->dev, &host->sgl, 1,
468 DMA_BIDIRECTIONAL);
469 if (res != 1) {
470 dev_err(mtd->dev.parent, "Failed to map sg list\n");
471 return -ENXIO;
472 }
473 desc = dmaengine_prep_slave_sg(host->dma_chan, &host->sgl, 1, dir,
474 flags);
475 if (!desc) {
476 dev_err(mtd->dev.parent, "Failed to prepare slave sg\n");
477 goto out1;
478 }
479
480 init_completion(&host->comp);
481 desc->callback = lpc32xx_dma_complete_func;
482 desc->callback_param = &host->comp;
483
484 dmaengine_submit(desc);
485 dma_async_issue_pending(host->dma_chan);
486
487 wait_for_completion_timeout(&host->comp, msecs_to_jiffies(1000));
488
489 dma_unmap_sg(host->dma_chan->device->dev, &host->sgl, 1,
490 DMA_BIDIRECTIONAL);
491
492 return 0;
493out1:
494 dma_unmap_sg(host->dma_chan->device->dev, &host->sgl, 1,
495 DMA_BIDIRECTIONAL);
496 return -ENXIO;
497}
498
499/*
500 * DMA read/write transfers with ECC support
501 */
502static int lpc32xx_xfer(struct mtd_info *mtd, uint8_t *buf, int eccsubpages,
503 int read)
504{
505 struct nand_chip *chip = mtd->priv;
506 struct lpc32xx_nand_host *host = chip->priv;
507 int i, status = 0;
508 unsigned long timeout;
509 int res;
510 enum dma_transfer_direction dir =
511 read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
512 uint8_t *dma_buf;
513 bool dma_mapped;
514
515 if ((void *)buf <= high_memory) {
516 dma_buf = buf;
517 dma_mapped = true;
518 } else {
519 dma_buf = host->data_buf;
520 dma_mapped = false;
521 if (!read)
522 memcpy(host->data_buf, buf, mtd->writesize);
523 }
524
525 if (read) {
526 writel(readl(SLC_CFG(host->io_base)) |
527 SLCCFG_DMA_DIR | SLCCFG_ECC_EN | SLCCFG_DMA_ECC |
528 SLCCFG_DMA_BURST, SLC_CFG(host->io_base));
529 } else {
530 writel((readl(SLC_CFG(host->io_base)) |
531 SLCCFG_ECC_EN | SLCCFG_DMA_ECC | SLCCFG_DMA_BURST) &
532 ~SLCCFG_DMA_DIR,
533 SLC_CFG(host->io_base));
534 }
535
536 /* Clear initial ECC */
537 writel(SLCCTRL_ECC_CLEAR, SLC_CTRL(host->io_base));
538
539 /* Transfer size is data area only */
540 writel(mtd->writesize, SLC_TC(host->io_base));
541
542 /* Start transfer in the NAND controller */
543 writel(readl(SLC_CTRL(host->io_base)) | SLCCTRL_DMA_START,
544 SLC_CTRL(host->io_base));
545
546 for (i = 0; i < chip->ecc.steps; i++) {
547 /* Data */
548 res = lpc32xx_xmit_dma(mtd, SLC_DMA_DATA(host->io_base_dma),
549 dma_buf + i * chip->ecc.size,
550 mtd->writesize / chip->ecc.steps, dir);
551 if (res)
552 return res;
553
554 /* Always _read_ ECC */
555 if (i == chip->ecc.steps - 1)
556 break;
557 if (!read) /* ECC availability delayed on write */
558 udelay(10);
559 res = lpc32xx_xmit_dma(mtd, SLC_ECC(host->io_base_dma),
560 &host->ecc_buf[i], 4, DMA_DEV_TO_MEM);
561 if (res)
562 return res;
563 }
564
565 /*
566 * According to NXP, the DMA can be finished here, but the NAND
567 * controller may still have buffered data. After porting to using the
568 * dmaengine DMA driver (amba-pl080), the condition (DMA_FIFO empty)
569 * appears to be always true, according to tests. Keeping the check for
570 * safety reasons for now.
571 */
572 if (readl(SLC_STAT(host->io_base)) & SLCSTAT_DMA_FIFO) {
573 dev_warn(mtd->dev.parent, "FIFO not empty!\n");
574 timeout = jiffies + msecs_to_jiffies(LPC32XX_DMA_TIMEOUT);
575 while ((readl(SLC_STAT(host->io_base)) & SLCSTAT_DMA_FIFO) &&
576 time_before(jiffies, timeout))
577 cpu_relax();
578 if (!time_before(jiffies, timeout)) {
579 dev_err(mtd->dev.parent, "FIFO held data too long\n");
580 status = -EIO;
581 }
582 }
583
584 /* Read last calculated ECC value */
585 if (!read)
586 udelay(10);
587 host->ecc_buf[chip->ecc.steps - 1] =
588 readl(SLC_ECC(host->io_base));
589
590 /* Flush DMA */
591 dmaengine_terminate_all(host->dma_chan);
592
593 if (readl(SLC_STAT(host->io_base)) & SLCSTAT_DMA_FIFO ||
594 readl(SLC_TC(host->io_base))) {
595 /* Something is left in the FIFO, something is wrong */
596 dev_err(mtd->dev.parent, "DMA FIFO failure\n");
597 status = -EIO;
598 }
599
600 /* Stop DMA & HW ECC */
601 writel(readl(SLC_CTRL(host->io_base)) & ~SLCCTRL_DMA_START,
602 SLC_CTRL(host->io_base));
603 writel(readl(SLC_CFG(host->io_base)) &
604 ~(SLCCFG_DMA_DIR | SLCCFG_ECC_EN | SLCCFG_DMA_ECC |
605 SLCCFG_DMA_BURST), SLC_CFG(host->io_base));
606
607 if (!dma_mapped && read)
608 memcpy(buf, host->data_buf, mtd->writesize);
609
610 return status;
611}
612
613/*
614 * Read the data and OOB data from the device, use ECC correction with the
615 * data, disable ECC for the OOB data
616 */
617static int lpc32xx_nand_read_page_syndrome(struct mtd_info *mtd,
618 struct nand_chip *chip, uint8_t *buf,
619 int oob_required, int page)
620{
621 struct lpc32xx_nand_host *host = chip->priv;
622 int stat, i, status;
623 uint8_t *oobecc, tmpecc[LPC32XX_ECC_SAVE_SIZE];
624
625 /* Issue read command */
626 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
627
628 /* Read data and oob, calculate ECC */
629 status = lpc32xx_xfer(mtd, buf, chip->ecc.steps, 1);
630
631 /* Get OOB data */
632 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
633
634 /* Convert to stored ECC format */
635 lpc32xx_slc_ecc_copy(tmpecc, (uint32_t *) host->ecc_buf, chip->ecc.steps);
636
637 /* Pointer to ECC data retrieved from NAND spare area */
638 oobecc = chip->oob_poi + chip->ecc.layout->eccpos[0];
639
640 for (i = 0; i < chip->ecc.steps; i++) {
641 stat = chip->ecc.correct(mtd, buf, oobecc,
642 &tmpecc[i * chip->ecc.bytes]);
643 if (stat < 0)
644 mtd->ecc_stats.failed++;
645 else
646 mtd->ecc_stats.corrected += stat;
647
648 buf += chip->ecc.size;
649 oobecc += chip->ecc.bytes;
650 }
651
652 return status;
653}
654
655/*
656 * Read the data and OOB data from the device, no ECC correction with the
657 * data or OOB data
658 */
659static int lpc32xx_nand_read_page_raw_syndrome(struct mtd_info *mtd,
660 struct nand_chip *chip,
661 uint8_t *buf, int oob_required,
662 int page)
663{
664 /* Issue read command */
665 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
666
667 /* Raw reads can just use the FIFO interface */
668 chip->read_buf(mtd, buf, chip->ecc.size * chip->ecc.steps);
669 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
670
671 return 0;
672}
673
674/*
675 * Write the data and OOB data to the device, use ECC with the data,
676 * disable ECC for the OOB data
677 */
678static int lpc32xx_nand_write_page_syndrome(struct mtd_info *mtd,
679 struct nand_chip *chip,
680 const uint8_t *buf, int oob_required)
681{
682 struct lpc32xx_nand_host *host = chip->priv;
683 uint8_t *pb = chip->oob_poi + chip->ecc.layout->eccpos[0];
684 int error;
685
686 /* Write data, calculate ECC on outbound data */
687 error = lpc32xx_xfer(mtd, (uint8_t *)buf, chip->ecc.steps, 0);
688 if (error)
689 return error;
690
691 /*
692 * The calculated ECC needs some manual work done to it before
693 * committing it to NAND. Process the calculated ECC and place
694 * the resultant values directly into the OOB buffer. */
695 lpc32xx_slc_ecc_copy(pb, (uint32_t *)host->ecc_buf, chip->ecc.steps);
696
697 /* Write ECC data to device */
698 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
699 return 0;
700}
701
702/*
703 * Write the data and OOB data to the device, no ECC correction with the
704 * data or OOB data
705 */
706static int lpc32xx_nand_write_page_raw_syndrome(struct mtd_info *mtd,
707 struct nand_chip *chip,
708 const uint8_t *buf,
709 int oob_required)
710{
711 /* Raw writes can just use the FIFO interface */
712 chip->write_buf(mtd, buf, chip->ecc.size * chip->ecc.steps);
713 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
714 return 0;
715}
716
Roland Stigge2944a442012-06-07 12:22:15 +0200717static int lpc32xx_nand_dma_setup(struct lpc32xx_nand_host *host)
718{
719 struct mtd_info *mtd = &host->mtd;
720 dma_cap_mask_t mask;
721
722 dma_cap_zero(mask);
723 dma_cap_set(DMA_SLAVE, mask);
Roland Stigge314a1562012-07-12 14:22:56 +0200724 host->dma_chan = dma_request_channel(mask, pl08x_filter_id, "nand-slc");
Roland Stigge2944a442012-06-07 12:22:15 +0200725 if (!host->dma_chan) {
726 dev_err(mtd->dev.parent, "Failed to request DMA channel\n");
727 return -EBUSY;
728 }
729
730 return 0;
731}
732
733#ifdef CONFIG_OF
734static struct lpc32xx_nand_cfg_slc *lpc32xx_parse_dt(struct device *dev)
735{
736 struct lpc32xx_nand_cfg_slc *pdata;
737 struct device_node *np = dev->of_node;
738
739 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
740 if (!pdata) {
741 dev_err(dev, "could not allocate memory for platform data\n");
742 return NULL;
743 }
744
745 of_property_read_u32(np, "nxp,wdr-clks", &pdata->wdr_clks);
746 of_property_read_u32(np, "nxp,wwidth", &pdata->wwidth);
747 of_property_read_u32(np, "nxp,whold", &pdata->whold);
748 of_property_read_u32(np, "nxp,wsetup", &pdata->wsetup);
749 of_property_read_u32(np, "nxp,rdr-clks", &pdata->rdr_clks);
750 of_property_read_u32(np, "nxp,rwidth", &pdata->rwidth);
751 of_property_read_u32(np, "nxp,rhold", &pdata->rhold);
752 of_property_read_u32(np, "nxp,rsetup", &pdata->rsetup);
753
754 if (!pdata->wdr_clks || !pdata->wwidth || !pdata->whold ||
755 !pdata->wsetup || !pdata->rdr_clks || !pdata->rwidth ||
756 !pdata->rhold || !pdata->rsetup) {
757 dev_err(dev, "chip parameters not specified correctly\n");
758 return NULL;
759 }
760
761 pdata->use_bbt = of_get_nand_on_flash_bbt(np);
Roland Stigge21535ab2012-06-27 17:51:14 +0200762 pdata->wp_gpio = of_get_named_gpio(np, "gpios", 0);
Roland Stigge2944a442012-06-07 12:22:15 +0200763
764 return pdata;
765}
766#else
767static struct lpc32xx_nand_cfg_slc *lpc32xx_parse_dt(struct device *dev)
768{
769 return NULL;
770}
771#endif
772
773/*
774 * Probe for NAND controller
775 */
776static int __devinit lpc32xx_nand_probe(struct platform_device *pdev)
777{
778 struct lpc32xx_nand_host *host;
779 struct mtd_info *mtd;
780 struct nand_chip *chip;
781 struct resource *rc;
782 struct mtd_part_parser_data ppdata = {};
783 int res;
784
785 rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
786 if (rc == NULL) {
787 dev_err(&pdev->dev, "No memory resource found for device\n");
788 return -EBUSY;
789 }
790
791 /* Allocate memory for the device structure (and zero it) */
792 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
793 if (!host) {
794 dev_err(&pdev->dev, "failed to allocate device structure\n");
795 return -ENOMEM;
796 }
797 host->io_base_dma = rc->start;
798
799 host->io_base = devm_request_and_ioremap(&pdev->dev, rc);
800 if (host->io_base == NULL) {
801 dev_err(&pdev->dev, "ioremap failed\n");
802 return -ENOMEM;
803 }
804
805 if (pdev->dev.of_node)
806 host->ncfg = lpc32xx_parse_dt(&pdev->dev);
807 else
808 host->ncfg = pdev->dev.platform_data;
809 if (!host->ncfg) {
810 dev_err(&pdev->dev, "Missing platform data\n");
811 return -ENOENT;
812 }
Roland Stigged5842ab2012-06-27 17:51:15 +0200813 if (host->ncfg->wp_gpio == -EPROBE_DEFER)
814 return -EPROBE_DEFER;
Alexandre Pereira da Silvadf63fe72012-06-27 17:51:13 +0200815 if (gpio_is_valid(host->ncfg->wp_gpio) &&
816 gpio_request(host->ncfg->wp_gpio, "NAND WP")) {
Roland Stigge2944a442012-06-07 12:22:15 +0200817 dev_err(&pdev->dev, "GPIO not available\n");
818 return -EBUSY;
819 }
820 lpc32xx_wp_disable(host);
821
822 mtd = &host->mtd;
823 chip = &host->nand_chip;
824 chip->priv = host;
825 mtd->priv = chip;
826 mtd->owner = THIS_MODULE;
827 mtd->dev.parent = &pdev->dev;
828
829 /* Get NAND clock */
830 host->clk = clk_get(&pdev->dev, NULL);
831 if (IS_ERR(host->clk)) {
832 dev_err(&pdev->dev, "Clock failure\n");
833 res = -ENOENT;
834 goto err_exit1;
835 }
836 clk_enable(host->clk);
837
838 /* Set NAND IO addresses and command/ready functions */
839 chip->IO_ADDR_R = SLC_DATA(host->io_base);
840 chip->IO_ADDR_W = SLC_DATA(host->io_base);
841 chip->cmd_ctrl = lpc32xx_nand_cmd_ctrl;
842 chip->dev_ready = lpc32xx_nand_device_ready;
843 chip->chip_delay = 20; /* 20us command delay time */
844
845 /* Init NAND controller */
846 lpc32xx_nand_setup(host);
847
848 platform_set_drvdata(pdev, host);
849
850 /* NAND callbacks for LPC32xx SLC hardware */
851 chip->ecc.mode = NAND_ECC_HW_SYNDROME;
852 chip->read_byte = lpc32xx_nand_read_byte;
853 chip->read_buf = lpc32xx_nand_read_buf;
854 chip->write_buf = lpc32xx_nand_write_buf;
855 chip->ecc.read_page_raw = lpc32xx_nand_read_page_raw_syndrome;
856 chip->ecc.read_page = lpc32xx_nand_read_page_syndrome;
857 chip->ecc.write_page_raw = lpc32xx_nand_write_page_raw_syndrome;
858 chip->ecc.write_page = lpc32xx_nand_write_page_syndrome;
859 chip->ecc.write_oob = lpc32xx_nand_write_oob_syndrome;
860 chip->ecc.read_oob = lpc32xx_nand_read_oob_syndrome;
861 chip->ecc.calculate = lpc32xx_nand_ecc_calculate;
862 chip->ecc.correct = nand_correct_data;
863 chip->ecc.strength = 1;
864 chip->ecc.hwctl = lpc32xx_nand_ecc_enable;
865 chip->verify_buf = lpc32xx_verify_buf;
866
867 /* bitflip_threshold's default is defined as ecc_strength anyway.
868 * Unfortunately, it is set only later at add_mtd_device(). Meanwhile
869 * being 0, it causes bad block table scanning errors in
870 * nand_scan_tail(), so preparing it here already. */
871 mtd->bitflip_threshold = chip->ecc.strength;
872
873 /*
874 * Allocate a large enough buffer for a single huge page plus
875 * extra space for the spare area and ECC storage area
876 */
877 host->dma_buf_len = LPC32XX_DMA_DATA_SIZE + LPC32XX_ECC_SAVE_SIZE;
878 host->data_buf = devm_kzalloc(&pdev->dev, host->dma_buf_len,
879 GFP_KERNEL);
880 if (host->data_buf == NULL) {
881 dev_err(&pdev->dev, "Error allocating memory\n");
882 res = -ENOMEM;
883 goto err_exit2;
884 }
885
886 res = lpc32xx_nand_dma_setup(host);
887 if (res) {
888 res = -EIO;
889 goto err_exit2;
890 }
891
892 /* Find NAND device */
893 if (nand_scan_ident(mtd, 1, NULL)) {
894 res = -ENXIO;
895 goto err_exit3;
896 }
897
898 /* OOB and ECC CPU and DMA work areas */
899 host->ecc_buf = (uint32_t *)(host->data_buf + LPC32XX_DMA_DATA_SIZE);
900
901 /*
902 * Small page FLASH has a unique OOB layout, but large and huge
903 * page FLASH use the standard layout. Small page FLASH uses a
904 * custom BBT marker layout.
905 */
906 if (mtd->writesize <= 512)
907 chip->ecc.layout = &lpc32xx_nand_oob_16;
908
909 /* These sizes remain the same regardless of page size */
910 chip->ecc.size = 256;
911 chip->ecc.bytes = LPC32XX_SLC_DEV_ECC_BYTES;
912 chip->ecc.prepad = chip->ecc.postpad = 0;
913
914 /* Avoid extra scan if using BBT, setup BBT support */
915 if (host->ncfg->use_bbt) {
916 chip->options |= NAND_SKIP_BBTSCAN;
917 chip->bbt_options |= NAND_BBT_USE_FLASH;
918
919 /*
920 * Use a custom BBT marker setup for small page FLASH that
921 * won't interfere with the ECC layout. Large and huge page
922 * FLASH use the standard layout.
923 */
924 if (mtd->writesize <= 512) {
925 chip->bbt_td = &bbt_smallpage_main_descr;
926 chip->bbt_md = &bbt_smallpage_mirror_descr;
927 }
928 }
929
930 /*
931 * Fills out all the uninitialized function pointers with the defaults
932 */
933 if (nand_scan_tail(mtd)) {
934 res = -ENXIO;
935 goto err_exit3;
936 }
937
938 /* Standard layout in FLASH for bad block tables */
939 if (host->ncfg->use_bbt) {
940 if (nand_default_bbt(mtd) < 0)
941 dev_err(&pdev->dev,
942 "Error initializing default bad block tables\n");
943 }
944
945 mtd->name = "nxp_lpc3220_slc";
946 ppdata.of_node = pdev->dev.of_node;
947 res = mtd_device_parse_register(mtd, NULL, &ppdata, host->ncfg->parts,
948 host->ncfg->num_parts);
949 if (!res)
950 return res;
951
952 nand_release(mtd);
953
954err_exit3:
955 dma_release_channel(host->dma_chan);
956err_exit2:
957 clk_disable(host->clk);
958 clk_put(host->clk);
959 platform_set_drvdata(pdev, NULL);
960err_exit1:
961 lpc32xx_wp_enable(host);
962 gpio_free(host->ncfg->wp_gpio);
963
964 return res;
965}
966
967/*
968 * Remove NAND device.
969 */
970static int __devexit lpc32xx_nand_remove(struct platform_device *pdev)
971{
972 uint32_t tmp;
973 struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
974 struct mtd_info *mtd = &host->mtd;
975
976 nand_release(mtd);
977 dma_release_channel(host->dma_chan);
978
979 /* Force CE high */
980 tmp = readl(SLC_CTRL(host->io_base));
981 tmp &= ~SLCCFG_CE_LOW;
982 writel(tmp, SLC_CTRL(host->io_base));
983
984 clk_disable(host->clk);
985 clk_put(host->clk);
986 platform_set_drvdata(pdev, NULL);
987 lpc32xx_wp_enable(host);
988 gpio_free(host->ncfg->wp_gpio);
989
990 return 0;
991}
992
993#ifdef CONFIG_PM
994static int lpc32xx_nand_resume(struct platform_device *pdev)
995{
996 struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
997
998 /* Re-enable NAND clock */
999 clk_enable(host->clk);
1000
1001 /* Fresh init of NAND controller */
1002 lpc32xx_nand_setup(host);
1003
1004 /* Disable write protect */
1005 lpc32xx_wp_disable(host);
1006
1007 return 0;
1008}
1009
1010static int lpc32xx_nand_suspend(struct platform_device *pdev, pm_message_t pm)
1011{
1012 uint32_t tmp;
1013 struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
1014
1015 /* Force CE high */
1016 tmp = readl(SLC_CTRL(host->io_base));
1017 tmp &= ~SLCCFG_CE_LOW;
1018 writel(tmp, SLC_CTRL(host->io_base));
1019
1020 /* Enable write protect for safety */
1021 lpc32xx_wp_enable(host);
1022
1023 /* Disable clock */
1024 clk_disable(host->clk);
1025
1026 return 0;
1027}
1028
1029#else
1030#define lpc32xx_nand_resume NULL
1031#define lpc32xx_nand_suspend NULL
1032#endif
1033
1034#if defined(CONFIG_OF)
1035static const struct of_device_id lpc32xx_nand_match[] = {
1036 { .compatible = "nxp,lpc3220-slc" },
1037 { /* sentinel */ },
1038};
1039MODULE_DEVICE_TABLE(of, lpc32xx_nand_match);
1040#endif
1041
1042static struct platform_driver lpc32xx_nand_driver = {
1043 .probe = lpc32xx_nand_probe,
1044 .remove = __devexit_p(lpc32xx_nand_remove),
1045 .resume = lpc32xx_nand_resume,
1046 .suspend = lpc32xx_nand_suspend,
1047 .driver = {
1048 .name = LPC32XX_MODNAME,
1049 .owner = THIS_MODULE,
1050 .of_match_table = of_match_ptr(lpc32xx_nand_match),
1051 },
1052};
1053
1054module_platform_driver(lpc32xx_nand_driver);
1055
1056MODULE_LICENSE("GPL");
1057MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
1058MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
1059MODULE_DESCRIPTION("NAND driver for the NXP LPC32XX SLC controller");