blob: b300705d41cb67ce432023f003b2ee63904d59ab [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
Hans-Christian Egtvedt5c39c4c2011-04-13 15:55:17 +020033#include <linux/dmaengine.h>
David Woodhouse90574d02008-06-07 08:49:00 +010034#include <linux/gpio.h>
35#include <linux/io.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020036
Russell Kinga09e64f2008-08-05 16:14:15 +010037#include <mach/board.h>
38#include <mach/cpu.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020039
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020040#ifdef CONFIG_MTD_NAND_ATMEL_ECC_HW
Richard Genoud77f54922008-04-23 19:51:14 +020041#define hard_ecc 1
42#else
43#define hard_ecc 0
44#endif
45
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020046#ifdef CONFIG_MTD_NAND_ATMEL_ECC_NONE
Richard Genoud77f54922008-04-23 19:51:14 +020047#define no_ecc 1
48#else
49#define no_ecc 0
50#endif
51
Hong Xucbc6c5e2011-01-18 14:36:05 +080052static int use_dma = 1;
53module_param(use_dma, int, 0);
54
Simon Polettef4fa6972009-05-27 18:19:39 +030055static int on_flash_bbt = 0;
56module_param(on_flash_bbt, int, 0);
57
Richard Genoud77f54922008-04-23 19:51:14 +020058/* Register access macros */
59#define ecc_readl(add, reg) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020060 __raw_readl(add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020061#define ecc_writel(add, reg, value) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020062 __raw_writel((value), add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020063
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020064#include "atmel_nand_ecc.h" /* Hardware ECC registers */
Richard Genoud77f54922008-04-23 19:51:14 +020065
66/* oob layout for large page size
67 * bad block info is on bytes 0 and 1
68 * the bytes have to be consecutives to avoid
69 * several NAND_CMD_RNDOUT during read
70 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020071static struct nand_ecclayout atmel_oobinfo_large = {
Richard Genoud77f54922008-04-23 19:51:14 +020072 .eccbytes = 4,
73 .eccpos = {60, 61, 62, 63},
74 .oobfree = {
75 {2, 58}
76 },
77};
78
79/* oob layout for small page size
80 * bad block info is on bytes 4 and 5
81 * the bytes have to be consecutives to avoid
82 * several NAND_CMD_RNDOUT during read
83 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020084static struct nand_ecclayout atmel_oobinfo_small = {
Richard Genoud77f54922008-04-23 19:51:14 +020085 .eccbytes = 4,
86 .eccpos = {0, 1, 2, 3},
87 .oobfree = {
88 {6, 10}
89 },
90};
91
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020092struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +020093 struct nand_chip nand_chip;
94 struct mtd_info mtd;
95 void __iomem *io_base;
Hong Xucbc6c5e2011-01-18 14:36:05 +080096 dma_addr_t io_phys;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020097 struct atmel_nand_data *board;
Richard Genoud77f54922008-04-23 19:51:14 +020098 struct device *dev;
99 void __iomem *ecc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800100
101 struct completion comp;
102 struct dma_chan *dma_chan;
Andrew Victor42cb1402006-10-19 18:24:35 +0200103};
104
Hong Xucbc6c5e2011-01-18 14:36:05 +0800105static int cpu_has_dma(void)
106{
107 return cpu_is_at91sam9rl() || cpu_is_at91sam9g45();
108}
109
Andrew Victor42cb1402006-10-19 18:24:35 +0200110/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900111 * Enable NAND.
112 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200113static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900114{
115 if (host->board->enable_pin)
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200116 gpio_set_value(host->board->enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900117}
118
119/*
120 * Disable NAND.
121 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200122static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900123{
124 if (host->board->enable_pin)
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200125 gpio_set_value(host->board->enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900126}
127
128/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200129 * Hardware specific access to control-lines
130 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200131static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200132{
133 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200134 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200135
Atsushi Nemoto81365082008-04-27 01:51:12 +0900136 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900137 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200138 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900139 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200140 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900141 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200142 if (cmd == NAND_CMD_NONE)
143 return;
144
145 if (ctrl & NAND_CLE)
146 writeb(cmd, host->io_base + (1 << host->board->cle));
147 else
148 writeb(cmd, host->io_base + (1 << host->board->ale));
149}
150
151/*
152 * Read the Device Ready pin.
153 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200154static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200155{
156 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200157 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200158
Gregory CLEMENT744f6592009-02-16 21:21:47 +0100159 return gpio_get_value(host->board->rdy_pin) ^
160 !!host->board->rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200161}
162
163/*
David Brownell23a346c2008-07-03 23:40:16 -0700164 * Minimal-overhead PIO for data access.
165 */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800166static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
David Brownell23a346c2008-07-03 23:40:16 -0700167{
168 struct nand_chip *nand_chip = mtd->priv;
169
170 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
171}
172
173static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
174{
175 struct nand_chip *nand_chip = mtd->priv;
176
177 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
178}
179
Hong Xucbc6c5e2011-01-18 14:36:05 +0800180static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
David Brownell23a346c2008-07-03 23:40:16 -0700181{
182 struct nand_chip *nand_chip = mtd->priv;
183
184 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
185}
186
187static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
188{
189 struct nand_chip *nand_chip = mtd->priv;
190
191 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
192}
193
Hong Xucbc6c5e2011-01-18 14:36:05 +0800194static void dma_complete_func(void *completion)
195{
196 complete(completion);
197}
198
199static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
200 int is_read)
201{
202 struct dma_device *dma_dev;
203 enum dma_ctrl_flags flags;
204 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
205 struct dma_async_tx_descriptor *tx = NULL;
206 dma_cookie_t cookie;
207 struct nand_chip *chip = mtd->priv;
208 struct atmel_nand_host *host = chip->priv;
209 void *p = buf;
210 int err = -EIO;
211 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
212
Hong Xu80b4f812011-03-31 18:33:15 +0800213 if (buf >= high_memory)
214 goto err_buf;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800215
216 dma_dev = host->dma_chan->device;
217
218 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_SRC_UNMAP |
219 DMA_COMPL_SKIP_DEST_UNMAP;
220
221 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
222 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
223 dev_err(host->dev, "Failed to dma_map_single\n");
224 goto err_buf;
225 }
226
227 if (is_read) {
228 dma_src_addr = host->io_phys;
229 dma_dst_addr = phys_addr;
230 } else {
231 dma_src_addr = phys_addr;
232 dma_dst_addr = host->io_phys;
233 }
234
235 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
236 dma_src_addr, len, flags);
237 if (!tx) {
238 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
239 goto err_dma;
240 }
241
242 init_completion(&host->comp);
243 tx->callback = dma_complete_func;
244 tx->callback_param = &host->comp;
245
246 cookie = tx->tx_submit(tx);
247 if (dma_submit_error(cookie)) {
248 dev_err(host->dev, "Failed to do DMA tx_submit\n");
249 goto err_dma;
250 }
251
252 dma_async_issue_pending(host->dma_chan);
253 wait_for_completion(&host->comp);
254
255 err = 0;
256
257err_dma:
258 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
259err_buf:
260 if (err != 0)
261 dev_warn(host->dev, "Fall back to CPU I/O\n");
262 return err;
263}
264
265static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
266{
267 struct nand_chip *chip = mtd->priv;
268 struct atmel_nand_host *host = chip->priv;
269
Nicolas Ferre9d515672011-04-01 16:40:44 +0200270 if (use_dma && len > mtd->oobsize)
271 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800272 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
273 return;
274
275 if (host->board->bus_width_16)
276 atmel_read_buf16(mtd, buf, len);
277 else
278 atmel_read_buf8(mtd, buf, len);
279}
280
281static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
282{
283 struct nand_chip *chip = mtd->priv;
284 struct atmel_nand_host *host = chip->priv;
285
Nicolas Ferre9d515672011-04-01 16:40:44 +0200286 if (use_dma && len > mtd->oobsize)
287 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800288 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
289 return;
290
291 if (host->board->bus_width_16)
292 atmel_write_buf16(mtd, buf, len);
293 else
294 atmel_write_buf8(mtd, buf, len);
295}
296
David Brownell23a346c2008-07-03 23:40:16 -0700297/*
Richard Genoud77f54922008-04-23 19:51:14 +0200298 * Calculate HW ECC
299 *
300 * function called after a write
301 *
302 * mtd: MTD block structure
303 * dat: raw data (unused)
304 * ecc_code: buffer for ECC
305 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200306static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +0200307 const u_char *dat, unsigned char *ecc_code)
308{
309 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200310 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +0200311 unsigned int ecc_value;
312
313 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +0200314 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +0200315
Richard Genoud3fc23892008-10-12 08:42:28 +0200316 ecc_code[0] = ecc_value & 0xFF;
317 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +0200318
319 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200320 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +0200321
Richard Genoud3fc23892008-10-12 08:42:28 +0200322 ecc_code[2] = ecc_value & 0xFF;
323 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +0200324
325 return 0;
326}
327
328/*
329 * HW ECC read page function
330 *
331 * mtd: mtd info structure
332 * chip: nand chip info structure
333 * buf: buffer to store read data
334 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200335static int atmel_nand_read_page(struct mtd_info *mtd,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -0700336 struct nand_chip *chip, uint8_t *buf, int page)
Richard Genoud77f54922008-04-23 19:51:14 +0200337{
338 int eccsize = chip->ecc.size;
339 int eccbytes = chip->ecc.bytes;
340 uint32_t *eccpos = chip->ecc.layout->eccpos;
341 uint8_t *p = buf;
342 uint8_t *oob = chip->oob_poi;
343 uint8_t *ecc_pos;
344 int stat;
345
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700346 /*
347 * Errata: ALE is incorrectly wired up to the ECC controller
348 * on the AP7000, so it will include the address cycles in the
349 * ECC calculation.
350 *
351 * Workaround: Reset the parity registers before reading the
352 * actual data.
353 */
354 if (cpu_is_at32ap7000()) {
355 struct atmel_nand_host *host = chip->priv;
356 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
357 }
358
Richard Genoud77f54922008-04-23 19:51:14 +0200359 /* read the page */
360 chip->read_buf(mtd, p, eccsize);
361
362 /* move to ECC position if needed */
363 if (eccpos[0] != 0) {
364 /* This only works on large pages
365 * because the ECC controller waits for
366 * NAND_CMD_RNDOUTSTART after the
367 * NAND_CMD_RNDOUT.
368 * anyway, for small pages, the eccpos[0] == 0
369 */
370 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
371 mtd->writesize + eccpos[0], -1);
372 }
373
374 /* the ECC controller needs to read the ECC just after the data */
375 ecc_pos = oob + eccpos[0];
376 chip->read_buf(mtd, ecc_pos, eccbytes);
377
378 /* check if there's an error */
379 stat = chip->ecc.correct(mtd, p, oob, NULL);
380
381 if (stat < 0)
382 mtd->ecc_stats.failed++;
383 else
384 mtd->ecc_stats.corrected += stat;
385
386 /* get back to oob start (end of page) */
387 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
388
389 /* read the oob */
390 chip->read_buf(mtd, oob, mtd->oobsize);
391
392 return 0;
393}
394
395/*
396 * HW ECC Correction
397 *
398 * function called after a read
399 *
400 * mtd: MTD block structure
401 * dat: raw data read from the chip
402 * read_ecc: ECC from the chip (unused)
403 * isnull: unused
404 *
405 * Detect and correct a 1 bit error for a page
406 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200407static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +0200408 u_char *read_ecc, u_char *isnull)
409{
410 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200411 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +0200412 unsigned int ecc_status;
413 unsigned int ecc_word, ecc_bit;
414
415 /* get the status from the Status Register */
416 ecc_status = ecc_readl(host->ecc, SR);
417
418 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200419 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +0200420 return 0;
421
422 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200423 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +0200424 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200425 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +0200426 ecc_word >>= 4;
427
428 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200429 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +0200430 /* check if it is a freshly erased block
431 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200432 if ((ecc_bit == ATMEL_ECC_BITADDR)
433 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +0200434 /* the block has just been erased, return OK */
435 return 0;
436 }
437 /* it doesn't seems to be a freshly
438 * erased block.
439 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200440 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +0200441 " Unable to correct.\n");
442 return -EIO;
443 }
444
445 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200446 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +0200447 /* there's nothing much to do here.
448 * the bit error is on the ECC itself.
449 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200450 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +0200451 " Nothing to correct\n");
452 return 0;
453 }
454
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200455 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +0200456 " (word offset in the page :"
457 " 0x%x bit offset : 0x%x)\n",
458 ecc_word, ecc_bit);
459 /* correct the error */
460 if (nand_chip->options & NAND_BUSWIDTH_16) {
461 /* 16 bits words */
462 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
463 } else {
464 /* 8 bits words */
465 dat[ecc_word] ^= (1 << ecc_bit);
466 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200467 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +0200468 return 1;
469}
470
471/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700472 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +0200473 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700474static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
475{
476 if (cpu_is_at32ap7000()) {
477 struct nand_chip *nand_chip = mtd->priv;
478 struct atmel_nand_host *host = nand_chip->priv;
479 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
480 }
481}
Richard Genoud77f54922008-04-23 19:51:14 +0200482
Andreas Bießmann9a9745c2010-08-05 12:38:41 +0200483#ifdef CONFIG_MTD_CMDLINE_PARTS
Atsushi Nemoto52f83012008-03-30 21:59:37 +0900484static const char *part_probes[] = { "cmdlinepart", NULL };
Andrew Victor693ef662007-05-03 08:16:44 +0200485#endif
486
Andrew Victor42cb1402006-10-19 18:24:35 +0200487/*
488 * Probe for the NAND device.
489 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200490static int __init atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +0200491{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200492 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +0200493 struct mtd_info *mtd;
494 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +0200495 struct resource *regs;
496 struct resource *mem;
Andrew Victor42cb1402006-10-19 18:24:35 +0200497 int res;
Andrew Victor42cb1402006-10-19 18:24:35 +0200498 struct mtd_partition *partitions = NULL;
499 int num_partitions = 0;
Andrew Victor42cb1402006-10-19 18:24:35 +0200500
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200501 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
502 if (!mem) {
503 printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
504 return -ENXIO;
505 }
506
Andrew Victor42cb1402006-10-19 18:24:35 +0200507 /* Allocate memory for the device structure (and zero it) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200508 host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
Andrew Victor42cb1402006-10-19 18:24:35 +0200509 if (!host) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200510 printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +0200511 return -ENOMEM;
512 }
513
Hong Xucbc6c5e2011-01-18 14:36:05 +0800514 host->io_phys = (dma_addr_t)mem->start;
515
Richard Genoud77f54922008-04-23 19:51:14 +0200516 host->io_base = ioremap(mem->start, mem->end - mem->start + 1);
Andrew Victor42cb1402006-10-19 18:24:35 +0200517 if (host->io_base == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200518 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200519 res = -EIO;
520 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +0200521 }
522
523 mtd = &host->mtd;
524 nand_chip = &host->nand_chip;
525 host->board = pdev->dev.platform_data;
Richard Genoud77f54922008-04-23 19:51:14 +0200526 host->dev = &pdev->dev;
Andrew Victor42cb1402006-10-19 18:24:35 +0200527
528 nand_chip->priv = host; /* link the private data structures */
529 mtd->priv = nand_chip;
530 mtd->owner = THIS_MODULE;
531
532 /* Set address of NAND IO lines */
533 nand_chip->IO_ADDR_R = host->io_base;
534 nand_chip->IO_ADDR_W = host->io_base;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200535 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Ivan Kutena4265f82007-05-24 14:35:58 +0300536
537 if (host->board->rdy_pin)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200538 nand_chip->dev_ready = atmel_nand_device_ready;
Ivan Kutena4265f82007-05-24 14:35:58 +0300539
Richard Genoud77f54922008-04-23 19:51:14 +0200540 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
541 if (!regs && hard_ecc) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200542 printk(KERN_ERR "atmel_nand: can't get I/O resource "
Richard Genoud77f54922008-04-23 19:51:14 +0200543 "regs\nFalling back on software ECC\n");
544 }
545
Andrew Victor42cb1402006-10-19 18:24:35 +0200546 nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
Richard Genoud77f54922008-04-23 19:51:14 +0200547 if (no_ecc)
548 nand_chip->ecc.mode = NAND_ECC_NONE;
549 if (hard_ecc && regs) {
550 host->ecc = ioremap(regs->start, regs->end - regs->start + 1);
551 if (host->ecc == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200552 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Richard Genoud77f54922008-04-23 19:51:14 +0200553 res = -EIO;
554 goto err_ecc_ioremap;
555 }
Richard Genoud3fc23892008-10-12 08:42:28 +0200556 nand_chip->ecc.mode = NAND_ECC_HW;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200557 nand_chip->ecc.calculate = atmel_nand_calculate;
558 nand_chip->ecc.correct = atmel_nand_correct;
559 nand_chip->ecc.hwctl = atmel_nand_hwctl;
560 nand_chip->ecc.read_page = atmel_nand_read_page;
Richard Genoud77f54922008-04-23 19:51:14 +0200561 nand_chip->ecc.bytes = 4;
Richard Genoud77f54922008-04-23 19:51:14 +0200562 }
563
Andrew Victor42cb1402006-10-19 18:24:35 +0200564 nand_chip->chip_delay = 20; /* 20us command delay time */
565
Hong Xucbc6c5e2011-01-18 14:36:05 +0800566 if (host->board->bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +0200567 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800568
569 nand_chip->read_buf = atmel_read_buf;
570 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +0200571
Andrew Victor42cb1402006-10-19 18:24:35 +0200572 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200573 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200574
575 if (host->board->det_pin) {
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200576 if (gpio_get_value(host->board->det_pin)) {
Simon Polettef4fa6972009-05-27 18:19:39 +0300577 printk(KERN_INFO "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +0100578 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200579 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +0200580 }
581 }
582
Simon Polettef4fa6972009-05-27 18:19:39 +0300583 if (on_flash_bbt) {
584 printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
585 nand_chip->options |= NAND_USE_FLASH_BBT;
586 }
587
Hong Xucb457a42011-03-30 16:26:41 +0800588 if (!cpu_has_dma())
589 use_dma = 0;
590
591 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +0800592 dma_cap_mask_t mask;
593
594 dma_cap_zero(mask);
595 dma_cap_set(DMA_MEMCPY, mask);
596 host->dma_chan = dma_request_channel(mask, 0, NULL);
597 if (!host->dma_chan) {
598 dev_err(host->dev, "Failed to request DMA channel\n");
599 use_dma = 0;
600 }
601 }
602 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +0800603 dev_info(host->dev, "Using %s for DMA transfers.\n",
604 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +0800605 else
606 dev_info(host->dev, "No DMA support for NAND access.\n");
607
Richard Genoud77f54922008-04-23 19:51:14 +0200608 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +0000609 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +0200610 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200611 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +0200612 }
613
Richard Genoud3fc23892008-10-12 08:42:28 +0200614 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Richard Genoud77f54922008-04-23 19:51:14 +0200615 /* ECC is calculated for the whole page (1 step) */
616 nand_chip->ecc.size = mtd->writesize;
617
618 /* set ECC page size and oob layout */
619 switch (mtd->writesize) {
620 case 512:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200621 nand_chip->ecc.layout = &atmel_oobinfo_small;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200622 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
Richard Genoud77f54922008-04-23 19:51:14 +0200623 break;
624 case 1024:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200625 nand_chip->ecc.layout = &atmel_oobinfo_large;
626 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
Richard Genoud77f54922008-04-23 19:51:14 +0200627 break;
628 case 2048:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200629 nand_chip->ecc.layout = &atmel_oobinfo_large;
630 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
Richard Genoud77f54922008-04-23 19:51:14 +0200631 break;
632 case 4096:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200633 nand_chip->ecc.layout = &atmel_oobinfo_large;
634 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
Richard Genoud77f54922008-04-23 19:51:14 +0200635 break;
636 default:
637 /* page size not handled by HW ECC */
638 /* switching back to soft ECC */
639 nand_chip->ecc.mode = NAND_ECC_SOFT;
640 nand_chip->ecc.calculate = NULL;
641 nand_chip->ecc.correct = NULL;
642 nand_chip->ecc.hwctl = NULL;
643 nand_chip->ecc.read_page = NULL;
644 nand_chip->ecc.postpad = 0;
645 nand_chip->ecc.prepad = 0;
646 nand_chip->ecc.bytes = 0;
647 break;
648 }
649 }
650
651 /* second phase scan */
652 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +0200653 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200654 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +0200655 }
656
Andrew Victor693ef662007-05-03 08:16:44 +0200657#ifdef CONFIG_MTD_CMDLINE_PARTS
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200658 mtd->name = "atmel_nand";
Atsushi Nemoto842b1a102008-01-29 22:28:22 +0900659 num_partitions = parse_mtd_partitions(mtd, part_probes,
660 &partitions, 0);
Andrew Victor693ef662007-05-03 08:16:44 +0200661#endif
Atsushi Nemoto842b1a102008-01-29 22:28:22 +0900662 if (num_partitions <= 0 && host->board->partition_info)
663 partitions = host->board->partition_info(mtd->size,
664 &num_partitions);
Andrew Victor42cb1402006-10-19 18:24:35 +0200665
666 if ((!partitions) || (num_partitions == 0)) {
Thadeu Lima de Souza Cascardoae27a7a2009-06-24 18:40:46 -0300667 printk(KERN_ERR "atmel_nand: No partitions defined, or unsupported device.\n");
Roel Kluin895fb492009-11-11 21:47:06 +0100668 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200669 goto err_no_partitions;
Andrew Victor42cb1402006-10-19 18:24:35 +0200670 }
671
Jamie Ilese6232b42011-05-23 10:23:13 +0100672 res = mtd_device_register(mtd, partitions, num_partitions);
Andrew Victor42cb1402006-10-19 18:24:35 +0200673 if (!res)
674 return res;
675
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200676err_no_partitions:
Andrew Victor42cb1402006-10-19 18:24:35 +0200677 nand_release(mtd);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200678err_scan_tail:
679err_scan_ident:
680err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200681 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200682 platform_set_drvdata(pdev, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800683 if (host->dma_chan)
684 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200685 if (host->ecc)
686 iounmap(host->ecc);
687err_ecc_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +0200688 iounmap(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200689err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +0200690 kfree(host);
691 return res;
692}
693
694/*
695 * Remove a NAND device.
696 */
David Brownell23a346c2008-07-03 23:40:16 -0700697static int __exit atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +0200698{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200699 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +0200700 struct mtd_info *mtd = &host->mtd;
701
702 nand_release(mtd);
703
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200704 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200705
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200706 if (host->ecc)
707 iounmap(host->ecc);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800708
709 if (host->dma_chan)
710 dma_release_channel(host->dma_chan);
711
Andrew Victor42cb1402006-10-19 18:24:35 +0200712 iounmap(host->io_base);
713 kfree(host);
714
715 return 0;
716}
717
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200718static struct platform_driver atmel_nand_driver = {
David Brownell23a346c2008-07-03 23:40:16 -0700719 .remove = __exit_p(atmel_nand_remove),
Andrew Victor42cb1402006-10-19 18:24:35 +0200720 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200721 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +0200722 .owner = THIS_MODULE,
723 },
724};
725
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200726static int __init atmel_nand_init(void)
Andrew Victor42cb1402006-10-19 18:24:35 +0200727{
David Brownell23a346c2008-07-03 23:40:16 -0700728 return platform_driver_probe(&atmel_nand_driver, atmel_nand_probe);
Andrew Victor42cb1402006-10-19 18:24:35 +0200729}
730
731
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200732static void __exit atmel_nand_exit(void)
Andrew Victor42cb1402006-10-19 18:24:35 +0200733{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200734 platform_driver_unregister(&atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +0200735}
736
737
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200738module_init(atmel_nand_init);
739module_exit(atmel_nand_exit);
Andrew Victor42cb1402006-10-19 18:24:35 +0200740
741MODULE_LICENSE("GPL");
742MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +0200743MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200744MODULE_ALIAS("platform:atmel_nand");