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