blob: 9d1cf005878fece2b60ade3b17a5fad317a59238 [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>
25#include <linux/init.h>
26#include <linux/kernel.h>
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>
32#include <asm/fsl_ifc.h>
33
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +053034#define FSL_IFC_V1_1_0 0x01010000
Prabhakar Kushwaha82771882012-03-15 11:04:23 +053035#define ERR_BYTE 0xFF /* Value returned for read
36 bytes when read failed */
37#define IFC_TIMEOUT_MSECS 500 /* Maximum number of mSecs to wait
38 for IFC NAND Machine */
39
40struct fsl_ifc_ctrl;
41
42/* mtd information per set */
43struct fsl_ifc_mtd {
44 struct mtd_info mtd;
45 struct nand_chip chip;
46 struct fsl_ifc_ctrl *ctrl;
47
48 struct device *dev;
49 int bank; /* Chip select bank number */
50 unsigned int bufnum_mask; /* bufnum = page & bufnum_mask */
51 u8 __iomem *vbase; /* Chip select base virtual address */
52};
53
54/* overview of the fsl ifc controller */
55struct fsl_ifc_nand_ctrl {
56 struct nand_hw_control controller;
57 struct fsl_ifc_mtd *chips[FSL_IFC_BANK_COUNT];
58
59 u8 __iomem *addr; /* Address of assigned IFC buffer */
60 unsigned int page; /* Last page written to / read from */
61 unsigned int read_bytes;/* Number of bytes read during command */
62 unsigned int column; /* Saved column from SEQIN */
63 unsigned int index; /* Pointer to next byte to 'read' */
64 unsigned int oob; /* Non zero if operating on OOB data */
65 unsigned int eccread; /* Non zero for a full-page ECC read */
66 unsigned int counter; /* counter for the initializations */
Mike Dunn3f91e942012-04-25 12:06:09 -070067 unsigned int max_bitflips; /* Saved during READ0 cmd */
Prabhakar Kushwaha82771882012-03-15 11:04:23 +053068};
69
70static struct fsl_ifc_nand_ctrl *ifc_nand_ctrl;
71
72/* 512-byte page with 4-bit ECC, 8-bit */
73static struct nand_ecclayout oob_512_8bit_ecc4 = {
74 .eccbytes = 8,
75 .eccpos = {8, 9, 10, 11, 12, 13, 14, 15},
76 .oobfree = { {0, 5}, {6, 2} },
77};
78
79/* 512-byte page with 4-bit ECC, 16-bit */
80static struct nand_ecclayout oob_512_16bit_ecc4 = {
81 .eccbytes = 8,
82 .eccpos = {8, 9, 10, 11, 12, 13, 14, 15},
83 .oobfree = { {2, 6}, },
84};
85
86/* 2048-byte page size with 4-bit ECC */
87static struct nand_ecclayout oob_2048_ecc4 = {
88 .eccbytes = 32,
89 .eccpos = {
90 8, 9, 10, 11, 12, 13, 14, 15,
91 16, 17, 18, 19, 20, 21, 22, 23,
92 24, 25, 26, 27, 28, 29, 30, 31,
93 32, 33, 34, 35, 36, 37, 38, 39,
94 },
95 .oobfree = { {2, 6}, {40, 24} },
96};
97
98/* 4096-byte page size with 4-bit ECC */
99static struct nand_ecclayout oob_4096_ecc4 = {
100 .eccbytes = 64,
101 .eccpos = {
102 8, 9, 10, 11, 12, 13, 14, 15,
103 16, 17, 18, 19, 20, 21, 22, 23,
104 24, 25, 26, 27, 28, 29, 30, 31,
105 32, 33, 34, 35, 36, 37, 38, 39,
106 40, 41, 42, 43, 44, 45, 46, 47,
107 48, 49, 50, 51, 52, 53, 54, 55,
108 56, 57, 58, 59, 60, 61, 62, 63,
109 64, 65, 66, 67, 68, 69, 70, 71,
110 },
111 .oobfree = { {2, 6}, {72, 56} },
112};
113
114/* 4096-byte page size with 8-bit ECC -- requires 218-byte OOB */
115static struct nand_ecclayout oob_4096_ecc8 = {
116 .eccbytes = 128,
117 .eccpos = {
118 8, 9, 10, 11, 12, 13, 14, 15,
119 16, 17, 18, 19, 20, 21, 22, 23,
120 24, 25, 26, 27, 28, 29, 30, 31,
121 32, 33, 34, 35, 36, 37, 38, 39,
122 40, 41, 42, 43, 44, 45, 46, 47,
123 48, 49, 50, 51, 52, 53, 54, 55,
124 56, 57, 58, 59, 60, 61, 62, 63,
125 64, 65, 66, 67, 68, 69, 70, 71,
126 72, 73, 74, 75, 76, 77, 78, 79,
127 80, 81, 82, 83, 84, 85, 86, 87,
128 88, 89, 90, 91, 92, 93, 94, 95,
129 96, 97, 98, 99, 100, 101, 102, 103,
130 104, 105, 106, 107, 108, 109, 110, 111,
131 112, 113, 114, 115, 116, 117, 118, 119,
132 120, 121, 122, 123, 124, 125, 126, 127,
133 128, 129, 130, 131, 132, 133, 134, 135,
134 },
135 .oobfree = { {2, 6}, {136, 82} },
136};
137
Prabhakar Kushwahaebff90b2013-09-24 16:41:23 +0530138/* 8192-byte page size with 4-bit ECC */
139static struct nand_ecclayout oob_8192_ecc4 = {
140 .eccbytes = 128,
141 .eccpos = {
142 8, 9, 10, 11, 12, 13, 14, 15,
143 16, 17, 18, 19, 20, 21, 22, 23,
144 24, 25, 26, 27, 28, 29, 30, 31,
145 32, 33, 34, 35, 36, 37, 38, 39,
146 40, 41, 42, 43, 44, 45, 46, 47,
147 48, 49, 50, 51, 52, 53, 54, 55,
148 56, 57, 58, 59, 60, 61, 62, 63,
149 64, 65, 66, 67, 68, 69, 70, 71,
150 72, 73, 74, 75, 76, 77, 78, 79,
151 80, 81, 82, 83, 84, 85, 86, 87,
152 88, 89, 90, 91, 92, 93, 94, 95,
153 96, 97, 98, 99, 100, 101, 102, 103,
154 104, 105, 106, 107, 108, 109, 110, 111,
155 112, 113, 114, 115, 116, 117, 118, 119,
156 120, 121, 122, 123, 124, 125, 126, 127,
157 128, 129, 130, 131, 132, 133, 134, 135,
158 },
159 .oobfree = { {2, 6}, {136, 208} },
160};
161
162/* 8192-byte page size with 8-bit ECC -- requires 218-byte OOB */
163static struct nand_ecclayout oob_8192_ecc8 = {
164 .eccbytes = 256,
165 .eccpos = {
166 8, 9, 10, 11, 12, 13, 14, 15,
167 16, 17, 18, 19, 20, 21, 22, 23,
168 24, 25, 26, 27, 28, 29, 30, 31,
169 32, 33, 34, 35, 36, 37, 38, 39,
170 40, 41, 42, 43, 44, 45, 46, 47,
171 48, 49, 50, 51, 52, 53, 54, 55,
172 56, 57, 58, 59, 60, 61, 62, 63,
173 64, 65, 66, 67, 68, 69, 70, 71,
174 72, 73, 74, 75, 76, 77, 78, 79,
175 80, 81, 82, 83, 84, 85, 86, 87,
176 88, 89, 90, 91, 92, 93, 94, 95,
177 96, 97, 98, 99, 100, 101, 102, 103,
178 104, 105, 106, 107, 108, 109, 110, 111,
179 112, 113, 114, 115, 116, 117, 118, 119,
180 120, 121, 122, 123, 124, 125, 126, 127,
181 128, 129, 130, 131, 132, 133, 134, 135,
182 136, 137, 138, 139, 140, 141, 142, 143,
183 144, 145, 146, 147, 148, 149, 150, 151,
184 152, 153, 154, 155, 156, 157, 158, 159,
185 160, 161, 162, 163, 164, 165, 166, 167,
186 168, 169, 170, 171, 172, 173, 174, 175,
187 176, 177, 178, 179, 180, 181, 182, 183,
188 184, 185, 186, 187, 188, 189, 190, 191,
189 192, 193, 194, 195, 196, 197, 198, 199,
190 200, 201, 202, 203, 204, 205, 206, 207,
191 208, 209, 210, 211, 212, 213, 214, 215,
192 216, 217, 218, 219, 220, 221, 222, 223,
193 224, 225, 226, 227, 228, 229, 230, 231,
194 232, 233, 234, 235, 236, 237, 238, 239,
195 240, 241, 242, 243, 244, 245, 246, 247,
196 248, 249, 250, 251, 252, 253, 254, 255,
197 256, 257, 258, 259, 260, 261, 262, 263,
198 },
199 .oobfree = { {2, 6}, {264, 80} },
200};
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530201
202/*
203 * Generic flash bbt descriptors
204 */
205static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
206static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
207
208static struct nand_bbt_descr bbt_main_descr = {
209 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
210 NAND_BBT_2BIT | NAND_BBT_VERSION,
211 .offs = 2, /* 0 on 8-bit small page */
212 .len = 4,
213 .veroffs = 6,
214 .maxblocks = 4,
215 .pattern = bbt_pattern,
216};
217
218static struct nand_bbt_descr bbt_mirror_descr = {
219 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
220 NAND_BBT_2BIT | NAND_BBT_VERSION,
221 .offs = 2, /* 0 on 8-bit small page */
222 .len = 4,
223 .veroffs = 6,
224 .maxblocks = 4,
225 .pattern = mirror_pattern,
226};
227
228/*
229 * Set up the IFC hardware block and page address fields, and the ifc nand
230 * structure addr field to point to the correct IFC buffer in memory
231 */
232static void set_addr(struct mtd_info *mtd, int column, int page_addr, int oob)
233{
234 struct nand_chip *chip = mtd->priv;
235 struct fsl_ifc_mtd *priv = chip->priv;
236 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
237 struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
238 int buf_num;
239
240 ifc_nand_ctrl->page = page_addr;
241 /* Program ROW0/COL0 */
Kim Phillips0c69fb02013-01-11 16:23:59 -0600242 iowrite32be(page_addr, &ifc->ifc_nand.row0);
243 iowrite32be((oob ? IFC_NAND_COL_MS : 0) | column, &ifc->ifc_nand.col0);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530244
245 buf_num = page_addr & priv->bufnum_mask;
246
247 ifc_nand_ctrl->addr = priv->vbase + buf_num * (mtd->writesize * 2);
248 ifc_nand_ctrl->index = column;
249
250 /* for OOB data point to the second half of the buffer */
251 if (oob)
252 ifc_nand_ctrl->index += mtd->writesize;
253}
254
255static int is_blank(struct mtd_info *mtd, unsigned int bufnum)
256{
257 struct nand_chip *chip = mtd->priv;
258 struct fsl_ifc_mtd *priv = chip->priv;
259 u8 __iomem *addr = priv->vbase + bufnum * (mtd->writesize * 2);
Kim Phillips2caf87a2012-09-13 18:56:07 -0500260 u32 __iomem *mainarea = (u32 __iomem *)addr;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530261 u8 __iomem *oob = addr + mtd->writesize;
262 int i;
263
264 for (i = 0; i < mtd->writesize / 4; i++) {
265 if (__raw_readl(&mainarea[i]) != 0xffffffff)
266 return 0;
267 }
268
269 for (i = 0; i < chip->ecc.layout->eccbytes; i++) {
270 int pos = chip->ecc.layout->eccpos[i];
271
272 if (__raw_readb(&oob[pos]) != 0xff)
273 return 0;
274 }
275
276 return 1;
277}
278
279/* returns nonzero if entire page is blank */
280static int check_read_ecc(struct mtd_info *mtd, struct fsl_ifc_ctrl *ctrl,
281 u32 *eccstat, unsigned int bufnum)
282{
283 u32 reg = eccstat[bufnum / 4];
284 int errors;
285
286 errors = (reg >> ((3 - bufnum % 4) * 8)) & 15;
287
288 return errors;
289}
290
291/*
292 * execute IFC NAND command and wait for it to complete
293 */
294static void fsl_ifc_run_command(struct mtd_info *mtd)
295{
296 struct nand_chip *chip = mtd->priv;
297 struct fsl_ifc_mtd *priv = chip->priv;
298 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
299 struct fsl_ifc_nand_ctrl *nctrl = ifc_nand_ctrl;
300 struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
301 u32 eccstat[4];
302 int i;
303
304 /* set the chip select for NAND Transaction */
Kim Phillips0c69fb02013-01-11 16:23:59 -0600305 iowrite32be(priv->bank << IFC_NAND_CSEL_SHIFT,
306 &ifc->ifc_nand.nand_csel);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530307
308 dev_vdbg(priv->dev,
309 "%s: fir0=%08x fcr0=%08x\n",
310 __func__,
Kim Phillips0c69fb02013-01-11 16:23:59 -0600311 ioread32be(&ifc->ifc_nand.nand_fir0),
312 ioread32be(&ifc->ifc_nand.nand_fcr0));
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530313
314 ctrl->nand_stat = 0;
315
316 /* start read/write seq */
Kim Phillips0c69fb02013-01-11 16:23:59 -0600317 iowrite32be(IFC_NAND_SEQ_STRT_FIR_STRT, &ifc->ifc_nand.nandseq_strt);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530318
319 /* wait for command complete flag or timeout */
320 wait_event_timeout(ctrl->nand_wait, ctrl->nand_stat,
321 IFC_TIMEOUT_MSECS * HZ/1000);
322
323 /* ctrl->nand_stat will be updated from IRQ context */
324 if (!ctrl->nand_stat)
325 dev_err(priv->dev, "Controller is not responding\n");
326 if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_FTOER)
327 dev_err(priv->dev, "NAND Flash Timeout Error\n");
328 if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_WPER)
329 dev_err(priv->dev, "NAND Flash Write Protect Error\n");
330
Mike Dunn3f91e942012-04-25 12:06:09 -0700331 nctrl->max_bitflips = 0;
332
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530333 if (nctrl->eccread) {
334 int errors;
335 int bufnum = nctrl->page & priv->bufnum_mask;
336 int sector = bufnum * chip->ecc.steps;
337 int sector_end = sector + chip->ecc.steps - 1;
338
339 for (i = sector / 4; i <= sector_end / 4; i++)
Kim Phillips0c69fb02013-01-11 16:23:59 -0600340 eccstat[i] = ioread32be(&ifc->ifc_nand.nand_eccstat[i]);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530341
342 for (i = sector; i <= sector_end; i++) {
343 errors = check_read_ecc(mtd, ctrl, eccstat, i);
344
345 if (errors == 15) {
346 /*
347 * Uncorrectable error.
348 * OK only if the whole page is blank.
349 *
350 * We disable ECCER reporting due to...
351 * erratum IFC-A002770 -- so report it now if we
352 * see an uncorrectable error in ECCSTAT.
353 */
354 if (!is_blank(mtd, bufnum))
355 ctrl->nand_stat |=
356 IFC_NAND_EVTER_STAT_ECCER;
357 break;
358 }
359
360 mtd->ecc_stats.corrected += errors;
Mike Dunn3f91e942012-04-25 12:06:09 -0700361 nctrl->max_bitflips = max_t(unsigned int,
362 nctrl->max_bitflips,
363 errors);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530364 }
365
366 nctrl->eccread = 0;
367 }
368}
369
370static void fsl_ifc_do_read(struct nand_chip *chip,
371 int oob,
372 struct mtd_info *mtd)
373{
374 struct fsl_ifc_mtd *priv = chip->priv;
375 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
376 struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
377
378 /* Program FIR/IFC_NAND_FCR0 for Small/Large page */
379 if (mtd->writesize > 512) {
Kim Phillips0c69fb02013-01-11 16:23:59 -0600380 iowrite32be((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
381 (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
382 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
383 (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP3_SHIFT) |
384 (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP4_SHIFT),
385 &ifc->ifc_nand.nand_fir0);
386 iowrite32be(0x0, &ifc->ifc_nand.nand_fir1);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530387
Kim Phillips0c69fb02013-01-11 16:23:59 -0600388 iowrite32be((NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT) |
389 (NAND_CMD_READSTART << IFC_NAND_FCR0_CMD1_SHIFT),
390 &ifc->ifc_nand.nand_fcr0);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530391 } else {
Kim Phillips0c69fb02013-01-11 16:23:59 -0600392 iowrite32be((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
393 (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
394 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
395 (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP3_SHIFT),
396 &ifc->ifc_nand.nand_fir0);
397 iowrite32be(0x0, &ifc->ifc_nand.nand_fir1);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530398
399 if (oob)
Kim Phillips0c69fb02013-01-11 16:23:59 -0600400 iowrite32be(NAND_CMD_READOOB <<
401 IFC_NAND_FCR0_CMD0_SHIFT,
402 &ifc->ifc_nand.nand_fcr0);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530403 else
Kim Phillips0c69fb02013-01-11 16:23:59 -0600404 iowrite32be(NAND_CMD_READ0 <<
405 IFC_NAND_FCR0_CMD0_SHIFT,
406 &ifc->ifc_nand.nand_fcr0);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530407 }
408}
409
410/* cmdfunc send commands to the IFC NAND Machine */
411static void fsl_ifc_cmdfunc(struct mtd_info *mtd, unsigned int command,
412 int column, int page_addr) {
413 struct nand_chip *chip = mtd->priv;
414 struct fsl_ifc_mtd *priv = chip->priv;
415 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
416 struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
417
418 /* clear the read buffer */
419 ifc_nand_ctrl->read_bytes = 0;
420 if (command != NAND_CMD_PAGEPROG)
421 ifc_nand_ctrl->index = 0;
422
423 switch (command) {
424 /* READ0 read the entire buffer to use hardware ECC. */
425 case NAND_CMD_READ0:
Kim Phillips0c69fb02013-01-11 16:23:59 -0600426 iowrite32be(0, &ifc->ifc_nand.nand_fbcr);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530427 set_addr(mtd, 0, page_addr, 0);
428
429 ifc_nand_ctrl->read_bytes = mtd->writesize + mtd->oobsize;
430 ifc_nand_ctrl->index += column;
431
432 if (chip->ecc.mode == NAND_ECC_HW)
433 ifc_nand_ctrl->eccread = 1;
434
435 fsl_ifc_do_read(chip, 0, mtd);
436 fsl_ifc_run_command(mtd);
437 return;
438
439 /* READOOB reads only the OOB because no ECC is performed. */
440 case NAND_CMD_READOOB:
Kim Phillips0c69fb02013-01-11 16:23:59 -0600441 iowrite32be(mtd->oobsize - column, &ifc->ifc_nand.nand_fbcr);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530442 set_addr(mtd, column, page_addr, 1);
443
444 ifc_nand_ctrl->read_bytes = mtd->writesize + mtd->oobsize;
445
446 fsl_ifc_do_read(chip, 1, mtd);
447 fsl_ifc_run_command(mtd);
448
449 return;
450
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530451 case NAND_CMD_READID:
Prabhakar Kushwaha59fdd5b2012-04-09 10:55:22 +0530452 case NAND_CMD_PARAM: {
453 int timing = IFC_FIR_OP_RB;
454 if (command == NAND_CMD_PARAM)
455 timing = IFC_FIR_OP_RBCD;
456
Kim Phillips0c69fb02013-01-11 16:23:59 -0600457 iowrite32be((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
458 (IFC_FIR_OP_UA << IFC_NAND_FIR0_OP1_SHIFT) |
459 (timing << IFC_NAND_FIR0_OP2_SHIFT),
460 &ifc->ifc_nand.nand_fir0);
461 iowrite32be(command << IFC_NAND_FCR0_CMD0_SHIFT,
462 &ifc->ifc_nand.nand_fcr0);
463 iowrite32be(column, &ifc->ifc_nand.row3);
Prabhakar Kushwaha59fdd5b2012-04-09 10:55:22 +0530464
465 /*
466 * although currently it's 8 bytes for READID, we always read
467 * the maximum 256 bytes(for PARAM)
468 */
Kim Phillips0c69fb02013-01-11 16:23:59 -0600469 iowrite32be(256, &ifc->ifc_nand.nand_fbcr);
Prabhakar Kushwaha59fdd5b2012-04-09 10:55:22 +0530470 ifc_nand_ctrl->read_bytes = 256;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530471
472 set_addr(mtd, 0, 0, 0);
473 fsl_ifc_run_command(mtd);
474 return;
Prabhakar Kushwaha59fdd5b2012-04-09 10:55:22 +0530475 }
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530476
477 /* ERASE1 stores the block and page address */
478 case NAND_CMD_ERASE1:
479 set_addr(mtd, 0, page_addr, 0);
480 return;
481
482 /* ERASE2 uses the block and page address from ERASE1 */
483 case NAND_CMD_ERASE2:
Kim Phillips0c69fb02013-01-11 16:23:59 -0600484 iowrite32be((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
485 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP1_SHIFT) |
486 (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP2_SHIFT),
487 &ifc->ifc_nand.nand_fir0);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530488
Kim Phillips0c69fb02013-01-11 16:23:59 -0600489 iowrite32be((NAND_CMD_ERASE1 << IFC_NAND_FCR0_CMD0_SHIFT) |
490 (NAND_CMD_ERASE2 << IFC_NAND_FCR0_CMD1_SHIFT),
491 &ifc->ifc_nand.nand_fcr0);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530492
Kim Phillips0c69fb02013-01-11 16:23:59 -0600493 iowrite32be(0, &ifc->ifc_nand.nand_fbcr);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530494 ifc_nand_ctrl->read_bytes = 0;
495 fsl_ifc_run_command(mtd);
496 return;
497
498 /* SEQIN sets up the addr buffer and all registers except the length */
499 case NAND_CMD_SEQIN: {
500 u32 nand_fcr0;
501 ifc_nand_ctrl->column = column;
502 ifc_nand_ctrl->oob = 0;
503
504 if (mtd->writesize > 512) {
505 nand_fcr0 =
506 (NAND_CMD_SEQIN << IFC_NAND_FCR0_CMD0_SHIFT) |
507 (NAND_CMD_PAGEPROG << IFC_NAND_FCR0_CMD1_SHIFT);
508
Kim Phillips0c69fb02013-01-11 16:23:59 -0600509 iowrite32be(
510 (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
511 (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
512 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
513 (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP3_SHIFT) |
514 (IFC_FIR_OP_CW1 << IFC_NAND_FIR0_OP4_SHIFT),
515 &ifc->ifc_nand.nand_fir0);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530516 } else {
517 nand_fcr0 = ((NAND_CMD_PAGEPROG <<
518 IFC_NAND_FCR0_CMD1_SHIFT) |
519 (NAND_CMD_SEQIN <<
520 IFC_NAND_FCR0_CMD2_SHIFT));
521
Kim Phillips0c69fb02013-01-11 16:23:59 -0600522 iowrite32be(
523 (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
524 (IFC_FIR_OP_CMD2 << IFC_NAND_FIR0_OP1_SHIFT) |
525 (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP2_SHIFT) |
526 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP3_SHIFT) |
527 (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP4_SHIFT),
528 &ifc->ifc_nand.nand_fir0);
529 iowrite32be(IFC_FIR_OP_CW1 << IFC_NAND_FIR1_OP5_SHIFT,
530 &ifc->ifc_nand.nand_fir1);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530531
532 if (column >= mtd->writesize)
533 nand_fcr0 |=
534 NAND_CMD_READOOB << IFC_NAND_FCR0_CMD0_SHIFT;
535 else
536 nand_fcr0 |=
537 NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT;
538 }
539
540 if (column >= mtd->writesize) {
541 /* OOB area --> READOOB */
542 column -= mtd->writesize;
543 ifc_nand_ctrl->oob = 1;
544 }
Kim Phillips0c69fb02013-01-11 16:23:59 -0600545 iowrite32be(nand_fcr0, &ifc->ifc_nand.nand_fcr0);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530546 set_addr(mtd, column, page_addr, ifc_nand_ctrl->oob);
547 return;
548 }
549
550 /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
551 case NAND_CMD_PAGEPROG: {
552 if (ifc_nand_ctrl->oob) {
Kim Phillips0c69fb02013-01-11 16:23:59 -0600553 iowrite32be(ifc_nand_ctrl->index -
554 ifc_nand_ctrl->column,
555 &ifc->ifc_nand.nand_fbcr);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530556 } else {
Kim Phillips0c69fb02013-01-11 16:23:59 -0600557 iowrite32be(0, &ifc->ifc_nand.nand_fbcr);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530558 }
559
560 fsl_ifc_run_command(mtd);
561 return;
562 }
563
564 case NAND_CMD_STATUS:
Kim Phillips0c69fb02013-01-11 16:23:59 -0600565 iowrite32be((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
566 (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP1_SHIFT),
567 &ifc->ifc_nand.nand_fir0);
568 iowrite32be(NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT,
569 &ifc->ifc_nand.nand_fcr0);
570 iowrite32be(1, &ifc->ifc_nand.nand_fbcr);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530571 set_addr(mtd, 0, 0, 0);
572 ifc_nand_ctrl->read_bytes = 1;
573
574 fsl_ifc_run_command(mtd);
575
576 /*
577 * The chip always seems to report that it is
578 * write-protected, even when it is not.
579 */
580 setbits8(ifc_nand_ctrl->addr, NAND_STATUS_WP);
581 return;
582
583 case NAND_CMD_RESET:
Kim Phillips0c69fb02013-01-11 16:23:59 -0600584 iowrite32be(IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT,
585 &ifc->ifc_nand.nand_fir0);
586 iowrite32be(NAND_CMD_RESET << IFC_NAND_FCR0_CMD0_SHIFT,
587 &ifc->ifc_nand.nand_fcr0);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530588 fsl_ifc_run_command(mtd);
589 return;
590
591 default:
592 dev_err(priv->dev, "%s: error, unsupported command 0x%x.\n",
593 __func__, command);
594 }
595}
596
597static void fsl_ifc_select_chip(struct mtd_info *mtd, int chip)
598{
599 /* The hardware does not seem to support multiple
600 * chips per bank.
601 */
602}
603
604/*
605 * Write buf to the IFC NAND Controller Data Buffer
606 */
607static void fsl_ifc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
608{
609 struct nand_chip *chip = mtd->priv;
610 struct fsl_ifc_mtd *priv = chip->priv;
611 unsigned int bufsize = mtd->writesize + mtd->oobsize;
612
613 if (len <= 0) {
614 dev_err(priv->dev, "%s: len %d bytes", __func__, len);
615 return;
616 }
617
618 if ((unsigned int)len > bufsize - ifc_nand_ctrl->index) {
619 dev_err(priv->dev,
620 "%s: beyond end of buffer (%d requested, %u available)\n",
621 __func__, len, bufsize - ifc_nand_ctrl->index);
622 len = bufsize - ifc_nand_ctrl->index;
623 }
624
625 memcpy_toio(&ifc_nand_ctrl->addr[ifc_nand_ctrl->index], buf, len);
626 ifc_nand_ctrl->index += len;
627}
628
629/*
630 * Read a byte from either the IFC hardware buffer
631 * read function for 8-bit buswidth
632 */
633static uint8_t fsl_ifc_read_byte(struct mtd_info *mtd)
634{
635 struct nand_chip *chip = mtd->priv;
636 struct fsl_ifc_mtd *priv = chip->priv;
637
638 /*
639 * If there are still bytes in the IFC buffer, then use the
640 * next byte.
641 */
642 if (ifc_nand_ctrl->index < ifc_nand_ctrl->read_bytes)
643 return in_8(&ifc_nand_ctrl->addr[ifc_nand_ctrl->index++]);
644
645 dev_err(priv->dev, "%s: beyond end of buffer\n", __func__);
646 return ERR_BYTE;
647}
648
649/*
650 * Read two bytes from the IFC hardware buffer
651 * read function for 16-bit buswith
652 */
653static uint8_t fsl_ifc_read_byte16(struct mtd_info *mtd)
654{
655 struct nand_chip *chip = mtd->priv;
656 struct fsl_ifc_mtd *priv = chip->priv;
657 uint16_t data;
658
659 /*
660 * If there are still bytes in the IFC buffer, then use the
661 * next byte.
662 */
663 if (ifc_nand_ctrl->index < ifc_nand_ctrl->read_bytes) {
Kim Phillips2caf87a2012-09-13 18:56:07 -0500664 data = in_be16((uint16_t __iomem *)&ifc_nand_ctrl->
665 addr[ifc_nand_ctrl->index]);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530666 ifc_nand_ctrl->index += 2;
667 return (uint8_t) data;
668 }
669
670 dev_err(priv->dev, "%s: beyond end of buffer\n", __func__);
671 return ERR_BYTE;
672}
673
674/*
675 * Read from the IFC Controller Data Buffer
676 */
677static void fsl_ifc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
678{
679 struct nand_chip *chip = mtd->priv;
680 struct fsl_ifc_mtd *priv = chip->priv;
681 int avail;
682
683 if (len < 0) {
684 dev_err(priv->dev, "%s: len %d bytes", __func__, len);
685 return;
686 }
687
688 avail = min((unsigned int)len,
689 ifc_nand_ctrl->read_bytes - ifc_nand_ctrl->index);
690 memcpy_fromio(buf, &ifc_nand_ctrl->addr[ifc_nand_ctrl->index], avail);
691 ifc_nand_ctrl->index += avail;
692
693 if (len > avail)
694 dev_err(priv->dev,
695 "%s: beyond end of buffer (%d requested, %d available)\n",
696 __func__, len, avail);
697}
698
699/*
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530700 * This function is called after Program and Erase Operations to
701 * check for success or failure.
702 */
703static int fsl_ifc_wait(struct mtd_info *mtd, struct nand_chip *chip)
704{
705 struct fsl_ifc_mtd *priv = chip->priv;
706 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
707 struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
708 u32 nand_fsr;
709
710 /* Use READ_STATUS command, but wait for the device to be ready */
Kim Phillips0c69fb02013-01-11 16:23:59 -0600711 iowrite32be((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
712 (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR0_OP1_SHIFT),
713 &ifc->ifc_nand.nand_fir0);
714 iowrite32be(NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT,
715 &ifc->ifc_nand.nand_fcr0);
716 iowrite32be(1, &ifc->ifc_nand.nand_fbcr);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530717 set_addr(mtd, 0, 0, 0);
718 ifc_nand_ctrl->read_bytes = 1;
719
720 fsl_ifc_run_command(mtd);
721
Kim Phillips0c69fb02013-01-11 16:23:59 -0600722 nand_fsr = ioread32be(&ifc->ifc_nand.nand_fsr);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530723
724 /*
725 * The chip always seems to report that it is
726 * write-protected, even when it is not.
727 */
728 return nand_fsr | NAND_STATUS_WP;
729}
730
Brian Norris1fbb9382012-05-02 10:14:55 -0700731static int fsl_ifc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
732 uint8_t *buf, int oob_required, int page)
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530733{
734 struct fsl_ifc_mtd *priv = chip->priv;
735 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
Mike Dunn3f91e942012-04-25 12:06:09 -0700736 struct fsl_ifc_nand_ctrl *nctrl = ifc_nand_ctrl;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530737
738 fsl_ifc_read_buf(mtd, buf, mtd->writesize);
Brian Norrisa6976cd2012-05-02 10:15:01 -0700739 if (oob_required)
740 fsl_ifc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530741
742 if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_ECCER)
743 dev_err(priv->dev, "NAND Flash ECC Uncorrectable Error\n");
744
745 if (ctrl->nand_stat != IFC_NAND_EVTER_STAT_OPC)
746 mtd->ecc_stats.failed++;
747
Mike Dunn3f91e942012-04-25 12:06:09 -0700748 return nctrl->max_bitflips;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530749}
750
751/* ECC will be calculated automatically, and errors will be detected in
752 * waitfunc.
753 */
Josh Wufdbad98d2012-06-25 18:07:45 +0800754static int fsl_ifc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700755 const uint8_t *buf, int oob_required)
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530756{
757 fsl_ifc_write_buf(mtd, buf, mtd->writesize);
758 fsl_ifc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800759
760 return 0;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530761}
762
763static int fsl_ifc_chip_init_tail(struct mtd_info *mtd)
764{
765 struct nand_chip *chip = mtd->priv;
766 struct fsl_ifc_mtd *priv = chip->priv;
767
768 dev_dbg(priv->dev, "%s: nand->numchips = %d\n", __func__,
769 chip->numchips);
770 dev_dbg(priv->dev, "%s: nand->chipsize = %lld\n", __func__,
771 chip->chipsize);
772 dev_dbg(priv->dev, "%s: nand->pagemask = %8x\n", __func__,
773 chip->pagemask);
774 dev_dbg(priv->dev, "%s: nand->chip_delay = %d\n", __func__,
775 chip->chip_delay);
776 dev_dbg(priv->dev, "%s: nand->badblockpos = %d\n", __func__,
777 chip->badblockpos);
778 dev_dbg(priv->dev, "%s: nand->chip_shift = %d\n", __func__,
779 chip->chip_shift);
780 dev_dbg(priv->dev, "%s: nand->page_shift = %d\n", __func__,
781 chip->page_shift);
782 dev_dbg(priv->dev, "%s: nand->phys_erase_shift = %d\n", __func__,
783 chip->phys_erase_shift);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530784 dev_dbg(priv->dev, "%s: nand->ecc.mode = %d\n", __func__,
785 chip->ecc.mode);
786 dev_dbg(priv->dev, "%s: nand->ecc.steps = %d\n", __func__,
787 chip->ecc.steps);
788 dev_dbg(priv->dev, "%s: nand->ecc.bytes = %d\n", __func__,
789 chip->ecc.bytes);
790 dev_dbg(priv->dev, "%s: nand->ecc.total = %d\n", __func__,
791 chip->ecc.total);
792 dev_dbg(priv->dev, "%s: nand->ecc.layout = %p\n", __func__,
793 chip->ecc.layout);
794 dev_dbg(priv->dev, "%s: mtd->flags = %08x\n", __func__, mtd->flags);
795 dev_dbg(priv->dev, "%s: mtd->size = %lld\n", __func__, mtd->size);
796 dev_dbg(priv->dev, "%s: mtd->erasesize = %d\n", __func__,
797 mtd->erasesize);
798 dev_dbg(priv->dev, "%s: mtd->writesize = %d\n", __func__,
799 mtd->writesize);
800 dev_dbg(priv->dev, "%s: mtd->oobsize = %d\n", __func__,
801 mtd->oobsize);
802
803 return 0;
804}
805
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530806static void fsl_ifc_sram_init(struct fsl_ifc_mtd *priv)
807{
808 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
809 struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
810 uint32_t csor = 0, csor_8k = 0, csor_ext = 0;
811 uint32_t cs = priv->bank;
812
813 /* Save CSOR and CSOR_ext */
Kim Phillips0c69fb02013-01-11 16:23:59 -0600814 csor = ioread32be(&ifc->csor_cs[cs].csor);
815 csor_ext = ioread32be(&ifc->csor_cs[cs].csor_ext);
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530816
817 /* chage PageSize 8K and SpareSize 1K*/
818 csor_8k = (csor & ~(CSOR_NAND_PGS_MASK)) | 0x0018C000;
Kim Phillips0c69fb02013-01-11 16:23:59 -0600819 iowrite32be(csor_8k, &ifc->csor_cs[cs].csor);
820 iowrite32be(0x0000400, &ifc->csor_cs[cs].csor_ext);
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530821
822 /* READID */
Kim Phillips0c69fb02013-01-11 16:23:59 -0600823 iowrite32be((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
824 (IFC_FIR_OP_UA << IFC_NAND_FIR0_OP1_SHIFT) |
825 (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP2_SHIFT),
826 &ifc->ifc_nand.nand_fir0);
827 iowrite32be(NAND_CMD_READID << IFC_NAND_FCR0_CMD0_SHIFT,
828 &ifc->ifc_nand.nand_fcr0);
829 iowrite32be(0x0, &ifc->ifc_nand.row3);
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530830
Kim Phillips0c69fb02013-01-11 16:23:59 -0600831 iowrite32be(0x0, &ifc->ifc_nand.nand_fbcr);
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530832
833 /* Program ROW0/COL0 */
Kim Phillips0c69fb02013-01-11 16:23:59 -0600834 iowrite32be(0x0, &ifc->ifc_nand.row0);
835 iowrite32be(0x0, &ifc->ifc_nand.col0);
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530836
837 /* set the chip select for NAND Transaction */
Kim Phillips0c69fb02013-01-11 16:23:59 -0600838 iowrite32be(cs << IFC_NAND_CSEL_SHIFT, &ifc->ifc_nand.nand_csel);
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530839
840 /* start read seq */
Kim Phillips0c69fb02013-01-11 16:23:59 -0600841 iowrite32be(IFC_NAND_SEQ_STRT_FIR_STRT, &ifc->ifc_nand.nandseq_strt);
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530842
843 /* wait for command complete flag or timeout */
844 wait_event_timeout(ctrl->nand_wait, ctrl->nand_stat,
845 IFC_TIMEOUT_MSECS * HZ/1000);
846
847 if (ctrl->nand_stat != IFC_NAND_EVTER_STAT_OPC)
848 printk(KERN_ERR "fsl-ifc: Failed to Initialise SRAM\n");
849
850 /* Restore CSOR and CSOR_ext */
Kim Phillips0c69fb02013-01-11 16:23:59 -0600851 iowrite32be(csor, &ifc->csor_cs[cs].csor);
852 iowrite32be(csor_ext, &ifc->csor_cs[cs].csor_ext);
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530853}
854
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530855static int fsl_ifc_chip_init(struct fsl_ifc_mtd *priv)
856{
857 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
858 struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
859 struct nand_chip *chip = &priv->chip;
860 struct nand_ecclayout *layout;
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530861 u32 csor, ver;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530862
863 /* Fill in fsl_ifc_mtd structure */
864 priv->mtd.priv = chip;
865 priv->mtd.owner = THIS_MODULE;
866
867 /* fill in nand_chip structure */
868 /* set up function call table */
Kim Phillips0c69fb02013-01-11 16:23:59 -0600869 if ((ioread32be(&ifc->cspr_cs[priv->bank].cspr)) & CSPR_PORT_SIZE_16)
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530870 chip->read_byte = fsl_ifc_read_byte16;
871 else
872 chip->read_byte = fsl_ifc_read_byte;
873
874 chip->write_buf = fsl_ifc_write_buf;
875 chip->read_buf = fsl_ifc_read_buf;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530876 chip->select_chip = fsl_ifc_select_chip;
877 chip->cmdfunc = fsl_ifc_cmdfunc;
878 chip->waitfunc = fsl_ifc_wait;
879
880 chip->bbt_td = &bbt_main_descr;
881 chip->bbt_md = &bbt_mirror_descr;
882
Kim Phillips0c69fb02013-01-11 16:23:59 -0600883 iowrite32be(0x0, &ifc->ifc_nand.ncfgr);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530884
885 /* set up nand options */
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530886 chip->bbt_options = NAND_BBT_USE_FLASH;
Scott Wood20cd0002013-04-10 17:34:37 -0500887 chip->options = NAND_NO_SUBPAGE_WRITE;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530888
Kim Phillips0c69fb02013-01-11 16:23:59 -0600889 if (ioread32be(&ifc->cspr_cs[priv->bank].cspr) & CSPR_PORT_SIZE_16) {
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530890 chip->read_byte = fsl_ifc_read_byte16;
891 chip->options |= NAND_BUSWIDTH_16;
892 } else {
893 chip->read_byte = fsl_ifc_read_byte;
894 }
895
896 chip->controller = &ifc_nand_ctrl->controller;
897 chip->priv = priv;
898
899 chip->ecc.read_page = fsl_ifc_read_page;
900 chip->ecc.write_page = fsl_ifc_write_page;
901
Kim Phillips0c69fb02013-01-11 16:23:59 -0600902 csor = ioread32be(&ifc->csor_cs[priv->bank].csor);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530903
904 /* Hardware generates ECC per 512 Bytes */
905 chip->ecc.size = 512;
906 chip->ecc.bytes = 8;
Mike Dunn44df4d12012-04-25 12:06:06 -0700907 chip->ecc.strength = 4;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530908
909 switch (csor & CSOR_NAND_PGS_MASK) {
910 case CSOR_NAND_PGS_512:
911 if (chip->options & NAND_BUSWIDTH_16) {
912 layout = &oob_512_16bit_ecc4;
913 } else {
914 layout = &oob_512_8bit_ecc4;
915
916 /* Avoid conflict with bad block marker */
917 bbt_main_descr.offs = 0;
918 bbt_mirror_descr.offs = 0;
919 }
920
921 priv->bufnum_mask = 15;
922 break;
923
924 case CSOR_NAND_PGS_2K:
925 layout = &oob_2048_ecc4;
926 priv->bufnum_mask = 3;
927 break;
928
929 case CSOR_NAND_PGS_4K:
930 if ((csor & CSOR_NAND_ECC_MODE_MASK) ==
931 CSOR_NAND_ECC_MODE_4) {
932 layout = &oob_4096_ecc4;
933 } else {
934 layout = &oob_4096_ecc8;
935 chip->ecc.bytes = 16;
Prabhakar Kushwahaebff90b2013-09-24 16:41:23 +0530936 chip->ecc.strength = 8;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530937 }
938
939 priv->bufnum_mask = 1;
940 break;
941
Prabhakar Kushwahaebff90b2013-09-24 16:41:23 +0530942 case CSOR_NAND_PGS_8K:
943 if ((csor & CSOR_NAND_ECC_MODE_MASK) ==
944 CSOR_NAND_ECC_MODE_4) {
945 layout = &oob_8192_ecc4;
946 } else {
947 layout = &oob_8192_ecc8;
948 chip->ecc.bytes = 16;
949 chip->ecc.strength = 8;
950 }
951
952 priv->bufnum_mask = 0;
953 break;
954
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530955 default:
956 dev_err(priv->dev, "bad csor %#x: bad page size\n", csor);
957 return -ENODEV;
958 }
959
960 /* Must also set CSOR_NAND_ECC_ENC_EN if DEC_EN set */
961 if (csor & CSOR_NAND_ECC_DEC_EN) {
962 chip->ecc.mode = NAND_ECC_HW;
963 chip->ecc.layout = layout;
964 } else {
965 chip->ecc.mode = NAND_ECC_SOFT;
966 }
967
Kim Phillips0c69fb02013-01-11 16:23:59 -0600968 ver = ioread32be(&ifc->ifc_rev);
Prabhakar Kushwaha10bfa762012-09-13 14:24:49 +0530969 if (ver == FSL_IFC_V1_1_0)
970 fsl_ifc_sram_init(priv);
971
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530972 return 0;
973}
974
975static int fsl_ifc_chip_remove(struct fsl_ifc_mtd *priv)
976{
977 nand_release(&priv->mtd);
978
979 kfree(priv->mtd.name);
980
981 if (priv->vbase)
982 iounmap(priv->vbase);
983
984 ifc_nand_ctrl->chips[priv->bank] = NULL;
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530985
986 return 0;
987}
988
989static int match_bank(struct fsl_ifc_regs __iomem *ifc, int bank,
990 phys_addr_t addr)
991{
Kim Phillips0c69fb02013-01-11 16:23:59 -0600992 u32 cspr = ioread32be(&ifc->cspr_cs[bank].cspr);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +0530993
994 if (!(cspr & CSPR_V))
995 return 0;
996 if ((cspr & CSPR_MSEL) != CSPR_MSEL_NAND)
997 return 0;
998
999 return (cspr & CSPR_BA) == convert_ifc_address(addr);
1000}
1001
1002static DEFINE_MUTEX(fsl_ifc_nand_mutex);
1003
Bill Pemberton06f25512012-11-19 13:23:07 -05001004static int fsl_ifc_nand_probe(struct platform_device *dev)
Prabhakar Kushwaha82771882012-03-15 11:04:23 +05301005{
1006 struct fsl_ifc_regs __iomem *ifc;
1007 struct fsl_ifc_mtd *priv;
1008 struct resource res;
1009 static const char *part_probe_types[]
1010 = { "cmdlinepart", "RedBoot", "ofpart", NULL };
1011 int ret;
1012 int bank;
1013 struct device_node *node = dev->dev.of_node;
1014 struct mtd_part_parser_data ppdata;
1015
1016 ppdata.of_node = dev->dev.of_node;
1017 if (!fsl_ifc_ctrl_dev || !fsl_ifc_ctrl_dev->regs)
1018 return -ENODEV;
1019 ifc = fsl_ifc_ctrl_dev->regs;
1020
1021 /* get, allocate and map the memory resource */
1022 ret = of_address_to_resource(node, 0, &res);
1023 if (ret) {
1024 dev_err(&dev->dev, "%s: failed to get resource\n", __func__);
1025 return ret;
1026 }
1027
1028 /* find which chip select it is connected to */
1029 for (bank = 0; bank < FSL_IFC_BANK_COUNT; bank++) {
1030 if (match_bank(ifc, bank, res.start))
1031 break;
1032 }
1033
1034 if (bank >= FSL_IFC_BANK_COUNT) {
1035 dev_err(&dev->dev, "%s: address did not match any chip selects\n",
1036 __func__);
1037 return -ENODEV;
1038 }
1039
1040 priv = devm_kzalloc(&dev->dev, sizeof(*priv), GFP_KERNEL);
1041 if (!priv)
1042 return -ENOMEM;
1043
1044 mutex_lock(&fsl_ifc_nand_mutex);
1045 if (!fsl_ifc_ctrl_dev->nand) {
1046 ifc_nand_ctrl = kzalloc(sizeof(*ifc_nand_ctrl), GFP_KERNEL);
1047 if (!ifc_nand_ctrl) {
1048 dev_err(&dev->dev, "failed to allocate memory\n");
1049 mutex_unlock(&fsl_ifc_nand_mutex);
1050 return -ENOMEM;
1051 }
1052
1053 ifc_nand_ctrl->read_bytes = 0;
1054 ifc_nand_ctrl->index = 0;
1055 ifc_nand_ctrl->addr = NULL;
1056 fsl_ifc_ctrl_dev->nand = ifc_nand_ctrl;
1057
1058 spin_lock_init(&ifc_nand_ctrl->controller.lock);
1059 init_waitqueue_head(&ifc_nand_ctrl->controller.wq);
1060 } else {
1061 ifc_nand_ctrl = fsl_ifc_ctrl_dev->nand;
1062 }
1063 mutex_unlock(&fsl_ifc_nand_mutex);
1064
1065 ifc_nand_ctrl->chips[bank] = priv;
1066 priv->bank = bank;
1067 priv->ctrl = fsl_ifc_ctrl_dev;
1068 priv->dev = &dev->dev;
1069
1070 priv->vbase = ioremap(res.start, resource_size(&res));
1071 if (!priv->vbase) {
1072 dev_err(priv->dev, "%s: failed to map chip region\n", __func__);
1073 ret = -ENOMEM;
1074 goto err;
1075 }
1076
1077 dev_set_drvdata(priv->dev, priv);
1078
Kim Phillips0c69fb02013-01-11 16:23:59 -06001079 iowrite32be(IFC_NAND_EVTER_EN_OPC_EN |
1080 IFC_NAND_EVTER_EN_FTOER_EN |
1081 IFC_NAND_EVTER_EN_WPER_EN,
1082 &ifc->ifc_nand.nand_evter_en);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +05301083
1084 /* enable NAND Machine Interrupts */
Kim Phillips0c69fb02013-01-11 16:23:59 -06001085 iowrite32be(IFC_NAND_EVTER_INTR_OPCIR_EN |
1086 IFC_NAND_EVTER_INTR_FTOERIR_EN |
1087 IFC_NAND_EVTER_INTR_WPERIR_EN,
1088 &ifc->ifc_nand.nand_evter_intr_en);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +05301089 priv->mtd.name = kasprintf(GFP_KERNEL, "%x.flash", (unsigned)res.start);
1090 if (!priv->mtd.name) {
1091 ret = -ENOMEM;
1092 goto err;
1093 }
1094
1095 ret = fsl_ifc_chip_init(priv);
1096 if (ret)
1097 goto err;
1098
1099 ret = nand_scan_ident(&priv->mtd, 1, NULL);
1100 if (ret)
1101 goto err;
1102
1103 ret = fsl_ifc_chip_init_tail(&priv->mtd);
1104 if (ret)
1105 goto err;
1106
1107 ret = nand_scan_tail(&priv->mtd);
1108 if (ret)
1109 goto err;
1110
1111 /* First look for RedBoot table or partitions on the command
1112 * line, these take precedence over device tree information */
1113 mtd_device_parse_register(&priv->mtd, part_probe_types, &ppdata,
1114 NULL, 0);
1115
1116 dev_info(priv->dev, "IFC NAND device at 0x%llx, bank %d\n",
1117 (unsigned long long)res.start, priv->bank);
1118 return 0;
1119
1120err:
1121 fsl_ifc_chip_remove(priv);
1122 return ret;
1123}
1124
1125static int fsl_ifc_nand_remove(struct platform_device *dev)
1126{
1127 struct fsl_ifc_mtd *priv = dev_get_drvdata(&dev->dev);
1128
1129 fsl_ifc_chip_remove(priv);
1130
1131 mutex_lock(&fsl_ifc_nand_mutex);
1132 ifc_nand_ctrl->counter--;
1133 if (!ifc_nand_ctrl->counter) {
1134 fsl_ifc_ctrl_dev->nand = NULL;
1135 kfree(ifc_nand_ctrl);
1136 }
1137 mutex_unlock(&fsl_ifc_nand_mutex);
1138
1139 return 0;
1140}
1141
1142static const struct of_device_id fsl_ifc_nand_match[] = {
1143 {
1144 .compatible = "fsl,ifc-nand",
1145 },
1146 {}
1147};
1148
1149static struct platform_driver fsl_ifc_nand_driver = {
1150 .driver = {
1151 .name = "fsl,ifc-nand",
1152 .owner = THIS_MODULE,
1153 .of_match_table = fsl_ifc_nand_match,
1154 },
1155 .probe = fsl_ifc_nand_probe,
1156 .remove = fsl_ifc_nand_remove,
1157};
1158
Sachin Kamatc69ad0e2013-10-08 15:08:20 +05301159module_platform_driver(fsl_ifc_nand_driver);
Prabhakar Kushwaha82771882012-03-15 11:04:23 +05301160
1161MODULE_LICENSE("GPL");
1162MODULE_AUTHOR("Freescale");
1163MODULE_DESCRIPTION("Freescale Integrated Flash Controller MTD NAND driver");