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 * fsmc_nand_platform_data - platform specific NAND controller config
137 * @nand_timings: timing setup for the physical NAND interface
138 * @partitions: partition table for the platform, use a default fallback
140 * @nr_partitions: the number of partitions in the previous entry
141 * @options: different options for the driver
143 * @bank: default bank
144 * platform-specific. If the controller only supports one bank
145 * this may be set to NULL
147 struct fsmc_nand_platform_data {
148 struct fsmc_nand_timings *nand_timings;
149 struct mtd_partition *partitions;
150 unsigned int nr_partitions;
151 unsigned int options;
154 enum access_mode mode;
156 /* priv structures for dma accesses */
158 void *write_dma_priv;
162 * struct fsmc_nand_data - structure for FSMC NAND device state
164 * @pid: Part ID on the AMBA PrimeCell format
165 * @mtd: MTD info for a NAND flash.
166 * @nand: Chip related info for a NAND flash.
167 * @partitions: Partition info for a NAND Flash.
168 * @nr_partitions: Total number of partition of a NAND flash.
170 * @bank: Bank number for probed device.
171 * @clk: Clock structure for FSMC.
173 * @read_dma_chan: DMA channel for read access
174 * @write_dma_chan: DMA channel for write access to NAND
175 * @dma_access_complete: Completion structure
177 * @data_pa: NAND Physical port for Data.
178 * @data_va: NAND port for Data.
179 * @cmd_va: NAND port for Command.
180 * @addr_va: NAND port for Address.
181 * @regs_va: FSMC regs base address.
183 struct fsmc_nand_data {
185 struct nand_chip nand;
186 struct mtd_partition *partitions;
187 unsigned int nr_partitions;
191 enum access_mode mode;
194 /* DMA related objects */
195 struct dma_chan *read_dma_chan;
196 struct dma_chan *write_dma_chan;
197 struct completion dma_access_complete;
199 struct fsmc_nand_timings *dev_timings;
202 void __iomem *data_va;
203 void __iomem *cmd_va;
204 void __iomem *addr_va;
205 void __iomem *regs_va;
207 void (*select_chip)(uint32_t bank, uint32_t busw);
210 static int fsmc_ecc1_ooblayout_ecc(struct mtd_info *mtd, int section,
211 struct mtd_oob_region *oobregion)
213 struct nand_chip *chip = mtd_to_nand(mtd);
215 if (section >= chip->ecc.steps)
218 oobregion->offset = (section * 16) + 2;
219 oobregion->length = 3;
224 static int fsmc_ecc1_ooblayout_free(struct mtd_info *mtd, int section,
225 struct mtd_oob_region *oobregion)
227 struct nand_chip *chip = mtd_to_nand(mtd);
229 if (section >= chip->ecc.steps)
232 oobregion->offset = (section * 16) + 8;
234 if (section < chip->ecc.steps - 1)
235 oobregion->length = 8;
237 oobregion->length = mtd->oobsize - oobregion->offset;
242 static const struct mtd_ooblayout_ops fsmc_ecc1_ooblayout_ops = {
243 .ecc = fsmc_ecc1_ooblayout_ecc,
244 .free = fsmc_ecc1_ooblayout_free,
248 * ECC placement definitions in oobfree type format.
249 * There are 13 bytes of ecc for every 512 byte block and it has to be read
250 * consecutively and immediately after the 512 byte data block for hardware to
251 * generate the error bit offsets in 512 byte data.
253 static int fsmc_ecc4_ooblayout_ecc(struct mtd_info *mtd, int section,
254 struct mtd_oob_region *oobregion)
256 struct nand_chip *chip = mtd_to_nand(mtd);
258 if (section >= chip->ecc.steps)
261 oobregion->length = chip->ecc.bytes;
263 if (!section && mtd->writesize <= 512)
264 oobregion->offset = 0;
266 oobregion->offset = (section * 16) + 2;
271 static int fsmc_ecc4_ooblayout_free(struct mtd_info *mtd, int section,
272 struct mtd_oob_region *oobregion)
274 struct nand_chip *chip = mtd_to_nand(mtd);
276 if (section >= chip->ecc.steps)
279 oobregion->offset = (section * 16) + 15;
281 if (section < chip->ecc.steps - 1)
282 oobregion->length = 3;
284 oobregion->length = mtd->oobsize - oobregion->offset;
289 static const struct mtd_ooblayout_ops fsmc_ecc4_ooblayout_ops = {
290 .ecc = fsmc_ecc4_ooblayout_ecc,
291 .free = fsmc_ecc4_ooblayout_free,
294 static inline struct fsmc_nand_data *mtd_to_fsmc(struct mtd_info *mtd)
296 return container_of(mtd_to_nand(mtd), struct fsmc_nand_data, nand);
299 /* Assert CS signal based on chipnr */
300 static void fsmc_select_chip(struct mtd_info *mtd, int chipnr)
302 struct nand_chip *chip = mtd_to_nand(mtd);
303 struct fsmc_nand_data *host;
305 host = mtd_to_fsmc(mtd);
309 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
315 if (host->select_chip)
316 host->select_chip(chipnr,
317 chip->options & NAND_BUSWIDTH_16);
321 dev_err(host->dev, "unsupported chip-select %d\n", chipnr);
326 * fsmc_cmd_ctrl - For facilitaing Hardware access
327 * This routine allows hardware specific access to control-lines(ALE,CLE)
329 static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
331 struct nand_chip *this = mtd_to_nand(mtd);
332 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
333 void __iomem *regs = host->regs_va;
334 unsigned int bank = host->bank;
336 if (ctrl & NAND_CTRL_CHANGE) {
339 if (ctrl & NAND_CLE) {
340 this->IO_ADDR_R = host->cmd_va;
341 this->IO_ADDR_W = host->cmd_va;
342 } else if (ctrl & NAND_ALE) {
343 this->IO_ADDR_R = host->addr_va;
344 this->IO_ADDR_W = host->addr_va;
346 this->IO_ADDR_R = host->data_va;
347 this->IO_ADDR_W = host->data_va;
350 pc = readl(FSMC_NAND_REG(regs, bank, PC));
355 writel_relaxed(pc, FSMC_NAND_REG(regs, bank, PC));
360 if (cmd != NAND_CMD_NONE)
361 writeb_relaxed(cmd, this->IO_ADDR_W);
365 * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
367 * This routine initializes timing parameters related to NAND memory access in
370 static void fsmc_nand_setup(void __iomem *regs, uint32_t bank,
371 uint32_t busw, struct fsmc_nand_timings *timings)
373 uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
374 uint32_t tclr, tar, thiz, thold, twait, tset;
375 struct fsmc_nand_timings *tims;
376 struct fsmc_nand_timings default_timings = {
380 .thold = FSMC_THOLD_4,
381 .twait = FSMC_TWAIT_6,
388 tims = &default_timings;
390 tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT;
391 tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT;
392 thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT;
393 thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT;
394 twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT;
395 tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT;
398 writel_relaxed(value | FSMC_DEVWID_16,
399 FSMC_NAND_REG(regs, bank, PC));
401 writel_relaxed(value | FSMC_DEVWID_8,
402 FSMC_NAND_REG(regs, bank, PC));
404 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) | tclr | tar,
405 FSMC_NAND_REG(regs, bank, PC));
406 writel_relaxed(thiz | thold | twait | tset,
407 FSMC_NAND_REG(regs, bank, COMM));
408 writel_relaxed(thiz | thold | twait | tset,
409 FSMC_NAND_REG(regs, bank, ATTRIB));
413 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
415 static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
417 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
418 void __iomem *regs = host->regs_va;
419 uint32_t bank = host->bank;
421 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCPLEN_256,
422 FSMC_NAND_REG(regs, bank, PC));
423 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCEN,
424 FSMC_NAND_REG(regs, bank, PC));
425 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) | FSMC_ECCEN,
426 FSMC_NAND_REG(regs, bank, PC));
430 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
431 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
434 static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
437 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
438 void __iomem *regs = host->regs_va;
439 uint32_t bank = host->bank;
441 unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
444 if (readl_relaxed(FSMC_NAND_REG(regs, bank, STS)) & FSMC_CODE_RDY)
448 } while (!time_after_eq(jiffies, deadline));
450 if (time_after_eq(jiffies, deadline)) {
451 dev_err(host->dev, "calculate ecc timed out\n");
455 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
456 ecc[0] = (uint8_t) (ecc_tmp >> 0);
457 ecc[1] = (uint8_t) (ecc_tmp >> 8);
458 ecc[2] = (uint8_t) (ecc_tmp >> 16);
459 ecc[3] = (uint8_t) (ecc_tmp >> 24);
461 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC2));
462 ecc[4] = (uint8_t) (ecc_tmp >> 0);
463 ecc[5] = (uint8_t) (ecc_tmp >> 8);
464 ecc[6] = (uint8_t) (ecc_tmp >> 16);
465 ecc[7] = (uint8_t) (ecc_tmp >> 24);
467 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC3));
468 ecc[8] = (uint8_t) (ecc_tmp >> 0);
469 ecc[9] = (uint8_t) (ecc_tmp >> 8);
470 ecc[10] = (uint8_t) (ecc_tmp >> 16);
471 ecc[11] = (uint8_t) (ecc_tmp >> 24);
473 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, STS));
474 ecc[12] = (uint8_t) (ecc_tmp >> 16);
480 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
481 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
484 static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
487 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
488 void __iomem *regs = host->regs_va;
489 uint32_t bank = host->bank;
492 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
493 ecc[0] = (uint8_t) (ecc_tmp >> 0);
494 ecc[1] = (uint8_t) (ecc_tmp >> 8);
495 ecc[2] = (uint8_t) (ecc_tmp >> 16);
500 /* Count the number of 0's in buff upto a max of max_bits */
501 static int count_written_bits(uint8_t *buff, int size, int max_bits)
503 int k, written_bits = 0;
505 for (k = 0; k < size; k++) {
506 written_bits += hweight8(~buff[k]);
507 if (written_bits > max_bits)
514 static void dma_complete(void *param)
516 struct fsmc_nand_data *host = param;
518 complete(&host->dma_access_complete);
521 static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len,
522 enum dma_data_direction direction)
524 struct dma_chan *chan;
525 struct dma_device *dma_dev;
526 struct dma_async_tx_descriptor *tx;
527 dma_addr_t dma_dst, dma_src, dma_addr;
529 unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
531 unsigned long time_left;
533 if (direction == DMA_TO_DEVICE)
534 chan = host->write_dma_chan;
535 else if (direction == DMA_FROM_DEVICE)
536 chan = host->read_dma_chan;
540 dma_dev = chan->device;
541 dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction);
543 if (direction == DMA_TO_DEVICE) {
545 dma_dst = host->data_pa;
547 dma_src = host->data_pa;
551 tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
554 dev_err(host->dev, "device_prep_dma_memcpy error\n");
559 tx->callback = dma_complete;
560 tx->callback_param = host;
561 cookie = tx->tx_submit(tx);
563 ret = dma_submit_error(cookie);
565 dev_err(host->dev, "dma_submit_error %d\n", cookie);
569 dma_async_issue_pending(chan);
572 wait_for_completion_timeout(&host->dma_access_complete,
573 msecs_to_jiffies(3000));
574 if (time_left == 0) {
575 dmaengine_terminate_all(chan);
576 dev_err(host->dev, "wait_for_completion_timeout\n");
584 dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
590 * fsmc_write_buf - write buffer to chip
591 * @mtd: MTD device structure
593 * @len: number of bytes to write
595 static void fsmc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
598 struct nand_chip *chip = mtd_to_nand(mtd);
600 if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
601 IS_ALIGNED(len, sizeof(uint32_t))) {
602 uint32_t *p = (uint32_t *)buf;
604 for (i = 0; i < len; i++)
605 writel_relaxed(p[i], chip->IO_ADDR_W);
607 for (i = 0; i < len; i++)
608 writeb_relaxed(buf[i], chip->IO_ADDR_W);
613 * fsmc_read_buf - read chip data into buffer
614 * @mtd: MTD device structure
615 * @buf: buffer to store date
616 * @len: number of bytes to read
618 static void fsmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
621 struct nand_chip *chip = mtd_to_nand(mtd);
623 if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
624 IS_ALIGNED(len, sizeof(uint32_t))) {
625 uint32_t *p = (uint32_t *)buf;
627 for (i = 0; i < len; i++)
628 p[i] = readl_relaxed(chip->IO_ADDR_R);
630 for (i = 0; i < len; i++)
631 buf[i] = readb_relaxed(chip->IO_ADDR_R);
636 * fsmc_read_buf_dma - read chip data into buffer
637 * @mtd: MTD device structure
638 * @buf: buffer to store date
639 * @len: number of bytes to read
641 static void fsmc_read_buf_dma(struct mtd_info *mtd, uint8_t *buf, int len)
643 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
645 dma_xfer(host, buf, len, DMA_FROM_DEVICE);
649 * fsmc_write_buf_dma - write buffer to chip
650 * @mtd: MTD device structure
652 * @len: number of bytes to write
654 static void fsmc_write_buf_dma(struct mtd_info *mtd, const uint8_t *buf,
657 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
659 dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
663 * fsmc_read_page_hwecc
664 * @mtd: mtd info structure
665 * @chip: nand chip info structure
666 * @buf: buffer to store read data
667 * @oob_required: caller expects OOB data read to chip->oob_poi
668 * @page: page number to read
670 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
671 * performed in a strict sequence as follows:
672 * data(512 byte) -> ecc(13 byte)
673 * After this read, fsmc hardware generates and reports error data bits(up to a
676 static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
677 uint8_t *buf, int oob_required, int page)
679 int i, j, s, stat, eccsize = chip->ecc.size;
680 int eccbytes = chip->ecc.bytes;
681 int eccsteps = chip->ecc.steps;
683 uint8_t *ecc_calc = chip->buffers->ecccalc;
684 uint8_t *ecc_code = chip->buffers->ecccode;
685 int off, len, group = 0;
687 * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
688 * end up reading 14 bytes (7 words) from oob. The local array is
689 * to maintain word alignment
692 uint8_t *oob = (uint8_t *)&ecc_oob[0];
693 unsigned int max_bitflips = 0;
695 for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
696 chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
697 chip->ecc.hwctl(mtd, NAND_ECC_READ);
698 chip->read_buf(mtd, p, eccsize);
700 for (j = 0; j < eccbytes;) {
701 struct mtd_oob_region oobregion;
704 ret = mtd_ooblayout_ecc(mtd, group++, &oobregion);
708 off = oobregion.offset;
709 len = oobregion.length;
712 * length is intentionally kept a higher multiple of 2
713 * to read at least 13 bytes even in case of 16 bit NAND
716 if (chip->options & NAND_BUSWIDTH_16)
717 len = roundup(len, 2);
719 chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
720 chip->read_buf(mtd, oob + j, len);
724 memcpy(&ecc_code[i], oob, chip->ecc.bytes);
725 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
727 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
729 mtd->ecc_stats.failed++;
731 mtd->ecc_stats.corrected += stat;
732 max_bitflips = max_t(unsigned int, max_bitflips, stat);
740 * fsmc_bch8_correct_data
741 * @mtd: mtd info structure
742 * @dat: buffer of read data
743 * @read_ecc: ecc read from device spare area
744 * @calc_ecc: ecc calculated from read data
746 * calc_ecc is a 104 bit information containing maximum of 8 error
747 * offset informations of 13 bits each in 512 bytes of read data.
749 static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
750 uint8_t *read_ecc, uint8_t *calc_ecc)
752 struct nand_chip *chip = mtd_to_nand(mtd);
753 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
754 void __iomem *regs = host->regs_va;
755 unsigned int bank = host->bank;
758 uint32_t ecc1, ecc2, ecc3, ecc4;
760 num_err = (readl_relaxed(FSMC_NAND_REG(regs, bank, STS)) >> 10) & 0xF;
762 /* no bit flipping */
763 if (likely(num_err == 0))
766 /* too many errors */
767 if (unlikely(num_err > 8)) {
769 * This is a temporary erase check. A newly erased page read
770 * would result in an ecc error because the oob data is also
771 * erased to FF and the calculated ecc for an FF data is not
773 * This is a workaround to skip performing correction in case
777 * For every page, each bit written as 0 is counted until these
778 * number of bits are greater than 8 (the maximum correction
779 * capability of FSMC for each 512 + 13 bytes)
782 int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
783 int bits_data = count_written_bits(dat, chip->ecc.size, 8);
785 if ((bits_ecc + bits_data) <= 8) {
787 memset(dat, 0xff, chip->ecc.size);
795 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
796 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
798 * calc_ecc is a 104 bit information containing maximum of 8 error
799 * offset informations of 13 bits each. calc_ecc is copied into a
800 * uint64_t array and error offset indexes are populated in err_idx
803 ecc1 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
804 ecc2 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC2));
805 ecc3 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC3));
806 ecc4 = readl_relaxed(FSMC_NAND_REG(regs, bank, STS));
808 err_idx[0] = (ecc1 >> 0) & 0x1FFF;
809 err_idx[1] = (ecc1 >> 13) & 0x1FFF;
810 err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
811 err_idx[3] = (ecc2 >> 7) & 0x1FFF;
812 err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
813 err_idx[5] = (ecc3 >> 1) & 0x1FFF;
814 err_idx[6] = (ecc3 >> 14) & 0x1FFF;
815 err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
819 change_bit(0, (unsigned long *)&err_idx[i]);
820 change_bit(1, (unsigned long *)&err_idx[i]);
822 if (err_idx[i] < chip->ecc.size * 8) {
823 change_bit(err_idx[i], (unsigned long *)dat);
830 static bool filter(struct dma_chan *chan, void *slave)
832 chan->private = slave;
836 static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
837 struct device_node *np)
839 struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
845 if (!of_property_read_u32(np, "bank-width", &val)) {
847 pdata->options |= NAND_BUSWIDTH_16;
848 } else if (val != 1) {
849 dev_err(&pdev->dev, "invalid bank-width %u\n", val);
854 if (of_get_property(np, "nand-skip-bbtscan", NULL))
855 pdata->options |= NAND_SKIP_BBTSCAN;
857 pdata->nand_timings = devm_kzalloc(&pdev->dev,
858 sizeof(*pdata->nand_timings), GFP_KERNEL);
859 if (!pdata->nand_timings)
861 ret = of_property_read_u8_array(np, "timings", (u8 *)pdata->nand_timings,
862 sizeof(*pdata->nand_timings));
864 dev_info(&pdev->dev, "No timings in dts specified, using default timings!\n");
865 pdata->nand_timings = NULL;
868 /* Set default NAND bank to 0 */
870 if (!of_property_read_u32(np, "bank", &val)) {
872 dev_err(&pdev->dev, "invalid bank %u\n", val);
881 * fsmc_nand_probe - Probe function
882 * @pdev: platform device structure
884 static int __init fsmc_nand_probe(struct platform_device *pdev)
886 struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
887 struct device_node __maybe_unused *np = pdev->dev.of_node;
888 struct fsmc_nand_data *host;
889 struct mtd_info *mtd;
890 struct nand_chip *nand;
891 struct resource *res;
897 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
901 pdev->dev.platform_data = pdata;
902 ret = fsmc_nand_probe_config_dt(pdev, np);
904 dev_err(&pdev->dev, "no platform data\n");
908 /* Allocate memory for the device structure (and zero it) */
909 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
913 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
914 host->data_va = devm_ioremap_resource(&pdev->dev, res);
915 if (IS_ERR(host->data_va))
916 return PTR_ERR(host->data_va);
918 host->data_pa = (dma_addr_t)res->start;
920 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
921 host->addr_va = devm_ioremap_resource(&pdev->dev, res);
922 if (IS_ERR(host->addr_va))
923 return PTR_ERR(host->addr_va);
925 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
926 host->cmd_va = devm_ioremap_resource(&pdev->dev, res);
927 if (IS_ERR(host->cmd_va))
928 return PTR_ERR(host->cmd_va);
930 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
931 host->regs_va = devm_ioremap_resource(&pdev->dev, res);
932 if (IS_ERR(host->regs_va))
933 return PTR_ERR(host->regs_va);
935 host->clk = clk_get(&pdev->dev, NULL);
936 if (IS_ERR(host->clk)) {
937 dev_err(&pdev->dev, "failed to fetch block clock\n");
938 return PTR_ERR(host->clk);
941 ret = clk_prepare_enable(host->clk);
943 goto err_clk_prepare_enable;
946 * This device ID is actually a common AMBA ID as used on the
947 * AMBA PrimeCell bus. However it is not a PrimeCell.
949 for (pid = 0, i = 0; i < 4; i++)
950 pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
952 dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
953 "revision %02x, config %02x\n",
954 AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
955 AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
957 host->bank = pdata->bank;
958 host->partitions = pdata->partitions;
959 host->nr_partitions = pdata->nr_partitions;
960 host->dev = &pdev->dev;
961 host->dev_timings = pdata->nand_timings;
962 host->mode = pdata->mode;
964 if (host->mode == USE_DMA_ACCESS)
965 init_completion(&host->dma_access_complete);
967 /* Link all private pointers */
968 mtd = nand_to_mtd(&host->nand);
970 nand_set_controller_data(nand, host);
971 nand_set_flash_node(nand, np);
973 mtd->dev.parent = &pdev->dev;
974 nand->IO_ADDR_R = host->data_va;
975 nand->IO_ADDR_W = host->data_va;
976 nand->cmd_ctrl = fsmc_cmd_ctrl;
977 nand->chip_delay = 30;
980 * Setup default ECC mode. nand_dt_init() called from nand_scan_ident()
981 * can overwrite this value if the DT provides a different value.
983 nand->ecc.mode = NAND_ECC_HW;
984 nand->ecc.hwctl = fsmc_enable_hwecc;
985 nand->ecc.size = 512;
986 nand->options = pdata->options;
987 nand->select_chip = fsmc_select_chip;
988 nand->badblockbits = 7;
989 nand_set_flash_node(nand, np);
991 switch (host->mode) {
994 dma_cap_set(DMA_MEMCPY, mask);
995 host->read_dma_chan = dma_request_channel(mask, filter,
996 pdata->read_dma_priv);
997 if (!host->read_dma_chan) {
998 dev_err(&pdev->dev, "Unable to get read dma channel\n");
999 goto err_req_read_chnl;
1001 host->write_dma_chan = dma_request_channel(mask, filter,
1002 pdata->write_dma_priv);
1003 if (!host->write_dma_chan) {
1004 dev_err(&pdev->dev, "Unable to get write dma channel\n");
1005 goto err_req_write_chnl;
1007 nand->read_buf = fsmc_read_buf_dma;
1008 nand->write_buf = fsmc_write_buf_dma;
1012 case USE_WORD_ACCESS:
1013 nand->read_buf = fsmc_read_buf;
1014 nand->write_buf = fsmc_write_buf;
1018 fsmc_nand_setup(host->regs_va, host->bank,
1019 nand->options & NAND_BUSWIDTH_16,
1022 if (AMBA_REV_BITS(host->pid) >= 8) {
1023 nand->ecc.read_page = fsmc_read_page_hwecc;
1024 nand->ecc.calculate = fsmc_read_hwecc_ecc4;
1025 nand->ecc.correct = fsmc_bch8_correct_data;
1026 nand->ecc.bytes = 13;
1027 nand->ecc.strength = 8;
1031 * Scan to find existence of the device
1033 ret = nand_scan_ident(mtd, 1, NULL);
1035 dev_err(&pdev->dev, "No NAND Device found!\n");
1036 goto err_scan_ident;
1039 if (AMBA_REV_BITS(host->pid) >= 8) {
1040 switch (mtd->oobsize) {
1048 dev_warn(&pdev->dev, "No oob scheme defined for oobsize %d\n",
1054 mtd_set_ooblayout(mtd, &fsmc_ecc4_ooblayout_ops);
1056 switch (nand->ecc.mode) {
1058 dev_info(&pdev->dev, "Using 1-bit HW ECC scheme\n");
1059 nand->ecc.calculate = fsmc_read_hwecc_ecc1;
1060 nand->ecc.correct = nand_correct_data;
1061 nand->ecc.bytes = 3;
1062 nand->ecc.strength = 1;
1066 if (nand->ecc.algo == NAND_ECC_BCH) {
1067 dev_info(&pdev->dev, "Using 4-bit SW BCH ECC scheme\n");
1072 dev_err(&pdev->dev, "Unsupported ECC mode!\n");
1077 * Don't set layout for BCH4 SW ECC. This will be
1078 * generated later in nand_bch_init() later.
1080 if (nand->ecc.mode == NAND_ECC_HW) {
1081 switch (mtd->oobsize) {
1085 mtd_set_ooblayout(mtd,
1086 &fsmc_ecc1_ooblayout_ops);
1089 dev_warn(&pdev->dev,
1090 "No oob scheme defined for oobsize %d\n",
1098 /* Second stage of scan to fill MTD data-structures */
1099 ret = nand_scan_tail(mtd);
1104 * The partition information can is accessed by (in the same precedence)
1106 * command line through Bootloader,
1108 * default partition information present in driver.
1111 * Check for partition info passed
1114 ret = mtd_device_register(mtd, host->partitions, host->nr_partitions);
1118 platform_set_drvdata(pdev, host);
1119 dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
1124 if (host->mode == USE_DMA_ACCESS)
1125 dma_release_channel(host->write_dma_chan);
1127 if (host->mode == USE_DMA_ACCESS)
1128 dma_release_channel(host->read_dma_chan);
1130 clk_disable_unprepare(host->clk);
1131 err_clk_prepare_enable:
1139 static int fsmc_nand_remove(struct platform_device *pdev)
1141 struct fsmc_nand_data *host = platform_get_drvdata(pdev);
1144 nand_release(nand_to_mtd(&host->nand));
1146 if (host->mode == USE_DMA_ACCESS) {
1147 dma_release_channel(host->write_dma_chan);
1148 dma_release_channel(host->read_dma_chan);
1150 clk_disable_unprepare(host->clk);
1157 #ifdef CONFIG_PM_SLEEP
1158 static int fsmc_nand_suspend(struct device *dev)
1160 struct fsmc_nand_data *host = dev_get_drvdata(dev);
1162 clk_disable_unprepare(host->clk);
1166 static int fsmc_nand_resume(struct device *dev)
1168 struct fsmc_nand_data *host = dev_get_drvdata(dev);
1170 clk_prepare_enable(host->clk);
1171 fsmc_nand_setup(host->regs_va, host->bank,
1172 host->nand.options & NAND_BUSWIDTH_16,
1179 static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
1182 static const struct of_device_id fsmc_nand_id_table[] = {
1183 { .compatible = "st,spear600-fsmc-nand" },
1184 { .compatible = "stericsson,fsmc-nand" },
1187 MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);
1190 static struct platform_driver fsmc_nand_driver = {
1191 .remove = fsmc_nand_remove,
1193 .name = "fsmc-nand",
1194 .of_match_table = of_match_ptr(fsmc_nand_id_table),
1195 .pm = &fsmc_nand_pm_ops,
1199 module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe);
1201 MODULE_LICENSE("GPL");
1202 MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
1203 MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");