blob: 01044b858a9348522b07fe105c636fff68c737a2 [file] [log] [blame]
Linus Walleij6c009ab2010-09-13 00:35:22 +02001/*
Linus Walleij6c009ab2010-09-13 00:35:22 +02002 * ST Microelectronics
3 * Flexible Static Memory Controller (FSMC)
4 * Driver for NAND portions
5 *
6 * Copyright © 2010 ST Microelectronics
7 * Vipin Kumar <vipin.kumar@st.com>
8 * Ashish Priyadarshi
9 *
Boris Brezillon187c54482018-02-05 23:02:02 +010010 * Based on drivers/mtd/nand/nomadik_nand.c (removed in v3.8)
Boris Brezillon7b6afee2018-02-05 23:02:03 +010011 * Copyright © 2007 STMicroelectronics Pvt. Ltd.
12 * Copyright © 2009 Alessandro Rubini
Linus Walleij6c009ab2010-09-13 00:35:22 +020013 *
14 * This file is licensed under the terms of the GNU General Public
15 * License version 2. This program is licensed "as is" without any
16 * warranty of any kind, whether express or implied.
17 */
18
19#include <linux/clk.h>
Vipin Kumar4774fb02012-03-14 11:47:18 +053020#include <linux/completion.h>
21#include <linux/dmaengine.h>
22#include <linux/dma-direction.h>
23#include <linux/dma-mapping.h>
Linus Walleij6c009ab2010-09-13 00:35:22 +020024#include <linux/err.h>
25#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/resource.h>
28#include <linux/sched.h>
29#include <linux/types.h>
30#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020031#include <linux/mtd/rawnand.h>
Linus Walleij6c009ab2010-09-13 00:35:22 +020032#include <linux/mtd/nand_ecc.h>
33#include <linux/platform_device.h>
Stefan Roeseeea62812012-03-16 10:19:31 +010034#include <linux/of.h>
Linus Walleij6c009ab2010-09-13 00:35:22 +020035#include <linux/mtd/partitions.h>
36#include <linux/io.h>
37#include <linux/slab.h>
Linus Walleij593cd872010-11-29 13:52:19 +010038#include <linux/amba/bus.h>
Linus Walleij6c009ab2010-09-13 00:35:22 +020039#include <mtd/mtd-abi.h>
40
Linus Walleij4404d7d2016-12-18 12:34:55 +010041/* fsmc controller registers for NOR flash */
42#define CTRL 0x0
43 /* ctrl register definitions */
44 #define BANK_ENABLE (1 << 0)
45 #define MUXED (1 << 1)
46 #define NOR_DEV (2 << 2)
47 #define WIDTH_8 (0 << 4)
48 #define WIDTH_16 (1 << 4)
49 #define RSTPWRDWN (1 << 6)
50 #define WPROT (1 << 7)
51 #define WRT_ENABLE (1 << 12)
52 #define WAIT_ENB (1 << 13)
53
54#define CTRL_TIM 0x4
55 /* ctrl_tim register definitions */
56
57#define FSMC_NOR_BANK_SZ 0x8
58#define FSMC_NOR_REG_SIZE 0x40
59
60#define FSMC_NOR_REG(base, bank, reg) (base + \
61 FSMC_NOR_BANK_SZ * (bank) + \
62 reg)
63
64/* fsmc controller registers for NAND flash */
Boris Brezillon8f3931e2018-07-09 22:09:34 +020065#define FSMC_PC 0x00
Linus Walleij4404d7d2016-12-18 12:34:55 +010066 /* pc register definitions */
67 #define FSMC_RESET (1 << 0)
68 #define FSMC_WAITON (1 << 1)
69 #define FSMC_ENABLE (1 << 2)
70 #define FSMC_DEVTYPE_NAND (1 << 3)
71 #define FSMC_DEVWID_8 (0 << 4)
72 #define FSMC_DEVWID_16 (1 << 4)
73 #define FSMC_ECCEN (1 << 6)
74 #define FSMC_ECCPLEN_512 (0 << 7)
75 #define FSMC_ECCPLEN_256 (1 << 7)
76 #define FSMC_TCLR_1 (1)
77 #define FSMC_TCLR_SHIFT (9)
78 #define FSMC_TCLR_MASK (0xF)
79 #define FSMC_TAR_1 (1)
80 #define FSMC_TAR_SHIFT (13)
81 #define FSMC_TAR_MASK (0xF)
82#define STS 0x04
83 /* sts register definitions */
84 #define FSMC_CODE_RDY (1 << 15)
85#define COMM 0x08
86 /* comm register definitions */
87 #define FSMC_TSET_0 0
88 #define FSMC_TSET_SHIFT 0
89 #define FSMC_TSET_MASK 0xFF
90 #define FSMC_TWAIT_6 6
91 #define FSMC_TWAIT_SHIFT 8
92 #define FSMC_TWAIT_MASK 0xFF
93 #define FSMC_THOLD_4 4
94 #define FSMC_THOLD_SHIFT 16
95 #define FSMC_THOLD_MASK 0xFF
96 #define FSMC_THIZ_1 1
97 #define FSMC_THIZ_SHIFT 24
98 #define FSMC_THIZ_MASK 0xFF
99#define ATTRIB 0x0C
100#define IOATA 0x10
101#define ECC1 0x14
102#define ECC2 0x18
103#define ECC3 0x1C
104#define FSMC_NAND_BANK_SZ 0x20
105
Linus Walleij4404d7d2016-12-18 12:34:55 +0100106#define FSMC_BUSY_WAIT_TIMEOUT (1 * HZ)
107
108struct fsmc_nand_timings {
109 uint8_t tclr;
110 uint8_t tar;
111 uint8_t thiz;
112 uint8_t thold;
113 uint8_t twait;
114 uint8_t tset;
115};
116
117enum access_mode {
118 USE_DMA_ACCESS = 1,
119 USE_WORD_ACCESS,
120};
121
122/**
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100123 * struct fsmc_nand_data - structure for FSMC NAND device state
124 *
125 * @pid: Part ID on the AMBA PrimeCell format
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100126 * @nand: Chip related info for a NAND flash.
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100127 *
128 * @bank: Bank number for probed device.
Boris Brezillon5b47f402018-11-20 10:02:31 +0100129 * @dev: Parent device
130 * @mode: Access mode
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100131 * @clk: Clock structure for FSMC.
132 *
133 * @read_dma_chan: DMA channel for read access
134 * @write_dma_chan: DMA channel for write access to NAND
135 * @dma_access_complete: Completion structure
136 *
Boris Brezillon5b47f402018-11-20 10:02:31 +0100137 * @dev_timings: NAND timings
138 *
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100139 * @data_pa: NAND Physical port for Data.
140 * @data_va: NAND port for Data.
141 * @cmd_va: NAND port for Command.
142 * @addr_va: NAND port for Address.
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100143 * @regs_va: Registers base address for a given bank.
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100144 */
145struct fsmc_nand_data {
146 u32 pid;
147 struct nand_chip nand;
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100148
149 unsigned int bank;
150 struct device *dev;
151 enum access_mode mode;
152 struct clk *clk;
153
154 /* DMA related objects */
155 struct dma_chan *read_dma_chan;
156 struct dma_chan *write_dma_chan;
157 struct completion dma_access_complete;
158
159 struct fsmc_nand_timings *dev_timings;
160
161 dma_addr_t data_pa;
162 void __iomem *data_va;
163 void __iomem *cmd_va;
164 void __iomem *addr_va;
165 void __iomem *regs_va;
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100166};
167
Boris Brezillon22b46952016-02-03 20:01:42 +0100168static int fsmc_ecc1_ooblayout_ecc(struct mtd_info *mtd, int section,
169 struct mtd_oob_region *oobregion)
170{
171 struct nand_chip *chip = mtd_to_nand(mtd);
172
173 if (section >= chip->ecc.steps)
174 return -ERANGE;
175
176 oobregion->offset = (section * 16) + 2;
177 oobregion->length = 3;
178
179 return 0;
180}
181
182static int fsmc_ecc1_ooblayout_free(struct mtd_info *mtd, int section,
183 struct mtd_oob_region *oobregion)
184{
185 struct nand_chip *chip = mtd_to_nand(mtd);
186
187 if (section >= chip->ecc.steps)
188 return -ERANGE;
189
190 oobregion->offset = (section * 16) + 8;
191
192 if (section < chip->ecc.steps - 1)
193 oobregion->length = 8;
194 else
195 oobregion->length = mtd->oobsize - oobregion->offset;
196
197 return 0;
198}
199
200static const struct mtd_ooblayout_ops fsmc_ecc1_ooblayout_ops = {
201 .ecc = fsmc_ecc1_ooblayout_ecc,
202 .free = fsmc_ecc1_ooblayout_free,
203};
204
Boris Brezillon04a123a2016-02-09 15:01:21 +0100205/*
206 * ECC placement definitions in oobfree type format.
207 * There are 13 bytes of ecc for every 512 byte block and it has to be read
208 * consecutively and immediately after the 512 byte data block for hardware to
209 * generate the error bit offsets in 512 byte data.
210 */
Boris Brezillon22b46952016-02-03 20:01:42 +0100211static int fsmc_ecc4_ooblayout_ecc(struct mtd_info *mtd, int section,
212 struct mtd_oob_region *oobregion)
213{
214 struct nand_chip *chip = mtd_to_nand(mtd);
215
216 if (section >= chip->ecc.steps)
217 return -ERANGE;
218
219 oobregion->length = chip->ecc.bytes;
220
221 if (!section && mtd->writesize <= 512)
222 oobregion->offset = 0;
223 else
224 oobregion->offset = (section * 16) + 2;
225
226 return 0;
227}
228
229static int fsmc_ecc4_ooblayout_free(struct mtd_info *mtd, int section,
230 struct mtd_oob_region *oobregion)
231{
232 struct nand_chip *chip = mtd_to_nand(mtd);
233
234 if (section >= chip->ecc.steps)
235 return -ERANGE;
236
237 oobregion->offset = (section * 16) + 15;
238
239 if (section < chip->ecc.steps - 1)
240 oobregion->length = 3;
241 else
242 oobregion->length = mtd->oobsize - oobregion->offset;
243
244 return 0;
245}
246
247static const struct mtd_ooblayout_ops fsmc_ecc4_ooblayout_ops = {
248 .ecc = fsmc_ecc4_ooblayout_ecc,
249 .free = fsmc_ecc4_ooblayout_free,
250};
251
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100252static inline struct fsmc_nand_data *nand_to_fsmc(struct nand_chip *chip)
Boris BREZILLON277af422015-12-10 08:59:46 +0100253{
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100254 return container_of(chip, struct fsmc_nand_data, nand);
Boris BREZILLON277af422015-12-10 08:59:46 +0100255}
256
Linus Walleij6c009ab2010-09-13 00:35:22 +0200257/*
Linus Walleij6c009ab2010-09-13 00:35:22 +0200258 * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
259 *
260 * This routine initializes timing parameters related to NAND memory access in
261 * FSMC registers
262 */
Thomas Petazzoni6335b502017-04-29 10:52:34 +0200263static void fsmc_nand_setup(struct fsmc_nand_data *host,
Thomas Petazzoni1debdb92017-04-29 10:52:36 +0200264 struct fsmc_nand_timings *tims)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200265{
266 uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530267 uint32_t tclr, tar, thiz, thold, twait, tset;
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530268
269 tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT;
270 tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT;
271 thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT;
272 thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT;
273 twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT;
274 tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200275
Thomas Petazzoni6335b502017-04-29 10:52:34 +0200276 if (host->nand.options & NAND_BUSWIDTH_16)
Boris Brezillon8f3931e2018-07-09 22:09:34 +0200277 writel_relaxed(value | FSMC_DEVWID_16,
278 host->regs_va + FSMC_PC);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200279 else
Boris Brezillon8f3931e2018-07-09 22:09:34 +0200280 writel_relaxed(value | FSMC_DEVWID_8, host->regs_va + FSMC_PC);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200281
Boris Brezillon8f3931e2018-07-09 22:09:34 +0200282 writel_relaxed(readl(host->regs_va + FSMC_PC) | tclr | tar,
283 host->regs_va + FSMC_PC);
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100284 writel_relaxed(thiz | thold | twait | tset, host->regs_va + COMM);
285 writel_relaxed(thiz | thold | twait | tset, host->regs_va + ATTRIB);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200286}
287
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200288static int fsmc_calc_timings(struct fsmc_nand_data *host,
289 const struct nand_sdr_timings *sdrt,
290 struct fsmc_nand_timings *tims)
291{
292 unsigned long hclk = clk_get_rate(host->clk);
293 unsigned long hclkn = NSEC_PER_SEC / hclk;
294 uint32_t thiz, thold, twait, tset;
295
296 if (sdrt->tRC_min < 30000)
297 return -EOPNOTSUPP;
298
299 tims->tar = DIV_ROUND_UP(sdrt->tAR_min / 1000, hclkn) - 1;
300 if (tims->tar > FSMC_TAR_MASK)
301 tims->tar = FSMC_TAR_MASK;
302 tims->tclr = DIV_ROUND_UP(sdrt->tCLR_min / 1000, hclkn) - 1;
303 if (tims->tclr > FSMC_TCLR_MASK)
304 tims->tclr = FSMC_TCLR_MASK;
305
306 thiz = sdrt->tCS_min - sdrt->tWP_min;
307 tims->thiz = DIV_ROUND_UP(thiz / 1000, hclkn);
308
309 thold = sdrt->tDH_min;
310 if (thold < sdrt->tCH_min)
311 thold = sdrt->tCH_min;
312 if (thold < sdrt->tCLH_min)
313 thold = sdrt->tCLH_min;
314 if (thold < sdrt->tWH_min)
315 thold = sdrt->tWH_min;
316 if (thold < sdrt->tALH_min)
317 thold = sdrt->tALH_min;
318 if (thold < sdrt->tREH_min)
319 thold = sdrt->tREH_min;
320 tims->thold = DIV_ROUND_UP(thold / 1000, hclkn);
321 if (tims->thold == 0)
322 tims->thold = 1;
323 else if (tims->thold > FSMC_THOLD_MASK)
324 tims->thold = FSMC_THOLD_MASK;
325
326 twait = max(sdrt->tRP_min, sdrt->tWP_min);
327 tims->twait = DIV_ROUND_UP(twait / 1000, hclkn) - 1;
328 if (tims->twait == 0)
329 tims->twait = 1;
330 else if (tims->twait > FSMC_TWAIT_MASK)
331 tims->twait = FSMC_TWAIT_MASK;
332
333 tset = max(sdrt->tCS_min - sdrt->tWP_min,
334 sdrt->tCEA_max - sdrt->tREA_max);
335 tims->tset = DIV_ROUND_UP(tset / 1000, hclkn) - 1;
336 if (tims->tset == 0)
337 tims->tset = 1;
338 else if (tims->tset > FSMC_TSET_MASK)
339 tims->tset = FSMC_TSET_MASK;
340
341 return 0;
342}
343
Boris Brezillon858838b2018-09-06 14:05:33 +0200344static int fsmc_setup_data_interface(struct nand_chip *nand, int csline,
Boris Brezillon104e4422017-03-16 09:35:58 +0100345 const struct nand_data_interface *conf)
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200346{
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200347 struct fsmc_nand_data *host = nand_get_controller_data(nand);
348 struct fsmc_nand_timings tims;
349 const struct nand_sdr_timings *sdrt;
350 int ret;
351
352 sdrt = nand_get_sdr_timings(conf);
353 if (IS_ERR(sdrt))
354 return PTR_ERR(sdrt);
355
356 ret = fsmc_calc_timings(host, sdrt, &tims);
357 if (ret)
358 return ret;
359
Boris Brezillon104e4422017-03-16 09:35:58 +0100360 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200361 return 0;
362
363 fsmc_nand_setup(host, &tims);
364
365 return 0;
366}
367
Linus Walleij6c009ab2010-09-13 00:35:22 +0200368/*
369 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
370 */
Boris Brezillonec476362018-09-06 14:05:17 +0200371static void fsmc_enable_hwecc(struct nand_chip *chip, int mode)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200372{
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100373 struct fsmc_nand_data *host = nand_to_fsmc(chip);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200374
Boris Brezillon8f3931e2018-07-09 22:09:34 +0200375 writel_relaxed(readl(host->regs_va + FSMC_PC) & ~FSMC_ECCPLEN_256,
376 host->regs_va + FSMC_PC);
377 writel_relaxed(readl(host->regs_va + FSMC_PC) & ~FSMC_ECCEN,
378 host->regs_va + FSMC_PC);
379 writel_relaxed(readl(host->regs_va + FSMC_PC) | FSMC_ECCEN,
380 host->regs_va + FSMC_PC);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200381}
382
383/*
384 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300385 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200386 * max of 8-bits)
387 */
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200388static int fsmc_read_hwecc_ecc4(struct nand_chip *chip, const uint8_t *data,
Linus Walleij6c009ab2010-09-13 00:35:22 +0200389 uint8_t *ecc)
390{
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100391 struct fsmc_nand_data *host = nand_to_fsmc(chip);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200392 uint32_t ecc_tmp;
393 unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
394
395 do {
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100396 if (readl_relaxed(host->regs_va + STS) & FSMC_CODE_RDY)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200397 break;
398 else
399 cond_resched();
400 } while (!time_after_eq(jiffies, deadline));
401
Vipin Kumar712c4ad2012-03-14 11:47:16 +0530402 if (time_after_eq(jiffies, deadline)) {
403 dev_err(host->dev, "calculate ecc timed out\n");
404 return -ETIMEDOUT;
405 }
406
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100407 ecc_tmp = readl_relaxed(host->regs_va + ECC1);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200408 ecc[0] = (uint8_t) (ecc_tmp >> 0);
409 ecc[1] = (uint8_t) (ecc_tmp >> 8);
410 ecc[2] = (uint8_t) (ecc_tmp >> 16);
411 ecc[3] = (uint8_t) (ecc_tmp >> 24);
412
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100413 ecc_tmp = readl_relaxed(host->regs_va + ECC2);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200414 ecc[4] = (uint8_t) (ecc_tmp >> 0);
415 ecc[5] = (uint8_t) (ecc_tmp >> 8);
416 ecc[6] = (uint8_t) (ecc_tmp >> 16);
417 ecc[7] = (uint8_t) (ecc_tmp >> 24);
418
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100419 ecc_tmp = readl_relaxed(host->regs_va + ECC3);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200420 ecc[8] = (uint8_t) (ecc_tmp >> 0);
421 ecc[9] = (uint8_t) (ecc_tmp >> 8);
422 ecc[10] = (uint8_t) (ecc_tmp >> 16);
423 ecc[11] = (uint8_t) (ecc_tmp >> 24);
424
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100425 ecc_tmp = readl_relaxed(host->regs_va + STS);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200426 ecc[12] = (uint8_t) (ecc_tmp >> 16);
427
428 return 0;
429}
430
431/*
432 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300433 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200434 * max of 1-bit)
435 */
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200436static int fsmc_read_hwecc_ecc1(struct nand_chip *chip, const uint8_t *data,
Linus Walleij6c009ab2010-09-13 00:35:22 +0200437 uint8_t *ecc)
438{
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100439 struct fsmc_nand_data *host = nand_to_fsmc(chip);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200440 uint32_t ecc_tmp;
441
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100442 ecc_tmp = readl_relaxed(host->regs_va + ECC1);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200443 ecc[0] = (uint8_t) (ecc_tmp >> 0);
444 ecc[1] = (uint8_t) (ecc_tmp >> 8);
445 ecc[2] = (uint8_t) (ecc_tmp >> 16);
446
447 return 0;
448}
449
Vipin Kumar519300c2012-03-07 17:00:49 +0530450/* Count the number of 0's in buff upto a max of max_bits */
451static int count_written_bits(uint8_t *buff, int size, int max_bits)
452{
453 int k, written_bits = 0;
454
455 for (k = 0; k < size; k++) {
456 written_bits += hweight8(~buff[k]);
457 if (written_bits > max_bits)
458 break;
459 }
460
461 return written_bits;
462}
463
Vipin Kumar4774fb02012-03-14 11:47:18 +0530464static void dma_complete(void *param)
465{
466 struct fsmc_nand_data *host = param;
467
468 complete(&host->dma_access_complete);
469}
470
471static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len,
472 enum dma_data_direction direction)
473{
474 struct dma_chan *chan;
475 struct dma_device *dma_dev;
476 struct dma_async_tx_descriptor *tx;
477 dma_addr_t dma_dst, dma_src, dma_addr;
478 dma_cookie_t cookie;
479 unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
480 int ret;
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400481 unsigned long time_left;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530482
483 if (direction == DMA_TO_DEVICE)
484 chan = host->write_dma_chan;
485 else if (direction == DMA_FROM_DEVICE)
486 chan = host->read_dma_chan;
487 else
488 return -EINVAL;
489
490 dma_dev = chan->device;
491 dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction);
492
493 if (direction == DMA_TO_DEVICE) {
494 dma_src = dma_addr;
495 dma_dst = host->data_pa;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530496 } else {
497 dma_src = host->data_pa;
498 dma_dst = dma_addr;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530499 }
500
501 tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
502 len, flags);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530503 if (!tx) {
504 dev_err(host->dev, "device_prep_dma_memcpy error\n");
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000505 ret = -EIO;
506 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530507 }
508
509 tx->callback = dma_complete;
510 tx->callback_param = host;
511 cookie = tx->tx_submit(tx);
512
513 ret = dma_submit_error(cookie);
514 if (ret) {
515 dev_err(host->dev, "dma_submit_error %d\n", cookie);
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000516 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530517 }
518
519 dma_async_issue_pending(chan);
520
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400521 time_left =
Vipin Kumar928aa2a2012-10-09 16:14:48 +0530522 wait_for_completion_timeout(&host->dma_access_complete,
Vipin Kumar4774fb02012-03-14 11:47:18 +0530523 msecs_to_jiffies(3000));
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400524 if (time_left == 0) {
Vinod Koulb177ea32014-10-11 21:10:32 +0530525 dmaengine_terminate_all(chan);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530526 dev_err(host->dev, "wait_for_completion_timeout\n");
Nicholas Mc Guire0bda3e12015-03-13 07:54:45 -0400527 ret = -ETIMEDOUT;
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000528 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530529 }
530
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000531 ret = 0;
532
533unmap_dma:
534 dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
535
536 return ret;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530537}
538
Linus Walleij6c009ab2010-09-13 00:35:22 +0200539/*
Vipin Kumar604e7542012-03-14 11:47:17 +0530540 * fsmc_write_buf - write buffer to chip
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100541 * @host: FSMC NAND controller
Vipin Kumar604e7542012-03-14 11:47:17 +0530542 * @buf: data buffer
543 * @len: number of bytes to write
544 */
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100545static void fsmc_write_buf(struct fsmc_nand_data *host, const uint8_t *buf,
546 int len)
Vipin Kumar604e7542012-03-14 11:47:17 +0530547{
548 int i;
Vipin Kumar604e7542012-03-14 11:47:17 +0530549
Boris Brezillonf55824c2018-07-09 22:09:35 +0200550 if (IS_ALIGNED((uintptr_t)buf, sizeof(uint32_t)) &&
Vipin Kumar604e7542012-03-14 11:47:17 +0530551 IS_ALIGNED(len, sizeof(uint32_t))) {
552 uint32_t *p = (uint32_t *)buf;
553 len = len >> 2;
554 for (i = 0; i < len; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100555 writel_relaxed(p[i], host->data_va);
Vipin Kumar604e7542012-03-14 11:47:17 +0530556 } else {
557 for (i = 0; i < len; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100558 writeb_relaxed(buf[i], host->data_va);
Vipin Kumar604e7542012-03-14 11:47:17 +0530559 }
560}
561
562/*
563 * fsmc_read_buf - read chip data into buffer
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100564 * @host: FSMC NAND controller
Vipin Kumar604e7542012-03-14 11:47:17 +0530565 * @buf: buffer to store date
566 * @len: number of bytes to read
567 */
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100568static void fsmc_read_buf(struct fsmc_nand_data *host, uint8_t *buf, int len)
Vipin Kumar604e7542012-03-14 11:47:17 +0530569{
570 int i;
Vipin Kumar604e7542012-03-14 11:47:17 +0530571
Boris Brezillonf55824c2018-07-09 22:09:35 +0200572 if (IS_ALIGNED((uintptr_t)buf, sizeof(uint32_t)) &&
Vipin Kumar604e7542012-03-14 11:47:17 +0530573 IS_ALIGNED(len, sizeof(uint32_t))) {
574 uint32_t *p = (uint32_t *)buf;
575 len = len >> 2;
576 for (i = 0; i < len; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100577 p[i] = readl_relaxed(host->data_va);
Vipin Kumar604e7542012-03-14 11:47:17 +0530578 } else {
579 for (i = 0; i < len; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100580 buf[i] = readb_relaxed(host->data_va);
Vipin Kumar604e7542012-03-14 11:47:17 +0530581 }
582}
583
584/*
Vipin Kumar4774fb02012-03-14 11:47:18 +0530585 * fsmc_read_buf_dma - read chip data into buffer
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100586 * @host: FSMC NAND controller
Vipin Kumar4774fb02012-03-14 11:47:18 +0530587 * @buf: buffer to store date
588 * @len: number of bytes to read
589 */
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100590static void fsmc_read_buf_dma(struct fsmc_nand_data *host, uint8_t *buf,
591 int len)
Vipin Kumar4774fb02012-03-14 11:47:18 +0530592{
Vipin Kumar4774fb02012-03-14 11:47:18 +0530593 dma_xfer(host, buf, len, DMA_FROM_DEVICE);
594}
595
596/*
597 * fsmc_write_buf_dma - write buffer to chip
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100598 * @host: FSMC NAND controller
Vipin Kumar4774fb02012-03-14 11:47:18 +0530599 * @buf: data buffer
600 * @len: number of bytes to write
601 */
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100602static void fsmc_write_buf_dma(struct fsmc_nand_data *host, const uint8_t *buf,
603 int len)
Vipin Kumar4774fb02012-03-14 11:47:18 +0530604{
Vipin Kumar4774fb02012-03-14 11:47:18 +0530605 dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
606}
607
Miquel Raynal4da712e2018-02-16 15:22:48 +0100608/* fsmc_select_chip - assert or deassert nCE */
Boris Brezillon550b9fc2018-11-11 08:55:17 +0100609static void fsmc_ce_ctrl(struct fsmc_nand_data *host, bool assert)
Miquel Raynal4da712e2018-02-16 15:22:48 +0100610{
Boris Brezillon550b9fc2018-11-11 08:55:17 +0100611 u32 pc = readl(host->regs_va + FSMC_PC);
Miquel Raynal4da712e2018-02-16 15:22:48 +0100612
Boris Brezillon550b9fc2018-11-11 08:55:17 +0100613 if (!assert)
Boris Brezillon8f3931e2018-07-09 22:09:34 +0200614 writel_relaxed(pc & ~FSMC_ENABLE, host->regs_va + FSMC_PC);
Miquel Raynal4da712e2018-02-16 15:22:48 +0100615 else
Boris Brezillon8f3931e2018-07-09 22:09:34 +0200616 writel_relaxed(pc | FSMC_ENABLE, host->regs_va + FSMC_PC);
Miquel Raynal4da712e2018-02-16 15:22:48 +0100617
Boris Brezillon550b9fc2018-11-11 08:55:17 +0100618 /*
619 * nCE line changes must be applied before returning from this
620 * function.
621 */
Miquel Raynal4da712e2018-02-16 15:22:48 +0100622 mb();
623}
624
625/*
626 * fsmc_exec_op - hook called by the core to execute NAND operations
627 *
628 * This controller is simple enough and thus does not need to use the parser
629 * provided by the core, instead, handle every situation here.
630 */
631static int fsmc_exec_op(struct nand_chip *chip, const struct nand_operation *op,
632 bool check_only)
633{
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100634 struct fsmc_nand_data *host = nand_to_fsmc(chip);
Miquel Raynal4da712e2018-02-16 15:22:48 +0100635 const struct nand_op_instr *instr = NULL;
636 int ret = 0;
637 unsigned int op_id;
638 int i;
639
640 pr_debug("Executing operation [%d instructions]:\n", op->ninstrs);
Boris Brezillon550b9fc2018-11-11 08:55:17 +0100641
642 fsmc_ce_ctrl(host, true);
643
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
647 switch (instr->type) {
648 case NAND_OP_CMD_INSTR:
649 pr_debug(" ->CMD [0x%02x]\n",
650 instr->ctx.cmd.opcode);
651
652 writeb_relaxed(instr->ctx.cmd.opcode, host->cmd_va);
653 break;
654
655 case NAND_OP_ADDR_INSTR:
656 pr_debug(" ->ADDR [%d cyc]",
657 instr->ctx.addr.naddrs);
658
659 for (i = 0; i < instr->ctx.addr.naddrs; i++)
660 writeb_relaxed(instr->ctx.addr.addrs[i],
661 host->addr_va);
662 break;
663
664 case NAND_OP_DATA_IN_INSTR:
665 pr_debug(" ->DATA_IN [%d B%s]\n", instr->ctx.data.len,
666 instr->ctx.data.force_8bit ?
667 ", force 8-bit" : "");
668
669 if (host->mode == USE_DMA_ACCESS)
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100670 fsmc_read_buf_dma(host, instr->ctx.data.buf.in,
Miquel Raynal4da712e2018-02-16 15:22:48 +0100671 instr->ctx.data.len);
672 else
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100673 fsmc_read_buf(host, instr->ctx.data.buf.in,
Miquel Raynal4da712e2018-02-16 15:22:48 +0100674 instr->ctx.data.len);
675 break;
676
677 case NAND_OP_DATA_OUT_INSTR:
678 pr_debug(" ->DATA_OUT [%d B%s]\n", instr->ctx.data.len,
679 instr->ctx.data.force_8bit ?
680 ", force 8-bit" : "");
681
682 if (host->mode == USE_DMA_ACCESS)
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100683 fsmc_write_buf_dma(host, instr->ctx.data.buf.out,
Miquel Raynal4da712e2018-02-16 15:22:48 +0100684 instr->ctx.data.len);
685 else
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100686 fsmc_write_buf(host, instr->ctx.data.buf.out,
Miquel Raynal4da712e2018-02-16 15:22:48 +0100687 instr->ctx.data.len);
688 break;
689
690 case NAND_OP_WAITRDY_INSTR:
691 pr_debug(" ->WAITRDY [max %d ms]\n",
692 instr->ctx.waitrdy.timeout_ms);
693
694 ret = nand_soft_waitrdy(chip,
695 instr->ctx.waitrdy.timeout_ms);
696 break;
697 }
698 }
699
Boris Brezillon550b9fc2018-11-11 08:55:17 +0100700 fsmc_ce_ctrl(host, false);
701
Miquel Raynal4da712e2018-02-16 15:22:48 +0100702 return ret;
703}
704
Vipin Kumar4774fb02012-03-14 11:47:18 +0530705/*
Linus Walleij6c009ab2010-09-13 00:35:22 +0200706 * fsmc_read_page_hwecc
Linus Walleij6c009ab2010-09-13 00:35:22 +0200707 * @chip: nand chip info structure
708 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -0700709 * @oob_required: caller expects OOB data read to chip->oob_poi
Linus Walleij6c009ab2010-09-13 00:35:22 +0200710 * @page: page number to read
711 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300712 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
Linus Walleij6c009ab2010-09-13 00:35:22 +0200713 * performed in a strict sequence as follows:
714 * data(512 byte) -> ecc(13 byte)
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300715 * After this read, fsmc hardware generates and reports error data bits(up to a
Linus Walleij6c009ab2010-09-13 00:35:22 +0200716 * max of 8 bits)
717 */
Boris Brezillonb9761682018-09-06 14:05:20 +0200718static int fsmc_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
719 int oob_required, int page)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200720{
Boris Brezillonb9761682018-09-06 14:05:20 +0200721 struct mtd_info *mtd = nand_to_mtd(chip);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200722 int i, j, s, stat, eccsize = chip->ecc.size;
723 int eccbytes = chip->ecc.bytes;
724 int eccsteps = chip->ecc.steps;
725 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +0900726 uint8_t *ecc_calc = chip->ecc.calc_buf;
727 uint8_t *ecc_code = chip->ecc.code_buf;
Gustavo A. R. Silva41d6f0d2018-10-10 17:58:58 +0200728 int off, len, ret, group = 0;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200729 /*
730 * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
731 * end up reading 14 bytes (7 words) from oob. The local array is
732 * to maintain word alignment
733 */
734 uint16_t ecc_oob[7];
735 uint8_t *oob = (uint8_t *)&ecc_oob[0];
Mike Dunn3f91e942012-04-25 12:06:09 -0700736 unsigned int max_bitflips = 0;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200737
738 for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100739 nand_read_page_op(chip, page, s * eccsize, NULL, 0);
Boris Brezillonec476362018-09-06 14:05:17 +0200740 chip->ecc.hwctl(chip, NAND_ECC_READ);
Gustavo A. R. Silva41d6f0d2018-10-10 17:58:58 +0200741 ret = nand_read_data_op(chip, p, eccsize, false);
742 if (ret)
743 return ret;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200744
745 for (j = 0; j < eccbytes;) {
Boris Brezillon04a123a2016-02-09 15:01:21 +0100746 struct mtd_oob_region oobregion;
Boris Brezillon04a123a2016-02-09 15:01:21 +0100747
748 ret = mtd_ooblayout_ecc(mtd, group++, &oobregion);
749 if (ret)
750 return ret;
751
752 off = oobregion.offset;
753 len = oobregion.length;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200754
755 /*
Vipin Kumar4cbe1bf02012-03-14 11:47:09 +0530756 * length is intentionally kept a higher multiple of 2
757 * to read at least 13 bytes even in case of 16 bit NAND
758 * devices
759 */
Vipin Kumaraea686b2012-03-14 11:47:10 +0530760 if (chip->options & NAND_BUSWIDTH_16)
761 len = roundup(len, 2);
762
Boris Brezillon97d90da2017-11-30 18:01:29 +0100763 nand_read_oob_op(chip, page, off, oob + j, len);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200764 j += len;
765 }
766
Vipin Kumar519300c2012-03-07 17:00:49 +0530767 memcpy(&ecc_code[i], oob, chip->ecc.bytes);
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200768 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200769
Boris Brezillon00da2ea2018-09-06 14:05:19 +0200770 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -0700771 if (stat < 0) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200772 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -0700773 } else {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200774 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -0700775 max_bitflips = max_t(unsigned int, max_bitflips, stat);
776 }
Linus Walleij6c009ab2010-09-13 00:35:22 +0200777 }
778
Mike Dunn3f91e942012-04-25 12:06:09 -0700779 return max_bitflips;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200780}
781
782/*
Armando Visconti753e0132012-03-07 17:00:54 +0530783 * fsmc_bch8_correct_data
Linus Walleij6c009ab2010-09-13 00:35:22 +0200784 * @mtd: mtd info structure
785 * @dat: buffer of read data
786 * @read_ecc: ecc read from device spare area
787 * @calc_ecc: ecc calculated from read data
788 *
789 * calc_ecc is a 104 bit information containing maximum of 8 error
790 * offset informations of 13 bits each in 512 bytes of read data.
791 */
Boris Brezillon00da2ea2018-09-06 14:05:19 +0200792static int fsmc_bch8_correct_data(struct nand_chip *chip, uint8_t *dat,
793 uint8_t *read_ecc, uint8_t *calc_ecc)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200794{
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100795 struct fsmc_nand_data *host = nand_to_fsmc(chip);
Armando Viscontia612c2a2012-03-07 17:00:53 +0530796 uint32_t err_idx[8];
Linus Walleij6c009ab2010-09-13 00:35:22 +0200797 uint32_t num_err, i;
Armando Visconti753e0132012-03-07 17:00:54 +0530798 uint32_t ecc1, ecc2, ecc3, ecc4;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200799
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100800 num_err = (readl_relaxed(host->regs_va + STS) >> 10) & 0xF;
Vipin Kumar519300c2012-03-07 17:00:49 +0530801
802 /* no bit flipping */
803 if (likely(num_err == 0))
804 return 0;
805
806 /* too many errors */
807 if (unlikely(num_err > 8)) {
808 /*
809 * This is a temporary erase check. A newly erased page read
810 * would result in an ecc error because the oob data is also
811 * erased to FF and the calculated ecc for an FF data is not
812 * FF..FF.
813 * This is a workaround to skip performing correction in case
814 * data is FF..FF
815 *
816 * Logic:
817 * For every page, each bit written as 0 is counted until these
818 * number of bits are greater than 8 (the maximum correction
819 * capability of FSMC for each 512 + 13 bytes)
820 */
821
822 int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
823 int bits_data = count_written_bits(dat, chip->ecc.size, 8);
824
825 if ((bits_ecc + bits_data) <= 8) {
826 if (bits_data)
827 memset(dat, 0xff, chip->ecc.size);
828 return bits_data;
829 }
830
831 return -EBADMSG;
832 }
833
Linus Walleij6c009ab2010-09-13 00:35:22 +0200834 /*
835 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
836 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
837 *
838 * calc_ecc is a 104 bit information containing maximum of 8 error
839 * offset informations of 13 bits each. calc_ecc is copied into a
840 * uint64_t array and error offset indexes are populated in err_idx
841 * array
842 */
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100843 ecc1 = readl_relaxed(host->regs_va + ECC1);
844 ecc2 = readl_relaxed(host->regs_va + ECC2);
845 ecc3 = readl_relaxed(host->regs_va + ECC3);
846 ecc4 = readl_relaxed(host->regs_va + STS);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200847
Armando Visconti753e0132012-03-07 17:00:54 +0530848 err_idx[0] = (ecc1 >> 0) & 0x1FFF;
849 err_idx[1] = (ecc1 >> 13) & 0x1FFF;
850 err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
851 err_idx[3] = (ecc2 >> 7) & 0x1FFF;
852 err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
853 err_idx[5] = (ecc3 >> 1) & 0x1FFF;
854 err_idx[6] = (ecc3 >> 14) & 0x1FFF;
855 err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200856
857 i = 0;
858 while (num_err--) {
859 change_bit(0, (unsigned long *)&err_idx[i]);
860 change_bit(1, (unsigned long *)&err_idx[i]);
861
Vipin Kumarb533f8d2012-03-14 11:47:11 +0530862 if (err_idx[i] < chip->ecc.size * 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200863 change_bit(err_idx[i], (unsigned long *)dat);
864 i++;
865 }
866 }
867 return i;
868}
869
Vipin Kumar4774fb02012-03-14 11:47:18 +0530870static bool filter(struct dma_chan *chan, void *slave)
871{
872 chan->private = slave;
873 return true;
874}
875
Bill Pemberton06f25512012-11-19 13:23:07 -0500876static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100877 struct fsmc_nand_data *host,
878 struct nand_chip *nand)
Stefan Roeseeea62812012-03-16 10:19:31 +0100879{
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100880 struct device_node *np = pdev->dev.of_node;
Stefan Roeseeea62812012-03-16 10:19:31 +0100881 u32 val;
Stefan Roese62b57f42015-03-19 14:34:29 +0100882 int ret;
Stefan Roeseeea62812012-03-16 10:19:31 +0100883
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100884 nand->options = 0;
Thomas Petazzoniee568742017-03-21 11:03:53 +0100885
Stefan Roeseeea62812012-03-16 10:19:31 +0100886 if (!of_property_read_u32(np, "bank-width", &val)) {
887 if (val == 2) {
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100888 nand->options |= NAND_BUSWIDTH_16;
Stefan Roeseeea62812012-03-16 10:19:31 +0100889 } else if (val != 1) {
890 dev_err(&pdev->dev, "invalid bank-width %u\n", val);
891 return -EINVAL;
892 }
893 }
Thomas Petazzoniee568742017-03-21 11:03:53 +0100894
Stefan Roeseeea62812012-03-16 10:19:31 +0100895 if (of_get_property(np, "nand-skip-bbtscan", NULL))
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100896 nand->options |= NAND_SKIP_BBTSCAN;
Stefan Roeseeea62812012-03-16 10:19:31 +0100897
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100898 host->dev_timings = devm_kzalloc(&pdev->dev,
899 sizeof(*host->dev_timings), GFP_KERNEL);
900 if (!host->dev_timings)
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200901 return -ENOMEM;
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100902 ret = of_property_read_u8_array(np, "timings", (u8 *)host->dev_timings,
903 sizeof(*host->dev_timings));
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200904 if (ret)
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100905 host->dev_timings = NULL;
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200906
907 /* Set default NAND bank to 0 */
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100908 host->bank = 0;
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200909 if (!of_property_read_u32(np, "bank", &val)) {
910 if (val > 3) {
911 dev_err(&pdev->dev, "invalid bank %u\n", val);
912 return -EINVAL;
913 }
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100914 host->bank = val;
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200915 }
Stefan Roeseeea62812012-03-16 10:19:31 +0100916 return 0;
917}
Stefan Roeseeea62812012-03-16 10:19:31 +0100918
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200919static int fsmc_nand_attach_chip(struct nand_chip *nand)
920{
921 struct mtd_info *mtd = nand_to_mtd(nand);
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100922 struct fsmc_nand_data *host = nand_to_fsmc(nand);
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200923
924 if (AMBA_REV_BITS(host->pid) >= 8) {
925 switch (mtd->oobsize) {
926 case 16:
927 case 64:
928 case 128:
929 case 224:
930 case 256:
931 break;
932 default:
933 dev_warn(host->dev,
934 "No oob scheme defined for oobsize %d\n",
935 mtd->oobsize);
936 return -EINVAL;
937 }
938
939 mtd_set_ooblayout(mtd, &fsmc_ecc4_ooblayout_ops);
940
941 return 0;
942 }
943
944 switch (nand->ecc.mode) {
945 case NAND_ECC_HW:
946 dev_info(host->dev, "Using 1-bit HW ECC scheme\n");
947 nand->ecc.calculate = fsmc_read_hwecc_ecc1;
948 nand->ecc.correct = nand_correct_data;
949 nand->ecc.bytes = 3;
950 nand->ecc.strength = 1;
Boris Brezillon309600c2018-09-04 16:23:28 +0200951 nand->ecc.options |= NAND_ECC_SOFT_HAMMING_SM_ORDER;
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200952 break;
953
954 case NAND_ECC_SOFT:
955 if (nand->ecc.algo == NAND_ECC_BCH) {
956 dev_info(host->dev,
957 "Using 4-bit SW BCH ECC scheme\n");
958 break;
959 }
960
961 case NAND_ECC_ON_DIE:
962 break;
963
964 default:
965 dev_err(host->dev, "Unsupported ECC mode!\n");
966 return -ENOTSUPP;
967 }
968
969 /*
970 * Don't set layout for BCH4 SW ECC. This will be
971 * generated later in nand_bch_init() later.
972 */
973 if (nand->ecc.mode == NAND_ECC_HW) {
974 switch (mtd->oobsize) {
975 case 16:
976 case 64:
977 case 128:
978 mtd_set_ooblayout(mtd,
979 &fsmc_ecc1_ooblayout_ops);
980 break;
981 default:
982 dev_warn(host->dev,
983 "No oob scheme defined for oobsize %d\n",
984 mtd->oobsize);
985 return -EINVAL;
986 }
987 }
988
989 return 0;
990}
991
992static const struct nand_controller_ops fsmc_nand_controller_ops = {
993 .attach_chip = fsmc_nand_attach_chip,
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100994 .exec_op = fsmc_exec_op,
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100995 .setup_data_interface = fsmc_setup_data_interface,
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200996};
997
Linus Walleij6c009ab2010-09-13 00:35:22 +0200998/*
999 * fsmc_nand_probe - Probe function
1000 * @pdev: platform device structure
1001 */
1002static int __init fsmc_nand_probe(struct platform_device *pdev)
1003{
Linus Walleij6c009ab2010-09-13 00:35:22 +02001004 struct fsmc_nand_data *host;
1005 struct mtd_info *mtd;
1006 struct nand_chip *nand;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001007 struct resource *res;
Miquel Raynal4df6ed42018-02-16 15:22:47 +01001008 void __iomem *base;
Vipin Kumar4774fb02012-03-14 11:47:18 +05301009 dma_cap_mask_t mask;
Linus Walleij4ad916b2010-11-29 13:52:06 +01001010 int ret = 0;
Linus Walleij593cd872010-11-29 13:52:19 +01001011 u32 pid;
1012 int i;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001013
Linus Walleij6c009ab2010-09-13 00:35:22 +02001014 /* Allocate memory for the device structure (and zero it) */
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301015 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Jingoo Hand9a21ae2013-12-26 12:16:38 +09001016 if (!host)
Linus Walleij6c009ab2010-09-13 00:35:22 +02001017 return -ENOMEM;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001018
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +01001019 nand = &host->nand;
1020
1021 ret = fsmc_nand_probe_config_dt(pdev, host, nand);
1022 if (ret)
1023 return ret;
1024
Linus Walleij6c009ab2010-09-13 00:35:22 +02001025 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
Thierry Redingb0de7742013-01-21 11:09:12 +01001026 host->data_va = devm_ioremap_resource(&pdev->dev, res);
1027 if (IS_ERR(host->data_va))
1028 return PTR_ERR(host->data_va);
Stefan Roesecbf29b82015-10-02 12:40:20 +02001029
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +02001030 host->data_pa = (dma_addr_t)res->start;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001031
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +02001032 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
Thierry Redingb0de7742013-01-21 11:09:12 +01001033 host->addr_va = devm_ioremap_resource(&pdev->dev, res);
1034 if (IS_ERR(host->addr_va))
1035 return PTR_ERR(host->addr_va);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001036
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +02001037 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
Thierry Redingb0de7742013-01-21 11:09:12 +01001038 host->cmd_va = devm_ioremap_resource(&pdev->dev, res);
1039 if (IS_ERR(host->cmd_va))
1040 return PTR_ERR(host->cmd_va);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001041
1042 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
Miquel Raynal4df6ed42018-02-16 15:22:47 +01001043 base = devm_ioremap_resource(&pdev->dev, res);
1044 if (IS_ERR(base))
1045 return PTR_ERR(base);
1046
1047 host->regs_va = base + FSMC_NOR_REG_SIZE +
1048 (host->bank * FSMC_NAND_BANK_SZ);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001049
Thomas Petazzonifb8ed2c2017-03-21 11:04:03 +01001050 host->clk = devm_clk_get(&pdev->dev, NULL);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001051 if (IS_ERR(host->clk)) {
1052 dev_err(&pdev->dev, "failed to fetch block clock\n");
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301053 return PTR_ERR(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001054 }
1055
Viresh Kumare25da1c2012-04-17 17:07:57 +05301056 ret = clk_prepare_enable(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001057 if (ret)
Thomas Petazzonifb8ed2c2017-03-21 11:04:03 +01001058 return ret;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001059
Linus Walleij593cd872010-11-29 13:52:19 +01001060 /*
1061 * This device ID is actually a common AMBA ID as used on the
1062 * AMBA PrimeCell bus. However it is not a PrimeCell.
1063 */
1064 for (pid = 0, i = 0; i < 4; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +01001065 pid |= (readl(base + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
Linus Walleij593cd872010-11-29 13:52:19 +01001066 host->pid = pid;
1067 dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
1068 "revision %02x, config %02x\n",
1069 AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
1070 AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
1071
Vipin Kumar712c4ad2012-03-14 11:47:16 +05301072 host->dev = &pdev->dev;
Vipin Kumar4774fb02012-03-14 11:47:18 +05301073
1074 if (host->mode == USE_DMA_ACCESS)
1075 init_completion(&host->dma_access_complete);
1076
Linus Walleij6c009ab2010-09-13 00:35:22 +02001077 /* Link all private pointers */
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001078 mtd = nand_to_mtd(&host->nand);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001079 nand_set_controller_data(nand, host);
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +01001080 nand_set_flash_node(nand, pdev->dev.of_node);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001081
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001082 mtd->dev.parent = &pdev->dev;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001083
Stefan Roesee278fc72015-10-19 08:40:13 +02001084 /*
1085 * Setup default ECC mode. nand_dt_init() called from nand_scan_ident()
1086 * can overwrite this value if the DT provides a different value.
1087 */
Linus Walleij6c009ab2010-09-13 00:35:22 +02001088 nand->ecc.mode = NAND_ECC_HW;
1089 nand->ecc.hwctl = fsmc_enable_hwecc;
1090 nand->ecc.size = 512;
Vipin Kumar467e6e72012-03-14 11:47:12 +05301091 nand->badblockbits = 7;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001092
Miquel Raynal4da712e2018-02-16 15:22:48 +01001093 if (host->mode == USE_DMA_ACCESS) {
Vipin Kumar4774fb02012-03-14 11:47:18 +05301094 dma_cap_zero(mask);
1095 dma_cap_set(DMA_MEMCPY, mask);
Thomas Petazzonifeb1e572017-03-21 11:03:59 +01001096 host->read_dma_chan = dma_request_channel(mask, filter, NULL);
Vipin Kumar4774fb02012-03-14 11:47:18 +05301097 if (!host->read_dma_chan) {
1098 dev_err(&pdev->dev, "Unable to get read dma channel\n");
Miquel Raynal43fab012018-04-21 20:00:36 +02001099 goto disable_clk;
Vipin Kumar4774fb02012-03-14 11:47:18 +05301100 }
Thomas Petazzonifeb1e572017-03-21 11:03:59 +01001101 host->write_dma_chan = dma_request_channel(mask, filter, NULL);
Vipin Kumar4774fb02012-03-14 11:47:18 +05301102 if (!host->write_dma_chan) {
1103 dev_err(&pdev->dev, "Unable to get write dma channel\n");
Miquel Raynal43fab012018-04-21 20:00:36 +02001104 goto release_dma_read_chan;
Vipin Kumar4774fb02012-03-14 11:47:18 +05301105 }
Vipin Kumar604e7542012-03-14 11:47:17 +05301106 }
1107
Boris Brezillon7a08dba2018-11-11 08:55:24 +01001108 if (host->dev_timings) {
Thomas Petazzonid9fb0792017-04-29 10:52:35 +02001109 fsmc_nand_setup(host, host->dev_timings);
Boris Brezillon7a08dba2018-11-11 08:55:24 +01001110 nand->options |= NAND_KEEP_TIMINGS;
1111 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001112
Linus Walleij593cd872010-11-29 13:52:19 +01001113 if (AMBA_REV_BITS(host->pid) >= 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +02001114 nand->ecc.read_page = fsmc_read_page_hwecc;
1115 nand->ecc.calculate = fsmc_read_hwecc_ecc4;
Armando Visconti753e0132012-03-07 17:00:54 +05301116 nand->ecc.correct = fsmc_bch8_correct_data;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001117 nand->ecc.bytes = 13;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001118 nand->ecc.strength = 8;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001119 }
1120
1121 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001122 * Scan to find existence of the device
Linus Walleij6c009ab2010-09-13 00:35:22 +02001123 */
Miquel Raynal3bbddfa2018-07-20 17:14:59 +02001124 nand->dummy_controller.ops = &fsmc_nand_controller_ops;
Boris Brezillon00ad3782018-09-06 14:05:14 +02001125 ret = nand_scan(nand, 1);
Masahiro Yamadaad5678e2016-11-04 19:43:00 +09001126 if (ret)
Miquel Raynal43fab012018-04-21 20:00:36 +02001127 goto release_dma_write_chan;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001128
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001129 mtd->name = "nand";
Thomas Petazzoniede29a02017-03-21 11:04:00 +01001130 ret = mtd_device_register(mtd, NULL, 0);
Jamie Iles99335d02011-05-23 10:23:23 +01001131 if (ret)
Miquel Raynal682cae22018-04-21 20:00:37 +02001132 goto cleanup_nand;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001133
1134 platform_set_drvdata(pdev, host);
1135 dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
Miquel Raynal43fab012018-04-21 20:00:36 +02001136
Linus Walleij6c009ab2010-09-13 00:35:22 +02001137 return 0;
1138
Miquel Raynal682cae22018-04-21 20:00:37 +02001139cleanup_nand:
1140 nand_cleanup(nand);
Miquel Raynal43fab012018-04-21 20:00:36 +02001141release_dma_write_chan:
Vipin Kumar4774fb02012-03-14 11:47:18 +05301142 if (host->mode == USE_DMA_ACCESS)
1143 dma_release_channel(host->write_dma_chan);
Miquel Raynal43fab012018-04-21 20:00:36 +02001144release_dma_read_chan:
Vipin Kumar4774fb02012-03-14 11:47:18 +05301145 if (host->mode == USE_DMA_ACCESS)
1146 dma_release_channel(host->read_dma_chan);
Miquel Raynal43fab012018-04-21 20:00:36 +02001147disable_clk:
Viresh Kumare25da1c2012-04-17 17:07:57 +05301148 clk_disable_unprepare(host->clk);
Miquel Raynal43fab012018-04-21 20:00:36 +02001149
Linus Walleij6c009ab2010-09-13 00:35:22 +02001150 return ret;
1151}
1152
1153/*
1154 * Clean up routine
1155 */
1156static int fsmc_nand_remove(struct platform_device *pdev)
1157{
1158 struct fsmc_nand_data *host = platform_get_drvdata(pdev);
1159
Linus Walleij6c009ab2010-09-13 00:35:22 +02001160 if (host) {
Boris Brezillon59ac2762018-09-06 14:05:15 +02001161 nand_release(&host->nand);
Vipin Kumar4774fb02012-03-14 11:47:18 +05301162
1163 if (host->mode == USE_DMA_ACCESS) {
1164 dma_release_channel(host->write_dma_chan);
1165 dma_release_channel(host->read_dma_chan);
1166 }
Viresh Kumare25da1c2012-04-17 17:07:57 +05301167 clk_disable_unprepare(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001168 }
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301169
Linus Walleij6c009ab2010-09-13 00:35:22 +02001170 return 0;
1171}
1172
Jingoo Han80ce4dd2013-03-26 15:53:48 +09001173#ifdef CONFIG_PM_SLEEP
Linus Walleij6c009ab2010-09-13 00:35:22 +02001174static int fsmc_nand_suspend(struct device *dev)
1175{
1176 struct fsmc_nand_data *host = dev_get_drvdata(dev);
1177 if (host)
Viresh Kumare25da1c2012-04-17 17:07:57 +05301178 clk_disable_unprepare(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001179 return 0;
1180}
1181
1182static int fsmc_nand_resume(struct device *dev)
1183{
1184 struct fsmc_nand_data *host = dev_get_drvdata(dev);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301185 if (host) {
Viresh Kumare25da1c2012-04-17 17:07:57 +05301186 clk_prepare_enable(host->clk);
Thomas Petazzonid9fb0792017-04-29 10:52:35 +02001187 if (host->dev_timings)
1188 fsmc_nand_setup(host, host->dev_timings);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301189 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001190 return 0;
1191}
Jingoo Han80ce4dd2013-03-26 15:53:48 +09001192#endif
Linus Walleij6c009ab2010-09-13 00:35:22 +02001193
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301194static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001195
Stefan Roeseeea62812012-03-16 10:19:31 +01001196static const struct of_device_id fsmc_nand_id_table[] = {
1197 { .compatible = "st,spear600-fsmc-nand" },
Linus Walleijba785202013-01-05 22:28:32 +01001198 { .compatible = "stericsson,fsmc-nand" },
Stefan Roeseeea62812012-03-16 10:19:31 +01001199 {}
1200};
1201MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);
Stefan Roeseeea62812012-03-16 10:19:31 +01001202
Linus Walleij6c009ab2010-09-13 00:35:22 +02001203static struct platform_driver fsmc_nand_driver = {
1204 .remove = fsmc_nand_remove,
1205 .driver = {
Linus Walleij6c009ab2010-09-13 00:35:22 +02001206 .name = "fsmc-nand",
Thomas Petazzoni33575b22017-03-21 11:04:05 +01001207 .of_match_table = fsmc_nand_id_table,
Linus Walleij6c009ab2010-09-13 00:35:22 +02001208 .pm = &fsmc_nand_pm_ops,
Linus Walleij6c009ab2010-09-13 00:35:22 +02001209 },
1210};
1211
Jingoo Han307d2a512013-03-05 13:30:36 +09001212module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001213
1214MODULE_LICENSE("GPL");
1215MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
1216MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");