blob: ddfd75d32d069766edf23760a520af896c7fd147 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Scott Wood40c6e092008-03-21 16:12:51 -05002/* Freescale Enhanced Local Bus Controller FCM NAND driver
3 *
4 * Copyright (c) 2006-2008 Freescale Semiconductor
5 *
6 * Authors: Nick Spence <nick.spence@freescale.com>,
7 * Scott Wood <scottwood@freescale.com>
Scott Wood40c6e092008-03-21 16:12:51 -05008 */
9
10#include <common.h>
Simon Glassadaaa482019-11-14 12:57:43 -070011#include <command.h>
Scott Wood40c6e092008-03-21 16:12:51 -050012#include <malloc.h>
Scott Woodea0da782012-01-12 19:42:58 -060013#include <nand.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070014#include <dm/devres.h>
Scott Wood40c6e092008-03-21 16:12:51 -050015
16#include <linux/mtd/mtd.h>
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090017#include <linux/mtd/rawnand.h>
Scott Wood40c6e092008-03-21 16:12:51 -050018#include <linux/mtd/nand_ecc.h>
19
20#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090021#include <linux/errno.h>
Scott Wood40c6e092008-03-21 16:12:51 -050022
23#ifdef VERBOSE_DEBUG
24#define DEBUG_ELBC
25#define vdbg(format, arg...) printf("DEBUG: " format, ##arg)
26#else
27#define vdbg(format, arg...) do {} while (0)
28#endif
29
30/* Can't use plain old DEBUG because the linux mtd
31 * headers define it as a macro.
32 */
33#ifdef DEBUG_ELBC
34#define dbg(format, arg...) printf("DEBUG: " format, ##arg)
35#else
36#define dbg(format, arg...) do {} while (0)
37#endif
38
39#define MAX_BANKS 8
40#define ERR_BYTE 0xFF /* Value returned for read bytes when read failed */
Scott Wood40c6e092008-03-21 16:12:51 -050041
42#define LTESR_NAND_MASK (LTESR_FCT | LTESR_PAR | LTESR_CC)
43
44struct fsl_elbc_ctrl;
45
46/* mtd information per set */
47
48struct fsl_elbc_mtd {
Scott Wood40c6e092008-03-21 16:12:51 -050049 struct nand_chip chip;
50 struct fsl_elbc_ctrl *ctrl;
51
52 struct device *dev;
53 int bank; /* Chip select bank number */
54 u8 __iomem *vbase; /* Chip select base virtual address */
55 int page_size; /* NAND page size (0=512, 1=2048) */
56 unsigned int fmr; /* FCM Flash Mode Register value */
57};
58
59/* overview of the fsl elbc controller */
60
61struct fsl_elbc_ctrl {
62 struct nand_hw_control controller;
63 struct fsl_elbc_mtd *chips[MAX_BANKS];
64
65 /* device info */
Becky Bruce0d4cee12010-06-17 11:37:20 -050066 fsl_lbc_t *regs;
Scott Wood40c6e092008-03-21 16:12:51 -050067 u8 __iomem *addr; /* Address of assigned FCM buffer */
68 unsigned int page; /* Last page written to / read from */
69 unsigned int read_bytes; /* Number of bytes read during command */
70 unsigned int column; /* Saved column from SEQIN */
71 unsigned int index; /* Pointer to next byte to 'read' */
72 unsigned int status; /* status read from LTESR after last op */
73 unsigned int mdr; /* UPM/FCM Data Register value */
74 unsigned int use_mdr; /* Non zero if the MDR is to be set */
75 unsigned int oob; /* Non zero if operating on OOB data */
Scott Wood40c6e092008-03-21 16:12:51 -050076};
77
78/* These map to the positions used by the FCM hardware ECC generator */
79
80/* Small Page FLASH with FMR[ECCM] = 0 */
81static struct nand_ecclayout fsl_elbc_oob_sp_eccm0 = {
82 .eccbytes = 3,
83 .eccpos = {6, 7, 8},
84 .oobfree = { {0, 5}, {9, 7} },
Scott Wood40c6e092008-03-21 16:12:51 -050085};
86
87/* Small Page FLASH with FMR[ECCM] = 1 */
88static struct nand_ecclayout fsl_elbc_oob_sp_eccm1 = {
89 .eccbytes = 3,
90 .eccpos = {8, 9, 10},
91 .oobfree = { {0, 5}, {6, 2}, {11, 5} },
Scott Wood40c6e092008-03-21 16:12:51 -050092};
93
94/* Large Page FLASH with FMR[ECCM] = 0 */
95static struct nand_ecclayout fsl_elbc_oob_lp_eccm0 = {
96 .eccbytes = 12,
97 .eccpos = {6, 7, 8, 22, 23, 24, 38, 39, 40, 54, 55, 56},
98 .oobfree = { {1, 5}, {9, 13}, {25, 13}, {41, 13}, {57, 7} },
Scott Wood40c6e092008-03-21 16:12:51 -050099};
100
101/* Large Page FLASH with FMR[ECCM] = 1 */
102static struct nand_ecclayout fsl_elbc_oob_lp_eccm1 = {
103 .eccbytes = 12,
104 .eccpos = {8, 9, 10, 24, 25, 26, 40, 41, 42, 56, 57, 58},
105 .oobfree = { {1, 7}, {11, 13}, {27, 13}, {43, 13}, {59, 5} },
Scott Wood40c6e092008-03-21 16:12:51 -0500106};
107
Anton Vorontsov5e0400f2008-06-27 23:04:04 +0400108/*
109 * fsl_elbc_oob_lp_eccm* specify that LP NAND's OOB free area starts at offset
110 * 1, so we have to adjust bad block pattern. This pattern should be used for
111 * x8 chips only. So far hardware does not support x16 chips anyway.
112 */
113static u8 scan_ff_pattern[] = { 0xff, };
114
115static struct nand_bbt_descr largepage_memorybased = {
116 .options = 0,
117 .offs = 0,
118 .len = 1,
119 .pattern = scan_ff_pattern,
120};
121
Anton Vorontsovd481b072008-06-27 23:04:13 +0400122/*
123 * ELBC may use HW ECC, so that OOB offsets, that NAND core uses for bbt,
124 * interfere with ECC positions, that's why we implement our own descriptors.
125 * OOB {11, 5}, works for both SP and LP chips, with ECCM = 1 and ECCM = 0.
126 */
127static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
128static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
129
130static struct nand_bbt_descr bbt_main_descr = {
131 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
132 NAND_BBT_2BIT | NAND_BBT_VERSION,
133 .offs = 11,
134 .len = 4,
135 .veroffs = 15,
136 .maxblocks = 4,
137 .pattern = bbt_pattern,
138};
139
140static struct nand_bbt_descr bbt_mirror_descr = {
141 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
142 NAND_BBT_2BIT | NAND_BBT_VERSION,
143 .offs = 11,
144 .len = 4,
145 .veroffs = 15,
146 .maxblocks = 4,
147 .pattern = mirror_pattern,
148};
149
Scott Wood40c6e092008-03-21 16:12:51 -0500150/*=================================*/
151
152/*
153 * Set up the FCM hardware block and page address fields, and the fcm
154 * structure addr field to point to the correct FCM buffer in memory
155 */
156static void set_addr(struct mtd_info *mtd, int column, int page_addr, int oob)
157{
Scott Wood17fed142016-05-30 13:57:56 -0500158 struct nand_chip *chip = mtd_to_nand(mtd);
159 struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
Scott Wood40c6e092008-03-21 16:12:51 -0500160 struct fsl_elbc_ctrl *ctrl = priv->ctrl;
Becky Bruce0d4cee12010-06-17 11:37:20 -0500161 fsl_lbc_t *lbc = ctrl->regs;
Scott Wood40c6e092008-03-21 16:12:51 -0500162 int buf_num;
163
164 ctrl->page = page_addr;
165
Scott Wood40c6e092008-03-21 16:12:51 -0500166 if (priv->page_size) {
Scott Woodf82f59b2008-05-22 15:02:46 -0500167 out_be32(&lbc->fbar, page_addr >> 6);
Scott Wood40c6e092008-03-21 16:12:51 -0500168 out_be32(&lbc->fpar,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200169 ((page_addr << FPAR_LP_PI_SHIFT) & FPAR_LP_PI) |
170 (oob ? FPAR_LP_MS : 0) | column);
Scott Wood40c6e092008-03-21 16:12:51 -0500171 buf_num = (page_addr & 1) << 2;
172 } else {
Scott Woodf82f59b2008-05-22 15:02:46 -0500173 out_be32(&lbc->fbar, page_addr >> 5);
Scott Wood40c6e092008-03-21 16:12:51 -0500174 out_be32(&lbc->fpar,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200175 ((page_addr << FPAR_SP_PI_SHIFT) & FPAR_SP_PI) |
176 (oob ? FPAR_SP_MS : 0) | column);
Scott Wood40c6e092008-03-21 16:12:51 -0500177 buf_num = page_addr & 7;
178 }
179
180 ctrl->addr = priv->vbase + buf_num * 1024;
181 ctrl->index = column;
182
183 /* for OOB data point to the second half of the buffer */
184 if (oob)
185 ctrl->index += priv->page_size ? 2048 : 512;
186
187 vdbg("set_addr: bank=%d, ctrl->addr=0x%p (0x%p), "
188 "index %x, pes %d ps %d\n",
189 buf_num, ctrl->addr, priv->vbase, ctrl->index,
190 chip->phys_erase_shift, chip->page_shift);
191}
192
193/*
194 * execute FCM command and wait for it to complete
195 */
196static int fsl_elbc_run_command(struct mtd_info *mtd)
197{
Scott Wood17fed142016-05-30 13:57:56 -0500198 struct nand_chip *chip = mtd_to_nand(mtd);
199 struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
Scott Wood40c6e092008-03-21 16:12:51 -0500200 struct fsl_elbc_ctrl *ctrl = priv->ctrl;
Becky Bruce0d4cee12010-06-17 11:37:20 -0500201 fsl_lbc_t *lbc = ctrl->regs;
Prabhakar Kushwahad7550e12014-09-23 09:57:47 +0530202 u32 timeo = (CONFIG_SYS_HZ * 10) / 1000;
203 u32 time_start;
Scott Wood40c6e092008-03-21 16:12:51 -0500204 u32 ltesr;
205
206 /* Setup the FMR[OP] to execute without write protection */
207 out_be32(&lbc->fmr, priv->fmr | 3);
208 if (ctrl->use_mdr)
209 out_be32(&lbc->mdr, ctrl->mdr);
210
211 vdbg("fsl_elbc_run_command: fmr=%08x fir=%08x fcr=%08x\n",
212 in_be32(&lbc->fmr), in_be32(&lbc->fir), in_be32(&lbc->fcr));
213 vdbg("fsl_elbc_run_command: fbar=%08x fpar=%08x "
214 "fbcr=%08x bank=%d\n",
215 in_be32(&lbc->fbar), in_be32(&lbc->fpar),
216 in_be32(&lbc->fbcr), priv->bank);
217
218 /* execute special operation */
219 out_be32(&lbc->lsor, priv->bank);
220
221 /* wait for FCM complete flag or timeout */
Prabhakar Kushwahad7550e12014-09-23 09:57:47 +0530222 time_start = get_timer(0);
Scott Wood40c6e092008-03-21 16:12:51 -0500223
224 ltesr = 0;
Prabhakar Kushwahad7550e12014-09-23 09:57:47 +0530225 while (get_timer(time_start) < timeo) {
Scott Wood40c6e092008-03-21 16:12:51 -0500226 ltesr = in_be32(&lbc->ltesr);
227 if (ltesr & LTESR_CC)
228 break;
229 }
230
231 ctrl->status = ltesr & LTESR_NAND_MASK;
232 out_be32(&lbc->ltesr, ctrl->status);
233 out_be32(&lbc->lteatr, 0);
234
235 /* store mdr value in case it was needed */
236 if (ctrl->use_mdr)
237 ctrl->mdr = in_be32(&lbc->mdr);
238
239 ctrl->use_mdr = 0;
240
241 vdbg("fsl_elbc_run_command: stat=%08x mdr=%08x fmr=%08x\n",
242 ctrl->status, ctrl->mdr, in_be32(&lbc->fmr));
243
244 /* returns 0 on success otherwise non-zero) */
245 return ctrl->status == LTESR_CC ? 0 : -EIO;
246}
247
248static void fsl_elbc_do_read(struct nand_chip *chip, int oob)
249{
Scott Wood17fed142016-05-30 13:57:56 -0500250 struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
Scott Wood40c6e092008-03-21 16:12:51 -0500251 struct fsl_elbc_ctrl *ctrl = priv->ctrl;
Becky Bruce0d4cee12010-06-17 11:37:20 -0500252 fsl_lbc_t *lbc = ctrl->regs;
Scott Wood40c6e092008-03-21 16:12:51 -0500253
254 if (priv->page_size) {
255 out_be32(&lbc->fir,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200256 (FIR_OP_CW0 << FIR_OP0_SHIFT) |
257 (FIR_OP_CA << FIR_OP1_SHIFT) |
258 (FIR_OP_PA << FIR_OP2_SHIFT) |
259 (FIR_OP_CW1 << FIR_OP3_SHIFT) |
260 (FIR_OP_RBW << FIR_OP4_SHIFT));
Scott Wood40c6e092008-03-21 16:12:51 -0500261
262 out_be32(&lbc->fcr, (NAND_CMD_READ0 << FCR_CMD0_SHIFT) |
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200263 (NAND_CMD_READSTART << FCR_CMD1_SHIFT));
Scott Wood40c6e092008-03-21 16:12:51 -0500264 } else {
265 out_be32(&lbc->fir,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200266 (FIR_OP_CW0 << FIR_OP0_SHIFT) |
267 (FIR_OP_CA << FIR_OP1_SHIFT) |
268 (FIR_OP_PA << FIR_OP2_SHIFT) |
269 (FIR_OP_RBW << FIR_OP3_SHIFT));
Scott Wood40c6e092008-03-21 16:12:51 -0500270
271 if (oob)
272 out_be32(&lbc->fcr,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200273 NAND_CMD_READOOB << FCR_CMD0_SHIFT);
Scott Wood40c6e092008-03-21 16:12:51 -0500274 else
275 out_be32(&lbc->fcr, NAND_CMD_READ0 << FCR_CMD0_SHIFT);
276 }
277}
278
279/* cmdfunc send commands to the FCM */
280static void fsl_elbc_cmdfunc(struct mtd_info *mtd, unsigned int command,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200281 int column, int page_addr)
Scott Wood40c6e092008-03-21 16:12:51 -0500282{
Scott Wood17fed142016-05-30 13:57:56 -0500283 struct nand_chip *chip = mtd_to_nand(mtd);
284 struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
Scott Wood40c6e092008-03-21 16:12:51 -0500285 struct fsl_elbc_ctrl *ctrl = priv->ctrl;
Becky Bruce0d4cee12010-06-17 11:37:20 -0500286 fsl_lbc_t *lbc = ctrl->regs;
Scott Wood40c6e092008-03-21 16:12:51 -0500287
288 ctrl->use_mdr = 0;
289
290 /* clear the read buffer */
291 ctrl->read_bytes = 0;
292 if (command != NAND_CMD_PAGEPROG)
293 ctrl->index = 0;
294
295 switch (command) {
296 /* READ0 and READ1 read the entire buffer to use hardware ECC. */
297 case NAND_CMD_READ1:
298 column += 256;
299
300 /* fall-through */
301 case NAND_CMD_READ0:
302 vdbg("fsl_elbc_cmdfunc: NAND_CMD_READ0, page_addr:"
303 " 0x%x, column: 0x%x.\n", page_addr, column);
304
305 out_be32(&lbc->fbcr, 0); /* read entire page to enable ECC */
306 set_addr(mtd, 0, page_addr, 0);
307
308 ctrl->read_bytes = mtd->writesize + mtd->oobsize;
309 ctrl->index += column;
310
311 fsl_elbc_do_read(chip, 0);
312 fsl_elbc_run_command(mtd);
313 return;
314
315 /* READOOB reads only the OOB because no ECC is performed. */
316 case NAND_CMD_READOOB:
317 vdbg("fsl_elbc_cmdfunc: NAND_CMD_READOOB, page_addr:"
318 " 0x%x, column: 0x%x.\n", page_addr, column);
319
320 out_be32(&lbc->fbcr, mtd->oobsize - column);
321 set_addr(mtd, column, page_addr, 1);
322
323 ctrl->read_bytes = mtd->writesize + mtd->oobsize;
324
325 fsl_elbc_do_read(chip, 1);
326 fsl_elbc_run_command(mtd);
327
328 return;
329
330 /* READID must read all 5 possible bytes while CEB is active */
331 case NAND_CMD_READID:
Shengzhou Liue9635022011-12-12 17:49:57 +0800332 case NAND_CMD_PARAM:
333 vdbg("fsl_elbc_cmdfunc: NAND_CMD 0x%x.\n", command);
Scott Wood40c6e092008-03-21 16:12:51 -0500334
335 out_be32(&lbc->fir, (FIR_OP_CW0 << FIR_OP0_SHIFT) |
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200336 (FIR_OP_UA << FIR_OP1_SHIFT) |
337 (FIR_OP_RBW << FIR_OP2_SHIFT));
Shengzhou Liue9635022011-12-12 17:49:57 +0800338 out_be32(&lbc->fcr, command << FCR_CMD0_SHIFT);
339 /*
340 * although currently it's 8 bytes for READID, we always read
341 * the maximum 256 bytes(for PARAM)
342 */
343 out_be32(&lbc->fbcr, 256);
344 ctrl->read_bytes = 256;
Scott Wood40c6e092008-03-21 16:12:51 -0500345 ctrl->use_mdr = 1;
Shengzhou Liue9635022011-12-12 17:49:57 +0800346 ctrl->mdr = column;
Scott Wood40c6e092008-03-21 16:12:51 -0500347 set_addr(mtd, 0, 0, 0);
348 fsl_elbc_run_command(mtd);
349 return;
350
351 /* ERASE1 stores the block and page address */
352 case NAND_CMD_ERASE1:
353 vdbg("fsl_elbc_cmdfunc: NAND_CMD_ERASE1, "
354 "page_addr: 0x%x.\n", page_addr);
355 set_addr(mtd, 0, page_addr, 0);
356 return;
357
358 /* ERASE2 uses the block and page address from ERASE1 */
359 case NAND_CMD_ERASE2:
360 vdbg("fsl_elbc_cmdfunc: NAND_CMD_ERASE2.\n");
361
362 out_be32(&lbc->fir,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200363 (FIR_OP_CW0 << FIR_OP0_SHIFT) |
364 (FIR_OP_PA << FIR_OP1_SHIFT) |
365 (FIR_OP_CM1 << FIR_OP2_SHIFT));
Scott Wood40c6e092008-03-21 16:12:51 -0500366
367 out_be32(&lbc->fcr,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200368 (NAND_CMD_ERASE1 << FCR_CMD0_SHIFT) |
369 (NAND_CMD_ERASE2 << FCR_CMD1_SHIFT));
Scott Wood40c6e092008-03-21 16:12:51 -0500370
371 out_be32(&lbc->fbcr, 0);
372 ctrl->read_bytes = 0;
373
374 fsl_elbc_run_command(mtd);
375 return;
376
377 /* SEQIN sets up the addr buffer and all registers except the length */
378 case NAND_CMD_SEQIN: {
379 u32 fcr;
380 vdbg("fsl_elbc_cmdfunc: NAND_CMD_SEQIN/PAGE_PROG, "
381 "page_addr: 0x%x, column: 0x%x.\n",
382 page_addr, column);
383
384 ctrl->column = column;
385 ctrl->oob = 0;
386
387 if (priv->page_size) {
388 fcr = (NAND_CMD_SEQIN << FCR_CMD0_SHIFT) |
389 (NAND_CMD_PAGEPROG << FCR_CMD1_SHIFT);
390
391 out_be32(&lbc->fir,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200392 (FIR_OP_CW0 << FIR_OP0_SHIFT) |
393 (FIR_OP_CA << FIR_OP1_SHIFT) |
394 (FIR_OP_PA << FIR_OP2_SHIFT) |
395 (FIR_OP_WB << FIR_OP3_SHIFT) |
396 (FIR_OP_CW1 << FIR_OP4_SHIFT));
Scott Wood40c6e092008-03-21 16:12:51 -0500397 } else {
398 fcr = (NAND_CMD_PAGEPROG << FCR_CMD1_SHIFT) |
399 (NAND_CMD_SEQIN << FCR_CMD2_SHIFT);
400
401 out_be32(&lbc->fir,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200402 (FIR_OP_CW0 << FIR_OP0_SHIFT) |
403 (FIR_OP_CM2 << FIR_OP1_SHIFT) |
404 (FIR_OP_CA << FIR_OP2_SHIFT) |
405 (FIR_OP_PA << FIR_OP3_SHIFT) |
406 (FIR_OP_WB << FIR_OP4_SHIFT) |
407 (FIR_OP_CW1 << FIR_OP5_SHIFT));
Scott Wood40c6e092008-03-21 16:12:51 -0500408
409 if (column >= mtd->writesize) {
410 /* OOB area --> READOOB */
411 column -= mtd->writesize;
412 fcr |= NAND_CMD_READOOB << FCR_CMD0_SHIFT;
413 ctrl->oob = 1;
414 } else if (column < 256) {
415 /* First 256 bytes --> READ0 */
416 fcr |= NAND_CMD_READ0 << FCR_CMD0_SHIFT;
417 } else {
418 /* Second 256 bytes --> READ1 */
419 fcr |= NAND_CMD_READ1 << FCR_CMD0_SHIFT;
420 }
421 }
422
423 out_be32(&lbc->fcr, fcr);
424 set_addr(mtd, column, page_addr, ctrl->oob);
425 return;
426 }
427
428 /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
429 case NAND_CMD_PAGEPROG: {
Scott Wood40c6e092008-03-21 16:12:51 -0500430 vdbg("fsl_elbc_cmdfunc: NAND_CMD_PAGEPROG "
431 "writing %d bytes.\n", ctrl->index);
432
433 /* if the write did not start at 0 or is not a full page
434 * then set the exact length, otherwise use a full page
435 * write so the HW generates the ECC.
436 */
437 if (ctrl->oob || ctrl->column != 0 ||
mhench05f1b8b2011-07-11 12:29:43 +0000438 ctrl->index != mtd->writesize + mtd->oobsize)
Scott Wood40c6e092008-03-21 16:12:51 -0500439 out_be32(&lbc->fbcr, ctrl->index);
mhench05f1b8b2011-07-11 12:29:43 +0000440 else
Scott Wood40c6e092008-03-21 16:12:51 -0500441 out_be32(&lbc->fbcr, 0);
Scott Wood40c6e092008-03-21 16:12:51 -0500442
443 fsl_elbc_run_command(mtd);
444
Scott Wood40c6e092008-03-21 16:12:51 -0500445 return;
446 }
447
448 /* CMD_STATUS must read the status byte while CEB is active */
449 /* Note - it does not wait for the ready line */
450 case NAND_CMD_STATUS:
451 out_be32(&lbc->fir,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200452 (FIR_OP_CM0 << FIR_OP0_SHIFT) |
453 (FIR_OP_RBW << FIR_OP1_SHIFT));
Scott Wood40c6e092008-03-21 16:12:51 -0500454 out_be32(&lbc->fcr, NAND_CMD_STATUS << FCR_CMD0_SHIFT);
455 out_be32(&lbc->fbcr, 1);
456 set_addr(mtd, 0, 0, 0);
457 ctrl->read_bytes = 1;
458
459 fsl_elbc_run_command(mtd);
460
461 /* The chip always seems to report that it is
462 * write-protected, even when it is not.
463 */
464 out_8(ctrl->addr, in_8(ctrl->addr) | NAND_STATUS_WP);
465 return;
466
467 /* RESET without waiting for the ready line */
468 case NAND_CMD_RESET:
469 dbg("fsl_elbc_cmdfunc: NAND_CMD_RESET.\n");
470 out_be32(&lbc->fir, FIR_OP_CM0 << FIR_OP0_SHIFT);
471 out_be32(&lbc->fcr, NAND_CMD_RESET << FCR_CMD0_SHIFT);
472 fsl_elbc_run_command(mtd);
473 return;
474
475 default:
476 printf("fsl_elbc_cmdfunc: error, unsupported command 0x%x.\n",
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200477 command);
Scott Wood40c6e092008-03-21 16:12:51 -0500478 }
479}
480
481static void fsl_elbc_select_chip(struct mtd_info *mtd, int chip)
482{
483 /* The hardware does not seem to support multiple
484 * chips per bank.
485 */
486}
487
488/*
489 * Write buf to the FCM Controller Data Buffer
490 */
491static void fsl_elbc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
492{
Scott Wood17fed142016-05-30 13:57:56 -0500493 struct nand_chip *chip = mtd_to_nand(mtd);
494 struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
Scott Wood40c6e092008-03-21 16:12:51 -0500495 struct fsl_elbc_ctrl *ctrl = priv->ctrl;
496 unsigned int bufsize = mtd->writesize + mtd->oobsize;
497
Anton Vorontsov42f15632008-03-28 22:10:54 +0300498 if (len <= 0) {
Scott Wood40c6e092008-03-21 16:12:51 -0500499 printf("write_buf of %d bytes", len);
500 ctrl->status = 0;
501 return;
502 }
503
504 if ((unsigned int)len > bufsize - ctrl->index) {
505 printf("write_buf beyond end of buffer "
506 "(%d requested, %u available)\n",
507 len, bufsize - ctrl->index);
508 len = bufsize - ctrl->index;
509 }
510
511 memcpy_toio(&ctrl->addr[ctrl->index], buf, len);
Anton Vorontsov42f15632008-03-28 22:10:54 +0300512 /*
513 * This is workaround for the weird elbc hangs during nand write,
514 * Scott Wood says: "...perhaps difference in how long it takes a
515 * write to make it through the localbus compared to a write to IMMR
516 * is causing problems, and sync isn't helping for some reason."
517 * Reading back the last byte helps though.
518 */
519 in_8(&ctrl->addr[ctrl->index] + len - 1);
520
Scott Wood40c6e092008-03-21 16:12:51 -0500521 ctrl->index += len;
522}
523
524/*
525 * read a byte from either the FCM hardware buffer if it has any data left
526 * otherwise issue a command to read a single byte.
527 */
528static u8 fsl_elbc_read_byte(struct mtd_info *mtd)
529{
Scott Wood17fed142016-05-30 13:57:56 -0500530 struct nand_chip *chip = mtd_to_nand(mtd);
531 struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
Scott Wood40c6e092008-03-21 16:12:51 -0500532 struct fsl_elbc_ctrl *ctrl = priv->ctrl;
533
534 /* If there are still bytes in the FCM, then use the next byte. */
535 if (ctrl->index < ctrl->read_bytes)
536 return in_8(&ctrl->addr[ctrl->index++]);
537
538 printf("read_byte beyond end of buffer\n");
539 return ERR_BYTE;
540}
541
542/*
543 * Read from the FCM Controller Data Buffer
544 */
545static void fsl_elbc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
546{
Scott Wood17fed142016-05-30 13:57:56 -0500547 struct nand_chip *chip = mtd_to_nand(mtd);
548 struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
Scott Wood40c6e092008-03-21 16:12:51 -0500549 struct fsl_elbc_ctrl *ctrl = priv->ctrl;
550 int avail;
551
552 if (len < 0)
553 return;
554
555 avail = min((unsigned int)len, ctrl->read_bytes - ctrl->index);
556 memcpy_fromio(buf, &ctrl->addr[ctrl->index], avail);
557 ctrl->index += avail;
558
559 if (len > avail)
560 printf("read_buf beyond end of buffer "
561 "(%d requested, %d available)\n",
562 len, avail);
563}
564
Scott Wood40c6e092008-03-21 16:12:51 -0500565/* This function is called after Program and Erase Operations to
566 * check for success or failure.
567 */
568static int fsl_elbc_wait(struct mtd_info *mtd, struct nand_chip *chip)
569{
Scott Wood17fed142016-05-30 13:57:56 -0500570 struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
Scott Wood40c6e092008-03-21 16:12:51 -0500571 struct fsl_elbc_ctrl *ctrl = priv->ctrl;
Becky Bruce0d4cee12010-06-17 11:37:20 -0500572 fsl_lbc_t *lbc = ctrl->regs;
Scott Wood40c6e092008-03-21 16:12:51 -0500573
574 if (ctrl->status != LTESR_CC)
575 return NAND_STATUS_FAIL;
576
577 /* Use READ_STATUS command, but wait for the device to be ready */
578 ctrl->use_mdr = 0;
579 out_be32(&lbc->fir,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200580 (FIR_OP_CW0 << FIR_OP0_SHIFT) |
581 (FIR_OP_RBW << FIR_OP1_SHIFT));
Scott Wood40c6e092008-03-21 16:12:51 -0500582 out_be32(&lbc->fcr, NAND_CMD_STATUS << FCR_CMD0_SHIFT);
583 out_be32(&lbc->fbcr, 1);
584 set_addr(mtd, 0, 0, 0);
585 ctrl->read_bytes = 1;
586
587 fsl_elbc_run_command(mtd);
588
589 if (ctrl->status != LTESR_CC)
590 return NAND_STATUS_FAIL;
591
592 /* The chip always seems to report that it is
593 * write-protected, even when it is not.
594 */
595 out_8(ctrl->addr, in_8(ctrl->addr) | NAND_STATUS_WP);
596 return fsl_elbc_read_byte(mtd);
597}
598
Sergey Lapin3a38a552013-01-14 03:46:50 +0000599static int fsl_elbc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
600 uint8_t *buf, int oob_required, int page)
Scott Wood40c6e092008-03-21 16:12:51 -0500601{
602 fsl_elbc_read_buf(mtd, buf, mtd->writesize);
603 fsl_elbc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
604
605 if (fsl_elbc_wait(mtd, chip) & NAND_STATUS_FAIL)
606 mtd->ecc_stats.failed++;
607
608 return 0;
609}
610
611/* ECC will be calculated automatically, and errors will be detected in
612 * waitfunc.
613 */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000614static int fsl_elbc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -0500615 const uint8_t *buf, int oob_required,
616 int page)
Scott Wood40c6e092008-03-21 16:12:51 -0500617{
Scott Wood40c6e092008-03-21 16:12:51 -0500618 fsl_elbc_write_buf(mtd, buf, mtd->writesize);
619 fsl_elbc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000620
621 return 0;
Scott Wood40c6e092008-03-21 16:12:51 -0500622}
623
624static struct fsl_elbc_ctrl *elbc_ctrl;
625
Scott Wood3ea94ed2015-06-26 19:03:26 -0500626/* ECC will be calculated automatically, and errors will be detected in
627 * waitfunc.
628 */
629static int fsl_elbc_write_subpage(struct mtd_info *mtd, struct nand_chip *chip,
630 uint32_t offset, uint32_t data_len,
Scott Wood46e13102016-05-30 13:57:57 -0500631 const uint8_t *buf, int oob_required, int page)
Scott Wood3ea94ed2015-06-26 19:03:26 -0500632{
633 fsl_elbc_write_buf(mtd, buf, mtd->writesize);
634 fsl_elbc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
635
636 return 0;
637}
638
Scott Wood40c6e092008-03-21 16:12:51 -0500639static void fsl_elbc_ctrl_init(void)
640{
Scott Wood40c6e092008-03-21 16:12:51 -0500641 elbc_ctrl = kzalloc(sizeof(*elbc_ctrl), GFP_KERNEL);
642 if (!elbc_ctrl)
643 return;
644
Becky Bruce0d4cee12010-06-17 11:37:20 -0500645 elbc_ctrl->regs = LBC_BASE_ADDR;
Scott Wood40c6e092008-03-21 16:12:51 -0500646
647 /* clear event registers */
648 out_be32(&elbc_ctrl->regs->ltesr, LTESR_NAND_MASK);
649 out_be32(&elbc_ctrl->regs->lteatr, 0);
650
651 /* Enable interrupts for any detected events */
652 out_be32(&elbc_ctrl->regs->lteir, LTESR_NAND_MASK);
653
654 elbc_ctrl->read_bytes = 0;
655 elbc_ctrl->index = 0;
656 elbc_ctrl->addr = NULL;
657}
658
Scott Woodea0da782012-01-12 19:42:58 -0600659static int fsl_elbc_chip_init(int devnum, u8 *addr)
Scott Wood40c6e092008-03-21 16:12:51 -0500660{
Scott Wood2c1b7e12016-05-30 13:57:55 -0500661 struct mtd_info *mtd;
Scott Woodea0da782012-01-12 19:42:58 -0600662 struct nand_chip *nand;
Scott Wood40c6e092008-03-21 16:12:51 -0500663 struct fsl_elbc_mtd *priv;
Kumar Galae58c8392008-12-16 14:59:22 -0600664 uint32_t br = 0, or = 0;
Scott Woodea0da782012-01-12 19:42:58 -0600665 int ret;
Scott Wood40c6e092008-03-21 16:12:51 -0500666
667 if (!elbc_ctrl) {
668 fsl_elbc_ctrl_init();
669 if (!elbc_ctrl)
670 return -1;
671 }
672
673 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
674 if (!priv)
675 return -ENOMEM;
676
677 priv->ctrl = elbc_ctrl;
Scott Woodea0da782012-01-12 19:42:58 -0600678 priv->vbase = addr;
Scott Wood40c6e092008-03-21 16:12:51 -0500679
680 /* Find which chip select it is connected to. It'd be nice
681 * if we could pass more than one datum to the NAND driver...
682 */
683 for (priv->bank = 0; priv->bank < MAX_BANKS; priv->bank++) {
Scott Woodea0da782012-01-12 19:42:58 -0600684 phys_addr_t phys_addr = virt_to_phys(addr);
Kumar Galae58c8392008-12-16 14:59:22 -0600685
Scott Wood40c6e092008-03-21 16:12:51 -0500686 br = in_be32(&elbc_ctrl->regs->bank[priv->bank].br);
687 or = in_be32(&elbc_ctrl->regs->bank[priv->bank].or);
688
689 if ((br & BR_V) && (br & BR_MSEL) == BR_MS_FCM &&
Scott Woodea0da782012-01-12 19:42:58 -0600690 (br & or & BR_BA) == BR_PHYS_ADDR(phys_addr))
Scott Wood40c6e092008-03-21 16:12:51 -0500691 break;
692 }
693
694 if (priv->bank >= MAX_BANKS) {
695 printf("fsl_elbc_nand: address did not match any "
696 "chip selects\n");
Raghav Dogra6c90d982015-05-20 14:54:58 +0530697 kfree(priv);
Scott Wood40c6e092008-03-21 16:12:51 -0500698 return -ENODEV;
699 }
700
Scott Woodea0da782012-01-12 19:42:58 -0600701 nand = &priv->chip;
Scott Wood17fed142016-05-30 13:57:56 -0500702 mtd = nand_to_mtd(nand);
Scott Woodea0da782012-01-12 19:42:58 -0600703
Scott Wood40c6e092008-03-21 16:12:51 -0500704 elbc_ctrl->chips[priv->bank] = priv;
705
706 /* fill in nand_chip structure */
707 /* set up function call table */
708 nand->read_byte = fsl_elbc_read_byte;
709 nand->write_buf = fsl_elbc_write_buf;
710 nand->read_buf = fsl_elbc_read_buf;
Scott Wood40c6e092008-03-21 16:12:51 -0500711 nand->select_chip = fsl_elbc_select_chip;
712 nand->cmdfunc = fsl_elbc_cmdfunc;
713 nand->waitfunc = fsl_elbc_wait;
714
715 /* set up nand options */
Anton Vorontsovd481b072008-06-27 23:04:13 +0400716 nand->bbt_td = &bbt_main_descr;
717 nand->bbt_md = &bbt_mirror_descr;
718
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200719 /* set up nand options */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000720 nand->options = NAND_NO_SUBPAGE_WRITE;
721 nand->bbt_options = NAND_BBT_USE_FLASH;
Scott Wood40c6e092008-03-21 16:12:51 -0500722
723 nand->controller = &elbc_ctrl->controller;
Scott Wood17fed142016-05-30 13:57:56 -0500724 nand_set_controller_data(nand, priv);
Scott Wood40c6e092008-03-21 16:12:51 -0500725
726 nand->ecc.read_page = fsl_elbc_read_page;
727 nand->ecc.write_page = fsl_elbc_write_page;
Scott Wood3ea94ed2015-06-26 19:03:26 -0500728 nand->ecc.write_subpage = fsl_elbc_write_subpage;
Scott Wood40c6e092008-03-21 16:12:51 -0500729
Scott Woodc500d762008-10-29 13:42:41 -0500730 priv->fmr = (15 << FMR_CWTO_SHIFT) | (2 << FMR_AL_SHIFT);
731
Scott Wood40c6e092008-03-21 16:12:51 -0500732 /* If CS Base Register selects full hardware ECC then use it */
733 if ((br & BR_DECC) == BR_DECC_CHK_GEN) {
734 nand->ecc.mode = NAND_ECC_HW;
735
736 nand->ecc.layout = (priv->fmr & FMR_ECCM) ?
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200737 &fsl_elbc_oob_sp_eccm1 :
738 &fsl_elbc_oob_sp_eccm0;
Scott Wood40c6e092008-03-21 16:12:51 -0500739
740 nand->ecc.size = 512;
741 nand->ecc.bytes = 3;
742 nand->ecc.steps = 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000743 nand->ecc.strength = 1;
Scott Wood40c6e092008-03-21 16:12:51 -0500744 } else {
Valentin Longchampcbb65662013-10-18 11:47:22 +0200745 /* otherwise fall back to software ECC */
746#if defined(CONFIG_NAND_ECC_BCH)
747 nand->ecc.mode = NAND_ECC_SOFT_BCH;
748#else
Scott Wood40c6e092008-03-21 16:12:51 -0500749 nand->ecc.mode = NAND_ECC_SOFT;
Valentin Longchampcbb65662013-10-18 11:47:22 +0200750#endif
Scott Wood40c6e092008-03-21 16:12:51 -0500751 }
752
Scott Wood8bd28742013-02-26 13:00:50 +0000753 ret = nand_scan_ident(mtd, 1, NULL);
754 if (ret)
755 return ret;
756
Anton Vorontsov5e0400f2008-06-27 23:04:04 +0400757 /* Large-page-specific setup */
Scott Wood8bd28742013-02-26 13:00:50 +0000758 if (mtd->writesize == 2048) {
759 setbits_be32(&elbc_ctrl->regs->bank[priv->bank].or,
760 OR_FCM_PGS);
761 in_be32(&elbc_ctrl->regs->bank[priv->bank].or);
762
Scott Wood40c6e092008-03-21 16:12:51 -0500763 priv->page_size = 1;
Anton Vorontsov5e0400f2008-06-27 23:04:04 +0400764 nand->badblock_pattern = &largepage_memorybased;
Scott Wood40c6e092008-03-21 16:12:51 -0500765
Scott Wood8bd28742013-02-26 13:00:50 +0000766 /*
767 * Hardware expects small page has ECCM0, large page has
768 * ECCM1 when booting from NAND, and we follow that even
769 * when not booting from NAND.
770 */
771 priv->fmr |= FMR_ECCM;
772
Scott Wood40c6e092008-03-21 16:12:51 -0500773 /* adjust ecc setup if needed */
774 if ((br & BR_DECC) == BR_DECC_CHK_GEN) {
775 nand->ecc.steps = 4;
776 nand->ecc.layout = (priv->fmr & FMR_ECCM) ?
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200777 &fsl_elbc_oob_lp_eccm1 :
778 &fsl_elbc_oob_lp_eccm0;
Scott Wood40c6e092008-03-21 16:12:51 -0500779 }
Scott Wood8bd28742013-02-26 13:00:50 +0000780 } else if (mtd->writesize == 512) {
781 clrbits_be32(&elbc_ctrl->regs->bank[priv->bank].or,
782 OR_FCM_PGS);
783 in_be32(&elbc_ctrl->regs->bank[priv->bank].or);
784 } else {
785 return -ENODEV;
Scott Wood40c6e092008-03-21 16:12:51 -0500786 }
787
Scott Woodea0da782012-01-12 19:42:58 -0600788 ret = nand_scan_tail(mtd);
789 if (ret)
790 return ret;
791
Scott Wood2c1b7e12016-05-30 13:57:55 -0500792 ret = nand_register(devnum, mtd);
Scott Woodea0da782012-01-12 19:42:58 -0600793 if (ret)
794 return ret;
795
Scott Wood40c6e092008-03-21 16:12:51 -0500796 return 0;
797}
Scott Woodea0da782012-01-12 19:42:58 -0600798
799#ifndef CONFIG_SYS_NAND_BASE_LIST
800#define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
801#endif
802
803static unsigned long base_address[CONFIG_SYS_MAX_NAND_DEVICE] =
804 CONFIG_SYS_NAND_BASE_LIST;
805
806void board_nand_init(void)
807{
808 int i;
809
810 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
811 fsl_elbc_chip_init(i, (u8 *)base_address[i]);
812}