2 * Copyright 2009-2015 Freescale Semiconductor, Inc. and others
4 * Description: MPC5125, VF610, MCF54418 and Kinetis K70 Nand driver.
5 * Jason ported to M54418TWR and MVFA5 (VF610).
6 * Authors: Stefan Agner <stefan.agner@toradex.com>
7 * Bill Pringlemeir <bpringlemeir@nbsps.com>
8 * Shaohui Xie <b21989@freescale.com>
9 * Jason Jin <Jason.jin@freescale.com>
11 * Based on original driver mpc5121_nfc.c.
13 * This is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
19 * - Untested on MPC5125 and M54418.
20 * - DMA and pipelining not used.
22 * - HW ECC: Only 2K page with 64+ OOB.
23 * - HW ECC: Only 24 and 32-bit error correction implemented.
26 #include <linux/module.h>
27 #include <linux/bitops.h>
28 #include <linux/clk.h>
29 #include <linux/delay.h>
30 #include <linux/init.h>
31 #include <linux/interrupt.h>
33 #include <linux/mtd/mtd.h>
34 #include <linux/mtd/nand.h>
35 #include <linux/mtd/partitions.h>
36 #include <linux/of_mtd.h>
37 #include <linux/of_device.h>
38 #include <linux/pinctrl/consumer.h>
39 #include <linux/platform_device.h>
40 #include <linux/slab.h>
42 #define DRV_NAME "vf610_nfc"
44 /* Register Offsets */
45 #define NFC_FLASH_CMD1 0x3F00
46 #define NFC_FLASH_CMD2 0x3F04
47 #define NFC_COL_ADDR 0x3F08
48 #define NFC_ROW_ADDR 0x3F0c
49 #define NFC_ROW_ADDR_INC 0x3F14
50 #define NFC_FLASH_STATUS1 0x3F18
51 #define NFC_FLASH_STATUS2 0x3F1c
52 #define NFC_CACHE_SWAP 0x3F28
53 #define NFC_SECTOR_SIZE 0x3F2c
54 #define NFC_FLASH_CONFIG 0x3F30
55 #define NFC_IRQ_STATUS 0x3F38
57 /* Addresses for NFC MAIN RAM BUFFER areas */
58 #define NFC_MAIN_AREA(n) ((n) * 0x1000)
60 #define PAGE_2K 0x0800
62 #define OOB_MAX 0x0100
65 * NFC_CMD2[CODE] values. See section:
66 * - 31.4.7 Flash Command Code Description, Vybrid manual
67 * - 23.8.6 Flash Command Sequencer, MPC5125 manual
69 * Briefly these are bitmasks of controller cycles.
71 #define READ_PAGE_CMD_CODE 0x7EE0
72 #define READ_ONFI_PARAM_CMD_CODE 0x4860
73 #define PROGRAM_PAGE_CMD_CODE 0x7FC0
74 #define ERASE_CMD_CODE 0x4EC0
75 #define READ_ID_CMD_CODE 0x4804
76 #define RESET_CMD_CODE 0x4040
77 #define STATUS_READ_CMD_CODE 0x4068
79 /* NFC ECC mode define */
84 /*** Register Mask and bit definitions */
86 /* NFC_FLASH_CMD1 Field */
87 #define CMD_BYTE2_MASK 0xFF000000
88 #define CMD_BYTE2_SHIFT 24
90 /* NFC_FLASH_CM2 Field */
91 #define CMD_BYTE1_MASK 0xFF000000
92 #define CMD_BYTE1_SHIFT 24
93 #define CMD_CODE_MASK 0x00FFFF00
94 #define CMD_CODE_SHIFT 8
95 #define BUFNO_MASK 0x00000006
97 #define START_BIT BIT(0)
99 /* NFC_COL_ADDR Field */
100 #define COL_ADDR_MASK 0x0000FFFF
101 #define COL_ADDR_SHIFT 0
103 /* NFC_ROW_ADDR Field */
104 #define ROW_ADDR_MASK 0x00FFFFFF
105 #define ROW_ADDR_SHIFT 0
106 #define ROW_ADDR_CHIP_SEL_RB_MASK 0xF0000000
107 #define ROW_ADDR_CHIP_SEL_RB_SHIFT 28
108 #define ROW_ADDR_CHIP_SEL_MASK 0x0F000000
109 #define ROW_ADDR_CHIP_SEL_SHIFT 24
111 /* NFC_FLASH_STATUS2 Field */
112 #define STATUS_BYTE1_MASK 0x000000FF
114 /* NFC_FLASH_CONFIG Field */
115 #define CONFIG_ECC_SRAM_ADDR_MASK 0x7FC00000
116 #define CONFIG_ECC_SRAM_ADDR_SHIFT 22
117 #define CONFIG_ECC_SRAM_REQ_BIT BIT(21)
118 #define CONFIG_DMA_REQ_BIT BIT(20)
119 #define CONFIG_ECC_MODE_MASK 0x000E0000
120 #define CONFIG_ECC_MODE_SHIFT 17
121 #define CONFIG_FAST_FLASH_BIT BIT(16)
122 #define CONFIG_16BIT BIT(7)
123 #define CONFIG_BOOT_MODE_BIT BIT(6)
124 #define CONFIG_ADDR_AUTO_INCR_BIT BIT(5)
125 #define CONFIG_BUFNO_AUTO_INCR_BIT BIT(4)
126 #define CONFIG_PAGE_CNT_MASK 0xF
127 #define CONFIG_PAGE_CNT_SHIFT 0
129 /* NFC_IRQ_STATUS Field */
130 #define IDLE_IRQ_BIT BIT(29)
131 #define IDLE_EN_BIT BIT(20)
132 #define CMD_DONE_CLEAR_BIT BIT(18)
133 #define IDLE_CLEAR_BIT BIT(17)
136 * ECC status - seems to consume 8 bytes (double word). The documented
137 * status byte is located in the lowest byte of the second word (which is
138 * the 4th or 7th byte depending on endianness).
139 * Calculate an offset to store the ECC status at the end of the buffer.
141 #define ECC_SRAM_ADDR (PAGE_2K + OOB_MAX - 8)
143 #define ECC_STATUS 0x4
144 #define ECC_STATUS_MASK 0x80
145 #define ECC_STATUS_ERR_COUNT 0x3F
147 enum vf610_nfc_alt_buf
{
154 enum vf610_nfc_variant
{
160 struct nand_chip chip
;
163 struct completion cmd_done
;
166 /* Status and ID are in alternate locations. */
167 enum vf610_nfc_alt_buf alt_buf
;
168 enum vf610_nfc_variant variant
;
174 #define mtd_to_nfc(_mtd) container_of(_mtd, struct vf610_nfc, mtd)
176 static struct nand_ecclayout vf610_nfc_ecc45
= {
178 .eccpos
= {19, 20, 21, 22, 23,
179 24, 25, 26, 27, 28, 29, 30, 31,
180 32, 33, 34, 35, 36, 37, 38, 39,
181 40, 41, 42, 43, 44, 45, 46, 47,
182 48, 49, 50, 51, 52, 53, 54, 55,
183 56, 57, 58, 59, 60, 61, 62, 63},
189 static struct nand_ecclayout vf610_nfc_ecc60
= {
191 .eccpos
= { 4, 5, 6, 7, 8, 9, 10, 11,
192 12, 13, 14, 15, 16, 17, 18, 19,
193 20, 21, 22, 23, 24, 25, 26, 27,
194 28, 29, 30, 31, 32, 33, 34, 35,
195 36, 37, 38, 39, 40, 41, 42, 43,
196 44, 45, 46, 47, 48, 49, 50, 51,
197 52, 53, 54, 55, 56, 57, 58, 59,
204 static inline u32
vf610_nfc_read(struct vf610_nfc
*nfc
, uint reg
)
206 return readl(nfc
->regs
+ reg
);
209 static inline void vf610_nfc_write(struct vf610_nfc
*nfc
, uint reg
, u32 val
)
211 writel(val
, nfc
->regs
+ reg
);
214 static inline void vf610_nfc_set(struct vf610_nfc
*nfc
, uint reg
, u32 bits
)
216 vf610_nfc_write(nfc
, reg
, vf610_nfc_read(nfc
, reg
) | bits
);
219 static inline void vf610_nfc_clear(struct vf610_nfc
*nfc
, uint reg
, u32 bits
)
221 vf610_nfc_write(nfc
, reg
, vf610_nfc_read(nfc
, reg
) & ~bits
);
224 static inline void vf610_nfc_set_field(struct vf610_nfc
*nfc
, u32 reg
,
225 u32 mask
, u32 shift
, u32 val
)
227 vf610_nfc_write(nfc
, reg
,
228 (vf610_nfc_read(nfc
, reg
) & (~mask
)) | val
<< shift
);
231 static inline void vf610_nfc_memcpy(void *dst
, const void __iomem
*src
,
235 * Use this accessor for the internal SRAM buffers. On the ARM
236 * Freescale Vybrid SoC it's known that the driver can treat
237 * the SRAM buffer as if it's memory. Other platform might need
238 * to treat the buffers differently.
240 * For the time being, use memcpy
245 /* Clear flags for upcoming command */
246 static inline void vf610_nfc_clear_status(struct vf610_nfc
*nfc
)
248 u32 tmp
= vf610_nfc_read(nfc
, NFC_IRQ_STATUS
);
250 tmp
|= CMD_DONE_CLEAR_BIT
| IDLE_CLEAR_BIT
;
251 vf610_nfc_write(nfc
, NFC_IRQ_STATUS
, tmp
);
254 static void vf610_nfc_done(struct vf610_nfc
*nfc
)
256 unsigned long timeout
= msecs_to_jiffies(100);
259 * Barrier is needed after this write. This write need
260 * to be done before reading the next register the first
262 * vf610_nfc_set implicates such a barrier by using writel
263 * to write to the register.
265 vf610_nfc_set(nfc
, NFC_IRQ_STATUS
, IDLE_EN_BIT
);
266 vf610_nfc_set(nfc
, NFC_FLASH_CMD2
, START_BIT
);
268 if (!wait_for_completion_timeout(&nfc
->cmd_done
, timeout
))
269 dev_warn(nfc
->dev
, "Timeout while waiting for BUSY.\n");
271 vf610_nfc_clear_status(nfc
);
274 static u8
vf610_nfc_get_id(struct vf610_nfc
*nfc
, int col
)
279 flash_id
= vf610_nfc_read(nfc
, NFC_FLASH_STATUS1
);
280 flash_id
>>= (3 - col
) * 8;
282 flash_id
= vf610_nfc_read(nfc
, NFC_FLASH_STATUS2
);
286 return flash_id
& 0xff;
289 static u8
vf610_nfc_get_status(struct vf610_nfc
*nfc
)
291 return vf610_nfc_read(nfc
, NFC_FLASH_STATUS2
) & STATUS_BYTE1_MASK
;
294 static void vf610_nfc_send_command(struct vf610_nfc
*nfc
, u32 cmd_byte1
,
299 vf610_nfc_clear_status(nfc
);
301 tmp
= vf610_nfc_read(nfc
, NFC_FLASH_CMD2
);
302 tmp
&= ~(CMD_BYTE1_MASK
| CMD_CODE_MASK
| BUFNO_MASK
);
303 tmp
|= cmd_byte1
<< CMD_BYTE1_SHIFT
;
304 tmp
|= cmd_code
<< CMD_CODE_SHIFT
;
305 vf610_nfc_write(nfc
, NFC_FLASH_CMD2
, tmp
);
308 static void vf610_nfc_send_commands(struct vf610_nfc
*nfc
, u32 cmd_byte1
,
309 u32 cmd_byte2
, u32 cmd_code
)
313 vf610_nfc_send_command(nfc
, cmd_byte1
, cmd_code
);
315 tmp
= vf610_nfc_read(nfc
, NFC_FLASH_CMD1
);
316 tmp
&= ~CMD_BYTE2_MASK
;
317 tmp
|= cmd_byte2
<< CMD_BYTE2_SHIFT
;
318 vf610_nfc_write(nfc
, NFC_FLASH_CMD1
, tmp
);
321 static irqreturn_t
vf610_nfc_irq(int irq
, void *data
)
323 struct mtd_info
*mtd
= data
;
324 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
326 vf610_nfc_clear(nfc
, NFC_IRQ_STATUS
, IDLE_EN_BIT
);
327 complete(&nfc
->cmd_done
);
332 static void vf610_nfc_addr_cycle(struct vf610_nfc
*nfc
, int column
, int page
)
335 if (nfc
->chip
.options
& NAND_BUSWIDTH_16
)
337 vf610_nfc_set_field(nfc
, NFC_COL_ADDR
, COL_ADDR_MASK
,
338 COL_ADDR_SHIFT
, column
);
341 vf610_nfc_set_field(nfc
, NFC_ROW_ADDR
, ROW_ADDR_MASK
,
342 ROW_ADDR_SHIFT
, page
);
345 static inline void vf610_nfc_ecc_mode(struct vf610_nfc
*nfc
, int ecc_mode
)
347 vf610_nfc_set_field(nfc
, NFC_FLASH_CONFIG
,
348 CONFIG_ECC_MODE_MASK
,
349 CONFIG_ECC_MODE_SHIFT
, ecc_mode
);
352 static inline void vf610_nfc_transfer_size(struct vf610_nfc
*nfc
, int size
)
354 vf610_nfc_write(nfc
, NFC_SECTOR_SIZE
, size
);
357 static void vf610_nfc_command(struct mtd_info
*mtd
, unsigned command
,
358 int column
, int page
)
360 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
361 int trfr_sz
= nfc
->chip
.options
& NAND_BUSWIDTH_16
? 1 : 0;
363 nfc
->buf_offset
= max(column
, 0);
364 nfc
->alt_buf
= ALT_BUF_DATA
;
368 /* Use valid column/page from preread... */
369 vf610_nfc_addr_cycle(nfc
, column
, page
);
373 * SEQIN => data => PAGEPROG sequence is done by the controller
374 * hence we do not need to issue the command here...
377 case NAND_CMD_PAGEPROG
:
378 trfr_sz
+= nfc
->write_sz
;
379 vf610_nfc_transfer_size(nfc
, trfr_sz
);
380 vf610_nfc_send_commands(nfc
, NAND_CMD_SEQIN
,
381 command
, PROGRAM_PAGE_CMD_CODE
);
383 vf610_nfc_ecc_mode(nfc
, nfc
->ecc_mode
);
385 vf610_nfc_ecc_mode(nfc
, ECC_BYPASS
);
389 vf610_nfc_transfer_size(nfc
, 0);
390 vf610_nfc_send_command(nfc
, command
, RESET_CMD_CODE
);
393 case NAND_CMD_READOOB
:
394 trfr_sz
+= mtd
->oobsize
;
395 column
= mtd
->writesize
;
396 vf610_nfc_transfer_size(nfc
, trfr_sz
);
397 vf610_nfc_send_commands(nfc
, NAND_CMD_READ0
,
398 NAND_CMD_READSTART
, READ_PAGE_CMD_CODE
);
399 vf610_nfc_addr_cycle(nfc
, column
, page
);
400 vf610_nfc_ecc_mode(nfc
, ECC_BYPASS
);
404 trfr_sz
+= mtd
->writesize
+ mtd
->oobsize
;
405 vf610_nfc_transfer_size(nfc
, trfr_sz
);
406 vf610_nfc_send_commands(nfc
, NAND_CMD_READ0
,
407 NAND_CMD_READSTART
, READ_PAGE_CMD_CODE
);
408 vf610_nfc_addr_cycle(nfc
, column
, page
);
409 vf610_nfc_ecc_mode(nfc
, nfc
->ecc_mode
);
413 nfc
->alt_buf
= ALT_BUF_ONFI
;
414 trfr_sz
= 3 * sizeof(struct nand_onfi_params
);
415 vf610_nfc_transfer_size(nfc
, trfr_sz
);
416 vf610_nfc_send_command(nfc
, command
, READ_ONFI_PARAM_CMD_CODE
);
417 vf610_nfc_addr_cycle(nfc
, -1, column
);
418 vf610_nfc_ecc_mode(nfc
, ECC_BYPASS
);
421 case NAND_CMD_ERASE1
:
422 vf610_nfc_transfer_size(nfc
, 0);
423 vf610_nfc_send_commands(nfc
, command
,
424 NAND_CMD_ERASE2
, ERASE_CMD_CODE
);
425 vf610_nfc_addr_cycle(nfc
, column
, page
);
428 case NAND_CMD_READID
:
429 nfc
->alt_buf
= ALT_BUF_ID
;
431 vf610_nfc_transfer_size(nfc
, 0);
432 vf610_nfc_send_command(nfc
, command
, READ_ID_CMD_CODE
);
433 vf610_nfc_addr_cycle(nfc
, -1, column
);
436 case NAND_CMD_STATUS
:
437 nfc
->alt_buf
= ALT_BUF_STAT
;
438 vf610_nfc_transfer_size(nfc
, 0);
439 vf610_nfc_send_command(nfc
, command
, STATUS_READ_CMD_CODE
);
447 nfc
->use_hw_ecc
= false;
451 static void vf610_nfc_read_buf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
453 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
454 uint c
= nfc
->buf_offset
;
456 /* Alternate buffers are only supported through read_byte */
457 WARN_ON(nfc
->alt_buf
);
459 vf610_nfc_memcpy(buf
, nfc
->regs
+ NFC_MAIN_AREA(0) + c
, len
);
461 nfc
->buf_offset
+= len
;
464 static void vf610_nfc_write_buf(struct mtd_info
*mtd
, const uint8_t *buf
,
467 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
468 uint c
= nfc
->buf_offset
;
471 l
= min_t(uint
, len
, mtd
->writesize
+ mtd
->oobsize
- c
);
472 vf610_nfc_memcpy(nfc
->regs
+ NFC_MAIN_AREA(0) + c
, buf
, l
);
475 nfc
->buf_offset
+= l
;
478 static uint8_t vf610_nfc_read_byte(struct mtd_info
*mtd
)
480 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
482 uint c
= nfc
->buf_offset
;
484 switch (nfc
->alt_buf
) {
486 tmp
= vf610_nfc_get_id(nfc
, c
);
489 tmp
= vf610_nfc_get_status(nfc
);
491 #ifdef __LITTLE_ENDIAN
493 /* Reverse byte since the controller uses big endianness */
494 c
= nfc
->buf_offset
^ 0x3;
498 tmp
= *((u8
*)(nfc
->regs
+ NFC_MAIN_AREA(0) + c
));
505 static u16
vf610_nfc_read_word(struct mtd_info
*mtd
)
509 vf610_nfc_read_buf(mtd
, (u_char
*)&tmp
, sizeof(tmp
));
513 /* If not provided, upper layers apply a fixed delay. */
514 static int vf610_nfc_dev_ready(struct mtd_info
*mtd
)
516 /* NFC handles R/B internally; always ready. */
521 * This function supports Vybrid only (MPC5125 would have full RB and four CS)
523 static void vf610_nfc_select_chip(struct mtd_info
*mtd
, int chip
)
525 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
526 u32 tmp
= vf610_nfc_read(nfc
, NFC_ROW_ADDR
);
528 /* Vybrid only (MPC5125 would have full RB and four CS) */
529 if (nfc
->variant
!= NFC_VFC610
)
532 tmp
&= ~(ROW_ADDR_CHIP_SEL_RB_MASK
| ROW_ADDR_CHIP_SEL_MASK
);
535 tmp
|= 1 << ROW_ADDR_CHIP_SEL_RB_SHIFT
;
536 tmp
|= BIT(chip
) << ROW_ADDR_CHIP_SEL_SHIFT
;
539 vf610_nfc_write(nfc
, NFC_ROW_ADDR
, tmp
);
542 /* Count the number of 0's in buff up to max_bits */
543 static inline int count_written_bits(uint8_t *buff
, int size
, int max_bits
)
545 uint32_t *buff32
= (uint32_t *)buff
;
546 int k
, written_bits
= 0;
548 for (k
= 0; k
< (size
/ 4); k
++) {
549 written_bits
+= hweight32(~buff32
[k
]);
550 if (unlikely(written_bits
> max_bits
))
557 static inline int vf610_nfc_correct_data(struct mtd_info
*mtd
, uint8_t *dat
,
558 uint8_t *oob
, int page
)
560 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
561 u32 ecc_status_off
= NFC_MAIN_AREA(0) + ECC_SRAM_ADDR
+ ECC_STATUS
;
564 int flips_threshold
= nfc
->chip
.ecc
.strength
/ 2;
566 ecc_status
= vf610_nfc_read(nfc
, ecc_status_off
) & 0xff;
567 ecc_count
= ecc_status
& ECC_STATUS_ERR_COUNT
;
569 if (!(ecc_status
& ECC_STATUS_MASK
))
572 /* Read OOB without ECC unit enabled */
573 vf610_nfc_command(mtd
, NAND_CMD_READOOB
, 0, page
);
574 vf610_nfc_read_buf(mtd
, oob
, mtd
->oobsize
);
577 * On an erased page, bit count (including OOB) should be zero or
578 * at least less then half of the ECC strength.
580 return nand_check_erased_ecc_chunk(dat
, nfc
->chip
.ecc
.size
, oob
,
581 mtd
->oobsize
, NULL
, 0,
585 static int vf610_nfc_read_page(struct mtd_info
*mtd
, struct nand_chip
*chip
,
586 uint8_t *buf
, int oob_required
, int page
)
588 int eccsize
= chip
->ecc
.size
;
591 vf610_nfc_read_buf(mtd
, buf
, eccsize
);
593 vf610_nfc_read_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
595 stat
= vf610_nfc_correct_data(mtd
, buf
, chip
->oob_poi
, page
);
598 mtd
->ecc_stats
.failed
++;
601 mtd
->ecc_stats
.corrected
+= stat
;
606 static int vf610_nfc_write_page(struct mtd_info
*mtd
, struct nand_chip
*chip
,
607 const uint8_t *buf
, int oob_required
, int page
)
609 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
611 vf610_nfc_write_buf(mtd
, buf
, mtd
->writesize
);
613 vf610_nfc_write_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
615 /* Always write whole page including OOB due to HW ECC */
616 nfc
->use_hw_ecc
= true;
617 nfc
->write_sz
= mtd
->writesize
+ mtd
->oobsize
;
622 static const struct of_device_id vf610_nfc_dt_ids
[] = {
623 { .compatible
= "fsl,vf610-nfc", .data
= (void *)NFC_VFC610
},
626 MODULE_DEVICE_TABLE(of
, vf610_nfc_dt_ids
);
628 static void vf610_nfc_preinit_controller(struct vf610_nfc
*nfc
)
630 vf610_nfc_clear(nfc
, NFC_FLASH_CONFIG
, CONFIG_16BIT
);
631 vf610_nfc_clear(nfc
, NFC_FLASH_CONFIG
, CONFIG_ADDR_AUTO_INCR_BIT
);
632 vf610_nfc_clear(nfc
, NFC_FLASH_CONFIG
, CONFIG_BUFNO_AUTO_INCR_BIT
);
633 vf610_nfc_clear(nfc
, NFC_FLASH_CONFIG
, CONFIG_BOOT_MODE_BIT
);
634 vf610_nfc_clear(nfc
, NFC_FLASH_CONFIG
, CONFIG_DMA_REQ_BIT
);
635 vf610_nfc_set(nfc
, NFC_FLASH_CONFIG
, CONFIG_FAST_FLASH_BIT
);
637 /* Disable virtual pages, only one elementary transfer unit */
638 vf610_nfc_set_field(nfc
, NFC_FLASH_CONFIG
, CONFIG_PAGE_CNT_MASK
,
639 CONFIG_PAGE_CNT_SHIFT
, 1);
642 static void vf610_nfc_init_controller(struct vf610_nfc
*nfc
)
644 if (nfc
->chip
.options
& NAND_BUSWIDTH_16
)
645 vf610_nfc_set(nfc
, NFC_FLASH_CONFIG
, CONFIG_16BIT
);
647 vf610_nfc_clear(nfc
, NFC_FLASH_CONFIG
, CONFIG_16BIT
);
649 if (nfc
->chip
.ecc
.mode
== NAND_ECC_HW
) {
650 /* Set ECC status offset in SRAM */
651 vf610_nfc_set_field(nfc
, NFC_FLASH_CONFIG
,
652 CONFIG_ECC_SRAM_ADDR_MASK
,
653 CONFIG_ECC_SRAM_ADDR_SHIFT
,
656 /* Enable ECC status in SRAM */
657 vf610_nfc_set(nfc
, NFC_FLASH_CONFIG
, CONFIG_ECC_SRAM_REQ_BIT
);
661 static int vf610_nfc_probe(struct platform_device
*pdev
)
663 struct vf610_nfc
*nfc
;
664 struct resource
*res
;
665 struct mtd_info
*mtd
;
666 struct nand_chip
*chip
;
667 struct device_node
*child
;
668 const struct of_device_id
*of_id
;
672 nfc
= devm_kzalloc(&pdev
->dev
, sizeof(*nfc
), GFP_KERNEL
);
676 nfc
->dev
= &pdev
->dev
;
681 mtd
->owner
= THIS_MODULE
;
682 mtd
->dev
.parent
= nfc
->dev
;
683 mtd
->name
= DRV_NAME
;
685 irq
= platform_get_irq(pdev
, 0);
689 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
690 nfc
->regs
= devm_ioremap_resource(nfc
->dev
, res
);
691 if (IS_ERR(nfc
->regs
))
692 return PTR_ERR(nfc
->regs
);
694 nfc
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
695 if (IS_ERR(nfc
->clk
))
696 return PTR_ERR(nfc
->clk
);
698 err
= clk_prepare_enable(nfc
->clk
);
700 dev_err(nfc
->dev
, "Unable to enable clock!\n");
704 of_id
= of_match_device(vf610_nfc_dt_ids
, &pdev
->dev
);
705 nfc
->variant
= (enum vf610_nfc_variant
)of_id
->data
;
707 for_each_available_child_of_node(nfc
->dev
->of_node
, child
) {
708 if (of_device_is_compatible(child
, "fsl,vf610-nfc-nandcs")) {
710 if (chip
->flash_node
) {
712 "Only one NAND chip supported!\n");
717 chip
->flash_node
= child
;
721 if (!chip
->flash_node
) {
722 dev_err(nfc
->dev
, "NAND chip sub-node missing!\n");
727 chip
->dev_ready
= vf610_nfc_dev_ready
;
728 chip
->cmdfunc
= vf610_nfc_command
;
729 chip
->read_byte
= vf610_nfc_read_byte
;
730 chip
->read_word
= vf610_nfc_read_word
;
731 chip
->read_buf
= vf610_nfc_read_buf
;
732 chip
->write_buf
= vf610_nfc_write_buf
;
733 chip
->select_chip
= vf610_nfc_select_chip
;
735 chip
->options
|= NAND_NO_SUBPAGE_WRITE
;
737 init_completion(&nfc
->cmd_done
);
739 err
= devm_request_irq(nfc
->dev
, irq
, vf610_nfc_irq
, 0, DRV_NAME
, mtd
);
741 dev_err(nfc
->dev
, "Error requesting IRQ!\n");
745 vf610_nfc_preinit_controller(nfc
);
747 /* first scan to find the device and get the page size */
748 if (nand_scan_ident(mtd
, 1, NULL
)) {
753 vf610_nfc_init_controller(nfc
);
755 /* Bad block options. */
756 if (chip
->bbt_options
& NAND_BBT_USE_FLASH
)
757 chip
->bbt_options
|= NAND_BBT_NO_OOB
;
759 /* Single buffer only, max 256 OOB minus ECC status */
760 if (mtd
->writesize
+ mtd
->oobsize
> PAGE_2K
+ OOB_MAX
- 8) {
761 dev_err(nfc
->dev
, "Unsupported flash page size\n");
766 if (chip
->ecc
.mode
== NAND_ECC_HW
) {
767 if (mtd
->writesize
!= PAGE_2K
&& mtd
->oobsize
< 64) {
768 dev_err(nfc
->dev
, "Unsupported flash with hwecc\n");
773 if (chip
->ecc
.size
!= mtd
->writesize
) {
774 dev_err(nfc
->dev
, "Step size needs to be page size\n");
779 /* Only 64 byte ECC layouts known */
780 if (mtd
->oobsize
> 64)
783 if (chip
->ecc
.strength
== 32) {
784 nfc
->ecc_mode
= ECC_60_BYTE
;
785 chip
->ecc
.bytes
= 60;
786 chip
->ecc
.layout
= &vf610_nfc_ecc60
;
787 } else if (chip
->ecc
.strength
== 24) {
788 nfc
->ecc_mode
= ECC_45_BYTE
;
789 chip
->ecc
.bytes
= 45;
790 chip
->ecc
.layout
= &vf610_nfc_ecc45
;
792 dev_err(nfc
->dev
, "Unsupported ECC strength\n");
797 /* propagate ecc.layout to mtd_info */
798 mtd
->ecclayout
= chip
->ecc
.layout
;
799 chip
->ecc
.read_page
= vf610_nfc_read_page
;
800 chip
->ecc
.write_page
= vf610_nfc_write_page
;
802 chip
->ecc
.size
= PAGE_2K
;
805 /* second phase scan */
806 if (nand_scan_tail(mtd
)) {
811 platform_set_drvdata(pdev
, mtd
);
813 /* Register device in MTD */
814 return mtd_device_parse_register(mtd
, NULL
,
815 &(struct mtd_part_parser_data
){
816 .of_node
= chip
->flash_node
,
821 of_node_put(chip
->flash_node
);
823 clk_disable_unprepare(nfc
->clk
);
827 static int vf610_nfc_remove(struct platform_device
*pdev
)
829 struct mtd_info
*mtd
= platform_get_drvdata(pdev
);
830 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
833 clk_disable_unprepare(nfc
->clk
);
837 #ifdef CONFIG_PM_SLEEP
838 static int vf610_nfc_suspend(struct device
*dev
)
840 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
841 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
843 clk_disable_unprepare(nfc
->clk
);
847 static int vf610_nfc_resume(struct device
*dev
)
849 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
850 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
852 pinctrl_pm_select_default_state(dev
);
854 clk_prepare_enable(nfc
->clk
);
856 vf610_nfc_preinit_controller(nfc
);
857 vf610_nfc_init_controller(nfc
);
862 static SIMPLE_DEV_PM_OPS(vf610_nfc_pm_ops
, vf610_nfc_suspend
, vf610_nfc_resume
);
864 static struct platform_driver vf610_nfc_driver
= {
867 .of_match_table
= vf610_nfc_dt_ids
,
868 .pm
= &vf610_nfc_pm_ops
,
870 .probe
= vf610_nfc_probe
,
871 .remove
= vf610_nfc_remove
,
874 module_platform_driver(vf610_nfc_driver
);
876 MODULE_AUTHOR("Stefan Agner <stefan.agner@toradex.com>");
877 MODULE_DESCRIPTION("Freescale VF610/MPC5125 NFC MTD NAND driver");
878 MODULE_LICENSE("GPL");