Stefan Roese | 42fbddd | 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 | 1eb122a | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 6 | * (C) Copyright 2006-2007 |
Stefan Roese | 42fbddd | 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 | |
Jon Loeliger | a521774 | 2007-07-09 18:57:22 -0500 | [diff] [blame] | 34 | #if defined(CONFIG_CMD_NAND) && !defined(CFG_NAND_LEGACY) && \ |
Stefan Roese | 6e7cd7c | 2006-09-07 13:09:53 +0200 | [diff] [blame] | 35 | (defined(CONFIG_440EP) || defined(CONFIG_440GR) || \ |
Stefan Roese | cd2308d | 2007-05-22 12:46:10 +0200 | [diff] [blame] | 36 | defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \ |
Stefan Roese | 153b3e2 | 2007-10-05 17:10:59 +0200 | [diff] [blame] | 37 | defined(CONFIG_405EZ) || defined(CONFIG_405EX)) |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 38 | |
| 39 | #include <nand.h> |
| 40 | #include <linux/mtd/ndfc.h> |
Stefan Roese | 1eb122a | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 41 | #include <linux/mtd/nand_ecc.h> |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 42 | #include <asm/processor.h> |
Stefan Roese | 1eb122a | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 43 | #include <asm/io.h> |
Stefan Roese | cd2308d | 2007-05-22 12:46:10 +0200 | [diff] [blame] | 44 | #include <ppc4xx.h> |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 45 | |
| 46 | static u8 hwctl = 0; |
| 47 | |
| 48 | static void ndfc_hwcontrol(struct mtd_info *mtdinfo, int cmd) |
| 49 | { |
| 50 | switch (cmd) { |
| 51 | case NAND_CTL_SETCLE: |
| 52 | hwctl |= 0x1; |
| 53 | break; |
| 54 | |
| 55 | case NAND_CTL_CLRCLE: |
| 56 | hwctl &= ~0x1; |
| 57 | break; |
| 58 | |
| 59 | case NAND_CTL_SETALE: |
| 60 | hwctl |= 0x2; |
| 61 | break; |
| 62 | |
| 63 | case NAND_CTL_CLRALE: |
| 64 | hwctl &= ~0x2; |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static void ndfc_write_byte(struct mtd_info *mtdinfo, u_char byte) |
| 70 | { |
Wolfgang Denk | 4df0da5 | 2006-10-09 00:42:01 +0200 | [diff] [blame] | 71 | struct nand_chip *this = mtdinfo->priv; |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 72 | ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc; |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 73 | |
| 74 | if (hwctl & 0x1) |
Stefan Roese | 1eb122a | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 75 | out_8((u8 *)(base + NDFC_CMD), byte); |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 76 | else if (hwctl & 0x2) |
Stefan Roese | 1eb122a | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 77 | out_8((u8 *)(base + NDFC_ALE), byte); |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 78 | else |
Stefan Roese | 1eb122a | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 79 | out_8((u8 *)(base + NDFC_DATA), byte); |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static u_char ndfc_read_byte(struct mtd_info *mtdinfo) |
| 83 | { |
Wolfgang Denk | 4df0da5 | 2006-10-09 00:42:01 +0200 | [diff] [blame] | 84 | struct nand_chip *this = mtdinfo->priv; |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 85 | ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc; |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 86 | |
Stefan Roese | 1eb122a | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 87 | return (in_8((u8 *)(base + NDFC_DATA))); |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static int ndfc_dev_ready(struct mtd_info *mtdinfo) |
| 91 | { |
Wolfgang Denk | 4df0da5 | 2006-10-09 00:42:01 +0200 | [diff] [blame] | 92 | struct nand_chip *this = mtdinfo->priv; |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 93 | ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc; |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 94 | |
Stefan Roese | 1eb122a | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 95 | while (!(in_be32((u32 *)(base + NDFC_STAT)) & NDFC_STAT_IS_READY)) |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 96 | ; |
| 97 | |
| 98 | return 1; |
| 99 | } |
| 100 | |
Stefan Roese | 1eb122a | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 101 | static void ndfc_enable_hwecc(struct mtd_info *mtdinfo, int mode) |
| 102 | { |
| 103 | struct nand_chip *this = mtdinfo->priv; |
| 104 | ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc; |
| 105 | u32 ccr; |
| 106 | |
| 107 | ccr = in_be32((u32 *)(base + NDFC_CCR)); |
| 108 | ccr |= NDFC_CCR_RESET_ECC; |
| 109 | out_be32((u32 *)(base + NDFC_CCR), ccr); |
| 110 | } |
| 111 | |
| 112 | static int ndfc_calculate_ecc(struct mtd_info *mtdinfo, |
| 113 | const u_char *dat, u_char *ecc_code) |
| 114 | { |
| 115 | struct nand_chip *this = mtdinfo->priv; |
| 116 | ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc; |
| 117 | u32 ecc; |
| 118 | u8 *p = (u8 *)&ecc; |
| 119 | |
| 120 | ecc = in_be32((u32 *)(base + NDFC_ECC)); |
| 121 | |
| 122 | /* The NDFC uses Smart Media (SMC) bytes order |
| 123 | */ |
| 124 | ecc_code[0] = p[2]; |
| 125 | ecc_code[1] = p[1]; |
| 126 | ecc_code[2] = p[3]; |
| 127 | |
| 128 | return 0; |
| 129 | } |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * Speedups for buffer read/write/verify |
| 133 | * |
| 134 | * NDFC allows 32bit read/write of data. So we can speed up the buffer |
| 135 | * functions. No further checking, as nand_base will always read/write |
| 136 | * page aligned. |
| 137 | */ |
| 138 | static void ndfc_read_buf(struct mtd_info *mtdinfo, uint8_t *buf, int len) |
| 139 | { |
Wolfgang Denk | 4df0da5 | 2006-10-09 00:42:01 +0200 | [diff] [blame] | 140 | struct nand_chip *this = mtdinfo->priv; |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 141 | ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc; |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 142 | uint32_t *p = (uint32_t *) buf; |
| 143 | |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 144 | for (;len > 0; len -= 4) |
Stefan Roese | 1eb122a | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 145 | *p++ = in_be32((u32 *)(base + NDFC_DATA)); |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Stefan Roese | 1eb122a | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 148 | #ifndef CONFIG_NAND_SPL |
| 149 | /* |
| 150 | * Don't use these speedup functions in NAND boot image, since the image |
| 151 | * has to fit into 4kByte. |
| 152 | */ |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 153 | static void ndfc_write_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len) |
| 154 | { |
Wolfgang Denk | 4df0da5 | 2006-10-09 00:42:01 +0200 | [diff] [blame] | 155 | struct nand_chip *this = mtdinfo->priv; |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 156 | ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc; |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 157 | uint32_t *p = (uint32_t *) buf; |
| 158 | |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 159 | for (; len > 0; len -= 4) |
Stefan Roese | 1eb122a | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 160 | out_be32((u32 *)(base + NDFC_DATA), *p++); |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static int ndfc_verify_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len) |
| 164 | { |
Wolfgang Denk | 4df0da5 | 2006-10-09 00:42:01 +0200 | [diff] [blame] | 165 | struct nand_chip *this = mtdinfo->priv; |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 166 | ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc; |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 167 | uint32_t *p = (uint32_t *) buf; |
| 168 | |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 169 | for (; len > 0; len -= 4) |
Stefan Roese | 1eb122a | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 170 | if (*p++ != in_be32((u32 *)(base + NDFC_DATA))) |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 171 | return -1; |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | #endif /* #ifndef CONFIG_NAND_SPL */ |
| 176 | |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 177 | void board_nand_select_device(struct nand_chip *nand, int chip) |
| 178 | { |
Stefan Roese | d07e572 | 2006-10-24 18:06:48 +0200 | [diff] [blame] | 179 | /* |
| 180 | * Don't use "chip" to address the NAND device, |
| 181 | * generate the cs from the address where it is encoded. |
| 182 | */ |
| 183 | int cs = (ulong)nand->IO_ADDR_W & 0x00000003; |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 184 | ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc; |
| 185 | |
| 186 | /* Set NandFlash Core Configuration Register */ |
Stefan Roese | 1eb122a | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 187 | /* 1 col x 2 rows */ |
| 188 | out_be32((u32 *)(base + NDFC_CCR), 0x00000000 | (cs << 24)); |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Heiko Schocher | 3ec4366 | 2006-12-21 17:17:02 +0100 | [diff] [blame] | 191 | int board_nand_init(struct nand_chip *nand) |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 192 | { |
Stefan Roese | d07e572 | 2006-10-24 18:06:48 +0200 | [diff] [blame] | 193 | int cs = (ulong)nand->IO_ADDR_W & 0x00000003; |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 194 | ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc; |
| 195 | |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 196 | nand->hwcontrol = ndfc_hwcontrol; |
| 197 | nand->read_byte = ndfc_read_byte; |
Stefan Roese | 1eb122a | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 198 | nand->read_buf = ndfc_read_buf; |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 199 | nand->write_byte = ndfc_write_byte; |
| 200 | nand->dev_ready = ndfc_dev_ready; |
| 201 | |
Stefan Roese | 1eb122a | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 202 | nand->eccmode = NAND_ECC_HW3_256; |
| 203 | nand->enable_hwecc = ndfc_enable_hwecc; |
| 204 | nand->calculate_ecc = ndfc_calculate_ecc; |
| 205 | nand->correct_data = nand_correct_data; |
| 206 | |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 207 | #ifndef CONFIG_NAND_SPL |
| 208 | nand->write_buf = ndfc_write_buf; |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 209 | nand->verify_buf = ndfc_verify_buf; |
| 210 | #else |
| 211 | /* |
| 212 | * Setup EBC (CS0 only right now) |
| 213 | */ |
Stefan Roese | cd2308d | 2007-05-22 12:46:10 +0200 | [diff] [blame] | 214 | mtebc(EBC0_CFG, 0xb8400000); |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 215 | |
| 216 | mtebc(pb0cr, CFG_EBC_PB0CR); |
| 217 | mtebc(pb0ap, CFG_EBC_PB0AP); |
| 218 | #endif |
| 219 | |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 220 | /* |
| 221 | * Select required NAND chip in NDFC |
| 222 | */ |
Stefan Roese | d07e572 | 2006-10-24 18:06:48 +0200 | [diff] [blame] | 223 | board_nand_select_device(nand, cs); |
Stefan Roese | 1eb122a | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 224 | out_be32((u32 *)(base + NDFC_BCFG0 + (cs << 2)), 0x80002222); |
Stefan Roese | 153b3e2 | 2007-10-05 17:10:59 +0200 | [diff] [blame] | 225 | |
Heiko Schocher | 3ec4366 | 2006-12-21 17:17:02 +0100 | [diff] [blame] | 226 | return 0; |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | #endif |