blob: 09aac38f488e6a61f8c34b0552cf23175ec66a1a [file] [log] [blame]
Stefan Roese42fbddd2006-09-07 11:51:23 +02001/*
2 * Overview:
3 * Platform independend driver for NDFC (NanD Flash Controller)
4 * integrated into EP440 cores
5 *
Stefan Roese1eb122a2007-06-01 15:15:12 +02006 * (C) Copyright 2006-2007
Stefan Roese42fbddd2006-09-07 11:51:23 +02007 * Stefan Roese, DENX Software Engineering, sr@denx.de.
8 *
9 * Based on original work by
10 * Thomas Gleixner
11 * Copyright 2006 IBM
12 *
13 * See file CREDITS for list of people who contributed to this
14 * project.
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * MA 02111-1307 USA
30 */
31
32#include <common.h>
33
Stefan Roese6e7cd7c2006-09-07 13:09:53 +020034#if (CONFIG_COMMANDS & CFG_CMD_NAND) && !defined(CFG_NAND_LEGACY) && \
35 (defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
36 defined(CONFIG_440EPX) || defined(CONFIG_440GRX))
Stefan Roese42fbddd2006-09-07 11:51:23 +020037
38#include <nand.h>
39#include <linux/mtd/ndfc.h>
Stefan Roese1eb122a2007-06-01 15:15:12 +020040#include <linux/mtd/nand_ecc.h>
Stefan Roese42fbddd2006-09-07 11:51:23 +020041#include <asm/processor.h>
Stefan Roese1eb122a2007-06-01 15:15:12 +020042#include <asm/io.h>
Stefan Roese42fbddd2006-09-07 11:51:23 +020043#include <ppc440.h>
44
45static u8 hwctl = 0;
46
47static void ndfc_hwcontrol(struct mtd_info *mtdinfo, int cmd)
48{
49 switch (cmd) {
50 case NAND_CTL_SETCLE:
51 hwctl |= 0x1;
52 break;
53
54 case NAND_CTL_CLRCLE:
55 hwctl &= ~0x1;
56 break;
57
58 case NAND_CTL_SETALE:
59 hwctl |= 0x2;
60 break;
61
62 case NAND_CTL_CLRALE:
63 hwctl &= ~0x2;
64 break;
65 }
66}
67
68static void ndfc_write_byte(struct mtd_info *mtdinfo, u_char byte)
69{
Wolfgang Denk4df0da52006-10-09 00:42:01 +020070 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +020071 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese42fbddd2006-09-07 11:51:23 +020072
73 if (hwctl & 0x1)
Stefan Roese1eb122a2007-06-01 15:15:12 +020074 out_8((u8 *)(base + NDFC_CMD), byte);
Stefan Roese42fbddd2006-09-07 11:51:23 +020075 else if (hwctl & 0x2)
Stefan Roese1eb122a2007-06-01 15:15:12 +020076 out_8((u8 *)(base + NDFC_ALE), byte);
Stefan Roese42fbddd2006-09-07 11:51:23 +020077 else
Stefan Roese1eb122a2007-06-01 15:15:12 +020078 out_8((u8 *)(base + NDFC_DATA), byte);
Stefan Roese42fbddd2006-09-07 11:51:23 +020079}
80
81static u_char ndfc_read_byte(struct mtd_info *mtdinfo)
82{
Wolfgang Denk4df0da52006-10-09 00:42:01 +020083 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +020084 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese42fbddd2006-09-07 11:51:23 +020085
Stefan Roese1eb122a2007-06-01 15:15:12 +020086 return (in_8((u8 *)(base + NDFC_DATA)));
Stefan Roese42fbddd2006-09-07 11:51:23 +020087}
88
89static int ndfc_dev_ready(struct mtd_info *mtdinfo)
90{
Wolfgang Denk4df0da52006-10-09 00:42:01 +020091 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +020092 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese42fbddd2006-09-07 11:51:23 +020093
Stefan Roese1eb122a2007-06-01 15:15:12 +020094 while (!(in_be32((u32 *)(base + NDFC_STAT)) & NDFC_STAT_IS_READY))
Stefan Roese42fbddd2006-09-07 11:51:23 +020095 ;
96
97 return 1;
98}
99
Stefan Roese1eb122a2007-06-01 15:15:12 +0200100static void ndfc_enable_hwecc(struct mtd_info *mtdinfo, int mode)
101{
102 struct nand_chip *this = mtdinfo->priv;
103 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
104 u32 ccr;
105
106 ccr = in_be32((u32 *)(base + NDFC_CCR));
107 ccr |= NDFC_CCR_RESET_ECC;
108 out_be32((u32 *)(base + NDFC_CCR), ccr);
109}
110
111static int ndfc_calculate_ecc(struct mtd_info *mtdinfo,
112 const u_char *dat, u_char *ecc_code)
113{
114 struct nand_chip *this = mtdinfo->priv;
115 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
116 u32 ecc;
117 u8 *p = (u8 *)&ecc;
118
119 ecc = in_be32((u32 *)(base + NDFC_ECC));
120
121 /* The NDFC uses Smart Media (SMC) bytes order
122 */
123 ecc_code[0] = p[2];
124 ecc_code[1] = p[1];
125 ecc_code[2] = p[3];
126
127 return 0;
128}
Stefan Roese42fbddd2006-09-07 11:51:23 +0200129
130/*
131 * Speedups for buffer read/write/verify
132 *
133 * NDFC allows 32bit read/write of data. So we can speed up the buffer
134 * functions. No further checking, as nand_base will always read/write
135 * page aligned.
136 */
137static void ndfc_read_buf(struct mtd_info *mtdinfo, uint8_t *buf, int len)
138{
Wolfgang Denk4df0da52006-10-09 00:42:01 +0200139 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200140 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200141 uint32_t *p = (uint32_t *) buf;
142
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200143 for (;len > 0; len -= 4)
Stefan Roese1eb122a2007-06-01 15:15:12 +0200144 *p++ = in_be32((u32 *)(base + NDFC_DATA));
Stefan Roese42fbddd2006-09-07 11:51:23 +0200145}
146
Stefan Roese1eb122a2007-06-01 15:15:12 +0200147#ifndef CONFIG_NAND_SPL
148/*
149 * Don't use these speedup functions in NAND boot image, since the image
150 * has to fit into 4kByte.
151 */
Stefan Roese42fbddd2006-09-07 11:51:23 +0200152static void ndfc_write_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
153{
Wolfgang Denk4df0da52006-10-09 00:42:01 +0200154 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200155 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200156 uint32_t *p = (uint32_t *) buf;
157
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200158 for (; len > 0; len -= 4)
Stefan Roese1eb122a2007-06-01 15:15:12 +0200159 out_be32((u32 *)(base + NDFC_DATA), *p++);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200160}
161
162static int ndfc_verify_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
163{
Wolfgang Denk4df0da52006-10-09 00:42:01 +0200164 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200165 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200166 uint32_t *p = (uint32_t *) buf;
167
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200168 for (; len > 0; len -= 4)
Stefan Roese1eb122a2007-06-01 15:15:12 +0200169 if (*p++ != in_be32((u32 *)(base + NDFC_DATA)))
Stefan Roese42fbddd2006-09-07 11:51:23 +0200170 return -1;
171
172 return 0;
173}
174#endif /* #ifndef CONFIG_NAND_SPL */
175
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200176void board_nand_select_device(struct nand_chip *nand, int chip)
177{
Stefan Roesed07e5722006-10-24 18:06:48 +0200178 /*
179 * Don't use "chip" to address the NAND device,
180 * generate the cs from the address where it is encoded.
181 */
182 int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200183 ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc;
184
185 /* Set NandFlash Core Configuration Register */
Stefan Roese1eb122a2007-06-01 15:15:12 +0200186 /* 1 col x 2 rows */
187 out_be32((u32 *)(base + NDFC_CCR), 0x00000000 | (cs << 24));
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200188}
189
Heiko Schocher3ec43662006-12-21 17:17:02 +0100190int board_nand_init(struct nand_chip *nand)
Stefan Roese42fbddd2006-09-07 11:51:23 +0200191{
Stefan Roesed07e5722006-10-24 18:06:48 +0200192 int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200193 ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc;
194
Stefan Roese42fbddd2006-09-07 11:51:23 +0200195 nand->hwcontrol = ndfc_hwcontrol;
196 nand->read_byte = ndfc_read_byte;
Stefan Roese1eb122a2007-06-01 15:15:12 +0200197 nand->read_buf = ndfc_read_buf;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200198 nand->write_byte = ndfc_write_byte;
199 nand->dev_ready = ndfc_dev_ready;
200
Stefan Roese1eb122a2007-06-01 15:15:12 +0200201 nand->eccmode = NAND_ECC_HW3_256;
202 nand->enable_hwecc = ndfc_enable_hwecc;
203 nand->calculate_ecc = ndfc_calculate_ecc;
204 nand->correct_data = nand_correct_data;
205
Stefan Roese42fbddd2006-09-07 11:51:23 +0200206#ifndef CONFIG_NAND_SPL
207 nand->write_buf = ndfc_write_buf;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200208 nand->verify_buf = ndfc_verify_buf;
209#else
210 /*
211 * Setup EBC (CS0 only right now)
212 */
213 mtdcr(ebccfga, xbcfg);
214 mtdcr(ebccfgd, 0xb8400000);
215
216 mtebc(pb0cr, CFG_EBC_PB0CR);
217 mtebc(pb0ap, CFG_EBC_PB0AP);
218#endif
219
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200220 /*
221 * Select required NAND chip in NDFC
222 */
Stefan Roesed07e5722006-10-24 18:06:48 +0200223 board_nand_select_device(nand, cs);
Stefan Roese1eb122a2007-06-01 15:15:12 +0200224 out_be32((u32 *)(base + NDFC_BCFG0 + (cs << 2)), 0x80002222);
Heiko Schocher3ec43662006-12-21 17:17:02 +0100225 return 0;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200226}
227
228#endif