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