blob: 2584608641bf79916adb8562b53cab1619e8508c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasut0d4e8502011-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 Vasut0d4e8502011-11-08 23:18:16 +000013 */
14
Tom Warren651eb732012-09-10 08:47:51 -070015#include <common.h>
Marek Vasut0d4e8502011-11-08 23:18:16 +000016#include <linux/mtd/mtd.h>
Masahiro Yamada6ae39002017-11-30 13:45:24 +090017#include <linux/mtd/rawnand.h>
Marek Vasut0d4e8502011-11-08 23:18:16 +000018#include <linux/types.h>
Marek Vasut0d4e8502011-11-08 23:18:16 +000019#include <malloc.h>
Stefan Agner5346c312018-06-22 17:19:47 +020020#include <nand.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090021#include <linux/errno.h>
Marek Vasut0d4e8502011-11-08 23:18:16 +000022#include <asm/io.h>
23#include <asm/arch/clock.h>
24#include <asm/arch/imx-regs.h>
Stefano Babic552a8482017-06-29 10:16:06 +020025#include <asm/mach-imx/regs-bch.h>
26#include <asm/mach-imx/regs-gpmi.h>
Marek Vasut0d4e8502011-11-08 23:18:16 +000027#include <asm/arch/sys_proto.h>
Stefano Babic552a8482017-06-29 10:16:06 +020028#include <asm/mach-imx/dma.h>
Stefan Agner93459432018-06-22 17:19:46 +020029#include "mxs_nand.h"
Marek Vasut0d4e8502011-11-08 23:18:16 +000030
31#define MXS_NAND_DMA_DESCRIPTOR_COUNT 4
32
33#define MXS_NAND_CHUNK_DATA_CHUNK_SIZE 512
Peng Fanbedaa842015-12-22 17:04:23 +080034#if (defined(CONFIG_MX6) || defined(CONFIG_MX7))
Stefan Roeseae695b12013-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 Vasut0d4e8502011-11-08 23:18:16 +000039#define MXS_NAND_METADATA_SIZE 10
Jörg Krause1fbdb702015-04-15 09:27:22 +020040#define MXS_NAND_BITS_PER_ECC_LEVEL 13
Stefan Agner2a83c952016-08-01 23:55:18 -070041
42#if !defined(CONFIG_SYS_CACHELINE_SIZE) || CONFIG_SYS_CACHELINE_SIZE < 32
Marek Vasut0d4e8502011-11-08 23:18:16 +000043#define MXS_NAND_COMMAND_BUFFER_SIZE 32
Stefan Agner2a83c952016-08-01 23:55:18 -070044#else
45#define MXS_NAND_COMMAND_BUFFER_SIZE CONFIG_SYS_CACHELINE_SIZE
46#endif
Marek Vasut0d4e8502011-11-08 23:18:16 +000047
48#define MXS_NAND_BCH_TIMEOUT 10000
49
50struct mxs_nand_info {
Stefan Agner5346c312018-06-22 17:19:47 +020051 struct nand_chip chip;
Marek Vasut0d4e8502011-11-08 23:18:16 +000052 int cur_chip;
53
54 uint32_t cmd_queue_len;
Marek Vasut6b9408e2012-03-15 18:33:19 +000055 uint32_t data_buf_size;
Marek Vasut0d4e8502011-11-08 23:18:16 +000056
57 uint8_t *cmd_buf;
58 uint8_t *data_buf;
59 uint8_t *oob_buf;
60
61 uint8_t marking_block_bad;
62 uint8_t raw_oob_mode;
63
64 /* Functions with altered behaviour */
65 int (*hooked_read_oob)(struct mtd_info *mtd,
66 loff_t from, struct mtd_oob_ops *ops);
67 int (*hooked_write_oob)(struct mtd_info *mtd,
68 loff_t to, struct mtd_oob_ops *ops);
69 int (*hooked_block_markbad)(struct mtd_info *mtd,
70 loff_t ofs);
71
72 /* DMA descriptors */
73 struct mxs_dma_desc **desc;
74 uint32_t desc_index;
75};
76
77struct nand_ecclayout fake_ecc_layout;
Peng Fan63b29d82015-07-21 16:15:19 +080078static int chunk_data_size = MXS_NAND_CHUNK_DATA_CHUNK_SIZE;
79static int galois_field = 13;
Marek Vasut0d4e8502011-11-08 23:18:16 +000080
Marek Vasut6b9408e2012-03-15 18:33:19 +000081/*
82 * Cache management functions
83 */
84#ifndef CONFIG_SYS_DCACHE_OFF
85static void mxs_nand_flush_data_buf(struct mxs_nand_info *info)
86{
87 uint32_t addr = (uint32_t)info->data_buf;
88
89 flush_dcache_range(addr, addr + info->data_buf_size);
90}
91
92static void mxs_nand_inval_data_buf(struct mxs_nand_info *info)
93{
94 uint32_t addr = (uint32_t)info->data_buf;
95
96 invalidate_dcache_range(addr, addr + info->data_buf_size);
97}
98
99static void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info)
100{
101 uint32_t addr = (uint32_t)info->cmd_buf;
102
103 flush_dcache_range(addr, addr + MXS_NAND_COMMAND_BUFFER_SIZE);
104}
105#else
106static inline void mxs_nand_flush_data_buf(struct mxs_nand_info *info) {}
107static inline void mxs_nand_inval_data_buf(struct mxs_nand_info *info) {}
108static inline void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info) {}
109#endif
110
Marek Vasut0d4e8502011-11-08 23:18:16 +0000111static struct mxs_dma_desc *mxs_nand_get_dma_desc(struct mxs_nand_info *info)
112{
113 struct mxs_dma_desc *desc;
114
115 if (info->desc_index >= MXS_NAND_DMA_DESCRIPTOR_COUNT) {
116 printf("MXS NAND: Too many DMA descriptors requested\n");
117 return NULL;
118 }
119
120 desc = info->desc[info->desc_index];
121 info->desc_index++;
122
123 return desc;
124}
125
126static void mxs_nand_return_dma_descs(struct mxs_nand_info *info)
127{
128 int i;
129 struct mxs_dma_desc *desc;
130
131 for (i = 0; i < info->desc_index; i++) {
132 desc = info->desc[i];
133 memset(desc, 0, sizeof(struct mxs_dma_desc));
134 desc->address = (dma_addr_t)desc;
135 }
136
137 info->desc_index = 0;
138}
139
140static uint32_t mxs_nand_ecc_chunk_cnt(uint32_t page_data_size)
141{
Peng Fan63b29d82015-07-21 16:15:19 +0800142 return page_data_size / chunk_data_size;
Marek Vasut0d4e8502011-11-08 23:18:16 +0000143}
144
145static uint32_t mxs_nand_ecc_size_in_bits(uint32_t ecc_strength)
146{
Peng Fan63b29d82015-07-21 16:15:19 +0800147 return ecc_strength * galois_field;
Marek Vasut0d4e8502011-11-08 23:18:16 +0000148}
149
150static uint32_t mxs_nand_aux_status_offset(void)
151{
152 return (MXS_NAND_METADATA_SIZE + 0x3) & ~0x3;
153}
154
155static inline uint32_t mxs_nand_get_ecc_strength(uint32_t page_data_size,
156 uint32_t page_oob_size)
157{
Peng Fanbd38da12015-04-15 09:27:21 +0200158 int ecc_strength;
Peng Fan168617c2015-09-07 16:12:11 +0800159 int max_ecc_strength_supported;
160
161 /* Refer to Chapter 17 for i.MX6DQ, Chapter 18 for i.MX6SX */
Peng Fan9aa550d2016-05-23 18:36:02 +0800162 if (is_mx6sx() || is_mx7())
Peng Fan168617c2015-09-07 16:12:11 +0800163 max_ecc_strength_supported = 62;
164 else
165 max_ecc_strength_supported = 40;
Marek Vasutf9cfe172014-09-25 21:13:36 +0200166
Peng Fanbd38da12015-04-15 09:27:21 +0200167 /*
168 * Determine the ECC layout with the formula:
169 * ECC bits per chunk = (total page spare data bits) /
170 * (bits per ECC level) / (chunks per page)
171 * where:
172 * total page spare data bits =
173 * (page oob size - meta data size) * (bits per byte)
174 */
175 ecc_strength = ((page_oob_size - MXS_NAND_METADATA_SIZE) * 8)
Peng Fan63b29d82015-07-21 16:15:19 +0800176 / (galois_field *
177 mxs_nand_ecc_chunk_cnt(page_data_size));
Marek Vasut0d4e8502011-11-08 23:18:16 +0000178
Peng Fan168617c2015-09-07 16:12:11 +0800179 return min(round_down(ecc_strength, 2), max_ecc_strength_supported);
Marek Vasut0d4e8502011-11-08 23:18:16 +0000180}
181
182static inline uint32_t mxs_nand_get_mark_offset(uint32_t page_data_size,
183 uint32_t ecc_strength)
184{
185 uint32_t chunk_data_size_in_bits;
186 uint32_t chunk_ecc_size_in_bits;
187 uint32_t chunk_total_size_in_bits;
188 uint32_t block_mark_chunk_number;
189 uint32_t block_mark_chunk_bit_offset;
190 uint32_t block_mark_bit_offset;
191
Peng Fan63b29d82015-07-21 16:15:19 +0800192 chunk_data_size_in_bits = chunk_data_size * 8;
Marek Vasut0d4e8502011-11-08 23:18:16 +0000193 chunk_ecc_size_in_bits = mxs_nand_ecc_size_in_bits(ecc_strength);
194
195 chunk_total_size_in_bits =
196 chunk_data_size_in_bits + chunk_ecc_size_in_bits;
197
198 /* Compute the bit offset of the block mark within the physical page. */
199 block_mark_bit_offset = page_data_size * 8;
200
201 /* Subtract the metadata bits. */
202 block_mark_bit_offset -= MXS_NAND_METADATA_SIZE * 8;
203
204 /*
205 * Compute the chunk number (starting at zero) in which the block mark
206 * appears.
207 */
208 block_mark_chunk_number =
209 block_mark_bit_offset / chunk_total_size_in_bits;
210
211 /*
212 * Compute the bit offset of the block mark within its chunk, and
213 * validate it.
214 */
215 block_mark_chunk_bit_offset = block_mark_bit_offset -
216 (block_mark_chunk_number * chunk_total_size_in_bits);
217
218 if (block_mark_chunk_bit_offset > chunk_data_size_in_bits)
219 return 1;
220
221 /*
222 * Now that we know the chunk number in which the block mark appears,
223 * we can subtract all the ECC bits that appear before it.
224 */
225 block_mark_bit_offset -=
226 block_mark_chunk_number * chunk_ecc_size_in_bits;
227
228 return block_mark_bit_offset;
229}
230
231static uint32_t mxs_nand_mark_byte_offset(struct mtd_info *mtd)
232{
233 uint32_t ecc_strength;
234 ecc_strength = mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize);
235 return mxs_nand_get_mark_offset(mtd->writesize, ecc_strength) >> 3;
236}
237
238static uint32_t mxs_nand_mark_bit_offset(struct mtd_info *mtd)
239{
240 uint32_t ecc_strength;
241 ecc_strength = mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize);
242 return mxs_nand_get_mark_offset(mtd->writesize, ecc_strength) & 0x7;
243}
244
245/*
246 * Wait for BCH complete IRQ and clear the IRQ
247 */
248static int mxs_nand_wait_for_bch_complete(void)
249{
Otavio Salvador9c471142012-08-05 09:05:31 +0000250 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
Marek Vasut0d4e8502011-11-08 23:18:16 +0000251 int timeout = MXS_NAND_BCH_TIMEOUT;
252 int ret;
253
Otavio Salvadorfa7a51c2012-08-13 09:53:12 +0000254 ret = mxs_wait_mask_set(&bch_regs->hw_bch_ctrl_reg,
Marek Vasut0d4e8502011-11-08 23:18:16 +0000255 BCH_CTRL_COMPLETE_IRQ, timeout);
256
257 writel(BCH_CTRL_COMPLETE_IRQ, &bch_regs->hw_bch_ctrl_clr);
258
259 return ret;
260}
261
262/*
263 * This is the function that we install in the cmd_ctrl function pointer of the
264 * owning struct nand_chip. The only functions in the reference implementation
265 * that use these functions pointers are cmdfunc and select_chip.
266 *
267 * In this driver, we implement our own select_chip, so this function will only
268 * be called by the reference implementation's cmdfunc. For this reason, we can
269 * ignore the chip enable bit and concentrate only on sending bytes to the NAND
270 * Flash.
271 */
272static void mxs_nand_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
273{
Scott Wood17cb4b82016-05-30 13:57:56 -0500274 struct nand_chip *nand = mtd_to_nand(mtd);
275 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut0d4e8502011-11-08 23:18:16 +0000276 struct mxs_dma_desc *d;
277 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
278 int ret;
279
280 /*
281 * If this condition is true, something is _VERY_ wrong in MTD
282 * subsystem!
283 */
284 if (nand_info->cmd_queue_len == MXS_NAND_COMMAND_BUFFER_SIZE) {
285 printf("MXS NAND: Command queue too long\n");
286 return;
287 }
288
289 /*
290 * Every operation begins with a command byte and a series of zero or
291 * more address bytes. These are distinguished by either the Address
292 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
293 * asserted. When MTD is ready to execute the command, it will
294 * deasert both latch enables.
295 *
296 * Rather than run a separate DMA operation for every single byte, we
297 * queue them up and run a single DMA operation for the entire series
298 * of command and data bytes.
299 */
300 if (ctrl & (NAND_ALE | NAND_CLE)) {
301 if (data != NAND_CMD_NONE)
302 nand_info->cmd_buf[nand_info->cmd_queue_len++] = data;
303 return;
304 }
305
306 /*
307 * If control arrives here, MTD has deasserted both the ALE and CLE,
308 * which means it's ready to run an operation. Check if we have any
309 * bytes to send.
310 */
311 if (nand_info->cmd_queue_len == 0)
312 return;
313
314 /* Compile the DMA descriptor -- a descriptor that sends command. */
315 d = mxs_nand_get_dma_desc(nand_info);
316 d->cmd.data =
317 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
318 MXS_DMA_DESC_CHAIN | MXS_DMA_DESC_DEC_SEM |
319 MXS_DMA_DESC_WAIT4END | (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
320 (nand_info->cmd_queue_len << MXS_DMA_DESC_BYTES_OFFSET);
321
322 d->cmd.address = (dma_addr_t)nand_info->cmd_buf;
323
324 d->cmd.pio_words[0] =
325 GPMI_CTRL0_COMMAND_MODE_WRITE |
326 GPMI_CTRL0_WORD_LENGTH |
327 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
328 GPMI_CTRL0_ADDRESS_NAND_CLE |
329 GPMI_CTRL0_ADDRESS_INCREMENT |
330 nand_info->cmd_queue_len;
331
332 mxs_dma_desc_append(channel, d);
333
Marek Vasut6b9408e2012-03-15 18:33:19 +0000334 /* Flush caches */
335 mxs_nand_flush_cmd_buf(nand_info);
336
Marek Vasut0d4e8502011-11-08 23:18:16 +0000337 /* Execute the DMA chain. */
338 ret = mxs_dma_go(channel);
339 if (ret)
340 printf("MXS NAND: Error sending command\n");
341
342 mxs_nand_return_dma_descs(nand_info);
343
344 /* Reset the command queue. */
345 nand_info->cmd_queue_len = 0;
346}
347
348/*
349 * Test if the NAND flash is ready.
350 */
351static int mxs_nand_device_ready(struct mtd_info *mtd)
352{
Scott Wood17cb4b82016-05-30 13:57:56 -0500353 struct nand_chip *chip = mtd_to_nand(mtd);
354 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Otavio Salvador9c471142012-08-05 09:05:31 +0000355 struct mxs_gpmi_regs *gpmi_regs =
356 (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
Marek Vasut0d4e8502011-11-08 23:18:16 +0000357 uint32_t tmp;
358
359 tmp = readl(&gpmi_regs->hw_gpmi_stat);
360 tmp >>= (GPMI_STAT_READY_BUSY_OFFSET + nand_info->cur_chip);
361
362 return tmp & 1;
363}
364
365/*
366 * Select the NAND chip.
367 */
368static void mxs_nand_select_chip(struct mtd_info *mtd, int chip)
369{
Scott Wood17cb4b82016-05-30 13:57:56 -0500370 struct nand_chip *nand = mtd_to_nand(mtd);
371 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut0d4e8502011-11-08 23:18:16 +0000372
373 nand_info->cur_chip = chip;
374}
375
376/*
377 * Handle block mark swapping.
378 *
379 * Note that, when this function is called, it doesn't know whether it's
380 * swapping the block mark, or swapping it *back* -- but it doesn't matter
381 * because the the operation is the same.
382 */
383static void mxs_nand_swap_block_mark(struct mtd_info *mtd,
384 uint8_t *data_buf, uint8_t *oob_buf)
385{
386 uint32_t bit_offset;
387 uint32_t buf_offset;
388
389 uint32_t src;
390 uint32_t dst;
391
392 bit_offset = mxs_nand_mark_bit_offset(mtd);
393 buf_offset = mxs_nand_mark_byte_offset(mtd);
394
395 /*
396 * Get the byte from the data area that overlays the block mark. Since
397 * the ECC engine applies its own view to the bits in the page, the
398 * physical block mark won't (in general) appear on a byte boundary in
399 * the data.
400 */
401 src = data_buf[buf_offset] >> bit_offset;
402 src |= data_buf[buf_offset + 1] << (8 - bit_offset);
403
404 dst = oob_buf[0];
405
406 oob_buf[0] = src;
407
408 data_buf[buf_offset] &= ~(0xff << bit_offset);
409 data_buf[buf_offset + 1] &= 0xff << bit_offset;
410
411 data_buf[buf_offset] |= dst << bit_offset;
412 data_buf[buf_offset + 1] |= dst >> (8 - bit_offset);
413}
414
415/*
416 * Read data from NAND.
417 */
418static void mxs_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int length)
419{
Scott Wood17cb4b82016-05-30 13:57:56 -0500420 struct nand_chip *nand = mtd_to_nand(mtd);
421 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut0d4e8502011-11-08 23:18:16 +0000422 struct mxs_dma_desc *d;
423 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
424 int ret;
425
426 if (length > NAND_MAX_PAGESIZE) {
427 printf("MXS NAND: DMA buffer too big\n");
428 return;
429 }
430
431 if (!buf) {
432 printf("MXS NAND: DMA buffer is NULL\n");
433 return;
434 }
435
436 /* Compile the DMA descriptor - a descriptor that reads data. */
437 d = mxs_nand_get_dma_desc(nand_info);
438 d->cmd.data =
439 MXS_DMA_DESC_COMMAND_DMA_WRITE | MXS_DMA_DESC_IRQ |
440 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
441 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
442 (length << MXS_DMA_DESC_BYTES_OFFSET);
443
444 d->cmd.address = (dma_addr_t)nand_info->data_buf;
445
446 d->cmd.pio_words[0] =
447 GPMI_CTRL0_COMMAND_MODE_READ |
448 GPMI_CTRL0_WORD_LENGTH |
449 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
450 GPMI_CTRL0_ADDRESS_NAND_DATA |
451 length;
452
453 mxs_dma_desc_append(channel, d);
454
455 /*
456 * A DMA descriptor that waits for the command to end and the chip to
457 * become ready.
458 *
459 * I think we actually should *not* be waiting for the chip to become
460 * ready because, after all, we don't care. I think the original code
461 * did that and no one has re-thought it yet.
462 */
463 d = mxs_nand_get_dma_desc(nand_info);
464 d->cmd.data =
465 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
466 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_DEC_SEM |
Luca Ellero5263a022014-12-16 15:36:14 +0100467 MXS_DMA_DESC_WAIT4END | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
Marek Vasut0d4e8502011-11-08 23:18:16 +0000468
469 d->cmd.address = 0;
470
471 d->cmd.pio_words[0] =
472 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
473 GPMI_CTRL0_WORD_LENGTH |
474 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
475 GPMI_CTRL0_ADDRESS_NAND_DATA;
476
477 mxs_dma_desc_append(channel, d);
478
Peng Fanecfb8762015-07-21 16:15:21 +0800479 /* Invalidate caches */
480 mxs_nand_inval_data_buf(nand_info);
481
Marek Vasut0d4e8502011-11-08 23:18:16 +0000482 /* Execute the DMA chain. */
483 ret = mxs_dma_go(channel);
484 if (ret) {
485 printf("MXS NAND: DMA read error\n");
486 goto rtn;
487 }
488
Marek Vasut6b9408e2012-03-15 18:33:19 +0000489 /* Invalidate caches */
490 mxs_nand_inval_data_buf(nand_info);
491
Marek Vasut0d4e8502011-11-08 23:18:16 +0000492 memcpy(buf, nand_info->data_buf, length);
493
494rtn:
495 mxs_nand_return_dma_descs(nand_info);
496}
497
498/*
499 * Write data to NAND.
500 */
501static void mxs_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
502 int length)
503{
Scott Wood17cb4b82016-05-30 13:57:56 -0500504 struct nand_chip *nand = mtd_to_nand(mtd);
505 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut0d4e8502011-11-08 23:18:16 +0000506 struct mxs_dma_desc *d;
507 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
508 int ret;
509
510 if (length > NAND_MAX_PAGESIZE) {
511 printf("MXS NAND: DMA buffer too big\n");
512 return;
513 }
514
515 if (!buf) {
516 printf("MXS NAND: DMA buffer is NULL\n");
517 return;
518 }
519
520 memcpy(nand_info->data_buf, buf, length);
521
522 /* Compile the DMA descriptor - a descriptor that writes data. */
523 d = mxs_nand_get_dma_desc(nand_info);
524 d->cmd.data =
525 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
526 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
Luca Ellero88a2cbb2014-12-16 15:36:15 +0100527 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
Marek Vasut0d4e8502011-11-08 23:18:16 +0000528 (length << MXS_DMA_DESC_BYTES_OFFSET);
529
530 d->cmd.address = (dma_addr_t)nand_info->data_buf;
531
532 d->cmd.pio_words[0] =
533 GPMI_CTRL0_COMMAND_MODE_WRITE |
534 GPMI_CTRL0_WORD_LENGTH |
535 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
536 GPMI_CTRL0_ADDRESS_NAND_DATA |
537 length;
538
539 mxs_dma_desc_append(channel, d);
540
Marek Vasut6b9408e2012-03-15 18:33:19 +0000541 /* Flush caches */
542 mxs_nand_flush_data_buf(nand_info);
543
Marek Vasut0d4e8502011-11-08 23:18:16 +0000544 /* Execute the DMA chain. */
545 ret = mxs_dma_go(channel);
546 if (ret)
547 printf("MXS NAND: DMA write error\n");
548
549 mxs_nand_return_dma_descs(nand_info);
550}
551
552/*
553 * Read a single byte from NAND.
554 */
555static uint8_t mxs_nand_read_byte(struct mtd_info *mtd)
556{
557 uint8_t buf;
558 mxs_nand_read_buf(mtd, &buf, 1);
559 return buf;
560}
561
562/*
563 * Read a page from NAND.
564 */
565static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000566 uint8_t *buf, int oob_required,
567 int page)
Marek Vasut0d4e8502011-11-08 23:18:16 +0000568{
Scott Wood17cb4b82016-05-30 13:57:56 -0500569 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut0d4e8502011-11-08 23:18:16 +0000570 struct mxs_dma_desc *d;
571 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
572 uint32_t corrected = 0, failed = 0;
573 uint8_t *status;
574 int i, ret;
575
576 /* Compile the DMA descriptor - wait for ready. */
577 d = mxs_nand_get_dma_desc(nand_info);
578 d->cmd.data =
579 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
580 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
581 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
582
583 d->cmd.address = 0;
584
585 d->cmd.pio_words[0] =
586 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
587 GPMI_CTRL0_WORD_LENGTH |
588 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
589 GPMI_CTRL0_ADDRESS_NAND_DATA;
590
591 mxs_dma_desc_append(channel, d);
592
593 /* Compile the DMA descriptor - enable the BCH block and read. */
594 d = mxs_nand_get_dma_desc(nand_info);
595 d->cmd.data =
596 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
597 MXS_DMA_DESC_WAIT4END | (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
598
599 d->cmd.address = 0;
600
601 d->cmd.pio_words[0] =
602 GPMI_CTRL0_COMMAND_MODE_READ |
603 GPMI_CTRL0_WORD_LENGTH |
604 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
605 GPMI_CTRL0_ADDRESS_NAND_DATA |
606 (mtd->writesize + mtd->oobsize);
607 d->cmd.pio_words[1] = 0;
608 d->cmd.pio_words[2] =
609 GPMI_ECCCTRL_ENABLE_ECC |
610 GPMI_ECCCTRL_ECC_CMD_DECODE |
611 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
612 d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
613 d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
614 d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
615
616 mxs_dma_desc_append(channel, d);
617
618 /* Compile the DMA descriptor - disable the BCH block. */
619 d = mxs_nand_get_dma_desc(nand_info);
620 d->cmd.data =
621 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
622 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
623 (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
624
625 d->cmd.address = 0;
626
627 d->cmd.pio_words[0] =
628 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
629 GPMI_CTRL0_WORD_LENGTH |
630 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
631 GPMI_CTRL0_ADDRESS_NAND_DATA |
632 (mtd->writesize + mtd->oobsize);
633 d->cmd.pio_words[1] = 0;
634 d->cmd.pio_words[2] = 0;
635
636 mxs_dma_desc_append(channel, d);
637
638 /* Compile the DMA descriptor - deassert the NAND lock and interrupt. */
639 d = mxs_nand_get_dma_desc(nand_info);
640 d->cmd.data =
641 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
642 MXS_DMA_DESC_DEC_SEM;
643
644 d->cmd.address = 0;
645
646 mxs_dma_desc_append(channel, d);
647
Peng Fanecfb8762015-07-21 16:15:21 +0800648 /* Invalidate caches */
649 mxs_nand_inval_data_buf(nand_info);
650
Marek Vasut0d4e8502011-11-08 23:18:16 +0000651 /* Execute the DMA chain. */
652 ret = mxs_dma_go(channel);
653 if (ret) {
654 printf("MXS NAND: DMA read error\n");
655 goto rtn;
656 }
657
658 ret = mxs_nand_wait_for_bch_complete();
659 if (ret) {
660 printf("MXS NAND: BCH read timeout\n");
661 goto rtn;
662 }
663
Marek Vasut6b9408e2012-03-15 18:33:19 +0000664 /* Invalidate caches */
665 mxs_nand_inval_data_buf(nand_info);
666
Marek Vasut0d4e8502011-11-08 23:18:16 +0000667 /* Read DMA completed, now do the mark swapping. */
668 mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);
669
670 /* Loop over status bytes, accumulating ECC status. */
671 status = nand_info->oob_buf + mxs_nand_aux_status_offset();
672 for (i = 0; i < mxs_nand_ecc_chunk_cnt(mtd->writesize); i++) {
673 if (status[i] == 0x00)
674 continue;
675
676 if (status[i] == 0xff)
677 continue;
678
679 if (status[i] == 0xfe) {
680 failed++;
681 continue;
682 }
683
684 corrected += status[i];
685 }
686
687 /* Propagate ECC status to the owning MTD. */
688 mtd->ecc_stats.failed += failed;
689 mtd->ecc_stats.corrected += corrected;
690
691 /*
692 * It's time to deliver the OOB bytes. See mxs_nand_ecc_read_oob() for
693 * details about our policy for delivering the OOB.
694 *
695 * We fill the caller's buffer with set bits, and then copy the block
696 * mark to the caller's buffer. Note that, if block mark swapping was
697 * necessary, it has already been done, so we can rely on the first
698 * byte of the auxiliary buffer to contain the block mark.
699 */
700 memset(nand->oob_poi, 0xff, mtd->oobsize);
701
702 nand->oob_poi[0] = nand_info->oob_buf[0];
703
704 memcpy(buf, nand_info->data_buf, mtd->writesize);
705
706rtn:
707 mxs_nand_return_dma_descs(nand_info);
708
709 return ret;
710}
711
712/*
713 * Write a page to NAND.
714 */
Sergey Lapindfe64e22013-01-14 03:46:50 +0000715static int mxs_nand_ecc_write_page(struct mtd_info *mtd,
716 struct nand_chip *nand, const uint8_t *buf,
Scott Wood81c77252016-05-30 13:57:57 -0500717 int oob_required, int page)
Marek Vasut0d4e8502011-11-08 23:18:16 +0000718{
Scott Wood17cb4b82016-05-30 13:57:56 -0500719 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut0d4e8502011-11-08 23:18:16 +0000720 struct mxs_dma_desc *d;
721 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
722 int ret;
723
724 memcpy(nand_info->data_buf, buf, mtd->writesize);
725 memcpy(nand_info->oob_buf, nand->oob_poi, mtd->oobsize);
726
727 /* Handle block mark swapping. */
728 mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);
729
730 /* Compile the DMA descriptor - write data. */
731 d = mxs_nand_get_dma_desc(nand_info);
732 d->cmd.data =
733 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
734 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
735 (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
736
737 d->cmd.address = 0;
738
739 d->cmd.pio_words[0] =
740 GPMI_CTRL0_COMMAND_MODE_WRITE |
741 GPMI_CTRL0_WORD_LENGTH |
742 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
743 GPMI_CTRL0_ADDRESS_NAND_DATA;
744 d->cmd.pio_words[1] = 0;
745 d->cmd.pio_words[2] =
746 GPMI_ECCCTRL_ENABLE_ECC |
747 GPMI_ECCCTRL_ECC_CMD_ENCODE |
748 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
749 d->cmd.pio_words[3] = (mtd->writesize + mtd->oobsize);
750 d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
751 d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
752
753 mxs_dma_desc_append(channel, d);
754
Marek Vasut6b9408e2012-03-15 18:33:19 +0000755 /* Flush caches */
756 mxs_nand_flush_data_buf(nand_info);
757
Marek Vasut0d4e8502011-11-08 23:18:16 +0000758 /* Execute the DMA chain. */
759 ret = mxs_dma_go(channel);
760 if (ret) {
761 printf("MXS NAND: DMA write error\n");
762 goto rtn;
763 }
764
765 ret = mxs_nand_wait_for_bch_complete();
766 if (ret) {
767 printf("MXS NAND: BCH write timeout\n");
768 goto rtn;
769 }
770
771rtn:
772 mxs_nand_return_dma_descs(nand_info);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000773 return 0;
Marek Vasut0d4e8502011-11-08 23:18:16 +0000774}
775
776/*
777 * Read OOB from NAND.
778 *
779 * This function is a veneer that replaces the function originally installed by
780 * the NAND Flash MTD code.
781 */
782static int mxs_nand_hook_read_oob(struct mtd_info *mtd, loff_t from,
783 struct mtd_oob_ops *ops)
784{
Scott Wood17cb4b82016-05-30 13:57:56 -0500785 struct nand_chip *chip = mtd_to_nand(mtd);
786 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Marek Vasut0d4e8502011-11-08 23:18:16 +0000787 int ret;
788
Sergey Lapindfe64e22013-01-14 03:46:50 +0000789 if (ops->mode == MTD_OPS_RAW)
Marek Vasut0d4e8502011-11-08 23:18:16 +0000790 nand_info->raw_oob_mode = 1;
791 else
792 nand_info->raw_oob_mode = 0;
793
794 ret = nand_info->hooked_read_oob(mtd, from, ops);
795
796 nand_info->raw_oob_mode = 0;
797
798 return ret;
799}
800
801/*
802 * Write OOB to NAND.
803 *
804 * This function is a veneer that replaces the function originally installed by
805 * the NAND Flash MTD code.
806 */
807static int mxs_nand_hook_write_oob(struct mtd_info *mtd, loff_t to,
808 struct mtd_oob_ops *ops)
809{
Scott Wood17cb4b82016-05-30 13:57:56 -0500810 struct nand_chip *chip = mtd_to_nand(mtd);
811 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Marek Vasut0d4e8502011-11-08 23:18:16 +0000812 int ret;
813
Sergey Lapindfe64e22013-01-14 03:46:50 +0000814 if (ops->mode == MTD_OPS_RAW)
Marek Vasut0d4e8502011-11-08 23:18:16 +0000815 nand_info->raw_oob_mode = 1;
816 else
817 nand_info->raw_oob_mode = 0;
818
819 ret = nand_info->hooked_write_oob(mtd, to, ops);
820
821 nand_info->raw_oob_mode = 0;
822
823 return ret;
824}
825
826/*
827 * Mark a block bad in NAND.
828 *
829 * This function is a veneer that replaces the function originally installed by
830 * the NAND Flash MTD code.
831 */
832static int mxs_nand_hook_block_markbad(struct mtd_info *mtd, loff_t ofs)
833{
Scott Wood17cb4b82016-05-30 13:57:56 -0500834 struct nand_chip *chip = mtd_to_nand(mtd);
835 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Marek Vasut0d4e8502011-11-08 23:18:16 +0000836 int ret;
837
838 nand_info->marking_block_bad = 1;
839
840 ret = nand_info->hooked_block_markbad(mtd, ofs);
841
842 nand_info->marking_block_bad = 0;
843
844 return ret;
845}
846
847/*
848 * There are several places in this driver where we have to handle the OOB and
849 * block marks. This is the function where things are the most complicated, so
850 * this is where we try to explain it all. All the other places refer back to
851 * here.
852 *
853 * These are the rules, in order of decreasing importance:
854 *
855 * 1) Nothing the caller does can be allowed to imperil the block mark, so all
856 * write operations take measures to protect it.
857 *
858 * 2) In read operations, the first byte of the OOB we return must reflect the
859 * true state of the block mark, no matter where that block mark appears in
860 * the physical page.
861 *
862 * 3) ECC-based read operations return an OOB full of set bits (since we never
863 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
864 * return).
865 *
866 * 4) "Raw" read operations return a direct view of the physical bytes in the
867 * page, using the conventional definition of which bytes are data and which
868 * are OOB. This gives the caller a way to see the actual, physical bytes
869 * in the page, without the distortions applied by our ECC engine.
870 *
871 * What we do for this specific read operation depends on whether we're doing
872 * "raw" read, or an ECC-based read.
873 *
874 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
875 * easy. When reading a page, for example, the NAND Flash MTD code calls our
876 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
877 * ECC-based or raw view of the page is implicit in which function it calls
878 * (there is a similar pair of ECC-based/raw functions for writing).
879 *
880 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
881 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
882 * caller wants an ECC-based or raw view of the page is not propagated down to
883 * this driver.
884 *
885 * Since our OOB *is* covered by ECC, we need this information. So, we hook the
886 * ecc.read_oob and ecc.write_oob function pointers in the owning
887 * struct mtd_info with our own functions. These hook functions set the
888 * raw_oob_mode field so that, when control finally arrives here, we'll know
889 * what to do.
890 */
891static int mxs_nand_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000892 int page)
Marek Vasut0d4e8502011-11-08 23:18:16 +0000893{
Scott Wood17cb4b82016-05-30 13:57:56 -0500894 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut0d4e8502011-11-08 23:18:16 +0000895
896 /*
897 * First, fill in the OOB buffer. If we're doing a raw read, we need to
898 * get the bytes from the physical page. If we're not doing a raw read,
899 * we need to fill the buffer with set bits.
900 */
901 if (nand_info->raw_oob_mode) {
902 /*
903 * If control arrives here, we're doing a "raw" read. Send the
904 * command to read the conventional OOB and read it.
905 */
906 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
907 nand->read_buf(mtd, nand->oob_poi, mtd->oobsize);
908 } else {
909 /*
910 * If control arrives here, we're not doing a "raw" read. Fill
911 * the OOB buffer with set bits and correct the block mark.
912 */
913 memset(nand->oob_poi, 0xff, mtd->oobsize);
914
915 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
916 mxs_nand_read_buf(mtd, nand->oob_poi, 1);
917 }
918
919 return 0;
920
921}
922
923/*
924 * Write OOB data to NAND.
925 */
926static int mxs_nand_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
927 int page)
928{
Scott Wood17cb4b82016-05-30 13:57:56 -0500929 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Marek Vasut0d4e8502011-11-08 23:18:16 +0000930 uint8_t block_mark = 0;
931
932 /*
933 * There are fundamental incompatibilities between the i.MX GPMI NFC and
934 * the NAND Flash MTD model that make it essentially impossible to write
935 * the out-of-band bytes.
936 *
937 * We permit *ONE* exception. If the *intent* of writing the OOB is to
938 * mark a block bad, we can do that.
939 */
940
941 if (!nand_info->marking_block_bad) {
942 printf("NXS NAND: Writing OOB isn't supported\n");
943 return -EIO;
944 }
945
946 /* Write the block mark. */
947 nand->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
948 nand->write_buf(mtd, &block_mark, 1);
949 nand->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
950
951 /* Check if it worked. */
952 if (nand->waitfunc(mtd, nand) & NAND_STATUS_FAIL)
953 return -EIO;
954
955 return 0;
956}
957
958/*
959 * Claims all blocks are good.
960 *
961 * In principle, this function is *only* called when the NAND Flash MTD system
962 * isn't allowed to keep an in-memory bad block table, so it is forced to ask
963 * the driver for bad block information.
964 *
965 * In fact, we permit the NAND Flash MTD system to have an in-memory BBT, so
966 * this function is *only* called when we take it away.
967 *
968 * Thus, this function is only called when we want *all* blocks to look good,
969 * so it *always* return success.
970 */
Scott Woodceee07b2016-05-30 13:57:58 -0500971static int mxs_nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Marek Vasut0d4e8502011-11-08 23:18:16 +0000972{
973 return 0;
974}
975
976/*
Marek Vasut0d4e8502011-11-08 23:18:16 +0000977 * At this point, the physical NAND Flash chips have been identified and
978 * counted, so we know the physical geometry. This enables us to make some
979 * important configuration decisions.
980 *
Robert P. J. Day62a3b7d2016-07-15 13:44:45 -0400981 * The return value of this function propagates directly back to this driver's
Stefan Agner5346c312018-06-22 17:19:47 +0200982 * board_nand_init(). Anything other than zero will cause this driver to
Marek Vasut0d4e8502011-11-08 23:18:16 +0000983 * tear everything down and declare failure.
984 */
Stefan Agner5346c312018-06-22 17:19:47 +0200985int mxs_nand_setup_ecc(struct mtd_info *mtd)
Marek Vasut0d4e8502011-11-08 23:18:16 +0000986{
Scott Wood17cb4b82016-05-30 13:57:56 -0500987 struct nand_chip *nand = mtd_to_nand(mtd);
988 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
Otavio Salvador9c471142012-08-05 09:05:31 +0000989 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
Marek Vasut0d4e8502011-11-08 23:18:16 +0000990 uint32_t tmp;
991
Peng Fan63b29d82015-07-21 16:15:19 +0800992 if (mtd->oobsize > MXS_NAND_CHUNK_DATA_CHUNK_SIZE) {
993 galois_field = 14;
994 chunk_data_size = MXS_NAND_CHUNK_DATA_CHUNK_SIZE * 2;
995 }
996
997 if (mtd->oobsize > chunk_data_size) {
998 printf("Not support the NAND chips whose oob size is larger then %d bytes!\n", chunk_data_size);
999 return -EINVAL;
1000 }
1001
Marek Vasut0d4e8502011-11-08 23:18:16 +00001002 /* Configure BCH and set NFC geometry */
Otavio Salvadorfa7a51c2012-08-13 09:53:12 +00001003 mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
Marek Vasut0d4e8502011-11-08 23:18:16 +00001004
1005 /* Configure layout 0 */
1006 tmp = (mxs_nand_ecc_chunk_cnt(mtd->writesize) - 1)
1007 << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1008 tmp |= MXS_NAND_METADATA_SIZE << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1009 tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1)
1010 << BCH_FLASHLAYOUT0_ECC0_OFFSET;
Peng Fan63b29d82015-07-21 16:15:19 +08001011 tmp |= chunk_data_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
1012 tmp |= (14 == galois_field ? 1 : 0) <<
1013 BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET;
Marek Vasut0d4e8502011-11-08 23:18:16 +00001014 writel(tmp, &bch_regs->hw_bch_flash0layout0);
1015
1016 tmp = (mtd->writesize + mtd->oobsize)
1017 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
1018 tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1)
1019 << BCH_FLASHLAYOUT1_ECCN_OFFSET;
Peng Fan63b29d82015-07-21 16:15:19 +08001020 tmp |= chunk_data_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
1021 tmp |= (14 == galois_field ? 1 : 0) <<
1022 BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
Marek Vasut0d4e8502011-11-08 23:18:16 +00001023 writel(tmp, &bch_regs->hw_bch_flash0layout1);
1024
1025 /* Set *all* chip selects to use layout 0 */
1026 writel(0, &bch_regs->hw_bch_layoutselect);
1027
1028 /* Enable BCH complete interrupt */
1029 writel(BCH_CTRL_COMPLETE_IRQ_EN, &bch_regs->hw_bch_ctrl_set);
1030
1031 /* Hook some operations at the MTD level. */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001032 if (mtd->_read_oob != mxs_nand_hook_read_oob) {
1033 nand_info->hooked_read_oob = mtd->_read_oob;
1034 mtd->_read_oob = mxs_nand_hook_read_oob;
Marek Vasut0d4e8502011-11-08 23:18:16 +00001035 }
1036
Sergey Lapindfe64e22013-01-14 03:46:50 +00001037 if (mtd->_write_oob != mxs_nand_hook_write_oob) {
1038 nand_info->hooked_write_oob = mtd->_write_oob;
1039 mtd->_write_oob = mxs_nand_hook_write_oob;
Marek Vasut0d4e8502011-11-08 23:18:16 +00001040 }
1041
Sergey Lapindfe64e22013-01-14 03:46:50 +00001042 if (mtd->_block_markbad != mxs_nand_hook_block_markbad) {
1043 nand_info->hooked_block_markbad = mtd->_block_markbad;
1044 mtd->_block_markbad = mxs_nand_hook_block_markbad;
Marek Vasut0d4e8502011-11-08 23:18:16 +00001045 }
1046
Stefan Agner5346c312018-06-22 17:19:47 +02001047 return 0;
Marek Vasut0d4e8502011-11-08 23:18:16 +00001048}
1049
1050/*
1051 * Allocate DMA buffers
1052 */
1053int mxs_nand_alloc_buffers(struct mxs_nand_info *nand_info)
1054{
1055 uint8_t *buf;
1056 const int size = NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE;
1057
Marek Vasut6b9408e2012-03-15 18:33:19 +00001058 nand_info->data_buf_size = roundup(size, MXS_DMA_ALIGNMENT);
1059
Marek Vasut0d4e8502011-11-08 23:18:16 +00001060 /* DMA buffers */
Marek Vasut6b9408e2012-03-15 18:33:19 +00001061 buf = memalign(MXS_DMA_ALIGNMENT, nand_info->data_buf_size);
Marek Vasut0d4e8502011-11-08 23:18:16 +00001062 if (!buf) {
1063 printf("MXS NAND: Error allocating DMA buffers\n");
1064 return -ENOMEM;
1065 }
1066
Marek Vasut6b9408e2012-03-15 18:33:19 +00001067 memset(buf, 0, nand_info->data_buf_size);
Marek Vasut0d4e8502011-11-08 23:18:16 +00001068
1069 nand_info->data_buf = buf;
1070 nand_info->oob_buf = buf + NAND_MAX_PAGESIZE;
Marek Vasut0d4e8502011-11-08 23:18:16 +00001071 /* Command buffers */
1072 nand_info->cmd_buf = memalign(MXS_DMA_ALIGNMENT,
1073 MXS_NAND_COMMAND_BUFFER_SIZE);
1074 if (!nand_info->cmd_buf) {
1075 free(buf);
1076 printf("MXS NAND: Error allocating command buffers\n");
1077 return -ENOMEM;
1078 }
1079 memset(nand_info->cmd_buf, 0, MXS_NAND_COMMAND_BUFFER_SIZE);
1080 nand_info->cmd_queue_len = 0;
1081
1082 return 0;
1083}
1084
1085/*
1086 * Initializes the NFC hardware.
1087 */
1088int mxs_nand_init(struct mxs_nand_info *info)
1089{
Otavio Salvador9c471142012-08-05 09:05:31 +00001090 struct mxs_gpmi_regs *gpmi_regs =
1091 (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
Wolfram Sang0b38fff2012-12-05 10:48:47 +00001092 struct mxs_bch_regs *bch_regs =
1093 (struct mxs_bch_regs *)MXS_BCH_BASE;
Peng Fan549d7c02016-01-27 10:38:02 +08001094 int i = 0, j, ret = 0;
Marek Vasut0d4e8502011-11-08 23:18:16 +00001095
1096 info->desc = malloc(sizeof(struct mxs_dma_desc *) *
1097 MXS_NAND_DMA_DESCRIPTOR_COUNT);
Peng Fan549d7c02016-01-27 10:38:02 +08001098 if (!info->desc) {
1099 ret = -ENOMEM;
Marek Vasut0d4e8502011-11-08 23:18:16 +00001100 goto err1;
Peng Fan549d7c02016-01-27 10:38:02 +08001101 }
Marek Vasut0d4e8502011-11-08 23:18:16 +00001102
1103 /* Allocate the DMA descriptors. */
1104 for (i = 0; i < MXS_NAND_DMA_DESCRIPTOR_COUNT; i++) {
1105 info->desc[i] = mxs_dma_desc_alloc();
Peng Fan549d7c02016-01-27 10:38:02 +08001106 if (!info->desc[i]) {
1107 ret = -ENOMEM;
Marek Vasut0d4e8502011-11-08 23:18:16 +00001108 goto err2;
Peng Fan549d7c02016-01-27 10:38:02 +08001109 }
Marek Vasut0d4e8502011-11-08 23:18:16 +00001110 }
1111
1112 /* Init the DMA controller. */
Fabio Estevama1d1fdc2017-06-29 09:33:44 -03001113 mxs_dma_init();
Marek Vasut96666a32012-04-08 17:34:46 +00001114 for (j = MXS_DMA_CHANNEL_AHB_APBH_GPMI0;
1115 j <= MXS_DMA_CHANNEL_AHB_APBH_GPMI7; j++) {
Peng Fan549d7c02016-01-27 10:38:02 +08001116 ret = mxs_dma_init_channel(j);
1117 if (ret)
Marek Vasut96666a32012-04-08 17:34:46 +00001118 goto err3;
1119 }
Marek Vasut0d4e8502011-11-08 23:18:16 +00001120
1121 /* Reset the GPMI block. */
Otavio Salvadorfa7a51c2012-08-13 09:53:12 +00001122 mxs_reset_block(&gpmi_regs->hw_gpmi_ctrl0_reg);
Wolfram Sang0b38fff2012-12-05 10:48:47 +00001123 mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
Marek Vasut0d4e8502011-11-08 23:18:16 +00001124
1125 /*
1126 * Choose NAND mode, set IRQ polarity, disable write protection and
1127 * select BCH ECC.
1128 */
1129 clrsetbits_le32(&gpmi_regs->hw_gpmi_ctrl1,
1130 GPMI_CTRL1_GPMI_MODE,
1131 GPMI_CTRL1_ATA_IRQRDY_POLARITY | GPMI_CTRL1_DEV_RESET |
1132 GPMI_CTRL1_BCH_MODE);
1133
1134 return 0;
1135
Marek Vasut96666a32012-04-08 17:34:46 +00001136err3:
Peng Fan549d7c02016-01-27 10:38:02 +08001137 for (--j; j >= MXS_DMA_CHANNEL_AHB_APBH_GPMI0; j--)
Marek Vasut96666a32012-04-08 17:34:46 +00001138 mxs_dma_release(j);
Marek Vasut0d4e8502011-11-08 23:18:16 +00001139err2:
Marek Vasut0d4e8502011-11-08 23:18:16 +00001140 for (--i; i >= 0; i--)
1141 mxs_dma_desc_free(info->desc[i]);
Peng Fan549d7c02016-01-27 10:38:02 +08001142 free(info->desc);
1143err1:
1144 if (ret == -ENOMEM)
1145 printf("MXS NAND: Unable to allocate DMA descriptors\n");
1146 return ret;
Marek Vasut0d4e8502011-11-08 23:18:16 +00001147}
1148
Stefan Agner93459432018-06-22 17:19:46 +02001149int mxs_nand_init_spl(struct nand_chip *nand)
1150{
1151 struct mxs_nand_info *nand_info;
1152 int err;
1153
1154 nand_info = malloc(sizeof(struct mxs_nand_info));
1155 if (!nand_info) {
1156 printf("MXS NAND: Failed to allocate private data\n");
1157 return -ENOMEM;
1158 }
1159 memset(nand_info, 0, sizeof(struct mxs_nand_info));
1160
1161 err = mxs_nand_alloc_buffers(nand_info);
1162 if (err)
1163 return err;
1164
1165 err = mxs_nand_init(nand_info);
1166 if (err)
1167 return err;
1168
1169 nand_set_controller_data(nand, nand_info);
1170
1171 nand->options |= NAND_NO_SUBPAGE_WRITE;
1172
1173 nand->cmd_ctrl = mxs_nand_cmd_ctrl;
1174 nand->dev_ready = mxs_nand_device_ready;
1175 nand->select_chip = mxs_nand_select_chip;
Stefan Agner93459432018-06-22 17:19:46 +02001176
1177 nand->read_byte = mxs_nand_read_byte;
1178 nand->read_buf = mxs_nand_read_buf;
1179
1180 nand->ecc.read_page = mxs_nand_ecc_read_page;
1181
1182 nand->ecc.mode = NAND_ECC_HW;
1183 nand->ecc.bytes = 9;
1184 nand->ecc.size = 512;
1185 nand->ecc.strength = 8;
1186
1187 return 0;
1188}
1189
Stefan Agner5346c312018-06-22 17:19:47 +02001190void board_nand_init(void)
Marek Vasut0d4e8502011-11-08 23:18:16 +00001191{
Stefan Agner5346c312018-06-22 17:19:47 +02001192 struct mtd_info *mtd;
Marek Vasut0d4e8502011-11-08 23:18:16 +00001193 struct mxs_nand_info *nand_info;
Stefan Agner5346c312018-06-22 17:19:47 +02001194 struct nand_chip *nand;
Marek Vasut0d4e8502011-11-08 23:18:16 +00001195 int err;
1196
1197 nand_info = malloc(sizeof(struct mxs_nand_info));
1198 if (!nand_info) {
1199 printf("MXS NAND: Failed to allocate private data\n");
Stefan Agner5346c312018-06-22 17:19:47 +02001200 return;
Marek Vasut0d4e8502011-11-08 23:18:16 +00001201 }
1202 memset(nand_info, 0, sizeof(struct mxs_nand_info));
1203
Stefan Agner5346c312018-06-22 17:19:47 +02001204 nand = &nand_info->chip;
1205 mtd = nand_to_mtd(nand);
Marek Vasut0d4e8502011-11-08 23:18:16 +00001206 err = mxs_nand_alloc_buffers(nand_info);
1207 if (err)
1208 goto err1;
1209
1210 err = mxs_nand_init(nand_info);
1211 if (err)
1212 goto err2;
1213
1214 memset(&fake_ecc_layout, 0, sizeof(fake_ecc_layout));
1215
Stefan Agnerdc0b69f2018-06-22 17:19:48 +02001216#ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1217 nand->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
1218#endif
1219
Scott Wood17cb4b82016-05-30 13:57:56 -05001220 nand_set_controller_data(nand, nand_info);
Marek Vasut0d4e8502011-11-08 23:18:16 +00001221 nand->options |= NAND_NO_SUBPAGE_WRITE;
1222
1223 nand->cmd_ctrl = mxs_nand_cmd_ctrl;
1224
1225 nand->dev_ready = mxs_nand_device_ready;
1226 nand->select_chip = mxs_nand_select_chip;
1227 nand->block_bad = mxs_nand_block_bad;
Marek Vasut0d4e8502011-11-08 23:18:16 +00001228
1229 nand->read_byte = mxs_nand_read_byte;
1230
1231 nand->read_buf = mxs_nand_read_buf;
1232 nand->write_buf = mxs_nand_write_buf;
1233
Stefan Agner5346c312018-06-22 17:19:47 +02001234 /* first scan to find the device and get the page size */
1235 if (nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_DEVICE, NULL))
1236 goto err2;
1237
1238 if (mxs_nand_setup_ecc(mtd))
1239 goto err2;
1240
Marek Vasut0d4e8502011-11-08 23:18:16 +00001241 nand->ecc.read_page = mxs_nand_ecc_read_page;
1242 nand->ecc.write_page = mxs_nand_ecc_write_page;
1243 nand->ecc.read_oob = mxs_nand_ecc_read_oob;
1244 nand->ecc.write_oob = mxs_nand_ecc_write_oob;
1245
1246 nand->ecc.layout = &fake_ecc_layout;
1247 nand->ecc.mode = NAND_ECC_HW;
1248 nand->ecc.bytes = 9;
1249 nand->ecc.size = 512;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001250 nand->ecc.strength = 8;
Marek Vasut0d4e8502011-11-08 23:18:16 +00001251
Stefan Agner5346c312018-06-22 17:19:47 +02001252 /* second phase scan */
1253 err = nand_scan_tail(mtd);
1254 if (err)
1255 goto err2;
1256
1257 err = nand_register(0, mtd);
1258 if (err)
1259 goto err2;
1260
1261 return;
Marek Vasut0d4e8502011-11-08 23:18:16 +00001262
1263err2:
1264 free(nand_info->data_buf);
1265 free(nand_info->cmd_buf);
1266err1:
1267 free(nand_info);
Stefan Agner5346c312018-06-22 17:19:47 +02001268 return;
Marek Vasut0d4e8502011-11-08 23:18:16 +00001269}