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