]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/mtd/nand/mxs_nand.c
nand: mxs: make fake_ecc_layout static
[karo-tx-uboot.git] / drivers / mtd / nand / mxs_nand.c
index 37ed099641194b2ed8ff072df1fdfbab38251a29..480b4a514c5aef4ef4a8616773f24b898c2b5dec 100644 (file)
  * Copyright (C) 2010 Freescale Semiconductor, Inc.
  * Copyright (C) 2008 Embedded Alley Solutions, Inc.
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 //#define DEBUG
 
 #include <asm/io.h>
 #include <asm/arch/clock.h>
 #include <asm/arch/imx-regs.h>
-#include <asm/arch/regs-bch.h>
-#include <asm/arch/regs-gpmi.h>
+#include <asm/imx-common/regs-bch.h>
+#include <asm/imx-common/regs-gpmi.h>
 #include <asm/arch/sys_proto.h>
-#include <asm/arch/dma.h>
+#include <asm/imx-common/dma.h>
 
 #define        MXS_NAND_DMA_DESCRIPTOR_COUNT           4
 
-#ifndef CONFIG_MX6Q
 #define        MXS_NAND_CHUNK_DATA_CHUNK_SIZE          512
+#if defined(CONFIG_ARCH_MX6)
+#define        MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT    2
 #else
-#define        MXS_NAND_CHUNK_DATA_CHUNK_SIZE          (512 / 4)
+#define        MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT    0
 #endif
-
 #define        MXS_NAND_METADATA_SIZE                  10
-
+#define        MXS_NAND_BITS_PER_ECC_LEVEL             13
 #define        MXS_NAND_COMMAND_BUFFER_SIZE            32
 
 /* BCH timeout in microseconds */
@@ -186,7 +174,9 @@ static inline void dump_regs(void)
 }
 #endif
 
-struct nand_ecclayout fake_ecc_layout;
+static struct nand_ecclayout fake_ecc_layout;
+static int chunk_data_size = MXS_NAND_CHUNK_DATA_CHUNK_SIZE;
+static int galois_field = 13;
 
 /*
  * Cache management functions
@@ -247,15 +237,14 @@ static void mxs_nand_return_dma_descs(struct mxs_nand_info *info)
        info->desc_index = 0;
 }
 
-static uint32_t mxs_nand_ecc_chunk_cnt(struct mtd_info *mtd)
+static uint32_t mxs_nand_ecc_chunk_cnt(uint32_t page_data_size)
 {
-       struct nand_chip *nand = mtd->priv;
-       return mtd->writesize / nand->ecc.size;
+       return page_data_size / chunk_data_size;
 }
 
 static uint32_t mxs_nand_ecc_size_in_bits(uint32_t ecc_strength)
 {
-       return ecc_strength * 13;
+       return ecc_strength * galois_field;
 }
 
 static uint32_t mxs_nand_aux_status_offset(void)
@@ -269,8 +258,16 @@ static int mxs_nand_gpmi_init(void)
 
        /* Reset the GPMI block. */
        ret = mxs_reset_block(&gpmi_regs->hw_gpmi_ctrl0_reg);
-       if (ret)
+       if (ret) {
+               printf("Failed to reset GPMI block\n");
                return ret;
+       }
+
+       ret = mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
+       if (ret) {
+               printf("Failed to reset BCH block\n");
+               return ret;
+       }
 
        /*
         * Choose NAND mode, set IRQ polarity, disable write protection and
@@ -287,18 +284,21 @@ static int mxs_nand_gpmi_init(void)
 static inline uint32_t mxs_nand_get_ecc_strength(uint32_t page_data_size,
                                                uint32_t page_oob_size)
 {
-       if (page_data_size == 2048)
-               return 8;
-
-       if (page_data_size == 4096) {
-               if (page_oob_size == 128)
-                       return 8;
+       int ecc_strength;
 
-               if (page_oob_size == 218)
-                       return 16;
-       }
+       /*
+        * Determine the ECC layout with the formula:
+        *      ECC bits per chunk = (total page spare data bits) /
+        *              (bits per ECC level) / (chunks per page)
+        * where:
+        *      total page spare data bits =
+        *              (page oob size - meta data size) * (bits per byte)
+        */
+       ecc_strength = ((page_oob_size - MXS_NAND_METADATA_SIZE) * 8)
+                       / (galois_field *
+                          mxs_nand_ecc_chunk_cnt(page_data_size));
 
-       return 0;
+       return round_down(ecc_strength, 2);
 }
 
 static inline uint32_t mxs_nand_get_mark_offset(uint32_t page_data_size,
@@ -311,7 +311,7 @@ static inline uint32_t mxs_nand_get_mark_offset(uint32_t page_data_size,
        uint32_t block_mark_chunk_bit_offset;
        uint32_t block_mark_bit_offset;
 
-       chunk_data_size_in_bits = MXS_NAND_CHUNK_DATA_CHUNK_SIZE * 8;
+       chunk_data_size_in_bits = chunk_data_size * 8;
        chunk_ecc_size_in_bits  = mxs_nand_ecc_size_in_bits(ecc_strength);
 
        chunk_total_size_in_bits =
@@ -350,14 +350,14 @@ static inline uint32_t mxs_nand_get_mark_offset(uint32_t page_data_size,
        return block_mark_bit_offset;
 }
 
-static uint32_t mxs_nand_mark_byte_offset(struct mtd_info *mtd)
+static inline uint32_t mxs_nand_mark_byte_offset(struct mtd_info *mtd)
 {
        uint32_t ecc_strength;
        ecc_strength = mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize);
        return mxs_nand_get_mark_offset(mtd->writesize, ecc_strength) >> 3;
 }
 
-static uint32_t mxs_nand_mark_bit_offset(struct mtd_info *mtd)
+static inline uint32_t mxs_nand_mark_bit_offset(struct mtd_info *mtd)
 {
        uint32_t ecc_strength;
        ecc_strength = mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize);
@@ -510,6 +510,7 @@ static void mxs_nand_select_chip(struct mtd_info *mtd, int chip)
  * swapping the block mark, or swapping it *back* -- but it doesn't matter
  * because the the operation is the same.
  */
+#ifndef CONFIG_NAND_MXS_NO_BBM_SWAP
 static void mxs_nand_swap_block_mark(struct mtd_info *mtd,
                                        uint8_t *data_buf, uint8_t *oob_buf)
 {
@@ -518,7 +519,7 @@ static void mxs_nand_swap_block_mark(struct mtd_info *mtd,
 
        uint32_t src;
        uint32_t dst;
-return;
+
        bit_offset = mxs_nand_mark_bit_offset(mtd);
        buf_offset = mxs_nand_mark_byte_offset(mtd);
 
@@ -544,6 +545,12 @@ return;
        data_buf[buf_offset] |= dst << bit_offset;
        data_buf[buf_offset + 1] |= dst >> (8 - bit_offset);
 }
+#else
+static inline void mxs_nand_swap_block_mark(struct mtd_info *mtd,
+                                       uint8_t *data_buf, uint8_t *oob_buf)
+{
+}
+#endif
 
 /*
  * Read data from NAND.
@@ -566,8 +573,6 @@ static void mxs_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int length)
                return;
        }
 
-       memset(buf, 0xee, length);
-
        /* Compile the DMA descriptor - a descriptor that reads data. */
        d = mxs_nand_get_dma_desc(nand_info);
        d->cmd.data =
@@ -586,7 +591,8 @@ static void mxs_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int length)
                length;
 
        mxs_dma_desc_append(channel, d);
-#ifndef CONFIG_MX6Q
+
+#ifndef CONFIG_ARCH_MX6
        /*
         * A DMA descriptor that waits for the command to end and the chip to
         * become ready.
@@ -599,7 +605,7 @@ static void mxs_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int length)
        d->cmd.data =
                MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
                MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_DEC_SEM |
-               MXS_DMA_DESC_WAIT4END | (4 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
+               MXS_DMA_DESC_WAIT4END | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
 
        d->cmd.address = 0;
 
@@ -611,6 +617,10 @@ static void mxs_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int length)
 
        mxs_dma_desc_append(channel, d);
 #endif
+
+       /* Invalidate caches */
+       mxs_nand_inval_data_buf(nand_info);
+
        /* Execute the DMA chain. */
        ret = mxs_dma_go(channel);
        if (ret) {
@@ -656,7 +666,7 @@ static void mxs_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
        d->cmd.data =
                MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
                MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
-               (4 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
+               (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
                (length << MXS_DMA_DESC_BYTES_OFFSET);
 
        d->cmd.address = (dma_addr_t)nand_info->data_buf;
@@ -705,7 +715,8 @@ static void flush_buffers(struct mtd_info *mtd, struct mxs_nand_info *nand_info)
  * Read a page from NAND.
  */
 static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
-                                       uint8_t *buf, int page)
+                                       uint8_t *buf, int oob_required,
+                                       int page)
 {
        struct mxs_nand_info *nand_info = nand->priv;
        struct mxs_dma_desc *d;
@@ -788,6 +799,9 @@ static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
 
        mxs_dma_desc_append(channel, d);
 
+       /* Invalidate caches */
+       mxs_nand_inval_data_buf(nand_info);
+
        /* Execute the DMA chain. */
        ret = mxs_dma_go(channel);
        if (ret) {
@@ -809,7 +823,7 @@ static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
 
        /* Loop over status bytes, accumulating ECC status. */
        status = nand_info->oob_buf + mxs_nand_aux_status_offset();
-       for (i = 0; i < mxs_nand_ecc_chunk_cnt(mtd); i++) {
+       for (i = 0; i < mxs_nand_ecc_chunk_cnt(mtd->writesize); i++) {
                if (status[i] == 0x00)
                        continue;
 
@@ -852,8 +866,9 @@ rtn:
 /*
  * Write a page to NAND.
  */
-static void mxs_nand_ecc_write_page(struct mtd_info *mtd,
-                               struct nand_chip *nand, const uint8_t *buf)
+static int mxs_nand_ecc_write_page(struct mtd_info *mtd,
+                               struct nand_chip *nand, const uint8_t *buf,
+                               int oob_required)
 {
        struct mxs_nand_info *nand_info = nand->priv;
        struct mxs_dma_desc *d;
@@ -911,6 +926,7 @@ static void mxs_nand_ecc_write_page(struct mtd_info *mtd,
 
 rtn:
        mxs_nand_return_dma_descs(nand_info);
+       return 0;
 }
 
 /*
@@ -926,7 +942,7 @@ static int mxs_nand_hook_read_oob(struct mtd_info *mtd, loff_t from,
        struct mxs_nand_info *nand_info = chip->priv;
        int ret;
 
-       if (ops->mode == MTD_OOB_RAW)
+       if (ops->mode == MTD_OPS_RAW)
                nand_info->raw_oob_mode = 1;
        else
                nand_info->raw_oob_mode = 0;
@@ -951,7 +967,7 @@ static int mxs_nand_hook_write_oob(struct mtd_info *mtd, loff_t to,
        struct mxs_nand_info *nand_info = chip->priv;
        int ret;
 
-       if (ops->mode == MTD_OOB_RAW)
+       if (ops->mode == MTD_OPS_RAW)
                nand_info->raw_oob_mode = 1;
        else
                nand_info->raw_oob_mode = 0;
@@ -1029,7 +1045,7 @@ static int mxs_nand_hook_block_markbad(struct mtd_info *mtd, loff_t ofs)
  * what to do.
  */
 static int mxs_nand_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
-                               int page, int cmd)
+                               int page)
 {
        struct mxs_nand_info *nand_info = nand->priv;
 
@@ -1133,35 +1149,42 @@ static int mxs_nand_scan_bbt(struct mtd_info *mtd)
        struct mxs_nand_info *nand_info = nand->priv;
        uint32_t tmp;
 
+       if (mtd->oobsize > MXS_NAND_CHUNK_DATA_CHUNK_SIZE) {
+               galois_field = 14;
+               chunk_data_size = MXS_NAND_CHUNK_DATA_CHUNK_SIZE * 2;
+       }
+
+       if (mtd->oobsize > chunk_data_size) {
+               printf("OOB size of chip (%u bytes) is larger than max. supported size (%u bytes)\n",
+                       mtd->oobsize, chunk_data_size);
+               return -EINVAL;
+       }
+
        /* Configure BCH and set NFC geometry */
-       if (readl(&bch_regs->hw_bch_ctrl_reg) &
-               (BCH_CTRL_SFTRST | BCH_CTRL_CLKGATE))
-               /* When booting from NAND the BCH engine will already
-                * be operational and obviously does not like being reset here.
-                * There will be occasional read errors upon boot when this
-                * reset is done.
-                */
-               mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
-       readl(&bch_regs->hw_bch_ctrl_reg);
+       mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
 
        debug("mtd->writesize=%d\n", mtd->writesize);
        debug("mtd->oobsize=%d\n", mtd->oobsize);
        debug("ecc_strength=%d\n", mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize));
 
        /* Configure layout 0 */
-       tmp = (mxs_nand_ecc_chunk_cnt(mtd) - 1)
+       tmp = (mxs_nand_ecc_chunk_cnt(mtd->writesize) - 1)
                << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
        tmp |= MXS_NAND_METADATA_SIZE << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
        tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1)
                << BCH_FLASHLAYOUT0_ECC0_OFFSET;
-       tmp |= MXS_NAND_CHUNK_DATA_CHUNK_SIZE;
+       tmp |= chunk_data_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
+       tmp |= (14 == galois_field ? 1 : 0) <<
+               BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET;
        writel(tmp, &bch_regs->hw_bch_flash0layout0);
 
        tmp = (mtd->writesize + mtd->oobsize)
                << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
        tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1)
                << BCH_FLASHLAYOUT1_ECCN_OFFSET;
-       tmp |= MXS_NAND_CHUNK_DATA_CHUNK_SIZE;
+       tmp |= chunk_data_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
+       tmp |= (14 == galois_field ? 1 : 0) <<
+               BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
        writel(tmp, &bch_regs->hw_bch_flash0layout1);
 
        /* Set *all* chip selects to use layout 0 */
@@ -1171,19 +1194,19 @@ static int mxs_nand_scan_bbt(struct mtd_info *mtd)
        writel(BCH_CTRL_COMPLETE_IRQ_EN, &bch_regs->hw_bch_ctrl_set);
 
        /* Hook some operations at the MTD level. */
-       if (mtd->read_oob != mxs_nand_hook_read_oob) {
-               nand_info->hooked_read_oob = mtd->read_oob;
-               mtd->read_oob = mxs_nand_hook_read_oob;
+       if (mtd->_read_oob != mxs_nand_hook_read_oob) {
+               nand_info->hooked_read_oob = mtd->_read_oob;
+               mtd->_read_oob = mxs_nand_hook_read_oob;
        }
 
-       if (mtd->write_oob != mxs_nand_hook_write_oob) {
-               nand_info->hooked_write_oob = mtd->write_oob;
-               mtd->write_oob = mxs_nand_hook_write_oob;
+       if (mtd->_write_oob != mxs_nand_hook_write_oob) {
+               nand_info->hooked_write_oob = mtd->_write_oob;
+               mtd->_write_oob = mxs_nand_hook_write_oob;
        }
 
-       if (mtd->block_markbad != mxs_nand_hook_block_markbad) {
-               nand_info->hooked_block_markbad = mtd->block_markbad;
-               mtd->block_markbad = mxs_nand_hook_block_markbad;
+       if (mtd->_block_markbad != mxs_nand_hook_block_markbad) {
+               nand_info->hooked_block_markbad = mtd->_block_markbad;
+               mtd->_block_markbad = mxs_nand_hook_block_markbad;
        }
 
        /* We use the reference implementation for bad block management. */
@@ -1233,8 +1256,9 @@ int mxs_nand_init(struct mxs_nand_info *info)
        int ret;
        int i;
 
-       info->desc = malloc(sizeof(struct mxs_dma_desc *) *
-                               MXS_NAND_DMA_DESCRIPTOR_COUNT);
+       info->desc = calloc(MXS_NAND_DMA_DESCRIPTOR_COUNT,
+                       sizeof(struct mxs_dma_desc *));
+
        if (!info->desc) {
                printf("MXS NAND: Unable to allocate DMA descriptor table\n");
                ret = -ENOMEM;
@@ -1289,19 +1313,18 @@ err1:
  *                information that is used by the suspend, resume and
  *                remove functions
  *
- * @return  The function always returns 0.
+ * @return  0 for success; errno value in case of error
  */
 int board_nand_init(struct nand_chip *nand)
 {
        struct mxs_nand_info *nand_info;
        int err;
 
-       nand_info = malloc(sizeof(struct mxs_nand_info));
+       nand_info = kzalloc(sizeof(struct mxs_nand_info), 0);
        if (!nand_info) {
                printf("MXS NAND: Failed to allocate private data\n");
                return -ENOMEM;
        }
-       memset(nand_info, 0, sizeof(struct mxs_nand_info));
 
        err = mxs_nand_alloc_buffers(nand_info);
        if (err)
@@ -1316,7 +1339,7 @@ int board_nand_init(struct nand_chip *nand)
        nand->priv = nand_info;
        nand->options |= NAND_NO_SUBPAGE_WRITE;
 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
-       nand->options |= NAND_USE_FLASH_BBT | NAND_USE_FLASH_BBT_NO_OOB;
+       nand->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
 #endif
        nand->cmd_ctrl          = mxs_nand_cmd_ctrl;
 
@@ -1339,6 +1362,7 @@ int board_nand_init(struct nand_chip *nand)
        nand->ecc.mode          = NAND_ECC_HW;
        nand->ecc.bytes         = 9;
        nand->ecc.size          = 512;
+       nand->ecc.strength      = 8;
 
        return 0;