blob: 47a33cec3793d4b3f1a8b512e3fcf95d5ca83aba [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>
27#include <linux/platform_device.h>
28#include <linux/mtd/mtd.h>
29#include <linux/mtd/nand.h>
30#include <linux/mtd/partitions.h>
31
David Woodhouse90574d02008-06-07 08:49:00 +010032#include <linux/gpio.h>
33#include <linux/io.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020034
Russell Kinga09e64f2008-08-05 16:14:15 +010035#include <mach/board.h>
36#include <mach/cpu.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020037
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020038#ifdef CONFIG_MTD_NAND_ATMEL_ECC_HW
Richard Genoud77f54922008-04-23 19:51:14 +020039#define hard_ecc 1
40#else
41#define hard_ecc 0
42#endif
43
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020044#ifdef CONFIG_MTD_NAND_ATMEL_ECC_NONE
Richard Genoud77f54922008-04-23 19:51:14 +020045#define no_ecc 1
46#else
47#define no_ecc 0
48#endif
49
50/* Register access macros */
51#define ecc_readl(add, reg) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020052 __raw_readl(add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020053#define ecc_writel(add, reg, value) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020054 __raw_writel((value), add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020055
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020056#include "atmel_nand_ecc.h" /* Hardware ECC registers */
Richard Genoud77f54922008-04-23 19:51:14 +020057
58/* oob layout for large page size
59 * bad block info is on bytes 0 and 1
60 * the bytes have to be consecutives to avoid
61 * several NAND_CMD_RNDOUT during read
62 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020063static struct nand_ecclayout atmel_oobinfo_large = {
Richard Genoud77f54922008-04-23 19:51:14 +020064 .eccbytes = 4,
65 .eccpos = {60, 61, 62, 63},
66 .oobfree = {
67 {2, 58}
68 },
69};
70
71/* oob layout for small page size
72 * bad block info is on bytes 4 and 5
73 * the bytes have to be consecutives to avoid
74 * several NAND_CMD_RNDOUT during read
75 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020076static struct nand_ecclayout atmel_oobinfo_small = {
Richard Genoud77f54922008-04-23 19:51:14 +020077 .eccbytes = 4,
78 .eccpos = {0, 1, 2, 3},
79 .oobfree = {
80 {6, 10}
81 },
82};
83
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020084struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +020085 struct nand_chip nand_chip;
86 struct mtd_info mtd;
87 void __iomem *io_base;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020088 struct atmel_nand_data *board;
Richard Genoud77f54922008-04-23 19:51:14 +020089 struct device *dev;
90 void __iomem *ecc;
Andrew Victor42cb1402006-10-19 18:24:35 +020091};
92
93/*
Atsushi Nemoto81365082008-04-27 01:51:12 +090094 * Enable NAND.
95 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020096static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +090097{
98 if (host->board->enable_pin)
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +020099 gpio_set_value(host->board->enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900100}
101
102/*
103 * Disable NAND.
104 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200105static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900106{
107 if (host->board->enable_pin)
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200108 gpio_set_value(host->board->enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900109}
110
111/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200112 * Hardware specific access to control-lines
113 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200114static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200115{
116 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200117 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200118
Atsushi Nemoto81365082008-04-27 01:51:12 +0900119 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900120 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200121 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900122 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200123 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900124 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200125 if (cmd == NAND_CMD_NONE)
126 return;
127
128 if (ctrl & NAND_CLE)
129 writeb(cmd, host->io_base + (1 << host->board->cle));
130 else
131 writeb(cmd, host->io_base + (1 << host->board->ale));
132}
133
134/*
135 * Read the Device Ready pin.
136 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200137static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200138{
139 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200140 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200141
Gregory CLEMENT744f6592009-02-16 21:21:47 +0100142 return gpio_get_value(host->board->rdy_pin) ^
143 !!host->board->rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200144}
145
146/*
David Brownell23a346c2008-07-03 23:40:16 -0700147 * Minimal-overhead PIO for data access.
148 */
149static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
150{
151 struct nand_chip *nand_chip = mtd->priv;
152
153 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
154}
155
156static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
157{
158 struct nand_chip *nand_chip = mtd->priv;
159
160 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
161}
162
163static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
164{
165 struct nand_chip *nand_chip = mtd->priv;
166
167 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
168}
169
170static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
171{
172 struct nand_chip *nand_chip = mtd->priv;
173
174 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
175}
176
177/*
Richard Genoud77f54922008-04-23 19:51:14 +0200178 * Calculate HW ECC
179 *
180 * function called after a write
181 *
182 * mtd: MTD block structure
183 * dat: raw data (unused)
184 * ecc_code: buffer for ECC
185 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200186static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +0200187 const u_char *dat, unsigned char *ecc_code)
188{
189 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200190 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +0200191 uint32_t *eccpos = nand_chip->ecc.layout->eccpos;
192 unsigned int ecc_value;
193
194 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +0200195 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +0200196
Richard Genoud3fc23892008-10-12 08:42:28 +0200197 ecc_code[0] = ecc_value & 0xFF;
198 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +0200199
200 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200201 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +0200202
Richard Genoud3fc23892008-10-12 08:42:28 +0200203 ecc_code[2] = ecc_value & 0xFF;
204 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +0200205
206 return 0;
207}
208
209/*
210 * HW ECC read page function
211 *
212 * mtd: mtd info structure
213 * chip: nand chip info structure
214 * buf: buffer to store read data
215 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200216static int atmel_nand_read_page(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +0200217 struct nand_chip *chip, uint8_t *buf)
218{
219 int eccsize = chip->ecc.size;
220 int eccbytes = chip->ecc.bytes;
221 uint32_t *eccpos = chip->ecc.layout->eccpos;
222 uint8_t *p = buf;
223 uint8_t *oob = chip->oob_poi;
224 uint8_t *ecc_pos;
225 int stat;
226
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700227 /*
228 * Errata: ALE is incorrectly wired up to the ECC controller
229 * on the AP7000, so it will include the address cycles in the
230 * ECC calculation.
231 *
232 * Workaround: Reset the parity registers before reading the
233 * actual data.
234 */
235 if (cpu_is_at32ap7000()) {
236 struct atmel_nand_host *host = chip->priv;
237 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
238 }
239
Richard Genoud77f54922008-04-23 19:51:14 +0200240 /* read the page */
241 chip->read_buf(mtd, p, eccsize);
242
243 /* move to ECC position if needed */
244 if (eccpos[0] != 0) {
245 /* This only works on large pages
246 * because the ECC controller waits for
247 * NAND_CMD_RNDOUTSTART after the
248 * NAND_CMD_RNDOUT.
249 * anyway, for small pages, the eccpos[0] == 0
250 */
251 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
252 mtd->writesize + eccpos[0], -1);
253 }
254
255 /* the ECC controller needs to read the ECC just after the data */
256 ecc_pos = oob + eccpos[0];
257 chip->read_buf(mtd, ecc_pos, eccbytes);
258
259 /* check if there's an error */
260 stat = chip->ecc.correct(mtd, p, oob, NULL);
261
262 if (stat < 0)
263 mtd->ecc_stats.failed++;
264 else
265 mtd->ecc_stats.corrected += stat;
266
267 /* get back to oob start (end of page) */
268 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
269
270 /* read the oob */
271 chip->read_buf(mtd, oob, mtd->oobsize);
272
273 return 0;
274}
275
276/*
277 * HW ECC Correction
278 *
279 * function called after a read
280 *
281 * mtd: MTD block structure
282 * dat: raw data read from the chip
283 * read_ecc: ECC from the chip (unused)
284 * isnull: unused
285 *
286 * Detect and correct a 1 bit error for a page
287 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200288static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +0200289 u_char *read_ecc, u_char *isnull)
290{
291 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200292 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +0200293 unsigned int ecc_status;
294 unsigned int ecc_word, ecc_bit;
295
296 /* get the status from the Status Register */
297 ecc_status = ecc_readl(host->ecc, SR);
298
299 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200300 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +0200301 return 0;
302
303 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200304 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +0200305 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200306 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +0200307 ecc_word >>= 4;
308
309 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200310 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +0200311 /* check if it is a freshly erased block
312 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200313 if ((ecc_bit == ATMEL_ECC_BITADDR)
314 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +0200315 /* the block has just been erased, return OK */
316 return 0;
317 }
318 /* it doesn't seems to be a freshly
319 * erased block.
320 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200321 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +0200322 " Unable to correct.\n");
323 return -EIO;
324 }
325
326 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200327 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +0200328 /* there's nothing much to do here.
329 * the bit error is on the ECC itself.
330 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200331 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +0200332 " Nothing to correct\n");
333 return 0;
334 }
335
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200336 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +0200337 " (word offset in the page :"
338 " 0x%x bit offset : 0x%x)\n",
339 ecc_word, ecc_bit);
340 /* correct the error */
341 if (nand_chip->options & NAND_BUSWIDTH_16) {
342 /* 16 bits words */
343 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
344 } else {
345 /* 8 bits words */
346 dat[ecc_word] ^= (1 << ecc_bit);
347 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200348 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +0200349 return 1;
350}
351
352/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700353 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +0200354 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -0700355static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
356{
357 if (cpu_is_at32ap7000()) {
358 struct nand_chip *nand_chip = mtd->priv;
359 struct atmel_nand_host *host = nand_chip->priv;
360 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
361 }
362}
Richard Genoud77f54922008-04-23 19:51:14 +0200363
Andrew Victor693ef662007-05-03 08:16:44 +0200364#ifdef CONFIG_MTD_PARTITIONS
Atsushi Nemoto52f83012008-03-30 21:59:37 +0900365static const char *part_probes[] = { "cmdlinepart", NULL };
Andrew Victor693ef662007-05-03 08:16:44 +0200366#endif
367
Andrew Victor42cb1402006-10-19 18:24:35 +0200368/*
369 * Probe for the NAND device.
370 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200371static int __init atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +0200372{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200373 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +0200374 struct mtd_info *mtd;
375 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +0200376 struct resource *regs;
377 struct resource *mem;
Andrew Victor42cb1402006-10-19 18:24:35 +0200378 int res;
379
380#ifdef CONFIG_MTD_PARTITIONS
381 struct mtd_partition *partitions = NULL;
382 int num_partitions = 0;
383#endif
384
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200385 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
386 if (!mem) {
387 printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
388 return -ENXIO;
389 }
390
Andrew Victor42cb1402006-10-19 18:24:35 +0200391 /* Allocate memory for the device structure (and zero it) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200392 host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
Andrew Victor42cb1402006-10-19 18:24:35 +0200393 if (!host) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200394 printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +0200395 return -ENOMEM;
396 }
397
Richard Genoud77f54922008-04-23 19:51:14 +0200398 host->io_base = ioremap(mem->start, mem->end - mem->start + 1);
Andrew Victor42cb1402006-10-19 18:24:35 +0200399 if (host->io_base == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200400 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200401 res = -EIO;
402 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +0200403 }
404
405 mtd = &host->mtd;
406 nand_chip = &host->nand_chip;
407 host->board = pdev->dev.platform_data;
Richard Genoud77f54922008-04-23 19:51:14 +0200408 host->dev = &pdev->dev;
Andrew Victor42cb1402006-10-19 18:24:35 +0200409
410 nand_chip->priv = host; /* link the private data structures */
411 mtd->priv = nand_chip;
412 mtd->owner = THIS_MODULE;
413
414 /* Set address of NAND IO lines */
415 nand_chip->IO_ADDR_R = host->io_base;
416 nand_chip->IO_ADDR_W = host->io_base;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200417 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Ivan Kutena4265f82007-05-24 14:35:58 +0300418
419 if (host->board->rdy_pin)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200420 nand_chip->dev_ready = atmel_nand_device_ready;
Ivan Kutena4265f82007-05-24 14:35:58 +0300421
Richard Genoud77f54922008-04-23 19:51:14 +0200422 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
423 if (!regs && hard_ecc) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200424 printk(KERN_ERR "atmel_nand: can't get I/O resource "
Richard Genoud77f54922008-04-23 19:51:14 +0200425 "regs\nFalling back on software ECC\n");
426 }
427
Andrew Victor42cb1402006-10-19 18:24:35 +0200428 nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
Richard Genoud77f54922008-04-23 19:51:14 +0200429 if (no_ecc)
430 nand_chip->ecc.mode = NAND_ECC_NONE;
431 if (hard_ecc && regs) {
432 host->ecc = ioremap(regs->start, regs->end - regs->start + 1);
433 if (host->ecc == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200434 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Richard Genoud77f54922008-04-23 19:51:14 +0200435 res = -EIO;
436 goto err_ecc_ioremap;
437 }
Richard Genoud3fc23892008-10-12 08:42:28 +0200438 nand_chip->ecc.mode = NAND_ECC_HW;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200439 nand_chip->ecc.calculate = atmel_nand_calculate;
440 nand_chip->ecc.correct = atmel_nand_correct;
441 nand_chip->ecc.hwctl = atmel_nand_hwctl;
442 nand_chip->ecc.read_page = atmel_nand_read_page;
Richard Genoud77f54922008-04-23 19:51:14 +0200443 nand_chip->ecc.bytes = 4;
Richard Genoud77f54922008-04-23 19:51:14 +0200444 }
445
Andrew Victor42cb1402006-10-19 18:24:35 +0200446 nand_chip->chip_delay = 20; /* 20us command delay time */
447
David Brownell23a346c2008-07-03 23:40:16 -0700448 if (host->board->bus_width_16) { /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +0200449 nand_chip->options |= NAND_BUSWIDTH_16;
David Brownell23a346c2008-07-03 23:40:16 -0700450 nand_chip->read_buf = atmel_read_buf16;
451 nand_chip->write_buf = atmel_write_buf16;
452 } else {
453 nand_chip->read_buf = atmel_read_buf;
454 nand_chip->write_buf = atmel_write_buf;
455 }
Andrew Victordd11b8c2006-12-08 13:49:42 +0200456
Andrew Victor42cb1402006-10-19 18:24:35 +0200457 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200458 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200459
460 if (host->board->det_pin) {
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200461 if (gpio_get_value(host->board->det_pin)) {
David Woodhouse90574d02008-06-07 08:49:00 +0100462 printk("No SmartMedia card inserted.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +0200463 res = ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200464 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +0200465 }
466 }
467
Richard Genoud77f54922008-04-23 19:51:14 +0200468 /* first scan to find the device and get the page size */
469 if (nand_scan_ident(mtd, 1)) {
470 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200471 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +0200472 }
473
Richard Genoud3fc23892008-10-12 08:42:28 +0200474 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Richard Genoud77f54922008-04-23 19:51:14 +0200475 /* ECC is calculated for the whole page (1 step) */
476 nand_chip->ecc.size = mtd->writesize;
477
478 /* set ECC page size and oob layout */
479 switch (mtd->writesize) {
480 case 512:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200481 nand_chip->ecc.layout = &atmel_oobinfo_small;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200482 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
Richard Genoud77f54922008-04-23 19:51:14 +0200483 break;
484 case 1024:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200485 nand_chip->ecc.layout = &atmel_oobinfo_large;
486 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
Richard Genoud77f54922008-04-23 19:51:14 +0200487 break;
488 case 2048:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200489 nand_chip->ecc.layout = &atmel_oobinfo_large;
490 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
Richard Genoud77f54922008-04-23 19:51:14 +0200491 break;
492 case 4096:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200493 nand_chip->ecc.layout = &atmel_oobinfo_large;
494 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
Richard Genoud77f54922008-04-23 19:51:14 +0200495 break;
496 default:
497 /* page size not handled by HW ECC */
498 /* switching back to soft ECC */
499 nand_chip->ecc.mode = NAND_ECC_SOFT;
500 nand_chip->ecc.calculate = NULL;
501 nand_chip->ecc.correct = NULL;
502 nand_chip->ecc.hwctl = NULL;
503 nand_chip->ecc.read_page = NULL;
504 nand_chip->ecc.postpad = 0;
505 nand_chip->ecc.prepad = 0;
506 nand_chip->ecc.bytes = 0;
507 break;
508 }
509 }
510
511 /* second phase scan */
512 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +0200513 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200514 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +0200515 }
516
517#ifdef CONFIG_MTD_PARTITIONS
Andrew Victor693ef662007-05-03 08:16:44 +0200518#ifdef CONFIG_MTD_CMDLINE_PARTS
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200519 mtd->name = "atmel_nand";
Atsushi Nemoto842b1a102008-01-29 22:28:22 +0900520 num_partitions = parse_mtd_partitions(mtd, part_probes,
521 &partitions, 0);
Andrew Victor693ef662007-05-03 08:16:44 +0200522#endif
Atsushi Nemoto842b1a102008-01-29 22:28:22 +0900523 if (num_partitions <= 0 && host->board->partition_info)
524 partitions = host->board->partition_info(mtd->size,
525 &num_partitions);
Andrew Victor42cb1402006-10-19 18:24:35 +0200526
527 if ((!partitions) || (num_partitions == 0)) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200528 printk(KERN_ERR "atmel_nand: No parititions defined, or unsupported device.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +0200529 res = ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200530 goto err_no_partitions;
Andrew Victor42cb1402006-10-19 18:24:35 +0200531 }
532
533 res = add_mtd_partitions(mtd, partitions, num_partitions);
534#else
535 res = add_mtd_device(mtd);
536#endif
537
538 if (!res)
539 return res;
540
Richard Genoud77f54922008-04-23 19:51:14 +0200541#ifdef CONFIG_MTD_PARTITIONS
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200542err_no_partitions:
Richard Genoud77f54922008-04-23 19:51:14 +0200543#endif
Andrew Victor42cb1402006-10-19 18:24:35 +0200544 nand_release(mtd);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200545err_scan_tail:
546err_scan_ident:
547err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200548 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200549 platform_set_drvdata(pdev, NULL);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200550 if (host->ecc)
551 iounmap(host->ecc);
552err_ecc_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +0200553 iounmap(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200554err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +0200555 kfree(host);
556 return res;
557}
558
559/*
560 * Remove a NAND device.
561 */
David Brownell23a346c2008-07-03 23:40:16 -0700562static int __exit atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +0200563{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200564 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +0200565 struct mtd_info *mtd = &host->mtd;
566
567 nand_release(mtd);
568
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200569 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +0200570
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +0200571 if (host->ecc)
572 iounmap(host->ecc);
Andrew Victor42cb1402006-10-19 18:24:35 +0200573 iounmap(host->io_base);
574 kfree(host);
575
576 return 0;
577}
578
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200579static struct platform_driver atmel_nand_driver = {
David Brownell23a346c2008-07-03 23:40:16 -0700580 .remove = __exit_p(atmel_nand_remove),
Andrew Victor42cb1402006-10-19 18:24:35 +0200581 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200582 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +0200583 .owner = THIS_MODULE,
584 },
585};
586
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200587static int __init atmel_nand_init(void)
Andrew Victor42cb1402006-10-19 18:24:35 +0200588{
David Brownell23a346c2008-07-03 23:40:16 -0700589 return platform_driver_probe(&atmel_nand_driver, atmel_nand_probe);
Andrew Victor42cb1402006-10-19 18:24:35 +0200590}
591
592
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200593static void __exit atmel_nand_exit(void)
Andrew Victor42cb1402006-10-19 18:24:35 +0200594{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200595 platform_driver_unregister(&atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +0200596}
597
598
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200599module_init(atmel_nand_init);
600module_exit(atmel_nand_exit);
Andrew Victor42cb1402006-10-19 18:24:35 +0200601
602MODULE_LICENSE("GPL");
603MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +0200604MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200605MODULE_ALIAS("platform:atmel_nand");