blob: 045d174b8277ad02c67f76acd035887dec71c05c [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 Polettef4fa697c2009-05-27 18:19:39 +030028#include <linux/moduleparam.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020029#include <linux/platform_device.h>
30#include <linux/mtd/mtd.h>
31#include <linux/mtd/nand.h>
32#include <linux/mtd/partitions.h>
33
Hans-Christian Egtvedt5c39c4c2011-04-13 15:55:17 +020034#include <linux/dmaengine.h>
David Woodhouse90574d02008-06-07 08:49:00 +010035#include <linux/gpio.h>
36#include <linux/io.h>
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +080037#include <linux/platform_data/atmel.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020038
Russell Kinga09e64f2008-08-05 16:14:15 +010039#include <mach/cpu.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020040
Hong Xucbc6c5e2011-01-18 14:36:05 +080041static int use_dma = 1;
42module_param(use_dma, int, 0);
43
Simon Polettef4fa697c2009-05-27 18:19:39 +030044static int on_flash_bbt = 0;
45module_param(on_flash_bbt, int, 0);
46
Richard Genoud77f54922008-04-23 19:51:14 +020047/* Register access macros */
48#define ecc_readl(add, reg) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020049 __raw_readl(add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020050#define ecc_writel(add, reg, value) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020051 __raw_writel((value), add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020052
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020053#include "atmel_nand_ecc.h" /* Hardware ECC registers */
Richard Genoud77f54922008-04-23 19:51:14 +020054
55/* oob layout for large page size
56 * bad block info is on bytes 0 and 1
57 * the bytes have to be consecutives to avoid
58 * several NAND_CMD_RNDOUT during read
59 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020060static struct nand_ecclayout atmel_oobinfo_large = {
Richard Genoud77f54922008-04-23 19:51:14 +020061 .eccbytes = 4,
62 .eccpos = {60, 61, 62, 63},
63 .oobfree = {
64 {2, 58}
65 },
66};
67
68/* oob layout for small page size
69 * bad block info is on bytes 4 and 5
70 * the bytes have to be consecutives to avoid
71 * several NAND_CMD_RNDOUT during read
72 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020073static struct nand_ecclayout atmel_oobinfo_small = {
Richard Genoud77f54922008-04-23 19:51:14 +020074 .eccbytes = 4,
75 .eccpos = {0, 1, 2, 3},
76 .oobfree = {
77 {6, 10}
78 },
79};
80
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020081struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +020082 struct nand_chip nand_chip;
83 struct mtd_info mtd;
84 void __iomem *io_base;
Hong Xucbc6c5e2011-01-18 14:36:05 +080085 dma_addr_t io_phys;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020086 struct atmel_nand_data *board;
Richard Genoud77f54922008-04-23 19:51:14 +020087 struct device *dev;
88 void __iomem *ecc;
Hong Xucbc6c5e2011-01-18 14:36:05 +080089
90 struct completion comp;
91 struct dma_chan *dma_chan;
Andrew Victor42cb1402006-10-19 18:24:35 +020092};
93
Hong Xucbc6c5e2011-01-18 14:36:05 +080094static int cpu_has_dma(void)
95{
96 return cpu_is_at91sam9rl() || cpu_is_at91sam9g45();
97}
98
Andrew Victor42cb1402006-10-19 18:24:35 +020099/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900100 * Enable NAND.
101 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200102static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900103{
Jean-Christophe PLAGNIOL-VILLARD1d6dc062011-09-19 15:31:37 +0800104 if (gpio_is_valid(host->board->enable_pin))
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200105 gpio_set_value(host->board->enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900106}
107
108/*
109 * Disable NAND.
110 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200111static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900112{
Jean-Christophe PLAGNIOL-VILLARD1d6dc062011-09-19 15:31:37 +0800113 if (gpio_is_valid(host->board->enable_pin))
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200114 gpio_set_value(host->board->enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900115}
116
117/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200118 * Hardware specific access to control-lines
119 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200120static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200121{
122 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200123 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200124
Atsushi Nemoto81365082008-04-27 01:51:12 +0900125 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900126 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200127 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900128 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200129 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900130 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200131 if (cmd == NAND_CMD_NONE)
132 return;
133
134 if (ctrl & NAND_CLE)
135 writeb(cmd, host->io_base + (1 << host->board->cle));
136 else
137 writeb(cmd, host->io_base + (1 << host->board->ale));
138}
139
140/*
141 * Read the Device Ready pin.
142 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200143static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200144{
145 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200146 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200147
Gregory CLEMENT744f6592009-02-16 21:21:47 +0100148 return gpio_get_value(host->board->rdy_pin) ^
149 !!host->board->rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200150}
151
Artem Bityutskiy50082312012-02-02 13:54:25 +0200152/*
153 * Minimal-overhead PIO for data access.
154 */
155static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
156{
157 struct nand_chip *nand_chip = mtd->priv;
158
159 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
160}
161
162static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
163{
164 struct nand_chip *nand_chip = mtd->priv;
165
166 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
167}
168
169static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
170{
171 struct nand_chip *nand_chip = mtd->priv;
172
173 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
174}
175
176static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
177{
178 struct nand_chip *nand_chip = mtd->priv;
179
180 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
181}
182
Hong Xucbc6c5e2011-01-18 14:36:05 +0800183static void dma_complete_func(void *completion)
184{
185 complete(completion);
186}
187
188static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
189 int is_read)
190{
191 struct dma_device *dma_dev;
192 enum dma_ctrl_flags flags;
193 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
194 struct dma_async_tx_descriptor *tx = NULL;
195 dma_cookie_t cookie;
196 struct nand_chip *chip = mtd->priv;
197 struct atmel_nand_host *host = chip->priv;
198 void *p = buf;
199 int err = -EIO;
200 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
201
Hong Xu80b4f812011-03-31 18:33:15 +0800202 if (buf >= high_memory)
203 goto err_buf;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800204
205 dma_dev = host->dma_chan->device;
206
207 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_SRC_UNMAP |
208 DMA_COMPL_SKIP_DEST_UNMAP;
209
210 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
211 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
212 dev_err(host->dev, "Failed to dma_map_single\n");
213 goto err_buf;
214 }
215
216 if (is_read) {
217 dma_src_addr = host->io_phys;
218 dma_dst_addr = phys_addr;
219 } else {
220 dma_src_addr = phys_addr;
221 dma_dst_addr = host->io_phys;
222 }
223
224 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
225 dma_src_addr, len, flags);
226 if (!tx) {
227 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
228 goto err_dma;
229 }
230
231 init_completion(&host->comp);
232 tx->callback = dma_complete_func;
233 tx->callback_param = &host->comp;
234
235 cookie = tx->tx_submit(tx);
236 if (dma_submit_error(cookie)) {
237 dev_err(host->dev, "Failed to do DMA tx_submit\n");
238 goto err_dma;
239 }
240
241 dma_async_issue_pending(host->dma_chan);
242 wait_for_completion(&host->comp);
243
244 err = 0;
245
246err_dma:
247 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
248err_buf:
249 if (err != 0)
250 dev_warn(host->dev, "Fall back to CPU I/O\n");
251 return err;
252}
253
254static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
255{
256 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200257 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800258
Nicolas Ferre9d515672011-04-01 16:40:44 +0200259 if (use_dma && len > mtd->oobsize)
260 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800261 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
262 return;
263
Artem Bityutskiy50082312012-02-02 13:54:25 +0200264 if (host->board->bus_width_16)
265 atmel_read_buf16(mtd, buf, len);
266 else
267 atmel_read_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800268}
269
270static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
271{
272 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200273 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800274
Nicolas Ferre9d515672011-04-01 16:40:44 +0200275 if (use_dma && len > mtd->oobsize)
276 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800277 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
278 return;
279
Artem Bityutskiy50082312012-02-02 13:54:25 +0200280 if (host->board->bus_width_16)
281 atmel_write_buf16(mtd, buf, len);
282 else
283 atmel_write_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800284}
285
David Brownell23a346c2008-07-03 23:40:16 -0700286/*
Richard Genoud77f54922008-04-23 19:51:14 +0200287 * Calculate HW ECC
288 *
289 * function called after a write
290 *
291 * mtd: MTD block structure
292 * dat: raw data (unused)
293 * ecc_code: buffer for ECC
294 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200295static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +0200296 const u_char *dat, unsigned char *ecc_code)
297{
298 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200299 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +0200300 unsigned int ecc_value;
301
302 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +0200303 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +0200304
Richard Genoud3fc23892008-10-12 08:42:28 +0200305 ecc_code[0] = ecc_value & 0xFF;
306 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +0200307
308 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200309 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +0200310
Richard Genoud3fc23892008-10-12 08:42:28 +0200311 ecc_code[2] = ecc_value & 0xFF;
312 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +0200313
314 return 0;
315}
316
317/*
318 * HW ECC read page function
319 *
320 * mtd: mtd info structure
321 * chip: nand chip info structure
322 * buf: buffer to store read data
323 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200324static int atmel_nand_read_page(struct mtd_info *mtd,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700325 struct nand_chip *chip, uint8_t *buf, int page)
Richard Genoud77f54922008-04-23 19:51:14 +0200326{
327 int eccsize = chip->ecc.size;
328 int eccbytes = chip->ecc.bytes;
329 uint32_t *eccpos = chip->ecc.layout->eccpos;
330 uint8_t *p = buf;
331 uint8_t *oob = chip->oob_poi;
332 uint8_t *ecc_pos;
333 int stat;
334
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700335 /*
336 * Errata: ALE is incorrectly wired up to the ECC controller
337 * on the AP7000, so it will include the address cycles in the
338 * ECC calculation.
339 *
340 * Workaround: Reset the parity registers before reading the
341 * actual data.
342 */
343 if (cpu_is_at32ap7000()) {
344 struct atmel_nand_host *host = chip->priv;
345 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
346 }
347
Richard Genoud77f54922008-04-23 19:51:14 +0200348 /* read the page */
349 chip->read_buf(mtd, p, eccsize);
350
351 /* move to ECC position if needed */
352 if (eccpos[0] != 0) {
353 /* This only works on large pages
354 * because the ECC controller waits for
355 * NAND_CMD_RNDOUTSTART after the
356 * NAND_CMD_RNDOUT.
357 * anyway, for small pages, the eccpos[0] == 0
358 */
359 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
360 mtd->writesize + eccpos[0], -1);
361 }
362
363 /* the ECC controller needs to read the ECC just after the data */
364 ecc_pos = oob + eccpos[0];
365 chip->read_buf(mtd, ecc_pos, eccbytes);
366
367 /* check if there's an error */
368 stat = chip->ecc.correct(mtd, p, oob, NULL);
369
370 if (stat < 0)
371 mtd->ecc_stats.failed++;
372 else
373 mtd->ecc_stats.corrected += stat;
374
375 /* get back to oob start (end of page) */
376 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
377
378 /* read the oob */
379 chip->read_buf(mtd, oob, mtd->oobsize);
380
381 return 0;
382}
383
384/*
385 * HW ECC Correction
386 *
387 * function called after a read
388 *
389 * mtd: MTD block structure
390 * dat: raw data read from the chip
391 * read_ecc: ECC from the chip (unused)
392 * isnull: unused
393 *
394 * Detect and correct a 1 bit error for a page
395 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200396static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +0200397 u_char *read_ecc, u_char *isnull)
398{
399 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200400 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +0200401 unsigned int ecc_status;
402 unsigned int ecc_word, ecc_bit;
403
404 /* get the status from the Status Register */
405 ecc_status = ecc_readl(host->ecc, SR);
406
407 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200408 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +0200409 return 0;
410
411 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200412 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +0200413 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200414 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +0200415 ecc_word >>= 4;
416
417 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200418 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +0200419 /* check if it is a freshly erased block
420 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200421 if ((ecc_bit == ATMEL_ECC_BITADDR)
422 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +0200423 /* the block has just been erased, return OK */
424 return 0;
425 }
426 /* it doesn't seems to be a freshly
427 * erased block.
428 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200429 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +0200430 " Unable to correct.\n");
431 return -EIO;
432 }
433
434 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200435 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +0200436 /* there's nothing much to do here.
437 * the bit error is on the ECC itself.
438 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200439 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +0200440 " Nothing to correct\n");
441 return 0;
442 }
443
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200444 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +0200445 " (word offset in the page :"
446 " 0x%x bit offset : 0x%x)\n",
447 ecc_word, ecc_bit);
448 /* correct the error */
449 if (nand_chip->options & NAND_BUSWIDTH_16) {
450 /* 16 bits words */
451 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
452 } else {
453 /* 8 bits words */
454 dat[ecc_word] ^= (1 << ecc_bit);
455 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200456 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +0200457 return 1;
458}
459
460/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700461 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +0200462 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700463static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
464{
465 if (cpu_is_at32ap7000()) {
466 struct nand_chip *nand_chip = mtd->priv;
467 struct atmel_nand_host *host = nand_chip->priv;
468 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
469 }
470}
Richard Genoud77f54922008-04-23 19:51:14 +0200471
Andrew Victor42cb1402006-10-19 18:24:35 +0200472/*
473 * Probe for the NAND device.
474 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200475static int __init atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +0200476{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200477 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +0200478 struct mtd_info *mtd;
479 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +0200480 struct resource *regs;
481 struct resource *mem;
Andrew Victor42cb1402006-10-19 18:24:35 +0200482 int res;
Andrew Victor42cb1402006-10-19 18:24:35 +0200483
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200484 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
485 if (!mem) {
486 printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
487 return -ENXIO;
488 }
489
Andrew Victor42cb1402006-10-19 18:24:35 +0200490 /* Allocate memory for the device structure (and zero it) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200491 host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
Andrew Victor42cb1402006-10-19 18:24:35 +0200492 if (!host) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200493 printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +0200494 return -ENOMEM;
495 }
496
Hong Xucbc6c5e2011-01-18 14:36:05 +0800497 host->io_phys = (dma_addr_t)mem->start;
498
Joe Perches28f65c112011-06-09 09:13:32 -0700499 host->io_base = ioremap(mem->start, resource_size(mem));
Andrew Victor42cb1402006-10-19 18:24:35 +0200500 if (host->io_base == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200501 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200502 res = -EIO;
503 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +0200504 }
505
506 mtd = &host->mtd;
507 nand_chip = &host->nand_chip;
508 host->board = pdev->dev.platform_data;
Richard Genoud77f54922008-04-23 19:51:14 +0200509 host->dev = &pdev->dev;
Andrew Victor42cb1402006-10-19 18:24:35 +0200510
511 nand_chip->priv = host; /* link the private data structures */
512 mtd->priv = nand_chip;
513 mtd->owner = THIS_MODULE;
514
515 /* Set address of NAND IO lines */
516 nand_chip->IO_ADDR_R = host->io_base;
517 nand_chip->IO_ADDR_W = host->io_base;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200518 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Ivan Kutena4265f82007-05-24 14:35:58 +0300519
Jean-Christophe PLAGNIOL-VILLARD1d6dc062011-09-19 15:31:37 +0800520 if (gpio_is_valid(host->board->rdy_pin))
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200521 nand_chip->dev_ready = atmel_nand_device_ready;
Ivan Kutena4265f82007-05-24 14:35:58 +0300522
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +0800523 nand_chip->ecc.mode = host->board->ecc_mode;
524
Richard Genoud77f54922008-04-23 19:51:14 +0200525 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +0800526 if (!regs && nand_chip->ecc.mode == NAND_ECC_HW) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200527 printk(KERN_ERR "atmel_nand: can't get I/O resource "
Richard Genoud77f54922008-04-23 19:51:14 +0200528 "regs\nFalling back on software ECC\n");
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +0800529 nand_chip->ecc.mode = NAND_ECC_SOFT;
Richard Genoud77f54922008-04-23 19:51:14 +0200530 }
531
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +0800532 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Joe Perches28f65c112011-06-09 09:13:32 -0700533 host->ecc = ioremap(regs->start, resource_size(regs));
Richard Genoud77f54922008-04-23 19:51:14 +0200534 if (host->ecc == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200535 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Richard Genoud77f54922008-04-23 19:51:14 +0200536 res = -EIO;
537 goto err_ecc_ioremap;
538 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200539 nand_chip->ecc.calculate = atmel_nand_calculate;
540 nand_chip->ecc.correct = atmel_nand_correct;
541 nand_chip->ecc.hwctl = atmel_nand_hwctl;
542 nand_chip->ecc.read_page = atmel_nand_read_page;
Richard Genoud77f54922008-04-23 19:51:14 +0200543 nand_chip->ecc.bytes = 4;
Richard Genoud77f54922008-04-23 19:51:14 +0200544 }
545
Andrew Victor42cb1402006-10-19 18:24:35 +0200546 nand_chip->chip_delay = 20; /* 20us command delay time */
547
Hong Xucbc6c5e2011-01-18 14:36:05 +0800548 if (host->board->bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +0200549 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800550
551 nand_chip->read_buf = atmel_read_buf;
552 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +0200553
Andrew Victor42cb1402006-10-19 18:24:35 +0200554 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200555 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200556
Jean-Christophe PLAGNIOL-VILLARD1d6dc062011-09-19 15:31:37 +0800557 if (gpio_is_valid(host->board->det_pin)) {
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200558 if (gpio_get_value(host->board->det_pin)) {
Simon Polettef4fa697c2009-05-27 18:19:39 +0300559 printk(KERN_INFO "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +0100560 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200561 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +0200562 }
563 }
564
Jean-Christophe PLAGNIOL-VILLARD3dcb7ea2011-12-29 14:59:54 +0800565 if (host->board->on_flash_bbt || on_flash_bbt) {
Simon Polettef4fa697c2009-05-27 18:19:39 +0300566 printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
Brian Norrisbb9ebd4e2011-05-31 16:31:23 -0700567 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
Simon Polettef4fa697c2009-05-27 18:19:39 +0300568 }
569
Hong Xucb457a42011-03-30 16:26:41 +0800570 if (!cpu_has_dma())
571 use_dma = 0;
572
573 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +0800574 dma_cap_mask_t mask;
575
576 dma_cap_zero(mask);
577 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab5362011-06-29 18:41:16 +0200578 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800579 if (!host->dma_chan) {
580 dev_err(host->dev, "Failed to request DMA channel\n");
581 use_dma = 0;
582 }
583 }
584 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +0800585 dev_info(host->dev, "Using %s for DMA transfers.\n",
586 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +0800587 else
588 dev_info(host->dev, "No DMA support for NAND access.\n");
589
Richard Genoud77f54922008-04-23 19:51:14 +0200590 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +0000591 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +0200592 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200593 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +0200594 }
595
Richard Genoud3fc23892008-10-12 08:42:28 +0200596 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Richard Genoud77f54922008-04-23 19:51:14 +0200597 /* ECC is calculated for the whole page (1 step) */
598 nand_chip->ecc.size = mtd->writesize;
599
600 /* set ECC page size and oob layout */
601 switch (mtd->writesize) {
602 case 512:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200603 nand_chip->ecc.layout = &atmel_oobinfo_small;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200604 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
Richard Genoud77f54922008-04-23 19:51:14 +0200605 break;
606 case 1024:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200607 nand_chip->ecc.layout = &atmel_oobinfo_large;
608 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
Richard Genoud77f54922008-04-23 19:51:14 +0200609 break;
610 case 2048:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200611 nand_chip->ecc.layout = &atmel_oobinfo_large;
612 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
Richard Genoud77f54922008-04-23 19:51:14 +0200613 break;
614 case 4096:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200615 nand_chip->ecc.layout = &atmel_oobinfo_large;
616 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
Richard Genoud77f54922008-04-23 19:51:14 +0200617 break;
618 default:
619 /* page size not handled by HW ECC */
620 /* switching back to soft ECC */
621 nand_chip->ecc.mode = NAND_ECC_SOFT;
622 nand_chip->ecc.calculate = NULL;
623 nand_chip->ecc.correct = NULL;
624 nand_chip->ecc.hwctl = NULL;
625 nand_chip->ecc.read_page = NULL;
626 nand_chip->ecc.postpad = 0;
627 nand_chip->ecc.prepad = 0;
628 nand_chip->ecc.bytes = 0;
629 break;
630 }
631 }
632
633 /* second phase scan */
634 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +0200635 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200636 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +0200637 }
638
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200639 mtd->name = "atmel_nand";
Dmitry Eremin-Solenikovef5d79f2011-06-02 18:00:23 +0400640 res = mtd_device_parse_register(mtd, NULL, 0,
641 host->board->parts, host->board->num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +0200642 if (!res)
643 return res;
644
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200645err_scan_tail:
646err_scan_ident:
647err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200648 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200649 platform_set_drvdata(pdev, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800650 if (host->dma_chan)
651 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200652 if (host->ecc)
653 iounmap(host->ecc);
654err_ecc_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +0200655 iounmap(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200656err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +0200657 kfree(host);
658 return res;
659}
660
661/*
662 * Remove a NAND device.
663 */
David Brownell23a346c2008-07-03 23:40:16 -0700664static int __exit atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +0200665{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200666 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +0200667 struct mtd_info *mtd = &host->mtd;
668
669 nand_release(mtd);
670
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200671 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200672
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200673 if (host->ecc)
674 iounmap(host->ecc);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800675
676 if (host->dma_chan)
677 dma_release_channel(host->dma_chan);
678
Andrew Victor42cb1402006-10-19 18:24:35 +0200679 iounmap(host->io_base);
680 kfree(host);
681
682 return 0;
683}
684
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200685static struct platform_driver atmel_nand_driver = {
David Brownell23a346c2008-07-03 23:40:16 -0700686 .remove = __exit_p(atmel_nand_remove),
Andrew Victor42cb1402006-10-19 18:24:35 +0200687 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200688 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +0200689 .owner = THIS_MODULE,
690 },
691};
692
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200693static int __init atmel_nand_init(void)
Andrew Victor42cb1402006-10-19 18:24:35 +0200694{
David Brownell23a346c2008-07-03 23:40:16 -0700695 return platform_driver_probe(&atmel_nand_driver, atmel_nand_probe);
Andrew Victor42cb1402006-10-19 18:24:35 +0200696}
697
698
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200699static void __exit atmel_nand_exit(void)
Andrew Victor42cb1402006-10-19 18:24:35 +0200700{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200701 platform_driver_unregister(&atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +0200702}
703
704
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200705module_init(atmel_nand_init);
706module_exit(atmel_nand_exit);
Andrew Victor42cb1402006-10-19 18:24:35 +0200707
708MODULE_LICENSE("GPL");
709MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +0200710MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200711MODULE_ALIAS("platform:atmel_nand");