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