blob: 398457726f426b3898ff794b6ebcd1018eaf3f76 [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
Jon Loeligera5217742007-07-09 18:57:22 -050034#if defined(CONFIG_CMD_NAND) && !defined(CFG_NAND_LEGACY) && \
Stefan Roese6e7cd7c2006-09-07 13:09:53 +020035 (defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
Stefan Roesecd2308d2007-05-22 12:46:10 +020036 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
37 defined(CONFIG_405EZ))
Stefan Roese42fbddd2006-09-07 11:51:23 +020038
39#include <nand.h>
40#include <linux/mtd/ndfc.h>
Stefan Roese1eb122a2007-06-01 15:15:12 +020041#include <linux/mtd/nand_ecc.h>
Stefan Roese42fbddd2006-09-07 11:51:23 +020042#include <asm/processor.h>
Stefan Roese1eb122a2007-06-01 15:15:12 +020043#include <asm/io.h>
Stefan Roesecd2308d2007-05-22 12:46:10 +020044#include <ppc4xx.h>
Stefan Roese42fbddd2006-09-07 11:51:23 +020045
46static u8 hwctl = 0;
47
48static void ndfc_hwcontrol(struct mtd_info *mtdinfo, int cmd)
49{
50 switch (cmd) {
51 case NAND_CTL_SETCLE:
52 hwctl |= 0x1;
53 break;
54
55 case NAND_CTL_CLRCLE:
56 hwctl &= ~0x1;
57 break;
58
59 case NAND_CTL_SETALE:
60 hwctl |= 0x2;
61 break;
62
63 case NAND_CTL_CLRALE:
64 hwctl &= ~0x2;
65 break;
66 }
67}
68
69static void ndfc_write_byte(struct mtd_info *mtdinfo, u_char byte)
70{
Wolfgang Denk4df0da52006-10-09 00:42:01 +020071 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +020072 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese42fbddd2006-09-07 11:51:23 +020073
74 if (hwctl & 0x1)
Stefan Roese1eb122a2007-06-01 15:15:12 +020075 out_8((u8 *)(base + NDFC_CMD), byte);
Stefan Roese42fbddd2006-09-07 11:51:23 +020076 else if (hwctl & 0x2)
Stefan Roese1eb122a2007-06-01 15:15:12 +020077 out_8((u8 *)(base + NDFC_ALE), byte);
Stefan Roese42fbddd2006-09-07 11:51:23 +020078 else
Stefan Roese1eb122a2007-06-01 15:15:12 +020079 out_8((u8 *)(base + NDFC_DATA), byte);
Stefan Roese42fbddd2006-09-07 11:51:23 +020080}
81
82static u_char ndfc_read_byte(struct mtd_info *mtdinfo)
83{
Wolfgang Denk4df0da52006-10-09 00:42:01 +020084 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +020085 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese42fbddd2006-09-07 11:51:23 +020086
Stefan Roese1eb122a2007-06-01 15:15:12 +020087 return (in_8((u8 *)(base + NDFC_DATA)));
Stefan Roese42fbddd2006-09-07 11:51:23 +020088}
89
90static int ndfc_dev_ready(struct mtd_info *mtdinfo)
91{
Wolfgang Denk4df0da52006-10-09 00:42:01 +020092 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +020093 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese42fbddd2006-09-07 11:51:23 +020094
Stefan Roese1eb122a2007-06-01 15:15:12 +020095 while (!(in_be32((u32 *)(base + NDFC_STAT)) & NDFC_STAT_IS_READY))
Stefan Roese42fbddd2006-09-07 11:51:23 +020096 ;
97
98 return 1;
99}
100
Stefan Roese1eb122a2007-06-01 15:15:12 +0200101static void ndfc_enable_hwecc(struct mtd_info *mtdinfo, int mode)
102{
103 struct nand_chip *this = mtdinfo->priv;
104 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
105 u32 ccr;
106
107 ccr = in_be32((u32 *)(base + NDFC_CCR));
108 ccr |= NDFC_CCR_RESET_ECC;
109 out_be32((u32 *)(base + NDFC_CCR), ccr);
110}
111
112static int ndfc_calculate_ecc(struct mtd_info *mtdinfo,
113 const u_char *dat, u_char *ecc_code)
114{
115 struct nand_chip *this = mtdinfo->priv;
116 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
117 u32 ecc;
118 u8 *p = (u8 *)&ecc;
119
120 ecc = in_be32((u32 *)(base + NDFC_ECC));
121
122 /* The NDFC uses Smart Media (SMC) bytes order
123 */
124 ecc_code[0] = p[2];
125 ecc_code[1] = p[1];
126 ecc_code[2] = p[3];
127
128 return 0;
129}
Stefan Roese42fbddd2006-09-07 11:51:23 +0200130
131/*
132 * Speedups for buffer read/write/verify
133 *
134 * NDFC allows 32bit read/write of data. So we can speed up the buffer
135 * functions. No further checking, as nand_base will always read/write
136 * page aligned.
137 */
138static void ndfc_read_buf(struct mtd_info *mtdinfo, uint8_t *buf, int len)
139{
Wolfgang Denk4df0da52006-10-09 00:42:01 +0200140 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200141 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200142 uint32_t *p = (uint32_t *) buf;
143
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200144 for (;len > 0; len -= 4)
Stefan Roese1eb122a2007-06-01 15:15:12 +0200145 *p++ = in_be32((u32 *)(base + NDFC_DATA));
Stefan Roese42fbddd2006-09-07 11:51:23 +0200146}
147
Stefan Roese1eb122a2007-06-01 15:15:12 +0200148#ifndef CONFIG_NAND_SPL
149/*
150 * Don't use these speedup functions in NAND boot image, since the image
151 * has to fit into 4kByte.
152 */
Stefan Roese42fbddd2006-09-07 11:51:23 +0200153static void ndfc_write_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
154{
Wolfgang Denk4df0da52006-10-09 00:42:01 +0200155 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200156 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200157 uint32_t *p = (uint32_t *) buf;
158
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200159 for (; len > 0; len -= 4)
Stefan Roese1eb122a2007-06-01 15:15:12 +0200160 out_be32((u32 *)(base + NDFC_DATA), *p++);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200161}
162
163static int ndfc_verify_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
164{
Wolfgang Denk4df0da52006-10-09 00:42:01 +0200165 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200166 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200167 uint32_t *p = (uint32_t *) buf;
168
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200169 for (; len > 0; len -= 4)
Stefan Roese1eb122a2007-06-01 15:15:12 +0200170 if (*p++ != in_be32((u32 *)(base + NDFC_DATA)))
Stefan Roese42fbddd2006-09-07 11:51:23 +0200171 return -1;
172
173 return 0;
174}
175#endif /* #ifndef CONFIG_NAND_SPL */
176
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200177void board_nand_select_device(struct nand_chip *nand, int chip)
178{
Stefan Roesed07e5722006-10-24 18:06:48 +0200179 /*
180 * Don't use "chip" to address the NAND device,
181 * generate the cs from the address where it is encoded.
182 */
183 int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200184 ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc;
185
186 /* Set NandFlash Core Configuration Register */
Stefan Roese1eb122a2007-06-01 15:15:12 +0200187 /* 1 col x 2 rows */
188 out_be32((u32 *)(base + NDFC_CCR), 0x00000000 | (cs << 24));
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200189}
190
Heiko Schocher3ec43662006-12-21 17:17:02 +0100191int board_nand_init(struct nand_chip *nand)
Stefan Roese42fbddd2006-09-07 11:51:23 +0200192{
Stefan Roesed07e5722006-10-24 18:06:48 +0200193 int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200194 ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc;
195
Stefan Roese42fbddd2006-09-07 11:51:23 +0200196 nand->hwcontrol = ndfc_hwcontrol;
197 nand->read_byte = ndfc_read_byte;
Stefan Roese1eb122a2007-06-01 15:15:12 +0200198 nand->read_buf = ndfc_read_buf;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200199 nand->write_byte = ndfc_write_byte;
200 nand->dev_ready = ndfc_dev_ready;
201
Stefan Roese1eb122a2007-06-01 15:15:12 +0200202 nand->eccmode = NAND_ECC_HW3_256;
203 nand->enable_hwecc = ndfc_enable_hwecc;
204 nand->calculate_ecc = ndfc_calculate_ecc;
205 nand->correct_data = nand_correct_data;
206
Stefan Roese42fbddd2006-09-07 11:51:23 +0200207#ifndef CONFIG_NAND_SPL
208 nand->write_buf = ndfc_write_buf;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200209 nand->verify_buf = ndfc_verify_buf;
210#else
211 /*
212 * Setup EBC (CS0 only right now)
213 */
Stefan Roesecd2308d2007-05-22 12:46:10 +0200214 mtebc(EBC0_CFG, 0xb8400000);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200215
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