blob: 5b2ae88d93b84242edc6910f1fcf702d908cd93d [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) || \
Stefan Roesecc019d12008-03-11 15:05:50 +010037 defined(CONFIG_405EZ) || defined(CONFIG_405EX) || \
38 defined(CONFIG_460EX) || defined(CONFIG_460GT))
Stefan Roese42fbddd2006-09-07 11:51:23 +020039
40#include <nand.h>
41#include <linux/mtd/ndfc.h>
Stefan Roese1eb122a2007-06-01 15:15:12 +020042#include <linux/mtd/nand_ecc.h>
Stefan Roese42fbddd2006-09-07 11:51:23 +020043#include <asm/processor.h>
Stefan Roese1eb122a2007-06-01 15:15:12 +020044#include <asm/io.h>
Stefan Roesecd2308d2007-05-22 12:46:10 +020045#include <ppc4xx.h>
Stefan Roese42fbddd2006-09-07 11:51:23 +020046
47static u8 hwctl = 0;
48
49static void ndfc_hwcontrol(struct mtd_info *mtdinfo, int cmd)
50{
51 switch (cmd) {
52 case NAND_CTL_SETCLE:
53 hwctl |= 0x1;
54 break;
55
56 case NAND_CTL_CLRCLE:
57 hwctl &= ~0x1;
58 break;
59
60 case NAND_CTL_SETALE:
61 hwctl |= 0x2;
62 break;
63
64 case NAND_CTL_CLRALE:
65 hwctl &= ~0x2;
66 break;
67 }
68}
69
70static void ndfc_write_byte(struct mtd_info *mtdinfo, u_char byte)
71{
Wolfgang Denk4df0da52006-10-09 00:42:01 +020072 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +020073 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese42fbddd2006-09-07 11:51:23 +020074
75 if (hwctl & 0x1)
Stefan Roese1eb122a2007-06-01 15:15:12 +020076 out_8((u8 *)(base + NDFC_CMD), byte);
Stefan Roese42fbddd2006-09-07 11:51:23 +020077 else if (hwctl & 0x2)
Stefan Roese1eb122a2007-06-01 15:15:12 +020078 out_8((u8 *)(base + NDFC_ALE), byte);
Stefan Roese42fbddd2006-09-07 11:51:23 +020079 else
Stefan Roese1eb122a2007-06-01 15:15:12 +020080 out_8((u8 *)(base + NDFC_DATA), byte);
Stefan Roese42fbddd2006-09-07 11:51:23 +020081}
82
83static u_char ndfc_read_byte(struct mtd_info *mtdinfo)
84{
Wolfgang Denk4df0da52006-10-09 00:42:01 +020085 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +020086 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese42fbddd2006-09-07 11:51:23 +020087
Stefan Roese1eb122a2007-06-01 15:15:12 +020088 return (in_8((u8 *)(base + NDFC_DATA)));
Stefan Roese42fbddd2006-09-07 11:51:23 +020089}
90
91static int ndfc_dev_ready(struct mtd_info *mtdinfo)
92{
Wolfgang Denk4df0da52006-10-09 00:42:01 +020093 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +020094 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese42fbddd2006-09-07 11:51:23 +020095
Stefan Roese1eb122a2007-06-01 15:15:12 +020096 while (!(in_be32((u32 *)(base + NDFC_STAT)) & NDFC_STAT_IS_READY))
Stefan Roese42fbddd2006-09-07 11:51:23 +020097 ;
98
99 return 1;
100}
101
Stefan Roese1eb122a2007-06-01 15:15:12 +0200102static void ndfc_enable_hwecc(struct mtd_info *mtdinfo, int mode)
103{
104 struct nand_chip *this = mtdinfo->priv;
105 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
106 u32 ccr;
107
108 ccr = in_be32((u32 *)(base + NDFC_CCR));
109 ccr |= NDFC_CCR_RESET_ECC;
110 out_be32((u32 *)(base + NDFC_CCR), ccr);
111}
112
113static int ndfc_calculate_ecc(struct mtd_info *mtdinfo,
114 const u_char *dat, u_char *ecc_code)
115{
116 struct nand_chip *this = mtdinfo->priv;
117 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
118 u32 ecc;
119 u8 *p = (u8 *)&ecc;
120
121 ecc = in_be32((u32 *)(base + NDFC_ECC));
122
123 /* The NDFC uses Smart Media (SMC) bytes order
124 */
Stefan Roese41b7f6c2008-02-01 09:38:29 +0100125 ecc_code[0] = p[1];
126 ecc_code[1] = p[2];
Stefan Roese1eb122a2007-06-01 15:15:12 +0200127 ecc_code[2] = p[3];
128
129 return 0;
130}
Stefan Roese42fbddd2006-09-07 11:51:23 +0200131
132/*
133 * Speedups for buffer read/write/verify
134 *
135 * NDFC allows 32bit read/write of data. So we can speed up the buffer
136 * functions. No further checking, as nand_base will always read/write
137 * page aligned.
138 */
139static void ndfc_read_buf(struct mtd_info *mtdinfo, uint8_t *buf, int len)
140{
Wolfgang Denk4df0da52006-10-09 00:42:01 +0200141 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200142 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200143 uint32_t *p = (uint32_t *) buf;
144
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200145 for (;len > 0; len -= 4)
Stefan Roese1eb122a2007-06-01 15:15:12 +0200146 *p++ = in_be32((u32 *)(base + NDFC_DATA));
Stefan Roese42fbddd2006-09-07 11:51:23 +0200147}
148
Stefan Roese1eb122a2007-06-01 15:15:12 +0200149#ifndef CONFIG_NAND_SPL
150/*
151 * Don't use these speedup functions in NAND boot image, since the image
152 * has to fit into 4kByte.
153 */
Stefan Roese42fbddd2006-09-07 11:51:23 +0200154static void ndfc_write_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
155{
Wolfgang Denk4df0da52006-10-09 00:42:01 +0200156 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200157 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200158 uint32_t *p = (uint32_t *) buf;
159
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200160 for (; len > 0; len -= 4)
Stefan Roese1eb122a2007-06-01 15:15:12 +0200161 out_be32((u32 *)(base + NDFC_DATA), *p++);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200162}
163
164static int ndfc_verify_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
165{
Wolfgang Denk4df0da52006-10-09 00:42:01 +0200166 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200167 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200168 uint32_t *p = (uint32_t *) buf;
169
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200170 for (; len > 0; len -= 4)
Stefan Roese1eb122a2007-06-01 15:15:12 +0200171 if (*p++ != in_be32((u32 *)(base + NDFC_DATA)))
Stefan Roese42fbddd2006-09-07 11:51:23 +0200172 return -1;
173
174 return 0;
175}
176#endif /* #ifndef CONFIG_NAND_SPL */
177
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200178void board_nand_select_device(struct nand_chip *nand, int chip)
179{
Stefan Roesed07e5722006-10-24 18:06:48 +0200180 /*
181 * Don't use "chip" to address the NAND device,
182 * generate the cs from the address where it is encoded.
183 */
184 int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200185 ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc;
186
187 /* Set NandFlash Core Configuration Register */
Stefan Roese1eb122a2007-06-01 15:15:12 +0200188 /* 1 col x 2 rows */
189 out_be32((u32 *)(base + NDFC_CCR), 0x00000000 | (cs << 24));
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200190}
191
Heiko Schocher3ec43662006-12-21 17:17:02 +0100192int board_nand_init(struct nand_chip *nand)
Stefan Roese42fbddd2006-09-07 11:51:23 +0200193{
Stefan Roesed07e5722006-10-24 18:06:48 +0200194 int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200195 ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc;
196
Stefan Roese42fbddd2006-09-07 11:51:23 +0200197 nand->hwcontrol = ndfc_hwcontrol;
198 nand->read_byte = ndfc_read_byte;
Stefan Roese1eb122a2007-06-01 15:15:12 +0200199 nand->read_buf = ndfc_read_buf;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200200 nand->write_byte = ndfc_write_byte;
201 nand->dev_ready = ndfc_dev_ready;
202
Stefan Roese1eb122a2007-06-01 15:15:12 +0200203 nand->eccmode = NAND_ECC_HW3_256;
204 nand->enable_hwecc = ndfc_enable_hwecc;
205 nand->calculate_ecc = ndfc_calculate_ecc;
206 nand->correct_data = nand_correct_data;
207
Stefan Roese42fbddd2006-09-07 11:51:23 +0200208#ifndef CONFIG_NAND_SPL
209 nand->write_buf = ndfc_write_buf;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200210 nand->verify_buf = ndfc_verify_buf;
211#else
212 /*
213 * Setup EBC (CS0 only right now)
214 */
Stefan Roesecd2308d2007-05-22 12:46:10 +0200215 mtebc(EBC0_CFG, 0xb8400000);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200216
217 mtebc(pb0cr, CFG_EBC_PB0CR);
218 mtebc(pb0ap, CFG_EBC_PB0AP);
219#endif
220
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200221 /*
222 * Select required NAND chip in NDFC
223 */
Stefan Roesed07e5722006-10-24 18:06:48 +0200224 board_nand_select_device(nand, cs);
Stefan Roese1eb122a2007-06-01 15:15:12 +0200225 out_be32((u32 *)(base + NDFC_BCFG0 + (cs << 2)), 0x80002222);
Stefan Roese153b3e22007-10-05 17:10:59 +0200226
Heiko Schocher3ec43662006-12-21 17:17:02 +0100227 return 0;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200228}
229
230#endif