blob: 11b0247b2842b7b9c91f3826de8f58dedd7f3d36 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasut913a7252011-11-08 23:18:16 +00002/*
3 * Freescale i.MX28 NAND flash driver
4 *
5 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
6 * on behalf of DENX Software Engineering GmbH
7 *
8 * Based on code from LTIB:
9 * Freescale GPMI NFC NAND Flash Driver
10 *
11 * Copyright (C) 2010 Freescale Semiconductor, Inc.
12 * Copyright (C) 2008 Embedded Alley Solutions, Inc.
Peng Fan9e813732020-05-04 22:08:53 +080013 * Copyright 2017-2019 NXP
Marek Vasut913a7252011-11-08 23:18:16 +000014 */
15
Michael Trimarchifd6e13e2022-08-30 16:48:47 +020016#include <clk.h>
Simon Glass63334482019-11-14 12:57:39 -070017#include <cpu_func.h>
Stefan Agner19f90512018-06-22 18:06:16 +020018#include <dm.h>
Sean Anderson0a749442020-10-04 21:39:45 -040019#include <dm/device_compat.h>
Marek Vasut913a7252011-11-08 23:18:16 +000020#include <malloc.h>
Sean Anderson0a749442020-10-04 21:39:45 -040021#include <mxs_nand.h>
Marek Vasut913a7252011-11-08 23:18:16 +000022#include <asm/arch/clock.h>
23#include <asm/arch/imx-regs.h>
Sean Anderson0a749442020-10-04 21:39:45 -040024#include <asm/arch/sys_proto.h>
25#include <asm/cache.h>
26#include <asm/io.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020027#include <asm/mach-imx/regs-bch.h>
28#include <asm/mach-imx/regs-gpmi.h>
Michael Trimarchifd6e13e2022-08-30 16:48:47 +020029#include <linux/delay.h>
Sean Anderson0a749442020-10-04 21:39:45 -040030#include <linux/errno.h>
31#include <linux/mtd/rawnand.h>
32#include <linux/sizes.h>
Igor Prusovc3421ea2023-11-09 20:10:04 +030033#include <linux/time.h>
Sean Anderson0a749442020-10-04 21:39:45 -040034#include <linux/types.h>
Michael Trimarchifd6e13e2022-08-30 16:48:47 +020035#include <linux/math64.h>
Marek Vasut913a7252011-11-08 23:18:16 +000036
37#define MXS_NAND_DMA_DESCRIPTOR_COUNT 4
38
Peng Fan128abf42020-05-04 22:09:00 +080039#if defined(CONFIG_MX6) || defined(CONFIG_MX7) || defined(CONFIG_IMX8) || \
40 defined(CONFIG_IMX8M)
Stefan Roese8338d1d2013-04-15 21:14:12 +000041#define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT 2
42#else
43#define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT 0
44#endif
Marek Vasut913a7252011-11-08 23:18:16 +000045#define MXS_NAND_METADATA_SIZE 10
Jörg Krause1d870262015-04-15 09:27:22 +020046#define MXS_NAND_BITS_PER_ECC_LEVEL 13
Stefan Agner54bf8082016-08-01 23:55:18 -070047
48#if !defined(CONFIG_SYS_CACHELINE_SIZE) || CONFIG_SYS_CACHELINE_SIZE < 32
Marek Vasut913a7252011-11-08 23:18:16 +000049#define MXS_NAND_COMMAND_BUFFER_SIZE 32
Stefan Agner54bf8082016-08-01 23:55:18 -070050#else
51#define MXS_NAND_COMMAND_BUFFER_SIZE CONFIG_SYS_CACHELINE_SIZE
52#endif
Marek Vasut913a7252011-11-08 23:18:16 +000053
54#define MXS_NAND_BCH_TIMEOUT 10000
Michael Trimarchifd6e13e2022-08-30 16:48:47 +020055
56#define TO_CYCLES(duration, period) DIV_ROUND_UP_ULL(duration, period)
Marek Vasut913a7252011-11-08 23:18:16 +000057
Marek Vasut913a7252011-11-08 23:18:16 +000058struct nand_ecclayout fake_ecc_layout;
59
Marek Vasut1b120e82012-03-15 18:33:19 +000060/*
61 * Cache management functions
62 */
Trevor Woerner43ec7e02019-05-03 09:41:00 -040063#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Marek Vasut1b120e82012-03-15 18:33:19 +000064static void mxs_nand_flush_data_buf(struct mxs_nand_info *info)
65{
Peng Fan128abf42020-05-04 22:09:00 +080066 uint32_t addr = (uintptr_t)info->data_buf;
Marek Vasut1b120e82012-03-15 18:33:19 +000067
68 flush_dcache_range(addr, addr + info->data_buf_size);
69}
70
71static void mxs_nand_inval_data_buf(struct mxs_nand_info *info)
72{
Peng Fan128abf42020-05-04 22:09:00 +080073 uint32_t addr = (uintptr_t)info->data_buf;
Marek Vasut1b120e82012-03-15 18:33:19 +000074
75 invalidate_dcache_range(addr, addr + info->data_buf_size);
76}
77
78static void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info)
79{
Peng Fan128abf42020-05-04 22:09:00 +080080 uint32_t addr = (uintptr_t)info->cmd_buf;
Marek Vasut1b120e82012-03-15 18:33:19 +000081
82 flush_dcache_range(addr, addr + MXS_NAND_COMMAND_BUFFER_SIZE);
83}
84#else
85static inline void mxs_nand_flush_data_buf(struct mxs_nand_info *info) {}
86static inline void mxs_nand_inval_data_buf(struct mxs_nand_info *info) {}
87static inline void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info) {}
88#endif
89
Marek Vasut913a7252011-11-08 23:18:16 +000090static struct mxs_dma_desc *mxs_nand_get_dma_desc(struct mxs_nand_info *info)
91{
92 struct mxs_dma_desc *desc;
93
94 if (info->desc_index >= MXS_NAND_DMA_DESCRIPTOR_COUNT) {
95 printf("MXS NAND: Too many DMA descriptors requested\n");
96 return NULL;
97 }
98
99 desc = info->desc[info->desc_index];
100 info->desc_index++;
101
102 return desc;
103}
104
105static void mxs_nand_return_dma_descs(struct mxs_nand_info *info)
106{
107 int i;
108 struct mxs_dma_desc *desc;
109
110 for (i = 0; i < info->desc_index; i++) {
111 desc = info->desc[i];
112 memset(desc, 0, sizeof(struct mxs_dma_desc));
113 desc->address = (dma_addr_t)desc;
114 }
115
116 info->desc_index = 0;
117}
118
Marek Vasut913a7252011-11-08 23:18:16 +0000119static uint32_t mxs_nand_aux_status_offset(void)
120{
121 return (MXS_NAND_METADATA_SIZE + 0x3) & ~0x3;
122}
123
Sean Anderson0a749442020-10-04 21:39:45 -0400124static inline bool mxs_nand_bbm_in_data_chunk(struct bch_geometry *geo,
125 struct mtd_info *mtd,
126 unsigned int *chunk_num)
Marek Vasut913a7252011-11-08 23:18:16 +0000127{
Ye Li94547442020-05-04 22:08:50 +0800128 unsigned int i, j;
Marek Vasut913a7252011-11-08 23:18:16 +0000129
Ye Li94547442020-05-04 22:08:50 +0800130 if (geo->ecc_chunk0_size != geo->ecc_chunkn_size) {
Sean Anderson0a749442020-10-04 21:39:45 -0400131 dev_err(mtd->dev, "The size of chunk0 must equal to chunkn\n");
Ye Li94547442020-05-04 22:08:50 +0800132 return false;
133 }
Marek Vasut913a7252011-11-08 23:18:16 +0000134
Ye Li94547442020-05-04 22:08:50 +0800135 i = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8) /
136 (geo->gf_len * geo->ecc_strength +
137 geo->ecc_chunkn_size * 8);
Marek Vasut913a7252011-11-08 23:18:16 +0000138
Ye Li94547442020-05-04 22:08:50 +0800139 j = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8) -
140 (geo->gf_len * geo->ecc_strength +
141 geo->ecc_chunkn_size * 8) * i;
Marek Vasut913a7252011-11-08 23:18:16 +0000142
Ye Li94547442020-05-04 22:08:50 +0800143 if (j < geo->ecc_chunkn_size * 8) {
144 *chunk_num = i + 1;
Sean Anderson0a749442020-10-04 21:39:45 -0400145 dev_dbg(mtd->dev, "Set ecc to %d and bbm in chunk %d\n",
Ye Li94547442020-05-04 22:08:50 +0800146 geo->ecc_strength, *chunk_num);
147 return true;
148 }
Marek Vasut913a7252011-11-08 23:18:16 +0000149
Ye Li94547442020-05-04 22:08:50 +0800150 return false;
Marek Vasut913a7252011-11-08 23:18:16 +0000151}
152
Stefan Agner4d42ac12018-06-22 17:19:51 +0200153static inline int mxs_nand_calc_ecc_layout_by_info(struct bch_geometry *geo,
Stefan Agneread66eb2018-06-22 18:06:18 +0200154 struct mtd_info *mtd,
155 unsigned int ecc_strength,
156 unsigned int ecc_step)
Stefan Agner4d42ac12018-06-22 17:19:51 +0200157{
158 struct nand_chip *chip = mtd_to_nand(mtd);
Stefan Agner4dc98db2018-06-22 18:06:15 +0200159 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Ye Li94547442020-05-04 22:08:50 +0800160 unsigned int block_mark_bit_offset;
Stefan Agner4d42ac12018-06-22 17:19:51 +0200161
Stefan Agneread66eb2018-06-22 18:06:18 +0200162 switch (ecc_step) {
Stefan Agner4d42ac12018-06-22 17:19:51 +0200163 case SZ_512:
164 geo->gf_len = 13;
165 break;
166 case SZ_1K:
167 geo->gf_len = 14;
168 break;
169 default:
170 return -EINVAL;
171 }
172
Ye Li94547442020-05-04 22:08:50 +0800173 geo->ecc_chunk0_size = ecc_step;
174 geo->ecc_chunkn_size = ecc_step;
Stefan Agneread66eb2018-06-22 18:06:18 +0200175 geo->ecc_strength = round_up(ecc_strength, 2);
Stefan Agner4d42ac12018-06-22 17:19:51 +0200176
177 /* Keep the C >= O */
Ye Li94547442020-05-04 22:08:50 +0800178 if (geo->ecc_chunkn_size < mtd->oobsize)
Stefan Agner4d42ac12018-06-22 17:19:51 +0200179 return -EINVAL;
180
Stefan Agner4dc98db2018-06-22 18:06:15 +0200181 if (geo->ecc_strength > nand_info->max_ecc_strength_supported)
Stefan Agner4d42ac12018-06-22 17:19:51 +0200182 return -EINVAL;
183
Ye Li94547442020-05-04 22:08:50 +0800184 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size;
185
186 /* For bit swap. */
187 block_mark_bit_offset = mtd->writesize * 8 -
188 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
189 + MXS_NAND_METADATA_SIZE * 8);
190
191 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
192 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
Stefan Agner4d42ac12018-06-22 17:19:51 +0200193
194 return 0;
195}
196
Ye Li94547442020-05-04 22:08:50 +0800197static inline int mxs_nand_legacy_calc_ecc_layout(struct bch_geometry *geo,
Stefan Agnerd0778b32018-06-22 17:19:49 +0200198 struct mtd_info *mtd)
Marek Vasut913a7252011-11-08 23:18:16 +0000199{
Stefan Agner4dc98db2018-06-22 18:06:15 +0200200 struct nand_chip *chip = mtd_to_nand(mtd);
201 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Ye Li94547442020-05-04 22:08:50 +0800202 unsigned int block_mark_bit_offset;
Han Xu2ee499e2022-03-25 08:36:38 -0500203 int corr, ds_corr;
Stefan Agner4dc98db2018-06-22 18:06:15 +0200204
Stefan Agnerd0778b32018-06-22 17:19:49 +0200205 /* The default for the length of Galois Field. */
206 geo->gf_len = 13;
207
208 /* The default for chunk size. */
Ye Li94547442020-05-04 22:08:50 +0800209 geo->ecc_chunk0_size = 512;
210 geo->ecc_chunkn_size = 512;
Stefan Agnerd0778b32018-06-22 17:19:49 +0200211
Ye Li94547442020-05-04 22:08:50 +0800212 if (geo->ecc_chunkn_size < mtd->oobsize) {
Stefan Agnerd0778b32018-06-22 17:19:49 +0200213 geo->gf_len = 14;
Ye Li94547442020-05-04 22:08:50 +0800214 geo->ecc_chunk0_size *= 2;
215 geo->ecc_chunkn_size *= 2;
Stefan Agnerd0778b32018-06-22 17:19:49 +0200216 }
217
Ye Li94547442020-05-04 22:08:50 +0800218 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size;
Stefan Agnerd0778b32018-06-22 17:19:49 +0200219
Stefan Agnerd0778b32018-06-22 17:19:49 +0200220 /*
221 * Determine the ECC layout with the formula:
222 * ECC bits per chunk = (total page spare data bits) /
223 * (bits per ECC level) / (chunks per page)
224 * where:
225 * total page spare data bits =
226 * (page oob size - meta data size) * (bits per byte)
227 */
228 geo->ecc_strength = ((mtd->oobsize - MXS_NAND_METADATA_SIZE) * 8)
229 / (geo->gf_len * geo->ecc_chunk_count);
230
Stefan Agner4d42ac12018-06-22 17:19:51 +0200231 geo->ecc_strength = min(round_down(geo->ecc_strength, 2),
Stefan Agner4dc98db2018-06-22 18:06:15 +0200232 nand_info->max_ecc_strength_supported);
Stefan Agnerd0778b32018-06-22 17:19:49 +0200233
Han Xu2ee499e2022-03-25 08:36:38 -0500234 /* check ecc strength, same as nand_ecc_is_strong_enough() did*/
235 if (chip->ecc_step_ds) {
236 corr = mtd->writesize * geo->ecc_strength /
237 geo->ecc_chunkn_size;
238 ds_corr = mtd->writesize * chip->ecc_strength_ds /
239 chip->ecc_step_ds;
240 if (corr < ds_corr ||
241 geo->ecc_strength < chip->ecc_strength_ds)
242 return -EINVAL;
243 }
244
Ye Li94547442020-05-04 22:08:50 +0800245 block_mark_bit_offset = mtd->writesize * 8 -
246 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
247 + MXS_NAND_METADATA_SIZE * 8);
248
249 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
250 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
251
252 return 0;
253}
254
255static inline int mxs_nand_calc_ecc_for_large_oob(struct bch_geometry *geo,
256 struct mtd_info *mtd)
257{
258 struct nand_chip *chip = mtd_to_nand(mtd);
259 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
260 unsigned int block_mark_bit_offset;
261 unsigned int max_ecc;
262 unsigned int bbm_chunk;
263 unsigned int i;
264
265 /* sanity check for the minimum ecc nand required */
266 if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
267 return -EINVAL;
268 geo->ecc_strength = chip->ecc_strength_ds;
269
270 /* calculate the maximum ecc platform can support*/
271 geo->gf_len = 14;
272 geo->ecc_chunk0_size = 1024;
273 geo->ecc_chunkn_size = 1024;
274 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size;
275 max_ecc = ((mtd->oobsize - MXS_NAND_METADATA_SIZE) * 8)
276 / (geo->gf_len * geo->ecc_chunk_count);
277 max_ecc = min(round_down(max_ecc, 2),
278 nand_info->max_ecc_strength_supported);
279
280
281 /* search a supported ecc strength that makes bbm */
282 /* located in data chunk */
283 geo->ecc_strength = chip->ecc_strength_ds;
284 while (!(geo->ecc_strength > max_ecc)) {
285 if (mxs_nand_bbm_in_data_chunk(geo, mtd, &bbm_chunk))
286 break;
287 geo->ecc_strength += 2;
288 }
289
290 /* if none of them works, keep using the minimum ecc */
291 /* nand required but changing ecc page layout */
292 if (geo->ecc_strength > max_ecc) {
293 geo->ecc_strength = chip->ecc_strength_ds;
294 /* add extra ecc for meta data */
295 geo->ecc_chunk0_size = 0;
296 geo->ecc_chunk_count = (mtd->writesize / geo->ecc_chunkn_size) + 1;
297 geo->ecc_for_meta = 1;
298 /* check if oob can afford this extra ecc chunk */
299 if (mtd->oobsize * 8 < MXS_NAND_METADATA_SIZE * 8 +
300 geo->gf_len * geo->ecc_strength
301 * geo->ecc_chunk_count) {
302 printf("unsupported NAND chip with new layout\n");
303 return -EINVAL;
304 }
305
306 /* calculate in which chunk bbm located */
307 bbm_chunk = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8 -
308 geo->gf_len * geo->ecc_strength) /
309 (geo->gf_len * geo->ecc_strength +
310 geo->ecc_chunkn_size * 8) + 1;
311 }
312
313 /* calculate the number of ecc chunk behind the bbm */
314 i = (mtd->writesize / geo->ecc_chunkn_size) - bbm_chunk + 1;
315
316 block_mark_bit_offset = mtd->writesize * 8 -
317 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - i)
318 + MXS_NAND_METADATA_SIZE * 8);
319
320 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
321 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
322
Stefan Agnerd0778b32018-06-22 17:19:49 +0200323 return 0;
Marek Vasut913a7252011-11-08 23:18:16 +0000324}
325
326/*
327 * Wait for BCH complete IRQ and clear the IRQ
328 */
Stefan Agnerdc8af6d2018-06-22 18:06:12 +0200329static int mxs_nand_wait_for_bch_complete(struct mxs_nand_info *nand_info)
Marek Vasut913a7252011-11-08 23:18:16 +0000330{
Marek Vasut913a7252011-11-08 23:18:16 +0000331 int timeout = MXS_NAND_BCH_TIMEOUT;
332 int ret;
333
Stefan Agnerdc8af6d2018-06-22 18:06:12 +0200334 ret = mxs_wait_mask_set(&nand_info->bch_regs->hw_bch_ctrl_reg,
Marek Vasut913a7252011-11-08 23:18:16 +0000335 BCH_CTRL_COMPLETE_IRQ, timeout);
336
Stefan Agnerdc8af6d2018-06-22 18:06:12 +0200337 writel(BCH_CTRL_COMPLETE_IRQ, &nand_info->bch_regs->hw_bch_ctrl_clr);
Marek Vasut913a7252011-11-08 23:18:16 +0000338
339 return ret;
340}
341
342/*
343 * This is the function that we install in the cmd_ctrl function pointer of the
344 * owning struct nand_chip. The only functions in the reference implementation
345 * that use these functions pointers are cmdfunc and select_chip.
346 *
347 * In this driver, we implement our own select_chip, so this function will only
348 * be called by the reference implementation's cmdfunc. For this reason, we can
349 * ignore the chip enable bit and concentrate only on sending bytes to the NAND
350 * Flash.
351 */
352static void mxs_nand_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
353{
Scott Wood17fed142016-05-30 13:57:56 -0500354 struct nand_chip *nand = mtd_to_nand(mtd);
355 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut913a7252011-11-08 23:18:16 +0000356 struct mxs_dma_desc *d;
357 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
358 int ret;
359
360 /*
361 * If this condition is true, something is _VERY_ wrong in MTD
362 * subsystem!
363 */
364 if (nand_info->cmd_queue_len == MXS_NAND_COMMAND_BUFFER_SIZE) {
365 printf("MXS NAND: Command queue too long\n");
366 return;
367 }
368
369 /*
370 * Every operation begins with a command byte and a series of zero or
371 * more address bytes. These are distinguished by either the Address
372 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
373 * asserted. When MTD is ready to execute the command, it will
374 * deasert both latch enables.
375 *
376 * Rather than run a separate DMA operation for every single byte, we
377 * queue them up and run a single DMA operation for the entire series
378 * of command and data bytes.
379 */
380 if (ctrl & (NAND_ALE | NAND_CLE)) {
381 if (data != NAND_CMD_NONE)
382 nand_info->cmd_buf[nand_info->cmd_queue_len++] = data;
383 return;
384 }
385
386 /*
387 * If control arrives here, MTD has deasserted both the ALE and CLE,
388 * which means it's ready to run an operation. Check if we have any
389 * bytes to send.
390 */
391 if (nand_info->cmd_queue_len == 0)
392 return;
393
394 /* Compile the DMA descriptor -- a descriptor that sends command. */
395 d = mxs_nand_get_dma_desc(nand_info);
396 d->cmd.data =
397 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
398 MXS_DMA_DESC_CHAIN | MXS_DMA_DESC_DEC_SEM |
399 MXS_DMA_DESC_WAIT4END | (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
400 (nand_info->cmd_queue_len << MXS_DMA_DESC_BYTES_OFFSET);
401
402 d->cmd.address = (dma_addr_t)nand_info->cmd_buf;
403
404 d->cmd.pio_words[0] =
405 GPMI_CTRL0_COMMAND_MODE_WRITE |
406 GPMI_CTRL0_WORD_LENGTH |
407 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
408 GPMI_CTRL0_ADDRESS_NAND_CLE |
409 GPMI_CTRL0_ADDRESS_INCREMENT |
410 nand_info->cmd_queue_len;
411
412 mxs_dma_desc_append(channel, d);
413
Marek Vasut1b120e82012-03-15 18:33:19 +0000414 /* Flush caches */
415 mxs_nand_flush_cmd_buf(nand_info);
416
Marek Vasut913a7252011-11-08 23:18:16 +0000417 /* Execute the DMA chain. */
418 ret = mxs_dma_go(channel);
419 if (ret)
420 printf("MXS NAND: Error sending command\n");
421
422 mxs_nand_return_dma_descs(nand_info);
423
424 /* Reset the command queue. */
425 nand_info->cmd_queue_len = 0;
426}
427
428/*
429 * Test if the NAND flash is ready.
430 */
431static int mxs_nand_device_ready(struct mtd_info *mtd)
432{
Scott Wood17fed142016-05-30 13:57:56 -0500433 struct nand_chip *chip = mtd_to_nand(mtd);
434 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Marek Vasut913a7252011-11-08 23:18:16 +0000435 uint32_t tmp;
436
Stefan Agnerdc8af6d2018-06-22 18:06:12 +0200437 tmp = readl(&nand_info->gpmi_regs->hw_gpmi_stat);
Marek Vasut913a7252011-11-08 23:18:16 +0000438 tmp >>= (GPMI_STAT_READY_BUSY_OFFSET + nand_info->cur_chip);
439
440 return tmp & 1;
441}
442
443/*
444 * Select the NAND chip.
445 */
446static void mxs_nand_select_chip(struct mtd_info *mtd, int chip)
447{
Scott Wood17fed142016-05-30 13:57:56 -0500448 struct nand_chip *nand = mtd_to_nand(mtd);
449 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut913a7252011-11-08 23:18:16 +0000450
451 nand_info->cur_chip = chip;
452}
453
454/*
455 * Handle block mark swapping.
456 *
457 * Note that, when this function is called, it doesn't know whether it's
458 * swapping the block mark, or swapping it *back* -- but it doesn't matter
459 * because the the operation is the same.
460 */
Stefan Agnerd0778b32018-06-22 17:19:49 +0200461static void mxs_nand_swap_block_mark(struct bch_geometry *geo,
462 uint8_t *data_buf, uint8_t *oob_buf)
Marek Vasut913a7252011-11-08 23:18:16 +0000463{
Stefan Agnerd0778b32018-06-22 17:19:49 +0200464 uint32_t bit_offset = geo->block_mark_bit_offset;
465 uint32_t buf_offset = geo->block_mark_byte_offset;
Marek Vasut913a7252011-11-08 23:18:16 +0000466
467 uint32_t src;
468 uint32_t dst;
469
Marek Vasut913a7252011-11-08 23:18:16 +0000470 /*
471 * Get the byte from the data area that overlays the block mark. Since
472 * the ECC engine applies its own view to the bits in the page, the
473 * physical block mark won't (in general) appear on a byte boundary in
474 * the data.
475 */
476 src = data_buf[buf_offset] >> bit_offset;
477 src |= data_buf[buf_offset + 1] << (8 - bit_offset);
478
479 dst = oob_buf[0];
480
481 oob_buf[0] = src;
482
483 data_buf[buf_offset] &= ~(0xff << bit_offset);
484 data_buf[buf_offset + 1] &= 0xff << bit_offset;
485
486 data_buf[buf_offset] |= dst << bit_offset;
487 data_buf[buf_offset + 1] |= dst >> (8 - bit_offset);
488}
489
490/*
491 * Read data from NAND.
492 */
493static void mxs_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int length)
494{
Scott Wood17fed142016-05-30 13:57:56 -0500495 struct nand_chip *nand = mtd_to_nand(mtd);
496 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut913a7252011-11-08 23:18:16 +0000497 struct mxs_dma_desc *d;
498 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
499 int ret;
500
501 if (length > NAND_MAX_PAGESIZE) {
502 printf("MXS NAND: DMA buffer too big\n");
503 return;
504 }
505
506 if (!buf) {
507 printf("MXS NAND: DMA buffer is NULL\n");
508 return;
509 }
510
511 /* Compile the DMA descriptor - a descriptor that reads data. */
512 d = mxs_nand_get_dma_desc(nand_info);
513 d->cmd.data =
514 MXS_DMA_DESC_COMMAND_DMA_WRITE | MXS_DMA_DESC_IRQ |
515 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
516 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
517 (length << MXS_DMA_DESC_BYTES_OFFSET);
518
519 d->cmd.address = (dma_addr_t)nand_info->data_buf;
520
521 d->cmd.pio_words[0] =
522 GPMI_CTRL0_COMMAND_MODE_READ |
523 GPMI_CTRL0_WORD_LENGTH |
524 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
525 GPMI_CTRL0_ADDRESS_NAND_DATA |
526 length;
527
528 mxs_dma_desc_append(channel, d);
529
530 /*
531 * A DMA descriptor that waits for the command to end and the chip to
532 * become ready.
533 *
534 * I think we actually should *not* be waiting for the chip to become
535 * ready because, after all, we don't care. I think the original code
536 * did that and no one has re-thought it yet.
537 */
538 d = mxs_nand_get_dma_desc(nand_info);
539 d->cmd.data =
540 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
541 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_DEC_SEM |
Luca Ellero80f06b82014-12-16 15:36:14 +0100542 MXS_DMA_DESC_WAIT4END | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
Marek Vasut913a7252011-11-08 23:18:16 +0000543
544 d->cmd.address = 0;
545
546 d->cmd.pio_words[0] =
547 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
548 GPMI_CTRL0_WORD_LENGTH |
549 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
550 GPMI_CTRL0_ADDRESS_NAND_DATA;
551
552 mxs_dma_desc_append(channel, d);
553
Peng Fane3bbfb72015-07-21 16:15:21 +0800554 /* Invalidate caches */
555 mxs_nand_inval_data_buf(nand_info);
556
Marek Vasut913a7252011-11-08 23:18:16 +0000557 /* Execute the DMA chain. */
558 ret = mxs_dma_go(channel);
559 if (ret) {
560 printf("MXS NAND: DMA read error\n");
561 goto rtn;
562 }
563
Marek Vasut1b120e82012-03-15 18:33:19 +0000564 /* Invalidate caches */
565 mxs_nand_inval_data_buf(nand_info);
566
Marek Vasut913a7252011-11-08 23:18:16 +0000567 memcpy(buf, nand_info->data_buf, length);
568
569rtn:
570 mxs_nand_return_dma_descs(nand_info);
571}
572
573/*
574 * Write data to NAND.
575 */
576static void mxs_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
577 int length)
578{
Scott Wood17fed142016-05-30 13:57:56 -0500579 struct nand_chip *nand = mtd_to_nand(mtd);
580 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut913a7252011-11-08 23:18:16 +0000581 struct mxs_dma_desc *d;
582 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
583 int ret;
584
585 if (length > NAND_MAX_PAGESIZE) {
586 printf("MXS NAND: DMA buffer too big\n");
587 return;
588 }
589
590 if (!buf) {
591 printf("MXS NAND: DMA buffer is NULL\n");
592 return;
593 }
594
595 memcpy(nand_info->data_buf, buf, length);
596
597 /* Compile the DMA descriptor - a descriptor that writes data. */
598 d = mxs_nand_get_dma_desc(nand_info);
599 d->cmd.data =
600 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
601 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
Luca Ellero966f1cd2014-12-16 15:36:15 +0100602 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
Marek Vasut913a7252011-11-08 23:18:16 +0000603 (length << MXS_DMA_DESC_BYTES_OFFSET);
604
605 d->cmd.address = (dma_addr_t)nand_info->data_buf;
606
607 d->cmd.pio_words[0] =
608 GPMI_CTRL0_COMMAND_MODE_WRITE |
609 GPMI_CTRL0_WORD_LENGTH |
610 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
611 GPMI_CTRL0_ADDRESS_NAND_DATA |
612 length;
613
614 mxs_dma_desc_append(channel, d);
615
Marek Vasut1b120e82012-03-15 18:33:19 +0000616 /* Flush caches */
617 mxs_nand_flush_data_buf(nand_info);
618
Marek Vasut913a7252011-11-08 23:18:16 +0000619 /* Execute the DMA chain. */
620 ret = mxs_dma_go(channel);
621 if (ret)
622 printf("MXS NAND: DMA write error\n");
623
624 mxs_nand_return_dma_descs(nand_info);
625}
626
627/*
628 * Read a single byte from NAND.
629 */
630static uint8_t mxs_nand_read_byte(struct mtd_info *mtd)
631{
632 uint8_t buf;
633 mxs_nand_read_buf(mtd, &buf, 1);
634 return buf;
635}
636
Peng Fandf23c9d2020-05-04 22:08:52 +0800637static bool mxs_nand_erased_page(struct mtd_info *mtd, struct nand_chip *nand,
638 u8 *buf, int chunk, int page)
639{
640 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
641 struct bch_geometry *geo = &nand_info->bch_geometry;
642 unsigned int flip_bits = 0, flip_bits_noecc = 0;
643 unsigned int threshold;
644 unsigned int base = geo->ecc_chunkn_size * chunk;
645 u32 *dma_buf = (u32 *)buf;
646 int i;
647
648 threshold = geo->gf_len / 2;
649 if (threshold > geo->ecc_strength)
650 threshold = geo->ecc_strength;
651
652 for (i = 0; i < geo->ecc_chunkn_size; i++) {
653 flip_bits += hweight8(~buf[base + i]);
654 if (flip_bits > threshold)
655 return false;
656 }
657
658 nand->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
659 nand->read_buf(mtd, buf, mtd->writesize);
660
661 for (i = 0; i < mtd->writesize / 4; i++) {
662 flip_bits_noecc += hweight32(~dma_buf[i]);
663 if (flip_bits_noecc > threshold)
664 return false;
665 }
666
667 mtd->ecc_stats.corrected += flip_bits;
668
669 memset(buf, 0xff, mtd->writesize);
670
671 printf("The page(%d) is an erased page(%d,%d,%d,%d).\n", page, chunk, threshold, flip_bits, flip_bits_noecc);
672
673 return true;
674}
675
Marek Vasut913a7252011-11-08 23:18:16 +0000676/*
677 * Read a page from NAND.
678 */
679static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000680 uint8_t *buf, int oob_required,
681 int page)
Marek Vasut913a7252011-11-08 23:18:16 +0000682{
Scott Wood17fed142016-05-30 13:57:56 -0500683 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Stefan Agnerd0778b32018-06-22 17:19:49 +0200684 struct bch_geometry *geo = &nand_info->bch_geometry;
Peng Fan9e813732020-05-04 22:08:53 +0800685 struct mxs_bch_regs *bch_regs = nand_info->bch_regs;
Marek Vasut913a7252011-11-08 23:18:16 +0000686 struct mxs_dma_desc *d;
687 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
688 uint32_t corrected = 0, failed = 0;
689 uint8_t *status;
690 int i, ret;
Peng Fan9e813732020-05-04 22:08:53 +0800691 int flag = 0;
Marek Vasut913a7252011-11-08 23:18:16 +0000692
693 /* Compile the DMA descriptor - wait for ready. */
694 d = mxs_nand_get_dma_desc(nand_info);
695 d->cmd.data =
696 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
697 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
698 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
699
700 d->cmd.address = 0;
701
702 d->cmd.pio_words[0] =
703 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
704 GPMI_CTRL0_WORD_LENGTH |
705 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
706 GPMI_CTRL0_ADDRESS_NAND_DATA;
707
708 mxs_dma_desc_append(channel, d);
709
710 /* Compile the DMA descriptor - enable the BCH block and read. */
711 d = mxs_nand_get_dma_desc(nand_info);
712 d->cmd.data =
713 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
714 MXS_DMA_DESC_WAIT4END | (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
715
716 d->cmd.address = 0;
717
718 d->cmd.pio_words[0] =
719 GPMI_CTRL0_COMMAND_MODE_READ |
720 GPMI_CTRL0_WORD_LENGTH |
721 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
722 GPMI_CTRL0_ADDRESS_NAND_DATA |
723 (mtd->writesize + mtd->oobsize);
724 d->cmd.pio_words[1] = 0;
725 d->cmd.pio_words[2] =
726 GPMI_ECCCTRL_ENABLE_ECC |
727 GPMI_ECCCTRL_ECC_CMD_DECODE |
728 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
729 d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
730 d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
731 d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
732
Han Xuafed2a12020-05-06 20:59:19 +0800733 if (nand_info->en_randomizer) {
Alice Guo3f277782020-05-04 22:09:03 +0800734 d->cmd.pio_words[2] |= GPMI_ECCCTRL_RANDOMIZER_ENABLE |
735 GPMI_ECCCTRL_RANDOMIZER_TYPE2;
736 d->cmd.pio_words[3] |= (page % 256) << 16;
737 }
738
Marek Vasut913a7252011-11-08 23:18:16 +0000739 mxs_dma_desc_append(channel, d);
740
741 /* Compile the DMA descriptor - disable the BCH block. */
742 d = mxs_nand_get_dma_desc(nand_info);
743 d->cmd.data =
744 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
745 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
746 (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
747
748 d->cmd.address = 0;
749
750 d->cmd.pio_words[0] =
751 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
752 GPMI_CTRL0_WORD_LENGTH |
753 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
754 GPMI_CTRL0_ADDRESS_NAND_DATA |
755 (mtd->writesize + mtd->oobsize);
756 d->cmd.pio_words[1] = 0;
757 d->cmd.pio_words[2] = 0;
758
759 mxs_dma_desc_append(channel, d);
760
761 /* Compile the DMA descriptor - deassert the NAND lock and interrupt. */
762 d = mxs_nand_get_dma_desc(nand_info);
763 d->cmd.data =
764 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
765 MXS_DMA_DESC_DEC_SEM;
766
767 d->cmd.address = 0;
768
769 mxs_dma_desc_append(channel, d);
770
Peng Fane3bbfb72015-07-21 16:15:21 +0800771 /* Invalidate caches */
772 mxs_nand_inval_data_buf(nand_info);
773
Marek Vasut913a7252011-11-08 23:18:16 +0000774 /* Execute the DMA chain. */
775 ret = mxs_dma_go(channel);
776 if (ret) {
777 printf("MXS NAND: DMA read error\n");
778 goto rtn;
779 }
780
Stefan Agnerdc8af6d2018-06-22 18:06:12 +0200781 ret = mxs_nand_wait_for_bch_complete(nand_info);
Marek Vasut913a7252011-11-08 23:18:16 +0000782 if (ret) {
783 printf("MXS NAND: BCH read timeout\n");
784 goto rtn;
785 }
786
Peng Fandf23c9d2020-05-04 22:08:52 +0800787 mxs_nand_return_dma_descs(nand_info);
788
Marek Vasut1b120e82012-03-15 18:33:19 +0000789 /* Invalidate caches */
790 mxs_nand_inval_data_buf(nand_info);
791
Marek Vasut913a7252011-11-08 23:18:16 +0000792 /* Read DMA completed, now do the mark swapping. */
Stefan Agnerd0778b32018-06-22 17:19:49 +0200793 mxs_nand_swap_block_mark(geo, nand_info->data_buf, nand_info->oob_buf);
Marek Vasut913a7252011-11-08 23:18:16 +0000794
795 /* Loop over status bytes, accumulating ECC status. */
796 status = nand_info->oob_buf + mxs_nand_aux_status_offset();
Stefan Agnerd0778b32018-06-22 17:19:49 +0200797 for (i = 0; i < geo->ecc_chunk_count; i++) {
Marek Vasut913a7252011-11-08 23:18:16 +0000798 if (status[i] == 0x00)
799 continue;
800
Peng Fan9e813732020-05-04 22:08:53 +0800801 if (status[i] == 0xff) {
Han Xue4f2b002020-05-04 22:09:02 +0800802 if (!nand_info->en_randomizer &&
803 (is_mx6dqp() || is_mx7() || is_mx6ul() ||
804 is_imx8() || is_imx8m()))
Peng Fan9e813732020-05-04 22:08:53 +0800805 if (readl(&bch_regs->hw_bch_debug1))
806 flag = 1;
Marek Vasut913a7252011-11-08 23:18:16 +0000807 continue;
Peng Fan9e813732020-05-04 22:08:53 +0800808 }
Marek Vasut913a7252011-11-08 23:18:16 +0000809
810 if (status[i] == 0xfe) {
Peng Fandf23c9d2020-05-04 22:08:52 +0800811 if (mxs_nand_erased_page(mtd, nand,
812 nand_info->data_buf, i, page))
813 break;
Marek Vasut913a7252011-11-08 23:18:16 +0000814 failed++;
815 continue;
816 }
817
818 corrected += status[i];
819 }
820
821 /* Propagate ECC status to the owning MTD. */
822 mtd->ecc_stats.failed += failed;
823 mtd->ecc_stats.corrected += corrected;
824
825 /*
826 * It's time to deliver the OOB bytes. See mxs_nand_ecc_read_oob() for
827 * details about our policy for delivering the OOB.
828 *
829 * We fill the caller's buffer with set bits, and then copy the block
830 * mark to the caller's buffer. Note that, if block mark swapping was
831 * necessary, it has already been done, so we can rely on the first
832 * byte of the auxiliary buffer to contain the block mark.
833 */
834 memset(nand->oob_poi, 0xff, mtd->oobsize);
835
836 nand->oob_poi[0] = nand_info->oob_buf[0];
837
838 memcpy(buf, nand_info->data_buf, mtd->writesize);
839
Peng Fan9e813732020-05-04 22:08:53 +0800840 if (flag)
841 memset(buf, 0xff, mtd->writesize);
Marek Vasut913a7252011-11-08 23:18:16 +0000842rtn:
843 mxs_nand_return_dma_descs(nand_info);
844
845 return ret;
846}
847
848/*
849 * Write a page to NAND.
850 */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000851static int mxs_nand_ecc_write_page(struct mtd_info *mtd,
852 struct nand_chip *nand, const uint8_t *buf,
Scott Wood46e13102016-05-30 13:57:57 -0500853 int oob_required, int page)
Marek Vasut913a7252011-11-08 23:18:16 +0000854{
Scott Wood17fed142016-05-30 13:57:56 -0500855 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Stefan Agnerd0778b32018-06-22 17:19:49 +0200856 struct bch_geometry *geo = &nand_info->bch_geometry;
Marek Vasut913a7252011-11-08 23:18:16 +0000857 struct mxs_dma_desc *d;
858 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
859 int ret;
860
861 memcpy(nand_info->data_buf, buf, mtd->writesize);
862 memcpy(nand_info->oob_buf, nand->oob_poi, mtd->oobsize);
863
864 /* Handle block mark swapping. */
Stefan Agnerd0778b32018-06-22 17:19:49 +0200865 mxs_nand_swap_block_mark(geo, nand_info->data_buf, nand_info->oob_buf);
Marek Vasut913a7252011-11-08 23:18:16 +0000866
867 /* Compile the DMA descriptor - write data. */
868 d = mxs_nand_get_dma_desc(nand_info);
869 d->cmd.data =
870 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
871 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
872 (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
873
874 d->cmd.address = 0;
875
876 d->cmd.pio_words[0] =
877 GPMI_CTRL0_COMMAND_MODE_WRITE |
878 GPMI_CTRL0_WORD_LENGTH |
879 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
880 GPMI_CTRL0_ADDRESS_NAND_DATA;
881 d->cmd.pio_words[1] = 0;
882 d->cmd.pio_words[2] =
883 GPMI_ECCCTRL_ENABLE_ECC |
884 GPMI_ECCCTRL_ECC_CMD_ENCODE |
885 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
886 d->cmd.pio_words[3] = (mtd->writesize + mtd->oobsize);
887 d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
888 d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
889
Han Xuafed2a12020-05-06 20:59:19 +0800890 if (nand_info->en_randomizer) {
Igor Opaniukc55401372019-11-03 16:49:43 +0100891 d->cmd.pio_words[2] |= GPMI_ECCCTRL_RANDOMIZER_ENABLE |
892 GPMI_ECCCTRL_RANDOMIZER_TYPE2;
893 /*
894 * Write NAND page number needed to be randomized
895 * to GPMI_ECCCOUNT register.
896 *
897 * The value is between 0-255. For additional details
898 * check 9.6.6.4 of i.MX7D Applications Processor reference
899 */
Alice Guo3f277782020-05-04 22:09:03 +0800900 d->cmd.pio_words[3] |= (page % 256) << 16;
Igor Opaniukc55401372019-11-03 16:49:43 +0100901 }
902
Marek Vasut913a7252011-11-08 23:18:16 +0000903 mxs_dma_desc_append(channel, d);
904
Marek Vasut1b120e82012-03-15 18:33:19 +0000905 /* Flush caches */
906 mxs_nand_flush_data_buf(nand_info);
907
Marek Vasut913a7252011-11-08 23:18:16 +0000908 /* Execute the DMA chain. */
909 ret = mxs_dma_go(channel);
910 if (ret) {
911 printf("MXS NAND: DMA write error\n");
912 goto rtn;
913 }
914
Stefan Agnerdc8af6d2018-06-22 18:06:12 +0200915 ret = mxs_nand_wait_for_bch_complete(nand_info);
Marek Vasut913a7252011-11-08 23:18:16 +0000916 if (ret) {
917 printf("MXS NAND: BCH write timeout\n");
918 goto rtn;
919 }
920
921rtn:
922 mxs_nand_return_dma_descs(nand_info);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000923 return 0;
Marek Vasut913a7252011-11-08 23:18:16 +0000924}
925
926/*
927 * Read OOB from NAND.
928 *
929 * This function is a veneer that replaces the function originally installed by
930 * the NAND Flash MTD code.
931 */
932static int mxs_nand_hook_read_oob(struct mtd_info *mtd, loff_t from,
933 struct mtd_oob_ops *ops)
934{
Scott Wood17fed142016-05-30 13:57:56 -0500935 struct nand_chip *chip = mtd_to_nand(mtd);
936 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Marek Vasut913a7252011-11-08 23:18:16 +0000937 int ret;
938
Sergey Lapin3a38a552013-01-14 03:46:50 +0000939 if (ops->mode == MTD_OPS_RAW)
Marek Vasut913a7252011-11-08 23:18:16 +0000940 nand_info->raw_oob_mode = 1;
941 else
942 nand_info->raw_oob_mode = 0;
943
944 ret = nand_info->hooked_read_oob(mtd, from, ops);
945
946 nand_info->raw_oob_mode = 0;
947
948 return ret;
949}
950
951/*
952 * Write OOB to NAND.
953 *
954 * This function is a veneer that replaces the function originally installed by
955 * the NAND Flash MTD code.
956 */
957static int mxs_nand_hook_write_oob(struct mtd_info *mtd, loff_t to,
958 struct mtd_oob_ops *ops)
959{
Scott Wood17fed142016-05-30 13:57:56 -0500960 struct nand_chip *chip = mtd_to_nand(mtd);
961 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Marek Vasut913a7252011-11-08 23:18:16 +0000962 int ret;
963
Sergey Lapin3a38a552013-01-14 03:46:50 +0000964 if (ops->mode == MTD_OPS_RAW)
Marek Vasut913a7252011-11-08 23:18:16 +0000965 nand_info->raw_oob_mode = 1;
966 else
967 nand_info->raw_oob_mode = 0;
968
969 ret = nand_info->hooked_write_oob(mtd, to, ops);
970
971 nand_info->raw_oob_mode = 0;
972
973 return ret;
974}
975
976/*
977 * Mark a block bad in NAND.
978 *
979 * This function is a veneer that replaces the function originally installed by
980 * the NAND Flash MTD code.
981 */
982static int mxs_nand_hook_block_markbad(struct mtd_info *mtd, loff_t ofs)
983{
Scott Wood17fed142016-05-30 13:57:56 -0500984 struct nand_chip *chip = mtd_to_nand(mtd);
985 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Marek Vasut913a7252011-11-08 23:18:16 +0000986 int ret;
987
988 nand_info->marking_block_bad = 1;
989
990 ret = nand_info->hooked_block_markbad(mtd, ofs);
991
992 nand_info->marking_block_bad = 0;
993
994 return ret;
995}
996
997/*
998 * There are several places in this driver where we have to handle the OOB and
999 * block marks. This is the function where things are the most complicated, so
1000 * this is where we try to explain it all. All the other places refer back to
1001 * here.
1002 *
1003 * These are the rules, in order of decreasing importance:
1004 *
1005 * 1) Nothing the caller does can be allowed to imperil the block mark, so all
1006 * write operations take measures to protect it.
1007 *
1008 * 2) In read operations, the first byte of the OOB we return must reflect the
1009 * true state of the block mark, no matter where that block mark appears in
1010 * the physical page.
1011 *
1012 * 3) ECC-based read operations return an OOB full of set bits (since we never
1013 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1014 * return).
1015 *
1016 * 4) "Raw" read operations return a direct view of the physical bytes in the
1017 * page, using the conventional definition of which bytes are data and which
1018 * are OOB. This gives the caller a way to see the actual, physical bytes
1019 * in the page, without the distortions applied by our ECC engine.
1020 *
1021 * What we do for this specific read operation depends on whether we're doing
1022 * "raw" read, or an ECC-based read.
1023 *
1024 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1025 * easy. When reading a page, for example, the NAND Flash MTD code calls our
1026 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1027 * ECC-based or raw view of the page is implicit in which function it calls
1028 * (there is a similar pair of ECC-based/raw functions for writing).
1029 *
1030 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
1031 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
1032 * caller wants an ECC-based or raw view of the page is not propagated down to
1033 * this driver.
1034 *
1035 * Since our OOB *is* covered by ECC, we need this information. So, we hook the
1036 * ecc.read_oob and ecc.write_oob function pointers in the owning
1037 * struct mtd_info with our own functions. These hook functions set the
1038 * raw_oob_mode field so that, when control finally arrives here, we'll know
1039 * what to do.
1040 */
1041static int mxs_nand_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001042 int page)
Marek Vasut913a7252011-11-08 23:18:16 +00001043{
Scott Wood17fed142016-05-30 13:57:56 -05001044 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut913a7252011-11-08 23:18:16 +00001045
1046 /*
1047 * First, fill in the OOB buffer. If we're doing a raw read, we need to
1048 * get the bytes from the physical page. If we're not doing a raw read,
1049 * we need to fill the buffer with set bits.
1050 */
1051 if (nand_info->raw_oob_mode) {
1052 /*
1053 * If control arrives here, we're doing a "raw" read. Send the
1054 * command to read the conventional OOB and read it.
1055 */
1056 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1057 nand->read_buf(mtd, nand->oob_poi, mtd->oobsize);
1058 } else {
1059 /*
1060 * If control arrives here, we're not doing a "raw" read. Fill
1061 * the OOB buffer with set bits and correct the block mark.
1062 */
1063 memset(nand->oob_poi, 0xff, mtd->oobsize);
1064
1065 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1066 mxs_nand_read_buf(mtd, nand->oob_poi, 1);
1067 }
1068
1069 return 0;
1070
1071}
1072
1073/*
1074 * Write OOB data to NAND.
1075 */
1076static int mxs_nand_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
1077 int page)
1078{
Scott Wood17fed142016-05-30 13:57:56 -05001079 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut913a7252011-11-08 23:18:16 +00001080 uint8_t block_mark = 0;
1081
1082 /*
1083 * There are fundamental incompatibilities between the i.MX GPMI NFC and
1084 * the NAND Flash MTD model that make it essentially impossible to write
1085 * the out-of-band bytes.
1086 *
1087 * We permit *ONE* exception. If the *intent* of writing the OOB is to
1088 * mark a block bad, we can do that.
1089 */
1090
1091 if (!nand_info->marking_block_bad) {
1092 printf("NXS NAND: Writing OOB isn't supported\n");
1093 return -EIO;
1094 }
1095
1096 /* Write the block mark. */
1097 nand->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1098 nand->write_buf(mtd, &block_mark, 1);
1099 nand->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1100
1101 /* Check if it worked. */
1102 if (nand->waitfunc(mtd, nand) & NAND_STATUS_FAIL)
1103 return -EIO;
1104
1105 return 0;
1106}
1107
1108/*
1109 * Claims all blocks are good.
1110 *
1111 * In principle, this function is *only* called when the NAND Flash MTD system
1112 * isn't allowed to keep an in-memory bad block table, so it is forced to ask
1113 * the driver for bad block information.
1114 *
1115 * In fact, we permit the NAND Flash MTD system to have an in-memory BBT, so
1116 * this function is *only* called when we take it away.
1117 *
1118 * Thus, this function is only called when we want *all* blocks to look good,
1119 * so it *always* return success.
1120 */
Scott Wood52ab7ce2016-05-30 13:57:58 -05001121static int mxs_nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Marek Vasut913a7252011-11-08 23:18:16 +00001122{
Stefan Agneread66eb2018-06-22 18:06:18 +02001123 return 0;
1124}
1125
1126static int mxs_nand_set_geometry(struct mtd_info *mtd, struct bch_geometry *geo)
1127{
1128 struct nand_chip *chip = mtd_to_nand(mtd);
1129 struct nand_chip *nand = mtd_to_nand(mtd);
1130 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Han Xu2ee499e2022-03-25 08:36:38 -05001131 int err;
Stefan Agneread66eb2018-06-22 18:06:18 +02001132
Ye Li94547442020-05-04 22:08:50 +08001133 if (chip->ecc_strength_ds > nand_info->max_ecc_strength_supported) {
1134 printf("unsupported NAND chip, minimum ecc required %d\n"
1135 , chip->ecc_strength_ds);
1136 return -EINVAL;
1137 }
Stefan Agneread66eb2018-06-22 18:06:18 +02001138
Han Xu2ee499e2022-03-25 08:36:38 -05001139 /* use the legacy bch setting by default */
1140 if ((!nand_info->use_minimum_ecc && mtd->oobsize < 1024) ||
1141 !(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0)) {
1142 dev_dbg(mtd->dev, "use legacy bch geometry\n");
1143 err = mxs_nand_legacy_calc_ecc_layout(geo, mtd);
1144 if (!err)
1145 return 0;
Ye Li94547442020-05-04 22:08:50 +08001146 }
Stefan Agneread66eb2018-06-22 18:06:18 +02001147
Han Xu2ee499e2022-03-25 08:36:38 -05001148 /* for large oob nand */
1149 if (mtd->oobsize > 1024) {
1150 dev_dbg(mtd->dev, "use large oob bch geometry\n");
1151 err = mxs_nand_calc_ecc_for_large_oob(geo, mtd);
1152 if (!err)
1153 return 0;
1154 }
Ye Li94547442020-05-04 22:08:50 +08001155
Han Xu2ee499e2022-03-25 08:36:38 -05001156 /* otherwise use the minimum ecc nand chips required */
1157 dev_dbg(mtd->dev, "use minimum ecc bch geometry\n");
1158 err = mxs_nand_calc_ecc_layout_by_info(geo, mtd, chip->ecc_strength_ds,
1159 chip->ecc_step_ds);
Stefan Agneread66eb2018-06-22 18:06:18 +02001160
Han Xu2ee499e2022-03-25 08:36:38 -05001161 if (err)
1162 dev_err(mtd->dev, "none of the bch geometry setting works\n");
1163
1164 return err;
1165}
1166
1167void mxs_nand_dump_geo(struct mtd_info *mtd)
1168{
1169 struct nand_chip *nand = mtd_to_nand(mtd);
1170 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1171 struct bch_geometry *geo = &nand_info->bch_geometry;
1172
1173 dev_dbg(mtd->dev, "BCH Geometry :\n"
1174 "GF Length\t\t: %u\n"
1175 "ECC Strength\t\t: %u\n"
1176 "ECC for Meta\t\t: %u\n"
1177 "ECC Chunk0 Size\t\t: %u\n"
1178 "ECC Chunkn Size\t\t: %u\n"
1179 "ECC Chunk Count\t\t: %u\n"
1180 "Block Mark Byte Offset\t: %u\n"
1181 "Block Mark Bit Offset\t: %u\n",
1182 geo->gf_len,
1183 geo->ecc_strength,
1184 geo->ecc_for_meta,
1185 geo->ecc_chunk0_size,
1186 geo->ecc_chunkn_size,
1187 geo->ecc_chunk_count,
1188 geo->block_mark_byte_offset,
1189 geo->block_mark_bit_offset);
Marek Vasut913a7252011-11-08 23:18:16 +00001190}
1191
1192/*
Marek Vasut913a7252011-11-08 23:18:16 +00001193 * At this point, the physical NAND Flash chips have been identified and
1194 * counted, so we know the physical geometry. This enables us to make some
1195 * important configuration decisions.
1196 *
Robert P. J. Day8d56db92016-07-15 13:44:45 -04001197 * The return value of this function propagates directly back to this driver's
Stefan Agner5883e552018-06-22 17:19:47 +02001198 * board_nand_init(). Anything other than zero will cause this driver to
Marek Vasut913a7252011-11-08 23:18:16 +00001199 * tear everything down and declare failure.
1200 */
Stefan Agner5883e552018-06-22 17:19:47 +02001201int mxs_nand_setup_ecc(struct mtd_info *mtd)
Marek Vasut913a7252011-11-08 23:18:16 +00001202{
Scott Wood17fed142016-05-30 13:57:56 -05001203 struct nand_chip *nand = mtd_to_nand(mtd);
1204 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Stefan Agnerd0778b32018-06-22 17:19:49 +02001205 struct bch_geometry *geo = &nand_info->bch_geometry;
Stefan Agnerdc8af6d2018-06-22 18:06:12 +02001206 struct mxs_bch_regs *bch_regs = nand_info->bch_regs;
Marek Vasut913a7252011-11-08 23:18:16 +00001207 uint32_t tmp;
Stefan Agneread66eb2018-06-22 18:06:18 +02001208 int ret;
Stefan Agner4d42ac12018-06-22 17:19:51 +02001209
Igor Opaniukc55401372019-11-03 16:49:43 +01001210 nand_info->en_randomizer = 0;
1211 nand_info->oobsize = mtd->oobsize;
1212 nand_info->writesize = mtd->writesize;
1213
Stefan Agneread66eb2018-06-22 18:06:18 +02001214 ret = mxs_nand_set_geometry(mtd, geo);
Stefan Agner4d42ac12018-06-22 17:19:51 +02001215 if (ret)
1216 return ret;
1217
Han Xu2ee499e2022-03-25 08:36:38 -05001218 mxs_nand_dump_geo(mtd);
1219
Marek Vasut913a7252011-11-08 23:18:16 +00001220 /* Configure BCH and set NFC geometry */
Otavio Salvadorcbf0bf22012-08-13 09:53:12 +00001221 mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
Marek Vasut913a7252011-11-08 23:18:16 +00001222
1223 /* Configure layout 0 */
Stefan Agnerd0778b32018-06-22 17:19:49 +02001224 tmp = (geo->ecc_chunk_count - 1) << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
Marek Vasut913a7252011-11-08 23:18:16 +00001225 tmp |= MXS_NAND_METADATA_SIZE << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
Stefan Agnerd0778b32018-06-22 17:19:49 +02001226 tmp |= (geo->ecc_strength >> 1) << BCH_FLASHLAYOUT0_ECC0_OFFSET;
Ye Li94547442020-05-04 22:08:50 +08001227 tmp |= geo->ecc_chunk0_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
Stefan Agnerd0778b32018-06-22 17:19:49 +02001228 tmp |= (geo->gf_len == 14 ? 1 : 0) <<
Peng Fanc94f09d2015-07-21 16:15:19 +08001229 BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET;
Marek Vasut913a7252011-11-08 23:18:16 +00001230 writel(tmp, &bch_regs->hw_bch_flash0layout0);
Igor Opaniukc55401372019-11-03 16:49:43 +01001231 nand_info->bch_flash0layout0 = tmp;
Marek Vasut913a7252011-11-08 23:18:16 +00001232
1233 tmp = (mtd->writesize + mtd->oobsize)
1234 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
Stefan Agnerd0778b32018-06-22 17:19:49 +02001235 tmp |= (geo->ecc_strength >> 1) << BCH_FLASHLAYOUT1_ECCN_OFFSET;
Ye Li94547442020-05-04 22:08:50 +08001236 tmp |= geo->ecc_chunkn_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
Stefan Agnerd0778b32018-06-22 17:19:49 +02001237 tmp |= (geo->gf_len == 14 ? 1 : 0) <<
Peng Fanc94f09d2015-07-21 16:15:19 +08001238 BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
Marek Vasut913a7252011-11-08 23:18:16 +00001239 writel(tmp, &bch_regs->hw_bch_flash0layout1);
Igor Opaniukc55401372019-11-03 16:49:43 +01001240 nand_info->bch_flash0layout1 = tmp;
Marek Vasut913a7252011-11-08 23:18:16 +00001241
Peng Fan9e813732020-05-04 22:08:53 +08001242 /* Set erase threshold to ecc strength for mx6ul, mx6qp and mx7 */
1243 if (is_mx6dqp() || is_mx7() ||
Peng Fan128abf42020-05-04 22:09:00 +08001244 is_mx6ul() || is_imx8() || is_imx8m())
Peng Fan9e813732020-05-04 22:08:53 +08001245 writel(BCH_MODE_ERASE_THRESHOLD(geo->ecc_strength),
1246 &bch_regs->hw_bch_mode);
1247
Marek Vasut913a7252011-11-08 23:18:16 +00001248 /* Set *all* chip selects to use layout 0 */
1249 writel(0, &bch_regs->hw_bch_layoutselect);
1250
1251 /* Enable BCH complete interrupt */
1252 writel(BCH_CTRL_COMPLETE_IRQ_EN, &bch_regs->hw_bch_ctrl_set);
1253
Stefan Agner5883e552018-06-22 17:19:47 +02001254 return 0;
Marek Vasut913a7252011-11-08 23:18:16 +00001255}
1256
1257/*
1258 * Allocate DMA buffers
1259 */
1260int mxs_nand_alloc_buffers(struct mxs_nand_info *nand_info)
1261{
1262 uint8_t *buf;
1263 const int size = NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE;
1264
Marek Vasut1b120e82012-03-15 18:33:19 +00001265 nand_info->data_buf_size = roundup(size, MXS_DMA_ALIGNMENT);
1266
Marek Vasut913a7252011-11-08 23:18:16 +00001267 /* DMA buffers */
Marek Vasut1b120e82012-03-15 18:33:19 +00001268 buf = memalign(MXS_DMA_ALIGNMENT, nand_info->data_buf_size);
Marek Vasut913a7252011-11-08 23:18:16 +00001269 if (!buf) {
1270 printf("MXS NAND: Error allocating DMA buffers\n");
1271 return -ENOMEM;
1272 }
1273
Marek Vasut1b120e82012-03-15 18:33:19 +00001274 memset(buf, 0, nand_info->data_buf_size);
Marek Vasut913a7252011-11-08 23:18:16 +00001275
1276 nand_info->data_buf = buf;
1277 nand_info->oob_buf = buf + NAND_MAX_PAGESIZE;
Marek Vasut913a7252011-11-08 23:18:16 +00001278 /* Command buffers */
1279 nand_info->cmd_buf = memalign(MXS_DMA_ALIGNMENT,
1280 MXS_NAND_COMMAND_BUFFER_SIZE);
1281 if (!nand_info->cmd_buf) {
1282 free(buf);
1283 printf("MXS NAND: Error allocating command buffers\n");
1284 return -ENOMEM;
1285 }
1286 memset(nand_info->cmd_buf, 0, MXS_NAND_COMMAND_BUFFER_SIZE);
1287 nand_info->cmd_queue_len = 0;
1288
1289 return 0;
1290}
1291
1292/*
1293 * Initializes the NFC hardware.
1294 */
Adam Ford6edb91a2019-01-12 06:25:48 -06001295static int mxs_nand_init_dma(struct mxs_nand_info *info)
Marek Vasut913a7252011-11-08 23:18:16 +00001296{
Peng Fane37d5a92016-01-27 10:38:02 +08001297 int i = 0, j, ret = 0;
Marek Vasut913a7252011-11-08 23:18:16 +00001298
1299 info->desc = malloc(sizeof(struct mxs_dma_desc *) *
1300 MXS_NAND_DMA_DESCRIPTOR_COUNT);
Peng Fane37d5a92016-01-27 10:38:02 +08001301 if (!info->desc) {
1302 ret = -ENOMEM;
Marek Vasut913a7252011-11-08 23:18:16 +00001303 goto err1;
Peng Fane37d5a92016-01-27 10:38:02 +08001304 }
Marek Vasut913a7252011-11-08 23:18:16 +00001305
1306 /* Allocate the DMA descriptors. */
1307 for (i = 0; i < MXS_NAND_DMA_DESCRIPTOR_COUNT; i++) {
1308 info->desc[i] = mxs_dma_desc_alloc();
Peng Fane37d5a92016-01-27 10:38:02 +08001309 if (!info->desc[i]) {
1310 ret = -ENOMEM;
Marek Vasut913a7252011-11-08 23:18:16 +00001311 goto err2;
Peng Fane37d5a92016-01-27 10:38:02 +08001312 }
Marek Vasut913a7252011-11-08 23:18:16 +00001313 }
1314
1315 /* Init the DMA controller. */
Fabio Estevam17156222017-06-29 09:33:44 -03001316 mxs_dma_init();
Marek Vasut93541b42012-04-08 17:34:46 +00001317 for (j = MXS_DMA_CHANNEL_AHB_APBH_GPMI0;
1318 j <= MXS_DMA_CHANNEL_AHB_APBH_GPMI7; j++) {
Peng Fane37d5a92016-01-27 10:38:02 +08001319 ret = mxs_dma_init_channel(j);
1320 if (ret)
Marek Vasut93541b42012-04-08 17:34:46 +00001321 goto err3;
1322 }
Marek Vasut913a7252011-11-08 23:18:16 +00001323
1324 /* Reset the GPMI block. */
Stefan Agnerdc8af6d2018-06-22 18:06:12 +02001325 mxs_reset_block(&info->gpmi_regs->hw_gpmi_ctrl0_reg);
1326 mxs_reset_block(&info->bch_regs->hw_bch_ctrl_reg);
Marek Vasut913a7252011-11-08 23:18:16 +00001327
1328 /*
1329 * Choose NAND mode, set IRQ polarity, disable write protection and
1330 * select BCH ECC.
1331 */
Stefan Agnerdc8af6d2018-06-22 18:06:12 +02001332 clrsetbits_le32(&info->gpmi_regs->hw_gpmi_ctrl1,
Marek Vasut913a7252011-11-08 23:18:16 +00001333 GPMI_CTRL1_GPMI_MODE,
1334 GPMI_CTRL1_ATA_IRQRDY_POLARITY | GPMI_CTRL1_DEV_RESET |
1335 GPMI_CTRL1_BCH_MODE);
1336
1337 return 0;
1338
Marek Vasut93541b42012-04-08 17:34:46 +00001339err3:
Peng Fane37d5a92016-01-27 10:38:02 +08001340 for (--j; j >= MXS_DMA_CHANNEL_AHB_APBH_GPMI0; j--)
Marek Vasut93541b42012-04-08 17:34:46 +00001341 mxs_dma_release(j);
Marek Vasut913a7252011-11-08 23:18:16 +00001342err2:
Marek Vasut913a7252011-11-08 23:18:16 +00001343 for (--i; i >= 0; i--)
1344 mxs_dma_desc_free(info->desc[i]);
Peng Fane37d5a92016-01-27 10:38:02 +08001345 free(info->desc);
1346err1:
1347 if (ret == -ENOMEM)
1348 printf("MXS NAND: Unable to allocate DMA descriptors\n");
1349 return ret;
Marek Vasut913a7252011-11-08 23:18:16 +00001350}
1351
Michael Trimarchifd6e13e2022-08-30 16:48:47 +02001352/*
1353 * <1> Firstly, we should know what's the GPMI-clock means.
1354 * The GPMI-clock is the internal clock in the gpmi nand controller.
1355 * If you set 100MHz to gpmi nand controller, the GPMI-clock's period
1356 * is 10ns. Mark the GPMI-clock's period as GPMI-clock-period.
1357 *
1358 * <2> Secondly, we should know what's the frequency on the nand chip pins.
1359 * The frequency on the nand chip pins is derived from the GPMI-clock.
1360 * We can get it from the following equation:
1361 *
1362 * F = G / (DS + DH)
1363 *
1364 * F : the frequency on the nand chip pins.
1365 * G : the GPMI clock, such as 100MHz.
1366 * DS : GPMI_HW_GPMI_TIMING0:DATA_SETUP
1367 * DH : GPMI_HW_GPMI_TIMING0:DATA_HOLD
1368 *
1369 * <3> Thirdly, when the frequency on the nand chip pins is above 33MHz,
1370 * the nand EDO(extended Data Out) timing could be applied.
1371 * The GPMI implements a feedback read strobe to sample the read data.
1372 * The feedback read strobe can be delayed to support the nand EDO timing
1373 * where the read strobe may deasserts before the read data is valid, and
1374 * read data is valid for some time after read strobe.
1375 *
1376 * The following figure illustrates some aspects of a NAND Flash read:
1377 *
1378 * |<---tREA---->|
1379 * | |
1380 * | | |
1381 * |<--tRP-->| |
1382 * | | |
1383 * __ ___|__________________________________
1384 * RDN \________/ |
1385 * |
1386 * /---------\
1387 * Read Data --------------< >---------
1388 * \---------/
1389 * | |
1390 * |<-D->|
1391 * FeedbackRDN ________ ____________
1392 * \___________/
1393 *
1394 * D stands for delay, set in the HW_GPMI_CTRL1:RDN_DELAY.
1395 *
1396 *
1397 * <4> Now, we begin to describe how to compute the right RDN_DELAY.
1398 *
1399 * 4.1) From the aspect of the nand chip pins:
1400 * Delay = (tREA + C - tRP) {1}
1401 *
1402 * tREA : the maximum read access time.
1403 * C : a constant to adjust the delay. default is 4000ps.
1404 * tRP : the read pulse width, which is exactly:
1405 * tRP = (GPMI-clock-period) * DATA_SETUP
1406 *
1407 * 4.2) From the aspect of the GPMI nand controller:
1408 * Delay = RDN_DELAY * 0.125 * RP {2}
1409 *
1410 * RP : the DLL reference period.
1411 * if (GPMI-clock-period > DLL_THRETHOLD)
1412 * RP = GPMI-clock-period / 2;
1413 * else
1414 * RP = GPMI-clock-period;
1415 *
1416 * Set the HW_GPMI_CTRL1:HALF_PERIOD if GPMI-clock-period
1417 * is greater DLL_THRETHOLD. In other SOCs, the DLL_THRETHOLD
1418 * is 16000ps, but in mx6q, we use 12000ps.
1419 *
1420 * 4.3) since {1} equals {2}, we get:
1421 *
1422 * (tREA + 4000 - tRP) * 8
1423 * RDN_DELAY = ----------------------- {3}
1424 * RP
1425 */
1426static void mxs_compute_timings(struct nand_chip *chip,
1427 const struct nand_sdr_timings *sdr)
1428{
1429 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
1430 unsigned long clk_rate;
1431 unsigned int dll_wait_time_us;
1432 unsigned int dll_threshold_ps = nand_info->max_chain_delay;
1433 unsigned int period_ps, reference_period_ps;
1434 unsigned int data_setup_cycles, data_hold_cycles, addr_setup_cycles;
1435 unsigned int tRP_ps;
1436 bool use_half_period;
1437 int sample_delay_ps, sample_delay_factor;
1438 u16 busy_timeout_cycles;
1439 u8 wrn_dly_sel;
1440 u32 timing0;
1441 u32 timing1;
1442 u32 ctrl1n;
1443
1444 if (sdr->tRC_min >= 30000) {
1445 /* ONFI non-EDO modes [0-3] */
1446 clk_rate = 22000000;
1447 wrn_dly_sel = GPMI_CTRL1_WRN_DLY_SEL_4_TO_8NS;
1448 } else if (sdr->tRC_min >= 25000) {
1449 /* ONFI EDO mode 4 */
1450 clk_rate = 80000000;
1451 wrn_dly_sel = GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
1452 debug("%s, setting ONFI onfi edo 4\n", __func__);
1453 } else {
1454 /* ONFI EDO mode 5 */
1455 clk_rate = 100000000;
1456 wrn_dly_sel = GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
1457 debug("%s, setting ONFI onfi edo 5\n", __func__);
1458 }
1459
1460 /* SDR core timings are given in picoseconds */
1461 period_ps = div_u64((u64)NSEC_PER_SEC * 1000, clk_rate);
1462
1463 addr_setup_cycles = TO_CYCLES(sdr->tALS_min, period_ps);
1464 data_setup_cycles = TO_CYCLES(sdr->tDS_min, period_ps);
1465 data_hold_cycles = TO_CYCLES(sdr->tDH_min, period_ps);
1466 busy_timeout_cycles = TO_CYCLES(sdr->tWB_max + sdr->tR_max, period_ps);
1467
1468 timing0 = (addr_setup_cycles << GPMI_TIMING0_ADDRESS_SETUP_OFFSET) |
1469 (data_hold_cycles << GPMI_TIMING0_DATA_HOLD_OFFSET) |
1470 (data_setup_cycles << GPMI_TIMING0_DATA_SETUP_OFFSET);
1471 timing1 = (busy_timeout_cycles * 4096) << GPMI_TIMING1_DEVICE_BUSY_TIMEOUT_OFFSET;
1472
1473 /*
1474 * Derive NFC ideal delay from {3}:
1475 *
1476 * (tREA + 4000 - tRP) * 8
1477 * RDN_DELAY = -----------------------
1478 * RP
1479 */
1480 if (period_ps > dll_threshold_ps) {
1481 use_half_period = true;
1482 reference_period_ps = period_ps / 2;
1483 } else {
1484 use_half_period = false;
1485 reference_period_ps = period_ps;
1486 }
1487
1488 tRP_ps = data_setup_cycles * period_ps;
1489 sample_delay_ps = (sdr->tREA_max + 4000 - tRP_ps) * 8;
1490 if (sample_delay_ps > 0)
1491 sample_delay_factor = sample_delay_ps / reference_period_ps;
1492 else
1493 sample_delay_factor = 0;
1494
1495 ctrl1n = (wrn_dly_sel << GPMI_CTRL1_WRN_DLY_SEL_OFFSET);
1496 if (sample_delay_factor)
1497 ctrl1n |= (sample_delay_factor << GPMI_CTRL1_RDN_DELAY_OFFSET) |
1498 GPMI_CTRL1_DLL_ENABLE |
1499 (use_half_period ? GPMI_CTRL1_HALF_PERIOD : 0);
1500
1501 writel(timing0, &nand_info->gpmi_regs->hw_gpmi_timing0);
1502 writel(timing1, &nand_info->gpmi_regs->hw_gpmi_timing1);
1503
1504 /*
1505 * Clear several CTRL1 fields, DLL must be disabled when setting
1506 * RDN_DELAY or HALF_PERIOD.
1507 */
1508 writel(GPMI_CTRL1_CLEAR_MASK, &nand_info->gpmi_regs->hw_gpmi_ctrl1_clr);
1509 writel(ctrl1n, &nand_info->gpmi_regs->hw_gpmi_ctrl1_set);
1510
1511 clk_set_rate(nand_info->gpmi_clk, clk_rate);
1512
1513 /* Wait 64 clock cycles before using the GPMI after enabling the DLL */
1514 dll_wait_time_us = USEC_PER_SEC / clk_rate * 64;
1515 if (!dll_wait_time_us)
1516 dll_wait_time_us = 1;
1517
1518 /* Wait for the DLL to settle. */
1519 udelay(dll_wait_time_us);
1520}
1521
1522static int mxs_nand_setup_interface(struct mtd_info *mtd, int chipnr,
1523 const struct nand_data_interface *conf)
1524{
1525 struct nand_chip *chip = mtd_to_nand(mtd);
1526 const struct nand_sdr_timings *sdr;
1527
1528 sdr = nand_get_sdr_timings(conf);
1529 if (IS_ERR(sdr))
1530 return PTR_ERR(sdr);
1531
1532 /* Stop here if this call was just a check */
1533 if (chipnr < 0)
1534 return 0;
1535
1536 /* Do the actual derivation of the controller timings */
1537 mxs_compute_timings(chip, sdr);
1538
1539 return 0;
1540}
1541
Stefan Agner7152f342018-06-22 17:19:46 +02001542int mxs_nand_init_spl(struct nand_chip *nand)
1543{
1544 struct mxs_nand_info *nand_info;
1545 int err;
1546
1547 nand_info = malloc(sizeof(struct mxs_nand_info));
1548 if (!nand_info) {
1549 printf("MXS NAND: Failed to allocate private data\n");
1550 return -ENOMEM;
1551 }
1552 memset(nand_info, 0, sizeof(struct mxs_nand_info));
1553
Stefan Agnerdc8af6d2018-06-22 18:06:12 +02001554 nand_info->gpmi_regs = (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
1555 nand_info->bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
Adam Ford10210732019-01-02 20:36:52 -06001556
Peng Fan128abf42020-05-04 22:09:00 +08001557 if (is_mx6sx() || is_mx7() || is_imx8() || is_imx8m())
Adam Ford10210732019-01-02 20:36:52 -06001558 nand_info->max_ecc_strength_supported = 62;
1559 else
1560 nand_info->max_ecc_strength_supported = 40;
1561
Ye Li61771d22022-03-31 13:27:47 +08001562 if (IS_ENABLED(CONFIG_NAND_MXS_USE_MINIMUM_ECC))
1563 nand_info->use_minimum_ecc = true;
1564
Stefan Agner7152f342018-06-22 17:19:46 +02001565 err = mxs_nand_alloc_buffers(nand_info);
1566 if (err)
1567 return err;
1568
Stefan Agner00e65162018-06-22 18:06:13 +02001569 err = mxs_nand_init_dma(nand_info);
Stefan Agner7152f342018-06-22 17:19:46 +02001570 if (err)
1571 return err;
1572
1573 nand_set_controller_data(nand, nand_info);
1574
1575 nand->options |= NAND_NO_SUBPAGE_WRITE;
1576
1577 nand->cmd_ctrl = mxs_nand_cmd_ctrl;
1578 nand->dev_ready = mxs_nand_device_ready;
1579 nand->select_chip = mxs_nand_select_chip;
Stefan Agner7152f342018-06-22 17:19:46 +02001580
1581 nand->read_byte = mxs_nand_read_byte;
1582 nand->read_buf = mxs_nand_read_buf;
1583
1584 nand->ecc.read_page = mxs_nand_ecc_read_page;
1585
1586 nand->ecc.mode = NAND_ECC_HW;
Stefan Agner7152f342018-06-22 17:19:46 +02001587
1588 return 0;
1589}
1590
Stefan Agner19f90512018-06-22 18:06:16 +02001591int mxs_nand_init_ctrl(struct mxs_nand_info *nand_info)
Marek Vasut913a7252011-11-08 23:18:16 +00001592{
Stefan Agner5883e552018-06-22 17:19:47 +02001593 struct mtd_info *mtd;
Stefan Agner5883e552018-06-22 17:19:47 +02001594 struct nand_chip *nand;
Marek Vasut913a7252011-11-08 23:18:16 +00001595 int err;
1596
Stefan Agner5883e552018-06-22 17:19:47 +02001597 nand = &nand_info->chip;
1598 mtd = nand_to_mtd(nand);
Marek Vasut913a7252011-11-08 23:18:16 +00001599 err = mxs_nand_alloc_buffers(nand_info);
1600 if (err)
Stefan Agner404b1102018-06-22 18:06:14 +02001601 return err;
Marek Vasut913a7252011-11-08 23:18:16 +00001602
Stefan Agner00e65162018-06-22 18:06:13 +02001603 err = mxs_nand_init_dma(nand_info);
Marek Vasut913a7252011-11-08 23:18:16 +00001604 if (err)
Stefan Agner404b1102018-06-22 18:06:14 +02001605 goto err_free_buffers;
Marek Vasut913a7252011-11-08 23:18:16 +00001606
1607 memset(&fake_ecc_layout, 0, sizeof(fake_ecc_layout));
1608
Stefan Agner95f376f2018-06-22 17:19:48 +02001609#ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1610 nand->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
1611#endif
1612
Scott Wood17fed142016-05-30 13:57:56 -05001613 nand_set_controller_data(nand, nand_info);
Marek Vasut913a7252011-11-08 23:18:16 +00001614 nand->options |= NAND_NO_SUBPAGE_WRITE;
1615
Stefan Agner150ddbc2018-06-22 18:06:17 +02001616 if (nand_info->dev)
Patrice Chotard33d2cf92021-09-13 16:25:53 +02001617 nand->flash_node = dev_ofnode(nand_info->dev);
Stefan Agner150ddbc2018-06-22 18:06:17 +02001618
Marek Vasut913a7252011-11-08 23:18:16 +00001619 nand->cmd_ctrl = mxs_nand_cmd_ctrl;
1620
1621 nand->dev_ready = mxs_nand_device_ready;
1622 nand->select_chip = mxs_nand_select_chip;
1623 nand->block_bad = mxs_nand_block_bad;
Marek Vasut913a7252011-11-08 23:18:16 +00001624
1625 nand->read_byte = mxs_nand_read_byte;
1626
1627 nand->read_buf = mxs_nand_read_buf;
1628 nand->write_buf = mxs_nand_write_buf;
1629
Michael Trimarchifd6e13e2022-08-30 16:48:47 +02001630 if (nand_info->gpmi_clk)
1631 nand->setup_data_interface = mxs_nand_setup_interface;
1632
Stefan Agner5883e552018-06-22 17:19:47 +02001633 /* first scan to find the device and get the page size */
1634 if (nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_DEVICE, NULL))
Stefan Agner404b1102018-06-22 18:06:14 +02001635 goto err_free_buffers;
Stefan Agner5883e552018-06-22 17:19:47 +02001636
1637 if (mxs_nand_setup_ecc(mtd))
Stefan Agner404b1102018-06-22 18:06:14 +02001638 goto err_free_buffers;
Stefan Agner5883e552018-06-22 17:19:47 +02001639
Marek Vasut913a7252011-11-08 23:18:16 +00001640 nand->ecc.read_page = mxs_nand_ecc_read_page;
1641 nand->ecc.write_page = mxs_nand_ecc_write_page;
1642 nand->ecc.read_oob = mxs_nand_ecc_read_oob;
1643 nand->ecc.write_oob = mxs_nand_ecc_write_oob;
1644
1645 nand->ecc.layout = &fake_ecc_layout;
1646 nand->ecc.mode = NAND_ECC_HW;
Ye Li94547442020-05-04 22:08:50 +08001647 nand->ecc.size = nand_info->bch_geometry.ecc_chunkn_size;
Stefan Agner72d627d2018-06-22 17:19:50 +02001648 nand->ecc.strength = nand_info->bch_geometry.ecc_strength;
Marek Vasut913a7252011-11-08 23:18:16 +00001649
Stefan Agner5883e552018-06-22 17:19:47 +02001650 /* second phase scan */
1651 err = nand_scan_tail(mtd);
1652 if (err)
Stefan Agner404b1102018-06-22 18:06:14 +02001653 goto err_free_buffers;
Stefan Agner5883e552018-06-22 17:19:47 +02001654
Michael Trimarchidc3da882022-05-15 11:35:30 +02001655 /* Hook some operations at the MTD level. */
1656 if (mtd->_read_oob != mxs_nand_hook_read_oob) {
1657 nand_info->hooked_read_oob = mtd->_read_oob;
1658 mtd->_read_oob = mxs_nand_hook_read_oob;
1659 }
1660
1661 if (mtd->_write_oob != mxs_nand_hook_write_oob) {
1662 nand_info->hooked_write_oob = mtd->_write_oob;
1663 mtd->_write_oob = mxs_nand_hook_write_oob;
1664 }
1665
1666 if (mtd->_block_markbad != mxs_nand_hook_block_markbad) {
1667 nand_info->hooked_block_markbad = mtd->_block_markbad;
1668 mtd->_block_markbad = mxs_nand_hook_block_markbad;
1669 }
1670
Stefan Agner5883e552018-06-22 17:19:47 +02001671 err = nand_register(0, mtd);
1672 if (err)
Stefan Agner404b1102018-06-22 18:06:14 +02001673 goto err_free_buffers;
Stefan Agner5883e552018-06-22 17:19:47 +02001674
Stefan Agner404b1102018-06-22 18:06:14 +02001675 return 0;
Marek Vasut913a7252011-11-08 23:18:16 +00001676
Stefan Agner404b1102018-06-22 18:06:14 +02001677err_free_buffers:
Marek Vasut913a7252011-11-08 23:18:16 +00001678 free(nand_info->data_buf);
1679 free(nand_info->cmd_buf);
Stefan Agner404b1102018-06-22 18:06:14 +02001680
1681 return err;
1682}
1683
Stefan Agner150ddbc2018-06-22 18:06:17 +02001684#ifndef CONFIG_NAND_MXS_DT
Stefan Agner404b1102018-06-22 18:06:14 +02001685void board_nand_init(void)
1686{
1687 struct mxs_nand_info *nand_info;
1688
1689 nand_info = malloc(sizeof(struct mxs_nand_info));
1690 if (!nand_info) {
1691 printf("MXS NAND: Failed to allocate private data\n");
1692 return;
1693 }
1694 memset(nand_info, 0, sizeof(struct mxs_nand_info));
1695
1696 nand_info->gpmi_regs = (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
1697 nand_info->bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1698
Stefan Agner4dc98db2018-06-22 18:06:15 +02001699 /* Refer to Chapter 17 for i.MX6DQ, Chapter 18 for i.MX6SX */
1700 if (is_mx6sx() || is_mx7())
1701 nand_info->max_ecc_strength_supported = 62;
1702 else
1703 nand_info->max_ecc_strength_supported = 40;
1704
1705#ifdef CONFIG_NAND_MXS_USE_MINIMUM_ECC
1706 nand_info->use_minimum_ecc = true;
1707#endif
1708
Stefan Agner19f90512018-06-22 18:06:16 +02001709 if (mxs_nand_init_ctrl(nand_info) < 0)
Stefan Agner404b1102018-06-22 18:06:14 +02001710 goto err;
1711
Stefan Agner5883e552018-06-22 17:19:47 +02001712 return;
Stefan Agner404b1102018-06-22 18:06:14 +02001713
1714err:
1715 free(nand_info);
Marek Vasut913a7252011-11-08 23:18:16 +00001716}
Stefan Agner150ddbc2018-06-22 18:06:17 +02001717#endif
Igor Opaniukc55401372019-11-03 16:49:43 +01001718
1719/*
1720 * Read NAND layout for FCB block generation.
1721 */
1722void mxs_nand_get_layout(struct mtd_info *mtd, struct mxs_nand_layout *l)
1723{
1724 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1725 u32 tmp;
1726
1727 tmp = readl(&bch_regs->hw_bch_flash0layout0);
1728 l->nblocks = (tmp & BCH_FLASHLAYOUT0_NBLOCKS_MASK) >>
1729 BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1730 l->meta_size = (tmp & BCH_FLASHLAYOUT0_META_SIZE_MASK) >>
1731 BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1732
1733 tmp = readl(&bch_regs->hw_bch_flash0layout1);
1734 l->data0_size = 4 * ((tmp & BCH_FLASHLAYOUT0_DATA0_SIZE_MASK) >>
1735 BCH_FLASHLAYOUT0_DATA0_SIZE_OFFSET);
1736 l->ecc0 = (tmp & BCH_FLASHLAYOUT0_ECC0_MASK) >>
1737 BCH_FLASHLAYOUT0_ECC0_OFFSET;
1738 l->datan_size = 4 * ((tmp & BCH_FLASHLAYOUT1_DATAN_SIZE_MASK) >>
1739 BCH_FLASHLAYOUT1_DATAN_SIZE_OFFSET);
1740 l->eccn = (tmp & BCH_FLASHLAYOUT1_ECCN_MASK) >>
1741 BCH_FLASHLAYOUT1_ECCN_OFFSET;
Han Xu33543b52020-05-04 22:08:58 +08001742 l->gf_len = (tmp & BCH_FLASHLAYOUT1_GF13_0_GF14_1_MASK) >>
1743 BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
Igor Opaniukc55401372019-11-03 16:49:43 +01001744}
1745
1746/*
1747 * Set BCH to specific layout used by ROM bootloader to read FCB.
1748 */
Han Xuafed2a12020-05-06 20:59:19 +08001749void mxs_nand_mode_fcb_62bit(struct mtd_info *mtd)
Igor Opaniukc55401372019-11-03 16:49:43 +01001750{
1751 u32 tmp;
1752 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1753 struct nand_chip *nand = mtd_to_nand(mtd);
1754 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1755
1756 nand_info->en_randomizer = 1;
1757
1758 mtd->writesize = 1024;
1759 mtd->oobsize = 1862 - 1024;
1760
1761 /* 8 ecc_chunks_*/
1762 tmp = 7 << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1763 /* 32 bytes for metadata */
1764 tmp |= 32 << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1765 /* using ECC62 level to be performed */
1766 tmp |= 0x1F << BCH_FLASHLAYOUT0_ECC0_OFFSET;
1767 /* 0x20 * 4 bytes of the data0 block */
1768 tmp |= 0x20 << BCH_FLASHLAYOUT0_DATA0_SIZE_OFFSET;
1769 tmp |= 0 << BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET;
1770 writel(tmp, &bch_regs->hw_bch_flash0layout0);
1771
1772 /* 1024 for data + 838 for OOB */
1773 tmp = 1862 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
1774 /* using ECC62 level to be performed */
1775 tmp |= 0x1F << BCH_FLASHLAYOUT1_ECCN_OFFSET;
1776 /* 0x20 * 4 bytes of the data0 block */
1777 tmp |= 0x20 << BCH_FLASHLAYOUT1_DATAN_SIZE_OFFSET;
1778 tmp |= 0 << BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
1779 writel(tmp, &bch_regs->hw_bch_flash0layout1);
1780}
1781
1782/*
Han Xuafed2a12020-05-06 20:59:19 +08001783 * Set BCH to specific layout used by ROM bootloader to read FCB.
1784 */
1785void mxs_nand_mode_fcb_40bit(struct mtd_info *mtd)
1786{
1787 u32 tmp;
1788 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1789 struct nand_chip *nand = mtd_to_nand(mtd);
1790 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1791
1792 /* no randomizer in this setting*/
1793 nand_info->en_randomizer = 0;
1794
1795 mtd->writesize = 1024;
1796 mtd->oobsize = 1576 - 1024;
1797
1798 /* 8 ecc_chunks_*/
1799 tmp = 7 << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1800 /* 32 bytes for metadata */
1801 tmp |= 32 << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1802 /* using ECC40 level to be performed */
1803 tmp |= 0x14 << BCH_FLASHLAYOUT0_ECC0_OFFSET;
1804 /* 0x20 * 4 bytes of the data0 block */
1805 tmp |= 0x20 << BCH_FLASHLAYOUT0_DATA0_SIZE_OFFSET;
1806 tmp |= 0 << BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET;
1807 writel(tmp, &bch_regs->hw_bch_flash0layout0);
1808
1809 /* 1024 for data + 552 for OOB */
1810 tmp = 1576 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
1811 /* using ECC40 level to be performed */
1812 tmp |= 0x14 << BCH_FLASHLAYOUT1_ECCN_OFFSET;
1813 /* 0x20 * 4 bytes of the data0 block */
1814 tmp |= 0x20 << BCH_FLASHLAYOUT1_DATAN_SIZE_OFFSET;
1815 tmp |= 0 << BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
1816 writel(tmp, &bch_regs->hw_bch_flash0layout1);
1817}
1818
1819/*
Igor Opaniukc55401372019-11-03 16:49:43 +01001820 * Restore BCH to normal settings.
1821 */
1822void mxs_nand_mode_normal(struct mtd_info *mtd)
1823{
1824 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1825 struct nand_chip *nand = mtd_to_nand(mtd);
1826 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1827
1828 nand_info->en_randomizer = 0;
1829
1830 mtd->writesize = nand_info->writesize;
1831 mtd->oobsize = nand_info->oobsize;
1832
1833 writel(nand_info->bch_flash0layout0, &bch_regs->hw_bch_flash0layout0);
1834 writel(nand_info->bch_flash0layout1, &bch_regs->hw_bch_flash0layout1);
1835}
1836
1837uint32_t mxs_nand_mark_byte_offset(struct mtd_info *mtd)
1838{
1839 struct nand_chip *chip = mtd_to_nand(mtd);
1840 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
1841 struct bch_geometry *geo = &nand_info->bch_geometry;
1842
1843 return geo->block_mark_byte_offset;
1844}
1845
1846uint32_t mxs_nand_mark_bit_offset(struct mtd_info *mtd)
1847{
1848 struct nand_chip *chip = mtd_to_nand(mtd);
1849 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
1850 struct bch_geometry *geo = &nand_info->bch_geometry;
1851
1852 return geo->block_mark_bit_offset;
1853}