blob: 049018213090f4d20dd84a7d0fd38eaaef4ad50f [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);
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530363 void *__iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200364 unsigned int bank = host->bank;
365
366 if (ctrl & NAND_CTRL_CHANGE) {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530367 u32 pc;
368
Linus Walleij6c009ab2010-09-13 00:35:22 +0200369 if (ctrl & NAND_CLE) {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530370 this->IO_ADDR_R = host->cmd_va;
371 this->IO_ADDR_W = host->cmd_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200372 } else if (ctrl & NAND_ALE) {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530373 this->IO_ADDR_R = host->addr_va;
374 this->IO_ADDR_W = host->addr_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200375 } else {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530376 this->IO_ADDR_R = host->data_va;
377 this->IO_ADDR_W = host->data_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200378 }
379
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530380 pc = readl(FSMC_NAND_REG(regs, bank, PC));
381 if (ctrl & NAND_NCE)
382 pc |= FSMC_ENABLE;
383 else
384 pc &= ~FSMC_ENABLE;
385 writel(pc, FSMC_NAND_REG(regs, bank, PC));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200386 }
387
388 mb();
389
390 if (cmd != NAND_CMD_NONE)
391 writeb(cmd, this->IO_ADDR_W);
392}
393
394/*
395 * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
396 *
397 * This routine initializes timing parameters related to NAND memory access in
398 * FSMC registers
399 */
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530400static void fsmc_nand_setup(void __iomem *regs, uint32_t bank,
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530401 uint32_t busw, struct fsmc_nand_timings *timings)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200402{
403 uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530404 uint32_t tclr, tar, thiz, thold, twait, tset;
405 struct fsmc_nand_timings *tims;
406 struct fsmc_nand_timings default_timings = {
407 .tclr = FSMC_TCLR_1,
408 .tar = FSMC_TAR_1,
409 .thiz = FSMC_THIZ_1,
410 .thold = FSMC_THOLD_4,
411 .twait = FSMC_TWAIT_6,
412 .tset = FSMC_TSET_0,
413 };
414
415 if (timings)
416 tims = timings;
417 else
418 tims = &default_timings;
419
420 tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT;
421 tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT;
422 thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT;
423 thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT;
424 twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT;
425 tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200426
427 if (busw)
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530428 writel(value | FSMC_DEVWID_16, FSMC_NAND_REG(regs, bank, PC));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200429 else
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530430 writel(value | FSMC_DEVWID_8, FSMC_NAND_REG(regs, bank, PC));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200431
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530432 writel(readl(FSMC_NAND_REG(regs, bank, PC)) | tclr | tar,
433 FSMC_NAND_REG(regs, bank, PC));
434 writel(thiz | thold | twait | tset, FSMC_NAND_REG(regs, bank, COMM));
435 writel(thiz | thold | twait | tset, FSMC_NAND_REG(regs, bank, ATTRIB));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200436}
437
438/*
439 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
440 */
441static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
442{
443 struct fsmc_nand_data *host = container_of(mtd,
444 struct fsmc_nand_data, mtd);
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530445 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200446 uint32_t bank = host->bank;
447
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530448 writel(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCPLEN_256,
449 FSMC_NAND_REG(regs, bank, PC));
450 writel(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCEN,
451 FSMC_NAND_REG(regs, bank, PC));
452 writel(readl(FSMC_NAND_REG(regs, bank, PC)) | FSMC_ECCEN,
453 FSMC_NAND_REG(regs, bank, PC));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200454}
455
456/*
457 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300458 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200459 * max of 8-bits)
460 */
461static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
462 uint8_t *ecc)
463{
464 struct fsmc_nand_data *host = container_of(mtd,
465 struct fsmc_nand_data, mtd);
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530466 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200467 uint32_t bank = host->bank;
468 uint32_t ecc_tmp;
469 unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
470
471 do {
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530472 if (readl(FSMC_NAND_REG(regs, bank, STS)) & FSMC_CODE_RDY)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200473 break;
474 else
475 cond_resched();
476 } while (!time_after_eq(jiffies, deadline));
477
Vipin Kumar712c4ad2012-03-14 11:47:16 +0530478 if (time_after_eq(jiffies, deadline)) {
479 dev_err(host->dev, "calculate ecc timed out\n");
480 return -ETIMEDOUT;
481 }
482
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530483 ecc_tmp = readl(FSMC_NAND_REG(regs, bank, ECC1));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200484 ecc[0] = (uint8_t) (ecc_tmp >> 0);
485 ecc[1] = (uint8_t) (ecc_tmp >> 8);
486 ecc[2] = (uint8_t) (ecc_tmp >> 16);
487 ecc[3] = (uint8_t) (ecc_tmp >> 24);
488
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530489 ecc_tmp = readl(FSMC_NAND_REG(regs, bank, ECC2));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200490 ecc[4] = (uint8_t) (ecc_tmp >> 0);
491 ecc[5] = (uint8_t) (ecc_tmp >> 8);
492 ecc[6] = (uint8_t) (ecc_tmp >> 16);
493 ecc[7] = (uint8_t) (ecc_tmp >> 24);
494
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530495 ecc_tmp = readl(FSMC_NAND_REG(regs, bank, ECC3));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200496 ecc[8] = (uint8_t) (ecc_tmp >> 0);
497 ecc[9] = (uint8_t) (ecc_tmp >> 8);
498 ecc[10] = (uint8_t) (ecc_tmp >> 16);
499 ecc[11] = (uint8_t) (ecc_tmp >> 24);
500
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530501 ecc_tmp = readl(FSMC_NAND_REG(regs, bank, STS));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200502 ecc[12] = (uint8_t) (ecc_tmp >> 16);
503
504 return 0;
505}
506
507/*
508 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300509 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200510 * max of 1-bit)
511 */
512static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
513 uint8_t *ecc)
514{
515 struct fsmc_nand_data *host = container_of(mtd,
516 struct fsmc_nand_data, mtd);
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530517 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200518 uint32_t bank = host->bank;
519 uint32_t ecc_tmp;
520
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530521 ecc_tmp = readl(FSMC_NAND_REG(regs, bank, ECC1));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200522 ecc[0] = (uint8_t) (ecc_tmp >> 0);
523 ecc[1] = (uint8_t) (ecc_tmp >> 8);
524 ecc[2] = (uint8_t) (ecc_tmp >> 16);
525
526 return 0;
527}
528
Vipin Kumar519300c2012-03-07 17:00:49 +0530529/* Count the number of 0's in buff upto a max of max_bits */
530static int count_written_bits(uint8_t *buff, int size, int max_bits)
531{
532 int k, written_bits = 0;
533
534 for (k = 0; k < size; k++) {
535 written_bits += hweight8(~buff[k]);
536 if (written_bits > max_bits)
537 break;
538 }
539
540 return written_bits;
541}
542
Vipin Kumar4774fb02012-03-14 11:47:18 +0530543static void dma_complete(void *param)
544{
545 struct fsmc_nand_data *host = param;
546
547 complete(&host->dma_access_complete);
548}
549
550static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len,
551 enum dma_data_direction direction)
552{
553 struct dma_chan *chan;
554 struct dma_device *dma_dev;
555 struct dma_async_tx_descriptor *tx;
556 dma_addr_t dma_dst, dma_src, dma_addr;
557 dma_cookie_t cookie;
558 unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
559 int ret;
560
561 if (direction == DMA_TO_DEVICE)
562 chan = host->write_dma_chan;
563 else if (direction == DMA_FROM_DEVICE)
564 chan = host->read_dma_chan;
565 else
566 return -EINVAL;
567
568 dma_dev = chan->device;
569 dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction);
570
571 if (direction == DMA_TO_DEVICE) {
572 dma_src = dma_addr;
573 dma_dst = host->data_pa;
574 flags |= DMA_COMPL_SRC_UNMAP_SINGLE | DMA_COMPL_SKIP_DEST_UNMAP;
575 } else {
576 dma_src = host->data_pa;
577 dma_dst = dma_addr;
578 flags |= DMA_COMPL_DEST_UNMAP_SINGLE | DMA_COMPL_SKIP_SRC_UNMAP;
579 }
580
581 tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
582 len, flags);
583
584 if (!tx) {
585 dev_err(host->dev, "device_prep_dma_memcpy error\n");
586 dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
587 return -EIO;
588 }
589
590 tx->callback = dma_complete;
591 tx->callback_param = host;
592 cookie = tx->tx_submit(tx);
593
594 ret = dma_submit_error(cookie);
595 if (ret) {
596 dev_err(host->dev, "dma_submit_error %d\n", cookie);
597 return ret;
598 }
599
600 dma_async_issue_pending(chan);
601
602 ret =
603 wait_for_completion_interruptible_timeout(&host->dma_access_complete,
604 msecs_to_jiffies(3000));
605 if (ret <= 0) {
606 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
607 dev_err(host->dev, "wait_for_completion_timeout\n");
608 return ret ? ret : -ETIMEDOUT;
609 }
610
611 return 0;
612}
613
Linus Walleij6c009ab2010-09-13 00:35:22 +0200614/*
Vipin Kumar604e7542012-03-14 11:47:17 +0530615 * fsmc_write_buf - write buffer to chip
616 * @mtd: MTD device structure
617 * @buf: data buffer
618 * @len: number of bytes to write
619 */
620static void fsmc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
621{
622 int i;
623 struct nand_chip *chip = mtd->priv;
624
625 if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
626 IS_ALIGNED(len, sizeof(uint32_t))) {
627 uint32_t *p = (uint32_t *)buf;
628 len = len >> 2;
629 for (i = 0; i < len; i++)
630 writel(p[i], chip->IO_ADDR_W);
631 } else {
632 for (i = 0; i < len; i++)
633 writeb(buf[i], chip->IO_ADDR_W);
634 }
635}
636
637/*
638 * fsmc_read_buf - read chip data into buffer
639 * @mtd: MTD device structure
640 * @buf: buffer to store date
641 * @len: number of bytes to read
642 */
643static void fsmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
644{
645 int i;
646 struct nand_chip *chip = mtd->priv;
647
648 if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
649 IS_ALIGNED(len, sizeof(uint32_t))) {
650 uint32_t *p = (uint32_t *)buf;
651 len = len >> 2;
652 for (i = 0; i < len; i++)
653 p[i] = readl(chip->IO_ADDR_R);
654 } else {
655 for (i = 0; i < len; i++)
656 buf[i] = readb(chip->IO_ADDR_R);
657 }
658}
659
660/*
Vipin Kumar4774fb02012-03-14 11:47:18 +0530661 * fsmc_read_buf_dma - read chip data into buffer
662 * @mtd: MTD device structure
663 * @buf: buffer to store date
664 * @len: number of bytes to read
665 */
666static void fsmc_read_buf_dma(struct mtd_info *mtd, uint8_t *buf, int len)
667{
668 struct fsmc_nand_data *host;
669
670 host = container_of(mtd, struct fsmc_nand_data, mtd);
671 dma_xfer(host, buf, len, DMA_FROM_DEVICE);
672}
673
674/*
675 * fsmc_write_buf_dma - write buffer to chip
676 * @mtd: MTD device structure
677 * @buf: data buffer
678 * @len: number of bytes to write
679 */
680static void fsmc_write_buf_dma(struct mtd_info *mtd, const uint8_t *buf,
681 int len)
682{
683 struct fsmc_nand_data *host;
684
685 host = container_of(mtd, struct fsmc_nand_data, mtd);
686 dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
687}
688
689/*
Linus Walleij6c009ab2010-09-13 00:35:22 +0200690 * fsmc_read_page_hwecc
691 * @mtd: mtd info structure
692 * @chip: nand chip info structure
693 * @buf: buffer to store read data
694 * @page: page number to read
695 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300696 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
Linus Walleij6c009ab2010-09-13 00:35:22 +0200697 * performed in a strict sequence as follows:
698 * data(512 byte) -> ecc(13 byte)
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300699 * After this read, fsmc hardware generates and reports error data bits(up to a
Linus Walleij6c009ab2010-09-13 00:35:22 +0200700 * max of 8 bits)
701 */
702static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
703 uint8_t *buf, int page)
704{
705 struct fsmc_nand_data *host = container_of(mtd,
706 struct fsmc_nand_data, mtd);
707 struct fsmc_eccplace *ecc_place = host->ecc_place;
708 int i, j, s, stat, eccsize = chip->ecc.size;
709 int eccbytes = chip->ecc.bytes;
710 int eccsteps = chip->ecc.steps;
711 uint8_t *p = buf;
712 uint8_t *ecc_calc = chip->buffers->ecccalc;
713 uint8_t *ecc_code = chip->buffers->ecccode;
714 int off, len, group = 0;
715 /*
716 * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
717 * end up reading 14 bytes (7 words) from oob. The local array is
718 * to maintain word alignment
719 */
720 uint16_t ecc_oob[7];
721 uint8_t *oob = (uint8_t *)&ecc_oob[0];
722
723 for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200724 chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
725 chip->ecc.hwctl(mtd, NAND_ECC_READ);
726 chip->read_buf(mtd, p, eccsize);
727
728 for (j = 0; j < eccbytes;) {
729 off = ecc_place->eccplace[group].offset;
730 len = ecc_place->eccplace[group].length;
731 group++;
732
733 /*
Vipin Kumar4cbe1bf02012-03-14 11:47:09 +0530734 * length is intentionally kept a higher multiple of 2
735 * to read at least 13 bytes even in case of 16 bit NAND
736 * devices
737 */
Vipin Kumaraea686b2012-03-14 11:47:10 +0530738 if (chip->options & NAND_BUSWIDTH_16)
739 len = roundup(len, 2);
740
Linus Walleij6c009ab2010-09-13 00:35:22 +0200741 chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
742 chip->read_buf(mtd, oob + j, len);
743 j += len;
744 }
745
Vipin Kumar519300c2012-03-07 17:00:49 +0530746 memcpy(&ecc_code[i], oob, chip->ecc.bytes);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200747 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
748
749 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
750 if (stat < 0)
751 mtd->ecc_stats.failed++;
752 else
753 mtd->ecc_stats.corrected += stat;
754 }
755
756 return 0;
757}
758
759/*
Armando Visconti753e0132012-03-07 17:00:54 +0530760 * fsmc_bch8_correct_data
Linus Walleij6c009ab2010-09-13 00:35:22 +0200761 * @mtd: mtd info structure
762 * @dat: buffer of read data
763 * @read_ecc: ecc read from device spare area
764 * @calc_ecc: ecc calculated from read data
765 *
766 * calc_ecc is a 104 bit information containing maximum of 8 error
767 * offset informations of 13 bits each in 512 bytes of read data.
768 */
Armando Visconti753e0132012-03-07 17:00:54 +0530769static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
Linus Walleij6c009ab2010-09-13 00:35:22 +0200770 uint8_t *read_ecc, uint8_t *calc_ecc)
771{
772 struct fsmc_nand_data *host = container_of(mtd,
773 struct fsmc_nand_data, mtd);
Vipin Kumar519300c2012-03-07 17:00:49 +0530774 struct nand_chip *chip = mtd->priv;
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530775 void __iomem *regs = host->regs_va;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200776 unsigned int bank = host->bank;
Armando Viscontia612c2a2012-03-07 17:00:53 +0530777 uint32_t err_idx[8];
Linus Walleij6c009ab2010-09-13 00:35:22 +0200778 uint32_t num_err, i;
Armando Visconti753e0132012-03-07 17:00:54 +0530779 uint32_t ecc1, ecc2, ecc3, ecc4;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200780
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530781 num_err = (readl(FSMC_NAND_REG(regs, bank, STS)) >> 10) & 0xF;
Vipin Kumar519300c2012-03-07 17:00:49 +0530782
783 /* no bit flipping */
784 if (likely(num_err == 0))
785 return 0;
786
787 /* too many errors */
788 if (unlikely(num_err > 8)) {
789 /*
790 * This is a temporary erase check. A newly erased page read
791 * would result in an ecc error because the oob data is also
792 * erased to FF and the calculated ecc for an FF data is not
793 * FF..FF.
794 * This is a workaround to skip performing correction in case
795 * data is FF..FF
796 *
797 * Logic:
798 * For every page, each bit written as 0 is counted until these
799 * number of bits are greater than 8 (the maximum correction
800 * capability of FSMC for each 512 + 13 bytes)
801 */
802
803 int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
804 int bits_data = count_written_bits(dat, chip->ecc.size, 8);
805
806 if ((bits_ecc + bits_data) <= 8) {
807 if (bits_data)
808 memset(dat, 0xff, chip->ecc.size);
809 return bits_data;
810 }
811
812 return -EBADMSG;
813 }
814
Linus Walleij6c009ab2010-09-13 00:35:22 +0200815 /*
816 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
817 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
818 *
819 * calc_ecc is a 104 bit information containing maximum of 8 error
820 * offset informations of 13 bits each. calc_ecc is copied into a
821 * uint64_t array and error offset indexes are populated in err_idx
822 * array
823 */
Vipin Kumar2a5dbead2012-03-14 11:47:19 +0530824 ecc1 = readl(FSMC_NAND_REG(regs, bank, ECC1));
825 ecc2 = readl(FSMC_NAND_REG(regs, bank, ECC2));
826 ecc3 = readl(FSMC_NAND_REG(regs, bank, ECC3));
827 ecc4 = readl(FSMC_NAND_REG(regs, bank, STS));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200828
Armando Visconti753e0132012-03-07 17:00:54 +0530829 err_idx[0] = (ecc1 >> 0) & 0x1FFF;
830 err_idx[1] = (ecc1 >> 13) & 0x1FFF;
831 err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
832 err_idx[3] = (ecc2 >> 7) & 0x1FFF;
833 err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
834 err_idx[5] = (ecc3 >> 1) & 0x1FFF;
835 err_idx[6] = (ecc3 >> 14) & 0x1FFF;
836 err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200837
838 i = 0;
839 while (num_err--) {
840 change_bit(0, (unsigned long *)&err_idx[i]);
841 change_bit(1, (unsigned long *)&err_idx[i]);
842
Vipin Kumarb533f8d2012-03-14 11:47:11 +0530843 if (err_idx[i] < chip->ecc.size * 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200844 change_bit(err_idx[i], (unsigned long *)dat);
845 i++;
846 }
847 }
848 return i;
849}
850
Vipin Kumar4774fb02012-03-14 11:47:18 +0530851static bool filter(struct dma_chan *chan, void *slave)
852{
853 chan->private = slave;
854 return true;
855}
856
Linus Walleij6c009ab2010-09-13 00:35:22 +0200857/*
858 * fsmc_nand_probe - Probe function
859 * @pdev: platform device structure
860 */
861static int __init fsmc_nand_probe(struct platform_device *pdev)
862{
863 struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
864 struct fsmc_nand_data *host;
865 struct mtd_info *mtd;
866 struct nand_chip *nand;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200867 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 /* Link all private pointers */
980 mtd = &host->mtd;
981 nand = &host->nand;
982 mtd->priv = nand;
983 nand->priv = host;
984
985 host->mtd.owner = THIS_MODULE;
986 nand->IO_ADDR_R = host->data_va;
987 nand->IO_ADDR_W = host->data_va;
988 nand->cmd_ctrl = fsmc_cmd_ctrl;
989 nand->chip_delay = 30;
990
991 nand->ecc.mode = NAND_ECC_HW;
992 nand->ecc.hwctl = fsmc_enable_hwecc;
993 nand->ecc.size = 512;
994 nand->options = pdata->options;
995 nand->select_chip = fsmc_select_chip;
Vipin Kumar467e6e72012-03-14 11:47:12 +0530996 nand->badblockbits = 7;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200997
998 if (pdata->width == FSMC_NAND_BW16)
999 nand->options |= NAND_BUSWIDTH_16;
1000
Vipin Kumar4774fb02012-03-14 11:47:18 +05301001 switch (host->mode) {
1002 case USE_DMA_ACCESS:
1003 dma_cap_zero(mask);
1004 dma_cap_set(DMA_MEMCPY, mask);
1005 host->read_dma_chan = dma_request_channel(mask, filter,
1006 pdata->read_dma_priv);
1007 if (!host->read_dma_chan) {
1008 dev_err(&pdev->dev, "Unable to get read dma channel\n");
1009 goto err_req_read_chnl;
1010 }
1011 host->write_dma_chan = dma_request_channel(mask, filter,
1012 pdata->write_dma_priv);
1013 if (!host->write_dma_chan) {
1014 dev_err(&pdev->dev, "Unable to get write dma channel\n");
1015 goto err_req_write_chnl;
1016 }
1017 nand->read_buf = fsmc_read_buf_dma;
1018 nand->write_buf = fsmc_write_buf_dma;
1019 break;
1020
1021 default:
1022 case USE_WORD_ACCESS:
Vipin Kumar604e7542012-03-14 11:47:17 +05301023 nand->read_buf = fsmc_read_buf;
1024 nand->write_buf = fsmc_write_buf;
Vipin Kumar4774fb02012-03-14 11:47:18 +05301025 break;
Vipin Kumar604e7542012-03-14 11:47:17 +05301026 }
1027
Vipin Kumar2a5dbead2012-03-14 11:47:19 +05301028 fsmc_nand_setup(host->regs_va, host->bank,
1029 nand->options & NAND_BUSWIDTH_16,
Vipin Kumare2f6bce2012-03-14 11:47:14 +05301030 host->dev_timings);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001031
Linus Walleij593cd872010-11-29 13:52:19 +01001032 if (AMBA_REV_BITS(host->pid) >= 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +02001033 nand->ecc.read_page = fsmc_read_page_hwecc;
1034 nand->ecc.calculate = fsmc_read_hwecc_ecc4;
Armando Visconti753e0132012-03-07 17:00:54 +05301035 nand->ecc.correct = fsmc_bch8_correct_data;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001036 nand->ecc.bytes = 13;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001037 nand->ecc.strength = 8;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001038 } else {
1039 nand->ecc.calculate = fsmc_read_hwecc_ecc1;
1040 nand->ecc.correct = nand_correct_data;
1041 nand->ecc.bytes = 3;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001042 nand->ecc.strength = 1;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001043 }
1044
1045 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001046 * Scan to find existence of the device
Linus Walleij6c009ab2010-09-13 00:35:22 +02001047 */
1048 if (nand_scan_ident(&host->mtd, 1, NULL)) {
1049 ret = -ENXIO;
1050 dev_err(&pdev->dev, "No NAND Device found!\n");
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301051 goto err_scan_ident;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001052 }
1053
Linus Walleij593cd872010-11-29 13:52:19 +01001054 if (AMBA_REV_BITS(host->pid) >= 8) {
Bhavna Yadave29ee572012-03-07 17:00:50 +05301055 switch (host->mtd.oobsize) {
1056 case 16:
1057 nand->ecc.layout = &fsmc_ecc4_16_layout;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001058 host->ecc_place = &fsmc_ecc4_sp_place;
Bhavna Yadave29ee572012-03-07 17:00:50 +05301059 break;
1060 case 64:
1061 nand->ecc.layout = &fsmc_ecc4_64_layout;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001062 host->ecc_place = &fsmc_ecc4_lp_place;
Bhavna Yadave29ee572012-03-07 17:00:50 +05301063 break;
1064 case 128:
1065 nand->ecc.layout = &fsmc_ecc4_128_layout;
1066 host->ecc_place = &fsmc_ecc4_lp_place;
1067 break;
Armando Visconti0c78e932012-03-07 17:00:55 +05301068 case 224:
1069 nand->ecc.layout = &fsmc_ecc4_224_layout;
1070 host->ecc_place = &fsmc_ecc4_lp_place;
1071 break;
Bhavna Yadave29ee572012-03-07 17:00:50 +05301072 case 256:
1073 nand->ecc.layout = &fsmc_ecc4_256_layout;
1074 host->ecc_place = &fsmc_ecc4_lp_place;
1075 break;
1076 default:
1077 printk(KERN_WARNING "No oob scheme defined for "
1078 "oobsize %d\n", mtd->oobsize);
1079 BUG();
Linus Walleij6c009ab2010-09-13 00:35:22 +02001080 }
1081 } else {
Bhavna Yadave29ee572012-03-07 17:00:50 +05301082 switch (host->mtd.oobsize) {
1083 case 16:
1084 nand->ecc.layout = &fsmc_ecc1_16_layout;
1085 break;
1086 case 64:
1087 nand->ecc.layout = &fsmc_ecc1_64_layout;
1088 break;
1089 case 128:
1090 nand->ecc.layout = &fsmc_ecc1_128_layout;
1091 break;
1092 default:
1093 printk(KERN_WARNING "No oob scheme defined for "
1094 "oobsize %d\n", mtd->oobsize);
1095 BUG();
1096 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001097 }
1098
1099 /* Second stage of scan to fill MTD data-structures */
1100 if (nand_scan_tail(&host->mtd)) {
1101 ret = -ENXIO;
1102 goto err_probe;
1103 }
1104
1105 /*
1106 * The partition information can is accessed by (in the same precedence)
1107 *
1108 * command line through Bootloader,
1109 * platform data,
1110 * default partition information present in driver.
1111 */
Linus Walleij6c009ab2010-09-13 00:35:22 +02001112 /*
Dmitry Eremin-Solenikov8d3f8bb2011-05-29 20:16:57 +04001113 * Check for partition info passed
Linus Walleij6c009ab2010-09-13 00:35:22 +02001114 */
1115 host->mtd.name = "nand";
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001116 ret = mtd_device_parse_register(&host->mtd, NULL, NULL,
Vipin Kumar71470322012-03-14 11:47:07 +05301117 host->partitions, host->nr_partitions);
Jamie Iles99335d02011-05-23 10:23:23 +01001118 if (ret)
Linus Walleij6c009ab2010-09-13 00:35:22 +02001119 goto err_probe;
Linus Walleij6c009ab2010-09-13 00:35:22 +02001120
1121 platform_set_drvdata(pdev, host);
1122 dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
1123 return 0;
1124
1125err_probe:
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301126err_scan_ident:
Vipin Kumar4774fb02012-03-14 11:47:18 +05301127 if (host->mode == USE_DMA_ACCESS)
1128 dma_release_channel(host->write_dma_chan);
1129err_req_write_chnl:
1130 if (host->mode == USE_DMA_ACCESS)
1131 dma_release_channel(host->read_dma_chan);
1132err_req_read_chnl:
Linus Walleij6c009ab2010-09-13 00:35:22 +02001133 clk_disable(host->clk);
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301134err_clk_enable:
1135 clk_put(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001136 return ret;
1137}
1138
1139/*
1140 * Clean up routine
1141 */
1142static int fsmc_nand_remove(struct platform_device *pdev)
1143{
1144 struct fsmc_nand_data *host = platform_get_drvdata(pdev);
1145
1146 platform_set_drvdata(pdev, NULL);
1147
1148 if (host) {
Axel Lin82e023a2011-06-03 13:15:30 +08001149 nand_release(&host->mtd);
Vipin Kumar4774fb02012-03-14 11:47:18 +05301150
1151 if (host->mode == USE_DMA_ACCESS) {
1152 dma_release_channel(host->write_dma_chan);
1153 dma_release_channel(host->read_dma_chan);
1154 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001155 clk_disable(host->clk);
1156 clk_put(host->clk);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001157 }
Vipin Kumar82b9dbe2012-03-14 11:47:15 +05301158
Linus Walleij6c009ab2010-09-13 00:35:22 +02001159 return 0;
1160}
1161
1162#ifdef CONFIG_PM
1163static int fsmc_nand_suspend(struct device *dev)
1164{
1165 struct fsmc_nand_data *host = dev_get_drvdata(dev);
1166 if (host)
1167 clk_disable(host->clk);
1168 return 0;
1169}
1170
1171static int fsmc_nand_resume(struct device *dev)
1172{
1173 struct fsmc_nand_data *host = dev_get_drvdata(dev);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301174 if (host) {
Linus Walleij6c009ab2010-09-13 00:35:22 +02001175 clk_enable(host->clk);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301176 fsmc_nand_setup(host->regs_va, host->bank,
Vipin Kumare2f6bce2012-03-14 11:47:14 +05301177 host->nand.options & NAND_BUSWIDTH_16,
1178 host->dev_timings);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301179 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001180 return 0;
1181}
1182
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301183static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001184#endif
1185
1186static struct platform_driver fsmc_nand_driver = {
1187 .remove = fsmc_nand_remove,
1188 .driver = {
1189 .owner = THIS_MODULE,
1190 .name = "fsmc-nand",
1191#ifdef CONFIG_PM
1192 .pm = &fsmc_nand_pm_ops,
1193#endif
1194 },
1195};
1196
1197static int __init fsmc_nand_init(void)
1198{
1199 return platform_driver_probe(&fsmc_nand_driver,
1200 fsmc_nand_probe);
1201}
1202module_init(fsmc_nand_init);
1203
1204static void __exit fsmc_nand_exit(void)
1205{
1206 platform_driver_unregister(&fsmc_nand_driver);
1207}
1208module_exit(fsmc_nand_exit);
1209
1210MODULE_LICENSE("GPL");
1211MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
1212MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");