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> |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 33 | #include <linux/irq.h> |
| 34 | #include <linux/completion.h> |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 35 | |
| 36 | #include <asm/mach/flash.h> |
| 37 | #include <mach/mxc_nand.h> |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 38 | #include <mach/hardware.h> |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 39 | |
| 40 | #define DRIVER_NAME "mxc_nand" |
| 41 | |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 42 | #define nfc_is_v21() (cpu_is_mx25() || cpu_is_mx35()) |
Ivo Clarysse | a47bfd2 | 2010-04-08 16:16:51 +0200 | [diff] [blame] | 43 | #define nfc_is_v1() (cpu_is_mx31() || cpu_is_mx27() || cpu_is_mx21()) |
Jason Liu | c97926d | 2011-08-22 14:13:17 +0800 | [diff] [blame] | 44 | #define nfc_is_v3_2() (cpu_is_mx51() || cpu_is_mx53()) |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 45 | #define nfc_is_v3() nfc_is_v3_2() |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 46 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 47 | /* Addresses for NFC registers */ |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 48 | #define NFC_V1_V2_BUF_SIZE (host->regs + 0x00) |
| 49 | #define NFC_V1_V2_BUF_ADDR (host->regs + 0x04) |
| 50 | #define NFC_V1_V2_FLASH_ADDR (host->regs + 0x06) |
| 51 | #define NFC_V1_V2_FLASH_CMD (host->regs + 0x08) |
| 52 | #define NFC_V1_V2_CONFIG (host->regs + 0x0a) |
| 53 | #define NFC_V1_V2_ECC_STATUS_RESULT (host->regs + 0x0c) |
| 54 | #define NFC_V1_V2_RSLTMAIN_AREA (host->regs + 0x0e) |
| 55 | #define NFC_V1_V2_RSLTSPARE_AREA (host->regs + 0x10) |
| 56 | #define NFC_V1_V2_WRPROT (host->regs + 0x12) |
| 57 | #define NFC_V1_UNLOCKSTART_BLKADDR (host->regs + 0x14) |
| 58 | #define NFC_V1_UNLOCKEND_BLKADDR (host->regs + 0x16) |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 59 | #define NFC_V21_UNLOCKSTART_BLKADDR0 (host->regs + 0x20) |
| 60 | #define NFC_V21_UNLOCKSTART_BLKADDR1 (host->regs + 0x24) |
| 61 | #define NFC_V21_UNLOCKSTART_BLKADDR2 (host->regs + 0x28) |
| 62 | #define NFC_V21_UNLOCKSTART_BLKADDR3 (host->regs + 0x2c) |
| 63 | #define NFC_V21_UNLOCKEND_BLKADDR0 (host->regs + 0x22) |
| 64 | #define NFC_V21_UNLOCKEND_BLKADDR1 (host->regs + 0x26) |
| 65 | #define NFC_V21_UNLOCKEND_BLKADDR2 (host->regs + 0x2a) |
| 66 | #define NFC_V21_UNLOCKEND_BLKADDR3 (host->regs + 0x2e) |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 67 | #define NFC_V1_V2_NF_WRPRST (host->regs + 0x18) |
| 68 | #define NFC_V1_V2_CONFIG1 (host->regs + 0x1a) |
| 69 | #define NFC_V1_V2_CONFIG2 (host->regs + 0x1c) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 70 | |
Sascha Hauer | 6e85dfd | 2010-08-06 15:53:10 +0200 | [diff] [blame] | 71 | #define NFC_V2_CONFIG1_ECC_MODE_4 (1 << 0) |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 72 | #define NFC_V1_V2_CONFIG1_SP_EN (1 << 2) |
| 73 | #define NFC_V1_V2_CONFIG1_ECC_EN (1 << 3) |
| 74 | #define NFC_V1_V2_CONFIG1_INT_MSK (1 << 4) |
| 75 | #define NFC_V1_V2_CONFIG1_BIG (1 << 5) |
| 76 | #define NFC_V1_V2_CONFIG1_RST (1 << 6) |
| 77 | #define NFC_V1_V2_CONFIG1_CE (1 << 7) |
Sascha Hauer | b8db2f5 | 2010-08-09 15:04:19 +0200 | [diff] [blame] | 78 | #define NFC_V2_CONFIG1_ONE_CYCLE (1 << 8) |
| 79 | #define NFC_V2_CONFIG1_PPB(x) (((x) & 0x3) << 9) |
| 80 | #define NFC_V2_CONFIG1_FP_INT (1 << 11) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 81 | |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 82 | #define NFC_V1_V2_CONFIG2_INT (1 << 15) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 83 | |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 84 | /* |
| 85 | * Operation modes for the NFC. Valid for v1, v2 and v3 |
| 86 | * type controllers. |
| 87 | */ |
| 88 | #define NFC_CMD (1 << 0) |
| 89 | #define NFC_ADDR (1 << 1) |
| 90 | #define NFC_INPUT (1 << 2) |
| 91 | #define NFC_OUTPUT (1 << 3) |
| 92 | #define NFC_ID (1 << 4) |
| 93 | #define NFC_STATUS (1 << 5) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 94 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 95 | #define NFC_V3_FLASH_CMD (host->regs_axi + 0x00) |
| 96 | #define NFC_V3_FLASH_ADDR0 (host->regs_axi + 0x04) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 97 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 98 | #define NFC_V3_CONFIG1 (host->regs_axi + 0x34) |
| 99 | #define NFC_V3_CONFIG1_SP_EN (1 << 0) |
| 100 | #define NFC_V3_CONFIG1_RBA(x) (((x) & 0x7 ) << 4) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 101 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 102 | #define NFC_V3_ECC_STATUS_RESULT (host->regs_axi + 0x38) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 103 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 104 | #define NFC_V3_LAUNCH (host->regs_axi + 0x40) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 105 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 106 | #define NFC_V3_WRPROT (host->regs_ip + 0x0) |
| 107 | #define NFC_V3_WRPROT_LOCK_TIGHT (1 << 0) |
| 108 | #define NFC_V3_WRPROT_LOCK (1 << 1) |
| 109 | #define NFC_V3_WRPROT_UNLOCK (1 << 2) |
| 110 | #define NFC_V3_WRPROT_BLS_UNLOCK (2 << 6) |
| 111 | |
| 112 | #define NFC_V3_WRPROT_UNLOCK_BLK_ADD0 (host->regs_ip + 0x04) |
| 113 | |
| 114 | #define NFC_V3_CONFIG2 (host->regs_ip + 0x24) |
| 115 | #define NFC_V3_CONFIG2_PS_512 (0 << 0) |
| 116 | #define NFC_V3_CONFIG2_PS_2048 (1 << 0) |
| 117 | #define NFC_V3_CONFIG2_PS_4096 (2 << 0) |
| 118 | #define NFC_V3_CONFIG2_ONE_CYCLE (1 << 2) |
| 119 | #define NFC_V3_CONFIG2_ECC_EN (1 << 3) |
| 120 | #define NFC_V3_CONFIG2_2CMD_PHASES (1 << 4) |
| 121 | #define NFC_V3_CONFIG2_NUM_ADDR_PHASE0 (1 << 5) |
| 122 | #define NFC_V3_CONFIG2_ECC_MODE_8 (1 << 6) |
| 123 | #define NFC_V3_CONFIG2_PPB(x) (((x) & 0x3) << 7) |
| 124 | #define NFC_V3_CONFIG2_NUM_ADDR_PHASE1(x) (((x) & 0x3) << 12) |
| 125 | #define NFC_V3_CONFIG2_INT_MSK (1 << 15) |
| 126 | #define NFC_V3_CONFIG2_ST_CMD(x) (((x) & 0xff) << 24) |
| 127 | #define NFC_V3_CONFIG2_SPAS(x) (((x) & 0xff) << 16) |
| 128 | |
| 129 | #define NFC_V3_CONFIG3 (host->regs_ip + 0x28) |
| 130 | #define NFC_V3_CONFIG3_ADD_OP(x) (((x) & 0x3) << 0) |
| 131 | #define NFC_V3_CONFIG3_FW8 (1 << 3) |
| 132 | #define NFC_V3_CONFIG3_SBB(x) (((x) & 0x7) << 8) |
| 133 | #define NFC_V3_CONFIG3_NUM_OF_DEVICES(x) (((x) & 0x7) << 12) |
| 134 | #define NFC_V3_CONFIG3_RBB_MODE (1 << 15) |
| 135 | #define NFC_V3_CONFIG3_NO_SDMA (1 << 20) |
| 136 | |
| 137 | #define NFC_V3_IPC (host->regs_ip + 0x2C) |
| 138 | #define NFC_V3_IPC_CREQ (1 << 0) |
| 139 | #define NFC_V3_IPC_INT (1 << 31) |
| 140 | |
| 141 | #define NFC_V3_DELAY_LINE (host->regs_ip + 0x34) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 142 | |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 143 | struct mxc_nand_host; |
| 144 | |
| 145 | struct mxc_nand_devtype_data { |
| 146 | void (*preset)(struct mtd_info *); |
| 147 | void (*send_cmd)(struct mxc_nand_host *, uint16_t, int); |
| 148 | void (*send_addr)(struct mxc_nand_host *, uint16_t, int); |
| 149 | void (*send_page)(struct mtd_info *, unsigned int); |
| 150 | void (*send_read_id)(struct mxc_nand_host *); |
| 151 | uint16_t (*get_dev_status)(struct mxc_nand_host *); |
| 152 | int (*check_int)(struct mxc_nand_host *); |
| 153 | void (*irq_control)(struct mxc_nand_host *, int); |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 154 | u32 (*get_ecc_status)(struct mxc_nand_host *); |
Uwe Kleine-König | 6dcdf99 | 2012-04-23 11:23:37 +0200 | [diff] [blame^] | 155 | struct nand_ecclayout *ecclayout_512, *ecclayout_2k, *ecclayout_4k; |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 156 | }; |
| 157 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 158 | struct mxc_nand_host { |
| 159 | struct mtd_info mtd; |
| 160 | struct nand_chip nand; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 161 | struct device *dev; |
| 162 | |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 163 | void *spare0; |
| 164 | void *main_area0; |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 165 | |
| 166 | void __iomem *base; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 167 | void __iomem *regs; |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 168 | void __iomem *regs_axi; |
| 169 | void __iomem *regs_ip; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 170 | int status_request; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 171 | struct clk *clk; |
| 172 | int clk_act; |
| 173 | int irq; |
Sascha Hauer | 94f77e5 | 2010-08-06 15:53:09 +0200 | [diff] [blame] | 174 | int eccsize; |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 175 | int active_cs; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 176 | |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 177 | struct completion op_completion; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 178 | |
| 179 | uint8_t *data_buf; |
| 180 | unsigned int buf_start; |
| 181 | int spare_len; |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 182 | |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 183 | const struct mxc_nand_devtype_data *devtype_data; |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 184 | |
| 185 | /* |
| 186 | * On i.MX21 the CONFIG2:INT bit cannot be read if interrupts are masked |
| 187 | * (CONFIG1:INT_MSK is set). To handle this the driver uses |
| 188 | * enable_irq/disable_irq_nosync instead of CONFIG1:INT_MSK |
| 189 | */ |
| 190 | int irqpending_quirk; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 191 | }; |
| 192 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 193 | /* OOB placement block for use with hardware ecc generation */ |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 194 | static struct nand_ecclayout nandv1_hw_eccoob_smallpage = { |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 195 | .eccbytes = 5, |
| 196 | .eccpos = {6, 7, 8, 9, 10}, |
Sascha Hauer | 8c1fd89 | 2009-10-21 10:22:01 +0200 | [diff] [blame] | 197 | .oobfree = {{0, 5}, {12, 4}, } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 198 | }; |
| 199 | |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 200 | static struct nand_ecclayout nandv1_hw_eccoob_largepage = { |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 201 | .eccbytes = 20, |
| 202 | .eccpos = {6, 7, 8, 9, 10, 22, 23, 24, 25, 26, |
| 203 | 38, 39, 40, 41, 42, 54, 55, 56, 57, 58}, |
| 204 | .oobfree = {{2, 4}, {11, 10}, {27, 10}, {43, 10}, {59, 5}, } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 205 | }; |
| 206 | |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 207 | /* OOB description for 512 byte pages with 16 byte OOB */ |
| 208 | static struct nand_ecclayout nandv2_hw_eccoob_smallpage = { |
| 209 | .eccbytes = 1 * 9, |
| 210 | .eccpos = { |
| 211 | 7, 8, 9, 10, 11, 12, 13, 14, 15 |
| 212 | }, |
| 213 | .oobfree = { |
| 214 | {.offset = 0, .length = 5} |
| 215 | } |
| 216 | }; |
| 217 | |
| 218 | /* OOB description for 2048 byte pages with 64 byte OOB */ |
| 219 | static struct nand_ecclayout nandv2_hw_eccoob_largepage = { |
| 220 | .eccbytes = 4 * 9, |
| 221 | .eccpos = { |
| 222 | 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 223 | 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 224 | 39, 40, 41, 42, 43, 44, 45, 46, 47, |
| 225 | 55, 56, 57, 58, 59, 60, 61, 62, 63 |
| 226 | }, |
| 227 | .oobfree = { |
| 228 | {.offset = 2, .length = 4}, |
| 229 | {.offset = 16, .length = 7}, |
| 230 | {.offset = 32, .length = 7}, |
| 231 | {.offset = 48, .length = 7} |
| 232 | } |
| 233 | }; |
| 234 | |
Baruch Siach | 2c1c5f1 | 2011-03-09 16:12:20 +0200 | [diff] [blame] | 235 | /* OOB description for 4096 byte pages with 128 byte OOB */ |
| 236 | static struct nand_ecclayout nandv2_hw_eccoob_4k = { |
| 237 | .eccbytes = 8 * 9, |
| 238 | .eccpos = { |
| 239 | 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 240 | 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 241 | 39, 40, 41, 42, 43, 44, 45, 46, 47, |
| 242 | 55, 56, 57, 58, 59, 60, 61, 62, 63, |
| 243 | 71, 72, 73, 74, 75, 76, 77, 78, 79, |
| 244 | 87, 88, 89, 90, 91, 92, 93, 94, 95, |
| 245 | 103, 104, 105, 106, 107, 108, 109, 110, 111, |
| 246 | 119, 120, 121, 122, 123, 124, 125, 126, 127, |
| 247 | }, |
| 248 | .oobfree = { |
| 249 | {.offset = 2, .length = 4}, |
| 250 | {.offset = 16, .length = 7}, |
| 251 | {.offset = 32, .length = 7}, |
| 252 | {.offset = 48, .length = 7}, |
| 253 | {.offset = 64, .length = 7}, |
| 254 | {.offset = 80, .length = 7}, |
| 255 | {.offset = 96, .length = 7}, |
| 256 | {.offset = 112, .length = 7}, |
| 257 | } |
| 258 | }; |
| 259 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 260 | static const char *part_probes[] = { "RedBoot", "cmdlinepart", NULL }; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 261 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 262 | static int check_int_v3(struct mxc_nand_host *host) |
| 263 | { |
| 264 | uint32_t tmp; |
| 265 | |
| 266 | tmp = readl(NFC_V3_IPC); |
| 267 | if (!(tmp & NFC_V3_IPC_INT)) |
| 268 | return 0; |
| 269 | |
| 270 | tmp &= ~NFC_V3_IPC_INT; |
| 271 | writel(tmp, NFC_V3_IPC); |
| 272 | |
| 273 | return 1; |
| 274 | } |
| 275 | |
Sascha Hauer | 7aaf28a | 2010-08-06 15:53:07 +0200 | [diff] [blame] | 276 | static int check_int_v1_v2(struct mxc_nand_host *host) |
| 277 | { |
| 278 | uint32_t tmp; |
| 279 | |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 280 | tmp = readw(NFC_V1_V2_CONFIG2); |
| 281 | if (!(tmp & NFC_V1_V2_CONFIG2_INT)) |
Sascha Hauer | 7aaf28a | 2010-08-06 15:53:07 +0200 | [diff] [blame] | 282 | return 0; |
| 283 | |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 284 | if (!host->irqpending_quirk) |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 285 | writew(tmp & ~NFC_V1_V2_CONFIG2_INT, NFC_V1_V2_CONFIG2); |
Sascha Hauer | 7aaf28a | 2010-08-06 15:53:07 +0200 | [diff] [blame] | 286 | |
| 287 | return 1; |
| 288 | } |
| 289 | |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 290 | static void irq_control_v1_v2(struct mxc_nand_host *host, int activate) |
| 291 | { |
| 292 | uint16_t tmp; |
| 293 | |
| 294 | tmp = readw(NFC_V1_V2_CONFIG1); |
| 295 | |
| 296 | if (activate) |
| 297 | tmp &= ~NFC_V1_V2_CONFIG1_INT_MSK; |
| 298 | else |
| 299 | tmp |= NFC_V1_V2_CONFIG1_INT_MSK; |
| 300 | |
| 301 | writew(tmp, NFC_V1_V2_CONFIG1); |
| 302 | } |
| 303 | |
| 304 | static void irq_control_v3(struct mxc_nand_host *host, int activate) |
| 305 | { |
| 306 | uint32_t tmp; |
| 307 | |
| 308 | tmp = readl(NFC_V3_CONFIG2); |
| 309 | |
| 310 | if (activate) |
| 311 | tmp &= ~NFC_V3_CONFIG2_INT_MSK; |
| 312 | else |
| 313 | tmp |= NFC_V3_CONFIG2_INT_MSK; |
| 314 | |
| 315 | writel(tmp, NFC_V3_CONFIG2); |
| 316 | } |
| 317 | |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 318 | static void irq_control(struct mxc_nand_host *host, int activate) |
| 319 | { |
| 320 | if (host->irqpending_quirk) { |
| 321 | if (activate) |
| 322 | enable_irq(host->irq); |
| 323 | else |
| 324 | disable_irq_nosync(host->irq); |
| 325 | } else { |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 326 | host->devtype_data->irq_control(host, activate); |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 330 | static u32 get_ecc_status_v1(struct mxc_nand_host *host) |
| 331 | { |
| 332 | return readw(NFC_V1_V2_ECC_STATUS_RESULT); |
| 333 | } |
| 334 | |
| 335 | static u32 get_ecc_status_v2(struct mxc_nand_host *host) |
| 336 | { |
| 337 | return readl(NFC_V1_V2_ECC_STATUS_RESULT); |
| 338 | } |
| 339 | |
| 340 | static u32 get_ecc_status_v3(struct mxc_nand_host *host) |
| 341 | { |
| 342 | return readl(NFC_V3_ECC_STATUS_RESULT); |
| 343 | } |
| 344 | |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 345 | static irqreturn_t mxc_nfc_irq(int irq, void *dev_id) |
| 346 | { |
| 347 | struct mxc_nand_host *host = dev_id; |
| 348 | |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 349 | if (!host->devtype_data->check_int(host)) |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 350 | return IRQ_NONE; |
| 351 | |
| 352 | irq_control(host, 0); |
| 353 | |
| 354 | complete(&host->op_completion); |
| 355 | |
| 356 | return IRQ_HANDLED; |
| 357 | } |
| 358 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 359 | /* This function polls the NANDFC to wait for the basic operation to |
| 360 | * complete by checking the INT bit of config2 register. |
| 361 | */ |
Sascha Hauer | c110eaf | 2009-10-21 16:01:02 +0200 | [diff] [blame] | 362 | static void wait_op_done(struct mxc_nand_host *host, int useirq) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 363 | { |
Ivo Clarysse | a47bfd2 | 2010-04-08 16:16:51 +0200 | [diff] [blame] | 364 | int max_retries = 8000; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 365 | |
| 366 | if (useirq) { |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 367 | if (!host->devtype_data->check_int(host)) { |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 368 | INIT_COMPLETION(host->op_completion); |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 369 | irq_control(host, 1); |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 370 | wait_for_completion(&host->op_completion); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 371 | } |
| 372 | } else { |
| 373 | while (max_retries-- > 0) { |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 374 | if (host->devtype_data->check_int(host)) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 375 | break; |
Sascha Hauer | 7aaf28a | 2010-08-06 15:53:07 +0200 | [diff] [blame] | 376 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 377 | udelay(1); |
| 378 | } |
Roel Kluin | 43950a6 | 2009-06-04 16:24:59 +0200 | [diff] [blame] | 379 | if (max_retries < 0) |
Brian Norris | 0a32a10 | 2011-07-19 10:06:10 -0700 | [diff] [blame] | 380 | pr_debug("%s: INT not set\n", __func__); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 384 | static void send_cmd_v3(struct mxc_nand_host *host, uint16_t cmd, int useirq) |
| 385 | { |
| 386 | /* fill command */ |
| 387 | writel(cmd, NFC_V3_FLASH_CMD); |
| 388 | |
| 389 | /* send out command */ |
| 390 | writel(NFC_CMD, NFC_V3_LAUNCH); |
| 391 | |
| 392 | /* Wait for operation to complete */ |
| 393 | wait_op_done(host, useirq); |
| 394 | } |
| 395 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 396 | /* This function issues the specified command to the NAND device and |
| 397 | * waits for completion. */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 398 | 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] | 399 | { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 400 | pr_debug("send_cmd(host, 0x%x, %d)\n", cmd, useirq); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 401 | |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 402 | writew(cmd, NFC_V1_V2_FLASH_CMD); |
| 403 | writew(NFC_CMD, NFC_V1_V2_CONFIG2); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 404 | |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 405 | if (host->irqpending_quirk && (cmd == NAND_CMD_RESET)) { |
Ivo Clarysse | a47bfd2 | 2010-04-08 16:16:51 +0200 | [diff] [blame] | 406 | int max_retries = 100; |
| 407 | /* Reset completion is indicated by NFC_CONFIG2 */ |
| 408 | /* being set to 0 */ |
| 409 | while (max_retries-- > 0) { |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 410 | if (readw(NFC_V1_V2_CONFIG2) == 0) { |
Ivo Clarysse | a47bfd2 | 2010-04-08 16:16:51 +0200 | [diff] [blame] | 411 | break; |
| 412 | } |
| 413 | udelay(1); |
| 414 | } |
| 415 | if (max_retries < 0) |
Brian Norris | 0a32a10 | 2011-07-19 10:06:10 -0700 | [diff] [blame] | 416 | pr_debug("%s: RESET failed\n", __func__); |
Ivo Clarysse | a47bfd2 | 2010-04-08 16:16:51 +0200 | [diff] [blame] | 417 | } else { |
| 418 | /* Wait for operation to complete */ |
| 419 | wait_op_done(host, useirq); |
| 420 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 421 | } |
| 422 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 423 | static void send_addr_v3(struct mxc_nand_host *host, uint16_t addr, int islast) |
| 424 | { |
| 425 | /* fill address */ |
| 426 | writel(addr, NFC_V3_FLASH_ADDR0); |
| 427 | |
| 428 | /* send out address */ |
| 429 | writel(NFC_ADDR, NFC_V3_LAUNCH); |
| 430 | |
| 431 | wait_op_done(host, 0); |
| 432 | } |
| 433 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 434 | /* This function sends an address (or partial address) to the |
| 435 | * NAND device. The address is used to select the source/destination for |
| 436 | * a NAND command. */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 437 | 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] | 438 | { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 439 | pr_debug("send_addr(host, 0x%x %d)\n", addr, islast); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 440 | |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 441 | writew(addr, NFC_V1_V2_FLASH_ADDR); |
| 442 | writew(NFC_ADDR, NFC_V1_V2_CONFIG2); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 443 | |
| 444 | /* Wait for operation to complete */ |
Sascha Hauer | c110eaf | 2009-10-21 16:01:02 +0200 | [diff] [blame] | 445 | wait_op_done(host, islast); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 448 | static void send_page_v3(struct mtd_info *mtd, unsigned int ops) |
| 449 | { |
| 450 | struct nand_chip *nand_chip = mtd->priv; |
| 451 | struct mxc_nand_host *host = nand_chip->priv; |
| 452 | uint32_t tmp; |
| 453 | |
| 454 | tmp = readl(NFC_V3_CONFIG1); |
| 455 | tmp &= ~(7 << 4); |
| 456 | writel(tmp, NFC_V3_CONFIG1); |
| 457 | |
| 458 | /* transfer data from NFC ram to nand */ |
| 459 | writel(ops, NFC_V3_LAUNCH); |
| 460 | |
| 461 | wait_op_done(host, false); |
| 462 | } |
| 463 | |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 464 | static void send_page_v2(struct mtd_info *mtd, unsigned int ops) |
| 465 | { |
| 466 | struct nand_chip *nand_chip = mtd->priv; |
| 467 | struct mxc_nand_host *host = nand_chip->priv; |
| 468 | |
| 469 | /* NANDFC buffer 0 is used for page read/write */ |
| 470 | writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR); |
| 471 | |
| 472 | writew(ops, NFC_V1_V2_CONFIG2); |
| 473 | |
| 474 | /* Wait for operation to complete */ |
| 475 | wait_op_done(host, true); |
| 476 | } |
| 477 | |
| 478 | static void send_page_v1(struct mtd_info *mtd, unsigned int ops) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 479 | { |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame] | 480 | struct nand_chip *nand_chip = mtd->priv; |
| 481 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 482 | int bufs, i; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 483 | |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 484 | if (mtd->writesize > 512) |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 485 | bufs = 4; |
| 486 | else |
| 487 | bufs = 1; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 488 | |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 489 | for (i = 0; i < bufs; i++) { |
| 490 | |
| 491 | /* NANDFC buffer 0 is used for page read/write */ |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 492 | writew((host->active_cs << 4) | i, NFC_V1_V2_BUF_ADDR); |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 493 | |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 494 | writew(ops, NFC_V1_V2_CONFIG2); |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 495 | |
| 496 | /* Wait for operation to complete */ |
Sascha Hauer | c110eaf | 2009-10-21 16:01:02 +0200 | [diff] [blame] | 497 | wait_op_done(host, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 498 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 499 | } |
| 500 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 501 | static void send_read_id_v3(struct mxc_nand_host *host) |
| 502 | { |
| 503 | /* Read ID into main buffer */ |
| 504 | writel(NFC_ID, NFC_V3_LAUNCH); |
| 505 | |
| 506 | wait_op_done(host, true); |
| 507 | |
| 508 | memcpy(host->data_buf, host->main_area0, 16); |
| 509 | } |
| 510 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 511 | /* Request the NANDFC to perform a read of the NAND device ID. */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 512 | static void send_read_id_v1_v2(struct mxc_nand_host *host) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 513 | { |
| 514 | struct nand_chip *this = &host->nand; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 515 | |
| 516 | /* NANDFC buffer 0 is used for device ID output */ |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 517 | writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 518 | |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 519 | writew(NFC_ID, NFC_V1_V2_CONFIG2); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 520 | |
| 521 | /* Wait for operation to complete */ |
Sascha Hauer | c110eaf | 2009-10-21 16:01:02 +0200 | [diff] [blame] | 522 | wait_op_done(host, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 523 | |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 524 | memcpy(host->data_buf, host->main_area0, 16); |
John Ogness | f7b66e5 | 2010-06-18 18:59:47 +0200 | [diff] [blame] | 525 | |
| 526 | if (this->options & NAND_BUSWIDTH_16) { |
| 527 | /* compress the ID info */ |
| 528 | host->data_buf[1] = host->data_buf[2]; |
| 529 | host->data_buf[2] = host->data_buf[4]; |
| 530 | host->data_buf[3] = host->data_buf[6]; |
| 531 | host->data_buf[4] = host->data_buf[8]; |
| 532 | host->data_buf[5] = host->data_buf[10]; |
| 533 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 534 | } |
| 535 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 536 | static uint16_t get_dev_status_v3(struct mxc_nand_host *host) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 537 | { |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 538 | writew(NFC_STATUS, NFC_V3_LAUNCH); |
Sascha Hauer | c110eaf | 2009-10-21 16:01:02 +0200 | [diff] [blame] | 539 | wait_op_done(host, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 540 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 541 | return readl(NFC_V3_CONFIG1) >> 16; |
| 542 | } |
| 543 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 544 | /* This function requests the NANDFC to perform a read of the |
| 545 | * NAND device status and returns the current status. */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 546 | 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] | 547 | { |
Sascha Hauer | c29c607 | 2010-08-06 15:53:05 +0200 | [diff] [blame] | 548 | void __iomem *main_buf = host->main_area0; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 549 | uint32_t store; |
| 550 | uint16_t ret; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 551 | |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 552 | writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR); |
Sascha Hauer | c29c607 | 2010-08-06 15:53:05 +0200 | [diff] [blame] | 553 | |
| 554 | /* |
| 555 | * The device status is stored in main_area0. To |
| 556 | * prevent corruption of the buffer save the value |
| 557 | * and restore it afterwards. |
| 558 | */ |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 559 | store = readl(main_buf); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 560 | |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 561 | writew(NFC_STATUS, NFC_V1_V2_CONFIG2); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 562 | wait_op_done(host, true); |
| 563 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 564 | ret = readw(main_buf); |
Sascha Hauer | c29c607 | 2010-08-06 15:53:05 +0200 | [diff] [blame] | 565 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 566 | writel(store, main_buf); |
| 567 | |
| 568 | return ret; |
| 569 | } |
| 570 | |
| 571 | /* This functions is used by upper layer to checks if device is ready */ |
| 572 | static int mxc_nand_dev_ready(struct mtd_info *mtd) |
| 573 | { |
| 574 | /* |
| 575 | * NFC handles R/B internally. Therefore, this function |
| 576 | * always returns status as ready. |
| 577 | */ |
| 578 | return 1; |
| 579 | } |
| 580 | |
| 581 | static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode) |
| 582 | { |
| 583 | /* |
| 584 | * If HW ECC is enabled, we turn it on during init. There is |
| 585 | * no need to enable again here. |
| 586 | */ |
| 587 | } |
| 588 | |
Sascha Hauer | 94f77e5 | 2010-08-06 15:53:09 +0200 | [diff] [blame] | 589 | static int mxc_nand_correct_data_v1(struct mtd_info *mtd, u_char *dat, |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 590 | u_char *read_ecc, u_char *calc_ecc) |
| 591 | { |
| 592 | struct nand_chip *nand_chip = mtd->priv; |
| 593 | struct mxc_nand_host *host = nand_chip->priv; |
| 594 | |
| 595 | /* |
| 596 | * 1-Bit errors are automatically corrected in HW. No need for |
| 597 | * additional correction. 2-Bit errors cannot be corrected by |
| 598 | * HW ECC, so we need to return failure |
| 599 | */ |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 600 | uint16_t ecc_status = get_ecc_status_v1(host); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 601 | |
| 602 | if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 603 | pr_debug("MXC_NAND: HWECC uncorrectable 2-bit ECC error\n"); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 604 | return -1; |
| 605 | } |
| 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
Sascha Hauer | 94f77e5 | 2010-08-06 15:53:09 +0200 | [diff] [blame] | 610 | static int mxc_nand_correct_data_v2_v3(struct mtd_info *mtd, u_char *dat, |
| 611 | u_char *read_ecc, u_char *calc_ecc) |
| 612 | { |
| 613 | struct nand_chip *nand_chip = mtd->priv; |
| 614 | struct mxc_nand_host *host = nand_chip->priv; |
| 615 | u32 ecc_stat, err; |
| 616 | int no_subpages = 1; |
| 617 | int ret = 0; |
| 618 | u8 ecc_bit_mask, err_limit; |
| 619 | |
| 620 | ecc_bit_mask = (host->eccsize == 4) ? 0x7 : 0xf; |
| 621 | err_limit = (host->eccsize == 4) ? 0x4 : 0x8; |
| 622 | |
| 623 | no_subpages = mtd->writesize >> 9; |
| 624 | |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 625 | ecc_stat = host->devtype_data->get_ecc_status(host); |
Sascha Hauer | 94f77e5 | 2010-08-06 15:53:09 +0200 | [diff] [blame] | 626 | |
| 627 | do { |
| 628 | err = ecc_stat & ecc_bit_mask; |
| 629 | if (err > err_limit) { |
| 630 | printk(KERN_WARNING "UnCorrectable RS-ECC Error\n"); |
| 631 | return -1; |
| 632 | } else { |
| 633 | ret += err; |
| 634 | } |
| 635 | ecc_stat >>= 4; |
| 636 | } while (--no_subpages); |
| 637 | |
| 638 | mtd->ecc_stats.corrected += ret; |
| 639 | pr_debug("%d Symbol Correctable RS-ECC Error\n", ret); |
| 640 | |
| 641 | return ret; |
| 642 | } |
| 643 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 644 | static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, |
| 645 | u_char *ecc_code) |
| 646 | { |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | static u_char mxc_nand_read_byte(struct mtd_info *mtd) |
| 651 | { |
| 652 | struct nand_chip *nand_chip = mtd->priv; |
| 653 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 654 | uint8_t ret; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 655 | |
| 656 | /* Check for status request */ |
| 657 | if (host->status_request) |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 658 | return host->devtype_data->get_dev_status(host) & 0xFF; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 659 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 660 | ret = *(uint8_t *)(host->data_buf + host->buf_start); |
| 661 | host->buf_start++; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 662 | |
| 663 | return ret; |
| 664 | } |
| 665 | |
| 666 | static uint16_t mxc_nand_read_word(struct mtd_info *mtd) |
| 667 | { |
| 668 | struct nand_chip *nand_chip = mtd->priv; |
| 669 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 670 | uint16_t ret; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 671 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 672 | ret = *(uint16_t *)(host->data_buf + host->buf_start); |
| 673 | host->buf_start += 2; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 674 | |
| 675 | return ret; |
| 676 | } |
| 677 | |
| 678 | /* Write data of length len to buffer buf. The data to be |
| 679 | * written on NAND Flash is first copied to RAMbuffer. After the Data Input |
| 680 | * Operation by the NFC, the data is written to NAND Flash */ |
| 681 | static void mxc_nand_write_buf(struct mtd_info *mtd, |
| 682 | const u_char *buf, int len) |
| 683 | { |
| 684 | struct nand_chip *nand_chip = mtd->priv; |
| 685 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 686 | u16 col = host->buf_start; |
| 687 | int n = mtd->oobsize + mtd->writesize - col; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 688 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 689 | n = min(n, len); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 690 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 691 | memcpy(host->data_buf + col, buf, n); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 692 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 693 | host->buf_start += n; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /* Read the data buffer from the NAND Flash. To read the data from NAND |
| 697 | * Flash first the data output cycle is initiated by the NFC, which copies |
| 698 | * the data to RAMbuffer. This data of length len is then copied to buffer buf. |
| 699 | */ |
| 700 | static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 701 | { |
| 702 | struct nand_chip *nand_chip = mtd->priv; |
| 703 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 704 | u16 col = host->buf_start; |
| 705 | int n = mtd->oobsize + mtd->writesize - col; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 706 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 707 | n = min(n, len); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 708 | |
Baruch Siach | 5d9d993 | 2011-03-02 16:47:55 +0200 | [diff] [blame] | 709 | memcpy(buf, host->data_buf + col, n); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 710 | |
Baruch Siach | 5d9d993 | 2011-03-02 16:47:55 +0200 | [diff] [blame] | 711 | host->buf_start += n; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | /* Used by the upper layer to verify the data in NAND Flash |
| 715 | * with the data in the buf. */ |
| 716 | static int mxc_nand_verify_buf(struct mtd_info *mtd, |
| 717 | const u_char *buf, int len) |
| 718 | { |
| 719 | return -EFAULT; |
| 720 | } |
| 721 | |
| 722 | /* This function is used by upper layer for select and |
| 723 | * deselect of the NAND chip */ |
| 724 | static void mxc_nand_select_chip(struct mtd_info *mtd, int chip) |
| 725 | { |
| 726 | struct nand_chip *nand_chip = mtd->priv; |
| 727 | struct mxc_nand_host *host = nand_chip->priv; |
| 728 | |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 729 | if (chip == -1) { |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 730 | /* Disable the NFC clock */ |
| 731 | if (host->clk_act) { |
| 732 | clk_disable(host->clk); |
| 733 | host->clk_act = 0; |
| 734 | } |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 735 | return; |
| 736 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 737 | |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 738 | if (!host->clk_act) { |
| 739 | /* Enable the NFC clock */ |
| 740 | clk_enable(host->clk); |
| 741 | host->clk_act = 1; |
| 742 | } |
| 743 | |
| 744 | if (nfc_is_v21()) { |
| 745 | host->active_cs = chip; |
| 746 | writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 750 | /* |
| 751 | * Function to transfer data to/from spare area. |
| 752 | */ |
| 753 | static void copy_spare(struct mtd_info *mtd, bool bfrom) |
| 754 | { |
| 755 | struct nand_chip *this = mtd->priv; |
| 756 | struct mxc_nand_host *host = this->priv; |
| 757 | u16 i, j; |
| 758 | u16 n = mtd->writesize >> 9; |
| 759 | u8 *d = host->data_buf + mtd->writesize; |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 760 | u8 *s = host->spare0; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 761 | u16 t = host->spare_len; |
| 762 | |
| 763 | j = (mtd->oobsize / n >> 1) << 1; |
| 764 | |
| 765 | if (bfrom) { |
| 766 | for (i = 0; i < n - 1; i++) |
| 767 | memcpy(d + i * j, s + i * t, j); |
| 768 | |
| 769 | /* the last section */ |
| 770 | memcpy(d + i * j, s + i * t, mtd->oobsize - i * j); |
| 771 | } else { |
| 772 | for (i = 0; i < n - 1; i++) |
| 773 | memcpy(&s[i * t], &d[i * j], j); |
| 774 | |
| 775 | /* the last section */ |
| 776 | memcpy(&s[i * t], &d[i * j], mtd->oobsize - i * j); |
| 777 | } |
| 778 | } |
| 779 | |
Sascha Hauer | a3e65b6 | 2009-06-02 11:47:59 +0200 | [diff] [blame] | 780 | 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] | 781 | { |
| 782 | struct nand_chip *nand_chip = mtd->priv; |
| 783 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 784 | |
| 785 | /* Write out column address, if necessary */ |
| 786 | if (column != -1) { |
| 787 | /* |
| 788 | * MXC NANDFC can only perform full page+spare or |
| 789 | * spare-only read/write. When the upper layers |
Gilles Espinasse | 177b241 | 2011-01-09 08:59:49 +0100 | [diff] [blame] | 790 | * perform a read/write buf operation, the saved column |
| 791 | * address is used to index into the full page. |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 792 | */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 793 | host->devtype_data->send_addr(host, 0, page_addr == -1); |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame] | 794 | if (mtd->writesize > 512) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 795 | /* another col addr cycle for 2k page */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 796 | host->devtype_data->send_addr(host, 0, false); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /* Write out page address, if necessary */ |
| 800 | if (page_addr != -1) { |
| 801 | /* paddr_0 - p_addr_7 */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 802 | host->devtype_data->send_addr(host, (page_addr & 0xff), false); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 803 | |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame] | 804 | if (mtd->writesize > 512) { |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 805 | if (mtd->size >= 0x10000000) { |
| 806 | /* paddr_8 - paddr_15 */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 807 | host->devtype_data->send_addr(host, |
| 808 | (page_addr >> 8) & 0xff, |
| 809 | false); |
| 810 | host->devtype_data->send_addr(host, |
| 811 | (page_addr >> 16) & 0xff, |
| 812 | true); |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 813 | } else |
| 814 | /* paddr_8 - paddr_15 */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 815 | host->devtype_data->send_addr(host, |
| 816 | (page_addr >> 8) & 0xff, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 817 | } else { |
| 818 | /* One more address cycle for higher density devices */ |
| 819 | if (mtd->size >= 0x4000000) { |
| 820 | /* paddr_8 - paddr_15 */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 821 | host->devtype_data->send_addr(host, |
| 822 | (page_addr >> 8) & 0xff, |
| 823 | false); |
| 824 | host->devtype_data->send_addr(host, |
| 825 | (page_addr >> 16) & 0xff, |
| 826 | true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 827 | } else |
| 828 | /* paddr_8 - paddr_15 */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 829 | host->devtype_data->send_addr(host, |
| 830 | (page_addr >> 8) & 0xff, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 831 | } |
| 832 | } |
Sascha Hauer | a3e65b6 | 2009-06-02 11:47:59 +0200 | [diff] [blame] | 833 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 834 | |
Sascha Hauer | 6e85dfd | 2010-08-06 15:53:10 +0200 | [diff] [blame] | 835 | /* |
| 836 | * v2 and v3 type controllers can do 4bit or 8bit ecc depending |
| 837 | * on how much oob the nand chip has. For 8bit ecc we need at least |
| 838 | * 26 bytes of oob data per 512 byte block. |
| 839 | */ |
| 840 | static int get_eccsize(struct mtd_info *mtd) |
| 841 | { |
| 842 | int oobbytes_per_512 = 0; |
| 843 | |
| 844 | oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize; |
| 845 | |
| 846 | if (oobbytes_per_512 < 26) |
| 847 | return 4; |
| 848 | else |
| 849 | return 8; |
| 850 | } |
| 851 | |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 852 | static void preset_v1(struct mtd_info *mtd) |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 853 | { |
| 854 | struct nand_chip *nand_chip = mtd->priv; |
| 855 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | b8db2f5 | 2010-08-09 15:04:19 +0200 | [diff] [blame] | 856 | uint16_t config1 = 0; |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 857 | |
Sascha Hauer | b8db2f5 | 2010-08-09 15:04:19 +0200 | [diff] [blame] | 858 | if (nand_chip->ecc.mode == NAND_ECC_HW) |
| 859 | config1 |= NFC_V1_V2_CONFIG1_ECC_EN; |
| 860 | |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 861 | if (!host->irqpending_quirk) |
| 862 | config1 |= NFC_V1_V2_CONFIG1_INT_MSK; |
| 863 | |
| 864 | host->eccsize = 1; |
| 865 | |
| 866 | writew(config1, NFC_V1_V2_CONFIG1); |
| 867 | /* preset operation */ |
| 868 | |
| 869 | /* Unlock the internal RAM Buffer */ |
| 870 | writew(0x2, NFC_V1_V2_CONFIG); |
| 871 | |
| 872 | /* Blocks to be unlocked */ |
| 873 | writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR); |
| 874 | writew(0xffff, NFC_V1_UNLOCKEND_BLKADDR); |
| 875 | |
| 876 | /* Unlock Block Command for given address range */ |
| 877 | writew(0x4, NFC_V1_V2_WRPROT); |
| 878 | } |
| 879 | |
| 880 | static void preset_v2(struct mtd_info *mtd) |
| 881 | { |
| 882 | struct nand_chip *nand_chip = mtd->priv; |
| 883 | struct mxc_nand_host *host = nand_chip->priv; |
| 884 | uint16_t config1 = 0; |
| 885 | |
| 886 | if (nand_chip->ecc.mode == NAND_ECC_HW) |
| 887 | config1 |= NFC_V1_V2_CONFIG1_ECC_EN; |
| 888 | |
| 889 | config1 |= NFC_V2_CONFIG1_FP_INT; |
Sascha Hauer | b8db2f5 | 2010-08-09 15:04:19 +0200 | [diff] [blame] | 890 | |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 891 | if (!host->irqpending_quirk) |
Sascha Hauer | b8db2f5 | 2010-08-09 15:04:19 +0200 | [diff] [blame] | 892 | config1 |= NFC_V1_V2_CONFIG1_INT_MSK; |
Sascha Hauer | 6e85dfd | 2010-08-06 15:53:10 +0200 | [diff] [blame] | 893 | |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 894 | if (mtd->writesize) { |
Sascha Hauer | b8db2f5 | 2010-08-09 15:04:19 +0200 | [diff] [blame] | 895 | uint16_t pages_per_block = mtd->erasesize / mtd->writesize; |
| 896 | |
Sascha Hauer | 6e85dfd | 2010-08-06 15:53:10 +0200 | [diff] [blame] | 897 | host->eccsize = get_eccsize(mtd); |
| 898 | if (host->eccsize == 4) |
Sascha Hauer | b8db2f5 | 2010-08-09 15:04:19 +0200 | [diff] [blame] | 899 | config1 |= NFC_V2_CONFIG1_ECC_MODE_4; |
| 900 | |
| 901 | config1 |= NFC_V2_CONFIG1_PPB(ffs(pages_per_block) - 6); |
Sascha Hauer | 6e85dfd | 2010-08-06 15:53:10 +0200 | [diff] [blame] | 902 | } else { |
| 903 | host->eccsize = 1; |
| 904 | } |
| 905 | |
Sascha Hauer | b8db2f5 | 2010-08-09 15:04:19 +0200 | [diff] [blame] | 906 | writew(config1, NFC_V1_V2_CONFIG1); |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 907 | /* preset operation */ |
| 908 | |
| 909 | /* Unlock the internal RAM Buffer */ |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 910 | writew(0x2, NFC_V1_V2_CONFIG); |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 911 | |
| 912 | /* Blocks to be unlocked */ |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 913 | writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR0); |
| 914 | writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR1); |
| 915 | writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR2); |
| 916 | writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR3); |
| 917 | writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR0); |
| 918 | writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR1); |
| 919 | writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR2); |
| 920 | writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR3); |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 921 | |
| 922 | /* Unlock Block Command for given address range */ |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 923 | writew(0x4, NFC_V1_V2_WRPROT); |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 924 | } |
| 925 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 926 | static void preset_v3(struct mtd_info *mtd) |
| 927 | { |
| 928 | struct nand_chip *chip = mtd->priv; |
| 929 | struct mxc_nand_host *host = chip->priv; |
| 930 | uint32_t config2, config3; |
| 931 | int i, addr_phases; |
| 932 | |
| 933 | writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1); |
| 934 | writel(NFC_V3_IPC_CREQ, NFC_V3_IPC); |
| 935 | |
| 936 | /* Unlock the internal RAM Buffer */ |
| 937 | writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK, |
| 938 | NFC_V3_WRPROT); |
| 939 | |
| 940 | /* Blocks to be unlocked */ |
| 941 | for (i = 0; i < NAND_MAX_CHIPS; i++) |
| 942 | writel(0x0 | (0xffff << 16), |
| 943 | NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2)); |
| 944 | |
| 945 | writel(0, NFC_V3_IPC); |
| 946 | |
| 947 | config2 = NFC_V3_CONFIG2_ONE_CYCLE | |
| 948 | NFC_V3_CONFIG2_2CMD_PHASES | |
| 949 | NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) | |
| 950 | NFC_V3_CONFIG2_ST_CMD(0x70) | |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 951 | NFC_V3_CONFIG2_INT_MSK | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 952 | NFC_V3_CONFIG2_NUM_ADDR_PHASE0; |
| 953 | |
| 954 | if (chip->ecc.mode == NAND_ECC_HW) |
| 955 | config2 |= NFC_V3_CONFIG2_ECC_EN; |
| 956 | |
| 957 | addr_phases = fls(chip->pagemask) >> 3; |
| 958 | |
| 959 | if (mtd->writesize == 2048) { |
| 960 | config2 |= NFC_V3_CONFIG2_PS_2048; |
| 961 | config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases); |
| 962 | } else if (mtd->writesize == 4096) { |
| 963 | config2 |= NFC_V3_CONFIG2_PS_4096; |
| 964 | config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases); |
| 965 | } else { |
| 966 | config2 |= NFC_V3_CONFIG2_PS_512; |
| 967 | config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1); |
| 968 | } |
| 969 | |
| 970 | if (mtd->writesize) { |
| 971 | config2 |= NFC_V3_CONFIG2_PPB(ffs(mtd->erasesize / mtd->writesize) - 6); |
| 972 | host->eccsize = get_eccsize(mtd); |
| 973 | if (host->eccsize == 8) |
| 974 | config2 |= NFC_V3_CONFIG2_ECC_MODE_8; |
| 975 | } |
| 976 | |
| 977 | writel(config2, NFC_V3_CONFIG2); |
| 978 | |
| 979 | config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) | |
| 980 | NFC_V3_CONFIG3_NO_SDMA | |
| 981 | NFC_V3_CONFIG3_RBB_MODE | |
| 982 | NFC_V3_CONFIG3_SBB(6) | /* Reset default */ |
| 983 | NFC_V3_CONFIG3_ADD_OP(0); |
| 984 | |
| 985 | if (!(chip->options & NAND_BUSWIDTH_16)) |
| 986 | config3 |= NFC_V3_CONFIG3_FW8; |
| 987 | |
| 988 | writel(config3, NFC_V3_CONFIG3); |
| 989 | |
| 990 | writel(0, NFC_V3_DELAY_LINE); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 991 | } |
| 992 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 993 | /* Used by the upper layer to write command to NAND Flash for |
| 994 | * different operations to be carried out on NAND Flash */ |
| 995 | static void mxc_nand_command(struct mtd_info *mtd, unsigned command, |
| 996 | int column, int page_addr) |
| 997 | { |
| 998 | struct nand_chip *nand_chip = mtd->priv; |
| 999 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1000 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 1001 | pr_debug("mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n", |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1002 | command, column, page_addr); |
| 1003 | |
| 1004 | /* Reset command state information */ |
| 1005 | host->status_request = false; |
| 1006 | |
| 1007 | /* Command pre-processing step */ |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1008 | switch (command) { |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 1009 | case NAND_CMD_RESET: |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1010 | host->devtype_data->preset(mtd); |
| 1011 | host->devtype_data->send_cmd(host, command, false); |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 1012 | break; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1013 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1014 | case NAND_CMD_STATUS: |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 1015 | host->buf_start = 0; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1016 | host->status_request = true; |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1017 | |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1018 | host->devtype_data->send_cmd(host, command, true); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1019 | mxc_do_addr_cycle(mtd, column, page_addr); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1020 | break; |
| 1021 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1022 | case NAND_CMD_READ0: |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1023 | case NAND_CMD_READOOB: |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1024 | if (command == NAND_CMD_READ0) |
| 1025 | host->buf_start = column; |
| 1026 | else |
| 1027 | host->buf_start = column + mtd->writesize; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 1028 | |
Sascha Hauer | 5ea3202 | 2010-04-27 15:24:01 +0200 | [diff] [blame] | 1029 | command = NAND_CMD_READ0; /* only READ0 is valid */ |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1030 | |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1031 | host->devtype_data->send_cmd(host, command, false); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1032 | mxc_do_addr_cycle(mtd, column, page_addr); |
| 1033 | |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame] | 1034 | if (mtd->writesize > 512) |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1035 | host->devtype_data->send_cmd(host, |
| 1036 | NAND_CMD_READSTART, true); |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 1037 | |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1038 | host->devtype_data->send_page(mtd, NFC_OUTPUT); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1039 | |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 1040 | memcpy(host->data_buf, host->main_area0, mtd->writesize); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1041 | copy_spare(mtd, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1042 | break; |
| 1043 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1044 | case NAND_CMD_SEQIN: |
Sascha Hauer | 5ea3202 | 2010-04-27 15:24:01 +0200 | [diff] [blame] | 1045 | if (column >= mtd->writesize) |
| 1046 | /* call ourself to read a page */ |
| 1047 | mxc_nand_command(mtd, NAND_CMD_READ0, 0, page_addr); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1048 | |
Sascha Hauer | 5ea3202 | 2010-04-27 15:24:01 +0200 | [diff] [blame] | 1049 | host->buf_start = column; |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1050 | |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1051 | host->devtype_data->send_cmd(host, command, false); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1052 | mxc_do_addr_cycle(mtd, column, page_addr); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1053 | break; |
| 1054 | |
| 1055 | case NAND_CMD_PAGEPROG: |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 1056 | memcpy(host->main_area0, host->data_buf, mtd->writesize); |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 1057 | copy_spare(mtd, false); |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1058 | host->devtype_data->send_page(mtd, NFC_INPUT); |
| 1059 | host->devtype_data->send_cmd(host, command, true); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1060 | mxc_do_addr_cycle(mtd, column, page_addr); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1061 | break; |
| 1062 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1063 | case NAND_CMD_READID: |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1064 | host->devtype_data->send_cmd(host, command, true); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1065 | mxc_do_addr_cycle(mtd, column, page_addr); |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1066 | host->devtype_data->send_read_id(host); |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 1067 | host->buf_start = column; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1068 | break; |
| 1069 | |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1070 | case NAND_CMD_ERASE1: |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1071 | case NAND_CMD_ERASE2: |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1072 | host->devtype_data->send_cmd(host, command, false); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1073 | mxc_do_addr_cycle(mtd, column, page_addr); |
| 1074 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1075 | break; |
| 1076 | } |
| 1077 | } |
| 1078 | |
Sascha Hauer | f137205 | 2009-10-21 14:25:27 +0200 | [diff] [blame] | 1079 | /* |
| 1080 | * The generic flash bbt decriptors overlap with our ecc |
| 1081 | * hardware, so define some i.MX specific ones. |
| 1082 | */ |
| 1083 | static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' }; |
| 1084 | static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' }; |
| 1085 | |
| 1086 | static struct nand_bbt_descr bbt_main_descr = { |
| 1087 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 1088 | | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, |
| 1089 | .offs = 0, |
| 1090 | .len = 4, |
| 1091 | .veroffs = 4, |
| 1092 | .maxblocks = 4, |
| 1093 | .pattern = bbt_pattern, |
| 1094 | }; |
| 1095 | |
| 1096 | static struct nand_bbt_descr bbt_mirror_descr = { |
| 1097 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 1098 | | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, |
| 1099 | .offs = 0, |
| 1100 | .len = 4, |
| 1101 | .veroffs = 4, |
| 1102 | .maxblocks = 4, |
| 1103 | .pattern = mirror_pattern, |
| 1104 | }; |
| 1105 | |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1106 | /* v1: i.MX21, i.MX27, i.MX31 */ |
| 1107 | static const struct mxc_nand_devtype_data imx21_nand_devtype_data = { |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 1108 | .preset = preset_v1, |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1109 | .send_cmd = send_cmd_v1_v2, |
| 1110 | .send_addr = send_addr_v1_v2, |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 1111 | .send_page = send_page_v1, |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1112 | .send_read_id = send_read_id_v1_v2, |
| 1113 | .get_dev_status = get_dev_status_v1_v2, |
| 1114 | .check_int = check_int_v1_v2, |
| 1115 | .irq_control = irq_control_v1_v2, |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 1116 | .get_ecc_status = get_ecc_status_v1, |
Uwe Kleine-König | 6dcdf99 | 2012-04-23 11:23:37 +0200 | [diff] [blame^] | 1117 | .ecclayout_512 = &nandv1_hw_eccoob_smallpage, |
| 1118 | .ecclayout_2k = &nandv1_hw_eccoob_largepage, |
| 1119 | .ecclayout_4k = &nandv1_hw_eccoob_smallpage, /* XXX: needs fix */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1120 | }; |
| 1121 | |
| 1122 | /* v21: i.MX25, i.MX35 */ |
| 1123 | static const struct mxc_nand_devtype_data imx25_nand_devtype_data = { |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 1124 | .preset = preset_v2, |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1125 | .send_cmd = send_cmd_v1_v2, |
| 1126 | .send_addr = send_addr_v1_v2, |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 1127 | .send_page = send_page_v2, |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1128 | .send_read_id = send_read_id_v1_v2, |
| 1129 | .get_dev_status = get_dev_status_v1_v2, |
| 1130 | .check_int = check_int_v1_v2, |
| 1131 | .irq_control = irq_control_v1_v2, |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 1132 | .get_ecc_status = get_ecc_status_v2, |
Uwe Kleine-König | 6dcdf99 | 2012-04-23 11:23:37 +0200 | [diff] [blame^] | 1133 | .ecclayout_512 = &nandv2_hw_eccoob_smallpage, |
| 1134 | .ecclayout_2k = &nandv2_hw_eccoob_largepage, |
| 1135 | .ecclayout_4k = &nandv2_hw_eccoob_4k, |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1136 | }; |
| 1137 | |
| 1138 | /* v3: i.MX51, i.MX53 */ |
| 1139 | static const struct mxc_nand_devtype_data imx51_nand_devtype_data = { |
| 1140 | .preset = preset_v3, |
| 1141 | .send_cmd = send_cmd_v3, |
| 1142 | .send_addr = send_addr_v3, |
| 1143 | .send_page = send_page_v3, |
| 1144 | .send_read_id = send_read_id_v3, |
| 1145 | .get_dev_status = get_dev_status_v3, |
| 1146 | .check_int = check_int_v3, |
| 1147 | .irq_control = irq_control_v3, |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 1148 | .get_ecc_status = get_ecc_status_v3, |
Uwe Kleine-König | 6dcdf99 | 2012-04-23 11:23:37 +0200 | [diff] [blame^] | 1149 | .ecclayout_512 = &nandv2_hw_eccoob_smallpage, |
| 1150 | .ecclayout_2k = &nandv2_hw_eccoob_largepage, |
| 1151 | .ecclayout_4k = &nandv2_hw_eccoob_smallpage, /* XXX: needs fix */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1152 | }; |
| 1153 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1154 | static int __init mxcnd_probe(struct platform_device *pdev) |
| 1155 | { |
| 1156 | struct nand_chip *this; |
| 1157 | struct mtd_info *mtd; |
| 1158 | struct mxc_nand_platform_data *pdata = pdev->dev.platform_data; |
| 1159 | struct mxc_nand_host *host; |
| 1160 | struct resource *res; |
Dmitry Eremin-Solenikov | d4ed8f1 | 2011-06-02 18:00:43 +0400 | [diff] [blame] | 1161 | int err = 0; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1162 | |
| 1163 | /* Allocate memory for MTD device structure and private data */ |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 1164 | host = kzalloc(sizeof(struct mxc_nand_host) + NAND_MAX_PAGESIZE + |
| 1165 | NAND_MAX_OOBSIZE, GFP_KERNEL); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1166 | if (!host) |
| 1167 | return -ENOMEM; |
| 1168 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 1169 | host->data_buf = (uint8_t *)(host + 1); |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 1170 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1171 | host->dev = &pdev->dev; |
| 1172 | /* structures must be linked */ |
| 1173 | this = &host->nand; |
| 1174 | mtd = &host->mtd; |
| 1175 | mtd->priv = this; |
| 1176 | mtd->owner = THIS_MODULE; |
David Brownell | 87f39f0 | 2009-03-26 00:42:50 -0700 | [diff] [blame] | 1177 | mtd->dev.parent = &pdev->dev; |
Sascha Hauer | 1fbff0a | 2009-10-21 16:06:27 +0200 | [diff] [blame] | 1178 | mtd->name = DRIVER_NAME; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1179 | |
| 1180 | /* 50 us command delay time */ |
| 1181 | this->chip_delay = 5; |
| 1182 | |
| 1183 | this->priv = host; |
| 1184 | this->dev_ready = mxc_nand_dev_ready; |
| 1185 | this->cmdfunc = mxc_nand_command; |
| 1186 | this->select_chip = mxc_nand_select_chip; |
| 1187 | this->read_byte = mxc_nand_read_byte; |
| 1188 | this->read_word = mxc_nand_read_word; |
| 1189 | this->write_buf = mxc_nand_write_buf; |
| 1190 | this->read_buf = mxc_nand_read_buf; |
| 1191 | this->verify_buf = mxc_nand_verify_buf; |
| 1192 | |
Sascha Hauer | e65fb00 | 2009-02-16 14:29:10 +0100 | [diff] [blame] | 1193 | host->clk = clk_get(&pdev->dev, "nfc"); |
Vladimir Barinov | 8541c11 | 2009-04-23 15:47:22 +0400 | [diff] [blame] | 1194 | if (IS_ERR(host->clk)) { |
| 1195 | err = PTR_ERR(host->clk); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1196 | goto eclk; |
Vladimir Barinov | 8541c11 | 2009-04-23 15:47:22 +0400 | [diff] [blame] | 1197 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1198 | |
| 1199 | clk_enable(host->clk); |
| 1200 | host->clk_act = 1; |
| 1201 | |
| 1202 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1203 | if (!res) { |
| 1204 | err = -ENODEV; |
| 1205 | goto eres; |
| 1206 | } |
| 1207 | |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 1208 | host->base = ioremap(res->start, resource_size(res)); |
| 1209 | if (!host->base) { |
Vladimir Barinov | 8541c11 | 2009-04-23 15:47:22 +0400 | [diff] [blame] | 1210 | err = -ENOMEM; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1211 | goto eres; |
| 1212 | } |
| 1213 | |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 1214 | host->main_area0 = host->base; |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 1215 | |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1216 | if (nfc_is_v1()) { |
| 1217 | host->devtype_data = &imx21_nand_devtype_data; |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 1218 | if (cpu_is_mx21()) |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 1219 | host->irqpending_quirk = 1; |
Sascha Hauer | 938cf99 | 2010-08-06 15:53:04 +0200 | [diff] [blame] | 1220 | host->regs = host->base + 0xe00; |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 1221 | host->spare0 = host->base + 0x800; |
| 1222 | host->spare_len = 16; |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 1223 | this->ecc.bytes = 3; |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 1224 | host->eccsize = 1; |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1225 | } else if (nfc_is_v21()) { |
| 1226 | host->devtype_data = &imx25_nand_devtype_data; |
| 1227 | host->regs = host->base + 0x1e00; |
| 1228 | host->spare0 = host->base + 0x1000; |
| 1229 | host->spare_len = 64; |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1230 | this->ecc.bytes = 9; |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 1231 | } else if (nfc_is_v3_2()) { |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1232 | host->devtype_data = &imx51_nand_devtype_data; |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 1233 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1234 | if (!res) { |
| 1235 | err = -ENODEV; |
| 1236 | goto eirq; |
| 1237 | } |
| 1238 | host->regs_ip = ioremap(res->start, resource_size(res)); |
| 1239 | if (!host->regs_ip) { |
| 1240 | err = -ENOMEM; |
| 1241 | goto eirq; |
| 1242 | } |
| 1243 | host->regs_axi = host->base + 0x1e00; |
| 1244 | host->spare0 = host->base + 0x1000; |
| 1245 | host->spare_len = 64; |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 1246 | } else |
| 1247 | BUG(); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1248 | |
Sascha Hauer | 13e1add | 2009-10-21 10:39:05 +0200 | [diff] [blame] | 1249 | this->ecc.size = 512; |
Uwe Kleine-König | 6dcdf99 | 2012-04-23 11:23:37 +0200 | [diff] [blame^] | 1250 | this->ecc.layout = host->devtype_data->ecclayout_512; |
Sascha Hauer | 13e1add | 2009-10-21 10:39:05 +0200 | [diff] [blame] | 1251 | |
| 1252 | if (pdata->hw_ecc) { |
| 1253 | this->ecc.calculate = mxc_nand_calculate_ecc; |
| 1254 | this->ecc.hwctl = mxc_nand_enable_hwecc; |
Sascha Hauer | 94f77e5 | 2010-08-06 15:53:09 +0200 | [diff] [blame] | 1255 | if (nfc_is_v1()) |
| 1256 | this->ecc.correct = mxc_nand_correct_data_v1; |
| 1257 | else |
| 1258 | this->ecc.correct = mxc_nand_correct_data_v2_v3; |
Sascha Hauer | 13e1add | 2009-10-21 10:39:05 +0200 | [diff] [blame] | 1259 | this->ecc.mode = NAND_ECC_HW; |
Sascha Hauer | 13e1add | 2009-10-21 10:39:05 +0200 | [diff] [blame] | 1260 | } else { |
| 1261 | this->ecc.mode = NAND_ECC_SOFT; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1262 | } |
| 1263 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1264 | /* NAND bus width determines access funtions used by upper layer */ |
Sascha Hauer | 13e1add | 2009-10-21 10:39:05 +0200 | [diff] [blame] | 1265 | if (pdata->width == 2) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1266 | this->options |= NAND_BUSWIDTH_16; |
Sascha Hauer | 13e1add | 2009-10-21 10:39:05 +0200 | [diff] [blame] | 1267 | |
Sascha Hauer | f137205 | 2009-10-21 14:25:27 +0200 | [diff] [blame] | 1268 | if (pdata->flash_bbt) { |
| 1269 | this->bbt_td = &bbt_main_descr; |
| 1270 | this->bbt_md = &bbt_mirror_descr; |
| 1271 | /* update flash based bbt */ |
Brian Norris | bb9ebd4 | 2011-05-31 16:31:23 -0700 | [diff] [blame] | 1272 | this->bbt_options |= NAND_BBT_USE_FLASH; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1273 | } |
| 1274 | |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 1275 | init_completion(&host->op_completion); |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 1276 | |
| 1277 | host->irq = platform_get_irq(pdev, 0); |
| 1278 | |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 1279 | /* |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1280 | * Use host->devtype_data->irq_control() here instead of irq_control() |
| 1281 | * because we must not disable_irq_nosync without having requested the |
| 1282 | * irq. |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 1283 | */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1284 | host->devtype_data->irq_control(host, 0); |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 1285 | |
Ivo Clarysse | a47bfd2 | 2010-04-08 16:16:51 +0200 | [diff] [blame] | 1286 | 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] | 1287 | if (err) |
| 1288 | goto eirq; |
| 1289 | |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 1290 | /* |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 1291 | * Now that we "own" the interrupt make sure the interrupt mask bit is |
| 1292 | * cleared on i.MX21. Otherwise we can't read the interrupt status bit |
| 1293 | * on this machine. |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 1294 | */ |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 1295 | if (host->irqpending_quirk) { |
| 1296 | disable_irq_nosync(host->irq); |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1297 | host->devtype_data->irq_control(host, 1); |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 1298 | } |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 1299 | |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 1300 | /* first scan to find the device and get the page size */ |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 1301 | if (nand_scan_ident(mtd, nfc_is_v21() ? 4 : 1, NULL)) { |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 1302 | err = -ENXIO; |
| 1303 | goto escan; |
| 1304 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1305 | |
Sascha Hauer | 6e85dfd | 2010-08-06 15:53:10 +0200 | [diff] [blame] | 1306 | /* Call preset again, with correct writesize this time */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1307 | host->devtype_data->preset(mtd); |
Sascha Hauer | 6e85dfd | 2010-08-06 15:53:10 +0200 | [diff] [blame] | 1308 | |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame] | 1309 | if (mtd->writesize == 2048) |
Uwe Kleine-König | 6dcdf99 | 2012-04-23 11:23:37 +0200 | [diff] [blame^] | 1310 | this->ecc.layout = host->devtype_data->ecclayout_2k; |
| 1311 | else if (mtd->writesize == 4096) |
| 1312 | this->ecc.layout = host->devtype_data->ecclayout_4k; |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 1313 | |
| 1314 | /* second phase scan */ |
| 1315 | if (nand_scan_tail(mtd)) { |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1316 | err = -ENXIO; |
| 1317 | goto escan; |
| 1318 | } |
| 1319 | |
Mike Dunn | 6a918ba | 2012-03-11 14:21:11 -0700 | [diff] [blame] | 1320 | if (this->ecc.mode == NAND_ECC_HW) { |
| 1321 | if (nfc_is_v1()) |
| 1322 | this->ecc.strength = 1; |
| 1323 | else |
| 1324 | this->ecc.strength = (host->eccsize == 4) ? 4 : 8; |
| 1325 | } |
| 1326 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1327 | /* Register the partitions */ |
Artem Bityutskiy | 42d7fbe | 2012-03-09 19:24:26 +0200 | [diff] [blame] | 1328 | mtd_device_parse_register(mtd, part_probes, NULL, pdata->parts, |
| 1329 | pdata->nr_parts); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1330 | |
| 1331 | platform_set_drvdata(pdev, host); |
| 1332 | |
| 1333 | return 0; |
| 1334 | |
| 1335 | escan: |
Magnus Lilja | b258fd8 | 2009-05-08 21:57:47 +0200 | [diff] [blame] | 1336 | free_irq(host->irq, host); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1337 | eirq: |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 1338 | if (host->regs_ip) |
| 1339 | iounmap(host->regs_ip); |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 1340 | iounmap(host->base); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1341 | eres: |
| 1342 | clk_put(host->clk); |
| 1343 | eclk: |
| 1344 | kfree(host); |
| 1345 | |
| 1346 | return err; |
| 1347 | } |
| 1348 | |
Uwe Kleine-König | 51eeb87 | 2009-12-07 09:44:05 +0000 | [diff] [blame] | 1349 | static int __devexit mxcnd_remove(struct platform_device *pdev) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1350 | { |
| 1351 | struct mxc_nand_host *host = platform_get_drvdata(pdev); |
| 1352 | |
| 1353 | clk_put(host->clk); |
| 1354 | |
| 1355 | platform_set_drvdata(pdev, NULL); |
| 1356 | |
| 1357 | nand_release(&host->mtd); |
Magnus Lilja | b258fd8 | 2009-05-08 21:57:47 +0200 | [diff] [blame] | 1358 | free_irq(host->irq, host); |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 1359 | if (host->regs_ip) |
| 1360 | iounmap(host->regs_ip); |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 1361 | iounmap(host->base); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1362 | kfree(host); |
| 1363 | |
| 1364 | return 0; |
| 1365 | } |
| 1366 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1367 | static struct platform_driver mxcnd_driver = { |
| 1368 | .driver = { |
| 1369 | .name = DRIVER_NAME, |
Uwe Kleine-König | 8d1fd16 | 2012-04-23 11:23:33 +0200 | [diff] [blame] | 1370 | .owner = THIS_MODULE, |
Eric Bénard | 04dd0d3 | 2010-06-17 20:59:04 +0200 | [diff] [blame] | 1371 | }, |
Uwe Kleine-König | daa0f15 | 2009-11-24 22:07:08 +0100 | [diff] [blame] | 1372 | .remove = __devexit_p(mxcnd_remove), |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1373 | }; |
| 1374 | |
| 1375 | static int __init mxc_nd_init(void) |
| 1376 | { |
Vladimir Barinov | 8541c11 | 2009-04-23 15:47:22 +0400 | [diff] [blame] | 1377 | return platform_driver_probe(&mxcnd_driver, mxcnd_probe); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | static void __exit mxc_nd_cleanup(void) |
| 1381 | { |
| 1382 | /* Unregister the device structure */ |
| 1383 | platform_driver_unregister(&mxcnd_driver); |
| 1384 | } |
| 1385 | |
| 1386 | module_init(mxc_nd_init); |
| 1387 | module_exit(mxc_nd_cleanup); |
| 1388 | |
| 1389 | MODULE_AUTHOR("Freescale Semiconductor, Inc."); |
| 1390 | MODULE_DESCRIPTION("MXC NAND MTD driver"); |
| 1391 | MODULE_LICENSE("GPL"); |