blob: 85a6ba291a0e312628b85e31ef9f2c0ff74adb45 [file] [log] [blame]
Markus Klotzbücherf4a5c612006-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 Loeliger4ed9ed62007-07-09 18:24:55 -050025#if defined(CONFIG_CMD_NAND)
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +010026
27#include <nand.h>
28#include <asm/arch/pxa-regs.h>
29
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020030#ifdef CONFIG_SYS_DFC_DEBUG1
Markus Klotzbücher21a43f92006-03-04 18:35:51 +010031# define DFC_DEBUG1(fmt, args...) printf(fmt, ##args)
32#else
33# define DFC_DEBUG1(fmt, args...)
34#endif
35
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020036#ifdef CONFIG_SYS_DFC_DEBUG2
Markus Klotzbücher21a43f92006-03-04 18:35:51 +010037# define DFC_DEBUG2(fmt, args...) printf(fmt, ##args)
38#else
39# define DFC_DEBUG2(fmt, args...)
40#endif
41
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020042#ifdef CONFIG_SYS_DFC_DEBUG3
Markus Klotzbücher85678e22006-03-06 13:45:42 +010043# define DFC_DEBUG3(fmt, args...) printf(fmt, ##args)
44#else
45# define DFC_DEBUG3(fmt, args...)
46#endif
47
Markus Klotzbücher27eba142006-03-06 15:04:25 +010048/* These really don't belong here, as they are specific to the NAND Model */
Markus Klotzbücher21a43f92006-03-04 18:35:51 +010049static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
50
51static struct nand_bbt_descr delta_bbt_descr = {
52 .options = 0,
53 .offs = 0,
54 .len = 2,
55 .pattern = scan_ff_pattern
56};
57
Scott Wood08cb8b92008-09-10 11:48:49 -050058static struct nand_ecclayout delta_oob = {
Markus Klotzbücher21a43f92006-03-04 18:35:51 +010059 .eccbytes = 6,
60 .eccpos = {2, 3, 4, 5, 6, 7},
61 .oobfree = { {8, 2}, {12, 4} }
62};
63
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +010064/*
Markus Klotzbücher432a7b42006-03-01 23:33:27 +010065 * not required for Monahans DFC
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +010066 */
William Juul52c07962007-10-31 13:53:06 +010067static void dfc_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +010068{
Markus Klotzbücher432a7b42006-03-01 23:33:27 +010069 return;
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +010070}
71
Markus Klotzbücher27eba142006-03-06 15:04:25 +010072#if 0
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +010073/* read device ready pin */
Markus Klotzbücher27eba142006-03-06 15:04:25 +010074static int dfc_device_ready(struct mtd_info *mtdinfo)
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +010075{
76 if(NDSR & NDSR_RDY)
77 return 1;
78 else
79 return 0;
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +010080 return 0;
81}
Markus Klotzbücher27eba142006-03-06 15:04:25 +010082#endif
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +010083
Markus Klotzbücherddd78b02006-03-03 12:11:11 +010084/*
85 * Write buf to the DFC Controller Data Buffer
86 */
Markus Klotzbücher27eba142006-03-06 15:04:25 +010087static void dfc_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
Markus Klotzbücherddd78b02006-03-03 12:11:11 +010088{
89 unsigned long bytes_multi = len & 0xfffffffc;
90 unsigned long rest = len & 0x3;
91 unsigned long *long_buf;
92 int i;
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +010093
Markus Klotzbücher27eba142006-03-06 15:04:25 +010094 DFC_DEBUG2("dfc_write_buf: writing %d bytes starting with 0x%x.\n", len, *((unsigned long*) buf));
Markus Klotzbücherddd78b02006-03-03 12:11:11 +010095 if(bytes_multi) {
96 for(i=0; i<bytes_multi; i+=4) {
97 long_buf = (unsigned long*) &buf[i];
98 NDDB = *long_buf;
99 }
100 }
101 if(rest) {
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100102 printf("dfc_write_buf: ERROR, writing non 4-byte aligned data.\n");
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100103 }
104 return;
105}
106
107
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100108static void dfc_read_buf(struct mtd_info *mtd, u_char* const buf, int len)
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100109{
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100110 int i=0, j;
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100111
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100112 /* we have to be carefull not to overflow the buffer if len is
113 * not a multiple of 4 */
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100114 unsigned long bytes_multi = len & 0xfffffffc;
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100115 unsigned long rest = len & 0x3;
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100116 unsigned long *long_buf;
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100117
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100118 DFC_DEBUG3("dfc_read_buf: reading %d bytes.\n", len);
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100119 /* if there are any, first copy multiple of 4 bytes */
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100120 if(bytes_multi) {
121 for(i=0; i<bytes_multi; i+=4) {
122 long_buf = (unsigned long*) &buf[i];
Markus Klotzbüchera3bedae2006-03-02 12:10:01 +0100123 *long_buf = NDDB;
124 }
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100125 }
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100126
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100127 /* ...then the rest */
128 if(rest) {
129 unsigned long rest_data = NDDB;
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100130 for(j=0;j<rest; j++)
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100131 buf[i+j] = (u_char) ((rest_data>>j) & 0xff);
132 }
133
134 return;
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100135}
136
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100137/*
138 * read a word. Not implemented as not used in NAND code.
139 */
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100140static u16 dfc_read_word(struct mtd_info *mtd)
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100141{
William Juul52c07962007-10-31 13:53:06 +0100142 printf("dfc_read_word: UNIMPLEMENTED.\n");
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100143 return 0;
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100144}
145
146/* global var, too bad: mk@tbd: move to ->priv pointer */
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100147static unsigned long read_buf = 0;
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100148static int bytes_read = -1;
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100149
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100150/*
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100151 * read a byte from NDDB Because we can only read 4 bytes from NDDB at
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100152 * a time, we buffer the remaining bytes. The buffer is reset when a
153 * new command is sent to the chip.
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100154 *
155 * WARNING:
156 * This function is currently only used to read status and id
157 * bytes. For these commands always 8 bytes need to be read from
158 * NDDB. So we read and discard these bytes right now. In case this
159 * function is used for anything else in the future, we must check
160 * what was the last command issued and read the appropriate amount of
161 * bytes respectively.
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100162 */
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100163static u_char dfc_read_byte(struct mtd_info *mtd)
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100164{
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100165 unsigned char byte;
Markus Klotzbücher85678e22006-03-06 13:45:42 +0100166 unsigned long dummy;
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100167
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100168 if(bytes_read < 0) {
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100169 read_buf = NDDB;
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100170 dummy = NDDB;
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100171 bytes_read = 0;
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100172 }
173 byte = (unsigned char) (read_buf>>(8 * bytes_read++));
174 if(bytes_read >= 4)
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100175 bytes_read = -1;
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100176
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100177 DFC_DEBUG2("dfc_read_byte: byte %u: 0x%x of (0x%x).\n", bytes_read - 1, byte, read_buf);
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100178 return byte;
179}
180
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100181/* calculate delta between OSCR values start and now */
182static unsigned long get_delta(unsigned long start)
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100183{
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100184 unsigned long cur = OSCR;
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100185
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100186 if(cur < start) /* OSCR overflowed */
187 return (cur + (start^0xffffffff));
188 else
189 return (cur - start);
190}
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100191
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100192/* delay function, this doesn't belong here */
193static void wait_us(unsigned long us)
194{
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100195 unsigned long start = OSCR;
Wolfgang Denk23444552009-08-16 23:40:13 +0200196 us = DIV_ROUND_UP(us * OSCR_CLK_FREQ, 1000);
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100197
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100198 while (get_delta(start) < us) {
199 /* do nothing */
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100200 }
201}
202
Wolfgang Denk7fa6e902006-03-11 22:53:33 +0100203static void dfc_clear_nddb(void)
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100204{
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100205 NDCR &= ~NDCR_ND_RUN;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200206 wait_us(CONFIG_SYS_NAND_OTHER_TO);
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100207}
208
209/* wait_event with timeout */
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100210static unsigned long dfc_wait_event(unsigned long event)
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100211{
212 unsigned long ndsr, timeout, start = OSCR;
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100213
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100214 if(!event)
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100215 return 0xff000000;
216 else if(event & (NDSR_CS0_CMDD | NDSR_CS0_BBD))
Wolfgang Denk23444552009-08-16 23:40:13 +0200217 timeout = DIV_ROUND_UP(CONFIG_SYS_NAND_PROG_ERASE_TO
218 * OSCR_CLK_FREQ, 1000);
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100219 else
Wolfgang Denk23444552009-08-16 23:40:13 +0200220 timeout = DIV_ROUND_UP(CONFIG_SYS_NAND_OTHER_TO
221 * OSCR_CLK_FREQ, 1000);
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100222
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100223 while(1) {
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100224 ndsr = NDSR;
225 if(ndsr & event) {
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100226 NDSR |= event;
227 break;
228 }
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100229 if(get_delta(start) > timeout) {
Jean-Christophe PLAGNIOL-VILLARDc4fb57c2008-07-12 14:36:34 +0200230 DFC_DEBUG1("dfc_wait_event: TIMEOUT waiting for event: 0x%lx.\n", event);
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100231 return 0xff000000;
232 }
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100233
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100234 }
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100235 return ndsr;
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100236}
237
Markus Klotzbücherb6c2d402006-03-03 15:37:01 +0100238/* we don't always wan't to do this */
Wolfgang Denk7fa6e902006-03-11 22:53:33 +0100239static void dfc_new_cmd(void)
Markus Klotzbücherb6c2d402006-03-03 15:37:01 +0100240{
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100241 int retry = 0;
242 unsigned long status;
243
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200244 while(retry++ <= CONFIG_SYS_NAND_SENDCMD_RETRY) {
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100245 /* Clear NDSR */
246 NDSR = 0xFFF;
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100247
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100248 /* set NDCR[NDRUN] */
249 if(!(NDCR & NDCR_ND_RUN))
250 NDCR |= NDCR_ND_RUN;
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100251
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100252 status = dfc_wait_event(NDSR_WRCMDREQ);
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100253
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100254 if(status & NDSR_WRCMDREQ)
255 return;
256
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100257 DFC_DEBUG2("dfc_new_cmd: FAILED to get WRITECMDREQ, retry: %d.\n", retry);
258 dfc_clear_nddb();
Markus Klotzbücherb6c2d402006-03-03 15:37:01 +0100259 }
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100260 DFC_DEBUG1("dfc_new_cmd: giving up after %d retries.\n", retry);
Markus Klotzbücherb6c2d402006-03-03 15:37:01 +0100261}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100262
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100263/* this function is called after Programm and Erase Operations to
264 * check for success or failure */
William Juul52c07962007-10-31 13:53:06 +0100265static int dfc_wait(struct mtd_info *mtd, struct nand_chip *this)
Markus Klotzbücherb6c2d402006-03-03 15:37:01 +0100266{
Markus Klotzbücherb6c2d402006-03-03 15:37:01 +0100267 unsigned long ndsr=0, event=0;
William Juul52c07962007-10-31 13:53:06 +0100268 int state = this->state;
Markus Klotzbücherb6c2d402006-03-03 15:37:01 +0100269
Markus Klotzbücherb6c2d402006-03-03 15:37:01 +0100270 if(state == FL_WRITING) {
271 event = NDSR_CS0_CMDD | NDSR_CS0_BBD;
272 } else if(state == FL_ERASING) {
Markus Klotzbücherb2fc71d2006-03-03 20:13:43 +0100273 event = NDSR_CS0_CMDD | NDSR_CS0_BBD;
Markus Klotzbücherb6c2d402006-03-03 15:37:01 +0100274 }
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100275
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100276 ndsr = dfc_wait_event(event);
Markus Klotzbücherb6c2d402006-03-03 15:37:01 +0100277
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100278 if((ndsr & NDSR_CS0_BBD) || (ndsr & 0xff000000))
Markus Klotzbücherb6c2d402006-03-03 15:37:01 +0100279 return(0x1); /* Status Read error */
280 return 0;
281}
282
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100283/* cmdfunc send commands to the DFC */
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100284static void dfc_cmdfunc(struct mtd_info *mtd, unsigned command,
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100285 int column, int page_addr)
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100286{
287 /* register struct nand_chip *this = mtd->priv; */
Markus Klotzbücherf0840da2006-03-02 14:02:36 +0100288 unsigned long ndcb0=0, ndcb1=0, ndcb2=0, event=0;
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100289
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100290 /* clear the ugly byte read buffer */
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100291 bytes_read = -1;
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100292 read_buf = 0;
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100293
294 switch (command) {
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100295 case NAND_CMD_READ0:
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100296 DFC_DEBUG3("dfc_cmdfunc: NAND_CMD_READ0, page_addr: 0x%x, column: 0x%x.\n", page_addr, (column>>1));
297 dfc_new_cmd();
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100298 ndcb0 = (NAND_CMD_READ0 | (4<<16));
299 column >>= 1; /* adjust for 16 bit bus */
300 ndcb1 = (((column>>1) & 0xff) |
301 ((page_addr<<8) & 0xff00) |
302 ((page_addr<<8) & 0xff0000) |
303 ((page_addr<<8) & 0xff000000)); /* make this 0x01000000 ? */
Markus Klotzbücherf0840da2006-03-02 14:02:36 +0100304 event = NDSR_RDDREQ;
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100305 goto write_cmd;
Markus Klotzbücher85678e22006-03-06 13:45:42 +0100306 case NAND_CMD_READ1:
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100307 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_READ1 unimplemented!\n");
Markus Klotzbücher85678e22006-03-06 13:45:42 +0100308 goto end;
309 case NAND_CMD_READOOB:
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100310 DFC_DEBUG1("dfc_cmdfunc: NAND_CMD_READOOB unimplemented!\n");
Markus Klotzbücher85678e22006-03-06 13:45:42 +0100311 goto end;
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100312 case NAND_CMD_READID:
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100313 dfc_new_cmd();
314 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_READID.\n");
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100315 ndcb0 = (NAND_CMD_READID | (3 << 21) | (1 << 16)); /* addr cycles*/
Markus Klotzbücherf0840da2006-03-02 14:02:36 +0100316 event = NDSR_RDDREQ;
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100317 goto write_cmd;
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100318 case NAND_CMD_PAGEPROG:
Markus Klotzbücherb6c2d402006-03-03 15:37:01 +0100319 /* sent as a multicommand in NAND_CMD_SEQIN */
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100320 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_PAGEPROG empty due to multicmd.\n");
Markus Klotzbücherb6c2d402006-03-03 15:37:01 +0100321 goto end;
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100322 case NAND_CMD_ERASE1:
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100323 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_ERASE1, page_addr: 0x%x, column: 0x%x.\n", page_addr, (column>>1));
324 dfc_new_cmd();
Markus Klotzbücherb2fc71d2006-03-03 20:13:43 +0100325 ndcb0 = (0xd060 | (1<<25) | (2<<21) | (1<<19) | (3<<16));
326 ndcb1 = (page_addr & 0x00ffffff);
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100327 goto write_cmd;
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100328 case NAND_CMD_ERASE2:
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100329 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_ERASE2 empty due to multicmd.\n");
Markus Klotzbücherb6c2d402006-03-03 15:37:01 +0100330 goto end;
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100331 case NAND_CMD_SEQIN:
Markus Klotzbücherb6c2d402006-03-03 15:37:01 +0100332 /* send PAGE_PROG command(0x1080) */
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100333 dfc_new_cmd();
334 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_SEQIN/PAGE_PROG, page_addr: 0x%x, column: 0x%x.\n", page_addr, (column>>1));
Markus Klotzbücherb6c2d402006-03-03 15:37:01 +0100335 ndcb0 = (0x1080 | (1<<25) | (1<<21) | (1<<19) | (4<<16));
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100336 column >>= 1; /* adjust for 16 bit bus */
337 ndcb1 = (((column>>1) & 0xff) |
338 ((page_addr<<8) & 0xff00) |
339 ((page_addr<<8) & 0xff0000) |
340 ((page_addr<<8) & 0xff000000)); /* make this 0x01000000 ? */
341 event = NDSR_WRDREQ;
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100342 goto write_cmd;
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100343 case NAND_CMD_STATUS:
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100344 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_STATUS.\n");
345 dfc_new_cmd();
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100346 ndcb0 = NAND_CMD_STATUS | (4<<21);
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100347 event = NDSR_RDDREQ;
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100348 goto write_cmd;
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100349 case NAND_CMD_RESET:
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100350 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_RESET.\n");
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100351 ndcb0 = NAND_CMD_RESET | (5<<21);
352 event = NDSR_CS0_CMDD;
353 goto write_cmd;
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100354 default:
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100355 printk("dfc_cmdfunc: error, unsupported command.\n");
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100356 goto end;
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100357 }
358
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100359 write_cmd:
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100360 NDCB0 = ndcb0;
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100361 NDCB0 = ndcb1;
362 NDCB0 = ndcb2;
Markus Klotzbücherf0840da2006-03-02 14:02:36 +0100363
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100364 /* wait_event: */
365 dfc_wait_event(event);
Markus Klotzbücherddd78b02006-03-03 12:11:11 +0100366 end:
367 return;
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100368}
369
Wolfgang Denk7fa6e902006-03-11 22:53:33 +0100370static void dfc_gpio_init(void)
Markus Klotzbücherf14cc262006-02-28 22:51:01 +0100371{
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100372 DFC_DEBUG2("Setting up DFC GPIO's.\n");
Markus Klotzbücherf14cc262006-02-28 22:51:01 +0100373
374 /* no idea what is done here, see zylonite.c */
375 GPIO4 = 0x1;
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100376
Markus Klotzbücherf14cc262006-02-28 22:51:01 +0100377 DF_ALE_WE1 = 0x00000001;
378 DF_ALE_WE2 = 0x00000001;
379 DF_nCS0 = 0x00000001;
380 DF_nCS1 = 0x00000001;
381 DF_nWE = 0x00000001;
382 DF_nRE = 0x00000001;
383 DF_IO0 = 0x00000001;
384 DF_IO8 = 0x00000001;
385 DF_IO1 = 0x00000001;
386 DF_IO9 = 0x00000001;
387 DF_IO2 = 0x00000001;
388 DF_IO10 = 0x00000001;
389 DF_IO3 = 0x00000001;
390 DF_IO11 = 0x00000001;
391 DF_IO4 = 0x00000001;
392 DF_IO12 = 0x00000001;
393 DF_IO5 = 0x00000001;
394 DF_IO13 = 0x00000001;
395 DF_IO6 = 0x00000001;
396 DF_IO14 = 0x00000001;
397 DF_IO7 = 0x00000001;
398 DF_IO15 = 0x00000001;
399
400 DF_nWE = 0x1901;
401 DF_nRE = 0x1901;
402 DF_CLE_NOE = 0x1900;
403 DF_ALE_WE1 = 0x1901;
404 DF_INT_RnB = 0x1900;
405}
406
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100407/*
408 * Board-specific NAND initialization. The following members of the
409 * argument are board-specific (per include/linux/mtd/nand_new.h):
410 * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
411 * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
412 * - hwcontrol: hardwarespecific function for accesing control-lines
413 * - dev_ready: hardwarespecific function for accesing device ready/busy line
414 * - enable_hwecc?: function to enable (reset) hardware ecc generator. Must
415 * only be provided if a hardware ECC is available
William Juul52c07962007-10-31 13:53:06 +0100416 * - ecc.mode: mode of ecc, see defines
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100417 * - chip_delay: chip dependent delay for transfering data from array to
418 * read regs (tR)
419 * - options: various chip options. They can partly be set to inform
420 * nand_scan about special functionality. See the defines for further
421 * explanation
422 * Members with a "?" were not set in the merged testing-NAND branch,
423 * so they are not set here either.
424 */
Heiko Schocher3ec43662006-12-21 17:17:02 +0100425int board_nand_init(struct nand_chip *nand)
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100426{
427 unsigned long tCH, tCS, tWH, tWP, tRH, tRP, tRP_high, tR, tWHR, tAR;
428
429 /* set up GPIO Control Registers */
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100430 dfc_gpio_init();
Markus Klotzbücherf14cc262006-02-28 22:51:01 +0100431
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100432 /* turn on the NAND Controller Clock (104 MHz @ D0) */
433 CKENA |= (CKENA_4_NAND | CKENA_9_SMC);
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100434
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200435#undef CONFIG_SYS_TIMING_TIGHT
436#ifndef CONFIG_SYS_TIMING_TIGHT
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100437 tCH = MIN(((unsigned long) (NAND_TIMING_tCH * DFC_CLK_PER_US) + 1),
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100438 DFC_MAX_tCH);
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100439 tCS = MIN(((unsigned long) (NAND_TIMING_tCS * DFC_CLK_PER_US) + 1),
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100440 DFC_MAX_tCS);
441 tWH = MIN(((unsigned long) (NAND_TIMING_tWH * DFC_CLK_PER_US) + 1),
442 DFC_MAX_tWH);
443 tWP = MIN(((unsigned long) (NAND_TIMING_tWP * DFC_CLK_PER_US) + 1),
444 DFC_MAX_tWP);
445 tRH = MIN(((unsigned long) (NAND_TIMING_tRH * DFC_CLK_PER_US) + 1),
446 DFC_MAX_tRH);
447 tRP = MIN(((unsigned long) (NAND_TIMING_tRP * DFC_CLK_PER_US) + 1),
448 DFC_MAX_tRP);
449 tR = MIN(((unsigned long) (NAND_TIMING_tR * DFC_CLK_PER_US) + 1),
450 DFC_MAX_tR);
451 tWHR = MIN(((unsigned long) (NAND_TIMING_tWHR * DFC_CLK_PER_US) + 1),
452 DFC_MAX_tWHR);
453 tAR = MIN(((unsigned long) (NAND_TIMING_tAR * DFC_CLK_PER_US) + 1),
454 DFC_MAX_tAR);
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100455#else /* this is the tight timing */
456
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100457 tCH = MIN(((unsigned long) (NAND_TIMING_tCH * DFC_CLK_PER_US)),
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100458 DFC_MAX_tCH);
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100459 tCS = MIN(((unsigned long) (NAND_TIMING_tCS * DFC_CLK_PER_US)),
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100460 DFC_MAX_tCS);
461 tWH = MIN(((unsigned long) (NAND_TIMING_tWH * DFC_CLK_PER_US)),
462 DFC_MAX_tWH);
463 tWP = MIN(((unsigned long) (NAND_TIMING_tWP * DFC_CLK_PER_US)),
464 DFC_MAX_tWP);
465 tRH = MIN(((unsigned long) (NAND_TIMING_tRH * DFC_CLK_PER_US)),
466 DFC_MAX_tRH);
467 tRP = MIN(((unsigned long) (NAND_TIMING_tRP * DFC_CLK_PER_US)),
468 DFC_MAX_tRP);
469 tR = MIN(((unsigned long) (NAND_TIMING_tR * DFC_CLK_PER_US) - tCH - 2),
470 DFC_MAX_tR);
471 tWHR = MIN(((unsigned long) (NAND_TIMING_tWHR * DFC_CLK_PER_US) - tCH - 2),
472 DFC_MAX_tWHR);
473 tAR = MIN(((unsigned long) (NAND_TIMING_tAR * DFC_CLK_PER_US) - 2),
474 DFC_MAX_tAR);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200475#endif /* CONFIG_SYS_TIMING_TIGHT */
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100476
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100477
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100478 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ücher432a7b42006-03-01 23:33:27 +0100479
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100480 /* tRP value is split in the register */
481 if(tRP & (1 << 4)) {
482 tRP_high = 1;
483 tRP &= ~(1 << 4);
484 } else {
485 tRP_high = 0;
486 }
487
488 NDTR0CS0 = (tCH << 19) |
489 (tCS << 16) |
490 (tWH << 11) |
491 (tWP << 8) |
492 (tRP_high << 6) |
493 (tRH << 3) |
494 (tRP << 0);
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100495
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100496 NDTR1CS0 = (tR << 16) |
497 (tWHR << 4) |
498 (tAR << 0);
499
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100500 /* If it doesn't work (unlikely) think about:
501 * - ecc enable
502 * - chip select don't care
503 * - read id byte count
504 *
505 * Intentionally enabled by not setting bits:
506 * - dma (DMA_EN)
507 * - page size = 512
508 * - cs don't care, see if we can enable later!
509 * - row address start position (after second cycle)
510 * - pages per block = 32
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100511 * - ND_RDY : clears command buffer
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100512 */
Markus Klotzbücherb2fc71d2006-03-03 20:13:43 +0100513 /* NDCR_NCSX | /\* Chip select busy don't care *\/ */
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100514
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100515 NDCR = (NDCR_SPARE_EN | /* use the spare area */
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100516 NDCR_DWIDTH_C | /* 16bit DFC data bus width */
517 NDCR_DWIDTH_M | /* 16 bit Flash device data bus width */
Markus Klotzbücher85678e22006-03-06 13:45:42 +0100518 (2 << 16) | /* read id count = 7 ???? mk@tbd */
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100519 NDCR_ND_ARB_EN | /* enable bus arbiter */
520 NDCR_RDYM | /* flash device ready ir masked */
521 NDCR_CS0_PAGEDM | /* ND_nCSx page done ir masked */
522 NDCR_CS1_PAGEDM |
523 NDCR_CS0_CMDDM | /* ND_CSx command done ir masked */
524 NDCR_CS1_CMDDM |
525 NDCR_CS0_BBDM | /* ND_CSx bad block detect ir masked */
526 NDCR_CS1_BBDM |
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100527 NDCR_DBERRM | /* double bit error ir masked */
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100528 NDCR_SBERRM | /* single bit error ir masked */
529 NDCR_WRDREQM | /* write data request ir masked */
530 NDCR_RDDREQM | /* read data request ir masked */
531 NDCR_WRCMDREQM); /* write command request ir masked */
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100532
Markus Klotzbücher432a7b42006-03-01 23:33:27 +0100533
534 /* wait 10 us due to cmd buffer clear reset */
Markus Klotzbuecher5a10caa2006-03-20 20:19:37 +0100535 /* wait(10); */
Wolfgang Denk61ccd1d2006-03-06 23:18:48 +0100536
537
William Juul52c07962007-10-31 13:53:06 +0100538 nand->cmd_ctrl = dfc_hwcontrol;
Markus Klotzbuecher5a10caa2006-03-20 20:19:37 +0100539/* nand->dev_ready = dfc_device_ready; */
William Juul52c07962007-10-31 13:53:06 +0100540 nand->ecc.mode = NAND_ECC_SOFT;
Scott Wood08cb8b92008-09-10 11:48:49 -0500541 nand->ecc.layout = &delta_oob;
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100542 nand->options = NAND_BUSWIDTH_16;
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100543 nand->waitfunc = dfc_wait;
544 nand->read_byte = dfc_read_byte;
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100545 nand->read_word = dfc_read_word;
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100546 nand->read_buf = dfc_read_buf;
547 nand->write_buf = dfc_write_buf;
Markus Klotzbücherb6c2d402006-03-03 15:37:01 +0100548
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100549 nand->cmdfunc = dfc_cmdfunc;
Markus Klotzbücher21a43f92006-03-04 18:35:51 +0100550 nand->badblock_pattern = &delta_bbt_descr;
Heiko Schocher3ec43662006-12-21 17:17:02 +0100551 return 0;
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100552}
553
Markus Klotzbücherf4a5c612006-02-28 18:05:25 +0100554#endif