blob: aff7c54fc2460975910815b8518f7d103357d5f2 [file] [log] [blame]
Markus Klotzbücher69493282006-02-28 18:05:25 +01001/*
2 * (C) Copyright 2006 DENX Software Engineering
3 *
4 * See file CREDITS for list of people who contributed to this
5 * project.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
21 */
22
23#include <common.h>
24
Jon Loeligerb9307262007-07-09 18:24:55 -050025#if defined(CONFIG_CMD_NAND)
Jean-Christophe PLAGNIOL-VILLARDcc4a0ce2008-08-13 01:40:43 +020026#if !defined(CONFIG_NAND_LEGACY)
Markus Klotzbücher69493282006-02-28 18:05:25 +010027
28#include <nand.h>
29#include <asm/arch/pxa-regs.h>
30
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020031#ifdef CONFIG_SYS_DFC_DEBUG1
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +010032# define DFC_DEBUG1(fmt, args...) printf(fmt, ##args)
33#else
34# define DFC_DEBUG1(fmt, args...)
35#endif
36
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020037#ifdef CONFIG_SYS_DFC_DEBUG2
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +010038# define DFC_DEBUG2(fmt, args...) printf(fmt, ##args)
39#else
40# define DFC_DEBUG2(fmt, args...)
41#endif
42
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020043#ifdef CONFIG_SYS_DFC_DEBUG3
Markus Klotzbücherf9e02912006-03-06 13:45:42 +010044# define DFC_DEBUG3(fmt, args...) printf(fmt, ##args)
45#else
46# define DFC_DEBUG3(fmt, args...)
47#endif
48
Markus Klotzbücher43638c62006-03-06 15:04:25 +010049/* These really don't belong here, as they are specific to the NAND Model */
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +010050static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
51
52static struct nand_bbt_descr delta_bbt_descr = {
53 .options = 0,
54 .offs = 0,
55 .len = 2,
56 .pattern = scan_ff_pattern
57};
58
Scott Wood66446412008-09-10 11:48:49 -050059static struct nand_ecclayout delta_oob = {
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +010060 .eccbytes = 6,
61 .eccpos = {2, 3, 4, 5, 6, 7},
62 .oobfree = { {8, 2}, {12, 4} }
63};
64
Markus Klotzbücher69493282006-02-28 18:05:25 +010065/*
Markus Klotzbücher481911c2006-03-01 23:33:27 +010066 * not required for Monahans DFC
Markus Klotzbücher69493282006-02-28 18:05:25 +010067 */
William Juulcfa460a2007-10-31 13:53:06 +010068static void dfc_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Markus Klotzbücher69493282006-02-28 18:05:25 +010069{
Markus Klotzbücher481911c2006-03-01 23:33:27 +010070 return;
Markus Klotzbücher69493282006-02-28 18:05:25 +010071}
72
Markus Klotzbücher43638c62006-03-06 15:04:25 +010073#if 0
Markus Klotzbücher69493282006-02-28 18:05:25 +010074/* read device ready pin */
Markus Klotzbücher43638c62006-03-06 15:04:25 +010075static int dfc_device_ready(struct mtd_info *mtdinfo)
Markus Klotzbücher69493282006-02-28 18:05:25 +010076{
77 if(NDSR & NDSR_RDY)
78 return 1;
79 else
80 return 0;
Markus Klotzbücher69493282006-02-28 18:05:25 +010081 return 0;
82}
Markus Klotzbücher43638c62006-03-06 15:04:25 +010083#endif
Markus Klotzbücher69493282006-02-28 18:05:25 +010084
Markus Klotzbücher19fdeff2006-03-03 12:11:11 +010085/*
86 * Write buf to the DFC Controller Data Buffer
87 */
Markus Klotzbücher43638c62006-03-06 15:04:25 +010088static void dfc_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
Markus Klotzbücher19fdeff2006-03-03 12:11:11 +010089{
90 unsigned long bytes_multi = len & 0xfffffffc;
91 unsigned long rest = len & 0x3;
92 unsigned long *long_buf;
93 int i;
Wolfgang Denk951a9542006-03-06 23:18:48 +010094
Markus Klotzbücher43638c62006-03-06 15:04:25 +010095 DFC_DEBUG2("dfc_write_buf: writing %d bytes starting with 0x%x.\n", len, *((unsigned long*) buf));
Markus Klotzbücher19fdeff2006-03-03 12:11:11 +010096 if(bytes_multi) {
97 for(i=0; i<bytes_multi; i+=4) {
98 long_buf = (unsigned long*) &buf[i];
99 NDDB = *long_buf;
100 }
101 }
102 if(rest) {
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100103 printf("dfc_write_buf: ERROR, writing non 4-byte aligned data.\n");
Markus Klotzbücher19fdeff2006-03-03 12:11:11 +0100104 }
105 return;
106}
107
108
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100109static void dfc_read_buf(struct mtd_info *mtd, u_char* const buf, int len)
Markus Klotzbücher69493282006-02-28 18:05:25 +0100110{
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100111 int i=0, j;
Markus Klotzbücher69493282006-02-28 18:05:25 +0100112
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100113 /* we have to be carefull not to overflow the buffer if len is
114 * not a multiple of 4 */
Markus Klotzbücher19fdeff2006-03-03 12:11:11 +0100115 unsigned long bytes_multi = len & 0xfffffffc;
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100116 unsigned long rest = len & 0x3;
Markus Klotzbücher19fdeff2006-03-03 12:11:11 +0100117 unsigned long *long_buf;
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100118
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100119 DFC_DEBUG3("dfc_read_buf: reading %d bytes.\n", len);
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100120 /* if there are any, first copy multiple of 4 bytes */
Markus Klotzbücher19fdeff2006-03-03 12:11:11 +0100121 if(bytes_multi) {
122 for(i=0; i<bytes_multi; i+=4) {
123 long_buf = (unsigned long*) &buf[i];
Markus Klotzbücherbb1ff042006-03-02 12:10:01 +0100124 *long_buf = NDDB;
125 }
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100126 }
Wolfgang Denk951a9542006-03-06 23:18:48 +0100127
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100128 /* ...then the rest */
129 if(rest) {
130 unsigned long rest_data = NDDB;
Markus Klotzbücher19fdeff2006-03-03 12:11:11 +0100131 for(j=0;j<rest; j++)
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100132 buf[i+j] = (u_char) ((rest_data>>j) & 0xff);
133 }
134
135 return;
136}
137
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100138/*
139 * read a word. Not implemented as not used in NAND code.
140 */
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100141static u16 dfc_read_word(struct mtd_info *mtd)
Markus Klotzbücher19fdeff2006-03-03 12:11:11 +0100142{
William Juulcfa460a2007-10-31 13:53:06 +0100143 printf("dfc_read_word: UNIMPLEMENTED.\n");
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100144 return 0;
Markus Klotzbücher19fdeff2006-03-03 12:11:11 +0100145}
146
147/* global var, too bad: mk@tbd: move to ->priv pointer */
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100148static unsigned long read_buf = 0;
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100149static int bytes_read = -1;
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100150
Wolfgang Denk951a9542006-03-06 23:18:48 +0100151/*
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100152 * read a byte from NDDB Because we can only read 4 bytes from NDDB at
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100153 * a time, we buffer the remaining bytes. The buffer is reset when a
154 * new command is sent to the chip.
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100155 *
156 * WARNING:
157 * This function is currently only used to read status and id
158 * bytes. For these commands always 8 bytes need to be read from
159 * NDDB. So we read and discard these bytes right now. In case this
160 * function is used for anything else in the future, we must check
161 * what was the last command issued and read the appropriate amount of
162 * bytes respectively.
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100163 */
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100164static u_char dfc_read_byte(struct mtd_info *mtd)
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100165{
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100166 unsigned char byte;
Markus Klotzbücherf9e02912006-03-06 13:45:42 +0100167 unsigned long dummy;
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100168
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100169 if(bytes_read < 0) {
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100170 read_buf = NDDB;
Wolfgang Denk951a9542006-03-06 23:18:48 +0100171 dummy = NDDB;
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100172 bytes_read = 0;
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100173 }
174 byte = (unsigned char) (read_buf>>(8 * bytes_read++));
175 if(bytes_read >= 4)
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100176 bytes_read = -1;
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100177
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100178 DFC_DEBUG2("dfc_read_byte: byte %u: 0x%x of (0x%x).\n", bytes_read - 1, byte, read_buf);
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100179 return byte;
Markus Klotzbücher69493282006-02-28 18:05:25 +0100180}
181
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100182/* calculate delta between OSCR values start and now */
183static unsigned long get_delta(unsigned long start)
Markus Klotzbücher19fdeff2006-03-03 12:11:11 +0100184{
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100185 unsigned long cur = OSCR;
Wolfgang Denk951a9542006-03-06 23:18:48 +0100186
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100187 if(cur < start) /* OSCR overflowed */
188 return (cur + (start^0xffffffff));
189 else
190 return (cur - start);
191}
Markus Klotzbücher19fdeff2006-03-03 12:11:11 +0100192
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100193/* delay function, this doesn't belong here */
194static void wait_us(unsigned long us)
195{
Markus Klotzbücher19fdeff2006-03-03 12:11:11 +0100196 unsigned long start = OSCR;
Markus Klotzbücher19fdeff2006-03-03 12:11:11 +0100197 us *= OSCR_CLK_FREQ;
198
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100199 while (get_delta(start) < us) {
200 /* do nothing */
Markus Klotzbücher19fdeff2006-03-03 12:11:11 +0100201 }
202}
203
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100204static void dfc_clear_nddb(void)
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100205{
206 NDCR &= ~NDCR_ND_RUN;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200207 wait_us(CONFIG_SYS_NAND_OTHER_TO);
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100208}
209
210/* wait_event with timeout */
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100211static unsigned long dfc_wait_event(unsigned long event)
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100212{
213 unsigned long ndsr, timeout, start = OSCR;
Wolfgang Denk951a9542006-03-06 23:18:48 +0100214
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100215 if(!event)
216 return 0xff000000;
217 else if(event & (NDSR_CS0_CMDD | NDSR_CS0_BBD))
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200218 timeout = CONFIG_SYS_NAND_PROG_ERASE_TO * OSCR_CLK_FREQ;
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100219 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200220 timeout = CONFIG_SYS_NAND_OTHER_TO * OSCR_CLK_FREQ;
Wolfgang Denk951a9542006-03-06 23:18:48 +0100221
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100222 while(1) {
223 ndsr = NDSR;
224 if(ndsr & event) {
225 NDSR |= event;
226 break;
227 }
228 if(get_delta(start) > timeout) {
Jean-Christophe PLAGNIOL-VILLARD0a5676b2008-07-12 14:36:34 +0200229 DFC_DEBUG1("dfc_wait_event: TIMEOUT waiting for event: 0x%lx.\n", event);
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100230 return 0xff000000;
231 }
Wolfgang Denk951a9542006-03-06 23:18:48 +0100232
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100233 }
234 return ndsr;
235}
236
Markus Klotzbücher9187a352006-03-03 15:37:01 +0100237/* we don't always wan't to do this */
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100238static void dfc_new_cmd(void)
Markus Klotzbücher9187a352006-03-03 15:37:01 +0100239{
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100240 int retry = 0;
241 unsigned long status;
242
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200243 while(retry++ <= CONFIG_SYS_NAND_SENDCMD_RETRY) {
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100244 /* Clear NDSR */
245 NDSR = 0xFFF;
Wolfgang Denk951a9542006-03-06 23:18:48 +0100246
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100247 /* set NDCR[NDRUN] */
248 if(!(NDCR & NDCR_ND_RUN))
249 NDCR |= NDCR_ND_RUN;
Wolfgang Denk951a9542006-03-06 23:18:48 +0100250
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100251 status = dfc_wait_event(NDSR_WRCMDREQ);
Wolfgang Denk951a9542006-03-06 23:18:48 +0100252
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100253 if(status & NDSR_WRCMDREQ)
254 return;
255
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100256 DFC_DEBUG2("dfc_new_cmd: FAILED to get WRITECMDREQ, retry: %d.\n", retry);
257 dfc_clear_nddb();
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100258 }
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100259 DFC_DEBUG1("dfc_new_cmd: giving up after %d retries.\n", retry);
Markus Klotzbücher9187a352006-03-03 15:37:01 +0100260}
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100261
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100262/* this function is called after Programm and Erase Operations to
263 * check for success or failure */
William Juulcfa460a2007-10-31 13:53:06 +0100264static int dfc_wait(struct mtd_info *mtd, struct nand_chip *this)
Markus Klotzbücher9187a352006-03-03 15:37:01 +0100265{
Markus Klotzbücher9187a352006-03-03 15:37:01 +0100266 unsigned long ndsr=0, event=0;
William Juulcfa460a2007-10-31 13:53:06 +0100267 int state = this->state;
Markus Klotzbücher9187a352006-03-03 15:37:01 +0100268
Markus Klotzbücher9187a352006-03-03 15:37:01 +0100269 if(state == FL_WRITING) {
270 event = NDSR_CS0_CMDD | NDSR_CS0_BBD;
271 } else if(state == FL_ERASING) {
Markus Klotzbücherf8785e92006-03-03 20:13:43 +0100272 event = NDSR_CS0_CMDD | NDSR_CS0_BBD;
Markus Klotzbücher9187a352006-03-03 15:37:01 +0100273 }
Wolfgang Denk951a9542006-03-06 23:18:48 +0100274
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100275 ndsr = dfc_wait_event(event);
Markus Klotzbücher9187a352006-03-03 15:37:01 +0100276
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100277 if((ndsr & NDSR_CS0_BBD) || (ndsr & 0xff000000))
Markus Klotzbücher9187a352006-03-03 15:37:01 +0100278 return(0x1); /* Status Read error */
279 return 0;
280}
281
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100282/* cmdfunc send commands to the DFC */
Wolfgang Denk951a9542006-03-06 23:18:48 +0100283static void dfc_cmdfunc(struct mtd_info *mtd, unsigned command,
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100284 int column, int page_addr)
Markus Klotzbücher69493282006-02-28 18:05:25 +0100285{
286 /* register struct nand_chip *this = mtd->priv; */
Markus Klotzbüchere2053f92006-03-02 14:02:36 +0100287 unsigned long ndcb0=0, ndcb1=0, ndcb2=0, event=0;
Markus Klotzbücher69493282006-02-28 18:05:25 +0100288
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100289 /* clear the ugly byte read buffer */
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100290 bytes_read = -1;
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100291 read_buf = 0;
Markus Klotzbücher69493282006-02-28 18:05:25 +0100292
293 switch (command) {
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100294 case NAND_CMD_READ0:
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100295 DFC_DEBUG3("dfc_cmdfunc: NAND_CMD_READ0, page_addr: 0x%x, column: 0x%x.\n", page_addr, (column>>1));
296 dfc_new_cmd();
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100297 ndcb0 = (NAND_CMD_READ0 | (4<<16));
298 column >>= 1; /* adjust for 16 bit bus */
299 ndcb1 = (((column>>1) & 0xff) |
300 ((page_addr<<8) & 0xff00) |
301 ((page_addr<<8) & 0xff0000) |
302 ((page_addr<<8) & 0xff000000)); /* make this 0x01000000 ? */
Markus Klotzbüchere2053f92006-03-02 14:02:36 +0100303 event = NDSR_RDDREQ;
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100304 goto write_cmd;
Markus Klotzbücherf9e02912006-03-06 13:45:42 +0100305 case NAND_CMD_READ1:
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100306 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_READ1 unimplemented!\n");
Markus Klotzbücherf9e02912006-03-06 13:45:42 +0100307 goto end;
308 case NAND_CMD_READOOB:
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100309 DFC_DEBUG1("dfc_cmdfunc: NAND_CMD_READOOB unimplemented!\n");
Markus Klotzbücherf9e02912006-03-06 13:45:42 +0100310 goto end;
Markus Klotzbücher69493282006-02-28 18:05:25 +0100311 case NAND_CMD_READID:
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100312 dfc_new_cmd();
313 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_READID.\n");
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100314 ndcb0 = (NAND_CMD_READID | (3 << 21) | (1 << 16)); /* addr cycles*/
Markus Klotzbüchere2053f92006-03-02 14:02:36 +0100315 event = NDSR_RDDREQ;
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100316 goto write_cmd;
Markus Klotzbücher69493282006-02-28 18:05:25 +0100317 case NAND_CMD_PAGEPROG:
Markus Klotzbücher9187a352006-03-03 15:37:01 +0100318 /* sent as a multicommand in NAND_CMD_SEQIN */
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100319 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_PAGEPROG empty due to multicmd.\n");
Markus Klotzbücher9187a352006-03-03 15:37:01 +0100320 goto end;
Markus Klotzbücher69493282006-02-28 18:05:25 +0100321 case NAND_CMD_ERASE1:
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100322 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_ERASE1, page_addr: 0x%x, column: 0x%x.\n", page_addr, (column>>1));
323 dfc_new_cmd();
Markus Klotzbücherf8785e92006-03-03 20:13:43 +0100324 ndcb0 = (0xd060 | (1<<25) | (2<<21) | (1<<19) | (3<<16));
325 ndcb1 = (page_addr & 0x00ffffff);
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100326 goto write_cmd;
Markus Klotzbücher69493282006-02-28 18:05:25 +0100327 case NAND_CMD_ERASE2:
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100328 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_ERASE2 empty due to multicmd.\n");
Markus Klotzbücher9187a352006-03-03 15:37:01 +0100329 goto end;
Markus Klotzbücher69493282006-02-28 18:05:25 +0100330 case NAND_CMD_SEQIN:
Markus Klotzbücher9187a352006-03-03 15:37:01 +0100331 /* send PAGE_PROG command(0x1080) */
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100332 dfc_new_cmd();
333 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_SEQIN/PAGE_PROG, page_addr: 0x%x, column: 0x%x.\n", page_addr, (column>>1));
Markus Klotzbücher9187a352006-03-03 15:37:01 +0100334 ndcb0 = (0x1080 | (1<<25) | (1<<21) | (1<<19) | (4<<16));
Markus Klotzbücher19fdeff2006-03-03 12:11:11 +0100335 column >>= 1; /* adjust for 16 bit bus */
336 ndcb1 = (((column>>1) & 0xff) |
337 ((page_addr<<8) & 0xff00) |
338 ((page_addr<<8) & 0xff0000) |
339 ((page_addr<<8) & 0xff000000)); /* make this 0x01000000 ? */
340 event = NDSR_WRDREQ;
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100341 goto write_cmd;
Markus Klotzbücher69493282006-02-28 18:05:25 +0100342 case NAND_CMD_STATUS:
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100343 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_STATUS.\n");
344 dfc_new_cmd();
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100345 ndcb0 = NAND_CMD_STATUS | (4<<21);
Markus Klotzbücher19fdeff2006-03-03 12:11:11 +0100346 event = NDSR_RDDREQ;
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100347 goto write_cmd;
Markus Klotzbücher69493282006-02-28 18:05:25 +0100348 case NAND_CMD_RESET:
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100349 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_RESET.\n");
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100350 ndcb0 = NAND_CMD_RESET | (5<<21);
351 event = NDSR_CS0_CMDD;
352 goto write_cmd;
Markus Klotzbücher69493282006-02-28 18:05:25 +0100353 default:
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100354 printk("dfc_cmdfunc: error, unsupported command.\n");
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100355 goto end;
Markus Klotzbücher69493282006-02-28 18:05:25 +0100356 }
357
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100358 write_cmd:
Markus Klotzbücher69493282006-02-28 18:05:25 +0100359 NDCB0 = ndcb0;
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100360 NDCB0 = ndcb1;
361 NDCB0 = ndcb2;
Markus Klotzbüchere2053f92006-03-02 14:02:36 +0100362
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100363 /* wait_event: */
364 dfc_wait_event(event);
Markus Klotzbücher19fdeff2006-03-03 12:11:11 +0100365 end:
366 return;
Markus Klotzbücher69493282006-02-28 18:05:25 +0100367}
368
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100369static void dfc_gpio_init(void)
Markus Klotzbücher00c35bd2006-02-28 22:51:01 +0100370{
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100371 DFC_DEBUG2("Setting up DFC GPIO's.\n");
Markus Klotzbücher00c35bd2006-02-28 22:51:01 +0100372
373 /* no idea what is done here, see zylonite.c */
374 GPIO4 = 0x1;
Wolfgang Denk951a9542006-03-06 23:18:48 +0100375
Markus Klotzbücher00c35bd2006-02-28 22:51:01 +0100376 DF_ALE_WE1 = 0x00000001;
377 DF_ALE_WE2 = 0x00000001;
378 DF_nCS0 = 0x00000001;
379 DF_nCS1 = 0x00000001;
380 DF_nWE = 0x00000001;
381 DF_nRE = 0x00000001;
382 DF_IO0 = 0x00000001;
383 DF_IO8 = 0x00000001;
384 DF_IO1 = 0x00000001;
385 DF_IO9 = 0x00000001;
386 DF_IO2 = 0x00000001;
387 DF_IO10 = 0x00000001;
388 DF_IO3 = 0x00000001;
389 DF_IO11 = 0x00000001;
390 DF_IO4 = 0x00000001;
391 DF_IO12 = 0x00000001;
392 DF_IO5 = 0x00000001;
393 DF_IO13 = 0x00000001;
394 DF_IO6 = 0x00000001;
395 DF_IO14 = 0x00000001;
396 DF_IO7 = 0x00000001;
397 DF_IO15 = 0x00000001;
398
399 DF_nWE = 0x1901;
400 DF_nRE = 0x1901;
401 DF_CLE_NOE = 0x1900;
402 DF_ALE_WE1 = 0x1901;
403 DF_INT_RnB = 0x1900;
404}
405
Markus Klotzbücher69493282006-02-28 18:05:25 +0100406/*
407 * Board-specific NAND initialization. The following members of the
408 * argument are board-specific (per include/linux/mtd/nand_new.h):
409 * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
410 * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
411 * - hwcontrol: hardwarespecific function for accesing control-lines
412 * - dev_ready: hardwarespecific function for accesing device ready/busy line
413 * - enable_hwecc?: function to enable (reset) hardware ecc generator. Must
414 * only be provided if a hardware ECC is available
William Juulcfa460a2007-10-31 13:53:06 +0100415 * - ecc.mode: mode of ecc, see defines
Markus Klotzbücher69493282006-02-28 18:05:25 +0100416 * - chip_delay: chip dependent delay for transfering data from array to
417 * read regs (tR)
418 * - options: various chip options. They can partly be set to inform
419 * nand_scan about special functionality. See the defines for further
420 * explanation
421 * Members with a "?" were not set in the merged testing-NAND branch,
422 * so they are not set here either.
423 */
Heiko Schocherfa230442006-12-21 17:17:02 +0100424int board_nand_init(struct nand_chip *nand)
Markus Klotzbücher69493282006-02-28 18:05:25 +0100425{
426 unsigned long tCH, tCS, tWH, tWP, tRH, tRP, tRP_high, tR, tWHR, tAR;
427
428 /* set up GPIO Control Registers */
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100429 dfc_gpio_init();
Markus Klotzbücher00c35bd2006-02-28 22:51:01 +0100430
Markus Klotzbücher69493282006-02-28 18:05:25 +0100431 /* turn on the NAND Controller Clock (104 MHz @ D0) */
432 CKENA |= (CKENA_4_NAND | CKENA_9_SMC);
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100433
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200434#undef CONFIG_SYS_TIMING_TIGHT
435#ifndef CONFIG_SYS_TIMING_TIGHT
Wolfgang Denk951a9542006-03-06 23:18:48 +0100436 tCH = MIN(((unsigned long) (NAND_TIMING_tCH * DFC_CLK_PER_US) + 1),
Markus Klotzbücher69493282006-02-28 18:05:25 +0100437 DFC_MAX_tCH);
Wolfgang Denk951a9542006-03-06 23:18:48 +0100438 tCS = MIN(((unsigned long) (NAND_TIMING_tCS * DFC_CLK_PER_US) + 1),
Markus Klotzbücher69493282006-02-28 18:05:25 +0100439 DFC_MAX_tCS);
440 tWH = MIN(((unsigned long) (NAND_TIMING_tWH * DFC_CLK_PER_US) + 1),
441 DFC_MAX_tWH);
442 tWP = MIN(((unsigned long) (NAND_TIMING_tWP * DFC_CLK_PER_US) + 1),
443 DFC_MAX_tWP);
444 tRH = MIN(((unsigned long) (NAND_TIMING_tRH * DFC_CLK_PER_US) + 1),
445 DFC_MAX_tRH);
446 tRP = MIN(((unsigned long) (NAND_TIMING_tRP * DFC_CLK_PER_US) + 1),
447 DFC_MAX_tRP);
448 tR = MIN(((unsigned long) (NAND_TIMING_tR * DFC_CLK_PER_US) + 1),
449 DFC_MAX_tR);
450 tWHR = MIN(((unsigned long) (NAND_TIMING_tWHR * DFC_CLK_PER_US) + 1),
451 DFC_MAX_tWHR);
452 tAR = MIN(((unsigned long) (NAND_TIMING_tAR * DFC_CLK_PER_US) + 1),
453 DFC_MAX_tAR);
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100454#else /* this is the tight timing */
Markus Klotzbücher69493282006-02-28 18:05:25 +0100455
Wolfgang Denk951a9542006-03-06 23:18:48 +0100456 tCH = MIN(((unsigned long) (NAND_TIMING_tCH * DFC_CLK_PER_US)),
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100457 DFC_MAX_tCH);
Wolfgang Denk951a9542006-03-06 23:18:48 +0100458 tCS = MIN(((unsigned long) (NAND_TIMING_tCS * DFC_CLK_PER_US)),
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100459 DFC_MAX_tCS);
460 tWH = MIN(((unsigned long) (NAND_TIMING_tWH * DFC_CLK_PER_US)),
461 DFC_MAX_tWH);
462 tWP = MIN(((unsigned long) (NAND_TIMING_tWP * DFC_CLK_PER_US)),
463 DFC_MAX_tWP);
464 tRH = MIN(((unsigned long) (NAND_TIMING_tRH * DFC_CLK_PER_US)),
465 DFC_MAX_tRH);
466 tRP = MIN(((unsigned long) (NAND_TIMING_tRP * DFC_CLK_PER_US)),
467 DFC_MAX_tRP);
468 tR = MIN(((unsigned long) (NAND_TIMING_tR * DFC_CLK_PER_US) - tCH - 2),
469 DFC_MAX_tR);
470 tWHR = MIN(((unsigned long) (NAND_TIMING_tWHR * DFC_CLK_PER_US) - tCH - 2),
471 DFC_MAX_tWHR);
472 tAR = MIN(((unsigned long) (NAND_TIMING_tAR * DFC_CLK_PER_US) - 2),
473 DFC_MAX_tAR);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200474#endif /* CONFIG_SYS_TIMING_TIGHT */
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100475
476
477 DFC_DEBUG2("tCH=%u, tCS=%u, tWH=%u, tWP=%u, tRH=%u, tRP=%u, tR=%u, tWHR=%u, tAR=%u.\n", tCH, tCS, tWH, tWP, tRH, tRP, tR, tWHR, tAR);
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100478
Markus Klotzbücher69493282006-02-28 18:05:25 +0100479 /* tRP value is split in the register */
480 if(tRP & (1 << 4)) {
481 tRP_high = 1;
482 tRP &= ~(1 << 4);
483 } else {
484 tRP_high = 0;
485 }
486
487 NDTR0CS0 = (tCH << 19) |
488 (tCS << 16) |
489 (tWH << 11) |
490 (tWP << 8) |
491 (tRP_high << 6) |
492 (tRH << 3) |
493 (tRP << 0);
Wolfgang Denk951a9542006-03-06 23:18:48 +0100494
Markus Klotzbücher69493282006-02-28 18:05:25 +0100495 NDTR1CS0 = (tR << 16) |
496 (tWHR << 4) |
497 (tAR << 0);
498
Markus Klotzbücher69493282006-02-28 18:05:25 +0100499 /* If it doesn't work (unlikely) think about:
500 * - ecc enable
501 * - chip select don't care
502 * - read id byte count
503 *
504 * Intentionally enabled by not setting bits:
505 * - dma (DMA_EN)
506 * - page size = 512
507 * - cs don't care, see if we can enable later!
508 * - row address start position (after second cycle)
509 * - pages per block = 32
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100510 * - ND_RDY : clears command buffer
Markus Klotzbücher69493282006-02-28 18:05:25 +0100511 */
Markus Klotzbücherf8785e92006-03-03 20:13:43 +0100512 /* NDCR_NCSX | /\* Chip select busy don't care *\/ */
Wolfgang Denk951a9542006-03-06 23:18:48 +0100513
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100514 NDCR = (NDCR_SPARE_EN | /* use the spare area */
Markus Klotzbücher69493282006-02-28 18:05:25 +0100515 NDCR_DWIDTH_C | /* 16bit DFC data bus width */
516 NDCR_DWIDTH_M | /* 16 bit Flash device data bus width */
Markus Klotzbücherf9e02912006-03-06 13:45:42 +0100517 (2 << 16) | /* read id count = 7 ???? mk@tbd */
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100518 NDCR_ND_ARB_EN | /* enable bus arbiter */
519 NDCR_RDYM | /* flash device ready ir masked */
520 NDCR_CS0_PAGEDM | /* ND_nCSx page done ir masked */
521 NDCR_CS1_PAGEDM |
522 NDCR_CS0_CMDDM | /* ND_CSx command done ir masked */
523 NDCR_CS1_CMDDM |
524 NDCR_CS0_BBDM | /* ND_CSx bad block detect ir masked */
525 NDCR_CS1_BBDM |
Wolfgang Denk951a9542006-03-06 23:18:48 +0100526 NDCR_DBERRM | /* double bit error ir masked */
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100527 NDCR_SBERRM | /* single bit error ir masked */
528 NDCR_WRDREQM | /* write data request ir masked */
529 NDCR_RDDREQM | /* read data request ir masked */
530 NDCR_WRCMDREQM); /* write command request ir masked */
Wolfgang Denk951a9542006-03-06 23:18:48 +0100531
Markus Klotzbücher481911c2006-03-01 23:33:27 +0100532
533 /* wait 10 us due to cmd buffer clear reset */
Markus Klotzbuecher552fc622006-03-20 20:19:37 +0100534 /* wait(10); */
Wolfgang Denk951a9542006-03-06 23:18:48 +0100535
536
William Juulcfa460a2007-10-31 13:53:06 +0100537 nand->cmd_ctrl = dfc_hwcontrol;
Markus Klotzbuecher552fc622006-03-20 20:19:37 +0100538/* nand->dev_ready = dfc_device_ready; */
William Juulcfa460a2007-10-31 13:53:06 +0100539 nand->ecc.mode = NAND_ECC_SOFT;
Scott Wood66446412008-09-10 11:48:49 -0500540 nand->ecc.layout = &delta_oob;
Markus Klotzbücher69493282006-02-28 18:05:25 +0100541 nand->options = NAND_BUSWIDTH_16;
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100542 nand->waitfunc = dfc_wait;
543 nand->read_byte = dfc_read_byte;
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100544 nand->read_word = dfc_read_word;
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100545 nand->read_buf = dfc_read_buf;
546 nand->write_buf = dfc_write_buf;
Markus Klotzbücher9187a352006-03-03 15:37:01 +0100547
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100548 nand->cmdfunc = dfc_cmdfunc;
Markus Klotzbücherbf7cac02006-03-04 18:35:51 +0100549 nand->badblock_pattern = &delta_bbt_descr;
Heiko Schocherfa230442006-12-21 17:17:02 +0100550 return 0;
Markus Klotzbücher69493282006-02-28 18:05:25 +0100551}
552
553#else
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100554 #error "U-Boot legacy NAND support not available for Monahans DFC."
Markus Klotzbücher69493282006-02-28 18:05:25 +0100555#endif
556#endif