blob: 43108ddd7bdd0db166842be59595aa03716cd887 [file] [log] [blame]
Linus Walleij6c009ab2010-09-13 00:35:22 +02001/*
2 * drivers/mtd/nand/fsmc_nand.c
3 *
4 * ST Microelectronics
5 * Flexible Static Memory Controller (FSMC)
6 * Driver for NAND portions
7 *
8 * Copyright © 2010 ST Microelectronics
9 * Vipin Kumar <vipin.kumar@st.com>
10 * Ashish Priyadarshi
11 *
12 * Based on drivers/mtd/nand/nomadik_nand.c
13 *
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>
31#include <linux/mtd/nand.h>
32#include <linux/mtd/nand_ecc.h>
33#include <linux/platform_device.h>
Stefan Roeseeea62812012-03-16 10:19:31 +010034#include <linux/of.h>
Linus Walleij6c009ab2010-09-13 00:35:22 +020035#include <linux/mtd/partitions.h>
36#include <linux/io.h>
37#include <linux/slab.h>
Linus Walleij593cd872010-11-29 13:52:19 +010038#include <linux/amba/bus.h>
Linus Walleij6c009ab2010-09-13 00:35:22 +020039#include <mtd/mtd-abi.h>
40
Linus Walleij4404d7d2016-12-18 12:34:55 +010041/* fsmc controller registers for NOR flash */
42#define CTRL 0x0
43 /* ctrl register definitions */
44 #define BANK_ENABLE (1 << 0)
45 #define MUXED (1 << 1)
46 #define NOR_DEV (2 << 2)
47 #define WIDTH_8 (0 << 4)
48 #define WIDTH_16 (1 << 4)
49 #define RSTPWRDWN (1 << 6)
50 #define WPROT (1 << 7)
51 #define WRT_ENABLE (1 << 12)
52 #define WAIT_ENB (1 << 13)
53
54#define CTRL_TIM 0x4
55 /* ctrl_tim register definitions */
56
57#define FSMC_NOR_BANK_SZ 0x8
58#define FSMC_NOR_REG_SIZE 0x40
59
60#define FSMC_NOR_REG(base, bank, reg) (base + \
61 FSMC_NOR_BANK_SZ * (bank) + \
62 reg)
63
64/* fsmc controller registers for NAND flash */
65#define PC 0x00
66 /* pc register definitions */
67 #define FSMC_RESET (1 << 0)
68 #define FSMC_WAITON (1 << 1)
69 #define FSMC_ENABLE (1 << 2)
70 #define FSMC_DEVTYPE_NAND (1 << 3)
71 #define FSMC_DEVWID_8 (0 << 4)
72 #define FSMC_DEVWID_16 (1 << 4)
73 #define FSMC_ECCEN (1 << 6)
74 #define FSMC_ECCPLEN_512 (0 << 7)
75 #define FSMC_ECCPLEN_256 (1 << 7)
76 #define FSMC_TCLR_1 (1)
77 #define FSMC_TCLR_SHIFT (9)
78 #define FSMC_TCLR_MASK (0xF)
79 #define FSMC_TAR_1 (1)
80 #define FSMC_TAR_SHIFT (13)
81 #define FSMC_TAR_MASK (0xF)
82#define STS 0x04
83 /* sts register definitions */
84 #define FSMC_CODE_RDY (1 << 15)
85#define COMM 0x08
86 /* comm register definitions */
87 #define FSMC_TSET_0 0
88 #define FSMC_TSET_SHIFT 0
89 #define FSMC_TSET_MASK 0xFF
90 #define FSMC_TWAIT_6 6
91 #define FSMC_TWAIT_SHIFT 8
92 #define FSMC_TWAIT_MASK 0xFF
93 #define FSMC_THOLD_4 4
94 #define FSMC_THOLD_SHIFT 16
95 #define FSMC_THOLD_MASK 0xFF
96 #define FSMC_THIZ_1 1
97 #define FSMC_THIZ_SHIFT 24
98 #define FSMC_THIZ_MASK 0xFF
99#define ATTRIB 0x0C
100#define IOATA 0x10
101#define ECC1 0x14
102#define ECC2 0x18
103#define ECC3 0x1C
104#define FSMC_NAND_BANK_SZ 0x20
105
106#define FSMC_NAND_REG(base, bank, reg) (base + FSMC_NOR_REG_SIZE + \
107 (FSMC_NAND_BANK_SZ * (bank)) + \
108 reg)
109
110#define FSMC_BUSY_WAIT_TIMEOUT (1 * HZ)
111
112struct fsmc_nand_timings {
113 uint8_t tclr;
114 uint8_t tar;
115 uint8_t thiz;
116 uint8_t thold;
117 uint8_t twait;
118 uint8_t tset;
119};
120
121enum access_mode {
122 USE_DMA_ACCESS = 1,
123 USE_WORD_ACCESS,
124};
125
126/**
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100127 * struct fsmc_nand_data - structure for FSMC NAND device state
128 *
129 * @pid: Part ID on the AMBA PrimeCell format
130 * @mtd: MTD info for a NAND flash.
131 * @nand: Chip related info for a NAND flash.
132 * @partitions: Partition info for a NAND Flash.
133 * @nr_partitions: Total number of partition of a NAND flash.
134 *
135 * @bank: Bank number for probed device.
136 * @clk: Clock structure for FSMC.
137 *
138 * @read_dma_chan: DMA channel for read access
139 * @write_dma_chan: DMA channel for write access to NAND
140 * @dma_access_complete: Completion structure
141 *
142 * @data_pa: NAND Physical port for Data.
143 * @data_va: NAND port for Data.
144 * @cmd_va: NAND port for Command.
145 * @addr_va: NAND port for Address.
146 * @regs_va: FSMC regs base address.
147 */
148struct fsmc_nand_data {
149 u32 pid;
150 struct nand_chip nand;
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100151
152 unsigned int bank;
153 struct device *dev;
154 enum access_mode mode;
155 struct clk *clk;
156
157 /* DMA related objects */
158 struct dma_chan *read_dma_chan;
159 struct dma_chan *write_dma_chan;
160 struct completion dma_access_complete;
161
162 struct fsmc_nand_timings *dev_timings;
163
164 dma_addr_t data_pa;
165 void __iomem *data_va;
166 void __iomem *cmd_va;
167 void __iomem *addr_va;
168 void __iomem *regs_va;
Thomas Petazzonie7cda012017-03-21 11:03:56 +0100169};
170
Boris Brezillon22b46952016-02-03 20:01:42 +0100171static int fsmc_ecc1_ooblayout_ecc(struct mtd_info *mtd, int section,
172 struct mtd_oob_region *oobregion)
173{
174 struct nand_chip *chip = mtd_to_nand(mtd);
175
176 if (section >= chip->ecc.steps)
177 return -ERANGE;
178
179 oobregion->offset = (section * 16) + 2;
180 oobregion->length = 3;
181
182 return 0;
183}
184
185static int fsmc_ecc1_ooblayout_free(struct mtd_info *mtd, int section,
186 struct mtd_oob_region *oobregion)
187{
188 struct nand_chip *chip = mtd_to_nand(mtd);
189
190 if (section >= chip->ecc.steps)
191 return -ERANGE;
192
193 oobregion->offset = (section * 16) + 8;
194
195 if (section < chip->ecc.steps - 1)
196 oobregion->length = 8;
197 else
198 oobregion->length = mtd->oobsize - oobregion->offset;
199
200 return 0;
201}
202
203static const struct mtd_ooblayout_ops fsmc_ecc1_ooblayout_ops = {
204 .ecc = fsmc_ecc1_ooblayout_ecc,
205 .free = fsmc_ecc1_ooblayout_free,
206};
207
Boris Brezillon04a123a2016-02-09 15:01:21 +0100208/*
209 * ECC placement definitions in oobfree type format.
210 * There are 13 bytes of ecc for every 512 byte block and it has to be read
211 * consecutively and immediately after the 512 byte data block for hardware to
212 * generate the error bit offsets in 512 byte data.
213 */
Boris Brezillon22b46952016-02-03 20:01:42 +0100214static int fsmc_ecc4_ooblayout_ecc(struct mtd_info *mtd, int section,
215 struct mtd_oob_region *oobregion)
216{
217 struct nand_chip *chip = mtd_to_nand(mtd);
218
219 if (section >= chip->ecc.steps)
220 return -ERANGE;
221
222 oobregion->length = chip->ecc.bytes;
223
224 if (!section && mtd->writesize <= 512)
225 oobregion->offset = 0;
226 else
227 oobregion->offset = (section * 16) + 2;
228
229 return 0;
230}
231
232static int fsmc_ecc4_ooblayout_free(struct mtd_info *mtd, int section,
233 struct mtd_oob_region *oobregion)
234{
235 struct nand_chip *chip = mtd_to_nand(mtd);
236
237 if (section >= chip->ecc.steps)
238 return -ERANGE;
239
240 oobregion->offset = (section * 16) + 15;
241
242 if (section < chip->ecc.steps - 1)
243 oobregion->length = 3;
244 else
245 oobregion->length = mtd->oobsize - oobregion->offset;
246
247 return 0;
248}
249
250static const struct mtd_ooblayout_ops fsmc_ecc4_ooblayout_ops = {
251 .ecc = fsmc_ecc4_ooblayout_ecc,
252 .free = fsmc_ecc4_ooblayout_free,
253};
254
Boris BREZILLON277af422015-12-10 08:59:46 +0100255static inline struct fsmc_nand_data *mtd_to_fsmc(struct mtd_info *mtd)
256{
Boris BREZILLONbdf3a552015-12-10 09:00:05 +0100257 return container_of(mtd_to_nand(mtd), struct fsmc_nand_data, nand);
Boris BREZILLON277af422015-12-10 08:59:46 +0100258}
259
Linus Walleij6c009ab2010-09-13 00:35:22 +0200260/*
261 * fsmc_cmd_ctrl - For facilitaing Hardware access
262 * This routine allows hardware specific access to control-lines(ALE,CLE)
263 */
264static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
265{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100266 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLON277af422015-12-10 08:59:46 +0100267 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar605add72012-10-09 16:14:43 +0530268 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200269 unsigned int bank = host->bank;
270
271 if (ctrl & NAND_CTRL_CHANGE) {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530272 u32 pc;
273
Linus Walleij6c009ab2010-09-13 00:35:22 +0200274 if (ctrl & NAND_CLE) {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530275 this->IO_ADDR_R = host->cmd_va;
276 this->IO_ADDR_W = host->cmd_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200277 } else if (ctrl & NAND_ALE) {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530278 this->IO_ADDR_R = host->addr_va;
279 this->IO_ADDR_W = host->addr_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200280 } else {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530281 this->IO_ADDR_R = host->data_va;
282 this->IO_ADDR_W = host->data_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200283 }
284
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530285 pc = readl(FSMC_NAND_REG(regs, bank, PC));
286 if (ctrl & NAND_NCE)
287 pc |= FSMC_ENABLE;
288 else
289 pc &= ~FSMC_ENABLE;
Vipin Kumara4742d52012-10-09 16:14:50 +0530290 writel_relaxed(pc, FSMC_NAND_REG(regs, bank, PC));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200291 }
292
293 mb();
294
295 if (cmd != NAND_CMD_NONE)
Vipin Kumara4742d52012-10-09 16:14:50 +0530296 writeb_relaxed(cmd, this->IO_ADDR_W);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200297}
298
299/*
300 * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
301 *
302 * This routine initializes timing parameters related to NAND memory access in
303 * FSMC registers
304 */
Thomas Petazzoni6335b502017-04-29 10:52:34 +0200305static void fsmc_nand_setup(struct fsmc_nand_data *host,
306 struct fsmc_nand_timings *timings)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200307{
308 uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530309 uint32_t tclr, tar, thiz, thold, twait, tset;
Thomas Petazzoni6335b502017-04-29 10:52:34 +0200310 unsigned int bank = host->bank;
311 void __iomem *regs = host->regs_va;
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530312 struct fsmc_nand_timings *tims;
313 struct fsmc_nand_timings default_timings = {
314 .tclr = FSMC_TCLR_1,
315 .tar = FSMC_TAR_1,
316 .thiz = FSMC_THIZ_1,
317 .thold = FSMC_THOLD_4,
318 .twait = FSMC_TWAIT_6,
319 .tset = FSMC_TSET_0,
320 };
321
322 if (timings)
Thomas Petazzoni6335b502017-04-29 10:52:34 +0200323 tims = host->dev_timings;
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530324 else
325 tims = &default_timings;
326
327 tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT;
328 tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT;
329 thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT;
330 thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT;
331 twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT;
332 tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200333
Thomas Petazzoni6335b502017-04-29 10:52:34 +0200334 if (host->nand.options & NAND_BUSWIDTH_16)
Vipin Kumara4742d52012-10-09 16:14:50 +0530335 writel_relaxed(value | FSMC_DEVWID_16,
336 FSMC_NAND_REG(regs, bank, PC));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200337 else
Vipin Kumara4742d52012-10-09 16:14:50 +0530338 writel_relaxed(value | FSMC_DEVWID_8,
339 FSMC_NAND_REG(regs, bank, PC));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200340
Vipin Kumara4742d52012-10-09 16:14:50 +0530341 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) | tclr | tar,
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530342 FSMC_NAND_REG(regs, bank, PC));
Vipin Kumara4742d52012-10-09 16:14:50 +0530343 writel_relaxed(thiz | thold | twait | tset,
344 FSMC_NAND_REG(regs, bank, COMM));
345 writel_relaxed(thiz | thold | twait | tset,
346 FSMC_NAND_REG(regs, bank, ATTRIB));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200347}
348
349/*
350 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
351 */
352static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
353{
Boris BREZILLON277af422015-12-10 08:59:46 +0100354 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530355 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200356 uint32_t bank = host->bank;
357
Vipin Kumara4742d52012-10-09 16:14:50 +0530358 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCPLEN_256,
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530359 FSMC_NAND_REG(regs, bank, PC));
Vipin Kumara4742d52012-10-09 16:14:50 +0530360 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCEN,
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530361 FSMC_NAND_REG(regs, bank, PC));
Vipin Kumara4742d52012-10-09 16:14:50 +0530362 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) | FSMC_ECCEN,
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530363 FSMC_NAND_REG(regs, bank, PC));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200364}
365
366/*
367 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300368 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200369 * max of 8-bits)
370 */
371static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
372 uint8_t *ecc)
373{
Boris BREZILLON277af422015-12-10 08:59:46 +0100374 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530375 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200376 uint32_t bank = host->bank;
377 uint32_t ecc_tmp;
378 unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
379
380 do {
Vipin Kumara4742d52012-10-09 16:14:50 +0530381 if (readl_relaxed(FSMC_NAND_REG(regs, bank, STS)) & FSMC_CODE_RDY)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200382 break;
383 else
384 cond_resched();
385 } while (!time_after_eq(jiffies, deadline));
386
Vipin Kumar712c4ad2012-03-14 11:47:16 +0530387 if (time_after_eq(jiffies, deadline)) {
388 dev_err(host->dev, "calculate ecc timed out\n");
389 return -ETIMEDOUT;
390 }
391
Vipin Kumara4742d52012-10-09 16:14:50 +0530392 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200393 ecc[0] = (uint8_t) (ecc_tmp >> 0);
394 ecc[1] = (uint8_t) (ecc_tmp >> 8);
395 ecc[2] = (uint8_t) (ecc_tmp >> 16);
396 ecc[3] = (uint8_t) (ecc_tmp >> 24);
397
Vipin Kumara4742d52012-10-09 16:14:50 +0530398 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC2));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200399 ecc[4] = (uint8_t) (ecc_tmp >> 0);
400 ecc[5] = (uint8_t) (ecc_tmp >> 8);
401 ecc[6] = (uint8_t) (ecc_tmp >> 16);
402 ecc[7] = (uint8_t) (ecc_tmp >> 24);
403
Vipin Kumara4742d52012-10-09 16:14:50 +0530404 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC3));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200405 ecc[8] = (uint8_t) (ecc_tmp >> 0);
406 ecc[9] = (uint8_t) (ecc_tmp >> 8);
407 ecc[10] = (uint8_t) (ecc_tmp >> 16);
408 ecc[11] = (uint8_t) (ecc_tmp >> 24);
409
Vipin Kumara4742d52012-10-09 16:14:50 +0530410 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, STS));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200411 ecc[12] = (uint8_t) (ecc_tmp >> 16);
412
413 return 0;
414}
415
416/*
417 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300418 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200419 * max of 1-bit)
420 */
421static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
422 uint8_t *ecc)
423{
Boris BREZILLON277af422015-12-10 08:59:46 +0100424 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530425 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200426 uint32_t bank = host->bank;
427 uint32_t ecc_tmp;
428
Vipin Kumara4742d52012-10-09 16:14:50 +0530429 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200430 ecc[0] = (uint8_t) (ecc_tmp >> 0);
431 ecc[1] = (uint8_t) (ecc_tmp >> 8);
432 ecc[2] = (uint8_t) (ecc_tmp >> 16);
433
434 return 0;
435}
436
Vipin Kumar519300c2012-03-07 17:00:49 +0530437/* Count the number of 0's in buff upto a max of max_bits */
438static int count_written_bits(uint8_t *buff, int size, int max_bits)
439{
440 int k, written_bits = 0;
441
442 for (k = 0; k < size; k++) {
443 written_bits += hweight8(~buff[k]);
444 if (written_bits > max_bits)
445 break;
446 }
447
448 return written_bits;
449}
450
Vipin Kumar4774fb02012-03-14 11:47:18 +0530451static void dma_complete(void *param)
452{
453 struct fsmc_nand_data *host = param;
454
455 complete(&host->dma_access_complete);
456}
457
458static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len,
459 enum dma_data_direction direction)
460{
461 struct dma_chan *chan;
462 struct dma_device *dma_dev;
463 struct dma_async_tx_descriptor *tx;
464 dma_addr_t dma_dst, dma_src, dma_addr;
465 dma_cookie_t cookie;
466 unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
467 int ret;
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400468 unsigned long time_left;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530469
470 if (direction == DMA_TO_DEVICE)
471 chan = host->write_dma_chan;
472 else if (direction == DMA_FROM_DEVICE)
473 chan = host->read_dma_chan;
474 else
475 return -EINVAL;
476
477 dma_dev = chan->device;
478 dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction);
479
480 if (direction == DMA_TO_DEVICE) {
481 dma_src = dma_addr;
482 dma_dst = host->data_pa;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530483 } else {
484 dma_src = host->data_pa;
485 dma_dst = dma_addr;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530486 }
487
488 tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
489 len, flags);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530490 if (!tx) {
491 dev_err(host->dev, "device_prep_dma_memcpy error\n");
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000492 ret = -EIO;
493 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530494 }
495
496 tx->callback = dma_complete;
497 tx->callback_param = host;
498 cookie = tx->tx_submit(tx);
499
500 ret = dma_submit_error(cookie);
501 if (ret) {
502 dev_err(host->dev, "dma_submit_error %d\n", cookie);
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000503 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530504 }
505
506 dma_async_issue_pending(chan);
507
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400508 time_left =
Vipin Kumar928aa2a2012-10-09 16:14:48 +0530509 wait_for_completion_timeout(&host->dma_access_complete,
Vipin Kumar4774fb02012-03-14 11:47:18 +0530510 msecs_to_jiffies(3000));
Nicholas Mc Guire818a45b2015-03-13 07:54:46 -0400511 if (time_left == 0) {
Vinod Koulb177ea32014-10-11 21:10:32 +0530512 dmaengine_terminate_all(chan);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530513 dev_err(host->dev, "wait_for_completion_timeout\n");
Nicholas Mc Guire0bda3e12015-03-13 07:54:45 -0400514 ret = -ETIMEDOUT;
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000515 goto unmap_dma;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530516 }
517
Bartlomiej Zolnierkiewiczd1806a52012-11-05 10:00:14 +0000518 ret = 0;
519
520unmap_dma:
521 dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
522
523 return ret;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530524}
525
Linus Walleij6c009ab2010-09-13 00:35:22 +0200526/*
Vipin Kumar604e7542012-03-14 11:47:17 +0530527 * fsmc_write_buf - write buffer to chip
528 * @mtd: MTD device structure
529 * @buf: data buffer
530 * @len: number of bytes to write
531 */
532static void fsmc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
533{
534 int i;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100535 struct nand_chip *chip = mtd_to_nand(mtd);
Vipin Kumar604e7542012-03-14 11:47:17 +0530536
537 if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
538 IS_ALIGNED(len, sizeof(uint32_t))) {
539 uint32_t *p = (uint32_t *)buf;
540 len = len >> 2;
541 for (i = 0; i < len; i++)
Vipin Kumara4742d52012-10-09 16:14:50 +0530542 writel_relaxed(p[i], chip->IO_ADDR_W);
Vipin Kumar604e7542012-03-14 11:47:17 +0530543 } else {
544 for (i = 0; i < len; i++)
Vipin Kumara4742d52012-10-09 16:14:50 +0530545 writeb_relaxed(buf[i], chip->IO_ADDR_W);
Vipin Kumar604e7542012-03-14 11:47:17 +0530546 }
547}
548
549/*
550 * fsmc_read_buf - read chip data into buffer
551 * @mtd: MTD device structure
552 * @buf: buffer to store date
553 * @len: number of bytes to read
554 */
555static void fsmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
556{
557 int i;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100558 struct nand_chip *chip = mtd_to_nand(mtd);
Vipin Kumar604e7542012-03-14 11:47:17 +0530559
560 if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
561 IS_ALIGNED(len, sizeof(uint32_t))) {
562 uint32_t *p = (uint32_t *)buf;
563 len = len >> 2;
564 for (i = 0; i < len; i++)
Vipin Kumara4742d52012-10-09 16:14:50 +0530565 p[i] = readl_relaxed(chip->IO_ADDR_R);
Vipin Kumar604e7542012-03-14 11:47:17 +0530566 } else {
567 for (i = 0; i < len; i++)
Vipin Kumara4742d52012-10-09 16:14:50 +0530568 buf[i] = readb_relaxed(chip->IO_ADDR_R);
Vipin Kumar604e7542012-03-14 11:47:17 +0530569 }
570}
571
572/*
Vipin Kumar4774fb02012-03-14 11:47:18 +0530573 * fsmc_read_buf_dma - read chip data into buffer
574 * @mtd: MTD device structure
575 * @buf: buffer to store date
576 * @len: number of bytes to read
577 */
578static void fsmc_read_buf_dma(struct mtd_info *mtd, uint8_t *buf, int len)
579{
Boris BREZILLON277af422015-12-10 08:59:46 +0100580 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530581
Vipin Kumar4774fb02012-03-14 11:47:18 +0530582 dma_xfer(host, buf, len, DMA_FROM_DEVICE);
583}
584
585/*
586 * fsmc_write_buf_dma - write buffer to chip
587 * @mtd: MTD device structure
588 * @buf: data buffer
589 * @len: number of bytes to write
590 */
591static void fsmc_write_buf_dma(struct mtd_info *mtd, const uint8_t *buf,
592 int len)
593{
Boris BREZILLON277af422015-12-10 08:59:46 +0100594 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530595
Vipin Kumar4774fb02012-03-14 11:47:18 +0530596 dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
597}
598
599/*
Linus Walleij6c009ab2010-09-13 00:35:22 +0200600 * fsmc_read_page_hwecc
601 * @mtd: mtd info structure
602 * @chip: nand chip info structure
603 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -0700604 * @oob_required: caller expects OOB data read to chip->oob_poi
Linus Walleij6c009ab2010-09-13 00:35:22 +0200605 * @page: page number to read
606 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300607 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
Linus Walleij6c009ab2010-09-13 00:35:22 +0200608 * performed in a strict sequence as follows:
609 * data(512 byte) -> ecc(13 byte)
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300610 * After this read, fsmc hardware generates and reports error data bits(up to a
Linus Walleij6c009ab2010-09-13 00:35:22 +0200611 * max of 8 bits)
612 */
613static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700614 uint8_t *buf, int oob_required, int page)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200615{
Linus Walleij6c009ab2010-09-13 00:35:22 +0200616 int i, j, s, stat, eccsize = chip->ecc.size;
617 int eccbytes = chip->ecc.bytes;
618 int eccsteps = chip->ecc.steps;
619 uint8_t *p = buf;
620 uint8_t *ecc_calc = chip->buffers->ecccalc;
621 uint8_t *ecc_code = chip->buffers->ecccode;
622 int off, len, group = 0;
623 /*
624 * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
625 * end up reading 14 bytes (7 words) from oob. The local array is
626 * to maintain word alignment
627 */
628 uint16_t ecc_oob[7];
629 uint8_t *oob = (uint8_t *)&ecc_oob[0];
Mike Dunn3f91e942012-04-25 12:06:09 -0700630 unsigned int max_bitflips = 0;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200631
632 for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200633 chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
634 chip->ecc.hwctl(mtd, NAND_ECC_READ);
635 chip->read_buf(mtd, p, eccsize);
636
637 for (j = 0; j < eccbytes;) {
Boris Brezillon04a123a2016-02-09 15:01:21 +0100638 struct mtd_oob_region oobregion;
639 int ret;
640
641 ret = mtd_ooblayout_ecc(mtd, group++, &oobregion);
642 if (ret)
643 return ret;
644
645 off = oobregion.offset;
646 len = oobregion.length;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200647
648 /*
Vipin Kumar4cbe1bf02012-03-14 11:47:09 +0530649 * length is intentionally kept a higher multiple of 2
650 * to read at least 13 bytes even in case of 16 bit NAND
651 * devices
652 */
Vipin Kumaraea686b2012-03-14 11:47:10 +0530653 if (chip->options & NAND_BUSWIDTH_16)
654 len = roundup(len, 2);
655
Linus Walleij6c009ab2010-09-13 00:35:22 +0200656 chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
657 chip->read_buf(mtd, oob + j, len);
658 j += len;
659 }
660
Vipin Kumar519300c2012-03-07 17:00:49 +0530661 memcpy(&ecc_code[i], oob, chip->ecc.bytes);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200662 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
663
664 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -0700665 if (stat < 0) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200666 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -0700667 } else {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200668 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -0700669 max_bitflips = max_t(unsigned int, max_bitflips, stat);
670 }
Linus Walleij6c009ab2010-09-13 00:35:22 +0200671 }
672
Mike Dunn3f91e942012-04-25 12:06:09 -0700673 return max_bitflips;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200674}
675
676/*
Armando Visconti753e0132012-03-07 17:00:54 +0530677 * fsmc_bch8_correct_data
Linus Walleij6c009ab2010-09-13 00:35:22 +0200678 * @mtd: mtd info structure
679 * @dat: buffer of read data
680 * @read_ecc: ecc read from device spare area
681 * @calc_ecc: ecc calculated from read data
682 *
683 * calc_ecc is a 104 bit information containing maximum of 8 error
684 * offset informations of 13 bits each in 512 bytes of read data.
685 */
Armando Visconti753e0132012-03-07 17:00:54 +0530686static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
Linus Walleij6c009ab2010-09-13 00:35:22 +0200687 uint8_t *read_ecc, uint8_t *calc_ecc)
688{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100689 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLON277af422015-12-10 08:59:46 +0100690 struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530691 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200692 unsigned int bank = host->bank;
Armando Viscontia612c2a2012-03-07 17:00:53 +0530693 uint32_t err_idx[8];
Linus Walleij6c009ab2010-09-13 00:35:22 +0200694 uint32_t num_err, i;
Armando Visconti753e0132012-03-07 17:00:54 +0530695 uint32_t ecc1, ecc2, ecc3, ecc4;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200696
Vipin Kumara4742d52012-10-09 16:14:50 +0530697 num_err = (readl_relaxed(FSMC_NAND_REG(regs, bank, STS)) >> 10) & 0xF;
Vipin Kumar519300c2012-03-07 17:00:49 +0530698
699 /* no bit flipping */
700 if (likely(num_err == 0))
701 return 0;
702
703 /* too many errors */
704 if (unlikely(num_err > 8)) {
705 /*
706 * This is a temporary erase check. A newly erased page read
707 * would result in an ecc error because the oob data is also
708 * erased to FF and the calculated ecc for an FF data is not
709 * FF..FF.
710 * This is a workaround to skip performing correction in case
711 * data is FF..FF
712 *
713 * Logic:
714 * For every page, each bit written as 0 is counted until these
715 * number of bits are greater than 8 (the maximum correction
716 * capability of FSMC for each 512 + 13 bytes)
717 */
718
719 int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
720 int bits_data = count_written_bits(dat, chip->ecc.size, 8);
721
722 if ((bits_ecc + bits_data) <= 8) {
723 if (bits_data)
724 memset(dat, 0xff, chip->ecc.size);
725 return bits_data;
726 }
727
728 return -EBADMSG;
729 }
730
Linus Walleij6c009ab2010-09-13 00:35:22 +0200731 /*
732 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
733 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
734 *
735 * calc_ecc is a 104 bit information containing maximum of 8 error
736 * offset informations of 13 bits each. calc_ecc is copied into a
737 * uint64_t array and error offset indexes are populated in err_idx
738 * array
739 */
Vipin Kumara4742d52012-10-09 16:14:50 +0530740 ecc1 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
741 ecc2 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC2));
742 ecc3 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC3));
743 ecc4 = readl_relaxed(FSMC_NAND_REG(regs, bank, STS));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200744
Armando Visconti753e0132012-03-07 17:00:54 +0530745 err_idx[0] = (ecc1 >> 0) & 0x1FFF;
746 err_idx[1] = (ecc1 >> 13) & 0x1FFF;
747 err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
748 err_idx[3] = (ecc2 >> 7) & 0x1FFF;
749 err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
750 err_idx[5] = (ecc3 >> 1) & 0x1FFF;
751 err_idx[6] = (ecc3 >> 14) & 0x1FFF;
752 err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200753
754 i = 0;
755 while (num_err--) {
756 change_bit(0, (unsigned long *)&err_idx[i]);
757 change_bit(1, (unsigned long *)&err_idx[i]);
758
Vipin Kumarb533f8d2012-03-14 11:47:11 +0530759 if (err_idx[i] < chip->ecc.size * 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200760 change_bit(err_idx[i], (unsigned long *)dat);
761 i++;
762 }
763 }
764 return i;
765}
766
Vipin Kumar4774fb02012-03-14 11:47:18 +0530767static bool filter(struct dma_chan *chan, void *slave)
768{
769 chan->private = slave;
770 return true;
771}
772
Bill Pemberton06f25512012-11-19 13:23:07 -0500773static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100774 struct fsmc_nand_data *host,
775 struct nand_chip *nand)
Stefan Roeseeea62812012-03-16 10:19:31 +0100776{
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100777 struct device_node *np = pdev->dev.of_node;
Stefan Roeseeea62812012-03-16 10:19:31 +0100778 u32 val;
Stefan Roese62b57f42015-03-19 14:34:29 +0100779 int ret;
Stefan Roeseeea62812012-03-16 10:19:31 +0100780
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100781 nand->options = 0;
Thomas Petazzoniee568742017-03-21 11:03:53 +0100782
Stefan Roeseeea62812012-03-16 10:19:31 +0100783 if (!of_property_read_u32(np, "bank-width", &val)) {
784 if (val == 2) {
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100785 nand->options |= NAND_BUSWIDTH_16;
Stefan Roeseeea62812012-03-16 10:19:31 +0100786 } else if (val != 1) {
787 dev_err(&pdev->dev, "invalid bank-width %u\n", val);
788 return -EINVAL;
789 }
790 }
Thomas Petazzoniee568742017-03-21 11:03:53 +0100791
Stefan Roeseeea62812012-03-16 10:19:31 +0100792 if (of_get_property(np, "nand-skip-bbtscan", NULL))
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100793 nand->options |= NAND_SKIP_BBTSCAN;
Stefan Roeseeea62812012-03-16 10:19:31 +0100794
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100795 host->dev_timings = devm_kzalloc(&pdev->dev,
796 sizeof(*host->dev_timings), GFP_KERNEL);
797 if (!host->dev_timings)
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200798 return -ENOMEM;
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100799 ret = of_property_read_u8_array(np, "timings", (u8 *)host->dev_timings,
800 sizeof(*host->dev_timings));
Stefan Roese62b57f42015-03-19 14:34:29 +0100801 if (ret) {
802 dev_info(&pdev->dev, "No timings in dts specified, using default timings!\n");
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100803 host->dev_timings = NULL;
Stefan Roese62b57f42015-03-19 14:34:29 +0100804 }
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200805
806 /* Set default NAND bank to 0 */
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100807 host->bank = 0;
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200808 if (!of_property_read_u32(np, "bank", &val)) {
809 if (val > 3) {
810 dev_err(&pdev->dev, "invalid bank %u\n", val);
811 return -EINVAL;
812 }
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100813 host->bank = val;
Mian Yousaf Kaukab64ddba42013-04-29 14:07:48 +0200814 }
Stefan Roeseeea62812012-03-16 10:19:31 +0100815 return 0;
816}
Stefan Roeseeea62812012-03-16 10:19:31 +0100817
Linus Walleij6c009ab2010-09-13 00:35:22 +0200818/*
819 * fsmc_nand_probe - Probe function
820 * @pdev: platform device structure
821 */
822static int __init fsmc_nand_probe(struct platform_device *pdev)
823{
Linus Walleij6c009ab2010-09-13 00:35:22 +0200824 struct fsmc_nand_data *host;
825 struct mtd_info *mtd;
826 struct nand_chip *nand;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200827 struct resource *res;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530828 dma_cap_mask_t mask;
Linus Walleij4ad916b2010-11-29 13:52:06 +0100829 int ret = 0;
Linus Walleij593cd872010-11-29 13:52:19 +0100830 u32 pid;
831 int i;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200832
Linus Walleij6c009ab2010-09-13 00:35:22 +0200833 /* Allocate memory for the device structure (and zero it) */
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530834 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Jingoo Hand9a21ae2013-12-26 12:16:38 +0900835 if (!host)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200836 return -ENOMEM;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200837
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100838 nand = &host->nand;
839
840 ret = fsmc_nand_probe_config_dt(pdev, host, nand);
841 if (ret)
842 return ret;
843
Linus Walleij6c009ab2010-09-13 00:35:22 +0200844 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
Thierry Redingb0de7742013-01-21 11:09:12 +0100845 host->data_va = devm_ioremap_resource(&pdev->dev, res);
846 if (IS_ERR(host->data_va))
847 return PTR_ERR(host->data_va);
Stefan Roesecbf29b82015-10-02 12:40:20 +0200848
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +0200849 host->data_pa = (dma_addr_t)res->start;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200850
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +0200851 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
Thierry Redingb0de7742013-01-21 11:09:12 +0100852 host->addr_va = devm_ioremap_resource(&pdev->dev, res);
853 if (IS_ERR(host->addr_va))
854 return PTR_ERR(host->addr_va);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200855
Jean-Christophe PLAGNIOL-VILLARD6d7b42a2012-10-04 15:14:16 +0200856 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
Thierry Redingb0de7742013-01-21 11:09:12 +0100857 host->cmd_va = devm_ioremap_resource(&pdev->dev, res);
858 if (IS_ERR(host->cmd_va))
859 return PTR_ERR(host->cmd_va);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200860
861 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
Thierry Redingb0de7742013-01-21 11:09:12 +0100862 host->regs_va = devm_ioremap_resource(&pdev->dev, res);
863 if (IS_ERR(host->regs_va))
864 return PTR_ERR(host->regs_va);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200865
Thomas Petazzonifb8ed2c2017-03-21 11:04:03 +0100866 host->clk = devm_clk_get(&pdev->dev, NULL);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200867 if (IS_ERR(host->clk)) {
868 dev_err(&pdev->dev, "failed to fetch block clock\n");
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530869 return PTR_ERR(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200870 }
871
Viresh Kumare25da1c2012-04-17 17:07:57 +0530872 ret = clk_prepare_enable(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200873 if (ret)
Thomas Petazzonifb8ed2c2017-03-21 11:04:03 +0100874 return ret;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200875
Linus Walleij593cd872010-11-29 13:52:19 +0100876 /*
877 * This device ID is actually a common AMBA ID as used on the
878 * AMBA PrimeCell bus. However it is not a PrimeCell.
879 */
880 for (pid = 0, i = 0; i < 4; i++)
881 pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
882 host->pid = pid;
883 dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
884 "revision %02x, config %02x\n",
885 AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
886 AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
887
Vipin Kumar712c4ad2012-03-14 11:47:16 +0530888 host->dev = &pdev->dev;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530889
890 if (host->mode == USE_DMA_ACCESS)
891 init_completion(&host->dma_access_complete);
892
Linus Walleij6c009ab2010-09-13 00:35:22 +0200893 /* Link all private pointers */
Boris BREZILLONbdf3a552015-12-10 09:00:05 +0100894 mtd = nand_to_mtd(&host->nand);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100895 nand_set_controller_data(nand, host);
Thomas Petazzonia1b1e1d2017-03-21 11:04:02 +0100896 nand_set_flash_node(nand, pdev->dev.of_node);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200897
Boris BREZILLONbdf3a552015-12-10 09:00:05 +0100898 mtd->dev.parent = &pdev->dev;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200899 nand->IO_ADDR_R = host->data_va;
900 nand->IO_ADDR_W = host->data_va;
901 nand->cmd_ctrl = fsmc_cmd_ctrl;
902 nand->chip_delay = 30;
903
Stefan Roesee278fc72015-10-19 08:40:13 +0200904 /*
905 * Setup default ECC mode. nand_dt_init() called from nand_scan_ident()
906 * can overwrite this value if the DT provides a different value.
907 */
Linus Walleij6c009ab2010-09-13 00:35:22 +0200908 nand->ecc.mode = NAND_ECC_HW;
909 nand->ecc.hwctl = fsmc_enable_hwecc;
910 nand->ecc.size = 512;
Vipin Kumar467e6e72012-03-14 11:47:12 +0530911 nand->badblockbits = 7;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200912
Vipin Kumar4774fb02012-03-14 11:47:18 +0530913 switch (host->mode) {
914 case USE_DMA_ACCESS:
915 dma_cap_zero(mask);
916 dma_cap_set(DMA_MEMCPY, mask);
Thomas Petazzonifeb1e572017-03-21 11:03:59 +0100917 host->read_dma_chan = dma_request_channel(mask, filter, NULL);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530918 if (!host->read_dma_chan) {
919 dev_err(&pdev->dev, "Unable to get read dma channel\n");
920 goto err_req_read_chnl;
921 }
Thomas Petazzonifeb1e572017-03-21 11:03:59 +0100922 host->write_dma_chan = dma_request_channel(mask, filter, NULL);
Vipin Kumar4774fb02012-03-14 11:47:18 +0530923 if (!host->write_dma_chan) {
924 dev_err(&pdev->dev, "Unable to get write dma channel\n");
925 goto err_req_write_chnl;
926 }
927 nand->read_buf = fsmc_read_buf_dma;
928 nand->write_buf = fsmc_write_buf_dma;
929 break;
930
931 default:
932 case USE_WORD_ACCESS:
Vipin Kumar604e7542012-03-14 11:47:17 +0530933 nand->read_buf = fsmc_read_buf;
934 nand->write_buf = fsmc_write_buf;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530935 break;
Vipin Kumar604e7542012-03-14 11:47:17 +0530936 }
937
Thomas Petazzoni6335b502017-04-29 10:52:34 +0200938 fsmc_nand_setup(host, host->dev_timings);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200939
Linus Walleij593cd872010-11-29 13:52:19 +0100940 if (AMBA_REV_BITS(host->pid) >= 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200941 nand->ecc.read_page = fsmc_read_page_hwecc;
942 nand->ecc.calculate = fsmc_read_hwecc_ecc4;
Armando Visconti753e0132012-03-07 17:00:54 +0530943 nand->ecc.correct = fsmc_bch8_correct_data;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200944 nand->ecc.bytes = 13;
Mike Dunn6a918ba2012-03-11 14:21:11 -0700945 nand->ecc.strength = 8;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200946 }
947
948 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300949 * Scan to find existence of the device
Linus Walleij6c009ab2010-09-13 00:35:22 +0200950 */
Masahiro Yamadaad5678e2016-11-04 19:43:00 +0900951 ret = nand_scan_ident(mtd, 1, NULL);
952 if (ret) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200953 dev_err(&pdev->dev, "No NAND Device found!\n");
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530954 goto err_scan_ident;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200955 }
956
Linus Walleij593cd872010-11-29 13:52:19 +0100957 if (AMBA_REV_BITS(host->pid) >= 8) {
Boris BREZILLONbdf3a552015-12-10 09:00:05 +0100958 switch (mtd->oobsize) {
Bhavna Yadave29ee572012-03-07 17:00:50 +0530959 case 16:
Bhavna Yadave29ee572012-03-07 17:00:50 +0530960 case 64:
Bhavna Yadave29ee572012-03-07 17:00:50 +0530961 case 128:
Armando Visconti0c78e932012-03-07 17:00:55 +0530962 case 224:
Bhavna Yadave29ee572012-03-07 17:00:50 +0530963 case 256:
Bhavna Yadave29ee572012-03-07 17:00:50 +0530964 break;
965 default:
Jingoo Han67b19a62013-12-26 12:31:25 +0900966 dev_warn(&pdev->dev, "No oob scheme defined for oobsize %d\n",
967 mtd->oobsize);
Stefan Roese6efadcf2015-10-02 12:40:21 +0200968 ret = -EINVAL;
969 goto err_probe;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200970 }
Boris Brezillon22b46952016-02-03 20:01:42 +0100971
972 mtd_set_ooblayout(mtd, &fsmc_ecc4_ooblayout_ops);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200973 } else {
Stefan Roesee278fc72015-10-19 08:40:13 +0200974 switch (nand->ecc.mode) {
975 case NAND_ECC_HW:
976 dev_info(&pdev->dev, "Using 1-bit HW ECC scheme\n");
977 nand->ecc.calculate = fsmc_read_hwecc_ecc1;
978 nand->ecc.correct = nand_correct_data;
979 nand->ecc.bytes = 3;
980 nand->ecc.strength = 1;
Bhavna Yadave29ee572012-03-07 17:00:50 +0530981 break;
Stefan Roesee278fc72015-10-19 08:40:13 +0200982
Rafał Miłeckief296dc2016-04-17 22:53:04 +0200983 case NAND_ECC_SOFT:
Rafał Miłeckief296dc2016-04-17 22:53:04 +0200984 if (nand->ecc.algo == NAND_ECC_BCH) {
985 dev_info(&pdev->dev, "Using 4-bit SW BCH ECC scheme\n");
986 break;
987 }
Stefan Roesee278fc72015-10-19 08:40:13 +0200988
Bhavna Yadave29ee572012-03-07 17:00:50 +0530989 default:
Stefan Roesee278fc72015-10-19 08:40:13 +0200990 dev_err(&pdev->dev, "Unsupported ECC mode!\n");
Stefan Roese6efadcf2015-10-02 12:40:21 +0200991 goto err_probe;
Bhavna Yadave29ee572012-03-07 17:00:50 +0530992 }
Stefan Roesee278fc72015-10-19 08:40:13 +0200993
994 /*
995 * Don't set layout for BCH4 SW ECC. This will be
996 * generated later in nand_bch_init() later.
997 */
Rafał Miłeckie4225ae2016-04-17 22:53:07 +0200998 if (nand->ecc.mode == NAND_ECC_HW) {
Boris BREZILLONbdf3a552015-12-10 09:00:05 +0100999 switch (mtd->oobsize) {
Stefan Roesee278fc72015-10-19 08:40:13 +02001000 case 16:
Stefan Roesee278fc72015-10-19 08:40:13 +02001001 case 64:
Stefan Roesee278fc72015-10-19 08:40:13 +02001002 case 128:
Boris Brezillon22b46952016-02-03 20:01:42 +01001003 mtd_set_ooblayout(mtd,
1004 &fsmc_ecc1_ooblayout_ops);
Stefan Roesee278fc72015-10-19 08:40:13 +02001005 break;
1006 default:
1007 dev_warn(&pdev->dev,
1008 "No oob scheme defined for oobsize %d\n",
1009 mtd->oobsize);
1010 ret = -EINVAL;
1011 goto err_probe;
1012 }
1013 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001014 }
1015
1016 /* Second stage of scan to fill MTD data-structures */
Masahiro Yamadaad5678e2016-11-04 19:43:00 +09001017 ret = nand_scan_tail(mtd);
1018 if (ret)
Linus Walleij6c009ab2010-09-13 00:35:22 +02001019 goto err_probe;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001020
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001021 mtd->name = "nand";
Thomas Petazzoniede29a02017-03-21 11:04:00 +01001022 ret = mtd_device_register(mtd, NULL, 0);
Jamie Iles99335d02011-05-23 10:23:23 +01001023 if (ret)
Linus Walleij6c009ab2010-09-13 00:35:22 +02001024 goto err_probe;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001025
1026 platform_set_drvdata(pdev, host);
1027 dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
1028 return 0;
1029
1030err_probe:
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301031err_scan_ident:
Vipin Kumar4774fb02012-03-14 11:47:18 +05301032 if (host->mode == USE_DMA_ACCESS)
1033 dma_release_channel(host->write_dma_chan);
1034err_req_write_chnl:
1035 if (host->mode == USE_DMA_ACCESS)
1036 dma_release_channel(host->read_dma_chan);
1037err_req_read_chnl:
Viresh Kumare25da1c2012-04-17 17:07:57 +05301038 clk_disable_unprepare(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001039 return ret;
1040}
1041
1042/*
1043 * Clean up routine
1044 */
1045static int fsmc_nand_remove(struct platform_device *pdev)
1046{
1047 struct fsmc_nand_data *host = platform_get_drvdata(pdev);
1048
Linus Walleij6c009ab2010-09-13 00:35:22 +02001049 if (host) {
Boris BREZILLONbdf3a552015-12-10 09:00:05 +01001050 nand_release(nand_to_mtd(&host->nand));
Vipin Kumar4774fb02012-03-14 11:47:18 +05301051
1052 if (host->mode == USE_DMA_ACCESS) {
1053 dma_release_channel(host->write_dma_chan);
1054 dma_release_channel(host->read_dma_chan);
1055 }
Viresh Kumare25da1c2012-04-17 17:07:57 +05301056 clk_disable_unprepare(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001057 }
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301058
Linus Walleij6c009ab2010-09-13 00:35:22 +02001059 return 0;
1060}
1061
Jingoo Han80ce4dd2013-03-26 15:53:48 +09001062#ifdef CONFIG_PM_SLEEP
Linus Walleij6c009ab2010-09-13 00:35:22 +02001063static int fsmc_nand_suspend(struct device *dev)
1064{
1065 struct fsmc_nand_data *host = dev_get_drvdata(dev);
1066 if (host)
Viresh Kumare25da1c2012-04-17 17:07:57 +05301067 clk_disable_unprepare(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001068 return 0;
1069}
1070
1071static int fsmc_nand_resume(struct device *dev)
1072{
1073 struct fsmc_nand_data *host = dev_get_drvdata(dev);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301074 if (host) {
Viresh Kumare25da1c2012-04-17 17:07:57 +05301075 clk_prepare_enable(host->clk);
Thomas Petazzoni6335b502017-04-29 10:52:34 +02001076 fsmc_nand_setup(host, host->dev_timings);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301077 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001078 return 0;
1079}
Jingoo Han80ce4dd2013-03-26 15:53:48 +09001080#endif
Linus Walleij6c009ab2010-09-13 00:35:22 +02001081
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301082static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001083
Stefan Roeseeea62812012-03-16 10:19:31 +01001084static const struct of_device_id fsmc_nand_id_table[] = {
1085 { .compatible = "st,spear600-fsmc-nand" },
Linus Walleijba785202013-01-05 22:28:32 +01001086 { .compatible = "stericsson,fsmc-nand" },
Stefan Roeseeea62812012-03-16 10:19:31 +01001087 {}
1088};
1089MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);
Stefan Roeseeea62812012-03-16 10:19:31 +01001090
Linus Walleij6c009ab2010-09-13 00:35:22 +02001091static struct platform_driver fsmc_nand_driver = {
1092 .remove = fsmc_nand_remove,
1093 .driver = {
Linus Walleij6c009ab2010-09-13 00:35:22 +02001094 .name = "fsmc-nand",
Thomas Petazzoni33575b22017-03-21 11:04:05 +01001095 .of_match_table = fsmc_nand_id_table,
Linus Walleij6c009ab2010-09-13 00:35:22 +02001096 .pm = &fsmc_nand_pm_ops,
Linus Walleij6c009ab2010-09-13 00:35:22 +02001097 },
1098};
1099
Jingoo Han307d2a512013-03-05 13:30:36 +09001100module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001101
1102MODULE_LICENSE("GPL");
1103MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
1104MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");