Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Freescale i.MX28 NAND flash driver |
| 4 | * |
| 5 | * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com> |
| 6 | * on behalf of DENX Software Engineering GmbH |
| 7 | * |
| 8 | * Based on code from LTIB: |
| 9 | * Freescale GPMI NFC NAND Flash Driver |
| 10 | * |
| 11 | * Copyright (C) 2010 Freescale Semiconductor, Inc. |
| 12 | * Copyright (C) 2008 Embedded Alley Solutions, Inc. |
Peng Fan | 9e81373 | 2020-05-04 22:08:53 +0800 | [diff] [blame] | 13 | * Copyright 2017-2019 NXP |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Tom Warren | c88d30f | 2012-09-10 08:47:51 -0700 | [diff] [blame] | 16 | #include <common.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 17 | #include <cpu_func.h> |
Stefan Agner | 19f9051 | 2018-06-22 18:06:16 +0200 | [diff] [blame] | 18 | #include <dm.h> |
Masahiro Yamada | 2b7a873 | 2017-11-30 13:45:24 +0900 | [diff] [blame] | 19 | #include <linux/mtd/rawnand.h> |
Stefan Agner | 4d42ac1 | 2018-06-22 17:19:51 +0200 | [diff] [blame] | 20 | #include <linux/sizes.h> |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 21 | #include <linux/types.h> |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 22 | #include <malloc.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 23 | #include <linux/errno.h> |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 24 | #include <asm/io.h> |
| 25 | #include <asm/arch/clock.h> |
| 26 | #include <asm/arch/imx-regs.h> |
Stefano Babic | 33731bc | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 27 | #include <asm/mach-imx/regs-bch.h> |
| 28 | #include <asm/mach-imx/regs-gpmi.h> |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 29 | #include <asm/arch/sys_proto.h> |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 30 | #include <mxs_nand.h> |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 31 | |
| 32 | #define MXS_NAND_DMA_DESCRIPTOR_COUNT 4 |
| 33 | |
Peng Fan | 128abf4 | 2020-05-04 22:09:00 +0800 | [diff] [blame] | 34 | #if defined(CONFIG_MX6) || defined(CONFIG_MX7) || defined(CONFIG_IMX8) || \ |
| 35 | defined(CONFIG_IMX8M) |
Stefan Roese | 8338d1d | 2013-04-15 21:14:12 +0000 | [diff] [blame] | 36 | #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT 2 |
| 37 | #else |
| 38 | #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT 0 |
| 39 | #endif |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 40 | #define MXS_NAND_METADATA_SIZE 10 |
Jörg Krause | 1d87026 | 2015-04-15 09:27:22 +0200 | [diff] [blame] | 41 | #define MXS_NAND_BITS_PER_ECC_LEVEL 13 |
Stefan Agner | 54bf808 | 2016-08-01 23:55:18 -0700 | [diff] [blame] | 42 | |
| 43 | #if !defined(CONFIG_SYS_CACHELINE_SIZE) || CONFIG_SYS_CACHELINE_SIZE < 32 |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 44 | #define MXS_NAND_COMMAND_BUFFER_SIZE 32 |
Stefan Agner | 54bf808 | 2016-08-01 23:55:18 -0700 | [diff] [blame] | 45 | #else |
| 46 | #define MXS_NAND_COMMAND_BUFFER_SIZE CONFIG_SYS_CACHELINE_SIZE |
| 47 | #endif |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 48 | |
| 49 | #define MXS_NAND_BCH_TIMEOUT 10000 |
| 50 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 51 | struct nand_ecclayout fake_ecc_layout; |
| 52 | |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 53 | /* |
| 54 | * Cache management functions |
| 55 | */ |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 56 | #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 57 | static void mxs_nand_flush_data_buf(struct mxs_nand_info *info) |
| 58 | { |
Peng Fan | 128abf4 | 2020-05-04 22:09:00 +0800 | [diff] [blame] | 59 | uint32_t addr = (uintptr_t)info->data_buf; |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 60 | |
| 61 | flush_dcache_range(addr, addr + info->data_buf_size); |
| 62 | } |
| 63 | |
| 64 | static void mxs_nand_inval_data_buf(struct mxs_nand_info *info) |
| 65 | { |
Peng Fan | 128abf4 | 2020-05-04 22:09:00 +0800 | [diff] [blame] | 66 | uint32_t addr = (uintptr_t)info->data_buf; |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 67 | |
| 68 | invalidate_dcache_range(addr, addr + info->data_buf_size); |
| 69 | } |
| 70 | |
| 71 | static void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info) |
| 72 | { |
Peng Fan | 128abf4 | 2020-05-04 22:09:00 +0800 | [diff] [blame] | 73 | uint32_t addr = (uintptr_t)info->cmd_buf; |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 74 | |
| 75 | flush_dcache_range(addr, addr + MXS_NAND_COMMAND_BUFFER_SIZE); |
| 76 | } |
| 77 | #else |
| 78 | static inline void mxs_nand_flush_data_buf(struct mxs_nand_info *info) {} |
| 79 | static inline void mxs_nand_inval_data_buf(struct mxs_nand_info *info) {} |
| 80 | static inline void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info) {} |
| 81 | #endif |
| 82 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 83 | static struct mxs_dma_desc *mxs_nand_get_dma_desc(struct mxs_nand_info *info) |
| 84 | { |
| 85 | struct mxs_dma_desc *desc; |
| 86 | |
| 87 | if (info->desc_index >= MXS_NAND_DMA_DESCRIPTOR_COUNT) { |
| 88 | printf("MXS NAND: Too many DMA descriptors requested\n"); |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | desc = info->desc[info->desc_index]; |
| 93 | info->desc_index++; |
| 94 | |
| 95 | return desc; |
| 96 | } |
| 97 | |
| 98 | static void mxs_nand_return_dma_descs(struct mxs_nand_info *info) |
| 99 | { |
| 100 | int i; |
| 101 | struct mxs_dma_desc *desc; |
| 102 | |
| 103 | for (i = 0; i < info->desc_index; i++) { |
| 104 | desc = info->desc[i]; |
| 105 | memset(desc, 0, sizeof(struct mxs_dma_desc)); |
| 106 | desc->address = (dma_addr_t)desc; |
| 107 | } |
| 108 | |
| 109 | info->desc_index = 0; |
| 110 | } |
| 111 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 112 | static uint32_t mxs_nand_aux_status_offset(void) |
| 113 | { |
| 114 | return (MXS_NAND_METADATA_SIZE + 0x3) & ~0x3; |
| 115 | } |
| 116 | |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 117 | static inline bool mxs_nand_bbm_in_data_chunk(struct bch_geometry *geo, struct mtd_info *mtd, |
| 118 | unsigned int *chunk_num) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 119 | { |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 120 | unsigned int i, j; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 121 | |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 122 | if (geo->ecc_chunk0_size != geo->ecc_chunkn_size) { |
| 123 | dev_err(this->dev, "The size of chunk0 must equal to chunkn\n"); |
| 124 | return false; |
| 125 | } |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 126 | |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 127 | i = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8) / |
| 128 | (geo->gf_len * geo->ecc_strength + |
| 129 | geo->ecc_chunkn_size * 8); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 130 | |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 131 | j = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8) - |
| 132 | (geo->gf_len * geo->ecc_strength + |
| 133 | geo->ecc_chunkn_size * 8) * i; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 134 | |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 135 | if (j < geo->ecc_chunkn_size * 8) { |
| 136 | *chunk_num = i + 1; |
| 137 | dev_dbg(this->dev, "Set ecc to %d and bbm in chunk %d\n", |
| 138 | geo->ecc_strength, *chunk_num); |
| 139 | return true; |
| 140 | } |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 141 | |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 142 | return false; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Stefan Agner | 4d42ac1 | 2018-06-22 17:19:51 +0200 | [diff] [blame] | 145 | static inline int mxs_nand_calc_ecc_layout_by_info(struct bch_geometry *geo, |
Stefan Agner | ead66eb | 2018-06-22 18:06:18 +0200 | [diff] [blame] | 146 | struct mtd_info *mtd, |
| 147 | unsigned int ecc_strength, |
| 148 | unsigned int ecc_step) |
Stefan Agner | 4d42ac1 | 2018-06-22 17:19:51 +0200 | [diff] [blame] | 149 | { |
| 150 | struct nand_chip *chip = mtd_to_nand(mtd); |
Stefan Agner | 4dc98db | 2018-06-22 18:06:15 +0200 | [diff] [blame] | 151 | struct mxs_nand_info *nand_info = nand_get_controller_data(chip); |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 152 | unsigned int block_mark_bit_offset; |
Stefan Agner | 4d42ac1 | 2018-06-22 17:19:51 +0200 | [diff] [blame] | 153 | |
Stefan Agner | ead66eb | 2018-06-22 18:06:18 +0200 | [diff] [blame] | 154 | switch (ecc_step) { |
Stefan Agner | 4d42ac1 | 2018-06-22 17:19:51 +0200 | [diff] [blame] | 155 | case SZ_512: |
| 156 | geo->gf_len = 13; |
| 157 | break; |
| 158 | case SZ_1K: |
| 159 | geo->gf_len = 14; |
| 160 | break; |
| 161 | default: |
| 162 | return -EINVAL; |
| 163 | } |
| 164 | |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 165 | geo->ecc_chunk0_size = ecc_step; |
| 166 | geo->ecc_chunkn_size = ecc_step; |
Stefan Agner | ead66eb | 2018-06-22 18:06:18 +0200 | [diff] [blame] | 167 | geo->ecc_strength = round_up(ecc_strength, 2); |
Stefan Agner | 4d42ac1 | 2018-06-22 17:19:51 +0200 | [diff] [blame] | 168 | |
| 169 | /* Keep the C >= O */ |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 170 | if (geo->ecc_chunkn_size < mtd->oobsize) |
Stefan Agner | 4d42ac1 | 2018-06-22 17:19:51 +0200 | [diff] [blame] | 171 | return -EINVAL; |
| 172 | |
Stefan Agner | 4dc98db | 2018-06-22 18:06:15 +0200 | [diff] [blame] | 173 | if (geo->ecc_strength > nand_info->max_ecc_strength_supported) |
Stefan Agner | 4d42ac1 | 2018-06-22 17:19:51 +0200 | [diff] [blame] | 174 | return -EINVAL; |
| 175 | |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 176 | geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size; |
| 177 | |
| 178 | /* For bit swap. */ |
| 179 | block_mark_bit_offset = mtd->writesize * 8 - |
| 180 | (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1) |
| 181 | + MXS_NAND_METADATA_SIZE * 8); |
| 182 | |
| 183 | geo->block_mark_byte_offset = block_mark_bit_offset / 8; |
| 184 | geo->block_mark_bit_offset = block_mark_bit_offset % 8; |
Stefan Agner | 4d42ac1 | 2018-06-22 17:19:51 +0200 | [diff] [blame] | 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 189 | static inline int mxs_nand_legacy_calc_ecc_layout(struct bch_geometry *geo, |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 190 | struct mtd_info *mtd) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 191 | { |
Stefan Agner | 4dc98db | 2018-06-22 18:06:15 +0200 | [diff] [blame] | 192 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 193 | struct mxs_nand_info *nand_info = nand_get_controller_data(chip); |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 194 | unsigned int block_mark_bit_offset; |
Stefan Agner | 4dc98db | 2018-06-22 18:06:15 +0200 | [diff] [blame] | 195 | |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 196 | /* The default for the length of Galois Field. */ |
| 197 | geo->gf_len = 13; |
| 198 | |
| 199 | /* The default for chunk size. */ |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 200 | geo->ecc_chunk0_size = 512; |
| 201 | geo->ecc_chunkn_size = 512; |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 202 | |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 203 | if (geo->ecc_chunkn_size < mtd->oobsize) { |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 204 | geo->gf_len = 14; |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 205 | geo->ecc_chunk0_size *= 2; |
| 206 | geo->ecc_chunkn_size *= 2; |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 209 | geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size; |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 210 | |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 211 | /* |
| 212 | * Determine the ECC layout with the formula: |
| 213 | * ECC bits per chunk = (total page spare data bits) / |
| 214 | * (bits per ECC level) / (chunks per page) |
| 215 | * where: |
| 216 | * total page spare data bits = |
| 217 | * (page oob size - meta data size) * (bits per byte) |
| 218 | */ |
| 219 | geo->ecc_strength = ((mtd->oobsize - MXS_NAND_METADATA_SIZE) * 8) |
| 220 | / (geo->gf_len * geo->ecc_chunk_count); |
| 221 | |
Stefan Agner | 4d42ac1 | 2018-06-22 17:19:51 +0200 | [diff] [blame] | 222 | geo->ecc_strength = min(round_down(geo->ecc_strength, 2), |
Stefan Agner | 4dc98db | 2018-06-22 18:06:15 +0200 | [diff] [blame] | 223 | nand_info->max_ecc_strength_supported); |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 224 | |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 225 | block_mark_bit_offset = mtd->writesize * 8 - |
| 226 | (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1) |
| 227 | + MXS_NAND_METADATA_SIZE * 8); |
| 228 | |
| 229 | geo->block_mark_byte_offset = block_mark_bit_offset / 8; |
| 230 | geo->block_mark_bit_offset = block_mark_bit_offset % 8; |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static inline int mxs_nand_calc_ecc_for_large_oob(struct bch_geometry *geo, |
| 236 | struct mtd_info *mtd) |
| 237 | { |
| 238 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 239 | struct mxs_nand_info *nand_info = nand_get_controller_data(chip); |
| 240 | unsigned int block_mark_bit_offset; |
| 241 | unsigned int max_ecc; |
| 242 | unsigned int bbm_chunk; |
| 243 | unsigned int i; |
| 244 | |
| 245 | /* sanity check for the minimum ecc nand required */ |
| 246 | if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0)) |
| 247 | return -EINVAL; |
| 248 | geo->ecc_strength = chip->ecc_strength_ds; |
| 249 | |
| 250 | /* calculate the maximum ecc platform can support*/ |
| 251 | geo->gf_len = 14; |
| 252 | geo->ecc_chunk0_size = 1024; |
| 253 | geo->ecc_chunkn_size = 1024; |
| 254 | geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size; |
| 255 | max_ecc = ((mtd->oobsize - MXS_NAND_METADATA_SIZE) * 8) |
| 256 | / (geo->gf_len * geo->ecc_chunk_count); |
| 257 | max_ecc = min(round_down(max_ecc, 2), |
| 258 | nand_info->max_ecc_strength_supported); |
| 259 | |
| 260 | |
| 261 | /* search a supported ecc strength that makes bbm */ |
| 262 | /* located in data chunk */ |
| 263 | geo->ecc_strength = chip->ecc_strength_ds; |
| 264 | while (!(geo->ecc_strength > max_ecc)) { |
| 265 | if (mxs_nand_bbm_in_data_chunk(geo, mtd, &bbm_chunk)) |
| 266 | break; |
| 267 | geo->ecc_strength += 2; |
| 268 | } |
| 269 | |
| 270 | /* if none of them works, keep using the minimum ecc */ |
| 271 | /* nand required but changing ecc page layout */ |
| 272 | if (geo->ecc_strength > max_ecc) { |
| 273 | geo->ecc_strength = chip->ecc_strength_ds; |
| 274 | /* add extra ecc for meta data */ |
| 275 | geo->ecc_chunk0_size = 0; |
| 276 | geo->ecc_chunk_count = (mtd->writesize / geo->ecc_chunkn_size) + 1; |
| 277 | geo->ecc_for_meta = 1; |
| 278 | /* check if oob can afford this extra ecc chunk */ |
| 279 | if (mtd->oobsize * 8 < MXS_NAND_METADATA_SIZE * 8 + |
| 280 | geo->gf_len * geo->ecc_strength |
| 281 | * geo->ecc_chunk_count) { |
| 282 | printf("unsupported NAND chip with new layout\n"); |
| 283 | return -EINVAL; |
| 284 | } |
| 285 | |
| 286 | /* calculate in which chunk bbm located */ |
| 287 | bbm_chunk = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8 - |
| 288 | geo->gf_len * geo->ecc_strength) / |
| 289 | (geo->gf_len * geo->ecc_strength + |
| 290 | geo->ecc_chunkn_size * 8) + 1; |
| 291 | } |
| 292 | |
| 293 | /* calculate the number of ecc chunk behind the bbm */ |
| 294 | i = (mtd->writesize / geo->ecc_chunkn_size) - bbm_chunk + 1; |
| 295 | |
| 296 | block_mark_bit_offset = mtd->writesize * 8 - |
| 297 | (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - i) |
| 298 | + MXS_NAND_METADATA_SIZE * 8); |
| 299 | |
| 300 | geo->block_mark_byte_offset = block_mark_bit_offset / 8; |
| 301 | geo->block_mark_bit_offset = block_mark_bit_offset % 8; |
| 302 | |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 303 | return 0; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Wait for BCH complete IRQ and clear the IRQ |
| 308 | */ |
Stefan Agner | dc8af6d | 2018-06-22 18:06:12 +0200 | [diff] [blame] | 309 | static int mxs_nand_wait_for_bch_complete(struct mxs_nand_info *nand_info) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 310 | { |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 311 | int timeout = MXS_NAND_BCH_TIMEOUT; |
| 312 | int ret; |
| 313 | |
Stefan Agner | dc8af6d | 2018-06-22 18:06:12 +0200 | [diff] [blame] | 314 | ret = mxs_wait_mask_set(&nand_info->bch_regs->hw_bch_ctrl_reg, |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 315 | BCH_CTRL_COMPLETE_IRQ, timeout); |
| 316 | |
Stefan Agner | dc8af6d | 2018-06-22 18:06:12 +0200 | [diff] [blame] | 317 | writel(BCH_CTRL_COMPLETE_IRQ, &nand_info->bch_regs->hw_bch_ctrl_clr); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 318 | |
| 319 | return ret; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * This is the function that we install in the cmd_ctrl function pointer of the |
| 324 | * owning struct nand_chip. The only functions in the reference implementation |
| 325 | * that use these functions pointers are cmdfunc and select_chip. |
| 326 | * |
| 327 | * In this driver, we implement our own select_chip, so this function will only |
| 328 | * be called by the reference implementation's cmdfunc. For this reason, we can |
| 329 | * ignore the chip enable bit and concentrate only on sending bytes to the NAND |
| 330 | * Flash. |
| 331 | */ |
| 332 | static void mxs_nand_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl) |
| 333 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 334 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 335 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 336 | struct mxs_dma_desc *d; |
| 337 | uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip; |
| 338 | int ret; |
| 339 | |
| 340 | /* |
| 341 | * If this condition is true, something is _VERY_ wrong in MTD |
| 342 | * subsystem! |
| 343 | */ |
| 344 | if (nand_info->cmd_queue_len == MXS_NAND_COMMAND_BUFFER_SIZE) { |
| 345 | printf("MXS NAND: Command queue too long\n"); |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Every operation begins with a command byte and a series of zero or |
| 351 | * more address bytes. These are distinguished by either the Address |
| 352 | * Latch Enable (ALE) or Command Latch Enable (CLE) signals being |
| 353 | * asserted. When MTD is ready to execute the command, it will |
| 354 | * deasert both latch enables. |
| 355 | * |
| 356 | * Rather than run a separate DMA operation for every single byte, we |
| 357 | * queue them up and run a single DMA operation for the entire series |
| 358 | * of command and data bytes. |
| 359 | */ |
| 360 | if (ctrl & (NAND_ALE | NAND_CLE)) { |
| 361 | if (data != NAND_CMD_NONE) |
| 362 | nand_info->cmd_buf[nand_info->cmd_queue_len++] = data; |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | /* |
| 367 | * If control arrives here, MTD has deasserted both the ALE and CLE, |
| 368 | * which means it's ready to run an operation. Check if we have any |
| 369 | * bytes to send. |
| 370 | */ |
| 371 | if (nand_info->cmd_queue_len == 0) |
| 372 | return; |
| 373 | |
| 374 | /* Compile the DMA descriptor -- a descriptor that sends command. */ |
| 375 | d = mxs_nand_get_dma_desc(nand_info); |
| 376 | d->cmd.data = |
| 377 | MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ | |
| 378 | MXS_DMA_DESC_CHAIN | MXS_DMA_DESC_DEC_SEM | |
| 379 | MXS_DMA_DESC_WAIT4END | (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET) | |
| 380 | (nand_info->cmd_queue_len << MXS_DMA_DESC_BYTES_OFFSET); |
| 381 | |
| 382 | d->cmd.address = (dma_addr_t)nand_info->cmd_buf; |
| 383 | |
| 384 | d->cmd.pio_words[0] = |
| 385 | GPMI_CTRL0_COMMAND_MODE_WRITE | |
| 386 | GPMI_CTRL0_WORD_LENGTH | |
| 387 | (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) | |
| 388 | GPMI_CTRL0_ADDRESS_NAND_CLE | |
| 389 | GPMI_CTRL0_ADDRESS_INCREMENT | |
| 390 | nand_info->cmd_queue_len; |
| 391 | |
| 392 | mxs_dma_desc_append(channel, d); |
| 393 | |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 394 | /* Flush caches */ |
| 395 | mxs_nand_flush_cmd_buf(nand_info); |
| 396 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 397 | /* Execute the DMA chain. */ |
| 398 | ret = mxs_dma_go(channel); |
| 399 | if (ret) |
| 400 | printf("MXS NAND: Error sending command\n"); |
| 401 | |
| 402 | mxs_nand_return_dma_descs(nand_info); |
| 403 | |
| 404 | /* Reset the command queue. */ |
| 405 | nand_info->cmd_queue_len = 0; |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Test if the NAND flash is ready. |
| 410 | */ |
| 411 | static int mxs_nand_device_ready(struct mtd_info *mtd) |
| 412 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 413 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 414 | struct mxs_nand_info *nand_info = nand_get_controller_data(chip); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 415 | uint32_t tmp; |
| 416 | |
Stefan Agner | dc8af6d | 2018-06-22 18:06:12 +0200 | [diff] [blame] | 417 | tmp = readl(&nand_info->gpmi_regs->hw_gpmi_stat); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 418 | tmp >>= (GPMI_STAT_READY_BUSY_OFFSET + nand_info->cur_chip); |
| 419 | |
| 420 | return tmp & 1; |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * Select the NAND chip. |
| 425 | */ |
| 426 | static void mxs_nand_select_chip(struct mtd_info *mtd, int chip) |
| 427 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 428 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 429 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 430 | |
| 431 | nand_info->cur_chip = chip; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Handle block mark swapping. |
| 436 | * |
| 437 | * Note that, when this function is called, it doesn't know whether it's |
| 438 | * swapping the block mark, or swapping it *back* -- but it doesn't matter |
| 439 | * because the the operation is the same. |
| 440 | */ |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 441 | static void mxs_nand_swap_block_mark(struct bch_geometry *geo, |
| 442 | uint8_t *data_buf, uint8_t *oob_buf) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 443 | { |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 444 | uint32_t bit_offset = geo->block_mark_bit_offset; |
| 445 | uint32_t buf_offset = geo->block_mark_byte_offset; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 446 | |
| 447 | uint32_t src; |
| 448 | uint32_t dst; |
| 449 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 450 | /* |
| 451 | * Get the byte from the data area that overlays the block mark. Since |
| 452 | * the ECC engine applies its own view to the bits in the page, the |
| 453 | * physical block mark won't (in general) appear on a byte boundary in |
| 454 | * the data. |
| 455 | */ |
| 456 | src = data_buf[buf_offset] >> bit_offset; |
| 457 | src |= data_buf[buf_offset + 1] << (8 - bit_offset); |
| 458 | |
| 459 | dst = oob_buf[0]; |
| 460 | |
| 461 | oob_buf[0] = src; |
| 462 | |
| 463 | data_buf[buf_offset] &= ~(0xff << bit_offset); |
| 464 | data_buf[buf_offset + 1] &= 0xff << bit_offset; |
| 465 | |
| 466 | data_buf[buf_offset] |= dst << bit_offset; |
| 467 | data_buf[buf_offset + 1] |= dst >> (8 - bit_offset); |
| 468 | } |
| 469 | |
| 470 | /* |
| 471 | * Read data from NAND. |
| 472 | */ |
| 473 | static void mxs_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int length) |
| 474 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 475 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 476 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 477 | struct mxs_dma_desc *d; |
| 478 | uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip; |
| 479 | int ret; |
| 480 | |
| 481 | if (length > NAND_MAX_PAGESIZE) { |
| 482 | printf("MXS NAND: DMA buffer too big\n"); |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | if (!buf) { |
| 487 | printf("MXS NAND: DMA buffer is NULL\n"); |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | /* Compile the DMA descriptor - a descriptor that reads data. */ |
| 492 | d = mxs_nand_get_dma_desc(nand_info); |
| 493 | d->cmd.data = |
| 494 | MXS_DMA_DESC_COMMAND_DMA_WRITE | MXS_DMA_DESC_IRQ | |
| 495 | MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END | |
| 496 | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) | |
| 497 | (length << MXS_DMA_DESC_BYTES_OFFSET); |
| 498 | |
| 499 | d->cmd.address = (dma_addr_t)nand_info->data_buf; |
| 500 | |
| 501 | d->cmd.pio_words[0] = |
| 502 | GPMI_CTRL0_COMMAND_MODE_READ | |
| 503 | GPMI_CTRL0_WORD_LENGTH | |
| 504 | (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) | |
| 505 | GPMI_CTRL0_ADDRESS_NAND_DATA | |
| 506 | length; |
| 507 | |
| 508 | mxs_dma_desc_append(channel, d); |
| 509 | |
| 510 | /* |
| 511 | * A DMA descriptor that waits for the command to end and the chip to |
| 512 | * become ready. |
| 513 | * |
| 514 | * I think we actually should *not* be waiting for the chip to become |
| 515 | * ready because, after all, we don't care. I think the original code |
| 516 | * did that and no one has re-thought it yet. |
| 517 | */ |
| 518 | d = mxs_nand_get_dma_desc(nand_info); |
| 519 | d->cmd.data = |
| 520 | MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ | |
| 521 | MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_DEC_SEM | |
Luca Ellero | 80f06b8 | 2014-12-16 15:36:14 +0100 | [diff] [blame] | 522 | MXS_DMA_DESC_WAIT4END | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 523 | |
| 524 | d->cmd.address = 0; |
| 525 | |
| 526 | d->cmd.pio_words[0] = |
| 527 | GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY | |
| 528 | GPMI_CTRL0_WORD_LENGTH | |
| 529 | (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) | |
| 530 | GPMI_CTRL0_ADDRESS_NAND_DATA; |
| 531 | |
| 532 | mxs_dma_desc_append(channel, d); |
| 533 | |
Peng Fan | e3bbfb7 | 2015-07-21 16:15:21 +0800 | [diff] [blame] | 534 | /* Invalidate caches */ |
| 535 | mxs_nand_inval_data_buf(nand_info); |
| 536 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 537 | /* Execute the DMA chain. */ |
| 538 | ret = mxs_dma_go(channel); |
| 539 | if (ret) { |
| 540 | printf("MXS NAND: DMA read error\n"); |
| 541 | goto rtn; |
| 542 | } |
| 543 | |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 544 | /* Invalidate caches */ |
| 545 | mxs_nand_inval_data_buf(nand_info); |
| 546 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 547 | memcpy(buf, nand_info->data_buf, length); |
| 548 | |
| 549 | rtn: |
| 550 | mxs_nand_return_dma_descs(nand_info); |
| 551 | } |
| 552 | |
| 553 | /* |
| 554 | * Write data to NAND. |
| 555 | */ |
| 556 | static void mxs_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, |
| 557 | int length) |
| 558 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 559 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 560 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 561 | struct mxs_dma_desc *d; |
| 562 | uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip; |
| 563 | int ret; |
| 564 | |
| 565 | if (length > NAND_MAX_PAGESIZE) { |
| 566 | printf("MXS NAND: DMA buffer too big\n"); |
| 567 | return; |
| 568 | } |
| 569 | |
| 570 | if (!buf) { |
| 571 | printf("MXS NAND: DMA buffer is NULL\n"); |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | memcpy(nand_info->data_buf, buf, length); |
| 576 | |
| 577 | /* Compile the DMA descriptor - a descriptor that writes data. */ |
| 578 | d = mxs_nand_get_dma_desc(nand_info); |
| 579 | d->cmd.data = |
| 580 | MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ | |
| 581 | MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END | |
Luca Ellero | 966f1cd | 2014-12-16 15:36:15 +0100 | [diff] [blame] | 582 | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 583 | (length << MXS_DMA_DESC_BYTES_OFFSET); |
| 584 | |
| 585 | d->cmd.address = (dma_addr_t)nand_info->data_buf; |
| 586 | |
| 587 | d->cmd.pio_words[0] = |
| 588 | GPMI_CTRL0_COMMAND_MODE_WRITE | |
| 589 | GPMI_CTRL0_WORD_LENGTH | |
| 590 | (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) | |
| 591 | GPMI_CTRL0_ADDRESS_NAND_DATA | |
| 592 | length; |
| 593 | |
| 594 | mxs_dma_desc_append(channel, d); |
| 595 | |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 596 | /* Flush caches */ |
| 597 | mxs_nand_flush_data_buf(nand_info); |
| 598 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 599 | /* Execute the DMA chain. */ |
| 600 | ret = mxs_dma_go(channel); |
| 601 | if (ret) |
| 602 | printf("MXS NAND: DMA write error\n"); |
| 603 | |
| 604 | mxs_nand_return_dma_descs(nand_info); |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | * Read a single byte from NAND. |
| 609 | */ |
| 610 | static uint8_t mxs_nand_read_byte(struct mtd_info *mtd) |
| 611 | { |
| 612 | uint8_t buf; |
| 613 | mxs_nand_read_buf(mtd, &buf, 1); |
| 614 | return buf; |
| 615 | } |
| 616 | |
Peng Fan | df23c9d | 2020-05-04 22:08:52 +0800 | [diff] [blame] | 617 | static bool mxs_nand_erased_page(struct mtd_info *mtd, struct nand_chip *nand, |
| 618 | u8 *buf, int chunk, int page) |
| 619 | { |
| 620 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
| 621 | struct bch_geometry *geo = &nand_info->bch_geometry; |
| 622 | unsigned int flip_bits = 0, flip_bits_noecc = 0; |
| 623 | unsigned int threshold; |
| 624 | unsigned int base = geo->ecc_chunkn_size * chunk; |
| 625 | u32 *dma_buf = (u32 *)buf; |
| 626 | int i; |
| 627 | |
| 628 | threshold = geo->gf_len / 2; |
| 629 | if (threshold > geo->ecc_strength) |
| 630 | threshold = geo->ecc_strength; |
| 631 | |
| 632 | for (i = 0; i < geo->ecc_chunkn_size; i++) { |
| 633 | flip_bits += hweight8(~buf[base + i]); |
| 634 | if (flip_bits > threshold) |
| 635 | return false; |
| 636 | } |
| 637 | |
| 638 | nand->cmdfunc(mtd, NAND_CMD_READ0, 0, page); |
| 639 | nand->read_buf(mtd, buf, mtd->writesize); |
| 640 | |
| 641 | for (i = 0; i < mtd->writesize / 4; i++) { |
| 642 | flip_bits_noecc += hweight32(~dma_buf[i]); |
| 643 | if (flip_bits_noecc > threshold) |
| 644 | return false; |
| 645 | } |
| 646 | |
| 647 | mtd->ecc_stats.corrected += flip_bits; |
| 648 | |
| 649 | memset(buf, 0xff, mtd->writesize); |
| 650 | |
| 651 | printf("The page(%d) is an erased page(%d,%d,%d,%d).\n", page, chunk, threshold, flip_bits, flip_bits_noecc); |
| 652 | |
| 653 | return true; |
| 654 | } |
| 655 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 656 | /* |
| 657 | * Read a page from NAND. |
| 658 | */ |
| 659 | static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 660 | uint8_t *buf, int oob_required, |
| 661 | int page) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 662 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 663 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 664 | struct bch_geometry *geo = &nand_info->bch_geometry; |
Peng Fan | 9e81373 | 2020-05-04 22:08:53 +0800 | [diff] [blame] | 665 | struct mxs_bch_regs *bch_regs = nand_info->bch_regs; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 666 | struct mxs_dma_desc *d; |
| 667 | uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip; |
| 668 | uint32_t corrected = 0, failed = 0; |
| 669 | uint8_t *status; |
| 670 | int i, ret; |
Peng Fan | 9e81373 | 2020-05-04 22:08:53 +0800 | [diff] [blame] | 671 | int flag = 0; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 672 | |
| 673 | /* Compile the DMA descriptor - wait for ready. */ |
| 674 | d = mxs_nand_get_dma_desc(nand_info); |
| 675 | d->cmd.data = |
| 676 | MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN | |
| 677 | MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END | |
| 678 | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET); |
| 679 | |
| 680 | d->cmd.address = 0; |
| 681 | |
| 682 | d->cmd.pio_words[0] = |
| 683 | GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY | |
| 684 | GPMI_CTRL0_WORD_LENGTH | |
| 685 | (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) | |
| 686 | GPMI_CTRL0_ADDRESS_NAND_DATA; |
| 687 | |
| 688 | mxs_dma_desc_append(channel, d); |
| 689 | |
| 690 | /* Compile the DMA descriptor - enable the BCH block and read. */ |
| 691 | d = mxs_nand_get_dma_desc(nand_info); |
| 692 | d->cmd.data = |
| 693 | MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN | |
| 694 | MXS_DMA_DESC_WAIT4END | (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET); |
| 695 | |
| 696 | d->cmd.address = 0; |
| 697 | |
| 698 | d->cmd.pio_words[0] = |
| 699 | GPMI_CTRL0_COMMAND_MODE_READ | |
| 700 | GPMI_CTRL0_WORD_LENGTH | |
| 701 | (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) | |
| 702 | GPMI_CTRL0_ADDRESS_NAND_DATA | |
| 703 | (mtd->writesize + mtd->oobsize); |
| 704 | d->cmd.pio_words[1] = 0; |
| 705 | d->cmd.pio_words[2] = |
| 706 | GPMI_ECCCTRL_ENABLE_ECC | |
| 707 | GPMI_ECCCTRL_ECC_CMD_DECODE | |
| 708 | GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE; |
| 709 | d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize; |
| 710 | d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf; |
| 711 | d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf; |
| 712 | |
Han Xu | afed2a1 | 2020-05-06 20:59:19 +0800 | [diff] [blame^] | 713 | if (nand_info->en_randomizer) { |
Alice Guo | 3f27778 | 2020-05-04 22:09:03 +0800 | [diff] [blame] | 714 | d->cmd.pio_words[2] |= GPMI_ECCCTRL_RANDOMIZER_ENABLE | |
| 715 | GPMI_ECCCTRL_RANDOMIZER_TYPE2; |
| 716 | d->cmd.pio_words[3] |= (page % 256) << 16; |
| 717 | } |
| 718 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 719 | mxs_dma_desc_append(channel, d); |
| 720 | |
| 721 | /* Compile the DMA descriptor - disable the BCH block. */ |
| 722 | d = mxs_nand_get_dma_desc(nand_info); |
| 723 | d->cmd.data = |
| 724 | MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN | |
| 725 | MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END | |
| 726 | (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET); |
| 727 | |
| 728 | d->cmd.address = 0; |
| 729 | |
| 730 | d->cmd.pio_words[0] = |
| 731 | GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY | |
| 732 | GPMI_CTRL0_WORD_LENGTH | |
| 733 | (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) | |
| 734 | GPMI_CTRL0_ADDRESS_NAND_DATA | |
| 735 | (mtd->writesize + mtd->oobsize); |
| 736 | d->cmd.pio_words[1] = 0; |
| 737 | d->cmd.pio_words[2] = 0; |
| 738 | |
| 739 | mxs_dma_desc_append(channel, d); |
| 740 | |
| 741 | /* Compile the DMA descriptor - deassert the NAND lock and interrupt. */ |
| 742 | d = mxs_nand_get_dma_desc(nand_info); |
| 743 | d->cmd.data = |
| 744 | MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ | |
| 745 | MXS_DMA_DESC_DEC_SEM; |
| 746 | |
| 747 | d->cmd.address = 0; |
| 748 | |
| 749 | mxs_dma_desc_append(channel, d); |
| 750 | |
Peng Fan | e3bbfb7 | 2015-07-21 16:15:21 +0800 | [diff] [blame] | 751 | /* Invalidate caches */ |
| 752 | mxs_nand_inval_data_buf(nand_info); |
| 753 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 754 | /* Execute the DMA chain. */ |
| 755 | ret = mxs_dma_go(channel); |
| 756 | if (ret) { |
| 757 | printf("MXS NAND: DMA read error\n"); |
| 758 | goto rtn; |
| 759 | } |
| 760 | |
Stefan Agner | dc8af6d | 2018-06-22 18:06:12 +0200 | [diff] [blame] | 761 | ret = mxs_nand_wait_for_bch_complete(nand_info); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 762 | if (ret) { |
| 763 | printf("MXS NAND: BCH read timeout\n"); |
| 764 | goto rtn; |
| 765 | } |
| 766 | |
Peng Fan | df23c9d | 2020-05-04 22:08:52 +0800 | [diff] [blame] | 767 | mxs_nand_return_dma_descs(nand_info); |
| 768 | |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 769 | /* Invalidate caches */ |
| 770 | mxs_nand_inval_data_buf(nand_info); |
| 771 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 772 | /* Read DMA completed, now do the mark swapping. */ |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 773 | mxs_nand_swap_block_mark(geo, nand_info->data_buf, nand_info->oob_buf); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 774 | |
| 775 | /* Loop over status bytes, accumulating ECC status. */ |
| 776 | status = nand_info->oob_buf + mxs_nand_aux_status_offset(); |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 777 | for (i = 0; i < geo->ecc_chunk_count; i++) { |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 778 | if (status[i] == 0x00) |
| 779 | continue; |
| 780 | |
Peng Fan | 9e81373 | 2020-05-04 22:08:53 +0800 | [diff] [blame] | 781 | if (status[i] == 0xff) { |
Han Xu | e4f2b00 | 2020-05-04 22:09:02 +0800 | [diff] [blame] | 782 | if (!nand_info->en_randomizer && |
| 783 | (is_mx6dqp() || is_mx7() || is_mx6ul() || |
| 784 | is_imx8() || is_imx8m())) |
Peng Fan | 9e81373 | 2020-05-04 22:08:53 +0800 | [diff] [blame] | 785 | if (readl(&bch_regs->hw_bch_debug1)) |
| 786 | flag = 1; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 787 | continue; |
Peng Fan | 9e81373 | 2020-05-04 22:08:53 +0800 | [diff] [blame] | 788 | } |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 789 | |
| 790 | if (status[i] == 0xfe) { |
Peng Fan | df23c9d | 2020-05-04 22:08:52 +0800 | [diff] [blame] | 791 | if (mxs_nand_erased_page(mtd, nand, |
| 792 | nand_info->data_buf, i, page)) |
| 793 | break; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 794 | failed++; |
| 795 | continue; |
| 796 | } |
| 797 | |
| 798 | corrected += status[i]; |
| 799 | } |
| 800 | |
| 801 | /* Propagate ECC status to the owning MTD. */ |
| 802 | mtd->ecc_stats.failed += failed; |
| 803 | mtd->ecc_stats.corrected += corrected; |
| 804 | |
| 805 | /* |
| 806 | * It's time to deliver the OOB bytes. See mxs_nand_ecc_read_oob() for |
| 807 | * details about our policy for delivering the OOB. |
| 808 | * |
| 809 | * We fill the caller's buffer with set bits, and then copy the block |
| 810 | * mark to the caller's buffer. Note that, if block mark swapping was |
| 811 | * necessary, it has already been done, so we can rely on the first |
| 812 | * byte of the auxiliary buffer to contain the block mark. |
| 813 | */ |
| 814 | memset(nand->oob_poi, 0xff, mtd->oobsize); |
| 815 | |
| 816 | nand->oob_poi[0] = nand_info->oob_buf[0]; |
| 817 | |
| 818 | memcpy(buf, nand_info->data_buf, mtd->writesize); |
| 819 | |
Peng Fan | 9e81373 | 2020-05-04 22:08:53 +0800 | [diff] [blame] | 820 | if (flag) |
| 821 | memset(buf, 0xff, mtd->writesize); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 822 | rtn: |
| 823 | mxs_nand_return_dma_descs(nand_info); |
| 824 | |
| 825 | return ret; |
| 826 | } |
| 827 | |
| 828 | /* |
| 829 | * Write a page to NAND. |
| 830 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 831 | static int mxs_nand_ecc_write_page(struct mtd_info *mtd, |
| 832 | struct nand_chip *nand, const uint8_t *buf, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 833 | int oob_required, int page) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 834 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 835 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 836 | struct bch_geometry *geo = &nand_info->bch_geometry; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 837 | struct mxs_dma_desc *d; |
| 838 | uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip; |
| 839 | int ret; |
| 840 | |
| 841 | memcpy(nand_info->data_buf, buf, mtd->writesize); |
| 842 | memcpy(nand_info->oob_buf, nand->oob_poi, mtd->oobsize); |
| 843 | |
| 844 | /* Handle block mark swapping. */ |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 845 | mxs_nand_swap_block_mark(geo, nand_info->data_buf, nand_info->oob_buf); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 846 | |
| 847 | /* Compile the DMA descriptor - write data. */ |
| 848 | d = mxs_nand_get_dma_desc(nand_info); |
| 849 | d->cmd.data = |
| 850 | MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ | |
| 851 | MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END | |
| 852 | (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET); |
| 853 | |
| 854 | d->cmd.address = 0; |
| 855 | |
| 856 | d->cmd.pio_words[0] = |
| 857 | GPMI_CTRL0_COMMAND_MODE_WRITE | |
| 858 | GPMI_CTRL0_WORD_LENGTH | |
| 859 | (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) | |
| 860 | GPMI_CTRL0_ADDRESS_NAND_DATA; |
| 861 | d->cmd.pio_words[1] = 0; |
| 862 | d->cmd.pio_words[2] = |
| 863 | GPMI_ECCCTRL_ENABLE_ECC | |
| 864 | GPMI_ECCCTRL_ECC_CMD_ENCODE | |
| 865 | GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE; |
| 866 | d->cmd.pio_words[3] = (mtd->writesize + mtd->oobsize); |
| 867 | d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf; |
| 868 | d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf; |
| 869 | |
Han Xu | afed2a1 | 2020-05-06 20:59:19 +0800 | [diff] [blame^] | 870 | if (nand_info->en_randomizer) { |
Igor Opaniuk | c5540137 | 2019-11-03 16:49:43 +0100 | [diff] [blame] | 871 | d->cmd.pio_words[2] |= GPMI_ECCCTRL_RANDOMIZER_ENABLE | |
| 872 | GPMI_ECCCTRL_RANDOMIZER_TYPE2; |
| 873 | /* |
| 874 | * Write NAND page number needed to be randomized |
| 875 | * to GPMI_ECCCOUNT register. |
| 876 | * |
| 877 | * The value is between 0-255. For additional details |
| 878 | * check 9.6.6.4 of i.MX7D Applications Processor reference |
| 879 | */ |
Alice Guo | 3f27778 | 2020-05-04 22:09:03 +0800 | [diff] [blame] | 880 | d->cmd.pio_words[3] |= (page % 256) << 16; |
Igor Opaniuk | c5540137 | 2019-11-03 16:49:43 +0100 | [diff] [blame] | 881 | } |
| 882 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 883 | mxs_dma_desc_append(channel, d); |
| 884 | |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 885 | /* Flush caches */ |
| 886 | mxs_nand_flush_data_buf(nand_info); |
| 887 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 888 | /* Execute the DMA chain. */ |
| 889 | ret = mxs_dma_go(channel); |
| 890 | if (ret) { |
| 891 | printf("MXS NAND: DMA write error\n"); |
| 892 | goto rtn; |
| 893 | } |
| 894 | |
Stefan Agner | dc8af6d | 2018-06-22 18:06:12 +0200 | [diff] [blame] | 895 | ret = mxs_nand_wait_for_bch_complete(nand_info); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 896 | if (ret) { |
| 897 | printf("MXS NAND: BCH write timeout\n"); |
| 898 | goto rtn; |
| 899 | } |
| 900 | |
| 901 | rtn: |
| 902 | mxs_nand_return_dma_descs(nand_info); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 903 | return 0; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | /* |
| 907 | * Read OOB from NAND. |
| 908 | * |
| 909 | * This function is a veneer that replaces the function originally installed by |
| 910 | * the NAND Flash MTD code. |
| 911 | */ |
| 912 | static int mxs_nand_hook_read_oob(struct mtd_info *mtd, loff_t from, |
| 913 | struct mtd_oob_ops *ops) |
| 914 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 915 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 916 | struct mxs_nand_info *nand_info = nand_get_controller_data(chip); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 917 | int ret; |
| 918 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 919 | if (ops->mode == MTD_OPS_RAW) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 920 | nand_info->raw_oob_mode = 1; |
| 921 | else |
| 922 | nand_info->raw_oob_mode = 0; |
| 923 | |
| 924 | ret = nand_info->hooked_read_oob(mtd, from, ops); |
| 925 | |
| 926 | nand_info->raw_oob_mode = 0; |
| 927 | |
| 928 | return ret; |
| 929 | } |
| 930 | |
| 931 | /* |
| 932 | * Write OOB to NAND. |
| 933 | * |
| 934 | * This function is a veneer that replaces the function originally installed by |
| 935 | * the NAND Flash MTD code. |
| 936 | */ |
| 937 | static int mxs_nand_hook_write_oob(struct mtd_info *mtd, loff_t to, |
| 938 | struct mtd_oob_ops *ops) |
| 939 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 940 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 941 | struct mxs_nand_info *nand_info = nand_get_controller_data(chip); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 942 | int ret; |
| 943 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 944 | if (ops->mode == MTD_OPS_RAW) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 945 | nand_info->raw_oob_mode = 1; |
| 946 | else |
| 947 | nand_info->raw_oob_mode = 0; |
| 948 | |
| 949 | ret = nand_info->hooked_write_oob(mtd, to, ops); |
| 950 | |
| 951 | nand_info->raw_oob_mode = 0; |
| 952 | |
| 953 | return ret; |
| 954 | } |
| 955 | |
| 956 | /* |
| 957 | * Mark a block bad in NAND. |
| 958 | * |
| 959 | * This function is a veneer that replaces the function originally installed by |
| 960 | * the NAND Flash MTD code. |
| 961 | */ |
| 962 | static int mxs_nand_hook_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 963 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 964 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 965 | struct mxs_nand_info *nand_info = nand_get_controller_data(chip); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 966 | int ret; |
| 967 | |
| 968 | nand_info->marking_block_bad = 1; |
| 969 | |
| 970 | ret = nand_info->hooked_block_markbad(mtd, ofs); |
| 971 | |
| 972 | nand_info->marking_block_bad = 0; |
| 973 | |
| 974 | return ret; |
| 975 | } |
| 976 | |
| 977 | /* |
| 978 | * There are several places in this driver where we have to handle the OOB and |
| 979 | * block marks. This is the function where things are the most complicated, so |
| 980 | * this is where we try to explain it all. All the other places refer back to |
| 981 | * here. |
| 982 | * |
| 983 | * These are the rules, in order of decreasing importance: |
| 984 | * |
| 985 | * 1) Nothing the caller does can be allowed to imperil the block mark, so all |
| 986 | * write operations take measures to protect it. |
| 987 | * |
| 988 | * 2) In read operations, the first byte of the OOB we return must reflect the |
| 989 | * true state of the block mark, no matter where that block mark appears in |
| 990 | * the physical page. |
| 991 | * |
| 992 | * 3) ECC-based read operations return an OOB full of set bits (since we never |
| 993 | * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads |
| 994 | * return). |
| 995 | * |
| 996 | * 4) "Raw" read operations return a direct view of the physical bytes in the |
| 997 | * page, using the conventional definition of which bytes are data and which |
| 998 | * are OOB. This gives the caller a way to see the actual, physical bytes |
| 999 | * in the page, without the distortions applied by our ECC engine. |
| 1000 | * |
| 1001 | * What we do for this specific read operation depends on whether we're doing |
| 1002 | * "raw" read, or an ECC-based read. |
| 1003 | * |
| 1004 | * It turns out that knowing whether we want an "ECC-based" or "raw" read is not |
| 1005 | * easy. When reading a page, for example, the NAND Flash MTD code calls our |
| 1006 | * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an |
| 1007 | * ECC-based or raw view of the page is implicit in which function it calls |
| 1008 | * (there is a similar pair of ECC-based/raw functions for writing). |
| 1009 | * |
| 1010 | * Since MTD assumes the OOB is not covered by ECC, there is no pair of |
| 1011 | * ECC-based/raw functions for reading or or writing the OOB. The fact that the |
| 1012 | * caller wants an ECC-based or raw view of the page is not propagated down to |
| 1013 | * this driver. |
| 1014 | * |
| 1015 | * Since our OOB *is* covered by ECC, we need this information. So, we hook the |
| 1016 | * ecc.read_oob and ecc.write_oob function pointers in the owning |
| 1017 | * struct mtd_info with our own functions. These hook functions set the |
| 1018 | * raw_oob_mode field so that, when control finally arrives here, we'll know |
| 1019 | * what to do. |
| 1020 | */ |
| 1021 | static int mxs_nand_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *nand, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1022 | int page) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1023 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1024 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1025 | |
| 1026 | /* |
| 1027 | * First, fill in the OOB buffer. If we're doing a raw read, we need to |
| 1028 | * get the bytes from the physical page. If we're not doing a raw read, |
| 1029 | * we need to fill the buffer with set bits. |
| 1030 | */ |
| 1031 | if (nand_info->raw_oob_mode) { |
| 1032 | /* |
| 1033 | * If control arrives here, we're doing a "raw" read. Send the |
| 1034 | * command to read the conventional OOB and read it. |
| 1035 | */ |
| 1036 | nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page); |
| 1037 | nand->read_buf(mtd, nand->oob_poi, mtd->oobsize); |
| 1038 | } else { |
| 1039 | /* |
| 1040 | * If control arrives here, we're not doing a "raw" read. Fill |
| 1041 | * the OOB buffer with set bits and correct the block mark. |
| 1042 | */ |
| 1043 | memset(nand->oob_poi, 0xff, mtd->oobsize); |
| 1044 | |
| 1045 | nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page); |
| 1046 | mxs_nand_read_buf(mtd, nand->oob_poi, 1); |
| 1047 | } |
| 1048 | |
| 1049 | return 0; |
| 1050 | |
| 1051 | } |
| 1052 | |
| 1053 | /* |
| 1054 | * Write OOB data to NAND. |
| 1055 | */ |
| 1056 | static int mxs_nand_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *nand, |
| 1057 | int page) |
| 1058 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1059 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1060 | uint8_t block_mark = 0; |
| 1061 | |
| 1062 | /* |
| 1063 | * There are fundamental incompatibilities between the i.MX GPMI NFC and |
| 1064 | * the NAND Flash MTD model that make it essentially impossible to write |
| 1065 | * the out-of-band bytes. |
| 1066 | * |
| 1067 | * We permit *ONE* exception. If the *intent* of writing the OOB is to |
| 1068 | * mark a block bad, we can do that. |
| 1069 | */ |
| 1070 | |
| 1071 | if (!nand_info->marking_block_bad) { |
| 1072 | printf("NXS NAND: Writing OOB isn't supported\n"); |
| 1073 | return -EIO; |
| 1074 | } |
| 1075 | |
| 1076 | /* Write the block mark. */ |
| 1077 | nand->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page); |
| 1078 | nand->write_buf(mtd, &block_mark, 1); |
| 1079 | nand->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1080 | |
| 1081 | /* Check if it worked. */ |
| 1082 | if (nand->waitfunc(mtd, nand) & NAND_STATUS_FAIL) |
| 1083 | return -EIO; |
| 1084 | |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | /* |
| 1089 | * Claims all blocks are good. |
| 1090 | * |
| 1091 | * In principle, this function is *only* called when the NAND Flash MTD system |
| 1092 | * isn't allowed to keep an in-memory bad block table, so it is forced to ask |
| 1093 | * the driver for bad block information. |
| 1094 | * |
| 1095 | * In fact, we permit the NAND Flash MTD system to have an in-memory BBT, so |
| 1096 | * this function is *only* called when we take it away. |
| 1097 | * |
| 1098 | * Thus, this function is only called when we want *all* blocks to look good, |
| 1099 | * so it *always* return success. |
| 1100 | */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 1101 | static int mxs_nand_block_bad(struct mtd_info *mtd, loff_t ofs) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1102 | { |
Stefan Agner | ead66eb | 2018-06-22 18:06:18 +0200 | [diff] [blame] | 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | static int mxs_nand_set_geometry(struct mtd_info *mtd, struct bch_geometry *geo) |
| 1107 | { |
| 1108 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1109 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 1110 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
| 1111 | |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 1112 | if (chip->ecc_strength_ds > nand_info->max_ecc_strength_supported) { |
| 1113 | printf("unsupported NAND chip, minimum ecc required %d\n" |
| 1114 | , chip->ecc_strength_ds); |
| 1115 | return -EINVAL; |
| 1116 | } |
Stefan Agner | ead66eb | 2018-06-22 18:06:18 +0200 | [diff] [blame] | 1117 | |
Ye Li | c673613 | 2020-05-04 22:08:51 +0800 | [diff] [blame] | 1118 | if ((!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0) && |
| 1119 | mtd->oobsize < 1024) || nand_info->legacy_bch_geometry) { |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 1120 | dev_warn(this->dev, "use legacy bch geometry\n"); |
| 1121 | return mxs_nand_legacy_calc_ecc_layout(geo, mtd); |
| 1122 | } |
Stefan Agner | ead66eb | 2018-06-22 18:06:18 +0200 | [diff] [blame] | 1123 | |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 1124 | if (mtd->oobsize > 1024 || chip->ecc_step_ds < mtd->oobsize) |
| 1125 | return mxs_nand_calc_ecc_for_large_oob(geo, mtd); |
| 1126 | |
| 1127 | return mxs_nand_calc_ecc_layout_by_info(geo, mtd, |
Stefan Agner | ead66eb | 2018-06-22 18:06:18 +0200 | [diff] [blame] | 1128 | chip->ecc_strength_ds, chip->ecc_step_ds); |
Stefan Agner | ead66eb | 2018-06-22 18:06:18 +0200 | [diff] [blame] | 1129 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | /* |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1134 | * At this point, the physical NAND Flash chips have been identified and |
| 1135 | * counted, so we know the physical geometry. This enables us to make some |
| 1136 | * important configuration decisions. |
| 1137 | * |
Robert P. J. Day | 8d56db9 | 2016-07-15 13:44:45 -0400 | [diff] [blame] | 1138 | * The return value of this function propagates directly back to this driver's |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1139 | * board_nand_init(). Anything other than zero will cause this driver to |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1140 | * tear everything down and declare failure. |
| 1141 | */ |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1142 | int mxs_nand_setup_ecc(struct mtd_info *mtd) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1143 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1144 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 1145 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 1146 | struct bch_geometry *geo = &nand_info->bch_geometry; |
Stefan Agner | dc8af6d | 2018-06-22 18:06:12 +0200 | [diff] [blame] | 1147 | struct mxs_bch_regs *bch_regs = nand_info->bch_regs; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1148 | uint32_t tmp; |
Stefan Agner | ead66eb | 2018-06-22 18:06:18 +0200 | [diff] [blame] | 1149 | int ret; |
Stefan Agner | 4d42ac1 | 2018-06-22 17:19:51 +0200 | [diff] [blame] | 1150 | |
Igor Opaniuk | c5540137 | 2019-11-03 16:49:43 +0100 | [diff] [blame] | 1151 | nand_info->en_randomizer = 0; |
| 1152 | nand_info->oobsize = mtd->oobsize; |
| 1153 | nand_info->writesize = mtd->writesize; |
| 1154 | |
Stefan Agner | ead66eb | 2018-06-22 18:06:18 +0200 | [diff] [blame] | 1155 | ret = mxs_nand_set_geometry(mtd, geo); |
Stefan Agner | 4d42ac1 | 2018-06-22 17:19:51 +0200 | [diff] [blame] | 1156 | if (ret) |
| 1157 | return ret; |
| 1158 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1159 | /* Configure BCH and set NFC geometry */ |
Otavio Salvador | cbf0bf2 | 2012-08-13 09:53:12 +0000 | [diff] [blame] | 1160 | mxs_reset_block(&bch_regs->hw_bch_ctrl_reg); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1161 | |
| 1162 | /* Configure layout 0 */ |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 1163 | tmp = (geo->ecc_chunk_count - 1) << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1164 | tmp |= MXS_NAND_METADATA_SIZE << BCH_FLASHLAYOUT0_META_SIZE_OFFSET; |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 1165 | tmp |= (geo->ecc_strength >> 1) << BCH_FLASHLAYOUT0_ECC0_OFFSET; |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 1166 | tmp |= geo->ecc_chunk0_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT; |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 1167 | tmp |= (geo->gf_len == 14 ? 1 : 0) << |
Peng Fan | c94f09d | 2015-07-21 16:15:19 +0800 | [diff] [blame] | 1168 | BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1169 | writel(tmp, &bch_regs->hw_bch_flash0layout0); |
Igor Opaniuk | c5540137 | 2019-11-03 16:49:43 +0100 | [diff] [blame] | 1170 | nand_info->bch_flash0layout0 = tmp; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1171 | |
| 1172 | tmp = (mtd->writesize + mtd->oobsize) |
| 1173 | << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET; |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 1174 | tmp |= (geo->ecc_strength >> 1) << BCH_FLASHLAYOUT1_ECCN_OFFSET; |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 1175 | tmp |= geo->ecc_chunkn_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT; |
Stefan Agner | d0778b3 | 2018-06-22 17:19:49 +0200 | [diff] [blame] | 1176 | tmp |= (geo->gf_len == 14 ? 1 : 0) << |
Peng Fan | c94f09d | 2015-07-21 16:15:19 +0800 | [diff] [blame] | 1177 | BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1178 | writel(tmp, &bch_regs->hw_bch_flash0layout1); |
Igor Opaniuk | c5540137 | 2019-11-03 16:49:43 +0100 | [diff] [blame] | 1179 | nand_info->bch_flash0layout1 = tmp; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1180 | |
Peng Fan | 9e81373 | 2020-05-04 22:08:53 +0800 | [diff] [blame] | 1181 | /* Set erase threshold to ecc strength for mx6ul, mx6qp and mx7 */ |
| 1182 | if (is_mx6dqp() || is_mx7() || |
Peng Fan | 128abf4 | 2020-05-04 22:09:00 +0800 | [diff] [blame] | 1183 | is_mx6ul() || is_imx8() || is_imx8m()) |
Peng Fan | 9e81373 | 2020-05-04 22:08:53 +0800 | [diff] [blame] | 1184 | writel(BCH_MODE_ERASE_THRESHOLD(geo->ecc_strength), |
| 1185 | &bch_regs->hw_bch_mode); |
| 1186 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1187 | /* Set *all* chip selects to use layout 0 */ |
| 1188 | writel(0, &bch_regs->hw_bch_layoutselect); |
| 1189 | |
| 1190 | /* Enable BCH complete interrupt */ |
| 1191 | writel(BCH_CTRL_COMPLETE_IRQ_EN, &bch_regs->hw_bch_ctrl_set); |
| 1192 | |
| 1193 | /* Hook some operations at the MTD level. */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1194 | if (mtd->_read_oob != mxs_nand_hook_read_oob) { |
| 1195 | nand_info->hooked_read_oob = mtd->_read_oob; |
| 1196 | mtd->_read_oob = mxs_nand_hook_read_oob; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1199 | if (mtd->_write_oob != mxs_nand_hook_write_oob) { |
| 1200 | nand_info->hooked_write_oob = mtd->_write_oob; |
| 1201 | mtd->_write_oob = mxs_nand_hook_write_oob; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1204 | if (mtd->_block_markbad != mxs_nand_hook_block_markbad) { |
| 1205 | nand_info->hooked_block_markbad = mtd->_block_markbad; |
| 1206 | mtd->_block_markbad = mxs_nand_hook_block_markbad; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1209 | return 0; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | /* |
| 1213 | * Allocate DMA buffers |
| 1214 | */ |
| 1215 | int mxs_nand_alloc_buffers(struct mxs_nand_info *nand_info) |
| 1216 | { |
| 1217 | uint8_t *buf; |
| 1218 | const int size = NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE; |
| 1219 | |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 1220 | nand_info->data_buf_size = roundup(size, MXS_DMA_ALIGNMENT); |
| 1221 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1222 | /* DMA buffers */ |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 1223 | buf = memalign(MXS_DMA_ALIGNMENT, nand_info->data_buf_size); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1224 | if (!buf) { |
| 1225 | printf("MXS NAND: Error allocating DMA buffers\n"); |
| 1226 | return -ENOMEM; |
| 1227 | } |
| 1228 | |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 1229 | memset(buf, 0, nand_info->data_buf_size); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1230 | |
| 1231 | nand_info->data_buf = buf; |
| 1232 | nand_info->oob_buf = buf + NAND_MAX_PAGESIZE; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1233 | /* Command buffers */ |
| 1234 | nand_info->cmd_buf = memalign(MXS_DMA_ALIGNMENT, |
| 1235 | MXS_NAND_COMMAND_BUFFER_SIZE); |
| 1236 | if (!nand_info->cmd_buf) { |
| 1237 | free(buf); |
| 1238 | printf("MXS NAND: Error allocating command buffers\n"); |
| 1239 | return -ENOMEM; |
| 1240 | } |
| 1241 | memset(nand_info->cmd_buf, 0, MXS_NAND_COMMAND_BUFFER_SIZE); |
| 1242 | nand_info->cmd_queue_len = 0; |
| 1243 | |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | /* |
| 1248 | * Initializes the NFC hardware. |
| 1249 | */ |
Adam Ford | 6edb91a | 2019-01-12 06:25:48 -0600 | [diff] [blame] | 1250 | static int mxs_nand_init_dma(struct mxs_nand_info *info) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1251 | { |
Peng Fan | e37d5a9 | 2016-01-27 10:38:02 +0800 | [diff] [blame] | 1252 | int i = 0, j, ret = 0; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1253 | |
| 1254 | info->desc = malloc(sizeof(struct mxs_dma_desc *) * |
| 1255 | MXS_NAND_DMA_DESCRIPTOR_COUNT); |
Peng Fan | e37d5a9 | 2016-01-27 10:38:02 +0800 | [diff] [blame] | 1256 | if (!info->desc) { |
| 1257 | ret = -ENOMEM; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1258 | goto err1; |
Peng Fan | e37d5a9 | 2016-01-27 10:38:02 +0800 | [diff] [blame] | 1259 | } |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1260 | |
| 1261 | /* Allocate the DMA descriptors. */ |
| 1262 | for (i = 0; i < MXS_NAND_DMA_DESCRIPTOR_COUNT; i++) { |
| 1263 | info->desc[i] = mxs_dma_desc_alloc(); |
Peng Fan | e37d5a9 | 2016-01-27 10:38:02 +0800 | [diff] [blame] | 1264 | if (!info->desc[i]) { |
| 1265 | ret = -ENOMEM; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1266 | goto err2; |
Peng Fan | e37d5a9 | 2016-01-27 10:38:02 +0800 | [diff] [blame] | 1267 | } |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | /* Init the DMA controller. */ |
Fabio Estevam | 1715622 | 2017-06-29 09:33:44 -0300 | [diff] [blame] | 1271 | mxs_dma_init(); |
Marek Vasut | 93541b4 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 1272 | for (j = MXS_DMA_CHANNEL_AHB_APBH_GPMI0; |
| 1273 | j <= MXS_DMA_CHANNEL_AHB_APBH_GPMI7; j++) { |
Peng Fan | e37d5a9 | 2016-01-27 10:38:02 +0800 | [diff] [blame] | 1274 | ret = mxs_dma_init_channel(j); |
| 1275 | if (ret) |
Marek Vasut | 93541b4 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 1276 | goto err3; |
| 1277 | } |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1278 | |
| 1279 | /* Reset the GPMI block. */ |
Stefan Agner | dc8af6d | 2018-06-22 18:06:12 +0200 | [diff] [blame] | 1280 | mxs_reset_block(&info->gpmi_regs->hw_gpmi_ctrl0_reg); |
| 1281 | mxs_reset_block(&info->bch_regs->hw_bch_ctrl_reg); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1282 | |
| 1283 | /* |
| 1284 | * Choose NAND mode, set IRQ polarity, disable write protection and |
| 1285 | * select BCH ECC. |
| 1286 | */ |
Stefan Agner | dc8af6d | 2018-06-22 18:06:12 +0200 | [diff] [blame] | 1287 | clrsetbits_le32(&info->gpmi_regs->hw_gpmi_ctrl1, |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1288 | GPMI_CTRL1_GPMI_MODE, |
| 1289 | GPMI_CTRL1_ATA_IRQRDY_POLARITY | GPMI_CTRL1_DEV_RESET | |
| 1290 | GPMI_CTRL1_BCH_MODE); |
| 1291 | |
| 1292 | return 0; |
| 1293 | |
Marek Vasut | 93541b4 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 1294 | err3: |
Peng Fan | e37d5a9 | 2016-01-27 10:38:02 +0800 | [diff] [blame] | 1295 | for (--j; j >= MXS_DMA_CHANNEL_AHB_APBH_GPMI0; j--) |
Marek Vasut | 93541b4 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 1296 | mxs_dma_release(j); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1297 | err2: |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1298 | for (--i; i >= 0; i--) |
| 1299 | mxs_dma_desc_free(info->desc[i]); |
Peng Fan | e37d5a9 | 2016-01-27 10:38:02 +0800 | [diff] [blame] | 1300 | free(info->desc); |
| 1301 | err1: |
| 1302 | if (ret == -ENOMEM) |
| 1303 | printf("MXS NAND: Unable to allocate DMA descriptors\n"); |
| 1304 | return ret; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Stefan Agner | 7152f34 | 2018-06-22 17:19:46 +0200 | [diff] [blame] | 1307 | int mxs_nand_init_spl(struct nand_chip *nand) |
| 1308 | { |
| 1309 | struct mxs_nand_info *nand_info; |
| 1310 | int err; |
| 1311 | |
| 1312 | nand_info = malloc(sizeof(struct mxs_nand_info)); |
| 1313 | if (!nand_info) { |
| 1314 | printf("MXS NAND: Failed to allocate private data\n"); |
| 1315 | return -ENOMEM; |
| 1316 | } |
| 1317 | memset(nand_info, 0, sizeof(struct mxs_nand_info)); |
| 1318 | |
Stefan Agner | dc8af6d | 2018-06-22 18:06:12 +0200 | [diff] [blame] | 1319 | nand_info->gpmi_regs = (struct mxs_gpmi_regs *)MXS_GPMI_BASE; |
| 1320 | nand_info->bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE; |
Adam Ford | 1021073 | 2019-01-02 20:36:52 -0600 | [diff] [blame] | 1321 | |
Peng Fan | 128abf4 | 2020-05-04 22:09:00 +0800 | [diff] [blame] | 1322 | if (is_mx6sx() || is_mx7() || is_imx8() || is_imx8m()) |
Adam Ford | 1021073 | 2019-01-02 20:36:52 -0600 | [diff] [blame] | 1323 | nand_info->max_ecc_strength_supported = 62; |
| 1324 | else |
| 1325 | nand_info->max_ecc_strength_supported = 40; |
| 1326 | |
Stefan Agner | 7152f34 | 2018-06-22 17:19:46 +0200 | [diff] [blame] | 1327 | err = mxs_nand_alloc_buffers(nand_info); |
| 1328 | if (err) |
| 1329 | return err; |
| 1330 | |
Stefan Agner | 00e6516 | 2018-06-22 18:06:13 +0200 | [diff] [blame] | 1331 | err = mxs_nand_init_dma(nand_info); |
Stefan Agner | 7152f34 | 2018-06-22 17:19:46 +0200 | [diff] [blame] | 1332 | if (err) |
| 1333 | return err; |
| 1334 | |
| 1335 | nand_set_controller_data(nand, nand_info); |
| 1336 | |
| 1337 | nand->options |= NAND_NO_SUBPAGE_WRITE; |
| 1338 | |
| 1339 | nand->cmd_ctrl = mxs_nand_cmd_ctrl; |
| 1340 | nand->dev_ready = mxs_nand_device_ready; |
| 1341 | nand->select_chip = mxs_nand_select_chip; |
Stefan Agner | 7152f34 | 2018-06-22 17:19:46 +0200 | [diff] [blame] | 1342 | |
| 1343 | nand->read_byte = mxs_nand_read_byte; |
| 1344 | nand->read_buf = mxs_nand_read_buf; |
| 1345 | |
| 1346 | nand->ecc.read_page = mxs_nand_ecc_read_page; |
| 1347 | |
| 1348 | nand->ecc.mode = NAND_ECC_HW; |
Stefan Agner | 7152f34 | 2018-06-22 17:19:46 +0200 | [diff] [blame] | 1349 | |
| 1350 | return 0; |
| 1351 | } |
| 1352 | |
Stefan Agner | 19f9051 | 2018-06-22 18:06:16 +0200 | [diff] [blame] | 1353 | int mxs_nand_init_ctrl(struct mxs_nand_info *nand_info) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1354 | { |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1355 | struct mtd_info *mtd; |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1356 | struct nand_chip *nand; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1357 | int err; |
| 1358 | |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1359 | nand = &nand_info->chip; |
| 1360 | mtd = nand_to_mtd(nand); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1361 | err = mxs_nand_alloc_buffers(nand_info); |
| 1362 | if (err) |
Stefan Agner | 404b110 | 2018-06-22 18:06:14 +0200 | [diff] [blame] | 1363 | return err; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1364 | |
Stefan Agner | 00e6516 | 2018-06-22 18:06:13 +0200 | [diff] [blame] | 1365 | err = mxs_nand_init_dma(nand_info); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1366 | if (err) |
Stefan Agner | 404b110 | 2018-06-22 18:06:14 +0200 | [diff] [blame] | 1367 | goto err_free_buffers; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1368 | |
| 1369 | memset(&fake_ecc_layout, 0, sizeof(fake_ecc_layout)); |
| 1370 | |
Stefan Agner | 95f376f | 2018-06-22 17:19:48 +0200 | [diff] [blame] | 1371 | #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT |
| 1372 | nand->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB; |
| 1373 | #endif |
| 1374 | |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1375 | nand_set_controller_data(nand, nand_info); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1376 | nand->options |= NAND_NO_SUBPAGE_WRITE; |
| 1377 | |
Stefan Agner | 150ddbc | 2018-06-22 18:06:17 +0200 | [diff] [blame] | 1378 | if (nand_info->dev) |
| 1379 | nand->flash_node = dev_of_offset(nand_info->dev); |
| 1380 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1381 | nand->cmd_ctrl = mxs_nand_cmd_ctrl; |
| 1382 | |
| 1383 | nand->dev_ready = mxs_nand_device_ready; |
| 1384 | nand->select_chip = mxs_nand_select_chip; |
| 1385 | nand->block_bad = mxs_nand_block_bad; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1386 | |
| 1387 | nand->read_byte = mxs_nand_read_byte; |
| 1388 | |
| 1389 | nand->read_buf = mxs_nand_read_buf; |
| 1390 | nand->write_buf = mxs_nand_write_buf; |
| 1391 | |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1392 | /* first scan to find the device and get the page size */ |
| 1393 | if (nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_DEVICE, NULL)) |
Stefan Agner | 404b110 | 2018-06-22 18:06:14 +0200 | [diff] [blame] | 1394 | goto err_free_buffers; |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1395 | |
| 1396 | if (mxs_nand_setup_ecc(mtd)) |
Stefan Agner | 404b110 | 2018-06-22 18:06:14 +0200 | [diff] [blame] | 1397 | goto err_free_buffers; |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1398 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1399 | nand->ecc.read_page = mxs_nand_ecc_read_page; |
| 1400 | nand->ecc.write_page = mxs_nand_ecc_write_page; |
| 1401 | nand->ecc.read_oob = mxs_nand_ecc_read_oob; |
| 1402 | nand->ecc.write_oob = mxs_nand_ecc_write_oob; |
| 1403 | |
| 1404 | nand->ecc.layout = &fake_ecc_layout; |
| 1405 | nand->ecc.mode = NAND_ECC_HW; |
Ye Li | 9454744 | 2020-05-04 22:08:50 +0800 | [diff] [blame] | 1406 | nand->ecc.size = nand_info->bch_geometry.ecc_chunkn_size; |
Stefan Agner | 72d627d | 2018-06-22 17:19:50 +0200 | [diff] [blame] | 1407 | nand->ecc.strength = nand_info->bch_geometry.ecc_strength; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1408 | |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1409 | /* second phase scan */ |
| 1410 | err = nand_scan_tail(mtd); |
| 1411 | if (err) |
Stefan Agner | 404b110 | 2018-06-22 18:06:14 +0200 | [diff] [blame] | 1412 | goto err_free_buffers; |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1413 | |
| 1414 | err = nand_register(0, mtd); |
| 1415 | if (err) |
Stefan Agner | 404b110 | 2018-06-22 18:06:14 +0200 | [diff] [blame] | 1416 | goto err_free_buffers; |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1417 | |
Stefan Agner | 404b110 | 2018-06-22 18:06:14 +0200 | [diff] [blame] | 1418 | return 0; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1419 | |
Stefan Agner | 404b110 | 2018-06-22 18:06:14 +0200 | [diff] [blame] | 1420 | err_free_buffers: |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1421 | free(nand_info->data_buf); |
| 1422 | free(nand_info->cmd_buf); |
Stefan Agner | 404b110 | 2018-06-22 18:06:14 +0200 | [diff] [blame] | 1423 | |
| 1424 | return err; |
| 1425 | } |
| 1426 | |
Stefan Agner | 150ddbc | 2018-06-22 18:06:17 +0200 | [diff] [blame] | 1427 | #ifndef CONFIG_NAND_MXS_DT |
Stefan Agner | 404b110 | 2018-06-22 18:06:14 +0200 | [diff] [blame] | 1428 | void board_nand_init(void) |
| 1429 | { |
| 1430 | struct mxs_nand_info *nand_info; |
| 1431 | |
| 1432 | nand_info = malloc(sizeof(struct mxs_nand_info)); |
| 1433 | if (!nand_info) { |
| 1434 | printf("MXS NAND: Failed to allocate private data\n"); |
| 1435 | return; |
| 1436 | } |
| 1437 | memset(nand_info, 0, sizeof(struct mxs_nand_info)); |
| 1438 | |
| 1439 | nand_info->gpmi_regs = (struct mxs_gpmi_regs *)MXS_GPMI_BASE; |
| 1440 | nand_info->bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE; |
| 1441 | |
Stefan Agner | 4dc98db | 2018-06-22 18:06:15 +0200 | [diff] [blame] | 1442 | /* Refer to Chapter 17 for i.MX6DQ, Chapter 18 for i.MX6SX */ |
| 1443 | if (is_mx6sx() || is_mx7()) |
| 1444 | nand_info->max_ecc_strength_supported = 62; |
| 1445 | else |
| 1446 | nand_info->max_ecc_strength_supported = 40; |
| 1447 | |
| 1448 | #ifdef CONFIG_NAND_MXS_USE_MINIMUM_ECC |
| 1449 | nand_info->use_minimum_ecc = true; |
| 1450 | #endif |
| 1451 | |
Stefan Agner | 19f9051 | 2018-06-22 18:06:16 +0200 | [diff] [blame] | 1452 | if (mxs_nand_init_ctrl(nand_info) < 0) |
Stefan Agner | 404b110 | 2018-06-22 18:06:14 +0200 | [diff] [blame] | 1453 | goto err; |
| 1454 | |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1455 | return; |
Stefan Agner | 404b110 | 2018-06-22 18:06:14 +0200 | [diff] [blame] | 1456 | |
| 1457 | err: |
| 1458 | free(nand_info); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1459 | } |
Stefan Agner | 150ddbc | 2018-06-22 18:06:17 +0200 | [diff] [blame] | 1460 | #endif |
Igor Opaniuk | c5540137 | 2019-11-03 16:49:43 +0100 | [diff] [blame] | 1461 | |
| 1462 | /* |
| 1463 | * Read NAND layout for FCB block generation. |
| 1464 | */ |
| 1465 | void mxs_nand_get_layout(struct mtd_info *mtd, struct mxs_nand_layout *l) |
| 1466 | { |
| 1467 | struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE; |
| 1468 | u32 tmp; |
| 1469 | |
| 1470 | tmp = readl(&bch_regs->hw_bch_flash0layout0); |
| 1471 | l->nblocks = (tmp & BCH_FLASHLAYOUT0_NBLOCKS_MASK) >> |
| 1472 | BCH_FLASHLAYOUT0_NBLOCKS_OFFSET; |
| 1473 | l->meta_size = (tmp & BCH_FLASHLAYOUT0_META_SIZE_MASK) >> |
| 1474 | BCH_FLASHLAYOUT0_META_SIZE_OFFSET; |
| 1475 | |
| 1476 | tmp = readl(&bch_regs->hw_bch_flash0layout1); |
| 1477 | l->data0_size = 4 * ((tmp & BCH_FLASHLAYOUT0_DATA0_SIZE_MASK) >> |
| 1478 | BCH_FLASHLAYOUT0_DATA0_SIZE_OFFSET); |
| 1479 | l->ecc0 = (tmp & BCH_FLASHLAYOUT0_ECC0_MASK) >> |
| 1480 | BCH_FLASHLAYOUT0_ECC0_OFFSET; |
| 1481 | l->datan_size = 4 * ((tmp & BCH_FLASHLAYOUT1_DATAN_SIZE_MASK) >> |
| 1482 | BCH_FLASHLAYOUT1_DATAN_SIZE_OFFSET); |
| 1483 | l->eccn = (tmp & BCH_FLASHLAYOUT1_ECCN_MASK) >> |
| 1484 | BCH_FLASHLAYOUT1_ECCN_OFFSET; |
Han Xu | 33543b5 | 2020-05-04 22:08:58 +0800 | [diff] [blame] | 1485 | l->gf_len = (tmp & BCH_FLASHLAYOUT1_GF13_0_GF14_1_MASK) >> |
| 1486 | BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET; |
Igor Opaniuk | c5540137 | 2019-11-03 16:49:43 +0100 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | /* |
| 1490 | * Set BCH to specific layout used by ROM bootloader to read FCB. |
| 1491 | */ |
Han Xu | afed2a1 | 2020-05-06 20:59:19 +0800 | [diff] [blame^] | 1492 | void mxs_nand_mode_fcb_62bit(struct mtd_info *mtd) |
Igor Opaniuk | c5540137 | 2019-11-03 16:49:43 +0100 | [diff] [blame] | 1493 | { |
| 1494 | u32 tmp; |
| 1495 | struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE; |
| 1496 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 1497 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
| 1498 | |
| 1499 | nand_info->en_randomizer = 1; |
| 1500 | |
| 1501 | mtd->writesize = 1024; |
| 1502 | mtd->oobsize = 1862 - 1024; |
| 1503 | |
| 1504 | /* 8 ecc_chunks_*/ |
| 1505 | tmp = 7 << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET; |
| 1506 | /* 32 bytes for metadata */ |
| 1507 | tmp |= 32 << BCH_FLASHLAYOUT0_META_SIZE_OFFSET; |
| 1508 | /* using ECC62 level to be performed */ |
| 1509 | tmp |= 0x1F << BCH_FLASHLAYOUT0_ECC0_OFFSET; |
| 1510 | /* 0x20 * 4 bytes of the data0 block */ |
| 1511 | tmp |= 0x20 << BCH_FLASHLAYOUT0_DATA0_SIZE_OFFSET; |
| 1512 | tmp |= 0 << BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET; |
| 1513 | writel(tmp, &bch_regs->hw_bch_flash0layout0); |
| 1514 | |
| 1515 | /* 1024 for data + 838 for OOB */ |
| 1516 | tmp = 1862 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET; |
| 1517 | /* using ECC62 level to be performed */ |
| 1518 | tmp |= 0x1F << BCH_FLASHLAYOUT1_ECCN_OFFSET; |
| 1519 | /* 0x20 * 4 bytes of the data0 block */ |
| 1520 | tmp |= 0x20 << BCH_FLASHLAYOUT1_DATAN_SIZE_OFFSET; |
| 1521 | tmp |= 0 << BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET; |
| 1522 | writel(tmp, &bch_regs->hw_bch_flash0layout1); |
| 1523 | } |
| 1524 | |
| 1525 | /* |
Han Xu | afed2a1 | 2020-05-06 20:59:19 +0800 | [diff] [blame^] | 1526 | * Set BCH to specific layout used by ROM bootloader to read FCB. |
| 1527 | */ |
| 1528 | void mxs_nand_mode_fcb_40bit(struct mtd_info *mtd) |
| 1529 | { |
| 1530 | u32 tmp; |
| 1531 | struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE; |
| 1532 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 1533 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
| 1534 | |
| 1535 | /* no randomizer in this setting*/ |
| 1536 | nand_info->en_randomizer = 0; |
| 1537 | |
| 1538 | mtd->writesize = 1024; |
| 1539 | mtd->oobsize = 1576 - 1024; |
| 1540 | |
| 1541 | /* 8 ecc_chunks_*/ |
| 1542 | tmp = 7 << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET; |
| 1543 | /* 32 bytes for metadata */ |
| 1544 | tmp |= 32 << BCH_FLASHLAYOUT0_META_SIZE_OFFSET; |
| 1545 | /* using ECC40 level to be performed */ |
| 1546 | tmp |= 0x14 << BCH_FLASHLAYOUT0_ECC0_OFFSET; |
| 1547 | /* 0x20 * 4 bytes of the data0 block */ |
| 1548 | tmp |= 0x20 << BCH_FLASHLAYOUT0_DATA0_SIZE_OFFSET; |
| 1549 | tmp |= 0 << BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET; |
| 1550 | writel(tmp, &bch_regs->hw_bch_flash0layout0); |
| 1551 | |
| 1552 | /* 1024 for data + 552 for OOB */ |
| 1553 | tmp = 1576 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET; |
| 1554 | /* using ECC40 level to be performed */ |
| 1555 | tmp |= 0x14 << BCH_FLASHLAYOUT1_ECCN_OFFSET; |
| 1556 | /* 0x20 * 4 bytes of the data0 block */ |
| 1557 | tmp |= 0x20 << BCH_FLASHLAYOUT1_DATAN_SIZE_OFFSET; |
| 1558 | tmp |= 0 << BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET; |
| 1559 | writel(tmp, &bch_regs->hw_bch_flash0layout1); |
| 1560 | } |
| 1561 | |
| 1562 | /* |
Igor Opaniuk | c5540137 | 2019-11-03 16:49:43 +0100 | [diff] [blame] | 1563 | * Restore BCH to normal settings. |
| 1564 | */ |
| 1565 | void mxs_nand_mode_normal(struct mtd_info *mtd) |
| 1566 | { |
| 1567 | struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE; |
| 1568 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 1569 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
| 1570 | |
| 1571 | nand_info->en_randomizer = 0; |
| 1572 | |
| 1573 | mtd->writesize = nand_info->writesize; |
| 1574 | mtd->oobsize = nand_info->oobsize; |
| 1575 | |
| 1576 | writel(nand_info->bch_flash0layout0, &bch_regs->hw_bch_flash0layout0); |
| 1577 | writel(nand_info->bch_flash0layout1, &bch_regs->hw_bch_flash0layout1); |
| 1578 | } |
| 1579 | |
| 1580 | uint32_t mxs_nand_mark_byte_offset(struct mtd_info *mtd) |
| 1581 | { |
| 1582 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1583 | struct mxs_nand_info *nand_info = nand_get_controller_data(chip); |
| 1584 | struct bch_geometry *geo = &nand_info->bch_geometry; |
| 1585 | |
| 1586 | return geo->block_mark_byte_offset; |
| 1587 | } |
| 1588 | |
| 1589 | uint32_t mxs_nand_mark_bit_offset(struct mtd_info *mtd) |
| 1590 | { |
| 1591 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1592 | struct mxs_nand_info *nand_info = nand_get_controller_data(chip); |
| 1593 | struct bch_geometry *geo = &nand_info->bch_geometry; |
| 1594 | |
| 1595 | return geo->block_mark_bit_offset; |
| 1596 | } |