Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved. |
| 3 | * Copyright 2008 Sascha Hauer, kernel@pengutronix.de |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 17 | * MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/mtd/mtd.h> |
| 25 | #include <linux/mtd/nand.h> |
| 26 | #include <linux/mtd/partitions.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/device.h> |
| 29 | #include <linux/platform_device.h> |
| 30 | #include <linux/clk.h> |
| 31 | #include <linux/err.h> |
| 32 | #include <linux/io.h> |
| 33 | |
| 34 | #include <asm/mach/flash.h> |
| 35 | #include <mach/mxc_nand.h> |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 36 | #include <mach/hardware.h> |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 37 | |
| 38 | #define DRIVER_NAME "mxc_nand" |
| 39 | |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 40 | #define nfc_is_v21() (cpu_is_mx25() || cpu_is_mx35()) |
Ivo Clarysse | a47bfd2 | 2010-04-08 16:16:51 +0200 | [diff] [blame] | 41 | #define nfc_is_v1() (cpu_is_mx31() || cpu_is_mx27() || cpu_is_mx21()) |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 42 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 43 | /* Addresses for NFC registers */ |
Sascha Hauer | 938cf99 | 2010-08-06 15:53:04 +0200 | [diff] [blame] | 44 | #define NFC_BUF_SIZE 0x00 |
| 45 | #define NFC_BUF_ADDR 0x04 |
| 46 | #define NFC_FLASH_ADDR 0x06 |
| 47 | #define NFC_FLASH_CMD 0x08 |
| 48 | #define NFC_CONFIG 0x0a |
| 49 | #define NFC_ECC_STATUS_RESULT 0x0c |
| 50 | #define NFC_RSLTMAIN_AREA 0x0e |
| 51 | #define NFC_RSLTSPARE_AREA 0x10 |
| 52 | #define NFC_WRPROT 0x12 |
| 53 | #define NFC_V1_UNLOCKSTART_BLKADDR 0x14 |
| 54 | #define NFC_V1_UNLOCKEND_BLKADDR 0x16 |
| 55 | #define NFC_V21_UNLOCKSTART_BLKADDR 0x20 |
| 56 | #define NFC_V21_UNLOCKEND_BLKADDR 0x22 |
| 57 | #define NFC_NF_WRPRST 0x18 |
| 58 | #define NFC_CONFIG1 0x1a |
| 59 | #define NFC_CONFIG2 0x1c |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 60 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 61 | /* Set INT to 0, FCMD to 1, rest to 0 in NFC_CONFIG2 Register |
| 62 | * for Command operation */ |
| 63 | #define NFC_CMD 0x1 |
| 64 | |
| 65 | /* Set INT to 0, FADD to 1, rest to 0 in NFC_CONFIG2 Register |
| 66 | * for Address operation */ |
| 67 | #define NFC_ADDR 0x2 |
| 68 | |
| 69 | /* Set INT to 0, FDI to 1, rest to 0 in NFC_CONFIG2 Register |
| 70 | * for Input operation */ |
| 71 | #define NFC_INPUT 0x4 |
| 72 | |
| 73 | /* Set INT to 0, FDO to 001, rest to 0 in NFC_CONFIG2 Register |
| 74 | * for Data Output operation */ |
| 75 | #define NFC_OUTPUT 0x8 |
| 76 | |
| 77 | /* Set INT to 0, FD0 to 010, rest to 0 in NFC_CONFIG2 Register |
| 78 | * for Read ID operation */ |
| 79 | #define NFC_ID 0x10 |
| 80 | |
| 81 | /* Set INT to 0, FDO to 100, rest to 0 in NFC_CONFIG2 Register |
| 82 | * for Read Status operation */ |
| 83 | #define NFC_STATUS 0x20 |
| 84 | |
| 85 | /* Set INT to 1, rest to 0 in NFC_CONFIG2 Register for Read |
| 86 | * Status operation */ |
| 87 | #define NFC_INT 0x8000 |
| 88 | |
| 89 | #define NFC_SP_EN (1 << 2) |
| 90 | #define NFC_ECC_EN (1 << 3) |
| 91 | #define NFC_INT_MSK (1 << 4) |
| 92 | #define NFC_BIG (1 << 5) |
| 93 | #define NFC_RST (1 << 6) |
| 94 | #define NFC_CE (1 << 7) |
| 95 | #define NFC_ONE_CYCLE (1 << 8) |
| 96 | |
| 97 | struct mxc_nand_host { |
| 98 | struct mtd_info mtd; |
| 99 | struct nand_chip nand; |
| 100 | struct mtd_partition *parts; |
| 101 | struct device *dev; |
| 102 | |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 103 | void *spare0; |
| 104 | void *main_area0; |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 105 | |
| 106 | void __iomem *base; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 107 | void __iomem *regs; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 108 | int status_request; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 109 | struct clk *clk; |
| 110 | int clk_act; |
| 111 | int irq; |
| 112 | |
| 113 | wait_queue_head_t irq_waitq; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 114 | |
| 115 | uint8_t *data_buf; |
| 116 | unsigned int buf_start; |
| 117 | int spare_len; |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 118 | |
| 119 | void (*preset)(struct mtd_info *); |
| 120 | void (*send_cmd)(struct mxc_nand_host *, uint16_t, int); |
| 121 | void (*send_addr)(struct mxc_nand_host *, uint16_t, int); |
| 122 | void (*send_page)(struct mtd_info *, unsigned int); |
| 123 | void (*send_read_id)(struct mxc_nand_host *); |
| 124 | uint16_t (*get_dev_status)(struct mxc_nand_host *); |
Sascha Hauer | 7aaf28a | 2010-08-06 15:53:07 +0200 | [diff] [blame^] | 125 | int (*check_int)(struct mxc_nand_host *); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 126 | }; |
| 127 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 128 | /* OOB placement block for use with hardware ecc generation */ |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 129 | static struct nand_ecclayout nandv1_hw_eccoob_smallpage = { |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 130 | .eccbytes = 5, |
| 131 | .eccpos = {6, 7, 8, 9, 10}, |
Sascha Hauer | 8c1fd89 | 2009-10-21 10:22:01 +0200 | [diff] [blame] | 132 | .oobfree = {{0, 5}, {12, 4}, } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 133 | }; |
| 134 | |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 135 | static struct nand_ecclayout nandv1_hw_eccoob_largepage = { |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 136 | .eccbytes = 20, |
| 137 | .eccpos = {6, 7, 8, 9, 10, 22, 23, 24, 25, 26, |
| 138 | 38, 39, 40, 41, 42, 54, 55, 56, 57, 58}, |
| 139 | .oobfree = {{2, 4}, {11, 10}, {27, 10}, {43, 10}, {59, 5}, } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 140 | }; |
| 141 | |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 142 | /* OOB description for 512 byte pages with 16 byte OOB */ |
| 143 | static struct nand_ecclayout nandv2_hw_eccoob_smallpage = { |
| 144 | .eccbytes = 1 * 9, |
| 145 | .eccpos = { |
| 146 | 7, 8, 9, 10, 11, 12, 13, 14, 15 |
| 147 | }, |
| 148 | .oobfree = { |
| 149 | {.offset = 0, .length = 5} |
| 150 | } |
| 151 | }; |
| 152 | |
| 153 | /* OOB description for 2048 byte pages with 64 byte OOB */ |
| 154 | static struct nand_ecclayout nandv2_hw_eccoob_largepage = { |
| 155 | .eccbytes = 4 * 9, |
| 156 | .eccpos = { |
| 157 | 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 158 | 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 159 | 39, 40, 41, 42, 43, 44, 45, 46, 47, |
| 160 | 55, 56, 57, 58, 59, 60, 61, 62, 63 |
| 161 | }, |
| 162 | .oobfree = { |
| 163 | {.offset = 2, .length = 4}, |
| 164 | {.offset = 16, .length = 7}, |
| 165 | {.offset = 32, .length = 7}, |
| 166 | {.offset = 48, .length = 7} |
| 167 | } |
| 168 | }; |
| 169 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 170 | #ifdef CONFIG_MTD_PARTITIONS |
| 171 | static const char *part_probes[] = { "RedBoot", "cmdlinepart", NULL }; |
| 172 | #endif |
| 173 | |
| 174 | static irqreturn_t mxc_nfc_irq(int irq, void *dev_id) |
| 175 | { |
| 176 | struct mxc_nand_host *host = dev_id; |
| 177 | |
Ivo Clarysse | a47bfd2 | 2010-04-08 16:16:51 +0200 | [diff] [blame] | 178 | disable_irq_nosync(irq); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 179 | |
| 180 | wake_up(&host->irq_waitq); |
| 181 | |
| 182 | return IRQ_HANDLED; |
| 183 | } |
| 184 | |
Sascha Hauer | 7aaf28a | 2010-08-06 15:53:07 +0200 | [diff] [blame^] | 185 | static int check_int_v1_v2(struct mxc_nand_host *host) |
| 186 | { |
| 187 | uint32_t tmp; |
| 188 | |
| 189 | tmp = readw(host->regs + NFC_CONFIG2); |
| 190 | if (!(tmp & NFC_INT)) |
| 191 | return 0; |
| 192 | |
| 193 | writew(tmp & ~NFC_INT, NFC_CONFIG2); |
| 194 | |
| 195 | return 1; |
| 196 | } |
| 197 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 198 | /* This function polls the NANDFC to wait for the basic operation to |
| 199 | * complete by checking the INT bit of config2 register. |
| 200 | */ |
Sascha Hauer | c110eaf | 2009-10-21 16:01:02 +0200 | [diff] [blame] | 201 | static void wait_op_done(struct mxc_nand_host *host, int useirq) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 202 | { |
Ivo Clarysse | a47bfd2 | 2010-04-08 16:16:51 +0200 | [diff] [blame] | 203 | int max_retries = 8000; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 204 | |
| 205 | if (useirq) { |
Sascha Hauer | 7aaf28a | 2010-08-06 15:53:07 +0200 | [diff] [blame^] | 206 | if (!host->check_int(host)) { |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 207 | |
Ivo Clarysse | a47bfd2 | 2010-04-08 16:16:51 +0200 | [diff] [blame] | 208 | enable_irq(host->irq); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 209 | |
Sascha Hauer | 7aaf28a | 2010-08-06 15:53:07 +0200 | [diff] [blame^] | 210 | wait_event(host->irq_waitq, host->check_int(host)); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 211 | } |
| 212 | } else { |
| 213 | while (max_retries-- > 0) { |
Sascha Hauer | 7aaf28a | 2010-08-06 15:53:07 +0200 | [diff] [blame^] | 214 | if (host->check_int(host)) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 215 | break; |
Sascha Hauer | 7aaf28a | 2010-08-06 15:53:07 +0200 | [diff] [blame^] | 216 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 217 | udelay(1); |
| 218 | } |
Roel Kluin | 43950a6 | 2009-06-04 16:24:59 +0200 | [diff] [blame] | 219 | if (max_retries < 0) |
Sascha Hauer | 6246549 | 2009-06-04 15:57:20 +0200 | [diff] [blame] | 220 | DEBUG(MTD_DEBUG_LEVEL0, "%s: INT not set\n", |
| 221 | __func__); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | /* This function issues the specified command to the NAND device and |
| 226 | * waits for completion. */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 227 | static void send_cmd_v1_v2(struct mxc_nand_host *host, uint16_t cmd, int useirq) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 228 | { |
| 229 | DEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x, %d)\n", cmd, useirq); |
| 230 | |
| 231 | writew(cmd, host->regs + NFC_FLASH_CMD); |
| 232 | writew(NFC_CMD, host->regs + NFC_CONFIG2); |
| 233 | |
Ivo Clarysse | a47bfd2 | 2010-04-08 16:16:51 +0200 | [diff] [blame] | 234 | if (cpu_is_mx21() && (cmd == NAND_CMD_RESET)) { |
| 235 | int max_retries = 100; |
| 236 | /* Reset completion is indicated by NFC_CONFIG2 */ |
| 237 | /* being set to 0 */ |
| 238 | while (max_retries-- > 0) { |
| 239 | if (readw(host->regs + NFC_CONFIG2) == 0) { |
| 240 | break; |
| 241 | } |
| 242 | udelay(1); |
| 243 | } |
| 244 | if (max_retries < 0) |
| 245 | DEBUG(MTD_DEBUG_LEVEL0, "%s: RESET failed\n", |
| 246 | __func__); |
| 247 | } else { |
| 248 | /* Wait for operation to complete */ |
| 249 | wait_op_done(host, useirq); |
| 250 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* This function sends an address (or partial address) to the |
| 254 | * NAND device. The address is used to select the source/destination for |
| 255 | * a NAND command. */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 256 | static void send_addr_v1_v2(struct mxc_nand_host *host, uint16_t addr, int islast) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 257 | { |
| 258 | DEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x %d)\n", addr, islast); |
| 259 | |
| 260 | writew(addr, host->regs + NFC_FLASH_ADDR); |
| 261 | writew(NFC_ADDR, host->regs + NFC_CONFIG2); |
| 262 | |
| 263 | /* Wait for operation to complete */ |
Sascha Hauer | c110eaf | 2009-10-21 16:01:02 +0200 | [diff] [blame] | 264 | wait_op_done(host, islast); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 265 | } |
| 266 | |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 267 | static void send_page_v1_v2(struct mtd_info *mtd, unsigned int ops) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 268 | { |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame] | 269 | struct nand_chip *nand_chip = mtd->priv; |
| 270 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 271 | int bufs, i; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 272 | |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 273 | if (nfc_is_v1() && mtd->writesize > 512) |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 274 | bufs = 4; |
| 275 | else |
| 276 | bufs = 1; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 277 | |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 278 | for (i = 0; i < bufs; i++) { |
| 279 | |
| 280 | /* NANDFC buffer 0 is used for page read/write */ |
| 281 | writew(i, host->regs + NFC_BUF_ADDR); |
| 282 | |
| 283 | writew(ops, host->regs + NFC_CONFIG2); |
| 284 | |
| 285 | /* Wait for operation to complete */ |
Sascha Hauer | c110eaf | 2009-10-21 16:01:02 +0200 | [diff] [blame] | 286 | wait_op_done(host, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 287 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /* Request the NANDFC to perform a read of the NAND device ID. */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 291 | static void send_read_id_v1_v2(struct mxc_nand_host *host) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 292 | { |
| 293 | struct nand_chip *this = &host->nand; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 294 | |
| 295 | /* NANDFC buffer 0 is used for device ID output */ |
| 296 | writew(0x0, host->regs + NFC_BUF_ADDR); |
| 297 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 298 | writew(NFC_ID, host->regs + NFC_CONFIG2); |
| 299 | |
| 300 | /* Wait for operation to complete */ |
Sascha Hauer | c110eaf | 2009-10-21 16:01:02 +0200 | [diff] [blame] | 301 | wait_op_done(host, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 302 | |
| 303 | if (this->options & NAND_BUSWIDTH_16) { |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 304 | void __iomem *main_buf = host->main_area0; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 305 | /* compress the ID info */ |
| 306 | writeb(readb(main_buf + 2), main_buf + 1); |
| 307 | writeb(readb(main_buf + 4), main_buf + 2); |
| 308 | writeb(readb(main_buf + 6), main_buf + 3); |
| 309 | writeb(readb(main_buf + 8), main_buf + 4); |
| 310 | writeb(readb(main_buf + 10), main_buf + 5); |
| 311 | } |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 312 | memcpy(host->data_buf, host->main_area0, 16); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /* This function requests the NANDFC to perform a read of the |
| 316 | * NAND device status and returns the current status. */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 317 | static uint16_t get_dev_status_v1_v2(struct mxc_nand_host *host) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 318 | { |
Sascha Hauer | c29c607 | 2010-08-06 15:53:05 +0200 | [diff] [blame] | 319 | void __iomem *main_buf = host->main_area0; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 320 | uint32_t store; |
Sascha Hauer | f06368f | 2009-10-05 17:18:42 +0200 | [diff] [blame] | 321 | uint16_t ret; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 322 | |
Sascha Hauer | c29c607 | 2010-08-06 15:53:05 +0200 | [diff] [blame] | 323 | writew(0x0, NFC_V1_V2_BUF_ADDR); |
| 324 | |
| 325 | /* |
| 326 | * The device status is stored in main_area0. To |
| 327 | * prevent corruption of the buffer save the value |
| 328 | * and restore it afterwards. |
| 329 | */ |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 330 | store = readl(main_buf); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 331 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 332 | writew(NFC_STATUS, host->regs + NFC_CONFIG2); |
Sascha Hauer | c110eaf | 2009-10-21 16:01:02 +0200 | [diff] [blame] | 333 | wait_op_done(host, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 334 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 335 | ret = readw(main_buf); |
Sascha Hauer | c29c607 | 2010-08-06 15:53:05 +0200 | [diff] [blame] | 336 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 337 | writel(store, main_buf); |
| 338 | |
| 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | /* This functions is used by upper layer to checks if device is ready */ |
| 343 | static int mxc_nand_dev_ready(struct mtd_info *mtd) |
| 344 | { |
| 345 | /* |
| 346 | * NFC handles R/B internally. Therefore, this function |
| 347 | * always returns status as ready. |
| 348 | */ |
| 349 | return 1; |
| 350 | } |
| 351 | |
| 352 | static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode) |
| 353 | { |
| 354 | /* |
| 355 | * If HW ECC is enabled, we turn it on during init. There is |
| 356 | * no need to enable again here. |
| 357 | */ |
| 358 | } |
| 359 | |
| 360 | static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat, |
| 361 | u_char *read_ecc, u_char *calc_ecc) |
| 362 | { |
| 363 | struct nand_chip *nand_chip = mtd->priv; |
| 364 | struct mxc_nand_host *host = nand_chip->priv; |
| 365 | |
| 366 | /* |
| 367 | * 1-Bit errors are automatically corrected in HW. No need for |
| 368 | * additional correction. 2-Bit errors cannot be corrected by |
| 369 | * HW ECC, so we need to return failure |
| 370 | */ |
| 371 | uint16_t ecc_status = readw(host->regs + NFC_ECC_STATUS_RESULT); |
| 372 | |
| 373 | if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) { |
| 374 | DEBUG(MTD_DEBUG_LEVEL0, |
| 375 | "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n"); |
| 376 | return -1; |
| 377 | } |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, |
| 383 | u_char *ecc_code) |
| 384 | { |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | static u_char mxc_nand_read_byte(struct mtd_info *mtd) |
| 389 | { |
| 390 | struct nand_chip *nand_chip = mtd->priv; |
| 391 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 392 | uint8_t ret; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 393 | |
| 394 | /* Check for status request */ |
| 395 | if (host->status_request) |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 396 | return host->get_dev_status(host) & 0xFF; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 397 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 398 | ret = *(uint8_t *)(host->data_buf + host->buf_start); |
| 399 | host->buf_start++; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 400 | |
| 401 | return ret; |
| 402 | } |
| 403 | |
| 404 | static uint16_t mxc_nand_read_word(struct mtd_info *mtd) |
| 405 | { |
| 406 | struct nand_chip *nand_chip = mtd->priv; |
| 407 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 408 | uint16_t ret; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 409 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 410 | ret = *(uint16_t *)(host->data_buf + host->buf_start); |
| 411 | host->buf_start += 2; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 412 | |
| 413 | return ret; |
| 414 | } |
| 415 | |
| 416 | /* Write data of length len to buffer buf. The data to be |
| 417 | * written on NAND Flash is first copied to RAMbuffer. After the Data Input |
| 418 | * Operation by the NFC, the data is written to NAND Flash */ |
| 419 | static void mxc_nand_write_buf(struct mtd_info *mtd, |
| 420 | const u_char *buf, int len) |
| 421 | { |
| 422 | struct nand_chip *nand_chip = mtd->priv; |
| 423 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 424 | u16 col = host->buf_start; |
| 425 | int n = mtd->oobsize + mtd->writesize - col; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 426 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 427 | n = min(n, len); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 428 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 429 | memcpy(host->data_buf + col, buf, n); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 430 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 431 | host->buf_start += n; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* Read the data buffer from the NAND Flash. To read the data from NAND |
| 435 | * Flash first the data output cycle is initiated by the NFC, which copies |
| 436 | * the data to RAMbuffer. This data of length len is then copied to buffer buf. |
| 437 | */ |
| 438 | static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 439 | { |
| 440 | struct nand_chip *nand_chip = mtd->priv; |
| 441 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 442 | u16 col = host->buf_start; |
| 443 | int n = mtd->oobsize + mtd->writesize - col; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 444 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 445 | n = min(n, len); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 446 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 447 | memcpy(buf, host->data_buf + col, len); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 448 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 449 | host->buf_start += len; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* Used by the upper layer to verify the data in NAND Flash |
| 453 | * with the data in the buf. */ |
| 454 | static int mxc_nand_verify_buf(struct mtd_info *mtd, |
| 455 | const u_char *buf, int len) |
| 456 | { |
| 457 | return -EFAULT; |
| 458 | } |
| 459 | |
| 460 | /* This function is used by upper layer for select and |
| 461 | * deselect of the NAND chip */ |
| 462 | static void mxc_nand_select_chip(struct mtd_info *mtd, int chip) |
| 463 | { |
| 464 | struct nand_chip *nand_chip = mtd->priv; |
| 465 | struct mxc_nand_host *host = nand_chip->priv; |
| 466 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 467 | switch (chip) { |
| 468 | case -1: |
| 469 | /* Disable the NFC clock */ |
| 470 | if (host->clk_act) { |
| 471 | clk_disable(host->clk); |
| 472 | host->clk_act = 0; |
| 473 | } |
| 474 | break; |
| 475 | case 0: |
| 476 | /* Enable the NFC clock */ |
| 477 | if (!host->clk_act) { |
| 478 | clk_enable(host->clk); |
| 479 | host->clk_act = 1; |
| 480 | } |
| 481 | break; |
| 482 | |
| 483 | default: |
| 484 | break; |
| 485 | } |
| 486 | } |
| 487 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 488 | /* |
| 489 | * Function to transfer data to/from spare area. |
| 490 | */ |
| 491 | static void copy_spare(struct mtd_info *mtd, bool bfrom) |
| 492 | { |
| 493 | struct nand_chip *this = mtd->priv; |
| 494 | struct mxc_nand_host *host = this->priv; |
| 495 | u16 i, j; |
| 496 | u16 n = mtd->writesize >> 9; |
| 497 | u8 *d = host->data_buf + mtd->writesize; |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 498 | u8 *s = host->spare0; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 499 | u16 t = host->spare_len; |
| 500 | |
| 501 | j = (mtd->oobsize / n >> 1) << 1; |
| 502 | |
| 503 | if (bfrom) { |
| 504 | for (i = 0; i < n - 1; i++) |
| 505 | memcpy(d + i * j, s + i * t, j); |
| 506 | |
| 507 | /* the last section */ |
| 508 | memcpy(d + i * j, s + i * t, mtd->oobsize - i * j); |
| 509 | } else { |
| 510 | for (i = 0; i < n - 1; i++) |
| 511 | memcpy(&s[i * t], &d[i * j], j); |
| 512 | |
| 513 | /* the last section */ |
| 514 | memcpy(&s[i * t], &d[i * j], mtd->oobsize - i * j); |
| 515 | } |
| 516 | } |
| 517 | |
Sascha Hauer | a3e65b6 | 2009-06-02 11:47:59 +0200 | [diff] [blame] | 518 | static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 519 | { |
| 520 | struct nand_chip *nand_chip = mtd->priv; |
| 521 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 522 | |
| 523 | /* Write out column address, if necessary */ |
| 524 | if (column != -1) { |
| 525 | /* |
| 526 | * MXC NANDFC can only perform full page+spare or |
| 527 | * spare-only read/write. When the upper layers |
| 528 | * layers perform a read/write buf operation, |
Daniel Mack | 3ad2f3f | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 529 | * we will used the saved column address to index into |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 530 | * the full page. |
| 531 | */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 532 | host->send_addr(host, 0, page_addr == -1); |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame] | 533 | if (mtd->writesize > 512) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 534 | /* another col addr cycle for 2k page */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 535 | host->send_addr(host, 0, false); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /* Write out page address, if necessary */ |
| 539 | if (page_addr != -1) { |
| 540 | /* paddr_0 - p_addr_7 */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 541 | host->send_addr(host, (page_addr & 0xff), false); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 542 | |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame] | 543 | if (mtd->writesize > 512) { |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 544 | if (mtd->size >= 0x10000000) { |
| 545 | /* paddr_8 - paddr_15 */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 546 | host->send_addr(host, (page_addr >> 8) & 0xff, false); |
| 547 | host->send_addr(host, (page_addr >> 16) & 0xff, true); |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 548 | } else |
| 549 | /* paddr_8 - paddr_15 */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 550 | host->send_addr(host, (page_addr >> 8) & 0xff, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 551 | } else { |
| 552 | /* One more address cycle for higher density devices */ |
| 553 | if (mtd->size >= 0x4000000) { |
| 554 | /* paddr_8 - paddr_15 */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 555 | host->send_addr(host, (page_addr >> 8) & 0xff, false); |
| 556 | host->send_addr(host, (page_addr >> 16) & 0xff, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 557 | } else |
| 558 | /* paddr_8 - paddr_15 */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 559 | host->send_addr(host, (page_addr >> 8) & 0xff, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 560 | } |
| 561 | } |
Sascha Hauer | a3e65b6 | 2009-06-02 11:47:59 +0200 | [diff] [blame] | 562 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 563 | |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 564 | static void preset_v1_v2(struct mtd_info *mtd) |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 565 | { |
| 566 | struct nand_chip *nand_chip = mtd->priv; |
| 567 | struct mxc_nand_host *host = nand_chip->priv; |
| 568 | uint16_t tmp; |
| 569 | |
Ivo Clarysse | a47bfd2 | 2010-04-08 16:16:51 +0200 | [diff] [blame] | 570 | /* enable interrupt, disable spare enable */ |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 571 | tmp = readw(host->regs + NFC_CONFIG1); |
Ivo Clarysse | a47bfd2 | 2010-04-08 16:16:51 +0200 | [diff] [blame] | 572 | tmp &= ~NFC_INT_MSK; |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 573 | tmp &= ~NFC_SP_EN; |
| 574 | if (nand_chip->ecc.mode == NAND_ECC_HW) { |
| 575 | tmp |= NFC_ECC_EN; |
| 576 | } else { |
| 577 | tmp &= ~NFC_ECC_EN; |
| 578 | } |
| 579 | writew(tmp, host->regs + NFC_CONFIG1); |
| 580 | /* preset operation */ |
| 581 | |
| 582 | /* Unlock the internal RAM Buffer */ |
| 583 | writew(0x2, host->regs + NFC_CONFIG); |
| 584 | |
| 585 | /* Blocks to be unlocked */ |
| 586 | if (nfc_is_v21()) { |
| 587 | writew(0x0, host->regs + NFC_V21_UNLOCKSTART_BLKADDR); |
| 588 | writew(0xffff, host->regs + NFC_V21_UNLOCKEND_BLKADDR); |
| 589 | } else if (nfc_is_v1()) { |
| 590 | writew(0x0, host->regs + NFC_V1_UNLOCKSTART_BLKADDR); |
| 591 | writew(0x4000, host->regs + NFC_V1_UNLOCKEND_BLKADDR); |
| 592 | } else |
| 593 | BUG(); |
| 594 | |
| 595 | /* Unlock Block Command for given address range */ |
| 596 | writew(0x4, host->regs + NFC_WRPROT); |
| 597 | } |
| 598 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 599 | /* Used by the upper layer to write command to NAND Flash for |
| 600 | * different operations to be carried out on NAND Flash */ |
| 601 | static void mxc_nand_command(struct mtd_info *mtd, unsigned command, |
| 602 | int column, int page_addr) |
| 603 | { |
| 604 | struct nand_chip *nand_chip = mtd->priv; |
| 605 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 606 | |
| 607 | DEBUG(MTD_DEBUG_LEVEL3, |
| 608 | "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n", |
| 609 | command, column, page_addr); |
| 610 | |
| 611 | /* Reset command state information */ |
| 612 | host->status_request = false; |
| 613 | |
| 614 | /* Command pre-processing step */ |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 615 | switch (command) { |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 616 | case NAND_CMD_RESET: |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 617 | host->preset(mtd); |
| 618 | host->send_cmd(host, command, false); |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 619 | break; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 620 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 621 | case NAND_CMD_STATUS: |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 622 | host->buf_start = 0; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 623 | host->status_request = true; |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 624 | |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 625 | host->send_cmd(host, command, true); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 626 | mxc_do_addr_cycle(mtd, column, page_addr); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 627 | break; |
| 628 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 629 | case NAND_CMD_READ0: |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 630 | case NAND_CMD_READOOB: |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 631 | if (command == NAND_CMD_READ0) |
| 632 | host->buf_start = column; |
| 633 | else |
| 634 | host->buf_start = column + mtd->writesize; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 635 | |
Sascha Hauer | 6c49939 | 2010-05-28 10:02:17 +0200 | [diff] [blame] | 636 | command = NAND_CMD_READ0; /* only READ0 is valid */ |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 637 | |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 638 | host->send_cmd(host, command, false); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 639 | mxc_do_addr_cycle(mtd, column, page_addr); |
| 640 | |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame] | 641 | if (mtd->writesize > 512) |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 642 | host->send_cmd(host, NAND_CMD_READSTART, true); |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 643 | |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 644 | host->send_page(mtd, NFC_OUTPUT); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 645 | |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 646 | memcpy(host->data_buf, host->main_area0, mtd->writesize); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 647 | copy_spare(mtd, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 648 | break; |
| 649 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 650 | case NAND_CMD_SEQIN: |
Sascha Hauer | 6c49939 | 2010-05-28 10:02:17 +0200 | [diff] [blame] | 651 | if (column >= mtd->writesize) |
| 652 | /* call ourself to read a page */ |
| 653 | mxc_nand_command(mtd, NAND_CMD_READ0, 0, page_addr); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 654 | |
Sascha Hauer | 6c49939 | 2010-05-28 10:02:17 +0200 | [diff] [blame] | 655 | host->buf_start = column; |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 656 | |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 657 | host->send_cmd(host, command, false); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 658 | mxc_do_addr_cycle(mtd, column, page_addr); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 659 | break; |
| 660 | |
| 661 | case NAND_CMD_PAGEPROG: |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 662 | memcpy(host->main_area0, host->data_buf, mtd->writesize); |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 663 | copy_spare(mtd, false); |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 664 | host->send_page(mtd, NFC_INPUT); |
| 665 | host->send_cmd(host, command, true); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 666 | mxc_do_addr_cycle(mtd, column, page_addr); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 667 | break; |
| 668 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 669 | case NAND_CMD_READID: |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 670 | host->send_cmd(host, command, true); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 671 | mxc_do_addr_cycle(mtd, column, page_addr); |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 672 | host->send_read_id(host); |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 673 | host->buf_start = column; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 674 | break; |
| 675 | |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 676 | case NAND_CMD_ERASE1: |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 677 | case NAND_CMD_ERASE2: |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 678 | host->send_cmd(host, command, false); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 679 | mxc_do_addr_cycle(mtd, column, page_addr); |
| 680 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 681 | break; |
| 682 | } |
| 683 | } |
| 684 | |
Sascha Hauer | f137205 | 2009-10-21 14:25:27 +0200 | [diff] [blame] | 685 | /* |
| 686 | * The generic flash bbt decriptors overlap with our ecc |
| 687 | * hardware, so define some i.MX specific ones. |
| 688 | */ |
| 689 | static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' }; |
| 690 | static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' }; |
| 691 | |
| 692 | static struct nand_bbt_descr bbt_main_descr = { |
| 693 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 694 | | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, |
| 695 | .offs = 0, |
| 696 | .len = 4, |
| 697 | .veroffs = 4, |
| 698 | .maxblocks = 4, |
| 699 | .pattern = bbt_pattern, |
| 700 | }; |
| 701 | |
| 702 | static struct nand_bbt_descr bbt_mirror_descr = { |
| 703 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 704 | | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, |
| 705 | .offs = 0, |
| 706 | .len = 4, |
| 707 | .veroffs = 4, |
| 708 | .maxblocks = 4, |
| 709 | .pattern = mirror_pattern, |
| 710 | }; |
| 711 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 712 | static int __init mxcnd_probe(struct platform_device *pdev) |
| 713 | { |
| 714 | struct nand_chip *this; |
| 715 | struct mtd_info *mtd; |
| 716 | struct mxc_nand_platform_data *pdata = pdev->dev.platform_data; |
| 717 | struct mxc_nand_host *host; |
| 718 | struct resource *res; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 719 | int err = 0, nr_parts = 0; |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 720 | struct nand_ecclayout *oob_smallpage, *oob_largepage; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 721 | |
| 722 | /* Allocate memory for MTD device structure and private data */ |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 723 | host = kzalloc(sizeof(struct mxc_nand_host) + NAND_MAX_PAGESIZE + |
| 724 | NAND_MAX_OOBSIZE, GFP_KERNEL); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 725 | if (!host) |
| 726 | return -ENOMEM; |
| 727 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 728 | host->data_buf = (uint8_t *)(host + 1); |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 729 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 730 | host->dev = &pdev->dev; |
| 731 | /* structures must be linked */ |
| 732 | this = &host->nand; |
| 733 | mtd = &host->mtd; |
| 734 | mtd->priv = this; |
| 735 | mtd->owner = THIS_MODULE; |
David Brownell | 87f39f0 | 2009-03-26 00:42:50 -0700 | [diff] [blame] | 736 | mtd->dev.parent = &pdev->dev; |
Sascha Hauer | 1fbff0a | 2009-10-21 16:06:27 +0200 | [diff] [blame] | 737 | mtd->name = DRIVER_NAME; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 738 | |
| 739 | /* 50 us command delay time */ |
| 740 | this->chip_delay = 5; |
| 741 | |
| 742 | this->priv = host; |
| 743 | this->dev_ready = mxc_nand_dev_ready; |
| 744 | this->cmdfunc = mxc_nand_command; |
| 745 | this->select_chip = mxc_nand_select_chip; |
| 746 | this->read_byte = mxc_nand_read_byte; |
| 747 | this->read_word = mxc_nand_read_word; |
| 748 | this->write_buf = mxc_nand_write_buf; |
| 749 | this->read_buf = mxc_nand_read_buf; |
| 750 | this->verify_buf = mxc_nand_verify_buf; |
| 751 | |
Sascha Hauer | e65fb00 | 2009-02-16 14:29:10 +0100 | [diff] [blame] | 752 | host->clk = clk_get(&pdev->dev, "nfc"); |
Vladimir Barinov | 8541c11 | 2009-04-23 15:47:22 +0400 | [diff] [blame] | 753 | if (IS_ERR(host->clk)) { |
| 754 | err = PTR_ERR(host->clk); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 755 | goto eclk; |
Vladimir Barinov | 8541c11 | 2009-04-23 15:47:22 +0400 | [diff] [blame] | 756 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 757 | |
| 758 | clk_enable(host->clk); |
| 759 | host->clk_act = 1; |
| 760 | |
| 761 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 762 | if (!res) { |
| 763 | err = -ENODEV; |
| 764 | goto eres; |
| 765 | } |
| 766 | |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 767 | host->base = ioremap(res->start, resource_size(res)); |
| 768 | if (!host->base) { |
Vladimir Barinov | 8541c11 | 2009-04-23 15:47:22 +0400 | [diff] [blame] | 769 | err = -ENOMEM; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 770 | goto eres; |
| 771 | } |
| 772 | |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 773 | host->main_area0 = host->base; |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 774 | |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 775 | if (nfc_is_v1() || nfc_is_v21()) { |
| 776 | host->preset = preset_v1_v2; |
| 777 | host->send_cmd = send_cmd_v1_v2; |
| 778 | host->send_addr = send_addr_v1_v2; |
| 779 | host->send_page = send_page_v1_v2; |
| 780 | host->send_read_id = send_read_id_v1_v2; |
| 781 | host->get_dev_status = get_dev_status_v1_v2; |
Sascha Hauer | 7aaf28a | 2010-08-06 15:53:07 +0200 | [diff] [blame^] | 782 | host->check_int = check_int_v1_v2; |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 783 | } |
| 784 | |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 785 | if (nfc_is_v21()) { |
Sascha Hauer | 938cf99 | 2010-08-06 15:53:04 +0200 | [diff] [blame] | 786 | host->regs = host->base + 0x1e00; |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 787 | host->spare0 = host->base + 0x1000; |
| 788 | host->spare_len = 64; |
| 789 | oob_smallpage = &nandv2_hw_eccoob_smallpage; |
| 790 | oob_largepage = &nandv2_hw_eccoob_largepage; |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 791 | this->ecc.bytes = 9; |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 792 | } else if (nfc_is_v1()) { |
Sascha Hauer | 938cf99 | 2010-08-06 15:53:04 +0200 | [diff] [blame] | 793 | host->regs = host->base + 0xe00; |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 794 | host->spare0 = host->base + 0x800; |
| 795 | host->spare_len = 16; |
| 796 | oob_smallpage = &nandv1_hw_eccoob_smallpage; |
| 797 | oob_largepage = &nandv1_hw_eccoob_largepage; |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 798 | this->ecc.bytes = 3; |
| 799 | } else |
| 800 | BUG(); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 801 | |
Sascha Hauer | 13e1add | 2009-10-21 10:39:05 +0200 | [diff] [blame] | 802 | this->ecc.size = 512; |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 803 | this->ecc.layout = oob_smallpage; |
Sascha Hauer | 13e1add | 2009-10-21 10:39:05 +0200 | [diff] [blame] | 804 | |
| 805 | if (pdata->hw_ecc) { |
| 806 | this->ecc.calculate = mxc_nand_calculate_ecc; |
| 807 | this->ecc.hwctl = mxc_nand_enable_hwecc; |
| 808 | this->ecc.correct = mxc_nand_correct_data; |
| 809 | this->ecc.mode = NAND_ECC_HW; |
Sascha Hauer | 13e1add | 2009-10-21 10:39:05 +0200 | [diff] [blame] | 810 | } else { |
| 811 | this->ecc.mode = NAND_ECC_SOFT; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 812 | } |
| 813 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 814 | /* NAND bus width determines access funtions used by upper layer */ |
Sascha Hauer | 13e1add | 2009-10-21 10:39:05 +0200 | [diff] [blame] | 815 | if (pdata->width == 2) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 816 | this->options |= NAND_BUSWIDTH_16; |
Sascha Hauer | 13e1add | 2009-10-21 10:39:05 +0200 | [diff] [blame] | 817 | |
Sascha Hauer | f137205 | 2009-10-21 14:25:27 +0200 | [diff] [blame] | 818 | if (pdata->flash_bbt) { |
| 819 | this->bbt_td = &bbt_main_descr; |
| 820 | this->bbt_md = &bbt_mirror_descr; |
| 821 | /* update flash based bbt */ |
| 822 | this->options |= NAND_USE_FLASH_BBT; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 823 | } |
| 824 | |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 825 | init_waitqueue_head(&host->irq_waitq); |
| 826 | |
| 827 | host->irq = platform_get_irq(pdev, 0); |
| 828 | |
Ivo Clarysse | a47bfd2 | 2010-04-08 16:16:51 +0200 | [diff] [blame] | 829 | err = request_irq(host->irq, mxc_nfc_irq, IRQF_DISABLED, DRIVER_NAME, host); |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 830 | if (err) |
| 831 | goto eirq; |
| 832 | |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 833 | /* first scan to find the device and get the page size */ |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 834 | if (nand_scan_ident(mtd, 1, NULL)) { |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 835 | err = -ENXIO; |
| 836 | goto escan; |
| 837 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 838 | |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame] | 839 | if (mtd->writesize == 2048) |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 840 | this->ecc.layout = oob_largepage; |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 841 | |
| 842 | /* second phase scan */ |
| 843 | if (nand_scan_tail(mtd)) { |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 844 | err = -ENXIO; |
| 845 | goto escan; |
| 846 | } |
| 847 | |
| 848 | /* Register the partitions */ |
| 849 | #ifdef CONFIG_MTD_PARTITIONS |
| 850 | nr_parts = |
| 851 | parse_mtd_partitions(mtd, part_probes, &host->parts, 0); |
| 852 | if (nr_parts > 0) |
| 853 | add_mtd_partitions(mtd, host->parts, nr_parts); |
| 854 | else |
| 855 | #endif |
| 856 | { |
| 857 | pr_info("Registering %s as whole device\n", mtd->name); |
| 858 | add_mtd_device(mtd); |
| 859 | } |
| 860 | |
| 861 | platform_set_drvdata(pdev, host); |
| 862 | |
| 863 | return 0; |
| 864 | |
| 865 | escan: |
Magnus Lilja | b258fd8 | 2009-05-08 21:57:47 +0200 | [diff] [blame] | 866 | free_irq(host->irq, host); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 867 | eirq: |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 868 | iounmap(host->base); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 869 | eres: |
| 870 | clk_put(host->clk); |
| 871 | eclk: |
| 872 | kfree(host); |
| 873 | |
| 874 | return err; |
| 875 | } |
| 876 | |
Uwe Kleine-König | 51eeb87 | 2009-12-07 09:44:05 +0000 | [diff] [blame] | 877 | static int __devexit mxcnd_remove(struct platform_device *pdev) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 878 | { |
| 879 | struct mxc_nand_host *host = platform_get_drvdata(pdev); |
| 880 | |
| 881 | clk_put(host->clk); |
| 882 | |
| 883 | platform_set_drvdata(pdev, NULL); |
| 884 | |
| 885 | nand_release(&host->mtd); |
Magnus Lilja | b258fd8 | 2009-05-08 21:57:47 +0200 | [diff] [blame] | 886 | free_irq(host->irq, host); |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 887 | iounmap(host->base); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 888 | kfree(host); |
| 889 | |
| 890 | return 0; |
| 891 | } |
| 892 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 893 | static struct platform_driver mxcnd_driver = { |
| 894 | .driver = { |
| 895 | .name = DRIVER_NAME, |
Eric Bénard | 04dd0d3 | 2010-06-17 20:59:04 +0200 | [diff] [blame] | 896 | }, |
Uwe Kleine-König | daa0f15 | 2009-11-24 22:07:08 +0100 | [diff] [blame] | 897 | .remove = __devexit_p(mxcnd_remove), |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 898 | }; |
| 899 | |
| 900 | static int __init mxc_nd_init(void) |
| 901 | { |
Vladimir Barinov | 8541c11 | 2009-04-23 15:47:22 +0400 | [diff] [blame] | 902 | return platform_driver_probe(&mxcnd_driver, mxcnd_probe); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | static void __exit mxc_nd_cleanup(void) |
| 906 | { |
| 907 | /* Unregister the device structure */ |
| 908 | platform_driver_unregister(&mxcnd_driver); |
| 909 | } |
| 910 | |
| 911 | module_init(mxc_nd_init); |
| 912 | module_exit(mxc_nd_cleanup); |
| 913 | |
| 914 | MODULE_AUTHOR("Freescale Semiconductor, Inc."); |
| 915 | MODULE_DESCRIPTION("MXC NAND MTD driver"); |
| 916 | MODULE_LICENSE("GPL"); |