blob: 05d3562ec748d11eb8ae766ea2f653460af70047 [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>
Paul Gortmakera0e5cc52011-07-03 15:17:31 -040014#include <linux/module.h>
Sukumar Ghorai4e070372011-01-28 15:42:06 +053015#include <linux/interrupt.h>
vimal singhc276aca2009-06-27 11:07:06 +053016#include <linux/jiffies.h>
17#include <linux/sched.h>
Vimal Singh67ce04b2009-05-12 13:47:03 -070018#include <linux/mtd/mtd.h>
19#include <linux/mtd/nand.h>
20#include <linux/mtd/partitions.h>
21#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Vimal Singh67ce04b2009-05-12 13:47:03 -070023
Tony Lindgrence491cf2009-10-20 09:40:47 -070024#include <plat/dma.h>
25#include <plat/gpmc.h>
26#include <plat/nand.h>
Vimal Singh67ce04b2009-05-12 13:47:03 -070027
Vimal Singh67ce04b2009-05-12 13:47:03 -070028#define DRIVER_NAME "omap2-nand"
Sukumar Ghorai4e070372011-01-28 15:42:06 +053029#define OMAP_NAND_TIMEOUT_MS 5000
Vimal Singh67ce04b2009-05-12 13:47:03 -070030
Vimal Singh67ce04b2009-05-12 13:47:03 -070031#define NAND_Ecc_P1e (1 << 0)
32#define NAND_Ecc_P2e (1 << 1)
33#define NAND_Ecc_P4e (1 << 2)
34#define NAND_Ecc_P8e (1 << 3)
35#define NAND_Ecc_P16e (1 << 4)
36#define NAND_Ecc_P32e (1 << 5)
37#define NAND_Ecc_P64e (1 << 6)
38#define NAND_Ecc_P128e (1 << 7)
39#define NAND_Ecc_P256e (1 << 8)
40#define NAND_Ecc_P512e (1 << 9)
41#define NAND_Ecc_P1024e (1 << 10)
42#define NAND_Ecc_P2048e (1 << 11)
43
44#define NAND_Ecc_P1o (1 << 16)
45#define NAND_Ecc_P2o (1 << 17)
46#define NAND_Ecc_P4o (1 << 18)
47#define NAND_Ecc_P8o (1 << 19)
48#define NAND_Ecc_P16o (1 << 20)
49#define NAND_Ecc_P32o (1 << 21)
50#define NAND_Ecc_P64o (1 << 22)
51#define NAND_Ecc_P128o (1 << 23)
52#define NAND_Ecc_P256o (1 << 24)
53#define NAND_Ecc_P512o (1 << 25)
54#define NAND_Ecc_P1024o (1 << 26)
55#define NAND_Ecc_P2048o (1 << 27)
56
57#define TF(value) (value ? 1 : 0)
58
59#define P2048e(a) (TF(a & NAND_Ecc_P2048e) << 0)
60#define P2048o(a) (TF(a & NAND_Ecc_P2048o) << 1)
61#define P1e(a) (TF(a & NAND_Ecc_P1e) << 2)
62#define P1o(a) (TF(a & NAND_Ecc_P1o) << 3)
63#define P2e(a) (TF(a & NAND_Ecc_P2e) << 4)
64#define P2o(a) (TF(a & NAND_Ecc_P2o) << 5)
65#define P4e(a) (TF(a & NAND_Ecc_P4e) << 6)
66#define P4o(a) (TF(a & NAND_Ecc_P4o) << 7)
67
68#define P8e(a) (TF(a & NAND_Ecc_P8e) << 0)
69#define P8o(a) (TF(a & NAND_Ecc_P8o) << 1)
70#define P16e(a) (TF(a & NAND_Ecc_P16e) << 2)
71#define P16o(a) (TF(a & NAND_Ecc_P16o) << 3)
72#define P32e(a) (TF(a & NAND_Ecc_P32e) << 4)
73#define P32o(a) (TF(a & NAND_Ecc_P32o) << 5)
74#define P64e(a) (TF(a & NAND_Ecc_P64e) << 6)
75#define P64o(a) (TF(a & NAND_Ecc_P64o) << 7)
76
77#define P128e(a) (TF(a & NAND_Ecc_P128e) << 0)
78#define P128o(a) (TF(a & NAND_Ecc_P128o) << 1)
79#define P256e(a) (TF(a & NAND_Ecc_P256e) << 2)
80#define P256o(a) (TF(a & NAND_Ecc_P256o) << 3)
81#define P512e(a) (TF(a & NAND_Ecc_P512e) << 4)
82#define P512o(a) (TF(a & NAND_Ecc_P512o) << 5)
83#define P1024e(a) (TF(a & NAND_Ecc_P1024e) << 6)
84#define P1024o(a) (TF(a & NAND_Ecc_P1024o) << 7)
85
86#define P8e_s(a) (TF(a & NAND_Ecc_P8e) << 0)
87#define P8o_s(a) (TF(a & NAND_Ecc_P8o) << 1)
88#define P16e_s(a) (TF(a & NAND_Ecc_P16e) << 2)
89#define P16o_s(a) (TF(a & NAND_Ecc_P16o) << 3)
90#define P1e_s(a) (TF(a & NAND_Ecc_P1e) << 4)
91#define P1o_s(a) (TF(a & NAND_Ecc_P1o) << 5)
92#define P2e_s(a) (TF(a & NAND_Ecc_P2e) << 6)
93#define P2o_s(a) (TF(a & NAND_Ecc_P2o) << 7)
94
95#define P4e_s(a) (TF(a & NAND_Ecc_P4e) << 0)
96#define P4o_s(a) (TF(a & NAND_Ecc_P4o) << 1)
97
Sukumar Ghoraif040d332011-01-28 15:42:09 +053098/* oob info generated runtime depending on ecc algorithm and layout selected */
99static struct nand_ecclayout omap_oobinfo;
100/* Define some generic bad / good block scan pattern which are used
101 * while scanning a device for factory marked good / bad blocks
102 */
103static uint8_t scan_ff_pattern[] = { 0xff };
104static struct nand_bbt_descr bb_descrip_flashbased = {
105 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
106 .offs = 0,
107 .len = 1,
108 .pattern = scan_ff_pattern,
109};
vimal singh59e9c5a2009-07-13 16:26:24 +0530110
vimal singh59e9c5a2009-07-13 16:26:24 +0530111
Vimal Singh67ce04b2009-05-12 13:47:03 -0700112struct omap_nand_info {
113 struct nand_hw_control controller;
114 struct omap_nand_platform_data *pdata;
115 struct mtd_info mtd;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700116 struct nand_chip nand;
117 struct platform_device *pdev;
118
119 int gpmc_cs;
120 unsigned long phys_base;
vimal singhdfe32892009-07-13 16:29:16 +0530121 struct completion comp;
122 int dma_ch;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530123 int gpmc_irq;
124 enum {
125 OMAP_NAND_IO_READ = 0, /* read */
126 OMAP_NAND_IO_WRITE, /* write */
127 } iomode;
128 u_char *buf;
129 int buf_len;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700130};
131
132/**
Vimal Singh67ce04b2009-05-12 13:47:03 -0700133 * omap_hwcontrol - hardware specific access to control-lines
134 * @mtd: MTD device structure
135 * @cmd: command to device
136 * @ctrl:
137 * NAND_NCE: bit 0 -> don't care
138 * NAND_CLE: bit 1 -> Command Latch
139 * NAND_ALE: bit 2 -> Address Latch
140 *
141 * NOTE: boards may use different bits for these!!
142 */
143static void omap_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
144{
145 struct omap_nand_info *info = container_of(mtd,
146 struct omap_nand_info, mtd);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700147
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000148 if (cmd != NAND_CMD_NONE) {
149 if (ctrl & NAND_CLE)
150 gpmc_nand_write(info->gpmc_cs, GPMC_NAND_COMMAND, cmd);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700151
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000152 else if (ctrl & NAND_ALE)
153 gpmc_nand_write(info->gpmc_cs, GPMC_NAND_ADDRESS, cmd);
154
155 else /* NAND_NCE */
156 gpmc_nand_write(info->gpmc_cs, GPMC_NAND_DATA, cmd);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700157 }
Vimal Singh67ce04b2009-05-12 13:47:03 -0700158}
159
160/**
vimal singh59e9c5a2009-07-13 16:26:24 +0530161 * omap_read_buf8 - read data from NAND controller into buffer
162 * @mtd: MTD device structure
163 * @buf: buffer to store date
164 * @len: number of bytes to read
165 */
166static void omap_read_buf8(struct mtd_info *mtd, u_char *buf, int len)
167{
168 struct nand_chip *nand = mtd->priv;
169
170 ioread8_rep(nand->IO_ADDR_R, buf, len);
171}
172
173/**
174 * omap_write_buf8 - write buffer to NAND controller
175 * @mtd: MTD device structure
176 * @buf: data buffer
177 * @len: number of bytes to write
178 */
179static void omap_write_buf8(struct mtd_info *mtd, const u_char *buf, int len)
180{
181 struct omap_nand_info *info = container_of(mtd,
182 struct omap_nand_info, mtd);
183 u_char *p = (u_char *)buf;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000184 u32 status = 0;
vimal singh59e9c5a2009-07-13 16:26:24 +0530185
186 while (len--) {
187 iowrite8(*p++, info->nand.IO_ADDR_W);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000188 /* wait until buffer is available for write */
189 do {
190 status = gpmc_read_status(GPMC_STATUS_BUFFER);
191 } while (!status);
vimal singh59e9c5a2009-07-13 16:26:24 +0530192 }
193}
194
195/**
Vimal Singh67ce04b2009-05-12 13:47:03 -0700196 * omap_read_buf16 - read data from NAND controller into buffer
197 * @mtd: MTD device structure
198 * @buf: buffer to store date
199 * @len: number of bytes to read
200 */
201static void omap_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
202{
203 struct nand_chip *nand = mtd->priv;
204
vimal singh59e9c5a2009-07-13 16:26:24 +0530205 ioread16_rep(nand->IO_ADDR_R, buf, len / 2);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700206}
207
208/**
209 * omap_write_buf16 - write buffer to NAND controller
210 * @mtd: MTD device structure
211 * @buf: data buffer
212 * @len: number of bytes to write
213 */
214static void omap_write_buf16(struct mtd_info *mtd, const u_char * buf, int len)
215{
216 struct omap_nand_info *info = container_of(mtd,
217 struct omap_nand_info, mtd);
218 u16 *p = (u16 *) buf;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000219 u32 status = 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700220 /* FIXME try bursts of writesw() or DMA ... */
221 len >>= 1;
222
223 while (len--) {
vimal singh59e9c5a2009-07-13 16:26:24 +0530224 iowrite16(*p++, info->nand.IO_ADDR_W);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000225 /* wait until buffer is available for write */
226 do {
227 status = gpmc_read_status(GPMC_STATUS_BUFFER);
228 } while (!status);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700229 }
230}
vimal singh59e9c5a2009-07-13 16:26:24 +0530231
232/**
233 * omap_read_buf_pref - read data from NAND controller into buffer
234 * @mtd: MTD device structure
235 * @buf: buffer to store date
236 * @len: number of bytes to read
237 */
238static void omap_read_buf_pref(struct mtd_info *mtd, u_char *buf, int len)
239{
240 struct omap_nand_info *info = container_of(mtd,
241 struct omap_nand_info, mtd);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000242 uint32_t r_count = 0;
vimal singh59e9c5a2009-07-13 16:26:24 +0530243 int ret = 0;
244 u32 *p = (u32 *)buf;
245
246 /* take care of subpage reads */
Vimal Singhc3341d02010-01-07 12:16:26 +0530247 if (len % 4) {
248 if (info->nand.options & NAND_BUSWIDTH_16)
249 omap_read_buf16(mtd, buf, len % 4);
250 else
251 omap_read_buf8(mtd, buf, len % 4);
252 p = (u32 *) (buf + len % 4);
253 len -= len % 4;
vimal singh59e9c5a2009-07-13 16:26:24 +0530254 }
vimal singh59e9c5a2009-07-13 16:26:24 +0530255
256 /* configure and start prefetch transfer */
Sukumar Ghorai317379a2011-01-28 15:42:07 +0530257 ret = gpmc_prefetch_enable(info->gpmc_cs,
258 PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x0);
vimal singh59e9c5a2009-07-13 16:26:24 +0530259 if (ret) {
260 /* PFPW engine is busy, use cpu copy method */
261 if (info->nand.options & NAND_BUSWIDTH_16)
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530262 omap_read_buf16(mtd, (u_char *)p, len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530263 else
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530264 omap_read_buf8(mtd, (u_char *)p, len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530265 } else {
266 do {
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000267 r_count = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT);
268 r_count = r_count >> 2;
269 ioread32_rep(info->nand.IO_ADDR_R, p, r_count);
vimal singh59e9c5a2009-07-13 16:26:24 +0530270 p += r_count;
271 len -= r_count << 2;
272 } while (len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530273 /* disable and stop the PFPW engine */
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000274 gpmc_prefetch_reset(info->gpmc_cs);
vimal singh59e9c5a2009-07-13 16:26:24 +0530275 }
276}
277
278/**
279 * omap_write_buf_pref - write buffer to NAND controller
280 * @mtd: MTD device structure
281 * @buf: data buffer
282 * @len: number of bytes to write
283 */
284static void omap_write_buf_pref(struct mtd_info *mtd,
285 const u_char *buf, int len)
286{
287 struct omap_nand_info *info = container_of(mtd,
288 struct omap_nand_info, mtd);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530289 uint32_t w_count = 0;
vimal singh59e9c5a2009-07-13 16:26:24 +0530290 int i = 0, ret = 0;
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530291 u16 *p = (u16 *)buf;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530292 unsigned long tim, limit;
vimal singh59e9c5a2009-07-13 16:26:24 +0530293
294 /* take care of subpage writes */
295 if (len % 2 != 0) {
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000296 writeb(*buf, info->nand.IO_ADDR_W);
vimal singh59e9c5a2009-07-13 16:26:24 +0530297 p = (u16 *)(buf + 1);
298 len--;
299 }
300
301 /* configure and start prefetch transfer */
Sukumar Ghorai317379a2011-01-28 15:42:07 +0530302 ret = gpmc_prefetch_enable(info->gpmc_cs,
303 PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x1);
vimal singh59e9c5a2009-07-13 16:26:24 +0530304 if (ret) {
305 /* PFPW engine is busy, use cpu copy method */
306 if (info->nand.options & NAND_BUSWIDTH_16)
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530307 omap_write_buf16(mtd, (u_char *)p, len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530308 else
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530309 omap_write_buf8(mtd, (u_char *)p, len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530310 } else {
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000311 while (len) {
312 w_count = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT);
313 w_count = w_count >> 1;
vimal singh59e9c5a2009-07-13 16:26:24 +0530314 for (i = 0; (i < w_count) && len; i++, len -= 2)
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000315 iowrite16(*p++, info->nand.IO_ADDR_W);
vimal singh59e9c5a2009-07-13 16:26:24 +0530316 }
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000317 /* wait for data to flushed-out before reset the prefetch */
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530318 tim = 0;
319 limit = (loops_per_jiffy *
320 msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
321 while (gpmc_read_status(GPMC_PREFETCH_COUNT) && (tim++ < limit))
322 cpu_relax();
323
vimal singh59e9c5a2009-07-13 16:26:24 +0530324 /* disable and stop the PFPW engine */
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000325 gpmc_prefetch_reset(info->gpmc_cs);
vimal singh59e9c5a2009-07-13 16:26:24 +0530326 }
327}
328
vimal singhdfe32892009-07-13 16:29:16 +0530329/*
330 * omap_nand_dma_cb: callback on the completion of dma transfer
331 * @lch: logical channel
332 * @ch_satuts: channel status
333 * @data: pointer to completion data structure
334 */
335static void omap_nand_dma_cb(int lch, u16 ch_status, void *data)
336{
337 complete((struct completion *) data);
338}
339
340/*
341 * omap_nand_dma_transfer: configer and start dma transfer
342 * @mtd: MTD device structure
343 * @addr: virtual address in RAM of source/destination
344 * @len: number of data bytes to be transferred
345 * @is_write: flag for read/write operation
346 */
347static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
348 unsigned int len, int is_write)
349{
350 struct omap_nand_info *info = container_of(mtd,
351 struct omap_nand_info, mtd);
vimal singhdfe32892009-07-13 16:29:16 +0530352 enum dma_data_direction dir = is_write ? DMA_TO_DEVICE :
353 DMA_FROM_DEVICE;
354 dma_addr_t dma_addr;
355 int ret;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530356 unsigned long tim, limit;
vimal singhdfe32892009-07-13 16:29:16 +0530357
Sukumar Ghorai317379a2011-01-28 15:42:07 +0530358 /* The fifo depth is 64 bytes max.
359 * But configure the FIFO-threahold to 32 to get a sync at each frame
360 * and frame length is 32 bytes.
vimal singhdfe32892009-07-13 16:29:16 +0530361 */
362 int buf_len = len >> 6;
363
364 if (addr >= high_memory) {
365 struct page *p1;
366
367 if (((size_t)addr & PAGE_MASK) !=
368 ((size_t)(addr + len - 1) & PAGE_MASK))
369 goto out_copy;
370 p1 = vmalloc_to_page(addr);
371 if (!p1)
372 goto out_copy;
373 addr = page_address(p1) + ((size_t)addr & ~PAGE_MASK);
374 }
375
376 dma_addr = dma_map_single(&info->pdev->dev, addr, len, dir);
377 if (dma_mapping_error(&info->pdev->dev, dma_addr)) {
378 dev_err(&info->pdev->dev,
379 "Couldn't DMA map a %d byte buffer\n", len);
380 goto out_copy;
381 }
382
383 if (is_write) {
384 omap_set_dma_dest_params(info->dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
385 info->phys_base, 0, 0);
386 omap_set_dma_src_params(info->dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
387 dma_addr, 0, 0);
388 omap_set_dma_transfer_params(info->dma_ch, OMAP_DMA_DATA_TYPE_S32,
389 0x10, buf_len, OMAP_DMA_SYNC_FRAME,
390 OMAP24XX_DMA_GPMC, OMAP_DMA_DST_SYNC);
391 } else {
392 omap_set_dma_src_params(info->dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
393 info->phys_base, 0, 0);
394 omap_set_dma_dest_params(info->dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
395 dma_addr, 0, 0);
396 omap_set_dma_transfer_params(info->dma_ch, OMAP_DMA_DATA_TYPE_S32,
397 0x10, buf_len, OMAP_DMA_SYNC_FRAME,
398 OMAP24XX_DMA_GPMC, OMAP_DMA_SRC_SYNC);
399 }
400 /* configure and start prefetch transfer */
Sukumar Ghorai317379a2011-01-28 15:42:07 +0530401 ret = gpmc_prefetch_enable(info->gpmc_cs,
402 PREFETCH_FIFOTHRESHOLD_MAX, 0x1, len, is_write);
vimal singhdfe32892009-07-13 16:29:16 +0530403 if (ret)
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530404 /* PFPW engine is busy, use cpu copy method */
Grazvydas Ignotasd7efe222012-04-11 04:04:34 +0300405 goto out_copy_unmap;
vimal singhdfe32892009-07-13 16:29:16 +0530406
407 init_completion(&info->comp);
408
409 omap_start_dma(info->dma_ch);
410
411 /* setup and start DMA using dma_addr */
412 wait_for_completion(&info->comp);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530413 tim = 0;
414 limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
415 while (gpmc_read_status(GPMC_PREFETCH_COUNT) && (tim++ < limit))
416 cpu_relax();
vimal singhdfe32892009-07-13 16:29:16 +0530417
vimal singhdfe32892009-07-13 16:29:16 +0530418 /* disable and stop the PFPW engine */
Daniel J Bluemanf12f6622010-09-29 21:01:55 +0100419 gpmc_prefetch_reset(info->gpmc_cs);
vimal singhdfe32892009-07-13 16:29:16 +0530420
421 dma_unmap_single(&info->pdev->dev, dma_addr, len, dir);
422 return 0;
423
Grazvydas Ignotasd7efe222012-04-11 04:04:34 +0300424out_copy_unmap:
425 dma_unmap_single(&info->pdev->dev, dma_addr, len, dir);
vimal singhdfe32892009-07-13 16:29:16 +0530426out_copy:
427 if (info->nand.options & NAND_BUSWIDTH_16)
428 is_write == 0 ? omap_read_buf16(mtd, (u_char *) addr, len)
429 : omap_write_buf16(mtd, (u_char *) addr, len);
430 else
431 is_write == 0 ? omap_read_buf8(mtd, (u_char *) addr, len)
432 : omap_write_buf8(mtd, (u_char *) addr, len);
433 return 0;
434}
vimal singhdfe32892009-07-13 16:29:16 +0530435
436/**
437 * omap_read_buf_dma_pref - read data from NAND controller into buffer
438 * @mtd: MTD device structure
439 * @buf: buffer to store date
440 * @len: number of bytes to read
441 */
442static void omap_read_buf_dma_pref(struct mtd_info *mtd, u_char *buf, int len)
443{
444 if (len <= mtd->oobsize)
445 omap_read_buf_pref(mtd, buf, len);
446 else
447 /* start transfer in DMA mode */
448 omap_nand_dma_transfer(mtd, buf, len, 0x0);
449}
450
451/**
452 * omap_write_buf_dma_pref - write buffer to NAND controller
453 * @mtd: MTD device structure
454 * @buf: data buffer
455 * @len: number of bytes to write
456 */
457static void omap_write_buf_dma_pref(struct mtd_info *mtd,
458 const u_char *buf, int len)
459{
460 if (len <= mtd->oobsize)
461 omap_write_buf_pref(mtd, buf, len);
462 else
463 /* start transfer in DMA mode */
Vimal Singhbdaefc42010-01-05 12:49:24 +0530464 omap_nand_dma_transfer(mtd, (u_char *) buf, len, 0x1);
vimal singhdfe32892009-07-13 16:29:16 +0530465}
466
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530467/*
468 * omap_nand_irq - GMPC irq handler
469 * @this_irq: gpmc irq number
470 * @dev: omap_nand_info structure pointer is passed here
471 */
472static irqreturn_t omap_nand_irq(int this_irq, void *dev)
473{
474 struct omap_nand_info *info = (struct omap_nand_info *) dev;
475 u32 bytes;
476 u32 irq_stat;
477
478 irq_stat = gpmc_read_status(GPMC_GET_IRQ_STATUS);
479 bytes = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT);
480 bytes = bytes & 0xFFFC; /* io in multiple of 4 bytes */
481 if (info->iomode == OMAP_NAND_IO_WRITE) { /* checks for write io */
482 if (irq_stat & 0x2)
483 goto done;
484
485 if (info->buf_len && (info->buf_len < bytes))
486 bytes = info->buf_len;
487 else if (!info->buf_len)
488 bytes = 0;
489 iowrite32_rep(info->nand.IO_ADDR_W,
490 (u32 *)info->buf, bytes >> 2);
491 info->buf = info->buf + bytes;
492 info->buf_len -= bytes;
493
494 } else {
495 ioread32_rep(info->nand.IO_ADDR_R,
496 (u32 *)info->buf, bytes >> 2);
497 info->buf = info->buf + bytes;
498
499 if (irq_stat & 0x2)
500 goto done;
501 }
502 gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, irq_stat);
503
504 return IRQ_HANDLED;
505
506done:
507 complete(&info->comp);
508 /* disable irq */
509 gpmc_cs_configure(info->gpmc_cs, GPMC_ENABLE_IRQ, 0);
510
511 /* clear status */
512 gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, irq_stat);
513
514 return IRQ_HANDLED;
515}
516
517/*
518 * omap_read_buf_irq_pref - read data from NAND controller into buffer
519 * @mtd: MTD device structure
520 * @buf: buffer to store date
521 * @len: number of bytes to read
522 */
523static void omap_read_buf_irq_pref(struct mtd_info *mtd, u_char *buf, int len)
524{
525 struct omap_nand_info *info = container_of(mtd,
526 struct omap_nand_info, mtd);
527 int ret = 0;
528
529 if (len <= mtd->oobsize) {
530 omap_read_buf_pref(mtd, buf, len);
531 return;
532 }
533
534 info->iomode = OMAP_NAND_IO_READ;
535 info->buf = buf;
536 init_completion(&info->comp);
537
538 /* configure and start prefetch transfer */
Sukumar Ghorai317379a2011-01-28 15:42:07 +0530539 ret = gpmc_prefetch_enable(info->gpmc_cs,
540 PREFETCH_FIFOTHRESHOLD_MAX/2, 0x0, len, 0x0);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530541 if (ret)
542 /* PFPW engine is busy, use cpu copy method */
543 goto out_copy;
544
545 info->buf_len = len;
546 /* enable irq */
547 gpmc_cs_configure(info->gpmc_cs, GPMC_ENABLE_IRQ,
548 (GPMC_IRQ_FIFOEVENTENABLE | GPMC_IRQ_COUNT_EVENT));
549
550 /* waiting for read to complete */
551 wait_for_completion(&info->comp);
552
553 /* disable and stop the PFPW engine */
554 gpmc_prefetch_reset(info->gpmc_cs);
555 return;
556
557out_copy:
558 if (info->nand.options & NAND_BUSWIDTH_16)
559 omap_read_buf16(mtd, buf, len);
560 else
561 omap_read_buf8(mtd, buf, len);
562}
563
564/*
565 * omap_write_buf_irq_pref - write buffer to NAND controller
566 * @mtd: MTD device structure
567 * @buf: data buffer
568 * @len: number of bytes to write
569 */
570static void omap_write_buf_irq_pref(struct mtd_info *mtd,
571 const u_char *buf, int len)
572{
573 struct omap_nand_info *info = container_of(mtd,
574 struct omap_nand_info, mtd);
575 int ret = 0;
576 unsigned long tim, limit;
577
578 if (len <= mtd->oobsize) {
579 omap_write_buf_pref(mtd, buf, len);
580 return;
581 }
582
583 info->iomode = OMAP_NAND_IO_WRITE;
584 info->buf = (u_char *) buf;
585 init_completion(&info->comp);
586
Sukumar Ghorai317379a2011-01-28 15:42:07 +0530587 /* configure and start prefetch transfer : size=24 */
588 ret = gpmc_prefetch_enable(info->gpmc_cs,
589 (PREFETCH_FIFOTHRESHOLD_MAX * 3) / 8, 0x0, len, 0x1);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530590 if (ret)
591 /* PFPW engine is busy, use cpu copy method */
592 goto out_copy;
593
594 info->buf_len = len;
595 /* enable irq */
596 gpmc_cs_configure(info->gpmc_cs, GPMC_ENABLE_IRQ,
597 (GPMC_IRQ_FIFOEVENTENABLE | GPMC_IRQ_COUNT_EVENT));
598
599 /* waiting for write to complete */
600 wait_for_completion(&info->comp);
601 /* wait for data to flushed-out before reset the prefetch */
602 tim = 0;
603 limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
604 while (gpmc_read_status(GPMC_PREFETCH_COUNT) && (tim++ < limit))
605 cpu_relax();
606
607 /* disable and stop the PFPW engine */
608 gpmc_prefetch_reset(info->gpmc_cs);
609 return;
610
611out_copy:
612 if (info->nand.options & NAND_BUSWIDTH_16)
613 omap_write_buf16(mtd, buf, len);
614 else
615 omap_write_buf8(mtd, buf, len);
616}
617
Vimal Singh67ce04b2009-05-12 13:47:03 -0700618/**
619 * omap_verify_buf - Verify chip data against buffer
620 * @mtd: MTD device structure
621 * @buf: buffer containing the data to compare
622 * @len: number of bytes to compare
623 */
624static int omap_verify_buf(struct mtd_info *mtd, const u_char * buf, int len)
625{
626 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
627 mtd);
628 u16 *p = (u16 *) buf;
629
630 len >>= 1;
631 while (len--) {
632 if (*p++ != cpu_to_le16(readw(info->nand.IO_ADDR_R)))
633 return -EFAULT;
634 }
635
636 return 0;
637}
638
Vimal Singh67ce04b2009-05-12 13:47:03 -0700639/**
640 * gen_true_ecc - This function will generate true ECC value
641 * @ecc_buf: buffer to store ecc code
642 *
643 * This generated true ECC value can be used when correcting
644 * data read from NAND flash memory core
645 */
646static void gen_true_ecc(u8 *ecc_buf)
647{
648 u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
649 ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
650
651 ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
652 P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
653 ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
654 P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
655 ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
656 P1e(tmp) | P2048o(tmp) | P2048e(tmp));
657}
658
659/**
660 * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data
661 * @ecc_data1: ecc code from nand spare area
662 * @ecc_data2: ecc code from hardware register obtained from hardware ecc
663 * @page_data: page data
664 *
665 * This function compares two ECC's and indicates if there is an error.
666 * If the error can be corrected it will be corrected to the buffer.
John Ogness74f1b722011-02-28 13:12:46 +0100667 * If there is no error, %0 is returned. If there is an error but it
668 * was corrected, %1 is returned. Otherwise, %-1 is returned.
Vimal Singh67ce04b2009-05-12 13:47:03 -0700669 */
670static int omap_compare_ecc(u8 *ecc_data1, /* read from NAND memory */
671 u8 *ecc_data2, /* read from register */
672 u8 *page_data)
673{
674 uint i;
675 u8 tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
676 u8 comp0_bit[8], comp1_bit[8], comp2_bit[8];
677 u8 ecc_bit[24];
678 u8 ecc_sum = 0;
679 u8 find_bit = 0;
680 uint find_byte = 0;
681 int isEccFF;
682
683 isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
684
685 gen_true_ecc(ecc_data1);
686 gen_true_ecc(ecc_data2);
687
688 for (i = 0; i <= 2; i++) {
689 *(ecc_data1 + i) = ~(*(ecc_data1 + i));
690 *(ecc_data2 + i) = ~(*(ecc_data2 + i));
691 }
692
693 for (i = 0; i < 8; i++) {
694 tmp0_bit[i] = *ecc_data1 % 2;
695 *ecc_data1 = *ecc_data1 / 2;
696 }
697
698 for (i = 0; i < 8; i++) {
699 tmp1_bit[i] = *(ecc_data1 + 1) % 2;
700 *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
701 }
702
703 for (i = 0; i < 8; i++) {
704 tmp2_bit[i] = *(ecc_data1 + 2) % 2;
705 *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
706 }
707
708 for (i = 0; i < 8; i++) {
709 comp0_bit[i] = *ecc_data2 % 2;
710 *ecc_data2 = *ecc_data2 / 2;
711 }
712
713 for (i = 0; i < 8; i++) {
714 comp1_bit[i] = *(ecc_data2 + 1) % 2;
715 *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
716 }
717
718 for (i = 0; i < 8; i++) {
719 comp2_bit[i] = *(ecc_data2 + 2) % 2;
720 *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
721 }
722
723 for (i = 0; i < 6; i++)
724 ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
725
726 for (i = 0; i < 8; i++)
727 ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
728
729 for (i = 0; i < 8; i++)
730 ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
731
732 ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
733 ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
734
735 for (i = 0; i < 24; i++)
736 ecc_sum += ecc_bit[i];
737
738 switch (ecc_sum) {
739 case 0:
740 /* Not reached because this function is not called if
741 * ECC values are equal
742 */
743 return 0;
744
745 case 1:
746 /* Uncorrectable error */
Brian Norris289c0522011-07-19 10:06:09 -0700747 pr_debug("ECC UNCORRECTED_ERROR 1\n");
Vimal Singh67ce04b2009-05-12 13:47:03 -0700748 return -1;
749
750 case 11:
751 /* UN-Correctable error */
Brian Norris289c0522011-07-19 10:06:09 -0700752 pr_debug("ECC UNCORRECTED_ERROR B\n");
Vimal Singh67ce04b2009-05-12 13:47:03 -0700753 return -1;
754
755 case 12:
756 /* Correctable error */
757 find_byte = (ecc_bit[23] << 8) +
758 (ecc_bit[21] << 7) +
759 (ecc_bit[19] << 6) +
760 (ecc_bit[17] << 5) +
761 (ecc_bit[15] << 4) +
762 (ecc_bit[13] << 3) +
763 (ecc_bit[11] << 2) +
764 (ecc_bit[9] << 1) +
765 ecc_bit[7];
766
767 find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
768
Brian Norris0a32a102011-07-19 10:06:10 -0700769 pr_debug("Correcting single bit ECC error at offset: "
770 "%d, bit: %d\n", find_byte, find_bit);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700771
772 page_data[find_byte] ^= (1 << find_bit);
773
John Ogness74f1b722011-02-28 13:12:46 +0100774 return 1;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700775 default:
776 if (isEccFF) {
777 if (ecc_data2[0] == 0 &&
778 ecc_data2[1] == 0 &&
779 ecc_data2[2] == 0)
780 return 0;
781 }
Brian Norris289c0522011-07-19 10:06:09 -0700782 pr_debug("UNCORRECTED_ERROR default\n");
Vimal Singh67ce04b2009-05-12 13:47:03 -0700783 return -1;
784 }
785}
786
787/**
788 * omap_correct_data - Compares the ECC read with HW generated ECC
789 * @mtd: MTD device structure
790 * @dat: page data
791 * @read_ecc: ecc read from nand flash
792 * @calc_ecc: ecc read from HW ECC registers
793 *
794 * Compares the ecc read from nand spare area with ECC registers values
John Ogness74f1b722011-02-28 13:12:46 +0100795 * and if ECC's mismatched, it will call 'omap_compare_ecc' for error
796 * detection and correction. If there are no errors, %0 is returned. If
797 * there were errors and all of the errors were corrected, the number of
798 * corrected errors is returned. If uncorrectable errors exist, %-1 is
799 * returned.
Vimal Singh67ce04b2009-05-12 13:47:03 -0700800 */
801static int omap_correct_data(struct mtd_info *mtd, u_char *dat,
802 u_char *read_ecc, u_char *calc_ecc)
803{
804 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
805 mtd);
806 int blockCnt = 0, i = 0, ret = 0;
John Ogness74f1b722011-02-28 13:12:46 +0100807 int stat = 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700808
809 /* Ex NAND_ECC_HW12_2048 */
810 if ((info->nand.ecc.mode == NAND_ECC_HW) &&
811 (info->nand.ecc.size == 2048))
812 blockCnt = 4;
813 else
814 blockCnt = 1;
815
816 for (i = 0; i < blockCnt; i++) {
817 if (memcmp(read_ecc, calc_ecc, 3) != 0) {
818 ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
819 if (ret < 0)
820 return ret;
John Ogness74f1b722011-02-28 13:12:46 +0100821 /* keep track of the number of corrected errors */
822 stat += ret;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700823 }
824 read_ecc += 3;
825 calc_ecc += 3;
826 dat += 512;
827 }
John Ogness74f1b722011-02-28 13:12:46 +0100828 return stat;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700829}
830
831/**
832 * omap_calcuate_ecc - Generate non-inverted ECC bytes.
833 * @mtd: MTD device structure
834 * @dat: The pointer to data on which ecc is computed
835 * @ecc_code: The ecc_code buffer
836 *
837 * Using noninverted ECC can be considered ugly since writing a blank
838 * page ie. padding will clear the ECC bytes. This is no problem as long
839 * nobody is trying to write data on the seemingly unused page. Reading
840 * an erased page will produce an ECC mismatch between generated and read
841 * ECC bytes that has to be dealt with separately.
842 */
843static int omap_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
844 u_char *ecc_code)
845{
846 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
847 mtd);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000848 return gpmc_calculate_ecc(info->gpmc_cs, dat, ecc_code);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700849}
850
851/**
852 * omap_enable_hwecc - This function enables the hardware ecc functionality
853 * @mtd: MTD device structure
854 * @mode: Read/Write mode
855 */
856static void omap_enable_hwecc(struct mtd_info *mtd, int mode)
857{
858 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
859 mtd);
860 struct nand_chip *chip = mtd->priv;
861 unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700862
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000863 gpmc_enable_hwecc(info->gpmc_cs, mode, dev_width, info->nand.ecc.size);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700864}
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000865
Vimal Singh67ce04b2009-05-12 13:47:03 -0700866/**
867 * omap_wait - wait until the command is done
868 * @mtd: MTD device structure
869 * @chip: NAND Chip structure
870 *
871 * Wait function is called during Program and erase operations and
872 * the way it is called from MTD layer, we should wait till the NAND
873 * chip is ready after the programming/erase operation has completed.
874 *
875 * Erase can take up to 400ms and program up to 20ms according to
876 * general NAND and SmartMedia specs
877 */
878static int omap_wait(struct mtd_info *mtd, struct nand_chip *chip)
879{
880 struct nand_chip *this = mtd->priv;
881 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
882 mtd);
883 unsigned long timeo = jiffies;
Ivan Djelica9c465f2012-04-17 13:11:53 +0200884 int status, state = this->state;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700885
886 if (state == FL_ERASING)
887 timeo += (HZ * 400) / 1000;
888 else
889 timeo += (HZ * 20) / 1000;
890
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000891 gpmc_nand_write(info->gpmc_cs,
892 GPMC_NAND_COMMAND, (NAND_CMD_STATUS & 0xFF));
Vimal Singh67ce04b2009-05-12 13:47:03 -0700893 while (time_before(jiffies, timeo)) {
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000894 status = gpmc_nand_read(info->gpmc_cs, GPMC_NAND_DATA);
vimal singhc276aca2009-06-27 11:07:06 +0530895 if (status & NAND_STATUS_READY)
Vimal Singh67ce04b2009-05-12 13:47:03 -0700896 break;
vimal singhc276aca2009-06-27 11:07:06 +0530897 cond_resched();
Vimal Singh67ce04b2009-05-12 13:47:03 -0700898 }
Ivan Djelica9c465f2012-04-17 13:11:53 +0200899
900 status = gpmc_nand_read(info->gpmc_cs, GPMC_NAND_DATA);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700901 return status;
902}
903
904/**
905 * omap_dev_ready - calls the platform specific dev_ready function
906 * @mtd: MTD device structure
907 */
908static int omap_dev_ready(struct mtd_info *mtd)
909{
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000910 unsigned int val = 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700911 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
912 mtd);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700913
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000914 val = gpmc_read_status(GPMC_GET_IRQ_STATUS);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700915 if ((val & 0x100) == 0x100) {
916 /* Clear IRQ Interrupt */
917 val |= 0x100;
918 val &= ~(0x0);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000919 gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, val);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700920 } else {
921 unsigned int cnt = 0;
922 while (cnt++ < 0x1FF) {
923 if ((val & 0x100) == 0x100)
924 return 0;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000925 val = gpmc_read_status(GPMC_GET_IRQ_STATUS);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700926 }
927 }
928
929 return 1;
930}
931
932static int __devinit omap_nand_probe(struct platform_device *pdev)
933{
934 struct omap_nand_info *info;
935 struct omap_nand_platform_data *pdata;
936 int err;
Sukumar Ghoraif040d332011-01-28 15:42:09 +0530937 int i, offset;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700938
939 pdata = pdev->dev.platform_data;
940 if (pdata == NULL) {
941 dev_err(&pdev->dev, "platform data missing\n");
942 return -ENODEV;
943 }
944
945 info = kzalloc(sizeof(struct omap_nand_info), GFP_KERNEL);
946 if (!info)
947 return -ENOMEM;
948
949 platform_set_drvdata(pdev, info);
950
951 spin_lock_init(&info->controller.lock);
952 init_waitqueue_head(&info->controller.wq);
953
954 info->pdev = pdev;
955
956 info->gpmc_cs = pdata->cs;
Vimal Singh2f70a1e2010-02-15 10:03:33 -0800957 info->phys_base = pdata->phys_base;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700958
959 info->mtd.priv = &info->nand;
960 info->mtd.name = dev_name(&pdev->dev);
961 info->mtd.owner = THIS_MODULE;
962
Sukumar Ghoraid5ce2b62011-01-28 15:42:03 +0530963 info->nand.options = pdata->devsize;
Vimal Singh2f70a1e2010-02-15 10:03:33 -0800964 info->nand.options |= NAND_SKIP_BBTSCAN;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700965
966 /* NAND write protect off */
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000967 gpmc_cs_configure(info->gpmc_cs, GPMC_CONFIG_WP, 0);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700968
969 if (!request_mem_region(info->phys_base, NAND_IO_SIZE,
970 pdev->dev.driver->name)) {
971 err = -EBUSY;
Vimal Singh2f70a1e2010-02-15 10:03:33 -0800972 goto out_free_info;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700973 }
974
975 info->nand.IO_ADDR_R = ioremap(info->phys_base, NAND_IO_SIZE);
976 if (!info->nand.IO_ADDR_R) {
977 err = -ENOMEM;
978 goto out_release_mem_region;
979 }
vimal singh59e9c5a2009-07-13 16:26:24 +0530980
Vimal Singh67ce04b2009-05-12 13:47:03 -0700981 info->nand.controller = &info->controller;
982
983 info->nand.IO_ADDR_W = info->nand.IO_ADDR_R;
984 info->nand.cmd_ctrl = omap_hwcontrol;
985
Vimal Singh67ce04b2009-05-12 13:47:03 -0700986 /*
987 * If RDY/BSY line is connected to OMAP then use the omap ready
988 * funcrtion and the generic nand_wait function which reads the status
989 * register after monitoring the RDY/BSY line.Otherwise use a standard
990 * chip delay which is slightly more than tR (AC Timing) of the NAND
991 * device and read status register until you get a failure or success
992 */
993 if (pdata->dev_ready) {
994 info->nand.dev_ready = omap_dev_ready;
995 info->nand.chip_delay = 0;
996 } else {
997 info->nand.waitfunc = omap_wait;
998 info->nand.chip_delay = 50;
999 }
1000
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301001 switch (pdata->xfer_type) {
1002 case NAND_OMAP_PREFETCH_POLLED:
vimal singh59e9c5a2009-07-13 16:26:24 +05301003 info->nand.read_buf = omap_read_buf_pref;
1004 info->nand.write_buf = omap_write_buf_pref;
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301005 break;
vimal singhdfe32892009-07-13 16:29:16 +05301006
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301007 case NAND_OMAP_POLLED:
vimal singh59e9c5a2009-07-13 16:26:24 +05301008 if (info->nand.options & NAND_BUSWIDTH_16) {
1009 info->nand.read_buf = omap_read_buf16;
1010 info->nand.write_buf = omap_write_buf16;
1011 } else {
1012 info->nand.read_buf = omap_read_buf8;
1013 info->nand.write_buf = omap_write_buf8;
1014 }
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301015 break;
1016
1017 case NAND_OMAP_PREFETCH_DMA:
1018 err = omap_request_dma(OMAP24XX_DMA_GPMC, "NAND",
1019 omap_nand_dma_cb, &info->comp, &info->dma_ch);
1020 if (err < 0) {
1021 info->dma_ch = -1;
1022 dev_err(&pdev->dev, "DMA request failed!\n");
1023 goto out_release_mem_region;
1024 } else {
1025 omap_set_dma_dest_burst_mode(info->dma_ch,
1026 OMAP_DMA_DATA_BURST_16);
1027 omap_set_dma_src_burst_mode(info->dma_ch,
1028 OMAP_DMA_DATA_BURST_16);
1029
1030 info->nand.read_buf = omap_read_buf_dma_pref;
1031 info->nand.write_buf = omap_write_buf_dma_pref;
1032 }
1033 break;
1034
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301035 case NAND_OMAP_PREFETCH_IRQ:
1036 err = request_irq(pdata->gpmc_irq,
1037 omap_nand_irq, IRQF_SHARED, "gpmc-nand", info);
1038 if (err) {
1039 dev_err(&pdev->dev, "requesting irq(%d) error:%d",
1040 pdata->gpmc_irq, err);
1041 goto out_release_mem_region;
1042 } else {
1043 info->gpmc_irq = pdata->gpmc_irq;
1044 info->nand.read_buf = omap_read_buf_irq_pref;
1045 info->nand.write_buf = omap_write_buf_irq_pref;
1046 }
1047 break;
1048
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301049 default:
1050 dev_err(&pdev->dev,
1051 "xfer_type(%d) not supported!\n", pdata->xfer_type);
1052 err = -EINVAL;
1053 goto out_release_mem_region;
vimal singh59e9c5a2009-07-13 16:26:24 +05301054 }
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301055
vimal singh59e9c5a2009-07-13 16:26:24 +05301056 info->nand.verify_buf = omap_verify_buf;
1057
Sukumar Ghoraif3d73f32011-01-28 15:42:08 +05301058 /* selsect the ecc type */
1059 if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_DEFAULT)
1060 info->nand.ecc.mode = NAND_ECC_SOFT;
Sukumar Ghoraif040d332011-01-28 15:42:09 +05301061 else if ((pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW) ||
1062 (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE)) {
Sukumar Ghoraif3d73f32011-01-28 15:42:08 +05301063 info->nand.ecc.bytes = 3;
1064 info->nand.ecc.size = 512;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001065 info->nand.ecc.strength = 1;
Sukumar Ghoraif3d73f32011-01-28 15:42:08 +05301066 info->nand.ecc.calculate = omap_calculate_ecc;
1067 info->nand.ecc.hwctl = omap_enable_hwecc;
1068 info->nand.ecc.correct = omap_correct_data;
1069 info->nand.ecc.mode = NAND_ECC_HW;
1070 }
Vimal Singh67ce04b2009-05-12 13:47:03 -07001071
1072 /* DIP switches on some boards change between 8 and 16 bit
1073 * bus widths for flash. Try the other width if the first try fails.
1074 */
Jan Weitzela80f1c12011-04-19 16:15:34 +02001075 if (nand_scan_ident(&info->mtd, 1, NULL)) {
Vimal Singh67ce04b2009-05-12 13:47:03 -07001076 info->nand.options ^= NAND_BUSWIDTH_16;
Jan Weitzela80f1c12011-04-19 16:15:34 +02001077 if (nand_scan_ident(&info->mtd, 1, NULL)) {
Vimal Singh67ce04b2009-05-12 13:47:03 -07001078 err = -ENXIO;
1079 goto out_release_mem_region;
1080 }
1081 }
1082
Sukumar Ghoraif040d332011-01-28 15:42:09 +05301083 /* rom code layout */
1084 if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE) {
1085
1086 if (info->nand.options & NAND_BUSWIDTH_16)
1087 offset = 2;
1088 else {
1089 offset = 1;
1090 info->nand.badblock_pattern = &bb_descrip_flashbased;
1091 }
1092 omap_oobinfo.eccbytes = 3 * (info->mtd.oobsize/16);
1093 for (i = 0; i < omap_oobinfo.eccbytes; i++)
1094 omap_oobinfo.eccpos[i] = i+offset;
1095
1096 omap_oobinfo.oobfree->offset = offset + omap_oobinfo.eccbytes;
1097 omap_oobinfo.oobfree->length = info->mtd.oobsize -
1098 (offset + omap_oobinfo.eccbytes);
1099
1100 info->nand.ecc.layout = &omap_oobinfo;
1101 }
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301102
Jan Weitzela80f1c12011-04-19 16:15:34 +02001103 /* second phase scan */
1104 if (nand_scan_tail(&info->mtd)) {
1105 err = -ENXIO;
1106 goto out_release_mem_region;
1107 }
1108
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001109 mtd_device_parse_register(&info->mtd, NULL, NULL, pdata->parts,
1110 pdata->nr_parts);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001111
1112 platform_set_drvdata(pdev, &info->mtd);
1113
1114 return 0;
1115
1116out_release_mem_region:
1117 release_mem_region(info->phys_base, NAND_IO_SIZE);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001118out_free_info:
1119 kfree(info);
1120
1121 return err;
1122}
1123
1124static int omap_nand_remove(struct platform_device *pdev)
1125{
1126 struct mtd_info *mtd = platform_get_drvdata(pdev);
Vimal Singhf35b6ed2010-01-05 16:01:08 +05301127 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1128 mtd);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001129
1130 platform_set_drvdata(pdev, NULL);
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301131 if (info->dma_ch != -1)
vimal singhdfe32892009-07-13 16:29:16 +05301132 omap_free_dma(info->dma_ch);
1133
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301134 if (info->gpmc_irq)
1135 free_irq(info->gpmc_irq, info);
1136
Vimal Singh67ce04b2009-05-12 13:47:03 -07001137 /* Release NAND device, its internal structures and partitions */
1138 nand_release(&info->mtd);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +00001139 iounmap(info->nand.IO_ADDR_R);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001140 kfree(&info->mtd);
1141 return 0;
1142}
1143
1144static struct platform_driver omap_nand_driver = {
1145 .probe = omap_nand_probe,
1146 .remove = omap_nand_remove,
1147 .driver = {
1148 .name = DRIVER_NAME,
1149 .owner = THIS_MODULE,
1150 },
1151};
1152
Axel Linf99640d2011-11-27 20:45:03 +08001153module_platform_driver(omap_nand_driver);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001154
Axel Linc804c732011-03-07 11:04:24 +08001155MODULE_ALIAS("platform:" DRIVER_NAME);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001156MODULE_LICENSE("GPL");
1157MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");