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