Boris Brezillon | bb69634 | 2018-11-20 10:02:34 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 2 | /* |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 3 | * ST Microelectronics |
| 4 | * Flexible Static Memory Controller (FSMC) |
| 5 | * Driver for NAND portions |
| 6 | * |
| 7 | * Copyright © 2010 ST Microelectronics |
| 8 | * Vipin Kumar <vipin.kumar@st.com> |
| 9 | * Ashish Priyadarshi |
| 10 | * |
Boris Brezillon | 187c5448 | 2018-02-05 23:02:02 +0100 | [diff] [blame] | 11 | * Based on drivers/mtd/nand/nomadik_nand.c (removed in v3.8) |
Boris Brezillon | 7b6afee | 2018-02-05 23:02:03 +0100 | [diff] [blame] | 12 | * Copyright © 2007 STMicroelectronics Pvt. Ltd. |
| 13 | * Copyright © 2009 Alessandro Rubini |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <linux/clk.h> |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 17 | #include <linux/completion.h> |
| 18 | #include <linux/dmaengine.h> |
| 19 | #include <linux/dma-direction.h> |
| 20 | #include <linux/dma-mapping.h> |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 21 | #include <linux/err.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/resource.h> |
| 25 | #include <linux/sched.h> |
| 26 | #include <linux/types.h> |
| 27 | #include <linux/mtd/mtd.h> |
Boris Brezillon | d4092d7 | 2017-08-04 17:29:10 +0200 | [diff] [blame] | 28 | #include <linux/mtd/rawnand.h> |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 29 | #include <linux/mtd/nand_ecc.h> |
| 30 | #include <linux/platform_device.h> |
Stefan Roese | eea6281 | 2012-03-16 10:19:31 +0100 | [diff] [blame] | 31 | #include <linux/of.h> |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 32 | #include <linux/mtd/partitions.h> |
| 33 | #include <linux/io.h> |
| 34 | #include <linux/slab.h> |
Linus Walleij | 593cd87 | 2010-11-29 13:52:19 +0100 | [diff] [blame] | 35 | #include <linux/amba/bus.h> |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 36 | #include <mtd/mtd-abi.h> |
| 37 | |
Linus Walleij | 4404d7d | 2016-12-18 12:34:55 +0100 | [diff] [blame] | 38 | /* fsmc controller registers for NOR flash */ |
| 39 | #define CTRL 0x0 |
| 40 | /* ctrl register definitions */ |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 41 | #define BANK_ENABLE BIT(0) |
| 42 | #define MUXED BIT(1) |
Linus Walleij | 4404d7d | 2016-12-18 12:34:55 +0100 | [diff] [blame] | 43 | #define NOR_DEV (2 << 2) |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 44 | #define WIDTH_16 BIT(4) |
| 45 | #define RSTPWRDWN BIT(6) |
| 46 | #define WPROT BIT(7) |
| 47 | #define WRT_ENABLE BIT(12) |
| 48 | #define WAIT_ENB BIT(13) |
Linus Walleij | 4404d7d | 2016-12-18 12:34:55 +0100 | [diff] [blame] | 49 | |
| 50 | #define CTRL_TIM 0x4 |
| 51 | /* ctrl_tim register definitions */ |
| 52 | |
| 53 | #define FSMC_NOR_BANK_SZ 0x8 |
| 54 | #define FSMC_NOR_REG_SIZE 0x40 |
| 55 | |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 56 | #define FSMC_NOR_REG(base, bank, reg) ((base) + \ |
| 57 | (FSMC_NOR_BANK_SZ * (bank)) + \ |
| 58 | (reg)) |
Linus Walleij | 4404d7d | 2016-12-18 12:34:55 +0100 | [diff] [blame] | 59 | |
| 60 | /* fsmc controller registers for NAND flash */ |
Boris Brezillon | 8f3931e | 2018-07-09 22:09:34 +0200 | [diff] [blame] | 61 | #define FSMC_PC 0x00 |
Linus Walleij | 4404d7d | 2016-12-18 12:34:55 +0100 | [diff] [blame] | 62 | /* pc register definitions */ |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 63 | #define FSMC_RESET BIT(0) |
| 64 | #define FSMC_WAITON BIT(1) |
| 65 | #define FSMC_ENABLE BIT(2) |
| 66 | #define FSMC_DEVTYPE_NAND BIT(3) |
| 67 | #define FSMC_DEVWID_16 BIT(4) |
| 68 | #define FSMC_ECCEN BIT(6) |
| 69 | #define FSMC_ECCPLEN_256 BIT(7) |
Linus Walleij | 4404d7d | 2016-12-18 12:34:55 +0100 | [diff] [blame] | 70 | #define FSMC_TCLR_SHIFT (9) |
| 71 | #define FSMC_TCLR_MASK (0xF) |
Linus Walleij | 4404d7d | 2016-12-18 12:34:55 +0100 | [diff] [blame] | 72 | #define FSMC_TAR_SHIFT (13) |
| 73 | #define FSMC_TAR_MASK (0xF) |
| 74 | #define STS 0x04 |
| 75 | /* sts register definitions */ |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 76 | #define FSMC_CODE_RDY BIT(15) |
Linus Walleij | 4404d7d | 2016-12-18 12:34:55 +0100 | [diff] [blame] | 77 | #define COMM 0x08 |
| 78 | /* comm register definitions */ |
Linus Walleij | 4404d7d | 2016-12-18 12:34:55 +0100 | [diff] [blame] | 79 | #define FSMC_TSET_SHIFT 0 |
| 80 | #define FSMC_TSET_MASK 0xFF |
Linus Walleij | 4404d7d | 2016-12-18 12:34:55 +0100 | [diff] [blame] | 81 | #define FSMC_TWAIT_SHIFT 8 |
| 82 | #define FSMC_TWAIT_MASK 0xFF |
Linus Walleij | 4404d7d | 2016-12-18 12:34:55 +0100 | [diff] [blame] | 83 | #define FSMC_THOLD_SHIFT 16 |
| 84 | #define FSMC_THOLD_MASK 0xFF |
Linus Walleij | 4404d7d | 2016-12-18 12:34:55 +0100 | [diff] [blame] | 85 | #define FSMC_THIZ_SHIFT 24 |
| 86 | #define FSMC_THIZ_MASK 0xFF |
| 87 | #define ATTRIB 0x0C |
| 88 | #define IOATA 0x10 |
| 89 | #define ECC1 0x14 |
| 90 | #define ECC2 0x18 |
| 91 | #define ECC3 0x1C |
| 92 | #define FSMC_NAND_BANK_SZ 0x20 |
| 93 | |
Linus Walleij | 4404d7d | 2016-12-18 12:34:55 +0100 | [diff] [blame] | 94 | #define FSMC_BUSY_WAIT_TIMEOUT (1 * HZ) |
| 95 | |
| 96 | struct fsmc_nand_timings { |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 97 | u8 tclr; |
| 98 | u8 tar; |
| 99 | u8 thiz; |
| 100 | u8 thold; |
| 101 | u8 twait; |
| 102 | u8 tset; |
Linus Walleij | 4404d7d | 2016-12-18 12:34:55 +0100 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | enum access_mode { |
| 106 | USE_DMA_ACCESS = 1, |
| 107 | USE_WORD_ACCESS, |
| 108 | }; |
| 109 | |
| 110 | /** |
Thomas Petazzoni | e7cda01 | 2017-03-21 11:03:56 +0100 | [diff] [blame] | 111 | * struct fsmc_nand_data - structure for FSMC NAND device state |
| 112 | * |
Boris Brezillon | ad71148 | 2018-11-20 10:02:33 +0100 | [diff] [blame] | 113 | * @base: Inherit from the nand_controller struct |
Thomas Petazzoni | e7cda01 | 2017-03-21 11:03:56 +0100 | [diff] [blame] | 114 | * @pid: Part ID on the AMBA PrimeCell format |
Thomas Petazzoni | e7cda01 | 2017-03-21 11:03:56 +0100 | [diff] [blame] | 115 | * @nand: Chip related info for a NAND flash. |
Thomas Petazzoni | e7cda01 | 2017-03-21 11:03:56 +0100 | [diff] [blame] | 116 | * |
| 117 | * @bank: Bank number for probed device. |
Boris Brezillon | 5b47f40 | 2018-11-20 10:02:31 +0100 | [diff] [blame] | 118 | * @dev: Parent device |
| 119 | * @mode: Access mode |
Thomas Petazzoni | e7cda01 | 2017-03-21 11:03:56 +0100 | [diff] [blame] | 120 | * @clk: Clock structure for FSMC. |
| 121 | * |
| 122 | * @read_dma_chan: DMA channel for read access |
| 123 | * @write_dma_chan: DMA channel for write access to NAND |
| 124 | * @dma_access_complete: Completion structure |
| 125 | * |
Boris Brezillon | 5b47f40 | 2018-11-20 10:02:31 +0100 | [diff] [blame] | 126 | * @dev_timings: NAND timings |
| 127 | * |
Thomas Petazzoni | e7cda01 | 2017-03-21 11:03:56 +0100 | [diff] [blame] | 128 | * @data_pa: NAND Physical port for Data. |
| 129 | * @data_va: NAND port for Data. |
| 130 | * @cmd_va: NAND port for Command. |
| 131 | * @addr_va: NAND port for Address. |
Miquel Raynal | 4df6ed4 | 2018-02-16 15:22:47 +0100 | [diff] [blame] | 132 | * @regs_va: Registers base address for a given bank. |
Thomas Petazzoni | e7cda01 | 2017-03-21 11:03:56 +0100 | [diff] [blame] | 133 | */ |
| 134 | struct fsmc_nand_data { |
Boris Brezillon | ad71148 | 2018-11-20 10:02:33 +0100 | [diff] [blame] | 135 | struct nand_controller base; |
Thomas Petazzoni | e7cda01 | 2017-03-21 11:03:56 +0100 | [diff] [blame] | 136 | u32 pid; |
| 137 | struct nand_chip nand; |
Thomas Petazzoni | e7cda01 | 2017-03-21 11:03:56 +0100 | [diff] [blame] | 138 | |
| 139 | unsigned int bank; |
| 140 | struct device *dev; |
| 141 | enum access_mode mode; |
| 142 | struct clk *clk; |
| 143 | |
| 144 | /* DMA related objects */ |
| 145 | struct dma_chan *read_dma_chan; |
| 146 | struct dma_chan *write_dma_chan; |
| 147 | struct completion dma_access_complete; |
| 148 | |
| 149 | struct fsmc_nand_timings *dev_timings; |
| 150 | |
| 151 | dma_addr_t data_pa; |
| 152 | void __iomem *data_va; |
| 153 | void __iomem *cmd_va; |
| 154 | void __iomem *addr_va; |
| 155 | void __iomem *regs_va; |
Thomas Petazzoni | e7cda01 | 2017-03-21 11:03:56 +0100 | [diff] [blame] | 156 | }; |
| 157 | |
Boris Brezillon | 22b4695 | 2016-02-03 20:01:42 +0100 | [diff] [blame] | 158 | static int fsmc_ecc1_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 159 | struct mtd_oob_region *oobregion) |
| 160 | { |
| 161 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 162 | |
| 163 | if (section >= chip->ecc.steps) |
| 164 | return -ERANGE; |
| 165 | |
| 166 | oobregion->offset = (section * 16) + 2; |
| 167 | oobregion->length = 3; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static int fsmc_ecc1_ooblayout_free(struct mtd_info *mtd, int section, |
| 173 | struct mtd_oob_region *oobregion) |
| 174 | { |
| 175 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 176 | |
| 177 | if (section >= chip->ecc.steps) |
| 178 | return -ERANGE; |
| 179 | |
| 180 | oobregion->offset = (section * 16) + 8; |
| 181 | |
| 182 | if (section < chip->ecc.steps - 1) |
| 183 | oobregion->length = 8; |
| 184 | else |
| 185 | oobregion->length = mtd->oobsize - oobregion->offset; |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static const struct mtd_ooblayout_ops fsmc_ecc1_ooblayout_ops = { |
| 191 | .ecc = fsmc_ecc1_ooblayout_ecc, |
| 192 | .free = fsmc_ecc1_ooblayout_free, |
| 193 | }; |
| 194 | |
Boris Brezillon | 04a123a | 2016-02-09 15:01:21 +0100 | [diff] [blame] | 195 | /* |
| 196 | * ECC placement definitions in oobfree type format. |
| 197 | * There are 13 bytes of ecc for every 512 byte block and it has to be read |
| 198 | * consecutively and immediately after the 512 byte data block for hardware to |
| 199 | * generate the error bit offsets in 512 byte data. |
| 200 | */ |
Boris Brezillon | 22b4695 | 2016-02-03 20:01:42 +0100 | [diff] [blame] | 201 | static int fsmc_ecc4_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 202 | struct mtd_oob_region *oobregion) |
| 203 | { |
| 204 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 205 | |
| 206 | if (section >= chip->ecc.steps) |
| 207 | return -ERANGE; |
| 208 | |
| 209 | oobregion->length = chip->ecc.bytes; |
| 210 | |
| 211 | if (!section && mtd->writesize <= 512) |
| 212 | oobregion->offset = 0; |
| 213 | else |
| 214 | oobregion->offset = (section * 16) + 2; |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static int fsmc_ecc4_ooblayout_free(struct mtd_info *mtd, int section, |
| 220 | struct mtd_oob_region *oobregion) |
| 221 | { |
| 222 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 223 | |
| 224 | if (section >= chip->ecc.steps) |
| 225 | return -ERANGE; |
| 226 | |
| 227 | oobregion->offset = (section * 16) + 15; |
| 228 | |
| 229 | if (section < chip->ecc.steps - 1) |
| 230 | oobregion->length = 3; |
| 231 | else |
| 232 | oobregion->length = mtd->oobsize - oobregion->offset; |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static const struct mtd_ooblayout_ops fsmc_ecc4_ooblayout_ops = { |
| 238 | .ecc = fsmc_ecc4_ooblayout_ecc, |
| 239 | .free = fsmc_ecc4_ooblayout_free, |
| 240 | }; |
| 241 | |
Boris Brezillon | bfc535f | 2018-11-20 10:02:30 +0100 | [diff] [blame] | 242 | static inline struct fsmc_nand_data *nand_to_fsmc(struct nand_chip *chip) |
Boris BREZILLON | 277af42 | 2015-12-10 08:59:46 +0100 | [diff] [blame] | 243 | { |
Boris Brezillon | bfc535f | 2018-11-20 10:02:30 +0100 | [diff] [blame] | 244 | return container_of(chip, struct fsmc_nand_data, nand); |
Boris BREZILLON | 277af42 | 2015-12-10 08:59:46 +0100 | [diff] [blame] | 245 | } |
| 246 | |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 247 | /* |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 248 | * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine |
| 249 | * |
| 250 | * This routine initializes timing parameters related to NAND memory access in |
| 251 | * FSMC registers |
| 252 | */ |
Thomas Petazzoni | 6335b50 | 2017-04-29 10:52:34 +0200 | [diff] [blame] | 253 | static void fsmc_nand_setup(struct fsmc_nand_data *host, |
Thomas Petazzoni | 1debdb9 | 2017-04-29 10:52:36 +0200 | [diff] [blame] | 254 | struct fsmc_nand_timings *tims) |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 255 | { |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 256 | u32 value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON; |
| 257 | u32 tclr, tar, thiz, thold, twait, tset; |
Vipin Kumar | e2f6bce | 2012-03-14 11:47:14 +0530 | [diff] [blame] | 258 | |
| 259 | tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT; |
| 260 | tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT; |
| 261 | thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT; |
| 262 | thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT; |
| 263 | twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT; |
| 264 | tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 265 | |
Thomas Petazzoni | 6335b50 | 2017-04-29 10:52:34 +0200 | [diff] [blame] | 266 | if (host->nand.options & NAND_BUSWIDTH_16) |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 267 | value |= FSMC_DEVWID_16; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 268 | |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 269 | writel_relaxed(value | tclr | tar, host->regs_va + FSMC_PC); |
Miquel Raynal | 4df6ed4 | 2018-02-16 15:22:47 +0100 | [diff] [blame] | 270 | writel_relaxed(thiz | thold | twait | tset, host->regs_va + COMM); |
| 271 | writel_relaxed(thiz | thold | twait | tset, host->regs_va + ATTRIB); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Thomas Petazzoni | d9fb079 | 2017-04-29 10:52:35 +0200 | [diff] [blame] | 274 | static int fsmc_calc_timings(struct fsmc_nand_data *host, |
| 275 | const struct nand_sdr_timings *sdrt, |
| 276 | struct fsmc_nand_timings *tims) |
| 277 | { |
| 278 | unsigned long hclk = clk_get_rate(host->clk); |
| 279 | unsigned long hclkn = NSEC_PER_SEC / hclk; |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 280 | u32 thiz, thold, twait, tset; |
Thomas Petazzoni | d9fb079 | 2017-04-29 10:52:35 +0200 | [diff] [blame] | 281 | |
| 282 | if (sdrt->tRC_min < 30000) |
| 283 | return -EOPNOTSUPP; |
| 284 | |
| 285 | tims->tar = DIV_ROUND_UP(sdrt->tAR_min / 1000, hclkn) - 1; |
| 286 | if (tims->tar > FSMC_TAR_MASK) |
| 287 | tims->tar = FSMC_TAR_MASK; |
| 288 | tims->tclr = DIV_ROUND_UP(sdrt->tCLR_min / 1000, hclkn) - 1; |
| 289 | if (tims->tclr > FSMC_TCLR_MASK) |
| 290 | tims->tclr = FSMC_TCLR_MASK; |
| 291 | |
| 292 | thiz = sdrt->tCS_min - sdrt->tWP_min; |
| 293 | tims->thiz = DIV_ROUND_UP(thiz / 1000, hclkn); |
| 294 | |
| 295 | thold = sdrt->tDH_min; |
| 296 | if (thold < sdrt->tCH_min) |
| 297 | thold = sdrt->tCH_min; |
| 298 | if (thold < sdrt->tCLH_min) |
| 299 | thold = sdrt->tCLH_min; |
| 300 | if (thold < sdrt->tWH_min) |
| 301 | thold = sdrt->tWH_min; |
| 302 | if (thold < sdrt->tALH_min) |
| 303 | thold = sdrt->tALH_min; |
| 304 | if (thold < sdrt->tREH_min) |
| 305 | thold = sdrt->tREH_min; |
| 306 | tims->thold = DIV_ROUND_UP(thold / 1000, hclkn); |
| 307 | if (tims->thold == 0) |
| 308 | tims->thold = 1; |
| 309 | else if (tims->thold > FSMC_THOLD_MASK) |
| 310 | tims->thold = FSMC_THOLD_MASK; |
| 311 | |
| 312 | twait = max(sdrt->tRP_min, sdrt->tWP_min); |
| 313 | tims->twait = DIV_ROUND_UP(twait / 1000, hclkn) - 1; |
| 314 | if (tims->twait == 0) |
| 315 | tims->twait = 1; |
| 316 | else if (tims->twait > FSMC_TWAIT_MASK) |
| 317 | tims->twait = FSMC_TWAIT_MASK; |
| 318 | |
| 319 | tset = max(sdrt->tCS_min - sdrt->tWP_min, |
| 320 | sdrt->tCEA_max - sdrt->tREA_max); |
| 321 | tims->tset = DIV_ROUND_UP(tset / 1000, hclkn) - 1; |
| 322 | if (tims->tset == 0) |
| 323 | tims->tset = 1; |
| 324 | else if (tims->tset > FSMC_TSET_MASK) |
| 325 | tims->tset = FSMC_TSET_MASK; |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
Miquel Raynal | 4c46667 | 2020-05-29 13:13:13 +0200 | [diff] [blame] | 330 | static int fsmc_setup_interface(struct nand_chip *nand, int csline, |
| 331 | const struct nand_interface_config *conf) |
Thomas Petazzoni | d9fb079 | 2017-04-29 10:52:35 +0200 | [diff] [blame] | 332 | { |
Boris Brezillon | 1e809f7 | 2018-11-20 10:02:32 +0100 | [diff] [blame] | 333 | struct fsmc_nand_data *host = nand_to_fsmc(nand); |
Thomas Petazzoni | d9fb079 | 2017-04-29 10:52:35 +0200 | [diff] [blame] | 334 | struct fsmc_nand_timings tims; |
| 335 | const struct nand_sdr_timings *sdrt; |
| 336 | int ret; |
| 337 | |
| 338 | sdrt = nand_get_sdr_timings(conf); |
| 339 | if (IS_ERR(sdrt)) |
| 340 | return PTR_ERR(sdrt); |
| 341 | |
| 342 | ret = fsmc_calc_timings(host, sdrt, &tims); |
| 343 | if (ret) |
| 344 | return ret; |
| 345 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 346 | if (csline == NAND_DATA_IFACE_CHECK_ONLY) |
Thomas Petazzoni | d9fb079 | 2017-04-29 10:52:35 +0200 | [diff] [blame] | 347 | return 0; |
| 348 | |
| 349 | fsmc_nand_setup(host, &tims); |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 354 | /* |
| 355 | * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers |
| 356 | */ |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 357 | static void fsmc_enable_hwecc(struct nand_chip *chip, int mode) |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 358 | { |
Boris Brezillon | bfc535f | 2018-11-20 10:02:30 +0100 | [diff] [blame] | 359 | struct fsmc_nand_data *host = nand_to_fsmc(chip); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 360 | |
Boris Brezillon | 8f3931e | 2018-07-09 22:09:34 +0200 | [diff] [blame] | 361 | writel_relaxed(readl(host->regs_va + FSMC_PC) & ~FSMC_ECCPLEN_256, |
| 362 | host->regs_va + FSMC_PC); |
| 363 | writel_relaxed(readl(host->regs_va + FSMC_PC) & ~FSMC_ECCEN, |
| 364 | host->regs_va + FSMC_PC); |
| 365 | writel_relaxed(readl(host->regs_va + FSMC_PC) | FSMC_ECCEN, |
| 366 | host->regs_va + FSMC_PC); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /* |
| 370 | * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 371 | * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 372 | * max of 8-bits) |
| 373 | */ |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 374 | static int fsmc_read_hwecc_ecc4(struct nand_chip *chip, const u8 *data, |
| 375 | u8 *ecc) |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 376 | { |
Boris Brezillon | bfc535f | 2018-11-20 10:02:30 +0100 | [diff] [blame] | 377 | struct fsmc_nand_data *host = nand_to_fsmc(chip); |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 378 | u32 ecc_tmp; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 379 | unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT; |
| 380 | |
| 381 | do { |
Miquel Raynal | 4df6ed4 | 2018-02-16 15:22:47 +0100 | [diff] [blame] | 382 | if (readl_relaxed(host->regs_va + STS) & FSMC_CODE_RDY) |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 383 | break; |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 384 | |
| 385 | cond_resched(); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 386 | } while (!time_after_eq(jiffies, deadline)); |
| 387 | |
Vipin Kumar | 712c4ad | 2012-03-14 11:47:16 +0530 | [diff] [blame] | 388 | if (time_after_eq(jiffies, deadline)) { |
| 389 | dev_err(host->dev, "calculate ecc timed out\n"); |
| 390 | return -ETIMEDOUT; |
| 391 | } |
| 392 | |
Miquel Raynal | 4df6ed4 | 2018-02-16 15:22:47 +0100 | [diff] [blame] | 393 | ecc_tmp = readl_relaxed(host->regs_va + ECC1); |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 394 | ecc[0] = ecc_tmp; |
| 395 | ecc[1] = ecc_tmp >> 8; |
| 396 | ecc[2] = ecc_tmp >> 16; |
| 397 | ecc[3] = ecc_tmp >> 24; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 398 | |
Miquel Raynal | 4df6ed4 | 2018-02-16 15:22:47 +0100 | [diff] [blame] | 399 | ecc_tmp = readl_relaxed(host->regs_va + ECC2); |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 400 | ecc[4] = ecc_tmp; |
| 401 | ecc[5] = ecc_tmp >> 8; |
| 402 | ecc[6] = ecc_tmp >> 16; |
| 403 | ecc[7] = ecc_tmp >> 24; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 404 | |
Miquel Raynal | 4df6ed4 | 2018-02-16 15:22:47 +0100 | [diff] [blame] | 405 | ecc_tmp = readl_relaxed(host->regs_va + ECC3); |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 406 | ecc[8] = ecc_tmp; |
| 407 | ecc[9] = ecc_tmp >> 8; |
| 408 | ecc[10] = ecc_tmp >> 16; |
| 409 | ecc[11] = ecc_tmp >> 24; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 410 | |
Miquel Raynal | 4df6ed4 | 2018-02-16 15:22:47 +0100 | [diff] [blame] | 411 | ecc_tmp = readl_relaxed(host->regs_va + STS); |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 412 | ecc[12] = ecc_tmp >> 16; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 419 | * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 420 | * max of 1-bit) |
| 421 | */ |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 422 | static int fsmc_read_hwecc_ecc1(struct nand_chip *chip, const u8 *data, |
| 423 | u8 *ecc) |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 424 | { |
Boris Brezillon | bfc535f | 2018-11-20 10:02:30 +0100 | [diff] [blame] | 425 | struct fsmc_nand_data *host = nand_to_fsmc(chip); |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 426 | u32 ecc_tmp; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 427 | |
Miquel Raynal | 4df6ed4 | 2018-02-16 15:22:47 +0100 | [diff] [blame] | 428 | ecc_tmp = readl_relaxed(host->regs_va + ECC1); |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 429 | ecc[0] = ecc_tmp; |
| 430 | ecc[1] = ecc_tmp >> 8; |
| 431 | ecc[2] = ecc_tmp >> 16; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
Vipin Kumar | 519300c | 2012-03-07 17:00:49 +0530 | [diff] [blame] | 436 | /* Count the number of 0's in buff upto a max of max_bits */ |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 437 | static int count_written_bits(u8 *buff, int size, int max_bits) |
Vipin Kumar | 519300c | 2012-03-07 17:00:49 +0530 | [diff] [blame] | 438 | { |
| 439 | int k, written_bits = 0; |
| 440 | |
| 441 | for (k = 0; k < size; k++) { |
| 442 | written_bits += hweight8(~buff[k]); |
| 443 | if (written_bits > max_bits) |
| 444 | break; |
| 445 | } |
| 446 | |
| 447 | return written_bits; |
| 448 | } |
| 449 | |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 450 | static void dma_complete(void *param) |
| 451 | { |
| 452 | struct fsmc_nand_data *host = param; |
| 453 | |
| 454 | complete(&host->dma_access_complete); |
| 455 | } |
| 456 | |
| 457 | static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len, |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 458 | enum dma_data_direction direction) |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 459 | { |
| 460 | struct dma_chan *chan; |
| 461 | struct dma_device *dma_dev; |
| 462 | struct dma_async_tx_descriptor *tx; |
| 463 | dma_addr_t dma_dst, dma_src, dma_addr; |
| 464 | dma_cookie_t cookie; |
| 465 | unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT; |
| 466 | int ret; |
Nicholas Mc Guire | 818a45b | 2015-03-13 07:54:46 -0400 | [diff] [blame] | 467 | unsigned long time_left; |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 468 | |
| 469 | if (direction == DMA_TO_DEVICE) |
| 470 | chan = host->write_dma_chan; |
| 471 | else if (direction == DMA_FROM_DEVICE) |
| 472 | chan = host->read_dma_chan; |
| 473 | else |
| 474 | return -EINVAL; |
| 475 | |
| 476 | dma_dev = chan->device; |
| 477 | dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction); |
| 478 | |
| 479 | if (direction == DMA_TO_DEVICE) { |
| 480 | dma_src = dma_addr; |
| 481 | dma_dst = host->data_pa; |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 482 | } else { |
| 483 | dma_src = host->data_pa; |
| 484 | dma_dst = dma_addr; |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src, |
| 488 | len, flags); |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 489 | if (!tx) { |
| 490 | dev_err(host->dev, "device_prep_dma_memcpy error\n"); |
Bartlomiej Zolnierkiewicz | d1806a5 | 2012-11-05 10:00:14 +0000 | [diff] [blame] | 491 | ret = -EIO; |
| 492 | goto unmap_dma; |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | tx->callback = dma_complete; |
| 496 | tx->callback_param = host; |
| 497 | cookie = tx->tx_submit(tx); |
| 498 | |
| 499 | ret = dma_submit_error(cookie); |
| 500 | if (ret) { |
| 501 | dev_err(host->dev, "dma_submit_error %d\n", cookie); |
Bartlomiej Zolnierkiewicz | d1806a5 | 2012-11-05 10:00:14 +0000 | [diff] [blame] | 502 | goto unmap_dma; |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | dma_async_issue_pending(chan); |
| 506 | |
Nicholas Mc Guire | 818a45b | 2015-03-13 07:54:46 -0400 | [diff] [blame] | 507 | time_left = |
Vipin Kumar | 928aa2a | 2012-10-09 16:14:48 +0530 | [diff] [blame] | 508 | wait_for_completion_timeout(&host->dma_access_complete, |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 509 | msecs_to_jiffies(3000)); |
Nicholas Mc Guire | 818a45b | 2015-03-13 07:54:46 -0400 | [diff] [blame] | 510 | if (time_left == 0) { |
Vinod Koul | b177ea3 | 2014-10-11 21:10:32 +0530 | [diff] [blame] | 511 | dmaengine_terminate_all(chan); |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 512 | dev_err(host->dev, "wait_for_completion_timeout\n"); |
Nicholas Mc Guire | 0bda3e1 | 2015-03-13 07:54:45 -0400 | [diff] [blame] | 513 | ret = -ETIMEDOUT; |
Bartlomiej Zolnierkiewicz | d1806a5 | 2012-11-05 10:00:14 +0000 | [diff] [blame] | 514 | goto unmap_dma; |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 515 | } |
| 516 | |
Bartlomiej Zolnierkiewicz | d1806a5 | 2012-11-05 10:00:14 +0000 | [diff] [blame] | 517 | ret = 0; |
| 518 | |
| 519 | unmap_dma: |
| 520 | dma_unmap_single(dma_dev->dev, dma_addr, len, direction); |
| 521 | |
| 522 | return ret; |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 523 | } |
| 524 | |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 525 | /* |
Vipin Kumar | 604e754 | 2012-03-14 11:47:17 +0530 | [diff] [blame] | 526 | * fsmc_write_buf - write buffer to chip |
Boris Brezillon | bfc535f | 2018-11-20 10:02:30 +0100 | [diff] [blame] | 527 | * @host: FSMC NAND controller |
Vipin Kumar | 604e754 | 2012-03-14 11:47:17 +0530 | [diff] [blame] | 528 | * @buf: data buffer |
| 529 | * @len: number of bytes to write |
| 530 | */ |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 531 | static void fsmc_write_buf(struct fsmc_nand_data *host, const u8 *buf, |
Boris Brezillon | bfc535f | 2018-11-20 10:02:30 +0100 | [diff] [blame] | 532 | int len) |
Vipin Kumar | 604e754 | 2012-03-14 11:47:17 +0530 | [diff] [blame] | 533 | { |
| 534 | int i; |
Vipin Kumar | 604e754 | 2012-03-14 11:47:17 +0530 | [diff] [blame] | 535 | |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 536 | if (IS_ALIGNED((uintptr_t)buf, sizeof(u32)) && |
| 537 | IS_ALIGNED(len, sizeof(u32))) { |
| 538 | u32 *p = (u32 *)buf; |
| 539 | |
Vipin Kumar | 604e754 | 2012-03-14 11:47:17 +0530 | [diff] [blame] | 540 | len = len >> 2; |
| 541 | for (i = 0; i < len; i++) |
Miquel Raynal | 4df6ed4 | 2018-02-16 15:22:47 +0100 | [diff] [blame] | 542 | writel_relaxed(p[i], host->data_va); |
Vipin Kumar | 604e754 | 2012-03-14 11:47:17 +0530 | [diff] [blame] | 543 | } else { |
| 544 | for (i = 0; i < len; i++) |
Miquel Raynal | 4df6ed4 | 2018-02-16 15:22:47 +0100 | [diff] [blame] | 545 | writeb_relaxed(buf[i], host->data_va); |
Vipin Kumar | 604e754 | 2012-03-14 11:47:17 +0530 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * fsmc_read_buf - read chip data into buffer |
Boris Brezillon | bfc535f | 2018-11-20 10:02:30 +0100 | [diff] [blame] | 551 | * @host: FSMC NAND controller |
Vipin Kumar | 604e754 | 2012-03-14 11:47:17 +0530 | [diff] [blame] | 552 | * @buf: buffer to store date |
| 553 | * @len: number of bytes to read |
| 554 | */ |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 555 | static void fsmc_read_buf(struct fsmc_nand_data *host, u8 *buf, int len) |
Vipin Kumar | 604e754 | 2012-03-14 11:47:17 +0530 | [diff] [blame] | 556 | { |
| 557 | int i; |
Vipin Kumar | 604e754 | 2012-03-14 11:47:17 +0530 | [diff] [blame] | 558 | |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 559 | if (IS_ALIGNED((uintptr_t)buf, sizeof(u32)) && |
| 560 | IS_ALIGNED(len, sizeof(u32))) { |
| 561 | u32 *p = (u32 *)buf; |
| 562 | |
Vipin Kumar | 604e754 | 2012-03-14 11:47:17 +0530 | [diff] [blame] | 563 | len = len >> 2; |
| 564 | for (i = 0; i < len; i++) |
Miquel Raynal | 4df6ed4 | 2018-02-16 15:22:47 +0100 | [diff] [blame] | 565 | p[i] = readl_relaxed(host->data_va); |
Vipin Kumar | 604e754 | 2012-03-14 11:47:17 +0530 | [diff] [blame] | 566 | } else { |
| 567 | for (i = 0; i < len; i++) |
Miquel Raynal | 4df6ed4 | 2018-02-16 15:22:47 +0100 | [diff] [blame] | 568 | buf[i] = readb_relaxed(host->data_va); |
Vipin Kumar | 604e754 | 2012-03-14 11:47:17 +0530 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
| 572 | /* |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 573 | * fsmc_read_buf_dma - read chip data into buffer |
Boris Brezillon | bfc535f | 2018-11-20 10:02:30 +0100 | [diff] [blame] | 574 | * @host: FSMC NAND controller |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 575 | * @buf: buffer to store date |
| 576 | * @len: number of bytes to read |
| 577 | */ |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 578 | static void fsmc_read_buf_dma(struct fsmc_nand_data *host, u8 *buf, |
Boris Brezillon | bfc535f | 2018-11-20 10:02:30 +0100 | [diff] [blame] | 579 | int len) |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 580 | { |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 581 | dma_xfer(host, buf, len, DMA_FROM_DEVICE); |
| 582 | } |
| 583 | |
| 584 | /* |
| 585 | * fsmc_write_buf_dma - write buffer to chip |
Boris Brezillon | bfc535f | 2018-11-20 10:02:30 +0100 | [diff] [blame] | 586 | * @host: FSMC NAND controller |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 587 | * @buf: data buffer |
| 588 | * @len: number of bytes to write |
| 589 | */ |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 590 | static void fsmc_write_buf_dma(struct fsmc_nand_data *host, const u8 *buf, |
Boris Brezillon | bfc535f | 2018-11-20 10:02:30 +0100 | [diff] [blame] | 591 | int len) |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 592 | { |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 593 | dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE); |
| 594 | } |
| 595 | |
Miquel Raynal | 4da712e | 2018-02-16 15:22:48 +0100 | [diff] [blame] | 596 | /* |
| 597 | * fsmc_exec_op - hook called by the core to execute NAND operations |
| 598 | * |
| 599 | * This controller is simple enough and thus does not need to use the parser |
| 600 | * provided by the core, instead, handle every situation here. |
| 601 | */ |
| 602 | static int fsmc_exec_op(struct nand_chip *chip, const struct nand_operation *op, |
| 603 | bool check_only) |
| 604 | { |
Boris Brezillon | bfc535f | 2018-11-20 10:02:30 +0100 | [diff] [blame] | 605 | struct fsmc_nand_data *host = nand_to_fsmc(chip); |
Miquel Raynal | 4da712e | 2018-02-16 15:22:48 +0100 | [diff] [blame] | 606 | const struct nand_op_instr *instr = NULL; |
| 607 | int ret = 0; |
| 608 | unsigned int op_id; |
| 609 | int i; |
| 610 | |
Boris Brezillon | ce446b4 | 2020-04-18 21:42:17 +0200 | [diff] [blame] | 611 | if (check_only) |
| 612 | return 0; |
| 613 | |
Miquel Raynal | 4da712e | 2018-02-16 15:22:48 +0100 | [diff] [blame] | 614 | pr_debug("Executing operation [%d instructions]:\n", op->ninstrs); |
Boris Brezillon | 550b9fc | 2018-11-11 08:55:17 +0100 | [diff] [blame] | 615 | |
Miquel Raynal | 4da712e | 2018-02-16 15:22:48 +0100 | [diff] [blame] | 616 | for (op_id = 0; op_id < op->ninstrs; op_id++) { |
| 617 | instr = &op->instrs[op_id]; |
| 618 | |
Sascha Hauer | bf82832 | 2019-05-21 09:06:31 +0200 | [diff] [blame] | 619 | nand_op_trace(" ", instr); |
| 620 | |
Miquel Raynal | 4da712e | 2018-02-16 15:22:48 +0100 | [diff] [blame] | 621 | switch (instr->type) { |
| 622 | case NAND_OP_CMD_INSTR: |
Miquel Raynal | 4da712e | 2018-02-16 15:22:48 +0100 | [diff] [blame] | 623 | writeb_relaxed(instr->ctx.cmd.opcode, host->cmd_va); |
| 624 | break; |
| 625 | |
| 626 | case NAND_OP_ADDR_INSTR: |
Miquel Raynal | 4da712e | 2018-02-16 15:22:48 +0100 | [diff] [blame] | 627 | for (i = 0; i < instr->ctx.addr.naddrs; i++) |
| 628 | writeb_relaxed(instr->ctx.addr.addrs[i], |
| 629 | host->addr_va); |
| 630 | break; |
| 631 | |
| 632 | case NAND_OP_DATA_IN_INSTR: |
Miquel Raynal | 4da712e | 2018-02-16 15:22:48 +0100 | [diff] [blame] | 633 | if (host->mode == USE_DMA_ACCESS) |
Boris Brezillon | bfc535f | 2018-11-20 10:02:30 +0100 | [diff] [blame] | 634 | fsmc_read_buf_dma(host, instr->ctx.data.buf.in, |
Miquel Raynal | 4da712e | 2018-02-16 15:22:48 +0100 | [diff] [blame] | 635 | instr->ctx.data.len); |
| 636 | else |
Boris Brezillon | bfc535f | 2018-11-20 10:02:30 +0100 | [diff] [blame] | 637 | fsmc_read_buf(host, instr->ctx.data.buf.in, |
Miquel Raynal | 4da712e | 2018-02-16 15:22:48 +0100 | [diff] [blame] | 638 | instr->ctx.data.len); |
| 639 | break; |
| 640 | |
| 641 | case NAND_OP_DATA_OUT_INSTR: |
Miquel Raynal | 4da712e | 2018-02-16 15:22:48 +0100 | [diff] [blame] | 642 | if (host->mode == USE_DMA_ACCESS) |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 643 | fsmc_write_buf_dma(host, |
| 644 | instr->ctx.data.buf.out, |
Miquel Raynal | 4da712e | 2018-02-16 15:22:48 +0100 | [diff] [blame] | 645 | instr->ctx.data.len); |
| 646 | else |
Boris Brezillon | bfc535f | 2018-11-20 10:02:30 +0100 | [diff] [blame] | 647 | fsmc_write_buf(host, instr->ctx.data.buf.out, |
Miquel Raynal | 4da712e | 2018-02-16 15:22:48 +0100 | [diff] [blame] | 648 | instr->ctx.data.len); |
| 649 | break; |
| 650 | |
| 651 | case NAND_OP_WAITRDY_INSTR: |
Miquel Raynal | 4da712e | 2018-02-16 15:22:48 +0100 | [diff] [blame] | 652 | ret = nand_soft_waitrdy(chip, |
| 653 | instr->ctx.waitrdy.timeout_ms); |
| 654 | break; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | return ret; |
| 659 | } |
| 660 | |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 661 | /* |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 662 | * fsmc_read_page_hwecc |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 663 | * @chip: nand chip info structure |
| 664 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 665 | * @oob_required: caller expects OOB data read to chip->oob_poi |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 666 | * @page: page number to read |
| 667 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 668 | * This routine is needed for fsmc version 8 as reading from NAND chip has to be |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 669 | * performed in a strict sequence as follows: |
| 670 | * data(512 byte) -> ecc(13 byte) |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 671 | * After this read, fsmc hardware generates and reports error data bits(up to a |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 672 | * max of 8 bits) |
| 673 | */ |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 674 | static int fsmc_read_page_hwecc(struct nand_chip *chip, u8 *buf, |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 675 | int oob_required, int page) |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 676 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 677 | struct mtd_info *mtd = nand_to_mtd(chip); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 678 | int i, j, s, stat, eccsize = chip->ecc.size; |
| 679 | int eccbytes = chip->ecc.bytes; |
| 680 | int eccsteps = chip->ecc.steps; |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 681 | u8 *p = buf; |
| 682 | u8 *ecc_calc = chip->ecc.calc_buf; |
| 683 | u8 *ecc_code = chip->ecc.code_buf; |
Gustavo A. R. Silva | 41d6f0d | 2018-10-10 17:58:58 +0200 | [diff] [blame] | 684 | int off, len, ret, group = 0; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 685 | /* |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 686 | * ecc_oob is intentionally taken as u16. In 16bit devices, we |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 687 | * end up reading 14 bytes (7 words) from oob. The local array is |
| 688 | * to maintain word alignment |
| 689 | */ |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 690 | u16 ecc_oob[7]; |
| 691 | u8 *oob = (u8 *)&ecc_oob[0]; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 692 | unsigned int max_bitflips = 0; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 693 | |
| 694 | for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 695 | nand_read_page_op(chip, page, s * eccsize, NULL, 0); |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 696 | chip->ecc.hwctl(chip, NAND_ECC_READ); |
Miquel Raynal | b451f5b | 2020-05-07 12:52:36 +0200 | [diff] [blame] | 697 | ret = nand_read_data_op(chip, p, eccsize, false, false); |
Gustavo A. R. Silva | 41d6f0d | 2018-10-10 17:58:58 +0200 | [diff] [blame] | 698 | if (ret) |
| 699 | return ret; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 700 | |
| 701 | for (j = 0; j < eccbytes;) { |
Boris Brezillon | 04a123a | 2016-02-09 15:01:21 +0100 | [diff] [blame] | 702 | struct mtd_oob_region oobregion; |
Boris Brezillon | 04a123a | 2016-02-09 15:01:21 +0100 | [diff] [blame] | 703 | |
| 704 | ret = mtd_ooblayout_ecc(mtd, group++, &oobregion); |
| 705 | if (ret) |
| 706 | return ret; |
| 707 | |
| 708 | off = oobregion.offset; |
| 709 | len = oobregion.length; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 710 | |
| 711 | /* |
Vipin Kumar | 4cbe1bf0 | 2012-03-14 11:47:09 +0530 | [diff] [blame] | 712 | * length is intentionally kept a higher multiple of 2 |
| 713 | * to read at least 13 bytes even in case of 16 bit NAND |
| 714 | * devices |
| 715 | */ |
Vipin Kumar | aea686b | 2012-03-14 11:47:10 +0530 | [diff] [blame] | 716 | if (chip->options & NAND_BUSWIDTH_16) |
| 717 | len = roundup(len, 2); |
| 718 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 719 | nand_read_oob_op(chip, page, off, oob + j, len); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 720 | j += len; |
| 721 | } |
| 722 | |
Vipin Kumar | 519300c | 2012-03-07 17:00:49 +0530 | [diff] [blame] | 723 | memcpy(&ecc_code[i], oob, chip->ecc.bytes); |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 724 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 725 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 726 | stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]); |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 727 | if (stat < 0) { |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 728 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 729 | } else { |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 730 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 731 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 732 | } |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 733 | } |
| 734 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 735 | return max_bitflips; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /* |
Armando Visconti | 753e013 | 2012-03-07 17:00:54 +0530 | [diff] [blame] | 739 | * fsmc_bch8_correct_data |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 740 | * @mtd: mtd info structure |
| 741 | * @dat: buffer of read data |
| 742 | * @read_ecc: ecc read from device spare area |
| 743 | * @calc_ecc: ecc calculated from read data |
| 744 | * |
| 745 | * calc_ecc is a 104 bit information containing maximum of 8 error |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 746 | * offset information of 13 bits each in 512 bytes of read data. |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 747 | */ |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 748 | static int fsmc_bch8_correct_data(struct nand_chip *chip, u8 *dat, |
| 749 | u8 *read_ecc, u8 *calc_ecc) |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 750 | { |
Boris Brezillon | bfc535f | 2018-11-20 10:02:30 +0100 | [diff] [blame] | 751 | struct fsmc_nand_data *host = nand_to_fsmc(chip); |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 752 | u32 err_idx[8]; |
| 753 | u32 num_err, i; |
| 754 | u32 ecc1, ecc2, ecc3, ecc4; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 755 | |
Miquel Raynal | 4df6ed4 | 2018-02-16 15:22:47 +0100 | [diff] [blame] | 756 | num_err = (readl_relaxed(host->regs_va + STS) >> 10) & 0xF; |
Vipin Kumar | 519300c | 2012-03-07 17:00:49 +0530 | [diff] [blame] | 757 | |
| 758 | /* no bit flipping */ |
| 759 | if (likely(num_err == 0)) |
| 760 | return 0; |
| 761 | |
| 762 | /* too many errors */ |
| 763 | if (unlikely(num_err > 8)) { |
| 764 | /* |
| 765 | * This is a temporary erase check. A newly erased page read |
| 766 | * would result in an ecc error because the oob data is also |
| 767 | * erased to FF and the calculated ecc for an FF data is not |
| 768 | * FF..FF. |
| 769 | * This is a workaround to skip performing correction in case |
| 770 | * data is FF..FF |
| 771 | * |
| 772 | * Logic: |
| 773 | * For every page, each bit written as 0 is counted until these |
| 774 | * number of bits are greater than 8 (the maximum correction |
| 775 | * capability of FSMC for each 512 + 13 bytes) |
| 776 | */ |
| 777 | |
| 778 | int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8); |
| 779 | int bits_data = count_written_bits(dat, chip->ecc.size, 8); |
| 780 | |
| 781 | if ((bits_ecc + bits_data) <= 8) { |
| 782 | if (bits_data) |
| 783 | memset(dat, 0xff, chip->ecc.size); |
| 784 | return bits_data; |
| 785 | } |
| 786 | |
| 787 | return -EBADMSG; |
| 788 | } |
| 789 | |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 790 | /* |
| 791 | * ------------------- calc_ecc[] bit wise -----------|--13 bits--| |
| 792 | * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--| |
| 793 | * |
| 794 | * calc_ecc is a 104 bit information containing maximum of 8 error |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 795 | * offset information of 13 bits each. calc_ecc is copied into a |
| 796 | * u64 array and error offset indexes are populated in err_idx |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 797 | * array |
| 798 | */ |
Miquel Raynal | 4df6ed4 | 2018-02-16 15:22:47 +0100 | [diff] [blame] | 799 | ecc1 = readl_relaxed(host->regs_va + ECC1); |
| 800 | ecc2 = readl_relaxed(host->regs_va + ECC2); |
| 801 | ecc3 = readl_relaxed(host->regs_va + ECC3); |
| 802 | ecc4 = readl_relaxed(host->regs_va + STS); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 803 | |
Armando Visconti | 753e013 | 2012-03-07 17:00:54 +0530 | [diff] [blame] | 804 | err_idx[0] = (ecc1 >> 0) & 0x1FFF; |
| 805 | err_idx[1] = (ecc1 >> 13) & 0x1FFF; |
| 806 | err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F); |
| 807 | err_idx[3] = (ecc2 >> 7) & 0x1FFF; |
| 808 | err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF); |
| 809 | err_idx[5] = (ecc3 >> 1) & 0x1FFF; |
| 810 | err_idx[6] = (ecc3 >> 14) & 0x1FFF; |
| 811 | err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 812 | |
| 813 | i = 0; |
| 814 | while (num_err--) { |
Fenghua Yu | 7c26e6e | 2019-12-20 16:05:55 -0800 | [diff] [blame] | 815 | err_idx[i] ^= 3; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 816 | |
Vipin Kumar | b533f8d | 2012-03-14 11:47:11 +0530 | [diff] [blame] | 817 | if (err_idx[i] < chip->ecc.size * 8) { |
Fenghua Yu | 7c26e6e | 2019-12-20 16:05:55 -0800 | [diff] [blame] | 818 | int err = err_idx[i]; |
| 819 | |
| 820 | dat[err >> 3] ^= BIT(err & 7); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 821 | i++; |
| 822 | } |
| 823 | } |
| 824 | return i; |
| 825 | } |
| 826 | |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 827 | static bool filter(struct dma_chan *chan, void *slave) |
| 828 | { |
| 829 | chan->private = slave; |
| 830 | return true; |
| 831 | } |
| 832 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 833 | static int fsmc_nand_probe_config_dt(struct platform_device *pdev, |
Thomas Petazzoni | a1b1e1d | 2017-03-21 11:04:02 +0100 | [diff] [blame] | 834 | struct fsmc_nand_data *host, |
| 835 | struct nand_chip *nand) |
Stefan Roese | eea6281 | 2012-03-16 10:19:31 +0100 | [diff] [blame] | 836 | { |
Thomas Petazzoni | a1b1e1d | 2017-03-21 11:04:02 +0100 | [diff] [blame] | 837 | struct device_node *np = pdev->dev.of_node; |
Stefan Roese | eea6281 | 2012-03-16 10:19:31 +0100 | [diff] [blame] | 838 | u32 val; |
Stefan Roese | 62b57f4 | 2015-03-19 14:34:29 +0100 | [diff] [blame] | 839 | int ret; |
Stefan Roese | eea6281 | 2012-03-16 10:19:31 +0100 | [diff] [blame] | 840 | |
Thomas Petazzoni | a1b1e1d | 2017-03-21 11:04:02 +0100 | [diff] [blame] | 841 | nand->options = 0; |
Thomas Petazzoni | ee56874 | 2017-03-21 11:03:53 +0100 | [diff] [blame] | 842 | |
Stefan Roese | eea6281 | 2012-03-16 10:19:31 +0100 | [diff] [blame] | 843 | if (!of_property_read_u32(np, "bank-width", &val)) { |
| 844 | if (val == 2) { |
Thomas Petazzoni | a1b1e1d | 2017-03-21 11:04:02 +0100 | [diff] [blame] | 845 | nand->options |= NAND_BUSWIDTH_16; |
Stefan Roese | eea6281 | 2012-03-16 10:19:31 +0100 | [diff] [blame] | 846 | } else if (val != 1) { |
| 847 | dev_err(&pdev->dev, "invalid bank-width %u\n", val); |
| 848 | return -EINVAL; |
| 849 | } |
| 850 | } |
Thomas Petazzoni | ee56874 | 2017-03-21 11:03:53 +0100 | [diff] [blame] | 851 | |
Stefan Roese | eea6281 | 2012-03-16 10:19:31 +0100 | [diff] [blame] | 852 | if (of_get_property(np, "nand-skip-bbtscan", NULL)) |
Thomas Petazzoni | a1b1e1d | 2017-03-21 11:04:02 +0100 | [diff] [blame] | 853 | nand->options |= NAND_SKIP_BBTSCAN; |
Stefan Roese | eea6281 | 2012-03-16 10:19:31 +0100 | [diff] [blame] | 854 | |
Thomas Petazzoni | a1b1e1d | 2017-03-21 11:04:02 +0100 | [diff] [blame] | 855 | host->dev_timings = devm_kzalloc(&pdev->dev, |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 856 | sizeof(*host->dev_timings), |
| 857 | GFP_KERNEL); |
Thomas Petazzoni | a1b1e1d | 2017-03-21 11:04:02 +0100 | [diff] [blame] | 858 | if (!host->dev_timings) |
Mian Yousaf Kaukab | 64ddba4 | 2013-04-29 14:07:48 +0200 | [diff] [blame] | 859 | return -ENOMEM; |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 860 | |
Thomas Petazzoni | a1b1e1d | 2017-03-21 11:04:02 +0100 | [diff] [blame] | 861 | ret = of_property_read_u8_array(np, "timings", (u8 *)host->dev_timings, |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 862 | sizeof(*host->dev_timings)); |
Thomas Petazzoni | d9fb079 | 2017-04-29 10:52:35 +0200 | [diff] [blame] | 863 | if (ret) |
Thomas Petazzoni | a1b1e1d | 2017-03-21 11:04:02 +0100 | [diff] [blame] | 864 | host->dev_timings = NULL; |
Mian Yousaf Kaukab | 64ddba4 | 2013-04-29 14:07:48 +0200 | [diff] [blame] | 865 | |
| 866 | /* Set default NAND bank to 0 */ |
Thomas Petazzoni | a1b1e1d | 2017-03-21 11:04:02 +0100 | [diff] [blame] | 867 | host->bank = 0; |
Mian Yousaf Kaukab | 64ddba4 | 2013-04-29 14:07:48 +0200 | [diff] [blame] | 868 | if (!of_property_read_u32(np, "bank", &val)) { |
| 869 | if (val > 3) { |
| 870 | dev_err(&pdev->dev, "invalid bank %u\n", val); |
| 871 | return -EINVAL; |
| 872 | } |
Thomas Petazzoni | a1b1e1d | 2017-03-21 11:04:02 +0100 | [diff] [blame] | 873 | host->bank = val; |
Mian Yousaf Kaukab | 64ddba4 | 2013-04-29 14:07:48 +0200 | [diff] [blame] | 874 | } |
Stefan Roese | eea6281 | 2012-03-16 10:19:31 +0100 | [diff] [blame] | 875 | return 0; |
| 876 | } |
Stefan Roese | eea6281 | 2012-03-16 10:19:31 +0100 | [diff] [blame] | 877 | |
Miquel Raynal | 3bbddfa | 2018-07-20 17:14:59 +0200 | [diff] [blame] | 878 | static int fsmc_nand_attach_chip(struct nand_chip *nand) |
| 879 | { |
| 880 | struct mtd_info *mtd = nand_to_mtd(nand); |
Boris Brezillon | bfc535f | 2018-11-20 10:02:30 +0100 | [diff] [blame] | 881 | struct fsmc_nand_data *host = nand_to_fsmc(nand); |
Miquel Raynal | 3bbddfa | 2018-07-20 17:14:59 +0200 | [diff] [blame] | 882 | |
Miquel Raynal | 98591a6 | 2020-11-13 13:34:13 +0100 | [diff] [blame^] | 883 | if (nand->ecc.engine_type == NAND_ECC_ENGINE_TYPE_INVALID) |
| 884 | nand->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST; |
| 885 | |
| 886 | if (!nand->ecc.size) |
| 887 | nand->ecc.size = 512; |
| 888 | |
| 889 | if (AMBA_REV_BITS(host->pid) >= 8) { |
| 890 | nand->ecc.read_page = fsmc_read_page_hwecc; |
| 891 | nand->ecc.calculate = fsmc_read_hwecc_ecc4; |
| 892 | nand->ecc.correct = fsmc_bch8_correct_data; |
| 893 | nand->ecc.bytes = 13; |
| 894 | nand->ecc.strength = 8; |
| 895 | } |
| 896 | |
Miquel Raynal | 3bbddfa | 2018-07-20 17:14:59 +0200 | [diff] [blame] | 897 | if (AMBA_REV_BITS(host->pid) >= 8) { |
| 898 | switch (mtd->oobsize) { |
| 899 | case 16: |
| 900 | case 64: |
| 901 | case 128: |
| 902 | case 224: |
| 903 | case 256: |
| 904 | break; |
| 905 | default: |
| 906 | dev_warn(host->dev, |
| 907 | "No oob scheme defined for oobsize %d\n", |
| 908 | mtd->oobsize); |
| 909 | return -EINVAL; |
| 910 | } |
| 911 | |
| 912 | mtd_set_ooblayout(mtd, &fsmc_ecc4_ooblayout_ops); |
| 913 | |
| 914 | return 0; |
| 915 | } |
| 916 | |
Miquel Raynal | bace41f | 2020-08-27 10:51:58 +0200 | [diff] [blame] | 917 | switch (nand->ecc.engine_type) { |
| 918 | case NAND_ECC_ENGINE_TYPE_ON_HOST: |
Miquel Raynal | 3bbddfa | 2018-07-20 17:14:59 +0200 | [diff] [blame] | 919 | dev_info(host->dev, "Using 1-bit HW ECC scheme\n"); |
| 920 | nand->ecc.calculate = fsmc_read_hwecc_ecc1; |
| 921 | nand->ecc.correct = nand_correct_data; |
Miquel Raynal | 98591a6 | 2020-11-13 13:34:13 +0100 | [diff] [blame^] | 922 | nand->ecc.hwctl = fsmc_enable_hwecc; |
Miquel Raynal | 3bbddfa | 2018-07-20 17:14:59 +0200 | [diff] [blame] | 923 | nand->ecc.bytes = 3; |
| 924 | nand->ecc.strength = 1; |
Boris Brezillon | 309600c | 2018-09-04 16:23:28 +0200 | [diff] [blame] | 925 | nand->ecc.options |= NAND_ECC_SOFT_HAMMING_SM_ORDER; |
Miquel Raynal | 3bbddfa | 2018-07-20 17:14:59 +0200 | [diff] [blame] | 926 | break; |
| 927 | |
Miquel Raynal | bace41f | 2020-08-27 10:51:58 +0200 | [diff] [blame] | 928 | case NAND_ECC_ENGINE_TYPE_SOFT: |
Miquel Raynal | e0a564a | 2020-08-27 10:51:50 +0200 | [diff] [blame] | 929 | if (nand->ecc.algo == NAND_ECC_ALGO_BCH) { |
Miquel Raynal | 3bbddfa | 2018-07-20 17:14:59 +0200 | [diff] [blame] | 930 | dev_info(host->dev, |
| 931 | "Using 4-bit SW BCH ECC scheme\n"); |
| 932 | break; |
| 933 | } |
| 934 | |
Miquel Raynal | bace41f | 2020-08-27 10:51:58 +0200 | [diff] [blame] | 935 | case NAND_ECC_ENGINE_TYPE_ON_DIE: |
Miquel Raynal | 3bbddfa | 2018-07-20 17:14:59 +0200 | [diff] [blame] | 936 | break; |
| 937 | |
| 938 | default: |
| 939 | dev_err(host->dev, "Unsupported ECC mode!\n"); |
| 940 | return -ENOTSUPP; |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * Don't set layout for BCH4 SW ECC. This will be |
| 945 | * generated later in nand_bch_init() later. |
| 946 | */ |
Miquel Raynal | bace41f | 2020-08-27 10:51:58 +0200 | [diff] [blame] | 947 | if (nand->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) { |
Miquel Raynal | 3bbddfa | 2018-07-20 17:14:59 +0200 | [diff] [blame] | 948 | switch (mtd->oobsize) { |
| 949 | case 16: |
| 950 | case 64: |
| 951 | case 128: |
| 952 | mtd_set_ooblayout(mtd, |
| 953 | &fsmc_ecc1_ooblayout_ops); |
| 954 | break; |
| 955 | default: |
| 956 | dev_warn(host->dev, |
| 957 | "No oob scheme defined for oobsize %d\n", |
| 958 | mtd->oobsize); |
| 959 | return -EINVAL; |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | static const struct nand_controller_ops fsmc_nand_controller_ops = { |
| 967 | .attach_chip = fsmc_nand_attach_chip, |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 968 | .exec_op = fsmc_exec_op, |
Miquel Raynal | 4c46667 | 2020-05-29 13:13:13 +0200 | [diff] [blame] | 969 | .setup_interface = fsmc_setup_interface, |
Miquel Raynal | 3bbddfa | 2018-07-20 17:14:59 +0200 | [diff] [blame] | 970 | }; |
| 971 | |
Linus Walleij | ab3ab7b | 2019-01-26 14:10:56 +0100 | [diff] [blame] | 972 | /** |
| 973 | * fsmc_nand_disable() - Disables the NAND bank |
| 974 | * @host: The instance to disable |
| 975 | */ |
| 976 | static void fsmc_nand_disable(struct fsmc_nand_data *host) |
| 977 | { |
| 978 | u32 val; |
| 979 | |
| 980 | val = readl(host->regs_va + FSMC_PC); |
| 981 | val &= ~FSMC_ENABLE; |
| 982 | writel(val, host->regs_va + FSMC_PC); |
| 983 | } |
| 984 | |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 985 | /* |
| 986 | * fsmc_nand_probe - Probe function |
| 987 | * @pdev: platform device structure |
| 988 | */ |
| 989 | static int __init fsmc_nand_probe(struct platform_device *pdev) |
| 990 | { |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 991 | struct fsmc_nand_data *host; |
| 992 | struct mtd_info *mtd; |
| 993 | struct nand_chip *nand; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 994 | struct resource *res; |
Miquel Raynal | 4df6ed4 | 2018-02-16 15:22:47 +0100 | [diff] [blame] | 995 | void __iomem *base; |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 996 | dma_cap_mask_t mask; |
Linus Walleij | 4ad916b | 2010-11-29 13:52:06 +0100 | [diff] [blame] | 997 | int ret = 0; |
Linus Walleij | 593cd87 | 2010-11-29 13:52:19 +0100 | [diff] [blame] | 998 | u32 pid; |
| 999 | int i; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1000 | |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1001 | /* Allocate memory for the device structure (and zero it) */ |
Vipin Kumar | 82b9dbe | 2012-03-14 11:47:15 +0530 | [diff] [blame] | 1002 | host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL); |
Jingoo Han | d9a21ae | 2013-12-26 12:16:38 +0900 | [diff] [blame] | 1003 | if (!host) |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1004 | return -ENOMEM; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1005 | |
Thomas Petazzoni | a1b1e1d | 2017-03-21 11:04:02 +0100 | [diff] [blame] | 1006 | nand = &host->nand; |
| 1007 | |
| 1008 | ret = fsmc_nand_probe_config_dt(pdev, host, nand); |
| 1009 | if (ret) |
| 1010 | return ret; |
| 1011 | |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1012 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data"); |
Thierry Reding | b0de774 | 2013-01-21 11:09:12 +0100 | [diff] [blame] | 1013 | host->data_va = devm_ioremap_resource(&pdev->dev, res); |
| 1014 | if (IS_ERR(host->data_va)) |
| 1015 | return PTR_ERR(host->data_va); |
Stefan Roese | cbf29b8 | 2015-10-02 12:40:20 +0200 | [diff] [blame] | 1016 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d7b42a | 2012-10-04 15:14:16 +0200 | [diff] [blame] | 1017 | host->data_pa = (dma_addr_t)res->start; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1018 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d7b42a | 2012-10-04 15:14:16 +0200 | [diff] [blame] | 1019 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr"); |
Thierry Reding | b0de774 | 2013-01-21 11:09:12 +0100 | [diff] [blame] | 1020 | host->addr_va = devm_ioremap_resource(&pdev->dev, res); |
| 1021 | if (IS_ERR(host->addr_va)) |
| 1022 | return PTR_ERR(host->addr_va); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1023 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d7b42a | 2012-10-04 15:14:16 +0200 | [diff] [blame] | 1024 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd"); |
Thierry Reding | b0de774 | 2013-01-21 11:09:12 +0100 | [diff] [blame] | 1025 | host->cmd_va = devm_ioremap_resource(&pdev->dev, res); |
| 1026 | if (IS_ERR(host->cmd_va)) |
| 1027 | return PTR_ERR(host->cmd_va); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1028 | |
| 1029 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs"); |
Miquel Raynal | 4df6ed4 | 2018-02-16 15:22:47 +0100 | [diff] [blame] | 1030 | base = devm_ioremap_resource(&pdev->dev, res); |
| 1031 | if (IS_ERR(base)) |
| 1032 | return PTR_ERR(base); |
| 1033 | |
| 1034 | host->regs_va = base + FSMC_NOR_REG_SIZE + |
| 1035 | (host->bank * FSMC_NAND_BANK_SZ); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1036 | |
Thomas Petazzoni | fb8ed2c | 2017-03-21 11:04:03 +0100 | [diff] [blame] | 1037 | host->clk = devm_clk_get(&pdev->dev, NULL); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1038 | if (IS_ERR(host->clk)) { |
| 1039 | dev_err(&pdev->dev, "failed to fetch block clock\n"); |
Vipin Kumar | 82b9dbe | 2012-03-14 11:47:15 +0530 | [diff] [blame] | 1040 | return PTR_ERR(host->clk); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1041 | } |
| 1042 | |
Viresh Kumar | e25da1c | 2012-04-17 17:07:57 +0530 | [diff] [blame] | 1043 | ret = clk_prepare_enable(host->clk); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1044 | if (ret) |
Thomas Petazzoni | fb8ed2c | 2017-03-21 11:04:03 +0100 | [diff] [blame] | 1045 | return ret; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1046 | |
Linus Walleij | 593cd87 | 2010-11-29 13:52:19 +0100 | [diff] [blame] | 1047 | /* |
| 1048 | * This device ID is actually a common AMBA ID as used on the |
| 1049 | * AMBA PrimeCell bus. However it is not a PrimeCell. |
| 1050 | */ |
| 1051 | for (pid = 0, i = 0; i < 4; i++) |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 1052 | pid |= (readl(base + resource_size(res) - 0x20 + 4 * i) & |
| 1053 | 255) << (i * 8); |
| 1054 | |
Linus Walleij | 593cd87 | 2010-11-29 13:52:19 +0100 | [diff] [blame] | 1055 | host->pid = pid; |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 1056 | |
| 1057 | dev_info(&pdev->dev, |
| 1058 | "FSMC device partno %03x, manufacturer %02x, revision %02x, config %02x\n", |
Linus Walleij | 593cd87 | 2010-11-29 13:52:19 +0100 | [diff] [blame] | 1059 | AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid), |
| 1060 | AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid)); |
| 1061 | |
Vipin Kumar | 712c4ad | 2012-03-14 11:47:16 +0530 | [diff] [blame] | 1062 | host->dev = &pdev->dev; |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 1063 | |
| 1064 | if (host->mode == USE_DMA_ACCESS) |
| 1065 | init_completion(&host->dma_access_complete); |
| 1066 | |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1067 | /* Link all private pointers */ |
Boris BREZILLON | bdf3a55 | 2015-12-10 09:00:05 +0100 | [diff] [blame] | 1068 | mtd = nand_to_mtd(&host->nand); |
Thomas Petazzoni | a1b1e1d | 2017-03-21 11:04:02 +0100 | [diff] [blame] | 1069 | nand_set_flash_node(nand, pdev->dev.of_node); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1070 | |
Boris BREZILLON | bdf3a55 | 2015-12-10 09:00:05 +0100 | [diff] [blame] | 1071 | mtd->dev.parent = &pdev->dev; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1072 | |
Vipin Kumar | 467e6e7 | 2012-03-14 11:47:12 +0530 | [diff] [blame] | 1073 | nand->badblockbits = 7; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1074 | |
Miquel Raynal | 4da712e | 2018-02-16 15:22:48 +0100 | [diff] [blame] | 1075 | if (host->mode == USE_DMA_ACCESS) { |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 1076 | dma_cap_zero(mask); |
| 1077 | dma_cap_set(DMA_MEMCPY, mask); |
Thomas Petazzoni | feb1e57 | 2017-03-21 11:03:59 +0100 | [diff] [blame] | 1078 | host->read_dma_chan = dma_request_channel(mask, filter, NULL); |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 1079 | if (!host->read_dma_chan) { |
| 1080 | dev_err(&pdev->dev, "Unable to get read dma channel\n"); |
Miquel Raynal | 43fab01 | 2018-04-21 20:00:36 +0200 | [diff] [blame] | 1081 | goto disable_clk; |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 1082 | } |
Thomas Petazzoni | feb1e57 | 2017-03-21 11:03:59 +0100 | [diff] [blame] | 1083 | host->write_dma_chan = dma_request_channel(mask, filter, NULL); |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 1084 | if (!host->write_dma_chan) { |
| 1085 | dev_err(&pdev->dev, "Unable to get write dma channel\n"); |
Miquel Raynal | 43fab01 | 2018-04-21 20:00:36 +0200 | [diff] [blame] | 1086 | goto release_dma_read_chan; |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 1087 | } |
Vipin Kumar | 604e754 | 2012-03-14 11:47:17 +0530 | [diff] [blame] | 1088 | } |
| 1089 | |
Boris Brezillon | 7a08dba | 2018-11-11 08:55:24 +0100 | [diff] [blame] | 1090 | if (host->dev_timings) { |
Thomas Petazzoni | d9fb079 | 2017-04-29 10:52:35 +0200 | [diff] [blame] | 1091 | fsmc_nand_setup(host, host->dev_timings); |
Boris Brezillon | 7a08dba | 2018-11-11 08:55:24 +0100 | [diff] [blame] | 1092 | nand->options |= NAND_KEEP_TIMINGS; |
| 1093 | } |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1094 | |
Boris Brezillon | ad71148 | 2018-11-20 10:02:33 +0100 | [diff] [blame] | 1095 | nand_controller_init(&host->base); |
| 1096 | host->base.ops = &fsmc_nand_controller_ops; |
| 1097 | nand->controller = &host->base; |
| 1098 | |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1099 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1100 | * Scan to find existence of the device |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1101 | */ |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 1102 | ret = nand_scan(nand, 1); |
Masahiro Yamada | ad5678e | 2016-11-04 19:43:00 +0900 | [diff] [blame] | 1103 | if (ret) |
Miquel Raynal | 43fab01 | 2018-04-21 20:00:36 +0200 | [diff] [blame] | 1104 | goto release_dma_write_chan; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1105 | |
Boris BREZILLON | bdf3a55 | 2015-12-10 09:00:05 +0100 | [diff] [blame] | 1106 | mtd->name = "nand"; |
Thomas Petazzoni | ede29a0 | 2017-03-21 11:04:00 +0100 | [diff] [blame] | 1107 | ret = mtd_device_register(mtd, NULL, 0); |
Jamie Iles | 99335d0 | 2011-05-23 10:23:23 +0100 | [diff] [blame] | 1108 | if (ret) |
Miquel Raynal | 682cae2 | 2018-04-21 20:00:37 +0200 | [diff] [blame] | 1109 | goto cleanup_nand; |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1110 | |
| 1111 | platform_set_drvdata(pdev, host); |
| 1112 | dev_info(&pdev->dev, "FSMC NAND driver registration successful\n"); |
Miquel Raynal | 43fab01 | 2018-04-21 20:00:36 +0200 | [diff] [blame] | 1113 | |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1114 | return 0; |
| 1115 | |
Miquel Raynal | 682cae2 | 2018-04-21 20:00:37 +0200 | [diff] [blame] | 1116 | cleanup_nand: |
| 1117 | nand_cleanup(nand); |
Miquel Raynal | 43fab01 | 2018-04-21 20:00:36 +0200 | [diff] [blame] | 1118 | release_dma_write_chan: |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 1119 | if (host->mode == USE_DMA_ACCESS) |
| 1120 | dma_release_channel(host->write_dma_chan); |
Miquel Raynal | 43fab01 | 2018-04-21 20:00:36 +0200 | [diff] [blame] | 1121 | release_dma_read_chan: |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 1122 | if (host->mode == USE_DMA_ACCESS) |
| 1123 | dma_release_channel(host->read_dma_chan); |
Miquel Raynal | 43fab01 | 2018-04-21 20:00:36 +0200 | [diff] [blame] | 1124 | disable_clk: |
Linus Walleij | ab3ab7b | 2019-01-26 14:10:56 +0100 | [diff] [blame] | 1125 | fsmc_nand_disable(host); |
Viresh Kumar | e25da1c | 2012-04-17 17:07:57 +0530 | [diff] [blame] | 1126 | clk_disable_unprepare(host->clk); |
Miquel Raynal | 43fab01 | 2018-04-21 20:00:36 +0200 | [diff] [blame] | 1127 | |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1128 | return ret; |
| 1129 | } |
| 1130 | |
| 1131 | /* |
| 1132 | * Clean up routine |
| 1133 | */ |
| 1134 | static int fsmc_nand_remove(struct platform_device *pdev) |
| 1135 | { |
| 1136 | struct fsmc_nand_data *host = platform_get_drvdata(pdev); |
| 1137 | |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1138 | if (host) { |
Miquel Raynal | 9cc02f4 | 2020-05-19 14:59:50 +0200 | [diff] [blame] | 1139 | struct nand_chip *chip = &host->nand; |
| 1140 | int ret; |
| 1141 | |
| 1142 | ret = mtd_device_unregister(nand_to_mtd(chip)); |
| 1143 | WARN_ON(ret); |
| 1144 | nand_cleanup(chip); |
Linus Walleij | ab3ab7b | 2019-01-26 14:10:56 +0100 | [diff] [blame] | 1145 | fsmc_nand_disable(host); |
Vipin Kumar | 4774fb0 | 2012-03-14 11:47:18 +0530 | [diff] [blame] | 1146 | |
| 1147 | if (host->mode == USE_DMA_ACCESS) { |
| 1148 | dma_release_channel(host->write_dma_chan); |
| 1149 | dma_release_channel(host->read_dma_chan); |
| 1150 | } |
Viresh Kumar | e25da1c | 2012-04-17 17:07:57 +0530 | [diff] [blame] | 1151 | clk_disable_unprepare(host->clk); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1152 | } |
Vipin Kumar | 82b9dbe | 2012-03-14 11:47:15 +0530 | [diff] [blame] | 1153 | |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1154 | return 0; |
| 1155 | } |
| 1156 | |
Jingoo Han | 80ce4dd | 2013-03-26 15:53:48 +0900 | [diff] [blame] | 1157 | #ifdef CONFIG_PM_SLEEP |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1158 | static int fsmc_nand_suspend(struct device *dev) |
| 1159 | { |
| 1160 | struct fsmc_nand_data *host = dev_get_drvdata(dev); |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 1161 | |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1162 | if (host) |
Viresh Kumar | e25da1c | 2012-04-17 17:07:57 +0530 | [diff] [blame] | 1163 | clk_disable_unprepare(host->clk); |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 1164 | |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1165 | return 0; |
| 1166 | } |
| 1167 | |
| 1168 | static int fsmc_nand_resume(struct device *dev) |
| 1169 | { |
| 1170 | struct fsmc_nand_data *host = dev_get_drvdata(dev); |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 1171 | |
Shiraz Hashim | f63acb7 | 2012-03-14 11:47:13 +0530 | [diff] [blame] | 1172 | if (host) { |
Viresh Kumar | e25da1c | 2012-04-17 17:07:57 +0530 | [diff] [blame] | 1173 | clk_prepare_enable(host->clk); |
Thomas Petazzoni | d9fb079 | 2017-04-29 10:52:35 +0200 | [diff] [blame] | 1174 | if (host->dev_timings) |
| 1175 | fsmc_nand_setup(host, host->dev_timings); |
Linus Walleij | 30c72ab | 2019-01-26 14:10:55 +0100 | [diff] [blame] | 1176 | nand_reset(&host->nand, 0); |
Shiraz Hashim | f63acb7 | 2012-03-14 11:47:13 +0530 | [diff] [blame] | 1177 | } |
Boris Brezillon | fc43f45 | 2018-11-20 10:02:35 +0100 | [diff] [blame] | 1178 | |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1179 | return 0; |
| 1180 | } |
Jingoo Han | 80ce4dd | 2013-03-26 15:53:48 +0900 | [diff] [blame] | 1181 | #endif |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1182 | |
Shiraz Hashim | f63acb7 | 2012-03-14 11:47:13 +0530 | [diff] [blame] | 1183 | static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1184 | |
Stefan Roese | eea6281 | 2012-03-16 10:19:31 +0100 | [diff] [blame] | 1185 | static const struct of_device_id fsmc_nand_id_table[] = { |
| 1186 | { .compatible = "st,spear600-fsmc-nand" }, |
Linus Walleij | ba78520 | 2013-01-05 22:28:32 +0100 | [diff] [blame] | 1187 | { .compatible = "stericsson,fsmc-nand" }, |
Stefan Roese | eea6281 | 2012-03-16 10:19:31 +0100 | [diff] [blame] | 1188 | {} |
| 1189 | }; |
| 1190 | MODULE_DEVICE_TABLE(of, fsmc_nand_id_table); |
Stefan Roese | eea6281 | 2012-03-16 10:19:31 +0100 | [diff] [blame] | 1191 | |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1192 | static struct platform_driver fsmc_nand_driver = { |
| 1193 | .remove = fsmc_nand_remove, |
| 1194 | .driver = { |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1195 | .name = "fsmc-nand", |
Thomas Petazzoni | 33575b2 | 2017-03-21 11:04:05 +0100 | [diff] [blame] | 1196 | .of_match_table = fsmc_nand_id_table, |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1197 | .pm = &fsmc_nand_pm_ops, |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1198 | }, |
| 1199 | }; |
| 1200 | |
Jingoo Han | 307d2a51 | 2013-03-05 13:30:36 +0900 | [diff] [blame] | 1201 | module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1202 | |
Boris Brezillon | bb69634 | 2018-11-20 10:02:34 +0100 | [diff] [blame] | 1203 | MODULE_LICENSE("GPL v2"); |
Linus Walleij | 6c009ab | 2010-09-13 00:35:22 +0200 | [diff] [blame] | 1204 | MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi"); |
| 1205 | MODULE_DESCRIPTION("NAND driver for SPEAr Platforms"); |