blob: e9fdbe46b247209dccb9ef9e19600feded555fe0 [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
25#include <linux/slab.h>
26#include <linux/module.h>
Simon Polettef4fa6972009-05-27 18:19:39 +030027#include <linux/moduleparam.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020028#include <linux/platform_device.h>
29#include <linux/mtd/mtd.h>
30#include <linux/mtd/nand.h>
31#include <linux/mtd/partitions.h>
32
David Woodhouse90574d02008-06-07 08:49:00 +010033#include <linux/gpio.h>
34#include <linux/io.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020035
Russell Kinga09e64f2008-08-05 16:14:15 +010036#include <mach/board.h>
37#include <mach/cpu.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020038
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020039#ifdef CONFIG_MTD_NAND_ATMEL_ECC_HW
Richard Genoud77f54922008-04-23 19:51:14 +020040#define hard_ecc 1
41#else
42#define hard_ecc 0
43#endif
44
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020045#ifdef CONFIG_MTD_NAND_ATMEL_ECC_NONE
Richard Genoud77f54922008-04-23 19:51:14 +020046#define no_ecc 1
47#else
48#define no_ecc 0
49#endif
50
Hong Xucbc6c5e2011-01-18 14:36:05 +080051static int use_dma = 1;
52module_param(use_dma, int, 0);
53
Simon Polettef4fa6972009-05-27 18:19:39 +030054static int on_flash_bbt = 0;
55module_param(on_flash_bbt, int, 0);
56
Richard Genoud77f54922008-04-23 19:51:14 +020057/* Register access macros */
58#define ecc_readl(add, reg) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020059 __raw_readl(add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020060#define ecc_writel(add, reg, value) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020061 __raw_writel((value), add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020062
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020063#include "atmel_nand_ecc.h" /* Hardware ECC registers */
Richard Genoud77f54922008-04-23 19:51:14 +020064
65/* oob layout for large page size
66 * bad block info is on bytes 0 and 1
67 * the bytes have to be consecutives to avoid
68 * several NAND_CMD_RNDOUT during read
69 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020070static struct nand_ecclayout atmel_oobinfo_large = {
Richard Genoud77f54922008-04-23 19:51:14 +020071 .eccbytes = 4,
72 .eccpos = {60, 61, 62, 63},
73 .oobfree = {
74 {2, 58}
75 },
76};
77
78/* oob layout for small page size
79 * bad block info is on bytes 4 and 5
80 * the bytes have to be consecutives to avoid
81 * several NAND_CMD_RNDOUT during read
82 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020083static struct nand_ecclayout atmel_oobinfo_small = {
Richard Genoud77f54922008-04-23 19:51:14 +020084 .eccbytes = 4,
85 .eccpos = {0, 1, 2, 3},
86 .oobfree = {
87 {6, 10}
88 },
89};
90
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020091struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +020092 struct nand_chip nand_chip;
93 struct mtd_info mtd;
94 void __iomem *io_base;
Hong Xucbc6c5e2011-01-18 14:36:05 +080095 dma_addr_t io_phys;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020096 struct atmel_nand_data *board;
Richard Genoud77f54922008-04-23 19:51:14 +020097 struct device *dev;
98 void __iomem *ecc;
Hong Xucbc6c5e2011-01-18 14:36:05 +080099
100 struct completion comp;
101 struct dma_chan *dma_chan;
Andrew Victor42cb1402006-10-19 18:24:35 +0200102};
103
Hong Xucbc6c5e2011-01-18 14:36:05 +0800104static int cpu_has_dma(void)
105{
106 return cpu_is_at91sam9rl() || cpu_is_at91sam9g45();
107}
108
Andrew Victor42cb1402006-10-19 18:24:35 +0200109/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900110 * Enable NAND.
111 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200112static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900113{
114 if (host->board->enable_pin)
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200115 gpio_set_value(host->board->enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900116}
117
118/*
119 * Disable NAND.
120 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200121static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900122{
123 if (host->board->enable_pin)
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200124 gpio_set_value(host->board->enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900125}
126
127/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200128 * Hardware specific access to control-lines
129 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200130static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200131{
132 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200133 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200134
Atsushi Nemoto81365082008-04-27 01:51:12 +0900135 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900136 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200137 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900138 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200139 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900140 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200141 if (cmd == NAND_CMD_NONE)
142 return;
143
144 if (ctrl & NAND_CLE)
145 writeb(cmd, host->io_base + (1 << host->board->cle));
146 else
147 writeb(cmd, host->io_base + (1 << host->board->ale));
148}
149
150/*
151 * Read the Device Ready pin.
152 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200153static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200154{
155 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200156 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200157
Gregory CLEMENT744f6592009-02-16 21:21:47 +0100158 return gpio_get_value(host->board->rdy_pin) ^
159 !!host->board->rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200160}
161
162/*
David Brownell23a346c2008-07-03 23:40:16 -0700163 * Minimal-overhead PIO for data access.
164 */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800165static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
David Brownell23a346c2008-07-03 23:40:16 -0700166{
167 struct nand_chip *nand_chip = mtd->priv;
168
169 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
170}
171
172static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
173{
174 struct nand_chip *nand_chip = mtd->priv;
175
176 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
177}
178
Hong Xucbc6c5e2011-01-18 14:36:05 +0800179static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
David Brownell23a346c2008-07-03 23:40:16 -0700180{
181 struct nand_chip *nand_chip = mtd->priv;
182
183 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
184}
185
186static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
187{
188 struct nand_chip *nand_chip = mtd->priv;
189
190 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
191}
192
Hong Xucbc6c5e2011-01-18 14:36:05 +0800193static void dma_complete_func(void *completion)
194{
195 complete(completion);
196}
197
198static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
199 int is_read)
200{
201 struct dma_device *dma_dev;
202 enum dma_ctrl_flags flags;
203 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
204 struct dma_async_tx_descriptor *tx = NULL;
205 dma_cookie_t cookie;
206 struct nand_chip *chip = mtd->priv;
207 struct atmel_nand_host *host = chip->priv;
208 void *p = buf;
209 int err = -EIO;
210 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
211
212 if (buf >= high_memory) {
213 struct page *pg;
214
215 if (((size_t)buf & PAGE_MASK) !=
216 ((size_t)(buf + len - 1) & PAGE_MASK)) {
217 dev_warn(host->dev, "Buffer not fit in one page\n");
218 goto err_buf;
219 }
220
221 pg = vmalloc_to_page(buf);
222 if (pg == 0) {
223 dev_err(host->dev, "Failed to vmalloc_to_page\n");
224 goto err_buf;
225 }
226 p = page_address(pg) + ((size_t)buf & ~PAGE_MASK);
227 }
228
229 dma_dev = host->dma_chan->device;
230
231 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_SRC_UNMAP |
232 DMA_COMPL_SKIP_DEST_UNMAP;
233
234 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
235 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
236 dev_err(host->dev, "Failed to dma_map_single\n");
237 goto err_buf;
238 }
239
240 if (is_read) {
241 dma_src_addr = host->io_phys;
242 dma_dst_addr = phys_addr;
243 } else {
244 dma_src_addr = phys_addr;
245 dma_dst_addr = host->io_phys;
246 }
247
248 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
249 dma_src_addr, len, flags);
250 if (!tx) {
251 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
252 goto err_dma;
253 }
254
255 init_completion(&host->comp);
256 tx->callback = dma_complete_func;
257 tx->callback_param = &host->comp;
258
259 cookie = tx->tx_submit(tx);
260 if (dma_submit_error(cookie)) {
261 dev_err(host->dev, "Failed to do DMA tx_submit\n");
262 goto err_dma;
263 }
264
265 dma_async_issue_pending(host->dma_chan);
266 wait_for_completion(&host->comp);
267
268 err = 0;
269
270err_dma:
271 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
272err_buf:
273 if (err != 0)
274 dev_warn(host->dev, "Fall back to CPU I/O\n");
275 return err;
276}
277
278static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
279{
280 struct nand_chip *chip = mtd->priv;
281 struct atmel_nand_host *host = chip->priv;
282
Nicolas Ferre9d515672011-04-01 16:40:44 +0200283 if (use_dma && len > mtd->oobsize)
284 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800285 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
286 return;
287
288 if (host->board->bus_width_16)
289 atmel_read_buf16(mtd, buf, len);
290 else
291 atmel_read_buf8(mtd, buf, len);
292}
293
294static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
295{
296 struct nand_chip *chip = mtd->priv;
297 struct atmel_nand_host *host = chip->priv;
298
Nicolas Ferre9d515672011-04-01 16:40:44 +0200299 if (use_dma && len > mtd->oobsize)
300 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800301 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
302 return;
303
304 if (host->board->bus_width_16)
305 atmel_write_buf16(mtd, buf, len);
306 else
307 atmel_write_buf8(mtd, buf, len);
308}
309
David Brownell23a346c2008-07-03 23:40:16 -0700310/*
Richard Genoud77f54922008-04-23 19:51:14 +0200311 * Calculate HW ECC
312 *
313 * function called after a write
314 *
315 * mtd: MTD block structure
316 * dat: raw data (unused)
317 * ecc_code: buffer for ECC
318 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200319static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +0200320 const u_char *dat, unsigned char *ecc_code)
321{
322 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200323 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +0200324 unsigned int ecc_value;
325
326 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +0200327 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +0200328
Richard Genoud3fc23892008-10-12 08:42:28 +0200329 ecc_code[0] = ecc_value & 0xFF;
330 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +0200331
332 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200333 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +0200334
Richard Genoud3fc23892008-10-12 08:42:28 +0200335 ecc_code[2] = ecc_value & 0xFF;
336 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +0200337
338 return 0;
339}
340
341/*
342 * HW ECC read page function
343 *
344 * mtd: mtd info structure
345 * chip: nand chip info structure
346 * buf: buffer to store read data
347 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200348static int atmel_nand_read_page(struct mtd_info *mtd,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700349 struct nand_chip *chip, uint8_t *buf, int page)
Richard Genoud77f54922008-04-23 19:51:14 +0200350{
351 int eccsize = chip->ecc.size;
352 int eccbytes = chip->ecc.bytes;
353 uint32_t *eccpos = chip->ecc.layout->eccpos;
354 uint8_t *p = buf;
355 uint8_t *oob = chip->oob_poi;
356 uint8_t *ecc_pos;
357 int stat;
358
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700359 /*
360 * Errata: ALE is incorrectly wired up to the ECC controller
361 * on the AP7000, so it will include the address cycles in the
362 * ECC calculation.
363 *
364 * Workaround: Reset the parity registers before reading the
365 * actual data.
366 */
367 if (cpu_is_at32ap7000()) {
368 struct atmel_nand_host *host = chip->priv;
369 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
370 }
371
Richard Genoud77f54922008-04-23 19:51:14 +0200372 /* read the page */
373 chip->read_buf(mtd, p, eccsize);
374
375 /* move to ECC position if needed */
376 if (eccpos[0] != 0) {
377 /* This only works on large pages
378 * because the ECC controller waits for
379 * NAND_CMD_RNDOUTSTART after the
380 * NAND_CMD_RNDOUT.
381 * anyway, for small pages, the eccpos[0] == 0
382 */
383 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
384 mtd->writesize + eccpos[0], -1);
385 }
386
387 /* the ECC controller needs to read the ECC just after the data */
388 ecc_pos = oob + eccpos[0];
389 chip->read_buf(mtd, ecc_pos, eccbytes);
390
391 /* check if there's an error */
392 stat = chip->ecc.correct(mtd, p, oob, NULL);
393
394 if (stat < 0)
395 mtd->ecc_stats.failed++;
396 else
397 mtd->ecc_stats.corrected += stat;
398
399 /* get back to oob start (end of page) */
400 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
401
402 /* read the oob */
403 chip->read_buf(mtd, oob, mtd->oobsize);
404
405 return 0;
406}
407
408/*
409 * HW ECC Correction
410 *
411 * function called after a read
412 *
413 * mtd: MTD block structure
414 * dat: raw data read from the chip
415 * read_ecc: ECC from the chip (unused)
416 * isnull: unused
417 *
418 * Detect and correct a 1 bit error for a page
419 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200420static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +0200421 u_char *read_ecc, u_char *isnull)
422{
423 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200424 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +0200425 unsigned int ecc_status;
426 unsigned int ecc_word, ecc_bit;
427
428 /* get the status from the Status Register */
429 ecc_status = ecc_readl(host->ecc, SR);
430
431 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200432 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +0200433 return 0;
434
435 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200436 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +0200437 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200438 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +0200439 ecc_word >>= 4;
440
441 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200442 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +0200443 /* check if it is a freshly erased block
444 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200445 if ((ecc_bit == ATMEL_ECC_BITADDR)
446 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +0200447 /* the block has just been erased, return OK */
448 return 0;
449 }
450 /* it doesn't seems to be a freshly
451 * erased block.
452 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200453 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +0200454 " Unable to correct.\n");
455 return -EIO;
456 }
457
458 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200459 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +0200460 /* there's nothing much to do here.
461 * the bit error is on the ECC itself.
462 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200463 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +0200464 " Nothing to correct\n");
465 return 0;
466 }
467
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200468 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +0200469 " (word offset in the page :"
470 " 0x%x bit offset : 0x%x)\n",
471 ecc_word, ecc_bit);
472 /* correct the error */
473 if (nand_chip->options & NAND_BUSWIDTH_16) {
474 /* 16 bits words */
475 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
476 } else {
477 /* 8 bits words */
478 dat[ecc_word] ^= (1 << ecc_bit);
479 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200480 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +0200481 return 1;
482}
483
484/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700485 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +0200486 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700487static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
488{
489 if (cpu_is_at32ap7000()) {
490 struct nand_chip *nand_chip = mtd->priv;
491 struct atmel_nand_host *host = nand_chip->priv;
492 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
493 }
494}
Richard Genoud77f54922008-04-23 19:51:14 +0200495
Andreas Bießmann9a9745c2010-08-05 12:38:41 +0200496#ifdef CONFIG_MTD_CMDLINE_PARTS
Atsushi Nemoto52f83012008-03-30 21:59:37 +0900497static const char *part_probes[] = { "cmdlinepart", NULL };
Andrew Victor693ef662007-05-03 08:16:44 +0200498#endif
499
Andrew Victor42cb1402006-10-19 18:24:35 +0200500/*
501 * Probe for the NAND device.
502 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200503static int __init atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +0200504{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200505 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +0200506 struct mtd_info *mtd;
507 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +0200508 struct resource *regs;
509 struct resource *mem;
Andrew Victor42cb1402006-10-19 18:24:35 +0200510 int res;
511
512#ifdef CONFIG_MTD_PARTITIONS
513 struct mtd_partition *partitions = NULL;
514 int num_partitions = 0;
515#endif
516
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200517 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
518 if (!mem) {
519 printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
520 return -ENXIO;
521 }
522
Andrew Victor42cb1402006-10-19 18:24:35 +0200523 /* Allocate memory for the device structure (and zero it) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200524 host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
Andrew Victor42cb1402006-10-19 18:24:35 +0200525 if (!host) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200526 printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +0200527 return -ENOMEM;
528 }
529
Hong Xucbc6c5e2011-01-18 14:36:05 +0800530 host->io_phys = (dma_addr_t)mem->start;
531
Richard Genoud77f54922008-04-23 19:51:14 +0200532 host->io_base = ioremap(mem->start, mem->end - mem->start + 1);
Andrew Victor42cb1402006-10-19 18:24:35 +0200533 if (host->io_base == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200534 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200535 res = -EIO;
536 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +0200537 }
538
539 mtd = &host->mtd;
540 nand_chip = &host->nand_chip;
541 host->board = pdev->dev.platform_data;
Richard Genoud77f54922008-04-23 19:51:14 +0200542 host->dev = &pdev->dev;
Andrew Victor42cb1402006-10-19 18:24:35 +0200543
544 nand_chip->priv = host; /* link the private data structures */
545 mtd->priv = nand_chip;
546 mtd->owner = THIS_MODULE;
547
548 /* Set address of NAND IO lines */
549 nand_chip->IO_ADDR_R = host->io_base;
550 nand_chip->IO_ADDR_W = host->io_base;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200551 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Ivan Kutena4265f82007-05-24 14:35:58 +0300552
553 if (host->board->rdy_pin)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200554 nand_chip->dev_ready = atmel_nand_device_ready;
Ivan Kutena4265f82007-05-24 14:35:58 +0300555
Richard Genoud77f54922008-04-23 19:51:14 +0200556 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
557 if (!regs && hard_ecc) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200558 printk(KERN_ERR "atmel_nand: can't get I/O resource "
Richard Genoud77f54922008-04-23 19:51:14 +0200559 "regs\nFalling back on software ECC\n");
560 }
561
Andrew Victor42cb1402006-10-19 18:24:35 +0200562 nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
Richard Genoud77f54922008-04-23 19:51:14 +0200563 if (no_ecc)
564 nand_chip->ecc.mode = NAND_ECC_NONE;
565 if (hard_ecc && regs) {
566 host->ecc = ioremap(regs->start, regs->end - regs->start + 1);
567 if (host->ecc == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200568 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Richard Genoud77f54922008-04-23 19:51:14 +0200569 res = -EIO;
570 goto err_ecc_ioremap;
571 }
Richard Genoud3fc23892008-10-12 08:42:28 +0200572 nand_chip->ecc.mode = NAND_ECC_HW;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200573 nand_chip->ecc.calculate = atmel_nand_calculate;
574 nand_chip->ecc.correct = atmel_nand_correct;
575 nand_chip->ecc.hwctl = atmel_nand_hwctl;
576 nand_chip->ecc.read_page = atmel_nand_read_page;
Richard Genoud77f54922008-04-23 19:51:14 +0200577 nand_chip->ecc.bytes = 4;
Richard Genoud77f54922008-04-23 19:51:14 +0200578 }
579
Andrew Victor42cb1402006-10-19 18:24:35 +0200580 nand_chip->chip_delay = 20; /* 20us command delay time */
581
Hong Xucbc6c5e2011-01-18 14:36:05 +0800582 if (host->board->bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +0200583 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800584
585 nand_chip->read_buf = atmel_read_buf;
586 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +0200587
Andrew Victor42cb1402006-10-19 18:24:35 +0200588 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200589 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200590
591 if (host->board->det_pin) {
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200592 if (gpio_get_value(host->board->det_pin)) {
Simon Polettef4fa6972009-05-27 18:19:39 +0300593 printk(KERN_INFO "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +0100594 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200595 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +0200596 }
597 }
598
Simon Polettef4fa6972009-05-27 18:19:39 +0300599 if (on_flash_bbt) {
600 printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
601 nand_chip->options |= NAND_USE_FLASH_BBT;
602 }
603
Hong Xucb457a42011-03-30 16:26:41 +0800604 if (!cpu_has_dma())
605 use_dma = 0;
606
607 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +0800608 dma_cap_mask_t mask;
609
610 dma_cap_zero(mask);
611 dma_cap_set(DMA_MEMCPY, mask);
612 host->dma_chan = dma_request_channel(mask, 0, NULL);
613 if (!host->dma_chan) {
614 dev_err(host->dev, "Failed to request DMA channel\n");
615 use_dma = 0;
616 }
617 }
618 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +0800619 dev_info(host->dev, "Using %s for DMA transfers.\n",
620 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +0800621 else
622 dev_info(host->dev, "No DMA support for NAND access.\n");
623
Richard Genoud77f54922008-04-23 19:51:14 +0200624 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +0000625 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +0200626 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200627 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +0200628 }
629
Richard Genoud3fc23892008-10-12 08:42:28 +0200630 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Richard Genoud77f54922008-04-23 19:51:14 +0200631 /* ECC is calculated for the whole page (1 step) */
632 nand_chip->ecc.size = mtd->writesize;
633
634 /* set ECC page size and oob layout */
635 switch (mtd->writesize) {
636 case 512:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200637 nand_chip->ecc.layout = &atmel_oobinfo_small;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200638 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
Richard Genoud77f54922008-04-23 19:51:14 +0200639 break;
640 case 1024:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200641 nand_chip->ecc.layout = &atmel_oobinfo_large;
642 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
Richard Genoud77f54922008-04-23 19:51:14 +0200643 break;
644 case 2048:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200645 nand_chip->ecc.layout = &atmel_oobinfo_large;
646 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
Richard Genoud77f54922008-04-23 19:51:14 +0200647 break;
648 case 4096:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200649 nand_chip->ecc.layout = &atmel_oobinfo_large;
650 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
Richard Genoud77f54922008-04-23 19:51:14 +0200651 break;
652 default:
653 /* page size not handled by HW ECC */
654 /* switching back to soft ECC */
655 nand_chip->ecc.mode = NAND_ECC_SOFT;
656 nand_chip->ecc.calculate = NULL;
657 nand_chip->ecc.correct = NULL;
658 nand_chip->ecc.hwctl = NULL;
659 nand_chip->ecc.read_page = NULL;
660 nand_chip->ecc.postpad = 0;
661 nand_chip->ecc.prepad = 0;
662 nand_chip->ecc.bytes = 0;
663 break;
664 }
665 }
666
667 /* second phase scan */
668 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +0200669 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200670 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +0200671 }
672
673#ifdef CONFIG_MTD_PARTITIONS
Andrew Victor693ef662007-05-03 08:16:44 +0200674#ifdef CONFIG_MTD_CMDLINE_PARTS
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200675 mtd->name = "atmel_nand";
Atsushi Nemoto842b1a102008-01-29 22:28:22 +0900676 num_partitions = parse_mtd_partitions(mtd, part_probes,
677 &partitions, 0);
Andrew Victor693ef662007-05-03 08:16:44 +0200678#endif
Atsushi Nemoto842b1a102008-01-29 22:28:22 +0900679 if (num_partitions <= 0 && host->board->partition_info)
680 partitions = host->board->partition_info(mtd->size,
681 &num_partitions);
Andrew Victor42cb1402006-10-19 18:24:35 +0200682
683 if ((!partitions) || (num_partitions == 0)) {
Thadeu Lima de Souza Cascardoae27a7a2009-06-24 18:40:46 -0300684 printk(KERN_ERR "atmel_nand: No partitions defined, or unsupported device.\n");
Roel Kluin895fb492009-11-11 21:47:06 +0100685 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200686 goto err_no_partitions;
Andrew Victor42cb1402006-10-19 18:24:35 +0200687 }
688
689 res = add_mtd_partitions(mtd, partitions, num_partitions);
690#else
691 res = add_mtd_device(mtd);
692#endif
693
694 if (!res)
695 return res;
696
Richard Genoud77f54922008-04-23 19:51:14 +0200697#ifdef CONFIG_MTD_PARTITIONS
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200698err_no_partitions:
Richard Genoud77f54922008-04-23 19:51:14 +0200699#endif
Andrew Victor42cb1402006-10-19 18:24:35 +0200700 nand_release(mtd);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200701err_scan_tail:
702err_scan_ident:
703err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200704 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200705 platform_set_drvdata(pdev, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800706 if (host->dma_chan)
707 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200708 if (host->ecc)
709 iounmap(host->ecc);
710err_ecc_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +0200711 iounmap(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200712err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +0200713 kfree(host);
714 return res;
715}
716
717/*
718 * Remove a NAND device.
719 */
David Brownell23a346c2008-07-03 23:40:16 -0700720static int __exit atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +0200721{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200722 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +0200723 struct mtd_info *mtd = &host->mtd;
724
725 nand_release(mtd);
726
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200727 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200728
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200729 if (host->ecc)
730 iounmap(host->ecc);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800731
732 if (host->dma_chan)
733 dma_release_channel(host->dma_chan);
734
Andrew Victor42cb1402006-10-19 18:24:35 +0200735 iounmap(host->io_base);
736 kfree(host);
737
738 return 0;
739}
740
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200741static struct platform_driver atmel_nand_driver = {
David Brownell23a346c2008-07-03 23:40:16 -0700742 .remove = __exit_p(atmel_nand_remove),
Andrew Victor42cb1402006-10-19 18:24:35 +0200743 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200744 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +0200745 .owner = THIS_MODULE,
746 },
747};
748
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200749static int __init atmel_nand_init(void)
Andrew Victor42cb1402006-10-19 18:24:35 +0200750{
David Brownell23a346c2008-07-03 23:40:16 -0700751 return platform_driver_probe(&atmel_nand_driver, atmel_nand_probe);
Andrew Victor42cb1402006-10-19 18:24:35 +0200752}
753
754
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200755static void __exit atmel_nand_exit(void)
Andrew Victor42cb1402006-10-19 18:24:35 +0200756{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200757 platform_driver_unregister(&atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +0200758}
759
760
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200761module_init(atmel_nand_init);
762module_exit(atmel_nand_exit);
Andrew Victor42cb1402006-10-19 18:24:35 +0200763
764MODULE_LICENSE("GPL");
765MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +0200766MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200767MODULE_ALIAS("platform:atmel_nand");