blob: d585b7a65223da75582412de33e652f59f9d0926 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jim Lin5d309e62012-07-29 20:53:29 +00002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
4 * (C) Copyright 2011 NVIDIA Corporation <www.nvidia.com>
5 * (C) Copyright 2006 Detlev Zundel, dzu@denx.de
6 * (C) Copyright 2006 DENX Software Engineering
Jim Lin5d309e62012-07-29 20:53:29 +00007 */
8
9#include <common.h>
10#include <asm/io.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060011#include <memalign.h>
Jim Lin5d309e62012-07-29 20:53:29 +000012#include <nand.h>
Jim Lin5d309e62012-07-29 20:53:29 +000013#include <asm/arch/clock.h>
14#include <asm/arch/funcmux.h>
Tom Warrenab371962012-09-19 15:50:56 -070015#include <asm/arch-tegra/clk_rst.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090016#include <linux/errno.h>
Tom Warrenab371962012-09-19 15:50:56 -070017#include <asm/gpio.h>
Jim Lin5d309e62012-07-29 20:53:29 +000018#include <fdtdec.h>
Marcel Ziswilerd5c69222015-08-06 00:47:06 +020019#include <bouncebuf.h>
Jim Lin5d309e62012-07-29 20:53:29 +000020#include "tegra_nand.h"
21
22DECLARE_GLOBAL_DATA_PTR;
23
24#define NAND_CMD_TIMEOUT_MS 10
25
26#define SKIPPED_SPARE_BYTES 4
27
28/* ECC bytes to be generated for tag data */
29#define TAG_ECC_BYTES 4
30
31/* 64 byte oob block info for large page (== 2KB) device
32 *
33 * OOB flash layout for Tegra with Reed-Solomon 4 symbol correct ECC:
34 * Skipped bytes(4)
35 * Main area Ecc(36)
36 * Tag data(20)
37 * Tag data Ecc(4)
38 *
39 * Yaffs2 will use 16 tag bytes.
40 */
41static struct nand_ecclayout eccoob = {
42 .eccbytes = 36,
43 .eccpos = {
44 4, 5, 6, 7, 8, 9, 10, 11, 12,
45 13, 14, 15, 16, 17, 18, 19, 20, 21,
46 22, 23, 24, 25, 26, 27, 28, 29, 30,
47 31, 32, 33, 34, 35, 36, 37, 38, 39,
48 },
49 .oobavail = 20,
50 .oobfree = {
51 {
52 .offset = 40,
53 .length = 20,
54 },
55 }
56};
57
58enum {
59 ECC_OK,
60 ECC_TAG_ERROR = 1 << 0,
61 ECC_DATA_ERROR = 1 << 1
62};
63
64/* Timing parameters */
65enum {
66 FDT_NAND_MAX_TRP_TREA,
67 FDT_NAND_TWB,
68 FDT_NAND_MAX_TCR_TAR_TRR,
69 FDT_NAND_TWHR,
70 FDT_NAND_MAX_TCS_TCH_TALS_TALH,
71 FDT_NAND_TWH,
72 FDT_NAND_TWP,
73 FDT_NAND_TRH,
74 FDT_NAND_TADL,
75
76 FDT_NAND_TIMING_COUNT
77};
78
79/* Information about an attached NAND chip */
80struct fdt_nand {
81 struct nand_ctlr *reg;
82 int enabled; /* 1 to enable, 0 to disable */
Simon Glass67042a22015-01-05 20:05:36 -070083 struct gpio_desc wp_gpio; /* write-protect GPIO */
Jim Lin5d309e62012-07-29 20:53:29 +000084 s32 width; /* bit width, normally 8 */
85 u32 timing[FDT_NAND_TIMING_COUNT];
86};
87
88struct nand_drv {
89 struct nand_ctlr *reg;
Jim Lin5d309e62012-07-29 20:53:29 +000090 struct fdt_nand config;
91};
92
93static struct nand_drv nand_ctrl;
94static struct mtd_info *our_mtd;
95static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
96
Jim Lin5d309e62012-07-29 20:53:29 +000097/**
98 * Wait for command completion
99 *
100 * @param reg nand_ctlr structure
101 * @return
102 * 1 - Command completed
103 * 0 - Timeout
104 */
105static int nand_waitfor_cmd_completion(struct nand_ctlr *reg)
106{
107 u32 reg_val;
108 int running;
109 int i;
110
111 for (i = 0; i < NAND_CMD_TIMEOUT_MS * 1000; i++) {
112 if ((readl(&reg->command) & CMD_GO) ||
113 !(readl(&reg->status) & STATUS_RBSY0) ||
114 !(readl(&reg->isr) & ISR_IS_CMD_DONE)) {
115 udelay(1);
116 continue;
117 }
118 reg_val = readl(&reg->dma_mst_ctrl);
119 /*
120 * If DMA_MST_CTRL_EN_A_ENABLE or DMA_MST_CTRL_EN_B_ENABLE
121 * is set, that means DMA engine is running.
122 *
123 * Then we have to wait until DMA_MST_CTRL_IS_DMA_DONE
124 * is cleared, indicating DMA transfer completion.
125 */
126 running = reg_val & (DMA_MST_CTRL_EN_A_ENABLE |
127 DMA_MST_CTRL_EN_B_ENABLE);
128 if (!running || (reg_val & DMA_MST_CTRL_IS_DMA_DONE))
129 return 1;
130 udelay(1);
131 }
132 return 0;
133}
134
135/**
136 * Read one byte from the chip
137 *
138 * @param mtd MTD device structure
139 * @return data byte
140 *
141 * Read function for 8bit bus-width
142 */
143static uint8_t read_byte(struct mtd_info *mtd)
144{
Scott Wood17fed142016-05-30 13:57:56 -0500145 struct nand_chip *chip = mtd_to_nand(mtd);
Jim Lin5d309e62012-07-29 20:53:29 +0000146 struct nand_drv *info;
147
Scott Wood17fed142016-05-30 13:57:56 -0500148 info = (struct nand_drv *)nand_get_controller_data(chip);
Jim Lin5d309e62012-07-29 20:53:29 +0000149
Marcel Ziswilerdf13b142015-08-06 00:47:05 +0200150 writel(CMD_GO | CMD_PIO | CMD_RX | CMD_CE0 | CMD_A_VALID,
151 &info->reg->command);
152 if (!nand_waitfor_cmd_completion(info->reg))
153 printf("Command timeout\n");
Jim Lin5d309e62012-07-29 20:53:29 +0000154
Marcel Ziswilerdf13b142015-08-06 00:47:05 +0200155 return (uint8_t)readl(&info->reg->resp);
Jim Lin5d309e62012-07-29 20:53:29 +0000156}
157
158/**
Lucas Stach8a538552012-10-07 11:29:38 +0000159 * Read len bytes from the chip into a buffer
160 *
161 * @param mtd MTD device structure
162 * @param buf buffer to store data to
163 * @param len number of bytes to read
164 *
165 * Read function for 8bit bus-width
166 */
167static void read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
168{
169 int i, s;
170 unsigned int reg;
Scott Wood17fed142016-05-30 13:57:56 -0500171 struct nand_chip *chip = mtd_to_nand(mtd);
172 struct nand_drv *info = (struct nand_drv *)nand_get_controller_data(chip);
Lucas Stach8a538552012-10-07 11:29:38 +0000173
174 for (i = 0; i < len; i += 4) {
175 s = (len - i) > 4 ? 4 : len - i;
176 writel(CMD_PIO | CMD_RX | CMD_A_VALID | CMD_CE0 |
177 ((s - 1) << CMD_TRANS_SIZE_SHIFT) | CMD_GO,
178 &info->reg->command);
179 if (!nand_waitfor_cmd_completion(info->reg))
180 puts("Command timeout during read_buf\n");
181 reg = readl(&info->reg->resp);
182 memcpy(buf + i, &reg, s);
183 }
184}
185
186/**
Jim Lin5d309e62012-07-29 20:53:29 +0000187 * Check NAND status to see if it is ready or not
188 *
189 * @param mtd MTD device structure
190 * @return
191 * 1 - ready
192 * 0 - not ready
193 */
194static int nand_dev_ready(struct mtd_info *mtd)
195{
Scott Wood17fed142016-05-30 13:57:56 -0500196 struct nand_chip *chip = mtd_to_nand(mtd);
Jim Lin5d309e62012-07-29 20:53:29 +0000197 int reg_val;
198 struct nand_drv *info;
199
Scott Wood17fed142016-05-30 13:57:56 -0500200 info = (struct nand_drv *)nand_get_controller_data(chip);
Jim Lin5d309e62012-07-29 20:53:29 +0000201
202 reg_val = readl(&info->reg->status);
203 if (reg_val & STATUS_RBSY0)
204 return 1;
205 else
206 return 0;
207}
208
209/* Dummy implementation: we don't support multiple chips */
210static void nand_select_chip(struct mtd_info *mtd, int chipnr)
211{
212 switch (chipnr) {
213 case -1:
214 case 0:
215 break;
216
217 default:
218 BUG();
219 }
220}
221
222/**
223 * Clear all interrupt status bits
224 *
225 * @param reg nand_ctlr structure
226 */
227static void nand_clear_interrupt_status(struct nand_ctlr *reg)
228{
229 u32 reg_val;
230
231 /* Clear interrupt status */
232 reg_val = readl(&reg->isr);
233 writel(reg_val, &reg->isr);
234}
235
236/**
237 * Send command to NAND device
238 *
239 * @param mtd MTD device structure
240 * @param command the command to be sent
241 * @param column the column address for this command, -1 if none
242 * @param page_addr the page address for this command, -1 if none
243 */
244static void nand_command(struct mtd_info *mtd, unsigned int command,
245 int column, int page_addr)
246{
Scott Wood17fed142016-05-30 13:57:56 -0500247 struct nand_chip *chip = mtd_to_nand(mtd);
Jim Lin5d309e62012-07-29 20:53:29 +0000248 struct nand_drv *info;
249
Scott Wood17fed142016-05-30 13:57:56 -0500250 info = (struct nand_drv *)nand_get_controller_data(chip);
Jim Lin5d309e62012-07-29 20:53:29 +0000251
252 /*
253 * Write out the command to the device.
254 *
255 * Only command NAND_CMD_RESET or NAND_CMD_READID will come
256 * here before mtd->writesize is initialized.
257 */
258
259 /* Emulate NAND_CMD_READOOB */
260 if (command == NAND_CMD_READOOB) {
261 assert(mtd->writesize != 0);
262 column += mtd->writesize;
263 command = NAND_CMD_READ0;
264 }
265
266 /* Adjust columns for 16 bit bus-width */
267 if (column != -1 && (chip->options & NAND_BUSWIDTH_16))
268 column >>= 1;
269
270 nand_clear_interrupt_status(info->reg);
271
272 /* Stop DMA engine, clear DMA completion status */
273 writel(DMA_MST_CTRL_EN_A_DISABLE
274 | DMA_MST_CTRL_EN_B_DISABLE
275 | DMA_MST_CTRL_IS_DMA_DONE,
276 &info->reg->dma_mst_ctrl);
277
278 /*
279 * Program and erase have their own busy handlers
280 * status and sequential in needs no delay
281 */
282 switch (command) {
283 case NAND_CMD_READID:
284 writel(NAND_CMD_READID, &info->reg->cmd_reg1);
Lucas Stach8a538552012-10-07 11:29:38 +0000285 writel(column & 0xFF, &info->reg->addr_reg1);
Marcel Ziswilerdf13b142015-08-06 00:47:05 +0200286 writel(CMD_GO | CMD_CLE | CMD_ALE | CMD_CE0,
287 &info->reg->command);
Jim Lin5d309e62012-07-29 20:53:29 +0000288 break;
Lucas Stach8a538552012-10-07 11:29:38 +0000289 case NAND_CMD_PARAM:
290 writel(NAND_CMD_PARAM, &info->reg->cmd_reg1);
291 writel(column & 0xFF, &info->reg->addr_reg1);
292 writel(CMD_GO | CMD_CLE | CMD_ALE | CMD_CE0,
293 &info->reg->command);
294 break;
Jim Lin5d309e62012-07-29 20:53:29 +0000295 case NAND_CMD_READ0:
296 writel(NAND_CMD_READ0, &info->reg->cmd_reg1);
297 writel(NAND_CMD_READSTART, &info->reg->cmd_reg2);
298 writel((page_addr << 16) | (column & 0xFFFF),
299 &info->reg->addr_reg1);
300 writel(page_addr >> 16, &info->reg->addr_reg2);
301 return;
302 case NAND_CMD_SEQIN:
303 writel(NAND_CMD_SEQIN, &info->reg->cmd_reg1);
304 writel(NAND_CMD_PAGEPROG, &info->reg->cmd_reg2);
305 writel((page_addr << 16) | (column & 0xFFFF),
306 &info->reg->addr_reg1);
307 writel(page_addr >> 16,
308 &info->reg->addr_reg2);
309 return;
310 case NAND_CMD_PAGEPROG:
311 return;
312 case NAND_CMD_ERASE1:
313 writel(NAND_CMD_ERASE1, &info->reg->cmd_reg1);
314 writel(NAND_CMD_ERASE2, &info->reg->cmd_reg2);
315 writel(page_addr, &info->reg->addr_reg1);
316 writel(CMD_GO | CMD_CLE | CMD_ALE |
317 CMD_SEC_CMD | CMD_CE0 | CMD_ALE_BYTES3,
318 &info->reg->command);
319 break;
320 case NAND_CMD_ERASE2:
321 return;
322 case NAND_CMD_STATUS:
323 writel(NAND_CMD_STATUS, &info->reg->cmd_reg1);
324 writel(CMD_GO | CMD_CLE | CMD_PIO | CMD_RX
325 | ((1 - 0) << CMD_TRANS_SIZE_SHIFT)
326 | CMD_CE0,
327 &info->reg->command);
Jim Lin5d309e62012-07-29 20:53:29 +0000328 break;
329 case NAND_CMD_RESET:
330 writel(NAND_CMD_RESET, &info->reg->cmd_reg1);
331 writel(CMD_GO | CMD_CLE | CMD_CE0,
332 &info->reg->command);
333 break;
334 case NAND_CMD_RNDOUT:
335 default:
336 printf("%s: Unsupported command %d\n", __func__, command);
337 return;
338 }
339 if (!nand_waitfor_cmd_completion(info->reg))
340 printf("Command 0x%02X timeout\n", command);
341}
342
343/**
344 * Check whether the pointed buffer are all 0xff (blank).
345 *
346 * @param buf data buffer for blank check
347 * @param len length of the buffer in byte
348 * @return
349 * 1 - blank
350 * 0 - non-blank
351 */
352static int blank_check(u8 *buf, int len)
353{
354 int i;
355
356 for (i = 0; i < len; i++)
357 if (buf[i] != 0xFF)
358 return 0;
359 return 1;
360}
361
362/**
363 * After a DMA transfer for read, we call this function to see whether there
364 * is any uncorrectable error on the pointed data buffer or oob buffer.
365 *
366 * @param reg nand_ctlr structure
367 * @param databuf data buffer
368 * @param a_len data buffer length
369 * @param oobbuf oob buffer
370 * @param b_len oob buffer length
371 * @return
372 * ECC_OK - no ECC error or correctable ECC error
373 * ECC_TAG_ERROR - uncorrectable tag ECC error
374 * ECC_DATA_ERROR - uncorrectable data ECC error
375 * ECC_DATA_ERROR + ECC_TAG_ERROR - uncorrectable data+tag ECC error
376 */
377static int check_ecc_error(struct nand_ctlr *reg, u8 *databuf,
378 int a_len, u8 *oobbuf, int b_len)
379{
380 int return_val = ECC_OK;
381 u32 reg_val;
382
383 if (!(readl(&reg->isr) & ISR_IS_ECC_ERR))
384 return ECC_OK;
385
386 /*
387 * Area A is used for the data block (databuf). Area B is used for
388 * the spare block (oobbuf)
389 */
390 reg_val = readl(&reg->dec_status);
391 if ((reg_val & DEC_STATUS_A_ECC_FAIL) && databuf) {
392 reg_val = readl(&reg->bch_dec_status_buf);
393 /*
394 * If uncorrectable error occurs on data area, then see whether
395 * they are all FF. If all are FF, it's a blank page.
396 * Not error.
397 */
398 if ((reg_val & BCH_DEC_STATUS_FAIL_SEC_FLAG_MASK) &&
399 !blank_check(databuf, a_len))
400 return_val |= ECC_DATA_ERROR;
401 }
402
403 if ((reg_val & DEC_STATUS_B_ECC_FAIL) && oobbuf) {
404 reg_val = readl(&reg->bch_dec_status_buf);
405 /*
406 * If uncorrectable error occurs on tag area, then see whether
407 * they are all FF. If all are FF, it's a blank page.
408 * Not error.
409 */
410 if ((reg_val & BCH_DEC_STATUS_FAIL_TAG_MASK) &&
411 !blank_check(oobbuf, b_len))
412 return_val |= ECC_TAG_ERROR;
413 }
414
415 return return_val;
416}
417
418/**
419 * Set GO bit to send command to device
420 *
421 * @param reg nand_ctlr structure
422 */
423static void start_command(struct nand_ctlr *reg)
424{
425 u32 reg_val;
426
427 reg_val = readl(&reg->command);
428 reg_val |= CMD_GO;
429 writel(reg_val, &reg->command);
430}
431
432/**
433 * Clear command GO bit, DMA GO bit, and DMA completion status
434 *
435 * @param reg nand_ctlr structure
436 */
437static void stop_command(struct nand_ctlr *reg)
438{
439 /* Stop command */
440 writel(0, &reg->command);
441
442 /* Stop DMA engine and clear DMA completion status */
443 writel(DMA_MST_CTRL_GO_DISABLE
444 | DMA_MST_CTRL_IS_DMA_DONE,
445 &reg->dma_mst_ctrl);
446}
447
448/**
449 * Set up NAND bus width and page size
450 *
451 * @param info nand_info structure
452 * @param *reg_val address of reg_val
453 * @return 0 if ok, -1 on error
454 */
455static int set_bus_width_page_size(struct fdt_nand *config,
456 u32 *reg_val)
457{
458 if (config->width == 8)
459 *reg_val = CFG_BUS_WIDTH_8BIT;
460 else if (config->width == 16)
461 *reg_val = CFG_BUS_WIDTH_16BIT;
462 else {
463 debug("%s: Unsupported bus width %d\n", __func__,
464 config->width);
465 return -1;
466 }
467
468 if (our_mtd->writesize == 512)
469 *reg_val |= CFG_PAGE_SIZE_512;
470 else if (our_mtd->writesize == 2048)
471 *reg_val |= CFG_PAGE_SIZE_2048;
472 else if (our_mtd->writesize == 4096)
473 *reg_val |= CFG_PAGE_SIZE_4096;
474 else {
475 debug("%s: Unsupported page size %d\n", __func__,
476 our_mtd->writesize);
477 return -1;
478 }
479
480 return 0;
481}
482
483/**
484 * Page read/write function
485 *
486 * @param mtd mtd info structure
487 * @param chip nand chip info structure
488 * @param buf data buffer
489 * @param page page number
490 * @param with_ecc 1 to enable ECC, 0 to disable ECC
491 * @param is_writing 0 for read, 1 for write
492 * @return 0 when successfully completed
493 * -EIO when command timeout
494 */
495static int nand_rw_page(struct mtd_info *mtd, struct nand_chip *chip,
496 uint8_t *buf, int page, int with_ecc, int is_writing)
497{
498 u32 reg_val;
499 int tag_size;
500 struct nand_oobfree *free = chip->ecc.layout->oobfree;
501 /* 4*128=512 (byte) is the value that our HW can support. */
502 ALLOC_CACHE_ALIGN_BUFFER(u32, tag_buf, 128);
503 char *tag_ptr;
504 struct nand_drv *info;
505 struct fdt_nand *config;
Marcel Ziswilerd5c69222015-08-06 00:47:06 +0200506 unsigned int bbflags;
507 struct bounce_buffer bbstate, bbstate_oob;
Jim Lin5d309e62012-07-29 20:53:29 +0000508
509 if ((uintptr_t)buf & 0x03) {
510 printf("buf %p has to be 4-byte aligned\n", buf);
511 return -EINVAL;
512 }
513
Scott Wood17fed142016-05-30 13:57:56 -0500514 info = (struct nand_drv *)nand_get_controller_data(chip);
Jim Lin5d309e62012-07-29 20:53:29 +0000515 config = &info->config;
516 if (set_bus_width_page_size(config, &reg_val))
517 return -EINVAL;
518
519 /* Need to be 4-byte aligned */
520 tag_ptr = (char *)tag_buf;
521
522 stop_command(info->reg);
523
Marcel Ziswilerd5c69222015-08-06 00:47:06 +0200524 if (is_writing)
525 bbflags = GEN_BB_READ;
526 else
527 bbflags = GEN_BB_WRITE;
528
529 bounce_buffer_start(&bbstate, (void *)buf, 1 << chip->page_shift,
530 bbflags);
Jim Lin5d309e62012-07-29 20:53:29 +0000531 writel((1 << chip->page_shift) - 1, &info->reg->dma_cfg_a);
Marcel Ziswilerd5c69222015-08-06 00:47:06 +0200532 writel(virt_to_phys(bbstate.bounce_buffer), &info->reg->data_block_ptr);
Jim Lin5d309e62012-07-29 20:53:29 +0000533
Marcel Ziswilerd5c69222015-08-06 00:47:06 +0200534 /* Set ECC selection, configure ECC settings */
Jim Lin5d309e62012-07-29 20:53:29 +0000535 if (with_ecc) {
Jim Lin5d309e62012-07-29 20:53:29 +0000536 if (is_writing)
537 memcpy(tag_ptr, chip->oob_poi + free->offset,
Marcel Ziswilerd5c69222015-08-06 00:47:06 +0200538 chip->ecc.layout->oobavail + TAG_ECC_BYTES);
Jim Lin5d309e62012-07-29 20:53:29 +0000539 tag_size = chip->ecc.layout->oobavail + TAG_ECC_BYTES;
540 reg_val |= (CFG_SKIP_SPARE_SEL_4
541 | CFG_SKIP_SPARE_ENABLE
542 | CFG_HW_ECC_CORRECTION_ENABLE
543 | CFG_ECC_EN_TAG_DISABLE
544 | CFG_HW_ECC_SEL_RS
545 | CFG_HW_ECC_ENABLE
546 | CFG_TVAL4
547 | (tag_size - 1));
548
549 if (!is_writing)
550 tag_size += SKIPPED_SPARE_BYTES;
Marcel Ziswilerd5c69222015-08-06 00:47:06 +0200551 bounce_buffer_start(&bbstate_oob, (void *)tag_ptr, tag_size,
552 bbflags);
Jim Lin5d309e62012-07-29 20:53:29 +0000553 } else {
554 tag_size = mtd->oobsize;
555 reg_val |= (CFG_SKIP_SPARE_DISABLE
556 | CFG_HW_ECC_CORRECTION_DISABLE
557 | CFG_ECC_EN_TAG_DISABLE
558 | CFG_HW_ECC_DISABLE
559 | (tag_size - 1));
Marcel Ziswilerd5c69222015-08-06 00:47:06 +0200560 bounce_buffer_start(&bbstate_oob, (void *)chip->oob_poi,
561 tag_size, bbflags);
Jim Lin5d309e62012-07-29 20:53:29 +0000562 }
563 writel(reg_val, &info->reg->config);
Marcel Ziswilerd5c69222015-08-06 00:47:06 +0200564 writel(virt_to_phys(bbstate_oob.bounce_buffer), &info->reg->tag_ptr);
Jim Lin5d309e62012-07-29 20:53:29 +0000565 writel(BCH_CONFIG_BCH_ECC_DISABLE, &info->reg->bch_config);
Jim Lin5d309e62012-07-29 20:53:29 +0000566 writel(tag_size - 1, &info->reg->dma_cfg_b);
567
568 nand_clear_interrupt_status(info->reg);
569
570 reg_val = CMD_CLE | CMD_ALE
571 | CMD_SEC_CMD
572 | (CMD_ALE_BYTES5 << CMD_ALE_BYTE_SIZE_SHIFT)
573 | CMD_A_VALID
574 | CMD_B_VALID
575 | (CMD_TRANS_SIZE_PAGE << CMD_TRANS_SIZE_SHIFT)
576 | CMD_CE0;
577 if (!is_writing)
578 reg_val |= (CMD_AFT_DAT_DISABLE | CMD_RX);
579 else
580 reg_val |= (CMD_AFT_DAT_ENABLE | CMD_TX);
581 writel(reg_val, &info->reg->command);
582
583 /* Setup DMA engine */
584 reg_val = DMA_MST_CTRL_GO_ENABLE
585 | DMA_MST_CTRL_BURST_8WORDS
586 | DMA_MST_CTRL_EN_A_ENABLE
587 | DMA_MST_CTRL_EN_B_ENABLE;
588
589 if (!is_writing)
590 reg_val |= DMA_MST_CTRL_DIR_READ;
591 else
592 reg_val |= DMA_MST_CTRL_DIR_WRITE;
593
594 writel(reg_val, &info->reg->dma_mst_ctrl);
595
596 start_command(info->reg);
597
598 if (!nand_waitfor_cmd_completion(info->reg)) {
599 if (!is_writing)
600 printf("Read Page 0x%X timeout ", page);
601 else
602 printf("Write Page 0x%X timeout ", page);
603 if (with_ecc)
604 printf("with ECC");
605 else
606 printf("without ECC");
607 printf("\n");
608 return -EIO;
609 }
610
Marcel Ziswilerd5c69222015-08-06 00:47:06 +0200611 bounce_buffer_stop(&bbstate_oob);
612 bounce_buffer_stop(&bbstate);
613
Jim Lin5d309e62012-07-29 20:53:29 +0000614 if (with_ecc && !is_writing) {
615 memcpy(chip->oob_poi, tag_ptr,
616 SKIPPED_SPARE_BYTES);
617 memcpy(chip->oob_poi + free->offset,
618 tag_ptr + SKIPPED_SPARE_BYTES,
619 chip->ecc.layout->oobavail);
620 reg_val = (u32)check_ecc_error(info->reg, (u8 *)buf,
621 1 << chip->page_shift,
622 (u8 *)(tag_ptr + SKIPPED_SPARE_BYTES),
623 chip->ecc.layout->oobavail);
624 if (reg_val & ECC_TAG_ERROR)
625 printf("Read Page 0x%X tag ECC error\n", page);
626 if (reg_val & ECC_DATA_ERROR)
627 printf("Read Page 0x%X data ECC error\n",
628 page);
629 if (reg_val & (ECC_DATA_ERROR | ECC_TAG_ERROR))
630 return -EIO;
631 }
632 return 0;
633}
634
635/**
636 * Hardware ecc based page read function
637 *
638 * @param mtd mtd info structure
639 * @param chip nand chip info structure
640 * @param buf buffer to store read data
641 * @param page page number to read
642 * @return 0 when successfully completed
643 * -EIO when command timeout
644 */
645static int nand_read_page_hwecc(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000646 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Jim Lin5d309e62012-07-29 20:53:29 +0000647{
648 return nand_rw_page(mtd, chip, buf, page, 1, 0);
649}
650
651/**
652 * Hardware ecc based page write function
653 *
654 * @param mtd mtd info structure
655 * @param chip nand chip info structure
656 * @param buf data buffer
657 */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000658static int nand_write_page_hwecc(struct mtd_info *mtd,
Scott Wood46e13102016-05-30 13:57:57 -0500659 struct nand_chip *chip, const uint8_t *buf, int oob_required,
660 int page)
Jim Lin5d309e62012-07-29 20:53:29 +0000661{
Jim Lin5d309e62012-07-29 20:53:29 +0000662 nand_rw_page(mtd, chip, (uint8_t *)buf, page, 1, 1);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000663 return 0;
Jim Lin5d309e62012-07-29 20:53:29 +0000664}
665
666
667/**
668 * Read raw page data without ecc
669 *
670 * @param mtd mtd info structure
671 * @param chip nand chip info structure
672 * @param buf buffer to store read data
673 * @param page page number to read
674 * @return 0 when successfully completed
675 * -EINVAL when chip->oob_poi is not double-word aligned
676 * -EIO when command timeout
677 */
678static int nand_read_page_raw(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000679 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Jim Lin5d309e62012-07-29 20:53:29 +0000680{
681 return nand_rw_page(mtd, chip, buf, page, 0, 0);
682}
683
684/**
685 * Raw page write function
686 *
687 * @param mtd mtd info structure
688 * @param chip nand chip info structure
689 * @param buf data buffer
690 */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000691static int nand_write_page_raw(struct mtd_info *mtd,
Scott Wood46e13102016-05-30 13:57:57 -0500692 struct nand_chip *chip, const uint8_t *buf,
693 int oob_required, int page)
Jim Lin5d309e62012-07-29 20:53:29 +0000694{
Jim Lin5d309e62012-07-29 20:53:29 +0000695 nand_rw_page(mtd, chip, (uint8_t *)buf, page, 0, 1);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000696 return 0;
Jim Lin5d309e62012-07-29 20:53:29 +0000697}
698
699/**
700 * OOB data read/write function
701 *
702 * @param mtd mtd info structure
703 * @param chip nand chip info structure
704 * @param page page number to read
705 * @param with_ecc 1 to enable ECC, 0 to disable ECC
706 * @param is_writing 0 for read, 1 for write
707 * @return 0 when successfully completed
708 * -EINVAL when chip->oob_poi is not double-word aligned
709 * -EIO when command timeout
710 */
711static int nand_rw_oob(struct mtd_info *mtd, struct nand_chip *chip,
712 int page, int with_ecc, int is_writing)
713{
714 u32 reg_val;
715 int tag_size;
716 struct nand_oobfree *free = chip->ecc.layout->oobfree;
717 struct nand_drv *info;
Marcel Ziswilerd5c69222015-08-06 00:47:06 +0200718 unsigned int bbflags;
719 struct bounce_buffer bbstate_oob;
Jim Lin5d309e62012-07-29 20:53:29 +0000720
721 if (((int)chip->oob_poi) & 0x03)
722 return -EINVAL;
Scott Wood17fed142016-05-30 13:57:56 -0500723 info = (struct nand_drv *)nand_get_controller_data(chip);
Jim Lin5d309e62012-07-29 20:53:29 +0000724 if (set_bus_width_page_size(&info->config, &reg_val))
725 return -EINVAL;
726
727 stop_command(info->reg);
728
Jim Lin5d309e62012-07-29 20:53:29 +0000729 /* Set ECC selection */
730 tag_size = mtd->oobsize;
731 if (with_ecc)
732 reg_val |= CFG_ECC_EN_TAG_ENABLE;
733 else
734 reg_val |= (CFG_ECC_EN_TAG_DISABLE);
735
736 reg_val |= ((tag_size - 1) |
737 CFG_SKIP_SPARE_DISABLE |
738 CFG_HW_ECC_CORRECTION_DISABLE |
739 CFG_HW_ECC_DISABLE);
740 writel(reg_val, &info->reg->config);
741
Jim Lin5d309e62012-07-29 20:53:29 +0000742 if (is_writing && with_ecc)
743 tag_size -= TAG_ECC_BYTES;
744
Marcel Ziswilerd5c69222015-08-06 00:47:06 +0200745 if (is_writing)
746 bbflags = GEN_BB_READ;
747 else
748 bbflags = GEN_BB_WRITE;
749
750 bounce_buffer_start(&bbstate_oob, (void *)chip->oob_poi, tag_size,
751 bbflags);
752 writel(virt_to_phys(bbstate_oob.bounce_buffer), &info->reg->tag_ptr);
753
754 writel(BCH_CONFIG_BCH_ECC_DISABLE, &info->reg->bch_config);
755
Jim Lin5d309e62012-07-29 20:53:29 +0000756 writel(tag_size - 1, &info->reg->dma_cfg_b);
757
758 nand_clear_interrupt_status(info->reg);
759
760 reg_val = CMD_CLE | CMD_ALE
761 | CMD_SEC_CMD
762 | (CMD_ALE_BYTES5 << CMD_ALE_BYTE_SIZE_SHIFT)
763 | CMD_B_VALID
764 | CMD_CE0;
765 if (!is_writing)
766 reg_val |= (CMD_AFT_DAT_DISABLE | CMD_RX);
767 else
768 reg_val |= (CMD_AFT_DAT_ENABLE | CMD_TX);
769 writel(reg_val, &info->reg->command);
770
771 /* Setup DMA engine */
772 reg_val = DMA_MST_CTRL_GO_ENABLE
773 | DMA_MST_CTRL_BURST_8WORDS
774 | DMA_MST_CTRL_EN_B_ENABLE;
775 if (!is_writing)
776 reg_val |= DMA_MST_CTRL_DIR_READ;
777 else
778 reg_val |= DMA_MST_CTRL_DIR_WRITE;
779
780 writel(reg_val, &info->reg->dma_mst_ctrl);
781
782 start_command(info->reg);
783
784 if (!nand_waitfor_cmd_completion(info->reg)) {
785 if (!is_writing)
786 printf("Read OOB of Page 0x%X timeout\n", page);
787 else
788 printf("Write OOB of Page 0x%X timeout\n", page);
789 return -EIO;
790 }
791
Marcel Ziswilerd5c69222015-08-06 00:47:06 +0200792 bounce_buffer_stop(&bbstate_oob);
793
Jim Lin5d309e62012-07-29 20:53:29 +0000794 if (with_ecc && !is_writing) {
795 reg_val = (u32)check_ecc_error(info->reg, 0, 0,
796 (u8 *)(chip->oob_poi + free->offset),
797 chip->ecc.layout->oobavail);
798 if (reg_val & ECC_TAG_ERROR)
799 printf("Read OOB of Page 0x%X tag ECC error\n", page);
800 }
801 return 0;
802}
803
804/**
805 * OOB data read function
806 *
807 * @param mtd mtd info structure
808 * @param chip nand chip info structure
809 * @param page page number to read
Jim Lin5d309e62012-07-29 20:53:29 +0000810 */
811static int nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000812 int page)
Jim Lin5d309e62012-07-29 20:53:29 +0000813{
Sergey Lapin3a38a552013-01-14 03:46:50 +0000814 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Jim Lin5d309e62012-07-29 20:53:29 +0000815 nand_rw_oob(mtd, chip, page, 0, 0);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000816 return 0;
Jim Lin5d309e62012-07-29 20:53:29 +0000817}
818
819/**
820 * OOB data write function
821 *
822 * @param mtd mtd info structure
823 * @param chip nand chip info structure
824 * @param page page number to write
825 * @return 0 when successfully completed
826 * -EINVAL when chip->oob_poi is not double-word aligned
827 * -EIO when command timeout
828 */
829static int nand_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
830 int page)
831{
832 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
833
834 return nand_rw_oob(mtd, chip, page, 0, 1);
835}
836
837/**
838 * Set up NAND memory timings according to the provided parameters
839 *
840 * @param timing Timing parameters
841 * @param reg NAND controller register address
842 */
843static void setup_timing(unsigned timing[FDT_NAND_TIMING_COUNT],
844 struct nand_ctlr *reg)
845{
846 u32 reg_val, clk_rate, clk_period, time_val;
847
848 clk_rate = (u32)clock_get_periph_rate(PERIPH_ID_NDFLASH,
849 CLOCK_ID_PERIPH) / 1000000;
850 clk_period = 1000 / clk_rate;
851 reg_val = ((timing[FDT_NAND_MAX_TRP_TREA] / clk_period) <<
852 TIMING_TRP_RESP_CNT_SHIFT) & TIMING_TRP_RESP_CNT_MASK;
853 reg_val |= ((timing[FDT_NAND_TWB] / clk_period) <<
854 TIMING_TWB_CNT_SHIFT) & TIMING_TWB_CNT_MASK;
855 time_val = timing[FDT_NAND_MAX_TCR_TAR_TRR] / clk_period;
856 if (time_val > 2)
857 reg_val |= ((time_val - 2) << TIMING_TCR_TAR_TRR_CNT_SHIFT) &
858 TIMING_TCR_TAR_TRR_CNT_MASK;
859 reg_val |= ((timing[FDT_NAND_TWHR] / clk_period) <<
860 TIMING_TWHR_CNT_SHIFT) & TIMING_TWHR_CNT_MASK;
861 time_val = timing[FDT_NAND_MAX_TCS_TCH_TALS_TALH] / clk_period;
862 if (time_val > 1)
863 reg_val |= ((time_val - 1) << TIMING_TCS_CNT_SHIFT) &
864 TIMING_TCS_CNT_MASK;
865 reg_val |= ((timing[FDT_NAND_TWH] / clk_period) <<
866 TIMING_TWH_CNT_SHIFT) & TIMING_TWH_CNT_MASK;
867 reg_val |= ((timing[FDT_NAND_TWP] / clk_period) <<
868 TIMING_TWP_CNT_SHIFT) & TIMING_TWP_CNT_MASK;
869 reg_val |= ((timing[FDT_NAND_TRH] / clk_period) <<
870 TIMING_TRH_CNT_SHIFT) & TIMING_TRH_CNT_MASK;
871 reg_val |= ((timing[FDT_NAND_MAX_TRP_TREA] / clk_period) <<
872 TIMING_TRP_CNT_SHIFT) & TIMING_TRP_CNT_MASK;
873 writel(reg_val, &reg->timing);
874
875 reg_val = 0;
876 time_val = timing[FDT_NAND_TADL] / clk_period;
877 if (time_val > 2)
878 reg_val = (time_val - 2) & TIMING2_TADL_CNT_MASK;
879 writel(reg_val, &reg->timing2);
880}
881
882/**
883 * Decode NAND parameters from the device tree
884 *
885 * @param blob Device tree blob
Robert P. J. Day8d56db92016-07-15 13:44:45 -0400886 * @param node Node containing "nand-flash" compatible node
Jim Lin5d309e62012-07-29 20:53:29 +0000887 * @return 0 if ok, -ve on error (FDT_ERR_...)
888 */
889static int fdt_decode_nand(const void *blob, int node, struct fdt_nand *config)
890{
891 int err;
892
893 config->reg = (struct nand_ctlr *)fdtdec_get_addr(blob, node, "reg");
894 config->enabled = fdtdec_get_is_enabled(blob, node);
895 config->width = fdtdec_get_int(blob, node, "nvidia,nand-width", 8);
Simon Glass1d9af1f2017-05-30 21:47:09 -0600896 err = gpio_request_by_name_nodev(offset_to_ofnode(node),
897 "nvidia,wp-gpios", 0, &config->wp_gpio, GPIOD_IS_OUT);
Jim Lin5d309e62012-07-29 20:53:29 +0000898 if (err)
899 return err;
900 err = fdtdec_get_int_array(blob, node, "nvidia,timing",
901 config->timing, FDT_NAND_TIMING_COUNT);
902 if (err < 0)
903 return err;
904
905 /* Now look up the controller and decode that */
906 node = fdt_next_node(blob, node, NULL);
907 if (node < 0)
908 return node;
909
910 return 0;
911}
912
913/**
914 * Board-specific NAND initialization
915 *
916 * @param nand nand chip info structure
917 * @return 0, after initialized, -1 on error
918 */
919int tegra_nand_init(struct nand_chip *nand, int devnum)
920{
921 struct nand_drv *info = &nand_ctrl;
922 struct fdt_nand *config = &info->config;
923 int node, ret;
924
925 node = fdtdec_next_compatible(gd->fdt_blob, 0,
926 COMPAT_NVIDIA_TEGRA20_NAND);
927 if (node < 0)
928 return -1;
929 if (fdt_decode_nand(gd->fdt_blob, node, config)) {
930 printf("Could not decode nand-flash in device tree\n");
931 return -1;
932 }
933 if (!config->enabled)
934 return -1;
935 info->reg = config->reg;
936 nand->ecc.mode = NAND_ECC_HW;
937 nand->ecc.layout = &eccoob;
938
939 nand->options = LP_OPTIONS;
940 nand->cmdfunc = nand_command;
941 nand->read_byte = read_byte;
Lucas Stach8a538552012-10-07 11:29:38 +0000942 nand->read_buf = read_buf;
Jim Lin5d309e62012-07-29 20:53:29 +0000943 nand->ecc.read_page = nand_read_page_hwecc;
944 nand->ecc.write_page = nand_write_page_hwecc;
945 nand->ecc.read_page_raw = nand_read_page_raw;
946 nand->ecc.write_page_raw = nand_write_page_raw;
947 nand->ecc.read_oob = nand_read_oob;
948 nand->ecc.write_oob = nand_write_oob;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000949 nand->ecc.strength = 1;
Jim Lin5d309e62012-07-29 20:53:29 +0000950 nand->select_chip = nand_select_chip;
951 nand->dev_ready = nand_dev_ready;
Scott Wood17fed142016-05-30 13:57:56 -0500952 nand_set_controller_data(nand, &nand_ctrl);
Jim Lin5d309e62012-07-29 20:53:29 +0000953
Marcel Ziswilercdbf2082015-08-06 00:47:13 +0200954 /* Disable subpage writes as we do not provide ecc->hwctl */
955 nand->options |= NAND_NO_SUBPAGE_WRITE;
956
Jim Lin5d309e62012-07-29 20:53:29 +0000957 /* Adjust controller clock rate */
958 clock_start_periph_pll(PERIPH_ID_NDFLASH, CLOCK_ID_PERIPH, 52000000);
959
960 /* Adjust timing for NAND device */
961 setup_timing(config->timing, info->reg);
962
Simon Glass67042a22015-01-05 20:05:36 -0700963 dm_gpio_set_value(&config->wp_gpio, 1);
Jim Lin5d309e62012-07-29 20:53:29 +0000964
Scott Wood17fed142016-05-30 13:57:56 -0500965 our_mtd = nand_to_mtd(nand);
Jim Lin5d309e62012-07-29 20:53:29 +0000966 ret = nand_scan_ident(our_mtd, CONFIG_SYS_NAND_MAX_CHIPS, NULL);
967 if (ret)
968 return ret;
969
970 nand->ecc.size = our_mtd->writesize;
971 nand->ecc.bytes = our_mtd->oobsize;
972
973 ret = nand_scan_tail(our_mtd);
974 if (ret)
975 return ret;
976
Scott Wood2c1b7e12016-05-30 13:57:55 -0500977 ret = nand_register(devnum, our_mtd);
Jim Lin5d309e62012-07-29 20:53:29 +0000978 if (ret)
979 return ret;
980
981 return 0;
982}
983
984void board_nand_init(void)
985{
986 struct nand_chip *nand = &nand_chip[0];
987
988 if (tegra_nand_init(nand, 0))
989 puts("Tegra NAND init failed\n");
990}