Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / drivers / mtd / nand / atmel_nand.c
1 /*
2  *  Copyright (C) 2003 Rick Bronson
3  *
4  *  Derived from drivers/mtd/nand/autcpu12.c
5  *       Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
6  *
7  *  Derived from drivers/mtd/spia.c
8  *       Copyright (C) 2000 Steven J. Hill (sjhill@cotw.com)
9  *
10  *
11  *  Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
12  *     Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright (C) 2007
13  *
14  *     Derived from Das U-Boot source code
15  *              (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
16  *     (C) Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
17  *
18  *
19  * This program is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License version 2 as
21  * published by the Free Software Foundation.
22  *
23  */
24
25 #include <linux/slab.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/platform_device.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/nand.h>
31 #include <linux/mtd/partitions.h>
32
33 #include <linux/dmaengine.h>
34 #include <linux/gpio.h>
35 #include <linux/io.h>
36
37 #include <mach/board.h>
38 #include <mach/cpu.h>
39
40 #ifdef CONFIG_MTD_NAND_ATMEL_ECC_HW
41 #define hard_ecc        1
42 #else
43 #define hard_ecc        0
44 #endif
45
46 #ifdef CONFIG_MTD_NAND_ATMEL_ECC_NONE
47 #define no_ecc          1
48 #else
49 #define no_ecc          0
50 #endif
51
52 static int use_dma = 1;
53 module_param(use_dma, int, 0);
54
55 static int on_flash_bbt = 0;
56 module_param(on_flash_bbt, int, 0);
57
58 /* Register access macros */
59 #define ecc_readl(add, reg)                             \
60         __raw_readl(add + ATMEL_ECC_##reg)
61 #define ecc_writel(add, reg, value)                     \
62         __raw_writel((value), add + ATMEL_ECC_##reg)
63
64 #include "atmel_nand_ecc.h"     /* Hardware ECC registers */
65
66 /* oob layout for large page size
67  * bad block info is on bytes 0 and 1
68  * the bytes have to be consecutives to avoid
69  * several NAND_CMD_RNDOUT during read
70  */
71 static struct nand_ecclayout atmel_oobinfo_large = {
72         .eccbytes = 4,
73         .eccpos = {60, 61, 62, 63},
74         .oobfree = {
75                 {2, 58}
76         },
77 };
78
79 /* oob layout for small page size
80  * bad block info is on bytes 4 and 5
81  * the bytes have to be consecutives to avoid
82  * several NAND_CMD_RNDOUT during read
83  */
84 static struct nand_ecclayout atmel_oobinfo_small = {
85         .eccbytes = 4,
86         .eccpos = {0, 1, 2, 3},
87         .oobfree = {
88                 {6, 10}
89         },
90 };
91
92 struct atmel_nand_host {
93         struct nand_chip        nand_chip;
94         struct mtd_info         mtd;
95         void __iomem            *io_base;
96         dma_addr_t              io_phys;
97         struct atmel_nand_data  *board;
98         struct device           *dev;
99         void __iomem            *ecc;
100
101         struct completion       comp;
102         struct dma_chan         *dma_chan;
103 };
104
105 static int cpu_has_dma(void)
106 {
107         return cpu_is_at91sam9rl() || cpu_is_at91sam9g45();
108 }
109
110 /*
111  * Enable NAND.
112  */
113 static void atmel_nand_enable(struct atmel_nand_host *host)
114 {
115         if (host->board->enable_pin)
116                 gpio_set_value(host->board->enable_pin, 0);
117 }
118
119 /*
120  * Disable NAND.
121  */
122 static void atmel_nand_disable(struct atmel_nand_host *host)
123 {
124         if (host->board->enable_pin)
125                 gpio_set_value(host->board->enable_pin, 1);
126 }
127
128 /*
129  * Hardware specific access to control-lines
130  */
131 static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
132 {
133         struct nand_chip *nand_chip = mtd->priv;
134         struct atmel_nand_host *host = nand_chip->priv;
135
136         if (ctrl & NAND_CTRL_CHANGE) {
137                 if (ctrl & NAND_NCE)
138                         atmel_nand_enable(host);
139                 else
140                         atmel_nand_disable(host);
141         }
142         if (cmd == NAND_CMD_NONE)
143                 return;
144
145         if (ctrl & NAND_CLE)
146                 writeb(cmd, host->io_base + (1 << host->board->cle));
147         else
148                 writeb(cmd, host->io_base + (1 << host->board->ale));
149 }
150
151 /*
152  * Read the Device Ready pin.
153  */
154 static int atmel_nand_device_ready(struct mtd_info *mtd)
155 {
156         struct nand_chip *nand_chip = mtd->priv;
157         struct atmel_nand_host *host = nand_chip->priv;
158
159         return gpio_get_value(host->board->rdy_pin) ^
160                 !!host->board->rdy_pin_active_low;
161 }
162
163 /*
164  * Minimal-overhead PIO for data access.
165  */
166 static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
167 {
168         struct nand_chip        *nand_chip = mtd->priv;
169
170         __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
171 }
172
173 static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
174 {
175         struct nand_chip        *nand_chip = mtd->priv;
176
177         __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
178 }
179
180 static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
181 {
182         struct nand_chip        *nand_chip = mtd->priv;
183
184         __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
185 }
186
187 static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
188 {
189         struct nand_chip        *nand_chip = mtd->priv;
190
191         __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
192 }
193
194 static void dma_complete_func(void *completion)
195 {
196         complete(completion);
197 }
198
199 static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
200                                int is_read)
201 {
202         struct dma_device *dma_dev;
203         enum dma_ctrl_flags flags;
204         dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
205         struct dma_async_tx_descriptor *tx = NULL;
206         dma_cookie_t cookie;
207         struct nand_chip *chip = mtd->priv;
208         struct atmel_nand_host *host = chip->priv;
209         void *p = buf;
210         int err = -EIO;
211         enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
212
213         if (buf >= high_memory)
214                 goto err_buf;
215
216         dma_dev = host->dma_chan->device;
217
218         flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_SRC_UNMAP |
219                 DMA_COMPL_SKIP_DEST_UNMAP;
220
221         phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
222         if (dma_mapping_error(dma_dev->dev, phys_addr)) {
223                 dev_err(host->dev, "Failed to dma_map_single\n");
224                 goto err_buf;
225         }
226
227         if (is_read) {
228                 dma_src_addr = host->io_phys;
229                 dma_dst_addr = phys_addr;
230         } else {
231                 dma_src_addr = phys_addr;
232                 dma_dst_addr = host->io_phys;
233         }
234
235         tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
236                                              dma_src_addr, len, flags);
237         if (!tx) {
238                 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
239                 goto err_dma;
240         }
241
242         init_completion(&host->comp);
243         tx->callback = dma_complete_func;
244         tx->callback_param = &host->comp;
245
246         cookie = tx->tx_submit(tx);
247         if (dma_submit_error(cookie)) {
248                 dev_err(host->dev, "Failed to do DMA tx_submit\n");
249                 goto err_dma;
250         }
251
252         dma_async_issue_pending(host->dma_chan);
253         wait_for_completion(&host->comp);
254
255         err = 0;
256
257 err_dma:
258         dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
259 err_buf:
260         if (err != 0)
261                 dev_warn(host->dev, "Fall back to CPU I/O\n");
262         return err;
263 }
264
265 static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
266 {
267         struct nand_chip *chip = mtd->priv;
268         struct atmel_nand_host *host = chip->priv;
269
270         if (use_dma && len > mtd->oobsize)
271                 /* only use DMA for bigger than oob size: better performances */
272                 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
273                         return;
274
275         if (host->board->bus_width_16)
276                 atmel_read_buf16(mtd, buf, len);
277         else
278                 atmel_read_buf8(mtd, buf, len);
279 }
280
281 static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
282 {
283         struct nand_chip *chip = mtd->priv;
284         struct atmel_nand_host *host = chip->priv;
285
286         if (use_dma && len > mtd->oobsize)
287                 /* only use DMA for bigger than oob size: better performances */
288                 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
289                         return;
290
291         if (host->board->bus_width_16)
292                 atmel_write_buf16(mtd, buf, len);
293         else
294                 atmel_write_buf8(mtd, buf, len);
295 }
296
297 /*
298  * Calculate HW ECC
299  *
300  * function called after a write
301  *
302  * mtd:        MTD block structure
303  * dat:        raw data (unused)
304  * ecc_code:   buffer for ECC
305  */
306 static int atmel_nand_calculate(struct mtd_info *mtd,
307                 const u_char *dat, unsigned char *ecc_code)
308 {
309         struct nand_chip *nand_chip = mtd->priv;
310         struct atmel_nand_host *host = nand_chip->priv;
311         unsigned int ecc_value;
312
313         /* get the first 2 ECC bytes */
314         ecc_value = ecc_readl(host->ecc, PR);
315
316         ecc_code[0] = ecc_value & 0xFF;
317         ecc_code[1] = (ecc_value >> 8) & 0xFF;
318
319         /* get the last 2 ECC bytes */
320         ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
321
322         ecc_code[2] = ecc_value & 0xFF;
323         ecc_code[3] = (ecc_value >> 8) & 0xFF;
324
325         return 0;
326 }
327
328 /*
329  * HW ECC read page function
330  *
331  * mtd:        mtd info structure
332  * chip:       nand chip info structure
333  * buf:        buffer to store read data
334  */
335 static int atmel_nand_read_page(struct mtd_info *mtd,
336                 struct nand_chip *chip, uint8_t *buf, int page)
337 {
338         int eccsize = chip->ecc.size;
339         int eccbytes = chip->ecc.bytes;
340         uint32_t *eccpos = chip->ecc.layout->eccpos;
341         uint8_t *p = buf;
342         uint8_t *oob = chip->oob_poi;
343         uint8_t *ecc_pos;
344         int stat;
345
346         /*
347          * Errata: ALE is incorrectly wired up to the ECC controller
348          * on the AP7000, so it will include the address cycles in the
349          * ECC calculation.
350          *
351          * Workaround: Reset the parity registers before reading the
352          * actual data.
353          */
354         if (cpu_is_at32ap7000()) {
355                 struct atmel_nand_host *host = chip->priv;
356                 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
357         }
358
359         /* read the page */
360         chip->read_buf(mtd, p, eccsize);
361
362         /* move to ECC position if needed */
363         if (eccpos[0] != 0) {
364                 /* This only works on large pages
365                  * because the ECC controller waits for
366                  * NAND_CMD_RNDOUTSTART after the
367                  * NAND_CMD_RNDOUT.
368                  * anyway, for small pages, the eccpos[0] == 0
369                  */
370                 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
371                                 mtd->writesize + eccpos[0], -1);
372         }
373
374         /* the ECC controller needs to read the ECC just after the data */
375         ecc_pos = oob + eccpos[0];
376         chip->read_buf(mtd, ecc_pos, eccbytes);
377
378         /* check if there's an error */
379         stat = chip->ecc.correct(mtd, p, oob, NULL);
380
381         if (stat < 0)
382                 mtd->ecc_stats.failed++;
383         else
384                 mtd->ecc_stats.corrected += stat;
385
386         /* get back to oob start (end of page) */
387         chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
388
389         /* read the oob */
390         chip->read_buf(mtd, oob, mtd->oobsize);
391
392         return 0;
393 }
394
395 /*
396  * HW ECC Correction
397  *
398  * function called after a read
399  *
400  * mtd:        MTD block structure
401  * dat:        raw data read from the chip
402  * read_ecc:   ECC from the chip (unused)
403  * isnull:     unused
404  *
405  * Detect and correct a 1 bit error for a page
406  */
407 static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
408                 u_char *read_ecc, u_char *isnull)
409 {
410         struct nand_chip *nand_chip = mtd->priv;
411         struct atmel_nand_host *host = nand_chip->priv;
412         unsigned int ecc_status;
413         unsigned int ecc_word, ecc_bit;
414
415         /* get the status from the Status Register */
416         ecc_status = ecc_readl(host->ecc, SR);
417
418         /* if there's no error */
419         if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
420                 return 0;
421
422         /* get error bit offset (4 bits) */
423         ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
424         /* get word address (12 bits) */
425         ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
426         ecc_word >>= 4;
427
428         /* if there are multiple errors */
429         if (ecc_status & ATMEL_ECC_MULERR) {
430                 /* check if it is a freshly erased block
431                  * (filled with 0xff) */
432                 if ((ecc_bit == ATMEL_ECC_BITADDR)
433                                 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
434                         /* the block has just been erased, return OK */
435                         return 0;
436                 }
437                 /* it doesn't seems to be a freshly
438                  * erased block.
439                  * We can't correct so many errors */
440                 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
441                                 " Unable to correct.\n");
442                 return -EIO;
443         }
444
445         /* if there's a single bit error : we can correct it */
446         if (ecc_status & ATMEL_ECC_ECCERR) {
447                 /* there's nothing much to do here.
448                  * the bit error is on the ECC itself.
449                  */
450                 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
451                                 " Nothing to correct\n");
452                 return 0;
453         }
454
455         dev_dbg(host->dev, "atmel_nand : one bit error on data."
456                         " (word offset in the page :"
457                         " 0x%x bit offset : 0x%x)\n",
458                         ecc_word, ecc_bit);
459         /* correct the error */
460         if (nand_chip->options & NAND_BUSWIDTH_16) {
461                 /* 16 bits words */
462                 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
463         } else {
464                 /* 8 bits words */
465                 dat[ecc_word] ^= (1 << ecc_bit);
466         }
467         dev_dbg(host->dev, "atmel_nand : error corrected\n");
468         return 1;
469 }
470
471 /*
472  * Enable HW ECC : unused on most chips
473  */
474 static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
475 {
476         if (cpu_is_at32ap7000()) {
477                 struct nand_chip *nand_chip = mtd->priv;
478                 struct atmel_nand_host *host = nand_chip->priv;
479                 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
480         }
481 }
482
483 #ifdef CONFIG_MTD_CMDLINE_PARTS
484 static const char *part_probes[] = { "cmdlinepart", NULL };
485 #endif
486
487 /*
488  * Probe for the NAND device.
489  */
490 static int __init atmel_nand_probe(struct platform_device *pdev)
491 {
492         struct atmel_nand_host *host;
493         struct mtd_info *mtd;
494         struct nand_chip *nand_chip;
495         struct resource *regs;
496         struct resource *mem;
497         int res;
498         struct mtd_partition *partitions = NULL;
499         int num_partitions = 0;
500
501         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
502         if (!mem) {
503                 printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
504                 return -ENXIO;
505         }
506
507         /* Allocate memory for the device structure (and zero it) */
508         host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
509         if (!host) {
510                 printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
511                 return -ENOMEM;
512         }
513
514         host->io_phys = (dma_addr_t)mem->start;
515
516         host->io_base = ioremap(mem->start, mem->end - mem->start + 1);
517         if (host->io_base == NULL) {
518                 printk(KERN_ERR "atmel_nand: ioremap failed\n");
519                 res = -EIO;
520                 goto err_nand_ioremap;
521         }
522
523         mtd = &host->mtd;
524         nand_chip = &host->nand_chip;
525         host->board = pdev->dev.platform_data;
526         host->dev = &pdev->dev;
527
528         nand_chip->priv = host;         /* link the private data structures */
529         mtd->priv = nand_chip;
530         mtd->owner = THIS_MODULE;
531
532         /* Set address of NAND IO lines */
533         nand_chip->IO_ADDR_R = host->io_base;
534         nand_chip->IO_ADDR_W = host->io_base;
535         nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
536
537         if (host->board->rdy_pin)
538                 nand_chip->dev_ready = atmel_nand_device_ready;
539
540         regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
541         if (!regs && hard_ecc) {
542                 printk(KERN_ERR "atmel_nand: can't get I/O resource "
543                                 "regs\nFalling back on software ECC\n");
544         }
545
546         nand_chip->ecc.mode = NAND_ECC_SOFT;    /* enable ECC */
547         if (no_ecc)
548                 nand_chip->ecc.mode = NAND_ECC_NONE;
549         if (hard_ecc && regs) {
550                 host->ecc = ioremap(regs->start, regs->end - regs->start + 1);
551                 if (host->ecc == NULL) {
552                         printk(KERN_ERR "atmel_nand: ioremap failed\n");
553                         res = -EIO;
554                         goto err_ecc_ioremap;
555                 }
556                 nand_chip->ecc.mode = NAND_ECC_HW;
557                 nand_chip->ecc.calculate = atmel_nand_calculate;
558                 nand_chip->ecc.correct = atmel_nand_correct;
559                 nand_chip->ecc.hwctl = atmel_nand_hwctl;
560                 nand_chip->ecc.read_page = atmel_nand_read_page;
561                 nand_chip->ecc.bytes = 4;
562         }
563
564         nand_chip->chip_delay = 20;             /* 20us command delay time */
565
566         if (host->board->bus_width_16)  /* 16-bit bus width */
567                 nand_chip->options |= NAND_BUSWIDTH_16;
568
569         nand_chip->read_buf = atmel_read_buf;
570         nand_chip->write_buf = atmel_write_buf;
571
572         platform_set_drvdata(pdev, host);
573         atmel_nand_enable(host);
574
575         if (host->board->det_pin) {
576                 if (gpio_get_value(host->board->det_pin)) {
577                         printk(KERN_INFO "No SmartMedia card inserted.\n");
578                         res = -ENXIO;
579                         goto err_no_card;
580                 }
581         }
582
583         if (on_flash_bbt) {
584                 printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
585                 nand_chip->options |= NAND_USE_FLASH_BBT;
586         }
587
588         if (!cpu_has_dma())
589                 use_dma = 0;
590
591         if (use_dma) {
592                 dma_cap_mask_t mask;
593
594                 dma_cap_zero(mask);
595                 dma_cap_set(DMA_MEMCPY, mask);
596                 host->dma_chan = dma_request_channel(mask, 0, NULL);
597                 if (!host->dma_chan) {
598                         dev_err(host->dev, "Failed to request DMA channel\n");
599                         use_dma = 0;
600                 }
601         }
602         if (use_dma)
603                 dev_info(host->dev, "Using %s for DMA transfers.\n",
604                                         dma_chan_name(host->dma_chan));
605         else
606                 dev_info(host->dev, "No DMA support for NAND access.\n");
607
608         /* first scan to find the device and get the page size */
609         if (nand_scan_ident(mtd, 1, NULL)) {
610                 res = -ENXIO;
611                 goto err_scan_ident;
612         }
613
614         if (nand_chip->ecc.mode == NAND_ECC_HW) {
615                 /* ECC is calculated for the whole page (1 step) */
616                 nand_chip->ecc.size = mtd->writesize;
617
618                 /* set ECC page size and oob layout */
619                 switch (mtd->writesize) {
620                 case 512:
621                         nand_chip->ecc.layout = &atmel_oobinfo_small;
622                         ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
623                         break;
624                 case 1024:
625                         nand_chip->ecc.layout = &atmel_oobinfo_large;
626                         ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
627                         break;
628                 case 2048:
629                         nand_chip->ecc.layout = &atmel_oobinfo_large;
630                         ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
631                         break;
632                 case 4096:
633                         nand_chip->ecc.layout = &atmel_oobinfo_large;
634                         ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
635                         break;
636                 default:
637                         /* page size not handled by HW ECC */
638                         /* switching back to soft ECC */
639                         nand_chip->ecc.mode = NAND_ECC_SOFT;
640                         nand_chip->ecc.calculate = NULL;
641                         nand_chip->ecc.correct = NULL;
642                         nand_chip->ecc.hwctl = NULL;
643                         nand_chip->ecc.read_page = NULL;
644                         nand_chip->ecc.postpad = 0;
645                         nand_chip->ecc.prepad = 0;
646                         nand_chip->ecc.bytes = 0;
647                         break;
648                 }
649         }
650
651         /* second phase scan */
652         if (nand_scan_tail(mtd)) {
653                 res = -ENXIO;
654                 goto err_scan_tail;
655         }
656
657 #ifdef CONFIG_MTD_CMDLINE_PARTS
658         mtd->name = "atmel_nand";
659         num_partitions = parse_mtd_partitions(mtd, part_probes,
660                                               &partitions, 0);
661 #endif
662         if (num_partitions <= 0 && host->board->partition_info)
663                 partitions = host->board->partition_info(mtd->size,
664                                                          &num_partitions);
665
666         if ((!partitions) || (num_partitions == 0)) {
667                 printk(KERN_ERR "atmel_nand: No partitions defined, or unsupported device.\n");
668                 res = -ENXIO;
669                 goto err_no_partitions;
670         }
671
672         res = mtd_device_register(mtd, partitions, num_partitions);
673         if (!res)
674                 return res;
675
676 err_no_partitions:
677         nand_release(mtd);
678 err_scan_tail:
679 err_scan_ident:
680 err_no_card:
681         atmel_nand_disable(host);
682         platform_set_drvdata(pdev, NULL);
683         if (host->dma_chan)
684                 dma_release_channel(host->dma_chan);
685         if (host->ecc)
686                 iounmap(host->ecc);
687 err_ecc_ioremap:
688         iounmap(host->io_base);
689 err_nand_ioremap:
690         kfree(host);
691         return res;
692 }
693
694 /*
695  * Remove a NAND device.
696  */
697 static int __exit atmel_nand_remove(struct platform_device *pdev)
698 {
699         struct atmel_nand_host *host = platform_get_drvdata(pdev);
700         struct mtd_info *mtd = &host->mtd;
701
702         nand_release(mtd);
703
704         atmel_nand_disable(host);
705
706         if (host->ecc)
707                 iounmap(host->ecc);
708
709         if (host->dma_chan)
710                 dma_release_channel(host->dma_chan);
711
712         iounmap(host->io_base);
713         kfree(host);
714
715         return 0;
716 }
717
718 static struct platform_driver atmel_nand_driver = {
719         .remove         = __exit_p(atmel_nand_remove),
720         .driver         = {
721                 .name   = "atmel_nand",
722                 .owner  = THIS_MODULE,
723         },
724 };
725
726 static int __init atmel_nand_init(void)
727 {
728         return platform_driver_probe(&atmel_nand_driver, atmel_nand_probe);
729 }
730
731
732 static void __exit atmel_nand_exit(void)
733 {
734         platform_driver_unregister(&atmel_nand_driver);
735 }
736
737
738 module_init(atmel_nand_init);
739 module_exit(atmel_nand_exit);
740
741 MODULE_LICENSE("GPL");
742 MODULE_AUTHOR("Rick Bronson");
743 MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
744 MODULE_ALIAS("platform:atmel_nand");