blob: ae7e37d9ac172fa411d99aacbb4c3b196cdc5991 [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>
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +080030#include <linux/of.h>
31#include <linux/of_device.h>
32#include <linux/of_gpio.h>
33#include <linux/of_mtd.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020034#include <linux/mtd/mtd.h>
35#include <linux/mtd/nand.h>
36#include <linux/mtd/partitions.h>
37
Hans-Christian Egtvedt5c39c4c2011-04-13 15:55:17 +020038#include <linux/dmaengine.h>
David Woodhouse90574d02008-06-07 08:49:00 +010039#include <linux/gpio.h>
40#include <linux/io.h>
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +080041#include <linux/platform_data/atmel.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020042
Russell Kinga09e64f2008-08-05 16:14:15 +010043#include <mach/cpu.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020044
Hong Xucbc6c5e2011-01-18 14:36:05 +080045static int use_dma = 1;
46module_param(use_dma, int, 0);
47
Simon Polettef4fa697c2009-05-27 18:19:39 +030048static int on_flash_bbt = 0;
49module_param(on_flash_bbt, int, 0);
50
Richard Genoud77f54922008-04-23 19:51:14 +020051/* Register access macros */
52#define ecc_readl(add, reg) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020053 __raw_readl(add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020054#define ecc_writel(add, reg, value) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020055 __raw_writel((value), add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020056
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020057#include "atmel_nand_ecc.h" /* Hardware ECC registers */
Richard Genoud77f54922008-04-23 19:51:14 +020058
59/* oob layout for large page size
60 * bad block info is on bytes 0 and 1
61 * the bytes have to be consecutives to avoid
62 * several NAND_CMD_RNDOUT during read
63 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020064static struct nand_ecclayout atmel_oobinfo_large = {
Richard Genoud77f54922008-04-23 19:51:14 +020065 .eccbytes = 4,
66 .eccpos = {60, 61, 62, 63},
67 .oobfree = {
68 {2, 58}
69 },
70};
71
72/* oob layout for small page size
73 * bad block info is on bytes 4 and 5
74 * the bytes have to be consecutives to avoid
75 * several NAND_CMD_RNDOUT during read
76 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020077static struct nand_ecclayout atmel_oobinfo_small = {
Richard Genoud77f54922008-04-23 19:51:14 +020078 .eccbytes = 4,
79 .eccpos = {0, 1, 2, 3},
80 .oobfree = {
81 {6, 10}
82 },
83};
84
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020085struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +020086 struct nand_chip nand_chip;
87 struct mtd_info mtd;
88 void __iomem *io_base;
Hong Xucbc6c5e2011-01-18 14:36:05 +080089 dma_addr_t io_phys;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +080090 struct atmel_nand_data board;
Richard Genoud77f54922008-04-23 19:51:14 +020091 struct device *dev;
92 void __iomem *ecc;
Hong Xucbc6c5e2011-01-18 14:36:05 +080093
94 struct completion comp;
95 struct dma_chan *dma_chan;
Andrew Victor42cb1402006-10-19 18:24:35 +020096};
97
Hong Xucbc6c5e2011-01-18 14:36:05 +080098static int cpu_has_dma(void)
99{
100 return cpu_is_at91sam9rl() || cpu_is_at91sam9g45();
101}
102
Andrew Victor42cb1402006-10-19 18:24:35 +0200103/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900104 * Enable NAND.
105 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200106static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900107{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800108 if (gpio_is_valid(host->board.enable_pin))
109 gpio_set_value(host->board.enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900110}
111
112/*
113 * Disable NAND.
114 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200115static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900116{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800117 if (gpio_is_valid(host->board.enable_pin))
118 gpio_set_value(host->board.enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900119}
120
121/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200122 * Hardware specific access to control-lines
123 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200124static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200125{
126 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200127 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200128
Atsushi Nemoto81365082008-04-27 01:51:12 +0900129 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900130 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200131 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900132 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200133 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900134 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200135 if (cmd == NAND_CMD_NONE)
136 return;
137
138 if (ctrl & NAND_CLE)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800139 writeb(cmd, host->io_base + (1 << host->board.cle));
Andrew Victor42cb1402006-10-19 18:24:35 +0200140 else
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800141 writeb(cmd, host->io_base + (1 << host->board.ale));
Andrew Victor42cb1402006-10-19 18:24:35 +0200142}
143
144/*
145 * Read the Device Ready pin.
146 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200147static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200148{
149 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200150 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200151
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800152 return gpio_get_value(host->board.rdy_pin) ^
153 !!host->board.rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200154}
155
Artem Bityutskiy50082312012-02-02 13:54:25 +0200156/*
157 * Minimal-overhead PIO for data access.
158 */
159static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
160{
161 struct nand_chip *nand_chip = mtd->priv;
162
163 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
164}
165
166static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
167{
168 struct nand_chip *nand_chip = mtd->priv;
169
170 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
171}
172
173static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
174{
175 struct nand_chip *nand_chip = mtd->priv;
176
177 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
178}
179
180static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
181{
182 struct nand_chip *nand_chip = mtd->priv;
183
184 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
185}
186
Hong Xucbc6c5e2011-01-18 14:36:05 +0800187static void dma_complete_func(void *completion)
188{
189 complete(completion);
190}
191
192static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
193 int is_read)
194{
195 struct dma_device *dma_dev;
196 enum dma_ctrl_flags flags;
197 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
198 struct dma_async_tx_descriptor *tx = NULL;
199 dma_cookie_t cookie;
200 struct nand_chip *chip = mtd->priv;
201 struct atmel_nand_host *host = chip->priv;
202 void *p = buf;
203 int err = -EIO;
204 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
205
Hong Xu80b4f812011-03-31 18:33:15 +0800206 if (buf >= high_memory)
207 goto err_buf;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800208
209 dma_dev = host->dma_chan->device;
210
211 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_SRC_UNMAP |
212 DMA_COMPL_SKIP_DEST_UNMAP;
213
214 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
215 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
216 dev_err(host->dev, "Failed to dma_map_single\n");
217 goto err_buf;
218 }
219
220 if (is_read) {
221 dma_src_addr = host->io_phys;
222 dma_dst_addr = phys_addr;
223 } else {
224 dma_src_addr = phys_addr;
225 dma_dst_addr = host->io_phys;
226 }
227
228 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
229 dma_src_addr, len, flags);
230 if (!tx) {
231 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
232 goto err_dma;
233 }
234
235 init_completion(&host->comp);
236 tx->callback = dma_complete_func;
237 tx->callback_param = &host->comp;
238
239 cookie = tx->tx_submit(tx);
240 if (dma_submit_error(cookie)) {
241 dev_err(host->dev, "Failed to do DMA tx_submit\n");
242 goto err_dma;
243 }
244
245 dma_async_issue_pending(host->dma_chan);
246 wait_for_completion(&host->comp);
247
248 err = 0;
249
250err_dma:
251 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
252err_buf:
253 if (err != 0)
254 dev_warn(host->dev, "Fall back to CPU I/O\n");
255 return err;
256}
257
258static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
259{
260 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200261 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800262
Nicolas Ferre9d515672011-04-01 16:40:44 +0200263 if (use_dma && len > mtd->oobsize)
264 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800265 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
266 return;
267
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800268 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200269 atmel_read_buf16(mtd, buf, len);
270 else
271 atmel_read_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800272}
273
274static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
275{
276 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200277 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800278
Nicolas Ferre9d515672011-04-01 16:40:44 +0200279 if (use_dma && len > mtd->oobsize)
280 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800281 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
282 return;
283
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800284 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200285 atmel_write_buf16(mtd, buf, len);
286 else
287 atmel_write_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800288}
289
David Brownell23a346c2008-07-03 23:40:16 -0700290/*
Richard Genoud77f54922008-04-23 19:51:14 +0200291 * Calculate HW ECC
292 *
293 * function called after a write
294 *
295 * mtd: MTD block structure
296 * dat: raw data (unused)
297 * ecc_code: buffer for ECC
298 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200299static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +0200300 const u_char *dat, unsigned char *ecc_code)
301{
302 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200303 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +0200304 unsigned int ecc_value;
305
306 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +0200307 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +0200308
Richard Genoud3fc23892008-10-12 08:42:28 +0200309 ecc_code[0] = ecc_value & 0xFF;
310 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +0200311
312 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200313 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +0200314
Richard Genoud3fc23892008-10-12 08:42:28 +0200315 ecc_code[2] = ecc_value & 0xFF;
316 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +0200317
318 return 0;
319}
320
321/*
322 * HW ECC read page function
323 *
324 * mtd: mtd info structure
325 * chip: nand chip info structure
326 * buf: buffer to store read data
327 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200328static int atmel_nand_read_page(struct mtd_info *mtd,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700329 struct nand_chip *chip, uint8_t *buf, int page)
Richard Genoud77f54922008-04-23 19:51:14 +0200330{
331 int eccsize = chip->ecc.size;
332 int eccbytes = chip->ecc.bytes;
333 uint32_t *eccpos = chip->ecc.layout->eccpos;
334 uint8_t *p = buf;
335 uint8_t *oob = chip->oob_poi;
336 uint8_t *ecc_pos;
337 int stat;
338
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700339 /*
340 * Errata: ALE is incorrectly wired up to the ECC controller
341 * on the AP7000, so it will include the address cycles in the
342 * ECC calculation.
343 *
344 * Workaround: Reset the parity registers before reading the
345 * actual data.
346 */
347 if (cpu_is_at32ap7000()) {
348 struct atmel_nand_host *host = chip->priv;
349 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
350 }
351
Richard Genoud77f54922008-04-23 19:51:14 +0200352 /* read the page */
353 chip->read_buf(mtd, p, eccsize);
354
355 /* move to ECC position if needed */
356 if (eccpos[0] != 0) {
357 /* This only works on large pages
358 * because the ECC controller waits for
359 * NAND_CMD_RNDOUTSTART after the
360 * NAND_CMD_RNDOUT.
361 * anyway, for small pages, the eccpos[0] == 0
362 */
363 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
364 mtd->writesize + eccpos[0], -1);
365 }
366
367 /* the ECC controller needs to read the ECC just after the data */
368 ecc_pos = oob + eccpos[0];
369 chip->read_buf(mtd, ecc_pos, eccbytes);
370
371 /* check if there's an error */
372 stat = chip->ecc.correct(mtd, p, oob, NULL);
373
374 if (stat < 0)
375 mtd->ecc_stats.failed++;
376 else
377 mtd->ecc_stats.corrected += stat;
378
379 /* get back to oob start (end of page) */
380 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
381
382 /* read the oob */
383 chip->read_buf(mtd, oob, mtd->oobsize);
384
385 return 0;
386}
387
388/*
389 * HW ECC Correction
390 *
391 * function called after a read
392 *
393 * mtd: MTD block structure
394 * dat: raw data read from the chip
395 * read_ecc: ECC from the chip (unused)
396 * isnull: unused
397 *
398 * Detect and correct a 1 bit error for a page
399 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200400static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +0200401 u_char *read_ecc, u_char *isnull)
402{
403 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200404 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +0200405 unsigned int ecc_status;
406 unsigned int ecc_word, ecc_bit;
407
408 /* get the status from the Status Register */
409 ecc_status = ecc_readl(host->ecc, SR);
410
411 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200412 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +0200413 return 0;
414
415 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200416 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +0200417 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200418 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +0200419 ecc_word >>= 4;
420
421 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200422 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +0200423 /* check if it is a freshly erased block
424 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200425 if ((ecc_bit == ATMEL_ECC_BITADDR)
426 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +0200427 /* the block has just been erased, return OK */
428 return 0;
429 }
430 /* it doesn't seems to be a freshly
431 * erased block.
432 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200433 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +0200434 " Unable to correct.\n");
435 return -EIO;
436 }
437
438 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200439 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +0200440 /* there's nothing much to do here.
441 * the bit error is on the ECC itself.
442 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200443 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +0200444 " Nothing to correct\n");
445 return 0;
446 }
447
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200448 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +0200449 " (word offset in the page :"
450 " 0x%x bit offset : 0x%x)\n",
451 ecc_word, ecc_bit);
452 /* correct the error */
453 if (nand_chip->options & NAND_BUSWIDTH_16) {
454 /* 16 bits words */
455 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
456 } else {
457 /* 8 bits words */
458 dat[ecc_word] ^= (1 << ecc_bit);
459 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200460 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +0200461 return 1;
462}
463
464/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700465 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +0200466 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700467static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
468{
469 if (cpu_is_at32ap7000()) {
470 struct nand_chip *nand_chip = mtd->priv;
471 struct atmel_nand_host *host = nand_chip->priv;
472 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
473 }
474}
Richard Genoud77f54922008-04-23 19:51:14 +0200475
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800476#if defined(CONFIG_OF)
477static int __devinit atmel_of_init_port(struct atmel_nand_host *host,
478 struct device_node *np)
479{
480 u32 val;
481 int ecc_mode;
482 struct atmel_nand_data *board = &host->board;
483 enum of_gpio_flags flags;
484
485 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
486 if (val >= 32) {
487 dev_err(host->dev, "invalid addr-offset %u\n", val);
488 return -EINVAL;
489 }
490 board->ale = val;
491 }
492
493 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
494 if (val >= 32) {
495 dev_err(host->dev, "invalid cmd-offset %u\n", val);
496 return -EINVAL;
497 }
498 board->cle = val;
499 }
500
501 ecc_mode = of_get_nand_ecc_mode(np);
502
503 board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
504
505 board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
506
507 if (of_get_nand_bus_width(np) == 16)
508 board->bus_width_16 = 1;
509
510 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
511 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
512
513 board->enable_pin = of_get_gpio(np, 1);
514 board->det_pin = of_get_gpio(np, 2);
515
516 return 0;
517}
518#else
519static int __devinit atmel_of_init_port(struct atmel_nand_host *host,
520 struct device_node *np)
521{
522 return -EINVAL;
523}
524#endif
525
Andrew Victor42cb1402006-10-19 18:24:35 +0200526/*
527 * Probe for the NAND device.
528 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200529static int __init atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +0200530{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200531 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +0200532 struct mtd_info *mtd;
533 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +0200534 struct resource *regs;
535 struct resource *mem;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800536 struct mtd_part_parser_data ppdata = {};
Andrew Victor42cb1402006-10-19 18:24:35 +0200537 int res;
Andrew Victor42cb1402006-10-19 18:24:35 +0200538
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200539 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
540 if (!mem) {
541 printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
542 return -ENXIO;
543 }
544
Andrew Victor42cb1402006-10-19 18:24:35 +0200545 /* Allocate memory for the device structure (and zero it) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200546 host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
Andrew Victor42cb1402006-10-19 18:24:35 +0200547 if (!host) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200548 printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +0200549 return -ENOMEM;
550 }
551
Hong Xucbc6c5e2011-01-18 14:36:05 +0800552 host->io_phys = (dma_addr_t)mem->start;
553
Joe Perches28f65c112011-06-09 09:13:32 -0700554 host->io_base = ioremap(mem->start, resource_size(mem));
Andrew Victor42cb1402006-10-19 18:24:35 +0200555 if (host->io_base == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200556 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200557 res = -EIO;
558 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +0200559 }
560
561 mtd = &host->mtd;
562 nand_chip = &host->nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +0200563 host->dev = &pdev->dev;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800564 if (pdev->dev.of_node) {
565 res = atmel_of_init_port(host, pdev->dev.of_node);
566 if (res)
567 goto err_nand_ioremap;
568 } else {
569 memcpy(&host->board, pdev->dev.platform_data,
570 sizeof(struct atmel_nand_data));
571 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200572
573 nand_chip->priv = host; /* link the private data structures */
574 mtd->priv = nand_chip;
575 mtd->owner = THIS_MODULE;
576
577 /* Set address of NAND IO lines */
578 nand_chip->IO_ADDR_R = host->io_base;
579 nand_chip->IO_ADDR_W = host->io_base;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200580 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Ivan Kutena4265f82007-05-24 14:35:58 +0300581
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800582 if (gpio_is_valid(host->board.rdy_pin))
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200583 nand_chip->dev_ready = atmel_nand_device_ready;
Ivan Kutena4265f82007-05-24 14:35:58 +0300584
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800585 nand_chip->ecc.mode = host->board.ecc_mode;
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +0800586
Richard Genoud77f54922008-04-23 19:51:14 +0200587 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +0800588 if (!regs && nand_chip->ecc.mode == NAND_ECC_HW) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200589 printk(KERN_ERR "atmel_nand: can't get I/O resource "
Richard Genoud77f54922008-04-23 19:51:14 +0200590 "regs\nFalling back on software ECC\n");
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +0800591 nand_chip->ecc.mode = NAND_ECC_SOFT;
Richard Genoud77f54922008-04-23 19:51:14 +0200592 }
593
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +0800594 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Joe Perches28f65c112011-06-09 09:13:32 -0700595 host->ecc = ioremap(regs->start, resource_size(regs));
Richard Genoud77f54922008-04-23 19:51:14 +0200596 if (host->ecc == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200597 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Richard Genoud77f54922008-04-23 19:51:14 +0200598 res = -EIO;
599 goto err_ecc_ioremap;
600 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200601 nand_chip->ecc.calculate = atmel_nand_calculate;
602 nand_chip->ecc.correct = atmel_nand_correct;
603 nand_chip->ecc.hwctl = atmel_nand_hwctl;
604 nand_chip->ecc.read_page = atmel_nand_read_page;
Richard Genoud77f54922008-04-23 19:51:14 +0200605 nand_chip->ecc.bytes = 4;
Richard Genoud77f54922008-04-23 19:51:14 +0200606 }
607
Andrew Victor42cb1402006-10-19 18:24:35 +0200608 nand_chip->chip_delay = 20; /* 20us command delay time */
609
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800610 if (host->board.bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +0200611 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800612
613 nand_chip->read_buf = atmel_read_buf;
614 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +0200615
Andrew Victor42cb1402006-10-19 18:24:35 +0200616 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200617 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200618
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800619 if (gpio_is_valid(host->board.det_pin)) {
620 if (gpio_get_value(host->board.det_pin)) {
Simon Polettef4fa697c2009-05-27 18:19:39 +0300621 printk(KERN_INFO "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +0100622 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200623 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +0200624 }
625 }
626
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800627 if (host->board.on_flash_bbt || on_flash_bbt) {
Simon Polettef4fa697c2009-05-27 18:19:39 +0300628 printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
Brian Norrisbb9ebd42011-05-31 16:31:23 -0700629 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
Simon Polettef4fa697c2009-05-27 18:19:39 +0300630 }
631
Hong Xucb457a42011-03-30 16:26:41 +0800632 if (!cpu_has_dma())
633 use_dma = 0;
634
635 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +0800636 dma_cap_mask_t mask;
637
638 dma_cap_zero(mask);
639 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab532011-06-29 18:41:16 +0200640 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800641 if (!host->dma_chan) {
642 dev_err(host->dev, "Failed to request DMA channel\n");
643 use_dma = 0;
644 }
645 }
646 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +0800647 dev_info(host->dev, "Using %s for DMA transfers.\n",
648 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +0800649 else
650 dev_info(host->dev, "No DMA support for NAND access.\n");
651
Richard Genoud77f54922008-04-23 19:51:14 +0200652 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +0000653 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +0200654 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200655 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +0200656 }
657
Richard Genoud3fc23892008-10-12 08:42:28 +0200658 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Richard Genoud77f54922008-04-23 19:51:14 +0200659 /* ECC is calculated for the whole page (1 step) */
660 nand_chip->ecc.size = mtd->writesize;
661
662 /* set ECC page size and oob layout */
663 switch (mtd->writesize) {
664 case 512:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200665 nand_chip->ecc.layout = &atmel_oobinfo_small;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200666 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
Richard Genoud77f54922008-04-23 19:51:14 +0200667 break;
668 case 1024:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200669 nand_chip->ecc.layout = &atmel_oobinfo_large;
670 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
Richard Genoud77f54922008-04-23 19:51:14 +0200671 break;
672 case 2048:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200673 nand_chip->ecc.layout = &atmel_oobinfo_large;
674 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
Richard Genoud77f54922008-04-23 19:51:14 +0200675 break;
676 case 4096:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200677 nand_chip->ecc.layout = &atmel_oobinfo_large;
678 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
Richard Genoud77f54922008-04-23 19:51:14 +0200679 break;
680 default:
681 /* page size not handled by HW ECC */
682 /* switching back to soft ECC */
683 nand_chip->ecc.mode = NAND_ECC_SOFT;
684 nand_chip->ecc.calculate = NULL;
685 nand_chip->ecc.correct = NULL;
686 nand_chip->ecc.hwctl = NULL;
687 nand_chip->ecc.read_page = NULL;
688 nand_chip->ecc.postpad = 0;
689 nand_chip->ecc.prepad = 0;
690 nand_chip->ecc.bytes = 0;
691 break;
692 }
693 }
694
695 /* second phase scan */
696 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +0200697 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200698 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +0200699 }
700
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200701 mtd->name = "atmel_nand";
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800702 ppdata.of_node = pdev->dev.of_node;
703 res = mtd_device_parse_register(mtd, NULL, &ppdata,
704 host->board.parts, host->board.num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +0200705 if (!res)
706 return res;
707
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200708err_scan_tail:
709err_scan_ident:
710err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200711 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200712 platform_set_drvdata(pdev, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800713 if (host->dma_chan)
714 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200715 if (host->ecc)
716 iounmap(host->ecc);
717err_ecc_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +0200718 iounmap(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200719err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +0200720 kfree(host);
721 return res;
722}
723
724/*
725 * Remove a NAND device.
726 */
David Brownell23a346c2008-07-03 23:40:16 -0700727static int __exit atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +0200728{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200729 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +0200730 struct mtd_info *mtd = &host->mtd;
731
732 nand_release(mtd);
733
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200734 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200735
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200736 if (host->ecc)
737 iounmap(host->ecc);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800738
739 if (host->dma_chan)
740 dma_release_channel(host->dma_chan);
741
Andrew Victor42cb1402006-10-19 18:24:35 +0200742 iounmap(host->io_base);
743 kfree(host);
744
745 return 0;
746}
747
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800748#if defined(CONFIG_OF)
749static const struct of_device_id atmel_nand_dt_ids[] = {
750 { .compatible = "atmel,at91rm9200-nand" },
751 { /* sentinel */ }
752};
753
754MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
755#endif
756
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200757static struct platform_driver atmel_nand_driver = {
David Brownell23a346c2008-07-03 23:40:16 -0700758 .remove = __exit_p(atmel_nand_remove),
Andrew Victor42cb1402006-10-19 18:24:35 +0200759 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200760 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +0200761 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800762 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
Andrew Victor42cb1402006-10-19 18:24:35 +0200763 },
764};
765
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200766static int __init atmel_nand_init(void)
Andrew Victor42cb1402006-10-19 18:24:35 +0200767{
David Brownell23a346c2008-07-03 23:40:16 -0700768 return platform_driver_probe(&atmel_nand_driver, atmel_nand_probe);
Andrew Victor42cb1402006-10-19 18:24:35 +0200769}
770
771
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200772static void __exit atmel_nand_exit(void)
Andrew Victor42cb1402006-10-19 18:24:35 +0200773{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200774 platform_driver_unregister(&atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +0200775}
776
777
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200778module_init(atmel_nand_init);
779module_exit(atmel_nand_exit);
Andrew Victor42cb1402006-10-19 18:24:35 +0200780
781MODULE_LICENSE("GPL");
782MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +0200783MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200784MODULE_ALIAS("platform:atmel_nand");