blob: b78b4e6023803fa6824050d5777fd4b7b61ef76c [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
Stefan Roese75659da2015-07-23 10:26:16 +02009#include <malloc.h>
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +030010#include <fdtdec.h>
Stefan Roese75659da2015-07-23 10:26:16 +020011#include <nand.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070014#include <dm/devres.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060015#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060016#include <linux/bug.h>
Simon Glassdbd79542020-05-10 11:40:11 -060017#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070018#include <linux/err.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090019#include <linux/errno.h>
Stefan Roese75659da2015-07-23 10:26:16 +020020#include <asm/io.h>
21#include <asm/arch/cpu.h>
22#include <linux/mtd/mtd.h>
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090023#include <linux/mtd/rawnand.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060024#include <linux/printk.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)
Chris Packhamf2c1ea72023-07-10 10:47:35 +1200128#define GENCONF_SOC_DEVICE_MUX_NFC_DEVBUS_ARB_EN BIT(27)
Shmuel Hazan58983222020-10-29 08:52:20 +0200129
Ofer Heifetz4a574aa2018-08-29 11:56:05 +0300130/*
131 * This should be large enough to read 'ONFI' and 'JEDEC'.
132 * Let's use 7 bytes, which is the maximum ID count supported
133 * by the controller (see NDCR_RD_ID_CNT_MASK).
134 */
135#define READ_ID_BYTES 7
136
Stefan Roese75659da2015-07-23 10:26:16 +0200137/* macros for registers read/write */
138#define nand_writel(info, off, val) \
139 writel((val), (info)->mmio_base + (off))
140
141#define nand_readl(info, off) \
142 readl((info)->mmio_base + (off))
143
144/* error code and state */
145enum {
146 ERR_NONE = 0,
147 ERR_DMABUSERR = -1,
148 ERR_SENDCMD = -2,
149 ERR_UNCORERR = -3,
150 ERR_BBERR = -4,
151 ERR_CORERR = -5,
152};
153
154enum {
155 STATE_IDLE = 0,
156 STATE_PREPARED,
157 STATE_CMD_HANDLE,
158 STATE_DMA_READING,
159 STATE_DMA_WRITING,
160 STATE_DMA_DONE,
161 STATE_PIO_READING,
162 STATE_PIO_WRITING,
163 STATE_CMD_DONE,
164 STATE_READY,
165};
166
167enum pxa3xx_nand_variant {
168 PXA3XX_NAND_VARIANT_PXA,
169 PXA3XX_NAND_VARIANT_ARMADA370,
Shmuel Hazan58983222020-10-29 08:52:20 +0200170 PXA3XX_NAND_VARIANT_ARMADA_8K,
Chris Packhambd3ce6f2023-07-10 10:47:34 +1200171 PXA3XX_NAND_VARIANT_AC5,
Stefan Roese75659da2015-07-23 10:26:16 +0200172};
173
174struct pxa3xx_nand_host {
175 struct nand_chip chip;
Stefan Roese75659da2015-07-23 10:26:16 +0200176 void *info_data;
177
178 /* page size of attached chip */
179 int use_ecc;
180 int cs;
181
182 /* calculated from pxa3xx_nand_flash data */
183 unsigned int col_addr_cycles;
184 unsigned int row_addr_cycles;
Stefan Roese75659da2015-07-23 10:26:16 +0200185};
186
187struct pxa3xx_nand_info {
188 struct nand_hw_control controller;
189 struct pxa3xx_nand_platform_data *pdata;
190
191 struct clk *clk;
192 void __iomem *mmio_base;
193 unsigned long mmio_phys;
194 int cmd_complete, dev_ready;
195
196 unsigned int buf_start;
197 unsigned int buf_count;
198 unsigned int buf_size;
199 unsigned int data_buff_pos;
200 unsigned int oob_buff_pos;
201
202 unsigned char *data_buff;
203 unsigned char *oob_buff;
204
205 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
206 unsigned int state;
207
208 /*
209 * This driver supports NFCv1 (as found in PXA SoC)
210 * and NFCv2 (as found in Armada 370/XP SoC).
211 */
212 enum pxa3xx_nand_variant variant;
213
214 int cs;
215 int use_ecc; /* use HW ECC ? */
Miquel Raynal30a016a2018-10-11 17:45:42 +0200216 int force_raw; /* prevent use_ecc to be set */
Stefan Roese75659da2015-07-23 10:26:16 +0200217 int ecc_bch; /* using BCH ECC? */
218 int use_spare; /* use spare ? */
219 int need_wait;
220
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300221 /* Amount of real data per full chunk */
222 unsigned int chunk_size;
223
224 /* Amount of spare data per full chunk */
Stefan Roese75659da2015-07-23 10:26:16 +0200225 unsigned int spare_size;
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300226
227 /* Number of full chunks (i.e chunk_size + spare_size) */
228 unsigned int nfullchunks;
229
230 /*
231 * Total number of chunks. If equal to nfullchunks, then there
232 * are only full chunks. Otherwise, there is one last chunk of
233 * size (last_chunk_size + last_spare_size)
234 */
235 unsigned int ntotalchunks;
236
237 /* Amount of real data in the last chunk */
238 unsigned int last_chunk_size;
239
240 /* Amount of spare data in the last chunk */
241 unsigned int last_spare_size;
242
Stefan Roese75659da2015-07-23 10:26:16 +0200243 unsigned int ecc_size;
244 unsigned int ecc_err_cnt;
245 unsigned int max_bitflips;
246 int retcode;
247
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300248 /*
249 * Variables only valid during command
250 * execution. step_chunk_size and step_spare_size is the
251 * amount of real data and spare data in the current
252 * chunk. cur_chunk is the current chunk being
253 * read/programmed.
254 */
255 unsigned int step_chunk_size;
256 unsigned int step_spare_size;
257 unsigned int cur_chunk;
258
Stefan Roese75659da2015-07-23 10:26:16 +0200259 /* cached register value */
260 uint32_t reg_ndcr;
261 uint32_t ndtr0cs0;
262 uint32_t ndtr1cs0;
263
264 /* generated NDCBx register values */
265 uint32_t ndcb0;
266 uint32_t ndcb1;
267 uint32_t ndcb2;
268 uint32_t ndcb3;
269};
270
271static struct pxa3xx_nand_timing timing[] = {
Konstantin Porotchkina692cde2018-08-29 11:56:16 +0300272 /*
273 * tCH Enable signal hold time
274 * tCS Enable signal setup time
275 * tWH ND_nWE high duration
276 * tWP ND_nWE pulse time
277 * tRH ND_nRE high duration
278 * tRP ND_nRE pulse width
279 * tR ND_nWE high to ND_nRE low for read
280 * tWHR ND_nWE high to ND_nRE low for status read
281 * tAR ND_ALE low to ND_nRE low delay
282 */
Konstantin Porotchkin029be942018-08-29 11:56:14 +0300283 /*ch cs wh wp rh rp r whr ar */
Stefan Roese75659da2015-07-23 10:26:16 +0200284 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
285 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
286 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
287 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
Konstantin Porotchkin029be942018-08-29 11:56:14 +0300288 { 5, 20, 10, 12, 10, 12, 25000, 60, 10, },
Stefan Roese75659da2015-07-23 10:26:16 +0200289};
290
291static struct pxa3xx_nand_flash builtin_flash_types[] = {
Konstantin Porotchkina692cde2018-08-29 11:56:16 +0300292 /*
293 * chip_id
294 * flash_width Width of Flash memory (DWIDTH_M)
295 * dfc_width Width of flash controller(DWIDTH_C)
296 * *timing
297 * http://www.linux-mtd.infradead.org/nand-data/nanddata.html
298 */
Stefan Roese75659da2015-07-23 10:26:16 +0200299 { 0x46ec, 16, 16, &timing[1] },
300 { 0xdaec, 8, 8, &timing[1] },
301 { 0xd7ec, 8, 8, &timing[1] },
302 { 0xa12c, 8, 8, &timing[2] },
303 { 0xb12c, 16, 16, &timing[2] },
304 { 0xdc2c, 8, 8, &timing[2] },
305 { 0xcc2c, 16, 16, &timing[2] },
306 { 0xba20, 16, 16, &timing[3] },
Konstantin Porotchkin029be942018-08-29 11:56:14 +0300307 { 0xda98, 8, 8, &timing[4] },
Stefan Roese75659da2015-07-23 10:26:16 +0200308};
309
Sean Nyekjaera12a8e82017-11-22 13:39:08 +0100310#ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
Stefan Roese75659da2015-07-23 10:26:16 +0200311static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
312static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
313
314static struct nand_bbt_descr bbt_main_descr = {
315 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
316 | NAND_BBT_2BIT | NAND_BBT_VERSION,
317 .offs = 8,
318 .len = 6,
319 .veroffs = 14,
320 .maxblocks = 8, /* Last 8 blocks in each chip */
321 .pattern = bbt_pattern
322};
323
324static struct nand_bbt_descr bbt_mirror_descr = {
325 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
326 | NAND_BBT_2BIT | NAND_BBT_VERSION,
327 .offs = 8,
328 .len = 6,
329 .veroffs = 14,
330 .maxblocks = 8, /* Last 8 blocks in each chip */
331 .pattern = bbt_mirror_pattern
332};
Sean Nyekjaera12a8e82017-11-22 13:39:08 +0100333#endif
Stefan Roese75659da2015-07-23 10:26:16 +0200334
Chris Packham03085ca2022-08-25 16:59:49 +1200335struct marvell_hw_ecc_layout {
336 int page_size;
337 int strength;
338 unsigned int ecc_size;
339 unsigned int nfullchunks;
340 unsigned int chunk_size;
341 unsigned int spare_size;
342 unsigned int last_chunk_size;
343 unsigned int last_spare_size;
Stefan Roese75659da2015-07-23 10:26:16 +0200344};
345
Chris Packham03085ca2022-08-25 16:59:49 +1200346static const struct marvell_hw_ecc_layout nfc_layouts[] = {
347 /* page_size strength ecc_size nfullchunks chunk_size spare_size last_chunk last_spare */
348 { 512, 1, 8, 1, 512, 8, 0, 0 },
349 { 2048, 1, 24, 1, 2048, 40, 0, 0 },
Konstantin Porotchkine0e232e2018-08-29 11:56:17 +0300350
Chris Packham03085ca2022-08-25 16:59:49 +1200351 { 2048, 4, 32, 1, 2048, 32, 0, 0 },
352 { 2048, 8, 32, 1, 1024, 0, 1024, 32 },
353 { 2048, 12, 32, 2, 704, 0, 640, 0 },
354 { 2048, 16, 32, 4, 512, 0, 0, 32 },
355 { 4096, 4, 32, 2, 2048, 32, 0, 0 },
356 { 4096, 8, 32, 4, 1024, 0, 0, 64 },
357 { 4096, 12, 32, 5, 704, 0, 576, 32 },
358 { 4096, 16, 32, 8, 512, 0, 0, 32 },
Konstantin Porotchkine0e232e2018-08-29 11:56:17 +0300359
Chris Packham03085ca2022-08-25 16:59:49 +1200360 { 8192, 4, 32, 4, 2048, 32, 0, 0 },
361 { 8192, 8, 32, 8, 1024, 0, 0, 160 },
362 { 8192, 12, 32, 11, 704, 0, 448, 64 },
363 { 8192, 16, 32, 16, 512, 0, 0, 32 },
364 { },
Konstantin Porotchkine0e232e2018-08-29 11:56:17 +0300365};
366
Chris Packham03085ca2022-08-25 16:59:49 +1200367static struct nand_ecclayout ecc_layout_empty = {
368 .eccbytes = 0,
369 .eccpos = { },
Stefan Roese75659da2015-07-23 10:26:16 +0200370 .oobfree = { }
371};
372
373#define NDTR0_tCH(c) (min((c), 7) << 19)
374#define NDTR0_tCS(c) (min((c), 7) << 16)
375#define NDTR0_tWH(c) (min((c), 7) << 11)
376#define NDTR0_tWP(c) (min((c), 7) << 8)
377#define NDTR0_tRH(c) (min((c), 7) << 3)
378#define NDTR0_tRP(c) (min((c), 7) << 0)
379
380#define NDTR1_tR(c) (min((c), 65535) << 16)
381#define NDTR1_tWHR(c) (min((c), 15) << 4)
382#define NDTR1_tAR(c) (min((c), 15) << 0)
383
384/* convert nano-seconds to nand flash controller clock cycles */
385#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
386
Shmuel Hazan759349e2020-10-29 08:52:18 +0200387static const struct udevice_id pxa3xx_nand_dt_ids[] = {
388 {
Pali Rohárc96bc1d2022-07-27 14:47:35 +0200389 .compatible = "marvell,armada370-nand-controller",
Shmuel Hazan759349e2020-10-29 08:52:18 +0200390 .data = PXA3XX_NAND_VARIANT_ARMADA370,
391 },
Shmuel Hazan58983222020-10-29 08:52:20 +0200392 {
393 .compatible = "marvell,armada-8k-nand-controller",
394 .data = PXA3XX_NAND_VARIANT_ARMADA_8K,
395 },
Chris Packhambd3ce6f2023-07-10 10:47:34 +1200396 {
397 .compatible = "marvell,mvebu-ac5-pxa3xx-nand",
398 .data = PXA3XX_NAND_VARIANT_AC5,
399 },
Shmuel Hazan759349e2020-10-29 08:52:18 +0200400 {}
401};
402
Shmuel Hazan58983222020-10-29 08:52:20 +0200403static enum pxa3xx_nand_variant pxa3xx_nand_get_variant(struct udevice *dev)
Stefan Roese75659da2015-07-23 10:26:16 +0200404{
Shmuel Hazan58983222020-10-29 08:52:20 +0200405 return dev_get_driver_data(dev);
Stefan Roese75659da2015-07-23 10:26:16 +0200406}
407
408static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
409 const struct pxa3xx_nand_timing *t)
410{
411 struct pxa3xx_nand_info *info = host->info_data;
412 unsigned long nand_clk = mvebu_get_nand_clock();
413 uint32_t ndtr0, ndtr1;
414
415 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
416 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
417 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
418 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
419 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
420 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
421
422 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
423 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
424 NDTR1_tAR(ns2cycle(t->tAR, 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 void pxa3xx_nand_set_sdr_timing(struct pxa3xx_nand_host *host,
433 const struct nand_sdr_timings *t)
434{
435 struct pxa3xx_nand_info *info = host->info_data;
436 struct nand_chip *chip = &host->chip;
437 unsigned long nand_clk = mvebu_get_nand_clock();
438 uint32_t ndtr0, ndtr1;
439
440 u32 tCH_min = DIV_ROUND_UP(t->tCH_min, 1000);
441 u32 tCS_min = DIV_ROUND_UP(t->tCS_min, 1000);
442 u32 tWH_min = DIV_ROUND_UP(t->tWH_min, 1000);
Ofer Heifetz8f8d4582018-08-29 11:56:02 +0300443 u32 tWP_min = DIV_ROUND_UP(t->tWC_min - t->tWH_min, 1000);
Stefan Roese75659da2015-07-23 10:26:16 +0200444 u32 tREH_min = DIV_ROUND_UP(t->tREH_min, 1000);
Ofer Heifetz8f8d4582018-08-29 11:56:02 +0300445 u32 tRP_min = DIV_ROUND_UP(t->tRC_min - t->tREH_min, 1000);
Stefan Roese75659da2015-07-23 10:26:16 +0200446 u32 tR = chip->chip_delay * 1000;
447 u32 tWHR_min = DIV_ROUND_UP(t->tWHR_min, 1000);
448 u32 tAR_min = DIV_ROUND_UP(t->tAR_min, 1000);
449
450 /* fallback to a default value if tR = 0 */
451 if (!tR)
452 tR = 20000;
453
454 ndtr0 = NDTR0_tCH(ns2cycle(tCH_min, nand_clk)) |
455 NDTR0_tCS(ns2cycle(tCS_min, nand_clk)) |
456 NDTR0_tWH(ns2cycle(tWH_min, nand_clk)) |
457 NDTR0_tWP(ns2cycle(tWP_min, nand_clk)) |
458 NDTR0_tRH(ns2cycle(tREH_min, nand_clk)) |
459 NDTR0_tRP(ns2cycle(tRP_min, nand_clk));
460
461 ndtr1 = NDTR1_tR(ns2cycle(tR, nand_clk)) |
462 NDTR1_tWHR(ns2cycle(tWHR_min, nand_clk)) |
463 NDTR1_tAR(ns2cycle(tAR_min, nand_clk));
464
465 info->ndtr0cs0 = ndtr0;
466 info->ndtr1cs0 = ndtr1;
467 nand_writel(info, NDTR0CS0, ndtr0);
468 nand_writel(info, NDTR1CS0, ndtr1);
469}
470
471static int pxa3xx_nand_init_timings(struct pxa3xx_nand_host *host)
472{
473 const struct nand_sdr_timings *timings;
474 struct nand_chip *chip = &host->chip;
475 struct pxa3xx_nand_info *info = host->info_data;
476 const struct pxa3xx_nand_flash *f = NULL;
Ofer Heifetz0da35df2018-08-29 11:56:01 +0300477 struct mtd_info *mtd = nand_to_mtd(&host->chip);
Stefan Roese75659da2015-07-23 10:26:16 +0200478 int mode, id, ntypes, i;
479
480 mode = onfi_get_async_timing_mode(chip);
481 if (mode == ONFI_TIMING_MODE_UNKNOWN) {
482 ntypes = ARRAY_SIZE(builtin_flash_types);
483
Ofer Heifetz0da35df2018-08-29 11:56:01 +0300484 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Stefan Roese75659da2015-07-23 10:26:16 +0200485
Ofer Heifetz0da35df2018-08-29 11:56:01 +0300486 id = chip->read_byte(mtd);
487 id |= chip->read_byte(mtd) << 0x8;
Stefan Roese75659da2015-07-23 10:26:16 +0200488
489 for (i = 0; i < ntypes; i++) {
490 f = &builtin_flash_types[i];
491
492 if (f->chip_id == id)
493 break;
494 }
495
496 if (i == ntypes) {
Sean Andersonc6302f02020-09-15 10:44:40 -0400497 dev_err(mtd->dev, "Error: timings not found\n");
Stefan Roese75659da2015-07-23 10:26:16 +0200498 return -EINVAL;
499 }
500
501 pxa3xx_nand_set_timing(host, f->timing);
502
503 if (f->flash_width == 16) {
504 info->reg_ndcr |= NDCR_DWIDTH_M;
505 chip->options |= NAND_BUSWIDTH_16;
506 }
507
508 info->reg_ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
509 } else {
510 mode = fls(mode) - 1;
511 if (mode < 0)
512 mode = 0;
513
Chris Packhambd3ce6f2023-07-10 10:47:34 +1200514 if (info->variant == PXA3XX_NAND_VARIANT_AC5)
515 mode = min(mode, 3);
516
Stefan Roese75659da2015-07-23 10:26:16 +0200517 timings = onfi_async_timing_mode_to_sdr_timings(mode);
518 if (IS_ERR(timings))
519 return PTR_ERR(timings);
520
521 pxa3xx_nand_set_sdr_timing(host, timings);
522 }
523
524 return 0;
525}
526
Stefan Roese75659da2015-07-23 10:26:16 +0200527/**
Vagrant Cascadianbeb288b2015-11-24 14:46:24 -0800528 * NOTE: it is a must to set ND_RUN first, then write
Stefan Roese75659da2015-07-23 10:26:16 +0200529 * command buffer, otherwise, it does not work.
530 * We enable all the interrupt at the same time, and
531 * let pxa3xx_nand_irq to handle all logic.
532 */
533static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
534{
535 uint32_t ndcr;
536
537 ndcr = info->reg_ndcr;
538
539 if (info->use_ecc) {
540 ndcr |= NDCR_ECC_EN;
541 if (info->ecc_bch)
542 nand_writel(info, NDECCCTRL, 0x1);
543 } else {
544 ndcr &= ~NDCR_ECC_EN;
545 if (info->ecc_bch)
546 nand_writel(info, NDECCCTRL, 0x0);
547 }
548
549 ndcr &= ~NDCR_DMA_EN;
550
551 if (info->use_spare)
552 ndcr |= NDCR_SPARE_EN;
553 else
554 ndcr &= ~NDCR_SPARE_EN;
555
556 ndcr |= NDCR_ND_RUN;
557
558 /* clear status bits and run */
Stefan Roese75659da2015-07-23 10:26:16 +0200559 nand_writel(info, NDSR, NDSR_MASK);
Ofer Heifetzd92d8992018-08-29 11:56:03 +0300560 nand_writel(info, NDCR, 0);
Stefan Roese75659da2015-07-23 10:26:16 +0200561 nand_writel(info, NDCR, ndcr);
562}
563
564static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
565{
566 uint32_t ndcr;
567
568 ndcr = nand_readl(info, NDCR);
569 nand_writel(info, NDCR, ndcr | int_mask);
570}
571
572static void drain_fifo(struct pxa3xx_nand_info *info, void *data, int len)
573{
Miquel Raynal30a016a2018-10-11 17:45:42 +0200574 if (info->ecc_bch && !info->force_raw) {
Stefan Roese75659da2015-07-23 10:26:16 +0200575 u32 ts;
576
577 /*
578 * According to the datasheet, when reading from NDDB
579 * with BCH enabled, after each 32 bytes reads, we
580 * have to make sure that the NDSR.RDDREQ bit is set.
581 *
582 * Drain the FIFO 8 32 bits reads at a time, and skip
583 * the polling on the last read.
584 */
585 while (len > 8) {
586 readsl(info->mmio_base + NDDB, data, 8);
587
588 ts = get_timer(0);
589 while (!(nand_readl(info, NDSR) & NDSR_RDDREQ)) {
590 if (get_timer(ts) > TIMEOUT_DRAIN_FIFO) {
Sean Andersonc6302f02020-09-15 10:44:40 -0400591 dev_err(info->controller.active->mtd.dev,
Stefan Roese75659da2015-07-23 10:26:16 +0200592 "Timeout on RDDREQ while draining the FIFO\n");
593 return;
594 }
595 }
596
597 data += 32;
598 len -= 8;
599 }
600 }
601
602 readsl(info->mmio_base + NDDB, data, len);
603}
604
605static void handle_data_pio(struct pxa3xx_nand_info *info)
606{
Miquel Raynal30a016a2018-10-11 17:45:42 +0200607 int data_len = info->step_chunk_size;
608
609 /*
610 * In raw mode, include the spare area and the ECC bytes that are not
611 * consumed by the controller in the data section. Do not reorganize
612 * here, do it in the ->read_page_raw() handler instead.
613 */
614 if (info->force_raw)
615 data_len += info->step_spare_size + info->ecc_size;
616
Stefan Roese75659da2015-07-23 10:26:16 +0200617 switch (info->state) {
618 case STATE_PIO_WRITING:
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300619 if (info->step_chunk_size)
620 writesl(info->mmio_base + NDDB,
621 info->data_buff + info->data_buff_pos,
Miquel Raynal30a016a2018-10-11 17:45:42 +0200622 DIV_ROUND_UP(data_len, 4));
Stefan Roese75659da2015-07-23 10:26:16 +0200623
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300624 if (info->step_spare_size)
Stefan Roese75659da2015-07-23 10:26:16 +0200625 writesl(info->mmio_base + NDDB,
626 info->oob_buff + info->oob_buff_pos,
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300627 DIV_ROUND_UP(info->step_spare_size, 4));
Stefan Roese75659da2015-07-23 10:26:16 +0200628 break;
629 case STATE_PIO_READING:
Baruch Siach9167e4d2020-04-05 19:19:31 +0300630 if (data_len)
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300631 drain_fifo(info,
632 info->data_buff + info->data_buff_pos,
Miquel Raynal30a016a2018-10-11 17:45:42 +0200633 DIV_ROUND_UP(data_len, 4));
634
635 if (info->force_raw)
636 break;
Stefan Roese75659da2015-07-23 10:26:16 +0200637
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300638 if (info->step_spare_size)
Stefan Roese75659da2015-07-23 10:26:16 +0200639 drain_fifo(info,
640 info->oob_buff + info->oob_buff_pos,
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300641 DIV_ROUND_UP(info->step_spare_size, 4));
Stefan Roese75659da2015-07-23 10:26:16 +0200642 break;
643 default:
Sean Andersonc6302f02020-09-15 10:44:40 -0400644 dev_err(info->controller.active->mtd.dev,
645 "%s: invalid state %d\n", __func__, info->state);
Stefan Roese75659da2015-07-23 10:26:16 +0200646 BUG();
647 }
648
649 /* Update buffer pointers for multi-page read/write */
Miquel Raynal30a016a2018-10-11 17:45:42 +0200650 info->data_buff_pos += data_len;
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300651 info->oob_buff_pos += info->step_spare_size;
Stefan Roese75659da2015-07-23 10:26:16 +0200652}
653
654static void pxa3xx_nand_irq_thread(struct pxa3xx_nand_info *info)
655{
656 handle_data_pio(info);
657
658 info->state = STATE_CMD_DONE;
659 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
660}
661
662static irqreturn_t pxa3xx_nand_irq(struct pxa3xx_nand_info *info)
663{
664 unsigned int status, is_completed = 0, is_ready = 0;
665 unsigned int ready, cmd_done;
666 irqreturn_t ret = IRQ_HANDLED;
667
668 if (info->cs == 0) {
669 ready = NDSR_FLASH_RDY;
670 cmd_done = NDSR_CS0_CMDD;
671 } else {
672 ready = NDSR_RDY;
673 cmd_done = NDSR_CS1_CMDD;
674 }
675
David Sniatkiwicz2087f7e2018-08-29 11:56:18 +0300676 /* TODO - find out why we need the delay during write operation. */
677 ndelay(1);
678
Stefan Roese75659da2015-07-23 10:26:16 +0200679 status = nand_readl(info, NDSR);
680
681 if (status & NDSR_UNCORERR)
682 info->retcode = ERR_UNCORERR;
683 if (status & NDSR_CORERR) {
684 info->retcode = ERR_CORERR;
Shmuel Hazan58983222020-10-29 08:52:20 +0200685 if ((info->variant == PXA3XX_NAND_VARIANT_ARMADA370 ||
686 info->variant == PXA3XX_NAND_VARIANT_ARMADA_8K) &&
Stefan Roese75659da2015-07-23 10:26:16 +0200687 info->ecc_bch)
688 info->ecc_err_cnt = NDSR_ERR_CNT(status);
689 else
690 info->ecc_err_cnt = 1;
691
692 /*
693 * Each chunk composing a page is corrected independently,
694 * and we need to store maximum number of corrected bitflips
695 * to return it to the MTD layer in ecc.read_page().
696 */
697 info->max_bitflips = max_t(unsigned int,
698 info->max_bitflips,
699 info->ecc_err_cnt);
700 }
701 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
702 info->state = (status & NDSR_RDDREQ) ?
703 STATE_PIO_READING : STATE_PIO_WRITING;
704 /* Call the IRQ thread in U-Boot directly */
705 pxa3xx_nand_irq_thread(info);
706 return 0;
707 }
708 if (status & cmd_done) {
709 info->state = STATE_CMD_DONE;
710 is_completed = 1;
711 }
712 if (status & ready) {
713 info->state = STATE_READY;
714 is_ready = 1;
715 }
716
Ofer Heifetzde323162018-08-29 11:56:04 +0300717 /*
718 * Clear all status bit before issuing the next command, which
719 * can and will alter the status bits and will deserve a new
720 * interrupt on its own. This lets the controller exit the IRQ
721 */
722 nand_writel(info, NDSR, status);
723
Stefan Roese75659da2015-07-23 10:26:16 +0200724 if (status & NDSR_WRCMDREQ) {
Stefan Roese75659da2015-07-23 10:26:16 +0200725 status &= ~NDSR_WRCMDREQ;
726 info->state = STATE_CMD_HANDLE;
727
728 /*
729 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
730 * must be loaded by writing directly either 12 or 16
731 * bytes directly to NDCB0, four bytes at a time.
732 *
733 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
734 * but each NDCBx register can be read.
735 */
736 nand_writel(info, NDCB0, info->ndcb0);
737 nand_writel(info, NDCB0, info->ndcb1);
738 nand_writel(info, NDCB0, info->ndcb2);
739
740 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
Shmuel Hazan58983222020-10-29 08:52:20 +0200741 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 ||
Chris Packhambd3ce6f2023-07-10 10:47:34 +1200742 info->variant == PXA3XX_NAND_VARIANT_ARMADA_8K ||
743 info->variant == PXA3XX_NAND_VARIANT_AC5)
Stefan Roese75659da2015-07-23 10:26:16 +0200744 nand_writel(info, NDCB0, info->ndcb3);
745 }
746
Stefan Roese75659da2015-07-23 10:26:16 +0200747 if (is_completed)
748 info->cmd_complete = 1;
749 if (is_ready)
750 info->dev_ready = 1;
751
752 return ret;
753}
754
755static inline int is_buf_blank(uint8_t *buf, size_t len)
756{
757 for (; len > 0; len--)
758 if (*buf++ != 0xff)
759 return 0;
760 return 1;
761}
762
763static void set_command_address(struct pxa3xx_nand_info *info,
764 unsigned int page_size, uint16_t column, int page_addr)
765{
766 /* small page addr setting */
Konstantin Porotchkin06f9b6b2018-08-29 11:56:15 +0300767 if (page_size < info->chunk_size) {
Stefan Roese75659da2015-07-23 10:26:16 +0200768 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
769 | (column & 0xFF);
770
771 info->ndcb2 = 0;
772 } else {
773 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
774 | (column & 0xFFFF);
775
776 if (page_addr & 0xFF0000)
777 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
778 else
779 info->ndcb2 = 0;
780 }
781}
782
783static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
784{
785 struct pxa3xx_nand_host *host = info->host[info->cs];
Ofer Heifetz0da35df2018-08-29 11:56:01 +0300786 struct mtd_info *mtd = nand_to_mtd(&host->chip);
Stefan Roese75659da2015-07-23 10:26:16 +0200787
788 /* reset data and oob column point to handle data */
789 info->buf_start = 0;
790 info->buf_count = 0;
Stefan Roese75659da2015-07-23 10:26:16 +0200791 info->data_buff_pos = 0;
792 info->oob_buff_pos = 0;
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300793 info->step_chunk_size = 0;
794 info->step_spare_size = 0;
795 info->cur_chunk = 0;
Stefan Roese75659da2015-07-23 10:26:16 +0200796 info->use_ecc = 0;
797 info->use_spare = 1;
798 info->retcode = ERR_NONE;
799 info->ecc_err_cnt = 0;
800 info->ndcb3 = 0;
801 info->need_wait = 0;
Ravi Minnikantia68da5b2024-04-27 09:15:28 -0700802 /*
803 * Reset max_bitflips to zero. Once command is complete,
804 * max_bitflips for this READ is returned in ecc.read_page()
805 */
806 info->max_bitflips = 0;
Stefan Roese75659da2015-07-23 10:26:16 +0200807
808 switch (command) {
809 case NAND_CMD_READ0:
Boris Brezillona558a392018-08-29 11:56:12 +0300810 case NAND_CMD_READOOB:
Stefan Roese75659da2015-07-23 10:26:16 +0200811 case NAND_CMD_PAGEPROG:
Miquel Raynal30a016a2018-10-11 17:45:42 +0200812 if (!info->force_raw)
813 info->use_ecc = 1;
Stefan Roese75659da2015-07-23 10:26:16 +0200814 break;
815 case NAND_CMD_PARAM:
816 info->use_spare = 0;
817 break;
818 default:
819 info->ndcb1 = 0;
820 info->ndcb2 = 0;
821 break;
822 }
823
824 /*
825 * If we are about to issue a read command, or about to set
826 * the write address, then clean the data buffer.
827 */
828 if (command == NAND_CMD_READ0 ||
829 command == NAND_CMD_READOOB ||
830 command == NAND_CMD_SEQIN) {
831 info->buf_count = mtd->writesize + mtd->oobsize;
832 memset(info->data_buff, 0xFF, info->buf_count);
833 }
834}
835
836static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
837 int ext_cmd_type, uint16_t column, int page_addr)
838{
839 int addr_cycle, exec_cmd;
840 struct pxa3xx_nand_host *host;
841 struct mtd_info *mtd;
842
843 host = info->host[info->cs];
Ofer Heifetz0da35df2018-08-29 11:56:01 +0300844 mtd = nand_to_mtd(&host->chip);
Stefan Roese75659da2015-07-23 10:26:16 +0200845 addr_cycle = 0;
846 exec_cmd = 1;
847
848 if (info->cs != 0)
849 info->ndcb0 = NDCB0_CSEL;
850 else
851 info->ndcb0 = 0;
852
853 if (command == NAND_CMD_SEQIN)
854 exec_cmd = 0;
855
856 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
857 + host->col_addr_cycles);
858
859 switch (command) {
860 case NAND_CMD_READOOB:
861 case NAND_CMD_READ0:
862 info->buf_start = column;
863 info->ndcb0 |= NDCB0_CMD_TYPE(0)
864 | addr_cycle
865 | NAND_CMD_READ0;
866
867 if (command == NAND_CMD_READOOB)
868 info->buf_start += mtd->writesize;
869
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300870 if (info->cur_chunk < info->nfullchunks) {
871 info->step_chunk_size = info->chunk_size;
872 info->step_spare_size = info->spare_size;
873 } else {
874 info->step_chunk_size = info->last_chunk_size;
875 info->step_spare_size = info->last_spare_size;
876 }
877
Stefan Roese75659da2015-07-23 10:26:16 +0200878 /*
879 * Multiple page read needs an 'extended command type' field,
880 * which is either naked-read or last-read according to the
881 * state.
882 */
Miquel Raynal30a016a2018-10-11 17:45:42 +0200883 if (info->force_raw) {
884 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8) |
885 NDCB0_LEN_OVRD |
886 NDCB0_EXT_CMD_TYPE(ext_cmd_type);
887 info->ndcb3 = info->step_chunk_size +
888 info->step_spare_size + info->ecc_size;
889 } else if (mtd->writesize == info->chunk_size) {
Stefan Roese75659da2015-07-23 10:26:16 +0200890 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Konstantin Porotchkin06f9b6b2018-08-29 11:56:15 +0300891 } else if (mtd->writesize > info->chunk_size) {
Stefan Roese75659da2015-07-23 10:26:16 +0200892 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
893 | NDCB0_LEN_OVRD
894 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300895 info->ndcb3 = info->step_chunk_size +
896 info->step_spare_size;
Stefan Roese75659da2015-07-23 10:26:16 +0200897 }
898
899 set_command_address(info, mtd->writesize, column, page_addr);
900 break;
901
902 case NAND_CMD_SEQIN:
903
904 info->buf_start = column;
905 set_command_address(info, mtd->writesize, 0, page_addr);
906
907 /*
908 * Multiple page programming needs to execute the initial
909 * SEQIN command that sets the page address.
910 */
Konstantin Porotchkin06f9b6b2018-08-29 11:56:15 +0300911 if (mtd->writesize > info->chunk_size) {
Stefan Roese75659da2015-07-23 10:26:16 +0200912 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
913 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
914 | addr_cycle
915 | command;
Stefan Roese75659da2015-07-23 10:26:16 +0200916 exec_cmd = 1;
917 }
918 break;
919
920 case NAND_CMD_PAGEPROG:
921 if (is_buf_blank(info->data_buff,
922 (mtd->writesize + mtd->oobsize))) {
923 exec_cmd = 0;
924 break;
925 }
926
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300927 if (info->cur_chunk < info->nfullchunks) {
928 info->step_chunk_size = info->chunk_size;
929 info->step_spare_size = info->spare_size;
930 } else {
931 info->step_chunk_size = info->last_chunk_size;
932 info->step_spare_size = info->last_spare_size;
933 }
934
Stefan Roese75659da2015-07-23 10:26:16 +0200935 /* Second command setting for large pages */
Konstantin Porotchkin06f9b6b2018-08-29 11:56:15 +0300936 if (mtd->writesize > info->chunk_size) {
Stefan Roese75659da2015-07-23 10:26:16 +0200937 /*
938 * Multiple page write uses the 'extended command'
939 * field. This can be used to issue a command dispatch
940 * or a naked-write depending on the current stage.
941 */
942 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
943 | NDCB0_LEN_OVRD
944 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300945 info->ndcb3 = info->step_chunk_size +
946 info->step_spare_size;
Stefan Roese75659da2015-07-23 10:26:16 +0200947
948 /*
949 * This is the command dispatch that completes a chunked
950 * page program operation.
951 */
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300952 if (info->cur_chunk == info->ntotalchunks) {
Stefan Roese75659da2015-07-23 10:26:16 +0200953 info->ndcb0 = NDCB0_CMD_TYPE(0x1)
954 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
955 | command;
956 info->ndcb1 = 0;
957 info->ndcb2 = 0;
958 info->ndcb3 = 0;
959 }
960 } else {
961 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
962 | NDCB0_AUTO_RS
963 | NDCB0_ST_ROW_EN
964 | NDCB0_DBC
965 | (NAND_CMD_PAGEPROG << 8)
966 | NAND_CMD_SEQIN
967 | addr_cycle;
968 }
969 break;
970
971 case NAND_CMD_PARAM:
Ofer Heifetzfdf5b232018-08-29 11:56:00 +0300972 info->buf_count = INIT_BUFFER_SIZE;
Stefan Roese75659da2015-07-23 10:26:16 +0200973 info->ndcb0 |= NDCB0_CMD_TYPE(0)
974 | NDCB0_ADDR_CYC(1)
975 | NDCB0_LEN_OVRD
976 | command;
977 info->ndcb1 = (column & 0xFF);
Ofer Heifetzfdf5b232018-08-29 11:56:00 +0300978 info->ndcb3 = INIT_BUFFER_SIZE;
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300979 info->step_chunk_size = INIT_BUFFER_SIZE;
Stefan Roese75659da2015-07-23 10:26:16 +0200980 break;
981
982 case NAND_CMD_READID:
Ofer Heifetz4a574aa2018-08-29 11:56:05 +0300983 info->buf_count = READ_ID_BYTES;
Stefan Roese75659da2015-07-23 10:26:16 +0200984 info->ndcb0 |= NDCB0_CMD_TYPE(3)
985 | NDCB0_ADDR_CYC(1)
986 | command;
987 info->ndcb1 = (column & 0xFF);
988
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300989 info->step_chunk_size = 8;
Stefan Roese75659da2015-07-23 10:26:16 +0200990 break;
991 case NAND_CMD_STATUS:
992 info->buf_count = 1;
993 info->ndcb0 |= NDCB0_CMD_TYPE(4)
994 | NDCB0_ADDR_CYC(1)
995 | command;
996
Ofer Heifetz191b5be2018-08-29 11:56:09 +0300997 info->step_chunk_size = 8;
Stefan Roese75659da2015-07-23 10:26:16 +0200998 break;
999
1000 case NAND_CMD_ERASE1:
1001 info->ndcb0 |= NDCB0_CMD_TYPE(2)
1002 | NDCB0_AUTO_RS
1003 | NDCB0_ADDR_CYC(3)
1004 | NDCB0_DBC
1005 | (NAND_CMD_ERASE2 << 8)
1006 | NAND_CMD_ERASE1;
1007 info->ndcb1 = page_addr;
1008 info->ndcb2 = 0;
1009
1010 break;
1011 case NAND_CMD_RESET:
1012 info->ndcb0 |= NDCB0_CMD_TYPE(5)
1013 | command;
1014
1015 break;
1016
1017 case NAND_CMD_ERASE2:
1018 exec_cmd = 0;
1019 break;
1020
1021 default:
1022 exec_cmd = 0;
Sean Andersonc6302f02020-09-15 10:44:40 -04001023 dev_err(mtd->dev, "non-supported command %x\n",
Stefan Roese75659da2015-07-23 10:26:16 +02001024 command);
1025 break;
1026 }
1027
1028 return exec_cmd;
1029}
1030
1031static void nand_cmdfunc(struct mtd_info *mtd, unsigned command,
1032 int column, int page_addr)
1033{
Scott Wood17fed142016-05-30 13:57:56 -05001034 struct nand_chip *chip = mtd_to_nand(mtd);
1035 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001036 struct pxa3xx_nand_info *info = host->info_data;
1037 int exec_cmd;
1038
1039 /*
1040 * if this is a x16 device ,then convert the input
1041 * "byte" address into a "word" address appropriate
1042 * for indexing a word-oriented device
1043 */
1044 if (info->reg_ndcr & NDCR_DWIDTH_M)
1045 column /= 2;
1046
1047 /*
1048 * There may be different NAND chip hooked to
1049 * different chip select, so check whether
1050 * chip select has been changed, if yes, reset the timing
1051 */
1052 if (info->cs != host->cs) {
1053 info->cs = host->cs;
1054 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1055 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
1056 }
1057
1058 prepare_start_command(info, command);
1059
1060 info->state = STATE_PREPARED;
1061 exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
1062
1063 if (exec_cmd) {
1064 u32 ts;
1065
1066 info->cmd_complete = 0;
1067 info->dev_ready = 0;
1068 info->need_wait = 1;
1069 pxa3xx_nand_start(info);
1070
1071 ts = get_timer(0);
1072 while (1) {
1073 u32 status;
1074
1075 status = nand_readl(info, NDSR);
1076 if (status)
1077 pxa3xx_nand_irq(info);
1078
1079 if (info->cmd_complete)
1080 break;
1081
1082 if (get_timer(ts) > CHIP_DELAY_TIMEOUT) {
Sean Andersonc6302f02020-09-15 10:44:40 -04001083 dev_err(mtd->dev, "Wait timeout!!!\n");
Stefan Roese75659da2015-07-23 10:26:16 +02001084 return;
1085 }
1086 }
1087 }
1088 info->state = STATE_IDLE;
1089}
1090
1091static void nand_cmdfunc_extended(struct mtd_info *mtd,
1092 const unsigned command,
1093 int column, int page_addr)
1094{
Scott Wood17fed142016-05-30 13:57:56 -05001095 struct nand_chip *chip = mtd_to_nand(mtd);
1096 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001097 struct pxa3xx_nand_info *info = host->info_data;
1098 int exec_cmd, ext_cmd_type;
1099
1100 /*
1101 * if this is a x16 device then convert the input
1102 * "byte" address into a "word" address appropriate
1103 * for indexing a word-oriented device
1104 */
1105 if (info->reg_ndcr & NDCR_DWIDTH_M)
1106 column /= 2;
1107
1108 /*
1109 * There may be different NAND chip hooked to
1110 * different chip select, so check whether
1111 * chip select has been changed, if yes, reset the timing
1112 */
1113 if (info->cs != host->cs) {
1114 info->cs = host->cs;
1115 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1116 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
1117 }
1118
1119 /* Select the extended command for the first command */
1120 switch (command) {
1121 case NAND_CMD_READ0:
1122 case NAND_CMD_READOOB:
1123 ext_cmd_type = EXT_CMD_TYPE_MONO;
1124 break;
1125 case NAND_CMD_SEQIN:
1126 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
1127 break;
1128 case NAND_CMD_PAGEPROG:
1129 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
1130 break;
1131 default:
1132 ext_cmd_type = 0;
1133 break;
1134 }
1135
1136 prepare_start_command(info, command);
1137
1138 /*
1139 * Prepare the "is ready" completion before starting a command
1140 * transaction sequence. If the command is not executed the
1141 * completion will be completed, see below.
1142 *
1143 * We can do that inside the loop because the command variable
1144 * is invariant and thus so is the exec_cmd.
1145 */
1146 info->need_wait = 1;
1147 info->dev_ready = 0;
1148
1149 do {
1150 u32 ts;
1151
1152 info->state = STATE_PREPARED;
1153 exec_cmd = prepare_set_command(info, command, ext_cmd_type,
1154 column, page_addr);
1155 if (!exec_cmd) {
1156 info->need_wait = 0;
1157 info->dev_ready = 1;
1158 break;
1159 }
1160
1161 info->cmd_complete = 0;
1162 pxa3xx_nand_start(info);
1163
1164 ts = get_timer(0);
1165 while (1) {
1166 u32 status;
1167
1168 status = nand_readl(info, NDSR);
1169 if (status)
1170 pxa3xx_nand_irq(info);
1171
1172 if (info->cmd_complete)
1173 break;
1174
1175 if (get_timer(ts) > CHIP_DELAY_TIMEOUT) {
Sean Andersonc6302f02020-09-15 10:44:40 -04001176 dev_err(mtd->dev, "Wait timeout!!!\n");
Stefan Roese75659da2015-07-23 10:26:16 +02001177 return;
1178 }
1179 }
1180
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001181 /* Only a few commands need several steps */
1182 if (command != NAND_CMD_PAGEPROG &&
1183 command != NAND_CMD_READ0 &&
1184 command != NAND_CMD_READOOB)
1185 break;
1186
1187 info->cur_chunk++;
1188
Stefan Roese75659da2015-07-23 10:26:16 +02001189 /* Check if the sequence is complete */
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001190 if (info->cur_chunk == info->ntotalchunks &&
1191 command != NAND_CMD_PAGEPROG)
Stefan Roese75659da2015-07-23 10:26:16 +02001192 break;
1193
1194 /*
1195 * After a splitted program command sequence has issued
1196 * the command dispatch, the command sequence is complete.
1197 */
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001198 if (info->cur_chunk == (info->ntotalchunks + 1) &&
Stefan Roese75659da2015-07-23 10:26:16 +02001199 command == NAND_CMD_PAGEPROG &&
1200 ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
1201 break;
1202
1203 if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
1204 /* Last read: issue a 'last naked read' */
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001205 if (info->cur_chunk == info->ntotalchunks - 1)
Stefan Roese75659da2015-07-23 10:26:16 +02001206 ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
1207 else
1208 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
1209
1210 /*
1211 * If a splitted program command has no more data to transfer,
1212 * the command dispatch must be issued to complete.
1213 */
1214 } else if (command == NAND_CMD_PAGEPROG &&
Ofer Heifetz191b5be2018-08-29 11:56:09 +03001215 info->cur_chunk == info->ntotalchunks) {
Stefan Roese75659da2015-07-23 10:26:16 +02001216 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
1217 }
1218 } while (1);
1219
1220 info->state = STATE_IDLE;
1221}
1222
1223static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Scott Wood46e13102016-05-30 13:57:57 -05001224 struct nand_chip *chip, const uint8_t *buf, int oob_required,
1225 int page)
Stefan Roese75659da2015-07-23 10:26:16 +02001226{
1227 chip->write_buf(mtd, buf, mtd->writesize);
1228 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1229
1230 return 0;
1231}
1232
1233static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
1234 struct nand_chip *chip, uint8_t *buf, int oob_required,
1235 int page)
1236{
Scott Wood17fed142016-05-30 13:57:56 -05001237 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001238 struct pxa3xx_nand_info *info = host->info_data;
Miquel Raynal35f1ebd2018-10-11 17:45:43 +02001239 int bf;
Stefan Roese75659da2015-07-23 10:26:16 +02001240
1241 chip->read_buf(mtd, buf, mtd->writesize);
1242 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1243
1244 if (info->retcode == ERR_CORERR && info->use_ecc) {
1245 mtd->ecc_stats.corrected += info->ecc_err_cnt;
1246
Miquel Raynal35f1ebd2018-10-11 17:45:43 +02001247 } else if (info->retcode == ERR_UNCORERR && info->ecc_bch) {
Stefan Roese75659da2015-07-23 10:26:16 +02001248 /*
Miquel Raynal35f1ebd2018-10-11 17:45:43 +02001249 * Empty pages will trigger uncorrectable errors. Re-read the
1250 * entire page in raw mode and check for bits not being "1".
1251 * If there are more than the supported strength, then it means
1252 * this is an actual uncorrectable error.
Stefan Roese75659da2015-07-23 10:26:16 +02001253 */
Miquel Raynal35f1ebd2018-10-11 17:45:43 +02001254 chip->ecc.read_page_raw(mtd, chip, buf, oob_required, page);
1255 bf = nand_check_erased_ecc_chunk(buf, mtd->writesize,
1256 chip->oob_poi, mtd->oobsize,
1257 NULL, 0, chip->ecc.strength);
1258 if (bf < 0) {
1259 mtd->ecc_stats.failed++;
1260 } else if (bf) {
1261 mtd->ecc_stats.corrected += bf;
1262 info->max_bitflips = max_t(unsigned int,
1263 info->max_bitflips, bf);
1264 info->retcode = ERR_CORERR;
1265 } else {
1266 info->retcode = ERR_NONE;
1267 }
1268
1269 } else if (info->retcode == ERR_UNCORERR && !info->ecc_bch) {
1270 /* Raw read is not supported with Hamming ECC engine */
Stefan Roese75659da2015-07-23 10:26:16 +02001271 if (is_buf_blank(buf, mtd->writesize))
1272 info->retcode = ERR_NONE;
1273 else
1274 mtd->ecc_stats.failed++;
1275 }
1276
1277 return info->max_bitflips;
1278}
1279
Miquel Raynal30a016a2018-10-11 17:45:42 +02001280static int pxa3xx_nand_read_page_raw(struct mtd_info *mtd,
1281 struct nand_chip *chip, uint8_t *buf,
1282 int oob_required, int page)
1283{
1284 struct pxa3xx_nand_host *host = chip->priv;
1285 struct pxa3xx_nand_info *info = host->info_data;
1286 int chunk, ecc_off_buf;
1287
1288 if (!info->ecc_bch)
1289 return -ENOTSUPP;
1290
1291 /*
1292 * Set the force_raw boolean, then re-call ->cmdfunc() that will run
1293 * pxa3xx_nand_start(), which will actually disable the ECC engine.
1294 */
1295 info->force_raw = true;
1296 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1297
1298 ecc_off_buf = (info->nfullchunks * info->spare_size) +
1299 info->last_spare_size;
1300 for (chunk = 0; chunk < info->nfullchunks; chunk++) {
1301 chip->read_buf(mtd,
1302 buf + (chunk * info->chunk_size),
1303 info->chunk_size);
1304 chip->read_buf(mtd,
1305 chip->oob_poi +
1306 (chunk * (info->spare_size)),
1307 info->spare_size);
1308 chip->read_buf(mtd,
1309 chip->oob_poi + ecc_off_buf +
1310 (chunk * (info->ecc_size)),
1311 info->ecc_size - 2);
1312 }
1313
1314 if (info->ntotalchunks > info->nfullchunks) {
1315 chip->read_buf(mtd,
1316 buf + (info->nfullchunks * info->chunk_size),
1317 info->last_chunk_size);
1318 chip->read_buf(mtd,
1319 chip->oob_poi +
1320 (info->nfullchunks * (info->spare_size)),
1321 info->last_spare_size);
1322 chip->read_buf(mtd,
1323 chip->oob_poi + ecc_off_buf +
1324 (info->nfullchunks * (info->ecc_size)),
1325 info->ecc_size - 2);
1326 }
1327
1328 info->force_raw = false;
1329
1330 return 0;
1331}
1332
1333static int pxa3xx_nand_read_oob_raw(struct mtd_info *mtd,
1334 struct nand_chip *chip, int page)
1335{
1336 /* Invalidate page cache */
1337 chip->pagebuf = -1;
1338
1339 return chip->ecc.read_page_raw(mtd, chip, chip->buffers->databuf, true,
1340 page);
1341}
1342
Stefan Roese75659da2015-07-23 10:26:16 +02001343static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
1344{
Scott Wood17fed142016-05-30 13:57:56 -05001345 struct nand_chip *chip = mtd_to_nand(mtd);
1346 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001347 struct pxa3xx_nand_info *info = host->info_data;
1348 char retval = 0xFF;
1349
1350 if (info->buf_start < info->buf_count)
1351 /* Has just send a new command? */
1352 retval = info->data_buff[info->buf_start++];
1353
1354 return retval;
1355}
1356
1357static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
1358{
Scott Wood17fed142016-05-30 13:57:56 -05001359 struct nand_chip *chip = mtd_to_nand(mtd);
1360 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001361 struct pxa3xx_nand_info *info = host->info_data;
1362 u16 retval = 0xFFFF;
1363
1364 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
1365 retval = *((u16 *)(info->data_buff+info->buf_start));
1366 info->buf_start += 2;
1367 }
1368 return retval;
1369}
1370
1371static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1372{
Scott Wood17fed142016-05-30 13:57:56 -05001373 struct nand_chip *chip = mtd_to_nand(mtd);
1374 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001375 struct pxa3xx_nand_info *info = host->info_data;
1376 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1377
1378 memcpy(buf, info->data_buff + info->buf_start, real_len);
1379 info->buf_start += real_len;
1380}
1381
1382static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
1383 const uint8_t *buf, int len)
1384{
Scott Wood17fed142016-05-30 13:57:56 -05001385 struct nand_chip *chip = mtd_to_nand(mtd);
1386 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001387 struct pxa3xx_nand_info *info = host->info_data;
1388 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1389
1390 memcpy(info->data_buff + info->buf_start, buf, real_len);
1391 info->buf_start += real_len;
1392}
1393
1394static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
1395{
1396 return;
1397}
1398
1399static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
1400{
Scott Wood17fed142016-05-30 13:57:56 -05001401 struct nand_chip *chip = mtd_to_nand(mtd);
1402 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001403 struct pxa3xx_nand_info *info = host->info_data;
1404
1405 if (info->need_wait) {
1406 u32 ts;
1407
1408 info->need_wait = 0;
1409
1410 ts = get_timer(0);
1411 while (1) {
1412 u32 status;
1413
1414 status = nand_readl(info, NDSR);
1415 if (status)
1416 pxa3xx_nand_irq(info);
1417
1418 if (info->dev_ready)
1419 break;
1420
1421 if (get_timer(ts) > CHIP_DELAY_TIMEOUT) {
Sean Andersonc6302f02020-09-15 10:44:40 -04001422 dev_err(mtd->dev, "Ready timeout!!!\n");
Stefan Roese75659da2015-07-23 10:26:16 +02001423 return NAND_STATUS_FAIL;
1424 }
1425 }
1426 }
1427
1428 /* pxa3xx_nand_send_command has waited for command complete */
1429 if (this->state == FL_WRITING || this->state == FL_ERASING) {
1430 if (info->retcode == ERR_NONE)
1431 return 0;
1432 else
1433 return NAND_STATUS_FAIL;
1434 }
1435
1436 return NAND_STATUS_READY;
1437}
1438
Ofer Heifetz531816e2018-08-29 11:56:07 +03001439static int pxa3xx_nand_config_ident(struct pxa3xx_nand_info *info)
1440{
1441 struct pxa3xx_nand_platform_data *pdata = info->pdata;
1442
1443 /* Configure default flash values */
Ofer Heifetz531816e2018-08-29 11:56:07 +03001444 info->reg_ndcr = 0x0; /* enable all interrupts */
1445 info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
1446 info->reg_ndcr |= NDCR_RD_ID_CNT(READ_ID_BYTES);
1447 info->reg_ndcr |= NDCR_SPARE_EN;
1448
1449 return 0;
1450}
1451
1452static void pxa3xx_nand_config_tail(struct pxa3xx_nand_info *info)
Stefan Roese75659da2015-07-23 10:26:16 +02001453{
1454 struct pxa3xx_nand_host *host = info->host[info->cs];
Ofer Heifetz531816e2018-08-29 11:56:07 +03001455 struct mtd_info *mtd = nand_to_mtd(&info->host[info->cs]->chip);
Scott Wood17fed142016-05-30 13:57:56 -05001456 struct nand_chip *chip = mtd_to_nand(mtd);
Stefan Roese75659da2015-07-23 10:26:16 +02001457
1458 info->reg_ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
1459 info->reg_ndcr |= (chip->page_shift == 6) ? NDCR_PG_PER_BLK : 0;
1460 info->reg_ndcr |= (mtd->writesize == 2048) ? NDCR_PAGE_SZ : 0;
Stefan Roese75659da2015-07-23 10:26:16 +02001461}
1462
Ofer Heifetz268979f2018-08-29 11:56:08 +03001463static void pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
Stefan Roese75659da2015-07-23 10:26:16 +02001464{
Ofer Heifetz531816e2018-08-29 11:56:07 +03001465 struct pxa3xx_nand_platform_data *pdata = info->pdata;
Stefan Roese75659da2015-07-23 10:26:16 +02001466 uint32_t ndcr = nand_readl(info, NDCR);
1467
Stefan Roese75659da2015-07-23 10:26:16 +02001468 /* Set an initial chunk size */
Ofer Heifetz4a574aa2018-08-29 11:56:05 +03001469 info->chunk_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
Ofer Heifetz531816e2018-08-29 11:56:07 +03001470 info->reg_ndcr = ndcr &
1471 ~(NDCR_INT_MASK | NDCR_ND_ARB_EN | NFCV1_NDCR_ARB_CNTL);
1472 info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Stefan Roese75659da2015-07-23 10:26:16 +02001473 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
1474 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Stefan Roese75659da2015-07-23 10:26:16 +02001475}
1476
1477static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1478{
1479 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1480 if (info->data_buff == NULL)
1481 return -ENOMEM;
1482 return 0;
1483}
1484
1485static int pxa3xx_nand_sensing(struct pxa3xx_nand_host *host)
1486{
1487 struct pxa3xx_nand_info *info = host->info_data;
1488 struct pxa3xx_nand_platform_data *pdata = info->pdata;
1489 struct mtd_info *mtd;
1490 struct nand_chip *chip;
1491 const struct nand_sdr_timings *timings;
1492 int ret;
1493
Ofer Heifetz0da35df2018-08-29 11:56:01 +03001494 mtd = nand_to_mtd(&info->host[info->cs]->chip);
Scott Wood17fed142016-05-30 13:57:56 -05001495 chip = mtd_to_nand(mtd);
Stefan Roese75659da2015-07-23 10:26:16 +02001496
1497 /* configure default flash values */
1498 info->reg_ndcr = 0x0; /* enable all interrupts */
1499 info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Ofer Heifetz4a574aa2018-08-29 11:56:05 +03001500 info->reg_ndcr |= NDCR_RD_ID_CNT(READ_ID_BYTES);
Stefan Roese75659da2015-07-23 10:26:16 +02001501 info->reg_ndcr |= NDCR_SPARE_EN; /* enable spare by default */
1502
1503 /* use the common timing to make a try */
1504 timings = onfi_async_timing_mode_to_sdr_timings(0);
1505 if (IS_ERR(timings))
1506 return PTR_ERR(timings);
1507
1508 pxa3xx_nand_set_sdr_timing(host, timings);
1509
1510 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
1511 ret = chip->waitfunc(mtd, chip);
1512 if (ret & NAND_STATUS_FAIL)
1513 return -ENODEV;
1514
1515 return 0;
1516}
1517
1518static int pxa_ecc_init(struct pxa3xx_nand_info *info,
1519 struct nand_ecc_ctrl *ecc,
1520 int strength, int ecc_stepsize, int page_size)
1521{
Chris Packham03085ca2022-08-25 16:59:49 +12001522 int i = 0;
Stefan Roese75659da2015-07-23 10:26:16 +02001523
Chris Packham03085ca2022-08-25 16:59:49 +12001524 /* if ecc strength is 1 ecc algo is Hamming else bch */
1525 info->ecc_bch = (strength == 1) ? 0 : 1;
Stefan Roese75659da2015-07-23 10:26:16 +02001526
Chris Packham03085ca2022-08-25 16:59:49 +12001527 ecc->mode = NAND_ECC_HW;
Stefan Roese75659da2015-07-23 10:26:16 +02001528
Chris Packham03085ca2022-08-25 16:59:49 +12001529 /* ecc->layout is not in use for pxa driver (but shouldn't be NULL)*/
1530 if (info->ecc_bch == 1)
1531 ecc->layout = &ecc_layout_empty;
Stefan Roese75659da2015-07-23 10:26:16 +02001532
Chris Packham03085ca2022-08-25 16:59:49 +12001533 /* for bch actual ecc strength is 16 per chunk */
1534 ecc->strength = (info->ecc_bch == 1) ? 16 : 1;
Konstantin Porotchkine0e232e2018-08-29 11:56:17 +03001535
Chris Packham03085ca2022-08-25 16:59:49 +12001536 while (nfc_layouts[i].strength) {
1537 if (strength == nfc_layouts[i].strength && page_size == nfc_layouts[i].page_size) {
1538 info->nfullchunks = nfc_layouts[i].nfullchunks;
1539 info->chunk_size = nfc_layouts[i].chunk_size;
1540 info->spare_size = nfc_layouts[i].spare_size;
1541 info->last_chunk_size = nfc_layouts[i].last_chunk_size;
1542 info->last_spare_size = nfc_layouts[i].last_spare_size;
1543 info->ntotalchunks = (info->last_spare_size || info->last_chunk_size) ?
1544 info->nfullchunks + 1 : info->nfullchunks;
1545 info->ecc_size = nfc_layouts[i].ecc_size;
1546 break;
1547 }
1548 ++i;
1549 }
Konstantin Porotchkine0e232e2018-08-29 11:56:17 +03001550
Chris Packham03085ca2022-08-25 16:59:49 +12001551 /* for bch the ecc is calculated per chunk size and for Hamming it is 512 */
1552 ecc->size = (info->ecc_bch) ? info->chunk_size : 512;
Konstantin Porotchkina692cde2018-08-29 11:56:16 +03001553
Chris Packham03085ca2022-08-25 16:59:49 +12001554 /* nand_scan_tail func perform validity tests for ECC strength, and it
1555 * assumes that all chunks are with same size. in our case when ecc is 12
1556 * the chunk size is 704 but the last chunk is with different size so
1557 * we cheat it nand_scan_tail validity tests by set info->ecc_size value to 512
1558 */
1559 if (strength == 12)
1560 ecc->size = 512;
Konstantin Porotchkina692cde2018-08-29 11:56:16 +03001561
Chris Packham03085ca2022-08-25 16:59:49 +12001562 if (ecc_stepsize != 512 || !(nfc_layouts[i].strength)) {
Sean Andersonc6302f02020-09-15 10:44:40 -04001563 dev_err(info->controller.active->mtd.dev,
Stefan Roese75659da2015-07-23 10:26:16 +02001564 "ECC strength %d at page size %d is not supported\n",
1565 strength, page_size);
1566 return -ENODEV;
1567 }
1568
1569 return 0;
1570}
1571
1572static int pxa3xx_nand_scan(struct mtd_info *mtd)
1573{
Scott Wood17fed142016-05-30 13:57:56 -05001574 struct nand_chip *chip = mtd_to_nand(mtd);
1575 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001576 struct pxa3xx_nand_info *info = host->info_data;
1577 struct pxa3xx_nand_platform_data *pdata = info->pdata;
Stefan Roese75659da2015-07-23 10:26:16 +02001578 int ret;
1579 uint16_t ecc_strength, ecc_step;
1580
Ofer Heifetz268979f2018-08-29 11:56:08 +03001581 if (pdata->keep_config) {
1582 pxa3xx_nand_detect_config(info);
1583 } else {
1584 ret = pxa3xx_nand_config_ident(info);
1585 if (ret)
1586 return ret;
1587 ret = pxa3xx_nand_sensing(host);
1588 if (ret) {
Sean Andersonc6302f02020-09-15 10:44:40 -04001589 dev_info(mtd->dev, "There is no chip on cs %d!\n",
Ofer Heifetz268979f2018-08-29 11:56:08 +03001590 info->cs);
1591 return ret;
1592 }
Stefan Roese75659da2015-07-23 10:26:16 +02001593 }
1594
Stefan Roese75659da2015-07-23 10:26:16 +02001595 /* Device detection must be done with ECC disabled */
Shmuel Hazan58983222020-10-29 08:52:20 +02001596 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 ||
Chris Packhambd3ce6f2023-07-10 10:47:34 +12001597 info->variant == PXA3XX_NAND_VARIANT_ARMADA_8K ||
1598 info->variant == PXA3XX_NAND_VARIANT_AC5)
Stefan Roese75659da2015-07-23 10:26:16 +02001599 nand_writel(info, NDECCCTRL, 0x0);
1600
1601 if (nand_scan_ident(mtd, 1, NULL))
1602 return -ENODEV;
1603
1604 if (!pdata->keep_config) {
1605 ret = pxa3xx_nand_init_timings(host);
1606 if (ret) {
Sean Andersonc6302f02020-09-15 10:44:40 -04001607 dev_err(mtd->dev,
Stefan Roese75659da2015-07-23 10:26:16 +02001608 "Failed to set timings: %d\n", ret);
1609 return ret;
1610 }
1611 }
1612
Stefan Roese75659da2015-07-23 10:26:16 +02001613#ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1614 /*
1615 * We'll use a bad block table stored in-flash and don't
1616 * allow writing the bad block marker to the flash.
1617 */
1618 chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB_BBM;
1619 chip->bbt_td = &bbt_main_descr;
1620 chip->bbt_md = &bbt_mirror_descr;
1621#endif
1622
Stefan Roese75659da2015-07-23 10:26:16 +02001623 if (pdata->ecc_strength && pdata->ecc_step_size) {
1624 ecc_strength = pdata->ecc_strength;
1625 ecc_step = pdata->ecc_step_size;
1626 } else {
1627 ecc_strength = chip->ecc_strength_ds;
1628 ecc_step = chip->ecc_step_ds;
1629 }
1630
1631 /* Set default ECC strength requirements on non-ONFI devices */
1632 if (ecc_strength < 1 && ecc_step < 1) {
1633 ecc_strength = 1;
1634 ecc_step = 512;
1635 }
1636
1637 ret = pxa_ecc_init(info, &chip->ecc, ecc_strength,
1638 ecc_step, mtd->writesize);
1639 if (ret)
1640 return ret;
1641
Konstantin Porotchkin06f9b6b2018-08-29 11:56:15 +03001642 /*
1643 * If the page size is bigger than the FIFO size, let's check
1644 * we are given the right variant and then switch to the extended
1645 * (aka split) command handling,
1646 */
1647 if (mtd->writesize > info->chunk_size) {
Shmuel Hazan58983222020-10-29 08:52:20 +02001648 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 ||
Chris Packhambd3ce6f2023-07-10 10:47:34 +12001649 info->variant == PXA3XX_NAND_VARIANT_ARMADA_8K ||
1650 info->variant == PXA3XX_NAND_VARIANT_AC5) {
Konstantin Porotchkin06f9b6b2018-08-29 11:56:15 +03001651 chip->cmdfunc = nand_cmdfunc_extended;
1652 } else {
Sean Andersonc6302f02020-09-15 10:44:40 -04001653 dev_err(mtd->dev,
Konstantin Porotchkin06f9b6b2018-08-29 11:56:15 +03001654 "unsupported page size on this variant\n");
1655 return -ENODEV;
1656 }
1657 }
1658
Stefan Roese75659da2015-07-23 10:26:16 +02001659 /* calculate addressing information */
1660 if (mtd->writesize >= 2048)
1661 host->col_addr_cycles = 2;
1662 else
1663 host->col_addr_cycles = 1;
1664
1665 /* release the initial buffer */
1666 kfree(info->data_buff);
1667
1668 /* allocate the real data + oob buffer */
1669 info->buf_size = mtd->writesize + mtd->oobsize;
1670 ret = pxa3xx_nand_init_buff(info);
1671 if (ret)
1672 return ret;
1673 info->oob_buff = info->data_buff + mtd->writesize;
1674
1675 if ((mtd->size >> chip->page_shift) > 65536)
1676 host->row_addr_cycles = 3;
1677 else
1678 host->row_addr_cycles = 2;
Ofer Heifetz531816e2018-08-29 11:56:07 +03001679
1680 if (!pdata->keep_config)
1681 pxa3xx_nand_config_tail(info);
1682
Stefan Roese75659da2015-07-23 10:26:16 +02001683 return nand_scan_tail(mtd);
1684}
1685
Shmuel Hazan58983222020-10-29 08:52:20 +02001686static int alloc_nand_resource(struct udevice *dev, struct pxa3xx_nand_info *info)
Stefan Roese75659da2015-07-23 10:26:16 +02001687{
1688 struct pxa3xx_nand_platform_data *pdata;
1689 struct pxa3xx_nand_host *host;
1690 struct nand_chip *chip = NULL;
1691 struct mtd_info *mtd;
Baruch Siach807ae582020-10-29 08:52:19 +02001692 int cs;
Stefan Roese75659da2015-07-23 10:26:16 +02001693
1694 pdata = info->pdata;
1695 if (pdata->num_cs <= 0)
1696 return -ENODEV;
1697
Shmuel Hazan58983222020-10-29 08:52:20 +02001698 info->variant = pxa3xx_nand_get_variant(dev);
Stefan Roese75659da2015-07-23 10:26:16 +02001699 for (cs = 0; cs < pdata->num_cs; cs++) {
Kevin Smith4d21b592016-01-14 16:01:38 +00001700 chip = (struct nand_chip *)
1701 ((u8 *)&info[1] + sizeof(*host) * cs);
Scott Wood17fed142016-05-30 13:57:56 -05001702 mtd = nand_to_mtd(chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001703 host = (struct pxa3xx_nand_host *)chip;
1704 info->host[cs] = host;
Stefan Roese75659da2015-07-23 10:26:16 +02001705 host->cs = cs;
1706 host->info_data = info;
Stefan Roese75659da2015-07-23 10:26:16 +02001707 mtd->owner = THIS_MODULE;
1708
Chris Packham3c2170a2016-08-29 15:20:52 +12001709 nand_set_controller_data(chip, host);
Stefan Roese75659da2015-07-23 10:26:16 +02001710 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
Miquel Raynal30a016a2018-10-11 17:45:42 +02001711 chip->ecc.read_page_raw = pxa3xx_nand_read_page_raw;
1712 chip->ecc.read_oob_raw = pxa3xx_nand_read_oob_raw;
Stefan Roese75659da2015-07-23 10:26:16 +02001713 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1714 chip->controller = &info->controller;
1715 chip->waitfunc = pxa3xx_nand_waitfunc;
1716 chip->select_chip = pxa3xx_nand_select_chip;
1717 chip->read_word = pxa3xx_nand_read_word;
1718 chip->read_byte = pxa3xx_nand_read_byte;
1719 chip->read_buf = pxa3xx_nand_read_buf;
1720 chip->write_buf = pxa3xx_nand_write_buf;
1721 chip->options |= NAND_NO_SUBPAGE_WRITE;
1722 chip->cmdfunc = nand_cmdfunc;
1723 }
1724
Stefan Roese75659da2015-07-23 10:26:16 +02001725 /* Allocate a buffer to allow flash detection */
1726 info->buf_size = INIT_BUFFER_SIZE;
1727 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
Baruch Siach807ae582020-10-29 08:52:19 +02001728 if (info->data_buff == NULL)
1729 return -ENOMEM;
Stefan Roese75659da2015-07-23 10:26:16 +02001730
1731 /* initialize all interrupts to be disabled */
1732 disable_int(info, NDSR_MASK);
1733
Shmuel Hazan58983222020-10-29 08:52:20 +02001734 /*
1735 * Some SoCs like A7k/A8k need to enable manually the NAND
1736 * controller to avoid being bootloader dependent. This is done
1737 * through the use of a single bit in the System Functions registers.
1738 */
1739 if (pxa3xx_nand_get_variant(dev) == PXA3XX_NAND_VARIANT_ARMADA_8K) {
1740 struct regmap *sysctrl_base = syscon_regmap_lookup_by_phandle(
1741 dev, "marvell,system-controller");
1742 u32 reg;
1743
1744 if (IS_ERR(sysctrl_base))
1745 return PTR_ERR(sysctrl_base);
1746
1747 regmap_read(sysctrl_base, GENCONF_SOC_DEVICE_MUX, &reg);
Chris Packhamf2c1ea72023-07-10 10:47:35 +12001748 reg |= GENCONF_SOC_DEVICE_MUX_NFC_EN | GENCONF_SOC_DEVICE_MUX_NFC_DEVBUS_ARB_EN;
Shmuel Hazan58983222020-10-29 08:52:20 +02001749 regmap_write(sysctrl_base, GENCONF_SOC_DEVICE_MUX, reg);
1750 }
1751
Stefan Roese75659da2015-07-23 10:26:16 +02001752 return 0;
Stefan Roese75659da2015-07-23 10:26:16 +02001753}
1754
Shmuel Hazan759349e2020-10-29 08:52:18 +02001755static int pxa3xx_nand_probe_dt(struct udevice *dev, struct pxa3xx_nand_info *info)
Stefan Roese75659da2015-07-23 10:26:16 +02001756{
1757 struct pxa3xx_nand_platform_data *pdata;
1758
1759 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1760 if (!pdata)
1761 return -ENOMEM;
1762
Shmuel Hazan759349e2020-10-29 08:52:18 +02001763 info->mmio_base = dev_read_addr_ptr(dev);
Stefan Roese75659da2015-07-23 10:26:16 +02001764
Shmuel Hazan759349e2020-10-29 08:52:18 +02001765 pdata->num_cs = dev_read_u32_default(dev, "num-cs", 1);
1766 if (pdata->num_cs != 1) {
1767 pr_err("pxa3xx driver supports single CS only\n");
1768 return -EINVAL;
1769 }
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +03001770
Pierre Bourdonfa7890e2021-12-25 05:46:29 +01001771 if (dev_read_bool(dev, "marvell,nand-enable-arbiter"))
Shmuel Hazan759349e2020-10-29 08:52:18 +02001772 pdata->enable_arbiter = 1;
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +03001773
Pierre Bourdonfa7890e2021-12-25 05:46:29 +01001774 if (dev_read_bool(dev, "marvell,nand-keep-config"))
Shmuel Hazan759349e2020-10-29 08:52:18 +02001775 pdata->keep_config = 1;
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +03001776
Shmuel Hazan759349e2020-10-29 08:52:18 +02001777 /*
1778 * ECC parameters.
1779 * If these are not set, they will be selected according
1780 * to the detected flash type.
1781 */
1782 /* ECC strength */
1783 pdata->ecc_strength = dev_read_u32_default(dev, "nand-ecc-strength", 0);
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +03001784
Shmuel Hazan759349e2020-10-29 08:52:18 +02001785 /* ECC step size */
1786 pdata->ecc_step_size = dev_read_u32_default(dev, "nand-ecc-step-size",
1787 0);
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +03001788
Shmuel Hazan759349e2020-10-29 08:52:18 +02001789 info->pdata = pdata;
Konstantin Porotchkin93c9f392017-03-28 18:16:54 +03001790
Shmuel Hazan759349e2020-10-29 08:52:18 +02001791 return 0;
Stefan Roese75659da2015-07-23 10:26:16 +02001792}
1793
Shmuel Hazan759349e2020-10-29 08:52:18 +02001794static int pxa3xx_nand_probe(struct udevice *dev)
Stefan Roese75659da2015-07-23 10:26:16 +02001795{
1796 struct pxa3xx_nand_platform_data *pdata;
1797 int ret, cs, probe_success;
Shmuel Hazan759349e2020-10-29 08:52:18 +02001798 struct pxa3xx_nand_info *info = dev_get_priv(dev);
Stefan Roese75659da2015-07-23 10:26:16 +02001799
Shmuel Hazan759349e2020-10-29 08:52:18 +02001800 ret = pxa3xx_nand_probe_dt(dev, info);
Stefan Roese75659da2015-07-23 10:26:16 +02001801 if (ret)
1802 return ret;
1803
1804 pdata = info->pdata;
1805
Shmuel Hazan58983222020-10-29 08:52:20 +02001806 ret = alloc_nand_resource(dev, info);
Stefan Roese75659da2015-07-23 10:26:16 +02001807 if (ret) {
Shmuel Hazan759349e2020-10-29 08:52:18 +02001808 dev_err(dev, "alloc nand resource failed\n");
Stefan Roese75659da2015-07-23 10:26:16 +02001809 return ret;
1810 }
1811
1812 probe_success = 0;
1813 for (cs = 0; cs < pdata->num_cs; cs++) {
Ofer Heifetz0da35df2018-08-29 11:56:01 +03001814 struct mtd_info *mtd = nand_to_mtd(&info->host[cs]->chip);
Stefan Roese75659da2015-07-23 10:26:16 +02001815
1816 /*
1817 * The mtd name matches the one used in 'mtdparts' kernel
1818 * parameter. This name cannot be changed or otherwise
1819 * user's mtd partitions configuration would get broken.
1820 */
1821 mtd->name = "pxa3xx_nand-0";
Robert Marko142f41a2022-01-05 16:01:00 +01001822 mtd->dev = dev;
Stefan Roese75659da2015-07-23 10:26:16 +02001823 info->cs = cs;
1824 ret = pxa3xx_nand_scan(mtd);
1825 if (ret) {
Sean Andersonc6302f02020-09-15 10:44:40 -04001826 dev_info(mtd->dev, "failed to scan nand at cs %d\n",
Stefan Roese75659da2015-07-23 10:26:16 +02001827 cs);
1828 continue;
1829 }
1830
Scott Wood2c1b7e12016-05-30 13:57:55 -05001831 if (nand_register(cs, mtd))
1832 continue;
1833
1834 probe_success = 1;
Stefan Roese75659da2015-07-23 10:26:16 +02001835 }
1836
1837 if (!probe_success)
1838 return -ENODEV;
1839
1840 return 0;
1841}
1842
Shmuel Hazan759349e2020-10-29 08:52:18 +02001843U_BOOT_DRIVER(pxa3xx_nand) = {
1844 .name = "pxa3xx-nand",
1845 .id = UCLASS_MTD,
1846 .of_match = pxa3xx_nand_dt_ids,
1847 .probe = pxa3xx_nand_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001848 .priv_auto = sizeof(struct pxa3xx_nand_info) +
Shmuel Hazan759349e2020-10-29 08:52:18 +02001849 sizeof(struct pxa3xx_nand_host) * CONFIG_SYS_MAX_NAND_DEVICE,
1850};
1851
Stefan Roese75659da2015-07-23 10:26:16 +02001852void board_nand_init(void)
1853{
Shmuel Hazan759349e2020-10-29 08:52:18 +02001854 struct udevice *dev;
Stefan Roese75659da2015-07-23 10:26:16 +02001855 int ret;
1856
Shmuel Hazan759349e2020-10-29 08:52:18 +02001857 ret = uclass_get_device_by_driver(UCLASS_MTD,
Simon Glass65130cd2020-12-28 20:34:56 -07001858 DM_DRIVER_GET(pxa3xx_nand), &dev);
Shmuel Hazan759349e2020-10-29 08:52:18 +02001859 if (ret && ret != -ENODEV) {
1860 pr_err("Failed to initialize %s. (error %d)\n", dev->name,
1861 ret);
1862 }
Stefan Roese75659da2015-07-23 10:26:16 +02001863}