blob: 5173fadc9a4e637f01817ed040a42b72af1d7a68 [file] [log] [blame]
Sascha Hauer34f6e152008-09-02 17:16:59 +02001/*
2 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
3 * Copyright 2008 Sascha Hauer, kernel@pengutronix.de
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301, USA.
18 */
19
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/mtd/mtd.h>
25#include <linux/mtd/nand.h>
26#include <linux/mtd/partitions.h>
27#include <linux/interrupt.h>
28#include <linux/device.h>
29#include <linux/platform_device.h>
30#include <linux/clk.h>
31#include <linux/err.h>
32#include <linux/io.h>
Sascha Hauer63f14742010-10-18 10:16:26 +020033#include <linux/irq.h>
34#include <linux/completion.h>
Sachin Kamatd367e372013-10-18 16:16:35 +053035#include <linux/of.h>
Uwe Kleine-König64363562012-04-23 11:23:41 +020036#include <linux/of_device.h>
Sascha Hauer34f6e152008-09-02 17:16:59 +020037
38#include <asm/mach/flash.h>
Arnd Bergmann82906b12012-08-24 15:14:29 +020039#include <linux/platform_data/mtd-mxc_nand.h>
Sascha Hauer34f6e152008-09-02 17:16:59 +020040
41#define DRIVER_NAME "mxc_nand"
42
43/* Addresses for NFC registers */
Sascha Hauer1bc99182010-08-06 15:53:08 +020044#define NFC_V1_V2_BUF_SIZE (host->regs + 0x00)
45#define NFC_V1_V2_BUF_ADDR (host->regs + 0x04)
46#define NFC_V1_V2_FLASH_ADDR (host->regs + 0x06)
47#define NFC_V1_V2_FLASH_CMD (host->regs + 0x08)
48#define NFC_V1_V2_CONFIG (host->regs + 0x0a)
49#define NFC_V1_V2_ECC_STATUS_RESULT (host->regs + 0x0c)
50#define NFC_V1_V2_RSLTMAIN_AREA (host->regs + 0x0e)
51#define NFC_V1_V2_RSLTSPARE_AREA (host->regs + 0x10)
52#define NFC_V1_V2_WRPROT (host->regs + 0x12)
53#define NFC_V1_UNLOCKSTART_BLKADDR (host->regs + 0x14)
54#define NFC_V1_UNLOCKEND_BLKADDR (host->regs + 0x16)
Baruch Siachd178e3e2011-03-14 09:01:56 +020055#define NFC_V21_UNLOCKSTART_BLKADDR0 (host->regs + 0x20)
56#define NFC_V21_UNLOCKSTART_BLKADDR1 (host->regs + 0x24)
57#define NFC_V21_UNLOCKSTART_BLKADDR2 (host->regs + 0x28)
58#define NFC_V21_UNLOCKSTART_BLKADDR3 (host->regs + 0x2c)
59#define NFC_V21_UNLOCKEND_BLKADDR0 (host->regs + 0x22)
60#define NFC_V21_UNLOCKEND_BLKADDR1 (host->regs + 0x26)
61#define NFC_V21_UNLOCKEND_BLKADDR2 (host->regs + 0x2a)
62#define NFC_V21_UNLOCKEND_BLKADDR3 (host->regs + 0x2e)
Sascha Hauer1bc99182010-08-06 15:53:08 +020063#define NFC_V1_V2_NF_WRPRST (host->regs + 0x18)
64#define NFC_V1_V2_CONFIG1 (host->regs + 0x1a)
65#define NFC_V1_V2_CONFIG2 (host->regs + 0x1c)
Sascha Hauer34f6e152008-09-02 17:16:59 +020066
Sascha Hauer6e85dfd2010-08-06 15:53:10 +020067#define NFC_V2_CONFIG1_ECC_MODE_4 (1 << 0)
Sascha Hauer1bc99182010-08-06 15:53:08 +020068#define NFC_V1_V2_CONFIG1_SP_EN (1 << 2)
69#define NFC_V1_V2_CONFIG1_ECC_EN (1 << 3)
70#define NFC_V1_V2_CONFIG1_INT_MSK (1 << 4)
71#define NFC_V1_V2_CONFIG1_BIG (1 << 5)
72#define NFC_V1_V2_CONFIG1_RST (1 << 6)
73#define NFC_V1_V2_CONFIG1_CE (1 << 7)
Sascha Hauerb8db2f52010-08-09 15:04:19 +020074#define NFC_V2_CONFIG1_ONE_CYCLE (1 << 8)
75#define NFC_V2_CONFIG1_PPB(x) (((x) & 0x3) << 9)
76#define NFC_V2_CONFIG1_FP_INT (1 << 11)
Sascha Hauer34f6e152008-09-02 17:16:59 +020077
Sascha Hauer1bc99182010-08-06 15:53:08 +020078#define NFC_V1_V2_CONFIG2_INT (1 << 15)
Sascha Hauer34f6e152008-09-02 17:16:59 +020079
Sascha Hauer1bc99182010-08-06 15:53:08 +020080/*
81 * Operation modes for the NFC. Valid for v1, v2 and v3
82 * type controllers.
83 */
84#define NFC_CMD (1 << 0)
85#define NFC_ADDR (1 << 1)
86#define NFC_INPUT (1 << 2)
87#define NFC_OUTPUT (1 << 3)
88#define NFC_ID (1 << 4)
89#define NFC_STATUS (1 << 5)
Sascha Hauer34f6e152008-09-02 17:16:59 +020090
Sascha Hauer71ec5152010-08-06 15:53:11 +020091#define NFC_V3_FLASH_CMD (host->regs_axi + 0x00)
92#define NFC_V3_FLASH_ADDR0 (host->regs_axi + 0x04)
Sascha Hauer34f6e152008-09-02 17:16:59 +020093
Sascha Hauer71ec5152010-08-06 15:53:11 +020094#define NFC_V3_CONFIG1 (host->regs_axi + 0x34)
95#define NFC_V3_CONFIG1_SP_EN (1 << 0)
96#define NFC_V3_CONFIG1_RBA(x) (((x) & 0x7 ) << 4)
Sascha Hauer34f6e152008-09-02 17:16:59 +020097
Sascha Hauer71ec5152010-08-06 15:53:11 +020098#define NFC_V3_ECC_STATUS_RESULT (host->regs_axi + 0x38)
Sascha Hauer34f6e152008-09-02 17:16:59 +020099
Sascha Hauer71ec5152010-08-06 15:53:11 +0200100#define NFC_V3_LAUNCH (host->regs_axi + 0x40)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200101
Sascha Hauer71ec5152010-08-06 15:53:11 +0200102#define NFC_V3_WRPROT (host->regs_ip + 0x0)
103#define NFC_V3_WRPROT_LOCK_TIGHT (1 << 0)
104#define NFC_V3_WRPROT_LOCK (1 << 1)
105#define NFC_V3_WRPROT_UNLOCK (1 << 2)
106#define NFC_V3_WRPROT_BLS_UNLOCK (2 << 6)
107
108#define NFC_V3_WRPROT_UNLOCK_BLK_ADD0 (host->regs_ip + 0x04)
109
110#define NFC_V3_CONFIG2 (host->regs_ip + 0x24)
111#define NFC_V3_CONFIG2_PS_512 (0 << 0)
112#define NFC_V3_CONFIG2_PS_2048 (1 << 0)
113#define NFC_V3_CONFIG2_PS_4096 (2 << 0)
114#define NFC_V3_CONFIG2_ONE_CYCLE (1 << 2)
115#define NFC_V3_CONFIG2_ECC_EN (1 << 3)
116#define NFC_V3_CONFIG2_2CMD_PHASES (1 << 4)
117#define NFC_V3_CONFIG2_NUM_ADDR_PHASE0 (1 << 5)
118#define NFC_V3_CONFIG2_ECC_MODE_8 (1 << 6)
Sascha Hauer71718a8e2012-06-06 12:33:15 +0200119#define NFC_V3_CONFIG2_PPB(x, shift) (((x) & 0x3) << shift)
Sascha Hauer71ec5152010-08-06 15:53:11 +0200120#define NFC_V3_CONFIG2_NUM_ADDR_PHASE1(x) (((x) & 0x3) << 12)
121#define NFC_V3_CONFIG2_INT_MSK (1 << 15)
122#define NFC_V3_CONFIG2_ST_CMD(x) (((x) & 0xff) << 24)
123#define NFC_V3_CONFIG2_SPAS(x) (((x) & 0xff) << 16)
124
125#define NFC_V3_CONFIG3 (host->regs_ip + 0x28)
126#define NFC_V3_CONFIG3_ADD_OP(x) (((x) & 0x3) << 0)
127#define NFC_V3_CONFIG3_FW8 (1 << 3)
128#define NFC_V3_CONFIG3_SBB(x) (((x) & 0x7) << 8)
129#define NFC_V3_CONFIG3_NUM_OF_DEVICES(x) (((x) & 0x7) << 12)
130#define NFC_V3_CONFIG3_RBB_MODE (1 << 15)
131#define NFC_V3_CONFIG3_NO_SDMA (1 << 20)
132
133#define NFC_V3_IPC (host->regs_ip + 0x2C)
134#define NFC_V3_IPC_CREQ (1 << 0)
135#define NFC_V3_IPC_INT (1 << 31)
136
137#define NFC_V3_DELAY_LINE (host->regs_ip + 0x34)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200138
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +0200139struct mxc_nand_host;
140
141struct mxc_nand_devtype_data {
142 void (*preset)(struct mtd_info *);
143 void (*send_cmd)(struct mxc_nand_host *, uint16_t, int);
144 void (*send_addr)(struct mxc_nand_host *, uint16_t, int);
145 void (*send_page)(struct mtd_info *, unsigned int);
146 void (*send_read_id)(struct mxc_nand_host *);
147 uint16_t (*get_dev_status)(struct mxc_nand_host *);
148 int (*check_int)(struct mxc_nand_host *);
149 void (*irq_control)(struct mxc_nand_host *, int);
Uwe Kleine-König6d38af22012-04-23 11:23:36 +0200150 u32 (*get_ecc_status)(struct mxc_nand_host *);
Boris Brezillona894cf6c2016-02-03 20:02:54 +0100151 const struct mtd_ooblayout_ops *ooblayout;
Uwe Kleine-König5e05a2d2012-04-23 11:23:38 +0200152 void (*select_chip)(struct mtd_info *mtd, int chip);
Uwe Kleine-König69d023b2012-04-23 11:23:39 +0200153 int (*correct_data)(struct mtd_info *mtd, u_char *dat,
154 u_char *read_ecc, u_char *calc_ecc);
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +0200155
156 /*
157 * On i.MX21 the CONFIG2:INT bit cannot be read if interrupts are masked
158 * (CONFIG1:INT_MSK is set). To handle this the driver uses
159 * enable_irq/disable_irq_nosync instead of CONFIG1:INT_MSK
160 */
161 int irqpending_quirk;
162 int needs_ip;
163
164 size_t regs_offset;
165 size_t spare0_offset;
166 size_t axi_offset;
167
168 int spare_len;
169 int eccbytes;
170 int eccsize;
Sascha Hauer71718a8e2012-06-06 12:33:15 +0200171 int ppb_shift;
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +0200172};
173
Sascha Hauer34f6e152008-09-02 17:16:59 +0200174struct mxc_nand_host {
Sascha Hauer34f6e152008-09-02 17:16:59 +0200175 struct nand_chip nand;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200176 struct device *dev;
177
Uwe Kleine-König4b6f05e2012-04-24 10:05:22 +0200178 void __iomem *spare0;
179 void __iomem *main_area0;
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200180
181 void __iomem *base;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200182 void __iomem *regs;
Sascha Hauer71ec5152010-08-06 15:53:11 +0200183 void __iomem *regs_axi;
184 void __iomem *regs_ip;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200185 int status_request;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200186 struct clk *clk;
187 int clk_act;
188 int irq;
Sascha Hauer94f77e52010-08-06 15:53:09 +0200189 int eccsize;
Baruch Siach7e7e4732015-05-13 11:17:37 +0300190 int used_oobsize;
Baruch Siachd178e3e2011-03-14 09:01:56 +0200191 int active_cs;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200192
Sascha Hauer63f14742010-10-18 10:16:26 +0200193 struct completion op_completion;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200194
195 uint8_t *data_buf;
196 unsigned int buf_start;
Sascha Hauer5f973042010-08-06 15:53:06 +0200197
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +0200198 const struct mxc_nand_devtype_data *devtype_data;
Uwe Kleine-König64363562012-04-23 11:23:41 +0200199 struct mxc_nand_platform_data pdata;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200200};
201
Jingoo Hanb2ac0372013-08-07 16:18:52 +0900202static const char * const part_probes[] = {
Lothar Waßmann740bb0c2012-12-06 08:42:28 +0100203 "cmdlinepart", "RedBoot", "ofpart", NULL };
Sascha Hauer34f6e152008-09-02 17:16:59 +0200204
Sascha Hauer096bcc22012-05-29 10:16:09 +0200205static void memcpy32_fromio(void *trg, const void __iomem *src, size_t size)
206{
207 int i;
208 u32 *t = trg;
209 const __iomem u32 *s = src;
210
211 for (i = 0; i < (size >> 2); i++)
212 *t++ = __raw_readl(s++);
213}
214
Baruch Siach0d17fc32015-05-13 11:17:38 +0300215static void memcpy16_fromio(void *trg, const void __iomem *src, size_t size)
216{
217 int i;
218 u16 *t = trg;
219 const __iomem u16 *s = src;
220
221 /* We assume that src (IO) is always 32bit aligned */
222 if (PTR_ALIGN(trg, 4) == trg && IS_ALIGNED(size, 4)) {
223 memcpy32_fromio(trg, src, size);
224 return;
225 }
226
227 for (i = 0; i < (size >> 1); i++)
228 *t++ = __raw_readw(s++);
229}
230
Koul, Vinod33a87a12014-10-20 21:36:13 +0530231static inline void memcpy32_toio(void __iomem *trg, const void *src, int size)
Sascha Hauer096bcc22012-05-29 10:16:09 +0200232{
Koul, Vinod33a87a12014-10-20 21:36:13 +0530233 /* __iowrite32_copy use 32bit size values so divide by 4 */
234 __iowrite32_copy(trg, src, size / 4);
Sascha Hauer096bcc22012-05-29 10:16:09 +0200235}
236
Baruch Siach0d17fc32015-05-13 11:17:38 +0300237static void memcpy16_toio(void __iomem *trg, const void *src, int size)
238{
239 int i;
240 __iomem u16 *t = trg;
241 const u16 *s = src;
242
243 /* We assume that trg (IO) is always 32bit aligned */
244 if (PTR_ALIGN(src, 4) == src && IS_ALIGNED(size, 4)) {
245 memcpy32_toio(trg, src, size);
246 return;
247 }
248
249 for (i = 0; i < (size >> 1); i++)
250 __raw_writew(*s++, t++);
251}
252
Sascha Hauer71ec5152010-08-06 15:53:11 +0200253static int check_int_v3(struct mxc_nand_host *host)
254{
255 uint32_t tmp;
256
257 tmp = readl(NFC_V3_IPC);
258 if (!(tmp & NFC_V3_IPC_INT))
259 return 0;
260
261 tmp &= ~NFC_V3_IPC_INT;
262 writel(tmp, NFC_V3_IPC);
263
264 return 1;
265}
266
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200267static int check_int_v1_v2(struct mxc_nand_host *host)
268{
269 uint32_t tmp;
270
Sascha Hauer1bc99182010-08-06 15:53:08 +0200271 tmp = readw(NFC_V1_V2_CONFIG2);
272 if (!(tmp & NFC_V1_V2_CONFIG2_INT))
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200273 return 0;
274
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +0200275 if (!host->devtype_data->irqpending_quirk)
Sascha Hauer63f14742010-10-18 10:16:26 +0200276 writew(tmp & ~NFC_V1_V2_CONFIG2_INT, NFC_V1_V2_CONFIG2);
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200277
278 return 1;
279}
280
Sascha Hauer63f14742010-10-18 10:16:26 +0200281static void irq_control_v1_v2(struct mxc_nand_host *host, int activate)
282{
283 uint16_t tmp;
284
285 tmp = readw(NFC_V1_V2_CONFIG1);
286
287 if (activate)
288 tmp &= ~NFC_V1_V2_CONFIG1_INT_MSK;
289 else
290 tmp |= NFC_V1_V2_CONFIG1_INT_MSK;
291
292 writew(tmp, NFC_V1_V2_CONFIG1);
293}
294
295static void irq_control_v3(struct mxc_nand_host *host, int activate)
296{
297 uint32_t tmp;
298
299 tmp = readl(NFC_V3_CONFIG2);
300
301 if (activate)
302 tmp &= ~NFC_V3_CONFIG2_INT_MSK;
303 else
304 tmp |= NFC_V3_CONFIG2_INT_MSK;
305
306 writel(tmp, NFC_V3_CONFIG2);
307}
308
Uwe Kleine-König85569582012-04-23 11:23:34 +0200309static void irq_control(struct mxc_nand_host *host, int activate)
310{
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +0200311 if (host->devtype_data->irqpending_quirk) {
Uwe Kleine-König85569582012-04-23 11:23:34 +0200312 if (activate)
313 enable_irq(host->irq);
314 else
315 disable_irq_nosync(host->irq);
316 } else {
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +0200317 host->devtype_data->irq_control(host, activate);
Uwe Kleine-König85569582012-04-23 11:23:34 +0200318 }
319}
320
Uwe Kleine-König6d38af22012-04-23 11:23:36 +0200321static u32 get_ecc_status_v1(struct mxc_nand_host *host)
322{
323 return readw(NFC_V1_V2_ECC_STATUS_RESULT);
324}
325
326static u32 get_ecc_status_v2(struct mxc_nand_host *host)
327{
328 return readl(NFC_V1_V2_ECC_STATUS_RESULT);
329}
330
331static u32 get_ecc_status_v3(struct mxc_nand_host *host)
332{
333 return readl(NFC_V3_ECC_STATUS_RESULT);
334}
335
Uwe Kleine-König85569582012-04-23 11:23:34 +0200336static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
337{
338 struct mxc_nand_host *host = dev_id;
339
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +0200340 if (!host->devtype_data->check_int(host))
Uwe Kleine-König85569582012-04-23 11:23:34 +0200341 return IRQ_NONE;
342
343 irq_control(host, 0);
344
345 complete(&host->op_completion);
346
347 return IRQ_HANDLED;
348}
349
Sascha Hauer34f6e152008-09-02 17:16:59 +0200350/* This function polls the NANDFC to wait for the basic operation to
351 * complete by checking the INT bit of config2 register.
352 */
Uwe Kleine-Könige35d1d82015-02-10 19:59:55 +0100353static int wait_op_done(struct mxc_nand_host *host, int useirq)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200354{
Uwe Kleine-Könige35d1d82015-02-10 19:59:55 +0100355 int ret = 0;
356
357 /*
358 * If operation is already complete, don't bother to setup an irq or a
359 * loop.
360 */
361 if (host->devtype_data->check_int(host))
362 return 0;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200363
364 if (useirq) {
Uwe Kleine-Könige35d1d82015-02-10 19:59:55 +0100365 unsigned long timeout;
366
367 reinit_completion(&host->op_completion);
368
369 irq_control(host, 1);
370
371 timeout = wait_for_completion_timeout(&host->op_completion, HZ);
372 if (!timeout && !host->devtype_data->check_int(host)) {
373 dev_dbg(host->dev, "timeout waiting for irq\n");
374 ret = -ETIMEDOUT;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200375 }
376 } else {
Uwe Kleine-Könige35d1d82015-02-10 19:59:55 +0100377 int max_retries = 8000;
378 int done;
379
380 do {
381 udelay(1);
382
383 done = host->devtype_data->check_int(host);
384 if (done)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200385 break;
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200386
Uwe Kleine-Könige35d1d82015-02-10 19:59:55 +0100387 } while (--max_retries);
388
389 if (!done) {
390 dev_dbg(host->dev, "timeout polling for completion\n");
391 ret = -ETIMEDOUT;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200392 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200393 }
Uwe Kleine-Könige35d1d82015-02-10 19:59:55 +0100394
395 WARN_ONCE(ret < 0, "timeout! useirq=%d\n", useirq);
396
397 return ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200398}
399
Sascha Hauer71ec5152010-08-06 15:53:11 +0200400static void send_cmd_v3(struct mxc_nand_host *host, uint16_t cmd, int useirq)
401{
402 /* fill command */
403 writel(cmd, NFC_V3_FLASH_CMD);
404
405 /* send out command */
406 writel(NFC_CMD, NFC_V3_LAUNCH);
407
408 /* Wait for operation to complete */
409 wait_op_done(host, useirq);
410}
411
Sascha Hauer34f6e152008-09-02 17:16:59 +0200412/* This function issues the specified command to the NAND device and
413 * waits for completion. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200414static void send_cmd_v1_v2(struct mxc_nand_host *host, uint16_t cmd, int useirq)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200415{
Brian Norris289c0522011-07-19 10:06:09 -0700416 pr_debug("send_cmd(host, 0x%x, %d)\n", cmd, useirq);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200417
Sascha Hauer1bc99182010-08-06 15:53:08 +0200418 writew(cmd, NFC_V1_V2_FLASH_CMD);
419 writew(NFC_CMD, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200420
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +0200421 if (host->devtype_data->irqpending_quirk && (cmd == NAND_CMD_RESET)) {
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200422 int max_retries = 100;
423 /* Reset completion is indicated by NFC_CONFIG2 */
424 /* being set to 0 */
425 while (max_retries-- > 0) {
Sascha Hauer1bc99182010-08-06 15:53:08 +0200426 if (readw(NFC_V1_V2_CONFIG2) == 0) {
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200427 break;
428 }
429 udelay(1);
430 }
431 if (max_retries < 0)
Brian Norris0a32a102011-07-19 10:06:10 -0700432 pr_debug("%s: RESET failed\n", __func__);
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200433 } else {
434 /* Wait for operation to complete */
435 wait_op_done(host, useirq);
436 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200437}
438
Sascha Hauer71ec5152010-08-06 15:53:11 +0200439static void send_addr_v3(struct mxc_nand_host *host, uint16_t addr, int islast)
440{
441 /* fill address */
442 writel(addr, NFC_V3_FLASH_ADDR0);
443
444 /* send out address */
445 writel(NFC_ADDR, NFC_V3_LAUNCH);
446
447 wait_op_done(host, 0);
448}
449
Sascha Hauer34f6e152008-09-02 17:16:59 +0200450/* This function sends an address (or partial address) to the
451 * NAND device. The address is used to select the source/destination for
452 * a NAND command. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200453static void send_addr_v1_v2(struct mxc_nand_host *host, uint16_t addr, int islast)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200454{
Brian Norris289c0522011-07-19 10:06:09 -0700455 pr_debug("send_addr(host, 0x%x %d)\n", addr, islast);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200456
Sascha Hauer1bc99182010-08-06 15:53:08 +0200457 writew(addr, NFC_V1_V2_FLASH_ADDR);
458 writew(NFC_ADDR, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200459
460 /* Wait for operation to complete */
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200461 wait_op_done(host, islast);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200462}
463
Sascha Hauer71ec5152010-08-06 15:53:11 +0200464static void send_page_v3(struct mtd_info *mtd, unsigned int ops)
465{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100466 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100467 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauer71ec5152010-08-06 15:53:11 +0200468 uint32_t tmp;
469
470 tmp = readl(NFC_V3_CONFIG1);
471 tmp &= ~(7 << 4);
472 writel(tmp, NFC_V3_CONFIG1);
473
474 /* transfer data from NFC ram to nand */
475 writel(ops, NFC_V3_LAUNCH);
476
477 wait_op_done(host, false);
478}
479
Uwe Kleine-König6d38af22012-04-23 11:23:36 +0200480static void send_page_v2(struct mtd_info *mtd, unsigned int ops)
481{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100482 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100483 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Uwe Kleine-König6d38af22012-04-23 11:23:36 +0200484
485 /* NANDFC buffer 0 is used for page read/write */
486 writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
487
488 writew(ops, NFC_V1_V2_CONFIG2);
489
490 /* Wait for operation to complete */
491 wait_op_done(host, true);
492}
493
494static void send_page_v1(struct mtd_info *mtd, unsigned int ops)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200495{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100496 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100497 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200498 int bufs, i;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200499
Uwe Kleine-König6d38af22012-04-23 11:23:36 +0200500 if (mtd->writesize > 512)
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200501 bufs = 4;
502 else
503 bufs = 1;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200504
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200505 for (i = 0; i < bufs; i++) {
506
507 /* NANDFC buffer 0 is used for page read/write */
Baruch Siachd178e3e2011-03-14 09:01:56 +0200508 writew((host->active_cs << 4) | i, NFC_V1_V2_BUF_ADDR);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200509
Sascha Hauer1bc99182010-08-06 15:53:08 +0200510 writew(ops, NFC_V1_V2_CONFIG2);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200511
512 /* Wait for operation to complete */
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200513 wait_op_done(host, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200514 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200515}
516
Sascha Hauer71ec5152010-08-06 15:53:11 +0200517static void send_read_id_v3(struct mxc_nand_host *host)
518{
519 /* Read ID into main buffer */
520 writel(NFC_ID, NFC_V3_LAUNCH);
521
522 wait_op_done(host, true);
523
Sascha Hauer096bcc22012-05-29 10:16:09 +0200524 memcpy32_fromio(host->data_buf, host->main_area0, 16);
Sascha Hauer71ec5152010-08-06 15:53:11 +0200525}
526
Sascha Hauer34f6e152008-09-02 17:16:59 +0200527/* Request the NANDFC to perform a read of the NAND device ID. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200528static void send_read_id_v1_v2(struct mxc_nand_host *host)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200529{
Sascha Hauer34f6e152008-09-02 17:16:59 +0200530 /* NANDFC buffer 0 is used for device ID output */
Baruch Siachd178e3e2011-03-14 09:01:56 +0200531 writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200532
Sascha Hauer1bc99182010-08-06 15:53:08 +0200533 writew(NFC_ID, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200534
535 /* Wait for operation to complete */
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200536 wait_op_done(host, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200537
Sascha Hauer096bcc22012-05-29 10:16:09 +0200538 memcpy32_fromio(host->data_buf, host->main_area0, 16);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200539}
540
Sascha Hauer71ec5152010-08-06 15:53:11 +0200541static uint16_t get_dev_status_v3(struct mxc_nand_host *host)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200542{
Sascha Hauer71ec5152010-08-06 15:53:11 +0200543 writew(NFC_STATUS, NFC_V3_LAUNCH);
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200544 wait_op_done(host, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200545
Sascha Hauer71ec5152010-08-06 15:53:11 +0200546 return readl(NFC_V3_CONFIG1) >> 16;
547}
548
Sascha Hauer34f6e152008-09-02 17:16:59 +0200549/* This function requests the NANDFC to perform a read of the
550 * NAND device status and returns the current status. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200551static uint16_t get_dev_status_v1_v2(struct mxc_nand_host *host)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200552{
Sascha Hauerc29c6072010-08-06 15:53:05 +0200553 void __iomem *main_buf = host->main_area0;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200554 uint32_t store;
555 uint16_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200556
Baruch Siachd178e3e2011-03-14 09:01:56 +0200557 writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
Sascha Hauerc29c6072010-08-06 15:53:05 +0200558
559 /*
560 * The device status is stored in main_area0. To
561 * prevent corruption of the buffer save the value
562 * and restore it afterwards.
563 */
Sascha Hauer34f6e152008-09-02 17:16:59 +0200564 store = readl(main_buf);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200565
Sascha Hauer1bc99182010-08-06 15:53:08 +0200566 writew(NFC_STATUS, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200567 wait_op_done(host, true);
568
Sascha Hauer34f6e152008-09-02 17:16:59 +0200569 ret = readw(main_buf);
Sascha Hauerc29c6072010-08-06 15:53:05 +0200570
Sascha Hauer34f6e152008-09-02 17:16:59 +0200571 writel(store, main_buf);
572
573 return ret;
574}
575
576/* This functions is used by upper layer to checks if device is ready */
577static int mxc_nand_dev_ready(struct mtd_info *mtd)
578{
579 /*
580 * NFC handles R/B internally. Therefore, this function
581 * always returns status as ready.
582 */
583 return 1;
584}
585
586static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
587{
588 /*
589 * If HW ECC is enabled, we turn it on during init. There is
590 * no need to enable again here.
591 */
592}
593
Sascha Hauer94f77e52010-08-06 15:53:09 +0200594static int mxc_nand_correct_data_v1(struct mtd_info *mtd, u_char *dat,
Sascha Hauer34f6e152008-09-02 17:16:59 +0200595 u_char *read_ecc, u_char *calc_ecc)
596{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100597 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100598 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200599
600 /*
601 * 1-Bit errors are automatically corrected in HW. No need for
602 * additional correction. 2-Bit errors cannot be corrected by
603 * HW ECC, so we need to return failure
604 */
Uwe Kleine-König6d38af22012-04-23 11:23:36 +0200605 uint16_t ecc_status = get_ecc_status_v1(host);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200606
607 if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
Brian Norris289c0522011-07-19 10:06:09 -0700608 pr_debug("MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
Boris BREZILLON6e941192015-12-30 20:32:03 +0100609 return -EBADMSG;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200610 }
611
612 return 0;
613}
614
Sascha Hauer94f77e52010-08-06 15:53:09 +0200615static int mxc_nand_correct_data_v2_v3(struct mtd_info *mtd, u_char *dat,
616 u_char *read_ecc, u_char *calc_ecc)
617{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100618 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100619 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauer94f77e52010-08-06 15:53:09 +0200620 u32 ecc_stat, err;
621 int no_subpages = 1;
622 int ret = 0;
623 u8 ecc_bit_mask, err_limit;
624
625 ecc_bit_mask = (host->eccsize == 4) ? 0x7 : 0xf;
626 err_limit = (host->eccsize == 4) ? 0x4 : 0x8;
627
628 no_subpages = mtd->writesize >> 9;
629
Uwe Kleine-König6d38af22012-04-23 11:23:36 +0200630 ecc_stat = host->devtype_data->get_ecc_status(host);
Sascha Hauer94f77e52010-08-06 15:53:09 +0200631
632 do {
633 err = ecc_stat & ecc_bit_mask;
634 if (err > err_limit) {
635 printk(KERN_WARNING "UnCorrectable RS-ECC Error\n");
Boris BREZILLON6e941192015-12-30 20:32:03 +0100636 return -EBADMSG;
Sascha Hauer94f77e52010-08-06 15:53:09 +0200637 } else {
638 ret += err;
639 }
640 ecc_stat >>= 4;
641 } while (--no_subpages);
642
Sascha Hauer94f77e52010-08-06 15:53:09 +0200643 pr_debug("%d Symbol Correctable RS-ECC Error\n", ret);
644
645 return ret;
646}
647
Sascha Hauer34f6e152008-09-02 17:16:59 +0200648static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
649 u_char *ecc_code)
650{
651 return 0;
652}
653
654static u_char mxc_nand_read_byte(struct mtd_info *mtd)
655{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100656 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100657 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauerf8f96082009-06-04 17:12:26 +0200658 uint8_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200659
660 /* Check for status request */
661 if (host->status_request)
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +0200662 return host->devtype_data->get_dev_status(host) & 0xFF;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200663
Uwe Kleine-König3f410692015-02-10 19:59:57 +0100664 if (nand_chip->options & NAND_BUSWIDTH_16) {
665 /* only take the lower byte of each word */
666 ret = *(uint16_t *)(host->data_buf + host->buf_start);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200667
Uwe Kleine-König3f410692015-02-10 19:59:57 +0100668 host->buf_start += 2;
669 } else {
670 ret = *(uint8_t *)(host->data_buf + host->buf_start);
671 host->buf_start++;
672 }
673
674 pr_debug("%s: ret=0x%hhx (start=%u)\n", __func__, ret, host->buf_start);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200675 return ret;
676}
677
678static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
679{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100680 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100681 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauerf8f96082009-06-04 17:12:26 +0200682 uint16_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200683
Sascha Hauerf8f96082009-06-04 17:12:26 +0200684 ret = *(uint16_t *)(host->data_buf + host->buf_start);
685 host->buf_start += 2;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200686
687 return ret;
688}
689
690/* Write data of length len to buffer buf. The data to be
691 * written on NAND Flash is first copied to RAMbuffer. After the Data Input
692 * Operation by the NFC, the data is written to NAND Flash */
693static void mxc_nand_write_buf(struct mtd_info *mtd,
694 const u_char *buf, int len)
695{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100696 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100697 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauerf8f96082009-06-04 17:12:26 +0200698 u16 col = host->buf_start;
699 int n = mtd->oobsize + mtd->writesize - col;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200700
Sascha Hauerf8f96082009-06-04 17:12:26 +0200701 n = min(n, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200702
Sascha Hauerf8f96082009-06-04 17:12:26 +0200703 memcpy(host->data_buf + col, buf, n);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200704
Sascha Hauerf8f96082009-06-04 17:12:26 +0200705 host->buf_start += n;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200706}
707
708/* Read the data buffer from the NAND Flash. To read the data from NAND
709 * Flash first the data output cycle is initiated by the NFC, which copies
710 * the data to RAMbuffer. This data of length len is then copied to buffer buf.
711 */
712static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
713{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100714 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100715 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauerf8f96082009-06-04 17:12:26 +0200716 u16 col = host->buf_start;
717 int n = mtd->oobsize + mtd->writesize - col;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200718
Sascha Hauerf8f96082009-06-04 17:12:26 +0200719 n = min(n, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200720
Baruch Siach5d9d9932011-03-02 16:47:55 +0200721 memcpy(buf, host->data_buf + col, n);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200722
Baruch Siach5d9d9932011-03-02 16:47:55 +0200723 host->buf_start += n;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200724}
725
Sascha Hauer34f6e152008-09-02 17:16:59 +0200726/* This function is used by upper layer for select and
727 * deselect of the NAND chip */
Uwe Kleine-König5e05a2d2012-04-23 11:23:38 +0200728static void mxc_nand_select_chip_v1_v3(struct mtd_info *mtd, int chip)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200729{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100730 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100731 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200732
Baruch Siachd178e3e2011-03-14 09:01:56 +0200733 if (chip == -1) {
Sascha Hauer34f6e152008-09-02 17:16:59 +0200734 /* Disable the NFC clock */
735 if (host->clk_act) {
Sascha Hauer97c32132012-03-07 20:56:35 +0100736 clk_disable_unprepare(host->clk);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200737 host->clk_act = 0;
738 }
Baruch Siachd178e3e2011-03-14 09:01:56 +0200739 return;
740 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200741
Baruch Siachd178e3e2011-03-14 09:01:56 +0200742 if (!host->clk_act) {
743 /* Enable the NFC clock */
Sascha Hauer97c32132012-03-07 20:56:35 +0100744 clk_prepare_enable(host->clk);
Baruch Siachd178e3e2011-03-14 09:01:56 +0200745 host->clk_act = 1;
746 }
Uwe Kleine-König5e05a2d2012-04-23 11:23:38 +0200747}
Baruch Siachd178e3e2011-03-14 09:01:56 +0200748
Uwe Kleine-König5e05a2d2012-04-23 11:23:38 +0200749static void mxc_nand_select_chip_v2(struct mtd_info *mtd, int chip)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200750{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100751 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100752 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200753
754 if (chip == -1) {
755 /* Disable the NFC clock */
756 if (host->clk_act) {
Fabio Estevam3d059692012-05-25 20:14:50 -0300757 clk_disable_unprepare(host->clk);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200758 host->clk_act = 0;
759 }
760 return;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200761 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200762
763 if (!host->clk_act) {
764 /* Enable the NFC clock */
Fabio Estevam3d059692012-05-25 20:14:50 -0300765 clk_prepare_enable(host->clk);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200766 host->clk_act = 1;
767 }
768
Uwe Kleine-König5e05a2d2012-04-23 11:23:38 +0200769 host->active_cs = chip;
770 writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200771}
772
Sascha Hauerf8f96082009-06-04 17:12:26 +0200773/*
Uwe Kleine-König35d5d202015-05-13 11:17:36 +0300774 * The controller splits a page into data chunks of 512 bytes + partial oob.
775 * There are writesize / 512 such chunks, the size of the partial oob parts is
776 * oobsize / #chunks rounded down to a multiple of 2. The last oob chunk then
777 * contains additionally the byte lost by rounding (if any).
778 * This function handles the needed shuffling between host->data_buf (which
779 * holds a page in natural order, i.e. writesize bytes data + oobsize bytes
780 * spare) and the NFC buffer.
Sascha Hauerf8f96082009-06-04 17:12:26 +0200781 */
782static void copy_spare(struct mtd_info *mtd, bool bfrom)
783{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100784 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100785 struct mxc_nand_host *host = nand_get_controller_data(this);
Uwe Kleine-König35d5d202015-05-13 11:17:36 +0300786 u16 i, oob_chunk_size;
787 u16 num_chunks = mtd->writesize / 512;
788
Sascha Hauerf8f96082009-06-04 17:12:26 +0200789 u8 *d = host->data_buf + mtd->writesize;
Uwe Kleine-König4b6f05e2012-04-24 10:05:22 +0200790 u8 __iomem *s = host->spare0;
Uwe Kleine-König35d5d202015-05-13 11:17:36 +0300791 u16 sparebuf_size = host->devtype_data->spare_len;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200792
Uwe Kleine-König35d5d202015-05-13 11:17:36 +0300793 /* size of oob chunk for all but possibly the last one */
Baruch Siach7e7e4732015-05-13 11:17:37 +0300794 oob_chunk_size = (host->used_oobsize / num_chunks) & ~1;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200795
796 if (bfrom) {
Uwe Kleine-König35d5d202015-05-13 11:17:36 +0300797 for (i = 0; i < num_chunks - 1; i++)
Baruch Siach0d17fc32015-05-13 11:17:38 +0300798 memcpy16_fromio(d + i * oob_chunk_size,
Uwe Kleine-König35d5d202015-05-13 11:17:36 +0300799 s + i * sparebuf_size,
800 oob_chunk_size);
Sascha Hauerf8f96082009-06-04 17:12:26 +0200801
Uwe Kleine-König35d5d202015-05-13 11:17:36 +0300802 /* the last chunk */
Baruch Siach0d17fc32015-05-13 11:17:38 +0300803 memcpy16_fromio(d + i * oob_chunk_size,
Uwe Kleine-König35d5d202015-05-13 11:17:36 +0300804 s + i * sparebuf_size,
Baruch Siach7e7e4732015-05-13 11:17:37 +0300805 host->used_oobsize - i * oob_chunk_size);
Sascha Hauerf8f96082009-06-04 17:12:26 +0200806 } else {
Uwe Kleine-König35d5d202015-05-13 11:17:36 +0300807 for (i = 0; i < num_chunks - 1; i++)
Baruch Siach0d17fc32015-05-13 11:17:38 +0300808 memcpy16_toio(&s[i * sparebuf_size],
Uwe Kleine-König35d5d202015-05-13 11:17:36 +0300809 &d[i * oob_chunk_size],
810 oob_chunk_size);
Sascha Hauerf8f96082009-06-04 17:12:26 +0200811
Uwe Kleine-König35d5d202015-05-13 11:17:36 +0300812 /* the last chunk */
Eric Benarde5a5d922015-09-23 17:07:28 +0200813 memcpy16_toio(&s[i * sparebuf_size],
Uwe Kleine-König35d5d202015-05-13 11:17:36 +0300814 &d[i * oob_chunk_size],
Baruch Siach7e7e4732015-05-13 11:17:37 +0300815 host->used_oobsize - i * oob_chunk_size);
Sascha Hauerf8f96082009-06-04 17:12:26 +0200816 }
817}
818
Uwe Kleine-Königc4ca3992015-02-10 19:59:58 +0100819/*
820 * MXC NANDFC can only perform full page+spare or spare-only read/write. When
821 * the upper layers perform a read/write buf operation, the saved column address
822 * is used to index into the full page. So usually this function is called with
823 * column == 0 (unless no column cycle is needed indicated by column == -1)
824 */
Sascha Hauera3e65b62009-06-02 11:47:59 +0200825static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200826{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100827 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100828 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200829
830 /* Write out column address, if necessary */
831 if (column != -1) {
Uwe Kleine-Königc4ca3992015-02-10 19:59:58 +0100832 host->devtype_data->send_addr(host, column & 0xff,
833 page_addr == -1);
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200834 if (mtd->writesize > 512)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200835 /* another col addr cycle for 2k page */
Uwe Kleine-Königc4ca3992015-02-10 19:59:58 +0100836 host->devtype_data->send_addr(host,
837 (column >> 8) & 0xff,
838 false);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200839 }
840
841 /* Write out page address, if necessary */
842 if (page_addr != -1) {
843 /* paddr_0 - p_addr_7 */
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +0200844 host->devtype_data->send_addr(host, (page_addr & 0xff), false);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200845
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200846 if (mtd->writesize > 512) {
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400847 if (mtd->size >= 0x10000000) {
848 /* paddr_8 - paddr_15 */
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +0200849 host->devtype_data->send_addr(host,
850 (page_addr >> 8) & 0xff,
851 false);
852 host->devtype_data->send_addr(host,
853 (page_addr >> 16) & 0xff,
854 true);
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400855 } else
856 /* paddr_8 - paddr_15 */
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +0200857 host->devtype_data->send_addr(host,
858 (page_addr >> 8) & 0xff, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200859 } else {
860 /* One more address cycle for higher density devices */
861 if (mtd->size >= 0x4000000) {
862 /* paddr_8 - paddr_15 */
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +0200863 host->devtype_data->send_addr(host,
864 (page_addr >> 8) & 0xff,
865 false);
866 host->devtype_data->send_addr(host,
867 (page_addr >> 16) & 0xff,
868 true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200869 } else
870 /* paddr_8 - paddr_15 */
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +0200871 host->devtype_data->send_addr(host,
872 (page_addr >> 8) & 0xff, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200873 }
874 }
Sascha Hauera3e65b62009-06-02 11:47:59 +0200875}
Sascha Hauer34f6e152008-09-02 17:16:59 +0200876
Boris Brezillona894cf6c2016-02-03 20:02:54 +0100877static int mxc_v1_ooblayout_ecc(struct mtd_info *mtd, int section,
878 struct mtd_oob_region *oobregion)
879{
880 struct nand_chip *nand_chip = mtd_to_nand(mtd);
881
882 if (section >= nand_chip->ecc.steps)
883 return -ERANGE;
884
885 oobregion->offset = (section * 16) + 6;
886 oobregion->length = nand_chip->ecc.bytes;
887
888 return 0;
889}
890
891static int mxc_v1_ooblayout_free(struct mtd_info *mtd, int section,
892 struct mtd_oob_region *oobregion)
893{
894 struct nand_chip *nand_chip = mtd_to_nand(mtd);
895
896 if (section > nand_chip->ecc.steps)
897 return -ERANGE;
898
899 if (!section) {
900 if (mtd->writesize <= 512) {
901 oobregion->offset = 0;
902 oobregion->length = 5;
903 } else {
904 oobregion->offset = 2;
905 oobregion->length = 4;
906 }
907 } else {
908 oobregion->offset = ((section - 1) * 16) +
909 nand_chip->ecc.bytes + 6;
910 if (section < nand_chip->ecc.steps)
911 oobregion->length = (section * 16) + 6 -
912 oobregion->offset;
913 else
914 oobregion->length = mtd->oobsize - oobregion->offset;
915 }
916
917 return 0;
918}
919
920static const struct mtd_ooblayout_ops mxc_v1_ooblayout_ops = {
921 .ecc = mxc_v1_ooblayout_ecc,
922 .free = mxc_v1_ooblayout_free,
923};
924
925static int mxc_v2_ooblayout_ecc(struct mtd_info *mtd, int section,
926 struct mtd_oob_region *oobregion)
927{
928 struct nand_chip *nand_chip = mtd_to_nand(mtd);
929 int stepsize = nand_chip->ecc.bytes == 9 ? 16 : 26;
930
931 if (section >= nand_chip->ecc.steps)
932 return -ERANGE;
933
934 oobregion->offset = (section * stepsize) + 7;
935 oobregion->length = nand_chip->ecc.bytes;
936
937 return 0;
938}
939
940static int mxc_v2_ooblayout_free(struct mtd_info *mtd, int section,
941 struct mtd_oob_region *oobregion)
942{
943 struct nand_chip *nand_chip = mtd_to_nand(mtd);
944 int stepsize = nand_chip->ecc.bytes == 9 ? 16 : 26;
945
946 if (section > nand_chip->ecc.steps)
947 return -ERANGE;
948
949 if (!section) {
950 if (mtd->writesize <= 512) {
951 oobregion->offset = 0;
952 oobregion->length = 5;
953 } else {
954 oobregion->offset = 2;
955 oobregion->length = 4;
956 }
957 } else {
958 oobregion->offset = section * stepsize;
959 oobregion->length = 7;
960 }
961
962 return 0;
963}
964
965static const struct mtd_ooblayout_ops mxc_v2_ooblayout_ops = {
966 .ecc = mxc_v2_ooblayout_ecc,
967 .free = mxc_v2_ooblayout_free,
968};
969
Sascha Hauer6e85dfd2010-08-06 15:53:10 +0200970/*
971 * v2 and v3 type controllers can do 4bit or 8bit ecc depending
972 * on how much oob the nand chip has. For 8bit ecc we need at least
973 * 26 bytes of oob data per 512 byte block.
974 */
975static int get_eccsize(struct mtd_info *mtd)
976{
977 int oobbytes_per_512 = 0;
978
979 oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize;
980
981 if (oobbytes_per_512 < 26)
982 return 4;
983 else
984 return 8;
985}
986
Uwe Kleine-König6d38af22012-04-23 11:23:36 +0200987static void preset_v1(struct mtd_info *mtd)
Ivo Claryssed4840182010-04-08 16:14:44 +0200988{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100989 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100990 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauerb8db2f52010-08-09 15:04:19 +0200991 uint16_t config1 = 0;
Ivo Claryssed4840182010-04-08 16:14:44 +0200992
Uwe Kleine-König1f42adc2015-02-10 19:59:56 +0100993 if (nand_chip->ecc.mode == NAND_ECC_HW && mtd->writesize)
Sascha Hauerb8db2f52010-08-09 15:04:19 +0200994 config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
995
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +0200996 if (!host->devtype_data->irqpending_quirk)
Sascha Hauerb8db2f52010-08-09 15:04:19 +0200997 config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
Sascha Hauer6e85dfd2010-08-06 15:53:10 +0200998
Uwe Kleine-König6d38af22012-04-23 11:23:36 +0200999 host->eccsize = 1;
1000
1001 writew(config1, NFC_V1_V2_CONFIG1);
1002 /* preset operation */
1003
1004 /* Unlock the internal RAM Buffer */
1005 writew(0x2, NFC_V1_V2_CONFIG);
1006
1007 /* Blocks to be unlocked */
1008 writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR);
1009 writew(0xffff, NFC_V1_UNLOCKEND_BLKADDR);
1010
1011 /* Unlock Block Command for given address range */
1012 writew(0x4, NFC_V1_V2_WRPROT);
1013}
1014
1015static void preset_v2(struct mtd_info *mtd)
1016{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001017 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001018 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001019 uint16_t config1 = 0;
1020
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001021 config1 |= NFC_V2_CONFIG1_FP_INT;
Ivo Claryssed4840182010-04-08 16:14:44 +02001022
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001023 if (!host->devtype_data->irqpending_quirk)
Ivo Claryssed4840182010-04-08 16:14:44 +02001024 config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
Sascha Hauer6e85dfd2010-08-06 15:53:10 +02001025
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001026 if (mtd->writesize) {
Sascha Hauerb8db2f52010-08-09 15:04:19 +02001027 uint16_t pages_per_block = mtd->erasesize / mtd->writesize;
1028
Uwe Kleine-König1f42adc2015-02-10 19:59:56 +01001029 if (nand_chip->ecc.mode == NAND_ECC_HW)
1030 config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
1031
Sascha Hauer6e85dfd2010-08-06 15:53:10 +02001032 host->eccsize = get_eccsize(mtd);
1033 if (host->eccsize == 4)
Sascha Hauerb8db2f52010-08-09 15:04:19 +02001034 config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
1035
1036 config1 |= NFC_V2_CONFIG1_PPB(ffs(pages_per_block) - 6);
Sascha Hauer6e85dfd2010-08-06 15:53:10 +02001037 } else {
1038 host->eccsize = 1;
1039 }
1040
Sascha Hauerb8db2f52010-08-09 15:04:19 +02001041 writew(config1, NFC_V1_V2_CONFIG1);
Ivo Claryssed4840182010-04-08 16:14:44 +02001042 /* preset operation */
1043
1044 /* Unlock the internal RAM Buffer */
Sascha Hauer1bc99182010-08-06 15:53:08 +02001045 writew(0x2, NFC_V1_V2_CONFIG);
Ivo Claryssed4840182010-04-08 16:14:44 +02001046
1047 /* Blocks to be unlocked */
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001048 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR0);
1049 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR1);
1050 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR2);
1051 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR3);
1052 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR0);
1053 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR1);
1054 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR2);
1055 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR3);
Ivo Claryssed4840182010-04-08 16:14:44 +02001056
1057 /* Unlock Block Command for given address range */
Sascha Hauer1bc99182010-08-06 15:53:08 +02001058 writew(0x4, NFC_V1_V2_WRPROT);
Ivo Claryssed4840182010-04-08 16:14:44 +02001059}
1060
Sascha Hauer71ec5152010-08-06 15:53:11 +02001061static void preset_v3(struct mtd_info *mtd)
1062{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001063 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001064 struct mxc_nand_host *host = nand_get_controller_data(chip);
Sascha Hauer71ec5152010-08-06 15:53:11 +02001065 uint32_t config2, config3;
1066 int i, addr_phases;
1067
1068 writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1);
1069 writel(NFC_V3_IPC_CREQ, NFC_V3_IPC);
1070
1071 /* Unlock the internal RAM Buffer */
1072 writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
1073 NFC_V3_WRPROT);
1074
1075 /* Blocks to be unlocked */
1076 for (i = 0; i < NAND_MAX_CHIPS; i++)
Fabio Estevam1b15b1f2015-11-17 13:58:50 -02001077 writel(0xffff << 16, NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2));
Sascha Hauer71ec5152010-08-06 15:53:11 +02001078
1079 writel(0, NFC_V3_IPC);
1080
1081 config2 = NFC_V3_CONFIG2_ONE_CYCLE |
1082 NFC_V3_CONFIG2_2CMD_PHASES |
1083 NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) |
1084 NFC_V3_CONFIG2_ST_CMD(0x70) |
Sascha Hauer63f14742010-10-18 10:16:26 +02001085 NFC_V3_CONFIG2_INT_MSK |
Sascha Hauer71ec5152010-08-06 15:53:11 +02001086 NFC_V3_CONFIG2_NUM_ADDR_PHASE0;
1087
Sascha Hauer71ec5152010-08-06 15:53:11 +02001088 addr_phases = fls(chip->pagemask) >> 3;
1089
1090 if (mtd->writesize == 2048) {
1091 config2 |= NFC_V3_CONFIG2_PS_2048;
1092 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
1093 } else if (mtd->writesize == 4096) {
1094 config2 |= NFC_V3_CONFIG2_PS_4096;
1095 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
1096 } else {
1097 config2 |= NFC_V3_CONFIG2_PS_512;
1098 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1);
1099 }
1100
1101 if (mtd->writesize) {
Uwe Kleine-König1f42adc2015-02-10 19:59:56 +01001102 if (chip->ecc.mode == NAND_ECC_HW)
1103 config2 |= NFC_V3_CONFIG2_ECC_EN;
1104
Sascha Hauer71718a8e2012-06-06 12:33:15 +02001105 config2 |= NFC_V3_CONFIG2_PPB(
1106 ffs(mtd->erasesize / mtd->writesize) - 6,
1107 host->devtype_data->ppb_shift);
Sascha Hauer71ec5152010-08-06 15:53:11 +02001108 host->eccsize = get_eccsize(mtd);
1109 if (host->eccsize == 8)
1110 config2 |= NFC_V3_CONFIG2_ECC_MODE_8;
1111 }
1112
1113 writel(config2, NFC_V3_CONFIG2);
1114
1115 config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
1116 NFC_V3_CONFIG3_NO_SDMA |
1117 NFC_V3_CONFIG3_RBB_MODE |
1118 NFC_V3_CONFIG3_SBB(6) | /* Reset default */
1119 NFC_V3_CONFIG3_ADD_OP(0);
1120
1121 if (!(chip->options & NAND_BUSWIDTH_16))
1122 config3 |= NFC_V3_CONFIG3_FW8;
1123
1124 writel(config3, NFC_V3_CONFIG3);
1125
1126 writel(0, NFC_V3_DELAY_LINE);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001127}
1128
Sascha Hauer34f6e152008-09-02 17:16:59 +02001129/* Used by the upper layer to write command to NAND Flash for
1130 * different operations to be carried out on NAND Flash */
1131static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
1132 int column, int page_addr)
1133{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001134 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001135 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001136
Brian Norris289c0522011-07-19 10:06:09 -07001137 pr_debug("mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
Sascha Hauer34f6e152008-09-02 17:16:59 +02001138 command, column, page_addr);
1139
1140 /* Reset command state information */
1141 host->status_request = false;
1142
1143 /* Command pre-processing step */
Sascha Hauer34f6e152008-09-02 17:16:59 +02001144 switch (command) {
Ivo Claryssed4840182010-04-08 16:14:44 +02001145 case NAND_CMD_RESET:
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001146 host->devtype_data->preset(mtd);
1147 host->devtype_data->send_cmd(host, command, false);
Ivo Claryssed4840182010-04-08 16:14:44 +02001148 break;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001149
Sascha Hauer34f6e152008-09-02 17:16:59 +02001150 case NAND_CMD_STATUS:
Sascha Hauerf8f96082009-06-04 17:12:26 +02001151 host->buf_start = 0;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001152 host->status_request = true;
Sascha Hauer89121a62009-06-04 17:18:01 +02001153
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001154 host->devtype_data->send_cmd(host, command, true);
Uwe Kleine-Königc4ca3992015-02-10 19:59:58 +01001155 WARN_ONCE(column != -1 || page_addr != -1,
1156 "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n",
1157 command, column, page_addr);
Sascha Hauer89121a62009-06-04 17:18:01 +02001158 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001159 break;
1160
Sascha Hauer34f6e152008-09-02 17:16:59 +02001161 case NAND_CMD_READ0:
Sascha Hauer34f6e152008-09-02 17:16:59 +02001162 case NAND_CMD_READOOB:
Sascha Hauer89121a62009-06-04 17:18:01 +02001163 if (command == NAND_CMD_READ0)
1164 host->buf_start = column;
1165 else
1166 host->buf_start = column + mtd->writesize;
Sascha Hauerf8f96082009-06-04 17:12:26 +02001167
Sascha Hauer5ea32022010-04-27 15:24:01 +02001168 command = NAND_CMD_READ0; /* only READ0 is valid */
Sascha Hauer89121a62009-06-04 17:18:01 +02001169
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001170 host->devtype_data->send_cmd(host, command, false);
Uwe Kleine-Königc4ca3992015-02-10 19:59:58 +01001171 WARN_ONCE(column < 0,
1172 "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n",
1173 command, column, page_addr);
1174 mxc_do_addr_cycle(mtd, 0, page_addr);
Sascha Hauer89121a62009-06-04 17:18:01 +02001175
Sascha Hauer2d69c7f2009-10-05 11:24:02 +02001176 if (mtd->writesize > 512)
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001177 host->devtype_data->send_cmd(host,
1178 NAND_CMD_READSTART, true);
Sascha Hauerc5d23f12009-06-04 17:25:53 +02001179
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001180 host->devtype_data->send_page(mtd, NFC_OUTPUT);
Sascha Hauer89121a62009-06-04 17:18:01 +02001181
Sascha Hauer096bcc22012-05-29 10:16:09 +02001182 memcpy32_fromio(host->data_buf, host->main_area0,
1183 mtd->writesize);
Sascha Hauer89121a62009-06-04 17:18:01 +02001184 copy_spare(mtd, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001185 break;
1186
Sascha Hauer34f6e152008-09-02 17:16:59 +02001187 case NAND_CMD_SEQIN:
Sascha Hauer5ea32022010-04-27 15:24:01 +02001188 if (column >= mtd->writesize)
1189 /* call ourself to read a page */
1190 mxc_nand_command(mtd, NAND_CMD_READ0, 0, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001191
Sascha Hauer5ea32022010-04-27 15:24:01 +02001192 host->buf_start = column;
Sascha Hauer89121a62009-06-04 17:18:01 +02001193
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001194 host->devtype_data->send_cmd(host, command, false);
Uwe Kleine-Königc4ca3992015-02-10 19:59:58 +01001195 WARN_ONCE(column < -1,
1196 "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n",
1197 command, column, page_addr);
1198 mxc_do_addr_cycle(mtd, 0, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001199 break;
1200
1201 case NAND_CMD_PAGEPROG:
Sascha Hauer096bcc22012-05-29 10:16:09 +02001202 memcpy32_toio(host->main_area0, host->data_buf, mtd->writesize);
Sascha Hauerf8f96082009-06-04 17:12:26 +02001203 copy_spare(mtd, false);
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001204 host->devtype_data->send_page(mtd, NFC_INPUT);
1205 host->devtype_data->send_cmd(host, command, true);
Uwe Kleine-Königc4ca3992015-02-10 19:59:58 +01001206 WARN_ONCE(column != -1 || page_addr != -1,
1207 "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n",
1208 command, column, page_addr);
Sascha Hauer89121a62009-06-04 17:18:01 +02001209 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001210 break;
1211
Sascha Hauer34f6e152008-09-02 17:16:59 +02001212 case NAND_CMD_READID:
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001213 host->devtype_data->send_cmd(host, command, true);
Sascha Hauer89121a62009-06-04 17:18:01 +02001214 mxc_do_addr_cycle(mtd, column, page_addr);
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001215 host->devtype_data->send_read_id(host);
Uwe Kleine-Königc4ca3992015-02-10 19:59:58 +01001216 host->buf_start = 0;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001217 break;
1218
Sascha Hauer89121a62009-06-04 17:18:01 +02001219 case NAND_CMD_ERASE1:
Sascha Hauer34f6e152008-09-02 17:16:59 +02001220 case NAND_CMD_ERASE2:
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001221 host->devtype_data->send_cmd(host, command, false);
Uwe Kleine-Königc4ca3992015-02-10 19:59:58 +01001222 WARN_ONCE(column != -1,
1223 "Unexpected column value (cmd=%u, col=%d)\n",
1224 command, column);
Sascha Hauer89121a62009-06-04 17:18:01 +02001225 mxc_do_addr_cycle(mtd, column, page_addr);
1226
Sascha Hauer34f6e152008-09-02 17:16:59 +02001227 break;
Uwe Kleine-König3d6e81c2015-02-10 19:59:59 +01001228 case NAND_CMD_PARAM:
1229 host->devtype_data->send_cmd(host, command, false);
1230 mxc_do_addr_cycle(mtd, column, page_addr);
1231 host->devtype_data->send_page(mtd, NFC_OUTPUT);
1232 memcpy32_fromio(host->data_buf, host->main_area0, 512);
1233 host->buf_start = 0;
1234 break;
Uwe Kleine-König98ebb522015-02-10 20:00:00 +01001235 default:
1236 WARN_ONCE(1, "Unimplemented command (cmd=%u)\n",
1237 command);
1238 break;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001239 }
1240}
1241
Sascha Hauerf1372052009-10-21 14:25:27 +02001242/*
1243 * The generic flash bbt decriptors overlap with our ecc
1244 * hardware, so define some i.MX specific ones.
1245 */
1246static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
1247static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
1248
1249static struct nand_bbt_descr bbt_main_descr = {
1250 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1251 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1252 .offs = 0,
1253 .len = 4,
1254 .veroffs = 4,
1255 .maxblocks = 4,
1256 .pattern = bbt_pattern,
1257};
1258
1259static struct nand_bbt_descr bbt_mirror_descr = {
1260 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1261 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1262 .offs = 0,
1263 .len = 4,
1264 .veroffs = 4,
1265 .maxblocks = 4,
1266 .pattern = mirror_pattern,
1267};
1268
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001269/* v1 + irqpending_quirk: i.MX21 */
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001270static const struct mxc_nand_devtype_data imx21_nand_devtype_data = {
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001271 .preset = preset_v1,
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001272 .send_cmd = send_cmd_v1_v2,
1273 .send_addr = send_addr_v1_v2,
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001274 .send_page = send_page_v1,
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001275 .send_read_id = send_read_id_v1_v2,
1276 .get_dev_status = get_dev_status_v1_v2,
1277 .check_int = check_int_v1_v2,
1278 .irq_control = irq_control_v1_v2,
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001279 .get_ecc_status = get_ecc_status_v1,
Boris Brezillona894cf6c2016-02-03 20:02:54 +01001280 .ooblayout = &mxc_v1_ooblayout_ops,
Uwe Kleine-König5e05a2d2012-04-23 11:23:38 +02001281 .select_chip = mxc_nand_select_chip_v1_v3,
Uwe Kleine-König69d023b2012-04-23 11:23:39 +02001282 .correct_data = mxc_nand_correct_data_v1,
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001283 .irqpending_quirk = 1,
1284 .needs_ip = 0,
1285 .regs_offset = 0xe00,
1286 .spare0_offset = 0x800,
1287 .spare_len = 16,
1288 .eccbytes = 3,
1289 .eccsize = 1,
1290};
1291
1292/* v1 + !irqpending_quirk: i.MX27, i.MX31 */
1293static const struct mxc_nand_devtype_data imx27_nand_devtype_data = {
1294 .preset = preset_v1,
1295 .send_cmd = send_cmd_v1_v2,
1296 .send_addr = send_addr_v1_v2,
1297 .send_page = send_page_v1,
1298 .send_read_id = send_read_id_v1_v2,
1299 .get_dev_status = get_dev_status_v1_v2,
1300 .check_int = check_int_v1_v2,
1301 .irq_control = irq_control_v1_v2,
1302 .get_ecc_status = get_ecc_status_v1,
Boris Brezillona894cf6c2016-02-03 20:02:54 +01001303 .ooblayout = &mxc_v1_ooblayout_ops,
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001304 .select_chip = mxc_nand_select_chip_v1_v3,
1305 .correct_data = mxc_nand_correct_data_v1,
1306 .irqpending_quirk = 0,
1307 .needs_ip = 0,
1308 .regs_offset = 0xe00,
1309 .spare0_offset = 0x800,
1310 .axi_offset = 0,
1311 .spare_len = 16,
1312 .eccbytes = 3,
1313 .eccsize = 1,
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001314};
1315
1316/* v21: i.MX25, i.MX35 */
1317static const struct mxc_nand_devtype_data imx25_nand_devtype_data = {
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001318 .preset = preset_v2,
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001319 .send_cmd = send_cmd_v1_v2,
1320 .send_addr = send_addr_v1_v2,
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001321 .send_page = send_page_v2,
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001322 .send_read_id = send_read_id_v1_v2,
1323 .get_dev_status = get_dev_status_v1_v2,
1324 .check_int = check_int_v1_v2,
1325 .irq_control = irq_control_v1_v2,
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001326 .get_ecc_status = get_ecc_status_v2,
Boris Brezillona894cf6c2016-02-03 20:02:54 +01001327 .ooblayout = &mxc_v2_ooblayout_ops,
Uwe Kleine-König5e05a2d2012-04-23 11:23:38 +02001328 .select_chip = mxc_nand_select_chip_v2,
Uwe Kleine-König69d023b2012-04-23 11:23:39 +02001329 .correct_data = mxc_nand_correct_data_v2_v3,
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001330 .irqpending_quirk = 0,
1331 .needs_ip = 0,
1332 .regs_offset = 0x1e00,
1333 .spare0_offset = 0x1000,
1334 .axi_offset = 0,
1335 .spare_len = 64,
1336 .eccbytes = 9,
1337 .eccsize = 0,
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001338};
1339
Sascha Hauer71718a8e2012-06-06 12:33:15 +02001340/* v3.2a: i.MX51 */
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001341static const struct mxc_nand_devtype_data imx51_nand_devtype_data = {
1342 .preset = preset_v3,
1343 .send_cmd = send_cmd_v3,
1344 .send_addr = send_addr_v3,
1345 .send_page = send_page_v3,
1346 .send_read_id = send_read_id_v3,
1347 .get_dev_status = get_dev_status_v3,
1348 .check_int = check_int_v3,
1349 .irq_control = irq_control_v3,
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001350 .get_ecc_status = get_ecc_status_v3,
Boris Brezillona894cf6c2016-02-03 20:02:54 +01001351 .ooblayout = &mxc_v2_ooblayout_ops,
Uwe Kleine-König5e05a2d2012-04-23 11:23:38 +02001352 .select_chip = mxc_nand_select_chip_v1_v3,
Uwe Kleine-König69d023b2012-04-23 11:23:39 +02001353 .correct_data = mxc_nand_correct_data_v2_v3,
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001354 .irqpending_quirk = 0,
1355 .needs_ip = 1,
1356 .regs_offset = 0,
1357 .spare0_offset = 0x1000,
1358 .axi_offset = 0x1e00,
1359 .spare_len = 64,
1360 .eccbytes = 0,
1361 .eccsize = 0,
Sascha Hauer71718a8e2012-06-06 12:33:15 +02001362 .ppb_shift = 7,
1363};
1364
1365/* v3.2b: i.MX53 */
1366static const struct mxc_nand_devtype_data imx53_nand_devtype_data = {
1367 .preset = preset_v3,
1368 .send_cmd = send_cmd_v3,
1369 .send_addr = send_addr_v3,
1370 .send_page = send_page_v3,
1371 .send_read_id = send_read_id_v3,
1372 .get_dev_status = get_dev_status_v3,
1373 .check_int = check_int_v3,
1374 .irq_control = irq_control_v3,
1375 .get_ecc_status = get_ecc_status_v3,
Boris Brezillona894cf6c2016-02-03 20:02:54 +01001376 .ooblayout = &mxc_v2_ooblayout_ops,
Sascha Hauer71718a8e2012-06-06 12:33:15 +02001377 .select_chip = mxc_nand_select_chip_v1_v3,
1378 .correct_data = mxc_nand_correct_data_v2_v3,
1379 .irqpending_quirk = 0,
1380 .needs_ip = 1,
1381 .regs_offset = 0,
1382 .spare0_offset = 0x1000,
1383 .axi_offset = 0x1e00,
1384 .spare_len = 64,
1385 .eccbytes = 0,
1386 .eccsize = 0,
1387 .ppb_shift = 8,
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001388};
1389
Shawn Guo4d624352012-09-15 13:34:09 +08001390static inline int is_imx21_nfc(struct mxc_nand_host *host)
1391{
1392 return host->devtype_data == &imx21_nand_devtype_data;
1393}
1394
1395static inline int is_imx27_nfc(struct mxc_nand_host *host)
1396{
1397 return host->devtype_data == &imx27_nand_devtype_data;
1398}
1399
1400static inline int is_imx25_nfc(struct mxc_nand_host *host)
1401{
1402 return host->devtype_data == &imx25_nand_devtype_data;
1403}
1404
1405static inline int is_imx51_nfc(struct mxc_nand_host *host)
1406{
1407 return host->devtype_data == &imx51_nand_devtype_data;
1408}
1409
1410static inline int is_imx53_nfc(struct mxc_nand_host *host)
1411{
1412 return host->devtype_data == &imx53_nand_devtype_data;
1413}
1414
Krzysztof Kozlowski8d1e5682015-05-02 00:50:01 +09001415static const struct platform_device_id mxcnd_devtype[] = {
Shawn Guo4d624352012-09-15 13:34:09 +08001416 {
1417 .name = "imx21-nand",
1418 .driver_data = (kernel_ulong_t) &imx21_nand_devtype_data,
1419 }, {
1420 .name = "imx27-nand",
1421 .driver_data = (kernel_ulong_t) &imx27_nand_devtype_data,
1422 }, {
1423 .name = "imx25-nand",
1424 .driver_data = (kernel_ulong_t) &imx25_nand_devtype_data,
1425 }, {
1426 .name = "imx51-nand",
1427 .driver_data = (kernel_ulong_t) &imx51_nand_devtype_data,
1428 }, {
1429 .name = "imx53-nand",
1430 .driver_data = (kernel_ulong_t) &imx53_nand_devtype_data,
1431 }, {
1432 /* sentinel */
1433 }
1434};
1435MODULE_DEVICE_TABLE(platform, mxcnd_devtype);
1436
Uwe Kleine-König64363562012-04-23 11:23:41 +02001437#ifdef CONFIG_OF_MTD
1438static const struct of_device_id mxcnd_dt_ids[] = {
1439 {
1440 .compatible = "fsl,imx21-nand",
1441 .data = &imx21_nand_devtype_data,
1442 }, {
1443 .compatible = "fsl,imx27-nand",
1444 .data = &imx27_nand_devtype_data,
1445 }, {
1446 .compatible = "fsl,imx25-nand",
1447 .data = &imx25_nand_devtype_data,
1448 }, {
1449 .compatible = "fsl,imx51-nand",
1450 .data = &imx51_nand_devtype_data,
Sascha Hauer71718a8e2012-06-06 12:33:15 +02001451 }, {
1452 .compatible = "fsl,imx53-nand",
1453 .data = &imx53_nand_devtype_data,
Uwe Kleine-König64363562012-04-23 11:23:41 +02001454 },
1455 { /* sentinel */ }
1456};
Luis de Bethencourtb33c35b2015-09-18 00:13:28 +02001457MODULE_DEVICE_TABLE(of, mxcnd_dt_ids);
Uwe Kleine-König64363562012-04-23 11:23:41 +02001458
1459static int __init mxcnd_probe_dt(struct mxc_nand_host *host)
1460{
1461 struct device_node *np = host->dev->of_node;
Uwe Kleine-König64363562012-04-23 11:23:41 +02001462 const struct of_device_id *of_id =
1463 of_match_device(mxcnd_dt_ids, host->dev);
Uwe Kleine-König64363562012-04-23 11:23:41 +02001464
1465 if (!np)
1466 return 1;
1467
Uwe Kleine-König64363562012-04-23 11:23:41 +02001468 host->devtype_data = of_id->data;
1469
1470 return 0;
1471}
1472#else
1473static int __init mxcnd_probe_dt(struct mxc_nand_host *host)
1474{
1475 return 1;
1476}
1477#endif
1478
Bill Pemberton06f25512012-11-19 13:23:07 -05001479static int mxcnd_probe(struct platform_device *pdev)
Sascha Hauer34f6e152008-09-02 17:16:59 +02001480{
1481 struct nand_chip *this;
1482 struct mtd_info *mtd;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001483 struct mxc_nand_host *host;
1484 struct resource *res;
Dmitry Eremin-Solenikovd4ed8f12011-06-02 18:00:43 +04001485 int err = 0;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001486
1487 /* Allocate memory for MTD device structure and private data */
Huang Shijiea5900552013-12-21 00:02:27 +08001488 host = devm_kzalloc(&pdev->dev, sizeof(struct mxc_nand_host),
1489 GFP_KERNEL);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001490 if (!host)
1491 return -ENOMEM;
1492
Huang Shijiea5900552013-12-21 00:02:27 +08001493 /* allocate a temporary buffer for the nand_scan_ident() */
1494 host->data_buf = devm_kzalloc(&pdev->dev, PAGE_SIZE, GFP_KERNEL);
1495 if (!host->data_buf)
1496 return -ENOMEM;
Sascha Hauerf8f96082009-06-04 17:12:26 +02001497
Sascha Hauer34f6e152008-09-02 17:16:59 +02001498 host->dev = &pdev->dev;
1499 /* structures must be linked */
1500 this = &host->nand;
Boris BREZILLONa008deb2015-12-10 09:00:12 +01001501 mtd = nand_to_mtd(this);
David Brownell87f39f02009-03-26 00:42:50 -07001502 mtd->dev.parent = &pdev->dev;
Sascha Hauer1fbff0a2009-10-21 16:06:27 +02001503 mtd->name = DRIVER_NAME;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001504
1505 /* 50 us command delay time */
1506 this->chip_delay = 5;
1507
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001508 nand_set_controller_data(this, host);
Brian Norrisa61ae812015-10-30 20:33:25 -07001509 nand_set_flash_node(this, pdev->dev.of_node),
Sascha Hauer34f6e152008-09-02 17:16:59 +02001510 this->dev_ready = mxc_nand_dev_ready;
1511 this->cmdfunc = mxc_nand_command;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001512 this->read_byte = mxc_nand_read_byte;
1513 this->read_word = mxc_nand_read_word;
1514 this->write_buf = mxc_nand_write_buf;
1515 this->read_buf = mxc_nand_read_buf;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001516
Fabio Estevam24b82d32012-09-05 11:52:27 -03001517 host->clk = devm_clk_get(&pdev->dev, NULL);
Sascha Hauere4a09cb2012-06-06 12:33:13 +02001518 if (IS_ERR(host->clk))
1519 return PTR_ERR(host->clk);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001520
Sascha Hauer71885b62012-06-06 12:33:14 +02001521 err = mxcnd_probe_dt(host);
Shawn Guo4d624352012-09-15 13:34:09 +08001522 if (err > 0) {
Jingoo Han453810b2013-07-30 17:18:33 +09001523 struct mxc_nand_platform_data *pdata =
1524 dev_get_platdata(&pdev->dev);
Shawn Guo4d624352012-09-15 13:34:09 +08001525 if (pdata) {
1526 host->pdata = *pdata;
1527 host->devtype_data = (struct mxc_nand_devtype_data *)
1528 pdev->id_entry->driver_data;
1529 } else {
1530 err = -ENODEV;
1531 }
1532 }
Sascha Hauer71885b62012-06-06 12:33:14 +02001533 if (err < 0)
1534 return err;
1535
1536 if (host->devtype_data->needs_ip) {
1537 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingb0de7742013-01-21 11:09:12 +01001538 host->regs_ip = devm_ioremap_resource(&pdev->dev, res);
1539 if (IS_ERR(host->regs_ip))
1540 return PTR_ERR(host->regs_ip);
Sascha Hauer71885b62012-06-06 12:33:14 +02001541
1542 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1543 } else {
1544 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1545 }
1546
Thierry Redingb0de7742013-01-21 11:09:12 +01001547 host->base = devm_ioremap_resource(&pdev->dev, res);
1548 if (IS_ERR(host->base))
1549 return PTR_ERR(host->base);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001550
Sascha Hauerc6de7e12009-10-05 11:14:35 +02001551 host->main_area0 = host->base;
Sascha Hauer94671142009-10-05 12:14:21 +02001552
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001553 if (host->devtype_data->regs_offset)
1554 host->regs = host->base + host->devtype_data->regs_offset;
1555 host->spare0 = host->base + host->devtype_data->spare0_offset;
1556 if (host->devtype_data->axi_offset)
1557 host->regs_axi = host->base + host->devtype_data->axi_offset;
1558
1559 this->ecc.bytes = host->devtype_data->eccbytes;
1560 host->eccsize = host->devtype_data->eccsize;
1561
1562 this->select_chip = host->devtype_data->select_chip;
1563 this->ecc.size = 512;
Boris Brezillona894cf6c2016-02-03 20:02:54 +01001564 mtd_set_ooblayout(mtd, host->devtype_data->ooblayout);
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001565
Uwe Kleine-König64363562012-04-23 11:23:41 +02001566 if (host->pdata.hw_ecc) {
Sascha Hauer13e1add2009-10-21 10:39:05 +02001567 this->ecc.mode = NAND_ECC_HW;
Sascha Hauer13e1add2009-10-21 10:39:05 +02001568 } else {
1569 this->ecc.mode = NAND_ECC_SOFT;
Rafał Miłeckic1c70402016-04-08 12:23:46 +02001570 this->ecc.algo = NAND_ECC_HAMMING;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001571 }
1572
Uwe Kleine-König64363562012-04-23 11:23:41 +02001573 /* NAND bus width determines access functions used by upper layer */
1574 if (host->pdata.width == 2)
Sascha Hauer34f6e152008-09-02 17:16:59 +02001575 this->options |= NAND_BUSWIDTH_16;
Sascha Hauer13e1add2009-10-21 10:39:05 +02001576
Boris Brezillon609468f2016-04-01 14:54:29 +02001577 /* update flash based bbt */
1578 if (host->pdata.flash_bbt)
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001579 this->bbt_options |= NAND_BBT_USE_FLASH;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001580
Sascha Hauer63f14742010-10-18 10:16:26 +02001581 init_completion(&host->op_completion);
Ivo Claryssed4840182010-04-08 16:14:44 +02001582
1583 host->irq = platform_get_irq(pdev, 0);
Fabio Estevam26fbf482014-02-14 01:09:34 -02001584 if (host->irq < 0)
1585 return host->irq;
Ivo Claryssed4840182010-04-08 16:14:44 +02001586
Sascha Hauer63f14742010-10-18 10:16:26 +02001587 /*
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001588 * Use host->devtype_data->irq_control() here instead of irq_control()
1589 * because we must not disable_irq_nosync without having requested the
1590 * irq.
Sascha Hauer63f14742010-10-18 10:16:26 +02001591 */
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001592 host->devtype_data->irq_control(host, 0);
Sascha Hauer63f14742010-10-18 10:16:26 +02001593
Sascha Hauere4a09cb2012-06-06 12:33:13 +02001594 err = devm_request_irq(&pdev->dev, host->irq, mxc_nfc_irq,
Michael Opdenackerb1eb2342013-10-13 08:21:32 +02001595 0, DRIVER_NAME, host);
Ivo Claryssed4840182010-04-08 16:14:44 +02001596 if (err)
Sascha Hauere4a09cb2012-06-06 12:33:13 +02001597 return err;
1598
Fabio Estevamdcedf622013-12-02 00:50:02 -02001599 err = clk_prepare_enable(host->clk);
1600 if (err)
1601 return err;
Sascha Hauere4a09cb2012-06-06 12:33:13 +02001602 host->clk_act = 1;
Ivo Claryssed4840182010-04-08 16:14:44 +02001603
Sascha Hauer63f14742010-10-18 10:16:26 +02001604 /*
Uwe Kleine-König85569582012-04-23 11:23:34 +02001605 * Now that we "own" the interrupt make sure the interrupt mask bit is
1606 * cleared on i.MX21. Otherwise we can't read the interrupt status bit
1607 * on this machine.
Sascha Hauer63f14742010-10-18 10:16:26 +02001608 */
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001609 if (host->devtype_data->irqpending_quirk) {
Uwe Kleine-König85569582012-04-23 11:23:34 +02001610 disable_irq_nosync(host->irq);
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001611 host->devtype_data->irq_control(host, 1);
Uwe Kleine-König85569582012-04-23 11:23:34 +02001612 }
Sascha Hauer63f14742010-10-18 10:16:26 +02001613
Vladimir Barinovbd3fd622009-05-25 13:06:17 +04001614 /* first scan to find the device and get the page size */
Shawn Guo4d624352012-09-15 13:34:09 +08001615 if (nand_scan_ident(mtd, is_imx25_nfc(host) ? 4 : 1, NULL)) {
Vladimir Barinovbd3fd622009-05-25 13:06:17 +04001616 err = -ENXIO;
1617 goto escan;
1618 }
Sascha Hauer34f6e152008-09-02 17:16:59 +02001619
Boris Brezillon609468f2016-04-01 14:54:29 +02001620 switch (this->ecc.mode) {
1621 case NAND_ECC_HW:
1622 this->ecc.calculate = mxc_nand_calculate_ecc;
1623 this->ecc.hwctl = mxc_nand_enable_hwecc;
1624 this->ecc.correct = host->devtype_data->correct_data;
1625 break;
1626
1627 case NAND_ECC_SOFT:
Boris Brezillon609468f2016-04-01 14:54:29 +02001628 break;
1629
1630 default:
1631 err = -EINVAL;
1632 goto escan;
1633 }
1634
1635 if (this->bbt_options & NAND_BBT_USE_FLASH) {
1636 this->bbt_td = &bbt_main_descr;
1637 this->bbt_md = &bbt_mirror_descr;
1638 }
1639
Huang Shijiea5900552013-12-21 00:02:27 +08001640 /* allocate the right size buffer now */
1641 devm_kfree(&pdev->dev, (void *)host->data_buf);
1642 host->data_buf = devm_kzalloc(&pdev->dev, mtd->writesize + mtd->oobsize,
1643 GFP_KERNEL);
1644 if (!host->data_buf) {
1645 err = -ENOMEM;
1646 goto escan;
1647 }
1648
Sascha Hauer6e85dfd2010-08-06 15:53:10 +02001649 /* Call preset again, with correct writesize this time */
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001650 host->devtype_data->preset(mtd);
Sascha Hauer6e85dfd2010-08-06 15:53:10 +02001651
Boris Brezillona894cf6c2016-02-03 20:02:54 +01001652 if (!this->ecc.bytes) {
1653 if (host->eccsize == 8)
1654 this->ecc.bytes = 18;
1655 else if (host->eccsize == 4)
1656 this->ecc.bytes = 9;
Baruch Siach8eeb4c52015-05-13 11:17:39 +03001657 }
Sascha Hauer34f6e152008-09-02 17:16:59 +02001658
Baruch Siach7e7e4732015-05-13 11:17:37 +03001659 /*
1660 * Experimentation shows that i.MX NFC can only handle up to 218 oob
1661 * bytes. Limit used_oobsize to 218 so as to not confuse copy_spare()
1662 * into copying invalid data to/from the spare IO buffer, as this
1663 * might cause ECC data corruption when doing sub-page write to a
1664 * partially written page.
1665 */
1666 host->used_oobsize = min(mtd->oobsize, 218U);
1667
Mike Dunn6a918ba2012-03-11 14:21:11 -07001668 if (this->ecc.mode == NAND_ECC_HW) {
Shawn Guo4d624352012-09-15 13:34:09 +08001669 if (is_imx21_nfc(host) || is_imx27_nfc(host))
Mike Dunn6a918ba2012-03-11 14:21:11 -07001670 this->ecc.strength = 1;
1671 else
1672 this->ecc.strength = (host->eccsize == 4) ? 4 : 8;
1673 }
1674
Sascha Hauer4a43faf2012-05-25 16:22:42 +02001675 /* second phase scan */
1676 if (nand_scan_tail(mtd)) {
1677 err = -ENXIO;
1678 goto escan;
1679 }
1680
Sascha Hauer34f6e152008-09-02 17:16:59 +02001681 /* Register the partitions */
Uwe Kleine-König64363562012-04-23 11:23:41 +02001682 mtd_device_parse_register(mtd, part_probes,
Brian Norrisa61ae812015-10-30 20:33:25 -07001683 NULL,
Uwe Kleine-König64363562012-04-23 11:23:41 +02001684 host->pdata.parts,
1685 host->pdata.nr_parts);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001686
1687 platform_set_drvdata(pdev, host);
1688
1689 return 0;
1690
1691escan:
Lothar Waßmannc10d8ee2012-12-06 08:42:27 +01001692 if (host->clk_act)
1693 clk_disable_unprepare(host->clk);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001694
1695 return err;
1696}
1697
Bill Pemberton810b7e02012-11-19 13:26:04 -05001698static int mxcnd_remove(struct platform_device *pdev)
Sascha Hauer34f6e152008-09-02 17:16:59 +02001699{
1700 struct mxc_nand_host *host = platform_get_drvdata(pdev);
1701
Boris BREZILLONa008deb2015-12-10 09:00:12 +01001702 nand_release(nand_to_mtd(&host->nand));
Wei Yongjun8bfd4f72013-12-17 11:35:35 +08001703 if (host->clk_act)
1704 clk_disable_unprepare(host->clk);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001705
1706 return 0;
1707}
1708
Sascha Hauer34f6e152008-09-02 17:16:59 +02001709static struct platform_driver mxcnd_driver = {
1710 .driver = {
1711 .name = DRIVER_NAME,
Uwe Kleine-König64363562012-04-23 11:23:41 +02001712 .of_match_table = of_match_ptr(mxcnd_dt_ids),
Eric Bénard04dd0d32010-06-17 20:59:04 +02001713 },
Shawn Guo4d624352012-09-15 13:34:09 +08001714 .id_table = mxcnd_devtype,
Fabio Estevamddf16d62012-09-05 11:35:25 -03001715 .probe = mxcnd_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -05001716 .remove = mxcnd_remove,
Sascha Hauer34f6e152008-09-02 17:16:59 +02001717};
Fabio Estevamddf16d62012-09-05 11:35:25 -03001718module_platform_driver(mxcnd_driver);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001719
1720MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1721MODULE_DESCRIPTION("MXC NAND MTD driver");
1722MODULE_LICENSE("GPL");