blob: 265959502d8320a0533d59eb4be88528984518a8 [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020013 * SPDX-License-Identifier: GPL-2.0+
Stefan Roese887e2ec2006-09-07 11:51:23 +020014 */
15
16#include <common.h>
Stefan Roese887e2ec2006-09-07 11:51:23 +020017#include <nand.h>
18#include <linux/mtd/ndfc.h>
Stefan Roese91da09c2007-06-01 15:15:12 +020019#include <linux/mtd/nand_ecc.h>
Stefan Roese887e2ec2006-09-07 11:51:23 +020020#include <asm/processor.h>
Stefan Roese91da09c2007-06-01 15:15:12 +020021#include <asm/io.h>
Stefan Roeseb36df562010-09-09 19:18:00 +020022#include <asm/ppc4xx.h>
Stefan Roese887e2ec2006-09-07 11:51:23 +020023
Alex Watermaneced4622011-05-19 15:08:36 -040024#ifndef CONFIG_SYS_NAND_BCR
25#define CONFIG_SYS_NAND_BCR 0x80002222
26#endif
27#ifndef CONFIG_SYS_NDFC_EBC0_CFG
28#define CONFIG_SYS_NDFC_EBC0_CFG 0xb8400000
29#endif
30
Stefan Roese3df2ece2008-01-05 16:47:58 +010031/*
32 * We need to store the info, which chip-select (CS) is used for the
33 * chip number. For example on Sequoia NAND chip #0 uses
34 * CS #3.
35 */
36static int ndfc_cs[NDFC_MAX_BANKS];
Stefan Roese887e2ec2006-09-07 11:51:23 +020037
William Juulcfa460a2007-10-31 13:53:06 +010038static void ndfc_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Stefan Roese887e2ec2006-09-07 11:51:23 +020039{
William Juul5e1dae52007-11-09 13:32:30 +010040 struct nand_chip *this = mtd->priv;
Stefan Roese3df2ece2008-01-05 16:47:58 +010041 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
Stefan Roese887e2ec2006-09-07 11:51:23 +020042
Stefan Roese3df2ece2008-01-05 16:47:58 +010043 if (cmd == NAND_CMD_NONE)
44 return;
Stefan Roese887e2ec2006-09-07 11:51:23 +020045
Stefan Roese3df2ece2008-01-05 16:47:58 +010046 if (ctrl & NAND_CLE)
47 out_8((u8 *)(base + NDFC_CMD), cmd & 0xFF);
48 else
49 out_8((u8 *)(base + NDFC_ALE), cmd & 0xFF);
Stefan Roese887e2ec2006-09-07 11:51:23 +020050}
51
52static int ndfc_dev_ready(struct mtd_info *mtdinfo)
53{
Wolfgang Denk511d0c72006-10-09 00:42:01 +020054 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3df2ece2008-01-05 16:47:58 +010055 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
Stefan Roese887e2ec2006-09-07 11:51:23 +020056
Stefan Roese3df2ece2008-01-05 16:47:58 +010057 return (in_be32((u32 *)(base + NDFC_STAT)) & NDFC_STAT_IS_READY);
Stefan Roese887e2ec2006-09-07 11:51:23 +020058}
59
Stefan Roese91da09c2007-06-01 15:15:12 +020060static void ndfc_enable_hwecc(struct mtd_info *mtdinfo, int mode)
61{
62 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3df2ece2008-01-05 16:47:58 +010063 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
Stefan Roese91da09c2007-06-01 15:15:12 +020064 u32 ccr;
65
66 ccr = in_be32((u32 *)(base + NDFC_CCR));
67 ccr |= NDFC_CCR_RESET_ECC;
68 out_be32((u32 *)(base + NDFC_CCR), ccr);
69}
70
71static int ndfc_calculate_ecc(struct mtd_info *mtdinfo,
72 const u_char *dat, u_char *ecc_code)
73{
74 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3df2ece2008-01-05 16:47:58 +010075 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
Stefan Roese91da09c2007-06-01 15:15:12 +020076 u32 ecc;
77 u8 *p = (u8 *)&ecc;
78
79 ecc = in_be32((u32 *)(base + NDFC_ECC));
80
81 /* The NDFC uses Smart Media (SMC) bytes order
82 */
Feng Kan68e74562009-08-21 10:59:42 -070083 ecc_code[0] = p[1];
84 ecc_code[1] = p[2];
Stefan Roese91da09c2007-06-01 15:15:12 +020085 ecc_code[2] = p[3];
86
87 return 0;
88}
Stefan Roese887e2ec2006-09-07 11:51:23 +020089
90/*
91 * Speedups for buffer read/write/verify
92 *
93 * NDFC allows 32bit read/write of data. So we can speed up the buffer
94 * functions. No further checking, as nand_base will always read/write
95 * page aligned.
96 */
97static void ndfc_read_buf(struct mtd_info *mtdinfo, uint8_t *buf, int len)
98{
Wolfgang Denk511d0c72006-10-09 00:42:01 +020099 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3df2ece2008-01-05 16:47:58 +0100100 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200101 uint32_t *p = (uint32_t *) buf;
102
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200103 for (;len > 0; len -= 4)
Stefan Roese91da09c2007-06-01 15:15:12 +0200104 *p++ = in_be32((u32 *)(base + NDFC_DATA));
Stefan Roese887e2ec2006-09-07 11:51:23 +0200105}
106
Stefan Roese91da09c2007-06-01 15:15:12 +0200107/*
108 * Don't use these speedup functions in NAND boot image, since the image
109 * has to fit into 4kByte.
110 */
Stefan Roese887e2ec2006-09-07 11:51:23 +0200111static void ndfc_write_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
112{
Wolfgang Denk511d0c72006-10-09 00:42:01 +0200113 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3df2ece2008-01-05 16:47:58 +0100114 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200115 uint32_t *p = (uint32_t *) buf;
116
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200117 for (; len > 0; len -= 4)
Stefan Roese91da09c2007-06-01 15:15:12 +0200118 out_be32((u32 *)(base + NDFC_DATA), *p++);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200119}
120
Heiko Schocherff94bc42014-06-24 10:10:04 +0200121#if defined(CONFIG_MTD_NAND_VERIFY_WRITE)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200122static int ndfc_verify_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
123{
Wolfgang Denk511d0c72006-10-09 00:42:01 +0200124 struct nand_chip *this = mtdinfo->priv;
Stefan Roese3df2ece2008-01-05 16:47:58 +0100125 ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200126 uint32_t *p = (uint32_t *) buf;
127
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200128 for (; len > 0; len -= 4)
Stefan Roese91da09c2007-06-01 15:15:12 +0200129 if (*p++ != in_be32((u32 *)(base + NDFC_DATA)))
Stefan Roese887e2ec2006-09-07 11:51:23 +0200130 return -1;
131
132 return 0;
133}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200134#endif
Stefan Roese887e2ec2006-09-07 11:51:23 +0200135
Alex Watermaneced4622011-05-19 15:08:36 -0400136/*
137 * Read a byte from the NDFC.
138 */
139static uint8_t ndfc_read_byte(struct mtd_info *mtd)
140{
141
142 struct nand_chip *chip = mtd->priv;
143
Fabio Estevam66bd1842013-04-11 09:35:34 +0000144#ifdef CONFIG_SYS_NAND_BUSWIDTH_16BIT
Alex Watermaneced4622011-05-19 15:08:36 -0400145 return (uint8_t) readw(chip->IO_ADDR_R);
146#else
147 return readb(chip->IO_ADDR_R);
Wolfgang Ocker52aef8f2008-08-26 19:55:23 +0200148#endif
149
Alex Watermaneced4622011-05-19 15:08:36 -0400150}
151
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200152void board_nand_select_device(struct nand_chip *nand, int chip)
153{
Stefan Roese7ade0c62006-10-24 18:06:48 +0200154 /*
155 * Don't use "chip" to address the NAND device,
156 * generate the cs from the address where it is encoded.
157 */
Stefan Roese3df2ece2008-01-05 16:47:58 +0100158 ulong base = (ulong)nand->IO_ADDR_W & 0xffffff00;
159 int cs = ndfc_cs[chip];
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200160
161 /* Set NandFlash Core Configuration Register */
Stefan Roese91da09c2007-06-01 15:15:12 +0200162 /* 1 col x 2 rows */
163 out_be32((u32 *)(base + NDFC_CCR), 0x00000000 | (cs << 24));
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200164 out_be32((u32 *)(base + NDFC_BCFG0 + (cs << 2)), CONFIG_SYS_NAND_BCR);
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200165}
166
Stefan Roesec2b4b2e2008-08-29 11:56:49 +0200167static void ndfc_select_chip(struct mtd_info *mtd, int chip)
168{
169 /*
170 * Nothing to do here!
171 */
172}
173
Heiko Schocherfa230442006-12-21 17:17:02 +0100174int board_nand_init(struct nand_chip *nand)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200175{
Stefan Roese7ade0c62006-10-24 18:06:48 +0200176 int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
Stefan Roese3df2ece2008-01-05 16:47:58 +0100177 ulong base = (ulong)nand->IO_ADDR_W & 0xffffff00;
178 static int chip = 0;
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200179
Stefan Roese3df2ece2008-01-05 16:47:58 +0100180 /*
181 * Save chip-select for this chip #
182 */
183 ndfc_cs[chip] = cs;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200184
Stefan Roese3df2ece2008-01-05 16:47:58 +0100185 /*
186 * Select required NAND chip in NDFC
187 */
188 board_nand_select_device(nand, chip);
189
190 nand->IO_ADDR_R = (void __iomem *)(base + NDFC_DATA);
191 nand->IO_ADDR_W = (void __iomem *)(base + NDFC_DATA);
192 nand->cmd_ctrl = ndfc_hwcontrol;
193 nand->chip_delay = 50;
194 nand->read_buf = ndfc_read_buf;
195 nand->dev_ready = ndfc_dev_ready;
William Juul5e1dae52007-11-09 13:32:30 +0100196 nand->ecc.correct = nand_correct_data;
197 nand->ecc.hwctl = ndfc_enable_hwecc;
198 nand->ecc.calculate = ndfc_calculate_ecc;
199 nand->ecc.mode = NAND_ECC_HW;
200 nand->ecc.size = 256;
201 nand->ecc.bytes = 3;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000202 nand->ecc.strength = 1;
Stefan Roesec2b4b2e2008-08-29 11:56:49 +0200203 nand->select_chip = ndfc_select_chip;
Stefan Roese91da09c2007-06-01 15:15:12 +0200204
Fabio Estevam66bd1842013-04-11 09:35:34 +0000205#ifdef CONFIG_SYS_NAND_BUSWIDTH_16BIT
Alex Watermaneced4622011-05-19 15:08:36 -0400206 nand->options |= NAND_BUSWIDTH_16;
207#endif
208
Stefan Roese887e2ec2006-09-07 11:51:23 +0200209 nand->write_buf = ndfc_write_buf;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200210#if defined(CONFIG_MTD_NAND_VERIFY_WRITE)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200211 nand->verify_buf = ndfc_verify_buf;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200212#endif
Alex Watermaneced4622011-05-19 15:08:36 -0400213 nand->read_byte = ndfc_read_byte;
Stefan Roesec5d02822010-11-23 14:32:48 +0100214
215 chip++;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200216
Heiko Schocherfa230442006-12-21 17:17:02 +0100217 return 0;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200218}