blob: 52c8a94778cb664998b6d3f597e7e3dadaa199fb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +02002/*
Stefan Agner59ae13a2015-10-13 22:11:42 -07003 * Copyright 2009-2015 Freescale Semiconductor, Inc. and others
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +02004 *
5 * Description: MPC5125, VF610, MCF54418 and Kinetis K70 Nand driver.
6 * Ported to U-Boot by Stefan Agner
7 * Based on RFC driver posted on Kernel Mailing list by Bill Pringlemeir
8 * Jason ported to M54418TWR and MVFA5.
9 * Authors: Stefan Agner <stefan.agner@toradex.com>
10 * Bill Pringlemeir <bpringlemeir@nbsps.com>
11 * Shaohui Xie <b21989@freescale.com>
12 * Jason Jin <Jason.jin@freescale.com>
13 *
14 * Based on original driver mpc5121_nfc.c.
15 *
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +020016 * Limitations:
17 * - Untested on MPC5125 and M54418.
Stefan Agner59ae13a2015-10-13 22:11:42 -070018 * - DMA and pipelining not used.
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +020019 * - 2K pages or less.
Stefan Agner59ae13a2015-10-13 22:11:42 -070020 * - HW ECC: Only 2K page with 64+ OOB.
21 * - HW ECC: Only 24 and 32-bit error correction implemented.
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +020022 */
23
24#include <common.h>
25#include <malloc.h>
Simon Glass9bc15642020-02-03 07:36:16 -070026#include <dm/device_compat.h>
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +020027
28#include <linux/mtd/mtd.h>
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090029#include <linux/mtd/rawnand.h>
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +020030#include <linux/mtd/partitions.h>
31
32#include <nand.h>
33#include <errno.h>
34#include <asm/io.h>
Lukasz Majewskia8f1a672018-12-03 10:24:51 +010035#if CONFIG_NAND_VF610_NFC_DT
36#include <dm.h>
37#include <linux/io.h>
38#include <linux/ioport.h>
39#endif
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +020040
41/* Register Offsets */
42#define NFC_FLASH_CMD1 0x3F00
43#define NFC_FLASH_CMD2 0x3F04
44#define NFC_COL_ADDR 0x3F08
45#define NFC_ROW_ADDR 0x3F0c
46#define NFC_ROW_ADDR_INC 0x3F14
47#define NFC_FLASH_STATUS1 0x3F18
48#define NFC_FLASH_STATUS2 0x3F1c
49#define NFC_CACHE_SWAP 0x3F28
50#define NFC_SECTOR_SIZE 0x3F2c
51#define NFC_FLASH_CONFIG 0x3F30
52#define NFC_IRQ_STATUS 0x3F38
53
54/* Addresses for NFC MAIN RAM BUFFER areas */
55#define NFC_MAIN_AREA(n) ((n) * 0x1000)
56
57#define PAGE_2K 0x0800
58#define OOB_64 0x0040
Stefan Agner59ae13a2015-10-13 22:11:42 -070059#define OOB_MAX 0x0100
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +020060
61/*
62 * NFC_CMD2[CODE] values. See section:
63 * - 31.4.7 Flash Command Code Description, Vybrid manual
64 * - 23.8.6 Flash Command Sequencer, MPC5125 manual
65 *
66 * Briefly these are bitmasks of controller cycles.
67 */
68#define READ_PAGE_CMD_CODE 0x7EE0
Stefan Agner4ce682a2015-05-08 19:07:13 +020069#define READ_ONFI_PARAM_CMD_CODE 0x4860
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +020070#define PROGRAM_PAGE_CMD_CODE 0x7FC0
71#define ERASE_CMD_CODE 0x4EC0
72#define READ_ID_CMD_CODE 0x4804
73#define RESET_CMD_CODE 0x4040
74#define STATUS_READ_CMD_CODE 0x4068
75
76/* NFC ECC mode define */
77#define ECC_BYPASS 0
78#define ECC_45_BYTE 6
Stefan Agnerfe10d3f2015-05-08 19:07:12 +020079#define ECC_60_BYTE 7
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +020080
81/*** Register Mask and bit definitions */
82
83/* NFC_FLASH_CMD1 Field */
84#define CMD_BYTE2_MASK 0xFF000000
85#define CMD_BYTE2_SHIFT 24
86
87/* NFC_FLASH_CM2 Field */
88#define CMD_BYTE1_MASK 0xFF000000
89#define CMD_BYTE1_SHIFT 24
90#define CMD_CODE_MASK 0x00FFFF00
91#define CMD_CODE_SHIFT 8
92#define BUFNO_MASK 0x00000006
93#define BUFNO_SHIFT 1
94#define START_BIT (1<<0)
95
96/* NFC_COL_ADDR Field */
97#define COL_ADDR_MASK 0x0000FFFF
98#define COL_ADDR_SHIFT 0
99
100/* NFC_ROW_ADDR Field */
101#define ROW_ADDR_MASK 0x00FFFFFF
102#define ROW_ADDR_SHIFT 0
103#define ROW_ADDR_CHIP_SEL_RB_MASK 0xF0000000
104#define ROW_ADDR_CHIP_SEL_RB_SHIFT 28
105#define ROW_ADDR_CHIP_SEL_MASK 0x0F000000
106#define ROW_ADDR_CHIP_SEL_SHIFT 24
107
108/* NFC_FLASH_STATUS2 Field */
109#define STATUS_BYTE1_MASK 0x000000FF
110
111/* NFC_FLASH_CONFIG Field */
112#define CONFIG_ECC_SRAM_ADDR_MASK 0x7FC00000
113#define CONFIG_ECC_SRAM_ADDR_SHIFT 22
114#define CONFIG_ECC_SRAM_REQ_BIT (1<<21)
115#define CONFIG_DMA_REQ_BIT (1<<20)
116#define CONFIG_ECC_MODE_MASK 0x000E0000
117#define CONFIG_ECC_MODE_SHIFT 17
118#define CONFIG_FAST_FLASH_BIT (1<<16)
119#define CONFIG_16BIT (1<<7)
120#define CONFIG_BOOT_MODE_BIT (1<<6)
121#define CONFIG_ADDR_AUTO_INCR_BIT (1<<5)
122#define CONFIG_BUFNO_AUTO_INCR_BIT (1<<4)
123#define CONFIG_PAGE_CNT_MASK 0xF
124#define CONFIG_PAGE_CNT_SHIFT 0
125
126/* NFC_IRQ_STATUS Field */
127#define IDLE_IRQ_BIT (1<<29)
128#define IDLE_EN_BIT (1<<20)
129#define CMD_DONE_CLEAR_BIT (1<<18)
130#define IDLE_CLEAR_BIT (1<<17)
131
132#define NFC_TIMEOUT (1000)
133
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200134/*
Stefan Agner59ae13a2015-10-13 22:11:42 -0700135 * ECC status - seems to consume 8 bytes (double word). The documented
136 * status byte is located in the lowest byte of the second word (which is
137 * the 4th or 7th byte depending on endianness).
138 * Calculate an offset to store the ECC status at the end of the buffer.
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200139 */
Stefan Agner59ae13a2015-10-13 22:11:42 -0700140#define ECC_SRAM_ADDR (PAGE_2K + OOB_MAX - 8)
141
142#define ECC_STATUS 0x4
143#define ECC_STATUS_MASK 0x80
144#define ECC_STATUS_ERR_COUNT 0x3F
145
146enum vf610_nfc_alt_buf {
147 ALT_BUF_DATA = 0,
148 ALT_BUF_ID = 1,
149 ALT_BUF_STAT = 2,
150 ALT_BUF_ONFI = 3,
151};
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200152
153struct vf610_nfc {
Stefan Agner59ae13a2015-10-13 22:11:42 -0700154 struct nand_chip chip;
155 void __iomem *regs;
156 uint buf_offset;
157 int write_sz;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200158 /* Status and ID are in alternate locations. */
Stefan Agner59ae13a2015-10-13 22:11:42 -0700159 enum vf610_nfc_alt_buf alt_buf;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200160};
161
Scott Wood17fed142016-05-30 13:57:56 -0500162#define mtd_to_nfc(_mtd) nand_get_controller_data(mtd_to_nand(_mtd))
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200163
Stefan Agnerfe10d3f2015-05-08 19:07:12 +0200164#if defined(CONFIG_SYS_NAND_VF610_NFC_45_ECC_BYTES)
165#define ECC_HW_MODE ECC_45_BYTE
166
167static struct nand_ecclayout vf610_nfc_ecc = {
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200168 .eccbytes = 45,
169 .eccpos = {19, 20, 21, 22, 23,
170 24, 25, 26, 27, 28, 29, 30, 31,
171 32, 33, 34, 35, 36, 37, 38, 39,
172 40, 41, 42, 43, 44, 45, 46, 47,
173 48, 49, 50, 51, 52, 53, 54, 55,
174 56, 57, 58, 59, 60, 61, 62, 63},
175 .oobfree = {
Stefan Agner59ae13a2015-10-13 22:11:42 -0700176 {.offset = 2,
177 .length = 17} }
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200178};
Stefan Agnerfe10d3f2015-05-08 19:07:12 +0200179#elif defined(CONFIG_SYS_NAND_VF610_NFC_60_ECC_BYTES)
180#define ECC_HW_MODE ECC_60_BYTE
181
182static struct nand_ecclayout vf610_nfc_ecc = {
183 .eccbytes = 60,
184 .eccpos = { 4, 5, 6, 7, 8, 9, 10, 11,
185 12, 13, 14, 15, 16, 17, 18, 19,
186 20, 21, 22, 23, 24, 25, 26, 27,
187 28, 29, 30, 31, 32, 33, 34, 35,
188 36, 37, 38, 39, 40, 41, 42, 43,
189 44, 45, 46, 47, 48, 49, 50, 51,
190 52, 53, 54, 55, 56, 57, 58, 59,
191 60, 61, 62, 63 },
192 .oobfree = {
193 {.offset = 2,
194 .length = 2} }
195};
196#endif
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200197
198static inline u32 vf610_nfc_read(struct mtd_info *mtd, uint reg)
199{
200 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
201
202 return readl(nfc->regs + reg);
203}
204
205static inline void vf610_nfc_write(struct mtd_info *mtd, uint reg, u32 val)
206{
207 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
208
209 writel(val, nfc->regs + reg);
210}
211
212static inline void vf610_nfc_set(struct mtd_info *mtd, uint reg, u32 bits)
213{
214 vf610_nfc_write(mtd, reg, vf610_nfc_read(mtd, reg) | bits);
215}
216
217static inline void vf610_nfc_clear(struct mtd_info *mtd, uint reg, u32 bits)
218{
219 vf610_nfc_write(mtd, reg, vf610_nfc_read(mtd, reg) & ~bits);
220}
221
222static inline void vf610_nfc_set_field(struct mtd_info *mtd, u32 reg,
223 u32 mask, u32 shift, u32 val)
224{
225 vf610_nfc_write(mtd, reg,
226 (vf610_nfc_read(mtd, reg) & (~mask)) | val << shift);
227}
228
229static inline void vf610_nfc_memcpy(void *dst, const void *src, size_t n)
230{
231 /*
Stefan Agner59ae13a2015-10-13 22:11:42 -0700232 * Use this accessor for the internal SRAM buffers. On the ARM
233 * Freescale Vybrid SoC it's known that the driver can treat
234 * the SRAM buffer as if it's memory. Other platform might need
235 * to treat the buffers differently.
236 *
237 * For the time being, use memcpy
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200238 */
239 memcpy(dst, src, n);
240}
241
242/* Clear flags for upcoming command */
243static inline void vf610_nfc_clear_status(void __iomem *regbase)
244{
245 void __iomem *reg = regbase + NFC_IRQ_STATUS;
246 u32 tmp = __raw_readl(reg);
247 tmp |= CMD_DONE_CLEAR_BIT | IDLE_CLEAR_BIT;
248 __raw_writel(tmp, reg);
249}
250
251/* Wait for complete operation */
Stefan Agner59ae13a2015-10-13 22:11:42 -0700252static void vf610_nfc_done(struct mtd_info *mtd)
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200253{
254 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
255 uint start;
256
257 /*
258 * Barrier is needed after this write. This write need
259 * to be done before reading the next register the first
260 * time.
261 * vf610_nfc_set implicates such a barrier by using writel
262 * to write to the register.
263 */
264 vf610_nfc_set(mtd, NFC_FLASH_CMD2, START_BIT);
265
266 start = get_timer(0);
267
268 while (!(vf610_nfc_read(mtd, NFC_IRQ_STATUS) & IDLE_IRQ_BIT)) {
269 if (get_timer(start) > NFC_TIMEOUT) {
Stefan Agner59ae13a2015-10-13 22:11:42 -0700270 printf("Timeout while waiting for IDLE.\n");
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200271 return;
272 }
273 }
274 vf610_nfc_clear_status(nfc->regs);
275}
276
277static u8 vf610_nfc_get_id(struct mtd_info *mtd, int col)
278{
279 u32 flash_id;
280
281 if (col < 4) {
282 flash_id = vf610_nfc_read(mtd, NFC_FLASH_STATUS1);
Stefan Agner59ae13a2015-10-13 22:11:42 -0700283 flash_id >>= (3 - col) * 8;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200284 } else {
285 flash_id = vf610_nfc_read(mtd, NFC_FLASH_STATUS2);
Stefan Agner59ae13a2015-10-13 22:11:42 -0700286 flash_id >>= 24;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200287 }
Stefan Agner59ae13a2015-10-13 22:11:42 -0700288
289 return flash_id & 0xff;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200290}
291
292static u8 vf610_nfc_get_status(struct mtd_info *mtd)
293{
294 return vf610_nfc_read(mtd, NFC_FLASH_STATUS2) & STATUS_BYTE1_MASK;
295}
296
297/* Single command */
298static void vf610_nfc_send_command(void __iomem *regbase, u32 cmd_byte1,
299 u32 cmd_code)
300{
301 void __iomem *reg = regbase + NFC_FLASH_CMD2;
302 u32 tmp;
303 vf610_nfc_clear_status(regbase);
304
305 tmp = __raw_readl(reg);
306 tmp &= ~(CMD_BYTE1_MASK | CMD_CODE_MASK | BUFNO_MASK);
307 tmp |= cmd_byte1 << CMD_BYTE1_SHIFT;
308 tmp |= cmd_code << CMD_CODE_SHIFT;
309 __raw_writel(tmp, reg);
310}
311
312/* Two commands */
313static void vf610_nfc_send_commands(void __iomem *regbase, u32 cmd_byte1,
314 u32 cmd_byte2, u32 cmd_code)
315{
316 void __iomem *reg = regbase + NFC_FLASH_CMD1;
317 u32 tmp;
318 vf610_nfc_send_command(regbase, cmd_byte1, cmd_code);
319
320 tmp = __raw_readl(reg);
321 tmp &= ~CMD_BYTE2_MASK;
322 tmp |= cmd_byte2 << CMD_BYTE2_SHIFT;
323 __raw_writel(tmp, reg);
324}
325
326static void vf610_nfc_addr_cycle(struct mtd_info *mtd, int column, int page)
327{
328 if (column != -1) {
329 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
Stefan Agner7056fee2015-05-08 19:07:09 +0200330 if (nfc->chip.options & NAND_BUSWIDTH_16)
331 column = column / 2;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200332 vf610_nfc_set_field(mtd, NFC_COL_ADDR, COL_ADDR_MASK,
333 COL_ADDR_SHIFT, column);
334 }
335 if (page != -1)
336 vf610_nfc_set_field(mtd, NFC_ROW_ADDR, ROW_ADDR_MASK,
337 ROW_ADDR_SHIFT, page);
338}
339
Stefan Agner7056fee2015-05-08 19:07:09 +0200340static inline void vf610_nfc_ecc_mode(struct mtd_info *mtd, int ecc_mode)
341{
342 vf610_nfc_set_field(mtd, NFC_FLASH_CONFIG,
343 CONFIG_ECC_MODE_MASK,
344 CONFIG_ECC_MODE_SHIFT, ecc_mode);
345}
346
Stefan Agner11f1fa02015-03-24 17:54:20 +0100347static inline void vf610_nfc_transfer_size(void __iomem *regbase, int size)
348{
349 __raw_writel(size, regbase + NFC_SECTOR_SIZE);
350}
351
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200352/* Send command to NAND chip */
353static void vf610_nfc_command(struct mtd_info *mtd, unsigned command,
354 int column, int page)
355{
356 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
Stefan Agner59ae13a2015-10-13 22:11:42 -0700357 int trfr_sz = nfc->chip.options & NAND_BUSWIDTH_16 ? 1 : 0;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200358
Stefan Agner59ae13a2015-10-13 22:11:42 -0700359 nfc->buf_offset = max(column, 0);
360 nfc->alt_buf = ALT_BUF_DATA;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200361
362 switch (command) {
Stefan Agner04213412015-05-08 19:07:07 +0200363 case NAND_CMD_SEQIN:
364 /* Use valid column/page from preread... */
365 vf610_nfc_addr_cycle(mtd, column, page);
Stefan Agner59ae13a2015-10-13 22:11:42 -0700366 nfc->buf_offset = 0;
367
Stefan Agner04213412015-05-08 19:07:07 +0200368 /*
369 * SEQIN => data => PAGEPROG sequence is done by the controller
370 * hence we do not need to issue the command here...
371 */
372 return;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200373 case NAND_CMD_PAGEPROG:
Stefan Agner59ae13a2015-10-13 22:11:42 -0700374 trfr_sz += nfc->write_sz;
375 vf610_nfc_ecc_mode(mtd, ECC_HW_MODE);
376 vf610_nfc_transfer_size(nfc->regs, trfr_sz);
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200377 vf610_nfc_send_commands(nfc->regs, NAND_CMD_SEQIN,
378 command, PROGRAM_PAGE_CMD_CODE);
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200379 break;
380
381 case NAND_CMD_RESET:
Stefan Agner11f1fa02015-03-24 17:54:20 +0100382 vf610_nfc_transfer_size(nfc->regs, 0);
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200383 vf610_nfc_send_command(nfc->regs, command, RESET_CMD_CODE);
384 break;
Stefan Agner7056fee2015-05-08 19:07:09 +0200385
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200386 case NAND_CMD_READOOB:
Stefan Agner59ae13a2015-10-13 22:11:42 -0700387 trfr_sz += mtd->oobsize;
Stefan Agner7056fee2015-05-08 19:07:09 +0200388 column = mtd->writesize;
Stefan Agner59ae13a2015-10-13 22:11:42 -0700389 vf610_nfc_transfer_size(nfc->regs, trfr_sz);
Stefan Agner7056fee2015-05-08 19:07:09 +0200390 vf610_nfc_send_commands(nfc->regs, NAND_CMD_READ0,
391 NAND_CMD_READSTART, READ_PAGE_CMD_CODE);
392 vf610_nfc_addr_cycle(mtd, column, page);
393 vf610_nfc_ecc_mode(mtd, ECC_BYPASS);
394 break;
395
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200396 case NAND_CMD_READ0:
Stefan Agner59ae13a2015-10-13 22:11:42 -0700397 trfr_sz += mtd->writesize + mtd->oobsize;
398 vf610_nfc_transfer_size(nfc->regs, trfr_sz);
399 vf610_nfc_ecc_mode(mtd, ECC_HW_MODE);
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200400 vf610_nfc_send_commands(nfc->regs, NAND_CMD_READ0,
401 NAND_CMD_READSTART, READ_PAGE_CMD_CODE);
402 vf610_nfc_addr_cycle(mtd, column, page);
403 break;
404
Stefan Agner4ce682a2015-05-08 19:07:13 +0200405 case NAND_CMD_PARAM:
406 nfc->alt_buf = ALT_BUF_ONFI;
Stefan Agner59ae13a2015-10-13 22:11:42 -0700407 trfr_sz = 3 * sizeof(struct nand_onfi_params);
408 vf610_nfc_transfer_size(nfc->regs, trfr_sz);
Stefan Agner4ce682a2015-05-08 19:07:13 +0200409 vf610_nfc_send_command(nfc->regs, NAND_CMD_PARAM,
410 READ_ONFI_PARAM_CMD_CODE);
411 vf610_nfc_set_field(mtd, NFC_ROW_ADDR, ROW_ADDR_MASK,
412 ROW_ADDR_SHIFT, column);
413 vf610_nfc_ecc_mode(mtd, ECC_BYPASS);
414 break;
415
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200416 case NAND_CMD_ERASE1:
Stefan Agner11f1fa02015-03-24 17:54:20 +0100417 vf610_nfc_transfer_size(nfc->regs, 0);
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200418 vf610_nfc_send_commands(nfc->regs, command,
419 NAND_CMD_ERASE2, ERASE_CMD_CODE);
420 vf610_nfc_addr_cycle(mtd, column, page);
421 break;
422
423 case NAND_CMD_READID:
424 nfc->alt_buf = ALT_BUF_ID;
Stefan Agner59ae13a2015-10-13 22:11:42 -0700425 nfc->buf_offset = 0;
Stefan Agner11f1fa02015-03-24 17:54:20 +0100426 vf610_nfc_transfer_size(nfc->regs, 0);
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200427 vf610_nfc_send_command(nfc->regs, command, READ_ID_CMD_CODE);
Stefan Agner4ce682a2015-05-08 19:07:13 +0200428 vf610_nfc_set_field(mtd, NFC_ROW_ADDR, ROW_ADDR_MASK,
429 ROW_ADDR_SHIFT, column);
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200430 break;
431
432 case NAND_CMD_STATUS:
433 nfc->alt_buf = ALT_BUF_STAT;
Stefan Agner11f1fa02015-03-24 17:54:20 +0100434 vf610_nfc_transfer_size(nfc->regs, 0);
Stefan Agner59ae13a2015-10-13 22:11:42 -0700435 vf610_nfc_send_command(nfc->regs, command, STATUS_READ_CMD_CODE);
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200436 break;
437 default:
438 return;
439 }
440
441 vf610_nfc_done(mtd);
Stefan Agner59ae13a2015-10-13 22:11:42 -0700442
443 nfc->write_sz = 0;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200444}
445
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200446/* Read data from NFC buffers */
447static void vf610_nfc_read_buf(struct mtd_info *mtd, u_char *buf, int len)
448{
449 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
Stefan Agner59ae13a2015-10-13 22:11:42 -0700450 uint c = nfc->buf_offset;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200451
Stefan Agner4ce682a2015-05-08 19:07:13 +0200452 /* Alternate buffers are only supported through read_byte */
453 if (nfc->alt_buf)
454 return;
455
456 vf610_nfc_memcpy(buf, nfc->regs + NFC_MAIN_AREA(0) + c, len);
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200457
Stefan Agner59ae13a2015-10-13 22:11:42 -0700458 nfc->buf_offset += len;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200459}
460
461/* Write data to NFC buffers */
Stefan Agner59ae13a2015-10-13 22:11:42 -0700462static void vf610_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200463 int len)
464{
465 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
Stefan Agner59ae13a2015-10-13 22:11:42 -0700466 uint c = nfc->buf_offset;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200467 uint l;
468
Stefan Agner59ae13a2015-10-13 22:11:42 -0700469 l = min_t(uint, len, mtd->writesize + mtd->oobsize - c);
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200470 vf610_nfc_memcpy(nfc->regs + NFC_MAIN_AREA(0) + c, buf, l);
Stefan Agner59ae13a2015-10-13 22:11:42 -0700471
472 nfc->write_sz += l;
473 nfc->buf_offset += l;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200474}
475
476/* Read byte from NFC buffers */
Stefan Agner59ae13a2015-10-13 22:11:42 -0700477static uint8_t vf610_nfc_read_byte(struct mtd_info *mtd)
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200478{
Stefan Agner4ce682a2015-05-08 19:07:13 +0200479 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200480 u8 tmp;
Stefan Agner59ae13a2015-10-13 22:11:42 -0700481 uint c = nfc->buf_offset;
Stefan Agner4ce682a2015-05-08 19:07:13 +0200482
483 switch (nfc->alt_buf) {
484 case ALT_BUF_ID:
485 tmp = vf610_nfc_get_id(mtd, c);
486 break;
487 case ALT_BUF_STAT:
488 tmp = vf610_nfc_get_status(mtd);
489 break;
Stefan Agner4ce682a2015-05-08 19:07:13 +0200490#ifdef __LITTLE_ENDIAN
Stefan Agner59ae13a2015-10-13 22:11:42 -0700491 case ALT_BUF_ONFI:
Stefan Agner4ce682a2015-05-08 19:07:13 +0200492 /* Reverse byte since the controller uses big endianness */
Stefan Agner59ae13a2015-10-13 22:11:42 -0700493 c = nfc->buf_offset ^ 0x3;
494 /* fall-through */
Stefan Agner4ce682a2015-05-08 19:07:13 +0200495#endif
496 default:
497 tmp = *((u8 *)(nfc->regs + NFC_MAIN_AREA(0) + c));
498 break;
499 }
Stefan Agner59ae13a2015-10-13 22:11:42 -0700500 nfc->buf_offset++;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200501 return tmp;
502}
503
504/* Read word from NFC buffers */
505static u16 vf610_nfc_read_word(struct mtd_info *mtd)
506{
507 u16 tmp;
Stefan Agner59ae13a2015-10-13 22:11:42 -0700508
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200509 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. */
514static 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 */
523static void vf610_nfc_select_chip(struct mtd_info *mtd, int chip)
524{
525#ifdef CONFIG_VF610
526 u32 tmp = vf610_nfc_read(mtd, NFC_ROW_ADDR);
527 tmp &= ~(ROW_ADDR_CHIP_SEL_RB_MASK | ROW_ADDR_CHIP_SEL_MASK);
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200528
Stefan Agner59ae13a2015-10-13 22:11:42 -0700529 if (chip >= 0) {
530 tmp |= 1 << ROW_ADDR_CHIP_SEL_RB_SHIFT;
531 tmp |= (1 << chip) << ROW_ADDR_CHIP_SEL_SHIFT;
532 }
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200533
534 vf610_nfc_write(mtd, NFC_ROW_ADDR, tmp);
535#endif
536}
537
538/* Count the number of 0's in buff upto max_bits */
539static inline int count_written_bits(uint8_t *buff, int size, int max_bits)
540{
541 uint32_t *buff32 = (uint32_t *)buff;
542 int k, written_bits = 0;
543
544 for (k = 0; k < (size / 4); k++) {
545 written_bits += hweight32(~buff32[k]);
546 if (written_bits > max_bits)
547 break;
548 }
549
550 return written_bits;
551}
552
Stefan Agner59ae13a2015-10-13 22:11:42 -0700553static inline int vf610_nfc_correct_data(struct mtd_info *mtd, uint8_t *dat,
554 uint8_t *oob, int page)
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200555{
556 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
Stefan Agner59ae13a2015-10-13 22:11:42 -0700557 u32 ecc_status_off = NFC_MAIN_AREA(0) + ECC_SRAM_ADDR + ECC_STATUS;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200558 u8 ecc_status;
559 u8 ecc_count;
Stefan Agner59ae13a2015-10-13 22:11:42 -0700560 int flips;
561 int flips_threshold = nfc->chip.ecc.strength / 2;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200562
Stefan Agner59ae13a2015-10-13 22:11:42 -0700563 ecc_status = vf610_nfc_read(mtd, ecc_status_off) & 0xff;
564 ecc_count = ecc_status & ECC_STATUS_ERR_COUNT;
565
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200566 if (!(ecc_status & ECC_STATUS_MASK))
567 return ecc_count;
568
Stefan Agner59ae13a2015-10-13 22:11:42 -0700569 /* Read OOB without ECC unit enabled */
570 vf610_nfc_command(mtd, NAND_CMD_READOOB, 0, page);
571 vf610_nfc_read_buf(mtd, oob, mtd->oobsize);
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200572
Stefan Agner59ae13a2015-10-13 22:11:42 -0700573 /*
574 * On an erased page, bit count (including OOB) should be zero or
575 * at least less then half of the ECC strength.
576 */
577 flips = count_written_bits(dat, nfc->chip.ecc.size, flips_threshold);
578 flips += count_written_bits(oob, mtd->oobsize, flips_threshold);
579
580 if (unlikely(flips > flips_threshold))
581 return -EINVAL;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200582
583 /* Erased page. */
584 memset(dat, 0xff, nfc->chip.ecc.size);
Stefan Agner59ae13a2015-10-13 22:11:42 -0700585 memset(oob, 0xff, mtd->oobsize);
586 return flips;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200587}
588
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200589static int vf610_nfc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
590 uint8_t *buf, int oob_required, int page)
591{
592 int eccsize = chip->ecc.size;
593 int stat;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200594
Stefan Agner59ae13a2015-10-13 22:11:42 -0700595 vf610_nfc_read_buf(mtd, buf, eccsize);
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200596 if (oob_required)
597 vf610_nfc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
598
Stefan Agner59ae13a2015-10-13 22:11:42 -0700599 stat = vf610_nfc_correct_data(mtd, buf, chip->oob_poi, page);
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200600
Stefan Agner59ae13a2015-10-13 22:11:42 -0700601 if (stat < 0) {
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200602 mtd->ecc_stats.failed++;
Stefan Agner59ae13a2015-10-13 22:11:42 -0700603 return 0;
604 } else {
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200605 mtd->ecc_stats.corrected += stat;
Stefan Agner59ae13a2015-10-13 22:11:42 -0700606 return stat;
607 }
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200608}
609
610/*
611 * ECC will be calculated automatically
612 */
613static int vf610_nfc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -0500614 const uint8_t *buf, int oob_required, int page)
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200615{
Stefan Agner59ae13a2015-10-13 22:11:42 -0700616 struct vf610_nfc *nfc = mtd_to_nfc(mtd);
617
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200618 vf610_nfc_write_buf(mtd, buf, mtd->writesize);
619 if (oob_required)
620 vf610_nfc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
621
Stefan Agner59ae13a2015-10-13 22:11:42 -0700622 /* Always write whole page including OOB due to HW ECC */
623 nfc->write_sz = mtd->writesize + mtd->oobsize;
624
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200625 return 0;
626}
627
628struct vf610_nfc_config {
629 int hardware_ecc;
630 int width;
631 int flash_bbt;
632};
633
634static int vf610_nfc_nand_init(int devnum, void __iomem *addr)
635{
Scott Wood2c1b7e12016-05-30 13:57:55 -0500636 struct mtd_info *mtd;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200637 struct nand_chip *chip;
638 struct vf610_nfc *nfc;
639 int err = 0;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200640 struct vf610_nfc_config cfg = {
641 .hardware_ecc = 1,
642#ifdef CONFIG_SYS_NAND_BUSWIDTH_16BIT
643 .width = 16,
644#else
645 .width = 8,
646#endif
647 .flash_bbt = 1,
648 };
649
Lukasz Majewskie94930b2018-12-03 10:20:47 +0100650 nfc = calloc(1, sizeof(*nfc));
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200651 if (!nfc) {
652 printf(KERN_ERR "%s: Memory exhausted!\n", __func__);
653 return -ENOMEM;
654 }
655
656 chip = &nfc->chip;
657 nfc->regs = addr;
658
Scott Wood17fed142016-05-30 13:57:56 -0500659 mtd = nand_to_mtd(chip);
660 nand_set_controller_data(chip, nfc);
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200661
Stefan Agner4ce682a2015-05-08 19:07:13 +0200662 if (cfg.width == 16)
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200663 chip->options |= NAND_BUSWIDTH_16;
Stefan Agner4ce682a2015-05-08 19:07:13 +0200664
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200665 chip->dev_ready = vf610_nfc_dev_ready;
666 chip->cmdfunc = vf610_nfc_command;
667 chip->read_byte = vf610_nfc_read_byte;
668 chip->read_word = vf610_nfc_read_word;
669 chip->read_buf = vf610_nfc_read_buf;
670 chip->write_buf = vf610_nfc_write_buf;
671 chip->select_chip = vf610_nfc_select_chip;
672
Stefan Agner59ae13a2015-10-13 22:11:42 -0700673 chip->options |= NAND_NO_SUBPAGE_WRITE;
674
675 chip->ecc.size = PAGE_2K;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200676
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200677 /* Set configuration register. */
Stefan Agner59ae13a2015-10-13 22:11:42 -0700678 vf610_nfc_clear(mtd, NFC_FLASH_CONFIG, CONFIG_16BIT);
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200679 vf610_nfc_clear(mtd, NFC_FLASH_CONFIG, CONFIG_ADDR_AUTO_INCR_BIT);
680 vf610_nfc_clear(mtd, NFC_FLASH_CONFIG, CONFIG_BUFNO_AUTO_INCR_BIT);
681 vf610_nfc_clear(mtd, NFC_FLASH_CONFIG, CONFIG_BOOT_MODE_BIT);
682 vf610_nfc_clear(mtd, NFC_FLASH_CONFIG, CONFIG_DMA_REQ_BIT);
683 vf610_nfc_set(mtd, NFC_FLASH_CONFIG, CONFIG_FAST_FLASH_BIT);
684
Stefan Agner59ae13a2015-10-13 22:11:42 -0700685 /* Disable virtual pages, only one elementary transfer unit */
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200686 vf610_nfc_set_field(mtd, NFC_FLASH_CONFIG, CONFIG_PAGE_CNT_MASK,
687 CONFIG_PAGE_CNT_SHIFT, 1);
688
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200689 /* first scan to find the device and get the page size */
690 if (nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_DEVICE, NULL)) {
691 err = -ENXIO;
692 goto error;
693 }
694
Stefan Agner4ce682a2015-05-08 19:07:13 +0200695 if (cfg.width == 16)
696 vf610_nfc_set(mtd, NFC_FLASH_CONFIG, CONFIG_16BIT);
697
Stefan Agner59ae13a2015-10-13 22:11:42 -0700698 /* Bad block options. */
699 if (cfg.flash_bbt)
700 chip->bbt_options = NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB |
701 NAND_BBT_CREATE;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200702
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200703 /* Single buffer only, max 256 OOB minus ECC status */
Stefan Agner59ae13a2015-10-13 22:11:42 -0700704 if (mtd->writesize + mtd->oobsize > PAGE_2K + OOB_MAX - 8) {
705 dev_err(nfc->dev, "Unsupported flash page size\n");
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200706 err = -ENXIO;
707 goto error;
708 }
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200709
710 if (cfg.hardware_ecc) {
711 if (mtd->writesize != PAGE_2K && mtd->oobsize < 64) {
712 dev_err(nfc->dev, "Unsupported flash with hwecc\n");
713 err = -ENXIO;
714 goto error;
715 }
716
Stefan Agner59ae13a2015-10-13 22:11:42 -0700717 if (chip->ecc.size != mtd->writesize) {
718 dev_err(nfc->dev, "ecc size: %d\n", chip->ecc.size);
719 dev_err(nfc->dev, "Step size needs to be page size\n");
720 err = -ENXIO;
721 goto error;
722 }
723
Stefan Agnerfe10d3f2015-05-08 19:07:12 +0200724 /* Current HW ECC layouts only use 64 bytes of OOB */
725 if (mtd->oobsize > 64)
726 mtd->oobsize = 64;
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200727
728 /* propagate ecc.layout to mtd_info */
729 mtd->ecclayout = chip->ecc.layout;
730 chip->ecc.read_page = vf610_nfc_read_page;
731 chip->ecc.write_page = vf610_nfc_write_page;
732 chip->ecc.mode = NAND_ECC_HW;
733
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200734 chip->ecc.size = PAGE_2K;
Stefan Agnerfe10d3f2015-05-08 19:07:12 +0200735 chip->ecc.layout = &vf610_nfc_ecc;
736#if defined(CONFIG_SYS_NAND_VF610_NFC_45_ECC_BYTES)
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200737 chip->ecc.strength = 24;
Stefan Agnerfe10d3f2015-05-08 19:07:12 +0200738 chip->ecc.bytes = 45;
739#elif defined(CONFIG_SYS_NAND_VF610_NFC_60_ECC_BYTES)
740 chip->ecc.strength = 32;
741 chip->ecc.bytes = 60;
742#endif
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200743
Stefan Agner59ae13a2015-10-13 22:11:42 -0700744 /* Set ECC_STATUS offset */
745 vf610_nfc_set_field(mtd, NFC_FLASH_CONFIG,
746 CONFIG_ECC_SRAM_ADDR_MASK,
747 CONFIG_ECC_SRAM_ADDR_SHIFT,
748 ECC_SRAM_ADDR >> 3);
749
750 /* Enable ECC status in SRAM */
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200751 vf610_nfc_set(mtd, NFC_FLASH_CONFIG, CONFIG_ECC_SRAM_REQ_BIT);
752 }
753
754 /* second phase scan */
755 err = nand_scan_tail(mtd);
756 if (err)
757 return err;
758
Scott Wood2c1b7e12016-05-30 13:57:55 -0500759 err = nand_register(devnum, mtd);
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200760 if (err)
761 return err;
762
763 return 0;
764
765error:
766 return err;
767}
768
Lukasz Majewskia8f1a672018-12-03 10:24:51 +0100769#if CONFIG_NAND_VF610_NFC_DT
770static const struct udevice_id vf610_nfc_dt_ids[] = {
771 {
772 .compatible = "fsl,vf610-nfc",
773 },
774 { /* sentinel */ }
775};
776
777static int vf610_nfc_dt_probe(struct udevice *dev)
778{
779 struct resource res;
780 int ret;
781
782 ret = dev_read_resource(dev, 0, &res);
783 if (ret)
784 return ret;
785
786 return vf610_nfc_nand_init(0, devm_ioremap(dev, res.start,
787 resource_size(&res)));
788}
789
790U_BOOT_DRIVER(vf610_nfc_dt) = {
791 .name = "vf610-nfc-dt",
792 .id = UCLASS_MTD,
793 .of_match = vf610_nfc_dt_ids,
794 .probe = vf610_nfc_dt_probe,
795};
796
797void board_nand_init(void)
798{
799 struct udevice *dev;
800 int ret;
801
802 ret = uclass_get_device_by_driver(UCLASS_MTD,
803 DM_GET_DRIVER(vf610_nfc_dt),
804 &dev);
805 if (ret && ret != -ENODEV)
806 pr_err("Failed to initialize NAND controller. (error %d)\n",
807 ret);
808}
809#else
Stefan Agnerd6b1ccb2014-09-12 13:06:35 +0200810void board_nand_init(void)
811{
812 int err = vf610_nfc_nand_init(0, (void __iomem *)CONFIG_SYS_NAND_BASE);
813 if (err)
814 printf("VF610 NAND init failed (err %d)\n", err);
815}
Lukasz Majewskia8f1a672018-12-03 10:24:51 +0100816#endif /* CONFIG_NAND_VF610_NFC_DT */