blob: 275a98ca4f6a8e3c120f2645ee628f4460e6b183 [file] [log] [blame]
Linus Walleij6c009ab2010-09-13 00:35:22 +02001/*
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 Kumar4774fb02012-03-14 11:47:18 +053020#include <linux/completion.h>
21#include <linux/dmaengine.h>
22#include <linux/dma-direction.h>
23#include <linux/dma-mapping.h>
Linus Walleij6c009ab2010-09-13 00:35:22 +020024#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 Roeseeea62812012-03-16 10:19:31 +010034#include <linux/of.h>
Linus Walleij6c009ab2010-09-13 00:35:22 +020035#include <linux/mtd/partitions.h>
36#include <linux/io.h>
37#include <linux/slab.h>
38#include <linux/mtd/fsmc.h>
Linus Walleij593cd872010-11-29 13:52:19 +010039#include <linux/amba/bus.h>
Linus Walleij6c009ab2010-09-13 00:35:22 +020040#include <mtd/mtd-abi.h>
41
Bhavna Yadave29ee572012-03-07 17:00:50 +053042/*
Linus Walleij6c009ab2010-09-13 00:35:22 +020043 * 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 */
51static 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 Walleij6c009ab2010-09-13 00:35:22 +020064static struct fsmc_eccplace fsmc_ecc4_sp_place = {
65 .eccplace = {
66 {.offset = 0, .length = 4},
67 {.offset = 6, .length = 9}
68 }
69};
70
Boris Brezillon22b46952016-02-03 20:01:42 +010071static 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
85static 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
103static const struct mtd_ooblayout_ops fsmc_ecc1_ooblayout_ops = {
104 .ecc = fsmc_ecc1_ooblayout_ecc,
105 .free = fsmc_ecc1_ooblayout_free,
106};
107
108static 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
126static 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
144static const struct mtd_ooblayout_ops fsmc_ecc4_ooblayout_ops = {
145 .ecc = fsmc_ecc4_ooblayout_ecc,
146 .free = fsmc_ecc4_ooblayout_free,
147};
148
Linus Walleij6c009ab2010-09-13 00:35:22 +0200149/**
Linus Walleij593cd872010-11-29 13:52:19 +0100150 * struct fsmc_nand_data - structure for FSMC NAND device state
Linus Walleij6c009ab2010-09-13 00:35:22 +0200151 *
Linus Walleij593cd872010-11-29 13:52:19 +0100152 * @pid: Part ID on the AMBA PrimeCell format
Linus Walleij6c009ab2010-09-13 00:35:22 +0200153 * @mtd: MTD info for a NAND flash.
154 * @nand: Chip related info for a NAND flash.
Vipin Kumar71470322012-03-14 11:47:07 +0530155 * @partitions: Partition info for a NAND Flash.
156 * @nr_partitions: Total number of partition of a NAND flash.
Linus Walleij6c009ab2010-09-13 00:35:22 +0200157 *
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 Kumar4774fb02012-03-14 11:47:18 +0530162 * @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 Walleij6c009ab2010-09-13 00:35:22 +0200167 * @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 */
172struct fsmc_nand_data {
Linus Walleij593cd872010-11-29 13:52:19 +0100173 u32 pid;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200174 struct nand_chip nand;
Vipin Kumar71470322012-03-14 11:47:07 +0530175 struct mtd_partition *partitions;
176 unsigned int nr_partitions;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200177
178 struct fsmc_eccplace *ecc_place;
179 unsigned int bank;
Vipin Kumar712c4ad2012-03-14 11:47:16 +0530180 struct device *dev;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530181 enum access_mode mode;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200182 struct clk *clk;
183
Vipin Kumar4774fb02012-03-14 11:47:18 +0530184 /* 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 Kumare2f6bce2012-03-14 11:47:14 +0530189 struct fsmc_nand_timings *dev_timings;
190
Vipin Kumar4774fb02012-03-14 11:47:18 +0530191 dma_addr_t data_pa;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200192 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 BREZILLON277af422015-12-10 08:59:46 +0100200static inline struct fsmc_nand_data *mtd_to_fsmc(struct mtd_info *mtd)
201{
Boris BREZILLONbdf3a552015-12-10 09:00:05 +0100202 return container_of(mtd_to_nand(mtd), struct fsmc_nand_data, nand);
Boris BREZILLON277af422015-12-10 08:59:46 +0100203}
204
Linus Walleij6c009ab2010-09-13 00:35:22 +0200205/* Assert CS signal based on chipnr */
206static void fsmc_select_chip(struct mtd_info *mtd, int chipnr)
207{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100208 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200209 struct fsmc_nand_data *host;
210
Boris BREZILLON277af422015-12-10 08:59:46 +0100211 host = mtd_to_fsmc(mtd);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200212
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 Roese6efadcf2015-10-02 12:40:21 +0200227 dev_err(host->dev, "unsupported chip-select %d\n", chipnr);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200228 }
229}
230
231/*
232 * fsmc_cmd_ctrl - For facilitaing Hardware access
233 * This routine allows hardware specific access to control-lines(ALE,CLE)
234 */
235static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
236{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100237 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLON277af422015-12-10 08:59:46 +0100238 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar605add72012-10-09 16:14:43 +0530239 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200240 unsigned int bank = host->bank;
241
242 if (ctrl & NAND_CTRL_CHANGE) {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530243 u32 pc;
244
Linus Walleij6c009ab2010-09-13 00:35:22 +0200245 if (ctrl & NAND_CLE) {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530246 this->IO_ADDR_R = host->cmd_va;
247 this->IO_ADDR_W = host->cmd_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200248 } else if (ctrl & NAND_ALE) {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530249 this->IO_ADDR_R = host->addr_va;
250 this->IO_ADDR_W = host->addr_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200251 } else {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530252 this->IO_ADDR_R = host->data_va;
253 this->IO_ADDR_W = host->data_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200254 }
255
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530256 pc = readl(FSMC_NAND_REG(regs, bank, PC));
257 if (ctrl & NAND_NCE)
258 pc |= FSMC_ENABLE;
259 else
260 pc &= ~FSMC_ENABLE;
Vipin Kumara4742d52012-10-09 16:14:50 +0530261 writel_relaxed(pc, FSMC_NAND_REG(regs, bank, PC));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200262 }
263
264 mb();
265
266 if (cmd != NAND_CMD_NONE)
Vipin Kumara4742d52012-10-09 16:14:50 +0530267 writeb_relaxed(cmd, this->IO_ADDR_W);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200268}
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 Kumar2a5dbead2012-03-14 11:47:19 +0530276static void fsmc_nand_setup(void __iomem *regs, uint32_t bank,
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530277 uint32_t busw, struct fsmc_nand_timings *timings)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200278{
279 uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530280 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 Walleij6c009ab2010-09-13 00:35:22 +0200302
303 if (busw)
Vipin Kumara4742d52012-10-09 16:14:50 +0530304 writel_relaxed(value | FSMC_DEVWID_16,
305 FSMC_NAND_REG(regs, bank, PC));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200306 else
Vipin Kumara4742d52012-10-09 16:14:50 +0530307 writel_relaxed(value | FSMC_DEVWID_8,
308 FSMC_NAND_REG(regs, bank, PC));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200309
Vipin Kumara4742d52012-10-09 16:14:50 +0530310 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) | tclr | tar,
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530311 FSMC_NAND_REG(regs, bank, PC));
Vipin Kumara4742d52012-10-09 16:14:50 +0530312 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 Walleij6c009ab2010-09-13 00:35:22 +0200316}
317
318/*
319 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
320 */
321static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
322{
Boris BREZILLON277af422015-12-10 08:59:46 +0100323 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530324 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200325 uint32_t bank = host->bank;
326
Vipin Kumara4742d52012-10-09 16:14:50 +0530327 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCPLEN_256,
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530328 FSMC_NAND_REG(regs, bank, PC));
Vipin Kumara4742d52012-10-09 16:14:50 +0530329 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCEN,
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530330 FSMC_NAND_REG(regs, bank, PC));
Vipin Kumara4742d52012-10-09 16:14:50 +0530331 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) | FSMC_ECCEN,
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530332 FSMC_NAND_REG(regs, bank, PC));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200333}
334
335/*
336 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300337 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200338 * max of 8-bits)
339 */
340static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
341 uint8_t *ecc)
342{
Boris BREZILLON277af422015-12-10 08:59:46 +0100343 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530344 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200345 uint32_t bank = host->bank;
346 uint32_t ecc_tmp;
347 unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
348
349 do {
Vipin Kumara4742d52012-10-09 16:14:50 +0530350 if (readl_relaxed(FSMC_NAND_REG(regs, bank, STS)) & FSMC_CODE_RDY)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200351 break;
352 else
353 cond_resched();
354 } while (!time_after_eq(jiffies, deadline));
355
Vipin Kumar712c4ad2012-03-14 11:47:16 +0530356 if (time_after_eq(jiffies, deadline)) {
357 dev_err(host->dev, "calculate ecc timed out\n");
358 return -ETIMEDOUT;
359 }
360
Vipin Kumara4742d52012-10-09 16:14:50 +0530361 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200362 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 Kumara4742d52012-10-09 16:14:50 +0530367 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC2));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200368 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 Kumara4742d52012-10-09 16:14:50 +0530373 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC3));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200374 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 Kumara4742d52012-10-09 16:14:50 +0530379 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, STS));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200380 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 Marchi25985ed2011-03-30 22:57:33 -0300387 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200388 * max of 1-bit)
389 */
390static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
391 uint8_t *ecc)
392{
Boris BREZILLON277af422015-12-10 08:59:46 +0100393 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530394 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200395 uint32_t bank = host->bank;
396 uint32_t ecc_tmp;
397
Vipin Kumara4742d52012-10-09 16:14:50 +0530398 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200399 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 Kumar519300c2012-03-07 17:00:49 +0530406/* Count the number of 0's in buff upto a max of max_bits */
407static 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 Kumar4774fb02012-03-14 11:47:18 +0530420static void dma_complete(void *param)
421{
422 struct fsmc_nand_data *host = param;
423
424 complete(&host->dma_access_complete);
425}
426
427static 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 Guire818a45b2015-03-13 07:54:46 -0400437 unsigned long time_left;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530438
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 Kumar4774fb02012-03-14 11:47:18 +0530452 } else {
453 dma_src = host->data_pa;
454 dma_dst = dma_addr;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530455 }
456
457 tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
458 len, flags);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530459 if (!tx) {
460 dev_err(host->dev, "device_prep_dma_memcpy error\n");
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000461 ret = -EIO;
462 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530463 }
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 Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000472 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530473 }
474
475 dma_async_issue_pending(chan);
476
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400477 time_left =
Vipin Kumar928aa2a2012-10-09 16:14:48 +0530478 wait_for_completion_timeout(&host->dma_access_complete,
Vipin Kumar4774fb02012-03-14 11:47:18 +0530479 msecs_to_jiffies(3000));
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400480 if (time_left == 0) {
Vinod Koulb177ea32014-10-11 21:10:32 +0530481 dmaengine_terminate_all(chan);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530482 dev_err(host->dev, "wait_for_completion_timeout\n");
Nicholas Mc Guire0bda3e12015-03-13 07:54:45 -0400483 ret = -ETIMEDOUT;
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000484 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530485 }
486
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000487 ret = 0;
488
489unmap_dma:
490 dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
491
492 return ret;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530493}
494
Linus Walleij6c009ab2010-09-13 00:35:22 +0200495/*
Vipin Kumar604e7542012-03-14 11:47:17 +0530496 * fsmc_write_buf - write buffer to chip
497 * @mtd: MTD device structure
498 * @buf: data buffer
499 * @len: number of bytes to write
500 */
501static void fsmc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
502{
503 int i;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100504 struct nand_chip *chip = mtd_to_nand(mtd);
Vipin Kumar604e7542012-03-14 11:47:17 +0530505
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 Kumara4742d52012-10-09 16:14:50 +0530511 writel_relaxed(p[i], chip->IO_ADDR_W);
Vipin Kumar604e7542012-03-14 11:47:17 +0530512 } else {
513 for (i = 0; i < len; i++)
Vipin Kumara4742d52012-10-09 16:14:50 +0530514 writeb_relaxed(buf[i], chip->IO_ADDR_W);
Vipin Kumar604e7542012-03-14 11:47:17 +0530515 }
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 */
524static void fsmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
525{
526 int i;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100527 struct nand_chip *chip = mtd_to_nand(mtd);
Vipin Kumar604e7542012-03-14 11:47:17 +0530528
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 Kumara4742d52012-10-09 16:14:50 +0530534 p[i] = readl_relaxed(chip->IO_ADDR_R);
Vipin Kumar604e7542012-03-14 11:47:17 +0530535 } else {
536 for (i = 0; i < len; i++)
Vipin Kumara4742d52012-10-09 16:14:50 +0530537 buf[i] = readb_relaxed(chip->IO_ADDR_R);
Vipin Kumar604e7542012-03-14 11:47:17 +0530538 }
539}
540
541/*
Vipin Kumar4774fb02012-03-14 11:47:18 +0530542 * 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 */
547static void fsmc_read_buf_dma(struct mtd_info *mtd, uint8_t *buf, int len)
548{
Boris BREZILLON277af422015-12-10 08:59:46 +0100549 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530550
Vipin Kumar4774fb02012-03-14 11:47:18 +0530551 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 */
560static void fsmc_write_buf_dma(struct mtd_info *mtd, const uint8_t *buf,
561 int len)
562{
Boris BREZILLON277af422015-12-10 08:59:46 +0100563 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530564
Vipin Kumar4774fb02012-03-14 11:47:18 +0530565 dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
566}
567
568/*
Linus Walleij6c009ab2010-09-13 00:35:22 +0200569 * fsmc_read_page_hwecc
570 * @mtd: mtd info structure
571 * @chip: nand chip info structure
572 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -0700573 * @oob_required: caller expects OOB data read to chip->oob_poi
Linus Walleij6c009ab2010-09-13 00:35:22 +0200574 * @page: page number to read
575 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300576 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
Linus Walleij6c009ab2010-09-13 00:35:22 +0200577 * performed in a strict sequence as follows:
578 * data(512 byte) -> ecc(13 byte)
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300579 * After this read, fsmc hardware generates and reports error data bits(up to a
Linus Walleij6c009ab2010-09-13 00:35:22 +0200580 * max of 8 bits)
581 */
582static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700583 uint8_t *buf, int oob_required, int page)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200584{
Boris BREZILLON277af422015-12-10 08:59:46 +0100585 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200586 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 Dunn3f91e942012-04-25 12:06:09 -0700601 unsigned int max_bitflips = 0;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200602
603 for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200604 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 Kumar4cbe1bf02012-03-14 11:47:09 +0530614 * 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 Kumaraea686b2012-03-14 11:47:10 +0530618 if (chip->options & NAND_BUSWIDTH_16)
619 len = roundup(len, 2);
620
Linus Walleij6c009ab2010-09-13 00:35:22 +0200621 chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
622 chip->read_buf(mtd, oob + j, len);
623 j += len;
624 }
625
Vipin Kumar519300c2012-03-07 17:00:49 +0530626 memcpy(&ecc_code[i], oob, chip->ecc.bytes);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200627 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
628
629 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -0700630 if (stat < 0) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200631 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -0700632 } else {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200633 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -0700634 max_bitflips = max_t(unsigned int, max_bitflips, stat);
635 }
Linus Walleij6c009ab2010-09-13 00:35:22 +0200636 }
637
Mike Dunn3f91e942012-04-25 12:06:09 -0700638 return max_bitflips;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200639}
640
641/*
Armando Visconti753e0132012-03-07 17:00:54 +0530642 * fsmc_bch8_correct_data
Linus Walleij6c009ab2010-09-13 00:35:22 +0200643 * @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 Visconti753e0132012-03-07 17:00:54 +0530651static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
Linus Walleij6c009ab2010-09-13 00:35:22 +0200652 uint8_t *read_ecc, uint8_t *calc_ecc)
653{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100654 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLON277af422015-12-10 08:59:46 +0100655 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530656 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200657 unsigned int bank = host->bank;
Armando Viscontia612c2a2012-03-07 17:00:53 +0530658 uint32_t err_idx[8];
Linus Walleij6c009ab2010-09-13 00:35:22 +0200659 uint32_t num_err, i;
Armando Visconti753e0132012-03-07 17:00:54 +0530660 uint32_t ecc1, ecc2, ecc3, ecc4;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200661
Vipin Kumara4742d52012-10-09 16:14:50 +0530662 num_err = (readl_relaxed(FSMC_NAND_REG(regs, bank, STS)) >> 10) & 0xF;
Vipin Kumar519300c2012-03-07 17:00:49 +0530663
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 Walleij6c009ab2010-09-13 00:35:22 +0200696 /*
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 Kumara4742d52012-10-09 16:14:50 +0530705 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 Walleij6c009ab2010-09-13 00:35:22 +0200709
Armando Visconti753e0132012-03-07 17:00:54 +0530710 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 Walleij6c009ab2010-09-13 00:35:22 +0200718
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 Kumarb533f8d2012-03-14 11:47:11 +0530724 if (err_idx[i] < chip->ecc.size * 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200725 change_bit(err_idx[i], (unsigned long *)dat);
726 i++;
727 }
728 }
729 return i;
730}
731
Vipin Kumar4774fb02012-03-14 11:47:18 +0530732static bool filter(struct dma_chan *chan, void *slave)
733{
734 chan->private = slave;
735 return true;
736}
737
Stefan Roeseeea62812012-03-16 10:19:31 +0100738#ifdef CONFIG_OF
Bill Pemberton06f25512012-11-19 13:23:07 -0500739static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800740 struct device_node *np)
Stefan Roeseeea62812012-03-16 10:19:31 +0100741{
742 struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
743 u32 val;
Stefan Roese62b57f42015-03-19 14:34:29 +0100744 int ret;
Stefan Roeseeea62812012-03-16 10:19:31 +0100745
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 Roeseeea62812012-03-16 10:19:31 +0100756 if (of_get_property(np, "nand-skip-bbtscan", NULL))
757 pdata->options = NAND_SKIP_BBTSCAN;
758
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200759 pdata->nand_timings = devm_kzalloc(&pdev->dev,
760 sizeof(*pdata->nand_timings), GFP_KERNEL);
Jingoo Hand9a21ae2013-12-26 12:16:38 +0900761 if (!pdata->nand_timings)
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200762 return -ENOMEM;
Stefan Roese62b57f42015-03-19 14:34:29 +0100763 ret = of_property_read_u8_array(np, "timings", (u8 *)pdata->nand_timings,
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200764 sizeof(*pdata->nand_timings));
Stefan Roese62b57f42015-03-19 14:34:29 +0100765 if (ret) {
766 dev_info(&pdev->dev, "No timings in dts specified, using default timings!\n");
767 pdata->nand_timings = NULL;
768 }
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200769
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 Roeseeea62812012-03-16 10:19:31 +0100779 return 0;
780}
781#else
Bill Pemberton06f25512012-11-19 13:23:07 -0500782static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800783 struct device_node *np)
Stefan Roeseeea62812012-03-16 10:19:31 +0100784{
785 return -ENOSYS;
786}
787#endif
788
Linus Walleij6c009ab2010-09-13 00:35:22 +0200789/*
790 * fsmc_nand_probe - Probe function
791 * @pdev: platform device structure
792 */
793static int __init fsmc_nand_probe(struct platform_device *pdev)
794{
795 struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Stefan Roeseeea62812012-03-16 10:19:31 +0100796 struct device_node __maybe_unused *np = pdev->dev.of_node;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200797 struct fsmc_nand_data *host;
798 struct mtd_info *mtd;
799 struct nand_chip *nand;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200800 struct resource *res;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530801 dma_cap_mask_t mask;
Linus Walleij4ad916b2010-11-29 13:52:06 +0100802 int ret = 0;
Linus Walleij593cd872010-11-29 13:52:19 +0100803 u32 pid;
804 int i;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200805
Stefan Roeseeea62812012-03-16 10:19:31 +0100806 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 Walleij6c009ab2010-09-13 00:35:22 +0200816 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 Kumar82b9dbe2012-03-14 11:47:15 +0530822 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Jingoo Hand9a21ae2013-12-26 12:16:38 +0900823 if (!host)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200824 return -ENOMEM;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200825
826 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
Thierry Redingb0de7742013-01-21 11:09:12 +0100827 host->data_va = devm_ioremap_resource(&pdev->dev, res);
828 if (IS_ERR(host->data_va))
829 return PTR_ERR(host->data_va);
Stefan Roesecbf29b82015-10-02 12:40:20 +0200830
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +0200831 host->data_pa = (dma_addr_t)res->start;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200832
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +0200833 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
Thierry Redingb0de7742013-01-21 11:09:12 +0100834 host->addr_va = devm_ioremap_resource(&pdev->dev, res);
835 if (IS_ERR(host->addr_va))
836 return PTR_ERR(host->addr_va);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200837
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +0200838 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
Thierry Redingb0de7742013-01-21 11:09:12 +0100839 host->cmd_va = devm_ioremap_resource(&pdev->dev, res);
840 if (IS_ERR(host->cmd_va))
841 return PTR_ERR(host->cmd_va);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200842
843 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
Thierry Redingb0de7742013-01-21 11:09:12 +0100844 host->regs_va = devm_ioremap_resource(&pdev->dev, res);
845 if (IS_ERR(host->regs_va))
846 return PTR_ERR(host->regs_va);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200847
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 Kumar82b9dbe2012-03-14 11:47:15 +0530851 return PTR_ERR(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200852 }
853
Viresh Kumare25da1c2012-04-17 17:07:57 +0530854 ret = clk_prepare_enable(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200855 if (ret)
Viresh Kumare25da1c2012-04-17 17:07:57 +0530856 goto err_clk_prepare_enable;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200857
Linus Walleij593cd872010-11-29 13:52:19 +0100858 /*
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 Walleij6c009ab2010-09-13 00:35:22 +0200870 host->bank = pdata->bank;
871 host->select_chip = pdata->select_bank;
Vipin Kumar71470322012-03-14 11:47:07 +0530872 host->partitions = pdata->partitions;
873 host->nr_partitions = pdata->nr_partitions;
Vipin Kumar712c4ad2012-03-14 11:47:16 +0530874 host->dev = &pdev->dev;
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530875 host->dev_timings = pdata->nand_timings;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530876 host->mode = pdata->mode;
877
878 if (host->mode == USE_DMA_ACCESS)
879 init_completion(&host->dma_access_complete);
880
Linus Walleij6c009ab2010-09-13 00:35:22 +0200881 /* Link all private pointers */
Boris BREZILLONbdf3a552015-12-10 09:00:05 +0100882 mtd = nand_to_mtd(&host->nand);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200883 nand = &host->nand;
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100884 nand_set_controller_data(nand, host);
Brian Norrisa61ae812015-10-30 20:33:25 -0700885 nand_set_flash_node(nand, np);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200886
Boris BREZILLONbdf3a552015-12-10 09:00:05 +0100887 mtd->dev.parent = &pdev->dev;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200888 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 Roesee278fc72015-10-19 08:40:13 +0200893 /*
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 Walleij6c009ab2010-09-13 00:35:22 +0200897 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 Kumar467e6e72012-03-14 11:47:12 +0530902 nand->badblockbits = 7;
Brian Norris63752192015-10-30 20:33:23 -0700903 nand_set_flash_node(nand, np);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200904
905 if (pdata->width == FSMC_NAND_BW16)
906 nand->options |= NAND_BUSWIDTH_16;
907
Vipin Kumar4774fb02012-03-14 11:47:18 +0530908 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 Kumar604e7542012-03-14 11:47:17 +0530930 nand->read_buf = fsmc_read_buf;
931 nand->write_buf = fsmc_write_buf;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530932 break;
Vipin Kumar604e7542012-03-14 11:47:17 +0530933 }
934
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530935 fsmc_nand_setup(host->regs_va, host->bank,
936 nand->options & NAND_BUSWIDTH_16,
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530937 host->dev_timings);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200938
Linus Walleij593cd872010-11-29 13:52:19 +0100939 if (AMBA_REV_BITS(host->pid) >= 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200940 nand->ecc.read_page = fsmc_read_page_hwecc;
941 nand->ecc.calculate = fsmc_read_hwecc_ecc4;
Armando Visconti753e0132012-03-07 17:00:54 +0530942 nand->ecc.correct = fsmc_bch8_correct_data;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200943 nand->ecc.bytes = 13;
Mike Dunn6a918ba2012-03-11 14:21:11 -0700944 nand->ecc.strength = 8;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200945 }
946
947 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300948 * Scan to find existence of the device
Linus Walleij6c009ab2010-09-13 00:35:22 +0200949 */
Boris BREZILLONbdf3a552015-12-10 09:00:05 +0100950 if (nand_scan_ident(mtd, 1, NULL)) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200951 ret = -ENXIO;
952 dev_err(&pdev->dev, "No NAND Device found!\n");
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530953 goto err_scan_ident;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200954 }
955
Linus Walleij593cd872010-11-29 13:52:19 +0100956 if (AMBA_REV_BITS(host->pid) >= 8) {
Boris BREZILLONbdf3a552015-12-10 09:00:05 +0100957 switch (mtd->oobsize) {
Bhavna Yadave29ee572012-03-07 17:00:50 +0530958 case 16:
Linus Walleij6c009ab2010-09-13 00:35:22 +0200959 host->ecc_place = &fsmc_ecc4_sp_place;
Bhavna Yadave29ee572012-03-07 17:00:50 +0530960 break;
961 case 64:
Linus Walleij6c009ab2010-09-13 00:35:22 +0200962 host->ecc_place = &fsmc_ecc4_lp_place;
Bhavna Yadave29ee572012-03-07 17:00:50 +0530963 break;
964 case 128:
Bhavna Yadave29ee572012-03-07 17:00:50 +0530965 host->ecc_place = &fsmc_ecc4_lp_place;
966 break;
Armando Visconti0c78e932012-03-07 17:00:55 +0530967 case 224:
Armando Visconti0c78e932012-03-07 17:00:55 +0530968 host->ecc_place = &fsmc_ecc4_lp_place;
969 break;
Bhavna Yadave29ee572012-03-07 17:00:50 +0530970 case 256:
Bhavna Yadave29ee572012-03-07 17:00:50 +0530971 host->ecc_place = &fsmc_ecc4_lp_place;
972 break;
973 default:
Jingoo Han67b19a62013-12-26 12:31:25 +0900974 dev_warn(&pdev->dev, "No oob scheme defined for oobsize %d\n",
975 mtd->oobsize);
Stefan Roese6efadcf2015-10-02 12:40:21 +0200976 ret = -EINVAL;
977 goto err_probe;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200978 }
Boris Brezillon22b46952016-02-03 20:01:42 +0100979
980 mtd_set_ooblayout(mtd, &fsmc_ecc4_ooblayout_ops);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200981 } else {
Stefan Roesee278fc72015-10-19 08:40:13 +0200982 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 Yadave29ee572012-03-07 17:00:50 +0530989 break;
Stefan Roesee278fc72015-10-19 08:40:13 +0200990
991 case NAND_ECC_SOFT_BCH:
992 dev_info(&pdev->dev, "Using 4-bit SW BCH ECC scheme\n");
Bhavna Yadave29ee572012-03-07 17:00:50 +0530993 break;
Stefan Roesee278fc72015-10-19 08:40:13 +0200994
Bhavna Yadave29ee572012-03-07 17:00:50 +0530995 default:
Stefan Roesee278fc72015-10-19 08:40:13 +0200996 dev_err(&pdev->dev, "Unsupported ECC mode!\n");
Stefan Roese6efadcf2015-10-02 12:40:21 +0200997 goto err_probe;
Bhavna Yadave29ee572012-03-07 17:00:50 +0530998 }
Stefan Roesee278fc72015-10-19 08:40:13 +0200999
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 BREZILLONbdf3a552015-12-10 09:00:05 +01001005 switch (mtd->oobsize) {
Stefan Roesee278fc72015-10-19 08:40:13 +02001006 case 16:
Stefan Roesee278fc72015-10-19 08:40:13 +02001007 case 64:
Stefan Roesee278fc72015-10-19 08:40:13 +02001008 case 128:
Boris Brezillon22b46952016-02-03 20:01:42 +01001009 mtd_set_ooblayout(mtd,
1010 &fsmc_ecc1_ooblayout_ops);
Stefan Roesee278fc72015-10-19 08:40:13 +02001011 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 Walleij6c009ab2010-09-13 00:35:22 +02001020 }
1021
1022 /* Second stage of scan to fill MTD data-structures */
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001023 if (nand_scan_tail(mtd)) {
Linus Walleij6c009ab2010-09-13 00:35:22 +02001024 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 Walleij6c009ab2010-09-13 00:35:22 +02001035 /*
Dmitry Eremin-Solenikov8d3f8bb2011-05-29 20:16:57 +04001036 * Check for partition info passed
Linus Walleij6c009ab2010-09-13 00:35:22 +02001037 */
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001038 mtd->name = "nand";
1039 ret = mtd_device_register(mtd, host->partitions, host->nr_partitions);
Jamie Iles99335d02011-05-23 10:23:23 +01001040 if (ret)
Linus Walleij6c009ab2010-09-13 00:35:22 +02001041 goto err_probe;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001042
1043 platform_set_drvdata(pdev, host);
1044 dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
1045 return 0;
1046
1047err_probe:
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301048err_scan_ident:
Vipin Kumar4774fb02012-03-14 11:47:18 +05301049 if (host->mode == USE_DMA_ACCESS)
1050 dma_release_channel(host->write_dma_chan);
1051err_req_write_chnl:
1052 if (host->mode == USE_DMA_ACCESS)
1053 dma_release_channel(host->read_dma_chan);
1054err_req_read_chnl:
Viresh Kumare25da1c2012-04-17 17:07:57 +05301055 clk_disable_unprepare(host->clk);
1056err_clk_prepare_enable:
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301057 clk_put(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001058 return ret;
1059}
1060
1061/*
1062 * Clean up routine
1063 */
1064static int fsmc_nand_remove(struct platform_device *pdev)
1065{
1066 struct fsmc_nand_data *host = platform_get_drvdata(pdev);
1067
Linus Walleij6c009ab2010-09-13 00:35:22 +02001068 if (host) {
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001069 nand_release(nand_to_mtd(&host->nand));
Vipin Kumar4774fb02012-03-14 11:47:18 +05301070
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 Kumare25da1c2012-04-17 17:07:57 +05301075 clk_disable_unprepare(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001076 clk_put(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001077 }
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301078
Linus Walleij6c009ab2010-09-13 00:35:22 +02001079 return 0;
1080}
1081
Jingoo Han80ce4dd2013-03-26 15:53:48 +09001082#ifdef CONFIG_PM_SLEEP
Linus Walleij6c009ab2010-09-13 00:35:22 +02001083static int fsmc_nand_suspend(struct device *dev)
1084{
1085 struct fsmc_nand_data *host = dev_get_drvdata(dev);
1086 if (host)
Viresh Kumare25da1c2012-04-17 17:07:57 +05301087 clk_disable_unprepare(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001088 return 0;
1089}
1090
1091static int fsmc_nand_resume(struct device *dev)
1092{
1093 struct fsmc_nand_data *host = dev_get_drvdata(dev);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301094 if (host) {
Viresh Kumare25da1c2012-04-17 17:07:57 +05301095 clk_prepare_enable(host->clk);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301096 fsmc_nand_setup(host->regs_va, host->bank,
Vipin Kumare2f6bce2012-03-14 11:47:14 +05301097 host->nand.options & NAND_BUSWIDTH_16,
1098 host->dev_timings);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301099 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001100 return 0;
1101}
Jingoo Han80ce4dd2013-03-26 15:53:48 +09001102#endif
Linus Walleij6c009ab2010-09-13 00:35:22 +02001103
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301104static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001105
Stefan Roeseeea62812012-03-16 10:19:31 +01001106#ifdef CONFIG_OF
1107static const struct of_device_id fsmc_nand_id_table[] = {
1108 { .compatible = "st,spear600-fsmc-nand" },
Linus Walleijba785202013-01-05 22:28:32 +01001109 { .compatible = "stericsson,fsmc-nand" },
Stefan Roeseeea62812012-03-16 10:19:31 +01001110 {}
1111};
1112MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);
1113#endif
1114
Linus Walleij6c009ab2010-09-13 00:35:22 +02001115static struct platform_driver fsmc_nand_driver = {
1116 .remove = fsmc_nand_remove,
1117 .driver = {
Linus Walleij6c009ab2010-09-13 00:35:22 +02001118 .name = "fsmc-nand",
Stefan Roeseeea62812012-03-16 10:19:31 +01001119 .of_match_table = of_match_ptr(fsmc_nand_id_table),
Linus Walleij6c009ab2010-09-13 00:35:22 +02001120 .pm = &fsmc_nand_pm_ops,
Linus Walleij6c009ab2010-09-13 00:35:22 +02001121 },
1122};
1123
Jingoo Han307d2a512013-03-05 13:30:36 +09001124module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001125
1126MODULE_LICENSE("GPL");
1127MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
1128MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");