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