blob: 6b2bda815b880c969c239d9dc6336d2392dcbb42 [file] [log] [blame]
Boris Brezillonbb696342018-11-20 10:02:34 +01001// SPDX-License-Identifier: GPL-2.0
Linus Walleij6c009ab2010-09-13 00:35:22 +02002/*
Linus Walleij6c009ab2010-09-13 00:35:22 +02003 * ST Microelectronics
4 * Flexible Static Memory Controller (FSMC)
5 * Driver for NAND portions
6 *
7 * Copyright © 2010 ST Microelectronics
8 * Vipin Kumar <vipin.kumar@st.com>
9 * Ashish Priyadarshi
10 *
Boris Brezillon187c54482018-02-05 23:02:02 +010011 * Based on drivers/mtd/nand/nomadik_nand.c (removed in v3.8)
Boris Brezillon7b6afee2018-02-05 23:02:03 +010012 * Copyright © 2007 STMicroelectronics Pvt. Ltd.
13 * Copyright © 2009 Alessandro Rubini
Linus Walleij6c009ab2010-09-13 00:35:22 +020014 */
15
16#include <linux/clk.h>
Vipin Kumar4774fb02012-03-14 11:47:18 +053017#include <linux/completion.h>
Herve Codinaa4ca0c42021-11-19 16:03:15 +010018#include <linux/delay.h>
Vipin Kumar4774fb02012-03-14 11:47:18 +053019#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>
Miquel Raynalad9ffdc2021-04-13 18:18:35 +020029#include <linux/mtd/nand-ecc-sw-hamming.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020030#include <linux/mtd/rawnand.h>
Linus Walleij6c009ab2010-09-13 00:35:22 +020031#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 */
Boris Brezillonfc43f452018-11-20 10:02:35 +010042 #define BANK_ENABLE BIT(0)
43 #define MUXED BIT(1)
Linus Walleij4404d7d2016-12-18 12:34:55 +010044 #define NOR_DEV (2 << 2)
Boris Brezillonfc43f452018-11-20 10:02:35 +010045 #define WIDTH_16 BIT(4)
46 #define RSTPWRDWN BIT(6)
47 #define WPROT BIT(7)
48 #define WRT_ENABLE BIT(12)
49 #define WAIT_ENB BIT(13)
Linus Walleij4404d7d2016-12-18 12:34:55 +010050
51#define CTRL_TIM 0x4
52 /* ctrl_tim register definitions */
53
54#define FSMC_NOR_BANK_SZ 0x8
55#define FSMC_NOR_REG_SIZE 0x40
56
Boris Brezillonfc43f452018-11-20 10:02:35 +010057#define FSMC_NOR_REG(base, bank, reg) ((base) + \
58 (FSMC_NOR_BANK_SZ * (bank)) + \
59 (reg))
Linus Walleij4404d7d2016-12-18 12:34:55 +010060
61/* fsmc controller registers for NAND flash */
Boris Brezillon8f3931e2018-07-09 22:09:34 +020062#define FSMC_PC 0x00
Linus Walleij4404d7d2016-12-18 12:34:55 +010063 /* pc register definitions */
Boris Brezillonfc43f452018-11-20 10:02:35 +010064 #define FSMC_RESET BIT(0)
65 #define FSMC_WAITON BIT(1)
66 #define FSMC_ENABLE BIT(2)
67 #define FSMC_DEVTYPE_NAND BIT(3)
68 #define FSMC_DEVWID_16 BIT(4)
69 #define FSMC_ECCEN BIT(6)
70 #define FSMC_ECCPLEN_256 BIT(7)
Linus Walleij4404d7d2016-12-18 12:34:55 +010071 #define FSMC_TCLR_SHIFT (9)
72 #define FSMC_TCLR_MASK (0xF)
Linus Walleij4404d7d2016-12-18 12:34:55 +010073 #define FSMC_TAR_SHIFT (13)
74 #define FSMC_TAR_MASK (0xF)
75#define STS 0x04
76 /* sts register definitions */
Boris Brezillonfc43f452018-11-20 10:02:35 +010077 #define FSMC_CODE_RDY BIT(15)
Linus Walleij4404d7d2016-12-18 12:34:55 +010078#define COMM 0x08
79 /* comm register definitions */
Linus Walleij4404d7d2016-12-18 12:34:55 +010080 #define FSMC_TSET_SHIFT 0
81 #define FSMC_TSET_MASK 0xFF
Linus Walleij4404d7d2016-12-18 12:34:55 +010082 #define FSMC_TWAIT_SHIFT 8
83 #define FSMC_TWAIT_MASK 0xFF
Linus Walleij4404d7d2016-12-18 12:34:55 +010084 #define FSMC_THOLD_SHIFT 16
85 #define FSMC_THOLD_MASK 0xFF
Linus Walleij4404d7d2016-12-18 12:34:55 +010086 #define FSMC_THIZ_SHIFT 24
87 #define FSMC_THIZ_MASK 0xFF
88#define ATTRIB 0x0C
89#define IOATA 0x10
90#define ECC1 0x14
91#define ECC2 0x18
92#define ECC3 0x1C
93#define FSMC_NAND_BANK_SZ 0x20
94
Linus Walleij4404d7d2016-12-18 12:34:55 +010095#define FSMC_BUSY_WAIT_TIMEOUT (1 * HZ)
96
Herve Codina9472335e2021-11-19 16:03:16 +010097/*
98 * According to SPEAr300 Reference Manual (RM0082)
99 * TOUDEL = 7ns (Output delay from the flip-flops to the board)
100 * TINDEL = 5ns (Input delay from the board to the flipflop)
101 */
102#define TOUTDEL 7000
103#define TINDEL 5000
104
Linus Walleij4404d7d2016-12-18 12:34:55 +0100105struct fsmc_nand_timings {
Boris Brezillonfc43f452018-11-20 10:02:35 +0100106 u8 tclr;
107 u8 tar;
108 u8 thiz;
109 u8 thold;
110 u8 twait;
111 u8 tset;
Linus Walleij4404d7d2016-12-18 12:34:55 +0100112};
113
114enum access_mode {
115 USE_DMA_ACCESS = 1,
116 USE_WORD_ACCESS,
117};
118
119/**
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100120 * struct fsmc_nand_data - structure for FSMC NAND device state
121 *
Boris Brezillonad711482018-11-20 10:02:33 +0100122 * @base: Inherit from the nand_controller struct
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100123 * @pid: Part ID on the AMBA PrimeCell format
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100124 * @nand: Chip related info for a NAND flash.
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100125 *
126 * @bank: Bank number for probed device.
Boris Brezillon5b47f402018-11-20 10:02:31 +0100127 * @dev: Parent device
128 * @mode: Access mode
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100129 * @clk: Clock structure for FSMC.
130 *
131 * @read_dma_chan: DMA channel for read access
132 * @write_dma_chan: DMA channel for write access to NAND
133 * @dma_access_complete: Completion structure
134 *
Boris Brezillon5b47f402018-11-20 10:02:31 +0100135 * @dev_timings: NAND timings
136 *
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100137 * @data_pa: NAND Physical port for Data.
138 * @data_va: NAND port for Data.
139 * @cmd_va: NAND port for Command.
140 * @addr_va: NAND port for Address.
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100141 * @regs_va: Registers base address for a given bank.
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100142 */
143struct fsmc_nand_data {
Boris Brezillonad711482018-11-20 10:02:33 +0100144 struct nand_controller base;
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100145 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 Brezillonbfc535f2018-11-20 10:02:30 +0100251static inline struct fsmc_nand_data *nand_to_fsmc(struct nand_chip *chip)
Boris BREZILLON277af422015-12-10 08:59:46 +0100252{
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100253 return container_of(chip, struct fsmc_nand_data, nand);
Boris BREZILLON277af422015-12-10 08:59:46 +0100254}
255
Linus Walleij6c009ab2010-09-13 00:35:22 +0200256/*
Linus Walleij6c009ab2010-09-13 00:35:22 +0200257 * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
258 *
259 * This routine initializes timing parameters related to NAND memory access in
260 * FSMC registers
261 */
Thomas Petazzoni6335b502017-04-29 10:52:34 +0200262static void fsmc_nand_setup(struct fsmc_nand_data *host,
Thomas Petazzoni1debdb92017-04-29 10:52:36 +0200263 struct fsmc_nand_timings *tims)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200264{
Boris Brezillonfc43f452018-11-20 10:02:35 +0100265 u32 value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
266 u32 tclr, tar, thiz, thold, twait, tset;
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530267
268 tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT;
269 tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT;
270 thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT;
271 thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT;
272 twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT;
273 tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200274
Thomas Petazzoni6335b502017-04-29 10:52:34 +0200275 if (host->nand.options & NAND_BUSWIDTH_16)
Boris Brezillonfc43f452018-11-20 10:02:35 +0100276 value |= FSMC_DEVWID_16;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200277
Boris Brezillonfc43f452018-11-20 10:02:35 +0100278 writel_relaxed(value | tclr | tar, host->regs_va + FSMC_PC);
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100279 writel_relaxed(thiz | thold | twait | tset, host->regs_va + COMM);
280 writel_relaxed(thiz | thold | twait | tset, host->regs_va + ATTRIB);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200281}
282
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200283static int fsmc_calc_timings(struct fsmc_nand_data *host,
284 const struct nand_sdr_timings *sdrt,
285 struct fsmc_nand_timings *tims)
286{
287 unsigned long hclk = clk_get_rate(host->clk);
288 unsigned long hclkn = NSEC_PER_SEC / hclk;
Herve Codina9472335e2021-11-19 16:03:16 +0100289 u32 thiz, thold, twait, tset, twait_min;
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200290
291 if (sdrt->tRC_min < 30000)
292 return -EOPNOTSUPP;
293
294 tims->tar = DIV_ROUND_UP(sdrt->tAR_min / 1000, hclkn) - 1;
295 if (tims->tar > FSMC_TAR_MASK)
296 tims->tar = FSMC_TAR_MASK;
297 tims->tclr = DIV_ROUND_UP(sdrt->tCLR_min / 1000, hclkn) - 1;
298 if (tims->tclr > FSMC_TCLR_MASK)
299 tims->tclr = FSMC_TCLR_MASK;
300
301 thiz = sdrt->tCS_min - sdrt->tWP_min;
302 tims->thiz = DIV_ROUND_UP(thiz / 1000, hclkn);
303
304 thold = sdrt->tDH_min;
305 if (thold < sdrt->tCH_min)
306 thold = sdrt->tCH_min;
307 if (thold < sdrt->tCLH_min)
308 thold = sdrt->tCLH_min;
309 if (thold < sdrt->tWH_min)
310 thold = sdrt->tWH_min;
311 if (thold < sdrt->tALH_min)
312 thold = sdrt->tALH_min;
313 if (thold < sdrt->tREH_min)
314 thold = sdrt->tREH_min;
315 tims->thold = DIV_ROUND_UP(thold / 1000, hclkn);
316 if (tims->thold == 0)
317 tims->thold = 1;
318 else if (tims->thold > FSMC_THOLD_MASK)
319 tims->thold = FSMC_THOLD_MASK;
320
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200321 tset = max(sdrt->tCS_min - sdrt->tWP_min,
322 sdrt->tCEA_max - sdrt->tREA_max);
323 tims->tset = DIV_ROUND_UP(tset / 1000, hclkn) - 1;
324 if (tims->tset == 0)
325 tims->tset = 1;
326 else if (tims->tset > FSMC_TSET_MASK)
327 tims->tset = FSMC_TSET_MASK;
328
Herve Codina9472335e2021-11-19 16:03:16 +0100329 /*
330 * According to SPEAr300 Reference Manual (RM0082) which gives more
331 * information related to FSMSC timings than the SPEAr600 one (RM0305),
332 * twait >= tCEA - (tset * TCLK) + TOUTDEL + TINDEL
333 */
334 twait_min = sdrt->tCEA_max - ((tims->tset + 1) * hclkn * 1000)
335 + TOUTDEL + TINDEL;
336 twait = max3(sdrt->tRP_min, sdrt->tWP_min, twait_min);
337
338 tims->twait = DIV_ROUND_UP(twait / 1000, hclkn) - 1;
339 if (tims->twait == 0)
340 tims->twait = 1;
341 else if (tims->twait > FSMC_TWAIT_MASK)
342 tims->twait = FSMC_TWAIT_MASK;
343
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200344 return 0;
345}
346
Miquel Raynal4c466672020-05-29 13:13:13 +0200347static int fsmc_setup_interface(struct nand_chip *nand, int csline,
348 const struct nand_interface_config *conf)
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200349{
Boris Brezillon1e809f72018-11-20 10:02:32 +0100350 struct fsmc_nand_data *host = nand_to_fsmc(nand);
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200351 struct fsmc_nand_timings tims;
352 const struct nand_sdr_timings *sdrt;
353 int ret;
354
355 sdrt = nand_get_sdr_timings(conf);
356 if (IS_ERR(sdrt))
357 return PTR_ERR(sdrt);
358
359 ret = fsmc_calc_timings(host, sdrt, &tims);
360 if (ret)
361 return ret;
362
Boris Brezillon104e4422017-03-16 09:35:58 +0100363 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200364 return 0;
365
366 fsmc_nand_setup(host, &tims);
367
368 return 0;
369}
370
Linus Walleij6c009ab2010-09-13 00:35:22 +0200371/*
372 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
373 */
Boris Brezillonec476362018-09-06 14:05:17 +0200374static void fsmc_enable_hwecc(struct nand_chip *chip, int mode)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200375{
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100376 struct fsmc_nand_data *host = nand_to_fsmc(chip);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200377
Boris Brezillon8f3931e2018-07-09 22:09:34 +0200378 writel_relaxed(readl(host->regs_va + FSMC_PC) & ~FSMC_ECCPLEN_256,
379 host->regs_va + FSMC_PC);
380 writel_relaxed(readl(host->regs_va + FSMC_PC) & ~FSMC_ECCEN,
381 host->regs_va + FSMC_PC);
382 writel_relaxed(readl(host->regs_va + FSMC_PC) | FSMC_ECCEN,
383 host->regs_va + FSMC_PC);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200384}
385
386/*
387 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300388 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200389 * max of 8-bits)
390 */
Boris Brezillonfc43f452018-11-20 10:02:35 +0100391static int fsmc_read_hwecc_ecc4(struct nand_chip *chip, const u8 *data,
392 u8 *ecc)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200393{
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100394 struct fsmc_nand_data *host = nand_to_fsmc(chip);
Boris Brezillonfc43f452018-11-20 10:02:35 +0100395 u32 ecc_tmp;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200396 unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
397
398 do {
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100399 if (readl_relaxed(host->regs_va + STS) & FSMC_CODE_RDY)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200400 break;
Boris Brezillonfc43f452018-11-20 10:02:35 +0100401
402 cond_resched();
Linus Walleij6c009ab2010-09-13 00:35:22 +0200403 } while (!time_after_eq(jiffies, deadline));
404
Vipin Kumar712c4ad2012-03-14 11:47:16 +0530405 if (time_after_eq(jiffies, deadline)) {
406 dev_err(host->dev, "calculate ecc timed out\n");
407 return -ETIMEDOUT;
408 }
409
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100410 ecc_tmp = readl_relaxed(host->regs_va + ECC1);
Boris Brezillonfc43f452018-11-20 10:02:35 +0100411 ecc[0] = ecc_tmp;
412 ecc[1] = ecc_tmp >> 8;
413 ecc[2] = ecc_tmp >> 16;
414 ecc[3] = ecc_tmp >> 24;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200415
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100416 ecc_tmp = readl_relaxed(host->regs_va + ECC2);
Boris Brezillonfc43f452018-11-20 10:02:35 +0100417 ecc[4] = ecc_tmp;
418 ecc[5] = ecc_tmp >> 8;
419 ecc[6] = ecc_tmp >> 16;
420 ecc[7] = ecc_tmp >> 24;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200421
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100422 ecc_tmp = readl_relaxed(host->regs_va + ECC3);
Boris Brezillonfc43f452018-11-20 10:02:35 +0100423 ecc[8] = ecc_tmp;
424 ecc[9] = ecc_tmp >> 8;
425 ecc[10] = ecc_tmp >> 16;
426 ecc[11] = ecc_tmp >> 24;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200427
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100428 ecc_tmp = readl_relaxed(host->regs_va + STS);
Boris Brezillonfc43f452018-11-20 10:02:35 +0100429 ecc[12] = ecc_tmp >> 16;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200430
431 return 0;
432}
433
434/*
435 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300436 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200437 * max of 1-bit)
438 */
Boris Brezillonfc43f452018-11-20 10:02:35 +0100439static int fsmc_read_hwecc_ecc1(struct nand_chip *chip, const u8 *data,
440 u8 *ecc)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200441{
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100442 struct fsmc_nand_data *host = nand_to_fsmc(chip);
Boris Brezillonfc43f452018-11-20 10:02:35 +0100443 u32 ecc_tmp;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200444
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100445 ecc_tmp = readl_relaxed(host->regs_va + ECC1);
Boris Brezillonfc43f452018-11-20 10:02:35 +0100446 ecc[0] = ecc_tmp;
447 ecc[1] = ecc_tmp >> 8;
448 ecc[2] = ecc_tmp >> 16;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200449
450 return 0;
451}
452
Miquel Raynalad9ffdc2021-04-13 18:18:35 +0200453static int fsmc_correct_ecc1(struct nand_chip *chip,
454 unsigned char *buf,
455 unsigned char *read_ecc,
456 unsigned char *calc_ecc)
457{
Miquel Raynal9be14462021-09-29 00:15:00 +0200458 bool sm_order = chip->ecc.options & NAND_ECC_SOFT_HAMMING_SM_ORDER;
459
Miquel Raynalad9ffdc2021-04-13 18:18:35 +0200460 return ecc_sw_hamming_correct(buf, read_ecc, calc_ecc,
Miquel Raynal9be14462021-09-29 00:15:00 +0200461 chip->ecc.size, sm_order);
Miquel Raynalad9ffdc2021-04-13 18:18:35 +0200462}
463
Vipin Kumar519300c2012-03-07 17:00:49 +0530464/* Count the number of 0's in buff upto a max of max_bits */
Boris Brezillonfc43f452018-11-20 10:02:35 +0100465static int count_written_bits(u8 *buff, int size, int max_bits)
Vipin Kumar519300c2012-03-07 17:00:49 +0530466{
467 int k, written_bits = 0;
468
469 for (k = 0; k < size; k++) {
470 written_bits += hweight8(~buff[k]);
471 if (written_bits > max_bits)
472 break;
473 }
474
475 return written_bits;
476}
477
Vipin Kumar4774fb02012-03-14 11:47:18 +0530478static void dma_complete(void *param)
479{
480 struct fsmc_nand_data *host = param;
481
482 complete(&host->dma_access_complete);
483}
484
485static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len,
Boris Brezillonfc43f452018-11-20 10:02:35 +0100486 enum dma_data_direction direction)
Vipin Kumar4774fb02012-03-14 11:47:18 +0530487{
488 struct dma_chan *chan;
489 struct dma_device *dma_dev;
490 struct dma_async_tx_descriptor *tx;
491 dma_addr_t dma_dst, dma_src, dma_addr;
492 dma_cookie_t cookie;
493 unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
494 int ret;
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400495 unsigned long time_left;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530496
497 if (direction == DMA_TO_DEVICE)
498 chan = host->write_dma_chan;
499 else if (direction == DMA_FROM_DEVICE)
500 chan = host->read_dma_chan;
501 else
502 return -EINVAL;
503
504 dma_dev = chan->device;
505 dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction);
506
507 if (direction == DMA_TO_DEVICE) {
508 dma_src = dma_addr;
509 dma_dst = host->data_pa;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530510 } else {
511 dma_src = host->data_pa;
512 dma_dst = dma_addr;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530513 }
514
515 tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
516 len, flags);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530517 if (!tx) {
518 dev_err(host->dev, "device_prep_dma_memcpy error\n");
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000519 ret = -EIO;
520 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530521 }
522
523 tx->callback = dma_complete;
524 tx->callback_param = host;
525 cookie = tx->tx_submit(tx);
526
527 ret = dma_submit_error(cookie);
528 if (ret) {
529 dev_err(host->dev, "dma_submit_error %d\n", cookie);
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000530 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530531 }
532
533 dma_async_issue_pending(chan);
534
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400535 time_left =
Vipin Kumar928aa2a2012-10-09 16:14:48 +0530536 wait_for_completion_timeout(&host->dma_access_complete,
Boris Brezillonfc43f452018-11-20 10:02:35 +0100537 msecs_to_jiffies(3000));
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400538 if (time_left == 0) {
Vinod Koulb177ea32014-10-11 21:10:32 +0530539 dmaengine_terminate_all(chan);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530540 dev_err(host->dev, "wait_for_completion_timeout\n");
Nicholas Mc Guire0bda3e12015-03-13 07:54:45 -0400541 ret = -ETIMEDOUT;
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000542 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530543 }
544
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000545 ret = 0;
546
547unmap_dma:
548 dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
549
550 return ret;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530551}
552
Linus Walleij6c009ab2010-09-13 00:35:22 +0200553/*
Vipin Kumar604e7542012-03-14 11:47:17 +0530554 * fsmc_write_buf - write buffer to chip
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100555 * @host: FSMC NAND controller
Vipin Kumar604e7542012-03-14 11:47:17 +0530556 * @buf: data buffer
557 * @len: number of bytes to write
558 */
Boris Brezillonfc43f452018-11-20 10:02:35 +0100559static void fsmc_write_buf(struct fsmc_nand_data *host, const u8 *buf,
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100560 int len)
Vipin Kumar604e7542012-03-14 11:47:17 +0530561{
562 int i;
Vipin Kumar604e7542012-03-14 11:47:17 +0530563
Boris Brezillonfc43f452018-11-20 10:02:35 +0100564 if (IS_ALIGNED((uintptr_t)buf, sizeof(u32)) &&
565 IS_ALIGNED(len, sizeof(u32))) {
566 u32 *p = (u32 *)buf;
567
Vipin Kumar604e7542012-03-14 11:47:17 +0530568 len = len >> 2;
569 for (i = 0; i < len; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100570 writel_relaxed(p[i], host->data_va);
Vipin Kumar604e7542012-03-14 11:47:17 +0530571 } else {
572 for (i = 0; i < len; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100573 writeb_relaxed(buf[i], host->data_va);
Vipin Kumar604e7542012-03-14 11:47:17 +0530574 }
575}
576
577/*
578 * fsmc_read_buf - read chip data into buffer
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100579 * @host: FSMC NAND controller
Vipin Kumar604e7542012-03-14 11:47:17 +0530580 * @buf: buffer to store date
581 * @len: number of bytes to read
582 */
Boris Brezillonfc43f452018-11-20 10:02:35 +0100583static void fsmc_read_buf(struct fsmc_nand_data *host, u8 *buf, int len)
Vipin Kumar604e7542012-03-14 11:47:17 +0530584{
585 int i;
Vipin Kumar604e7542012-03-14 11:47:17 +0530586
Boris Brezillonfc43f452018-11-20 10:02:35 +0100587 if (IS_ALIGNED((uintptr_t)buf, sizeof(u32)) &&
588 IS_ALIGNED(len, sizeof(u32))) {
589 u32 *p = (u32 *)buf;
590
Vipin Kumar604e7542012-03-14 11:47:17 +0530591 len = len >> 2;
592 for (i = 0; i < len; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100593 p[i] = readl_relaxed(host->data_va);
Vipin Kumar604e7542012-03-14 11:47:17 +0530594 } else {
595 for (i = 0; i < len; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100596 buf[i] = readb_relaxed(host->data_va);
Vipin Kumar604e7542012-03-14 11:47:17 +0530597 }
598}
599
600/*
Vipin Kumar4774fb02012-03-14 11:47:18 +0530601 * fsmc_read_buf_dma - read chip data into buffer
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100602 * @host: FSMC NAND controller
Vipin Kumar4774fb02012-03-14 11:47:18 +0530603 * @buf: buffer to store date
604 * @len: number of bytes to read
605 */
Boris Brezillonfc43f452018-11-20 10:02:35 +0100606static void fsmc_read_buf_dma(struct fsmc_nand_data *host, u8 *buf,
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100607 int len)
Vipin Kumar4774fb02012-03-14 11:47:18 +0530608{
Vipin Kumar4774fb02012-03-14 11:47:18 +0530609 dma_xfer(host, buf, len, DMA_FROM_DEVICE);
610}
611
612/*
613 * fsmc_write_buf_dma - write buffer to chip
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100614 * @host: FSMC NAND controller
Vipin Kumar4774fb02012-03-14 11:47:18 +0530615 * @buf: data buffer
616 * @len: number of bytes to write
617 */
Boris Brezillonfc43f452018-11-20 10:02:35 +0100618static void fsmc_write_buf_dma(struct fsmc_nand_data *host, const u8 *buf,
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100619 int len)
Vipin Kumar4774fb02012-03-14 11:47:18 +0530620{
Vipin Kumar4774fb02012-03-14 11:47:18 +0530621 dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
622}
623
Miquel Raynal4da712e2018-02-16 15:22:48 +0100624/*
625 * fsmc_exec_op - hook called by the core to execute NAND operations
626 *
627 * This controller is simple enough and thus does not need to use the parser
628 * provided by the core, instead, handle every situation here.
629 */
630static int fsmc_exec_op(struct nand_chip *chip, const struct nand_operation *op,
631 bool check_only)
632{
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100633 struct fsmc_nand_data *host = nand_to_fsmc(chip);
Miquel Raynal4da712e2018-02-16 15:22:48 +0100634 const struct nand_op_instr *instr = NULL;
635 int ret = 0;
636 unsigned int op_id;
637 int i;
638
Boris Brezillonce446b42020-04-18 21:42:17 +0200639 if (check_only)
640 return 0;
641
Miquel Raynal4da712e2018-02-16 15:22:48 +0100642 pr_debug("Executing operation [%d instructions]:\n", op->ninstrs);
Boris Brezillon550b9fc2018-11-11 08:55:17 +0100643
Miquel Raynal4da712e2018-02-16 15:22:48 +0100644 for (op_id = 0; op_id < op->ninstrs; op_id++) {
645 instr = &op->instrs[op_id];
646
Sascha Hauerbf828322019-05-21 09:06:31 +0200647 nand_op_trace(" ", instr);
648
Miquel Raynal4da712e2018-02-16 15:22:48 +0100649 switch (instr->type) {
650 case NAND_OP_CMD_INSTR:
Miquel Raynal4da712e2018-02-16 15:22:48 +0100651 writeb_relaxed(instr->ctx.cmd.opcode, host->cmd_va);
652 break;
653
654 case NAND_OP_ADDR_INSTR:
Miquel Raynal4da712e2018-02-16 15:22:48 +0100655 for (i = 0; i < instr->ctx.addr.naddrs; i++)
656 writeb_relaxed(instr->ctx.addr.addrs[i],
657 host->addr_va);
658 break;
659
660 case NAND_OP_DATA_IN_INSTR:
Miquel Raynal4da712e2018-02-16 15:22:48 +0100661 if (host->mode == USE_DMA_ACCESS)
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100662 fsmc_read_buf_dma(host, instr->ctx.data.buf.in,
Miquel Raynal4da712e2018-02-16 15:22:48 +0100663 instr->ctx.data.len);
664 else
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100665 fsmc_read_buf(host, instr->ctx.data.buf.in,
Miquel Raynal4da712e2018-02-16 15:22:48 +0100666 instr->ctx.data.len);
667 break;
668
669 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynal4da712e2018-02-16 15:22:48 +0100670 if (host->mode == USE_DMA_ACCESS)
Boris Brezillonfc43f452018-11-20 10:02:35 +0100671 fsmc_write_buf_dma(host,
672 instr->ctx.data.buf.out,
Miquel Raynal4da712e2018-02-16 15:22:48 +0100673 instr->ctx.data.len);
674 else
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100675 fsmc_write_buf(host, instr->ctx.data.buf.out,
Miquel Raynal4da712e2018-02-16 15:22:48 +0100676 instr->ctx.data.len);
677 break;
678
679 case NAND_OP_WAITRDY_INSTR:
Miquel Raynal4da712e2018-02-16 15:22:48 +0100680 ret = nand_soft_waitrdy(chip,
681 instr->ctx.waitrdy.timeout_ms);
682 break;
683 }
Herve Codinaa4ca0c42021-11-19 16:03:15 +0100684
685 if (instr->delay_ns)
686 ndelay(instr->delay_ns);
Miquel Raynal4da712e2018-02-16 15:22:48 +0100687 }
688
689 return ret;
690}
691
Vipin Kumar4774fb02012-03-14 11:47:18 +0530692/*
Linus Walleij6c009ab2010-09-13 00:35:22 +0200693 * fsmc_read_page_hwecc
Linus Walleij6c009ab2010-09-13 00:35:22 +0200694 * @chip: nand chip info structure
695 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -0700696 * @oob_required: caller expects OOB data read to chip->oob_poi
Linus Walleij6c009ab2010-09-13 00:35:22 +0200697 * @page: page number to read
698 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300699 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
Linus Walleij6c009ab2010-09-13 00:35:22 +0200700 * performed in a strict sequence as follows:
701 * data(512 byte) -> ecc(13 byte)
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300702 * After this read, fsmc hardware generates and reports error data bits(up to a
Linus Walleij6c009ab2010-09-13 00:35:22 +0200703 * max of 8 bits)
704 */
Boris Brezillonfc43f452018-11-20 10:02:35 +0100705static int fsmc_read_page_hwecc(struct nand_chip *chip, u8 *buf,
Boris Brezillonb9761682018-09-06 14:05:20 +0200706 int oob_required, int page)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200707{
Boris Brezillonb9761682018-09-06 14:05:20 +0200708 struct mtd_info *mtd = nand_to_mtd(chip);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200709 int i, j, s, stat, eccsize = chip->ecc.size;
710 int eccbytes = chip->ecc.bytes;
711 int eccsteps = chip->ecc.steps;
Boris Brezillonfc43f452018-11-20 10:02:35 +0100712 u8 *p = buf;
713 u8 *ecc_calc = chip->ecc.calc_buf;
714 u8 *ecc_code = chip->ecc.code_buf;
Gustavo A. R. Silva41d6f0d2018-10-10 17:58:58 +0200715 int off, len, ret, group = 0;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200716 /*
Boris Brezillonfc43f452018-11-20 10:02:35 +0100717 * ecc_oob is intentionally taken as u16. In 16bit devices, we
Linus Walleij6c009ab2010-09-13 00:35:22 +0200718 * end up reading 14 bytes (7 words) from oob. The local array is
719 * to maintain word alignment
720 */
Boris Brezillonfc43f452018-11-20 10:02:35 +0100721 u16 ecc_oob[7];
722 u8 *oob = (u8 *)&ecc_oob[0];
Mike Dunn3f91e942012-04-25 12:06:09 -0700723 unsigned int max_bitflips = 0;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200724
725 for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100726 nand_read_page_op(chip, page, s * eccsize, NULL, 0);
Boris Brezillonec476362018-09-06 14:05:17 +0200727 chip->ecc.hwctl(chip, NAND_ECC_READ);
Miquel Raynalb451f5b2020-05-07 12:52:36 +0200728 ret = nand_read_data_op(chip, p, eccsize, false, false);
Gustavo A. R. Silva41d6f0d2018-10-10 17:58:58 +0200729 if (ret)
730 return ret;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200731
732 for (j = 0; j < eccbytes;) {
Boris Brezillon04a123a2016-02-09 15:01:21 +0100733 struct mtd_oob_region oobregion;
Boris Brezillon04a123a2016-02-09 15:01:21 +0100734
735 ret = mtd_ooblayout_ecc(mtd, group++, &oobregion);
736 if (ret)
737 return ret;
738
739 off = oobregion.offset;
740 len = oobregion.length;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200741
742 /*
Vipin Kumar4cbe1bf02012-03-14 11:47:09 +0530743 * length is intentionally kept a higher multiple of 2
744 * to read at least 13 bytes even in case of 16 bit NAND
745 * devices
746 */
Vipin Kumaraea686b2012-03-14 11:47:10 +0530747 if (chip->options & NAND_BUSWIDTH_16)
748 len = roundup(len, 2);
749
Boris Brezillon97d90da2017-11-30 18:01:29 +0100750 nand_read_oob_op(chip, page, off, oob + j, len);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200751 j += len;
752 }
753
Vipin Kumar519300c2012-03-07 17:00:49 +0530754 memcpy(&ecc_code[i], oob, chip->ecc.bytes);
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200755 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200756
Boris Brezillon00da2ea2018-09-06 14:05:19 +0200757 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -0700758 if (stat < 0) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200759 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -0700760 } else {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200761 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -0700762 max_bitflips = max_t(unsigned int, max_bitflips, stat);
763 }
Linus Walleij6c009ab2010-09-13 00:35:22 +0200764 }
765
Mike Dunn3f91e942012-04-25 12:06:09 -0700766 return max_bitflips;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200767}
768
769/*
Armando Visconti753e0132012-03-07 17:00:54 +0530770 * fsmc_bch8_correct_data
Linus Walleij6c009ab2010-09-13 00:35:22 +0200771 * @mtd: mtd info structure
772 * @dat: buffer of read data
773 * @read_ecc: ecc read from device spare area
774 * @calc_ecc: ecc calculated from read data
775 *
776 * calc_ecc is a 104 bit information containing maximum of 8 error
Boris Brezillonfc43f452018-11-20 10:02:35 +0100777 * offset information of 13 bits each in 512 bytes of read data.
Linus Walleij6c009ab2010-09-13 00:35:22 +0200778 */
Boris Brezillonfc43f452018-11-20 10:02:35 +0100779static int fsmc_bch8_correct_data(struct nand_chip *chip, u8 *dat,
780 u8 *read_ecc, u8 *calc_ecc)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200781{
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100782 struct fsmc_nand_data *host = nand_to_fsmc(chip);
Boris Brezillonfc43f452018-11-20 10:02:35 +0100783 u32 err_idx[8];
784 u32 num_err, i;
785 u32 ecc1, ecc2, ecc3, ecc4;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200786
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100787 num_err = (readl_relaxed(host->regs_va + STS) >> 10) & 0xF;
Vipin Kumar519300c2012-03-07 17:00:49 +0530788
789 /* no bit flipping */
790 if (likely(num_err == 0))
791 return 0;
792
793 /* too many errors */
794 if (unlikely(num_err > 8)) {
795 /*
796 * This is a temporary erase check. A newly erased page read
797 * would result in an ecc error because the oob data is also
798 * erased to FF and the calculated ecc for an FF data is not
799 * FF..FF.
800 * This is a workaround to skip performing correction in case
801 * data is FF..FF
802 *
803 * Logic:
804 * For every page, each bit written as 0 is counted until these
805 * number of bits are greater than 8 (the maximum correction
806 * capability of FSMC for each 512 + 13 bytes)
807 */
808
809 int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
810 int bits_data = count_written_bits(dat, chip->ecc.size, 8);
811
812 if ((bits_ecc + bits_data) <= 8) {
813 if (bits_data)
814 memset(dat, 0xff, chip->ecc.size);
815 return bits_data;
816 }
817
818 return -EBADMSG;
819 }
820
Linus Walleij6c009ab2010-09-13 00:35:22 +0200821 /*
822 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
823 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
824 *
825 * calc_ecc is a 104 bit information containing maximum of 8 error
Boris Brezillonfc43f452018-11-20 10:02:35 +0100826 * offset information of 13 bits each. calc_ecc is copied into a
827 * u64 array and error offset indexes are populated in err_idx
Linus Walleij6c009ab2010-09-13 00:35:22 +0200828 * array
829 */
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100830 ecc1 = readl_relaxed(host->regs_va + ECC1);
831 ecc2 = readl_relaxed(host->regs_va + ECC2);
832 ecc3 = readl_relaxed(host->regs_va + ECC3);
833 ecc4 = readl_relaxed(host->regs_va + STS);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200834
Armando Visconti753e0132012-03-07 17:00:54 +0530835 err_idx[0] = (ecc1 >> 0) & 0x1FFF;
836 err_idx[1] = (ecc1 >> 13) & 0x1FFF;
837 err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
838 err_idx[3] = (ecc2 >> 7) & 0x1FFF;
839 err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
840 err_idx[5] = (ecc3 >> 1) & 0x1FFF;
841 err_idx[6] = (ecc3 >> 14) & 0x1FFF;
842 err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200843
844 i = 0;
845 while (num_err--) {
Fenghua Yu7c26e6e2019-12-20 16:05:55 -0800846 err_idx[i] ^= 3;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200847
Vipin Kumarb533f8d2012-03-14 11:47:11 +0530848 if (err_idx[i] < chip->ecc.size * 8) {
Fenghua Yu7c26e6e2019-12-20 16:05:55 -0800849 int err = err_idx[i];
850
851 dat[err >> 3] ^= BIT(err & 7);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200852 i++;
853 }
854 }
855 return i;
856}
857
Vipin Kumar4774fb02012-03-14 11:47:18 +0530858static bool filter(struct dma_chan *chan, void *slave)
859{
860 chan->private = slave;
861 return true;
862}
863
Bill Pemberton06f25512012-11-19 13:23:07 -0500864static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100865 struct fsmc_nand_data *host,
866 struct nand_chip *nand)
Stefan Roeseeea62812012-03-16 10:19:31 +0100867{
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100868 struct device_node *np = pdev->dev.of_node;
Stefan Roeseeea62812012-03-16 10:19:31 +0100869 u32 val;
Stefan Roese62b57f42015-03-19 14:34:29 +0100870 int ret;
Stefan Roeseeea62812012-03-16 10:19:31 +0100871
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100872 nand->options = 0;
Thomas Petazzoniee568742017-03-21 11:03:53 +0100873
Stefan Roeseeea62812012-03-16 10:19:31 +0100874 if (!of_property_read_u32(np, "bank-width", &val)) {
875 if (val == 2) {
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100876 nand->options |= NAND_BUSWIDTH_16;
Stefan Roeseeea62812012-03-16 10:19:31 +0100877 } else if (val != 1) {
878 dev_err(&pdev->dev, "invalid bank-width %u\n", val);
879 return -EINVAL;
880 }
881 }
Thomas Petazzoniee568742017-03-21 11:03:53 +0100882
Stefan Roeseeea62812012-03-16 10:19:31 +0100883 if (of_get_property(np, "nand-skip-bbtscan", NULL))
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100884 nand->options |= NAND_SKIP_BBTSCAN;
Stefan Roeseeea62812012-03-16 10:19:31 +0100885
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100886 host->dev_timings = devm_kzalloc(&pdev->dev,
Boris Brezillonfc43f452018-11-20 10:02:35 +0100887 sizeof(*host->dev_timings),
888 GFP_KERNEL);
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100889 if (!host->dev_timings)
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200890 return -ENOMEM;
Boris Brezillonfc43f452018-11-20 10:02:35 +0100891
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100892 ret = of_property_read_u8_array(np, "timings", (u8 *)host->dev_timings,
Boris Brezillonfc43f452018-11-20 10:02:35 +0100893 sizeof(*host->dev_timings));
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200894 if (ret)
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100895 host->dev_timings = NULL;
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200896
897 /* Set default NAND bank to 0 */
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100898 host->bank = 0;
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200899 if (!of_property_read_u32(np, "bank", &val)) {
900 if (val > 3) {
901 dev_err(&pdev->dev, "invalid bank %u\n", val);
902 return -EINVAL;
903 }
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100904 host->bank = val;
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200905 }
Stefan Roeseeea62812012-03-16 10:19:31 +0100906 return 0;
907}
Stefan Roeseeea62812012-03-16 10:19:31 +0100908
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200909static int fsmc_nand_attach_chip(struct nand_chip *nand)
910{
911 struct mtd_info *mtd = nand_to_mtd(nand);
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100912 struct fsmc_nand_data *host = nand_to_fsmc(nand);
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200913
Miquel Raynal98591a62020-11-13 13:34:13 +0100914 if (nand->ecc.engine_type == NAND_ECC_ENGINE_TYPE_INVALID)
915 nand->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
916
917 if (!nand->ecc.size)
918 nand->ecc.size = 512;
919
920 if (AMBA_REV_BITS(host->pid) >= 8) {
921 nand->ecc.read_page = fsmc_read_page_hwecc;
922 nand->ecc.calculate = fsmc_read_hwecc_ecc4;
923 nand->ecc.correct = fsmc_bch8_correct_data;
924 nand->ecc.bytes = 13;
925 nand->ecc.strength = 8;
926 }
927
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200928 if (AMBA_REV_BITS(host->pid) >= 8) {
929 switch (mtd->oobsize) {
930 case 16:
931 case 64:
932 case 128:
933 case 224:
934 case 256:
935 break;
936 default:
937 dev_warn(host->dev,
938 "No oob scheme defined for oobsize %d\n",
939 mtd->oobsize);
940 return -EINVAL;
941 }
942
943 mtd_set_ooblayout(mtd, &fsmc_ecc4_ooblayout_ops);
944
945 return 0;
946 }
947
Miquel Raynalbace41f2020-08-27 10:51:58 +0200948 switch (nand->ecc.engine_type) {
949 case NAND_ECC_ENGINE_TYPE_ON_HOST:
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200950 dev_info(host->dev, "Using 1-bit HW ECC scheme\n");
951 nand->ecc.calculate = fsmc_read_hwecc_ecc1;
Miquel Raynalad9ffdc2021-04-13 18:18:35 +0200952 nand->ecc.correct = fsmc_correct_ecc1;
Miquel Raynal98591a62020-11-13 13:34:13 +0100953 nand->ecc.hwctl = fsmc_enable_hwecc;
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200954 nand->ecc.bytes = 3;
955 nand->ecc.strength = 1;
Boris Brezillon309600c2018-09-04 16:23:28 +0200956 nand->ecc.options |= NAND_ECC_SOFT_HAMMING_SM_ORDER;
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200957 break;
958
Miquel Raynalbace41f2020-08-27 10:51:58 +0200959 case NAND_ECC_ENGINE_TYPE_SOFT:
Miquel Raynale0a564a2020-08-27 10:51:50 +0200960 if (nand->ecc.algo == NAND_ECC_ALGO_BCH) {
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200961 dev_info(host->dev,
962 "Using 4-bit SW BCH ECC scheme\n");
963 break;
964 }
Gustavo A. R. Silvafe1bc212021-03-05 02:25:59 -0600965 break;
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200966
Miquel Raynalbace41f2020-08-27 10:51:58 +0200967 case NAND_ECC_ENGINE_TYPE_ON_DIE:
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200968 break;
969
970 default:
971 dev_err(host->dev, "Unsupported ECC mode!\n");
972 return -ENOTSUPP;
973 }
974
975 /*
976 * Don't set layout for BCH4 SW ECC. This will be
Miquel Raynal3c0fe362020-09-30 01:01:08 +0200977 * generated later during BCH initialization.
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200978 */
Miquel Raynalbace41f2020-08-27 10:51:58 +0200979 if (nand->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) {
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200980 switch (mtd->oobsize) {
981 case 16:
982 case 64:
983 case 128:
984 mtd_set_ooblayout(mtd,
985 &fsmc_ecc1_ooblayout_ops);
986 break;
987 default:
988 dev_warn(host->dev,
989 "No oob scheme defined for oobsize %d\n",
990 mtd->oobsize);
991 return -EINVAL;
992 }
993 }
994
995 return 0;
996}
997
998static const struct nand_controller_ops fsmc_nand_controller_ops = {
999 .attach_chip = fsmc_nand_attach_chip,
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001000 .exec_op = fsmc_exec_op,
Miquel Raynal4c466672020-05-29 13:13:13 +02001001 .setup_interface = fsmc_setup_interface,
Miquel Raynal3bbddfa2018-07-20 17:14:59 +02001002};
1003
Linus Walleijab3ab7b2019-01-26 14:10:56 +01001004/**
1005 * fsmc_nand_disable() - Disables the NAND bank
1006 * @host: The instance to disable
1007 */
1008static void fsmc_nand_disable(struct fsmc_nand_data *host)
1009{
1010 u32 val;
1011
1012 val = readl(host->regs_va + FSMC_PC);
1013 val &= ~FSMC_ENABLE;
1014 writel(val, host->regs_va + FSMC_PC);
1015}
1016
Linus Walleij6c009ab2010-09-13 00:35:22 +02001017/*
1018 * fsmc_nand_probe - Probe function
1019 * @pdev: platform device structure
1020 */
1021static int __init fsmc_nand_probe(struct platform_device *pdev)
1022{
Linus Walleij6c009ab2010-09-13 00:35:22 +02001023 struct fsmc_nand_data *host;
1024 struct mtd_info *mtd;
1025 struct nand_chip *nand;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001026 struct resource *res;
Miquel Raynal4df6ed42018-02-16 15:22:47 +01001027 void __iomem *base;
Vipin Kumar4774fb02012-03-14 11:47:18 +05301028 dma_cap_mask_t mask;
Linus Walleij4ad916b2010-11-29 13:52:06 +01001029 int ret = 0;
Linus Walleij593cd872010-11-29 13:52:19 +01001030 u32 pid;
1031 int i;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001032
Linus Walleij6c009ab2010-09-13 00:35:22 +02001033 /* Allocate memory for the device structure (and zero it) */
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301034 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Jingoo Hand9a21ae2013-12-26 12:16:38 +09001035 if (!host)
Linus Walleij6c009ab2010-09-13 00:35:22 +02001036 return -ENOMEM;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001037
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +01001038 nand = &host->nand;
1039
1040 ret = fsmc_nand_probe_config_dt(pdev, host, nand);
1041 if (ret)
1042 return ret;
1043
Linus Walleij6c009ab2010-09-13 00:35:22 +02001044 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
Thierry Redingb0de7742013-01-21 11:09:12 +01001045 host->data_va = devm_ioremap_resource(&pdev->dev, res);
1046 if (IS_ERR(host->data_va))
1047 return PTR_ERR(host->data_va);
Stefan Roesecbf29b82015-10-02 12:40:20 +02001048
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +02001049 host->data_pa = (dma_addr_t)res->start;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001050
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +02001051 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
Thierry Redingb0de7742013-01-21 11:09:12 +01001052 host->addr_va = devm_ioremap_resource(&pdev->dev, res);
1053 if (IS_ERR(host->addr_va))
1054 return PTR_ERR(host->addr_va);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001055
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +02001056 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
Thierry Redingb0de7742013-01-21 11:09:12 +01001057 host->cmd_va = devm_ioremap_resource(&pdev->dev, res);
1058 if (IS_ERR(host->cmd_va))
1059 return PTR_ERR(host->cmd_va);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001060
1061 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
Miquel Raynal4df6ed42018-02-16 15:22:47 +01001062 base = devm_ioremap_resource(&pdev->dev, res);
1063 if (IS_ERR(base))
1064 return PTR_ERR(base);
1065
1066 host->regs_va = base + FSMC_NOR_REG_SIZE +
1067 (host->bank * FSMC_NAND_BANK_SZ);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001068
Thomas Petazzonifb8ed2c2017-03-21 11:04:03 +01001069 host->clk = devm_clk_get(&pdev->dev, NULL);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001070 if (IS_ERR(host->clk)) {
1071 dev_err(&pdev->dev, "failed to fetch block clock\n");
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301072 return PTR_ERR(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001073 }
1074
Viresh Kumare25da1c2012-04-17 17:07:57 +05301075 ret = clk_prepare_enable(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001076 if (ret)
Thomas Petazzonifb8ed2c2017-03-21 11:04:03 +01001077 return ret;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001078
Linus Walleij593cd872010-11-29 13:52:19 +01001079 /*
1080 * This device ID is actually a common AMBA ID as used on the
1081 * AMBA PrimeCell bus. However it is not a PrimeCell.
1082 */
1083 for (pid = 0, i = 0; i < 4; i++)
Boris Brezillonfc43f452018-11-20 10:02:35 +01001084 pid |= (readl(base + resource_size(res) - 0x20 + 4 * i) &
1085 255) << (i * 8);
1086
Linus Walleij593cd872010-11-29 13:52:19 +01001087 host->pid = pid;
Boris Brezillonfc43f452018-11-20 10:02:35 +01001088
1089 dev_info(&pdev->dev,
1090 "FSMC device partno %03x, manufacturer %02x, revision %02x, config %02x\n",
Linus Walleij593cd872010-11-29 13:52:19 +01001091 AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
1092 AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
1093
Vipin Kumar712c4ad2012-03-14 11:47:16 +05301094 host->dev = &pdev->dev;
Vipin Kumar4774fb02012-03-14 11:47:18 +05301095
1096 if (host->mode == USE_DMA_ACCESS)
1097 init_completion(&host->dma_access_complete);
1098
Linus Walleij6c009ab2010-09-13 00:35:22 +02001099 /* Link all private pointers */
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001100 mtd = nand_to_mtd(&host->nand);
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +01001101 nand_set_flash_node(nand, pdev->dev.of_node);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001102
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001103 mtd->dev.parent = &pdev->dev;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001104
Vipin Kumar467e6e72012-03-14 11:47:12 +05301105 nand->badblockbits = 7;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001106
Miquel Raynal4da712e2018-02-16 15:22:48 +01001107 if (host->mode == USE_DMA_ACCESS) {
Vipin Kumar4774fb02012-03-14 11:47:18 +05301108 dma_cap_zero(mask);
1109 dma_cap_set(DMA_MEMCPY, mask);
Thomas Petazzonifeb1e572017-03-21 11:03:59 +01001110 host->read_dma_chan = dma_request_channel(mask, filter, NULL);
Vipin Kumar4774fb02012-03-14 11:47:18 +05301111 if (!host->read_dma_chan) {
1112 dev_err(&pdev->dev, "Unable to get read dma channel\n");
Dan Carpentere7a97522021-02-15 18:58:49 +03001113 ret = -ENODEV;
Miquel Raynal43fab012018-04-21 20:00:36 +02001114 goto disable_clk;
Vipin Kumar4774fb02012-03-14 11:47:18 +05301115 }
Thomas Petazzonifeb1e572017-03-21 11:03:59 +01001116 host->write_dma_chan = dma_request_channel(mask, filter, NULL);
Vipin Kumar4774fb02012-03-14 11:47:18 +05301117 if (!host->write_dma_chan) {
1118 dev_err(&pdev->dev, "Unable to get write dma channel\n");
Dan Carpentere7a97522021-02-15 18:58:49 +03001119 ret = -ENODEV;
Miquel Raynal43fab012018-04-21 20:00:36 +02001120 goto release_dma_read_chan;
Vipin Kumar4774fb02012-03-14 11:47:18 +05301121 }
Vipin Kumar604e7542012-03-14 11:47:17 +05301122 }
1123
Boris Brezillon7a08dba2018-11-11 08:55:24 +01001124 if (host->dev_timings) {
Thomas Petazzonid9fb0792017-04-29 10:52:35 +02001125 fsmc_nand_setup(host, host->dev_timings);
Boris Brezillon7a08dba2018-11-11 08:55:24 +01001126 nand->options |= NAND_KEEP_TIMINGS;
1127 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001128
Boris Brezillonad711482018-11-20 10:02:33 +01001129 nand_controller_init(&host->base);
1130 host->base.ops = &fsmc_nand_controller_ops;
1131 nand->controller = &host->base;
1132
Linus Walleij6c009ab2010-09-13 00:35:22 +02001133 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001134 * Scan to find existence of the device
Linus Walleij6c009ab2010-09-13 00:35:22 +02001135 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02001136 ret = nand_scan(nand, 1);
Masahiro Yamadaad5678e2016-11-04 19:43:00 +09001137 if (ret)
Miquel Raynal43fab012018-04-21 20:00:36 +02001138 goto release_dma_write_chan;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001139
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001140 mtd->name = "nand";
Thomas Petazzoniede29a02017-03-21 11:04:00 +01001141 ret = mtd_device_register(mtd, NULL, 0);
Jamie Iles99335d02011-05-23 10:23:23 +01001142 if (ret)
Miquel Raynal682cae22018-04-21 20:00:37 +02001143 goto cleanup_nand;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001144
1145 platform_set_drvdata(pdev, host);
1146 dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
Miquel Raynal43fab012018-04-21 20:00:36 +02001147
Linus Walleij6c009ab2010-09-13 00:35:22 +02001148 return 0;
1149
Miquel Raynal682cae22018-04-21 20:00:37 +02001150cleanup_nand:
1151 nand_cleanup(nand);
Miquel Raynal43fab012018-04-21 20:00:36 +02001152release_dma_write_chan:
Vipin Kumar4774fb02012-03-14 11:47:18 +05301153 if (host->mode == USE_DMA_ACCESS)
1154 dma_release_channel(host->write_dma_chan);
Miquel Raynal43fab012018-04-21 20:00:36 +02001155release_dma_read_chan:
Vipin Kumar4774fb02012-03-14 11:47:18 +05301156 if (host->mode == USE_DMA_ACCESS)
1157 dma_release_channel(host->read_dma_chan);
Miquel Raynal43fab012018-04-21 20:00:36 +02001158disable_clk:
Linus Walleijab3ab7b2019-01-26 14:10:56 +01001159 fsmc_nand_disable(host);
Viresh Kumare25da1c2012-04-17 17:07:57 +05301160 clk_disable_unprepare(host->clk);
Miquel Raynal43fab012018-04-21 20:00:36 +02001161
Linus Walleij6c009ab2010-09-13 00:35:22 +02001162 return ret;
1163}
1164
1165/*
1166 * Clean up routine
1167 */
1168static int fsmc_nand_remove(struct platform_device *pdev)
1169{
1170 struct fsmc_nand_data *host = platform_get_drvdata(pdev);
1171
Linus Walleij6c009ab2010-09-13 00:35:22 +02001172 if (host) {
Miquel Raynal9cc02f42020-05-19 14:59:50 +02001173 struct nand_chip *chip = &host->nand;
1174 int ret;
1175
1176 ret = mtd_device_unregister(nand_to_mtd(chip));
1177 WARN_ON(ret);
1178 nand_cleanup(chip);
Linus Walleijab3ab7b2019-01-26 14:10:56 +01001179 fsmc_nand_disable(host);
Vipin Kumar4774fb02012-03-14 11:47:18 +05301180
1181 if (host->mode == USE_DMA_ACCESS) {
1182 dma_release_channel(host->write_dma_chan);
1183 dma_release_channel(host->read_dma_chan);
1184 }
Viresh Kumare25da1c2012-04-17 17:07:57 +05301185 clk_disable_unprepare(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001186 }
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301187
Linus Walleij6c009ab2010-09-13 00:35:22 +02001188 return 0;
1189}
1190
Jingoo Han80ce4dd2013-03-26 15:53:48 +09001191#ifdef CONFIG_PM_SLEEP
Linus Walleij6c009ab2010-09-13 00:35:22 +02001192static int fsmc_nand_suspend(struct device *dev)
1193{
1194 struct fsmc_nand_data *host = dev_get_drvdata(dev);
Boris Brezillonfc43f452018-11-20 10:02:35 +01001195
Linus Walleij6c009ab2010-09-13 00:35:22 +02001196 if (host)
Viresh Kumare25da1c2012-04-17 17:07:57 +05301197 clk_disable_unprepare(host->clk);
Boris Brezillonfc43f452018-11-20 10:02:35 +01001198
Linus Walleij6c009ab2010-09-13 00:35:22 +02001199 return 0;
1200}
1201
1202static int fsmc_nand_resume(struct device *dev)
1203{
1204 struct fsmc_nand_data *host = dev_get_drvdata(dev);
Boris Brezillonfc43f452018-11-20 10:02:35 +01001205
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301206 if (host) {
Viresh Kumare25da1c2012-04-17 17:07:57 +05301207 clk_prepare_enable(host->clk);
Thomas Petazzonid9fb0792017-04-29 10:52:35 +02001208 if (host->dev_timings)
1209 fsmc_nand_setup(host, host->dev_timings);
Linus Walleij30c72ab2019-01-26 14:10:55 +01001210 nand_reset(&host->nand, 0);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301211 }
Boris Brezillonfc43f452018-11-20 10:02:35 +01001212
Linus Walleij6c009ab2010-09-13 00:35:22 +02001213 return 0;
1214}
Jingoo Han80ce4dd2013-03-26 15:53:48 +09001215#endif
Linus Walleij6c009ab2010-09-13 00:35:22 +02001216
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301217static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001218
Stefan Roeseeea62812012-03-16 10:19:31 +01001219static const struct of_device_id fsmc_nand_id_table[] = {
1220 { .compatible = "st,spear600-fsmc-nand" },
Linus Walleijba785202013-01-05 22:28:32 +01001221 { .compatible = "stericsson,fsmc-nand" },
Stefan Roeseeea62812012-03-16 10:19:31 +01001222 {}
1223};
1224MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);
Stefan Roeseeea62812012-03-16 10:19:31 +01001225
Linus Walleij6c009ab2010-09-13 00:35:22 +02001226static struct platform_driver fsmc_nand_driver = {
1227 .remove = fsmc_nand_remove,
1228 .driver = {
Linus Walleij6c009ab2010-09-13 00:35:22 +02001229 .name = "fsmc-nand",
Thomas Petazzoni33575b22017-03-21 11:04:05 +01001230 .of_match_table = fsmc_nand_id_table,
Linus Walleij6c009ab2010-09-13 00:35:22 +02001231 .pm = &fsmc_nand_pm_ops,
Linus Walleij6c009ab2010-09-13 00:35:22 +02001232 },
1233};
1234
Jingoo Han307d2a512013-03-05 13:30:36 +09001235module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001236
Boris Brezillonbb696342018-11-20 10:02:34 +01001237MODULE_LICENSE("GPL v2");
Linus Walleij6c009ab2010-09-13 00:35:22 +02001238MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
1239MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");