Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1 | /* |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 2 | * Copyright © 2003 Rick Bronson |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 3 | * |
| 4 | * Derived from drivers/mtd/nand/autcpu12.c |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 5 | * Copyright © 2001 Thomas Gleixner (gleixner@autronix.de) |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 6 | * |
| 7 | * Derived from drivers/mtd/spia.c |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 8 | * Copyright © 2000 Steven J. Hill (sjhill@cotw.com) |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 9 | * |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 10 | * |
| 11 | * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263 |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 12 | * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright © 2007 |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 13 | * |
| 14 | * Derived from Das U-Boot source code |
| 15 | * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c) |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 16 | * © Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 17 | * |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 18 | * Add Programmable Multibit ECC support for various AT91 SoC |
| 19 | * © Copyright 2012 ATMEL, Hong Xu |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 20 | * |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 21 | * This program is free software; you can redistribute it and/or modify |
| 22 | * it under the terms of the GNU General Public License version 2 as |
| 23 | * published by the Free Software Foundation. |
| 24 | * |
| 25 | */ |
| 26 | |
Alexey Dobriyan | b7f080c | 2011-06-16 11:01:34 +0000 | [diff] [blame] | 27 | #include <linux/dma-mapping.h> |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 28 | #include <linux/slab.h> |
| 29 | #include <linux/module.h> |
Simon Polette | f4fa697c | 2009-05-27 18:19:39 +0300 | [diff] [blame] | 30 | #include <linux/moduleparam.h> |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 31 | #include <linux/platform_device.h> |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 32 | #include <linux/of.h> |
| 33 | #include <linux/of_device.h> |
| 34 | #include <linux/of_gpio.h> |
| 35 | #include <linux/of_mtd.h> |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 36 | #include <linux/mtd/mtd.h> |
| 37 | #include <linux/mtd/nand.h> |
| 38 | #include <linux/mtd/partitions.h> |
| 39 | |
Hans-Christian Egtvedt | 5c39c4c | 2011-04-13 15:55:17 +0200 | [diff] [blame] | 40 | #include <linux/dmaengine.h> |
David Woodhouse | 90574d0 | 2008-06-07 08:49:00 +0100 | [diff] [blame] | 41 | #include <linux/gpio.h> |
| 42 | #include <linux/io.h> |
Jean-Christophe PLAGNIOL-VILLARD | bf4289c | 2011-12-29 14:43:24 +0800 | [diff] [blame] | 43 | #include <linux/platform_data/atmel.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; |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 96 | |
| 97 | bool has_pmecc; |
| 98 | u8 pmecc_corr_cap; |
| 99 | u16 pmecc_sector_size; |
| 100 | u32 pmecc_lookup_table_offset; |
Josh Wu | e66b431 | 2013-01-23 20:47:11 +0800 | [diff] [blame] | 101 | u32 pmecc_lookup_table_offset_512; |
| 102 | u32 pmecc_lookup_table_offset_1024; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 103 | |
| 104 | int pmecc_bytes_per_sector; |
| 105 | int pmecc_sector_number; |
| 106 | int pmecc_degree; /* Degree of remainders */ |
| 107 | int pmecc_cw_len; /* Length of codeword */ |
| 108 | |
| 109 | void __iomem *pmerrloc_base; |
| 110 | void __iomem *pmecc_rom_base; |
| 111 | |
| 112 | /* lookup table for alpha_to and index_of */ |
| 113 | void __iomem *pmecc_alpha_to; |
| 114 | void __iomem *pmecc_index_of; |
| 115 | |
| 116 | /* data for pmecc computation */ |
| 117 | int16_t *pmecc_partial_syn; |
| 118 | int16_t *pmecc_si; |
| 119 | int16_t *pmecc_smu; /* Sigma table */ |
| 120 | int16_t *pmecc_lmu; /* polynomal order */ |
| 121 | int *pmecc_mu; |
| 122 | int *pmecc_dmu; |
| 123 | int *pmecc_delta; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 124 | }; |
| 125 | |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 126 | static struct nand_ecclayout atmel_pmecc_oobinfo; |
| 127 | |
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 | * Enable NAND. |
| 130 | */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 131 | static void atmel_nand_enable(struct atmel_nand_host *host) |
Atsushi Nemoto | 8136508 | 2008-04-27 01:51:12 +0900 | [diff] [blame] | 132 | { |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 133 | if (gpio_is_valid(host->board.enable_pin)) |
| 134 | gpio_set_value(host->board.enable_pin, 0); |
Atsushi Nemoto | 8136508 | 2008-04-27 01:51:12 +0900 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Disable NAND. |
| 139 | */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 140 | static void atmel_nand_disable(struct atmel_nand_host *host) |
Atsushi Nemoto | 8136508 | 2008-04-27 01:51:12 +0900 | [diff] [blame] | 141 | { |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 142 | if (gpio_is_valid(host->board.enable_pin)) |
| 143 | gpio_set_value(host->board.enable_pin, 1); |
Atsushi Nemoto | 8136508 | 2008-04-27 01:51:12 +0900 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 147 | * Hardware specific access to control-lines |
| 148 | */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 149 | 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] | 150 | { |
| 151 | struct nand_chip *nand_chip = mtd->priv; |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 152 | struct atmel_nand_host *host = nand_chip->priv; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 153 | |
Atsushi Nemoto | 8136508 | 2008-04-27 01:51:12 +0900 | [diff] [blame] | 154 | if (ctrl & NAND_CTRL_CHANGE) { |
Atsushi Nemoto | 2314488 | 2008-04-24 23:51:29 +0900 | [diff] [blame] | 155 | if (ctrl & NAND_NCE) |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 156 | atmel_nand_enable(host); |
Atsushi Nemoto | 2314488 | 2008-04-24 23:51:29 +0900 | [diff] [blame] | 157 | else |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 158 | atmel_nand_disable(host); |
Atsushi Nemoto | 2314488 | 2008-04-24 23:51:29 +0900 | [diff] [blame] | 159 | } |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 160 | if (cmd == NAND_CMD_NONE) |
| 161 | return; |
| 162 | |
| 163 | if (ctrl & NAND_CLE) |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 164 | writeb(cmd, host->io_base + (1 << host->board.cle)); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 165 | else |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 166 | writeb(cmd, host->io_base + (1 << host->board.ale)); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Read the Device Ready pin. |
| 171 | */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 172 | static int atmel_nand_device_ready(struct mtd_info *mtd) |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 173 | { |
| 174 | struct nand_chip *nand_chip = mtd->priv; |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 175 | struct atmel_nand_host *host = nand_chip->priv; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 176 | |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 177 | return gpio_get_value(host->board.rdy_pin) ^ |
| 178 | !!host->board.rdy_pin_active_low; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Artem Bityutskiy | 5008231 | 2012-02-02 13:54:25 +0200 | [diff] [blame] | 181 | /* |
| 182 | * Minimal-overhead PIO for data access. |
| 183 | */ |
| 184 | static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len) |
| 185 | { |
| 186 | struct nand_chip *nand_chip = mtd->priv; |
| 187 | |
| 188 | __raw_readsb(nand_chip->IO_ADDR_R, buf, len); |
| 189 | } |
| 190 | |
| 191 | static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len) |
| 192 | { |
| 193 | struct nand_chip *nand_chip = mtd->priv; |
| 194 | |
| 195 | __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2); |
| 196 | } |
| 197 | |
| 198 | static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len) |
| 199 | { |
| 200 | struct nand_chip *nand_chip = mtd->priv; |
| 201 | |
| 202 | __raw_writesb(nand_chip->IO_ADDR_W, buf, len); |
| 203 | } |
| 204 | |
| 205 | static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len) |
| 206 | { |
| 207 | struct nand_chip *nand_chip = mtd->priv; |
| 208 | |
| 209 | __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2); |
| 210 | } |
| 211 | |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 212 | static void dma_complete_func(void *completion) |
| 213 | { |
| 214 | complete(completion); |
| 215 | } |
| 216 | |
| 217 | static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len, |
| 218 | int is_read) |
| 219 | { |
| 220 | struct dma_device *dma_dev; |
| 221 | enum dma_ctrl_flags flags; |
| 222 | dma_addr_t dma_src_addr, dma_dst_addr, phys_addr; |
| 223 | struct dma_async_tx_descriptor *tx = NULL; |
| 224 | dma_cookie_t cookie; |
| 225 | struct nand_chip *chip = mtd->priv; |
| 226 | struct atmel_nand_host *host = chip->priv; |
| 227 | void *p = buf; |
| 228 | int err = -EIO; |
| 229 | enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
| 230 | |
Hong Xu | 80b4f81 | 2011-03-31 18:33:15 +0800 | [diff] [blame] | 231 | if (buf >= high_memory) |
| 232 | goto err_buf; |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 233 | |
| 234 | dma_dev = host->dma_chan->device; |
| 235 | |
| 236 | flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_SRC_UNMAP | |
| 237 | DMA_COMPL_SKIP_DEST_UNMAP; |
| 238 | |
| 239 | phys_addr = dma_map_single(dma_dev->dev, p, len, dir); |
| 240 | if (dma_mapping_error(dma_dev->dev, phys_addr)) { |
| 241 | dev_err(host->dev, "Failed to dma_map_single\n"); |
| 242 | goto err_buf; |
| 243 | } |
| 244 | |
| 245 | if (is_read) { |
| 246 | dma_src_addr = host->io_phys; |
| 247 | dma_dst_addr = phys_addr; |
| 248 | } else { |
| 249 | dma_src_addr = phys_addr; |
| 250 | dma_dst_addr = host->io_phys; |
| 251 | } |
| 252 | |
| 253 | tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr, |
| 254 | dma_src_addr, len, flags); |
| 255 | if (!tx) { |
| 256 | dev_err(host->dev, "Failed to prepare DMA memcpy\n"); |
| 257 | goto err_dma; |
| 258 | } |
| 259 | |
| 260 | init_completion(&host->comp); |
| 261 | tx->callback = dma_complete_func; |
| 262 | tx->callback_param = &host->comp; |
| 263 | |
| 264 | cookie = tx->tx_submit(tx); |
| 265 | if (dma_submit_error(cookie)) { |
| 266 | dev_err(host->dev, "Failed to do DMA tx_submit\n"); |
| 267 | goto err_dma; |
| 268 | } |
| 269 | |
| 270 | dma_async_issue_pending(host->dma_chan); |
| 271 | wait_for_completion(&host->comp); |
| 272 | |
| 273 | err = 0; |
| 274 | |
| 275 | err_dma: |
| 276 | dma_unmap_single(dma_dev->dev, phys_addr, len, dir); |
| 277 | err_buf: |
| 278 | if (err != 0) |
| 279 | dev_warn(host->dev, "Fall back to CPU I/O\n"); |
| 280 | return err; |
| 281 | } |
| 282 | |
| 283 | static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len) |
| 284 | { |
| 285 | struct nand_chip *chip = mtd->priv; |
Artem Bityutskiy | 5008231 | 2012-02-02 13:54:25 +0200 | [diff] [blame] | 286 | struct atmel_nand_host *host = chip->priv; |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 287 | |
Nicolas Ferre | 9d51567 | 2011-04-01 16:40:44 +0200 | [diff] [blame] | 288 | if (use_dma && len > mtd->oobsize) |
| 289 | /* only use DMA for bigger than oob size: better performances */ |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 290 | if (atmel_nand_dma_op(mtd, buf, len, 1) == 0) |
| 291 | return; |
| 292 | |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 293 | if (host->board.bus_width_16) |
Artem Bityutskiy | 5008231 | 2012-02-02 13:54:25 +0200 | [diff] [blame] | 294 | atmel_read_buf16(mtd, buf, len); |
| 295 | else |
| 296 | atmel_read_buf8(mtd, buf, len); |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len) |
| 300 | { |
| 301 | struct nand_chip *chip = mtd->priv; |
Artem Bityutskiy | 5008231 | 2012-02-02 13:54:25 +0200 | [diff] [blame] | 302 | struct atmel_nand_host *host = chip->priv; |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 303 | |
Nicolas Ferre | 9d51567 | 2011-04-01 16:40:44 +0200 | [diff] [blame] | 304 | if (use_dma && len > mtd->oobsize) |
| 305 | /* only use DMA for bigger than oob size: better performances */ |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 306 | if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0) |
| 307 | return; |
| 308 | |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 309 | if (host->board.bus_width_16) |
Artem Bityutskiy | 5008231 | 2012-02-02 13:54:25 +0200 | [diff] [blame] | 310 | atmel_write_buf16(mtd, buf, len); |
| 311 | else |
| 312 | atmel_write_buf8(mtd, buf, len); |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 313 | } |
| 314 | |
David Brownell | 23a346c | 2008-07-03 23:40:16 -0700 | [diff] [blame] | 315 | /* |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 316 | * Return number of ecc bytes per sector according to sector size and |
| 317 | * correction capability |
| 318 | * |
| 319 | * Following table shows what at91 PMECC supported: |
| 320 | * Correction Capability Sector_512_bytes Sector_1024_bytes |
| 321 | * ===================== ================ ================= |
| 322 | * 2-bits 4-bytes 4-bytes |
| 323 | * 4-bits 7-bytes 7-bytes |
| 324 | * 8-bits 13-bytes 14-bytes |
| 325 | * 12-bits 20-bytes 21-bytes |
| 326 | * 24-bits 39-bytes 42-bytes |
| 327 | */ |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 328 | static int pmecc_get_ecc_bytes(int cap, int sector_size) |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 329 | { |
| 330 | int m = 12 + sector_size / 512; |
| 331 | return (m * cap + 7) / 8; |
| 332 | } |
| 333 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 334 | static void pmecc_config_ecc_layout(struct nand_ecclayout *layout, |
Greg Kroah-Hartman | d892994 | 2012-12-21 13:19:05 -0800 | [diff] [blame] | 335 | int oobsize, int ecc_len) |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 336 | { |
| 337 | int i; |
| 338 | |
| 339 | layout->eccbytes = ecc_len; |
| 340 | |
| 341 | /* ECC will occupy the last ecc_len bytes continuously */ |
| 342 | for (i = 0; i < ecc_len; i++) |
| 343 | layout->eccpos[i] = oobsize - ecc_len + i; |
| 344 | |
| 345 | layout->oobfree[0].offset = 2; |
| 346 | layout->oobfree[0].length = |
| 347 | oobsize - ecc_len - layout->oobfree[0].offset; |
| 348 | } |
| 349 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 350 | static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host) |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 351 | { |
| 352 | int table_size; |
| 353 | |
| 354 | table_size = host->pmecc_sector_size == 512 ? |
| 355 | PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024; |
| 356 | |
| 357 | return host->pmecc_rom_base + host->pmecc_lookup_table_offset + |
| 358 | table_size * sizeof(int16_t); |
| 359 | } |
| 360 | |
| 361 | static void pmecc_data_free(struct atmel_nand_host *host) |
| 362 | { |
| 363 | kfree(host->pmecc_partial_syn); |
| 364 | kfree(host->pmecc_si); |
| 365 | kfree(host->pmecc_lmu); |
| 366 | kfree(host->pmecc_smu); |
| 367 | kfree(host->pmecc_mu); |
| 368 | kfree(host->pmecc_dmu); |
| 369 | kfree(host->pmecc_delta); |
| 370 | } |
| 371 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 372 | static int pmecc_data_alloc(struct atmel_nand_host *host) |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 373 | { |
| 374 | const int cap = host->pmecc_corr_cap; |
| 375 | |
| 376 | host->pmecc_partial_syn = kzalloc((2 * cap + 1) * sizeof(int16_t), |
| 377 | GFP_KERNEL); |
| 378 | host->pmecc_si = kzalloc((2 * cap + 1) * sizeof(int16_t), GFP_KERNEL); |
| 379 | host->pmecc_lmu = kzalloc((cap + 1) * sizeof(int16_t), GFP_KERNEL); |
| 380 | host->pmecc_smu = kzalloc((cap + 2) * (2 * cap + 1) * sizeof(int16_t), |
| 381 | GFP_KERNEL); |
| 382 | host->pmecc_mu = kzalloc((cap + 1) * sizeof(int), GFP_KERNEL); |
| 383 | host->pmecc_dmu = kzalloc((cap + 1) * sizeof(int), GFP_KERNEL); |
| 384 | host->pmecc_delta = kzalloc((cap + 1) * sizeof(int), GFP_KERNEL); |
| 385 | |
| 386 | if (host->pmecc_partial_syn && |
| 387 | host->pmecc_si && |
| 388 | host->pmecc_lmu && |
| 389 | host->pmecc_smu && |
| 390 | host->pmecc_mu && |
| 391 | host->pmecc_dmu && |
| 392 | host->pmecc_delta) |
| 393 | return 0; |
| 394 | |
| 395 | /* error happened */ |
| 396 | pmecc_data_free(host); |
| 397 | return -ENOMEM; |
| 398 | } |
| 399 | |
| 400 | static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector) |
| 401 | { |
| 402 | struct nand_chip *nand_chip = mtd->priv; |
| 403 | struct atmel_nand_host *host = nand_chip->priv; |
| 404 | int i; |
| 405 | uint32_t value; |
| 406 | |
| 407 | /* Fill odd syndromes */ |
| 408 | for (i = 0; i < host->pmecc_corr_cap; i++) { |
| 409 | value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2); |
| 410 | if (i & 1) |
| 411 | value >>= 16; |
| 412 | value &= 0xffff; |
| 413 | host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | static void pmecc_substitute(struct mtd_info *mtd) |
| 418 | { |
| 419 | struct nand_chip *nand_chip = mtd->priv; |
| 420 | struct atmel_nand_host *host = nand_chip->priv; |
| 421 | int16_t __iomem *alpha_to = host->pmecc_alpha_to; |
| 422 | int16_t __iomem *index_of = host->pmecc_index_of; |
| 423 | int16_t *partial_syn = host->pmecc_partial_syn; |
| 424 | const int cap = host->pmecc_corr_cap; |
| 425 | int16_t *si; |
| 426 | int i, j; |
| 427 | |
| 428 | /* si[] is a table that holds the current syndrome value, |
| 429 | * an element of that table belongs to the field |
| 430 | */ |
| 431 | si = host->pmecc_si; |
| 432 | |
| 433 | memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1)); |
| 434 | |
| 435 | /* Computation 2t syndromes based on S(x) */ |
| 436 | /* Odd syndromes */ |
| 437 | for (i = 1; i < 2 * cap; i += 2) { |
| 438 | for (j = 0; j < host->pmecc_degree; j++) { |
| 439 | if (partial_syn[i] & ((unsigned short)0x1 << j)) |
| 440 | si[i] = readw_relaxed(alpha_to + i * j) ^ si[i]; |
| 441 | } |
| 442 | } |
| 443 | /* Even syndrome = (Odd syndrome) ** 2 */ |
| 444 | for (i = 2, j = 1; j <= cap; i = ++j << 1) { |
| 445 | if (si[j] == 0) { |
| 446 | si[i] = 0; |
| 447 | } else { |
| 448 | int16_t tmp; |
| 449 | |
| 450 | tmp = readw_relaxed(index_of + si[j]); |
| 451 | tmp = (tmp * 2) % host->pmecc_cw_len; |
| 452 | si[i] = readw_relaxed(alpha_to + tmp); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | static void pmecc_get_sigma(struct mtd_info *mtd) |
| 460 | { |
| 461 | struct nand_chip *nand_chip = mtd->priv; |
| 462 | struct atmel_nand_host *host = nand_chip->priv; |
| 463 | |
| 464 | int16_t *lmu = host->pmecc_lmu; |
| 465 | int16_t *si = host->pmecc_si; |
| 466 | int *mu = host->pmecc_mu; |
| 467 | int *dmu = host->pmecc_dmu; /* Discrepancy */ |
| 468 | int *delta = host->pmecc_delta; /* Delta order */ |
| 469 | int cw_len = host->pmecc_cw_len; |
| 470 | const int16_t cap = host->pmecc_corr_cap; |
| 471 | const int num = 2 * cap + 1; |
| 472 | int16_t __iomem *index_of = host->pmecc_index_of; |
| 473 | int16_t __iomem *alpha_to = host->pmecc_alpha_to; |
| 474 | int i, j, k; |
| 475 | uint32_t dmu_0_count, tmp; |
| 476 | int16_t *smu = host->pmecc_smu; |
| 477 | |
| 478 | /* index of largest delta */ |
| 479 | int ro; |
| 480 | int largest; |
| 481 | int diff; |
| 482 | |
| 483 | dmu_0_count = 0; |
| 484 | |
| 485 | /* First Row */ |
| 486 | |
| 487 | /* Mu */ |
| 488 | mu[0] = -1; |
| 489 | |
| 490 | memset(smu, 0, sizeof(int16_t) * num); |
| 491 | smu[0] = 1; |
| 492 | |
| 493 | /* discrepancy set to 1 */ |
| 494 | dmu[0] = 1; |
| 495 | /* polynom order set to 0 */ |
| 496 | lmu[0] = 0; |
| 497 | delta[0] = (mu[0] * 2 - lmu[0]) >> 1; |
| 498 | |
| 499 | /* Second Row */ |
| 500 | |
| 501 | /* Mu */ |
| 502 | mu[1] = 0; |
| 503 | /* Sigma(x) set to 1 */ |
| 504 | memset(&smu[num], 0, sizeof(int16_t) * num); |
| 505 | smu[num] = 1; |
| 506 | |
| 507 | /* discrepancy set to S1 */ |
| 508 | dmu[1] = si[1]; |
| 509 | |
| 510 | /* polynom order set to 0 */ |
| 511 | lmu[1] = 0; |
| 512 | |
| 513 | delta[1] = (mu[1] * 2 - lmu[1]) >> 1; |
| 514 | |
| 515 | /* Init the Sigma(x) last row */ |
| 516 | memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num); |
| 517 | |
| 518 | for (i = 1; i <= cap; i++) { |
| 519 | mu[i + 1] = i << 1; |
| 520 | /* Begin Computing Sigma (Mu+1) and L(mu) */ |
| 521 | /* check if discrepancy is set to 0 */ |
| 522 | if (dmu[i] == 0) { |
| 523 | dmu_0_count++; |
| 524 | |
| 525 | tmp = ((cap - (lmu[i] >> 1) - 1) / 2); |
| 526 | if ((cap - (lmu[i] >> 1) - 1) & 0x1) |
| 527 | tmp += 2; |
| 528 | else |
| 529 | tmp += 1; |
| 530 | |
| 531 | if (dmu_0_count == tmp) { |
| 532 | for (j = 0; j <= (lmu[i] >> 1) + 1; j++) |
| 533 | smu[(cap + 1) * num + j] = |
| 534 | smu[i * num + j]; |
| 535 | |
| 536 | lmu[cap + 1] = lmu[i]; |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | /* copy polynom */ |
| 541 | for (j = 0; j <= lmu[i] >> 1; j++) |
| 542 | smu[(i + 1) * num + j] = smu[i * num + j]; |
| 543 | |
| 544 | /* copy previous polynom order to the next */ |
| 545 | lmu[i + 1] = lmu[i]; |
| 546 | } else { |
| 547 | ro = 0; |
| 548 | largest = -1; |
| 549 | /* find largest delta with dmu != 0 */ |
| 550 | for (j = 0; j < i; j++) { |
| 551 | if ((dmu[j]) && (delta[j] > largest)) { |
| 552 | largest = delta[j]; |
| 553 | ro = j; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /* compute difference */ |
| 558 | diff = (mu[i] - mu[ro]); |
| 559 | |
| 560 | /* Compute degree of the new smu polynomial */ |
| 561 | if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff)) |
| 562 | lmu[i + 1] = lmu[i]; |
| 563 | else |
| 564 | lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2; |
| 565 | |
| 566 | /* Init smu[i+1] with 0 */ |
| 567 | for (k = 0; k < num; k++) |
| 568 | smu[(i + 1) * num + k] = 0; |
| 569 | |
| 570 | /* Compute smu[i+1] */ |
| 571 | for (k = 0; k <= lmu[ro] >> 1; k++) { |
| 572 | int16_t a, b, c; |
| 573 | |
| 574 | if (!(smu[ro * num + k] && dmu[i])) |
| 575 | continue; |
| 576 | a = readw_relaxed(index_of + dmu[i]); |
| 577 | b = readw_relaxed(index_of + dmu[ro]); |
| 578 | c = readw_relaxed(index_of + smu[ro * num + k]); |
| 579 | tmp = a + (cw_len - b) + c; |
| 580 | a = readw_relaxed(alpha_to + tmp % cw_len); |
| 581 | smu[(i + 1) * num + (k + diff)] = a; |
| 582 | } |
| 583 | |
| 584 | for (k = 0; k <= lmu[i] >> 1; k++) |
| 585 | smu[(i + 1) * num + k] ^= smu[i * num + k]; |
| 586 | } |
| 587 | |
| 588 | /* End Computing Sigma (Mu+1) and L(mu) */ |
| 589 | /* In either case compute delta */ |
| 590 | delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1; |
| 591 | |
| 592 | /* Do not compute discrepancy for the last iteration */ |
| 593 | if (i >= cap) |
| 594 | continue; |
| 595 | |
| 596 | for (k = 0; k <= (lmu[i + 1] >> 1); k++) { |
| 597 | tmp = 2 * (i - 1); |
| 598 | if (k == 0) { |
| 599 | dmu[i + 1] = si[tmp + 3]; |
| 600 | } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) { |
| 601 | int16_t a, b, c; |
| 602 | a = readw_relaxed(index_of + |
| 603 | smu[(i + 1) * num + k]); |
| 604 | b = si[2 * (i - 1) + 3 - k]; |
| 605 | c = readw_relaxed(index_of + b); |
| 606 | tmp = a + c; |
| 607 | tmp %= cw_len; |
| 608 | dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^ |
| 609 | dmu[i + 1]; |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | return; |
| 615 | } |
| 616 | |
| 617 | static int pmecc_err_location(struct mtd_info *mtd) |
| 618 | { |
| 619 | struct nand_chip *nand_chip = mtd->priv; |
| 620 | struct atmel_nand_host *host = nand_chip->priv; |
| 621 | unsigned long end_time; |
| 622 | const int cap = host->pmecc_corr_cap; |
| 623 | const int num = 2 * cap + 1; |
| 624 | int sector_size = host->pmecc_sector_size; |
| 625 | int err_nbr = 0; /* number of error */ |
| 626 | int roots_nbr; /* number of roots */ |
| 627 | int i; |
| 628 | uint32_t val; |
| 629 | int16_t *smu = host->pmecc_smu; |
| 630 | |
| 631 | pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE); |
| 632 | |
| 633 | for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) { |
| 634 | pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i, |
| 635 | smu[(cap + 1) * num + i]); |
| 636 | err_nbr++; |
| 637 | } |
| 638 | |
| 639 | val = (err_nbr - 1) << 16; |
| 640 | if (sector_size == 1024) |
| 641 | val |= 1; |
| 642 | |
| 643 | pmerrloc_writel(host->pmerrloc_base, ELCFG, val); |
| 644 | pmerrloc_writel(host->pmerrloc_base, ELEN, |
| 645 | sector_size * 8 + host->pmecc_degree * cap); |
| 646 | |
| 647 | end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS); |
| 648 | while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR) |
| 649 | & PMERRLOC_CALC_DONE)) { |
| 650 | if (unlikely(time_after(jiffies, end_time))) { |
| 651 | dev_err(host->dev, "PMECC: Timeout to calculate error location.\n"); |
| 652 | return -1; |
| 653 | } |
| 654 | cpu_relax(); |
| 655 | } |
| 656 | |
| 657 | roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR) |
| 658 | & PMERRLOC_ERR_NUM_MASK) >> 8; |
| 659 | /* Number of roots == degree of smu hence <= cap */ |
| 660 | if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1) |
| 661 | return err_nbr - 1; |
| 662 | |
| 663 | /* Number of roots does not match the degree of smu |
| 664 | * unable to correct error */ |
| 665 | return -1; |
| 666 | } |
| 667 | |
| 668 | static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc, |
| 669 | int sector_num, int extra_bytes, int err_nbr) |
| 670 | { |
| 671 | struct nand_chip *nand_chip = mtd->priv; |
| 672 | struct atmel_nand_host *host = nand_chip->priv; |
| 673 | int i = 0; |
| 674 | int byte_pos, bit_pos, sector_size, pos; |
| 675 | uint32_t tmp; |
| 676 | uint8_t err_byte; |
| 677 | |
| 678 | sector_size = host->pmecc_sector_size; |
| 679 | |
| 680 | while (err_nbr) { |
| 681 | tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_base, i) - 1; |
| 682 | byte_pos = tmp / 8; |
| 683 | bit_pos = tmp % 8; |
| 684 | |
| 685 | if (byte_pos >= (sector_size + extra_bytes)) |
| 686 | BUG(); /* should never happen */ |
| 687 | |
| 688 | if (byte_pos < sector_size) { |
| 689 | err_byte = *(buf + byte_pos); |
| 690 | *(buf + byte_pos) ^= (1 << bit_pos); |
| 691 | |
| 692 | pos = sector_num * host->pmecc_sector_size + byte_pos; |
| 693 | dev_info(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n", |
| 694 | pos, bit_pos, err_byte, *(buf + byte_pos)); |
| 695 | } else { |
| 696 | /* Bit flip in OOB area */ |
| 697 | tmp = sector_num * host->pmecc_bytes_per_sector |
| 698 | + (byte_pos - sector_size); |
| 699 | err_byte = ecc[tmp]; |
| 700 | ecc[tmp] ^= (1 << bit_pos); |
| 701 | |
| 702 | pos = tmp + nand_chip->ecc.layout->eccpos[0]; |
| 703 | dev_info(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n", |
| 704 | pos, bit_pos, err_byte, ecc[tmp]); |
| 705 | } |
| 706 | |
| 707 | i++; |
| 708 | err_nbr--; |
| 709 | } |
| 710 | |
| 711 | return; |
| 712 | } |
| 713 | |
| 714 | static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf, |
| 715 | u8 *ecc) |
| 716 | { |
| 717 | struct nand_chip *nand_chip = mtd->priv; |
| 718 | struct atmel_nand_host *host = nand_chip->priv; |
| 719 | int i, err_nbr, eccbytes; |
| 720 | uint8_t *buf_pos; |
Josh Wu | c0c70d9 | 2012-11-27 18:50:31 +0800 | [diff] [blame] | 721 | int total_err = 0; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 722 | |
| 723 | eccbytes = nand_chip->ecc.bytes; |
| 724 | for (i = 0; i < eccbytes; i++) |
| 725 | if (ecc[i] != 0xff) |
| 726 | goto normal_check; |
| 727 | /* Erased page, return OK */ |
| 728 | return 0; |
| 729 | |
| 730 | normal_check: |
| 731 | for (i = 0; i < host->pmecc_sector_number; i++) { |
| 732 | err_nbr = 0; |
| 733 | if (pmecc_stat & 0x1) { |
| 734 | buf_pos = buf + i * host->pmecc_sector_size; |
| 735 | |
| 736 | pmecc_gen_syndrome(mtd, i); |
| 737 | pmecc_substitute(mtd); |
| 738 | pmecc_get_sigma(mtd); |
| 739 | |
| 740 | err_nbr = pmecc_err_location(mtd); |
| 741 | if (err_nbr == -1) { |
| 742 | dev_err(host->dev, "PMECC: Too many errors\n"); |
| 743 | mtd->ecc_stats.failed++; |
| 744 | return -EIO; |
| 745 | } else { |
| 746 | pmecc_correct_data(mtd, buf_pos, ecc, i, |
| 747 | host->pmecc_bytes_per_sector, err_nbr); |
| 748 | mtd->ecc_stats.corrected += err_nbr; |
Josh Wu | c0c70d9 | 2012-11-27 18:50:31 +0800 | [diff] [blame] | 749 | total_err += err_nbr; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | pmecc_stat >>= 1; |
| 753 | } |
| 754 | |
Josh Wu | c0c70d9 | 2012-11-27 18:50:31 +0800 | [diff] [blame] | 755 | return total_err; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | static int atmel_nand_pmecc_read_page(struct mtd_info *mtd, |
| 759 | struct nand_chip *chip, uint8_t *buf, int oob_required, int page) |
| 760 | { |
| 761 | struct atmel_nand_host *host = chip->priv; |
| 762 | int eccsize = chip->ecc.size; |
| 763 | uint8_t *oob = chip->oob_poi; |
| 764 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 765 | uint32_t stat; |
| 766 | unsigned long end_time; |
Josh Wu | c0c70d9 | 2012-11-27 18:50:31 +0800 | [diff] [blame] | 767 | int bitflips = 0; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 768 | |
| 769 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST); |
| 770 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE); |
| 771 | pmecc_writel(host->ecc, CFG, (pmecc_readl_relaxed(host->ecc, CFG) |
| 772 | & ~PMECC_CFG_WRITE_OP) | PMECC_CFG_AUTO_ENABLE); |
| 773 | |
| 774 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE); |
| 775 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA); |
| 776 | |
| 777 | chip->read_buf(mtd, buf, eccsize); |
| 778 | chip->read_buf(mtd, oob, mtd->oobsize); |
| 779 | |
| 780 | end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS); |
| 781 | while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) { |
| 782 | if (unlikely(time_after(jiffies, end_time))) { |
| 783 | dev_err(host->dev, "PMECC: Timeout to get error status.\n"); |
| 784 | return -EIO; |
| 785 | } |
| 786 | cpu_relax(); |
| 787 | } |
| 788 | |
| 789 | stat = pmecc_readl_relaxed(host->ecc, ISR); |
Josh Wu | c0c70d9 | 2012-11-27 18:50:31 +0800 | [diff] [blame] | 790 | if (stat != 0) { |
| 791 | bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]); |
| 792 | if (bitflips < 0) |
| 793 | /* uncorrectable errors */ |
| 794 | return 0; |
| 795 | } |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 796 | |
Josh Wu | c0c70d9 | 2012-11-27 18:50:31 +0800 | [diff] [blame] | 797 | return bitflips; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | static int atmel_nand_pmecc_write_page(struct mtd_info *mtd, |
| 801 | struct nand_chip *chip, const uint8_t *buf, int oob_required) |
| 802 | { |
| 803 | struct atmel_nand_host *host = chip->priv; |
| 804 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 805 | int i, j; |
| 806 | unsigned long end_time; |
| 807 | |
| 808 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST); |
| 809 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE); |
| 810 | |
| 811 | pmecc_writel(host->ecc, CFG, (pmecc_readl_relaxed(host->ecc, CFG) | |
| 812 | PMECC_CFG_WRITE_OP) & ~PMECC_CFG_AUTO_ENABLE); |
| 813 | |
| 814 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE); |
| 815 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA); |
| 816 | |
| 817 | chip->write_buf(mtd, (u8 *)buf, mtd->writesize); |
| 818 | |
| 819 | end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS); |
| 820 | while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) { |
| 821 | if (unlikely(time_after(jiffies, end_time))) { |
| 822 | dev_err(host->dev, "PMECC: Timeout to get ECC value.\n"); |
| 823 | return -EIO; |
| 824 | } |
| 825 | cpu_relax(); |
| 826 | } |
| 827 | |
| 828 | for (i = 0; i < host->pmecc_sector_number; i++) { |
| 829 | for (j = 0; j < host->pmecc_bytes_per_sector; j++) { |
| 830 | int pos; |
| 831 | |
| 832 | pos = i * host->pmecc_bytes_per_sector + j; |
| 833 | chip->oob_poi[eccpos[pos]] = |
| 834 | pmecc_readb_ecc_relaxed(host->ecc, i, j); |
| 835 | } |
| 836 | } |
| 837 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 838 | |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | static void atmel_pmecc_core_init(struct mtd_info *mtd) |
| 843 | { |
| 844 | struct nand_chip *nand_chip = mtd->priv; |
| 845 | struct atmel_nand_host *host = nand_chip->priv; |
| 846 | uint32_t val = 0; |
| 847 | struct nand_ecclayout *ecc_layout; |
| 848 | |
| 849 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST); |
| 850 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE); |
| 851 | |
| 852 | switch (host->pmecc_corr_cap) { |
| 853 | case 2: |
| 854 | val = PMECC_CFG_BCH_ERR2; |
| 855 | break; |
| 856 | case 4: |
| 857 | val = PMECC_CFG_BCH_ERR4; |
| 858 | break; |
| 859 | case 8: |
| 860 | val = PMECC_CFG_BCH_ERR8; |
| 861 | break; |
| 862 | case 12: |
| 863 | val = PMECC_CFG_BCH_ERR12; |
| 864 | break; |
| 865 | case 24: |
| 866 | val = PMECC_CFG_BCH_ERR24; |
| 867 | break; |
| 868 | } |
| 869 | |
| 870 | if (host->pmecc_sector_size == 512) |
| 871 | val |= PMECC_CFG_SECTOR512; |
| 872 | else if (host->pmecc_sector_size == 1024) |
| 873 | val |= PMECC_CFG_SECTOR1024; |
| 874 | |
| 875 | switch (host->pmecc_sector_number) { |
| 876 | case 1: |
| 877 | val |= PMECC_CFG_PAGE_1SECTOR; |
| 878 | break; |
| 879 | case 2: |
| 880 | val |= PMECC_CFG_PAGE_2SECTORS; |
| 881 | break; |
| 882 | case 4: |
| 883 | val |= PMECC_CFG_PAGE_4SECTORS; |
| 884 | break; |
| 885 | case 8: |
| 886 | val |= PMECC_CFG_PAGE_8SECTORS; |
| 887 | break; |
| 888 | } |
| 889 | |
| 890 | val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE |
| 891 | | PMECC_CFG_AUTO_DISABLE); |
| 892 | pmecc_writel(host->ecc, CFG, val); |
| 893 | |
| 894 | ecc_layout = nand_chip->ecc.layout; |
| 895 | pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1); |
| 896 | pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]); |
| 897 | pmecc_writel(host->ecc, EADDR, |
| 898 | ecc_layout->eccpos[ecc_layout->eccbytes - 1]); |
| 899 | /* See datasheet about PMECC Clock Control Register */ |
| 900 | pmecc_writel(host->ecc, CLK, 2); |
| 901 | pmecc_writel(host->ecc, IDR, 0xff); |
| 902 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE); |
| 903 | } |
| 904 | |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 905 | /* |
| 906 | * Get ECC requirement in ONFI parameters, returns -1 if ONFI |
| 907 | * parameters is not supported. |
| 908 | * return 0 if success to get the ECC requirement. |
| 909 | */ |
| 910 | static int get_onfi_ecc_param(struct nand_chip *chip, |
| 911 | int *ecc_bits, int *sector_size) |
| 912 | { |
| 913 | *ecc_bits = *sector_size = 0; |
| 914 | |
| 915 | if (chip->onfi_params.ecc_bits == 0xff) |
| 916 | /* TODO: the sector_size and ecc_bits need to be find in |
| 917 | * extended ecc parameter, currently we don't support it. |
| 918 | */ |
| 919 | return -1; |
| 920 | |
| 921 | *ecc_bits = chip->onfi_params.ecc_bits; |
| 922 | |
| 923 | /* The default sector size (ecc codeword size) is 512 */ |
| 924 | *sector_size = 512; |
| 925 | |
| 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | /* |
| 930 | * Get ecc requirement from ONFI parameters ecc requirement. |
| 931 | * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function |
| 932 | * will set them according to ONFI ecc requirement. Otherwise, use the |
| 933 | * value in DTS file. |
| 934 | * return 0 if success. otherwise return error code. |
| 935 | */ |
| 936 | static int pmecc_choose_ecc(struct atmel_nand_host *host, |
| 937 | int *cap, int *sector_size) |
| 938 | { |
| 939 | /* Get ECC requirement from ONFI parameters */ |
| 940 | *cap = *sector_size = 0; |
| 941 | if (host->nand_chip.onfi_version) { |
| 942 | if (!get_onfi_ecc_param(&host->nand_chip, cap, sector_size)) |
| 943 | dev_info(host->dev, "ONFI params, minimum required ECC: %d bits in %d bytes\n", |
| 944 | *cap, *sector_size); |
| 945 | else |
| 946 | dev_info(host->dev, "NAND chip ECC reqirement is in Extended ONFI parameter, we don't support yet.\n"); |
| 947 | } else { |
| 948 | dev_info(host->dev, "NAND chip is not ONFI compliant, assume ecc_bits is 2 in 512 bytes"); |
| 949 | } |
| 950 | if (*cap == 0 && *sector_size == 0) { |
| 951 | *cap = 2; |
| 952 | *sector_size = 512; |
| 953 | } |
| 954 | |
| 955 | /* If dts file doesn't specify then use the one in ONFI parameters */ |
| 956 | if (host->pmecc_corr_cap == 0) { |
| 957 | /* use the most fitable ecc bits (the near bigger one ) */ |
| 958 | if (*cap <= 2) |
| 959 | host->pmecc_corr_cap = 2; |
| 960 | else if (*cap <= 4) |
| 961 | host->pmecc_corr_cap = 4; |
Josh Wu | edc9cba | 2013-07-03 17:56:19 +0800 | [diff] [blame^] | 962 | else if (*cap <= 8) |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 963 | host->pmecc_corr_cap = 8; |
Josh Wu | edc9cba | 2013-07-03 17:56:19 +0800 | [diff] [blame^] | 964 | else if (*cap <= 12) |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 965 | host->pmecc_corr_cap = 12; |
Josh Wu | edc9cba | 2013-07-03 17:56:19 +0800 | [diff] [blame^] | 966 | else if (*cap <= 24) |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 967 | host->pmecc_corr_cap = 24; |
| 968 | else |
| 969 | return -EINVAL; |
| 970 | } |
| 971 | if (host->pmecc_sector_size == 0) { |
| 972 | /* use the most fitable sector size (the near smaller one ) */ |
| 973 | if (*sector_size >= 1024) |
| 974 | host->pmecc_sector_size = 1024; |
| 975 | else if (*sector_size >= 512) |
| 976 | host->pmecc_sector_size = 512; |
| 977 | else |
| 978 | return -EINVAL; |
| 979 | } |
| 980 | return 0; |
| 981 | } |
| 982 | |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 983 | static int __init atmel_pmecc_nand_init_params(struct platform_device *pdev, |
| 984 | struct atmel_nand_host *host) |
| 985 | { |
| 986 | struct mtd_info *mtd = &host->mtd; |
| 987 | struct nand_chip *nand_chip = &host->nand_chip; |
| 988 | struct resource *regs, *regs_pmerr, *regs_rom; |
| 989 | int cap, sector_size, err_no; |
| 990 | |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 991 | err_no = pmecc_choose_ecc(host, &cap, §or_size); |
| 992 | if (err_no) { |
| 993 | dev_err(host->dev, "The NAND flash's ECC requirement are not support!"); |
| 994 | return err_no; |
| 995 | } |
| 996 | |
Richard Genoud | f666d64 | 2013-07-30 17:17:29 +0200 | [diff] [blame] | 997 | if (cap > host->pmecc_corr_cap || |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 998 | sector_size != host->pmecc_sector_size) |
| 999 | dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n"); |
Josh Wu | e66b431 | 2013-01-23 20:47:11 +0800 | [diff] [blame] | 1000 | |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1001 | cap = host->pmecc_corr_cap; |
| 1002 | sector_size = host->pmecc_sector_size; |
Josh Wu | e66b431 | 2013-01-23 20:47:11 +0800 | [diff] [blame] | 1003 | host->pmecc_lookup_table_offset = (sector_size == 512) ? |
| 1004 | host->pmecc_lookup_table_offset_512 : |
| 1005 | host->pmecc_lookup_table_offset_1024; |
| 1006 | |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1007 | dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n", |
| 1008 | cap, sector_size); |
| 1009 | |
| 1010 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1011 | if (!regs) { |
| 1012 | dev_warn(host->dev, |
| 1013 | "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n"); |
| 1014 | nand_chip->ecc.mode = NAND_ECC_SOFT; |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
| 1018 | host->ecc = ioremap(regs->start, resource_size(regs)); |
| 1019 | if (host->ecc == NULL) { |
| 1020 | dev_err(host->dev, "ioremap failed\n"); |
| 1021 | err_no = -EIO; |
| 1022 | goto err_pmecc_ioremap; |
| 1023 | } |
| 1024 | |
| 1025 | regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2); |
| 1026 | regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3); |
| 1027 | if (regs_pmerr && regs_rom) { |
| 1028 | host->pmerrloc_base = ioremap(regs_pmerr->start, |
| 1029 | resource_size(regs_pmerr)); |
| 1030 | host->pmecc_rom_base = ioremap(regs_rom->start, |
| 1031 | resource_size(regs_rom)); |
| 1032 | } |
| 1033 | |
| 1034 | if (!host->pmerrloc_base || !host->pmecc_rom_base) { |
| 1035 | dev_err(host->dev, |
| 1036 | "Can not get I/O resource for PMECC ERRLOC controller or ROM!\n"); |
| 1037 | err_no = -EIO; |
| 1038 | goto err_pmloc_ioremap; |
| 1039 | } |
| 1040 | |
| 1041 | /* ECC is calculated for the whole page (1 step) */ |
| 1042 | nand_chip->ecc.size = mtd->writesize; |
| 1043 | |
| 1044 | /* set ECC page size and oob layout */ |
| 1045 | switch (mtd->writesize) { |
| 1046 | case 2048: |
| 1047 | host->pmecc_degree = PMECC_GF_DIMENSION_13; |
| 1048 | host->pmecc_cw_len = (1 << host->pmecc_degree) - 1; |
| 1049 | host->pmecc_sector_number = mtd->writesize / sector_size; |
| 1050 | host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes( |
| 1051 | cap, sector_size); |
| 1052 | host->pmecc_alpha_to = pmecc_get_alpha_to(host); |
| 1053 | host->pmecc_index_of = host->pmecc_rom_base + |
| 1054 | host->pmecc_lookup_table_offset; |
| 1055 | |
| 1056 | nand_chip->ecc.steps = 1; |
| 1057 | nand_chip->ecc.strength = cap; |
| 1058 | nand_chip->ecc.bytes = host->pmecc_bytes_per_sector * |
| 1059 | host->pmecc_sector_number; |
| 1060 | if (nand_chip->ecc.bytes > mtd->oobsize - 2) { |
| 1061 | dev_err(host->dev, "No room for ECC bytes\n"); |
| 1062 | err_no = -EINVAL; |
| 1063 | goto err_no_ecc_room; |
| 1064 | } |
| 1065 | pmecc_config_ecc_layout(&atmel_pmecc_oobinfo, |
| 1066 | mtd->oobsize, |
| 1067 | nand_chip->ecc.bytes); |
| 1068 | nand_chip->ecc.layout = &atmel_pmecc_oobinfo; |
| 1069 | break; |
| 1070 | case 512: |
| 1071 | case 1024: |
| 1072 | case 4096: |
| 1073 | /* TODO */ |
| 1074 | dev_warn(host->dev, |
| 1075 | "Unsupported page size for PMECC, use Software ECC\n"); |
| 1076 | default: |
| 1077 | /* page size not handled by HW ECC */ |
| 1078 | /* switching back to soft ECC */ |
| 1079 | nand_chip->ecc.mode = NAND_ECC_SOFT; |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
| 1083 | /* Allocate data for PMECC computation */ |
| 1084 | err_no = pmecc_data_alloc(host); |
| 1085 | if (err_no) { |
| 1086 | dev_err(host->dev, |
| 1087 | "Cannot allocate memory for PMECC computation!\n"); |
| 1088 | goto err_pmecc_data_alloc; |
| 1089 | } |
| 1090 | |
| 1091 | nand_chip->ecc.read_page = atmel_nand_pmecc_read_page; |
| 1092 | nand_chip->ecc.write_page = atmel_nand_pmecc_write_page; |
| 1093 | |
| 1094 | atmel_pmecc_core_init(mtd); |
| 1095 | |
| 1096 | return 0; |
| 1097 | |
| 1098 | err_pmecc_data_alloc: |
| 1099 | err_no_ecc_room: |
| 1100 | err_pmloc_ioremap: |
| 1101 | iounmap(host->ecc); |
| 1102 | if (host->pmerrloc_base) |
| 1103 | iounmap(host->pmerrloc_base); |
| 1104 | if (host->pmecc_rom_base) |
| 1105 | iounmap(host->pmecc_rom_base); |
| 1106 | err_pmecc_ioremap: |
| 1107 | return err_no; |
| 1108 | } |
| 1109 | |
| 1110 | /* |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1111 | * Calculate HW ECC |
| 1112 | * |
| 1113 | * function called after a write |
| 1114 | * |
| 1115 | * mtd: MTD block structure |
| 1116 | * dat: raw data (unused) |
| 1117 | * ecc_code: buffer for ECC |
| 1118 | */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1119 | static int atmel_nand_calculate(struct mtd_info *mtd, |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1120 | const u_char *dat, unsigned char *ecc_code) |
| 1121 | { |
| 1122 | struct nand_chip *nand_chip = mtd->priv; |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1123 | struct atmel_nand_host *host = nand_chip->priv; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1124 | unsigned int ecc_value; |
| 1125 | |
| 1126 | /* get the first 2 ECC bytes */ |
Richard Genoud | d43fa14 | 2008-04-25 09:32:26 +0200 | [diff] [blame] | 1127 | ecc_value = ecc_readl(host->ecc, PR); |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1128 | |
Richard Genoud | 3fc2389 | 2008-10-12 08:42:28 +0200 | [diff] [blame] | 1129 | ecc_code[0] = ecc_value & 0xFF; |
| 1130 | ecc_code[1] = (ecc_value >> 8) & 0xFF; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1131 | |
| 1132 | /* get the last 2 ECC bytes */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1133 | ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1134 | |
Richard Genoud | 3fc2389 | 2008-10-12 08:42:28 +0200 | [diff] [blame] | 1135 | ecc_code[2] = ecc_value & 0xFF; |
| 1136 | ecc_code[3] = (ecc_value >> 8) & 0xFF; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1137 | |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
| 1141 | /* |
| 1142 | * HW ECC read page function |
| 1143 | * |
| 1144 | * mtd: mtd info structure |
| 1145 | * chip: nand chip info structure |
| 1146 | * buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1147 | * oob_required: caller expects OOB data read to chip->oob_poi |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1148 | */ |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1149 | static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip, |
| 1150 | uint8_t *buf, int oob_required, int page) |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1151 | { |
| 1152 | int eccsize = chip->ecc.size; |
| 1153 | int eccbytes = chip->ecc.bytes; |
| 1154 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 1155 | uint8_t *p = buf; |
| 1156 | uint8_t *oob = chip->oob_poi; |
| 1157 | uint8_t *ecc_pos; |
| 1158 | int stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1159 | unsigned int max_bitflips = 0; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1160 | |
Haavard Skinnemoen | d6248fd | 2008-07-03 23:40:18 -0700 | [diff] [blame] | 1161 | /* |
| 1162 | * Errata: ALE is incorrectly wired up to the ECC controller |
| 1163 | * on the AP7000, so it will include the address cycles in the |
| 1164 | * ECC calculation. |
| 1165 | * |
| 1166 | * Workaround: Reset the parity registers before reading the |
| 1167 | * actual data. |
| 1168 | */ |
Josh Wu | 71b94e2 | 2013-05-09 15:34:54 +0800 | [diff] [blame] | 1169 | struct atmel_nand_host *host = chip->priv; |
| 1170 | if (host->board.need_reset_workaround) |
Haavard Skinnemoen | d6248fd | 2008-07-03 23:40:18 -0700 | [diff] [blame] | 1171 | ecc_writel(host->ecc, CR, ATMEL_ECC_RST); |
Haavard Skinnemoen | d6248fd | 2008-07-03 23:40:18 -0700 | [diff] [blame] | 1172 | |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1173 | /* read the page */ |
| 1174 | chip->read_buf(mtd, p, eccsize); |
| 1175 | |
| 1176 | /* move to ECC position if needed */ |
| 1177 | if (eccpos[0] != 0) { |
| 1178 | /* This only works on large pages |
| 1179 | * because the ECC controller waits for |
| 1180 | * NAND_CMD_RNDOUTSTART after the |
| 1181 | * NAND_CMD_RNDOUT. |
| 1182 | * anyway, for small pages, the eccpos[0] == 0 |
| 1183 | */ |
| 1184 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, |
| 1185 | mtd->writesize + eccpos[0], -1); |
| 1186 | } |
| 1187 | |
| 1188 | /* the ECC controller needs to read the ECC just after the data */ |
| 1189 | ecc_pos = oob + eccpos[0]; |
| 1190 | chip->read_buf(mtd, ecc_pos, eccbytes); |
| 1191 | |
| 1192 | /* check if there's an error */ |
| 1193 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
| 1194 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1195 | if (stat < 0) { |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1196 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1197 | } else { |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1198 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1199 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1200 | } |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1201 | |
| 1202 | /* get back to oob start (end of page) */ |
| 1203 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1); |
| 1204 | |
| 1205 | /* read the oob */ |
| 1206 | chip->read_buf(mtd, oob, mtd->oobsize); |
| 1207 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1208 | return max_bitflips; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | /* |
| 1212 | * HW ECC Correction |
| 1213 | * |
| 1214 | * function called after a read |
| 1215 | * |
| 1216 | * mtd: MTD block structure |
| 1217 | * dat: raw data read from the chip |
| 1218 | * read_ecc: ECC from the chip (unused) |
| 1219 | * isnull: unused |
| 1220 | * |
| 1221 | * Detect and correct a 1 bit error for a page |
| 1222 | */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1223 | static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat, |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1224 | u_char *read_ecc, u_char *isnull) |
| 1225 | { |
| 1226 | struct nand_chip *nand_chip = mtd->priv; |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1227 | struct atmel_nand_host *host = nand_chip->priv; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1228 | unsigned int ecc_status; |
| 1229 | unsigned int ecc_word, ecc_bit; |
| 1230 | |
| 1231 | /* get the status from the Status Register */ |
| 1232 | ecc_status = ecc_readl(host->ecc, SR); |
| 1233 | |
| 1234 | /* if there's no error */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1235 | if (likely(!(ecc_status & ATMEL_ECC_RECERR))) |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1236 | return 0; |
| 1237 | |
| 1238 | /* get error bit offset (4 bits) */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1239 | ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1240 | /* get word address (12 bits) */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1241 | ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1242 | ecc_word >>= 4; |
| 1243 | |
| 1244 | /* if there are multiple errors */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1245 | if (ecc_status & ATMEL_ECC_MULERR) { |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1246 | /* check if it is a freshly erased block |
| 1247 | * (filled with 0xff) */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1248 | if ((ecc_bit == ATMEL_ECC_BITADDR) |
| 1249 | && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) { |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1250 | /* the block has just been erased, return OK */ |
| 1251 | return 0; |
| 1252 | } |
| 1253 | /* it doesn't seems to be a freshly |
| 1254 | * erased block. |
| 1255 | * We can't correct so many errors */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1256 | dev_dbg(host->dev, "atmel_nand : multiple errors detected." |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1257 | " Unable to correct.\n"); |
| 1258 | return -EIO; |
| 1259 | } |
| 1260 | |
| 1261 | /* if there's a single bit error : we can correct it */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1262 | if (ecc_status & ATMEL_ECC_ECCERR) { |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1263 | /* there's nothing much to do here. |
| 1264 | * the bit error is on the ECC itself. |
| 1265 | */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1266 | dev_dbg(host->dev, "atmel_nand : one bit error on ECC code." |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1267 | " Nothing to correct\n"); |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1271 | dev_dbg(host->dev, "atmel_nand : one bit error on data." |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1272 | " (word offset in the page :" |
| 1273 | " 0x%x bit offset : 0x%x)\n", |
| 1274 | ecc_word, ecc_bit); |
| 1275 | /* correct the error */ |
| 1276 | if (nand_chip->options & NAND_BUSWIDTH_16) { |
| 1277 | /* 16 bits words */ |
| 1278 | ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit); |
| 1279 | } else { |
| 1280 | /* 8 bits words */ |
| 1281 | dat[ecc_word] ^= (1 << ecc_bit); |
| 1282 | } |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1283 | dev_dbg(host->dev, "atmel_nand : error corrected\n"); |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1284 | return 1; |
| 1285 | } |
| 1286 | |
| 1287 | /* |
Haavard Skinnemoen | d6248fd | 2008-07-03 23:40:18 -0700 | [diff] [blame] | 1288 | * Enable HW ECC : unused on most chips |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1289 | */ |
Haavard Skinnemoen | d6248fd | 2008-07-03 23:40:18 -0700 | [diff] [blame] | 1290 | static void atmel_nand_hwctl(struct mtd_info *mtd, int mode) |
| 1291 | { |
Josh Wu | 71b94e2 | 2013-05-09 15:34:54 +0800 | [diff] [blame] | 1292 | struct nand_chip *nand_chip = mtd->priv; |
| 1293 | struct atmel_nand_host *host = nand_chip->priv; |
| 1294 | |
| 1295 | if (host->board.need_reset_workaround) |
Haavard Skinnemoen | d6248fd | 2008-07-03 23:40:18 -0700 | [diff] [blame] | 1296 | ecc_writel(host->ecc, CR, ATMEL_ECC_RST); |
Haavard Skinnemoen | d6248fd | 2008-07-03 23:40:18 -0700 | [diff] [blame] | 1297 | } |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1298 | |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1299 | #if defined(CONFIG_OF) |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 1300 | static int atmel_of_init_port(struct atmel_nand_host *host, |
Greg Kroah-Hartman | d892994 | 2012-12-21 13:19:05 -0800 | [diff] [blame] | 1301 | struct device_node *np) |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1302 | { |
Josh Wu | c0cf787 | 2013-01-23 20:47:08 +0800 | [diff] [blame] | 1303 | u32 val; |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1304 | u32 offset[2]; |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1305 | int ecc_mode; |
| 1306 | struct atmel_nand_data *board = &host->board; |
| 1307 | enum of_gpio_flags flags; |
| 1308 | |
| 1309 | if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) { |
| 1310 | if (val >= 32) { |
| 1311 | dev_err(host->dev, "invalid addr-offset %u\n", val); |
| 1312 | return -EINVAL; |
| 1313 | } |
| 1314 | board->ale = val; |
| 1315 | } |
| 1316 | |
| 1317 | if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) { |
| 1318 | if (val >= 32) { |
| 1319 | dev_err(host->dev, "invalid cmd-offset %u\n", val); |
| 1320 | return -EINVAL; |
| 1321 | } |
| 1322 | board->cle = val; |
| 1323 | } |
| 1324 | |
| 1325 | ecc_mode = of_get_nand_ecc_mode(np); |
| 1326 | |
| 1327 | board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode; |
| 1328 | |
| 1329 | board->on_flash_bbt = of_get_nand_on_flash_bbt(np); |
| 1330 | |
Josh Wu | 1b71926 | 2013-05-09 15:34:55 +0800 | [diff] [blame] | 1331 | board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma"); |
| 1332 | |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1333 | if (of_get_nand_bus_width(np) == 16) |
| 1334 | board->bus_width_16 = 1; |
| 1335 | |
| 1336 | board->rdy_pin = of_get_gpio_flags(np, 0, &flags); |
| 1337 | board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW); |
| 1338 | |
| 1339 | board->enable_pin = of_get_gpio(np, 1); |
| 1340 | board->det_pin = of_get_gpio(np, 2); |
| 1341 | |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1342 | host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc"); |
| 1343 | |
| 1344 | if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc) |
| 1345 | return 0; /* Not using PMECC */ |
| 1346 | |
| 1347 | /* use PMECC, get correction capability, sector size and lookup |
| 1348 | * table offset. |
Josh Wu | e66b431 | 2013-01-23 20:47:11 +0800 | [diff] [blame] | 1349 | * If correction bits and sector size are not specified, then find |
| 1350 | * them from NAND ONFI parameters. |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1351 | */ |
Josh Wu | e66b431 | 2013-01-23 20:47:11 +0800 | [diff] [blame] | 1352 | if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) { |
| 1353 | if ((val != 2) && (val != 4) && (val != 8) && (val != 12) && |
| 1354 | (val != 24)) { |
| 1355 | dev_err(host->dev, |
| 1356 | "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n", |
| 1357 | val); |
| 1358 | return -EINVAL; |
| 1359 | } |
| 1360 | host->pmecc_corr_cap = (u8)val; |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1361 | } |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1362 | |
Josh Wu | e66b431 | 2013-01-23 20:47:11 +0800 | [diff] [blame] | 1363 | if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) { |
| 1364 | if ((val != 512) && (val != 1024)) { |
| 1365 | dev_err(host->dev, |
| 1366 | "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n", |
| 1367 | val); |
| 1368 | return -EINVAL; |
| 1369 | } |
| 1370 | host->pmecc_sector_size = (u16)val; |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1371 | } |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1372 | |
| 1373 | if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset", |
| 1374 | offset, 2) != 0) { |
| 1375 | dev_err(host->dev, "Cannot get PMECC lookup table offset\n"); |
| 1376 | return -EINVAL; |
| 1377 | } |
Josh Wu | c0cf787 | 2013-01-23 20:47:08 +0800 | [diff] [blame] | 1378 | if (!offset[0] && !offset[1]) { |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1379 | dev_err(host->dev, "Invalid PMECC lookup table offset\n"); |
| 1380 | return -EINVAL; |
| 1381 | } |
Josh Wu | e66b431 | 2013-01-23 20:47:11 +0800 | [diff] [blame] | 1382 | host->pmecc_lookup_table_offset_512 = offset[0]; |
| 1383 | host->pmecc_lookup_table_offset_1024 = offset[1]; |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1384 | |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1385 | return 0; |
| 1386 | } |
| 1387 | #else |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 1388 | static int atmel_of_init_port(struct atmel_nand_host *host, |
Greg Kroah-Hartman | d892994 | 2012-12-21 13:19:05 -0800 | [diff] [blame] | 1389 | struct device_node *np) |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1390 | { |
| 1391 | return -EINVAL; |
| 1392 | } |
| 1393 | #endif |
| 1394 | |
Josh Wu | 3dfe41a | 2012-06-25 18:07:43 +0800 | [diff] [blame] | 1395 | static int __init atmel_hw_nand_init_params(struct platform_device *pdev, |
| 1396 | struct atmel_nand_host *host) |
| 1397 | { |
| 1398 | struct mtd_info *mtd = &host->mtd; |
| 1399 | struct nand_chip *nand_chip = &host->nand_chip; |
| 1400 | struct resource *regs; |
| 1401 | |
| 1402 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1403 | if (!regs) { |
| 1404 | dev_err(host->dev, |
| 1405 | "Can't get I/O resource regs, use software ECC\n"); |
| 1406 | nand_chip->ecc.mode = NAND_ECC_SOFT; |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
| 1410 | host->ecc = ioremap(regs->start, resource_size(regs)); |
| 1411 | if (host->ecc == NULL) { |
| 1412 | dev_err(host->dev, "ioremap failed\n"); |
| 1413 | return -EIO; |
| 1414 | } |
| 1415 | |
| 1416 | /* ECC is calculated for the whole page (1 step) */ |
| 1417 | nand_chip->ecc.size = mtd->writesize; |
| 1418 | |
| 1419 | /* set ECC page size and oob layout */ |
| 1420 | switch (mtd->writesize) { |
| 1421 | case 512: |
| 1422 | nand_chip->ecc.layout = &atmel_oobinfo_small; |
| 1423 | ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528); |
| 1424 | break; |
| 1425 | case 1024: |
| 1426 | nand_chip->ecc.layout = &atmel_oobinfo_large; |
| 1427 | ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056); |
| 1428 | break; |
| 1429 | case 2048: |
| 1430 | nand_chip->ecc.layout = &atmel_oobinfo_large; |
| 1431 | ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112); |
| 1432 | break; |
| 1433 | case 4096: |
| 1434 | nand_chip->ecc.layout = &atmel_oobinfo_large; |
| 1435 | ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224); |
| 1436 | break; |
| 1437 | default: |
| 1438 | /* page size not handled by HW ECC */ |
| 1439 | /* switching back to soft ECC */ |
| 1440 | nand_chip->ecc.mode = NAND_ECC_SOFT; |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
| 1444 | /* set up for HW ECC */ |
| 1445 | nand_chip->ecc.calculate = atmel_nand_calculate; |
| 1446 | nand_chip->ecc.correct = atmel_nand_correct; |
| 1447 | nand_chip->ecc.hwctl = atmel_nand_hwctl; |
| 1448 | nand_chip->ecc.read_page = atmel_nand_read_page; |
| 1449 | nand_chip->ecc.bytes = 4; |
| 1450 | nand_chip->ecc.strength = 1; |
| 1451 | |
| 1452 | return 0; |
| 1453 | } |
| 1454 | |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1455 | /* |
| 1456 | * Probe for the NAND device. |
| 1457 | */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1458 | static int __init atmel_nand_probe(struct platform_device *pdev) |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1459 | { |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1460 | struct atmel_nand_host *host; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1461 | struct mtd_info *mtd; |
| 1462 | struct nand_chip *nand_chip; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1463 | struct resource *mem; |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1464 | struct mtd_part_parser_data ppdata = {}; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1465 | int res; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1466 | |
Håvard Skinnemoen | cc0c72e | 2008-06-06 18:04:54 +0200 | [diff] [blame] | 1467 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1468 | if (!mem) { |
| 1469 | printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n"); |
| 1470 | return -ENXIO; |
| 1471 | } |
| 1472 | |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1473 | /* Allocate memory for the device structure (and zero it) */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1474 | host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1475 | if (!host) { |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1476 | printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n"); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1477 | return -ENOMEM; |
| 1478 | } |
| 1479 | |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 1480 | host->io_phys = (dma_addr_t)mem->start; |
| 1481 | |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 1482 | host->io_base = ioremap(mem->start, resource_size(mem)); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1483 | if (host->io_base == NULL) { |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1484 | printk(KERN_ERR "atmel_nand: ioremap failed\n"); |
Håvard Skinnemoen | cc0c72e | 2008-06-06 18:04:54 +0200 | [diff] [blame] | 1485 | res = -EIO; |
| 1486 | goto err_nand_ioremap; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | mtd = &host->mtd; |
| 1490 | nand_chip = &host->nand_chip; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1491 | host->dev = &pdev->dev; |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1492 | if (pdev->dev.of_node) { |
| 1493 | res = atmel_of_init_port(host, pdev->dev.of_node); |
| 1494 | if (res) |
Jean-Christophe PLAGNIOL-VILLARD | 28446ac | 2012-07-12 10:31:08 +0200 | [diff] [blame] | 1495 | goto err_ecc_ioremap; |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1496 | } else { |
| 1497 | memcpy(&host->board, pdev->dev.platform_data, |
| 1498 | sizeof(struct atmel_nand_data)); |
| 1499 | } |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1500 | |
| 1501 | nand_chip->priv = host; /* link the private data structures */ |
| 1502 | mtd->priv = nand_chip; |
| 1503 | mtd->owner = THIS_MODULE; |
| 1504 | |
| 1505 | /* Set address of NAND IO lines */ |
| 1506 | nand_chip->IO_ADDR_R = host->io_base; |
| 1507 | nand_chip->IO_ADDR_W = host->io_base; |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1508 | nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl; |
Ivan Kuten | a4265f8 | 2007-05-24 14:35:58 +0300 | [diff] [blame] | 1509 | |
Jean-Christophe PLAGNIOL-VILLARD | 28446ac | 2012-07-12 10:31:08 +0200 | [diff] [blame] | 1510 | if (gpio_is_valid(host->board.rdy_pin)) { |
| 1511 | res = gpio_request(host->board.rdy_pin, "nand_rdy"); |
| 1512 | if (res < 0) { |
| 1513 | dev_err(&pdev->dev, |
| 1514 | "can't request rdy gpio %d\n", |
| 1515 | host->board.rdy_pin); |
| 1516 | goto err_ecc_ioremap; |
| 1517 | } |
| 1518 | |
| 1519 | res = gpio_direction_input(host->board.rdy_pin); |
| 1520 | if (res < 0) { |
| 1521 | dev_err(&pdev->dev, |
| 1522 | "can't request input direction rdy gpio %d\n", |
| 1523 | host->board.rdy_pin); |
| 1524 | goto err_ecc_ioremap; |
| 1525 | } |
| 1526 | |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1527 | nand_chip->dev_ready = atmel_nand_device_ready; |
Jean-Christophe PLAGNIOL-VILLARD | 28446ac | 2012-07-12 10:31:08 +0200 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | if (gpio_is_valid(host->board.enable_pin)) { |
| 1531 | res = gpio_request(host->board.enable_pin, "nand_enable"); |
| 1532 | if (res < 0) { |
| 1533 | dev_err(&pdev->dev, |
| 1534 | "can't request enable gpio %d\n", |
| 1535 | host->board.enable_pin); |
| 1536 | goto err_ecc_ioremap; |
| 1537 | } |
| 1538 | |
| 1539 | res = gpio_direction_output(host->board.enable_pin, 1); |
| 1540 | if (res < 0) { |
| 1541 | dev_err(&pdev->dev, |
| 1542 | "can't request output direction enable gpio %d\n", |
| 1543 | host->board.enable_pin); |
| 1544 | goto err_ecc_ioremap; |
| 1545 | } |
| 1546 | } |
Ivan Kuten | a4265f8 | 2007-05-24 14:35:58 +0300 | [diff] [blame] | 1547 | |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1548 | nand_chip->ecc.mode = host->board.ecc_mode; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1549 | nand_chip->chip_delay = 20; /* 20us command delay time */ |
| 1550 | |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1551 | if (host->board.bus_width_16) /* 16-bit bus width */ |
Andrew Victor | dd11b8c | 2006-12-08 13:49:42 +0200 | [diff] [blame] | 1552 | nand_chip->options |= NAND_BUSWIDTH_16; |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 1553 | |
| 1554 | nand_chip->read_buf = atmel_read_buf; |
| 1555 | nand_chip->write_buf = atmel_write_buf; |
Andrew Victor | dd11b8c | 2006-12-08 13:49:42 +0200 | [diff] [blame] | 1556 | |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1557 | platform_set_drvdata(pdev, host); |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1558 | atmel_nand_enable(host); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1559 | |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1560 | if (gpio_is_valid(host->board.det_pin)) { |
Jean-Christophe PLAGNIOL-VILLARD | 28446ac | 2012-07-12 10:31:08 +0200 | [diff] [blame] | 1561 | res = gpio_request(host->board.det_pin, "nand_det"); |
| 1562 | if (res < 0) { |
| 1563 | dev_err(&pdev->dev, |
| 1564 | "can't request det gpio %d\n", |
| 1565 | host->board.det_pin); |
| 1566 | goto err_no_card; |
| 1567 | } |
| 1568 | |
| 1569 | res = gpio_direction_input(host->board.det_pin); |
| 1570 | if (res < 0) { |
| 1571 | dev_err(&pdev->dev, |
| 1572 | "can't request input direction det gpio %d\n", |
| 1573 | host->board.det_pin); |
| 1574 | goto err_no_card; |
| 1575 | } |
| 1576 | |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1577 | if (gpio_get_value(host->board.det_pin)) { |
Simon Polette | f4fa697c | 2009-05-27 18:19:39 +0300 | [diff] [blame] | 1578 | printk(KERN_INFO "No SmartMedia card inserted.\n"); |
Roel Kluin | 895fb49 | 2009-11-11 21:47:06 +0100 | [diff] [blame] | 1579 | res = -ENXIO; |
Håvard Skinnemoen | cc0c72e | 2008-06-06 18:04:54 +0200 | [diff] [blame] | 1580 | goto err_no_card; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1581 | } |
| 1582 | } |
| 1583 | |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1584 | if (host->board.on_flash_bbt || on_flash_bbt) { |
Simon Polette | f4fa697c | 2009-05-27 18:19:39 +0300 | [diff] [blame] | 1585 | printk(KERN_INFO "atmel_nand: Use On Flash BBT\n"); |
Brian Norris | bb9ebd4 | 2011-05-31 16:31:23 -0700 | [diff] [blame] | 1586 | nand_chip->bbt_options |= NAND_BBT_USE_FLASH; |
Simon Polette | f4fa697c | 2009-05-27 18:19:39 +0300 | [diff] [blame] | 1587 | } |
| 1588 | |
Josh Wu | 1b71926 | 2013-05-09 15:34:55 +0800 | [diff] [blame] | 1589 | if (!host->board.has_dma) |
Hong Xu | cb457a4 | 2011-03-30 16:26:41 +0800 | [diff] [blame] | 1590 | use_dma = 0; |
| 1591 | |
| 1592 | if (use_dma) { |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 1593 | dma_cap_mask_t mask; |
| 1594 | |
| 1595 | dma_cap_zero(mask); |
| 1596 | dma_cap_set(DMA_MEMCPY, mask); |
Nicolas Ferre | 201ab53 | 2011-06-29 18:41:16 +0200 | [diff] [blame] | 1597 | host->dma_chan = dma_request_channel(mask, NULL, NULL); |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 1598 | if (!host->dma_chan) { |
| 1599 | dev_err(host->dev, "Failed to request DMA channel\n"); |
| 1600 | use_dma = 0; |
| 1601 | } |
| 1602 | } |
| 1603 | if (use_dma) |
Nicolas Ferre | 042bc9c | 2011-03-30 16:26:40 +0800 | [diff] [blame] | 1604 | dev_info(host->dev, "Using %s for DMA transfers.\n", |
| 1605 | dma_chan_name(host->dma_chan)); |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 1606 | else |
| 1607 | dev_info(host->dev, "No DMA support for NAND access.\n"); |
| 1608 | |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1609 | /* first scan to find the device and get the page size */ |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 1610 | if (nand_scan_ident(mtd, 1, NULL)) { |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1611 | res = -ENXIO; |
Håvard Skinnemoen | cc0c72e | 2008-06-06 18:04:54 +0200 | [diff] [blame] | 1612 | goto err_scan_ident; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1613 | } |
| 1614 | |
Richard Genoud | 3fc2389 | 2008-10-12 08:42:28 +0200 | [diff] [blame] | 1615 | if (nand_chip->ecc.mode == NAND_ECC_HW) { |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1616 | if (host->has_pmecc) |
| 1617 | res = atmel_pmecc_nand_init_params(pdev, host); |
| 1618 | else |
| 1619 | res = atmel_hw_nand_init_params(pdev, host); |
| 1620 | |
Josh Wu | 3dfe41a | 2012-06-25 18:07:43 +0800 | [diff] [blame] | 1621 | if (res != 0) |
| 1622 | goto err_hw_ecc; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | /* second phase scan */ |
| 1626 | if (nand_scan_tail(mtd)) { |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1627 | res = -ENXIO; |
Håvard Skinnemoen | cc0c72e | 2008-06-06 18:04:54 +0200 | [diff] [blame] | 1628 | goto err_scan_tail; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1629 | } |
| 1630 | |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1631 | mtd->name = "atmel_nand"; |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1632 | ppdata.of_node = pdev->dev.of_node; |
| 1633 | res = mtd_device_parse_register(mtd, NULL, &ppdata, |
| 1634 | host->board.parts, host->board.num_parts); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1635 | if (!res) |
| 1636 | return res; |
| 1637 | |
Håvard Skinnemoen | cc0c72e | 2008-06-06 18:04:54 +0200 | [diff] [blame] | 1638 | err_scan_tail: |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1639 | if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) { |
| 1640 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE); |
| 1641 | pmecc_data_free(host); |
| 1642 | } |
Josh Wu | 3dfe41a | 2012-06-25 18:07:43 +0800 | [diff] [blame] | 1643 | if (host->ecc) |
| 1644 | iounmap(host->ecc); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1645 | if (host->pmerrloc_base) |
| 1646 | iounmap(host->pmerrloc_base); |
| 1647 | if (host->pmecc_rom_base) |
| 1648 | iounmap(host->pmecc_rom_base); |
Josh Wu | 3dfe41a | 2012-06-25 18:07:43 +0800 | [diff] [blame] | 1649 | err_hw_ecc: |
Håvard Skinnemoen | cc0c72e | 2008-06-06 18:04:54 +0200 | [diff] [blame] | 1650 | err_scan_ident: |
| 1651 | err_no_card: |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1652 | atmel_nand_disable(host); |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 1653 | if (host->dma_chan) |
| 1654 | dma_release_channel(host->dma_chan); |
Jean-Christophe PLAGNIOL-VILLARD | 28446ac | 2012-07-12 10:31:08 +0200 | [diff] [blame] | 1655 | err_ecc_ioremap: |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1656 | iounmap(host->io_base); |
Håvard Skinnemoen | cc0c72e | 2008-06-06 18:04:54 +0200 | [diff] [blame] | 1657 | err_nand_ioremap: |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1658 | kfree(host); |
| 1659 | return res; |
| 1660 | } |
| 1661 | |
| 1662 | /* |
| 1663 | * Remove a NAND device. |
| 1664 | */ |
David Brownell | 23a346c | 2008-07-03 23:40:16 -0700 | [diff] [blame] | 1665 | static int __exit atmel_nand_remove(struct platform_device *pdev) |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1666 | { |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1667 | struct atmel_nand_host *host = platform_get_drvdata(pdev); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1668 | struct mtd_info *mtd = &host->mtd; |
| 1669 | |
| 1670 | nand_release(mtd); |
| 1671 | |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1672 | atmel_nand_disable(host); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1673 | |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1674 | if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) { |
| 1675 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE); |
| 1676 | pmerrloc_writel(host->pmerrloc_base, ELDIS, |
| 1677 | PMERRLOC_DISABLE); |
| 1678 | pmecc_data_free(host); |
| 1679 | } |
| 1680 | |
Jean-Christophe PLAGNIOL-VILLARD | 28446ac | 2012-07-12 10:31:08 +0200 | [diff] [blame] | 1681 | if (gpio_is_valid(host->board.det_pin)) |
| 1682 | gpio_free(host->board.det_pin); |
| 1683 | |
| 1684 | if (gpio_is_valid(host->board.enable_pin)) |
| 1685 | gpio_free(host->board.enable_pin); |
| 1686 | |
| 1687 | if (gpio_is_valid(host->board.rdy_pin)) |
| 1688 | gpio_free(host->board.rdy_pin); |
| 1689 | |
Håvard Skinnemoen | cc0c72e | 2008-06-06 18:04:54 +0200 | [diff] [blame] | 1690 | if (host->ecc) |
| 1691 | iounmap(host->ecc); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1692 | if (host->pmecc_rom_base) |
| 1693 | iounmap(host->pmecc_rom_base); |
| 1694 | if (host->pmerrloc_base) |
| 1695 | iounmap(host->pmerrloc_base); |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 1696 | |
| 1697 | if (host->dma_chan) |
| 1698 | dma_release_channel(host->dma_chan); |
| 1699 | |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1700 | iounmap(host->io_base); |
| 1701 | kfree(host); |
| 1702 | |
| 1703 | return 0; |
| 1704 | } |
| 1705 | |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1706 | #if defined(CONFIG_OF) |
| 1707 | static const struct of_device_id atmel_nand_dt_ids[] = { |
| 1708 | { .compatible = "atmel,at91rm9200-nand" }, |
| 1709 | { /* sentinel */ } |
| 1710 | }; |
| 1711 | |
| 1712 | MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids); |
| 1713 | #endif |
| 1714 | |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1715 | static struct platform_driver atmel_nand_driver = { |
David Brownell | 23a346c | 2008-07-03 23:40:16 -0700 | [diff] [blame] | 1716 | .remove = __exit_p(atmel_nand_remove), |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1717 | .driver = { |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1718 | .name = "atmel_nand", |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1719 | .owner = THIS_MODULE, |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1720 | .of_match_table = of_match_ptr(atmel_nand_dt_ids), |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1721 | }, |
| 1722 | }; |
| 1723 | |
Jingoo Han | c5345ed | 2013-03-05 13:30:04 +0900 | [diff] [blame] | 1724 | module_platform_driver_probe(atmel_nand_driver, atmel_nand_probe); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1725 | |
| 1726 | MODULE_LICENSE("GPL"); |
| 1727 | MODULE_AUTHOR("Rick Bronson"); |
Håvard Skinnemoen | d4f4c0a | 2008-06-06 18:04:52 +0200 | [diff] [blame] | 1728 | MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32"); |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1729 | MODULE_ALIAS("platform:atmel_nand"); |