blob: 35b4fb55dbd6569dad6d4c927c9807698804b882 [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>
Andrew Victor42cb1402006-10-19 18:24:35 +020037
Russell Kinga09e64f2008-08-05 16:14:15 +010038#include <mach/board.h>
39#include <mach/cpu.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020040
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020041#ifdef CONFIG_MTD_NAND_ATMEL_ECC_HW
Richard Genoud77f54922008-04-23 19:51:14 +020042#define hard_ecc 1
43#else
44#define hard_ecc 0
45#endif
46
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020047#ifdef CONFIG_MTD_NAND_ATMEL_ECC_NONE
Richard Genoud77f54922008-04-23 19:51:14 +020048#define no_ecc 1
49#else
50#define no_ecc 0
51#endif
52
Hong Xucbc6c5e2011-01-18 14:36:05 +080053static int use_dma = 1;
54module_param(use_dma, int, 0);
55
Simon Polettef4fa697c2009-05-27 18:19:39 +030056static int on_flash_bbt = 0;
57module_param(on_flash_bbt, int, 0);
58
Richard Genoud77f54922008-04-23 19:51:14 +020059/* Register access macros */
60#define ecc_readl(add, reg) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020061 __raw_readl(add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020062#define ecc_writel(add, reg, value) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020063 __raw_writel((value), add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020064
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020065#include "atmel_nand_ecc.h" /* Hardware ECC registers */
Richard Genoud77f54922008-04-23 19:51:14 +020066
67/* oob layout for large page size
68 * bad block info is on bytes 0 and 1
69 * the bytes have to be consecutives to avoid
70 * several NAND_CMD_RNDOUT during read
71 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020072static struct nand_ecclayout atmel_oobinfo_large = {
Richard Genoud77f54922008-04-23 19:51:14 +020073 .eccbytes = 4,
74 .eccpos = {60, 61, 62, 63},
75 .oobfree = {
76 {2, 58}
77 },
78};
79
80/* oob layout for small page size
81 * bad block info is on bytes 4 and 5
82 * the bytes have to be consecutives to avoid
83 * several NAND_CMD_RNDOUT during read
84 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020085static struct nand_ecclayout atmel_oobinfo_small = {
Richard Genoud77f54922008-04-23 19:51:14 +020086 .eccbytes = 4,
87 .eccpos = {0, 1, 2, 3},
88 .oobfree = {
89 {6, 10}
90 },
91};
92
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020093struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +020094 struct nand_chip nand_chip;
95 struct mtd_info mtd;
96 void __iomem *io_base;
Hong Xucbc6c5e2011-01-18 14:36:05 +080097 dma_addr_t io_phys;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020098 struct atmel_nand_data *board;
Richard Genoud77f54922008-04-23 19:51:14 +020099 struct device *dev;
100 void __iomem *ecc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800101
102 struct completion comp;
103 struct dma_chan *dma_chan;
Andrew Victor42cb1402006-10-19 18:24:35 +0200104};
105
Hong Xucbc6c5e2011-01-18 14:36:05 +0800106static int cpu_has_dma(void)
107{
108 return cpu_is_at91sam9rl() || cpu_is_at91sam9g45();
109}
110
Andrew Victor42cb1402006-10-19 18:24:35 +0200111/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900112 * Enable NAND.
113 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200114static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900115{
Jean-Christophe PLAGNIOL-VILLARD1d6dc062011-09-19 15:31:37 +0800116 if (gpio_is_valid(host->board->enable_pin))
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200117 gpio_set_value(host->board->enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900118}
119
120/*
121 * Disable NAND.
122 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200123static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900124{
Jean-Christophe PLAGNIOL-VILLARD1d6dc062011-09-19 15:31:37 +0800125 if (gpio_is_valid(host->board->enable_pin))
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200126 gpio_set_value(host->board->enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900127}
128
129/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200130 * Hardware specific access to control-lines
131 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200132static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200133{
134 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200135 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200136
Atsushi Nemoto81365082008-04-27 01:51:12 +0900137 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900138 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200139 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900140 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200141 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900142 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200143 if (cmd == NAND_CMD_NONE)
144 return;
145
146 if (ctrl & NAND_CLE)
147 writeb(cmd, host->io_base + (1 << host->board->cle));
148 else
149 writeb(cmd, host->io_base + (1 << host->board->ale));
150}
151
152/*
153 * Read the Device Ready pin.
154 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200155static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200156{
157 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200158 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200159
Gregory CLEMENT744f6592009-02-16 21:21:47 +0100160 return gpio_get_value(host->board->rdy_pin) ^
161 !!host->board->rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200162}
163
Artem Bityutskiy50082312012-02-02 13:54:25 +0200164/*
165 * Minimal-overhead PIO for data access.
166 */
167static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
168{
169 struct nand_chip *nand_chip = mtd->priv;
170
171 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
172}
173
174static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
175{
176 struct nand_chip *nand_chip = mtd->priv;
177
178 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
179}
180
181static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
182{
183 struct nand_chip *nand_chip = mtd->priv;
184
185 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
186}
187
188static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
189{
190 struct nand_chip *nand_chip = mtd->priv;
191
192 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
193}
194
Hong Xucbc6c5e2011-01-18 14:36:05 +0800195static void dma_complete_func(void *completion)
196{
197 complete(completion);
198}
199
200static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
201 int is_read)
202{
203 struct dma_device *dma_dev;
204 enum dma_ctrl_flags flags;
205 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
206 struct dma_async_tx_descriptor *tx = NULL;
207 dma_cookie_t cookie;
208 struct nand_chip *chip = mtd->priv;
209 struct atmel_nand_host *host = chip->priv;
210 void *p = buf;
211 int err = -EIO;
212 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
213
Hong Xu80b4f812011-03-31 18:33:15 +0800214 if (buf >= high_memory)
215 goto err_buf;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800216
217 dma_dev = host->dma_chan->device;
218
219 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_SRC_UNMAP |
220 DMA_COMPL_SKIP_DEST_UNMAP;
221
222 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
223 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
224 dev_err(host->dev, "Failed to dma_map_single\n");
225 goto err_buf;
226 }
227
228 if (is_read) {
229 dma_src_addr = host->io_phys;
230 dma_dst_addr = phys_addr;
231 } else {
232 dma_src_addr = phys_addr;
233 dma_dst_addr = host->io_phys;
234 }
235
236 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
237 dma_src_addr, len, flags);
238 if (!tx) {
239 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
240 goto err_dma;
241 }
242
243 init_completion(&host->comp);
244 tx->callback = dma_complete_func;
245 tx->callback_param = &host->comp;
246
247 cookie = tx->tx_submit(tx);
248 if (dma_submit_error(cookie)) {
249 dev_err(host->dev, "Failed to do DMA tx_submit\n");
250 goto err_dma;
251 }
252
253 dma_async_issue_pending(host->dma_chan);
254 wait_for_completion(&host->comp);
255
256 err = 0;
257
258err_dma:
259 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
260err_buf:
261 if (err != 0)
262 dev_warn(host->dev, "Fall back to CPU I/O\n");
263 return err;
264}
265
266static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
267{
268 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200269 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800270
Nicolas Ferre9d515672011-04-01 16:40:44 +0200271 if (use_dma && len > mtd->oobsize)
272 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800273 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
274 return;
275
Artem Bityutskiy50082312012-02-02 13:54:25 +0200276 if (host->board->bus_width_16)
277 atmel_read_buf16(mtd, buf, len);
278 else
279 atmel_read_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800280}
281
282static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
283{
284 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200285 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800286
Nicolas Ferre9d515672011-04-01 16:40:44 +0200287 if (use_dma && len > mtd->oobsize)
288 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800289 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
290 return;
291
Artem Bityutskiy50082312012-02-02 13:54:25 +0200292 if (host->board->bus_width_16)
293 atmel_write_buf16(mtd, buf, len);
294 else
295 atmel_write_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800296}
297
David Brownell23a346c2008-07-03 23:40:16 -0700298/*
Richard Genoud77f54922008-04-23 19:51:14 +0200299 * Calculate HW ECC
300 *
301 * function called after a write
302 *
303 * mtd: MTD block structure
304 * dat: raw data (unused)
305 * ecc_code: buffer for ECC
306 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200307static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +0200308 const u_char *dat, unsigned char *ecc_code)
309{
310 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200311 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +0200312 unsigned int ecc_value;
313
314 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +0200315 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +0200316
Richard Genoud3fc23892008-10-12 08:42:28 +0200317 ecc_code[0] = ecc_value & 0xFF;
318 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +0200319
320 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200321 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +0200322
Richard Genoud3fc23892008-10-12 08:42:28 +0200323 ecc_code[2] = ecc_value & 0xFF;
324 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +0200325
326 return 0;
327}
328
329/*
330 * HW ECC read page function
331 *
332 * mtd: mtd info structure
333 * chip: nand chip info structure
334 * buf: buffer to store read data
335 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200336static int atmel_nand_read_page(struct mtd_info *mtd,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700337 struct nand_chip *chip, uint8_t *buf, int page)
Richard Genoud77f54922008-04-23 19:51:14 +0200338{
339 int eccsize = chip->ecc.size;
340 int eccbytes = chip->ecc.bytes;
341 uint32_t *eccpos = chip->ecc.layout->eccpos;
342 uint8_t *p = buf;
343 uint8_t *oob = chip->oob_poi;
344 uint8_t *ecc_pos;
345 int stat;
346
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700347 /*
348 * Errata: ALE is incorrectly wired up to the ECC controller
349 * on the AP7000, so it will include the address cycles in the
350 * ECC calculation.
351 *
352 * Workaround: Reset the parity registers before reading the
353 * actual data.
354 */
355 if (cpu_is_at32ap7000()) {
356 struct atmel_nand_host *host = chip->priv;
357 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
358 }
359
Richard Genoud77f54922008-04-23 19:51:14 +0200360 /* read the page */
361 chip->read_buf(mtd, p, eccsize);
362
363 /* move to ECC position if needed */
364 if (eccpos[0] != 0) {
365 /* This only works on large pages
366 * because the ECC controller waits for
367 * NAND_CMD_RNDOUTSTART after the
368 * NAND_CMD_RNDOUT.
369 * anyway, for small pages, the eccpos[0] == 0
370 */
371 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
372 mtd->writesize + eccpos[0], -1);
373 }
374
375 /* the ECC controller needs to read the ECC just after the data */
376 ecc_pos = oob + eccpos[0];
377 chip->read_buf(mtd, ecc_pos, eccbytes);
378
379 /* check if there's an error */
380 stat = chip->ecc.correct(mtd, p, oob, NULL);
381
382 if (stat < 0)
383 mtd->ecc_stats.failed++;
384 else
385 mtd->ecc_stats.corrected += stat;
386
387 /* get back to oob start (end of page) */
388 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
389
390 /* read the oob */
391 chip->read_buf(mtd, oob, mtd->oobsize);
392
393 return 0;
394}
395
396/*
397 * HW ECC Correction
398 *
399 * function called after a read
400 *
401 * mtd: MTD block structure
402 * dat: raw data read from the chip
403 * read_ecc: ECC from the chip (unused)
404 * isnull: unused
405 *
406 * Detect and correct a 1 bit error for a page
407 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200408static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +0200409 u_char *read_ecc, u_char *isnull)
410{
411 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200412 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +0200413 unsigned int ecc_status;
414 unsigned int ecc_word, ecc_bit;
415
416 /* get the status from the Status Register */
417 ecc_status = ecc_readl(host->ecc, SR);
418
419 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200420 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +0200421 return 0;
422
423 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200424 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +0200425 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200426 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +0200427 ecc_word >>= 4;
428
429 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200430 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +0200431 /* check if it is a freshly erased block
432 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200433 if ((ecc_bit == ATMEL_ECC_BITADDR)
434 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +0200435 /* the block has just been erased, return OK */
436 return 0;
437 }
438 /* it doesn't seems to be a freshly
439 * erased block.
440 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200441 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +0200442 " Unable to correct.\n");
443 return -EIO;
444 }
445
446 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200447 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +0200448 /* there's nothing much to do here.
449 * the bit error is on the ECC itself.
450 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200451 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +0200452 " Nothing to correct\n");
453 return 0;
454 }
455
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200456 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +0200457 " (word offset in the page :"
458 " 0x%x bit offset : 0x%x)\n",
459 ecc_word, ecc_bit);
460 /* correct the error */
461 if (nand_chip->options & NAND_BUSWIDTH_16) {
462 /* 16 bits words */
463 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
464 } else {
465 /* 8 bits words */
466 dat[ecc_word] ^= (1 << ecc_bit);
467 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200468 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +0200469 return 1;
470}
471
472/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700473 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +0200474 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700475static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
476{
477 if (cpu_is_at32ap7000()) {
478 struct nand_chip *nand_chip = mtd->priv;
479 struct atmel_nand_host *host = nand_chip->priv;
480 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
481 }
482}
Richard Genoud77f54922008-04-23 19:51:14 +0200483
Andrew Victor42cb1402006-10-19 18:24:35 +0200484/*
485 * Probe for the NAND device.
486 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200487static int __init atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +0200488{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200489 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +0200490 struct mtd_info *mtd;
491 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +0200492 struct resource *regs;
493 struct resource *mem;
Andrew Victor42cb1402006-10-19 18:24:35 +0200494 int res;
Andrew Victor42cb1402006-10-19 18:24:35 +0200495
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200496 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
497 if (!mem) {
498 printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
499 return -ENXIO;
500 }
501
Andrew Victor42cb1402006-10-19 18:24:35 +0200502 /* Allocate memory for the device structure (and zero it) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200503 host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
Andrew Victor42cb1402006-10-19 18:24:35 +0200504 if (!host) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200505 printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +0200506 return -ENOMEM;
507 }
508
Hong Xucbc6c5e2011-01-18 14:36:05 +0800509 host->io_phys = (dma_addr_t)mem->start;
510
Joe Perches28f65c112011-06-09 09:13:32 -0700511 host->io_base = ioremap(mem->start, resource_size(mem));
Andrew Victor42cb1402006-10-19 18:24:35 +0200512 if (host->io_base == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200513 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200514 res = -EIO;
515 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +0200516 }
517
518 mtd = &host->mtd;
519 nand_chip = &host->nand_chip;
520 host->board = pdev->dev.platform_data;
Richard Genoud77f54922008-04-23 19:51:14 +0200521 host->dev = &pdev->dev;
Andrew Victor42cb1402006-10-19 18:24:35 +0200522
523 nand_chip->priv = host; /* link the private data structures */
524 mtd->priv = nand_chip;
525 mtd->owner = THIS_MODULE;
526
527 /* Set address of NAND IO lines */
528 nand_chip->IO_ADDR_R = host->io_base;
529 nand_chip->IO_ADDR_W = host->io_base;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200530 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Ivan Kutena4265f82007-05-24 14:35:58 +0300531
Jean-Christophe PLAGNIOL-VILLARD1d6dc062011-09-19 15:31:37 +0800532 if (gpio_is_valid(host->board->rdy_pin))
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200533 nand_chip->dev_ready = atmel_nand_device_ready;
Ivan Kutena4265f82007-05-24 14:35:58 +0300534
Richard Genoud77f54922008-04-23 19:51:14 +0200535 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
536 if (!regs && hard_ecc) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200537 printk(KERN_ERR "atmel_nand: can't get I/O resource "
Richard Genoud77f54922008-04-23 19:51:14 +0200538 "regs\nFalling back on software ECC\n");
539 }
540
Andrew Victor42cb1402006-10-19 18:24:35 +0200541 nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
Richard Genoud77f54922008-04-23 19:51:14 +0200542 if (no_ecc)
543 nand_chip->ecc.mode = NAND_ECC_NONE;
544 if (hard_ecc && regs) {
Joe Perches28f65c112011-06-09 09:13:32 -0700545 host->ecc = ioremap(regs->start, resource_size(regs));
Richard Genoud77f54922008-04-23 19:51:14 +0200546 if (host->ecc == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200547 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Richard Genoud77f54922008-04-23 19:51:14 +0200548 res = -EIO;
549 goto err_ecc_ioremap;
550 }
Richard Genoud3fc23892008-10-12 08:42:28 +0200551 nand_chip->ecc.mode = NAND_ECC_HW;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200552 nand_chip->ecc.calculate = atmel_nand_calculate;
553 nand_chip->ecc.correct = atmel_nand_correct;
554 nand_chip->ecc.hwctl = atmel_nand_hwctl;
555 nand_chip->ecc.read_page = atmel_nand_read_page;
Richard Genoud77f54922008-04-23 19:51:14 +0200556 nand_chip->ecc.bytes = 4;
Richard Genoud77f54922008-04-23 19:51:14 +0200557 }
558
Andrew Victor42cb1402006-10-19 18:24:35 +0200559 nand_chip->chip_delay = 20; /* 20us command delay time */
560
Hong Xucbc6c5e2011-01-18 14:36:05 +0800561 if (host->board->bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +0200562 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800563
564 nand_chip->read_buf = atmel_read_buf;
565 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +0200566
Andrew Victor42cb1402006-10-19 18:24:35 +0200567 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200568 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200569
Jean-Christophe PLAGNIOL-VILLARD1d6dc062011-09-19 15:31:37 +0800570 if (gpio_is_valid(host->board->det_pin)) {
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200571 if (gpio_get_value(host->board->det_pin)) {
Simon Polettef4fa697c2009-05-27 18:19:39 +0300572 printk(KERN_INFO "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +0100573 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200574 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +0200575 }
576 }
577
Simon Polettef4fa697c2009-05-27 18:19:39 +0300578 if (on_flash_bbt) {
579 printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
Brian Norrisbb9ebd42011-05-31 16:31:23 -0700580 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
Simon Polettef4fa697c2009-05-27 18:19:39 +0300581 }
582
Hong Xucb457a42011-03-30 16:26:41 +0800583 if (!cpu_has_dma())
584 use_dma = 0;
585
586 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +0800587 dma_cap_mask_t mask;
588
589 dma_cap_zero(mask);
590 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab532011-06-29 18:41:16 +0200591 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800592 if (!host->dma_chan) {
593 dev_err(host->dev, "Failed to request DMA channel\n");
594 use_dma = 0;
595 }
596 }
597 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +0800598 dev_info(host->dev, "Using %s for DMA transfers.\n",
599 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +0800600 else
601 dev_info(host->dev, "No DMA support for NAND access.\n");
602
Richard Genoud77f54922008-04-23 19:51:14 +0200603 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +0000604 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +0200605 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200606 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +0200607 }
608
Richard Genoud3fc23892008-10-12 08:42:28 +0200609 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Richard Genoud77f54922008-04-23 19:51:14 +0200610 /* ECC is calculated for the whole page (1 step) */
611 nand_chip->ecc.size = mtd->writesize;
612
613 /* set ECC page size and oob layout */
614 switch (mtd->writesize) {
615 case 512:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200616 nand_chip->ecc.layout = &atmel_oobinfo_small;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200617 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
Richard Genoud77f54922008-04-23 19:51:14 +0200618 break;
619 case 1024:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200620 nand_chip->ecc.layout = &atmel_oobinfo_large;
621 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
Richard Genoud77f54922008-04-23 19:51:14 +0200622 break;
623 case 2048:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200624 nand_chip->ecc.layout = &atmel_oobinfo_large;
625 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
Richard Genoud77f54922008-04-23 19:51:14 +0200626 break;
627 case 4096:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200628 nand_chip->ecc.layout = &atmel_oobinfo_large;
629 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
Richard Genoud77f54922008-04-23 19:51:14 +0200630 break;
631 default:
632 /* page size not handled by HW ECC */
633 /* switching back to soft ECC */
634 nand_chip->ecc.mode = NAND_ECC_SOFT;
635 nand_chip->ecc.calculate = NULL;
636 nand_chip->ecc.correct = NULL;
637 nand_chip->ecc.hwctl = NULL;
638 nand_chip->ecc.read_page = NULL;
639 nand_chip->ecc.postpad = 0;
640 nand_chip->ecc.prepad = 0;
641 nand_chip->ecc.bytes = 0;
642 break;
643 }
644 }
645
646 /* second phase scan */
647 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +0200648 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200649 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +0200650 }
651
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200652 mtd->name = "atmel_nand";
Dmitry Eremin-Solenikovef5d79f2011-06-02 18:00:23 +0400653 res = mtd_device_parse_register(mtd, NULL, 0,
654 host->board->parts, host->board->num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +0200655 if (!res)
656 return res;
657
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200658err_scan_tail:
659err_scan_ident:
660err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200661 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200662 platform_set_drvdata(pdev, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800663 if (host->dma_chan)
664 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200665 if (host->ecc)
666 iounmap(host->ecc);
667err_ecc_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +0200668 iounmap(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200669err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +0200670 kfree(host);
671 return res;
672}
673
674/*
675 * Remove a NAND device.
676 */
David Brownell23a346c2008-07-03 23:40:16 -0700677static int __exit atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +0200678{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200679 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +0200680 struct mtd_info *mtd = &host->mtd;
681
682 nand_release(mtd);
683
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200684 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200685
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200686 if (host->ecc)
687 iounmap(host->ecc);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800688
689 if (host->dma_chan)
690 dma_release_channel(host->dma_chan);
691
Andrew Victor42cb1402006-10-19 18:24:35 +0200692 iounmap(host->io_base);
693 kfree(host);
694
695 return 0;
696}
697
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200698static struct platform_driver atmel_nand_driver = {
David Brownell23a346c2008-07-03 23:40:16 -0700699 .remove = __exit_p(atmel_nand_remove),
Andrew Victor42cb1402006-10-19 18:24:35 +0200700 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200701 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +0200702 .owner = THIS_MODULE,
703 },
704};
705
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200706static int __init atmel_nand_init(void)
Andrew Victor42cb1402006-10-19 18:24:35 +0200707{
David Brownell23a346c2008-07-03 23:40:16 -0700708 return platform_driver_probe(&atmel_nand_driver, atmel_nand_probe);
Andrew Victor42cb1402006-10-19 18:24:35 +0200709}
710
711
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200712static void __exit atmel_nand_exit(void)
Andrew Victor42cb1402006-10-19 18:24:35 +0200713{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200714 platform_driver_unregister(&atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +0200715}
716
717
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200718module_init(atmel_nand_init);
719module_exit(atmel_nand_exit);
Andrew Victor42cb1402006-10-19 18:24:35 +0200720
721MODULE_LICENSE("GPL");
722MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +0200723MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200724MODULE_ALIAS("platform:atmel_nand");