blob: 845a639b059568a9bffe52dee2ee4dc789ce00ce [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>
Stefan Agner1cbe30b2018-03-09 15:50:36 +010039#include <linux/swab.h>
Stefan Agner456930d2015-09-02 18:06:33 -070040
41#define DRV_NAME "vf610_nfc"
42
43/* Register Offsets */
44#define NFC_FLASH_CMD1 0x3F00
45#define NFC_FLASH_CMD2 0x3F04
46#define NFC_COL_ADDR 0x3F08
47#define NFC_ROW_ADDR 0x3F0c
48#define NFC_ROW_ADDR_INC 0x3F14
49#define NFC_FLASH_STATUS1 0x3F18
50#define NFC_FLASH_STATUS2 0x3F1c
51#define NFC_CACHE_SWAP 0x3F28
52#define NFC_SECTOR_SIZE 0x3F2c
53#define NFC_FLASH_CONFIG 0x3F30
54#define NFC_IRQ_STATUS 0x3F38
55
56/* Addresses for NFC MAIN RAM BUFFER areas */
57#define NFC_MAIN_AREA(n) ((n) * 0x1000)
58
59#define PAGE_2K 0x0800
60#define OOB_64 0x0040
61#define OOB_MAX 0x0100
62
Stefan Agner1cbe30b2018-03-09 15:50:36 +010063/* NFC_CMD2[CODE] controller cycle bit masks */
64#define COMMAND_CMD_BYTE1 BIT(14)
65#define COMMAND_CAR_BYTE1 BIT(13)
66#define COMMAND_CAR_BYTE2 BIT(12)
67#define COMMAND_RAR_BYTE1 BIT(11)
68#define COMMAND_RAR_BYTE2 BIT(10)
69#define COMMAND_RAR_BYTE3 BIT(9)
70#define COMMAND_NADDR_BYTES(x) GENMASK(13, 13 - (x) + 1)
71#define COMMAND_WRITE_DATA BIT(8)
72#define COMMAND_CMD_BYTE2 BIT(7)
73#define COMMAND_RB_HANDSHAKE BIT(6)
74#define COMMAND_READ_DATA BIT(5)
75#define COMMAND_CMD_BYTE3 BIT(4)
76#define COMMAND_READ_STATUS BIT(3)
77#define COMMAND_READ_ID BIT(2)
78
Stefan Agner456930d2015-09-02 18:06:33 -070079/* NFC ECC mode define */
80#define ECC_BYPASS 0
Stefan Agner049f4252015-09-02 18:06:34 -070081#define ECC_45_BYTE 6
82#define ECC_60_BYTE 7
Stefan Agner456930d2015-09-02 18:06:33 -070083
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
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100102#define COL_ADDR(pos, val) (((val) & 0xFF) << (8 * (pos)))
Stefan Agner456930d2015-09-02 18:06:33 -0700103
104/* NFC_ROW_ADDR Field */
105#define ROW_ADDR_MASK 0x00FFFFFF
106#define ROW_ADDR_SHIFT 0
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100107#define ROW_ADDR(pos, val) (((val) & 0xFF) << (8 * (pos)))
108
Stefan Agner456930d2015-09-02 18:06:33 -0700109#define ROW_ADDR_CHIP_SEL_RB_MASK 0xF0000000
110#define ROW_ADDR_CHIP_SEL_RB_SHIFT 28
111#define ROW_ADDR_CHIP_SEL_MASK 0x0F000000
112#define ROW_ADDR_CHIP_SEL_SHIFT 24
113
114/* NFC_FLASH_STATUS2 Field */
115#define STATUS_BYTE1_MASK 0x000000FF
116
117/* NFC_FLASH_CONFIG Field */
118#define CONFIG_ECC_SRAM_ADDR_MASK 0x7FC00000
119#define CONFIG_ECC_SRAM_ADDR_SHIFT 22
120#define CONFIG_ECC_SRAM_REQ_BIT BIT(21)
121#define CONFIG_DMA_REQ_BIT BIT(20)
122#define CONFIG_ECC_MODE_MASK 0x000E0000
123#define CONFIG_ECC_MODE_SHIFT 17
124#define CONFIG_FAST_FLASH_BIT BIT(16)
125#define CONFIG_16BIT BIT(7)
126#define CONFIG_BOOT_MODE_BIT BIT(6)
127#define CONFIG_ADDR_AUTO_INCR_BIT BIT(5)
128#define CONFIG_BUFNO_AUTO_INCR_BIT BIT(4)
129#define CONFIG_PAGE_CNT_MASK 0xF
130#define CONFIG_PAGE_CNT_SHIFT 0
131
132/* NFC_IRQ_STATUS Field */
133#define IDLE_IRQ_BIT BIT(29)
134#define IDLE_EN_BIT BIT(20)
135#define CMD_DONE_CLEAR_BIT BIT(18)
136#define IDLE_CLEAR_BIT BIT(17)
137
Stefan Agner049f4252015-09-02 18:06:34 -0700138/*
139 * ECC status - seems to consume 8 bytes (double word). The documented
140 * status byte is located in the lowest byte of the second word (which is
141 * the 4th or 7th byte depending on endianness).
142 * Calculate an offset to store the ECC status at the end of the buffer.
143 */
144#define ECC_SRAM_ADDR (PAGE_2K + OOB_MAX - 8)
145
146#define ECC_STATUS 0x4
147#define ECC_STATUS_MASK 0x80
148#define ECC_STATUS_ERR_COUNT 0x3F
149
Stefan Agner456930d2015-09-02 18:06:33 -0700150enum vf610_nfc_variant {
151 NFC_VFC610 = 1,
152};
153
154struct vf610_nfc {
Stefan Agner456930d2015-09-02 18:06:33 -0700155 struct nand_chip chip;
156 struct device *dev;
157 void __iomem *regs;
158 struct completion cmd_done;
Stefan Agner456930d2015-09-02 18:06:33 -0700159 /* Status and ID are in alternate locations. */
Stefan Agner456930d2015-09-02 18:06:33 -0700160 enum vf610_nfc_variant variant;
161 struct clk *clk;
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100162 /*
163 * Indicate that user data is accessed (full page/oob). This is
164 * useful to indicate the driver whether to swap byte endianness.
165 * See comments in vf610_nfc_rd_from_sram/vf610_nfc_wr_to_sram.
166 */
167 bool data_access;
Stefan Agner049f4252015-09-02 18:06:34 -0700168 u32 ecc_mode;
Stefan Agner456930d2015-09-02 18:06:33 -0700169};
170
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100171static inline struct vf610_nfc *chip_to_nfc(struct nand_chip *chip)
172{
173 return container_of(chip, struct vf610_nfc, chip);
174}
175
Stefan Agner456930d2015-09-02 18:06:33 -0700176static 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
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100203static inline bool vf610_nfc_kernel_is_little_endian(void)
204{
205#ifdef __LITTLE_ENDIAN
206 return true;
207#else
208 return false;
209#endif
210}
211
212/**
213 * Read accessor for internal SRAM buffer
214 * @dst: destination address in regular memory
215 * @src: source address in SRAM buffer
216 * @len: bytes to copy
217 * @fix_endian: Fix endianness if required
218 *
219 * Use this accessor for the internal SRAM buffers. On the ARM
220 * Freescale Vybrid SoC it's known that the driver can treat
221 * the SRAM buffer as if it's memory. Other platform might need
222 * to treat the buffers differently.
223 *
224 * The controller stores bytes from the NAND chip internally in big
225 * endianness. On little endian platforms such as Vybrid this leads
226 * to reversed byte order.
227 * For performance reason (and earlier probably due to unawareness)
228 * the driver avoids correcting endianness where it has control over
229 * write and read side (e.g. page wise data access).
230 */
231static inline void vf610_nfc_rd_from_sram(void *dst, const void __iomem *src,
232 size_t len, bool fix_endian)
233{
234 if (vf610_nfc_kernel_is_little_endian() && fix_endian) {
235 unsigned int i;
236
237 for (i = 0; i < len; i += 4) {
238 u32 val = swab32(__raw_readl(src + i));
239
240 memcpy(dst + i, &val, min(sizeof(val), len - i));
241 }
242 } else {
243 memcpy_fromio(dst, src, len);
244 }
245}
246
247/**
248 * Write accessor for internal SRAM buffer
249 * @dst: destination address in SRAM buffer
250 * @src: source address in regular memory
251 * @len: bytes to copy
252 * @fix_endian: Fix endianness if required
253 *
254 * Use this accessor for the internal SRAM buffers. On the ARM
255 * Freescale Vybrid SoC it's known that the driver can treat
256 * the SRAM buffer as if it's memory. Other platform might need
257 * to treat the buffers differently.
258 *
259 * The controller stores bytes from the NAND chip internally in big
260 * endianness. On little endian platforms such as Vybrid this leads
261 * to reversed byte order.
262 * For performance reason (and earlier probably due to unawareness)
263 * the driver avoids correcting endianness where it has control over
264 * write and read side (e.g. page wise data access).
265 */
266static inline void vf610_nfc_wr_to_sram(void __iomem *dst, const void *src,
267 size_t len, bool fix_endian)
268{
269 if (vf610_nfc_kernel_is_little_endian() && fix_endian) {
270 unsigned int i;
271
272 for (i = 0; i < len; i += 4) {
273 u32 val;
274
275 memcpy(&val, src + i, min(sizeof(val), len - i));
276 __raw_writel(swab32(val), dst + i);
277 }
278 } else {
279 memcpy_toio(dst, src, len);
280 }
281}
282
Stefan Agner456930d2015-09-02 18:06:33 -0700283/* Clear flags for upcoming command */
284static inline void vf610_nfc_clear_status(struct vf610_nfc *nfc)
285{
286 u32 tmp = vf610_nfc_read(nfc, NFC_IRQ_STATUS);
287
288 tmp |= CMD_DONE_CLEAR_BIT | IDLE_CLEAR_BIT;
289 vf610_nfc_write(nfc, NFC_IRQ_STATUS, tmp);
290}
291
292static void vf610_nfc_done(struct vf610_nfc *nfc)
293{
294 unsigned long timeout = msecs_to_jiffies(100);
295
296 /*
297 * Barrier is needed after this write. This write need
298 * to be done before reading the next register the first
299 * time.
300 * vf610_nfc_set implicates such a barrier by using writel
301 * to write to the register.
302 */
303 vf610_nfc_set(nfc, NFC_IRQ_STATUS, IDLE_EN_BIT);
304 vf610_nfc_set(nfc, NFC_FLASH_CMD2, START_BIT);
305
306 if (!wait_for_completion_timeout(&nfc->cmd_done, timeout))
307 dev_warn(nfc->dev, "Timeout while waiting for BUSY.\n");
308
309 vf610_nfc_clear_status(nfc);
310}
311
Stefan Agner456930d2015-09-02 18:06:33 -0700312static irqreturn_t vf610_nfc_irq(int irq, void *data)
313{
Boris Brezillon4440f782018-11-20 10:02:36 +0100314 struct vf610_nfc *nfc = data;
Stefan Agner456930d2015-09-02 18:06:33 -0700315
316 vf610_nfc_clear(nfc, NFC_IRQ_STATUS, IDLE_EN_BIT);
317 complete(&nfc->cmd_done);
318
319 return IRQ_HANDLED;
320}
321
Stefan Agner049f4252015-09-02 18:06:34 -0700322static inline void vf610_nfc_ecc_mode(struct vf610_nfc *nfc, int ecc_mode)
323{
324 vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG,
325 CONFIG_ECC_MODE_MASK,
326 CONFIG_ECC_MODE_SHIFT, ecc_mode);
327}
328
Stefan Agner456930d2015-09-02 18:06:33 -0700329static inline void vf610_nfc_transfer_size(struct vf610_nfc *nfc, int size)
330{
331 vf610_nfc_write(nfc, NFC_SECTOR_SIZE, size);
332}
333
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100334static inline void vf610_nfc_run(struct vf610_nfc *nfc, u32 col, u32 row,
335 u32 cmd1, u32 cmd2, u32 trfr_sz)
336{
337 vf610_nfc_set_field(nfc, NFC_COL_ADDR, COL_ADDR_MASK,
338 COL_ADDR_SHIFT, col);
339
340 vf610_nfc_set_field(nfc, NFC_ROW_ADDR, ROW_ADDR_MASK,
341 ROW_ADDR_SHIFT, row);
342
343 vf610_nfc_write(nfc, NFC_SECTOR_SIZE, trfr_sz);
344 vf610_nfc_write(nfc, NFC_FLASH_CMD1, cmd1);
345 vf610_nfc_write(nfc, NFC_FLASH_CMD2, cmd2);
346
347 dev_dbg(nfc->dev,
348 "col 0x%04x, row 0x%08x, cmd1 0x%08x, cmd2 0x%08x, len %d\n",
349 col, row, cmd1, cmd2, trfr_sz);
350
351 vf610_nfc_done(nfc);
352}
353
354static inline const struct nand_op_instr *
355vf610_get_next_instr(const struct nand_subop *subop, int *op_id)
356{
357 if (*op_id + 1 >= subop->ninstrs)
358 return NULL;
359
360 (*op_id)++;
361
362 return &subop->instrs[*op_id];
363}
364
365static int vf610_nfc_cmd(struct nand_chip *chip,
366 const struct nand_subop *subop)
367{
368 const struct nand_op_instr *instr;
369 struct vf610_nfc *nfc = chip_to_nfc(chip);
370 int op_id = -1, trfr_sz = 0, offset;
371 u32 col = 0, row = 0, cmd1 = 0, cmd2 = 0, code = 0;
372 bool force8bit = false;
373
374 /*
375 * Some ops are optional, but the hardware requires the operations
376 * to be in this exact order.
377 * The op parser enforces the order and makes sure that there isn't
378 * a read and write element in a single operation.
379 */
380 instr = vf610_get_next_instr(subop, &op_id);
381 if (!instr)
382 return -EINVAL;
383
384 if (instr && instr->type == NAND_OP_CMD_INSTR) {
385 cmd2 |= instr->ctx.cmd.opcode << CMD_BYTE1_SHIFT;
386 code |= COMMAND_CMD_BYTE1;
387
388 instr = vf610_get_next_instr(subop, &op_id);
389 }
390
391 if (instr && instr->type == NAND_OP_ADDR_INSTR) {
392 int naddrs = nand_subop_get_num_addr_cyc(subop, op_id);
393 int i = nand_subop_get_addr_start_off(subop, op_id);
394
395 for (; i < naddrs; i++) {
396 u8 val = instr->ctx.addr.addrs[i];
397
398 if (i < 2)
399 col |= COL_ADDR(i, val);
400 else
401 row |= ROW_ADDR(i - 2, val);
402 }
403 code |= COMMAND_NADDR_BYTES(naddrs);
404
405 instr = vf610_get_next_instr(subop, &op_id);
406 }
407
408 if (instr && instr->type == NAND_OP_DATA_OUT_INSTR) {
409 trfr_sz = nand_subop_get_data_len(subop, op_id);
410 offset = nand_subop_get_data_start_off(subop, op_id);
411 force8bit = instr->ctx.data.force_8bit;
412
413 /*
414 * Don't fix endianness on page access for historical reasons.
415 * See comment in vf610_nfc_wr_to_sram
416 */
417 vf610_nfc_wr_to_sram(nfc->regs + NFC_MAIN_AREA(0) + offset,
418 instr->ctx.data.buf.out + offset,
419 trfr_sz, !nfc->data_access);
420 code |= COMMAND_WRITE_DATA;
421
422 instr = vf610_get_next_instr(subop, &op_id);
423 }
424
425 if (instr && instr->type == NAND_OP_CMD_INSTR) {
426 cmd1 |= instr->ctx.cmd.opcode << CMD_BYTE2_SHIFT;
427 code |= COMMAND_CMD_BYTE2;
428
429 instr = vf610_get_next_instr(subop, &op_id);
430 }
431
432 if (instr && instr->type == NAND_OP_WAITRDY_INSTR) {
433 code |= COMMAND_RB_HANDSHAKE;
434
435 instr = vf610_get_next_instr(subop, &op_id);
436 }
437
438 if (instr && instr->type == NAND_OP_DATA_IN_INSTR) {
439 trfr_sz = nand_subop_get_data_len(subop, op_id);
440 offset = nand_subop_get_data_start_off(subop, op_id);
441 force8bit = instr->ctx.data.force_8bit;
442
443 code |= COMMAND_READ_DATA;
444 }
445
446 if (force8bit && (chip->options & NAND_BUSWIDTH_16))
447 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
448
449 cmd2 |= code << CMD_CODE_SHIFT;
450
451 vf610_nfc_run(nfc, col, row, cmd1, cmd2, trfr_sz);
452
453 if (instr && instr->type == NAND_OP_DATA_IN_INSTR) {
454 /*
455 * Don't fix endianness on page access for historical reasons.
456 * See comment in vf610_nfc_rd_from_sram
457 */
458 vf610_nfc_rd_from_sram(instr->ctx.data.buf.in + offset,
459 nfc->regs + NFC_MAIN_AREA(0) + offset,
460 trfr_sz, !nfc->data_access);
461 }
462
463 if (force8bit && (chip->options & NAND_BUSWIDTH_16))
464 vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
465
466 return 0;
467}
468
469static const struct nand_op_parser vf610_nfc_op_parser = NAND_OP_PARSER(
470 NAND_OP_PARSER_PATTERN(vf610_nfc_cmd,
471 NAND_OP_PARSER_PAT_CMD_ELEM(true),
472 NAND_OP_PARSER_PAT_ADDR_ELEM(true, 5),
473 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(true, PAGE_2K + OOB_MAX),
474 NAND_OP_PARSER_PAT_CMD_ELEM(true),
475 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
476 NAND_OP_PARSER_PATTERN(vf610_nfc_cmd,
477 NAND_OP_PARSER_PAT_CMD_ELEM(true),
478 NAND_OP_PARSER_PAT_ADDR_ELEM(true, 5),
479 NAND_OP_PARSER_PAT_CMD_ELEM(true),
480 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
481 NAND_OP_PARSER_PAT_DATA_IN_ELEM(true, PAGE_2K + OOB_MAX)),
482 );
483
Stefan Agner456930d2015-09-02 18:06:33 -0700484/*
485 * This function supports Vybrid only (MPC5125 would have full RB and four CS)
486 */
Boris Brezillon653c57c2018-11-11 08:55:20 +0100487static void vf610_nfc_select_target(struct nand_chip *chip, unsigned int cs)
Stefan Agner456930d2015-09-02 18:06:33 -0700488{
Boris Brezillon4440f782018-11-20 10:02:36 +0100489 struct vf610_nfc *nfc = chip_to_nfc(chip);
Boris Brezillon653c57c2018-11-11 08:55:20 +0100490 u32 tmp;
Stefan Agner456930d2015-09-02 18:06:33 -0700491
492 /* Vybrid only (MPC5125 would have full RB and four CS) */
493 if (nfc->variant != NFC_VFC610)
494 return;
495
Boris Brezillon653c57c2018-11-11 08:55:20 +0100496 tmp = vf610_nfc_read(nfc, NFC_ROW_ADDR);
Stefan Agner456930d2015-09-02 18:06:33 -0700497 tmp &= ~(ROW_ADDR_CHIP_SEL_RB_MASK | ROW_ADDR_CHIP_SEL_MASK);
Boris Brezillon653c57c2018-11-11 08:55:20 +0100498 tmp |= 1 << ROW_ADDR_CHIP_SEL_RB_SHIFT;
499 tmp |= BIT(cs) << ROW_ADDR_CHIP_SEL_SHIFT;
Stefan Agner456930d2015-09-02 18:06:33 -0700500
501 vf610_nfc_write(nfc, NFC_ROW_ADDR, tmp);
502}
503
Boris Brezillon653c57c2018-11-11 08:55:20 +0100504static int vf610_nfc_exec_op(struct nand_chip *chip,
505 const struct nand_operation *op,
506 bool check_only)
507{
508 vf610_nfc_select_target(chip, op->cs);
509 return nand_op_parser_exec_op(chip, &vf610_nfc_op_parser, op,
510 check_only);
511}
512
Boris Brezillon4440f782018-11-20 10:02:36 +0100513static inline int vf610_nfc_correct_data(struct nand_chip *chip, uint8_t *dat,
Stefan Agner049f4252015-09-02 18:06:34 -0700514 uint8_t *oob, int page)
515{
Boris Brezillon4440f782018-11-20 10:02:36 +0100516 struct vf610_nfc *nfc = chip_to_nfc(chip);
517 struct mtd_info *mtd = nand_to_mtd(chip);
Stefan Agner049f4252015-09-02 18:06:34 -0700518 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
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100529 nfc->data_access = true;
530 nand_read_oob_op(&nfc->chip, page, 0, oob, mtd->oobsize);
531 nfc->data_access = false;
Stefan Agner049f4252015-09-02 18:06:34 -0700532
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
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100542static void vf610_nfc_fill_row(struct nand_chip *chip, int page, u32 *code,
543 u32 *row)
544{
545 *row = ROW_ADDR(0, page & 0xff) | ROW_ADDR(1, page >> 8);
546 *code |= COMMAND_RAR_BYTE1 | COMMAND_RAR_BYTE2;
547
548 if (chip->options & NAND_ROW_ADDR_3) {
549 *row |= ROW_ADDR(2, page >> 16);
550 *code |= COMMAND_RAR_BYTE3;
551 }
552}
553
Boris Brezillonb9761682018-09-06 14:05:20 +0200554static int vf610_nfc_read_page(struct nand_chip *chip, uint8_t *buf,
555 int oob_required, int page)
Stefan Agner049f4252015-09-02 18:06:34 -0700556{
Boris Brezillon4440f782018-11-20 10:02:36 +0100557 struct vf610_nfc *nfc = chip_to_nfc(chip);
Boris Brezillonb9761682018-09-06 14:05:20 +0200558 struct mtd_info *mtd = nand_to_mtd(chip);
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100559 int trfr_sz = mtd->writesize + mtd->oobsize;
560 u32 row = 0, cmd1 = 0, cmd2 = 0, code = 0;
Stefan Agner049f4252015-09-02 18:06:34 -0700561 int stat;
562
Boris Brezillon653c57c2018-11-11 08:55:20 +0100563 vf610_nfc_select_target(chip, chip->cur_cs);
564
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100565 cmd2 |= NAND_CMD_READ0 << CMD_BYTE1_SHIFT;
566 code |= COMMAND_CMD_BYTE1 | COMMAND_CAR_BYTE1 | COMMAND_CAR_BYTE2;
567
568 vf610_nfc_fill_row(chip, page, &code, &row);
569
570 cmd1 |= NAND_CMD_READSTART << CMD_BYTE2_SHIFT;
571 code |= COMMAND_CMD_BYTE2 | COMMAND_RB_HANDSHAKE | COMMAND_READ_DATA;
572
573 cmd2 |= code << CMD_CODE_SHIFT;
574
575 vf610_nfc_ecc_mode(nfc, nfc->ecc_mode);
576 vf610_nfc_run(nfc, 0, row, cmd1, cmd2, trfr_sz);
577 vf610_nfc_ecc_mode(nfc, ECC_BYPASS);
578
579 /*
580 * Don't fix endianness on page access for historical reasons.
581 * See comment in vf610_nfc_rd_from_sram
582 */
583 vf610_nfc_rd_from_sram(buf, nfc->regs + NFC_MAIN_AREA(0),
584 mtd->writesize, false);
Stefan Agner049f4252015-09-02 18:06:34 -0700585 if (oob_required)
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100586 vf610_nfc_rd_from_sram(chip->oob_poi,
587 nfc->regs + NFC_MAIN_AREA(0) +
588 mtd->writesize,
589 mtd->oobsize, false);
Stefan Agner049f4252015-09-02 18:06:34 -0700590
Boris Brezillon4440f782018-11-20 10:02:36 +0100591 stat = vf610_nfc_correct_data(chip, buf, chip->oob_poi, page);
Stefan Agner049f4252015-09-02 18:06:34 -0700592
593 if (stat < 0) {
594 mtd->ecc_stats.failed++;
595 return 0;
596 } else {
597 mtd->ecc_stats.corrected += stat;
598 return stat;
599 }
600}
601
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200602static int vf610_nfc_write_page(struct nand_chip *chip, const uint8_t *buf,
603 int oob_required, int page)
Stefan Agner049f4252015-09-02 18:06:34 -0700604{
Boris Brezillon4440f782018-11-20 10:02:36 +0100605 struct vf610_nfc *nfc = chip_to_nfc(chip);
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200606 struct mtd_info *mtd = nand_to_mtd(chip);
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100607 int trfr_sz = mtd->writesize + mtd->oobsize;
608 u32 row = 0, cmd1 = 0, cmd2 = 0, code = 0;
609 u8 status;
610 int ret;
Stefan Agner049f4252015-09-02 18:06:34 -0700611
Boris Brezillon653c57c2018-11-11 08:55:20 +0100612 vf610_nfc_select_target(chip, chip->cur_cs);
613
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100614 cmd2 |= NAND_CMD_SEQIN << CMD_BYTE1_SHIFT;
615 code |= COMMAND_CMD_BYTE1 | COMMAND_CAR_BYTE1 | COMMAND_CAR_BYTE2;
Stefan Agner049f4252015-09-02 18:06:34 -0700616
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100617 vf610_nfc_fill_row(chip, page, &code, &row);
618
619 cmd1 |= NAND_CMD_PAGEPROG << CMD_BYTE2_SHIFT;
620 code |= COMMAND_CMD_BYTE2 | COMMAND_WRITE_DATA;
621
622 /*
623 * Don't fix endianness on page access for historical reasons.
624 * See comment in vf610_nfc_wr_to_sram
625 */
626 vf610_nfc_wr_to_sram(nfc->regs + NFC_MAIN_AREA(0), buf,
627 mtd->writesize, false);
628
629 code |= COMMAND_RB_HANDSHAKE;
630 cmd2 |= code << CMD_CODE_SHIFT;
631
632 vf610_nfc_ecc_mode(nfc, nfc->ecc_mode);
633 vf610_nfc_run(nfc, 0, row, cmd1, cmd2, trfr_sz);
634 vf610_nfc_ecc_mode(nfc, ECC_BYPASS);
635
636 ret = nand_status_op(chip, &status);
637 if (ret)
638 return ret;
639
640 if (status & NAND_STATUS_FAIL)
641 return -EIO;
642
643 return 0;
644}
645
Boris Brezillonb9761682018-09-06 14:05:20 +0200646static int vf610_nfc_read_page_raw(struct nand_chip *chip, u8 *buf,
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100647 int oob_required, int page)
648{
Boris Brezillon4440f782018-11-20 10:02:36 +0100649 struct vf610_nfc *nfc = chip_to_nfc(chip);
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100650 int ret;
651
652 nfc->data_access = true;
Boris Brezillonb9761682018-09-06 14:05:20 +0200653 ret = nand_read_page_raw(chip, buf, oob_required, page);
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100654 nfc->data_access = false;
655
656 return ret;
657}
658
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200659static int vf610_nfc_write_page_raw(struct nand_chip *chip, const u8 *buf,
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100660 int oob_required, int page)
661{
Boris Brezillon4440f782018-11-20 10:02:36 +0100662 struct vf610_nfc *nfc = chip_to_nfc(chip);
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200663 struct mtd_info *mtd = nand_to_mtd(chip);
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100664 int ret;
665
666 nfc->data_access = true;
667 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
668 if (!ret && oob_required)
669 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
670 false);
671 nfc->data_access = false;
672
673 if (ret)
674 return ret;
675
676 return nand_prog_page_end_op(chip);
677}
678
Boris Brezillonb9761682018-09-06 14:05:20 +0200679static int vf610_nfc_read_oob(struct nand_chip *chip, int page)
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100680{
Boris Brezillon4440f782018-11-20 10:02:36 +0100681 struct vf610_nfc *nfc = chip_to_nfc(chip);
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100682 int ret;
683
684 nfc->data_access = true;
Boris Brezillonb9761682018-09-06 14:05:20 +0200685 ret = nand_read_oob_std(chip, page);
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100686 nfc->data_access = false;
687
688 return ret;
689}
690
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200691static int vf610_nfc_write_oob(struct nand_chip *chip, int page)
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100692{
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200693 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon4440f782018-11-20 10:02:36 +0100694 struct vf610_nfc *nfc = chip_to_nfc(chip);
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100695 int ret;
696
697 nfc->data_access = true;
698 ret = nand_prog_page_begin_op(chip, page, mtd->writesize,
699 chip->oob_poi, mtd->oobsize);
700 nfc->data_access = false;
701
702 if (ret)
703 return ret;
Stefan Agner049f4252015-09-02 18:06:34 -0700704
Boris Brezillon25f815f2017-11-30 18:01:30 +0100705 return nand_prog_page_end_op(chip);
Stefan Agner049f4252015-09-02 18:06:34 -0700706}
707
Stefan Agner456930d2015-09-02 18:06:33 -0700708static const struct of_device_id vf610_nfc_dt_ids[] = {
709 { .compatible = "fsl,vf610-nfc", .data = (void *)NFC_VFC610 },
710 { /* sentinel */ }
711};
712MODULE_DEVICE_TABLE(of, vf610_nfc_dt_ids);
713
714static void vf610_nfc_preinit_controller(struct vf610_nfc *nfc)
715{
716 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
717 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_ADDR_AUTO_INCR_BIT);
718 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_BUFNO_AUTO_INCR_BIT);
719 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_BOOT_MODE_BIT);
720 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_DMA_REQ_BIT);
721 vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_FAST_FLASH_BIT);
Stefan Agner1cbe30b2018-03-09 15:50:36 +0100722 vf610_nfc_ecc_mode(nfc, ECC_BYPASS);
Stefan Agner456930d2015-09-02 18:06:33 -0700723
724 /* Disable virtual pages, only one elementary transfer unit */
725 vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG, CONFIG_PAGE_CNT_MASK,
726 CONFIG_PAGE_CNT_SHIFT, 1);
727}
728
729static void vf610_nfc_init_controller(struct vf610_nfc *nfc)
730{
731 if (nfc->chip.options & NAND_BUSWIDTH_16)
732 vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
733 else
734 vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
Stefan Agner049f4252015-09-02 18:06:34 -0700735
736 if (nfc->chip.ecc.mode == NAND_ECC_HW) {
737 /* Set ECC status offset in SRAM */
738 vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG,
739 CONFIG_ECC_SRAM_ADDR_MASK,
740 CONFIG_ECC_SRAM_ADDR_SHIFT,
741 ECC_SRAM_ADDR >> 3);
742
743 /* Enable ECC status in SRAM */
744 vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_ECC_SRAM_REQ_BIT);
745 }
Stefan Agner456930d2015-09-02 18:06:33 -0700746}
747
Miquel Raynal962c35e2018-07-20 17:15:17 +0200748static int vf610_nfc_attach_chip(struct nand_chip *chip)
749{
750 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon4440f782018-11-20 10:02:36 +0100751 struct vf610_nfc *nfc = chip_to_nfc(chip);
Miquel Raynal962c35e2018-07-20 17:15:17 +0200752
753 vf610_nfc_init_controller(nfc);
754
755 /* Bad block options. */
756 if (chip->bbt_options & NAND_BBT_USE_FLASH)
757 chip->bbt_options |= NAND_BBT_NO_OOB;
758
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");
762 return -ENXIO;
763 }
764
765 if (chip->ecc.mode != NAND_ECC_HW)
766 return 0;
767
768 if (mtd->writesize != PAGE_2K && mtd->oobsize < 64) {
769 dev_err(nfc->dev, "Unsupported flash with hwecc\n");
770 return -ENXIO;
771 }
772
773 if (chip->ecc.size != mtd->writesize) {
774 dev_err(nfc->dev, "Step size needs to be page size\n");
775 return -ENXIO;
776 }
777
778 /* Only 64 byte ECC layouts known */
779 if (mtd->oobsize > 64)
780 mtd->oobsize = 64;
781
782 /* Use default large page ECC layout defined in NAND core */
783 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
784 if (chip->ecc.strength == 32) {
785 nfc->ecc_mode = ECC_60_BYTE;
786 chip->ecc.bytes = 60;
787 } else if (chip->ecc.strength == 24) {
788 nfc->ecc_mode = ECC_45_BYTE;
789 chip->ecc.bytes = 45;
790 } else {
791 dev_err(nfc->dev, "Unsupported ECC strength\n");
792 return -ENXIO;
793 }
794
795 chip->ecc.read_page = vf610_nfc_read_page;
796 chip->ecc.write_page = vf610_nfc_write_page;
797 chip->ecc.read_page_raw = vf610_nfc_read_page_raw;
798 chip->ecc.write_page_raw = vf610_nfc_write_page_raw;
799 chip->ecc.read_oob = vf610_nfc_read_oob;
800 chip->ecc.write_oob = vf610_nfc_write_oob;
801
802 chip->ecc.size = PAGE_2K;
803
804 return 0;
805}
806
807static const struct nand_controller_ops vf610_nfc_controller_ops = {
808 .attach_chip = vf610_nfc_attach_chip,
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100809 .exec_op = vf610_nfc_exec_op,
810
Miquel Raynal962c35e2018-07-20 17:15:17 +0200811};
812
Stefan Agner456930d2015-09-02 18:06:33 -0700813static int vf610_nfc_probe(struct platform_device *pdev)
814{
815 struct vf610_nfc *nfc;
816 struct resource *res;
817 struct mtd_info *mtd;
818 struct nand_chip *chip;
819 struct device_node *child;
820 const struct of_device_id *of_id;
821 int err;
822 int irq;
823
824 nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
825 if (!nfc)
826 return -ENOMEM;
827
828 nfc->dev = &pdev->dev;
Stefan Agner456930d2015-09-02 18:06:33 -0700829 chip = &nfc->chip;
Boris BREZILLON960823a2015-12-10 09:00:29 +0100830 mtd = nand_to_mtd(chip);
Stefan Agner456930d2015-09-02 18:06:33 -0700831
Stefan Agner456930d2015-09-02 18:06:33 -0700832 mtd->owner = THIS_MODULE;
833 mtd->dev.parent = nfc->dev;
834 mtd->name = DRV_NAME;
835
836 irq = platform_get_irq(pdev, 0);
837 if (irq <= 0)
838 return -EINVAL;
839
840 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
841 nfc->regs = devm_ioremap_resource(nfc->dev, res);
842 if (IS_ERR(nfc->regs))
843 return PTR_ERR(nfc->regs);
844
845 nfc->clk = devm_clk_get(&pdev->dev, NULL);
846 if (IS_ERR(nfc->clk))
847 return PTR_ERR(nfc->clk);
848
849 err = clk_prepare_enable(nfc->clk);
850 if (err) {
851 dev_err(nfc->dev, "Unable to enable clock!\n");
852 return err;
853 }
854
855 of_id = of_match_device(vf610_nfc_dt_ids, &pdev->dev);
856 nfc->variant = (enum vf610_nfc_variant)of_id->data;
857
858 for_each_available_child_of_node(nfc->dev->of_node, child) {
859 if (of_device_is_compatible(child, "fsl,vf610-nfc-nandcs")) {
860
Boris BREZILLON44ec23c2015-11-02 00:03:38 +0100861 if (nand_get_flash_node(chip)) {
Stefan Agner456930d2015-09-02 18:06:33 -0700862 dev_err(nfc->dev,
863 "Only one NAND chip supported!\n");
864 err = -EINVAL;
Alexey Khoroshilov196644f2018-02-10 01:28:35 +0300865 goto err_disable_clk;
Stefan Agner456930d2015-09-02 18:06:33 -0700866 }
867
Brian Norris63752192015-10-30 20:33:23 -0700868 nand_set_flash_node(chip, child);
Stefan Agner456930d2015-09-02 18:06:33 -0700869 }
870 }
871
Boris BREZILLON44ec23c2015-11-02 00:03:38 +0100872 if (!nand_get_flash_node(chip)) {
Stefan Agner456930d2015-09-02 18:06:33 -0700873 dev_err(nfc->dev, "NAND chip sub-node missing!\n");
874 err = -ENODEV;
Alexey Khoroshilov196644f2018-02-10 01:28:35 +0300875 goto err_disable_clk;
Stefan Agner456930d2015-09-02 18:06:33 -0700876 }
877
Stefan Agner456930d2015-09-02 18:06:33 -0700878 chip->options |= NAND_NO_SUBPAGE_WRITE;
879
880 init_completion(&nfc->cmd_done);
881
Boris Brezillon4440f782018-11-20 10:02:36 +0100882 err = devm_request_irq(nfc->dev, irq, vf610_nfc_irq, 0, DRV_NAME, nfc);
Stefan Agner456930d2015-09-02 18:06:33 -0700883 if (err) {
884 dev_err(nfc->dev, "Error requesting IRQ!\n");
Alexey Khoroshilov196644f2018-02-10 01:28:35 +0300885 goto err_disable_clk;
Stefan Agner456930d2015-09-02 18:06:33 -0700886 }
887
888 vf610_nfc_preinit_controller(nfc);
889
Miquel Raynal962c35e2018-07-20 17:15:17 +0200890 /* Scan the NAND chip */
891 chip->dummy_controller.ops = &vf610_nfc_controller_ops;
Boris Brezillon00ad3782018-09-06 14:05:14 +0200892 err = nand_scan(chip, 1);
Masahiro Yamadae9d354b2016-11-04 19:43:05 +0900893 if (err)
Alexey Khoroshilov196644f2018-02-10 01:28:35 +0300894 goto err_disable_clk;
Stefan Agner456930d2015-09-02 18:06:33 -0700895
Boris Brezillon4440f782018-11-20 10:02:36 +0100896 platform_set_drvdata(pdev, nfc);
Stefan Agner456930d2015-09-02 18:06:33 -0700897
898 /* Register device in MTD */
Alexey Khoroshilov1b8c9092018-02-10 01:28:36 +0300899 err = mtd_device_register(mtd, NULL, 0);
900 if (err)
901 goto err_cleanup_nand;
902 return 0;
Stefan Agner456930d2015-09-02 18:06:33 -0700903
Alexey Khoroshilov1b8c9092018-02-10 01:28:36 +0300904err_cleanup_nand:
905 nand_cleanup(chip);
Alexey Khoroshilov196644f2018-02-10 01:28:35 +0300906err_disable_clk:
Stefan Agner456930d2015-09-02 18:06:33 -0700907 clk_disable_unprepare(nfc->clk);
908 return err;
909}
910
911static int vf610_nfc_remove(struct platform_device *pdev)
912{
Boris Brezillon4440f782018-11-20 10:02:36 +0100913 struct vf610_nfc *nfc = platform_get_drvdata(pdev);
Stefan Agner456930d2015-09-02 18:06:33 -0700914
Boris Brezillon4440f782018-11-20 10:02:36 +0100915 nand_release(&nfc->chip);
Stefan Agner456930d2015-09-02 18:06:33 -0700916 clk_disable_unprepare(nfc->clk);
917 return 0;
918}
919
920#ifdef CONFIG_PM_SLEEP
921static int vf610_nfc_suspend(struct device *dev)
922{
Boris Brezillon4440f782018-11-20 10:02:36 +0100923 struct vf610_nfc *nfc = dev_get_drvdata(dev);
Stefan Agner456930d2015-09-02 18:06:33 -0700924
925 clk_disable_unprepare(nfc->clk);
926 return 0;
927}
928
929static int vf610_nfc_resume(struct device *dev)
930{
Boris Brezillon4440f782018-11-20 10:02:36 +0100931 struct vf610_nfc *nfc = dev_get_drvdata(dev);
Fabio Estevam03fba862017-07-17 21:54:07 -0300932 int err;
933
Fabio Estevam03fba862017-07-17 21:54:07 -0300934 err = clk_prepare_enable(nfc->clk);
935 if (err)
936 return err;
Stefan Agner456930d2015-09-02 18:06:33 -0700937
938 vf610_nfc_preinit_controller(nfc);
939 vf610_nfc_init_controller(nfc);
940 return 0;
941}
942#endif
943
944static SIMPLE_DEV_PM_OPS(vf610_nfc_pm_ops, vf610_nfc_suspend, vf610_nfc_resume);
945
946static struct platform_driver vf610_nfc_driver = {
947 .driver = {
948 .name = DRV_NAME,
949 .of_match_table = vf610_nfc_dt_ids,
950 .pm = &vf610_nfc_pm_ops,
951 },
952 .probe = vf610_nfc_probe,
953 .remove = vf610_nfc_remove,
954};
955
956module_platform_driver(vf610_nfc_driver);
957
958MODULE_AUTHOR("Stefan Agner <stefan.agner@toradex.com>");
959MODULE_DESCRIPTION("Freescale VF610/MPC5125 NFC MTD NAND driver");
960MODULE_LICENSE("GPL");