blob: 0729e0c9c02a5d00c250f4fa92f6b1eb8a49a5c4 [file] [log] [blame]
Stefan Roese887e2ec2006-09-07 11:51:23 +02001/*
2 * Overview:
3 * Platform independend driver for NDFC (NanD Flash Controller)
Stefan Roese5d841fa2009-05-20 10:58:01 +02004 * integrated into IBM/AMCC PPC4xx cores
Stefan Roese887e2ec2006-09-07 11:51:23 +02005 *
Stefan Roese5d841fa2009-05-20 10:58:01 +02006 * (C) Copyright 2006-2009
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>
Stefan Roese887e2ec2006-09-07 11:51:23 +020033#include <nand.h>
34#include <linux/mtd/ndfc.h>
Stefan Roese91da09c2007-06-01 15:15:12 +020035#include <linux/mtd/nand_ecc.h>
Stefan Roese887e2ec2006-09-07 11:51:23 +020036#include <asm/processor.h>
Stefan Roese91da09c2007-06-01 15:15:12 +020037#include <asm/io.h>
Stefan Roeseb36df562010-09-09 19:18:00 +020038#include <asm/ppc4xx.h>
Stefan Roese887e2ec2006-09-07 11:51:23 +020039
Stefan Roese3df2ece2008-01-05 16:47:58 +010040/*
41 * We need to store the info, which chip-select (CS) is used for the
42 * chip number. For example on Sequoia NAND chip #0 uses
43 * CS #3.
44 */
45static int ndfc_cs[NDFC_MAX_BANKS];
Stefan Roese887e2ec2006-09-07 11:51:23 +020046
William Juulcfa460a2007-10-31 13:53:06 +010047static void ndfc_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Stefan Roese887e2ec2006-09-07 11:51:23 +020048{
William Juul5e1dae52007-11-09 13:32:30 +010049 struct nand_chip *this = mtd->priv;
Stefan Roese3df2ece2008-01-05 16:47:58 +010050 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
Stefan Roese887e2ec2006-09-07 11:51:23 +020051
Stefan Roese3df2ece2008-01-05 16:47:58 +010052 if (cmd == NAND_CMD_NONE)
53 return;
Stefan Roese887e2ec2006-09-07 11:51:23 +020054
Stefan Roese3df2ece2008-01-05 16:47:58 +010055 if (ctrl & NAND_CLE)
56 out_8((u8 *)(base + NDFC_CMD), cmd & 0xFF);
57 else
58 out_8((u8 *)(base + NDFC_ALE), cmd & 0xFF);
Stefan Roese887e2ec2006-09-07 11:51:23 +020059}
60
61static int ndfc_dev_ready(struct mtd_info *mtdinfo)
62{
Wolfgang Denk511d0c72006-10-09 00:42:01 +020063 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3df2ece2008-01-05 16:47:58 +010064 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
Stefan Roese887e2ec2006-09-07 11:51:23 +020065
Stefan Roese3df2ece2008-01-05 16:47:58 +010066 return (in_be32((u32 *)(base + NDFC_STAT)) & NDFC_STAT_IS_READY);
Stefan Roese887e2ec2006-09-07 11:51:23 +020067}
68
Stefan Roese91da09c2007-06-01 15:15:12 +020069static void ndfc_enable_hwecc(struct mtd_info *mtdinfo, int mode)
70{
71 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3df2ece2008-01-05 16:47:58 +010072 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
Stefan Roese91da09c2007-06-01 15:15:12 +020073 u32 ccr;
74
75 ccr = in_be32((u32 *)(base + NDFC_CCR));
76 ccr |= NDFC_CCR_RESET_ECC;
77 out_be32((u32 *)(base + NDFC_CCR), ccr);
78}
79
80static int ndfc_calculate_ecc(struct mtd_info *mtdinfo,
81 const u_char *dat, u_char *ecc_code)
82{
83 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3df2ece2008-01-05 16:47:58 +010084 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
Stefan Roese91da09c2007-06-01 15:15:12 +020085 u32 ecc;
86 u8 *p = (u8 *)&ecc;
87
88 ecc = in_be32((u32 *)(base + NDFC_ECC));
89
90 /* The NDFC uses Smart Media (SMC) bytes order
91 */
Feng Kan68e74562009-08-21 10:59:42 -070092 ecc_code[0] = p[1];
93 ecc_code[1] = p[2];
Stefan Roese91da09c2007-06-01 15:15:12 +020094 ecc_code[2] = p[3];
95
96 return 0;
97}
Stefan Roese887e2ec2006-09-07 11:51:23 +020098
99/*
100 * Speedups for buffer read/write/verify
101 *
102 * NDFC allows 32bit read/write of data. So we can speed up the buffer
103 * functions. No further checking, as nand_base will always read/write
104 * page aligned.
105 */
106static void ndfc_read_buf(struct mtd_info *mtdinfo, uint8_t *buf, int len)
107{
Wolfgang Denk511d0c72006-10-09 00:42:01 +0200108 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3df2ece2008-01-05 16:47:58 +0100109 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200110 uint32_t *p = (uint32_t *) buf;
111
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200112 for (;len > 0; len -= 4)
Stefan Roese91da09c2007-06-01 15:15:12 +0200113 *p++ = in_be32((u32 *)(base + NDFC_DATA));
Stefan Roese887e2ec2006-09-07 11:51:23 +0200114}
115
Stefan Roese91da09c2007-06-01 15:15:12 +0200116#ifndef CONFIG_NAND_SPL
117/*
118 * Don't use these speedup functions in NAND boot image, since the image
119 * has to fit into 4kByte.
120 */
Stefan Roese887e2ec2006-09-07 11:51:23 +0200121static void ndfc_write_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
122{
Wolfgang Denk511d0c72006-10-09 00:42:01 +0200123 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3df2ece2008-01-05 16:47:58 +0100124 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200125 uint32_t *p = (uint32_t *) buf;
126
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200127 for (; len > 0; len -= 4)
Stefan Roese91da09c2007-06-01 15:15:12 +0200128 out_be32((u32 *)(base + NDFC_DATA), *p++);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200129}
130
131static int ndfc_verify_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
132{
Wolfgang Denk511d0c72006-10-09 00:42:01 +0200133 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3df2ece2008-01-05 16:47:58 +0100134 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200135 uint32_t *p = (uint32_t *) buf;
136
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200137 for (; len > 0; len -= 4)
Stefan Roese91da09c2007-06-01 15:15:12 +0200138 if (*p++ != in_be32((u32 *)(base + NDFC_DATA)))
Stefan Roese887e2ec2006-09-07 11:51:23 +0200139 return -1;
140
141 return 0;
142}
143#endif /* #ifndef CONFIG_NAND_SPL */
144
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200145#ifndef CONFIG_SYS_NAND_BCR
146#define CONFIG_SYS_NAND_BCR 0x80002222
Wolfgang Ocker52aef8f2008-08-26 19:55:23 +0200147#endif
148
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200149void board_nand_select_device(struct nand_chip *nand, int chip)
150{
Stefan Roese7ade0c62006-10-24 18:06:48 +0200151 /*
152 * Don't use "chip" to address the NAND device,
153 * generate the cs from the address where it is encoded.
154 */
Stefan Roese3df2ece2008-01-05 16:47:58 +0100155 ulong base = (ulong)nand->IO_ADDR_W & 0xffffff00;
156 int cs = ndfc_cs[chip];
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200157
158 /* Set NandFlash Core Configuration Register */
Stefan Roese91da09c2007-06-01 15:15:12 +0200159 /* 1 col x 2 rows */
160 out_be32((u32 *)(base + NDFC_CCR), 0x00000000 | (cs << 24));
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200161 out_be32((u32 *)(base + NDFC_BCFG0 + (cs << 2)), CONFIG_SYS_NAND_BCR);
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200162}
163
Stefan Roesec2b4b2e2008-08-29 11:56:49 +0200164static void ndfc_select_chip(struct mtd_info *mtd, int chip)
165{
166 /*
167 * Nothing to do here!
168 */
169}
170
Heiko Schocherfa230442006-12-21 17:17:02 +0100171int board_nand_init(struct nand_chip *nand)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200172{
Stefan Roese7ade0c62006-10-24 18:06:48 +0200173 int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
Stefan Roese3df2ece2008-01-05 16:47:58 +0100174 ulong base = (ulong)nand->IO_ADDR_W & 0xffffff00;
175 static int chip = 0;
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200176
Stefan Roese3df2ece2008-01-05 16:47:58 +0100177 /*
178 * Save chip-select for this chip #
179 */
180 ndfc_cs[chip] = cs;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200181
Stefan Roese3df2ece2008-01-05 16:47:58 +0100182 /*
183 * Select required NAND chip in NDFC
184 */
185 board_nand_select_device(nand, chip);
186
187 nand->IO_ADDR_R = (void __iomem *)(base + NDFC_DATA);
188 nand->IO_ADDR_W = (void __iomem *)(base + NDFC_DATA);
189 nand->cmd_ctrl = ndfc_hwcontrol;
190 nand->chip_delay = 50;
191 nand->read_buf = ndfc_read_buf;
192 nand->dev_ready = ndfc_dev_ready;
William Juul5e1dae52007-11-09 13:32:30 +0100193 nand->ecc.correct = nand_correct_data;
194 nand->ecc.hwctl = ndfc_enable_hwecc;
195 nand->ecc.calculate = ndfc_calculate_ecc;
196 nand->ecc.mode = NAND_ECC_HW;
197 nand->ecc.size = 256;
198 nand->ecc.bytes = 3;
Stefan Roesec2b4b2e2008-08-29 11:56:49 +0200199 nand->select_chip = ndfc_select_chip;
Stefan Roese91da09c2007-06-01 15:15:12 +0200200
Stefan Roese887e2ec2006-09-07 11:51:23 +0200201#ifndef CONFIG_NAND_SPL
202 nand->write_buf = ndfc_write_buf;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200203 nand->verify_buf = ndfc_verify_buf;
Stefan Roesec5d02822010-11-23 14:32:48 +0100204
205 chip++;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200206#else
207 /*
208 * Setup EBC (CS0 only right now)
209 */
Stefan Roese6f3dfc12007-05-22 12:46:10 +0200210 mtebc(EBC0_CFG, 0xb8400000);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200211
Stefan Roesed1c3b272009-09-09 16:25:29 +0200212 mtebc(PB0CR, CONFIG_SYS_EBC_PB0CR);
213 mtebc(PB0AP, CONFIG_SYS_EBC_PB0AP);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200214#endif
215
Heiko Schocherfa230442006-12-21 17:17:02 +0100216 return 0;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200217}