]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/mtd/nand/bf5xx_nand.c
mtd: Blackfin NFC: wait for the ECC reset to finish
[net-next-2.6.git] / drivers / mtd / nand / bf5xx_nand.c
CommitLineData
b37bde14
BW
1/* linux/drivers/mtd/nand/bf5xx_nand.c
2 *
afc4bca6 3 * Copyright 2006-2008 Analog Devices Inc.
b37bde14
BW
4 * http://blackfin.uclinux.org/
5 * Bryan Wu <bryan.wu@analog.com>
6 *
8e87d782 7 * Blackfin BF5xx on-chip NAND flash controller driver
b37bde14
BW
8 *
9 * Derived from drivers/mtd/nand/s3c2410.c
10 * Copyright (c) 2007 Ben Dooks <ben@simtec.co.uk>
11 *
12 * Derived from drivers/mtd/nand/cafe.c
13 * Copyright © 2006 Red Hat, Inc.
14 * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
15 *
16 * Changelog:
17 * 12-Jun-2007 Bryan Wu: Initial version
18 * 18-Jul-2007 Bryan Wu:
19 * - ECC_HW and ECC_SW supported
20 * - DMA supported in ECC_HW
21 * - YAFFS tested as rootfs in both ECC_HW and ECC_SW
22 *
b37bde14
BW
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36*/
37
38#include <linux/module.h>
39#include <linux/types.h>
40#include <linux/init.h>
41#include <linux/kernel.h>
42#include <linux/string.h>
43#include <linux/ioport.h>
44#include <linux/platform_device.h>
45#include <linux/delay.h>
46#include <linux/dma-mapping.h>
47#include <linux/err.h>
48#include <linux/slab.h>
49#include <linux/io.h>
50#include <linux/bitops.h>
51
52#include <linux/mtd/mtd.h>
53#include <linux/mtd/nand.h>
54#include <linux/mtd/nand_ecc.h>
55#include <linux/mtd/partitions.h>
56
57#include <asm/blackfin.h>
58#include <asm/dma.h>
59#include <asm/cacheflush.h>
60#include <asm/nand.h>
61#include <asm/portmux.h>
62
63#define DRV_NAME "bf5xx-nand"
64#define DRV_VERSION "1.2"
65#define DRV_AUTHOR "Bryan Wu <bryan.wu@analog.com>"
66#define DRV_DESC "BF5xx on-chip NAND FLash Controller Driver"
67
ac39ee30
MF
68/* NFC_STAT Masks */
69#define NBUSY 0x01 /* Not Busy */
70#define WB_FULL 0x02 /* Write Buffer Full */
71#define PG_WR_STAT 0x04 /* Page Write Pending */
72#define PG_RD_STAT 0x08 /* Page Read Pending */
73#define WB_EMPTY 0x10 /* Write Buffer Empty */
74
75/* NFC_IRQSTAT Masks */
76#define NBUSYIRQ 0x01 /* Not Busy IRQ */
77#define WB_OVF 0x02 /* Write Buffer Overflow */
78#define WB_EDGE 0x04 /* Write Buffer Edge Detect */
79#define RD_RDY 0x08 /* Read Data Ready */
80#define WR_DONE 0x10 /* Page Write Done */
81
82/* NFC_RST Masks */
83#define ECC_RST 0x01 /* ECC (and NFC counters) Reset */
84
85/* NFC_PGCTL Masks */
86#define PG_RD_START 0x01 /* Page Read Start */
87#define PG_WR_START 0x02 /* Page Write Start */
88
b37bde14
BW
89#ifdef CONFIG_MTD_NAND_BF5XX_HWECC
90static int hardware_ecc = 1;
91#else
92static int hardware_ecc;
93#endif
94
afc4bca6 95static const unsigned short bfin_nfc_pin_req[] =
a25b7fee
MH
96 {P_NAND_CE,
97 P_NAND_RB,
98 P_NAND_D0,
99 P_NAND_D1,
100 P_NAND_D2,
101 P_NAND_D3,
102 P_NAND_D4,
103 P_NAND_D5,
104 P_NAND_D6,
105 P_NAND_D7,
106 P_NAND_WE,
107 P_NAND_RE,
108 P_NAND_CLE,
109 P_NAND_ALE,
110 0};
b37bde14 111
fcb90ba7
MF
112#ifdef CONFIG_MTD_NAND_BF5XX_BOOTROM_ECC
113static uint8_t bbt_pattern[] = { 0xff };
114
115static struct nand_bbt_descr bootrom_bbt = {
116 .options = 0,
117 .offs = 63,
118 .len = 1,
119 .pattern = bbt_pattern,
120};
121
122static struct nand_ecclayout bootrom_ecclayout = {
123 .eccbytes = 24,
124 .eccpos = {
125 0x8 * 0, 0x8 * 0 + 1, 0x8 * 0 + 2,
126 0x8 * 1, 0x8 * 1 + 1, 0x8 * 1 + 2,
127 0x8 * 2, 0x8 * 2 + 1, 0x8 * 2 + 2,
128 0x8 * 3, 0x8 * 3 + 1, 0x8 * 3 + 2,
129 0x8 * 4, 0x8 * 4 + 1, 0x8 * 4 + 2,
130 0x8 * 5, 0x8 * 5 + 1, 0x8 * 5 + 2,
131 0x8 * 6, 0x8 * 6 + 1, 0x8 * 6 + 2,
132 0x8 * 7, 0x8 * 7 + 1, 0x8 * 7 + 2
133 },
134 .oobfree = {
135 { 0x8 * 0 + 3, 5 },
136 { 0x8 * 1 + 3, 5 },
137 { 0x8 * 2 + 3, 5 },
138 { 0x8 * 3 + 3, 5 },
139 { 0x8 * 4 + 3, 5 },
140 { 0x8 * 5 + 3, 5 },
141 { 0x8 * 6 + 3, 5 },
142 { 0x8 * 7 + 3, 5 },
143 }
144};
145#endif
146
b37bde14
BW
147/*
148 * Data structures for bf5xx nand flash controller driver
149 */
150
151/* bf5xx nand info */
152struct bf5xx_nand_info {
153 /* mtd info */
154 struct nand_hw_control controller;
155 struct mtd_info mtd;
156 struct nand_chip chip;
157
158 /* platform info */
159 struct bf5xx_nand_platform *platform;
160
161 /* device info */
162 struct device *device;
163
164 /* DMA stuff */
165 struct completion dma_completion;
166};
167
168/*
169 * Conversion functions
170 */
171static struct bf5xx_nand_info *mtd_to_nand_info(struct mtd_info *mtd)
172{
173 return container_of(mtd, struct bf5xx_nand_info, mtd);
174}
175
176static struct bf5xx_nand_info *to_nand_info(struct platform_device *pdev)
177{
178 return platform_get_drvdata(pdev);
179}
180
181static struct bf5xx_nand_platform *to_nand_plat(struct platform_device *pdev)
182{
183 return pdev->dev.platform_data;
184}
185
186/*
187 * struct nand_chip interface function pointers
188 */
189
190/*
191 * bf5xx_nand_hwcontrol
192 *
193 * Issue command and address cycles to the chip
194 */
195static void bf5xx_nand_hwcontrol(struct mtd_info *mtd, int cmd,
196 unsigned int ctrl)
197{
198 if (cmd == NAND_CMD_NONE)
199 return;
200
201 while (bfin_read_NFC_STAT() & WB_FULL)
202 cpu_relax();
203
204 if (ctrl & NAND_CLE)
205 bfin_write_NFC_CMD(cmd);
fd508da2 206 else if (ctrl & NAND_ALE)
b37bde14
BW
207 bfin_write_NFC_ADDR(cmd);
208 SSYNC();
209}
210
211/*
212 * bf5xx_nand_devready()
213 *
214 * returns 0 if the nand is busy, 1 if it is ready
215 */
216static int bf5xx_nand_devready(struct mtd_info *mtd)
217{
218 unsigned short val = bfin_read_NFC_IRQSTAT();
219
220 if ((val & NBUSYIRQ) == NBUSYIRQ)
221 return 1;
222 else
223 return 0;
224}
225
226/*
227 * ECC functions
228 * These allow the bf5xx to use the controller's ECC
229 * generator block to ECC the data as it passes through
230 */
231
232/*
233 * ECC error correction function
234 */
235static int bf5xx_nand_correct_data_256(struct mtd_info *mtd, u_char *dat,
236 u_char *read_ecc, u_char *calc_ecc)
237{
238 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
239 u32 syndrome[5];
240 u32 calced, stored;
241 int i;
242 unsigned short failing_bit, failing_byte;
243 u_char data;
244
245 calced = calc_ecc[0] | (calc_ecc[1] << 8) | (calc_ecc[2] << 16);
246 stored = read_ecc[0] | (read_ecc[1] << 8) | (read_ecc[2] << 16);
247
248 syndrome[0] = (calced ^ stored);
249
250 /*
251 * syndrome 0: all zero
252 * No error in data
253 * No action
254 */
255 if (!syndrome[0] || !calced || !stored)
256 return 0;
257
258 /*
259 * sysdrome 0: only one bit is one
260 * ECC data was incorrect
261 * No action
262 */
263 if (hweight32(syndrome[0]) == 1) {
264 dev_err(info->device, "ECC data was incorrect!\n");
265 return 1;
266 }
267
268 syndrome[1] = (calced & 0x7FF) ^ (stored & 0x7FF);
269 syndrome[2] = (calced & 0x7FF) ^ ((calced >> 11) & 0x7FF);
270 syndrome[3] = (stored & 0x7FF) ^ ((stored >> 11) & 0x7FF);
271 syndrome[4] = syndrome[2] ^ syndrome[3];
272
273 for (i = 0; i < 5; i++)
274 dev_info(info->device, "syndrome[%d] 0x%08x\n", i, syndrome[i]);
275
276 dev_info(info->device,
277 "calced[0x%08x], stored[0x%08x]\n",
278 calced, stored);
279
280 /*
281 * sysdrome 0: exactly 11 bits are one, each parity
282 * and parity' pair is 1 & 0 or 0 & 1.
283 * 1-bit correctable error
284 * Correct the error
285 */
286 if (hweight32(syndrome[0]) == 11 && syndrome[4] == 0x7FF) {
287 dev_info(info->device,
288 "1-bit correctable error, correct it.\n");
289 dev_info(info->device,
290 "syndrome[1] 0x%08x\n", syndrome[1]);
291
292 failing_bit = syndrome[1] & 0x7;
293 failing_byte = syndrome[1] >> 0x3;
294 data = *(dat + failing_byte);
295 data = data ^ (0x1 << failing_bit);
296 *(dat + failing_byte) = data;
297
298 return 0;
299 }
300
301 /*
302 * sysdrome 0: random data
303 * More than 1-bit error, non-correctable error
304 * Discard data, mark bad block
305 */
306 dev_err(info->device,
307 "More than 1-bit error, non-correctable error.\n");
308 dev_err(info->device,
309 "Please discard data, mark bad block\n");
310
311 return 1;
312}
313
314static int bf5xx_nand_correct_data(struct mtd_info *mtd, u_char *dat,
315 u_char *read_ecc, u_char *calc_ecc)
316{
317 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
318 struct bf5xx_nand_platform *plat = info->platform;
319 unsigned short page_size = (plat->page_size ? 512 : 256);
320 int ret;
321
322 ret = bf5xx_nand_correct_data_256(mtd, dat, read_ecc, calc_ecc);
323
324 /* If page size is 512, correct second 256 bytes */
325 if (page_size == 512) {
326 dat += 256;
327 read_ecc += 8;
328 calc_ecc += 8;
e274f025 329 ret |= bf5xx_nand_correct_data_256(mtd, dat, read_ecc, calc_ecc);
b37bde14
BW
330 }
331
332 return ret;
333}
334
335static void bf5xx_nand_enable_hwecc(struct mtd_info *mtd, int mode)
336{
337 return;
338}
339
340static int bf5xx_nand_calculate_ecc(struct mtd_info *mtd,
341 const u_char *dat, u_char *ecc_code)
342{
343 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
344 struct bf5xx_nand_platform *plat = info->platform;
345 u16 page_size = (plat->page_size ? 512 : 256);
346 u16 ecc0, ecc1;
347 u32 code[2];
348 u8 *p;
b37bde14
BW
349
350 /* first 4 bytes ECC code for 256 page size */
351 ecc0 = bfin_read_NFC_ECC0();
352 ecc1 = bfin_read_NFC_ECC1();
353
cf840392 354 code[0] = (ecc0 & 0x7ff) | ((ecc1 & 0x7ff) << 11);
b37bde14
BW
355
356 dev_dbg(info->device, "returning ecc 0x%08x\n", code[0]);
357
5eb91034
BW
358 /* first 3 bytes in ecc_code for 256 page size */
359 p = (u8 *) code;
360 memcpy(ecc_code, p, 3);
361
b37bde14
BW
362 /* second 4 bytes ECC code for 512 page size */
363 if (page_size == 512) {
364 ecc0 = bfin_read_NFC_ECC2();
365 ecc1 = bfin_read_NFC_ECC3();
cf840392 366 code[1] = (ecc0 & 0x7ff) | ((ecc1 & 0x7ff) << 11);
5eb91034
BW
367
368 /* second 3 bytes in ecc_code for second 256
369 * bytes of 512 page size
370 */
371 p = (u8 *) (code + 1);
372 memcpy((ecc_code + 3), p, 3);
b37bde14
BW
373 dev_dbg(info->device, "returning ecc 0x%08x\n", code[1]);
374 }
375
b37bde14
BW
376 return 0;
377}
378
379/*
380 * PIO mode for buffer writing and reading
381 */
382static void bf5xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
383{
384 int i;
385 unsigned short val;
386
387 /*
388 * Data reads are requested by first writing to NFC_DATA_RD
389 * and then reading back from NFC_READ.
390 */
391 for (i = 0; i < len; i++) {
392 while (bfin_read_NFC_STAT() & WB_FULL)
393 cpu_relax();
394
395 /* Contents do not matter */
396 bfin_write_NFC_DATA_RD(0x0000);
397 SSYNC();
398
399 while ((bfin_read_NFC_IRQSTAT() & RD_RDY) != RD_RDY)
400 cpu_relax();
401
402 buf[i] = bfin_read_NFC_READ();
403
404 val = bfin_read_NFC_IRQSTAT();
405 val |= RD_RDY;
406 bfin_write_NFC_IRQSTAT(val);
407 SSYNC();
408 }
409}
410
411static uint8_t bf5xx_nand_read_byte(struct mtd_info *mtd)
412{
413 uint8_t val;
414
415 bf5xx_nand_read_buf(mtd, &val, 1);
416
417 return val;
418}
419
420static void bf5xx_nand_write_buf(struct mtd_info *mtd,
421 const uint8_t *buf, int len)
422{
423 int i;
424
425 for (i = 0; i < len; i++) {
426 while (bfin_read_NFC_STAT() & WB_FULL)
427 cpu_relax();
428
429 bfin_write_NFC_DATA_WR(buf[i]);
430 SSYNC();
431 }
432}
433
434static void bf5xx_nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
435{
436 int i;
437 u16 *p = (u16 *) buf;
438 len >>= 1;
439
440 /*
441 * Data reads are requested by first writing to NFC_DATA_RD
442 * and then reading back from NFC_READ.
443 */
444 bfin_write_NFC_DATA_RD(0x5555);
445
446 SSYNC();
447
448 for (i = 0; i < len; i++)
449 p[i] = bfin_read_NFC_READ();
450}
451
452static void bf5xx_nand_write_buf16(struct mtd_info *mtd,
453 const uint8_t *buf, int len)
454{
455 int i;
456 u16 *p = (u16 *) buf;
457 len >>= 1;
458
459 for (i = 0; i < len; i++)
460 bfin_write_NFC_DATA_WR(p[i]);
461
462 SSYNC();
463}
464
465/*
466 * DMA functions for buffer writing and reading
467 */
468static irqreturn_t bf5xx_nand_dma_irq(int irq, void *dev_id)
469{
470 struct bf5xx_nand_info *info = dev_id;
471
472 clear_dma_irqstat(CH_NFC);
473 disable_dma(CH_NFC);
474 complete(&info->dma_completion);
475
476 return IRQ_HANDLED;
477}
478
530c3b60 479static void bf5xx_nand_dma_rw(struct mtd_info *mtd,
b37bde14
BW
480 uint8_t *buf, int is_read)
481{
482 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
483 struct bf5xx_nand_platform *plat = info->platform;
484 unsigned short page_size = (plat->page_size ? 512 : 256);
485 unsigned short val;
486
487 dev_dbg(info->device, " mtd->%p, buf->%p, is_read %d\n",
488 mtd, buf, is_read);
489
490 /*
491 * Before starting a dma transfer, be sure to invalidate/flush
492 * the cache over the address range of your DMA buffer to
493 * prevent cache coherency problems. Otherwise very subtle bugs
494 * can be introduced to your driver.
495 */
496 if (is_read)
497 invalidate_dcache_range((unsigned int)buf,
498 (unsigned int)(buf + page_size));
499 else
500 flush_dcache_range((unsigned int)buf,
501 (unsigned int)(buf + page_size));
502
503 /*
504 * This register must be written before each page is
505 * transferred to generate the correct ECC register
506 * values.
507 */
ac39ee30 508 bfin_write_NFC_RST(ECC_RST);
b37bde14 509 SSYNC();
752b957a
BS
510 while (bfin_read_NFC_RST() & ECC_RST)
511 cpu_relax();
b37bde14
BW
512
513 disable_dma(CH_NFC);
514 clear_dma_irqstat(CH_NFC);
515
516 /* setup DMA register with Blackfin DMA API */
517 set_dma_config(CH_NFC, 0x0);
518 set_dma_start_addr(CH_NFC, (unsigned long) buf);
c3a9f356 519
ac39ee30 520 /* The DMAs have different size on BF52x and BF54x */
c3a9f356
CC
521#ifdef CONFIG_BF52x
522 set_dma_x_count(CH_NFC, (page_size >> 1));
523 set_dma_x_modify(CH_NFC, 2);
524 val = DI_EN | WDSIZE_16;
525#endif
526
527#ifdef CONFIG_BF54x
b37bde14
BW
528 set_dma_x_count(CH_NFC, (page_size >> 2));
529 set_dma_x_modify(CH_NFC, 4);
b37bde14 530 val = DI_EN | WDSIZE_32;
c3a9f356
CC
531#endif
532 /* setup write or read operation */
b37bde14
BW
533 if (is_read)
534 val |= WNR;
535 set_dma_config(CH_NFC, val);
536 enable_dma(CH_NFC);
537
538 /* Start PAGE read/write operation */
539 if (is_read)
ac39ee30 540 bfin_write_NFC_PGCTL(PG_RD_START);
b37bde14 541 else
ac39ee30 542 bfin_write_NFC_PGCTL(PG_WR_START);
b37bde14 543 wait_for_completion(&info->dma_completion);
b37bde14
BW
544}
545
546static void bf5xx_nand_dma_read_buf(struct mtd_info *mtd,
547 uint8_t *buf, int len)
548{
549 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
550 struct bf5xx_nand_platform *plat = info->platform;
551 unsigned short page_size = (plat->page_size ? 512 : 256);
552
553 dev_dbg(info->device, "mtd->%p, buf->%p, int %d\n", mtd, buf, len);
554
555 if (len == page_size)
556 bf5xx_nand_dma_rw(mtd, buf, 1);
557 else
558 bf5xx_nand_read_buf(mtd, buf, len);
559}
560
561static void bf5xx_nand_dma_write_buf(struct mtd_info *mtd,
562 const uint8_t *buf, int len)
563{
564 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
565 struct bf5xx_nand_platform *plat = info->platform;
566 unsigned short page_size = (plat->page_size ? 512 : 256);
567
568 dev_dbg(info->device, "mtd->%p, buf->%p, len %d\n", mtd, buf, len);
569
570 if (len == page_size)
571 bf5xx_nand_dma_rw(mtd, (uint8_t *)buf, 0);
572 else
573 bf5xx_nand_write_buf(mtd, buf, len);
574}
575
085d45fb
BS
576static int bf5xx_nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
577 uint8_t *buf, int page)
578{
579 bf5xx_nand_read_buf(mtd, buf, mtd->writesize);
580 bf5xx_nand_read_buf(mtd, chip->oob_poi, mtd->oobsize);
581
582 return 0;
583}
584
585static void bf5xx_nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
586 const uint8_t *buf)
587{
588 bf5xx_nand_write_buf(mtd, buf, mtd->writesize);
589 bf5xx_nand_write_buf(mtd, chip->oob_poi, mtd->oobsize);
590}
591
b37bde14
BW
592/*
593 * System initialization functions
594 */
b37bde14
BW
595static int bf5xx_nand_dma_init(struct bf5xx_nand_info *info)
596{
597 int ret;
b37bde14
BW
598
599 /* Do not use dma */
600 if (!hardware_ecc)
601 return 0;
602
603 init_completion(&info->dma_completion);
604
b37bde14
BW
605 /* Request NFC DMA channel */
606 ret = request_dma(CH_NFC, "BF5XX NFC driver");
607 if (ret < 0) {
608 dev_err(info->device, " unable to get DMA channel\n");
609 return ret;
610 }
611
08d2503e
MF
612#ifdef CONFIG_BF54x
613 /* Setup DMAC1 channel mux for NFC which shared with SDH */
614 bfin_write_DMAC1_PERIMUX(bfin_read_DMAC1_PERIMUX() & ~1);
615 SSYNC();
616#endif
617
bfc49257 618 set_dma_callback(CH_NFC, bf5xx_nand_dma_irq, info);
b37bde14
BW
619
620 /* Turn off the DMA channel first */
621 disable_dma(CH_NFC);
622 return 0;
623}
624
4f0ca70e
BW
625static void bf5xx_nand_dma_remove(struct bf5xx_nand_info *info)
626{
627 /* Free NFC DMA channel */
628 if (hardware_ecc)
629 free_dma(CH_NFC);
630}
631
b37bde14
BW
632/*
633 * BF5XX NFC hardware initialization
634 * - pin mux setup
635 * - clear interrupt status
636 */
637static int bf5xx_nand_hw_init(struct bf5xx_nand_info *info)
638{
639 int err = 0;
640 unsigned short val;
641 struct bf5xx_nand_platform *plat = info->platform;
642
643 /* setup NFC_CTL register */
644 dev_info(info->device,
645 "page_size=%d, data_width=%d, wr_dly=%d, rd_dly=%d\n",
646 (plat->page_size ? 512 : 256),
647 (plat->data_width ? 16 : 8),
648 plat->wr_dly, plat->rd_dly);
649
650 val = (plat->page_size << NFC_PG_SIZE_OFFSET) |
651 (plat->data_width << NFC_NWIDTH_OFFSET) |
652 (plat->rd_dly << NFC_RDDLY_OFFSET) |
00355b0b 653 (plat->wr_dly << NFC_WRDLY_OFFSET);
b37bde14
BW
654 dev_dbg(info->device, "NFC_CTL is 0x%04x\n", val);
655
656 bfin_write_NFC_CTL(val);
657 SSYNC();
658
659 /* clear interrupt status */
660 bfin_write_NFC_IRQMASK(0x0);
661 SSYNC();
662 val = bfin_read_NFC_IRQSTAT();
663 bfin_write_NFC_IRQSTAT(val);
664 SSYNC();
665
b37bde14
BW
666 /* DMA initialization */
667 if (bf5xx_nand_dma_init(info))
668 err = -ENXIO;
669
670 return err;
671}
672
673/*
674 * Device management interface
675 */
8d30cab0 676static int __devinit bf5xx_nand_add_partition(struct bf5xx_nand_info *info)
b37bde14
BW
677{
678 struct mtd_info *mtd = &info->mtd;
679
680#ifdef CONFIG_MTD_PARTITIONS
681 struct mtd_partition *parts = info->platform->partitions;
682 int nr = info->platform->nr_partitions;
683
684 return add_mtd_partitions(mtd, parts, nr);
685#else
686 return add_mtd_device(mtd);
687#endif
688}
689
2445af38 690static int __devexit bf5xx_nand_remove(struct platform_device *pdev)
b37bde14
BW
691{
692 struct bf5xx_nand_info *info = to_nand_info(pdev);
693 struct mtd_info *mtd = NULL;
694
695 platform_set_drvdata(pdev, NULL);
696
697 /* first thing we need to do is release all our mtds
698 * and their partitions, then go through freeing the
699 * resources used
700 */
701 mtd = &info->mtd;
702 if (mtd) {
703 nand_release(mtd);
704 kfree(mtd);
705 }
706
707 peripheral_free_list(bfin_nfc_pin_req);
4f0ca70e 708 bf5xx_nand_dma_remove(info);
b37bde14
BW
709
710 /* free the common resources */
711 kfree(info);
712
713 return 0;
714}
715
716/*
717 * bf5xx_nand_probe
718 *
719 * called by device layer when it finds a device matching
720 * one our driver can handled. This code checks to see if
721 * it can allocate all necessary resources then calls the
722 * nand layer to look for devices
723 */
2445af38 724static int __devinit bf5xx_nand_probe(struct platform_device *pdev)
b37bde14
BW
725{
726 struct bf5xx_nand_platform *plat = to_nand_plat(pdev);
727 struct bf5xx_nand_info *info = NULL;
728 struct nand_chip *chip = NULL;
729 struct mtd_info *mtd = NULL;
730 int err = 0;
731
732 dev_dbg(&pdev->dev, "(%p)\n", pdev);
733
4f0ca70e
BW
734 if (!plat) {
735 dev_err(&pdev->dev, "no platform specific information\n");
736 return -EINVAL;
737 }
738
afc4bca6 739 if (peripheral_request_list(bfin_nfc_pin_req, DRV_NAME)) {
0ee002b0 740 dev_err(&pdev->dev, "requesting Peripherals failed\n");
afc4bca6
MH
741 return -EFAULT;
742 }
743
b37bde14
BW
744 info = kzalloc(sizeof(*info), GFP_KERNEL);
745 if (info == NULL) {
746 dev_err(&pdev->dev, "no memory for flash info\n");
747 err = -ENOMEM;
4f0ca70e 748 goto out_err_kzalloc;
b37bde14
BW
749 }
750
751 platform_set_drvdata(pdev, info);
752
753 spin_lock_init(&info->controller.lock);
754 init_waitqueue_head(&info->controller.wq);
755
756 info->device = &pdev->dev;
757 info->platform = plat;
758
759 /* initialise chip data struct */
760 chip = &info->chip;
761
762 if (plat->data_width)
763 chip->options |= NAND_BUSWIDTH_16;
764
765 chip->options |= NAND_CACHEPRG | NAND_SKIP_BBTSCAN;
766
767 chip->read_buf = (plat->data_width) ?
768 bf5xx_nand_read_buf16 : bf5xx_nand_read_buf;
769 chip->write_buf = (plat->data_width) ?
770 bf5xx_nand_write_buf16 : bf5xx_nand_write_buf;
771
772 chip->read_byte = bf5xx_nand_read_byte;
773
774 chip->cmd_ctrl = bf5xx_nand_hwcontrol;
775 chip->dev_ready = bf5xx_nand_devready;
776
777 chip->priv = &info->mtd;
778 chip->controller = &info->controller;
779
780 chip->IO_ADDR_R = (void __iomem *) NFC_READ;
781 chip->IO_ADDR_W = (void __iomem *) NFC_DATA_WR;
782
783 chip->chip_delay = 0;
784
785 /* initialise mtd info data struct */
786 mtd = &info->mtd;
787 mtd->priv = chip;
788 mtd->owner = THIS_MODULE;
789
790 /* initialise the hardware */
791 err = bf5xx_nand_hw_init(info);
4f0ca70e
BW
792 if (err)
793 goto out_err_hw_init;
b37bde14
BW
794
795 /* setup hardware ECC data struct */
796 if (hardware_ecc) {
fcb90ba7
MF
797#ifdef CONFIG_MTD_NAND_BF5XX_BOOTROM_ECC
798 chip->badblock_pattern = &bootrom_bbt;
799 chip->ecc.layout = &bootrom_ecclayout;
800#endif
801
b37bde14
BW
802 if (plat->page_size == NFC_PG_SIZE_256) {
803 chip->ecc.bytes = 3;
804 chip->ecc.size = 256;
805 } else if (plat->page_size == NFC_PG_SIZE_512) {
806 chip->ecc.bytes = 6;
807 chip->ecc.size = 512;
808 }
809
810 chip->read_buf = bf5xx_nand_dma_read_buf;
811 chip->write_buf = bf5xx_nand_dma_write_buf;
812 chip->ecc.calculate = bf5xx_nand_calculate_ecc;
813 chip->ecc.correct = bf5xx_nand_correct_data;
814 chip->ecc.mode = NAND_ECC_HW;
815 chip->ecc.hwctl = bf5xx_nand_enable_hwecc;
085d45fb
BS
816 chip->ecc.read_page_raw = bf5xx_nand_read_page_raw;
817 chip->ecc.write_page_raw = bf5xx_nand_write_page_raw;
b37bde14
BW
818 } else {
819 chip->ecc.mode = NAND_ECC_SOFT;
820 }
821
822 /* scan hardware nand chip and setup mtd info data struct */
823 if (nand_scan(mtd, 1)) {
824 err = -ENXIO;
4f0ca70e 825 goto out_err_nand_scan;
b37bde14
BW
826 }
827
828 /* add NAND partition */
829 bf5xx_nand_add_partition(info);
830
831 dev_dbg(&pdev->dev, "initialised ok\n");
832 return 0;
833
4f0ca70e
BW
834out_err_nand_scan:
835 bf5xx_nand_dma_remove(info);
836out_err_hw_init:
837 platform_set_drvdata(pdev, NULL);
838 kfree(info);
839out_err_kzalloc:
840 peripheral_free_list(bfin_nfc_pin_req);
b37bde14 841
b37bde14
BW
842 return err;
843}
844
845/* PM Support */
846#ifdef CONFIG_PM
847
848static int bf5xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
849{
850 struct bf5xx_nand_info *info = platform_get_drvdata(dev);
851
852 return 0;
853}
854
855static int bf5xx_nand_resume(struct platform_device *dev)
856{
857 struct bf5xx_nand_info *info = platform_get_drvdata(dev);
858
b37bde14
BW
859 return 0;
860}
861
862#else
863#define bf5xx_nand_suspend NULL
864#define bf5xx_nand_resume NULL
865#endif
866
867/* driver device registration */
868static struct platform_driver bf5xx_nand_driver = {
869 .probe = bf5xx_nand_probe,
2445af38 870 .remove = __devexit_p(bf5xx_nand_remove),
b37bde14
BW
871 .suspend = bf5xx_nand_suspend,
872 .resume = bf5xx_nand_resume,
873 .driver = {
874 .name = DRV_NAME,
875 .owner = THIS_MODULE,
876 },
877};
878
879static int __init bf5xx_nand_init(void)
880{
881 printk(KERN_INFO "%s, Version %s (c) 2007 Analog Devices, Inc.\n",
882 DRV_DESC, DRV_VERSION);
883
884 return platform_driver_register(&bf5xx_nand_driver);
885}
886
887static void __exit bf5xx_nand_exit(void)
888{
889 platform_driver_unregister(&bf5xx_nand_driver);
890}
891
892module_init(bf5xx_nand_init);
893module_exit(bf5xx_nand_exit);
894
895MODULE_LICENSE("GPL");
896MODULE_AUTHOR(DRV_AUTHOR);
897MODULE_DESCRIPTION(DRV_DESC);
1ff18422 898MODULE_ALIAS("platform:" DRV_NAME);