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