Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Freescale Integrated Flash Controller NAND driver |
| 3 | * |
| 4 | * Copyright 2011-2012 Freescale Semiconductor, Inc |
| 5 | * |
| 6 | * Author: Dipen Dudhat <Dipen.Dudhat@freescale.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/types.h> |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 25 | #include <linux/kernel.h> |
Rob Herring | 5af5073 | 2013-09-17 14:28:33 -0500 | [diff] [blame] | 26 | #include <linux/of_address.h> |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 27 | #include <linux/slab.h> |
| 28 | #include <linux/mtd/mtd.h> |
| 29 | #include <linux/mtd/nand.h> |
| 30 | #include <linux/mtd/partitions.h> |
| 31 | #include <linux/mtd/nand_ecc.h> |
Prabhakar Kushwaha | d2ae2e2 | 2014-01-17 11:15:16 +0530 | [diff] [blame] | 32 | #include <linux/fsl_ifc.h> |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 33 | |
| 34 | #define ERR_BYTE 0xFF /* Value returned for read |
| 35 | bytes when read failed */ |
| 36 | #define IFC_TIMEOUT_MSECS 500 /* Maximum number of mSecs to wait |
| 37 | for IFC NAND Machine */ |
| 38 | |
| 39 | struct fsl_ifc_ctrl; |
| 40 | |
| 41 | /* mtd information per set */ |
| 42 | struct fsl_ifc_mtd { |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 43 | struct nand_chip chip; |
| 44 | struct fsl_ifc_ctrl *ctrl; |
| 45 | |
| 46 | struct device *dev; |
| 47 | int bank; /* Chip select bank number */ |
| 48 | unsigned int bufnum_mask; /* bufnum = page & bufnum_mask */ |
| 49 | u8 __iomem *vbase; /* Chip select base virtual address */ |
| 50 | }; |
| 51 | |
| 52 | /* overview of the fsl ifc controller */ |
| 53 | struct fsl_ifc_nand_ctrl { |
| 54 | struct nand_hw_control controller; |
| 55 | struct fsl_ifc_mtd *chips[FSL_IFC_BANK_COUNT]; |
| 56 | |
Aaron Sierra | 4454406 | 2014-04-07 11:58:12 -0500 | [diff] [blame] | 57 | void __iomem *addr; /* Address of assigned IFC buffer */ |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 58 | unsigned int page; /* Last page written to / read from */ |
| 59 | unsigned int read_bytes;/* Number of bytes read during command */ |
| 60 | unsigned int column; /* Saved column from SEQIN */ |
| 61 | unsigned int index; /* Pointer to next byte to 'read' */ |
| 62 | unsigned int oob; /* Non zero if operating on OOB data */ |
| 63 | unsigned int eccread; /* Non zero for a full-page ECC read */ |
| 64 | unsigned int counter; /* counter for the initializations */ |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 65 | unsigned int max_bitflips; /* Saved during READ0 cmd */ |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | static struct fsl_ifc_nand_ctrl *ifc_nand_ctrl; |
| 69 | |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 70 | /* |
| 71 | * Generic flash bbt descriptors |
| 72 | */ |
| 73 | static u8 bbt_pattern[] = {'B', 'b', 't', '0' }; |
| 74 | static u8 mirror_pattern[] = {'1', 't', 'b', 'B' }; |
| 75 | |
| 76 | static struct nand_bbt_descr bbt_main_descr = { |
| 77 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE | |
| 78 | NAND_BBT_2BIT | NAND_BBT_VERSION, |
| 79 | .offs = 2, /* 0 on 8-bit small page */ |
| 80 | .len = 4, |
| 81 | .veroffs = 6, |
| 82 | .maxblocks = 4, |
| 83 | .pattern = bbt_pattern, |
| 84 | }; |
| 85 | |
| 86 | static struct nand_bbt_descr bbt_mirror_descr = { |
| 87 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE | |
| 88 | NAND_BBT_2BIT | NAND_BBT_VERSION, |
| 89 | .offs = 2, /* 0 on 8-bit small page */ |
| 90 | .len = 4, |
| 91 | .veroffs = 6, |
| 92 | .maxblocks = 4, |
| 93 | .pattern = mirror_pattern, |
| 94 | }; |
| 95 | |
Boris Brezillon | caf5129 | 2016-02-09 17:01:57 +0100 | [diff] [blame] | 96 | static int fsl_ifc_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 97 | struct mtd_oob_region *oobregion) |
| 98 | { |
| 99 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 100 | |
| 101 | if (section) |
| 102 | return -ERANGE; |
| 103 | |
| 104 | oobregion->offset = 8; |
| 105 | oobregion->length = chip->ecc.total; |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int fsl_ifc_ooblayout_free(struct mtd_info *mtd, int section, |
| 111 | struct mtd_oob_region *oobregion) |
| 112 | { |
| 113 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 114 | |
| 115 | if (section > 1) |
| 116 | return -ERANGE; |
| 117 | |
| 118 | if (mtd->writesize == 512 && |
| 119 | !(chip->options & NAND_BUSWIDTH_16)) { |
| 120 | if (!section) { |
| 121 | oobregion->offset = 0; |
| 122 | oobregion->length = 5; |
| 123 | } else { |
| 124 | oobregion->offset = 6; |
| 125 | oobregion->length = 2; |
| 126 | } |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | if (!section) { |
| 132 | oobregion->offset = 2; |
| 133 | oobregion->length = 6; |
| 134 | } else { |
| 135 | oobregion->offset = chip->ecc.total + 8; |
| 136 | oobregion->length = mtd->oobsize - oobregion->offset; |
| 137 | } |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static const struct mtd_ooblayout_ops fsl_ifc_ooblayout_ops = { |
| 143 | .ecc = fsl_ifc_ooblayout_ecc, |
| 144 | .free = fsl_ifc_ooblayout_free, |
| 145 | }; |
| 146 | |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 147 | /* |
| 148 | * Set up the IFC hardware block and page address fields, and the ifc nand |
| 149 | * structure addr field to point to the correct IFC buffer in memory |
| 150 | */ |
| 151 | static void set_addr(struct mtd_info *mtd, int column, int page_addr, int oob) |
| 152 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 153 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 154 | struct fsl_ifc_mtd *priv = nand_get_controller_data(chip); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 155 | struct fsl_ifc_ctrl *ctrl = priv->ctrl; |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 156 | struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 157 | int buf_num; |
| 158 | |
| 159 | ifc_nand_ctrl->page = page_addr; |
| 160 | /* Program ROW0/COL0 */ |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 161 | ifc_out32(page_addr, &ifc->ifc_nand.row0); |
| 162 | ifc_out32((oob ? IFC_NAND_COL_MS : 0) | column, &ifc->ifc_nand.col0); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 163 | |
| 164 | buf_num = page_addr & priv->bufnum_mask; |
| 165 | |
| 166 | ifc_nand_ctrl->addr = priv->vbase + buf_num * (mtd->writesize * 2); |
| 167 | ifc_nand_ctrl->index = column; |
| 168 | |
| 169 | /* for OOB data point to the second half of the buffer */ |
| 170 | if (oob) |
| 171 | ifc_nand_ctrl->index += mtd->writesize; |
| 172 | } |
| 173 | |
| 174 | static int is_blank(struct mtd_info *mtd, unsigned int bufnum) |
| 175 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 176 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 177 | struct fsl_ifc_mtd *priv = nand_get_controller_data(chip); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 178 | u8 __iomem *addr = priv->vbase + bufnum * (mtd->writesize * 2); |
Kim Phillips | 2caf87a | 2012-09-13 18:56:07 -0500 | [diff] [blame] | 179 | u32 __iomem *mainarea = (u32 __iomem *)addr; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 180 | u8 __iomem *oob = addr + mtd->writesize; |
Boris Brezillon | 9ed92dd | 2016-02-03 20:11:32 +0100 | [diff] [blame] | 181 | struct mtd_oob_region oobregion = { }; |
| 182 | int i, section = 0; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 183 | |
| 184 | for (i = 0; i < mtd->writesize / 4; i++) { |
| 185 | if (__raw_readl(&mainarea[i]) != 0xffffffff) |
| 186 | return 0; |
| 187 | } |
| 188 | |
Boris Brezillon | 9ed92dd | 2016-02-03 20:11:32 +0100 | [diff] [blame] | 189 | mtd_ooblayout_ecc(mtd, section++, &oobregion); |
| 190 | while (oobregion.length) { |
| 191 | for (i = 0; i < oobregion.length; i++) { |
| 192 | if (__raw_readb(&oob[oobregion.offset + i]) != 0xff) |
| 193 | return 0; |
| 194 | } |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 195 | |
Boris Brezillon | 9ed92dd | 2016-02-03 20:11:32 +0100 | [diff] [blame] | 196 | mtd_ooblayout_ecc(mtd, section++, &oobregion); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | return 1; |
| 200 | } |
| 201 | |
| 202 | /* returns nonzero if entire page is blank */ |
| 203 | static int check_read_ecc(struct mtd_info *mtd, struct fsl_ifc_ctrl *ctrl, |
Jagdish Gediya | eca95cb | 2018-03-21 05:51:46 +0530 | [diff] [blame] | 204 | u32 eccstat, unsigned int bufnum) |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 205 | { |
Jagdish Gediya | eca95cb | 2018-03-21 05:51:46 +0530 | [diff] [blame] | 206 | return (eccstat >> ((3 - bufnum % 4) * 8)) & 15; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /* |
| 210 | * execute IFC NAND command and wait for it to complete |
| 211 | */ |
| 212 | static void fsl_ifc_run_command(struct mtd_info *mtd) |
| 213 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 214 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 215 | struct fsl_ifc_mtd *priv = nand_get_controller_data(chip); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 216 | struct fsl_ifc_ctrl *ctrl = priv->ctrl; |
| 217 | struct fsl_ifc_nand_ctrl *nctrl = ifc_nand_ctrl; |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 218 | struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs; |
Jagdish Gediya | eca95cb | 2018-03-21 05:51:46 +0530 | [diff] [blame] | 219 | u32 eccstat; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 220 | int i; |
| 221 | |
| 222 | /* set the chip select for NAND Transaction */ |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 223 | ifc_out32(priv->bank << IFC_NAND_CSEL_SHIFT, |
| 224 | &ifc->ifc_nand.nand_csel); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 225 | |
| 226 | dev_vdbg(priv->dev, |
| 227 | "%s: fir0=%08x fcr0=%08x\n", |
| 228 | __func__, |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 229 | ifc_in32(&ifc->ifc_nand.nand_fir0), |
| 230 | ifc_in32(&ifc->ifc_nand.nand_fcr0)); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 231 | |
| 232 | ctrl->nand_stat = 0; |
| 233 | |
| 234 | /* start read/write seq */ |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 235 | ifc_out32(IFC_NAND_SEQ_STRT_FIR_STRT, &ifc->ifc_nand.nandseq_strt); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 236 | |
| 237 | /* wait for command complete flag or timeout */ |
| 238 | wait_event_timeout(ctrl->nand_wait, ctrl->nand_stat, |
Nicholas Mc Guire | 95d7066 | 2015-03-13 07:23:47 -0400 | [diff] [blame] | 239 | msecs_to_jiffies(IFC_TIMEOUT_MSECS)); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 240 | |
| 241 | /* ctrl->nand_stat will be updated from IRQ context */ |
| 242 | if (!ctrl->nand_stat) |
| 243 | dev_err(priv->dev, "Controller is not responding\n"); |
| 244 | if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_FTOER) |
| 245 | dev_err(priv->dev, "NAND Flash Timeout Error\n"); |
| 246 | if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_WPER) |
| 247 | dev_err(priv->dev, "NAND Flash Write Protect Error\n"); |
| 248 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 249 | nctrl->max_bitflips = 0; |
| 250 | |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 251 | if (nctrl->eccread) { |
| 252 | int errors; |
| 253 | int bufnum = nctrl->page & priv->bufnum_mask; |
Jagdish Gediya | eca95cb | 2018-03-21 05:51:46 +0530 | [diff] [blame] | 254 | int sector_start = bufnum * chip->ecc.steps; |
| 255 | int sector_end = sector_start + chip->ecc.steps - 1; |
Mark Marshall | 9d82393 | 2017-01-26 16:18:27 +0100 | [diff] [blame] | 256 | __be32 *eccstat_regs; |
| 257 | |
Jagdish Gediya | 4d9ed68 | 2018-03-22 01:08:10 +0530 | [diff] [blame] | 258 | eccstat_regs = ifc->ifc_nand.nand_eccstat; |
Jagdish Gediya | eca95cb | 2018-03-21 05:51:46 +0530 | [diff] [blame] | 259 | eccstat = ifc_in32(&eccstat_regs[sector_start / 4]); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 260 | |
Jagdish Gediya | eca95cb | 2018-03-21 05:51:46 +0530 | [diff] [blame] | 261 | for (i = sector_start; i <= sector_end; i++) { |
| 262 | if (i != sector_start && !(i % 4)) |
| 263 | eccstat = ifc_in32(&eccstat_regs[i / 4]); |
| 264 | |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 265 | errors = check_read_ecc(mtd, ctrl, eccstat, i); |
| 266 | |
| 267 | if (errors == 15) { |
| 268 | /* |
| 269 | * Uncorrectable error. |
| 270 | * OK only if the whole page is blank. |
| 271 | * |
| 272 | * We disable ECCER reporting due to... |
| 273 | * erratum IFC-A002770 -- so report it now if we |
| 274 | * see an uncorrectable error in ECCSTAT. |
| 275 | */ |
| 276 | if (!is_blank(mtd, bufnum)) |
| 277 | ctrl->nand_stat |= |
| 278 | IFC_NAND_EVTER_STAT_ECCER; |
| 279 | break; |
| 280 | } |
| 281 | |
| 282 | mtd->ecc_stats.corrected += errors; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 283 | nctrl->max_bitflips = max_t(unsigned int, |
| 284 | nctrl->max_bitflips, |
| 285 | errors); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | nctrl->eccread = 0; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | static void fsl_ifc_do_read(struct nand_chip *chip, |
| 293 | int oob, |
| 294 | struct mtd_info *mtd) |
| 295 | { |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 296 | struct fsl_ifc_mtd *priv = nand_get_controller_data(chip); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 297 | struct fsl_ifc_ctrl *ctrl = priv->ctrl; |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 298 | struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 299 | |
| 300 | /* Program FIR/IFC_NAND_FCR0 for Small/Large page */ |
| 301 | if (mtd->writesize > 512) { |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 302 | ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) | |
| 303 | (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) | |
| 304 | (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) | |
| 305 | (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP3_SHIFT) | |
| 306 | (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP4_SHIFT), |
| 307 | &ifc->ifc_nand.nand_fir0); |
| 308 | ifc_out32(0x0, &ifc->ifc_nand.nand_fir1); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 309 | |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 310 | ifc_out32((NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT) | |
| 311 | (NAND_CMD_READSTART << IFC_NAND_FCR0_CMD1_SHIFT), |
| 312 | &ifc->ifc_nand.nand_fcr0); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 313 | } else { |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 314 | ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) | |
| 315 | (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) | |
| 316 | (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) | |
| 317 | (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP3_SHIFT), |
| 318 | &ifc->ifc_nand.nand_fir0); |
| 319 | ifc_out32(0x0, &ifc->ifc_nand.nand_fir1); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 320 | |
| 321 | if (oob) |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 322 | ifc_out32(NAND_CMD_READOOB << |
| 323 | IFC_NAND_FCR0_CMD0_SHIFT, |
| 324 | &ifc->ifc_nand.nand_fcr0); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 325 | else |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 326 | ifc_out32(NAND_CMD_READ0 << |
| 327 | IFC_NAND_FCR0_CMD0_SHIFT, |
| 328 | &ifc->ifc_nand.nand_fcr0); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | /* cmdfunc send commands to the IFC NAND Machine */ |
| 333 | static void fsl_ifc_cmdfunc(struct mtd_info *mtd, unsigned int command, |
| 334 | int column, int page_addr) { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 335 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 336 | struct fsl_ifc_mtd *priv = nand_get_controller_data(chip); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 337 | struct fsl_ifc_ctrl *ctrl = priv->ctrl; |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 338 | struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 339 | |
| 340 | /* clear the read buffer */ |
| 341 | ifc_nand_ctrl->read_bytes = 0; |
| 342 | if (command != NAND_CMD_PAGEPROG) |
| 343 | ifc_nand_ctrl->index = 0; |
| 344 | |
| 345 | switch (command) { |
| 346 | /* READ0 read the entire buffer to use hardware ECC. */ |
| 347 | case NAND_CMD_READ0: |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 348 | ifc_out32(0, &ifc->ifc_nand.nand_fbcr); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 349 | set_addr(mtd, 0, page_addr, 0); |
| 350 | |
| 351 | ifc_nand_ctrl->read_bytes = mtd->writesize + mtd->oobsize; |
| 352 | ifc_nand_ctrl->index += column; |
| 353 | |
| 354 | if (chip->ecc.mode == NAND_ECC_HW) |
| 355 | ifc_nand_ctrl->eccread = 1; |
| 356 | |
| 357 | fsl_ifc_do_read(chip, 0, mtd); |
| 358 | fsl_ifc_run_command(mtd); |
| 359 | return; |
| 360 | |
| 361 | /* READOOB reads only the OOB because no ECC is performed. */ |
| 362 | case NAND_CMD_READOOB: |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 363 | ifc_out32(mtd->oobsize - column, &ifc->ifc_nand.nand_fbcr); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 364 | set_addr(mtd, column, page_addr, 1); |
| 365 | |
| 366 | ifc_nand_ctrl->read_bytes = mtd->writesize + mtd->oobsize; |
| 367 | |
| 368 | fsl_ifc_do_read(chip, 1, mtd); |
| 369 | fsl_ifc_run_command(mtd); |
| 370 | |
| 371 | return; |
| 372 | |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 373 | case NAND_CMD_READID: |
Prabhakar Kushwaha | 59fdd5b | 2012-04-09 10:55:22 +0530 | [diff] [blame] | 374 | case NAND_CMD_PARAM: { |
| 375 | int timing = IFC_FIR_OP_RB; |
| 376 | if (command == NAND_CMD_PARAM) |
| 377 | timing = IFC_FIR_OP_RBCD; |
| 378 | |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 379 | ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) | |
| 380 | (IFC_FIR_OP_UA << IFC_NAND_FIR0_OP1_SHIFT) | |
| 381 | (timing << IFC_NAND_FIR0_OP2_SHIFT), |
| 382 | &ifc->ifc_nand.nand_fir0); |
| 383 | ifc_out32(command << IFC_NAND_FCR0_CMD0_SHIFT, |
| 384 | &ifc->ifc_nand.nand_fcr0); |
| 385 | ifc_out32(column, &ifc->ifc_nand.row3); |
Prabhakar Kushwaha | 59fdd5b | 2012-04-09 10:55:22 +0530 | [diff] [blame] | 386 | |
| 387 | /* |
| 388 | * although currently it's 8 bytes for READID, we always read |
| 389 | * the maximum 256 bytes(for PARAM) |
| 390 | */ |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 391 | ifc_out32(256, &ifc->ifc_nand.nand_fbcr); |
Prabhakar Kushwaha | 59fdd5b | 2012-04-09 10:55:22 +0530 | [diff] [blame] | 392 | ifc_nand_ctrl->read_bytes = 256; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 393 | |
| 394 | set_addr(mtd, 0, 0, 0); |
| 395 | fsl_ifc_run_command(mtd); |
| 396 | return; |
Prabhakar Kushwaha | 59fdd5b | 2012-04-09 10:55:22 +0530 | [diff] [blame] | 397 | } |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 398 | |
| 399 | /* ERASE1 stores the block and page address */ |
| 400 | case NAND_CMD_ERASE1: |
| 401 | set_addr(mtd, 0, page_addr, 0); |
| 402 | return; |
| 403 | |
| 404 | /* ERASE2 uses the block and page address from ERASE1 */ |
| 405 | case NAND_CMD_ERASE2: |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 406 | ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) | |
| 407 | (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP1_SHIFT) | |
| 408 | (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP2_SHIFT), |
| 409 | &ifc->ifc_nand.nand_fir0); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 410 | |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 411 | ifc_out32((NAND_CMD_ERASE1 << IFC_NAND_FCR0_CMD0_SHIFT) | |
| 412 | (NAND_CMD_ERASE2 << IFC_NAND_FCR0_CMD1_SHIFT), |
| 413 | &ifc->ifc_nand.nand_fcr0); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 414 | |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 415 | ifc_out32(0, &ifc->ifc_nand.nand_fbcr); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 416 | ifc_nand_ctrl->read_bytes = 0; |
| 417 | fsl_ifc_run_command(mtd); |
| 418 | return; |
| 419 | |
| 420 | /* SEQIN sets up the addr buffer and all registers except the length */ |
| 421 | case NAND_CMD_SEQIN: { |
| 422 | u32 nand_fcr0; |
| 423 | ifc_nand_ctrl->column = column; |
| 424 | ifc_nand_ctrl->oob = 0; |
| 425 | |
| 426 | if (mtd->writesize > 512) { |
| 427 | nand_fcr0 = |
| 428 | (NAND_CMD_SEQIN << IFC_NAND_FCR0_CMD0_SHIFT) | |
Prabhakar Kushwaha | 4af9874 | 2013-10-03 11:36:41 +0530 | [diff] [blame] | 429 | (NAND_CMD_STATUS << IFC_NAND_FCR0_CMD1_SHIFT) | |
| 430 | (NAND_CMD_PAGEPROG << IFC_NAND_FCR0_CMD2_SHIFT); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 431 | |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 432 | ifc_out32( |
| 433 | (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) | |
| 434 | (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) | |
| 435 | (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) | |
| 436 | (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP3_SHIFT) | |
| 437 | (IFC_FIR_OP_CMD2 << IFC_NAND_FIR0_OP4_SHIFT), |
| 438 | &ifc->ifc_nand.nand_fir0); |
| 439 | ifc_out32( |
| 440 | (IFC_FIR_OP_CW1 << IFC_NAND_FIR1_OP5_SHIFT) | |
| 441 | (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR1_OP6_SHIFT) | |
| 442 | (IFC_FIR_OP_NOP << IFC_NAND_FIR1_OP7_SHIFT), |
| 443 | &ifc->ifc_nand.nand_fir1); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 444 | } else { |
| 445 | nand_fcr0 = ((NAND_CMD_PAGEPROG << |
| 446 | IFC_NAND_FCR0_CMD1_SHIFT) | |
| 447 | (NAND_CMD_SEQIN << |
Prabhakar Kushwaha | 4af9874 | 2013-10-03 11:36:41 +0530 | [diff] [blame] | 448 | IFC_NAND_FCR0_CMD2_SHIFT) | |
| 449 | (NAND_CMD_STATUS << |
| 450 | IFC_NAND_FCR0_CMD3_SHIFT)); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 451 | |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 452 | ifc_out32( |
Kim Phillips | 0c69fb0 | 2013-01-11 16:23:59 -0600 | [diff] [blame] | 453 | (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) | |
| 454 | (IFC_FIR_OP_CMD2 << IFC_NAND_FIR0_OP1_SHIFT) | |
| 455 | (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP2_SHIFT) | |
| 456 | (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP3_SHIFT) | |
| 457 | (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP4_SHIFT), |
| 458 | &ifc->ifc_nand.nand_fir0); |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 459 | ifc_out32( |
| 460 | (IFC_FIR_OP_CMD1 << IFC_NAND_FIR1_OP5_SHIFT) | |
| 461 | (IFC_FIR_OP_CW3 << IFC_NAND_FIR1_OP6_SHIFT) | |
| 462 | (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR1_OP7_SHIFT) | |
| 463 | (IFC_FIR_OP_NOP << IFC_NAND_FIR1_OP8_SHIFT), |
| 464 | &ifc->ifc_nand.nand_fir1); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 465 | |
| 466 | if (column >= mtd->writesize) |
| 467 | nand_fcr0 |= |
| 468 | NAND_CMD_READOOB << IFC_NAND_FCR0_CMD0_SHIFT; |
| 469 | else |
| 470 | nand_fcr0 |= |
| 471 | NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT; |
| 472 | } |
| 473 | |
| 474 | if (column >= mtd->writesize) { |
| 475 | /* OOB area --> READOOB */ |
| 476 | column -= mtd->writesize; |
| 477 | ifc_nand_ctrl->oob = 1; |
| 478 | } |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 479 | ifc_out32(nand_fcr0, &ifc->ifc_nand.nand_fcr0); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 480 | set_addr(mtd, column, page_addr, ifc_nand_ctrl->oob); |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | /* PAGEPROG reuses all of the setup from SEQIN and adds the length */ |
| 485 | case NAND_CMD_PAGEPROG: { |
| 486 | if (ifc_nand_ctrl->oob) { |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 487 | ifc_out32(ifc_nand_ctrl->index - |
| 488 | ifc_nand_ctrl->column, |
| 489 | &ifc->ifc_nand.nand_fbcr); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 490 | } else { |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 491 | ifc_out32(0, &ifc->ifc_nand.nand_fbcr); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | fsl_ifc_run_command(mtd); |
| 495 | return; |
| 496 | } |
| 497 | |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 498 | case NAND_CMD_STATUS: { |
| 499 | void __iomem *addr; |
| 500 | |
| 501 | ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) | |
| 502 | (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP1_SHIFT), |
| 503 | &ifc->ifc_nand.nand_fir0); |
| 504 | ifc_out32(NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT, |
| 505 | &ifc->ifc_nand.nand_fcr0); |
| 506 | ifc_out32(1, &ifc->ifc_nand.nand_fbcr); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 507 | set_addr(mtd, 0, 0, 0); |
| 508 | ifc_nand_ctrl->read_bytes = 1; |
| 509 | |
| 510 | fsl_ifc_run_command(mtd); |
| 511 | |
| 512 | /* |
| 513 | * The chip always seems to report that it is |
| 514 | * write-protected, even when it is not. |
| 515 | */ |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 516 | addr = ifc_nand_ctrl->addr; |
Joe Schultz | 2170480 | 2014-04-07 11:58:18 -0500 | [diff] [blame] | 517 | if (chip->options & NAND_BUSWIDTH_16) |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 518 | ifc_out16(ifc_in16(addr) | (NAND_STATUS_WP), addr); |
Joe Schultz | 2170480 | 2014-04-07 11:58:18 -0500 | [diff] [blame] | 519 | else |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 520 | ifc_out8(ifc_in8(addr) | (NAND_STATUS_WP), addr); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 521 | return; |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 522 | } |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 523 | |
| 524 | case NAND_CMD_RESET: |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 525 | ifc_out32(IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT, |
| 526 | &ifc->ifc_nand.nand_fir0); |
| 527 | ifc_out32(NAND_CMD_RESET << IFC_NAND_FCR0_CMD0_SHIFT, |
| 528 | &ifc->ifc_nand.nand_fcr0); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 529 | fsl_ifc_run_command(mtd); |
| 530 | return; |
| 531 | |
| 532 | default: |
| 533 | dev_err(priv->dev, "%s: error, unsupported command 0x%x.\n", |
| 534 | __func__, command); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | static void fsl_ifc_select_chip(struct mtd_info *mtd, int chip) |
| 539 | { |
| 540 | /* The hardware does not seem to support multiple |
| 541 | * chips per bank. |
| 542 | */ |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * Write buf to the IFC NAND Controller Data Buffer |
| 547 | */ |
| 548 | static void fsl_ifc_write_buf(struct mtd_info *mtd, const u8 *buf, int len) |
| 549 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 550 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 551 | struct fsl_ifc_mtd *priv = nand_get_controller_data(chip); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 552 | unsigned int bufsize = mtd->writesize + mtd->oobsize; |
| 553 | |
| 554 | if (len <= 0) { |
| 555 | dev_err(priv->dev, "%s: len %d bytes", __func__, len); |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | if ((unsigned int)len > bufsize - ifc_nand_ctrl->index) { |
| 560 | dev_err(priv->dev, |
| 561 | "%s: beyond end of buffer (%d requested, %u available)\n", |
| 562 | __func__, len, bufsize - ifc_nand_ctrl->index); |
| 563 | len = bufsize - ifc_nand_ctrl->index; |
| 564 | } |
| 565 | |
Aaron Sierra | 4454406 | 2014-04-07 11:58:12 -0500 | [diff] [blame] | 566 | memcpy_toio(ifc_nand_ctrl->addr + ifc_nand_ctrl->index, buf, len); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 567 | ifc_nand_ctrl->index += len; |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | * Read a byte from either the IFC hardware buffer |
| 572 | * read function for 8-bit buswidth |
| 573 | */ |
| 574 | static uint8_t fsl_ifc_read_byte(struct mtd_info *mtd) |
| 575 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 576 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 577 | struct fsl_ifc_mtd *priv = nand_get_controller_data(chip); |
Aaron Sierra | 4454406 | 2014-04-07 11:58:12 -0500 | [diff] [blame] | 578 | unsigned int offset; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 579 | |
| 580 | /* |
| 581 | * If there are still bytes in the IFC buffer, then use the |
| 582 | * next byte. |
| 583 | */ |
Aaron Sierra | 4454406 | 2014-04-07 11:58:12 -0500 | [diff] [blame] | 584 | if (ifc_nand_ctrl->index < ifc_nand_ctrl->read_bytes) { |
| 585 | offset = ifc_nand_ctrl->index++; |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 586 | return ifc_in8(ifc_nand_ctrl->addr + offset); |
Aaron Sierra | 4454406 | 2014-04-07 11:58:12 -0500 | [diff] [blame] | 587 | } |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 588 | |
| 589 | dev_err(priv->dev, "%s: beyond end of buffer\n", __func__); |
| 590 | return ERR_BYTE; |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * Read two bytes from the IFC hardware buffer |
| 595 | * read function for 16-bit buswith |
| 596 | */ |
| 597 | static uint8_t fsl_ifc_read_byte16(struct mtd_info *mtd) |
| 598 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 599 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 600 | struct fsl_ifc_mtd *priv = nand_get_controller_data(chip); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 601 | uint16_t data; |
| 602 | |
| 603 | /* |
| 604 | * If there are still bytes in the IFC buffer, then use the |
| 605 | * next byte. |
| 606 | */ |
| 607 | if (ifc_nand_ctrl->index < ifc_nand_ctrl->read_bytes) { |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 608 | data = ifc_in16(ifc_nand_ctrl->addr + ifc_nand_ctrl->index); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 609 | ifc_nand_ctrl->index += 2; |
| 610 | return (uint8_t) data; |
| 611 | } |
| 612 | |
| 613 | dev_err(priv->dev, "%s: beyond end of buffer\n", __func__); |
| 614 | return ERR_BYTE; |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | * Read from the IFC Controller Data Buffer |
| 619 | */ |
| 620 | static void fsl_ifc_read_buf(struct mtd_info *mtd, u8 *buf, int len) |
| 621 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 622 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 623 | struct fsl_ifc_mtd *priv = nand_get_controller_data(chip); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 624 | int avail; |
| 625 | |
| 626 | if (len < 0) { |
| 627 | dev_err(priv->dev, "%s: len %d bytes", __func__, len); |
| 628 | return; |
| 629 | } |
| 630 | |
| 631 | avail = min((unsigned int)len, |
| 632 | ifc_nand_ctrl->read_bytes - ifc_nand_ctrl->index); |
Aaron Sierra | 4454406 | 2014-04-07 11:58:12 -0500 | [diff] [blame] | 633 | memcpy_fromio(buf, ifc_nand_ctrl->addr + ifc_nand_ctrl->index, avail); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 634 | ifc_nand_ctrl->index += avail; |
| 635 | |
| 636 | if (len > avail) |
| 637 | dev_err(priv->dev, |
| 638 | "%s: beyond end of buffer (%d requested, %d available)\n", |
| 639 | __func__, len, avail); |
| 640 | } |
| 641 | |
| 642 | /* |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 643 | * This function is called after Program and Erase Operations to |
| 644 | * check for success or failure. |
| 645 | */ |
| 646 | static int fsl_ifc_wait(struct mtd_info *mtd, struct nand_chip *chip) |
| 647 | { |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 648 | struct fsl_ifc_mtd *priv = nand_get_controller_data(chip); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 649 | struct fsl_ifc_ctrl *ctrl = priv->ctrl; |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 650 | struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 651 | u32 nand_fsr; |
Jagdish Gediya | 9b5dd84 | 2018-03-21 04:31:36 +0530 | [diff] [blame] | 652 | int status; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 653 | |
| 654 | /* Use READ_STATUS command, but wait for the device to be ready */ |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 655 | ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) | |
| 656 | (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR0_OP1_SHIFT), |
| 657 | &ifc->ifc_nand.nand_fir0); |
| 658 | ifc_out32(NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT, |
| 659 | &ifc->ifc_nand.nand_fcr0); |
| 660 | ifc_out32(1, &ifc->ifc_nand.nand_fbcr); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 661 | set_addr(mtd, 0, 0, 0); |
| 662 | ifc_nand_ctrl->read_bytes = 1; |
| 663 | |
| 664 | fsl_ifc_run_command(mtd); |
| 665 | |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 666 | nand_fsr = ifc_in32(&ifc->ifc_nand.nand_fsr); |
Jagdish Gediya | 9b5dd84 | 2018-03-21 04:31:36 +0530 | [diff] [blame] | 667 | status = nand_fsr >> 24; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 668 | /* |
| 669 | * The chip always seems to report that it is |
| 670 | * write-protected, even when it is not. |
| 671 | */ |
Jagdish Gediya | 9b5dd84 | 2018-03-21 04:31:36 +0530 | [diff] [blame] | 672 | return status | NAND_STATUS_WP; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 673 | } |
| 674 | |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 675 | static int fsl_ifc_read_page(struct mtd_info *mtd, struct nand_chip *chip, |
| 676 | uint8_t *buf, int oob_required, int page) |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 677 | { |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 678 | struct fsl_ifc_mtd *priv = nand_get_controller_data(chip); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 679 | struct fsl_ifc_ctrl *ctrl = priv->ctrl; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 680 | struct fsl_ifc_nand_ctrl *nctrl = ifc_nand_ctrl; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 681 | |
| 682 | fsl_ifc_read_buf(mtd, buf, mtd->writesize); |
Brian Norris | a6976cd | 2012-05-02 10:15:01 -0700 | [diff] [blame] | 683 | if (oob_required) |
| 684 | fsl_ifc_read_buf(mtd, chip->oob_poi, mtd->oobsize); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 685 | |
| 686 | if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_ECCER) |
| 687 | dev_err(priv->dev, "NAND Flash ECC Uncorrectable Error\n"); |
| 688 | |
| 689 | if (ctrl->nand_stat != IFC_NAND_EVTER_STAT_OPC) |
| 690 | mtd->ecc_stats.failed++; |
| 691 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 692 | return nctrl->max_bitflips; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | /* ECC will be calculated automatically, and errors will be detected in |
| 696 | * waitfunc. |
| 697 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 698 | static int fsl_ifc_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 699 | const uint8_t *buf, int oob_required, int page) |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 700 | { |
| 701 | fsl_ifc_write_buf(mtd, buf, mtd->writesize); |
| 702 | fsl_ifc_write_buf(mtd, chip->oob_poi, mtd->oobsize); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 703 | |
| 704 | return 0; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | static int fsl_ifc_chip_init_tail(struct mtd_info *mtd) |
| 708 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 709 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 710 | struct fsl_ifc_mtd *priv = nand_get_controller_data(chip); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 711 | |
| 712 | dev_dbg(priv->dev, "%s: nand->numchips = %d\n", __func__, |
| 713 | chip->numchips); |
| 714 | dev_dbg(priv->dev, "%s: nand->chipsize = %lld\n", __func__, |
| 715 | chip->chipsize); |
| 716 | dev_dbg(priv->dev, "%s: nand->pagemask = %8x\n", __func__, |
| 717 | chip->pagemask); |
| 718 | dev_dbg(priv->dev, "%s: nand->chip_delay = %d\n", __func__, |
| 719 | chip->chip_delay); |
| 720 | dev_dbg(priv->dev, "%s: nand->badblockpos = %d\n", __func__, |
| 721 | chip->badblockpos); |
| 722 | dev_dbg(priv->dev, "%s: nand->chip_shift = %d\n", __func__, |
| 723 | chip->chip_shift); |
| 724 | dev_dbg(priv->dev, "%s: nand->page_shift = %d\n", __func__, |
| 725 | chip->page_shift); |
| 726 | dev_dbg(priv->dev, "%s: nand->phys_erase_shift = %d\n", __func__, |
| 727 | chip->phys_erase_shift); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 728 | dev_dbg(priv->dev, "%s: nand->ecc.mode = %d\n", __func__, |
| 729 | chip->ecc.mode); |
| 730 | dev_dbg(priv->dev, "%s: nand->ecc.steps = %d\n", __func__, |
| 731 | chip->ecc.steps); |
| 732 | dev_dbg(priv->dev, "%s: nand->ecc.bytes = %d\n", __func__, |
| 733 | chip->ecc.bytes); |
| 734 | dev_dbg(priv->dev, "%s: nand->ecc.total = %d\n", __func__, |
| 735 | chip->ecc.total); |
Boris Brezillon | caf5129 | 2016-02-09 17:01:57 +0100 | [diff] [blame] | 736 | dev_dbg(priv->dev, "%s: mtd->ooblayout = %p\n", __func__, |
| 737 | mtd->ooblayout); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 738 | dev_dbg(priv->dev, "%s: mtd->flags = %08x\n", __func__, mtd->flags); |
| 739 | dev_dbg(priv->dev, "%s: mtd->size = %lld\n", __func__, mtd->size); |
| 740 | dev_dbg(priv->dev, "%s: mtd->erasesize = %d\n", __func__, |
| 741 | mtd->erasesize); |
| 742 | dev_dbg(priv->dev, "%s: mtd->writesize = %d\n", __func__, |
| 743 | mtd->writesize); |
| 744 | dev_dbg(priv->dev, "%s: mtd->oobsize = %d\n", __func__, |
| 745 | mtd->oobsize); |
| 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | |
Prabhakar Kushwaha | 10bfa76 | 2012-09-13 14:24:49 +0530 | [diff] [blame] | 750 | static void fsl_ifc_sram_init(struct fsl_ifc_mtd *priv) |
| 751 | { |
| 752 | struct fsl_ifc_ctrl *ctrl = priv->ctrl; |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 753 | struct fsl_ifc_runtime __iomem *ifc_runtime = ctrl->rregs; |
| 754 | struct fsl_ifc_global __iomem *ifc_global = ctrl->gregs; |
Prabhakar Kushwaha | 10bfa76 | 2012-09-13 14:24:49 +0530 | [diff] [blame] | 755 | uint32_t csor = 0, csor_8k = 0, csor_ext = 0; |
| 756 | uint32_t cs = priv->bank; |
| 757 | |
| 758 | /* Save CSOR and CSOR_ext */ |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 759 | csor = ifc_in32(&ifc_global->csor_cs[cs].csor); |
| 760 | csor_ext = ifc_in32(&ifc_global->csor_cs[cs].csor_ext); |
Prabhakar Kushwaha | 10bfa76 | 2012-09-13 14:24:49 +0530 | [diff] [blame] | 761 | |
| 762 | /* chage PageSize 8K and SpareSize 1K*/ |
| 763 | csor_8k = (csor & ~(CSOR_NAND_PGS_MASK)) | 0x0018C000; |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 764 | ifc_out32(csor_8k, &ifc_global->csor_cs[cs].csor); |
| 765 | ifc_out32(0x0000400, &ifc_global->csor_cs[cs].csor_ext); |
Prabhakar Kushwaha | 10bfa76 | 2012-09-13 14:24:49 +0530 | [diff] [blame] | 766 | |
| 767 | /* READID */ |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 768 | ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) | |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 769 | (IFC_FIR_OP_UA << IFC_NAND_FIR0_OP1_SHIFT) | |
| 770 | (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP2_SHIFT), |
| 771 | &ifc_runtime->ifc_nand.nand_fir0); |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 772 | ifc_out32(NAND_CMD_READID << IFC_NAND_FCR0_CMD0_SHIFT, |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 773 | &ifc_runtime->ifc_nand.nand_fcr0); |
| 774 | ifc_out32(0x0, &ifc_runtime->ifc_nand.row3); |
Prabhakar Kushwaha | 10bfa76 | 2012-09-13 14:24:49 +0530 | [diff] [blame] | 775 | |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 776 | ifc_out32(0x0, &ifc_runtime->ifc_nand.nand_fbcr); |
Prabhakar Kushwaha | 10bfa76 | 2012-09-13 14:24:49 +0530 | [diff] [blame] | 777 | |
| 778 | /* Program ROW0/COL0 */ |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 779 | ifc_out32(0x0, &ifc_runtime->ifc_nand.row0); |
| 780 | ifc_out32(0x0, &ifc_runtime->ifc_nand.col0); |
Prabhakar Kushwaha | 10bfa76 | 2012-09-13 14:24:49 +0530 | [diff] [blame] | 781 | |
| 782 | /* set the chip select for NAND Transaction */ |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 783 | ifc_out32(cs << IFC_NAND_CSEL_SHIFT, |
| 784 | &ifc_runtime->ifc_nand.nand_csel); |
Prabhakar Kushwaha | 10bfa76 | 2012-09-13 14:24:49 +0530 | [diff] [blame] | 785 | |
| 786 | /* start read seq */ |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 787 | ifc_out32(IFC_NAND_SEQ_STRT_FIR_STRT, |
| 788 | &ifc_runtime->ifc_nand.nandseq_strt); |
Prabhakar Kushwaha | 10bfa76 | 2012-09-13 14:24:49 +0530 | [diff] [blame] | 789 | |
| 790 | /* wait for command complete flag or timeout */ |
| 791 | wait_event_timeout(ctrl->nand_wait, ctrl->nand_stat, |
Nicholas Mc Guire | 95d7066 | 2015-03-13 07:23:47 -0400 | [diff] [blame] | 792 | msecs_to_jiffies(IFC_TIMEOUT_MSECS)); |
Prabhakar Kushwaha | 10bfa76 | 2012-09-13 14:24:49 +0530 | [diff] [blame] | 793 | |
| 794 | if (ctrl->nand_stat != IFC_NAND_EVTER_STAT_OPC) |
| 795 | printk(KERN_ERR "fsl-ifc: Failed to Initialise SRAM\n"); |
| 796 | |
| 797 | /* Restore CSOR and CSOR_ext */ |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 798 | ifc_out32(csor, &ifc_global->csor_cs[cs].csor); |
| 799 | ifc_out32(csor_ext, &ifc_global->csor_cs[cs].csor_ext); |
Prabhakar Kushwaha | 10bfa76 | 2012-09-13 14:24:49 +0530 | [diff] [blame] | 800 | } |
| 801 | |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 802 | static int fsl_ifc_chip_init(struct fsl_ifc_mtd *priv) |
| 803 | { |
| 804 | struct fsl_ifc_ctrl *ctrl = priv->ctrl; |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 805 | struct fsl_ifc_global __iomem *ifc_global = ctrl->gregs; |
| 806 | struct fsl_ifc_runtime __iomem *ifc_runtime = ctrl->rregs; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 807 | struct nand_chip *chip = &priv->chip; |
Boris BREZILLON | 5e9fb93 | 2015-12-10 09:00:03 +0100 | [diff] [blame] | 808 | struct mtd_info *mtd = nand_to_mtd(&priv->chip); |
Aaron Sierra | 0969166 | 2014-08-26 18:18:33 -0500 | [diff] [blame] | 809 | u32 csor; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 810 | |
| 811 | /* Fill in fsl_ifc_mtd structure */ |
Boris BREZILLON | 5e9fb93 | 2015-12-10 09:00:03 +0100 | [diff] [blame] | 812 | mtd->dev.parent = priv->dev; |
Brian Norris | a61ae81 | 2015-10-30 20:33:25 -0700 | [diff] [blame] | 813 | nand_set_flash_node(chip, priv->dev->of_node); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 814 | |
| 815 | /* fill in nand_chip structure */ |
| 816 | /* set up function call table */ |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 817 | if ((ifc_in32(&ifc_global->cspr_cs[priv->bank].cspr)) |
| 818 | & CSPR_PORT_SIZE_16) |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 819 | chip->read_byte = fsl_ifc_read_byte16; |
| 820 | else |
| 821 | chip->read_byte = fsl_ifc_read_byte; |
| 822 | |
| 823 | chip->write_buf = fsl_ifc_write_buf; |
| 824 | chip->read_buf = fsl_ifc_read_buf; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 825 | chip->select_chip = fsl_ifc_select_chip; |
| 826 | chip->cmdfunc = fsl_ifc_cmdfunc; |
| 827 | chip->waitfunc = fsl_ifc_wait; |
| 828 | |
| 829 | chip->bbt_td = &bbt_main_descr; |
| 830 | chip->bbt_md = &bbt_mirror_descr; |
| 831 | |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 832 | ifc_out32(0x0, &ifc_runtime->ifc_nand.ncfgr); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 833 | |
| 834 | /* set up nand options */ |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 835 | chip->bbt_options = NAND_BBT_USE_FLASH; |
Scott Wood | 20cd000 | 2013-04-10 17:34:37 -0500 | [diff] [blame] | 836 | chip->options = NAND_NO_SUBPAGE_WRITE; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 837 | |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 838 | if (ifc_in32(&ifc_global->cspr_cs[priv->bank].cspr) |
| 839 | & CSPR_PORT_SIZE_16) { |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 840 | chip->read_byte = fsl_ifc_read_byte16; |
| 841 | chip->options |= NAND_BUSWIDTH_16; |
| 842 | } else { |
| 843 | chip->read_byte = fsl_ifc_read_byte; |
| 844 | } |
| 845 | |
| 846 | chip->controller = &ifc_nand_ctrl->controller; |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 847 | nand_set_controller_data(chip, priv); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 848 | |
| 849 | chip->ecc.read_page = fsl_ifc_read_page; |
| 850 | chip->ecc.write_page = fsl_ifc_write_page; |
| 851 | |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 852 | csor = ifc_in32(&ifc_global->csor_cs[priv->bank].csor); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 853 | |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 854 | switch (csor & CSOR_NAND_PGS_MASK) { |
| 855 | case CSOR_NAND_PGS_512: |
Boris Brezillon | caf5129 | 2016-02-09 17:01:57 +0100 | [diff] [blame] | 856 | if (!(chip->options & NAND_BUSWIDTH_16)) { |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 857 | /* Avoid conflict with bad block marker */ |
| 858 | bbt_main_descr.offs = 0; |
| 859 | bbt_mirror_descr.offs = 0; |
| 860 | } |
| 861 | |
| 862 | priv->bufnum_mask = 15; |
| 863 | break; |
| 864 | |
| 865 | case CSOR_NAND_PGS_2K: |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 866 | priv->bufnum_mask = 3; |
| 867 | break; |
| 868 | |
| 869 | case CSOR_NAND_PGS_4K: |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 870 | priv->bufnum_mask = 1; |
| 871 | break; |
| 872 | |
Prabhakar Kushwaha | ebff90b | 2013-09-24 16:41:23 +0530 | [diff] [blame] | 873 | case CSOR_NAND_PGS_8K: |
Prabhakar Kushwaha | ebff90b | 2013-09-24 16:41:23 +0530 | [diff] [blame] | 874 | priv->bufnum_mask = 0; |
Boris Brezillon | caf5129 | 2016-02-09 17:01:57 +0100 | [diff] [blame] | 875 | break; |
Prabhakar Kushwaha | ebff90b | 2013-09-24 16:41:23 +0530 | [diff] [blame] | 876 | |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 877 | default: |
| 878 | dev_err(priv->dev, "bad csor %#x: bad page size\n", csor); |
| 879 | return -ENODEV; |
| 880 | } |
| 881 | |
| 882 | /* Must also set CSOR_NAND_ECC_ENC_EN if DEC_EN set */ |
| 883 | if (csor & CSOR_NAND_ECC_DEC_EN) { |
| 884 | chip->ecc.mode = NAND_ECC_HW; |
Boris Brezillon | caf5129 | 2016-02-09 17:01:57 +0100 | [diff] [blame] | 885 | mtd_set_ooblayout(mtd, &fsl_ifc_ooblayout_ops); |
| 886 | |
| 887 | /* Hardware generates ECC per 512 Bytes */ |
| 888 | chip->ecc.size = 512; |
| 889 | if ((csor & CSOR_NAND_ECC_MODE_MASK) == CSOR_NAND_ECC_MODE_4) { |
| 890 | chip->ecc.bytes = 8; |
| 891 | chip->ecc.strength = 4; |
| 892 | } else { |
| 893 | chip->ecc.bytes = 16; |
| 894 | chip->ecc.strength = 8; |
| 895 | } |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 896 | } else { |
| 897 | chip->ecc.mode = NAND_ECC_SOFT; |
Rafał Miłecki | ff1ef35 | 2016-04-13 14:07:01 +0200 | [diff] [blame] | 898 | chip->ecc.algo = NAND_ECC_HAMMING; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 899 | } |
| 900 | |
Aaron Sierra | 0969166 | 2014-08-26 18:18:33 -0500 | [diff] [blame] | 901 | if (ctrl->version == FSL_IFC_VERSION_1_1_0) |
Prabhakar Kushwaha | 10bfa76 | 2012-09-13 14:24:49 +0530 | [diff] [blame] | 902 | fsl_ifc_sram_init(priv); |
| 903 | |
Jagdish Gediya | 8834721 | 2017-11-23 17:04:31 +0530 | [diff] [blame] | 904 | /* |
| 905 | * As IFC version 2.0.0 has 16KB of internal SRAM as compared to older |
| 906 | * versions which had 8KB. Hence bufnum mask needs to be updated. |
| 907 | */ |
| 908 | if (ctrl->version >= FSL_IFC_VERSION_2_0_0) |
| 909 | priv->bufnum_mask = (priv->bufnum_mask * 2) + 1; |
| 910 | |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | static int fsl_ifc_chip_remove(struct fsl_ifc_mtd *priv) |
| 915 | { |
Boris BREZILLON | 5e9fb93 | 2015-12-10 09:00:03 +0100 | [diff] [blame] | 916 | struct mtd_info *mtd = nand_to_mtd(&priv->chip); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 917 | |
Boris BREZILLON | 5e9fb93 | 2015-12-10 09:00:03 +0100 | [diff] [blame] | 918 | nand_release(mtd); |
| 919 | |
| 920 | kfree(mtd->name); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 921 | |
| 922 | if (priv->vbase) |
| 923 | iounmap(priv->vbase); |
| 924 | |
| 925 | ifc_nand_ctrl->chips[priv->bank] = NULL; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 926 | |
| 927 | return 0; |
| 928 | } |
| 929 | |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 930 | static int match_bank(struct fsl_ifc_global __iomem *ifc_global, int bank, |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 931 | phys_addr_t addr) |
| 932 | { |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 933 | u32 cspr = ifc_in32(&ifc_global->cspr_cs[bank].cspr); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 934 | |
| 935 | if (!(cspr & CSPR_V)) |
| 936 | return 0; |
| 937 | if ((cspr & CSPR_MSEL) != CSPR_MSEL_NAND) |
| 938 | return 0; |
| 939 | |
| 940 | return (cspr & CSPR_BA) == convert_ifc_address(addr); |
| 941 | } |
| 942 | |
| 943 | static DEFINE_MUTEX(fsl_ifc_nand_mutex); |
| 944 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 945 | static int fsl_ifc_nand_probe(struct platform_device *dev) |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 946 | { |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 947 | struct fsl_ifc_runtime __iomem *ifc; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 948 | struct fsl_ifc_mtd *priv; |
| 949 | struct resource res; |
| 950 | static const char *part_probe_types[] |
| 951 | = { "cmdlinepart", "RedBoot", "ofpart", NULL }; |
| 952 | int ret; |
| 953 | int bank; |
| 954 | struct device_node *node = dev->dev.of_node; |
Boris BREZILLON | 5e9fb93 | 2015-12-10 09:00:03 +0100 | [diff] [blame] | 955 | struct mtd_info *mtd; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 956 | |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 957 | if (!fsl_ifc_ctrl_dev || !fsl_ifc_ctrl_dev->rregs) |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 958 | return -ENODEV; |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 959 | ifc = fsl_ifc_ctrl_dev->rregs; |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 960 | |
| 961 | /* get, allocate and map the memory resource */ |
| 962 | ret = of_address_to_resource(node, 0, &res); |
| 963 | if (ret) { |
| 964 | dev_err(&dev->dev, "%s: failed to get resource\n", __func__); |
| 965 | return ret; |
| 966 | } |
| 967 | |
| 968 | /* find which chip select it is connected to */ |
Aaron Sierra | 0969166 | 2014-08-26 18:18:33 -0500 | [diff] [blame] | 969 | for (bank = 0; bank < fsl_ifc_ctrl_dev->banks; bank++) { |
Raghav Dogra | 7a65417 | 2016-02-17 16:54:18 +0530 | [diff] [blame] | 970 | if (match_bank(fsl_ifc_ctrl_dev->gregs, bank, res.start)) |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 971 | break; |
| 972 | } |
| 973 | |
Aaron Sierra | 0969166 | 2014-08-26 18:18:33 -0500 | [diff] [blame] | 974 | if (bank >= fsl_ifc_ctrl_dev->banks) { |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 975 | dev_err(&dev->dev, "%s: address did not match any chip selects\n", |
| 976 | __func__); |
| 977 | return -ENODEV; |
| 978 | } |
| 979 | |
| 980 | priv = devm_kzalloc(&dev->dev, sizeof(*priv), GFP_KERNEL); |
| 981 | if (!priv) |
| 982 | return -ENOMEM; |
| 983 | |
| 984 | mutex_lock(&fsl_ifc_nand_mutex); |
| 985 | if (!fsl_ifc_ctrl_dev->nand) { |
| 986 | ifc_nand_ctrl = kzalloc(sizeof(*ifc_nand_ctrl), GFP_KERNEL); |
| 987 | if (!ifc_nand_ctrl) { |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 988 | mutex_unlock(&fsl_ifc_nand_mutex); |
| 989 | return -ENOMEM; |
| 990 | } |
| 991 | |
| 992 | ifc_nand_ctrl->read_bytes = 0; |
| 993 | ifc_nand_ctrl->index = 0; |
| 994 | ifc_nand_ctrl->addr = NULL; |
| 995 | fsl_ifc_ctrl_dev->nand = ifc_nand_ctrl; |
| 996 | |
Marc Gonzalez | d45bc58 | 2016-07-27 11:23:52 +0200 | [diff] [blame] | 997 | nand_hw_control_init(&ifc_nand_ctrl->controller); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 998 | } else { |
| 999 | ifc_nand_ctrl = fsl_ifc_ctrl_dev->nand; |
| 1000 | } |
| 1001 | mutex_unlock(&fsl_ifc_nand_mutex); |
| 1002 | |
| 1003 | ifc_nand_ctrl->chips[bank] = priv; |
| 1004 | priv->bank = bank; |
| 1005 | priv->ctrl = fsl_ifc_ctrl_dev; |
| 1006 | priv->dev = &dev->dev; |
| 1007 | |
| 1008 | priv->vbase = ioremap(res.start, resource_size(&res)); |
| 1009 | if (!priv->vbase) { |
| 1010 | dev_err(priv->dev, "%s: failed to map chip region\n", __func__); |
| 1011 | ret = -ENOMEM; |
| 1012 | goto err; |
| 1013 | } |
| 1014 | |
| 1015 | dev_set_drvdata(priv->dev, priv); |
| 1016 | |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 1017 | ifc_out32(IFC_NAND_EVTER_EN_OPC_EN | |
| 1018 | IFC_NAND_EVTER_EN_FTOER_EN | |
| 1019 | IFC_NAND_EVTER_EN_WPER_EN, |
| 1020 | &ifc->ifc_nand.nand_evter_en); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 1021 | |
| 1022 | /* enable NAND Machine Interrupts */ |
Jaiprakash Singh | cf184dc | 2015-05-20 21:17:11 -0500 | [diff] [blame] | 1023 | ifc_out32(IFC_NAND_EVTER_INTR_OPCIR_EN | |
| 1024 | IFC_NAND_EVTER_INTR_FTOERIR_EN | |
| 1025 | IFC_NAND_EVTER_INTR_WPERIR_EN, |
| 1026 | &ifc->ifc_nand.nand_evter_intr_en); |
Boris BREZILLON | 5e9fb93 | 2015-12-10 09:00:03 +0100 | [diff] [blame] | 1027 | |
| 1028 | mtd = nand_to_mtd(&priv->chip); |
| 1029 | mtd->name = kasprintf(GFP_KERNEL, "%llx.flash", (u64)res.start); |
| 1030 | if (!mtd->name) { |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 1031 | ret = -ENOMEM; |
| 1032 | goto err; |
| 1033 | } |
| 1034 | |
| 1035 | ret = fsl_ifc_chip_init(priv); |
| 1036 | if (ret) |
| 1037 | goto err; |
| 1038 | |
Boris BREZILLON | 5e9fb93 | 2015-12-10 09:00:03 +0100 | [diff] [blame] | 1039 | ret = nand_scan_ident(mtd, 1, NULL); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 1040 | if (ret) |
| 1041 | goto err; |
| 1042 | |
Boris BREZILLON | 5e9fb93 | 2015-12-10 09:00:03 +0100 | [diff] [blame] | 1043 | ret = fsl_ifc_chip_init_tail(mtd); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 1044 | if (ret) |
| 1045 | goto err; |
| 1046 | |
Boris BREZILLON | 5e9fb93 | 2015-12-10 09:00:03 +0100 | [diff] [blame] | 1047 | ret = nand_scan_tail(mtd); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 1048 | if (ret) |
| 1049 | goto err; |
| 1050 | |
| 1051 | /* First look for RedBoot table or partitions on the command |
| 1052 | * line, these take precedence over device tree information */ |
Boris BREZILLON | 5e9fb93 | 2015-12-10 09:00:03 +0100 | [diff] [blame] | 1053 | mtd_device_parse_register(mtd, part_probe_types, NULL, NULL, 0); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 1054 | |
| 1055 | dev_info(priv->dev, "IFC NAND device at 0x%llx, bank %d\n", |
| 1056 | (unsigned long long)res.start, priv->bank); |
| 1057 | return 0; |
| 1058 | |
| 1059 | err: |
| 1060 | fsl_ifc_chip_remove(priv); |
| 1061 | return ret; |
| 1062 | } |
| 1063 | |
| 1064 | static int fsl_ifc_nand_remove(struct platform_device *dev) |
| 1065 | { |
| 1066 | struct fsl_ifc_mtd *priv = dev_get_drvdata(&dev->dev); |
| 1067 | |
| 1068 | fsl_ifc_chip_remove(priv); |
| 1069 | |
| 1070 | mutex_lock(&fsl_ifc_nand_mutex); |
| 1071 | ifc_nand_ctrl->counter--; |
| 1072 | if (!ifc_nand_ctrl->counter) { |
| 1073 | fsl_ifc_ctrl_dev->nand = NULL; |
| 1074 | kfree(ifc_nand_ctrl); |
| 1075 | } |
| 1076 | mutex_unlock(&fsl_ifc_nand_mutex); |
| 1077 | |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | static const struct of_device_id fsl_ifc_nand_match[] = { |
| 1082 | { |
| 1083 | .compatible = "fsl,ifc-nand", |
| 1084 | }, |
| 1085 | {} |
| 1086 | }; |
Luis de Bethencourt | 3f7f7a5 | 2015-09-18 00:12:30 +0200 | [diff] [blame] | 1087 | MODULE_DEVICE_TABLE(of, fsl_ifc_nand_match); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 1088 | |
| 1089 | static struct platform_driver fsl_ifc_nand_driver = { |
| 1090 | .driver = { |
| 1091 | .name = "fsl,ifc-nand", |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 1092 | .of_match_table = fsl_ifc_nand_match, |
| 1093 | }, |
| 1094 | .probe = fsl_ifc_nand_probe, |
| 1095 | .remove = fsl_ifc_nand_remove, |
| 1096 | }; |
| 1097 | |
Sachin Kamat | c69ad0e | 2013-10-08 15:08:20 +0530 | [diff] [blame] | 1098 | module_platform_driver(fsl_ifc_nand_driver); |
Prabhakar Kushwaha | 8277188 | 2012-03-15 11:04:23 +0530 | [diff] [blame] | 1099 | |
| 1100 | MODULE_LICENSE("GPL"); |
| 1101 | MODULE_AUTHOR("Freescale"); |
| 1102 | MODULE_DESCRIPTION("Freescale Integrated Flash Controller MTD NAND driver"); |