blob: 8da59e39c0188debec13b85fc6f2301ce0e0e54f [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.
Marek Vasut913a7252011-11-08 23:18:16 +000013 */
14
Tom Warrenc88d30f2012-09-10 08:47:51 -070015#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070016#include <cpu_func.h>
Stefan Agner19f90512018-06-22 18:06:16 +020017#include <dm.h>
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090018#include <linux/mtd/rawnand.h>
Stefan Agner4d42ac12018-06-22 17:19:51 +020019#include <linux/sizes.h>
Marek Vasut913a7252011-11-08 23:18:16 +000020#include <linux/types.h>
Marek Vasut913a7252011-11-08 23:18:16 +000021#include <malloc.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090022#include <linux/errno.h>
Marek Vasut913a7252011-11-08 23:18:16 +000023#include <asm/io.h>
24#include <asm/arch/clock.h>
25#include <asm/arch/imx-regs.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020026#include <asm/mach-imx/regs-bch.h>
27#include <asm/mach-imx/regs-gpmi.h>
Marek Vasut913a7252011-11-08 23:18:16 +000028#include <asm/arch/sys_proto.h>
Shyam Sainif63ef492019-06-14 13:05:33 +053029#include <mxs_nand.h>
Marek Vasut913a7252011-11-08 23:18:16 +000030
31#define MXS_NAND_DMA_DESCRIPTOR_COUNT 4
32
Peng Fan007c8da2015-12-22 17:04:23 +080033#if (defined(CONFIG_MX6) || defined(CONFIG_MX7))
Stefan Roese8338d1d2013-04-15 21:14:12 +000034#define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT 2
35#else
36#define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT 0
37#endif
Marek Vasut913a7252011-11-08 23:18:16 +000038#define MXS_NAND_METADATA_SIZE 10
Jörg Krause1d870262015-04-15 09:27:22 +020039#define MXS_NAND_BITS_PER_ECC_LEVEL 13
Stefan Agner54bf8082016-08-01 23:55:18 -070040
41#if !defined(CONFIG_SYS_CACHELINE_SIZE) || CONFIG_SYS_CACHELINE_SIZE < 32
Marek Vasut913a7252011-11-08 23:18:16 +000042#define MXS_NAND_COMMAND_BUFFER_SIZE 32
Stefan Agner54bf8082016-08-01 23:55:18 -070043#else
44#define MXS_NAND_COMMAND_BUFFER_SIZE CONFIG_SYS_CACHELINE_SIZE
45#endif
Marek Vasut913a7252011-11-08 23:18:16 +000046
47#define MXS_NAND_BCH_TIMEOUT 10000
48
Marek Vasut913a7252011-11-08 23:18:16 +000049struct nand_ecclayout fake_ecc_layout;
50
Marek Vasut1b120e82012-03-15 18:33:19 +000051/*
52 * Cache management functions
53 */
Trevor Woerner43ec7e02019-05-03 09:41:00 -040054#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Marek Vasut1b120e82012-03-15 18:33:19 +000055static void mxs_nand_flush_data_buf(struct mxs_nand_info *info)
56{
57 uint32_t addr = (uint32_t)info->data_buf;
58
59 flush_dcache_range(addr, addr + info->data_buf_size);
60}
61
62static void mxs_nand_inval_data_buf(struct mxs_nand_info *info)
63{
64 uint32_t addr = (uint32_t)info->data_buf;
65
66 invalidate_dcache_range(addr, addr + info->data_buf_size);
67}
68
69static void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info)
70{
71 uint32_t addr = (uint32_t)info->cmd_buf;
72
73 flush_dcache_range(addr, addr + MXS_NAND_COMMAND_BUFFER_SIZE);
74}
75#else
76static inline void mxs_nand_flush_data_buf(struct mxs_nand_info *info) {}
77static inline void mxs_nand_inval_data_buf(struct mxs_nand_info *info) {}
78static inline void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info) {}
79#endif
80
Marek Vasut913a7252011-11-08 23:18:16 +000081static struct mxs_dma_desc *mxs_nand_get_dma_desc(struct mxs_nand_info *info)
82{
83 struct mxs_dma_desc *desc;
84
85 if (info->desc_index >= MXS_NAND_DMA_DESCRIPTOR_COUNT) {
86 printf("MXS NAND: Too many DMA descriptors requested\n");
87 return NULL;
88 }
89
90 desc = info->desc[info->desc_index];
91 info->desc_index++;
92
93 return desc;
94}
95
96static void mxs_nand_return_dma_descs(struct mxs_nand_info *info)
97{
98 int i;
99 struct mxs_dma_desc *desc;
100
101 for (i = 0; i < info->desc_index; i++) {
102 desc = info->desc[i];
103 memset(desc, 0, sizeof(struct mxs_dma_desc));
104 desc->address = (dma_addr_t)desc;
105 }
106
107 info->desc_index = 0;
108}
109
Marek Vasut913a7252011-11-08 23:18:16 +0000110static uint32_t mxs_nand_aux_status_offset(void)
111{
112 return (MXS_NAND_METADATA_SIZE + 0x3) & ~0x3;
113}
114
Ye Li94547442020-05-04 22:08:50 +0800115static inline bool mxs_nand_bbm_in_data_chunk(struct bch_geometry *geo, struct mtd_info *mtd,
116 unsigned int *chunk_num)
Marek Vasut913a7252011-11-08 23:18:16 +0000117{
Ye Li94547442020-05-04 22:08:50 +0800118 unsigned int i, j;
Marek Vasut913a7252011-11-08 23:18:16 +0000119
Ye Li94547442020-05-04 22:08:50 +0800120 if (geo->ecc_chunk0_size != geo->ecc_chunkn_size) {
121 dev_err(this->dev, "The size of chunk0 must equal to chunkn\n");
122 return false;
123 }
Marek Vasut913a7252011-11-08 23:18:16 +0000124
Ye Li94547442020-05-04 22:08:50 +0800125 i = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8) /
126 (geo->gf_len * geo->ecc_strength +
127 geo->ecc_chunkn_size * 8);
Marek Vasut913a7252011-11-08 23:18:16 +0000128
Ye Li94547442020-05-04 22:08:50 +0800129 j = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8) -
130 (geo->gf_len * geo->ecc_strength +
131 geo->ecc_chunkn_size * 8) * i;
Marek Vasut913a7252011-11-08 23:18:16 +0000132
Ye Li94547442020-05-04 22:08:50 +0800133 if (j < geo->ecc_chunkn_size * 8) {
134 *chunk_num = i + 1;
135 dev_dbg(this->dev, "Set ecc to %d and bbm in chunk %d\n",
136 geo->ecc_strength, *chunk_num);
137 return true;
138 }
Marek Vasut913a7252011-11-08 23:18:16 +0000139
Ye Li94547442020-05-04 22:08:50 +0800140 return false;
Marek Vasut913a7252011-11-08 23:18:16 +0000141}
142
Stefan Agner4d42ac12018-06-22 17:19:51 +0200143static inline int mxs_nand_calc_ecc_layout_by_info(struct bch_geometry *geo,
Stefan Agneread66eb2018-06-22 18:06:18 +0200144 struct mtd_info *mtd,
145 unsigned int ecc_strength,
146 unsigned int ecc_step)
Stefan Agner4d42ac12018-06-22 17:19:51 +0200147{
148 struct nand_chip *chip = mtd_to_nand(mtd);
Stefan Agner4dc98db2018-06-22 18:06:15 +0200149 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Ye Li94547442020-05-04 22:08:50 +0800150 unsigned int block_mark_bit_offset;
Stefan Agner4d42ac12018-06-22 17:19:51 +0200151
Stefan Agneread66eb2018-06-22 18:06:18 +0200152 switch (ecc_step) {
Stefan Agner4d42ac12018-06-22 17:19:51 +0200153 case SZ_512:
154 geo->gf_len = 13;
155 break;
156 case SZ_1K:
157 geo->gf_len = 14;
158 break;
159 default:
160 return -EINVAL;
161 }
162
Ye Li94547442020-05-04 22:08:50 +0800163 geo->ecc_chunk0_size = ecc_step;
164 geo->ecc_chunkn_size = ecc_step;
Stefan Agneread66eb2018-06-22 18:06:18 +0200165 geo->ecc_strength = round_up(ecc_strength, 2);
Stefan Agner4d42ac12018-06-22 17:19:51 +0200166
167 /* Keep the C >= O */
Ye Li94547442020-05-04 22:08:50 +0800168 if (geo->ecc_chunkn_size < mtd->oobsize)
Stefan Agner4d42ac12018-06-22 17:19:51 +0200169 return -EINVAL;
170
Stefan Agner4dc98db2018-06-22 18:06:15 +0200171 if (geo->ecc_strength > nand_info->max_ecc_strength_supported)
Stefan Agner4d42ac12018-06-22 17:19:51 +0200172 return -EINVAL;
173
Ye Li94547442020-05-04 22:08:50 +0800174 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size;
175
176 /* For bit swap. */
177 block_mark_bit_offset = mtd->writesize * 8 -
178 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
179 + MXS_NAND_METADATA_SIZE * 8);
180
181 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
182 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
Stefan Agner4d42ac12018-06-22 17:19:51 +0200183
184 return 0;
185}
186
Ye Li94547442020-05-04 22:08:50 +0800187static inline int mxs_nand_legacy_calc_ecc_layout(struct bch_geometry *geo,
Stefan Agnerd0778b32018-06-22 17:19:49 +0200188 struct mtd_info *mtd)
Marek Vasut913a7252011-11-08 23:18:16 +0000189{
Stefan Agner4dc98db2018-06-22 18:06:15 +0200190 struct nand_chip *chip = mtd_to_nand(mtd);
191 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Ye Li94547442020-05-04 22:08:50 +0800192 unsigned int block_mark_bit_offset;
Stefan Agner4dc98db2018-06-22 18:06:15 +0200193
Stefan Agnerd0778b32018-06-22 17:19:49 +0200194 /* The default for the length of Galois Field. */
195 geo->gf_len = 13;
196
197 /* The default for chunk size. */
Ye Li94547442020-05-04 22:08:50 +0800198 geo->ecc_chunk0_size = 512;
199 geo->ecc_chunkn_size = 512;
Stefan Agnerd0778b32018-06-22 17:19:49 +0200200
Ye Li94547442020-05-04 22:08:50 +0800201 if (geo->ecc_chunkn_size < mtd->oobsize) {
Stefan Agnerd0778b32018-06-22 17:19:49 +0200202 geo->gf_len = 14;
Ye Li94547442020-05-04 22:08:50 +0800203 geo->ecc_chunk0_size *= 2;
204 geo->ecc_chunkn_size *= 2;
Stefan Agnerd0778b32018-06-22 17:19:49 +0200205 }
206
Ye Li94547442020-05-04 22:08:50 +0800207 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size;
Stefan Agnerd0778b32018-06-22 17:19:49 +0200208
Stefan Agnerd0778b32018-06-22 17:19:49 +0200209 /*
210 * Determine the ECC layout with the formula:
211 * ECC bits per chunk = (total page spare data bits) /
212 * (bits per ECC level) / (chunks per page)
213 * where:
214 * total page spare data bits =
215 * (page oob size - meta data size) * (bits per byte)
216 */
217 geo->ecc_strength = ((mtd->oobsize - MXS_NAND_METADATA_SIZE) * 8)
218 / (geo->gf_len * geo->ecc_chunk_count);
219
Stefan Agner4d42ac12018-06-22 17:19:51 +0200220 geo->ecc_strength = min(round_down(geo->ecc_strength, 2),
Stefan Agner4dc98db2018-06-22 18:06:15 +0200221 nand_info->max_ecc_strength_supported);
Stefan Agnerd0778b32018-06-22 17:19:49 +0200222
Ye Li94547442020-05-04 22:08:50 +0800223 block_mark_bit_offset = mtd->writesize * 8 -
224 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
225 + MXS_NAND_METADATA_SIZE * 8);
226
227 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
228 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
229
230 return 0;
231}
232
233static inline int mxs_nand_calc_ecc_for_large_oob(struct bch_geometry *geo,
234 struct mtd_info *mtd)
235{
236 struct nand_chip *chip = mtd_to_nand(mtd);
237 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
238 unsigned int block_mark_bit_offset;
239 unsigned int max_ecc;
240 unsigned int bbm_chunk;
241 unsigned int i;
242
243 /* sanity check for the minimum ecc nand required */
244 if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
245 return -EINVAL;
246 geo->ecc_strength = chip->ecc_strength_ds;
247
248 /* calculate the maximum ecc platform can support*/
249 geo->gf_len = 14;
250 geo->ecc_chunk0_size = 1024;
251 geo->ecc_chunkn_size = 1024;
252 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size;
253 max_ecc = ((mtd->oobsize - MXS_NAND_METADATA_SIZE) * 8)
254 / (geo->gf_len * geo->ecc_chunk_count);
255 max_ecc = min(round_down(max_ecc, 2),
256 nand_info->max_ecc_strength_supported);
257
258
259 /* search a supported ecc strength that makes bbm */
260 /* located in data chunk */
261 geo->ecc_strength = chip->ecc_strength_ds;
262 while (!(geo->ecc_strength > max_ecc)) {
263 if (mxs_nand_bbm_in_data_chunk(geo, mtd, &bbm_chunk))
264 break;
265 geo->ecc_strength += 2;
266 }
267
268 /* if none of them works, keep using the minimum ecc */
269 /* nand required but changing ecc page layout */
270 if (geo->ecc_strength > max_ecc) {
271 geo->ecc_strength = chip->ecc_strength_ds;
272 /* add extra ecc for meta data */
273 geo->ecc_chunk0_size = 0;
274 geo->ecc_chunk_count = (mtd->writesize / geo->ecc_chunkn_size) + 1;
275 geo->ecc_for_meta = 1;
276 /* check if oob can afford this extra ecc chunk */
277 if (mtd->oobsize * 8 < MXS_NAND_METADATA_SIZE * 8 +
278 geo->gf_len * geo->ecc_strength
279 * geo->ecc_chunk_count) {
280 printf("unsupported NAND chip with new layout\n");
281 return -EINVAL;
282 }
283
284 /* calculate in which chunk bbm located */
285 bbm_chunk = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8 -
286 geo->gf_len * geo->ecc_strength) /
287 (geo->gf_len * geo->ecc_strength +
288 geo->ecc_chunkn_size * 8) + 1;
289 }
290
291 /* calculate the number of ecc chunk behind the bbm */
292 i = (mtd->writesize / geo->ecc_chunkn_size) - bbm_chunk + 1;
293
294 block_mark_bit_offset = mtd->writesize * 8 -
295 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - i)
296 + MXS_NAND_METADATA_SIZE * 8);
297
298 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
299 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
300
Stefan Agnerd0778b32018-06-22 17:19:49 +0200301 return 0;
Marek Vasut913a7252011-11-08 23:18:16 +0000302}
303
304/*
305 * Wait for BCH complete IRQ and clear the IRQ
306 */
Stefan Agnerdc8af6d2018-06-22 18:06:12 +0200307static int mxs_nand_wait_for_bch_complete(struct mxs_nand_info *nand_info)
Marek Vasut913a7252011-11-08 23:18:16 +0000308{
Marek Vasut913a7252011-11-08 23:18:16 +0000309 int timeout = MXS_NAND_BCH_TIMEOUT;
310 int ret;
311
Stefan Agnerdc8af6d2018-06-22 18:06:12 +0200312 ret = mxs_wait_mask_set(&nand_info->bch_regs->hw_bch_ctrl_reg,
Marek Vasut913a7252011-11-08 23:18:16 +0000313 BCH_CTRL_COMPLETE_IRQ, timeout);
314
Stefan Agnerdc8af6d2018-06-22 18:06:12 +0200315 writel(BCH_CTRL_COMPLETE_IRQ, &nand_info->bch_regs->hw_bch_ctrl_clr);
Marek Vasut913a7252011-11-08 23:18:16 +0000316
317 return ret;
318}
319
320/*
321 * This is the function that we install in the cmd_ctrl function pointer of the
322 * owning struct nand_chip. The only functions in the reference implementation
323 * that use these functions pointers are cmdfunc and select_chip.
324 *
325 * In this driver, we implement our own select_chip, so this function will only
326 * be called by the reference implementation's cmdfunc. For this reason, we can
327 * ignore the chip enable bit and concentrate only on sending bytes to the NAND
328 * Flash.
329 */
330static void mxs_nand_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
331{
Scott Wood17fed142016-05-30 13:57:56 -0500332 struct nand_chip *nand = mtd_to_nand(mtd);
333 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut913a7252011-11-08 23:18:16 +0000334 struct mxs_dma_desc *d;
335 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
336 int ret;
337
338 /*
339 * If this condition is true, something is _VERY_ wrong in MTD
340 * subsystem!
341 */
342 if (nand_info->cmd_queue_len == MXS_NAND_COMMAND_BUFFER_SIZE) {
343 printf("MXS NAND: Command queue too long\n");
344 return;
345 }
346
347 /*
348 * Every operation begins with a command byte and a series of zero or
349 * more address bytes. These are distinguished by either the Address
350 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
351 * asserted. When MTD is ready to execute the command, it will
352 * deasert both latch enables.
353 *
354 * Rather than run a separate DMA operation for every single byte, we
355 * queue them up and run a single DMA operation for the entire series
356 * of command and data bytes.
357 */
358 if (ctrl & (NAND_ALE | NAND_CLE)) {
359 if (data != NAND_CMD_NONE)
360 nand_info->cmd_buf[nand_info->cmd_queue_len++] = data;
361 return;
362 }
363
364 /*
365 * If control arrives here, MTD has deasserted both the ALE and CLE,
366 * which means it's ready to run an operation. Check if we have any
367 * bytes to send.
368 */
369 if (nand_info->cmd_queue_len == 0)
370 return;
371
372 /* Compile the DMA descriptor -- a descriptor that sends command. */
373 d = mxs_nand_get_dma_desc(nand_info);
374 d->cmd.data =
375 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
376 MXS_DMA_DESC_CHAIN | MXS_DMA_DESC_DEC_SEM |
377 MXS_DMA_DESC_WAIT4END | (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
378 (nand_info->cmd_queue_len << MXS_DMA_DESC_BYTES_OFFSET);
379
380 d->cmd.address = (dma_addr_t)nand_info->cmd_buf;
381
382 d->cmd.pio_words[0] =
383 GPMI_CTRL0_COMMAND_MODE_WRITE |
384 GPMI_CTRL0_WORD_LENGTH |
385 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
386 GPMI_CTRL0_ADDRESS_NAND_CLE |
387 GPMI_CTRL0_ADDRESS_INCREMENT |
388 nand_info->cmd_queue_len;
389
390 mxs_dma_desc_append(channel, d);
391
Marek Vasut1b120e82012-03-15 18:33:19 +0000392 /* Flush caches */
393 mxs_nand_flush_cmd_buf(nand_info);
394
Marek Vasut913a7252011-11-08 23:18:16 +0000395 /* Execute the DMA chain. */
396 ret = mxs_dma_go(channel);
397 if (ret)
398 printf("MXS NAND: Error sending command\n");
399
400 mxs_nand_return_dma_descs(nand_info);
401
402 /* Reset the command queue. */
403 nand_info->cmd_queue_len = 0;
404}
405
406/*
407 * Test if the NAND flash is ready.
408 */
409static int mxs_nand_device_ready(struct mtd_info *mtd)
410{
Scott Wood17fed142016-05-30 13:57:56 -0500411 struct nand_chip *chip = mtd_to_nand(mtd);
412 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Marek Vasut913a7252011-11-08 23:18:16 +0000413 uint32_t tmp;
414
Stefan Agnerdc8af6d2018-06-22 18:06:12 +0200415 tmp = readl(&nand_info->gpmi_regs->hw_gpmi_stat);
Marek Vasut913a7252011-11-08 23:18:16 +0000416 tmp >>= (GPMI_STAT_READY_BUSY_OFFSET + nand_info->cur_chip);
417
418 return tmp & 1;
419}
420
421/*
422 * Select the NAND chip.
423 */
424static void mxs_nand_select_chip(struct mtd_info *mtd, int chip)
425{
Scott Wood17fed142016-05-30 13:57:56 -0500426 struct nand_chip *nand = mtd_to_nand(mtd);
427 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut913a7252011-11-08 23:18:16 +0000428
429 nand_info->cur_chip = chip;
430}
431
432/*
433 * Handle block mark swapping.
434 *
435 * Note that, when this function is called, it doesn't know whether it's
436 * swapping the block mark, or swapping it *back* -- but it doesn't matter
437 * because the the operation is the same.
438 */
Stefan Agnerd0778b32018-06-22 17:19:49 +0200439static void mxs_nand_swap_block_mark(struct bch_geometry *geo,
440 uint8_t *data_buf, uint8_t *oob_buf)
Marek Vasut913a7252011-11-08 23:18:16 +0000441{
Stefan Agnerd0778b32018-06-22 17:19:49 +0200442 uint32_t bit_offset = geo->block_mark_bit_offset;
443 uint32_t buf_offset = geo->block_mark_byte_offset;
Marek Vasut913a7252011-11-08 23:18:16 +0000444
445 uint32_t src;
446 uint32_t dst;
447
Marek Vasut913a7252011-11-08 23:18:16 +0000448 /*
449 * Get the byte from the data area that overlays the block mark. Since
450 * the ECC engine applies its own view to the bits in the page, the
451 * physical block mark won't (in general) appear on a byte boundary in
452 * the data.
453 */
454 src = data_buf[buf_offset] >> bit_offset;
455 src |= data_buf[buf_offset + 1] << (8 - bit_offset);
456
457 dst = oob_buf[0];
458
459 oob_buf[0] = src;
460
461 data_buf[buf_offset] &= ~(0xff << bit_offset);
462 data_buf[buf_offset + 1] &= 0xff << bit_offset;
463
464 data_buf[buf_offset] |= dst << bit_offset;
465 data_buf[buf_offset + 1] |= dst >> (8 - bit_offset);
466}
467
468/*
469 * Read data from NAND.
470 */
471static void mxs_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int length)
472{
Scott Wood17fed142016-05-30 13:57:56 -0500473 struct nand_chip *nand = mtd_to_nand(mtd);
474 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut913a7252011-11-08 23:18:16 +0000475 struct mxs_dma_desc *d;
476 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
477 int ret;
478
479 if (length > NAND_MAX_PAGESIZE) {
480 printf("MXS NAND: DMA buffer too big\n");
481 return;
482 }
483
484 if (!buf) {
485 printf("MXS NAND: DMA buffer is NULL\n");
486 return;
487 }
488
489 /* Compile the DMA descriptor - a descriptor that reads data. */
490 d = mxs_nand_get_dma_desc(nand_info);
491 d->cmd.data =
492 MXS_DMA_DESC_COMMAND_DMA_WRITE | MXS_DMA_DESC_IRQ |
493 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
494 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
495 (length << MXS_DMA_DESC_BYTES_OFFSET);
496
497 d->cmd.address = (dma_addr_t)nand_info->data_buf;
498
499 d->cmd.pio_words[0] =
500 GPMI_CTRL0_COMMAND_MODE_READ |
501 GPMI_CTRL0_WORD_LENGTH |
502 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
503 GPMI_CTRL0_ADDRESS_NAND_DATA |
504 length;
505
506 mxs_dma_desc_append(channel, d);
507
508 /*
509 * A DMA descriptor that waits for the command to end and the chip to
510 * become ready.
511 *
512 * I think we actually should *not* be waiting for the chip to become
513 * ready because, after all, we don't care. I think the original code
514 * did that and no one has re-thought it yet.
515 */
516 d = mxs_nand_get_dma_desc(nand_info);
517 d->cmd.data =
518 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
519 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_DEC_SEM |
Luca Ellero80f06b82014-12-16 15:36:14 +0100520 MXS_DMA_DESC_WAIT4END | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
Marek Vasut913a7252011-11-08 23:18:16 +0000521
522 d->cmd.address = 0;
523
524 d->cmd.pio_words[0] =
525 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
526 GPMI_CTRL0_WORD_LENGTH |
527 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
528 GPMI_CTRL0_ADDRESS_NAND_DATA;
529
530 mxs_dma_desc_append(channel, d);
531
Peng Fane3bbfb72015-07-21 16:15:21 +0800532 /* Invalidate caches */
533 mxs_nand_inval_data_buf(nand_info);
534
Marek Vasut913a7252011-11-08 23:18:16 +0000535 /* Execute the DMA chain. */
536 ret = mxs_dma_go(channel);
537 if (ret) {
538 printf("MXS NAND: DMA read error\n");
539 goto rtn;
540 }
541
Marek Vasut1b120e82012-03-15 18:33:19 +0000542 /* Invalidate caches */
543 mxs_nand_inval_data_buf(nand_info);
544
Marek Vasut913a7252011-11-08 23:18:16 +0000545 memcpy(buf, nand_info->data_buf, length);
546
547rtn:
548 mxs_nand_return_dma_descs(nand_info);
549}
550
551/*
552 * Write data to NAND.
553 */
554static void mxs_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
555 int length)
556{
Scott Wood17fed142016-05-30 13:57:56 -0500557 struct nand_chip *nand = mtd_to_nand(mtd);
558 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut913a7252011-11-08 23:18:16 +0000559 struct mxs_dma_desc *d;
560 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
561 int ret;
562
563 if (length > NAND_MAX_PAGESIZE) {
564 printf("MXS NAND: DMA buffer too big\n");
565 return;
566 }
567
568 if (!buf) {
569 printf("MXS NAND: DMA buffer is NULL\n");
570 return;
571 }
572
573 memcpy(nand_info->data_buf, buf, length);
574
575 /* Compile the DMA descriptor - a descriptor that writes data. */
576 d = mxs_nand_get_dma_desc(nand_info);
577 d->cmd.data =
578 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
579 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
Luca Ellero966f1cd2014-12-16 15:36:15 +0100580 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
Marek Vasut913a7252011-11-08 23:18:16 +0000581 (length << MXS_DMA_DESC_BYTES_OFFSET);
582
583 d->cmd.address = (dma_addr_t)nand_info->data_buf;
584
585 d->cmd.pio_words[0] =
586 GPMI_CTRL0_COMMAND_MODE_WRITE |
587 GPMI_CTRL0_WORD_LENGTH |
588 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
589 GPMI_CTRL0_ADDRESS_NAND_DATA |
590 length;
591
592 mxs_dma_desc_append(channel, d);
593
Marek Vasut1b120e82012-03-15 18:33:19 +0000594 /* Flush caches */
595 mxs_nand_flush_data_buf(nand_info);
596
Marek Vasut913a7252011-11-08 23:18:16 +0000597 /* Execute the DMA chain. */
598 ret = mxs_dma_go(channel);
599 if (ret)
600 printf("MXS NAND: DMA write error\n");
601
602 mxs_nand_return_dma_descs(nand_info);
603}
604
605/*
606 * Read a single byte from NAND.
607 */
608static uint8_t mxs_nand_read_byte(struct mtd_info *mtd)
609{
610 uint8_t buf;
611 mxs_nand_read_buf(mtd, &buf, 1);
612 return buf;
613}
614
615/*
616 * Read a page from NAND.
617 */
618static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000619 uint8_t *buf, int oob_required,
620 int page)
Marek Vasut913a7252011-11-08 23:18:16 +0000621{
Scott Wood17fed142016-05-30 13:57:56 -0500622 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Stefan Agnerd0778b32018-06-22 17:19:49 +0200623 struct bch_geometry *geo = &nand_info->bch_geometry;
Marek Vasut913a7252011-11-08 23:18:16 +0000624 struct mxs_dma_desc *d;
625 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
626 uint32_t corrected = 0, failed = 0;
627 uint8_t *status;
628 int i, ret;
629
630 /* Compile the DMA descriptor - wait for ready. */
631 d = mxs_nand_get_dma_desc(nand_info);
632 d->cmd.data =
633 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
634 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
635 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
636
637 d->cmd.address = 0;
638
639 d->cmd.pio_words[0] =
640 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
641 GPMI_CTRL0_WORD_LENGTH |
642 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
643 GPMI_CTRL0_ADDRESS_NAND_DATA;
644
645 mxs_dma_desc_append(channel, d);
646
647 /* Compile the DMA descriptor - enable the BCH block and read. */
648 d = mxs_nand_get_dma_desc(nand_info);
649 d->cmd.data =
650 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
651 MXS_DMA_DESC_WAIT4END | (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
652
653 d->cmd.address = 0;
654
655 d->cmd.pio_words[0] =
656 GPMI_CTRL0_COMMAND_MODE_READ |
657 GPMI_CTRL0_WORD_LENGTH |
658 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
659 GPMI_CTRL0_ADDRESS_NAND_DATA |
660 (mtd->writesize + mtd->oobsize);
661 d->cmd.pio_words[1] = 0;
662 d->cmd.pio_words[2] =
663 GPMI_ECCCTRL_ENABLE_ECC |
664 GPMI_ECCCTRL_ECC_CMD_DECODE |
665 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
666 d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
667 d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
668 d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
669
670 mxs_dma_desc_append(channel, d);
671
672 /* Compile the DMA descriptor - disable the BCH block. */
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 (3 << 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 (mtd->writesize + mtd->oobsize);
687 d->cmd.pio_words[1] = 0;
688 d->cmd.pio_words[2] = 0;
689
690 mxs_dma_desc_append(channel, d);
691
692 /* Compile the DMA descriptor - deassert the NAND lock and interrupt. */
693 d = mxs_nand_get_dma_desc(nand_info);
694 d->cmd.data =
695 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
696 MXS_DMA_DESC_DEC_SEM;
697
698 d->cmd.address = 0;
699
700 mxs_dma_desc_append(channel, d);
701
Peng Fane3bbfb72015-07-21 16:15:21 +0800702 /* Invalidate caches */
703 mxs_nand_inval_data_buf(nand_info);
704
Marek Vasut913a7252011-11-08 23:18:16 +0000705 /* Execute the DMA chain. */
706 ret = mxs_dma_go(channel);
707 if (ret) {
708 printf("MXS NAND: DMA read error\n");
709 goto rtn;
710 }
711
Stefan Agnerdc8af6d2018-06-22 18:06:12 +0200712 ret = mxs_nand_wait_for_bch_complete(nand_info);
Marek Vasut913a7252011-11-08 23:18:16 +0000713 if (ret) {
714 printf("MXS NAND: BCH read timeout\n");
715 goto rtn;
716 }
717
Marek Vasut1b120e82012-03-15 18:33:19 +0000718 /* Invalidate caches */
719 mxs_nand_inval_data_buf(nand_info);
720
Marek Vasut913a7252011-11-08 23:18:16 +0000721 /* Read DMA completed, now do the mark swapping. */
Stefan Agnerd0778b32018-06-22 17:19:49 +0200722 mxs_nand_swap_block_mark(geo, nand_info->data_buf, nand_info->oob_buf);
Marek Vasut913a7252011-11-08 23:18:16 +0000723
724 /* Loop over status bytes, accumulating ECC status. */
725 status = nand_info->oob_buf + mxs_nand_aux_status_offset();
Stefan Agnerd0778b32018-06-22 17:19:49 +0200726 for (i = 0; i < geo->ecc_chunk_count; i++) {
Marek Vasut913a7252011-11-08 23:18:16 +0000727 if (status[i] == 0x00)
728 continue;
729
730 if (status[i] == 0xff)
731 continue;
732
733 if (status[i] == 0xfe) {
734 failed++;
735 continue;
736 }
737
738 corrected += status[i];
739 }
740
741 /* Propagate ECC status to the owning MTD. */
742 mtd->ecc_stats.failed += failed;
743 mtd->ecc_stats.corrected += corrected;
744
745 /*
746 * It's time to deliver the OOB bytes. See mxs_nand_ecc_read_oob() for
747 * details about our policy for delivering the OOB.
748 *
749 * We fill the caller's buffer with set bits, and then copy the block
750 * mark to the caller's buffer. Note that, if block mark swapping was
751 * necessary, it has already been done, so we can rely on the first
752 * byte of the auxiliary buffer to contain the block mark.
753 */
754 memset(nand->oob_poi, 0xff, mtd->oobsize);
755
756 nand->oob_poi[0] = nand_info->oob_buf[0];
757
758 memcpy(buf, nand_info->data_buf, mtd->writesize);
759
760rtn:
761 mxs_nand_return_dma_descs(nand_info);
762
763 return ret;
764}
765
766/*
767 * Write a page to NAND.
768 */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000769static int mxs_nand_ecc_write_page(struct mtd_info *mtd,
770 struct nand_chip *nand, const uint8_t *buf,
Scott Wood46e13102016-05-30 13:57:57 -0500771 int oob_required, int page)
Marek Vasut913a7252011-11-08 23:18:16 +0000772{
Scott Wood17fed142016-05-30 13:57:56 -0500773 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Stefan Agnerd0778b32018-06-22 17:19:49 +0200774 struct bch_geometry *geo = &nand_info->bch_geometry;
Marek Vasut913a7252011-11-08 23:18:16 +0000775 struct mxs_dma_desc *d;
776 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
777 int ret;
778
779 memcpy(nand_info->data_buf, buf, mtd->writesize);
780 memcpy(nand_info->oob_buf, nand->oob_poi, mtd->oobsize);
781
782 /* Handle block mark swapping. */
Stefan Agnerd0778b32018-06-22 17:19:49 +0200783 mxs_nand_swap_block_mark(geo, nand_info->data_buf, nand_info->oob_buf);
Marek Vasut913a7252011-11-08 23:18:16 +0000784
785 /* Compile the DMA descriptor - write data. */
786 d = mxs_nand_get_dma_desc(nand_info);
787 d->cmd.data =
788 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
789 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
790 (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
791
792 d->cmd.address = 0;
793
794 d->cmd.pio_words[0] =
795 GPMI_CTRL0_COMMAND_MODE_WRITE |
796 GPMI_CTRL0_WORD_LENGTH |
797 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
798 GPMI_CTRL0_ADDRESS_NAND_DATA;
799 d->cmd.pio_words[1] = 0;
800 d->cmd.pio_words[2] =
801 GPMI_ECCCTRL_ENABLE_ECC |
802 GPMI_ECCCTRL_ECC_CMD_ENCODE |
803 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
804 d->cmd.pio_words[3] = (mtd->writesize + mtd->oobsize);
805 d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
806 d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
807
Igor Opaniukc55401372019-11-03 16:49:43 +0100808 if (is_mx7() && nand_info->en_randomizer) {
809 d->cmd.pio_words[2] |= GPMI_ECCCTRL_RANDOMIZER_ENABLE |
810 GPMI_ECCCTRL_RANDOMIZER_TYPE2;
811 /*
812 * Write NAND page number needed to be randomized
813 * to GPMI_ECCCOUNT register.
814 *
815 * The value is between 0-255. For additional details
816 * check 9.6.6.4 of i.MX7D Applications Processor reference
817 */
818 d->cmd.pio_words[3] |= (page % 255) << 16;
819 }
820
Marek Vasut913a7252011-11-08 23:18:16 +0000821 mxs_dma_desc_append(channel, d);
822
Marek Vasut1b120e82012-03-15 18:33:19 +0000823 /* Flush caches */
824 mxs_nand_flush_data_buf(nand_info);
825
Marek Vasut913a7252011-11-08 23:18:16 +0000826 /* Execute the DMA chain. */
827 ret = mxs_dma_go(channel);
828 if (ret) {
829 printf("MXS NAND: DMA write error\n");
830 goto rtn;
831 }
832
Stefan Agnerdc8af6d2018-06-22 18:06:12 +0200833 ret = mxs_nand_wait_for_bch_complete(nand_info);
Marek Vasut913a7252011-11-08 23:18:16 +0000834 if (ret) {
835 printf("MXS NAND: BCH write timeout\n");
836 goto rtn;
837 }
838
839rtn:
840 mxs_nand_return_dma_descs(nand_info);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000841 return 0;
Marek Vasut913a7252011-11-08 23:18:16 +0000842}
843
844/*
845 * Read OOB from NAND.
846 *
847 * This function is a veneer that replaces the function originally installed by
848 * the NAND Flash MTD code.
849 */
850static int mxs_nand_hook_read_oob(struct mtd_info *mtd, loff_t from,
851 struct mtd_oob_ops *ops)
852{
Scott Wood17fed142016-05-30 13:57:56 -0500853 struct nand_chip *chip = mtd_to_nand(mtd);
854 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Marek Vasut913a7252011-11-08 23:18:16 +0000855 int ret;
856
Sergey Lapin3a38a552013-01-14 03:46:50 +0000857 if (ops->mode == MTD_OPS_RAW)
Marek Vasut913a7252011-11-08 23:18:16 +0000858 nand_info->raw_oob_mode = 1;
859 else
860 nand_info->raw_oob_mode = 0;
861
862 ret = nand_info->hooked_read_oob(mtd, from, ops);
863
864 nand_info->raw_oob_mode = 0;
865
866 return ret;
867}
868
869/*
870 * Write OOB to NAND.
871 *
872 * This function is a veneer that replaces the function originally installed by
873 * the NAND Flash MTD code.
874 */
875static int mxs_nand_hook_write_oob(struct mtd_info *mtd, loff_t to,
876 struct mtd_oob_ops *ops)
877{
Scott Wood17fed142016-05-30 13:57:56 -0500878 struct nand_chip *chip = mtd_to_nand(mtd);
879 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Marek Vasut913a7252011-11-08 23:18:16 +0000880 int ret;
881
Sergey Lapin3a38a552013-01-14 03:46:50 +0000882 if (ops->mode == MTD_OPS_RAW)
Marek Vasut913a7252011-11-08 23:18:16 +0000883 nand_info->raw_oob_mode = 1;
884 else
885 nand_info->raw_oob_mode = 0;
886
887 ret = nand_info->hooked_write_oob(mtd, to, ops);
888
889 nand_info->raw_oob_mode = 0;
890
891 return ret;
892}
893
894/*
895 * Mark a block bad in NAND.
896 *
897 * This function is a veneer that replaces the function originally installed by
898 * the NAND Flash MTD code.
899 */
900static int mxs_nand_hook_block_markbad(struct mtd_info *mtd, loff_t ofs)
901{
Scott Wood17fed142016-05-30 13:57:56 -0500902 struct nand_chip *chip = mtd_to_nand(mtd);
903 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Marek Vasut913a7252011-11-08 23:18:16 +0000904 int ret;
905
906 nand_info->marking_block_bad = 1;
907
908 ret = nand_info->hooked_block_markbad(mtd, ofs);
909
910 nand_info->marking_block_bad = 0;
911
912 return ret;
913}
914
915/*
916 * There are several places in this driver where we have to handle the OOB and
917 * block marks. This is the function where things are the most complicated, so
918 * this is where we try to explain it all. All the other places refer back to
919 * here.
920 *
921 * These are the rules, in order of decreasing importance:
922 *
923 * 1) Nothing the caller does can be allowed to imperil the block mark, so all
924 * write operations take measures to protect it.
925 *
926 * 2) In read operations, the first byte of the OOB we return must reflect the
927 * true state of the block mark, no matter where that block mark appears in
928 * the physical page.
929 *
930 * 3) ECC-based read operations return an OOB full of set bits (since we never
931 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
932 * return).
933 *
934 * 4) "Raw" read operations return a direct view of the physical bytes in the
935 * page, using the conventional definition of which bytes are data and which
936 * are OOB. This gives the caller a way to see the actual, physical bytes
937 * in the page, without the distortions applied by our ECC engine.
938 *
939 * What we do for this specific read operation depends on whether we're doing
940 * "raw" read, or an ECC-based read.
941 *
942 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
943 * easy. When reading a page, for example, the NAND Flash MTD code calls our
944 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
945 * ECC-based or raw view of the page is implicit in which function it calls
946 * (there is a similar pair of ECC-based/raw functions for writing).
947 *
948 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
949 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
950 * caller wants an ECC-based or raw view of the page is not propagated down to
951 * this driver.
952 *
953 * Since our OOB *is* covered by ECC, we need this information. So, we hook the
954 * ecc.read_oob and ecc.write_oob function pointers in the owning
955 * struct mtd_info with our own functions. These hook functions set the
956 * raw_oob_mode field so that, when control finally arrives here, we'll know
957 * what to do.
958 */
959static int mxs_nand_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000960 int page)
Marek Vasut913a7252011-11-08 23:18:16 +0000961{
Scott Wood17fed142016-05-30 13:57:56 -0500962 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut913a7252011-11-08 23:18:16 +0000963
964 /*
965 * First, fill in the OOB buffer. If we're doing a raw read, we need to
966 * get the bytes from the physical page. If we're not doing a raw read,
967 * we need to fill the buffer with set bits.
968 */
969 if (nand_info->raw_oob_mode) {
970 /*
971 * If control arrives here, we're doing a "raw" read. Send the
972 * command to read the conventional OOB and read it.
973 */
974 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
975 nand->read_buf(mtd, nand->oob_poi, mtd->oobsize);
976 } else {
977 /*
978 * If control arrives here, we're not doing a "raw" read. Fill
979 * the OOB buffer with set bits and correct the block mark.
980 */
981 memset(nand->oob_poi, 0xff, mtd->oobsize);
982
983 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
984 mxs_nand_read_buf(mtd, nand->oob_poi, 1);
985 }
986
987 return 0;
988
989}
990
991/*
992 * Write OOB data to NAND.
993 */
994static int mxs_nand_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
995 int page)
996{
Scott Wood17fed142016-05-30 13:57:56 -0500997 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut913a7252011-11-08 23:18:16 +0000998 uint8_t block_mark = 0;
999
1000 /*
1001 * There are fundamental incompatibilities between the i.MX GPMI NFC and
1002 * the NAND Flash MTD model that make it essentially impossible to write
1003 * the out-of-band bytes.
1004 *
1005 * We permit *ONE* exception. If the *intent* of writing the OOB is to
1006 * mark a block bad, we can do that.
1007 */
1008
1009 if (!nand_info->marking_block_bad) {
1010 printf("NXS NAND: Writing OOB isn't supported\n");
1011 return -EIO;
1012 }
1013
1014 /* Write the block mark. */
1015 nand->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1016 nand->write_buf(mtd, &block_mark, 1);
1017 nand->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1018
1019 /* Check if it worked. */
1020 if (nand->waitfunc(mtd, nand) & NAND_STATUS_FAIL)
1021 return -EIO;
1022
1023 return 0;
1024}
1025
1026/*
1027 * Claims all blocks are good.
1028 *
1029 * In principle, this function is *only* called when the NAND Flash MTD system
1030 * isn't allowed to keep an in-memory bad block table, so it is forced to ask
1031 * the driver for bad block information.
1032 *
1033 * In fact, we permit the NAND Flash MTD system to have an in-memory BBT, so
1034 * this function is *only* called when we take it away.
1035 *
1036 * Thus, this function is only called when we want *all* blocks to look good,
1037 * so it *always* return success.
1038 */
Scott Wood52ab7ce2016-05-30 13:57:58 -05001039static int mxs_nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Marek Vasut913a7252011-11-08 23:18:16 +00001040{
Stefan Agneread66eb2018-06-22 18:06:18 +02001041 return 0;
1042}
1043
1044static int mxs_nand_set_geometry(struct mtd_info *mtd, struct bch_geometry *geo)
1045{
1046 struct nand_chip *chip = mtd_to_nand(mtd);
1047 struct nand_chip *nand = mtd_to_nand(mtd);
1048 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1049
Ye Li94547442020-05-04 22:08:50 +08001050 if (chip->ecc_strength_ds > nand_info->max_ecc_strength_supported) {
1051 printf("unsupported NAND chip, minimum ecc required %d\n"
1052 , chip->ecc_strength_ds);
1053 return -EINVAL;
1054 }
Stefan Agneread66eb2018-06-22 18:06:18 +02001055
Ye Li94547442020-05-04 22:08:50 +08001056 if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0) &&
1057 (mtd->oobsize < 1024)) {
1058 dev_warn(this->dev, "use legacy bch geometry\n");
1059 return mxs_nand_legacy_calc_ecc_layout(geo, mtd);
1060 }
Stefan Agneread66eb2018-06-22 18:06:18 +02001061
Ye Li94547442020-05-04 22:08:50 +08001062 if (mtd->oobsize > 1024 || chip->ecc_step_ds < mtd->oobsize)
1063 return mxs_nand_calc_ecc_for_large_oob(geo, mtd);
1064
1065 return mxs_nand_calc_ecc_layout_by_info(geo, mtd,
Stefan Agneread66eb2018-06-22 18:06:18 +02001066 chip->ecc_strength_ds, chip->ecc_step_ds);
Stefan Agneread66eb2018-06-22 18:06:18 +02001067
Marek Vasut913a7252011-11-08 23:18:16 +00001068 return 0;
1069}
1070
1071/*
Marek Vasut913a7252011-11-08 23:18:16 +00001072 * At this point, the physical NAND Flash chips have been identified and
1073 * counted, so we know the physical geometry. This enables us to make some
1074 * important configuration decisions.
1075 *
Robert P. J. Day8d56db92016-07-15 13:44:45 -04001076 * The return value of this function propagates directly back to this driver's
Stefan Agner5883e552018-06-22 17:19:47 +02001077 * board_nand_init(). Anything other than zero will cause this driver to
Marek Vasut913a7252011-11-08 23:18:16 +00001078 * tear everything down and declare failure.
1079 */
Stefan Agner5883e552018-06-22 17:19:47 +02001080int mxs_nand_setup_ecc(struct mtd_info *mtd)
Marek Vasut913a7252011-11-08 23:18:16 +00001081{
Scott Wood17fed142016-05-30 13:57:56 -05001082 struct nand_chip *nand = mtd_to_nand(mtd);
1083 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Stefan Agnerd0778b32018-06-22 17:19:49 +02001084 struct bch_geometry *geo = &nand_info->bch_geometry;
Stefan Agnerdc8af6d2018-06-22 18:06:12 +02001085 struct mxs_bch_regs *bch_regs = nand_info->bch_regs;
Marek Vasut913a7252011-11-08 23:18:16 +00001086 uint32_t tmp;
Stefan Agneread66eb2018-06-22 18:06:18 +02001087 int ret;
Stefan Agner4d42ac12018-06-22 17:19:51 +02001088
Igor Opaniukc55401372019-11-03 16:49:43 +01001089 nand_info->en_randomizer = 0;
1090 nand_info->oobsize = mtd->oobsize;
1091 nand_info->writesize = mtd->writesize;
1092
Stefan Agneread66eb2018-06-22 18:06:18 +02001093 ret = mxs_nand_set_geometry(mtd, geo);
Stefan Agner4d42ac12018-06-22 17:19:51 +02001094 if (ret)
1095 return ret;
1096
Marek Vasut913a7252011-11-08 23:18:16 +00001097 /* Configure BCH and set NFC geometry */
Otavio Salvadorcbf0bf22012-08-13 09:53:12 +00001098 mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
Marek Vasut913a7252011-11-08 23:18:16 +00001099
1100 /* Configure layout 0 */
Stefan Agnerd0778b32018-06-22 17:19:49 +02001101 tmp = (geo->ecc_chunk_count - 1) << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
Marek Vasut913a7252011-11-08 23:18:16 +00001102 tmp |= MXS_NAND_METADATA_SIZE << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
Stefan Agnerd0778b32018-06-22 17:19:49 +02001103 tmp |= (geo->ecc_strength >> 1) << BCH_FLASHLAYOUT0_ECC0_OFFSET;
Ye Li94547442020-05-04 22:08:50 +08001104 tmp |= geo->ecc_chunk0_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
Stefan Agnerd0778b32018-06-22 17:19:49 +02001105 tmp |= (geo->gf_len == 14 ? 1 : 0) <<
Peng Fanc94f09d2015-07-21 16:15:19 +08001106 BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET;
Marek Vasut913a7252011-11-08 23:18:16 +00001107 writel(tmp, &bch_regs->hw_bch_flash0layout0);
Igor Opaniukc55401372019-11-03 16:49:43 +01001108 nand_info->bch_flash0layout0 = tmp;
Marek Vasut913a7252011-11-08 23:18:16 +00001109
1110 tmp = (mtd->writesize + mtd->oobsize)
1111 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
Stefan Agnerd0778b32018-06-22 17:19:49 +02001112 tmp |= (geo->ecc_strength >> 1) << BCH_FLASHLAYOUT1_ECCN_OFFSET;
Ye Li94547442020-05-04 22:08:50 +08001113 tmp |= geo->ecc_chunkn_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
Stefan Agnerd0778b32018-06-22 17:19:49 +02001114 tmp |= (geo->gf_len == 14 ? 1 : 0) <<
Peng Fanc94f09d2015-07-21 16:15:19 +08001115 BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
Marek Vasut913a7252011-11-08 23:18:16 +00001116 writel(tmp, &bch_regs->hw_bch_flash0layout1);
Igor Opaniukc55401372019-11-03 16:49:43 +01001117 nand_info->bch_flash0layout1 = tmp;
Marek Vasut913a7252011-11-08 23:18:16 +00001118
1119 /* Set *all* chip selects to use layout 0 */
1120 writel(0, &bch_regs->hw_bch_layoutselect);
1121
1122 /* Enable BCH complete interrupt */
1123 writel(BCH_CTRL_COMPLETE_IRQ_EN, &bch_regs->hw_bch_ctrl_set);
1124
1125 /* Hook some operations at the MTD level. */
Sergey Lapin3a38a552013-01-14 03:46:50 +00001126 if (mtd->_read_oob != mxs_nand_hook_read_oob) {
1127 nand_info->hooked_read_oob = mtd->_read_oob;
1128 mtd->_read_oob = mxs_nand_hook_read_oob;
Marek Vasut913a7252011-11-08 23:18:16 +00001129 }
1130
Sergey Lapin3a38a552013-01-14 03:46:50 +00001131 if (mtd->_write_oob != mxs_nand_hook_write_oob) {
1132 nand_info->hooked_write_oob = mtd->_write_oob;
1133 mtd->_write_oob = mxs_nand_hook_write_oob;
Marek Vasut913a7252011-11-08 23:18:16 +00001134 }
1135
Sergey Lapin3a38a552013-01-14 03:46:50 +00001136 if (mtd->_block_markbad != mxs_nand_hook_block_markbad) {
1137 nand_info->hooked_block_markbad = mtd->_block_markbad;
1138 mtd->_block_markbad = mxs_nand_hook_block_markbad;
Marek Vasut913a7252011-11-08 23:18:16 +00001139 }
1140
Stefan Agner5883e552018-06-22 17:19:47 +02001141 return 0;
Marek Vasut913a7252011-11-08 23:18:16 +00001142}
1143
1144/*
1145 * Allocate DMA buffers
1146 */
1147int mxs_nand_alloc_buffers(struct mxs_nand_info *nand_info)
1148{
1149 uint8_t *buf;
1150 const int size = NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE;
1151
Marek Vasut1b120e82012-03-15 18:33:19 +00001152 nand_info->data_buf_size = roundup(size, MXS_DMA_ALIGNMENT);
1153
Marek Vasut913a7252011-11-08 23:18:16 +00001154 /* DMA buffers */
Marek Vasut1b120e82012-03-15 18:33:19 +00001155 buf = memalign(MXS_DMA_ALIGNMENT, nand_info->data_buf_size);
Marek Vasut913a7252011-11-08 23:18:16 +00001156 if (!buf) {
1157 printf("MXS NAND: Error allocating DMA buffers\n");
1158 return -ENOMEM;
1159 }
1160
Marek Vasut1b120e82012-03-15 18:33:19 +00001161 memset(buf, 0, nand_info->data_buf_size);
Marek Vasut913a7252011-11-08 23:18:16 +00001162
1163 nand_info->data_buf = buf;
1164 nand_info->oob_buf = buf + NAND_MAX_PAGESIZE;
Marek Vasut913a7252011-11-08 23:18:16 +00001165 /* Command buffers */
1166 nand_info->cmd_buf = memalign(MXS_DMA_ALIGNMENT,
1167 MXS_NAND_COMMAND_BUFFER_SIZE);
1168 if (!nand_info->cmd_buf) {
1169 free(buf);
1170 printf("MXS NAND: Error allocating command buffers\n");
1171 return -ENOMEM;
1172 }
1173 memset(nand_info->cmd_buf, 0, MXS_NAND_COMMAND_BUFFER_SIZE);
1174 nand_info->cmd_queue_len = 0;
1175
1176 return 0;
1177}
1178
1179/*
1180 * Initializes the NFC hardware.
1181 */
Adam Ford6edb91a2019-01-12 06:25:48 -06001182static int mxs_nand_init_dma(struct mxs_nand_info *info)
Marek Vasut913a7252011-11-08 23:18:16 +00001183{
Peng Fane37d5a92016-01-27 10:38:02 +08001184 int i = 0, j, ret = 0;
Marek Vasut913a7252011-11-08 23:18:16 +00001185
1186 info->desc = malloc(sizeof(struct mxs_dma_desc *) *
1187 MXS_NAND_DMA_DESCRIPTOR_COUNT);
Peng Fane37d5a92016-01-27 10:38:02 +08001188 if (!info->desc) {
1189 ret = -ENOMEM;
Marek Vasut913a7252011-11-08 23:18:16 +00001190 goto err1;
Peng Fane37d5a92016-01-27 10:38:02 +08001191 }
Marek Vasut913a7252011-11-08 23:18:16 +00001192
1193 /* Allocate the DMA descriptors. */
1194 for (i = 0; i < MXS_NAND_DMA_DESCRIPTOR_COUNT; i++) {
1195 info->desc[i] = mxs_dma_desc_alloc();
Peng Fane37d5a92016-01-27 10:38:02 +08001196 if (!info->desc[i]) {
1197 ret = -ENOMEM;
Marek Vasut913a7252011-11-08 23:18:16 +00001198 goto err2;
Peng Fane37d5a92016-01-27 10:38:02 +08001199 }
Marek Vasut913a7252011-11-08 23:18:16 +00001200 }
1201
1202 /* Init the DMA controller. */
Fabio Estevam17156222017-06-29 09:33:44 -03001203 mxs_dma_init();
Marek Vasut93541b42012-04-08 17:34:46 +00001204 for (j = MXS_DMA_CHANNEL_AHB_APBH_GPMI0;
1205 j <= MXS_DMA_CHANNEL_AHB_APBH_GPMI7; j++) {
Peng Fane37d5a92016-01-27 10:38:02 +08001206 ret = mxs_dma_init_channel(j);
1207 if (ret)
Marek Vasut93541b42012-04-08 17:34:46 +00001208 goto err3;
1209 }
Marek Vasut913a7252011-11-08 23:18:16 +00001210
1211 /* Reset the GPMI block. */
Stefan Agnerdc8af6d2018-06-22 18:06:12 +02001212 mxs_reset_block(&info->gpmi_regs->hw_gpmi_ctrl0_reg);
1213 mxs_reset_block(&info->bch_regs->hw_bch_ctrl_reg);
Marek Vasut913a7252011-11-08 23:18:16 +00001214
1215 /*
1216 * Choose NAND mode, set IRQ polarity, disable write protection and
1217 * select BCH ECC.
1218 */
Stefan Agnerdc8af6d2018-06-22 18:06:12 +02001219 clrsetbits_le32(&info->gpmi_regs->hw_gpmi_ctrl1,
Marek Vasut913a7252011-11-08 23:18:16 +00001220 GPMI_CTRL1_GPMI_MODE,
1221 GPMI_CTRL1_ATA_IRQRDY_POLARITY | GPMI_CTRL1_DEV_RESET |
1222 GPMI_CTRL1_BCH_MODE);
1223
1224 return 0;
1225
Marek Vasut93541b42012-04-08 17:34:46 +00001226err3:
Peng Fane37d5a92016-01-27 10:38:02 +08001227 for (--j; j >= MXS_DMA_CHANNEL_AHB_APBH_GPMI0; j--)
Marek Vasut93541b42012-04-08 17:34:46 +00001228 mxs_dma_release(j);
Marek Vasut913a7252011-11-08 23:18:16 +00001229err2:
Marek Vasut913a7252011-11-08 23:18:16 +00001230 for (--i; i >= 0; i--)
1231 mxs_dma_desc_free(info->desc[i]);
Peng Fane37d5a92016-01-27 10:38:02 +08001232 free(info->desc);
1233err1:
1234 if (ret == -ENOMEM)
1235 printf("MXS NAND: Unable to allocate DMA descriptors\n");
1236 return ret;
Marek Vasut913a7252011-11-08 23:18:16 +00001237}
1238
Stefan Agner7152f342018-06-22 17:19:46 +02001239int mxs_nand_init_spl(struct nand_chip *nand)
1240{
1241 struct mxs_nand_info *nand_info;
1242 int err;
1243
1244 nand_info = malloc(sizeof(struct mxs_nand_info));
1245 if (!nand_info) {
1246 printf("MXS NAND: Failed to allocate private data\n");
1247 return -ENOMEM;
1248 }
1249 memset(nand_info, 0, sizeof(struct mxs_nand_info));
1250
Stefan Agnerdc8af6d2018-06-22 18:06:12 +02001251 nand_info->gpmi_regs = (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
1252 nand_info->bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
Adam Ford10210732019-01-02 20:36:52 -06001253
1254 if (is_mx6sx() || is_mx7())
1255 nand_info->max_ecc_strength_supported = 62;
1256 else
1257 nand_info->max_ecc_strength_supported = 40;
1258
Stefan Agner7152f342018-06-22 17:19:46 +02001259 err = mxs_nand_alloc_buffers(nand_info);
1260 if (err)
1261 return err;
1262
Stefan Agner00e65162018-06-22 18:06:13 +02001263 err = mxs_nand_init_dma(nand_info);
Stefan Agner7152f342018-06-22 17:19:46 +02001264 if (err)
1265 return err;
1266
1267 nand_set_controller_data(nand, nand_info);
1268
1269 nand->options |= NAND_NO_SUBPAGE_WRITE;
1270
1271 nand->cmd_ctrl = mxs_nand_cmd_ctrl;
1272 nand->dev_ready = mxs_nand_device_ready;
1273 nand->select_chip = mxs_nand_select_chip;
Stefan Agner7152f342018-06-22 17:19:46 +02001274
1275 nand->read_byte = mxs_nand_read_byte;
1276 nand->read_buf = mxs_nand_read_buf;
1277
1278 nand->ecc.read_page = mxs_nand_ecc_read_page;
1279
1280 nand->ecc.mode = NAND_ECC_HW;
Stefan Agner7152f342018-06-22 17:19:46 +02001281
1282 return 0;
1283}
1284
Stefan Agner19f90512018-06-22 18:06:16 +02001285int mxs_nand_init_ctrl(struct mxs_nand_info *nand_info)
Marek Vasut913a7252011-11-08 23:18:16 +00001286{
Stefan Agner5883e552018-06-22 17:19:47 +02001287 struct mtd_info *mtd;
Stefan Agner5883e552018-06-22 17:19:47 +02001288 struct nand_chip *nand;
Marek Vasut913a7252011-11-08 23:18:16 +00001289 int err;
1290
Stefan Agner5883e552018-06-22 17:19:47 +02001291 nand = &nand_info->chip;
1292 mtd = nand_to_mtd(nand);
Marek Vasut913a7252011-11-08 23:18:16 +00001293 err = mxs_nand_alloc_buffers(nand_info);
1294 if (err)
Stefan Agner404b1102018-06-22 18:06:14 +02001295 return err;
Marek Vasut913a7252011-11-08 23:18:16 +00001296
Stefan Agner00e65162018-06-22 18:06:13 +02001297 err = mxs_nand_init_dma(nand_info);
Marek Vasut913a7252011-11-08 23:18:16 +00001298 if (err)
Stefan Agner404b1102018-06-22 18:06:14 +02001299 goto err_free_buffers;
Marek Vasut913a7252011-11-08 23:18:16 +00001300
1301 memset(&fake_ecc_layout, 0, sizeof(fake_ecc_layout));
1302
Stefan Agner95f376f2018-06-22 17:19:48 +02001303#ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1304 nand->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
1305#endif
1306
Scott Wood17fed142016-05-30 13:57:56 -05001307 nand_set_controller_data(nand, nand_info);
Marek Vasut913a7252011-11-08 23:18:16 +00001308 nand->options |= NAND_NO_SUBPAGE_WRITE;
1309
Stefan Agner150ddbc2018-06-22 18:06:17 +02001310 if (nand_info->dev)
1311 nand->flash_node = dev_of_offset(nand_info->dev);
1312
Marek Vasut913a7252011-11-08 23:18:16 +00001313 nand->cmd_ctrl = mxs_nand_cmd_ctrl;
1314
1315 nand->dev_ready = mxs_nand_device_ready;
1316 nand->select_chip = mxs_nand_select_chip;
1317 nand->block_bad = mxs_nand_block_bad;
Marek Vasut913a7252011-11-08 23:18:16 +00001318
1319 nand->read_byte = mxs_nand_read_byte;
1320
1321 nand->read_buf = mxs_nand_read_buf;
1322 nand->write_buf = mxs_nand_write_buf;
1323
Stefan Agner5883e552018-06-22 17:19:47 +02001324 /* first scan to find the device and get the page size */
1325 if (nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_DEVICE, NULL))
Stefan Agner404b1102018-06-22 18:06:14 +02001326 goto err_free_buffers;
Stefan Agner5883e552018-06-22 17:19:47 +02001327
1328 if (mxs_nand_setup_ecc(mtd))
Stefan Agner404b1102018-06-22 18:06:14 +02001329 goto err_free_buffers;
Stefan Agner5883e552018-06-22 17:19:47 +02001330
Marek Vasut913a7252011-11-08 23:18:16 +00001331 nand->ecc.read_page = mxs_nand_ecc_read_page;
1332 nand->ecc.write_page = mxs_nand_ecc_write_page;
1333 nand->ecc.read_oob = mxs_nand_ecc_read_oob;
1334 nand->ecc.write_oob = mxs_nand_ecc_write_oob;
1335
1336 nand->ecc.layout = &fake_ecc_layout;
1337 nand->ecc.mode = NAND_ECC_HW;
Ye Li94547442020-05-04 22:08:50 +08001338 nand->ecc.size = nand_info->bch_geometry.ecc_chunkn_size;
Stefan Agner72d627d2018-06-22 17:19:50 +02001339 nand->ecc.strength = nand_info->bch_geometry.ecc_strength;
Marek Vasut913a7252011-11-08 23:18:16 +00001340
Stefan Agner5883e552018-06-22 17:19:47 +02001341 /* second phase scan */
1342 err = nand_scan_tail(mtd);
1343 if (err)
Stefan Agner404b1102018-06-22 18:06:14 +02001344 goto err_free_buffers;
Stefan Agner5883e552018-06-22 17:19:47 +02001345
1346 err = nand_register(0, mtd);
1347 if (err)
Stefan Agner404b1102018-06-22 18:06:14 +02001348 goto err_free_buffers;
Stefan Agner5883e552018-06-22 17:19:47 +02001349
Stefan Agner404b1102018-06-22 18:06:14 +02001350 return 0;
Marek Vasut913a7252011-11-08 23:18:16 +00001351
Stefan Agner404b1102018-06-22 18:06:14 +02001352err_free_buffers:
Marek Vasut913a7252011-11-08 23:18:16 +00001353 free(nand_info->data_buf);
1354 free(nand_info->cmd_buf);
Stefan Agner404b1102018-06-22 18:06:14 +02001355
1356 return err;
1357}
1358
Stefan Agner150ddbc2018-06-22 18:06:17 +02001359#ifndef CONFIG_NAND_MXS_DT
Stefan Agner404b1102018-06-22 18:06:14 +02001360void board_nand_init(void)
1361{
1362 struct mxs_nand_info *nand_info;
1363
1364 nand_info = malloc(sizeof(struct mxs_nand_info));
1365 if (!nand_info) {
1366 printf("MXS NAND: Failed to allocate private data\n");
1367 return;
1368 }
1369 memset(nand_info, 0, sizeof(struct mxs_nand_info));
1370
1371 nand_info->gpmi_regs = (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
1372 nand_info->bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1373
Stefan Agner4dc98db2018-06-22 18:06:15 +02001374 /* Refer to Chapter 17 for i.MX6DQ, Chapter 18 for i.MX6SX */
1375 if (is_mx6sx() || is_mx7())
1376 nand_info->max_ecc_strength_supported = 62;
1377 else
1378 nand_info->max_ecc_strength_supported = 40;
1379
1380#ifdef CONFIG_NAND_MXS_USE_MINIMUM_ECC
1381 nand_info->use_minimum_ecc = true;
1382#endif
1383
Stefan Agner19f90512018-06-22 18:06:16 +02001384 if (mxs_nand_init_ctrl(nand_info) < 0)
Stefan Agner404b1102018-06-22 18:06:14 +02001385 goto err;
1386
Stefan Agner5883e552018-06-22 17:19:47 +02001387 return;
Stefan Agner404b1102018-06-22 18:06:14 +02001388
1389err:
1390 free(nand_info);
Marek Vasut913a7252011-11-08 23:18:16 +00001391}
Stefan Agner150ddbc2018-06-22 18:06:17 +02001392#endif
Igor Opaniukc55401372019-11-03 16:49:43 +01001393
1394/*
1395 * Read NAND layout for FCB block generation.
1396 */
1397void mxs_nand_get_layout(struct mtd_info *mtd, struct mxs_nand_layout *l)
1398{
1399 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1400 u32 tmp;
1401
1402 tmp = readl(&bch_regs->hw_bch_flash0layout0);
1403 l->nblocks = (tmp & BCH_FLASHLAYOUT0_NBLOCKS_MASK) >>
1404 BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1405 l->meta_size = (tmp & BCH_FLASHLAYOUT0_META_SIZE_MASK) >>
1406 BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1407
1408 tmp = readl(&bch_regs->hw_bch_flash0layout1);
1409 l->data0_size = 4 * ((tmp & BCH_FLASHLAYOUT0_DATA0_SIZE_MASK) >>
1410 BCH_FLASHLAYOUT0_DATA0_SIZE_OFFSET);
1411 l->ecc0 = (tmp & BCH_FLASHLAYOUT0_ECC0_MASK) >>
1412 BCH_FLASHLAYOUT0_ECC0_OFFSET;
1413 l->datan_size = 4 * ((tmp & BCH_FLASHLAYOUT1_DATAN_SIZE_MASK) >>
1414 BCH_FLASHLAYOUT1_DATAN_SIZE_OFFSET);
1415 l->eccn = (tmp & BCH_FLASHLAYOUT1_ECCN_MASK) >>
1416 BCH_FLASHLAYOUT1_ECCN_OFFSET;
1417}
1418
1419/*
1420 * Set BCH to specific layout used by ROM bootloader to read FCB.
1421 */
1422void mxs_nand_mode_fcb(struct mtd_info *mtd)
1423{
1424 u32 tmp;
1425 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1426 struct nand_chip *nand = mtd_to_nand(mtd);
1427 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1428
1429 nand_info->en_randomizer = 1;
1430
1431 mtd->writesize = 1024;
1432 mtd->oobsize = 1862 - 1024;
1433
1434 /* 8 ecc_chunks_*/
1435 tmp = 7 << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1436 /* 32 bytes for metadata */
1437 tmp |= 32 << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1438 /* using ECC62 level to be performed */
1439 tmp |= 0x1F << BCH_FLASHLAYOUT0_ECC0_OFFSET;
1440 /* 0x20 * 4 bytes of the data0 block */
1441 tmp |= 0x20 << BCH_FLASHLAYOUT0_DATA0_SIZE_OFFSET;
1442 tmp |= 0 << BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET;
1443 writel(tmp, &bch_regs->hw_bch_flash0layout0);
1444
1445 /* 1024 for data + 838 for OOB */
1446 tmp = 1862 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
1447 /* using ECC62 level to be performed */
1448 tmp |= 0x1F << BCH_FLASHLAYOUT1_ECCN_OFFSET;
1449 /* 0x20 * 4 bytes of the data0 block */
1450 tmp |= 0x20 << BCH_FLASHLAYOUT1_DATAN_SIZE_OFFSET;
1451 tmp |= 0 << BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
1452 writel(tmp, &bch_regs->hw_bch_flash0layout1);
1453}
1454
1455/*
1456 * Restore BCH to normal settings.
1457 */
1458void mxs_nand_mode_normal(struct mtd_info *mtd)
1459{
1460 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1461 struct nand_chip *nand = mtd_to_nand(mtd);
1462 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1463
1464 nand_info->en_randomizer = 0;
1465
1466 mtd->writesize = nand_info->writesize;
1467 mtd->oobsize = nand_info->oobsize;
1468
1469 writel(nand_info->bch_flash0layout0, &bch_regs->hw_bch_flash0layout0);
1470 writel(nand_info->bch_flash0layout1, &bch_regs->hw_bch_flash0layout1);
1471}
1472
1473uint32_t mxs_nand_mark_byte_offset(struct mtd_info *mtd)
1474{
1475 struct nand_chip *chip = mtd_to_nand(mtd);
1476 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
1477 struct bch_geometry *geo = &nand_info->bch_geometry;
1478
1479 return geo->block_mark_byte_offset;
1480}
1481
1482uint32_t mxs_nand_mark_bit_offset(struct mtd_info *mtd)
1483{
1484 struct nand_chip *chip = mtd_to_nand(mtd);
1485 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
1486 struct bch_geometry *geo = &nand_info->bch_geometry;
1487
1488 return geo->block_mark_bit_offset;
1489}