blob: a4a5da318eed56a8b0ae3c5cb1671c6bede097ee [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)
Linus Walleij6c009ab2010-09-13 00:35:22 +020011 *
12 * This file is licensed under the terms of the GNU General Public
13 * License version 2. This program is licensed "as is" without any
14 * warranty of any kind, whether express or implied.
15 */
16
17#include <linux/clk.h>
Vipin Kumar4774fb02012-03-14 11:47:18 +053018#include <linux/completion.h>
19#include <linux/dmaengine.h>
20#include <linux/dma-direction.h>
21#include <linux/dma-mapping.h>
Linus Walleij6c009ab2010-09-13 00:35:22 +020022#include <linux/err.h>
23#include <linux/init.h>
24#include <linux/module.h>
25#include <linux/resource.h>
26#include <linux/sched.h>
27#include <linux/types.h>
28#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020029#include <linux/mtd/rawnand.h>
Linus Walleij6c009ab2010-09-13 00:35:22 +020030#include <linux/mtd/nand_ecc.h>
31#include <linux/platform_device.h>
Stefan Roeseeea62812012-03-16 10:19:31 +010032#include <linux/of.h>
Linus Walleij6c009ab2010-09-13 00:35:22 +020033#include <linux/mtd/partitions.h>
34#include <linux/io.h>
35#include <linux/slab.h>
Linus Walleij593cd872010-11-29 13:52:19 +010036#include <linux/amba/bus.h>
Linus Walleij6c009ab2010-09-13 00:35:22 +020037#include <mtd/mtd-abi.h>
38
Linus Walleij4404d7d2016-12-18 12:34:55 +010039/* fsmc controller registers for NOR flash */
40#define CTRL 0x0
41 /* ctrl register definitions */
42 #define BANK_ENABLE (1 << 0)
43 #define MUXED (1 << 1)
44 #define NOR_DEV (2 << 2)
45 #define WIDTH_8 (0 << 4)
46 #define WIDTH_16 (1 << 4)
47 #define RSTPWRDWN (1 << 6)
48 #define WPROT (1 << 7)
49 #define WRT_ENABLE (1 << 12)
50 #define WAIT_ENB (1 << 13)
51
52#define CTRL_TIM 0x4
53 /* ctrl_tim register definitions */
54
55#define FSMC_NOR_BANK_SZ 0x8
56#define FSMC_NOR_REG_SIZE 0x40
57
58#define FSMC_NOR_REG(base, bank, reg) (base + \
59 FSMC_NOR_BANK_SZ * (bank) + \
60 reg)
61
62/* fsmc controller registers for NAND flash */
63#define PC 0x00
64 /* pc register definitions */
65 #define FSMC_RESET (1 << 0)
66 #define FSMC_WAITON (1 << 1)
67 #define FSMC_ENABLE (1 << 2)
68 #define FSMC_DEVTYPE_NAND (1 << 3)
69 #define FSMC_DEVWID_8 (0 << 4)
70 #define FSMC_DEVWID_16 (1 << 4)
71 #define FSMC_ECCEN (1 << 6)
72 #define FSMC_ECCPLEN_512 (0 << 7)
73 #define FSMC_ECCPLEN_256 (1 << 7)
74 #define FSMC_TCLR_1 (1)
75 #define FSMC_TCLR_SHIFT (9)
76 #define FSMC_TCLR_MASK (0xF)
77 #define FSMC_TAR_1 (1)
78 #define FSMC_TAR_SHIFT (13)
79 #define FSMC_TAR_MASK (0xF)
80#define STS 0x04
81 /* sts register definitions */
82 #define FSMC_CODE_RDY (1 << 15)
83#define COMM 0x08
84 /* comm register definitions */
85 #define FSMC_TSET_0 0
86 #define FSMC_TSET_SHIFT 0
87 #define FSMC_TSET_MASK 0xFF
88 #define FSMC_TWAIT_6 6
89 #define FSMC_TWAIT_SHIFT 8
90 #define FSMC_TWAIT_MASK 0xFF
91 #define FSMC_THOLD_4 4
92 #define FSMC_THOLD_SHIFT 16
93 #define FSMC_THOLD_MASK 0xFF
94 #define FSMC_THIZ_1 1
95 #define FSMC_THIZ_SHIFT 24
96 #define FSMC_THIZ_MASK 0xFF
97#define ATTRIB 0x0C
98#define IOATA 0x10
99#define ECC1 0x14
100#define ECC2 0x18
101#define ECC3 0x1C
102#define FSMC_NAND_BANK_SZ 0x20
103
104#define FSMC_NAND_REG(base, bank, reg) (base + FSMC_NOR_REG_SIZE + \
105 (FSMC_NAND_BANK_SZ * (bank)) + \
106 reg)
107
108#define FSMC_BUSY_WAIT_TIMEOUT (1 * HZ)
109
110struct fsmc_nand_timings {
111 uint8_t tclr;
112 uint8_t tar;
113 uint8_t thiz;
114 uint8_t thold;
115 uint8_t twait;
116 uint8_t tset;
117};
118
119enum access_mode {
120 USE_DMA_ACCESS = 1,
121 USE_WORD_ACCESS,
122};
123
124/**
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100125 * struct fsmc_nand_data - structure for FSMC NAND device state
126 *
127 * @pid: Part ID on the AMBA PrimeCell format
128 * @mtd: MTD info for a NAND flash.
129 * @nand: Chip related info for a NAND flash.
130 * @partitions: Partition info for a NAND Flash.
131 * @nr_partitions: Total number of partition of a NAND flash.
132 *
133 * @bank: Bank number for probed device.
134 * @clk: Clock structure for FSMC.
135 *
136 * @read_dma_chan: DMA channel for read access
137 * @write_dma_chan: DMA channel for write access to NAND
138 * @dma_access_complete: Completion structure
139 *
140 * @data_pa: NAND Physical port for Data.
141 * @data_va: NAND port for Data.
142 * @cmd_va: NAND port for Command.
143 * @addr_va: NAND port for Address.
144 * @regs_va: FSMC regs base address.
145 */
146struct fsmc_nand_data {
147 u32 pid;
148 struct nand_chip nand;
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100149
150 unsigned int bank;
151 struct device *dev;
152 enum access_mode mode;
153 struct clk *clk;
154
155 /* DMA related objects */
156 struct dma_chan *read_dma_chan;
157 struct dma_chan *write_dma_chan;
158 struct completion dma_access_complete;
159
160 struct fsmc_nand_timings *dev_timings;
161
162 dma_addr_t data_pa;
163 void __iomem *data_va;
164 void __iomem *cmd_va;
165 void __iomem *addr_va;
166 void __iomem *regs_va;
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100167};
168
Boris Brezillon22b46952016-02-03 20:01:42 +0100169static int fsmc_ecc1_ooblayout_ecc(struct mtd_info *mtd, int section,
170 struct mtd_oob_region *oobregion)
171{
172 struct nand_chip *chip = mtd_to_nand(mtd);
173
174 if (section >= chip->ecc.steps)
175 return -ERANGE;
176
177 oobregion->offset = (section * 16) + 2;
178 oobregion->length = 3;
179
180 return 0;
181}
182
183static int fsmc_ecc1_ooblayout_free(struct mtd_info *mtd, int section,
184 struct mtd_oob_region *oobregion)
185{
186 struct nand_chip *chip = mtd_to_nand(mtd);
187
188 if (section >= chip->ecc.steps)
189 return -ERANGE;
190
191 oobregion->offset = (section * 16) + 8;
192
193 if (section < chip->ecc.steps - 1)
194 oobregion->length = 8;
195 else
196 oobregion->length = mtd->oobsize - oobregion->offset;
197
198 return 0;
199}
200
201static const struct mtd_ooblayout_ops fsmc_ecc1_ooblayout_ops = {
202 .ecc = fsmc_ecc1_ooblayout_ecc,
203 .free = fsmc_ecc1_ooblayout_free,
204};
205
Boris Brezillon04a123a2016-02-09 15:01:21 +0100206/*
207 * ECC placement definitions in oobfree type format.
208 * There are 13 bytes of ecc for every 512 byte block and it has to be read
209 * consecutively and immediately after the 512 byte data block for hardware to
210 * generate the error bit offsets in 512 byte data.
211 */
Boris Brezillon22b46952016-02-03 20:01:42 +0100212static int fsmc_ecc4_ooblayout_ecc(struct mtd_info *mtd, int section,
213 struct mtd_oob_region *oobregion)
214{
215 struct nand_chip *chip = mtd_to_nand(mtd);
216
217 if (section >= chip->ecc.steps)
218 return -ERANGE;
219
220 oobregion->length = chip->ecc.bytes;
221
222 if (!section && mtd->writesize <= 512)
223 oobregion->offset = 0;
224 else
225 oobregion->offset = (section * 16) + 2;
226
227 return 0;
228}
229
230static int fsmc_ecc4_ooblayout_free(struct mtd_info *mtd, int section,
231 struct mtd_oob_region *oobregion)
232{
233 struct nand_chip *chip = mtd_to_nand(mtd);
234
235 if (section >= chip->ecc.steps)
236 return -ERANGE;
237
238 oobregion->offset = (section * 16) + 15;
239
240 if (section < chip->ecc.steps - 1)
241 oobregion->length = 3;
242 else
243 oobregion->length = mtd->oobsize - oobregion->offset;
244
245 return 0;
246}
247
248static const struct mtd_ooblayout_ops fsmc_ecc4_ooblayout_ops = {
249 .ecc = fsmc_ecc4_ooblayout_ecc,
250 .free = fsmc_ecc4_ooblayout_free,
251};
252
Boris BREZILLON277af422015-12-10 08:59:46 +0100253static inline struct fsmc_nand_data *mtd_to_fsmc(struct mtd_info *mtd)
254{
Boris BREZILLONbdf3a552015-12-10 09:00:05 +0100255 return container_of(mtd_to_nand(mtd), struct fsmc_nand_data, nand);
Boris BREZILLON277af422015-12-10 08:59:46 +0100256}
257
Linus Walleij6c009ab2010-09-13 00:35:22 +0200258/*
259 * fsmc_cmd_ctrl - For facilitaing Hardware access
260 * This routine allows hardware specific access to control-lines(ALE,CLE)
261 */
262static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
263{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100264 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLON277af422015-12-10 08:59:46 +0100265 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar605add72012-10-09 16:14:43 +0530266 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200267 unsigned int bank = host->bank;
268
269 if (ctrl & NAND_CTRL_CHANGE) {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530270 u32 pc;
271
Linus Walleij6c009ab2010-09-13 00:35:22 +0200272 if (ctrl & NAND_CLE) {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530273 this->IO_ADDR_R = host->cmd_va;
274 this->IO_ADDR_W = host->cmd_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200275 } else if (ctrl & NAND_ALE) {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530276 this->IO_ADDR_R = host->addr_va;
277 this->IO_ADDR_W = host->addr_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200278 } else {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530279 this->IO_ADDR_R = host->data_va;
280 this->IO_ADDR_W = host->data_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200281 }
282
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530283 pc = readl(FSMC_NAND_REG(regs, bank, PC));
284 if (ctrl & NAND_NCE)
285 pc |= FSMC_ENABLE;
286 else
287 pc &= ~FSMC_ENABLE;
Vipin Kumara4742d52012-10-09 16:14:50 +0530288 writel_relaxed(pc, FSMC_NAND_REG(regs, bank, PC));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200289 }
290
291 mb();
292
293 if (cmd != NAND_CMD_NONE)
Vipin Kumara4742d52012-10-09 16:14:50 +0530294 writeb_relaxed(cmd, this->IO_ADDR_W);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200295}
296
297/*
298 * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
299 *
300 * This routine initializes timing parameters related to NAND memory access in
301 * FSMC registers
302 */
Thomas Petazzoni6335b502017-04-29 10:52:34 +0200303static void fsmc_nand_setup(struct fsmc_nand_data *host,
Thomas Petazzoni1debdb92017-04-29 10:52:36 +0200304 struct fsmc_nand_timings *tims)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200305{
306 uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530307 uint32_t tclr, tar, thiz, thold, twait, tset;
Thomas Petazzoni6335b502017-04-29 10:52:34 +0200308 unsigned int bank = host->bank;
309 void __iomem *regs = host->regs_va;
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530310
311 tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT;
312 tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT;
313 thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT;
314 thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT;
315 twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT;
316 tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200317
Thomas Petazzoni6335b502017-04-29 10:52:34 +0200318 if (host->nand.options & NAND_BUSWIDTH_16)
Vipin Kumara4742d52012-10-09 16:14:50 +0530319 writel_relaxed(value | FSMC_DEVWID_16,
320 FSMC_NAND_REG(regs, bank, PC));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200321 else
Vipin Kumara4742d52012-10-09 16:14:50 +0530322 writel_relaxed(value | FSMC_DEVWID_8,
323 FSMC_NAND_REG(regs, bank, PC));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200324
Vipin Kumara4742d52012-10-09 16:14:50 +0530325 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) | tclr | tar,
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530326 FSMC_NAND_REG(regs, bank, PC));
Vipin Kumara4742d52012-10-09 16:14:50 +0530327 writel_relaxed(thiz | thold | twait | tset,
328 FSMC_NAND_REG(regs, bank, COMM));
329 writel_relaxed(thiz | thold | twait | tset,
330 FSMC_NAND_REG(regs, bank, ATTRIB));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200331}
332
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200333static int fsmc_calc_timings(struct fsmc_nand_data *host,
334 const struct nand_sdr_timings *sdrt,
335 struct fsmc_nand_timings *tims)
336{
337 unsigned long hclk = clk_get_rate(host->clk);
338 unsigned long hclkn = NSEC_PER_SEC / hclk;
339 uint32_t thiz, thold, twait, tset;
340
341 if (sdrt->tRC_min < 30000)
342 return -EOPNOTSUPP;
343
344 tims->tar = DIV_ROUND_UP(sdrt->tAR_min / 1000, hclkn) - 1;
345 if (tims->tar > FSMC_TAR_MASK)
346 tims->tar = FSMC_TAR_MASK;
347 tims->tclr = DIV_ROUND_UP(sdrt->tCLR_min / 1000, hclkn) - 1;
348 if (tims->tclr > FSMC_TCLR_MASK)
349 tims->tclr = FSMC_TCLR_MASK;
350
351 thiz = sdrt->tCS_min - sdrt->tWP_min;
352 tims->thiz = DIV_ROUND_UP(thiz / 1000, hclkn);
353
354 thold = sdrt->tDH_min;
355 if (thold < sdrt->tCH_min)
356 thold = sdrt->tCH_min;
357 if (thold < sdrt->tCLH_min)
358 thold = sdrt->tCLH_min;
359 if (thold < sdrt->tWH_min)
360 thold = sdrt->tWH_min;
361 if (thold < sdrt->tALH_min)
362 thold = sdrt->tALH_min;
363 if (thold < sdrt->tREH_min)
364 thold = sdrt->tREH_min;
365 tims->thold = DIV_ROUND_UP(thold / 1000, hclkn);
366 if (tims->thold == 0)
367 tims->thold = 1;
368 else if (tims->thold > FSMC_THOLD_MASK)
369 tims->thold = FSMC_THOLD_MASK;
370
371 twait = max(sdrt->tRP_min, sdrt->tWP_min);
372 tims->twait = DIV_ROUND_UP(twait / 1000, hclkn) - 1;
373 if (tims->twait == 0)
374 tims->twait = 1;
375 else if (tims->twait > FSMC_TWAIT_MASK)
376 tims->twait = FSMC_TWAIT_MASK;
377
378 tset = max(sdrt->tCS_min - sdrt->tWP_min,
379 sdrt->tCEA_max - sdrt->tREA_max);
380 tims->tset = DIV_ROUND_UP(tset / 1000, hclkn) - 1;
381 if (tims->tset == 0)
382 tims->tset = 1;
383 else if (tims->tset > FSMC_TSET_MASK)
384 tims->tset = FSMC_TSET_MASK;
385
386 return 0;
387}
388
Boris Brezillon104e4422017-03-16 09:35:58 +0100389static int fsmc_setup_data_interface(struct mtd_info *mtd, int csline,
390 const struct nand_data_interface *conf)
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200391{
392 struct nand_chip *nand = mtd_to_nand(mtd);
393 struct fsmc_nand_data *host = nand_get_controller_data(nand);
394 struct fsmc_nand_timings tims;
395 const struct nand_sdr_timings *sdrt;
396 int ret;
397
398 sdrt = nand_get_sdr_timings(conf);
399 if (IS_ERR(sdrt))
400 return PTR_ERR(sdrt);
401
402 ret = fsmc_calc_timings(host, sdrt, &tims);
403 if (ret)
404 return ret;
405
Boris Brezillon104e4422017-03-16 09:35:58 +0100406 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200407 return 0;
408
409 fsmc_nand_setup(host, &tims);
410
411 return 0;
412}
413
Linus Walleij6c009ab2010-09-13 00:35:22 +0200414/*
415 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
416 */
417static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
418{
Boris BREZILLON277af422015-12-10 08:59:46 +0100419 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530420 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200421 uint32_t bank = host->bank;
422
Vipin Kumara4742d52012-10-09 16:14:50 +0530423 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCPLEN_256,
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530424 FSMC_NAND_REG(regs, bank, PC));
Vipin Kumara4742d52012-10-09 16:14:50 +0530425 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCEN,
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530426 FSMC_NAND_REG(regs, bank, PC));
Vipin Kumara4742d52012-10-09 16:14:50 +0530427 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) | FSMC_ECCEN,
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530428 FSMC_NAND_REG(regs, bank, PC));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200429}
430
431/*
432 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300433 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200434 * max of 8-bits)
435 */
436static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
437 uint8_t *ecc)
438{
Boris BREZILLON277af422015-12-10 08:59:46 +0100439 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530440 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200441 uint32_t bank = host->bank;
442 uint32_t ecc_tmp;
443 unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
444
445 do {
Vipin Kumara4742d52012-10-09 16:14:50 +0530446 if (readl_relaxed(FSMC_NAND_REG(regs, bank, STS)) & FSMC_CODE_RDY)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200447 break;
448 else
449 cond_resched();
450 } while (!time_after_eq(jiffies, deadline));
451
Vipin Kumar712c4ad2012-03-14 11:47:16 +0530452 if (time_after_eq(jiffies, deadline)) {
453 dev_err(host->dev, "calculate ecc timed out\n");
454 return -ETIMEDOUT;
455 }
456
Vipin Kumara4742d52012-10-09 16:14:50 +0530457 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200458 ecc[0] = (uint8_t) (ecc_tmp >> 0);
459 ecc[1] = (uint8_t) (ecc_tmp >> 8);
460 ecc[2] = (uint8_t) (ecc_tmp >> 16);
461 ecc[3] = (uint8_t) (ecc_tmp >> 24);
462
Vipin Kumara4742d52012-10-09 16:14:50 +0530463 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC2));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200464 ecc[4] = (uint8_t) (ecc_tmp >> 0);
465 ecc[5] = (uint8_t) (ecc_tmp >> 8);
466 ecc[6] = (uint8_t) (ecc_tmp >> 16);
467 ecc[7] = (uint8_t) (ecc_tmp >> 24);
468
Vipin Kumara4742d52012-10-09 16:14:50 +0530469 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC3));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200470 ecc[8] = (uint8_t) (ecc_tmp >> 0);
471 ecc[9] = (uint8_t) (ecc_tmp >> 8);
472 ecc[10] = (uint8_t) (ecc_tmp >> 16);
473 ecc[11] = (uint8_t) (ecc_tmp >> 24);
474
Vipin Kumara4742d52012-10-09 16:14:50 +0530475 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, STS));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200476 ecc[12] = (uint8_t) (ecc_tmp >> 16);
477
478 return 0;
479}
480
481/*
482 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300483 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200484 * max of 1-bit)
485 */
486static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
487 uint8_t *ecc)
488{
Boris BREZILLON277af422015-12-10 08:59:46 +0100489 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530490 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200491 uint32_t bank = host->bank;
492 uint32_t ecc_tmp;
493
Vipin Kumara4742d52012-10-09 16:14:50 +0530494 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200495 ecc[0] = (uint8_t) (ecc_tmp >> 0);
496 ecc[1] = (uint8_t) (ecc_tmp >> 8);
497 ecc[2] = (uint8_t) (ecc_tmp >> 16);
498
499 return 0;
500}
501
Vipin Kumar519300c2012-03-07 17:00:49 +0530502/* Count the number of 0's in buff upto a max of max_bits */
503static int count_written_bits(uint8_t *buff, int size, int max_bits)
504{
505 int k, written_bits = 0;
506
507 for (k = 0; k < size; k++) {
508 written_bits += hweight8(~buff[k]);
509 if (written_bits > max_bits)
510 break;
511 }
512
513 return written_bits;
514}
515
Vipin Kumar4774fb02012-03-14 11:47:18 +0530516static void dma_complete(void *param)
517{
518 struct fsmc_nand_data *host = param;
519
520 complete(&host->dma_access_complete);
521}
522
523static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len,
524 enum dma_data_direction direction)
525{
526 struct dma_chan *chan;
527 struct dma_device *dma_dev;
528 struct dma_async_tx_descriptor *tx;
529 dma_addr_t dma_dst, dma_src, dma_addr;
530 dma_cookie_t cookie;
531 unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
532 int ret;
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400533 unsigned long time_left;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530534
535 if (direction == DMA_TO_DEVICE)
536 chan = host->write_dma_chan;
537 else if (direction == DMA_FROM_DEVICE)
538 chan = host->read_dma_chan;
539 else
540 return -EINVAL;
541
542 dma_dev = chan->device;
543 dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction);
544
545 if (direction == DMA_TO_DEVICE) {
546 dma_src = dma_addr;
547 dma_dst = host->data_pa;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530548 } else {
549 dma_src = host->data_pa;
550 dma_dst = dma_addr;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530551 }
552
553 tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
554 len, flags);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530555 if (!tx) {
556 dev_err(host->dev, "device_prep_dma_memcpy error\n");
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000557 ret = -EIO;
558 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530559 }
560
561 tx->callback = dma_complete;
562 tx->callback_param = host;
563 cookie = tx->tx_submit(tx);
564
565 ret = dma_submit_error(cookie);
566 if (ret) {
567 dev_err(host->dev, "dma_submit_error %d\n", cookie);
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000568 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530569 }
570
571 dma_async_issue_pending(chan);
572
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400573 time_left =
Vipin Kumar928aa2a2012-10-09 16:14:48 +0530574 wait_for_completion_timeout(&host->dma_access_complete,
Vipin Kumar4774fb02012-03-14 11:47:18 +0530575 msecs_to_jiffies(3000));
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400576 if (time_left == 0) {
Vinod Koulb177ea32014-10-11 21:10:32 +0530577 dmaengine_terminate_all(chan);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530578 dev_err(host->dev, "wait_for_completion_timeout\n");
Nicholas Mc Guire0bda3e12015-03-13 07:54:45 -0400579 ret = -ETIMEDOUT;
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000580 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530581 }
582
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000583 ret = 0;
584
585unmap_dma:
586 dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
587
588 return ret;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530589}
590
Linus Walleij6c009ab2010-09-13 00:35:22 +0200591/*
Vipin Kumar604e7542012-03-14 11:47:17 +0530592 * fsmc_write_buf - write buffer to chip
593 * @mtd: MTD device structure
594 * @buf: data buffer
595 * @len: number of bytes to write
596 */
597static void fsmc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
598{
599 int i;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100600 struct nand_chip *chip = mtd_to_nand(mtd);
Vipin Kumar604e7542012-03-14 11:47:17 +0530601
602 if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
603 IS_ALIGNED(len, sizeof(uint32_t))) {
604 uint32_t *p = (uint32_t *)buf;
605 len = len >> 2;
606 for (i = 0; i < len; i++)
Vipin Kumara4742d52012-10-09 16:14:50 +0530607 writel_relaxed(p[i], chip->IO_ADDR_W);
Vipin Kumar604e7542012-03-14 11:47:17 +0530608 } else {
609 for (i = 0; i < len; i++)
Vipin Kumara4742d52012-10-09 16:14:50 +0530610 writeb_relaxed(buf[i], chip->IO_ADDR_W);
Vipin Kumar604e7542012-03-14 11:47:17 +0530611 }
612}
613
614/*
615 * fsmc_read_buf - read chip data into buffer
616 * @mtd: MTD device structure
617 * @buf: buffer to store date
618 * @len: number of bytes to read
619 */
620static void fsmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
621{
622 int i;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100623 struct nand_chip *chip = mtd_to_nand(mtd);
Vipin Kumar604e7542012-03-14 11:47:17 +0530624
625 if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
626 IS_ALIGNED(len, sizeof(uint32_t))) {
627 uint32_t *p = (uint32_t *)buf;
628 len = len >> 2;
629 for (i = 0; i < len; i++)
Vipin Kumara4742d52012-10-09 16:14:50 +0530630 p[i] = readl_relaxed(chip->IO_ADDR_R);
Vipin Kumar604e7542012-03-14 11:47:17 +0530631 } else {
632 for (i = 0; i < len; i++)
Vipin Kumara4742d52012-10-09 16:14:50 +0530633 buf[i] = readb_relaxed(chip->IO_ADDR_R);
Vipin Kumar604e7542012-03-14 11:47:17 +0530634 }
635}
636
637/*
Vipin Kumar4774fb02012-03-14 11:47:18 +0530638 * fsmc_read_buf_dma - read chip data into buffer
639 * @mtd: MTD device structure
640 * @buf: buffer to store date
641 * @len: number of bytes to read
642 */
643static void fsmc_read_buf_dma(struct mtd_info *mtd, uint8_t *buf, int len)
644{
Boris BREZILLON277af422015-12-10 08:59:46 +0100645 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530646
Vipin Kumar4774fb02012-03-14 11:47:18 +0530647 dma_xfer(host, buf, len, DMA_FROM_DEVICE);
648}
649
650/*
651 * fsmc_write_buf_dma - write buffer to chip
652 * @mtd: MTD device structure
653 * @buf: data buffer
654 * @len: number of bytes to write
655 */
656static void fsmc_write_buf_dma(struct mtd_info *mtd, const uint8_t *buf,
657 int len)
658{
Boris BREZILLON277af422015-12-10 08:59:46 +0100659 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530660
Vipin Kumar4774fb02012-03-14 11:47:18 +0530661 dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
662}
663
664/*
Linus Walleij6c009ab2010-09-13 00:35:22 +0200665 * fsmc_read_page_hwecc
666 * @mtd: mtd info structure
667 * @chip: nand chip info structure
668 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -0700669 * @oob_required: caller expects OOB data read to chip->oob_poi
Linus Walleij6c009ab2010-09-13 00:35:22 +0200670 * @page: page number to read
671 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300672 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
Linus Walleij6c009ab2010-09-13 00:35:22 +0200673 * performed in a strict sequence as follows:
674 * data(512 byte) -> ecc(13 byte)
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300675 * After this read, fsmc hardware generates and reports error data bits(up to a
Linus Walleij6c009ab2010-09-13 00:35:22 +0200676 * max of 8 bits)
677 */
678static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700679 uint8_t *buf, int oob_required, int page)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200680{
Linus Walleij6c009ab2010-09-13 00:35:22 +0200681 int i, j, s, stat, eccsize = chip->ecc.size;
682 int eccbytes = chip->ecc.bytes;
683 int eccsteps = chip->ecc.steps;
684 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +0900685 uint8_t *ecc_calc = chip->ecc.calc_buf;
686 uint8_t *ecc_code = chip->ecc.code_buf;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200687 int off, len, group = 0;
688 /*
689 * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
690 * end up reading 14 bytes (7 words) from oob. The local array is
691 * to maintain word alignment
692 */
693 uint16_t ecc_oob[7];
694 uint8_t *oob = (uint8_t *)&ecc_oob[0];
Mike Dunn3f91e942012-04-25 12:06:09 -0700695 unsigned int max_bitflips = 0;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200696
697 for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100698 nand_read_page_op(chip, page, s * eccsize, NULL, 0);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200699 chip->ecc.hwctl(mtd, NAND_ECC_READ);
700 chip->read_buf(mtd, p, eccsize);
701
702 for (j = 0; j < eccbytes;) {
Boris Brezillon04a123a2016-02-09 15:01:21 +0100703 struct mtd_oob_region oobregion;
704 int ret;
705
706 ret = mtd_ooblayout_ecc(mtd, group++, &oobregion);
707 if (ret)
708 return ret;
709
710 off = oobregion.offset;
711 len = oobregion.length;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200712
713 /*
Vipin Kumar4cbe1bf02012-03-14 11:47:09 +0530714 * length is intentionally kept a higher multiple of 2
715 * to read at least 13 bytes even in case of 16 bit NAND
716 * devices
717 */
Vipin Kumaraea686b2012-03-14 11:47:10 +0530718 if (chip->options & NAND_BUSWIDTH_16)
719 len = roundup(len, 2);
720
Boris Brezillon97d90da2017-11-30 18:01:29 +0100721 nand_read_oob_op(chip, page, off, oob + j, len);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200722 j += len;
723 }
724
Vipin Kumar519300c2012-03-07 17:00:49 +0530725 memcpy(&ecc_code[i], oob, chip->ecc.bytes);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200726 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
727
728 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -0700729 if (stat < 0) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200730 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -0700731 } else {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200732 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -0700733 max_bitflips = max_t(unsigned int, max_bitflips, stat);
734 }
Linus Walleij6c009ab2010-09-13 00:35:22 +0200735 }
736
Mike Dunn3f91e942012-04-25 12:06:09 -0700737 return max_bitflips;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200738}
739
740/*
Armando Visconti753e0132012-03-07 17:00:54 +0530741 * fsmc_bch8_correct_data
Linus Walleij6c009ab2010-09-13 00:35:22 +0200742 * @mtd: mtd info structure
743 * @dat: buffer of read data
744 * @read_ecc: ecc read from device spare area
745 * @calc_ecc: ecc calculated from read data
746 *
747 * calc_ecc is a 104 bit information containing maximum of 8 error
748 * offset informations of 13 bits each in 512 bytes of read data.
749 */
Armando Visconti753e0132012-03-07 17:00:54 +0530750static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
Linus Walleij6c009ab2010-09-13 00:35:22 +0200751 uint8_t *read_ecc, uint8_t *calc_ecc)
752{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100753 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLON277af422015-12-10 08:59:46 +0100754 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530755 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200756 unsigned int bank = host->bank;
Armando Viscontia612c2a2012-03-07 17:00:53 +0530757 uint32_t err_idx[8];
Linus Walleij6c009ab2010-09-13 00:35:22 +0200758 uint32_t num_err, i;
Armando Visconti753e0132012-03-07 17:00:54 +0530759 uint32_t ecc1, ecc2, ecc3, ecc4;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200760
Vipin Kumara4742d52012-10-09 16:14:50 +0530761 num_err = (readl_relaxed(FSMC_NAND_REG(regs, bank, STS)) >> 10) & 0xF;
Vipin Kumar519300c2012-03-07 17:00:49 +0530762
763 /* no bit flipping */
764 if (likely(num_err == 0))
765 return 0;
766
767 /* too many errors */
768 if (unlikely(num_err > 8)) {
769 /*
770 * This is a temporary erase check. A newly erased page read
771 * would result in an ecc error because the oob data is also
772 * erased to FF and the calculated ecc for an FF data is not
773 * FF..FF.
774 * This is a workaround to skip performing correction in case
775 * data is FF..FF
776 *
777 * Logic:
778 * For every page, each bit written as 0 is counted until these
779 * number of bits are greater than 8 (the maximum correction
780 * capability of FSMC for each 512 + 13 bytes)
781 */
782
783 int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
784 int bits_data = count_written_bits(dat, chip->ecc.size, 8);
785
786 if ((bits_ecc + bits_data) <= 8) {
787 if (bits_data)
788 memset(dat, 0xff, chip->ecc.size);
789 return bits_data;
790 }
791
792 return -EBADMSG;
793 }
794
Linus Walleij6c009ab2010-09-13 00:35:22 +0200795 /*
796 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
797 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
798 *
799 * calc_ecc is a 104 bit information containing maximum of 8 error
800 * offset informations of 13 bits each. calc_ecc is copied into a
801 * uint64_t array and error offset indexes are populated in err_idx
802 * array
803 */
Vipin Kumara4742d52012-10-09 16:14:50 +0530804 ecc1 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
805 ecc2 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC2));
806 ecc3 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC3));
807 ecc4 = readl_relaxed(FSMC_NAND_REG(regs, bank, STS));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200808
Armando Visconti753e0132012-03-07 17:00:54 +0530809 err_idx[0] = (ecc1 >> 0) & 0x1FFF;
810 err_idx[1] = (ecc1 >> 13) & 0x1FFF;
811 err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
812 err_idx[3] = (ecc2 >> 7) & 0x1FFF;
813 err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
814 err_idx[5] = (ecc3 >> 1) & 0x1FFF;
815 err_idx[6] = (ecc3 >> 14) & 0x1FFF;
816 err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200817
818 i = 0;
819 while (num_err--) {
820 change_bit(0, (unsigned long *)&err_idx[i]);
821 change_bit(1, (unsigned long *)&err_idx[i]);
822
Vipin Kumarb533f8d2012-03-14 11:47:11 +0530823 if (err_idx[i] < chip->ecc.size * 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200824 change_bit(err_idx[i], (unsigned long *)dat);
825 i++;
826 }
827 }
828 return i;
829}
830
Vipin Kumar4774fb02012-03-14 11:47:18 +0530831static bool filter(struct dma_chan *chan, void *slave)
832{
833 chan->private = slave;
834 return true;
835}
836
Bill Pemberton06f25512012-11-19 13:23:07 -0500837static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100838 struct fsmc_nand_data *host,
839 struct nand_chip *nand)
Stefan Roeseeea62812012-03-16 10:19:31 +0100840{
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100841 struct device_node *np = pdev->dev.of_node;
Stefan Roeseeea62812012-03-16 10:19:31 +0100842 u32 val;
Stefan Roese62b57f42015-03-19 14:34:29 +0100843 int ret;
Stefan Roeseeea62812012-03-16 10:19:31 +0100844
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100845 nand->options = 0;
Thomas Petazzoniee568742017-03-21 11:03:53 +0100846
Stefan Roeseeea62812012-03-16 10:19:31 +0100847 if (!of_property_read_u32(np, "bank-width", &val)) {
848 if (val == 2) {
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100849 nand->options |= NAND_BUSWIDTH_16;
Stefan Roeseeea62812012-03-16 10:19:31 +0100850 } else if (val != 1) {
851 dev_err(&pdev->dev, "invalid bank-width %u\n", val);
852 return -EINVAL;
853 }
854 }
Thomas Petazzoniee568742017-03-21 11:03:53 +0100855
Stefan Roeseeea62812012-03-16 10:19:31 +0100856 if (of_get_property(np, "nand-skip-bbtscan", NULL))
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100857 nand->options |= NAND_SKIP_BBTSCAN;
Stefan Roeseeea62812012-03-16 10:19:31 +0100858
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100859 host->dev_timings = devm_kzalloc(&pdev->dev,
860 sizeof(*host->dev_timings), GFP_KERNEL);
861 if (!host->dev_timings)
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200862 return -ENOMEM;
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100863 ret = of_property_read_u8_array(np, "timings", (u8 *)host->dev_timings,
864 sizeof(*host->dev_timings));
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200865 if (ret)
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100866 host->dev_timings = NULL;
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200867
868 /* Set default NAND bank to 0 */
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100869 host->bank = 0;
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200870 if (!of_property_read_u32(np, "bank", &val)) {
871 if (val > 3) {
872 dev_err(&pdev->dev, "invalid bank %u\n", val);
873 return -EINVAL;
874 }
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100875 host->bank = val;
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200876 }
Stefan Roeseeea62812012-03-16 10:19:31 +0100877 return 0;
878}
Stefan Roeseeea62812012-03-16 10:19:31 +0100879
Linus Walleij6c009ab2010-09-13 00:35:22 +0200880/*
881 * fsmc_nand_probe - Probe function
882 * @pdev: platform device structure
883 */
884static int __init fsmc_nand_probe(struct platform_device *pdev)
885{
Linus Walleij6c009ab2010-09-13 00:35:22 +0200886 struct fsmc_nand_data *host;
887 struct mtd_info *mtd;
888 struct nand_chip *nand;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200889 struct resource *res;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530890 dma_cap_mask_t mask;
Linus Walleij4ad916b2010-11-29 13:52:06 +0100891 int ret = 0;
Linus Walleij593cd872010-11-29 13:52:19 +0100892 u32 pid;
893 int i;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200894
Linus Walleij6c009ab2010-09-13 00:35:22 +0200895 /* Allocate memory for the device structure (and zero it) */
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530896 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Jingoo Hand9a21ae2013-12-26 12:16:38 +0900897 if (!host)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200898 return -ENOMEM;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200899
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100900 nand = &host->nand;
901
902 ret = fsmc_nand_probe_config_dt(pdev, host, nand);
903 if (ret)
904 return ret;
905
Linus Walleij6c009ab2010-09-13 00:35:22 +0200906 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
Thierry Redingb0de7742013-01-21 11:09:12 +0100907 host->data_va = devm_ioremap_resource(&pdev->dev, res);
908 if (IS_ERR(host->data_va))
909 return PTR_ERR(host->data_va);
Stefan Roesecbf29b82015-10-02 12:40:20 +0200910
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +0200911 host->data_pa = (dma_addr_t)res->start;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200912
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +0200913 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
Thierry Redingb0de7742013-01-21 11:09:12 +0100914 host->addr_va = devm_ioremap_resource(&pdev->dev, res);
915 if (IS_ERR(host->addr_va))
916 return PTR_ERR(host->addr_va);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200917
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +0200918 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
Thierry Redingb0de7742013-01-21 11:09:12 +0100919 host->cmd_va = devm_ioremap_resource(&pdev->dev, res);
920 if (IS_ERR(host->cmd_va))
921 return PTR_ERR(host->cmd_va);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200922
923 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
Thierry Redingb0de7742013-01-21 11:09:12 +0100924 host->regs_va = devm_ioremap_resource(&pdev->dev, res);
925 if (IS_ERR(host->regs_va))
926 return PTR_ERR(host->regs_va);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200927
Thomas Petazzonifb8ed2c2017-03-21 11:04:03 +0100928 host->clk = devm_clk_get(&pdev->dev, NULL);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200929 if (IS_ERR(host->clk)) {
930 dev_err(&pdev->dev, "failed to fetch block clock\n");
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530931 return PTR_ERR(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200932 }
933
Viresh Kumare25da1c2012-04-17 17:07:57 +0530934 ret = clk_prepare_enable(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200935 if (ret)
Thomas Petazzonifb8ed2c2017-03-21 11:04:03 +0100936 return ret;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200937
Linus Walleij593cd872010-11-29 13:52:19 +0100938 /*
939 * This device ID is actually a common AMBA ID as used on the
940 * AMBA PrimeCell bus. However it is not a PrimeCell.
941 */
942 for (pid = 0, i = 0; i < 4; i++)
943 pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
944 host->pid = pid;
945 dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
946 "revision %02x, config %02x\n",
947 AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
948 AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
949
Vipin Kumar712c4ad2012-03-14 11:47:16 +0530950 host->dev = &pdev->dev;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530951
952 if (host->mode == USE_DMA_ACCESS)
953 init_completion(&host->dma_access_complete);
954
Linus Walleij6c009ab2010-09-13 00:35:22 +0200955 /* Link all private pointers */
Boris BREZILLONbdf3a552015-12-10 09:00:05 +0100956 mtd = nand_to_mtd(&host->nand);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100957 nand_set_controller_data(nand, host);
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100958 nand_set_flash_node(nand, pdev->dev.of_node);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200959
Boris BREZILLONbdf3a552015-12-10 09:00:05 +0100960 mtd->dev.parent = &pdev->dev;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200961 nand->IO_ADDR_R = host->data_va;
962 nand->IO_ADDR_W = host->data_va;
963 nand->cmd_ctrl = fsmc_cmd_ctrl;
964 nand->chip_delay = 30;
965
Stefan Roesee278fc72015-10-19 08:40:13 +0200966 /*
967 * Setup default ECC mode. nand_dt_init() called from nand_scan_ident()
968 * can overwrite this value if the DT provides a different value.
969 */
Linus Walleij6c009ab2010-09-13 00:35:22 +0200970 nand->ecc.mode = NAND_ECC_HW;
971 nand->ecc.hwctl = fsmc_enable_hwecc;
972 nand->ecc.size = 512;
Vipin Kumar467e6e72012-03-14 11:47:12 +0530973 nand->badblockbits = 7;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200974
Vipin Kumar4774fb02012-03-14 11:47:18 +0530975 switch (host->mode) {
976 case USE_DMA_ACCESS:
977 dma_cap_zero(mask);
978 dma_cap_set(DMA_MEMCPY, mask);
Thomas Petazzonifeb1e572017-03-21 11:03:59 +0100979 host->read_dma_chan = dma_request_channel(mask, filter, NULL);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530980 if (!host->read_dma_chan) {
981 dev_err(&pdev->dev, "Unable to get read dma channel\n");
982 goto err_req_read_chnl;
983 }
Thomas Petazzonifeb1e572017-03-21 11:03:59 +0100984 host->write_dma_chan = dma_request_channel(mask, filter, NULL);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530985 if (!host->write_dma_chan) {
986 dev_err(&pdev->dev, "Unable to get write dma channel\n");
987 goto err_req_write_chnl;
988 }
989 nand->read_buf = fsmc_read_buf_dma;
990 nand->write_buf = fsmc_write_buf_dma;
991 break;
992
993 default:
994 case USE_WORD_ACCESS:
Vipin Kumar604e7542012-03-14 11:47:17 +0530995 nand->read_buf = fsmc_read_buf;
996 nand->write_buf = fsmc_write_buf;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530997 break;
Vipin Kumar604e7542012-03-14 11:47:17 +0530998 }
999
Thomas Petazzonid9fb0792017-04-29 10:52:35 +02001000 if (host->dev_timings)
1001 fsmc_nand_setup(host, host->dev_timings);
1002 else
1003 nand->setup_data_interface = fsmc_setup_data_interface;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001004
Linus Walleij593cd872010-11-29 13:52:19 +01001005 if (AMBA_REV_BITS(host->pid) >= 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +02001006 nand->ecc.read_page = fsmc_read_page_hwecc;
1007 nand->ecc.calculate = fsmc_read_hwecc_ecc4;
Armando Visconti753e0132012-03-07 17:00:54 +05301008 nand->ecc.correct = fsmc_bch8_correct_data;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001009 nand->ecc.bytes = 13;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001010 nand->ecc.strength = 8;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001011 }
1012
1013 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001014 * Scan to find existence of the device
Linus Walleij6c009ab2010-09-13 00:35:22 +02001015 */
Masahiro Yamadaad5678e2016-11-04 19:43:00 +09001016 ret = nand_scan_ident(mtd, 1, NULL);
1017 if (ret) {
Linus Walleij6c009ab2010-09-13 00:35:22 +02001018 dev_err(&pdev->dev, "No NAND Device found!\n");
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301019 goto err_scan_ident;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001020 }
1021
Linus Walleij593cd872010-11-29 13:52:19 +01001022 if (AMBA_REV_BITS(host->pid) >= 8) {
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001023 switch (mtd->oobsize) {
Bhavna Yadave29ee572012-03-07 17:00:50 +05301024 case 16:
Bhavna Yadave29ee572012-03-07 17:00:50 +05301025 case 64:
Bhavna Yadave29ee572012-03-07 17:00:50 +05301026 case 128:
Armando Visconti0c78e932012-03-07 17:00:55 +05301027 case 224:
Bhavna Yadave29ee572012-03-07 17:00:50 +05301028 case 256:
Bhavna Yadave29ee572012-03-07 17:00:50 +05301029 break;
1030 default:
Jingoo Han67b19a62013-12-26 12:31:25 +09001031 dev_warn(&pdev->dev, "No oob scheme defined for oobsize %d\n",
1032 mtd->oobsize);
Stefan Roese6efadcf2015-10-02 12:40:21 +02001033 ret = -EINVAL;
1034 goto err_probe;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001035 }
Boris Brezillon22b46952016-02-03 20:01:42 +01001036
1037 mtd_set_ooblayout(mtd, &fsmc_ecc4_ooblayout_ops);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001038 } else {
Stefan Roesee278fc72015-10-19 08:40:13 +02001039 switch (nand->ecc.mode) {
1040 case NAND_ECC_HW:
1041 dev_info(&pdev->dev, "Using 1-bit HW ECC scheme\n");
1042 nand->ecc.calculate = fsmc_read_hwecc_ecc1;
1043 nand->ecc.correct = nand_correct_data;
1044 nand->ecc.bytes = 3;
1045 nand->ecc.strength = 1;
Bhavna Yadave29ee572012-03-07 17:00:50 +05301046 break;
Stefan Roesee278fc72015-10-19 08:40:13 +02001047
Rafał Miłeckief296dc2016-04-17 22:53:04 +02001048 case NAND_ECC_SOFT:
Rafał Miłeckief296dc2016-04-17 22:53:04 +02001049 if (nand->ecc.algo == NAND_ECC_BCH) {
1050 dev_info(&pdev->dev, "Using 4-bit SW BCH ECC scheme\n");
1051 break;
1052 }
Stefan Roesee278fc72015-10-19 08:40:13 +02001053
Thomas Petazzoni838ff7b2017-04-29 11:06:46 +02001054 case NAND_ECC_ON_DIE:
1055 break;
1056
Bhavna Yadave29ee572012-03-07 17:00:50 +05301057 default:
Stefan Roesee278fc72015-10-19 08:40:13 +02001058 dev_err(&pdev->dev, "Unsupported ECC mode!\n");
Stefan Roese6efadcf2015-10-02 12:40:21 +02001059 goto err_probe;
Bhavna Yadave29ee572012-03-07 17:00:50 +05301060 }
Stefan Roesee278fc72015-10-19 08:40:13 +02001061
1062 /*
1063 * Don't set layout for BCH4 SW ECC. This will be
1064 * generated later in nand_bch_init() later.
1065 */
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02001066 if (nand->ecc.mode == NAND_ECC_HW) {
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001067 switch (mtd->oobsize) {
Stefan Roesee278fc72015-10-19 08:40:13 +02001068 case 16:
Stefan Roesee278fc72015-10-19 08:40:13 +02001069 case 64:
Stefan Roesee278fc72015-10-19 08:40:13 +02001070 case 128:
Boris Brezillon22b46952016-02-03 20:01:42 +01001071 mtd_set_ooblayout(mtd,
1072 &fsmc_ecc1_ooblayout_ops);
Stefan Roesee278fc72015-10-19 08:40:13 +02001073 break;
1074 default:
1075 dev_warn(&pdev->dev,
1076 "No oob scheme defined for oobsize %d\n",
1077 mtd->oobsize);
1078 ret = -EINVAL;
1079 goto err_probe;
1080 }
1081 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001082 }
1083
1084 /* Second stage of scan to fill MTD data-structures */
Masahiro Yamadaad5678e2016-11-04 19:43:00 +09001085 ret = nand_scan_tail(mtd);
1086 if (ret)
Linus Walleij6c009ab2010-09-13 00:35:22 +02001087 goto err_probe;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001088
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001089 mtd->name = "nand";
Thomas Petazzoniede29a02017-03-21 11:04:00 +01001090 ret = mtd_device_register(mtd, NULL, 0);
Jamie Iles99335d02011-05-23 10:23:23 +01001091 if (ret)
Linus Walleij6c009ab2010-09-13 00:35:22 +02001092 goto err_probe;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001093
1094 platform_set_drvdata(pdev, host);
1095 dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
1096 return 0;
1097
1098err_probe:
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301099err_scan_ident:
Vipin Kumar4774fb02012-03-14 11:47:18 +05301100 if (host->mode == USE_DMA_ACCESS)
1101 dma_release_channel(host->write_dma_chan);
1102err_req_write_chnl:
1103 if (host->mode == USE_DMA_ACCESS)
1104 dma_release_channel(host->read_dma_chan);
1105err_req_read_chnl:
Viresh Kumare25da1c2012-04-17 17:07:57 +05301106 clk_disable_unprepare(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001107 return ret;
1108}
1109
1110/*
1111 * Clean up routine
1112 */
1113static int fsmc_nand_remove(struct platform_device *pdev)
1114{
1115 struct fsmc_nand_data *host = platform_get_drvdata(pdev);
1116
Linus Walleij6c009ab2010-09-13 00:35:22 +02001117 if (host) {
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001118 nand_release(nand_to_mtd(&host->nand));
Vipin Kumar4774fb02012-03-14 11:47:18 +05301119
1120 if (host->mode == USE_DMA_ACCESS) {
1121 dma_release_channel(host->write_dma_chan);
1122 dma_release_channel(host->read_dma_chan);
1123 }
Viresh Kumare25da1c2012-04-17 17:07:57 +05301124 clk_disable_unprepare(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001125 }
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301126
Linus Walleij6c009ab2010-09-13 00:35:22 +02001127 return 0;
1128}
1129
Jingoo Han80ce4dd2013-03-26 15:53:48 +09001130#ifdef CONFIG_PM_SLEEP
Linus Walleij6c009ab2010-09-13 00:35:22 +02001131static int fsmc_nand_suspend(struct device *dev)
1132{
1133 struct fsmc_nand_data *host = dev_get_drvdata(dev);
1134 if (host)
Viresh Kumare25da1c2012-04-17 17:07:57 +05301135 clk_disable_unprepare(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001136 return 0;
1137}
1138
1139static int fsmc_nand_resume(struct device *dev)
1140{
1141 struct fsmc_nand_data *host = dev_get_drvdata(dev);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301142 if (host) {
Viresh Kumare25da1c2012-04-17 17:07:57 +05301143 clk_prepare_enable(host->clk);
Thomas Petazzonid9fb0792017-04-29 10:52:35 +02001144 if (host->dev_timings)
1145 fsmc_nand_setup(host, host->dev_timings);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301146 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001147 return 0;
1148}
Jingoo Han80ce4dd2013-03-26 15:53:48 +09001149#endif
Linus Walleij6c009ab2010-09-13 00:35:22 +02001150
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301151static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001152
Stefan Roeseeea62812012-03-16 10:19:31 +01001153static const struct of_device_id fsmc_nand_id_table[] = {
1154 { .compatible = "st,spear600-fsmc-nand" },
Linus Walleijba785202013-01-05 22:28:32 +01001155 { .compatible = "stericsson,fsmc-nand" },
Stefan Roeseeea62812012-03-16 10:19:31 +01001156 {}
1157};
1158MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);
Stefan Roeseeea62812012-03-16 10:19:31 +01001159
Linus Walleij6c009ab2010-09-13 00:35:22 +02001160static struct platform_driver fsmc_nand_driver = {
1161 .remove = fsmc_nand_remove,
1162 .driver = {
Linus Walleij6c009ab2010-09-13 00:35:22 +02001163 .name = "fsmc-nand",
Thomas Petazzoni33575b22017-03-21 11:04:05 +01001164 .of_match_table = fsmc_nand_id_table,
Linus Walleij6c009ab2010-09-13 00:35:22 +02001165 .pm = &fsmc_nand_pm_ops,
Linus Walleij6c009ab2010-09-13 00:35:22 +02001166 },
1167};
1168
Jingoo Han307d2a512013-03-05 13:30:36 +09001169module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001170
1171MODULE_LICENSE("GPL");
1172MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
1173MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");