Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 2 | /* |
Scott Wood | 454a426 | 2009-09-02 16:45:31 -0500 | [diff] [blame] | 3 | * Copyright 2004-2007 Freescale Semiconductor, Inc. |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 4 | * Copyright 2008 Sascha Hauer, kernel@pengutronix.de |
| 5 | * Copyright 2009 Ilya Yanok, <yanok@emcraft.com> |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <nand.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <asm/io.h> |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 12 | #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX35) || \ |
| 13 | defined(CONFIG_MX51) || defined(CONFIG_MX53) |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 14 | #include <asm/arch/imx-regs.h> |
| 15 | #endif |
Benoît Thébaudeau | efb7c00 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 16 | #include "mxc_nand.h" |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 17 | |
| 18 | #define DRIVER_NAME "mxc_nand" |
| 19 | |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 20 | struct mxc_nand_host { |
Benoît Thébaudeau | 3f77519 | 2012-08-13 22:48:12 +0200 | [diff] [blame] | 21 | struct nand_chip *nand; |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 22 | |
Benoît Thébaudeau | efb7c00 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 23 | struct mxc_nand_regs __iomem *regs; |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 24 | #ifdef MXC_NFC_V3_2 |
Benoît Thébaudeau | efb7c00 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 25 | struct mxc_nand_ip_regs __iomem *ip_regs; |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 26 | #endif |
Benoît Thébaudeau | 3f77519 | 2012-08-13 22:48:12 +0200 | [diff] [blame] | 27 | int spare_only; |
| 28 | int status_request; |
| 29 | int pagesize_2k; |
| 30 | int clk_act; |
| 31 | uint16_t col_addr; |
| 32 | unsigned int page_addr; |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static struct mxc_nand_host mxc_host; |
| 36 | static struct mxc_nand_host *host = &mxc_host; |
| 37 | |
| 38 | /* Define delays in microsec for NAND device operations */ |
| 39 | #define TROP_US_DELAY 2000 |
| 40 | /* Macros to get byte and bit positions of ECC */ |
| 41 | #define COLPOS(x) ((x) >> 3) |
| 42 | #define BITPOS(x) ((x) & 0xf) |
| 43 | |
| 44 | /* Define single bit Error positions in Main & Spare area */ |
| 45 | #define MAIN_SINGLEBIT_ERROR 0x4 |
| 46 | #define SPARE_SINGLEBIT_ERROR 0x1 |
| 47 | |
| 48 | /* OOB placement block for use with hardware ecc generation */ |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 49 | #if defined(MXC_NFC_V1) |
| 50 | #ifndef CONFIG_SYS_NAND_LARGEPAGE |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 51 | static struct nand_ecclayout nand_hw_eccoob = { |
| 52 | .eccbytes = 5, |
| 53 | .eccpos = {6, 7, 8, 9, 10}, |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 54 | .oobfree = { {0, 5}, {11, 5}, } |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 55 | }; |
| 56 | #else |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 57 | static struct nand_ecclayout nand_hw_eccoob2k = { |
| 58 | .eccbytes = 20, |
| 59 | .eccpos = { |
| 60 | 6, 7, 8, 9, 10, |
| 61 | 22, 23, 24, 25, 26, |
| 62 | 38, 39, 40, 41, 42, |
| 63 | 54, 55, 56, 57, 58, |
| 64 | }, |
| 65 | .oobfree = { {2, 4}, {11, 11}, {27, 11}, {43, 11}, {59, 5} }, |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 66 | }; |
| 67 | #endif |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 68 | #elif defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2) |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 69 | #ifndef CONFIG_SYS_NAND_LARGEPAGE |
| 70 | static struct nand_ecclayout nand_hw_eccoob = { |
| 71 | .eccbytes = 9, |
| 72 | .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15}, |
| 73 | .oobfree = { {2, 5} } |
| 74 | }; |
| 75 | #else |
| 76 | static struct nand_ecclayout nand_hw_eccoob2k = { |
| 77 | .eccbytes = 36, |
| 78 | .eccpos = { |
| 79 | 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 80 | 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 81 | 39, 40, 41, 42, 43, 44, 45, 46, 47, |
| 82 | 55, 56, 57, 58, 59, 60, 61, 62, 63, |
| 83 | }, |
| 84 | .oobfree = { {2, 5}, {16, 7}, {32, 7}, {48, 7} }, |
Magnus Lilja | ab5b3b6 | 2010-01-17 17:46:10 +0100 | [diff] [blame] | 85 | }; |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 86 | #endif |
| 87 | #endif |
Magnus Lilja | ab5b3b6 | 2010-01-17 17:46:10 +0100 | [diff] [blame] | 88 | |
Magnus Lilja | 6e0dbd8 | 2009-11-11 20:18:43 +0100 | [diff] [blame] | 89 | static int is_16bit_nand(void) |
| 90 | { |
Fabio Estevam | 417052b | 2013-04-11 09:35:35 +0000 | [diff] [blame] | 91 | #if defined(CONFIG_SYS_NAND_BUSWIDTH_16BIT) |
| 92 | return 1; |
Magnus Lilja | 6e0dbd8 | 2009-11-11 20:18:43 +0100 | [diff] [blame] | 93 | #else |
Magnus Lilja | 6e0dbd8 | 2009-11-11 20:18:43 +0100 | [diff] [blame] | 94 | return 0; |
Magnus Lilja | 6e0dbd8 | 2009-11-11 20:18:43 +0100 | [diff] [blame] | 95 | #endif |
Fabio Estevam | 417052b | 2013-04-11 09:35:35 +0000 | [diff] [blame] | 96 | } |
Magnus Lilja | 6e0dbd8 | 2009-11-11 20:18:43 +0100 | [diff] [blame] | 97 | |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 98 | static uint32_t *mxc_nand_memcpy32(uint32_t *dest, uint32_t *source, size_t size) |
| 99 | { |
| 100 | uint32_t *d = dest; |
| 101 | |
| 102 | size >>= 2; |
| 103 | while (size--) |
| 104 | __raw_writel(__raw_readl(source++), d++); |
| 105 | return dest; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * This function polls the NANDFC to wait for the basic operation to |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 110 | * complete by checking the INT bit. |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 111 | */ |
| 112 | static void wait_op_done(struct mxc_nand_host *host, int max_retries, |
| 113 | uint16_t param) |
| 114 | { |
| 115 | uint32_t tmp; |
| 116 | |
| 117 | while (max_retries-- > 0) { |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 118 | #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1) |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 119 | tmp = readnfc(&host->regs->config2); |
| 120 | if (tmp & NFC_V1_V2_CONFIG2_INT) { |
| 121 | tmp &= ~NFC_V1_V2_CONFIG2_INT; |
| 122 | writenfc(tmp, &host->regs->config2); |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 123 | #elif defined(MXC_NFC_V3_2) |
| 124 | tmp = readnfc(&host->ip_regs->ipc); |
| 125 | if (tmp & NFC_V3_IPC_INT) { |
| 126 | tmp &= ~NFC_V3_IPC_INT; |
| 127 | writenfc(tmp, &host->ip_regs->ipc); |
| 128 | #endif |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 129 | break; |
| 130 | } |
| 131 | udelay(1); |
| 132 | } |
| 133 | if (max_retries < 0) { |
Masahiro Yamada | f8a5d51 | 2017-10-18 00:10:48 +0900 | [diff] [blame] | 134 | pr_debug("%s(%d): INT not set\n", |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 135 | __func__, param); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * This function issues the specified command to the NAND device and |
| 141 | * waits for completion. |
| 142 | */ |
| 143 | static void send_cmd(struct mxc_nand_host *host, uint16_t cmd) |
| 144 | { |
Masahiro Yamada | f8a5d51 | 2017-10-18 00:10:48 +0900 | [diff] [blame] | 145 | pr_debug("send_cmd(host, 0x%x)\n", cmd); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 146 | |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 147 | writenfc(cmd, &host->regs->flash_cmd); |
| 148 | writenfc(NFC_CMD, &host->regs->operation); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 149 | |
| 150 | /* Wait for operation to complete */ |
| 151 | wait_op_done(host, TROP_US_DELAY, cmd); |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * This function sends an address (or partial address) to the |
| 156 | * NAND device. The address is used to select the source/destination for |
| 157 | * a NAND command. |
| 158 | */ |
| 159 | static void send_addr(struct mxc_nand_host *host, uint16_t addr) |
| 160 | { |
Masahiro Yamada | f8a5d51 | 2017-10-18 00:10:48 +0900 | [diff] [blame] | 161 | pr_debug("send_addr(host, 0x%x)\n", addr); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 162 | |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 163 | writenfc(addr, &host->regs->flash_addr); |
| 164 | writenfc(NFC_ADDR, &host->regs->operation); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 165 | |
| 166 | /* Wait for operation to complete */ |
| 167 | wait_op_done(host, TROP_US_DELAY, addr); |
| 168 | } |
| 169 | |
| 170 | /* |
Helmut Raiger | c8f4a4c | 2011-07-06 09:40:28 +0200 | [diff] [blame] | 171 | * This function requests the NANDFC to initiate the transfer |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 172 | * of data currently in the NANDFC RAM buffer to the NAND device. |
| 173 | */ |
| 174 | static void send_prog_page(struct mxc_nand_host *host, uint8_t buf_id, |
| 175 | int spare_only) |
| 176 | { |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 177 | if (spare_only) |
Masahiro Yamada | f8a5d51 | 2017-10-18 00:10:48 +0900 | [diff] [blame] | 178 | pr_debug("send_prog_page (%d)\n", spare_only); |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 179 | |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 180 | if (is_mxc_nfc_21() || is_mxc_nfc_32()) { |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 181 | int i; |
| 182 | /* |
| 183 | * The controller copies the 64 bytes of spare data from |
| 184 | * the first 16 bytes of each of the 4 64 byte spare buffers. |
| 185 | * Copy the contiguous data starting in spare_area[0] to |
| 186 | * the four spare area buffers. |
| 187 | */ |
| 188 | for (i = 1; i < 4; i++) { |
| 189 | void __iomem *src = &host->regs->spare_area[0][i * 16]; |
| 190 | void __iomem *dst = &host->regs->spare_area[i][0]; |
| 191 | |
| 192 | mxc_nand_memcpy32(dst, src, 16); |
| 193 | } |
| 194 | } |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 195 | |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 196 | #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1) |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 197 | writenfc(buf_id, &host->regs->buf_addr); |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 198 | #elif defined(MXC_NFC_V3_2) |
| 199 | uint32_t tmp = readnfc(&host->regs->config1); |
| 200 | tmp &= ~NFC_V3_CONFIG1_RBA_MASK; |
| 201 | tmp |= NFC_V3_CONFIG1_RBA(buf_id); |
| 202 | writenfc(tmp, &host->regs->config1); |
| 203 | #endif |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 204 | |
| 205 | /* Configure spare or page+spare access */ |
| 206 | if (!host->pagesize_2k) { |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 207 | uint32_t config1 = readnfc(&host->regs->config1); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 208 | if (spare_only) |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 209 | config1 |= NFC_CONFIG1_SP_EN; |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 210 | else |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 211 | config1 &= ~NFC_CONFIG1_SP_EN; |
| 212 | writenfc(config1, &host->regs->config1); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 213 | } |
| 214 | |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 215 | writenfc(NFC_INPUT, &host->regs->operation); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 216 | |
| 217 | /* Wait for operation to complete */ |
| 218 | wait_op_done(host, TROP_US_DELAY, spare_only); |
| 219 | } |
| 220 | |
| 221 | /* |
Helmut Raiger | c8f4a4c | 2011-07-06 09:40:28 +0200 | [diff] [blame] | 222 | * Requests NANDFC to initiate the transfer of data from the |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 223 | * NAND device into in the NANDFC ram buffer. |
| 224 | */ |
| 225 | static void send_read_page(struct mxc_nand_host *host, uint8_t buf_id, |
| 226 | int spare_only) |
| 227 | { |
Masahiro Yamada | f8a5d51 | 2017-10-18 00:10:48 +0900 | [diff] [blame] | 228 | pr_debug("send_read_page (%d)\n", spare_only); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 229 | |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 230 | #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1) |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 231 | writenfc(buf_id, &host->regs->buf_addr); |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 232 | #elif defined(MXC_NFC_V3_2) |
| 233 | uint32_t tmp = readnfc(&host->regs->config1); |
| 234 | tmp &= ~NFC_V3_CONFIG1_RBA_MASK; |
| 235 | tmp |= NFC_V3_CONFIG1_RBA(buf_id); |
| 236 | writenfc(tmp, &host->regs->config1); |
| 237 | #endif |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 238 | |
| 239 | /* Configure spare or page+spare access */ |
| 240 | if (!host->pagesize_2k) { |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 241 | uint32_t config1 = readnfc(&host->regs->config1); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 242 | if (spare_only) |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 243 | config1 |= NFC_CONFIG1_SP_EN; |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 244 | else |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 245 | config1 &= ~NFC_CONFIG1_SP_EN; |
| 246 | writenfc(config1, &host->regs->config1); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 247 | } |
| 248 | |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 249 | writenfc(NFC_OUTPUT, &host->regs->operation); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 250 | |
| 251 | /* Wait for operation to complete */ |
| 252 | wait_op_done(host, TROP_US_DELAY, spare_only); |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 253 | |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 254 | if (is_mxc_nfc_21() || is_mxc_nfc_32()) { |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 255 | int i; |
| 256 | |
| 257 | /* |
| 258 | * The controller copies the 64 bytes of spare data to |
| 259 | * the first 16 bytes of each of the 4 spare buffers. |
| 260 | * Make the data contiguous starting in spare_area[0]. |
| 261 | */ |
| 262 | for (i = 1; i < 4; i++) { |
| 263 | void __iomem *src = &host->regs->spare_area[i][0]; |
| 264 | void __iomem *dst = &host->regs->spare_area[0][i * 16]; |
| 265 | |
| 266 | mxc_nand_memcpy32(dst, src, 16); |
| 267 | } |
| 268 | } |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* Request the NANDFC to perform a read of the NAND device ID. */ |
| 272 | static void send_read_id(struct mxc_nand_host *host) |
| 273 | { |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 274 | uint32_t tmp; |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 275 | |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 276 | #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1) |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 277 | /* NANDFC buffer 0 is used for device ID output */ |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 278 | writenfc(0x0, &host->regs->buf_addr); |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 279 | #elif defined(MXC_NFC_V3_2) |
| 280 | tmp = readnfc(&host->regs->config1); |
| 281 | tmp &= ~NFC_V3_CONFIG1_RBA_MASK; |
| 282 | writenfc(tmp, &host->regs->config1); |
| 283 | #endif |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 284 | |
| 285 | /* Read ID into main buffer */ |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 286 | tmp = readnfc(&host->regs->config1); |
| 287 | tmp &= ~NFC_CONFIG1_SP_EN; |
| 288 | writenfc(tmp, &host->regs->config1); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 289 | |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 290 | writenfc(NFC_ID, &host->regs->operation); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 291 | |
| 292 | /* Wait for operation to complete */ |
| 293 | wait_op_done(host, TROP_US_DELAY, 0); |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * This function requests the NANDFC to perform a read of the |
| 298 | * NAND device status and returns the current status. |
| 299 | */ |
| 300 | static uint16_t get_dev_status(struct mxc_nand_host *host) |
| 301 | { |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 302 | #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1) |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 303 | void __iomem *main_buf = host->regs->main_area[1]; |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 304 | uint32_t store; |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 305 | #endif |
| 306 | uint32_t ret, tmp; |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 307 | /* Issue status request to NAND device */ |
| 308 | |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 309 | #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1) |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 310 | /* store the main area1 first word, later do recovery */ |
| 311 | store = readl(main_buf); |
| 312 | /* NANDFC buffer 1 is used for device status */ |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 313 | writenfc(1, &host->regs->buf_addr); |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 314 | #endif |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 315 | |
| 316 | /* Read status into main buffer */ |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 317 | tmp = readnfc(&host->regs->config1); |
| 318 | tmp &= ~NFC_CONFIG1_SP_EN; |
| 319 | writenfc(tmp, &host->regs->config1); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 320 | |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 321 | writenfc(NFC_STATUS, &host->regs->operation); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 322 | |
| 323 | /* Wait for operation to complete */ |
| 324 | wait_op_done(host, TROP_US_DELAY, 0); |
| 325 | |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 326 | #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1) |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 327 | /* |
| 328 | * Status is placed in first word of main buffer |
| 329 | * get status, then recovery area 1 data |
| 330 | */ |
| 331 | ret = readw(main_buf); |
| 332 | writel(store, main_buf); |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 333 | #elif defined(MXC_NFC_V3_2) |
| 334 | ret = readnfc(&host->regs->config1) >> 16; |
| 335 | #endif |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 336 | |
| 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | /* This function is used by upper layer to checks if device is ready */ |
| 341 | static int mxc_nand_dev_ready(struct mtd_info *mtd) |
| 342 | { |
| 343 | /* |
| 344 | * NFC handles R/B internally. Therefore, this function |
| 345 | * always returns status as ready. |
| 346 | */ |
| 347 | return 1; |
| 348 | } |
| 349 | |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 350 | static void _mxc_nand_enable_hwecc(struct mtd_info *mtd, int on) |
| 351 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 352 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
| 353 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 354 | #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1) |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 355 | uint16_t tmp = readnfc(&host->regs->config1); |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 356 | |
| 357 | if (on) |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 358 | tmp |= NFC_V1_V2_CONFIG1_ECC_EN; |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 359 | else |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 360 | tmp &= ~NFC_V1_V2_CONFIG1_ECC_EN; |
| 361 | writenfc(tmp, &host->regs->config1); |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 362 | #elif defined(MXC_NFC_V3_2) |
| 363 | uint32_t tmp = readnfc(&host->ip_regs->config2); |
| 364 | |
| 365 | if (on) |
| 366 | tmp |= NFC_V3_CONFIG2_ECC_EN; |
| 367 | else |
| 368 | tmp &= ~NFC_V3_CONFIG2_ECC_EN; |
| 369 | writenfc(tmp, &host->ip_regs->config2); |
| 370 | #endif |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Benoît Thébaudeau | 9166d5f | 2012-08-13 22:50:07 +0200 | [diff] [blame] | 373 | #ifdef CONFIG_MXC_NAND_HWECC |
| 374 | static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode) |
| 375 | { |
| 376 | /* |
| 377 | * If HW ECC is enabled, we turn it on during init. There is |
| 378 | * no need to enable again here. |
| 379 | */ |
| 380 | } |
| 381 | |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 382 | #if defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2) |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 383 | static int mxc_nand_read_oob_syndrome(struct mtd_info *mtd, |
| 384 | struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 385 | int page) |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 386 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 387 | struct mxc_nand_host *host = nand_get_controller_data(chip); |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 388 | uint8_t *buf = chip->oob_poi; |
| 389 | int length = mtd->oobsize; |
| 390 | int eccpitch = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 391 | uint8_t *bufpoi = buf; |
| 392 | int i, toread; |
| 393 | |
Masahiro Yamada | f8a5d51 | 2017-10-18 00:10:48 +0900 | [diff] [blame] | 394 | pr_debug("%s: Reading OOB area of page %u to oob %p\n", |
Benoît Thébaudeau | 9889e3f | 2013-04-11 09:35:40 +0000 | [diff] [blame] | 395 | __func__, page, buf); |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 396 | |
| 397 | chip->cmdfunc(mtd, NAND_CMD_READOOB, mtd->writesize, page); |
| 398 | for (i = 0; i < chip->ecc.steps; i++) { |
| 399 | toread = min_t(int, length, chip->ecc.prepad); |
| 400 | if (toread) { |
| 401 | chip->read_buf(mtd, bufpoi, toread); |
| 402 | bufpoi += toread; |
| 403 | length -= toread; |
| 404 | } |
| 405 | bufpoi += chip->ecc.bytes; |
| 406 | host->col_addr += chip->ecc.bytes; |
| 407 | length -= chip->ecc.bytes; |
| 408 | |
| 409 | toread = min_t(int, length, chip->ecc.postpad); |
| 410 | if (toread) { |
| 411 | chip->read_buf(mtd, bufpoi, toread); |
| 412 | bufpoi += toread; |
| 413 | length -= toread; |
| 414 | } |
| 415 | } |
| 416 | if (length > 0) |
| 417 | chip->read_buf(mtd, bufpoi, length); |
| 418 | |
| 419 | _mxc_nand_enable_hwecc(mtd, 0); |
| 420 | chip->cmdfunc(mtd, NAND_CMD_READOOB, |
| 421 | mtd->writesize + chip->ecc.prepad, page); |
| 422 | bufpoi = buf + chip->ecc.prepad; |
| 423 | length = mtd->oobsize - chip->ecc.prepad; |
| 424 | for (i = 0; i < chip->ecc.steps; i++) { |
| 425 | toread = min_t(int, length, chip->ecc.bytes); |
| 426 | chip->read_buf(mtd, bufpoi, toread); |
| 427 | bufpoi += eccpitch; |
| 428 | length -= eccpitch; |
| 429 | host->col_addr += chip->ecc.postpad + chip->ecc.prepad; |
| 430 | } |
| 431 | _mxc_nand_enable_hwecc(mtd, 1); |
| 432 | return 1; |
| 433 | } |
| 434 | |
| 435 | static int mxc_nand_read_page_raw_syndrome(struct mtd_info *mtd, |
| 436 | struct nand_chip *chip, |
| 437 | uint8_t *buf, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 438 | int oob_required, |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 439 | int page) |
| 440 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 441 | struct mxc_nand_host *host = nand_get_controller_data(chip); |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 442 | int eccsize = chip->ecc.size; |
| 443 | int eccbytes = chip->ecc.bytes; |
| 444 | int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
| 445 | uint8_t *oob = chip->oob_poi; |
| 446 | int steps, size; |
| 447 | int n; |
| 448 | |
| 449 | _mxc_nand_enable_hwecc(mtd, 0); |
Benoît Thébaudeau | 4b0dbba | 2013-04-11 09:35:41 +0000 | [diff] [blame] | 450 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page); |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 451 | |
| 452 | for (n = 0, steps = chip->ecc.steps; steps > 0; n++, steps--) { |
| 453 | host->col_addr = n * eccsize; |
| 454 | chip->read_buf(mtd, buf, eccsize); |
| 455 | buf += eccsize; |
| 456 | |
| 457 | host->col_addr = mtd->writesize + n * eccpitch; |
| 458 | if (chip->ecc.prepad) { |
| 459 | chip->read_buf(mtd, oob, chip->ecc.prepad); |
| 460 | oob += chip->ecc.prepad; |
| 461 | } |
| 462 | |
| 463 | chip->read_buf(mtd, oob, eccbytes); |
| 464 | oob += eccbytes; |
| 465 | |
| 466 | if (chip->ecc.postpad) { |
| 467 | chip->read_buf(mtd, oob, chip->ecc.postpad); |
| 468 | oob += chip->ecc.postpad; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | size = mtd->oobsize - (oob - chip->oob_poi); |
| 473 | if (size) |
| 474 | chip->read_buf(mtd, oob, size); |
Benoît Thébaudeau | c157827 | 2012-08-13 22:50:19 +0200 | [diff] [blame] | 475 | _mxc_nand_enable_hwecc(mtd, 1); |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | static int mxc_nand_read_page_syndrome(struct mtd_info *mtd, |
| 481 | struct nand_chip *chip, |
| 482 | uint8_t *buf, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 483 | int oob_required, |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 484 | int page) |
| 485 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 486 | struct mxc_nand_host *host = nand_get_controller_data(chip); |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 487 | int n, eccsize = chip->ecc.size; |
| 488 | int eccbytes = chip->ecc.bytes; |
| 489 | int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
| 490 | int eccsteps = chip->ecc.steps; |
| 491 | uint8_t *p = buf; |
| 492 | uint8_t *oob = chip->oob_poi; |
| 493 | |
Masahiro Yamada | f8a5d51 | 2017-10-18 00:10:48 +0900 | [diff] [blame] | 494 | pr_debug("Reading page %u to buf %p oob %p\n", |
| 495 | page, buf, oob); |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 496 | |
Helmut Raiger | c8f4a4c | 2011-07-06 09:40:28 +0200 | [diff] [blame] | 497 | /* first read the data area and the available portion of OOB */ |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 498 | for (n = 0; eccsteps; n++, eccsteps--, p += eccsize) { |
| 499 | int stat; |
| 500 | |
| 501 | host->col_addr = n * eccsize; |
| 502 | |
| 503 | chip->read_buf(mtd, p, eccsize); |
| 504 | |
| 505 | host->col_addr = mtd->writesize + n * eccpitch; |
| 506 | |
| 507 | if (chip->ecc.prepad) { |
| 508 | chip->read_buf(mtd, oob, chip->ecc.prepad); |
| 509 | oob += chip->ecc.prepad; |
| 510 | } |
| 511 | |
| 512 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
| 513 | |
| 514 | if (stat < 0) |
| 515 | mtd->ecc_stats.failed++; |
| 516 | else |
| 517 | mtd->ecc_stats.corrected += stat; |
| 518 | oob += eccbytes; |
| 519 | |
| 520 | if (chip->ecc.postpad) { |
| 521 | chip->read_buf(mtd, oob, chip->ecc.postpad); |
| 522 | oob += chip->ecc.postpad; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /* Calculate remaining oob bytes */ |
| 527 | n = mtd->oobsize - (oob - chip->oob_poi); |
| 528 | if (n) |
| 529 | chip->read_buf(mtd, oob, n); |
| 530 | |
| 531 | /* Then switch ECC off and read the OOB area to get the ECC code */ |
| 532 | _mxc_nand_enable_hwecc(mtd, 0); |
Benoît Thébaudeau | 4b0dbba | 2013-04-11 09:35:41 +0000 | [diff] [blame] | 533 | chip->cmdfunc(mtd, NAND_CMD_READOOB, mtd->writesize, page); |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 534 | eccsteps = chip->ecc.steps; |
| 535 | oob = chip->oob_poi + chip->ecc.prepad; |
| 536 | for (n = 0; eccsteps; n++, eccsteps--, p += eccsize) { |
| 537 | host->col_addr = mtd->writesize + |
| 538 | n * eccpitch + |
| 539 | chip->ecc.prepad; |
| 540 | chip->read_buf(mtd, oob, eccbytes); |
| 541 | oob += eccbytes + chip->ecc.postpad; |
| 542 | } |
| 543 | _mxc_nand_enable_hwecc(mtd, 1); |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | static int mxc_nand_write_oob_syndrome(struct mtd_info *mtd, |
| 548 | struct nand_chip *chip, int page) |
| 549 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 550 | struct mxc_nand_host *host = nand_get_controller_data(chip); |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 551 | int eccpitch = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 552 | int length = mtd->oobsize; |
| 553 | int i, len, status, steps = chip->ecc.steps; |
| 554 | const uint8_t *bufpoi = chip->oob_poi; |
| 555 | |
| 556 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page); |
| 557 | for (i = 0; i < steps; i++) { |
| 558 | len = min_t(int, length, eccpitch); |
| 559 | |
| 560 | chip->write_buf(mtd, bufpoi, len); |
| 561 | bufpoi += len; |
| 562 | length -= len; |
| 563 | host->col_addr += chip->ecc.prepad + chip->ecc.postpad; |
| 564 | } |
| 565 | if (length > 0) |
| 566 | chip->write_buf(mtd, bufpoi, length); |
| 567 | |
| 568 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 569 | status = chip->waitfunc(mtd, chip); |
| 570 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
| 571 | } |
| 572 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 573 | static int mxc_nand_write_page_raw_syndrome(struct mtd_info *mtd, |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 574 | struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 575 | const uint8_t *buf, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 576 | int oob_required, int page) |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 577 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 578 | struct mxc_nand_host *host = nand_get_controller_data(chip); |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 579 | int eccsize = chip->ecc.size; |
| 580 | int eccbytes = chip->ecc.bytes; |
| 581 | int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
| 582 | uint8_t *oob = chip->oob_poi; |
| 583 | int steps, size; |
| 584 | int n; |
| 585 | |
| 586 | for (n = 0, steps = chip->ecc.steps; steps > 0; n++, steps--) { |
| 587 | host->col_addr = n * eccsize; |
| 588 | chip->write_buf(mtd, buf, eccsize); |
| 589 | buf += eccsize; |
| 590 | |
| 591 | host->col_addr = mtd->writesize + n * eccpitch; |
| 592 | |
| 593 | if (chip->ecc.prepad) { |
| 594 | chip->write_buf(mtd, oob, chip->ecc.prepad); |
| 595 | oob += chip->ecc.prepad; |
| 596 | } |
| 597 | |
| 598 | host->col_addr += eccbytes; |
| 599 | oob += eccbytes; |
| 600 | |
| 601 | if (chip->ecc.postpad) { |
| 602 | chip->write_buf(mtd, oob, chip->ecc.postpad); |
| 603 | oob += chip->ecc.postpad; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | size = mtd->oobsize - (oob - chip->oob_poi); |
| 608 | if (size) |
| 609 | chip->write_buf(mtd, oob, size); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 610 | return 0; |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 613 | static int mxc_nand_write_page_syndrome(struct mtd_info *mtd, |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 614 | struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 615 | const uint8_t *buf, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 616 | int oob_required, int page) |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 617 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 618 | struct mxc_nand_host *host = nand_get_controller_data(chip); |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 619 | int i, n, eccsize = chip->ecc.size; |
| 620 | int eccbytes = chip->ecc.bytes; |
| 621 | int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
| 622 | int eccsteps = chip->ecc.steps; |
| 623 | const uint8_t *p = buf; |
| 624 | uint8_t *oob = chip->oob_poi; |
| 625 | |
| 626 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 627 | |
| 628 | for (i = n = 0; |
| 629 | eccsteps; |
| 630 | n++, eccsteps--, i += eccbytes, p += eccsize) { |
| 631 | host->col_addr = n * eccsize; |
| 632 | |
| 633 | chip->write_buf(mtd, p, eccsize); |
| 634 | |
| 635 | host->col_addr = mtd->writesize + n * eccpitch; |
| 636 | |
| 637 | if (chip->ecc.prepad) { |
| 638 | chip->write_buf(mtd, oob, chip->ecc.prepad); |
| 639 | oob += chip->ecc.prepad; |
| 640 | } |
| 641 | |
| 642 | chip->write_buf(mtd, oob, eccbytes); |
| 643 | oob += eccbytes; |
| 644 | |
| 645 | if (chip->ecc.postpad) { |
| 646 | chip->write_buf(mtd, oob, chip->ecc.postpad); |
| 647 | oob += chip->ecc.postpad; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /* Calculate remaining oob bytes */ |
| 652 | i = mtd->oobsize - (oob - chip->oob_poi); |
| 653 | if (i) |
| 654 | chip->write_buf(mtd, oob, i); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 655 | return 0; |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 658 | static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat, |
| 659 | u_char *read_ecc, u_char *calc_ecc) |
| 660 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 661 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
| 662 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Benoît Thébaudeau | d29aeba | 2012-08-13 22:49:42 +0200 | [diff] [blame] | 663 | uint32_t ecc_status = readl(&host->regs->ecc_status_result); |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 664 | int subpages = mtd->writesize / nand_chip->subpagesize; |
| 665 | int pg2blk_shift = nand_chip->phys_erase_shift - |
| 666 | nand_chip->page_shift; |
| 667 | |
| 668 | do { |
| 669 | if ((ecc_status & 0xf) > 4) { |
| 670 | static int last_bad = -1; |
| 671 | |
| 672 | if (last_bad != host->page_addr >> pg2blk_shift) { |
| 673 | last_bad = host->page_addr >> pg2blk_shift; |
| 674 | printk(KERN_DEBUG |
| 675 | "MXC_NAND: HWECC uncorrectable ECC error" |
| 676 | " in block %u page %u subpage %d\n", |
| 677 | last_bad, host->page_addr, |
| 678 | mtd->writesize / nand_chip->subpagesize |
| 679 | - subpages); |
| 680 | } |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 681 | return -EBADMSG; |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 682 | } |
| 683 | ecc_status >>= 4; |
| 684 | subpages--; |
| 685 | } while (subpages > 0); |
| 686 | |
| 687 | return 0; |
| 688 | } |
| 689 | #else |
| 690 | #define mxc_nand_read_page_syndrome NULL |
| 691 | #define mxc_nand_read_page_raw_syndrome NULL |
| 692 | #define mxc_nand_read_oob_syndrome NULL |
| 693 | #define mxc_nand_write_page_syndrome NULL |
| 694 | #define mxc_nand_write_page_raw_syndrome NULL |
| 695 | #define mxc_nand_write_oob_syndrome NULL |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 696 | |
| 697 | static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat, |
| 698 | u_char *read_ecc, u_char *calc_ecc) |
| 699 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 700 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
| 701 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 702 | |
| 703 | /* |
| 704 | * 1-Bit errors are automatically corrected in HW. No need for |
| 705 | * additional correction. 2-Bit errors cannot be corrected by |
| 706 | * HW ECC, so we need to return failure |
| 707 | */ |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 708 | uint16_t ecc_status = readnfc(&host->regs->ecc_status_result); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 709 | |
| 710 | if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) { |
Masahiro Yamada | f8a5d51 | 2017-10-18 00:10:48 +0900 | [diff] [blame] | 711 | pr_debug("MXC_NAND: HWECC uncorrectable 2-bit ECC error\n"); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 712 | return -EBADMSG; |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | return 0; |
| 716 | } |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 717 | #endif |
| 718 | |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 719 | static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, |
| 720 | u_char *ecc_code) |
| 721 | { |
| 722 | return 0; |
| 723 | } |
| 724 | #endif |
| 725 | |
| 726 | static u_char mxc_nand_read_byte(struct mtd_info *mtd) |
| 727 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 728 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
| 729 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 730 | uint8_t ret = 0; |
| 731 | uint16_t col; |
| 732 | uint16_t __iomem *main_buf = |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 733 | (uint16_t __iomem *)host->regs->main_area[0]; |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 734 | uint16_t __iomem *spare_buf = |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 735 | (uint16_t __iomem *)host->regs->spare_area[0]; |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 736 | union { |
| 737 | uint16_t word; |
| 738 | uint8_t bytes[2]; |
| 739 | } nfc_word; |
| 740 | |
| 741 | /* Check for status request */ |
| 742 | if (host->status_request) |
| 743 | return get_dev_status(host) & 0xFF; |
| 744 | |
| 745 | /* Get column for 16-bit access */ |
| 746 | col = host->col_addr >> 1; |
| 747 | |
| 748 | /* If we are accessing the spare region */ |
| 749 | if (host->spare_only) |
| 750 | nfc_word.word = readw(&spare_buf[col]); |
| 751 | else |
| 752 | nfc_word.word = readw(&main_buf[col]); |
| 753 | |
| 754 | /* Pick upper/lower byte of word from RAM buffer */ |
| 755 | ret = nfc_word.bytes[host->col_addr & 0x1]; |
| 756 | |
| 757 | /* Update saved column address */ |
| 758 | if (nand_chip->options & NAND_BUSWIDTH_16) |
| 759 | host->col_addr += 2; |
| 760 | else |
| 761 | host->col_addr++; |
| 762 | |
| 763 | return ret; |
| 764 | } |
| 765 | |
| 766 | static uint16_t mxc_nand_read_word(struct mtd_info *mtd) |
| 767 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 768 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
| 769 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 770 | uint16_t col, ret; |
| 771 | uint16_t __iomem *p; |
| 772 | |
Masahiro Yamada | f8a5d51 | 2017-10-18 00:10:48 +0900 | [diff] [blame] | 773 | pr_debug("mxc_nand_read_word(col = %d)\n", host->col_addr); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 774 | |
| 775 | col = host->col_addr; |
| 776 | /* Adjust saved column address */ |
| 777 | if (col < mtd->writesize && host->spare_only) |
| 778 | col += mtd->writesize; |
| 779 | |
| 780 | if (col < mtd->writesize) { |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 781 | p = (uint16_t __iomem *)(host->regs->main_area[0] + |
| 782 | (col >> 1)); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 783 | } else { |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 784 | p = (uint16_t __iomem *)(host->regs->spare_area[0] + |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 785 | ((col - mtd->writesize) >> 1)); |
| 786 | } |
| 787 | |
| 788 | if (col & 1) { |
| 789 | union { |
| 790 | uint16_t word; |
| 791 | uint8_t bytes[2]; |
| 792 | } nfc_word[3]; |
| 793 | |
| 794 | nfc_word[0].word = readw(p); |
| 795 | nfc_word[1].word = readw(p + 1); |
| 796 | |
| 797 | nfc_word[2].bytes[0] = nfc_word[0].bytes[1]; |
| 798 | nfc_word[2].bytes[1] = nfc_word[1].bytes[0]; |
| 799 | |
| 800 | ret = nfc_word[2].word; |
| 801 | } else { |
| 802 | ret = readw(p); |
| 803 | } |
| 804 | |
| 805 | /* Update saved column address */ |
| 806 | host->col_addr = col + 2; |
| 807 | |
| 808 | return ret; |
| 809 | } |
| 810 | |
| 811 | /* |
| 812 | * Write data of length len to buffer buf. The data to be |
| 813 | * written on NAND Flash is first copied to RAMbuffer. After the Data Input |
| 814 | * Operation by the NFC, the data is written to NAND Flash |
| 815 | */ |
| 816 | static void mxc_nand_write_buf(struct mtd_info *mtd, |
| 817 | const u_char *buf, int len) |
| 818 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 819 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
| 820 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 821 | int n, col, i = 0; |
| 822 | |
Masahiro Yamada | f8a5d51 | 2017-10-18 00:10:48 +0900 | [diff] [blame] | 823 | pr_debug("mxc_nand_write_buf(col = %d, len = %d)\n", host->col_addr, |
| 824 | len); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 825 | |
| 826 | col = host->col_addr; |
| 827 | |
| 828 | /* Adjust saved column address */ |
| 829 | if (col < mtd->writesize && host->spare_only) |
| 830 | col += mtd->writesize; |
| 831 | |
| 832 | n = mtd->writesize + mtd->oobsize - col; |
| 833 | n = min(len, n); |
| 834 | |
Masahiro Yamada | f8a5d51 | 2017-10-18 00:10:48 +0900 | [diff] [blame] | 835 | pr_debug("%s:%d: col = %d, n = %d\n", __func__, __LINE__, col, n); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 836 | |
| 837 | while (n > 0) { |
| 838 | void __iomem *p; |
| 839 | |
| 840 | if (col < mtd->writesize) { |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 841 | p = host->regs->main_area[0] + (col & ~3); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 842 | } else { |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 843 | p = host->regs->spare_area[0] - |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 844 | mtd->writesize + (col & ~3); |
| 845 | } |
| 846 | |
Masahiro Yamada | f8a5d51 | 2017-10-18 00:10:48 +0900 | [diff] [blame] | 847 | pr_debug("%s:%d: p = %p\n", __func__, |
| 848 | __LINE__, p); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 849 | |
| 850 | if (((col | (unsigned long)&buf[i]) & 3) || n < 4) { |
| 851 | union { |
| 852 | uint32_t word; |
| 853 | uint8_t bytes[4]; |
| 854 | } nfc_word; |
| 855 | |
| 856 | nfc_word.word = readl(p); |
| 857 | nfc_word.bytes[col & 3] = buf[i++]; |
| 858 | n--; |
| 859 | col++; |
| 860 | |
| 861 | writel(nfc_word.word, p); |
| 862 | } else { |
| 863 | int m = mtd->writesize - col; |
| 864 | |
| 865 | if (col >= mtd->writesize) |
| 866 | m += mtd->oobsize; |
| 867 | |
| 868 | m = min(n, m) & ~3; |
| 869 | |
Masahiro Yamada | f8a5d51 | 2017-10-18 00:10:48 +0900 | [diff] [blame] | 870 | pr_debug("%s:%d: n = %d, m = %d, i = %d, col = %d\n", |
| 871 | __func__, __LINE__, n, m, i, col); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 872 | |
| 873 | mxc_nand_memcpy32(p, (uint32_t *)&buf[i], m); |
| 874 | col += m; |
| 875 | i += m; |
| 876 | n -= m; |
| 877 | } |
| 878 | } |
| 879 | /* Update saved column address */ |
| 880 | host->col_addr = col; |
| 881 | } |
| 882 | |
| 883 | /* |
| 884 | * Read the data buffer from the NAND Flash. To read the data from NAND |
| 885 | * Flash first the data output cycle is initiated by the NFC, which copies |
| 886 | * the data to RAMbuffer. This data of length len is then copied to buffer buf. |
| 887 | */ |
| 888 | static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 889 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 890 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
| 891 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 892 | int n, col, i = 0; |
| 893 | |
Masahiro Yamada | f8a5d51 | 2017-10-18 00:10:48 +0900 | [diff] [blame] | 894 | pr_debug("mxc_nand_read_buf(col = %d, len = %d)\n", host->col_addr, |
| 895 | len); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 896 | |
| 897 | col = host->col_addr; |
| 898 | |
| 899 | /* Adjust saved column address */ |
| 900 | if (col < mtd->writesize && host->spare_only) |
| 901 | col += mtd->writesize; |
| 902 | |
| 903 | n = mtd->writesize + mtd->oobsize - col; |
| 904 | n = min(len, n); |
| 905 | |
| 906 | while (n > 0) { |
| 907 | void __iomem *p; |
| 908 | |
| 909 | if (col < mtd->writesize) { |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 910 | p = host->regs->main_area[0] + (col & ~3); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 911 | } else { |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 912 | p = host->regs->spare_area[0] - |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 913 | mtd->writesize + (col & ~3); |
| 914 | } |
| 915 | |
| 916 | if (((col | (int)&buf[i]) & 3) || n < 4) { |
| 917 | union { |
| 918 | uint32_t word; |
| 919 | uint8_t bytes[4]; |
| 920 | } nfc_word; |
| 921 | |
| 922 | nfc_word.word = readl(p); |
| 923 | buf[i++] = nfc_word.bytes[col & 3]; |
| 924 | n--; |
| 925 | col++; |
| 926 | } else { |
| 927 | int m = mtd->writesize - col; |
| 928 | |
| 929 | if (col >= mtd->writesize) |
| 930 | m += mtd->oobsize; |
| 931 | |
| 932 | m = min(n, m) & ~3; |
| 933 | mxc_nand_memcpy32((uint32_t *)&buf[i], p, m); |
| 934 | |
| 935 | col += m; |
| 936 | i += m; |
| 937 | n -= m; |
| 938 | } |
| 939 | } |
| 940 | /* Update saved column address */ |
| 941 | host->col_addr = col; |
| 942 | } |
| 943 | |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 944 | /* |
| 945 | * This function is used by upper layer for select and |
| 946 | * deselect of the NAND chip |
| 947 | */ |
| 948 | static void mxc_nand_select_chip(struct mtd_info *mtd, int chip) |
| 949 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 950 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
| 951 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 952 | |
| 953 | switch (chip) { |
| 954 | case -1: |
| 955 | /* TODO: Disable the NFC clock */ |
| 956 | if (host->clk_act) |
| 957 | host->clk_act = 0; |
| 958 | break; |
| 959 | case 0: |
| 960 | /* TODO: Enable the NFC clock */ |
| 961 | if (!host->clk_act) |
| 962 | host->clk_act = 1; |
| 963 | break; |
| 964 | |
| 965 | default: |
| 966 | break; |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | /* |
| 971 | * Used by the upper layer to write command to NAND Flash for |
| 972 | * different operations to be carried out on NAND Flash |
| 973 | */ |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 974 | void mxc_nand_command(struct mtd_info *mtd, unsigned command, |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 975 | int column, int page_addr) |
| 976 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 977 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
| 978 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 979 | |
Masahiro Yamada | f8a5d51 | 2017-10-18 00:10:48 +0900 | [diff] [blame] | 980 | pr_debug("mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n", |
| 981 | command, column, page_addr); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 982 | |
| 983 | /* Reset command state information */ |
| 984 | host->status_request = false; |
| 985 | |
| 986 | /* Command pre-processing step */ |
| 987 | switch (command) { |
| 988 | |
| 989 | case NAND_CMD_STATUS: |
| 990 | host->col_addr = 0; |
| 991 | host->status_request = true; |
| 992 | break; |
| 993 | |
| 994 | case NAND_CMD_READ0: |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 995 | host->page_addr = page_addr; |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 996 | host->col_addr = column; |
| 997 | host->spare_only = false; |
| 998 | break; |
| 999 | |
| 1000 | case NAND_CMD_READOOB: |
| 1001 | host->col_addr = column; |
| 1002 | host->spare_only = true; |
| 1003 | if (host->pagesize_2k) |
| 1004 | command = NAND_CMD_READ0; /* only READ0 is valid */ |
| 1005 | break; |
| 1006 | |
| 1007 | case NAND_CMD_SEQIN: |
| 1008 | if (column >= mtd->writesize) { |
| 1009 | /* |
| 1010 | * before sending SEQIN command for partial write, |
| 1011 | * we need read one page out. FSL NFC does not support |
Helmut Raiger | c8f4a4c | 2011-07-06 09:40:28 +0200 | [diff] [blame] | 1012 | * partial write. It always sends out 512+ecc+512+ecc |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1013 | * for large page nand flash. But for small page nand |
| 1014 | * flash, it does support SPARE ONLY operation. |
| 1015 | */ |
| 1016 | if (host->pagesize_2k) { |
| 1017 | /* call ourself to read a page */ |
| 1018 | mxc_nand_command(mtd, NAND_CMD_READ0, 0, |
| 1019 | page_addr); |
| 1020 | } |
| 1021 | |
| 1022 | host->col_addr = column - mtd->writesize; |
| 1023 | host->spare_only = true; |
| 1024 | |
| 1025 | /* Set program pointer to spare region */ |
| 1026 | if (!host->pagesize_2k) |
| 1027 | send_cmd(host, NAND_CMD_READOOB); |
| 1028 | } else { |
| 1029 | host->spare_only = false; |
| 1030 | host->col_addr = column; |
| 1031 | |
| 1032 | /* Set program pointer to page start */ |
| 1033 | if (!host->pagesize_2k) |
| 1034 | send_cmd(host, NAND_CMD_READ0); |
| 1035 | } |
| 1036 | break; |
| 1037 | |
| 1038 | case NAND_CMD_PAGEPROG: |
| 1039 | send_prog_page(host, 0, host->spare_only); |
| 1040 | |
Benoît Thébaudeau | c5b4eb1 | 2012-08-13 22:50:53 +0200 | [diff] [blame] | 1041 | if (host->pagesize_2k && is_mxc_nfc_1()) { |
Helmut Raiger | c8f4a4c | 2011-07-06 09:40:28 +0200 | [diff] [blame] | 1042 | /* data in 4 areas */ |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1043 | send_prog_page(host, 1, host->spare_only); |
| 1044 | send_prog_page(host, 2, host->spare_only); |
| 1045 | send_prog_page(host, 3, host->spare_only); |
| 1046 | } |
| 1047 | |
| 1048 | break; |
| 1049 | } |
| 1050 | |
| 1051 | /* Write out the command to the device. */ |
| 1052 | send_cmd(host, command); |
| 1053 | |
| 1054 | /* Write out column address, if necessary */ |
| 1055 | if (column != -1) { |
| 1056 | /* |
| 1057 | * MXC NANDFC can only perform full page+spare or |
Helmut Raiger | c8f4a4c | 2011-07-06 09:40:28 +0200 | [diff] [blame] | 1058 | * spare-only read/write. When the upper layers perform |
| 1059 | * a read/write buffer operation, we will use the saved |
| 1060 | * column address to index into the full page. |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1061 | */ |
| 1062 | send_addr(host, 0); |
| 1063 | if (host->pagesize_2k) |
| 1064 | /* another col addr cycle for 2k page */ |
| 1065 | send_addr(host, 0); |
| 1066 | } |
| 1067 | |
| 1068 | /* Write out page address, if necessary */ |
| 1069 | if (page_addr != -1) { |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 1070 | u32 page_mask = nand_chip->pagemask; |
| 1071 | do { |
| 1072 | send_addr(host, page_addr & 0xFF); |
| 1073 | page_addr >>= 8; |
| 1074 | page_mask >>= 8; |
| 1075 | } while (page_mask); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | /* Command post-processing step */ |
| 1079 | switch (command) { |
| 1080 | |
| 1081 | case NAND_CMD_RESET: |
| 1082 | break; |
| 1083 | |
| 1084 | case NAND_CMD_READOOB: |
| 1085 | case NAND_CMD_READ0: |
| 1086 | if (host->pagesize_2k) { |
| 1087 | /* send read confirm command */ |
| 1088 | send_cmd(host, NAND_CMD_READSTART); |
| 1089 | /* read for each AREA */ |
| 1090 | send_read_page(host, 0, host->spare_only); |
Benoît Thébaudeau | c5b4eb1 | 2012-08-13 22:50:53 +0200 | [diff] [blame] | 1091 | if (is_mxc_nfc_1()) { |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 1092 | send_read_page(host, 1, host->spare_only); |
| 1093 | send_read_page(host, 2, host->spare_only); |
| 1094 | send_read_page(host, 3, host->spare_only); |
| 1095 | } |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1096 | } else { |
| 1097 | send_read_page(host, 0, host->spare_only); |
| 1098 | } |
| 1099 | break; |
| 1100 | |
| 1101 | case NAND_CMD_READID: |
| 1102 | host->col_addr = 0; |
| 1103 | send_read_id(host); |
| 1104 | break; |
| 1105 | |
| 1106 | case NAND_CMD_PAGEPROG: |
| 1107 | break; |
| 1108 | |
| 1109 | case NAND_CMD_STATUS: |
| 1110 | break; |
| 1111 | |
| 1112 | case NAND_CMD_ERASE2: |
| 1113 | break; |
| 1114 | } |
| 1115 | } |
| 1116 | |
Timo Ketola | ad85f95 | 2012-04-18 22:55:31 +0000 | [diff] [blame] | 1117 | #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT |
| 1118 | |
| 1119 | static u8 bbt_pattern[] = {'B', 'b', 't', '0' }; |
| 1120 | static u8 mirror_pattern[] = {'1', 't', 'b', 'B' }; |
| 1121 | |
| 1122 | static struct nand_bbt_descr bbt_main_descr = { |
| 1123 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE | |
| 1124 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, |
| 1125 | .offs = 0, |
| 1126 | .len = 4, |
| 1127 | .veroffs = 4, |
| 1128 | .maxblocks = 4, |
| 1129 | .pattern = bbt_pattern, |
| 1130 | }; |
| 1131 | |
| 1132 | static struct nand_bbt_descr bbt_mirror_descr = { |
| 1133 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE | |
| 1134 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, |
| 1135 | .offs = 0, |
| 1136 | .len = 4, |
| 1137 | .veroffs = 4, |
| 1138 | .maxblocks = 4, |
| 1139 | .pattern = mirror_pattern, |
| 1140 | }; |
| 1141 | |
| 1142 | #endif |
| 1143 | |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1144 | int board_nand_init(struct nand_chip *this) |
| 1145 | { |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1146 | struct mtd_info *mtd; |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 1147 | #if defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2) |
| 1148 | uint32_t tmp; |
Tom Rini | 05ae4c9 | 2012-09-18 09:24:22 -0700 | [diff] [blame] | 1149 | #endif |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1150 | |
Timo Ketola | ad85f95 | 2012-04-18 22:55:31 +0000 | [diff] [blame] | 1151 | #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1152 | this->bbt_options |= NAND_BBT_USE_FLASH; |
Timo Ketola | ad85f95 | 2012-04-18 22:55:31 +0000 | [diff] [blame] | 1153 | this->bbt_td = &bbt_main_descr; |
| 1154 | this->bbt_md = &bbt_mirror_descr; |
| 1155 | #endif |
| 1156 | |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1157 | /* structures must be linked */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 1158 | mtd = &this->mtd; |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1159 | host->nand = this; |
| 1160 | |
| 1161 | /* 5 us command delay time */ |
| 1162 | this->chip_delay = 5; |
| 1163 | |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1164 | nand_set_controller_data(this, host); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1165 | this->dev_ready = mxc_nand_dev_ready; |
| 1166 | this->cmdfunc = mxc_nand_command; |
| 1167 | this->select_chip = mxc_nand_select_chip; |
| 1168 | this->read_byte = mxc_nand_read_byte; |
| 1169 | this->read_word = mxc_nand_read_word; |
| 1170 | this->write_buf = mxc_nand_write_buf; |
| 1171 | this->read_buf = mxc_nand_read_buf; |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1172 | |
Benoît Thébaudeau | efb7c00 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 1173 | host->regs = (struct mxc_nand_regs __iomem *)CONFIG_MXC_NAND_REGS_BASE; |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 1174 | #ifdef MXC_NFC_V3_2 |
| 1175 | host->ip_regs = |
Benoît Thébaudeau | efb7c00 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 1176 | (struct mxc_nand_ip_regs __iomem *)CONFIG_MXC_NAND_IP_REGS_BASE; |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 1177 | #endif |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1178 | host->clk_act = 1; |
| 1179 | |
| 1180 | #ifdef CONFIG_MXC_NAND_HWECC |
| 1181 | this->ecc.calculate = mxc_nand_calculate_ecc; |
| 1182 | this->ecc.hwctl = mxc_nand_enable_hwecc; |
| 1183 | this->ecc.correct = mxc_nand_correct_data; |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 1184 | if (is_mxc_nfc_21() || is_mxc_nfc_32()) { |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 1185 | this->ecc.mode = NAND_ECC_HW_SYNDROME; |
| 1186 | this->ecc.read_page = mxc_nand_read_page_syndrome; |
| 1187 | this->ecc.read_page_raw = mxc_nand_read_page_raw_syndrome; |
| 1188 | this->ecc.read_oob = mxc_nand_read_oob_syndrome; |
| 1189 | this->ecc.write_page = mxc_nand_write_page_syndrome; |
| 1190 | this->ecc.write_page_raw = mxc_nand_write_page_raw_syndrome; |
| 1191 | this->ecc.write_oob = mxc_nand_write_oob_syndrome; |
| 1192 | this->ecc.bytes = 9; |
| 1193 | this->ecc.prepad = 7; |
| 1194 | } else { |
| 1195 | this->ecc.mode = NAND_ECC_HW; |
| 1196 | } |
| 1197 | |
Marek Vasut | 499aab9 | 2013-07-03 02:34:34 +0200 | [diff] [blame] | 1198 | if (is_mxc_nfc_1()) |
| 1199 | this->ecc.strength = 1; |
| 1200 | else |
| 1201 | this->ecc.strength = 4; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1202 | |
John Rigby | 3c285c7 | 2010-01-26 19:24:18 -0700 | [diff] [blame] | 1203 | host->pagesize_2k = 0; |
| 1204 | |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1205 | this->ecc.size = 512; |
Benoît Thébaudeau | 9166d5f | 2012-08-13 22:50:07 +0200 | [diff] [blame] | 1206 | _mxc_nand_enable_hwecc(mtd, 1); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1207 | #else |
| 1208 | this->ecc.layout = &nand_soft_eccoob; |
| 1209 | this->ecc.mode = NAND_ECC_SOFT; |
Benoît Thébaudeau | 9166d5f | 2012-08-13 22:50:07 +0200 | [diff] [blame] | 1210 | _mxc_nand_enable_hwecc(mtd, 0); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1211 | #endif |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1212 | /* Reset NAND */ |
| 1213 | this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
| 1214 | |
Benoît Thébaudeau | 0452dcd | 2012-08-13 22:50:30 +0200 | [diff] [blame] | 1215 | /* NAND bus width determines access functions used by upper layer */ |
| 1216 | if (is_16bit_nand()) |
| 1217 | this->options |= NAND_BUSWIDTH_16; |
| 1218 | |
| 1219 | #ifdef CONFIG_SYS_NAND_LARGEPAGE |
| 1220 | host->pagesize_2k = 1; |
| 1221 | this->ecc.layout = &nand_hw_eccoob2k; |
| 1222 | #else |
| 1223 | host->pagesize_2k = 0; |
| 1224 | this->ecc.layout = &nand_hw_eccoob; |
| 1225 | #endif |
| 1226 | |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 1227 | #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1) |
Benoît Thébaudeau | c5b4eb1 | 2012-08-13 22:50:53 +0200 | [diff] [blame] | 1228 | #ifdef MXC_NFC_V2_1 |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 1229 | tmp = readnfc(&host->regs->config1); |
| 1230 | tmp |= NFC_V2_CONFIG1_ONE_CYCLE; |
| 1231 | tmp |= NFC_V2_CONFIG1_ECC_MODE_4; |
| 1232 | writenfc(tmp, &host->regs->config1); |
Benoît Thébaudeau | 0452dcd | 2012-08-13 22:50:30 +0200 | [diff] [blame] | 1233 | if (host->pagesize_2k) |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 1234 | writenfc(64/2, &host->regs->spare_area_size); |
Benoît Thébaudeau | 0452dcd | 2012-08-13 22:50:30 +0200 | [diff] [blame] | 1235 | else |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 1236 | writenfc(16/2, &host->regs->spare_area_size); |
Benoît Thébaudeau | 0452dcd | 2012-08-13 22:50:30 +0200 | [diff] [blame] | 1237 | #endif |
| 1238 | |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1239 | /* |
| 1240 | * preset operation |
| 1241 | * Unlock the internal RAM Buffer |
| 1242 | */ |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 1243 | writenfc(0x2, &host->regs->config); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1244 | |
| 1245 | /* Blocks to be unlocked */ |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 1246 | writenfc(0x0, &host->regs->unlockstart_blkaddr); |
Helmut Raiger | 38ba324 | 2011-07-06 19:04:41 +0200 | [diff] [blame] | 1247 | /* Originally (Freescale LTIB 2.6.21) 0x4000 was written to the |
| 1248 | * unlockend_blkaddr, but the magic 0x4000 does not always work |
| 1249 | * when writing more than some 32 megabytes (on 2k page nands) |
| 1250 | * However 0xFFFF doesn't seem to have this kind |
| 1251 | * of limitation (tried it back and forth several times). |
| 1252 | * The linux kernel driver sets this to 0xFFFF for the v2 controller |
| 1253 | * only, but probably this was not tested there for v1. |
| 1254 | * The very same limitation seems to apply to this kernel driver. |
| 1255 | * This might be NAND chip specific and the i.MX31 datasheet is |
| 1256 | * extremely vague about the semantics of this register. |
| 1257 | */ |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 1258 | writenfc(0xFFFF, &host->regs->unlockend_blkaddr); |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1259 | |
| 1260 | /* Unlock Block Command for given address range */ |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 1261 | writenfc(0x4, &host->regs->wrprot); |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 1262 | #elif defined(MXC_NFC_V3_2) |
| 1263 | writenfc(NFC_V3_CONFIG1_RBA(0), &host->regs->config1); |
| 1264 | writenfc(NFC_V3_IPC_CREQ, &host->ip_regs->ipc); |
| 1265 | |
| 1266 | /* Unlock the internal RAM Buffer */ |
| 1267 | writenfc(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK, |
| 1268 | &host->ip_regs->wrprot); |
| 1269 | |
| 1270 | /* Blocks to be unlocked */ |
| 1271 | for (tmp = 0; tmp < CONFIG_SYS_NAND_MAX_CHIPS; tmp++) |
| 1272 | writenfc(0x0 | 0xFFFF << 16, |
| 1273 | &host->ip_regs->wrprot_unlock_blkaddr[tmp]); |
| 1274 | |
| 1275 | writenfc(0, &host->ip_regs->ipc); |
| 1276 | |
| 1277 | tmp = readnfc(&host->ip_regs->config2); |
| 1278 | tmp &= ~(NFC_V3_CONFIG2_SPAS_MASK | NFC_V3_CONFIG2_EDC_MASK | |
| 1279 | NFC_V3_CONFIG2_ECC_MODE_8 | NFC_V3_CONFIG2_PS_MASK); |
| 1280 | tmp |= NFC_V3_CONFIG2_ONE_CYCLE; |
| 1281 | |
| 1282 | if (host->pagesize_2k) { |
| 1283 | tmp |= NFC_V3_CONFIG2_SPAS(64/2); |
| 1284 | tmp |= NFC_V3_CONFIG2_PS_2048; |
| 1285 | } else { |
| 1286 | tmp |= NFC_V3_CONFIG2_SPAS(16/2); |
| 1287 | tmp |= NFC_V3_CONFIG2_PS_512; |
| 1288 | } |
| 1289 | |
| 1290 | writenfc(tmp, &host->ip_regs->config2); |
| 1291 | |
| 1292 | tmp = NFC_V3_CONFIG3_NUM_OF_DEVS(0) | |
| 1293 | NFC_V3_CONFIG3_NO_SDMA | |
| 1294 | NFC_V3_CONFIG3_RBB_MODE | |
| 1295 | NFC_V3_CONFIG3_SBB(6) | /* Reset default */ |
| 1296 | NFC_V3_CONFIG3_ADD_OP(0); |
| 1297 | |
| 1298 | if (!(this->options & NAND_BUSWIDTH_16)) |
| 1299 | tmp |= NFC_V3_CONFIG3_FW8; |
| 1300 | |
| 1301 | writenfc(tmp, &host->ip_regs->config3); |
| 1302 | |
| 1303 | writenfc(0, &host->ip_regs->delay_line); |
| 1304 | #endif |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1305 | |
Benoît Thébaudeau | 32ae5b4 | 2012-08-13 22:48:26 +0200 | [diff] [blame] | 1306 | return 0; |
Ilya Yanok | f9e2bee | 2009-08-11 02:32:54 +0400 | [diff] [blame] | 1307 | } |