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