nand.c
来自「最新版的u-boot,2008-10-18发布」· C语言 代码 · 共 563 行 · 第 1/2 页
C
563 行
/* * (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 defined(CONFIG_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_ecclayout delta_oob = { .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 *mtd, int cmd, unsigned int ctrl){ 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;}/* 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_read_word: 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%lx.\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){ unsigned long ndsr=0, event=0; int state = this->state; if(state == FL_WRITING) { event = NDSR_CS0_CMDD | NDSR_CS0_BBD; } else if(state == FL_ERASING) { event = NDSR_CS0_CMDD | NDSR_CS0_BBD; } ndsr = dfc_wait_event(event);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?