1 // SPDX-License-Identifier: GPL-2.0
4 * Flexible Static Memory Controller (FSMC)
5 * Driver for NAND portions
7 * Copyright © 2010 ST Microelectronics
8 * Vipin Kumar <vipin.kumar@st.com>
11 * Based on drivers/mtd/nand/nomadik_nand.c (removed in v3.8)
12 * Copyright © 2007 STMicroelectronics Pvt. Ltd.
13 * Copyright © 2009 Alessandro Rubini
16 #include <linux/clk.h>
17 #include <linux/completion.h>
18 #include <linux/dmaengine.h>
19 #include <linux/dma-direction.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/err.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/resource.h>
25 #include <linux/sched.h>
26 #include <linux/types.h>
27 #include <linux/mtd/mtd.h>
28 #include <linux/mtd/rawnand.h>
29 #include <linux/platform_device.h>
31 #include <linux/mtd/partitions.h>
33 #include <linux/slab.h>
34 #include <linux/amba/bus.h>
35 #include <mtd/mtd-abi.h>
37 /* fsmc controller registers for NOR flash */
39 /* ctrl register definitions */
40 #define BANK_ENABLE BIT(0)
42 #define NOR_DEV (2 << 2)
43 #define WIDTH_16 BIT(4)
44 #define RSTPWRDWN BIT(6)
46 #define WRT_ENABLE BIT(12)
47 #define WAIT_ENB BIT(13)
50 /* ctrl_tim register definitions */
52 #define FSMC_NOR_BANK_SZ 0x8
53 #define FSMC_NOR_REG_SIZE 0x40
55 #define FSMC_NOR_REG(base, bank, reg) ((base) + \
56 (FSMC_NOR_BANK_SZ * (bank)) + \
59 /* fsmc controller registers for NAND flash */
61 /* pc register definitions */
62 #define FSMC_RESET BIT(0)
63 #define FSMC_WAITON BIT(1)
64 #define FSMC_ENABLE BIT(2)
65 #define FSMC_DEVTYPE_NAND BIT(3)
66 #define FSMC_DEVWID_16 BIT(4)
67 #define FSMC_ECCEN BIT(6)
68 #define FSMC_ECCPLEN_256 BIT(7)
69 #define FSMC_TCLR_SHIFT (9)
70 #define FSMC_TCLR_MASK (0xF)
71 #define FSMC_TAR_SHIFT (13)
72 #define FSMC_TAR_MASK (0xF)
74 /* sts register definitions */
75 #define FSMC_CODE_RDY BIT(15)
77 /* comm register definitions */
78 #define FSMC_TSET_SHIFT 0
79 #define FSMC_TSET_MASK 0xFF
80 #define FSMC_TWAIT_SHIFT 8
81 #define FSMC_TWAIT_MASK 0xFF
82 #define FSMC_THOLD_SHIFT 16
83 #define FSMC_THOLD_MASK 0xFF
84 #define FSMC_THIZ_SHIFT 24
85 #define FSMC_THIZ_MASK 0xFF
91 #define FSMC_NAND_BANK_SZ 0x20
93 #define FSMC_BUSY_WAIT_TIMEOUT (1 * HZ)
95 struct fsmc_nand_timings
{
110 * struct fsmc_nand_data - structure for FSMC NAND device state
112 * @base: Inherit from the nand_controller struct
113 * @pid: Part ID on the AMBA PrimeCell format
114 * @nand: Chip related info for a NAND flash.
116 * @bank: Bank number for probed device.
117 * @dev: Parent device
119 * @clk: Clock structure for FSMC.
121 * @read_dma_chan: DMA channel for read access
122 * @write_dma_chan: DMA channel for write access to NAND
123 * @dma_access_complete: Completion structure
125 * @dev_timings: NAND timings
127 * @data_pa: NAND Physical port for Data.
128 * @data_va: NAND port for Data.
129 * @cmd_va: NAND port for Command.
130 * @addr_va: NAND port for Address.
131 * @regs_va: Registers base address for a given bank.
133 struct fsmc_nand_data
{
134 struct nand_controller base
;
136 struct nand_chip nand
;
140 enum access_mode mode
;
143 /* DMA related objects */
144 struct dma_chan
*read_dma_chan
;
145 struct dma_chan
*write_dma_chan
;
146 struct completion dma_access_complete
;
148 struct fsmc_nand_timings
*dev_timings
;
151 void __iomem
*data_va
;
152 void __iomem
*cmd_va
;
153 void __iomem
*addr_va
;
154 void __iomem
*regs_va
;
157 static int fsmc_ecc1_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
158 struct mtd_oob_region
*oobregion
)
160 struct nand_chip
*chip
= mtd_to_nand(mtd
);
162 if (section
>= chip
->ecc
.steps
)
165 oobregion
->offset
= (section
* 16) + 2;
166 oobregion
->length
= 3;
171 static int fsmc_ecc1_ooblayout_free(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) + 8;
181 if (section
< chip
->ecc
.steps
- 1)
182 oobregion
->length
= 8;
184 oobregion
->length
= mtd
->oobsize
- oobregion
->offset
;
189 static const struct mtd_ooblayout_ops fsmc_ecc1_ooblayout_ops
= {
190 .ecc
= fsmc_ecc1_ooblayout_ecc
,
191 .free
= fsmc_ecc1_ooblayout_free
,
195 * ECC placement definitions in oobfree type format.
196 * There are 13 bytes of ecc for every 512 byte block and it has to be read
197 * consecutively and immediately after the 512 byte data block for hardware to
198 * generate the error bit offsets in 512 byte data.
200 static int fsmc_ecc4_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
201 struct mtd_oob_region
*oobregion
)
203 struct nand_chip
*chip
= mtd_to_nand(mtd
);
205 if (section
>= chip
->ecc
.steps
)
208 oobregion
->length
= chip
->ecc
.bytes
;
210 if (!section
&& mtd
->writesize
<= 512)
211 oobregion
->offset
= 0;
213 oobregion
->offset
= (section
* 16) + 2;
218 static int fsmc_ecc4_ooblayout_free(struct mtd_info
*mtd
, int section
,
219 struct mtd_oob_region
*oobregion
)
221 struct nand_chip
*chip
= mtd_to_nand(mtd
);
223 if (section
>= chip
->ecc
.steps
)
226 oobregion
->offset
= (section
* 16) + 15;
228 if (section
< chip
->ecc
.steps
- 1)
229 oobregion
->length
= 3;
231 oobregion
->length
= mtd
->oobsize
- oobregion
->offset
;
236 static const struct mtd_ooblayout_ops fsmc_ecc4_ooblayout_ops
= {
237 .ecc
= fsmc_ecc4_ooblayout_ecc
,
238 .free
= fsmc_ecc4_ooblayout_free
,
241 static inline struct fsmc_nand_data
*nand_to_fsmc(struct nand_chip
*chip
)
243 return container_of(chip
, struct fsmc_nand_data
, nand
);
247 * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
249 * This routine initializes timing parameters related to NAND memory access in
252 static void fsmc_nand_setup(struct fsmc_nand_data
*host
,
253 struct fsmc_nand_timings
*tims
)
255 u32 value
= FSMC_DEVTYPE_NAND
| FSMC_ENABLE
| FSMC_WAITON
;
256 u32 tclr
, tar
, thiz
, thold
, twait
, tset
;
258 tclr
= (tims
->tclr
& FSMC_TCLR_MASK
) << FSMC_TCLR_SHIFT
;
259 tar
= (tims
->tar
& FSMC_TAR_MASK
) << FSMC_TAR_SHIFT
;
260 thiz
= (tims
->thiz
& FSMC_THIZ_MASK
) << FSMC_THIZ_SHIFT
;
261 thold
= (tims
->thold
& FSMC_THOLD_MASK
) << FSMC_THOLD_SHIFT
;
262 twait
= (tims
->twait
& FSMC_TWAIT_MASK
) << FSMC_TWAIT_SHIFT
;
263 tset
= (tims
->tset
& FSMC_TSET_MASK
) << FSMC_TSET_SHIFT
;
265 if (host
->nand
.options
& NAND_BUSWIDTH_16
)
266 value
|= FSMC_DEVWID_16
;
268 writel_relaxed(value
| tclr
| tar
, host
->regs_va
+ FSMC_PC
);
269 writel_relaxed(thiz
| thold
| twait
| tset
, host
->regs_va
+ COMM
);
270 writel_relaxed(thiz
| thold
| twait
| tset
, host
->regs_va
+ ATTRIB
);
273 static int fsmc_calc_timings(struct fsmc_nand_data
*host
,
274 const struct nand_sdr_timings
*sdrt
,
275 struct fsmc_nand_timings
*tims
)
277 unsigned long hclk
= clk_get_rate(host
->clk
);
278 unsigned long hclkn
= NSEC_PER_SEC
/ hclk
;
279 u32 thiz
, thold
, twait
, tset
;
281 if (sdrt
->tRC_min
< 30000)
284 tims
->tar
= DIV_ROUND_UP(sdrt
->tAR_min
/ 1000, hclkn
) - 1;
285 if (tims
->tar
> FSMC_TAR_MASK
)
286 tims
->tar
= FSMC_TAR_MASK
;
287 tims
->tclr
= DIV_ROUND_UP(sdrt
->tCLR_min
/ 1000, hclkn
) - 1;
288 if (tims
->tclr
> FSMC_TCLR_MASK
)
289 tims
->tclr
= FSMC_TCLR_MASK
;
291 thiz
= sdrt
->tCS_min
- sdrt
->tWP_min
;
292 tims
->thiz
= DIV_ROUND_UP(thiz
/ 1000, hclkn
);
294 thold
= sdrt
->tDH_min
;
295 if (thold
< sdrt
->tCH_min
)
296 thold
= sdrt
->tCH_min
;
297 if (thold
< sdrt
->tCLH_min
)
298 thold
= sdrt
->tCLH_min
;
299 if (thold
< sdrt
->tWH_min
)
300 thold
= sdrt
->tWH_min
;
301 if (thold
< sdrt
->tALH_min
)
302 thold
= sdrt
->tALH_min
;
303 if (thold
< sdrt
->tREH_min
)
304 thold
= sdrt
->tREH_min
;
305 tims
->thold
= DIV_ROUND_UP(thold
/ 1000, hclkn
);
306 if (tims
->thold
== 0)
308 else if (tims
->thold
> FSMC_THOLD_MASK
)
309 tims
->thold
= FSMC_THOLD_MASK
;
311 twait
= max(sdrt
->tRP_min
, sdrt
->tWP_min
);
312 tims
->twait
= DIV_ROUND_UP(twait
/ 1000, hclkn
) - 1;
313 if (tims
->twait
== 0)
315 else if (tims
->twait
> FSMC_TWAIT_MASK
)
316 tims
->twait
= FSMC_TWAIT_MASK
;
318 tset
= max(sdrt
->tCS_min
- sdrt
->tWP_min
,
319 sdrt
->tCEA_max
- sdrt
->tREA_max
);
320 tims
->tset
= DIV_ROUND_UP(tset
/ 1000, hclkn
) - 1;
323 else if (tims
->tset
> FSMC_TSET_MASK
)
324 tims
->tset
= FSMC_TSET_MASK
;
329 static int fsmc_setup_interface(struct nand_chip
*nand
, int csline
,
330 const struct nand_interface_config
*conf
)
332 struct fsmc_nand_data
*host
= nand_to_fsmc(nand
);
333 struct fsmc_nand_timings tims
;
334 const struct nand_sdr_timings
*sdrt
;
337 sdrt
= nand_get_sdr_timings(conf
);
339 return PTR_ERR(sdrt
);
341 ret
= fsmc_calc_timings(host
, sdrt
, &tims
);
345 if (csline
== NAND_DATA_IFACE_CHECK_ONLY
)
348 fsmc_nand_setup(host
, &tims
);
354 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
356 static void fsmc_enable_hwecc(struct nand_chip
*chip
, int mode
)
358 struct fsmc_nand_data
*host
= nand_to_fsmc(chip
);
360 writel_relaxed(readl(host
->regs_va
+ FSMC_PC
) & ~FSMC_ECCPLEN_256
,
361 host
->regs_va
+ FSMC_PC
);
362 writel_relaxed(readl(host
->regs_va
+ FSMC_PC
) & ~FSMC_ECCEN
,
363 host
->regs_va
+ FSMC_PC
);
364 writel_relaxed(readl(host
->regs_va
+ FSMC_PC
) | FSMC_ECCEN
,
365 host
->regs_va
+ FSMC_PC
);
369 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
370 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
373 static int fsmc_read_hwecc_ecc4(struct nand_chip
*chip
, const u8
*data
,
376 struct fsmc_nand_data
*host
= nand_to_fsmc(chip
);
378 unsigned long deadline
= jiffies
+ FSMC_BUSY_WAIT_TIMEOUT
;
381 if (readl_relaxed(host
->regs_va
+ STS
) & FSMC_CODE_RDY
)
385 } while (!time_after_eq(jiffies
, deadline
));
387 if (time_after_eq(jiffies
, deadline
)) {
388 dev_err(host
->dev
, "calculate ecc timed out\n");
392 ecc_tmp
= readl_relaxed(host
->regs_va
+ ECC1
);
394 ecc
[1] = ecc_tmp
>> 8;
395 ecc
[2] = ecc_tmp
>> 16;
396 ecc
[3] = ecc_tmp
>> 24;
398 ecc_tmp
= readl_relaxed(host
->regs_va
+ ECC2
);
400 ecc
[5] = ecc_tmp
>> 8;
401 ecc
[6] = ecc_tmp
>> 16;
402 ecc
[7] = ecc_tmp
>> 24;
404 ecc_tmp
= readl_relaxed(host
->regs_va
+ ECC3
);
406 ecc
[9] = ecc_tmp
>> 8;
407 ecc
[10] = ecc_tmp
>> 16;
408 ecc
[11] = ecc_tmp
>> 24;
410 ecc_tmp
= readl_relaxed(host
->regs_va
+ STS
);
411 ecc
[12] = ecc_tmp
>> 16;
417 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
418 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
421 static int fsmc_read_hwecc_ecc1(struct nand_chip
*chip
, const u8
*data
,
424 struct fsmc_nand_data
*host
= nand_to_fsmc(chip
);
427 ecc_tmp
= readl_relaxed(host
->regs_va
+ ECC1
);
429 ecc
[1] = ecc_tmp
>> 8;
430 ecc
[2] = ecc_tmp
>> 16;
435 /* Count the number of 0's in buff upto a max of max_bits */
436 static int count_written_bits(u8
*buff
, int size
, int max_bits
)
438 int k
, written_bits
= 0;
440 for (k
= 0; k
< size
; k
++) {
441 written_bits
+= hweight8(~buff
[k
]);
442 if (written_bits
> max_bits
)
449 static void dma_complete(void *param
)
451 struct fsmc_nand_data
*host
= param
;
453 complete(&host
->dma_access_complete
);
456 static int dma_xfer(struct fsmc_nand_data
*host
, void *buffer
, int len
,
457 enum dma_data_direction direction
)
459 struct dma_chan
*chan
;
460 struct dma_device
*dma_dev
;
461 struct dma_async_tx_descriptor
*tx
;
462 dma_addr_t dma_dst
, dma_src
, dma_addr
;
464 unsigned long flags
= DMA_CTRL_ACK
| DMA_PREP_INTERRUPT
;
466 unsigned long time_left
;
468 if (direction
== DMA_TO_DEVICE
)
469 chan
= host
->write_dma_chan
;
470 else if (direction
== DMA_FROM_DEVICE
)
471 chan
= host
->read_dma_chan
;
475 dma_dev
= chan
->device
;
476 dma_addr
= dma_map_single(dma_dev
->dev
, buffer
, len
, direction
);
478 if (direction
== DMA_TO_DEVICE
) {
480 dma_dst
= host
->data_pa
;
482 dma_src
= host
->data_pa
;
486 tx
= dma_dev
->device_prep_dma_memcpy(chan
, dma_dst
, dma_src
,
489 dev_err(host
->dev
, "device_prep_dma_memcpy error\n");
494 tx
->callback
= dma_complete
;
495 tx
->callback_param
= host
;
496 cookie
= tx
->tx_submit(tx
);
498 ret
= dma_submit_error(cookie
);
500 dev_err(host
->dev
, "dma_submit_error %d\n", cookie
);
504 dma_async_issue_pending(chan
);
507 wait_for_completion_timeout(&host
->dma_access_complete
,
508 msecs_to_jiffies(3000));
509 if (time_left
== 0) {
510 dmaengine_terminate_all(chan
);
511 dev_err(host
->dev
, "wait_for_completion_timeout\n");
519 dma_unmap_single(dma_dev
->dev
, dma_addr
, len
, direction
);
525 * fsmc_write_buf - write buffer to chip
526 * @host: FSMC NAND controller
528 * @len: number of bytes to write
530 static void fsmc_write_buf(struct fsmc_nand_data
*host
, const u8
*buf
,
535 if (IS_ALIGNED((uintptr_t)buf
, sizeof(u32
)) &&
536 IS_ALIGNED(len
, sizeof(u32
))) {
540 for (i
= 0; i
< len
; i
++)
541 writel_relaxed(p
[i
], host
->data_va
);
543 for (i
= 0; i
< len
; i
++)
544 writeb_relaxed(buf
[i
], host
->data_va
);
549 * fsmc_read_buf - read chip data into buffer
550 * @host: FSMC NAND controller
551 * @buf: buffer to store date
552 * @len: number of bytes to read
554 static void fsmc_read_buf(struct fsmc_nand_data
*host
, u8
*buf
, int len
)
558 if (IS_ALIGNED((uintptr_t)buf
, sizeof(u32
)) &&
559 IS_ALIGNED(len
, sizeof(u32
))) {
563 for (i
= 0; i
< len
; i
++)
564 p
[i
] = readl_relaxed(host
->data_va
);
566 for (i
= 0; i
< len
; i
++)
567 buf
[i
] = readb_relaxed(host
->data_va
);
572 * fsmc_read_buf_dma - read chip data into buffer
573 * @host: FSMC NAND controller
574 * @buf: buffer to store date
575 * @len: number of bytes to read
577 static void fsmc_read_buf_dma(struct fsmc_nand_data
*host
, u8
*buf
,
580 dma_xfer(host
, buf
, len
, DMA_FROM_DEVICE
);
584 * fsmc_write_buf_dma - write buffer to chip
585 * @host: FSMC NAND controller
587 * @len: number of bytes to write
589 static void fsmc_write_buf_dma(struct fsmc_nand_data
*host
, const u8
*buf
,
592 dma_xfer(host
, (void *)buf
, len
, DMA_TO_DEVICE
);
596 * fsmc_exec_op - hook called by the core to execute NAND operations
598 * This controller is simple enough and thus does not need to use the parser
599 * provided by the core, instead, handle every situation here.
601 static int fsmc_exec_op(struct nand_chip
*chip
, const struct nand_operation
*op
,
604 struct fsmc_nand_data
*host
= nand_to_fsmc(chip
);
605 const struct nand_op_instr
*instr
= NULL
;
613 pr_debug("Executing operation [%d instructions]:\n", op
->ninstrs
);
615 for (op_id
= 0; op_id
< op
->ninstrs
; op_id
++) {
616 instr
= &op
->instrs
[op_id
];
618 nand_op_trace(" ", instr
);
620 switch (instr
->type
) {
621 case NAND_OP_CMD_INSTR
:
622 writeb_relaxed(instr
->ctx
.cmd
.opcode
, host
->cmd_va
);
625 case NAND_OP_ADDR_INSTR
:
626 for (i
= 0; i
< instr
->ctx
.addr
.naddrs
; i
++)
627 writeb_relaxed(instr
->ctx
.addr
.addrs
[i
],
631 case NAND_OP_DATA_IN_INSTR
:
632 if (host
->mode
== USE_DMA_ACCESS
)
633 fsmc_read_buf_dma(host
, instr
->ctx
.data
.buf
.in
,
634 instr
->ctx
.data
.len
);
636 fsmc_read_buf(host
, instr
->ctx
.data
.buf
.in
,
637 instr
->ctx
.data
.len
);
640 case NAND_OP_DATA_OUT_INSTR
:
641 if (host
->mode
== USE_DMA_ACCESS
)
642 fsmc_write_buf_dma(host
,
643 instr
->ctx
.data
.buf
.out
,
644 instr
->ctx
.data
.len
);
646 fsmc_write_buf(host
, instr
->ctx
.data
.buf
.out
,
647 instr
->ctx
.data
.len
);
650 case NAND_OP_WAITRDY_INSTR
:
651 ret
= nand_soft_waitrdy(chip
,
652 instr
->ctx
.waitrdy
.timeout_ms
);
661 * fsmc_read_page_hwecc
662 * @chip: nand chip info structure
663 * @buf: buffer to store read data
664 * @oob_required: caller expects OOB data read to chip->oob_poi
665 * @page: page number to read
667 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
668 * performed in a strict sequence as follows:
669 * data(512 byte) -> ecc(13 byte)
670 * After this read, fsmc hardware generates and reports error data bits(up to a
673 static int fsmc_read_page_hwecc(struct nand_chip
*chip
, u8
*buf
,
674 int oob_required
, int page
)
676 struct mtd_info
*mtd
= nand_to_mtd(chip
);
677 int i
, j
, s
, stat
, eccsize
= chip
->ecc
.size
;
678 int eccbytes
= chip
->ecc
.bytes
;
679 int eccsteps
= chip
->ecc
.steps
;
681 u8
*ecc_calc
= chip
->ecc
.calc_buf
;
682 u8
*ecc_code
= chip
->ecc
.code_buf
;
683 int off
, len
, ret
, group
= 0;
685 * ecc_oob is intentionally taken as u16. In 16bit devices, we
686 * end up reading 14 bytes (7 words) from oob. The local array is
687 * to maintain word alignment
690 u8
*oob
= (u8
*)&ecc_oob
[0];
691 unsigned int max_bitflips
= 0;
693 for (i
= 0, s
= 0; s
< eccsteps
; s
++, i
+= eccbytes
, p
+= eccsize
) {
694 nand_read_page_op(chip
, page
, s
* eccsize
, NULL
, 0);
695 chip
->ecc
.hwctl(chip
, NAND_ECC_READ
);
696 ret
= nand_read_data_op(chip
, p
, eccsize
, false, false);
700 for (j
= 0; j
< eccbytes
;) {
701 struct mtd_oob_region oobregion
;
703 ret
= mtd_ooblayout_ecc(mtd
, group
++, &oobregion
);
707 off
= oobregion
.offset
;
708 len
= oobregion
.length
;
711 * length is intentionally kept a higher multiple of 2
712 * to read at least 13 bytes even in case of 16 bit NAND
715 if (chip
->options
& NAND_BUSWIDTH_16
)
716 len
= roundup(len
, 2);
718 nand_read_oob_op(chip
, page
, off
, oob
+ j
, len
);
722 memcpy(&ecc_code
[i
], oob
, chip
->ecc
.bytes
);
723 chip
->ecc
.calculate(chip
, p
, &ecc_calc
[i
]);
725 stat
= chip
->ecc
.correct(chip
, p
, &ecc_code
[i
], &ecc_calc
[i
]);
727 mtd
->ecc_stats
.failed
++;
729 mtd
->ecc_stats
.corrected
+= stat
;
730 max_bitflips
= max_t(unsigned int, max_bitflips
, stat
);
738 * fsmc_bch8_correct_data
739 * @mtd: mtd info structure
740 * @dat: buffer of read data
741 * @read_ecc: ecc read from device spare area
742 * @calc_ecc: ecc calculated from read data
744 * calc_ecc is a 104 bit information containing maximum of 8 error
745 * offset information of 13 bits each in 512 bytes of read data.
747 static int fsmc_bch8_correct_data(struct nand_chip
*chip
, u8
*dat
,
748 u8
*read_ecc
, u8
*calc_ecc
)
750 struct fsmc_nand_data
*host
= nand_to_fsmc(chip
);
753 u32 ecc1
, ecc2
, ecc3
, ecc4
;
755 num_err
= (readl_relaxed(host
->regs_va
+ STS
) >> 10) & 0xF;
757 /* no bit flipping */
758 if (likely(num_err
== 0))
761 /* too many errors */
762 if (unlikely(num_err
> 8)) {
764 * This is a temporary erase check. A newly erased page read
765 * would result in an ecc error because the oob data is also
766 * erased to FF and the calculated ecc for an FF data is not
768 * This is a workaround to skip performing correction in case
772 * For every page, each bit written as 0 is counted until these
773 * number of bits are greater than 8 (the maximum correction
774 * capability of FSMC for each 512 + 13 bytes)
777 int bits_ecc
= count_written_bits(read_ecc
, chip
->ecc
.bytes
, 8);
778 int bits_data
= count_written_bits(dat
, chip
->ecc
.size
, 8);
780 if ((bits_ecc
+ bits_data
) <= 8) {
782 memset(dat
, 0xff, chip
->ecc
.size
);
790 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
791 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
793 * calc_ecc is a 104 bit information containing maximum of 8 error
794 * offset information of 13 bits each. calc_ecc is copied into a
795 * u64 array and error offset indexes are populated in err_idx
798 ecc1
= readl_relaxed(host
->regs_va
+ ECC1
);
799 ecc2
= readl_relaxed(host
->regs_va
+ ECC2
);
800 ecc3
= readl_relaxed(host
->regs_va
+ ECC3
);
801 ecc4
= readl_relaxed(host
->regs_va
+ STS
);
803 err_idx
[0] = (ecc1
>> 0) & 0x1FFF;
804 err_idx
[1] = (ecc1
>> 13) & 0x1FFF;
805 err_idx
[2] = (((ecc2
>> 0) & 0x7F) << 6) | ((ecc1
>> 26) & 0x3F);
806 err_idx
[3] = (ecc2
>> 7) & 0x1FFF;
807 err_idx
[4] = (((ecc3
>> 0) & 0x1) << 12) | ((ecc2
>> 20) & 0xFFF);
808 err_idx
[5] = (ecc3
>> 1) & 0x1FFF;
809 err_idx
[6] = (ecc3
>> 14) & 0x1FFF;
810 err_idx
[7] = (((ecc4
>> 16) & 0xFF) << 5) | ((ecc3
>> 27) & 0x1F);
816 if (err_idx
[i
] < chip
->ecc
.size
* 8) {
817 int err
= err_idx
[i
];
819 dat
[err
>> 3] ^= BIT(err
& 7);
826 static bool filter(struct dma_chan
*chan
, void *slave
)
828 chan
->private = slave
;
832 static int fsmc_nand_probe_config_dt(struct platform_device
*pdev
,
833 struct fsmc_nand_data
*host
,
834 struct nand_chip
*nand
)
836 struct device_node
*np
= pdev
->dev
.of_node
;
842 if (!of_property_read_u32(np
, "bank-width", &val
)) {
844 nand
->options
|= NAND_BUSWIDTH_16
;
845 } else if (val
!= 1) {
846 dev_err(&pdev
->dev
, "invalid bank-width %u\n", val
);
851 if (of_get_property(np
, "nand-skip-bbtscan", NULL
))
852 nand
->options
|= NAND_SKIP_BBTSCAN
;
854 host
->dev_timings
= devm_kzalloc(&pdev
->dev
,
855 sizeof(*host
->dev_timings
),
857 if (!host
->dev_timings
)
860 ret
= of_property_read_u8_array(np
, "timings", (u8
*)host
->dev_timings
,
861 sizeof(*host
->dev_timings
));
863 host
->dev_timings
= NULL
;
865 /* Set default NAND bank to 0 */
867 if (!of_property_read_u32(np
, "bank", &val
)) {
869 dev_err(&pdev
->dev
, "invalid bank %u\n", val
);
877 static int fsmc_nand_attach_chip(struct nand_chip
*nand
)
879 struct mtd_info
*mtd
= nand_to_mtd(nand
);
880 struct fsmc_nand_data
*host
= nand_to_fsmc(nand
);
882 if (nand
->ecc
.engine_type
== NAND_ECC_ENGINE_TYPE_INVALID
)
883 nand
->ecc
.engine_type
= NAND_ECC_ENGINE_TYPE_ON_HOST
;
886 nand
->ecc
.size
= 512;
888 if (AMBA_REV_BITS(host
->pid
) >= 8) {
889 nand
->ecc
.read_page
= fsmc_read_page_hwecc
;
890 nand
->ecc
.calculate
= fsmc_read_hwecc_ecc4
;
891 nand
->ecc
.correct
= fsmc_bch8_correct_data
;
892 nand
->ecc
.bytes
= 13;
893 nand
->ecc
.strength
= 8;
896 if (AMBA_REV_BITS(host
->pid
) >= 8) {
897 switch (mtd
->oobsize
) {
906 "No oob scheme defined for oobsize %d\n",
911 mtd_set_ooblayout(mtd
, &fsmc_ecc4_ooblayout_ops
);
916 switch (nand
->ecc
.engine_type
) {
917 case NAND_ECC_ENGINE_TYPE_ON_HOST
:
918 dev_info(host
->dev
, "Using 1-bit HW ECC scheme\n");
919 nand
->ecc
.calculate
= fsmc_read_hwecc_ecc1
;
920 nand
->ecc
.correct
= rawnand_sw_hamming_correct
;
921 nand
->ecc
.hwctl
= fsmc_enable_hwecc
;
923 nand
->ecc
.strength
= 1;
924 nand
->ecc
.options
|= NAND_ECC_SOFT_HAMMING_SM_ORDER
;
927 case NAND_ECC_ENGINE_TYPE_SOFT
:
928 if (nand
->ecc
.algo
== NAND_ECC_ALGO_BCH
) {
930 "Using 4-bit SW BCH ECC scheme\n");
934 case NAND_ECC_ENGINE_TYPE_ON_DIE
:
938 dev_err(host
->dev
, "Unsupported ECC mode!\n");
943 * Don't set layout for BCH4 SW ECC. This will be
944 * generated later during BCH initialization.
946 if (nand
->ecc
.engine_type
== NAND_ECC_ENGINE_TYPE_ON_HOST
) {
947 switch (mtd
->oobsize
) {
951 mtd_set_ooblayout(mtd
,
952 &fsmc_ecc1_ooblayout_ops
);
956 "No oob scheme defined for oobsize %d\n",
965 static const struct nand_controller_ops fsmc_nand_controller_ops
= {
966 .attach_chip
= fsmc_nand_attach_chip
,
967 .exec_op
= fsmc_exec_op
,
968 .setup_interface
= fsmc_setup_interface
,
972 * fsmc_nand_disable() - Disables the NAND bank
973 * @host: The instance to disable
975 static void fsmc_nand_disable(struct fsmc_nand_data
*host
)
979 val
= readl(host
->regs_va
+ FSMC_PC
);
981 writel(val
, host
->regs_va
+ FSMC_PC
);
985 * fsmc_nand_probe - Probe function
986 * @pdev: platform device structure
988 static int __init
fsmc_nand_probe(struct platform_device
*pdev
)
990 struct fsmc_nand_data
*host
;
991 struct mtd_info
*mtd
;
992 struct nand_chip
*nand
;
993 struct resource
*res
;
1000 /* Allocate memory for the device structure (and zero it) */
1001 host
= devm_kzalloc(&pdev
->dev
, sizeof(*host
), GFP_KERNEL
);
1007 ret
= fsmc_nand_probe_config_dt(pdev
, host
, nand
);
1011 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "nand_data");
1012 host
->data_va
= devm_ioremap_resource(&pdev
->dev
, res
);
1013 if (IS_ERR(host
->data_va
))
1014 return PTR_ERR(host
->data_va
);
1016 host
->data_pa
= (dma_addr_t
)res
->start
;
1018 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "nand_addr");
1019 host
->addr_va
= devm_ioremap_resource(&pdev
->dev
, res
);
1020 if (IS_ERR(host
->addr_va
))
1021 return PTR_ERR(host
->addr_va
);
1023 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "nand_cmd");
1024 host
->cmd_va
= devm_ioremap_resource(&pdev
->dev
, res
);
1025 if (IS_ERR(host
->cmd_va
))
1026 return PTR_ERR(host
->cmd_va
);
1028 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "fsmc_regs");
1029 base
= devm_ioremap_resource(&pdev
->dev
, res
);
1031 return PTR_ERR(base
);
1033 host
->regs_va
= base
+ FSMC_NOR_REG_SIZE
+
1034 (host
->bank
* FSMC_NAND_BANK_SZ
);
1036 host
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
1037 if (IS_ERR(host
->clk
)) {
1038 dev_err(&pdev
->dev
, "failed to fetch block clock\n");
1039 return PTR_ERR(host
->clk
);
1042 ret
= clk_prepare_enable(host
->clk
);
1047 * This device ID is actually a common AMBA ID as used on the
1048 * AMBA PrimeCell bus. However it is not a PrimeCell.
1050 for (pid
= 0, i
= 0; i
< 4; i
++)
1051 pid
|= (readl(base
+ resource_size(res
) - 0x20 + 4 * i
) &
1056 dev_info(&pdev
->dev
,
1057 "FSMC device partno %03x, manufacturer %02x, revision %02x, config %02x\n",
1058 AMBA_PART_BITS(pid
), AMBA_MANF_BITS(pid
),
1059 AMBA_REV_BITS(pid
), AMBA_CONFIG_BITS(pid
));
1061 host
->dev
= &pdev
->dev
;
1063 if (host
->mode
== USE_DMA_ACCESS
)
1064 init_completion(&host
->dma_access_complete
);
1066 /* Link all private pointers */
1067 mtd
= nand_to_mtd(&host
->nand
);
1068 nand_set_flash_node(nand
, pdev
->dev
.of_node
);
1070 mtd
->dev
.parent
= &pdev
->dev
;
1072 nand
->badblockbits
= 7;
1074 if (host
->mode
== USE_DMA_ACCESS
) {
1076 dma_cap_set(DMA_MEMCPY
, mask
);
1077 host
->read_dma_chan
= dma_request_channel(mask
, filter
, NULL
);
1078 if (!host
->read_dma_chan
) {
1079 dev_err(&pdev
->dev
, "Unable to get read dma channel\n");
1082 host
->write_dma_chan
= dma_request_channel(mask
, filter
, NULL
);
1083 if (!host
->write_dma_chan
) {
1084 dev_err(&pdev
->dev
, "Unable to get write dma channel\n");
1085 goto release_dma_read_chan
;
1089 if (host
->dev_timings
) {
1090 fsmc_nand_setup(host
, host
->dev_timings
);
1091 nand
->options
|= NAND_KEEP_TIMINGS
;
1094 nand_controller_init(&host
->base
);
1095 host
->base
.ops
= &fsmc_nand_controller_ops
;
1096 nand
->controller
= &host
->base
;
1099 * Scan to find existence of the device
1101 ret
= nand_scan(nand
, 1);
1103 goto release_dma_write_chan
;
1106 ret
= mtd_device_register(mtd
, NULL
, 0);
1110 platform_set_drvdata(pdev
, host
);
1111 dev_info(&pdev
->dev
, "FSMC NAND driver registration successful\n");
1117 release_dma_write_chan
:
1118 if (host
->mode
== USE_DMA_ACCESS
)
1119 dma_release_channel(host
->write_dma_chan
);
1120 release_dma_read_chan
:
1121 if (host
->mode
== USE_DMA_ACCESS
)
1122 dma_release_channel(host
->read_dma_chan
);
1124 fsmc_nand_disable(host
);
1125 clk_disable_unprepare(host
->clk
);
1133 static int fsmc_nand_remove(struct platform_device
*pdev
)
1135 struct fsmc_nand_data
*host
= platform_get_drvdata(pdev
);
1138 struct nand_chip
*chip
= &host
->nand
;
1141 ret
= mtd_device_unregister(nand_to_mtd(chip
));
1144 fsmc_nand_disable(host
);
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
);
1156 #ifdef CONFIG_PM_SLEEP
1157 static int fsmc_nand_suspend(struct device
*dev
)
1159 struct fsmc_nand_data
*host
= dev_get_drvdata(dev
);
1162 clk_disable_unprepare(host
->clk
);
1167 static int fsmc_nand_resume(struct device
*dev
)
1169 struct fsmc_nand_data
*host
= dev_get_drvdata(dev
);
1172 clk_prepare_enable(host
->clk
);
1173 if (host
->dev_timings
)
1174 fsmc_nand_setup(host
, host
->dev_timings
);
1175 nand_reset(&host
->nand
, 0);
1182 static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops
, fsmc_nand_suspend
, fsmc_nand_resume
);
1184 static const struct of_device_id fsmc_nand_id_table
[] = {
1185 { .compatible
= "st,spear600-fsmc-nand" },
1186 { .compatible
= "stericsson,fsmc-nand" },
1189 MODULE_DEVICE_TABLE(of
, fsmc_nand_id_table
);
1191 static struct platform_driver fsmc_nand_driver
= {
1192 .remove
= fsmc_nand_remove
,
1194 .name
= "fsmc-nand",
1195 .of_match_table
= fsmc_nand_id_table
,
1196 .pm
= &fsmc_nand_pm_ops
,
1200 module_platform_driver_probe(fsmc_nand_driver
, fsmc_nand_probe
);
1202 MODULE_LICENSE("GPL v2");
1203 MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
1204 MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");