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