blob: 2f6b55229d5b818d99965faf3d36bf20862e2a1b [file] [log] [blame]
Prabhakar Kushwaha82771882012-03-15 11:04:23 +05301/*
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 Kushwaha82771882012-03-15 11:04:23 +053025#include <linux/kernel.h>
Rob Herring5af50732013-09-17 14:28:33 -050026#include <linux/of_address.h>
Prabhakar Kushwaha82771882012-03-15 11:04:23 +053027#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 Kushwahad2ae2e22014-01-17 11:15:16 +053032#include <linux/fsl_ifc.h>
Prabhakar Kushwaha82771882012-03-15 11:04:23 +053033
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
39struct fsl_ifc_ctrl;
40
41/* mtd information per set */
42struct fsl_ifc_mtd {
Prabhakar Kushwaha82771882012-03-15 11:04:23 +053043 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 */
53struct fsl_ifc_nand_ctrl {
54 struct nand_hw_control controller;
55 struct fsl_ifc_mtd *chips[FSL_IFC_BANK_COUNT];
56
Aaron Sierra44544062014-04-07 11:58:12 -050057 void __iomem *addr; /* Address of assigned IFC buffer */
Prabhakar Kushwaha82771882012-03-15 11:04:23 +053058 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 Dunn3f91e942012-04-25 12:06:09 -070065 unsigned int max_bitflips; /* Saved during READ0 cmd */
Prabhakar Kushwaha82771882012-03-15 11:04:23 +053066};
67
68static struct fsl_ifc_nand_ctrl *ifc_nand_ctrl;
69
Prabhakar Kushwaha82771882012-03-15 11:04:23 +053070/*
71 * Generic flash bbt descriptors
72 */
73static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
74static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
75
76static 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
86static 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 Brezilloncaf51292016-02-09 17:01:57 +010096static 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
110static 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
142static const struct mtd_ooblayout_ops fsl_ifc_ooblayout_ops = {
143 .ecc = fsl_ifc_ooblayout_ecc,
144 .free = fsl_ifc_ooblayout_free,
145};
146
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530147/*
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 */
151static void set_addr(struct mtd_info *mtd, int column, int page_addr, int oob)
152{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100153 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100154 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530155 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
Raghav Dogra7a654172016-02-17 16:54:18 +0530156 struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530157 int buf_num;
158
159 ifc_nand_ctrl->page = page_addr;
160 /* Program ROW0/COL0 */
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500161 ifc_out32(page_addr, &ifc->ifc_nand.row0);
162 ifc_out32((oob ? IFC_NAND_COL_MS : 0) | column, &ifc->ifc_nand.col0);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530163
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
174static int is_blank(struct mtd_info *mtd, unsigned int bufnum)
175{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100176 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100177 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530178 u8 __iomem *addr = priv->vbase + bufnum * (mtd->writesize * 2);
Kim Phillips2caf87a2012-09-13 18:56:07 -0500179 u32 __iomem *mainarea = (u32 __iomem *)addr;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530180 u8 __iomem *oob = addr + mtd->writesize;
Boris Brezillon9ed92dd2016-02-03 20:11:32 +0100181 struct mtd_oob_region oobregion = { };
182 int i, section = 0;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530183
184 for (i = 0; i < mtd->writesize / 4; i++) {
185 if (__raw_readl(&mainarea[i]) != 0xffffffff)
186 return 0;
187 }
188
Boris Brezillon9ed92dd2016-02-03 20:11:32 +0100189 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 Kushwaha82771882012-03-15 11:04:23 +0530195
Boris Brezillon9ed92dd2016-02-03 20:11:32 +0100196 mtd_ooblayout_ecc(mtd, section++, &oobregion);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530197 }
198
199 return 1;
200}
201
202/* returns nonzero if entire page is blank */
203static int check_read_ecc(struct mtd_info *mtd, struct fsl_ifc_ctrl *ctrl,
Jagdish Gediyaeca95cb2018-03-21 05:51:46 +0530204 u32 eccstat, unsigned int bufnum)
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530205{
Jagdish Gediyaeca95cb2018-03-21 05:51:46 +0530206 return (eccstat >> ((3 - bufnum % 4) * 8)) & 15;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530207}
208
209/*
210 * execute IFC NAND command and wait for it to complete
211 */
212static void fsl_ifc_run_command(struct mtd_info *mtd)
213{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100214 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100215 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530216 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
217 struct fsl_ifc_nand_ctrl *nctrl = ifc_nand_ctrl;
Raghav Dogra7a654172016-02-17 16:54:18 +0530218 struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs;
Jagdish Gediyaeca95cb2018-03-21 05:51:46 +0530219 u32 eccstat;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530220 int i;
221
222 /* set the chip select for NAND Transaction */
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500223 ifc_out32(priv->bank << IFC_NAND_CSEL_SHIFT,
224 &ifc->ifc_nand.nand_csel);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530225
226 dev_vdbg(priv->dev,
227 "%s: fir0=%08x fcr0=%08x\n",
228 __func__,
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500229 ifc_in32(&ifc->ifc_nand.nand_fir0),
230 ifc_in32(&ifc->ifc_nand.nand_fcr0));
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530231
232 ctrl->nand_stat = 0;
233
234 /* start read/write seq */
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500235 ifc_out32(IFC_NAND_SEQ_STRT_FIR_STRT, &ifc->ifc_nand.nandseq_strt);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530236
237 /* wait for command complete flag or timeout */
238 wait_event_timeout(ctrl->nand_wait, ctrl->nand_stat,
Nicholas Mc Guire95d70662015-03-13 07:23:47 -0400239 msecs_to_jiffies(IFC_TIMEOUT_MSECS));
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530240
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 Dunn3f91e942012-04-25 12:06:09 -0700249 nctrl->max_bitflips = 0;
250
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530251 if (nctrl->eccread) {
252 int errors;
253 int bufnum = nctrl->page & priv->bufnum_mask;
Jagdish Gediyaeca95cb2018-03-21 05:51:46 +0530254 int sector_start = bufnum * chip->ecc.steps;
255 int sector_end = sector_start + chip->ecc.steps - 1;
Mark Marshall9d823932017-01-26 16:18:27 +0100256 __be32 *eccstat_regs;
257
Jagdish Gediya4d9ed682018-03-22 01:08:10 +0530258 eccstat_regs = ifc->ifc_nand.nand_eccstat;
Jagdish Gediyaeca95cb2018-03-21 05:51:46 +0530259 eccstat = ifc_in32(&eccstat_regs[sector_start / 4]);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530260
Jagdish Gediyaeca95cb2018-03-21 05:51:46 +0530261 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 Kushwaha82771882012-03-15 11:04:23 +0530265 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 Dunn3f91e942012-04-25 12:06:09 -0700283 nctrl->max_bitflips = max_t(unsigned int,
284 nctrl->max_bitflips,
285 errors);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530286 }
287
288 nctrl->eccread = 0;
289 }
290}
291
292static void fsl_ifc_do_read(struct nand_chip *chip,
293 int oob,
294 struct mtd_info *mtd)
295{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100296 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530297 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
Raghav Dogra7a654172016-02-17 16:54:18 +0530298 struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530299
300 /* Program FIR/IFC_NAND_FCR0 for Small/Large page */
301 if (mtd->writesize > 512) {
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500302 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 Kushwaha82771882012-03-15 11:04:23 +0530309
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500310 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 Kushwaha82771882012-03-15 11:04:23 +0530313 } else {
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500314 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 Kushwaha82771882012-03-15 11:04:23 +0530320
321 if (oob)
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500322 ifc_out32(NAND_CMD_READOOB <<
323 IFC_NAND_FCR0_CMD0_SHIFT,
324 &ifc->ifc_nand.nand_fcr0);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530325 else
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500326 ifc_out32(NAND_CMD_READ0 <<
327 IFC_NAND_FCR0_CMD0_SHIFT,
328 &ifc->ifc_nand.nand_fcr0);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530329 }
330}
331
332/* cmdfunc send commands to the IFC NAND Machine */
333static void fsl_ifc_cmdfunc(struct mtd_info *mtd, unsigned int command,
334 int column, int page_addr) {
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100335 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100336 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530337 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
Raghav Dogra7a654172016-02-17 16:54:18 +0530338 struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530339
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 Singhcf184dc2015-05-20 21:17:11 -0500348 ifc_out32(0, &ifc->ifc_nand.nand_fbcr);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530349 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 Singhcf184dc2015-05-20 21:17:11 -0500363 ifc_out32(mtd->oobsize - column, &ifc->ifc_nand.nand_fbcr);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530364 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 Kushwaha82771882012-03-15 11:04:23 +0530373 case NAND_CMD_READID:
Prabhakar Kushwaha59fdd5b2012-04-09 10:55:22 +0530374 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 Singhcf184dc2015-05-20 21:17:11 -0500379 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 Kushwaha59fdd5b2012-04-09 10:55:22 +0530386
387 /*
388 * although currently it's 8 bytes for READID, we always read
389 * the maximum 256 bytes(for PARAM)
390 */
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500391 ifc_out32(256, &ifc->ifc_nand.nand_fbcr);
Prabhakar Kushwaha59fdd5b2012-04-09 10:55:22 +0530392 ifc_nand_ctrl->read_bytes = 256;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530393
394 set_addr(mtd, 0, 0, 0);
395 fsl_ifc_run_command(mtd);
396 return;
Prabhakar Kushwaha59fdd5b2012-04-09 10:55:22 +0530397 }
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530398
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 Singhcf184dc2015-05-20 21:17:11 -0500406 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 Kushwaha82771882012-03-15 11:04:23 +0530410
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500411 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 Kushwaha82771882012-03-15 11:04:23 +0530414
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500415 ifc_out32(0, &ifc->ifc_nand.nand_fbcr);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530416 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 Kushwaha4af98742013-10-03 11:36:41 +0530429 (NAND_CMD_STATUS << IFC_NAND_FCR0_CMD1_SHIFT) |
430 (NAND_CMD_PAGEPROG << IFC_NAND_FCR0_CMD2_SHIFT);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530431
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500432 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 Kushwaha82771882012-03-15 11:04:23 +0530444 } else {
445 nand_fcr0 = ((NAND_CMD_PAGEPROG <<
446 IFC_NAND_FCR0_CMD1_SHIFT) |
447 (NAND_CMD_SEQIN <<
Prabhakar Kushwaha4af98742013-10-03 11:36:41 +0530448 IFC_NAND_FCR0_CMD2_SHIFT) |
449 (NAND_CMD_STATUS <<
450 IFC_NAND_FCR0_CMD3_SHIFT));
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530451
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500452 ifc_out32(
Kim Phillips0c69fb02013-01-11 16:23:59 -0600453 (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 Singhcf184dc2015-05-20 21:17:11 -0500459 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 Kushwaha82771882012-03-15 11:04:23 +0530465
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 Singhcf184dc2015-05-20 21:17:11 -0500479 ifc_out32(nand_fcr0, &ifc->ifc_nand.nand_fcr0);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530480 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 Singhcf184dc2015-05-20 21:17:11 -0500487 ifc_out32(ifc_nand_ctrl->index -
488 ifc_nand_ctrl->column,
489 &ifc->ifc_nand.nand_fbcr);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530490 } else {
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500491 ifc_out32(0, &ifc->ifc_nand.nand_fbcr);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530492 }
493
494 fsl_ifc_run_command(mtd);
495 return;
496 }
497
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500498 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 Kushwaha82771882012-03-15 11:04:23 +0530507 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 Singhcf184dc2015-05-20 21:17:11 -0500516 addr = ifc_nand_ctrl->addr;
Joe Schultz21704802014-04-07 11:58:18 -0500517 if (chip->options & NAND_BUSWIDTH_16)
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500518 ifc_out16(ifc_in16(addr) | (NAND_STATUS_WP), addr);
Joe Schultz21704802014-04-07 11:58:18 -0500519 else
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500520 ifc_out8(ifc_in8(addr) | (NAND_STATUS_WP), addr);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530521 return;
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500522 }
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530523
524 case NAND_CMD_RESET:
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500525 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 Kushwaha82771882012-03-15 11:04:23 +0530529 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
538static 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 */
548static void fsl_ifc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
549{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100550 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100551 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530552 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 Sierra44544062014-04-07 11:58:12 -0500566 memcpy_toio(ifc_nand_ctrl->addr + ifc_nand_ctrl->index, buf, len);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530567 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 */
574static uint8_t fsl_ifc_read_byte(struct mtd_info *mtd)
575{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100576 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100577 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
Aaron Sierra44544062014-04-07 11:58:12 -0500578 unsigned int offset;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530579
580 /*
581 * If there are still bytes in the IFC buffer, then use the
582 * next byte.
583 */
Aaron Sierra44544062014-04-07 11:58:12 -0500584 if (ifc_nand_ctrl->index < ifc_nand_ctrl->read_bytes) {
585 offset = ifc_nand_ctrl->index++;
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500586 return ifc_in8(ifc_nand_ctrl->addr + offset);
Aaron Sierra44544062014-04-07 11:58:12 -0500587 }
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530588
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 */
597static uint8_t fsl_ifc_read_byte16(struct mtd_info *mtd)
598{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100599 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100600 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530601 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 Singhcf184dc2015-05-20 21:17:11 -0500608 data = ifc_in16(ifc_nand_ctrl->addr + ifc_nand_ctrl->index);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530609 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 */
620static void fsl_ifc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
621{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100622 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100623 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530624 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 Sierra44544062014-04-07 11:58:12 -0500633 memcpy_fromio(buf, ifc_nand_ctrl->addr + ifc_nand_ctrl->index, avail);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530634 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 Kushwaha82771882012-03-15 11:04:23 +0530643 * This function is called after Program and Erase Operations to
644 * check for success or failure.
645 */
646static int fsl_ifc_wait(struct mtd_info *mtd, struct nand_chip *chip)
647{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100648 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530649 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
Raghav Dogra7a654172016-02-17 16:54:18 +0530650 struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530651 u32 nand_fsr;
Jagdish Gediya9b5dd842018-03-21 04:31:36 +0530652 int status;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530653
654 /* Use READ_STATUS command, but wait for the device to be ready */
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500655 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 Kushwaha82771882012-03-15 11:04:23 +0530661 set_addr(mtd, 0, 0, 0);
662 ifc_nand_ctrl->read_bytes = 1;
663
664 fsl_ifc_run_command(mtd);
665
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500666 nand_fsr = ifc_in32(&ifc->ifc_nand.nand_fsr);
Jagdish Gediya9b5dd842018-03-21 04:31:36 +0530667 status = nand_fsr >> 24;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530668 /*
669 * The chip always seems to report that it is
670 * write-protected, even when it is not.
671 */
Jagdish Gediya9b5dd842018-03-21 04:31:36 +0530672 return status | NAND_STATUS_WP;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530673}
674
Brian Norris1fbb9382012-05-02 10:14:55 -0700675static int fsl_ifc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
676 uint8_t *buf, int oob_required, int page)
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530677{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100678 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530679 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
Mike Dunn3f91e942012-04-25 12:06:09 -0700680 struct fsl_ifc_nand_ctrl *nctrl = ifc_nand_ctrl;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530681
682 fsl_ifc_read_buf(mtd, buf, mtd->writesize);
Brian Norrisa6976cd2012-05-02 10:15:01 -0700683 if (oob_required)
684 fsl_ifc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530685
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 Dunn3f91e942012-04-25 12:06:09 -0700692 return nctrl->max_bitflips;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530693}
694
695/* ECC will be calculated automatically, and errors will be detected in
696 * waitfunc.
697 */
Josh Wufdbad98d2012-06-25 18:07:45 +0800698static int fsl_ifc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +0200699 const uint8_t *buf, int oob_required, int page)
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530700{
701 fsl_ifc_write_buf(mtd, buf, mtd->writesize);
702 fsl_ifc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800703
704 return 0;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530705}
706
707static int fsl_ifc_chip_init_tail(struct mtd_info *mtd)
708{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100709 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100710 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530711
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 Kushwaha82771882012-03-15 11:04:23 +0530728 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 Brezilloncaf51292016-02-09 17:01:57 +0100736 dev_dbg(priv->dev, "%s: mtd->ooblayout = %p\n", __func__,
737 mtd->ooblayout);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530738 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 Kushwaha10bfa762012-09-13 14:24:49 +0530750static void fsl_ifc_sram_init(struct fsl_ifc_mtd *priv)
751{
752 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
Raghav Dogra7a654172016-02-17 16:54:18 +0530753 struct fsl_ifc_runtime __iomem *ifc_runtime = ctrl->rregs;
754 struct fsl_ifc_global __iomem *ifc_global = ctrl->gregs;
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530755 uint32_t csor = 0, csor_8k = 0, csor_ext = 0;
756 uint32_t cs = priv->bank;
757
758 /* Save CSOR and CSOR_ext */
Raghav Dogra7a654172016-02-17 16:54:18 +0530759 csor = ifc_in32(&ifc_global->csor_cs[cs].csor);
760 csor_ext = ifc_in32(&ifc_global->csor_cs[cs].csor_ext);
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530761
762 /* chage PageSize 8K and SpareSize 1K*/
763 csor_8k = (csor & ~(CSOR_NAND_PGS_MASK)) | 0x0018C000;
Raghav Dogra7a654172016-02-17 16:54:18 +0530764 ifc_out32(csor_8k, &ifc_global->csor_cs[cs].csor);
765 ifc_out32(0x0000400, &ifc_global->csor_cs[cs].csor_ext);
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530766
767 /* READID */
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -0500768 ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
Raghav Dogra7a654172016-02-17 16:54:18 +0530769 (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 Singhcf184dc2015-05-20 21:17:11 -0500772 ifc_out32(NAND_CMD_READID << IFC_NAND_FCR0_CMD0_SHIFT,
Raghav Dogra7a654172016-02-17 16:54:18 +0530773 &ifc_runtime->ifc_nand.nand_fcr0);
774 ifc_out32(0x0, &ifc_runtime->ifc_nand.row3);
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530775
Raghav Dogra7a654172016-02-17 16:54:18 +0530776 ifc_out32(0x0, &ifc_runtime->ifc_nand.nand_fbcr);
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530777
778 /* Program ROW0/COL0 */
Raghav Dogra7a654172016-02-17 16:54:18 +0530779 ifc_out32(0x0, &ifc_runtime->ifc_nand.row0);
780 ifc_out32(0x0, &ifc_runtime->ifc_nand.col0);
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530781
782 /* set the chip select for NAND Transaction */
Raghav Dogra7a654172016-02-17 16:54:18 +0530783 ifc_out32(cs << IFC_NAND_CSEL_SHIFT,
784 &ifc_runtime->ifc_nand.nand_csel);
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530785
786 /* start read seq */
Raghav Dogra7a654172016-02-17 16:54:18 +0530787 ifc_out32(IFC_NAND_SEQ_STRT_FIR_STRT,
788 &ifc_runtime->ifc_nand.nandseq_strt);
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530789
790 /* wait for command complete flag or timeout */
791 wait_event_timeout(ctrl->nand_wait, ctrl->nand_stat,
Nicholas Mc Guire95d70662015-03-13 07:23:47 -0400792 msecs_to_jiffies(IFC_TIMEOUT_MSECS));
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530793
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 Dogra7a654172016-02-17 16:54:18 +0530798 ifc_out32(csor, &ifc_global->csor_cs[cs].csor);
799 ifc_out32(csor_ext, &ifc_global->csor_cs[cs].csor_ext);
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530800}
801
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530802static int fsl_ifc_chip_init(struct fsl_ifc_mtd *priv)
803{
804 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
Raghav Dogra7a654172016-02-17 16:54:18 +0530805 struct fsl_ifc_global __iomem *ifc_global = ctrl->gregs;
806 struct fsl_ifc_runtime __iomem *ifc_runtime = ctrl->rregs;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530807 struct nand_chip *chip = &priv->chip;
Boris BREZILLON5e9fb932015-12-10 09:00:03 +0100808 struct mtd_info *mtd = nand_to_mtd(&priv->chip);
Aaron Sierra09691662014-08-26 18:18:33 -0500809 u32 csor;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530810
811 /* Fill in fsl_ifc_mtd structure */
Boris BREZILLON5e9fb932015-12-10 09:00:03 +0100812 mtd->dev.parent = priv->dev;
Brian Norrisa61ae812015-10-30 20:33:25 -0700813 nand_set_flash_node(chip, priv->dev->of_node);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530814
815 /* fill in nand_chip structure */
816 /* set up function call table */
Raghav Dogra7a654172016-02-17 16:54:18 +0530817 if ((ifc_in32(&ifc_global->cspr_cs[priv->bank].cspr))
818 & CSPR_PORT_SIZE_16)
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530819 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 Kushwaha82771882012-03-15 11:04:23 +0530825 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 Dogra7a654172016-02-17 16:54:18 +0530832 ifc_out32(0x0, &ifc_runtime->ifc_nand.ncfgr);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530833
834 /* set up nand options */
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530835 chip->bbt_options = NAND_BBT_USE_FLASH;
Scott Wood20cd0002013-04-10 17:34:37 -0500836 chip->options = NAND_NO_SUBPAGE_WRITE;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530837
Raghav Dogra7a654172016-02-17 16:54:18 +0530838 if (ifc_in32(&ifc_global->cspr_cs[priv->bank].cspr)
839 & CSPR_PORT_SIZE_16) {
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530840 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 BREZILLONd699ed22015-12-10 09:00:41 +0100847 nand_set_controller_data(chip, priv);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530848
849 chip->ecc.read_page = fsl_ifc_read_page;
850 chip->ecc.write_page = fsl_ifc_write_page;
851
Raghav Dogra7a654172016-02-17 16:54:18 +0530852 csor = ifc_in32(&ifc_global->csor_cs[priv->bank].csor);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530853
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530854 switch (csor & CSOR_NAND_PGS_MASK) {
855 case CSOR_NAND_PGS_512:
Boris Brezilloncaf51292016-02-09 17:01:57 +0100856 if (!(chip->options & NAND_BUSWIDTH_16)) {
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530857 /* 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 Kushwaha82771882012-03-15 11:04:23 +0530866 priv->bufnum_mask = 3;
867 break;
868
869 case CSOR_NAND_PGS_4K:
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530870 priv->bufnum_mask = 1;
871 break;
872
Prabhakar Kushwahaebff90b2013-09-24 16:41:23 +0530873 case CSOR_NAND_PGS_8K:
Prabhakar Kushwahaebff90b2013-09-24 16:41:23 +0530874 priv->bufnum_mask = 0;
Boris Brezilloncaf51292016-02-09 17:01:57 +0100875 break;
Prabhakar Kushwahaebff90b2013-09-24 16:41:23 +0530876
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530877 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 Brezilloncaf51292016-02-09 17:01:57 +0100885 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 Kushwaha82771882012-03-15 11:04:23 +0530896 } else {
897 chip->ecc.mode = NAND_ECC_SOFT;
Rafał Miłeckiff1ef352016-04-13 14:07:01 +0200898 chip->ecc.algo = NAND_ECC_HAMMING;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530899 }
900
Aaron Sierra09691662014-08-26 18:18:33 -0500901 if (ctrl->version == FSL_IFC_VERSION_1_1_0)
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530902 fsl_ifc_sram_init(priv);
903
Jagdish Gediya88347212017-11-23 17:04:31 +0530904 /*
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 Kushwaha82771882012-03-15 11:04:23 +0530911 return 0;
912}
913
914static int fsl_ifc_chip_remove(struct fsl_ifc_mtd *priv)
915{
Boris BREZILLON5e9fb932015-12-10 09:00:03 +0100916 struct mtd_info *mtd = nand_to_mtd(&priv->chip);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530917
Boris BREZILLON5e9fb932015-12-10 09:00:03 +0100918 nand_release(mtd);
919
920 kfree(mtd->name);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530921
922 if (priv->vbase)
923 iounmap(priv->vbase);
924
925 ifc_nand_ctrl->chips[priv->bank] = NULL;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530926
927 return 0;
928}
929
Raghav Dogra7a654172016-02-17 16:54:18 +0530930static int match_bank(struct fsl_ifc_global __iomem *ifc_global, int bank,
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530931 phys_addr_t addr)
932{
Raghav Dogra7a654172016-02-17 16:54:18 +0530933 u32 cspr = ifc_in32(&ifc_global->cspr_cs[bank].cspr);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530934
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
943static DEFINE_MUTEX(fsl_ifc_nand_mutex);
944
Bill Pemberton06f25512012-11-19 13:23:07 -0500945static int fsl_ifc_nand_probe(struct platform_device *dev)
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530946{
Raghav Dogra7a654172016-02-17 16:54:18 +0530947 struct fsl_ifc_runtime __iomem *ifc;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530948 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 BREZILLON5e9fb932015-12-10 09:00:03 +0100955 struct mtd_info *mtd;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530956
Raghav Dogra7a654172016-02-17 16:54:18 +0530957 if (!fsl_ifc_ctrl_dev || !fsl_ifc_ctrl_dev->rregs)
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530958 return -ENODEV;
Raghav Dogra7a654172016-02-17 16:54:18 +0530959 ifc = fsl_ifc_ctrl_dev->rregs;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530960
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 Sierra09691662014-08-26 18:18:33 -0500969 for (bank = 0; bank < fsl_ifc_ctrl_dev->banks; bank++) {
Raghav Dogra7a654172016-02-17 16:54:18 +0530970 if (match_bank(fsl_ifc_ctrl_dev->gregs, bank, res.start))
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530971 break;
972 }
973
Aaron Sierra09691662014-08-26 18:18:33 -0500974 if (bank >= fsl_ifc_ctrl_dev->banks) {
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530975 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 Kushwaha82771882012-03-15 11:04:23 +0530988 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 Gonzalezd45bc582016-07-27 11:23:52 +0200997 nand_hw_control_init(&ifc_nand_ctrl->controller);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530998 } 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 Singhcf184dc2015-05-20 21:17:11 -05001017 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 Kushwaha82771882012-03-15 11:04:23 +05301021
1022 /* enable NAND Machine Interrupts */
Jaiprakash Singhcf184dc2015-05-20 21:17:11 -05001023 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 BREZILLON5e9fb932015-12-10 09:00:03 +01001027
1028 mtd = nand_to_mtd(&priv->chip);
1029 mtd->name = kasprintf(GFP_KERNEL, "%llx.flash", (u64)res.start);
1030 if (!mtd->name) {
Prabhakar Kushwaha82771882012-03-15 11:04:23 +05301031 ret = -ENOMEM;
1032 goto err;
1033 }
1034
1035 ret = fsl_ifc_chip_init(priv);
1036 if (ret)
1037 goto err;
1038
Boris BREZILLON5e9fb932015-12-10 09:00:03 +01001039 ret = nand_scan_ident(mtd, 1, NULL);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +05301040 if (ret)
1041 goto err;
1042
Boris BREZILLON5e9fb932015-12-10 09:00:03 +01001043 ret = fsl_ifc_chip_init_tail(mtd);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +05301044 if (ret)
1045 goto err;
1046
Boris BREZILLON5e9fb932015-12-10 09:00:03 +01001047 ret = nand_scan_tail(mtd);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +05301048 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 BREZILLON5e9fb932015-12-10 09:00:03 +01001053 mtd_device_parse_register(mtd, part_probe_types, NULL, NULL, 0);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +05301054
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
1059err:
1060 fsl_ifc_chip_remove(priv);
1061 return ret;
1062}
1063
1064static 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
1081static const struct of_device_id fsl_ifc_nand_match[] = {
1082 {
1083 .compatible = "fsl,ifc-nand",
1084 },
1085 {}
1086};
Luis de Bethencourt3f7f7a52015-09-18 00:12:30 +02001087MODULE_DEVICE_TABLE(of, fsl_ifc_nand_match);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +05301088
1089static struct platform_driver fsl_ifc_nand_driver = {
1090 .driver = {
1091 .name = "fsl,ifc-nand",
Prabhakar Kushwaha82771882012-03-15 11:04:23 +05301092 .of_match_table = fsl_ifc_nand_match,
1093 },
1094 .probe = fsl_ifc_nand_probe,
1095 .remove = fsl_ifc_nand_remove,
1096};
1097
Sachin Kamatc69ad0e2013-10-08 15:08:20 +05301098module_platform_driver(fsl_ifc_nand_driver);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +05301099
1100MODULE_LICENSE("GPL");
1101MODULE_AUTHOR("Freescale");
1102MODULE_DESCRIPTION("Freescale Integrated Flash Controller MTD NAND driver");