mtd: nand: pass page number to ecc->write_xxx() methods
[firefly-linux-kernel-4.4.55.git] / drivers / mtd / nand / vf610_nfc.c
1 /*
2  * Copyright 2009-2015 Freescale Semiconductor, Inc. and others
3  *
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>
10  *
11  * Based on original driver mpc5121_nfc.c.
12  *
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.
17  *
18  * Limitations:
19  * - Untested on MPC5125 and M54418.
20  * - DMA and pipelining not used.
21  * - 2K pages or less.
22  * - HW ECC: Only 2K page with 64+ OOB.
23  * - HW ECC: Only 24 and 32-bit error correction implemented.
24  */
25
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>
32 #include <linux/io.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>
41
42 #define DRV_NAME                "vf610_nfc"
43
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
56
57 /* Addresses for NFC MAIN RAM BUFFER areas */
58 #define NFC_MAIN_AREA(n)                ((n) *  0x1000)
59
60 #define PAGE_2K                         0x0800
61 #define OOB_64                          0x0040
62 #define OOB_MAX                         0x0100
63
64 /*
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
68  *
69  * Briefly these are bitmasks of controller cycles.
70  */
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
78
79 /* NFC ECC mode define */
80 #define ECC_BYPASS                      0
81 #define ECC_45_BYTE                     6
82 #define ECC_60_BYTE                     7
83
84 /*** Register Mask and bit definitions */
85
86 /* NFC_FLASH_CMD1 Field */
87 #define CMD_BYTE2_MASK                          0xFF000000
88 #define CMD_BYTE2_SHIFT                         24
89
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
96 #define BUFNO_SHIFT                             1
97 #define START_BIT                               BIT(0)
98
99 /* NFC_COL_ADDR Field */
100 #define COL_ADDR_MASK                           0x0000FFFF
101 #define COL_ADDR_SHIFT                          0
102
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
110
111 /* NFC_FLASH_STATUS2 Field */
112 #define STATUS_BYTE1_MASK                       0x000000FF
113
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
128
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)
134
135 /*
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.
140  */
141 #define ECC_SRAM_ADDR           (PAGE_2K + OOB_MAX - 8)
142
143 #define ECC_STATUS              0x4
144 #define ECC_STATUS_MASK         0x80
145 #define ECC_STATUS_ERR_COUNT    0x3F
146
147 enum vf610_nfc_alt_buf {
148         ALT_BUF_DATA = 0,
149         ALT_BUF_ID = 1,
150         ALT_BUF_STAT = 2,
151         ALT_BUF_ONFI = 3,
152 };
153
154 enum vf610_nfc_variant {
155         NFC_VFC610 = 1,
156 };
157
158 struct vf610_nfc {
159         struct mtd_info mtd;
160         struct nand_chip chip;
161         struct device *dev;
162         void __iomem *regs;
163         struct completion cmd_done;
164         uint buf_offset;
165         int write_sz;
166         /* Status and ID are in alternate locations. */
167         enum vf610_nfc_alt_buf alt_buf;
168         enum vf610_nfc_variant variant;
169         struct clk *clk;
170         bool use_hw_ecc;
171         u32 ecc_mode;
172 };
173
174 #define mtd_to_nfc(_mtd) container_of(_mtd, struct vf610_nfc, mtd)
175
176 static struct nand_ecclayout vf610_nfc_ecc45 = {
177         .eccbytes = 45,
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},
184         .oobfree = {
185                 {.offset = 2,
186                  .length = 17} }
187 };
188
189 static struct nand_ecclayout vf610_nfc_ecc60 = {
190         .eccbytes = 60,
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,
198                    60, 61, 62, 63 },
199         .oobfree = {
200                 {.offset = 2,
201                  .length = 2} }
202 };
203
204 static inline u32 vf610_nfc_read(struct vf610_nfc *nfc, uint reg)
205 {
206         return readl(nfc->regs + reg);
207 }
208
209 static inline void vf610_nfc_write(struct vf610_nfc *nfc, uint reg, u32 val)
210 {
211         writel(val, nfc->regs + reg);
212 }
213
214 static inline void vf610_nfc_set(struct vf610_nfc *nfc, uint reg, u32 bits)
215 {
216         vf610_nfc_write(nfc, reg, vf610_nfc_read(nfc, reg) | bits);
217 }
218
219 static inline void vf610_nfc_clear(struct vf610_nfc *nfc, uint reg, u32 bits)
220 {
221         vf610_nfc_write(nfc, reg, vf610_nfc_read(nfc, reg) & ~bits);
222 }
223
224 static inline void vf610_nfc_set_field(struct vf610_nfc *nfc, u32 reg,
225                                        u32 mask, u32 shift, u32 val)
226 {
227         vf610_nfc_write(nfc, reg,
228                         (vf610_nfc_read(nfc, reg) & (~mask)) | val << shift);
229 }
230
231 static inline void vf610_nfc_memcpy(void *dst, const void __iomem *src,
232                                     size_t n)
233 {
234         /*
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.
239          *
240          * For the time being, use memcpy
241          */
242         memcpy(dst, src, n);
243 }
244
245 /* Clear flags for upcoming command */
246 static inline void vf610_nfc_clear_status(struct vf610_nfc *nfc)
247 {
248         u32 tmp = vf610_nfc_read(nfc, NFC_IRQ_STATUS);
249
250         tmp |= CMD_DONE_CLEAR_BIT | IDLE_CLEAR_BIT;
251         vf610_nfc_write(nfc, NFC_IRQ_STATUS, tmp);
252 }
253
254 static void vf610_nfc_done(struct vf610_nfc *nfc)
255 {
256         unsigned long timeout = msecs_to_jiffies(100);
257
258         /*
259          * Barrier is needed after this write. This write need
260          * to be done before reading the next register the first
261          * time.
262          * vf610_nfc_set implicates such a barrier by using writel
263          * to write to the register.
264          */
265         vf610_nfc_set(nfc, NFC_IRQ_STATUS, IDLE_EN_BIT);
266         vf610_nfc_set(nfc, NFC_FLASH_CMD2, START_BIT);
267
268         if (!wait_for_completion_timeout(&nfc->cmd_done, timeout))
269                 dev_warn(nfc->dev, "Timeout while waiting for BUSY.\n");
270
271         vf610_nfc_clear_status(nfc);
272 }
273
274 static u8 vf610_nfc_get_id(struct vf610_nfc *nfc, int col)
275 {
276         u32 flash_id;
277
278         if (col < 4) {
279                 flash_id = vf610_nfc_read(nfc, NFC_FLASH_STATUS1);
280                 flash_id >>= (3 - col) * 8;
281         } else {
282                 flash_id = vf610_nfc_read(nfc, NFC_FLASH_STATUS2);
283                 flash_id >>= 24;
284         }
285
286         return flash_id & 0xff;
287 }
288
289 static u8 vf610_nfc_get_status(struct vf610_nfc *nfc)
290 {
291         return vf610_nfc_read(nfc, NFC_FLASH_STATUS2) & STATUS_BYTE1_MASK;
292 }
293
294 static void vf610_nfc_send_command(struct vf610_nfc *nfc, u32 cmd_byte1,
295                                    u32 cmd_code)
296 {
297         u32 tmp;
298
299         vf610_nfc_clear_status(nfc);
300
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);
306 }
307
308 static void vf610_nfc_send_commands(struct vf610_nfc *nfc, u32 cmd_byte1,
309                                     u32 cmd_byte2, u32 cmd_code)
310 {
311         u32 tmp;
312
313         vf610_nfc_send_command(nfc, cmd_byte1, cmd_code);
314
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);
319 }
320
321 static irqreturn_t vf610_nfc_irq(int irq, void *data)
322 {
323         struct mtd_info *mtd = data;
324         struct vf610_nfc *nfc = mtd_to_nfc(mtd);
325
326         vf610_nfc_clear(nfc, NFC_IRQ_STATUS, IDLE_EN_BIT);
327         complete(&nfc->cmd_done);
328
329         return IRQ_HANDLED;
330 }
331
332 static void vf610_nfc_addr_cycle(struct vf610_nfc *nfc, int column, int page)
333 {
334         if (column != -1) {
335                 if (nfc->chip.options & NAND_BUSWIDTH_16)
336                         column = column / 2;
337                 vf610_nfc_set_field(nfc, NFC_COL_ADDR, COL_ADDR_MASK,
338                                     COL_ADDR_SHIFT, column);
339         }
340         if (page != -1)
341                 vf610_nfc_set_field(nfc, NFC_ROW_ADDR, ROW_ADDR_MASK,
342                                     ROW_ADDR_SHIFT, page);
343 }
344
345 static inline void vf610_nfc_ecc_mode(struct vf610_nfc *nfc, int ecc_mode)
346 {
347         vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG,
348                             CONFIG_ECC_MODE_MASK,
349                             CONFIG_ECC_MODE_SHIFT, ecc_mode);
350 }
351
352 static inline void vf610_nfc_transfer_size(struct vf610_nfc *nfc, int size)
353 {
354         vf610_nfc_write(nfc, NFC_SECTOR_SIZE, size);
355 }
356
357 static void vf610_nfc_command(struct mtd_info *mtd, unsigned command,
358                               int column, int page)
359 {
360         struct vf610_nfc *nfc = mtd_to_nfc(mtd);
361         int trfr_sz = nfc->chip.options & NAND_BUSWIDTH_16 ? 1 : 0;
362
363         nfc->buf_offset = max(column, 0);
364         nfc->alt_buf = ALT_BUF_DATA;
365
366         switch (command) {
367         case NAND_CMD_SEQIN:
368                 /* Use valid column/page from preread... */
369                 vf610_nfc_addr_cycle(nfc, column, page);
370                 nfc->buf_offset = 0;
371
372                 /*
373                  * SEQIN => data => PAGEPROG sequence is done by the controller
374                  * hence we do not need to issue the command here...
375                  */
376                 return;
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);
382                 if (nfc->use_hw_ecc)
383                         vf610_nfc_ecc_mode(nfc, nfc->ecc_mode);
384                 else
385                         vf610_nfc_ecc_mode(nfc, ECC_BYPASS);
386                 break;
387
388         case NAND_CMD_RESET:
389                 vf610_nfc_transfer_size(nfc, 0);
390                 vf610_nfc_send_command(nfc, command, RESET_CMD_CODE);
391                 break;
392
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);
401                 break;
402
403         case NAND_CMD_READ0:
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);
410                 break;
411
412         case NAND_CMD_PARAM:
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);
419                 break;
420
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);
426                 break;
427
428         case NAND_CMD_READID:
429                 nfc->alt_buf = ALT_BUF_ID;
430                 nfc->buf_offset = 0;
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);
434                 break;
435
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);
440                 break;
441         default:
442                 return;
443         }
444
445         vf610_nfc_done(nfc);
446
447         nfc->use_hw_ecc = false;
448         nfc->write_sz = 0;
449 }
450
451 static void vf610_nfc_read_buf(struct mtd_info *mtd, u_char *buf, int len)
452 {
453         struct vf610_nfc *nfc = mtd_to_nfc(mtd);
454         uint c = nfc->buf_offset;
455
456         /* Alternate buffers are only supported through read_byte */
457         WARN_ON(nfc->alt_buf);
458
459         vf610_nfc_memcpy(buf, nfc->regs + NFC_MAIN_AREA(0) + c, len);
460
461         nfc->buf_offset += len;
462 }
463
464 static void vf610_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
465                                 int len)
466 {
467         struct vf610_nfc *nfc = mtd_to_nfc(mtd);
468         uint c = nfc->buf_offset;
469         uint l;
470
471         l = min_t(uint, len, mtd->writesize + mtd->oobsize - c);
472         vf610_nfc_memcpy(nfc->regs + NFC_MAIN_AREA(0) + c, buf, l);
473
474         nfc->write_sz += l;
475         nfc->buf_offset += l;
476 }
477
478 static uint8_t vf610_nfc_read_byte(struct mtd_info *mtd)
479 {
480         struct vf610_nfc *nfc = mtd_to_nfc(mtd);
481         u8 tmp;
482         uint c = nfc->buf_offset;
483
484         switch (nfc->alt_buf) {
485         case ALT_BUF_ID:
486                 tmp = vf610_nfc_get_id(nfc, c);
487                 break;
488         case ALT_BUF_STAT:
489                 tmp = vf610_nfc_get_status(nfc);
490                 break;
491 #ifdef __LITTLE_ENDIAN
492         case ALT_BUF_ONFI:
493                 /* Reverse byte since the controller uses big endianness */
494                 c = nfc->buf_offset ^ 0x3;
495                 /* fall-through */
496 #endif
497         default:
498                 tmp = *((u8 *)(nfc->regs + NFC_MAIN_AREA(0) + c));
499                 break;
500         }
501         nfc->buf_offset++;
502         return tmp;
503 }
504
505 static u16 vf610_nfc_read_word(struct mtd_info *mtd)
506 {
507         u16 tmp;
508
509         vf610_nfc_read_buf(mtd, (u_char *)&tmp, sizeof(tmp));
510         return tmp;
511 }
512
513 /* If not provided, upper layers apply a fixed delay. */
514 static int vf610_nfc_dev_ready(struct mtd_info *mtd)
515 {
516         /* NFC handles R/B internally; always ready.  */
517         return 1;
518 }
519
520 /*
521  * This function supports Vybrid only (MPC5125 would have full RB and four CS)
522  */
523 static void vf610_nfc_select_chip(struct mtd_info *mtd, int chip)
524 {
525         struct vf610_nfc *nfc = mtd_to_nfc(mtd);
526         u32 tmp = vf610_nfc_read(nfc, NFC_ROW_ADDR);
527
528         /* Vybrid only (MPC5125 would have full RB and four CS) */
529         if (nfc->variant != NFC_VFC610)
530                 return;
531
532         tmp &= ~(ROW_ADDR_CHIP_SEL_RB_MASK | ROW_ADDR_CHIP_SEL_MASK);
533
534         if (chip >= 0) {
535                 tmp |= 1 << ROW_ADDR_CHIP_SEL_RB_SHIFT;
536                 tmp |= BIT(chip) << ROW_ADDR_CHIP_SEL_SHIFT;
537         }
538
539         vf610_nfc_write(nfc, NFC_ROW_ADDR, tmp);
540 }
541
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)
544 {
545         uint32_t *buff32 = (uint32_t *)buff;
546         int k, written_bits = 0;
547
548         for (k = 0; k < (size / 4); k++) {
549                 written_bits += hweight32(~buff32[k]);
550                 if (unlikely(written_bits > max_bits))
551                         break;
552         }
553
554         return written_bits;
555 }
556
557 static inline int vf610_nfc_correct_data(struct mtd_info *mtd, uint8_t *dat,
558                                          uint8_t *oob, int page)
559 {
560         struct vf610_nfc *nfc = mtd_to_nfc(mtd);
561         u32 ecc_status_off = NFC_MAIN_AREA(0) + ECC_SRAM_ADDR + ECC_STATUS;
562         u8 ecc_status;
563         u8 ecc_count;
564         int flips;
565         int flips_threshold = nfc->chip.ecc.strength / 2;
566
567         ecc_status = vf610_nfc_read(nfc, ecc_status_off) & 0xff;
568         ecc_count = ecc_status & ECC_STATUS_ERR_COUNT;
569
570         if (!(ecc_status & ECC_STATUS_MASK))
571                 return ecc_count;
572
573         /* Read OOB without ECC unit enabled */
574         vf610_nfc_command(mtd, NAND_CMD_READOOB, 0, page);
575         vf610_nfc_read_buf(mtd, oob, mtd->oobsize);
576
577         /*
578          * On an erased page, bit count (including OOB) should be zero or
579          * at least less then half of the ECC strength.
580          */
581         flips = count_written_bits(dat, nfc->chip.ecc.size, flips_threshold);
582         flips += count_written_bits(oob, mtd->oobsize, flips_threshold);
583
584         if (unlikely(flips > flips_threshold))
585                 return -EINVAL;
586
587         /* Erased page. */
588         memset(dat, 0xff, nfc->chip.ecc.size);
589         memset(oob, 0xff, mtd->oobsize);
590         return flips;
591 }
592
593 static int vf610_nfc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
594                                 uint8_t *buf, int oob_required, int page)
595 {
596         int eccsize = chip->ecc.size;
597         int stat;
598
599         vf610_nfc_read_buf(mtd, buf, eccsize);
600         if (oob_required)
601                 vf610_nfc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
602
603         stat = vf610_nfc_correct_data(mtd, buf, chip->oob_poi, page);
604
605         if (stat < 0) {
606                 mtd->ecc_stats.failed++;
607                 return 0;
608         } else {
609                 mtd->ecc_stats.corrected += stat;
610                 return stat;
611         }
612 }
613
614 static int vf610_nfc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
615                                 const uint8_t *buf, int oob_required, int page)
616 {
617         struct vf610_nfc *nfc = mtd_to_nfc(mtd);
618
619         vf610_nfc_write_buf(mtd, buf, mtd->writesize);
620         if (oob_required)
621                 vf610_nfc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
622
623         /* Always write whole page including OOB due to HW ECC */
624         nfc->use_hw_ecc = true;
625         nfc->write_sz = mtd->writesize + mtd->oobsize;
626
627         return 0;
628 }
629
630 static const struct of_device_id vf610_nfc_dt_ids[] = {
631         { .compatible = "fsl,vf610-nfc", .data = (void *)NFC_VFC610 },
632         { /* sentinel */ }
633 };
634 MODULE_DEVICE_TABLE(of, vf610_nfc_dt_ids);
635
636 static void vf610_nfc_preinit_controller(struct vf610_nfc *nfc)
637 {
638         vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
639         vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_ADDR_AUTO_INCR_BIT);
640         vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_BUFNO_AUTO_INCR_BIT);
641         vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_BOOT_MODE_BIT);
642         vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_DMA_REQ_BIT);
643         vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_FAST_FLASH_BIT);
644
645         /* Disable virtual pages, only one elementary transfer unit */
646         vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG, CONFIG_PAGE_CNT_MASK,
647                             CONFIG_PAGE_CNT_SHIFT, 1);
648 }
649
650 static void vf610_nfc_init_controller(struct vf610_nfc *nfc)
651 {
652         if (nfc->chip.options & NAND_BUSWIDTH_16)
653                 vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
654         else
655                 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
656
657         if (nfc->chip.ecc.mode == NAND_ECC_HW) {
658                 /* Set ECC status offset in SRAM */
659                 vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG,
660                                     CONFIG_ECC_SRAM_ADDR_MASK,
661                                     CONFIG_ECC_SRAM_ADDR_SHIFT,
662                                     ECC_SRAM_ADDR >> 3);
663
664                 /* Enable ECC status in SRAM */
665                 vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_ECC_SRAM_REQ_BIT);
666         }
667 }
668
669 static int vf610_nfc_probe(struct platform_device *pdev)
670 {
671         struct vf610_nfc *nfc;
672         struct resource *res;
673         struct mtd_info *mtd;
674         struct nand_chip *chip;
675         struct device_node *child;
676         const struct of_device_id *of_id;
677         int err;
678         int irq;
679
680         nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
681         if (!nfc)
682                 return -ENOMEM;
683
684         nfc->dev = &pdev->dev;
685         mtd = &nfc->mtd;
686         chip = &nfc->chip;
687
688         mtd->priv = chip;
689         mtd->owner = THIS_MODULE;
690         mtd->dev.parent = nfc->dev;
691         mtd->name = DRV_NAME;
692
693         irq = platform_get_irq(pdev, 0);
694         if (irq <= 0)
695                 return -EINVAL;
696
697         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
698         nfc->regs = devm_ioremap_resource(nfc->dev, res);
699         if (IS_ERR(nfc->regs))
700                 return PTR_ERR(nfc->regs);
701
702         nfc->clk = devm_clk_get(&pdev->dev, NULL);
703         if (IS_ERR(nfc->clk))
704                 return PTR_ERR(nfc->clk);
705
706         err = clk_prepare_enable(nfc->clk);
707         if (err) {
708                 dev_err(nfc->dev, "Unable to enable clock!\n");
709                 return err;
710         }
711
712         of_id = of_match_device(vf610_nfc_dt_ids, &pdev->dev);
713         nfc->variant = (enum vf610_nfc_variant)of_id->data;
714
715         for_each_available_child_of_node(nfc->dev->of_node, child) {
716                 if (of_device_is_compatible(child, "fsl,vf610-nfc-nandcs")) {
717
718                         if (chip->flash_node) {
719                                 dev_err(nfc->dev,
720                                         "Only one NAND chip supported!\n");
721                                 err = -EINVAL;
722                                 goto error;
723                         }
724
725                         chip->flash_node = child;
726                 }
727         }
728
729         if (!chip->flash_node) {
730                 dev_err(nfc->dev, "NAND chip sub-node missing!\n");
731                 err = -ENODEV;
732                 goto err_clk;
733         }
734
735         chip->dev_ready = vf610_nfc_dev_ready;
736         chip->cmdfunc = vf610_nfc_command;
737         chip->read_byte = vf610_nfc_read_byte;
738         chip->read_word = vf610_nfc_read_word;
739         chip->read_buf = vf610_nfc_read_buf;
740         chip->write_buf = vf610_nfc_write_buf;
741         chip->select_chip = vf610_nfc_select_chip;
742
743         chip->options |= NAND_NO_SUBPAGE_WRITE;
744
745         init_completion(&nfc->cmd_done);
746
747         err = devm_request_irq(nfc->dev, irq, vf610_nfc_irq, 0, DRV_NAME, mtd);
748         if (err) {
749                 dev_err(nfc->dev, "Error requesting IRQ!\n");
750                 goto error;
751         }
752
753         vf610_nfc_preinit_controller(nfc);
754
755         /* first scan to find the device and get the page size */
756         if (nand_scan_ident(mtd, 1, NULL)) {
757                 err = -ENXIO;
758                 goto error;
759         }
760
761         vf610_nfc_init_controller(nfc);
762
763         /* Bad block options. */
764         if (chip->bbt_options & NAND_BBT_USE_FLASH)
765                 chip->bbt_options |= NAND_BBT_NO_OOB;
766
767         /* Single buffer only, max 256 OOB minus ECC status */
768         if (mtd->writesize + mtd->oobsize > PAGE_2K + OOB_MAX - 8) {
769                 dev_err(nfc->dev, "Unsupported flash page size\n");
770                 err = -ENXIO;
771                 goto error;
772         }
773
774         if (chip->ecc.mode == NAND_ECC_HW) {
775                 if (mtd->writesize != PAGE_2K && mtd->oobsize < 64) {
776                         dev_err(nfc->dev, "Unsupported flash with hwecc\n");
777                         err = -ENXIO;
778                         goto error;
779                 }
780
781                 if (chip->ecc.size != mtd->writesize) {
782                         dev_err(nfc->dev, "Step size needs to be page size\n");
783                         err = -ENXIO;
784                         goto error;
785                 }
786
787                 /* Only 64 byte ECC layouts known */
788                 if (mtd->oobsize > 64)
789                         mtd->oobsize = 64;
790
791                 if (chip->ecc.strength == 32) {
792                         nfc->ecc_mode = ECC_60_BYTE;
793                         chip->ecc.bytes = 60;
794                         chip->ecc.layout = &vf610_nfc_ecc60;
795                 } else if (chip->ecc.strength == 24) {
796                         nfc->ecc_mode = ECC_45_BYTE;
797                         chip->ecc.bytes = 45;
798                         chip->ecc.layout = &vf610_nfc_ecc45;
799                 } else {
800                         dev_err(nfc->dev, "Unsupported ECC strength\n");
801                         err = -ENXIO;
802                         goto error;
803                 }
804
805                 /* propagate ecc.layout to mtd_info */
806                 mtd->ecclayout = chip->ecc.layout;
807                 chip->ecc.read_page = vf610_nfc_read_page;
808                 chip->ecc.write_page = vf610_nfc_write_page;
809
810                 chip->ecc.size = PAGE_2K;
811         }
812
813         /* second phase scan */
814         if (nand_scan_tail(mtd)) {
815                 err = -ENXIO;
816                 goto error;
817         }
818
819         platform_set_drvdata(pdev, mtd);
820
821         /* Register device in MTD */
822         return mtd_device_parse_register(mtd, NULL,
823                 &(struct mtd_part_parser_data){
824                         .of_node = chip->flash_node,
825                 },
826                 NULL, 0);
827
828 error:
829         of_node_put(chip->flash_node);
830 err_clk:
831         clk_disable_unprepare(nfc->clk);
832         return err;
833 }
834
835 static int vf610_nfc_remove(struct platform_device *pdev)
836 {
837         struct mtd_info *mtd = platform_get_drvdata(pdev);
838         struct vf610_nfc *nfc = mtd_to_nfc(mtd);
839
840         nand_release(mtd);
841         clk_disable_unprepare(nfc->clk);
842         return 0;
843 }
844
845 #ifdef CONFIG_PM_SLEEP
846 static int vf610_nfc_suspend(struct device *dev)
847 {
848         struct mtd_info *mtd = dev_get_drvdata(dev);
849         struct vf610_nfc *nfc = mtd_to_nfc(mtd);
850
851         clk_disable_unprepare(nfc->clk);
852         return 0;
853 }
854
855 static int vf610_nfc_resume(struct device *dev)
856 {
857         struct mtd_info *mtd = dev_get_drvdata(dev);
858         struct vf610_nfc *nfc = mtd_to_nfc(mtd);
859
860         pinctrl_pm_select_default_state(dev);
861
862         clk_prepare_enable(nfc->clk);
863
864         vf610_nfc_preinit_controller(nfc);
865         vf610_nfc_init_controller(nfc);
866         return 0;
867 }
868 #endif
869
870 static SIMPLE_DEV_PM_OPS(vf610_nfc_pm_ops, vf610_nfc_suspend, vf610_nfc_resume);
871
872 static struct platform_driver vf610_nfc_driver = {
873         .driver         = {
874                 .name   = DRV_NAME,
875                 .of_match_table = vf610_nfc_dt_ids,
876                 .pm     = &vf610_nfc_pm_ops,
877         },
878         .probe          = vf610_nfc_probe,
879         .remove         = vf610_nfc_remove,
880 };
881
882 module_platform_driver(vf610_nfc_driver);
883
884 MODULE_AUTHOR("Stefan Agner <stefan.agner@toradex.com>");
885 MODULE_DESCRIPTION("Freescale VF610/MPC5125 NFC MTD NAND driver");
886 MODULE_LICENSE("GPL");