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/mtd/fsmc.h>
39 #include <linux/amba/bus.h>
40 #include <mtd/mtd-abi.h>
42 static int fsmc_ecc1_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
43 struct mtd_oob_region
*oobregion
)
45 struct nand_chip
*chip
= mtd_to_nand(mtd
);
47 if (section
>= chip
->ecc
.steps
)
50 oobregion
->offset
= (section
* 16) + 2;
51 oobregion
->length
= 3;
56 static int fsmc_ecc1_ooblayout_free(struct mtd_info
*mtd
, int section
,
57 struct mtd_oob_region
*oobregion
)
59 struct nand_chip
*chip
= mtd_to_nand(mtd
);
61 if (section
>= chip
->ecc
.steps
)
64 oobregion
->offset
= (section
* 16) + 8;
66 if (section
< chip
->ecc
.steps
- 1)
67 oobregion
->length
= 8;
69 oobregion
->length
= mtd
->oobsize
- oobregion
->offset
;
74 static const struct mtd_ooblayout_ops fsmc_ecc1_ooblayout_ops
= {
75 .ecc
= fsmc_ecc1_ooblayout_ecc
,
76 .free
= fsmc_ecc1_ooblayout_free
,
80 * ECC placement definitions in oobfree type format.
81 * There are 13 bytes of ecc for every 512 byte block and it has to be read
82 * consecutively and immediately after the 512 byte data block for hardware to
83 * generate the error bit offsets in 512 byte data.
85 static int fsmc_ecc4_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
86 struct mtd_oob_region
*oobregion
)
88 struct nand_chip
*chip
= mtd_to_nand(mtd
);
90 if (section
>= chip
->ecc
.steps
)
93 oobregion
->length
= chip
->ecc
.bytes
;
95 if (!section
&& mtd
->writesize
<= 512)
96 oobregion
->offset
= 0;
98 oobregion
->offset
= (section
* 16) + 2;
103 static int fsmc_ecc4_ooblayout_free(struct mtd_info
*mtd
, int section
,
104 struct mtd_oob_region
*oobregion
)
106 struct nand_chip
*chip
= mtd_to_nand(mtd
);
108 if (section
>= chip
->ecc
.steps
)
111 oobregion
->offset
= (section
* 16) + 15;
113 if (section
< chip
->ecc
.steps
- 1)
114 oobregion
->length
= 3;
116 oobregion
->length
= mtd
->oobsize
- oobregion
->offset
;
121 static const struct mtd_ooblayout_ops fsmc_ecc4_ooblayout_ops
= {
122 .ecc
= fsmc_ecc4_ooblayout_ecc
,
123 .free
= fsmc_ecc4_ooblayout_free
,
127 * struct fsmc_nand_data - structure for FSMC NAND device state
129 * @pid: Part ID on the AMBA PrimeCell format
130 * @mtd: MTD info for a NAND flash.
131 * @nand: Chip related info for a NAND flash.
132 * @partitions: Partition info for a NAND Flash.
133 * @nr_partitions: Total number of partition of a NAND flash.
135 * @bank: Bank number for probed device.
136 * @clk: Clock structure for FSMC.
138 * @read_dma_chan: DMA channel for read access
139 * @write_dma_chan: DMA channel for write access to NAND
140 * @dma_access_complete: Completion structure
142 * @data_pa: NAND Physical port for Data.
143 * @data_va: NAND port for Data.
144 * @cmd_va: NAND port for Command.
145 * @addr_va: NAND port for Address.
146 * @regs_va: FSMC regs base address.
148 struct fsmc_nand_data
{
150 struct nand_chip nand
;
151 struct mtd_partition
*partitions
;
152 unsigned int nr_partitions
;
156 enum access_mode mode
;
159 /* DMA related objects */
160 struct dma_chan
*read_dma_chan
;
161 struct dma_chan
*write_dma_chan
;
162 struct completion dma_access_complete
;
164 struct fsmc_nand_timings
*dev_timings
;
167 void __iomem
*data_va
;
168 void __iomem
*cmd_va
;
169 void __iomem
*addr_va
;
170 void __iomem
*regs_va
;
172 void (*select_chip
)(uint32_t bank
, uint32_t busw
);
175 static inline struct fsmc_nand_data
*mtd_to_fsmc(struct mtd_info
*mtd
)
177 return container_of(mtd_to_nand(mtd
), struct fsmc_nand_data
, nand
);
180 /* Assert CS signal based on chipnr */
181 static void fsmc_select_chip(struct mtd_info
*mtd
, int chipnr
)
183 struct nand_chip
*chip
= mtd_to_nand(mtd
);
184 struct fsmc_nand_data
*host
;
186 host
= mtd_to_fsmc(mtd
);
190 chip
->cmd_ctrl(mtd
, NAND_CMD_NONE
, 0 | NAND_CTRL_CHANGE
);
196 if (host
->select_chip
)
197 host
->select_chip(chipnr
,
198 chip
->options
& NAND_BUSWIDTH_16
);
202 dev_err(host
->dev
, "unsupported chip-select %d\n", chipnr
);
207 * fsmc_cmd_ctrl - For facilitaing Hardware access
208 * This routine allows hardware specific access to control-lines(ALE,CLE)
210 static void fsmc_cmd_ctrl(struct mtd_info
*mtd
, int cmd
, unsigned int ctrl
)
212 struct nand_chip
*this = mtd_to_nand(mtd
);
213 struct fsmc_nand_data
*host
= mtd_to_fsmc(mtd
);
214 void __iomem
*regs
= host
->regs_va
;
215 unsigned int bank
= host
->bank
;
217 if (ctrl
& NAND_CTRL_CHANGE
) {
220 if (ctrl
& NAND_CLE
) {
221 this->IO_ADDR_R
= host
->cmd_va
;
222 this->IO_ADDR_W
= host
->cmd_va
;
223 } else if (ctrl
& NAND_ALE
) {
224 this->IO_ADDR_R
= host
->addr_va
;
225 this->IO_ADDR_W
= host
->addr_va
;
227 this->IO_ADDR_R
= host
->data_va
;
228 this->IO_ADDR_W
= host
->data_va
;
231 pc
= readl(FSMC_NAND_REG(regs
, bank
, PC
));
236 writel_relaxed(pc
, FSMC_NAND_REG(regs
, bank
, PC
));
241 if (cmd
!= NAND_CMD_NONE
)
242 writeb_relaxed(cmd
, this->IO_ADDR_W
);
246 * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
248 * This routine initializes timing parameters related to NAND memory access in
251 static void fsmc_nand_setup(void __iomem
*regs
, uint32_t bank
,
252 uint32_t busw
, struct fsmc_nand_timings
*timings
)
254 uint32_t value
= FSMC_DEVTYPE_NAND
| FSMC_ENABLE
| FSMC_WAITON
;
255 uint32_t tclr
, tar
, thiz
, thold
, twait
, tset
;
256 struct fsmc_nand_timings
*tims
;
257 struct fsmc_nand_timings default_timings
= {
261 .thold
= FSMC_THOLD_4
,
262 .twait
= FSMC_TWAIT_6
,
269 tims
= &default_timings
;
271 tclr
= (tims
->tclr
& FSMC_TCLR_MASK
) << FSMC_TCLR_SHIFT
;
272 tar
= (tims
->tar
& FSMC_TAR_MASK
) << FSMC_TAR_SHIFT
;
273 thiz
= (tims
->thiz
& FSMC_THIZ_MASK
) << FSMC_THIZ_SHIFT
;
274 thold
= (tims
->thold
& FSMC_THOLD_MASK
) << FSMC_THOLD_SHIFT
;
275 twait
= (tims
->twait
& FSMC_TWAIT_MASK
) << FSMC_TWAIT_SHIFT
;
276 tset
= (tims
->tset
& FSMC_TSET_MASK
) << FSMC_TSET_SHIFT
;
279 writel_relaxed(value
| FSMC_DEVWID_16
,
280 FSMC_NAND_REG(regs
, bank
, PC
));
282 writel_relaxed(value
| FSMC_DEVWID_8
,
283 FSMC_NAND_REG(regs
, bank
, PC
));
285 writel_relaxed(readl(FSMC_NAND_REG(regs
, bank
, PC
)) | tclr
| tar
,
286 FSMC_NAND_REG(regs
, bank
, PC
));
287 writel_relaxed(thiz
| thold
| twait
| tset
,
288 FSMC_NAND_REG(regs
, bank
, COMM
));
289 writel_relaxed(thiz
| thold
| twait
| tset
,
290 FSMC_NAND_REG(regs
, bank
, ATTRIB
));
294 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
296 static void fsmc_enable_hwecc(struct mtd_info
*mtd
, int mode
)
298 struct fsmc_nand_data
*host
= mtd_to_fsmc(mtd
);
299 void __iomem
*regs
= host
->regs_va
;
300 uint32_t bank
= host
->bank
;
302 writel_relaxed(readl(FSMC_NAND_REG(regs
, bank
, PC
)) & ~FSMC_ECCPLEN_256
,
303 FSMC_NAND_REG(regs
, bank
, PC
));
304 writel_relaxed(readl(FSMC_NAND_REG(regs
, bank
, PC
)) & ~FSMC_ECCEN
,
305 FSMC_NAND_REG(regs
, bank
, PC
));
306 writel_relaxed(readl(FSMC_NAND_REG(regs
, bank
, PC
)) | FSMC_ECCEN
,
307 FSMC_NAND_REG(regs
, bank
, PC
));
311 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
312 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
315 static int fsmc_read_hwecc_ecc4(struct mtd_info
*mtd
, const uint8_t *data
,
318 struct fsmc_nand_data
*host
= mtd_to_fsmc(mtd
);
319 void __iomem
*regs
= host
->regs_va
;
320 uint32_t bank
= host
->bank
;
322 unsigned long deadline
= jiffies
+ FSMC_BUSY_WAIT_TIMEOUT
;
325 if (readl_relaxed(FSMC_NAND_REG(regs
, bank
, STS
)) & FSMC_CODE_RDY
)
329 } while (!time_after_eq(jiffies
, deadline
));
331 if (time_after_eq(jiffies
, deadline
)) {
332 dev_err(host
->dev
, "calculate ecc timed out\n");
336 ecc_tmp
= readl_relaxed(FSMC_NAND_REG(regs
, bank
, ECC1
));
337 ecc
[0] = (uint8_t) (ecc_tmp
>> 0);
338 ecc
[1] = (uint8_t) (ecc_tmp
>> 8);
339 ecc
[2] = (uint8_t) (ecc_tmp
>> 16);
340 ecc
[3] = (uint8_t) (ecc_tmp
>> 24);
342 ecc_tmp
= readl_relaxed(FSMC_NAND_REG(regs
, bank
, ECC2
));
343 ecc
[4] = (uint8_t) (ecc_tmp
>> 0);
344 ecc
[5] = (uint8_t) (ecc_tmp
>> 8);
345 ecc
[6] = (uint8_t) (ecc_tmp
>> 16);
346 ecc
[7] = (uint8_t) (ecc_tmp
>> 24);
348 ecc_tmp
= readl_relaxed(FSMC_NAND_REG(regs
, bank
, ECC3
));
349 ecc
[8] = (uint8_t) (ecc_tmp
>> 0);
350 ecc
[9] = (uint8_t) (ecc_tmp
>> 8);
351 ecc
[10] = (uint8_t) (ecc_tmp
>> 16);
352 ecc
[11] = (uint8_t) (ecc_tmp
>> 24);
354 ecc_tmp
= readl_relaxed(FSMC_NAND_REG(regs
, bank
, STS
));
355 ecc
[12] = (uint8_t) (ecc_tmp
>> 16);
361 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
362 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
365 static int fsmc_read_hwecc_ecc1(struct mtd_info
*mtd
, const uint8_t *data
,
368 struct fsmc_nand_data
*host
= mtd_to_fsmc(mtd
);
369 void __iomem
*regs
= host
->regs_va
;
370 uint32_t bank
= host
->bank
;
373 ecc_tmp
= readl_relaxed(FSMC_NAND_REG(regs
, bank
, ECC1
));
374 ecc
[0] = (uint8_t) (ecc_tmp
>> 0);
375 ecc
[1] = (uint8_t) (ecc_tmp
>> 8);
376 ecc
[2] = (uint8_t) (ecc_tmp
>> 16);
381 /* Count the number of 0's in buff upto a max of max_bits */
382 static int count_written_bits(uint8_t *buff
, int size
, int max_bits
)
384 int k
, written_bits
= 0;
386 for (k
= 0; k
< size
; k
++) {
387 written_bits
+= hweight8(~buff
[k
]);
388 if (written_bits
> max_bits
)
395 static void dma_complete(void *param
)
397 struct fsmc_nand_data
*host
= param
;
399 complete(&host
->dma_access_complete
);
402 static int dma_xfer(struct fsmc_nand_data
*host
, void *buffer
, int len
,
403 enum dma_data_direction direction
)
405 struct dma_chan
*chan
;
406 struct dma_device
*dma_dev
;
407 struct dma_async_tx_descriptor
*tx
;
408 dma_addr_t dma_dst
, dma_src
, dma_addr
;
410 unsigned long flags
= DMA_CTRL_ACK
| DMA_PREP_INTERRUPT
;
412 unsigned long time_left
;
414 if (direction
== DMA_TO_DEVICE
)
415 chan
= host
->write_dma_chan
;
416 else if (direction
== DMA_FROM_DEVICE
)
417 chan
= host
->read_dma_chan
;
421 dma_dev
= chan
->device
;
422 dma_addr
= dma_map_single(dma_dev
->dev
, buffer
, len
, direction
);
424 if (direction
== DMA_TO_DEVICE
) {
426 dma_dst
= host
->data_pa
;
428 dma_src
= host
->data_pa
;
432 tx
= dma_dev
->device_prep_dma_memcpy(chan
, dma_dst
, dma_src
,
435 dev_err(host
->dev
, "device_prep_dma_memcpy error\n");
440 tx
->callback
= dma_complete
;
441 tx
->callback_param
= host
;
442 cookie
= tx
->tx_submit(tx
);
444 ret
= dma_submit_error(cookie
);
446 dev_err(host
->dev
, "dma_submit_error %d\n", cookie
);
450 dma_async_issue_pending(chan
);
453 wait_for_completion_timeout(&host
->dma_access_complete
,
454 msecs_to_jiffies(3000));
455 if (time_left
== 0) {
456 dmaengine_terminate_all(chan
);
457 dev_err(host
->dev
, "wait_for_completion_timeout\n");
465 dma_unmap_single(dma_dev
->dev
, dma_addr
, len
, direction
);
471 * fsmc_write_buf - write buffer to chip
472 * @mtd: MTD device structure
474 * @len: number of bytes to write
476 static void fsmc_write_buf(struct mtd_info
*mtd
, const uint8_t *buf
, int len
)
479 struct nand_chip
*chip
= mtd_to_nand(mtd
);
481 if (IS_ALIGNED((uint32_t)buf
, sizeof(uint32_t)) &&
482 IS_ALIGNED(len
, sizeof(uint32_t))) {
483 uint32_t *p
= (uint32_t *)buf
;
485 for (i
= 0; i
< len
; i
++)
486 writel_relaxed(p
[i
], chip
->IO_ADDR_W
);
488 for (i
= 0; i
< len
; i
++)
489 writeb_relaxed(buf
[i
], chip
->IO_ADDR_W
);
494 * fsmc_read_buf - read chip data into buffer
495 * @mtd: MTD device structure
496 * @buf: buffer to store date
497 * @len: number of bytes to read
499 static void fsmc_read_buf(struct mtd_info
*mtd
, uint8_t *buf
, int len
)
502 struct nand_chip
*chip
= mtd_to_nand(mtd
);
504 if (IS_ALIGNED((uint32_t)buf
, sizeof(uint32_t)) &&
505 IS_ALIGNED(len
, sizeof(uint32_t))) {
506 uint32_t *p
= (uint32_t *)buf
;
508 for (i
= 0; i
< len
; i
++)
509 p
[i
] = readl_relaxed(chip
->IO_ADDR_R
);
511 for (i
= 0; i
< len
; i
++)
512 buf
[i
] = readb_relaxed(chip
->IO_ADDR_R
);
517 * fsmc_read_buf_dma - read chip data into buffer
518 * @mtd: MTD device structure
519 * @buf: buffer to store date
520 * @len: number of bytes to read
522 static void fsmc_read_buf_dma(struct mtd_info
*mtd
, uint8_t *buf
, int len
)
524 struct fsmc_nand_data
*host
= mtd_to_fsmc(mtd
);
526 dma_xfer(host
, buf
, len
, DMA_FROM_DEVICE
);
530 * fsmc_write_buf_dma - write buffer to chip
531 * @mtd: MTD device structure
533 * @len: number of bytes to write
535 static void fsmc_write_buf_dma(struct mtd_info
*mtd
, const uint8_t *buf
,
538 struct fsmc_nand_data
*host
= mtd_to_fsmc(mtd
);
540 dma_xfer(host
, (void *)buf
, len
, DMA_TO_DEVICE
);
544 * fsmc_read_page_hwecc
545 * @mtd: mtd info structure
546 * @chip: nand chip info structure
547 * @buf: buffer to store read data
548 * @oob_required: caller expects OOB data read to chip->oob_poi
549 * @page: page number to read
551 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
552 * performed in a strict sequence as follows:
553 * data(512 byte) -> ecc(13 byte)
554 * After this read, fsmc hardware generates and reports error data bits(up to a
557 static int fsmc_read_page_hwecc(struct mtd_info
*mtd
, struct nand_chip
*chip
,
558 uint8_t *buf
, int oob_required
, int page
)
560 int i
, j
, s
, stat
, eccsize
= chip
->ecc
.size
;
561 int eccbytes
= chip
->ecc
.bytes
;
562 int eccsteps
= chip
->ecc
.steps
;
564 uint8_t *ecc_calc
= chip
->buffers
->ecccalc
;
565 uint8_t *ecc_code
= chip
->buffers
->ecccode
;
566 int off
, len
, group
= 0;
568 * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
569 * end up reading 14 bytes (7 words) from oob. The local array is
570 * to maintain word alignment
573 uint8_t *oob
= (uint8_t *)&ecc_oob
[0];
574 unsigned int max_bitflips
= 0;
576 for (i
= 0, s
= 0; s
< eccsteps
; s
++, i
+= eccbytes
, p
+= eccsize
) {
577 chip
->cmdfunc(mtd
, NAND_CMD_READ0
, s
* eccsize
, page
);
578 chip
->ecc
.hwctl(mtd
, NAND_ECC_READ
);
579 chip
->read_buf(mtd
, p
, eccsize
);
581 for (j
= 0; j
< eccbytes
;) {
582 struct mtd_oob_region oobregion
;
585 ret
= mtd_ooblayout_ecc(mtd
, group
++, &oobregion
);
589 off
= oobregion
.offset
;
590 len
= oobregion
.length
;
593 * length is intentionally kept a higher multiple of 2
594 * to read at least 13 bytes even in case of 16 bit NAND
597 if (chip
->options
& NAND_BUSWIDTH_16
)
598 len
= roundup(len
, 2);
600 chip
->cmdfunc(mtd
, NAND_CMD_READOOB
, off
, page
);
601 chip
->read_buf(mtd
, oob
+ j
, len
);
605 memcpy(&ecc_code
[i
], oob
, chip
->ecc
.bytes
);
606 chip
->ecc
.calculate(mtd
, p
, &ecc_calc
[i
]);
608 stat
= chip
->ecc
.correct(mtd
, p
, &ecc_code
[i
], &ecc_calc
[i
]);
610 mtd
->ecc_stats
.failed
++;
612 mtd
->ecc_stats
.corrected
+= stat
;
613 max_bitflips
= max_t(unsigned int, max_bitflips
, stat
);
621 * fsmc_bch8_correct_data
622 * @mtd: mtd info structure
623 * @dat: buffer of read data
624 * @read_ecc: ecc read from device spare area
625 * @calc_ecc: ecc calculated from read data
627 * calc_ecc is a 104 bit information containing maximum of 8 error
628 * offset informations of 13 bits each in 512 bytes of read data.
630 static int fsmc_bch8_correct_data(struct mtd_info
*mtd
, uint8_t *dat
,
631 uint8_t *read_ecc
, uint8_t *calc_ecc
)
633 struct nand_chip
*chip
= mtd_to_nand(mtd
);
634 struct fsmc_nand_data
*host
= mtd_to_fsmc(mtd
);
635 void __iomem
*regs
= host
->regs_va
;
636 unsigned int bank
= host
->bank
;
639 uint32_t ecc1
, ecc2
, ecc3
, ecc4
;
641 num_err
= (readl_relaxed(FSMC_NAND_REG(regs
, bank
, STS
)) >> 10) & 0xF;
643 /* no bit flipping */
644 if (likely(num_err
== 0))
647 /* too many errors */
648 if (unlikely(num_err
> 8)) {
650 * This is a temporary erase check. A newly erased page read
651 * would result in an ecc error because the oob data is also
652 * erased to FF and the calculated ecc for an FF data is not
654 * This is a workaround to skip performing correction in case
658 * For every page, each bit written as 0 is counted until these
659 * number of bits are greater than 8 (the maximum correction
660 * capability of FSMC for each 512 + 13 bytes)
663 int bits_ecc
= count_written_bits(read_ecc
, chip
->ecc
.bytes
, 8);
664 int bits_data
= count_written_bits(dat
, chip
->ecc
.size
, 8);
666 if ((bits_ecc
+ bits_data
) <= 8) {
668 memset(dat
, 0xff, chip
->ecc
.size
);
676 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
677 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
679 * calc_ecc is a 104 bit information containing maximum of 8 error
680 * offset informations of 13 bits each. calc_ecc is copied into a
681 * uint64_t array and error offset indexes are populated in err_idx
684 ecc1
= readl_relaxed(FSMC_NAND_REG(regs
, bank
, ECC1
));
685 ecc2
= readl_relaxed(FSMC_NAND_REG(regs
, bank
, ECC2
));
686 ecc3
= readl_relaxed(FSMC_NAND_REG(regs
, bank
, ECC3
));
687 ecc4
= readl_relaxed(FSMC_NAND_REG(regs
, bank
, STS
));
689 err_idx
[0] = (ecc1
>> 0) & 0x1FFF;
690 err_idx
[1] = (ecc1
>> 13) & 0x1FFF;
691 err_idx
[2] = (((ecc2
>> 0) & 0x7F) << 6) | ((ecc1
>> 26) & 0x3F);
692 err_idx
[3] = (ecc2
>> 7) & 0x1FFF;
693 err_idx
[4] = (((ecc3
>> 0) & 0x1) << 12) | ((ecc2
>> 20) & 0xFFF);
694 err_idx
[5] = (ecc3
>> 1) & 0x1FFF;
695 err_idx
[6] = (ecc3
>> 14) & 0x1FFF;
696 err_idx
[7] = (((ecc4
>> 16) & 0xFF) << 5) | ((ecc3
>> 27) & 0x1F);
700 change_bit(0, (unsigned long *)&err_idx
[i
]);
701 change_bit(1, (unsigned long *)&err_idx
[i
]);
703 if (err_idx
[i
] < chip
->ecc
.size
* 8) {
704 change_bit(err_idx
[i
], (unsigned long *)dat
);
711 static bool filter(struct dma_chan
*chan
, void *slave
)
713 chan
->private = slave
;
718 static int fsmc_nand_probe_config_dt(struct platform_device
*pdev
,
719 struct device_node
*np
)
721 struct fsmc_nand_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
725 /* Set default NAND width to 8 bits */
727 if (!of_property_read_u32(np
, "bank-width", &val
)) {
730 } else if (val
!= 1) {
731 dev_err(&pdev
->dev
, "invalid bank-width %u\n", val
);
735 if (of_get_property(np
, "nand-skip-bbtscan", NULL
))
736 pdata
->options
= NAND_SKIP_BBTSCAN
;
738 pdata
->nand_timings
= devm_kzalloc(&pdev
->dev
,
739 sizeof(*pdata
->nand_timings
), GFP_KERNEL
);
740 if (!pdata
->nand_timings
)
742 ret
= of_property_read_u8_array(np
, "timings", (u8
*)pdata
->nand_timings
,
743 sizeof(*pdata
->nand_timings
));
745 dev_info(&pdev
->dev
, "No timings in dts specified, using default timings!\n");
746 pdata
->nand_timings
= NULL
;
749 /* Set default NAND bank to 0 */
751 if (!of_property_read_u32(np
, "bank", &val
)) {
753 dev_err(&pdev
->dev
, "invalid bank %u\n", val
);
761 static int fsmc_nand_probe_config_dt(struct platform_device
*pdev
,
762 struct device_node
*np
)
769 * fsmc_nand_probe - Probe function
770 * @pdev: platform device structure
772 static int __init
fsmc_nand_probe(struct platform_device
*pdev
)
774 struct fsmc_nand_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
775 struct device_node __maybe_unused
*np
= pdev
->dev
.of_node
;
776 struct fsmc_nand_data
*host
;
777 struct mtd_info
*mtd
;
778 struct nand_chip
*nand
;
779 struct resource
*res
;
786 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
787 pdev
->dev
.platform_data
= pdata
;
788 ret
= fsmc_nand_probe_config_dt(pdev
, np
);
790 dev_err(&pdev
->dev
, "no platform data\n");
796 dev_err(&pdev
->dev
, "platform data is NULL\n");
800 /* Allocate memory for the device structure (and zero it) */
801 host
= devm_kzalloc(&pdev
->dev
, sizeof(*host
), GFP_KERNEL
);
805 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "nand_data");
806 host
->data_va
= devm_ioremap_resource(&pdev
->dev
, res
);
807 if (IS_ERR(host
->data_va
))
808 return PTR_ERR(host
->data_va
);
810 host
->data_pa
= (dma_addr_t
)res
->start
;
812 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "nand_addr");
813 host
->addr_va
= devm_ioremap_resource(&pdev
->dev
, res
);
814 if (IS_ERR(host
->addr_va
))
815 return PTR_ERR(host
->addr_va
);
817 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "nand_cmd");
818 host
->cmd_va
= devm_ioremap_resource(&pdev
->dev
, res
);
819 if (IS_ERR(host
->cmd_va
))
820 return PTR_ERR(host
->cmd_va
);
822 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "fsmc_regs");
823 host
->regs_va
= devm_ioremap_resource(&pdev
->dev
, res
);
824 if (IS_ERR(host
->regs_va
))
825 return PTR_ERR(host
->regs_va
);
827 host
->clk
= clk_get(&pdev
->dev
, NULL
);
828 if (IS_ERR(host
->clk
)) {
829 dev_err(&pdev
->dev
, "failed to fetch block clock\n");
830 return PTR_ERR(host
->clk
);
833 ret
= clk_prepare_enable(host
->clk
);
835 goto err_clk_prepare_enable
;
838 * This device ID is actually a common AMBA ID as used on the
839 * AMBA PrimeCell bus. However it is not a PrimeCell.
841 for (pid
= 0, i
= 0; i
< 4; i
++)
842 pid
|= (readl(host
->regs_va
+ resource_size(res
) - 0x20 + 4 * i
) & 255) << (i
* 8);
844 dev_info(&pdev
->dev
, "FSMC device partno %03x, manufacturer %02x, "
845 "revision %02x, config %02x\n",
846 AMBA_PART_BITS(pid
), AMBA_MANF_BITS(pid
),
847 AMBA_REV_BITS(pid
), AMBA_CONFIG_BITS(pid
));
849 host
->bank
= pdata
->bank
;
850 host
->select_chip
= pdata
->select_bank
;
851 host
->partitions
= pdata
->partitions
;
852 host
->nr_partitions
= pdata
->nr_partitions
;
853 host
->dev
= &pdev
->dev
;
854 host
->dev_timings
= pdata
->nand_timings
;
855 host
->mode
= pdata
->mode
;
857 if (host
->mode
== USE_DMA_ACCESS
)
858 init_completion(&host
->dma_access_complete
);
860 /* Link all private pointers */
861 mtd
= nand_to_mtd(&host
->nand
);
863 nand_set_controller_data(nand
, host
);
864 nand_set_flash_node(nand
, np
);
866 mtd
->dev
.parent
= &pdev
->dev
;
867 nand
->IO_ADDR_R
= host
->data_va
;
868 nand
->IO_ADDR_W
= host
->data_va
;
869 nand
->cmd_ctrl
= fsmc_cmd_ctrl
;
870 nand
->chip_delay
= 30;
873 * Setup default ECC mode. nand_dt_init() called from nand_scan_ident()
874 * can overwrite this value if the DT provides a different value.
876 nand
->ecc
.mode
= NAND_ECC_HW
;
877 nand
->ecc
.hwctl
= fsmc_enable_hwecc
;
878 nand
->ecc
.size
= 512;
879 nand
->options
= pdata
->options
;
880 nand
->select_chip
= fsmc_select_chip
;
881 nand
->badblockbits
= 7;
882 nand_set_flash_node(nand
, np
);
884 if (pdata
->width
== FSMC_NAND_BW16
)
885 nand
->options
|= NAND_BUSWIDTH_16
;
887 switch (host
->mode
) {
890 dma_cap_set(DMA_MEMCPY
, mask
);
891 host
->read_dma_chan
= dma_request_channel(mask
, filter
,
892 pdata
->read_dma_priv
);
893 if (!host
->read_dma_chan
) {
894 dev_err(&pdev
->dev
, "Unable to get read dma channel\n");
895 goto err_req_read_chnl
;
897 host
->write_dma_chan
= dma_request_channel(mask
, filter
,
898 pdata
->write_dma_priv
);
899 if (!host
->write_dma_chan
) {
900 dev_err(&pdev
->dev
, "Unable to get write dma channel\n");
901 goto err_req_write_chnl
;
903 nand
->read_buf
= fsmc_read_buf_dma
;
904 nand
->write_buf
= fsmc_write_buf_dma
;
908 case USE_WORD_ACCESS
:
909 nand
->read_buf
= fsmc_read_buf
;
910 nand
->write_buf
= fsmc_write_buf
;
914 fsmc_nand_setup(host
->regs_va
, host
->bank
,
915 nand
->options
& NAND_BUSWIDTH_16
,
918 if (AMBA_REV_BITS(host
->pid
) >= 8) {
919 nand
->ecc
.read_page
= fsmc_read_page_hwecc
;
920 nand
->ecc
.calculate
= fsmc_read_hwecc_ecc4
;
921 nand
->ecc
.correct
= fsmc_bch8_correct_data
;
922 nand
->ecc
.bytes
= 13;
923 nand
->ecc
.strength
= 8;
927 * Scan to find existence of the device
929 if (nand_scan_ident(mtd
, 1, NULL
)) {
931 dev_err(&pdev
->dev
, "No NAND Device found!\n");
935 if (AMBA_REV_BITS(host
->pid
) >= 8) {
936 switch (mtd
->oobsize
) {
944 dev_warn(&pdev
->dev
, "No oob scheme defined for oobsize %d\n",
950 mtd_set_ooblayout(mtd
, &fsmc_ecc4_ooblayout_ops
);
952 switch (nand
->ecc
.mode
) {
954 dev_info(&pdev
->dev
, "Using 1-bit HW ECC scheme\n");
955 nand
->ecc
.calculate
= fsmc_read_hwecc_ecc1
;
956 nand
->ecc
.correct
= nand_correct_data
;
958 nand
->ecc
.strength
= 1;
962 if (nand
->ecc
.algo
== NAND_ECC_BCH
) {
963 dev_info(&pdev
->dev
, "Using 4-bit SW BCH ECC scheme\n");
968 dev_err(&pdev
->dev
, "Unsupported ECC mode!\n");
973 * Don't set layout for BCH4 SW ECC. This will be
974 * generated later in nand_bch_init() later.
976 if (nand
->ecc
.mode
== NAND_ECC_HW
) {
977 switch (mtd
->oobsize
) {
981 mtd_set_ooblayout(mtd
,
982 &fsmc_ecc1_ooblayout_ops
);
986 "No oob scheme defined for oobsize %d\n",
994 /* Second stage of scan to fill MTD data-structures */
995 if (nand_scan_tail(mtd
)) {
1001 * The partition information can is accessed by (in the same precedence)
1003 * command line through Bootloader,
1005 * default partition information present in driver.
1008 * Check for partition info passed
1011 ret
= mtd_device_register(mtd
, host
->partitions
, host
->nr_partitions
);
1015 platform_set_drvdata(pdev
, host
);
1016 dev_info(&pdev
->dev
, "FSMC NAND driver registration successful\n");
1021 if (host
->mode
== USE_DMA_ACCESS
)
1022 dma_release_channel(host
->write_dma_chan
);
1024 if (host
->mode
== USE_DMA_ACCESS
)
1025 dma_release_channel(host
->read_dma_chan
);
1027 clk_disable_unprepare(host
->clk
);
1028 err_clk_prepare_enable
:
1036 static int fsmc_nand_remove(struct platform_device
*pdev
)
1038 struct fsmc_nand_data
*host
= platform_get_drvdata(pdev
);
1041 nand_release(nand_to_mtd(&host
->nand
));
1043 if (host
->mode
== USE_DMA_ACCESS
) {
1044 dma_release_channel(host
->write_dma_chan
);
1045 dma_release_channel(host
->read_dma_chan
);
1047 clk_disable_unprepare(host
->clk
);
1054 #ifdef CONFIG_PM_SLEEP
1055 static int fsmc_nand_suspend(struct device
*dev
)
1057 struct fsmc_nand_data
*host
= dev_get_drvdata(dev
);
1059 clk_disable_unprepare(host
->clk
);
1063 static int fsmc_nand_resume(struct device
*dev
)
1065 struct fsmc_nand_data
*host
= dev_get_drvdata(dev
);
1067 clk_prepare_enable(host
->clk
);
1068 fsmc_nand_setup(host
->regs_va
, host
->bank
,
1069 host
->nand
.options
& NAND_BUSWIDTH_16
,
1076 static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops
, fsmc_nand_suspend
, fsmc_nand_resume
);
1079 static const struct of_device_id fsmc_nand_id_table
[] = {
1080 { .compatible
= "st,spear600-fsmc-nand" },
1081 { .compatible
= "stericsson,fsmc-nand" },
1084 MODULE_DEVICE_TABLE(of
, fsmc_nand_id_table
);
1087 static struct platform_driver fsmc_nand_driver
= {
1088 .remove
= fsmc_nand_remove
,
1090 .name
= "fsmc-nand",
1091 .of_match_table
= of_match_ptr(fsmc_nand_id_table
),
1092 .pm
= &fsmc_nand_pm_ops
,
1096 module_platform_driver_probe(fsmc_nand_driver
, fsmc_nand_probe
);
1098 MODULE_LICENSE("GPL");
1099 MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
1100 MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");