Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Overview: |
| 3 | * Platform independend driver for NDFC (NanD Flash Controller) |
| 4 | * integrated into EP440 cores |
| 5 | * |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 6 | * (C) Copyright 2006-2007 |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 7 | * 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 | |
Jean-Christophe PLAGNIOL-VILLARD | cc4a0ce | 2008-08-13 01:40:43 +0200 | [diff] [blame] | 34 | #if defined(CONFIG_CMD_NAND) && !defined(CONFIG_NAND_LEGACY) && \ |
Stefan Roese | 2d65896 | 2006-09-07 13:09:53 +0200 | [diff] [blame] | 35 | (defined(CONFIG_440EP) || defined(CONFIG_440GR) || \ |
Stefan Roese | 6f3dfc1 | 2007-05-22 12:46:10 +0200 | [diff] [blame] | 36 | defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \ |
Stefan Roese | 2801b2d | 2008-03-11 15:05:50 +0100 | [diff] [blame] | 37 | defined(CONFIG_405EZ) || defined(CONFIG_405EX) || \ |
| 38 | defined(CONFIG_460EX) || defined(CONFIG_460GT)) |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 39 | |
| 40 | #include <nand.h> |
| 41 | #include <linux/mtd/ndfc.h> |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 42 | #include <linux/mtd/nand_ecc.h> |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 43 | #include <asm/processor.h> |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 44 | #include <asm/io.h> |
Stefan Roese | 6f3dfc1 | 2007-05-22 12:46:10 +0200 | [diff] [blame] | 45 | #include <ppc4xx.h> |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 46 | |
Stefan Roese | 3df2ece | 2008-01-05 16:47:58 +0100 | [diff] [blame] | 47 | /* |
| 48 | * We need to store the info, which chip-select (CS) is used for the |
| 49 | * chip number. For example on Sequoia NAND chip #0 uses |
| 50 | * CS #3. |
| 51 | */ |
| 52 | static int ndfc_cs[NDFC_MAX_BANKS]; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 53 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 54 | static void ndfc_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl) |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 55 | { |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 56 | struct nand_chip *this = mtd->priv; |
Stefan Roese | 3df2ece | 2008-01-05 16:47:58 +0100 | [diff] [blame] | 57 | ulong base = (ulong) this->IO_ADDR_W & 0xffffff00; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 58 | |
Stefan Roese | 3df2ece | 2008-01-05 16:47:58 +0100 | [diff] [blame] | 59 | if (cmd == NAND_CMD_NONE) |
| 60 | return; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 61 | |
Stefan Roese | 3df2ece | 2008-01-05 16:47:58 +0100 | [diff] [blame] | 62 | if (ctrl & NAND_CLE) |
| 63 | out_8((u8 *)(base + NDFC_CMD), cmd & 0xFF); |
| 64 | else |
| 65 | out_8((u8 *)(base + NDFC_ALE), cmd & 0xFF); |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static int ndfc_dev_ready(struct mtd_info *mtdinfo) |
| 69 | { |
Wolfgang Denk | 511d0c7 | 2006-10-09 00:42:01 +0200 | [diff] [blame] | 70 | struct nand_chip *this = mtdinfo->priv; |
Stefan Roese | 3df2ece | 2008-01-05 16:47:58 +0100 | [diff] [blame] | 71 | ulong base = (ulong) this->IO_ADDR_W & 0xffffff00; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 72 | |
Stefan Roese | 3df2ece | 2008-01-05 16:47:58 +0100 | [diff] [blame] | 73 | return (in_be32((u32 *)(base + NDFC_STAT)) & NDFC_STAT_IS_READY); |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 76 | static void ndfc_enable_hwecc(struct mtd_info *mtdinfo, int mode) |
| 77 | { |
| 78 | struct nand_chip *this = mtdinfo->priv; |
Stefan Roese | 3df2ece | 2008-01-05 16:47:58 +0100 | [diff] [blame] | 79 | ulong base = (ulong) this->IO_ADDR_W & 0xffffff00; |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 80 | u32 ccr; |
| 81 | |
| 82 | ccr = in_be32((u32 *)(base + NDFC_CCR)); |
| 83 | ccr |= NDFC_CCR_RESET_ECC; |
| 84 | out_be32((u32 *)(base + NDFC_CCR), ccr); |
| 85 | } |
| 86 | |
| 87 | static int ndfc_calculate_ecc(struct mtd_info *mtdinfo, |
| 88 | const u_char *dat, u_char *ecc_code) |
| 89 | { |
| 90 | struct nand_chip *this = mtdinfo->priv; |
Stefan Roese | 3df2ece | 2008-01-05 16:47:58 +0100 | [diff] [blame] | 91 | ulong base = (ulong) this->IO_ADDR_W & 0xffffff00; |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 92 | u32 ecc; |
| 93 | u8 *p = (u8 *)&ecc; |
| 94 | |
| 95 | ecc = in_be32((u32 *)(base + NDFC_ECC)); |
| 96 | |
| 97 | /* The NDFC uses Smart Media (SMC) bytes order |
| 98 | */ |
Stefan Roese | ff02f13 | 2008-02-01 09:38:29 +0100 | [diff] [blame] | 99 | ecc_code[0] = p[1]; |
| 100 | ecc_code[1] = p[2]; |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 101 | ecc_code[2] = p[3]; |
| 102 | |
| 103 | return 0; |
| 104 | } |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | * Speedups for buffer read/write/verify |
| 108 | * |
| 109 | * NDFC allows 32bit read/write of data. So we can speed up the buffer |
| 110 | * functions. No further checking, as nand_base will always read/write |
| 111 | * page aligned. |
| 112 | */ |
| 113 | static void ndfc_read_buf(struct mtd_info *mtdinfo, uint8_t *buf, int len) |
| 114 | { |
Wolfgang Denk | 511d0c7 | 2006-10-09 00:42:01 +0200 | [diff] [blame] | 115 | struct nand_chip *this = mtdinfo->priv; |
Stefan Roese | 3df2ece | 2008-01-05 16:47:58 +0100 | [diff] [blame] | 116 | ulong base = (ulong) this->IO_ADDR_W & 0xffffff00; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 117 | uint32_t *p = (uint32_t *) buf; |
| 118 | |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 119 | for (;len > 0; len -= 4) |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 120 | *p++ = in_be32((u32 *)(base + NDFC_DATA)); |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 123 | #ifndef CONFIG_NAND_SPL |
| 124 | /* |
| 125 | * Don't use these speedup functions in NAND boot image, since the image |
| 126 | * has to fit into 4kByte. |
| 127 | */ |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 128 | static void ndfc_write_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len) |
| 129 | { |
Wolfgang Denk | 511d0c7 | 2006-10-09 00:42:01 +0200 | [diff] [blame] | 130 | struct nand_chip *this = mtdinfo->priv; |
Stefan Roese | 3df2ece | 2008-01-05 16:47:58 +0100 | [diff] [blame] | 131 | ulong base = (ulong) this->IO_ADDR_W & 0xffffff00; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 132 | uint32_t *p = (uint32_t *) buf; |
| 133 | |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 134 | for (; len > 0; len -= 4) |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 135 | out_be32((u32 *)(base + NDFC_DATA), *p++); |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static int ndfc_verify_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len) |
| 139 | { |
Wolfgang Denk | 511d0c7 | 2006-10-09 00:42:01 +0200 | [diff] [blame] | 140 | struct nand_chip *this = mtdinfo->priv; |
Stefan Roese | 3df2ece | 2008-01-05 16:47:58 +0100 | [diff] [blame] | 141 | ulong base = (ulong) this->IO_ADDR_W & 0xffffff00; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 142 | uint32_t *p = (uint32_t *) buf; |
| 143 | |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 144 | for (; len > 0; len -= 4) |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 145 | if (*p++ != in_be32((u32 *)(base + NDFC_DATA))) |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 146 | return -1; |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | #endif /* #ifndef CONFIG_NAND_SPL */ |
| 151 | |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 152 | void board_nand_select_device(struct nand_chip *nand, int chip) |
| 153 | { |
Stefan Roese | 7ade0c6 | 2006-10-24 18:06:48 +0200 | [diff] [blame] | 154 | /* |
| 155 | * Don't use "chip" to address the NAND device, |
| 156 | * generate the cs from the address where it is encoded. |
| 157 | */ |
Stefan Roese | 3df2ece | 2008-01-05 16:47:58 +0100 | [diff] [blame] | 158 | ulong base = (ulong)nand->IO_ADDR_W & 0xffffff00; |
| 159 | int cs = ndfc_cs[chip]; |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 160 | |
| 161 | /* Set NandFlash Core Configuration Register */ |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 162 | /* 1 col x 2 rows */ |
| 163 | out_be32((u32 *)(base + NDFC_CCR), 0x00000000 | (cs << 24)); |
Stefan Roese | 3df2ece | 2008-01-05 16:47:58 +0100 | [diff] [blame] | 164 | out_be32((u32 *)(base + NDFC_BCFG0 + (cs << 2)), 0x80002222); |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 165 | } |
| 166 | |
Heiko Schocher | fa23044 | 2006-12-21 17:17:02 +0100 | [diff] [blame] | 167 | int board_nand_init(struct nand_chip *nand) |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 168 | { |
Stefan Roese | 7ade0c6 | 2006-10-24 18:06:48 +0200 | [diff] [blame] | 169 | int cs = (ulong)nand->IO_ADDR_W & 0x00000003; |
Stefan Roese | 3df2ece | 2008-01-05 16:47:58 +0100 | [diff] [blame] | 170 | ulong base = (ulong)nand->IO_ADDR_W & 0xffffff00; |
| 171 | static int chip = 0; |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 172 | |
Stefan Roese | 3df2ece | 2008-01-05 16:47:58 +0100 | [diff] [blame] | 173 | /* |
| 174 | * Save chip-select for this chip # |
| 175 | */ |
| 176 | ndfc_cs[chip] = cs; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 177 | |
Stefan Roese | 3df2ece | 2008-01-05 16:47:58 +0100 | [diff] [blame] | 178 | /* |
| 179 | * Select required NAND chip in NDFC |
| 180 | */ |
| 181 | board_nand_select_device(nand, chip); |
| 182 | |
| 183 | nand->IO_ADDR_R = (void __iomem *)(base + NDFC_DATA); |
| 184 | nand->IO_ADDR_W = (void __iomem *)(base + NDFC_DATA); |
| 185 | nand->cmd_ctrl = ndfc_hwcontrol; |
| 186 | nand->chip_delay = 50; |
| 187 | nand->read_buf = ndfc_read_buf; |
| 188 | nand->dev_ready = ndfc_dev_ready; |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 189 | nand->ecc.correct = nand_correct_data; |
| 190 | nand->ecc.hwctl = ndfc_enable_hwecc; |
| 191 | nand->ecc.calculate = ndfc_calculate_ecc; |
| 192 | nand->ecc.mode = NAND_ECC_HW; |
| 193 | nand->ecc.size = 256; |
| 194 | nand->ecc.bytes = 3; |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 195 | |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 196 | #ifndef CONFIG_NAND_SPL |
| 197 | nand->write_buf = ndfc_write_buf; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 198 | nand->verify_buf = ndfc_verify_buf; |
| 199 | #else |
| 200 | /* |
| 201 | * Setup EBC (CS0 only right now) |
| 202 | */ |
Stefan Roese | 6f3dfc1 | 2007-05-22 12:46:10 +0200 | [diff] [blame] | 203 | mtebc(EBC0_CFG, 0xb8400000); |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 204 | |
| 205 | mtebc(pb0cr, CFG_EBC_PB0CR); |
| 206 | mtebc(pb0ap, CFG_EBC_PB0AP); |
| 207 | #endif |
| 208 | |
Stefan Roese | 3df2ece | 2008-01-05 16:47:58 +0100 | [diff] [blame] | 209 | chip++; |
Stefan Roese | dbbd125 | 2007-10-05 17:10:59 +0200 | [diff] [blame] | 210 | |
Heiko Schocher | fa23044 | 2006-12-21 17:17:02 +0100 | [diff] [blame] | 211 | return 0; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | #endif |