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