blob: 54c854451493be72dd8d00ec158277637ee11ea9 [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
126 * @mtd: MTD info for a NAND flash.
127 * @nand: Chip related info for a NAND flash.
128 * @partitions: Partition info for a NAND Flash.
129 * @nr_partitions: Total number of partition of a NAND flash.
130 *
131 * @bank: Bank number for probed device.
132 * @clk: Clock structure for FSMC.
133 *
134 * @read_dma_chan: DMA channel for read access
135 * @write_dma_chan: DMA channel for write access to NAND
136 * @dma_access_complete: Completion structure
137 *
138 * @data_pa: NAND Physical port for Data.
139 * @data_va: NAND port for Data.
140 * @cmd_va: NAND port for Command.
141 * @addr_va: NAND port for Address.
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100142 * @regs_va: Registers base address for a given bank.
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100143 */
144struct fsmc_nand_data {
145 u32 pid;
146 struct nand_chip nand;
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100147
148 unsigned int bank;
149 struct device *dev;
150 enum access_mode mode;
151 struct clk *clk;
152
153 /* DMA related objects */
154 struct dma_chan *read_dma_chan;
155 struct dma_chan *write_dma_chan;
156 struct completion dma_access_complete;
157
158 struct fsmc_nand_timings *dev_timings;
159
160 dma_addr_t data_pa;
161 void __iomem *data_va;
162 void __iomem *cmd_va;
163 void __iomem *addr_va;
164 void __iomem *regs_va;
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100165};
166
Boris Brezillon22b46952016-02-03 20:01:42 +0100167static int fsmc_ecc1_ooblayout_ecc(struct mtd_info *mtd, int section,
168 struct mtd_oob_region *oobregion)
169{
170 struct nand_chip *chip = mtd_to_nand(mtd);
171
172 if (section >= chip->ecc.steps)
173 return -ERANGE;
174
175 oobregion->offset = (section * 16) + 2;
176 oobregion->length = 3;
177
178 return 0;
179}
180
181static int fsmc_ecc1_ooblayout_free(struct mtd_info *mtd, int section,
182 struct mtd_oob_region *oobregion)
183{
184 struct nand_chip *chip = mtd_to_nand(mtd);
185
186 if (section >= chip->ecc.steps)
187 return -ERANGE;
188
189 oobregion->offset = (section * 16) + 8;
190
191 if (section < chip->ecc.steps - 1)
192 oobregion->length = 8;
193 else
194 oobregion->length = mtd->oobsize - oobregion->offset;
195
196 return 0;
197}
198
199static const struct mtd_ooblayout_ops fsmc_ecc1_ooblayout_ops = {
200 .ecc = fsmc_ecc1_ooblayout_ecc,
201 .free = fsmc_ecc1_ooblayout_free,
202};
203
Boris Brezillon04a123a2016-02-09 15:01:21 +0100204/*
205 * ECC placement definitions in oobfree type format.
206 * There are 13 bytes of ecc for every 512 byte block and it has to be read
207 * consecutively and immediately after the 512 byte data block for hardware to
208 * generate the error bit offsets in 512 byte data.
209 */
Boris Brezillon22b46952016-02-03 20:01:42 +0100210static int fsmc_ecc4_ooblayout_ecc(struct mtd_info *mtd, int section,
211 struct mtd_oob_region *oobregion)
212{
213 struct nand_chip *chip = mtd_to_nand(mtd);
214
215 if (section >= chip->ecc.steps)
216 return -ERANGE;
217
218 oobregion->length = chip->ecc.bytes;
219
220 if (!section && mtd->writesize <= 512)
221 oobregion->offset = 0;
222 else
223 oobregion->offset = (section * 16) + 2;
224
225 return 0;
226}
227
228static int fsmc_ecc4_ooblayout_free(struct mtd_info *mtd, int section,
229 struct mtd_oob_region *oobregion)
230{
231 struct nand_chip *chip = mtd_to_nand(mtd);
232
233 if (section >= chip->ecc.steps)
234 return -ERANGE;
235
236 oobregion->offset = (section * 16) + 15;
237
238 if (section < chip->ecc.steps - 1)
239 oobregion->length = 3;
240 else
241 oobregion->length = mtd->oobsize - oobregion->offset;
242
243 return 0;
244}
245
246static const struct mtd_ooblayout_ops fsmc_ecc4_ooblayout_ops = {
247 .ecc = fsmc_ecc4_ooblayout_ecc,
248 .free = fsmc_ecc4_ooblayout_free,
249};
250
Boris 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{
265 uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530266 uint32_t 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 Brezillon8f3931e2018-07-09 22:09:34 +0200276 writel_relaxed(value | FSMC_DEVWID_16,
277 host->regs_va + FSMC_PC);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200278 else
Boris Brezillon8f3931e2018-07-09 22:09:34 +0200279 writel_relaxed(value | FSMC_DEVWID_8, host->regs_va + FSMC_PC);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200280
Boris Brezillon8f3931e2018-07-09 22:09:34 +0200281 writel_relaxed(readl(host->regs_va + FSMC_PC) | tclr | tar,
282 host->regs_va + FSMC_PC);
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100283 writel_relaxed(thiz | thold | twait | tset, host->regs_va + COMM);
284 writel_relaxed(thiz | thold | twait | tset, host->regs_va + ATTRIB);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200285}
286
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200287static int fsmc_calc_timings(struct fsmc_nand_data *host,
288 const struct nand_sdr_timings *sdrt,
289 struct fsmc_nand_timings *tims)
290{
291 unsigned long hclk = clk_get_rate(host->clk);
292 unsigned long hclkn = NSEC_PER_SEC / hclk;
293 uint32_t thiz, thold, twait, tset;
294
295 if (sdrt->tRC_min < 30000)
296 return -EOPNOTSUPP;
297
298 tims->tar = DIV_ROUND_UP(sdrt->tAR_min / 1000, hclkn) - 1;
299 if (tims->tar > FSMC_TAR_MASK)
300 tims->tar = FSMC_TAR_MASK;
301 tims->tclr = DIV_ROUND_UP(sdrt->tCLR_min / 1000, hclkn) - 1;
302 if (tims->tclr > FSMC_TCLR_MASK)
303 tims->tclr = FSMC_TCLR_MASK;
304
305 thiz = sdrt->tCS_min - sdrt->tWP_min;
306 tims->thiz = DIV_ROUND_UP(thiz / 1000, hclkn);
307
308 thold = sdrt->tDH_min;
309 if (thold < sdrt->tCH_min)
310 thold = sdrt->tCH_min;
311 if (thold < sdrt->tCLH_min)
312 thold = sdrt->tCLH_min;
313 if (thold < sdrt->tWH_min)
314 thold = sdrt->tWH_min;
315 if (thold < sdrt->tALH_min)
316 thold = sdrt->tALH_min;
317 if (thold < sdrt->tREH_min)
318 thold = sdrt->tREH_min;
319 tims->thold = DIV_ROUND_UP(thold / 1000, hclkn);
320 if (tims->thold == 0)
321 tims->thold = 1;
322 else if (tims->thold > FSMC_THOLD_MASK)
323 tims->thold = FSMC_THOLD_MASK;
324
325 twait = max(sdrt->tRP_min, sdrt->tWP_min);
326 tims->twait = DIV_ROUND_UP(twait / 1000, hclkn) - 1;
327 if (tims->twait == 0)
328 tims->twait = 1;
329 else if (tims->twait > FSMC_TWAIT_MASK)
330 tims->twait = FSMC_TWAIT_MASK;
331
332 tset = max(sdrt->tCS_min - sdrt->tWP_min,
333 sdrt->tCEA_max - sdrt->tREA_max);
334 tims->tset = DIV_ROUND_UP(tset / 1000, hclkn) - 1;
335 if (tims->tset == 0)
336 tims->tset = 1;
337 else if (tims->tset > FSMC_TSET_MASK)
338 tims->tset = FSMC_TSET_MASK;
339
340 return 0;
341}
342
Boris Brezillon858838b2018-09-06 14:05:33 +0200343static int fsmc_setup_data_interface(struct nand_chip *nand, int csline,
Boris Brezillon104e4422017-03-16 09:35:58 +0100344 const struct nand_data_interface *conf)
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200345{
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200346 struct fsmc_nand_data *host = nand_get_controller_data(nand);
347 struct fsmc_nand_timings tims;
348 const struct nand_sdr_timings *sdrt;
349 int ret;
350
351 sdrt = nand_get_sdr_timings(conf);
352 if (IS_ERR(sdrt))
353 return PTR_ERR(sdrt);
354
355 ret = fsmc_calc_timings(host, sdrt, &tims);
356 if (ret)
357 return ret;
358
Boris Brezillon104e4422017-03-16 09:35:58 +0100359 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200360 return 0;
361
362 fsmc_nand_setup(host, &tims);
363
364 return 0;
365}
366
Linus Walleij6c009ab2010-09-13 00:35:22 +0200367/*
368 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
369 */
Boris Brezillonec476362018-09-06 14:05:17 +0200370static void fsmc_enable_hwecc(struct nand_chip *chip, int mode)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200371{
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100372 struct fsmc_nand_data *host = nand_to_fsmc(chip);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200373
Boris Brezillon8f3931e2018-07-09 22:09:34 +0200374 writel_relaxed(readl(host->regs_va + FSMC_PC) & ~FSMC_ECCPLEN_256,
375 host->regs_va + FSMC_PC);
376 writel_relaxed(readl(host->regs_va + FSMC_PC) & ~FSMC_ECCEN,
377 host->regs_va + FSMC_PC);
378 writel_relaxed(readl(host->regs_va + FSMC_PC) | FSMC_ECCEN,
379 host->regs_va + FSMC_PC);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200380}
381
382/*
383 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300384 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200385 * max of 8-bits)
386 */
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200387static int fsmc_read_hwecc_ecc4(struct nand_chip *chip, const uint8_t *data,
Linus Walleij6c009ab2010-09-13 00:35:22 +0200388 uint8_t *ecc)
389{
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100390 struct fsmc_nand_data *host = nand_to_fsmc(chip);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200391 uint32_t ecc_tmp;
392 unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
393
394 do {
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100395 if (readl_relaxed(host->regs_va + STS) & FSMC_CODE_RDY)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200396 break;
397 else
398 cond_resched();
399 } while (!time_after_eq(jiffies, deadline));
400
Vipin Kumar712c4ad2012-03-14 11:47:16 +0530401 if (time_after_eq(jiffies, deadline)) {
402 dev_err(host->dev, "calculate ecc timed out\n");
403 return -ETIMEDOUT;
404 }
405
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100406 ecc_tmp = readl_relaxed(host->regs_va + ECC1);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200407 ecc[0] = (uint8_t) (ecc_tmp >> 0);
408 ecc[1] = (uint8_t) (ecc_tmp >> 8);
409 ecc[2] = (uint8_t) (ecc_tmp >> 16);
410 ecc[3] = (uint8_t) (ecc_tmp >> 24);
411
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100412 ecc_tmp = readl_relaxed(host->regs_va + ECC2);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200413 ecc[4] = (uint8_t) (ecc_tmp >> 0);
414 ecc[5] = (uint8_t) (ecc_tmp >> 8);
415 ecc[6] = (uint8_t) (ecc_tmp >> 16);
416 ecc[7] = (uint8_t) (ecc_tmp >> 24);
417
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100418 ecc_tmp = readl_relaxed(host->regs_va + ECC3);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200419 ecc[8] = (uint8_t) (ecc_tmp >> 0);
420 ecc[9] = (uint8_t) (ecc_tmp >> 8);
421 ecc[10] = (uint8_t) (ecc_tmp >> 16);
422 ecc[11] = (uint8_t) (ecc_tmp >> 24);
423
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100424 ecc_tmp = readl_relaxed(host->regs_va + STS);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200425 ecc[12] = (uint8_t) (ecc_tmp >> 16);
426
427 return 0;
428}
429
430/*
431 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300432 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200433 * max of 1-bit)
434 */
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200435static int fsmc_read_hwecc_ecc1(struct nand_chip *chip, const uint8_t *data,
Linus Walleij6c009ab2010-09-13 00:35:22 +0200436 uint8_t *ecc)
437{
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100438 struct fsmc_nand_data *host = nand_to_fsmc(chip);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200439 uint32_t ecc_tmp;
440
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100441 ecc_tmp = readl_relaxed(host->regs_va + ECC1);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200442 ecc[0] = (uint8_t) (ecc_tmp >> 0);
443 ecc[1] = (uint8_t) (ecc_tmp >> 8);
444 ecc[2] = (uint8_t) (ecc_tmp >> 16);
445
446 return 0;
447}
448
Vipin Kumar519300c2012-03-07 17:00:49 +0530449/* Count the number of 0's in buff upto a max of max_bits */
450static int count_written_bits(uint8_t *buff, int size, int max_bits)
451{
452 int k, written_bits = 0;
453
454 for (k = 0; k < size; k++) {
455 written_bits += hweight8(~buff[k]);
456 if (written_bits > max_bits)
457 break;
458 }
459
460 return written_bits;
461}
462
Vipin Kumar4774fb02012-03-14 11:47:18 +0530463static void dma_complete(void *param)
464{
465 struct fsmc_nand_data *host = param;
466
467 complete(&host->dma_access_complete);
468}
469
470static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len,
471 enum dma_data_direction direction)
472{
473 struct dma_chan *chan;
474 struct dma_device *dma_dev;
475 struct dma_async_tx_descriptor *tx;
476 dma_addr_t dma_dst, dma_src, dma_addr;
477 dma_cookie_t cookie;
478 unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
479 int ret;
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400480 unsigned long time_left;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530481
482 if (direction == DMA_TO_DEVICE)
483 chan = host->write_dma_chan;
484 else if (direction == DMA_FROM_DEVICE)
485 chan = host->read_dma_chan;
486 else
487 return -EINVAL;
488
489 dma_dev = chan->device;
490 dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction);
491
492 if (direction == DMA_TO_DEVICE) {
493 dma_src = dma_addr;
494 dma_dst = host->data_pa;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530495 } else {
496 dma_src = host->data_pa;
497 dma_dst = dma_addr;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530498 }
499
500 tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
501 len, flags);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530502 if (!tx) {
503 dev_err(host->dev, "device_prep_dma_memcpy error\n");
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000504 ret = -EIO;
505 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530506 }
507
508 tx->callback = dma_complete;
509 tx->callback_param = host;
510 cookie = tx->tx_submit(tx);
511
512 ret = dma_submit_error(cookie);
513 if (ret) {
514 dev_err(host->dev, "dma_submit_error %d\n", cookie);
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000515 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530516 }
517
518 dma_async_issue_pending(chan);
519
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400520 time_left =
Vipin Kumar928aa2a2012-10-09 16:14:48 +0530521 wait_for_completion_timeout(&host->dma_access_complete,
Vipin Kumar4774fb02012-03-14 11:47:18 +0530522 msecs_to_jiffies(3000));
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400523 if (time_left == 0) {
Vinod Koulb177ea32014-10-11 21:10:32 +0530524 dmaengine_terminate_all(chan);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530525 dev_err(host->dev, "wait_for_completion_timeout\n");
Nicholas Mc Guire0bda3e12015-03-13 07:54:45 -0400526 ret = -ETIMEDOUT;
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000527 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530528 }
529
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000530 ret = 0;
531
532unmap_dma:
533 dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
534
535 return ret;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530536}
537
Linus Walleij6c009ab2010-09-13 00:35:22 +0200538/*
Vipin Kumar604e7542012-03-14 11:47:17 +0530539 * fsmc_write_buf - write buffer to chip
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100540 * @host: FSMC NAND controller
Vipin Kumar604e7542012-03-14 11:47:17 +0530541 * @buf: data buffer
542 * @len: number of bytes to write
543 */
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100544static void fsmc_write_buf(struct fsmc_nand_data *host, const uint8_t *buf,
545 int len)
Vipin Kumar604e7542012-03-14 11:47:17 +0530546{
547 int i;
Vipin Kumar604e7542012-03-14 11:47:17 +0530548
Boris Brezillonf55824c2018-07-09 22:09:35 +0200549 if (IS_ALIGNED((uintptr_t)buf, sizeof(uint32_t)) &&
Vipin Kumar604e7542012-03-14 11:47:17 +0530550 IS_ALIGNED(len, sizeof(uint32_t))) {
551 uint32_t *p = (uint32_t *)buf;
552 len = len >> 2;
553 for (i = 0; i < len; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100554 writel_relaxed(p[i], host->data_va);
Vipin Kumar604e7542012-03-14 11:47:17 +0530555 } else {
556 for (i = 0; i < len; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100557 writeb_relaxed(buf[i], host->data_va);
Vipin Kumar604e7542012-03-14 11:47:17 +0530558 }
559}
560
561/*
562 * fsmc_read_buf - read chip data into buffer
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100563 * @host: FSMC NAND controller
Vipin Kumar604e7542012-03-14 11:47:17 +0530564 * @buf: buffer to store date
565 * @len: number of bytes to read
566 */
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100567static void fsmc_read_buf(struct fsmc_nand_data *host, uint8_t *buf, int len)
Vipin Kumar604e7542012-03-14 11:47:17 +0530568{
569 int i;
Vipin Kumar604e7542012-03-14 11:47:17 +0530570
Boris Brezillonf55824c2018-07-09 22:09:35 +0200571 if (IS_ALIGNED((uintptr_t)buf, sizeof(uint32_t)) &&
Vipin Kumar604e7542012-03-14 11:47:17 +0530572 IS_ALIGNED(len, sizeof(uint32_t))) {
573 uint32_t *p = (uint32_t *)buf;
574 len = len >> 2;
575 for (i = 0; i < len; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100576 p[i] = readl_relaxed(host->data_va);
Vipin Kumar604e7542012-03-14 11:47:17 +0530577 } else {
578 for (i = 0; i < len; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100579 buf[i] = readb_relaxed(host->data_va);
Vipin Kumar604e7542012-03-14 11:47:17 +0530580 }
581}
582
583/*
Vipin Kumar4774fb02012-03-14 11:47:18 +0530584 * fsmc_read_buf_dma - read chip data into buffer
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100585 * @host: FSMC NAND controller
Vipin Kumar4774fb02012-03-14 11:47:18 +0530586 * @buf: buffer to store date
587 * @len: number of bytes to read
588 */
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100589static void fsmc_read_buf_dma(struct fsmc_nand_data *host, uint8_t *buf,
590 int len)
Vipin Kumar4774fb02012-03-14 11:47:18 +0530591{
Vipin Kumar4774fb02012-03-14 11:47:18 +0530592 dma_xfer(host, buf, len, DMA_FROM_DEVICE);
593}
594
595/*
596 * fsmc_write_buf_dma - write buffer to chip
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100597 * @host: FSMC NAND controller
Vipin Kumar4774fb02012-03-14 11:47:18 +0530598 * @buf: data buffer
599 * @len: number of bytes to write
600 */
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100601static void fsmc_write_buf_dma(struct fsmc_nand_data *host, const uint8_t *buf,
602 int len)
Vipin Kumar4774fb02012-03-14 11:47:18 +0530603{
Vipin Kumar4774fb02012-03-14 11:47:18 +0530604 dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
605}
606
Miquel Raynal4da712e2018-02-16 15:22:48 +0100607/* fsmc_select_chip - assert or deassert nCE */
Boris Brezillon550b9fc2018-11-11 08:55:17 +0100608static void fsmc_ce_ctrl(struct fsmc_nand_data *host, bool assert)
Miquel Raynal4da712e2018-02-16 15:22:48 +0100609{
Boris Brezillon550b9fc2018-11-11 08:55:17 +0100610 u32 pc = readl(host->regs_va + FSMC_PC);
Miquel Raynal4da712e2018-02-16 15:22:48 +0100611
Boris Brezillon550b9fc2018-11-11 08:55:17 +0100612 if (!assert)
Boris Brezillon8f3931e2018-07-09 22:09:34 +0200613 writel_relaxed(pc & ~FSMC_ENABLE, host->regs_va + FSMC_PC);
Miquel Raynal4da712e2018-02-16 15:22:48 +0100614 else
Boris Brezillon8f3931e2018-07-09 22:09:34 +0200615 writel_relaxed(pc | FSMC_ENABLE, host->regs_va + FSMC_PC);
Miquel Raynal4da712e2018-02-16 15:22:48 +0100616
Boris Brezillon550b9fc2018-11-11 08:55:17 +0100617 /*
618 * nCE line changes must be applied before returning from this
619 * function.
620 */
Miquel Raynal4da712e2018-02-16 15:22:48 +0100621 mb();
622}
623
624/*
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
639 pr_debug("Executing operation [%d instructions]:\n", op->ninstrs);
Boris Brezillon550b9fc2018-11-11 08:55:17 +0100640
641 fsmc_ce_ctrl(host, true);
642
Miquel Raynal4da712e2018-02-16 15:22:48 +0100643 for (op_id = 0; op_id < op->ninstrs; op_id++) {
644 instr = &op->instrs[op_id];
645
646 switch (instr->type) {
647 case NAND_OP_CMD_INSTR:
648 pr_debug(" ->CMD [0x%02x]\n",
649 instr->ctx.cmd.opcode);
650
651 writeb_relaxed(instr->ctx.cmd.opcode, host->cmd_va);
652 break;
653
654 case NAND_OP_ADDR_INSTR:
655 pr_debug(" ->ADDR [%d cyc]",
656 instr->ctx.addr.naddrs);
657
658 for (i = 0; i < instr->ctx.addr.naddrs; i++)
659 writeb_relaxed(instr->ctx.addr.addrs[i],
660 host->addr_va);
661 break;
662
663 case NAND_OP_DATA_IN_INSTR:
664 pr_debug(" ->DATA_IN [%d B%s]\n", instr->ctx.data.len,
665 instr->ctx.data.force_8bit ?
666 ", force 8-bit" : "");
667
668 if (host->mode == USE_DMA_ACCESS)
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100669 fsmc_read_buf_dma(host, instr->ctx.data.buf.in,
Miquel Raynal4da712e2018-02-16 15:22:48 +0100670 instr->ctx.data.len);
671 else
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100672 fsmc_read_buf(host, instr->ctx.data.buf.in,
Miquel Raynal4da712e2018-02-16 15:22:48 +0100673 instr->ctx.data.len);
674 break;
675
676 case NAND_OP_DATA_OUT_INSTR:
677 pr_debug(" ->DATA_OUT [%d B%s]\n", instr->ctx.data.len,
678 instr->ctx.data.force_8bit ?
679 ", force 8-bit" : "");
680
681 if (host->mode == USE_DMA_ACCESS)
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100682 fsmc_write_buf_dma(host, instr->ctx.data.buf.out,
Miquel Raynal4da712e2018-02-16 15:22:48 +0100683 instr->ctx.data.len);
684 else
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100685 fsmc_write_buf(host, instr->ctx.data.buf.out,
Miquel Raynal4da712e2018-02-16 15:22:48 +0100686 instr->ctx.data.len);
687 break;
688
689 case NAND_OP_WAITRDY_INSTR:
690 pr_debug(" ->WAITRDY [max %d ms]\n",
691 instr->ctx.waitrdy.timeout_ms);
692
693 ret = nand_soft_waitrdy(chip,
694 instr->ctx.waitrdy.timeout_ms);
695 break;
696 }
697 }
698
Boris Brezillon550b9fc2018-11-11 08:55:17 +0100699 fsmc_ce_ctrl(host, false);
700
Miquel Raynal4da712e2018-02-16 15:22:48 +0100701 return ret;
702}
703
Vipin Kumar4774fb02012-03-14 11:47:18 +0530704/*
Linus Walleij6c009ab2010-09-13 00:35:22 +0200705 * fsmc_read_page_hwecc
Linus Walleij6c009ab2010-09-13 00:35:22 +0200706 * @chip: nand chip info structure
707 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -0700708 * @oob_required: caller expects OOB data read to chip->oob_poi
Linus Walleij6c009ab2010-09-13 00:35:22 +0200709 * @page: page number to read
710 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300711 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
Linus Walleij6c009ab2010-09-13 00:35:22 +0200712 * performed in a strict sequence as follows:
713 * data(512 byte) -> ecc(13 byte)
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300714 * After this read, fsmc hardware generates and reports error data bits(up to a
Linus Walleij6c009ab2010-09-13 00:35:22 +0200715 * max of 8 bits)
716 */
Boris Brezillonb9761682018-09-06 14:05:20 +0200717static int fsmc_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
718 int oob_required, int page)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200719{
Boris Brezillonb9761682018-09-06 14:05:20 +0200720 struct mtd_info *mtd = nand_to_mtd(chip);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200721 int i, j, s, stat, eccsize = chip->ecc.size;
722 int eccbytes = chip->ecc.bytes;
723 int eccsteps = chip->ecc.steps;
724 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +0900725 uint8_t *ecc_calc = chip->ecc.calc_buf;
726 uint8_t *ecc_code = chip->ecc.code_buf;
Gustavo A. R. Silva41d6f0d2018-10-10 17:58:58 +0200727 int off, len, ret, group = 0;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200728 /*
729 * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
730 * end up reading 14 bytes (7 words) from oob. The local array is
731 * to maintain word alignment
732 */
733 uint16_t ecc_oob[7];
734 uint8_t *oob = (uint8_t *)&ecc_oob[0];
Mike Dunn3f91e942012-04-25 12:06:09 -0700735 unsigned int max_bitflips = 0;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200736
737 for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100738 nand_read_page_op(chip, page, s * eccsize, NULL, 0);
Boris Brezillonec476362018-09-06 14:05:17 +0200739 chip->ecc.hwctl(chip, NAND_ECC_READ);
Gustavo A. R. Silva41d6f0d2018-10-10 17:58:58 +0200740 ret = nand_read_data_op(chip, p, eccsize, false);
741 if (ret)
742 return ret;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200743
744 for (j = 0; j < eccbytes;) {
Boris Brezillon04a123a2016-02-09 15:01:21 +0100745 struct mtd_oob_region oobregion;
Boris Brezillon04a123a2016-02-09 15:01:21 +0100746
747 ret = mtd_ooblayout_ecc(mtd, group++, &oobregion);
748 if (ret)
749 return ret;
750
751 off = oobregion.offset;
752 len = oobregion.length;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200753
754 /*
Vipin Kumar4cbe1bf02012-03-14 11:47:09 +0530755 * length is intentionally kept a higher multiple of 2
756 * to read at least 13 bytes even in case of 16 bit NAND
757 * devices
758 */
Vipin Kumaraea686b2012-03-14 11:47:10 +0530759 if (chip->options & NAND_BUSWIDTH_16)
760 len = roundup(len, 2);
761
Boris Brezillon97d90da2017-11-30 18:01:29 +0100762 nand_read_oob_op(chip, page, off, oob + j, len);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200763 j += len;
764 }
765
Vipin Kumar519300c2012-03-07 17:00:49 +0530766 memcpy(&ecc_code[i], oob, chip->ecc.bytes);
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200767 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200768
Boris Brezillon00da2ea2018-09-06 14:05:19 +0200769 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -0700770 if (stat < 0) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200771 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -0700772 } else {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200773 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -0700774 max_bitflips = max_t(unsigned int, max_bitflips, stat);
775 }
Linus Walleij6c009ab2010-09-13 00:35:22 +0200776 }
777
Mike Dunn3f91e942012-04-25 12:06:09 -0700778 return max_bitflips;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200779}
780
781/*
Armando Visconti753e0132012-03-07 17:00:54 +0530782 * fsmc_bch8_correct_data
Linus Walleij6c009ab2010-09-13 00:35:22 +0200783 * @mtd: mtd info structure
784 * @dat: buffer of read data
785 * @read_ecc: ecc read from device spare area
786 * @calc_ecc: ecc calculated from read data
787 *
788 * calc_ecc is a 104 bit information containing maximum of 8 error
789 * offset informations of 13 bits each in 512 bytes of read data.
790 */
Boris Brezillon00da2ea2018-09-06 14:05:19 +0200791static int fsmc_bch8_correct_data(struct nand_chip *chip, uint8_t *dat,
792 uint8_t *read_ecc, uint8_t *calc_ecc)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200793{
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100794 struct fsmc_nand_data *host = nand_to_fsmc(chip);
Armando Viscontia612c2a2012-03-07 17:00:53 +0530795 uint32_t err_idx[8];
Linus Walleij6c009ab2010-09-13 00:35:22 +0200796 uint32_t num_err, i;
Armando Visconti753e0132012-03-07 17:00:54 +0530797 uint32_t ecc1, ecc2, ecc3, ecc4;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200798
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100799 num_err = (readl_relaxed(host->regs_va + STS) >> 10) & 0xF;
Vipin Kumar519300c2012-03-07 17:00:49 +0530800
801 /* no bit flipping */
802 if (likely(num_err == 0))
803 return 0;
804
805 /* too many errors */
806 if (unlikely(num_err > 8)) {
807 /*
808 * This is a temporary erase check. A newly erased page read
809 * would result in an ecc error because the oob data is also
810 * erased to FF and the calculated ecc for an FF data is not
811 * FF..FF.
812 * This is a workaround to skip performing correction in case
813 * data is FF..FF
814 *
815 * Logic:
816 * For every page, each bit written as 0 is counted until these
817 * number of bits are greater than 8 (the maximum correction
818 * capability of FSMC for each 512 + 13 bytes)
819 */
820
821 int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
822 int bits_data = count_written_bits(dat, chip->ecc.size, 8);
823
824 if ((bits_ecc + bits_data) <= 8) {
825 if (bits_data)
826 memset(dat, 0xff, chip->ecc.size);
827 return bits_data;
828 }
829
830 return -EBADMSG;
831 }
832
Linus Walleij6c009ab2010-09-13 00:35:22 +0200833 /*
834 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
835 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
836 *
837 * calc_ecc is a 104 bit information containing maximum of 8 error
838 * offset informations of 13 bits each. calc_ecc is copied into a
839 * uint64_t array and error offset indexes are populated in err_idx
840 * array
841 */
Miquel Raynal4df6ed42018-02-16 15:22:47 +0100842 ecc1 = readl_relaxed(host->regs_va + ECC1);
843 ecc2 = readl_relaxed(host->regs_va + ECC2);
844 ecc3 = readl_relaxed(host->regs_va + ECC3);
845 ecc4 = readl_relaxed(host->regs_va + STS);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200846
Armando Visconti753e0132012-03-07 17:00:54 +0530847 err_idx[0] = (ecc1 >> 0) & 0x1FFF;
848 err_idx[1] = (ecc1 >> 13) & 0x1FFF;
849 err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
850 err_idx[3] = (ecc2 >> 7) & 0x1FFF;
851 err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
852 err_idx[5] = (ecc3 >> 1) & 0x1FFF;
853 err_idx[6] = (ecc3 >> 14) & 0x1FFF;
854 err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200855
856 i = 0;
857 while (num_err--) {
858 change_bit(0, (unsigned long *)&err_idx[i]);
859 change_bit(1, (unsigned long *)&err_idx[i]);
860
Vipin Kumarb533f8d2012-03-14 11:47:11 +0530861 if (err_idx[i] < chip->ecc.size * 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200862 change_bit(err_idx[i], (unsigned long *)dat);
863 i++;
864 }
865 }
866 return i;
867}
868
Vipin Kumar4774fb02012-03-14 11:47:18 +0530869static bool filter(struct dma_chan *chan, void *slave)
870{
871 chan->private = slave;
872 return true;
873}
874
Bill Pemberton06f25512012-11-19 13:23:07 -0500875static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100876 struct fsmc_nand_data *host,
877 struct nand_chip *nand)
Stefan Roeseeea62812012-03-16 10:19:31 +0100878{
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100879 struct device_node *np = pdev->dev.of_node;
Stefan Roeseeea62812012-03-16 10:19:31 +0100880 u32 val;
Stefan Roese62b57f42015-03-19 14:34:29 +0100881 int ret;
Stefan Roeseeea62812012-03-16 10:19:31 +0100882
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100883 nand->options = 0;
Thomas Petazzoniee568742017-03-21 11:03:53 +0100884
Stefan Roeseeea62812012-03-16 10:19:31 +0100885 if (!of_property_read_u32(np, "bank-width", &val)) {
886 if (val == 2) {
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100887 nand->options |= NAND_BUSWIDTH_16;
Stefan Roeseeea62812012-03-16 10:19:31 +0100888 } else if (val != 1) {
889 dev_err(&pdev->dev, "invalid bank-width %u\n", val);
890 return -EINVAL;
891 }
892 }
Thomas Petazzoniee568742017-03-21 11:03:53 +0100893
Stefan Roeseeea62812012-03-16 10:19:31 +0100894 if (of_get_property(np, "nand-skip-bbtscan", NULL))
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100895 nand->options |= NAND_SKIP_BBTSCAN;
Stefan Roeseeea62812012-03-16 10:19:31 +0100896
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100897 host->dev_timings = devm_kzalloc(&pdev->dev,
898 sizeof(*host->dev_timings), GFP_KERNEL);
899 if (!host->dev_timings)
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200900 return -ENOMEM;
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100901 ret = of_property_read_u8_array(np, "timings", (u8 *)host->dev_timings,
902 sizeof(*host->dev_timings));
Thomas Petazzonid9fb0792017-04-29 10:52:35 +0200903 if (ret)
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100904 host->dev_timings = NULL;
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200905
906 /* Set default NAND bank to 0 */
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100907 host->bank = 0;
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200908 if (!of_property_read_u32(np, "bank", &val)) {
909 if (val > 3) {
910 dev_err(&pdev->dev, "invalid bank %u\n", val);
911 return -EINVAL;
912 }
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100913 host->bank = val;
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200914 }
Stefan Roeseeea62812012-03-16 10:19:31 +0100915 return 0;
916}
Stefan Roeseeea62812012-03-16 10:19:31 +0100917
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200918static int fsmc_nand_attach_chip(struct nand_chip *nand)
919{
920 struct mtd_info *mtd = nand_to_mtd(nand);
Boris Brezillonbfc535f2018-11-20 10:02:30 +0100921 struct fsmc_nand_data *host = nand_to_fsmc(nand);
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200922
923 if (AMBA_REV_BITS(host->pid) >= 8) {
924 switch (mtd->oobsize) {
925 case 16:
926 case 64:
927 case 128:
928 case 224:
929 case 256:
930 break;
931 default:
932 dev_warn(host->dev,
933 "No oob scheme defined for oobsize %d\n",
934 mtd->oobsize);
935 return -EINVAL;
936 }
937
938 mtd_set_ooblayout(mtd, &fsmc_ecc4_ooblayout_ops);
939
940 return 0;
941 }
942
943 switch (nand->ecc.mode) {
944 case NAND_ECC_HW:
945 dev_info(host->dev, "Using 1-bit HW ECC scheme\n");
946 nand->ecc.calculate = fsmc_read_hwecc_ecc1;
947 nand->ecc.correct = nand_correct_data;
948 nand->ecc.bytes = 3;
949 nand->ecc.strength = 1;
Boris Brezillon309600c2018-09-04 16:23:28 +0200950 nand->ecc.options |= NAND_ECC_SOFT_HAMMING_SM_ORDER;
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200951 break;
952
953 case NAND_ECC_SOFT:
954 if (nand->ecc.algo == NAND_ECC_BCH) {
955 dev_info(host->dev,
956 "Using 4-bit SW BCH ECC scheme\n");
957 break;
958 }
959
960 case NAND_ECC_ON_DIE:
961 break;
962
963 default:
964 dev_err(host->dev, "Unsupported ECC mode!\n");
965 return -ENOTSUPP;
966 }
967
968 /*
969 * Don't set layout for BCH4 SW ECC. This will be
970 * generated later in nand_bch_init() later.
971 */
972 if (nand->ecc.mode == NAND_ECC_HW) {
973 switch (mtd->oobsize) {
974 case 16:
975 case 64:
976 case 128:
977 mtd_set_ooblayout(mtd,
978 &fsmc_ecc1_ooblayout_ops);
979 break;
980 default:
981 dev_warn(host->dev,
982 "No oob scheme defined for oobsize %d\n",
983 mtd->oobsize);
984 return -EINVAL;
985 }
986 }
987
988 return 0;
989}
990
991static const struct nand_controller_ops fsmc_nand_controller_ops = {
992 .attach_chip = fsmc_nand_attach_chip,
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100993 .exec_op = fsmc_exec_op,
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100994 .setup_data_interface = fsmc_setup_data_interface,
Miquel Raynal3bbddfa2018-07-20 17:14:59 +0200995};
996
Linus Walleij6c009ab2010-09-13 00:35:22 +0200997/*
998 * fsmc_nand_probe - Probe function
999 * @pdev: platform device structure
1000 */
1001static int __init fsmc_nand_probe(struct platform_device *pdev)
1002{
Linus Walleij6c009ab2010-09-13 00:35:22 +02001003 struct fsmc_nand_data *host;
1004 struct mtd_info *mtd;
1005 struct nand_chip *nand;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001006 struct resource *res;
Miquel Raynal4df6ed42018-02-16 15:22:47 +01001007 void __iomem *base;
Vipin Kumar4774fb02012-03-14 11:47:18 +05301008 dma_cap_mask_t mask;
Linus Walleij4ad916b2010-11-29 13:52:06 +01001009 int ret = 0;
Linus Walleij593cd872010-11-29 13:52:19 +01001010 u32 pid;
1011 int i;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001012
Linus Walleij6c009ab2010-09-13 00:35:22 +02001013 /* Allocate memory for the device structure (and zero it) */
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301014 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Jingoo Hand9a21ae2013-12-26 12:16:38 +09001015 if (!host)
Linus Walleij6c009ab2010-09-13 00:35:22 +02001016 return -ENOMEM;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001017
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +01001018 nand = &host->nand;
1019
1020 ret = fsmc_nand_probe_config_dt(pdev, host, nand);
1021 if (ret)
1022 return ret;
1023
Linus Walleij6c009ab2010-09-13 00:35:22 +02001024 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
Thierry Redingb0de7742013-01-21 11:09:12 +01001025 host->data_va = devm_ioremap_resource(&pdev->dev, res);
1026 if (IS_ERR(host->data_va))
1027 return PTR_ERR(host->data_va);
Stefan Roesecbf29b82015-10-02 12:40:20 +02001028
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +02001029 host->data_pa = (dma_addr_t)res->start;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001030
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +02001031 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
Thierry Redingb0de7742013-01-21 11:09:12 +01001032 host->addr_va = devm_ioremap_resource(&pdev->dev, res);
1033 if (IS_ERR(host->addr_va))
1034 return PTR_ERR(host->addr_va);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001035
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +02001036 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
Thierry Redingb0de7742013-01-21 11:09:12 +01001037 host->cmd_va = devm_ioremap_resource(&pdev->dev, res);
1038 if (IS_ERR(host->cmd_va))
1039 return PTR_ERR(host->cmd_va);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001040
1041 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
Miquel Raynal4df6ed42018-02-16 15:22:47 +01001042 base = devm_ioremap_resource(&pdev->dev, res);
1043 if (IS_ERR(base))
1044 return PTR_ERR(base);
1045
1046 host->regs_va = base + FSMC_NOR_REG_SIZE +
1047 (host->bank * FSMC_NAND_BANK_SZ);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001048
Thomas Petazzonifb8ed2c2017-03-21 11:04:03 +01001049 host->clk = devm_clk_get(&pdev->dev, NULL);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001050 if (IS_ERR(host->clk)) {
1051 dev_err(&pdev->dev, "failed to fetch block clock\n");
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301052 return PTR_ERR(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001053 }
1054
Viresh Kumare25da1c2012-04-17 17:07:57 +05301055 ret = clk_prepare_enable(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001056 if (ret)
Thomas Petazzonifb8ed2c2017-03-21 11:04:03 +01001057 return ret;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001058
Linus Walleij593cd872010-11-29 13:52:19 +01001059 /*
1060 * This device ID is actually a common AMBA ID as used on the
1061 * AMBA PrimeCell bus. However it is not a PrimeCell.
1062 */
1063 for (pid = 0, i = 0; i < 4; i++)
Miquel Raynal4df6ed42018-02-16 15:22:47 +01001064 pid |= (readl(base + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
Linus Walleij593cd872010-11-29 13:52:19 +01001065 host->pid = pid;
1066 dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
1067 "revision %02x, config %02x\n",
1068 AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
1069 AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
1070
Vipin Kumar712c4ad2012-03-14 11:47:16 +05301071 host->dev = &pdev->dev;
Vipin Kumar4774fb02012-03-14 11:47:18 +05301072
1073 if (host->mode == USE_DMA_ACCESS)
1074 init_completion(&host->dma_access_complete);
1075
Linus Walleij6c009ab2010-09-13 00:35:22 +02001076 /* Link all private pointers */
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001077 mtd = nand_to_mtd(&host->nand);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001078 nand_set_controller_data(nand, host);
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +01001079 nand_set_flash_node(nand, pdev->dev.of_node);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001080
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001081 mtd->dev.parent = &pdev->dev;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001082
Stefan Roesee278fc72015-10-19 08:40:13 +02001083 /*
1084 * Setup default ECC mode. nand_dt_init() called from nand_scan_ident()
1085 * can overwrite this value if the DT provides a different value.
1086 */
Linus Walleij6c009ab2010-09-13 00:35:22 +02001087 nand->ecc.mode = NAND_ECC_HW;
1088 nand->ecc.hwctl = fsmc_enable_hwecc;
1089 nand->ecc.size = 512;
Vipin Kumar467e6e72012-03-14 11:47:12 +05301090 nand->badblockbits = 7;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001091
Miquel Raynal4da712e2018-02-16 15:22:48 +01001092 if (host->mode == USE_DMA_ACCESS) {
Vipin Kumar4774fb02012-03-14 11:47:18 +05301093 dma_cap_zero(mask);
1094 dma_cap_set(DMA_MEMCPY, mask);
Thomas Petazzonifeb1e572017-03-21 11:03:59 +01001095 host->read_dma_chan = dma_request_channel(mask, filter, NULL);
Vipin Kumar4774fb02012-03-14 11:47:18 +05301096 if (!host->read_dma_chan) {
1097 dev_err(&pdev->dev, "Unable to get read dma channel\n");
Miquel Raynal43fab012018-04-21 20:00:36 +02001098 goto disable_clk;
Vipin Kumar4774fb02012-03-14 11:47:18 +05301099 }
Thomas Petazzonifeb1e572017-03-21 11:03:59 +01001100 host->write_dma_chan = dma_request_channel(mask, filter, NULL);
Vipin Kumar4774fb02012-03-14 11:47:18 +05301101 if (!host->write_dma_chan) {
1102 dev_err(&pdev->dev, "Unable to get write dma channel\n");
Miquel Raynal43fab012018-04-21 20:00:36 +02001103 goto release_dma_read_chan;
Vipin Kumar4774fb02012-03-14 11:47:18 +05301104 }
Vipin Kumar604e7542012-03-14 11:47:17 +05301105 }
1106
Boris Brezillon7a08dba2018-11-11 08:55:24 +01001107 if (host->dev_timings) {
Thomas Petazzonid9fb0792017-04-29 10:52:35 +02001108 fsmc_nand_setup(host, host->dev_timings);
Boris Brezillon7a08dba2018-11-11 08:55:24 +01001109 nand->options |= NAND_KEEP_TIMINGS;
1110 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001111
Linus Walleij593cd872010-11-29 13:52:19 +01001112 if (AMBA_REV_BITS(host->pid) >= 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +02001113 nand->ecc.read_page = fsmc_read_page_hwecc;
1114 nand->ecc.calculate = fsmc_read_hwecc_ecc4;
Armando Visconti753e0132012-03-07 17:00:54 +05301115 nand->ecc.correct = fsmc_bch8_correct_data;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001116 nand->ecc.bytes = 13;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001117 nand->ecc.strength = 8;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001118 }
1119
1120 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001121 * Scan to find existence of the device
Linus Walleij6c009ab2010-09-13 00:35:22 +02001122 */
Miquel Raynal3bbddfa2018-07-20 17:14:59 +02001123 nand->dummy_controller.ops = &fsmc_nand_controller_ops;
Boris Brezillon00ad3782018-09-06 14:05:14 +02001124 ret = nand_scan(nand, 1);
Masahiro Yamadaad5678e2016-11-04 19:43:00 +09001125 if (ret)
Miquel Raynal43fab012018-04-21 20:00:36 +02001126 goto release_dma_write_chan;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001127
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001128 mtd->name = "nand";
Thomas Petazzoniede29a02017-03-21 11:04:00 +01001129 ret = mtd_device_register(mtd, NULL, 0);
Jamie Iles99335d02011-05-23 10:23:23 +01001130 if (ret)
Miquel Raynal682cae22018-04-21 20:00:37 +02001131 goto cleanup_nand;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001132
1133 platform_set_drvdata(pdev, host);
1134 dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
Miquel Raynal43fab012018-04-21 20:00:36 +02001135
Linus Walleij6c009ab2010-09-13 00:35:22 +02001136 return 0;
1137
Miquel Raynal682cae22018-04-21 20:00:37 +02001138cleanup_nand:
1139 nand_cleanup(nand);
Miquel Raynal43fab012018-04-21 20:00:36 +02001140release_dma_write_chan:
Vipin Kumar4774fb02012-03-14 11:47:18 +05301141 if (host->mode == USE_DMA_ACCESS)
1142 dma_release_channel(host->write_dma_chan);
Miquel Raynal43fab012018-04-21 20:00:36 +02001143release_dma_read_chan:
Vipin Kumar4774fb02012-03-14 11:47:18 +05301144 if (host->mode == USE_DMA_ACCESS)
1145 dma_release_channel(host->read_dma_chan);
Miquel Raynal43fab012018-04-21 20:00:36 +02001146disable_clk:
Viresh Kumare25da1c2012-04-17 17:07:57 +05301147 clk_disable_unprepare(host->clk);
Miquel Raynal43fab012018-04-21 20:00:36 +02001148
Linus Walleij6c009ab2010-09-13 00:35:22 +02001149 return ret;
1150}
1151
1152/*
1153 * Clean up routine
1154 */
1155static int fsmc_nand_remove(struct platform_device *pdev)
1156{
1157 struct fsmc_nand_data *host = platform_get_drvdata(pdev);
1158
Linus Walleij6c009ab2010-09-13 00:35:22 +02001159 if (host) {
Boris Brezillon59ac2762018-09-06 14:05:15 +02001160 nand_release(&host->nand);
Vipin Kumar4774fb02012-03-14 11:47:18 +05301161
1162 if (host->mode == USE_DMA_ACCESS) {
1163 dma_release_channel(host->write_dma_chan);
1164 dma_release_channel(host->read_dma_chan);
1165 }
Viresh Kumare25da1c2012-04-17 17:07:57 +05301166 clk_disable_unprepare(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001167 }
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301168
Linus Walleij6c009ab2010-09-13 00:35:22 +02001169 return 0;
1170}
1171
Jingoo Han80ce4dd2013-03-26 15:53:48 +09001172#ifdef CONFIG_PM_SLEEP
Linus Walleij6c009ab2010-09-13 00:35:22 +02001173static int fsmc_nand_suspend(struct device *dev)
1174{
1175 struct fsmc_nand_data *host = dev_get_drvdata(dev);
1176 if (host)
Viresh Kumare25da1c2012-04-17 17:07:57 +05301177 clk_disable_unprepare(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001178 return 0;
1179}
1180
1181static int fsmc_nand_resume(struct device *dev)
1182{
1183 struct fsmc_nand_data *host = dev_get_drvdata(dev);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301184 if (host) {
Viresh Kumare25da1c2012-04-17 17:07:57 +05301185 clk_prepare_enable(host->clk);
Thomas Petazzonid9fb0792017-04-29 10:52:35 +02001186 if (host->dev_timings)
1187 fsmc_nand_setup(host, host->dev_timings);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301188 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001189 return 0;
1190}
Jingoo Han80ce4dd2013-03-26 15:53:48 +09001191#endif
Linus Walleij6c009ab2010-09-13 00:35:22 +02001192
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301193static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001194
Stefan Roeseeea62812012-03-16 10:19:31 +01001195static const struct of_device_id fsmc_nand_id_table[] = {
1196 { .compatible = "st,spear600-fsmc-nand" },
Linus Walleijba785202013-01-05 22:28:32 +01001197 { .compatible = "stericsson,fsmc-nand" },
Stefan Roeseeea62812012-03-16 10:19:31 +01001198 {}
1199};
1200MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);
Stefan Roeseeea62812012-03-16 10:19:31 +01001201
Linus Walleij6c009ab2010-09-13 00:35:22 +02001202static struct platform_driver fsmc_nand_driver = {
1203 .remove = fsmc_nand_remove,
1204 .driver = {
Linus Walleij6c009ab2010-09-13 00:35:22 +02001205 .name = "fsmc-nand",
Thomas Petazzoni33575b22017-03-21 11:04:05 +01001206 .of_match_table = fsmc_nand_id_table,
Linus Walleij6c009ab2010-09-13 00:35:22 +02001207 .pm = &fsmc_nand_pm_ops,
Linus Walleij6c009ab2010-09-13 00:35:22 +02001208 },
1209};
1210
Jingoo Han307d2a512013-03-05 13:30:36 +09001211module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001212
1213MODULE_LICENSE("GPL");
1214MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
1215MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");