]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/mtd/nand/fsmc_nand.c
Drivers: mtd: remove __dev* attributes.
[mirror_ubuntu-bionic-kernel.git] / drivers / mtd / nand / fsmc_nand.c
CommitLineData
6c009ab8
LW
1/*
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>
4774fb0a
VK
20#include <linux/completion.h>
21#include <linux/dmaengine.h>
22#include <linux/dma-direction.h>
23#include <linux/dma-mapping.h>
6c009ab8
LW
24#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>
eea62819 34#include <linux/of.h>
6c009ab8
LW
35#include <linux/mtd/partitions.h>
36#include <linux/io.h>
37#include <linux/slab.h>
38#include <linux/mtd/fsmc.h>
593cd871 39#include <linux/amba/bus.h>
6c009ab8
LW
40#include <mtd/mtd-abi.h>
41
e29ee57b 42static struct nand_ecclayout fsmc_ecc1_128_layout = {
6c009ab8
LW
43 .eccbytes = 24,
44 .eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52,
45 66, 67, 68, 82, 83, 84, 98, 99, 100, 114, 115, 116},
46 .oobfree = {
47 {.offset = 8, .length = 8},
48 {.offset = 24, .length = 8},
49 {.offset = 40, .length = 8},
50 {.offset = 56, .length = 8},
51 {.offset = 72, .length = 8},
52 {.offset = 88, .length = 8},
53 {.offset = 104, .length = 8},
54 {.offset = 120, .length = 8}
55 }
56};
57
e29ee57b
BY
58static struct nand_ecclayout fsmc_ecc1_64_layout = {
59 .eccbytes = 12,
60 .eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52},
61 .oobfree = {
62 {.offset = 8, .length = 8},
63 {.offset = 24, .length = 8},
64 {.offset = 40, .length = 8},
65 {.offset = 56, .length = 8},
66 }
67};
68
69static struct nand_ecclayout fsmc_ecc1_16_layout = {
70 .eccbytes = 3,
71 .eccpos = {2, 3, 4},
72 .oobfree = {
73 {.offset = 8, .length = 8},
74 }
75};
76
77/*
78 * ECC4 layout for NAND of pagesize 8192 bytes & OOBsize 256 bytes. 13*16 bytes
79 * of OB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block and 46
80 * bytes are free for use.
81 */
82static struct nand_ecclayout fsmc_ecc4_256_layout = {
83 .eccbytes = 208,
84 .eccpos = { 2, 3, 4, 5, 6, 7, 8,
85 9, 10, 11, 12, 13, 14,
86 18, 19, 20, 21, 22, 23, 24,
87 25, 26, 27, 28, 29, 30,
88 34, 35, 36, 37, 38, 39, 40,
89 41, 42, 43, 44, 45, 46,
90 50, 51, 52, 53, 54, 55, 56,
91 57, 58, 59, 60, 61, 62,
92 66, 67, 68, 69, 70, 71, 72,
93 73, 74, 75, 76, 77, 78,
94 82, 83, 84, 85, 86, 87, 88,
95 89, 90, 91, 92, 93, 94,
96 98, 99, 100, 101, 102, 103, 104,
97 105, 106, 107, 108, 109, 110,
98 114, 115, 116, 117, 118, 119, 120,
99 121, 122, 123, 124, 125, 126,
100 130, 131, 132, 133, 134, 135, 136,
101 137, 138, 139, 140, 141, 142,
102 146, 147, 148, 149, 150, 151, 152,
103 153, 154, 155, 156, 157, 158,
104 162, 163, 164, 165, 166, 167, 168,
105 169, 170, 171, 172, 173, 174,
106 178, 179, 180, 181, 182, 183, 184,
107 185, 186, 187, 188, 189, 190,
108 194, 195, 196, 197, 198, 199, 200,
109 201, 202, 203, 204, 205, 206,
110 210, 211, 212, 213, 214, 215, 216,
111 217, 218, 219, 220, 221, 222,
112 226, 227, 228, 229, 230, 231, 232,
113 233, 234, 235, 236, 237, 238,
114 242, 243, 244, 245, 246, 247, 248,
115 249, 250, 251, 252, 253, 254
116 },
117 .oobfree = {
118 {.offset = 15, .length = 3},
119 {.offset = 31, .length = 3},
120 {.offset = 47, .length = 3},
121 {.offset = 63, .length = 3},
122 {.offset = 79, .length = 3},
123 {.offset = 95, .length = 3},
124 {.offset = 111, .length = 3},
125 {.offset = 127, .length = 3},
126 {.offset = 143, .length = 3},
127 {.offset = 159, .length = 3},
128 {.offset = 175, .length = 3},
129 {.offset = 191, .length = 3},
130 {.offset = 207, .length = 3},
131 {.offset = 223, .length = 3},
132 {.offset = 239, .length = 3},
133 {.offset = 255, .length = 1}
134 }
135};
136
0c78e93b
AV
137/*
138 * ECC4 layout for NAND of pagesize 4096 bytes & OOBsize 224 bytes. 13*8 bytes
139 * of OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block & 118
140 * bytes are free for use.
141 */
142static struct nand_ecclayout fsmc_ecc4_224_layout = {
143 .eccbytes = 104,
144 .eccpos = { 2, 3, 4, 5, 6, 7, 8,
145 9, 10, 11, 12, 13, 14,
146 18, 19, 20, 21, 22, 23, 24,
147 25, 26, 27, 28, 29, 30,
148 34, 35, 36, 37, 38, 39, 40,
149 41, 42, 43, 44, 45, 46,
150 50, 51, 52, 53, 54, 55, 56,
151 57, 58, 59, 60, 61, 62,
152 66, 67, 68, 69, 70, 71, 72,
153 73, 74, 75, 76, 77, 78,
154 82, 83, 84, 85, 86, 87, 88,
155 89, 90, 91, 92, 93, 94,
156 98, 99, 100, 101, 102, 103, 104,
157 105, 106, 107, 108, 109, 110,
158 114, 115, 116, 117, 118, 119, 120,
159 121, 122, 123, 124, 125, 126
160 },
161 .oobfree = {
162 {.offset = 15, .length = 3},
163 {.offset = 31, .length = 3},
164 {.offset = 47, .length = 3},
165 {.offset = 63, .length = 3},
166 {.offset = 79, .length = 3},
167 {.offset = 95, .length = 3},
168 {.offset = 111, .length = 3},
169 {.offset = 127, .length = 97}
170 }
171};
172
e29ee57b
BY
173/*
174 * ECC4 layout for NAND of pagesize 4096 bytes & OOBsize 128 bytes. 13*8 bytes
175 * of OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block & 22
176 * bytes are free for use.
177 */
178static struct nand_ecclayout fsmc_ecc4_128_layout = {
6c009ab8
LW
179 .eccbytes = 104,
180 .eccpos = { 2, 3, 4, 5, 6, 7, 8,
181 9, 10, 11, 12, 13, 14,
182 18, 19, 20, 21, 22, 23, 24,
183 25, 26, 27, 28, 29, 30,
184 34, 35, 36, 37, 38, 39, 40,
185 41, 42, 43, 44, 45, 46,
186 50, 51, 52, 53, 54, 55, 56,
187 57, 58, 59, 60, 61, 62,
188 66, 67, 68, 69, 70, 71, 72,
189 73, 74, 75, 76, 77, 78,
190 82, 83, 84, 85, 86, 87, 88,
191 89, 90, 91, 92, 93, 94,
192 98, 99, 100, 101, 102, 103, 104,
193 105, 106, 107, 108, 109, 110,
194 114, 115, 116, 117, 118, 119, 120,
195 121, 122, 123, 124, 125, 126
196 },
197 .oobfree = {
198 {.offset = 15, .length = 3},
199 {.offset = 31, .length = 3},
200 {.offset = 47, .length = 3},
201 {.offset = 63, .length = 3},
202 {.offset = 79, .length = 3},
203 {.offset = 95, .length = 3},
204 {.offset = 111, .length = 3},
205 {.offset = 127, .length = 1}
206 }
207};
208
e29ee57b
BY
209/*
210 * ECC4 layout for NAND of pagesize 2048 bytes & OOBsize 64 bytes. 13*4 bytes of
211 * OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block and 10
212 * bytes are free for use.
213 */
214static struct nand_ecclayout fsmc_ecc4_64_layout = {
215 .eccbytes = 52,
216 .eccpos = { 2, 3, 4, 5, 6, 7, 8,
217 9, 10, 11, 12, 13, 14,
218 18, 19, 20, 21, 22, 23, 24,
219 25, 26, 27, 28, 29, 30,
220 34, 35, 36, 37, 38, 39, 40,
221 41, 42, 43, 44, 45, 46,
222 50, 51, 52, 53, 54, 55, 56,
223 57, 58, 59, 60, 61, 62,
224 },
225 .oobfree = {
226 {.offset = 15, .length = 3},
227 {.offset = 31, .length = 3},
228 {.offset = 47, .length = 3},
229 {.offset = 63, .length = 1},
230 }
231};
232
233/*
234 * ECC4 layout for NAND of pagesize 512 bytes & OOBsize 16 bytes. 13 bytes of
235 * OOB size is reserved for ECC, Byte no. 4 & 5 reserved for bad block and One
236 * byte is free for use.
237 */
238static struct nand_ecclayout fsmc_ecc4_16_layout = {
239 .eccbytes = 13,
240 .eccpos = { 0, 1, 2, 3, 6, 7, 8,
241 9, 10, 11, 12, 13, 14
242 },
243 .oobfree = {
244 {.offset = 15, .length = 1},
245 }
246};
247
6c009ab8
LW
248/*
249 * ECC placement definitions in oobfree type format.
250 * There are 13 bytes of ecc for every 512 byte block and it has to be read
251 * consecutively and immediately after the 512 byte data block for hardware to
252 * generate the error bit offsets in 512 byte data.
253 * Managing the ecc bytes in the following way makes it easier for software to
254 * read ecc bytes consecutive to data bytes. This way is similar to
255 * oobfree structure maintained already in generic nand driver
256 */
257static struct fsmc_eccplace fsmc_ecc4_lp_place = {
258 .eccplace = {
259 {.offset = 2, .length = 13},
260 {.offset = 18, .length = 13},
261 {.offset = 34, .length = 13},
262 {.offset = 50, .length = 13},
263 {.offset = 66, .length = 13},
264 {.offset = 82, .length = 13},
265 {.offset = 98, .length = 13},
266 {.offset = 114, .length = 13}
267 }
268};
269
6c009ab8
LW
270static struct fsmc_eccplace fsmc_ecc4_sp_place = {
271 .eccplace = {
272 {.offset = 0, .length = 4},
273 {.offset = 6, .length = 9}
274 }
275};
276
6c009ab8 277/**
593cd871 278 * struct fsmc_nand_data - structure for FSMC NAND device state
6c009ab8 279 *
593cd871 280 * @pid: Part ID on the AMBA PrimeCell format
6c009ab8
LW
281 * @mtd: MTD info for a NAND flash.
282 * @nand: Chip related info for a NAND flash.
71470324
VK
283 * @partitions: Partition info for a NAND Flash.
284 * @nr_partitions: Total number of partition of a NAND flash.
6c009ab8
LW
285 *
286 * @ecc_place: ECC placing locations in oobfree type format.
287 * @bank: Bank number for probed device.
288 * @clk: Clock structure for FSMC.
289 *
4774fb0a
VK
290 * @read_dma_chan: DMA channel for read access
291 * @write_dma_chan: DMA channel for write access to NAND
292 * @dma_access_complete: Completion structure
293 *
294 * @data_pa: NAND Physical port for Data.
6c009ab8
LW
295 * @data_va: NAND port for Data.
296 * @cmd_va: NAND port for Command.
297 * @addr_va: NAND port for Address.
298 * @regs_va: FSMC regs base address.
299 */
300struct fsmc_nand_data {
593cd871 301 u32 pid;
6c009ab8
LW
302 struct mtd_info mtd;
303 struct nand_chip nand;
71470324
VK
304 struct mtd_partition *partitions;
305 unsigned int nr_partitions;
6c009ab8
LW
306
307 struct fsmc_eccplace *ecc_place;
308 unsigned int bank;
712c4add 309 struct device *dev;
4774fb0a 310 enum access_mode mode;
6c009ab8
LW
311 struct clk *clk;
312
4774fb0a
VK
313 /* DMA related objects */
314 struct dma_chan *read_dma_chan;
315 struct dma_chan *write_dma_chan;
316 struct completion dma_access_complete;
317
e2f6bce8
VK
318 struct fsmc_nand_timings *dev_timings;
319
4774fb0a 320 dma_addr_t data_pa;
6c009ab8
LW
321 void __iomem *data_va;
322 void __iomem *cmd_va;
323 void __iomem *addr_va;
324 void __iomem *regs_va;
325
326 void (*select_chip)(uint32_t bank, uint32_t busw);
327};
328
329/* Assert CS signal based on chipnr */
330static void fsmc_select_chip(struct mtd_info *mtd, int chipnr)
331{
332 struct nand_chip *chip = mtd->priv;
333 struct fsmc_nand_data *host;
334
335 host = container_of(mtd, struct fsmc_nand_data, mtd);
336
337 switch (chipnr) {
338 case -1:
339 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
340 break;
341 case 0:
342 case 1:
343 case 2:
344 case 3:
345 if (host->select_chip)
346 host->select_chip(chipnr,
347 chip->options & NAND_BUSWIDTH_16);
348 break;
349
350 default:
351 BUG();
352 }
353}
354
355/*
356 * fsmc_cmd_ctrl - For facilitaing Hardware access
357 * This routine allows hardware specific access to control-lines(ALE,CLE)
358 */
359static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
360{
361 struct nand_chip *this = mtd->priv;
362 struct fsmc_nand_data *host = container_of(mtd,
363 struct fsmc_nand_data, mtd);
605add7d 364 void __iomem *regs = host->regs_va;
6c009ab8
LW
365 unsigned int bank = host->bank;
366
367 if (ctrl & NAND_CTRL_CHANGE) {
2a5dbead
VK
368 u32 pc;
369
6c009ab8 370 if (ctrl & NAND_CLE) {
2a5dbead
VK
371 this->IO_ADDR_R = host->cmd_va;
372 this->IO_ADDR_W = host->cmd_va;
6c009ab8 373 } else if (ctrl & NAND_ALE) {
2a5dbead
VK
374 this->IO_ADDR_R = host->addr_va;
375 this->IO_ADDR_W = host->addr_va;
6c009ab8 376 } else {
2a5dbead
VK
377 this->IO_ADDR_R = host->data_va;
378 this->IO_ADDR_W = host->data_va;
6c009ab8
LW
379 }
380
2a5dbead
VK
381 pc = readl(FSMC_NAND_REG(regs, bank, PC));
382 if (ctrl & NAND_NCE)
383 pc |= FSMC_ENABLE;
384 else
385 pc &= ~FSMC_ENABLE;
a4742d51 386 writel_relaxed(pc, FSMC_NAND_REG(regs, bank, PC));
6c009ab8
LW
387 }
388
389 mb();
390
391 if (cmd != NAND_CMD_NONE)
a4742d51 392 writeb_relaxed(cmd, this->IO_ADDR_W);
6c009ab8
LW
393}
394
395/*
396 * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
397 *
398 * This routine initializes timing parameters related to NAND memory access in
399 * FSMC registers
400 */
2a5dbead 401static void fsmc_nand_setup(void __iomem *regs, uint32_t bank,
e2f6bce8 402 uint32_t busw, struct fsmc_nand_timings *timings)
6c009ab8
LW
403{
404 uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
e2f6bce8
VK
405 uint32_t tclr, tar, thiz, thold, twait, tset;
406 struct fsmc_nand_timings *tims;
407 struct fsmc_nand_timings default_timings = {
408 .tclr = FSMC_TCLR_1,
409 .tar = FSMC_TAR_1,
410 .thiz = FSMC_THIZ_1,
411 .thold = FSMC_THOLD_4,
412 .twait = FSMC_TWAIT_6,
413 .tset = FSMC_TSET_0,
414 };
415
416 if (timings)
417 tims = timings;
418 else
419 tims = &default_timings;
420
421 tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT;
422 tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT;
423 thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT;
424 thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT;
425 twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT;
426 tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT;
6c009ab8
LW
427
428 if (busw)
a4742d51
VK
429 writel_relaxed(value | FSMC_DEVWID_16,
430 FSMC_NAND_REG(regs, bank, PC));
6c009ab8 431 else
a4742d51
VK
432 writel_relaxed(value | FSMC_DEVWID_8,
433 FSMC_NAND_REG(regs, bank, PC));
6c009ab8 434
a4742d51 435 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) | tclr | tar,
2a5dbead 436 FSMC_NAND_REG(regs, bank, PC));
a4742d51
VK
437 writel_relaxed(thiz | thold | twait | tset,
438 FSMC_NAND_REG(regs, bank, COMM));
439 writel_relaxed(thiz | thold | twait | tset,
440 FSMC_NAND_REG(regs, bank, ATTRIB));
6c009ab8
LW
441}
442
443/*
444 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
445 */
446static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
447{
448 struct fsmc_nand_data *host = container_of(mtd,
449 struct fsmc_nand_data, mtd);
2a5dbead 450 void __iomem *regs = host->regs_va;
6c009ab8
LW
451 uint32_t bank = host->bank;
452
a4742d51 453 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCPLEN_256,
2a5dbead 454 FSMC_NAND_REG(regs, bank, PC));
a4742d51 455 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCEN,
2a5dbead 456 FSMC_NAND_REG(regs, bank, PC));
a4742d51 457 writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) | FSMC_ECCEN,
2a5dbead 458 FSMC_NAND_REG(regs, bank, PC));
6c009ab8
LW
459}
460
461/*
462 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
25985edc 463 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
6c009ab8
LW
464 * max of 8-bits)
465 */
466static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
467 uint8_t *ecc)
468{
469 struct fsmc_nand_data *host = container_of(mtd,
470 struct fsmc_nand_data, mtd);
2a5dbead 471 void __iomem *regs = host->regs_va;
6c009ab8
LW
472 uint32_t bank = host->bank;
473 uint32_t ecc_tmp;
474 unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
475
476 do {
a4742d51 477 if (readl_relaxed(FSMC_NAND_REG(regs, bank, STS)) & FSMC_CODE_RDY)
6c009ab8
LW
478 break;
479 else
480 cond_resched();
481 } while (!time_after_eq(jiffies, deadline));
482
712c4add
VK
483 if (time_after_eq(jiffies, deadline)) {
484 dev_err(host->dev, "calculate ecc timed out\n");
485 return -ETIMEDOUT;
486 }
487
a4742d51 488 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
6c009ab8
LW
489 ecc[0] = (uint8_t) (ecc_tmp >> 0);
490 ecc[1] = (uint8_t) (ecc_tmp >> 8);
491 ecc[2] = (uint8_t) (ecc_tmp >> 16);
492 ecc[3] = (uint8_t) (ecc_tmp >> 24);
493
a4742d51 494 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC2));
6c009ab8
LW
495 ecc[4] = (uint8_t) (ecc_tmp >> 0);
496 ecc[5] = (uint8_t) (ecc_tmp >> 8);
497 ecc[6] = (uint8_t) (ecc_tmp >> 16);
498 ecc[7] = (uint8_t) (ecc_tmp >> 24);
499
a4742d51 500 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC3));
6c009ab8
LW
501 ecc[8] = (uint8_t) (ecc_tmp >> 0);
502 ecc[9] = (uint8_t) (ecc_tmp >> 8);
503 ecc[10] = (uint8_t) (ecc_tmp >> 16);
504 ecc[11] = (uint8_t) (ecc_tmp >> 24);
505
a4742d51 506 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, STS));
6c009ab8
LW
507 ecc[12] = (uint8_t) (ecc_tmp >> 16);
508
509 return 0;
510}
511
512/*
513 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
25985edc 514 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
6c009ab8
LW
515 * max of 1-bit)
516 */
517static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
518 uint8_t *ecc)
519{
520 struct fsmc_nand_data *host = container_of(mtd,
521 struct fsmc_nand_data, mtd);
2a5dbead 522 void __iomem *regs = host->regs_va;
6c009ab8
LW
523 uint32_t bank = host->bank;
524 uint32_t ecc_tmp;
525
a4742d51 526 ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
6c009ab8
LW
527 ecc[0] = (uint8_t) (ecc_tmp >> 0);
528 ecc[1] = (uint8_t) (ecc_tmp >> 8);
529 ecc[2] = (uint8_t) (ecc_tmp >> 16);
530
531 return 0;
532}
533
519300cf
VK
534/* Count the number of 0's in buff upto a max of max_bits */
535static int count_written_bits(uint8_t *buff, int size, int max_bits)
536{
537 int k, written_bits = 0;
538
539 for (k = 0; k < size; k++) {
540 written_bits += hweight8(~buff[k]);
541 if (written_bits > max_bits)
542 break;
543 }
544
545 return written_bits;
546}
547
4774fb0a
VK
548static void dma_complete(void *param)
549{
550 struct fsmc_nand_data *host = param;
551
552 complete(&host->dma_access_complete);
553}
554
555static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len,
556 enum dma_data_direction direction)
557{
558 struct dma_chan *chan;
559 struct dma_device *dma_dev;
560 struct dma_async_tx_descriptor *tx;
561 dma_addr_t dma_dst, dma_src, dma_addr;
562 dma_cookie_t cookie;
563 unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
564 int ret;
565
566 if (direction == DMA_TO_DEVICE)
567 chan = host->write_dma_chan;
568 else if (direction == DMA_FROM_DEVICE)
569 chan = host->read_dma_chan;
570 else
571 return -EINVAL;
572
573 dma_dev = chan->device;
574 dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction);
575
576 if (direction == DMA_TO_DEVICE) {
577 dma_src = dma_addr;
578 dma_dst = host->data_pa;
579 flags |= DMA_COMPL_SRC_UNMAP_SINGLE | DMA_COMPL_SKIP_DEST_UNMAP;
580 } else {
581 dma_src = host->data_pa;
582 dma_dst = dma_addr;
583 flags |= DMA_COMPL_DEST_UNMAP_SINGLE | DMA_COMPL_SKIP_SRC_UNMAP;
584 }
585
586 tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
587 len, flags);
588
589 if (!tx) {
590 dev_err(host->dev, "device_prep_dma_memcpy error\n");
591 dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
592 return -EIO;
593 }
594
595 tx->callback = dma_complete;
596 tx->callback_param = host;
597 cookie = tx->tx_submit(tx);
598
599 ret = dma_submit_error(cookie);
600 if (ret) {
601 dev_err(host->dev, "dma_submit_error %d\n", cookie);
602 return ret;
603 }
604
605 dma_async_issue_pending(chan);
606
607 ret =
928aa2ae 608 wait_for_completion_timeout(&host->dma_access_complete,
4774fb0a
VK
609 msecs_to_jiffies(3000));
610 if (ret <= 0) {
611 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
612 dev_err(host->dev, "wait_for_completion_timeout\n");
613 return ret ? ret : -ETIMEDOUT;
614 }
615
616 return 0;
617}
618
604e7544
VK
619/*
620 * fsmc_write_buf - write buffer to chip
621 * @mtd: MTD device structure
622 * @buf: data buffer
623 * @len: number of bytes to write
624 */
625static void fsmc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
626{
627 int i;
628 struct nand_chip *chip = mtd->priv;
629
630 if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
631 IS_ALIGNED(len, sizeof(uint32_t))) {
632 uint32_t *p = (uint32_t *)buf;
633 len = len >> 2;
634 for (i = 0; i < len; i++)
a4742d51 635 writel_relaxed(p[i], chip->IO_ADDR_W);
604e7544
VK
636 } else {
637 for (i = 0; i < len; i++)
a4742d51 638 writeb_relaxed(buf[i], chip->IO_ADDR_W);
604e7544
VK
639 }
640}
641
642/*
643 * fsmc_read_buf - read chip data into buffer
644 * @mtd: MTD device structure
645 * @buf: buffer to store date
646 * @len: number of bytes to read
647 */
648static void fsmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
649{
650 int i;
651 struct nand_chip *chip = mtd->priv;
652
653 if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
654 IS_ALIGNED(len, sizeof(uint32_t))) {
655 uint32_t *p = (uint32_t *)buf;
656 len = len >> 2;
657 for (i = 0; i < len; i++)
a4742d51 658 p[i] = readl_relaxed(chip->IO_ADDR_R);
604e7544
VK
659 } else {
660 for (i = 0; i < len; i++)
a4742d51 661 buf[i] = readb_relaxed(chip->IO_ADDR_R);
604e7544
VK
662 }
663}
664
4774fb0a
VK
665/*
666 * fsmc_read_buf_dma - read chip data into buffer
667 * @mtd: MTD device structure
668 * @buf: buffer to store date
669 * @len: number of bytes to read
670 */
671static void fsmc_read_buf_dma(struct mtd_info *mtd, uint8_t *buf, int len)
672{
673 struct fsmc_nand_data *host;
674
675 host = container_of(mtd, struct fsmc_nand_data, mtd);
676 dma_xfer(host, buf, len, DMA_FROM_DEVICE);
677}
678
679/*
680 * fsmc_write_buf_dma - write buffer to chip
681 * @mtd: MTD device structure
682 * @buf: data buffer
683 * @len: number of bytes to write
684 */
685static void fsmc_write_buf_dma(struct mtd_info *mtd, const uint8_t *buf,
686 int len)
687{
688 struct fsmc_nand_data *host;
689
690 host = container_of(mtd, struct fsmc_nand_data, mtd);
691 dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
692}
693
6c009ab8
LW
694/*
695 * fsmc_read_page_hwecc
696 * @mtd: mtd info structure
697 * @chip: nand chip info structure
698 * @buf: buffer to store read data
1fbb938d 699 * @oob_required: caller expects OOB data read to chip->oob_poi
6c009ab8
LW
700 * @page: page number to read
701 *
25985edc 702 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
6c009ab8
LW
703 * performed in a strict sequence as follows:
704 * data(512 byte) -> ecc(13 byte)
25985edc 705 * After this read, fsmc hardware generates and reports error data bits(up to a
6c009ab8
LW
706 * max of 8 bits)
707 */
708static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1fbb938d 709 uint8_t *buf, int oob_required, int page)
6c009ab8
LW
710{
711 struct fsmc_nand_data *host = container_of(mtd,
712 struct fsmc_nand_data, mtd);
713 struct fsmc_eccplace *ecc_place = host->ecc_place;
714 int i, j, s, stat, eccsize = chip->ecc.size;
715 int eccbytes = chip->ecc.bytes;
716 int eccsteps = chip->ecc.steps;
717 uint8_t *p = buf;
718 uint8_t *ecc_calc = chip->buffers->ecccalc;
719 uint8_t *ecc_code = chip->buffers->ecccode;
720 int off, len, group = 0;
721 /*
722 * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
723 * end up reading 14 bytes (7 words) from oob. The local array is
724 * to maintain word alignment
725 */
726 uint16_t ecc_oob[7];
727 uint8_t *oob = (uint8_t *)&ecc_oob[0];
3f91e94f 728 unsigned int max_bitflips = 0;
6c009ab8
LW
729
730 for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
6c009ab8
LW
731 chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
732 chip->ecc.hwctl(mtd, NAND_ECC_READ);
733 chip->read_buf(mtd, p, eccsize);
734
735 for (j = 0; j < eccbytes;) {
736 off = ecc_place->eccplace[group].offset;
737 len = ecc_place->eccplace[group].length;
738 group++;
739
740 /*
4cbe1bf0
VK
741 * length is intentionally kept a higher multiple of 2
742 * to read at least 13 bytes even in case of 16 bit NAND
743 * devices
744 */
aea686b4
VK
745 if (chip->options & NAND_BUSWIDTH_16)
746 len = roundup(len, 2);
747
6c009ab8
LW
748 chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
749 chip->read_buf(mtd, oob + j, len);
750 j += len;
751 }
752
519300cf 753 memcpy(&ecc_code[i], oob, chip->ecc.bytes);
6c009ab8
LW
754 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
755
756 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
3f91e94f 757 if (stat < 0) {
6c009ab8 758 mtd->ecc_stats.failed++;
3f91e94f 759 } else {
6c009ab8 760 mtd->ecc_stats.corrected += stat;
3f91e94f
MD
761 max_bitflips = max_t(unsigned int, max_bitflips, stat);
762 }
6c009ab8
LW
763 }
764
3f91e94f 765 return max_bitflips;
6c009ab8
LW
766}
767
768/*
753e0139 769 * fsmc_bch8_correct_data
6c009ab8
LW
770 * @mtd: mtd info structure
771 * @dat: buffer of read data
772 * @read_ecc: ecc read from device spare area
773 * @calc_ecc: ecc calculated from read data
774 *
775 * calc_ecc is a 104 bit information containing maximum of 8 error
776 * offset informations of 13 bits each in 512 bytes of read data.
777 */
753e0139 778static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
6c009ab8
LW
779 uint8_t *read_ecc, uint8_t *calc_ecc)
780{
781 struct fsmc_nand_data *host = container_of(mtd,
782 struct fsmc_nand_data, mtd);
519300cf 783 struct nand_chip *chip = mtd->priv;
2a5dbead 784 void __iomem *regs = host->regs_va;
6c009ab8 785 unsigned int bank = host->bank;
a612c2ae 786 uint32_t err_idx[8];
6c009ab8 787 uint32_t num_err, i;
753e0139 788 uint32_t ecc1, ecc2, ecc3, ecc4;
6c009ab8 789
a4742d51 790 num_err = (readl_relaxed(FSMC_NAND_REG(regs, bank, STS)) >> 10) & 0xF;
519300cf
VK
791
792 /* no bit flipping */
793 if (likely(num_err == 0))
794 return 0;
795
796 /* too many errors */
797 if (unlikely(num_err > 8)) {
798 /*
799 * This is a temporary erase check. A newly erased page read
800 * would result in an ecc error because the oob data is also
801 * erased to FF and the calculated ecc for an FF data is not
802 * FF..FF.
803 * This is a workaround to skip performing correction in case
804 * data is FF..FF
805 *
806 * Logic:
807 * For every page, each bit written as 0 is counted until these
808 * number of bits are greater than 8 (the maximum correction
809 * capability of FSMC for each 512 + 13 bytes)
810 */
811
812 int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
813 int bits_data = count_written_bits(dat, chip->ecc.size, 8);
814
815 if ((bits_ecc + bits_data) <= 8) {
816 if (bits_data)
817 memset(dat, 0xff, chip->ecc.size);
818 return bits_data;
819 }
820
821 return -EBADMSG;
822 }
823
6c009ab8
LW
824 /*
825 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
826 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
827 *
828 * calc_ecc is a 104 bit information containing maximum of 8 error
829 * offset informations of 13 bits each. calc_ecc is copied into a
830 * uint64_t array and error offset indexes are populated in err_idx
831 * array
832 */
a4742d51
VK
833 ecc1 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
834 ecc2 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC2));
835 ecc3 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC3));
836 ecc4 = readl_relaxed(FSMC_NAND_REG(regs, bank, STS));
753e0139
AV
837
838 err_idx[0] = (ecc1 >> 0) & 0x1FFF;
839 err_idx[1] = (ecc1 >> 13) & 0x1FFF;
840 err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
841 err_idx[3] = (ecc2 >> 7) & 0x1FFF;
842 err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
843 err_idx[5] = (ecc3 >> 1) & 0x1FFF;
844 err_idx[6] = (ecc3 >> 14) & 0x1FFF;
845 err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
6c009ab8
LW
846
847 i = 0;
848 while (num_err--) {
849 change_bit(0, (unsigned long *)&err_idx[i]);
850 change_bit(1, (unsigned long *)&err_idx[i]);
851
b533f8d8 852 if (err_idx[i] < chip->ecc.size * 8) {
6c009ab8
LW
853 change_bit(err_idx[i], (unsigned long *)dat);
854 i++;
855 }
856 }
857 return i;
858}
859
4774fb0a
VK
860static bool filter(struct dma_chan *chan, void *slave)
861{
862 chan->private = slave;
863 return true;
864}
865
eea62819 866#ifdef CONFIG_OF
06f25510 867static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
d8929942 868 struct device_node *np)
eea62819
SR
869{
870 struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
871 u32 val;
872
873 /* Set default NAND width to 8 bits */
874 pdata->width = 8;
875 if (!of_property_read_u32(np, "bank-width", &val)) {
876 if (val == 2) {
877 pdata->width = 16;
878 } else if (val != 1) {
879 dev_err(&pdev->dev, "invalid bank-width %u\n", val);
880 return -EINVAL;
881 }
882 }
eea62819
SR
883 if (of_get_property(np, "nand-skip-bbtscan", NULL))
884 pdata->options = NAND_SKIP_BBTSCAN;
885
886 return 0;
887}
888#else
06f25510 889static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
d8929942 890 struct device_node *np)
eea62819
SR
891{
892 return -ENOSYS;
893}
894#endif
895
6c009ab8
LW
896/*
897 * fsmc_nand_probe - Probe function
898 * @pdev: platform device structure
899 */
900static int __init fsmc_nand_probe(struct platform_device *pdev)
901{
902 struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
eea62819
SR
903 struct device_node __maybe_unused *np = pdev->dev.of_node;
904 struct mtd_part_parser_data ppdata = {};
6c009ab8
LW
905 struct fsmc_nand_data *host;
906 struct mtd_info *mtd;
907 struct nand_chip *nand;
6c009ab8 908 struct resource *res;
4774fb0a 909 dma_cap_mask_t mask;
4ad916bc 910 int ret = 0;
593cd871
LW
911 u32 pid;
912 int i;
6c009ab8 913
eea62819
SR
914 if (np) {
915 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
916 pdev->dev.platform_data = pdata;
917 ret = fsmc_nand_probe_config_dt(pdev, np);
918 if (ret) {
919 dev_err(&pdev->dev, "no platform data\n");
920 return -ENODEV;
921 }
922 }
923
6c009ab8
LW
924 if (!pdata) {
925 dev_err(&pdev->dev, "platform data is NULL\n");
926 return -EINVAL;
927 }
928
929 /* Allocate memory for the device structure (and zero it) */
82b9dbe2 930 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
6c009ab8
LW
931 if (!host) {
932 dev_err(&pdev->dev, "failed to allocate device structure\n");
933 return -ENOMEM;
934 }
935
936 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
82b9dbe2
VK
937 if (!res)
938 return -EINVAL;
6c009ab8 939
6d7b42a4 940 host->data_va = devm_request_and_ioremap(&pdev->dev, res);
6c009ab8 941 if (!host->data_va) {
82b9dbe2
VK
942 dev_err(&pdev->dev, "data ioremap failed\n");
943 return -ENOMEM;
6c009ab8 944 }
6d7b42a4 945 host->data_pa = (dma_addr_t)res->start;
6c009ab8 946
6d7b42a4
JCPV
947 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
948 if (!res)
949 return -EINVAL;
6c009ab8 950
6d7b42a4 951 host->addr_va = devm_request_and_ioremap(&pdev->dev, res);
6c009ab8 952 if (!host->addr_va) {
82b9dbe2
VK
953 dev_err(&pdev->dev, "ale ioremap failed\n");
954 return -ENOMEM;
6c009ab8
LW
955 }
956
6d7b42a4
JCPV
957 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
958 if (!res)
959 return -EINVAL;
6c009ab8 960
6d7b42a4 961 host->cmd_va = devm_request_and_ioremap(&pdev->dev, res);
6c009ab8 962 if (!host->cmd_va) {
82b9dbe2
VK
963 dev_err(&pdev->dev, "ale ioremap failed\n");
964 return -ENOMEM;
6c009ab8
LW
965 }
966
967 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
82b9dbe2
VK
968 if (!res)
969 return -EINVAL;
6c009ab8 970
6d7b42a4 971 host->regs_va = devm_request_and_ioremap(&pdev->dev, res);
6c009ab8 972 if (!host->regs_va) {
82b9dbe2
VK
973 dev_err(&pdev->dev, "regs ioremap failed\n");
974 return -ENOMEM;
6c009ab8
LW
975 }
976
977 host->clk = clk_get(&pdev->dev, NULL);
978 if (IS_ERR(host->clk)) {
979 dev_err(&pdev->dev, "failed to fetch block clock\n");
82b9dbe2 980 return PTR_ERR(host->clk);
6c009ab8
LW
981 }
982
e25da1c0 983 ret = clk_prepare_enable(host->clk);
6c009ab8 984 if (ret)
e25da1c0 985 goto err_clk_prepare_enable;
6c009ab8 986
593cd871
LW
987 /*
988 * This device ID is actually a common AMBA ID as used on the
989 * AMBA PrimeCell bus. However it is not a PrimeCell.
990 */
991 for (pid = 0, i = 0; i < 4; i++)
992 pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
993 host->pid = pid;
994 dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
995 "revision %02x, config %02x\n",
996 AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
997 AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
998
6c009ab8
LW
999 host->bank = pdata->bank;
1000 host->select_chip = pdata->select_bank;
71470324
VK
1001 host->partitions = pdata->partitions;
1002 host->nr_partitions = pdata->nr_partitions;
712c4add 1003 host->dev = &pdev->dev;
e2f6bce8 1004 host->dev_timings = pdata->nand_timings;
4774fb0a
VK
1005 host->mode = pdata->mode;
1006
1007 if (host->mode == USE_DMA_ACCESS)
1008 init_completion(&host->dma_access_complete);
1009
6c009ab8
LW
1010 /* Link all private pointers */
1011 mtd = &host->mtd;
1012 nand = &host->nand;
1013 mtd->priv = nand;
1014 nand->priv = host;
1015
1016 host->mtd.owner = THIS_MODULE;
1017 nand->IO_ADDR_R = host->data_va;
1018 nand->IO_ADDR_W = host->data_va;
1019 nand->cmd_ctrl = fsmc_cmd_ctrl;
1020 nand->chip_delay = 30;
1021
1022 nand->ecc.mode = NAND_ECC_HW;
1023 nand->ecc.hwctl = fsmc_enable_hwecc;
1024 nand->ecc.size = 512;
1025 nand->options = pdata->options;
1026 nand->select_chip = fsmc_select_chip;
467e6e7b 1027 nand->badblockbits = 7;
6c009ab8
LW
1028
1029 if (pdata->width == FSMC_NAND_BW16)
1030 nand->options |= NAND_BUSWIDTH_16;
1031
4774fb0a
VK
1032 switch (host->mode) {
1033 case USE_DMA_ACCESS:
1034 dma_cap_zero(mask);
1035 dma_cap_set(DMA_MEMCPY, mask);
1036 host->read_dma_chan = dma_request_channel(mask, filter,
1037 pdata->read_dma_priv);
1038 if (!host->read_dma_chan) {
1039 dev_err(&pdev->dev, "Unable to get read dma channel\n");
1040 goto err_req_read_chnl;
1041 }
1042 host->write_dma_chan = dma_request_channel(mask, filter,
1043 pdata->write_dma_priv);
1044 if (!host->write_dma_chan) {
1045 dev_err(&pdev->dev, "Unable to get write dma channel\n");
1046 goto err_req_write_chnl;
1047 }
1048 nand->read_buf = fsmc_read_buf_dma;
1049 nand->write_buf = fsmc_write_buf_dma;
1050 break;
1051
1052 default:
1053 case USE_WORD_ACCESS:
604e7544
VK
1054 nand->read_buf = fsmc_read_buf;
1055 nand->write_buf = fsmc_write_buf;
4774fb0a 1056 break;
604e7544
VK
1057 }
1058
2a5dbead
VK
1059 fsmc_nand_setup(host->regs_va, host->bank,
1060 nand->options & NAND_BUSWIDTH_16,
e2f6bce8 1061 host->dev_timings);
6c009ab8 1062
593cd871 1063 if (AMBA_REV_BITS(host->pid) >= 8) {
6c009ab8
LW
1064 nand->ecc.read_page = fsmc_read_page_hwecc;
1065 nand->ecc.calculate = fsmc_read_hwecc_ecc4;
753e0139 1066 nand->ecc.correct = fsmc_bch8_correct_data;
6c009ab8 1067 nand->ecc.bytes = 13;
6a918bad 1068 nand->ecc.strength = 8;
6c009ab8
LW
1069 } else {
1070 nand->ecc.calculate = fsmc_read_hwecc_ecc1;
1071 nand->ecc.correct = nand_correct_data;
1072 nand->ecc.bytes = 3;
6a918bad 1073 nand->ecc.strength = 1;
6c009ab8
LW
1074 }
1075
1076 /*
25985edc 1077 * Scan to find existence of the device
6c009ab8
LW
1078 */
1079 if (nand_scan_ident(&host->mtd, 1, NULL)) {
1080 ret = -ENXIO;
1081 dev_err(&pdev->dev, "No NAND Device found!\n");
82b9dbe2 1082 goto err_scan_ident;
6c009ab8
LW
1083 }
1084
593cd871 1085 if (AMBA_REV_BITS(host->pid) >= 8) {
e29ee57b
BY
1086 switch (host->mtd.oobsize) {
1087 case 16:
1088 nand->ecc.layout = &fsmc_ecc4_16_layout;
6c009ab8 1089 host->ecc_place = &fsmc_ecc4_sp_place;
e29ee57b
BY
1090 break;
1091 case 64:
1092 nand->ecc.layout = &fsmc_ecc4_64_layout;
1093 host->ecc_place = &fsmc_ecc4_lp_place;
1094 break;
1095 case 128:
1096 nand->ecc.layout = &fsmc_ecc4_128_layout;
1097 host->ecc_place = &fsmc_ecc4_lp_place;
1098 break;
0c78e93b
AV
1099 case 224:
1100 nand->ecc.layout = &fsmc_ecc4_224_layout;
1101 host->ecc_place = &fsmc_ecc4_lp_place;
1102 break;
e29ee57b
BY
1103 case 256:
1104 nand->ecc.layout = &fsmc_ecc4_256_layout;
6c009ab8 1105 host->ecc_place = &fsmc_ecc4_lp_place;
e29ee57b
BY
1106 break;
1107 default:
1108 printk(KERN_WARNING "No oob scheme defined for "
1109 "oobsize %d\n", mtd->oobsize);
1110 BUG();
6c009ab8
LW
1111 }
1112 } else {
e29ee57b
BY
1113 switch (host->mtd.oobsize) {
1114 case 16:
1115 nand->ecc.layout = &fsmc_ecc1_16_layout;
1116 break;
1117 case 64:
1118 nand->ecc.layout = &fsmc_ecc1_64_layout;
1119 break;
1120 case 128:
1121 nand->ecc.layout = &fsmc_ecc1_128_layout;
1122 break;
1123 default:
1124 printk(KERN_WARNING "No oob scheme defined for "
1125 "oobsize %d\n", mtd->oobsize);
1126 BUG();
1127 }
6c009ab8
LW
1128 }
1129
1130 /* Second stage of scan to fill MTD data-structures */
1131 if (nand_scan_tail(&host->mtd)) {
1132 ret = -ENXIO;
1133 goto err_probe;
1134 }
1135
1136 /*
1137 * The partition information can is accessed by (in the same precedence)
1138 *
1139 * command line through Bootloader,
1140 * platform data,
1141 * default partition information present in driver.
1142 */
6c009ab8 1143 /*
8d3f8bb8 1144 * Check for partition info passed
6c009ab8
LW
1145 */
1146 host->mtd.name = "nand";
eea62819
SR
1147 ppdata.of_node = np;
1148 ret = mtd_device_parse_register(&host->mtd, NULL, &ppdata,
71470324 1149 host->partitions, host->nr_partitions);
99335d00 1150 if (ret)
6c009ab8 1151 goto err_probe;
6c009ab8
LW
1152
1153 platform_set_drvdata(pdev, host);
1154 dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
1155 return 0;
1156
1157err_probe:
82b9dbe2 1158err_scan_ident:
4774fb0a
VK
1159 if (host->mode == USE_DMA_ACCESS)
1160 dma_release_channel(host->write_dma_chan);
1161err_req_write_chnl:
1162 if (host->mode == USE_DMA_ACCESS)
1163 dma_release_channel(host->read_dma_chan);
1164err_req_read_chnl:
e25da1c0
VK
1165 clk_disable_unprepare(host->clk);
1166err_clk_prepare_enable:
82b9dbe2 1167 clk_put(host->clk);
6c009ab8
LW
1168 return ret;
1169}
1170
1171/*
1172 * Clean up routine
1173 */
1174static int fsmc_nand_remove(struct platform_device *pdev)
1175{
1176 struct fsmc_nand_data *host = platform_get_drvdata(pdev);
1177
1178 platform_set_drvdata(pdev, NULL);
1179
1180 if (host) {
82e023ab 1181 nand_release(&host->mtd);
4774fb0a
VK
1182
1183 if (host->mode == USE_DMA_ACCESS) {
1184 dma_release_channel(host->write_dma_chan);
1185 dma_release_channel(host->read_dma_chan);
1186 }
e25da1c0 1187 clk_disable_unprepare(host->clk);
6c009ab8 1188 clk_put(host->clk);
6c009ab8 1189 }
82b9dbe2 1190
6c009ab8
LW
1191 return 0;
1192}
1193
1194#ifdef CONFIG_PM
1195static int fsmc_nand_suspend(struct device *dev)
1196{
1197 struct fsmc_nand_data *host = dev_get_drvdata(dev);
1198 if (host)
e25da1c0 1199 clk_disable_unprepare(host->clk);
6c009ab8
LW
1200 return 0;
1201}
1202
1203static int fsmc_nand_resume(struct device *dev)
1204{
1205 struct fsmc_nand_data *host = dev_get_drvdata(dev);
f63acb75 1206 if (host) {
e25da1c0 1207 clk_prepare_enable(host->clk);
f63acb75 1208 fsmc_nand_setup(host->regs_va, host->bank,
e2f6bce8
VK
1209 host->nand.options & NAND_BUSWIDTH_16,
1210 host->dev_timings);
f63acb75 1211 }
6c009ab8
LW
1212 return 0;
1213}
1214
f63acb75 1215static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
6c009ab8
LW
1216#endif
1217
eea62819
SR
1218#ifdef CONFIG_OF
1219static const struct of_device_id fsmc_nand_id_table[] = {
1220 { .compatible = "st,spear600-fsmc-nand" },
1221 {}
1222};
1223MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);
1224#endif
1225
6c009ab8
LW
1226static struct platform_driver fsmc_nand_driver = {
1227 .remove = fsmc_nand_remove,
1228 .driver = {
1229 .owner = THIS_MODULE,
1230 .name = "fsmc-nand",
eea62819 1231 .of_match_table = of_match_ptr(fsmc_nand_id_table),
6c009ab8
LW
1232#ifdef CONFIG_PM
1233 .pm = &fsmc_nand_pm_ops,
1234#endif
1235 },
1236};
1237
1238static int __init fsmc_nand_init(void)
1239{
1240 return platform_driver_probe(&fsmc_nand_driver,
1241 fsmc_nand_probe);
1242}
1243module_init(fsmc_nand_init);
1244
1245static void __exit fsmc_nand_exit(void)
1246{
1247 platform_driver_unregister(&fsmc_nand_driver);
1248}
1249module_exit(fsmc_nand_exit);
1250
1251MODULE_LICENSE("GPL");
1252MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
1253MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");