blob: 380912f24ad85e5328ffa85d8366ccccd23621d5 [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;
498
499#ifdef CONFIG_MTD_PARTITIONS
500 struct mtd_partition *partitions = NULL;
501 int num_partitions = 0;
502#endif
503
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200504 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
505 if (!mem) {
506 printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
507 return -ENXIO;
508 }
509
Andrew Victor42cb1402006-10-19 18:24:35 +0200510 /* Allocate memory for the device structure (and zero it) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200511 host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
Andrew Victor42cb1402006-10-19 18:24:35 +0200512 if (!host) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200513 printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +0200514 return -ENOMEM;
515 }
516
Hong Xucbc6c5e2011-01-18 14:36:05 +0800517 host->io_phys = (dma_addr_t)mem->start;
518
Richard Genoud77f54922008-04-23 19:51:14 +0200519 host->io_base = ioremap(mem->start, mem->end - mem->start + 1);
Andrew Victor42cb1402006-10-19 18:24:35 +0200520 if (host->io_base == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200521 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200522 res = -EIO;
523 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +0200524 }
525
526 mtd = &host->mtd;
527 nand_chip = &host->nand_chip;
528 host->board = pdev->dev.platform_data;
Richard Genoud77f54922008-04-23 19:51:14 +0200529 host->dev = &pdev->dev;
Andrew Victor42cb1402006-10-19 18:24:35 +0200530
531 nand_chip->priv = host; /* link the private data structures */
532 mtd->priv = nand_chip;
533 mtd->owner = THIS_MODULE;
534
535 /* Set address of NAND IO lines */
536 nand_chip->IO_ADDR_R = host->io_base;
537 nand_chip->IO_ADDR_W = host->io_base;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200538 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Ivan Kutena4265f82007-05-24 14:35:58 +0300539
540 if (host->board->rdy_pin)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200541 nand_chip->dev_ready = atmel_nand_device_ready;
Ivan Kutena4265f82007-05-24 14:35:58 +0300542
Richard Genoud77f54922008-04-23 19:51:14 +0200543 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
544 if (!regs && hard_ecc) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200545 printk(KERN_ERR "atmel_nand: can't get I/O resource "
Richard Genoud77f54922008-04-23 19:51:14 +0200546 "regs\nFalling back on software ECC\n");
547 }
548
Andrew Victor42cb1402006-10-19 18:24:35 +0200549 nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
Richard Genoud77f54922008-04-23 19:51:14 +0200550 if (no_ecc)
551 nand_chip->ecc.mode = NAND_ECC_NONE;
552 if (hard_ecc && regs) {
553 host->ecc = ioremap(regs->start, regs->end - regs->start + 1);
554 if (host->ecc == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200555 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Richard Genoud77f54922008-04-23 19:51:14 +0200556 res = -EIO;
557 goto err_ecc_ioremap;
558 }
Richard Genoud3fc23892008-10-12 08:42:28 +0200559 nand_chip->ecc.mode = NAND_ECC_HW;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200560 nand_chip->ecc.calculate = atmel_nand_calculate;
561 nand_chip->ecc.correct = atmel_nand_correct;
562 nand_chip->ecc.hwctl = atmel_nand_hwctl;
563 nand_chip->ecc.read_page = atmel_nand_read_page;
Richard Genoud77f54922008-04-23 19:51:14 +0200564 nand_chip->ecc.bytes = 4;
Richard Genoud77f54922008-04-23 19:51:14 +0200565 }
566
Andrew Victor42cb1402006-10-19 18:24:35 +0200567 nand_chip->chip_delay = 20; /* 20us command delay time */
568
Hong Xucbc6c5e2011-01-18 14:36:05 +0800569 if (host->board->bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +0200570 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800571
572 nand_chip->read_buf = atmel_read_buf;
573 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +0200574
Andrew Victor42cb1402006-10-19 18:24:35 +0200575 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200576 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200577
578 if (host->board->det_pin) {
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200579 if (gpio_get_value(host->board->det_pin)) {
Simon Polettef4fa6972009-05-27 18:19:39 +0300580 printk(KERN_INFO "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +0100581 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200582 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +0200583 }
584 }
585
Simon Polettef4fa6972009-05-27 18:19:39 +0300586 if (on_flash_bbt) {
587 printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
588 nand_chip->options |= NAND_USE_FLASH_BBT;
589 }
590
Hong Xucb457a42011-03-30 16:26:41 +0800591 if (!cpu_has_dma())
592 use_dma = 0;
593
594 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +0800595 dma_cap_mask_t mask;
596
597 dma_cap_zero(mask);
598 dma_cap_set(DMA_MEMCPY, mask);
599 host->dma_chan = dma_request_channel(mask, 0, NULL);
600 if (!host->dma_chan) {
601 dev_err(host->dev, "Failed to request DMA channel\n");
602 use_dma = 0;
603 }
604 }
605 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +0800606 dev_info(host->dev, "Using %s for DMA transfers.\n",
607 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +0800608 else
609 dev_info(host->dev, "No DMA support for NAND access.\n");
610
Richard Genoud77f54922008-04-23 19:51:14 +0200611 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +0000612 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +0200613 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200614 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +0200615 }
616
Richard Genoud3fc23892008-10-12 08:42:28 +0200617 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Richard Genoud77f54922008-04-23 19:51:14 +0200618 /* ECC is calculated for the whole page (1 step) */
619 nand_chip->ecc.size = mtd->writesize;
620
621 /* set ECC page size and oob layout */
622 switch (mtd->writesize) {
623 case 512:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200624 nand_chip->ecc.layout = &atmel_oobinfo_small;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200625 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
Richard Genoud77f54922008-04-23 19:51:14 +0200626 break;
627 case 1024:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200628 nand_chip->ecc.layout = &atmel_oobinfo_large;
629 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
Richard Genoud77f54922008-04-23 19:51:14 +0200630 break;
631 case 2048:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200632 nand_chip->ecc.layout = &atmel_oobinfo_large;
633 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
Richard Genoud77f54922008-04-23 19:51:14 +0200634 break;
635 case 4096:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200636 nand_chip->ecc.layout = &atmel_oobinfo_large;
637 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
Richard Genoud77f54922008-04-23 19:51:14 +0200638 break;
639 default:
640 /* page size not handled by HW ECC */
641 /* switching back to soft ECC */
642 nand_chip->ecc.mode = NAND_ECC_SOFT;
643 nand_chip->ecc.calculate = NULL;
644 nand_chip->ecc.correct = NULL;
645 nand_chip->ecc.hwctl = NULL;
646 nand_chip->ecc.read_page = NULL;
647 nand_chip->ecc.postpad = 0;
648 nand_chip->ecc.prepad = 0;
649 nand_chip->ecc.bytes = 0;
650 break;
651 }
652 }
653
654 /* second phase scan */
655 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +0200656 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200657 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +0200658 }
659
660#ifdef CONFIG_MTD_PARTITIONS
Andrew Victor693ef662007-05-03 08:16:44 +0200661#ifdef CONFIG_MTD_CMDLINE_PARTS
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200662 mtd->name = "atmel_nand";
Atsushi Nemoto842b1a102008-01-29 22:28:22 +0900663 num_partitions = parse_mtd_partitions(mtd, part_probes,
664 &partitions, 0);
Andrew Victor693ef662007-05-03 08:16:44 +0200665#endif
Atsushi Nemoto842b1a102008-01-29 22:28:22 +0900666 if (num_partitions <= 0 && host->board->partition_info)
667 partitions = host->board->partition_info(mtd->size,
668 &num_partitions);
Andrew Victor42cb1402006-10-19 18:24:35 +0200669
670 if ((!partitions) || (num_partitions == 0)) {
Thadeu Lima de Souza Cascardoae27a7a2009-06-24 18:40:46 -0300671 printk(KERN_ERR "atmel_nand: No partitions defined, or unsupported device.\n");
Roel Kluin895fb492009-11-11 21:47:06 +0100672 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200673 goto err_no_partitions;
Andrew Victor42cb1402006-10-19 18:24:35 +0200674 }
675
676 res = add_mtd_partitions(mtd, partitions, num_partitions);
677#else
678 res = add_mtd_device(mtd);
679#endif
680
681 if (!res)
682 return res;
683
Richard Genoud77f54922008-04-23 19:51:14 +0200684#ifdef CONFIG_MTD_PARTITIONS
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200685err_no_partitions:
Richard Genoud77f54922008-04-23 19:51:14 +0200686#endif
Andrew Victor42cb1402006-10-19 18:24:35 +0200687 nand_release(mtd);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200688err_scan_tail:
689err_scan_ident:
690err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200691 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200692 platform_set_drvdata(pdev, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800693 if (host->dma_chan)
694 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200695 if (host->ecc)
696 iounmap(host->ecc);
697err_ecc_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +0200698 iounmap(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200699err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +0200700 kfree(host);
701 return res;
702}
703
704/*
705 * Remove a NAND device.
706 */
David Brownell23a346c2008-07-03 23:40:16 -0700707static int __exit atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +0200708{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200709 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +0200710 struct mtd_info *mtd = &host->mtd;
711
712 nand_release(mtd);
713
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200714 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200715
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200716 if (host->ecc)
717 iounmap(host->ecc);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800718
719 if (host->dma_chan)
720 dma_release_channel(host->dma_chan);
721
Andrew Victor42cb1402006-10-19 18:24:35 +0200722 iounmap(host->io_base);
723 kfree(host);
724
725 return 0;
726}
727
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200728static struct platform_driver atmel_nand_driver = {
David Brownell23a346c2008-07-03 23:40:16 -0700729 .remove = __exit_p(atmel_nand_remove),
Andrew Victor42cb1402006-10-19 18:24:35 +0200730 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200731 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +0200732 .owner = THIS_MODULE,
733 },
734};
735
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200736static int __init atmel_nand_init(void)
Andrew Victor42cb1402006-10-19 18:24:35 +0200737{
David Brownell23a346c2008-07-03 23:40:16 -0700738 return platform_driver_probe(&atmel_nand_driver, atmel_nand_probe);
Andrew Victor42cb1402006-10-19 18:24:35 +0200739}
740
741
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200742static void __exit atmel_nand_exit(void)
Andrew Victor42cb1402006-10-19 18:24:35 +0200743{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200744 platform_driver_unregister(&atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +0200745}
746
747
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200748module_init(atmel_nand_init);
749module_exit(atmel_nand_exit);
Andrew Victor42cb1402006-10-19 18:24:35 +0200750
751MODULE_LICENSE("GPL");
752MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +0200753MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200754MODULE_ALIAS("platform:atmel_nand");