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> |
Sachin Kamat | d367e37 | 2013-10-18 16:16:35 +0530 | [diff] [blame] | 35 | #include <linux/of.h> |
Uwe Kleine-König | 6436356 | 2012-04-23 11:23:41 +0200 | [diff] [blame] | 36 | #include <linux/of_device.h> |
| 37 | #include <linux/of_mtd.h> |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 38 | |
| 39 | #include <asm/mach/flash.h> |
Arnd Bergmann | 82906b1 | 2012-08-24 15:14:29 +0200 | [diff] [blame] | 40 | #include <linux/platform_data/mtd-mxc_nand.h> |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 41 | |
| 42 | #define DRIVER_NAME "mxc_nand" |
| 43 | |
| 44 | /* Addresses for NFC registers */ |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 45 | #define NFC_V1_V2_BUF_SIZE (host->regs + 0x00) |
| 46 | #define NFC_V1_V2_BUF_ADDR (host->regs + 0x04) |
| 47 | #define NFC_V1_V2_FLASH_ADDR (host->regs + 0x06) |
| 48 | #define NFC_V1_V2_FLASH_CMD (host->regs + 0x08) |
| 49 | #define NFC_V1_V2_CONFIG (host->regs + 0x0a) |
| 50 | #define NFC_V1_V2_ECC_STATUS_RESULT (host->regs + 0x0c) |
| 51 | #define NFC_V1_V2_RSLTMAIN_AREA (host->regs + 0x0e) |
| 52 | #define NFC_V1_V2_RSLTSPARE_AREA (host->regs + 0x10) |
| 53 | #define NFC_V1_V2_WRPROT (host->regs + 0x12) |
| 54 | #define NFC_V1_UNLOCKSTART_BLKADDR (host->regs + 0x14) |
| 55 | #define NFC_V1_UNLOCKEND_BLKADDR (host->regs + 0x16) |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 56 | #define NFC_V21_UNLOCKSTART_BLKADDR0 (host->regs + 0x20) |
| 57 | #define NFC_V21_UNLOCKSTART_BLKADDR1 (host->regs + 0x24) |
| 58 | #define NFC_V21_UNLOCKSTART_BLKADDR2 (host->regs + 0x28) |
| 59 | #define NFC_V21_UNLOCKSTART_BLKADDR3 (host->regs + 0x2c) |
| 60 | #define NFC_V21_UNLOCKEND_BLKADDR0 (host->regs + 0x22) |
| 61 | #define NFC_V21_UNLOCKEND_BLKADDR1 (host->regs + 0x26) |
| 62 | #define NFC_V21_UNLOCKEND_BLKADDR2 (host->regs + 0x2a) |
| 63 | #define NFC_V21_UNLOCKEND_BLKADDR3 (host->regs + 0x2e) |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 64 | #define NFC_V1_V2_NF_WRPRST (host->regs + 0x18) |
| 65 | #define NFC_V1_V2_CONFIG1 (host->regs + 0x1a) |
| 66 | #define NFC_V1_V2_CONFIG2 (host->regs + 0x1c) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 67 | |
Sascha Hauer | 6e85dfd | 2010-08-06 15:53:10 +0200 | [diff] [blame] | 68 | #define NFC_V2_CONFIG1_ECC_MODE_4 (1 << 0) |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 69 | #define NFC_V1_V2_CONFIG1_SP_EN (1 << 2) |
| 70 | #define NFC_V1_V2_CONFIG1_ECC_EN (1 << 3) |
| 71 | #define NFC_V1_V2_CONFIG1_INT_MSK (1 << 4) |
| 72 | #define NFC_V1_V2_CONFIG1_BIG (1 << 5) |
| 73 | #define NFC_V1_V2_CONFIG1_RST (1 << 6) |
| 74 | #define NFC_V1_V2_CONFIG1_CE (1 << 7) |
Sascha Hauer | b8db2f5 | 2010-08-09 15:04:19 +0200 | [diff] [blame] | 75 | #define NFC_V2_CONFIG1_ONE_CYCLE (1 << 8) |
| 76 | #define NFC_V2_CONFIG1_PPB(x) (((x) & 0x3) << 9) |
| 77 | #define NFC_V2_CONFIG1_FP_INT (1 << 11) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 78 | |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 79 | #define NFC_V1_V2_CONFIG2_INT (1 << 15) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 80 | |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 81 | /* |
| 82 | * Operation modes for the NFC. Valid for v1, v2 and v3 |
| 83 | * type controllers. |
| 84 | */ |
| 85 | #define NFC_CMD (1 << 0) |
| 86 | #define NFC_ADDR (1 << 1) |
| 87 | #define NFC_INPUT (1 << 2) |
| 88 | #define NFC_OUTPUT (1 << 3) |
| 89 | #define NFC_ID (1 << 4) |
| 90 | #define NFC_STATUS (1 << 5) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 91 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 92 | #define NFC_V3_FLASH_CMD (host->regs_axi + 0x00) |
| 93 | #define NFC_V3_FLASH_ADDR0 (host->regs_axi + 0x04) |
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_CONFIG1 (host->regs_axi + 0x34) |
| 96 | #define NFC_V3_CONFIG1_SP_EN (1 << 0) |
| 97 | #define NFC_V3_CONFIG1_RBA(x) (((x) & 0x7 ) << 4) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 98 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 99 | #define NFC_V3_ECC_STATUS_RESULT (host->regs_axi + 0x38) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 100 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 101 | #define NFC_V3_LAUNCH (host->regs_axi + 0x40) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 102 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 103 | #define NFC_V3_WRPROT (host->regs_ip + 0x0) |
| 104 | #define NFC_V3_WRPROT_LOCK_TIGHT (1 << 0) |
| 105 | #define NFC_V3_WRPROT_LOCK (1 << 1) |
| 106 | #define NFC_V3_WRPROT_UNLOCK (1 << 2) |
| 107 | #define NFC_V3_WRPROT_BLS_UNLOCK (2 << 6) |
| 108 | |
| 109 | #define NFC_V3_WRPROT_UNLOCK_BLK_ADD0 (host->regs_ip + 0x04) |
| 110 | |
| 111 | #define NFC_V3_CONFIG2 (host->regs_ip + 0x24) |
| 112 | #define NFC_V3_CONFIG2_PS_512 (0 << 0) |
| 113 | #define NFC_V3_CONFIG2_PS_2048 (1 << 0) |
| 114 | #define NFC_V3_CONFIG2_PS_4096 (2 << 0) |
| 115 | #define NFC_V3_CONFIG2_ONE_CYCLE (1 << 2) |
| 116 | #define NFC_V3_CONFIG2_ECC_EN (1 << 3) |
| 117 | #define NFC_V3_CONFIG2_2CMD_PHASES (1 << 4) |
| 118 | #define NFC_V3_CONFIG2_NUM_ADDR_PHASE0 (1 << 5) |
| 119 | #define NFC_V3_CONFIG2_ECC_MODE_8 (1 << 6) |
Sascha Hauer | 71718a8e | 2012-06-06 12:33:15 +0200 | [diff] [blame] | 120 | #define NFC_V3_CONFIG2_PPB(x, shift) (((x) & 0x3) << shift) |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 121 | #define NFC_V3_CONFIG2_NUM_ADDR_PHASE1(x) (((x) & 0x3) << 12) |
| 122 | #define NFC_V3_CONFIG2_INT_MSK (1 << 15) |
| 123 | #define NFC_V3_CONFIG2_ST_CMD(x) (((x) & 0xff) << 24) |
| 124 | #define NFC_V3_CONFIG2_SPAS(x) (((x) & 0xff) << 16) |
| 125 | |
| 126 | #define NFC_V3_CONFIG3 (host->regs_ip + 0x28) |
| 127 | #define NFC_V3_CONFIG3_ADD_OP(x) (((x) & 0x3) << 0) |
| 128 | #define NFC_V3_CONFIG3_FW8 (1 << 3) |
| 129 | #define NFC_V3_CONFIG3_SBB(x) (((x) & 0x7) << 8) |
| 130 | #define NFC_V3_CONFIG3_NUM_OF_DEVICES(x) (((x) & 0x7) << 12) |
| 131 | #define NFC_V3_CONFIG3_RBB_MODE (1 << 15) |
| 132 | #define NFC_V3_CONFIG3_NO_SDMA (1 << 20) |
| 133 | |
| 134 | #define NFC_V3_IPC (host->regs_ip + 0x2C) |
| 135 | #define NFC_V3_IPC_CREQ (1 << 0) |
| 136 | #define NFC_V3_IPC_INT (1 << 31) |
| 137 | |
| 138 | #define NFC_V3_DELAY_LINE (host->regs_ip + 0x34) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 139 | |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 140 | struct mxc_nand_host; |
| 141 | |
| 142 | struct mxc_nand_devtype_data { |
| 143 | void (*preset)(struct mtd_info *); |
| 144 | void (*send_cmd)(struct mxc_nand_host *, uint16_t, int); |
| 145 | void (*send_addr)(struct mxc_nand_host *, uint16_t, int); |
| 146 | void (*send_page)(struct mtd_info *, unsigned int); |
| 147 | void (*send_read_id)(struct mxc_nand_host *); |
| 148 | uint16_t (*get_dev_status)(struct mxc_nand_host *); |
| 149 | int (*check_int)(struct mxc_nand_host *); |
| 150 | void (*irq_control)(struct mxc_nand_host *, int); |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 151 | u32 (*get_ecc_status)(struct mxc_nand_host *); |
Boris Brezillon | a894cf6c | 2016-02-03 20:02:54 +0100 | [diff] [blame^] | 152 | const struct mtd_ooblayout_ops *ooblayout; |
Uwe Kleine-König | 5e05a2d6 | 2012-04-23 11:23:38 +0200 | [diff] [blame] | 153 | void (*select_chip)(struct mtd_info *mtd, int chip); |
Uwe Kleine-König | 69d023b | 2012-04-23 11:23:39 +0200 | [diff] [blame] | 154 | int (*correct_data)(struct mtd_info *mtd, u_char *dat, |
| 155 | u_char *read_ecc, u_char *calc_ecc); |
Uwe Kleine-König | f48d0f9 | 2012-04-23 11:23:40 +0200 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | * On i.MX21 the CONFIG2:INT bit cannot be read if interrupts are masked |
| 159 | * (CONFIG1:INT_MSK is set). To handle this the driver uses |
| 160 | * enable_irq/disable_irq_nosync instead of CONFIG1:INT_MSK |
| 161 | */ |
| 162 | int irqpending_quirk; |
| 163 | int needs_ip; |
| 164 | |
| 165 | size_t regs_offset; |
| 166 | size_t spare0_offset; |
| 167 | size_t axi_offset; |
| 168 | |
| 169 | int spare_len; |
| 170 | int eccbytes; |
| 171 | int eccsize; |
Sascha Hauer | 71718a8e | 2012-06-06 12:33:15 +0200 | [diff] [blame] | 172 | int ppb_shift; |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 173 | }; |
| 174 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 175 | struct mxc_nand_host { |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 176 | struct nand_chip nand; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 177 | struct device *dev; |
| 178 | |
Uwe Kleine-König | 4b6f05e | 2012-04-24 10:05:22 +0200 | [diff] [blame] | 179 | void __iomem *spare0; |
| 180 | void __iomem *main_area0; |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 181 | |
| 182 | void __iomem *base; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 183 | void __iomem *regs; |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 184 | void __iomem *regs_axi; |
| 185 | void __iomem *regs_ip; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 186 | int status_request; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 187 | struct clk *clk; |
| 188 | int clk_act; |
| 189 | int irq; |
Sascha Hauer | 94f77e5 | 2010-08-06 15:53:09 +0200 | [diff] [blame] | 190 | int eccsize; |
Baruch Siach | 7e7e473 | 2015-05-13 11:17:37 +0300 | [diff] [blame] | 191 | int used_oobsize; |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 192 | int active_cs; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 193 | |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 194 | struct completion op_completion; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 195 | |
| 196 | uint8_t *data_buf; |
| 197 | unsigned int buf_start; |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 198 | |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 199 | const struct mxc_nand_devtype_data *devtype_data; |
Uwe Kleine-König | 6436356 | 2012-04-23 11:23:41 +0200 | [diff] [blame] | 200 | struct mxc_nand_platform_data pdata; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 201 | }; |
| 202 | |
Jingoo Han | b2ac037 | 2013-08-07 16:18:52 +0900 | [diff] [blame] | 203 | static const char * const part_probes[] = { |
Lothar Waßmann | 740bb0c | 2012-12-06 08:42:28 +0100 | [diff] [blame] | 204 | "cmdlinepart", "RedBoot", "ofpart", NULL }; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 205 | |
Sascha Hauer | 096bcc2 | 2012-05-29 10:16:09 +0200 | [diff] [blame] | 206 | static void memcpy32_fromio(void *trg, const void __iomem *src, size_t size) |
| 207 | { |
| 208 | int i; |
| 209 | u32 *t = trg; |
| 210 | const __iomem u32 *s = src; |
| 211 | |
| 212 | for (i = 0; i < (size >> 2); i++) |
| 213 | *t++ = __raw_readl(s++); |
| 214 | } |
| 215 | |
Baruch Siach | 0d17fc3 | 2015-05-13 11:17:38 +0300 | [diff] [blame] | 216 | static void memcpy16_fromio(void *trg, const void __iomem *src, size_t size) |
| 217 | { |
| 218 | int i; |
| 219 | u16 *t = trg; |
| 220 | const __iomem u16 *s = src; |
| 221 | |
| 222 | /* We assume that src (IO) is always 32bit aligned */ |
| 223 | if (PTR_ALIGN(trg, 4) == trg && IS_ALIGNED(size, 4)) { |
| 224 | memcpy32_fromio(trg, src, size); |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | for (i = 0; i < (size >> 1); i++) |
| 229 | *t++ = __raw_readw(s++); |
| 230 | } |
| 231 | |
Koul, Vinod | 33a87a1 | 2014-10-20 21:36:13 +0530 | [diff] [blame] | 232 | static inline void memcpy32_toio(void __iomem *trg, const void *src, int size) |
Sascha Hauer | 096bcc2 | 2012-05-29 10:16:09 +0200 | [diff] [blame] | 233 | { |
Koul, Vinod | 33a87a1 | 2014-10-20 21:36:13 +0530 | [diff] [blame] | 234 | /* __iowrite32_copy use 32bit size values so divide by 4 */ |
| 235 | __iowrite32_copy(trg, src, size / 4); |
Sascha Hauer | 096bcc2 | 2012-05-29 10:16:09 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Baruch Siach | 0d17fc3 | 2015-05-13 11:17:38 +0300 | [diff] [blame] | 238 | static void memcpy16_toio(void __iomem *trg, const void *src, int size) |
| 239 | { |
| 240 | int i; |
| 241 | __iomem u16 *t = trg; |
| 242 | const u16 *s = src; |
| 243 | |
| 244 | /* We assume that trg (IO) is always 32bit aligned */ |
| 245 | if (PTR_ALIGN(src, 4) == src && IS_ALIGNED(size, 4)) { |
| 246 | memcpy32_toio(trg, src, size); |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | for (i = 0; i < (size >> 1); i++) |
| 251 | __raw_writew(*s++, t++); |
| 252 | } |
| 253 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 254 | static int check_int_v3(struct mxc_nand_host *host) |
| 255 | { |
| 256 | uint32_t tmp; |
| 257 | |
| 258 | tmp = readl(NFC_V3_IPC); |
| 259 | if (!(tmp & NFC_V3_IPC_INT)) |
| 260 | return 0; |
| 261 | |
| 262 | tmp &= ~NFC_V3_IPC_INT; |
| 263 | writel(tmp, NFC_V3_IPC); |
| 264 | |
| 265 | return 1; |
| 266 | } |
| 267 | |
Sascha Hauer | 7aaf28a | 2010-08-06 15:53:07 +0200 | [diff] [blame] | 268 | static int check_int_v1_v2(struct mxc_nand_host *host) |
| 269 | { |
| 270 | uint32_t tmp; |
| 271 | |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 272 | tmp = readw(NFC_V1_V2_CONFIG2); |
| 273 | if (!(tmp & NFC_V1_V2_CONFIG2_INT)) |
Sascha Hauer | 7aaf28a | 2010-08-06 15:53:07 +0200 | [diff] [blame] | 274 | return 0; |
| 275 | |
Uwe Kleine-König | f48d0f9 | 2012-04-23 11:23:40 +0200 | [diff] [blame] | 276 | if (!host->devtype_data->irqpending_quirk) |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 277 | writew(tmp & ~NFC_V1_V2_CONFIG2_INT, NFC_V1_V2_CONFIG2); |
Sascha Hauer | 7aaf28a | 2010-08-06 15:53:07 +0200 | [diff] [blame] | 278 | |
| 279 | return 1; |
| 280 | } |
| 281 | |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 282 | static void irq_control_v1_v2(struct mxc_nand_host *host, int activate) |
| 283 | { |
| 284 | uint16_t tmp; |
| 285 | |
| 286 | tmp = readw(NFC_V1_V2_CONFIG1); |
| 287 | |
| 288 | if (activate) |
| 289 | tmp &= ~NFC_V1_V2_CONFIG1_INT_MSK; |
| 290 | else |
| 291 | tmp |= NFC_V1_V2_CONFIG1_INT_MSK; |
| 292 | |
| 293 | writew(tmp, NFC_V1_V2_CONFIG1); |
| 294 | } |
| 295 | |
| 296 | static void irq_control_v3(struct mxc_nand_host *host, int activate) |
| 297 | { |
| 298 | uint32_t tmp; |
| 299 | |
| 300 | tmp = readl(NFC_V3_CONFIG2); |
| 301 | |
| 302 | if (activate) |
| 303 | tmp &= ~NFC_V3_CONFIG2_INT_MSK; |
| 304 | else |
| 305 | tmp |= NFC_V3_CONFIG2_INT_MSK; |
| 306 | |
| 307 | writel(tmp, NFC_V3_CONFIG2); |
| 308 | } |
| 309 | |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 310 | static void irq_control(struct mxc_nand_host *host, int activate) |
| 311 | { |
Uwe Kleine-König | f48d0f9 | 2012-04-23 11:23:40 +0200 | [diff] [blame] | 312 | if (host->devtype_data->irqpending_quirk) { |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 313 | if (activate) |
| 314 | enable_irq(host->irq); |
| 315 | else |
| 316 | disable_irq_nosync(host->irq); |
| 317 | } else { |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 318 | host->devtype_data->irq_control(host, activate); |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 322 | static u32 get_ecc_status_v1(struct mxc_nand_host *host) |
| 323 | { |
| 324 | return readw(NFC_V1_V2_ECC_STATUS_RESULT); |
| 325 | } |
| 326 | |
| 327 | static u32 get_ecc_status_v2(struct mxc_nand_host *host) |
| 328 | { |
| 329 | return readl(NFC_V1_V2_ECC_STATUS_RESULT); |
| 330 | } |
| 331 | |
| 332 | static u32 get_ecc_status_v3(struct mxc_nand_host *host) |
| 333 | { |
| 334 | return readl(NFC_V3_ECC_STATUS_RESULT); |
| 335 | } |
| 336 | |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 337 | static irqreturn_t mxc_nfc_irq(int irq, void *dev_id) |
| 338 | { |
| 339 | struct mxc_nand_host *host = dev_id; |
| 340 | |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 341 | if (!host->devtype_data->check_int(host)) |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 342 | return IRQ_NONE; |
| 343 | |
| 344 | irq_control(host, 0); |
| 345 | |
| 346 | complete(&host->op_completion); |
| 347 | |
| 348 | return IRQ_HANDLED; |
| 349 | } |
| 350 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 351 | /* This function polls the NANDFC to wait for the basic operation to |
| 352 | * complete by checking the INT bit of config2 register. |
| 353 | */ |
Uwe Kleine-König | e35d1d8 | 2015-02-10 19:59:55 +0100 | [diff] [blame] | 354 | static int wait_op_done(struct mxc_nand_host *host, int useirq) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 355 | { |
Uwe Kleine-König | e35d1d8 | 2015-02-10 19:59:55 +0100 | [diff] [blame] | 356 | int ret = 0; |
| 357 | |
| 358 | /* |
| 359 | * If operation is already complete, don't bother to setup an irq or a |
| 360 | * loop. |
| 361 | */ |
| 362 | if (host->devtype_data->check_int(host)) |
| 363 | return 0; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 364 | |
| 365 | if (useirq) { |
Uwe Kleine-König | e35d1d8 | 2015-02-10 19:59:55 +0100 | [diff] [blame] | 366 | unsigned long timeout; |
| 367 | |
| 368 | reinit_completion(&host->op_completion); |
| 369 | |
| 370 | irq_control(host, 1); |
| 371 | |
| 372 | timeout = wait_for_completion_timeout(&host->op_completion, HZ); |
| 373 | if (!timeout && !host->devtype_data->check_int(host)) { |
| 374 | dev_dbg(host->dev, "timeout waiting for irq\n"); |
| 375 | ret = -ETIMEDOUT; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 376 | } |
| 377 | } else { |
Uwe Kleine-König | e35d1d8 | 2015-02-10 19:59:55 +0100 | [diff] [blame] | 378 | int max_retries = 8000; |
| 379 | int done; |
| 380 | |
| 381 | do { |
| 382 | udelay(1); |
| 383 | |
| 384 | done = host->devtype_data->check_int(host); |
| 385 | if (done) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 386 | break; |
Sascha Hauer | 7aaf28a | 2010-08-06 15:53:07 +0200 | [diff] [blame] | 387 | |
Uwe Kleine-König | e35d1d8 | 2015-02-10 19:59:55 +0100 | [diff] [blame] | 388 | } while (--max_retries); |
| 389 | |
| 390 | if (!done) { |
| 391 | dev_dbg(host->dev, "timeout polling for completion\n"); |
| 392 | ret = -ETIMEDOUT; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 393 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 394 | } |
Uwe Kleine-König | e35d1d8 | 2015-02-10 19:59:55 +0100 | [diff] [blame] | 395 | |
| 396 | WARN_ONCE(ret < 0, "timeout! useirq=%d\n", useirq); |
| 397 | |
| 398 | return ret; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 401 | static void send_cmd_v3(struct mxc_nand_host *host, uint16_t cmd, int useirq) |
| 402 | { |
| 403 | /* fill command */ |
| 404 | writel(cmd, NFC_V3_FLASH_CMD); |
| 405 | |
| 406 | /* send out command */ |
| 407 | writel(NFC_CMD, NFC_V3_LAUNCH); |
| 408 | |
| 409 | /* Wait for operation to complete */ |
| 410 | wait_op_done(host, useirq); |
| 411 | } |
| 412 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 413 | /* This function issues the specified command to the NAND device and |
| 414 | * waits for completion. */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 415 | 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] | 416 | { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 417 | pr_debug("send_cmd(host, 0x%x, %d)\n", cmd, useirq); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 418 | |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 419 | writew(cmd, NFC_V1_V2_FLASH_CMD); |
| 420 | writew(NFC_CMD, NFC_V1_V2_CONFIG2); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 421 | |
Uwe Kleine-König | f48d0f9 | 2012-04-23 11:23:40 +0200 | [diff] [blame] | 422 | if (host->devtype_data->irqpending_quirk && (cmd == NAND_CMD_RESET)) { |
Ivo Clarysse | a47bfd2 | 2010-04-08 16:16:51 +0200 | [diff] [blame] | 423 | int max_retries = 100; |
| 424 | /* Reset completion is indicated by NFC_CONFIG2 */ |
| 425 | /* being set to 0 */ |
| 426 | while (max_retries-- > 0) { |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 427 | if (readw(NFC_V1_V2_CONFIG2) == 0) { |
Ivo Clarysse | a47bfd2 | 2010-04-08 16:16:51 +0200 | [diff] [blame] | 428 | break; |
| 429 | } |
| 430 | udelay(1); |
| 431 | } |
| 432 | if (max_retries < 0) |
Brian Norris | 0a32a10 | 2011-07-19 10:06:10 -0700 | [diff] [blame] | 433 | pr_debug("%s: RESET failed\n", __func__); |
Ivo Clarysse | a47bfd2 | 2010-04-08 16:16:51 +0200 | [diff] [blame] | 434 | } else { |
| 435 | /* Wait for operation to complete */ |
| 436 | wait_op_done(host, useirq); |
| 437 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 438 | } |
| 439 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 440 | static void send_addr_v3(struct mxc_nand_host *host, uint16_t addr, int islast) |
| 441 | { |
| 442 | /* fill address */ |
| 443 | writel(addr, NFC_V3_FLASH_ADDR0); |
| 444 | |
| 445 | /* send out address */ |
| 446 | writel(NFC_ADDR, NFC_V3_LAUNCH); |
| 447 | |
| 448 | wait_op_done(host, 0); |
| 449 | } |
| 450 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 451 | /* This function sends an address (or partial address) to the |
| 452 | * NAND device. The address is used to select the source/destination for |
| 453 | * a NAND command. */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 454 | 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] | 455 | { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 456 | pr_debug("send_addr(host, 0x%x %d)\n", addr, islast); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 457 | |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 458 | writew(addr, NFC_V1_V2_FLASH_ADDR); |
| 459 | writew(NFC_ADDR, NFC_V1_V2_CONFIG2); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 460 | |
| 461 | /* Wait for operation to complete */ |
Sascha Hauer | c110eaf | 2009-10-21 16:01:02 +0200 | [diff] [blame] | 462 | wait_op_done(host, islast); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 463 | } |
| 464 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 465 | static void send_page_v3(struct mtd_info *mtd, unsigned int ops) |
| 466 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 467 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 468 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 469 | uint32_t tmp; |
| 470 | |
| 471 | tmp = readl(NFC_V3_CONFIG1); |
| 472 | tmp &= ~(7 << 4); |
| 473 | writel(tmp, NFC_V3_CONFIG1); |
| 474 | |
| 475 | /* transfer data from NFC ram to nand */ |
| 476 | writel(ops, NFC_V3_LAUNCH); |
| 477 | |
| 478 | wait_op_done(host, false); |
| 479 | } |
| 480 | |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 481 | static void send_page_v2(struct mtd_info *mtd, unsigned int ops) |
| 482 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 483 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 484 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 485 | |
| 486 | /* NANDFC buffer 0 is used for page read/write */ |
| 487 | writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR); |
| 488 | |
| 489 | writew(ops, NFC_V1_V2_CONFIG2); |
| 490 | |
| 491 | /* Wait for operation to complete */ |
| 492 | wait_op_done(host, true); |
| 493 | } |
| 494 | |
| 495 | static void send_page_v1(struct mtd_info *mtd, unsigned int ops) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 496 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 497 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 498 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 499 | int bufs, i; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 500 | |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 501 | if (mtd->writesize > 512) |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 502 | bufs = 4; |
| 503 | else |
| 504 | bufs = 1; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 505 | |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 506 | for (i = 0; i < bufs; i++) { |
| 507 | |
| 508 | /* NANDFC buffer 0 is used for page read/write */ |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 509 | writew((host->active_cs << 4) | i, NFC_V1_V2_BUF_ADDR); |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 510 | |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 511 | writew(ops, NFC_V1_V2_CONFIG2); |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 512 | |
| 513 | /* Wait for operation to complete */ |
Sascha Hauer | c110eaf | 2009-10-21 16:01:02 +0200 | [diff] [blame] | 514 | wait_op_done(host, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 515 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 516 | } |
| 517 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 518 | static void send_read_id_v3(struct mxc_nand_host *host) |
| 519 | { |
| 520 | /* Read ID into main buffer */ |
| 521 | writel(NFC_ID, NFC_V3_LAUNCH); |
| 522 | |
| 523 | wait_op_done(host, true); |
| 524 | |
Sascha Hauer | 096bcc2 | 2012-05-29 10:16:09 +0200 | [diff] [blame] | 525 | memcpy32_fromio(host->data_buf, host->main_area0, 16); |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 526 | } |
| 527 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 528 | /* Request the NANDFC to perform a read of the NAND device ID. */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 529 | static void send_read_id_v1_v2(struct mxc_nand_host *host) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 530 | { |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 531 | /* NANDFC buffer 0 is used for device ID output */ |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 532 | writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 533 | |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 534 | writew(NFC_ID, NFC_V1_V2_CONFIG2); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 535 | |
| 536 | /* Wait for operation to complete */ |
Sascha Hauer | c110eaf | 2009-10-21 16:01:02 +0200 | [diff] [blame] | 537 | wait_op_done(host, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 538 | |
Sascha Hauer | 096bcc2 | 2012-05-29 10:16:09 +0200 | [diff] [blame] | 539 | memcpy32_fromio(host->data_buf, host->main_area0, 16); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 540 | } |
| 541 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 542 | static uint16_t get_dev_status_v3(struct mxc_nand_host *host) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 543 | { |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 544 | writew(NFC_STATUS, NFC_V3_LAUNCH); |
Sascha Hauer | c110eaf | 2009-10-21 16:01:02 +0200 | [diff] [blame] | 545 | wait_op_done(host, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 546 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 547 | return readl(NFC_V3_CONFIG1) >> 16; |
| 548 | } |
| 549 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 550 | /* This function requests the NANDFC to perform a read of the |
| 551 | * NAND device status and returns the current status. */ |
Sascha Hauer | 5f97304 | 2010-08-06 15:53:06 +0200 | [diff] [blame] | 552 | 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] | 553 | { |
Sascha Hauer | c29c607 | 2010-08-06 15:53:05 +0200 | [diff] [blame] | 554 | void __iomem *main_buf = host->main_area0; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 555 | uint32_t store; |
| 556 | uint16_t ret; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 557 | |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 558 | writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR); |
Sascha Hauer | c29c607 | 2010-08-06 15:53:05 +0200 | [diff] [blame] | 559 | |
| 560 | /* |
| 561 | * The device status is stored in main_area0. To |
| 562 | * prevent corruption of the buffer save the value |
| 563 | * and restore it afterwards. |
| 564 | */ |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 565 | store = readl(main_buf); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 566 | |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 567 | writew(NFC_STATUS, NFC_V1_V2_CONFIG2); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 568 | wait_op_done(host, true); |
| 569 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 570 | ret = readw(main_buf); |
Sascha Hauer | c29c607 | 2010-08-06 15:53:05 +0200 | [diff] [blame] | 571 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 572 | writel(store, main_buf); |
| 573 | |
| 574 | return ret; |
| 575 | } |
| 576 | |
| 577 | /* This functions is used by upper layer to checks if device is ready */ |
| 578 | static int mxc_nand_dev_ready(struct mtd_info *mtd) |
| 579 | { |
| 580 | /* |
| 581 | * NFC handles R/B internally. Therefore, this function |
| 582 | * always returns status as ready. |
| 583 | */ |
| 584 | return 1; |
| 585 | } |
| 586 | |
| 587 | static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode) |
| 588 | { |
| 589 | /* |
| 590 | * If HW ECC is enabled, we turn it on during init. There is |
| 591 | * no need to enable again here. |
| 592 | */ |
| 593 | } |
| 594 | |
Sascha Hauer | 94f77e5 | 2010-08-06 15:53:09 +0200 | [diff] [blame] | 595 | 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] | 596 | u_char *read_ecc, u_char *calc_ecc) |
| 597 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 598 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 599 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 600 | |
| 601 | /* |
| 602 | * 1-Bit errors are automatically corrected in HW. No need for |
| 603 | * additional correction. 2-Bit errors cannot be corrected by |
| 604 | * HW ECC, so we need to return failure |
| 605 | */ |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 606 | uint16_t ecc_status = get_ecc_status_v1(host); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 607 | |
| 608 | if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 609 | pr_debug("MXC_NAND: HWECC uncorrectable 2-bit ECC error\n"); |
Boris BREZILLON | 6e94119 | 2015-12-30 20:32:03 +0100 | [diff] [blame] | 610 | return -EBADMSG; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
Sascha Hauer | 94f77e5 | 2010-08-06 15:53:09 +0200 | [diff] [blame] | 616 | static int mxc_nand_correct_data_v2_v3(struct mtd_info *mtd, u_char *dat, |
| 617 | u_char *read_ecc, u_char *calc_ecc) |
| 618 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 619 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 620 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Sascha Hauer | 94f77e5 | 2010-08-06 15:53:09 +0200 | [diff] [blame] | 621 | u32 ecc_stat, err; |
| 622 | int no_subpages = 1; |
| 623 | int ret = 0; |
| 624 | u8 ecc_bit_mask, err_limit; |
| 625 | |
| 626 | ecc_bit_mask = (host->eccsize == 4) ? 0x7 : 0xf; |
| 627 | err_limit = (host->eccsize == 4) ? 0x4 : 0x8; |
| 628 | |
| 629 | no_subpages = mtd->writesize >> 9; |
| 630 | |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 631 | ecc_stat = host->devtype_data->get_ecc_status(host); |
Sascha Hauer | 94f77e5 | 2010-08-06 15:53:09 +0200 | [diff] [blame] | 632 | |
| 633 | do { |
| 634 | err = ecc_stat & ecc_bit_mask; |
| 635 | if (err > err_limit) { |
| 636 | printk(KERN_WARNING "UnCorrectable RS-ECC Error\n"); |
Boris BREZILLON | 6e94119 | 2015-12-30 20:32:03 +0100 | [diff] [blame] | 637 | return -EBADMSG; |
Sascha Hauer | 94f77e5 | 2010-08-06 15:53:09 +0200 | [diff] [blame] | 638 | } else { |
| 639 | ret += err; |
| 640 | } |
| 641 | ecc_stat >>= 4; |
| 642 | } while (--no_subpages); |
| 643 | |
Sascha Hauer | 94f77e5 | 2010-08-06 15:53:09 +0200 | [diff] [blame] | 644 | pr_debug("%d Symbol Correctable RS-ECC Error\n", ret); |
| 645 | |
| 646 | return ret; |
| 647 | } |
| 648 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 649 | static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, |
| 650 | u_char *ecc_code) |
| 651 | { |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | static u_char mxc_nand_read_byte(struct mtd_info *mtd) |
| 656 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 657 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 658 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 659 | uint8_t ret; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 660 | |
| 661 | /* Check for status request */ |
| 662 | if (host->status_request) |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 663 | return host->devtype_data->get_dev_status(host) & 0xFF; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 664 | |
Uwe Kleine-König | 3f41069 | 2015-02-10 19:59:57 +0100 | [diff] [blame] | 665 | if (nand_chip->options & NAND_BUSWIDTH_16) { |
| 666 | /* only take the lower byte of each word */ |
| 667 | ret = *(uint16_t *)(host->data_buf + host->buf_start); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 668 | |
Uwe Kleine-König | 3f41069 | 2015-02-10 19:59:57 +0100 | [diff] [blame] | 669 | host->buf_start += 2; |
| 670 | } else { |
| 671 | ret = *(uint8_t *)(host->data_buf + host->buf_start); |
| 672 | host->buf_start++; |
| 673 | } |
| 674 | |
| 675 | pr_debug("%s: ret=0x%hhx (start=%u)\n", __func__, ret, host->buf_start); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 676 | return ret; |
| 677 | } |
| 678 | |
| 679 | static uint16_t mxc_nand_read_word(struct mtd_info *mtd) |
| 680 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 681 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 682 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 683 | uint16_t ret; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 684 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 685 | ret = *(uint16_t *)(host->data_buf + host->buf_start); |
| 686 | host->buf_start += 2; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 687 | |
| 688 | return ret; |
| 689 | } |
| 690 | |
| 691 | /* Write data of length len to buffer buf. The data to be |
| 692 | * written on NAND Flash is first copied to RAMbuffer. After the Data Input |
| 693 | * Operation by the NFC, the data is written to NAND Flash */ |
| 694 | static void mxc_nand_write_buf(struct mtd_info *mtd, |
| 695 | const u_char *buf, int len) |
| 696 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 697 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 698 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 699 | u16 col = host->buf_start; |
| 700 | int n = mtd->oobsize + mtd->writesize - col; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 701 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 702 | n = min(n, len); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 703 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 704 | memcpy(host->data_buf + col, buf, n); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 705 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 706 | host->buf_start += n; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | /* Read the data buffer from the NAND Flash. To read the data from NAND |
| 710 | * Flash first the data output cycle is initiated by the NFC, which copies |
| 711 | * the data to RAMbuffer. This data of length len is then copied to buffer buf. |
| 712 | */ |
| 713 | static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 714 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 715 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 716 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 717 | u16 col = host->buf_start; |
| 718 | int n = mtd->oobsize + mtd->writesize - col; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 719 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 720 | n = min(n, len); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 721 | |
Baruch Siach | 5d9d993 | 2011-03-02 16:47:55 +0200 | [diff] [blame] | 722 | memcpy(buf, host->data_buf + col, n); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 723 | |
Baruch Siach | 5d9d993 | 2011-03-02 16:47:55 +0200 | [diff] [blame] | 724 | host->buf_start += n; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 725 | } |
| 726 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 727 | /* This function is used by upper layer for select and |
| 728 | * deselect of the NAND chip */ |
Uwe Kleine-König | 5e05a2d6 | 2012-04-23 11:23:38 +0200 | [diff] [blame] | 729 | static void mxc_nand_select_chip_v1_v3(struct mtd_info *mtd, int chip) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 730 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 731 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 732 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 733 | |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 734 | if (chip == -1) { |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 735 | /* Disable the NFC clock */ |
| 736 | if (host->clk_act) { |
Sascha Hauer | 97c3213 | 2012-03-07 20:56:35 +0100 | [diff] [blame] | 737 | clk_disable_unprepare(host->clk); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 738 | host->clk_act = 0; |
| 739 | } |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 740 | return; |
| 741 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 742 | |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 743 | if (!host->clk_act) { |
| 744 | /* Enable the NFC clock */ |
Sascha Hauer | 97c3213 | 2012-03-07 20:56:35 +0100 | [diff] [blame] | 745 | clk_prepare_enable(host->clk); |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 746 | host->clk_act = 1; |
| 747 | } |
Uwe Kleine-König | 5e05a2d6 | 2012-04-23 11:23:38 +0200 | [diff] [blame] | 748 | } |
Baruch Siach | d178e3e | 2011-03-14 09:01:56 +0200 | [diff] [blame] | 749 | |
Uwe Kleine-König | 5e05a2d6 | 2012-04-23 11:23:38 +0200 | [diff] [blame] | 750 | static void mxc_nand_select_chip_v2(struct mtd_info *mtd, int chip) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 751 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 752 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 753 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 754 | |
| 755 | if (chip == -1) { |
| 756 | /* Disable the NFC clock */ |
| 757 | if (host->clk_act) { |
Fabio Estevam | 3d05969 | 2012-05-25 20:14:50 -0300 | [diff] [blame] | 758 | clk_disable_unprepare(host->clk); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 759 | host->clk_act = 0; |
| 760 | } |
| 761 | return; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 762 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 763 | |
| 764 | if (!host->clk_act) { |
| 765 | /* Enable the NFC clock */ |
Fabio Estevam | 3d05969 | 2012-05-25 20:14:50 -0300 | [diff] [blame] | 766 | clk_prepare_enable(host->clk); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 767 | host->clk_act = 1; |
| 768 | } |
| 769 | |
Uwe Kleine-König | 5e05a2d6 | 2012-04-23 11:23:38 +0200 | [diff] [blame] | 770 | host->active_cs = chip; |
| 771 | writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 772 | } |
| 773 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 774 | /* |
Uwe Kleine-König | 35d5d20 | 2015-05-13 11:17:36 +0300 | [diff] [blame] | 775 | * The controller splits a page into data chunks of 512 bytes + partial oob. |
| 776 | * There are writesize / 512 such chunks, the size of the partial oob parts is |
| 777 | * oobsize / #chunks rounded down to a multiple of 2. The last oob chunk then |
| 778 | * contains additionally the byte lost by rounding (if any). |
| 779 | * This function handles the needed shuffling between host->data_buf (which |
| 780 | * holds a page in natural order, i.e. writesize bytes data + oobsize bytes |
| 781 | * spare) and the NFC buffer. |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 782 | */ |
| 783 | static void copy_spare(struct mtd_info *mtd, bool bfrom) |
| 784 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 785 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 786 | struct mxc_nand_host *host = nand_get_controller_data(this); |
Uwe Kleine-König | 35d5d20 | 2015-05-13 11:17:36 +0300 | [diff] [blame] | 787 | u16 i, oob_chunk_size; |
| 788 | u16 num_chunks = mtd->writesize / 512; |
| 789 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 790 | u8 *d = host->data_buf + mtd->writesize; |
Uwe Kleine-König | 4b6f05e | 2012-04-24 10:05:22 +0200 | [diff] [blame] | 791 | u8 __iomem *s = host->spare0; |
Uwe Kleine-König | 35d5d20 | 2015-05-13 11:17:36 +0300 | [diff] [blame] | 792 | u16 sparebuf_size = host->devtype_data->spare_len; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 793 | |
Uwe Kleine-König | 35d5d20 | 2015-05-13 11:17:36 +0300 | [diff] [blame] | 794 | /* size of oob chunk for all but possibly the last one */ |
Baruch Siach | 7e7e473 | 2015-05-13 11:17:37 +0300 | [diff] [blame] | 795 | oob_chunk_size = (host->used_oobsize / num_chunks) & ~1; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 796 | |
| 797 | if (bfrom) { |
Uwe Kleine-König | 35d5d20 | 2015-05-13 11:17:36 +0300 | [diff] [blame] | 798 | for (i = 0; i < num_chunks - 1; i++) |
Baruch Siach | 0d17fc3 | 2015-05-13 11:17:38 +0300 | [diff] [blame] | 799 | memcpy16_fromio(d + i * oob_chunk_size, |
Uwe Kleine-König | 35d5d20 | 2015-05-13 11:17:36 +0300 | [diff] [blame] | 800 | s + i * sparebuf_size, |
| 801 | oob_chunk_size); |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 802 | |
Uwe Kleine-König | 35d5d20 | 2015-05-13 11:17:36 +0300 | [diff] [blame] | 803 | /* the last chunk */ |
Baruch Siach | 0d17fc3 | 2015-05-13 11:17:38 +0300 | [diff] [blame] | 804 | memcpy16_fromio(d + i * oob_chunk_size, |
Uwe Kleine-König | 35d5d20 | 2015-05-13 11:17:36 +0300 | [diff] [blame] | 805 | s + i * sparebuf_size, |
Baruch Siach | 7e7e473 | 2015-05-13 11:17:37 +0300 | [diff] [blame] | 806 | host->used_oobsize - i * oob_chunk_size); |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 807 | } else { |
Uwe Kleine-König | 35d5d20 | 2015-05-13 11:17:36 +0300 | [diff] [blame] | 808 | for (i = 0; i < num_chunks - 1; i++) |
Baruch Siach | 0d17fc3 | 2015-05-13 11:17:38 +0300 | [diff] [blame] | 809 | memcpy16_toio(&s[i * sparebuf_size], |
Uwe Kleine-König | 35d5d20 | 2015-05-13 11:17:36 +0300 | [diff] [blame] | 810 | &d[i * oob_chunk_size], |
| 811 | oob_chunk_size); |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 812 | |
Uwe Kleine-König | 35d5d20 | 2015-05-13 11:17:36 +0300 | [diff] [blame] | 813 | /* the last chunk */ |
Eric Benard | e5a5d92 | 2015-09-23 17:07:28 +0200 | [diff] [blame] | 814 | memcpy16_toio(&s[i * sparebuf_size], |
Uwe Kleine-König | 35d5d20 | 2015-05-13 11:17:36 +0300 | [diff] [blame] | 815 | &d[i * oob_chunk_size], |
Baruch Siach | 7e7e473 | 2015-05-13 11:17:37 +0300 | [diff] [blame] | 816 | host->used_oobsize - i * oob_chunk_size); |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | |
Uwe Kleine-König | c4ca399 | 2015-02-10 19:59:58 +0100 | [diff] [blame] | 820 | /* |
| 821 | * MXC NANDFC can only perform full page+spare or spare-only read/write. When |
| 822 | * the upper layers perform a read/write buf operation, the saved column address |
| 823 | * is used to index into the full page. So usually this function is called with |
| 824 | * column == 0 (unless no column cycle is needed indicated by column == -1) |
| 825 | */ |
Sascha Hauer | a3e65b6 | 2009-06-02 11:47:59 +0200 | [diff] [blame] | 826 | 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] | 827 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 828 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 829 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 830 | |
| 831 | /* Write out column address, if necessary */ |
| 832 | if (column != -1) { |
Uwe Kleine-König | c4ca399 | 2015-02-10 19:59:58 +0100 | [diff] [blame] | 833 | host->devtype_data->send_addr(host, column & 0xff, |
| 834 | page_addr == -1); |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame] | 835 | if (mtd->writesize > 512) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 836 | /* another col addr cycle for 2k page */ |
Uwe Kleine-König | c4ca399 | 2015-02-10 19:59:58 +0100 | [diff] [blame] | 837 | host->devtype_data->send_addr(host, |
| 838 | (column >> 8) & 0xff, |
| 839 | false); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /* Write out page address, if necessary */ |
| 843 | if (page_addr != -1) { |
| 844 | /* paddr_0 - p_addr_7 */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 845 | host->devtype_data->send_addr(host, (page_addr & 0xff), false); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 846 | |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame] | 847 | if (mtd->writesize > 512) { |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 848 | if (mtd->size >= 0x10000000) { |
| 849 | /* paddr_8 - paddr_15 */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 850 | host->devtype_data->send_addr(host, |
| 851 | (page_addr >> 8) & 0xff, |
| 852 | false); |
| 853 | host->devtype_data->send_addr(host, |
| 854 | (page_addr >> 16) & 0xff, |
| 855 | true); |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 856 | } else |
| 857 | /* paddr_8 - paddr_15 */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 858 | host->devtype_data->send_addr(host, |
| 859 | (page_addr >> 8) & 0xff, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 860 | } else { |
| 861 | /* One more address cycle for higher density devices */ |
| 862 | if (mtd->size >= 0x4000000) { |
| 863 | /* paddr_8 - paddr_15 */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 864 | host->devtype_data->send_addr(host, |
| 865 | (page_addr >> 8) & 0xff, |
| 866 | false); |
| 867 | host->devtype_data->send_addr(host, |
| 868 | (page_addr >> 16) & 0xff, |
| 869 | true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 870 | } else |
| 871 | /* paddr_8 - paddr_15 */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 872 | host->devtype_data->send_addr(host, |
| 873 | (page_addr >> 8) & 0xff, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 874 | } |
| 875 | } |
Sascha Hauer | a3e65b6 | 2009-06-02 11:47:59 +0200 | [diff] [blame] | 876 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 877 | |
Boris Brezillon | a894cf6c | 2016-02-03 20:02:54 +0100 | [diff] [blame^] | 878 | static int mxc_v1_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 879 | struct mtd_oob_region *oobregion) |
| 880 | { |
| 881 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
| 882 | |
| 883 | if (section >= nand_chip->ecc.steps) |
| 884 | return -ERANGE; |
| 885 | |
| 886 | oobregion->offset = (section * 16) + 6; |
| 887 | oobregion->length = nand_chip->ecc.bytes; |
| 888 | |
| 889 | return 0; |
| 890 | } |
| 891 | |
| 892 | static int mxc_v1_ooblayout_free(struct mtd_info *mtd, int section, |
| 893 | struct mtd_oob_region *oobregion) |
| 894 | { |
| 895 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
| 896 | |
| 897 | if (section > nand_chip->ecc.steps) |
| 898 | return -ERANGE; |
| 899 | |
| 900 | if (!section) { |
| 901 | if (mtd->writesize <= 512) { |
| 902 | oobregion->offset = 0; |
| 903 | oobregion->length = 5; |
| 904 | } else { |
| 905 | oobregion->offset = 2; |
| 906 | oobregion->length = 4; |
| 907 | } |
| 908 | } else { |
| 909 | oobregion->offset = ((section - 1) * 16) + |
| 910 | nand_chip->ecc.bytes + 6; |
| 911 | if (section < nand_chip->ecc.steps) |
| 912 | oobregion->length = (section * 16) + 6 - |
| 913 | oobregion->offset; |
| 914 | else |
| 915 | oobregion->length = mtd->oobsize - oobregion->offset; |
| 916 | } |
| 917 | |
| 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | static const struct mtd_ooblayout_ops mxc_v1_ooblayout_ops = { |
| 922 | .ecc = mxc_v1_ooblayout_ecc, |
| 923 | .free = mxc_v1_ooblayout_free, |
| 924 | }; |
| 925 | |
| 926 | static int mxc_v2_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 927 | struct mtd_oob_region *oobregion) |
| 928 | { |
| 929 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
| 930 | int stepsize = nand_chip->ecc.bytes == 9 ? 16 : 26; |
| 931 | |
| 932 | if (section >= nand_chip->ecc.steps) |
| 933 | return -ERANGE; |
| 934 | |
| 935 | oobregion->offset = (section * stepsize) + 7; |
| 936 | oobregion->length = nand_chip->ecc.bytes; |
| 937 | |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | static int mxc_v2_ooblayout_free(struct mtd_info *mtd, int section, |
| 942 | struct mtd_oob_region *oobregion) |
| 943 | { |
| 944 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
| 945 | int stepsize = nand_chip->ecc.bytes == 9 ? 16 : 26; |
| 946 | |
| 947 | if (section > nand_chip->ecc.steps) |
| 948 | return -ERANGE; |
| 949 | |
| 950 | if (!section) { |
| 951 | if (mtd->writesize <= 512) { |
| 952 | oobregion->offset = 0; |
| 953 | oobregion->length = 5; |
| 954 | } else { |
| 955 | oobregion->offset = 2; |
| 956 | oobregion->length = 4; |
| 957 | } |
| 958 | } else { |
| 959 | oobregion->offset = section * stepsize; |
| 960 | oobregion->length = 7; |
| 961 | } |
| 962 | |
| 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | static const struct mtd_ooblayout_ops mxc_v2_ooblayout_ops = { |
| 967 | .ecc = mxc_v2_ooblayout_ecc, |
| 968 | .free = mxc_v2_ooblayout_free, |
| 969 | }; |
| 970 | |
Sascha Hauer | 6e85dfd | 2010-08-06 15:53:10 +0200 | [diff] [blame] | 971 | /* |
| 972 | * v2 and v3 type controllers can do 4bit or 8bit ecc depending |
| 973 | * on how much oob the nand chip has. For 8bit ecc we need at least |
| 974 | * 26 bytes of oob data per 512 byte block. |
| 975 | */ |
| 976 | static int get_eccsize(struct mtd_info *mtd) |
| 977 | { |
| 978 | int oobbytes_per_512 = 0; |
| 979 | |
| 980 | oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize; |
| 981 | |
| 982 | if (oobbytes_per_512 < 26) |
| 983 | return 4; |
| 984 | else |
| 985 | return 8; |
| 986 | } |
| 987 | |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 988 | static void preset_v1(struct mtd_info *mtd) |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 989 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 990 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 991 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Sascha Hauer | b8db2f5 | 2010-08-09 15:04:19 +0200 | [diff] [blame] | 992 | uint16_t config1 = 0; |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 993 | |
Uwe Kleine-König | 1f42adc | 2015-02-10 19:59:56 +0100 | [diff] [blame] | 994 | if (nand_chip->ecc.mode == NAND_ECC_HW && mtd->writesize) |
Sascha Hauer | b8db2f5 | 2010-08-09 15:04:19 +0200 | [diff] [blame] | 995 | config1 |= NFC_V1_V2_CONFIG1_ECC_EN; |
| 996 | |
Uwe Kleine-König | f48d0f9 | 2012-04-23 11:23:40 +0200 | [diff] [blame] | 997 | if (!host->devtype_data->irqpending_quirk) |
Sascha Hauer | b8db2f5 | 2010-08-09 15:04:19 +0200 | [diff] [blame] | 998 | config1 |= NFC_V1_V2_CONFIG1_INT_MSK; |
Sascha Hauer | 6e85dfd | 2010-08-06 15:53:10 +0200 | [diff] [blame] | 999 | |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 1000 | host->eccsize = 1; |
| 1001 | |
| 1002 | writew(config1, NFC_V1_V2_CONFIG1); |
| 1003 | /* preset operation */ |
| 1004 | |
| 1005 | /* Unlock the internal RAM Buffer */ |
| 1006 | writew(0x2, NFC_V1_V2_CONFIG); |
| 1007 | |
| 1008 | /* Blocks to be unlocked */ |
| 1009 | writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR); |
| 1010 | writew(0xffff, NFC_V1_UNLOCKEND_BLKADDR); |
| 1011 | |
| 1012 | /* Unlock Block Command for given address range */ |
| 1013 | writew(0x4, NFC_V1_V2_WRPROT); |
| 1014 | } |
| 1015 | |
| 1016 | static void preset_v2(struct mtd_info *mtd) |
| 1017 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1018 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1019 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 1020 | uint16_t config1 = 0; |
| 1021 | |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 1022 | config1 |= NFC_V2_CONFIG1_FP_INT; |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 1023 | |
Uwe Kleine-König | f48d0f9 | 2012-04-23 11:23:40 +0200 | [diff] [blame] | 1024 | if (!host->devtype_data->irqpending_quirk) |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 1025 | config1 |= NFC_V1_V2_CONFIG1_INT_MSK; |
Sascha Hauer | 6e85dfd | 2010-08-06 15:53:10 +0200 | [diff] [blame] | 1026 | |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 1027 | if (mtd->writesize) { |
Sascha Hauer | b8db2f5 | 2010-08-09 15:04:19 +0200 | [diff] [blame] | 1028 | uint16_t pages_per_block = mtd->erasesize / mtd->writesize; |
| 1029 | |
Uwe Kleine-König | 1f42adc | 2015-02-10 19:59:56 +0100 | [diff] [blame] | 1030 | if (nand_chip->ecc.mode == NAND_ECC_HW) |
| 1031 | config1 |= NFC_V1_V2_CONFIG1_ECC_EN; |
| 1032 | |
Sascha Hauer | 6e85dfd | 2010-08-06 15:53:10 +0200 | [diff] [blame] | 1033 | host->eccsize = get_eccsize(mtd); |
| 1034 | if (host->eccsize == 4) |
Sascha Hauer | b8db2f5 | 2010-08-09 15:04:19 +0200 | [diff] [blame] | 1035 | config1 |= NFC_V2_CONFIG1_ECC_MODE_4; |
| 1036 | |
| 1037 | config1 |= NFC_V2_CONFIG1_PPB(ffs(pages_per_block) - 6); |
Sascha Hauer | 6e85dfd | 2010-08-06 15:53:10 +0200 | [diff] [blame] | 1038 | } else { |
| 1039 | host->eccsize = 1; |
| 1040 | } |
| 1041 | |
Sascha Hauer | b8db2f5 | 2010-08-09 15:04:19 +0200 | [diff] [blame] | 1042 | writew(config1, NFC_V1_V2_CONFIG1); |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 1043 | /* preset operation */ |
| 1044 | |
| 1045 | /* Unlock the internal RAM Buffer */ |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 1046 | writew(0x2, NFC_V1_V2_CONFIG); |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 1047 | |
| 1048 | /* Blocks to be unlocked */ |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 1049 | writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR0); |
| 1050 | writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR1); |
| 1051 | writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR2); |
| 1052 | writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR3); |
| 1053 | writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR0); |
| 1054 | writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR1); |
| 1055 | writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR2); |
| 1056 | writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR3); |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 1057 | |
| 1058 | /* Unlock Block Command for given address range */ |
Sascha Hauer | 1bc9918 | 2010-08-06 15:53:08 +0200 | [diff] [blame] | 1059 | writew(0x4, NFC_V1_V2_WRPROT); |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 1060 | } |
| 1061 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 1062 | static void preset_v3(struct mtd_info *mtd) |
| 1063 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1064 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1065 | struct mxc_nand_host *host = nand_get_controller_data(chip); |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 1066 | uint32_t config2, config3; |
| 1067 | int i, addr_phases; |
| 1068 | |
| 1069 | writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1); |
| 1070 | writel(NFC_V3_IPC_CREQ, NFC_V3_IPC); |
| 1071 | |
| 1072 | /* Unlock the internal RAM Buffer */ |
| 1073 | writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK, |
| 1074 | NFC_V3_WRPROT); |
| 1075 | |
| 1076 | /* Blocks to be unlocked */ |
| 1077 | for (i = 0; i < NAND_MAX_CHIPS; i++) |
Fabio Estevam | 1b15b1f | 2015-11-17 13:58:50 -0200 | [diff] [blame] | 1078 | writel(0xffff << 16, NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2)); |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 1079 | |
| 1080 | writel(0, NFC_V3_IPC); |
| 1081 | |
| 1082 | config2 = NFC_V3_CONFIG2_ONE_CYCLE | |
| 1083 | NFC_V3_CONFIG2_2CMD_PHASES | |
| 1084 | NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) | |
| 1085 | NFC_V3_CONFIG2_ST_CMD(0x70) | |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 1086 | NFC_V3_CONFIG2_INT_MSK | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 1087 | NFC_V3_CONFIG2_NUM_ADDR_PHASE0; |
| 1088 | |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 1089 | addr_phases = fls(chip->pagemask) >> 3; |
| 1090 | |
| 1091 | if (mtd->writesize == 2048) { |
| 1092 | config2 |= NFC_V3_CONFIG2_PS_2048; |
| 1093 | config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases); |
| 1094 | } else if (mtd->writesize == 4096) { |
| 1095 | config2 |= NFC_V3_CONFIG2_PS_4096; |
| 1096 | config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases); |
| 1097 | } else { |
| 1098 | config2 |= NFC_V3_CONFIG2_PS_512; |
| 1099 | config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1); |
| 1100 | } |
| 1101 | |
| 1102 | if (mtd->writesize) { |
Uwe Kleine-König | 1f42adc | 2015-02-10 19:59:56 +0100 | [diff] [blame] | 1103 | if (chip->ecc.mode == NAND_ECC_HW) |
| 1104 | config2 |= NFC_V3_CONFIG2_ECC_EN; |
| 1105 | |
Sascha Hauer | 71718a8e | 2012-06-06 12:33:15 +0200 | [diff] [blame] | 1106 | config2 |= NFC_V3_CONFIG2_PPB( |
| 1107 | ffs(mtd->erasesize / mtd->writesize) - 6, |
| 1108 | host->devtype_data->ppb_shift); |
Sascha Hauer | 71ec515 | 2010-08-06 15:53:11 +0200 | [diff] [blame] | 1109 | host->eccsize = get_eccsize(mtd); |
| 1110 | if (host->eccsize == 8) |
| 1111 | config2 |= NFC_V3_CONFIG2_ECC_MODE_8; |
| 1112 | } |
| 1113 | |
| 1114 | writel(config2, NFC_V3_CONFIG2); |
| 1115 | |
| 1116 | config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) | |
| 1117 | NFC_V3_CONFIG3_NO_SDMA | |
| 1118 | NFC_V3_CONFIG3_RBB_MODE | |
| 1119 | NFC_V3_CONFIG3_SBB(6) | /* Reset default */ |
| 1120 | NFC_V3_CONFIG3_ADD_OP(0); |
| 1121 | |
| 1122 | if (!(chip->options & NAND_BUSWIDTH_16)) |
| 1123 | config3 |= NFC_V3_CONFIG3_FW8; |
| 1124 | |
| 1125 | writel(config3, NFC_V3_CONFIG3); |
| 1126 | |
| 1127 | writel(0, NFC_V3_DELAY_LINE); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1128 | } |
| 1129 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1130 | /* Used by the upper layer to write command to NAND Flash for |
| 1131 | * different operations to be carried out on NAND Flash */ |
| 1132 | static void mxc_nand_command(struct mtd_info *mtd, unsigned command, |
| 1133 | int column, int page_addr) |
| 1134 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1135 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1136 | struct mxc_nand_host *host = nand_get_controller_data(nand_chip); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1137 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 1138 | 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] | 1139 | command, column, page_addr); |
| 1140 | |
| 1141 | /* Reset command state information */ |
| 1142 | host->status_request = false; |
| 1143 | |
| 1144 | /* Command pre-processing step */ |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1145 | switch (command) { |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 1146 | case NAND_CMD_RESET: |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1147 | host->devtype_data->preset(mtd); |
| 1148 | host->devtype_data->send_cmd(host, command, false); |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 1149 | break; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1150 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1151 | case NAND_CMD_STATUS: |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 1152 | host->buf_start = 0; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1153 | host->status_request = true; |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1154 | |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1155 | host->devtype_data->send_cmd(host, command, true); |
Uwe Kleine-König | c4ca399 | 2015-02-10 19:59:58 +0100 | [diff] [blame] | 1156 | WARN_ONCE(column != -1 || page_addr != -1, |
| 1157 | "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n", |
| 1158 | command, column, page_addr); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1159 | mxc_do_addr_cycle(mtd, column, page_addr); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1160 | break; |
| 1161 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1162 | case NAND_CMD_READ0: |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1163 | case NAND_CMD_READOOB: |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1164 | if (command == NAND_CMD_READ0) |
| 1165 | host->buf_start = column; |
| 1166 | else |
| 1167 | host->buf_start = column + mtd->writesize; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 1168 | |
Sascha Hauer | 5ea3202 | 2010-04-27 15:24:01 +0200 | [diff] [blame] | 1169 | command = NAND_CMD_READ0; /* only READ0 is valid */ |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1170 | |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1171 | host->devtype_data->send_cmd(host, command, false); |
Uwe Kleine-König | c4ca399 | 2015-02-10 19:59:58 +0100 | [diff] [blame] | 1172 | WARN_ONCE(column < 0, |
| 1173 | "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n", |
| 1174 | command, column, page_addr); |
| 1175 | mxc_do_addr_cycle(mtd, 0, page_addr); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1176 | |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame] | 1177 | if (mtd->writesize > 512) |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1178 | host->devtype_data->send_cmd(host, |
| 1179 | NAND_CMD_READSTART, true); |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 1180 | |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1181 | host->devtype_data->send_page(mtd, NFC_OUTPUT); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1182 | |
Sascha Hauer | 096bcc2 | 2012-05-29 10:16:09 +0200 | [diff] [blame] | 1183 | memcpy32_fromio(host->data_buf, host->main_area0, |
| 1184 | mtd->writesize); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1185 | copy_spare(mtd, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1186 | break; |
| 1187 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1188 | case NAND_CMD_SEQIN: |
Sascha Hauer | 5ea3202 | 2010-04-27 15:24:01 +0200 | [diff] [blame] | 1189 | if (column >= mtd->writesize) |
| 1190 | /* call ourself to read a page */ |
| 1191 | mxc_nand_command(mtd, NAND_CMD_READ0, 0, page_addr); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1192 | |
Sascha Hauer | 5ea3202 | 2010-04-27 15:24:01 +0200 | [diff] [blame] | 1193 | host->buf_start = column; |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1194 | |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1195 | host->devtype_data->send_cmd(host, command, false); |
Uwe Kleine-König | c4ca399 | 2015-02-10 19:59:58 +0100 | [diff] [blame] | 1196 | WARN_ONCE(column < -1, |
| 1197 | "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n", |
| 1198 | command, column, page_addr); |
| 1199 | mxc_do_addr_cycle(mtd, 0, page_addr); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1200 | break; |
| 1201 | |
| 1202 | case NAND_CMD_PAGEPROG: |
Sascha Hauer | 096bcc2 | 2012-05-29 10:16:09 +0200 | [diff] [blame] | 1203 | memcpy32_toio(host->main_area0, host->data_buf, mtd->writesize); |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 1204 | copy_spare(mtd, false); |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1205 | host->devtype_data->send_page(mtd, NFC_INPUT); |
| 1206 | host->devtype_data->send_cmd(host, command, true); |
Uwe Kleine-König | c4ca399 | 2015-02-10 19:59:58 +0100 | [diff] [blame] | 1207 | WARN_ONCE(column != -1 || page_addr != -1, |
| 1208 | "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n", |
| 1209 | command, column, page_addr); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1210 | mxc_do_addr_cycle(mtd, column, page_addr); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1211 | break; |
| 1212 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1213 | case NAND_CMD_READID: |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1214 | host->devtype_data->send_cmd(host, command, true); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1215 | mxc_do_addr_cycle(mtd, column, page_addr); |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1216 | host->devtype_data->send_read_id(host); |
Uwe Kleine-König | c4ca399 | 2015-02-10 19:59:58 +0100 | [diff] [blame] | 1217 | host->buf_start = 0; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1218 | break; |
| 1219 | |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1220 | case NAND_CMD_ERASE1: |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1221 | case NAND_CMD_ERASE2: |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1222 | host->devtype_data->send_cmd(host, command, false); |
Uwe Kleine-König | c4ca399 | 2015-02-10 19:59:58 +0100 | [diff] [blame] | 1223 | WARN_ONCE(column != -1, |
| 1224 | "Unexpected column value (cmd=%u, col=%d)\n", |
| 1225 | command, column); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 1226 | mxc_do_addr_cycle(mtd, column, page_addr); |
| 1227 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1228 | break; |
Uwe Kleine-König | 3d6e81c | 2015-02-10 19:59:59 +0100 | [diff] [blame] | 1229 | case NAND_CMD_PARAM: |
| 1230 | host->devtype_data->send_cmd(host, command, false); |
| 1231 | mxc_do_addr_cycle(mtd, column, page_addr); |
| 1232 | host->devtype_data->send_page(mtd, NFC_OUTPUT); |
| 1233 | memcpy32_fromio(host->data_buf, host->main_area0, 512); |
| 1234 | host->buf_start = 0; |
| 1235 | break; |
Uwe Kleine-König | 98ebb52 | 2015-02-10 20:00:00 +0100 | [diff] [blame] | 1236 | default: |
| 1237 | WARN_ONCE(1, "Unimplemented command (cmd=%u)\n", |
| 1238 | command); |
| 1239 | break; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1240 | } |
| 1241 | } |
| 1242 | |
Sascha Hauer | f137205 | 2009-10-21 14:25:27 +0200 | [diff] [blame] | 1243 | /* |
| 1244 | * The generic flash bbt decriptors overlap with our ecc |
| 1245 | * hardware, so define some i.MX specific ones. |
| 1246 | */ |
| 1247 | static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' }; |
| 1248 | static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' }; |
| 1249 | |
| 1250 | static struct nand_bbt_descr bbt_main_descr = { |
| 1251 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 1252 | | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, |
| 1253 | .offs = 0, |
| 1254 | .len = 4, |
| 1255 | .veroffs = 4, |
| 1256 | .maxblocks = 4, |
| 1257 | .pattern = bbt_pattern, |
| 1258 | }; |
| 1259 | |
| 1260 | static struct nand_bbt_descr bbt_mirror_descr = { |
| 1261 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 1262 | | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, |
| 1263 | .offs = 0, |
| 1264 | .len = 4, |
| 1265 | .veroffs = 4, |
| 1266 | .maxblocks = 4, |
| 1267 | .pattern = mirror_pattern, |
| 1268 | }; |
| 1269 | |
Uwe Kleine-König | f48d0f9 | 2012-04-23 11:23:40 +0200 | [diff] [blame] | 1270 | /* v1 + irqpending_quirk: i.MX21 */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1271 | 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] | 1272 | .preset = preset_v1, |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1273 | .send_cmd = send_cmd_v1_v2, |
| 1274 | .send_addr = send_addr_v1_v2, |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 1275 | .send_page = send_page_v1, |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1276 | .send_read_id = send_read_id_v1_v2, |
| 1277 | .get_dev_status = get_dev_status_v1_v2, |
| 1278 | .check_int = check_int_v1_v2, |
| 1279 | .irq_control = irq_control_v1_v2, |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 1280 | .get_ecc_status = get_ecc_status_v1, |
Boris Brezillon | a894cf6c | 2016-02-03 20:02:54 +0100 | [diff] [blame^] | 1281 | .ooblayout = &mxc_v1_ooblayout_ops, |
Uwe Kleine-König | 5e05a2d6 | 2012-04-23 11:23:38 +0200 | [diff] [blame] | 1282 | .select_chip = mxc_nand_select_chip_v1_v3, |
Uwe Kleine-König | 69d023b | 2012-04-23 11:23:39 +0200 | [diff] [blame] | 1283 | .correct_data = mxc_nand_correct_data_v1, |
Uwe Kleine-König | f48d0f9 | 2012-04-23 11:23:40 +0200 | [diff] [blame] | 1284 | .irqpending_quirk = 1, |
| 1285 | .needs_ip = 0, |
| 1286 | .regs_offset = 0xe00, |
| 1287 | .spare0_offset = 0x800, |
| 1288 | .spare_len = 16, |
| 1289 | .eccbytes = 3, |
| 1290 | .eccsize = 1, |
| 1291 | }; |
| 1292 | |
| 1293 | /* v1 + !irqpending_quirk: i.MX27, i.MX31 */ |
| 1294 | static const struct mxc_nand_devtype_data imx27_nand_devtype_data = { |
| 1295 | .preset = preset_v1, |
| 1296 | .send_cmd = send_cmd_v1_v2, |
| 1297 | .send_addr = send_addr_v1_v2, |
| 1298 | .send_page = send_page_v1, |
| 1299 | .send_read_id = send_read_id_v1_v2, |
| 1300 | .get_dev_status = get_dev_status_v1_v2, |
| 1301 | .check_int = check_int_v1_v2, |
| 1302 | .irq_control = irq_control_v1_v2, |
| 1303 | .get_ecc_status = get_ecc_status_v1, |
Boris Brezillon | a894cf6c | 2016-02-03 20:02:54 +0100 | [diff] [blame^] | 1304 | .ooblayout = &mxc_v1_ooblayout_ops, |
Uwe Kleine-König | f48d0f9 | 2012-04-23 11:23:40 +0200 | [diff] [blame] | 1305 | .select_chip = mxc_nand_select_chip_v1_v3, |
| 1306 | .correct_data = mxc_nand_correct_data_v1, |
| 1307 | .irqpending_quirk = 0, |
| 1308 | .needs_ip = 0, |
| 1309 | .regs_offset = 0xe00, |
| 1310 | .spare0_offset = 0x800, |
| 1311 | .axi_offset = 0, |
| 1312 | .spare_len = 16, |
| 1313 | .eccbytes = 3, |
| 1314 | .eccsize = 1, |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1315 | }; |
| 1316 | |
| 1317 | /* v21: i.MX25, i.MX35 */ |
| 1318 | 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] | 1319 | .preset = preset_v2, |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1320 | .send_cmd = send_cmd_v1_v2, |
| 1321 | .send_addr = send_addr_v1_v2, |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 1322 | .send_page = send_page_v2, |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1323 | .send_read_id = send_read_id_v1_v2, |
| 1324 | .get_dev_status = get_dev_status_v1_v2, |
| 1325 | .check_int = check_int_v1_v2, |
| 1326 | .irq_control = irq_control_v1_v2, |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 1327 | .get_ecc_status = get_ecc_status_v2, |
Boris Brezillon | a894cf6c | 2016-02-03 20:02:54 +0100 | [diff] [blame^] | 1328 | .ooblayout = &mxc_v2_ooblayout_ops, |
Uwe Kleine-König | 5e05a2d6 | 2012-04-23 11:23:38 +0200 | [diff] [blame] | 1329 | .select_chip = mxc_nand_select_chip_v2, |
Uwe Kleine-König | 69d023b | 2012-04-23 11:23:39 +0200 | [diff] [blame] | 1330 | .correct_data = mxc_nand_correct_data_v2_v3, |
Uwe Kleine-König | f48d0f9 | 2012-04-23 11:23:40 +0200 | [diff] [blame] | 1331 | .irqpending_quirk = 0, |
| 1332 | .needs_ip = 0, |
| 1333 | .regs_offset = 0x1e00, |
| 1334 | .spare0_offset = 0x1000, |
| 1335 | .axi_offset = 0, |
| 1336 | .spare_len = 64, |
| 1337 | .eccbytes = 9, |
| 1338 | .eccsize = 0, |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1339 | }; |
| 1340 | |
Sascha Hauer | 71718a8e | 2012-06-06 12:33:15 +0200 | [diff] [blame] | 1341 | /* v3.2a: i.MX51 */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1342 | static const struct mxc_nand_devtype_data imx51_nand_devtype_data = { |
| 1343 | .preset = preset_v3, |
| 1344 | .send_cmd = send_cmd_v3, |
| 1345 | .send_addr = send_addr_v3, |
| 1346 | .send_page = send_page_v3, |
| 1347 | .send_read_id = send_read_id_v3, |
| 1348 | .get_dev_status = get_dev_status_v3, |
| 1349 | .check_int = check_int_v3, |
| 1350 | .irq_control = irq_control_v3, |
Uwe Kleine-König | 6d38af2 | 2012-04-23 11:23:36 +0200 | [diff] [blame] | 1351 | .get_ecc_status = get_ecc_status_v3, |
Boris Brezillon | a894cf6c | 2016-02-03 20:02:54 +0100 | [diff] [blame^] | 1352 | .ooblayout = &mxc_v2_ooblayout_ops, |
Uwe Kleine-König | 5e05a2d6 | 2012-04-23 11:23:38 +0200 | [diff] [blame] | 1353 | .select_chip = mxc_nand_select_chip_v1_v3, |
Uwe Kleine-König | 69d023b | 2012-04-23 11:23:39 +0200 | [diff] [blame] | 1354 | .correct_data = mxc_nand_correct_data_v2_v3, |
Uwe Kleine-König | f48d0f9 | 2012-04-23 11:23:40 +0200 | [diff] [blame] | 1355 | .irqpending_quirk = 0, |
| 1356 | .needs_ip = 1, |
| 1357 | .regs_offset = 0, |
| 1358 | .spare0_offset = 0x1000, |
| 1359 | .axi_offset = 0x1e00, |
| 1360 | .spare_len = 64, |
| 1361 | .eccbytes = 0, |
| 1362 | .eccsize = 0, |
Sascha Hauer | 71718a8e | 2012-06-06 12:33:15 +0200 | [diff] [blame] | 1363 | .ppb_shift = 7, |
| 1364 | }; |
| 1365 | |
| 1366 | /* v3.2b: i.MX53 */ |
| 1367 | static const struct mxc_nand_devtype_data imx53_nand_devtype_data = { |
| 1368 | .preset = preset_v3, |
| 1369 | .send_cmd = send_cmd_v3, |
| 1370 | .send_addr = send_addr_v3, |
| 1371 | .send_page = send_page_v3, |
| 1372 | .send_read_id = send_read_id_v3, |
| 1373 | .get_dev_status = get_dev_status_v3, |
| 1374 | .check_int = check_int_v3, |
| 1375 | .irq_control = irq_control_v3, |
| 1376 | .get_ecc_status = get_ecc_status_v3, |
Boris Brezillon | a894cf6c | 2016-02-03 20:02:54 +0100 | [diff] [blame^] | 1377 | .ooblayout = &mxc_v2_ooblayout_ops, |
Sascha Hauer | 71718a8e | 2012-06-06 12:33:15 +0200 | [diff] [blame] | 1378 | .select_chip = mxc_nand_select_chip_v1_v3, |
| 1379 | .correct_data = mxc_nand_correct_data_v2_v3, |
| 1380 | .irqpending_quirk = 0, |
| 1381 | .needs_ip = 1, |
| 1382 | .regs_offset = 0, |
| 1383 | .spare0_offset = 0x1000, |
| 1384 | .axi_offset = 0x1e00, |
| 1385 | .spare_len = 64, |
| 1386 | .eccbytes = 0, |
| 1387 | .eccsize = 0, |
| 1388 | .ppb_shift = 8, |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1389 | }; |
| 1390 | |
Shawn Guo | 4d62435 | 2012-09-15 13:34:09 +0800 | [diff] [blame] | 1391 | static inline int is_imx21_nfc(struct mxc_nand_host *host) |
| 1392 | { |
| 1393 | return host->devtype_data == &imx21_nand_devtype_data; |
| 1394 | } |
| 1395 | |
| 1396 | static inline int is_imx27_nfc(struct mxc_nand_host *host) |
| 1397 | { |
| 1398 | return host->devtype_data == &imx27_nand_devtype_data; |
| 1399 | } |
| 1400 | |
| 1401 | static inline int is_imx25_nfc(struct mxc_nand_host *host) |
| 1402 | { |
| 1403 | return host->devtype_data == &imx25_nand_devtype_data; |
| 1404 | } |
| 1405 | |
| 1406 | static inline int is_imx51_nfc(struct mxc_nand_host *host) |
| 1407 | { |
| 1408 | return host->devtype_data == &imx51_nand_devtype_data; |
| 1409 | } |
| 1410 | |
| 1411 | static inline int is_imx53_nfc(struct mxc_nand_host *host) |
| 1412 | { |
| 1413 | return host->devtype_data == &imx53_nand_devtype_data; |
| 1414 | } |
| 1415 | |
Krzysztof Kozlowski | 8d1e568 | 2015-05-02 00:50:01 +0900 | [diff] [blame] | 1416 | static const struct platform_device_id mxcnd_devtype[] = { |
Shawn Guo | 4d62435 | 2012-09-15 13:34:09 +0800 | [diff] [blame] | 1417 | { |
| 1418 | .name = "imx21-nand", |
| 1419 | .driver_data = (kernel_ulong_t) &imx21_nand_devtype_data, |
| 1420 | }, { |
| 1421 | .name = "imx27-nand", |
| 1422 | .driver_data = (kernel_ulong_t) &imx27_nand_devtype_data, |
| 1423 | }, { |
| 1424 | .name = "imx25-nand", |
| 1425 | .driver_data = (kernel_ulong_t) &imx25_nand_devtype_data, |
| 1426 | }, { |
| 1427 | .name = "imx51-nand", |
| 1428 | .driver_data = (kernel_ulong_t) &imx51_nand_devtype_data, |
| 1429 | }, { |
| 1430 | .name = "imx53-nand", |
| 1431 | .driver_data = (kernel_ulong_t) &imx53_nand_devtype_data, |
| 1432 | }, { |
| 1433 | /* sentinel */ |
| 1434 | } |
| 1435 | }; |
| 1436 | MODULE_DEVICE_TABLE(platform, mxcnd_devtype); |
| 1437 | |
Uwe Kleine-König | 6436356 | 2012-04-23 11:23:41 +0200 | [diff] [blame] | 1438 | #ifdef CONFIG_OF_MTD |
| 1439 | static const struct of_device_id mxcnd_dt_ids[] = { |
| 1440 | { |
| 1441 | .compatible = "fsl,imx21-nand", |
| 1442 | .data = &imx21_nand_devtype_data, |
| 1443 | }, { |
| 1444 | .compatible = "fsl,imx27-nand", |
| 1445 | .data = &imx27_nand_devtype_data, |
| 1446 | }, { |
| 1447 | .compatible = "fsl,imx25-nand", |
| 1448 | .data = &imx25_nand_devtype_data, |
| 1449 | }, { |
| 1450 | .compatible = "fsl,imx51-nand", |
| 1451 | .data = &imx51_nand_devtype_data, |
Sascha Hauer | 71718a8e | 2012-06-06 12:33:15 +0200 | [diff] [blame] | 1452 | }, { |
| 1453 | .compatible = "fsl,imx53-nand", |
| 1454 | .data = &imx53_nand_devtype_data, |
Uwe Kleine-König | 6436356 | 2012-04-23 11:23:41 +0200 | [diff] [blame] | 1455 | }, |
| 1456 | { /* sentinel */ } |
| 1457 | }; |
Luis de Bethencourt | b33c35b | 2015-09-18 00:13:28 +0200 | [diff] [blame] | 1458 | MODULE_DEVICE_TABLE(of, mxcnd_dt_ids); |
Uwe Kleine-König | 6436356 | 2012-04-23 11:23:41 +0200 | [diff] [blame] | 1459 | |
| 1460 | static int __init mxcnd_probe_dt(struct mxc_nand_host *host) |
| 1461 | { |
| 1462 | struct device_node *np = host->dev->of_node; |
| 1463 | struct mxc_nand_platform_data *pdata = &host->pdata; |
| 1464 | const struct of_device_id *of_id = |
| 1465 | of_match_device(mxcnd_dt_ids, host->dev); |
| 1466 | int buswidth; |
| 1467 | |
| 1468 | if (!np) |
| 1469 | return 1; |
| 1470 | |
| 1471 | if (of_get_nand_ecc_mode(np) >= 0) |
| 1472 | pdata->hw_ecc = 1; |
| 1473 | |
| 1474 | pdata->flash_bbt = of_get_nand_on_flash_bbt(np); |
| 1475 | |
| 1476 | buswidth = of_get_nand_bus_width(np); |
| 1477 | if (buswidth < 0) |
| 1478 | return buswidth; |
| 1479 | |
| 1480 | pdata->width = buswidth / 8; |
| 1481 | |
| 1482 | host->devtype_data = of_id->data; |
| 1483 | |
| 1484 | return 0; |
| 1485 | } |
| 1486 | #else |
| 1487 | static int __init mxcnd_probe_dt(struct mxc_nand_host *host) |
| 1488 | { |
| 1489 | return 1; |
| 1490 | } |
| 1491 | #endif |
| 1492 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 1493 | static int mxcnd_probe(struct platform_device *pdev) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1494 | { |
| 1495 | struct nand_chip *this; |
| 1496 | struct mtd_info *mtd; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1497 | struct mxc_nand_host *host; |
| 1498 | struct resource *res; |
Dmitry Eremin-Solenikov | d4ed8f1 | 2011-06-02 18:00:43 +0400 | [diff] [blame] | 1499 | int err = 0; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1500 | |
| 1501 | /* Allocate memory for MTD device structure and private data */ |
Huang Shijie | a590055 | 2013-12-21 00:02:27 +0800 | [diff] [blame] | 1502 | host = devm_kzalloc(&pdev->dev, sizeof(struct mxc_nand_host), |
| 1503 | GFP_KERNEL); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1504 | if (!host) |
| 1505 | return -ENOMEM; |
| 1506 | |
Huang Shijie | a590055 | 2013-12-21 00:02:27 +0800 | [diff] [blame] | 1507 | /* allocate a temporary buffer for the nand_scan_ident() */ |
| 1508 | host->data_buf = devm_kzalloc(&pdev->dev, PAGE_SIZE, GFP_KERNEL); |
| 1509 | if (!host->data_buf) |
| 1510 | return -ENOMEM; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 1511 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1512 | host->dev = &pdev->dev; |
| 1513 | /* structures must be linked */ |
| 1514 | this = &host->nand; |
Boris BREZILLON | a008deb | 2015-12-10 09:00:12 +0100 | [diff] [blame] | 1515 | mtd = nand_to_mtd(this); |
David Brownell | 87f39f0 | 2009-03-26 00:42:50 -0700 | [diff] [blame] | 1516 | mtd->dev.parent = &pdev->dev; |
Sascha Hauer | 1fbff0a | 2009-10-21 16:06:27 +0200 | [diff] [blame] | 1517 | mtd->name = DRIVER_NAME; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1518 | |
| 1519 | /* 50 us command delay time */ |
| 1520 | this->chip_delay = 5; |
| 1521 | |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1522 | nand_set_controller_data(this, host); |
Brian Norris | a61ae81 | 2015-10-30 20:33:25 -0700 | [diff] [blame] | 1523 | nand_set_flash_node(this, pdev->dev.of_node), |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1524 | this->dev_ready = mxc_nand_dev_ready; |
| 1525 | this->cmdfunc = mxc_nand_command; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1526 | this->read_byte = mxc_nand_read_byte; |
| 1527 | this->read_word = mxc_nand_read_word; |
| 1528 | this->write_buf = mxc_nand_write_buf; |
| 1529 | this->read_buf = mxc_nand_read_buf; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1530 | |
Fabio Estevam | 24b82d3 | 2012-09-05 11:52:27 -0300 | [diff] [blame] | 1531 | host->clk = devm_clk_get(&pdev->dev, NULL); |
Sascha Hauer | e4a09cb | 2012-06-06 12:33:13 +0200 | [diff] [blame] | 1532 | if (IS_ERR(host->clk)) |
| 1533 | return PTR_ERR(host->clk); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1534 | |
Sascha Hauer | 71885b6 | 2012-06-06 12:33:14 +0200 | [diff] [blame] | 1535 | err = mxcnd_probe_dt(host); |
Shawn Guo | 4d62435 | 2012-09-15 13:34:09 +0800 | [diff] [blame] | 1536 | if (err > 0) { |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 1537 | struct mxc_nand_platform_data *pdata = |
| 1538 | dev_get_platdata(&pdev->dev); |
Shawn Guo | 4d62435 | 2012-09-15 13:34:09 +0800 | [diff] [blame] | 1539 | if (pdata) { |
| 1540 | host->pdata = *pdata; |
| 1541 | host->devtype_data = (struct mxc_nand_devtype_data *) |
| 1542 | pdev->id_entry->driver_data; |
| 1543 | } else { |
| 1544 | err = -ENODEV; |
| 1545 | } |
| 1546 | } |
Sascha Hauer | 71885b6 | 2012-06-06 12:33:14 +0200 | [diff] [blame] | 1547 | if (err < 0) |
| 1548 | return err; |
| 1549 | |
| 1550 | if (host->devtype_data->needs_ip) { |
| 1551 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Thierry Reding | b0de774 | 2013-01-21 11:09:12 +0100 | [diff] [blame] | 1552 | host->regs_ip = devm_ioremap_resource(&pdev->dev, res); |
| 1553 | if (IS_ERR(host->regs_ip)) |
| 1554 | return PTR_ERR(host->regs_ip); |
Sascha Hauer | 71885b6 | 2012-06-06 12:33:14 +0200 | [diff] [blame] | 1555 | |
| 1556 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1557 | } else { |
| 1558 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1559 | } |
| 1560 | |
Thierry Reding | b0de774 | 2013-01-21 11:09:12 +0100 | [diff] [blame] | 1561 | host->base = devm_ioremap_resource(&pdev->dev, res); |
| 1562 | if (IS_ERR(host->base)) |
| 1563 | return PTR_ERR(host->base); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1564 | |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 1565 | host->main_area0 = host->base; |
Sascha Hauer | 9467114 | 2009-10-05 12:14:21 +0200 | [diff] [blame] | 1566 | |
Uwe Kleine-König | f48d0f9 | 2012-04-23 11:23:40 +0200 | [diff] [blame] | 1567 | if (host->devtype_data->regs_offset) |
| 1568 | host->regs = host->base + host->devtype_data->regs_offset; |
| 1569 | host->spare0 = host->base + host->devtype_data->spare0_offset; |
| 1570 | if (host->devtype_data->axi_offset) |
| 1571 | host->regs_axi = host->base + host->devtype_data->axi_offset; |
| 1572 | |
| 1573 | this->ecc.bytes = host->devtype_data->eccbytes; |
| 1574 | host->eccsize = host->devtype_data->eccsize; |
| 1575 | |
| 1576 | this->select_chip = host->devtype_data->select_chip; |
| 1577 | this->ecc.size = 512; |
Boris Brezillon | a894cf6c | 2016-02-03 20:02:54 +0100 | [diff] [blame^] | 1578 | mtd_set_ooblayout(mtd, host->devtype_data->ooblayout); |
Uwe Kleine-König | f48d0f9 | 2012-04-23 11:23:40 +0200 | [diff] [blame] | 1579 | |
Uwe Kleine-König | 6436356 | 2012-04-23 11:23:41 +0200 | [diff] [blame] | 1580 | if (host->pdata.hw_ecc) { |
Sascha Hauer | 13e1add | 2009-10-21 10:39:05 +0200 | [diff] [blame] | 1581 | this->ecc.calculate = mxc_nand_calculate_ecc; |
| 1582 | this->ecc.hwctl = mxc_nand_enable_hwecc; |
Uwe Kleine-König | 69d023b | 2012-04-23 11:23:39 +0200 | [diff] [blame] | 1583 | this->ecc.correct = host->devtype_data->correct_data; |
Sascha Hauer | 13e1add | 2009-10-21 10:39:05 +0200 | [diff] [blame] | 1584 | this->ecc.mode = NAND_ECC_HW; |
Sascha Hauer | 13e1add | 2009-10-21 10:39:05 +0200 | [diff] [blame] | 1585 | } else { |
| 1586 | this->ecc.mode = NAND_ECC_SOFT; |
Rafał Miłecki | c1c7040 | 2016-04-08 12:23:46 +0200 | [diff] [blame] | 1587 | this->ecc.algo = NAND_ECC_HAMMING; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1588 | } |
| 1589 | |
Uwe Kleine-König | 6436356 | 2012-04-23 11:23:41 +0200 | [diff] [blame] | 1590 | /* NAND bus width determines access functions used by upper layer */ |
| 1591 | if (host->pdata.width == 2) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1592 | this->options |= NAND_BUSWIDTH_16; |
Sascha Hauer | 13e1add | 2009-10-21 10:39:05 +0200 | [diff] [blame] | 1593 | |
Uwe Kleine-König | 6436356 | 2012-04-23 11:23:41 +0200 | [diff] [blame] | 1594 | if (host->pdata.flash_bbt) { |
Sascha Hauer | f137205 | 2009-10-21 14:25:27 +0200 | [diff] [blame] | 1595 | this->bbt_td = &bbt_main_descr; |
| 1596 | this->bbt_md = &bbt_mirror_descr; |
| 1597 | /* update flash based bbt */ |
Brian Norris | bb9ebd4 | 2011-05-31 16:31:23 -0700 | [diff] [blame] | 1598 | this->bbt_options |= NAND_BBT_USE_FLASH; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1599 | } |
| 1600 | |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 1601 | init_completion(&host->op_completion); |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 1602 | |
| 1603 | host->irq = platform_get_irq(pdev, 0); |
Fabio Estevam | 26fbf48 | 2014-02-14 01:09:34 -0200 | [diff] [blame] | 1604 | if (host->irq < 0) |
| 1605 | return host->irq; |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 1606 | |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 1607 | /* |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1608 | * Use host->devtype_data->irq_control() here instead of irq_control() |
| 1609 | * because we must not disable_irq_nosync without having requested the |
| 1610 | * irq. |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 1611 | */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1612 | host->devtype_data->irq_control(host, 0); |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 1613 | |
Sascha Hauer | e4a09cb | 2012-06-06 12:33:13 +0200 | [diff] [blame] | 1614 | err = devm_request_irq(&pdev->dev, host->irq, mxc_nfc_irq, |
Michael Opdenacker | b1eb234 | 2013-10-13 08:21:32 +0200 | [diff] [blame] | 1615 | 0, DRIVER_NAME, host); |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 1616 | if (err) |
Sascha Hauer | e4a09cb | 2012-06-06 12:33:13 +0200 | [diff] [blame] | 1617 | return err; |
| 1618 | |
Fabio Estevam | dcedf62 | 2013-12-02 00:50:02 -0200 | [diff] [blame] | 1619 | err = clk_prepare_enable(host->clk); |
| 1620 | if (err) |
| 1621 | return err; |
Sascha Hauer | e4a09cb | 2012-06-06 12:33:13 +0200 | [diff] [blame] | 1622 | host->clk_act = 1; |
Ivo Clarysse | d484018 | 2010-04-08 16:14:44 +0200 | [diff] [blame] | 1623 | |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 1624 | /* |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 1625 | * Now that we "own" the interrupt make sure the interrupt mask bit is |
| 1626 | * cleared on i.MX21. Otherwise we can't read the interrupt status bit |
| 1627 | * on this machine. |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 1628 | */ |
Uwe Kleine-König | f48d0f9 | 2012-04-23 11:23:40 +0200 | [diff] [blame] | 1629 | if (host->devtype_data->irqpending_quirk) { |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 1630 | disable_irq_nosync(host->irq); |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1631 | host->devtype_data->irq_control(host, 1); |
Uwe Kleine-König | 8556958 | 2012-04-23 11:23:34 +0200 | [diff] [blame] | 1632 | } |
Sascha Hauer | 63f1474 | 2010-10-18 10:16:26 +0200 | [diff] [blame] | 1633 | |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 1634 | /* first scan to find the device and get the page size */ |
Shawn Guo | 4d62435 | 2012-09-15 13:34:09 +0800 | [diff] [blame] | 1635 | if (nand_scan_ident(mtd, is_imx25_nfc(host) ? 4 : 1, NULL)) { |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 1636 | err = -ENXIO; |
| 1637 | goto escan; |
| 1638 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1639 | |
Huang Shijie | a590055 | 2013-12-21 00:02:27 +0800 | [diff] [blame] | 1640 | /* allocate the right size buffer now */ |
| 1641 | devm_kfree(&pdev->dev, (void *)host->data_buf); |
| 1642 | host->data_buf = devm_kzalloc(&pdev->dev, mtd->writesize + mtd->oobsize, |
| 1643 | GFP_KERNEL); |
| 1644 | if (!host->data_buf) { |
| 1645 | err = -ENOMEM; |
| 1646 | goto escan; |
| 1647 | } |
| 1648 | |
Sascha Hauer | 6e85dfd | 2010-08-06 15:53:10 +0200 | [diff] [blame] | 1649 | /* Call preset again, with correct writesize this time */ |
Uwe Kleine-König | e4303b2 | 2012-04-23 11:23:35 +0200 | [diff] [blame] | 1650 | host->devtype_data->preset(mtd); |
Sascha Hauer | 6e85dfd | 2010-08-06 15:53:10 +0200 | [diff] [blame] | 1651 | |
Boris Brezillon | a894cf6c | 2016-02-03 20:02:54 +0100 | [diff] [blame^] | 1652 | if (!this->ecc.bytes) { |
| 1653 | if (host->eccsize == 8) |
| 1654 | this->ecc.bytes = 18; |
| 1655 | else if (host->eccsize == 4) |
| 1656 | this->ecc.bytes = 9; |
Baruch Siach | 8eeb4c5 | 2015-05-13 11:17:39 +0300 | [diff] [blame] | 1657 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1658 | |
Baruch Siach | 7e7e473 | 2015-05-13 11:17:37 +0300 | [diff] [blame] | 1659 | /* |
| 1660 | * Experimentation shows that i.MX NFC can only handle up to 218 oob |
| 1661 | * bytes. Limit used_oobsize to 218 so as to not confuse copy_spare() |
| 1662 | * into copying invalid data to/from the spare IO buffer, as this |
| 1663 | * might cause ECC data corruption when doing sub-page write to a |
| 1664 | * partially written page. |
| 1665 | */ |
| 1666 | host->used_oobsize = min(mtd->oobsize, 218U); |
| 1667 | |
Mike Dunn | 6a918ba | 2012-03-11 14:21:11 -0700 | [diff] [blame] | 1668 | if (this->ecc.mode == NAND_ECC_HW) { |
Shawn Guo | 4d62435 | 2012-09-15 13:34:09 +0800 | [diff] [blame] | 1669 | if (is_imx21_nfc(host) || is_imx27_nfc(host)) |
Mike Dunn | 6a918ba | 2012-03-11 14:21:11 -0700 | [diff] [blame] | 1670 | this->ecc.strength = 1; |
| 1671 | else |
| 1672 | this->ecc.strength = (host->eccsize == 4) ? 4 : 8; |
| 1673 | } |
| 1674 | |
Sascha Hauer | 4a43faf | 2012-05-25 16:22:42 +0200 | [diff] [blame] | 1675 | /* second phase scan */ |
| 1676 | if (nand_scan_tail(mtd)) { |
| 1677 | err = -ENXIO; |
| 1678 | goto escan; |
| 1679 | } |
| 1680 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1681 | /* Register the partitions */ |
Uwe Kleine-König | 6436356 | 2012-04-23 11:23:41 +0200 | [diff] [blame] | 1682 | mtd_device_parse_register(mtd, part_probes, |
Brian Norris | a61ae81 | 2015-10-30 20:33:25 -0700 | [diff] [blame] | 1683 | NULL, |
Uwe Kleine-König | 6436356 | 2012-04-23 11:23:41 +0200 | [diff] [blame] | 1684 | host->pdata.parts, |
| 1685 | host->pdata.nr_parts); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1686 | |
| 1687 | platform_set_drvdata(pdev, host); |
| 1688 | |
| 1689 | return 0; |
| 1690 | |
| 1691 | escan: |
Lothar Waßmann | c10d8ee | 2012-12-06 08:42:27 +0100 | [diff] [blame] | 1692 | if (host->clk_act) |
| 1693 | clk_disable_unprepare(host->clk); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1694 | |
| 1695 | return err; |
| 1696 | } |
| 1697 | |
Bill Pemberton | 810b7e0 | 2012-11-19 13:26:04 -0500 | [diff] [blame] | 1698 | static int mxcnd_remove(struct platform_device *pdev) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1699 | { |
| 1700 | struct mxc_nand_host *host = platform_get_drvdata(pdev); |
| 1701 | |
Boris BREZILLON | a008deb | 2015-12-10 09:00:12 +0100 | [diff] [blame] | 1702 | nand_release(nand_to_mtd(&host->nand)); |
Wei Yongjun | 8bfd4f7 | 2013-12-17 11:35:35 +0800 | [diff] [blame] | 1703 | if (host->clk_act) |
| 1704 | clk_disable_unprepare(host->clk); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1705 | |
| 1706 | return 0; |
| 1707 | } |
| 1708 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1709 | static struct platform_driver mxcnd_driver = { |
| 1710 | .driver = { |
| 1711 | .name = DRIVER_NAME, |
Uwe Kleine-König | 6436356 | 2012-04-23 11:23:41 +0200 | [diff] [blame] | 1712 | .of_match_table = of_match_ptr(mxcnd_dt_ids), |
Eric Bénard | 04dd0d3 | 2010-06-17 20:59:04 +0200 | [diff] [blame] | 1713 | }, |
Shawn Guo | 4d62435 | 2012-09-15 13:34:09 +0800 | [diff] [blame] | 1714 | .id_table = mxcnd_devtype, |
Fabio Estevam | ddf16d6 | 2012-09-05 11:35:25 -0300 | [diff] [blame] | 1715 | .probe = mxcnd_probe, |
Bill Pemberton | 5153b88 | 2012-11-19 13:21:24 -0500 | [diff] [blame] | 1716 | .remove = mxcnd_remove, |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1717 | }; |
Fabio Estevam | ddf16d6 | 2012-09-05 11:35:25 -0300 | [diff] [blame] | 1718 | module_platform_driver(mxcnd_driver); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1719 | |
| 1720 | MODULE_AUTHOR("Freescale Semiconductor, Inc."); |
| 1721 | MODULE_DESCRIPTION("MXC NAND MTD driver"); |
| 1722 | MODULE_LICENSE("GPL"); |