blob: 86548293152022c861c8528dbb51e9a00516b6ba [file] [log] [blame]
Stefan Roese887e2ec2006-09-07 11:51:23 +02001/*
2 * Overview:
3 * Platform independend driver for NDFC (NanD Flash Controller)
4 * integrated into EP440 cores
5 *
Stefan Roese91da09c2007-06-01 15:15:12 +02006 * (C) Copyright 2006-2007
Stefan Roese887e2ec2006-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 Loeliger3a1ed1e2007-07-09 18:57:22 -050034#if defined(CONFIG_CMD_NAND) && !defined(CFG_NAND_LEGACY) && \
Stefan Roese2d658962006-09-07 13:09:53 +020035 (defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
Stefan Roese6f3dfc12007-05-22 12:46:10 +020036 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese2801b2d2008-03-11 15:05:50 +010037 defined(CONFIG_405EZ) || defined(CONFIG_405EX) || \
38 defined(CONFIG_460EX) || defined(CONFIG_460GT))
Stefan Roese887e2ec2006-09-07 11:51:23 +020039
40#include <nand.h>
41#include <linux/mtd/ndfc.h>
Stefan Roese91da09c2007-06-01 15:15:12 +020042#include <linux/mtd/nand_ecc.h>
Stefan Roese887e2ec2006-09-07 11:51:23 +020043#include <asm/processor.h>
Stefan Roese91da09c2007-06-01 15:15:12 +020044#include <asm/io.h>
Stefan Roese6f3dfc12007-05-22 12:46:10 +020045#include <ppc4xx.h>
Stefan Roese887e2ec2006-09-07 11:51:23 +020046
47static u8 hwctl = 0;
48
William Juulcfa460a2007-10-31 13:53:06 +010049static void ndfc_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Stefan Roese887e2ec2006-09-07 11:51:23 +020050{
William Juul5e1dae52007-11-09 13:32:30 +010051 struct nand_chip *this = mtd->priv;
Stefan Roese887e2ec2006-09-07 11:51:23 +020052
William Juulcfa460a2007-10-31 13:53:06 +010053 if (ctrl & NAND_CTRL_CHANGE) {
54 if ( ctrl & NAND_CLE )
55 hwctl |= 0x1;
56 else
57 hwctl &= ~0x1;
58 if ( ctrl & NAND_ALE )
59 hwctl |= 0x2;
60 else
61 hwctl &= ~0x2;
Stefan Roese887e2ec2006-09-07 11:51:23 +020062 }
William Juulcfa460a2007-10-31 13:53:06 +010063 if (cmd != NAND_CMD_NONE)
64 writeb(cmd, this->IO_ADDR_W);
Stefan Roese887e2ec2006-09-07 11:51:23 +020065}
66
67static u_char ndfc_read_byte(struct mtd_info *mtdinfo)
68{
Wolfgang Denk511d0c72006-10-09 00:42:01 +020069 struct nand_chip *this = mtdinfo->priv;
Stefan Roese43a2b0e2006-10-20 14:28:52 +020070 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese887e2ec2006-09-07 11:51:23 +020071
Stefan Roese91da09c2007-06-01 15:15:12 +020072 return (in_8((u8 *)(base + NDFC_DATA)));
Stefan Roese887e2ec2006-09-07 11:51:23 +020073}
74
75static int ndfc_dev_ready(struct mtd_info *mtdinfo)
76{
Wolfgang Denk511d0c72006-10-09 00:42:01 +020077 struct nand_chip *this = mtdinfo->priv;
Stefan Roese43a2b0e2006-10-20 14:28:52 +020078 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese887e2ec2006-09-07 11:51:23 +020079
Stefan Roese91da09c2007-06-01 15:15:12 +020080 while (!(in_be32((u32 *)(base + NDFC_STAT)) & NDFC_STAT_IS_READY))
Stefan Roese887e2ec2006-09-07 11:51:23 +020081 ;
82
83 return 1;
84}
85
Stefan Roese91da09c2007-06-01 15:15:12 +020086static void ndfc_enable_hwecc(struct mtd_info *mtdinfo, int mode)
87{
88 struct nand_chip *this = mtdinfo->priv;
89 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
90 u32 ccr;
91
92 ccr = in_be32((u32 *)(base + NDFC_CCR));
93 ccr |= NDFC_CCR_RESET_ECC;
94 out_be32((u32 *)(base + NDFC_CCR), ccr);
95}
96
97static int ndfc_calculate_ecc(struct mtd_info *mtdinfo,
98 const u_char *dat, u_char *ecc_code)
99{
100 struct nand_chip *this = mtdinfo->priv;
101 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
102 u32 ecc;
103 u8 *p = (u8 *)&ecc;
104
105 ecc = in_be32((u32 *)(base + NDFC_ECC));
106
107 /* The NDFC uses Smart Media (SMC) bytes order
108 */
Stefan Roeseff02f132008-02-01 09:38:29 +0100109 ecc_code[0] = p[1];
110 ecc_code[1] = p[2];
Stefan Roese91da09c2007-06-01 15:15:12 +0200111 ecc_code[2] = p[3];
112
113 return 0;
114}
Stefan Roese887e2ec2006-09-07 11:51:23 +0200115
116/*
117 * Speedups for buffer read/write/verify
118 *
119 * NDFC allows 32bit read/write of data. So we can speed up the buffer
120 * functions. No further checking, as nand_base will always read/write
121 * page aligned.
122 */
123static void ndfc_read_buf(struct mtd_info *mtdinfo, uint8_t *buf, int len)
124{
Wolfgang Denk511d0c72006-10-09 00:42:01 +0200125 struct nand_chip *this = mtdinfo->priv;
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200126 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200127 uint32_t *p = (uint32_t *) buf;
128
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200129 for (;len > 0; len -= 4)
Stefan Roese91da09c2007-06-01 15:15:12 +0200130 *p++ = in_be32((u32 *)(base + NDFC_DATA));
Stefan Roese887e2ec2006-09-07 11:51:23 +0200131}
132
Stefan Roese91da09c2007-06-01 15:15:12 +0200133#ifndef CONFIG_NAND_SPL
134/*
135 * Don't use these speedup functions in NAND boot image, since the image
136 * has to fit into 4kByte.
137 */
Stefan Roese887e2ec2006-09-07 11:51:23 +0200138static void ndfc_write_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
139{
Wolfgang Denk511d0c72006-10-09 00:42:01 +0200140 struct nand_chip *this = mtdinfo->priv;
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200141 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200142 uint32_t *p = (uint32_t *) buf;
143
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200144 for (; len > 0; len -= 4)
Stefan Roese91da09c2007-06-01 15:15:12 +0200145 out_be32((u32 *)(base + NDFC_DATA), *p++);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200146}
147
148static int ndfc_verify_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
149{
Wolfgang Denk511d0c72006-10-09 00:42:01 +0200150 struct nand_chip *this = mtdinfo->priv;
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200151 ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200152 uint32_t *p = (uint32_t *) buf;
153
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200154 for (; len > 0; len -= 4)
Stefan Roese91da09c2007-06-01 15:15:12 +0200155 if (*p++ != in_be32((u32 *)(base + NDFC_DATA)))
Stefan Roese887e2ec2006-09-07 11:51:23 +0200156 return -1;
157
158 return 0;
159}
160#endif /* #ifndef CONFIG_NAND_SPL */
161
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200162void board_nand_select_device(struct nand_chip *nand, int chip)
163{
Stefan Roese7ade0c62006-10-24 18:06:48 +0200164 /*
165 * Don't use "chip" to address the NAND device,
166 * generate the cs from the address where it is encoded.
167 */
168 int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200169 ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc;
170
171 /* Set NandFlash Core Configuration Register */
Stefan Roese91da09c2007-06-01 15:15:12 +0200172 /* 1 col x 2 rows */
173 out_be32((u32 *)(base + NDFC_CCR), 0x00000000 | (cs << 24));
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200174}
175
Heiko Schocherfa230442006-12-21 17:17:02 +0100176int board_nand_init(struct nand_chip *nand)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200177{
Stefan Roese7ade0c62006-10-24 18:06:48 +0200178 int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200179 ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc;
180
William Juulcfa460a2007-10-31 13:53:06 +0100181 nand->cmd_ctrl = ndfc_hwcontrol;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200182 nand->read_byte = ndfc_read_byte;
Stefan Roese91da09c2007-06-01 15:15:12 +0200183 nand->read_buf = ndfc_read_buf;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200184 nand->dev_ready = ndfc_dev_ready;
185
William Juul5e1dae52007-11-09 13:32:30 +0100186 nand->ecc.correct = nand_correct_data;
187 nand->ecc.hwctl = ndfc_enable_hwecc;
188 nand->ecc.calculate = ndfc_calculate_ecc;
189 nand->ecc.mode = NAND_ECC_HW;
190 nand->ecc.size = 256;
191 nand->ecc.bytes = 3;
Stefan Roese91da09c2007-06-01 15:15:12 +0200192
Stefan Roese887e2ec2006-09-07 11:51:23 +0200193#ifndef CONFIG_NAND_SPL
194 nand->write_buf = ndfc_write_buf;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200195 nand->verify_buf = ndfc_verify_buf;
196#else
197 /*
198 * Setup EBC (CS0 only right now)
199 */
Stefan Roese6f3dfc12007-05-22 12:46:10 +0200200 mtebc(EBC0_CFG, 0xb8400000);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200201
202 mtebc(pb0cr, CFG_EBC_PB0CR);
203 mtebc(pb0ap, CFG_EBC_PB0AP);
204#endif
205
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200206 /*
207 * Select required NAND chip in NDFC
208 */
Stefan Roese7ade0c62006-10-24 18:06:48 +0200209 board_nand_select_device(nand, cs);
Stefan Roese91da09c2007-06-01 15:15:12 +0200210 out_be32((u32 *)(base + NDFC_BCFG0 + (cs << 2)), 0x80002222);
Stefan Roesedbbd1252007-10-05 17:10:59 +0200211
Heiko Schocherfa230442006-12-21 17:17:02 +0100212 return 0;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200213}
214
215#endif