blob: 5d7a1f8f580f80825aa9faab662a857979d14b0c [file] [log] [blame]
Stefan Agner456930d2015-09-02 18:06:33 -07001/*
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.
Stefan Agner049f4252015-09-02 18:06:34 -070022 * - HW ECC: Only 2K page with 64+ OOB.
23 * - HW ECC: Only 24 and 32-bit error correction implemented.
Stefan Agner456930d2015-09-02 18:06:33 -070024 */
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>
Boris Brezillond4092d72017-08-04 17:29:10 +020034#include <linux/mtd/rawnand.h>
Stefan Agner456930d2015-09-02 18:06:33 -070035#include <linux/mtd/partitions.h>
Stefan Agner456930d2015-09-02 18:06:33 -070036#include <linux/of_device.h>
37#include <linux/platform_device.h>
38#include <linux/slab.h>
39
40#define DRV_NAME "vf610_nfc"
41
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
54
55/* Addresses for NFC MAIN RAM BUFFER areas */
56#define NFC_MAIN_AREA(n) ((n) * 0x1000)
57
58#define PAGE_2K 0x0800
59#define OOB_64 0x0040
60#define OOB_MAX 0x0100
61
62/*
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
66 *
67 * Briefly these are bitmasks of controller cycles.
68 */
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
76
77/* NFC ECC mode define */
78#define ECC_BYPASS 0
Stefan Agner049f4252015-09-02 18:06:34 -070079#define ECC_45_BYTE 6
80#define ECC_60_BYTE 7
Stefan Agner456930d2015-09-02 18:06:33 -070081
82/*** Register Mask and bit definitions */
83
84/* NFC_FLASH_CMD1 Field */
85#define CMD_BYTE2_MASK 0xFF000000
86#define CMD_BYTE2_SHIFT 24
87
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
94#define BUFNO_SHIFT 1
95#define START_BIT BIT(0)
96
97/* NFC_COL_ADDR Field */
98#define COL_ADDR_MASK 0x0000FFFF
99#define COL_ADDR_SHIFT 0
100
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
108
109/* NFC_FLASH_STATUS2 Field */
110#define STATUS_BYTE1_MASK 0x000000FF
111
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
126
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)
132
Stefan Agner049f4252015-09-02 18:06:34 -0700133/*
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.
138 */
139#define ECC_SRAM_ADDR (PAGE_2K + OOB_MAX - 8)
140
141#define ECC_STATUS 0x4
142#define ECC_STATUS_MASK 0x80
143#define ECC_STATUS_ERR_COUNT 0x3F
144
Stefan Agner456930d2015-09-02 18:06:33 -0700145enum vf610_nfc_alt_buf {
146 ALT_BUF_DATA = 0,
147 ALT_BUF_ID = 1,
148 ALT_BUF_STAT = 2,
149 ALT_BUF_ONFI = 3,
150};
151
152enum vf610_nfc_variant {
153 NFC_VFC610 = 1,
154};
155
156struct vf610_nfc {
Stefan Agner456930d2015-09-02 18:06:33 -0700157 struct nand_chip chip;
158 struct device *dev;
159 void __iomem *regs;
160 struct completion cmd_done;
161 uint buf_offset;
162 int write_sz;
163 /* Status and ID are in alternate locations. */
164 enum vf610_nfc_alt_buf alt_buf;
165 enum vf610_nfc_variant variant;
166 struct clk *clk;
Stefan Agner049f4252015-09-02 18:06:34 -0700167 bool use_hw_ecc;
168 u32 ecc_mode;
Stefan Agner456930d2015-09-02 18:06:33 -0700169};
170
Boris BREZILLON960823a2015-12-10 09:00:29 +0100171static inline struct vf610_nfc *mtd_to_nfc(struct mtd_info *mtd)
172{
173 return container_of(mtd_to_nand(mtd), struct vf610_nfc, chip);
174}
Stefan Agner456930d2015-09-02 18:06:33 -0700175
176static inline u32 vf610_nfc_read(struct vf610_nfc *nfc, uint reg)
177{
178 return readl(nfc->regs + reg);
179}
180
181static inline void vf610_nfc_write(struct vf610_nfc *nfc, uint reg, u32 val)
182{
183 writel(val, nfc->regs + reg);
184}
185
186static inline void vf610_nfc_set(struct vf610_nfc *nfc, uint reg, u32 bits)
187{
188 vf610_nfc_write(nfc, reg, vf610_nfc_read(nfc, reg) | bits);
189}
190
191static inline void vf610_nfc_clear(struct vf610_nfc *nfc, uint reg, u32 bits)
192{
193 vf610_nfc_write(nfc, reg, vf610_nfc_read(nfc, reg) & ~bits);
194}
195
196static inline void vf610_nfc_set_field(struct vf610_nfc *nfc, u32 reg,
197 u32 mask, u32 shift, u32 val)
198{
199 vf610_nfc_write(nfc, reg,
200 (vf610_nfc_read(nfc, reg) & (~mask)) | val << shift);
201}
202
203static inline void vf610_nfc_memcpy(void *dst, const void __iomem *src,
204 size_t n)
205{
206 /*
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.
211 *
212 * For the time being, use memcpy
213 */
214 memcpy(dst, src, n);
215}
216
217/* Clear flags for upcoming command */
218static inline void vf610_nfc_clear_status(struct vf610_nfc *nfc)
219{
220 u32 tmp = vf610_nfc_read(nfc, NFC_IRQ_STATUS);
221
222 tmp |= CMD_DONE_CLEAR_BIT | IDLE_CLEAR_BIT;
223 vf610_nfc_write(nfc, NFC_IRQ_STATUS, tmp);
224}
225
226static void vf610_nfc_done(struct vf610_nfc *nfc)
227{
228 unsigned long timeout = msecs_to_jiffies(100);
229
230 /*
231 * Barrier is needed after this write. This write need
232 * to be done before reading the next register the first
233 * time.
234 * vf610_nfc_set implicates such a barrier by using writel
235 * to write to the register.
236 */
237 vf610_nfc_set(nfc, NFC_IRQ_STATUS, IDLE_EN_BIT);
238 vf610_nfc_set(nfc, NFC_FLASH_CMD2, START_BIT);
239
240 if (!wait_for_completion_timeout(&nfc->cmd_done, timeout))
241 dev_warn(nfc->dev, "Timeout while waiting for BUSY.\n");
242
243 vf610_nfc_clear_status(nfc);
244}
245
246static u8 vf610_nfc_get_id(struct vf610_nfc *nfc, int col)
247{
248 u32 flash_id;
249
250 if (col < 4) {
251 flash_id = vf610_nfc_read(nfc, NFC_FLASH_STATUS1);
252 flash_id >>= (3 - col) * 8;
253 } else {
254 flash_id = vf610_nfc_read(nfc, NFC_FLASH_STATUS2);
255 flash_id >>= 24;
256 }
257
258 return flash_id & 0xff;
259}
260
261static u8 vf610_nfc_get_status(struct vf610_nfc *nfc)
262{
263 return vf610_nfc_read(nfc, NFC_FLASH_STATUS2) & STATUS_BYTE1_MASK;
264}
265
266static void vf610_nfc_send_command(struct vf610_nfc *nfc, u32 cmd_byte1,
267 u32 cmd_code)
268{
269 u32 tmp;
270
271 vf610_nfc_clear_status(nfc);
272
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);
278}
279
280static void vf610_nfc_send_commands(struct vf610_nfc *nfc, u32 cmd_byte1,
281 u32 cmd_byte2, u32 cmd_code)
282{
283 u32 tmp;
284
285 vf610_nfc_send_command(nfc, cmd_byte1, cmd_code);
286
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);
291}
292
293static irqreturn_t vf610_nfc_irq(int irq, void *data)
294{
295 struct mtd_info *mtd = data;
296 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
297
298 vf610_nfc_clear(nfc, NFC_IRQ_STATUS, IDLE_EN_BIT);
299 complete(&nfc->cmd_done);
300
301 return IRQ_HANDLED;
302}
303
304static void vf610_nfc_addr_cycle(struct vf610_nfc *nfc, int column, int page)
305{
306 if (column != -1) {
307 if (nfc->chip.options & NAND_BUSWIDTH_16)
308 column = column / 2;
309 vf610_nfc_set_field(nfc, NFC_COL_ADDR, COL_ADDR_MASK,
310 COL_ADDR_SHIFT, column);
311 }
312 if (page != -1)
313 vf610_nfc_set_field(nfc, NFC_ROW_ADDR, ROW_ADDR_MASK,
314 ROW_ADDR_SHIFT, page);
315}
316
Stefan Agner049f4252015-09-02 18:06:34 -0700317static inline void vf610_nfc_ecc_mode(struct vf610_nfc *nfc, int ecc_mode)
318{
319 vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG,
320 CONFIG_ECC_MODE_MASK,
321 CONFIG_ECC_MODE_SHIFT, ecc_mode);
322}
323
Stefan Agner456930d2015-09-02 18:06:33 -0700324static inline void vf610_nfc_transfer_size(struct vf610_nfc *nfc, int size)
325{
326 vf610_nfc_write(nfc, NFC_SECTOR_SIZE, size);
327}
328
329static void vf610_nfc_command(struct mtd_info *mtd, unsigned command,
330 int column, int page)
331{
332 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
333 int trfr_sz = nfc->chip.options & NAND_BUSWIDTH_16 ? 1 : 0;
334
335 nfc->buf_offset = max(column, 0);
336 nfc->alt_buf = ALT_BUF_DATA;
337
338 switch (command) {
339 case NAND_CMD_SEQIN:
340 /* Use valid column/page from preread... */
341 vf610_nfc_addr_cycle(nfc, column, page);
Stefan Agner049f4252015-09-02 18:06:34 -0700342 nfc->buf_offset = 0;
343
Stefan Agner456930d2015-09-02 18:06:33 -0700344 /*
345 * SEQIN => data => PAGEPROG sequence is done by the controller
346 * hence we do not need to issue the command here...
347 */
348 return;
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);
Stefan Agner049f4252015-09-02 18:06:34 -0700354 if (nfc->use_hw_ecc)
355 vf610_nfc_ecc_mode(nfc, nfc->ecc_mode);
356 else
357 vf610_nfc_ecc_mode(nfc, ECC_BYPASS);
Stefan Agner456930d2015-09-02 18:06:33 -0700358 break;
359
360 case NAND_CMD_RESET:
361 vf610_nfc_transfer_size(nfc, 0);
362 vf610_nfc_send_command(nfc, command, RESET_CMD_CODE);
363 break;
364
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);
Stefan Agner049f4252015-09-02 18:06:34 -0700372 vf610_nfc_ecc_mode(nfc, ECC_BYPASS);
Stefan Agner456930d2015-09-02 18:06:33 -0700373 break;
374
375 case NAND_CMD_READ0:
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);
Stefan Agner049f4252015-09-02 18:06:34 -0700381 vf610_nfc_ecc_mode(nfc, nfc->ecc_mode);
Stefan Agner456930d2015-09-02 18:06:33 -0700382 break;
383
384 case NAND_CMD_PARAM:
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);
Stefan Agner049f4252015-09-02 18:06:34 -0700390 vf610_nfc_ecc_mode(nfc, ECC_BYPASS);
Stefan Agner456930d2015-09-02 18:06:33 -0700391 break;
392
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);
398 break;
399
400 case NAND_CMD_READID:
401 nfc->alt_buf = ALT_BUF_ID;
402 nfc->buf_offset = 0;
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);
406 break;
407
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);
412 break;
413 default:
414 return;
415 }
416
417 vf610_nfc_done(nfc);
418
Stefan Agner049f4252015-09-02 18:06:34 -0700419 nfc->use_hw_ecc = false;
Stefan Agner456930d2015-09-02 18:06:33 -0700420 nfc->write_sz = 0;
421}
422
423static void vf610_nfc_read_buf(struct mtd_info *mtd, u_char *buf, int len)
424{
425 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
426 uint c = nfc->buf_offset;
427
428 /* Alternate buffers are only supported through read_byte */
429 WARN_ON(nfc->alt_buf);
430
431 vf610_nfc_memcpy(buf, nfc->regs + NFC_MAIN_AREA(0) + c, len);
432
433 nfc->buf_offset += len;
434}
435
436static void vf610_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
437 int len)
438{
439 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
440 uint c = nfc->buf_offset;
441 uint l;
442
443 l = min_t(uint, len, mtd->writesize + mtd->oobsize - c);
444 vf610_nfc_memcpy(nfc->regs + NFC_MAIN_AREA(0) + c, buf, l);
445
446 nfc->write_sz += l;
447 nfc->buf_offset += l;
448}
449
450static uint8_t vf610_nfc_read_byte(struct mtd_info *mtd)
451{
452 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
453 u8 tmp;
454 uint c = nfc->buf_offset;
455
456 switch (nfc->alt_buf) {
457 case ALT_BUF_ID:
458 tmp = vf610_nfc_get_id(nfc, c);
459 break;
460 case ALT_BUF_STAT:
461 tmp = vf610_nfc_get_status(nfc);
462 break;
463#ifdef __LITTLE_ENDIAN
464 case ALT_BUF_ONFI:
465 /* Reverse byte since the controller uses big endianness */
466 c = nfc->buf_offset ^ 0x3;
467 /* fall-through */
468#endif
469 default:
470 tmp = *((u8 *)(nfc->regs + NFC_MAIN_AREA(0) + c));
471 break;
472 }
473 nfc->buf_offset++;
474 return tmp;
475}
476
477static u16 vf610_nfc_read_word(struct mtd_info *mtd)
478{
479 u16 tmp;
480
481 vf610_nfc_read_buf(mtd, (u_char *)&tmp, sizeof(tmp));
482 return tmp;
483}
484
485/* If not provided, upper layers apply a fixed delay. */
486static int vf610_nfc_dev_ready(struct mtd_info *mtd)
487{
488 /* NFC handles R/B internally; always ready. */
489 return 1;
490}
491
492/*
493 * This function supports Vybrid only (MPC5125 would have full RB and four CS)
494 */
495static void vf610_nfc_select_chip(struct mtd_info *mtd, int chip)
496{
497 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
498 u32 tmp = vf610_nfc_read(nfc, NFC_ROW_ADDR);
499
500 /* Vybrid only (MPC5125 would have full RB and four CS) */
501 if (nfc->variant != NFC_VFC610)
502 return;
503
504 tmp &= ~(ROW_ADDR_CHIP_SEL_RB_MASK | ROW_ADDR_CHIP_SEL_MASK);
505
506 if (chip >= 0) {
507 tmp |= 1 << ROW_ADDR_CHIP_SEL_RB_SHIFT;
508 tmp |= BIT(chip) << ROW_ADDR_CHIP_SEL_SHIFT;
509 }
510
511 vf610_nfc_write(nfc, NFC_ROW_ADDR, tmp);
512}
513
Stefan Agner049f4252015-09-02 18:06:34 -0700514static inline int vf610_nfc_correct_data(struct mtd_info *mtd, uint8_t *dat,
515 uint8_t *oob, int page)
516{
517 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
518 u32 ecc_status_off = NFC_MAIN_AREA(0) + ECC_SRAM_ADDR + ECC_STATUS;
519 u8 ecc_status;
520 u8 ecc_count;
Stefan Agner049f4252015-09-02 18:06:34 -0700521 int flips_threshold = nfc->chip.ecc.strength / 2;
522
523 ecc_status = vf610_nfc_read(nfc, ecc_status_off) & 0xff;
524 ecc_count = ecc_status & ECC_STATUS_ERR_COUNT;
525
526 if (!(ecc_status & ECC_STATUS_MASK))
527 return ecc_count;
528
529 /* Read OOB without ECC unit enabled */
530 vf610_nfc_command(mtd, NAND_CMD_READOOB, 0, page);
531 vf610_nfc_read_buf(mtd, oob, mtd->oobsize);
532
533 /*
534 * On an erased page, bit count (including OOB) should be zero or
535 * at least less then half of the ECC strength.
536 */
Brian Norris48c25cf2015-09-29 14:11:56 -0700537 return nand_check_erased_ecc_chunk(dat, nfc->chip.ecc.size, oob,
538 mtd->oobsize, NULL, 0,
539 flips_threshold);
Stefan Agner049f4252015-09-02 18:06:34 -0700540}
541
542static int vf610_nfc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
543 uint8_t *buf, int oob_required, int page)
544{
545 int eccsize = chip->ecc.size;
546 int stat;
547
Boris Brezillon25f815f2017-11-30 18:01:30 +0100548 nand_read_page_op(chip, page, 0, buf, eccsize);
Stefan Agner049f4252015-09-02 18:06:34 -0700549 if (oob_required)
550 vf610_nfc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
551
552 stat = vf610_nfc_correct_data(mtd, buf, chip->oob_poi, page);
553
554 if (stat < 0) {
555 mtd->ecc_stats.failed++;
556 return 0;
557 } else {
558 mtd->ecc_stats.corrected += stat;
559 return stat;
560 }
561}
562
563static int vf610_nfc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +0200564 const uint8_t *buf, int oob_required, int page)
Stefan Agner049f4252015-09-02 18:06:34 -0700565{
566 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
567
Boris Brezillon25f815f2017-11-30 18:01:30 +0100568 nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Stefan Agner049f4252015-09-02 18:06:34 -0700569 if (oob_required)
570 vf610_nfc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
571
572 /* Always write whole page including OOB due to HW ECC */
573 nfc->use_hw_ecc = true;
574 nfc->write_sz = mtd->writesize + mtd->oobsize;
575
Boris Brezillon25f815f2017-11-30 18:01:30 +0100576 return nand_prog_page_end_op(chip);
Stefan Agner049f4252015-09-02 18:06:34 -0700577}
578
Stefan Agner456930d2015-09-02 18:06:33 -0700579static const struct of_device_id vf610_nfc_dt_ids[] = {
580 { .compatible = "fsl,vf610-nfc", .data = (void *)NFC_VFC610 },
581 { /* sentinel */ }
582};
583MODULE_DEVICE_TABLE(of, vf610_nfc_dt_ids);
584
585static void vf610_nfc_preinit_controller(struct vf610_nfc *nfc)
586{
587 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
588 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_ADDR_AUTO_INCR_BIT);
589 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_BUFNO_AUTO_INCR_BIT);
590 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_BOOT_MODE_BIT);
591 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_DMA_REQ_BIT);
592 vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_FAST_FLASH_BIT);
593
594 /* Disable virtual pages, only one elementary transfer unit */
595 vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG, CONFIG_PAGE_CNT_MASK,
596 CONFIG_PAGE_CNT_SHIFT, 1);
597}
598
599static void vf610_nfc_init_controller(struct vf610_nfc *nfc)
600{
601 if (nfc->chip.options & NAND_BUSWIDTH_16)
602 vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
603 else
604 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
Stefan Agner049f4252015-09-02 18:06:34 -0700605
606 if (nfc->chip.ecc.mode == NAND_ECC_HW) {
607 /* Set ECC status offset in SRAM */
608 vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG,
609 CONFIG_ECC_SRAM_ADDR_MASK,
610 CONFIG_ECC_SRAM_ADDR_SHIFT,
611 ECC_SRAM_ADDR >> 3);
612
613 /* Enable ECC status in SRAM */
614 vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_ECC_SRAM_REQ_BIT);
615 }
Stefan Agner456930d2015-09-02 18:06:33 -0700616}
617
618static int vf610_nfc_probe(struct platform_device *pdev)
619{
620 struct vf610_nfc *nfc;
621 struct resource *res;
622 struct mtd_info *mtd;
623 struct nand_chip *chip;
624 struct device_node *child;
625 const struct of_device_id *of_id;
626 int err;
627 int irq;
628
629 nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
630 if (!nfc)
631 return -ENOMEM;
632
633 nfc->dev = &pdev->dev;
Stefan Agner456930d2015-09-02 18:06:33 -0700634 chip = &nfc->chip;
Boris BREZILLON960823a2015-12-10 09:00:29 +0100635 mtd = nand_to_mtd(chip);
Stefan Agner456930d2015-09-02 18:06:33 -0700636
Stefan Agner456930d2015-09-02 18:06:33 -0700637 mtd->owner = THIS_MODULE;
638 mtd->dev.parent = nfc->dev;
639 mtd->name = DRV_NAME;
640
641 irq = platform_get_irq(pdev, 0);
642 if (irq <= 0)
643 return -EINVAL;
644
645 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
646 nfc->regs = devm_ioremap_resource(nfc->dev, res);
647 if (IS_ERR(nfc->regs))
648 return PTR_ERR(nfc->regs);
649
650 nfc->clk = devm_clk_get(&pdev->dev, NULL);
651 if (IS_ERR(nfc->clk))
652 return PTR_ERR(nfc->clk);
653
654 err = clk_prepare_enable(nfc->clk);
655 if (err) {
656 dev_err(nfc->dev, "Unable to enable clock!\n");
657 return err;
658 }
659
660 of_id = of_match_device(vf610_nfc_dt_ids, &pdev->dev);
661 nfc->variant = (enum vf610_nfc_variant)of_id->data;
662
663 for_each_available_child_of_node(nfc->dev->of_node, child) {
664 if (of_device_is_compatible(child, "fsl,vf610-nfc-nandcs")) {
665
Boris BREZILLON44ec23c2015-11-02 00:03:38 +0100666 if (nand_get_flash_node(chip)) {
Stefan Agner456930d2015-09-02 18:06:33 -0700667 dev_err(nfc->dev,
668 "Only one NAND chip supported!\n");
669 err = -EINVAL;
Alexey Khoroshilov196644f2018-02-10 01:28:35 +0300670 goto err_disable_clk;
Stefan Agner456930d2015-09-02 18:06:33 -0700671 }
672
Brian Norris63752192015-10-30 20:33:23 -0700673 nand_set_flash_node(chip, child);
Stefan Agner456930d2015-09-02 18:06:33 -0700674 }
675 }
676
Boris BREZILLON44ec23c2015-11-02 00:03:38 +0100677 if (!nand_get_flash_node(chip)) {
Stefan Agner456930d2015-09-02 18:06:33 -0700678 dev_err(nfc->dev, "NAND chip sub-node missing!\n");
679 err = -ENODEV;
Alexey Khoroshilov196644f2018-02-10 01:28:35 +0300680 goto err_disable_clk;
Stefan Agner456930d2015-09-02 18:06:33 -0700681 }
682
683 chip->dev_ready = vf610_nfc_dev_ready;
684 chip->cmdfunc = vf610_nfc_command;
685 chip->read_byte = vf610_nfc_read_byte;
686 chip->read_word = vf610_nfc_read_word;
687 chip->read_buf = vf610_nfc_read_buf;
688 chip->write_buf = vf610_nfc_write_buf;
689 chip->select_chip = vf610_nfc_select_chip;
Boris Brezillon4a78cc62017-05-26 17:10:15 +0200690 chip->onfi_set_features = nand_onfi_get_set_features_notsupp;
691 chip->onfi_get_features = nand_onfi_get_set_features_notsupp;
Stefan Agner456930d2015-09-02 18:06:33 -0700692
693 chip->options |= NAND_NO_SUBPAGE_WRITE;
694
695 init_completion(&nfc->cmd_done);
696
697 err = devm_request_irq(nfc->dev, irq, vf610_nfc_irq, 0, DRV_NAME, mtd);
698 if (err) {
699 dev_err(nfc->dev, "Error requesting IRQ!\n");
Alexey Khoroshilov196644f2018-02-10 01:28:35 +0300700 goto err_disable_clk;
Stefan Agner456930d2015-09-02 18:06:33 -0700701 }
702
703 vf610_nfc_preinit_controller(nfc);
704
705 /* first scan to find the device and get the page size */
Masahiro Yamadae9d354b2016-11-04 19:43:05 +0900706 err = nand_scan_ident(mtd, 1, NULL);
707 if (err)
Alexey Khoroshilov196644f2018-02-10 01:28:35 +0300708 goto err_disable_clk;
Stefan Agner456930d2015-09-02 18:06:33 -0700709
710 vf610_nfc_init_controller(nfc);
711
712 /* Bad block options. */
713 if (chip->bbt_options & NAND_BBT_USE_FLASH)
714 chip->bbt_options |= NAND_BBT_NO_OOB;
715
716 /* Single buffer only, max 256 OOB minus ECC status */
717 if (mtd->writesize + mtd->oobsize > PAGE_2K + OOB_MAX - 8) {
718 dev_err(nfc->dev, "Unsupported flash page size\n");
719 err = -ENXIO;
Alexey Khoroshilov196644f2018-02-10 01:28:35 +0300720 goto err_disable_clk;
Stefan Agner456930d2015-09-02 18:06:33 -0700721 }
722
Stefan Agner049f4252015-09-02 18:06:34 -0700723 if (chip->ecc.mode == NAND_ECC_HW) {
724 if (mtd->writesize != PAGE_2K && mtd->oobsize < 64) {
725 dev_err(nfc->dev, "Unsupported flash with hwecc\n");
726 err = -ENXIO;
Alexey Khoroshilov196644f2018-02-10 01:28:35 +0300727 goto err_disable_clk;
Stefan Agner049f4252015-09-02 18:06:34 -0700728 }
729
730 if (chip->ecc.size != mtd->writesize) {
731 dev_err(nfc->dev, "Step size needs to be page size\n");
732 err = -ENXIO;
Alexey Khoroshilov196644f2018-02-10 01:28:35 +0300733 goto err_disable_clk;
Stefan Agner049f4252015-09-02 18:06:34 -0700734 }
735
736 /* Only 64 byte ECC layouts known */
737 if (mtd->oobsize > 64)
738 mtd->oobsize = 64;
739
Boris Brezillon3cf32d12016-02-03 20:05:45 +0100740 /*
741 * mtd->ecclayout is not specified here because we're using the
742 * default large page ECC layout defined in NAND core.
743 */
Stefan Agner049f4252015-09-02 18:06:34 -0700744 if (chip->ecc.strength == 32) {
745 nfc->ecc_mode = ECC_60_BYTE;
746 chip->ecc.bytes = 60;
Stefan Agner049f4252015-09-02 18:06:34 -0700747 } else if (chip->ecc.strength == 24) {
748 nfc->ecc_mode = ECC_45_BYTE;
749 chip->ecc.bytes = 45;
Stefan Agner049f4252015-09-02 18:06:34 -0700750 } else {
751 dev_err(nfc->dev, "Unsupported ECC strength\n");
752 err = -ENXIO;
Alexey Khoroshilov196644f2018-02-10 01:28:35 +0300753 goto err_disable_clk;
Stefan Agner049f4252015-09-02 18:06:34 -0700754 }
755
Stefan Agner049f4252015-09-02 18:06:34 -0700756 chip->ecc.read_page = vf610_nfc_read_page;
757 chip->ecc.write_page = vf610_nfc_write_page;
758
759 chip->ecc.size = PAGE_2K;
760 }
761
Stefan Agner456930d2015-09-02 18:06:33 -0700762 /* second phase scan */
Masahiro Yamadae9d354b2016-11-04 19:43:05 +0900763 err = nand_scan_tail(mtd);
764 if (err)
Alexey Khoroshilov196644f2018-02-10 01:28:35 +0300765 goto err_disable_clk;
Stefan Agner456930d2015-09-02 18:06:33 -0700766
767 platform_set_drvdata(pdev, mtd);
768
769 /* Register device in MTD */
Alexey Khoroshilov1b8c9092018-02-10 01:28:36 +0300770 err = mtd_device_register(mtd, NULL, 0);
771 if (err)
772 goto err_cleanup_nand;
773 return 0;
Stefan Agner456930d2015-09-02 18:06:33 -0700774
Alexey Khoroshilov1b8c9092018-02-10 01:28:36 +0300775err_cleanup_nand:
776 nand_cleanup(chip);
Alexey Khoroshilov196644f2018-02-10 01:28:35 +0300777err_disable_clk:
Stefan Agner456930d2015-09-02 18:06:33 -0700778 clk_disable_unprepare(nfc->clk);
779 return err;
780}
781
782static int vf610_nfc_remove(struct platform_device *pdev)
783{
784 struct mtd_info *mtd = platform_get_drvdata(pdev);
785 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
786
787 nand_release(mtd);
788 clk_disable_unprepare(nfc->clk);
789 return 0;
790}
791
792#ifdef CONFIG_PM_SLEEP
793static int vf610_nfc_suspend(struct device *dev)
794{
795 struct mtd_info *mtd = dev_get_drvdata(dev);
796 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
797
798 clk_disable_unprepare(nfc->clk);
799 return 0;
800}
801
802static int vf610_nfc_resume(struct device *dev)
803{
Fabio Estevam03fba862017-07-17 21:54:07 -0300804 int err;
805
Stefan Agner456930d2015-09-02 18:06:33 -0700806 struct mtd_info *mtd = dev_get_drvdata(dev);
807 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
808
Fabio Estevam03fba862017-07-17 21:54:07 -0300809 err = clk_prepare_enable(nfc->clk);
810 if (err)
811 return err;
Stefan Agner456930d2015-09-02 18:06:33 -0700812
813 vf610_nfc_preinit_controller(nfc);
814 vf610_nfc_init_controller(nfc);
815 return 0;
816}
817#endif
818
819static SIMPLE_DEV_PM_OPS(vf610_nfc_pm_ops, vf610_nfc_suspend, vf610_nfc_resume);
820
821static struct platform_driver vf610_nfc_driver = {
822 .driver = {
823 .name = DRV_NAME,
824 .of_match_table = vf610_nfc_dt_ids,
825 .pm = &vf610_nfc_pm_ops,
826 },
827 .probe = vf610_nfc_probe,
828 .remove = vf610_nfc_remove,
829};
830
831module_platform_driver(vf610_nfc_driver);
832
833MODULE_AUTHOR("Stefan Agner <stefan.agner@toradex.com>");
834MODULE_DESCRIPTION("Freescale VF610/MPC5125 NFC MTD NAND driver");
835MODULE_LICENSE("GPL");