blob: f8698ec0158a247dceedf49fa15bfaa7cfbbcb73 [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
Pali Rohár2ef82282022-04-04 18:17:18 +0200315 /* RNDOUT moves the pointer inside the page */
316 case NAND_CMD_RNDOUT:
317 vdbg("fsl_elbc_cmdfunc: NAND_CMD_RNDOUT, column: 0x%x.\n",
318 column);
319
320 ctrl->index = column;
321 return;
322
Scott Wood40c6e092008-03-21 16:12:51 -0500323 /* READOOB reads only the OOB because no ECC is performed. */
324 case NAND_CMD_READOOB:
325 vdbg("fsl_elbc_cmdfunc: NAND_CMD_READOOB, page_addr:"
326 " 0x%x, column: 0x%x.\n", page_addr, column);
327
328 out_be32(&lbc->fbcr, mtd->oobsize - column);
329 set_addr(mtd, column, page_addr, 1);
330
331 ctrl->read_bytes = mtd->writesize + mtd->oobsize;
332
333 fsl_elbc_do_read(chip, 1);
334 fsl_elbc_run_command(mtd);
335
336 return;
337
338 /* READID must read all 5 possible bytes while CEB is active */
339 case NAND_CMD_READID:
Shengzhou Liue9635022011-12-12 17:49:57 +0800340 case NAND_CMD_PARAM:
341 vdbg("fsl_elbc_cmdfunc: NAND_CMD 0x%x.\n", command);
Scott Wood40c6e092008-03-21 16:12:51 -0500342
343 out_be32(&lbc->fir, (FIR_OP_CW0 << FIR_OP0_SHIFT) |
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200344 (FIR_OP_UA << FIR_OP1_SHIFT) |
345 (FIR_OP_RBW << FIR_OP2_SHIFT));
Shengzhou Liue9635022011-12-12 17:49:57 +0800346 out_be32(&lbc->fcr, command << FCR_CMD0_SHIFT);
347 /*
348 * although currently it's 8 bytes for READID, we always read
349 * the maximum 256 bytes(for PARAM)
350 */
351 out_be32(&lbc->fbcr, 256);
352 ctrl->read_bytes = 256;
Scott Wood40c6e092008-03-21 16:12:51 -0500353 ctrl->use_mdr = 1;
Shengzhou Liue9635022011-12-12 17:49:57 +0800354 ctrl->mdr = column;
Scott Wood40c6e092008-03-21 16:12:51 -0500355 set_addr(mtd, 0, 0, 0);
356 fsl_elbc_run_command(mtd);
357 return;
358
359 /* ERASE1 stores the block and page address */
360 case NAND_CMD_ERASE1:
361 vdbg("fsl_elbc_cmdfunc: NAND_CMD_ERASE1, "
362 "page_addr: 0x%x.\n", page_addr);
363 set_addr(mtd, 0, page_addr, 0);
364 return;
365
366 /* ERASE2 uses the block and page address from ERASE1 */
367 case NAND_CMD_ERASE2:
368 vdbg("fsl_elbc_cmdfunc: NAND_CMD_ERASE2.\n");
369
370 out_be32(&lbc->fir,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200371 (FIR_OP_CW0 << FIR_OP0_SHIFT) |
372 (FIR_OP_PA << FIR_OP1_SHIFT) |
373 (FIR_OP_CM1 << FIR_OP2_SHIFT));
Scott Wood40c6e092008-03-21 16:12:51 -0500374
375 out_be32(&lbc->fcr,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200376 (NAND_CMD_ERASE1 << FCR_CMD0_SHIFT) |
377 (NAND_CMD_ERASE2 << FCR_CMD1_SHIFT));
Scott Wood40c6e092008-03-21 16:12:51 -0500378
379 out_be32(&lbc->fbcr, 0);
380 ctrl->read_bytes = 0;
381
382 fsl_elbc_run_command(mtd);
383 return;
384
385 /* SEQIN sets up the addr buffer and all registers except the length */
386 case NAND_CMD_SEQIN: {
387 u32 fcr;
388 vdbg("fsl_elbc_cmdfunc: NAND_CMD_SEQIN/PAGE_PROG, "
389 "page_addr: 0x%x, column: 0x%x.\n",
390 page_addr, column);
391
392 ctrl->column = column;
393 ctrl->oob = 0;
394
395 if (priv->page_size) {
396 fcr = (NAND_CMD_SEQIN << FCR_CMD0_SHIFT) |
397 (NAND_CMD_PAGEPROG << FCR_CMD1_SHIFT);
398
399 out_be32(&lbc->fir,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200400 (FIR_OP_CW0 << FIR_OP0_SHIFT) |
401 (FIR_OP_CA << FIR_OP1_SHIFT) |
402 (FIR_OP_PA << FIR_OP2_SHIFT) |
403 (FIR_OP_WB << FIR_OP3_SHIFT) |
404 (FIR_OP_CW1 << FIR_OP4_SHIFT));
Scott Wood40c6e092008-03-21 16:12:51 -0500405 } else {
406 fcr = (NAND_CMD_PAGEPROG << FCR_CMD1_SHIFT) |
407 (NAND_CMD_SEQIN << FCR_CMD2_SHIFT);
408
409 out_be32(&lbc->fir,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200410 (FIR_OP_CW0 << FIR_OP0_SHIFT) |
411 (FIR_OP_CM2 << FIR_OP1_SHIFT) |
412 (FIR_OP_CA << FIR_OP2_SHIFT) |
413 (FIR_OP_PA << FIR_OP3_SHIFT) |
414 (FIR_OP_WB << FIR_OP4_SHIFT) |
415 (FIR_OP_CW1 << FIR_OP5_SHIFT));
Scott Wood40c6e092008-03-21 16:12:51 -0500416
417 if (column >= mtd->writesize) {
418 /* OOB area --> READOOB */
419 column -= mtd->writesize;
420 fcr |= NAND_CMD_READOOB << FCR_CMD0_SHIFT;
421 ctrl->oob = 1;
422 } else if (column < 256) {
423 /* First 256 bytes --> READ0 */
424 fcr |= NAND_CMD_READ0 << FCR_CMD0_SHIFT;
425 } else {
426 /* Second 256 bytes --> READ1 */
427 fcr |= NAND_CMD_READ1 << FCR_CMD0_SHIFT;
428 }
429 }
430
431 out_be32(&lbc->fcr, fcr);
432 set_addr(mtd, column, page_addr, ctrl->oob);
433 return;
434 }
435
436 /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
437 case NAND_CMD_PAGEPROG: {
Scott Wood40c6e092008-03-21 16:12:51 -0500438 vdbg("fsl_elbc_cmdfunc: NAND_CMD_PAGEPROG "
439 "writing %d bytes.\n", ctrl->index);
440
441 /* if the write did not start at 0 or is not a full page
442 * then set the exact length, otherwise use a full page
443 * write so the HW generates the ECC.
444 */
445 if (ctrl->oob || ctrl->column != 0 ||
mhench05f1b8b2011-07-11 12:29:43 +0000446 ctrl->index != mtd->writesize + mtd->oobsize)
Scott Wood40c6e092008-03-21 16:12:51 -0500447 out_be32(&lbc->fbcr, ctrl->index);
mhench05f1b8b2011-07-11 12:29:43 +0000448 else
Scott Wood40c6e092008-03-21 16:12:51 -0500449 out_be32(&lbc->fbcr, 0);
Scott Wood40c6e092008-03-21 16:12:51 -0500450
451 fsl_elbc_run_command(mtd);
452
Scott Wood40c6e092008-03-21 16:12:51 -0500453 return;
454 }
455
456 /* CMD_STATUS must read the status byte while CEB is active */
457 /* Note - it does not wait for the ready line */
458 case NAND_CMD_STATUS:
459 out_be32(&lbc->fir,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200460 (FIR_OP_CM0 << FIR_OP0_SHIFT) |
461 (FIR_OP_RBW << FIR_OP1_SHIFT));
Scott Wood40c6e092008-03-21 16:12:51 -0500462 out_be32(&lbc->fcr, NAND_CMD_STATUS << FCR_CMD0_SHIFT);
463 out_be32(&lbc->fbcr, 1);
464 set_addr(mtd, 0, 0, 0);
465 ctrl->read_bytes = 1;
466
467 fsl_elbc_run_command(mtd);
468
469 /* The chip always seems to report that it is
470 * write-protected, even when it is not.
471 */
472 out_8(ctrl->addr, in_8(ctrl->addr) | NAND_STATUS_WP);
473 return;
474
475 /* RESET without waiting for the ready line */
476 case NAND_CMD_RESET:
477 dbg("fsl_elbc_cmdfunc: NAND_CMD_RESET.\n");
478 out_be32(&lbc->fir, FIR_OP_CM0 << FIR_OP0_SHIFT);
479 out_be32(&lbc->fcr, NAND_CMD_RESET << FCR_CMD0_SHIFT);
480 fsl_elbc_run_command(mtd);
481 return;
482
483 default:
484 printf("fsl_elbc_cmdfunc: error, unsupported command 0x%x.\n",
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200485 command);
Scott Wood40c6e092008-03-21 16:12:51 -0500486 }
487}
488
489static void fsl_elbc_select_chip(struct mtd_info *mtd, int chip)
490{
491 /* The hardware does not seem to support multiple
492 * chips per bank.
493 */
494}
495
496/*
497 * Write buf to the FCM Controller Data Buffer
498 */
499static void fsl_elbc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
500{
Scott Wood17fed142016-05-30 13:57:56 -0500501 struct nand_chip *chip = mtd_to_nand(mtd);
502 struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
Scott Wood40c6e092008-03-21 16:12:51 -0500503 struct fsl_elbc_ctrl *ctrl = priv->ctrl;
504 unsigned int bufsize = mtd->writesize + mtd->oobsize;
505
Anton Vorontsov42f15632008-03-28 22:10:54 +0300506 if (len <= 0) {
Scott Wood40c6e092008-03-21 16:12:51 -0500507 printf("write_buf of %d bytes", len);
508 ctrl->status = 0;
509 return;
510 }
511
512 if ((unsigned int)len > bufsize - ctrl->index) {
513 printf("write_buf beyond end of buffer "
514 "(%d requested, %u available)\n",
515 len, bufsize - ctrl->index);
516 len = bufsize - ctrl->index;
517 }
518
519 memcpy_toio(&ctrl->addr[ctrl->index], buf, len);
Anton Vorontsov42f15632008-03-28 22:10:54 +0300520 /*
521 * This is workaround for the weird elbc hangs during nand write,
522 * Scott Wood says: "...perhaps difference in how long it takes a
523 * write to make it through the localbus compared to a write to IMMR
524 * is causing problems, and sync isn't helping for some reason."
525 * Reading back the last byte helps though.
526 */
527 in_8(&ctrl->addr[ctrl->index] + len - 1);
528
Scott Wood40c6e092008-03-21 16:12:51 -0500529 ctrl->index += len;
530}
531
532/*
533 * read a byte from either the FCM hardware buffer if it has any data left
534 * otherwise issue a command to read a single byte.
535 */
536static u8 fsl_elbc_read_byte(struct mtd_info *mtd)
537{
Scott Wood17fed142016-05-30 13:57:56 -0500538 struct nand_chip *chip = mtd_to_nand(mtd);
539 struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
Scott Wood40c6e092008-03-21 16:12:51 -0500540 struct fsl_elbc_ctrl *ctrl = priv->ctrl;
541
542 /* If there are still bytes in the FCM, then use the next byte. */
543 if (ctrl->index < ctrl->read_bytes)
544 return in_8(&ctrl->addr[ctrl->index++]);
545
546 printf("read_byte beyond end of buffer\n");
547 return ERR_BYTE;
548}
549
550/*
551 * Read from the FCM Controller Data Buffer
552 */
553static void fsl_elbc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
554{
Scott Wood17fed142016-05-30 13:57:56 -0500555 struct nand_chip *chip = mtd_to_nand(mtd);
556 struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
Scott Wood40c6e092008-03-21 16:12:51 -0500557 struct fsl_elbc_ctrl *ctrl = priv->ctrl;
558 int avail;
559
560 if (len < 0)
561 return;
562
563 avail = min((unsigned int)len, ctrl->read_bytes - ctrl->index);
564 memcpy_fromio(buf, &ctrl->addr[ctrl->index], avail);
565 ctrl->index += avail;
566
567 if (len > avail)
568 printf("read_buf beyond end of buffer "
569 "(%d requested, %d available)\n",
570 len, avail);
571}
572
Scott Wood40c6e092008-03-21 16:12:51 -0500573/* This function is called after Program and Erase Operations to
574 * check for success or failure.
575 */
576static int fsl_elbc_wait(struct mtd_info *mtd, struct nand_chip *chip)
577{
Scott Wood17fed142016-05-30 13:57:56 -0500578 struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
Scott Wood40c6e092008-03-21 16:12:51 -0500579 struct fsl_elbc_ctrl *ctrl = priv->ctrl;
Becky Bruce0d4cee12010-06-17 11:37:20 -0500580 fsl_lbc_t *lbc = ctrl->regs;
Scott Wood40c6e092008-03-21 16:12:51 -0500581
582 if (ctrl->status != LTESR_CC)
583 return NAND_STATUS_FAIL;
584
585 /* Use READ_STATUS command, but wait for the device to be ready */
586 ctrl->use_mdr = 0;
587 out_be32(&lbc->fir,
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200588 (FIR_OP_CW0 << FIR_OP0_SHIFT) |
589 (FIR_OP_RBW << FIR_OP1_SHIFT));
Scott Wood40c6e092008-03-21 16:12:51 -0500590 out_be32(&lbc->fcr, NAND_CMD_STATUS << FCR_CMD0_SHIFT);
591 out_be32(&lbc->fbcr, 1);
592 set_addr(mtd, 0, 0, 0);
593 ctrl->read_bytes = 1;
594
595 fsl_elbc_run_command(mtd);
596
597 if (ctrl->status != LTESR_CC)
598 return NAND_STATUS_FAIL;
599
600 /* The chip always seems to report that it is
601 * write-protected, even when it is not.
602 */
603 out_8(ctrl->addr, in_8(ctrl->addr) | NAND_STATUS_WP);
604 return fsl_elbc_read_byte(mtd);
605}
606
Sergey Lapin3a38a552013-01-14 03:46:50 +0000607static int fsl_elbc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
608 uint8_t *buf, int oob_required, int page)
Scott Wood40c6e092008-03-21 16:12:51 -0500609{
610 fsl_elbc_read_buf(mtd, buf, mtd->writesize);
611 fsl_elbc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
612
613 if (fsl_elbc_wait(mtd, chip) & NAND_STATUS_FAIL)
614 mtd->ecc_stats.failed++;
615
616 return 0;
617}
618
619/* ECC will be calculated automatically, and errors will be detected in
620 * waitfunc.
621 */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000622static int fsl_elbc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -0500623 const uint8_t *buf, int oob_required,
624 int page)
Scott Wood40c6e092008-03-21 16:12:51 -0500625{
Scott Wood40c6e092008-03-21 16:12:51 -0500626 fsl_elbc_write_buf(mtd, buf, mtd->writesize);
627 fsl_elbc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000628
629 return 0;
Scott Wood40c6e092008-03-21 16:12:51 -0500630}
631
632static struct fsl_elbc_ctrl *elbc_ctrl;
633
Scott Wood3ea94ed2015-06-26 19:03:26 -0500634/* ECC will be calculated automatically, and errors will be detected in
635 * waitfunc.
636 */
637static int fsl_elbc_write_subpage(struct mtd_info *mtd, struct nand_chip *chip,
638 uint32_t offset, uint32_t data_len,
Scott Wood46e13102016-05-30 13:57:57 -0500639 const uint8_t *buf, int oob_required, int page)
Scott Wood3ea94ed2015-06-26 19:03:26 -0500640{
641 fsl_elbc_write_buf(mtd, buf, mtd->writesize);
642 fsl_elbc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
643
644 return 0;
645}
646
Scott Wood40c6e092008-03-21 16:12:51 -0500647static void fsl_elbc_ctrl_init(void)
648{
Scott Wood40c6e092008-03-21 16:12:51 -0500649 elbc_ctrl = kzalloc(sizeof(*elbc_ctrl), GFP_KERNEL);
650 if (!elbc_ctrl)
651 return;
652
Becky Bruce0d4cee12010-06-17 11:37:20 -0500653 elbc_ctrl->regs = LBC_BASE_ADDR;
Scott Wood40c6e092008-03-21 16:12:51 -0500654
655 /* clear event registers */
656 out_be32(&elbc_ctrl->regs->ltesr, LTESR_NAND_MASK);
657 out_be32(&elbc_ctrl->regs->lteatr, 0);
658
659 /* Enable interrupts for any detected events */
660 out_be32(&elbc_ctrl->regs->lteir, LTESR_NAND_MASK);
661
662 elbc_ctrl->read_bytes = 0;
663 elbc_ctrl->index = 0;
664 elbc_ctrl->addr = NULL;
665}
666
Scott Woodea0da782012-01-12 19:42:58 -0600667static int fsl_elbc_chip_init(int devnum, u8 *addr)
Scott Wood40c6e092008-03-21 16:12:51 -0500668{
Scott Wood2c1b7e12016-05-30 13:57:55 -0500669 struct mtd_info *mtd;
Scott Woodea0da782012-01-12 19:42:58 -0600670 struct nand_chip *nand;
Scott Wood40c6e092008-03-21 16:12:51 -0500671 struct fsl_elbc_mtd *priv;
Kumar Galae58c8392008-12-16 14:59:22 -0600672 uint32_t br = 0, or = 0;
Scott Woodea0da782012-01-12 19:42:58 -0600673 int ret;
Scott Wood40c6e092008-03-21 16:12:51 -0500674
675 if (!elbc_ctrl) {
676 fsl_elbc_ctrl_init();
677 if (!elbc_ctrl)
678 return -1;
679 }
680
681 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
682 if (!priv)
683 return -ENOMEM;
684
685 priv->ctrl = elbc_ctrl;
Scott Woodea0da782012-01-12 19:42:58 -0600686 priv->vbase = addr;
Scott Wood40c6e092008-03-21 16:12:51 -0500687
688 /* Find which chip select it is connected to. It'd be nice
689 * if we could pass more than one datum to the NAND driver...
690 */
691 for (priv->bank = 0; priv->bank < MAX_BANKS; priv->bank++) {
Scott Woodea0da782012-01-12 19:42:58 -0600692 phys_addr_t phys_addr = virt_to_phys(addr);
Kumar Galae58c8392008-12-16 14:59:22 -0600693
Scott Wood40c6e092008-03-21 16:12:51 -0500694 br = in_be32(&elbc_ctrl->regs->bank[priv->bank].br);
695 or = in_be32(&elbc_ctrl->regs->bank[priv->bank].or);
696
697 if ((br & BR_V) && (br & BR_MSEL) == BR_MS_FCM &&
Scott Woodea0da782012-01-12 19:42:58 -0600698 (br & or & BR_BA) == BR_PHYS_ADDR(phys_addr))
Scott Wood40c6e092008-03-21 16:12:51 -0500699 break;
700 }
701
702 if (priv->bank >= MAX_BANKS) {
703 printf("fsl_elbc_nand: address did not match any "
704 "chip selects\n");
Raghav Dogra6c90d982015-05-20 14:54:58 +0530705 kfree(priv);
Scott Wood40c6e092008-03-21 16:12:51 -0500706 return -ENODEV;
707 }
708
Scott Woodea0da782012-01-12 19:42:58 -0600709 nand = &priv->chip;
Scott Wood17fed142016-05-30 13:57:56 -0500710 mtd = nand_to_mtd(nand);
Scott Woodea0da782012-01-12 19:42:58 -0600711
Scott Wood40c6e092008-03-21 16:12:51 -0500712 elbc_ctrl->chips[priv->bank] = priv;
713
714 /* fill in nand_chip structure */
715 /* set up function call table */
716 nand->read_byte = fsl_elbc_read_byte;
717 nand->write_buf = fsl_elbc_write_buf;
718 nand->read_buf = fsl_elbc_read_buf;
Scott Wood40c6e092008-03-21 16:12:51 -0500719 nand->select_chip = fsl_elbc_select_chip;
720 nand->cmdfunc = fsl_elbc_cmdfunc;
721 nand->waitfunc = fsl_elbc_wait;
722
723 /* set up nand options */
Anton Vorontsovd481b072008-06-27 23:04:13 +0400724 nand->bbt_td = &bbt_main_descr;
725 nand->bbt_md = &bbt_mirror_descr;
726
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200727 /* set up nand options */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000728 nand->options = NAND_NO_SUBPAGE_WRITE;
729 nand->bbt_options = NAND_BBT_USE_FLASH;
Scott Wood40c6e092008-03-21 16:12:51 -0500730
731 nand->controller = &elbc_ctrl->controller;
Scott Wood17fed142016-05-30 13:57:56 -0500732 nand_set_controller_data(nand, priv);
Scott Wood40c6e092008-03-21 16:12:51 -0500733
734 nand->ecc.read_page = fsl_elbc_read_page;
735 nand->ecc.write_page = fsl_elbc_write_page;
Scott Wood3ea94ed2015-06-26 19:03:26 -0500736 nand->ecc.write_subpage = fsl_elbc_write_subpage;
Scott Wood40c6e092008-03-21 16:12:51 -0500737
Scott Woodc500d762008-10-29 13:42:41 -0500738 priv->fmr = (15 << FMR_CWTO_SHIFT) | (2 << FMR_AL_SHIFT);
739
Scott Wood40c6e092008-03-21 16:12:51 -0500740 /* If CS Base Register selects full hardware ECC then use it */
741 if ((br & BR_DECC) == BR_DECC_CHK_GEN) {
742 nand->ecc.mode = NAND_ECC_HW;
743
744 nand->ecc.layout = (priv->fmr & FMR_ECCM) ?
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200745 &fsl_elbc_oob_sp_eccm1 :
746 &fsl_elbc_oob_sp_eccm0;
Scott Wood40c6e092008-03-21 16:12:51 -0500747
748 nand->ecc.size = 512;
749 nand->ecc.bytes = 3;
750 nand->ecc.steps = 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000751 nand->ecc.strength = 1;
Scott Wood40c6e092008-03-21 16:12:51 -0500752 } else {
Valentin Longchampcbb65662013-10-18 11:47:22 +0200753 /* otherwise fall back to software ECC */
754#if defined(CONFIG_NAND_ECC_BCH)
755 nand->ecc.mode = NAND_ECC_SOFT_BCH;
756#else
Scott Wood40c6e092008-03-21 16:12:51 -0500757 nand->ecc.mode = NAND_ECC_SOFT;
Valentin Longchampcbb65662013-10-18 11:47:22 +0200758#endif
Scott Wood40c6e092008-03-21 16:12:51 -0500759 }
760
Scott Wood8bd28742013-02-26 13:00:50 +0000761 ret = nand_scan_ident(mtd, 1, NULL);
762 if (ret)
763 return ret;
764
Anton Vorontsov5e0400f2008-06-27 23:04:04 +0400765 /* Large-page-specific setup */
Scott Wood8bd28742013-02-26 13:00:50 +0000766 if (mtd->writesize == 2048) {
767 setbits_be32(&elbc_ctrl->regs->bank[priv->bank].or,
768 OR_FCM_PGS);
769 in_be32(&elbc_ctrl->regs->bank[priv->bank].or);
770
Scott Wood40c6e092008-03-21 16:12:51 -0500771 priv->page_size = 1;
Anton Vorontsov5e0400f2008-06-27 23:04:04 +0400772 nand->badblock_pattern = &largepage_memorybased;
Scott Wood40c6e092008-03-21 16:12:51 -0500773
Scott Wood8bd28742013-02-26 13:00:50 +0000774 /*
775 * Hardware expects small page has ECCM0, large page has
776 * ECCM1 when booting from NAND, and we follow that even
777 * when not booting from NAND.
778 */
779 priv->fmr |= FMR_ECCM;
780
Scott Wood40c6e092008-03-21 16:12:51 -0500781 /* adjust ecc setup if needed */
782 if ((br & BR_DECC) == BR_DECC_CHK_GEN) {
783 nand->ecc.steps = 4;
784 nand->ecc.layout = (priv->fmr & FMR_ECCM) ?
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200785 &fsl_elbc_oob_lp_eccm1 :
786 &fsl_elbc_oob_lp_eccm0;
Scott Wood40c6e092008-03-21 16:12:51 -0500787 }
Scott Wood8bd28742013-02-26 13:00:50 +0000788 } else if (mtd->writesize == 512) {
789 clrbits_be32(&elbc_ctrl->regs->bank[priv->bank].or,
790 OR_FCM_PGS);
791 in_be32(&elbc_ctrl->regs->bank[priv->bank].or);
792 } else {
793 return -ENODEV;
Scott Wood40c6e092008-03-21 16:12:51 -0500794 }
795
Scott Woodea0da782012-01-12 19:42:58 -0600796 ret = nand_scan_tail(mtd);
797 if (ret)
798 return ret;
799
Scott Wood2c1b7e12016-05-30 13:57:55 -0500800 ret = nand_register(devnum, mtd);
Scott Woodea0da782012-01-12 19:42:58 -0600801 if (ret)
802 return ret;
803
Scott Wood40c6e092008-03-21 16:12:51 -0500804 return 0;
805}
Scott Woodea0da782012-01-12 19:42:58 -0600806
807#ifndef CONFIG_SYS_NAND_BASE_LIST
808#define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
809#endif
810
811static unsigned long base_address[CONFIG_SYS_MAX_NAND_DEVICE] =
812 CONFIG_SYS_NAND_BASE_LIST;
813
814void board_nand_init(void)
815{
816 int i;
817
818 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
819 fsl_elbc_chip_init(i, (u8 *)base_address[i]);
820}