spl: nand: sunxi: make the reset column helper more generic
[oweals/u-boot.git] / drivers / mtd / nand / sunxi_nand_spl.c
index 06695fc15feef5502a32a6051010eb7004221a61..9ebdcee3513e2753449d502720b8f609febd24a4 100644 (file)
@@ -55,7 +55,7 @@
 
 
 #define NFC_ADDR_NUM_OFFSET        16
-#define NFC_SEND_AD              (1 << 19)
+#define NFC_SEND_ADDR              (1 << 19)
 #define NFC_ACCESS_DIR             (1 << 20)
 #define NFC_DATA_TRANS             (1 << 21)
 #define NFC_SEND_CMD1              (1 << 22)
@@ -155,6 +155,42 @@ static inline int check_value_negated(int offset, int unexpected_bits,
        return check_value_inner(offset, unexpected_bits, timeout_us, 1);
 }
 
+static int nand_wait_cmd_fifo_empty(void)
+{
+       if (!check_value_negated(SUNXI_NFC_BASE + NFC_ST, NFC_ST_CMD_FIFO_STAT,
+                                DEFAULT_TIMEOUT_US)) {
+               printf("nand: timeout waiting for empty cmd FIFO\n");
+               return -ETIMEDOUT;
+       }
+
+       return 0;
+}
+
+static int nand_wait_int(void)
+{
+       if (!check_value(SUNXI_NFC_BASE + NFC_ST, NFC_ST_CMD_INT_FLAG,
+                        DEFAULT_TIMEOUT_US)) {
+               printf("nand: timeout waiting for interruption\n");
+               return -ETIMEDOUT;
+       }
+
+       return 0;
+}
+
+static int nand_exec_cmd(u32 cmd)
+{
+       int ret;
+
+       ret = nand_wait_cmd_fifo_empty();
+       if (ret)
+               return ret;
+
+       writel(NFC_ST_CMD_INT_FLAG, SUNXI_NFC_BASE + NFC_ST);
+       writel(cmd, SUNXI_NFC_BASE + NFC_CMD);
+
+       return nand_wait_int();
+}
+
 void nand_init(void)
 {
        uint32_t val;
@@ -172,22 +208,15 @@ void nand_init(void)
        }
 
        /* reset NAND */
-       writel(NFC_ST_CMD_INT_FLAG, SUNXI_NFC_BASE + NFC_ST);
-       writel(NFC_SEND_CMD1 | NFC_WAIT_FLAG | NAND_CMD_RESET,
-              SUNXI_NFC_BASE + NFC_CMD);
-
-       if (!check_value(SUNXI_NFC_BASE + NFC_ST, NFC_ST_CMD_INT_FLAG,
-                        DEFAULT_TIMEOUT_US)) {
-               printf("Error timeout waiting for nand reset\n");
-               return;
-       }
-       writel(NFC_ST_CMD_INT_FLAG, SUNXI_NFC_BASE + NFC_ST);
+       nand_exec_cmd(NFC_SEND_CMD1 | NFC_WAIT_FLAG | NAND_CMD_RESET);
 }
 
 static void nand_apply_config(const struct nfc_config *conf)
 {
        u32 val;
 
+       nand_wait_cmd_fifo_empty();
+
        val = readl(SUNXI_NFC_BASE + NFC_CTL);
        val &= ~NFC_CTL_PAGE_SIZE_MASK;
        writel(val | NFC_CTL_RAM_METHOD | NFC_CTL_PAGE_SIZE(conf->page_size),
@@ -206,36 +235,30 @@ static int nand_load_page(const struct nfc_config *conf, u32 offs)
               SUNXI_NFC_BASE + NFC_RCMD_SET);
        writel(((page & 0xFFFF) << 16), SUNXI_NFC_BASE + NFC_ADDR_LOW);
        writel((page >> 16) & 0xFF, SUNXI_NFC_BASE + NFC_ADDR_HIGH);
-       writel(NFC_ST_CMD_INT_FLAG, SUNXI_NFC_BASE + NFC_ST);
-       writel(NFC_SEND_CMD1 | NFC_SEND_CMD2 | NFC_RAW_CMD | NFC_WAIT_FLAG |
-              ((conf->addr_cycles - 1) << NFC_ADDR_NUM_OFFSET) | NFC_SEND_ADR,
-              SUNXI_NFC_BASE + NFC_CMD);
-
-       if (!check_value(SUNXI_NFC_BASE + NFC_ST, NFC_ST_CMD_INT_FLAG,
-                        DEFAULT_TIMEOUT_US)) {
-               printf("Error while initializing dma interrupt\n");
-               return -EIO;
-       }
 
-       return 0;
+       return nand_exec_cmd(NFC_SEND_CMD1 | NFC_SEND_CMD2 | NFC_RAW_CMD |
+                            NFC_SEND_ADDR | NFC_WAIT_FLAG |
+                            ((conf->addr_cycles - 1) << NFC_ADDR_NUM_OFFSET));
 }
 
-static int nand_reset_column(void)
+static int nand_change_column(u16 column)
 {
+       int ret;
+
        writel((NFC_CMD_RNDOUTSTART << NFC_RANDOM_READ_CMD1_OFFSET) |
               (NFC_CMD_RNDOUT << NFC_RANDOM_READ_CMD0_OFFSET) |
               (NFC_CMD_RNDOUTSTART << NFC_READ_CMD_OFFSET),
               SUNXI_NFC_BASE + NFC_RCMD_SET);
-       writel(0, SUNXI_NFC_BASE + NFC_ADDR_LOW);
-       writel(NFC_SEND_CMD1 | NFC_SEND_CMD2 | NFC_RAW_CMD |
-              (1 << NFC_ADDR_NUM_OFFSET) | NFC_SEND_ADR | NFC_CMD_RNDOUT,
-              SUNXI_NFC_BASE + NFC_CMD);
+       writel(column, SUNXI_NFC_BASE + NFC_ADDR_LOW);
 
-       if (!check_value(SUNXI_NFC_BASE + NFC_ST, NFC_ST_CMD_INT_FLAG,
-                        DEFAULT_TIMEOUT_US)) {
-               printf("Error while initializing dma interrupt\n");
-               return -1;
-       }
+       ret = nand_exec_cmd(NFC_SEND_CMD1 | NFC_SEND_CMD2 | NFC_RAW_CMD |
+                           (1 << NFC_ADDR_NUM_OFFSET) | NFC_SEND_ADDR |
+                           NFC_CMD_RNDOUT);
+       if (ret)
+               return ret;
+
+       /* Ensure tCCS has passed before reading data */
+       udelay(1);
 
        return 0;
 }
@@ -393,7 +416,7 @@ static int nand_detect_ecc_config(struct nfc_config *conf, u32 offs,
                     conf->ecc_strength >= 0;
                     conf->ecc_strength--) {
                        conf->randomize = false;
-                       if (nand_reset_column())
+                       if (nand_change_column(0))
                                return -EIO;
 
                        /*
@@ -413,7 +436,7 @@ static int nand_detect_ecc_config(struct nfc_config *conf, u32 offs,
                        conf->randomize = true;
                        conf->nseeds = ARRAY_SIZE(random_seed);
                        do {
-                               if (nand_reset_column())
+                               if (nand_change_column(0))
                                        return -EIO;
 
                                if (!nand_read_page(conf, offs, dest,
@@ -505,7 +528,7 @@ static int nand_read_buffer(struct nfc_config *conf, uint32_t offs,
                        /* Try to adjust ->nseeds and read the page again... */
                        conf->nseeds = cur_seed;
 
-                       if (nand_reset_column())
+                       if (nand_change_column(0))
                                return -EIO;
 
                        /* ... it still fails => it's a real corruption. */