blob: b97ad9f78d3971e1d117c4b797ac0b3d32ccf10e [file] [log] [blame]
Andrew Victor42cb1402006-10-19 18:24:35 +02001/*
Andrew Victor42cb1402006-10-19 18:24:35 +02002 * Copyright (C) 2003 Rick Bronson
3 *
4 * Derived from drivers/mtd/nand/autcpu12.c
5 * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
6 *
7 * Derived from drivers/mtd/spia.c
8 * Copyright (C) 2000 Steven J. Hill (sjhill@cotw.com)
9 *
Richard Genoud77f54922008-04-23 19:51:14 +020010 *
11 * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
12 * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright (C) 2007
13 *
14 * Derived from Das U-Boot source code
15 * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
16 * (C) Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
17 *
18 *
Andrew Victor42cb1402006-10-19 18:24:35 +020019 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 2 as
21 * published by the Free Software Foundation.
22 *
23 */
24
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000025#include <linux/dma-mapping.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020026#include <linux/slab.h>
27#include <linux/module.h>
Simon Polettef4fa6972009-05-27 18:19:39 +030028#include <linux/moduleparam.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020029#include <linux/platform_device.h>
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +080030#include <linux/of.h>
31#include <linux/of_device.h>
32#include <linux/of_gpio.h>
33#include <linux/of_mtd.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020034#include <linux/mtd/mtd.h>
35#include <linux/mtd/nand.h>
36#include <linux/mtd/partitions.h>
37
Hans-Christian Egtvedt5c39c4c2011-04-13 15:55:17 +020038#include <linux/dmaengine.h>
David Woodhouse90574d02008-06-07 08:49:00 +010039#include <linux/gpio.h>
40#include <linux/io.h>
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +080041#include <linux/platform_data/atmel.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020042
Russell Kinga09e64f2008-08-05 16:14:15 +010043#include <mach/cpu.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020044
Hong Xucbc6c5e2011-01-18 14:36:05 +080045static int use_dma = 1;
46module_param(use_dma, int, 0);
47
Simon Polettef4fa6972009-05-27 18:19:39 +030048static int on_flash_bbt = 0;
49module_param(on_flash_bbt, int, 0);
50
Richard Genoud77f54922008-04-23 19:51:14 +020051/* Register access macros */
52#define ecc_readl(add, reg) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020053 __raw_readl(add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020054#define ecc_writel(add, reg, value) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020055 __raw_writel((value), add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020056
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020057#include "atmel_nand_ecc.h" /* Hardware ECC registers */
Richard Genoud77f54922008-04-23 19:51:14 +020058
59/* oob layout for large page size
60 * bad block info is on bytes 0 and 1
61 * the bytes have to be consecutives to avoid
62 * several NAND_CMD_RNDOUT during read
63 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020064static struct nand_ecclayout atmel_oobinfo_large = {
Richard Genoud77f54922008-04-23 19:51:14 +020065 .eccbytes = 4,
66 .eccpos = {60, 61, 62, 63},
67 .oobfree = {
68 {2, 58}
69 },
70};
71
72/* oob layout for small page size
73 * bad block info is on bytes 4 and 5
74 * the bytes have to be consecutives to avoid
75 * several NAND_CMD_RNDOUT during read
76 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020077static struct nand_ecclayout atmel_oobinfo_small = {
Richard Genoud77f54922008-04-23 19:51:14 +020078 .eccbytes = 4,
79 .eccpos = {0, 1, 2, 3},
80 .oobfree = {
81 {6, 10}
82 },
83};
84
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020085struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +020086 struct nand_chip nand_chip;
87 struct mtd_info mtd;
88 void __iomem *io_base;
Hong Xucbc6c5e2011-01-18 14:36:05 +080089 dma_addr_t io_phys;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +080090 struct atmel_nand_data board;
Richard Genoud77f54922008-04-23 19:51:14 +020091 struct device *dev;
92 void __iomem *ecc;
Hong Xucbc6c5e2011-01-18 14:36:05 +080093
94 struct completion comp;
95 struct dma_chan *dma_chan;
Josh Wua41b51a2012-06-29 17:47:54 +080096
97 bool has_pmecc;
98 u8 pmecc_corr_cap;
99 u16 pmecc_sector_size;
100 u32 pmecc_lookup_table_offset;
Andrew Victor42cb1402006-10-19 18:24:35 +0200101};
102
Hong Xucbc6c5e2011-01-18 14:36:05 +0800103static int cpu_has_dma(void)
104{
105 return cpu_is_at91sam9rl() || cpu_is_at91sam9g45();
106}
107
Andrew Victor42cb1402006-10-19 18:24:35 +0200108/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900109 * Enable NAND.
110 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200111static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900112{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800113 if (gpio_is_valid(host->board.enable_pin))
114 gpio_set_value(host->board.enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900115}
116
117/*
118 * Disable NAND.
119 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200120static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900121{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800122 if (gpio_is_valid(host->board.enable_pin))
123 gpio_set_value(host->board.enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900124}
125
126/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200127 * Hardware specific access to control-lines
128 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200129static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200130{
131 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200132 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200133
Atsushi Nemoto81365082008-04-27 01:51:12 +0900134 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900135 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200136 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900137 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200138 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900139 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200140 if (cmd == NAND_CMD_NONE)
141 return;
142
143 if (ctrl & NAND_CLE)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800144 writeb(cmd, host->io_base + (1 << host->board.cle));
Andrew Victor42cb1402006-10-19 18:24:35 +0200145 else
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800146 writeb(cmd, host->io_base + (1 << host->board.ale));
Andrew Victor42cb1402006-10-19 18:24:35 +0200147}
148
149/*
150 * Read the Device Ready pin.
151 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200152static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200153{
154 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200155 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200156
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800157 return gpio_get_value(host->board.rdy_pin) ^
158 !!host->board.rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200159}
160
Artem Bityutskiy50082312012-02-02 13:54:25 +0200161/*
162 * Minimal-overhead PIO for data access.
163 */
164static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
165{
166 struct nand_chip *nand_chip = mtd->priv;
167
168 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
169}
170
171static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
172{
173 struct nand_chip *nand_chip = mtd->priv;
174
175 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
176}
177
178static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
179{
180 struct nand_chip *nand_chip = mtd->priv;
181
182 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
183}
184
185static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
186{
187 struct nand_chip *nand_chip = mtd->priv;
188
189 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
190}
191
Hong Xucbc6c5e2011-01-18 14:36:05 +0800192static void dma_complete_func(void *completion)
193{
194 complete(completion);
195}
196
197static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
198 int is_read)
199{
200 struct dma_device *dma_dev;
201 enum dma_ctrl_flags flags;
202 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
203 struct dma_async_tx_descriptor *tx = NULL;
204 dma_cookie_t cookie;
205 struct nand_chip *chip = mtd->priv;
206 struct atmel_nand_host *host = chip->priv;
207 void *p = buf;
208 int err = -EIO;
209 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
210
Hong Xu80b4f812011-03-31 18:33:15 +0800211 if (buf >= high_memory)
212 goto err_buf;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800213
214 dma_dev = host->dma_chan->device;
215
216 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_SRC_UNMAP |
217 DMA_COMPL_SKIP_DEST_UNMAP;
218
219 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
220 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
221 dev_err(host->dev, "Failed to dma_map_single\n");
222 goto err_buf;
223 }
224
225 if (is_read) {
226 dma_src_addr = host->io_phys;
227 dma_dst_addr = phys_addr;
228 } else {
229 dma_src_addr = phys_addr;
230 dma_dst_addr = host->io_phys;
231 }
232
233 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
234 dma_src_addr, len, flags);
235 if (!tx) {
236 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
237 goto err_dma;
238 }
239
240 init_completion(&host->comp);
241 tx->callback = dma_complete_func;
242 tx->callback_param = &host->comp;
243
244 cookie = tx->tx_submit(tx);
245 if (dma_submit_error(cookie)) {
246 dev_err(host->dev, "Failed to do DMA tx_submit\n");
247 goto err_dma;
248 }
249
250 dma_async_issue_pending(host->dma_chan);
251 wait_for_completion(&host->comp);
252
253 err = 0;
254
255err_dma:
256 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
257err_buf:
258 if (err != 0)
259 dev_warn(host->dev, "Fall back to CPU I/O\n");
260 return err;
261}
262
263static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
264{
265 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200266 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800267
Nicolas Ferre9d515672011-04-01 16:40:44 +0200268 if (use_dma && len > mtd->oobsize)
269 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800270 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
271 return;
272
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800273 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200274 atmel_read_buf16(mtd, buf, len);
275 else
276 atmel_read_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800277}
278
279static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
280{
281 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200282 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800283
Nicolas Ferre9d515672011-04-01 16:40:44 +0200284 if (use_dma && len > mtd->oobsize)
285 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800286 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
287 return;
288
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800289 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200290 atmel_write_buf16(mtd, buf, len);
291 else
292 atmel_write_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800293}
294
David Brownell23a346c2008-07-03 23:40:16 -0700295/*
Richard Genoud77f54922008-04-23 19:51:14 +0200296 * Calculate HW ECC
297 *
298 * function called after a write
299 *
300 * mtd: MTD block structure
301 * dat: raw data (unused)
302 * ecc_code: buffer for ECC
303 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200304static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +0200305 const u_char *dat, unsigned char *ecc_code)
306{
307 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200308 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +0200309 unsigned int ecc_value;
310
311 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +0200312 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +0200313
Richard Genoud3fc23892008-10-12 08:42:28 +0200314 ecc_code[0] = ecc_value & 0xFF;
315 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +0200316
317 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200318 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +0200319
Richard Genoud3fc23892008-10-12 08:42:28 +0200320 ecc_code[2] = ecc_value & 0xFF;
321 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +0200322
323 return 0;
324}
325
326/*
327 * HW ECC read page function
328 *
329 * mtd: mtd info structure
330 * chip: nand chip info structure
331 * buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -0700332 * oob_required: caller expects OOB data read to chip->oob_poi
Richard Genoud77f54922008-04-23 19:51:14 +0200333 */
Brian Norris1fbb9382012-05-02 10:14:55 -0700334static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
335 uint8_t *buf, int oob_required, int page)
Richard Genoud77f54922008-04-23 19:51:14 +0200336{
337 int eccsize = chip->ecc.size;
338 int eccbytes = chip->ecc.bytes;
339 uint32_t *eccpos = chip->ecc.layout->eccpos;
340 uint8_t *p = buf;
341 uint8_t *oob = chip->oob_poi;
342 uint8_t *ecc_pos;
343 int stat;
Mike Dunn3f91e942012-04-25 12:06:09 -0700344 unsigned int max_bitflips = 0;
Richard Genoud77f54922008-04-23 19:51:14 +0200345
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700346 /*
347 * Errata: ALE is incorrectly wired up to the ECC controller
348 * on the AP7000, so it will include the address cycles in the
349 * ECC calculation.
350 *
351 * Workaround: Reset the parity registers before reading the
352 * actual data.
353 */
354 if (cpu_is_at32ap7000()) {
355 struct atmel_nand_host *host = chip->priv;
356 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
357 }
358
Richard Genoud77f54922008-04-23 19:51:14 +0200359 /* read the page */
360 chip->read_buf(mtd, p, eccsize);
361
362 /* move to ECC position if needed */
363 if (eccpos[0] != 0) {
364 /* This only works on large pages
365 * because the ECC controller waits for
366 * NAND_CMD_RNDOUTSTART after the
367 * NAND_CMD_RNDOUT.
368 * anyway, for small pages, the eccpos[0] == 0
369 */
370 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
371 mtd->writesize + eccpos[0], -1);
372 }
373
374 /* the ECC controller needs to read the ECC just after the data */
375 ecc_pos = oob + eccpos[0];
376 chip->read_buf(mtd, ecc_pos, eccbytes);
377
378 /* check if there's an error */
379 stat = chip->ecc.correct(mtd, p, oob, NULL);
380
Mike Dunn3f91e942012-04-25 12:06:09 -0700381 if (stat < 0) {
Richard Genoud77f54922008-04-23 19:51:14 +0200382 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -0700383 } else {
Richard Genoud77f54922008-04-23 19:51:14 +0200384 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -0700385 max_bitflips = max_t(unsigned int, max_bitflips, stat);
386 }
Richard Genoud77f54922008-04-23 19:51:14 +0200387
388 /* get back to oob start (end of page) */
389 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
390
391 /* read the oob */
392 chip->read_buf(mtd, oob, mtd->oobsize);
393
Mike Dunn3f91e942012-04-25 12:06:09 -0700394 return max_bitflips;
Richard Genoud77f54922008-04-23 19:51:14 +0200395}
396
397/*
398 * HW ECC Correction
399 *
400 * function called after a read
401 *
402 * mtd: MTD block structure
403 * dat: raw data read from the chip
404 * read_ecc: ECC from the chip (unused)
405 * isnull: unused
406 *
407 * Detect and correct a 1 bit error for a page
408 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200409static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +0200410 u_char *read_ecc, u_char *isnull)
411{
412 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200413 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +0200414 unsigned int ecc_status;
415 unsigned int ecc_word, ecc_bit;
416
417 /* get the status from the Status Register */
418 ecc_status = ecc_readl(host->ecc, SR);
419
420 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200421 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +0200422 return 0;
423
424 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200425 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +0200426 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200427 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +0200428 ecc_word >>= 4;
429
430 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200431 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +0200432 /* check if it is a freshly erased block
433 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200434 if ((ecc_bit == ATMEL_ECC_BITADDR)
435 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +0200436 /* the block has just been erased, return OK */
437 return 0;
438 }
439 /* it doesn't seems to be a freshly
440 * erased block.
441 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200442 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +0200443 " Unable to correct.\n");
444 return -EIO;
445 }
446
447 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200448 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +0200449 /* there's nothing much to do here.
450 * the bit error is on the ECC itself.
451 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200452 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +0200453 " Nothing to correct\n");
454 return 0;
455 }
456
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200457 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +0200458 " (word offset in the page :"
459 " 0x%x bit offset : 0x%x)\n",
460 ecc_word, ecc_bit);
461 /* correct the error */
462 if (nand_chip->options & NAND_BUSWIDTH_16) {
463 /* 16 bits words */
464 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
465 } else {
466 /* 8 bits words */
467 dat[ecc_word] ^= (1 << ecc_bit);
468 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200469 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +0200470 return 1;
471}
472
473/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700474 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +0200475 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700476static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
477{
478 if (cpu_is_at32ap7000()) {
479 struct nand_chip *nand_chip = mtd->priv;
480 struct atmel_nand_host *host = nand_chip->priv;
481 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
482 }
483}
Richard Genoud77f54922008-04-23 19:51:14 +0200484
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800485#if defined(CONFIG_OF)
486static int __devinit atmel_of_init_port(struct atmel_nand_host *host,
487 struct device_node *np)
488{
Josh Wua41b51a2012-06-29 17:47:54 +0800489 u32 val, table_offset;
490 u32 offset[2];
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800491 int ecc_mode;
492 struct atmel_nand_data *board = &host->board;
493 enum of_gpio_flags flags;
494
495 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
496 if (val >= 32) {
497 dev_err(host->dev, "invalid addr-offset %u\n", val);
498 return -EINVAL;
499 }
500 board->ale = val;
501 }
502
503 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
504 if (val >= 32) {
505 dev_err(host->dev, "invalid cmd-offset %u\n", val);
506 return -EINVAL;
507 }
508 board->cle = val;
509 }
510
511 ecc_mode = of_get_nand_ecc_mode(np);
512
513 board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
514
515 board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
516
517 if (of_get_nand_bus_width(np) == 16)
518 board->bus_width_16 = 1;
519
520 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
521 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
522
523 board->enable_pin = of_get_gpio(np, 1);
524 board->det_pin = of_get_gpio(np, 2);
525
Josh Wua41b51a2012-06-29 17:47:54 +0800526 host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
527
528 if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
529 return 0; /* Not using PMECC */
530
531 /* use PMECC, get correction capability, sector size and lookup
532 * table offset.
533 */
534 if (of_property_read_u32(np, "atmel,pmecc-cap", &val) != 0) {
535 dev_err(host->dev, "Cannot decide PMECC Capability\n");
536 return -EINVAL;
537 } else if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
538 (val != 24)) {
539 dev_err(host->dev,
540 "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
541 val);
542 return -EINVAL;
543 }
544 host->pmecc_corr_cap = (u8)val;
545
546 if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) != 0) {
547 dev_err(host->dev, "Cannot decide PMECC Sector Size\n");
548 return -EINVAL;
549 } else if ((val != 512) && (val != 1024)) {
550 dev_err(host->dev,
551 "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
552 val);
553 return -EINVAL;
554 }
555 host->pmecc_sector_size = (u16)val;
556
557 if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
558 offset, 2) != 0) {
559 dev_err(host->dev, "Cannot get PMECC lookup table offset\n");
560 return -EINVAL;
561 }
562 table_offset = host->pmecc_sector_size == 512 ? offset[0] : offset[1];
563
564 if (!table_offset) {
565 dev_err(host->dev, "Invalid PMECC lookup table offset\n");
566 return -EINVAL;
567 }
568 host->pmecc_lookup_table_offset = table_offset;
569
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800570 return 0;
571}
572#else
573static int __devinit atmel_of_init_port(struct atmel_nand_host *host,
574 struct device_node *np)
575{
576 return -EINVAL;
577}
578#endif
579
Josh Wu3dfe41a2012-06-25 18:07:43 +0800580static int __init atmel_hw_nand_init_params(struct platform_device *pdev,
581 struct atmel_nand_host *host)
582{
583 struct mtd_info *mtd = &host->mtd;
584 struct nand_chip *nand_chip = &host->nand_chip;
585 struct resource *regs;
586
587 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
588 if (!regs) {
589 dev_err(host->dev,
590 "Can't get I/O resource regs, use software ECC\n");
591 nand_chip->ecc.mode = NAND_ECC_SOFT;
592 return 0;
593 }
594
595 host->ecc = ioremap(regs->start, resource_size(regs));
596 if (host->ecc == NULL) {
597 dev_err(host->dev, "ioremap failed\n");
598 return -EIO;
599 }
600
601 /* ECC is calculated for the whole page (1 step) */
602 nand_chip->ecc.size = mtd->writesize;
603
604 /* set ECC page size and oob layout */
605 switch (mtd->writesize) {
606 case 512:
607 nand_chip->ecc.layout = &atmel_oobinfo_small;
608 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
609 break;
610 case 1024:
611 nand_chip->ecc.layout = &atmel_oobinfo_large;
612 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
613 break;
614 case 2048:
615 nand_chip->ecc.layout = &atmel_oobinfo_large;
616 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
617 break;
618 case 4096:
619 nand_chip->ecc.layout = &atmel_oobinfo_large;
620 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
621 break;
622 default:
623 /* page size not handled by HW ECC */
624 /* switching back to soft ECC */
625 nand_chip->ecc.mode = NAND_ECC_SOFT;
626 return 0;
627 }
628
629 /* set up for HW ECC */
630 nand_chip->ecc.calculate = atmel_nand_calculate;
631 nand_chip->ecc.correct = atmel_nand_correct;
632 nand_chip->ecc.hwctl = atmel_nand_hwctl;
633 nand_chip->ecc.read_page = atmel_nand_read_page;
634 nand_chip->ecc.bytes = 4;
635 nand_chip->ecc.strength = 1;
636
637 return 0;
638}
639
Andrew Victor42cb1402006-10-19 18:24:35 +0200640/*
641 * Probe for the NAND device.
642 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200643static int __init atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +0200644{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200645 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +0200646 struct mtd_info *mtd;
647 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +0200648 struct resource *mem;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800649 struct mtd_part_parser_data ppdata = {};
Andrew Victor42cb1402006-10-19 18:24:35 +0200650 int res;
Andrew Victor42cb1402006-10-19 18:24:35 +0200651
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200652 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
653 if (!mem) {
654 printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
655 return -ENXIO;
656 }
657
Andrew Victor42cb1402006-10-19 18:24:35 +0200658 /* Allocate memory for the device structure (and zero it) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200659 host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
Andrew Victor42cb1402006-10-19 18:24:35 +0200660 if (!host) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200661 printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +0200662 return -ENOMEM;
663 }
664
Hong Xucbc6c5e2011-01-18 14:36:05 +0800665 host->io_phys = (dma_addr_t)mem->start;
666
Joe Perches28f65c112011-06-09 09:13:32 -0700667 host->io_base = ioremap(mem->start, resource_size(mem));
Andrew Victor42cb1402006-10-19 18:24:35 +0200668 if (host->io_base == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200669 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200670 res = -EIO;
671 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +0200672 }
673
674 mtd = &host->mtd;
675 nand_chip = &host->nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +0200676 host->dev = &pdev->dev;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800677 if (pdev->dev.of_node) {
678 res = atmel_of_init_port(host, pdev->dev.of_node);
679 if (res)
680 goto err_nand_ioremap;
681 } else {
682 memcpy(&host->board, pdev->dev.platform_data,
683 sizeof(struct atmel_nand_data));
684 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200685
686 nand_chip->priv = host; /* link the private data structures */
687 mtd->priv = nand_chip;
688 mtd->owner = THIS_MODULE;
689
690 /* Set address of NAND IO lines */
691 nand_chip->IO_ADDR_R = host->io_base;
692 nand_chip->IO_ADDR_W = host->io_base;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200693 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Ivan Kutena4265f82007-05-24 14:35:58 +0300694
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800695 if (gpio_is_valid(host->board.rdy_pin))
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200696 nand_chip->dev_ready = atmel_nand_device_ready;
Ivan Kutena4265f82007-05-24 14:35:58 +0300697
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800698 nand_chip->ecc.mode = host->board.ecc_mode;
Andrew Victor42cb1402006-10-19 18:24:35 +0200699 nand_chip->chip_delay = 20; /* 20us command delay time */
700
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800701 if (host->board.bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +0200702 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800703
704 nand_chip->read_buf = atmel_read_buf;
705 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +0200706
Andrew Victor42cb1402006-10-19 18:24:35 +0200707 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200708 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200709
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800710 if (gpio_is_valid(host->board.det_pin)) {
711 if (gpio_get_value(host->board.det_pin)) {
Simon Polettef4fa6972009-05-27 18:19:39 +0300712 printk(KERN_INFO "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +0100713 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200714 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +0200715 }
716 }
717
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800718 if (host->board.on_flash_bbt || on_flash_bbt) {
Simon Polettef4fa6972009-05-27 18:19:39 +0300719 printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
Brian Norrisbb9ebd42011-05-31 16:31:23 -0700720 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
Simon Polettef4fa6972009-05-27 18:19:39 +0300721 }
722
Hong Xucb457a42011-03-30 16:26:41 +0800723 if (!cpu_has_dma())
724 use_dma = 0;
725
726 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +0800727 dma_cap_mask_t mask;
728
729 dma_cap_zero(mask);
730 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab532011-06-29 18:41:16 +0200731 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800732 if (!host->dma_chan) {
733 dev_err(host->dev, "Failed to request DMA channel\n");
734 use_dma = 0;
735 }
736 }
737 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +0800738 dev_info(host->dev, "Using %s for DMA transfers.\n",
739 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +0800740 else
741 dev_info(host->dev, "No DMA support for NAND access.\n");
742
Richard Genoud77f54922008-04-23 19:51:14 +0200743 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +0000744 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +0200745 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200746 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +0200747 }
748
Richard Genoud3fc23892008-10-12 08:42:28 +0200749 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Josh Wu3dfe41a2012-06-25 18:07:43 +0800750 res = atmel_hw_nand_init_params(pdev, host);
751 if (res != 0)
752 goto err_hw_ecc;
Richard Genoud77f54922008-04-23 19:51:14 +0200753 }
754
755 /* second phase scan */
756 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +0200757 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200758 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +0200759 }
760
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200761 mtd->name = "atmel_nand";
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800762 ppdata.of_node = pdev->dev.of_node;
763 res = mtd_device_parse_register(mtd, NULL, &ppdata,
764 host->board.parts, host->board.num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +0200765 if (!res)
766 return res;
767
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200768err_scan_tail:
Josh Wu3dfe41a2012-06-25 18:07:43 +0800769 if (host->ecc)
770 iounmap(host->ecc);
771err_hw_ecc:
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200772err_scan_ident:
773err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200774 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200775 platform_set_drvdata(pdev, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800776 if (host->dma_chan)
777 dma_release_channel(host->dma_chan);
Andrew Victor42cb1402006-10-19 18:24:35 +0200778 iounmap(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200779err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +0200780 kfree(host);
781 return res;
782}
783
784/*
785 * Remove a NAND device.
786 */
David Brownell23a346c2008-07-03 23:40:16 -0700787static int __exit atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +0200788{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200789 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +0200790 struct mtd_info *mtd = &host->mtd;
791
792 nand_release(mtd);
793
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200794 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200795
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200796 if (host->ecc)
797 iounmap(host->ecc);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800798
799 if (host->dma_chan)
800 dma_release_channel(host->dma_chan);
801
Andrew Victor42cb1402006-10-19 18:24:35 +0200802 iounmap(host->io_base);
803 kfree(host);
804
805 return 0;
806}
807
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800808#if defined(CONFIG_OF)
809static const struct of_device_id atmel_nand_dt_ids[] = {
810 { .compatible = "atmel,at91rm9200-nand" },
811 { /* sentinel */ }
812};
813
814MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
815#endif
816
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200817static struct platform_driver atmel_nand_driver = {
David Brownell23a346c2008-07-03 23:40:16 -0700818 .remove = __exit_p(atmel_nand_remove),
Andrew Victor42cb1402006-10-19 18:24:35 +0200819 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200820 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +0200821 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800822 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
Andrew Victor42cb1402006-10-19 18:24:35 +0200823 },
824};
825
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200826static int __init atmel_nand_init(void)
Andrew Victor42cb1402006-10-19 18:24:35 +0200827{
David Brownell23a346c2008-07-03 23:40:16 -0700828 return platform_driver_probe(&atmel_nand_driver, atmel_nand_probe);
Andrew Victor42cb1402006-10-19 18:24:35 +0200829}
830
831
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200832static void __exit atmel_nand_exit(void)
Andrew Victor42cb1402006-10-19 18:24:35 +0200833{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200834 platform_driver_unregister(&atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +0200835}
836
837
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200838module_init(atmel_nand_init);
839module_exit(atmel_nand_exit);
Andrew Victor42cb1402006-10-19 18:24:35 +0200840
841MODULE_LICENSE("GPL");
842MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +0200843MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200844MODULE_ALIAS("platform:atmel_nand");