blob: fbe841467175949456dd31aff4df968c060f2291 [file] [log] [blame]
Vimal Singh67ce04b2009-05-12 13:47:03 -07001/*
2 * Copyright © 2004 Texas Instruments, Jian Zhang <jzhang@ti.com>
3 * Copyright © 2004 Micron Technology Inc.
4 * Copyright © 2004 David Brownell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/platform_device.h>
12#include <linux/dma-mapping.h>
13#include <linux/delay.h>
Sukumar Ghorai4e070372011-01-28 15:42:06 +053014#include <linux/interrupt.h>
vimal singhc276aca2009-06-27 11:07:06 +053015#include <linux/jiffies.h>
16#include <linux/sched.h>
Vimal Singh67ce04b2009-05-12 13:47:03 -070017#include <linux/mtd/mtd.h>
18#include <linux/mtd/nand.h>
19#include <linux/mtd/partitions.h>
20#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Vimal Singh67ce04b2009-05-12 13:47:03 -070022
Tony Lindgrence491cf2009-10-20 09:40:47 -070023#include <plat/dma.h>
24#include <plat/gpmc.h>
25#include <plat/nand.h>
Vimal Singh67ce04b2009-05-12 13:47:03 -070026
Vimal Singh67ce04b2009-05-12 13:47:03 -070027#define DRIVER_NAME "omap2-nand"
Sukumar Ghorai4e070372011-01-28 15:42:06 +053028#define OMAP_NAND_TIMEOUT_MS 5000
Vimal Singh67ce04b2009-05-12 13:47:03 -070029
Vimal Singh67ce04b2009-05-12 13:47:03 -070030#define NAND_Ecc_P1e (1 << 0)
31#define NAND_Ecc_P2e (1 << 1)
32#define NAND_Ecc_P4e (1 << 2)
33#define NAND_Ecc_P8e (1 << 3)
34#define NAND_Ecc_P16e (1 << 4)
35#define NAND_Ecc_P32e (1 << 5)
36#define NAND_Ecc_P64e (1 << 6)
37#define NAND_Ecc_P128e (1 << 7)
38#define NAND_Ecc_P256e (1 << 8)
39#define NAND_Ecc_P512e (1 << 9)
40#define NAND_Ecc_P1024e (1 << 10)
41#define NAND_Ecc_P2048e (1 << 11)
42
43#define NAND_Ecc_P1o (1 << 16)
44#define NAND_Ecc_P2o (1 << 17)
45#define NAND_Ecc_P4o (1 << 18)
46#define NAND_Ecc_P8o (1 << 19)
47#define NAND_Ecc_P16o (1 << 20)
48#define NAND_Ecc_P32o (1 << 21)
49#define NAND_Ecc_P64o (1 << 22)
50#define NAND_Ecc_P128o (1 << 23)
51#define NAND_Ecc_P256o (1 << 24)
52#define NAND_Ecc_P512o (1 << 25)
53#define NAND_Ecc_P1024o (1 << 26)
54#define NAND_Ecc_P2048o (1 << 27)
55
56#define TF(value) (value ? 1 : 0)
57
58#define P2048e(a) (TF(a & NAND_Ecc_P2048e) << 0)
59#define P2048o(a) (TF(a & NAND_Ecc_P2048o) << 1)
60#define P1e(a) (TF(a & NAND_Ecc_P1e) << 2)
61#define P1o(a) (TF(a & NAND_Ecc_P1o) << 3)
62#define P2e(a) (TF(a & NAND_Ecc_P2e) << 4)
63#define P2o(a) (TF(a & NAND_Ecc_P2o) << 5)
64#define P4e(a) (TF(a & NAND_Ecc_P4e) << 6)
65#define P4o(a) (TF(a & NAND_Ecc_P4o) << 7)
66
67#define P8e(a) (TF(a & NAND_Ecc_P8e) << 0)
68#define P8o(a) (TF(a & NAND_Ecc_P8o) << 1)
69#define P16e(a) (TF(a & NAND_Ecc_P16e) << 2)
70#define P16o(a) (TF(a & NAND_Ecc_P16o) << 3)
71#define P32e(a) (TF(a & NAND_Ecc_P32e) << 4)
72#define P32o(a) (TF(a & NAND_Ecc_P32o) << 5)
73#define P64e(a) (TF(a & NAND_Ecc_P64e) << 6)
74#define P64o(a) (TF(a & NAND_Ecc_P64o) << 7)
75
76#define P128e(a) (TF(a & NAND_Ecc_P128e) << 0)
77#define P128o(a) (TF(a & NAND_Ecc_P128o) << 1)
78#define P256e(a) (TF(a & NAND_Ecc_P256e) << 2)
79#define P256o(a) (TF(a & NAND_Ecc_P256o) << 3)
80#define P512e(a) (TF(a & NAND_Ecc_P512e) << 4)
81#define P512o(a) (TF(a & NAND_Ecc_P512o) << 5)
82#define P1024e(a) (TF(a & NAND_Ecc_P1024e) << 6)
83#define P1024o(a) (TF(a & NAND_Ecc_P1024o) << 7)
84
85#define P8e_s(a) (TF(a & NAND_Ecc_P8e) << 0)
86#define P8o_s(a) (TF(a & NAND_Ecc_P8o) << 1)
87#define P16e_s(a) (TF(a & NAND_Ecc_P16e) << 2)
88#define P16o_s(a) (TF(a & NAND_Ecc_P16o) << 3)
89#define P1e_s(a) (TF(a & NAND_Ecc_P1e) << 4)
90#define P1o_s(a) (TF(a & NAND_Ecc_P1o) << 5)
91#define P2e_s(a) (TF(a & NAND_Ecc_P2e) << 6)
92#define P2o_s(a) (TF(a & NAND_Ecc_P2o) << 7)
93
94#define P4e_s(a) (TF(a & NAND_Ecc_P4e) << 0)
95#define P4o_s(a) (TF(a & NAND_Ecc_P4o) << 1)
96
97#ifdef CONFIG_MTD_PARTITIONS
98static const char *part_probes[] = { "cmdlinepart", NULL };
99#endif
100
101struct omap_nand_info {
102 struct nand_hw_control controller;
103 struct omap_nand_platform_data *pdata;
104 struct mtd_info mtd;
105 struct mtd_partition *parts;
106 struct nand_chip nand;
107 struct platform_device *pdev;
108
109 int gpmc_cs;
110 unsigned long phys_base;
vimal singhdfe32892009-07-13 16:29:16 +0530111 struct completion comp;
112 int dma_ch;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530113 int gpmc_irq;
114 enum {
115 OMAP_NAND_IO_READ = 0, /* read */
116 OMAP_NAND_IO_WRITE, /* write */
117 } iomode;
118 u_char *buf;
119 int buf_len;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700120};
121
122/**
Vimal Singh67ce04b2009-05-12 13:47:03 -0700123 * omap_hwcontrol - hardware specific access to control-lines
124 * @mtd: MTD device structure
125 * @cmd: command to device
126 * @ctrl:
127 * NAND_NCE: bit 0 -> don't care
128 * NAND_CLE: bit 1 -> Command Latch
129 * NAND_ALE: bit 2 -> Address Latch
130 *
131 * NOTE: boards may use different bits for these!!
132 */
133static void omap_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
134{
135 struct omap_nand_info *info = container_of(mtd,
136 struct omap_nand_info, mtd);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700137
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000138 if (cmd != NAND_CMD_NONE) {
139 if (ctrl & NAND_CLE)
140 gpmc_nand_write(info->gpmc_cs, GPMC_NAND_COMMAND, cmd);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700141
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000142 else if (ctrl & NAND_ALE)
143 gpmc_nand_write(info->gpmc_cs, GPMC_NAND_ADDRESS, cmd);
144
145 else /* NAND_NCE */
146 gpmc_nand_write(info->gpmc_cs, GPMC_NAND_DATA, cmd);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700147 }
Vimal Singh67ce04b2009-05-12 13:47:03 -0700148}
149
150/**
vimal singh59e9c5a2009-07-13 16:26:24 +0530151 * omap_read_buf8 - read data from NAND controller into buffer
152 * @mtd: MTD device structure
153 * @buf: buffer to store date
154 * @len: number of bytes to read
155 */
156static void omap_read_buf8(struct mtd_info *mtd, u_char *buf, int len)
157{
158 struct nand_chip *nand = mtd->priv;
159
160 ioread8_rep(nand->IO_ADDR_R, buf, len);
161}
162
163/**
164 * omap_write_buf8 - write buffer to NAND controller
165 * @mtd: MTD device structure
166 * @buf: data buffer
167 * @len: number of bytes to write
168 */
169static void omap_write_buf8(struct mtd_info *mtd, const u_char *buf, int len)
170{
171 struct omap_nand_info *info = container_of(mtd,
172 struct omap_nand_info, mtd);
173 u_char *p = (u_char *)buf;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000174 u32 status = 0;
vimal singh59e9c5a2009-07-13 16:26:24 +0530175
176 while (len--) {
177 iowrite8(*p++, info->nand.IO_ADDR_W);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000178 /* wait until buffer is available for write */
179 do {
180 status = gpmc_read_status(GPMC_STATUS_BUFFER);
181 } while (!status);
vimal singh59e9c5a2009-07-13 16:26:24 +0530182 }
183}
184
185/**
Vimal Singh67ce04b2009-05-12 13:47:03 -0700186 * omap_read_buf16 - read data from NAND controller into buffer
187 * @mtd: MTD device structure
188 * @buf: buffer to store date
189 * @len: number of bytes to read
190 */
191static void omap_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
192{
193 struct nand_chip *nand = mtd->priv;
194
vimal singh59e9c5a2009-07-13 16:26:24 +0530195 ioread16_rep(nand->IO_ADDR_R, buf, len / 2);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700196}
197
198/**
199 * omap_write_buf16 - write buffer to NAND controller
200 * @mtd: MTD device structure
201 * @buf: data buffer
202 * @len: number of bytes to write
203 */
204static void omap_write_buf16(struct mtd_info *mtd, const u_char * buf, int len)
205{
206 struct omap_nand_info *info = container_of(mtd,
207 struct omap_nand_info, mtd);
208 u16 *p = (u16 *) buf;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000209 u32 status = 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700210 /* FIXME try bursts of writesw() or DMA ... */
211 len >>= 1;
212
213 while (len--) {
vimal singh59e9c5a2009-07-13 16:26:24 +0530214 iowrite16(*p++, info->nand.IO_ADDR_W);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000215 /* wait until buffer is available for write */
216 do {
217 status = gpmc_read_status(GPMC_STATUS_BUFFER);
218 } while (!status);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700219 }
220}
vimal singh59e9c5a2009-07-13 16:26:24 +0530221
222/**
223 * omap_read_buf_pref - read data from NAND controller into buffer
224 * @mtd: MTD device structure
225 * @buf: buffer to store date
226 * @len: number of bytes to read
227 */
228static void omap_read_buf_pref(struct mtd_info *mtd, u_char *buf, int len)
229{
230 struct omap_nand_info *info = container_of(mtd,
231 struct omap_nand_info, mtd);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000232 uint32_t r_count = 0;
vimal singh59e9c5a2009-07-13 16:26:24 +0530233 int ret = 0;
234 u32 *p = (u32 *)buf;
235
236 /* take care of subpage reads */
Vimal Singhc3341d02010-01-07 12:16:26 +0530237 if (len % 4) {
238 if (info->nand.options & NAND_BUSWIDTH_16)
239 omap_read_buf16(mtd, buf, len % 4);
240 else
241 omap_read_buf8(mtd, buf, len % 4);
242 p = (u32 *) (buf + len % 4);
243 len -= len % 4;
vimal singh59e9c5a2009-07-13 16:26:24 +0530244 }
vimal singh59e9c5a2009-07-13 16:26:24 +0530245
246 /* configure and start prefetch transfer */
247 ret = gpmc_prefetch_enable(info->gpmc_cs, 0x0, len, 0x0);
248 if (ret) {
249 /* PFPW engine is busy, use cpu copy method */
250 if (info->nand.options & NAND_BUSWIDTH_16)
251 omap_read_buf16(mtd, buf, len);
252 else
253 omap_read_buf8(mtd, buf, len);
254 } else {
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000255 p = (u32 *) buf;
vimal singh59e9c5a2009-07-13 16:26:24 +0530256 do {
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000257 r_count = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT);
258 r_count = r_count >> 2;
259 ioread32_rep(info->nand.IO_ADDR_R, p, r_count);
vimal singh59e9c5a2009-07-13 16:26:24 +0530260 p += r_count;
261 len -= r_count << 2;
262 } while (len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530263 /* disable and stop the PFPW engine */
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000264 gpmc_prefetch_reset(info->gpmc_cs);
vimal singh59e9c5a2009-07-13 16:26:24 +0530265 }
266}
267
268/**
269 * omap_write_buf_pref - write buffer to NAND controller
270 * @mtd: MTD device structure
271 * @buf: data buffer
272 * @len: number of bytes to write
273 */
274static void omap_write_buf_pref(struct mtd_info *mtd,
275 const u_char *buf, int len)
276{
277 struct omap_nand_info *info = container_of(mtd,
278 struct omap_nand_info, mtd);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530279 uint32_t w_count = 0;
vimal singh59e9c5a2009-07-13 16:26:24 +0530280 int i = 0, ret = 0;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000281 u16 *p;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530282 unsigned long tim, limit;
vimal singh59e9c5a2009-07-13 16:26:24 +0530283
284 /* take care of subpage writes */
285 if (len % 2 != 0) {
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000286 writeb(*buf, info->nand.IO_ADDR_W);
vimal singh59e9c5a2009-07-13 16:26:24 +0530287 p = (u16 *)(buf + 1);
288 len--;
289 }
290
291 /* configure and start prefetch transfer */
292 ret = gpmc_prefetch_enable(info->gpmc_cs, 0x0, len, 0x1);
293 if (ret) {
294 /* PFPW engine is busy, use cpu copy method */
295 if (info->nand.options & NAND_BUSWIDTH_16)
296 omap_write_buf16(mtd, buf, len);
297 else
298 omap_write_buf8(mtd, buf, len);
299 } else {
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000300 p = (u16 *) buf;
301 while (len) {
302 w_count = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT);
303 w_count = w_count >> 1;
vimal singh59e9c5a2009-07-13 16:26:24 +0530304 for (i = 0; (i < w_count) && len; i++, len -= 2)
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000305 iowrite16(*p++, info->nand.IO_ADDR_W);
vimal singh59e9c5a2009-07-13 16:26:24 +0530306 }
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000307 /* wait for data to flushed-out before reset the prefetch */
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530308 tim = 0;
309 limit = (loops_per_jiffy *
310 msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
311 while (gpmc_read_status(GPMC_PREFETCH_COUNT) && (tim++ < limit))
312 cpu_relax();
313
vimal singh59e9c5a2009-07-13 16:26:24 +0530314 /* disable and stop the PFPW engine */
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000315 gpmc_prefetch_reset(info->gpmc_cs);
vimal singh59e9c5a2009-07-13 16:26:24 +0530316 }
317}
318
vimal singhdfe32892009-07-13 16:29:16 +0530319/*
320 * omap_nand_dma_cb: callback on the completion of dma transfer
321 * @lch: logical channel
322 * @ch_satuts: channel status
323 * @data: pointer to completion data structure
324 */
325static void omap_nand_dma_cb(int lch, u16 ch_status, void *data)
326{
327 complete((struct completion *) data);
328}
329
330/*
331 * omap_nand_dma_transfer: configer and start dma transfer
332 * @mtd: MTD device structure
333 * @addr: virtual address in RAM of source/destination
334 * @len: number of data bytes to be transferred
335 * @is_write: flag for read/write operation
336 */
337static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
338 unsigned int len, int is_write)
339{
340 struct omap_nand_info *info = container_of(mtd,
341 struct omap_nand_info, mtd);
vimal singhdfe32892009-07-13 16:29:16 +0530342 enum dma_data_direction dir = is_write ? DMA_TO_DEVICE :
343 DMA_FROM_DEVICE;
344 dma_addr_t dma_addr;
345 int ret;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530346 unsigned long tim, limit;
vimal singhdfe32892009-07-13 16:29:16 +0530347
348 /* The fifo depth is 64 bytes. We have a sync at each frame and frame
349 * length is 64 bytes.
350 */
351 int buf_len = len >> 6;
352
353 if (addr >= high_memory) {
354 struct page *p1;
355
356 if (((size_t)addr & PAGE_MASK) !=
357 ((size_t)(addr + len - 1) & PAGE_MASK))
358 goto out_copy;
359 p1 = vmalloc_to_page(addr);
360 if (!p1)
361 goto out_copy;
362 addr = page_address(p1) + ((size_t)addr & ~PAGE_MASK);
363 }
364
365 dma_addr = dma_map_single(&info->pdev->dev, addr, len, dir);
366 if (dma_mapping_error(&info->pdev->dev, dma_addr)) {
367 dev_err(&info->pdev->dev,
368 "Couldn't DMA map a %d byte buffer\n", len);
369 goto out_copy;
370 }
371
372 if (is_write) {
373 omap_set_dma_dest_params(info->dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
374 info->phys_base, 0, 0);
375 omap_set_dma_src_params(info->dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
376 dma_addr, 0, 0);
377 omap_set_dma_transfer_params(info->dma_ch, OMAP_DMA_DATA_TYPE_S32,
378 0x10, buf_len, OMAP_DMA_SYNC_FRAME,
379 OMAP24XX_DMA_GPMC, OMAP_DMA_DST_SYNC);
380 } else {
381 omap_set_dma_src_params(info->dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
382 info->phys_base, 0, 0);
383 omap_set_dma_dest_params(info->dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
384 dma_addr, 0, 0);
385 omap_set_dma_transfer_params(info->dma_ch, OMAP_DMA_DATA_TYPE_S32,
386 0x10, buf_len, OMAP_DMA_SYNC_FRAME,
387 OMAP24XX_DMA_GPMC, OMAP_DMA_SRC_SYNC);
388 }
389 /* configure and start prefetch transfer */
390 ret = gpmc_prefetch_enable(info->gpmc_cs, 0x1, len, is_write);
391 if (ret)
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530392 /* PFPW engine is busy, use cpu copy method */
vimal singhdfe32892009-07-13 16:29:16 +0530393 goto out_copy;
394
395 init_completion(&info->comp);
396
397 omap_start_dma(info->dma_ch);
398
399 /* setup and start DMA using dma_addr */
400 wait_for_completion(&info->comp);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530401 tim = 0;
402 limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
403 while (gpmc_read_status(GPMC_PREFETCH_COUNT) && (tim++ < limit))
404 cpu_relax();
vimal singhdfe32892009-07-13 16:29:16 +0530405
vimal singhdfe32892009-07-13 16:29:16 +0530406 /* disable and stop the PFPW engine */
Daniel J Bluemanf12f6622010-09-29 21:01:55 +0100407 gpmc_prefetch_reset(info->gpmc_cs);
vimal singhdfe32892009-07-13 16:29:16 +0530408
409 dma_unmap_single(&info->pdev->dev, dma_addr, len, dir);
410 return 0;
411
412out_copy:
413 if (info->nand.options & NAND_BUSWIDTH_16)
414 is_write == 0 ? omap_read_buf16(mtd, (u_char *) addr, len)
415 : omap_write_buf16(mtd, (u_char *) addr, len);
416 else
417 is_write == 0 ? omap_read_buf8(mtd, (u_char *) addr, len)
418 : omap_write_buf8(mtd, (u_char *) addr, len);
419 return 0;
420}
vimal singhdfe32892009-07-13 16:29:16 +0530421
422/**
423 * omap_read_buf_dma_pref - read data from NAND controller into buffer
424 * @mtd: MTD device structure
425 * @buf: buffer to store date
426 * @len: number of bytes to read
427 */
428static void omap_read_buf_dma_pref(struct mtd_info *mtd, u_char *buf, int len)
429{
430 if (len <= mtd->oobsize)
431 omap_read_buf_pref(mtd, buf, len);
432 else
433 /* start transfer in DMA mode */
434 omap_nand_dma_transfer(mtd, buf, len, 0x0);
435}
436
437/**
438 * omap_write_buf_dma_pref - write buffer to NAND controller
439 * @mtd: MTD device structure
440 * @buf: data buffer
441 * @len: number of bytes to write
442 */
443static void omap_write_buf_dma_pref(struct mtd_info *mtd,
444 const u_char *buf, int len)
445{
446 if (len <= mtd->oobsize)
447 omap_write_buf_pref(mtd, buf, len);
448 else
449 /* start transfer in DMA mode */
Vimal Singhbdaefc42010-01-05 12:49:24 +0530450 omap_nand_dma_transfer(mtd, (u_char *) buf, len, 0x1);
vimal singhdfe32892009-07-13 16:29:16 +0530451}
452
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530453/*
454 * omap_nand_irq - GMPC irq handler
455 * @this_irq: gpmc irq number
456 * @dev: omap_nand_info structure pointer is passed here
457 */
458static irqreturn_t omap_nand_irq(int this_irq, void *dev)
459{
460 struct omap_nand_info *info = (struct omap_nand_info *) dev;
461 u32 bytes;
462 u32 irq_stat;
463
464 irq_stat = gpmc_read_status(GPMC_GET_IRQ_STATUS);
465 bytes = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT);
466 bytes = bytes & 0xFFFC; /* io in multiple of 4 bytes */
467 if (info->iomode == OMAP_NAND_IO_WRITE) { /* checks for write io */
468 if (irq_stat & 0x2)
469 goto done;
470
471 if (info->buf_len && (info->buf_len < bytes))
472 bytes = info->buf_len;
473 else if (!info->buf_len)
474 bytes = 0;
475 iowrite32_rep(info->nand.IO_ADDR_W,
476 (u32 *)info->buf, bytes >> 2);
477 info->buf = info->buf + bytes;
478 info->buf_len -= bytes;
479
480 } else {
481 ioread32_rep(info->nand.IO_ADDR_R,
482 (u32 *)info->buf, bytes >> 2);
483 info->buf = info->buf + bytes;
484
485 if (irq_stat & 0x2)
486 goto done;
487 }
488 gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, irq_stat);
489
490 return IRQ_HANDLED;
491
492done:
493 complete(&info->comp);
494 /* disable irq */
495 gpmc_cs_configure(info->gpmc_cs, GPMC_ENABLE_IRQ, 0);
496
497 /* clear status */
498 gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, irq_stat);
499
500 return IRQ_HANDLED;
501}
502
503/*
504 * omap_read_buf_irq_pref - read data from NAND controller into buffer
505 * @mtd: MTD device structure
506 * @buf: buffer to store date
507 * @len: number of bytes to read
508 */
509static void omap_read_buf_irq_pref(struct mtd_info *mtd, u_char *buf, int len)
510{
511 struct omap_nand_info *info = container_of(mtd,
512 struct omap_nand_info, mtd);
513 int ret = 0;
514
515 if (len <= mtd->oobsize) {
516 omap_read_buf_pref(mtd, buf, len);
517 return;
518 }
519
520 info->iomode = OMAP_NAND_IO_READ;
521 info->buf = buf;
522 init_completion(&info->comp);
523
524 /* configure and start prefetch transfer */
525 ret = gpmc_prefetch_enable(info->gpmc_cs, 0x0, len, 0x0);
526 if (ret)
527 /* PFPW engine is busy, use cpu copy method */
528 goto out_copy;
529
530 info->buf_len = len;
531 /* enable irq */
532 gpmc_cs_configure(info->gpmc_cs, GPMC_ENABLE_IRQ,
533 (GPMC_IRQ_FIFOEVENTENABLE | GPMC_IRQ_COUNT_EVENT));
534
535 /* waiting for read to complete */
536 wait_for_completion(&info->comp);
537
538 /* disable and stop the PFPW engine */
539 gpmc_prefetch_reset(info->gpmc_cs);
540 return;
541
542out_copy:
543 if (info->nand.options & NAND_BUSWIDTH_16)
544 omap_read_buf16(mtd, buf, len);
545 else
546 omap_read_buf8(mtd, buf, len);
547}
548
549/*
550 * omap_write_buf_irq_pref - write buffer to NAND controller
551 * @mtd: MTD device structure
552 * @buf: data buffer
553 * @len: number of bytes to write
554 */
555static void omap_write_buf_irq_pref(struct mtd_info *mtd,
556 const u_char *buf, int len)
557{
558 struct omap_nand_info *info = container_of(mtd,
559 struct omap_nand_info, mtd);
560 int ret = 0;
561 unsigned long tim, limit;
562
563 if (len <= mtd->oobsize) {
564 omap_write_buf_pref(mtd, buf, len);
565 return;
566 }
567
568 info->iomode = OMAP_NAND_IO_WRITE;
569 info->buf = (u_char *) buf;
570 init_completion(&info->comp);
571
572 /* configure and start prefetch transfer */
573 ret = gpmc_prefetch_enable(info->gpmc_cs, 0x0, len, 0x1);
574 if (ret)
575 /* PFPW engine is busy, use cpu copy method */
576 goto out_copy;
577
578 info->buf_len = len;
579 /* enable irq */
580 gpmc_cs_configure(info->gpmc_cs, GPMC_ENABLE_IRQ,
581 (GPMC_IRQ_FIFOEVENTENABLE | GPMC_IRQ_COUNT_EVENT));
582
583 /* waiting for write to complete */
584 wait_for_completion(&info->comp);
585 /* wait for data to flushed-out before reset the prefetch */
586 tim = 0;
587 limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
588 while (gpmc_read_status(GPMC_PREFETCH_COUNT) && (tim++ < limit))
589 cpu_relax();
590
591 /* disable and stop the PFPW engine */
592 gpmc_prefetch_reset(info->gpmc_cs);
593 return;
594
595out_copy:
596 if (info->nand.options & NAND_BUSWIDTH_16)
597 omap_write_buf16(mtd, buf, len);
598 else
599 omap_write_buf8(mtd, buf, len);
600}
601
Vimal Singh67ce04b2009-05-12 13:47:03 -0700602/**
603 * omap_verify_buf - Verify chip data against buffer
604 * @mtd: MTD device structure
605 * @buf: buffer containing the data to compare
606 * @len: number of bytes to compare
607 */
608static int omap_verify_buf(struct mtd_info *mtd, const u_char * buf, int len)
609{
610 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
611 mtd);
612 u16 *p = (u16 *) buf;
613
614 len >>= 1;
615 while (len--) {
616 if (*p++ != cpu_to_le16(readw(info->nand.IO_ADDR_R)))
617 return -EFAULT;
618 }
619
620 return 0;
621}
622
623#ifdef CONFIG_MTD_NAND_OMAP_HWECC
Vimal Singh67ce04b2009-05-12 13:47:03 -0700624
625/**
626 * gen_true_ecc - This function will generate true ECC value
627 * @ecc_buf: buffer to store ecc code
628 *
629 * This generated true ECC value can be used when correcting
630 * data read from NAND flash memory core
631 */
632static void gen_true_ecc(u8 *ecc_buf)
633{
634 u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
635 ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
636
637 ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
638 P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
639 ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
640 P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
641 ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
642 P1e(tmp) | P2048o(tmp) | P2048e(tmp));
643}
644
645/**
646 * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data
647 * @ecc_data1: ecc code from nand spare area
648 * @ecc_data2: ecc code from hardware register obtained from hardware ecc
649 * @page_data: page data
650 *
651 * This function compares two ECC's and indicates if there is an error.
652 * If the error can be corrected it will be corrected to the buffer.
653 */
654static int omap_compare_ecc(u8 *ecc_data1, /* read from NAND memory */
655 u8 *ecc_data2, /* read from register */
656 u8 *page_data)
657{
658 uint i;
659 u8 tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
660 u8 comp0_bit[8], comp1_bit[8], comp2_bit[8];
661 u8 ecc_bit[24];
662 u8 ecc_sum = 0;
663 u8 find_bit = 0;
664 uint find_byte = 0;
665 int isEccFF;
666
667 isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
668
669 gen_true_ecc(ecc_data1);
670 gen_true_ecc(ecc_data2);
671
672 for (i = 0; i <= 2; i++) {
673 *(ecc_data1 + i) = ~(*(ecc_data1 + i));
674 *(ecc_data2 + i) = ~(*(ecc_data2 + i));
675 }
676
677 for (i = 0; i < 8; i++) {
678 tmp0_bit[i] = *ecc_data1 % 2;
679 *ecc_data1 = *ecc_data1 / 2;
680 }
681
682 for (i = 0; i < 8; i++) {
683 tmp1_bit[i] = *(ecc_data1 + 1) % 2;
684 *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
685 }
686
687 for (i = 0; i < 8; i++) {
688 tmp2_bit[i] = *(ecc_data1 + 2) % 2;
689 *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
690 }
691
692 for (i = 0; i < 8; i++) {
693 comp0_bit[i] = *ecc_data2 % 2;
694 *ecc_data2 = *ecc_data2 / 2;
695 }
696
697 for (i = 0; i < 8; i++) {
698 comp1_bit[i] = *(ecc_data2 + 1) % 2;
699 *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
700 }
701
702 for (i = 0; i < 8; i++) {
703 comp2_bit[i] = *(ecc_data2 + 2) % 2;
704 *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
705 }
706
707 for (i = 0; i < 6; i++)
708 ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
709
710 for (i = 0; i < 8; i++)
711 ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
712
713 for (i = 0; i < 8; i++)
714 ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
715
716 ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
717 ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
718
719 for (i = 0; i < 24; i++)
720 ecc_sum += ecc_bit[i];
721
722 switch (ecc_sum) {
723 case 0:
724 /* Not reached because this function is not called if
725 * ECC values are equal
726 */
727 return 0;
728
729 case 1:
730 /* Uncorrectable error */
731 DEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR 1\n");
732 return -1;
733
734 case 11:
735 /* UN-Correctable error */
736 DEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR B\n");
737 return -1;
738
739 case 12:
740 /* Correctable error */
741 find_byte = (ecc_bit[23] << 8) +
742 (ecc_bit[21] << 7) +
743 (ecc_bit[19] << 6) +
744 (ecc_bit[17] << 5) +
745 (ecc_bit[15] << 4) +
746 (ecc_bit[13] << 3) +
747 (ecc_bit[11] << 2) +
748 (ecc_bit[9] << 1) +
749 ecc_bit[7];
750
751 find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
752
753 DEBUG(MTD_DEBUG_LEVEL0, "Correcting single bit ECC error at "
754 "offset: %d, bit: %d\n", find_byte, find_bit);
755
756 page_data[find_byte] ^= (1 << find_bit);
757
758 return 0;
759 default:
760 if (isEccFF) {
761 if (ecc_data2[0] == 0 &&
762 ecc_data2[1] == 0 &&
763 ecc_data2[2] == 0)
764 return 0;
765 }
766 DEBUG(MTD_DEBUG_LEVEL0, "UNCORRECTED_ERROR default\n");
767 return -1;
768 }
769}
770
771/**
772 * omap_correct_data - Compares the ECC read with HW generated ECC
773 * @mtd: MTD device structure
774 * @dat: page data
775 * @read_ecc: ecc read from nand flash
776 * @calc_ecc: ecc read from HW ECC registers
777 *
778 * Compares the ecc read from nand spare area with ECC registers values
779 * and if ECC's mismached, it will call 'omap_compare_ecc' for error detection
780 * and correction.
781 */
782static int omap_correct_data(struct mtd_info *mtd, u_char *dat,
783 u_char *read_ecc, u_char *calc_ecc)
784{
785 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
786 mtd);
787 int blockCnt = 0, i = 0, ret = 0;
788
789 /* Ex NAND_ECC_HW12_2048 */
790 if ((info->nand.ecc.mode == NAND_ECC_HW) &&
791 (info->nand.ecc.size == 2048))
792 blockCnt = 4;
793 else
794 blockCnt = 1;
795
796 for (i = 0; i < blockCnt; i++) {
797 if (memcmp(read_ecc, calc_ecc, 3) != 0) {
798 ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
799 if (ret < 0)
800 return ret;
801 }
802 read_ecc += 3;
803 calc_ecc += 3;
804 dat += 512;
805 }
806 return 0;
807}
808
809/**
810 * omap_calcuate_ecc - Generate non-inverted ECC bytes.
811 * @mtd: MTD device structure
812 * @dat: The pointer to data on which ecc is computed
813 * @ecc_code: The ecc_code buffer
814 *
815 * Using noninverted ECC can be considered ugly since writing a blank
816 * page ie. padding will clear the ECC bytes. This is no problem as long
817 * nobody is trying to write data on the seemingly unused page. Reading
818 * an erased page will produce an ECC mismatch between generated and read
819 * ECC bytes that has to be dealt with separately.
820 */
821static int omap_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
822 u_char *ecc_code)
823{
824 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
825 mtd);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000826 return gpmc_calculate_ecc(info->gpmc_cs, dat, ecc_code);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700827}
828
829/**
830 * omap_enable_hwecc - This function enables the hardware ecc functionality
831 * @mtd: MTD device structure
832 * @mode: Read/Write mode
833 */
834static void omap_enable_hwecc(struct mtd_info *mtd, int mode)
835{
836 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
837 mtd);
838 struct nand_chip *chip = mtd->priv;
839 unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700840
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000841 gpmc_enable_hwecc(info->gpmc_cs, mode, dev_width, info->nand.ecc.size);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700842}
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000843
Vimal Singh67ce04b2009-05-12 13:47:03 -0700844#endif
845
846/**
847 * omap_wait - wait until the command is done
848 * @mtd: MTD device structure
849 * @chip: NAND Chip structure
850 *
851 * Wait function is called during Program and erase operations and
852 * the way it is called from MTD layer, we should wait till the NAND
853 * chip is ready after the programming/erase operation has completed.
854 *
855 * Erase can take up to 400ms and program up to 20ms according to
856 * general NAND and SmartMedia specs
857 */
858static int omap_wait(struct mtd_info *mtd, struct nand_chip *chip)
859{
860 struct nand_chip *this = mtd->priv;
861 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
862 mtd);
863 unsigned long timeo = jiffies;
vimal singhc276aca2009-06-27 11:07:06 +0530864 int status = NAND_STATUS_FAIL, state = this->state;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700865
866 if (state == FL_ERASING)
867 timeo += (HZ * 400) / 1000;
868 else
869 timeo += (HZ * 20) / 1000;
870
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000871 gpmc_nand_write(info->gpmc_cs,
872 GPMC_NAND_COMMAND, (NAND_CMD_STATUS & 0xFF));
Vimal Singh67ce04b2009-05-12 13:47:03 -0700873 while (time_before(jiffies, timeo)) {
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000874 status = gpmc_nand_read(info->gpmc_cs, GPMC_NAND_DATA);
vimal singhc276aca2009-06-27 11:07:06 +0530875 if (status & NAND_STATUS_READY)
Vimal Singh67ce04b2009-05-12 13:47:03 -0700876 break;
vimal singhc276aca2009-06-27 11:07:06 +0530877 cond_resched();
Vimal Singh67ce04b2009-05-12 13:47:03 -0700878 }
879 return status;
880}
881
882/**
883 * omap_dev_ready - calls the platform specific dev_ready function
884 * @mtd: MTD device structure
885 */
886static int omap_dev_ready(struct mtd_info *mtd)
887{
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000888 unsigned int val = 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700889 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
890 mtd);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700891
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000892 val = gpmc_read_status(GPMC_GET_IRQ_STATUS);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700893 if ((val & 0x100) == 0x100) {
894 /* Clear IRQ Interrupt */
895 val |= 0x100;
896 val &= ~(0x0);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000897 gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, val);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700898 } else {
899 unsigned int cnt = 0;
900 while (cnt++ < 0x1FF) {
901 if ((val & 0x100) == 0x100)
902 return 0;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000903 val = gpmc_read_status(GPMC_GET_IRQ_STATUS);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700904 }
905 }
906
907 return 1;
908}
909
910static int __devinit omap_nand_probe(struct platform_device *pdev)
911{
912 struct omap_nand_info *info;
913 struct omap_nand_platform_data *pdata;
914 int err;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700915
916 pdata = pdev->dev.platform_data;
917 if (pdata == NULL) {
918 dev_err(&pdev->dev, "platform data missing\n");
919 return -ENODEV;
920 }
921
922 info = kzalloc(sizeof(struct omap_nand_info), GFP_KERNEL);
923 if (!info)
924 return -ENOMEM;
925
926 platform_set_drvdata(pdev, info);
927
928 spin_lock_init(&info->controller.lock);
929 init_waitqueue_head(&info->controller.wq);
930
931 info->pdev = pdev;
932
933 info->gpmc_cs = pdata->cs;
Vimal Singh2f70a1e2010-02-15 10:03:33 -0800934 info->phys_base = pdata->phys_base;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700935
936 info->mtd.priv = &info->nand;
937 info->mtd.name = dev_name(&pdev->dev);
938 info->mtd.owner = THIS_MODULE;
939
Sukumar Ghoraid5ce2b62011-01-28 15:42:03 +0530940 info->nand.options = pdata->devsize;
Vimal Singh2f70a1e2010-02-15 10:03:33 -0800941 info->nand.options |= NAND_SKIP_BBTSCAN;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700942
943 /* NAND write protect off */
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000944 gpmc_cs_configure(info->gpmc_cs, GPMC_CONFIG_WP, 0);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700945
946 if (!request_mem_region(info->phys_base, NAND_IO_SIZE,
947 pdev->dev.driver->name)) {
948 err = -EBUSY;
Vimal Singh2f70a1e2010-02-15 10:03:33 -0800949 goto out_free_info;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700950 }
951
952 info->nand.IO_ADDR_R = ioremap(info->phys_base, NAND_IO_SIZE);
953 if (!info->nand.IO_ADDR_R) {
954 err = -ENOMEM;
955 goto out_release_mem_region;
956 }
vimal singh59e9c5a2009-07-13 16:26:24 +0530957
Vimal Singh67ce04b2009-05-12 13:47:03 -0700958 info->nand.controller = &info->controller;
959
960 info->nand.IO_ADDR_W = info->nand.IO_ADDR_R;
961 info->nand.cmd_ctrl = omap_hwcontrol;
962
Vimal Singh67ce04b2009-05-12 13:47:03 -0700963 /*
964 * If RDY/BSY line is connected to OMAP then use the omap ready
965 * funcrtion and the generic nand_wait function which reads the status
966 * register after monitoring the RDY/BSY line.Otherwise use a standard
967 * chip delay which is slightly more than tR (AC Timing) of the NAND
968 * device and read status register until you get a failure or success
969 */
970 if (pdata->dev_ready) {
971 info->nand.dev_ready = omap_dev_ready;
972 info->nand.chip_delay = 0;
973 } else {
974 info->nand.waitfunc = omap_wait;
975 info->nand.chip_delay = 50;
976 }
977
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +0530978 switch (pdata->xfer_type) {
979 case NAND_OMAP_PREFETCH_POLLED:
vimal singh59e9c5a2009-07-13 16:26:24 +0530980 info->nand.read_buf = omap_read_buf_pref;
981 info->nand.write_buf = omap_write_buf_pref;
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +0530982 break;
vimal singhdfe32892009-07-13 16:29:16 +0530983
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +0530984 case NAND_OMAP_POLLED:
vimal singh59e9c5a2009-07-13 16:26:24 +0530985 if (info->nand.options & NAND_BUSWIDTH_16) {
986 info->nand.read_buf = omap_read_buf16;
987 info->nand.write_buf = omap_write_buf16;
988 } else {
989 info->nand.read_buf = omap_read_buf8;
990 info->nand.write_buf = omap_write_buf8;
991 }
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +0530992 break;
993
994 case NAND_OMAP_PREFETCH_DMA:
995 err = omap_request_dma(OMAP24XX_DMA_GPMC, "NAND",
996 omap_nand_dma_cb, &info->comp, &info->dma_ch);
997 if (err < 0) {
998 info->dma_ch = -1;
999 dev_err(&pdev->dev, "DMA request failed!\n");
1000 goto out_release_mem_region;
1001 } else {
1002 omap_set_dma_dest_burst_mode(info->dma_ch,
1003 OMAP_DMA_DATA_BURST_16);
1004 omap_set_dma_src_burst_mode(info->dma_ch,
1005 OMAP_DMA_DATA_BURST_16);
1006
1007 info->nand.read_buf = omap_read_buf_dma_pref;
1008 info->nand.write_buf = omap_write_buf_dma_pref;
1009 }
1010 break;
1011
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301012 case NAND_OMAP_PREFETCH_IRQ:
1013 err = request_irq(pdata->gpmc_irq,
1014 omap_nand_irq, IRQF_SHARED, "gpmc-nand", info);
1015 if (err) {
1016 dev_err(&pdev->dev, "requesting irq(%d) error:%d",
1017 pdata->gpmc_irq, err);
1018 goto out_release_mem_region;
1019 } else {
1020 info->gpmc_irq = pdata->gpmc_irq;
1021 info->nand.read_buf = omap_read_buf_irq_pref;
1022 info->nand.write_buf = omap_write_buf_irq_pref;
1023 }
1024 break;
1025
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301026 default:
1027 dev_err(&pdev->dev,
1028 "xfer_type(%d) not supported!\n", pdata->xfer_type);
1029 err = -EINVAL;
1030 goto out_release_mem_region;
vimal singh59e9c5a2009-07-13 16:26:24 +05301031 }
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301032
vimal singh59e9c5a2009-07-13 16:26:24 +05301033 info->nand.verify_buf = omap_verify_buf;
1034
Vimal Singh67ce04b2009-05-12 13:47:03 -07001035#ifdef CONFIG_MTD_NAND_OMAP_HWECC
1036 info->nand.ecc.bytes = 3;
1037 info->nand.ecc.size = 512;
1038 info->nand.ecc.calculate = omap_calculate_ecc;
1039 info->nand.ecc.hwctl = omap_enable_hwecc;
1040 info->nand.ecc.correct = omap_correct_data;
1041 info->nand.ecc.mode = NAND_ECC_HW;
1042
Vimal Singh67ce04b2009-05-12 13:47:03 -07001043#else
1044 info->nand.ecc.mode = NAND_ECC_SOFT;
1045#endif
1046
1047 /* DIP switches on some boards change between 8 and 16 bit
1048 * bus widths for flash. Try the other width if the first try fails.
1049 */
1050 if (nand_scan(&info->mtd, 1)) {
1051 info->nand.options ^= NAND_BUSWIDTH_16;
1052 if (nand_scan(&info->mtd, 1)) {
1053 err = -ENXIO;
1054 goto out_release_mem_region;
1055 }
1056 }
1057
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301058
Vimal Singh67ce04b2009-05-12 13:47:03 -07001059#ifdef CONFIG_MTD_PARTITIONS
1060 err = parse_mtd_partitions(&info->mtd, part_probes, &info->parts, 0);
1061 if (err > 0)
1062 add_mtd_partitions(&info->mtd, info->parts, err);
1063 else if (pdata->parts)
1064 add_mtd_partitions(&info->mtd, pdata->parts, pdata->nr_parts);
1065 else
1066#endif
1067 add_mtd_device(&info->mtd);
1068
1069 platform_set_drvdata(pdev, &info->mtd);
1070
1071 return 0;
1072
1073out_release_mem_region:
1074 release_mem_region(info->phys_base, NAND_IO_SIZE);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001075out_free_info:
1076 kfree(info);
1077
1078 return err;
1079}
1080
1081static int omap_nand_remove(struct platform_device *pdev)
1082{
1083 struct mtd_info *mtd = platform_get_drvdata(pdev);
Vimal Singhf35b6ed2010-01-05 16:01:08 +05301084 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1085 mtd);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001086
1087 platform_set_drvdata(pdev, NULL);
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301088 if (info->dma_ch != -1)
vimal singhdfe32892009-07-13 16:29:16 +05301089 omap_free_dma(info->dma_ch);
1090
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301091 if (info->gpmc_irq)
1092 free_irq(info->gpmc_irq, info);
1093
Vimal Singh67ce04b2009-05-12 13:47:03 -07001094 /* Release NAND device, its internal structures and partitions */
1095 nand_release(&info->mtd);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +00001096 iounmap(info->nand.IO_ADDR_R);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001097 kfree(&info->mtd);
1098 return 0;
1099}
1100
1101static struct platform_driver omap_nand_driver = {
1102 .probe = omap_nand_probe,
1103 .remove = omap_nand_remove,
1104 .driver = {
1105 .name = DRIVER_NAME,
1106 .owner = THIS_MODULE,
1107 },
1108};
1109
1110static int __init omap_nand_init(void)
1111{
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301112 pr_info("%s driver initializing\n", DRIVER_NAME);
vimal singhdfe32892009-07-13 16:29:16 +05301113
Vimal Singh67ce04b2009-05-12 13:47:03 -07001114 return platform_driver_register(&omap_nand_driver);
1115}
1116
1117static void __exit omap_nand_exit(void)
1118{
1119 platform_driver_unregister(&omap_nand_driver);
1120}
1121
1122module_init(omap_nand_init);
1123module_exit(omap_nand_exit);
1124
1125MODULE_ALIAS(DRIVER_NAME);
1126MODULE_LICENSE("GPL");
1127MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");