📄 nand.c
字号:
/* * (C) Copyright 2006 DENX Software Engineering * * See file CREDITS for list of people who contributed to this * project. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA */#include <common.h>#if (CONFIG_COMMANDS & CFG_CMD_NAND)#ifdef CONFIG_NEW_NAND_CODE#include <nand.h>#include <asm/arch/pxa-regs.h>#ifdef CFG_DFC_DEBUG1# define DFC_DEBUG1(fmt, args...) printf(fmt, ##args)#else# define DFC_DEBUG1(fmt, args...)#endif#ifdef CFG_DFC_DEBUG2# define DFC_DEBUG2(fmt, args...) printf(fmt, ##args)#else# define DFC_DEBUG2(fmt, args...)#endif#ifdef CFG_DFC_DEBUG3# define DFC_DEBUG3(fmt, args...) printf(fmt, ##args)#else# define DFC_DEBUG3(fmt, args...)#endif#define MIN(x, y) ((x < y) ? x : y)/* These really don't belong here, as they are specific to the NAND Model */static uint8_t scan_ff_pattern[] = { 0xff, 0xff };static struct nand_bbt_descr delta_bbt_descr = { .options = 0, .offs = 0, .len = 2, .pattern = scan_ff_pattern};static struct nand_oobinfo delta_oob = { .useecc = MTD_NANDECC_AUTOPL_USR, /* MTD_NANDECC_PLACEONLY, */ .eccbytes = 6, .eccpos = {2, 3, 4, 5, 6, 7}, .oobfree = { {8, 2}, {12, 4} }};/* * not required for Monahans DFC */static void dfc_hwcontrol(struct mtd_info *mtdinfo, int cmd){ return;}#if 0/* read device ready pin */static int dfc_device_ready(struct mtd_info *mtdinfo){ if(NDSR & NDSR_RDY) return 1; else return 0; return 0;}#endif/* * Write buf to the DFC Controller Data Buffer */static void dfc_write_buf(struct mtd_info *mtd, const u_char *buf, int len){ unsigned long bytes_multi = len & 0xfffffffc; unsigned long rest = len & 0x3; unsigned long *long_buf; int i; DFC_DEBUG2("dfc_write_buf: writing %d bytes starting with 0x%x.\n", len, *((unsigned long*) buf)); if(bytes_multi) { for(i=0; i<bytes_multi; i+=4) { long_buf = (unsigned long*) &buf[i]; NDDB = *long_buf; } } if(rest) { printf("dfc_write_buf: ERROR, writing non 4-byte aligned data.\n"); } return;}/* * These functions are quite problematic for the DFC. Luckily they are * not used in the current nand code, except for nand_command, which * we've defined our own anyway. The problem is, that we always need * to write 4 bytes to the DFC Data Buffer, but in these functions we * don't know if to buffer the bytes/half words until we've gathered 4 * bytes or if to send them straight away. * * Solution: Don't use these with Mona's DFC and complain loudly. */static void dfc_write_word(struct mtd_info *mtd, u16 word){ printf("dfc_write_word: WARNING, this function does not work with the Monahans DFC!\n");}static void dfc_write_byte(struct mtd_info *mtd, u_char byte){ printf("dfc_write_byte: WARNING, this function does not work with the Monahans DFC!\n");}/* The original: * static void dfc_read_buf(struct mtd_info *mtd, const u_char *buf, int len) * * Shouldn't this be "u_char * const buf" ? */static void dfc_read_buf(struct mtd_info *mtd, u_char* const buf, int len){ int i=0, j; /* we have to be carefull not to overflow the buffer if len is * not a multiple of 4 */ unsigned long bytes_multi = len & 0xfffffffc; unsigned long rest = len & 0x3; unsigned long *long_buf; DFC_DEBUG3("dfc_read_buf: reading %d bytes.\n", len); /* if there are any, first copy multiple of 4 bytes */ if(bytes_multi) { for(i=0; i<bytes_multi; i+=4) { long_buf = (unsigned long*) &buf[i]; *long_buf = NDDB; } } /* ...then the rest */ if(rest) { unsigned long rest_data = NDDB; for(j=0;j<rest; j++) buf[i+j] = (u_char) ((rest_data>>j) & 0xff); } return;}/* * read a word. Not implemented as not used in NAND code. */static u16 dfc_read_word(struct mtd_info *mtd){ printf("dfc_write_byte: UNIMPLEMENTED.\n"); return 0;}/* global var, too bad: mk@tbd: move to ->priv pointer */static unsigned long read_buf = 0;static int bytes_read = -1;/* * read a byte from NDDB Because we can only read 4 bytes from NDDB at * a time, we buffer the remaining bytes. The buffer is reset when a * new command is sent to the chip. * * WARNING: * This function is currently only used to read status and id * bytes. For these commands always 8 bytes need to be read from * NDDB. So we read and discard these bytes right now. In case this * function is used for anything else in the future, we must check * what was the last command issued and read the appropriate amount of * bytes respectively. */static u_char dfc_read_byte(struct mtd_info *mtd){ unsigned char byte; unsigned long dummy; if(bytes_read < 0) { read_buf = NDDB; dummy = NDDB; bytes_read = 0; } byte = (unsigned char) (read_buf>>(8 * bytes_read++)); if(bytes_read >= 4) bytes_read = -1; DFC_DEBUG2("dfc_read_byte: byte %u: 0x%x of (0x%x).\n", bytes_read - 1, byte, read_buf); return byte;}/* calculate delta between OSCR values start and now */static unsigned long get_delta(unsigned long start){ unsigned long cur = OSCR; if(cur < start) /* OSCR overflowed */ return (cur + (start^0xffffffff)); else return (cur - start);}/* delay function, this doesn't belong here */static void wait_us(unsigned long us){ unsigned long start = OSCR; us *= OSCR_CLK_FREQ; while (get_delta(start) < us) { /* do nothing */ }}static void dfc_clear_nddb(void){ NDCR &= ~NDCR_ND_RUN; wait_us(CFG_NAND_OTHER_TO);}/* wait_event with timeout */static unsigned long dfc_wait_event(unsigned long event){ unsigned long ndsr, timeout, start = OSCR; if(!event) return 0xff000000; else if(event & (NDSR_CS0_CMDD | NDSR_CS0_BBD)) timeout = CFG_NAND_PROG_ERASE_TO * OSCR_CLK_FREQ; else timeout = CFG_NAND_OTHER_TO * OSCR_CLK_FREQ; while(1) { ndsr = NDSR; if(ndsr & event) { NDSR |= event; break; } if(get_delta(start) > timeout) { DFC_DEBUG1("dfc_wait_event: TIMEOUT waiting for event: 0x%x.\n", event); return 0xff000000; } } return ndsr;}/* we don't always wan't to do this */static void dfc_new_cmd(void){ int retry = 0; unsigned long status; while(retry++ <= CFG_NAND_SENDCMD_RETRY) { /* Clear NDSR */ NDSR = 0xFFF; /* set NDCR[NDRUN] */ if(!(NDCR & NDCR_ND_RUN)) NDCR |= NDCR_ND_RUN; status = dfc_wait_event(NDSR_WRCMDREQ); if(status & NDSR_WRCMDREQ) return; DFC_DEBUG2("dfc_new_cmd: FAILED to get WRITECMDREQ, retry: %d.\n", retry); dfc_clear_nddb(); } DFC_DEBUG1("dfc_new_cmd: giving up after %d retries.\n", retry);}/* this function is called after Programm and Erase Operations to * check for success or failure */static int dfc_wait(struct mtd_info *mtd, struct nand_chip *this, int state){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -