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/rawnand.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 /* fsmc controller registers for NOR flash */
43 /* ctrl register definitions */
44 #define BANK_ENABLE (1 << 0)
45 #define MUXED (1 << 1)
46 #define NOR_DEV (2 << 2)
47 #define WIDTH_8 (0 << 4)
48 #define WIDTH_16 (1 << 4)
49 #define RSTPWRDWN (1 << 6)
50 #define WPROT (1 << 7)
51 #define WRT_ENABLE (1 << 12)
52 #define WAIT_ENB (1 << 13)
55 /* ctrl_tim register definitions */
57 #define FSMC_NOR_BANK_SZ 0x8
58 #define FSMC_NOR_REG_SIZE 0x40
60 #define FSMC_NOR_REG(base, bank, reg) (base + \
61 FSMC_NOR_BANK_SZ * (bank) + \
64 /* fsmc controller registers for NAND flash */
66 /* pc register definitions */
67 #define FSMC_RESET (1 << 0)
68 #define FSMC_WAITON (1 << 1)
69 #define FSMC_ENABLE (1 << 2)
70 #define FSMC_DEVTYPE_NAND (1 << 3)
71 #define FSMC_DEVWID_8 (0 << 4)
72 #define FSMC_DEVWID_16 (1 << 4)
73 #define FSMC_ECCEN (1 << 6)
74 #define FSMC_ECCPLEN_512 (0 << 7)
75 #define FSMC_ECCPLEN_256 (1 << 7)
76 #define FSMC_TCLR_1 (1)
77 #define FSMC_TCLR_SHIFT (9)
78 #define FSMC_TCLR_MASK (0xF)
79 #define FSMC_TAR_1 (1)
80 #define FSMC_TAR_SHIFT (13)
81 #define FSMC_TAR_MASK (0xF)
83 /* sts register definitions */
84 #define FSMC_CODE_RDY (1 << 15)
86 /* comm register definitions */
88 #define FSMC_TSET_SHIFT 0
89 #define FSMC_TSET_MASK 0xFF
90 #define FSMC_TWAIT_6 6
91 #define FSMC_TWAIT_SHIFT 8
92 #define FSMC_TWAIT_MASK 0xFF
93 #define FSMC_THOLD_4 4
94 #define FSMC_THOLD_SHIFT 16
95 #define FSMC_THOLD_MASK 0xFF
97 #define FSMC_THIZ_SHIFT 24
98 #define FSMC_THIZ_MASK 0xFF
104 #define FSMC_NAND_BANK_SZ 0x20
106 #define FSMC_NAND_REG(base, bank, reg) (base + FSMC_NOR_REG_SIZE + \
107 (FSMC_NAND_BANK_SZ * (bank)) + \
110 #define FSMC_BUSY_WAIT_TIMEOUT (1 * HZ)
112 struct fsmc_nand_timings
{
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
;
154 enum access_mode mode
;
157 /* DMA related objects */
158 struct dma_chan
*read_dma_chan
;
159 struct dma_chan
*write_dma_chan
;
160 struct completion dma_access_complete
;
162 struct fsmc_nand_timings
*dev_timings
;
165 void __iomem
*data_va
;
166 void __iomem
*cmd_va
;
167 void __iomem
*addr_va
;
168 void __iomem
*regs_va
;
171 static int fsmc_ecc1_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
172 struct mtd_oob_region
*oobregion
)
174 struct nand_chip
*chip
= mtd_to_nand(mtd
);
176 if (section
>= chip
->ecc
.steps
)
179 oobregion
->offset
= (section
* 16) + 2;
180 oobregion
->length
= 3;
185 static int fsmc_ecc1_ooblayout_free(struct mtd_info
*mtd
, int section
,
186 struct mtd_oob_region
*oobregion
)
188 struct nand_chip
*chip
= mtd_to_nand(mtd
);
190 if (section
>= chip
->ecc
.steps
)
193 oobregion
->offset
= (section
* 16) + 8;
195 if (section
< chip
->ecc
.steps
- 1)
196 oobregion
->length
= 8;
198 oobregion
->length
= mtd
->oobsize
- oobregion
->offset
;
203 static const struct mtd_ooblayout_ops fsmc_ecc1_ooblayout_ops
= {
204 .ecc
= fsmc_ecc1_ooblayout_ecc
,
205 .free
= fsmc_ecc1_ooblayout_free
,
209 * ECC placement definitions in oobfree type format.
210 * There are 13 bytes of ecc for every 512 byte block and it has to be read
211 * consecutively and immediately after the 512 byte data block for hardware to
212 * generate the error bit offsets in 512 byte data.
214 static int fsmc_ecc4_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
215 struct mtd_oob_region
*oobregion
)
217 struct nand_chip
*chip
= mtd_to_nand(mtd
);
219 if (section
>= chip
->ecc
.steps
)
222 oobregion
->length
= chip
->ecc
.bytes
;
224 if (!section
&& mtd
->writesize
<= 512)
225 oobregion
->offset
= 0;
227 oobregion
->offset
= (section
* 16) + 2;
232 static int fsmc_ecc4_ooblayout_free(struct mtd_info
*mtd
, int section
,
233 struct mtd_oob_region
*oobregion
)
235 struct nand_chip
*chip
= mtd_to_nand(mtd
);
237 if (section
>= chip
->ecc
.steps
)
240 oobregion
->offset
= (section
* 16) + 15;
242 if (section
< chip
->ecc
.steps
- 1)
243 oobregion
->length
= 3;
245 oobregion
->length
= mtd
->oobsize
- oobregion
->offset
;
250 static const struct mtd_ooblayout_ops fsmc_ecc4_ooblayout_ops
= {
251 .ecc
= fsmc_ecc4_ooblayout_ecc
,
252 .free
= fsmc_ecc4_ooblayout_free
,
255 static inline struct fsmc_nand_data
*mtd_to_fsmc(struct mtd_info
*mtd
)
257 return container_of(mtd_to_nand(mtd
), struct fsmc_nand_data
, nand
);
261 * fsmc_cmd_ctrl - For facilitaing Hardware access
262 * This routine allows hardware specific access to control-lines(ALE,CLE)
264 static void fsmc_cmd_ctrl(struct mtd_info
*mtd
, int cmd
, unsigned int ctrl
)
266 struct nand_chip
*this = mtd_to_nand(mtd
);
267 struct fsmc_nand_data
*host
= mtd_to_fsmc(mtd
);
268 void __iomem
*regs
= host
->regs_va
;
269 unsigned int bank
= host
->bank
;
271 if (ctrl
& NAND_CTRL_CHANGE
) {
274 if (ctrl
& NAND_CLE
) {
275 this->IO_ADDR_R
= host
->cmd_va
;
276 this->IO_ADDR_W
= host
->cmd_va
;
277 } else if (ctrl
& NAND_ALE
) {
278 this->IO_ADDR_R
= host
->addr_va
;
279 this->IO_ADDR_W
= host
->addr_va
;
281 this->IO_ADDR_R
= host
->data_va
;
282 this->IO_ADDR_W
= host
->data_va
;
285 pc
= readl(FSMC_NAND_REG(regs
, bank
, PC
));
290 writel_relaxed(pc
, FSMC_NAND_REG(regs
, bank
, PC
));
295 if (cmd
!= NAND_CMD_NONE
)
296 writeb_relaxed(cmd
, this->IO_ADDR_W
);
300 * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
302 * This routine initializes timing parameters related to NAND memory access in
305 static void fsmc_nand_setup(struct fsmc_nand_data
*host
,
306 struct fsmc_nand_timings
*tims
)
308 uint32_t value
= FSMC_DEVTYPE_NAND
| FSMC_ENABLE
| FSMC_WAITON
;
309 uint32_t tclr
, tar
, thiz
, thold
, twait
, tset
;
310 unsigned int bank
= host
->bank
;
311 void __iomem
*regs
= host
->regs_va
;
313 tclr
= (tims
->tclr
& FSMC_TCLR_MASK
) << FSMC_TCLR_SHIFT
;
314 tar
= (tims
->tar
& FSMC_TAR_MASK
) << FSMC_TAR_SHIFT
;
315 thiz
= (tims
->thiz
& FSMC_THIZ_MASK
) << FSMC_THIZ_SHIFT
;
316 thold
= (tims
->thold
& FSMC_THOLD_MASK
) << FSMC_THOLD_SHIFT
;
317 twait
= (tims
->twait
& FSMC_TWAIT_MASK
) << FSMC_TWAIT_SHIFT
;
318 tset
= (tims
->tset
& FSMC_TSET_MASK
) << FSMC_TSET_SHIFT
;
320 if (host
->nand
.options
& NAND_BUSWIDTH_16
)
321 writel_relaxed(value
| FSMC_DEVWID_16
,
322 FSMC_NAND_REG(regs
, bank
, PC
));
324 writel_relaxed(value
| FSMC_DEVWID_8
,
325 FSMC_NAND_REG(regs
, bank
, PC
));
327 writel_relaxed(readl(FSMC_NAND_REG(regs
, bank
, PC
)) | tclr
| tar
,
328 FSMC_NAND_REG(regs
, bank
, PC
));
329 writel_relaxed(thiz
| thold
| twait
| tset
,
330 FSMC_NAND_REG(regs
, bank
, COMM
));
331 writel_relaxed(thiz
| thold
| twait
| tset
,
332 FSMC_NAND_REG(regs
, bank
, ATTRIB
));
335 static int fsmc_calc_timings(struct fsmc_nand_data
*host
,
336 const struct nand_sdr_timings
*sdrt
,
337 struct fsmc_nand_timings
*tims
)
339 unsigned long hclk
= clk_get_rate(host
->clk
);
340 unsigned long hclkn
= NSEC_PER_SEC
/ hclk
;
341 uint32_t thiz
, thold
, twait
, tset
;
343 if (sdrt
->tRC_min
< 30000)
346 tims
->tar
= DIV_ROUND_UP(sdrt
->tAR_min
/ 1000, hclkn
) - 1;
347 if (tims
->tar
> FSMC_TAR_MASK
)
348 tims
->tar
= FSMC_TAR_MASK
;
349 tims
->tclr
= DIV_ROUND_UP(sdrt
->tCLR_min
/ 1000, hclkn
) - 1;
350 if (tims
->tclr
> FSMC_TCLR_MASK
)
351 tims
->tclr
= FSMC_TCLR_MASK
;
353 thiz
= sdrt
->tCS_min
- sdrt
->tWP_min
;
354 tims
->thiz
= DIV_ROUND_UP(thiz
/ 1000, hclkn
);
356 thold
= sdrt
->tDH_min
;
357 if (thold
< sdrt
->tCH_min
)
358 thold
= sdrt
->tCH_min
;
359 if (thold
< sdrt
->tCLH_min
)
360 thold
= sdrt
->tCLH_min
;
361 if (thold
< sdrt
->tWH_min
)
362 thold
= sdrt
->tWH_min
;
363 if (thold
< sdrt
->tALH_min
)
364 thold
= sdrt
->tALH_min
;
365 if (thold
< sdrt
->tREH_min
)
366 thold
= sdrt
->tREH_min
;
367 tims
->thold
= DIV_ROUND_UP(thold
/ 1000, hclkn
);
368 if (tims
->thold
== 0)
370 else if (tims
->thold
> FSMC_THOLD_MASK
)
371 tims
->thold
= FSMC_THOLD_MASK
;
373 twait
= max(sdrt
->tRP_min
, sdrt
->tWP_min
);
374 tims
->twait
= DIV_ROUND_UP(twait
/ 1000, hclkn
) - 1;
375 if (tims
->twait
== 0)
377 else if (tims
->twait
> FSMC_TWAIT_MASK
)
378 tims
->twait
= FSMC_TWAIT_MASK
;
380 tset
= max(sdrt
->tCS_min
- sdrt
->tWP_min
,
381 sdrt
->tCEA_max
- sdrt
->tREA_max
);
382 tims
->tset
= DIV_ROUND_UP(tset
/ 1000, hclkn
) - 1;
385 else if (tims
->tset
> FSMC_TSET_MASK
)
386 tims
->tset
= FSMC_TSET_MASK
;
391 static int fsmc_setup_data_interface(struct mtd_info
*mtd
, int csline
,
392 const struct nand_data_interface
*conf
)
394 struct nand_chip
*nand
= mtd_to_nand(mtd
);
395 struct fsmc_nand_data
*host
= nand_get_controller_data(nand
);
396 struct fsmc_nand_timings tims
;
397 const struct nand_sdr_timings
*sdrt
;
400 sdrt
= nand_get_sdr_timings(conf
);
402 return PTR_ERR(sdrt
);
404 ret
= fsmc_calc_timings(host
, sdrt
, &tims
);
408 if (csline
== NAND_DATA_IFACE_CHECK_ONLY
)
411 fsmc_nand_setup(host
, &tims
);
417 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
419 static void fsmc_enable_hwecc(struct mtd_info
*mtd
, int mode
)
421 struct fsmc_nand_data
*host
= mtd_to_fsmc(mtd
);
422 void __iomem
*regs
= host
->regs_va
;
423 uint32_t bank
= host
->bank
;
425 writel_relaxed(readl(FSMC_NAND_REG(regs
, bank
, PC
)) & ~FSMC_ECCPLEN_256
,
426 FSMC_NAND_REG(regs
, bank
, PC
));
427 writel_relaxed(readl(FSMC_NAND_REG(regs
, bank
, PC
)) & ~FSMC_ECCEN
,
428 FSMC_NAND_REG(regs
, bank
, PC
));
429 writel_relaxed(readl(FSMC_NAND_REG(regs
, bank
, PC
)) | FSMC_ECCEN
,
430 FSMC_NAND_REG(regs
, bank
, PC
));
434 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
435 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
438 static int fsmc_read_hwecc_ecc4(struct mtd_info
*mtd
, const uint8_t *data
,
441 struct fsmc_nand_data
*host
= mtd_to_fsmc(mtd
);
442 void __iomem
*regs
= host
->regs_va
;
443 uint32_t bank
= host
->bank
;
445 unsigned long deadline
= jiffies
+ FSMC_BUSY_WAIT_TIMEOUT
;
448 if (readl_relaxed(FSMC_NAND_REG(regs
, bank
, STS
)) & FSMC_CODE_RDY
)
452 } while (!time_after_eq(jiffies
, deadline
));
454 if (time_after_eq(jiffies
, deadline
)) {
455 dev_err(host
->dev
, "calculate ecc timed out\n");
459 ecc_tmp
= readl_relaxed(FSMC_NAND_REG(regs
, bank
, ECC1
));
460 ecc
[0] = (uint8_t) (ecc_tmp
>> 0);
461 ecc
[1] = (uint8_t) (ecc_tmp
>> 8);
462 ecc
[2] = (uint8_t) (ecc_tmp
>> 16);
463 ecc
[3] = (uint8_t) (ecc_tmp
>> 24);
465 ecc_tmp
= readl_relaxed(FSMC_NAND_REG(regs
, bank
, ECC2
));
466 ecc
[4] = (uint8_t) (ecc_tmp
>> 0);
467 ecc
[5] = (uint8_t) (ecc_tmp
>> 8);
468 ecc
[6] = (uint8_t) (ecc_tmp
>> 16);
469 ecc
[7] = (uint8_t) (ecc_tmp
>> 24);
471 ecc_tmp
= readl_relaxed(FSMC_NAND_REG(regs
, bank
, ECC3
));
472 ecc
[8] = (uint8_t) (ecc_tmp
>> 0);
473 ecc
[9] = (uint8_t) (ecc_tmp
>> 8);
474 ecc
[10] = (uint8_t) (ecc_tmp
>> 16);
475 ecc
[11] = (uint8_t) (ecc_tmp
>> 24);
477 ecc_tmp
= readl_relaxed(FSMC_NAND_REG(regs
, bank
, STS
));
478 ecc
[12] = (uint8_t) (ecc_tmp
>> 16);
484 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
485 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
488 static int fsmc_read_hwecc_ecc1(struct mtd_info
*mtd
, const uint8_t *data
,
491 struct fsmc_nand_data
*host
= mtd_to_fsmc(mtd
);
492 void __iomem
*regs
= host
->regs_va
;
493 uint32_t bank
= host
->bank
;
496 ecc_tmp
= readl_relaxed(FSMC_NAND_REG(regs
, bank
, ECC1
));
497 ecc
[0] = (uint8_t) (ecc_tmp
>> 0);
498 ecc
[1] = (uint8_t) (ecc_tmp
>> 8);
499 ecc
[2] = (uint8_t) (ecc_tmp
>> 16);
504 /* Count the number of 0's in buff upto a max of max_bits */
505 static int count_written_bits(uint8_t *buff
, int size
, int max_bits
)
507 int k
, written_bits
= 0;
509 for (k
= 0; k
< size
; k
++) {
510 written_bits
+= hweight8(~buff
[k
]);
511 if (written_bits
> max_bits
)
518 static void dma_complete(void *param
)
520 struct fsmc_nand_data
*host
= param
;
522 complete(&host
->dma_access_complete
);
525 static int dma_xfer(struct fsmc_nand_data
*host
, void *buffer
, int len
,
526 enum dma_data_direction direction
)
528 struct dma_chan
*chan
;
529 struct dma_device
*dma_dev
;
530 struct dma_async_tx_descriptor
*tx
;
531 dma_addr_t dma_dst
, dma_src
, dma_addr
;
533 unsigned long flags
= DMA_CTRL_ACK
| DMA_PREP_INTERRUPT
;
535 unsigned long time_left
;
537 if (direction
== DMA_TO_DEVICE
)
538 chan
= host
->write_dma_chan
;
539 else if (direction
== DMA_FROM_DEVICE
)
540 chan
= host
->read_dma_chan
;
544 dma_dev
= chan
->device
;
545 dma_addr
= dma_map_single(dma_dev
->dev
, buffer
, len
, direction
);
547 if (direction
== DMA_TO_DEVICE
) {
549 dma_dst
= host
->data_pa
;
551 dma_src
= host
->data_pa
;
555 tx
= dma_dev
->device_prep_dma_memcpy(chan
, dma_dst
, dma_src
,
558 dev_err(host
->dev
, "device_prep_dma_memcpy error\n");
563 tx
->callback
= dma_complete
;
564 tx
->callback_param
= host
;
565 cookie
= tx
->tx_submit(tx
);
567 ret
= dma_submit_error(cookie
);
569 dev_err(host
->dev
, "dma_submit_error %d\n", cookie
);
573 dma_async_issue_pending(chan
);
576 wait_for_completion_timeout(&host
->dma_access_complete
,
577 msecs_to_jiffies(3000));
578 if (time_left
== 0) {
579 dmaengine_terminate_all(chan
);
580 dev_err(host
->dev
, "wait_for_completion_timeout\n");
588 dma_unmap_single(dma_dev
->dev
, dma_addr
, len
, direction
);
594 * fsmc_write_buf - write buffer to chip
595 * @mtd: MTD device structure
597 * @len: number of bytes to write
599 static void fsmc_write_buf(struct mtd_info
*mtd
, const uint8_t *buf
, int len
)
602 struct nand_chip
*chip
= mtd_to_nand(mtd
);
604 if (IS_ALIGNED((uint32_t)buf
, sizeof(uint32_t)) &&
605 IS_ALIGNED(len
, sizeof(uint32_t))) {
606 uint32_t *p
= (uint32_t *)buf
;
608 for (i
= 0; i
< len
; i
++)
609 writel_relaxed(p
[i
], chip
->IO_ADDR_W
);
611 for (i
= 0; i
< len
; i
++)
612 writeb_relaxed(buf
[i
], chip
->IO_ADDR_W
);
617 * fsmc_read_buf - read chip data into buffer
618 * @mtd: MTD device structure
619 * @buf: buffer to store date
620 * @len: number of bytes to read
622 static void fsmc_read_buf(struct mtd_info
*mtd
, uint8_t *buf
, int len
)
625 struct nand_chip
*chip
= mtd_to_nand(mtd
);
627 if (IS_ALIGNED((uint32_t)buf
, sizeof(uint32_t)) &&
628 IS_ALIGNED(len
, sizeof(uint32_t))) {
629 uint32_t *p
= (uint32_t *)buf
;
631 for (i
= 0; i
< len
; i
++)
632 p
[i
] = readl_relaxed(chip
->IO_ADDR_R
);
634 for (i
= 0; i
< len
; i
++)
635 buf
[i
] = readb_relaxed(chip
->IO_ADDR_R
);
640 * fsmc_read_buf_dma - read chip data into buffer
641 * @mtd: MTD device structure
642 * @buf: buffer to store date
643 * @len: number of bytes to read
645 static void fsmc_read_buf_dma(struct mtd_info
*mtd
, uint8_t *buf
, int len
)
647 struct fsmc_nand_data
*host
= mtd_to_fsmc(mtd
);
649 dma_xfer(host
, buf
, len
, DMA_FROM_DEVICE
);
653 * fsmc_write_buf_dma - write buffer to chip
654 * @mtd: MTD device structure
656 * @len: number of bytes to write
658 static void fsmc_write_buf_dma(struct mtd_info
*mtd
, const uint8_t *buf
,
661 struct fsmc_nand_data
*host
= mtd_to_fsmc(mtd
);
663 dma_xfer(host
, (void *)buf
, len
, DMA_TO_DEVICE
);
667 * fsmc_read_page_hwecc
668 * @mtd: mtd info structure
669 * @chip: nand chip info structure
670 * @buf: buffer to store read data
671 * @oob_required: caller expects OOB data read to chip->oob_poi
672 * @page: page number to read
674 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
675 * performed in a strict sequence as follows:
676 * data(512 byte) -> ecc(13 byte)
677 * After this read, fsmc hardware generates and reports error data bits(up to a
680 static int fsmc_read_page_hwecc(struct mtd_info
*mtd
, struct nand_chip
*chip
,
681 uint8_t *buf
, int oob_required
, int page
)
683 int i
, j
, s
, stat
, eccsize
= chip
->ecc
.size
;
684 int eccbytes
= chip
->ecc
.bytes
;
685 int eccsteps
= chip
->ecc
.steps
;
687 uint8_t *ecc_calc
= chip
->ecc
.calc_buf
;
688 uint8_t *ecc_code
= chip
->ecc
.code_buf
;
689 int off
, len
, group
= 0;
691 * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
692 * end up reading 14 bytes (7 words) from oob. The local array is
693 * to maintain word alignment
696 uint8_t *oob
= (uint8_t *)&ecc_oob
[0];
697 unsigned int max_bitflips
= 0;
699 for (i
= 0, s
= 0; s
< eccsteps
; s
++, i
+= eccbytes
, p
+= eccsize
) {
700 nand_read_page_op(chip
, page
, s
* eccsize
, NULL
, 0);
701 chip
->ecc
.hwctl(mtd
, NAND_ECC_READ
);
702 chip
->read_buf(mtd
, p
, eccsize
);
704 for (j
= 0; j
< eccbytes
;) {
705 struct mtd_oob_region oobregion
;
708 ret
= mtd_ooblayout_ecc(mtd
, group
++, &oobregion
);
712 off
= oobregion
.offset
;
713 len
= oobregion
.length
;
716 * length is intentionally kept a higher multiple of 2
717 * to read at least 13 bytes even in case of 16 bit NAND
720 if (chip
->options
& NAND_BUSWIDTH_16
)
721 len
= roundup(len
, 2);
723 nand_read_oob_op(chip
, page
, off
, oob
+ j
, len
);
727 memcpy(&ecc_code
[i
], oob
, chip
->ecc
.bytes
);
728 chip
->ecc
.calculate(mtd
, p
, &ecc_calc
[i
]);
730 stat
= chip
->ecc
.correct(mtd
, p
, &ecc_code
[i
], &ecc_calc
[i
]);
732 mtd
->ecc_stats
.failed
++;
734 mtd
->ecc_stats
.corrected
+= stat
;
735 max_bitflips
= max_t(unsigned int, max_bitflips
, stat
);
743 * fsmc_bch8_correct_data
744 * @mtd: mtd info structure
745 * @dat: buffer of read data
746 * @read_ecc: ecc read from device spare area
747 * @calc_ecc: ecc calculated from read data
749 * calc_ecc is a 104 bit information containing maximum of 8 error
750 * offset informations of 13 bits each in 512 bytes of read data.
752 static int fsmc_bch8_correct_data(struct mtd_info
*mtd
, uint8_t *dat
,
753 uint8_t *read_ecc
, uint8_t *calc_ecc
)
755 struct nand_chip
*chip
= mtd_to_nand(mtd
);
756 struct fsmc_nand_data
*host
= mtd_to_fsmc(mtd
);
757 void __iomem
*regs
= host
->regs_va
;
758 unsigned int bank
= host
->bank
;
761 uint32_t ecc1
, ecc2
, ecc3
, ecc4
;
763 num_err
= (readl_relaxed(FSMC_NAND_REG(regs
, bank
, STS
)) >> 10) & 0xF;
765 /* no bit flipping */
766 if (likely(num_err
== 0))
769 /* too many errors */
770 if (unlikely(num_err
> 8)) {
772 * This is a temporary erase check. A newly erased page read
773 * would result in an ecc error because the oob data is also
774 * erased to FF and the calculated ecc for an FF data is not
776 * This is a workaround to skip performing correction in case
780 * For every page, each bit written as 0 is counted until these
781 * number of bits are greater than 8 (the maximum correction
782 * capability of FSMC for each 512 + 13 bytes)
785 int bits_ecc
= count_written_bits(read_ecc
, chip
->ecc
.bytes
, 8);
786 int bits_data
= count_written_bits(dat
, chip
->ecc
.size
, 8);
788 if ((bits_ecc
+ bits_data
) <= 8) {
790 memset(dat
, 0xff, chip
->ecc
.size
);
798 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
799 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
801 * calc_ecc is a 104 bit information containing maximum of 8 error
802 * offset informations of 13 bits each. calc_ecc is copied into a
803 * uint64_t array and error offset indexes are populated in err_idx
806 ecc1
= readl_relaxed(FSMC_NAND_REG(regs
, bank
, ECC1
));
807 ecc2
= readl_relaxed(FSMC_NAND_REG(regs
, bank
, ECC2
));
808 ecc3
= readl_relaxed(FSMC_NAND_REG(regs
, bank
, ECC3
));
809 ecc4
= readl_relaxed(FSMC_NAND_REG(regs
, bank
, STS
));
811 err_idx
[0] = (ecc1
>> 0) & 0x1FFF;
812 err_idx
[1] = (ecc1
>> 13) & 0x1FFF;
813 err_idx
[2] = (((ecc2
>> 0) & 0x7F) << 6) | ((ecc1
>> 26) & 0x3F);
814 err_idx
[3] = (ecc2
>> 7) & 0x1FFF;
815 err_idx
[4] = (((ecc3
>> 0) & 0x1) << 12) | ((ecc2
>> 20) & 0xFFF);
816 err_idx
[5] = (ecc3
>> 1) & 0x1FFF;
817 err_idx
[6] = (ecc3
>> 14) & 0x1FFF;
818 err_idx
[7] = (((ecc4
>> 16) & 0xFF) << 5) | ((ecc3
>> 27) & 0x1F);
822 change_bit(0, (unsigned long *)&err_idx
[i
]);
823 change_bit(1, (unsigned long *)&err_idx
[i
]);
825 if (err_idx
[i
] < chip
->ecc
.size
* 8) {
826 change_bit(err_idx
[i
], (unsigned long *)dat
);
833 static bool filter(struct dma_chan
*chan
, void *slave
)
835 chan
->private = slave
;
839 static int fsmc_nand_probe_config_dt(struct platform_device
*pdev
,
840 struct fsmc_nand_data
*host
,
841 struct nand_chip
*nand
)
843 struct device_node
*np
= pdev
->dev
.of_node
;
849 if (!of_property_read_u32(np
, "bank-width", &val
)) {
851 nand
->options
|= NAND_BUSWIDTH_16
;
852 } else if (val
!= 1) {
853 dev_err(&pdev
->dev
, "invalid bank-width %u\n", val
);
858 if (of_get_property(np
, "nand-skip-bbtscan", NULL
))
859 nand
->options
|= NAND_SKIP_BBTSCAN
;
861 host
->dev_timings
= devm_kzalloc(&pdev
->dev
,
862 sizeof(*host
->dev_timings
), GFP_KERNEL
);
863 if (!host
->dev_timings
)
865 ret
= of_property_read_u8_array(np
, "timings", (u8
*)host
->dev_timings
,
866 sizeof(*host
->dev_timings
));
868 host
->dev_timings
= NULL
;
870 /* Set default NAND bank to 0 */
872 if (!of_property_read_u32(np
, "bank", &val
)) {
874 dev_err(&pdev
->dev
, "invalid bank %u\n", val
);
883 * fsmc_nand_probe - Probe function
884 * @pdev: platform device structure
886 static int __init
fsmc_nand_probe(struct platform_device
*pdev
)
888 struct fsmc_nand_data
*host
;
889 struct mtd_info
*mtd
;
890 struct nand_chip
*nand
;
891 struct resource
*res
;
897 /* Allocate memory for the device structure (and zero it) */
898 host
= devm_kzalloc(&pdev
->dev
, sizeof(*host
), GFP_KERNEL
);
904 ret
= fsmc_nand_probe_config_dt(pdev
, host
, nand
);
908 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "nand_data");
909 host
->data_va
= devm_ioremap_resource(&pdev
->dev
, res
);
910 if (IS_ERR(host
->data_va
))
911 return PTR_ERR(host
->data_va
);
913 host
->data_pa
= (dma_addr_t
)res
->start
;
915 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "nand_addr");
916 host
->addr_va
= devm_ioremap_resource(&pdev
->dev
, res
);
917 if (IS_ERR(host
->addr_va
))
918 return PTR_ERR(host
->addr_va
);
920 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "nand_cmd");
921 host
->cmd_va
= devm_ioremap_resource(&pdev
->dev
, res
);
922 if (IS_ERR(host
->cmd_va
))
923 return PTR_ERR(host
->cmd_va
);
925 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "fsmc_regs");
926 host
->regs_va
= devm_ioremap_resource(&pdev
->dev
, res
);
927 if (IS_ERR(host
->regs_va
))
928 return PTR_ERR(host
->regs_va
);
930 host
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
931 if (IS_ERR(host
->clk
)) {
932 dev_err(&pdev
->dev
, "failed to fetch block clock\n");
933 return PTR_ERR(host
->clk
);
936 ret
= clk_prepare_enable(host
->clk
);
941 * This device ID is actually a common AMBA ID as used on the
942 * AMBA PrimeCell bus. However it is not a PrimeCell.
944 for (pid
= 0, i
= 0; i
< 4; i
++)
945 pid
|= (readl(host
->regs_va
+ resource_size(res
) - 0x20 + 4 * i
) & 255) << (i
* 8);
947 dev_info(&pdev
->dev
, "FSMC device partno %03x, manufacturer %02x, "
948 "revision %02x, config %02x\n",
949 AMBA_PART_BITS(pid
), AMBA_MANF_BITS(pid
),
950 AMBA_REV_BITS(pid
), AMBA_CONFIG_BITS(pid
));
952 host
->dev
= &pdev
->dev
;
954 if (host
->mode
== USE_DMA_ACCESS
)
955 init_completion(&host
->dma_access_complete
);
957 /* Link all private pointers */
958 mtd
= nand_to_mtd(&host
->nand
);
959 nand_set_controller_data(nand
, host
);
960 nand_set_flash_node(nand
, pdev
->dev
.of_node
);
962 mtd
->dev
.parent
= &pdev
->dev
;
963 nand
->IO_ADDR_R
= host
->data_va
;
964 nand
->IO_ADDR_W
= host
->data_va
;
965 nand
->cmd_ctrl
= fsmc_cmd_ctrl
;
966 nand
->chip_delay
= 30;
969 * Setup default ECC mode. nand_dt_init() called from nand_scan_ident()
970 * can overwrite this value if the DT provides a different value.
972 nand
->ecc
.mode
= NAND_ECC_HW
;
973 nand
->ecc
.hwctl
= fsmc_enable_hwecc
;
974 nand
->ecc
.size
= 512;
975 nand
->badblockbits
= 7;
977 switch (host
->mode
) {
980 dma_cap_set(DMA_MEMCPY
, mask
);
981 host
->read_dma_chan
= dma_request_channel(mask
, filter
, NULL
);
982 if (!host
->read_dma_chan
) {
983 dev_err(&pdev
->dev
, "Unable to get read dma channel\n");
984 goto err_req_read_chnl
;
986 host
->write_dma_chan
= dma_request_channel(mask
, filter
, NULL
);
987 if (!host
->write_dma_chan
) {
988 dev_err(&pdev
->dev
, "Unable to get write dma channel\n");
989 goto err_req_write_chnl
;
991 nand
->read_buf
= fsmc_read_buf_dma
;
992 nand
->write_buf
= fsmc_write_buf_dma
;
996 case USE_WORD_ACCESS
:
997 nand
->read_buf
= fsmc_read_buf
;
998 nand
->write_buf
= fsmc_write_buf
;
1002 if (host
->dev_timings
)
1003 fsmc_nand_setup(host
, host
->dev_timings
);
1005 nand
->setup_data_interface
= fsmc_setup_data_interface
;
1007 if (AMBA_REV_BITS(host
->pid
) >= 8) {
1008 nand
->ecc
.read_page
= fsmc_read_page_hwecc
;
1009 nand
->ecc
.calculate
= fsmc_read_hwecc_ecc4
;
1010 nand
->ecc
.correct
= fsmc_bch8_correct_data
;
1011 nand
->ecc
.bytes
= 13;
1012 nand
->ecc
.strength
= 8;
1016 * Scan to find existence of the device
1018 ret
= nand_scan_ident(mtd
, 1, NULL
);
1020 dev_err(&pdev
->dev
, "No NAND Device found!\n");
1021 goto err_scan_ident
;
1024 if (AMBA_REV_BITS(host
->pid
) >= 8) {
1025 switch (mtd
->oobsize
) {
1033 dev_warn(&pdev
->dev
, "No oob scheme defined for oobsize %d\n",
1039 mtd_set_ooblayout(mtd
, &fsmc_ecc4_ooblayout_ops
);
1041 switch (nand
->ecc
.mode
) {
1043 dev_info(&pdev
->dev
, "Using 1-bit HW ECC scheme\n");
1044 nand
->ecc
.calculate
= fsmc_read_hwecc_ecc1
;
1045 nand
->ecc
.correct
= nand_correct_data
;
1046 nand
->ecc
.bytes
= 3;
1047 nand
->ecc
.strength
= 1;
1051 if (nand
->ecc
.algo
== NAND_ECC_BCH
) {
1052 dev_info(&pdev
->dev
, "Using 4-bit SW BCH ECC scheme\n");
1056 case NAND_ECC_ON_DIE
:
1060 dev_err(&pdev
->dev
, "Unsupported ECC mode!\n");
1065 * Don't set layout for BCH4 SW ECC. This will be
1066 * generated later in nand_bch_init() later.
1068 if (nand
->ecc
.mode
== NAND_ECC_HW
) {
1069 switch (mtd
->oobsize
) {
1073 mtd_set_ooblayout(mtd
,
1074 &fsmc_ecc1_ooblayout_ops
);
1077 dev_warn(&pdev
->dev
,
1078 "No oob scheme defined for oobsize %d\n",
1086 /* Second stage of scan to fill MTD data-structures */
1087 ret
= nand_scan_tail(mtd
);
1092 ret
= mtd_device_register(mtd
, NULL
, 0);
1096 platform_set_drvdata(pdev
, host
);
1097 dev_info(&pdev
->dev
, "FSMC NAND driver registration successful\n");
1102 if (host
->mode
== USE_DMA_ACCESS
)
1103 dma_release_channel(host
->write_dma_chan
);
1105 if (host
->mode
== USE_DMA_ACCESS
)
1106 dma_release_channel(host
->read_dma_chan
);
1108 clk_disable_unprepare(host
->clk
);
1115 static int fsmc_nand_remove(struct platform_device
*pdev
)
1117 struct fsmc_nand_data
*host
= platform_get_drvdata(pdev
);
1120 nand_release(nand_to_mtd(&host
->nand
));
1122 if (host
->mode
== USE_DMA_ACCESS
) {
1123 dma_release_channel(host
->write_dma_chan
);
1124 dma_release_channel(host
->read_dma_chan
);
1126 clk_disable_unprepare(host
->clk
);
1132 #ifdef CONFIG_PM_SLEEP
1133 static int fsmc_nand_suspend(struct device
*dev
)
1135 struct fsmc_nand_data
*host
= dev_get_drvdata(dev
);
1137 clk_disable_unprepare(host
->clk
);
1141 static int fsmc_nand_resume(struct device
*dev
)
1143 struct fsmc_nand_data
*host
= dev_get_drvdata(dev
);
1145 clk_prepare_enable(host
->clk
);
1146 if (host
->dev_timings
)
1147 fsmc_nand_setup(host
, host
->dev_timings
);
1153 static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops
, fsmc_nand_suspend
, fsmc_nand_resume
);
1155 static const struct of_device_id fsmc_nand_id_table
[] = {
1156 { .compatible
= "st,spear600-fsmc-nand" },
1157 { .compatible
= "stericsson,fsmc-nand" },
1160 MODULE_DEVICE_TABLE(of
, fsmc_nand_id_table
);
1162 static struct platform_driver fsmc_nand_driver
= {
1163 .remove
= fsmc_nand_remove
,
1165 .name
= "fsmc-nand",
1166 .of_match_table
= fsmc_nand_id_table
,
1167 .pm
= &fsmc_nand_pm_ops
,
1171 module_platform_driver_probe(fsmc_nand_driver
, fsmc_nand_probe
);
1173 MODULE_LICENSE("GPL");
1174 MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
1175 MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");