blob: 0d6e0c754318cc534a247e04eac3ba46cc252554 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stefan Roese75659da2015-07-23 10:26:16 +02002/*
3 * drivers/mtd/nand/pxa3xx_nand.c
4 *
5 * Copyright © 2005 Intel Corporation
6 * Copyright © 2006 Marvell International Ltd.
Stefan Roese75659da2015-07-23 10:26:16 +02007 */
8
9#include <common.h>
10#include <malloc.h>
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +030011#include <fdtdec.h>
Stefan Roese75659da2015-07-23 10:26:16 +020012#include <nand.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090013#include <linux/errno.h>
Stefan Roese75659da2015-07-23 10:26:16 +020014#include <asm/io.h>
15#include <asm/arch/cpu.h>
16#include <linux/mtd/mtd.h>
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090017#include <linux/mtd/rawnand.h>
Stefan Roese75659da2015-07-23 10:26:16 +020018#include <linux/types.h>
19
20#include "pxa3xx_nand.h"
21
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +030022DECLARE_GLOBAL_DATA_PTR;
23
Stefan Roese75659da2015-07-23 10:26:16 +020024#define TIMEOUT_DRAIN_FIFO 5 /* in ms */
25#define CHIP_DELAY_TIMEOUT 200
26#define NAND_STOP_DELAY 40
27#define PAGE_CHUNK_SIZE (2048)
28
29/*
30 * Define a buffer size for the initial command that detects the flash device:
Ofer Heifetzfdf5b232018-08-29 11:56:00 +030031 * STATUS, READID and PARAM.
32 * ONFI param page is 256 bytes, and there are three redundant copies
33 * to be read. JEDEC param page is 512 bytes, and there are also three
34 * redundant copies to be read.
35 * Hence this buffer should be at least 512 x 3. Let's pick 2048.
Stefan Roese75659da2015-07-23 10:26:16 +020036 */
Ofer Heifetzfdf5b232018-08-29 11:56:00 +030037#define INIT_BUFFER_SIZE 2048
Stefan Roese75659da2015-07-23 10:26:16 +020038
39/* registers and bit definitions */
40#define NDCR (0x00) /* Control register */
41#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
42#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
43#define NDSR (0x14) /* Status Register */
44#define NDPCR (0x18) /* Page Count Register */
45#define NDBDR0 (0x1C) /* Bad Block Register 0 */
46#define NDBDR1 (0x20) /* Bad Block Register 1 */
47#define NDECCCTRL (0x28) /* ECC control */
48#define NDDB (0x40) /* Data Buffer */
49#define NDCB0 (0x48) /* Command Buffer0 */
50#define NDCB1 (0x4C) /* Command Buffer1 */
51#define NDCB2 (0x50) /* Command Buffer2 */
52
53#define NDCR_SPARE_EN (0x1 << 31)
54#define NDCR_ECC_EN (0x1 << 30)
55#define NDCR_DMA_EN (0x1 << 29)
56#define NDCR_ND_RUN (0x1 << 28)
57#define NDCR_DWIDTH_C (0x1 << 27)
58#define NDCR_DWIDTH_M (0x1 << 26)
59#define NDCR_PAGE_SZ (0x1 << 24)
60#define NDCR_NCSX (0x1 << 23)
61#define NDCR_ND_MODE (0x3 << 21)
62#define NDCR_NAND_MODE (0x0)
63#define NDCR_CLR_PG_CNT (0x1 << 20)
Ofer Heifetz531816e2018-08-29 11:56:07 +030064#define NFCV1_NDCR_ARB_CNTL (0x1 << 19)
65#define NFCV2_NDCR_STOP_ON_UNCOR (0x1 << 19)
Stefan Roese75659da2015-07-23 10:26:16 +020066#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
67#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
68
69#define NDCR_RA_START (0x1 << 15)
70#define NDCR_PG_PER_BLK (0x1 << 14)
71#define NDCR_ND_ARB_EN (0x1 << 12)
72#define NDCR_INT_MASK (0xFFF)
73
74#define NDSR_MASK (0xfff)
75#define NDSR_ERR_CNT_OFF (16)
76#define NDSR_ERR_CNT_MASK (0x1f)
77#define NDSR_ERR_CNT(sr) ((sr >> NDSR_ERR_CNT_OFF) & NDSR_ERR_CNT_MASK)
78#define NDSR_RDY (0x1 << 12)
79#define NDSR_FLASH_RDY (0x1 << 11)
80#define NDSR_CS0_PAGED (0x1 << 10)
81#define NDSR_CS1_PAGED (0x1 << 9)
82#define NDSR_CS0_CMDD (0x1 << 8)
83#define NDSR_CS1_CMDD (0x1 << 7)
84#define NDSR_CS0_BBD (0x1 << 6)
85#define NDSR_CS1_BBD (0x1 << 5)
86#define NDSR_UNCORERR (0x1 << 4)
87#define NDSR_CORERR (0x1 << 3)
88#define NDSR_WRDREQ (0x1 << 2)
89#define NDSR_RDDREQ (0x1 << 1)
90#define NDSR_WRCMDREQ (0x1)
91
92#define NDCB0_LEN_OVRD (0x1 << 28)
93#define NDCB0_ST_ROW_EN (0x1 << 26)
94#define NDCB0_AUTO_RS (0x1 << 25)
95#define NDCB0_CSEL (0x1 << 24)
96#define NDCB0_EXT_CMD_TYPE_MASK (0x7 << 29)
97#define NDCB0_EXT_CMD_TYPE(x) (((x) << 29) & NDCB0_EXT_CMD_TYPE_MASK)
98#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
99#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
100#define NDCB0_NC (0x1 << 20)
101#define NDCB0_DBC (0x1 << 19)
102#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
103#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
104#define NDCB0_CMD2_MASK (0xff << 8)
105#define NDCB0_CMD1_MASK (0xff)
106#define NDCB0_ADDR_CYC_SHIFT (16)
107
108#define EXT_CMD_TYPE_DISPATCH 6 /* Command dispatch */
109#define EXT_CMD_TYPE_NAKED_RW 5 /* Naked read or Naked write */
110#define EXT_CMD_TYPE_READ 4 /* Read */
111#define EXT_CMD_TYPE_DISP_WR 4 /* Command dispatch with write */
112#define EXT_CMD_TYPE_FINAL 3 /* Final command */
113#define EXT_CMD_TYPE_LAST_RW 1 /* Last naked read/write */
114#define EXT_CMD_TYPE_MONO 0 /* Monolithic read/write */
115
Ofer Heifetz4a574aa2018-08-29 11:56:05 +0300116/*
117 * This should be large enough to read 'ONFI' and 'JEDEC'.
118 * Let's use 7 bytes, which is the maximum ID count supported
119 * by the controller (see NDCR_RD_ID_CNT_MASK).
120 */
121#define READ_ID_BYTES 7
122
Stefan Roese75659da2015-07-23 10:26:16 +0200123/* macros for registers read/write */
124#define nand_writel(info, off, val) \
125 writel((val), (info)->mmio_base + (off))
126
127#define nand_readl(info, off) \
128 readl((info)->mmio_base + (off))
129
130/* error code and state */
131enum {
132 ERR_NONE = 0,
133 ERR_DMABUSERR = -1,
134 ERR_SENDCMD = -2,
135 ERR_UNCORERR = -3,
136 ERR_BBERR = -4,
137 ERR_CORERR = -5,
138};
139
140enum {
141 STATE_IDLE = 0,
142 STATE_PREPARED,
143 STATE_CMD_HANDLE,
144 STATE_DMA_READING,
145 STATE_DMA_WRITING,
146 STATE_DMA_DONE,
147 STATE_PIO_READING,
148 STATE_PIO_WRITING,
149 STATE_CMD_DONE,
150 STATE_READY,
151};
152
153enum pxa3xx_nand_variant {
154 PXA3XX_NAND_VARIANT_PXA,
155 PXA3XX_NAND_VARIANT_ARMADA370,
156};
157
158struct pxa3xx_nand_host {
159 struct nand_chip chip;
Stefan Roese75659da2015-07-23 10:26:16 +0200160 void *info_data;
161
162 /* page size of attached chip */
163 int use_ecc;
164 int cs;
165
166 /* calculated from pxa3xx_nand_flash data */
167 unsigned int col_addr_cycles;
168 unsigned int row_addr_cycles;
Stefan Roese75659da2015-07-23 10:26:16 +0200169};
170
171struct pxa3xx_nand_info {
172 struct nand_hw_control controller;
173 struct pxa3xx_nand_platform_data *pdata;
174
175 struct clk *clk;
176 void __iomem *mmio_base;
177 unsigned long mmio_phys;
178 int cmd_complete, dev_ready;
179
180 unsigned int buf_start;
181 unsigned int buf_count;
182 unsigned int buf_size;
183 unsigned int data_buff_pos;
184 unsigned int oob_buff_pos;
185
186 unsigned char *data_buff;
187 unsigned char *oob_buff;
188
189 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
190 unsigned int state;
191
192 /*
193 * This driver supports NFCv1 (as found in PXA SoC)
194 * and NFCv2 (as found in Armada 370/XP SoC).
195 */
196 enum pxa3xx_nand_variant variant;
197
198 int cs;
199 int use_ecc; /* use HW ECC ? */
200 int ecc_bch; /* using BCH ECC? */
201 int use_spare; /* use spare ? */
202 int need_wait;
203
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300204 /* Amount of real data per full chunk */
205 unsigned int chunk_size;
206
207 /* Amount of spare data per full chunk */
Stefan Roese75659da2015-07-23 10:26:16 +0200208 unsigned int spare_size;
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300209
210 /* Number of full chunks (i.e chunk_size + spare_size) */
211 unsigned int nfullchunks;
212
213 /*
214 * Total number of chunks. If equal to nfullchunks, then there
215 * are only full chunks. Otherwise, there is one last chunk of
216 * size (last_chunk_size + last_spare_size)
217 */
218 unsigned int ntotalchunks;
219
220 /* Amount of real data in the last chunk */
221 unsigned int last_chunk_size;
222
223 /* Amount of spare data in the last chunk */
224 unsigned int last_spare_size;
225
Stefan Roese75659da2015-07-23 10:26:16 +0200226 unsigned int ecc_size;
227 unsigned int ecc_err_cnt;
228 unsigned int max_bitflips;
229 int retcode;
230
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300231 /*
232 * Variables only valid during command
233 * execution. step_chunk_size and step_spare_size is the
234 * amount of real data and spare data in the current
235 * chunk. cur_chunk is the current chunk being
236 * read/programmed.
237 */
238 unsigned int step_chunk_size;
239 unsigned int step_spare_size;
240 unsigned int cur_chunk;
241
Stefan Roese75659da2015-07-23 10:26:16 +0200242 /* cached register value */
243 uint32_t reg_ndcr;
244 uint32_t ndtr0cs0;
245 uint32_t ndtr1cs0;
246
247 /* generated NDCBx register values */
248 uint32_t ndcb0;
249 uint32_t ndcb1;
250 uint32_t ndcb2;
251 uint32_t ndcb3;
252};
253
254static struct pxa3xx_nand_timing timing[] = {
Konstantin Porotchkin029be942018-08-29 11:56:14 +0300255 /*ch cs wh wp rh rp r whr ar */
Stefan Roese75659da2015-07-23 10:26:16 +0200256 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
257 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
258 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
259 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
Konstantin Porotchkin029be942018-08-29 11:56:14 +0300260 { 5, 20, 10, 12, 10, 12, 25000, 60, 10, },
Stefan Roese75659da2015-07-23 10:26:16 +0200261};
262
263static struct pxa3xx_nand_flash builtin_flash_types[] = {
264 { 0x46ec, 16, 16, &timing[1] },
265 { 0xdaec, 8, 8, &timing[1] },
266 { 0xd7ec, 8, 8, &timing[1] },
267 { 0xa12c, 8, 8, &timing[2] },
268 { 0xb12c, 16, 16, &timing[2] },
269 { 0xdc2c, 8, 8, &timing[2] },
270 { 0xcc2c, 16, 16, &timing[2] },
271 { 0xba20, 16, 16, &timing[3] },
Konstantin Porotchkin029be942018-08-29 11:56:14 +0300272 { 0xda98, 8, 8, &timing[4] },
Stefan Roese75659da2015-07-23 10:26:16 +0200273};
274
Sean Nyekjaera12a8e82017-11-22 13:39:08 +0100275#ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
Stefan Roese75659da2015-07-23 10:26:16 +0200276static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
277static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
278
279static struct nand_bbt_descr bbt_main_descr = {
280 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
281 | NAND_BBT_2BIT | NAND_BBT_VERSION,
282 .offs = 8,
283 .len = 6,
284 .veroffs = 14,
285 .maxblocks = 8, /* Last 8 blocks in each chip */
286 .pattern = bbt_pattern
287};
288
289static struct nand_bbt_descr bbt_mirror_descr = {
290 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
291 | NAND_BBT_2BIT | NAND_BBT_VERSION,
292 .offs = 8,
293 .len = 6,
294 .veroffs = 14,
295 .maxblocks = 8, /* Last 8 blocks in each chip */
296 .pattern = bbt_mirror_pattern
297};
Sean Nyekjaera12a8e82017-11-22 13:39:08 +0100298#endif
Stefan Roese75659da2015-07-23 10:26:16 +0200299
300static struct nand_ecclayout ecc_layout_2KB_bch4bit = {
301 .eccbytes = 32,
302 .eccpos = {
303 32, 33, 34, 35, 36, 37, 38, 39,
304 40, 41, 42, 43, 44, 45, 46, 47,
305 48, 49, 50, 51, 52, 53, 54, 55,
306 56, 57, 58, 59, 60, 61, 62, 63},
307 .oobfree = { {2, 30} }
308};
309
Victor Axelrodfdf9dfb2018-08-29 11:56:13 +0300310static struct nand_ecclayout ecc_layout_2KB_bch8bit = {
311 .eccbytes = 64,
312 .eccpos = {
313 64, 65, 66, 67, 68, 69, 70, 71,
314 72, 73, 74, 75, 76, 77, 78, 79,
315 80, 81, 82, 83, 84, 85, 86, 87,
316 88, 89, 90, 91, 92, 93, 94, 95,
317 96, 97, 98, 99, 100, 101, 102, 103,
318 104, 105, 106, 107, 108, 109, 110, 111,
319 112, 113, 114, 115, 116, 117, 118, 119,
320 120, 121, 122, 123, 124, 125, 126, 127},
321 .oobfree = { {1, 4}, {6, 26} }
322};
323
Stefan Roese75659da2015-07-23 10:26:16 +0200324static struct nand_ecclayout ecc_layout_4KB_bch4bit = {
325 .eccbytes = 64,
326 .eccpos = {
327 32, 33, 34, 35, 36, 37, 38, 39,
328 40, 41, 42, 43, 44, 45, 46, 47,
329 48, 49, 50, 51, 52, 53, 54, 55,
330 56, 57, 58, 59, 60, 61, 62, 63,
331 96, 97, 98, 99, 100, 101, 102, 103,
332 104, 105, 106, 107, 108, 109, 110, 111,
333 112, 113, 114, 115, 116, 117, 118, 119,
334 120, 121, 122, 123, 124, 125, 126, 127},
335 /* Bootrom looks in bytes 0 & 5 for bad blocks */
336 .oobfree = { {6, 26}, { 64, 32} }
337};
338
339static struct nand_ecclayout ecc_layout_4KB_bch8bit = {
340 .eccbytes = 128,
341 .eccpos = {
342 32, 33, 34, 35, 36, 37, 38, 39,
343 40, 41, 42, 43, 44, 45, 46, 47,
344 48, 49, 50, 51, 52, 53, 54, 55,
345 56, 57, 58, 59, 60, 61, 62, 63},
346 .oobfree = { }
347};
348
349#define NDTR0_tCH(c) (min((c), 7) << 19)
350#define NDTR0_tCS(c) (min((c), 7) << 16)
351#define NDTR0_tWH(c) (min((c), 7) << 11)
352#define NDTR0_tWP(c) (min((c), 7) << 8)
353#define NDTR0_tRH(c) (min((c), 7) << 3)
354#define NDTR0_tRP(c) (min((c), 7) << 0)
355
356#define NDTR1_tR(c) (min((c), 65535) << 16)
357#define NDTR1_tWHR(c) (min((c), 15) << 4)
358#define NDTR1_tAR(c) (min((c), 15) << 0)
359
360/* convert nano-seconds to nand flash controller clock cycles */
361#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
362
363static enum pxa3xx_nand_variant pxa3xx_nand_get_variant(void)
364{
365 /* We only support the Armada 370/XP/38x for now */
366 return PXA3XX_NAND_VARIANT_ARMADA370;
367}
368
369static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
370 const struct pxa3xx_nand_timing *t)
371{
372 struct pxa3xx_nand_info *info = host->info_data;
373 unsigned long nand_clk = mvebu_get_nand_clock();
374 uint32_t ndtr0, ndtr1;
375
376 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
377 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
378 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
379 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
380 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
381 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
382
383 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
384 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
385 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
386
387 info->ndtr0cs0 = ndtr0;
388 info->ndtr1cs0 = ndtr1;
389 nand_writel(info, NDTR0CS0, ndtr0);
390 nand_writel(info, NDTR1CS0, ndtr1);
391}
392
393static void pxa3xx_nand_set_sdr_timing(struct pxa3xx_nand_host *host,
394 const struct nand_sdr_timings *t)
395{
396 struct pxa3xx_nand_info *info = host->info_data;
397 struct nand_chip *chip = &host->chip;
398 unsigned long nand_clk = mvebu_get_nand_clock();
399 uint32_t ndtr0, ndtr1;
400
401 u32 tCH_min = DIV_ROUND_UP(t->tCH_min, 1000);
402 u32 tCS_min = DIV_ROUND_UP(t->tCS_min, 1000);
403 u32 tWH_min = DIV_ROUND_UP(t->tWH_min, 1000);
Ofer Heifetz8f8d4582018-08-29 11:56:02 +0300404 u32 tWP_min = DIV_ROUND_UP(t->tWC_min - t->tWH_min, 1000);
Stefan Roese75659da2015-07-23 10:26:16 +0200405 u32 tREH_min = DIV_ROUND_UP(t->tREH_min, 1000);
Ofer Heifetz8f8d4582018-08-29 11:56:02 +0300406 u32 tRP_min = DIV_ROUND_UP(t->tRC_min - t->tREH_min, 1000);
Stefan Roese75659da2015-07-23 10:26:16 +0200407 u32 tR = chip->chip_delay * 1000;
408 u32 tWHR_min = DIV_ROUND_UP(t->tWHR_min, 1000);
409 u32 tAR_min = DIV_ROUND_UP(t->tAR_min, 1000);
410
411 /* fallback to a default value if tR = 0 */
412 if (!tR)
413 tR = 20000;
414
415 ndtr0 = NDTR0_tCH(ns2cycle(tCH_min, nand_clk)) |
416 NDTR0_tCS(ns2cycle(tCS_min, nand_clk)) |
417 NDTR0_tWH(ns2cycle(tWH_min, nand_clk)) |
418 NDTR0_tWP(ns2cycle(tWP_min, nand_clk)) |
419 NDTR0_tRH(ns2cycle(tREH_min, nand_clk)) |
420 NDTR0_tRP(ns2cycle(tRP_min, nand_clk));
421
422 ndtr1 = NDTR1_tR(ns2cycle(tR, nand_clk)) |
423 NDTR1_tWHR(ns2cycle(tWHR_min, nand_clk)) |
424 NDTR1_tAR(ns2cycle(tAR_min, nand_clk));
425
426 info->ndtr0cs0 = ndtr0;
427 info->ndtr1cs0 = ndtr1;
428 nand_writel(info, NDTR0CS0, ndtr0);
429 nand_writel(info, NDTR1CS0, ndtr1);
430}
431
432static int pxa3xx_nand_init_timings(struct pxa3xx_nand_host *host)
433{
434 const struct nand_sdr_timings *timings;
435 struct nand_chip *chip = &host->chip;
436 struct pxa3xx_nand_info *info = host->info_data;
437 const struct pxa3xx_nand_flash *f = NULL;
Ofer Heifetz0da35df2018-08-29 11:56:01 +0300438 struct mtd_info *mtd = nand_to_mtd(&host->chip);
Stefan Roese75659da2015-07-23 10:26:16 +0200439 int mode, id, ntypes, i;
440
441 mode = onfi_get_async_timing_mode(chip);
442 if (mode == ONFI_TIMING_MODE_UNKNOWN) {
443 ntypes = ARRAY_SIZE(builtin_flash_types);
444
Ofer Heifetz0da35df2018-08-29 11:56:01 +0300445 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Stefan Roese75659da2015-07-23 10:26:16 +0200446
Ofer Heifetz0da35df2018-08-29 11:56:01 +0300447 id = chip->read_byte(mtd);
448 id |= chip->read_byte(mtd) << 0x8;
Stefan Roese75659da2015-07-23 10:26:16 +0200449
450 for (i = 0; i < ntypes; i++) {
451 f = &builtin_flash_types[i];
452
453 if (f->chip_id == id)
454 break;
455 }
456
457 if (i == ntypes) {
458 dev_err(&info->pdev->dev, "Error: timings not found\n");
459 return -EINVAL;
460 }
461
462 pxa3xx_nand_set_timing(host, f->timing);
463
464 if (f->flash_width == 16) {
465 info->reg_ndcr |= NDCR_DWIDTH_M;
466 chip->options |= NAND_BUSWIDTH_16;
467 }
468
469 info->reg_ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
470 } else {
471 mode = fls(mode) - 1;
472 if (mode < 0)
473 mode = 0;
474
475 timings = onfi_async_timing_mode_to_sdr_timings(mode);
476 if (IS_ERR(timings))
477 return PTR_ERR(timings);
478
479 pxa3xx_nand_set_sdr_timing(host, timings);
480 }
481
482 return 0;
483}
484
Stefan Roese75659da2015-07-23 10:26:16 +0200485/**
Vagrant Cascadianbeb288b2015-11-24 14:46:24 -0800486 * NOTE: it is a must to set ND_RUN first, then write
Stefan Roese75659da2015-07-23 10:26:16 +0200487 * command buffer, otherwise, it does not work.
488 * We enable all the interrupt at the same time, and
489 * let pxa3xx_nand_irq to handle all logic.
490 */
491static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
492{
493 uint32_t ndcr;
494
495 ndcr = info->reg_ndcr;
496
497 if (info->use_ecc) {
498 ndcr |= NDCR_ECC_EN;
499 if (info->ecc_bch)
500 nand_writel(info, NDECCCTRL, 0x1);
501 } else {
502 ndcr &= ~NDCR_ECC_EN;
503 if (info->ecc_bch)
504 nand_writel(info, NDECCCTRL, 0x0);
505 }
506
507 ndcr &= ~NDCR_DMA_EN;
508
509 if (info->use_spare)
510 ndcr |= NDCR_SPARE_EN;
511 else
512 ndcr &= ~NDCR_SPARE_EN;
513
514 ndcr |= NDCR_ND_RUN;
515
516 /* clear status bits and run */
Stefan Roese75659da2015-07-23 10:26:16 +0200517 nand_writel(info, NDSR, NDSR_MASK);
Ofer Heifetzd92d8992018-08-29 11:56:03 +0300518 nand_writel(info, NDCR, 0);
Stefan Roese75659da2015-07-23 10:26:16 +0200519 nand_writel(info, NDCR, ndcr);
520}
521
522static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
523{
524 uint32_t ndcr;
525
526 ndcr = nand_readl(info, NDCR);
527 nand_writel(info, NDCR, ndcr | int_mask);
528}
529
530static void drain_fifo(struct pxa3xx_nand_info *info, void *data, int len)
531{
532 if (info->ecc_bch) {
533 u32 ts;
534
535 /*
536 * According to the datasheet, when reading from NDDB
537 * with BCH enabled, after each 32 bytes reads, we
538 * have to make sure that the NDSR.RDDREQ bit is set.
539 *
540 * Drain the FIFO 8 32 bits reads at a time, and skip
541 * the polling on the last read.
542 */
543 while (len > 8) {
544 readsl(info->mmio_base + NDDB, data, 8);
545
546 ts = get_timer(0);
547 while (!(nand_readl(info, NDSR) & NDSR_RDDREQ)) {
548 if (get_timer(ts) > TIMEOUT_DRAIN_FIFO) {
549 dev_err(&info->pdev->dev,
550 "Timeout on RDDREQ while draining the FIFO\n");
551 return;
552 }
553 }
554
555 data += 32;
556 len -= 8;
557 }
558 }
559
560 readsl(info->mmio_base + NDDB, data, len);
561}
562
563static void handle_data_pio(struct pxa3xx_nand_info *info)
564{
Stefan Roese75659da2015-07-23 10:26:16 +0200565 switch (info->state) {
566 case STATE_PIO_WRITING:
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300567 if (info->step_chunk_size)
568 writesl(info->mmio_base + NDDB,
569 info->data_buff + info->data_buff_pos,
570 DIV_ROUND_UP(info->step_chunk_size, 4));
Stefan Roese75659da2015-07-23 10:26:16 +0200571
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300572 if (info->step_spare_size)
Stefan Roese75659da2015-07-23 10:26:16 +0200573 writesl(info->mmio_base + NDDB,
574 info->oob_buff + info->oob_buff_pos,
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300575 DIV_ROUND_UP(info->step_spare_size, 4));
Stefan Roese75659da2015-07-23 10:26:16 +0200576 break;
577 case STATE_PIO_READING:
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300578 if (info->step_chunk_size)
579 drain_fifo(info,
580 info->data_buff + info->data_buff_pos,
581 DIV_ROUND_UP(info->step_chunk_size, 4));
Stefan Roese75659da2015-07-23 10:26:16 +0200582
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300583 if (info->step_spare_size)
Stefan Roese75659da2015-07-23 10:26:16 +0200584 drain_fifo(info,
585 info->oob_buff + info->oob_buff_pos,
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300586 DIV_ROUND_UP(info->step_spare_size, 4));
Stefan Roese75659da2015-07-23 10:26:16 +0200587 break;
588 default:
589 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300590 info->state);
Stefan Roese75659da2015-07-23 10:26:16 +0200591 BUG();
592 }
593
594 /* Update buffer pointers for multi-page read/write */
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300595 info->data_buff_pos += info->step_chunk_size;
596 info->oob_buff_pos += info->step_spare_size;
Stefan Roese75659da2015-07-23 10:26:16 +0200597}
598
599static void pxa3xx_nand_irq_thread(struct pxa3xx_nand_info *info)
600{
601 handle_data_pio(info);
602
603 info->state = STATE_CMD_DONE;
604 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
605}
606
607static irqreturn_t pxa3xx_nand_irq(struct pxa3xx_nand_info *info)
608{
609 unsigned int status, is_completed = 0, is_ready = 0;
610 unsigned int ready, cmd_done;
611 irqreturn_t ret = IRQ_HANDLED;
612
613 if (info->cs == 0) {
614 ready = NDSR_FLASH_RDY;
615 cmd_done = NDSR_CS0_CMDD;
616 } else {
617 ready = NDSR_RDY;
618 cmd_done = NDSR_CS1_CMDD;
619 }
620
621 status = nand_readl(info, NDSR);
622
623 if (status & NDSR_UNCORERR)
624 info->retcode = ERR_UNCORERR;
625 if (status & NDSR_CORERR) {
626 info->retcode = ERR_CORERR;
627 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 &&
628 info->ecc_bch)
629 info->ecc_err_cnt = NDSR_ERR_CNT(status);
630 else
631 info->ecc_err_cnt = 1;
632
633 /*
634 * Each chunk composing a page is corrected independently,
635 * and we need to store maximum number of corrected bitflips
636 * to return it to the MTD layer in ecc.read_page().
637 */
638 info->max_bitflips = max_t(unsigned int,
639 info->max_bitflips,
640 info->ecc_err_cnt);
641 }
642 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
643 info->state = (status & NDSR_RDDREQ) ?
644 STATE_PIO_READING : STATE_PIO_WRITING;
645 /* Call the IRQ thread in U-Boot directly */
646 pxa3xx_nand_irq_thread(info);
647 return 0;
648 }
649 if (status & cmd_done) {
650 info->state = STATE_CMD_DONE;
651 is_completed = 1;
652 }
653 if (status & ready) {
654 info->state = STATE_READY;
655 is_ready = 1;
656 }
657
Ofer Heifetzde323162018-08-29 11:56:04 +0300658 /*
659 * Clear all status bit before issuing the next command, which
660 * can and will alter the status bits and will deserve a new
661 * interrupt on its own. This lets the controller exit the IRQ
662 */
663 nand_writel(info, NDSR, status);
664
Stefan Roese75659da2015-07-23 10:26:16 +0200665 if (status & NDSR_WRCMDREQ) {
Stefan Roese75659da2015-07-23 10:26:16 +0200666 status &= ~NDSR_WRCMDREQ;
667 info->state = STATE_CMD_HANDLE;
668
669 /*
670 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
671 * must be loaded by writing directly either 12 or 16
672 * bytes directly to NDCB0, four bytes at a time.
673 *
674 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
675 * but each NDCBx register can be read.
676 */
677 nand_writel(info, NDCB0, info->ndcb0);
678 nand_writel(info, NDCB0, info->ndcb1);
679 nand_writel(info, NDCB0, info->ndcb2);
680
681 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
682 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
683 nand_writel(info, NDCB0, info->ndcb3);
684 }
685
Stefan Roese75659da2015-07-23 10:26:16 +0200686 if (is_completed)
687 info->cmd_complete = 1;
688 if (is_ready)
689 info->dev_ready = 1;
690
691 return ret;
692}
693
694static inline int is_buf_blank(uint8_t *buf, size_t len)
695{
696 for (; len > 0; len--)
697 if (*buf++ != 0xff)
698 return 0;
699 return 1;
700}
701
702static void set_command_address(struct pxa3xx_nand_info *info,
703 unsigned int page_size, uint16_t column, int page_addr)
704{
705 /* small page addr setting */
706 if (page_size < PAGE_CHUNK_SIZE) {
707 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
708 | (column & 0xFF);
709
710 info->ndcb2 = 0;
711 } else {
712 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
713 | (column & 0xFFFF);
714
715 if (page_addr & 0xFF0000)
716 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
717 else
718 info->ndcb2 = 0;
719 }
720}
721
722static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
723{
724 struct pxa3xx_nand_host *host = info->host[info->cs];
Ofer Heifetz0da35df2018-08-29 11:56:01 +0300725 struct mtd_info *mtd = nand_to_mtd(&host->chip);
Stefan Roese75659da2015-07-23 10:26:16 +0200726
727 /* reset data and oob column point to handle data */
728 info->buf_start = 0;
729 info->buf_count = 0;
Stefan Roese75659da2015-07-23 10:26:16 +0200730 info->data_buff_pos = 0;
731 info->oob_buff_pos = 0;
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300732 info->step_chunk_size = 0;
733 info->step_spare_size = 0;
734 info->cur_chunk = 0;
Stefan Roese75659da2015-07-23 10:26:16 +0200735 info->use_ecc = 0;
736 info->use_spare = 1;
737 info->retcode = ERR_NONE;
738 info->ecc_err_cnt = 0;
739 info->ndcb3 = 0;
740 info->need_wait = 0;
741
742 switch (command) {
743 case NAND_CMD_READ0:
Boris Brezillona558a392018-08-29 11:56:12 +0300744 case NAND_CMD_READOOB:
Stefan Roese75659da2015-07-23 10:26:16 +0200745 case NAND_CMD_PAGEPROG:
746 info->use_ecc = 1;
Stefan Roese75659da2015-07-23 10:26:16 +0200747 break;
748 case NAND_CMD_PARAM:
749 info->use_spare = 0;
750 break;
751 default:
752 info->ndcb1 = 0;
753 info->ndcb2 = 0;
754 break;
755 }
756
757 /*
758 * If we are about to issue a read command, or about to set
759 * the write address, then clean the data buffer.
760 */
761 if (command == NAND_CMD_READ0 ||
762 command == NAND_CMD_READOOB ||
763 command == NAND_CMD_SEQIN) {
764 info->buf_count = mtd->writesize + mtd->oobsize;
765 memset(info->data_buff, 0xFF, info->buf_count);
766 }
767}
768
769static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
770 int ext_cmd_type, uint16_t column, int page_addr)
771{
772 int addr_cycle, exec_cmd;
773 struct pxa3xx_nand_host *host;
774 struct mtd_info *mtd;
775
776 host = info->host[info->cs];
Ofer Heifetz0da35df2018-08-29 11:56:01 +0300777 mtd = nand_to_mtd(&host->chip);
Stefan Roese75659da2015-07-23 10:26:16 +0200778 addr_cycle = 0;
779 exec_cmd = 1;
780
781 if (info->cs != 0)
782 info->ndcb0 = NDCB0_CSEL;
783 else
784 info->ndcb0 = 0;
785
786 if (command == NAND_CMD_SEQIN)
787 exec_cmd = 0;
788
789 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
790 + host->col_addr_cycles);
791
792 switch (command) {
793 case NAND_CMD_READOOB:
794 case NAND_CMD_READ0:
795 info->buf_start = column;
796 info->ndcb0 |= NDCB0_CMD_TYPE(0)
797 | addr_cycle
798 | NAND_CMD_READ0;
799
800 if (command == NAND_CMD_READOOB)
801 info->buf_start += mtd->writesize;
802
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300803 if (info->cur_chunk < info->nfullchunks) {
804 info->step_chunk_size = info->chunk_size;
805 info->step_spare_size = info->spare_size;
806 } else {
807 info->step_chunk_size = info->last_chunk_size;
808 info->step_spare_size = info->last_spare_size;
809 }
810
Stefan Roese75659da2015-07-23 10:26:16 +0200811 /*
812 * Multiple page read needs an 'extended command type' field,
813 * which is either naked-read or last-read according to the
814 * state.
815 */
816 if (mtd->writesize == PAGE_CHUNK_SIZE) {
817 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
818 } else if (mtd->writesize > PAGE_CHUNK_SIZE) {
819 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
820 | NDCB0_LEN_OVRD
821 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300822 info->ndcb3 = info->step_chunk_size +
823 info->step_spare_size;
Stefan Roese75659da2015-07-23 10:26:16 +0200824 }
825
826 set_command_address(info, mtd->writesize, column, page_addr);
827 break;
828
829 case NAND_CMD_SEQIN:
830
831 info->buf_start = column;
832 set_command_address(info, mtd->writesize, 0, page_addr);
833
834 /*
835 * Multiple page programming needs to execute the initial
836 * SEQIN command that sets the page address.
837 */
838 if (mtd->writesize > PAGE_CHUNK_SIZE) {
839 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
840 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
841 | addr_cycle
842 | command;
Stefan Roese75659da2015-07-23 10:26:16 +0200843 exec_cmd = 1;
844 }
845 break;
846
847 case NAND_CMD_PAGEPROG:
848 if (is_buf_blank(info->data_buff,
849 (mtd->writesize + mtd->oobsize))) {
850 exec_cmd = 0;
851 break;
852 }
853
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300854 if (info->cur_chunk < info->nfullchunks) {
855 info->step_chunk_size = info->chunk_size;
856 info->step_spare_size = info->spare_size;
857 } else {
858 info->step_chunk_size = info->last_chunk_size;
859 info->step_spare_size = info->last_spare_size;
860 }
861
Stefan Roese75659da2015-07-23 10:26:16 +0200862 /* Second command setting for large pages */
863 if (mtd->writesize > PAGE_CHUNK_SIZE) {
864 /*
865 * Multiple page write uses the 'extended command'
866 * field. This can be used to issue a command dispatch
867 * or a naked-write depending on the current stage.
868 */
869 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
870 | NDCB0_LEN_OVRD
871 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300872 info->ndcb3 = info->step_chunk_size +
873 info->step_spare_size;
Stefan Roese75659da2015-07-23 10:26:16 +0200874
875 /*
876 * This is the command dispatch that completes a chunked
877 * page program operation.
878 */
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300879 if (info->cur_chunk == info->ntotalchunks) {
Stefan Roese75659da2015-07-23 10:26:16 +0200880 info->ndcb0 = NDCB0_CMD_TYPE(0x1)
881 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
882 | command;
883 info->ndcb1 = 0;
884 info->ndcb2 = 0;
885 info->ndcb3 = 0;
886 }
887 } else {
888 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
889 | NDCB0_AUTO_RS
890 | NDCB0_ST_ROW_EN
891 | NDCB0_DBC
892 | (NAND_CMD_PAGEPROG << 8)
893 | NAND_CMD_SEQIN
894 | addr_cycle;
895 }
896 break;
897
898 case NAND_CMD_PARAM:
Ofer Heifetzfdf5b232018-08-29 11:56:00 +0300899 info->buf_count = INIT_BUFFER_SIZE;
Stefan Roese75659da2015-07-23 10:26:16 +0200900 info->ndcb0 |= NDCB0_CMD_TYPE(0)
901 | NDCB0_ADDR_CYC(1)
902 | NDCB0_LEN_OVRD
903 | command;
904 info->ndcb1 = (column & 0xFF);
Ofer Heifetzfdf5b232018-08-29 11:56:00 +0300905 info->ndcb3 = INIT_BUFFER_SIZE;
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300906 info->step_chunk_size = INIT_BUFFER_SIZE;
Stefan Roese75659da2015-07-23 10:26:16 +0200907 break;
908
909 case NAND_CMD_READID:
Ofer Heifetz4a574aa2018-08-29 11:56:05 +0300910 info->buf_count = READ_ID_BYTES;
Stefan Roese75659da2015-07-23 10:26:16 +0200911 info->ndcb0 |= NDCB0_CMD_TYPE(3)
912 | NDCB0_ADDR_CYC(1)
913 | command;
914 info->ndcb1 = (column & 0xFF);
915
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300916 info->step_chunk_size = 8;
Stefan Roese75659da2015-07-23 10:26:16 +0200917 break;
918 case NAND_CMD_STATUS:
919 info->buf_count = 1;
920 info->ndcb0 |= NDCB0_CMD_TYPE(4)
921 | NDCB0_ADDR_CYC(1)
922 | command;
923
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300924 info->step_chunk_size = 8;
Stefan Roese75659da2015-07-23 10:26:16 +0200925 break;
926
927 case NAND_CMD_ERASE1:
928 info->ndcb0 |= NDCB0_CMD_TYPE(2)
929 | NDCB0_AUTO_RS
930 | NDCB0_ADDR_CYC(3)
931 | NDCB0_DBC
932 | (NAND_CMD_ERASE2 << 8)
933 | NAND_CMD_ERASE1;
934 info->ndcb1 = page_addr;
935 info->ndcb2 = 0;
936
937 break;
938 case NAND_CMD_RESET:
939 info->ndcb0 |= NDCB0_CMD_TYPE(5)
940 | command;
941
942 break;
943
944 case NAND_CMD_ERASE2:
945 exec_cmd = 0;
946 break;
947
948 default:
949 exec_cmd = 0;
950 dev_err(&info->pdev->dev, "non-supported command %x\n",
951 command);
952 break;
953 }
954
955 return exec_cmd;
956}
957
958static void nand_cmdfunc(struct mtd_info *mtd, unsigned command,
959 int column, int page_addr)
960{
Scott Wood17fed142016-05-30 13:57:56 -0500961 struct nand_chip *chip = mtd_to_nand(mtd);
962 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +0200963 struct pxa3xx_nand_info *info = host->info_data;
964 int exec_cmd;
965
966 /*
967 * if this is a x16 device ,then convert the input
968 * "byte" address into a "word" address appropriate
969 * for indexing a word-oriented device
970 */
971 if (info->reg_ndcr & NDCR_DWIDTH_M)
972 column /= 2;
973
974 /*
975 * There may be different NAND chip hooked to
976 * different chip select, so check whether
977 * chip select has been changed, if yes, reset the timing
978 */
979 if (info->cs != host->cs) {
980 info->cs = host->cs;
981 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
982 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
983 }
984
985 prepare_start_command(info, command);
986
987 info->state = STATE_PREPARED;
988 exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
989
990 if (exec_cmd) {
991 u32 ts;
992
993 info->cmd_complete = 0;
994 info->dev_ready = 0;
995 info->need_wait = 1;
996 pxa3xx_nand_start(info);
997
998 ts = get_timer(0);
999 while (1) {
1000 u32 status;
1001
1002 status = nand_readl(info, NDSR);
1003 if (status)
1004 pxa3xx_nand_irq(info);
1005
1006 if (info->cmd_complete)
1007 break;
1008
1009 if (get_timer(ts) > CHIP_DELAY_TIMEOUT) {
1010 dev_err(&info->pdev->dev, "Wait timeout!!!\n");
1011 return;
1012 }
1013 }
1014 }
1015 info->state = STATE_IDLE;
1016}
1017
1018static void nand_cmdfunc_extended(struct mtd_info *mtd,
1019 const unsigned command,
1020 int column, int page_addr)
1021{
Scott Wood17fed142016-05-30 13:57:56 -05001022 struct nand_chip *chip = mtd_to_nand(mtd);
1023 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001024 struct pxa3xx_nand_info *info = host->info_data;
1025 int exec_cmd, ext_cmd_type;
1026
1027 /*
1028 * if this is a x16 device then convert the input
1029 * "byte" address into a "word" address appropriate
1030 * for indexing a word-oriented device
1031 */
1032 if (info->reg_ndcr & NDCR_DWIDTH_M)
1033 column /= 2;
1034
1035 /*
1036 * There may be different NAND chip hooked to
1037 * different chip select, so check whether
1038 * chip select has been changed, if yes, reset the timing
1039 */
1040 if (info->cs != host->cs) {
1041 info->cs = host->cs;
1042 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1043 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
1044 }
1045
1046 /* Select the extended command for the first command */
1047 switch (command) {
1048 case NAND_CMD_READ0:
1049 case NAND_CMD_READOOB:
1050 ext_cmd_type = EXT_CMD_TYPE_MONO;
1051 break;
1052 case NAND_CMD_SEQIN:
1053 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
1054 break;
1055 case NAND_CMD_PAGEPROG:
1056 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
1057 break;
1058 default:
1059 ext_cmd_type = 0;
1060 break;
1061 }
1062
1063 prepare_start_command(info, command);
1064
1065 /*
1066 * Prepare the "is ready" completion before starting a command
1067 * transaction sequence. If the command is not executed the
1068 * completion will be completed, see below.
1069 *
1070 * We can do that inside the loop because the command variable
1071 * is invariant and thus so is the exec_cmd.
1072 */
1073 info->need_wait = 1;
1074 info->dev_ready = 0;
1075
1076 do {
1077 u32 ts;
1078
1079 info->state = STATE_PREPARED;
1080 exec_cmd = prepare_set_command(info, command, ext_cmd_type,
1081 column, page_addr);
1082 if (!exec_cmd) {
1083 info->need_wait = 0;
1084 info->dev_ready = 1;
1085 break;
1086 }
1087
1088 info->cmd_complete = 0;
1089 pxa3xx_nand_start(info);
1090
1091 ts = get_timer(0);
1092 while (1) {
1093 u32 status;
1094
1095 status = nand_readl(info, NDSR);
1096 if (status)
1097 pxa3xx_nand_irq(info);
1098
1099 if (info->cmd_complete)
1100 break;
1101
1102 if (get_timer(ts) > CHIP_DELAY_TIMEOUT) {
1103 dev_err(&info->pdev->dev, "Wait timeout!!!\n");
1104 return;
1105 }
1106 }
1107
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001108 /* Only a few commands need several steps */
1109 if (command != NAND_CMD_PAGEPROG &&
1110 command != NAND_CMD_READ0 &&
1111 command != NAND_CMD_READOOB)
1112 break;
1113
1114 info->cur_chunk++;
1115
Stefan Roese75659da2015-07-23 10:26:16 +02001116 /* Check if the sequence is complete */
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001117 if (info->cur_chunk == info->ntotalchunks &&
1118 command != NAND_CMD_PAGEPROG)
Stefan Roese75659da2015-07-23 10:26:16 +02001119 break;
1120
1121 /*
1122 * After a splitted program command sequence has issued
1123 * the command dispatch, the command sequence is complete.
1124 */
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001125 if (info->cur_chunk == (info->ntotalchunks + 1) &&
Stefan Roese75659da2015-07-23 10:26:16 +02001126 command == NAND_CMD_PAGEPROG &&
1127 ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
1128 break;
1129
1130 if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
1131 /* Last read: issue a 'last naked read' */
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001132 if (info->cur_chunk == info->ntotalchunks - 1)
Stefan Roese75659da2015-07-23 10:26:16 +02001133 ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
1134 else
1135 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
1136
1137 /*
1138 * If a splitted program command has no more data to transfer,
1139 * the command dispatch must be issued to complete.
1140 */
1141 } else if (command == NAND_CMD_PAGEPROG &&
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001142 info->cur_chunk == info->ntotalchunks) {
Stefan Roese75659da2015-07-23 10:26:16 +02001143 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
1144 }
1145 } while (1);
1146
1147 info->state = STATE_IDLE;
1148}
1149
1150static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Scott Wood46e13102016-05-30 13:57:57 -05001151 struct nand_chip *chip, const uint8_t *buf, int oob_required,
1152 int page)
Stefan Roese75659da2015-07-23 10:26:16 +02001153{
1154 chip->write_buf(mtd, buf, mtd->writesize);
1155 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1156
1157 return 0;
1158}
1159
1160static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
1161 struct nand_chip *chip, uint8_t *buf, int oob_required,
1162 int page)
1163{
Scott Wood17fed142016-05-30 13:57:56 -05001164 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001165 struct pxa3xx_nand_info *info = host->info_data;
1166
1167 chip->read_buf(mtd, buf, mtd->writesize);
1168 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1169
1170 if (info->retcode == ERR_CORERR && info->use_ecc) {
1171 mtd->ecc_stats.corrected += info->ecc_err_cnt;
1172
1173 } else if (info->retcode == ERR_UNCORERR) {
1174 /*
1175 * for blank page (all 0xff), HW will calculate its ECC as
1176 * 0, which is different from the ECC information within
1177 * OOB, ignore such uncorrectable errors
1178 */
1179 if (is_buf_blank(buf, mtd->writesize))
1180 info->retcode = ERR_NONE;
1181 else
1182 mtd->ecc_stats.failed++;
1183 }
1184
1185 return info->max_bitflips;
1186}
1187
1188static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
1189{
Scott Wood17fed142016-05-30 13:57:56 -05001190 struct nand_chip *chip = mtd_to_nand(mtd);
1191 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001192 struct pxa3xx_nand_info *info = host->info_data;
1193 char retval = 0xFF;
1194
1195 if (info->buf_start < info->buf_count)
1196 /* Has just send a new command? */
1197 retval = info->data_buff[info->buf_start++];
1198
1199 return retval;
1200}
1201
1202static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
1203{
Scott Wood17fed142016-05-30 13:57:56 -05001204 struct nand_chip *chip = mtd_to_nand(mtd);
1205 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001206 struct pxa3xx_nand_info *info = host->info_data;
1207 u16 retval = 0xFFFF;
1208
1209 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
1210 retval = *((u16 *)(info->data_buff+info->buf_start));
1211 info->buf_start += 2;
1212 }
1213 return retval;
1214}
1215
1216static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1217{
Scott Wood17fed142016-05-30 13:57:56 -05001218 struct nand_chip *chip = mtd_to_nand(mtd);
1219 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001220 struct pxa3xx_nand_info *info = host->info_data;
1221 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1222
1223 memcpy(buf, info->data_buff + info->buf_start, real_len);
1224 info->buf_start += real_len;
1225}
1226
1227static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
1228 const uint8_t *buf, int len)
1229{
Scott Wood17fed142016-05-30 13:57:56 -05001230 struct nand_chip *chip = mtd_to_nand(mtd);
1231 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001232 struct pxa3xx_nand_info *info = host->info_data;
1233 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1234
1235 memcpy(info->data_buff + info->buf_start, buf, real_len);
1236 info->buf_start += real_len;
1237}
1238
1239static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
1240{
1241 return;
1242}
1243
1244static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
1245{
Scott Wood17fed142016-05-30 13:57:56 -05001246 struct nand_chip *chip = mtd_to_nand(mtd);
1247 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001248 struct pxa3xx_nand_info *info = host->info_data;
1249
1250 if (info->need_wait) {
1251 u32 ts;
1252
1253 info->need_wait = 0;
1254
1255 ts = get_timer(0);
1256 while (1) {
1257 u32 status;
1258
1259 status = nand_readl(info, NDSR);
1260 if (status)
1261 pxa3xx_nand_irq(info);
1262
1263 if (info->dev_ready)
1264 break;
1265
1266 if (get_timer(ts) > CHIP_DELAY_TIMEOUT) {
1267 dev_err(&info->pdev->dev, "Ready timeout!!!\n");
1268 return NAND_STATUS_FAIL;
1269 }
1270 }
1271 }
1272
1273 /* pxa3xx_nand_send_command has waited for command complete */
1274 if (this->state == FL_WRITING || this->state == FL_ERASING) {
1275 if (info->retcode == ERR_NONE)
1276 return 0;
1277 else
1278 return NAND_STATUS_FAIL;
1279 }
1280
1281 return NAND_STATUS_READY;
1282}
1283
Ofer Heifetz531816e2018-08-29 11:56:07 +03001284static int pxa3xx_nand_config_ident(struct pxa3xx_nand_info *info)
1285{
1286 struct pxa3xx_nand_platform_data *pdata = info->pdata;
1287
1288 /* Configure default flash values */
1289 info->chunk_size = PAGE_CHUNK_SIZE;
1290 info->reg_ndcr = 0x0; /* enable all interrupts */
1291 info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
1292 info->reg_ndcr |= NDCR_RD_ID_CNT(READ_ID_BYTES);
1293 info->reg_ndcr |= NDCR_SPARE_EN;
1294
1295 return 0;
1296}
1297
1298static void pxa3xx_nand_config_tail(struct pxa3xx_nand_info *info)
Stefan Roese75659da2015-07-23 10:26:16 +02001299{
1300 struct pxa3xx_nand_host *host = info->host[info->cs];
Ofer Heifetz531816e2018-08-29 11:56:07 +03001301 struct mtd_info *mtd = nand_to_mtd(&info->host[info->cs]->chip);
Scott Wood17fed142016-05-30 13:57:56 -05001302 struct nand_chip *chip = mtd_to_nand(mtd);
Stefan Roese75659da2015-07-23 10:26:16 +02001303
1304 info->reg_ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
1305 info->reg_ndcr |= (chip->page_shift == 6) ? NDCR_PG_PER_BLK : 0;
1306 info->reg_ndcr |= (mtd->writesize == 2048) ? NDCR_PAGE_SZ : 0;
Stefan Roese75659da2015-07-23 10:26:16 +02001307}
1308
Ofer Heifetz268979f2018-08-29 11:56:08 +03001309static void pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
Stefan Roese75659da2015-07-23 10:26:16 +02001310{
Ofer Heifetz531816e2018-08-29 11:56:07 +03001311 struct pxa3xx_nand_platform_data *pdata = info->pdata;
Stefan Roese75659da2015-07-23 10:26:16 +02001312 uint32_t ndcr = nand_readl(info, NDCR);
1313
Stefan Roese75659da2015-07-23 10:26:16 +02001314 /* Set an initial chunk size */
Ofer Heifetz4a574aa2018-08-29 11:56:05 +03001315 info->chunk_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
Ofer Heifetz531816e2018-08-29 11:56:07 +03001316 info->reg_ndcr = ndcr &
1317 ~(NDCR_INT_MASK | NDCR_ND_ARB_EN | NFCV1_NDCR_ARB_CNTL);
1318 info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Stefan Roese75659da2015-07-23 10:26:16 +02001319 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
1320 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Stefan Roese75659da2015-07-23 10:26:16 +02001321}
1322
1323static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1324{
1325 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1326 if (info->data_buff == NULL)
1327 return -ENOMEM;
1328 return 0;
1329}
1330
1331static int pxa3xx_nand_sensing(struct pxa3xx_nand_host *host)
1332{
1333 struct pxa3xx_nand_info *info = host->info_data;
1334 struct pxa3xx_nand_platform_data *pdata = info->pdata;
1335 struct mtd_info *mtd;
1336 struct nand_chip *chip;
1337 const struct nand_sdr_timings *timings;
1338 int ret;
1339
Ofer Heifetz0da35df2018-08-29 11:56:01 +03001340 mtd = nand_to_mtd(&info->host[info->cs]->chip);
Scott Wood17fed142016-05-30 13:57:56 -05001341 chip = mtd_to_nand(mtd);
Stefan Roese75659da2015-07-23 10:26:16 +02001342
1343 /* configure default flash values */
1344 info->reg_ndcr = 0x0; /* enable all interrupts */
1345 info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Ofer Heifetz4a574aa2018-08-29 11:56:05 +03001346 info->reg_ndcr |= NDCR_RD_ID_CNT(READ_ID_BYTES);
Stefan Roese75659da2015-07-23 10:26:16 +02001347 info->reg_ndcr |= NDCR_SPARE_EN; /* enable spare by default */
1348
1349 /* use the common timing to make a try */
1350 timings = onfi_async_timing_mode_to_sdr_timings(0);
1351 if (IS_ERR(timings))
1352 return PTR_ERR(timings);
1353
1354 pxa3xx_nand_set_sdr_timing(host, timings);
1355
1356 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
1357 ret = chip->waitfunc(mtd, chip);
1358 if (ret & NAND_STATUS_FAIL)
1359 return -ENODEV;
1360
1361 return 0;
1362}
1363
1364static int pxa_ecc_init(struct pxa3xx_nand_info *info,
1365 struct nand_ecc_ctrl *ecc,
1366 int strength, int ecc_stepsize, int page_size)
1367{
1368 if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) {
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001369 info->nfullchunks = 1;
1370 info->ntotalchunks = 1;
Stefan Roese75659da2015-07-23 10:26:16 +02001371 info->chunk_size = 2048;
1372 info->spare_size = 40;
1373 info->ecc_size = 24;
1374 ecc->mode = NAND_ECC_HW;
1375 ecc->size = 512;
1376 ecc->strength = 1;
1377
1378 } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) {
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001379 info->nfullchunks = 1;
1380 info->ntotalchunks = 1;
Stefan Roese75659da2015-07-23 10:26:16 +02001381 info->chunk_size = 512;
1382 info->spare_size = 8;
1383 info->ecc_size = 8;
1384 ecc->mode = NAND_ECC_HW;
1385 ecc->size = 512;
1386 ecc->strength = 1;
1387
1388 /*
1389 * Required ECC: 4-bit correction per 512 bytes
1390 * Select: 16-bit correction per 2048 bytes
1391 */
1392 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) {
1393 info->ecc_bch = 1;
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001394 info->nfullchunks = 1;
1395 info->ntotalchunks = 1;
Stefan Roese75659da2015-07-23 10:26:16 +02001396 info->chunk_size = 2048;
1397 info->spare_size = 32;
1398 info->ecc_size = 32;
1399 ecc->mode = NAND_ECC_HW;
1400 ecc->size = info->chunk_size;
1401 ecc->layout = &ecc_layout_2KB_bch4bit;
1402 ecc->strength = 16;
1403
1404 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
1405 info->ecc_bch = 1;
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001406 info->nfullchunks = 2;
1407 info->ntotalchunks = 2;
Stefan Roese75659da2015-07-23 10:26:16 +02001408 info->chunk_size = 2048;
1409 info->spare_size = 32;
1410 info->ecc_size = 32;
1411 ecc->mode = NAND_ECC_HW;
1412 ecc->size = info->chunk_size;
1413 ecc->layout = &ecc_layout_4KB_bch4bit;
1414 ecc->strength = 16;
1415
1416 /*
1417 * Required ECC: 8-bit correction per 512 bytes
1418 * Select: 16-bit correction per 1024 bytes
1419 */
1420 } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
1421 info->ecc_bch = 1;
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001422 info->nfullchunks = 4;
1423 info->ntotalchunks = 5;
Stefan Roese75659da2015-07-23 10:26:16 +02001424 info->chunk_size = 1024;
1425 info->spare_size = 0;
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001426 info->last_chunk_size = 0;
1427 info->last_spare_size = 64;
Stefan Roese75659da2015-07-23 10:26:16 +02001428 info->ecc_size = 32;
1429 ecc->mode = NAND_ECC_HW;
1430 ecc->size = info->chunk_size;
1431 ecc->layout = &ecc_layout_4KB_bch8bit;
1432 ecc->strength = 16;
Victor Axelrodfdf9dfb2018-08-29 11:56:13 +03001433 } else if (strength == 8 && ecc_stepsize == 512 && page_size == 2048) {
1434 info->ecc_bch = 1;
1435 info->nfullchunks = 1;
1436 info->ntotalchunks = 2;
1437 info->chunk_size = 1024;
1438 info->spare_size = 0;
1439 info->last_chunk_size = 1024;
1440 info->last_spare_size = 64;
1441 info->ecc_size = 32;
1442 ecc->mode = NAND_ECC_HW;
1443 ecc->size = info->chunk_size;
1444 ecc->layout = &ecc_layout_2KB_bch8bit;
1445 ecc->strength = 16;
Stefan Roese75659da2015-07-23 10:26:16 +02001446 } else {
1447 dev_err(&info->pdev->dev,
1448 "ECC strength %d at page size %d is not supported\n",
1449 strength, page_size);
1450 return -ENODEV;
1451 }
1452
1453 return 0;
1454}
1455
1456static int pxa3xx_nand_scan(struct mtd_info *mtd)
1457{
Scott Wood17fed142016-05-30 13:57:56 -05001458 struct nand_chip *chip = mtd_to_nand(mtd);
1459 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001460 struct pxa3xx_nand_info *info = host->info_data;
1461 struct pxa3xx_nand_platform_data *pdata = info->pdata;
Stefan Roese75659da2015-07-23 10:26:16 +02001462 int ret;
1463 uint16_t ecc_strength, ecc_step;
1464
Ofer Heifetz268979f2018-08-29 11:56:08 +03001465 if (pdata->keep_config) {
1466 pxa3xx_nand_detect_config(info);
1467 } else {
1468 ret = pxa3xx_nand_config_ident(info);
1469 if (ret)
1470 return ret;
1471 ret = pxa3xx_nand_sensing(host);
1472 if (ret) {
1473 dev_info(&info->pdev->dev,
1474 "There is no chip on cs %d!\n",
1475 info->cs);
1476 return ret;
1477 }
Stefan Roese75659da2015-07-23 10:26:16 +02001478 }
1479
Stefan Roese75659da2015-07-23 10:26:16 +02001480 /* Device detection must be done with ECC disabled */
1481 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1482 nand_writel(info, NDECCCTRL, 0x0);
1483
1484 if (nand_scan_ident(mtd, 1, NULL))
1485 return -ENODEV;
1486
1487 if (!pdata->keep_config) {
1488 ret = pxa3xx_nand_init_timings(host);
1489 if (ret) {
1490 dev_err(&info->pdev->dev,
1491 "Failed to set timings: %d\n", ret);
1492 return ret;
1493 }
1494 }
1495
Stefan Roese75659da2015-07-23 10:26:16 +02001496#ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1497 /*
1498 * We'll use a bad block table stored in-flash and don't
1499 * allow writing the bad block marker to the flash.
1500 */
1501 chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB_BBM;
1502 chip->bbt_td = &bbt_main_descr;
1503 chip->bbt_md = &bbt_mirror_descr;
1504#endif
1505
1506 /*
1507 * If the page size is bigger than the FIFO size, let's check
1508 * we are given the right variant and then switch to the extended
1509 * (aka splitted) command handling,
1510 */
1511 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1512 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) {
1513 chip->cmdfunc = nand_cmdfunc_extended;
1514 } else {
1515 dev_err(&info->pdev->dev,
1516 "unsupported page size on this variant\n");
1517 return -ENODEV;
1518 }
1519 }
1520
1521 if (pdata->ecc_strength && pdata->ecc_step_size) {
1522 ecc_strength = pdata->ecc_strength;
1523 ecc_step = pdata->ecc_step_size;
1524 } else {
1525 ecc_strength = chip->ecc_strength_ds;
1526 ecc_step = chip->ecc_step_ds;
1527 }
1528
1529 /* Set default ECC strength requirements on non-ONFI devices */
1530 if (ecc_strength < 1 && ecc_step < 1) {
1531 ecc_strength = 1;
1532 ecc_step = 512;
1533 }
1534
1535 ret = pxa_ecc_init(info, &chip->ecc, ecc_strength,
1536 ecc_step, mtd->writesize);
1537 if (ret)
1538 return ret;
1539
1540 /* calculate addressing information */
1541 if (mtd->writesize >= 2048)
1542 host->col_addr_cycles = 2;
1543 else
1544 host->col_addr_cycles = 1;
1545
1546 /* release the initial buffer */
1547 kfree(info->data_buff);
1548
1549 /* allocate the real data + oob buffer */
1550 info->buf_size = mtd->writesize + mtd->oobsize;
1551 ret = pxa3xx_nand_init_buff(info);
1552 if (ret)
1553 return ret;
1554 info->oob_buff = info->data_buff + mtd->writesize;
1555
1556 if ((mtd->size >> chip->page_shift) > 65536)
1557 host->row_addr_cycles = 3;
1558 else
1559 host->row_addr_cycles = 2;
Ofer Heifetz531816e2018-08-29 11:56:07 +03001560
1561 if (!pdata->keep_config)
1562 pxa3xx_nand_config_tail(info);
1563
Stefan Roese75659da2015-07-23 10:26:16 +02001564 return nand_scan_tail(mtd);
1565}
1566
1567static int alloc_nand_resource(struct pxa3xx_nand_info *info)
1568{
1569 struct pxa3xx_nand_platform_data *pdata;
1570 struct pxa3xx_nand_host *host;
1571 struct nand_chip *chip = NULL;
1572 struct mtd_info *mtd;
1573 int ret, cs;
1574
1575 pdata = info->pdata;
1576 if (pdata->num_cs <= 0)
1577 return -ENODEV;
1578
1579 info->variant = pxa3xx_nand_get_variant();
1580 for (cs = 0; cs < pdata->num_cs; cs++) {
Kevin Smith4d21b592016-01-14 16:01:38 +00001581 chip = (struct nand_chip *)
1582 ((u8 *)&info[1] + sizeof(*host) * cs);
Scott Wood17fed142016-05-30 13:57:56 -05001583 mtd = nand_to_mtd(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001584 host = (struct pxa3xx_nand_host *)chip;
1585 info->host[cs] = host;
Stefan Roese75659da2015-07-23 10:26:16 +02001586 host->cs = cs;
1587 host->info_data = info;
Stefan Roese75659da2015-07-23 10:26:16 +02001588 mtd->owner = THIS_MODULE;
1589
Chris Packham3c2170a2016-08-29 15:20:52 +12001590 nand_set_controller_data(chip, host);
Stefan Roese75659da2015-07-23 10:26:16 +02001591 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1592 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1593 chip->controller = &info->controller;
1594 chip->waitfunc = pxa3xx_nand_waitfunc;
1595 chip->select_chip = pxa3xx_nand_select_chip;
1596 chip->read_word = pxa3xx_nand_read_word;
1597 chip->read_byte = pxa3xx_nand_read_byte;
1598 chip->read_buf = pxa3xx_nand_read_buf;
1599 chip->write_buf = pxa3xx_nand_write_buf;
1600 chip->options |= NAND_NO_SUBPAGE_WRITE;
1601 chip->cmdfunc = nand_cmdfunc;
1602 }
1603
Stefan Roese75659da2015-07-23 10:26:16 +02001604 /* Allocate a buffer to allow flash detection */
1605 info->buf_size = INIT_BUFFER_SIZE;
1606 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1607 if (info->data_buff == NULL) {
1608 ret = -ENOMEM;
1609 goto fail_disable_clk;
1610 }
1611
1612 /* initialize all interrupts to be disabled */
1613 disable_int(info, NDSR_MASK);
1614
1615 return 0;
1616
1617 kfree(info->data_buff);
1618fail_disable_clk:
1619 return ret;
1620}
1621
1622static int pxa3xx_nand_probe_dt(struct pxa3xx_nand_info *info)
1623{
1624 struct pxa3xx_nand_platform_data *pdata;
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +03001625 const void *blob = gd->fdt_blob;
1626 int node = -1;
Stefan Roese75659da2015-07-23 10:26:16 +02001627
1628 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1629 if (!pdata)
1630 return -ENOMEM;
1631
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +03001632 /* Get address decoding nodes from the FDT blob */
1633 do {
1634 node = fdt_node_offset_by_compatible(blob, node,
1635 "marvell,mvebu-pxa3xx-nand");
1636 if (node < 0)
1637 break;
1638
1639 /* Bypass disabeld nodes */
1640 if (!fdtdec_get_is_enabled(blob, node))
1641 continue;
Stefan Roese75659da2015-07-23 10:26:16 +02001642
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +03001643 /* Get the first enabled NAND controler base address */
1644 info->mmio_base =
1645 (void __iomem *)fdtdec_get_addr_size_auto_noparent(
1646 blob, node, "reg", 0, NULL, true);
Stefan Roese75659da2015-07-23 10:26:16 +02001647
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +03001648 pdata->num_cs = fdtdec_get_int(blob, node, "num-cs", 1);
1649 if (pdata->num_cs != 1) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001650 pr_err("pxa3xx driver supports single CS only\n");
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +03001651 break;
1652 }
1653
1654 if (fdtdec_get_bool(blob, node, "nand-enable-arbiter"))
1655 pdata->enable_arbiter = 1;
1656
1657 if (fdtdec_get_bool(blob, node, "nand-keep-config"))
1658 pdata->keep_config = 1;
1659
1660 /*
1661 * ECC parameters.
1662 * If these are not set, they will be selected according
1663 * to the detected flash type.
1664 */
1665 /* ECC strength */
1666 pdata->ecc_strength = fdtdec_get_int(blob, node,
1667 "nand-ecc-strength", 0);
1668
1669 /* ECC step size */
1670 pdata->ecc_step_size = fdtdec_get_int(blob, node,
1671 "nand-ecc-step-size", 0);
1672
1673 info->pdata = pdata;
1674
1675 /* Currently support only a single NAND controller */
1676 return 0;
1677
1678 } while (node >= 0);
1679
1680 return -EINVAL;
Stefan Roese75659da2015-07-23 10:26:16 +02001681}
1682
1683static int pxa3xx_nand_probe(struct pxa3xx_nand_info *info)
1684{
1685 struct pxa3xx_nand_platform_data *pdata;
1686 int ret, cs, probe_success;
1687
1688 ret = pxa3xx_nand_probe_dt(info);
1689 if (ret)
1690 return ret;
1691
1692 pdata = info->pdata;
1693
1694 ret = alloc_nand_resource(info);
1695 if (ret) {
1696 dev_err(&pdev->dev, "alloc nand resource failed\n");
1697 return ret;
1698 }
1699
1700 probe_success = 0;
1701 for (cs = 0; cs < pdata->num_cs; cs++) {
Ofer Heifetz0da35df2018-08-29 11:56:01 +03001702 struct mtd_info *mtd = nand_to_mtd(&info->host[cs]->chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001703
1704 /*
1705 * The mtd name matches the one used in 'mtdparts' kernel
1706 * parameter. This name cannot be changed or otherwise
1707 * user's mtd partitions configuration would get broken.
1708 */
1709 mtd->name = "pxa3xx_nand-0";
1710 info->cs = cs;
1711 ret = pxa3xx_nand_scan(mtd);
1712 if (ret) {
1713 dev_info(&pdev->dev, "failed to scan nand at cs %d\n",
1714 cs);
1715 continue;
1716 }
1717
Scott Wood2c1b7e12016-05-30 13:57:55 -05001718 if (nand_register(cs, mtd))
1719 continue;
1720
1721 probe_success = 1;
Stefan Roese75659da2015-07-23 10:26:16 +02001722 }
1723
1724 if (!probe_success)
1725 return -ENODEV;
1726
1727 return 0;
1728}
1729
1730/*
1731 * Main initialization routine
1732 */
1733void board_nand_init(void)
1734{
1735 struct pxa3xx_nand_info *info;
1736 struct pxa3xx_nand_host *host;
1737 int ret;
1738
Kevin Smithf6ca2a62016-01-14 16:01:39 +00001739 info = kzalloc(sizeof(*info) +
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +03001740 sizeof(*host) * CONFIG_SYS_MAX_NAND_DEVICE,
1741 GFP_KERNEL);
Stefan Roese75659da2015-07-23 10:26:16 +02001742 if (!info)
1743 return;
1744
Stefan Roese75659da2015-07-23 10:26:16 +02001745 ret = pxa3xx_nand_probe(info);
1746 if (ret)
1747 return;
Stefan Roese75659da2015-07-23 10:26:16 +02001748}