cfi_flash: Fix logical continuations
[oweals/u-boot.git] / drivers / mtd / nand / atmel_nand.c
index 9c2cb4434403824dad1293c365e7812d26345def..65dd83eced5860ddbecf5a062867b2cc32a212c8 100644 (file)
@@ -24,9 +24,9 @@
 
 /* Register access macros */
 #define ecc_readl(add, reg)                            \
-       readl(AT91_BASE_SYS + add + ATMEL_ECC_##reg)
+       readl(add + ATMEL_ECC_##reg)
 #define ecc_writel(add, reg, value)                    \
-       writel((value), AT91_BASE_SYS + add + ATMEL_ECC_##reg)
+       writel((value), add + ATMEL_ECC_##reg)
 
 #include "atmel_nand_ecc.h"    /* Hardware ECC registers */
 
@@ -160,8 +160,8 @@ static int pmecc_data_alloc(struct atmel_nand_host *host)
 
 static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
 {
-       struct nand_chip *nand_chip = mtd->priv;
-       struct atmel_nand_host *host = nand_chip->priv;
+       struct nand_chip *nand_chip = mtd_to_nand(mtd);
+       struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
        int i;
        uint32_t value;
 
@@ -177,8 +177,8 @@ static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
 
 static void pmecc_substitute(struct mtd_info *mtd)
 {
-       struct nand_chip *nand_chip = mtd->priv;
-       struct atmel_nand_host *host = nand_chip->priv;
+       struct nand_chip *nand_chip = mtd_to_nand(mtd);
+       struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
        int16_t __iomem *alpha_to = host->pmecc_alpha_to;
        int16_t __iomem *index_of = host->pmecc_index_of;
        int16_t *partial_syn = host->pmecc_partial_syn;
@@ -227,8 +227,8 @@ static void pmecc_substitute(struct mtd_info *mtd)
  */
 static void pmecc_get_sigma(struct mtd_info *mtd)
 {
-       struct nand_chip *nand_chip = mtd->priv;
-       struct atmel_nand_host *host = nand_chip->priv;
+       struct nand_chip *nand_chip = mtd_to_nand(mtd);
+       struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
 
        int16_t *lmu = host->pmecc_lmu;
        int16_t *si = host->pmecc_si;
@@ -383,8 +383,8 @@ static void pmecc_get_sigma(struct mtd_info *mtd)
 
 static int pmecc_err_location(struct mtd_info *mtd)
 {
-       struct nand_chip *nand_chip = mtd->priv;
-       struct atmel_nand_host *host = nand_chip->priv;
+       struct nand_chip *nand_chip = mtd_to_nand(mtd);
+       struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
        const int cap = host->pmecc_corr_cap;
        const int num = 2 * cap + 1;
        int sector_size = host->pmecc_sector_size;
@@ -437,8 +437,8 @@ static int pmecc_err_location(struct mtd_info *mtd)
 static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
                int sector_num, int extra_bytes, int err_nbr)
 {
-       struct nand_chip *nand_chip = mtd->priv;
-       struct atmel_nand_host *host = nand_chip->priv;
+       struct nand_chip *nand_chip = mtd_to_nand(mtd);
+       struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
        int i = 0;
        int byte_pos, bit_pos, sector_size, pos;
        uint32_t tmp;
@@ -483,8 +483,8 @@ static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
 static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
        u8 *ecc)
 {
-       struct nand_chip *nand_chip = mtd->priv;
-       struct atmel_nand_host *host = nand_chip->priv;
+       struct nand_chip *nand_chip = mtd_to_nand(mtd);
+       struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
        int i, err_nbr, eccbytes;
        uint8_t *buf_pos;
 
@@ -513,7 +513,7 @@ normal_check:
                        if (err_nbr == -1) {
                                dev_err(host->dev, "PMECC: Too many errors\n");
                                mtd->ecc_stats.failed++;
-                               return -EIO;
+                               return -EBADMSG;
                        } else {
                                pmecc_correct_data(mtd, buf_pos, ecc, i,
                                        host->pmecc_bytes_per_sector, err_nbr);
@@ -529,7 +529,7 @@ normal_check:
 static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
        struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
 {
-       struct atmel_nand_host *host = chip->priv;
+       struct atmel_nand_host *host = nand_get_controller_data(chip);
        int eccsize = chip->ecc.size;
        uint8_t *oob = chip->oob_poi;
        uint32_t *eccpos = chip->ecc.layout->eccpos;
@@ -562,16 +562,16 @@ static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
        stat = pmecc_readl(host->pmecc, isr);
        if (stat != 0)
                if (pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]) != 0)
-                       return -EIO;
+                       return -EBADMSG;
 
        return 0;
 }
 
 static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
                struct nand_chip *chip, const uint8_t *buf,
-               int oob_required)
+               int oob_required, int page)
 {
-       struct atmel_nand_host *host = chip->priv;
+       struct atmel_nand_host *host = nand_get_controller_data(chip);
        uint32_t *eccpos = chip->ecc.layout->eccpos;
        int i, j;
        int timeout = PMECC_MAX_TIMEOUT_US;
@@ -615,8 +615,8 @@ out:
 
 static void atmel_pmecc_core_init(struct mtd_info *mtd)
 {
-       struct nand_chip *nand_chip = mtd->priv;
-       struct atmel_nand_host *host = nand_chip->priv;
+       struct nand_chip *nand_chip = mtd_to_nand(mtd);
+       struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
        uint32_t val = 0;
        struct nand_ecclayout *ecc_layout;
 
@@ -702,7 +702,7 @@ static int pmecc_choose_ecc(struct atmel_nand_host *host,
        if (chip->onfi_version) {
                *cap = chip->ecc_strength_ds;
                *sector_size = chip->ecc_step_ds;
-               MTDDEBUG(MTD_DEBUG_LEVEL1, "ONFI params, minimum required ECC: %d bits in %d bytes\n",
+               pr_debug("ONFI params, minimum required ECC: %d bits in %d bytes\n",
                         *cap, *sector_size);
        }
 
@@ -808,7 +808,8 @@ static int atmel_pmecc_nand_init_params(struct nand_chip *nand,
        struct atmel_nand_host *host;
        int cap, sector_size;
 
-       host = nand->priv = &pmecc_host;
+       host = &pmecc_host;
+       nand_set_controller_data(nand, host);
 
        nand->ecc.mode = NAND_ECC_HW;
        nand->ecc.calculate = NULL;
@@ -862,9 +863,8 @@ static int atmel_pmecc_nand_init_params(struct nand_chip *nand,
                host->pmecc_index_table_offset = ATMEL_PMECC_INDEX_OFFSET_1024;
 #endif
 
-       MTDDEBUG(MTD_DEBUG_LEVEL1,
-               "Initialize PMECC params, cap: %d, sector: %d\n",
-               cap, sector_size);
+       pr_debug("Initialize PMECC params, cap: %d, sector: %d\n",
+                cap, sector_size);
 
        host->pmecc = (struct pmecc_regs __iomem *) ATMEL_BASE_PMECC;
        host->pmerrloc = (struct pmecc_errloc_regs __iomem *)
@@ -1080,7 +1080,7 @@ static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
 static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
                u_char *read_ecc, u_char *isnull)
 {
-       struct nand_chip *nand_chip = mtd->priv;
+       struct nand_chip *nand_chip = mtd_to_nand(mtd);
        unsigned int ecc_status;
        unsigned int ecc_word, ecc_bit;
 
@@ -1111,7 +1111,7 @@ static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
                 * We can't correct so many errors */
                dev_warn(host->dev, "atmel_nand : multiple errors detected."
                                " Unable to correct.\n");
-               return -EIO;
+               return -EBADMSG;
        }
 
        /* if there's a single bit error : we can correct it */
@@ -1155,6 +1155,7 @@ int atmel_hwecc_nand_init_param(struct nand_chip *nand, struct mtd_info *mtd)
        nand->ecc.hwctl = atmel_nand_hwctl;
        nand->ecc.read_page = atmel_nand_read_page;
        nand->ecc.bytes = 4;
+       nand->ecc.strength = 4;
 
        if (nand->ecc.mode == NAND_ECC_HW) {
                /* ECC is calculated for the whole page (1 step) */
@@ -1207,7 +1208,7 @@ int atmel_hwecc_nand_init_param(struct nand_chip *nand, struct mtd_info *mtd)
 static void at91_nand_hwcontrol(struct mtd_info *mtd,
                                         int cmd, unsigned int ctrl)
 {
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *this = mtd_to_nand(mtd);
 
        if (ctrl & NAND_CTRL_CHANGE) {
                ulong IO_ADDR_W = (ulong) this->IO_ADDR_W;
@@ -1220,7 +1221,8 @@ static void at91_nand_hwcontrol(struct mtd_info *mtd,
                        IO_ADDR_W |= CONFIG_SYS_NAND_MASK_ALE;
 
 #ifdef CONFIG_SYS_NAND_ENABLE_PIN
-               gpio_set_value(CONFIG_SYS_NAND_ENABLE_PIN, !(ctrl & NAND_NCE));
+               at91_set_gpio_value(CONFIG_SYS_NAND_ENABLE_PIN,
+                                   !(ctrl & NAND_NCE));
 #endif
                this->IO_ADDR_W = (void *) IO_ADDR_W;
        }
@@ -1232,7 +1234,7 @@ static void at91_nand_hwcontrol(struct mtd_info *mtd,
 #ifdef CONFIG_SYS_NAND_READY_PIN
 static int at91_nand_ready(struct mtd_info *mtd)
 {
-       return gpio_get_value(CONFIG_SYS_NAND_READY_PIN);
+       return at91_get_gpio_value(CONFIG_SYS_NAND_READY_PIN);
 }
 #endif
 
@@ -1243,7 +1245,7 @@ static struct nand_chip nand_chip;
 
 static int nand_command(int block, int page, uint32_t offs, u8 cmd)
 {
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *this = mtd_to_nand(mtd);
        int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
        void (*hwctrl)(struct mtd_info *mtd, int cmd,
                        unsigned int ctrl) = this->cmd_ctrl;
@@ -1281,7 +1283,7 @@ static int nand_command(int block, int page, uint32_t offs, u8 cmd)
 
 static int nand_is_bad_block(int block)
 {
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *this = mtd_to_nand(mtd);
 
        nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS, NAND_CMD_READOOB);
 
@@ -1304,7 +1306,7 @@ static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
 
 static int nand_read_page(int block, int page, void *dst)
 {
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *this = mtd_to_nand(mtd);
        u_char ecc_calc[ECCTOTAL];
        u_char ecc_code[ECCTOTAL];
        u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
@@ -1337,7 +1339,7 @@ static int nand_read_page(int block, int page, void *dst)
 
 int spl_nand_erase_one(int block, int page)
 {
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *this = mtd_to_nand(mtd);
        void (*hwctrl)(struct mtd_info *mtd, int cmd,
                        unsigned int ctrl) = this->cmd_ctrl;
        int page_addr;
@@ -1368,7 +1370,7 @@ int spl_nand_erase_one(int block, int page)
 #else
 static int nand_read_page(int block, int page, void *dst)
 {
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *this = mtd_to_nand(mtd);
 
        nand_command(block, page, 0, NAND_CMD_READ0);
        atmel_nand_pmecc_read_page(mtd, this, dst, 0, page);
@@ -1377,37 +1379,9 @@ static int nand_read_page(int block, int page, void *dst)
 }
 #endif /* CONFIG_SPL_NAND_ECC */
 
-int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
-{
-       unsigned int block, lastblock;
-       unsigned int page;
-
-       block = offs / CONFIG_SYS_NAND_BLOCK_SIZE;
-       lastblock = (offs + size - 1) / CONFIG_SYS_NAND_BLOCK_SIZE;
-       page = (offs % CONFIG_SYS_NAND_BLOCK_SIZE) / CONFIG_SYS_NAND_PAGE_SIZE;
-
-       while (block <= lastblock) {
-               if (!nand_is_bad_block(block)) {
-                       while (page < CONFIG_SYS_NAND_PAGE_COUNT) {
-                               nand_read_page(block, page, dst);
-                               dst += CONFIG_SYS_NAND_PAGE_SIZE;
-                               page++;
-                       }
-
-                       page = 0;
-               } else {
-                       lastblock++;
-               }
-
-               block++;
-       }
-
-       return 0;
-}
-
 int at91_nand_wait_ready(struct mtd_info *mtd)
 {
-       struct nand_chip *this = mtd->priv;
+       struct nand_chip *this = mtd_to_nand(mtd);
 
        udelay(this->chip_delay);
 
@@ -1447,10 +1421,9 @@ int board_nand_init(struct nand_chip *nand)
 
 void nand_init(void)
 {
-       mtd = &nand_chip.mtd;
+       mtd = nand_to_mtd(&nand_chip);
        mtd->writesize = CONFIG_SYS_NAND_PAGE_SIZE;
        mtd->oobsize = CONFIG_SYS_NAND_OOBSIZE;
-       mtd->priv = &nand_chip;
        nand_chip.IO_ADDR_R = (void __iomem *)CONFIG_SYS_NAND_BASE;
        nand_chip.IO_ADDR_W = (void __iomem *)CONFIG_SYS_NAND_BASE;
        board_nand_init(&nand_chip);
@@ -1472,6 +1445,8 @@ void nand_deselect(void)
                nand_chip.select_chip(mtd, -1);
 }
 
+#include "nand_spl_loaders.c"
+
 #else
 
 #ifndef CONFIG_SYS_NAND_BASE_LIST
@@ -1484,9 +1459,8 @@ int atmel_nand_chip_init(int devnum, ulong base_addr)
 {
        int ret;
        struct nand_chip *nand = &nand_chip[devnum];
-       struct mtd_info *mtd = &nand->mtd;
+       struct mtd_info *mtd = nand_to_mtd(nand);
 
-       mtd->priv = nand;
        nand->IO_ADDR_R = nand->IO_ADDR_W = (void  __iomem *)base_addr;
 
 #ifdef CONFIG_NAND_ECC_BCH