blob: 75a825dc5a4cdca5d55245e3ce4cf6c8cc07f5cd [file] [log] [blame]
Linus Walleij6c009ab2010-09-13 00:35:22 +02001/*
Linus Walleij6c009ab2010-09-13 00:35:22 +02002 * ST Microelectronics
3 * Flexible Static Memory Controller (FSMC)
4 * Driver for NAND portions
5 *
6 * Copyright © 2010 ST Microelectronics
7 * Vipin Kumar <vipin.kumar@st.com>
8 * Ashish Priyadarshi
9 *
Boris Brezillon187c54482018-02-05 23:02:02 +010010 * Based on drivers/mtd/nand/nomadik_nand.c (removed in v3.8)
Boris Brezillon7b6afee2018-02-05 23:02:03 +010011 * Copyright © 2007 STMicroelectronics Pvt. Ltd.
12 * Copyright © 2009 Alessandro Rubini
Linus Walleij6c009ab2010-09-13 00:35:22 +020013 *
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>
Boris Brezillond4092d72017-08-04 17:29:10 +020031#include <linux/mtd/rawnand.h>
Linus Walleij6c009ab2010-09-13 00:35:22 +020032#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>
Linus Walleij593cd872010-11-29 13:52:19 +010038#include <linux/amba/bus.h>
Linus Walleij6c009ab2010-09-13 00:35:22 +020039#include <mtd/mtd-abi.h>
40
Linus Walleij4404d7d2016-12-18 12:34:55 +010041/* fsmc controller registers for NOR flash */
42#define CTRL 0x0
43 /* ctrl register definitions */
44 #define BANK_ENABLE (1 << 0)
45 #define MUXED (1 << 1)
46 #define NOR_DEV (2 << 2)
47 #define WIDTH_8 (0 << 4)
48 #define WIDTH_16 (1 << 4)
49 #define RSTPWRDWN (1 << 6)
50 #define WPROT (1 << 7)
51 #define WRT_ENABLE (1 << 12)
52 #define WAIT_ENB (1 << 13)
53
54#define CTRL_TIM 0x4
55 /* ctrl_tim register definitions */
56
57#define FSMC_NOR_BANK_SZ 0x8
58#define FSMC_NOR_REG_SIZE 0x40
59
60#define FSMC_NOR_REG(base, bank, reg) (base + \
61 FSMC_NOR_BANK_SZ * (bank) + \
62 reg)
63
64/* fsmc controller registers for NAND flash */
65#define PC 0x00
66 /* pc register definitions */
67 #define FSMC_RESET (1 << 0)
68 #define FSMC_WAITON (1 << 1)
69 #define FSMC_ENABLE (1 << 2)
70 #define FSMC_DEVTYPE_NAND (1 << 3)
71 #define FSMC_DEVWID_8 (0 << 4)
72 #define FSMC_DEVWID_16 (1 << 4)
73 #define FSMC_ECCEN (1 << 6)
74 #define FSMC_ECCPLEN_512 (0 << 7)
75 #define FSMC_ECCPLEN_256 (1 << 7)
76 #define FSMC_TCLR_1 (1)
77 #define FSMC_TCLR_SHIFT (9)
78 #define FSMC_TCLR_MASK (0xF)
79 #define FSMC_TAR_1 (1)
80 #define FSMC_TAR_SHIFT (13)
81 #define FSMC_TAR_MASK (0xF)
82#define STS 0x04
83 /* sts register definitions */
84 #define FSMC_CODE_RDY (1 << 15)
85#define COMM 0x08
86 /* comm register definitions */
87 #define FSMC_TSET_0 0
88 #define FSMC_TSET_SHIFT 0
89 #define FSMC_TSET_MASK 0xFF
90 #define FSMC_TWAIT_6 6
91 #define FSMC_TWAIT_SHIFT 8
92 #define FSMC_TWAIT_MASK 0xFF
93 #define FSMC_THOLD_4 4
94 #define FSMC_THOLD_SHIFT 16
95 #define FSMC_THOLD_MASK 0xFF
96 #define FSMC_THIZ_1 1
97 #define FSMC_THIZ_SHIFT 24
98 #define FSMC_THIZ_MASK 0xFF
99#define ATTRIB 0x0C
100#define IOATA 0x10
101#define ECC1 0x14
102#define ECC2 0x18
103#define ECC3 0x1C
104#define FSMC_NAND_BANK_SZ 0x20
105
Linus Walleij4404d7d2016-12-18 12:34:55 +0100106#define FSMC_BUSY_WAIT_TIMEOUT (1 * HZ)
107
108struct fsmc_nand_timings {
109 uint8_t tclr;
110 uint8_t tar;
111 uint8_t thiz;
112 uint8_t thold;
113 uint8_t twait;
114 uint8_t tset;
115};
116
117enum access_mode {
118 USE_DMA_ACCESS = 1,
119 USE_WORD_ACCESS,
120};
121
122/**
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100123 * struct fsmc_nand_data - structure for FSMC NAND device state
124 *
125 * @pid: Part ID on the AMBA PrimeCell format
126 * @mtd: MTD info for a NAND flash.
127 * @nand: Chip related info for a NAND flash.
128 * @partitions: Partition info for a NAND Flash.
129 * @nr_partitions: Total number of partition of a NAND flash.
130 *
131 * @bank: Bank number for probed device.
132 * @clk: Clock structure for FSMC.
133 *
134 * @read_dma_chan: DMA channel for read access
135 * @write_dma_chan: DMA channel for write access to NAND
136 * @dma_access_complete: Completion structure
137 *
138 * @data_pa: NAND Physical port for Data.
139 * @data_va: NAND port for Data.
140 * @cmd_va: NAND port for Command.
141 * @addr_va: NAND port for Address.
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100142 * @regs_va: Registers base address for a given bank.
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100143 */
144struct fsmc_nand_data {
145 u32 pid;
146 struct nand_chip nand;
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100147
148 unsigned int bank;
149 struct device *dev;
150 enum access_mode mode;
151 struct clk *clk;
152
153 /* DMA related objects */
154 struct dma_chan *read_dma_chan;
155 struct dma_chan *write_dma_chan;
156 struct completion dma_access_complete;
157
158 struct fsmc_nand_timings *dev_timings;
159
160 dma_addr_t data_pa;
161 void __iomem *data_va;
162 void __iomem *cmd_va;
163 void __iomem *addr_va;
164 void __iomem *regs_va;
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100165};
166
Boris Brezillon22b46952016-02-03 20:01:42 +0100167static int fsmc_ecc1_ooblayout_ecc(struct mtd_info *mtd, int section,
168 struct mtd_oob_region *oobregion)
169{
170 struct nand_chip *chip = mtd_to_nand(mtd);
171
172 if (section >= chip->ecc.steps)
173 return -ERANGE;
174
175 oobregion->offset = (section * 16) + 2;
176 oobregion->length = 3;
177
178 return 0;
179}
180
181static int fsmc_ecc1_ooblayout_free(struct mtd_info *mtd, int section,
182 struct mtd_oob_region *oobregion)
183{
184 struct nand_chip *chip = mtd_to_nand(mtd);
185
186 if (section >= chip->ecc.steps)
187 return -ERANGE;
188
189 oobregion->offset = (section * 16) + 8;
190
191 if (section < chip->ecc.steps - 1)
192 oobregion->length = 8;
193 else
194 oobregion->length = mtd->oobsize - oobregion->offset;
195
196 return 0;
197}
198
199static const struct mtd_ooblayout_ops fsmc_ecc1_ooblayout_ops = {
200 .ecc = fsmc_ecc1_ooblayout_ecc,
201 .free = fsmc_ecc1_ooblayout_free,
202};
203
Boris Brezillon04a123a2016-02-09 15:01:21 +0100204/*
205 * ECC placement definitions in oobfree type format.
206 * There are 13 bytes of ecc for every 512 byte block and it has to be read
207 * consecutively and immediately after the 512 byte data block for hardware to
208 * generate the error bit offsets in 512 byte data.
209 */
Boris Brezillon22b46952016-02-03 20:01:42 +0100210static int fsmc_ecc4_ooblayout_ecc(struct mtd_info *mtd, int section,
211 struct mtd_oob_region *oobregion)
212{
213 struct nand_chip *chip = mtd_to_nand(mtd);
214
215 if (section >= chip->ecc.steps)
216 return -ERANGE;
217
218 oobregion->length = chip->ecc.bytes;
219
220 if (!section && mtd->writesize <= 512)
221 oobregion->offset = 0;
222 else
223 oobregion->offset = (section * 16) + 2;
224
225 return 0;
226}
227
228static int fsmc_ecc4_ooblayout_free(struct mtd_info *mtd, int section,
229 struct mtd_oob_region *oobregion)
230{
231 struct nand_chip *chip = mtd_to_nand(mtd);
232
233 if (section >= chip->ecc.steps)
234 return -ERANGE;
235
236 oobregion->offset = (section * 16) + 15;
237
238 if (section < chip->ecc.steps - 1)
239 oobregion->length = 3;
240 else
241 oobregion->length = mtd->oobsize - oobregion->offset;
242
243 return 0;
244}
245
246static const struct mtd_ooblayout_ops fsmc_ecc4_ooblayout_ops = {
247 .ecc = fsmc_ecc4_ooblayout_ecc,
248 .free = fsmc_ecc4_ooblayout_free,
249};
250
Boris BREZILLON277af422015-12-10 08:59:46 +0100251static inline struct fsmc_nand_data *mtd_to_fsmc(struct mtd_info *mtd)
252{
Boris BREZILLONbdf3a552015-12-10 09:00:05 +0100253 return container_of(mtd_to_nand(mtd), struct fsmc_nand_data, nand);
Boris BREZILLON277af422015-12-10 08:59:46 +0100254}
255
Linus Walleij6c009ab2010-09-13 00:35:22 +0200256/*
257 * fsmc_cmd_ctrl - For facilitaing Hardware access
258 * This routine allows hardware specific access to control-lines(ALE,CLE)
259 */
260static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
261{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100262 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLON277af422015-12-10 08:59:46 +0100263 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200264
265 if (ctrl & NAND_CTRL_CHANGE) {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530266 u32 pc;
267
Linus Walleij6c009ab2010-09-13 00:35:22 +0200268 if (ctrl & NAND_CLE) {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530269 this->IO_ADDR_R = host->cmd_va;
270 this->IO_ADDR_W = host->cmd_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200271 } else if (ctrl & NAND_ALE) {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530272 this->IO_ADDR_R = host->addr_va;
273 this->IO_ADDR_W = host->addr_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200274 } else {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530275 this->IO_ADDR_R = host->data_va;
276 this->IO_ADDR_W = host->data_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200277 }
278
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100279 pc = readl(host->regs_va + PC);
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530280 if (ctrl & NAND_NCE)
281 pc |= FSMC_ENABLE;
282 else
283 pc &= ~FSMC_ENABLE;
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100284 writel_relaxed(pc, host->regs_va + PC);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200285 }
286
287 mb();
288
289 if (cmd != NAND_CMD_NONE)
Vipin Kumara4742d52012-10-09 16:14:50 +0530290 writeb_relaxed(cmd, this->IO_ADDR_W);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200291}
292
293/*
294 * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
295 *
296 * This routine initializes timing parameters related to NAND memory access in
297 * FSMC registers
298 */
Thomas Petazzoni6335b502017-04-29 10:52:34 +0200299static void fsmc_nand_setup(struct fsmc_nand_data *host,
Thomas Petazzoni1debdb92017-04-29 10:52:36 +0200300 struct fsmc_nand_timings *tims)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200301{
302 uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530303 uint32_t tclr, tar, thiz, thold, twait, tset;
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530304
305 tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT;
306 tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT;
307 thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT;
308 thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT;
309 twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT;
310 tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200311
Thomas Petazzoni6335b502017-04-29 10:52:34 +0200312 if (host->nand.options & NAND_BUSWIDTH_16)
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100313 writel_relaxed(value | FSMC_DEVWID_16, host->regs_va + PC);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200314 else
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100315 writel_relaxed(value | FSMC_DEVWID_8, host->regs_va + PC);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200316
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100317 writel_relaxed(readl(host->regs_va + PC) | tclr | tar,
318 host->regs_va + PC);
319 writel_relaxed(thiz | thold | twait | tset, host->regs_va + COMM);
320 writel_relaxed(thiz | thold | twait | tset, host->regs_va + ATTRIB);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200321}
322
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200323static int fsmc_calc_timings(struct fsmc_nand_data *host,
324 const struct nand_sdr_timings *sdrt,
325 struct fsmc_nand_timings *tims)
326{
327 unsigned long hclk = clk_get_rate(host->clk);
328 unsigned long hclkn = NSEC_PER_SEC / hclk;
329 uint32_t thiz, thold, twait, tset;
330
331 if (sdrt->tRC_min < 30000)
332 return -EOPNOTSUPP;
333
334 tims->tar = DIV_ROUND_UP(sdrt->tAR_min / 1000, hclkn) - 1;
335 if (tims->tar > FSMC_TAR_MASK)
336 tims->tar = FSMC_TAR_MASK;
337 tims->tclr = DIV_ROUND_UP(sdrt->tCLR_min / 1000, hclkn) - 1;
338 if (tims->tclr > FSMC_TCLR_MASK)
339 tims->tclr = FSMC_TCLR_MASK;
340
341 thiz = sdrt->tCS_min - sdrt->tWP_min;
342 tims->thiz = DIV_ROUND_UP(thiz / 1000, hclkn);
343
344 thold = sdrt->tDH_min;
345 if (thold < sdrt->tCH_min)
346 thold = sdrt->tCH_min;
347 if (thold < sdrt->tCLH_min)
348 thold = sdrt->tCLH_min;
349 if (thold < sdrt->tWH_min)
350 thold = sdrt->tWH_min;
351 if (thold < sdrt->tALH_min)
352 thold = sdrt->tALH_min;
353 if (thold < sdrt->tREH_min)
354 thold = sdrt->tREH_min;
355 tims->thold = DIV_ROUND_UP(thold / 1000, hclkn);
356 if (tims->thold == 0)
357 tims->thold = 1;
358 else if (tims->thold > FSMC_THOLD_MASK)
359 tims->thold = FSMC_THOLD_MASK;
360
361 twait = max(sdrt->tRP_min, sdrt->tWP_min);
362 tims->twait = DIV_ROUND_UP(twait / 1000, hclkn) - 1;
363 if (tims->twait == 0)
364 tims->twait = 1;
365 else if (tims->twait > FSMC_TWAIT_MASK)
366 tims->twait = FSMC_TWAIT_MASK;
367
368 tset = max(sdrt->tCS_min - sdrt->tWP_min,
369 sdrt->tCEA_max - sdrt->tREA_max);
370 tims->tset = DIV_ROUND_UP(tset / 1000, hclkn) - 1;
371 if (tims->tset == 0)
372 tims->tset = 1;
373 else if (tims->tset > FSMC_TSET_MASK)
374 tims->tset = FSMC_TSET_MASK;
375
376 return 0;
377}
378
Boris Brezillon104e4422017-03-16 09:35:58 +0100379static int fsmc_setup_data_interface(struct mtd_info *mtd, int csline,
380 const struct nand_data_interface *conf)
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200381{
382 struct nand_chip *nand = mtd_to_nand(mtd);
383 struct fsmc_nand_data *host = nand_get_controller_data(nand);
384 struct fsmc_nand_timings tims;
385 const struct nand_sdr_timings *sdrt;
386 int ret;
387
388 sdrt = nand_get_sdr_timings(conf);
389 if (IS_ERR(sdrt))
390 return PTR_ERR(sdrt);
391
392 ret = fsmc_calc_timings(host, sdrt, &tims);
393 if (ret)
394 return ret;
395
Boris Brezillon104e4422017-03-16 09:35:58 +0100396 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200397 return 0;
398
399 fsmc_nand_setup(host, &tims);
400
401 return 0;
402}
403
Linus Walleij6c009ab2010-09-13 00:35:22 +0200404/*
405 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
406 */
407static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
408{
Boris BREZILLON277af422015-12-10 08:59:46 +0100409 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200410
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100411 writel_relaxed(readl(host->regs_va + PC) & ~FSMC_ECCPLEN_256,
412 host->regs_va + PC);
413 writel_relaxed(readl(host->regs_va + PC) & ~FSMC_ECCEN,
414 host->regs_va + PC);
415 writel_relaxed(readl(host->regs_va + PC) | FSMC_ECCEN,
416 host->regs_va + PC);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200417}
418
419/*
420 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300421 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200422 * max of 8-bits)
423 */
424static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
425 uint8_t *ecc)
426{
Boris BREZILLON277af422015-12-10 08:59:46 +0100427 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200428 uint32_t ecc_tmp;
429 unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
430
431 do {
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100432 if (readl_relaxed(host->regs_va + STS) & FSMC_CODE_RDY)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200433 break;
434 else
435 cond_resched();
436 } while (!time_after_eq(jiffies, deadline));
437
Vipin Kumar712c4ad2012-03-14 11:47:16 +0530438 if (time_after_eq(jiffies, deadline)) {
439 dev_err(host->dev, "calculate ecc timed out\n");
440 return -ETIMEDOUT;
441 }
442
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100443 ecc_tmp = readl_relaxed(host->regs_va + ECC1);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200444 ecc[0] = (uint8_t) (ecc_tmp >> 0);
445 ecc[1] = (uint8_t) (ecc_tmp >> 8);
446 ecc[2] = (uint8_t) (ecc_tmp >> 16);
447 ecc[3] = (uint8_t) (ecc_tmp >> 24);
448
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100449 ecc_tmp = readl_relaxed(host->regs_va + ECC2);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200450 ecc[4] = (uint8_t) (ecc_tmp >> 0);
451 ecc[5] = (uint8_t) (ecc_tmp >> 8);
452 ecc[6] = (uint8_t) (ecc_tmp >> 16);
453 ecc[7] = (uint8_t) (ecc_tmp >> 24);
454
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100455 ecc_tmp = readl_relaxed(host->regs_va + ECC3);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200456 ecc[8] = (uint8_t) (ecc_tmp >> 0);
457 ecc[9] = (uint8_t) (ecc_tmp >> 8);
458 ecc[10] = (uint8_t) (ecc_tmp >> 16);
459 ecc[11] = (uint8_t) (ecc_tmp >> 24);
460
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100461 ecc_tmp = readl_relaxed(host->regs_va + STS);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200462 ecc[12] = (uint8_t) (ecc_tmp >> 16);
463
464 return 0;
465}
466
467/*
468 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300469 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200470 * max of 1-bit)
471 */
472static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
473 uint8_t *ecc)
474{
Boris BREZILLON277af422015-12-10 08:59:46 +0100475 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200476 uint32_t ecc_tmp;
477
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100478 ecc_tmp = readl_relaxed(host->regs_va + ECC1);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200479 ecc[0] = (uint8_t) (ecc_tmp >> 0);
480 ecc[1] = (uint8_t) (ecc_tmp >> 8);
481 ecc[2] = (uint8_t) (ecc_tmp >> 16);
482
483 return 0;
484}
485
Vipin Kumar519300c2012-03-07 17:00:49 +0530486/* Count the number of 0's in buff upto a max of max_bits */
487static int count_written_bits(uint8_t *buff, int size, int max_bits)
488{
489 int k, written_bits = 0;
490
491 for (k = 0; k < size; k++) {
492 written_bits += hweight8(~buff[k]);
493 if (written_bits > max_bits)
494 break;
495 }
496
497 return written_bits;
498}
499
Vipin Kumar4774fb02012-03-14 11:47:18 +0530500static void dma_complete(void *param)
501{
502 struct fsmc_nand_data *host = param;
503
504 complete(&host->dma_access_complete);
505}
506
507static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len,
508 enum dma_data_direction direction)
509{
510 struct dma_chan *chan;
511 struct dma_device *dma_dev;
512 struct dma_async_tx_descriptor *tx;
513 dma_addr_t dma_dst, dma_src, dma_addr;
514 dma_cookie_t cookie;
515 unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
516 int ret;
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400517 unsigned long time_left;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530518
519 if (direction == DMA_TO_DEVICE)
520 chan = host->write_dma_chan;
521 else if (direction == DMA_FROM_DEVICE)
522 chan = host->read_dma_chan;
523 else
524 return -EINVAL;
525
526 dma_dev = chan->device;
527 dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction);
528
529 if (direction == DMA_TO_DEVICE) {
530 dma_src = dma_addr;
531 dma_dst = host->data_pa;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530532 } else {
533 dma_src = host->data_pa;
534 dma_dst = dma_addr;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530535 }
536
537 tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
538 len, flags);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530539 if (!tx) {
540 dev_err(host->dev, "device_prep_dma_memcpy error\n");
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000541 ret = -EIO;
542 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530543 }
544
545 tx->callback = dma_complete;
546 tx->callback_param = host;
547 cookie = tx->tx_submit(tx);
548
549 ret = dma_submit_error(cookie);
550 if (ret) {
551 dev_err(host->dev, "dma_submit_error %d\n", cookie);
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000552 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530553 }
554
555 dma_async_issue_pending(chan);
556
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400557 time_left =
Vipin Kumar928aa2a2012-10-09 16:14:48 +0530558 wait_for_completion_timeout(&host->dma_access_complete,
Vipin Kumar4774fb02012-03-14 11:47:18 +0530559 msecs_to_jiffies(3000));
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400560 if (time_left == 0) {
Vinod Koulb177ea32014-10-11 21:10:32 +0530561 dmaengine_terminate_all(chan);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530562 dev_err(host->dev, "wait_for_completion_timeout\n");
Nicholas Mc Guire0bda3e12015-03-13 07:54:45 -0400563 ret = -ETIMEDOUT;
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000564 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530565 }
566
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000567 ret = 0;
568
569unmap_dma:
570 dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
571
572 return ret;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530573}
574
Linus Walleij6c009ab2010-09-13 00:35:22 +0200575/*
Vipin Kumar604e7542012-03-14 11:47:17 +0530576 * fsmc_write_buf - write buffer to chip
577 * @mtd: MTD device structure
578 * @buf: data buffer
579 * @len: number of bytes to write
580 */
581static void fsmc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
582{
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100583 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar604e7542012-03-14 11:47:17 +0530584 int i;
Vipin Kumar604e7542012-03-14 11:47:17 +0530585
586 if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
587 IS_ALIGNED(len, sizeof(uint32_t))) {
588 uint32_t *p = (uint32_t *)buf;
589 len = len >> 2;
590 for (i = 0; i < len; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100591 writel_relaxed(p[i], host->data_va);
Vipin Kumar604e7542012-03-14 11:47:17 +0530592 } else {
593 for (i = 0; i < len; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100594 writeb_relaxed(buf[i], host->data_va);
Vipin Kumar604e7542012-03-14 11:47:17 +0530595 }
596}
597
598/*
599 * fsmc_read_buf - read chip data into buffer
600 * @mtd: MTD device structure
601 * @buf: buffer to store date
602 * @len: number of bytes to read
603 */
604static void fsmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
605{
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100606 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar604e7542012-03-14 11:47:17 +0530607 int i;
Vipin Kumar604e7542012-03-14 11:47:17 +0530608
609 if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
610 IS_ALIGNED(len, sizeof(uint32_t))) {
611 uint32_t *p = (uint32_t *)buf;
612 len = len >> 2;
613 for (i = 0; i < len; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100614 p[i] = readl_relaxed(host->data_va);
Vipin Kumar604e7542012-03-14 11:47:17 +0530615 } else {
616 for (i = 0; i < len; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100617 buf[i] = readb_relaxed(host->data_va);
Vipin Kumar604e7542012-03-14 11:47:17 +0530618 }
619}
620
621/*
Vipin Kumar4774fb02012-03-14 11:47:18 +0530622 * fsmc_read_buf_dma - read chip data into buffer
623 * @mtd: MTD device structure
624 * @buf: buffer to store date
625 * @len: number of bytes to read
626 */
627static void fsmc_read_buf_dma(struct mtd_info *mtd, uint8_t *buf, int len)
628{
Boris BREZILLON277af422015-12-10 08:59:46 +0100629 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530630
Vipin Kumar4774fb02012-03-14 11:47:18 +0530631 dma_xfer(host, buf, len, DMA_FROM_DEVICE);
632}
633
634/*
635 * fsmc_write_buf_dma - write buffer to chip
636 * @mtd: MTD device structure
637 * @buf: data buffer
638 * @len: number of bytes to write
639 */
640static void fsmc_write_buf_dma(struct mtd_info *mtd, const uint8_t *buf,
641 int len)
642{
Boris BREZILLON277af422015-12-10 08:59:46 +0100643 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530644
Vipin Kumar4774fb02012-03-14 11:47:18 +0530645 dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
646}
647
648/*
Linus Walleij6c009ab2010-09-13 00:35:22 +0200649 * fsmc_read_page_hwecc
650 * @mtd: mtd info structure
651 * @chip: nand chip info structure
652 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -0700653 * @oob_required: caller expects OOB data read to chip->oob_poi
Linus Walleij6c009ab2010-09-13 00:35:22 +0200654 * @page: page number to read
655 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300656 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
Linus Walleij6c009ab2010-09-13 00:35:22 +0200657 * performed in a strict sequence as follows:
658 * data(512 byte) -> ecc(13 byte)
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300659 * After this read, fsmc hardware generates and reports error data bits(up to a
Linus Walleij6c009ab2010-09-13 00:35:22 +0200660 * max of 8 bits)
661 */
662static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700663 uint8_t *buf, int oob_required, int page)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200664{
Linus Walleij6c009ab2010-09-13 00:35:22 +0200665 int i, j, s, stat, eccsize = chip->ecc.size;
666 int eccbytes = chip->ecc.bytes;
667 int eccsteps = chip->ecc.steps;
668 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +0900669 uint8_t *ecc_calc = chip->ecc.calc_buf;
670 uint8_t *ecc_code = chip->ecc.code_buf;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200671 int off, len, group = 0;
672 /*
673 * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
674 * end up reading 14 bytes (7 words) from oob. The local array is
675 * to maintain word alignment
676 */
677 uint16_t ecc_oob[7];
678 uint8_t *oob = (uint8_t *)&ecc_oob[0];
Mike Dunn3f91e942012-04-25 12:06:09 -0700679 unsigned int max_bitflips = 0;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200680
681 for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100682 nand_read_page_op(chip, page, s * eccsize, NULL, 0);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200683 chip->ecc.hwctl(mtd, NAND_ECC_READ);
684 chip->read_buf(mtd, p, eccsize);
685
686 for (j = 0; j < eccbytes;) {
Boris Brezillon04a123a2016-02-09 15:01:21 +0100687 struct mtd_oob_region oobregion;
688 int ret;
689
690 ret = mtd_ooblayout_ecc(mtd, group++, &oobregion);
691 if (ret)
692 return ret;
693
694 off = oobregion.offset;
695 len = oobregion.length;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200696
697 /*
Vipin Kumar4cbe1bf02012-03-14 11:47:09 +0530698 * length is intentionally kept a higher multiple of 2
699 * to read at least 13 bytes even in case of 16 bit NAND
700 * devices
701 */
Vipin Kumaraea686b2012-03-14 11:47:10 +0530702 if (chip->options & NAND_BUSWIDTH_16)
703 len = roundup(len, 2);
704
Boris Brezillon97d90da2017-11-30 18:01:29 +0100705 nand_read_oob_op(chip, page, off, oob + j, len);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200706 j += len;
707 }
708
Vipin Kumar519300c2012-03-07 17:00:49 +0530709 memcpy(&ecc_code[i], oob, chip->ecc.bytes);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200710 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
711
712 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -0700713 if (stat < 0) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200714 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -0700715 } else {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200716 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -0700717 max_bitflips = max_t(unsigned int, max_bitflips, stat);
718 }
Linus Walleij6c009ab2010-09-13 00:35:22 +0200719 }
720
Mike Dunn3f91e942012-04-25 12:06:09 -0700721 return max_bitflips;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200722}
723
724/*
Armando Visconti753e0132012-03-07 17:00:54 +0530725 * fsmc_bch8_correct_data
Linus Walleij6c009ab2010-09-13 00:35:22 +0200726 * @mtd: mtd info structure
727 * @dat: buffer of read data
728 * @read_ecc: ecc read from device spare area
729 * @calc_ecc: ecc calculated from read data
730 *
731 * calc_ecc is a 104 bit information containing maximum of 8 error
732 * offset informations of 13 bits each in 512 bytes of read data.
733 */
Armando Visconti753e0132012-03-07 17:00:54 +0530734static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
Linus Walleij6c009ab2010-09-13 00:35:22 +0200735 uint8_t *read_ecc, uint8_t *calc_ecc)
736{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100737 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLON277af422015-12-10 08:59:46 +0100738 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Armando Viscontia612c2a2012-03-07 17:00:53 +0530739 uint32_t err_idx[8];
Linus Walleij6c009ab2010-09-13 00:35:22 +0200740 uint32_t num_err, i;
Armando Visconti753e0132012-03-07 17:00:54 +0530741 uint32_t ecc1, ecc2, ecc3, ecc4;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200742
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100743 num_err = (readl_relaxed(host->regs_va + STS) >> 10) & 0xF;
Vipin Kumar519300c2012-03-07 17:00:49 +0530744
745 /* no bit flipping */
746 if (likely(num_err == 0))
747 return 0;
748
749 /* too many errors */
750 if (unlikely(num_err > 8)) {
751 /*
752 * This is a temporary erase check. A newly erased page read
753 * would result in an ecc error because the oob data is also
754 * erased to FF and the calculated ecc for an FF data is not
755 * FF..FF.
756 * This is a workaround to skip performing correction in case
757 * data is FF..FF
758 *
759 * Logic:
760 * For every page, each bit written as 0 is counted until these
761 * number of bits are greater than 8 (the maximum correction
762 * capability of FSMC for each 512 + 13 bytes)
763 */
764
765 int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
766 int bits_data = count_written_bits(dat, chip->ecc.size, 8);
767
768 if ((bits_ecc + bits_data) <= 8) {
769 if (bits_data)
770 memset(dat, 0xff, chip->ecc.size);
771 return bits_data;
772 }
773
774 return -EBADMSG;
775 }
776
Linus Walleij6c009ab2010-09-13 00:35:22 +0200777 /*
778 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
779 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
780 *
781 * calc_ecc is a 104 bit information containing maximum of 8 error
782 * offset informations of 13 bits each. calc_ecc is copied into a
783 * uint64_t array and error offset indexes are populated in err_idx
784 * array
785 */
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100786 ecc1 = readl_relaxed(host->regs_va + ECC1);
787 ecc2 = readl_relaxed(host->regs_va + ECC2);
788 ecc3 = readl_relaxed(host->regs_va + ECC3);
789 ecc4 = readl_relaxed(host->regs_va + STS);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200790
Armando Visconti753e0132012-03-07 17:00:54 +0530791 err_idx[0] = (ecc1 >> 0) & 0x1FFF;
792 err_idx[1] = (ecc1 >> 13) & 0x1FFF;
793 err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
794 err_idx[3] = (ecc2 >> 7) & 0x1FFF;
795 err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
796 err_idx[5] = (ecc3 >> 1) & 0x1FFF;
797 err_idx[6] = (ecc3 >> 14) & 0x1FFF;
798 err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200799
800 i = 0;
801 while (num_err--) {
802 change_bit(0, (unsigned long *)&err_idx[i]);
803 change_bit(1, (unsigned long *)&err_idx[i]);
804
Vipin Kumarb533f8d2012-03-14 11:47:11 +0530805 if (err_idx[i] < chip->ecc.size * 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200806 change_bit(err_idx[i], (unsigned long *)dat);
807 i++;
808 }
809 }
810 return i;
811}
812
Vipin Kumar4774fb02012-03-14 11:47:18 +0530813static bool filter(struct dma_chan *chan, void *slave)
814{
815 chan->private = slave;
816 return true;
817}
818
Bill Pemberton06f25512012-11-19 13:23:07 -0500819static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100820 struct fsmc_nand_data *host,
821 struct nand_chip *nand)
Stefan Roeseeea62812012-03-16 10:19:31 +0100822{
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100823 struct device_node *np = pdev->dev.of_node;
Stefan Roeseeea62812012-03-16 10:19:31 +0100824 u32 val;
Stefan Roese62b57f42015-03-19 14:34:29 +0100825 int ret;
Stefan Roeseeea62812012-03-16 10:19:31 +0100826
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100827 nand->options = 0;
Thomas Petazzoniee568742017-03-21 11:03:53 +0100828
Stefan Roeseeea62812012-03-16 10:19:31 +0100829 if (!of_property_read_u32(np, "bank-width", &val)) {
830 if (val == 2) {
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100831 nand->options |= NAND_BUSWIDTH_16;
Stefan Roeseeea62812012-03-16 10:19:31 +0100832 } else if (val != 1) {
833 dev_err(&pdev->dev, "invalid bank-width %u\n", val);
834 return -EINVAL;
835 }
836 }
Thomas Petazzoniee568742017-03-21 11:03:53 +0100837
Stefan Roeseeea62812012-03-16 10:19:31 +0100838 if (of_get_property(np, "nand-skip-bbtscan", NULL))
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100839 nand->options |= NAND_SKIP_BBTSCAN;
Stefan Roeseeea62812012-03-16 10:19:31 +0100840
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100841 host->dev_timings = devm_kzalloc(&pdev->dev,
842 sizeof(*host->dev_timings), GFP_KERNEL);
843 if (!host->dev_timings)
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200844 return -ENOMEM;
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100845 ret = of_property_read_u8_array(np, "timings", (u8 *)host->dev_timings,
846 sizeof(*host->dev_timings));
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200847 if (ret)
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100848 host->dev_timings = NULL;
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200849
850 /* Set default NAND bank to 0 */
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100851 host->bank = 0;
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200852 if (!of_property_read_u32(np, "bank", &val)) {
853 if (val > 3) {
854 dev_err(&pdev->dev, "invalid bank %u\n", val);
855 return -EINVAL;
856 }
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100857 host->bank = val;
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200858 }
Stefan Roeseeea62812012-03-16 10:19:31 +0100859 return 0;
860}
Stefan Roeseeea62812012-03-16 10:19:31 +0100861
Linus Walleij6c009ab2010-09-13 00:35:22 +0200862/*
863 * fsmc_nand_probe - Probe function
864 * @pdev: platform device structure
865 */
866static int __init fsmc_nand_probe(struct platform_device *pdev)
867{
Linus Walleij6c009ab2010-09-13 00:35:22 +0200868 struct fsmc_nand_data *host;
869 struct mtd_info *mtd;
870 struct nand_chip *nand;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200871 struct resource *res;
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100872 void __iomem *base;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530873 dma_cap_mask_t mask;
Linus Walleij4ad916b2010-11-29 13:52:06 +0100874 int ret = 0;
Linus Walleij593cd872010-11-29 13:52:19 +0100875 u32 pid;
876 int i;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200877
Linus Walleij6c009ab2010-09-13 00:35:22 +0200878 /* Allocate memory for the device structure (and zero it) */
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530879 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Jingoo Hand9a21ae2013-12-26 12:16:38 +0900880 if (!host)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200881 return -ENOMEM;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200882
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100883 nand = &host->nand;
884
885 ret = fsmc_nand_probe_config_dt(pdev, host, nand);
886 if (ret)
887 return ret;
888
Linus Walleij6c009ab2010-09-13 00:35:22 +0200889 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
Thierry Redingb0de7742013-01-21 11:09:12 +0100890 host->data_va = devm_ioremap_resource(&pdev->dev, res);
891 if (IS_ERR(host->data_va))
892 return PTR_ERR(host->data_va);
Stefan Roesecbf29b82015-10-02 12:40:20 +0200893
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +0200894 host->data_pa = (dma_addr_t)res->start;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200895
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +0200896 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
Thierry Redingb0de7742013-01-21 11:09:12 +0100897 host->addr_va = devm_ioremap_resource(&pdev->dev, res);
898 if (IS_ERR(host->addr_va))
899 return PTR_ERR(host->addr_va);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200900
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +0200901 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
Thierry Redingb0de7742013-01-21 11:09:12 +0100902 host->cmd_va = devm_ioremap_resource(&pdev->dev, res);
903 if (IS_ERR(host->cmd_va))
904 return PTR_ERR(host->cmd_va);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200905
906 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100907 base = devm_ioremap_resource(&pdev->dev, res);
908 if (IS_ERR(base))
909 return PTR_ERR(base);
910
911 host->regs_va = base + FSMC_NOR_REG_SIZE +
912 (host->bank * FSMC_NAND_BANK_SZ);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200913
Thomas Petazzonifb8ed2c2017-03-21 11:04:03 +0100914 host->clk = devm_clk_get(&pdev->dev, NULL);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200915 if (IS_ERR(host->clk)) {
916 dev_err(&pdev->dev, "failed to fetch block clock\n");
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530917 return PTR_ERR(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200918 }
919
Viresh Kumare25da1c2012-04-17 17:07:57 +0530920 ret = clk_prepare_enable(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200921 if (ret)
Thomas Petazzonifb8ed2c2017-03-21 11:04:03 +0100922 return ret;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200923
Linus Walleij593cd872010-11-29 13:52:19 +0100924 /*
925 * This device ID is actually a common AMBA ID as used on the
926 * AMBA PrimeCell bus. However it is not a PrimeCell.
927 */
928 for (pid = 0, i = 0; i < 4; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100929 pid |= (readl(base + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
Linus Walleij593cd872010-11-29 13:52:19 +0100930 host->pid = pid;
931 dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
932 "revision %02x, config %02x\n",
933 AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
934 AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
935
Vipin Kumar712c4ad2012-03-14 11:47:16 +0530936 host->dev = &pdev->dev;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530937
938 if (host->mode == USE_DMA_ACCESS)
939 init_completion(&host->dma_access_complete);
940
Linus Walleij6c009ab2010-09-13 00:35:22 +0200941 /* Link all private pointers */
Boris BREZILLONbdf3a552015-12-10 09:00:05 +0100942 mtd = nand_to_mtd(&host->nand);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100943 nand_set_controller_data(nand, host);
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100944 nand_set_flash_node(nand, pdev->dev.of_node);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200945
Boris BREZILLONbdf3a552015-12-10 09:00:05 +0100946 mtd->dev.parent = &pdev->dev;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200947 nand->IO_ADDR_R = host->data_va;
948 nand->IO_ADDR_W = host->data_va;
949 nand->cmd_ctrl = fsmc_cmd_ctrl;
950 nand->chip_delay = 30;
951
Stefan Roesee278fc72015-10-19 08:40:13 +0200952 /*
953 * Setup default ECC mode. nand_dt_init() called from nand_scan_ident()
954 * can overwrite this value if the DT provides a different value.
955 */
Linus Walleij6c009ab2010-09-13 00:35:22 +0200956 nand->ecc.mode = NAND_ECC_HW;
957 nand->ecc.hwctl = fsmc_enable_hwecc;
958 nand->ecc.size = 512;
Vipin Kumar467e6e72012-03-14 11:47:12 +0530959 nand->badblockbits = 7;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200960
Vipin Kumar4774fb02012-03-14 11:47:18 +0530961 switch (host->mode) {
962 case USE_DMA_ACCESS:
963 dma_cap_zero(mask);
964 dma_cap_set(DMA_MEMCPY, mask);
Thomas Petazzonifeb1e572017-03-21 11:03:59 +0100965 host->read_dma_chan = dma_request_channel(mask, filter, NULL);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530966 if (!host->read_dma_chan) {
967 dev_err(&pdev->dev, "Unable to get read dma channel\n");
968 goto err_req_read_chnl;
969 }
Thomas Petazzonifeb1e572017-03-21 11:03:59 +0100970 host->write_dma_chan = dma_request_channel(mask, filter, NULL);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530971 if (!host->write_dma_chan) {
972 dev_err(&pdev->dev, "Unable to get write dma channel\n");
973 goto err_req_write_chnl;
974 }
975 nand->read_buf = fsmc_read_buf_dma;
976 nand->write_buf = fsmc_write_buf_dma;
977 break;
978
979 default:
980 case USE_WORD_ACCESS:
Vipin Kumar604e7542012-03-14 11:47:17 +0530981 nand->read_buf = fsmc_read_buf;
982 nand->write_buf = fsmc_write_buf;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530983 break;
Vipin Kumar604e7542012-03-14 11:47:17 +0530984 }
985
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200986 if (host->dev_timings)
987 fsmc_nand_setup(host, host->dev_timings);
988 else
989 nand->setup_data_interface = fsmc_setup_data_interface;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200990
Linus Walleij593cd872010-11-29 13:52:19 +0100991 if (AMBA_REV_BITS(host->pid) >= 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200992 nand->ecc.read_page = fsmc_read_page_hwecc;
993 nand->ecc.calculate = fsmc_read_hwecc_ecc4;
Armando Visconti753e0132012-03-07 17:00:54 +0530994 nand->ecc.correct = fsmc_bch8_correct_data;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200995 nand->ecc.bytes = 13;
Mike Dunn6a918ba2012-03-11 14:21:11 -0700996 nand->ecc.strength = 8;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200997 }
998
999 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001000 * Scan to find existence of the device
Linus Walleij6c009ab2010-09-13 00:35:22 +02001001 */
Masahiro Yamadaad5678e2016-11-04 19:43:00 +09001002 ret = nand_scan_ident(mtd, 1, NULL);
1003 if (ret) {
Linus Walleij6c009ab2010-09-13 00:35:22 +02001004 dev_err(&pdev->dev, "No NAND Device found!\n");
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301005 goto err_scan_ident;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001006 }
1007
Linus Walleij593cd872010-11-29 13:52:19 +01001008 if (AMBA_REV_BITS(host->pid) >= 8) {
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001009 switch (mtd->oobsize) {
Bhavna Yadave29ee572012-03-07 17:00:50 +05301010 case 16:
Bhavna Yadave29ee572012-03-07 17:00:50 +05301011 case 64:
Bhavna Yadave29ee572012-03-07 17:00:50 +05301012 case 128:
Armando Visconti0c78e932012-03-07 17:00:55 +05301013 case 224:
Bhavna Yadave29ee572012-03-07 17:00:50 +05301014 case 256:
Bhavna Yadave29ee572012-03-07 17:00:50 +05301015 break;
1016 default:
Jingoo Han67b19a62013-12-26 12:31:25 +09001017 dev_warn(&pdev->dev, "No oob scheme defined for oobsize %d\n",
1018 mtd->oobsize);
Stefan Roese6efadcf2015-10-02 12:40:21 +02001019 ret = -EINVAL;
1020 goto err_probe;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001021 }
Boris Brezillon22b46952016-02-03 20:01:42 +01001022
1023 mtd_set_ooblayout(mtd, &fsmc_ecc4_ooblayout_ops);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001024 } else {
Stefan Roesee278fc72015-10-19 08:40:13 +02001025 switch (nand->ecc.mode) {
1026 case NAND_ECC_HW:
1027 dev_info(&pdev->dev, "Using 1-bit HW ECC scheme\n");
1028 nand->ecc.calculate = fsmc_read_hwecc_ecc1;
1029 nand->ecc.correct = nand_correct_data;
1030 nand->ecc.bytes = 3;
1031 nand->ecc.strength = 1;
Bhavna Yadave29ee572012-03-07 17:00:50 +05301032 break;
Stefan Roesee278fc72015-10-19 08:40:13 +02001033
Rafał Miłeckief296dc2016-04-17 22:53:04 +02001034 case NAND_ECC_SOFT:
Rafał Miłeckief296dc2016-04-17 22:53:04 +02001035 if (nand->ecc.algo == NAND_ECC_BCH) {
1036 dev_info(&pdev->dev, "Using 4-bit SW BCH ECC scheme\n");
1037 break;
1038 }
Stefan Roesee278fc72015-10-19 08:40:13 +02001039
Thomas Petazzoni838ff7b2017-04-29 11:06:46 +02001040 case NAND_ECC_ON_DIE:
1041 break;
1042
Bhavna Yadave29ee572012-03-07 17:00:50 +05301043 default:
Stefan Roesee278fc72015-10-19 08:40:13 +02001044 dev_err(&pdev->dev, "Unsupported ECC mode!\n");
Stefan Roese6efadcf2015-10-02 12:40:21 +02001045 goto err_probe;
Bhavna Yadave29ee572012-03-07 17:00:50 +05301046 }
Stefan Roesee278fc72015-10-19 08:40:13 +02001047
1048 /*
1049 * Don't set layout for BCH4 SW ECC. This will be
1050 * generated later in nand_bch_init() later.
1051 */
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02001052 if (nand->ecc.mode == NAND_ECC_HW) {
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001053 switch (mtd->oobsize) {
Stefan Roesee278fc72015-10-19 08:40:13 +02001054 case 16:
Stefan Roesee278fc72015-10-19 08:40:13 +02001055 case 64:
Stefan Roesee278fc72015-10-19 08:40:13 +02001056 case 128:
Boris Brezillon22b46952016-02-03 20:01:42 +01001057 mtd_set_ooblayout(mtd,
1058 &fsmc_ecc1_ooblayout_ops);
Stefan Roesee278fc72015-10-19 08:40:13 +02001059 break;
1060 default:
1061 dev_warn(&pdev->dev,
1062 "No oob scheme defined for oobsize %d\n",
1063 mtd->oobsize);
1064 ret = -EINVAL;
1065 goto err_probe;
1066 }
1067 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001068 }
1069
1070 /* Second stage of scan to fill MTD data-structures */
Masahiro Yamadaad5678e2016-11-04 19:43:00 +09001071 ret = nand_scan_tail(mtd);
1072 if (ret)
Linus Walleij6c009ab2010-09-13 00:35:22 +02001073 goto err_probe;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001074
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001075 mtd->name = "nand";
Thomas Petazzoniede29a02017-03-21 11:04:00 +01001076 ret = mtd_device_register(mtd, NULL, 0);
Jamie Iles99335d02011-05-23 10:23:23 +01001077 if (ret)
Linus Walleij6c009ab2010-09-13 00:35:22 +02001078 goto err_probe;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001079
1080 platform_set_drvdata(pdev, host);
1081 dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
1082 return 0;
1083
1084err_probe:
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301085err_scan_ident:
Vipin Kumar4774fb02012-03-14 11:47:18 +05301086 if (host->mode == USE_DMA_ACCESS)
1087 dma_release_channel(host->write_dma_chan);
1088err_req_write_chnl:
1089 if (host->mode == USE_DMA_ACCESS)
1090 dma_release_channel(host->read_dma_chan);
1091err_req_read_chnl:
Viresh Kumare25da1c2012-04-17 17:07:57 +05301092 clk_disable_unprepare(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001093 return ret;
1094}
1095
1096/*
1097 * Clean up routine
1098 */
1099static int fsmc_nand_remove(struct platform_device *pdev)
1100{
1101 struct fsmc_nand_data *host = platform_get_drvdata(pdev);
1102
Linus Walleij6c009ab2010-09-13 00:35:22 +02001103 if (host) {
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001104 nand_release(nand_to_mtd(&host->nand));
Vipin Kumar4774fb02012-03-14 11:47:18 +05301105
1106 if (host->mode == USE_DMA_ACCESS) {
1107 dma_release_channel(host->write_dma_chan);
1108 dma_release_channel(host->read_dma_chan);
1109 }
Viresh Kumare25da1c2012-04-17 17:07:57 +05301110 clk_disable_unprepare(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001111 }
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301112
Linus Walleij6c009ab2010-09-13 00:35:22 +02001113 return 0;
1114}
1115
Jingoo Han80ce4dd2013-03-26 15:53:48 +09001116#ifdef CONFIG_PM_SLEEP
Linus Walleij6c009ab2010-09-13 00:35:22 +02001117static int fsmc_nand_suspend(struct device *dev)
1118{
1119 struct fsmc_nand_data *host = dev_get_drvdata(dev);
1120 if (host)
Viresh Kumare25da1c2012-04-17 17:07:57 +05301121 clk_disable_unprepare(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001122 return 0;
1123}
1124
1125static int fsmc_nand_resume(struct device *dev)
1126{
1127 struct fsmc_nand_data *host = dev_get_drvdata(dev);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301128 if (host) {
Viresh Kumare25da1c2012-04-17 17:07:57 +05301129 clk_prepare_enable(host->clk);
Thomas Petazzonid9fb0792017-04-29 10:52:35 +02001130 if (host->dev_timings)
1131 fsmc_nand_setup(host, host->dev_timings);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301132 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001133 return 0;
1134}
Jingoo Han80ce4dd2013-03-26 15:53:48 +09001135#endif
Linus Walleij6c009ab2010-09-13 00:35:22 +02001136
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301137static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001138
Stefan Roeseeea62812012-03-16 10:19:31 +01001139static const struct of_device_id fsmc_nand_id_table[] = {
1140 { .compatible = "st,spear600-fsmc-nand" },
Linus Walleijba785202013-01-05 22:28:32 +01001141 { .compatible = "stericsson,fsmc-nand" },
Stefan Roeseeea62812012-03-16 10:19:31 +01001142 {}
1143};
1144MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);
Stefan Roeseeea62812012-03-16 10:19:31 +01001145
Linus Walleij6c009ab2010-09-13 00:35:22 +02001146static struct platform_driver fsmc_nand_driver = {
1147 .remove = fsmc_nand_remove,
1148 .driver = {
Linus Walleij6c009ab2010-09-13 00:35:22 +02001149 .name = "fsmc-nand",
Thomas Petazzoni33575b22017-03-21 11:04:05 +01001150 .of_match_table = fsmc_nand_id_table,
Linus Walleij6c009ab2010-09-13 00:35:22 +02001151 .pm = &fsmc_nand_pm_ops,
Linus Walleij6c009ab2010-09-13 00:35:22 +02001152 },
1153};
1154
Jingoo Han307d2a512013-03-05 13:30:36 +09001155module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001156
1157MODULE_LICENSE("GPL");
1158MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
1159MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");