blob: d20a0c63251e687a7adbf72c11b36df9832357cf [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>
34#include <linux/mtd/partitions.h>
35#include <linux/io.h>
36#include <linux/slab.h>
37#include <linux/mtd/fsmc.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
Bhavna Yadave29ee572012-03-07 17:00:50 +053041static struct nand_ecclayout fsmc_ecc1_128_layout = {
Linus Walleij6c009ab2010-09-13 00:35:22 +020042 .eccbytes = 24,
43 .eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52,
44 66, 67, 68, 82, 83, 84, 98, 99, 100, 114, 115, 116},
45 .oobfree = {
46 {.offset = 8, .length = 8},
47 {.offset = 24, .length = 8},
48 {.offset = 40, .length = 8},
49 {.offset = 56, .length = 8},
50 {.offset = 72, .length = 8},
51 {.offset = 88, .length = 8},
52 {.offset = 104, .length = 8},
53 {.offset = 120, .length = 8}
54 }
55};
56
Bhavna Yadave29ee572012-03-07 17:00:50 +053057static struct nand_ecclayout fsmc_ecc1_64_layout = {
58 .eccbytes = 12,
59 .eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52},
60 .oobfree = {
61 {.offset = 8, .length = 8},
62 {.offset = 24, .length = 8},
63 {.offset = 40, .length = 8},
64 {.offset = 56, .length = 8},
65 }
66};
67
68static struct nand_ecclayout fsmc_ecc1_16_layout = {
69 .eccbytes = 3,
70 .eccpos = {2, 3, 4},
71 .oobfree = {
72 {.offset = 8, .length = 8},
73 }
74};
75
76/*
77 * ECC4 layout for NAND of pagesize 8192 bytes & OOBsize 256 bytes. 13*16 bytes
78 * of OB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block and 46
79 * bytes are free for use.
80 */
81static struct nand_ecclayout fsmc_ecc4_256_layout = {
82 .eccbytes = 208,
83 .eccpos = { 2, 3, 4, 5, 6, 7, 8,
84 9, 10, 11, 12, 13, 14,
85 18, 19, 20, 21, 22, 23, 24,
86 25, 26, 27, 28, 29, 30,
87 34, 35, 36, 37, 38, 39, 40,
88 41, 42, 43, 44, 45, 46,
89 50, 51, 52, 53, 54, 55, 56,
90 57, 58, 59, 60, 61, 62,
91 66, 67, 68, 69, 70, 71, 72,
92 73, 74, 75, 76, 77, 78,
93 82, 83, 84, 85, 86, 87, 88,
94 89, 90, 91, 92, 93, 94,
95 98, 99, 100, 101, 102, 103, 104,
96 105, 106, 107, 108, 109, 110,
97 114, 115, 116, 117, 118, 119, 120,
98 121, 122, 123, 124, 125, 126,
99 130, 131, 132, 133, 134, 135, 136,
100 137, 138, 139, 140, 141, 142,
101 146, 147, 148, 149, 150, 151, 152,
102 153, 154, 155, 156, 157, 158,
103 162, 163, 164, 165, 166, 167, 168,
104 169, 170, 171, 172, 173, 174,
105 178, 179, 180, 181, 182, 183, 184,
106 185, 186, 187, 188, 189, 190,
107 194, 195, 196, 197, 198, 199, 200,
108 201, 202, 203, 204, 205, 206,
109 210, 211, 212, 213, 214, 215, 216,
110 217, 218, 219, 220, 221, 222,
111 226, 227, 228, 229, 230, 231, 232,
112 233, 234, 235, 236, 237, 238,
113 242, 243, 244, 245, 246, 247, 248,
114 249, 250, 251, 252, 253, 254
115 },
116 .oobfree = {
117 {.offset = 15, .length = 3},
118 {.offset = 31, .length = 3},
119 {.offset = 47, .length = 3},
120 {.offset = 63, .length = 3},
121 {.offset = 79, .length = 3},
122 {.offset = 95, .length = 3},
123 {.offset = 111, .length = 3},
124 {.offset = 127, .length = 3},
125 {.offset = 143, .length = 3},
126 {.offset = 159, .length = 3},
127 {.offset = 175, .length = 3},
128 {.offset = 191, .length = 3},
129 {.offset = 207, .length = 3},
130 {.offset = 223, .length = 3},
131 {.offset = 239, .length = 3},
132 {.offset = 255, .length = 1}
133 }
134};
135
136/*
Armando Visconti0c78e932012-03-07 17:00:55 +0530137 * ECC4 layout for NAND of pagesize 4096 bytes & OOBsize 224 bytes. 13*8 bytes
138 * of OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block & 118
139 * bytes are free for use.
140 */
141static struct nand_ecclayout fsmc_ecc4_224_layout = {
142 .eccbytes = 104,
143 .eccpos = { 2, 3, 4, 5, 6, 7, 8,
144 9, 10, 11, 12, 13, 14,
145 18, 19, 20, 21, 22, 23, 24,
146 25, 26, 27, 28, 29, 30,
147 34, 35, 36, 37, 38, 39, 40,
148 41, 42, 43, 44, 45, 46,
149 50, 51, 52, 53, 54, 55, 56,
150 57, 58, 59, 60, 61, 62,
151 66, 67, 68, 69, 70, 71, 72,
152 73, 74, 75, 76, 77, 78,
153 82, 83, 84, 85, 86, 87, 88,
154 89, 90, 91, 92, 93, 94,
155 98, 99, 100, 101, 102, 103, 104,
156 105, 106, 107, 108, 109, 110,
157 114, 115, 116, 117, 118, 119, 120,
158 121, 122, 123, 124, 125, 126
159 },
160 .oobfree = {
161 {.offset = 15, .length = 3},
162 {.offset = 31, .length = 3},
163 {.offset = 47, .length = 3},
164 {.offset = 63, .length = 3},
165 {.offset = 79, .length = 3},
166 {.offset = 95, .length = 3},
167 {.offset = 111, .length = 3},
168 {.offset = 127, .length = 97}
169 }
170};
171
172/*
Bhavna Yadave29ee572012-03-07 17:00:50 +0530173 * ECC4 layout for NAND of pagesize 4096 bytes & OOBsize 128 bytes. 13*8 bytes
174 * of OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block & 22
175 * bytes are free for use.
176 */
177static struct nand_ecclayout fsmc_ecc4_128_layout = {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200178 .eccbytes = 104,
179 .eccpos = { 2, 3, 4, 5, 6, 7, 8,
180 9, 10, 11, 12, 13, 14,
181 18, 19, 20, 21, 22, 23, 24,
182 25, 26, 27, 28, 29, 30,
183 34, 35, 36, 37, 38, 39, 40,
184 41, 42, 43, 44, 45, 46,
185 50, 51, 52, 53, 54, 55, 56,
186 57, 58, 59, 60, 61, 62,
187 66, 67, 68, 69, 70, 71, 72,
188 73, 74, 75, 76, 77, 78,
189 82, 83, 84, 85, 86, 87, 88,
190 89, 90, 91, 92, 93, 94,
191 98, 99, 100, 101, 102, 103, 104,
192 105, 106, 107, 108, 109, 110,
193 114, 115, 116, 117, 118, 119, 120,
194 121, 122, 123, 124, 125, 126
195 },
196 .oobfree = {
197 {.offset = 15, .length = 3},
198 {.offset = 31, .length = 3},
199 {.offset = 47, .length = 3},
200 {.offset = 63, .length = 3},
201 {.offset = 79, .length = 3},
202 {.offset = 95, .length = 3},
203 {.offset = 111, .length = 3},
204 {.offset = 127, .length = 1}
205 }
206};
207
208/*
Bhavna Yadave29ee572012-03-07 17:00:50 +0530209 * ECC4 layout for NAND of pagesize 2048 bytes & OOBsize 64 bytes. 13*4 bytes of
210 * OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block and 10
211 * bytes are free for use.
212 */
213static struct nand_ecclayout fsmc_ecc4_64_layout = {
214 .eccbytes = 52,
215 .eccpos = { 2, 3, 4, 5, 6, 7, 8,
216 9, 10, 11, 12, 13, 14,
217 18, 19, 20, 21, 22, 23, 24,
218 25, 26, 27, 28, 29, 30,
219 34, 35, 36, 37, 38, 39, 40,
220 41, 42, 43, 44, 45, 46,
221 50, 51, 52, 53, 54, 55, 56,
222 57, 58, 59, 60, 61, 62,
223 },
224 .oobfree = {
225 {.offset = 15, .length = 3},
226 {.offset = 31, .length = 3},
227 {.offset = 47, .length = 3},
228 {.offset = 63, .length = 1},
229 }
230};
231
232/*
233 * ECC4 layout for NAND of pagesize 512 bytes & OOBsize 16 bytes. 13 bytes of
234 * OOB size is reserved for ECC, Byte no. 4 & 5 reserved for bad block and One
235 * byte is free for use.
236 */
237static struct nand_ecclayout fsmc_ecc4_16_layout = {
238 .eccbytes = 13,
239 .eccpos = { 0, 1, 2, 3, 6, 7, 8,
240 9, 10, 11, 12, 13, 14
241 },
242 .oobfree = {
243 {.offset = 15, .length = 1},
244 }
245};
246
247/*
Linus Walleij6c009ab2010-09-13 00:35:22 +0200248 * ECC placement definitions in oobfree type format.
249 * There are 13 bytes of ecc for every 512 byte block and it has to be read
250 * consecutively and immediately after the 512 byte data block for hardware to
251 * generate the error bit offsets in 512 byte data.
252 * Managing the ecc bytes in the following way makes it easier for software to
253 * read ecc bytes consecutive to data bytes. This way is similar to
254 * oobfree structure maintained already in generic nand driver
255 */
256static struct fsmc_eccplace fsmc_ecc4_lp_place = {
257 .eccplace = {
258 {.offset = 2, .length = 13},
259 {.offset = 18, .length = 13},
260 {.offset = 34, .length = 13},
261 {.offset = 50, .length = 13},
262 {.offset = 66, .length = 13},
263 {.offset = 82, .length = 13},
264 {.offset = 98, .length = 13},
265 {.offset = 114, .length = 13}
266 }
267};
268
Linus Walleij6c009ab2010-09-13 00:35:22 +0200269static struct fsmc_eccplace fsmc_ecc4_sp_place = {
270 .eccplace = {
271 {.offset = 0, .length = 4},
272 {.offset = 6, .length = 9}
273 }
274};
275
Linus Walleij6c009ab2010-09-13 00:35:22 +0200276/**
Linus Walleij593cd872010-11-29 13:52:19 +0100277 * struct fsmc_nand_data - structure for FSMC NAND device state
Linus Walleij6c009ab2010-09-13 00:35:22 +0200278 *
Linus Walleij593cd872010-11-29 13:52:19 +0100279 * @pid: Part ID on the AMBA PrimeCell format
Linus Walleij6c009ab2010-09-13 00:35:22 +0200280 * @mtd: MTD info for a NAND flash.
281 * @nand: Chip related info for a NAND flash.
Vipin Kumar71470322012-03-14 11:47:07 +0530282 * @partitions: Partition info for a NAND Flash.
283 * @nr_partitions: Total number of partition of a NAND flash.
Linus Walleij6c009ab2010-09-13 00:35:22 +0200284 *
285 * @ecc_place: ECC placing locations in oobfree type format.
286 * @bank: Bank number for probed device.
287 * @clk: Clock structure for FSMC.
288 *
Vipin Kumar4774fb02012-03-14 11:47:18 +0530289 * @read_dma_chan: DMA channel for read access
290 * @write_dma_chan: DMA channel for write access to NAND
291 * @dma_access_complete: Completion structure
292 *
293 * @data_pa: NAND Physical port for Data.
Linus Walleij6c009ab2010-09-13 00:35:22 +0200294 * @data_va: NAND port for Data.
295 * @cmd_va: NAND port for Command.
296 * @addr_va: NAND port for Address.
297 * @regs_va: FSMC regs base address.
298 */
299struct fsmc_nand_data {
Linus Walleij593cd872010-11-29 13:52:19 +0100300 u32 pid;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200301 struct mtd_info mtd;
302 struct nand_chip nand;
Vipin Kumar71470322012-03-14 11:47:07 +0530303 struct mtd_partition *partitions;
304 unsigned int nr_partitions;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200305
306 struct fsmc_eccplace *ecc_place;
307 unsigned int bank;
Vipin Kumar712c4ad2012-03-14 11:47:16 +0530308 struct device *dev;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530309 enum access_mode mode;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200310 struct clk *clk;
311
Vipin Kumar4774fb02012-03-14 11:47:18 +0530312 /* DMA related objects */
313 struct dma_chan *read_dma_chan;
314 struct dma_chan *write_dma_chan;
315 struct completion dma_access_complete;
316
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530317 struct fsmc_nand_timings *dev_timings;
318
Vipin Kumar4774fb02012-03-14 11:47:18 +0530319 dma_addr_t data_pa;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200320 void __iomem *data_va;
321 void __iomem *cmd_va;
322 void __iomem *addr_va;
323 void __iomem *regs_va;
324
325 void (*select_chip)(uint32_t bank, uint32_t busw);
326};
327
328/* Assert CS signal based on chipnr */
329static void fsmc_select_chip(struct mtd_info *mtd, int chipnr)
330{
331 struct nand_chip *chip = mtd->priv;
332 struct fsmc_nand_data *host;
333
334 host = container_of(mtd, struct fsmc_nand_data, mtd);
335
336 switch (chipnr) {
337 case -1:
338 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
339 break;
340 case 0:
341 case 1:
342 case 2:
343 case 3:
344 if (host->select_chip)
345 host->select_chip(chipnr,
346 chip->options & NAND_BUSWIDTH_16);
347 break;
348
349 default:
350 BUG();
351 }
352}
353
354/*
355 * fsmc_cmd_ctrl - For facilitaing Hardware access
356 * This routine allows hardware specific access to control-lines(ALE,CLE)
357 */
358static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
359{
360 struct nand_chip *this = mtd->priv;
361 struct fsmc_nand_data *host = container_of(mtd,
362 struct fsmc_nand_data, mtd);
363 struct fsmc_regs *regs = host->regs_va;
364 unsigned int bank = host->bank;
365
366 if (ctrl & NAND_CTRL_CHANGE) {
367 if (ctrl & NAND_CLE) {
368 this->IO_ADDR_R = (void __iomem *)host->cmd_va;
369 this->IO_ADDR_W = (void __iomem *)host->cmd_va;
370 } else if (ctrl & NAND_ALE) {
371 this->IO_ADDR_R = (void __iomem *)host->addr_va;
372 this->IO_ADDR_W = (void __iomem *)host->addr_va;
373 } else {
374 this->IO_ADDR_R = (void __iomem *)host->data_va;
375 this->IO_ADDR_W = (void __iomem *)host->data_va;
376 }
377
378 if (ctrl & NAND_NCE) {
379 writel(readl(&regs->bank_regs[bank].pc) | FSMC_ENABLE,
380 &regs->bank_regs[bank].pc);
381 } else {
382 writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ENABLE,
383 &regs->bank_regs[bank].pc);
384 }
385 }
386
387 mb();
388
389 if (cmd != NAND_CMD_NONE)
390 writeb(cmd, this->IO_ADDR_W);
391}
392
393/*
394 * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
395 *
396 * This routine initializes timing parameters related to NAND memory access in
397 * FSMC registers
398 */
Shiraz Hashimf63acb72012-03-14 11:47:13 +0530399static void fsmc_nand_setup(struct fsmc_regs *regs, uint32_t bank,
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530400 uint32_t busw, struct fsmc_nand_timings *timings)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200401{
402 uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530403 uint32_t tclr, tar, thiz, thold, twait, tset;
404 struct fsmc_nand_timings *tims;
405 struct fsmc_nand_timings default_timings = {
406 .tclr = FSMC_TCLR_1,
407 .tar = FSMC_TAR_1,
408 .thiz = FSMC_THIZ_1,
409 .thold = FSMC_THOLD_4,
410 .twait = FSMC_TWAIT_6,
411 .tset = FSMC_TSET_0,
412 };
413
414 if (timings)
415 tims = timings;
416 else
417 tims = &default_timings;
418
419 tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT;
420 tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT;
421 thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT;
422 thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT;
423 twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT;
424 tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200425
426 if (busw)
427 writel(value | FSMC_DEVWID_16, &regs->bank_regs[bank].pc);
428 else
429 writel(value | FSMC_DEVWID_8, &regs->bank_regs[bank].pc);
430
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530431 writel(readl(&regs->bank_regs[bank].pc) | tclr | tar,
Linus Walleij6c009ab2010-09-13 00:35:22 +0200432 &regs->bank_regs[bank].pc);
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530433 writel(thiz | thold | twait | tset, &regs->bank_regs[bank].comm);
434 writel(thiz | thold | twait | tset, &regs->bank_regs[bank].attrib);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200435}
436
437/*
438 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
439 */
440static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
441{
442 struct fsmc_nand_data *host = container_of(mtd,
443 struct fsmc_nand_data, mtd);
444 struct fsmc_regs *regs = host->regs_va;
445 uint32_t bank = host->bank;
446
447 writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ECCPLEN_256,
448 &regs->bank_regs[bank].pc);
449 writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ECCEN,
450 &regs->bank_regs[bank].pc);
451 writel(readl(&regs->bank_regs[bank].pc) | FSMC_ECCEN,
452 &regs->bank_regs[bank].pc);
453}
454
455/*
456 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300457 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200458 * max of 8-bits)
459 */
460static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
461 uint8_t *ecc)
462{
463 struct fsmc_nand_data *host = container_of(mtd,
464 struct fsmc_nand_data, mtd);
465 struct fsmc_regs *regs = host->regs_va;
466 uint32_t bank = host->bank;
467 uint32_t ecc_tmp;
468 unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
469
470 do {
471 if (readl(&regs->bank_regs[bank].sts) & FSMC_CODE_RDY)
472 break;
473 else
474 cond_resched();
475 } while (!time_after_eq(jiffies, deadline));
476
Vipin Kumar712c4ad2012-03-14 11:47:16 +0530477 if (time_after_eq(jiffies, deadline)) {
478 dev_err(host->dev, "calculate ecc timed out\n");
479 return -ETIMEDOUT;
480 }
481
Linus Walleij6c009ab2010-09-13 00:35:22 +0200482 ecc_tmp = readl(&regs->bank_regs[bank].ecc1);
483 ecc[0] = (uint8_t) (ecc_tmp >> 0);
484 ecc[1] = (uint8_t) (ecc_tmp >> 8);
485 ecc[2] = (uint8_t) (ecc_tmp >> 16);
486 ecc[3] = (uint8_t) (ecc_tmp >> 24);
487
488 ecc_tmp = readl(&regs->bank_regs[bank].ecc2);
489 ecc[4] = (uint8_t) (ecc_tmp >> 0);
490 ecc[5] = (uint8_t) (ecc_tmp >> 8);
491 ecc[6] = (uint8_t) (ecc_tmp >> 16);
492 ecc[7] = (uint8_t) (ecc_tmp >> 24);
493
494 ecc_tmp = readl(&regs->bank_regs[bank].ecc3);
495 ecc[8] = (uint8_t) (ecc_tmp >> 0);
496 ecc[9] = (uint8_t) (ecc_tmp >> 8);
497 ecc[10] = (uint8_t) (ecc_tmp >> 16);
498 ecc[11] = (uint8_t) (ecc_tmp >> 24);
499
500 ecc_tmp = readl(&regs->bank_regs[bank].sts);
501 ecc[12] = (uint8_t) (ecc_tmp >> 16);
502
503 return 0;
504}
505
506/*
507 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300508 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200509 * max of 1-bit)
510 */
511static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
512 uint8_t *ecc)
513{
514 struct fsmc_nand_data *host = container_of(mtd,
515 struct fsmc_nand_data, mtd);
516 struct fsmc_regs *regs = host->regs_va;
517 uint32_t bank = host->bank;
518 uint32_t ecc_tmp;
519
520 ecc_tmp = readl(&regs->bank_regs[bank].ecc1);
521 ecc[0] = (uint8_t) (ecc_tmp >> 0);
522 ecc[1] = (uint8_t) (ecc_tmp >> 8);
523 ecc[2] = (uint8_t) (ecc_tmp >> 16);
524
525 return 0;
526}
527
Vipin Kumar519300c2012-03-07 17:00:49 +0530528/* Count the number of 0's in buff upto a max of max_bits */
529static int count_written_bits(uint8_t *buff, int size, int max_bits)
530{
531 int k, written_bits = 0;
532
533 for (k = 0; k < size; k++) {
534 written_bits += hweight8(~buff[k]);
535 if (written_bits > max_bits)
536 break;
537 }
538
539 return written_bits;
540}
541
Vipin Kumar4774fb02012-03-14 11:47:18 +0530542static void dma_complete(void *param)
543{
544 struct fsmc_nand_data *host = param;
545
546 complete(&host->dma_access_complete);
547}
548
549static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len,
550 enum dma_data_direction direction)
551{
552 struct dma_chan *chan;
553 struct dma_device *dma_dev;
554 struct dma_async_tx_descriptor *tx;
555 dma_addr_t dma_dst, dma_src, dma_addr;
556 dma_cookie_t cookie;
557 unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
558 int ret;
559
560 if (direction == DMA_TO_DEVICE)
561 chan = host->write_dma_chan;
562 else if (direction == DMA_FROM_DEVICE)
563 chan = host->read_dma_chan;
564 else
565 return -EINVAL;
566
567 dma_dev = chan->device;
568 dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction);
569
570 if (direction == DMA_TO_DEVICE) {
571 dma_src = dma_addr;
572 dma_dst = host->data_pa;
573 flags |= DMA_COMPL_SRC_UNMAP_SINGLE | DMA_COMPL_SKIP_DEST_UNMAP;
574 } else {
575 dma_src = host->data_pa;
576 dma_dst = dma_addr;
577 flags |= DMA_COMPL_DEST_UNMAP_SINGLE | DMA_COMPL_SKIP_SRC_UNMAP;
578 }
579
580 tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
581 len, flags);
582
583 if (!tx) {
584 dev_err(host->dev, "device_prep_dma_memcpy error\n");
585 dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
586 return -EIO;
587 }
588
589 tx->callback = dma_complete;
590 tx->callback_param = host;
591 cookie = tx->tx_submit(tx);
592
593 ret = dma_submit_error(cookie);
594 if (ret) {
595 dev_err(host->dev, "dma_submit_error %d\n", cookie);
596 return ret;
597 }
598
599 dma_async_issue_pending(chan);
600
601 ret =
602 wait_for_completion_interruptible_timeout(&host->dma_access_complete,
603 msecs_to_jiffies(3000));
604 if (ret <= 0) {
605 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
606 dev_err(host->dev, "wait_for_completion_timeout\n");
607 return ret ? ret : -ETIMEDOUT;
608 }
609
610 return 0;
611}
612
Linus Walleij6c009ab2010-09-13 00:35:22 +0200613/*
Vipin Kumar604e7542012-03-14 11:47:17 +0530614 * fsmc_write_buf - write buffer to chip
615 * @mtd: MTD device structure
616 * @buf: data buffer
617 * @len: number of bytes to write
618 */
619static void fsmc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
620{
621 int i;
622 struct nand_chip *chip = mtd->priv;
623
624 if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
625 IS_ALIGNED(len, sizeof(uint32_t))) {
626 uint32_t *p = (uint32_t *)buf;
627 len = len >> 2;
628 for (i = 0; i < len; i++)
629 writel(p[i], chip->IO_ADDR_W);
630 } else {
631 for (i = 0; i < len; i++)
632 writeb(buf[i], chip->IO_ADDR_W);
633 }
634}
635
636/*
637 * fsmc_read_buf - read chip data into buffer
638 * @mtd: MTD device structure
639 * @buf: buffer to store date
640 * @len: number of bytes to read
641 */
642static void fsmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
643{
644 int i;
645 struct nand_chip *chip = mtd->priv;
646
647 if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
648 IS_ALIGNED(len, sizeof(uint32_t))) {
649 uint32_t *p = (uint32_t *)buf;
650 len = len >> 2;
651 for (i = 0; i < len; i++)
652 p[i] = readl(chip->IO_ADDR_R);
653 } else {
654 for (i = 0; i < len; i++)
655 buf[i] = readb(chip->IO_ADDR_R);
656 }
657}
658
659/*
Vipin Kumar4774fb02012-03-14 11:47:18 +0530660 * fsmc_read_buf_dma - read chip data into buffer
661 * @mtd: MTD device structure
662 * @buf: buffer to store date
663 * @len: number of bytes to read
664 */
665static void fsmc_read_buf_dma(struct mtd_info *mtd, uint8_t *buf, int len)
666{
667 struct fsmc_nand_data *host;
668
669 host = container_of(mtd, struct fsmc_nand_data, mtd);
670 dma_xfer(host, buf, len, DMA_FROM_DEVICE);
671}
672
673/*
674 * fsmc_write_buf_dma - write buffer to chip
675 * @mtd: MTD device structure
676 * @buf: data buffer
677 * @len: number of bytes to write
678 */
679static void fsmc_write_buf_dma(struct mtd_info *mtd, const uint8_t *buf,
680 int len)
681{
682 struct fsmc_nand_data *host;
683
684 host = container_of(mtd, struct fsmc_nand_data, mtd);
685 dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
686}
687
688/*
Linus Walleij6c009ab2010-09-13 00:35:22 +0200689 * fsmc_read_page_hwecc
690 * @mtd: mtd info structure
691 * @chip: nand chip info structure
692 * @buf: buffer to store read data
693 * @page: page number to read
694 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300695 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
Linus Walleij6c009ab2010-09-13 00:35:22 +0200696 * performed in a strict sequence as follows:
697 * data(512 byte) -> ecc(13 byte)
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300698 * After this read, fsmc hardware generates and reports error data bits(up to a
Linus Walleij6c009ab2010-09-13 00:35:22 +0200699 * max of 8 bits)
700 */
701static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
702 uint8_t *buf, int page)
703{
704 struct fsmc_nand_data *host = container_of(mtd,
705 struct fsmc_nand_data, mtd);
706 struct fsmc_eccplace *ecc_place = host->ecc_place;
707 int i, j, s, stat, eccsize = chip->ecc.size;
708 int eccbytes = chip->ecc.bytes;
709 int eccsteps = chip->ecc.steps;
710 uint8_t *p = buf;
711 uint8_t *ecc_calc = chip->buffers->ecccalc;
712 uint8_t *ecc_code = chip->buffers->ecccode;
713 int off, len, group = 0;
714 /*
715 * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
716 * end up reading 14 bytes (7 words) from oob. The local array is
717 * to maintain word alignment
718 */
719 uint16_t ecc_oob[7];
720 uint8_t *oob = (uint8_t *)&ecc_oob[0];
721
722 for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200723 chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
724 chip->ecc.hwctl(mtd, NAND_ECC_READ);
725 chip->read_buf(mtd, p, eccsize);
726
727 for (j = 0; j < eccbytes;) {
728 off = ecc_place->eccplace[group].offset;
729 len = ecc_place->eccplace[group].length;
730 group++;
731
732 /*
Vipin Kumar4cbe1bf02012-03-14 11:47:09 +0530733 * length is intentionally kept a higher multiple of 2
734 * to read at least 13 bytes even in case of 16 bit NAND
735 * devices
736 */
Vipin Kumaraea686b2012-03-14 11:47:10 +0530737 if (chip->options & NAND_BUSWIDTH_16)
738 len = roundup(len, 2);
739
Linus Walleij6c009ab2010-09-13 00:35:22 +0200740 chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
741 chip->read_buf(mtd, oob + j, len);
742 j += len;
743 }
744
Vipin Kumar519300c2012-03-07 17:00:49 +0530745 memcpy(&ecc_code[i], oob, chip->ecc.bytes);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200746 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
747
748 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
749 if (stat < 0)
750 mtd->ecc_stats.failed++;
751 else
752 mtd->ecc_stats.corrected += stat;
753 }
754
755 return 0;
756}
757
758/*
Armando Visconti753e0132012-03-07 17:00:54 +0530759 * fsmc_bch8_correct_data
Linus Walleij6c009ab2010-09-13 00:35:22 +0200760 * @mtd: mtd info structure
761 * @dat: buffer of read data
762 * @read_ecc: ecc read from device spare area
763 * @calc_ecc: ecc calculated from read data
764 *
765 * calc_ecc is a 104 bit information containing maximum of 8 error
766 * offset informations of 13 bits each in 512 bytes of read data.
767 */
Armando Visconti753e0132012-03-07 17:00:54 +0530768static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
Linus Walleij6c009ab2010-09-13 00:35:22 +0200769 uint8_t *read_ecc, uint8_t *calc_ecc)
770{
771 struct fsmc_nand_data *host = container_of(mtd,
772 struct fsmc_nand_data, mtd);
Vipin Kumar519300c2012-03-07 17:00:49 +0530773 struct nand_chip *chip = mtd->priv;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200774 struct fsmc_regs *regs = host->regs_va;
775 unsigned int bank = host->bank;
Armando Viscontia612c2a2012-03-07 17:00:53 +0530776 uint32_t err_idx[8];
Linus Walleij6c009ab2010-09-13 00:35:22 +0200777 uint32_t num_err, i;
Armando Visconti753e0132012-03-07 17:00:54 +0530778 uint32_t ecc1, ecc2, ecc3, ecc4;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200779
Vipin Kumar519300c2012-03-07 17:00:49 +0530780 num_err = (readl(&regs->bank_regs[bank].sts) >> 10) & 0xF;
781
782 /* no bit flipping */
783 if (likely(num_err == 0))
784 return 0;
785
786 /* too many errors */
787 if (unlikely(num_err > 8)) {
788 /*
789 * This is a temporary erase check. A newly erased page read
790 * would result in an ecc error because the oob data is also
791 * erased to FF and the calculated ecc for an FF data is not
792 * FF..FF.
793 * This is a workaround to skip performing correction in case
794 * data is FF..FF
795 *
796 * Logic:
797 * For every page, each bit written as 0 is counted until these
798 * number of bits are greater than 8 (the maximum correction
799 * capability of FSMC for each 512 + 13 bytes)
800 */
801
802 int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
803 int bits_data = count_written_bits(dat, chip->ecc.size, 8);
804
805 if ((bits_ecc + bits_data) <= 8) {
806 if (bits_data)
807 memset(dat, 0xff, chip->ecc.size);
808 return bits_data;
809 }
810
811 return -EBADMSG;
812 }
813
Linus Walleij6c009ab2010-09-13 00:35:22 +0200814 /*
815 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
816 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
817 *
818 * calc_ecc is a 104 bit information containing maximum of 8 error
819 * offset informations of 13 bits each. calc_ecc is copied into a
820 * uint64_t array and error offset indexes are populated in err_idx
821 * array
822 */
Armando Visconti753e0132012-03-07 17:00:54 +0530823 ecc1 = readl(&regs->bank_regs[bank].ecc1);
824 ecc2 = readl(&regs->bank_regs[bank].ecc2);
825 ecc3 = readl(&regs->bank_regs[bank].ecc3);
826 ecc4 = readl(&regs->bank_regs[bank].sts);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200827
Armando Visconti753e0132012-03-07 17:00:54 +0530828 err_idx[0] = (ecc1 >> 0) & 0x1FFF;
829 err_idx[1] = (ecc1 >> 13) & 0x1FFF;
830 err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
831 err_idx[3] = (ecc2 >> 7) & 0x1FFF;
832 err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
833 err_idx[5] = (ecc3 >> 1) & 0x1FFF;
834 err_idx[6] = (ecc3 >> 14) & 0x1FFF;
835 err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200836
837 i = 0;
838 while (num_err--) {
839 change_bit(0, (unsigned long *)&err_idx[i]);
840 change_bit(1, (unsigned long *)&err_idx[i]);
841
Vipin Kumarb533f8d2012-03-14 11:47:11 +0530842 if (err_idx[i] < chip->ecc.size * 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200843 change_bit(err_idx[i], (unsigned long *)dat);
844 i++;
845 }
846 }
847 return i;
848}
849
Vipin Kumar4774fb02012-03-14 11:47:18 +0530850static bool filter(struct dma_chan *chan, void *slave)
851{
852 chan->private = slave;
853 return true;
854}
855
Linus Walleij6c009ab2010-09-13 00:35:22 +0200856/*
857 * fsmc_nand_probe - Probe function
858 * @pdev: platform device structure
859 */
860static int __init fsmc_nand_probe(struct platform_device *pdev)
861{
862 struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
863 struct fsmc_nand_data *host;
864 struct mtd_info *mtd;
865 struct nand_chip *nand;
866 struct fsmc_regs *regs;
867 struct resource *res;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530868 dma_cap_mask_t mask;
Linus Walleij4ad916b2010-11-29 13:52:06 +0100869 int ret = 0;
Linus Walleij593cd872010-11-29 13:52:19 +0100870 u32 pid;
871 int i;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200872
873 if (!pdata) {
874 dev_err(&pdev->dev, "platform data is NULL\n");
875 return -EINVAL;
876 }
877
878 /* Allocate memory for the device structure (and zero it) */
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530879 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200880 if (!host) {
881 dev_err(&pdev->dev, "failed to allocate device structure\n");
882 return -ENOMEM;
883 }
884
885 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530886 if (!res)
887 return -EINVAL;
888
889 if (!devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
890 pdev->name)) {
891 dev_err(&pdev->dev, "Failed to get memory data resourse\n");
892 return -ENOENT;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200893 }
894
Vipin Kumar4774fb02012-03-14 11:47:18 +0530895 host->data_pa = (dma_addr_t)res->start;
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530896 host->data_va = devm_ioremap(&pdev->dev, res->start,
897 resource_size(res));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200898 if (!host->data_va) {
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530899 dev_err(&pdev->dev, "data ioremap failed\n");
900 return -ENOMEM;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200901 }
902
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530903 if (!devm_request_mem_region(&pdev->dev, res->start + pdata->ale_off,
904 resource_size(res), pdev->name)) {
905 dev_err(&pdev->dev, "Failed to get memory ale resourse\n");
906 return -ENOENT;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200907 }
908
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530909 host->addr_va = devm_ioremap(&pdev->dev, res->start + pdata->ale_off,
Shiraz Hashimb2acc922012-03-07 17:00:51 +0530910 resource_size(res));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200911 if (!host->addr_va) {
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530912 dev_err(&pdev->dev, "ale ioremap failed\n");
913 return -ENOMEM;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200914 }
915
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530916 if (!devm_request_mem_region(&pdev->dev, res->start + pdata->cle_off,
917 resource_size(res), pdev->name)) {
918 dev_err(&pdev->dev, "Failed to get memory cle resourse\n");
919 return -ENOENT;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200920 }
921
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530922 host->cmd_va = devm_ioremap(&pdev->dev, res->start + pdata->cle_off,
923 resource_size(res));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200924 if (!host->cmd_va) {
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530925 dev_err(&pdev->dev, "ale ioremap failed\n");
926 return -ENOMEM;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200927 }
928
929 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530930 if (!res)
931 return -EINVAL;
932
933 if (!devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
934 pdev->name)) {
935 dev_err(&pdev->dev, "Failed to get memory regs resourse\n");
936 return -ENOENT;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200937 }
938
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530939 host->regs_va = devm_ioremap(&pdev->dev, res->start,
940 resource_size(res));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200941 if (!host->regs_va) {
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530942 dev_err(&pdev->dev, "regs ioremap failed\n");
943 return -ENOMEM;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200944 }
945
946 host->clk = clk_get(&pdev->dev, NULL);
947 if (IS_ERR(host->clk)) {
948 dev_err(&pdev->dev, "failed to fetch block clock\n");
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530949 return PTR_ERR(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200950 }
951
952 ret = clk_enable(host->clk);
953 if (ret)
Vipin Kumar82b9dbe2012-03-14 11:47:15 +0530954 goto err_clk_enable;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200955
Linus Walleij593cd872010-11-29 13:52:19 +0100956 /*
957 * This device ID is actually a common AMBA ID as used on the
958 * AMBA PrimeCell bus. However it is not a PrimeCell.
959 */
960 for (pid = 0, i = 0; i < 4; i++)
961 pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
962 host->pid = pid;
963 dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
964 "revision %02x, config %02x\n",
965 AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
966 AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
967
Linus Walleij6c009ab2010-09-13 00:35:22 +0200968 host->bank = pdata->bank;
969 host->select_chip = pdata->select_bank;
Vipin Kumar71470322012-03-14 11:47:07 +0530970 host->partitions = pdata->partitions;
971 host->nr_partitions = pdata->nr_partitions;
Vipin Kumar712c4ad2012-03-14 11:47:16 +0530972 host->dev = &pdev->dev;
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530973 host->dev_timings = pdata->nand_timings;
Vipin Kumar4774fb02012-03-14 11:47:18 +0530974 host->mode = pdata->mode;
975
976 if (host->mode == USE_DMA_ACCESS)
977 init_completion(&host->dma_access_complete);
978
Linus Walleij6c009ab2010-09-13 00:35:22 +0200979 regs = host->regs_va;
980
981 /* Link all private pointers */
982 mtd = &host->mtd;
983 nand = &host->nand;
984 mtd->priv = nand;
985 nand->priv = host;
986
987 host->mtd.owner = THIS_MODULE;
988 nand->IO_ADDR_R = host->data_va;
989 nand->IO_ADDR_W = host->data_va;
990 nand->cmd_ctrl = fsmc_cmd_ctrl;
991 nand->chip_delay = 30;
992
993 nand->ecc.mode = NAND_ECC_HW;
994 nand->ecc.hwctl = fsmc_enable_hwecc;
995 nand->ecc.size = 512;
996 nand->options = pdata->options;
997 nand->select_chip = fsmc_select_chip;
Vipin Kumar467e6e72012-03-14 11:47:12 +0530998 nand->badblockbits = 7;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200999
1000 if (pdata->width == FSMC_NAND_BW16)
1001 nand->options |= NAND_BUSWIDTH_16;
1002
Vipin Kumar4774fb02012-03-14 11:47:18 +05301003 switch (host->mode) {
1004 case USE_DMA_ACCESS:
1005 dma_cap_zero(mask);
1006 dma_cap_set(DMA_MEMCPY, mask);
1007 host->read_dma_chan = dma_request_channel(mask, filter,
1008 pdata->read_dma_priv);
1009 if (!host->read_dma_chan) {
1010 dev_err(&pdev->dev, "Unable to get read dma channel\n");
1011 goto err_req_read_chnl;
1012 }
1013 host->write_dma_chan = dma_request_channel(mask, filter,
1014 pdata->write_dma_priv);
1015 if (!host->write_dma_chan) {
1016 dev_err(&pdev->dev, "Unable to get write dma channel\n");
1017 goto err_req_write_chnl;
1018 }
1019 nand->read_buf = fsmc_read_buf_dma;
1020 nand->write_buf = fsmc_write_buf_dma;
1021 break;
1022
1023 default:
1024 case USE_WORD_ACCESS:
Vipin Kumar604e7542012-03-14 11:47:17 +05301025 nand->read_buf = fsmc_read_buf;
1026 nand->write_buf = fsmc_write_buf;
Vipin Kumar4774fb02012-03-14 11:47:18 +05301027 break;
Vipin Kumar604e7542012-03-14 11:47:17 +05301028 }
1029
Vipin Kumare2f6bce2012-03-14 11:47:14 +05301030 fsmc_nand_setup(regs, host->bank, nand->options & NAND_BUSWIDTH_16,
1031 host->dev_timings);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001032
Linus Walleij593cd872010-11-29 13:52:19 +01001033 if (AMBA_REV_BITS(host->pid) >= 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +02001034 nand->ecc.read_page = fsmc_read_page_hwecc;
1035 nand->ecc.calculate = fsmc_read_hwecc_ecc4;
Armando Visconti753e0132012-03-07 17:00:54 +05301036 nand->ecc.correct = fsmc_bch8_correct_data;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001037 nand->ecc.bytes = 13;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001038 nand->ecc.strength = 8;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001039 } else {
1040 nand->ecc.calculate = fsmc_read_hwecc_ecc1;
1041 nand->ecc.correct = nand_correct_data;
1042 nand->ecc.bytes = 3;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001043 nand->ecc.strength = 1;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001044 }
1045
1046 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001047 * Scan to find existence of the device
Linus Walleij6c009ab2010-09-13 00:35:22 +02001048 */
1049 if (nand_scan_ident(&host->mtd, 1, NULL)) {
1050 ret = -ENXIO;
1051 dev_err(&pdev->dev, "No NAND Device found!\n");
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301052 goto err_scan_ident;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001053 }
1054
Linus Walleij593cd872010-11-29 13:52:19 +01001055 if (AMBA_REV_BITS(host->pid) >= 8) {
Bhavna Yadave29ee572012-03-07 17:00:50 +05301056 switch (host->mtd.oobsize) {
1057 case 16:
1058 nand->ecc.layout = &fsmc_ecc4_16_layout;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001059 host->ecc_place = &fsmc_ecc4_sp_place;
Bhavna Yadave29ee572012-03-07 17:00:50 +05301060 break;
1061 case 64:
1062 nand->ecc.layout = &fsmc_ecc4_64_layout;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001063 host->ecc_place = &fsmc_ecc4_lp_place;
Bhavna Yadave29ee572012-03-07 17:00:50 +05301064 break;
1065 case 128:
1066 nand->ecc.layout = &fsmc_ecc4_128_layout;
1067 host->ecc_place = &fsmc_ecc4_lp_place;
1068 break;
Armando Visconti0c78e932012-03-07 17:00:55 +05301069 case 224:
1070 nand->ecc.layout = &fsmc_ecc4_224_layout;
1071 host->ecc_place = &fsmc_ecc4_lp_place;
1072 break;
Bhavna Yadave29ee572012-03-07 17:00:50 +05301073 case 256:
1074 nand->ecc.layout = &fsmc_ecc4_256_layout;
1075 host->ecc_place = &fsmc_ecc4_lp_place;
1076 break;
1077 default:
1078 printk(KERN_WARNING "No oob scheme defined for "
1079 "oobsize %d\n", mtd->oobsize);
1080 BUG();
Linus Walleij6c009ab2010-09-13 00:35:22 +02001081 }
1082 } else {
Bhavna Yadave29ee572012-03-07 17:00:50 +05301083 switch (host->mtd.oobsize) {
1084 case 16:
1085 nand->ecc.layout = &fsmc_ecc1_16_layout;
1086 break;
1087 case 64:
1088 nand->ecc.layout = &fsmc_ecc1_64_layout;
1089 break;
1090 case 128:
1091 nand->ecc.layout = &fsmc_ecc1_128_layout;
1092 break;
1093 default:
1094 printk(KERN_WARNING "No oob scheme defined for "
1095 "oobsize %d\n", mtd->oobsize);
1096 BUG();
1097 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001098 }
1099
1100 /* Second stage of scan to fill MTD data-structures */
1101 if (nand_scan_tail(&host->mtd)) {
1102 ret = -ENXIO;
1103 goto err_probe;
1104 }
1105
1106 /*
1107 * The partition information can is accessed by (in the same precedence)
1108 *
1109 * command line through Bootloader,
1110 * platform data,
1111 * default partition information present in driver.
1112 */
Linus Walleij6c009ab2010-09-13 00:35:22 +02001113 /*
Dmitry Eremin-Solenikov8d3f8bb2011-05-29 20:16:57 +04001114 * Check for partition info passed
Linus Walleij6c009ab2010-09-13 00:35:22 +02001115 */
1116 host->mtd.name = "nand";
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001117 ret = mtd_device_parse_register(&host->mtd, NULL, NULL,
Vipin Kumar71470322012-03-14 11:47:07 +05301118 host->partitions, host->nr_partitions);
Jamie Iles99335d02011-05-23 10:23:23 +01001119 if (ret)
Linus Walleij6c009ab2010-09-13 00:35:22 +02001120 goto err_probe;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001121
1122 platform_set_drvdata(pdev, host);
1123 dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
1124 return 0;
1125
1126err_probe:
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301127err_scan_ident:
Vipin Kumar4774fb02012-03-14 11:47:18 +05301128 if (host->mode == USE_DMA_ACCESS)
1129 dma_release_channel(host->write_dma_chan);
1130err_req_write_chnl:
1131 if (host->mode == USE_DMA_ACCESS)
1132 dma_release_channel(host->read_dma_chan);
1133err_req_read_chnl:
Linus Walleij6c009ab2010-09-13 00:35:22 +02001134 clk_disable(host->clk);
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301135err_clk_enable:
1136 clk_put(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001137 return ret;
1138}
1139
1140/*
1141 * Clean up routine
1142 */
1143static int fsmc_nand_remove(struct platform_device *pdev)
1144{
1145 struct fsmc_nand_data *host = platform_get_drvdata(pdev);
1146
1147 platform_set_drvdata(pdev, NULL);
1148
1149 if (host) {
Axel Lin82e023a2011-06-03 13:15:30 +08001150 nand_release(&host->mtd);
Vipin Kumar4774fb02012-03-14 11:47:18 +05301151
1152 if (host->mode == USE_DMA_ACCESS) {
1153 dma_release_channel(host->write_dma_chan);
1154 dma_release_channel(host->read_dma_chan);
1155 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001156 clk_disable(host->clk);
1157 clk_put(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001158 }
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301159
Linus Walleij6c009ab2010-09-13 00:35:22 +02001160 return 0;
1161}
1162
1163#ifdef CONFIG_PM
1164static int fsmc_nand_suspend(struct device *dev)
1165{
1166 struct fsmc_nand_data *host = dev_get_drvdata(dev);
1167 if (host)
1168 clk_disable(host->clk);
1169 return 0;
1170}
1171
1172static int fsmc_nand_resume(struct device *dev)
1173{
1174 struct fsmc_nand_data *host = dev_get_drvdata(dev);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301175 if (host) {
Linus Walleij6c009ab2010-09-13 00:35:22 +02001176 clk_enable(host->clk);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301177 fsmc_nand_setup(host->regs_va, host->bank,
Vipin Kumare2f6bce2012-03-14 11:47:14 +05301178 host->nand.options & NAND_BUSWIDTH_16,
1179 host->dev_timings);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301180 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001181 return 0;
1182}
1183
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301184static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001185#endif
1186
1187static struct platform_driver fsmc_nand_driver = {
1188 .remove = fsmc_nand_remove,
1189 .driver = {
1190 .owner = THIS_MODULE,
1191 .name = "fsmc-nand",
1192#ifdef CONFIG_PM
1193 .pm = &fsmc_nand_pm_ops,
1194#endif
1195 },
1196};
1197
1198static int __init fsmc_nand_init(void)
1199{
1200 return platform_driver_probe(&fsmc_nand_driver,
1201 fsmc_nand_probe);
1202}
1203module_init(fsmc_nand_init);
1204
1205static void __exit fsmc_nand_exit(void)
1206{
1207 platform_driver_unregister(&fsmc_nand_driver);
1208}
1209module_exit(fsmc_nand_exit);
1210
1211MODULE_LICENSE("GPL");
1212MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
1213MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");