blob: 2802992b39daf67ce5c917103f03501d113f290e [file] [log] [blame]
Andrew Victor42cb1402006-10-19 18:24:35 +02001/*
Andrew Victor42cb1402006-10-19 18:24:35 +02002 * Copyright (C) 2003 Rick Bronson
3 *
4 * Derived from drivers/mtd/nand/autcpu12.c
5 * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
6 *
7 * Derived from drivers/mtd/spia.c
8 * Copyright (C) 2000 Steven J. Hill (sjhill@cotw.com)
9 *
Richard Genoud77f54922008-04-23 19:51:14 +020010 *
11 * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
12 * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright (C) 2007
13 *
14 * Derived from Das U-Boot source code
15 * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
16 * (C) Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
17 *
18 *
Andrew Victor42cb1402006-10-19 18:24:35 +020019 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 2 as
21 * published by the Free Software Foundation.
22 *
23 */
24
25#include <linux/slab.h>
26#include <linux/module.h>
Simon Polettef4fa6972009-05-27 18:19:39 +030027#include <linux/moduleparam.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020028#include <linux/platform_device.h>
29#include <linux/mtd/mtd.h>
30#include <linux/mtd/nand.h>
31#include <linux/mtd/partitions.h>
32
David Woodhouse90574d02008-06-07 08:49:00 +010033#include <linux/gpio.h>
34#include <linux/io.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020035
Russell Kinga09e64f2008-08-05 16:14:15 +010036#include <mach/board.h>
37#include <mach/cpu.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020038
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020039#ifdef CONFIG_MTD_NAND_ATMEL_ECC_HW
Richard Genoud77f54922008-04-23 19:51:14 +020040#define hard_ecc 1
41#else
42#define hard_ecc 0
43#endif
44
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020045#ifdef CONFIG_MTD_NAND_ATMEL_ECC_NONE
Richard Genoud77f54922008-04-23 19:51:14 +020046#define no_ecc 1
47#else
48#define no_ecc 0
49#endif
50
Simon Polettef4fa6972009-05-27 18:19:39 +030051static int on_flash_bbt = 0;
52module_param(on_flash_bbt, int, 0);
53
Richard Genoud77f54922008-04-23 19:51:14 +020054/* Register access macros */
55#define ecc_readl(add, reg) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020056 __raw_readl(add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020057#define ecc_writel(add, reg, value) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020058 __raw_writel((value), add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020059
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020060#include "atmel_nand_ecc.h" /* Hardware ECC registers */
Richard Genoud77f54922008-04-23 19:51:14 +020061
62/* oob layout for large page size
63 * bad block info is on bytes 0 and 1
64 * the bytes have to be consecutives to avoid
65 * several NAND_CMD_RNDOUT during read
66 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020067static struct nand_ecclayout atmel_oobinfo_large = {
Richard Genoud77f54922008-04-23 19:51:14 +020068 .eccbytes = 4,
69 .eccpos = {60, 61, 62, 63},
70 .oobfree = {
71 {2, 58}
72 },
73};
74
75/* oob layout for small page size
76 * bad block info is on bytes 4 and 5
77 * the bytes have to be consecutives to avoid
78 * several NAND_CMD_RNDOUT during read
79 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020080static struct nand_ecclayout atmel_oobinfo_small = {
Richard Genoud77f54922008-04-23 19:51:14 +020081 .eccbytes = 4,
82 .eccpos = {0, 1, 2, 3},
83 .oobfree = {
84 {6, 10}
85 },
86};
87
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020088struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +020089 struct nand_chip nand_chip;
90 struct mtd_info mtd;
91 void __iomem *io_base;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020092 struct atmel_nand_data *board;
Richard Genoud77f54922008-04-23 19:51:14 +020093 struct device *dev;
94 void __iomem *ecc;
Andrew Victor42cb1402006-10-19 18:24:35 +020095};
96
97/*
Atsushi Nemoto81365082008-04-27 01:51:12 +090098 * Enable NAND.
99 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200100static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900101{
102 if (host->board->enable_pin)
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200103 gpio_set_value(host->board->enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900104}
105
106/*
107 * Disable NAND.
108 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200109static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900110{
111 if (host->board->enable_pin)
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200112 gpio_set_value(host->board->enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900113}
114
115/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200116 * Hardware specific access to control-lines
117 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200118static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200119{
120 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200121 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200122
Atsushi Nemoto81365082008-04-27 01:51:12 +0900123 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900124 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200125 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900126 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200127 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900128 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200129 if (cmd == NAND_CMD_NONE)
130 return;
131
132 if (ctrl & NAND_CLE)
133 writeb(cmd, host->io_base + (1 << host->board->cle));
134 else
135 writeb(cmd, host->io_base + (1 << host->board->ale));
136}
137
138/*
139 * Read the Device Ready pin.
140 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200141static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200142{
143 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200144 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200145
Gregory CLEMENT744f6592009-02-16 21:21:47 +0100146 return gpio_get_value(host->board->rdy_pin) ^
147 !!host->board->rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200148}
149
150/*
David Brownell23a346c2008-07-03 23:40:16 -0700151 * Minimal-overhead PIO for data access.
152 */
153static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
154{
155 struct nand_chip *nand_chip = mtd->priv;
156
157 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
158}
159
160static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
161{
162 struct nand_chip *nand_chip = mtd->priv;
163
164 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
165}
166
167static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
168{
169 struct nand_chip *nand_chip = mtd->priv;
170
171 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
172}
173
174static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
175{
176 struct nand_chip *nand_chip = mtd->priv;
177
178 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
179}
180
181/*
Richard Genoud77f54922008-04-23 19:51:14 +0200182 * Calculate HW ECC
183 *
184 * function called after a write
185 *
186 * mtd: MTD block structure
187 * dat: raw data (unused)
188 * ecc_code: buffer for ECC
189 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200190static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +0200191 const u_char *dat, unsigned char *ecc_code)
192{
193 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200194 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +0200195 uint32_t *eccpos = nand_chip->ecc.layout->eccpos;
196 unsigned int ecc_value;
197
198 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +0200199 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +0200200
Richard Genoud3fc23892008-10-12 08:42:28 +0200201 ecc_code[0] = ecc_value & 0xFF;
202 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +0200203
204 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200205 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +0200206
Richard Genoud3fc23892008-10-12 08:42:28 +0200207 ecc_code[2] = ecc_value & 0xFF;
208 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +0200209
210 return 0;
211}
212
213/*
214 * HW ECC read page function
215 *
216 * mtd: mtd info structure
217 * chip: nand chip info structure
218 * buf: buffer to store read data
219 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200220static int atmel_nand_read_page(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +0200221 struct nand_chip *chip, uint8_t *buf)
222{
223 int eccsize = chip->ecc.size;
224 int eccbytes = chip->ecc.bytes;
225 uint32_t *eccpos = chip->ecc.layout->eccpos;
226 uint8_t *p = buf;
227 uint8_t *oob = chip->oob_poi;
228 uint8_t *ecc_pos;
229 int stat;
230
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700231 /*
232 * Errata: ALE is incorrectly wired up to the ECC controller
233 * on the AP7000, so it will include the address cycles in the
234 * ECC calculation.
235 *
236 * Workaround: Reset the parity registers before reading the
237 * actual data.
238 */
239 if (cpu_is_at32ap7000()) {
240 struct atmel_nand_host *host = chip->priv;
241 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
242 }
243
Richard Genoud77f54922008-04-23 19:51:14 +0200244 /* read the page */
245 chip->read_buf(mtd, p, eccsize);
246
247 /* move to ECC position if needed */
248 if (eccpos[0] != 0) {
249 /* This only works on large pages
250 * because the ECC controller waits for
251 * NAND_CMD_RNDOUTSTART after the
252 * NAND_CMD_RNDOUT.
253 * anyway, for small pages, the eccpos[0] == 0
254 */
255 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
256 mtd->writesize + eccpos[0], -1);
257 }
258
259 /* the ECC controller needs to read the ECC just after the data */
260 ecc_pos = oob + eccpos[0];
261 chip->read_buf(mtd, ecc_pos, eccbytes);
262
263 /* check if there's an error */
264 stat = chip->ecc.correct(mtd, p, oob, NULL);
265
266 if (stat < 0)
267 mtd->ecc_stats.failed++;
268 else
269 mtd->ecc_stats.corrected += stat;
270
271 /* get back to oob start (end of page) */
272 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
273
274 /* read the oob */
275 chip->read_buf(mtd, oob, mtd->oobsize);
276
277 return 0;
278}
279
280/*
281 * HW ECC Correction
282 *
283 * function called after a read
284 *
285 * mtd: MTD block structure
286 * dat: raw data read from the chip
287 * read_ecc: ECC from the chip (unused)
288 * isnull: unused
289 *
290 * Detect and correct a 1 bit error for a page
291 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200292static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +0200293 u_char *read_ecc, u_char *isnull)
294{
295 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200296 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +0200297 unsigned int ecc_status;
298 unsigned int ecc_word, ecc_bit;
299
300 /* get the status from the Status Register */
301 ecc_status = ecc_readl(host->ecc, SR);
302
303 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200304 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +0200305 return 0;
306
307 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200308 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +0200309 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200310 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +0200311 ecc_word >>= 4;
312
313 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200314 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +0200315 /* check if it is a freshly erased block
316 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200317 if ((ecc_bit == ATMEL_ECC_BITADDR)
318 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +0200319 /* the block has just been erased, return OK */
320 return 0;
321 }
322 /* it doesn't seems to be a freshly
323 * erased block.
324 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200325 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +0200326 " Unable to correct.\n");
327 return -EIO;
328 }
329
330 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200331 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +0200332 /* there's nothing much to do here.
333 * the bit error is on the ECC itself.
334 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200335 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +0200336 " Nothing to correct\n");
337 return 0;
338 }
339
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200340 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +0200341 " (word offset in the page :"
342 " 0x%x bit offset : 0x%x)\n",
343 ecc_word, ecc_bit);
344 /* correct the error */
345 if (nand_chip->options & NAND_BUSWIDTH_16) {
346 /* 16 bits words */
347 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
348 } else {
349 /* 8 bits words */
350 dat[ecc_word] ^= (1 << ecc_bit);
351 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200352 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +0200353 return 1;
354}
355
356/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700357 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +0200358 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700359static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
360{
361 if (cpu_is_at32ap7000()) {
362 struct nand_chip *nand_chip = mtd->priv;
363 struct atmel_nand_host *host = nand_chip->priv;
364 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
365 }
366}
Richard Genoud77f54922008-04-23 19:51:14 +0200367
Andrew Victor693ef662007-05-03 08:16:44 +0200368#ifdef CONFIG_MTD_PARTITIONS
Atsushi Nemoto52f83012008-03-30 21:59:37 +0900369static const char *part_probes[] = { "cmdlinepart", NULL };
Andrew Victor693ef662007-05-03 08:16:44 +0200370#endif
371
Andrew Victor42cb1402006-10-19 18:24:35 +0200372/*
373 * Probe for the NAND device.
374 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200375static int __init atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +0200376{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200377 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +0200378 struct mtd_info *mtd;
379 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +0200380 struct resource *regs;
381 struct resource *mem;
Andrew Victor42cb1402006-10-19 18:24:35 +0200382 int res;
383
384#ifdef CONFIG_MTD_PARTITIONS
385 struct mtd_partition *partitions = NULL;
386 int num_partitions = 0;
387#endif
388
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200389 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
390 if (!mem) {
391 printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
392 return -ENXIO;
393 }
394
Andrew Victor42cb1402006-10-19 18:24:35 +0200395 /* Allocate memory for the device structure (and zero it) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200396 host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
Andrew Victor42cb1402006-10-19 18:24:35 +0200397 if (!host) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200398 printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +0200399 return -ENOMEM;
400 }
401
Richard Genoud77f54922008-04-23 19:51:14 +0200402 host->io_base = ioremap(mem->start, mem->end - mem->start + 1);
Andrew Victor42cb1402006-10-19 18:24:35 +0200403 if (host->io_base == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200404 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200405 res = -EIO;
406 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +0200407 }
408
409 mtd = &host->mtd;
410 nand_chip = &host->nand_chip;
411 host->board = pdev->dev.platform_data;
Richard Genoud77f54922008-04-23 19:51:14 +0200412 host->dev = &pdev->dev;
Andrew Victor42cb1402006-10-19 18:24:35 +0200413
414 nand_chip->priv = host; /* link the private data structures */
415 mtd->priv = nand_chip;
416 mtd->owner = THIS_MODULE;
417
418 /* Set address of NAND IO lines */
419 nand_chip->IO_ADDR_R = host->io_base;
420 nand_chip->IO_ADDR_W = host->io_base;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200421 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Ivan Kutena4265f82007-05-24 14:35:58 +0300422
423 if (host->board->rdy_pin)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200424 nand_chip->dev_ready = atmel_nand_device_ready;
Ivan Kutena4265f82007-05-24 14:35:58 +0300425
Richard Genoud77f54922008-04-23 19:51:14 +0200426 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
427 if (!regs && hard_ecc) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200428 printk(KERN_ERR "atmel_nand: can't get I/O resource "
Richard Genoud77f54922008-04-23 19:51:14 +0200429 "regs\nFalling back on software ECC\n");
430 }
431
Andrew Victor42cb1402006-10-19 18:24:35 +0200432 nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
Richard Genoud77f54922008-04-23 19:51:14 +0200433 if (no_ecc)
434 nand_chip->ecc.mode = NAND_ECC_NONE;
435 if (hard_ecc && regs) {
436 host->ecc = ioremap(regs->start, regs->end - regs->start + 1);
437 if (host->ecc == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200438 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Richard Genoud77f54922008-04-23 19:51:14 +0200439 res = -EIO;
440 goto err_ecc_ioremap;
441 }
Richard Genoud3fc23892008-10-12 08:42:28 +0200442 nand_chip->ecc.mode = NAND_ECC_HW;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200443 nand_chip->ecc.calculate = atmel_nand_calculate;
444 nand_chip->ecc.correct = atmel_nand_correct;
445 nand_chip->ecc.hwctl = atmel_nand_hwctl;
446 nand_chip->ecc.read_page = atmel_nand_read_page;
Richard Genoud77f54922008-04-23 19:51:14 +0200447 nand_chip->ecc.bytes = 4;
Richard Genoud77f54922008-04-23 19:51:14 +0200448 }
449
Andrew Victor42cb1402006-10-19 18:24:35 +0200450 nand_chip->chip_delay = 20; /* 20us command delay time */
451
David Brownell23a346c2008-07-03 23:40:16 -0700452 if (host->board->bus_width_16) { /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +0200453 nand_chip->options |= NAND_BUSWIDTH_16;
David Brownell23a346c2008-07-03 23:40:16 -0700454 nand_chip->read_buf = atmel_read_buf16;
455 nand_chip->write_buf = atmel_write_buf16;
456 } else {
457 nand_chip->read_buf = atmel_read_buf;
458 nand_chip->write_buf = atmel_write_buf;
459 }
Andrew Victordd11b8c2006-12-08 13:49:42 +0200460
Andrew Victor42cb1402006-10-19 18:24:35 +0200461 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200462 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200463
464 if (host->board->det_pin) {
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200465 if (gpio_get_value(host->board->det_pin)) {
Simon Polettef4fa6972009-05-27 18:19:39 +0300466 printk(KERN_INFO "No SmartMedia card inserted.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +0200467 res = ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200468 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +0200469 }
470 }
471
Simon Polettef4fa6972009-05-27 18:19:39 +0300472 if (on_flash_bbt) {
473 printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
474 nand_chip->options |= NAND_USE_FLASH_BBT;
475 }
476
Richard Genoud77f54922008-04-23 19:51:14 +0200477 /* first scan to find the device and get the page size */
478 if (nand_scan_ident(mtd, 1)) {
479 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200480 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +0200481 }
482
Richard Genoud3fc23892008-10-12 08:42:28 +0200483 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Richard Genoud77f54922008-04-23 19:51:14 +0200484 /* ECC is calculated for the whole page (1 step) */
485 nand_chip->ecc.size = mtd->writesize;
486
487 /* set ECC page size and oob layout */
488 switch (mtd->writesize) {
489 case 512:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200490 nand_chip->ecc.layout = &atmel_oobinfo_small;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200491 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
Richard Genoud77f54922008-04-23 19:51:14 +0200492 break;
493 case 1024:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200494 nand_chip->ecc.layout = &atmel_oobinfo_large;
495 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
Richard Genoud77f54922008-04-23 19:51:14 +0200496 break;
497 case 2048:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200498 nand_chip->ecc.layout = &atmel_oobinfo_large;
499 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
Richard Genoud77f54922008-04-23 19:51:14 +0200500 break;
501 case 4096:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200502 nand_chip->ecc.layout = &atmel_oobinfo_large;
503 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
Richard Genoud77f54922008-04-23 19:51:14 +0200504 break;
505 default:
506 /* page size not handled by HW ECC */
507 /* switching back to soft ECC */
508 nand_chip->ecc.mode = NAND_ECC_SOFT;
509 nand_chip->ecc.calculate = NULL;
510 nand_chip->ecc.correct = NULL;
511 nand_chip->ecc.hwctl = NULL;
512 nand_chip->ecc.read_page = NULL;
513 nand_chip->ecc.postpad = 0;
514 nand_chip->ecc.prepad = 0;
515 nand_chip->ecc.bytes = 0;
516 break;
517 }
518 }
519
520 /* second phase scan */
521 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +0200522 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200523 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +0200524 }
525
526#ifdef CONFIG_MTD_PARTITIONS
Andrew Victor693ef662007-05-03 08:16:44 +0200527#ifdef CONFIG_MTD_CMDLINE_PARTS
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200528 mtd->name = "atmel_nand";
Atsushi Nemoto842b1a102008-01-29 22:28:22 +0900529 num_partitions = parse_mtd_partitions(mtd, part_probes,
530 &partitions, 0);
Andrew Victor693ef662007-05-03 08:16:44 +0200531#endif
Atsushi Nemoto842b1a102008-01-29 22:28:22 +0900532 if (num_partitions <= 0 && host->board->partition_info)
533 partitions = host->board->partition_info(mtd->size,
534 &num_partitions);
Andrew Victor42cb1402006-10-19 18:24:35 +0200535
536 if ((!partitions) || (num_partitions == 0)) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200537 printk(KERN_ERR "atmel_nand: No parititions defined, or unsupported device.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +0200538 res = ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200539 goto err_no_partitions;
Andrew Victor42cb1402006-10-19 18:24:35 +0200540 }
541
542 res = add_mtd_partitions(mtd, partitions, num_partitions);
543#else
544 res = add_mtd_device(mtd);
545#endif
546
547 if (!res)
548 return res;
549
Richard Genoud77f54922008-04-23 19:51:14 +0200550#ifdef CONFIG_MTD_PARTITIONS
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200551err_no_partitions:
Richard Genoud77f54922008-04-23 19:51:14 +0200552#endif
Andrew Victor42cb1402006-10-19 18:24:35 +0200553 nand_release(mtd);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200554err_scan_tail:
555err_scan_ident:
556err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200557 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200558 platform_set_drvdata(pdev, NULL);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200559 if (host->ecc)
560 iounmap(host->ecc);
561err_ecc_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +0200562 iounmap(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200563err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +0200564 kfree(host);
565 return res;
566}
567
568/*
569 * Remove a NAND device.
570 */
David Brownell23a346c2008-07-03 23:40:16 -0700571static int __exit atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +0200572{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200573 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +0200574 struct mtd_info *mtd = &host->mtd;
575
576 nand_release(mtd);
577
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200578 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200579
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200580 if (host->ecc)
581 iounmap(host->ecc);
Andrew Victor42cb1402006-10-19 18:24:35 +0200582 iounmap(host->io_base);
583 kfree(host);
584
585 return 0;
586}
587
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200588static struct platform_driver atmel_nand_driver = {
David Brownell23a346c2008-07-03 23:40:16 -0700589 .remove = __exit_p(atmel_nand_remove),
Andrew Victor42cb1402006-10-19 18:24:35 +0200590 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200591 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +0200592 .owner = THIS_MODULE,
593 },
594};
595
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200596static int __init atmel_nand_init(void)
Andrew Victor42cb1402006-10-19 18:24:35 +0200597{
David Brownell23a346c2008-07-03 23:40:16 -0700598 return platform_driver_probe(&atmel_nand_driver, atmel_nand_probe);
Andrew Victor42cb1402006-10-19 18:24:35 +0200599}
600
601
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200602static void __exit atmel_nand_exit(void)
Andrew Victor42cb1402006-10-19 18:24:35 +0200603{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200604 platform_driver_unregister(&atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +0200605}
606
607
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200608module_init(atmel_nand_init);
609module_exit(atmel_nand_exit);
Andrew Victor42cb1402006-10-19 18:24:35 +0200610
611MODULE_LICENSE("GPL");
612MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +0200613MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200614MODULE_ALIAS("platform:atmel_nand");