blob: facedf92c5c87295229f3f655830dfb2ce6e7e9b [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
Tom Warrenc88d30f2012-09-10 08:47:51 -070016#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070017#include <cpu_func.h>
Stefan Agner19f90512018-06-22 18:06:16 +020018#include <dm.h>
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090019#include <linux/mtd/rawnand.h>
Stefan Agner4d42ac12018-06-22 17:19:51 +020020#include <linux/sizes.h>
Marek Vasut913a7252011-11-08 23:18:16 +000021#include <linux/types.h>
Marek Vasut913a7252011-11-08 23:18:16 +000022#include <malloc.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090023#include <linux/errno.h>
Marek Vasut913a7252011-11-08 23:18:16 +000024#include <asm/io.h>
25#include <asm/arch/clock.h>
26#include <asm/arch/imx-regs.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020027#include <asm/mach-imx/regs-bch.h>
28#include <asm/mach-imx/regs-gpmi.h>
Marek Vasut913a7252011-11-08 23:18:16 +000029#include <asm/arch/sys_proto.h>
Shyam Sainif63ef492019-06-14 13:05:33 +053030#include <mxs_nand.h>
Marek Vasut913a7252011-11-08 23:18:16 +000031
32#define MXS_NAND_DMA_DESCRIPTOR_COUNT 4
33
Ye Li74948b82020-05-04 22:08:54 +080034#if (defined(CONFIG_MX6) || defined(CONFIG_MX7) || defined(CONFIG_IMX8M))
Stefan Roese8338d1d2013-04-15 21:14:12 +000035#define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT 2
36#else
37#define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT 0
38#endif
Marek Vasut913a7252011-11-08 23:18:16 +000039#define MXS_NAND_METADATA_SIZE 10
Jörg Krause1d870262015-04-15 09:27:22 +020040#define MXS_NAND_BITS_PER_ECC_LEVEL 13
Stefan Agner54bf8082016-08-01 23:55:18 -070041
42#if !defined(CONFIG_SYS_CACHELINE_SIZE) || CONFIG_SYS_CACHELINE_SIZE < 32
Marek Vasut913a7252011-11-08 23:18:16 +000043#define MXS_NAND_COMMAND_BUFFER_SIZE 32
Stefan Agner54bf8082016-08-01 23:55:18 -070044#else
45#define MXS_NAND_COMMAND_BUFFER_SIZE CONFIG_SYS_CACHELINE_SIZE
46#endif
Marek Vasut913a7252011-11-08 23:18:16 +000047
48#define MXS_NAND_BCH_TIMEOUT 10000
49
Marek Vasut913a7252011-11-08 23:18:16 +000050struct nand_ecclayout fake_ecc_layout;
51
Marek Vasut1b120e82012-03-15 18:33:19 +000052/*
53 * Cache management functions
54 */
Trevor Woerner43ec7e02019-05-03 09:41:00 -040055#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Marek Vasut1b120e82012-03-15 18:33:19 +000056static void mxs_nand_flush_data_buf(struct mxs_nand_info *info)
57{
58 uint32_t addr = (uint32_t)info->data_buf;
59
60 flush_dcache_range(addr, addr + info->data_buf_size);
61}
62
63static void mxs_nand_inval_data_buf(struct mxs_nand_info *info)
64{
65 uint32_t addr = (uint32_t)info->data_buf;
66
67 invalidate_dcache_range(addr, addr + info->data_buf_size);
68}
69
70static void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info)
71{
72 uint32_t addr = (uint32_t)info->cmd_buf;
73
74 flush_dcache_range(addr, addr + MXS_NAND_COMMAND_BUFFER_SIZE);
75}
76#else
77static inline void mxs_nand_flush_data_buf(struct mxs_nand_info *info) {}
78static inline void mxs_nand_inval_data_buf(struct mxs_nand_info *info) {}
79static inline void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info) {}
80#endif
81
Marek Vasut913a7252011-11-08 23:18:16 +000082static struct mxs_dma_desc *mxs_nand_get_dma_desc(struct mxs_nand_info *info)
83{
84 struct mxs_dma_desc *desc;
85
86 if (info->desc_index >= MXS_NAND_DMA_DESCRIPTOR_COUNT) {
87 printf("MXS NAND: Too many DMA descriptors requested\n");
88 return NULL;
89 }
90
91 desc = info->desc[info->desc_index];
92 info->desc_index++;
93
94 return desc;
95}
96
97static void mxs_nand_return_dma_descs(struct mxs_nand_info *info)
98{
99 int i;
100 struct mxs_dma_desc *desc;
101
102 for (i = 0; i < info->desc_index; i++) {
103 desc = info->desc[i];
104 memset(desc, 0, sizeof(struct mxs_dma_desc));
105 desc->address = (dma_addr_t)desc;
106 }
107
108 info->desc_index = 0;
109}
110
Marek Vasut913a7252011-11-08 23:18:16 +0000111static uint32_t mxs_nand_aux_status_offset(void)
112{
113 return (MXS_NAND_METADATA_SIZE + 0x3) & ~0x3;
114}
115
Ye Li94547442020-05-04 22:08:50 +0800116static inline bool mxs_nand_bbm_in_data_chunk(struct bch_geometry *geo, struct mtd_info *mtd,
117 unsigned int *chunk_num)
Marek Vasut913a7252011-11-08 23:18:16 +0000118{
Ye Li94547442020-05-04 22:08:50 +0800119 unsigned int i, j;
Marek Vasut913a7252011-11-08 23:18:16 +0000120
Ye Li94547442020-05-04 22:08:50 +0800121 if (geo->ecc_chunk0_size != geo->ecc_chunkn_size) {
122 dev_err(this->dev, "The size of chunk0 must equal to chunkn\n");
123 return false;
124 }
Marek Vasut913a7252011-11-08 23:18:16 +0000125
Ye Li94547442020-05-04 22:08:50 +0800126 i = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8) /
127 (geo->gf_len * geo->ecc_strength +
128 geo->ecc_chunkn_size * 8);
Marek Vasut913a7252011-11-08 23:18:16 +0000129
Ye Li94547442020-05-04 22:08:50 +0800130 j = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8) -
131 (geo->gf_len * geo->ecc_strength +
132 geo->ecc_chunkn_size * 8) * i;
Marek Vasut913a7252011-11-08 23:18:16 +0000133
Ye Li94547442020-05-04 22:08:50 +0800134 if (j < geo->ecc_chunkn_size * 8) {
135 *chunk_num = i + 1;
136 dev_dbg(this->dev, "Set ecc to %d and bbm in chunk %d\n",
137 geo->ecc_strength, *chunk_num);
138 return true;
139 }
Marek Vasut913a7252011-11-08 23:18:16 +0000140
Ye Li94547442020-05-04 22:08:50 +0800141 return false;
Marek Vasut913a7252011-11-08 23:18:16 +0000142}
143
Stefan Agner4d42ac12018-06-22 17:19:51 +0200144static inline int mxs_nand_calc_ecc_layout_by_info(struct bch_geometry *geo,
Stefan Agneread66eb2018-06-22 18:06:18 +0200145 struct mtd_info *mtd,
146 unsigned int ecc_strength,
147 unsigned int ecc_step)
Stefan Agner4d42ac12018-06-22 17:19:51 +0200148{
149 struct nand_chip *chip = mtd_to_nand(mtd);
Stefan Agner4dc98db2018-06-22 18:06:15 +0200150 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Ye Li94547442020-05-04 22:08:50 +0800151 unsigned int block_mark_bit_offset;
Stefan Agner4d42ac12018-06-22 17:19:51 +0200152
Stefan Agneread66eb2018-06-22 18:06:18 +0200153 switch (ecc_step) {
Stefan Agner4d42ac12018-06-22 17:19:51 +0200154 case SZ_512:
155 geo->gf_len = 13;
156 break;
157 case SZ_1K:
158 geo->gf_len = 14;
159 break;
160 default:
161 return -EINVAL;
162 }
163
Ye Li94547442020-05-04 22:08:50 +0800164 geo->ecc_chunk0_size = ecc_step;
165 geo->ecc_chunkn_size = ecc_step;
Stefan Agneread66eb2018-06-22 18:06:18 +0200166 geo->ecc_strength = round_up(ecc_strength, 2);
Stefan Agner4d42ac12018-06-22 17:19:51 +0200167
168 /* Keep the C >= O */
Ye Li94547442020-05-04 22:08:50 +0800169 if (geo->ecc_chunkn_size < mtd->oobsize)
Stefan Agner4d42ac12018-06-22 17:19:51 +0200170 return -EINVAL;
171
Stefan Agner4dc98db2018-06-22 18:06:15 +0200172 if (geo->ecc_strength > nand_info->max_ecc_strength_supported)
Stefan Agner4d42ac12018-06-22 17:19:51 +0200173 return -EINVAL;
174
Ye Li94547442020-05-04 22:08:50 +0800175 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size;
176
177 /* For bit swap. */
178 block_mark_bit_offset = mtd->writesize * 8 -
179 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
180 + MXS_NAND_METADATA_SIZE * 8);
181
182 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
183 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
Stefan Agner4d42ac12018-06-22 17:19:51 +0200184
185 return 0;
186}
187
Ye Li94547442020-05-04 22:08:50 +0800188static inline int mxs_nand_legacy_calc_ecc_layout(struct bch_geometry *geo,
Stefan Agnerd0778b32018-06-22 17:19:49 +0200189 struct mtd_info *mtd)
Marek Vasut913a7252011-11-08 23:18:16 +0000190{
Stefan Agner4dc98db2018-06-22 18:06:15 +0200191 struct nand_chip *chip = mtd_to_nand(mtd);
192 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Ye Li94547442020-05-04 22:08:50 +0800193 unsigned int block_mark_bit_offset;
Stefan Agner4dc98db2018-06-22 18:06:15 +0200194
Stefan Agnerd0778b32018-06-22 17:19:49 +0200195 /* The default for the length of Galois Field. */
196 geo->gf_len = 13;
197
198 /* The default for chunk size. */
Ye Li94547442020-05-04 22:08:50 +0800199 geo->ecc_chunk0_size = 512;
200 geo->ecc_chunkn_size = 512;
Stefan Agnerd0778b32018-06-22 17:19:49 +0200201
Ye Li94547442020-05-04 22:08:50 +0800202 if (geo->ecc_chunkn_size < mtd->oobsize) {
Stefan Agnerd0778b32018-06-22 17:19:49 +0200203 geo->gf_len = 14;
Ye Li94547442020-05-04 22:08:50 +0800204 geo->ecc_chunk0_size *= 2;
205 geo->ecc_chunkn_size *= 2;
Stefan Agnerd0778b32018-06-22 17:19:49 +0200206 }
207
Ye Li94547442020-05-04 22:08:50 +0800208 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size;
Stefan Agnerd0778b32018-06-22 17:19:49 +0200209
Stefan Agnerd0778b32018-06-22 17:19:49 +0200210 /*
211 * Determine the ECC layout with the formula:
212 * ECC bits per chunk = (total page spare data bits) /
213 * (bits per ECC level) / (chunks per page)
214 * where:
215 * total page spare data bits =
216 * (page oob size - meta data size) * (bits per byte)
217 */
218 geo->ecc_strength = ((mtd->oobsize - MXS_NAND_METADATA_SIZE) * 8)
219 / (geo->gf_len * geo->ecc_chunk_count);
220
Stefan Agner4d42ac12018-06-22 17:19:51 +0200221 geo->ecc_strength = min(round_down(geo->ecc_strength, 2),
Stefan Agner4dc98db2018-06-22 18:06:15 +0200222 nand_info->max_ecc_strength_supported);
Stefan Agnerd0778b32018-06-22 17:19:49 +0200223
Ye Li94547442020-05-04 22:08:50 +0800224 block_mark_bit_offset = mtd->writesize * 8 -
225 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
226 + MXS_NAND_METADATA_SIZE * 8);
227
228 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
229 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
230
231 return 0;
232}
233
234static inline int mxs_nand_calc_ecc_for_large_oob(struct bch_geometry *geo,
235 struct mtd_info *mtd)
236{
237 struct nand_chip *chip = mtd_to_nand(mtd);
238 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
239 unsigned int block_mark_bit_offset;
240 unsigned int max_ecc;
241 unsigned int bbm_chunk;
242 unsigned int i;
243
244 /* sanity check for the minimum ecc nand required */
245 if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
246 return -EINVAL;
247 geo->ecc_strength = chip->ecc_strength_ds;
248
249 /* calculate the maximum ecc platform can support*/
250 geo->gf_len = 14;
251 geo->ecc_chunk0_size = 1024;
252 geo->ecc_chunkn_size = 1024;
253 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size;
254 max_ecc = ((mtd->oobsize - MXS_NAND_METADATA_SIZE) * 8)
255 / (geo->gf_len * geo->ecc_chunk_count);
256 max_ecc = min(round_down(max_ecc, 2),
257 nand_info->max_ecc_strength_supported);
258
259
260 /* search a supported ecc strength that makes bbm */
261 /* located in data chunk */
262 geo->ecc_strength = chip->ecc_strength_ds;
263 while (!(geo->ecc_strength > max_ecc)) {
264 if (mxs_nand_bbm_in_data_chunk(geo, mtd, &bbm_chunk))
265 break;
266 geo->ecc_strength += 2;
267 }
268
269 /* if none of them works, keep using the minimum ecc */
270 /* nand required but changing ecc page layout */
271 if (geo->ecc_strength > max_ecc) {
272 geo->ecc_strength = chip->ecc_strength_ds;
273 /* add extra ecc for meta data */
274 geo->ecc_chunk0_size = 0;
275 geo->ecc_chunk_count = (mtd->writesize / geo->ecc_chunkn_size) + 1;
276 geo->ecc_for_meta = 1;
277 /* check if oob can afford this extra ecc chunk */
278 if (mtd->oobsize * 8 < MXS_NAND_METADATA_SIZE * 8 +
279 geo->gf_len * geo->ecc_strength
280 * geo->ecc_chunk_count) {
281 printf("unsupported NAND chip with new layout\n");
282 return -EINVAL;
283 }
284
285 /* calculate in which chunk bbm located */
286 bbm_chunk = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8 -
287 geo->gf_len * geo->ecc_strength) /
288 (geo->gf_len * geo->ecc_strength +
289 geo->ecc_chunkn_size * 8) + 1;
290 }
291
292 /* calculate the number of ecc chunk behind the bbm */
293 i = (mtd->writesize / geo->ecc_chunkn_size) - bbm_chunk + 1;
294
295 block_mark_bit_offset = mtd->writesize * 8 -
296 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - i)
297 + MXS_NAND_METADATA_SIZE * 8);
298
299 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
300 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
301
Stefan Agnerd0778b32018-06-22 17:19:49 +0200302 return 0;
Marek Vasut913a7252011-11-08 23:18:16 +0000303}
304
305/*
306 * Wait for BCH complete IRQ and clear the IRQ
307 */
Stefan Agnerdc8af6d2018-06-22 18:06:12 +0200308static int mxs_nand_wait_for_bch_complete(struct mxs_nand_info *nand_info)
Marek Vasut913a7252011-11-08 23:18:16 +0000309{
Marek Vasut913a7252011-11-08 23:18:16 +0000310 int timeout = MXS_NAND_BCH_TIMEOUT;
311 int ret;
312
Stefan Agnerdc8af6d2018-06-22 18:06:12 +0200313 ret = mxs_wait_mask_set(&nand_info->bch_regs->hw_bch_ctrl_reg,
Marek Vasut913a7252011-11-08 23:18:16 +0000314 BCH_CTRL_COMPLETE_IRQ, timeout);
315
Stefan Agnerdc8af6d2018-06-22 18:06:12 +0200316 writel(BCH_CTRL_COMPLETE_IRQ, &nand_info->bch_regs->hw_bch_ctrl_clr);
Marek Vasut913a7252011-11-08 23:18:16 +0000317
318 return ret;
319}
320
321/*
322 * This is the function that we install in the cmd_ctrl function pointer of the
323 * owning struct nand_chip. The only functions in the reference implementation
324 * that use these functions pointers are cmdfunc and select_chip.
325 *
326 * In this driver, we implement our own select_chip, so this function will only
327 * be called by the reference implementation's cmdfunc. For this reason, we can
328 * ignore the chip enable bit and concentrate only on sending bytes to the NAND
329 * Flash.
330 */
331static void mxs_nand_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
332{
Scott Wood17fed142016-05-30 13:57:56 -0500333 struct nand_chip *nand = mtd_to_nand(mtd);
334 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut913a7252011-11-08 23:18:16 +0000335 struct mxs_dma_desc *d;
336 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
337 int ret;
338
339 /*
340 * If this condition is true, something is _VERY_ wrong in MTD
341 * subsystem!
342 */
343 if (nand_info->cmd_queue_len == MXS_NAND_COMMAND_BUFFER_SIZE) {
344 printf("MXS NAND: Command queue too long\n");
345 return;
346 }
347
348 /*
349 * Every operation begins with a command byte and a series of zero or
350 * more address bytes. These are distinguished by either the Address
351 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
352 * asserted. When MTD is ready to execute the command, it will
353 * deasert both latch enables.
354 *
355 * Rather than run a separate DMA operation for every single byte, we
356 * queue them up and run a single DMA operation for the entire series
357 * of command and data bytes.
358 */
359 if (ctrl & (NAND_ALE | NAND_CLE)) {
360 if (data != NAND_CMD_NONE)
361 nand_info->cmd_buf[nand_info->cmd_queue_len++] = data;
362 return;
363 }
364
365 /*
366 * If control arrives here, MTD has deasserted both the ALE and CLE,
367 * which means it's ready to run an operation. Check if we have any
368 * bytes to send.
369 */
370 if (nand_info->cmd_queue_len == 0)
371 return;
372
373 /* Compile the DMA descriptor -- a descriptor that sends command. */
374 d = mxs_nand_get_dma_desc(nand_info);
375 d->cmd.data =
376 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
377 MXS_DMA_DESC_CHAIN | MXS_DMA_DESC_DEC_SEM |
378 MXS_DMA_DESC_WAIT4END | (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
379 (nand_info->cmd_queue_len << MXS_DMA_DESC_BYTES_OFFSET);
380
381 d->cmd.address = (dma_addr_t)nand_info->cmd_buf;
382
383 d->cmd.pio_words[0] =
384 GPMI_CTRL0_COMMAND_MODE_WRITE |
385 GPMI_CTRL0_WORD_LENGTH |
386 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
387 GPMI_CTRL0_ADDRESS_NAND_CLE |
388 GPMI_CTRL0_ADDRESS_INCREMENT |
389 nand_info->cmd_queue_len;
390
391 mxs_dma_desc_append(channel, d);
392
Marek Vasut1b120e82012-03-15 18:33:19 +0000393 /* Flush caches */
394 mxs_nand_flush_cmd_buf(nand_info);
395
Marek Vasut913a7252011-11-08 23:18:16 +0000396 /* Execute the DMA chain. */
397 ret = mxs_dma_go(channel);
398 if (ret)
399 printf("MXS NAND: Error sending command\n");
400
401 mxs_nand_return_dma_descs(nand_info);
402
403 /* Reset the command queue. */
404 nand_info->cmd_queue_len = 0;
405}
406
407/*
408 * Test if the NAND flash is ready.
409 */
410static int mxs_nand_device_ready(struct mtd_info *mtd)
411{
Scott Wood17fed142016-05-30 13:57:56 -0500412 struct nand_chip *chip = mtd_to_nand(mtd);
413 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Marek Vasut913a7252011-11-08 23:18:16 +0000414 uint32_t tmp;
415
Stefan Agnerdc8af6d2018-06-22 18:06:12 +0200416 tmp = readl(&nand_info->gpmi_regs->hw_gpmi_stat);
Marek Vasut913a7252011-11-08 23:18:16 +0000417 tmp >>= (GPMI_STAT_READY_BUSY_OFFSET + nand_info->cur_chip);
418
419 return tmp & 1;
420}
421
422/*
423 * Select the NAND chip.
424 */
425static void mxs_nand_select_chip(struct mtd_info *mtd, int chip)
426{
Scott Wood17fed142016-05-30 13:57:56 -0500427 struct nand_chip *nand = mtd_to_nand(mtd);
428 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut913a7252011-11-08 23:18:16 +0000429
430 nand_info->cur_chip = chip;
431}
432
433/*
434 * Handle block mark swapping.
435 *
436 * Note that, when this function is called, it doesn't know whether it's
437 * swapping the block mark, or swapping it *back* -- but it doesn't matter
438 * because the the operation is the same.
439 */
Stefan Agnerd0778b32018-06-22 17:19:49 +0200440static void mxs_nand_swap_block_mark(struct bch_geometry *geo,
441 uint8_t *data_buf, uint8_t *oob_buf)
Marek Vasut913a7252011-11-08 23:18:16 +0000442{
Stefan Agnerd0778b32018-06-22 17:19:49 +0200443 uint32_t bit_offset = geo->block_mark_bit_offset;
444 uint32_t buf_offset = geo->block_mark_byte_offset;
Marek Vasut913a7252011-11-08 23:18:16 +0000445
446 uint32_t src;
447 uint32_t dst;
448
Marek Vasut913a7252011-11-08 23:18:16 +0000449 /*
450 * Get the byte from the data area that overlays the block mark. Since
451 * the ECC engine applies its own view to the bits in the page, the
452 * physical block mark won't (in general) appear on a byte boundary in
453 * the data.
454 */
455 src = data_buf[buf_offset] >> bit_offset;
456 src |= data_buf[buf_offset + 1] << (8 - bit_offset);
457
458 dst = oob_buf[0];
459
460 oob_buf[0] = src;
461
462 data_buf[buf_offset] &= ~(0xff << bit_offset);
463 data_buf[buf_offset + 1] &= 0xff << bit_offset;
464
465 data_buf[buf_offset] |= dst << bit_offset;
466 data_buf[buf_offset + 1] |= dst >> (8 - bit_offset);
467}
468
469/*
470 * Read data from NAND.
471 */
472static void mxs_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int length)
473{
Scott Wood17fed142016-05-30 13:57:56 -0500474 struct nand_chip *nand = mtd_to_nand(mtd);
475 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut913a7252011-11-08 23:18:16 +0000476 struct mxs_dma_desc *d;
477 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
478 int ret;
479
480 if (length > NAND_MAX_PAGESIZE) {
481 printf("MXS NAND: DMA buffer too big\n");
482 return;
483 }
484
485 if (!buf) {
486 printf("MXS NAND: DMA buffer is NULL\n");
487 return;
488 }
489
490 /* Compile the DMA descriptor - a descriptor that reads data. */
491 d = mxs_nand_get_dma_desc(nand_info);
492 d->cmd.data =
493 MXS_DMA_DESC_COMMAND_DMA_WRITE | MXS_DMA_DESC_IRQ |
494 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
495 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
496 (length << MXS_DMA_DESC_BYTES_OFFSET);
497
498 d->cmd.address = (dma_addr_t)nand_info->data_buf;
499
500 d->cmd.pio_words[0] =
501 GPMI_CTRL0_COMMAND_MODE_READ |
502 GPMI_CTRL0_WORD_LENGTH |
503 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
504 GPMI_CTRL0_ADDRESS_NAND_DATA |
505 length;
506
507 mxs_dma_desc_append(channel, d);
508
509 /*
510 * A DMA descriptor that waits for the command to end and the chip to
511 * become ready.
512 *
513 * I think we actually should *not* be waiting for the chip to become
514 * ready because, after all, we don't care. I think the original code
515 * did that and no one has re-thought it yet.
516 */
517 d = mxs_nand_get_dma_desc(nand_info);
518 d->cmd.data =
519 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
520 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_DEC_SEM |
Luca Ellero80f06b82014-12-16 15:36:14 +0100521 MXS_DMA_DESC_WAIT4END | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
Marek Vasut913a7252011-11-08 23:18:16 +0000522
523 d->cmd.address = 0;
524
525 d->cmd.pio_words[0] =
526 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
527 GPMI_CTRL0_WORD_LENGTH |
528 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
529 GPMI_CTRL0_ADDRESS_NAND_DATA;
530
531 mxs_dma_desc_append(channel, d);
532
Peng Fane3bbfb72015-07-21 16:15:21 +0800533 /* Invalidate caches */
534 mxs_nand_inval_data_buf(nand_info);
535
Marek Vasut913a7252011-11-08 23:18:16 +0000536 /* Execute the DMA chain. */
537 ret = mxs_dma_go(channel);
538 if (ret) {
539 printf("MXS NAND: DMA read error\n");
540 goto rtn;
541 }
542
Marek Vasut1b120e82012-03-15 18:33:19 +0000543 /* Invalidate caches */
544 mxs_nand_inval_data_buf(nand_info);
545
Marek Vasut913a7252011-11-08 23:18:16 +0000546 memcpy(buf, nand_info->data_buf, length);
547
548rtn:
549 mxs_nand_return_dma_descs(nand_info);
550}
551
552/*
553 * Write data to NAND.
554 */
555static void mxs_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
556 int length)
557{
Scott Wood17fed142016-05-30 13:57:56 -0500558 struct nand_chip *nand = mtd_to_nand(mtd);
559 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut913a7252011-11-08 23:18:16 +0000560 struct mxs_dma_desc *d;
561 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
562 int ret;
563
564 if (length > NAND_MAX_PAGESIZE) {
565 printf("MXS NAND: DMA buffer too big\n");
566 return;
567 }
568
569 if (!buf) {
570 printf("MXS NAND: DMA buffer is NULL\n");
571 return;
572 }
573
574 memcpy(nand_info->data_buf, buf, length);
575
576 /* Compile the DMA descriptor - a descriptor that writes data. */
577 d = mxs_nand_get_dma_desc(nand_info);
578 d->cmd.data =
579 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
580 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
Luca Ellero966f1cd2014-12-16 15:36:15 +0100581 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
Marek Vasut913a7252011-11-08 23:18:16 +0000582 (length << MXS_DMA_DESC_BYTES_OFFSET);
583
584 d->cmd.address = (dma_addr_t)nand_info->data_buf;
585
586 d->cmd.pio_words[0] =
587 GPMI_CTRL0_COMMAND_MODE_WRITE |
588 GPMI_CTRL0_WORD_LENGTH |
589 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
590 GPMI_CTRL0_ADDRESS_NAND_DATA |
591 length;
592
593 mxs_dma_desc_append(channel, d);
594
Marek Vasut1b120e82012-03-15 18:33:19 +0000595 /* Flush caches */
596 mxs_nand_flush_data_buf(nand_info);
597
Marek Vasut913a7252011-11-08 23:18:16 +0000598 /* Execute the DMA chain. */
599 ret = mxs_dma_go(channel);
600 if (ret)
601 printf("MXS NAND: DMA write error\n");
602
603 mxs_nand_return_dma_descs(nand_info);
604}
605
606/*
607 * Read a single byte from NAND.
608 */
609static uint8_t mxs_nand_read_byte(struct mtd_info *mtd)
610{
611 uint8_t buf;
612 mxs_nand_read_buf(mtd, &buf, 1);
613 return buf;
614}
615
Peng Fandf23c9d2020-05-04 22:08:52 +0800616static bool mxs_nand_erased_page(struct mtd_info *mtd, struct nand_chip *nand,
617 u8 *buf, int chunk, int page)
618{
619 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
620 struct bch_geometry *geo = &nand_info->bch_geometry;
621 unsigned int flip_bits = 0, flip_bits_noecc = 0;
622 unsigned int threshold;
623 unsigned int base = geo->ecc_chunkn_size * chunk;
624 u32 *dma_buf = (u32 *)buf;
625 int i;
626
627 threshold = geo->gf_len / 2;
628 if (threshold > geo->ecc_strength)
629 threshold = geo->ecc_strength;
630
631 for (i = 0; i < geo->ecc_chunkn_size; i++) {
632 flip_bits += hweight8(~buf[base + i]);
633 if (flip_bits > threshold)
634 return false;
635 }
636
637 nand->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
638 nand->read_buf(mtd, buf, mtd->writesize);
639
640 for (i = 0; i < mtd->writesize / 4; i++) {
641 flip_bits_noecc += hweight32(~dma_buf[i]);
642 if (flip_bits_noecc > threshold)
643 return false;
644 }
645
646 mtd->ecc_stats.corrected += flip_bits;
647
648 memset(buf, 0xff, mtd->writesize);
649
650 printf("The page(%d) is an erased page(%d,%d,%d,%d).\n", page, chunk, threshold, flip_bits, flip_bits_noecc);
651
652 return true;
653}
654
Marek Vasut913a7252011-11-08 23:18:16 +0000655/*
656 * Read a page from NAND.
657 */
658static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000659 uint8_t *buf, int oob_required,
660 int page)
Marek Vasut913a7252011-11-08 23:18:16 +0000661{
Scott Wood17fed142016-05-30 13:57:56 -0500662 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Stefan Agnerd0778b32018-06-22 17:19:49 +0200663 struct bch_geometry *geo = &nand_info->bch_geometry;
Peng Fan9e813732020-05-04 22:08:53 +0800664 struct mxs_bch_regs *bch_regs = nand_info->bch_regs;
Marek Vasut913a7252011-11-08 23:18:16 +0000665 struct mxs_dma_desc *d;
666 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
667 uint32_t corrected = 0, failed = 0;
668 uint8_t *status;
669 int i, ret;
Peng Fan9e813732020-05-04 22:08:53 +0800670 int flag = 0;
Marek Vasut913a7252011-11-08 23:18:16 +0000671
672 /* Compile the DMA descriptor - wait for ready. */
673 d = mxs_nand_get_dma_desc(nand_info);
674 d->cmd.data =
675 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
676 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
677 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
678
679 d->cmd.address = 0;
680
681 d->cmd.pio_words[0] =
682 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
683 GPMI_CTRL0_WORD_LENGTH |
684 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
685 GPMI_CTRL0_ADDRESS_NAND_DATA;
686
687 mxs_dma_desc_append(channel, d);
688
689 /* Compile the DMA descriptor - enable the BCH block and read. */
690 d = mxs_nand_get_dma_desc(nand_info);
691 d->cmd.data =
692 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
693 MXS_DMA_DESC_WAIT4END | (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
694
695 d->cmd.address = 0;
696
697 d->cmd.pio_words[0] =
698 GPMI_CTRL0_COMMAND_MODE_READ |
699 GPMI_CTRL0_WORD_LENGTH |
700 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
701 GPMI_CTRL0_ADDRESS_NAND_DATA |
702 (mtd->writesize + mtd->oobsize);
703 d->cmd.pio_words[1] = 0;
704 d->cmd.pio_words[2] =
705 GPMI_ECCCTRL_ENABLE_ECC |
706 GPMI_ECCCTRL_ECC_CMD_DECODE |
707 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
708 d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
709 d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
710 d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
711
712 mxs_dma_desc_append(channel, d);
713
714 /* Compile the DMA descriptor - disable the BCH block. */
715 d = mxs_nand_get_dma_desc(nand_info);
716 d->cmd.data =
717 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
718 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
719 (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
720
721 d->cmd.address = 0;
722
723 d->cmd.pio_words[0] =
724 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
725 GPMI_CTRL0_WORD_LENGTH |
726 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
727 GPMI_CTRL0_ADDRESS_NAND_DATA |
728 (mtd->writesize + mtd->oobsize);
729 d->cmd.pio_words[1] = 0;
730 d->cmd.pio_words[2] = 0;
731
732 mxs_dma_desc_append(channel, d);
733
734 /* Compile the DMA descriptor - deassert the NAND lock and interrupt. */
735 d = mxs_nand_get_dma_desc(nand_info);
736 d->cmd.data =
737 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
738 MXS_DMA_DESC_DEC_SEM;
739
740 d->cmd.address = 0;
741
742 mxs_dma_desc_append(channel, d);
743
Peng Fane3bbfb72015-07-21 16:15:21 +0800744 /* Invalidate caches */
745 mxs_nand_inval_data_buf(nand_info);
746
Marek Vasut913a7252011-11-08 23:18:16 +0000747 /* Execute the DMA chain. */
748 ret = mxs_dma_go(channel);
749 if (ret) {
750 printf("MXS NAND: DMA read error\n");
751 goto rtn;
752 }
753
Stefan Agnerdc8af6d2018-06-22 18:06:12 +0200754 ret = mxs_nand_wait_for_bch_complete(nand_info);
Marek Vasut913a7252011-11-08 23:18:16 +0000755 if (ret) {
756 printf("MXS NAND: BCH read timeout\n");
757 goto rtn;
758 }
759
Peng Fandf23c9d2020-05-04 22:08:52 +0800760 mxs_nand_return_dma_descs(nand_info);
761
Marek Vasut1b120e82012-03-15 18:33:19 +0000762 /* Invalidate caches */
763 mxs_nand_inval_data_buf(nand_info);
764
Marek Vasut913a7252011-11-08 23:18:16 +0000765 /* Read DMA completed, now do the mark swapping. */
Stefan Agnerd0778b32018-06-22 17:19:49 +0200766 mxs_nand_swap_block_mark(geo, nand_info->data_buf, nand_info->oob_buf);
Marek Vasut913a7252011-11-08 23:18:16 +0000767
768 /* Loop over status bytes, accumulating ECC status. */
769 status = nand_info->oob_buf + mxs_nand_aux_status_offset();
Stefan Agnerd0778b32018-06-22 17:19:49 +0200770 for (i = 0; i < geo->ecc_chunk_count; i++) {
Marek Vasut913a7252011-11-08 23:18:16 +0000771 if (status[i] == 0x00)
772 continue;
773
Peng Fan9e813732020-05-04 22:08:53 +0800774 if (status[i] == 0xff) {
775 if (is_mx6dqp() || is_mx7() ||
Ye Li74948b82020-05-04 22:08:54 +0800776 is_mx6ul() || is_imx8m())
Peng Fan9e813732020-05-04 22:08:53 +0800777 if (readl(&bch_regs->hw_bch_debug1))
778 flag = 1;
Marek Vasut913a7252011-11-08 23:18:16 +0000779 continue;
Peng Fan9e813732020-05-04 22:08:53 +0800780 }
Marek Vasut913a7252011-11-08 23:18:16 +0000781
782 if (status[i] == 0xfe) {
Peng Fandf23c9d2020-05-04 22:08:52 +0800783 if (mxs_nand_erased_page(mtd, nand,
784 nand_info->data_buf, i, page))
785 break;
Marek Vasut913a7252011-11-08 23:18:16 +0000786 failed++;
787 continue;
788 }
789
790 corrected += status[i];
791 }
792
793 /* Propagate ECC status to the owning MTD. */
794 mtd->ecc_stats.failed += failed;
795 mtd->ecc_stats.corrected += corrected;
796
797 /*
798 * It's time to deliver the OOB bytes. See mxs_nand_ecc_read_oob() for
799 * details about our policy for delivering the OOB.
800 *
801 * We fill the caller's buffer with set bits, and then copy the block
802 * mark to the caller's buffer. Note that, if block mark swapping was
803 * necessary, it has already been done, so we can rely on the first
804 * byte of the auxiliary buffer to contain the block mark.
805 */
806 memset(nand->oob_poi, 0xff, mtd->oobsize);
807
808 nand->oob_poi[0] = nand_info->oob_buf[0];
809
810 memcpy(buf, nand_info->data_buf, mtd->writesize);
811
Peng Fan9e813732020-05-04 22:08:53 +0800812 if (flag)
813 memset(buf, 0xff, mtd->writesize);
Marek Vasut913a7252011-11-08 23:18:16 +0000814rtn:
815 mxs_nand_return_dma_descs(nand_info);
816
817 return ret;
818}
819
820/*
821 * Write a page to NAND.
822 */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000823static int mxs_nand_ecc_write_page(struct mtd_info *mtd,
824 struct nand_chip *nand, const uint8_t *buf,
Scott Wood46e13102016-05-30 13:57:57 -0500825 int oob_required, int page)
Marek Vasut913a7252011-11-08 23:18:16 +0000826{
Scott Wood17fed142016-05-30 13:57:56 -0500827 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Stefan Agnerd0778b32018-06-22 17:19:49 +0200828 struct bch_geometry *geo = &nand_info->bch_geometry;
Marek Vasut913a7252011-11-08 23:18:16 +0000829 struct mxs_dma_desc *d;
830 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
831 int ret;
832
833 memcpy(nand_info->data_buf, buf, mtd->writesize);
834 memcpy(nand_info->oob_buf, nand->oob_poi, mtd->oobsize);
835
836 /* Handle block mark swapping. */
Stefan Agnerd0778b32018-06-22 17:19:49 +0200837 mxs_nand_swap_block_mark(geo, nand_info->data_buf, nand_info->oob_buf);
Marek Vasut913a7252011-11-08 23:18:16 +0000838
839 /* Compile the DMA descriptor - write data. */
840 d = mxs_nand_get_dma_desc(nand_info);
841 d->cmd.data =
842 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
843 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
844 (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
845
846 d->cmd.address = 0;
847
848 d->cmd.pio_words[0] =
849 GPMI_CTRL0_COMMAND_MODE_WRITE |
850 GPMI_CTRL0_WORD_LENGTH |
851 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
852 GPMI_CTRL0_ADDRESS_NAND_DATA;
853 d->cmd.pio_words[1] = 0;
854 d->cmd.pio_words[2] =
855 GPMI_ECCCTRL_ENABLE_ECC |
856 GPMI_ECCCTRL_ECC_CMD_ENCODE |
857 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
858 d->cmd.pio_words[3] = (mtd->writesize + mtd->oobsize);
859 d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
860 d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
861
Igor Opaniukc55401372019-11-03 16:49:43 +0100862 if (is_mx7() && nand_info->en_randomizer) {
863 d->cmd.pio_words[2] |= GPMI_ECCCTRL_RANDOMIZER_ENABLE |
864 GPMI_ECCCTRL_RANDOMIZER_TYPE2;
865 /*
866 * Write NAND page number needed to be randomized
867 * to GPMI_ECCCOUNT register.
868 *
869 * The value is between 0-255. For additional details
870 * check 9.6.6.4 of i.MX7D Applications Processor reference
871 */
872 d->cmd.pio_words[3] |= (page % 255) << 16;
873 }
874
Marek Vasut913a7252011-11-08 23:18:16 +0000875 mxs_dma_desc_append(channel, d);
876
Marek Vasut1b120e82012-03-15 18:33:19 +0000877 /* Flush caches */
878 mxs_nand_flush_data_buf(nand_info);
879
Marek Vasut913a7252011-11-08 23:18:16 +0000880 /* Execute the DMA chain. */
881 ret = mxs_dma_go(channel);
882 if (ret) {
883 printf("MXS NAND: DMA write error\n");
884 goto rtn;
885 }
886
Stefan Agnerdc8af6d2018-06-22 18:06:12 +0200887 ret = mxs_nand_wait_for_bch_complete(nand_info);
Marek Vasut913a7252011-11-08 23:18:16 +0000888 if (ret) {
889 printf("MXS NAND: BCH write timeout\n");
890 goto rtn;
891 }
892
893rtn:
894 mxs_nand_return_dma_descs(nand_info);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000895 return 0;
Marek Vasut913a7252011-11-08 23:18:16 +0000896}
897
898/*
899 * Read OOB from NAND.
900 *
901 * This function is a veneer that replaces the function originally installed by
902 * the NAND Flash MTD code.
903 */
904static int mxs_nand_hook_read_oob(struct mtd_info *mtd, loff_t from,
905 struct mtd_oob_ops *ops)
906{
Scott Wood17fed142016-05-30 13:57:56 -0500907 struct nand_chip *chip = mtd_to_nand(mtd);
908 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Marek Vasut913a7252011-11-08 23:18:16 +0000909 int ret;
910
Sergey Lapin3a38a552013-01-14 03:46:50 +0000911 if (ops->mode == MTD_OPS_RAW)
Marek Vasut913a7252011-11-08 23:18:16 +0000912 nand_info->raw_oob_mode = 1;
913 else
914 nand_info->raw_oob_mode = 0;
915
916 ret = nand_info->hooked_read_oob(mtd, from, ops);
917
918 nand_info->raw_oob_mode = 0;
919
920 return ret;
921}
922
923/*
924 * Write OOB to NAND.
925 *
926 * This function is a veneer that replaces the function originally installed by
927 * the NAND Flash MTD code.
928 */
929static int mxs_nand_hook_write_oob(struct mtd_info *mtd, loff_t to,
930 struct mtd_oob_ops *ops)
931{
Scott Wood17fed142016-05-30 13:57:56 -0500932 struct nand_chip *chip = mtd_to_nand(mtd);
933 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Marek Vasut913a7252011-11-08 23:18:16 +0000934 int ret;
935
Sergey Lapin3a38a552013-01-14 03:46:50 +0000936 if (ops->mode == MTD_OPS_RAW)
Marek Vasut913a7252011-11-08 23:18:16 +0000937 nand_info->raw_oob_mode = 1;
938 else
939 nand_info->raw_oob_mode = 0;
940
941 ret = nand_info->hooked_write_oob(mtd, to, ops);
942
943 nand_info->raw_oob_mode = 0;
944
945 return ret;
946}
947
948/*
949 * Mark a block bad in NAND.
950 *
951 * This function is a veneer that replaces the function originally installed by
952 * the NAND Flash MTD code.
953 */
954static int mxs_nand_hook_block_markbad(struct mtd_info *mtd, loff_t ofs)
955{
Scott Wood17fed142016-05-30 13:57:56 -0500956 struct nand_chip *chip = mtd_to_nand(mtd);
957 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Marek Vasut913a7252011-11-08 23:18:16 +0000958 int ret;
959
960 nand_info->marking_block_bad = 1;
961
962 ret = nand_info->hooked_block_markbad(mtd, ofs);
963
964 nand_info->marking_block_bad = 0;
965
966 return ret;
967}
968
969/*
970 * There are several places in this driver where we have to handle the OOB and
971 * block marks. This is the function where things are the most complicated, so
972 * this is where we try to explain it all. All the other places refer back to
973 * here.
974 *
975 * These are the rules, in order of decreasing importance:
976 *
977 * 1) Nothing the caller does can be allowed to imperil the block mark, so all
978 * write operations take measures to protect it.
979 *
980 * 2) In read operations, the first byte of the OOB we return must reflect the
981 * true state of the block mark, no matter where that block mark appears in
982 * the physical page.
983 *
984 * 3) ECC-based read operations return an OOB full of set bits (since we never
985 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
986 * return).
987 *
988 * 4) "Raw" read operations return a direct view of the physical bytes in the
989 * page, using the conventional definition of which bytes are data and which
990 * are OOB. This gives the caller a way to see the actual, physical bytes
991 * in the page, without the distortions applied by our ECC engine.
992 *
993 * What we do for this specific read operation depends on whether we're doing
994 * "raw" read, or an ECC-based read.
995 *
996 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
997 * easy. When reading a page, for example, the NAND Flash MTD code calls our
998 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
999 * ECC-based or raw view of the page is implicit in which function it calls
1000 * (there is a similar pair of ECC-based/raw functions for writing).
1001 *
1002 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
1003 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
1004 * caller wants an ECC-based or raw view of the page is not propagated down to
1005 * this driver.
1006 *
1007 * Since our OOB *is* covered by ECC, we need this information. So, we hook the
1008 * ecc.read_oob and ecc.write_oob function pointers in the owning
1009 * struct mtd_info with our own functions. These hook functions set the
1010 * raw_oob_mode field so that, when control finally arrives here, we'll know
1011 * what to do.
1012 */
1013static int mxs_nand_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001014 int page)
Marek Vasut913a7252011-11-08 23:18:16 +00001015{
Scott Wood17fed142016-05-30 13:57:56 -05001016 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut913a7252011-11-08 23:18:16 +00001017
1018 /*
1019 * First, fill in the OOB buffer. If we're doing a raw read, we need to
1020 * get the bytes from the physical page. If we're not doing a raw read,
1021 * we need to fill the buffer with set bits.
1022 */
1023 if (nand_info->raw_oob_mode) {
1024 /*
1025 * If control arrives here, we're doing a "raw" read. Send the
1026 * command to read the conventional OOB and read it.
1027 */
1028 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1029 nand->read_buf(mtd, nand->oob_poi, mtd->oobsize);
1030 } else {
1031 /*
1032 * If control arrives here, we're not doing a "raw" read. Fill
1033 * the OOB buffer with set bits and correct the block mark.
1034 */
1035 memset(nand->oob_poi, 0xff, mtd->oobsize);
1036
1037 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1038 mxs_nand_read_buf(mtd, nand->oob_poi, 1);
1039 }
1040
1041 return 0;
1042
1043}
1044
1045/*
1046 * Write OOB data to NAND.
1047 */
1048static int mxs_nand_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
1049 int page)
1050{
Scott Wood17fed142016-05-30 13:57:56 -05001051 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut913a7252011-11-08 23:18:16 +00001052 uint8_t block_mark = 0;
1053
1054 /*
1055 * There are fundamental incompatibilities between the i.MX GPMI NFC and
1056 * the NAND Flash MTD model that make it essentially impossible to write
1057 * the out-of-band bytes.
1058 *
1059 * We permit *ONE* exception. If the *intent* of writing the OOB is to
1060 * mark a block bad, we can do that.
1061 */
1062
1063 if (!nand_info->marking_block_bad) {
1064 printf("NXS NAND: Writing OOB isn't supported\n");
1065 return -EIO;
1066 }
1067
1068 /* Write the block mark. */
1069 nand->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1070 nand->write_buf(mtd, &block_mark, 1);
1071 nand->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1072
1073 /* Check if it worked. */
1074 if (nand->waitfunc(mtd, nand) & NAND_STATUS_FAIL)
1075 return -EIO;
1076
1077 return 0;
1078}
1079
1080/*
1081 * Claims all blocks are good.
1082 *
1083 * In principle, this function is *only* called when the NAND Flash MTD system
1084 * isn't allowed to keep an in-memory bad block table, so it is forced to ask
1085 * the driver for bad block information.
1086 *
1087 * In fact, we permit the NAND Flash MTD system to have an in-memory BBT, so
1088 * this function is *only* called when we take it away.
1089 *
1090 * Thus, this function is only called when we want *all* blocks to look good,
1091 * so it *always* return success.
1092 */
Scott Wood52ab7ce2016-05-30 13:57:58 -05001093static int mxs_nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Marek Vasut913a7252011-11-08 23:18:16 +00001094{
Stefan Agneread66eb2018-06-22 18:06:18 +02001095 return 0;
1096}
1097
1098static int mxs_nand_set_geometry(struct mtd_info *mtd, struct bch_geometry *geo)
1099{
1100 struct nand_chip *chip = mtd_to_nand(mtd);
1101 struct nand_chip *nand = mtd_to_nand(mtd);
1102 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1103
Ye Li94547442020-05-04 22:08:50 +08001104 if (chip->ecc_strength_ds > nand_info->max_ecc_strength_supported) {
1105 printf("unsupported NAND chip, minimum ecc required %d\n"
1106 , chip->ecc_strength_ds);
1107 return -EINVAL;
1108 }
Stefan Agneread66eb2018-06-22 18:06:18 +02001109
Ye Lic6736132020-05-04 22:08:51 +08001110 if ((!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0) &&
1111 mtd->oobsize < 1024) || nand_info->legacy_bch_geometry) {
Ye Li94547442020-05-04 22:08:50 +08001112 dev_warn(this->dev, "use legacy bch geometry\n");
1113 return mxs_nand_legacy_calc_ecc_layout(geo, mtd);
1114 }
Stefan Agneread66eb2018-06-22 18:06:18 +02001115
Ye Li94547442020-05-04 22:08:50 +08001116 if (mtd->oobsize > 1024 || chip->ecc_step_ds < mtd->oobsize)
1117 return mxs_nand_calc_ecc_for_large_oob(geo, mtd);
1118
1119 return mxs_nand_calc_ecc_layout_by_info(geo, mtd,
Stefan Agneread66eb2018-06-22 18:06:18 +02001120 chip->ecc_strength_ds, chip->ecc_step_ds);
Stefan Agneread66eb2018-06-22 18:06:18 +02001121
Marek Vasut913a7252011-11-08 23:18:16 +00001122 return 0;
1123}
1124
1125/*
Marek Vasut913a7252011-11-08 23:18:16 +00001126 * At this point, the physical NAND Flash chips have been identified and
1127 * counted, so we know the physical geometry. This enables us to make some
1128 * important configuration decisions.
1129 *
Robert P. J. Day8d56db92016-07-15 13:44:45 -04001130 * The return value of this function propagates directly back to this driver's
Stefan Agner5883e552018-06-22 17:19:47 +02001131 * board_nand_init(). Anything other than zero will cause this driver to
Marek Vasut913a7252011-11-08 23:18:16 +00001132 * tear everything down and declare failure.
1133 */
Stefan Agner5883e552018-06-22 17:19:47 +02001134int mxs_nand_setup_ecc(struct mtd_info *mtd)
Marek Vasut913a7252011-11-08 23:18:16 +00001135{
Scott Wood17fed142016-05-30 13:57:56 -05001136 struct nand_chip *nand = mtd_to_nand(mtd);
1137 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Stefan Agnerd0778b32018-06-22 17:19:49 +02001138 struct bch_geometry *geo = &nand_info->bch_geometry;
Stefan Agnerdc8af6d2018-06-22 18:06:12 +02001139 struct mxs_bch_regs *bch_regs = nand_info->bch_regs;
Marek Vasut913a7252011-11-08 23:18:16 +00001140 uint32_t tmp;
Stefan Agneread66eb2018-06-22 18:06:18 +02001141 int ret;
Stefan Agner4d42ac12018-06-22 17:19:51 +02001142
Igor Opaniukc55401372019-11-03 16:49:43 +01001143 nand_info->en_randomizer = 0;
1144 nand_info->oobsize = mtd->oobsize;
1145 nand_info->writesize = mtd->writesize;
1146
Stefan Agneread66eb2018-06-22 18:06:18 +02001147 ret = mxs_nand_set_geometry(mtd, geo);
Stefan Agner4d42ac12018-06-22 17:19:51 +02001148 if (ret)
1149 return ret;
1150
Marek Vasut913a7252011-11-08 23:18:16 +00001151 /* Configure BCH and set NFC geometry */
Otavio Salvadorcbf0bf22012-08-13 09:53:12 +00001152 mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
Marek Vasut913a7252011-11-08 23:18:16 +00001153
1154 /* Configure layout 0 */
Stefan Agnerd0778b32018-06-22 17:19:49 +02001155 tmp = (geo->ecc_chunk_count - 1) << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
Marek Vasut913a7252011-11-08 23:18:16 +00001156 tmp |= MXS_NAND_METADATA_SIZE << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
Stefan Agnerd0778b32018-06-22 17:19:49 +02001157 tmp |= (geo->ecc_strength >> 1) << BCH_FLASHLAYOUT0_ECC0_OFFSET;
Ye Li94547442020-05-04 22:08:50 +08001158 tmp |= geo->ecc_chunk0_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
Stefan Agnerd0778b32018-06-22 17:19:49 +02001159 tmp |= (geo->gf_len == 14 ? 1 : 0) <<
Peng Fanc94f09d2015-07-21 16:15:19 +08001160 BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET;
Marek Vasut913a7252011-11-08 23:18:16 +00001161 writel(tmp, &bch_regs->hw_bch_flash0layout0);
Igor Opaniukc55401372019-11-03 16:49:43 +01001162 nand_info->bch_flash0layout0 = tmp;
Marek Vasut913a7252011-11-08 23:18:16 +00001163
1164 tmp = (mtd->writesize + mtd->oobsize)
1165 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
Stefan Agnerd0778b32018-06-22 17:19:49 +02001166 tmp |= (geo->ecc_strength >> 1) << BCH_FLASHLAYOUT1_ECCN_OFFSET;
Ye Li94547442020-05-04 22:08:50 +08001167 tmp |= geo->ecc_chunkn_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
Stefan Agnerd0778b32018-06-22 17:19:49 +02001168 tmp |= (geo->gf_len == 14 ? 1 : 0) <<
Peng Fanc94f09d2015-07-21 16:15:19 +08001169 BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
Marek Vasut913a7252011-11-08 23:18:16 +00001170 writel(tmp, &bch_regs->hw_bch_flash0layout1);
Igor Opaniukc55401372019-11-03 16:49:43 +01001171 nand_info->bch_flash0layout1 = tmp;
Marek Vasut913a7252011-11-08 23:18:16 +00001172
Peng Fan9e813732020-05-04 22:08:53 +08001173 /* Set erase threshold to ecc strength for mx6ul, mx6qp and mx7 */
1174 if (is_mx6dqp() || is_mx7() ||
Ye Li74948b82020-05-04 22:08:54 +08001175 is_mx6ul() || is_imx8m())
Peng Fan9e813732020-05-04 22:08:53 +08001176 writel(BCH_MODE_ERASE_THRESHOLD(geo->ecc_strength),
1177 &bch_regs->hw_bch_mode);
1178
Marek Vasut913a7252011-11-08 23:18:16 +00001179 /* Set *all* chip selects to use layout 0 */
1180 writel(0, &bch_regs->hw_bch_layoutselect);
1181
1182 /* Enable BCH complete interrupt */
1183 writel(BCH_CTRL_COMPLETE_IRQ_EN, &bch_regs->hw_bch_ctrl_set);
1184
1185 /* Hook some operations at the MTD level. */
Sergey Lapin3a38a552013-01-14 03:46:50 +00001186 if (mtd->_read_oob != mxs_nand_hook_read_oob) {
1187 nand_info->hooked_read_oob = mtd->_read_oob;
1188 mtd->_read_oob = mxs_nand_hook_read_oob;
Marek Vasut913a7252011-11-08 23:18:16 +00001189 }
1190
Sergey Lapin3a38a552013-01-14 03:46:50 +00001191 if (mtd->_write_oob != mxs_nand_hook_write_oob) {
1192 nand_info->hooked_write_oob = mtd->_write_oob;
1193 mtd->_write_oob = mxs_nand_hook_write_oob;
Marek Vasut913a7252011-11-08 23:18:16 +00001194 }
1195
Sergey Lapin3a38a552013-01-14 03:46:50 +00001196 if (mtd->_block_markbad != mxs_nand_hook_block_markbad) {
1197 nand_info->hooked_block_markbad = mtd->_block_markbad;
1198 mtd->_block_markbad = mxs_nand_hook_block_markbad;
Marek Vasut913a7252011-11-08 23:18:16 +00001199 }
1200
Stefan Agner5883e552018-06-22 17:19:47 +02001201 return 0;
Marek Vasut913a7252011-11-08 23:18:16 +00001202}
1203
1204/*
1205 * Allocate DMA buffers
1206 */
1207int mxs_nand_alloc_buffers(struct mxs_nand_info *nand_info)
1208{
1209 uint8_t *buf;
1210 const int size = NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE;
1211
Marek Vasut1b120e82012-03-15 18:33:19 +00001212 nand_info->data_buf_size = roundup(size, MXS_DMA_ALIGNMENT);
1213
Marek Vasut913a7252011-11-08 23:18:16 +00001214 /* DMA buffers */
Marek Vasut1b120e82012-03-15 18:33:19 +00001215 buf = memalign(MXS_DMA_ALIGNMENT, nand_info->data_buf_size);
Marek Vasut913a7252011-11-08 23:18:16 +00001216 if (!buf) {
1217 printf("MXS NAND: Error allocating DMA buffers\n");
1218 return -ENOMEM;
1219 }
1220
Marek Vasut1b120e82012-03-15 18:33:19 +00001221 memset(buf, 0, nand_info->data_buf_size);
Marek Vasut913a7252011-11-08 23:18:16 +00001222
1223 nand_info->data_buf = buf;
1224 nand_info->oob_buf = buf + NAND_MAX_PAGESIZE;
Marek Vasut913a7252011-11-08 23:18:16 +00001225 /* Command buffers */
1226 nand_info->cmd_buf = memalign(MXS_DMA_ALIGNMENT,
1227 MXS_NAND_COMMAND_BUFFER_SIZE);
1228 if (!nand_info->cmd_buf) {
1229 free(buf);
1230 printf("MXS NAND: Error allocating command buffers\n");
1231 return -ENOMEM;
1232 }
1233 memset(nand_info->cmd_buf, 0, MXS_NAND_COMMAND_BUFFER_SIZE);
1234 nand_info->cmd_queue_len = 0;
1235
1236 return 0;
1237}
1238
1239/*
1240 * Initializes the NFC hardware.
1241 */
Adam Ford6edb91a2019-01-12 06:25:48 -06001242static int mxs_nand_init_dma(struct mxs_nand_info *info)
Marek Vasut913a7252011-11-08 23:18:16 +00001243{
Peng Fane37d5a92016-01-27 10:38:02 +08001244 int i = 0, j, ret = 0;
Marek Vasut913a7252011-11-08 23:18:16 +00001245
1246 info->desc = malloc(sizeof(struct mxs_dma_desc *) *
1247 MXS_NAND_DMA_DESCRIPTOR_COUNT);
Peng Fane37d5a92016-01-27 10:38:02 +08001248 if (!info->desc) {
1249 ret = -ENOMEM;
Marek Vasut913a7252011-11-08 23:18:16 +00001250 goto err1;
Peng Fane37d5a92016-01-27 10:38:02 +08001251 }
Marek Vasut913a7252011-11-08 23:18:16 +00001252
1253 /* Allocate the DMA descriptors. */
1254 for (i = 0; i < MXS_NAND_DMA_DESCRIPTOR_COUNT; i++) {
1255 info->desc[i] = mxs_dma_desc_alloc();
Peng Fane37d5a92016-01-27 10:38:02 +08001256 if (!info->desc[i]) {
1257 ret = -ENOMEM;
Marek Vasut913a7252011-11-08 23:18:16 +00001258 goto err2;
Peng Fane37d5a92016-01-27 10:38:02 +08001259 }
Marek Vasut913a7252011-11-08 23:18:16 +00001260 }
1261
1262 /* Init the DMA controller. */
Fabio Estevam17156222017-06-29 09:33:44 -03001263 mxs_dma_init();
Marek Vasut93541b42012-04-08 17:34:46 +00001264 for (j = MXS_DMA_CHANNEL_AHB_APBH_GPMI0;
1265 j <= MXS_DMA_CHANNEL_AHB_APBH_GPMI7; j++) {
Peng Fane37d5a92016-01-27 10:38:02 +08001266 ret = mxs_dma_init_channel(j);
1267 if (ret)
Marek Vasut93541b42012-04-08 17:34:46 +00001268 goto err3;
1269 }
Marek Vasut913a7252011-11-08 23:18:16 +00001270
1271 /* Reset the GPMI block. */
Stefan Agnerdc8af6d2018-06-22 18:06:12 +02001272 mxs_reset_block(&info->gpmi_regs->hw_gpmi_ctrl0_reg);
1273 mxs_reset_block(&info->bch_regs->hw_bch_ctrl_reg);
Marek Vasut913a7252011-11-08 23:18:16 +00001274
1275 /*
1276 * Choose NAND mode, set IRQ polarity, disable write protection and
1277 * select BCH ECC.
1278 */
Stefan Agnerdc8af6d2018-06-22 18:06:12 +02001279 clrsetbits_le32(&info->gpmi_regs->hw_gpmi_ctrl1,
Marek Vasut913a7252011-11-08 23:18:16 +00001280 GPMI_CTRL1_GPMI_MODE,
1281 GPMI_CTRL1_ATA_IRQRDY_POLARITY | GPMI_CTRL1_DEV_RESET |
1282 GPMI_CTRL1_BCH_MODE);
1283
1284 return 0;
1285
Marek Vasut93541b42012-04-08 17:34:46 +00001286err3:
Peng Fane37d5a92016-01-27 10:38:02 +08001287 for (--j; j >= MXS_DMA_CHANNEL_AHB_APBH_GPMI0; j--)
Marek Vasut93541b42012-04-08 17:34:46 +00001288 mxs_dma_release(j);
Marek Vasut913a7252011-11-08 23:18:16 +00001289err2:
Marek Vasut913a7252011-11-08 23:18:16 +00001290 for (--i; i >= 0; i--)
1291 mxs_dma_desc_free(info->desc[i]);
Peng Fane37d5a92016-01-27 10:38:02 +08001292 free(info->desc);
1293err1:
1294 if (ret == -ENOMEM)
1295 printf("MXS NAND: Unable to allocate DMA descriptors\n");
1296 return ret;
Marek Vasut913a7252011-11-08 23:18:16 +00001297}
1298
Stefan Agner7152f342018-06-22 17:19:46 +02001299int mxs_nand_init_spl(struct nand_chip *nand)
1300{
1301 struct mxs_nand_info *nand_info;
1302 int err;
1303
1304 nand_info = malloc(sizeof(struct mxs_nand_info));
1305 if (!nand_info) {
1306 printf("MXS NAND: Failed to allocate private data\n");
1307 return -ENOMEM;
1308 }
1309 memset(nand_info, 0, sizeof(struct mxs_nand_info));
1310
Stefan Agnerdc8af6d2018-06-22 18:06:12 +02001311 nand_info->gpmi_regs = (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
1312 nand_info->bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
Adam Ford10210732019-01-02 20:36:52 -06001313
Ye Li74948b82020-05-04 22:08:54 +08001314 if (is_mx6sx() || is_mx7() || is_imx8m())
Adam Ford10210732019-01-02 20:36:52 -06001315 nand_info->max_ecc_strength_supported = 62;
1316 else
1317 nand_info->max_ecc_strength_supported = 40;
1318
Stefan Agner7152f342018-06-22 17:19:46 +02001319 err = mxs_nand_alloc_buffers(nand_info);
1320 if (err)
1321 return err;
1322
Stefan Agner00e65162018-06-22 18:06:13 +02001323 err = mxs_nand_init_dma(nand_info);
Stefan Agner7152f342018-06-22 17:19:46 +02001324 if (err)
1325 return err;
1326
1327 nand_set_controller_data(nand, nand_info);
1328
1329 nand->options |= NAND_NO_SUBPAGE_WRITE;
1330
1331 nand->cmd_ctrl = mxs_nand_cmd_ctrl;
1332 nand->dev_ready = mxs_nand_device_ready;
1333 nand->select_chip = mxs_nand_select_chip;
Stefan Agner7152f342018-06-22 17:19:46 +02001334
1335 nand->read_byte = mxs_nand_read_byte;
1336 nand->read_buf = mxs_nand_read_buf;
1337
1338 nand->ecc.read_page = mxs_nand_ecc_read_page;
1339
1340 nand->ecc.mode = NAND_ECC_HW;
Stefan Agner7152f342018-06-22 17:19:46 +02001341
1342 return 0;
1343}
1344
Stefan Agner19f90512018-06-22 18:06:16 +02001345int mxs_nand_init_ctrl(struct mxs_nand_info *nand_info)
Marek Vasut913a7252011-11-08 23:18:16 +00001346{
Stefan Agner5883e552018-06-22 17:19:47 +02001347 struct mtd_info *mtd;
Stefan Agner5883e552018-06-22 17:19:47 +02001348 struct nand_chip *nand;
Marek Vasut913a7252011-11-08 23:18:16 +00001349 int err;
1350
Stefan Agner5883e552018-06-22 17:19:47 +02001351 nand = &nand_info->chip;
1352 mtd = nand_to_mtd(nand);
Marek Vasut913a7252011-11-08 23:18:16 +00001353 err = mxs_nand_alloc_buffers(nand_info);
1354 if (err)
Stefan Agner404b1102018-06-22 18:06:14 +02001355 return err;
Marek Vasut913a7252011-11-08 23:18:16 +00001356
Stefan Agner00e65162018-06-22 18:06:13 +02001357 err = mxs_nand_init_dma(nand_info);
Marek Vasut913a7252011-11-08 23:18:16 +00001358 if (err)
Stefan Agner404b1102018-06-22 18:06:14 +02001359 goto err_free_buffers;
Marek Vasut913a7252011-11-08 23:18:16 +00001360
1361 memset(&fake_ecc_layout, 0, sizeof(fake_ecc_layout));
1362
Stefan Agner95f376f2018-06-22 17:19:48 +02001363#ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1364 nand->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
1365#endif
1366
Scott Wood17fed142016-05-30 13:57:56 -05001367 nand_set_controller_data(nand, nand_info);
Marek Vasut913a7252011-11-08 23:18:16 +00001368 nand->options |= NAND_NO_SUBPAGE_WRITE;
1369
Stefan Agner150ddbc2018-06-22 18:06:17 +02001370 if (nand_info->dev)
1371 nand->flash_node = dev_of_offset(nand_info->dev);
1372
Marek Vasut913a7252011-11-08 23:18:16 +00001373 nand->cmd_ctrl = mxs_nand_cmd_ctrl;
1374
1375 nand->dev_ready = mxs_nand_device_ready;
1376 nand->select_chip = mxs_nand_select_chip;
1377 nand->block_bad = mxs_nand_block_bad;
Marek Vasut913a7252011-11-08 23:18:16 +00001378
1379 nand->read_byte = mxs_nand_read_byte;
1380
1381 nand->read_buf = mxs_nand_read_buf;
1382 nand->write_buf = mxs_nand_write_buf;
1383
Stefan Agner5883e552018-06-22 17:19:47 +02001384 /* first scan to find the device and get the page size */
1385 if (nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_DEVICE, NULL))
Stefan Agner404b1102018-06-22 18:06:14 +02001386 goto err_free_buffers;
Stefan Agner5883e552018-06-22 17:19:47 +02001387
1388 if (mxs_nand_setup_ecc(mtd))
Stefan Agner404b1102018-06-22 18:06:14 +02001389 goto err_free_buffers;
Stefan Agner5883e552018-06-22 17:19:47 +02001390
Marek Vasut913a7252011-11-08 23:18:16 +00001391 nand->ecc.read_page = mxs_nand_ecc_read_page;
1392 nand->ecc.write_page = mxs_nand_ecc_write_page;
1393 nand->ecc.read_oob = mxs_nand_ecc_read_oob;
1394 nand->ecc.write_oob = mxs_nand_ecc_write_oob;
1395
1396 nand->ecc.layout = &fake_ecc_layout;
1397 nand->ecc.mode = NAND_ECC_HW;
Ye Li94547442020-05-04 22:08:50 +08001398 nand->ecc.size = nand_info->bch_geometry.ecc_chunkn_size;
Stefan Agner72d627d2018-06-22 17:19:50 +02001399 nand->ecc.strength = nand_info->bch_geometry.ecc_strength;
Marek Vasut913a7252011-11-08 23:18:16 +00001400
Stefan Agner5883e552018-06-22 17:19:47 +02001401 /* second phase scan */
1402 err = nand_scan_tail(mtd);
1403 if (err)
Stefan Agner404b1102018-06-22 18:06:14 +02001404 goto err_free_buffers;
Stefan Agner5883e552018-06-22 17:19:47 +02001405
1406 err = nand_register(0, mtd);
1407 if (err)
Stefan Agner404b1102018-06-22 18:06:14 +02001408 goto err_free_buffers;
Stefan Agner5883e552018-06-22 17:19:47 +02001409
Stefan Agner404b1102018-06-22 18:06:14 +02001410 return 0;
Marek Vasut913a7252011-11-08 23:18:16 +00001411
Stefan Agner404b1102018-06-22 18:06:14 +02001412err_free_buffers:
Marek Vasut913a7252011-11-08 23:18:16 +00001413 free(nand_info->data_buf);
1414 free(nand_info->cmd_buf);
Stefan Agner404b1102018-06-22 18:06:14 +02001415
1416 return err;
1417}
1418
Stefan Agner150ddbc2018-06-22 18:06:17 +02001419#ifndef CONFIG_NAND_MXS_DT
Stefan Agner404b1102018-06-22 18:06:14 +02001420void board_nand_init(void)
1421{
1422 struct mxs_nand_info *nand_info;
1423
1424 nand_info = malloc(sizeof(struct mxs_nand_info));
1425 if (!nand_info) {
1426 printf("MXS NAND: Failed to allocate private data\n");
1427 return;
1428 }
1429 memset(nand_info, 0, sizeof(struct mxs_nand_info));
1430
1431 nand_info->gpmi_regs = (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
1432 nand_info->bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1433
Stefan Agner4dc98db2018-06-22 18:06:15 +02001434 /* Refer to Chapter 17 for i.MX6DQ, Chapter 18 for i.MX6SX */
1435 if (is_mx6sx() || is_mx7())
1436 nand_info->max_ecc_strength_supported = 62;
1437 else
1438 nand_info->max_ecc_strength_supported = 40;
1439
1440#ifdef CONFIG_NAND_MXS_USE_MINIMUM_ECC
1441 nand_info->use_minimum_ecc = true;
1442#endif
1443
Stefan Agner19f90512018-06-22 18:06:16 +02001444 if (mxs_nand_init_ctrl(nand_info) < 0)
Stefan Agner404b1102018-06-22 18:06:14 +02001445 goto err;
1446
Stefan Agner5883e552018-06-22 17:19:47 +02001447 return;
Stefan Agner404b1102018-06-22 18:06:14 +02001448
1449err:
1450 free(nand_info);
Marek Vasut913a7252011-11-08 23:18:16 +00001451}
Stefan Agner150ddbc2018-06-22 18:06:17 +02001452#endif
Igor Opaniukc55401372019-11-03 16:49:43 +01001453
1454/*
1455 * Read NAND layout for FCB block generation.
1456 */
1457void mxs_nand_get_layout(struct mtd_info *mtd, struct mxs_nand_layout *l)
1458{
1459 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1460 u32 tmp;
1461
1462 tmp = readl(&bch_regs->hw_bch_flash0layout0);
1463 l->nblocks = (tmp & BCH_FLASHLAYOUT0_NBLOCKS_MASK) >>
1464 BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1465 l->meta_size = (tmp & BCH_FLASHLAYOUT0_META_SIZE_MASK) >>
1466 BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1467
1468 tmp = readl(&bch_regs->hw_bch_flash0layout1);
1469 l->data0_size = 4 * ((tmp & BCH_FLASHLAYOUT0_DATA0_SIZE_MASK) >>
1470 BCH_FLASHLAYOUT0_DATA0_SIZE_OFFSET);
1471 l->ecc0 = (tmp & BCH_FLASHLAYOUT0_ECC0_MASK) >>
1472 BCH_FLASHLAYOUT0_ECC0_OFFSET;
1473 l->datan_size = 4 * ((tmp & BCH_FLASHLAYOUT1_DATAN_SIZE_MASK) >>
1474 BCH_FLASHLAYOUT1_DATAN_SIZE_OFFSET);
1475 l->eccn = (tmp & BCH_FLASHLAYOUT1_ECCN_MASK) >>
1476 BCH_FLASHLAYOUT1_ECCN_OFFSET;
1477}
1478
1479/*
1480 * Set BCH to specific layout used by ROM bootloader to read FCB.
1481 */
1482void mxs_nand_mode_fcb(struct mtd_info *mtd)
1483{
1484 u32 tmp;
1485 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1486 struct nand_chip *nand = mtd_to_nand(mtd);
1487 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1488
1489 nand_info->en_randomizer = 1;
1490
1491 mtd->writesize = 1024;
1492 mtd->oobsize = 1862 - 1024;
1493
1494 /* 8 ecc_chunks_*/
1495 tmp = 7 << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1496 /* 32 bytes for metadata */
1497 tmp |= 32 << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1498 /* using ECC62 level to be performed */
1499 tmp |= 0x1F << BCH_FLASHLAYOUT0_ECC0_OFFSET;
1500 /* 0x20 * 4 bytes of the data0 block */
1501 tmp |= 0x20 << BCH_FLASHLAYOUT0_DATA0_SIZE_OFFSET;
1502 tmp |= 0 << BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET;
1503 writel(tmp, &bch_regs->hw_bch_flash0layout0);
1504
1505 /* 1024 for data + 838 for OOB */
1506 tmp = 1862 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
1507 /* using ECC62 level to be performed */
1508 tmp |= 0x1F << BCH_FLASHLAYOUT1_ECCN_OFFSET;
1509 /* 0x20 * 4 bytes of the data0 block */
1510 tmp |= 0x20 << BCH_FLASHLAYOUT1_DATAN_SIZE_OFFSET;
1511 tmp |= 0 << BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
1512 writel(tmp, &bch_regs->hw_bch_flash0layout1);
1513}
1514
1515/*
1516 * Restore BCH to normal settings.
1517 */
1518void mxs_nand_mode_normal(struct mtd_info *mtd)
1519{
1520 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1521 struct nand_chip *nand = mtd_to_nand(mtd);
1522 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1523
1524 nand_info->en_randomizer = 0;
1525
1526 mtd->writesize = nand_info->writesize;
1527 mtd->oobsize = nand_info->oobsize;
1528
1529 writel(nand_info->bch_flash0layout0, &bch_regs->hw_bch_flash0layout0);
1530 writel(nand_info->bch_flash0layout1, &bch_regs->hw_bch_flash0layout1);
1531}
1532
1533uint32_t mxs_nand_mark_byte_offset(struct mtd_info *mtd)
1534{
1535 struct nand_chip *chip = mtd_to_nand(mtd);
1536 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
1537 struct bch_geometry *geo = &nand_info->bch_geometry;
1538
1539 return geo->block_mark_byte_offset;
1540}
1541
1542uint32_t mxs_nand_mark_bit_offset(struct mtd_info *mtd)
1543{
1544 struct nand_chip *chip = mtd_to_nand(mtd);
1545 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
1546 struct bch_geometry *geo = &nand_info->bch_geometry;
1547
1548 return geo->block_mark_bit_offset;
1549}