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