blob: f186a371e4372aa189eb4ac4f4a2702d7e229a49 [file] [log] [blame]
Andrew Victor42cb1402006-10-19 18:24:35 +02001/*
Josh Wu1c7b8742012-06-29 17:47:55 +08002 * Copyright © 2003 Rick Bronson
Andrew Victor42cb1402006-10-19 18:24:35 +02003 *
4 * Derived from drivers/mtd/nand/autcpu12.c
Josh Wu1c7b8742012-06-29 17:47:55 +08005 * Copyright © 2001 Thomas Gleixner (gleixner@autronix.de)
Andrew Victor42cb1402006-10-19 18:24:35 +02006 *
7 * Derived from drivers/mtd/spia.c
Josh Wu1c7b8742012-06-29 17:47:55 +08008 * Copyright © 2000 Steven J. Hill (sjhill@cotw.com)
Andrew Victor42cb1402006-10-19 18:24:35 +02009 *
Richard Genoud77f54922008-04-23 19:51:14 +020010 *
11 * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
Josh Wu1c7b8742012-06-29 17:47:55 +080012 * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright © 2007
Richard Genoud77f54922008-04-23 19:51:14 +020013 *
14 * Derived from Das U-Boot source code
15 * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
Josh Wu1c7b8742012-06-29 17:47:55 +080016 * © Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
Richard Genoud77f54922008-04-23 19:51:14 +020017 *
Josh Wu1c7b8742012-06-29 17:47:55 +080018 * Add Programmable Multibit ECC support for various AT91 SoC
19 * © Copyright 2012 ATMEL, Hong Xu
Richard Genoud77f54922008-04-23 19:51:14 +020020 *
Andrew Victor42cb1402006-10-19 18:24:35 +020021 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License version 2 as
23 * published by the Free Software Foundation.
24 *
25 */
26
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000027#include <linux/dma-mapping.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020028#include <linux/slab.h>
29#include <linux/module.h>
Simon Polettef4fa697c2009-05-27 18:19:39 +030030#include <linux/moduleparam.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020031#include <linux/platform_device.h>
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +080032#include <linux/of.h>
33#include <linux/of_device.h>
34#include <linux/of_gpio.h>
35#include <linux/of_mtd.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020036#include <linux/mtd/mtd.h>
37#include <linux/mtd/nand.h>
38#include <linux/mtd/partitions.h>
39
Hans-Christian Egtvedt5c39c4c2011-04-13 15:55:17 +020040#include <linux/dmaengine.h>
David Woodhouse90574d02008-06-07 08:49:00 +010041#include <linux/gpio.h>
42#include <linux/io.h>
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +080043#include <linux/platform_data/atmel.h>
Jean-Christophe PLAGNIOL-VILLARD251e7832012-07-12 23:31:39 +080044#include <linux/pinctrl/consumer.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020045
Russell Kinga09e64f2008-08-05 16:14:15 +010046#include <mach/cpu.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020047
Hong Xucbc6c5e2011-01-18 14:36:05 +080048static int use_dma = 1;
49module_param(use_dma, int, 0);
50
Simon Polettef4fa697c2009-05-27 18:19:39 +030051static int on_flash_bbt = 0;
52module_param(on_flash_bbt, int, 0);
53
Richard Genoud77f54922008-04-23 19:51:14 +020054/* Register access macros */
55#define ecc_readl(add, reg) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020056 __raw_readl(add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020057#define ecc_writel(add, reg, value) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020058 __raw_writel((value), add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020059
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020060#include "atmel_nand_ecc.h" /* Hardware ECC registers */
Richard Genoud77f54922008-04-23 19:51:14 +020061
62/* oob layout for large page size
63 * bad block info is on bytes 0 and 1
64 * the bytes have to be consecutives to avoid
65 * several NAND_CMD_RNDOUT during read
66 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020067static struct nand_ecclayout atmel_oobinfo_large = {
Richard Genoud77f54922008-04-23 19:51:14 +020068 .eccbytes = 4,
69 .eccpos = {60, 61, 62, 63},
70 .oobfree = {
71 {2, 58}
72 },
73};
74
75/* oob layout for small page size
76 * bad block info is on bytes 4 and 5
77 * the bytes have to be consecutives to avoid
78 * several NAND_CMD_RNDOUT during read
79 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020080static struct nand_ecclayout atmel_oobinfo_small = {
Richard Genoud77f54922008-04-23 19:51:14 +020081 .eccbytes = 4,
82 .eccpos = {0, 1, 2, 3},
83 .oobfree = {
84 {6, 10}
85 },
86};
87
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020088struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +020089 struct nand_chip nand_chip;
90 struct mtd_info mtd;
91 void __iomem *io_base;
Hong Xucbc6c5e2011-01-18 14:36:05 +080092 dma_addr_t io_phys;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +080093 struct atmel_nand_data board;
Richard Genoud77f54922008-04-23 19:51:14 +020094 struct device *dev;
95 void __iomem *ecc;
Hong Xucbc6c5e2011-01-18 14:36:05 +080096
97 struct completion comp;
98 struct dma_chan *dma_chan;
Josh Wua41b51a2012-06-29 17:47:54 +080099
100 bool has_pmecc;
101 u8 pmecc_corr_cap;
102 u16 pmecc_sector_size;
103 u32 pmecc_lookup_table_offset;
Josh Wue66b4312013-01-23 20:47:11 +0800104 u32 pmecc_lookup_table_offset_512;
105 u32 pmecc_lookup_table_offset_1024;
Josh Wu1c7b8742012-06-29 17:47:55 +0800106
107 int pmecc_bytes_per_sector;
108 int pmecc_sector_number;
109 int pmecc_degree; /* Degree of remainders */
110 int pmecc_cw_len; /* Length of codeword */
111
112 void __iomem *pmerrloc_base;
113 void __iomem *pmecc_rom_base;
114
115 /* lookup table for alpha_to and index_of */
116 void __iomem *pmecc_alpha_to;
117 void __iomem *pmecc_index_of;
118
119 /* data for pmecc computation */
120 int16_t *pmecc_partial_syn;
121 int16_t *pmecc_si;
122 int16_t *pmecc_smu; /* Sigma table */
123 int16_t *pmecc_lmu; /* polynomal order */
124 int *pmecc_mu;
125 int *pmecc_dmu;
126 int *pmecc_delta;
Andrew Victor42cb1402006-10-19 18:24:35 +0200127};
128
Josh Wu1c7b8742012-06-29 17:47:55 +0800129static struct nand_ecclayout atmel_pmecc_oobinfo;
130
Hong Xucbc6c5e2011-01-18 14:36:05 +0800131static int cpu_has_dma(void)
132{
133 return cpu_is_at91sam9rl() || cpu_is_at91sam9g45();
134}
135
Andrew Victor42cb1402006-10-19 18:24:35 +0200136/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900137 * Enable NAND.
138 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200139static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900140{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800141 if (gpio_is_valid(host->board.enable_pin))
142 gpio_set_value(host->board.enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900143}
144
145/*
146 * Disable NAND.
147 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200148static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900149{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800150 if (gpio_is_valid(host->board.enable_pin))
151 gpio_set_value(host->board.enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900152}
153
154/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200155 * Hardware specific access to control-lines
156 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200157static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200158{
159 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200160 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200161
Atsushi Nemoto81365082008-04-27 01:51:12 +0900162 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900163 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200164 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900165 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200166 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900167 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200168 if (cmd == NAND_CMD_NONE)
169 return;
170
171 if (ctrl & NAND_CLE)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800172 writeb(cmd, host->io_base + (1 << host->board.cle));
Andrew Victor42cb1402006-10-19 18:24:35 +0200173 else
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800174 writeb(cmd, host->io_base + (1 << host->board.ale));
Andrew Victor42cb1402006-10-19 18:24:35 +0200175}
176
177/*
178 * Read the Device Ready pin.
179 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200180static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200181{
182 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200183 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200184
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800185 return gpio_get_value(host->board.rdy_pin) ^
186 !!host->board.rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200187}
188
Artem Bityutskiy50082312012-02-02 13:54:25 +0200189/*
190 * Minimal-overhead PIO for data access.
191 */
192static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
193{
194 struct nand_chip *nand_chip = mtd->priv;
195
196 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
197}
198
199static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
200{
201 struct nand_chip *nand_chip = mtd->priv;
202
203 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
204}
205
206static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
207{
208 struct nand_chip *nand_chip = mtd->priv;
209
210 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
211}
212
213static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
214{
215 struct nand_chip *nand_chip = mtd->priv;
216
217 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
218}
219
Hong Xucbc6c5e2011-01-18 14:36:05 +0800220static void dma_complete_func(void *completion)
221{
222 complete(completion);
223}
224
225static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
226 int is_read)
227{
228 struct dma_device *dma_dev;
229 enum dma_ctrl_flags flags;
230 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
231 struct dma_async_tx_descriptor *tx = NULL;
232 dma_cookie_t cookie;
233 struct nand_chip *chip = mtd->priv;
234 struct atmel_nand_host *host = chip->priv;
235 void *p = buf;
236 int err = -EIO;
237 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
238
Hong Xu80b4f812011-03-31 18:33:15 +0800239 if (buf >= high_memory)
240 goto err_buf;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800241
242 dma_dev = host->dma_chan->device;
243
244 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_SRC_UNMAP |
245 DMA_COMPL_SKIP_DEST_UNMAP;
246
247 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
248 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
249 dev_err(host->dev, "Failed to dma_map_single\n");
250 goto err_buf;
251 }
252
253 if (is_read) {
254 dma_src_addr = host->io_phys;
255 dma_dst_addr = phys_addr;
256 } else {
257 dma_src_addr = phys_addr;
258 dma_dst_addr = host->io_phys;
259 }
260
261 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
262 dma_src_addr, len, flags);
263 if (!tx) {
264 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
265 goto err_dma;
266 }
267
268 init_completion(&host->comp);
269 tx->callback = dma_complete_func;
270 tx->callback_param = &host->comp;
271
272 cookie = tx->tx_submit(tx);
273 if (dma_submit_error(cookie)) {
274 dev_err(host->dev, "Failed to do DMA tx_submit\n");
275 goto err_dma;
276 }
277
278 dma_async_issue_pending(host->dma_chan);
279 wait_for_completion(&host->comp);
280
281 err = 0;
282
283err_dma:
284 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
285err_buf:
286 if (err != 0)
287 dev_warn(host->dev, "Fall back to CPU I/O\n");
288 return err;
289}
290
291static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
292{
293 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200294 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800295
Nicolas Ferre9d515672011-04-01 16:40:44 +0200296 if (use_dma && len > mtd->oobsize)
297 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800298 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
299 return;
300
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800301 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200302 atmel_read_buf16(mtd, buf, len);
303 else
304 atmel_read_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800305}
306
307static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
308{
309 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200310 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800311
Nicolas Ferre9d515672011-04-01 16:40:44 +0200312 if (use_dma && len > mtd->oobsize)
313 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800314 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
315 return;
316
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800317 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200318 atmel_write_buf16(mtd, buf, len);
319 else
320 atmel_write_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800321}
322
David Brownell23a346c2008-07-03 23:40:16 -0700323/*
Josh Wu1c7b8742012-06-29 17:47:55 +0800324 * Return number of ecc bytes per sector according to sector size and
325 * correction capability
326 *
327 * Following table shows what at91 PMECC supported:
328 * Correction Capability Sector_512_bytes Sector_1024_bytes
329 * ===================== ================ =================
330 * 2-bits 4-bytes 4-bytes
331 * 4-bits 7-bytes 7-bytes
332 * 8-bits 13-bytes 14-bytes
333 * 12-bits 20-bytes 21-bytes
334 * 24-bits 39-bytes 42-bytes
335 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500336static int pmecc_get_ecc_bytes(int cap, int sector_size)
Josh Wu1c7b8742012-06-29 17:47:55 +0800337{
338 int m = 12 + sector_size / 512;
339 return (m * cap + 7) / 8;
340}
341
Bill Pemberton06f25512012-11-19 13:23:07 -0500342static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800343 int oobsize, int ecc_len)
Josh Wu1c7b8742012-06-29 17:47:55 +0800344{
345 int i;
346
347 layout->eccbytes = ecc_len;
348
349 /* ECC will occupy the last ecc_len bytes continuously */
350 for (i = 0; i < ecc_len; i++)
351 layout->eccpos[i] = oobsize - ecc_len + i;
352
353 layout->oobfree[0].offset = 2;
354 layout->oobfree[0].length =
355 oobsize - ecc_len - layout->oobfree[0].offset;
356}
357
Bill Pemberton06f25512012-11-19 13:23:07 -0500358static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800359{
360 int table_size;
361
362 table_size = host->pmecc_sector_size == 512 ?
363 PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
364
365 return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
366 table_size * sizeof(int16_t);
367}
368
369static void pmecc_data_free(struct atmel_nand_host *host)
370{
371 kfree(host->pmecc_partial_syn);
372 kfree(host->pmecc_si);
373 kfree(host->pmecc_lmu);
374 kfree(host->pmecc_smu);
375 kfree(host->pmecc_mu);
376 kfree(host->pmecc_dmu);
377 kfree(host->pmecc_delta);
378}
379
Bill Pemberton06f25512012-11-19 13:23:07 -0500380static int pmecc_data_alloc(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800381{
382 const int cap = host->pmecc_corr_cap;
383
384 host->pmecc_partial_syn = kzalloc((2 * cap + 1) * sizeof(int16_t),
385 GFP_KERNEL);
386 host->pmecc_si = kzalloc((2 * cap + 1) * sizeof(int16_t), GFP_KERNEL);
387 host->pmecc_lmu = kzalloc((cap + 1) * sizeof(int16_t), GFP_KERNEL);
388 host->pmecc_smu = kzalloc((cap + 2) * (2 * cap + 1) * sizeof(int16_t),
389 GFP_KERNEL);
390 host->pmecc_mu = kzalloc((cap + 1) * sizeof(int), GFP_KERNEL);
391 host->pmecc_dmu = kzalloc((cap + 1) * sizeof(int), GFP_KERNEL);
392 host->pmecc_delta = kzalloc((cap + 1) * sizeof(int), GFP_KERNEL);
393
394 if (host->pmecc_partial_syn &&
395 host->pmecc_si &&
396 host->pmecc_lmu &&
397 host->pmecc_smu &&
398 host->pmecc_mu &&
399 host->pmecc_dmu &&
400 host->pmecc_delta)
401 return 0;
402
403 /* error happened */
404 pmecc_data_free(host);
405 return -ENOMEM;
406}
407
408static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
409{
410 struct nand_chip *nand_chip = mtd->priv;
411 struct atmel_nand_host *host = nand_chip->priv;
412 int i;
413 uint32_t value;
414
415 /* Fill odd syndromes */
416 for (i = 0; i < host->pmecc_corr_cap; i++) {
417 value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
418 if (i & 1)
419 value >>= 16;
420 value &= 0xffff;
421 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
422 }
423}
424
425static void pmecc_substitute(struct mtd_info *mtd)
426{
427 struct nand_chip *nand_chip = mtd->priv;
428 struct atmel_nand_host *host = nand_chip->priv;
429 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
430 int16_t __iomem *index_of = host->pmecc_index_of;
431 int16_t *partial_syn = host->pmecc_partial_syn;
432 const int cap = host->pmecc_corr_cap;
433 int16_t *si;
434 int i, j;
435
436 /* si[] is a table that holds the current syndrome value,
437 * an element of that table belongs to the field
438 */
439 si = host->pmecc_si;
440
441 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
442
443 /* Computation 2t syndromes based on S(x) */
444 /* Odd syndromes */
445 for (i = 1; i < 2 * cap; i += 2) {
446 for (j = 0; j < host->pmecc_degree; j++) {
447 if (partial_syn[i] & ((unsigned short)0x1 << j))
448 si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
449 }
450 }
451 /* Even syndrome = (Odd syndrome) ** 2 */
452 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
453 if (si[j] == 0) {
454 si[i] = 0;
455 } else {
456 int16_t tmp;
457
458 tmp = readw_relaxed(index_of + si[j]);
459 tmp = (tmp * 2) % host->pmecc_cw_len;
460 si[i] = readw_relaxed(alpha_to + tmp);
461 }
462 }
463
464 return;
465}
466
467static void pmecc_get_sigma(struct mtd_info *mtd)
468{
469 struct nand_chip *nand_chip = mtd->priv;
470 struct atmel_nand_host *host = nand_chip->priv;
471
472 int16_t *lmu = host->pmecc_lmu;
473 int16_t *si = host->pmecc_si;
474 int *mu = host->pmecc_mu;
475 int *dmu = host->pmecc_dmu; /* Discrepancy */
476 int *delta = host->pmecc_delta; /* Delta order */
477 int cw_len = host->pmecc_cw_len;
478 const int16_t cap = host->pmecc_corr_cap;
479 const int num = 2 * cap + 1;
480 int16_t __iomem *index_of = host->pmecc_index_of;
481 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
482 int i, j, k;
483 uint32_t dmu_0_count, tmp;
484 int16_t *smu = host->pmecc_smu;
485
486 /* index of largest delta */
487 int ro;
488 int largest;
489 int diff;
490
491 dmu_0_count = 0;
492
493 /* First Row */
494
495 /* Mu */
496 mu[0] = -1;
497
498 memset(smu, 0, sizeof(int16_t) * num);
499 smu[0] = 1;
500
501 /* discrepancy set to 1 */
502 dmu[0] = 1;
503 /* polynom order set to 0 */
504 lmu[0] = 0;
505 delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
506
507 /* Second Row */
508
509 /* Mu */
510 mu[1] = 0;
511 /* Sigma(x) set to 1 */
512 memset(&smu[num], 0, sizeof(int16_t) * num);
513 smu[num] = 1;
514
515 /* discrepancy set to S1 */
516 dmu[1] = si[1];
517
518 /* polynom order set to 0 */
519 lmu[1] = 0;
520
521 delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
522
523 /* Init the Sigma(x) last row */
524 memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
525
526 for (i = 1; i <= cap; i++) {
527 mu[i + 1] = i << 1;
528 /* Begin Computing Sigma (Mu+1) and L(mu) */
529 /* check if discrepancy is set to 0 */
530 if (dmu[i] == 0) {
531 dmu_0_count++;
532
533 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
534 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
535 tmp += 2;
536 else
537 tmp += 1;
538
539 if (dmu_0_count == tmp) {
540 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
541 smu[(cap + 1) * num + j] =
542 smu[i * num + j];
543
544 lmu[cap + 1] = lmu[i];
545 return;
546 }
547
548 /* copy polynom */
549 for (j = 0; j <= lmu[i] >> 1; j++)
550 smu[(i + 1) * num + j] = smu[i * num + j];
551
552 /* copy previous polynom order to the next */
553 lmu[i + 1] = lmu[i];
554 } else {
555 ro = 0;
556 largest = -1;
557 /* find largest delta with dmu != 0 */
558 for (j = 0; j < i; j++) {
559 if ((dmu[j]) && (delta[j] > largest)) {
560 largest = delta[j];
561 ro = j;
562 }
563 }
564
565 /* compute difference */
566 diff = (mu[i] - mu[ro]);
567
568 /* Compute degree of the new smu polynomial */
569 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
570 lmu[i + 1] = lmu[i];
571 else
572 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
573
574 /* Init smu[i+1] with 0 */
575 for (k = 0; k < num; k++)
576 smu[(i + 1) * num + k] = 0;
577
578 /* Compute smu[i+1] */
579 for (k = 0; k <= lmu[ro] >> 1; k++) {
580 int16_t a, b, c;
581
582 if (!(smu[ro * num + k] && dmu[i]))
583 continue;
584 a = readw_relaxed(index_of + dmu[i]);
585 b = readw_relaxed(index_of + dmu[ro]);
586 c = readw_relaxed(index_of + smu[ro * num + k]);
587 tmp = a + (cw_len - b) + c;
588 a = readw_relaxed(alpha_to + tmp % cw_len);
589 smu[(i + 1) * num + (k + diff)] = a;
590 }
591
592 for (k = 0; k <= lmu[i] >> 1; k++)
593 smu[(i + 1) * num + k] ^= smu[i * num + k];
594 }
595
596 /* End Computing Sigma (Mu+1) and L(mu) */
597 /* In either case compute delta */
598 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
599
600 /* Do not compute discrepancy for the last iteration */
601 if (i >= cap)
602 continue;
603
604 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
605 tmp = 2 * (i - 1);
606 if (k == 0) {
607 dmu[i + 1] = si[tmp + 3];
608 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
609 int16_t a, b, c;
610 a = readw_relaxed(index_of +
611 smu[(i + 1) * num + k]);
612 b = si[2 * (i - 1) + 3 - k];
613 c = readw_relaxed(index_of + b);
614 tmp = a + c;
615 tmp %= cw_len;
616 dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
617 dmu[i + 1];
618 }
619 }
620 }
621
622 return;
623}
624
625static int pmecc_err_location(struct mtd_info *mtd)
626{
627 struct nand_chip *nand_chip = mtd->priv;
628 struct atmel_nand_host *host = nand_chip->priv;
629 unsigned long end_time;
630 const int cap = host->pmecc_corr_cap;
631 const int num = 2 * cap + 1;
632 int sector_size = host->pmecc_sector_size;
633 int err_nbr = 0; /* number of error */
634 int roots_nbr; /* number of roots */
635 int i;
636 uint32_t val;
637 int16_t *smu = host->pmecc_smu;
638
639 pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
640
641 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
642 pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
643 smu[(cap + 1) * num + i]);
644 err_nbr++;
645 }
646
647 val = (err_nbr - 1) << 16;
648 if (sector_size == 1024)
649 val |= 1;
650
651 pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
652 pmerrloc_writel(host->pmerrloc_base, ELEN,
653 sector_size * 8 + host->pmecc_degree * cap);
654
655 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
656 while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
657 & PMERRLOC_CALC_DONE)) {
658 if (unlikely(time_after(jiffies, end_time))) {
659 dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
660 return -1;
661 }
662 cpu_relax();
663 }
664
665 roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
666 & PMERRLOC_ERR_NUM_MASK) >> 8;
667 /* Number of roots == degree of smu hence <= cap */
668 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
669 return err_nbr - 1;
670
671 /* Number of roots does not match the degree of smu
672 * unable to correct error */
673 return -1;
674}
675
676static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
677 int sector_num, int extra_bytes, int err_nbr)
678{
679 struct nand_chip *nand_chip = mtd->priv;
680 struct atmel_nand_host *host = nand_chip->priv;
681 int i = 0;
682 int byte_pos, bit_pos, sector_size, pos;
683 uint32_t tmp;
684 uint8_t err_byte;
685
686 sector_size = host->pmecc_sector_size;
687
688 while (err_nbr) {
689 tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_base, i) - 1;
690 byte_pos = tmp / 8;
691 bit_pos = tmp % 8;
692
693 if (byte_pos >= (sector_size + extra_bytes))
694 BUG(); /* should never happen */
695
696 if (byte_pos < sector_size) {
697 err_byte = *(buf + byte_pos);
698 *(buf + byte_pos) ^= (1 << bit_pos);
699
700 pos = sector_num * host->pmecc_sector_size + byte_pos;
701 dev_info(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
702 pos, bit_pos, err_byte, *(buf + byte_pos));
703 } else {
704 /* Bit flip in OOB area */
705 tmp = sector_num * host->pmecc_bytes_per_sector
706 + (byte_pos - sector_size);
707 err_byte = ecc[tmp];
708 ecc[tmp] ^= (1 << bit_pos);
709
710 pos = tmp + nand_chip->ecc.layout->eccpos[0];
711 dev_info(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
712 pos, bit_pos, err_byte, ecc[tmp]);
713 }
714
715 i++;
716 err_nbr--;
717 }
718
719 return;
720}
721
722static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
723 u8 *ecc)
724{
725 struct nand_chip *nand_chip = mtd->priv;
726 struct atmel_nand_host *host = nand_chip->priv;
727 int i, err_nbr, eccbytes;
728 uint8_t *buf_pos;
Josh Wuc0c70d92012-11-27 18:50:31 +0800729 int total_err = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800730
731 eccbytes = nand_chip->ecc.bytes;
732 for (i = 0; i < eccbytes; i++)
733 if (ecc[i] != 0xff)
734 goto normal_check;
735 /* Erased page, return OK */
736 return 0;
737
738normal_check:
739 for (i = 0; i < host->pmecc_sector_number; i++) {
740 err_nbr = 0;
741 if (pmecc_stat & 0x1) {
742 buf_pos = buf + i * host->pmecc_sector_size;
743
744 pmecc_gen_syndrome(mtd, i);
745 pmecc_substitute(mtd);
746 pmecc_get_sigma(mtd);
747
748 err_nbr = pmecc_err_location(mtd);
749 if (err_nbr == -1) {
750 dev_err(host->dev, "PMECC: Too many errors\n");
751 mtd->ecc_stats.failed++;
752 return -EIO;
753 } else {
754 pmecc_correct_data(mtd, buf_pos, ecc, i,
755 host->pmecc_bytes_per_sector, err_nbr);
756 mtd->ecc_stats.corrected += err_nbr;
Josh Wuc0c70d92012-11-27 18:50:31 +0800757 total_err += err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800758 }
759 }
760 pmecc_stat >>= 1;
761 }
762
Josh Wuc0c70d92012-11-27 18:50:31 +0800763 return total_err;
Josh Wu1c7b8742012-06-29 17:47:55 +0800764}
765
766static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
767 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
768{
769 struct atmel_nand_host *host = chip->priv;
770 int eccsize = chip->ecc.size;
771 uint8_t *oob = chip->oob_poi;
772 uint32_t *eccpos = chip->ecc.layout->eccpos;
773 uint32_t stat;
774 unsigned long end_time;
Josh Wuc0c70d92012-11-27 18:50:31 +0800775 int bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800776
777 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
778 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
779 pmecc_writel(host->ecc, CFG, (pmecc_readl_relaxed(host->ecc, CFG)
780 & ~PMECC_CFG_WRITE_OP) | PMECC_CFG_AUTO_ENABLE);
781
782 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
783 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
784
785 chip->read_buf(mtd, buf, eccsize);
786 chip->read_buf(mtd, oob, mtd->oobsize);
787
788 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
789 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
790 if (unlikely(time_after(jiffies, end_time))) {
791 dev_err(host->dev, "PMECC: Timeout to get error status.\n");
792 return -EIO;
793 }
794 cpu_relax();
795 }
796
797 stat = pmecc_readl_relaxed(host->ecc, ISR);
Josh Wuc0c70d92012-11-27 18:50:31 +0800798 if (stat != 0) {
799 bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]);
800 if (bitflips < 0)
801 /* uncorrectable errors */
802 return 0;
803 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800804
Josh Wuc0c70d92012-11-27 18:50:31 +0800805 return bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800806}
807
808static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
809 struct nand_chip *chip, const uint8_t *buf, int oob_required)
810{
811 struct atmel_nand_host *host = chip->priv;
812 uint32_t *eccpos = chip->ecc.layout->eccpos;
813 int i, j;
814 unsigned long end_time;
815
816 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
817 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
818
819 pmecc_writel(host->ecc, CFG, (pmecc_readl_relaxed(host->ecc, CFG) |
820 PMECC_CFG_WRITE_OP) & ~PMECC_CFG_AUTO_ENABLE);
821
822 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
823 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
824
825 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
826
827 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
828 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
829 if (unlikely(time_after(jiffies, end_time))) {
830 dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
831 return -EIO;
832 }
833 cpu_relax();
834 }
835
836 for (i = 0; i < host->pmecc_sector_number; i++) {
837 for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
838 int pos;
839
840 pos = i * host->pmecc_bytes_per_sector + j;
841 chip->oob_poi[eccpos[pos]] =
842 pmecc_readb_ecc_relaxed(host->ecc, i, j);
843 }
844 }
845 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
846
847 return 0;
848}
849
850static void atmel_pmecc_core_init(struct mtd_info *mtd)
851{
852 struct nand_chip *nand_chip = mtd->priv;
853 struct atmel_nand_host *host = nand_chip->priv;
854 uint32_t val = 0;
855 struct nand_ecclayout *ecc_layout;
856
857 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
858 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
859
860 switch (host->pmecc_corr_cap) {
861 case 2:
862 val = PMECC_CFG_BCH_ERR2;
863 break;
864 case 4:
865 val = PMECC_CFG_BCH_ERR4;
866 break;
867 case 8:
868 val = PMECC_CFG_BCH_ERR8;
869 break;
870 case 12:
871 val = PMECC_CFG_BCH_ERR12;
872 break;
873 case 24:
874 val = PMECC_CFG_BCH_ERR24;
875 break;
876 }
877
878 if (host->pmecc_sector_size == 512)
879 val |= PMECC_CFG_SECTOR512;
880 else if (host->pmecc_sector_size == 1024)
881 val |= PMECC_CFG_SECTOR1024;
882
883 switch (host->pmecc_sector_number) {
884 case 1:
885 val |= PMECC_CFG_PAGE_1SECTOR;
886 break;
887 case 2:
888 val |= PMECC_CFG_PAGE_2SECTORS;
889 break;
890 case 4:
891 val |= PMECC_CFG_PAGE_4SECTORS;
892 break;
893 case 8:
894 val |= PMECC_CFG_PAGE_8SECTORS;
895 break;
896 }
897
898 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
899 | PMECC_CFG_AUTO_DISABLE);
900 pmecc_writel(host->ecc, CFG, val);
901
902 ecc_layout = nand_chip->ecc.layout;
903 pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
904 pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
905 pmecc_writel(host->ecc, EADDR,
906 ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
907 /* See datasheet about PMECC Clock Control Register */
908 pmecc_writel(host->ecc, CLK, 2);
909 pmecc_writel(host->ecc, IDR, 0xff);
910 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
911}
912
913static int __init atmel_pmecc_nand_init_params(struct platform_device *pdev,
914 struct atmel_nand_host *host)
915{
916 struct mtd_info *mtd = &host->mtd;
917 struct nand_chip *nand_chip = &host->nand_chip;
918 struct resource *regs, *regs_pmerr, *regs_rom;
919 int cap, sector_size, err_no;
920
Josh Wue66b4312013-01-23 20:47:11 +0800921 if (host->pmecc_corr_cap == 0 || host->pmecc_sector_size == 0)
922 /* TODO: Should use ONFI ecc parameters. */
923 return -EINVAL;
924
Josh Wu1c7b8742012-06-29 17:47:55 +0800925 cap = host->pmecc_corr_cap;
926 sector_size = host->pmecc_sector_size;
Josh Wue66b4312013-01-23 20:47:11 +0800927 host->pmecc_lookup_table_offset = (sector_size == 512) ?
928 host->pmecc_lookup_table_offset_512 :
929 host->pmecc_lookup_table_offset_1024;
930
Josh Wu1c7b8742012-06-29 17:47:55 +0800931 dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
932 cap, sector_size);
933
934 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
935 if (!regs) {
936 dev_warn(host->dev,
937 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
938 nand_chip->ecc.mode = NAND_ECC_SOFT;
939 return 0;
940 }
941
942 host->ecc = ioremap(regs->start, resource_size(regs));
943 if (host->ecc == NULL) {
944 dev_err(host->dev, "ioremap failed\n");
945 err_no = -EIO;
946 goto err_pmecc_ioremap;
947 }
948
949 regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
950 regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
951 if (regs_pmerr && regs_rom) {
952 host->pmerrloc_base = ioremap(regs_pmerr->start,
953 resource_size(regs_pmerr));
954 host->pmecc_rom_base = ioremap(regs_rom->start,
955 resource_size(regs_rom));
956 }
957
958 if (!host->pmerrloc_base || !host->pmecc_rom_base) {
959 dev_err(host->dev,
960 "Can not get I/O resource for PMECC ERRLOC controller or ROM!\n");
961 err_no = -EIO;
962 goto err_pmloc_ioremap;
963 }
964
965 /* ECC is calculated for the whole page (1 step) */
966 nand_chip->ecc.size = mtd->writesize;
967
968 /* set ECC page size and oob layout */
969 switch (mtd->writesize) {
970 case 2048:
971 host->pmecc_degree = PMECC_GF_DIMENSION_13;
972 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
973 host->pmecc_sector_number = mtd->writesize / sector_size;
974 host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
975 cap, sector_size);
976 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
977 host->pmecc_index_of = host->pmecc_rom_base +
978 host->pmecc_lookup_table_offset;
979
980 nand_chip->ecc.steps = 1;
981 nand_chip->ecc.strength = cap;
982 nand_chip->ecc.bytes = host->pmecc_bytes_per_sector *
983 host->pmecc_sector_number;
984 if (nand_chip->ecc.bytes > mtd->oobsize - 2) {
985 dev_err(host->dev, "No room for ECC bytes\n");
986 err_no = -EINVAL;
987 goto err_no_ecc_room;
988 }
989 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
990 mtd->oobsize,
991 nand_chip->ecc.bytes);
992 nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
993 break;
994 case 512:
995 case 1024:
996 case 4096:
997 /* TODO */
998 dev_warn(host->dev,
999 "Unsupported page size for PMECC, use Software ECC\n");
1000 default:
1001 /* page size not handled by HW ECC */
1002 /* switching back to soft ECC */
1003 nand_chip->ecc.mode = NAND_ECC_SOFT;
1004 return 0;
1005 }
1006
1007 /* Allocate data for PMECC computation */
1008 err_no = pmecc_data_alloc(host);
1009 if (err_no) {
1010 dev_err(host->dev,
1011 "Cannot allocate memory for PMECC computation!\n");
1012 goto err_pmecc_data_alloc;
1013 }
1014
1015 nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
1016 nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
1017
1018 atmel_pmecc_core_init(mtd);
1019
1020 return 0;
1021
1022err_pmecc_data_alloc:
1023err_no_ecc_room:
1024err_pmloc_ioremap:
1025 iounmap(host->ecc);
1026 if (host->pmerrloc_base)
1027 iounmap(host->pmerrloc_base);
1028 if (host->pmecc_rom_base)
1029 iounmap(host->pmecc_rom_base);
1030err_pmecc_ioremap:
1031 return err_no;
1032}
1033
1034/*
Richard Genoud77f54922008-04-23 19:51:14 +02001035 * Calculate HW ECC
1036 *
1037 * function called after a write
1038 *
1039 * mtd: MTD block structure
1040 * dat: raw data (unused)
1041 * ecc_code: buffer for ECC
1042 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001043static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +02001044 const u_char *dat, unsigned char *ecc_code)
1045{
1046 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001047 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001048 unsigned int ecc_value;
1049
1050 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +02001051 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +02001052
Richard Genoud3fc23892008-10-12 08:42:28 +02001053 ecc_code[0] = ecc_value & 0xFF;
1054 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001055
1056 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001057 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +02001058
Richard Genoud3fc23892008-10-12 08:42:28 +02001059 ecc_code[2] = ecc_value & 0xFF;
1060 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001061
1062 return 0;
1063}
1064
1065/*
1066 * HW ECC read page function
1067 *
1068 * mtd: mtd info structure
1069 * chip: nand chip info structure
1070 * buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001071 * oob_required: caller expects OOB data read to chip->oob_poi
Richard Genoud77f54922008-04-23 19:51:14 +02001072 */
Brian Norris1fbb9382012-05-02 10:14:55 -07001073static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1074 uint8_t *buf, int oob_required, int page)
Richard Genoud77f54922008-04-23 19:51:14 +02001075{
1076 int eccsize = chip->ecc.size;
1077 int eccbytes = chip->ecc.bytes;
1078 uint32_t *eccpos = chip->ecc.layout->eccpos;
1079 uint8_t *p = buf;
1080 uint8_t *oob = chip->oob_poi;
1081 uint8_t *ecc_pos;
1082 int stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001083 unsigned int max_bitflips = 0;
Richard Genoud77f54922008-04-23 19:51:14 +02001084
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001085 /*
1086 * Errata: ALE is incorrectly wired up to the ECC controller
1087 * on the AP7000, so it will include the address cycles in the
1088 * ECC calculation.
1089 *
1090 * Workaround: Reset the parity registers before reading the
1091 * actual data.
1092 */
1093 if (cpu_is_at32ap7000()) {
1094 struct atmel_nand_host *host = chip->priv;
1095 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
1096 }
1097
Richard Genoud77f54922008-04-23 19:51:14 +02001098 /* read the page */
1099 chip->read_buf(mtd, p, eccsize);
1100
1101 /* move to ECC position if needed */
1102 if (eccpos[0] != 0) {
1103 /* This only works on large pages
1104 * because the ECC controller waits for
1105 * NAND_CMD_RNDOUTSTART after the
1106 * NAND_CMD_RNDOUT.
1107 * anyway, for small pages, the eccpos[0] == 0
1108 */
1109 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1110 mtd->writesize + eccpos[0], -1);
1111 }
1112
1113 /* the ECC controller needs to read the ECC just after the data */
1114 ecc_pos = oob + eccpos[0];
1115 chip->read_buf(mtd, ecc_pos, eccbytes);
1116
1117 /* check if there's an error */
1118 stat = chip->ecc.correct(mtd, p, oob, NULL);
1119
Mike Dunn3f91e942012-04-25 12:06:09 -07001120 if (stat < 0) {
Richard Genoud77f54922008-04-23 19:51:14 +02001121 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001122 } else {
Richard Genoud77f54922008-04-23 19:51:14 +02001123 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001124 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1125 }
Richard Genoud77f54922008-04-23 19:51:14 +02001126
1127 /* get back to oob start (end of page) */
1128 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1129
1130 /* read the oob */
1131 chip->read_buf(mtd, oob, mtd->oobsize);
1132
Mike Dunn3f91e942012-04-25 12:06:09 -07001133 return max_bitflips;
Richard Genoud77f54922008-04-23 19:51:14 +02001134}
1135
1136/*
1137 * HW ECC Correction
1138 *
1139 * function called after a read
1140 *
1141 * mtd: MTD block structure
1142 * dat: raw data read from the chip
1143 * read_ecc: ECC from the chip (unused)
1144 * isnull: unused
1145 *
1146 * Detect and correct a 1 bit error for a page
1147 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001148static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +02001149 u_char *read_ecc, u_char *isnull)
1150{
1151 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001152 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001153 unsigned int ecc_status;
1154 unsigned int ecc_word, ecc_bit;
1155
1156 /* get the status from the Status Register */
1157 ecc_status = ecc_readl(host->ecc, SR);
1158
1159 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001160 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +02001161 return 0;
1162
1163 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001164 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001165 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001166 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001167 ecc_word >>= 4;
1168
1169 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001170 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001171 /* check if it is a freshly erased block
1172 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001173 if ((ecc_bit == ATMEL_ECC_BITADDR)
1174 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +02001175 /* the block has just been erased, return OK */
1176 return 0;
1177 }
1178 /* it doesn't seems to be a freshly
1179 * erased block.
1180 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001181 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +02001182 " Unable to correct.\n");
1183 return -EIO;
1184 }
1185
1186 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001187 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001188 /* there's nothing much to do here.
1189 * the bit error is on the ECC itself.
1190 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001191 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +02001192 " Nothing to correct\n");
1193 return 0;
1194 }
1195
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001196 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +02001197 " (word offset in the page :"
1198 " 0x%x bit offset : 0x%x)\n",
1199 ecc_word, ecc_bit);
1200 /* correct the error */
1201 if (nand_chip->options & NAND_BUSWIDTH_16) {
1202 /* 16 bits words */
1203 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1204 } else {
1205 /* 8 bits words */
1206 dat[ecc_word] ^= (1 << ecc_bit);
1207 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001208 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +02001209 return 1;
1210}
1211
1212/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001213 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +02001214 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001215static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1216{
1217 if (cpu_is_at32ap7000()) {
1218 struct nand_chip *nand_chip = mtd->priv;
1219 struct atmel_nand_host *host = nand_chip->priv;
1220 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
1221 }
1222}
Richard Genoud77f54922008-04-23 19:51:14 +02001223
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001224#if defined(CONFIG_OF)
Bill Pemberton06f25512012-11-19 13:23:07 -05001225static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001226 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001227{
Josh Wuc0cf7872013-01-23 20:47:08 +08001228 u32 val;
Josh Wua41b51a2012-06-29 17:47:54 +08001229 u32 offset[2];
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001230 int ecc_mode;
1231 struct atmel_nand_data *board = &host->board;
1232 enum of_gpio_flags flags;
1233
1234 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
1235 if (val >= 32) {
1236 dev_err(host->dev, "invalid addr-offset %u\n", val);
1237 return -EINVAL;
1238 }
1239 board->ale = val;
1240 }
1241
1242 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
1243 if (val >= 32) {
1244 dev_err(host->dev, "invalid cmd-offset %u\n", val);
1245 return -EINVAL;
1246 }
1247 board->cle = val;
1248 }
1249
1250 ecc_mode = of_get_nand_ecc_mode(np);
1251
1252 board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
1253
1254 board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
1255
1256 if (of_get_nand_bus_width(np) == 16)
1257 board->bus_width_16 = 1;
1258
1259 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
1260 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
1261
1262 board->enable_pin = of_get_gpio(np, 1);
1263 board->det_pin = of_get_gpio(np, 2);
1264
Josh Wua41b51a2012-06-29 17:47:54 +08001265 host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
1266
1267 if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
1268 return 0; /* Not using PMECC */
1269
1270 /* use PMECC, get correction capability, sector size and lookup
1271 * table offset.
Josh Wue66b4312013-01-23 20:47:11 +08001272 * If correction bits and sector size are not specified, then find
1273 * them from NAND ONFI parameters.
Josh Wua41b51a2012-06-29 17:47:54 +08001274 */
Josh Wue66b4312013-01-23 20:47:11 +08001275 if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
1276 if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
1277 (val != 24)) {
1278 dev_err(host->dev,
1279 "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
1280 val);
1281 return -EINVAL;
1282 }
1283 host->pmecc_corr_cap = (u8)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001284 }
Josh Wua41b51a2012-06-29 17:47:54 +08001285
Josh Wue66b4312013-01-23 20:47:11 +08001286 if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
1287 if ((val != 512) && (val != 1024)) {
1288 dev_err(host->dev,
1289 "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
1290 val);
1291 return -EINVAL;
1292 }
1293 host->pmecc_sector_size = (u16)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001294 }
Josh Wua41b51a2012-06-29 17:47:54 +08001295
1296 if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
1297 offset, 2) != 0) {
1298 dev_err(host->dev, "Cannot get PMECC lookup table offset\n");
1299 return -EINVAL;
1300 }
Josh Wuc0cf7872013-01-23 20:47:08 +08001301 if (!offset[0] && !offset[1]) {
Josh Wua41b51a2012-06-29 17:47:54 +08001302 dev_err(host->dev, "Invalid PMECC lookup table offset\n");
1303 return -EINVAL;
1304 }
Josh Wue66b4312013-01-23 20:47:11 +08001305 host->pmecc_lookup_table_offset_512 = offset[0];
1306 host->pmecc_lookup_table_offset_1024 = offset[1];
Josh Wua41b51a2012-06-29 17:47:54 +08001307
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001308 return 0;
1309}
1310#else
Bill Pemberton06f25512012-11-19 13:23:07 -05001311static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001312 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001313{
1314 return -EINVAL;
1315}
1316#endif
1317
Josh Wu3dfe41a2012-06-25 18:07:43 +08001318static int __init atmel_hw_nand_init_params(struct platform_device *pdev,
1319 struct atmel_nand_host *host)
1320{
1321 struct mtd_info *mtd = &host->mtd;
1322 struct nand_chip *nand_chip = &host->nand_chip;
1323 struct resource *regs;
1324
1325 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1326 if (!regs) {
1327 dev_err(host->dev,
1328 "Can't get I/O resource regs, use software ECC\n");
1329 nand_chip->ecc.mode = NAND_ECC_SOFT;
1330 return 0;
1331 }
1332
1333 host->ecc = ioremap(regs->start, resource_size(regs));
1334 if (host->ecc == NULL) {
1335 dev_err(host->dev, "ioremap failed\n");
1336 return -EIO;
1337 }
1338
1339 /* ECC is calculated for the whole page (1 step) */
1340 nand_chip->ecc.size = mtd->writesize;
1341
1342 /* set ECC page size and oob layout */
1343 switch (mtd->writesize) {
1344 case 512:
1345 nand_chip->ecc.layout = &atmel_oobinfo_small;
1346 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
1347 break;
1348 case 1024:
1349 nand_chip->ecc.layout = &atmel_oobinfo_large;
1350 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
1351 break;
1352 case 2048:
1353 nand_chip->ecc.layout = &atmel_oobinfo_large;
1354 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
1355 break;
1356 case 4096:
1357 nand_chip->ecc.layout = &atmel_oobinfo_large;
1358 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
1359 break;
1360 default:
1361 /* page size not handled by HW ECC */
1362 /* switching back to soft ECC */
1363 nand_chip->ecc.mode = NAND_ECC_SOFT;
1364 return 0;
1365 }
1366
1367 /* set up for HW ECC */
1368 nand_chip->ecc.calculate = atmel_nand_calculate;
1369 nand_chip->ecc.correct = atmel_nand_correct;
1370 nand_chip->ecc.hwctl = atmel_nand_hwctl;
1371 nand_chip->ecc.read_page = atmel_nand_read_page;
1372 nand_chip->ecc.bytes = 4;
1373 nand_chip->ecc.strength = 1;
1374
1375 return 0;
1376}
1377
Andrew Victor42cb1402006-10-19 18:24:35 +02001378/*
1379 * Probe for the NAND device.
1380 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001381static int __init atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02001382{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001383 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +02001384 struct mtd_info *mtd;
1385 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02001386 struct resource *mem;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001387 struct mtd_part_parser_data ppdata = {};
Andrew Victor42cb1402006-10-19 18:24:35 +02001388 int res;
Jean-Christophe PLAGNIOL-VILLARD251e7832012-07-12 23:31:39 +08001389 struct pinctrl *pinctrl;
Andrew Victor42cb1402006-10-19 18:24:35 +02001390
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001391 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1392 if (!mem) {
1393 printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
1394 return -ENXIO;
1395 }
1396
Andrew Victor42cb1402006-10-19 18:24:35 +02001397 /* Allocate memory for the device structure (and zero it) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001398 host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
Andrew Victor42cb1402006-10-19 18:24:35 +02001399 if (!host) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001400 printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +02001401 return -ENOMEM;
1402 }
1403
Hong Xucbc6c5e2011-01-18 14:36:05 +08001404 host->io_phys = (dma_addr_t)mem->start;
1405
Joe Perches28f65c112011-06-09 09:13:32 -07001406 host->io_base = ioremap(mem->start, resource_size(mem));
Andrew Victor42cb1402006-10-19 18:24:35 +02001407 if (host->io_base == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001408 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001409 res = -EIO;
1410 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +02001411 }
1412
1413 mtd = &host->mtd;
1414 nand_chip = &host->nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02001415 host->dev = &pdev->dev;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001416 if (pdev->dev.of_node) {
1417 res = atmel_of_init_port(host, pdev->dev.of_node);
1418 if (res)
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001419 goto err_ecc_ioremap;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001420 } else {
1421 memcpy(&host->board, pdev->dev.platform_data,
1422 sizeof(struct atmel_nand_data));
1423 }
Andrew Victor42cb1402006-10-19 18:24:35 +02001424
1425 nand_chip->priv = host; /* link the private data structures */
1426 mtd->priv = nand_chip;
1427 mtd->owner = THIS_MODULE;
1428
1429 /* Set address of NAND IO lines */
1430 nand_chip->IO_ADDR_R = host->io_base;
1431 nand_chip->IO_ADDR_W = host->io_base;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001432 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Ivan Kutena4265f82007-05-24 14:35:58 +03001433
Jean-Christophe PLAGNIOL-VILLARD251e7832012-07-12 23:31:39 +08001434 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1435 if (IS_ERR(pinctrl)) {
1436 dev_err(host->dev, "Failed to request pinctrl\n");
1437 res = PTR_ERR(pinctrl);
1438 goto err_ecc_ioremap;
1439 }
1440
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001441 if (gpio_is_valid(host->board.rdy_pin)) {
1442 res = gpio_request(host->board.rdy_pin, "nand_rdy");
1443 if (res < 0) {
1444 dev_err(&pdev->dev,
1445 "can't request rdy gpio %d\n",
1446 host->board.rdy_pin);
1447 goto err_ecc_ioremap;
1448 }
1449
1450 res = gpio_direction_input(host->board.rdy_pin);
1451 if (res < 0) {
1452 dev_err(&pdev->dev,
1453 "can't request input direction rdy gpio %d\n",
1454 host->board.rdy_pin);
1455 goto err_ecc_ioremap;
1456 }
1457
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001458 nand_chip->dev_ready = atmel_nand_device_ready;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001459 }
1460
1461 if (gpio_is_valid(host->board.enable_pin)) {
1462 res = gpio_request(host->board.enable_pin, "nand_enable");
1463 if (res < 0) {
1464 dev_err(&pdev->dev,
1465 "can't request enable gpio %d\n",
1466 host->board.enable_pin);
1467 goto err_ecc_ioremap;
1468 }
1469
1470 res = gpio_direction_output(host->board.enable_pin, 1);
1471 if (res < 0) {
1472 dev_err(&pdev->dev,
1473 "can't request output direction enable gpio %d\n",
1474 host->board.enable_pin);
1475 goto err_ecc_ioremap;
1476 }
1477 }
Ivan Kutena4265f82007-05-24 14:35:58 +03001478
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001479 nand_chip->ecc.mode = host->board.ecc_mode;
Andrew Victor42cb1402006-10-19 18:24:35 +02001480 nand_chip->chip_delay = 20; /* 20us command delay time */
1481
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001482 if (host->board.bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +02001483 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +08001484
1485 nand_chip->read_buf = atmel_read_buf;
1486 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +02001487
Andrew Victor42cb1402006-10-19 18:24:35 +02001488 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001489 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02001490
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001491 if (gpio_is_valid(host->board.det_pin)) {
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001492 res = gpio_request(host->board.det_pin, "nand_det");
1493 if (res < 0) {
1494 dev_err(&pdev->dev,
1495 "can't request det gpio %d\n",
1496 host->board.det_pin);
1497 goto err_no_card;
1498 }
1499
1500 res = gpio_direction_input(host->board.det_pin);
1501 if (res < 0) {
1502 dev_err(&pdev->dev,
1503 "can't request input direction det gpio %d\n",
1504 host->board.det_pin);
1505 goto err_no_card;
1506 }
1507
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001508 if (gpio_get_value(host->board.det_pin)) {
Simon Polettef4fa697c2009-05-27 18:19:39 +03001509 printk(KERN_INFO "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +01001510 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001511 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +02001512 }
1513 }
1514
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001515 if (host->board.on_flash_bbt || on_flash_bbt) {
Simon Polettef4fa697c2009-05-27 18:19:39 +03001516 printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
Brian Norrisbb9ebd4e2011-05-31 16:31:23 -07001517 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
Simon Polettef4fa697c2009-05-27 18:19:39 +03001518 }
1519
Hong Xucb457a42011-03-30 16:26:41 +08001520 if (!cpu_has_dma())
1521 use_dma = 0;
1522
1523 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +08001524 dma_cap_mask_t mask;
1525
1526 dma_cap_zero(mask);
1527 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab5362011-06-29 18:41:16 +02001528 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +08001529 if (!host->dma_chan) {
1530 dev_err(host->dev, "Failed to request DMA channel\n");
1531 use_dma = 0;
1532 }
1533 }
1534 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +08001535 dev_info(host->dev, "Using %s for DMA transfers.\n",
1536 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +08001537 else
1538 dev_info(host->dev, "No DMA support for NAND access.\n");
1539
Richard Genoud77f54922008-04-23 19:51:14 +02001540 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +00001541 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +02001542 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001543 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +02001544 }
1545
Richard Genoud3fc23892008-10-12 08:42:28 +02001546 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001547 if (host->has_pmecc)
1548 res = atmel_pmecc_nand_init_params(pdev, host);
1549 else
1550 res = atmel_hw_nand_init_params(pdev, host);
1551
Josh Wu3dfe41a2012-06-25 18:07:43 +08001552 if (res != 0)
1553 goto err_hw_ecc;
Richard Genoud77f54922008-04-23 19:51:14 +02001554 }
1555
1556 /* second phase scan */
1557 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +02001558 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001559 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +02001560 }
1561
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001562 mtd->name = "atmel_nand";
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001563 ppdata.of_node = pdev->dev.of_node;
1564 res = mtd_device_parse_register(mtd, NULL, &ppdata,
1565 host->board.parts, host->board.num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +02001566 if (!res)
1567 return res;
1568
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001569err_scan_tail:
Josh Wu1c7b8742012-06-29 17:47:55 +08001570 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
1571 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
1572 pmecc_data_free(host);
1573 }
Josh Wu3dfe41a2012-06-25 18:07:43 +08001574 if (host->ecc)
1575 iounmap(host->ecc);
Josh Wu1c7b8742012-06-29 17:47:55 +08001576 if (host->pmerrloc_base)
1577 iounmap(host->pmerrloc_base);
1578 if (host->pmecc_rom_base)
1579 iounmap(host->pmecc_rom_base);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001580err_hw_ecc:
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001581err_scan_ident:
1582err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001583 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02001584 platform_set_drvdata(pdev, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +08001585 if (host->dma_chan)
1586 dma_release_channel(host->dma_chan);
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001587err_ecc_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +02001588 iounmap(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001589err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +02001590 kfree(host);
1591 return res;
1592}
1593
1594/*
1595 * Remove a NAND device.
1596 */
David Brownell23a346c2008-07-03 23:40:16 -07001597static int __exit atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02001598{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001599 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +02001600 struct mtd_info *mtd = &host->mtd;
1601
1602 nand_release(mtd);
1603
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001604 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02001605
Josh Wu1c7b8742012-06-29 17:47:55 +08001606 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
1607 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
1608 pmerrloc_writel(host->pmerrloc_base, ELDIS,
1609 PMERRLOC_DISABLE);
1610 pmecc_data_free(host);
1611 }
1612
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001613 if (gpio_is_valid(host->board.det_pin))
1614 gpio_free(host->board.det_pin);
1615
1616 if (gpio_is_valid(host->board.enable_pin))
1617 gpio_free(host->board.enable_pin);
1618
1619 if (gpio_is_valid(host->board.rdy_pin))
1620 gpio_free(host->board.rdy_pin);
1621
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001622 if (host->ecc)
1623 iounmap(host->ecc);
Josh Wu1c7b8742012-06-29 17:47:55 +08001624 if (host->pmecc_rom_base)
1625 iounmap(host->pmecc_rom_base);
1626 if (host->pmerrloc_base)
1627 iounmap(host->pmerrloc_base);
Hong Xucbc6c5e2011-01-18 14:36:05 +08001628
1629 if (host->dma_chan)
1630 dma_release_channel(host->dma_chan);
1631
Andrew Victor42cb1402006-10-19 18:24:35 +02001632 iounmap(host->io_base);
1633 kfree(host);
1634
1635 return 0;
1636}
1637
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001638#if defined(CONFIG_OF)
1639static const struct of_device_id atmel_nand_dt_ids[] = {
1640 { .compatible = "atmel,at91rm9200-nand" },
1641 { /* sentinel */ }
1642};
1643
1644MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
1645#endif
1646
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001647static struct platform_driver atmel_nand_driver = {
David Brownell23a346c2008-07-03 23:40:16 -07001648 .remove = __exit_p(atmel_nand_remove),
Andrew Victor42cb1402006-10-19 18:24:35 +02001649 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001650 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +02001651 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001652 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
Andrew Victor42cb1402006-10-19 18:24:35 +02001653 },
1654};
1655
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001656static int __init atmel_nand_init(void)
Andrew Victor42cb1402006-10-19 18:24:35 +02001657{
David Brownell23a346c2008-07-03 23:40:16 -07001658 return platform_driver_probe(&atmel_nand_driver, atmel_nand_probe);
Andrew Victor42cb1402006-10-19 18:24:35 +02001659}
1660
1661
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001662static void __exit atmel_nand_exit(void)
Andrew Victor42cb1402006-10-19 18:24:35 +02001663{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001664 platform_driver_unregister(&atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +02001665}
1666
1667
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001668module_init(atmel_nand_init);
1669module_exit(atmel_nand_exit);
Andrew Victor42cb1402006-10-19 18:24:35 +02001670
1671MODULE_LICENSE("GPL");
1672MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +02001673MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001674MODULE_ALIAS("platform:atmel_nand");