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 | |
Jon Loeliger | 3a1ed1e | 2007-07-09 18:57:22 -0500 | [diff] [blame] | 34 | #if defined(CONFIG_CMD_NAND) && !defined(CFG_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 | |
| 47 | static u8 hwctl = 0; |
| 48 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 49 | 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] | 50 | { |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame^] | 51 | struct nand_chip *this = mtd->priv; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 52 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 53 | 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 Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 62 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 63 | if (cmd != NAND_CMD_NONE) |
| 64 | writeb(cmd, this->IO_ADDR_W); |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static u_char ndfc_read_byte(struct mtd_info *mtdinfo) |
| 68 | { |
Wolfgang Denk | 511d0c7 | 2006-10-09 00:42:01 +0200 | [diff] [blame] | 69 | struct nand_chip *this = mtdinfo->priv; |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 70 | ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 71 | |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 72 | return (in_8((u8 *)(base + NDFC_DATA))); |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static int ndfc_dev_ready(struct mtd_info *mtdinfo) |
| 76 | { |
Wolfgang Denk | 511d0c7 | 2006-10-09 00:42:01 +0200 | [diff] [blame] | 77 | struct nand_chip *this = mtdinfo->priv; |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 78 | ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 79 | |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 80 | while (!(in_be32((u32 *)(base + NDFC_STAT)) & NDFC_STAT_IS_READY)) |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 81 | ; |
| 82 | |
| 83 | return 1; |
| 84 | } |
| 85 | |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 86 | static 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 | |
| 97 | static 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 Roese | ff02f13 | 2008-02-01 09:38:29 +0100 | [diff] [blame] | 109 | ecc_code[0] = p[1]; |
| 110 | ecc_code[1] = p[2]; |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 111 | ecc_code[2] = p[3]; |
| 112 | |
| 113 | return 0; |
| 114 | } |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 115 | |
| 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 | */ |
| 123 | static void ndfc_read_buf(struct mtd_info *mtdinfo, uint8_t *buf, int len) |
| 124 | { |
Wolfgang Denk | 511d0c7 | 2006-10-09 00:42:01 +0200 | [diff] [blame] | 125 | struct nand_chip *this = mtdinfo->priv; |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 126 | ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 127 | uint32_t *p = (uint32_t *) buf; |
| 128 | |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 129 | for (;len > 0; len -= 4) |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 130 | *p++ = in_be32((u32 *)(base + NDFC_DATA)); |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 133 | #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 Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 138 | static void ndfc_write_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 | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 141 | ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc; |
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 | out_be32((u32 *)(base + NDFC_DATA), *p++); |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static int ndfc_verify_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len) |
| 149 | { |
Wolfgang Denk | 511d0c7 | 2006-10-09 00:42:01 +0200 | [diff] [blame] | 150 | struct nand_chip *this = mtdinfo->priv; |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 151 | ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 152 | uint32_t *p = (uint32_t *) buf; |
| 153 | |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 154 | for (; len > 0; len -= 4) |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 155 | if (*p++ != in_be32((u32 *)(base + NDFC_DATA))) |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 156 | return -1; |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | #endif /* #ifndef CONFIG_NAND_SPL */ |
| 161 | |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 162 | void board_nand_select_device(struct nand_chip *nand, int chip) |
| 163 | { |
Stefan Roese | 7ade0c6 | 2006-10-24 18:06:48 +0200 | [diff] [blame] | 164 | /* |
| 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 Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 169 | ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc; |
| 170 | |
| 171 | /* Set NandFlash Core Configuration Register */ |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 172 | /* 1 col x 2 rows */ |
| 173 | out_be32((u32 *)(base + NDFC_CCR), 0x00000000 | (cs << 24)); |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Heiko Schocher | fa23044 | 2006-12-21 17:17:02 +0100 | [diff] [blame] | 176 | int board_nand_init(struct nand_chip *nand) |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 177 | { |
Stefan Roese | 7ade0c6 | 2006-10-24 18:06:48 +0200 | [diff] [blame] | 178 | int cs = (ulong)nand->IO_ADDR_W & 0x00000003; |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 179 | ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc; |
| 180 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 181 | nand->cmd_ctrl = ndfc_hwcontrol; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 182 | nand->read_byte = ndfc_read_byte; |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 183 | nand->read_buf = ndfc_read_buf; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 184 | nand->dev_ready = ndfc_dev_ready; |
| 185 | |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame^] | 186 | 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 Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 192 | |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 193 | #ifndef CONFIG_NAND_SPL |
| 194 | nand->write_buf = ndfc_write_buf; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 195 | nand->verify_buf = ndfc_verify_buf; |
| 196 | #else |
| 197 | /* |
| 198 | * Setup EBC (CS0 only right now) |
| 199 | */ |
Stefan Roese | 6f3dfc1 | 2007-05-22 12:46:10 +0200 | [diff] [blame] | 200 | mtebc(EBC0_CFG, 0xb8400000); |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 201 | |
| 202 | mtebc(pb0cr, CFG_EBC_PB0CR); |
| 203 | mtebc(pb0ap, CFG_EBC_PB0AP); |
| 204 | #endif |
| 205 | |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 206 | /* |
| 207 | * Select required NAND chip in NDFC |
| 208 | */ |
Stefan Roese | 7ade0c6 | 2006-10-24 18:06:48 +0200 | [diff] [blame] | 209 | board_nand_select_device(nand, cs); |
Stefan Roese | 91da09c | 2007-06-01 15:15:12 +0200 | [diff] [blame] | 210 | out_be32((u32 *)(base + NDFC_BCFG0 + (cs << 2)), 0x80002222); |
Stefan Roese | dbbd125 | 2007-10-05 17:10:59 +0200 | [diff] [blame] | 211 | |
Heiko Schocher | fa23044 | 2006-12-21 17:17:02 +0100 | [diff] [blame] | 212 | return 0; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | #endif |