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