2 * drivers/mtd/nand/fsmc_nand.c
5 * Flexible Static Memory Controller (FSMC)
6 * Driver for NAND portions
8 * Copyright © 2010 ST Microelectronics
9 * Vipin Kumar <vipin.kumar@st.com>
12 * Based on drivers/mtd/nand/nomadik_nand.c
14 * This file is licensed under the terms of the GNU General Public
15 * License version 2. This program is licensed "as is" without any
16 * warranty of any kind, whether express or implied.
19 #include <linux/clk.h>
20 #include <linux/completion.h>
21 #include <linux/dmaengine.h>
22 #include <linux/dma-direction.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/err.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/resource.h>
28 #include <linux/sched.h>
29 #include <linux/types.h>
30 #include <linux/mtd/mtd.h>
31 #include <linux/mtd/nand.h>
32 #include <linux/mtd/nand_ecc.h>
33 #include <linux/platform_device.h>
35 #include <linux/mtd/partitions.h>
37 #include <linux/slab.h>
38 #include <linux/amba/bus.h>
39 #include <mtd/mtd-abi.h>
41 #define FSMC_NAND_BW8 1
42 #define FSMC_NAND_BW16 2
44 #define FSMC_MAX_NOR_BANKS 4
45 #define FSMC_MAX_NAND_BANKS 4
47 #define FSMC_FLASH_WIDTH8 1
48 #define FSMC_FLASH_WIDTH16 2
50 /* fsmc controller registers for NOR flash */
52 /* ctrl register definitions */
53 #define BANK_ENABLE (1 << 0)
54 #define MUXED (1 << 1)
55 #define NOR_DEV (2 << 2)
56 #define WIDTH_8 (0 << 4)
57 #define WIDTH_16 (1 << 4)
58 #define RSTPWRDWN (1 << 6)
59 #define WPROT (1 << 7)
60 #define WRT_ENABLE (1 << 12)
61 #define WAIT_ENB (1 << 13)
64 /* ctrl_tim register definitions */
66 #define FSMC_NOR_BANK_SZ 0x8
67 #define FSMC_NOR_REG_SIZE 0x40
69 #define FSMC_NOR_REG(base, bank, reg) (base + \
70 FSMC_NOR_BANK_SZ * (bank) + \
73 /* fsmc controller registers for NAND flash */
75 /* pc register definitions */
76 #define FSMC_RESET (1 << 0)
77 #define FSMC_WAITON (1 << 1)
78 #define FSMC_ENABLE (1 << 2)
79 #define FSMC_DEVTYPE_NAND (1 << 3)
80 #define FSMC_DEVWID_8 (0 << 4)
81 #define FSMC_DEVWID_16 (1 << 4)
82 #define FSMC_ECCEN (1 << 6)
83 #define FSMC_ECCPLEN_512 (0 << 7)
84 #define FSMC_ECCPLEN_256 (1 << 7)
85 #define FSMC_TCLR_1 (1)
86 #define FSMC_TCLR_SHIFT (9)
87 #define FSMC_TCLR_MASK (0xF)
88 #define FSMC_TAR_1 (1)
89 #define FSMC_TAR_SHIFT (13)
90 #define FSMC_TAR_MASK (0xF)
92 /* sts register definitions */
93 #define FSMC_CODE_RDY (1 << 15)
95 /* comm register definitions */
97 #define FSMC_TSET_SHIFT 0
98 #define FSMC_TSET_MASK 0xFF
99 #define FSMC_TWAIT_6 6
100 #define FSMC_TWAIT_SHIFT 8
101 #define FSMC_TWAIT_MASK 0xFF
102 #define FSMC_THOLD_4 4
103 #define FSMC_THOLD_SHIFT 16
104 #define FSMC_THOLD_MASK 0xFF
105 #define FSMC_THIZ_1 1
106 #define FSMC_THIZ_SHIFT 24
107 #define FSMC_THIZ_MASK 0xFF
113 #define FSMC_NAND_BANK_SZ 0x20
115 #define FSMC_NAND_REG(base, bank, reg) (base + FSMC_NOR_REG_SIZE + \
116 (FSMC_NAND_BANK_SZ * (bank)) + \
119 #define FSMC_BUSY_WAIT_TIMEOUT (1 * HZ)
121 struct fsmc_nand_timings {
136 * struct fsmc_nand_data - structure for FSMC NAND device state
138 * @pid: Part ID on the AMBA PrimeCell format
139 * @mtd: MTD info for a NAND flash.
140 * @nand: Chip related info for a NAND flash.
141 * @partitions: Partition info for a NAND Flash.
142 * @nr_partitions: Total number of partition of a NAND flash.
144 * @bank: Bank number for probed device.
145 * @clk: Clock structure for FSMC.
147 * @read_dma_chan: DMA channel for read access
148 * @write_dma_chan: DMA channel for write access to NAND
149 * @dma_access_complete: Completion structure
151 * @data_pa: NAND Physical port for Data.
152 * @data_va: NAND port for Data.
153 * @cmd_va: NAND port for Command.
154 * @addr_va: NAND port for Address.
155 * @regs_va: FSMC regs base address.
157 struct fsmc_nand_data {
159 struct nand_chip nand;
163 enum access_mode mode;
166 /* DMA related objects */
167 struct dma_chan *read_dma_chan;
168 struct dma_chan *write_dma_chan;
169 struct completion dma_access_complete;
171 struct fsmc_nand_timings *dev_timings;
174 void __iomem *data_va;
175 void __iomem *cmd_va;
176 void __iomem *addr_va;
177 void __iomem *regs_va;
180 static int fsmc_ecc1_ooblayout_ecc(struct mtd_info *mtd, int section,
181 struct mtd_oob_region *oobregion)
183 struct nand_chip *chip = mtd_to_nand(mtd);
185 if (section >= chip->ecc.steps)
188 oobregion->offset = (section * 16) + 2;
189 oobregion->length = 3;
194 static int fsmc_ecc1_ooblayout_free(struct mtd_info *mtd, int section,
195 struct mtd_oob_region *oobregion)
197 struct nand_chip *chip = mtd_to_nand(mtd);
199 if (section >= chip->ecc.steps)
202 oobregion->offset = (section * 16) + 8;
204 if (section < chip->ecc.steps - 1)
205 oobregion->length = 8;
207 oobregion->length = mtd->oobsize - oobregion->offset;
212 static const struct mtd_ooblayout_ops fsmc_ecc1_ooblayout_ops = {
213 .ecc = fsmc_ecc1_ooblayout_ecc,
214 .free = fsmc_ecc1_ooblayout_free,
218 * ECC placement definitions in oobfree type format.
219 * There are 13 bytes of ecc for every 512 byte block and it has to be read
220 * consecutively and immediately after the 512 byte data block for hardware to
221 * generate the error bit offsets in 512 byte data.
223 static int fsmc_ecc4_ooblayout_ecc(struct mtd_info *mtd, int section,
224 struct mtd_oob_region *oobregion)
226 struct nand_chip *chip = mtd_to_nand(mtd);
228 if (section >= chip->ecc.steps)
231 oobregion->length = chip->ecc.bytes;
233 if (!section && mtd->writesize <= 512)
234 oobregion->offset = 0;
236 oobregion->offset = (section * 16) + 2;
241 static int fsmc_ecc4_ooblayout_free(struct mtd_info *mtd, int section,
242 struct mtd_oob_region *oobregion)
244 struct nand_chip *chip = mtd_to_nand(mtd);
246 if (section >= chip->ecc.steps)
249 oobregion->offset = (section * 16) + 15;
251 if (section < chip->ecc.steps - 1)
252 oobregion->length = 3;
254 oobregion->length = mtd->oobsize - oobregion->offset;
259 static const struct mtd_ooblayout_ops fsmc_ecc4_ooblayout_ops = {
260 .ecc = fsmc_ecc4_ooblayout_ecc,
261 .free = fsmc_ecc4_ooblayout_free,
264 static inline struct fsmc_nand_data *mtd_to_fsmc(struct mtd_info *mtd)
266 return container_of(mtd_to_nand(mtd), struct fsmc_nand_data, nand);
270 * fsmc_cmd_ctrl - For facilitaing Hardware access
271 * This routine allows hardware specific access to control-lines(ALE,CLE)
273 static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
275 struct nand_chip *this = mtd_to_nand(mtd);
276 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
277 void __iomem *regs = host->regs_va;
278 unsigned int bank = host->bank;
280 if (ctrl & NAND_CTRL_CHANGE) {
283 if (ctrl & NAND_CLE) {
284 this->IO_ADDR_R = host->cmd_va;
285 this->IO_ADDR_W = host->cmd_va;
286 } else if (ctrl & NAND_ALE) {
287 this->IO_ADDR_R = host->addr_va;
288 this->IO_ADDR_W = host->addr_va;
290 this->IO_ADDR_R = host->data_va;
291 this->IO_ADDR_W = host->data_va;
294 pc = readl(FSMC_NAND_REG(regs, bank, PC));
299 writel_relaxed(pc, FSMC_NAND_REG(regs, bank, PC));
304 if (cmd != NAND_CMD_NONE)
305 writeb_relaxed(cmd, this->IO_ADDR_W);
309 * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
311 * This routine initializes timing parameters related to NAND memory access in
314 static void fsmc_nand_setup(void __iomem *regs, uint32_t bank,
315 uint32_t busw, struct fsmc_nand_timings *timings)
317 uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
318 uint32_t tclr, tar, thiz, thold, twait, tset;
319 struct fsmc_nand_timings *tims;
320 struct fsmc_nand_timings default_timings = {
324 .thold = FSMC_THOLD_4,
325 .twait = FSMC_TWAIT_6,
332 tims = &default_timings;
334 tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT;
335 tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT;
336 thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT;
337 thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT;
338 twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT;
339 tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT;
342 writel_relaxed(value | FSMC_DEVWID_16,
343 FSMC_NAND_REG(regs, bank, PC));
345 writel_relaxed(value | FSMC_DEVWID_8,
346 FSMC_NAND_REG(regs, bank, PC));
348 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) | tclr | tar,
349 FSMC_NAND_REG(regs, bank, PC));
350 writel_relaxed(thiz | thold | twait | tset,
351 FSMC_NAND_REG(regs, bank, COMM));
352 writel_relaxed(thiz | thold | twait | tset,
353 FSMC_NAND_REG(regs, bank, ATTRIB));
357 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
359 static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
361 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
362 void __iomem *regs = host->regs_va;
363 uint32_t bank = host->bank;
365 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCPLEN_256,
366 FSMC_NAND_REG(regs, bank, PC));
367 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCEN,
368 FSMC_NAND_REG(regs, bank, PC));
369 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) | FSMC_ECCEN,
370 FSMC_NAND_REG(regs, bank, PC));
374 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
375 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
378 static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
381 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
382 void __iomem *regs = host->regs_va;
383 uint32_t bank = host->bank;
385 unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
388 if (readl_relaxed(FSMC_NAND_REG(regs, bank, STS)) & FSMC_CODE_RDY)
392 } while (!time_after_eq(jiffies, deadline));
394 if (time_after_eq(jiffies, deadline)) {
395 dev_err(host->dev, "calculate ecc timed out\n");
399 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
400 ecc[0] = (uint8_t) (ecc_tmp >> 0);
401 ecc[1] = (uint8_t) (ecc_tmp >> 8);
402 ecc[2] = (uint8_t) (ecc_tmp >> 16);
403 ecc[3] = (uint8_t) (ecc_tmp >> 24);
405 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC2));
406 ecc[4] = (uint8_t) (ecc_tmp >> 0);
407 ecc[5] = (uint8_t) (ecc_tmp >> 8);
408 ecc[6] = (uint8_t) (ecc_tmp >> 16);
409 ecc[7] = (uint8_t) (ecc_tmp >> 24);
411 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC3));
412 ecc[8] = (uint8_t) (ecc_tmp >> 0);
413 ecc[9] = (uint8_t) (ecc_tmp >> 8);
414 ecc[10] = (uint8_t) (ecc_tmp >> 16);
415 ecc[11] = (uint8_t) (ecc_tmp >> 24);
417 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, STS));
418 ecc[12] = (uint8_t) (ecc_tmp >> 16);
424 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
425 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
428 static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
431 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
432 void __iomem *regs = host->regs_va;
433 uint32_t bank = host->bank;
436 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
437 ecc[0] = (uint8_t) (ecc_tmp >> 0);
438 ecc[1] = (uint8_t) (ecc_tmp >> 8);
439 ecc[2] = (uint8_t) (ecc_tmp >> 16);
444 /* Count the number of 0's in buff upto a max of max_bits */
445 static int count_written_bits(uint8_t *buff, int size, int max_bits)
447 int k, written_bits = 0;
449 for (k = 0; k < size; k++) {
450 written_bits += hweight8(~buff[k]);
451 if (written_bits > max_bits)
458 static void dma_complete(void *param)
460 struct fsmc_nand_data *host = param;
462 complete(&host->dma_access_complete);
465 static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len,
466 enum dma_data_direction direction)
468 struct dma_chan *chan;
469 struct dma_device *dma_dev;
470 struct dma_async_tx_descriptor *tx;
471 dma_addr_t dma_dst, dma_src, dma_addr;
473 unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
475 unsigned long time_left;
477 if (direction == DMA_TO_DEVICE)
478 chan = host->write_dma_chan;
479 else if (direction == DMA_FROM_DEVICE)
480 chan = host->read_dma_chan;
484 dma_dev = chan->device;
485 dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction);
487 if (direction == DMA_TO_DEVICE) {
489 dma_dst = host->data_pa;
491 dma_src = host->data_pa;
495 tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
498 dev_err(host->dev, "device_prep_dma_memcpy error\n");
503 tx->callback = dma_complete;
504 tx->callback_param = host;
505 cookie = tx->tx_submit(tx);
507 ret = dma_submit_error(cookie);
509 dev_err(host->dev, "dma_submit_error %d\n", cookie);
513 dma_async_issue_pending(chan);
516 wait_for_completion_timeout(&host->dma_access_complete,
517 msecs_to_jiffies(3000));
518 if (time_left == 0) {
519 dmaengine_terminate_all(chan);
520 dev_err(host->dev, "wait_for_completion_timeout\n");
528 dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
534 * fsmc_write_buf - write buffer to chip
535 * @mtd: MTD device structure
537 * @len: number of bytes to write
539 static void fsmc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
542 struct nand_chip *chip = mtd_to_nand(mtd);
544 if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
545 IS_ALIGNED(len, sizeof(uint32_t))) {
546 uint32_t *p = (uint32_t *)buf;
548 for (i = 0; i < len; i++)
549 writel_relaxed(p[i], chip->IO_ADDR_W);
551 for (i = 0; i < len; i++)
552 writeb_relaxed(buf[i], chip->IO_ADDR_W);
557 * fsmc_read_buf - read chip data into buffer
558 * @mtd: MTD device structure
559 * @buf: buffer to store date
560 * @len: number of bytes to read
562 static void fsmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
565 struct nand_chip *chip = mtd_to_nand(mtd);
567 if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
568 IS_ALIGNED(len, sizeof(uint32_t))) {
569 uint32_t *p = (uint32_t *)buf;
571 for (i = 0; i < len; i++)
572 p[i] = readl_relaxed(chip->IO_ADDR_R);
574 for (i = 0; i < len; i++)
575 buf[i] = readb_relaxed(chip->IO_ADDR_R);
580 * fsmc_read_buf_dma - read chip data into buffer
581 * @mtd: MTD device structure
582 * @buf: buffer to store date
583 * @len: number of bytes to read
585 static void fsmc_read_buf_dma(struct mtd_info *mtd, uint8_t *buf, int len)
587 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
589 dma_xfer(host, buf, len, DMA_FROM_DEVICE);
593 * fsmc_write_buf_dma - write buffer to chip
594 * @mtd: MTD device structure
596 * @len: number of bytes to write
598 static void fsmc_write_buf_dma(struct mtd_info *mtd, const uint8_t *buf,
601 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
603 dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
607 * fsmc_read_page_hwecc
608 * @mtd: mtd info structure
609 * @chip: nand chip info structure
610 * @buf: buffer to store read data
611 * @oob_required: caller expects OOB data read to chip->oob_poi
612 * @page: page number to read
614 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
615 * performed in a strict sequence as follows:
616 * data(512 byte) -> ecc(13 byte)
617 * After this read, fsmc hardware generates and reports error data bits(up to a
620 static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
621 uint8_t *buf, int oob_required, int page)
623 int i, j, s, stat, eccsize = chip->ecc.size;
624 int eccbytes = chip->ecc.bytes;
625 int eccsteps = chip->ecc.steps;
627 uint8_t *ecc_calc = chip->buffers->ecccalc;
628 uint8_t *ecc_code = chip->buffers->ecccode;
629 int off, len, group = 0;
631 * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
632 * end up reading 14 bytes (7 words) from oob. The local array is
633 * to maintain word alignment
636 uint8_t *oob = (uint8_t *)&ecc_oob[0];
637 unsigned int max_bitflips = 0;
639 for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
640 chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
641 chip->ecc.hwctl(mtd, NAND_ECC_READ);
642 chip->read_buf(mtd, p, eccsize);
644 for (j = 0; j < eccbytes;) {
645 struct mtd_oob_region oobregion;
648 ret = mtd_ooblayout_ecc(mtd, group++, &oobregion);
652 off = oobregion.offset;
653 len = oobregion.length;
656 * length is intentionally kept a higher multiple of 2
657 * to read at least 13 bytes even in case of 16 bit NAND
660 if (chip->options & NAND_BUSWIDTH_16)
661 len = roundup(len, 2);
663 chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
664 chip->read_buf(mtd, oob + j, len);
668 memcpy(&ecc_code[i], oob, chip->ecc.bytes);
669 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
671 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
673 mtd->ecc_stats.failed++;
675 mtd->ecc_stats.corrected += stat;
676 max_bitflips = max_t(unsigned int, max_bitflips, stat);
684 * fsmc_bch8_correct_data
685 * @mtd: mtd info structure
686 * @dat: buffer of read data
687 * @read_ecc: ecc read from device spare area
688 * @calc_ecc: ecc calculated from read data
690 * calc_ecc is a 104 bit information containing maximum of 8 error
691 * offset informations of 13 bits each in 512 bytes of read data.
693 static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
694 uint8_t *read_ecc, uint8_t *calc_ecc)
696 struct nand_chip *chip = mtd_to_nand(mtd);
697 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
698 void __iomem *regs = host->regs_va;
699 unsigned int bank = host->bank;
702 uint32_t ecc1, ecc2, ecc3, ecc4;
704 num_err = (readl_relaxed(FSMC_NAND_REG(regs, bank, STS)) >> 10) & 0xF;
706 /* no bit flipping */
707 if (likely(num_err == 0))
710 /* too many errors */
711 if (unlikely(num_err > 8)) {
713 * This is a temporary erase check. A newly erased page read
714 * would result in an ecc error because the oob data is also
715 * erased to FF and the calculated ecc for an FF data is not
717 * This is a workaround to skip performing correction in case
721 * For every page, each bit written as 0 is counted until these
722 * number of bits are greater than 8 (the maximum correction
723 * capability of FSMC for each 512 + 13 bytes)
726 int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
727 int bits_data = count_written_bits(dat, chip->ecc.size, 8);
729 if ((bits_ecc + bits_data) <= 8) {
731 memset(dat, 0xff, chip->ecc.size);
739 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
740 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
742 * calc_ecc is a 104 bit information containing maximum of 8 error
743 * offset informations of 13 bits each. calc_ecc is copied into a
744 * uint64_t array and error offset indexes are populated in err_idx
747 ecc1 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
748 ecc2 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC2));
749 ecc3 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC3));
750 ecc4 = readl_relaxed(FSMC_NAND_REG(regs, bank, STS));
752 err_idx[0] = (ecc1 >> 0) & 0x1FFF;
753 err_idx[1] = (ecc1 >> 13) & 0x1FFF;
754 err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
755 err_idx[3] = (ecc2 >> 7) & 0x1FFF;
756 err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
757 err_idx[5] = (ecc3 >> 1) & 0x1FFF;
758 err_idx[6] = (ecc3 >> 14) & 0x1FFF;
759 err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
763 change_bit(0, (unsigned long *)&err_idx[i]);
764 change_bit(1, (unsigned long *)&err_idx[i]);
766 if (err_idx[i] < chip->ecc.size * 8) {
767 change_bit(err_idx[i], (unsigned long *)dat);
774 static bool filter(struct dma_chan *chan, void *slave)
776 chan->private = slave;
780 static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
781 struct fsmc_nand_data *host,
782 struct nand_chip *nand)
784 struct device_node *np = pdev->dev.of_node;
790 if (!of_property_read_u32(np, "bank-width", &val)) {
792 nand->options |= NAND_BUSWIDTH_16;
793 } else if (val != 1) {
794 dev_err(&pdev->dev, "invalid bank-width %u\n", val);
799 if (of_get_property(np, "nand-skip-bbtscan", NULL))
800 nand->options |= NAND_SKIP_BBTSCAN;
802 host->dev_timings = devm_kzalloc(&pdev->dev,
803 sizeof(*host->dev_timings), GFP_KERNEL);
804 if (!host->dev_timings)
806 ret = of_property_read_u8_array(np, "timings", (u8 *)host->dev_timings,
807 sizeof(*host->dev_timings));
809 dev_info(&pdev->dev, "No timings in dts specified, using default timings!\n");
810 host->dev_timings = NULL;
813 /* Set default NAND bank to 0 */
815 if (!of_property_read_u32(np, "bank", &val)) {
817 dev_err(&pdev->dev, "invalid bank %u\n", val);
826 * fsmc_nand_probe - Probe function
827 * @pdev: platform device structure
829 static int __init fsmc_nand_probe(struct platform_device *pdev)
831 struct fsmc_nand_data *host;
832 struct mtd_info *mtd;
833 struct nand_chip *nand;
834 struct resource *res;
840 /* Allocate memory for the device structure (and zero it) */
841 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
847 ret = fsmc_nand_probe_config_dt(pdev, host, nand);
851 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
852 host->data_va = devm_ioremap_resource(&pdev->dev, res);
853 if (IS_ERR(host->data_va))
854 return PTR_ERR(host->data_va);
856 host->data_pa = (dma_addr_t)res->start;
858 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
859 host->addr_va = devm_ioremap_resource(&pdev->dev, res);
860 if (IS_ERR(host->addr_va))
861 return PTR_ERR(host->addr_va);
863 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
864 host->cmd_va = devm_ioremap_resource(&pdev->dev, res);
865 if (IS_ERR(host->cmd_va))
866 return PTR_ERR(host->cmd_va);
868 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
869 host->regs_va = devm_ioremap_resource(&pdev->dev, res);
870 if (IS_ERR(host->regs_va))
871 return PTR_ERR(host->regs_va);
873 host->clk = clk_get(&pdev->dev, NULL);
874 if (IS_ERR(host->clk)) {
875 dev_err(&pdev->dev, "failed to fetch block clock\n");
876 return PTR_ERR(host->clk);
879 ret = clk_prepare_enable(host->clk);
881 goto err_clk_prepare_enable;
884 * This device ID is actually a common AMBA ID as used on the
885 * AMBA PrimeCell bus. However it is not a PrimeCell.
887 for (pid = 0, i = 0; i < 4; i++)
888 pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
890 dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
891 "revision %02x, config %02x\n",
892 AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
893 AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
895 host->dev = &pdev->dev;
897 if (host->mode == USE_DMA_ACCESS)
898 init_completion(&host->dma_access_complete);
900 /* Link all private pointers */
901 mtd = nand_to_mtd(&host->nand);
902 nand_set_controller_data(nand, host);
903 nand_set_flash_node(nand, pdev->dev.of_node);
905 mtd->dev.parent = &pdev->dev;
906 nand->IO_ADDR_R = host->data_va;
907 nand->IO_ADDR_W = host->data_va;
908 nand->cmd_ctrl = fsmc_cmd_ctrl;
909 nand->chip_delay = 30;
912 * Setup default ECC mode. nand_dt_init() called from nand_scan_ident()
913 * can overwrite this value if the DT provides a different value.
915 nand->ecc.mode = NAND_ECC_HW;
916 nand->ecc.hwctl = fsmc_enable_hwecc;
917 nand->ecc.size = 512;
918 nand->badblockbits = 7;
920 switch (host->mode) {
923 dma_cap_set(DMA_MEMCPY, mask);
924 host->read_dma_chan = dma_request_channel(mask, filter, NULL);
925 if (!host->read_dma_chan) {
926 dev_err(&pdev->dev, "Unable to get read dma channel\n");
927 goto err_req_read_chnl;
929 host->write_dma_chan = dma_request_channel(mask, filter, NULL);
930 if (!host->write_dma_chan) {
931 dev_err(&pdev->dev, "Unable to get write dma channel\n");
932 goto err_req_write_chnl;
934 nand->read_buf = fsmc_read_buf_dma;
935 nand->write_buf = fsmc_write_buf_dma;
939 case USE_WORD_ACCESS:
940 nand->read_buf = fsmc_read_buf;
941 nand->write_buf = fsmc_write_buf;
945 fsmc_nand_setup(host->regs_va, host->bank,
946 nand->options & NAND_BUSWIDTH_16,
949 if (AMBA_REV_BITS(host->pid) >= 8) {
950 nand->ecc.read_page = fsmc_read_page_hwecc;
951 nand->ecc.calculate = fsmc_read_hwecc_ecc4;
952 nand->ecc.correct = fsmc_bch8_correct_data;
953 nand->ecc.bytes = 13;
954 nand->ecc.strength = 8;
958 * Scan to find existence of the device
960 ret = nand_scan_ident(mtd, 1, NULL);
962 dev_err(&pdev->dev, "No NAND Device found!\n");
966 if (AMBA_REV_BITS(host->pid) >= 8) {
967 switch (mtd->oobsize) {
975 dev_warn(&pdev->dev, "No oob scheme defined for oobsize %d\n",
981 mtd_set_ooblayout(mtd, &fsmc_ecc4_ooblayout_ops);
983 switch (nand->ecc.mode) {
985 dev_info(&pdev->dev, "Using 1-bit HW ECC scheme\n");
986 nand->ecc.calculate = fsmc_read_hwecc_ecc1;
987 nand->ecc.correct = nand_correct_data;
989 nand->ecc.strength = 1;
993 if (nand->ecc.algo == NAND_ECC_BCH) {
994 dev_info(&pdev->dev, "Using 4-bit SW BCH ECC scheme\n");
999 dev_err(&pdev->dev, "Unsupported ECC mode!\n");
1004 * Don't set layout for BCH4 SW ECC. This will be
1005 * generated later in nand_bch_init() later.
1007 if (nand->ecc.mode == NAND_ECC_HW) {
1008 switch (mtd->oobsize) {
1012 mtd_set_ooblayout(mtd,
1013 &fsmc_ecc1_ooblayout_ops);
1016 dev_warn(&pdev->dev,
1017 "No oob scheme defined for oobsize %d\n",
1025 /* Second stage of scan to fill MTD data-structures */
1026 ret = nand_scan_tail(mtd);
1031 ret = mtd_device_register(mtd, NULL, 0);
1035 platform_set_drvdata(pdev, host);
1036 dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
1041 if (host->mode == USE_DMA_ACCESS)
1042 dma_release_channel(host->write_dma_chan);
1044 if (host->mode == USE_DMA_ACCESS)
1045 dma_release_channel(host->read_dma_chan);
1047 clk_disable_unprepare(host->clk);
1048 err_clk_prepare_enable:
1056 static int fsmc_nand_remove(struct platform_device *pdev)
1058 struct fsmc_nand_data *host = platform_get_drvdata(pdev);
1061 nand_release(nand_to_mtd(&host->nand));
1063 if (host->mode == USE_DMA_ACCESS) {
1064 dma_release_channel(host->write_dma_chan);
1065 dma_release_channel(host->read_dma_chan);
1067 clk_disable_unprepare(host->clk);
1074 #ifdef CONFIG_PM_SLEEP
1075 static int fsmc_nand_suspend(struct device *dev)
1077 struct fsmc_nand_data *host = dev_get_drvdata(dev);
1079 clk_disable_unprepare(host->clk);
1083 static int fsmc_nand_resume(struct device *dev)
1085 struct fsmc_nand_data *host = dev_get_drvdata(dev);
1087 clk_prepare_enable(host->clk);
1088 fsmc_nand_setup(host->regs_va, host->bank,
1089 host->nand.options & NAND_BUSWIDTH_16,
1096 static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
1099 static const struct of_device_id fsmc_nand_id_table[] = {
1100 { .compatible = "st,spear600-fsmc-nand" },
1101 { .compatible = "stericsson,fsmc-nand" },
1104 MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);
1107 static struct platform_driver fsmc_nand_driver = {
1108 .remove = fsmc_nand_remove,
1110 .name = "fsmc-nand",
1111 .of_match_table = of_match_ptr(fsmc_nand_id_table),
1112 .pm = &fsmc_nand_pm_ops,
1116 module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe);
1118 MODULE_LICENSE("GPL");
1119 MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
1120 MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");