blob: 9dee580ab9c23babbb22f67aff3f49bbb0e6b75f [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/*
Miquel Raynal1f1ae152018-08-16 17:30:07 +02003 * drivers/mtd/nand/raw/pxa3xx_nand.c
Stefan Roese75659da2015-07-23 10:26:16 +02004 *
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>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070015#include <dm/devres.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060016#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060017#include <linux/bug.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070019#include <linux/err.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090020#include <linux/errno.h>
Stefan Roese75659da2015-07-23 10:26:16 +020021#include <asm/io.h>
22#include <asm/arch/cpu.h>
23#include <linux/mtd/mtd.h>
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090024#include <linux/mtd/rawnand.h>
Stefan Roese75659da2015-07-23 10:26:16 +020025#include <linux/types.h>
Shmuel Hazan58983222020-10-29 08:52:20 +020026#include <syscon.h>
27#include <regmap.h>
Shmuel Hazan759349e2020-10-29 08:52:18 +020028#include <dm/uclass.h>
29#include <dm/read.h>
Stefan Roese75659da2015-07-23 10:26:16 +020030
31#include "pxa3xx_nand.h"
32
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +030033DECLARE_GLOBAL_DATA_PTR;
34
Stefan Roese75659da2015-07-23 10:26:16 +020035#define TIMEOUT_DRAIN_FIFO 5 /* in ms */
36#define CHIP_DELAY_TIMEOUT 200
37#define NAND_STOP_DELAY 40
Stefan Roese75659da2015-07-23 10:26:16 +020038
39/*
40 * Define a buffer size for the initial command that detects the flash device:
Ofer Heifetzfdf5b232018-08-29 11:56:00 +030041 * STATUS, READID and PARAM.
42 * ONFI param page is 256 bytes, and there are three redundant copies
43 * to be read. JEDEC param page is 512 bytes, and there are also three
44 * redundant copies to be read.
45 * Hence this buffer should be at least 512 x 3. Let's pick 2048.
Stefan Roese75659da2015-07-23 10:26:16 +020046 */
Ofer Heifetzfdf5b232018-08-29 11:56:00 +030047#define INIT_BUFFER_SIZE 2048
Stefan Roese75659da2015-07-23 10:26:16 +020048
49/* registers and bit definitions */
50#define NDCR (0x00) /* Control register */
51#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
52#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
53#define NDSR (0x14) /* Status Register */
54#define NDPCR (0x18) /* Page Count Register */
55#define NDBDR0 (0x1C) /* Bad Block Register 0 */
56#define NDBDR1 (0x20) /* Bad Block Register 1 */
57#define NDECCCTRL (0x28) /* ECC control */
58#define NDDB (0x40) /* Data Buffer */
59#define NDCB0 (0x48) /* Command Buffer0 */
60#define NDCB1 (0x4C) /* Command Buffer1 */
61#define NDCB2 (0x50) /* Command Buffer2 */
62
63#define NDCR_SPARE_EN (0x1 << 31)
64#define NDCR_ECC_EN (0x1 << 30)
65#define NDCR_DMA_EN (0x1 << 29)
66#define NDCR_ND_RUN (0x1 << 28)
67#define NDCR_DWIDTH_C (0x1 << 27)
68#define NDCR_DWIDTH_M (0x1 << 26)
69#define NDCR_PAGE_SZ (0x1 << 24)
70#define NDCR_NCSX (0x1 << 23)
71#define NDCR_ND_MODE (0x3 << 21)
72#define NDCR_NAND_MODE (0x0)
73#define NDCR_CLR_PG_CNT (0x1 << 20)
Ofer Heifetz531816e2018-08-29 11:56:07 +030074#define NFCV1_NDCR_ARB_CNTL (0x1 << 19)
Stefan Roese75659da2015-07-23 10:26:16 +020075#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
76#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
77
78#define NDCR_RA_START (0x1 << 15)
79#define NDCR_PG_PER_BLK (0x1 << 14)
80#define NDCR_ND_ARB_EN (0x1 << 12)
81#define NDCR_INT_MASK (0xFFF)
82
83#define NDSR_MASK (0xfff)
84#define NDSR_ERR_CNT_OFF (16)
85#define NDSR_ERR_CNT_MASK (0x1f)
86#define NDSR_ERR_CNT(sr) ((sr >> NDSR_ERR_CNT_OFF) & NDSR_ERR_CNT_MASK)
87#define NDSR_RDY (0x1 << 12)
88#define NDSR_FLASH_RDY (0x1 << 11)
89#define NDSR_CS0_PAGED (0x1 << 10)
90#define NDSR_CS1_PAGED (0x1 << 9)
91#define NDSR_CS0_CMDD (0x1 << 8)
92#define NDSR_CS1_CMDD (0x1 << 7)
93#define NDSR_CS0_BBD (0x1 << 6)
94#define NDSR_CS1_BBD (0x1 << 5)
95#define NDSR_UNCORERR (0x1 << 4)
96#define NDSR_CORERR (0x1 << 3)
97#define NDSR_WRDREQ (0x1 << 2)
98#define NDSR_RDDREQ (0x1 << 1)
99#define NDSR_WRCMDREQ (0x1)
100
101#define NDCB0_LEN_OVRD (0x1 << 28)
102#define NDCB0_ST_ROW_EN (0x1 << 26)
103#define NDCB0_AUTO_RS (0x1 << 25)
104#define NDCB0_CSEL (0x1 << 24)
105#define NDCB0_EXT_CMD_TYPE_MASK (0x7 << 29)
106#define NDCB0_EXT_CMD_TYPE(x) (((x) << 29) & NDCB0_EXT_CMD_TYPE_MASK)
107#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
108#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
109#define NDCB0_NC (0x1 << 20)
110#define NDCB0_DBC (0x1 << 19)
111#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
112#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
113#define NDCB0_CMD2_MASK (0xff << 8)
114#define NDCB0_CMD1_MASK (0xff)
115#define NDCB0_ADDR_CYC_SHIFT (16)
116
117#define EXT_CMD_TYPE_DISPATCH 6 /* Command dispatch */
118#define EXT_CMD_TYPE_NAKED_RW 5 /* Naked read or Naked write */
119#define EXT_CMD_TYPE_READ 4 /* Read */
120#define EXT_CMD_TYPE_DISP_WR 4 /* Command dispatch with write */
121#define EXT_CMD_TYPE_FINAL 3 /* Final command */
122#define EXT_CMD_TYPE_LAST_RW 1 /* Last naked read/write */
123#define EXT_CMD_TYPE_MONO 0 /* Monolithic read/write */
124
Shmuel Hazan58983222020-10-29 08:52:20 +0200125/* System control register and bit to enable NAND on some SoCs */
126#define GENCONF_SOC_DEVICE_MUX 0x208
127#define GENCONF_SOC_DEVICE_MUX_NFC_EN BIT(0)
128
Ofer Heifetz4a574aa2018-08-29 11:56:05 +0300129/*
130 * This should be large enough to read 'ONFI' and 'JEDEC'.
131 * Let's use 7 bytes, which is the maximum ID count supported
132 * by the controller (see NDCR_RD_ID_CNT_MASK).
133 */
134#define READ_ID_BYTES 7
135
Stefan Roese75659da2015-07-23 10:26:16 +0200136/* macros for registers read/write */
137#define nand_writel(info, off, val) \
138 writel((val), (info)->mmio_base + (off))
139
140#define nand_readl(info, off) \
141 readl((info)->mmio_base + (off))
142
143/* error code and state */
144enum {
145 ERR_NONE = 0,
146 ERR_DMABUSERR = -1,
147 ERR_SENDCMD = -2,
148 ERR_UNCORERR = -3,
149 ERR_BBERR = -4,
150 ERR_CORERR = -5,
151};
152
153enum {
154 STATE_IDLE = 0,
155 STATE_PREPARED,
156 STATE_CMD_HANDLE,
157 STATE_DMA_READING,
158 STATE_DMA_WRITING,
159 STATE_DMA_DONE,
160 STATE_PIO_READING,
161 STATE_PIO_WRITING,
162 STATE_CMD_DONE,
163 STATE_READY,
164};
165
166enum pxa3xx_nand_variant {
167 PXA3XX_NAND_VARIANT_PXA,
168 PXA3XX_NAND_VARIANT_ARMADA370,
Shmuel Hazan58983222020-10-29 08:52:20 +0200169 PXA3XX_NAND_VARIANT_ARMADA_8K,
Chris Packhambd3ce6f2023-07-10 10:47:34 +1200170 PXA3XX_NAND_VARIANT_AC5,
Stefan Roese75659da2015-07-23 10:26:16 +0200171};
172
173struct pxa3xx_nand_host {
174 struct nand_chip chip;
Stefan Roese75659da2015-07-23 10:26:16 +0200175 void *info_data;
176
177 /* page size of attached chip */
178 int use_ecc;
179 int cs;
180
181 /* calculated from pxa3xx_nand_flash data */
182 unsigned int col_addr_cycles;
183 unsigned int row_addr_cycles;
Stefan Roese75659da2015-07-23 10:26:16 +0200184};
185
186struct pxa3xx_nand_info {
187 struct nand_hw_control controller;
188 struct pxa3xx_nand_platform_data *pdata;
189
190 struct clk *clk;
191 void __iomem *mmio_base;
192 unsigned long mmio_phys;
193 int cmd_complete, dev_ready;
194
195 unsigned int buf_start;
196 unsigned int buf_count;
197 unsigned int buf_size;
198 unsigned int data_buff_pos;
199 unsigned int oob_buff_pos;
200
201 unsigned char *data_buff;
202 unsigned char *oob_buff;
203
204 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
205 unsigned int state;
206
207 /*
208 * This driver supports NFCv1 (as found in PXA SoC)
209 * and NFCv2 (as found in Armada 370/XP SoC).
210 */
211 enum pxa3xx_nand_variant variant;
212
213 int cs;
214 int use_ecc; /* use HW ECC ? */
Miquel Raynal30a016a2018-10-11 17:45:42 +0200215 int force_raw; /* prevent use_ecc to be set */
Stefan Roese75659da2015-07-23 10:26:16 +0200216 int ecc_bch; /* using BCH ECC? */
217 int use_spare; /* use spare ? */
218 int need_wait;
219
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300220 /* Amount of real data per full chunk */
221 unsigned int chunk_size;
222
223 /* Amount of spare data per full chunk */
Stefan Roese75659da2015-07-23 10:26:16 +0200224 unsigned int spare_size;
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300225
226 /* Number of full chunks (i.e chunk_size + spare_size) */
227 unsigned int nfullchunks;
228
229 /*
230 * Total number of chunks. If equal to nfullchunks, then there
231 * are only full chunks. Otherwise, there is one last chunk of
232 * size (last_chunk_size + last_spare_size)
233 */
234 unsigned int ntotalchunks;
235
236 /* Amount of real data in the last chunk */
237 unsigned int last_chunk_size;
238
239 /* Amount of spare data in the last chunk */
240 unsigned int last_spare_size;
241
Stefan Roese75659da2015-07-23 10:26:16 +0200242 unsigned int ecc_size;
243 unsigned int ecc_err_cnt;
244 unsigned int max_bitflips;
245 int retcode;
246
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300247 /*
248 * Variables only valid during command
249 * execution. step_chunk_size and step_spare_size is the
250 * amount of real data and spare data in the current
251 * chunk. cur_chunk is the current chunk being
252 * read/programmed.
253 */
254 unsigned int step_chunk_size;
255 unsigned int step_spare_size;
256 unsigned int cur_chunk;
257
Stefan Roese75659da2015-07-23 10:26:16 +0200258 /* cached register value */
259 uint32_t reg_ndcr;
260 uint32_t ndtr0cs0;
261 uint32_t ndtr1cs0;
262
263 /* generated NDCBx register values */
264 uint32_t ndcb0;
265 uint32_t ndcb1;
266 uint32_t ndcb2;
267 uint32_t ndcb3;
268};
269
270static struct pxa3xx_nand_timing timing[] = {
Konstantin Porotchkina692cde2018-08-29 11:56:16 +0300271 /*
272 * tCH Enable signal hold time
273 * tCS Enable signal setup time
274 * tWH ND_nWE high duration
275 * tWP ND_nWE pulse time
276 * tRH ND_nRE high duration
277 * tRP ND_nRE pulse width
278 * tR ND_nWE high to ND_nRE low for read
279 * tWHR ND_nWE high to ND_nRE low for status read
280 * tAR ND_ALE low to ND_nRE low delay
281 */
Konstantin Porotchkin029be942018-08-29 11:56:14 +0300282 /*ch cs wh wp rh rp r whr ar */
Stefan Roese75659da2015-07-23 10:26:16 +0200283 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
284 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
285 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
286 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
Konstantin Porotchkin029be942018-08-29 11:56:14 +0300287 { 5, 20, 10, 12, 10, 12, 25000, 60, 10, },
Stefan Roese75659da2015-07-23 10:26:16 +0200288};
289
290static struct pxa3xx_nand_flash builtin_flash_types[] = {
Konstantin Porotchkina692cde2018-08-29 11:56:16 +0300291 /*
292 * chip_id
293 * flash_width Width of Flash memory (DWIDTH_M)
294 * dfc_width Width of flash controller(DWIDTH_C)
295 * *timing
296 * http://www.linux-mtd.infradead.org/nand-data/nanddata.html
297 */
Stefan Roese75659da2015-07-23 10:26:16 +0200298 { 0x46ec, 16, 16, &timing[1] },
299 { 0xdaec, 8, 8, &timing[1] },
300 { 0xd7ec, 8, 8, &timing[1] },
301 { 0xa12c, 8, 8, &timing[2] },
302 { 0xb12c, 16, 16, &timing[2] },
303 { 0xdc2c, 8, 8, &timing[2] },
304 { 0xcc2c, 16, 16, &timing[2] },
305 { 0xba20, 16, 16, &timing[3] },
Konstantin Porotchkin029be942018-08-29 11:56:14 +0300306 { 0xda98, 8, 8, &timing[4] },
Stefan Roese75659da2015-07-23 10:26:16 +0200307};
308
Sean Nyekjaera12a8e82017-11-22 13:39:08 +0100309#ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
Stefan Roese75659da2015-07-23 10:26:16 +0200310static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
311static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
312
313static struct nand_bbt_descr bbt_main_descr = {
314 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
315 | NAND_BBT_2BIT | NAND_BBT_VERSION,
316 .offs = 8,
317 .len = 6,
318 .veroffs = 14,
319 .maxblocks = 8, /* Last 8 blocks in each chip */
320 .pattern = bbt_pattern
321};
322
323static struct nand_bbt_descr bbt_mirror_descr = {
324 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
325 | NAND_BBT_2BIT | NAND_BBT_VERSION,
326 .offs = 8,
327 .len = 6,
328 .veroffs = 14,
329 .maxblocks = 8, /* Last 8 blocks in each chip */
330 .pattern = bbt_mirror_pattern
331};
Sean Nyekjaera12a8e82017-11-22 13:39:08 +0100332#endif
Stefan Roese75659da2015-07-23 10:26:16 +0200333
Chris Packham03085ca2022-08-25 16:59:49 +1200334struct marvell_hw_ecc_layout {
335 int page_size;
336 int strength;
337 unsigned int ecc_size;
338 unsigned int nfullchunks;
339 unsigned int chunk_size;
340 unsigned int spare_size;
341 unsigned int last_chunk_size;
342 unsigned int last_spare_size;
Stefan Roese75659da2015-07-23 10:26:16 +0200343};
344
Chris Packham03085ca2022-08-25 16:59:49 +1200345static const struct marvell_hw_ecc_layout nfc_layouts[] = {
346 /* page_size strength ecc_size nfullchunks chunk_size spare_size last_chunk last_spare */
347 { 512, 1, 8, 1, 512, 8, 0, 0 },
348 { 2048, 1, 24, 1, 2048, 40, 0, 0 },
Konstantin Porotchkine0e232e2018-08-29 11:56:17 +0300349
Chris Packham03085ca2022-08-25 16:59:49 +1200350 { 2048, 4, 32, 1, 2048, 32, 0, 0 },
351 { 2048, 8, 32, 1, 1024, 0, 1024, 32 },
352 { 2048, 12, 32, 2, 704, 0, 640, 0 },
353 { 2048, 16, 32, 4, 512, 0, 0, 32 },
354 { 4096, 4, 32, 2, 2048, 32, 0, 0 },
355 { 4096, 8, 32, 4, 1024, 0, 0, 64 },
356 { 4096, 12, 32, 5, 704, 0, 576, 32 },
357 { 4096, 16, 32, 8, 512, 0, 0, 32 },
Konstantin Porotchkine0e232e2018-08-29 11:56:17 +0300358
Chris Packham03085ca2022-08-25 16:59:49 +1200359 { 8192, 4, 32, 4, 2048, 32, 0, 0 },
360 { 8192, 8, 32, 8, 1024, 0, 0, 160 },
361 { 8192, 12, 32, 11, 704, 0, 448, 64 },
362 { 8192, 16, 32, 16, 512, 0, 0, 32 },
363 { },
Konstantin Porotchkine0e232e2018-08-29 11:56:17 +0300364};
365
Chris Packham03085ca2022-08-25 16:59:49 +1200366static struct nand_ecclayout ecc_layout_empty = {
367 .eccbytes = 0,
368 .eccpos = { },
Stefan Roese75659da2015-07-23 10:26:16 +0200369 .oobfree = { }
370};
371
372#define NDTR0_tCH(c) (min((c), 7) << 19)
373#define NDTR0_tCS(c) (min((c), 7) << 16)
374#define NDTR0_tWH(c) (min((c), 7) << 11)
375#define NDTR0_tWP(c) (min((c), 7) << 8)
376#define NDTR0_tRH(c) (min((c), 7) << 3)
377#define NDTR0_tRP(c) (min((c), 7) << 0)
378
379#define NDTR1_tR(c) (min((c), 65535) << 16)
380#define NDTR1_tWHR(c) (min((c), 15) << 4)
381#define NDTR1_tAR(c) (min((c), 15) << 0)
382
383/* convert nano-seconds to nand flash controller clock cycles */
384#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
385
Shmuel Hazan759349e2020-10-29 08:52:18 +0200386static const struct udevice_id pxa3xx_nand_dt_ids[] = {
387 {
Pali Rohárc96bc1d2022-07-27 14:47:35 +0200388 .compatible = "marvell,armada370-nand-controller",
Shmuel Hazan759349e2020-10-29 08:52:18 +0200389 .data = PXA3XX_NAND_VARIANT_ARMADA370,
390 },
Shmuel Hazan58983222020-10-29 08:52:20 +0200391 {
392 .compatible = "marvell,armada-8k-nand-controller",
393 .data = PXA3XX_NAND_VARIANT_ARMADA_8K,
394 },
Chris Packhambd3ce6f2023-07-10 10:47:34 +1200395 {
396 .compatible = "marvell,mvebu-ac5-pxa3xx-nand",
397 .data = PXA3XX_NAND_VARIANT_AC5,
398 },
Shmuel Hazan759349e2020-10-29 08:52:18 +0200399 {}
400};
401
Shmuel Hazan58983222020-10-29 08:52:20 +0200402static enum pxa3xx_nand_variant pxa3xx_nand_get_variant(struct udevice *dev)
Stefan Roese75659da2015-07-23 10:26:16 +0200403{
Shmuel Hazan58983222020-10-29 08:52:20 +0200404 return dev_get_driver_data(dev);
Stefan Roese75659da2015-07-23 10:26:16 +0200405}
406
407static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
408 const struct pxa3xx_nand_timing *t)
409{
410 struct pxa3xx_nand_info *info = host->info_data;
411 unsigned long nand_clk = mvebu_get_nand_clock();
412 uint32_t ndtr0, ndtr1;
413
414 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
415 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
416 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
417 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
418 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
419 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
420
421 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
422 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
423 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
424
425 info->ndtr0cs0 = ndtr0;
426 info->ndtr1cs0 = ndtr1;
427 nand_writel(info, NDTR0CS0, ndtr0);
428 nand_writel(info, NDTR1CS0, ndtr1);
429}
430
431static void pxa3xx_nand_set_sdr_timing(struct pxa3xx_nand_host *host,
432 const struct nand_sdr_timings *t)
433{
434 struct pxa3xx_nand_info *info = host->info_data;
435 struct nand_chip *chip = &host->chip;
436 unsigned long nand_clk = mvebu_get_nand_clock();
437 uint32_t ndtr0, ndtr1;
438
439 u32 tCH_min = DIV_ROUND_UP(t->tCH_min, 1000);
440 u32 tCS_min = DIV_ROUND_UP(t->tCS_min, 1000);
441 u32 tWH_min = DIV_ROUND_UP(t->tWH_min, 1000);
Ofer Heifetz8f8d4582018-08-29 11:56:02 +0300442 u32 tWP_min = DIV_ROUND_UP(t->tWC_min - t->tWH_min, 1000);
Stefan Roese75659da2015-07-23 10:26:16 +0200443 u32 tREH_min = DIV_ROUND_UP(t->tREH_min, 1000);
Ofer Heifetz8f8d4582018-08-29 11:56:02 +0300444 u32 tRP_min = DIV_ROUND_UP(t->tRC_min - t->tREH_min, 1000);
Stefan Roese75659da2015-07-23 10:26:16 +0200445 u32 tR = chip->chip_delay * 1000;
446 u32 tWHR_min = DIV_ROUND_UP(t->tWHR_min, 1000);
447 u32 tAR_min = DIV_ROUND_UP(t->tAR_min, 1000);
448
449 /* fallback to a default value if tR = 0 */
450 if (!tR)
451 tR = 20000;
452
453 ndtr0 = NDTR0_tCH(ns2cycle(tCH_min, nand_clk)) |
454 NDTR0_tCS(ns2cycle(tCS_min, nand_clk)) |
455 NDTR0_tWH(ns2cycle(tWH_min, nand_clk)) |
456 NDTR0_tWP(ns2cycle(tWP_min, nand_clk)) |
457 NDTR0_tRH(ns2cycle(tREH_min, nand_clk)) |
458 NDTR0_tRP(ns2cycle(tRP_min, nand_clk));
459
460 ndtr1 = NDTR1_tR(ns2cycle(tR, nand_clk)) |
461 NDTR1_tWHR(ns2cycle(tWHR_min, nand_clk)) |
462 NDTR1_tAR(ns2cycle(tAR_min, nand_clk));
463
464 info->ndtr0cs0 = ndtr0;
465 info->ndtr1cs0 = ndtr1;
466 nand_writel(info, NDTR0CS0, ndtr0);
467 nand_writel(info, NDTR1CS0, ndtr1);
468}
469
470static int pxa3xx_nand_init_timings(struct pxa3xx_nand_host *host)
471{
472 const struct nand_sdr_timings *timings;
473 struct nand_chip *chip = &host->chip;
474 struct pxa3xx_nand_info *info = host->info_data;
475 const struct pxa3xx_nand_flash *f = NULL;
Ofer Heifetz0da35df2018-08-29 11:56:01 +0300476 struct mtd_info *mtd = nand_to_mtd(&host->chip);
Stefan Roese75659da2015-07-23 10:26:16 +0200477 int mode, id, ntypes, i;
478
479 mode = onfi_get_async_timing_mode(chip);
480 if (mode == ONFI_TIMING_MODE_UNKNOWN) {
481 ntypes = ARRAY_SIZE(builtin_flash_types);
482
Ofer Heifetz0da35df2018-08-29 11:56:01 +0300483 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Stefan Roese75659da2015-07-23 10:26:16 +0200484
Ofer Heifetz0da35df2018-08-29 11:56:01 +0300485 id = chip->read_byte(mtd);
486 id |= chip->read_byte(mtd) << 0x8;
Stefan Roese75659da2015-07-23 10:26:16 +0200487
488 for (i = 0; i < ntypes; i++) {
489 f = &builtin_flash_types[i];
490
491 if (f->chip_id == id)
492 break;
493 }
494
495 if (i == ntypes) {
Sean Andersonc6302f02020-09-15 10:44:40 -0400496 dev_err(mtd->dev, "Error: timings not found\n");
Stefan Roese75659da2015-07-23 10:26:16 +0200497 return -EINVAL;
498 }
499
500 pxa3xx_nand_set_timing(host, f->timing);
501
502 if (f->flash_width == 16) {
503 info->reg_ndcr |= NDCR_DWIDTH_M;
504 chip->options |= NAND_BUSWIDTH_16;
505 }
506
507 info->reg_ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
508 } else {
509 mode = fls(mode) - 1;
510 if (mode < 0)
511 mode = 0;
512
Chris Packhambd3ce6f2023-07-10 10:47:34 +1200513 if (info->variant == PXA3XX_NAND_VARIANT_AC5)
514 mode = min(mode, 3);
515
Stefan Roese75659da2015-07-23 10:26:16 +0200516 timings = onfi_async_timing_mode_to_sdr_timings(mode);
517 if (IS_ERR(timings))
518 return PTR_ERR(timings);
519
520 pxa3xx_nand_set_sdr_timing(host, timings);
521 }
522
523 return 0;
524}
525
Stefan Roese75659da2015-07-23 10:26:16 +0200526/**
Vagrant Cascadianbeb288b2015-11-24 14:46:24 -0800527 * NOTE: it is a must to set ND_RUN first, then write
Stefan Roese75659da2015-07-23 10:26:16 +0200528 * command buffer, otherwise, it does not work.
529 * We enable all the interrupt at the same time, and
530 * let pxa3xx_nand_irq to handle all logic.
531 */
532static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
533{
534 uint32_t ndcr;
535
536 ndcr = info->reg_ndcr;
537
538 if (info->use_ecc) {
539 ndcr |= NDCR_ECC_EN;
540 if (info->ecc_bch)
541 nand_writel(info, NDECCCTRL, 0x1);
542 } else {
543 ndcr &= ~NDCR_ECC_EN;
544 if (info->ecc_bch)
545 nand_writel(info, NDECCCTRL, 0x0);
546 }
547
548 ndcr &= ~NDCR_DMA_EN;
549
550 if (info->use_spare)
551 ndcr |= NDCR_SPARE_EN;
552 else
553 ndcr &= ~NDCR_SPARE_EN;
554
555 ndcr |= NDCR_ND_RUN;
556
557 /* clear status bits and run */
Stefan Roese75659da2015-07-23 10:26:16 +0200558 nand_writel(info, NDSR, NDSR_MASK);
Ofer Heifetzd92d8992018-08-29 11:56:03 +0300559 nand_writel(info, NDCR, 0);
Stefan Roese75659da2015-07-23 10:26:16 +0200560 nand_writel(info, NDCR, ndcr);
561}
562
563static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
564{
565 uint32_t ndcr;
566
567 ndcr = nand_readl(info, NDCR);
568 nand_writel(info, NDCR, ndcr | int_mask);
569}
570
571static void drain_fifo(struct pxa3xx_nand_info *info, void *data, int len)
572{
Miquel Raynal30a016a2018-10-11 17:45:42 +0200573 if (info->ecc_bch && !info->force_raw) {
Stefan Roese75659da2015-07-23 10:26:16 +0200574 u32 ts;
575
576 /*
577 * According to the datasheet, when reading from NDDB
578 * with BCH enabled, after each 32 bytes reads, we
579 * have to make sure that the NDSR.RDDREQ bit is set.
580 *
581 * Drain the FIFO 8 32 bits reads at a time, and skip
582 * the polling on the last read.
583 */
584 while (len > 8) {
585 readsl(info->mmio_base + NDDB, data, 8);
586
587 ts = get_timer(0);
588 while (!(nand_readl(info, NDSR) & NDSR_RDDREQ)) {
589 if (get_timer(ts) > TIMEOUT_DRAIN_FIFO) {
Sean Andersonc6302f02020-09-15 10:44:40 -0400590 dev_err(info->controller.active->mtd.dev,
Stefan Roese75659da2015-07-23 10:26:16 +0200591 "Timeout on RDDREQ while draining the FIFO\n");
592 return;
593 }
594 }
595
596 data += 32;
597 len -= 8;
598 }
599 }
600
601 readsl(info->mmio_base + NDDB, data, len);
602}
603
604static void handle_data_pio(struct pxa3xx_nand_info *info)
605{
Miquel Raynal30a016a2018-10-11 17:45:42 +0200606 int data_len = info->step_chunk_size;
607
608 /*
609 * In raw mode, include the spare area and the ECC bytes that are not
610 * consumed by the controller in the data section. Do not reorganize
611 * here, do it in the ->read_page_raw() handler instead.
612 */
613 if (info->force_raw)
614 data_len += info->step_spare_size + info->ecc_size;
615
Stefan Roese75659da2015-07-23 10:26:16 +0200616 switch (info->state) {
617 case STATE_PIO_WRITING:
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300618 if (info->step_chunk_size)
619 writesl(info->mmio_base + NDDB,
620 info->data_buff + info->data_buff_pos,
Miquel Raynal30a016a2018-10-11 17:45:42 +0200621 DIV_ROUND_UP(data_len, 4));
Stefan Roese75659da2015-07-23 10:26:16 +0200622
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300623 if (info->step_spare_size)
Stefan Roese75659da2015-07-23 10:26:16 +0200624 writesl(info->mmio_base + NDDB,
625 info->oob_buff + info->oob_buff_pos,
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300626 DIV_ROUND_UP(info->step_spare_size, 4));
Stefan Roese75659da2015-07-23 10:26:16 +0200627 break;
628 case STATE_PIO_READING:
Baruch Siach9167e4d2020-04-05 19:19:31 +0300629 if (data_len)
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300630 drain_fifo(info,
631 info->data_buff + info->data_buff_pos,
Miquel Raynal30a016a2018-10-11 17:45:42 +0200632 DIV_ROUND_UP(data_len, 4));
633
634 if (info->force_raw)
635 break;
Stefan Roese75659da2015-07-23 10:26:16 +0200636
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300637 if (info->step_spare_size)
Stefan Roese75659da2015-07-23 10:26:16 +0200638 drain_fifo(info,
639 info->oob_buff + info->oob_buff_pos,
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300640 DIV_ROUND_UP(info->step_spare_size, 4));
Stefan Roese75659da2015-07-23 10:26:16 +0200641 break;
642 default:
Sean Andersonc6302f02020-09-15 10:44:40 -0400643 dev_err(info->controller.active->mtd.dev,
644 "%s: invalid state %d\n", __func__, info->state);
Stefan Roese75659da2015-07-23 10:26:16 +0200645 BUG();
646 }
647
648 /* Update buffer pointers for multi-page read/write */
Miquel Raynal30a016a2018-10-11 17:45:42 +0200649 info->data_buff_pos += data_len;
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300650 info->oob_buff_pos += info->step_spare_size;
Stefan Roese75659da2015-07-23 10:26:16 +0200651}
652
653static void pxa3xx_nand_irq_thread(struct pxa3xx_nand_info *info)
654{
655 handle_data_pio(info);
656
657 info->state = STATE_CMD_DONE;
658 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
659}
660
661static irqreturn_t pxa3xx_nand_irq(struct pxa3xx_nand_info *info)
662{
663 unsigned int status, is_completed = 0, is_ready = 0;
664 unsigned int ready, cmd_done;
665 irqreturn_t ret = IRQ_HANDLED;
666
667 if (info->cs == 0) {
668 ready = NDSR_FLASH_RDY;
669 cmd_done = NDSR_CS0_CMDD;
670 } else {
671 ready = NDSR_RDY;
672 cmd_done = NDSR_CS1_CMDD;
673 }
674
David Sniatkiwicz2087f7e2018-08-29 11:56:18 +0300675 /* TODO - find out why we need the delay during write operation. */
676 ndelay(1);
677
Stefan Roese75659da2015-07-23 10:26:16 +0200678 status = nand_readl(info, NDSR);
679
680 if (status & NDSR_UNCORERR)
681 info->retcode = ERR_UNCORERR;
682 if (status & NDSR_CORERR) {
683 info->retcode = ERR_CORERR;
Shmuel Hazan58983222020-10-29 08:52:20 +0200684 if ((info->variant == PXA3XX_NAND_VARIANT_ARMADA370 ||
685 info->variant == PXA3XX_NAND_VARIANT_ARMADA_8K) &&
Stefan Roese75659da2015-07-23 10:26:16 +0200686 info->ecc_bch)
687 info->ecc_err_cnt = NDSR_ERR_CNT(status);
688 else
689 info->ecc_err_cnt = 1;
690
691 /*
692 * Each chunk composing a page is corrected independently,
693 * and we need to store maximum number of corrected bitflips
694 * to return it to the MTD layer in ecc.read_page().
695 */
696 info->max_bitflips = max_t(unsigned int,
697 info->max_bitflips,
698 info->ecc_err_cnt);
699 }
700 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
701 info->state = (status & NDSR_RDDREQ) ?
702 STATE_PIO_READING : STATE_PIO_WRITING;
703 /* Call the IRQ thread in U-Boot directly */
704 pxa3xx_nand_irq_thread(info);
705 return 0;
706 }
707 if (status & cmd_done) {
708 info->state = STATE_CMD_DONE;
709 is_completed = 1;
710 }
711 if (status & ready) {
712 info->state = STATE_READY;
713 is_ready = 1;
714 }
715
Ofer Heifetzde323162018-08-29 11:56:04 +0300716 /*
717 * Clear all status bit before issuing the next command, which
718 * can and will alter the status bits and will deserve a new
719 * interrupt on its own. This lets the controller exit the IRQ
720 */
721 nand_writel(info, NDSR, status);
722
Stefan Roese75659da2015-07-23 10:26:16 +0200723 if (status & NDSR_WRCMDREQ) {
Stefan Roese75659da2015-07-23 10:26:16 +0200724 status &= ~NDSR_WRCMDREQ;
725 info->state = STATE_CMD_HANDLE;
726
727 /*
728 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
729 * must be loaded by writing directly either 12 or 16
730 * bytes directly to NDCB0, four bytes at a time.
731 *
732 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
733 * but each NDCBx register can be read.
734 */
735 nand_writel(info, NDCB0, info->ndcb0);
736 nand_writel(info, NDCB0, info->ndcb1);
737 nand_writel(info, NDCB0, info->ndcb2);
738
739 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
Shmuel Hazan58983222020-10-29 08:52:20 +0200740 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 ||
Chris Packhambd3ce6f2023-07-10 10:47:34 +1200741 info->variant == PXA3XX_NAND_VARIANT_ARMADA_8K ||
742 info->variant == PXA3XX_NAND_VARIANT_AC5)
Stefan Roese75659da2015-07-23 10:26:16 +0200743 nand_writel(info, NDCB0, info->ndcb3);
744 }
745
Stefan Roese75659da2015-07-23 10:26:16 +0200746 if (is_completed)
747 info->cmd_complete = 1;
748 if (is_ready)
749 info->dev_ready = 1;
750
751 return ret;
752}
753
754static inline int is_buf_blank(uint8_t *buf, size_t len)
755{
756 for (; len > 0; len--)
757 if (*buf++ != 0xff)
758 return 0;
759 return 1;
760}
761
762static void set_command_address(struct pxa3xx_nand_info *info,
763 unsigned int page_size, uint16_t column, int page_addr)
764{
765 /* small page addr setting */
Konstantin Porotchkin06f9b6b2018-08-29 11:56:15 +0300766 if (page_size < info->chunk_size) {
Stefan Roese75659da2015-07-23 10:26:16 +0200767 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
768 | (column & 0xFF);
769
770 info->ndcb2 = 0;
771 } else {
772 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
773 | (column & 0xFFFF);
774
775 if (page_addr & 0xFF0000)
776 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
777 else
778 info->ndcb2 = 0;
779 }
780}
781
782static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
783{
784 struct pxa3xx_nand_host *host = info->host[info->cs];
Ofer Heifetz0da35df2018-08-29 11:56:01 +0300785 struct mtd_info *mtd = nand_to_mtd(&host->chip);
Stefan Roese75659da2015-07-23 10:26:16 +0200786
787 /* reset data and oob column point to handle data */
788 info->buf_start = 0;
789 info->buf_count = 0;
Stefan Roese75659da2015-07-23 10:26:16 +0200790 info->data_buff_pos = 0;
791 info->oob_buff_pos = 0;
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300792 info->step_chunk_size = 0;
793 info->step_spare_size = 0;
794 info->cur_chunk = 0;
Stefan Roese75659da2015-07-23 10:26:16 +0200795 info->use_ecc = 0;
796 info->use_spare = 1;
797 info->retcode = ERR_NONE;
798 info->ecc_err_cnt = 0;
799 info->ndcb3 = 0;
800 info->need_wait = 0;
801
802 switch (command) {
803 case NAND_CMD_READ0:
Boris Brezillona558a392018-08-29 11:56:12 +0300804 case NAND_CMD_READOOB:
Stefan Roese75659da2015-07-23 10:26:16 +0200805 case NAND_CMD_PAGEPROG:
Miquel Raynal30a016a2018-10-11 17:45:42 +0200806 if (!info->force_raw)
807 info->use_ecc = 1;
Stefan Roese75659da2015-07-23 10:26:16 +0200808 break;
809 case NAND_CMD_PARAM:
810 info->use_spare = 0;
811 break;
812 default:
813 info->ndcb1 = 0;
814 info->ndcb2 = 0;
815 break;
816 }
817
818 /*
819 * If we are about to issue a read command, or about to set
820 * the write address, then clean the data buffer.
821 */
822 if (command == NAND_CMD_READ0 ||
823 command == NAND_CMD_READOOB ||
824 command == NAND_CMD_SEQIN) {
825 info->buf_count = mtd->writesize + mtd->oobsize;
826 memset(info->data_buff, 0xFF, info->buf_count);
827 }
828}
829
830static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
831 int ext_cmd_type, uint16_t column, int page_addr)
832{
833 int addr_cycle, exec_cmd;
834 struct pxa3xx_nand_host *host;
835 struct mtd_info *mtd;
836
837 host = info->host[info->cs];
Ofer Heifetz0da35df2018-08-29 11:56:01 +0300838 mtd = nand_to_mtd(&host->chip);
Stefan Roese75659da2015-07-23 10:26:16 +0200839 addr_cycle = 0;
840 exec_cmd = 1;
841
842 if (info->cs != 0)
843 info->ndcb0 = NDCB0_CSEL;
844 else
845 info->ndcb0 = 0;
846
847 if (command == NAND_CMD_SEQIN)
848 exec_cmd = 0;
849
850 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
851 + host->col_addr_cycles);
852
853 switch (command) {
854 case NAND_CMD_READOOB:
855 case NAND_CMD_READ0:
856 info->buf_start = column;
857 info->ndcb0 |= NDCB0_CMD_TYPE(0)
858 | addr_cycle
859 | NAND_CMD_READ0;
860
861 if (command == NAND_CMD_READOOB)
862 info->buf_start += mtd->writesize;
863
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300864 if (info->cur_chunk < info->nfullchunks) {
865 info->step_chunk_size = info->chunk_size;
866 info->step_spare_size = info->spare_size;
867 } else {
868 info->step_chunk_size = info->last_chunk_size;
869 info->step_spare_size = info->last_spare_size;
870 }
871
Stefan Roese75659da2015-07-23 10:26:16 +0200872 /*
873 * Multiple page read needs an 'extended command type' field,
874 * which is either naked-read or last-read according to the
875 * state.
876 */
Miquel Raynal30a016a2018-10-11 17:45:42 +0200877 if (info->force_raw) {
878 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8) |
879 NDCB0_LEN_OVRD |
880 NDCB0_EXT_CMD_TYPE(ext_cmd_type);
881 info->ndcb3 = info->step_chunk_size +
882 info->step_spare_size + info->ecc_size;
883 } else if (mtd->writesize == info->chunk_size) {
Stefan Roese75659da2015-07-23 10:26:16 +0200884 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Konstantin Porotchkin06f9b6b2018-08-29 11:56:15 +0300885 } else if (mtd->writesize > info->chunk_size) {
Stefan Roese75659da2015-07-23 10:26:16 +0200886 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
887 | NDCB0_LEN_OVRD
888 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300889 info->ndcb3 = info->step_chunk_size +
890 info->step_spare_size;
Stefan Roese75659da2015-07-23 10:26:16 +0200891 }
892
893 set_command_address(info, mtd->writesize, column, page_addr);
894 break;
895
896 case NAND_CMD_SEQIN:
897
898 info->buf_start = column;
899 set_command_address(info, mtd->writesize, 0, page_addr);
900
901 /*
902 * Multiple page programming needs to execute the initial
903 * SEQIN command that sets the page address.
904 */
Konstantin Porotchkin06f9b6b2018-08-29 11:56:15 +0300905 if (mtd->writesize > info->chunk_size) {
Stefan Roese75659da2015-07-23 10:26:16 +0200906 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
907 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
908 | addr_cycle
909 | command;
Stefan Roese75659da2015-07-23 10:26:16 +0200910 exec_cmd = 1;
911 }
912 break;
913
914 case NAND_CMD_PAGEPROG:
915 if (is_buf_blank(info->data_buff,
916 (mtd->writesize + mtd->oobsize))) {
917 exec_cmd = 0;
918 break;
919 }
920
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300921 if (info->cur_chunk < info->nfullchunks) {
922 info->step_chunk_size = info->chunk_size;
923 info->step_spare_size = info->spare_size;
924 } else {
925 info->step_chunk_size = info->last_chunk_size;
926 info->step_spare_size = info->last_spare_size;
927 }
928
Stefan Roese75659da2015-07-23 10:26:16 +0200929 /* Second command setting for large pages */
Konstantin Porotchkin06f9b6b2018-08-29 11:56:15 +0300930 if (mtd->writesize > info->chunk_size) {
Stefan Roese75659da2015-07-23 10:26:16 +0200931 /*
932 * Multiple page write uses the 'extended command'
933 * field. This can be used to issue a command dispatch
934 * or a naked-write depending on the current stage.
935 */
936 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
937 | NDCB0_LEN_OVRD
938 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300939 info->ndcb3 = info->step_chunk_size +
940 info->step_spare_size;
Stefan Roese75659da2015-07-23 10:26:16 +0200941
942 /*
943 * This is the command dispatch that completes a chunked
944 * page program operation.
945 */
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300946 if (info->cur_chunk == info->ntotalchunks) {
Stefan Roese75659da2015-07-23 10:26:16 +0200947 info->ndcb0 = NDCB0_CMD_TYPE(0x1)
948 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
949 | command;
950 info->ndcb1 = 0;
951 info->ndcb2 = 0;
952 info->ndcb3 = 0;
953 }
954 } else {
955 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
956 | NDCB0_AUTO_RS
957 | NDCB0_ST_ROW_EN
958 | NDCB0_DBC
959 | (NAND_CMD_PAGEPROG << 8)
960 | NAND_CMD_SEQIN
961 | addr_cycle;
962 }
963 break;
964
965 case NAND_CMD_PARAM:
Ofer Heifetzfdf5b232018-08-29 11:56:00 +0300966 info->buf_count = INIT_BUFFER_SIZE;
Stefan Roese75659da2015-07-23 10:26:16 +0200967 info->ndcb0 |= NDCB0_CMD_TYPE(0)
968 | NDCB0_ADDR_CYC(1)
969 | NDCB0_LEN_OVRD
970 | command;
971 info->ndcb1 = (column & 0xFF);
Ofer Heifetzfdf5b232018-08-29 11:56:00 +0300972 info->ndcb3 = INIT_BUFFER_SIZE;
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300973 info->step_chunk_size = INIT_BUFFER_SIZE;
Stefan Roese75659da2015-07-23 10:26:16 +0200974 break;
975
976 case NAND_CMD_READID:
Ofer Heifetz4a574aa2018-08-29 11:56:05 +0300977 info->buf_count = READ_ID_BYTES;
Stefan Roese75659da2015-07-23 10:26:16 +0200978 info->ndcb0 |= NDCB0_CMD_TYPE(3)
979 | NDCB0_ADDR_CYC(1)
980 | command;
981 info->ndcb1 = (column & 0xFF);
982
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300983 info->step_chunk_size = 8;
Stefan Roese75659da2015-07-23 10:26:16 +0200984 break;
985 case NAND_CMD_STATUS:
986 info->buf_count = 1;
987 info->ndcb0 |= NDCB0_CMD_TYPE(4)
988 | NDCB0_ADDR_CYC(1)
989 | command;
990
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300991 info->step_chunk_size = 8;
Stefan Roese75659da2015-07-23 10:26:16 +0200992 break;
993
994 case NAND_CMD_ERASE1:
995 info->ndcb0 |= NDCB0_CMD_TYPE(2)
996 | NDCB0_AUTO_RS
997 | NDCB0_ADDR_CYC(3)
998 | NDCB0_DBC
999 | (NAND_CMD_ERASE2 << 8)
1000 | NAND_CMD_ERASE1;
1001 info->ndcb1 = page_addr;
1002 info->ndcb2 = 0;
1003
1004 break;
1005 case NAND_CMD_RESET:
1006 info->ndcb0 |= NDCB0_CMD_TYPE(5)
1007 | command;
1008
1009 break;
1010
1011 case NAND_CMD_ERASE2:
1012 exec_cmd = 0;
1013 break;
1014
1015 default:
1016 exec_cmd = 0;
Sean Andersonc6302f02020-09-15 10:44:40 -04001017 dev_err(mtd->dev, "non-supported command %x\n",
Stefan Roese75659da2015-07-23 10:26:16 +02001018 command);
1019 break;
1020 }
1021
1022 return exec_cmd;
1023}
1024
1025static void nand_cmdfunc(struct mtd_info *mtd, unsigned command,
1026 int column, int page_addr)
1027{
Scott Wood17fed142016-05-30 13:57:56 -05001028 struct nand_chip *chip = mtd_to_nand(mtd);
1029 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001030 struct pxa3xx_nand_info *info = host->info_data;
1031 int exec_cmd;
1032
1033 /*
1034 * if this is a x16 device ,then convert the input
1035 * "byte" address into a "word" address appropriate
1036 * for indexing a word-oriented device
1037 */
1038 if (info->reg_ndcr & NDCR_DWIDTH_M)
1039 column /= 2;
1040
1041 /*
1042 * There may be different NAND chip hooked to
1043 * different chip select, so check whether
1044 * chip select has been changed, if yes, reset the timing
1045 */
1046 if (info->cs != host->cs) {
1047 info->cs = host->cs;
1048 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1049 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
1050 }
1051
1052 prepare_start_command(info, command);
1053
1054 info->state = STATE_PREPARED;
1055 exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
1056
1057 if (exec_cmd) {
1058 u32 ts;
1059
1060 info->cmd_complete = 0;
1061 info->dev_ready = 0;
1062 info->need_wait = 1;
1063 pxa3xx_nand_start(info);
1064
1065 ts = get_timer(0);
1066 while (1) {
1067 u32 status;
1068
1069 status = nand_readl(info, NDSR);
1070 if (status)
1071 pxa3xx_nand_irq(info);
1072
1073 if (info->cmd_complete)
1074 break;
1075
1076 if (get_timer(ts) > CHIP_DELAY_TIMEOUT) {
Sean Andersonc6302f02020-09-15 10:44:40 -04001077 dev_err(mtd->dev, "Wait timeout!!!\n");
Stefan Roese75659da2015-07-23 10:26:16 +02001078 return;
1079 }
1080 }
1081 }
1082 info->state = STATE_IDLE;
1083}
1084
1085static void nand_cmdfunc_extended(struct mtd_info *mtd,
1086 const unsigned command,
1087 int column, int page_addr)
1088{
Scott Wood17fed142016-05-30 13:57:56 -05001089 struct nand_chip *chip = mtd_to_nand(mtd);
1090 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001091 struct pxa3xx_nand_info *info = host->info_data;
1092 int exec_cmd, ext_cmd_type;
1093
1094 /*
1095 * if this is a x16 device then convert the input
1096 * "byte" address into a "word" address appropriate
1097 * for indexing a word-oriented device
1098 */
1099 if (info->reg_ndcr & NDCR_DWIDTH_M)
1100 column /= 2;
1101
1102 /*
1103 * There may be different NAND chip hooked to
1104 * different chip select, so check whether
1105 * chip select has been changed, if yes, reset the timing
1106 */
1107 if (info->cs != host->cs) {
1108 info->cs = host->cs;
1109 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1110 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
1111 }
1112
1113 /* Select the extended command for the first command */
1114 switch (command) {
1115 case NAND_CMD_READ0:
1116 case NAND_CMD_READOOB:
1117 ext_cmd_type = EXT_CMD_TYPE_MONO;
1118 break;
1119 case NAND_CMD_SEQIN:
1120 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
1121 break;
1122 case NAND_CMD_PAGEPROG:
1123 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
1124 break;
1125 default:
1126 ext_cmd_type = 0;
1127 break;
1128 }
1129
1130 prepare_start_command(info, command);
1131
1132 /*
1133 * Prepare the "is ready" completion before starting a command
1134 * transaction sequence. If the command is not executed the
1135 * completion will be completed, see below.
1136 *
1137 * We can do that inside the loop because the command variable
1138 * is invariant and thus so is the exec_cmd.
1139 */
1140 info->need_wait = 1;
1141 info->dev_ready = 0;
1142
1143 do {
1144 u32 ts;
1145
1146 info->state = STATE_PREPARED;
1147 exec_cmd = prepare_set_command(info, command, ext_cmd_type,
1148 column, page_addr);
1149 if (!exec_cmd) {
1150 info->need_wait = 0;
1151 info->dev_ready = 1;
1152 break;
1153 }
1154
1155 info->cmd_complete = 0;
1156 pxa3xx_nand_start(info);
1157
1158 ts = get_timer(0);
1159 while (1) {
1160 u32 status;
1161
1162 status = nand_readl(info, NDSR);
1163 if (status)
1164 pxa3xx_nand_irq(info);
1165
1166 if (info->cmd_complete)
1167 break;
1168
1169 if (get_timer(ts) > CHIP_DELAY_TIMEOUT) {
Sean Andersonc6302f02020-09-15 10:44:40 -04001170 dev_err(mtd->dev, "Wait timeout!!!\n");
Stefan Roese75659da2015-07-23 10:26:16 +02001171 return;
1172 }
1173 }
1174
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001175 /* Only a few commands need several steps */
1176 if (command != NAND_CMD_PAGEPROG &&
1177 command != NAND_CMD_READ0 &&
1178 command != NAND_CMD_READOOB)
1179 break;
1180
1181 info->cur_chunk++;
1182
Stefan Roese75659da2015-07-23 10:26:16 +02001183 /* Check if the sequence is complete */
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001184 if (info->cur_chunk == info->ntotalchunks &&
1185 command != NAND_CMD_PAGEPROG)
Stefan Roese75659da2015-07-23 10:26:16 +02001186 break;
1187
1188 /*
1189 * After a splitted program command sequence has issued
1190 * the command dispatch, the command sequence is complete.
1191 */
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001192 if (info->cur_chunk == (info->ntotalchunks + 1) &&
Stefan Roese75659da2015-07-23 10:26:16 +02001193 command == NAND_CMD_PAGEPROG &&
1194 ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
1195 break;
1196
1197 if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
1198 /* Last read: issue a 'last naked read' */
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001199 if (info->cur_chunk == info->ntotalchunks - 1)
Stefan Roese75659da2015-07-23 10:26:16 +02001200 ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
1201 else
1202 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
1203
1204 /*
1205 * If a splitted program command has no more data to transfer,
1206 * the command dispatch must be issued to complete.
1207 */
1208 } else if (command == NAND_CMD_PAGEPROG &&
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001209 info->cur_chunk == info->ntotalchunks) {
Stefan Roese75659da2015-07-23 10:26:16 +02001210 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
1211 }
1212 } while (1);
1213
1214 info->state = STATE_IDLE;
1215}
1216
1217static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Scott Wood46e13102016-05-30 13:57:57 -05001218 struct nand_chip *chip, const uint8_t *buf, int oob_required,
1219 int page)
Stefan Roese75659da2015-07-23 10:26:16 +02001220{
1221 chip->write_buf(mtd, buf, mtd->writesize);
1222 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1223
1224 return 0;
1225}
1226
1227static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
1228 struct nand_chip *chip, uint8_t *buf, int oob_required,
1229 int page)
1230{
Scott Wood17fed142016-05-30 13:57:56 -05001231 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;
Miquel Raynal35f1ebd2018-10-11 17:45:43 +02001233 int bf;
Stefan Roese75659da2015-07-23 10:26:16 +02001234
1235 chip->read_buf(mtd, buf, mtd->writesize);
1236 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1237
1238 if (info->retcode == ERR_CORERR && info->use_ecc) {
1239 mtd->ecc_stats.corrected += info->ecc_err_cnt;
1240
Miquel Raynal35f1ebd2018-10-11 17:45:43 +02001241 } else if (info->retcode == ERR_UNCORERR && info->ecc_bch) {
Stefan Roese75659da2015-07-23 10:26:16 +02001242 /*
Miquel Raynal35f1ebd2018-10-11 17:45:43 +02001243 * Empty pages will trigger uncorrectable errors. Re-read the
1244 * entire page in raw mode and check for bits not being "1".
1245 * If there are more than the supported strength, then it means
1246 * this is an actual uncorrectable error.
Stefan Roese75659da2015-07-23 10:26:16 +02001247 */
Miquel Raynal35f1ebd2018-10-11 17:45:43 +02001248 chip->ecc.read_page_raw(mtd, chip, buf, oob_required, page);
1249 bf = nand_check_erased_ecc_chunk(buf, mtd->writesize,
1250 chip->oob_poi, mtd->oobsize,
1251 NULL, 0, chip->ecc.strength);
1252 if (bf < 0) {
1253 mtd->ecc_stats.failed++;
1254 } else if (bf) {
1255 mtd->ecc_stats.corrected += bf;
1256 info->max_bitflips = max_t(unsigned int,
1257 info->max_bitflips, bf);
1258 info->retcode = ERR_CORERR;
1259 } else {
1260 info->retcode = ERR_NONE;
1261 }
1262
1263 } else if (info->retcode == ERR_UNCORERR && !info->ecc_bch) {
1264 /* Raw read is not supported with Hamming ECC engine */
Stefan Roese75659da2015-07-23 10:26:16 +02001265 if (is_buf_blank(buf, mtd->writesize))
1266 info->retcode = ERR_NONE;
1267 else
1268 mtd->ecc_stats.failed++;
1269 }
1270
1271 return info->max_bitflips;
1272}
1273
Miquel Raynal30a016a2018-10-11 17:45:42 +02001274static int pxa3xx_nand_read_page_raw(struct mtd_info *mtd,
1275 struct nand_chip *chip, uint8_t *buf,
1276 int oob_required, int page)
1277{
1278 struct pxa3xx_nand_host *host = chip->priv;
1279 struct pxa3xx_nand_info *info = host->info_data;
1280 int chunk, ecc_off_buf;
1281
1282 if (!info->ecc_bch)
1283 return -ENOTSUPP;
1284
1285 /*
1286 * Set the force_raw boolean, then re-call ->cmdfunc() that will run
1287 * pxa3xx_nand_start(), which will actually disable the ECC engine.
1288 */
1289 info->force_raw = true;
1290 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1291
1292 ecc_off_buf = (info->nfullchunks * info->spare_size) +
1293 info->last_spare_size;
1294 for (chunk = 0; chunk < info->nfullchunks; chunk++) {
1295 chip->read_buf(mtd,
1296 buf + (chunk * info->chunk_size),
1297 info->chunk_size);
1298 chip->read_buf(mtd,
1299 chip->oob_poi +
1300 (chunk * (info->spare_size)),
1301 info->spare_size);
1302 chip->read_buf(mtd,
1303 chip->oob_poi + ecc_off_buf +
1304 (chunk * (info->ecc_size)),
1305 info->ecc_size - 2);
1306 }
1307
1308 if (info->ntotalchunks > info->nfullchunks) {
1309 chip->read_buf(mtd,
1310 buf + (info->nfullchunks * info->chunk_size),
1311 info->last_chunk_size);
1312 chip->read_buf(mtd,
1313 chip->oob_poi +
1314 (info->nfullchunks * (info->spare_size)),
1315 info->last_spare_size);
1316 chip->read_buf(mtd,
1317 chip->oob_poi + ecc_off_buf +
1318 (info->nfullchunks * (info->ecc_size)),
1319 info->ecc_size - 2);
1320 }
1321
1322 info->force_raw = false;
1323
1324 return 0;
1325}
1326
1327static int pxa3xx_nand_read_oob_raw(struct mtd_info *mtd,
1328 struct nand_chip *chip, int page)
1329{
1330 /* Invalidate page cache */
1331 chip->pagebuf = -1;
1332
1333 return chip->ecc.read_page_raw(mtd, chip, chip->buffers->databuf, true,
1334 page);
1335}
1336
Stefan Roese75659da2015-07-23 10:26:16 +02001337static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
1338{
Scott Wood17fed142016-05-30 13:57:56 -05001339 struct nand_chip *chip = mtd_to_nand(mtd);
1340 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001341 struct pxa3xx_nand_info *info = host->info_data;
1342 char retval = 0xFF;
1343
1344 if (info->buf_start < info->buf_count)
1345 /* Has just send a new command? */
1346 retval = info->data_buff[info->buf_start++];
1347
1348 return retval;
1349}
1350
1351static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
1352{
Scott Wood17fed142016-05-30 13:57:56 -05001353 struct nand_chip *chip = mtd_to_nand(mtd);
1354 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001355 struct pxa3xx_nand_info *info = host->info_data;
1356 u16 retval = 0xFFFF;
1357
1358 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
1359 retval = *((u16 *)(info->data_buff+info->buf_start));
1360 info->buf_start += 2;
1361 }
1362 return retval;
1363}
1364
1365static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1366{
Scott Wood17fed142016-05-30 13:57:56 -05001367 struct nand_chip *chip = mtd_to_nand(mtd);
1368 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001369 struct pxa3xx_nand_info *info = host->info_data;
1370 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1371
1372 memcpy(buf, info->data_buff + info->buf_start, real_len);
1373 info->buf_start += real_len;
1374}
1375
1376static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
1377 const uint8_t *buf, int len)
1378{
Scott Wood17fed142016-05-30 13:57:56 -05001379 struct nand_chip *chip = mtd_to_nand(mtd);
1380 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001381 struct pxa3xx_nand_info *info = host->info_data;
1382 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1383
1384 memcpy(info->data_buff + info->buf_start, buf, real_len);
1385 info->buf_start += real_len;
1386}
1387
1388static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
1389{
1390 return;
1391}
1392
1393static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
1394{
Scott Wood17fed142016-05-30 13:57:56 -05001395 struct nand_chip *chip = mtd_to_nand(mtd);
1396 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001397 struct pxa3xx_nand_info *info = host->info_data;
1398
1399 if (info->need_wait) {
1400 u32 ts;
1401
1402 info->need_wait = 0;
1403
1404 ts = get_timer(0);
1405 while (1) {
1406 u32 status;
1407
1408 status = nand_readl(info, NDSR);
1409 if (status)
1410 pxa3xx_nand_irq(info);
1411
1412 if (info->dev_ready)
1413 break;
1414
1415 if (get_timer(ts) > CHIP_DELAY_TIMEOUT) {
Sean Andersonc6302f02020-09-15 10:44:40 -04001416 dev_err(mtd->dev, "Ready timeout!!!\n");
Stefan Roese75659da2015-07-23 10:26:16 +02001417 return NAND_STATUS_FAIL;
1418 }
1419 }
1420 }
1421
1422 /* pxa3xx_nand_send_command has waited for command complete */
1423 if (this->state == FL_WRITING || this->state == FL_ERASING) {
1424 if (info->retcode == ERR_NONE)
1425 return 0;
1426 else
1427 return NAND_STATUS_FAIL;
1428 }
1429
1430 return NAND_STATUS_READY;
1431}
1432
Ofer Heifetz531816e2018-08-29 11:56:07 +03001433static int pxa3xx_nand_config_ident(struct pxa3xx_nand_info *info)
1434{
1435 struct pxa3xx_nand_platform_data *pdata = info->pdata;
1436
1437 /* Configure default flash values */
Ofer Heifetz531816e2018-08-29 11:56:07 +03001438 info->reg_ndcr = 0x0; /* enable all interrupts */
1439 info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
1440 info->reg_ndcr |= NDCR_RD_ID_CNT(READ_ID_BYTES);
1441 info->reg_ndcr |= NDCR_SPARE_EN;
1442
1443 return 0;
1444}
1445
1446static void pxa3xx_nand_config_tail(struct pxa3xx_nand_info *info)
Stefan Roese75659da2015-07-23 10:26:16 +02001447{
1448 struct pxa3xx_nand_host *host = info->host[info->cs];
Ofer Heifetz531816e2018-08-29 11:56:07 +03001449 struct mtd_info *mtd = nand_to_mtd(&info->host[info->cs]->chip);
Scott Wood17fed142016-05-30 13:57:56 -05001450 struct nand_chip *chip = mtd_to_nand(mtd);
Stefan Roese75659da2015-07-23 10:26:16 +02001451
1452 info->reg_ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
1453 info->reg_ndcr |= (chip->page_shift == 6) ? NDCR_PG_PER_BLK : 0;
1454 info->reg_ndcr |= (mtd->writesize == 2048) ? NDCR_PAGE_SZ : 0;
Stefan Roese75659da2015-07-23 10:26:16 +02001455}
1456
Ofer Heifetz268979f2018-08-29 11:56:08 +03001457static void pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
Stefan Roese75659da2015-07-23 10:26:16 +02001458{
Ofer Heifetz531816e2018-08-29 11:56:07 +03001459 struct pxa3xx_nand_platform_data *pdata = info->pdata;
Stefan Roese75659da2015-07-23 10:26:16 +02001460 uint32_t ndcr = nand_readl(info, NDCR);
1461
Stefan Roese75659da2015-07-23 10:26:16 +02001462 /* Set an initial chunk size */
Ofer Heifetz4a574aa2018-08-29 11:56:05 +03001463 info->chunk_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
Ofer Heifetz531816e2018-08-29 11:56:07 +03001464 info->reg_ndcr = ndcr &
1465 ~(NDCR_INT_MASK | NDCR_ND_ARB_EN | NFCV1_NDCR_ARB_CNTL);
1466 info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Stefan Roese75659da2015-07-23 10:26:16 +02001467 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
1468 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Stefan Roese75659da2015-07-23 10:26:16 +02001469}
1470
1471static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1472{
1473 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1474 if (info->data_buff == NULL)
1475 return -ENOMEM;
1476 return 0;
1477}
1478
1479static int pxa3xx_nand_sensing(struct pxa3xx_nand_host *host)
1480{
1481 struct pxa3xx_nand_info *info = host->info_data;
1482 struct pxa3xx_nand_platform_data *pdata = info->pdata;
1483 struct mtd_info *mtd;
1484 struct nand_chip *chip;
1485 const struct nand_sdr_timings *timings;
1486 int ret;
1487
Ofer Heifetz0da35df2018-08-29 11:56:01 +03001488 mtd = nand_to_mtd(&info->host[info->cs]->chip);
Scott Wood17fed142016-05-30 13:57:56 -05001489 chip = mtd_to_nand(mtd);
Stefan Roese75659da2015-07-23 10:26:16 +02001490
1491 /* configure default flash values */
1492 info->reg_ndcr = 0x0; /* enable all interrupts */
1493 info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Ofer Heifetz4a574aa2018-08-29 11:56:05 +03001494 info->reg_ndcr |= NDCR_RD_ID_CNT(READ_ID_BYTES);
Stefan Roese75659da2015-07-23 10:26:16 +02001495 info->reg_ndcr |= NDCR_SPARE_EN; /* enable spare by default */
1496
1497 /* use the common timing to make a try */
1498 timings = onfi_async_timing_mode_to_sdr_timings(0);
1499 if (IS_ERR(timings))
1500 return PTR_ERR(timings);
1501
1502 pxa3xx_nand_set_sdr_timing(host, timings);
1503
1504 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
1505 ret = chip->waitfunc(mtd, chip);
1506 if (ret & NAND_STATUS_FAIL)
1507 return -ENODEV;
1508
1509 return 0;
1510}
1511
1512static int pxa_ecc_init(struct pxa3xx_nand_info *info,
1513 struct nand_ecc_ctrl *ecc,
1514 int strength, int ecc_stepsize, int page_size)
1515{
Chris Packham03085ca2022-08-25 16:59:49 +12001516 int i = 0;
Stefan Roese75659da2015-07-23 10:26:16 +02001517
Chris Packham03085ca2022-08-25 16:59:49 +12001518 /* if ecc strength is 1 ecc algo is Hamming else bch */
1519 info->ecc_bch = (strength == 1) ? 0 : 1;
Stefan Roese75659da2015-07-23 10:26:16 +02001520
Chris Packham03085ca2022-08-25 16:59:49 +12001521 ecc->mode = NAND_ECC_HW;
Stefan Roese75659da2015-07-23 10:26:16 +02001522
Chris Packham03085ca2022-08-25 16:59:49 +12001523 /* ecc->layout is not in use for pxa driver (but shouldn't be NULL)*/
1524 if (info->ecc_bch == 1)
1525 ecc->layout = &ecc_layout_empty;
Stefan Roese75659da2015-07-23 10:26:16 +02001526
Chris Packham03085ca2022-08-25 16:59:49 +12001527 /* for bch actual ecc strength is 16 per chunk */
1528 ecc->strength = (info->ecc_bch == 1) ? 16 : 1;
Konstantin Porotchkine0e232e2018-08-29 11:56:17 +03001529
Chris Packham03085ca2022-08-25 16:59:49 +12001530 while (nfc_layouts[i].strength) {
1531 if (strength == nfc_layouts[i].strength && page_size == nfc_layouts[i].page_size) {
1532 info->nfullchunks = nfc_layouts[i].nfullchunks;
1533 info->chunk_size = nfc_layouts[i].chunk_size;
1534 info->spare_size = nfc_layouts[i].spare_size;
1535 info->last_chunk_size = nfc_layouts[i].last_chunk_size;
1536 info->last_spare_size = nfc_layouts[i].last_spare_size;
1537 info->ntotalchunks = (info->last_spare_size || info->last_chunk_size) ?
1538 info->nfullchunks + 1 : info->nfullchunks;
1539 info->ecc_size = nfc_layouts[i].ecc_size;
1540 break;
1541 }
1542 ++i;
1543 }
Konstantin Porotchkine0e232e2018-08-29 11:56:17 +03001544
Chris Packham03085ca2022-08-25 16:59:49 +12001545 /* for bch the ecc is calculated per chunk size and for Hamming it is 512 */
1546 ecc->size = (info->ecc_bch) ? info->chunk_size : 512;
Konstantin Porotchkina692cde2018-08-29 11:56:16 +03001547
Chris Packham03085ca2022-08-25 16:59:49 +12001548 /* nand_scan_tail func perform validity tests for ECC strength, and it
1549 * assumes that all chunks are with same size. in our case when ecc is 12
1550 * the chunk size is 704 but the last chunk is with different size so
1551 * we cheat it nand_scan_tail validity tests by set info->ecc_size value to 512
1552 */
1553 if (strength == 12)
1554 ecc->size = 512;
Konstantin Porotchkina692cde2018-08-29 11:56:16 +03001555
Chris Packham03085ca2022-08-25 16:59:49 +12001556 if (ecc_stepsize != 512 || !(nfc_layouts[i].strength)) {
Sean Andersonc6302f02020-09-15 10:44:40 -04001557 dev_err(info->controller.active->mtd.dev,
Stefan Roese75659da2015-07-23 10:26:16 +02001558 "ECC strength %d at page size %d is not supported\n",
1559 strength, page_size);
1560 return -ENODEV;
1561 }
1562
1563 return 0;
1564}
1565
1566static int pxa3xx_nand_scan(struct mtd_info *mtd)
1567{
Scott Wood17fed142016-05-30 13:57:56 -05001568 struct nand_chip *chip = mtd_to_nand(mtd);
1569 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001570 struct pxa3xx_nand_info *info = host->info_data;
1571 struct pxa3xx_nand_platform_data *pdata = info->pdata;
Stefan Roese75659da2015-07-23 10:26:16 +02001572 int ret;
1573 uint16_t ecc_strength, ecc_step;
1574
Ofer Heifetz268979f2018-08-29 11:56:08 +03001575 if (pdata->keep_config) {
1576 pxa3xx_nand_detect_config(info);
1577 } else {
1578 ret = pxa3xx_nand_config_ident(info);
1579 if (ret)
1580 return ret;
1581 ret = pxa3xx_nand_sensing(host);
1582 if (ret) {
Sean Andersonc6302f02020-09-15 10:44:40 -04001583 dev_info(mtd->dev, "There is no chip on cs %d!\n",
Ofer Heifetz268979f2018-08-29 11:56:08 +03001584 info->cs);
1585 return ret;
1586 }
Stefan Roese75659da2015-07-23 10:26:16 +02001587 }
1588
Stefan Roese75659da2015-07-23 10:26:16 +02001589 /* Device detection must be done with ECC disabled */
Shmuel Hazan58983222020-10-29 08:52:20 +02001590 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 ||
Chris Packhambd3ce6f2023-07-10 10:47:34 +12001591 info->variant == PXA3XX_NAND_VARIANT_ARMADA_8K ||
1592 info->variant == PXA3XX_NAND_VARIANT_AC5)
Stefan Roese75659da2015-07-23 10:26:16 +02001593 nand_writel(info, NDECCCTRL, 0x0);
1594
1595 if (nand_scan_ident(mtd, 1, NULL))
1596 return -ENODEV;
1597
1598 if (!pdata->keep_config) {
1599 ret = pxa3xx_nand_init_timings(host);
1600 if (ret) {
Sean Andersonc6302f02020-09-15 10:44:40 -04001601 dev_err(mtd->dev,
Stefan Roese75659da2015-07-23 10:26:16 +02001602 "Failed to set timings: %d\n", ret);
1603 return ret;
1604 }
1605 }
1606
Stefan Roese75659da2015-07-23 10:26:16 +02001607#ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1608 /*
1609 * We'll use a bad block table stored in-flash and don't
1610 * allow writing the bad block marker to the flash.
1611 */
1612 chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB_BBM;
1613 chip->bbt_td = &bbt_main_descr;
1614 chip->bbt_md = &bbt_mirror_descr;
1615#endif
1616
Stefan Roese75659da2015-07-23 10:26:16 +02001617 if (pdata->ecc_strength && pdata->ecc_step_size) {
1618 ecc_strength = pdata->ecc_strength;
1619 ecc_step = pdata->ecc_step_size;
1620 } else {
1621 ecc_strength = chip->ecc_strength_ds;
1622 ecc_step = chip->ecc_step_ds;
1623 }
1624
1625 /* Set default ECC strength requirements on non-ONFI devices */
1626 if (ecc_strength < 1 && ecc_step < 1) {
1627 ecc_strength = 1;
1628 ecc_step = 512;
1629 }
1630
1631 ret = pxa_ecc_init(info, &chip->ecc, ecc_strength,
1632 ecc_step, mtd->writesize);
1633 if (ret)
1634 return ret;
1635
Konstantin Porotchkin06f9b6b2018-08-29 11:56:15 +03001636 /*
1637 * If the page size is bigger than the FIFO size, let's check
1638 * we are given the right variant and then switch to the extended
1639 * (aka split) command handling,
1640 */
1641 if (mtd->writesize > info->chunk_size) {
Shmuel Hazan58983222020-10-29 08:52:20 +02001642 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 ||
Chris Packhambd3ce6f2023-07-10 10:47:34 +12001643 info->variant == PXA3XX_NAND_VARIANT_ARMADA_8K ||
1644 info->variant == PXA3XX_NAND_VARIANT_AC5) {
Konstantin Porotchkin06f9b6b2018-08-29 11:56:15 +03001645 chip->cmdfunc = nand_cmdfunc_extended;
1646 } else {
Sean Andersonc6302f02020-09-15 10:44:40 -04001647 dev_err(mtd->dev,
Konstantin Porotchkin06f9b6b2018-08-29 11:56:15 +03001648 "unsupported page size on this variant\n");
1649 return -ENODEV;
1650 }
1651 }
1652
Stefan Roese75659da2015-07-23 10:26:16 +02001653 /* calculate addressing information */
1654 if (mtd->writesize >= 2048)
1655 host->col_addr_cycles = 2;
1656 else
1657 host->col_addr_cycles = 1;
1658
1659 /* release the initial buffer */
1660 kfree(info->data_buff);
1661
1662 /* allocate the real data + oob buffer */
1663 info->buf_size = mtd->writesize + mtd->oobsize;
1664 ret = pxa3xx_nand_init_buff(info);
1665 if (ret)
1666 return ret;
1667 info->oob_buff = info->data_buff + mtd->writesize;
1668
1669 if ((mtd->size >> chip->page_shift) > 65536)
1670 host->row_addr_cycles = 3;
1671 else
1672 host->row_addr_cycles = 2;
Ofer Heifetz531816e2018-08-29 11:56:07 +03001673
1674 if (!pdata->keep_config)
1675 pxa3xx_nand_config_tail(info);
1676
Stefan Roese75659da2015-07-23 10:26:16 +02001677 return nand_scan_tail(mtd);
1678}
1679
Shmuel Hazan58983222020-10-29 08:52:20 +02001680static int alloc_nand_resource(struct udevice *dev, struct pxa3xx_nand_info *info)
Stefan Roese75659da2015-07-23 10:26:16 +02001681{
1682 struct pxa3xx_nand_platform_data *pdata;
1683 struct pxa3xx_nand_host *host;
1684 struct nand_chip *chip = NULL;
1685 struct mtd_info *mtd;
Baruch Siach807ae582020-10-29 08:52:19 +02001686 int cs;
Stefan Roese75659da2015-07-23 10:26:16 +02001687
1688 pdata = info->pdata;
1689 if (pdata->num_cs <= 0)
1690 return -ENODEV;
1691
Shmuel Hazan58983222020-10-29 08:52:20 +02001692 info->variant = pxa3xx_nand_get_variant(dev);
Stefan Roese75659da2015-07-23 10:26:16 +02001693 for (cs = 0; cs < pdata->num_cs; cs++) {
Kevin Smith4d21b592016-01-14 16:01:38 +00001694 chip = (struct nand_chip *)
1695 ((u8 *)&info[1] + sizeof(*host) * cs);
Scott Wood17fed142016-05-30 13:57:56 -05001696 mtd = nand_to_mtd(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001697 host = (struct pxa3xx_nand_host *)chip;
1698 info->host[cs] = host;
Stefan Roese75659da2015-07-23 10:26:16 +02001699 host->cs = cs;
1700 host->info_data = info;
Stefan Roese75659da2015-07-23 10:26:16 +02001701 mtd->owner = THIS_MODULE;
1702
Chris Packham3c2170a2016-08-29 15:20:52 +12001703 nand_set_controller_data(chip, host);
Stefan Roese75659da2015-07-23 10:26:16 +02001704 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
Miquel Raynal30a016a2018-10-11 17:45:42 +02001705 chip->ecc.read_page_raw = pxa3xx_nand_read_page_raw;
1706 chip->ecc.read_oob_raw = pxa3xx_nand_read_oob_raw;
Stefan Roese75659da2015-07-23 10:26:16 +02001707 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1708 chip->controller = &info->controller;
1709 chip->waitfunc = pxa3xx_nand_waitfunc;
1710 chip->select_chip = pxa3xx_nand_select_chip;
1711 chip->read_word = pxa3xx_nand_read_word;
1712 chip->read_byte = pxa3xx_nand_read_byte;
1713 chip->read_buf = pxa3xx_nand_read_buf;
1714 chip->write_buf = pxa3xx_nand_write_buf;
1715 chip->options |= NAND_NO_SUBPAGE_WRITE;
1716 chip->cmdfunc = nand_cmdfunc;
1717 }
1718
Stefan Roese75659da2015-07-23 10:26:16 +02001719 /* Allocate a buffer to allow flash detection */
1720 info->buf_size = INIT_BUFFER_SIZE;
1721 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
Baruch Siach807ae582020-10-29 08:52:19 +02001722 if (info->data_buff == NULL)
1723 return -ENOMEM;
Stefan Roese75659da2015-07-23 10:26:16 +02001724
1725 /* initialize all interrupts to be disabled */
1726 disable_int(info, NDSR_MASK);
1727
Shmuel Hazan58983222020-10-29 08:52:20 +02001728 /*
1729 * Some SoCs like A7k/A8k need to enable manually the NAND
1730 * controller to avoid being bootloader dependent. This is done
1731 * through the use of a single bit in the System Functions registers.
1732 */
1733 if (pxa3xx_nand_get_variant(dev) == PXA3XX_NAND_VARIANT_ARMADA_8K) {
1734 struct regmap *sysctrl_base = syscon_regmap_lookup_by_phandle(
1735 dev, "marvell,system-controller");
1736 u32 reg;
1737
1738 if (IS_ERR(sysctrl_base))
1739 return PTR_ERR(sysctrl_base);
1740
1741 regmap_read(sysctrl_base, GENCONF_SOC_DEVICE_MUX, &reg);
1742 reg |= GENCONF_SOC_DEVICE_MUX_NFC_EN;
1743 regmap_write(sysctrl_base, GENCONF_SOC_DEVICE_MUX, reg);
1744 }
1745
Stefan Roese75659da2015-07-23 10:26:16 +02001746 return 0;
Stefan Roese75659da2015-07-23 10:26:16 +02001747}
1748
Shmuel Hazan759349e2020-10-29 08:52:18 +02001749static int pxa3xx_nand_probe_dt(struct udevice *dev, struct pxa3xx_nand_info *info)
Stefan Roese75659da2015-07-23 10:26:16 +02001750{
1751 struct pxa3xx_nand_platform_data *pdata;
1752
1753 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1754 if (!pdata)
1755 return -ENOMEM;
1756
Shmuel Hazan759349e2020-10-29 08:52:18 +02001757 info->mmio_base = dev_read_addr_ptr(dev);
Stefan Roese75659da2015-07-23 10:26:16 +02001758
Shmuel Hazan759349e2020-10-29 08:52:18 +02001759 pdata->num_cs = dev_read_u32_default(dev, "num-cs", 1);
1760 if (pdata->num_cs != 1) {
1761 pr_err("pxa3xx driver supports single CS only\n");
1762 return -EINVAL;
1763 }
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +03001764
Pierre Bourdonfa7890e2021-12-25 05:46:29 +01001765 if (dev_read_bool(dev, "marvell,nand-enable-arbiter"))
Shmuel Hazan759349e2020-10-29 08:52:18 +02001766 pdata->enable_arbiter = 1;
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +03001767
Pierre Bourdonfa7890e2021-12-25 05:46:29 +01001768 if (dev_read_bool(dev, "marvell,nand-keep-config"))
Shmuel Hazan759349e2020-10-29 08:52:18 +02001769 pdata->keep_config = 1;
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +03001770
Shmuel Hazan759349e2020-10-29 08:52:18 +02001771 /*
1772 * ECC parameters.
1773 * If these are not set, they will be selected according
1774 * to the detected flash type.
1775 */
1776 /* ECC strength */
1777 pdata->ecc_strength = dev_read_u32_default(dev, "nand-ecc-strength", 0);
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +03001778
Shmuel Hazan759349e2020-10-29 08:52:18 +02001779 /* ECC step size */
1780 pdata->ecc_step_size = dev_read_u32_default(dev, "nand-ecc-step-size",
1781 0);
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +03001782
Shmuel Hazan759349e2020-10-29 08:52:18 +02001783 info->pdata = pdata;
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +03001784
Shmuel Hazan759349e2020-10-29 08:52:18 +02001785 return 0;
Stefan Roese75659da2015-07-23 10:26:16 +02001786}
1787
Shmuel Hazan759349e2020-10-29 08:52:18 +02001788static int pxa3xx_nand_probe(struct udevice *dev)
Stefan Roese75659da2015-07-23 10:26:16 +02001789{
1790 struct pxa3xx_nand_platform_data *pdata;
1791 int ret, cs, probe_success;
Shmuel Hazan759349e2020-10-29 08:52:18 +02001792 struct pxa3xx_nand_info *info = dev_get_priv(dev);
Stefan Roese75659da2015-07-23 10:26:16 +02001793
Shmuel Hazan759349e2020-10-29 08:52:18 +02001794 ret = pxa3xx_nand_probe_dt(dev, info);
Stefan Roese75659da2015-07-23 10:26:16 +02001795 if (ret)
1796 return ret;
1797
1798 pdata = info->pdata;
1799
Shmuel Hazan58983222020-10-29 08:52:20 +02001800 ret = alloc_nand_resource(dev, info);
Stefan Roese75659da2015-07-23 10:26:16 +02001801 if (ret) {
Shmuel Hazan759349e2020-10-29 08:52:18 +02001802 dev_err(dev, "alloc nand resource failed\n");
Stefan Roese75659da2015-07-23 10:26:16 +02001803 return ret;
1804 }
1805
1806 probe_success = 0;
1807 for (cs = 0; cs < pdata->num_cs; cs++) {
Ofer Heifetz0da35df2018-08-29 11:56:01 +03001808 struct mtd_info *mtd = nand_to_mtd(&info->host[cs]->chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001809
1810 /*
1811 * The mtd name matches the one used in 'mtdparts' kernel
1812 * parameter. This name cannot be changed or otherwise
1813 * user's mtd partitions configuration would get broken.
1814 */
1815 mtd->name = "pxa3xx_nand-0";
Robert Marko142f41a2022-01-05 16:01:00 +01001816 mtd->dev = dev;
Stefan Roese75659da2015-07-23 10:26:16 +02001817 info->cs = cs;
1818 ret = pxa3xx_nand_scan(mtd);
1819 if (ret) {
Sean Andersonc6302f02020-09-15 10:44:40 -04001820 dev_info(mtd->dev, "failed to scan nand at cs %d\n",
Stefan Roese75659da2015-07-23 10:26:16 +02001821 cs);
1822 continue;
1823 }
1824
Scott Wood2c1b7e12016-05-30 13:57:55 -05001825 if (nand_register(cs, mtd))
1826 continue;
1827
1828 probe_success = 1;
Stefan Roese75659da2015-07-23 10:26:16 +02001829 }
1830
1831 if (!probe_success)
1832 return -ENODEV;
1833
1834 return 0;
1835}
1836
Shmuel Hazan759349e2020-10-29 08:52:18 +02001837U_BOOT_DRIVER(pxa3xx_nand) = {
1838 .name = "pxa3xx-nand",
1839 .id = UCLASS_MTD,
1840 .of_match = pxa3xx_nand_dt_ids,
1841 .probe = pxa3xx_nand_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001842 .priv_auto = sizeof(struct pxa3xx_nand_info) +
Shmuel Hazan759349e2020-10-29 08:52:18 +02001843 sizeof(struct pxa3xx_nand_host) * CONFIG_SYS_MAX_NAND_DEVICE,
1844};
1845
Stefan Roese75659da2015-07-23 10:26:16 +02001846void board_nand_init(void)
1847{
Shmuel Hazan759349e2020-10-29 08:52:18 +02001848 struct udevice *dev;
Stefan Roese75659da2015-07-23 10:26:16 +02001849 int ret;
1850
Shmuel Hazan759349e2020-10-29 08:52:18 +02001851 ret = uclass_get_device_by_driver(UCLASS_MTD,
Simon Glass65130cd2020-12-28 20:34:56 -07001852 DM_DRIVER_GET(pxa3xx_nand), &dev);
Shmuel Hazan759349e2020-10-29 08:52:18 +02001853 if (ret && ret != -ENODEV) {
1854 pr_err("Failed to initialize %s. (error %d)\n", dev->name,
1855 ret);
1856 }
Stefan Roese75659da2015-07-23 10:26:16 +02001857}