blob: 7338d33fe1ede77d4ba276bec93bf87b025f7c7c [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>
20#include <linux/err.h>
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/resource.h>
24#include <linux/sched.h>
25#include <linux/types.h>
26#include <linux/mtd/mtd.h>
27#include <linux/mtd/nand.h>
28#include <linux/mtd/nand_ecc.h>
29#include <linux/platform_device.h>
30#include <linux/mtd/partitions.h>
31#include <linux/io.h>
32#include <linux/slab.h>
33#include <linux/mtd/fsmc.h>
Linus Walleij593cd872010-11-29 13:52:19 +010034#include <linux/amba/bus.h>
Linus Walleij6c009ab2010-09-13 00:35:22 +020035#include <mtd/mtd-abi.h>
36
Bhavna Yadave29ee572012-03-07 17:00:50 +053037static struct nand_ecclayout fsmc_ecc1_128_layout = {
Linus Walleij6c009ab2010-09-13 00:35:22 +020038 .eccbytes = 24,
39 .eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52,
40 66, 67, 68, 82, 83, 84, 98, 99, 100, 114, 115, 116},
41 .oobfree = {
42 {.offset = 8, .length = 8},
43 {.offset = 24, .length = 8},
44 {.offset = 40, .length = 8},
45 {.offset = 56, .length = 8},
46 {.offset = 72, .length = 8},
47 {.offset = 88, .length = 8},
48 {.offset = 104, .length = 8},
49 {.offset = 120, .length = 8}
50 }
51};
52
Bhavna Yadave29ee572012-03-07 17:00:50 +053053static struct nand_ecclayout fsmc_ecc1_64_layout = {
54 .eccbytes = 12,
55 .eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52},
56 .oobfree = {
57 {.offset = 8, .length = 8},
58 {.offset = 24, .length = 8},
59 {.offset = 40, .length = 8},
60 {.offset = 56, .length = 8},
61 }
62};
63
64static struct nand_ecclayout fsmc_ecc1_16_layout = {
65 .eccbytes = 3,
66 .eccpos = {2, 3, 4},
67 .oobfree = {
68 {.offset = 8, .length = 8},
69 }
70};
71
72/*
73 * ECC4 layout for NAND of pagesize 8192 bytes & OOBsize 256 bytes. 13*16 bytes
74 * of OB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block and 46
75 * bytes are free for use.
76 */
77static struct nand_ecclayout fsmc_ecc4_256_layout = {
78 .eccbytes = 208,
79 .eccpos = { 2, 3, 4, 5, 6, 7, 8,
80 9, 10, 11, 12, 13, 14,
81 18, 19, 20, 21, 22, 23, 24,
82 25, 26, 27, 28, 29, 30,
83 34, 35, 36, 37, 38, 39, 40,
84 41, 42, 43, 44, 45, 46,
85 50, 51, 52, 53, 54, 55, 56,
86 57, 58, 59, 60, 61, 62,
87 66, 67, 68, 69, 70, 71, 72,
88 73, 74, 75, 76, 77, 78,
89 82, 83, 84, 85, 86, 87, 88,
90 89, 90, 91, 92, 93, 94,
91 98, 99, 100, 101, 102, 103, 104,
92 105, 106, 107, 108, 109, 110,
93 114, 115, 116, 117, 118, 119, 120,
94 121, 122, 123, 124, 125, 126,
95 130, 131, 132, 133, 134, 135, 136,
96 137, 138, 139, 140, 141, 142,
97 146, 147, 148, 149, 150, 151, 152,
98 153, 154, 155, 156, 157, 158,
99 162, 163, 164, 165, 166, 167, 168,
100 169, 170, 171, 172, 173, 174,
101 178, 179, 180, 181, 182, 183, 184,
102 185, 186, 187, 188, 189, 190,
103 194, 195, 196, 197, 198, 199, 200,
104 201, 202, 203, 204, 205, 206,
105 210, 211, 212, 213, 214, 215, 216,
106 217, 218, 219, 220, 221, 222,
107 226, 227, 228, 229, 230, 231, 232,
108 233, 234, 235, 236, 237, 238,
109 242, 243, 244, 245, 246, 247, 248,
110 249, 250, 251, 252, 253, 254
111 },
112 .oobfree = {
113 {.offset = 15, .length = 3},
114 {.offset = 31, .length = 3},
115 {.offset = 47, .length = 3},
116 {.offset = 63, .length = 3},
117 {.offset = 79, .length = 3},
118 {.offset = 95, .length = 3},
119 {.offset = 111, .length = 3},
120 {.offset = 127, .length = 3},
121 {.offset = 143, .length = 3},
122 {.offset = 159, .length = 3},
123 {.offset = 175, .length = 3},
124 {.offset = 191, .length = 3},
125 {.offset = 207, .length = 3},
126 {.offset = 223, .length = 3},
127 {.offset = 239, .length = 3},
128 {.offset = 255, .length = 1}
129 }
130};
131
132/*
Armando Visconti0c78e932012-03-07 17:00:55 +0530133 * ECC4 layout for NAND of pagesize 4096 bytes & OOBsize 224 bytes. 13*8 bytes
134 * of OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block & 118
135 * bytes are free for use.
136 */
137static struct nand_ecclayout fsmc_ecc4_224_layout = {
138 .eccbytes = 104,
139 .eccpos = { 2, 3, 4, 5, 6, 7, 8,
140 9, 10, 11, 12, 13, 14,
141 18, 19, 20, 21, 22, 23, 24,
142 25, 26, 27, 28, 29, 30,
143 34, 35, 36, 37, 38, 39, 40,
144 41, 42, 43, 44, 45, 46,
145 50, 51, 52, 53, 54, 55, 56,
146 57, 58, 59, 60, 61, 62,
147 66, 67, 68, 69, 70, 71, 72,
148 73, 74, 75, 76, 77, 78,
149 82, 83, 84, 85, 86, 87, 88,
150 89, 90, 91, 92, 93, 94,
151 98, 99, 100, 101, 102, 103, 104,
152 105, 106, 107, 108, 109, 110,
153 114, 115, 116, 117, 118, 119, 120,
154 121, 122, 123, 124, 125, 126
155 },
156 .oobfree = {
157 {.offset = 15, .length = 3},
158 {.offset = 31, .length = 3},
159 {.offset = 47, .length = 3},
160 {.offset = 63, .length = 3},
161 {.offset = 79, .length = 3},
162 {.offset = 95, .length = 3},
163 {.offset = 111, .length = 3},
164 {.offset = 127, .length = 97}
165 }
166};
167
168/*
Bhavna Yadave29ee572012-03-07 17:00:50 +0530169 * ECC4 layout for NAND of pagesize 4096 bytes & OOBsize 128 bytes. 13*8 bytes
170 * of OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block & 22
171 * bytes are free for use.
172 */
173static struct nand_ecclayout fsmc_ecc4_128_layout = {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200174 .eccbytes = 104,
175 .eccpos = { 2, 3, 4, 5, 6, 7, 8,
176 9, 10, 11, 12, 13, 14,
177 18, 19, 20, 21, 22, 23, 24,
178 25, 26, 27, 28, 29, 30,
179 34, 35, 36, 37, 38, 39, 40,
180 41, 42, 43, 44, 45, 46,
181 50, 51, 52, 53, 54, 55, 56,
182 57, 58, 59, 60, 61, 62,
183 66, 67, 68, 69, 70, 71, 72,
184 73, 74, 75, 76, 77, 78,
185 82, 83, 84, 85, 86, 87, 88,
186 89, 90, 91, 92, 93, 94,
187 98, 99, 100, 101, 102, 103, 104,
188 105, 106, 107, 108, 109, 110,
189 114, 115, 116, 117, 118, 119, 120,
190 121, 122, 123, 124, 125, 126
191 },
192 .oobfree = {
193 {.offset = 15, .length = 3},
194 {.offset = 31, .length = 3},
195 {.offset = 47, .length = 3},
196 {.offset = 63, .length = 3},
197 {.offset = 79, .length = 3},
198 {.offset = 95, .length = 3},
199 {.offset = 111, .length = 3},
200 {.offset = 127, .length = 1}
201 }
202};
203
204/*
Bhavna Yadave29ee572012-03-07 17:00:50 +0530205 * ECC4 layout for NAND of pagesize 2048 bytes & OOBsize 64 bytes. 13*4 bytes of
206 * OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block and 10
207 * bytes are free for use.
208 */
209static struct nand_ecclayout fsmc_ecc4_64_layout = {
210 .eccbytes = 52,
211 .eccpos = { 2, 3, 4, 5, 6, 7, 8,
212 9, 10, 11, 12, 13, 14,
213 18, 19, 20, 21, 22, 23, 24,
214 25, 26, 27, 28, 29, 30,
215 34, 35, 36, 37, 38, 39, 40,
216 41, 42, 43, 44, 45, 46,
217 50, 51, 52, 53, 54, 55, 56,
218 57, 58, 59, 60, 61, 62,
219 },
220 .oobfree = {
221 {.offset = 15, .length = 3},
222 {.offset = 31, .length = 3},
223 {.offset = 47, .length = 3},
224 {.offset = 63, .length = 1},
225 }
226};
227
228/*
229 * ECC4 layout for NAND of pagesize 512 bytes & OOBsize 16 bytes. 13 bytes of
230 * OOB size is reserved for ECC, Byte no. 4 & 5 reserved for bad block and One
231 * byte is free for use.
232 */
233static struct nand_ecclayout fsmc_ecc4_16_layout = {
234 .eccbytes = 13,
235 .eccpos = { 0, 1, 2, 3, 6, 7, 8,
236 9, 10, 11, 12, 13, 14
237 },
238 .oobfree = {
239 {.offset = 15, .length = 1},
240 }
241};
242
243/*
Linus Walleij6c009ab2010-09-13 00:35:22 +0200244 * ECC placement definitions in oobfree type format.
245 * There are 13 bytes of ecc for every 512 byte block and it has to be read
246 * consecutively and immediately after the 512 byte data block for hardware to
247 * generate the error bit offsets in 512 byte data.
248 * Managing the ecc bytes in the following way makes it easier for software to
249 * read ecc bytes consecutive to data bytes. This way is similar to
250 * oobfree structure maintained already in generic nand driver
251 */
252static struct fsmc_eccplace fsmc_ecc4_lp_place = {
253 .eccplace = {
254 {.offset = 2, .length = 13},
255 {.offset = 18, .length = 13},
256 {.offset = 34, .length = 13},
257 {.offset = 50, .length = 13},
258 {.offset = 66, .length = 13},
259 {.offset = 82, .length = 13},
260 {.offset = 98, .length = 13},
261 {.offset = 114, .length = 13}
262 }
263};
264
Linus Walleij6c009ab2010-09-13 00:35:22 +0200265static struct fsmc_eccplace fsmc_ecc4_sp_place = {
266 .eccplace = {
267 {.offset = 0, .length = 4},
268 {.offset = 6, .length = 9}
269 }
270};
271
272/*
273 * Default partition tables to be used if the partition information not
Linus Walleij4ad916b2010-11-29 13:52:06 +0100274 * provided through platform data.
275 *
Linus Walleij6c009ab2010-09-13 00:35:22 +0200276 * Default partition layout for small page(= 512 bytes) devices
277 * Size for "Root file system" is updated in driver based on actual device size
278 */
279static struct mtd_partition partition_info_16KB_blk[] = {
Linus Walleij4ad916b2010-11-29 13:52:06 +0100280 {
281 .name = "X-loader",
282 .offset = 0,
283 .size = 4*0x4000,
284 },
285 {
286 .name = "U-Boot",
287 .offset = 0x10000,
288 .size = 20*0x4000,
289 },
290 {
291 .name = "Kernel",
292 .offset = 0x60000,
293 .size = 256*0x4000,
294 },
295 {
296 .name = "Root File System",
297 .offset = 0x460000,
Dmitry Eremin-Solenikov0d04eda2011-06-02 18:00:38 +0400298 .size = MTDPART_SIZ_FULL,
Linus Walleij4ad916b2010-11-29 13:52:06 +0100299 },
Linus Walleij6c009ab2010-09-13 00:35:22 +0200300};
301
302/*
303 * Default partition layout for large page(> 512 bytes) devices
304 * Size for "Root file system" is updated in driver based on actual device size
305 */
306static struct mtd_partition partition_info_128KB_blk[] = {
Linus Walleij4ad916b2010-11-29 13:52:06 +0100307 {
308 .name = "X-loader",
309 .offset = 0,
310 .size = 4*0x20000,
311 },
312 {
313 .name = "U-Boot",
314 .offset = 0x80000,
315 .size = 12*0x20000,
316 },
317 {
318 .name = "Kernel",
319 .offset = 0x200000,
320 .size = 48*0x20000,
321 },
322 {
323 .name = "Root File System",
324 .offset = 0x800000,
Dmitry Eremin-Solenikov0d04eda2011-06-02 18:00:38 +0400325 .size = MTDPART_SIZ_FULL,
Linus Walleij4ad916b2010-11-29 13:52:06 +0100326 },
Linus Walleij6c009ab2010-09-13 00:35:22 +0200327};
328
Linus Walleij6c009ab2010-09-13 00:35:22 +0200329
330/**
Linus Walleij593cd872010-11-29 13:52:19 +0100331 * struct fsmc_nand_data - structure for FSMC NAND device state
Linus Walleij6c009ab2010-09-13 00:35:22 +0200332 *
Linus Walleij593cd872010-11-29 13:52:19 +0100333 * @pid: Part ID on the AMBA PrimeCell format
Linus Walleij6c009ab2010-09-13 00:35:22 +0200334 * @mtd: MTD info for a NAND flash.
335 * @nand: Chip related info for a NAND flash.
Vipin Kumar71470322012-03-14 11:47:07 +0530336 * @partitions: Partition info for a NAND Flash.
337 * @nr_partitions: Total number of partition of a NAND flash.
Linus Walleij6c009ab2010-09-13 00:35:22 +0200338 *
339 * @ecc_place: ECC placing locations in oobfree type format.
340 * @bank: Bank number for probed device.
341 * @clk: Clock structure for FSMC.
342 *
343 * @data_va: NAND port for Data.
344 * @cmd_va: NAND port for Command.
345 * @addr_va: NAND port for Address.
346 * @regs_va: FSMC regs base address.
347 */
348struct fsmc_nand_data {
Linus Walleij593cd872010-11-29 13:52:19 +0100349 u32 pid;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200350 struct mtd_info mtd;
351 struct nand_chip nand;
Vipin Kumar71470322012-03-14 11:47:07 +0530352 struct mtd_partition *partitions;
353 unsigned int nr_partitions;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200354
355 struct fsmc_eccplace *ecc_place;
356 unsigned int bank;
357 struct clk *clk;
358
359 struct resource *resregs;
360 struct resource *rescmd;
361 struct resource *resaddr;
362 struct resource *resdata;
363
364 void __iomem *data_va;
365 void __iomem *cmd_va;
366 void __iomem *addr_va;
367 void __iomem *regs_va;
368
369 void (*select_chip)(uint32_t bank, uint32_t busw);
370};
371
372/* Assert CS signal based on chipnr */
373static void fsmc_select_chip(struct mtd_info *mtd, int chipnr)
374{
375 struct nand_chip *chip = mtd->priv;
376 struct fsmc_nand_data *host;
377
378 host = container_of(mtd, struct fsmc_nand_data, mtd);
379
380 switch (chipnr) {
381 case -1:
382 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
383 break;
384 case 0:
385 case 1:
386 case 2:
387 case 3:
388 if (host->select_chip)
389 host->select_chip(chipnr,
390 chip->options & NAND_BUSWIDTH_16);
391 break;
392
393 default:
394 BUG();
395 }
396}
397
398/*
399 * fsmc_cmd_ctrl - For facilitaing Hardware access
400 * This routine allows hardware specific access to control-lines(ALE,CLE)
401 */
402static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
403{
404 struct nand_chip *this = mtd->priv;
405 struct fsmc_nand_data *host = container_of(mtd,
406 struct fsmc_nand_data, mtd);
407 struct fsmc_regs *regs = host->regs_va;
408 unsigned int bank = host->bank;
409
410 if (ctrl & NAND_CTRL_CHANGE) {
411 if (ctrl & NAND_CLE) {
412 this->IO_ADDR_R = (void __iomem *)host->cmd_va;
413 this->IO_ADDR_W = (void __iomem *)host->cmd_va;
414 } else if (ctrl & NAND_ALE) {
415 this->IO_ADDR_R = (void __iomem *)host->addr_va;
416 this->IO_ADDR_W = (void __iomem *)host->addr_va;
417 } else {
418 this->IO_ADDR_R = (void __iomem *)host->data_va;
419 this->IO_ADDR_W = (void __iomem *)host->data_va;
420 }
421
422 if (ctrl & NAND_NCE) {
423 writel(readl(&regs->bank_regs[bank].pc) | FSMC_ENABLE,
424 &regs->bank_regs[bank].pc);
425 } else {
426 writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ENABLE,
427 &regs->bank_regs[bank].pc);
428 }
429 }
430
431 mb();
432
433 if (cmd != NAND_CMD_NONE)
434 writeb(cmd, this->IO_ADDR_W);
435}
436
437/*
438 * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
439 *
440 * This routine initializes timing parameters related to NAND memory access in
441 * FSMC registers
442 */
443static void __init fsmc_nand_setup(struct fsmc_regs *regs, uint32_t bank,
444 uint32_t busw)
445{
446 uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
447
448 if (busw)
449 writel(value | FSMC_DEVWID_16, &regs->bank_regs[bank].pc);
450 else
451 writel(value | FSMC_DEVWID_8, &regs->bank_regs[bank].pc);
452
453 writel(readl(&regs->bank_regs[bank].pc) | FSMC_TCLR_1 | FSMC_TAR_1,
454 &regs->bank_regs[bank].pc);
455 writel(FSMC_THIZ_1 | FSMC_THOLD_4 | FSMC_TWAIT_6 | FSMC_TSET_0,
456 &regs->bank_regs[bank].comm);
457 writel(FSMC_THIZ_1 | FSMC_THOLD_4 | FSMC_TWAIT_6 | FSMC_TSET_0,
458 &regs->bank_regs[bank].attrib);
459}
460
461/*
462 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
463 */
464static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
465{
466 struct fsmc_nand_data *host = container_of(mtd,
467 struct fsmc_nand_data, mtd);
468 struct fsmc_regs *regs = host->regs_va;
469 uint32_t bank = host->bank;
470
471 writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ECCPLEN_256,
472 &regs->bank_regs[bank].pc);
473 writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ECCEN,
474 &regs->bank_regs[bank].pc);
475 writel(readl(&regs->bank_regs[bank].pc) | FSMC_ECCEN,
476 &regs->bank_regs[bank].pc);
477}
478
479/*
480 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300481 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200482 * max of 8-bits)
483 */
484static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
485 uint8_t *ecc)
486{
487 struct fsmc_nand_data *host = container_of(mtd,
488 struct fsmc_nand_data, mtd);
489 struct fsmc_regs *regs = host->regs_va;
490 uint32_t bank = host->bank;
491 uint32_t ecc_tmp;
492 unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
493
494 do {
495 if (readl(&regs->bank_regs[bank].sts) & FSMC_CODE_RDY)
496 break;
497 else
498 cond_resched();
499 } while (!time_after_eq(jiffies, deadline));
500
501 ecc_tmp = readl(&regs->bank_regs[bank].ecc1);
502 ecc[0] = (uint8_t) (ecc_tmp >> 0);
503 ecc[1] = (uint8_t) (ecc_tmp >> 8);
504 ecc[2] = (uint8_t) (ecc_tmp >> 16);
505 ecc[3] = (uint8_t) (ecc_tmp >> 24);
506
507 ecc_tmp = readl(&regs->bank_regs[bank].ecc2);
508 ecc[4] = (uint8_t) (ecc_tmp >> 0);
509 ecc[5] = (uint8_t) (ecc_tmp >> 8);
510 ecc[6] = (uint8_t) (ecc_tmp >> 16);
511 ecc[7] = (uint8_t) (ecc_tmp >> 24);
512
513 ecc_tmp = readl(&regs->bank_regs[bank].ecc3);
514 ecc[8] = (uint8_t) (ecc_tmp >> 0);
515 ecc[9] = (uint8_t) (ecc_tmp >> 8);
516 ecc[10] = (uint8_t) (ecc_tmp >> 16);
517 ecc[11] = (uint8_t) (ecc_tmp >> 24);
518
519 ecc_tmp = readl(&regs->bank_regs[bank].sts);
520 ecc[12] = (uint8_t) (ecc_tmp >> 16);
521
522 return 0;
523}
524
525/*
526 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300527 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200528 * max of 1-bit)
529 */
530static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
531 uint8_t *ecc)
532{
533 struct fsmc_nand_data *host = container_of(mtd,
534 struct fsmc_nand_data, mtd);
535 struct fsmc_regs *regs = host->regs_va;
536 uint32_t bank = host->bank;
537 uint32_t ecc_tmp;
538
539 ecc_tmp = readl(&regs->bank_regs[bank].ecc1);
540 ecc[0] = (uint8_t) (ecc_tmp >> 0);
541 ecc[1] = (uint8_t) (ecc_tmp >> 8);
542 ecc[2] = (uint8_t) (ecc_tmp >> 16);
543
544 return 0;
545}
546
Vipin Kumar519300c2012-03-07 17:00:49 +0530547/* Count the number of 0's in buff upto a max of max_bits */
548static int count_written_bits(uint8_t *buff, int size, int max_bits)
549{
550 int k, written_bits = 0;
551
552 for (k = 0; k < size; k++) {
553 written_bits += hweight8(~buff[k]);
554 if (written_bits > max_bits)
555 break;
556 }
557
558 return written_bits;
559}
560
Linus Walleij6c009ab2010-09-13 00:35:22 +0200561/*
562 * fsmc_read_page_hwecc
563 * @mtd: mtd info structure
564 * @chip: nand chip info structure
565 * @buf: buffer to store read data
566 * @page: page number to read
567 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300568 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
Linus Walleij6c009ab2010-09-13 00:35:22 +0200569 * performed in a strict sequence as follows:
570 * data(512 byte) -> ecc(13 byte)
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300571 * After this read, fsmc hardware generates and reports error data bits(up to a
Linus Walleij6c009ab2010-09-13 00:35:22 +0200572 * max of 8 bits)
573 */
574static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
575 uint8_t *buf, int page)
576{
577 struct fsmc_nand_data *host = container_of(mtd,
578 struct fsmc_nand_data, mtd);
579 struct fsmc_eccplace *ecc_place = host->ecc_place;
580 int i, j, s, stat, eccsize = chip->ecc.size;
581 int eccbytes = chip->ecc.bytes;
582 int eccsteps = chip->ecc.steps;
583 uint8_t *p = buf;
584 uint8_t *ecc_calc = chip->buffers->ecccalc;
585 uint8_t *ecc_code = chip->buffers->ecccode;
586 int off, len, group = 0;
587 /*
588 * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
589 * end up reading 14 bytes (7 words) from oob. The local array is
590 * to maintain word alignment
591 */
592 uint16_t ecc_oob[7];
593 uint8_t *oob = (uint8_t *)&ecc_oob[0];
594
595 for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200596 chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
597 chip->ecc.hwctl(mtd, NAND_ECC_READ);
598 chip->read_buf(mtd, p, eccsize);
599
600 for (j = 0; j < eccbytes;) {
601 off = ecc_place->eccplace[group].offset;
602 len = ecc_place->eccplace[group].length;
603 group++;
604
605 /*
606 * length is intentionally kept a higher multiple of 2
607 * to read at least 13 bytes even in case of 16 bit NAND
608 * devices
609 */
610 len = roundup(len, 2);
611 chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
612 chip->read_buf(mtd, oob + j, len);
613 j += len;
614 }
615
Vipin Kumar519300c2012-03-07 17:00:49 +0530616 memcpy(&ecc_code[i], oob, chip->ecc.bytes);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200617 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
618
619 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
620 if (stat < 0)
621 mtd->ecc_stats.failed++;
622 else
623 mtd->ecc_stats.corrected += stat;
624 }
625
626 return 0;
627}
628
629/*
Armando Visconti753e0132012-03-07 17:00:54 +0530630 * fsmc_bch8_correct_data
Linus Walleij6c009ab2010-09-13 00:35:22 +0200631 * @mtd: mtd info structure
632 * @dat: buffer of read data
633 * @read_ecc: ecc read from device spare area
634 * @calc_ecc: ecc calculated from read data
635 *
636 * calc_ecc is a 104 bit information containing maximum of 8 error
637 * offset informations of 13 bits each in 512 bytes of read data.
638 */
Armando Visconti753e0132012-03-07 17:00:54 +0530639static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
Linus Walleij6c009ab2010-09-13 00:35:22 +0200640 uint8_t *read_ecc, uint8_t *calc_ecc)
641{
642 struct fsmc_nand_data *host = container_of(mtd,
643 struct fsmc_nand_data, mtd);
Vipin Kumar519300c2012-03-07 17:00:49 +0530644 struct nand_chip *chip = mtd->priv;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200645 struct fsmc_regs *regs = host->regs_va;
646 unsigned int bank = host->bank;
Armando Viscontia612c2a2012-03-07 17:00:53 +0530647 uint32_t err_idx[8];
Linus Walleij6c009ab2010-09-13 00:35:22 +0200648 uint32_t num_err, i;
Armando Visconti753e0132012-03-07 17:00:54 +0530649 uint32_t ecc1, ecc2, ecc3, ecc4;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200650
Vipin Kumar519300c2012-03-07 17:00:49 +0530651 num_err = (readl(&regs->bank_regs[bank].sts) >> 10) & 0xF;
652
653 /* no bit flipping */
654 if (likely(num_err == 0))
655 return 0;
656
657 /* too many errors */
658 if (unlikely(num_err > 8)) {
659 /*
660 * This is a temporary erase check. A newly erased page read
661 * would result in an ecc error because the oob data is also
662 * erased to FF and the calculated ecc for an FF data is not
663 * FF..FF.
664 * This is a workaround to skip performing correction in case
665 * data is FF..FF
666 *
667 * Logic:
668 * For every page, each bit written as 0 is counted until these
669 * number of bits are greater than 8 (the maximum correction
670 * capability of FSMC for each 512 + 13 bytes)
671 */
672
673 int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
674 int bits_data = count_written_bits(dat, chip->ecc.size, 8);
675
676 if ((bits_ecc + bits_data) <= 8) {
677 if (bits_data)
678 memset(dat, 0xff, chip->ecc.size);
679 return bits_data;
680 }
681
682 return -EBADMSG;
683 }
684
Linus Walleij6c009ab2010-09-13 00:35:22 +0200685 /*
686 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
687 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
688 *
689 * calc_ecc is a 104 bit information containing maximum of 8 error
690 * offset informations of 13 bits each. calc_ecc is copied into a
691 * uint64_t array and error offset indexes are populated in err_idx
692 * array
693 */
Armando Visconti753e0132012-03-07 17:00:54 +0530694 ecc1 = readl(&regs->bank_regs[bank].ecc1);
695 ecc2 = readl(&regs->bank_regs[bank].ecc2);
696 ecc3 = readl(&regs->bank_regs[bank].ecc3);
697 ecc4 = readl(&regs->bank_regs[bank].sts);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200698
Armando Visconti753e0132012-03-07 17:00:54 +0530699 err_idx[0] = (ecc1 >> 0) & 0x1FFF;
700 err_idx[1] = (ecc1 >> 13) & 0x1FFF;
701 err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
702 err_idx[3] = (ecc2 >> 7) & 0x1FFF;
703 err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
704 err_idx[5] = (ecc3 >> 1) & 0x1FFF;
705 err_idx[6] = (ecc3 >> 14) & 0x1FFF;
706 err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200707
708 i = 0;
709 while (num_err--) {
710 change_bit(0, (unsigned long *)&err_idx[i]);
711 change_bit(1, (unsigned long *)&err_idx[i]);
712
Vipin Kumar519300c2012-03-07 17:00:49 +0530713 if (err_idx[i] <= chip->ecc.size * 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200714 change_bit(err_idx[i], (unsigned long *)dat);
715 i++;
716 }
717 }
718 return i;
719}
720
721/*
722 * fsmc_nand_probe - Probe function
723 * @pdev: platform device structure
724 */
725static int __init fsmc_nand_probe(struct platform_device *pdev)
726{
727 struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
728 struct fsmc_nand_data *host;
729 struct mtd_info *mtd;
730 struct nand_chip *nand;
731 struct fsmc_regs *regs;
732 struct resource *res;
Linus Walleij4ad916b2010-11-29 13:52:06 +0100733 int ret = 0;
Linus Walleij593cd872010-11-29 13:52:19 +0100734 u32 pid;
735 int i;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200736
737 if (!pdata) {
738 dev_err(&pdev->dev, "platform data is NULL\n");
739 return -EINVAL;
740 }
741
742 /* Allocate memory for the device structure (and zero it) */
743 host = kzalloc(sizeof(*host), GFP_KERNEL);
744 if (!host) {
745 dev_err(&pdev->dev, "failed to allocate device structure\n");
746 return -ENOMEM;
747 }
748
749 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
750 if (!res) {
751 ret = -EIO;
752 goto err_probe1;
753 }
754
755 host->resdata = request_mem_region(res->start, resource_size(res),
756 pdev->name);
757 if (!host->resdata) {
758 ret = -EIO;
759 goto err_probe1;
760 }
761
762 host->data_va = ioremap(res->start, resource_size(res));
763 if (!host->data_va) {
764 ret = -EIO;
765 goto err_probe1;
766 }
767
Shiraz Hashimb2acc922012-03-07 17:00:51 +0530768 host->resaddr = request_mem_region(res->start + pdata->ale_off,
Linus Walleij6c009ab2010-09-13 00:35:22 +0200769 resource_size(res), pdev->name);
770 if (!host->resaddr) {
771 ret = -EIO;
772 goto err_probe1;
773 }
774
Shiraz Hashimb2acc922012-03-07 17:00:51 +0530775 host->addr_va = ioremap(res->start + pdata->ale_off,
776 resource_size(res));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200777 if (!host->addr_va) {
778 ret = -EIO;
779 goto err_probe1;
780 }
781
Shiraz Hashimb2acc922012-03-07 17:00:51 +0530782 host->rescmd = request_mem_region(res->start + pdata->cle_off,
Linus Walleij6c009ab2010-09-13 00:35:22 +0200783 resource_size(res), pdev->name);
784 if (!host->rescmd) {
785 ret = -EIO;
786 goto err_probe1;
787 }
788
Shiraz Hashimb2acc922012-03-07 17:00:51 +0530789 host->cmd_va = ioremap(res->start + pdata->cle_off, resource_size(res));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200790 if (!host->cmd_va) {
791 ret = -EIO;
792 goto err_probe1;
793 }
794
795 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
796 if (!res) {
797 ret = -EIO;
798 goto err_probe1;
799 }
800
801 host->resregs = request_mem_region(res->start, resource_size(res),
802 pdev->name);
803 if (!host->resregs) {
804 ret = -EIO;
805 goto err_probe1;
806 }
807
808 host->regs_va = ioremap(res->start, resource_size(res));
809 if (!host->regs_va) {
810 ret = -EIO;
811 goto err_probe1;
812 }
813
814 host->clk = clk_get(&pdev->dev, NULL);
815 if (IS_ERR(host->clk)) {
816 dev_err(&pdev->dev, "failed to fetch block clock\n");
817 ret = PTR_ERR(host->clk);
818 host->clk = NULL;
819 goto err_probe1;
820 }
821
822 ret = clk_enable(host->clk);
823 if (ret)
824 goto err_probe1;
825
Linus Walleij593cd872010-11-29 13:52:19 +0100826 /*
827 * This device ID is actually a common AMBA ID as used on the
828 * AMBA PrimeCell bus. However it is not a PrimeCell.
829 */
830 for (pid = 0, i = 0; i < 4; i++)
831 pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
832 host->pid = pid;
833 dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
834 "revision %02x, config %02x\n",
835 AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
836 AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
837
Linus Walleij6c009ab2010-09-13 00:35:22 +0200838 host->bank = pdata->bank;
839 host->select_chip = pdata->select_bank;
Vipin Kumar71470322012-03-14 11:47:07 +0530840 host->partitions = pdata->partitions;
841 host->nr_partitions = pdata->nr_partitions;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200842 regs = host->regs_va;
843
844 /* Link all private pointers */
845 mtd = &host->mtd;
846 nand = &host->nand;
847 mtd->priv = nand;
848 nand->priv = host;
849
850 host->mtd.owner = THIS_MODULE;
851 nand->IO_ADDR_R = host->data_va;
852 nand->IO_ADDR_W = host->data_va;
853 nand->cmd_ctrl = fsmc_cmd_ctrl;
854 nand->chip_delay = 30;
855
856 nand->ecc.mode = NAND_ECC_HW;
857 nand->ecc.hwctl = fsmc_enable_hwecc;
858 nand->ecc.size = 512;
859 nand->options = pdata->options;
860 nand->select_chip = fsmc_select_chip;
861
862 if (pdata->width == FSMC_NAND_BW16)
863 nand->options |= NAND_BUSWIDTH_16;
864
865 fsmc_nand_setup(regs, host->bank, nand->options & NAND_BUSWIDTH_16);
866
Linus Walleij593cd872010-11-29 13:52:19 +0100867 if (AMBA_REV_BITS(host->pid) >= 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200868 nand->ecc.read_page = fsmc_read_page_hwecc;
869 nand->ecc.calculate = fsmc_read_hwecc_ecc4;
Armando Visconti753e0132012-03-07 17:00:54 +0530870 nand->ecc.correct = fsmc_bch8_correct_data;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200871 nand->ecc.bytes = 13;
Mike Dunn6a918ba2012-03-11 14:21:11 -0700872 nand->ecc.strength = 8;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200873 } else {
874 nand->ecc.calculate = fsmc_read_hwecc_ecc1;
875 nand->ecc.correct = nand_correct_data;
876 nand->ecc.bytes = 3;
Mike Dunn6a918ba2012-03-11 14:21:11 -0700877 nand->ecc.strength = 1;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200878 }
879
880 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300881 * Scan to find existence of the device
Linus Walleij6c009ab2010-09-13 00:35:22 +0200882 */
883 if (nand_scan_ident(&host->mtd, 1, NULL)) {
884 ret = -ENXIO;
885 dev_err(&pdev->dev, "No NAND Device found!\n");
886 goto err_probe;
887 }
888
Linus Walleij593cd872010-11-29 13:52:19 +0100889 if (AMBA_REV_BITS(host->pid) >= 8) {
Bhavna Yadave29ee572012-03-07 17:00:50 +0530890 switch (host->mtd.oobsize) {
891 case 16:
892 nand->ecc.layout = &fsmc_ecc4_16_layout;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200893 host->ecc_place = &fsmc_ecc4_sp_place;
Bhavna Yadave29ee572012-03-07 17:00:50 +0530894 break;
895 case 64:
896 nand->ecc.layout = &fsmc_ecc4_64_layout;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200897 host->ecc_place = &fsmc_ecc4_lp_place;
Bhavna Yadave29ee572012-03-07 17:00:50 +0530898 break;
899 case 128:
900 nand->ecc.layout = &fsmc_ecc4_128_layout;
901 host->ecc_place = &fsmc_ecc4_lp_place;
902 break;
Armando Visconti0c78e932012-03-07 17:00:55 +0530903 case 224:
904 nand->ecc.layout = &fsmc_ecc4_224_layout;
905 host->ecc_place = &fsmc_ecc4_lp_place;
906 break;
Bhavna Yadave29ee572012-03-07 17:00:50 +0530907 case 256:
908 nand->ecc.layout = &fsmc_ecc4_256_layout;
909 host->ecc_place = &fsmc_ecc4_lp_place;
910 break;
911 default:
912 printk(KERN_WARNING "No oob scheme defined for "
913 "oobsize %d\n", mtd->oobsize);
914 BUG();
Linus Walleij6c009ab2010-09-13 00:35:22 +0200915 }
916 } else {
Bhavna Yadave29ee572012-03-07 17:00:50 +0530917 switch (host->mtd.oobsize) {
918 case 16:
919 nand->ecc.layout = &fsmc_ecc1_16_layout;
920 break;
921 case 64:
922 nand->ecc.layout = &fsmc_ecc1_64_layout;
923 break;
924 case 128:
925 nand->ecc.layout = &fsmc_ecc1_128_layout;
926 break;
927 default:
928 printk(KERN_WARNING "No oob scheme defined for "
929 "oobsize %d\n", mtd->oobsize);
930 BUG();
931 }
Linus Walleij6c009ab2010-09-13 00:35:22 +0200932 }
933
934 /* Second stage of scan to fill MTD data-structures */
935 if (nand_scan_tail(&host->mtd)) {
936 ret = -ENXIO;
937 goto err_probe;
938 }
939
940 /*
941 * The partition information can is accessed by (in the same precedence)
942 *
943 * command line through Bootloader,
944 * platform data,
945 * default partition information present in driver.
946 */
Linus Walleij6c009ab2010-09-13 00:35:22 +0200947 /*
Dmitry Eremin-Solenikov8d3f8bb2011-05-29 20:16:57 +0400948 * Check for partition info passed
Linus Walleij6c009ab2010-09-13 00:35:22 +0200949 */
950 host->mtd.name = "nand";
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200951 ret = mtd_device_parse_register(&host->mtd, NULL, NULL,
Vipin Kumar71470322012-03-14 11:47:07 +0530952 host->partitions, host->nr_partitions);
Jamie Iles99335d02011-05-23 10:23:23 +0100953 if (ret)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200954 goto err_probe;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200955
956 platform_set_drvdata(pdev, host);
957 dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
958 return 0;
959
960err_probe:
961 clk_disable(host->clk);
962err_probe1:
963 if (host->clk)
964 clk_put(host->clk);
965 if (host->regs_va)
966 iounmap(host->regs_va);
967 if (host->resregs)
968 release_mem_region(host->resregs->start,
969 resource_size(host->resregs));
970 if (host->cmd_va)
971 iounmap(host->cmd_va);
972 if (host->rescmd)
973 release_mem_region(host->rescmd->start,
974 resource_size(host->rescmd));
975 if (host->addr_va)
976 iounmap(host->addr_va);
977 if (host->resaddr)
978 release_mem_region(host->resaddr->start,
979 resource_size(host->resaddr));
980 if (host->data_va)
981 iounmap(host->data_va);
982 if (host->resdata)
983 release_mem_region(host->resdata->start,
984 resource_size(host->resdata));
985
986 kfree(host);
987 return ret;
988}
989
990/*
991 * Clean up routine
992 */
993static int fsmc_nand_remove(struct platform_device *pdev)
994{
995 struct fsmc_nand_data *host = platform_get_drvdata(pdev);
996
997 platform_set_drvdata(pdev, NULL);
998
999 if (host) {
Axel Lin82e023a2011-06-03 13:15:30 +08001000 nand_release(&host->mtd);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001001 clk_disable(host->clk);
1002 clk_put(host->clk);
1003
1004 iounmap(host->regs_va);
1005 release_mem_region(host->resregs->start,
1006 resource_size(host->resregs));
1007 iounmap(host->cmd_va);
1008 release_mem_region(host->rescmd->start,
1009 resource_size(host->rescmd));
1010 iounmap(host->addr_va);
1011 release_mem_region(host->resaddr->start,
1012 resource_size(host->resaddr));
1013 iounmap(host->data_va);
1014 release_mem_region(host->resdata->start,
1015 resource_size(host->resdata));
1016
1017 kfree(host);
1018 }
1019 return 0;
1020}
1021
1022#ifdef CONFIG_PM
1023static int fsmc_nand_suspend(struct device *dev)
1024{
1025 struct fsmc_nand_data *host = dev_get_drvdata(dev);
1026 if (host)
1027 clk_disable(host->clk);
1028 return 0;
1029}
1030
1031static int fsmc_nand_resume(struct device *dev)
1032{
1033 struct fsmc_nand_data *host = dev_get_drvdata(dev);
1034 if (host)
1035 clk_enable(host->clk);
1036 return 0;
1037}
1038
1039static const struct dev_pm_ops fsmc_nand_pm_ops = {
1040 .suspend = fsmc_nand_suspend,
1041 .resume = fsmc_nand_resume,
1042};
1043#endif
1044
1045static struct platform_driver fsmc_nand_driver = {
1046 .remove = fsmc_nand_remove,
1047 .driver = {
1048 .owner = THIS_MODULE,
1049 .name = "fsmc-nand",
1050#ifdef CONFIG_PM
1051 .pm = &fsmc_nand_pm_ops,
1052#endif
1053 },
1054};
1055
1056static int __init fsmc_nand_init(void)
1057{
1058 return platform_driver_probe(&fsmc_nand_driver,
1059 fsmc_nand_probe);
1060}
1061module_init(fsmc_nand_init);
1062
1063static void __exit fsmc_nand_exit(void)
1064{
1065 platform_driver_unregister(&fsmc_nand_driver);
1066}
1067module_exit(fsmc_nand_exit);
1068
1069MODULE_LICENSE("GPL");
1070MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
1071MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");