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