blob: c3b851b2da5aa689f340a92e878d4688e75f9649 [file] [log] [blame]
xf.libfc6e712025-02-07 01:54:34 -08001/*
2 * NAND Flash Controller Device Driver
3 * Copyright © 2009-2010, Intel Corporation and its suppliers.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 */
19#include <malloc.h>
20#include <asm/io.h>
21#include <linux/mtd/mtd.h>
22#include <asm-generic/ioctl.h>
23#include <config.h>
24#include <common.h>
25#include <command.h>
26#include <asm/arch/nand.h>
27#include <linux/mtd/nand.h>
28#include "denali.h"
29#include <asm/arch/lsp_crpm.h>
30#include <boot_mode.h>
31
32
33/* DEBUG */
34#if DENALI_DEBUG
35#define denali_debug(fmt,args...) printf (fmt ,##args)
36#define denali_debugX(level,fmt,args...) if (DEBUG>=level) printf(fmt,##args);
37#else
38#define denali_debug(fmt,args...)
39#define denali_debugX(level,fmt,args...)
40#endif /* DEBUG */
41
42
43
44/* We define a module parameter that allows the user to override
45 * the hardware and decide what timing mode should be used.
46 */
47#define NAND_DEFAULT_TIMINGS -1
48
49//static int onfi_timing_mode = NAND_DEFAULT_TIMINGS;
50
51/* We define a macro here that combines all interrupts this driver uses into
52 * a single constant value, for convenience. */
53#define DENALI_IRQ_ALL (INTR_STATUS__DMA_CMD_COMP | \
54 INTR_STATUS__ECC_ERR | \
55 INTR_STATUS__PROGRAM_FAIL | \
56 INTR_STATUS__LOAD_COMP | \
57 INTR_STATUS__PROGRAM_COMP | \
58 INTR_STATUS__TIME_OUT | \
59 INTR_STATUS__ERASE_FAIL | \
60 INTR_STATUS__RST_COMP | \
61 INTR_STATUS__ERASE_COMP)
62
63/* indicates whether or not the internal value for the flash bank is
64 * valid or not */
65#define CHIP_SELECT_INVALID -1
66
67#define SUPPORT_8BITECC 1
68
69/* This macro divides two integers and rounds fractional values up
70 * to the nearest integer value. */
71#define CEIL_DIV(X, Y) (((X)%(Y)) ? ((X)/(Y)+1) : ((X)/(Y)))
72
73/* this macro allows us to convert from an MTD structure to our own
74 * device context (denali) structure.
75 */
76//#define mtd_to_denali(m) container_of(m, struct denali_nand_info, mtd)
77
78/* These constants are defined by the driver to enable common driver
79 * configuration options. */
80#define SPARE_ACCESS 0x41
81#define MAIN_ACCESS 0x42
82#define MAIN_SPARE_ACCESS 0x43
83
84#define DENALI_READ 0
85#define DENALI_WRITE 0x100
86
87/* types of device accesses. We can issue commands and get status */
88#define COMMAND_CYCLE 0
89#define ADDR_CYCLE 1
90#define STATUS_CYCLE 2
91
92/* this is a helper macro that allows us to
93 * format the bank into the proper bits for the controller */
94#define BANK(x) ((x) << 24)
95
96#define true 1
97#define false 0
98
99extern struct nand_flash_device_para nand_flash_para[];
100extern struct mtd_info nand_info[];
101extern struct nand_chip nand_chip[];
102extern int flash_dmabuf_disable_flag;
103
104
105struct denali_nand_info denali_info = {0};
106struct denali_nand_info *g_denali = &denali_info;
107struct nand_flash_device_para *g_nand_dev_info = NULL;
108
109/* forward declarations */
110//static void clear_interrupts(struct denali_nand_info *denali);
111//static void denali_irq_enable(struct denali_nand_info *denali,
112// uint32_t int_mask);
113//static uint32_t read_interrupt_status(struct denali_nand_info *denali);
114
115/* Certain operations for the denali NAND controller use
116 * an indexed mode to read/write data. The operation is
117 * performed by writing the address value of the command
118 * to the device memory followed by the data. This function
119 * abstracts this common operation.
120*/
121static void index_addr(struct denali_nand_info *denali,
122 uint32_t address, uint32_t data)
123{
124 writel(address, denali->flash_mem);
125 writel(data, denali->flash_mem + 0x10);
126}
127
128/* Perform an indexed read of the device */
129static void index_addr_read_data(struct denali_nand_info *denali,
130 uint32_t address, uint32_t *pdata)
131{
132 writel(address, denali->flash_mem);
133 *pdata = readl(denali->flash_mem + 0x10);
134}
135
136/* We need to buffer some data for some of the NAND core routines.
137 * The operations manage buffering that data. */
138static void reset_buf(struct denali_nand_info *denali)
139{
140 denali->buf.head = denali->buf.tail = 0;
141}
142
143static void write_byte_to_buf(struct denali_nand_info *denali, uint8_t byte)
144{
145 BUG_ON(denali->buf.tail >= sizeof(denali->buf.buf));
146 denali->buf.buf[denali->buf.tail++] = byte;
147}
148
149/* reads the status of the device */
150static void read_status(struct denali_nand_info *denali)
151{
152 uint32_t status, addr;
153 addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
154 index_addr(denali, (uint32_t)addr | 0, 0x70);
155 index_addr_read_data(denali,(uint32_t)addr | 2, &status);
156 write_byte_to_buf(denali, status);
157}
158
159/* resets a specific device connected to the core */
160static void reset_bank(struct denali_nand_info *denali)
161{
162// uint32_t status_type = 0;
163// uint32_t status_mask = INTR_STATUS__RST_COMP |
164// INTR_STATUS__TIME_OUT;
165
166// clear_interrupts(denali);
167
168 writel(1 << denali->flash_bank, denali->flash_reg + DEVICE_RESET);
169
170 while (!(readl(denali->flash_reg +
171 INTR_STATUS(denali->flash_bank)) &
172 (INTR_STATUS__RST_COMP | INTR_STATUS__TIME_OUT)))
173 {
174
175 if (readl(denali->flash_reg + INTR_STATUS(denali->flash_bank)) &
176 INTR_STATUS__TIME_OUT)
177 {
178 debug("NAND Reset operation timed out on bank %d\n", denali->flash_bank);
179 }
180 }
181}
182
183/* Reset the flash controller */
184static uint16_t denali_nand_reset(struct denali_nand_info *denali)
185{
186 uint32_t i;
187
188 for (i = 0 ; i < denali->max_banks; i++)
189 writel(INTR_STATUS__RST_COMP | INTR_STATUS__TIME_OUT,
190 denali->flash_reg + INTR_STATUS(i));
191
192 for (i = 0 ; i < denali->max_banks; i++)
193 {
194 writel(1 << i, denali->flash_reg + DEVICE_RESET);
195 while (!(readl(denali->flash_reg +
196 INTR_STATUS(i)) &
197 (INTR_STATUS__RST_COMP | INTR_STATUS__TIME_OUT))); //zhouqi fpga 4.27
198
199 if (readl(denali->flash_reg + INTR_STATUS(i)) &
200 INTR_STATUS__TIME_OUT)
201 debug("NAND Reset operation timed out on bank %d\n", i);
202 }
203
204 for (i = 0; i < denali->max_banks; i++)
205 writel(INTR_STATUS__RST_COMP | INTR_STATUS__TIME_OUT,
206 denali->flash_reg + INTR_STATUS(i));
207
208 return PASS;
209}
210
211
212/* determines how many NAND chips are connected to the controller. Note for
213 * Intel CE4100 devices we don't support more than one device.
214 */
215 #if 0
216static void find_valid_banks(struct denali_nand_info *denali)
217{
218 uint32_t id[denali->max_banks];
219 int i;
220
221 denali->total_used_banks = 1;
222 for (i = 0; i < denali->max_banks; i++)
223 {
224 index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 0), 0x90);
225 index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 1), 0);
226 index_addr_read_data(denali,
227 (uint32_t)(MODE_11 | (i << 24) | 2), &id[i]);
228
229 debug("Return 1st ID for bank[%d]: %x\n", i, id[i]);
230
231 if (i == 0)
232 {
233 if (!(id[i] & 0x0ff))
234 break; /* WTF? */
235 }
236 else
237 {
238 if ((id[i] & 0x0ff) == (id[0] & 0x0ff))
239 denali->total_used_banks++;
240 else
241 break;
242 }
243 }
244 debug( "denali->total_used_banks: %d\n", denali->total_used_banks);
245}
246#endif
247/*
248 * Use the configuration feature register to determine the maximum number of
249 * banks that the hardware supports.
250 */
251static void detect_max_banks(struct denali_nand_info *denali)
252{
253 //uint32_t features = readl(denali->flash_reg + FEATURES);
254
255 //denali->max_banks = 2 << (features & FEATURES__N_BANKS);
256 denali->max_banks = 1; //zhouqi for fpge 4.27 and for evb 7.19
257}
258
259
260static uint32_t detect_nand_bus_freq(void)
261{
262 uint32_t clk_reg = 0;
263
264 clk_reg = readl(0x01306050);
265 clk_reg &= 0xffffcfff; /*MOD_CLK_SEL[13:12]=00,7520v2 NAND 104MHz*/
266 writel(clk_reg, 0x01306050);
267 if((((readl(0x01306050))>>12) & 0x3) == 0)
268 return 104;
269 else
270 return 26;
271}
272
273#if 0
274static void detect_partition_feature(struct denali_nand_info *denali)
275{
276 /* For MRST platform, denali->fwblks represent the
277 * number of blocks firmware is taken,
278 * FW is in protect partition and MTD driver has no
279 * permission to access it. So let driver know how many
280 * blocks it can't touch.
281 * */
282 if (readl(denali->flash_reg + FEATURES) & FEATURES__PARTITION) {
283 if ((readl(denali->flash_reg + PERM_SRC_ID(1)) &
284 PERM_SRC_ID__SRCID) == SPECTRA_PARTITION_ID) {
285 denali->fwblks =
286 ((readl(denali->flash_reg + MIN_MAX_BANK(1)) &
287 MIN_MAX_BANK__MIN_VALUE) *
288 denali->blksperchip)
289 +
290 (readl(denali->flash_reg + MIN_BLK_ADDR(1)) &
291 MIN_BLK_ADDR__VALUE);
292 } else
293 denali->fwblks = SPECTRA_START_BLOCK;
294 } else
295 denali->fwblks = SPECTRA_START_BLOCK;
296}
297#endif
298
299static void denali_nand_register_set(struct denali_nand_info *denali,
300 struct nand_flash_device_para * table)
301{
302 writel(table->pages_per_block, denali->flash_reg + PAGES_PER_BLOCK);
303 writel(table->bus_num, denali->flash_reg + DEVICE_WIDTH);
304 writel(table->page_size, denali->flash_reg + DEVICE_MAIN_AREA_SIZE);
305 writel(table->oob_size, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
306
307 if(table->row_addr_num == 2)
308 writel(1, denali->flash_reg + TWO_ROW_ADDR_CYCLES);
309 else
310 writel(0, denali->flash_reg + TWO_ROW_ADDR_CYCLES);
311
312 writel(table->page_size, denali->flash_reg + LOGICAL_PAGE_DATA_SIZE);
313 writel(table->oob_size, denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
314 writel(1, denali->flash_reg + ECC_ENABLE);
315
316
317}
318
319
320static void denali_nand_timing_set(struct denali_nand_info *denali,
321 struct nand_flash_device_para * table)
322{
323 uint32_t bus_freq;
324 struct nand_flash_timing * timing = NULL;
325
326 timing = &(table->nand_timeing);
327 bus_freq = detect_nand_bus_freq();
328 writel(((timing->Twhr * bus_freq)/1000+2) | (((timing->Trr1 * bus_freq)/1000+2)<<8),
329 denali->flash_reg + WE_2_RE);
330 writel(((timing->Tadl * bus_freq)/1000+2) | (((timing->Trr2 * bus_freq)/1000+2)<<8),
331 denali->flash_reg + ADDR_2_DATA);
332 writel((timing->Trhw * bus_freq)/1000+2, denali->flash_reg + RE_2_WE);
333 writel((timing->Trp * bus_freq)/1000+2, denali->flash_reg + RDWR_EN_LO_CNT);
334 writel((timing->Treh * bus_freq)/1000+2, denali->flash_reg + RDWR_EN_HI_CNT);
335 writel((timing->Tcs * bus_freq)/1000+2, denali->flash_reg + CS_SETUP_CNT);
336 writel((timing->Trhz * bus_freq)/1000+2, denali->flash_reg + RE_2_RE);
337}
338
339static void denali_set_intr_modes(struct denali_nand_info *denali,
340 uint16_t INT_ENABLE)
341{
342 if (INT_ENABLE)
343 writel(1, denali->flash_reg + GLOBAL_INT_ENABLE);
344 else
345 writel(0, denali->flash_reg + GLOBAL_INT_ENABLE);
346}
347
348/* validation function to verify that the controlling software is making
349 * a valid request
350 */
351static inline uint32_t is_flash_bank_valid(int flash_bank)
352{
353 return (flash_bank >= 0 && flash_bank < 4);
354}
355
356static void denali_irq_init(struct denali_nand_info *denali)
357{
358 uint32_t int_mask = 0;
359 int i;
360
361 /* Disable global interrupts */
362 denali_set_intr_modes(denali, false);
363
364 int_mask = DENALI_IRQ_ALL;
365
366 /* Clear all status bits */
367 for (i = 0; i < denali->max_banks; ++i)
368 writel(0xFFFF, denali->flash_reg + INTR_STATUS(i));
369}
370
371#define BANK(x) ((x) << 24)
372
373static uint32_t wait_for_ready(struct denali_nand_info *denali, uint32_t status_type)
374{
375 uint32_t status = 0;
376
377 while (!(readl(denali->flash_reg +
378 INTR_STATUS(denali->flash_bank)) & status_type));
379 status = readl(denali->flash_reg + INTR_STATUS(denali->flash_bank));
380
381 #if 0
382 while (!(readl(0x1207410) & status_type));
383 status = readl(0x1207410);
384 #endif
385
386 if (status & INTR_STATUS__ECC_ERR )
387 {
388 printf (" Deanli Nand Failed: ECC Error\n");
389 }
390 if (status & INTR_STATUS__TIME_OUT)
391 {
392 printf (" Deanli Nand Failed: Time out\n");
393 }
394 if (status & INTR_STATUS__PROGRAM_FAIL)
395 {
396 printf (" Deanli Nand Failed: Program Fail\n");
397 }
398
399 writew(0xffff, denali->flash_reg + INTR_STATUS(denali->flash_bank));
400 return status;
401}
402
403/* This helper function setups the registers for ECC and whether or not
404 * the spare area will be transferred. */
405static void setup_ecc_for_xfer(struct denali_nand_info *denali, uint32_t ecc_en,uint32_t transfer_spare)
406{
407 int ecc_en_flag = 0, transfer_spare_flag = 0;
408 #if ECC_TEST_VER
409 ecc_en_flag = 0;
410 #else
411 /* set ECC, transfer spare bits if needed */
412 ecc_en_flag = ecc_en ? ECC_ENABLE__FLAG : 0;
413 #endif
414 transfer_spare_flag = transfer_spare ? TRANSFER_SPARE_REG__FLAG : 0;
415
416 /* Enable spare area/ECC per user's request. */
417 writel(ecc_en_flag, denali->flash_reg + ECC_ENABLE);
418 writel(transfer_spare_flag,
419 denali->flash_reg + TRANSFER_SPARE_REG);
420}
421
422
423static void setup_ecc_the_end(struct denali_nand_info *denali, uint32_t ecc_en)
424{
425 int ecc_en_flag = 0;
426
427 /* set ECC, transfer spare bits if needed */
428 ecc_en_flag = ecc_en ? ECC_ENABLE__FLAG : 0;
429
430 /* Enable spare area/ECC per user's request. */
431 writel(ecc_en_flag, denali->flash_reg + ECC_ENABLE);
432}
433
434
435/* sends a pipeline command operation to the controller. See the Denali NAND
436 * controller's user guide for more information (section 4.2.3.6).
437 */
438static int denali_send_pipeline_cmd(struct denali_nand_info *denali,
439 uint32_t ecc_en,
440 uint32_t transfer_spare,
441 int access_type,
442 int op)
443{
444 int status = PASS;
445 uint32_t addr = 0x0, cmd = 0x0, page_count = 1, status_type = 0,
446 status_mask = 0;
447
448 if (op == DENALI_READ)
449 status_mask = INTR_STATUS__LOAD_COMP;
450 else if (op == DENALI_WRITE)
451 status_mask = 0;
452 else
453 BUG();
454
455 setup_ecc_for_xfer(denali, ecc_en, transfer_spare);
456
457 addr = BANK(denali->flash_bank) | denali->page;
458
459 if (op == DENALI_WRITE && access_type != SPARE_ACCESS)
460 {
461 cmd = MODE_01 | addr;
462 writel(cmd, denali->flash_mem);
463 }
464 else if (op == DENALI_WRITE && access_type == SPARE_ACCESS)
465 {
466 /* read spare area */
467 cmd = MODE_10 | addr;
468 index_addr(denali, (uint32_t)cmd, access_type);
469
470 cmd = MODE_01 | addr;
471 writel(cmd, denali->flash_mem);
472 }
473 else if (op == DENALI_READ)
474 {
475 /* setup page read request for access type */
476 cmd = MODE_10 | addr;
477 index_addr(denali, (uint32_t)cmd, access_type);
478
479 /* page 33 of the NAND controller spec indicates we should not
480 use the pipeline commands in Spare area only mode. So we
481 don't.
482 */
483 if (access_type == SPARE_ACCESS)
484 {
485 cmd = MODE_01 | addr;
486 writel(cmd, denali->flash_mem);
487 }
488 else
489 {
490 index_addr(denali, (uint32_t)cmd,
491 0x2000 | op | page_count);
492
493 /* wait for command to be accepted
494 * can always use status0 bit as the
495 * mask is identical for each
496 * bank. */
497 status_type = wait_for_ready(denali, status_mask);
498
499 if (status_type == 0)
500 {
501 debug("cmd, page, addr on timeout "
502 "(0x%x, 0x%x, 0x%x)\n",
503 cmd, denali->page, addr);
504 status = FAIL;
505 }
506 else
507 {
508 cmd = MODE_01 | addr;
509 writel(cmd, denali->flash_mem);
510 }
511 }
512 }
513 return status;
514}
515
516/* helper function that simply writes a buffer to the flash */
517static int write_data_to_flash_mem(struct denali_nand_info *denali,
518 const uint8_t *buf,
519 int len)
520{
521 uint32_t i = 0, *buf32;
522
523 /* verify that the len is a multiple of 4. see comment in
524 * read_data_from_flash_mem() */
525 BUG_ON((len % 4) != 0);
526
527 /* write the data to the flash memory */
528 buf32 = (uint32_t *)buf;
529 for (i = 0; i < len / 4; i++)
530 writel(*buf32++, denali->flash_mem + 0x10);
531 return i*4; /* intent is to return the number of bytes read */
532}
533
534/* helper function that simply reads a buffer from the flash */
535static int read_data_from_flash_mem(struct denali_nand_info *denali,
536 uint8_t *buf,
537 int len)
538{
539 uint32_t i = 0, *buf32;
540
541 /* we assume that len will be a multiple of 4, if not
542 * it would be nice to know about it ASAP rather than
543 * have random failures...
544 * This assumption is based on the fact that this
545 * function is designed to be used to read flash pages,
546 * which are typically multiples of 4...
547 */
548
549 BUG_ON((len % 4) != 0);
550
551 /* transfer the data from the flash */
552 buf32 = (uint32_t *)buf;
553 for (i = 0; i < len / 4; i++)
554 *buf32++ = readl(denali->flash_mem + 0x10);
555 return i*4; /* intent is to return the number of bytes read */
556}
557
558/* writes OOB data to the device */
559static int write_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
560{
561 struct denali_nand_info *denali = g_denali;
562 uint32_t status_type = 0;
563 uint32_t status_mask = INTR_STATUS__PROGRAM_COMP |
564 INTR_STATUS__PROGRAM_FAIL;
565 int status = 0, addr = 0x0, cmd = 0x0;
566
567 denali->page = page;
568
569 /* Modified by zhouqi for xxx, 2013/09/03 */
570 if (denali_send_pipeline_cmd(denali, false, true, SPARE_ACCESS,
571 DENALI_WRITE) == PASS)
572 {
573 write_data_to_flash_mem(denali, buf, mtd->oobsize);
574
575 /* wait for operation to complete */
576 status_type = wait_for_ready(denali, status_mask);
577
578 if (status_type & INTR_STATUS__PROGRAM_FAIL) //zhouqi
579 {
580 debug("OOB write failed\n");
581 status = -1;
582 }
583 }
584 else
585 {
586 debug("unable to send pipeline command\n");
587 status = -1;
588 }
589
590 /* Added by zhouqi for xxx, 2013/09/03 */
591 /* We set the device back to MAIN_ACCESS here as I observed
592 * instability with the controller if you do a block erase
593 * and the last transaction was a SPARE_ACCESS. Block erase
594 * is reliable (according to the MTD test infrastructure)
595 * if you are in MAIN_ACCESS.
596 */
597 addr = BANK(denali->flash_bank) | denali->page;
598 cmd = MODE_10 | addr;
599 index_addr(denali, (uint32_t)cmd, MAIN_ACCESS);
600 /* End added. zhouqi, 2013/09/03 */
601
602 return status;
603}
604
605/* reads OOB data from the device */
606static void read_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
607{
608 struct denali_nand_info *denali = g_denali;
609 uint32_t status_mask = INTR_STATUS__LOAD_COMP | INTR_STATUS__TIME_OUT, //zhouqi
610 status_type = 0, addr = 0x0, cmd = 0x0;
611
612 denali->page = page;
613
614 if (denali_send_pipeline_cmd(denali, false, true, SPARE_ACCESS,
615 DENALI_READ) == PASS)
616 {
617 read_data_from_flash_mem(denali, buf, mtd->oobsize);
618
619 /* wait for command to be accepted
620 * can always use status0 bit as the mask is identical for each
621 * bank. */
622 status_type = wait_for_ready(denali, status_mask);
623
624 if (status_type & INTR_STATUS__TIME_OUT)
625 debug("page on OOB timeout %d\n",denali->page);
626
627 /* We set the device back to MAIN_ACCESS here as I observed
628 * instability with the controller if you do a block erase
629 * and the last transaction was a SPARE_ACCESS. Block erase
630 * is reliable (according to the MTD test infrastructure)
631 * if you are in MAIN_ACCESS.
632 */
633 addr = BANK(denali->flash_bank) | denali->page;
634 cmd = MODE_10 | addr;
635 index_addr(denali, (uint32_t)cmd, MAIN_ACCESS);
636 }
637}
638
639/* this function examines buffers to see if they contain data that
640 * indicate that the buffer is part of an erased region of flash.
641 */
642uint32_t is_erased(uint8_t *buf, int len)
643{
644 int i = 0;
645 for (i = 0; i < len; i++)
646 if (buf[i] != 0xFF)
647 return false;
648 return true;
649}
650#define ECC_SECTOR_SIZE 512
651
652#define ECC_SECTOR(x) (((x) & ECC_ERROR_ADDRESS__SECTOR_NR) >> 12)
653#define ECC_BYTE(x) (((x) & ECC_ERROR_ADDRESS__OFFSET))
654#define ECC_CORRECTION_VALUE(x) ((x) & ERR_CORRECTION_INFO__BYTEMASK)
655#define ECC_ERROR_CORRECTABLE(x) (!((x) & ERR_CORRECTION_INFO__ERROR_TYPE))
656#define ECC_ERR_DEVICE(x) (((x) & ERR_CORRECTION_INFO__DEVICE_NR) >> 8)
657#define ECC_LAST_ERR(x) ((x) & ERR_CORRECTION_INFO__LAST_ERR_INFO)
658
659static uint32_t handle_ecc(struct denali_nand_info *denali, uint8_t *buf,
660 uint32_t status_type)
661{
662 int check_erased_page = 0;
663 uint32_t err_correction_value = 0;
664 uint32_t err_correction_info = 0;
665
666#if 1
667 /*zx297520 use*/
668 if (status_type & INTR_STATUS__ECC_ERR)
669 {
670 check_erased_page = 1;
671
672 }
673 else
674 {
675
676 switch(denali->flash_bank)
677 {
678 case 0:
679 err_correction_info = readl(denali->flash_reg +ERR_CORRECTION_INFO_B01);
680 err_correction_value = err_correction_info & ERR_CORRECTION_INFO_B01__MAX_ERRORS_B0;
681 break;
682
683 case 1:
684 err_correction_info = readl(denali->flash_reg +ERR_CORRECTION_INFO_B01);
685 err_correction_value = (err_correction_info & ERR_CORRECTION_INFO_B01__MAX_ERRORS_B1)>>8;
686 break;
687
688 case 2:
689 err_correction_info = readl(denali->flash_reg +ERR_CORRECTION_INFO_B23);
690 err_correction_value = err_correction_info & ERR_CORRECTION_INFO_B01__MAX_ERRORS_B2;
691 break;
692
693 case 3:
694 err_correction_info = readl(denali->flash_reg +ERR_CORRECTION_INFO_B23);
695 err_correction_value = (err_correction_info & ERR_CORRECTION_INFO_B01__MAX_ERRORS_B3)>>8;
696 break;
697
698 default:
699 break;
700
701 }
702 if(err_correction_value)
703 printk("correct %d bit errors on page %x.\n",err_correction_value,denali->page);
704
705
706 }
707 return check_erased_page;
708#else
709 /* read the ECC errors. we'll ignore them for now */
710 uint32_t err_address = 0, err_correction_info = 0;
711 uint32_t err_byte = 0, err_sector = 0, err_device = 0;
712 uint32_t err_correction_value = 0;
713 denali_set_intr_modes(denali, false);
714
715 do
716 {
717 err_address = readl(denali->flash_reg +
718 ECC_ERROR_ADDRESS);
719 err_sector = ECC_SECTOR(err_address);
720 err_byte = ECC_BYTE(err_address);
721
722 err_correction_info = readl(denali->flash_reg +
723 ERR_CORRECTION_INFO);
724 err_correction_value =
725 ECC_CORRECTION_VALUE(err_correction_info);
726 err_device = ECC_ERR_DEVICE(err_correction_info);
727
728 if (ECC_ERROR_CORRECTABLE(err_correction_info))
729 {
730 /* If err_byte is larger than ECC_SECTOR_SIZE,
731 * means error happened in OOB, so we ignore
732 * it. It's no need for us to correct it
733 * err_device is represented the NAND error
734 * bits are happened in if there are more
735 * than one NAND connected.
736 * */
737 if (err_byte < ECC_SECTOR_SIZE)
738 {
739 int offset;
740 offset = (err_sector *
741 ECC_SECTOR_SIZE +
742 err_byte) *
743 denali->devnum +
744 err_device;
745 /* correct the ECC error */
746 buf[offset] ^= err_correction_value;
747 denali->mtd->ecc_stats.corrected++;
748 }
749 } else {
750 /* if the error is not correctable, need to
751 * look at the page to see if it is an erased
752 * page. if so, then it's not a real ECC error
753 * */
754 check_erased_page = true;
755 }
756 } while (!ECC_LAST_ERR(err_correction_info));
757 /* Once handle all ecc errors, controller will triger
758 * a ECC_TRANSACTION_DONE interrupt, so here just wait
759 * for a while for this interrupt
760 * */
761 while (!(read_interrupt_status(denali) &
762 INTR_STATUS__ECC_ERR))
763 clear_interrupts(denali);
764 denali_set_intr_modes(denali, false);
765 #endif
766 }
767
768
769
770
771/* programs the controller to either enable/disable DMA transfers */
772static void denali_enable_dma(struct denali_nand_info *denali, uint32_t en)
773{
774 uint32_t reg_val = 0x0;
775
776 if (en)
777 reg_val = DMA_ENABLE__FLAG;
778
779 writel(reg_val, denali->flash_reg + DMA_ENABLE);
780 readl(denali->flash_reg + DMA_ENABLE);
781}
782
783/* setups the HW to perform the data DMA */
784static void denali_setup_dma(struct denali_nand_info *denali, int op)
785
786{
787 uint32_t mode = 0x0;
788 const int page_count = 1;
789 dma_addr_t addr = denali->buf.dma_buf;
790
791 mode = MODE_10 | BANK(denali->flash_bank);
792
793 /* DMA is a four step process */
794
795 /* 1. setup transfer type and # of pages */
796 index_addr(denali, mode | denali->page, 0x2000 | op | page_count);
797
798 /* 2. set memory high address bits 23:8 */
799 index_addr(denali, mode | ((uint16_t)(addr >> 16) << 8), 0x2200);
800
801 /* 3. set memory low address bits 23:8 */
802 index_addr(denali, mode | ((uint16_t)addr << 8), 0x2300);
803
804 /* 4. interrupt when complete, burst len = 64 bytes*/
805 //writel(NAND_BASE, MODE_10|0x10000|(4 << 8));//BurstLength =4
806 index_addr(denali, mode | 0x14000, 0x2400); //zhouqi not interrupt 0X40
807}
808static void denali_setup_dma_buffer(struct denali_nand_info *denali, int op,char* buffer)
809
810{
811 uint32_t mode = 0x0;
812 const int page_count = 1;
813 dma_addr_t addr = buffer;
814
815 mode = MODE_10 | BANK(denali->flash_bank);
816
817 /* DMA is a four step process */
818
819 /* 1. setup transfer type and # of pages */
820 index_addr(denali, mode | denali->page, 0x2000 | op | page_count);
821
822 /* 2. set memory high address bits 23:8 */
823 index_addr(denali, mode | ((uint16_t)(addr >> 16) << 8), 0x2200);
824
825 /* 3. set memory low address bits 23:8 */
826 index_addr(denali, mode | ((uint16_t)addr << 8), 0x2300);
827
828 /* 4. interrupt when complete, burst len = 64 bytes*/
829 //writel(NAND_BASE, MODE_10|0x10000|(4 << 8));//BurstLength =4
830 index_addr(denali, mode | 0x14000, 0x2400); //zhouqi not interrupt 0X40
831}
832
833
834
835/* add by zhouqi */
836#if 0
837static void denali_setup_dma_derect(struct denali_nand_info *denali, int op,
838 dma_addr_t addr) /* add by zhouqi */
839{
840 uint32_t mode = 0x0;
841 const int page_count = 1;
842// dma_addr_t addr = denali->buf.dma_buf;
843
844 mode = MODE_10 | BANK(denali->flash_bank);
845
846 /* DMA is a four step process */
847
848 /* 1. setup transfer type and # of pages */
849 index_addr(denali, mode | denali->page, 0x2000 | op | page_count);
850
851 /* 2. set memory high address bits 23:8 */
852 index_addr(denali, mode | ((uint16_t)(addr >> 16) << 8), 0x2200);
853
854 /* 3. set memory low address bits 23:8 */
855 index_addr(denali, mode | ((uint16_t)addr << 8), 0x2300);
856
857 /* 4. interrupt when complete, burst len = 64 bytes*/
858 //writel(NAND_BASE, MODE_10|0x10000|(4 << 8));//BurstLength =4
859 index_addr(denali, mode | 0x14000, 0x2400); //zhouqi not interrupt 0X40
860}
861#endif
862
863/* writes a page. user specifies type, and this function handles the
864 * configuration details. */
865static void write_page(struct mtd_info *mtd, struct nand_chip *chip,
866 const uint8_t *buf, uint32_t raw_xfer)
867{
868 struct denali_nand_info *denali = g_denali;
869
870 uint32_t status_type = 0;
871 uint32_t status_mask = INTR_STATUS__DMA_CMD_COMP |
872 INTR_STATUS__PROGRAM_FAIL;
873
874 /* if it is a raw xfer, we want to disable ecc, and send
875 * the spare area.
876 * !raw_xfer - enable ecc
877 * raw_xfer - transfer spare
878 */
879 setup_ecc_for_xfer(denali, !raw_xfer, raw_xfer);
880
881 /* copy buffer into DMA buffer */
882 memcpy((void*)denali->buf.dma_buf, (void*)buf, mtd->writesize);
883
884 if (raw_xfer)
885 {
886 /* transfer the data to the spare area */
887 memcpy((void*)(denali->buf.dma_buf + mtd->writesize),
888 (void*)chip->oob_poi,mtd->oobsize);
889 }
890
891 denali_enable_dma(denali, true);
892
893 denali_setup_dma(denali, DENALI_WRITE); //zhouqi p779-p789
894
895 /* wait for operation to complete */
896 status_type = wait_for_ready(denali, status_mask);
897
898 if (status_type & INTR_STATUS__TIME_OUT)
899 {
900 debug("timeout on write_page (type = %d)\n",
901 raw_xfer);
902 denali->status =
903 (status_type & INTR_STATUS__PROGRAM_FAIL) ?
904 NAND_STATUS_FAIL : PASS;
905 }
906
907 denali_enable_dma(denali, false);
908
909 setup_ecc_the_end(denali, false); //zhouqi
910
911}
912
913//add by zhouqi
914static void write_page_ops(struct mtd_info *mtd, struct nand_chip *chip,
915 const uint8_t *buf)
916{
917 int N,len,sector_size,ecc_bytes,i;
918 struct denali_nand_info *denali = g_denali;
919
920 uint32_t status_type = 0;
921 uint32_t status_mask = INTR_STATUS__DMA_CMD_COMP |
922 INTR_STATUS__PROGRAM_FAIL;
923
924 /* if it is a raw xfer, we want to disable ecc, and send
925 * the spare area.
926 * !raw_xfer - enable ecc
927 * raw_xfer - transfer spare
928 */
929 setup_ecc_for_xfer(denali, 1, 1);
930 memset((void *)(denali->buf.dma_buf),0xff,denali->mtd->writesize+denali->mtd->oobsize);
931
932 sector_size = denali->nand->ecc.size;
933 ecc_bytes = denali->nand->ecc.bytes;
934 N = denali->mtd->writesize/(sector_size+ecc_bytes) + 1;
935 len = sector_size;
936
937 for(i=0;i < N;i++)
938 {
939 if(i==N-1)
940 {
941 len = denali->mtd->writesize - (sector_size+ecc_bytes)*i;
942 }
943
944 memcpy((void *)(denali->buf.dma_buf+(sector_size+ecc_bytes)*i), (void *)(buf+sector_size*i), len); ;
945
946 }
947
948 len = sector_size - len;
949
950 memcpy((void *)(denali->buf.dma_buf + denali->mtd->writesize+2), (void *)(buf + sector_size*i -len), len);
951 memcpy((void *)(denali->buf.dma_buf+denali->mtd->writesize+2+len+ecc_bytes), (void *)(chip->oob_poi + 2+len+ecc_bytes), \
952 denali->mtd->oobsize-2-len-ecc_bytes);
953
954 denali_enable_dma(denali, true);
955
956 denali_setup_dma(denali, DENALI_WRITE);
957
958 /* wait for operation to complete */
959 status_type = wait_for_ready(denali, status_mask);
960
961 if (status_type & INTR_STATUS__TIME_OUT)
962 {
963 debug("timeout on write_page (type = )\n");
964 denali->status =
965 (status_type & INTR_STATUS__PROGRAM_FAIL) ?
966 NAND_STATUS_FAIL : PASS;
967 }
968
969 denali_enable_dma(denali, false);
970 setup_ecc_the_end(denali, false); //zhouqi
971}
972
973/* NAND core entry points */
974
975/* this is the callback that the NAND core calls to write a page. Since
976 * writing a page with ECC or without is similar, all the work is done
977 * by write_page above.
978 * */
979static void denali_write_page(struct mtd_info *mtd, struct nand_chip *chip,
980 const uint8_t *buf, struct mtd_oob_ops *ops)//zhouqi add ops
981{
982 struct denali_nand_info *denali = g_denali;
983 /* for regular page writes, we let HW handle all the ECC
984 * data written to the device. */
985 int ecc_bits = readl(denali->flash_reg + ECC_CORRECTION);
986 if((ops->oobbuf != NULL) && (ops->ooblen != 0))
987 {
988 write_page_ops(mtd, chip, buf);
989 }
990 else
991 {
992 if(denali->page < 64)
993 {
994 writel(0x8, denali->flash_reg + ECC_CORRECTION);
995 }
996 write_page(mtd, chip, buf, false);
997 writel(ecc_bits, denali->flash_reg + ECC_CORRECTION);
998 }
999 update_led_twinkle();
1000}
1001
1002/* This is the callback that the NAND core calls to write a page without ECC.
1003 * raw access is similar to ECC page writes, so all the work is done in the
1004 * write_page() function above.
1005 */
1006static void denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1007 const uint8_t *buf)
1008{
1009 /* for raw page writes, we want to disable ECC and simply write
1010 whatever data is in the buffer. */
1011 write_page(mtd, chip, buf, true);
1012}
1013
1014static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
1015 int page)
1016{
1017 //printk(KERN_EMERG "[denali.c] denali_write_oob: page = 0x%0x\n",page); //zhouqi
1018 return write_oob_data(mtd, chip->oob_poi, page);
1019}
1020
1021static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1022 int page, int sndcmd)
1023{
1024 //printk(KERN_EMERG "[denali.c] denali_read_oob: page = 0x%0x\n",page); //zhouqi
1025 read_oob_data(mtd, chip->oob_poi, page);
1026
1027 return 0; /* notify NAND core to send command to
1028 NAND device. */
1029}
1030
1031
1032static int read_page(struct mtd_info *mtd, struct nand_chip *chip,
1033 uint8_t *buf, int page)
1034{
1035 struct denali_nand_info *denali = g_denali;
1036
1037 uint32_t status_type = 0;
1038// uint32_t status_mask = INTR_STATUS__ECC_ERR |
1039// INTR_STATUS__ECC_ERR;
1040 uint32_t status_mask = INTR_STATUS__DMA_CMD_COMP;
1041 uint32_t check_erased_page = false;
1042
1043 if (page != denali->page)
1044 {
1045 debug("IN %s: page %d is not"
1046 " equal to denali->page %d, investigate!!",
1047 __func__, page, denali->page);
1048 BUG();
1049 }
1050
1051 setup_ecc_for_xfer(denali, true, false);
1052
1053 denali_enable_dma(denali, true);
1054
1055 if(flash_dmabuf_disable_flag == 1)
1056 {
1057 denali_setup_dma(denali, DENALI_READ);
1058 /* wait for operation to complete */
1059 status_type = wait_for_ready(denali, status_mask);
1060 memcpy(buf, (void *)denali->buf.dma_buf, mtd->writesize); //zhouqi -p939
1061 }
1062 else
1063 {
1064 denali_setup_dma_buffer(denali, DENALI_READ,buf); //zhouqi
1065 /* wait for operation to complete */
1066 status_type = wait_for_ready(denali, status_mask);
1067 }
1068
1069 check_erased_page = handle_ecc(denali, buf, status_type);
1070 //check_erased_page = 0;
1071 denali_enable_dma(denali, false);
1072 setup_ecc_the_end(denali, false); //zhouqi
1073
1074 if (check_erased_page)
1075 {
1076 read_oob_data(denali->mtd, chip->oob_poi, denali->page);
1077
1078 if (!is_erased(buf, denali->mtd->writesize))
1079 denali->mtd->ecc_stats.failed++;
1080 if (!is_erased(chip->oob_poi, denali->mtd->oobsize))
1081 denali->mtd->ecc_stats.failed++;
1082 }
1083 return 0;
1084}
1085
1086
1087//add by zhouqi
1088static int read_page_ops(struct mtd_info *mtd, struct nand_chip *chip,
1089 uint8_t *buf, int page)
1090{
1091 int N,len,sector_size,ecc_bytes,i;
1092 struct denali_nand_info *denali = g_denali;
1093
1094 uint32_t status_type = 0;
1095 uint32_t status_mask = INTR_STATUS__DMA_CMD_COMP;
1096 uint32_t check_erased_page = false;
1097
1098 if (page != denali->page)
1099 {
1100 debug("IN %s: page %d is not"
1101 " equal to denali->page %d, investigate!!",
1102 __func__, page, denali->page);
1103 BUG();
1104 }
1105
1106 setup_ecc_for_xfer(denali, 1, 1);
1107
1108 denali_enable_dma(denali, true);
1109
1110 denali_setup_dma(denali, DENALI_READ);
1111
1112 /* wait for operation to complete */
1113 status_type = wait_for_ready(denali, status_mask);
1114
1115 sector_size = denali->nand->ecc.size;
1116 ecc_bytes = denali->nand->ecc.bytes;
1117 N = denali->mtd->writesize/(sector_size+ecc_bytes) + 1;
1118 len = sector_size;
1119
1120 for(i=0;i < N;i++)
1121 {
1122 if(i==N-1)
1123 {
1124 len = denali->mtd->writesize - (sector_size+ecc_bytes)*i;
1125 }
1126
1127 memcpy((void *)(buf+sector_size*i),(void *)( denali->buf.dma_buf + (sector_size+ecc_bytes)*i),len);
1128
1129 }
1130
1131 len = sector_size - len;
1132 memcpy((void *)(buf + sector_size*(N-1)+len), (void *)(denali->buf.dma_buf + denali->mtd->writesize +2), len);
1133
1134 memset((void *)(chip->oob_poi), 0xFF, len +ecc_bytes+2);
1135 memcpy((void *)(chip->oob_poi + len+ecc_bytes+2), (void *)(denali->buf.dma_buf + denali->mtd->writesize+len+ecc_bytes+2),\
1136 denali->mtd->oobsize-len -ecc_bytes-2);
1137
1138 check_erased_page = handle_ecc(denali, buf, status_type);
1139 //check_erased_page = 0;
1140 denali_enable_dma(denali, false);
1141 setup_ecc_the_end(denali, false); //zhouqi
1142
1143 if (check_erased_page)
1144 {
1145 read_oob_data(denali->mtd, chip->oob_poi, denali->page);
1146
1147 /* check ECC failures that may have occurred on erased pages */
1148 if (!is_erased(buf, denali->mtd->writesize))
1149 denali->mtd->ecc_stats.failed++;
1150 if (!is_erased(chip->oob_poi, denali->mtd->oobsize))
1151 denali->mtd->ecc_stats.failed++;
1152 }
1153 return 0;
1154}
1155
1156//add by zhouqi
1157static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1158 uint8_t *buf, int page, struct mtd_oob_ops *ops)//add by zhouqi
1159{
1160
1161 struct denali_nand_info *denali = g_denali;
1162
1163 int ecc_bits = readl(denali->flash_reg + ECC_CORRECTION);
1164
1165 if((ops->oobbuf != NULL) && ops->ooblen != 0)
1166 {
1167 //denali_debug("[denali.c]: read_page_ops: page = 0x%0x\n", page);//zhouqi
1168 read_page_ops(mtd, chip, buf, page);
1169 }
1170 else
1171 {
1172 if(denali->page < 64)
1173 {
1174 writel(0x8, denali->flash_reg + ECC_CORRECTION);
1175 }
1176 //denali_debug("[denali.c]: read_page: page = 0x%0x\n", page);//zhouqi
1177 read_page(mtd, chip, buf, page);
1178 writel(ecc_bits, denali->flash_reg + ECC_CORRECTION);
1179 }
1180 update_led_twinkle();
1181 return 0;
1182}
1183
1184static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1185 uint8_t *buf, int page)
1186{
1187 struct denali_nand_info *denali = g_denali;
1188
1189 uint32_t status_type = 0;
1190 uint32_t status_mask = INTR_STATUS__DMA_CMD_COMP;
1191
1192 if (page != denali->page)
1193 {
1194 debug("IN %s: page %d is not"
1195 " equal to denali->page %d, investigate!!",
1196 __func__, page, denali->page);
1197 BUG();
1198 }
1199
1200 setup_ecc_for_xfer(denali, false, true);
1201 denali_enable_dma(denali, true);
1202
1203 denali_setup_dma(denali, DENALI_READ);
1204
1205 /* wait for operation to complete */
1206 status_type = wait_for_ready(denali, status_mask);
1207
1208 denali_enable_dma(denali, false);
1209
1210 memcpy(buf, (void*)denali->buf.dma_buf, mtd->writesize);
1211 memcpy(chip->oob_poi, (void*)(denali->buf.dma_buf + mtd->writesize), mtd->oobsize);
1212
1213 return 0;
1214}
1215
1216static uint8_t denali_read_byte(struct mtd_info *mtd)
1217{
1218 struct denali_nand_info *denali = g_denali;
1219 uint8_t result = 0xff;
1220
1221 if (denali->buf.head < denali->buf.tail)
1222 result = denali->buf.buf[denali->buf.head++];
1223
1224 return result;
1225}
1226
1227static uint16_t denali_read_word(struct mtd_info *mtd)
1228{
1229 struct nand_chip *chip = mtd->priv;
1230 uint16_t result = 0x0;
1231
1232 result = (uint16_t)(*(chip->oob_poi));
1233 result = result << 8;
1234 result |= (uint16_t)(*(chip->oob_poi + 1));
1235
1236 return result;
1237}
1238
1239static void denali_select_chip(struct mtd_info *mtd, int chip)
1240{
1241 struct denali_nand_info *denali = g_denali;
1242
1243 denali->flash_bank = chip;
1244}
1245
1246static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
1247{
1248 struct denali_nand_info *denali = g_denali;
1249 int status = denali->status;
1250 denali->status = 0;
1251
1252 return status;
1253}
1254
1255static void denali_erase(struct mtd_info *mtd, int page)
1256{
1257 #if ECC_TEST_VER
1258 return;
1259 #endif
1260 struct denali_nand_info *denali = g_denali;
1261
1262 uint32_t cmd = 0x0, status_type = 0;
1263
1264 /* setup page read request for access type */
1265 cmd = MODE_10 | BANK(denali->flash_bank) | page;
1266 index_addr(denali, (uint32_t)cmd, 0x1);
1267
1268 /* wait for erase to complete or failure to occur */
1269 status_type = wait_for_ready(denali, INTR_STATUS__ERASE_COMP |
1270 INTR_STATUS__ERASE_FAIL);
1271
1272 denali->status = (status_type & INTR_STATUS__ERASE_FAIL) ?
1273 NAND_STATUS_FAIL : PASS;
1274 update_led_twinkle();
1275}
1276
1277static void denali_cmdfunc(struct mtd_info *mtd, unsigned int cmd, int col,
1278 int page)
1279{
1280 struct denali_nand_info *denali = g_denali;
1281 uint32_t addr, id;
1282 int i;
1283
1284 switch (cmd) {
1285 case NAND_CMD_PAGEPROG:
1286 break;
1287 case NAND_CMD_STATUS:
1288 reset_buf(denali);
1289 read_status(denali);
1290 break;
1291 case NAND_CMD_READID:
1292 case NAND_CMD_PARAM:
1293 reset_buf(denali);
1294 /*sometimes ManufactureId read from register is not right
1295 * e.g. some of Micron MT29F32G08QAA MLC NAND chips
1296 * So here we send READID cmd to NAND insteand
1297 * */
1298 addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
1299 index_addr(denali, (uint32_t)addr | 0, 0x90);
1300 index_addr(denali, (uint32_t)addr | 1, 0);
1301 for (i = 0; i < 5; i++)
1302 {
1303 index_addr_read_data(denali,(uint32_t)addr | 2, &id);
1304 write_byte_to_buf(denali, id);
1305 }
1306 break;
1307 case NAND_CMD_READ0:
1308 case NAND_CMD_SEQIN:
1309 denali->page = page;
1310 break;
1311 case NAND_CMD_RESET:
1312 reset_bank(denali);
1313 break;
1314 case NAND_CMD_READOOB:
1315 reset_buf(denali); //zhouqi
1316 denali_read_oob(mtd, mtd->priv, page, 0);
1317 /* TODO: Read OOB data */
1318 break;
1319 default:
1320 debug(": unsupported command"
1321 " received 0x%x\n", cmd);
1322 break;
1323 }
1324}
1325
1326/* stubs for ECC functions not used by the NAND core */
1327static int denali_ecc_calculate(struct mtd_info *mtd, const uint8_t *data,
1328 uint8_t *ecc_code)
1329{
1330 debug("denali_ecc_calculate called unexpectedly\n");
1331 BUG();
1332 return -1;
1333}
1334
1335static int denali_ecc_correct(struct mtd_info *mtd, uint8_t *data,
1336 uint8_t *read_ecc, uint8_t *calc_ecc)
1337{
1338 debug("denali_ecc_correct called unexpectedly\n");
1339 BUG();
1340 return -1;
1341}
1342
1343static void denali_ecc_hwctl(struct mtd_info *mtd, int mode)
1344{
1345 debug("denali_ecc_hwctl called unexpectedly\n");
1346 BUG();
1347}
1348/* end NAND core entry points */
1349
1350
1351/* Initialization code to bring the device up to a known good state */
1352static void denali_hw_init(struct denali_nand_info *denali)
1353{
1354 uint32_t id_bytes[5], addr;
1355 uint8_t i, maf_id, device_id, res_id;
1356 struct nand_flash_device_para * table = nand_flash_para;
1357
1358 denali->flash_reg = (void __iomem *)NAND_FLASH_REG;
1359 denali->flash_mem = (void __iomem *)NAND_FLASH_MEM;
1360
1361 detect_max_banks(denali); /* test the max banks support*/
1362 denali_nand_reset(denali);
1363 writel(0, denali->flash_reg + DMA_ENABLE); /* dma disable */
1364 writel(0, denali->flash_reg + ECC_ENABLE); /* ecc disable */
1365 writel(2, denali->flash_reg + SPARE_AREA_SKIP_BYTES);
1366 denali->bbtskipbytes = readl(denali->flash_reg +
1367 SPARE_AREA_SKIP_BYTES);
1368 writel(0x0F, denali->flash_reg + RB_PIN_ENABLED);
1369 writel(0, denali->flash_reg + CHIP_ENABLE_DONT_CARE);
1370 writel(1, denali->flash_reg + DEVICES_CONNECTED);
1371
1372
1373 writel(0xffff, denali->flash_reg + SPARE_AREA_MARKER);
1374
1375
1376 /* Use read id method to get device ID and other */
1377 addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
1378 index_addr(denali, (uint32_t)addr | 0, 0x90);
1379 index_addr(denali, (uint32_t)addr | 1, 0);
1380 for (i = 0; i < 5; i++)
1381 index_addr_read_data(denali, addr | 2, &id_bytes[i]);
1382 maf_id = id_bytes[0];
1383 device_id = id_bytes[1];
1384 res_id = id_bytes[2];
1385
1386 for (; table->manuf_id != 0; table++)
1387 {
1388 if ((maf_id == table->manuf_id) && (device_id == table->device_id)
1389 && (res_id == table->res_id) )
1390 {
1391 break;
1392 }
1393 }
1394 g_nand_dev_info = table;
1395 printf("maf_id=%x,dev_id=%x,res_id=%x\n",maf_id,device_id,res_id);
1396 /* Should set value for these registers when init */
1397 denali_nand_register_set(denali, table);
1398 denali_nand_timing_set(denali, table);
1399 //find_valid_banks(denali); //zhouqi for 7520 fpga
1400 //detect_partition_feature(denali);
1401 denali_irq_init(denali);
1402
1403}
1404
1405/* Althogh controller spec said SLC ECC is forceb to be 4bit,
1406 * but denali controller in MRST only support 15bit and 8bit ECC
1407 * correction
1408 * */
1409
1410static struct nand_ecclayout g_nand_oob;
1411
1412static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1413static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1414
1415static struct nand_bbt_descr bbt_main_descr = {
1416 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1417 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1418 .offs = 8,
1419 .len = 4,
1420 .veroffs = 12,
1421 .maxblocks = 4,
1422 .pattern = bbt_pattern,
1423};
1424
1425static struct nand_bbt_descr bbt_mirror_descr = {
1426 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1427 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1428 .offs = 8,
1429 .len = 4,
1430 .veroffs = 12,
1431 .maxblocks = 4,
1432 .pattern = mirror_pattern,
1433};
1434
1435/* initialize driver data structures */
1436void denali_drv_init(struct denali_nand_info *denali)
1437{
1438 /* indicate that MTD has not selected a valid bank yet */
1439 denali->flash_bank = CHIP_SELECT_INVALID;
1440
1441 /* initialize our status_type variable to indicate no interrupts */
1442}
1443
1444static int denali_nand_ecc_init(struct denali_nand_info *denali)
1445{
1446 int i,eccpos_start;
1447 denali->nand->ecc.mode = NAND_ECC_HW_SYNDROME;
1448 denali->nand->ecc.size = g_nand_dev_info->ecc.sector_size;
1449 denali->nand->ecc.steps = g_nand_dev_info->page_size/g_nand_dev_info->ecc.sector_size;
1450 denali->nand->ecc.strength = g_nand_dev_info->ecc.strength;
1451
1452 switch (denali->nand->ecc.size) {
1453 case 512:
1454 denali->nand->ecc.bytes =
1455 ( denali->nand->ecc.strength * 13 + 15) / 16 * 2;
1456 break;
1457 case 1024:
1458 denali->nand->ecc.bytes =
1459 ( denali->nand->ecc.strength * 14 + 15) / 16 * 2;
1460 break;
1461 default:
1462 printk("Unsupported ECC sector size\n");
1463
1464 BUG_ON(1);
1465 return -1;
1466 }
1467
1468 denali->nand->ecc.total = denali->nand->ecc.bytes* denali->nand->ecc.steps;
1469 if(g_nand_dev_info->oob_size >= (denali->nand->ecc.total+denali->bbtskipbytes + 8))
1470 {
1471
1472 writel(g_nand_dev_info->ecc.strength, denali->flash_reg + ECC_CORRECTION);
1473 g_nand_oob.eccbytes = denali->nand->ecc.total;
1474
1475 eccpos_start = denali->bbtskipbytes;
1476
1477 for (i = 0; i < g_nand_oob.eccbytes; i++)
1478 {
1479 g_nand_oob.eccpos[i] = eccpos_start + i;
1480 }
1481
1482 g_nand_oob.oobfree[0].offset = g_nand_oob.eccbytes+denali->bbtskipbytes;
1483 g_nand_oob.oobfree[0].length = g_nand_dev_info->oob_size -(g_nand_oob.eccbytes+denali->bbtskipbytes);
1484 denali->nand->ecc.layout = &g_nand_oob;
1485 }
1486 else
1487 {
1488 printk("Unsupported ECC strength,please check the id table\n");
1489 BUG();
1490 }
1491
1492
1493 return 0;
1494}
1495
1496
1497/* driver entry point */
1498int board_nand_init_denali(struct nand_chip *nand)
1499{
1500 int ret = -1;
1501 struct denali_nand_info *denali = g_denali;
1502
1503 denali_hw_init(denali);
1504 denali_drv_init(denali);
1505
1506 denali->mtd = (struct mtd_info *)&nand_info;
1507 denali->nand = (struct nand_chip *)&nand_chip;
1508 denali->buf.dma_buf = (dma_addr_t)CONFIG_NAND_DMA_BUF_ADDR;
1509
1510 /* register the driver with the NAND core subsystem */
1511 denali->nand->select_chip = denali_select_chip;
1512 denali->nand->cmdfunc = denali_cmdfunc;
1513 denali->nand->read_byte = denali_read_byte;
1514 denali->nand->read_word = denali_read_word;
1515 denali->nand->waitfunc = denali_waitfunc;
1516
1517 /* scan for NAND devices attached to the controller
1518 * this is the first stage in a two step process to register
1519 * with the nand subsystem */
1520
1521 if (nand_scan_ident(denali->mtd, denali->total_used_banks, NULL))
1522 {
1523 debug("nand ident: cant ident this nand device");
1524 return -1;
1525 }
1526
1527 /* MTD supported page sizes vary by kernel. We validate our
1528 * kernel supports the device here.
1529 */
1530 if (denali->mtd->writesize > NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE)
1531 {
1532 ret = -1;
1533 debug("Spectra: device size not supported by this version of MTD.");
1534 return ret;
1535 }
1536
1537 /* support for multi nand
1538 * MTD known nothing about multi nand,
1539 * so we should tell it the real pagesize
1540 * and anything necessery
1541 */
1542 denali->devnum = readl(denali->flash_reg + DEVICES_CONNECTED);
1543 denali->nand->chipsize <<= (denali->devnum - 1);
1544 denali->nand->page_shift += (denali->devnum - 1);
1545 denali->nand->pagemask = (denali->nand->chipsize >> denali->nand->page_shift) - 1;
1546 denali->nand->bbt_erase_shift += (denali->devnum - 1);
1547 denali->nand->phys_erase_shift = denali->nand->bbt_erase_shift;
1548 denali->nand->chip_shift += (denali->devnum - 1);
1549 denali->mtd->writesize <<= (denali->devnum - 1);
1550 denali->mtd->oobsize <<= (denali->devnum - 1);
1551 denali->mtd->erasesize <<= (denali->devnum - 1);
1552 denali->mtd->size = denali->nand->numchips * denali->nand->chipsize;
1553 denali->bbtskipbytes *= denali->devnum;
1554
1555 /* second stage of the NAND scan
1556 * this stage requires information regarding ECC and
1557 * bad block management. */
1558
1559 /* Bad block management */
1560 denali->nand->bbt_td = &bbt_main_descr;
1561 denali->nand->bbt_md = &bbt_mirror_descr;
1562
1563 /* skip the scan for now until we have OOB read and write support */
1564 denali->nand->options |= NAND_USE_FLASH_BBT;//NAND_SKIP_BBTSCAN
1565
1566 // init ecc
1567 denali_nand_ecc_init(denali);
1568
1569 /* Let driver know the total blocks number and
1570 * how many blocks contained by each nand chip.
1571 * blksperchip will help driver to know how many
1572 * blocks is taken by FW.
1573 * */
1574 denali->totalblks = denali->mtd->size >> denali->nand->phys_erase_shift;
1575 denali->blksperchip = denali->totalblks / denali->nand->numchips;
1576
1577 /* These functions are required by the NAND core framework, otherwise,
1578 * the NAND core will assert. However, we don't need them, so we'll stub
1579 * them out. */
1580 denali->nand->ecc.calculate = denali_ecc_calculate;
1581 denali->nand->ecc.correct = denali_ecc_correct;
1582 denali->nand->ecc.hwctl = denali_ecc_hwctl;
1583
1584 /* override the default read operations */
1585 denali->nand->ecc.read_page = denali_read_page;
1586 denali->nand->ecc.read_page_raw = denali_read_page_raw;
1587 denali->nand->ecc.write_page = denali_write_page;
1588 denali->nand->ecc.write_page_raw = denali_write_page_raw;
1589 denali->nand->ecc.read_oob = denali_read_oob;
1590 denali->nand->ecc.write_oob = denali_write_oob;
1591 denali->nand->erase_cmd = denali_erase;
1592
1593 if (nand_scan_tail(denali->mtd))
1594 {
1595 ret = -1;
1596 }
1597
1598 return 0;
1599}
1600