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