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