Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 2 | /* Freescale Enhanced Local Bus Controller FCM NAND driver |
| 3 | * |
| 4 | * Copyright (c) 2006-2008 Freescale Semiconductor |
| 5 | * |
| 6 | * Authors: Nick Spence <nick.spence@freescale.com>, |
| 7 | * Scott Wood <scottwood@freescale.com> |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | adaaa48 | 2019-11-14 12:57:43 -0700 | [diff] [blame] | 11 | #include <command.h> |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 12 | #include <malloc.h> |
Scott Wood | ea0da78 | 2012-01-12 19:42:58 -0600 | [diff] [blame] | 13 | #include <nand.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 14 | #include <dm/devres.h> |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 15 | |
| 16 | #include <linux/mtd/mtd.h> |
Masahiro Yamada | 2b7a873 | 2017-11-30 13:45:24 +0900 | [diff] [blame] | 17 | #include <linux/mtd/rawnand.h> |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 18 | #include <linux/mtd/nand_ecc.h> |
| 19 | |
| 20 | #include <asm/io.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 21 | #include <linux/errno.h> |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 22 | |
| 23 | #ifdef VERBOSE_DEBUG |
| 24 | #define DEBUG_ELBC |
| 25 | #define vdbg(format, arg...) printf("DEBUG: " format, ##arg) |
| 26 | #else |
| 27 | #define vdbg(format, arg...) do {} while (0) |
| 28 | #endif |
| 29 | |
| 30 | /* Can't use plain old DEBUG because the linux mtd |
| 31 | * headers define it as a macro. |
| 32 | */ |
| 33 | #ifdef DEBUG_ELBC |
| 34 | #define dbg(format, arg...) printf("DEBUG: " format, ##arg) |
| 35 | #else |
| 36 | #define dbg(format, arg...) do {} while (0) |
| 37 | #endif |
| 38 | |
| 39 | #define MAX_BANKS 8 |
| 40 | #define ERR_BYTE 0xFF /* Value returned for read bytes when read failed */ |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 41 | |
| 42 | #define LTESR_NAND_MASK (LTESR_FCT | LTESR_PAR | LTESR_CC) |
| 43 | |
| 44 | struct fsl_elbc_ctrl; |
| 45 | |
| 46 | /* mtd information per set */ |
| 47 | |
| 48 | struct fsl_elbc_mtd { |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 49 | struct nand_chip chip; |
| 50 | struct fsl_elbc_ctrl *ctrl; |
| 51 | |
| 52 | struct device *dev; |
| 53 | int bank; /* Chip select bank number */ |
| 54 | u8 __iomem *vbase; /* Chip select base virtual address */ |
| 55 | int page_size; /* NAND page size (0=512, 1=2048) */ |
| 56 | unsigned int fmr; /* FCM Flash Mode Register value */ |
| 57 | }; |
| 58 | |
| 59 | /* overview of the fsl elbc controller */ |
| 60 | |
| 61 | struct fsl_elbc_ctrl { |
| 62 | struct nand_hw_control controller; |
| 63 | struct fsl_elbc_mtd *chips[MAX_BANKS]; |
| 64 | |
| 65 | /* device info */ |
Becky Bruce | 0d4cee1 | 2010-06-17 11:37:20 -0500 | [diff] [blame] | 66 | fsl_lbc_t *regs; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 67 | u8 __iomem *addr; /* Address of assigned FCM buffer */ |
| 68 | unsigned int page; /* Last page written to / read from */ |
| 69 | unsigned int read_bytes; /* Number of bytes read during command */ |
| 70 | unsigned int column; /* Saved column from SEQIN */ |
| 71 | unsigned int index; /* Pointer to next byte to 'read' */ |
| 72 | unsigned int status; /* status read from LTESR after last op */ |
| 73 | unsigned int mdr; /* UPM/FCM Data Register value */ |
| 74 | unsigned int use_mdr; /* Non zero if the MDR is to be set */ |
| 75 | unsigned int oob; /* Non zero if operating on OOB data */ |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | /* These map to the positions used by the FCM hardware ECC generator */ |
| 79 | |
| 80 | /* Small Page FLASH with FMR[ECCM] = 0 */ |
| 81 | static struct nand_ecclayout fsl_elbc_oob_sp_eccm0 = { |
| 82 | .eccbytes = 3, |
| 83 | .eccpos = {6, 7, 8}, |
| 84 | .oobfree = { {0, 5}, {9, 7} }, |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | /* Small Page FLASH with FMR[ECCM] = 1 */ |
| 88 | static struct nand_ecclayout fsl_elbc_oob_sp_eccm1 = { |
| 89 | .eccbytes = 3, |
| 90 | .eccpos = {8, 9, 10}, |
| 91 | .oobfree = { {0, 5}, {6, 2}, {11, 5} }, |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | /* Large Page FLASH with FMR[ECCM] = 0 */ |
| 95 | static struct nand_ecclayout fsl_elbc_oob_lp_eccm0 = { |
| 96 | .eccbytes = 12, |
| 97 | .eccpos = {6, 7, 8, 22, 23, 24, 38, 39, 40, 54, 55, 56}, |
| 98 | .oobfree = { {1, 5}, {9, 13}, {25, 13}, {41, 13}, {57, 7} }, |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | /* Large Page FLASH with FMR[ECCM] = 1 */ |
| 102 | static struct nand_ecclayout fsl_elbc_oob_lp_eccm1 = { |
| 103 | .eccbytes = 12, |
| 104 | .eccpos = {8, 9, 10, 24, 25, 26, 40, 41, 42, 56, 57, 58}, |
| 105 | .oobfree = { {1, 7}, {11, 13}, {27, 13}, {43, 13}, {59, 5} }, |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 106 | }; |
| 107 | |
Anton Vorontsov | 5e0400f | 2008-06-27 23:04:04 +0400 | [diff] [blame] | 108 | /* |
| 109 | * fsl_elbc_oob_lp_eccm* specify that LP NAND's OOB free area starts at offset |
| 110 | * 1, so we have to adjust bad block pattern. This pattern should be used for |
| 111 | * x8 chips only. So far hardware does not support x16 chips anyway. |
| 112 | */ |
| 113 | static u8 scan_ff_pattern[] = { 0xff, }; |
| 114 | |
| 115 | static struct nand_bbt_descr largepage_memorybased = { |
| 116 | .options = 0, |
| 117 | .offs = 0, |
| 118 | .len = 1, |
| 119 | .pattern = scan_ff_pattern, |
| 120 | }; |
| 121 | |
Anton Vorontsov | d481b07 | 2008-06-27 23:04:13 +0400 | [diff] [blame] | 122 | /* |
| 123 | * ELBC may use HW ECC, so that OOB offsets, that NAND core uses for bbt, |
| 124 | * interfere with ECC positions, that's why we implement our own descriptors. |
| 125 | * OOB {11, 5}, works for both SP and LP chips, with ECCM = 1 and ECCM = 0. |
| 126 | */ |
| 127 | static u8 bbt_pattern[] = {'B', 'b', 't', '0' }; |
| 128 | static u8 mirror_pattern[] = {'1', 't', 'b', 'B' }; |
| 129 | |
| 130 | static struct nand_bbt_descr bbt_main_descr = { |
| 131 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE | |
| 132 | NAND_BBT_2BIT | NAND_BBT_VERSION, |
| 133 | .offs = 11, |
| 134 | .len = 4, |
| 135 | .veroffs = 15, |
| 136 | .maxblocks = 4, |
| 137 | .pattern = bbt_pattern, |
| 138 | }; |
| 139 | |
| 140 | static struct nand_bbt_descr bbt_mirror_descr = { |
| 141 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE | |
| 142 | NAND_BBT_2BIT | NAND_BBT_VERSION, |
| 143 | .offs = 11, |
| 144 | .len = 4, |
| 145 | .veroffs = 15, |
| 146 | .maxblocks = 4, |
| 147 | .pattern = mirror_pattern, |
| 148 | }; |
| 149 | |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 150 | /*=================================*/ |
| 151 | |
| 152 | /* |
| 153 | * Set up the FCM hardware block and page address fields, and the fcm |
| 154 | * structure addr field to point to the correct FCM buffer in memory |
| 155 | */ |
| 156 | static void set_addr(struct mtd_info *mtd, int column, int page_addr, int oob) |
| 157 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 158 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 159 | struct fsl_elbc_mtd *priv = nand_get_controller_data(chip); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 160 | struct fsl_elbc_ctrl *ctrl = priv->ctrl; |
Becky Bruce | 0d4cee1 | 2010-06-17 11:37:20 -0500 | [diff] [blame] | 161 | fsl_lbc_t *lbc = ctrl->regs; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 162 | int buf_num; |
| 163 | |
| 164 | ctrl->page = page_addr; |
| 165 | |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 166 | if (priv->page_size) { |
Scott Wood | f82f59b | 2008-05-22 15:02:46 -0500 | [diff] [blame] | 167 | out_be32(&lbc->fbar, page_addr >> 6); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 168 | out_be32(&lbc->fpar, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 169 | ((page_addr << FPAR_LP_PI_SHIFT) & FPAR_LP_PI) | |
| 170 | (oob ? FPAR_LP_MS : 0) | column); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 171 | buf_num = (page_addr & 1) << 2; |
| 172 | } else { |
Scott Wood | f82f59b | 2008-05-22 15:02:46 -0500 | [diff] [blame] | 173 | out_be32(&lbc->fbar, page_addr >> 5); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 174 | out_be32(&lbc->fpar, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 175 | ((page_addr << FPAR_SP_PI_SHIFT) & FPAR_SP_PI) | |
| 176 | (oob ? FPAR_SP_MS : 0) | column); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 177 | buf_num = page_addr & 7; |
| 178 | } |
| 179 | |
| 180 | ctrl->addr = priv->vbase + buf_num * 1024; |
| 181 | ctrl->index = column; |
| 182 | |
| 183 | /* for OOB data point to the second half of the buffer */ |
| 184 | if (oob) |
| 185 | ctrl->index += priv->page_size ? 2048 : 512; |
| 186 | |
| 187 | vdbg("set_addr: bank=%d, ctrl->addr=0x%p (0x%p), " |
| 188 | "index %x, pes %d ps %d\n", |
| 189 | buf_num, ctrl->addr, priv->vbase, ctrl->index, |
| 190 | chip->phys_erase_shift, chip->page_shift); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * execute FCM command and wait for it to complete |
| 195 | */ |
| 196 | static int fsl_elbc_run_command(struct mtd_info *mtd) |
| 197 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 198 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 199 | struct fsl_elbc_mtd *priv = nand_get_controller_data(chip); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 200 | struct fsl_elbc_ctrl *ctrl = priv->ctrl; |
Becky Bruce | 0d4cee1 | 2010-06-17 11:37:20 -0500 | [diff] [blame] | 201 | fsl_lbc_t *lbc = ctrl->regs; |
Prabhakar Kushwaha | d7550e1 | 2014-09-23 09:57:47 +0530 | [diff] [blame] | 202 | u32 timeo = (CONFIG_SYS_HZ * 10) / 1000; |
| 203 | u32 time_start; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 204 | u32 ltesr; |
| 205 | |
| 206 | /* Setup the FMR[OP] to execute without write protection */ |
| 207 | out_be32(&lbc->fmr, priv->fmr | 3); |
| 208 | if (ctrl->use_mdr) |
| 209 | out_be32(&lbc->mdr, ctrl->mdr); |
| 210 | |
| 211 | vdbg("fsl_elbc_run_command: fmr=%08x fir=%08x fcr=%08x\n", |
| 212 | in_be32(&lbc->fmr), in_be32(&lbc->fir), in_be32(&lbc->fcr)); |
| 213 | vdbg("fsl_elbc_run_command: fbar=%08x fpar=%08x " |
| 214 | "fbcr=%08x bank=%d\n", |
| 215 | in_be32(&lbc->fbar), in_be32(&lbc->fpar), |
| 216 | in_be32(&lbc->fbcr), priv->bank); |
| 217 | |
| 218 | /* execute special operation */ |
| 219 | out_be32(&lbc->lsor, priv->bank); |
| 220 | |
| 221 | /* wait for FCM complete flag or timeout */ |
Prabhakar Kushwaha | d7550e1 | 2014-09-23 09:57:47 +0530 | [diff] [blame] | 222 | time_start = get_timer(0); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 223 | |
| 224 | ltesr = 0; |
Prabhakar Kushwaha | d7550e1 | 2014-09-23 09:57:47 +0530 | [diff] [blame] | 225 | while (get_timer(time_start) < timeo) { |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 226 | ltesr = in_be32(&lbc->ltesr); |
| 227 | if (ltesr & LTESR_CC) |
| 228 | break; |
| 229 | } |
| 230 | |
| 231 | ctrl->status = ltesr & LTESR_NAND_MASK; |
| 232 | out_be32(&lbc->ltesr, ctrl->status); |
| 233 | out_be32(&lbc->lteatr, 0); |
| 234 | |
| 235 | /* store mdr value in case it was needed */ |
| 236 | if (ctrl->use_mdr) |
| 237 | ctrl->mdr = in_be32(&lbc->mdr); |
| 238 | |
| 239 | ctrl->use_mdr = 0; |
| 240 | |
| 241 | vdbg("fsl_elbc_run_command: stat=%08x mdr=%08x fmr=%08x\n", |
| 242 | ctrl->status, ctrl->mdr, in_be32(&lbc->fmr)); |
| 243 | |
| 244 | /* returns 0 on success otherwise non-zero) */ |
| 245 | return ctrl->status == LTESR_CC ? 0 : -EIO; |
| 246 | } |
| 247 | |
| 248 | static void fsl_elbc_do_read(struct nand_chip *chip, int oob) |
| 249 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 250 | struct fsl_elbc_mtd *priv = nand_get_controller_data(chip); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 251 | struct fsl_elbc_ctrl *ctrl = priv->ctrl; |
Becky Bruce | 0d4cee1 | 2010-06-17 11:37:20 -0500 | [diff] [blame] | 252 | fsl_lbc_t *lbc = ctrl->regs; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 253 | |
| 254 | if (priv->page_size) { |
| 255 | out_be32(&lbc->fir, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 256 | (FIR_OP_CW0 << FIR_OP0_SHIFT) | |
| 257 | (FIR_OP_CA << FIR_OP1_SHIFT) | |
| 258 | (FIR_OP_PA << FIR_OP2_SHIFT) | |
| 259 | (FIR_OP_CW1 << FIR_OP3_SHIFT) | |
| 260 | (FIR_OP_RBW << FIR_OP4_SHIFT)); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 261 | |
| 262 | out_be32(&lbc->fcr, (NAND_CMD_READ0 << FCR_CMD0_SHIFT) | |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 263 | (NAND_CMD_READSTART << FCR_CMD1_SHIFT)); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 264 | } else { |
| 265 | out_be32(&lbc->fir, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 266 | (FIR_OP_CW0 << FIR_OP0_SHIFT) | |
| 267 | (FIR_OP_CA << FIR_OP1_SHIFT) | |
| 268 | (FIR_OP_PA << FIR_OP2_SHIFT) | |
| 269 | (FIR_OP_RBW << FIR_OP3_SHIFT)); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 270 | |
| 271 | if (oob) |
| 272 | out_be32(&lbc->fcr, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 273 | NAND_CMD_READOOB << FCR_CMD0_SHIFT); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 274 | else |
| 275 | out_be32(&lbc->fcr, NAND_CMD_READ0 << FCR_CMD0_SHIFT); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /* cmdfunc send commands to the FCM */ |
| 280 | static void fsl_elbc_cmdfunc(struct mtd_info *mtd, unsigned int command, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 281 | int column, int page_addr) |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 282 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 283 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 284 | struct fsl_elbc_mtd *priv = nand_get_controller_data(chip); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 285 | struct fsl_elbc_ctrl *ctrl = priv->ctrl; |
Becky Bruce | 0d4cee1 | 2010-06-17 11:37:20 -0500 | [diff] [blame] | 286 | fsl_lbc_t *lbc = ctrl->regs; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 287 | |
| 288 | ctrl->use_mdr = 0; |
| 289 | |
| 290 | /* clear the read buffer */ |
| 291 | ctrl->read_bytes = 0; |
| 292 | if (command != NAND_CMD_PAGEPROG) |
| 293 | ctrl->index = 0; |
| 294 | |
| 295 | switch (command) { |
| 296 | /* READ0 and READ1 read the entire buffer to use hardware ECC. */ |
| 297 | case NAND_CMD_READ1: |
| 298 | column += 256; |
| 299 | |
| 300 | /* fall-through */ |
| 301 | case NAND_CMD_READ0: |
| 302 | vdbg("fsl_elbc_cmdfunc: NAND_CMD_READ0, page_addr:" |
| 303 | " 0x%x, column: 0x%x.\n", page_addr, column); |
| 304 | |
| 305 | out_be32(&lbc->fbcr, 0); /* read entire page to enable ECC */ |
| 306 | set_addr(mtd, 0, page_addr, 0); |
| 307 | |
| 308 | ctrl->read_bytes = mtd->writesize + mtd->oobsize; |
| 309 | ctrl->index += column; |
| 310 | |
| 311 | fsl_elbc_do_read(chip, 0); |
| 312 | fsl_elbc_run_command(mtd); |
| 313 | return; |
| 314 | |
Pali Rohár | 2ef8228 | 2022-04-04 18:17:18 +0200 | [diff] [blame^] | 315 | /* RNDOUT moves the pointer inside the page */ |
| 316 | case NAND_CMD_RNDOUT: |
| 317 | vdbg("fsl_elbc_cmdfunc: NAND_CMD_RNDOUT, column: 0x%x.\n", |
| 318 | column); |
| 319 | |
| 320 | ctrl->index = column; |
| 321 | return; |
| 322 | |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 323 | /* READOOB reads only the OOB because no ECC is performed. */ |
| 324 | case NAND_CMD_READOOB: |
| 325 | vdbg("fsl_elbc_cmdfunc: NAND_CMD_READOOB, page_addr:" |
| 326 | " 0x%x, column: 0x%x.\n", page_addr, column); |
| 327 | |
| 328 | out_be32(&lbc->fbcr, mtd->oobsize - column); |
| 329 | set_addr(mtd, column, page_addr, 1); |
| 330 | |
| 331 | ctrl->read_bytes = mtd->writesize + mtd->oobsize; |
| 332 | |
| 333 | fsl_elbc_do_read(chip, 1); |
| 334 | fsl_elbc_run_command(mtd); |
| 335 | |
| 336 | return; |
| 337 | |
| 338 | /* READID must read all 5 possible bytes while CEB is active */ |
| 339 | case NAND_CMD_READID: |
Shengzhou Liu | e963502 | 2011-12-12 17:49:57 +0800 | [diff] [blame] | 340 | case NAND_CMD_PARAM: |
| 341 | vdbg("fsl_elbc_cmdfunc: NAND_CMD 0x%x.\n", command); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 342 | |
| 343 | out_be32(&lbc->fir, (FIR_OP_CW0 << FIR_OP0_SHIFT) | |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 344 | (FIR_OP_UA << FIR_OP1_SHIFT) | |
| 345 | (FIR_OP_RBW << FIR_OP2_SHIFT)); |
Shengzhou Liu | e963502 | 2011-12-12 17:49:57 +0800 | [diff] [blame] | 346 | out_be32(&lbc->fcr, command << FCR_CMD0_SHIFT); |
| 347 | /* |
| 348 | * although currently it's 8 bytes for READID, we always read |
| 349 | * the maximum 256 bytes(for PARAM) |
| 350 | */ |
| 351 | out_be32(&lbc->fbcr, 256); |
| 352 | ctrl->read_bytes = 256; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 353 | ctrl->use_mdr = 1; |
Shengzhou Liu | e963502 | 2011-12-12 17:49:57 +0800 | [diff] [blame] | 354 | ctrl->mdr = column; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 355 | set_addr(mtd, 0, 0, 0); |
| 356 | fsl_elbc_run_command(mtd); |
| 357 | return; |
| 358 | |
| 359 | /* ERASE1 stores the block and page address */ |
| 360 | case NAND_CMD_ERASE1: |
| 361 | vdbg("fsl_elbc_cmdfunc: NAND_CMD_ERASE1, " |
| 362 | "page_addr: 0x%x.\n", page_addr); |
| 363 | set_addr(mtd, 0, page_addr, 0); |
| 364 | return; |
| 365 | |
| 366 | /* ERASE2 uses the block and page address from ERASE1 */ |
| 367 | case NAND_CMD_ERASE2: |
| 368 | vdbg("fsl_elbc_cmdfunc: NAND_CMD_ERASE2.\n"); |
| 369 | |
| 370 | out_be32(&lbc->fir, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 371 | (FIR_OP_CW0 << FIR_OP0_SHIFT) | |
| 372 | (FIR_OP_PA << FIR_OP1_SHIFT) | |
| 373 | (FIR_OP_CM1 << FIR_OP2_SHIFT)); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 374 | |
| 375 | out_be32(&lbc->fcr, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 376 | (NAND_CMD_ERASE1 << FCR_CMD0_SHIFT) | |
| 377 | (NAND_CMD_ERASE2 << FCR_CMD1_SHIFT)); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 378 | |
| 379 | out_be32(&lbc->fbcr, 0); |
| 380 | ctrl->read_bytes = 0; |
| 381 | |
| 382 | fsl_elbc_run_command(mtd); |
| 383 | return; |
| 384 | |
| 385 | /* SEQIN sets up the addr buffer and all registers except the length */ |
| 386 | case NAND_CMD_SEQIN: { |
| 387 | u32 fcr; |
| 388 | vdbg("fsl_elbc_cmdfunc: NAND_CMD_SEQIN/PAGE_PROG, " |
| 389 | "page_addr: 0x%x, column: 0x%x.\n", |
| 390 | page_addr, column); |
| 391 | |
| 392 | ctrl->column = column; |
| 393 | ctrl->oob = 0; |
| 394 | |
| 395 | if (priv->page_size) { |
| 396 | fcr = (NAND_CMD_SEQIN << FCR_CMD0_SHIFT) | |
| 397 | (NAND_CMD_PAGEPROG << FCR_CMD1_SHIFT); |
| 398 | |
| 399 | out_be32(&lbc->fir, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 400 | (FIR_OP_CW0 << FIR_OP0_SHIFT) | |
| 401 | (FIR_OP_CA << FIR_OP1_SHIFT) | |
| 402 | (FIR_OP_PA << FIR_OP2_SHIFT) | |
| 403 | (FIR_OP_WB << FIR_OP3_SHIFT) | |
| 404 | (FIR_OP_CW1 << FIR_OP4_SHIFT)); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 405 | } else { |
| 406 | fcr = (NAND_CMD_PAGEPROG << FCR_CMD1_SHIFT) | |
| 407 | (NAND_CMD_SEQIN << FCR_CMD2_SHIFT); |
| 408 | |
| 409 | out_be32(&lbc->fir, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 410 | (FIR_OP_CW0 << FIR_OP0_SHIFT) | |
| 411 | (FIR_OP_CM2 << FIR_OP1_SHIFT) | |
| 412 | (FIR_OP_CA << FIR_OP2_SHIFT) | |
| 413 | (FIR_OP_PA << FIR_OP3_SHIFT) | |
| 414 | (FIR_OP_WB << FIR_OP4_SHIFT) | |
| 415 | (FIR_OP_CW1 << FIR_OP5_SHIFT)); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 416 | |
| 417 | if (column >= mtd->writesize) { |
| 418 | /* OOB area --> READOOB */ |
| 419 | column -= mtd->writesize; |
| 420 | fcr |= NAND_CMD_READOOB << FCR_CMD0_SHIFT; |
| 421 | ctrl->oob = 1; |
| 422 | } else if (column < 256) { |
| 423 | /* First 256 bytes --> READ0 */ |
| 424 | fcr |= NAND_CMD_READ0 << FCR_CMD0_SHIFT; |
| 425 | } else { |
| 426 | /* Second 256 bytes --> READ1 */ |
| 427 | fcr |= NAND_CMD_READ1 << FCR_CMD0_SHIFT; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | out_be32(&lbc->fcr, fcr); |
| 432 | set_addr(mtd, column, page_addr, ctrl->oob); |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | /* PAGEPROG reuses all of the setup from SEQIN and adds the length */ |
| 437 | case NAND_CMD_PAGEPROG: { |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 438 | vdbg("fsl_elbc_cmdfunc: NAND_CMD_PAGEPROG " |
| 439 | "writing %d bytes.\n", ctrl->index); |
| 440 | |
| 441 | /* if the write did not start at 0 or is not a full page |
| 442 | * then set the exact length, otherwise use a full page |
| 443 | * write so the HW generates the ECC. |
| 444 | */ |
| 445 | if (ctrl->oob || ctrl->column != 0 || |
mhench | 05f1b8b | 2011-07-11 12:29:43 +0000 | [diff] [blame] | 446 | ctrl->index != mtd->writesize + mtd->oobsize) |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 447 | out_be32(&lbc->fbcr, ctrl->index); |
mhench | 05f1b8b | 2011-07-11 12:29:43 +0000 | [diff] [blame] | 448 | else |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 449 | out_be32(&lbc->fbcr, 0); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 450 | |
| 451 | fsl_elbc_run_command(mtd); |
| 452 | |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | |
| 456 | /* CMD_STATUS must read the status byte while CEB is active */ |
| 457 | /* Note - it does not wait for the ready line */ |
| 458 | case NAND_CMD_STATUS: |
| 459 | out_be32(&lbc->fir, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 460 | (FIR_OP_CM0 << FIR_OP0_SHIFT) | |
| 461 | (FIR_OP_RBW << FIR_OP1_SHIFT)); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 462 | out_be32(&lbc->fcr, NAND_CMD_STATUS << FCR_CMD0_SHIFT); |
| 463 | out_be32(&lbc->fbcr, 1); |
| 464 | set_addr(mtd, 0, 0, 0); |
| 465 | ctrl->read_bytes = 1; |
| 466 | |
| 467 | fsl_elbc_run_command(mtd); |
| 468 | |
| 469 | /* The chip always seems to report that it is |
| 470 | * write-protected, even when it is not. |
| 471 | */ |
| 472 | out_8(ctrl->addr, in_8(ctrl->addr) | NAND_STATUS_WP); |
| 473 | return; |
| 474 | |
| 475 | /* RESET without waiting for the ready line */ |
| 476 | case NAND_CMD_RESET: |
| 477 | dbg("fsl_elbc_cmdfunc: NAND_CMD_RESET.\n"); |
| 478 | out_be32(&lbc->fir, FIR_OP_CM0 << FIR_OP0_SHIFT); |
| 479 | out_be32(&lbc->fcr, NAND_CMD_RESET << FCR_CMD0_SHIFT); |
| 480 | fsl_elbc_run_command(mtd); |
| 481 | return; |
| 482 | |
| 483 | default: |
| 484 | printf("fsl_elbc_cmdfunc: error, unsupported command 0x%x.\n", |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 485 | command); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
| 489 | static void fsl_elbc_select_chip(struct mtd_info *mtd, int chip) |
| 490 | { |
| 491 | /* The hardware does not seem to support multiple |
| 492 | * chips per bank. |
| 493 | */ |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | * Write buf to the FCM Controller Data Buffer |
| 498 | */ |
| 499 | static void fsl_elbc_write_buf(struct mtd_info *mtd, const u8 *buf, int len) |
| 500 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 501 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 502 | struct fsl_elbc_mtd *priv = nand_get_controller_data(chip); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 503 | struct fsl_elbc_ctrl *ctrl = priv->ctrl; |
| 504 | unsigned int bufsize = mtd->writesize + mtd->oobsize; |
| 505 | |
Anton Vorontsov | 42f1563 | 2008-03-28 22:10:54 +0300 | [diff] [blame] | 506 | if (len <= 0) { |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 507 | printf("write_buf of %d bytes", len); |
| 508 | ctrl->status = 0; |
| 509 | return; |
| 510 | } |
| 511 | |
| 512 | if ((unsigned int)len > bufsize - ctrl->index) { |
| 513 | printf("write_buf beyond end of buffer " |
| 514 | "(%d requested, %u available)\n", |
| 515 | len, bufsize - ctrl->index); |
| 516 | len = bufsize - ctrl->index; |
| 517 | } |
| 518 | |
| 519 | memcpy_toio(&ctrl->addr[ctrl->index], buf, len); |
Anton Vorontsov | 42f1563 | 2008-03-28 22:10:54 +0300 | [diff] [blame] | 520 | /* |
| 521 | * This is workaround for the weird elbc hangs during nand write, |
| 522 | * Scott Wood says: "...perhaps difference in how long it takes a |
| 523 | * write to make it through the localbus compared to a write to IMMR |
| 524 | * is causing problems, and sync isn't helping for some reason." |
| 525 | * Reading back the last byte helps though. |
| 526 | */ |
| 527 | in_8(&ctrl->addr[ctrl->index] + len - 1); |
| 528 | |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 529 | ctrl->index += len; |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * read a byte from either the FCM hardware buffer if it has any data left |
| 534 | * otherwise issue a command to read a single byte. |
| 535 | */ |
| 536 | static u8 fsl_elbc_read_byte(struct mtd_info *mtd) |
| 537 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 538 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 539 | struct fsl_elbc_mtd *priv = nand_get_controller_data(chip); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 540 | struct fsl_elbc_ctrl *ctrl = priv->ctrl; |
| 541 | |
| 542 | /* If there are still bytes in the FCM, then use the next byte. */ |
| 543 | if (ctrl->index < ctrl->read_bytes) |
| 544 | return in_8(&ctrl->addr[ctrl->index++]); |
| 545 | |
| 546 | printf("read_byte beyond end of buffer\n"); |
| 547 | return ERR_BYTE; |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * Read from the FCM Controller Data Buffer |
| 552 | */ |
| 553 | static void fsl_elbc_read_buf(struct mtd_info *mtd, u8 *buf, int len) |
| 554 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 555 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 556 | struct fsl_elbc_mtd *priv = nand_get_controller_data(chip); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 557 | struct fsl_elbc_ctrl *ctrl = priv->ctrl; |
| 558 | int avail; |
| 559 | |
| 560 | if (len < 0) |
| 561 | return; |
| 562 | |
| 563 | avail = min((unsigned int)len, ctrl->read_bytes - ctrl->index); |
| 564 | memcpy_fromio(buf, &ctrl->addr[ctrl->index], avail); |
| 565 | ctrl->index += avail; |
| 566 | |
| 567 | if (len > avail) |
| 568 | printf("read_buf beyond end of buffer " |
| 569 | "(%d requested, %d available)\n", |
| 570 | len, avail); |
| 571 | } |
| 572 | |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 573 | /* This function is called after Program and Erase Operations to |
| 574 | * check for success or failure. |
| 575 | */ |
| 576 | static int fsl_elbc_wait(struct mtd_info *mtd, struct nand_chip *chip) |
| 577 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 578 | struct fsl_elbc_mtd *priv = nand_get_controller_data(chip); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 579 | struct fsl_elbc_ctrl *ctrl = priv->ctrl; |
Becky Bruce | 0d4cee1 | 2010-06-17 11:37:20 -0500 | [diff] [blame] | 580 | fsl_lbc_t *lbc = ctrl->regs; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 581 | |
| 582 | if (ctrl->status != LTESR_CC) |
| 583 | return NAND_STATUS_FAIL; |
| 584 | |
| 585 | /* Use READ_STATUS command, but wait for the device to be ready */ |
| 586 | ctrl->use_mdr = 0; |
| 587 | out_be32(&lbc->fir, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 588 | (FIR_OP_CW0 << FIR_OP0_SHIFT) | |
| 589 | (FIR_OP_RBW << FIR_OP1_SHIFT)); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 590 | out_be32(&lbc->fcr, NAND_CMD_STATUS << FCR_CMD0_SHIFT); |
| 591 | out_be32(&lbc->fbcr, 1); |
| 592 | set_addr(mtd, 0, 0, 0); |
| 593 | ctrl->read_bytes = 1; |
| 594 | |
| 595 | fsl_elbc_run_command(mtd); |
| 596 | |
| 597 | if (ctrl->status != LTESR_CC) |
| 598 | return NAND_STATUS_FAIL; |
| 599 | |
| 600 | /* The chip always seems to report that it is |
| 601 | * write-protected, even when it is not. |
| 602 | */ |
| 603 | out_8(ctrl->addr, in_8(ctrl->addr) | NAND_STATUS_WP); |
| 604 | return fsl_elbc_read_byte(mtd); |
| 605 | } |
| 606 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 607 | static int fsl_elbc_read_page(struct mtd_info *mtd, struct nand_chip *chip, |
| 608 | uint8_t *buf, int oob_required, int page) |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 609 | { |
| 610 | fsl_elbc_read_buf(mtd, buf, mtd->writesize); |
| 611 | fsl_elbc_read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 612 | |
| 613 | if (fsl_elbc_wait(mtd, chip) & NAND_STATUS_FAIL) |
| 614 | mtd->ecc_stats.failed++; |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | /* ECC will be calculated automatically, and errors will be detected in |
| 620 | * waitfunc. |
| 621 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 622 | static int fsl_elbc_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 623 | const uint8_t *buf, int oob_required, |
| 624 | int page) |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 625 | { |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 626 | fsl_elbc_write_buf(mtd, buf, mtd->writesize); |
| 627 | fsl_elbc_write_buf(mtd, chip->oob_poi, mtd->oobsize); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 628 | |
| 629 | return 0; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | static struct fsl_elbc_ctrl *elbc_ctrl; |
| 633 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 634 | /* ECC will be calculated automatically, and errors will be detected in |
| 635 | * waitfunc. |
| 636 | */ |
| 637 | static int fsl_elbc_write_subpage(struct mtd_info *mtd, struct nand_chip *chip, |
| 638 | uint32_t offset, uint32_t data_len, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 639 | const uint8_t *buf, int oob_required, int page) |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 640 | { |
| 641 | fsl_elbc_write_buf(mtd, buf, mtd->writesize); |
| 642 | fsl_elbc_write_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 643 | |
| 644 | return 0; |
| 645 | } |
| 646 | |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 647 | static void fsl_elbc_ctrl_init(void) |
| 648 | { |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 649 | elbc_ctrl = kzalloc(sizeof(*elbc_ctrl), GFP_KERNEL); |
| 650 | if (!elbc_ctrl) |
| 651 | return; |
| 652 | |
Becky Bruce | 0d4cee1 | 2010-06-17 11:37:20 -0500 | [diff] [blame] | 653 | elbc_ctrl->regs = LBC_BASE_ADDR; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 654 | |
| 655 | /* clear event registers */ |
| 656 | out_be32(&elbc_ctrl->regs->ltesr, LTESR_NAND_MASK); |
| 657 | out_be32(&elbc_ctrl->regs->lteatr, 0); |
| 658 | |
| 659 | /* Enable interrupts for any detected events */ |
| 660 | out_be32(&elbc_ctrl->regs->lteir, LTESR_NAND_MASK); |
| 661 | |
| 662 | elbc_ctrl->read_bytes = 0; |
| 663 | elbc_ctrl->index = 0; |
| 664 | elbc_ctrl->addr = NULL; |
| 665 | } |
| 666 | |
Scott Wood | ea0da78 | 2012-01-12 19:42:58 -0600 | [diff] [blame] | 667 | static int fsl_elbc_chip_init(int devnum, u8 *addr) |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 668 | { |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 669 | struct mtd_info *mtd; |
Scott Wood | ea0da78 | 2012-01-12 19:42:58 -0600 | [diff] [blame] | 670 | struct nand_chip *nand; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 671 | struct fsl_elbc_mtd *priv; |
Kumar Gala | e58c839 | 2008-12-16 14:59:22 -0600 | [diff] [blame] | 672 | uint32_t br = 0, or = 0; |
Scott Wood | ea0da78 | 2012-01-12 19:42:58 -0600 | [diff] [blame] | 673 | int ret; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 674 | |
| 675 | if (!elbc_ctrl) { |
| 676 | fsl_elbc_ctrl_init(); |
| 677 | if (!elbc_ctrl) |
| 678 | return -1; |
| 679 | } |
| 680 | |
| 681 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 682 | if (!priv) |
| 683 | return -ENOMEM; |
| 684 | |
| 685 | priv->ctrl = elbc_ctrl; |
Scott Wood | ea0da78 | 2012-01-12 19:42:58 -0600 | [diff] [blame] | 686 | priv->vbase = addr; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 687 | |
| 688 | /* Find which chip select it is connected to. It'd be nice |
| 689 | * if we could pass more than one datum to the NAND driver... |
| 690 | */ |
| 691 | for (priv->bank = 0; priv->bank < MAX_BANKS; priv->bank++) { |
Scott Wood | ea0da78 | 2012-01-12 19:42:58 -0600 | [diff] [blame] | 692 | phys_addr_t phys_addr = virt_to_phys(addr); |
Kumar Gala | e58c839 | 2008-12-16 14:59:22 -0600 | [diff] [blame] | 693 | |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 694 | br = in_be32(&elbc_ctrl->regs->bank[priv->bank].br); |
| 695 | or = in_be32(&elbc_ctrl->regs->bank[priv->bank].or); |
| 696 | |
| 697 | if ((br & BR_V) && (br & BR_MSEL) == BR_MS_FCM && |
Scott Wood | ea0da78 | 2012-01-12 19:42:58 -0600 | [diff] [blame] | 698 | (br & or & BR_BA) == BR_PHYS_ADDR(phys_addr)) |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 699 | break; |
| 700 | } |
| 701 | |
| 702 | if (priv->bank >= MAX_BANKS) { |
| 703 | printf("fsl_elbc_nand: address did not match any " |
| 704 | "chip selects\n"); |
Raghav Dogra | 6c90d98 | 2015-05-20 14:54:58 +0530 | [diff] [blame] | 705 | kfree(priv); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 706 | return -ENODEV; |
| 707 | } |
| 708 | |
Scott Wood | ea0da78 | 2012-01-12 19:42:58 -0600 | [diff] [blame] | 709 | nand = &priv->chip; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 710 | mtd = nand_to_mtd(nand); |
Scott Wood | ea0da78 | 2012-01-12 19:42:58 -0600 | [diff] [blame] | 711 | |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 712 | elbc_ctrl->chips[priv->bank] = priv; |
| 713 | |
| 714 | /* fill in nand_chip structure */ |
| 715 | /* set up function call table */ |
| 716 | nand->read_byte = fsl_elbc_read_byte; |
| 717 | nand->write_buf = fsl_elbc_write_buf; |
| 718 | nand->read_buf = fsl_elbc_read_buf; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 719 | nand->select_chip = fsl_elbc_select_chip; |
| 720 | nand->cmdfunc = fsl_elbc_cmdfunc; |
| 721 | nand->waitfunc = fsl_elbc_wait; |
| 722 | |
| 723 | /* set up nand options */ |
Anton Vorontsov | d481b07 | 2008-06-27 23:04:13 +0400 | [diff] [blame] | 724 | nand->bbt_td = &bbt_main_descr; |
| 725 | nand->bbt_md = &bbt_mirror_descr; |
| 726 | |
Wolfgang Denk | 62fb2b4 | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 727 | /* set up nand options */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 728 | nand->options = NAND_NO_SUBPAGE_WRITE; |
| 729 | nand->bbt_options = NAND_BBT_USE_FLASH; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 730 | |
| 731 | nand->controller = &elbc_ctrl->controller; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 732 | nand_set_controller_data(nand, priv); |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 733 | |
| 734 | nand->ecc.read_page = fsl_elbc_read_page; |
| 735 | nand->ecc.write_page = fsl_elbc_write_page; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 736 | nand->ecc.write_subpage = fsl_elbc_write_subpage; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 737 | |
Scott Wood | c500d76 | 2008-10-29 13:42:41 -0500 | [diff] [blame] | 738 | priv->fmr = (15 << FMR_CWTO_SHIFT) | (2 << FMR_AL_SHIFT); |
| 739 | |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 740 | /* If CS Base Register selects full hardware ECC then use it */ |
| 741 | if ((br & BR_DECC) == BR_DECC_CHK_GEN) { |
| 742 | nand->ecc.mode = NAND_ECC_HW; |
| 743 | |
| 744 | nand->ecc.layout = (priv->fmr & FMR_ECCM) ? |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 745 | &fsl_elbc_oob_sp_eccm1 : |
| 746 | &fsl_elbc_oob_sp_eccm0; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 747 | |
| 748 | nand->ecc.size = 512; |
| 749 | nand->ecc.bytes = 3; |
| 750 | nand->ecc.steps = 1; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 751 | nand->ecc.strength = 1; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 752 | } else { |
Valentin Longchamp | cbb6566 | 2013-10-18 11:47:22 +0200 | [diff] [blame] | 753 | /* otherwise fall back to software ECC */ |
| 754 | #if defined(CONFIG_NAND_ECC_BCH) |
| 755 | nand->ecc.mode = NAND_ECC_SOFT_BCH; |
| 756 | #else |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 757 | nand->ecc.mode = NAND_ECC_SOFT; |
Valentin Longchamp | cbb6566 | 2013-10-18 11:47:22 +0200 | [diff] [blame] | 758 | #endif |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 759 | } |
| 760 | |
Scott Wood | 8bd2874 | 2013-02-26 13:00:50 +0000 | [diff] [blame] | 761 | ret = nand_scan_ident(mtd, 1, NULL); |
| 762 | if (ret) |
| 763 | return ret; |
| 764 | |
Anton Vorontsov | 5e0400f | 2008-06-27 23:04:04 +0400 | [diff] [blame] | 765 | /* Large-page-specific setup */ |
Scott Wood | 8bd2874 | 2013-02-26 13:00:50 +0000 | [diff] [blame] | 766 | if (mtd->writesize == 2048) { |
| 767 | setbits_be32(&elbc_ctrl->regs->bank[priv->bank].or, |
| 768 | OR_FCM_PGS); |
| 769 | in_be32(&elbc_ctrl->regs->bank[priv->bank].or); |
| 770 | |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 771 | priv->page_size = 1; |
Anton Vorontsov | 5e0400f | 2008-06-27 23:04:04 +0400 | [diff] [blame] | 772 | nand->badblock_pattern = &largepage_memorybased; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 773 | |
Scott Wood | 8bd2874 | 2013-02-26 13:00:50 +0000 | [diff] [blame] | 774 | /* |
| 775 | * Hardware expects small page has ECCM0, large page has |
| 776 | * ECCM1 when booting from NAND, and we follow that even |
| 777 | * when not booting from NAND. |
| 778 | */ |
| 779 | priv->fmr |= FMR_ECCM; |
| 780 | |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 781 | /* adjust ecc setup if needed */ |
| 782 | if ((br & BR_DECC) == BR_DECC_CHK_GEN) { |
| 783 | nand->ecc.steps = 4; |
| 784 | nand->ecc.layout = (priv->fmr & FMR_ECCM) ? |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 785 | &fsl_elbc_oob_lp_eccm1 : |
| 786 | &fsl_elbc_oob_lp_eccm0; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 787 | } |
Scott Wood | 8bd2874 | 2013-02-26 13:00:50 +0000 | [diff] [blame] | 788 | } else if (mtd->writesize == 512) { |
| 789 | clrbits_be32(&elbc_ctrl->regs->bank[priv->bank].or, |
| 790 | OR_FCM_PGS); |
| 791 | in_be32(&elbc_ctrl->regs->bank[priv->bank].or); |
| 792 | } else { |
| 793 | return -ENODEV; |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 794 | } |
| 795 | |
Scott Wood | ea0da78 | 2012-01-12 19:42:58 -0600 | [diff] [blame] | 796 | ret = nand_scan_tail(mtd); |
| 797 | if (ret) |
| 798 | return ret; |
| 799 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 800 | ret = nand_register(devnum, mtd); |
Scott Wood | ea0da78 | 2012-01-12 19:42:58 -0600 | [diff] [blame] | 801 | if (ret) |
| 802 | return ret; |
| 803 | |
Scott Wood | 40c6e09 | 2008-03-21 16:12:51 -0500 | [diff] [blame] | 804 | return 0; |
| 805 | } |
Scott Wood | ea0da78 | 2012-01-12 19:42:58 -0600 | [diff] [blame] | 806 | |
| 807 | #ifndef CONFIG_SYS_NAND_BASE_LIST |
| 808 | #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE } |
| 809 | #endif |
| 810 | |
| 811 | static unsigned long base_address[CONFIG_SYS_MAX_NAND_DEVICE] = |
| 812 | CONFIG_SYS_NAND_BASE_LIST; |
| 813 | |
| 814 | void board_nand_init(void) |
| 815 | { |
| 816 | int i; |
| 817 | |
| 818 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) |
| 819 | fsl_elbc_chip_init(i, (u8 *)base_address[i]); |
| 820 | } |