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/rawnand.h>
35 #include <linux/mtd/partitions.h>
36 #include <linux/of_device.h>
37 #include <linux/platform_device.h>
38 #include <linux/slab.h>
40 #define DRV_NAME "vf610_nfc"
42 /* Register Offsets */
43 #define NFC_FLASH_CMD1 0x3F00
44 #define NFC_FLASH_CMD2 0x3F04
45 #define NFC_COL_ADDR 0x3F08
46 #define NFC_ROW_ADDR 0x3F0c
47 #define NFC_ROW_ADDR_INC 0x3F14
48 #define NFC_FLASH_STATUS1 0x3F18
49 #define NFC_FLASH_STATUS2 0x3F1c
50 #define NFC_CACHE_SWAP 0x3F28
51 #define NFC_SECTOR_SIZE 0x3F2c
52 #define NFC_FLASH_CONFIG 0x3F30
53 #define NFC_IRQ_STATUS 0x3F38
55 /* Addresses for NFC MAIN RAM BUFFER areas */
56 #define NFC_MAIN_AREA(n) ((n) * 0x1000)
58 #define PAGE_2K 0x0800
60 #define OOB_MAX 0x0100
63 * NFC_CMD2[CODE] values. See section:
64 * - 31.4.7 Flash Command Code Description, Vybrid manual
65 * - 23.8.6 Flash Command Sequencer, MPC5125 manual
67 * Briefly these are bitmasks of controller cycles.
69 #define READ_PAGE_CMD_CODE 0x7EE0
70 #define READ_ONFI_PARAM_CMD_CODE 0x4860
71 #define PROGRAM_PAGE_CMD_CODE 0x7FC0
72 #define ERASE_CMD_CODE 0x4EC0
73 #define READ_ID_CMD_CODE 0x4804
74 #define RESET_CMD_CODE 0x4040
75 #define STATUS_READ_CMD_CODE 0x4068
77 /* NFC ECC mode define */
82 /*** Register Mask and bit definitions */
84 /* NFC_FLASH_CMD1 Field */
85 #define CMD_BYTE2_MASK 0xFF000000
86 #define CMD_BYTE2_SHIFT 24
88 /* NFC_FLASH_CM2 Field */
89 #define CMD_BYTE1_MASK 0xFF000000
90 #define CMD_BYTE1_SHIFT 24
91 #define CMD_CODE_MASK 0x00FFFF00
92 #define CMD_CODE_SHIFT 8
93 #define BUFNO_MASK 0x00000006
95 #define START_BIT BIT(0)
97 /* NFC_COL_ADDR Field */
98 #define COL_ADDR_MASK 0x0000FFFF
99 #define COL_ADDR_SHIFT 0
101 /* NFC_ROW_ADDR Field */
102 #define ROW_ADDR_MASK 0x00FFFFFF
103 #define ROW_ADDR_SHIFT 0
104 #define ROW_ADDR_CHIP_SEL_RB_MASK 0xF0000000
105 #define ROW_ADDR_CHIP_SEL_RB_SHIFT 28
106 #define ROW_ADDR_CHIP_SEL_MASK 0x0F000000
107 #define ROW_ADDR_CHIP_SEL_SHIFT 24
109 /* NFC_FLASH_STATUS2 Field */
110 #define STATUS_BYTE1_MASK 0x000000FF
112 /* NFC_FLASH_CONFIG Field */
113 #define CONFIG_ECC_SRAM_ADDR_MASK 0x7FC00000
114 #define CONFIG_ECC_SRAM_ADDR_SHIFT 22
115 #define CONFIG_ECC_SRAM_REQ_BIT BIT(21)
116 #define CONFIG_DMA_REQ_BIT BIT(20)
117 #define CONFIG_ECC_MODE_MASK 0x000E0000
118 #define CONFIG_ECC_MODE_SHIFT 17
119 #define CONFIG_FAST_FLASH_BIT BIT(16)
120 #define CONFIG_16BIT BIT(7)
121 #define CONFIG_BOOT_MODE_BIT BIT(6)
122 #define CONFIG_ADDR_AUTO_INCR_BIT BIT(5)
123 #define CONFIG_BUFNO_AUTO_INCR_BIT BIT(4)
124 #define CONFIG_PAGE_CNT_MASK 0xF
125 #define CONFIG_PAGE_CNT_SHIFT 0
127 /* NFC_IRQ_STATUS Field */
128 #define IDLE_IRQ_BIT BIT(29)
129 #define IDLE_EN_BIT BIT(20)
130 #define CMD_DONE_CLEAR_BIT BIT(18)
131 #define IDLE_CLEAR_BIT BIT(17)
134 * ECC status - seems to consume 8 bytes (double word). The documented
135 * status byte is located in the lowest byte of the second word (which is
136 * the 4th or 7th byte depending on endianness).
137 * Calculate an offset to store the ECC status at the end of the buffer.
139 #define ECC_SRAM_ADDR (PAGE_2K + OOB_MAX - 8)
141 #define ECC_STATUS 0x4
142 #define ECC_STATUS_MASK 0x80
143 #define ECC_STATUS_ERR_COUNT 0x3F
145 enum vf610_nfc_alt_buf
{
152 enum vf610_nfc_variant
{
157 struct nand_chip chip
;
160 struct completion cmd_done
;
163 /* Status and ID are in alternate locations. */
164 enum vf610_nfc_alt_buf alt_buf
;
165 enum vf610_nfc_variant variant
;
171 static inline struct vf610_nfc
*mtd_to_nfc(struct mtd_info
*mtd
)
173 return container_of(mtd_to_nand(mtd
), struct vf610_nfc
, chip
);
176 static inline u32
vf610_nfc_read(struct vf610_nfc
*nfc
, uint reg
)
178 return readl(nfc
->regs
+ reg
);
181 static inline void vf610_nfc_write(struct vf610_nfc
*nfc
, uint reg
, u32 val
)
183 writel(val
, nfc
->regs
+ reg
);
186 static inline void vf610_nfc_set(struct vf610_nfc
*nfc
, uint reg
, u32 bits
)
188 vf610_nfc_write(nfc
, reg
, vf610_nfc_read(nfc
, reg
) | bits
);
191 static inline void vf610_nfc_clear(struct vf610_nfc
*nfc
, uint reg
, u32 bits
)
193 vf610_nfc_write(nfc
, reg
, vf610_nfc_read(nfc
, reg
) & ~bits
);
196 static inline void vf610_nfc_set_field(struct vf610_nfc
*nfc
, u32 reg
,
197 u32 mask
, u32 shift
, u32 val
)
199 vf610_nfc_write(nfc
, reg
,
200 (vf610_nfc_read(nfc
, reg
) & (~mask
)) | val
<< shift
);
203 static inline void vf610_nfc_memcpy(void *dst
, const void __iomem
*src
,
207 * Use this accessor for the internal SRAM buffers. On the ARM
208 * Freescale Vybrid SoC it's known that the driver can treat
209 * the SRAM buffer as if it's memory. Other platform might need
210 * to treat the buffers differently.
212 * For the time being, use memcpy
217 /* Clear flags for upcoming command */
218 static inline void vf610_nfc_clear_status(struct vf610_nfc
*nfc
)
220 u32 tmp
= vf610_nfc_read(nfc
, NFC_IRQ_STATUS
);
222 tmp
|= CMD_DONE_CLEAR_BIT
| IDLE_CLEAR_BIT
;
223 vf610_nfc_write(nfc
, NFC_IRQ_STATUS
, tmp
);
226 static void vf610_nfc_done(struct vf610_nfc
*nfc
)
228 unsigned long timeout
= msecs_to_jiffies(100);
231 * Barrier is needed after this write. This write need
232 * to be done before reading the next register the first
234 * vf610_nfc_set implicates such a barrier by using writel
235 * to write to the register.
237 vf610_nfc_set(nfc
, NFC_IRQ_STATUS
, IDLE_EN_BIT
);
238 vf610_nfc_set(nfc
, NFC_FLASH_CMD2
, START_BIT
);
240 if (!wait_for_completion_timeout(&nfc
->cmd_done
, timeout
))
241 dev_warn(nfc
->dev
, "Timeout while waiting for BUSY.\n");
243 vf610_nfc_clear_status(nfc
);
246 static u8
vf610_nfc_get_id(struct vf610_nfc
*nfc
, int col
)
251 flash_id
= vf610_nfc_read(nfc
, NFC_FLASH_STATUS1
);
252 flash_id
>>= (3 - col
) * 8;
254 flash_id
= vf610_nfc_read(nfc
, NFC_FLASH_STATUS2
);
258 return flash_id
& 0xff;
261 static u8
vf610_nfc_get_status(struct vf610_nfc
*nfc
)
263 return vf610_nfc_read(nfc
, NFC_FLASH_STATUS2
) & STATUS_BYTE1_MASK
;
266 static void vf610_nfc_send_command(struct vf610_nfc
*nfc
, u32 cmd_byte1
,
271 vf610_nfc_clear_status(nfc
);
273 tmp
= vf610_nfc_read(nfc
, NFC_FLASH_CMD2
);
274 tmp
&= ~(CMD_BYTE1_MASK
| CMD_CODE_MASK
| BUFNO_MASK
);
275 tmp
|= cmd_byte1
<< CMD_BYTE1_SHIFT
;
276 tmp
|= cmd_code
<< CMD_CODE_SHIFT
;
277 vf610_nfc_write(nfc
, NFC_FLASH_CMD2
, tmp
);
280 static void vf610_nfc_send_commands(struct vf610_nfc
*nfc
, u32 cmd_byte1
,
281 u32 cmd_byte2
, u32 cmd_code
)
285 vf610_nfc_send_command(nfc
, cmd_byte1
, cmd_code
);
287 tmp
= vf610_nfc_read(nfc
, NFC_FLASH_CMD1
);
288 tmp
&= ~CMD_BYTE2_MASK
;
289 tmp
|= cmd_byte2
<< CMD_BYTE2_SHIFT
;
290 vf610_nfc_write(nfc
, NFC_FLASH_CMD1
, tmp
);
293 static irqreturn_t
vf610_nfc_irq(int irq
, void *data
)
295 struct mtd_info
*mtd
= data
;
296 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
298 vf610_nfc_clear(nfc
, NFC_IRQ_STATUS
, IDLE_EN_BIT
);
299 complete(&nfc
->cmd_done
);
304 static void vf610_nfc_addr_cycle(struct vf610_nfc
*nfc
, int column
, int page
)
307 if (nfc
->chip
.options
& NAND_BUSWIDTH_16
)
309 vf610_nfc_set_field(nfc
, NFC_COL_ADDR
, COL_ADDR_MASK
,
310 COL_ADDR_SHIFT
, column
);
313 vf610_nfc_set_field(nfc
, NFC_ROW_ADDR
, ROW_ADDR_MASK
,
314 ROW_ADDR_SHIFT
, page
);
317 static inline void vf610_nfc_ecc_mode(struct vf610_nfc
*nfc
, int ecc_mode
)
319 vf610_nfc_set_field(nfc
, NFC_FLASH_CONFIG
,
320 CONFIG_ECC_MODE_MASK
,
321 CONFIG_ECC_MODE_SHIFT
, ecc_mode
);
324 static inline void vf610_nfc_transfer_size(struct vf610_nfc
*nfc
, int size
)
326 vf610_nfc_write(nfc
, NFC_SECTOR_SIZE
, size
);
329 static void vf610_nfc_command(struct mtd_info
*mtd
, unsigned command
,
330 int column
, int page
)
332 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
333 int trfr_sz
= nfc
->chip
.options
& NAND_BUSWIDTH_16
? 1 : 0;
335 nfc
->buf_offset
= max(column
, 0);
336 nfc
->alt_buf
= ALT_BUF_DATA
;
340 /* Use valid column/page from preread... */
341 vf610_nfc_addr_cycle(nfc
, column
, page
);
345 * SEQIN => data => PAGEPROG sequence is done by the controller
346 * hence we do not need to issue the command here...
349 case NAND_CMD_PAGEPROG
:
350 trfr_sz
+= nfc
->write_sz
;
351 vf610_nfc_transfer_size(nfc
, trfr_sz
);
352 vf610_nfc_send_commands(nfc
, NAND_CMD_SEQIN
,
353 command
, PROGRAM_PAGE_CMD_CODE
);
355 vf610_nfc_ecc_mode(nfc
, nfc
->ecc_mode
);
357 vf610_nfc_ecc_mode(nfc
, ECC_BYPASS
);
361 vf610_nfc_transfer_size(nfc
, 0);
362 vf610_nfc_send_command(nfc
, command
, RESET_CMD_CODE
);
365 case NAND_CMD_READOOB
:
366 trfr_sz
+= mtd
->oobsize
;
367 column
= mtd
->writesize
;
368 vf610_nfc_transfer_size(nfc
, trfr_sz
);
369 vf610_nfc_send_commands(nfc
, NAND_CMD_READ0
,
370 NAND_CMD_READSTART
, READ_PAGE_CMD_CODE
);
371 vf610_nfc_addr_cycle(nfc
, column
, page
);
372 vf610_nfc_ecc_mode(nfc
, ECC_BYPASS
);
376 trfr_sz
+= mtd
->writesize
+ mtd
->oobsize
;
377 vf610_nfc_transfer_size(nfc
, trfr_sz
);
378 vf610_nfc_send_commands(nfc
, NAND_CMD_READ0
,
379 NAND_CMD_READSTART
, READ_PAGE_CMD_CODE
);
380 vf610_nfc_addr_cycle(nfc
, column
, page
);
381 vf610_nfc_ecc_mode(nfc
, nfc
->ecc_mode
);
385 nfc
->alt_buf
= ALT_BUF_ONFI
;
386 trfr_sz
= 3 * sizeof(struct nand_onfi_params
);
387 vf610_nfc_transfer_size(nfc
, trfr_sz
);
388 vf610_nfc_send_command(nfc
, command
, READ_ONFI_PARAM_CMD_CODE
);
389 vf610_nfc_addr_cycle(nfc
, -1, column
);
390 vf610_nfc_ecc_mode(nfc
, ECC_BYPASS
);
393 case NAND_CMD_ERASE1
:
394 vf610_nfc_transfer_size(nfc
, 0);
395 vf610_nfc_send_commands(nfc
, command
,
396 NAND_CMD_ERASE2
, ERASE_CMD_CODE
);
397 vf610_nfc_addr_cycle(nfc
, column
, page
);
400 case NAND_CMD_READID
:
401 nfc
->alt_buf
= ALT_BUF_ID
;
403 vf610_nfc_transfer_size(nfc
, 0);
404 vf610_nfc_send_command(nfc
, command
, READ_ID_CMD_CODE
);
405 vf610_nfc_addr_cycle(nfc
, -1, column
);
408 case NAND_CMD_STATUS
:
409 nfc
->alt_buf
= ALT_BUF_STAT
;
410 vf610_nfc_transfer_size(nfc
, 0);
411 vf610_nfc_send_command(nfc
, command
, STATUS_READ_CMD_CODE
);
419 nfc
->use_hw_ecc
= false;
423 static void vf610_nfc_read_buf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
425 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
426 uint c
= nfc
->buf_offset
;
428 /* Alternate buffers are only supported through read_byte */
429 WARN_ON(nfc
->alt_buf
);
431 vf610_nfc_memcpy(buf
, nfc
->regs
+ NFC_MAIN_AREA(0) + c
, len
);
433 nfc
->buf_offset
+= len
;
436 static void vf610_nfc_write_buf(struct mtd_info
*mtd
, const uint8_t *buf
,
439 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
440 uint c
= nfc
->buf_offset
;
443 l
= min_t(uint
, len
, mtd
->writesize
+ mtd
->oobsize
- c
);
444 vf610_nfc_memcpy(nfc
->regs
+ NFC_MAIN_AREA(0) + c
, buf
, l
);
447 nfc
->buf_offset
+= l
;
450 static uint8_t vf610_nfc_read_byte(struct mtd_info
*mtd
)
452 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
454 uint c
= nfc
->buf_offset
;
456 switch (nfc
->alt_buf
) {
458 tmp
= vf610_nfc_get_id(nfc
, c
);
461 tmp
= vf610_nfc_get_status(nfc
);
463 #ifdef __LITTLE_ENDIAN
465 /* Reverse byte since the controller uses big endianness */
466 c
= nfc
->buf_offset
^ 0x3;
470 tmp
= *((u8
*)(nfc
->regs
+ NFC_MAIN_AREA(0) + c
));
477 static u16
vf610_nfc_read_word(struct mtd_info
*mtd
)
481 vf610_nfc_read_buf(mtd
, (u_char
*)&tmp
, sizeof(tmp
));
485 /* If not provided, upper layers apply a fixed delay. */
486 static int vf610_nfc_dev_ready(struct mtd_info
*mtd
)
488 /* NFC handles R/B internally; always ready. */
493 * This function supports Vybrid only (MPC5125 would have full RB and four CS)
495 static void vf610_nfc_select_chip(struct mtd_info
*mtd
, int chip
)
497 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
498 u32 tmp
= vf610_nfc_read(nfc
, NFC_ROW_ADDR
);
500 /* Vybrid only (MPC5125 would have full RB and four CS) */
501 if (nfc
->variant
!= NFC_VFC610
)
504 tmp
&= ~(ROW_ADDR_CHIP_SEL_RB_MASK
| ROW_ADDR_CHIP_SEL_MASK
);
507 tmp
|= 1 << ROW_ADDR_CHIP_SEL_RB_SHIFT
;
508 tmp
|= BIT(chip
) << ROW_ADDR_CHIP_SEL_SHIFT
;
511 vf610_nfc_write(nfc
, NFC_ROW_ADDR
, tmp
);
514 /* Count the number of 0's in buff up to max_bits */
515 static inline int count_written_bits(uint8_t *buff
, int size
, int max_bits
)
517 uint32_t *buff32
= (uint32_t *)buff
;
518 int k
, written_bits
= 0;
520 for (k
= 0; k
< (size
/ 4); k
++) {
521 written_bits
+= hweight32(~buff32
[k
]);
522 if (unlikely(written_bits
> max_bits
))
529 static inline int vf610_nfc_correct_data(struct mtd_info
*mtd
, uint8_t *dat
,
530 uint8_t *oob
, int page
)
532 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
533 u32 ecc_status_off
= NFC_MAIN_AREA(0) + ECC_SRAM_ADDR
+ ECC_STATUS
;
536 int flips_threshold
= nfc
->chip
.ecc
.strength
/ 2;
538 ecc_status
= vf610_nfc_read(nfc
, ecc_status_off
) & 0xff;
539 ecc_count
= ecc_status
& ECC_STATUS_ERR_COUNT
;
541 if (!(ecc_status
& ECC_STATUS_MASK
))
544 /* Read OOB without ECC unit enabled */
545 vf610_nfc_command(mtd
, NAND_CMD_READOOB
, 0, page
);
546 vf610_nfc_read_buf(mtd
, oob
, mtd
->oobsize
);
549 * On an erased page, bit count (including OOB) should be zero or
550 * at least less then half of the ECC strength.
552 return nand_check_erased_ecc_chunk(dat
, nfc
->chip
.ecc
.size
, oob
,
553 mtd
->oobsize
, NULL
, 0,
557 static int vf610_nfc_read_page(struct mtd_info
*mtd
, struct nand_chip
*chip
,
558 uint8_t *buf
, int oob_required
, int page
)
560 int eccsize
= chip
->ecc
.size
;
563 nand_read_page_op(chip
, page
, 0, buf
, eccsize
);
565 vf610_nfc_read_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
567 stat
= vf610_nfc_correct_data(mtd
, buf
, chip
->oob_poi
, page
);
570 mtd
->ecc_stats
.failed
++;
573 mtd
->ecc_stats
.corrected
+= stat
;
578 static int vf610_nfc_write_page(struct mtd_info
*mtd
, struct nand_chip
*chip
,
579 const uint8_t *buf
, int oob_required
, int page
)
581 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
583 nand_prog_page_begin_op(chip
, page
, 0, buf
, mtd
->writesize
);
585 vf610_nfc_write_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
587 /* Always write whole page including OOB due to HW ECC */
588 nfc
->use_hw_ecc
= true;
589 nfc
->write_sz
= mtd
->writesize
+ mtd
->oobsize
;
591 return nand_prog_page_end_op(chip
);
594 static const struct of_device_id vf610_nfc_dt_ids
[] = {
595 { .compatible
= "fsl,vf610-nfc", .data
= (void *)NFC_VFC610
},
598 MODULE_DEVICE_TABLE(of
, vf610_nfc_dt_ids
);
600 static void vf610_nfc_preinit_controller(struct vf610_nfc
*nfc
)
602 vf610_nfc_clear(nfc
, NFC_FLASH_CONFIG
, CONFIG_16BIT
);
603 vf610_nfc_clear(nfc
, NFC_FLASH_CONFIG
, CONFIG_ADDR_AUTO_INCR_BIT
);
604 vf610_nfc_clear(nfc
, NFC_FLASH_CONFIG
, CONFIG_BUFNO_AUTO_INCR_BIT
);
605 vf610_nfc_clear(nfc
, NFC_FLASH_CONFIG
, CONFIG_BOOT_MODE_BIT
);
606 vf610_nfc_clear(nfc
, NFC_FLASH_CONFIG
, CONFIG_DMA_REQ_BIT
);
607 vf610_nfc_set(nfc
, NFC_FLASH_CONFIG
, CONFIG_FAST_FLASH_BIT
);
609 /* Disable virtual pages, only one elementary transfer unit */
610 vf610_nfc_set_field(nfc
, NFC_FLASH_CONFIG
, CONFIG_PAGE_CNT_MASK
,
611 CONFIG_PAGE_CNT_SHIFT
, 1);
614 static void vf610_nfc_init_controller(struct vf610_nfc
*nfc
)
616 if (nfc
->chip
.options
& NAND_BUSWIDTH_16
)
617 vf610_nfc_set(nfc
, NFC_FLASH_CONFIG
, CONFIG_16BIT
);
619 vf610_nfc_clear(nfc
, NFC_FLASH_CONFIG
, CONFIG_16BIT
);
621 if (nfc
->chip
.ecc
.mode
== NAND_ECC_HW
) {
622 /* Set ECC status offset in SRAM */
623 vf610_nfc_set_field(nfc
, NFC_FLASH_CONFIG
,
624 CONFIG_ECC_SRAM_ADDR_MASK
,
625 CONFIG_ECC_SRAM_ADDR_SHIFT
,
628 /* Enable ECC status in SRAM */
629 vf610_nfc_set(nfc
, NFC_FLASH_CONFIG
, CONFIG_ECC_SRAM_REQ_BIT
);
633 static int vf610_nfc_probe(struct platform_device
*pdev
)
635 struct vf610_nfc
*nfc
;
636 struct resource
*res
;
637 struct mtd_info
*mtd
;
638 struct nand_chip
*chip
;
639 struct device_node
*child
;
640 const struct of_device_id
*of_id
;
644 nfc
= devm_kzalloc(&pdev
->dev
, sizeof(*nfc
), GFP_KERNEL
);
648 nfc
->dev
= &pdev
->dev
;
650 mtd
= nand_to_mtd(chip
);
652 mtd
->owner
= THIS_MODULE
;
653 mtd
->dev
.parent
= nfc
->dev
;
654 mtd
->name
= DRV_NAME
;
656 irq
= platform_get_irq(pdev
, 0);
660 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
661 nfc
->regs
= devm_ioremap_resource(nfc
->dev
, res
);
662 if (IS_ERR(nfc
->regs
))
663 return PTR_ERR(nfc
->regs
);
665 nfc
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
666 if (IS_ERR(nfc
->clk
))
667 return PTR_ERR(nfc
->clk
);
669 err
= clk_prepare_enable(nfc
->clk
);
671 dev_err(nfc
->dev
, "Unable to enable clock!\n");
675 of_id
= of_match_device(vf610_nfc_dt_ids
, &pdev
->dev
);
676 nfc
->variant
= (enum vf610_nfc_variant
)of_id
->data
;
678 for_each_available_child_of_node(nfc
->dev
->of_node
, child
) {
679 if (of_device_is_compatible(child
, "fsl,vf610-nfc-nandcs")) {
681 if (nand_get_flash_node(chip
)) {
683 "Only one NAND chip supported!\n");
688 nand_set_flash_node(chip
, child
);
692 if (!nand_get_flash_node(chip
)) {
693 dev_err(nfc
->dev
, "NAND chip sub-node missing!\n");
698 chip
->dev_ready
= vf610_nfc_dev_ready
;
699 chip
->cmdfunc
= vf610_nfc_command
;
700 chip
->read_byte
= vf610_nfc_read_byte
;
701 chip
->read_word
= vf610_nfc_read_word
;
702 chip
->read_buf
= vf610_nfc_read_buf
;
703 chip
->write_buf
= vf610_nfc_write_buf
;
704 chip
->select_chip
= vf610_nfc_select_chip
;
705 chip
->onfi_set_features
= nand_onfi_get_set_features_notsupp
;
706 chip
->onfi_get_features
= nand_onfi_get_set_features_notsupp
;
708 chip
->options
|= NAND_NO_SUBPAGE_WRITE
;
710 init_completion(&nfc
->cmd_done
);
712 err
= devm_request_irq(nfc
->dev
, irq
, vf610_nfc_irq
, 0, DRV_NAME
, mtd
);
714 dev_err(nfc
->dev
, "Error requesting IRQ!\n");
718 vf610_nfc_preinit_controller(nfc
);
720 /* first scan to find the device and get the page size */
721 err
= nand_scan_ident(mtd
, 1, NULL
);
725 vf610_nfc_init_controller(nfc
);
727 /* Bad block options. */
728 if (chip
->bbt_options
& NAND_BBT_USE_FLASH
)
729 chip
->bbt_options
|= NAND_BBT_NO_OOB
;
731 /* Single buffer only, max 256 OOB minus ECC status */
732 if (mtd
->writesize
+ mtd
->oobsize
> PAGE_2K
+ OOB_MAX
- 8) {
733 dev_err(nfc
->dev
, "Unsupported flash page size\n");
738 if (chip
->ecc
.mode
== NAND_ECC_HW
) {
739 if (mtd
->writesize
!= PAGE_2K
&& mtd
->oobsize
< 64) {
740 dev_err(nfc
->dev
, "Unsupported flash with hwecc\n");
745 if (chip
->ecc
.size
!= mtd
->writesize
) {
746 dev_err(nfc
->dev
, "Step size needs to be page size\n");
751 /* Only 64 byte ECC layouts known */
752 if (mtd
->oobsize
> 64)
755 /* Use default large page ECC layout defined in NAND core */
756 mtd_set_ooblayout(mtd
, &nand_ooblayout_lp_ops
);
757 if (chip
->ecc
.strength
== 32) {
758 nfc
->ecc_mode
= ECC_60_BYTE
;
759 chip
->ecc
.bytes
= 60;
760 } else if (chip
->ecc
.strength
== 24) {
761 nfc
->ecc_mode
= ECC_45_BYTE
;
762 chip
->ecc
.bytes
= 45;
764 dev_err(nfc
->dev
, "Unsupported ECC strength\n");
769 chip
->ecc
.read_page
= vf610_nfc_read_page
;
770 chip
->ecc
.write_page
= vf610_nfc_write_page
;
772 chip
->ecc
.size
= PAGE_2K
;
775 /* second phase scan */
776 err
= nand_scan_tail(mtd
);
780 platform_set_drvdata(pdev
, mtd
);
782 /* Register device in MTD */
783 return mtd_device_register(mtd
, NULL
, 0);
786 of_node_put(nand_get_flash_node(chip
));
788 clk_disable_unprepare(nfc
->clk
);
792 static int vf610_nfc_remove(struct platform_device
*pdev
)
794 struct mtd_info
*mtd
= platform_get_drvdata(pdev
);
795 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
798 clk_disable_unprepare(nfc
->clk
);
802 #ifdef CONFIG_PM_SLEEP
803 static int vf610_nfc_suspend(struct device
*dev
)
805 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
806 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
808 clk_disable_unprepare(nfc
->clk
);
812 static int vf610_nfc_resume(struct device
*dev
)
816 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
817 struct vf610_nfc
*nfc
= mtd_to_nfc(mtd
);
819 err
= clk_prepare_enable(nfc
->clk
);
823 vf610_nfc_preinit_controller(nfc
);
824 vf610_nfc_init_controller(nfc
);
829 static SIMPLE_DEV_PM_OPS(vf610_nfc_pm_ops
, vf610_nfc_suspend
, vf610_nfc_resume
);
831 static struct platform_driver vf610_nfc_driver
= {
834 .of_match_table
= vf610_nfc_dt_ids
,
835 .pm
= &vf610_nfc_pm_ops
,
837 .probe
= vf610_nfc_probe
,
838 .remove
= vf610_nfc_remove
,
841 module_platform_driver(vf610_nfc_driver
);
843 MODULE_AUTHOR("Stefan Agner <stefan.agner@toradex.com>");
844 MODULE_DESCRIPTION("Freescale VF610/MPC5125 NFC MTD NAND driver");
845 MODULE_LICENSE("GPL");