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. |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 13 | */ |
| 14 | |
Tom Warren | c88d30f | 2012-09-10 08:47:51 -0700 | [diff] [blame] | 15 | #include <common.h> |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 16 | #include <linux/mtd/mtd.h> |
Masahiro Yamada | 2b7a873 | 2017-11-30 13:45:24 +0900 | [diff] [blame] | 17 | #include <linux/mtd/rawnand.h> |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 18 | #include <linux/types.h> |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 19 | #include <malloc.h> |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 20 | #include <nand.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 21 | #include <linux/errno.h> |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 22 | #include <asm/io.h> |
| 23 | #include <asm/arch/clock.h> |
| 24 | #include <asm/arch/imx-regs.h> |
Stefano Babic | 33731bc | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 25 | #include <asm/mach-imx/regs-bch.h> |
| 26 | #include <asm/mach-imx/regs-gpmi.h> |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 27 | #include <asm/arch/sys_proto.h> |
Stefano Babic | 33731bc | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 28 | #include <asm/mach-imx/dma.h> |
Stefan Agner | 7152f34 | 2018-06-22 17:19:46 +0200 | [diff] [blame] | 29 | #include "mxs_nand.h" |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 30 | |
| 31 | #define MXS_NAND_DMA_DESCRIPTOR_COUNT 4 |
| 32 | |
| 33 | #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE 512 |
Peng Fan | 007c8da | 2015-12-22 17:04:23 +0800 | [diff] [blame] | 34 | #if (defined(CONFIG_MX6) || defined(CONFIG_MX7)) |
Stefan Roese | 8338d1d | 2013-04-15 21:14:12 +0000 | [diff] [blame] | 35 | #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT 2 |
| 36 | #else |
| 37 | #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT 0 |
| 38 | #endif |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 39 | #define MXS_NAND_METADATA_SIZE 10 |
Jörg Krause | 1d87026 | 2015-04-15 09:27:22 +0200 | [diff] [blame] | 40 | #define MXS_NAND_BITS_PER_ECC_LEVEL 13 |
Stefan Agner | 54bf808 | 2016-08-01 23:55:18 -0700 | [diff] [blame] | 41 | |
| 42 | #if !defined(CONFIG_SYS_CACHELINE_SIZE) || CONFIG_SYS_CACHELINE_SIZE < 32 |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 43 | #define MXS_NAND_COMMAND_BUFFER_SIZE 32 |
Stefan Agner | 54bf808 | 2016-08-01 23:55:18 -0700 | [diff] [blame] | 44 | #else |
| 45 | #define MXS_NAND_COMMAND_BUFFER_SIZE CONFIG_SYS_CACHELINE_SIZE |
| 46 | #endif |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 47 | |
| 48 | #define MXS_NAND_BCH_TIMEOUT 10000 |
| 49 | |
| 50 | struct mxs_nand_info { |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 51 | struct nand_chip chip; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 52 | int cur_chip; |
| 53 | |
| 54 | uint32_t cmd_queue_len; |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 55 | uint32_t data_buf_size; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 56 | |
| 57 | uint8_t *cmd_buf; |
| 58 | uint8_t *data_buf; |
| 59 | uint8_t *oob_buf; |
| 60 | |
| 61 | uint8_t marking_block_bad; |
| 62 | uint8_t raw_oob_mode; |
| 63 | |
| 64 | /* Functions with altered behaviour */ |
| 65 | int (*hooked_read_oob)(struct mtd_info *mtd, |
| 66 | loff_t from, struct mtd_oob_ops *ops); |
| 67 | int (*hooked_write_oob)(struct mtd_info *mtd, |
| 68 | loff_t to, struct mtd_oob_ops *ops); |
| 69 | int (*hooked_block_markbad)(struct mtd_info *mtd, |
| 70 | loff_t ofs); |
| 71 | |
| 72 | /* DMA descriptors */ |
| 73 | struct mxs_dma_desc **desc; |
| 74 | uint32_t desc_index; |
| 75 | }; |
| 76 | |
| 77 | struct nand_ecclayout fake_ecc_layout; |
Peng Fan | c94f09d | 2015-07-21 16:15:19 +0800 | [diff] [blame] | 78 | static int chunk_data_size = MXS_NAND_CHUNK_DATA_CHUNK_SIZE; |
| 79 | static int galois_field = 13; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 80 | |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 81 | /* |
| 82 | * Cache management functions |
| 83 | */ |
| 84 | #ifndef CONFIG_SYS_DCACHE_OFF |
| 85 | static void mxs_nand_flush_data_buf(struct mxs_nand_info *info) |
| 86 | { |
| 87 | uint32_t addr = (uint32_t)info->data_buf; |
| 88 | |
| 89 | flush_dcache_range(addr, addr + info->data_buf_size); |
| 90 | } |
| 91 | |
| 92 | static void mxs_nand_inval_data_buf(struct mxs_nand_info *info) |
| 93 | { |
| 94 | uint32_t addr = (uint32_t)info->data_buf; |
| 95 | |
| 96 | invalidate_dcache_range(addr, addr + info->data_buf_size); |
| 97 | } |
| 98 | |
| 99 | static void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info) |
| 100 | { |
| 101 | uint32_t addr = (uint32_t)info->cmd_buf; |
| 102 | |
| 103 | flush_dcache_range(addr, addr + MXS_NAND_COMMAND_BUFFER_SIZE); |
| 104 | } |
| 105 | #else |
| 106 | static inline void mxs_nand_flush_data_buf(struct mxs_nand_info *info) {} |
| 107 | static inline void mxs_nand_inval_data_buf(struct mxs_nand_info *info) {} |
| 108 | static inline void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info) {} |
| 109 | #endif |
| 110 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 111 | static struct mxs_dma_desc *mxs_nand_get_dma_desc(struct mxs_nand_info *info) |
| 112 | { |
| 113 | struct mxs_dma_desc *desc; |
| 114 | |
| 115 | if (info->desc_index >= MXS_NAND_DMA_DESCRIPTOR_COUNT) { |
| 116 | printf("MXS NAND: Too many DMA descriptors requested\n"); |
| 117 | return NULL; |
| 118 | } |
| 119 | |
| 120 | desc = info->desc[info->desc_index]; |
| 121 | info->desc_index++; |
| 122 | |
| 123 | return desc; |
| 124 | } |
| 125 | |
| 126 | static void mxs_nand_return_dma_descs(struct mxs_nand_info *info) |
| 127 | { |
| 128 | int i; |
| 129 | struct mxs_dma_desc *desc; |
| 130 | |
| 131 | for (i = 0; i < info->desc_index; i++) { |
| 132 | desc = info->desc[i]; |
| 133 | memset(desc, 0, sizeof(struct mxs_dma_desc)); |
| 134 | desc->address = (dma_addr_t)desc; |
| 135 | } |
| 136 | |
| 137 | info->desc_index = 0; |
| 138 | } |
| 139 | |
| 140 | static uint32_t mxs_nand_ecc_chunk_cnt(uint32_t page_data_size) |
| 141 | { |
Peng Fan | c94f09d | 2015-07-21 16:15:19 +0800 | [diff] [blame] | 142 | return page_data_size / chunk_data_size; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static uint32_t mxs_nand_ecc_size_in_bits(uint32_t ecc_strength) |
| 146 | { |
Peng Fan | c94f09d | 2015-07-21 16:15:19 +0800 | [diff] [blame] | 147 | return ecc_strength * galois_field; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static uint32_t mxs_nand_aux_status_offset(void) |
| 151 | { |
| 152 | return (MXS_NAND_METADATA_SIZE + 0x3) & ~0x3; |
| 153 | } |
| 154 | |
| 155 | static inline uint32_t mxs_nand_get_ecc_strength(uint32_t page_data_size, |
| 156 | uint32_t page_oob_size) |
| 157 | { |
Peng Fan | c6fe144 | 2015-04-15 09:27:21 +0200 | [diff] [blame] | 158 | int ecc_strength; |
Peng Fan | 2a0bb35 | 2015-09-07 16:12:11 +0800 | [diff] [blame] | 159 | int max_ecc_strength_supported; |
| 160 | |
| 161 | /* Refer to Chapter 17 for i.MX6DQ, Chapter 18 for i.MX6SX */ |
Peng Fan | 4e70a68 | 2016-05-23 18:36:02 +0800 | [diff] [blame] | 162 | if (is_mx6sx() || is_mx7()) |
Peng Fan | 2a0bb35 | 2015-09-07 16:12:11 +0800 | [diff] [blame] | 163 | max_ecc_strength_supported = 62; |
| 164 | else |
| 165 | max_ecc_strength_supported = 40; |
Marek Vasut | e854108 | 2014-09-25 21:13:36 +0200 | [diff] [blame] | 166 | |
Peng Fan | c6fe144 | 2015-04-15 09:27:21 +0200 | [diff] [blame] | 167 | /* |
| 168 | * Determine the ECC layout with the formula: |
| 169 | * ECC bits per chunk = (total page spare data bits) / |
| 170 | * (bits per ECC level) / (chunks per page) |
| 171 | * where: |
| 172 | * total page spare data bits = |
| 173 | * (page oob size - meta data size) * (bits per byte) |
| 174 | */ |
| 175 | ecc_strength = ((page_oob_size - MXS_NAND_METADATA_SIZE) * 8) |
Peng Fan | c94f09d | 2015-07-21 16:15:19 +0800 | [diff] [blame] | 176 | / (galois_field * |
| 177 | mxs_nand_ecc_chunk_cnt(page_data_size)); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 178 | |
Peng Fan | 2a0bb35 | 2015-09-07 16:12:11 +0800 | [diff] [blame] | 179 | return min(round_down(ecc_strength, 2), max_ecc_strength_supported); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static inline uint32_t mxs_nand_get_mark_offset(uint32_t page_data_size, |
| 183 | uint32_t ecc_strength) |
| 184 | { |
| 185 | uint32_t chunk_data_size_in_bits; |
| 186 | uint32_t chunk_ecc_size_in_bits; |
| 187 | uint32_t chunk_total_size_in_bits; |
| 188 | uint32_t block_mark_chunk_number; |
| 189 | uint32_t block_mark_chunk_bit_offset; |
| 190 | uint32_t block_mark_bit_offset; |
| 191 | |
Peng Fan | c94f09d | 2015-07-21 16:15:19 +0800 | [diff] [blame] | 192 | chunk_data_size_in_bits = chunk_data_size * 8; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 193 | chunk_ecc_size_in_bits = mxs_nand_ecc_size_in_bits(ecc_strength); |
| 194 | |
| 195 | chunk_total_size_in_bits = |
| 196 | chunk_data_size_in_bits + chunk_ecc_size_in_bits; |
| 197 | |
| 198 | /* Compute the bit offset of the block mark within the physical page. */ |
| 199 | block_mark_bit_offset = page_data_size * 8; |
| 200 | |
| 201 | /* Subtract the metadata bits. */ |
| 202 | block_mark_bit_offset -= MXS_NAND_METADATA_SIZE * 8; |
| 203 | |
| 204 | /* |
| 205 | * Compute the chunk number (starting at zero) in which the block mark |
| 206 | * appears. |
| 207 | */ |
| 208 | block_mark_chunk_number = |
| 209 | block_mark_bit_offset / chunk_total_size_in_bits; |
| 210 | |
| 211 | /* |
| 212 | * Compute the bit offset of the block mark within its chunk, and |
| 213 | * validate it. |
| 214 | */ |
| 215 | block_mark_chunk_bit_offset = block_mark_bit_offset - |
| 216 | (block_mark_chunk_number * chunk_total_size_in_bits); |
| 217 | |
| 218 | if (block_mark_chunk_bit_offset > chunk_data_size_in_bits) |
| 219 | return 1; |
| 220 | |
| 221 | /* |
| 222 | * Now that we know the chunk number in which the block mark appears, |
| 223 | * we can subtract all the ECC bits that appear before it. |
| 224 | */ |
| 225 | block_mark_bit_offset -= |
| 226 | block_mark_chunk_number * chunk_ecc_size_in_bits; |
| 227 | |
| 228 | return block_mark_bit_offset; |
| 229 | } |
| 230 | |
| 231 | static uint32_t mxs_nand_mark_byte_offset(struct mtd_info *mtd) |
| 232 | { |
| 233 | uint32_t ecc_strength; |
| 234 | ecc_strength = mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize); |
| 235 | return mxs_nand_get_mark_offset(mtd->writesize, ecc_strength) >> 3; |
| 236 | } |
| 237 | |
| 238 | static uint32_t mxs_nand_mark_bit_offset(struct mtd_info *mtd) |
| 239 | { |
| 240 | uint32_t ecc_strength; |
| 241 | ecc_strength = mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize); |
| 242 | return mxs_nand_get_mark_offset(mtd->writesize, ecc_strength) & 0x7; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Wait for BCH complete IRQ and clear the IRQ |
| 247 | */ |
| 248 | static int mxs_nand_wait_for_bch_complete(void) |
| 249 | { |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 250 | struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 251 | int timeout = MXS_NAND_BCH_TIMEOUT; |
| 252 | int ret; |
| 253 | |
Otavio Salvador | cbf0bf2 | 2012-08-13 09:53:12 +0000 | [diff] [blame] | 254 | ret = mxs_wait_mask_set(&bch_regs->hw_bch_ctrl_reg, |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 255 | BCH_CTRL_COMPLETE_IRQ, timeout); |
| 256 | |
| 257 | writel(BCH_CTRL_COMPLETE_IRQ, &bch_regs->hw_bch_ctrl_clr); |
| 258 | |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * This is the function that we install in the cmd_ctrl function pointer of the |
| 264 | * owning struct nand_chip. The only functions in the reference implementation |
| 265 | * that use these functions pointers are cmdfunc and select_chip. |
| 266 | * |
| 267 | * In this driver, we implement our own select_chip, so this function will only |
| 268 | * be called by the reference implementation's cmdfunc. For this reason, we can |
| 269 | * ignore the chip enable bit and concentrate only on sending bytes to the NAND |
| 270 | * Flash. |
| 271 | */ |
| 272 | static void mxs_nand_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl) |
| 273 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 274 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 275 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 276 | struct mxs_dma_desc *d; |
| 277 | uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip; |
| 278 | int ret; |
| 279 | |
| 280 | /* |
| 281 | * If this condition is true, something is _VERY_ wrong in MTD |
| 282 | * subsystem! |
| 283 | */ |
| 284 | if (nand_info->cmd_queue_len == MXS_NAND_COMMAND_BUFFER_SIZE) { |
| 285 | printf("MXS NAND: Command queue too long\n"); |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Every operation begins with a command byte and a series of zero or |
| 291 | * more address bytes. These are distinguished by either the Address |
| 292 | * Latch Enable (ALE) or Command Latch Enable (CLE) signals being |
| 293 | * asserted. When MTD is ready to execute the command, it will |
| 294 | * deasert both latch enables. |
| 295 | * |
| 296 | * Rather than run a separate DMA operation for every single byte, we |
| 297 | * queue them up and run a single DMA operation for the entire series |
| 298 | * of command and data bytes. |
| 299 | */ |
| 300 | if (ctrl & (NAND_ALE | NAND_CLE)) { |
| 301 | if (data != NAND_CMD_NONE) |
| 302 | nand_info->cmd_buf[nand_info->cmd_queue_len++] = data; |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * If control arrives here, MTD has deasserted both the ALE and CLE, |
| 308 | * which means it's ready to run an operation. Check if we have any |
| 309 | * bytes to send. |
| 310 | */ |
| 311 | if (nand_info->cmd_queue_len == 0) |
| 312 | return; |
| 313 | |
| 314 | /* Compile the DMA descriptor -- a descriptor that sends command. */ |
| 315 | d = mxs_nand_get_dma_desc(nand_info); |
| 316 | d->cmd.data = |
| 317 | MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ | |
| 318 | MXS_DMA_DESC_CHAIN | MXS_DMA_DESC_DEC_SEM | |
| 319 | MXS_DMA_DESC_WAIT4END | (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET) | |
| 320 | (nand_info->cmd_queue_len << MXS_DMA_DESC_BYTES_OFFSET); |
| 321 | |
| 322 | d->cmd.address = (dma_addr_t)nand_info->cmd_buf; |
| 323 | |
| 324 | d->cmd.pio_words[0] = |
| 325 | GPMI_CTRL0_COMMAND_MODE_WRITE | |
| 326 | GPMI_CTRL0_WORD_LENGTH | |
| 327 | (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) | |
| 328 | GPMI_CTRL0_ADDRESS_NAND_CLE | |
| 329 | GPMI_CTRL0_ADDRESS_INCREMENT | |
| 330 | nand_info->cmd_queue_len; |
| 331 | |
| 332 | mxs_dma_desc_append(channel, d); |
| 333 | |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 334 | /* Flush caches */ |
| 335 | mxs_nand_flush_cmd_buf(nand_info); |
| 336 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 337 | /* Execute the DMA chain. */ |
| 338 | ret = mxs_dma_go(channel); |
| 339 | if (ret) |
| 340 | printf("MXS NAND: Error sending command\n"); |
| 341 | |
| 342 | mxs_nand_return_dma_descs(nand_info); |
| 343 | |
| 344 | /* Reset the command queue. */ |
| 345 | nand_info->cmd_queue_len = 0; |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Test if the NAND flash is ready. |
| 350 | */ |
| 351 | static int mxs_nand_device_ready(struct mtd_info *mtd) |
| 352 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 353 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 354 | struct mxs_nand_info *nand_info = nand_get_controller_data(chip); |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 355 | struct mxs_gpmi_regs *gpmi_regs = |
| 356 | (struct mxs_gpmi_regs *)MXS_GPMI_BASE; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 357 | uint32_t tmp; |
| 358 | |
| 359 | tmp = readl(&gpmi_regs->hw_gpmi_stat); |
| 360 | tmp >>= (GPMI_STAT_READY_BUSY_OFFSET + nand_info->cur_chip); |
| 361 | |
| 362 | return tmp & 1; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Select the NAND chip. |
| 367 | */ |
| 368 | static void mxs_nand_select_chip(struct mtd_info *mtd, int chip) |
| 369 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 370 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 371 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 372 | |
| 373 | nand_info->cur_chip = chip; |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * Handle block mark swapping. |
| 378 | * |
| 379 | * Note that, when this function is called, it doesn't know whether it's |
| 380 | * swapping the block mark, or swapping it *back* -- but it doesn't matter |
| 381 | * because the the operation is the same. |
| 382 | */ |
| 383 | static void mxs_nand_swap_block_mark(struct mtd_info *mtd, |
| 384 | uint8_t *data_buf, uint8_t *oob_buf) |
| 385 | { |
| 386 | uint32_t bit_offset; |
| 387 | uint32_t buf_offset; |
| 388 | |
| 389 | uint32_t src; |
| 390 | uint32_t dst; |
| 391 | |
| 392 | bit_offset = mxs_nand_mark_bit_offset(mtd); |
| 393 | buf_offset = mxs_nand_mark_byte_offset(mtd); |
| 394 | |
| 395 | /* |
| 396 | * Get the byte from the data area that overlays the block mark. Since |
| 397 | * the ECC engine applies its own view to the bits in the page, the |
| 398 | * physical block mark won't (in general) appear on a byte boundary in |
| 399 | * the data. |
| 400 | */ |
| 401 | src = data_buf[buf_offset] >> bit_offset; |
| 402 | src |= data_buf[buf_offset + 1] << (8 - bit_offset); |
| 403 | |
| 404 | dst = oob_buf[0]; |
| 405 | |
| 406 | oob_buf[0] = src; |
| 407 | |
| 408 | data_buf[buf_offset] &= ~(0xff << bit_offset); |
| 409 | data_buf[buf_offset + 1] &= 0xff << bit_offset; |
| 410 | |
| 411 | data_buf[buf_offset] |= dst << bit_offset; |
| 412 | data_buf[buf_offset + 1] |= dst >> (8 - bit_offset); |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * Read data from NAND. |
| 417 | */ |
| 418 | static void mxs_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int length) |
| 419 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 420 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 421 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 422 | struct mxs_dma_desc *d; |
| 423 | uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip; |
| 424 | int ret; |
| 425 | |
| 426 | if (length > NAND_MAX_PAGESIZE) { |
| 427 | printf("MXS NAND: DMA buffer too big\n"); |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | if (!buf) { |
| 432 | printf("MXS NAND: DMA buffer is NULL\n"); |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | /* Compile the DMA descriptor - a descriptor that reads data. */ |
| 437 | d = mxs_nand_get_dma_desc(nand_info); |
| 438 | d->cmd.data = |
| 439 | MXS_DMA_DESC_COMMAND_DMA_WRITE | MXS_DMA_DESC_IRQ | |
| 440 | MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END | |
| 441 | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) | |
| 442 | (length << MXS_DMA_DESC_BYTES_OFFSET); |
| 443 | |
| 444 | d->cmd.address = (dma_addr_t)nand_info->data_buf; |
| 445 | |
| 446 | d->cmd.pio_words[0] = |
| 447 | GPMI_CTRL0_COMMAND_MODE_READ | |
| 448 | GPMI_CTRL0_WORD_LENGTH | |
| 449 | (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) | |
| 450 | GPMI_CTRL0_ADDRESS_NAND_DATA | |
| 451 | length; |
| 452 | |
| 453 | mxs_dma_desc_append(channel, d); |
| 454 | |
| 455 | /* |
| 456 | * A DMA descriptor that waits for the command to end and the chip to |
| 457 | * become ready. |
| 458 | * |
| 459 | * I think we actually should *not* be waiting for the chip to become |
| 460 | * ready because, after all, we don't care. I think the original code |
| 461 | * did that and no one has re-thought it yet. |
| 462 | */ |
| 463 | d = mxs_nand_get_dma_desc(nand_info); |
| 464 | d->cmd.data = |
| 465 | MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ | |
| 466 | MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_DEC_SEM | |
Luca Ellero | 80f06b8 | 2014-12-16 15:36:14 +0100 | [diff] [blame] | 467 | MXS_DMA_DESC_WAIT4END | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 468 | |
| 469 | d->cmd.address = 0; |
| 470 | |
| 471 | d->cmd.pio_words[0] = |
| 472 | GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY | |
| 473 | GPMI_CTRL0_WORD_LENGTH | |
| 474 | (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) | |
| 475 | GPMI_CTRL0_ADDRESS_NAND_DATA; |
| 476 | |
| 477 | mxs_dma_desc_append(channel, d); |
| 478 | |
Peng Fan | e3bbfb7 | 2015-07-21 16:15:21 +0800 | [diff] [blame] | 479 | /* Invalidate caches */ |
| 480 | mxs_nand_inval_data_buf(nand_info); |
| 481 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 482 | /* Execute the DMA chain. */ |
| 483 | ret = mxs_dma_go(channel); |
| 484 | if (ret) { |
| 485 | printf("MXS NAND: DMA read error\n"); |
| 486 | goto rtn; |
| 487 | } |
| 488 | |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 489 | /* Invalidate caches */ |
| 490 | mxs_nand_inval_data_buf(nand_info); |
| 491 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 492 | memcpy(buf, nand_info->data_buf, length); |
| 493 | |
| 494 | rtn: |
| 495 | mxs_nand_return_dma_descs(nand_info); |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * Write data to NAND. |
| 500 | */ |
| 501 | static void mxs_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, |
| 502 | int length) |
| 503 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 504 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 505 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 506 | struct mxs_dma_desc *d; |
| 507 | uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip; |
| 508 | int ret; |
| 509 | |
| 510 | if (length > NAND_MAX_PAGESIZE) { |
| 511 | printf("MXS NAND: DMA buffer too big\n"); |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | if (!buf) { |
| 516 | printf("MXS NAND: DMA buffer is NULL\n"); |
| 517 | return; |
| 518 | } |
| 519 | |
| 520 | memcpy(nand_info->data_buf, buf, length); |
| 521 | |
| 522 | /* Compile the DMA descriptor - a descriptor that writes data. */ |
| 523 | d = mxs_nand_get_dma_desc(nand_info); |
| 524 | d->cmd.data = |
| 525 | MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ | |
| 526 | MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END | |
Luca Ellero | 966f1cd | 2014-12-16 15:36:15 +0100 | [diff] [blame] | 527 | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 528 | (length << MXS_DMA_DESC_BYTES_OFFSET); |
| 529 | |
| 530 | d->cmd.address = (dma_addr_t)nand_info->data_buf; |
| 531 | |
| 532 | d->cmd.pio_words[0] = |
| 533 | GPMI_CTRL0_COMMAND_MODE_WRITE | |
| 534 | GPMI_CTRL0_WORD_LENGTH | |
| 535 | (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) | |
| 536 | GPMI_CTRL0_ADDRESS_NAND_DATA | |
| 537 | length; |
| 538 | |
| 539 | mxs_dma_desc_append(channel, d); |
| 540 | |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 541 | /* Flush caches */ |
| 542 | mxs_nand_flush_data_buf(nand_info); |
| 543 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 544 | /* Execute the DMA chain. */ |
| 545 | ret = mxs_dma_go(channel); |
| 546 | if (ret) |
| 547 | printf("MXS NAND: DMA write error\n"); |
| 548 | |
| 549 | mxs_nand_return_dma_descs(nand_info); |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * Read a single byte from NAND. |
| 554 | */ |
| 555 | static uint8_t mxs_nand_read_byte(struct mtd_info *mtd) |
| 556 | { |
| 557 | uint8_t buf; |
| 558 | mxs_nand_read_buf(mtd, &buf, 1); |
| 559 | return buf; |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * Read a page from NAND. |
| 564 | */ |
| 565 | 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] | 566 | uint8_t *buf, int oob_required, |
| 567 | int page) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 568 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 569 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 570 | struct mxs_dma_desc *d; |
| 571 | uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip; |
| 572 | uint32_t corrected = 0, failed = 0; |
| 573 | uint8_t *status; |
| 574 | int i, ret; |
| 575 | |
| 576 | /* Compile the DMA descriptor - wait for ready. */ |
| 577 | d = mxs_nand_get_dma_desc(nand_info); |
| 578 | d->cmd.data = |
| 579 | MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN | |
| 580 | MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END | |
| 581 | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET); |
| 582 | |
| 583 | d->cmd.address = 0; |
| 584 | |
| 585 | d->cmd.pio_words[0] = |
| 586 | GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY | |
| 587 | GPMI_CTRL0_WORD_LENGTH | |
| 588 | (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) | |
| 589 | GPMI_CTRL0_ADDRESS_NAND_DATA; |
| 590 | |
| 591 | mxs_dma_desc_append(channel, d); |
| 592 | |
| 593 | /* Compile the DMA descriptor - enable the BCH block and read. */ |
| 594 | d = mxs_nand_get_dma_desc(nand_info); |
| 595 | d->cmd.data = |
| 596 | MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN | |
| 597 | MXS_DMA_DESC_WAIT4END | (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET); |
| 598 | |
| 599 | d->cmd.address = 0; |
| 600 | |
| 601 | d->cmd.pio_words[0] = |
| 602 | GPMI_CTRL0_COMMAND_MODE_READ | |
| 603 | GPMI_CTRL0_WORD_LENGTH | |
| 604 | (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) | |
| 605 | GPMI_CTRL0_ADDRESS_NAND_DATA | |
| 606 | (mtd->writesize + mtd->oobsize); |
| 607 | d->cmd.pio_words[1] = 0; |
| 608 | d->cmd.pio_words[2] = |
| 609 | GPMI_ECCCTRL_ENABLE_ECC | |
| 610 | GPMI_ECCCTRL_ECC_CMD_DECODE | |
| 611 | GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE; |
| 612 | d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize; |
| 613 | d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf; |
| 614 | d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf; |
| 615 | |
| 616 | mxs_dma_desc_append(channel, d); |
| 617 | |
| 618 | /* Compile the DMA descriptor - disable the BCH block. */ |
| 619 | d = mxs_nand_get_dma_desc(nand_info); |
| 620 | d->cmd.data = |
| 621 | MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN | |
| 622 | MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END | |
| 623 | (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET); |
| 624 | |
| 625 | d->cmd.address = 0; |
| 626 | |
| 627 | d->cmd.pio_words[0] = |
| 628 | GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY | |
| 629 | GPMI_CTRL0_WORD_LENGTH | |
| 630 | (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) | |
| 631 | GPMI_CTRL0_ADDRESS_NAND_DATA | |
| 632 | (mtd->writesize + mtd->oobsize); |
| 633 | d->cmd.pio_words[1] = 0; |
| 634 | d->cmd.pio_words[2] = 0; |
| 635 | |
| 636 | mxs_dma_desc_append(channel, d); |
| 637 | |
| 638 | /* Compile the DMA descriptor - deassert the NAND lock and interrupt. */ |
| 639 | d = mxs_nand_get_dma_desc(nand_info); |
| 640 | d->cmd.data = |
| 641 | MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ | |
| 642 | MXS_DMA_DESC_DEC_SEM; |
| 643 | |
| 644 | d->cmd.address = 0; |
| 645 | |
| 646 | mxs_dma_desc_append(channel, d); |
| 647 | |
Peng Fan | e3bbfb7 | 2015-07-21 16:15:21 +0800 | [diff] [blame] | 648 | /* Invalidate caches */ |
| 649 | mxs_nand_inval_data_buf(nand_info); |
| 650 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 651 | /* Execute the DMA chain. */ |
| 652 | ret = mxs_dma_go(channel); |
| 653 | if (ret) { |
| 654 | printf("MXS NAND: DMA read error\n"); |
| 655 | goto rtn; |
| 656 | } |
| 657 | |
| 658 | ret = mxs_nand_wait_for_bch_complete(); |
| 659 | if (ret) { |
| 660 | printf("MXS NAND: BCH read timeout\n"); |
| 661 | goto rtn; |
| 662 | } |
| 663 | |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 664 | /* Invalidate caches */ |
| 665 | mxs_nand_inval_data_buf(nand_info); |
| 666 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 667 | /* Read DMA completed, now do the mark swapping. */ |
| 668 | mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf); |
| 669 | |
| 670 | /* Loop over status bytes, accumulating ECC status. */ |
| 671 | status = nand_info->oob_buf + mxs_nand_aux_status_offset(); |
| 672 | for (i = 0; i < mxs_nand_ecc_chunk_cnt(mtd->writesize); i++) { |
| 673 | if (status[i] == 0x00) |
| 674 | continue; |
| 675 | |
| 676 | if (status[i] == 0xff) |
| 677 | continue; |
| 678 | |
| 679 | if (status[i] == 0xfe) { |
| 680 | failed++; |
| 681 | continue; |
| 682 | } |
| 683 | |
| 684 | corrected += status[i]; |
| 685 | } |
| 686 | |
| 687 | /* Propagate ECC status to the owning MTD. */ |
| 688 | mtd->ecc_stats.failed += failed; |
| 689 | mtd->ecc_stats.corrected += corrected; |
| 690 | |
| 691 | /* |
| 692 | * It's time to deliver the OOB bytes. See mxs_nand_ecc_read_oob() for |
| 693 | * details about our policy for delivering the OOB. |
| 694 | * |
| 695 | * We fill the caller's buffer with set bits, and then copy the block |
| 696 | * mark to the caller's buffer. Note that, if block mark swapping was |
| 697 | * necessary, it has already been done, so we can rely on the first |
| 698 | * byte of the auxiliary buffer to contain the block mark. |
| 699 | */ |
| 700 | memset(nand->oob_poi, 0xff, mtd->oobsize); |
| 701 | |
| 702 | nand->oob_poi[0] = nand_info->oob_buf[0]; |
| 703 | |
| 704 | memcpy(buf, nand_info->data_buf, mtd->writesize); |
| 705 | |
| 706 | rtn: |
| 707 | mxs_nand_return_dma_descs(nand_info); |
| 708 | |
| 709 | return ret; |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * Write a page to NAND. |
| 714 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 715 | static int mxs_nand_ecc_write_page(struct mtd_info *mtd, |
| 716 | struct nand_chip *nand, const uint8_t *buf, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 717 | int oob_required, int page) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 718 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 719 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 720 | struct mxs_dma_desc *d; |
| 721 | uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip; |
| 722 | int ret; |
| 723 | |
| 724 | memcpy(nand_info->data_buf, buf, mtd->writesize); |
| 725 | memcpy(nand_info->oob_buf, nand->oob_poi, mtd->oobsize); |
| 726 | |
| 727 | /* Handle block mark swapping. */ |
| 728 | mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf); |
| 729 | |
| 730 | /* Compile the DMA descriptor - write data. */ |
| 731 | d = mxs_nand_get_dma_desc(nand_info); |
| 732 | d->cmd.data = |
| 733 | MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ | |
| 734 | MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END | |
| 735 | (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET); |
| 736 | |
| 737 | d->cmd.address = 0; |
| 738 | |
| 739 | d->cmd.pio_words[0] = |
| 740 | GPMI_CTRL0_COMMAND_MODE_WRITE | |
| 741 | GPMI_CTRL0_WORD_LENGTH | |
| 742 | (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) | |
| 743 | GPMI_CTRL0_ADDRESS_NAND_DATA; |
| 744 | d->cmd.pio_words[1] = 0; |
| 745 | d->cmd.pio_words[2] = |
| 746 | GPMI_ECCCTRL_ENABLE_ECC | |
| 747 | GPMI_ECCCTRL_ECC_CMD_ENCODE | |
| 748 | GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE; |
| 749 | d->cmd.pio_words[3] = (mtd->writesize + mtd->oobsize); |
| 750 | d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf; |
| 751 | d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf; |
| 752 | |
| 753 | mxs_dma_desc_append(channel, d); |
| 754 | |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 755 | /* Flush caches */ |
| 756 | mxs_nand_flush_data_buf(nand_info); |
| 757 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 758 | /* Execute the DMA chain. */ |
| 759 | ret = mxs_dma_go(channel); |
| 760 | if (ret) { |
| 761 | printf("MXS NAND: DMA write error\n"); |
| 762 | goto rtn; |
| 763 | } |
| 764 | |
| 765 | ret = mxs_nand_wait_for_bch_complete(); |
| 766 | if (ret) { |
| 767 | printf("MXS NAND: BCH write timeout\n"); |
| 768 | goto rtn; |
| 769 | } |
| 770 | |
| 771 | rtn: |
| 772 | mxs_nand_return_dma_descs(nand_info); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 773 | return 0; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /* |
| 777 | * Read OOB from NAND. |
| 778 | * |
| 779 | * This function is a veneer that replaces the function originally installed by |
| 780 | * the NAND Flash MTD code. |
| 781 | */ |
| 782 | static int mxs_nand_hook_read_oob(struct mtd_info *mtd, loff_t from, |
| 783 | struct mtd_oob_ops *ops) |
| 784 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 785 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 786 | struct mxs_nand_info *nand_info = nand_get_controller_data(chip); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 787 | int ret; |
| 788 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 789 | if (ops->mode == MTD_OPS_RAW) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 790 | nand_info->raw_oob_mode = 1; |
| 791 | else |
| 792 | nand_info->raw_oob_mode = 0; |
| 793 | |
| 794 | ret = nand_info->hooked_read_oob(mtd, from, ops); |
| 795 | |
| 796 | nand_info->raw_oob_mode = 0; |
| 797 | |
| 798 | return ret; |
| 799 | } |
| 800 | |
| 801 | /* |
| 802 | * Write OOB to NAND. |
| 803 | * |
| 804 | * This function is a veneer that replaces the function originally installed by |
| 805 | * the NAND Flash MTD code. |
| 806 | */ |
| 807 | static int mxs_nand_hook_write_oob(struct mtd_info *mtd, loff_t to, |
| 808 | struct mtd_oob_ops *ops) |
| 809 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 810 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 811 | struct mxs_nand_info *nand_info = nand_get_controller_data(chip); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 812 | int ret; |
| 813 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 814 | if (ops->mode == MTD_OPS_RAW) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 815 | nand_info->raw_oob_mode = 1; |
| 816 | else |
| 817 | nand_info->raw_oob_mode = 0; |
| 818 | |
| 819 | ret = nand_info->hooked_write_oob(mtd, to, ops); |
| 820 | |
| 821 | nand_info->raw_oob_mode = 0; |
| 822 | |
| 823 | return ret; |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * Mark a block bad in NAND. |
| 828 | * |
| 829 | * This function is a veneer that replaces the function originally installed by |
| 830 | * the NAND Flash MTD code. |
| 831 | */ |
| 832 | static int mxs_nand_hook_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 833 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 834 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 835 | struct mxs_nand_info *nand_info = nand_get_controller_data(chip); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 836 | int ret; |
| 837 | |
| 838 | nand_info->marking_block_bad = 1; |
| 839 | |
| 840 | ret = nand_info->hooked_block_markbad(mtd, ofs); |
| 841 | |
| 842 | nand_info->marking_block_bad = 0; |
| 843 | |
| 844 | return ret; |
| 845 | } |
| 846 | |
| 847 | /* |
| 848 | * There are several places in this driver where we have to handle the OOB and |
| 849 | * block marks. This is the function where things are the most complicated, so |
| 850 | * this is where we try to explain it all. All the other places refer back to |
| 851 | * here. |
| 852 | * |
| 853 | * These are the rules, in order of decreasing importance: |
| 854 | * |
| 855 | * 1) Nothing the caller does can be allowed to imperil the block mark, so all |
| 856 | * write operations take measures to protect it. |
| 857 | * |
| 858 | * 2) In read operations, the first byte of the OOB we return must reflect the |
| 859 | * true state of the block mark, no matter where that block mark appears in |
| 860 | * the physical page. |
| 861 | * |
| 862 | * 3) ECC-based read operations return an OOB full of set bits (since we never |
| 863 | * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads |
| 864 | * return). |
| 865 | * |
| 866 | * 4) "Raw" read operations return a direct view of the physical bytes in the |
| 867 | * page, using the conventional definition of which bytes are data and which |
| 868 | * are OOB. This gives the caller a way to see the actual, physical bytes |
| 869 | * in the page, without the distortions applied by our ECC engine. |
| 870 | * |
| 871 | * What we do for this specific read operation depends on whether we're doing |
| 872 | * "raw" read, or an ECC-based read. |
| 873 | * |
| 874 | * It turns out that knowing whether we want an "ECC-based" or "raw" read is not |
| 875 | * easy. When reading a page, for example, the NAND Flash MTD code calls our |
| 876 | * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an |
| 877 | * ECC-based or raw view of the page is implicit in which function it calls |
| 878 | * (there is a similar pair of ECC-based/raw functions for writing). |
| 879 | * |
| 880 | * Since MTD assumes the OOB is not covered by ECC, there is no pair of |
| 881 | * ECC-based/raw functions for reading or or writing the OOB. The fact that the |
| 882 | * caller wants an ECC-based or raw view of the page is not propagated down to |
| 883 | * this driver. |
| 884 | * |
| 885 | * Since our OOB *is* covered by ECC, we need this information. So, we hook the |
| 886 | * ecc.read_oob and ecc.write_oob function pointers in the owning |
| 887 | * struct mtd_info with our own functions. These hook functions set the |
| 888 | * raw_oob_mode field so that, when control finally arrives here, we'll know |
| 889 | * what to do. |
| 890 | */ |
| 891 | 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] | 892 | int page) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 893 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 894 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 895 | |
| 896 | /* |
| 897 | * First, fill in the OOB buffer. If we're doing a raw read, we need to |
| 898 | * get the bytes from the physical page. If we're not doing a raw read, |
| 899 | * we need to fill the buffer with set bits. |
| 900 | */ |
| 901 | if (nand_info->raw_oob_mode) { |
| 902 | /* |
| 903 | * If control arrives here, we're doing a "raw" read. Send the |
| 904 | * command to read the conventional OOB and read it. |
| 905 | */ |
| 906 | nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page); |
| 907 | nand->read_buf(mtd, nand->oob_poi, mtd->oobsize); |
| 908 | } else { |
| 909 | /* |
| 910 | * If control arrives here, we're not doing a "raw" read. Fill |
| 911 | * the OOB buffer with set bits and correct the block mark. |
| 912 | */ |
| 913 | memset(nand->oob_poi, 0xff, mtd->oobsize); |
| 914 | |
| 915 | nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page); |
| 916 | mxs_nand_read_buf(mtd, nand->oob_poi, 1); |
| 917 | } |
| 918 | |
| 919 | return 0; |
| 920 | |
| 921 | } |
| 922 | |
| 923 | /* |
| 924 | * Write OOB data to NAND. |
| 925 | */ |
| 926 | static int mxs_nand_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *nand, |
| 927 | int page) |
| 928 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 929 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 930 | uint8_t block_mark = 0; |
| 931 | |
| 932 | /* |
| 933 | * There are fundamental incompatibilities between the i.MX GPMI NFC and |
| 934 | * the NAND Flash MTD model that make it essentially impossible to write |
| 935 | * the out-of-band bytes. |
| 936 | * |
| 937 | * We permit *ONE* exception. If the *intent* of writing the OOB is to |
| 938 | * mark a block bad, we can do that. |
| 939 | */ |
| 940 | |
| 941 | if (!nand_info->marking_block_bad) { |
| 942 | printf("NXS NAND: Writing OOB isn't supported\n"); |
| 943 | return -EIO; |
| 944 | } |
| 945 | |
| 946 | /* Write the block mark. */ |
| 947 | nand->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page); |
| 948 | nand->write_buf(mtd, &block_mark, 1); |
| 949 | nand->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 950 | |
| 951 | /* Check if it worked. */ |
| 952 | if (nand->waitfunc(mtd, nand) & NAND_STATUS_FAIL) |
| 953 | return -EIO; |
| 954 | |
| 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * Claims all blocks are good. |
| 960 | * |
| 961 | * In principle, this function is *only* called when the NAND Flash MTD system |
| 962 | * isn't allowed to keep an in-memory bad block table, so it is forced to ask |
| 963 | * the driver for bad block information. |
| 964 | * |
| 965 | * In fact, we permit the NAND Flash MTD system to have an in-memory BBT, so |
| 966 | * this function is *only* called when we take it away. |
| 967 | * |
| 968 | * Thus, this function is only called when we want *all* blocks to look good, |
| 969 | * so it *always* return success. |
| 970 | */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 971 | 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] | 972 | { |
| 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | /* |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 977 | * At this point, the physical NAND Flash chips have been identified and |
| 978 | * counted, so we know the physical geometry. This enables us to make some |
| 979 | * important configuration decisions. |
| 980 | * |
Robert P. J. Day | 8d56db9 | 2016-07-15 13:44:45 -0400 | [diff] [blame] | 981 | * 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] | 982 | * board_nand_init(). Anything other than zero will cause this driver to |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 983 | * tear everything down and declare failure. |
| 984 | */ |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 985 | int mxs_nand_setup_ecc(struct mtd_info *mtd) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 986 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 987 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 988 | struct mxs_nand_info *nand_info = nand_get_controller_data(nand); |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 989 | struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 990 | uint32_t tmp; |
| 991 | |
Peng Fan | c94f09d | 2015-07-21 16:15:19 +0800 | [diff] [blame] | 992 | if (mtd->oobsize > MXS_NAND_CHUNK_DATA_CHUNK_SIZE) { |
| 993 | galois_field = 14; |
| 994 | chunk_data_size = MXS_NAND_CHUNK_DATA_CHUNK_SIZE * 2; |
| 995 | } |
| 996 | |
| 997 | if (mtd->oobsize > chunk_data_size) { |
| 998 | printf("Not support the NAND chips whose oob size is larger then %d bytes!\n", chunk_data_size); |
| 999 | return -EINVAL; |
| 1000 | } |
| 1001 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1002 | /* Configure BCH and set NFC geometry */ |
Otavio Salvador | cbf0bf2 | 2012-08-13 09:53:12 +0000 | [diff] [blame] | 1003 | mxs_reset_block(&bch_regs->hw_bch_ctrl_reg); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1004 | |
| 1005 | /* Configure layout 0 */ |
| 1006 | tmp = (mxs_nand_ecc_chunk_cnt(mtd->writesize) - 1) |
| 1007 | << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET; |
| 1008 | tmp |= MXS_NAND_METADATA_SIZE << BCH_FLASHLAYOUT0_META_SIZE_OFFSET; |
| 1009 | tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1) |
| 1010 | << BCH_FLASHLAYOUT0_ECC0_OFFSET; |
Peng Fan | c94f09d | 2015-07-21 16:15:19 +0800 | [diff] [blame] | 1011 | tmp |= chunk_data_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT; |
| 1012 | tmp |= (14 == galois_field ? 1 : 0) << |
| 1013 | BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1014 | writel(tmp, &bch_regs->hw_bch_flash0layout0); |
| 1015 | |
| 1016 | tmp = (mtd->writesize + mtd->oobsize) |
| 1017 | << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET; |
| 1018 | tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1) |
| 1019 | << BCH_FLASHLAYOUT1_ECCN_OFFSET; |
Peng Fan | c94f09d | 2015-07-21 16:15:19 +0800 | [diff] [blame] | 1020 | tmp |= chunk_data_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT; |
| 1021 | tmp |= (14 == galois_field ? 1 : 0) << |
| 1022 | BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1023 | writel(tmp, &bch_regs->hw_bch_flash0layout1); |
| 1024 | |
| 1025 | /* Set *all* chip selects to use layout 0 */ |
| 1026 | writel(0, &bch_regs->hw_bch_layoutselect); |
| 1027 | |
| 1028 | /* Enable BCH complete interrupt */ |
| 1029 | writel(BCH_CTRL_COMPLETE_IRQ_EN, &bch_regs->hw_bch_ctrl_set); |
| 1030 | |
| 1031 | /* Hook some operations at the MTD level. */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1032 | if (mtd->_read_oob != mxs_nand_hook_read_oob) { |
| 1033 | nand_info->hooked_read_oob = mtd->_read_oob; |
| 1034 | mtd->_read_oob = mxs_nand_hook_read_oob; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1037 | if (mtd->_write_oob != mxs_nand_hook_write_oob) { |
| 1038 | nand_info->hooked_write_oob = mtd->_write_oob; |
| 1039 | mtd->_write_oob = mxs_nand_hook_write_oob; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1042 | if (mtd->_block_markbad != mxs_nand_hook_block_markbad) { |
| 1043 | nand_info->hooked_block_markbad = mtd->_block_markbad; |
| 1044 | mtd->_block_markbad = mxs_nand_hook_block_markbad; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1047 | return 0; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | /* |
| 1051 | * Allocate DMA buffers |
| 1052 | */ |
| 1053 | int mxs_nand_alloc_buffers(struct mxs_nand_info *nand_info) |
| 1054 | { |
| 1055 | uint8_t *buf; |
| 1056 | const int size = NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE; |
| 1057 | |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 1058 | nand_info->data_buf_size = roundup(size, MXS_DMA_ALIGNMENT); |
| 1059 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1060 | /* DMA buffers */ |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 1061 | buf = memalign(MXS_DMA_ALIGNMENT, nand_info->data_buf_size); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1062 | if (!buf) { |
| 1063 | printf("MXS NAND: Error allocating DMA buffers\n"); |
| 1064 | return -ENOMEM; |
| 1065 | } |
| 1066 | |
Marek Vasut | 1b120e8 | 2012-03-15 18:33:19 +0000 | [diff] [blame] | 1067 | memset(buf, 0, nand_info->data_buf_size); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1068 | |
| 1069 | nand_info->data_buf = buf; |
| 1070 | nand_info->oob_buf = buf + NAND_MAX_PAGESIZE; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1071 | /* Command buffers */ |
| 1072 | nand_info->cmd_buf = memalign(MXS_DMA_ALIGNMENT, |
| 1073 | MXS_NAND_COMMAND_BUFFER_SIZE); |
| 1074 | if (!nand_info->cmd_buf) { |
| 1075 | free(buf); |
| 1076 | printf("MXS NAND: Error allocating command buffers\n"); |
| 1077 | return -ENOMEM; |
| 1078 | } |
| 1079 | memset(nand_info->cmd_buf, 0, MXS_NAND_COMMAND_BUFFER_SIZE); |
| 1080 | nand_info->cmd_queue_len = 0; |
| 1081 | |
| 1082 | return 0; |
| 1083 | } |
| 1084 | |
| 1085 | /* |
| 1086 | * Initializes the NFC hardware. |
| 1087 | */ |
| 1088 | int mxs_nand_init(struct mxs_nand_info *info) |
| 1089 | { |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 1090 | struct mxs_gpmi_regs *gpmi_regs = |
| 1091 | (struct mxs_gpmi_regs *)MXS_GPMI_BASE; |
Wolfram Sang | 4c0f063 | 2012-12-05 10:48:47 +0000 | [diff] [blame] | 1092 | struct mxs_bch_regs *bch_regs = |
| 1093 | (struct mxs_bch_regs *)MXS_BCH_BASE; |
Peng Fan | e37d5a9 | 2016-01-27 10:38:02 +0800 | [diff] [blame] | 1094 | int i = 0, j, ret = 0; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1095 | |
| 1096 | info->desc = malloc(sizeof(struct mxs_dma_desc *) * |
| 1097 | MXS_NAND_DMA_DESCRIPTOR_COUNT); |
Peng Fan | e37d5a9 | 2016-01-27 10:38:02 +0800 | [diff] [blame] | 1098 | if (!info->desc) { |
| 1099 | ret = -ENOMEM; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1100 | goto err1; |
Peng Fan | e37d5a9 | 2016-01-27 10:38:02 +0800 | [diff] [blame] | 1101 | } |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1102 | |
| 1103 | /* Allocate the DMA descriptors. */ |
| 1104 | for (i = 0; i < MXS_NAND_DMA_DESCRIPTOR_COUNT; i++) { |
| 1105 | info->desc[i] = mxs_dma_desc_alloc(); |
Peng Fan | e37d5a9 | 2016-01-27 10:38:02 +0800 | [diff] [blame] | 1106 | if (!info->desc[i]) { |
| 1107 | ret = -ENOMEM; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1108 | goto err2; |
Peng Fan | e37d5a9 | 2016-01-27 10:38:02 +0800 | [diff] [blame] | 1109 | } |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | /* Init the DMA controller. */ |
Fabio Estevam | 1715622 | 2017-06-29 09:33:44 -0300 | [diff] [blame] | 1113 | mxs_dma_init(); |
Marek Vasut | 93541b4 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 1114 | for (j = MXS_DMA_CHANNEL_AHB_APBH_GPMI0; |
| 1115 | j <= MXS_DMA_CHANNEL_AHB_APBH_GPMI7; j++) { |
Peng Fan | e37d5a9 | 2016-01-27 10:38:02 +0800 | [diff] [blame] | 1116 | ret = mxs_dma_init_channel(j); |
| 1117 | if (ret) |
Marek Vasut | 93541b4 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 1118 | goto err3; |
| 1119 | } |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1120 | |
| 1121 | /* Reset the GPMI block. */ |
Otavio Salvador | cbf0bf2 | 2012-08-13 09:53:12 +0000 | [diff] [blame] | 1122 | mxs_reset_block(&gpmi_regs->hw_gpmi_ctrl0_reg); |
Wolfram Sang | 4c0f063 | 2012-12-05 10:48:47 +0000 | [diff] [blame] | 1123 | mxs_reset_block(&bch_regs->hw_bch_ctrl_reg); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1124 | |
| 1125 | /* |
| 1126 | * Choose NAND mode, set IRQ polarity, disable write protection and |
| 1127 | * select BCH ECC. |
| 1128 | */ |
| 1129 | clrsetbits_le32(&gpmi_regs->hw_gpmi_ctrl1, |
| 1130 | GPMI_CTRL1_GPMI_MODE, |
| 1131 | GPMI_CTRL1_ATA_IRQRDY_POLARITY | GPMI_CTRL1_DEV_RESET | |
| 1132 | GPMI_CTRL1_BCH_MODE); |
| 1133 | |
| 1134 | return 0; |
| 1135 | |
Marek Vasut | 93541b4 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 1136 | err3: |
Peng Fan | e37d5a9 | 2016-01-27 10:38:02 +0800 | [diff] [blame] | 1137 | for (--j; j >= MXS_DMA_CHANNEL_AHB_APBH_GPMI0; j--) |
Marek Vasut | 93541b4 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 1138 | mxs_dma_release(j); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1139 | err2: |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1140 | for (--i; i >= 0; i--) |
| 1141 | mxs_dma_desc_free(info->desc[i]); |
Peng Fan | e37d5a9 | 2016-01-27 10:38:02 +0800 | [diff] [blame] | 1142 | free(info->desc); |
| 1143 | err1: |
| 1144 | if (ret == -ENOMEM) |
| 1145 | printf("MXS NAND: Unable to allocate DMA descriptors\n"); |
| 1146 | return ret; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Stefan Agner | 7152f34 | 2018-06-22 17:19:46 +0200 | [diff] [blame] | 1149 | int mxs_nand_init_spl(struct nand_chip *nand) |
| 1150 | { |
| 1151 | struct mxs_nand_info *nand_info; |
| 1152 | int err; |
| 1153 | |
| 1154 | nand_info = malloc(sizeof(struct mxs_nand_info)); |
| 1155 | if (!nand_info) { |
| 1156 | printf("MXS NAND: Failed to allocate private data\n"); |
| 1157 | return -ENOMEM; |
| 1158 | } |
| 1159 | memset(nand_info, 0, sizeof(struct mxs_nand_info)); |
| 1160 | |
| 1161 | err = mxs_nand_alloc_buffers(nand_info); |
| 1162 | if (err) |
| 1163 | return err; |
| 1164 | |
| 1165 | err = mxs_nand_init(nand_info); |
| 1166 | if (err) |
| 1167 | return err; |
| 1168 | |
| 1169 | nand_set_controller_data(nand, nand_info); |
| 1170 | |
| 1171 | nand->options |= NAND_NO_SUBPAGE_WRITE; |
| 1172 | |
| 1173 | nand->cmd_ctrl = mxs_nand_cmd_ctrl; |
| 1174 | nand->dev_ready = mxs_nand_device_ready; |
| 1175 | nand->select_chip = mxs_nand_select_chip; |
Stefan Agner | 7152f34 | 2018-06-22 17:19:46 +0200 | [diff] [blame] | 1176 | |
| 1177 | nand->read_byte = mxs_nand_read_byte; |
| 1178 | nand->read_buf = mxs_nand_read_buf; |
| 1179 | |
| 1180 | nand->ecc.read_page = mxs_nand_ecc_read_page; |
| 1181 | |
| 1182 | nand->ecc.mode = NAND_ECC_HW; |
| 1183 | nand->ecc.bytes = 9; |
| 1184 | nand->ecc.size = 512; |
| 1185 | nand->ecc.strength = 8; |
| 1186 | |
| 1187 | return 0; |
| 1188 | } |
| 1189 | |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1190 | void board_nand_init(void) |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1191 | { |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1192 | struct mtd_info *mtd; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1193 | struct mxs_nand_info *nand_info; |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1194 | struct nand_chip *nand; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1195 | int err; |
| 1196 | |
| 1197 | nand_info = malloc(sizeof(struct mxs_nand_info)); |
| 1198 | if (!nand_info) { |
| 1199 | printf("MXS NAND: Failed to allocate private data\n"); |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1200 | return; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1201 | } |
| 1202 | memset(nand_info, 0, sizeof(struct mxs_nand_info)); |
| 1203 | |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1204 | nand = &nand_info->chip; |
| 1205 | mtd = nand_to_mtd(nand); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1206 | err = mxs_nand_alloc_buffers(nand_info); |
| 1207 | if (err) |
| 1208 | goto err1; |
| 1209 | |
| 1210 | err = mxs_nand_init(nand_info); |
| 1211 | if (err) |
| 1212 | goto err2; |
| 1213 | |
| 1214 | memset(&fake_ecc_layout, 0, sizeof(fake_ecc_layout)); |
| 1215 | |
Stefan Agner | 95f376f | 2018-06-22 17:19:48 +0200 | [diff] [blame^] | 1216 | #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT |
| 1217 | nand->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB; |
| 1218 | #endif |
| 1219 | |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1220 | nand_set_controller_data(nand, nand_info); |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1221 | nand->options |= NAND_NO_SUBPAGE_WRITE; |
| 1222 | |
| 1223 | nand->cmd_ctrl = mxs_nand_cmd_ctrl; |
| 1224 | |
| 1225 | nand->dev_ready = mxs_nand_device_ready; |
| 1226 | nand->select_chip = mxs_nand_select_chip; |
| 1227 | nand->block_bad = mxs_nand_block_bad; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1228 | |
| 1229 | nand->read_byte = mxs_nand_read_byte; |
| 1230 | |
| 1231 | nand->read_buf = mxs_nand_read_buf; |
| 1232 | nand->write_buf = mxs_nand_write_buf; |
| 1233 | |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1234 | /* first scan to find the device and get the page size */ |
| 1235 | if (nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_DEVICE, NULL)) |
| 1236 | goto err2; |
| 1237 | |
| 1238 | if (mxs_nand_setup_ecc(mtd)) |
| 1239 | goto err2; |
| 1240 | |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1241 | nand->ecc.read_page = mxs_nand_ecc_read_page; |
| 1242 | nand->ecc.write_page = mxs_nand_ecc_write_page; |
| 1243 | nand->ecc.read_oob = mxs_nand_ecc_read_oob; |
| 1244 | nand->ecc.write_oob = mxs_nand_ecc_write_oob; |
| 1245 | |
| 1246 | nand->ecc.layout = &fake_ecc_layout; |
| 1247 | nand->ecc.mode = NAND_ECC_HW; |
| 1248 | nand->ecc.bytes = 9; |
| 1249 | nand->ecc.size = 512; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1250 | nand->ecc.strength = 8; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1251 | |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1252 | /* second phase scan */ |
| 1253 | err = nand_scan_tail(mtd); |
| 1254 | if (err) |
| 1255 | goto err2; |
| 1256 | |
| 1257 | err = nand_register(0, mtd); |
| 1258 | if (err) |
| 1259 | goto err2; |
| 1260 | |
| 1261 | return; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1262 | |
| 1263 | err2: |
| 1264 | free(nand_info->data_buf); |
| 1265 | free(nand_info->cmd_buf); |
| 1266 | err1: |
| 1267 | free(nand_info); |
Stefan Agner | 5883e55 | 2018-06-22 17:19:47 +0200 | [diff] [blame] | 1268 | return; |
Marek Vasut | 913a725 | 2011-11-08 23:18:16 +0000 | [diff] [blame] | 1269 | } |