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