blob: 9c7d9d85f75692a1bb5ae2e953ac2f65c6f42343 [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
37static struct nand_ecclayout fsmc_ecc1_layout = {
38 .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
53static struct nand_ecclayout fsmc_ecc4_lp_layout = {
54 .eccbytes = 104,
55 .eccpos = { 2, 3, 4, 5, 6, 7, 8,
56 9, 10, 11, 12, 13, 14,
57 18, 19, 20, 21, 22, 23, 24,
58 25, 26, 27, 28, 29, 30,
59 34, 35, 36, 37, 38, 39, 40,
60 41, 42, 43, 44, 45, 46,
61 50, 51, 52, 53, 54, 55, 56,
62 57, 58, 59, 60, 61, 62,
63 66, 67, 68, 69, 70, 71, 72,
64 73, 74, 75, 76, 77, 78,
65 82, 83, 84, 85, 86, 87, 88,
66 89, 90, 91, 92, 93, 94,
67 98, 99, 100, 101, 102, 103, 104,
68 105, 106, 107, 108, 109, 110,
69 114, 115, 116, 117, 118, 119, 120,
70 121, 122, 123, 124, 125, 126
71 },
72 .oobfree = {
73 {.offset = 15, .length = 3},
74 {.offset = 31, .length = 3},
75 {.offset = 47, .length = 3},
76 {.offset = 63, .length = 3},
77 {.offset = 79, .length = 3},
78 {.offset = 95, .length = 3},
79 {.offset = 111, .length = 3},
80 {.offset = 127, .length = 1}
81 }
82};
83
84/*
85 * ECC placement definitions in oobfree type format.
86 * There are 13 bytes of ecc for every 512 byte block and it has to be read
87 * consecutively and immediately after the 512 byte data block for hardware to
88 * generate the error bit offsets in 512 byte data.
89 * Managing the ecc bytes in the following way makes it easier for software to
90 * read ecc bytes consecutive to data bytes. This way is similar to
91 * oobfree structure maintained already in generic nand driver
92 */
93static struct fsmc_eccplace fsmc_ecc4_lp_place = {
94 .eccplace = {
95 {.offset = 2, .length = 13},
96 {.offset = 18, .length = 13},
97 {.offset = 34, .length = 13},
98 {.offset = 50, .length = 13},
99 {.offset = 66, .length = 13},
100 {.offset = 82, .length = 13},
101 {.offset = 98, .length = 13},
102 {.offset = 114, .length = 13}
103 }
104};
105
106static struct nand_ecclayout fsmc_ecc4_sp_layout = {
107 .eccbytes = 13,
108 .eccpos = { 0, 1, 2, 3, 6, 7, 8,
109 9, 10, 11, 12, 13, 14
110 },
111 .oobfree = {
112 {.offset = 15, .length = 1},
113 }
114};
115
116static struct fsmc_eccplace fsmc_ecc4_sp_place = {
117 .eccplace = {
118 {.offset = 0, .length = 4},
119 {.offset = 6, .length = 9}
120 }
121};
122
123/*
124 * Default partition tables to be used if the partition information not
Linus Walleij4ad916b2010-11-29 13:52:06 +0100125 * provided through platform data.
126 *
Linus Walleij6c009ab2010-09-13 00:35:22 +0200127 * Default partition layout for small page(= 512 bytes) devices
128 * Size for "Root file system" is updated in driver based on actual device size
129 */
130static struct mtd_partition partition_info_16KB_blk[] = {
Linus Walleij4ad916b2010-11-29 13:52:06 +0100131 {
132 .name = "X-loader",
133 .offset = 0,
134 .size = 4*0x4000,
135 },
136 {
137 .name = "U-Boot",
138 .offset = 0x10000,
139 .size = 20*0x4000,
140 },
141 {
142 .name = "Kernel",
143 .offset = 0x60000,
144 .size = 256*0x4000,
145 },
146 {
147 .name = "Root File System",
148 .offset = 0x460000,
Dmitry Eremin-Solenikov0d04eda2011-06-02 18:00:38 +0400149 .size = MTDPART_SIZ_FULL,
Linus Walleij4ad916b2010-11-29 13:52:06 +0100150 },
Linus Walleij6c009ab2010-09-13 00:35:22 +0200151};
152
153/*
154 * Default partition layout for large page(> 512 bytes) devices
155 * Size for "Root file system" is updated in driver based on actual device size
156 */
157static struct mtd_partition partition_info_128KB_blk[] = {
Linus Walleij4ad916b2010-11-29 13:52:06 +0100158 {
159 .name = "X-loader",
160 .offset = 0,
161 .size = 4*0x20000,
162 },
163 {
164 .name = "U-Boot",
165 .offset = 0x80000,
166 .size = 12*0x20000,
167 },
168 {
169 .name = "Kernel",
170 .offset = 0x200000,
171 .size = 48*0x20000,
172 },
173 {
174 .name = "Root File System",
175 .offset = 0x800000,
Dmitry Eremin-Solenikov0d04eda2011-06-02 18:00:38 +0400176 .size = MTDPART_SIZ_FULL,
Linus Walleij4ad916b2010-11-29 13:52:06 +0100177 },
Linus Walleij6c009ab2010-09-13 00:35:22 +0200178};
179
Linus Walleij6c009ab2010-09-13 00:35:22 +0200180
181/**
Linus Walleij593cd872010-11-29 13:52:19 +0100182 * struct fsmc_nand_data - structure for FSMC NAND device state
Linus Walleij6c009ab2010-09-13 00:35:22 +0200183 *
Linus Walleij593cd872010-11-29 13:52:19 +0100184 * @pid: Part ID on the AMBA PrimeCell format
Linus Walleij6c009ab2010-09-13 00:35:22 +0200185 * @mtd: MTD info for a NAND flash.
186 * @nand: Chip related info for a NAND flash.
Linus Walleij6c009ab2010-09-13 00:35:22 +0200187 *
188 * @ecc_place: ECC placing locations in oobfree type format.
189 * @bank: Bank number for probed device.
190 * @clk: Clock structure for FSMC.
191 *
192 * @data_va: NAND port for Data.
193 * @cmd_va: NAND port for Command.
194 * @addr_va: NAND port for Address.
195 * @regs_va: FSMC regs base address.
196 */
197struct fsmc_nand_data {
Linus Walleij593cd872010-11-29 13:52:19 +0100198 u32 pid;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200199 struct mtd_info mtd;
200 struct nand_chip nand;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200201
202 struct fsmc_eccplace *ecc_place;
203 unsigned int bank;
204 struct clk *clk;
205
206 struct resource *resregs;
207 struct resource *rescmd;
208 struct resource *resaddr;
209 struct resource *resdata;
210
211 void __iomem *data_va;
212 void __iomem *cmd_va;
213 void __iomem *addr_va;
214 void __iomem *regs_va;
215
216 void (*select_chip)(uint32_t bank, uint32_t busw);
217};
218
219/* Assert CS signal based on chipnr */
220static void fsmc_select_chip(struct mtd_info *mtd, int chipnr)
221{
222 struct nand_chip *chip = mtd->priv;
223 struct fsmc_nand_data *host;
224
225 host = container_of(mtd, struct fsmc_nand_data, mtd);
226
227 switch (chipnr) {
228 case -1:
229 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
230 break;
231 case 0:
232 case 1:
233 case 2:
234 case 3:
235 if (host->select_chip)
236 host->select_chip(chipnr,
237 chip->options & NAND_BUSWIDTH_16);
238 break;
239
240 default:
241 BUG();
242 }
243}
244
245/*
246 * fsmc_cmd_ctrl - For facilitaing Hardware access
247 * This routine allows hardware specific access to control-lines(ALE,CLE)
248 */
249static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
250{
251 struct nand_chip *this = mtd->priv;
252 struct fsmc_nand_data *host = container_of(mtd,
253 struct fsmc_nand_data, mtd);
254 struct fsmc_regs *regs = host->regs_va;
255 unsigned int bank = host->bank;
256
257 if (ctrl & NAND_CTRL_CHANGE) {
258 if (ctrl & NAND_CLE) {
259 this->IO_ADDR_R = (void __iomem *)host->cmd_va;
260 this->IO_ADDR_W = (void __iomem *)host->cmd_va;
261 } else if (ctrl & NAND_ALE) {
262 this->IO_ADDR_R = (void __iomem *)host->addr_va;
263 this->IO_ADDR_W = (void __iomem *)host->addr_va;
264 } else {
265 this->IO_ADDR_R = (void __iomem *)host->data_va;
266 this->IO_ADDR_W = (void __iomem *)host->data_va;
267 }
268
269 if (ctrl & NAND_NCE) {
270 writel(readl(&regs->bank_regs[bank].pc) | FSMC_ENABLE,
271 &regs->bank_regs[bank].pc);
272 } else {
273 writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ENABLE,
274 &regs->bank_regs[bank].pc);
275 }
276 }
277
278 mb();
279
280 if (cmd != NAND_CMD_NONE)
281 writeb(cmd, this->IO_ADDR_W);
282}
283
284/*
285 * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
286 *
287 * This routine initializes timing parameters related to NAND memory access in
288 * FSMC registers
289 */
290static void __init fsmc_nand_setup(struct fsmc_regs *regs, uint32_t bank,
291 uint32_t busw)
292{
293 uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
294
295 if (busw)
296 writel(value | FSMC_DEVWID_16, &regs->bank_regs[bank].pc);
297 else
298 writel(value | FSMC_DEVWID_8, &regs->bank_regs[bank].pc);
299
300 writel(readl(&regs->bank_regs[bank].pc) | FSMC_TCLR_1 | FSMC_TAR_1,
301 &regs->bank_regs[bank].pc);
302 writel(FSMC_THIZ_1 | FSMC_THOLD_4 | FSMC_TWAIT_6 | FSMC_TSET_0,
303 &regs->bank_regs[bank].comm);
304 writel(FSMC_THIZ_1 | FSMC_THOLD_4 | FSMC_TWAIT_6 | FSMC_TSET_0,
305 &regs->bank_regs[bank].attrib);
306}
307
308/*
309 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
310 */
311static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
312{
313 struct fsmc_nand_data *host = container_of(mtd,
314 struct fsmc_nand_data, mtd);
315 struct fsmc_regs *regs = host->regs_va;
316 uint32_t bank = host->bank;
317
318 writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ECCPLEN_256,
319 &regs->bank_regs[bank].pc);
320 writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ECCEN,
321 &regs->bank_regs[bank].pc);
322 writel(readl(&regs->bank_regs[bank].pc) | FSMC_ECCEN,
323 &regs->bank_regs[bank].pc);
324}
325
326/*
327 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300328 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200329 * max of 8-bits)
330 */
331static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
332 uint8_t *ecc)
333{
334 struct fsmc_nand_data *host = container_of(mtd,
335 struct fsmc_nand_data, mtd);
336 struct fsmc_regs *regs = host->regs_va;
337 uint32_t bank = host->bank;
338 uint32_t ecc_tmp;
339 unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
340
341 do {
342 if (readl(&regs->bank_regs[bank].sts) & FSMC_CODE_RDY)
343 break;
344 else
345 cond_resched();
346 } while (!time_after_eq(jiffies, deadline));
347
348 ecc_tmp = readl(&regs->bank_regs[bank].ecc1);
349 ecc[0] = (uint8_t) (ecc_tmp >> 0);
350 ecc[1] = (uint8_t) (ecc_tmp >> 8);
351 ecc[2] = (uint8_t) (ecc_tmp >> 16);
352 ecc[3] = (uint8_t) (ecc_tmp >> 24);
353
354 ecc_tmp = readl(&regs->bank_regs[bank].ecc2);
355 ecc[4] = (uint8_t) (ecc_tmp >> 0);
356 ecc[5] = (uint8_t) (ecc_tmp >> 8);
357 ecc[6] = (uint8_t) (ecc_tmp >> 16);
358 ecc[7] = (uint8_t) (ecc_tmp >> 24);
359
360 ecc_tmp = readl(&regs->bank_regs[bank].ecc3);
361 ecc[8] = (uint8_t) (ecc_tmp >> 0);
362 ecc[9] = (uint8_t) (ecc_tmp >> 8);
363 ecc[10] = (uint8_t) (ecc_tmp >> 16);
364 ecc[11] = (uint8_t) (ecc_tmp >> 24);
365
366 ecc_tmp = readl(&regs->bank_regs[bank].sts);
367 ecc[12] = (uint8_t) (ecc_tmp >> 16);
368
369 return 0;
370}
371
372/*
373 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300374 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
Linus Walleij6c009ab2010-09-13 00:35:22 +0200375 * max of 1-bit)
376 */
377static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
378 uint8_t *ecc)
379{
380 struct fsmc_nand_data *host = container_of(mtd,
381 struct fsmc_nand_data, mtd);
382 struct fsmc_regs *regs = host->regs_va;
383 uint32_t bank = host->bank;
384 uint32_t ecc_tmp;
385
386 ecc_tmp = readl(&regs->bank_regs[bank].ecc1);
387 ecc[0] = (uint8_t) (ecc_tmp >> 0);
388 ecc[1] = (uint8_t) (ecc_tmp >> 8);
389 ecc[2] = (uint8_t) (ecc_tmp >> 16);
390
391 return 0;
392}
393
Vipin Kumar519300c2012-03-07 17:00:49 +0530394/* Count the number of 0's in buff upto a max of max_bits */
395static int count_written_bits(uint8_t *buff, int size, int max_bits)
396{
397 int k, written_bits = 0;
398
399 for (k = 0; k < size; k++) {
400 written_bits += hweight8(~buff[k]);
401 if (written_bits > max_bits)
402 break;
403 }
404
405 return written_bits;
406}
407
Linus Walleij6c009ab2010-09-13 00:35:22 +0200408/*
409 * fsmc_read_page_hwecc
410 * @mtd: mtd info structure
411 * @chip: nand chip info structure
412 * @buf: buffer to store read data
413 * @page: page number to read
414 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300415 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
Linus Walleij6c009ab2010-09-13 00:35:22 +0200416 * performed in a strict sequence as follows:
417 * data(512 byte) -> ecc(13 byte)
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300418 * After this read, fsmc hardware generates and reports error data bits(up to a
Linus Walleij6c009ab2010-09-13 00:35:22 +0200419 * max of 8 bits)
420 */
421static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
422 uint8_t *buf, int page)
423{
424 struct fsmc_nand_data *host = container_of(mtd,
425 struct fsmc_nand_data, mtd);
426 struct fsmc_eccplace *ecc_place = host->ecc_place;
427 int i, j, s, stat, eccsize = chip->ecc.size;
428 int eccbytes = chip->ecc.bytes;
429 int eccsteps = chip->ecc.steps;
430 uint8_t *p = buf;
431 uint8_t *ecc_calc = chip->buffers->ecccalc;
432 uint8_t *ecc_code = chip->buffers->ecccode;
433 int off, len, group = 0;
434 /*
435 * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
436 * end up reading 14 bytes (7 words) from oob. The local array is
437 * to maintain word alignment
438 */
439 uint16_t ecc_oob[7];
440 uint8_t *oob = (uint8_t *)&ecc_oob[0];
441
442 for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200443 chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
444 chip->ecc.hwctl(mtd, NAND_ECC_READ);
445 chip->read_buf(mtd, p, eccsize);
446
447 for (j = 0; j < eccbytes;) {
448 off = ecc_place->eccplace[group].offset;
449 len = ecc_place->eccplace[group].length;
450 group++;
451
452 /*
453 * length is intentionally kept a higher multiple of 2
454 * to read at least 13 bytes even in case of 16 bit NAND
455 * devices
456 */
457 len = roundup(len, 2);
458 chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
459 chip->read_buf(mtd, oob + j, len);
460 j += len;
461 }
462
Vipin Kumar519300c2012-03-07 17:00:49 +0530463 memcpy(&ecc_code[i], oob, chip->ecc.bytes);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200464 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
465
466 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
467 if (stat < 0)
468 mtd->ecc_stats.failed++;
469 else
470 mtd->ecc_stats.corrected += stat;
471 }
472
473 return 0;
474}
475
476/*
477 * fsmc_correct_data
478 * @mtd: mtd info structure
479 * @dat: buffer of read data
480 * @read_ecc: ecc read from device spare area
481 * @calc_ecc: ecc calculated from read data
482 *
483 * calc_ecc is a 104 bit information containing maximum of 8 error
484 * offset informations of 13 bits each in 512 bytes of read data.
485 */
486static int fsmc_correct_data(struct mtd_info *mtd, uint8_t *dat,
487 uint8_t *read_ecc, uint8_t *calc_ecc)
488{
489 struct fsmc_nand_data *host = container_of(mtd,
490 struct fsmc_nand_data, mtd);
Vipin Kumar519300c2012-03-07 17:00:49 +0530491 struct nand_chip *chip = mtd->priv;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200492 struct fsmc_regs *regs = host->regs_va;
493 unsigned int bank = host->bank;
494 uint16_t err_idx[8];
495 uint64_t ecc_data[2];
496 uint32_t num_err, i;
497
Vipin Kumar519300c2012-03-07 17:00:49 +0530498 num_err = (readl(&regs->bank_regs[bank].sts) >> 10) & 0xF;
499
500 /* no bit flipping */
501 if (likely(num_err == 0))
502 return 0;
503
504 /* too many errors */
505 if (unlikely(num_err > 8)) {
506 /*
507 * This is a temporary erase check. A newly erased page read
508 * would result in an ecc error because the oob data is also
509 * erased to FF and the calculated ecc for an FF data is not
510 * FF..FF.
511 * This is a workaround to skip performing correction in case
512 * data is FF..FF
513 *
514 * Logic:
515 * For every page, each bit written as 0 is counted until these
516 * number of bits are greater than 8 (the maximum correction
517 * capability of FSMC for each 512 + 13 bytes)
518 */
519
520 int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
521 int bits_data = count_written_bits(dat, chip->ecc.size, 8);
522
523 if ((bits_ecc + bits_data) <= 8) {
524 if (bits_data)
525 memset(dat, 0xff, chip->ecc.size);
526 return bits_data;
527 }
528
529 return -EBADMSG;
530 }
531
Linus Walleij6c009ab2010-09-13 00:35:22 +0200532 /* The calculated ecc is actually the correction index in data */
Vipin Kumar519300c2012-03-07 17:00:49 +0530533 memcpy(ecc_data, calc_ecc, chip->ecc.bytes);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200534
535 /*
536 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
537 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
538 *
539 * calc_ecc is a 104 bit information containing maximum of 8 error
540 * offset informations of 13 bits each. calc_ecc is copied into a
541 * uint64_t array and error offset indexes are populated in err_idx
542 * array
543 */
544 for (i = 0; i < 8; i++) {
545 if (i == 4) {
546 err_idx[4] = ((ecc_data[1] & 0x1) << 12) | ecc_data[0];
547 ecc_data[1] >>= 1;
548 continue;
549 }
550 err_idx[i] = (ecc_data[i/4] & 0x1FFF);
551 ecc_data[i/4] >>= 13;
552 }
553
554 num_err = (readl(&regs->bank_regs[bank].sts) >> 10) & 0xF;
555
556 if (num_err == 0xF)
557 return -EBADMSG;
558
559 i = 0;
560 while (num_err--) {
561 change_bit(0, (unsigned long *)&err_idx[i]);
562 change_bit(1, (unsigned long *)&err_idx[i]);
563
Vipin Kumar519300c2012-03-07 17:00:49 +0530564 if (err_idx[i] <= chip->ecc.size * 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200565 change_bit(err_idx[i], (unsigned long *)dat);
566 i++;
567 }
568 }
569 return i;
570}
571
572/*
573 * fsmc_nand_probe - Probe function
574 * @pdev: platform device structure
575 */
576static int __init fsmc_nand_probe(struct platform_device *pdev)
577{
578 struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
579 struct fsmc_nand_data *host;
580 struct mtd_info *mtd;
581 struct nand_chip *nand;
582 struct fsmc_regs *regs;
583 struct resource *res;
Linus Walleij4ad916b2010-11-29 13:52:06 +0100584 int ret = 0;
Linus Walleij593cd872010-11-29 13:52:19 +0100585 u32 pid;
586 int i;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200587
588 if (!pdata) {
589 dev_err(&pdev->dev, "platform data is NULL\n");
590 return -EINVAL;
591 }
592
593 /* Allocate memory for the device structure (and zero it) */
594 host = kzalloc(sizeof(*host), GFP_KERNEL);
595 if (!host) {
596 dev_err(&pdev->dev, "failed to allocate device structure\n");
597 return -ENOMEM;
598 }
599
600 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
601 if (!res) {
602 ret = -EIO;
603 goto err_probe1;
604 }
605
606 host->resdata = request_mem_region(res->start, resource_size(res),
607 pdev->name);
608 if (!host->resdata) {
609 ret = -EIO;
610 goto err_probe1;
611 }
612
613 host->data_va = ioremap(res->start, resource_size(res));
614 if (!host->data_va) {
615 ret = -EIO;
616 goto err_probe1;
617 }
618
619 host->resaddr = request_mem_region(res->start + PLAT_NAND_ALE,
620 resource_size(res), pdev->name);
621 if (!host->resaddr) {
622 ret = -EIO;
623 goto err_probe1;
624 }
625
626 host->addr_va = ioremap(res->start + PLAT_NAND_ALE, resource_size(res));
627 if (!host->addr_va) {
628 ret = -EIO;
629 goto err_probe1;
630 }
631
632 host->rescmd = request_mem_region(res->start + PLAT_NAND_CLE,
633 resource_size(res), pdev->name);
634 if (!host->rescmd) {
635 ret = -EIO;
636 goto err_probe1;
637 }
638
639 host->cmd_va = ioremap(res->start + PLAT_NAND_CLE, resource_size(res));
640 if (!host->cmd_va) {
641 ret = -EIO;
642 goto err_probe1;
643 }
644
645 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
646 if (!res) {
647 ret = -EIO;
648 goto err_probe1;
649 }
650
651 host->resregs = request_mem_region(res->start, resource_size(res),
652 pdev->name);
653 if (!host->resregs) {
654 ret = -EIO;
655 goto err_probe1;
656 }
657
658 host->regs_va = ioremap(res->start, resource_size(res));
659 if (!host->regs_va) {
660 ret = -EIO;
661 goto err_probe1;
662 }
663
664 host->clk = clk_get(&pdev->dev, NULL);
665 if (IS_ERR(host->clk)) {
666 dev_err(&pdev->dev, "failed to fetch block clock\n");
667 ret = PTR_ERR(host->clk);
668 host->clk = NULL;
669 goto err_probe1;
670 }
671
672 ret = clk_enable(host->clk);
673 if (ret)
674 goto err_probe1;
675
Linus Walleij593cd872010-11-29 13:52:19 +0100676 /*
677 * This device ID is actually a common AMBA ID as used on the
678 * AMBA PrimeCell bus. However it is not a PrimeCell.
679 */
680 for (pid = 0, i = 0; i < 4; i++)
681 pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
682 host->pid = pid;
683 dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
684 "revision %02x, config %02x\n",
685 AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
686 AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
687
Linus Walleij6c009ab2010-09-13 00:35:22 +0200688 host->bank = pdata->bank;
689 host->select_chip = pdata->select_bank;
690 regs = host->regs_va;
691
692 /* Link all private pointers */
693 mtd = &host->mtd;
694 nand = &host->nand;
695 mtd->priv = nand;
696 nand->priv = host;
697
698 host->mtd.owner = THIS_MODULE;
699 nand->IO_ADDR_R = host->data_va;
700 nand->IO_ADDR_W = host->data_va;
701 nand->cmd_ctrl = fsmc_cmd_ctrl;
702 nand->chip_delay = 30;
703
704 nand->ecc.mode = NAND_ECC_HW;
705 nand->ecc.hwctl = fsmc_enable_hwecc;
706 nand->ecc.size = 512;
707 nand->options = pdata->options;
708 nand->select_chip = fsmc_select_chip;
709
710 if (pdata->width == FSMC_NAND_BW16)
711 nand->options |= NAND_BUSWIDTH_16;
712
713 fsmc_nand_setup(regs, host->bank, nand->options & NAND_BUSWIDTH_16);
714
Linus Walleij593cd872010-11-29 13:52:19 +0100715 if (AMBA_REV_BITS(host->pid) >= 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200716 nand->ecc.read_page = fsmc_read_page_hwecc;
717 nand->ecc.calculate = fsmc_read_hwecc_ecc4;
718 nand->ecc.correct = fsmc_correct_data;
719 nand->ecc.bytes = 13;
720 } else {
721 nand->ecc.calculate = fsmc_read_hwecc_ecc1;
722 nand->ecc.correct = nand_correct_data;
723 nand->ecc.bytes = 3;
724 }
725
726 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300727 * Scan to find existence of the device
Linus Walleij6c009ab2010-09-13 00:35:22 +0200728 */
729 if (nand_scan_ident(&host->mtd, 1, NULL)) {
730 ret = -ENXIO;
731 dev_err(&pdev->dev, "No NAND Device found!\n");
732 goto err_probe;
733 }
734
Linus Walleij593cd872010-11-29 13:52:19 +0100735 if (AMBA_REV_BITS(host->pid) >= 8) {
Linus Walleij6c009ab2010-09-13 00:35:22 +0200736 if (host->mtd.writesize == 512) {
737 nand->ecc.layout = &fsmc_ecc4_sp_layout;
738 host->ecc_place = &fsmc_ecc4_sp_place;
739 } else {
740 nand->ecc.layout = &fsmc_ecc4_lp_layout;
741 host->ecc_place = &fsmc_ecc4_lp_place;
742 }
743 } else {
744 nand->ecc.layout = &fsmc_ecc1_layout;
745 }
746
747 /* Second stage of scan to fill MTD data-structures */
748 if (nand_scan_tail(&host->mtd)) {
749 ret = -ENXIO;
750 goto err_probe;
751 }
752
753 /*
754 * The partition information can is accessed by (in the same precedence)
755 *
756 * command line through Bootloader,
757 * platform data,
758 * default partition information present in driver.
759 */
Linus Walleij6c009ab2010-09-13 00:35:22 +0200760 /*
Dmitry Eremin-Solenikov8d3f8bb2011-05-29 20:16:57 +0400761 * Check for partition info passed
Linus Walleij6c009ab2010-09-13 00:35:22 +0200762 */
763 host->mtd.name = "nand";
Dmitry Eremin-Solenikov0d04eda2011-06-02 18:00:38 +0400764 ret = mtd_device_parse_register(&host->mtd, NULL, 0,
765 host->mtd.size <= 0x04000000 ?
766 partition_info_16KB_blk :
767 partition_info_128KB_blk,
768 host->mtd.size <= 0x04000000 ?
769 ARRAY_SIZE(partition_info_16KB_blk) :
770 ARRAY_SIZE(partition_info_128KB_blk));
Jamie Iles99335d02011-05-23 10:23:23 +0100771 if (ret)
Linus Walleij6c009ab2010-09-13 00:35:22 +0200772 goto err_probe;
Linus Walleij6c009ab2010-09-13 00:35:22 +0200773
774 platform_set_drvdata(pdev, host);
775 dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
776 return 0;
777
778err_probe:
779 clk_disable(host->clk);
780err_probe1:
781 if (host->clk)
782 clk_put(host->clk);
783 if (host->regs_va)
784 iounmap(host->regs_va);
785 if (host->resregs)
786 release_mem_region(host->resregs->start,
787 resource_size(host->resregs));
788 if (host->cmd_va)
789 iounmap(host->cmd_va);
790 if (host->rescmd)
791 release_mem_region(host->rescmd->start,
792 resource_size(host->rescmd));
793 if (host->addr_va)
794 iounmap(host->addr_va);
795 if (host->resaddr)
796 release_mem_region(host->resaddr->start,
797 resource_size(host->resaddr));
798 if (host->data_va)
799 iounmap(host->data_va);
800 if (host->resdata)
801 release_mem_region(host->resdata->start,
802 resource_size(host->resdata));
803
804 kfree(host);
805 return ret;
806}
807
808/*
809 * Clean up routine
810 */
811static int fsmc_nand_remove(struct platform_device *pdev)
812{
813 struct fsmc_nand_data *host = platform_get_drvdata(pdev);
814
815 platform_set_drvdata(pdev, NULL);
816
817 if (host) {
Axel Lin82e023a2011-06-03 13:15:30 +0800818 nand_release(&host->mtd);
Linus Walleij6c009ab2010-09-13 00:35:22 +0200819 clk_disable(host->clk);
820 clk_put(host->clk);
821
822 iounmap(host->regs_va);
823 release_mem_region(host->resregs->start,
824 resource_size(host->resregs));
825 iounmap(host->cmd_va);
826 release_mem_region(host->rescmd->start,
827 resource_size(host->rescmd));
828 iounmap(host->addr_va);
829 release_mem_region(host->resaddr->start,
830 resource_size(host->resaddr));
831 iounmap(host->data_va);
832 release_mem_region(host->resdata->start,
833 resource_size(host->resdata));
834
835 kfree(host);
836 }
837 return 0;
838}
839
840#ifdef CONFIG_PM
841static int fsmc_nand_suspend(struct device *dev)
842{
843 struct fsmc_nand_data *host = dev_get_drvdata(dev);
844 if (host)
845 clk_disable(host->clk);
846 return 0;
847}
848
849static int fsmc_nand_resume(struct device *dev)
850{
851 struct fsmc_nand_data *host = dev_get_drvdata(dev);
852 if (host)
853 clk_enable(host->clk);
854 return 0;
855}
856
857static const struct dev_pm_ops fsmc_nand_pm_ops = {
858 .suspend = fsmc_nand_suspend,
859 .resume = fsmc_nand_resume,
860};
861#endif
862
863static struct platform_driver fsmc_nand_driver = {
864 .remove = fsmc_nand_remove,
865 .driver = {
866 .owner = THIS_MODULE,
867 .name = "fsmc-nand",
868#ifdef CONFIG_PM
869 .pm = &fsmc_nand_pm_ops,
870#endif
871 },
872};
873
874static int __init fsmc_nand_init(void)
875{
876 return platform_driver_probe(&fsmc_nand_driver,
877 fsmc_nand_probe);
878}
879module_init(fsmc_nand_init);
880
881static void __exit fsmc_nand_exit(void)
882{
883 platform_driver_unregister(&fsmc_nand_driver);
884}
885module_exit(fsmc_nand_exit);
886
887MODULE_LICENSE("GPL");
888MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
889MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");