blob: e7ae63ab59c6f2308944571a1da62308c4ab9c0a [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
Linus Walleij6c009ab2010-09-13 00:35:22 +0200272/**
Linus Walleij593cd872010-11-29 13:52:19 +0100273 * struct fsmc_nand_data - structure for FSMC NAND device state
Linus Walleij6c009ab2010-09-13 00:35:22 +0200274 *
Linus Walleij593cd872010-11-29 13:52:19 +0100275 * @pid: Part ID on the AMBA PrimeCell format
Linus Walleij6c009ab2010-09-13 00:35:22 +0200276 * @mtd: MTD info for a NAND flash.
277 * @nand: Chip related info for a NAND flash.
Vipin Kumar71470322012-03-14 11:47:07 +0530278 * @partitions: Partition info for a NAND Flash.
279 * @nr_partitions: Total number of partition of a NAND flash.
Linus Walleij6c009ab2010-09-13 00:35:22 +0200280 *
281 * @ecc_place: ECC placing locations in oobfree type format.
282 * @bank: Bank number for probed device.
283 * @clk: Clock structure for FSMC.
284 *
285 * @data_va: NAND port for Data.
286 * @cmd_va: NAND port for Command.
287 * @addr_va: NAND port for Address.
288 * @regs_va: FSMC regs base address.
289 */
290struct fsmc_nand_data {
Linus Walleij593cd872010-11-29 13:52:19 +0100291 u32 pid;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200292 struct mtd_info mtd;
293 struct nand_chip nand;
Vipin Kumar71470322012-03-14 11:47:07 +0530294 struct mtd_partition *partitions;
295 unsigned int nr_partitions;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200296
297 struct fsmc_eccplace *ecc_place;
298 unsigned int bank;
299 struct clk *clk;
300
301 struct resource *resregs;
302 struct resource *rescmd;
303 struct resource *resaddr;
304 struct resource *resdata;
305
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530306 struct fsmc_nand_timings *dev_timings;
307
Linus Walleij6c009ab2010-09-13 00:35:22 +0200308 void __iomem *data_va;
309 void __iomem *cmd_va;
310 void __iomem *addr_va;
311 void __iomem *regs_va;
312
313 void (*select_chip)(uint32_t bank, uint32_t busw);
314};
315
316/* Assert CS signal based on chipnr */
317static void fsmc_select_chip(struct mtd_info *mtd, int chipnr)
318{
319 struct nand_chip *chip = mtd->priv;
320 struct fsmc_nand_data *host;
321
322 host = container_of(mtd, struct fsmc_nand_data, mtd);
323
324 switch (chipnr) {
325 case -1:
326 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
327 break;
328 case 0:
329 case 1:
330 case 2:
331 case 3:
332 if (host->select_chip)
333 host->select_chip(chipnr,
334 chip->options & NAND_BUSWIDTH_16);
335 break;
336
337 default:
338 BUG();
339 }
340}
341
342/*
343 * fsmc_cmd_ctrl - For facilitaing Hardware access
344 * This routine allows hardware specific access to control-lines(ALE,CLE)
345 */
346static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
347{
348 struct nand_chip *this = mtd->priv;
349 struct fsmc_nand_data *host = container_of(mtd,
350 struct fsmc_nand_data, mtd);
351 struct fsmc_regs *regs = host->regs_va;
352 unsigned int bank = host->bank;
353
354 if (ctrl & NAND_CTRL_CHANGE) {
355 if (ctrl & NAND_CLE) {
356 this->IO_ADDR_R = (void __iomem *)host->cmd_va;
357 this->IO_ADDR_W = (void __iomem *)host->cmd_va;
358 } else if (ctrl & NAND_ALE) {
359 this->IO_ADDR_R = (void __iomem *)host->addr_va;
360 this->IO_ADDR_W = (void __iomem *)host->addr_va;
361 } else {
362 this->IO_ADDR_R = (void __iomem *)host->data_va;
363 this->IO_ADDR_W = (void __iomem *)host->data_va;
364 }
365
366 if (ctrl & NAND_NCE) {
367 writel(readl(&regs->bank_regs[bank].pc) | FSMC_ENABLE,
368 &regs->bank_regs[bank].pc);
369 } else {
370 writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ENABLE,
371 &regs->bank_regs[bank].pc);
372 }
373 }
374
375 mb();
376
377 if (cmd != NAND_CMD_NONE)
378 writeb(cmd, this->IO_ADDR_W);
379}
380
381/*
382 * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
383 *
384 * This routine initializes timing parameters related to NAND memory access in
385 * FSMC registers
386 */
Shiraz Hashimf63acb72012-03-14 11:47:13 +0530387static void fsmc_nand_setup(struct fsmc_regs *regs, uint32_t bank,
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530388 uint32_t busw, struct fsmc_nand_timings *timings)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200389{
390 uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530391 uint32_t tclr, tar, thiz, thold, twait, tset;
392 struct fsmc_nand_timings *tims;
393 struct fsmc_nand_timings default_timings = {
394 .tclr = FSMC_TCLR_1,
395 .tar = FSMC_TAR_1,
396 .thiz = FSMC_THIZ_1,
397 .thold = FSMC_THOLD_4,
398 .twait = FSMC_TWAIT_6,
399 .tset = FSMC_TSET_0,
400 };
401
402 if (timings)
403 tims = timings;
404 else
405 tims = &default_timings;
406
407 tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT;
408 tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT;
409 thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT;
410 thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT;
411 twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT;
412 tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200413
414 if (busw)
415 writel(value | FSMC_DEVWID_16, &regs->bank_regs[bank].pc);
416 else
417 writel(value | FSMC_DEVWID_8, &regs->bank_regs[bank].pc);
418
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530419 writel(readl(&regs->bank_regs[bank].pc) | tclr | tar,
Linus Walleij6c009ab2010-09-13 00:35:22 +0200420 &regs->bank_regs[bank].pc);
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530421 writel(thiz | thold | twait | tset, &regs->bank_regs[bank].comm);
422 writel(thiz | thold | twait | tset, &regs->bank_regs[bank].attrib);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200423}
424
425/*
426 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
427 */
428static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
429{
430 struct fsmc_nand_data *host = container_of(mtd,
431 struct fsmc_nand_data, mtd);
432 struct fsmc_regs *regs = host->regs_va;
433 uint32_t bank = host->bank;
434
435 writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ECCPLEN_256,
436 &regs->bank_regs[bank].pc);
437 writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ECCEN,
438 &regs->bank_regs[bank].pc);
439 writel(readl(&regs->bank_regs[bank].pc) | FSMC_ECCEN,
440 &regs->bank_regs[bank].pc);
441}
442
443/*
444 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300445 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200446 * max of 8-bits)
447 */
448static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
449 uint8_t *ecc)
450{
451 struct fsmc_nand_data *host = container_of(mtd,
452 struct fsmc_nand_data, mtd);
453 struct fsmc_regs *regs = host->regs_va;
454 uint32_t bank = host->bank;
455 uint32_t ecc_tmp;
456 unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
457
458 do {
459 if (readl(&regs->bank_regs[bank].sts) & FSMC_CODE_RDY)
460 break;
461 else
462 cond_resched();
463 } while (!time_after_eq(jiffies, deadline));
464
465 ecc_tmp = readl(&regs->bank_regs[bank].ecc1);
466 ecc[0] = (uint8_t) (ecc_tmp >> 0);
467 ecc[1] = (uint8_t) (ecc_tmp >> 8);
468 ecc[2] = (uint8_t) (ecc_tmp >> 16);
469 ecc[3] = (uint8_t) (ecc_tmp >> 24);
470
471 ecc_tmp = readl(&regs->bank_regs[bank].ecc2);
472 ecc[4] = (uint8_t) (ecc_tmp >> 0);
473 ecc[5] = (uint8_t) (ecc_tmp >> 8);
474 ecc[6] = (uint8_t) (ecc_tmp >> 16);
475 ecc[7] = (uint8_t) (ecc_tmp >> 24);
476
477 ecc_tmp = readl(&regs->bank_regs[bank].ecc3);
478 ecc[8] = (uint8_t) (ecc_tmp >> 0);
479 ecc[9] = (uint8_t) (ecc_tmp >> 8);
480 ecc[10] = (uint8_t) (ecc_tmp >> 16);
481 ecc[11] = (uint8_t) (ecc_tmp >> 24);
482
483 ecc_tmp = readl(&regs->bank_regs[bank].sts);
484 ecc[12] = (uint8_t) (ecc_tmp >> 16);
485
486 return 0;
487}
488
489/*
490 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300491 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200492 * max of 1-bit)
493 */
494static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
495 uint8_t *ecc)
496{
497 struct fsmc_nand_data *host = container_of(mtd,
498 struct fsmc_nand_data, mtd);
499 struct fsmc_regs *regs = host->regs_va;
500 uint32_t bank = host->bank;
501 uint32_t ecc_tmp;
502
503 ecc_tmp = readl(&regs->bank_regs[bank].ecc1);
504 ecc[0] = (uint8_t) (ecc_tmp >> 0);
505 ecc[1] = (uint8_t) (ecc_tmp >> 8);
506 ecc[2] = (uint8_t) (ecc_tmp >> 16);
507
508 return 0;
509}
510
Vipin Kumar519300c2012-03-07 17:00:49 +0530511/* Count the number of 0's in buff upto a max of max_bits */
512static int count_written_bits(uint8_t *buff, int size, int max_bits)
513{
514 int k, written_bits = 0;
515
516 for (k = 0; k < size; k++) {
517 written_bits += hweight8(~buff[k]);
518 if (written_bits > max_bits)
519 break;
520 }
521
522 return written_bits;
523}
524
Linus Walleij6c009ab2010-09-13 00:35:22 +0200525/*
526 * fsmc_read_page_hwecc
527 * @mtd: mtd info structure
528 * @chip: nand chip info structure
529 * @buf: buffer to store read data
530 * @page: page number to read
531 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300532 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
Linus Walleij6c009ab2010-09-13 00:35:22 +0200533 * performed in a strict sequence as follows:
534 * data(512 byte) -> ecc(13 byte)
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300535 * After this read, fsmc hardware generates and reports error data bits(up to a
Linus Walleij6c009ab2010-09-13 00:35:22 +0200536 * max of 8 bits)
537 */
538static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
539 uint8_t *buf, int page)
540{
541 struct fsmc_nand_data *host = container_of(mtd,
542 struct fsmc_nand_data, mtd);
543 struct fsmc_eccplace *ecc_place = host->ecc_place;
544 int i, j, s, stat, eccsize = chip->ecc.size;
545 int eccbytes = chip->ecc.bytes;
546 int eccsteps = chip->ecc.steps;
547 uint8_t *p = buf;
548 uint8_t *ecc_calc = chip->buffers->ecccalc;
549 uint8_t *ecc_code = chip->buffers->ecccode;
550 int off, len, group = 0;
551 /*
552 * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
553 * end up reading 14 bytes (7 words) from oob. The local array is
554 * to maintain word alignment
555 */
556 uint16_t ecc_oob[7];
557 uint8_t *oob = (uint8_t *)&ecc_oob[0];
558
559 for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200560 chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
561 chip->ecc.hwctl(mtd, NAND_ECC_READ);
562 chip->read_buf(mtd, p, eccsize);
563
564 for (j = 0; j < eccbytes;) {
565 off = ecc_place->eccplace[group].offset;
566 len = ecc_place->eccplace[group].length;
567 group++;
568
569 /*
Vipin Kumar4cbe1bf02012-03-14 11:47:09 +0530570 * length is intentionally kept a higher multiple of 2
571 * to read at least 13 bytes even in case of 16 bit NAND
572 * devices
573 */
Vipin Kumaraea686b2012-03-14 11:47:10 +0530574 if (chip->options & NAND_BUSWIDTH_16)
575 len = roundup(len, 2);
576
Linus Walleij6c009ab2010-09-13 00:35:22 +0200577 chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
578 chip->read_buf(mtd, oob + j, len);
579 j += len;
580 }
581
Vipin Kumar519300c2012-03-07 17:00:49 +0530582 memcpy(&ecc_code[i], oob, chip->ecc.bytes);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200583 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
584
585 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
586 if (stat < 0)
587 mtd->ecc_stats.failed++;
588 else
589 mtd->ecc_stats.corrected += stat;
590 }
591
592 return 0;
593}
594
595/*
Armando Visconti753e0132012-03-07 17:00:54 +0530596 * fsmc_bch8_correct_data
Linus Walleij6c009ab2010-09-13 00:35:22 +0200597 * @mtd: mtd info structure
598 * @dat: buffer of read data
599 * @read_ecc: ecc read from device spare area
600 * @calc_ecc: ecc calculated from read data
601 *
602 * calc_ecc is a 104 bit information containing maximum of 8 error
603 * offset informations of 13 bits each in 512 bytes of read data.
604 */
Armando Visconti753e0132012-03-07 17:00:54 +0530605static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
Linus Walleij6c009ab2010-09-13 00:35:22 +0200606 uint8_t *read_ecc, uint8_t *calc_ecc)
607{
608 struct fsmc_nand_data *host = container_of(mtd,
609 struct fsmc_nand_data, mtd);
Vipin Kumar519300c2012-03-07 17:00:49 +0530610 struct nand_chip *chip = mtd->priv;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200611 struct fsmc_regs *regs = host->regs_va;
612 unsigned int bank = host->bank;
Armando Viscontia612c2a2012-03-07 17:00:53 +0530613 uint32_t err_idx[8];
Linus Walleij6c009ab2010-09-13 00:35:22 +0200614 uint32_t num_err, i;
Armando Visconti753e0132012-03-07 17:00:54 +0530615 uint32_t ecc1, ecc2, ecc3, ecc4;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200616
Vipin Kumar519300c2012-03-07 17:00:49 +0530617 num_err = (readl(&regs->bank_regs[bank].sts) >> 10) & 0xF;
618
619 /* no bit flipping */
620 if (likely(num_err == 0))
621 return 0;
622
623 /* too many errors */
624 if (unlikely(num_err > 8)) {
625 /*
626 * This is a temporary erase check. A newly erased page read
627 * would result in an ecc error because the oob data is also
628 * erased to FF and the calculated ecc for an FF data is not
629 * FF..FF.
630 * This is a workaround to skip performing correction in case
631 * data is FF..FF
632 *
633 * Logic:
634 * For every page, each bit written as 0 is counted until these
635 * number of bits are greater than 8 (the maximum correction
636 * capability of FSMC for each 512 + 13 bytes)
637 */
638
639 int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
640 int bits_data = count_written_bits(dat, chip->ecc.size, 8);
641
642 if ((bits_ecc + bits_data) <= 8) {
643 if (bits_data)
644 memset(dat, 0xff, chip->ecc.size);
645 return bits_data;
646 }
647
648 return -EBADMSG;
649 }
650
Linus Walleij6c009ab2010-09-13 00:35:22 +0200651 /*
652 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
653 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
654 *
655 * calc_ecc is a 104 bit information containing maximum of 8 error
656 * offset informations of 13 bits each. calc_ecc is copied into a
657 * uint64_t array and error offset indexes are populated in err_idx
658 * array
659 */
Armando Visconti753e0132012-03-07 17:00:54 +0530660 ecc1 = readl(&regs->bank_regs[bank].ecc1);
661 ecc2 = readl(&regs->bank_regs[bank].ecc2);
662 ecc3 = readl(&regs->bank_regs[bank].ecc3);
663 ecc4 = readl(&regs->bank_regs[bank].sts);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200664
Armando Visconti753e0132012-03-07 17:00:54 +0530665 err_idx[0] = (ecc1 >> 0) & 0x1FFF;
666 err_idx[1] = (ecc1 >> 13) & 0x1FFF;
667 err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
668 err_idx[3] = (ecc2 >> 7) & 0x1FFF;
669 err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
670 err_idx[5] = (ecc3 >> 1) & 0x1FFF;
671 err_idx[6] = (ecc3 >> 14) & 0x1FFF;
672 err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200673
674 i = 0;
675 while (num_err--) {
676 change_bit(0, (unsigned long *)&err_idx[i]);
677 change_bit(1, (unsigned long *)&err_idx[i]);
678
Vipin Kumarb533f8d2012-03-14 11:47:11 +0530679 if (err_idx[i] < chip->ecc.size * 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200680 change_bit(err_idx[i], (unsigned long *)dat);
681 i++;
682 }
683 }
684 return i;
685}
686
687/*
688 * fsmc_nand_probe - Probe function
689 * @pdev: platform device structure
690 */
691static int __init fsmc_nand_probe(struct platform_device *pdev)
692{
693 struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
694 struct fsmc_nand_data *host;
695 struct mtd_info *mtd;
696 struct nand_chip *nand;
697 struct fsmc_regs *regs;
698 struct resource *res;
Linus Walleij4ad916b2010-11-29 13:52:06 +0100699 int ret = 0;
Linus Walleij593cd872010-11-29 13:52:19 +0100700 u32 pid;
701 int i;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200702
703 if (!pdata) {
704 dev_err(&pdev->dev, "platform data is NULL\n");
705 return -EINVAL;
706 }
707
708 /* Allocate memory for the device structure (and zero it) */
709 host = kzalloc(sizeof(*host), GFP_KERNEL);
710 if (!host) {
711 dev_err(&pdev->dev, "failed to allocate device structure\n");
712 return -ENOMEM;
713 }
714
715 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
716 if (!res) {
717 ret = -EIO;
718 goto err_probe1;
719 }
720
721 host->resdata = request_mem_region(res->start, resource_size(res),
722 pdev->name);
723 if (!host->resdata) {
724 ret = -EIO;
725 goto err_probe1;
726 }
727
728 host->data_va = ioremap(res->start, resource_size(res));
729 if (!host->data_va) {
730 ret = -EIO;
731 goto err_probe1;
732 }
733
Shiraz Hashimb2acc922012-03-07 17:00:51 +0530734 host->resaddr = request_mem_region(res->start + pdata->ale_off,
Linus Walleij6c009ab2010-09-13 00:35:22 +0200735 resource_size(res), pdev->name);
736 if (!host->resaddr) {
737 ret = -EIO;
738 goto err_probe1;
739 }
740
Shiraz Hashimb2acc922012-03-07 17:00:51 +0530741 host->addr_va = ioremap(res->start + pdata->ale_off,
742 resource_size(res));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200743 if (!host->addr_va) {
744 ret = -EIO;
745 goto err_probe1;
746 }
747
Shiraz Hashimb2acc922012-03-07 17:00:51 +0530748 host->rescmd = request_mem_region(res->start + pdata->cle_off,
Linus Walleij6c009ab2010-09-13 00:35:22 +0200749 resource_size(res), pdev->name);
750 if (!host->rescmd) {
751 ret = -EIO;
752 goto err_probe1;
753 }
754
Shiraz Hashimb2acc922012-03-07 17:00:51 +0530755 host->cmd_va = ioremap(res->start + pdata->cle_off, resource_size(res));
Linus Walleij6c009ab2010-09-13 00:35:22 +0200756 if (!host->cmd_va) {
757 ret = -EIO;
758 goto err_probe1;
759 }
760
761 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
762 if (!res) {
763 ret = -EIO;
764 goto err_probe1;
765 }
766
767 host->resregs = request_mem_region(res->start, resource_size(res),
768 pdev->name);
769 if (!host->resregs) {
770 ret = -EIO;
771 goto err_probe1;
772 }
773
774 host->regs_va = ioremap(res->start, resource_size(res));
775 if (!host->regs_va) {
776 ret = -EIO;
777 goto err_probe1;
778 }
779
780 host->clk = clk_get(&pdev->dev, NULL);
781 if (IS_ERR(host->clk)) {
782 dev_err(&pdev->dev, "failed to fetch block clock\n");
783 ret = PTR_ERR(host->clk);
784 host->clk = NULL;
785 goto err_probe1;
786 }
787
788 ret = clk_enable(host->clk);
789 if (ret)
790 goto err_probe1;
791
Linus Walleij593cd872010-11-29 13:52:19 +0100792 /*
793 * This device ID is actually a common AMBA ID as used on the
794 * AMBA PrimeCell bus. However it is not a PrimeCell.
795 */
796 for (pid = 0, i = 0; i < 4; i++)
797 pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
798 host->pid = pid;
799 dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
800 "revision %02x, config %02x\n",
801 AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
802 AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
803
Linus Walleij6c009ab2010-09-13 00:35:22 +0200804 host->bank = pdata->bank;
805 host->select_chip = pdata->select_bank;
Vipin Kumar71470322012-03-14 11:47:07 +0530806 host->partitions = pdata->partitions;
807 host->nr_partitions = pdata->nr_partitions;
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530808 host->dev_timings = pdata->nand_timings;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200809 regs = host->regs_va;
810
811 /* Link all private pointers */
812 mtd = &host->mtd;
813 nand = &host->nand;
814 mtd->priv = nand;
815 nand->priv = host;
816
817 host->mtd.owner = THIS_MODULE;
818 nand->IO_ADDR_R = host->data_va;
819 nand->IO_ADDR_W = host->data_va;
820 nand->cmd_ctrl = fsmc_cmd_ctrl;
821 nand->chip_delay = 30;
822
823 nand->ecc.mode = NAND_ECC_HW;
824 nand->ecc.hwctl = fsmc_enable_hwecc;
825 nand->ecc.size = 512;
826 nand->options = pdata->options;
827 nand->select_chip = fsmc_select_chip;
Vipin Kumar467e6e72012-03-14 11:47:12 +0530828 nand->badblockbits = 7;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200829
830 if (pdata->width == FSMC_NAND_BW16)
831 nand->options |= NAND_BUSWIDTH_16;
832
Vipin Kumare2f6bce2012-03-14 11:47:14 +0530833 fsmc_nand_setup(regs, host->bank, nand->options & NAND_BUSWIDTH_16,
834 host->dev_timings);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200835
Linus Walleij593cd872010-11-29 13:52:19 +0100836 if (AMBA_REV_BITS(host->pid) >= 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200837 nand->ecc.read_page = fsmc_read_page_hwecc;
838 nand->ecc.calculate = fsmc_read_hwecc_ecc4;
Armando Visconti753e0132012-03-07 17:00:54 +0530839 nand->ecc.correct = fsmc_bch8_correct_data;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200840 nand->ecc.bytes = 13;
Mike Dunn6a918ba2012-03-11 14:21:11 -0700841 nand->ecc.strength = 8;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200842 } else {
843 nand->ecc.calculate = fsmc_read_hwecc_ecc1;
844 nand->ecc.correct = nand_correct_data;
845 nand->ecc.bytes = 3;
Mike Dunn6a918ba2012-03-11 14:21:11 -0700846 nand->ecc.strength = 1;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200847 }
848
849 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300850 * Scan to find existence of the device
Linus Walleij6c009ab2010-09-13 00:35:22 +0200851 */
852 if (nand_scan_ident(&host->mtd, 1, NULL)) {
853 ret = -ENXIO;
854 dev_err(&pdev->dev, "No NAND Device found!\n");
855 goto err_probe;
856 }
857
Linus Walleij593cd872010-11-29 13:52:19 +0100858 if (AMBA_REV_BITS(host->pid) >= 8) {
Bhavna Yadave29ee572012-03-07 17:00:50 +0530859 switch (host->mtd.oobsize) {
860 case 16:
861 nand->ecc.layout = &fsmc_ecc4_16_layout;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200862 host->ecc_place = &fsmc_ecc4_sp_place;
Bhavna Yadave29ee572012-03-07 17:00:50 +0530863 break;
864 case 64:
865 nand->ecc.layout = &fsmc_ecc4_64_layout;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200866 host->ecc_place = &fsmc_ecc4_lp_place;
Bhavna Yadave29ee572012-03-07 17:00:50 +0530867 break;
868 case 128:
869 nand->ecc.layout = &fsmc_ecc4_128_layout;
870 host->ecc_place = &fsmc_ecc4_lp_place;
871 break;
Armando Visconti0c78e932012-03-07 17:00:55 +0530872 case 224:
873 nand->ecc.layout = &fsmc_ecc4_224_layout;
874 host->ecc_place = &fsmc_ecc4_lp_place;
875 break;
Bhavna Yadave29ee572012-03-07 17:00:50 +0530876 case 256:
877 nand->ecc.layout = &fsmc_ecc4_256_layout;
878 host->ecc_place = &fsmc_ecc4_lp_place;
879 break;
880 default:
881 printk(KERN_WARNING "No oob scheme defined for "
882 "oobsize %d\n", mtd->oobsize);
883 BUG();
Linus Walleij6c009ab2010-09-13 00:35:22 +0200884 }
885 } else {
Bhavna Yadave29ee572012-03-07 17:00:50 +0530886 switch (host->mtd.oobsize) {
887 case 16:
888 nand->ecc.layout = &fsmc_ecc1_16_layout;
889 break;
890 case 64:
891 nand->ecc.layout = &fsmc_ecc1_64_layout;
892 break;
893 case 128:
894 nand->ecc.layout = &fsmc_ecc1_128_layout;
895 break;
896 default:
897 printk(KERN_WARNING "No oob scheme defined for "
898 "oobsize %d\n", mtd->oobsize);
899 BUG();
900 }
Linus Walleij6c009ab2010-09-13 00:35:22 +0200901 }
902
903 /* Second stage of scan to fill MTD data-structures */
904 if (nand_scan_tail(&host->mtd)) {
905 ret = -ENXIO;
906 goto err_probe;
907 }
908
909 /*
910 * The partition information can is accessed by (in the same precedence)
911 *
912 * command line through Bootloader,
913 * platform data,
914 * default partition information present in driver.
915 */
Linus Walleij6c009ab2010-09-13 00:35:22 +0200916 /*
Dmitry Eremin-Solenikov8d3f8bb2011-05-29 20:16:57 +0400917 * Check for partition info passed
Linus Walleij6c009ab2010-09-13 00:35:22 +0200918 */
919 host->mtd.name = "nand";
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200920 ret = mtd_device_parse_register(&host->mtd, NULL, NULL,
Vipin Kumar71470322012-03-14 11:47:07 +0530921 host->partitions, host->nr_partitions);
Jamie Iles99335d02011-05-23 10:23:23 +0100922 if (ret)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200923 goto err_probe;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200924
925 platform_set_drvdata(pdev, host);
926 dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
927 return 0;
928
929err_probe:
930 clk_disable(host->clk);
931err_probe1:
932 if (host->clk)
933 clk_put(host->clk);
934 if (host->regs_va)
935 iounmap(host->regs_va);
936 if (host->resregs)
937 release_mem_region(host->resregs->start,
938 resource_size(host->resregs));
939 if (host->cmd_va)
940 iounmap(host->cmd_va);
941 if (host->rescmd)
942 release_mem_region(host->rescmd->start,
943 resource_size(host->rescmd));
944 if (host->addr_va)
945 iounmap(host->addr_va);
946 if (host->resaddr)
947 release_mem_region(host->resaddr->start,
948 resource_size(host->resaddr));
949 if (host->data_va)
950 iounmap(host->data_va);
951 if (host->resdata)
952 release_mem_region(host->resdata->start,
953 resource_size(host->resdata));
954
955 kfree(host);
956 return ret;
957}
958
959/*
960 * Clean up routine
961 */
962static int fsmc_nand_remove(struct platform_device *pdev)
963{
964 struct fsmc_nand_data *host = platform_get_drvdata(pdev);
965
966 platform_set_drvdata(pdev, NULL);
967
968 if (host) {
Axel Lin82e023a2011-06-03 13:15:30 +0800969 nand_release(&host->mtd);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200970 clk_disable(host->clk);
971 clk_put(host->clk);
972
973 iounmap(host->regs_va);
974 release_mem_region(host->resregs->start,
975 resource_size(host->resregs));
976 iounmap(host->cmd_va);
977 release_mem_region(host->rescmd->start,
978 resource_size(host->rescmd));
979 iounmap(host->addr_va);
980 release_mem_region(host->resaddr->start,
981 resource_size(host->resaddr));
982 iounmap(host->data_va);
983 release_mem_region(host->resdata->start,
984 resource_size(host->resdata));
985
986 kfree(host);
987 }
988 return 0;
989}
990
991#ifdef CONFIG_PM
992static int fsmc_nand_suspend(struct device *dev)
993{
994 struct fsmc_nand_data *host = dev_get_drvdata(dev);
995 if (host)
996 clk_disable(host->clk);
997 return 0;
998}
999
1000static int fsmc_nand_resume(struct device *dev)
1001{
1002 struct fsmc_nand_data *host = dev_get_drvdata(dev);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301003 if (host) {
Linus Walleij6c009ab2010-09-13 00:35:22 +02001004 clk_enable(host->clk);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301005 fsmc_nand_setup(host->regs_va, host->bank,
Vipin Kumare2f6bce2012-03-14 11:47:14 +05301006 host->nand.options & NAND_BUSWIDTH_16,
1007 host->dev_timings);
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301008 }
Linus Walleij6c009ab2010-09-13 00:35:22 +02001009 return 0;
1010}
1011
Shiraz Hashimf63acb72012-03-14 11:47:13 +05301012static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
Linus Walleij6c009ab2010-09-13 00:35:22 +02001013#endif
1014
1015static struct platform_driver fsmc_nand_driver = {
1016 .remove = fsmc_nand_remove,
1017 .driver = {
1018 .owner = THIS_MODULE,
1019 .name = "fsmc-nand",
1020#ifdef CONFIG_PM
1021 .pm = &fsmc_nand_pm_ops,
1022#endif
1023 },
1024};
1025
1026static int __init fsmc_nand_init(void)
1027{
1028 return platform_driver_probe(&fsmc_nand_driver,
1029 fsmc_nand_probe);
1030}
1031module_init(fsmc_nand_init);
1032
1033static void __exit fsmc_nand_exit(void)
1034{
1035 platform_driver_unregister(&fsmc_nand_driver);
1036}
1037module_exit(fsmc_nand_exit);
1038
1039MODULE_LICENSE("GPL");
1040MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
1041MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");