blob: 1ca03d88adf1530dd9c5c6b869516b24a1735da8 [file] [log] [blame]
Fabio Estevamc786bbc2018-07-07 14:25:21 -03001// SPDX-License-Identifier: GPL-2.0+
Sascha Hauer34f6e152008-09-02 17:16:59 +02002/*
3 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
4 * Copyright 2008 Sascha Hauer, kernel@pengutronix.de
Sascha Hauer34f6e152008-09-02 17:16:59 +02005 */
6
7#include <linux/delay.h>
8#include <linux/slab.h>
9#include <linux/init.h>
10#include <linux/module.h>
11#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020012#include <linux/mtd/rawnand.h>
Sascha Hauer34f6e152008-09-02 17:16:59 +020013#include <linux/mtd/partitions.h>
14#include <linux/interrupt.h>
15#include <linux/device.h>
16#include <linux/platform_device.h>
17#include <linux/clk.h>
18#include <linux/err.h>
19#include <linux/io.h>
Sascha Hauer63f14742010-10-18 10:16:26 +020020#include <linux/irq.h>
21#include <linux/completion.h>
Sachin Kamatd367e372013-10-18 16:16:35 +053022#include <linux/of.h>
Uwe Kleine-König64363562012-04-23 11:23:41 +020023#include <linux/of_device.h>
Arnd Bergmann82906b12012-08-24 15:14:29 +020024#include <linux/platform_data/mtd-mxc_nand.h>
Sascha Hauer34f6e152008-09-02 17:16:59 +020025
26#define DRIVER_NAME "mxc_nand"
27
28/* Addresses for NFC registers */
Sascha Hauer1bc99182010-08-06 15:53:08 +020029#define NFC_V1_V2_BUF_SIZE (host->regs + 0x00)
30#define NFC_V1_V2_BUF_ADDR (host->regs + 0x04)
31#define NFC_V1_V2_FLASH_ADDR (host->regs + 0x06)
32#define NFC_V1_V2_FLASH_CMD (host->regs + 0x08)
33#define NFC_V1_V2_CONFIG (host->regs + 0x0a)
34#define NFC_V1_V2_ECC_STATUS_RESULT (host->regs + 0x0c)
35#define NFC_V1_V2_RSLTMAIN_AREA (host->regs + 0x0e)
Martin Kaiser3f77f242018-06-18 22:41:03 +020036#define NFC_V21_RSLTSPARE_AREA (host->regs + 0x10)
Sascha Hauer1bc99182010-08-06 15:53:08 +020037#define NFC_V1_V2_WRPROT (host->regs + 0x12)
38#define NFC_V1_UNLOCKSTART_BLKADDR (host->regs + 0x14)
39#define NFC_V1_UNLOCKEND_BLKADDR (host->regs + 0x16)
Baruch Siachd178e3e2011-03-14 09:01:56 +020040#define NFC_V21_UNLOCKSTART_BLKADDR0 (host->regs + 0x20)
41#define NFC_V21_UNLOCKSTART_BLKADDR1 (host->regs + 0x24)
42#define NFC_V21_UNLOCKSTART_BLKADDR2 (host->regs + 0x28)
43#define NFC_V21_UNLOCKSTART_BLKADDR3 (host->regs + 0x2c)
44#define NFC_V21_UNLOCKEND_BLKADDR0 (host->regs + 0x22)
45#define NFC_V21_UNLOCKEND_BLKADDR1 (host->regs + 0x26)
46#define NFC_V21_UNLOCKEND_BLKADDR2 (host->regs + 0x2a)
47#define NFC_V21_UNLOCKEND_BLKADDR3 (host->regs + 0x2e)
Sascha Hauer1bc99182010-08-06 15:53:08 +020048#define NFC_V1_V2_NF_WRPRST (host->regs + 0x18)
49#define NFC_V1_V2_CONFIG1 (host->regs + 0x1a)
50#define NFC_V1_V2_CONFIG2 (host->regs + 0x1c)
Sascha Hauer34f6e152008-09-02 17:16:59 +020051
Sascha Hauer6e85dfd2010-08-06 15:53:10 +020052#define NFC_V2_CONFIG1_ECC_MODE_4 (1 << 0)
Sascha Hauer1bc99182010-08-06 15:53:08 +020053#define NFC_V1_V2_CONFIG1_SP_EN (1 << 2)
54#define NFC_V1_V2_CONFIG1_ECC_EN (1 << 3)
55#define NFC_V1_V2_CONFIG1_INT_MSK (1 << 4)
56#define NFC_V1_V2_CONFIG1_BIG (1 << 5)
57#define NFC_V1_V2_CONFIG1_RST (1 << 6)
58#define NFC_V1_V2_CONFIG1_CE (1 << 7)
Sascha Hauerb8db2f52010-08-09 15:04:19 +020059#define NFC_V2_CONFIG1_ONE_CYCLE (1 << 8)
60#define NFC_V2_CONFIG1_PPB(x) (((x) & 0x3) << 9)
61#define NFC_V2_CONFIG1_FP_INT (1 << 11)
Sascha Hauer34f6e152008-09-02 17:16:59 +020062
Sascha Hauer1bc99182010-08-06 15:53:08 +020063#define NFC_V1_V2_CONFIG2_INT (1 << 15)
Sascha Hauer34f6e152008-09-02 17:16:59 +020064
Sascha Hauer1bc99182010-08-06 15:53:08 +020065/*
66 * Operation modes for the NFC. Valid for v1, v2 and v3
67 * type controllers.
68 */
69#define NFC_CMD (1 << 0)
70#define NFC_ADDR (1 << 1)
71#define NFC_INPUT (1 << 2)
72#define NFC_OUTPUT (1 << 3)
73#define NFC_ID (1 << 4)
74#define NFC_STATUS (1 << 5)
Sascha Hauer34f6e152008-09-02 17:16:59 +020075
Sascha Hauer71ec5152010-08-06 15:53:11 +020076#define NFC_V3_FLASH_CMD (host->regs_axi + 0x00)
77#define NFC_V3_FLASH_ADDR0 (host->regs_axi + 0x04)
Sascha Hauer34f6e152008-09-02 17:16:59 +020078
Sascha Hauer71ec5152010-08-06 15:53:11 +020079#define NFC_V3_CONFIG1 (host->regs_axi + 0x34)
80#define NFC_V3_CONFIG1_SP_EN (1 << 0)
81#define NFC_V3_CONFIG1_RBA(x) (((x) & 0x7 ) << 4)
Sascha Hauer34f6e152008-09-02 17:16:59 +020082
Sascha Hauer71ec5152010-08-06 15:53:11 +020083#define NFC_V3_ECC_STATUS_RESULT (host->regs_axi + 0x38)
Sascha Hauer34f6e152008-09-02 17:16:59 +020084
Sascha Hauer71ec5152010-08-06 15:53:11 +020085#define NFC_V3_LAUNCH (host->regs_axi + 0x40)
Sascha Hauer34f6e152008-09-02 17:16:59 +020086
Sascha Hauer71ec5152010-08-06 15:53:11 +020087#define NFC_V3_WRPROT (host->regs_ip + 0x0)
88#define NFC_V3_WRPROT_LOCK_TIGHT (1 << 0)
89#define NFC_V3_WRPROT_LOCK (1 << 1)
90#define NFC_V3_WRPROT_UNLOCK (1 << 2)
91#define NFC_V3_WRPROT_BLS_UNLOCK (2 << 6)
92
93#define NFC_V3_WRPROT_UNLOCK_BLK_ADD0 (host->regs_ip + 0x04)
94
95#define NFC_V3_CONFIG2 (host->regs_ip + 0x24)
96#define NFC_V3_CONFIG2_PS_512 (0 << 0)
97#define NFC_V3_CONFIG2_PS_2048 (1 << 0)
98#define NFC_V3_CONFIG2_PS_4096 (2 << 0)
99#define NFC_V3_CONFIG2_ONE_CYCLE (1 << 2)
100#define NFC_V3_CONFIG2_ECC_EN (1 << 3)
101#define NFC_V3_CONFIG2_2CMD_PHASES (1 << 4)
102#define NFC_V3_CONFIG2_NUM_ADDR_PHASE0 (1 << 5)
103#define NFC_V3_CONFIG2_ECC_MODE_8 (1 << 6)
Sascha Hauer71718a8e2012-06-06 12:33:15 +0200104#define NFC_V3_CONFIG2_PPB(x, shift) (((x) & 0x3) << shift)
Sascha Hauer71ec5152010-08-06 15:53:11 +0200105#define NFC_V3_CONFIG2_NUM_ADDR_PHASE1(x) (((x) & 0x3) << 12)
106#define NFC_V3_CONFIG2_INT_MSK (1 << 15)
107#define NFC_V3_CONFIG2_ST_CMD(x) (((x) & 0xff) << 24)
108#define NFC_V3_CONFIG2_SPAS(x) (((x) & 0xff) << 16)
109
110#define NFC_V3_CONFIG3 (host->regs_ip + 0x28)
111#define NFC_V3_CONFIG3_ADD_OP(x) (((x) & 0x3) << 0)
112#define NFC_V3_CONFIG3_FW8 (1 << 3)
113#define NFC_V3_CONFIG3_SBB(x) (((x) & 0x7) << 8)
114#define NFC_V3_CONFIG3_NUM_OF_DEVICES(x) (((x) & 0x7) << 12)
115#define NFC_V3_CONFIG3_RBB_MODE (1 << 15)
116#define NFC_V3_CONFIG3_NO_SDMA (1 << 20)
117
118#define NFC_V3_IPC (host->regs_ip + 0x2C)
119#define NFC_V3_IPC_CREQ (1 << 0)
120#define NFC_V3_IPC_INT (1 << 31)
121
122#define NFC_V3_DELAY_LINE (host->regs_ip + 0x34)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200123
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +0200124struct mxc_nand_host;
125
126struct mxc_nand_devtype_data {
127 void (*preset)(struct mtd_info *);
Sascha Hauer67b87f62018-01-17 12:32:09 +0100128 int (*read_page)(struct nand_chip *chip, void *buf, void *oob, bool ecc,
129 int page);
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +0200130 void (*send_cmd)(struct mxc_nand_host *, uint16_t, int);
131 void (*send_addr)(struct mxc_nand_host *, uint16_t, int);
132 void (*send_page)(struct mtd_info *, unsigned int);
133 void (*send_read_id)(struct mxc_nand_host *);
134 uint16_t (*get_dev_status)(struct mxc_nand_host *);
135 int (*check_int)(struct mxc_nand_host *);
136 void (*irq_control)(struct mxc_nand_host *, int);
Uwe Kleine-König6d38af22012-04-23 11:23:36 +0200137 u32 (*get_ecc_status)(struct mxc_nand_host *);
Boris Brezillona894cf6c2016-02-03 20:02:54 +0100138 const struct mtd_ooblayout_ops *ooblayout;
Uwe Kleine-König5e05a2d62012-04-23 11:23:38 +0200139 void (*select_chip)(struct mtd_info *mtd, int chip);
Boris Brezillon104e4422017-03-16 09:35:58 +0100140 int (*setup_data_interface)(struct mtd_info *mtd, int csline,
141 const struct nand_data_interface *conf);
Sascha Hauer040bd3f2018-01-17 12:32:07 +0100142 void (*enable_hwecc)(struct nand_chip *chip, bool enable);
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +0200143
144 /*
145 * On i.MX21 the CONFIG2:INT bit cannot be read if interrupts are masked
146 * (CONFIG1:INT_MSK is set). To handle this the driver uses
147 * enable_irq/disable_irq_nosync instead of CONFIG1:INT_MSK
148 */
149 int irqpending_quirk;
150 int needs_ip;
151
152 size_t regs_offset;
153 size_t spare0_offset;
154 size_t axi_offset;
155
156 int spare_len;
157 int eccbytes;
158 int eccsize;
Sascha Hauer71718a8e2012-06-06 12:33:15 +0200159 int ppb_shift;
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +0200160};
161
Sascha Hauer34f6e152008-09-02 17:16:59 +0200162struct mxc_nand_host {
Sascha Hauer34f6e152008-09-02 17:16:59 +0200163 struct nand_chip nand;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200164 struct device *dev;
165
Uwe Kleine-König4b6f05e2012-04-24 10:05:22 +0200166 void __iomem *spare0;
167 void __iomem *main_area0;
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200168
169 void __iomem *base;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200170 void __iomem *regs;
Sascha Hauer71ec5152010-08-06 15:53:11 +0200171 void __iomem *regs_axi;
172 void __iomem *regs_ip;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200173 int status_request;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200174 struct clk *clk;
175 int clk_act;
176 int irq;
Sascha Hauer94f77e52010-08-06 15:53:09 +0200177 int eccsize;
Baruch Siach7e7e4732015-05-13 11:17:37 +0300178 int used_oobsize;
Baruch Siachd178e3e2011-03-14 09:01:56 +0200179 int active_cs;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200180
Sascha Hauer63f14742010-10-18 10:16:26 +0200181 struct completion op_completion;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200182
183 uint8_t *data_buf;
184 unsigned int buf_start;
Sascha Hauer5f973042010-08-06 15:53:06 +0200185
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +0200186 const struct mxc_nand_devtype_data *devtype_data;
Uwe Kleine-König64363562012-04-23 11:23:41 +0200187 struct mxc_nand_platform_data pdata;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200188};
189
Jingoo Hanb2ac0372013-08-07 16:18:52 +0900190static const char * const part_probes[] = {
Lothar Waßmann740bb0c2012-12-06 08:42:28 +0100191 "cmdlinepart", "RedBoot", "ofpart", NULL };
Sascha Hauer34f6e152008-09-02 17:16:59 +0200192
Sascha Hauer096bcc22012-05-29 10:16:09 +0200193static void memcpy32_fromio(void *trg, const void __iomem *src, size_t size)
194{
195 int i;
196 u32 *t = trg;
197 const __iomem u32 *s = src;
198
199 for (i = 0; i < (size >> 2); i++)
200 *t++ = __raw_readl(s++);
201}
202
Baruch Siach0d17fc32015-05-13 11:17:38 +0300203static void memcpy16_fromio(void *trg, const void __iomem *src, size_t size)
204{
205 int i;
206 u16 *t = trg;
207 const __iomem u16 *s = src;
208
209 /* We assume that src (IO) is always 32bit aligned */
210 if (PTR_ALIGN(trg, 4) == trg && IS_ALIGNED(size, 4)) {
211 memcpy32_fromio(trg, src, size);
212 return;
213 }
214
215 for (i = 0; i < (size >> 1); i++)
216 *t++ = __raw_readw(s++);
217}
218
Koul, Vinod33a87a12014-10-20 21:36:13 +0530219static inline void memcpy32_toio(void __iomem *trg, const void *src, int size)
Sascha Hauer096bcc22012-05-29 10:16:09 +0200220{
Koul, Vinod33a87a12014-10-20 21:36:13 +0530221 /* __iowrite32_copy use 32bit size values so divide by 4 */
222 __iowrite32_copy(trg, src, size / 4);
Sascha Hauer096bcc22012-05-29 10:16:09 +0200223}
224
Baruch Siach0d17fc32015-05-13 11:17:38 +0300225static void memcpy16_toio(void __iomem *trg, const void *src, int size)
226{
227 int i;
228 __iomem u16 *t = trg;
229 const u16 *s = src;
230
231 /* We assume that trg (IO) is always 32bit aligned */
232 if (PTR_ALIGN(src, 4) == src && IS_ALIGNED(size, 4)) {
233 memcpy32_toio(trg, src, size);
234 return;
235 }
236
237 for (i = 0; i < (size >> 1); i++)
238 __raw_writew(*s++, t++);
239}
240
Sascha Hauer15493332018-01-17 12:32:06 +0100241/*
242 * The controller splits a page into data chunks of 512 bytes + partial oob.
243 * There are writesize / 512 such chunks, the size of the partial oob parts is
244 * oobsize / #chunks rounded down to a multiple of 2. The last oob chunk then
245 * contains additionally the byte lost by rounding (if any).
246 * This function handles the needed shuffling between host->data_buf (which
247 * holds a page in natural order, i.e. writesize bytes data + oobsize bytes
248 * spare) and the NFC buffer.
249 */
Sascha Hauer1af0b312018-01-17 12:32:08 +0100250static void copy_spare(struct mtd_info *mtd, bool bfrom, void *buf)
Sascha Hauer15493332018-01-17 12:32:06 +0100251{
252 struct nand_chip *this = mtd_to_nand(mtd);
253 struct mxc_nand_host *host = nand_get_controller_data(this);
254 u16 i, oob_chunk_size;
255 u16 num_chunks = mtd->writesize / 512;
256
Sascha Hauer1af0b312018-01-17 12:32:08 +0100257 u8 *d = buf;
Sascha Hauer15493332018-01-17 12:32:06 +0100258 u8 __iomem *s = host->spare0;
259 u16 sparebuf_size = host->devtype_data->spare_len;
260
261 /* size of oob chunk for all but possibly the last one */
262 oob_chunk_size = (host->used_oobsize / num_chunks) & ~1;
263
264 if (bfrom) {
265 for (i = 0; i < num_chunks - 1; i++)
266 memcpy16_fromio(d + i * oob_chunk_size,
267 s + i * sparebuf_size,
268 oob_chunk_size);
269
270 /* the last chunk */
271 memcpy16_fromio(d + i * oob_chunk_size,
272 s + i * sparebuf_size,
273 host->used_oobsize - i * oob_chunk_size);
274 } else {
275 for (i = 0; i < num_chunks - 1; i++)
276 memcpy16_toio(&s[i * sparebuf_size],
277 &d[i * oob_chunk_size],
278 oob_chunk_size);
279
280 /* the last chunk */
281 memcpy16_toio(&s[i * sparebuf_size],
282 &d[i * oob_chunk_size],
283 host->used_oobsize - i * oob_chunk_size);
284 }
285}
286
287/*
288 * MXC NANDFC can only perform full page+spare or spare-only read/write. When
289 * the upper layers perform a read/write buf operation, the saved column address
290 * is used to index into the full page. So usually this function is called with
291 * column == 0 (unless no column cycle is needed indicated by column == -1)
292 */
293static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
294{
295 struct nand_chip *nand_chip = mtd_to_nand(mtd);
296 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
297
298 /* Write out column address, if necessary */
299 if (column != -1) {
300 host->devtype_data->send_addr(host, column & 0xff,
301 page_addr == -1);
302 if (mtd->writesize > 512)
303 /* another col addr cycle for 2k page */
304 host->devtype_data->send_addr(host,
305 (column >> 8) & 0xff,
306 false);
307 }
308
309 /* Write out page address, if necessary */
310 if (page_addr != -1) {
311 /* paddr_0 - p_addr_7 */
312 host->devtype_data->send_addr(host, (page_addr & 0xff), false);
313
314 if (mtd->writesize > 512) {
315 if (mtd->size >= 0x10000000) {
316 /* paddr_8 - paddr_15 */
317 host->devtype_data->send_addr(host,
318 (page_addr >> 8) & 0xff,
319 false);
320 host->devtype_data->send_addr(host,
321 (page_addr >> 16) & 0xff,
322 true);
323 } else
324 /* paddr_8 - paddr_15 */
325 host->devtype_data->send_addr(host,
326 (page_addr >> 8) & 0xff, true);
327 } else {
328 if (nand_chip->options & NAND_ROW_ADDR_3) {
329 /* paddr_8 - paddr_15 */
330 host->devtype_data->send_addr(host,
331 (page_addr >> 8) & 0xff,
332 false);
333 host->devtype_data->send_addr(host,
334 (page_addr >> 16) & 0xff,
335 true);
336 } else
337 /* paddr_8 - paddr_15 */
338 host->devtype_data->send_addr(host,
339 (page_addr >> 8) & 0xff, true);
340 }
341 }
342}
343
Sascha Hauer71ec5152010-08-06 15:53:11 +0200344static int check_int_v3(struct mxc_nand_host *host)
345{
346 uint32_t tmp;
347
348 tmp = readl(NFC_V3_IPC);
349 if (!(tmp & NFC_V3_IPC_INT))
350 return 0;
351
352 tmp &= ~NFC_V3_IPC_INT;
353 writel(tmp, NFC_V3_IPC);
354
355 return 1;
356}
357
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200358static int check_int_v1_v2(struct mxc_nand_host *host)
359{
360 uint32_t tmp;
361
Sascha Hauer1bc99182010-08-06 15:53:08 +0200362 tmp = readw(NFC_V1_V2_CONFIG2);
363 if (!(tmp & NFC_V1_V2_CONFIG2_INT))
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200364 return 0;
365
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +0200366 if (!host->devtype_data->irqpending_quirk)
Sascha Hauer63f14742010-10-18 10:16:26 +0200367 writew(tmp & ~NFC_V1_V2_CONFIG2_INT, NFC_V1_V2_CONFIG2);
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200368
369 return 1;
370}
371
Sascha Hauer63f14742010-10-18 10:16:26 +0200372static void irq_control_v1_v2(struct mxc_nand_host *host, int activate)
373{
374 uint16_t tmp;
375
376 tmp = readw(NFC_V1_V2_CONFIG1);
377
378 if (activate)
379 tmp &= ~NFC_V1_V2_CONFIG1_INT_MSK;
380 else
381 tmp |= NFC_V1_V2_CONFIG1_INT_MSK;
382
383 writew(tmp, NFC_V1_V2_CONFIG1);
384}
385
386static void irq_control_v3(struct mxc_nand_host *host, int activate)
387{
388 uint32_t tmp;
389
390 tmp = readl(NFC_V3_CONFIG2);
391
392 if (activate)
393 tmp &= ~NFC_V3_CONFIG2_INT_MSK;
394 else
395 tmp |= NFC_V3_CONFIG2_INT_MSK;
396
397 writel(tmp, NFC_V3_CONFIG2);
398}
399
Uwe Kleine-König85569582012-04-23 11:23:34 +0200400static void irq_control(struct mxc_nand_host *host, int activate)
401{
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +0200402 if (host->devtype_data->irqpending_quirk) {
Uwe Kleine-König85569582012-04-23 11:23:34 +0200403 if (activate)
404 enable_irq(host->irq);
405 else
406 disable_irq_nosync(host->irq);
407 } else {
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +0200408 host->devtype_data->irq_control(host, activate);
Uwe Kleine-König85569582012-04-23 11:23:34 +0200409 }
410}
411
Uwe Kleine-König6d38af22012-04-23 11:23:36 +0200412static u32 get_ecc_status_v1(struct mxc_nand_host *host)
413{
414 return readw(NFC_V1_V2_ECC_STATUS_RESULT);
415}
416
417static u32 get_ecc_status_v2(struct mxc_nand_host *host)
418{
419 return readl(NFC_V1_V2_ECC_STATUS_RESULT);
420}
421
422static u32 get_ecc_status_v3(struct mxc_nand_host *host)
423{
424 return readl(NFC_V3_ECC_STATUS_RESULT);
425}
426
Uwe Kleine-König85569582012-04-23 11:23:34 +0200427static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
428{
429 struct mxc_nand_host *host = dev_id;
430
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +0200431 if (!host->devtype_data->check_int(host))
Uwe Kleine-König85569582012-04-23 11:23:34 +0200432 return IRQ_NONE;
433
434 irq_control(host, 0);
435
436 complete(&host->op_completion);
437
438 return IRQ_HANDLED;
439}
440
Sascha Hauer34f6e152008-09-02 17:16:59 +0200441/* This function polls the NANDFC to wait for the basic operation to
442 * complete by checking the INT bit of config2 register.
443 */
Uwe Kleine-Könige35d1d82015-02-10 19:59:55 +0100444static int wait_op_done(struct mxc_nand_host *host, int useirq)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200445{
Uwe Kleine-Könige35d1d82015-02-10 19:59:55 +0100446 int ret = 0;
447
448 /*
449 * If operation is already complete, don't bother to setup an irq or a
450 * loop.
451 */
452 if (host->devtype_data->check_int(host))
453 return 0;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200454
455 if (useirq) {
Uwe Kleine-Könige35d1d82015-02-10 19:59:55 +0100456 unsigned long timeout;
457
458 reinit_completion(&host->op_completion);
459
460 irq_control(host, 1);
461
462 timeout = wait_for_completion_timeout(&host->op_completion, HZ);
463 if (!timeout && !host->devtype_data->check_int(host)) {
464 dev_dbg(host->dev, "timeout waiting for irq\n");
465 ret = -ETIMEDOUT;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200466 }
467 } else {
Uwe Kleine-Könige35d1d82015-02-10 19:59:55 +0100468 int max_retries = 8000;
469 int done;
470
471 do {
472 udelay(1);
473
474 done = host->devtype_data->check_int(host);
475 if (done)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200476 break;
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200477
Uwe Kleine-Könige35d1d82015-02-10 19:59:55 +0100478 } while (--max_retries);
479
480 if (!done) {
481 dev_dbg(host->dev, "timeout polling for completion\n");
482 ret = -ETIMEDOUT;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200483 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200484 }
Uwe Kleine-Könige35d1d82015-02-10 19:59:55 +0100485
486 WARN_ONCE(ret < 0, "timeout! useirq=%d\n", useirq);
487
488 return ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200489}
490
Sascha Hauer71ec5152010-08-06 15:53:11 +0200491static void send_cmd_v3(struct mxc_nand_host *host, uint16_t cmd, int useirq)
492{
493 /* fill command */
494 writel(cmd, NFC_V3_FLASH_CMD);
495
496 /* send out command */
497 writel(NFC_CMD, NFC_V3_LAUNCH);
498
499 /* Wait for operation to complete */
500 wait_op_done(host, useirq);
501}
502
Sascha Hauer34f6e152008-09-02 17:16:59 +0200503/* This function issues the specified command to the NAND device and
504 * waits for completion. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200505static void send_cmd_v1_v2(struct mxc_nand_host *host, uint16_t cmd, int useirq)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200506{
Sascha Hauer1f3df4d2017-10-16 11:51:55 +0200507 dev_dbg(host->dev, "send_cmd(host, 0x%x, %d)\n", cmd, useirq);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200508
Sascha Hauer1bc99182010-08-06 15:53:08 +0200509 writew(cmd, NFC_V1_V2_FLASH_CMD);
510 writew(NFC_CMD, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200511
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +0200512 if (host->devtype_data->irqpending_quirk && (cmd == NAND_CMD_RESET)) {
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200513 int max_retries = 100;
514 /* Reset completion is indicated by NFC_CONFIG2 */
515 /* being set to 0 */
516 while (max_retries-- > 0) {
Sascha Hauer1bc99182010-08-06 15:53:08 +0200517 if (readw(NFC_V1_V2_CONFIG2) == 0) {
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200518 break;
519 }
520 udelay(1);
521 }
522 if (max_retries < 0)
Sascha Hauer1f3df4d2017-10-16 11:51:55 +0200523 dev_dbg(host->dev, "%s: RESET failed\n", __func__);
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200524 } else {
525 /* Wait for operation to complete */
526 wait_op_done(host, useirq);
527 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200528}
529
Sascha Hauer71ec5152010-08-06 15:53:11 +0200530static void send_addr_v3(struct mxc_nand_host *host, uint16_t addr, int islast)
531{
532 /* fill address */
533 writel(addr, NFC_V3_FLASH_ADDR0);
534
535 /* send out address */
536 writel(NFC_ADDR, NFC_V3_LAUNCH);
537
538 wait_op_done(host, 0);
539}
540
Sascha Hauer34f6e152008-09-02 17:16:59 +0200541/* This function sends an address (or partial address) to the
542 * NAND device. The address is used to select the source/destination for
543 * a NAND command. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200544static void send_addr_v1_v2(struct mxc_nand_host *host, uint16_t addr, int islast)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200545{
Sascha Hauer1f3df4d2017-10-16 11:51:55 +0200546 dev_dbg(host->dev, "send_addr(host, 0x%x %d)\n", addr, islast);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200547
Sascha Hauer1bc99182010-08-06 15:53:08 +0200548 writew(addr, NFC_V1_V2_FLASH_ADDR);
549 writew(NFC_ADDR, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200550
551 /* Wait for operation to complete */
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200552 wait_op_done(host, islast);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200553}
554
Sascha Hauer71ec5152010-08-06 15:53:11 +0200555static void send_page_v3(struct mtd_info *mtd, unsigned int ops)
556{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100557 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100558 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauer71ec5152010-08-06 15:53:11 +0200559 uint32_t tmp;
560
561 tmp = readl(NFC_V3_CONFIG1);
562 tmp &= ~(7 << 4);
563 writel(tmp, NFC_V3_CONFIG1);
564
565 /* transfer data from NFC ram to nand */
566 writel(ops, NFC_V3_LAUNCH);
567
568 wait_op_done(host, false);
569}
570
Uwe Kleine-König6d38af22012-04-23 11:23:36 +0200571static void send_page_v2(struct mtd_info *mtd, unsigned int ops)
572{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100573 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100574 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Uwe Kleine-König6d38af22012-04-23 11:23:36 +0200575
576 /* NANDFC buffer 0 is used for page read/write */
577 writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
578
579 writew(ops, NFC_V1_V2_CONFIG2);
580
581 /* Wait for operation to complete */
582 wait_op_done(host, true);
583}
584
585static void send_page_v1(struct mtd_info *mtd, unsigned int ops)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200586{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100587 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100588 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200589 int bufs, i;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200590
Uwe Kleine-König6d38af22012-04-23 11:23:36 +0200591 if (mtd->writesize > 512)
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200592 bufs = 4;
593 else
594 bufs = 1;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200595
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200596 for (i = 0; i < bufs; i++) {
597
598 /* NANDFC buffer 0 is used for page read/write */
Baruch Siachd178e3e2011-03-14 09:01:56 +0200599 writew((host->active_cs << 4) | i, NFC_V1_V2_BUF_ADDR);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200600
Sascha Hauer1bc99182010-08-06 15:53:08 +0200601 writew(ops, NFC_V1_V2_CONFIG2);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200602
603 /* Wait for operation to complete */
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200604 wait_op_done(host, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200605 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200606}
607
Sascha Hauer71ec5152010-08-06 15:53:11 +0200608static void send_read_id_v3(struct mxc_nand_host *host)
609{
610 /* Read ID into main buffer */
611 writel(NFC_ID, NFC_V3_LAUNCH);
612
613 wait_op_done(host, true);
614
Sascha Hauer096bcc22012-05-29 10:16:09 +0200615 memcpy32_fromio(host->data_buf, host->main_area0, 16);
Sascha Hauer71ec5152010-08-06 15:53:11 +0200616}
617
Sascha Hauer34f6e152008-09-02 17:16:59 +0200618/* Request the NANDFC to perform a read of the NAND device ID. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200619static void send_read_id_v1_v2(struct mxc_nand_host *host)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200620{
Sascha Hauer34f6e152008-09-02 17:16:59 +0200621 /* NANDFC buffer 0 is used for device ID output */
Baruch Siachd178e3e2011-03-14 09:01:56 +0200622 writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200623
Sascha Hauer1bc99182010-08-06 15:53:08 +0200624 writew(NFC_ID, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200625
626 /* Wait for operation to complete */
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200627 wait_op_done(host, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200628
Sascha Hauer096bcc22012-05-29 10:16:09 +0200629 memcpy32_fromio(host->data_buf, host->main_area0, 16);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200630}
631
Sascha Hauer71ec5152010-08-06 15:53:11 +0200632static uint16_t get_dev_status_v3(struct mxc_nand_host *host)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200633{
Sascha Hauer71ec5152010-08-06 15:53:11 +0200634 writew(NFC_STATUS, NFC_V3_LAUNCH);
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200635 wait_op_done(host, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200636
Sascha Hauer71ec5152010-08-06 15:53:11 +0200637 return readl(NFC_V3_CONFIG1) >> 16;
638}
639
Sascha Hauer34f6e152008-09-02 17:16:59 +0200640/* This function requests the NANDFC to perform a read of the
641 * NAND device status and returns the current status. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200642static uint16_t get_dev_status_v1_v2(struct mxc_nand_host *host)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200643{
Sascha Hauerc29c6072010-08-06 15:53:05 +0200644 void __iomem *main_buf = host->main_area0;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200645 uint32_t store;
646 uint16_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200647
Baruch Siachd178e3e2011-03-14 09:01:56 +0200648 writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
Sascha Hauerc29c6072010-08-06 15:53:05 +0200649
650 /*
651 * The device status is stored in main_area0. To
652 * prevent corruption of the buffer save the value
653 * and restore it afterwards.
654 */
Sascha Hauer34f6e152008-09-02 17:16:59 +0200655 store = readl(main_buf);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200656
Sascha Hauer1bc99182010-08-06 15:53:08 +0200657 writew(NFC_STATUS, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200658 wait_op_done(host, true);
659
Sascha Hauer34f6e152008-09-02 17:16:59 +0200660 ret = readw(main_buf);
Sascha Hauerc29c6072010-08-06 15:53:05 +0200661
Sascha Hauer34f6e152008-09-02 17:16:59 +0200662 writel(store, main_buf);
663
664 return ret;
665}
666
Sascha Hauer040bd3f2018-01-17 12:32:07 +0100667static void mxc_nand_enable_hwecc_v1_v2(struct nand_chip *chip, bool enable)
668{
669 struct mxc_nand_host *host = nand_get_controller_data(chip);
670 uint16_t config1;
671
672 if (chip->ecc.mode != NAND_ECC_HW)
673 return;
674
675 config1 = readw(NFC_V1_V2_CONFIG1);
676
677 if (enable)
678 config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
679 else
680 config1 &= ~NFC_V1_V2_CONFIG1_ECC_EN;
681
682 writew(config1, NFC_V1_V2_CONFIG1);
683}
684
685static void mxc_nand_enable_hwecc_v3(struct nand_chip *chip, bool enable)
686{
687 struct mxc_nand_host *host = nand_get_controller_data(chip);
688 uint32_t config2;
689
690 if (chip->ecc.mode != NAND_ECC_HW)
691 return;
692
693 config2 = readl(NFC_V3_CONFIG2);
694
695 if (enable)
696 config2 |= NFC_V3_CONFIG2_ECC_EN;
697 else
698 config2 &= ~NFC_V3_CONFIG2_ECC_EN;
699
700 writel(config2, NFC_V3_CONFIG2);
701}
702
Sascha Hauer34f6e152008-09-02 17:16:59 +0200703/* This functions is used by upper layer to checks if device is ready */
704static int mxc_nand_dev_ready(struct mtd_info *mtd)
705{
706 /*
707 * NFC handles R/B internally. Therefore, this function
708 * always returns status as ready.
709 */
710 return 1;
711}
712
Sascha Hauer5039fc92018-01-17 12:32:10 +0100713static int mxc_nand_read_page_v1(struct nand_chip *chip, void *buf, void *oob,
714 bool ecc, int page)
715{
716 struct mtd_info *mtd = nand_to_mtd(chip);
717 struct mxc_nand_host *host = nand_get_controller_data(chip);
718 unsigned int bitflips_corrected = 0;
719 int no_subpages;
720 int i;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200721
Sascha Hauer5039fc92018-01-17 12:32:10 +0100722 host->devtype_data->enable_hwecc(chip, ecc);
723
724 host->devtype_data->send_cmd(host, NAND_CMD_READ0, false);
725 mxc_do_addr_cycle(mtd, 0, page);
726
727 if (mtd->writesize > 512)
728 host->devtype_data->send_cmd(host, NAND_CMD_READSTART, true);
729
730 no_subpages = mtd->writesize >> 9;
731
732 for (i = 0; i < no_subpages; i++) {
733 uint16_t ecc_stats;
734
735 /* NANDFC buffer 0 is used for page read/write */
736 writew((host->active_cs << 4) | i, NFC_V1_V2_BUF_ADDR);
737
738 writew(NFC_OUTPUT, NFC_V1_V2_CONFIG2);
739
740 /* Wait for operation to complete */
741 wait_op_done(host, true);
742
743 ecc_stats = get_ecc_status_v1(host);
744
745 ecc_stats >>= 2;
746
747 if (buf && ecc) {
748 switch (ecc_stats & 0x3) {
749 case 0:
750 default:
751 break;
752 case 1:
753 mtd->ecc_stats.corrected++;
754 bitflips_corrected = 1;
755 break;
756 case 2:
757 mtd->ecc_stats.failed++;
758 break;
759 }
760 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200761 }
762
Sascha Hauer5039fc92018-01-17 12:32:10 +0100763 if (buf)
764 memcpy32_fromio(buf, host->main_area0, mtd->writesize);
765 if (oob)
766 copy_spare(mtd, true, oob);
767
768 return bitflips_corrected;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200769}
770
Sascha Hauer67b87f62018-01-17 12:32:09 +0100771static int mxc_nand_read_page_v2_v3(struct nand_chip *chip, void *buf,
772 void *oob, bool ecc, int page)
773{
774 struct mtd_info *mtd = nand_to_mtd(chip);
775 struct mxc_nand_host *host = nand_get_controller_data(chip);
776 unsigned int max_bitflips = 0;
Sascha Hauer94f77e52010-08-06 15:53:09 +0200777 u32 ecc_stat, err;
Sascha Hauer67b87f62018-01-17 12:32:09 +0100778 int no_subpages;
Sascha Hauer94f77e52010-08-06 15:53:09 +0200779 u8 ecc_bit_mask, err_limit;
780
Sascha Hauer67b87f62018-01-17 12:32:09 +0100781 host->devtype_data->enable_hwecc(chip, ecc);
782
783 host->devtype_data->send_cmd(host, NAND_CMD_READ0, false);
784 mxc_do_addr_cycle(mtd, 0, page);
785
786 if (mtd->writesize > 512)
787 host->devtype_data->send_cmd(host,
788 NAND_CMD_READSTART, true);
789
790 host->devtype_data->send_page(mtd, NFC_OUTPUT);
791
792 if (buf)
793 memcpy32_fromio(buf, host->main_area0, mtd->writesize);
794 if (oob)
795 copy_spare(mtd, true, oob);
796
Sascha Hauer94f77e52010-08-06 15:53:09 +0200797 ecc_bit_mask = (host->eccsize == 4) ? 0x7 : 0xf;
798 err_limit = (host->eccsize == 4) ? 0x4 : 0x8;
799
800 no_subpages = mtd->writesize >> 9;
801
Uwe Kleine-König6d38af22012-04-23 11:23:36 +0200802 ecc_stat = host->devtype_data->get_ecc_status(host);
Sascha Hauer94f77e52010-08-06 15:53:09 +0200803
804 do {
805 err = ecc_stat & ecc_bit_mask;
806 if (err > err_limit) {
Sascha Hauer67b87f62018-01-17 12:32:09 +0100807 mtd->ecc_stats.failed++;
Sascha Hauer94f77e52010-08-06 15:53:09 +0200808 } else {
Sascha Hauer67b87f62018-01-17 12:32:09 +0100809 mtd->ecc_stats.corrected += err;
810 max_bitflips = max_t(unsigned int, max_bitflips, err);
Sascha Hauer94f77e52010-08-06 15:53:09 +0200811 }
Sascha Hauer67b87f62018-01-17 12:32:09 +0100812
Sascha Hauer94f77e52010-08-06 15:53:09 +0200813 ecc_stat >>= 4;
814 } while (--no_subpages);
815
Sascha Hauer67b87f62018-01-17 12:32:09 +0100816 return max_bitflips;
817}
Sascha Hauer94f77e52010-08-06 15:53:09 +0200818
Sascha Hauer67b87f62018-01-17 12:32:09 +0100819static int mxc_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
820 uint8_t *buf, int oob_required, int page)
821{
822 struct mxc_nand_host *host = nand_get_controller_data(chip);
823 void *oob_buf;
824
825 if (oob_required)
826 oob_buf = chip->oob_poi;
827 else
828 oob_buf = NULL;
829
830 return host->devtype_data->read_page(chip, buf, oob_buf, 1, page);
831}
832
833static int mxc_nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
834 uint8_t *buf, int oob_required, int page)
835{
836 struct mxc_nand_host *host = nand_get_controller_data(chip);
837 void *oob_buf;
838
839 if (oob_required)
840 oob_buf = chip->oob_poi;
841 else
842 oob_buf = NULL;
843
844 return host->devtype_data->read_page(chip, buf, oob_buf, 0, page);
845}
846
847static int mxc_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
848 int page)
849{
850 struct mxc_nand_host *host = nand_get_controller_data(chip);
851
852 return host->devtype_data->read_page(chip, NULL, chip->oob_poi, 0,
853 page);
Sascha Hauer94f77e52010-08-06 15:53:09 +0200854}
855
Sascha Hauer6811c462018-01-17 12:32:11 +0100856static int mxc_nand_write_page(struct nand_chip *chip, const uint8_t *buf,
857 bool ecc, int page)
858{
859 struct mtd_info *mtd = nand_to_mtd(chip);
860 struct mxc_nand_host *host = nand_get_controller_data(chip);
861
862 host->devtype_data->enable_hwecc(chip, ecc);
863
864 host->devtype_data->send_cmd(host, NAND_CMD_SEQIN, false);
865 mxc_do_addr_cycle(mtd, 0, page);
866
867 memcpy32_toio(host->main_area0, buf, mtd->writesize);
868 copy_spare(mtd, false, chip->oob_poi);
869
870 host->devtype_data->send_page(mtd, NFC_INPUT);
871 host->devtype_data->send_cmd(host, NAND_CMD_PAGEPROG, true);
872 mxc_do_addr_cycle(mtd, 0, page);
873
874 return 0;
875}
876
877static int mxc_nand_write_page_ecc(struct mtd_info *mtd, struct nand_chip *chip,
878 const uint8_t *buf, int oob_required,
879 int page)
880{
881 return mxc_nand_write_page(chip, buf, true, page);
882}
883
884static int mxc_nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
885 const uint8_t *buf, int oob_required, int page)
886{
887 return mxc_nand_write_page(chip, buf, false, page);
888}
889
890static int mxc_nand_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
891 int page)
892{
893 struct mxc_nand_host *host = nand_get_controller_data(chip);
894
895 memset(host->data_buf, 0xff, mtd->writesize);
896
897 return mxc_nand_write_page(chip, host->data_buf, false, page);
898}
899
Sascha Hauer34f6e152008-09-02 17:16:59 +0200900static u_char mxc_nand_read_byte(struct mtd_info *mtd)
901{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100902 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100903 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauerf8f96082009-06-04 17:12:26 +0200904 uint8_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200905
906 /* Check for status request */
907 if (host->status_request)
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +0200908 return host->devtype_data->get_dev_status(host) & 0xFF;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200909
Uwe Kleine-König3f410692015-02-10 19:59:57 +0100910 if (nand_chip->options & NAND_BUSWIDTH_16) {
911 /* only take the lower byte of each word */
912 ret = *(uint16_t *)(host->data_buf + host->buf_start);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200913
Uwe Kleine-König3f410692015-02-10 19:59:57 +0100914 host->buf_start += 2;
915 } else {
916 ret = *(uint8_t *)(host->data_buf + host->buf_start);
917 host->buf_start++;
918 }
919
Sascha Hauer1f3df4d2017-10-16 11:51:55 +0200920 dev_dbg(host->dev, "%s: ret=0x%hhx (start=%u)\n", __func__, ret, host->buf_start);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200921 return ret;
922}
923
Sascha Hauer34f6e152008-09-02 17:16:59 +0200924/* Write data of length len to buffer buf. The data to be
925 * written on NAND Flash is first copied to RAMbuffer. After the Data Input
926 * Operation by the NFC, the data is written to NAND Flash */
927static void mxc_nand_write_buf(struct mtd_info *mtd,
928 const u_char *buf, int len)
929{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100930 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100931 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauerf8f96082009-06-04 17:12:26 +0200932 u16 col = host->buf_start;
933 int n = mtd->oobsize + mtd->writesize - col;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200934
Sascha Hauerf8f96082009-06-04 17:12:26 +0200935 n = min(n, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200936
Sascha Hauerf8f96082009-06-04 17:12:26 +0200937 memcpy(host->data_buf + col, buf, n);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200938
Sascha Hauerf8f96082009-06-04 17:12:26 +0200939 host->buf_start += n;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200940}
941
942/* Read the data buffer from the NAND Flash. To read the data from NAND
943 * Flash first the data output cycle is initiated by the NFC, which copies
944 * the data to RAMbuffer. This data of length len is then copied to buffer buf.
945 */
946static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
947{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100948 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100949 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauerf8f96082009-06-04 17:12:26 +0200950 u16 col = host->buf_start;
951 int n = mtd->oobsize + mtd->writesize - col;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200952
Sascha Hauerf8f96082009-06-04 17:12:26 +0200953 n = min(n, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200954
Baruch Siach5d9d9932011-03-02 16:47:55 +0200955 memcpy(buf, host->data_buf + col, n);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200956
Baruch Siach5d9d9932011-03-02 16:47:55 +0200957 host->buf_start += n;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200958}
959
Sascha Hauer34f6e152008-09-02 17:16:59 +0200960/* This function is used by upper layer for select and
961 * deselect of the NAND chip */
Uwe Kleine-König5e05a2d62012-04-23 11:23:38 +0200962static void mxc_nand_select_chip_v1_v3(struct mtd_info *mtd, int chip)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200963{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100964 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100965 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200966
Baruch Siachd178e3e2011-03-14 09:01:56 +0200967 if (chip == -1) {
Sascha Hauer34f6e152008-09-02 17:16:59 +0200968 /* Disable the NFC clock */
969 if (host->clk_act) {
Sascha Hauer97c32132012-03-07 20:56:35 +0100970 clk_disable_unprepare(host->clk);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200971 host->clk_act = 0;
972 }
Baruch Siachd178e3e2011-03-14 09:01:56 +0200973 return;
974 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200975
Baruch Siachd178e3e2011-03-14 09:01:56 +0200976 if (!host->clk_act) {
977 /* Enable the NFC clock */
Sascha Hauer97c32132012-03-07 20:56:35 +0100978 clk_prepare_enable(host->clk);
Baruch Siachd178e3e2011-03-14 09:01:56 +0200979 host->clk_act = 1;
980 }
Uwe Kleine-König5e05a2d62012-04-23 11:23:38 +0200981}
Baruch Siachd178e3e2011-03-14 09:01:56 +0200982
Uwe Kleine-König5e05a2d62012-04-23 11:23:38 +0200983static void mxc_nand_select_chip_v2(struct mtd_info *mtd, int chip)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200984{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100985 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100986 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200987
988 if (chip == -1) {
989 /* Disable the NFC clock */
990 if (host->clk_act) {
Fabio Estevam3d059692012-05-25 20:14:50 -0300991 clk_disable_unprepare(host->clk);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200992 host->clk_act = 0;
993 }
994 return;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200995 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200996
997 if (!host->clk_act) {
998 /* Enable the NFC clock */
Fabio Estevam3d059692012-05-25 20:14:50 -0300999 clk_prepare_enable(host->clk);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001000 host->clk_act = 1;
1001 }
1002
Uwe Kleine-König5e05a2d62012-04-23 11:23:38 +02001003 host->active_cs = chip;
1004 writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001005}
1006
Boris Brezillon3bff08d2016-11-25 11:32:32 +01001007#define MXC_V1_ECCBYTES 5
1008
Boris Brezillona894cf6c2016-02-03 20:02:54 +01001009static int mxc_v1_ooblayout_ecc(struct mtd_info *mtd, int section,
1010 struct mtd_oob_region *oobregion)
1011{
1012 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1013
1014 if (section >= nand_chip->ecc.steps)
1015 return -ERANGE;
1016
1017 oobregion->offset = (section * 16) + 6;
Boris Brezillon3bff08d2016-11-25 11:32:32 +01001018 oobregion->length = MXC_V1_ECCBYTES;
Boris Brezillona894cf6c2016-02-03 20:02:54 +01001019
1020 return 0;
1021}
1022
1023static int mxc_v1_ooblayout_free(struct mtd_info *mtd, int section,
1024 struct mtd_oob_region *oobregion)
1025{
1026 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1027
1028 if (section > nand_chip->ecc.steps)
1029 return -ERANGE;
1030
1031 if (!section) {
1032 if (mtd->writesize <= 512) {
1033 oobregion->offset = 0;
1034 oobregion->length = 5;
1035 } else {
1036 oobregion->offset = 2;
1037 oobregion->length = 4;
1038 }
1039 } else {
Boris Brezillon3bff08d2016-11-25 11:32:32 +01001040 oobregion->offset = ((section - 1) * 16) + MXC_V1_ECCBYTES + 6;
Boris Brezillona894cf6c2016-02-03 20:02:54 +01001041 if (section < nand_chip->ecc.steps)
1042 oobregion->length = (section * 16) + 6 -
1043 oobregion->offset;
1044 else
1045 oobregion->length = mtd->oobsize - oobregion->offset;
1046 }
1047
1048 return 0;
1049}
1050
1051static const struct mtd_ooblayout_ops mxc_v1_ooblayout_ops = {
1052 .ecc = mxc_v1_ooblayout_ecc,
1053 .free = mxc_v1_ooblayout_free,
1054};
1055
1056static int mxc_v2_ooblayout_ecc(struct mtd_info *mtd, int section,
1057 struct mtd_oob_region *oobregion)
1058{
1059 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1060 int stepsize = nand_chip->ecc.bytes == 9 ? 16 : 26;
1061
1062 if (section >= nand_chip->ecc.steps)
1063 return -ERANGE;
1064
1065 oobregion->offset = (section * stepsize) + 7;
1066 oobregion->length = nand_chip->ecc.bytes;
1067
1068 return 0;
1069}
1070
1071static int mxc_v2_ooblayout_free(struct mtd_info *mtd, int section,
1072 struct mtd_oob_region *oobregion)
1073{
1074 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1075 int stepsize = nand_chip->ecc.bytes == 9 ? 16 : 26;
1076
Lothar Waßmann38178e72016-09-19 11:09:40 +02001077 if (section >= nand_chip->ecc.steps)
Boris Brezillona894cf6c2016-02-03 20:02:54 +01001078 return -ERANGE;
1079
1080 if (!section) {
1081 if (mtd->writesize <= 512) {
1082 oobregion->offset = 0;
1083 oobregion->length = 5;
1084 } else {
1085 oobregion->offset = 2;
1086 oobregion->length = 4;
1087 }
1088 } else {
1089 oobregion->offset = section * stepsize;
1090 oobregion->length = 7;
1091 }
1092
1093 return 0;
1094}
1095
1096static const struct mtd_ooblayout_ops mxc_v2_ooblayout_ops = {
1097 .ecc = mxc_v2_ooblayout_ecc,
1098 .free = mxc_v2_ooblayout_free,
1099};
1100
Sascha Hauer6e85dfd2010-08-06 15:53:10 +02001101/*
1102 * v2 and v3 type controllers can do 4bit or 8bit ecc depending
1103 * on how much oob the nand chip has. For 8bit ecc we need at least
1104 * 26 bytes of oob data per 512 byte block.
1105 */
1106static int get_eccsize(struct mtd_info *mtd)
1107{
1108 int oobbytes_per_512 = 0;
1109
1110 oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize;
1111
1112 if (oobbytes_per_512 < 26)
1113 return 4;
1114 else
1115 return 8;
1116}
1117
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001118static void preset_v1(struct mtd_info *mtd)
Ivo Claryssed4840182010-04-08 16:14:44 +02001119{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001120 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001121 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauerb8db2f52010-08-09 15:04:19 +02001122 uint16_t config1 = 0;
Ivo Claryssed4840182010-04-08 16:14:44 +02001123
Uwe Kleine-König1f42adc2015-02-10 19:59:56 +01001124 if (nand_chip->ecc.mode == NAND_ECC_HW && mtd->writesize)
Sascha Hauerb8db2f52010-08-09 15:04:19 +02001125 config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
1126
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001127 if (!host->devtype_data->irqpending_quirk)
Sascha Hauerb8db2f52010-08-09 15:04:19 +02001128 config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
Sascha Hauer6e85dfd2010-08-06 15:53:10 +02001129
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001130 host->eccsize = 1;
1131
1132 writew(config1, NFC_V1_V2_CONFIG1);
1133 /* preset operation */
1134
1135 /* Unlock the internal RAM Buffer */
1136 writew(0x2, NFC_V1_V2_CONFIG);
1137
1138 /* Blocks to be unlocked */
1139 writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR);
1140 writew(0xffff, NFC_V1_UNLOCKEND_BLKADDR);
1141
1142 /* Unlock Block Command for given address range */
1143 writew(0x4, NFC_V1_V2_WRPROT);
1144}
1145
Boris Brezillon104e4422017-03-16 09:35:58 +01001146static int mxc_nand_v2_setup_data_interface(struct mtd_info *mtd, int csline,
1147 const struct nand_data_interface *conf)
Sascha Hauer82830792016-09-15 10:32:53 +02001148{
1149 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1150 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
1151 int tRC_min_ns, tRC_ps, ret;
1152 unsigned long rate, rate_round;
1153 const struct nand_sdr_timings *timings;
1154 u16 config1;
1155
1156 timings = nand_get_sdr_timings(conf);
1157 if (IS_ERR(timings))
1158 return -ENOTSUPP;
1159
1160 config1 = readw(NFC_V1_V2_CONFIG1);
1161
1162 tRC_min_ns = timings->tRC_min / 1000;
1163 rate = 1000000000 / tRC_min_ns;
1164
1165 /*
1166 * For tRC < 30ns we have to use EDO mode. In this case the controller
1167 * does one access per clock cycle. Otherwise the controller does one
1168 * access in two clock cycles, thus we have to double the rate to the
1169 * controller.
1170 */
1171 if (tRC_min_ns < 30) {
1172 rate_round = clk_round_rate(host->clk, rate);
1173 config1 |= NFC_V2_CONFIG1_ONE_CYCLE;
1174 tRC_ps = 1000000000 / (rate_round / 1000);
1175 } else {
1176 rate *= 2;
1177 rate_round = clk_round_rate(host->clk, rate);
1178 config1 &= ~NFC_V2_CONFIG1_ONE_CYCLE;
1179 tRC_ps = 1000000000 / (rate_round / 1000 / 2);
1180 }
1181
1182 /*
1183 * The timing values compared against are from the i.MX25 Automotive
1184 * datasheet, Table 50. NFC Timing Parameters
1185 */
1186 if (timings->tCLS_min > tRC_ps - 1000 ||
1187 timings->tCLH_min > tRC_ps - 2000 ||
1188 timings->tCS_min > tRC_ps - 1000 ||
1189 timings->tCH_min > tRC_ps - 2000 ||
1190 timings->tWP_min > tRC_ps - 1500 ||
1191 timings->tALS_min > tRC_ps ||
1192 timings->tALH_min > tRC_ps - 3000 ||
1193 timings->tDS_min > tRC_ps ||
1194 timings->tDH_min > tRC_ps - 5000 ||
1195 timings->tWC_min > 2 * tRC_ps ||
1196 timings->tWH_min > tRC_ps - 2500 ||
1197 timings->tRR_min > 6 * tRC_ps ||
1198 timings->tRP_min > 3 * tRC_ps / 2 ||
1199 timings->tRC_min > 2 * tRC_ps ||
1200 timings->tREH_min > (tRC_ps / 2) - 2500) {
1201 dev_dbg(host->dev, "Timing out of bounds\n");
1202 return -EINVAL;
1203 }
1204
Boris Brezillon104e4422017-03-16 09:35:58 +01001205 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
Sascha Hauer82830792016-09-15 10:32:53 +02001206 return 0;
1207
1208 ret = clk_set_rate(host->clk, rate);
1209 if (ret)
1210 return ret;
1211
1212 writew(config1, NFC_V1_V2_CONFIG1);
1213
1214 dev_dbg(host->dev, "Setting rate to %ldHz, %s mode\n", rate_round,
1215 config1 & NFC_V2_CONFIG1_ONE_CYCLE ? "One cycle (EDO)" :
1216 "normal");
1217
1218 return 0;
1219}
1220
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001221static void preset_v2(struct mtd_info *mtd)
1222{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001223 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001224 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001225 uint16_t config1 = 0;
1226
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001227 config1 |= NFC_V2_CONFIG1_FP_INT;
Ivo Claryssed4840182010-04-08 16:14:44 +02001228
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001229 if (!host->devtype_data->irqpending_quirk)
Ivo Claryssed4840182010-04-08 16:14:44 +02001230 config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
Sascha Hauer6e85dfd2010-08-06 15:53:10 +02001231
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001232 if (mtd->writesize) {
Sascha Hauerb8db2f52010-08-09 15:04:19 +02001233 uint16_t pages_per_block = mtd->erasesize / mtd->writesize;
1234
Uwe Kleine-König1f42adc2015-02-10 19:59:56 +01001235 if (nand_chip->ecc.mode == NAND_ECC_HW)
1236 config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
1237
Sascha Hauer6e85dfd2010-08-06 15:53:10 +02001238 host->eccsize = get_eccsize(mtd);
1239 if (host->eccsize == 4)
Sascha Hauerb8db2f52010-08-09 15:04:19 +02001240 config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
1241
1242 config1 |= NFC_V2_CONFIG1_PPB(ffs(pages_per_block) - 6);
Sascha Hauer6e85dfd2010-08-06 15:53:10 +02001243 } else {
1244 host->eccsize = 1;
1245 }
1246
Sascha Hauerb8db2f52010-08-09 15:04:19 +02001247 writew(config1, NFC_V1_V2_CONFIG1);
Ivo Claryssed4840182010-04-08 16:14:44 +02001248 /* preset operation */
1249
Martin Kaiser3f77f242018-06-18 22:41:03 +02001250 /* spare area size in 16-bit half-words */
1251 writew(mtd->oobsize / 2, NFC_V21_RSLTSPARE_AREA);
1252
Ivo Claryssed4840182010-04-08 16:14:44 +02001253 /* Unlock the internal RAM Buffer */
Sascha Hauer1bc99182010-08-06 15:53:08 +02001254 writew(0x2, NFC_V1_V2_CONFIG);
Ivo Claryssed4840182010-04-08 16:14:44 +02001255
1256 /* Blocks to be unlocked */
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001257 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR0);
1258 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR1);
1259 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR2);
1260 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR3);
1261 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR0);
1262 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR1);
1263 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR2);
1264 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR3);
Ivo Claryssed4840182010-04-08 16:14:44 +02001265
1266 /* Unlock Block Command for given address range */
Sascha Hauer1bc99182010-08-06 15:53:08 +02001267 writew(0x4, NFC_V1_V2_WRPROT);
Ivo Claryssed4840182010-04-08 16:14:44 +02001268}
1269
Sascha Hauer71ec5152010-08-06 15:53:11 +02001270static void preset_v3(struct mtd_info *mtd)
1271{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001272 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001273 struct mxc_nand_host *host = nand_get_controller_data(chip);
Sascha Hauer71ec5152010-08-06 15:53:11 +02001274 uint32_t config2, config3;
1275 int i, addr_phases;
1276
1277 writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1);
1278 writel(NFC_V3_IPC_CREQ, NFC_V3_IPC);
1279
1280 /* Unlock the internal RAM Buffer */
1281 writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
1282 NFC_V3_WRPROT);
1283
1284 /* Blocks to be unlocked */
1285 for (i = 0; i < NAND_MAX_CHIPS; i++)
Fabio Estevam1b15b1f2015-11-17 13:58:50 -02001286 writel(0xffff << 16, NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2));
Sascha Hauer71ec5152010-08-06 15:53:11 +02001287
1288 writel(0, NFC_V3_IPC);
1289
1290 config2 = NFC_V3_CONFIG2_ONE_CYCLE |
1291 NFC_V3_CONFIG2_2CMD_PHASES |
1292 NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) |
1293 NFC_V3_CONFIG2_ST_CMD(0x70) |
Sascha Hauer63f14742010-10-18 10:16:26 +02001294 NFC_V3_CONFIG2_INT_MSK |
Sascha Hauer71ec5152010-08-06 15:53:11 +02001295 NFC_V3_CONFIG2_NUM_ADDR_PHASE0;
1296
Sascha Hauer71ec5152010-08-06 15:53:11 +02001297 addr_phases = fls(chip->pagemask) >> 3;
1298
1299 if (mtd->writesize == 2048) {
1300 config2 |= NFC_V3_CONFIG2_PS_2048;
1301 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
1302 } else if (mtd->writesize == 4096) {
1303 config2 |= NFC_V3_CONFIG2_PS_4096;
1304 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
1305 } else {
1306 config2 |= NFC_V3_CONFIG2_PS_512;
1307 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1);
1308 }
1309
1310 if (mtd->writesize) {
Uwe Kleine-König1f42adc2015-02-10 19:59:56 +01001311 if (chip->ecc.mode == NAND_ECC_HW)
1312 config2 |= NFC_V3_CONFIG2_ECC_EN;
1313
Sascha Hauer71718a8e2012-06-06 12:33:15 +02001314 config2 |= NFC_V3_CONFIG2_PPB(
1315 ffs(mtd->erasesize / mtd->writesize) - 6,
1316 host->devtype_data->ppb_shift);
Sascha Hauer71ec5152010-08-06 15:53:11 +02001317 host->eccsize = get_eccsize(mtd);
1318 if (host->eccsize == 8)
1319 config2 |= NFC_V3_CONFIG2_ECC_MODE_8;
1320 }
1321
1322 writel(config2, NFC_V3_CONFIG2);
1323
1324 config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
1325 NFC_V3_CONFIG3_NO_SDMA |
1326 NFC_V3_CONFIG3_RBB_MODE |
1327 NFC_V3_CONFIG3_SBB(6) | /* Reset default */
1328 NFC_V3_CONFIG3_ADD_OP(0);
1329
1330 if (!(chip->options & NAND_BUSWIDTH_16))
1331 config3 |= NFC_V3_CONFIG3_FW8;
1332
1333 writel(config3, NFC_V3_CONFIG3);
1334
1335 writel(0, NFC_V3_DELAY_LINE);
Sascha Hauera3e65b62009-06-02 11:47:59 +02001336}
Sascha Hauer34f6e152008-09-02 17:16:59 +02001337
Sascha Hauer34f6e152008-09-02 17:16:59 +02001338/* Used by the upper layer to write command to NAND Flash for
1339 * different operations to be carried out on NAND Flash */
1340static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
1341 int column, int page_addr)
1342{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001343 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001344 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001345
Sascha Hauer1f3df4d2017-10-16 11:51:55 +02001346 dev_dbg(host->dev, "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
Sascha Hauer34f6e152008-09-02 17:16:59 +02001347 command, column, page_addr);
1348
1349 /* Reset command state information */
1350 host->status_request = false;
1351
1352 /* Command pre-processing step */
Sascha Hauer34f6e152008-09-02 17:16:59 +02001353 switch (command) {
Ivo Claryssed4840182010-04-08 16:14:44 +02001354 case NAND_CMD_RESET:
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001355 host->devtype_data->preset(mtd);
1356 host->devtype_data->send_cmd(host, command, false);
Ivo Claryssed4840182010-04-08 16:14:44 +02001357 break;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001358
Sascha Hauer34f6e152008-09-02 17:16:59 +02001359 case NAND_CMD_STATUS:
Sascha Hauerf8f96082009-06-04 17:12:26 +02001360 host->buf_start = 0;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001361 host->status_request = true;
Sascha Hauer89121a62009-06-04 17:18:01 +02001362
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001363 host->devtype_data->send_cmd(host, command, true);
Uwe Kleine-Königc4ca3992015-02-10 19:59:58 +01001364 WARN_ONCE(column != -1 || page_addr != -1,
1365 "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n",
1366 command, column, page_addr);
Sascha Hauer89121a62009-06-04 17:18:01 +02001367 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001368 break;
1369
Sascha Hauer34f6e152008-09-02 17:16:59 +02001370 case NAND_CMD_READID:
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001371 host->devtype_data->send_cmd(host, command, true);
Sascha Hauer89121a62009-06-04 17:18:01 +02001372 mxc_do_addr_cycle(mtd, column, page_addr);
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001373 host->devtype_data->send_read_id(host);
Uwe Kleine-Königc4ca3992015-02-10 19:59:58 +01001374 host->buf_start = 0;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001375 break;
1376
Sascha Hauer89121a62009-06-04 17:18:01 +02001377 case NAND_CMD_ERASE1:
Sascha Hauer34f6e152008-09-02 17:16:59 +02001378 case NAND_CMD_ERASE2:
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001379 host->devtype_data->send_cmd(host, command, false);
Uwe Kleine-Königc4ca3992015-02-10 19:59:58 +01001380 WARN_ONCE(column != -1,
1381 "Unexpected column value (cmd=%u, col=%d)\n",
1382 command, column);
Sascha Hauer89121a62009-06-04 17:18:01 +02001383 mxc_do_addr_cycle(mtd, column, page_addr);
1384
Sascha Hauer34f6e152008-09-02 17:16:59 +02001385 break;
Uwe Kleine-König3d6e81c2015-02-10 19:59:59 +01001386 case NAND_CMD_PARAM:
1387 host->devtype_data->send_cmd(host, command, false);
1388 mxc_do_addr_cycle(mtd, column, page_addr);
1389 host->devtype_data->send_page(mtd, NFC_OUTPUT);
1390 memcpy32_fromio(host->data_buf, host->main_area0, 512);
1391 host->buf_start = 0;
1392 break;
Uwe Kleine-König98ebb522015-02-10 20:00:00 +01001393 default:
1394 WARN_ONCE(1, "Unimplemented command (cmd=%u)\n",
1395 command);
1396 break;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001397 }
1398}
1399
Miquel Raynalb9587582018-03-19 14:47:19 +01001400static int mxc_nand_set_features(struct mtd_info *mtd, struct nand_chip *chip,
1401 int addr, u8 *subfeature_param)
Sascha Hauer4123ea32016-09-15 10:32:52 +02001402{
1403 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1404 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
1405 int i;
1406
Sascha Hauer4123ea32016-09-15 10:32:52 +02001407 host->buf_start = 0;
1408
1409 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1410 chip->write_byte(mtd, subfeature_param[i]);
1411
1412 memcpy32_toio(host->main_area0, host->data_buf, mtd->writesize);
1413 host->devtype_data->send_cmd(host, NAND_CMD_SET_FEATURES, false);
1414 mxc_do_addr_cycle(mtd, addr, -1);
1415 host->devtype_data->send_page(mtd, NFC_INPUT);
1416
1417 return 0;
1418}
1419
Miquel Raynalb9587582018-03-19 14:47:19 +01001420static int mxc_nand_get_features(struct mtd_info *mtd, struct nand_chip *chip,
1421 int addr, u8 *subfeature_param)
Sascha Hauer4123ea32016-09-15 10:32:52 +02001422{
1423 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1424 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
1425 int i;
1426
Sascha Hauer4123ea32016-09-15 10:32:52 +02001427 host->devtype_data->send_cmd(host, NAND_CMD_GET_FEATURES, false);
1428 mxc_do_addr_cycle(mtd, addr, -1);
1429 host->devtype_data->send_page(mtd, NFC_OUTPUT);
1430 memcpy32_fromio(host->data_buf, host->main_area0, 512);
1431 host->buf_start = 0;
1432
1433 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1434 *subfeature_param++ = chip->read_byte(mtd);
1435
1436 return 0;
1437}
1438
Sascha Hauerf1372052009-10-21 14:25:27 +02001439/*
1440 * The generic flash bbt decriptors overlap with our ecc
1441 * hardware, so define some i.MX specific ones.
1442 */
1443static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
1444static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
1445
1446static struct nand_bbt_descr bbt_main_descr = {
1447 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1448 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1449 .offs = 0,
1450 .len = 4,
1451 .veroffs = 4,
1452 .maxblocks = 4,
1453 .pattern = bbt_pattern,
1454};
1455
1456static struct nand_bbt_descr bbt_mirror_descr = {
1457 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1458 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1459 .offs = 0,
1460 .len = 4,
1461 .veroffs = 4,
1462 .maxblocks = 4,
1463 .pattern = mirror_pattern,
1464};
1465
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001466/* v1 + irqpending_quirk: i.MX21 */
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001467static const struct mxc_nand_devtype_data imx21_nand_devtype_data = {
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001468 .preset = preset_v1,
Sascha Hauer5039fc92018-01-17 12:32:10 +01001469 .read_page = mxc_nand_read_page_v1,
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001470 .send_cmd = send_cmd_v1_v2,
1471 .send_addr = send_addr_v1_v2,
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001472 .send_page = send_page_v1,
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001473 .send_read_id = send_read_id_v1_v2,
1474 .get_dev_status = get_dev_status_v1_v2,
1475 .check_int = check_int_v1_v2,
1476 .irq_control = irq_control_v1_v2,
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001477 .get_ecc_status = get_ecc_status_v1,
Boris Brezillona894cf6c2016-02-03 20:02:54 +01001478 .ooblayout = &mxc_v1_ooblayout_ops,
Uwe Kleine-König5e05a2d62012-04-23 11:23:38 +02001479 .select_chip = mxc_nand_select_chip_v1_v3,
Sascha Hauer040bd3f2018-01-17 12:32:07 +01001480 .enable_hwecc = mxc_nand_enable_hwecc_v1_v2,
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001481 .irqpending_quirk = 1,
1482 .needs_ip = 0,
1483 .regs_offset = 0xe00,
1484 .spare0_offset = 0x800,
1485 .spare_len = 16,
1486 .eccbytes = 3,
1487 .eccsize = 1,
1488};
1489
1490/* v1 + !irqpending_quirk: i.MX27, i.MX31 */
1491static const struct mxc_nand_devtype_data imx27_nand_devtype_data = {
1492 .preset = preset_v1,
Sascha Hauer5039fc92018-01-17 12:32:10 +01001493 .read_page = mxc_nand_read_page_v1,
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001494 .send_cmd = send_cmd_v1_v2,
1495 .send_addr = send_addr_v1_v2,
1496 .send_page = send_page_v1,
1497 .send_read_id = send_read_id_v1_v2,
1498 .get_dev_status = get_dev_status_v1_v2,
1499 .check_int = check_int_v1_v2,
1500 .irq_control = irq_control_v1_v2,
1501 .get_ecc_status = get_ecc_status_v1,
Boris Brezillona894cf6c2016-02-03 20:02:54 +01001502 .ooblayout = &mxc_v1_ooblayout_ops,
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001503 .select_chip = mxc_nand_select_chip_v1_v3,
Sascha Hauer040bd3f2018-01-17 12:32:07 +01001504 .enable_hwecc = mxc_nand_enable_hwecc_v1_v2,
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001505 .irqpending_quirk = 0,
1506 .needs_ip = 0,
1507 .regs_offset = 0xe00,
1508 .spare0_offset = 0x800,
1509 .axi_offset = 0,
1510 .spare_len = 16,
1511 .eccbytes = 3,
1512 .eccsize = 1,
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001513};
1514
1515/* v21: i.MX25, i.MX35 */
1516static const struct mxc_nand_devtype_data imx25_nand_devtype_data = {
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001517 .preset = preset_v2,
Sascha Hauer67b87f62018-01-17 12:32:09 +01001518 .read_page = mxc_nand_read_page_v2_v3,
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001519 .send_cmd = send_cmd_v1_v2,
1520 .send_addr = send_addr_v1_v2,
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001521 .send_page = send_page_v2,
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001522 .send_read_id = send_read_id_v1_v2,
1523 .get_dev_status = get_dev_status_v1_v2,
1524 .check_int = check_int_v1_v2,
1525 .irq_control = irq_control_v1_v2,
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001526 .get_ecc_status = get_ecc_status_v2,
Boris Brezillona894cf6c2016-02-03 20:02:54 +01001527 .ooblayout = &mxc_v2_ooblayout_ops,
Uwe Kleine-König5e05a2d62012-04-23 11:23:38 +02001528 .select_chip = mxc_nand_select_chip_v2,
Sascha Hauer82830792016-09-15 10:32:53 +02001529 .setup_data_interface = mxc_nand_v2_setup_data_interface,
Sascha Hauer040bd3f2018-01-17 12:32:07 +01001530 .enable_hwecc = mxc_nand_enable_hwecc_v1_v2,
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001531 .irqpending_quirk = 0,
1532 .needs_ip = 0,
1533 .regs_offset = 0x1e00,
1534 .spare0_offset = 0x1000,
1535 .axi_offset = 0,
1536 .spare_len = 64,
1537 .eccbytes = 9,
1538 .eccsize = 0,
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001539};
1540
Sascha Hauer71718a8e2012-06-06 12:33:15 +02001541/* v3.2a: i.MX51 */
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001542static const struct mxc_nand_devtype_data imx51_nand_devtype_data = {
1543 .preset = preset_v3,
Sascha Hauer67b87f62018-01-17 12:32:09 +01001544 .read_page = mxc_nand_read_page_v2_v3,
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001545 .send_cmd = send_cmd_v3,
1546 .send_addr = send_addr_v3,
1547 .send_page = send_page_v3,
1548 .send_read_id = send_read_id_v3,
1549 .get_dev_status = get_dev_status_v3,
1550 .check_int = check_int_v3,
1551 .irq_control = irq_control_v3,
Uwe Kleine-König6d38af22012-04-23 11:23:36 +02001552 .get_ecc_status = get_ecc_status_v3,
Boris Brezillona894cf6c2016-02-03 20:02:54 +01001553 .ooblayout = &mxc_v2_ooblayout_ops,
Uwe Kleine-König5e05a2d62012-04-23 11:23:38 +02001554 .select_chip = mxc_nand_select_chip_v1_v3,
Sascha Hauer040bd3f2018-01-17 12:32:07 +01001555 .enable_hwecc = mxc_nand_enable_hwecc_v3,
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001556 .irqpending_quirk = 0,
1557 .needs_ip = 1,
1558 .regs_offset = 0,
1559 .spare0_offset = 0x1000,
1560 .axi_offset = 0x1e00,
1561 .spare_len = 64,
1562 .eccbytes = 0,
1563 .eccsize = 0,
Sascha Hauer71718a8e2012-06-06 12:33:15 +02001564 .ppb_shift = 7,
1565};
1566
1567/* v3.2b: i.MX53 */
1568static const struct mxc_nand_devtype_data imx53_nand_devtype_data = {
1569 .preset = preset_v3,
Sascha Hauer67b87f62018-01-17 12:32:09 +01001570 .read_page = mxc_nand_read_page_v2_v3,
Sascha Hauer71718a8e2012-06-06 12:33:15 +02001571 .send_cmd = send_cmd_v3,
1572 .send_addr = send_addr_v3,
1573 .send_page = send_page_v3,
1574 .send_read_id = send_read_id_v3,
1575 .get_dev_status = get_dev_status_v3,
1576 .check_int = check_int_v3,
1577 .irq_control = irq_control_v3,
1578 .get_ecc_status = get_ecc_status_v3,
Boris Brezillona894cf6c2016-02-03 20:02:54 +01001579 .ooblayout = &mxc_v2_ooblayout_ops,
Sascha Hauer71718a8e2012-06-06 12:33:15 +02001580 .select_chip = mxc_nand_select_chip_v1_v3,
Sascha Hauer040bd3f2018-01-17 12:32:07 +01001581 .enable_hwecc = mxc_nand_enable_hwecc_v3,
Sascha Hauer71718a8e2012-06-06 12:33:15 +02001582 .irqpending_quirk = 0,
1583 .needs_ip = 1,
1584 .regs_offset = 0,
1585 .spare0_offset = 0x1000,
1586 .axi_offset = 0x1e00,
1587 .spare_len = 64,
1588 .eccbytes = 0,
1589 .eccsize = 0,
1590 .ppb_shift = 8,
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001591};
1592
Shawn Guo4d624352012-09-15 13:34:09 +08001593static inline int is_imx21_nfc(struct mxc_nand_host *host)
1594{
1595 return host->devtype_data == &imx21_nand_devtype_data;
1596}
1597
1598static inline int is_imx27_nfc(struct mxc_nand_host *host)
1599{
1600 return host->devtype_data == &imx27_nand_devtype_data;
1601}
1602
1603static inline int is_imx25_nfc(struct mxc_nand_host *host)
1604{
1605 return host->devtype_data == &imx25_nand_devtype_data;
1606}
1607
1608static inline int is_imx51_nfc(struct mxc_nand_host *host)
1609{
1610 return host->devtype_data == &imx51_nand_devtype_data;
1611}
1612
1613static inline int is_imx53_nfc(struct mxc_nand_host *host)
1614{
1615 return host->devtype_data == &imx53_nand_devtype_data;
1616}
1617
Krzysztof Kozlowski8d1e5682015-05-02 00:50:01 +09001618static const struct platform_device_id mxcnd_devtype[] = {
Shawn Guo4d624352012-09-15 13:34:09 +08001619 {
1620 .name = "imx21-nand",
1621 .driver_data = (kernel_ulong_t) &imx21_nand_devtype_data,
1622 }, {
1623 .name = "imx27-nand",
1624 .driver_data = (kernel_ulong_t) &imx27_nand_devtype_data,
1625 }, {
1626 .name = "imx25-nand",
1627 .driver_data = (kernel_ulong_t) &imx25_nand_devtype_data,
1628 }, {
1629 .name = "imx51-nand",
1630 .driver_data = (kernel_ulong_t) &imx51_nand_devtype_data,
1631 }, {
1632 .name = "imx53-nand",
1633 .driver_data = (kernel_ulong_t) &imx53_nand_devtype_data,
1634 }, {
1635 /* sentinel */
1636 }
1637};
1638MODULE_DEVICE_TABLE(platform, mxcnd_devtype);
1639
Boris Brezillonba52b4d2016-09-17 19:44:43 +02001640#ifdef CONFIG_OF
Uwe Kleine-König64363562012-04-23 11:23:41 +02001641static const struct of_device_id mxcnd_dt_ids[] = {
1642 {
1643 .compatible = "fsl,imx21-nand",
1644 .data = &imx21_nand_devtype_data,
1645 }, {
1646 .compatible = "fsl,imx27-nand",
1647 .data = &imx27_nand_devtype_data,
1648 }, {
1649 .compatible = "fsl,imx25-nand",
1650 .data = &imx25_nand_devtype_data,
1651 }, {
1652 .compatible = "fsl,imx51-nand",
1653 .data = &imx51_nand_devtype_data,
Sascha Hauer71718a8e2012-06-06 12:33:15 +02001654 }, {
1655 .compatible = "fsl,imx53-nand",
1656 .data = &imx53_nand_devtype_data,
Uwe Kleine-König64363562012-04-23 11:23:41 +02001657 },
1658 { /* sentinel */ }
1659};
Luis de Bethencourtb33c35b2015-09-18 00:13:28 +02001660MODULE_DEVICE_TABLE(of, mxcnd_dt_ids);
Uwe Kleine-König64363562012-04-23 11:23:41 +02001661
Martin Kaiser24f0ae92018-06-27 22:47:44 +02001662static int mxcnd_probe_dt(struct mxc_nand_host *host)
Uwe Kleine-König64363562012-04-23 11:23:41 +02001663{
1664 struct device_node *np = host->dev->of_node;
Uwe Kleine-König64363562012-04-23 11:23:41 +02001665 const struct of_device_id *of_id =
1666 of_match_device(mxcnd_dt_ids, host->dev);
Uwe Kleine-König64363562012-04-23 11:23:41 +02001667
1668 if (!np)
1669 return 1;
1670
Uwe Kleine-König64363562012-04-23 11:23:41 +02001671 host->devtype_data = of_id->data;
1672
1673 return 0;
1674}
1675#else
Martin Kaiser24f0ae92018-06-27 22:47:44 +02001676static int mxcnd_probe_dt(struct mxc_nand_host *host)
Uwe Kleine-König64363562012-04-23 11:23:41 +02001677{
1678 return 1;
1679}
1680#endif
1681
Miquel Raynal96fa8e62018-07-20 17:15:07 +02001682static int mxcnd_attach_chip(struct nand_chip *chip)
1683{
1684 struct mtd_info *mtd = nand_to_mtd(chip);
1685 struct mxc_nand_host *host = nand_get_controller_data(chip);
1686 struct device *dev = mtd->dev.parent;
1687
1688 switch (chip->ecc.mode) {
1689 case NAND_ECC_HW:
1690 chip->ecc.read_page = mxc_nand_read_page;
1691 chip->ecc.read_page_raw = mxc_nand_read_page_raw;
1692 chip->ecc.read_oob = mxc_nand_read_oob;
1693 chip->ecc.write_page = mxc_nand_write_page_ecc;
1694 chip->ecc.write_page_raw = mxc_nand_write_page_raw;
1695 chip->ecc.write_oob = mxc_nand_write_oob;
1696 break;
1697
1698 case NAND_ECC_SOFT:
1699 break;
1700
1701 default:
1702 return -EINVAL;
1703 }
1704
1705 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
1706 chip->bbt_td = &bbt_main_descr;
1707 chip->bbt_md = &bbt_mirror_descr;
1708 }
1709
1710 /* Allocate the right size buffer now */
1711 devm_kfree(dev, (void *)host->data_buf);
1712 host->data_buf = devm_kzalloc(dev, mtd->writesize + mtd->oobsize,
1713 GFP_KERNEL);
1714 if (!host->data_buf)
1715 return -ENOMEM;
1716
1717 /* Call preset again, with correct writesize chip time */
1718 host->devtype_data->preset(mtd);
1719
1720 if (!chip->ecc.bytes) {
1721 if (host->eccsize == 8)
1722 chip->ecc.bytes = 18;
1723 else if (host->eccsize == 4)
1724 chip->ecc.bytes = 9;
1725 }
1726
1727 /*
1728 * Experimentation shows that i.MX NFC can only handle up to 218 oob
1729 * bytes. Limit used_oobsize to 218 so as to not confuse copy_spare()
1730 * into copying invalid data to/from the spare IO buffer, as this
1731 * might cause ECC data corruption when doing sub-page write to a
1732 * partially written page.
1733 */
1734 host->used_oobsize = min(mtd->oobsize, 218U);
1735
1736 if (chip->ecc.mode == NAND_ECC_HW) {
1737 if (is_imx21_nfc(host) || is_imx27_nfc(host))
1738 chip->ecc.strength = 1;
1739 else
1740 chip->ecc.strength = (host->eccsize == 4) ? 4 : 8;
1741 }
1742
1743 return 0;
1744}
1745
1746static const struct nand_controller_ops mxcnd_controller_ops = {
1747 .attach_chip = mxcnd_attach_chip,
1748};
1749
Bill Pemberton06f25512012-11-19 13:23:07 -05001750static int mxcnd_probe(struct platform_device *pdev)
Sascha Hauer34f6e152008-09-02 17:16:59 +02001751{
1752 struct nand_chip *this;
1753 struct mtd_info *mtd;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001754 struct mxc_nand_host *host;
1755 struct resource *res;
Dmitry Eremin-Solenikovd4ed8f12011-06-02 18:00:43 +04001756 int err = 0;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001757
1758 /* Allocate memory for MTD device structure and private data */
Huang Shijiea5900552013-12-21 00:02:27 +08001759 host = devm_kzalloc(&pdev->dev, sizeof(struct mxc_nand_host),
1760 GFP_KERNEL);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001761 if (!host)
1762 return -ENOMEM;
1763
Huang Shijiea5900552013-12-21 00:02:27 +08001764 /* allocate a temporary buffer for the nand_scan_ident() */
1765 host->data_buf = devm_kzalloc(&pdev->dev, PAGE_SIZE, GFP_KERNEL);
1766 if (!host->data_buf)
1767 return -ENOMEM;
Sascha Hauerf8f96082009-06-04 17:12:26 +02001768
Sascha Hauer34f6e152008-09-02 17:16:59 +02001769 host->dev = &pdev->dev;
1770 /* structures must be linked */
1771 this = &host->nand;
Boris BREZILLONa008deb2015-12-10 09:00:12 +01001772 mtd = nand_to_mtd(this);
David Brownell87f39f02009-03-26 00:42:50 -07001773 mtd->dev.parent = &pdev->dev;
Sascha Hauer1fbff0a2009-10-21 16:06:27 +02001774 mtd->name = DRIVER_NAME;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001775
1776 /* 50 us command delay time */
1777 this->chip_delay = 5;
1778
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001779 nand_set_controller_data(this, host);
Brian Norrisa61ae812015-10-30 20:33:25 -07001780 nand_set_flash_node(this, pdev->dev.of_node),
Sascha Hauer34f6e152008-09-02 17:16:59 +02001781 this->dev_ready = mxc_nand_dev_ready;
1782 this->cmdfunc = mxc_nand_command;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001783 this->read_byte = mxc_nand_read_byte;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001784 this->write_buf = mxc_nand_write_buf;
1785 this->read_buf = mxc_nand_read_buf;
Miquel Raynalb9587582018-03-19 14:47:19 +01001786 this->set_features = mxc_nand_set_features;
1787 this->get_features = mxc_nand_get_features;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001788
Fabio Estevam24b82d32012-09-05 11:52:27 -03001789 host->clk = devm_clk_get(&pdev->dev, NULL);
Sascha Hauere4a09cb2012-06-06 12:33:13 +02001790 if (IS_ERR(host->clk))
1791 return PTR_ERR(host->clk);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001792
Sascha Hauer71885b62012-06-06 12:33:14 +02001793 err = mxcnd_probe_dt(host);
Shawn Guo4d624352012-09-15 13:34:09 +08001794 if (err > 0) {
Jingoo Han453810b2013-07-30 17:18:33 +09001795 struct mxc_nand_platform_data *pdata =
1796 dev_get_platdata(&pdev->dev);
Shawn Guo4d624352012-09-15 13:34:09 +08001797 if (pdata) {
1798 host->pdata = *pdata;
1799 host->devtype_data = (struct mxc_nand_devtype_data *)
1800 pdev->id_entry->driver_data;
1801 } else {
1802 err = -ENODEV;
1803 }
1804 }
Sascha Hauer71885b62012-06-06 12:33:14 +02001805 if (err < 0)
1806 return err;
1807
Sascha Hauer82830792016-09-15 10:32:53 +02001808 this->setup_data_interface = host->devtype_data->setup_data_interface;
1809
Sascha Hauer71885b62012-06-06 12:33:14 +02001810 if (host->devtype_data->needs_ip) {
1811 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingb0de7742013-01-21 11:09:12 +01001812 host->regs_ip = devm_ioremap_resource(&pdev->dev, res);
1813 if (IS_ERR(host->regs_ip))
1814 return PTR_ERR(host->regs_ip);
Sascha Hauer71885b62012-06-06 12:33:14 +02001815
1816 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1817 } else {
1818 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1819 }
1820
Thierry Redingb0de7742013-01-21 11:09:12 +01001821 host->base = devm_ioremap_resource(&pdev->dev, res);
1822 if (IS_ERR(host->base))
1823 return PTR_ERR(host->base);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001824
Sascha Hauerc6de7e12009-10-05 11:14:35 +02001825 host->main_area0 = host->base;
Sascha Hauer94671142009-10-05 12:14:21 +02001826
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001827 if (host->devtype_data->regs_offset)
1828 host->regs = host->base + host->devtype_data->regs_offset;
1829 host->spare0 = host->base + host->devtype_data->spare0_offset;
1830 if (host->devtype_data->axi_offset)
1831 host->regs_axi = host->base + host->devtype_data->axi_offset;
1832
1833 this->ecc.bytes = host->devtype_data->eccbytes;
1834 host->eccsize = host->devtype_data->eccsize;
1835
1836 this->select_chip = host->devtype_data->select_chip;
1837 this->ecc.size = 512;
Boris Brezillona894cf6c2016-02-03 20:02:54 +01001838 mtd_set_ooblayout(mtd, host->devtype_data->ooblayout);
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001839
Uwe Kleine-König64363562012-04-23 11:23:41 +02001840 if (host->pdata.hw_ecc) {
Sascha Hauer13e1add2009-10-21 10:39:05 +02001841 this->ecc.mode = NAND_ECC_HW;
Sascha Hauer13e1add2009-10-21 10:39:05 +02001842 } else {
1843 this->ecc.mode = NAND_ECC_SOFT;
Rafał Miłeckic1c70402016-04-08 12:23:46 +02001844 this->ecc.algo = NAND_ECC_HAMMING;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001845 }
1846
Uwe Kleine-König64363562012-04-23 11:23:41 +02001847 /* NAND bus width determines access functions used by upper layer */
1848 if (host->pdata.width == 2)
Sascha Hauer34f6e152008-09-02 17:16:59 +02001849 this->options |= NAND_BUSWIDTH_16;
Sascha Hauer13e1add2009-10-21 10:39:05 +02001850
Boris Brezillon609468f2016-04-01 14:54:29 +02001851 /* update flash based bbt */
1852 if (host->pdata.flash_bbt)
Brian Norrisbb9ebd4e2011-05-31 16:31:23 -07001853 this->bbt_options |= NAND_BBT_USE_FLASH;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001854
Sascha Hauer63f14742010-10-18 10:16:26 +02001855 init_completion(&host->op_completion);
Ivo Claryssed4840182010-04-08 16:14:44 +02001856
1857 host->irq = platform_get_irq(pdev, 0);
Fabio Estevam26fbf482014-02-14 01:09:34 -02001858 if (host->irq < 0)
1859 return host->irq;
Ivo Claryssed4840182010-04-08 16:14:44 +02001860
Sascha Hauer63f14742010-10-18 10:16:26 +02001861 /*
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001862 * Use host->devtype_data->irq_control() here instead of irq_control()
1863 * because we must not disable_irq_nosync without having requested the
1864 * irq.
Sascha Hauer63f14742010-10-18 10:16:26 +02001865 */
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001866 host->devtype_data->irq_control(host, 0);
Sascha Hauer63f14742010-10-18 10:16:26 +02001867
Sascha Hauere4a09cb2012-06-06 12:33:13 +02001868 err = devm_request_irq(&pdev->dev, host->irq, mxc_nfc_irq,
Michael Opdenackerb1eb2342013-10-13 08:21:32 +02001869 0, DRIVER_NAME, host);
Ivo Claryssed4840182010-04-08 16:14:44 +02001870 if (err)
Sascha Hauere4a09cb2012-06-06 12:33:13 +02001871 return err;
1872
Fabio Estevamdcedf622013-12-02 00:50:02 -02001873 err = clk_prepare_enable(host->clk);
1874 if (err)
1875 return err;
Sascha Hauere4a09cb2012-06-06 12:33:13 +02001876 host->clk_act = 1;
Ivo Claryssed4840182010-04-08 16:14:44 +02001877
Sascha Hauer63f14742010-10-18 10:16:26 +02001878 /*
Uwe Kleine-König85569582012-04-23 11:23:34 +02001879 * Now that we "own" the interrupt make sure the interrupt mask bit is
1880 * cleared on i.MX21. Otherwise we can't read the interrupt status bit
1881 * on this machine.
Sascha Hauer63f14742010-10-18 10:16:26 +02001882 */
Uwe Kleine-Königf48d0f92012-04-23 11:23:40 +02001883 if (host->devtype_data->irqpending_quirk) {
Uwe Kleine-König85569582012-04-23 11:23:34 +02001884 disable_irq_nosync(host->irq);
Uwe Kleine-Könige4303b22012-04-23 11:23:35 +02001885 host->devtype_data->irq_control(host, 1);
Uwe Kleine-König85569582012-04-23 11:23:34 +02001886 }
Sascha Hauer63f14742010-10-18 10:16:26 +02001887
Miquel Raynal96fa8e62018-07-20 17:15:07 +02001888 /* Scan the NAND device */
1889 this->dummy_controller.ops = &mxcnd_controller_ops;
Boris Brezillon00ad3782018-09-06 14:05:14 +02001890 err = nand_scan(this, is_imx25_nfc(host) ? 4 : 1);
Masahiro Yamadabc83c782016-11-04 19:43:03 +09001891 if (err)
Sascha Hauer4a43faf2012-05-25 16:22:42 +02001892 goto escan;
Sascha Hauer4a43faf2012-05-25 16:22:42 +02001893
Sascha Hauer34f6e152008-09-02 17:16:59 +02001894 /* Register the partitions */
Miquel Raynalbe051bf2018-03-21 14:01:50 +01001895 err = mtd_device_parse_register(mtd, part_probes, NULL,
1896 host->pdata.parts,
1897 host->pdata.nr_parts);
1898 if (err)
1899 goto cleanup_nand;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001900
1901 platform_set_drvdata(pdev, host);
1902
1903 return 0;
1904
Miquel Raynalbe051bf2018-03-21 14:01:50 +01001905cleanup_nand:
1906 nand_cleanup(this);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001907escan:
Lothar Waßmannc10d8ee2012-12-06 08:42:27 +01001908 if (host->clk_act)
1909 clk_disable_unprepare(host->clk);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001910
1911 return err;
1912}
1913
Bill Pemberton810b7e02012-11-19 13:26:04 -05001914static int mxcnd_remove(struct platform_device *pdev)
Sascha Hauer34f6e152008-09-02 17:16:59 +02001915{
1916 struct mxc_nand_host *host = platform_get_drvdata(pdev);
1917
Boris BREZILLONa008deb2015-12-10 09:00:12 +01001918 nand_release(nand_to_mtd(&host->nand));
Wei Yongjun8bfd4f72013-12-17 11:35:35 +08001919 if (host->clk_act)
1920 clk_disable_unprepare(host->clk);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001921
1922 return 0;
1923}
1924
Sascha Hauer34f6e152008-09-02 17:16:59 +02001925static struct platform_driver mxcnd_driver = {
1926 .driver = {
1927 .name = DRIVER_NAME,
Uwe Kleine-König64363562012-04-23 11:23:41 +02001928 .of_match_table = of_match_ptr(mxcnd_dt_ids),
Eric Bénard04dd0d32010-06-17 20:59:04 +02001929 },
Shawn Guo4d624352012-09-15 13:34:09 +08001930 .id_table = mxcnd_devtype,
Fabio Estevamddf16d62012-09-05 11:35:25 -03001931 .probe = mxcnd_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -05001932 .remove = mxcnd_remove,
Sascha Hauer34f6e152008-09-02 17:16:59 +02001933};
Fabio Estevamddf16d62012-09-05 11:35:25 -03001934module_platform_driver(mxcnd_driver);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001935
1936MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1937MODULE_DESCRIPTION("MXC NAND MTD driver");
1938MODULE_LICENSE("GPL");