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