blob: 3282b151aa038a3c8d42b528b09055b32b70f622 [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>
Russell King763e7352012-04-25 00:16:00 +010012#include <linux/dmaengine.h>
Vimal Singh67ce04b2009-05-12 13:47:03 -070013#include <linux/dma-mapping.h>
14#include <linux/delay.h>
Paul Gortmakera0e5cc52011-07-03 15:17:31 -040015#include <linux/module.h>
Sukumar Ghorai4e070372011-01-28 15:42:06 +053016#include <linux/interrupt.h>
vimal singhc276aca2009-06-27 11:07:06 +053017#include <linux/jiffies.h>
18#include <linux/sched.h>
Vimal Singh67ce04b2009-05-12 13:47:03 -070019#include <linux/mtd/mtd.h>
20#include <linux/mtd/nand.h>
21#include <linux/mtd/partitions.h>
Russell King763e7352012-04-25 00:16:00 +010022#include <linux/omap-dma.h>
Vimal Singh67ce04b2009-05-12 13:47:03 -070023#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Vimal Singh67ce04b2009-05-12 13:47:03 -070025
Ivan Djelic0e618ef2012-04-30 12:17:18 +020026#ifdef CONFIG_MTD_NAND_OMAP_BCH
27#include <linux/bch.h>
28#endif
29
Tony Lindgrence491cf2009-10-20 09:40:47 -070030#include <plat/dma.h>
Arnd Bergmann22037472012-08-24 15:21:06 +020031#include <linux/platform_data/mtd-nand-omap2.h>
Vimal Singh67ce04b2009-05-12 13:47:03 -070032
Vimal Singh67ce04b2009-05-12 13:47:03 -070033#define DRIVER_NAME "omap2-nand"
Sukumar Ghorai4e070372011-01-28 15:42:06 +053034#define OMAP_NAND_TIMEOUT_MS 5000
Vimal Singh67ce04b2009-05-12 13:47:03 -070035
Vimal Singh67ce04b2009-05-12 13:47:03 -070036#define NAND_Ecc_P1e (1 << 0)
37#define NAND_Ecc_P2e (1 << 1)
38#define NAND_Ecc_P4e (1 << 2)
39#define NAND_Ecc_P8e (1 << 3)
40#define NAND_Ecc_P16e (1 << 4)
41#define NAND_Ecc_P32e (1 << 5)
42#define NAND_Ecc_P64e (1 << 6)
43#define NAND_Ecc_P128e (1 << 7)
44#define NAND_Ecc_P256e (1 << 8)
45#define NAND_Ecc_P512e (1 << 9)
46#define NAND_Ecc_P1024e (1 << 10)
47#define NAND_Ecc_P2048e (1 << 11)
48
49#define NAND_Ecc_P1o (1 << 16)
50#define NAND_Ecc_P2o (1 << 17)
51#define NAND_Ecc_P4o (1 << 18)
52#define NAND_Ecc_P8o (1 << 19)
53#define NAND_Ecc_P16o (1 << 20)
54#define NAND_Ecc_P32o (1 << 21)
55#define NAND_Ecc_P64o (1 << 22)
56#define NAND_Ecc_P128o (1 << 23)
57#define NAND_Ecc_P256o (1 << 24)
58#define NAND_Ecc_P512o (1 << 25)
59#define NAND_Ecc_P1024o (1 << 26)
60#define NAND_Ecc_P2048o (1 << 27)
61
62#define TF(value) (value ? 1 : 0)
63
64#define P2048e(a) (TF(a & NAND_Ecc_P2048e) << 0)
65#define P2048o(a) (TF(a & NAND_Ecc_P2048o) << 1)
66#define P1e(a) (TF(a & NAND_Ecc_P1e) << 2)
67#define P1o(a) (TF(a & NAND_Ecc_P1o) << 3)
68#define P2e(a) (TF(a & NAND_Ecc_P2e) << 4)
69#define P2o(a) (TF(a & NAND_Ecc_P2o) << 5)
70#define P4e(a) (TF(a & NAND_Ecc_P4e) << 6)
71#define P4o(a) (TF(a & NAND_Ecc_P4o) << 7)
72
73#define P8e(a) (TF(a & NAND_Ecc_P8e) << 0)
74#define P8o(a) (TF(a & NAND_Ecc_P8o) << 1)
75#define P16e(a) (TF(a & NAND_Ecc_P16e) << 2)
76#define P16o(a) (TF(a & NAND_Ecc_P16o) << 3)
77#define P32e(a) (TF(a & NAND_Ecc_P32e) << 4)
78#define P32o(a) (TF(a & NAND_Ecc_P32o) << 5)
79#define P64e(a) (TF(a & NAND_Ecc_P64e) << 6)
80#define P64o(a) (TF(a & NAND_Ecc_P64o) << 7)
81
82#define P128e(a) (TF(a & NAND_Ecc_P128e) << 0)
83#define P128o(a) (TF(a & NAND_Ecc_P128o) << 1)
84#define P256e(a) (TF(a & NAND_Ecc_P256e) << 2)
85#define P256o(a) (TF(a & NAND_Ecc_P256o) << 3)
86#define P512e(a) (TF(a & NAND_Ecc_P512e) << 4)
87#define P512o(a) (TF(a & NAND_Ecc_P512o) << 5)
88#define P1024e(a) (TF(a & NAND_Ecc_P1024e) << 6)
89#define P1024o(a) (TF(a & NAND_Ecc_P1024o) << 7)
90
91#define P8e_s(a) (TF(a & NAND_Ecc_P8e) << 0)
92#define P8o_s(a) (TF(a & NAND_Ecc_P8o) << 1)
93#define P16e_s(a) (TF(a & NAND_Ecc_P16e) << 2)
94#define P16o_s(a) (TF(a & NAND_Ecc_P16o) << 3)
95#define P1e_s(a) (TF(a & NAND_Ecc_P1e) << 4)
96#define P1o_s(a) (TF(a & NAND_Ecc_P1o) << 5)
97#define P2e_s(a) (TF(a & NAND_Ecc_P2e) << 6)
98#define P2o_s(a) (TF(a & NAND_Ecc_P2o) << 7)
99
100#define P4e_s(a) (TF(a & NAND_Ecc_P4e) << 0)
101#define P4o_s(a) (TF(a & NAND_Ecc_P4o) << 1)
102
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700103#define PREFETCH_CONFIG1_CS_SHIFT 24
104#define ECC_CONFIG_CS_SHIFT 1
105#define CS_MASK 0x7
106#define ENABLE_PREFETCH (0x1 << 7)
107#define DMA_MPU_MODE_SHIFT 2
Afzal Mohammed2ef9f3d2012-10-04 19:03:06 +0530108#define ECCSIZE0_SHIFT 12
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700109#define ECCSIZE1_SHIFT 22
110#define ECC1RESULTSIZE 0x1
111#define ECCCLEAR 0x100
112#define ECC1 0x1
Afzal Mohammed47f88af42012-09-29 18:20:11 +0530113#define PREFETCH_FIFOTHRESHOLD_MAX 0x40
114#define PREFETCH_FIFOTHRESHOLD(val) ((val) << 8)
115#define PREFETCH_STATUS_COUNT(val) (val & 0x00003fff)
116#define PREFETCH_STATUS_FIFO_CNT(val) ((val >> 24) & 0x7F)
117#define STATUS_BUFF_EMPTY 0x00000001
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700118
Sukumar Ghoraif040d332011-01-28 15:42:09 +0530119/* oob info generated runtime depending on ecc algorithm and layout selected */
120static struct nand_ecclayout omap_oobinfo;
121/* Define some generic bad / good block scan pattern which are used
122 * while scanning a device for factory marked good / bad blocks
123 */
124static uint8_t scan_ff_pattern[] = { 0xff };
125static struct nand_bbt_descr bb_descrip_flashbased = {
126 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
127 .offs = 0,
128 .len = 1,
129 .pattern = scan_ff_pattern,
130};
vimal singh59e9c5a2009-07-13 16:26:24 +0530131
vimal singh59e9c5a2009-07-13 16:26:24 +0530132
Vimal Singh67ce04b2009-05-12 13:47:03 -0700133struct omap_nand_info {
134 struct nand_hw_control controller;
135 struct omap_nand_platform_data *pdata;
136 struct mtd_info mtd;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700137 struct nand_chip nand;
138 struct platform_device *pdev;
139
140 int gpmc_cs;
141 unsigned long phys_base;
Afzal Mohammed9c4c2f82012-08-30 12:53:23 -0700142 unsigned long mem_size;
vimal singhdfe32892009-07-13 16:29:16 +0530143 struct completion comp;
Russell King763e7352012-04-25 00:16:00 +0100144 struct dma_chan *dma;
Afzal Mohammed5c468452012-08-30 12:53:24 -0700145 int gpmc_irq_fifo;
146 int gpmc_irq_count;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530147 enum {
148 OMAP_NAND_IO_READ = 0, /* read */
149 OMAP_NAND_IO_WRITE, /* write */
150 } iomode;
151 u_char *buf;
152 int buf_len;
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700153 struct gpmc_nand_regs reg;
Ivan Djelic0e618ef2012-04-30 12:17:18 +0200154
155#ifdef CONFIG_MTD_NAND_OMAP_BCH
156 struct bch_control *bch;
157 struct nand_ecclayout ecclayout;
158#endif
Vimal Singh67ce04b2009-05-12 13:47:03 -0700159};
160
161/**
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700162 * omap_prefetch_enable - configures and starts prefetch transfer
163 * @cs: cs (chip select) number
164 * @fifo_th: fifo threshold to be used for read/ write
165 * @dma_mode: dma mode enable (1) or disable (0)
166 * @u32_count: number of bytes to be transferred
167 * @is_write: prefetch read(0) or write post(1) mode
168 */
169static int omap_prefetch_enable(int cs, int fifo_th, int dma_mode,
170 unsigned int u32_count, int is_write, struct omap_nand_info *info)
171{
172 u32 val;
173
174 if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX)
175 return -1;
176
177 if (readl(info->reg.gpmc_prefetch_control))
178 return -EBUSY;
179
180 /* Set the amount of bytes to be prefetched */
181 writel(u32_count, info->reg.gpmc_prefetch_config2);
182
183 /* Set dma/mpu mode, the prefetch read / post write and
184 * enable the engine. Set which cs is has requested for.
185 */
186 val = ((cs << PREFETCH_CONFIG1_CS_SHIFT) |
187 PREFETCH_FIFOTHRESHOLD(fifo_th) | ENABLE_PREFETCH |
188 (dma_mode << DMA_MPU_MODE_SHIFT) | (0x1 & is_write));
189 writel(val, info->reg.gpmc_prefetch_config1);
190
191 /* Start the prefetch engine */
192 writel(0x1, info->reg.gpmc_prefetch_control);
193
194 return 0;
195}
196
197/**
198 * omap_prefetch_reset - disables and stops the prefetch engine
199 */
200static int omap_prefetch_reset(int cs, struct omap_nand_info *info)
201{
202 u32 config1;
203
204 /* check if the same module/cs is trying to reset */
205 config1 = readl(info->reg.gpmc_prefetch_config1);
206 if (((config1 >> PREFETCH_CONFIG1_CS_SHIFT) & CS_MASK) != cs)
207 return -EINVAL;
208
209 /* Stop the PFPW engine */
210 writel(0x0, info->reg.gpmc_prefetch_control);
211
212 /* Reset/disable the PFPW engine */
213 writel(0x0, info->reg.gpmc_prefetch_config1);
214
215 return 0;
216}
217
218/**
Vimal Singh67ce04b2009-05-12 13:47:03 -0700219 * omap_hwcontrol - hardware specific access to control-lines
220 * @mtd: MTD device structure
221 * @cmd: command to device
222 * @ctrl:
223 * NAND_NCE: bit 0 -> don't care
224 * NAND_CLE: bit 1 -> Command Latch
225 * NAND_ALE: bit 2 -> Address Latch
226 *
227 * NOTE: boards may use different bits for these!!
228 */
229static void omap_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
230{
231 struct omap_nand_info *info = container_of(mtd,
232 struct omap_nand_info, mtd);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700233
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000234 if (cmd != NAND_CMD_NONE) {
235 if (ctrl & NAND_CLE)
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700236 writeb(cmd, info->reg.gpmc_nand_command);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700237
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000238 else if (ctrl & NAND_ALE)
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700239 writeb(cmd, info->reg.gpmc_nand_address);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000240
241 else /* NAND_NCE */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700242 writeb(cmd, info->reg.gpmc_nand_data);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700243 }
Vimal Singh67ce04b2009-05-12 13:47:03 -0700244}
245
246/**
vimal singh59e9c5a2009-07-13 16:26:24 +0530247 * omap_read_buf8 - read data from NAND controller into buffer
248 * @mtd: MTD device structure
249 * @buf: buffer to store date
250 * @len: number of bytes to read
251 */
252static void omap_read_buf8(struct mtd_info *mtd, u_char *buf, int len)
253{
254 struct nand_chip *nand = mtd->priv;
255
256 ioread8_rep(nand->IO_ADDR_R, buf, len);
257}
258
259/**
260 * omap_write_buf8 - write buffer to NAND controller
261 * @mtd: MTD device structure
262 * @buf: data buffer
263 * @len: number of bytes to write
264 */
265static void omap_write_buf8(struct mtd_info *mtd, const u_char *buf, int len)
266{
267 struct omap_nand_info *info = container_of(mtd,
268 struct omap_nand_info, mtd);
269 u_char *p = (u_char *)buf;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000270 u32 status = 0;
vimal singh59e9c5a2009-07-13 16:26:24 +0530271
272 while (len--) {
273 iowrite8(*p++, info->nand.IO_ADDR_W);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000274 /* wait until buffer is available for write */
275 do {
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700276 status = readl(info->reg.gpmc_status) &
Afzal Mohammed47f88af42012-09-29 18:20:11 +0530277 STATUS_BUFF_EMPTY;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000278 } while (!status);
vimal singh59e9c5a2009-07-13 16:26:24 +0530279 }
280}
281
282/**
Vimal Singh67ce04b2009-05-12 13:47:03 -0700283 * omap_read_buf16 - read data from NAND controller into buffer
284 * @mtd: MTD device structure
285 * @buf: buffer to store date
286 * @len: number of bytes to read
287 */
288static void omap_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
289{
290 struct nand_chip *nand = mtd->priv;
291
vimal singh59e9c5a2009-07-13 16:26:24 +0530292 ioread16_rep(nand->IO_ADDR_R, buf, len / 2);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700293}
294
295/**
296 * omap_write_buf16 - write buffer to NAND controller
297 * @mtd: MTD device structure
298 * @buf: data buffer
299 * @len: number of bytes to write
300 */
301static void omap_write_buf16(struct mtd_info *mtd, const u_char * buf, int len)
302{
303 struct omap_nand_info *info = container_of(mtd,
304 struct omap_nand_info, mtd);
305 u16 *p = (u16 *) buf;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000306 u32 status = 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700307 /* FIXME try bursts of writesw() or DMA ... */
308 len >>= 1;
309
310 while (len--) {
vimal singh59e9c5a2009-07-13 16:26:24 +0530311 iowrite16(*p++, info->nand.IO_ADDR_W);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000312 /* wait until buffer is available for write */
313 do {
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700314 status = readl(info->reg.gpmc_status) &
Afzal Mohammed47f88af42012-09-29 18:20:11 +0530315 STATUS_BUFF_EMPTY;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000316 } while (!status);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700317 }
318}
vimal singh59e9c5a2009-07-13 16:26:24 +0530319
320/**
321 * omap_read_buf_pref - read data from NAND controller into buffer
322 * @mtd: MTD device structure
323 * @buf: buffer to store date
324 * @len: number of bytes to read
325 */
326static void omap_read_buf_pref(struct mtd_info *mtd, u_char *buf, int len)
327{
328 struct omap_nand_info *info = container_of(mtd,
329 struct omap_nand_info, mtd);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000330 uint32_t r_count = 0;
vimal singh59e9c5a2009-07-13 16:26:24 +0530331 int ret = 0;
332 u32 *p = (u32 *)buf;
333
334 /* take care of subpage reads */
Vimal Singhc3341d02010-01-07 12:16:26 +0530335 if (len % 4) {
336 if (info->nand.options & NAND_BUSWIDTH_16)
337 omap_read_buf16(mtd, buf, len % 4);
338 else
339 omap_read_buf8(mtd, buf, len % 4);
340 p = (u32 *) (buf + len % 4);
341 len -= len % 4;
vimal singh59e9c5a2009-07-13 16:26:24 +0530342 }
vimal singh59e9c5a2009-07-13 16:26:24 +0530343
344 /* configure and start prefetch transfer */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700345 ret = omap_prefetch_enable(info->gpmc_cs,
346 PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x0, info);
vimal singh59e9c5a2009-07-13 16:26:24 +0530347 if (ret) {
348 /* PFPW engine is busy, use cpu copy method */
349 if (info->nand.options & NAND_BUSWIDTH_16)
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530350 omap_read_buf16(mtd, (u_char *)p, len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530351 else
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530352 omap_read_buf8(mtd, (u_char *)p, len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530353 } else {
354 do {
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700355 r_count = readl(info->reg.gpmc_prefetch_status);
Afzal Mohammed47f88af42012-09-29 18:20:11 +0530356 r_count = PREFETCH_STATUS_FIFO_CNT(r_count);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000357 r_count = r_count >> 2;
358 ioread32_rep(info->nand.IO_ADDR_R, p, r_count);
vimal singh59e9c5a2009-07-13 16:26:24 +0530359 p += r_count;
360 len -= r_count << 2;
361 } while (len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530362 /* disable and stop the PFPW engine */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700363 omap_prefetch_reset(info->gpmc_cs, info);
vimal singh59e9c5a2009-07-13 16:26:24 +0530364 }
365}
366
367/**
368 * omap_write_buf_pref - write buffer to NAND controller
369 * @mtd: MTD device structure
370 * @buf: data buffer
371 * @len: number of bytes to write
372 */
373static void omap_write_buf_pref(struct mtd_info *mtd,
374 const u_char *buf, int len)
375{
376 struct omap_nand_info *info = container_of(mtd,
377 struct omap_nand_info, mtd);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530378 uint32_t w_count = 0;
vimal singh59e9c5a2009-07-13 16:26:24 +0530379 int i = 0, ret = 0;
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530380 u16 *p = (u16 *)buf;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530381 unsigned long tim, limit;
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700382 u32 val;
vimal singh59e9c5a2009-07-13 16:26:24 +0530383
384 /* take care of subpage writes */
385 if (len % 2 != 0) {
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000386 writeb(*buf, info->nand.IO_ADDR_W);
vimal singh59e9c5a2009-07-13 16:26:24 +0530387 p = (u16 *)(buf + 1);
388 len--;
389 }
390
391 /* configure and start prefetch transfer */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700392 ret = omap_prefetch_enable(info->gpmc_cs,
393 PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x1, info);
vimal singh59e9c5a2009-07-13 16:26:24 +0530394 if (ret) {
395 /* PFPW engine is busy, use cpu copy method */
396 if (info->nand.options & NAND_BUSWIDTH_16)
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530397 omap_write_buf16(mtd, (u_char *)p, len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530398 else
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530399 omap_write_buf8(mtd, (u_char *)p, len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530400 } else {
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000401 while (len) {
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700402 w_count = readl(info->reg.gpmc_prefetch_status);
Afzal Mohammed47f88af42012-09-29 18:20:11 +0530403 w_count = PREFETCH_STATUS_FIFO_CNT(w_count);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000404 w_count = w_count >> 1;
vimal singh59e9c5a2009-07-13 16:26:24 +0530405 for (i = 0; (i < w_count) && len; i++, len -= 2)
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000406 iowrite16(*p++, info->nand.IO_ADDR_W);
vimal singh59e9c5a2009-07-13 16:26:24 +0530407 }
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000408 /* wait for data to flushed-out before reset the prefetch */
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530409 tim = 0;
410 limit = (loops_per_jiffy *
411 msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700412 do {
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530413 cpu_relax();
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700414 val = readl(info->reg.gpmc_prefetch_status);
Afzal Mohammed47f88af42012-09-29 18:20:11 +0530415 val = PREFETCH_STATUS_COUNT(val);
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700416 } while (val && (tim++ < limit));
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530417
vimal singh59e9c5a2009-07-13 16:26:24 +0530418 /* disable and stop the PFPW engine */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700419 omap_prefetch_reset(info->gpmc_cs, info);
vimal singh59e9c5a2009-07-13 16:26:24 +0530420 }
421}
422
vimal singhdfe32892009-07-13 16:29:16 +0530423/*
Russell King2df41d02012-04-25 00:19:39 +0100424 * omap_nand_dma_callback: callback on the completion of dma transfer
vimal singhdfe32892009-07-13 16:29:16 +0530425 * @data: pointer to completion data structure
426 */
Russell King763e7352012-04-25 00:16:00 +0100427static void omap_nand_dma_callback(void *data)
428{
429 complete((struct completion *) data);
430}
vimal singhdfe32892009-07-13 16:29:16 +0530431
432/*
Peter Meerwald4cacbe22012-07-19 13:21:04 +0200433 * omap_nand_dma_transfer: configure and start dma transfer
vimal singhdfe32892009-07-13 16:29:16 +0530434 * @mtd: MTD device structure
435 * @addr: virtual address in RAM of source/destination
436 * @len: number of data bytes to be transferred
437 * @is_write: flag for read/write operation
438 */
439static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
440 unsigned int len, int is_write)
441{
442 struct omap_nand_info *info = container_of(mtd,
443 struct omap_nand_info, mtd);
Russell King2df41d02012-04-25 00:19:39 +0100444 struct dma_async_tx_descriptor *tx;
vimal singhdfe32892009-07-13 16:29:16 +0530445 enum dma_data_direction dir = is_write ? DMA_TO_DEVICE :
446 DMA_FROM_DEVICE;
Russell King2df41d02012-04-25 00:19:39 +0100447 struct scatterlist sg;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530448 unsigned long tim, limit;
Russell King2df41d02012-04-25 00:19:39 +0100449 unsigned n;
450 int ret;
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700451 u32 val;
vimal singhdfe32892009-07-13 16:29:16 +0530452
453 if (addr >= high_memory) {
454 struct page *p1;
455
456 if (((size_t)addr & PAGE_MASK) !=
457 ((size_t)(addr + len - 1) & PAGE_MASK))
458 goto out_copy;
459 p1 = vmalloc_to_page(addr);
460 if (!p1)
461 goto out_copy;
462 addr = page_address(p1) + ((size_t)addr & ~PAGE_MASK);
463 }
464
Russell King2df41d02012-04-25 00:19:39 +0100465 sg_init_one(&sg, addr, len);
466 n = dma_map_sg(info->dma->device->dev, &sg, 1, dir);
467 if (n == 0) {
vimal singhdfe32892009-07-13 16:29:16 +0530468 dev_err(&info->pdev->dev,
469 "Couldn't DMA map a %d byte buffer\n", len);
470 goto out_copy;
471 }
472
Russell King2df41d02012-04-25 00:19:39 +0100473 tx = dmaengine_prep_slave_sg(info->dma, &sg, n,
474 is_write ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
475 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
476 if (!tx)
477 goto out_copy_unmap;
478
479 tx->callback = omap_nand_dma_callback;
480 tx->callback_param = &info->comp;
481 dmaengine_submit(tx);
482
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700483 /* configure and start prefetch transfer */
484 ret = omap_prefetch_enable(info->gpmc_cs,
485 PREFETCH_FIFOTHRESHOLD_MAX, 0x1, len, is_write, info);
vimal singhdfe32892009-07-13 16:29:16 +0530486 if (ret)
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530487 /* PFPW engine is busy, use cpu copy method */
Grazvydas Ignotasd7efe222012-04-11 04:04:34 +0300488 goto out_copy_unmap;
vimal singhdfe32892009-07-13 16:29:16 +0530489
490 init_completion(&info->comp);
Russell King2df41d02012-04-25 00:19:39 +0100491 dma_async_issue_pending(info->dma);
vimal singhdfe32892009-07-13 16:29:16 +0530492
493 /* setup and start DMA using dma_addr */
494 wait_for_completion(&info->comp);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530495 tim = 0;
496 limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700497
498 do {
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530499 cpu_relax();
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700500 val = readl(info->reg.gpmc_prefetch_status);
Afzal Mohammed47f88af42012-09-29 18:20:11 +0530501 val = PREFETCH_STATUS_COUNT(val);
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700502 } while (val && (tim++ < limit));
vimal singhdfe32892009-07-13 16:29:16 +0530503
vimal singhdfe32892009-07-13 16:29:16 +0530504 /* disable and stop the PFPW engine */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700505 omap_prefetch_reset(info->gpmc_cs, info);
vimal singhdfe32892009-07-13 16:29:16 +0530506
Russell King2df41d02012-04-25 00:19:39 +0100507 dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
vimal singhdfe32892009-07-13 16:29:16 +0530508 return 0;
509
Grazvydas Ignotasd7efe222012-04-11 04:04:34 +0300510out_copy_unmap:
Russell King2df41d02012-04-25 00:19:39 +0100511 dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
vimal singhdfe32892009-07-13 16:29:16 +0530512out_copy:
513 if (info->nand.options & NAND_BUSWIDTH_16)
514 is_write == 0 ? omap_read_buf16(mtd, (u_char *) addr, len)
515 : omap_write_buf16(mtd, (u_char *) addr, len);
516 else
517 is_write == 0 ? omap_read_buf8(mtd, (u_char *) addr, len)
518 : omap_write_buf8(mtd, (u_char *) addr, len);
519 return 0;
520}
vimal singhdfe32892009-07-13 16:29:16 +0530521
522/**
523 * omap_read_buf_dma_pref - read data from NAND controller into buffer
524 * @mtd: MTD device structure
525 * @buf: buffer to store date
526 * @len: number of bytes to read
527 */
528static void omap_read_buf_dma_pref(struct mtd_info *mtd, u_char *buf, int len)
529{
530 if (len <= mtd->oobsize)
531 omap_read_buf_pref(mtd, buf, len);
532 else
533 /* start transfer in DMA mode */
534 omap_nand_dma_transfer(mtd, buf, len, 0x0);
535}
536
537/**
538 * omap_write_buf_dma_pref - write buffer to NAND controller
539 * @mtd: MTD device structure
540 * @buf: data buffer
541 * @len: number of bytes to write
542 */
543static void omap_write_buf_dma_pref(struct mtd_info *mtd,
544 const u_char *buf, int len)
545{
546 if (len <= mtd->oobsize)
547 omap_write_buf_pref(mtd, buf, len);
548 else
549 /* start transfer in DMA mode */
Vimal Singhbdaefc42010-01-05 12:49:24 +0530550 omap_nand_dma_transfer(mtd, (u_char *) buf, len, 0x1);
vimal singhdfe32892009-07-13 16:29:16 +0530551}
552
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530553/*
Peter Meerwald4cacbe22012-07-19 13:21:04 +0200554 * omap_nand_irq - GPMC irq handler
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530555 * @this_irq: gpmc irq number
556 * @dev: omap_nand_info structure pointer is passed here
557 */
558static irqreturn_t omap_nand_irq(int this_irq, void *dev)
559{
560 struct omap_nand_info *info = (struct omap_nand_info *) dev;
561 u32 bytes;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530562
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700563 bytes = readl(info->reg.gpmc_prefetch_status);
Afzal Mohammed47f88af42012-09-29 18:20:11 +0530564 bytes = PREFETCH_STATUS_FIFO_CNT(bytes);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530565 bytes = bytes & 0xFFFC; /* io in multiple of 4 bytes */
566 if (info->iomode == OMAP_NAND_IO_WRITE) { /* checks for write io */
Afzal Mohammed5c468452012-08-30 12:53:24 -0700567 if (this_irq == info->gpmc_irq_count)
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530568 goto done;
569
570 if (info->buf_len && (info->buf_len < bytes))
571 bytes = info->buf_len;
572 else if (!info->buf_len)
573 bytes = 0;
574 iowrite32_rep(info->nand.IO_ADDR_W,
575 (u32 *)info->buf, bytes >> 2);
576 info->buf = info->buf + bytes;
577 info->buf_len -= bytes;
578
579 } else {
580 ioread32_rep(info->nand.IO_ADDR_R,
581 (u32 *)info->buf, bytes >> 2);
582 info->buf = info->buf + bytes;
583
Afzal Mohammed5c468452012-08-30 12:53:24 -0700584 if (this_irq == info->gpmc_irq_count)
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530585 goto done;
586 }
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530587
588 return IRQ_HANDLED;
589
590done:
591 complete(&info->comp);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530592
Afzal Mohammed5c468452012-08-30 12:53:24 -0700593 disable_irq_nosync(info->gpmc_irq_fifo);
594 disable_irq_nosync(info->gpmc_irq_count);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530595
596 return IRQ_HANDLED;
597}
598
599/*
600 * omap_read_buf_irq_pref - read data from NAND controller into buffer
601 * @mtd: MTD device structure
602 * @buf: buffer to store date
603 * @len: number of bytes to read
604 */
605static void omap_read_buf_irq_pref(struct mtd_info *mtd, u_char *buf, int len)
606{
607 struct omap_nand_info *info = container_of(mtd,
608 struct omap_nand_info, mtd);
609 int ret = 0;
610
611 if (len <= mtd->oobsize) {
612 omap_read_buf_pref(mtd, buf, len);
613 return;
614 }
615
616 info->iomode = OMAP_NAND_IO_READ;
617 info->buf = buf;
618 init_completion(&info->comp);
619
620 /* configure and start prefetch transfer */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700621 ret = omap_prefetch_enable(info->gpmc_cs,
622 PREFETCH_FIFOTHRESHOLD_MAX/2, 0x0, len, 0x0, info);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530623 if (ret)
624 /* PFPW engine is busy, use cpu copy method */
625 goto out_copy;
626
627 info->buf_len = len;
Afzal Mohammed5c468452012-08-30 12:53:24 -0700628
629 enable_irq(info->gpmc_irq_count);
630 enable_irq(info->gpmc_irq_fifo);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530631
632 /* waiting for read to complete */
633 wait_for_completion(&info->comp);
634
635 /* disable and stop the PFPW engine */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700636 omap_prefetch_reset(info->gpmc_cs, info);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530637 return;
638
639out_copy:
640 if (info->nand.options & NAND_BUSWIDTH_16)
641 omap_read_buf16(mtd, buf, len);
642 else
643 omap_read_buf8(mtd, buf, len);
644}
645
646/*
647 * omap_write_buf_irq_pref - write buffer to NAND controller
648 * @mtd: MTD device structure
649 * @buf: data buffer
650 * @len: number of bytes to write
651 */
652static void omap_write_buf_irq_pref(struct mtd_info *mtd,
653 const u_char *buf, int len)
654{
655 struct omap_nand_info *info = container_of(mtd,
656 struct omap_nand_info, mtd);
657 int ret = 0;
658 unsigned long tim, limit;
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700659 u32 val;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530660
661 if (len <= mtd->oobsize) {
662 omap_write_buf_pref(mtd, buf, len);
663 return;
664 }
665
666 info->iomode = OMAP_NAND_IO_WRITE;
667 info->buf = (u_char *) buf;
668 init_completion(&info->comp);
669
Sukumar Ghorai317379a2011-01-28 15:42:07 +0530670 /* configure and start prefetch transfer : size=24 */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700671 ret = omap_prefetch_enable(info->gpmc_cs,
672 (PREFETCH_FIFOTHRESHOLD_MAX * 3) / 8, 0x0, len, 0x1, info);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530673 if (ret)
674 /* PFPW engine is busy, use cpu copy method */
675 goto out_copy;
676
677 info->buf_len = len;
Afzal Mohammed5c468452012-08-30 12:53:24 -0700678
679 enable_irq(info->gpmc_irq_count);
680 enable_irq(info->gpmc_irq_fifo);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530681
682 /* waiting for write to complete */
683 wait_for_completion(&info->comp);
Afzal Mohammed5c468452012-08-30 12:53:24 -0700684
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530685 /* wait for data to flushed-out before reset the prefetch */
686 tim = 0;
687 limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700688 do {
689 val = readl(info->reg.gpmc_prefetch_status);
Afzal Mohammed47f88af42012-09-29 18:20:11 +0530690 val = PREFETCH_STATUS_COUNT(val);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530691 cpu_relax();
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700692 } while (val && (tim++ < limit));
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530693
694 /* disable and stop the PFPW engine */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700695 omap_prefetch_reset(info->gpmc_cs, info);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530696 return;
697
698out_copy:
699 if (info->nand.options & NAND_BUSWIDTH_16)
700 omap_write_buf16(mtd, buf, len);
701 else
702 omap_write_buf8(mtd, buf, len);
703}
704
Vimal Singh67ce04b2009-05-12 13:47:03 -0700705/**
Vimal Singh67ce04b2009-05-12 13:47:03 -0700706 * gen_true_ecc - This function will generate true ECC value
707 * @ecc_buf: buffer to store ecc code
708 *
709 * This generated true ECC value can be used when correcting
710 * data read from NAND flash memory core
711 */
712static void gen_true_ecc(u8 *ecc_buf)
713{
714 u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
715 ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
716
717 ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
718 P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
719 ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
720 P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
721 ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
722 P1e(tmp) | P2048o(tmp) | P2048e(tmp));
723}
724
725/**
726 * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data
727 * @ecc_data1: ecc code from nand spare area
728 * @ecc_data2: ecc code from hardware register obtained from hardware ecc
729 * @page_data: page data
730 *
731 * This function compares two ECC's and indicates if there is an error.
732 * If the error can be corrected it will be corrected to the buffer.
John Ogness74f1b722011-02-28 13:12:46 +0100733 * If there is no error, %0 is returned. If there is an error but it
734 * was corrected, %1 is returned. Otherwise, %-1 is returned.
Vimal Singh67ce04b2009-05-12 13:47:03 -0700735 */
736static int omap_compare_ecc(u8 *ecc_data1, /* read from NAND memory */
737 u8 *ecc_data2, /* read from register */
738 u8 *page_data)
739{
740 uint i;
741 u8 tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
742 u8 comp0_bit[8], comp1_bit[8], comp2_bit[8];
743 u8 ecc_bit[24];
744 u8 ecc_sum = 0;
745 u8 find_bit = 0;
746 uint find_byte = 0;
747 int isEccFF;
748
749 isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
750
751 gen_true_ecc(ecc_data1);
752 gen_true_ecc(ecc_data2);
753
754 for (i = 0; i <= 2; i++) {
755 *(ecc_data1 + i) = ~(*(ecc_data1 + i));
756 *(ecc_data2 + i) = ~(*(ecc_data2 + i));
757 }
758
759 for (i = 0; i < 8; i++) {
760 tmp0_bit[i] = *ecc_data1 % 2;
761 *ecc_data1 = *ecc_data1 / 2;
762 }
763
764 for (i = 0; i < 8; i++) {
765 tmp1_bit[i] = *(ecc_data1 + 1) % 2;
766 *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
767 }
768
769 for (i = 0; i < 8; i++) {
770 tmp2_bit[i] = *(ecc_data1 + 2) % 2;
771 *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
772 }
773
774 for (i = 0; i < 8; i++) {
775 comp0_bit[i] = *ecc_data2 % 2;
776 *ecc_data2 = *ecc_data2 / 2;
777 }
778
779 for (i = 0; i < 8; i++) {
780 comp1_bit[i] = *(ecc_data2 + 1) % 2;
781 *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
782 }
783
784 for (i = 0; i < 8; i++) {
785 comp2_bit[i] = *(ecc_data2 + 2) % 2;
786 *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
787 }
788
789 for (i = 0; i < 6; i++)
790 ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
791
792 for (i = 0; i < 8; i++)
793 ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
794
795 for (i = 0; i < 8; i++)
796 ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
797
798 ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
799 ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
800
801 for (i = 0; i < 24; i++)
802 ecc_sum += ecc_bit[i];
803
804 switch (ecc_sum) {
805 case 0:
806 /* Not reached because this function is not called if
807 * ECC values are equal
808 */
809 return 0;
810
811 case 1:
812 /* Uncorrectable error */
Brian Norris289c0522011-07-19 10:06:09 -0700813 pr_debug("ECC UNCORRECTED_ERROR 1\n");
Vimal Singh67ce04b2009-05-12 13:47:03 -0700814 return -1;
815
816 case 11:
817 /* UN-Correctable error */
Brian Norris289c0522011-07-19 10:06:09 -0700818 pr_debug("ECC UNCORRECTED_ERROR B\n");
Vimal Singh67ce04b2009-05-12 13:47:03 -0700819 return -1;
820
821 case 12:
822 /* Correctable error */
823 find_byte = (ecc_bit[23] << 8) +
824 (ecc_bit[21] << 7) +
825 (ecc_bit[19] << 6) +
826 (ecc_bit[17] << 5) +
827 (ecc_bit[15] << 4) +
828 (ecc_bit[13] << 3) +
829 (ecc_bit[11] << 2) +
830 (ecc_bit[9] << 1) +
831 ecc_bit[7];
832
833 find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
834
Brian Norris0a32a102011-07-19 10:06:10 -0700835 pr_debug("Correcting single bit ECC error at offset: "
836 "%d, bit: %d\n", find_byte, find_bit);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700837
838 page_data[find_byte] ^= (1 << find_bit);
839
John Ogness74f1b722011-02-28 13:12:46 +0100840 return 1;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700841 default:
842 if (isEccFF) {
843 if (ecc_data2[0] == 0 &&
844 ecc_data2[1] == 0 &&
845 ecc_data2[2] == 0)
846 return 0;
847 }
Brian Norris289c0522011-07-19 10:06:09 -0700848 pr_debug("UNCORRECTED_ERROR default\n");
Vimal Singh67ce04b2009-05-12 13:47:03 -0700849 return -1;
850 }
851}
852
853/**
854 * omap_correct_data - Compares the ECC read with HW generated ECC
855 * @mtd: MTD device structure
856 * @dat: page data
857 * @read_ecc: ecc read from nand flash
858 * @calc_ecc: ecc read from HW ECC registers
859 *
860 * Compares the ecc read from nand spare area with ECC registers values
John Ogness74f1b722011-02-28 13:12:46 +0100861 * and if ECC's mismatched, it will call 'omap_compare_ecc' for error
862 * detection and correction. If there are no errors, %0 is returned. If
863 * there were errors and all of the errors were corrected, the number of
864 * corrected errors is returned. If uncorrectable errors exist, %-1 is
865 * returned.
Vimal Singh67ce04b2009-05-12 13:47:03 -0700866 */
867static int omap_correct_data(struct mtd_info *mtd, u_char *dat,
868 u_char *read_ecc, u_char *calc_ecc)
869{
870 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
871 mtd);
872 int blockCnt = 0, i = 0, ret = 0;
John Ogness74f1b722011-02-28 13:12:46 +0100873 int stat = 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700874
875 /* Ex NAND_ECC_HW12_2048 */
876 if ((info->nand.ecc.mode == NAND_ECC_HW) &&
877 (info->nand.ecc.size == 2048))
878 blockCnt = 4;
879 else
880 blockCnt = 1;
881
882 for (i = 0; i < blockCnt; i++) {
883 if (memcmp(read_ecc, calc_ecc, 3) != 0) {
884 ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
885 if (ret < 0)
886 return ret;
John Ogness74f1b722011-02-28 13:12:46 +0100887 /* keep track of the number of corrected errors */
888 stat += ret;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700889 }
890 read_ecc += 3;
891 calc_ecc += 3;
892 dat += 512;
893 }
John Ogness74f1b722011-02-28 13:12:46 +0100894 return stat;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700895}
896
897/**
898 * omap_calcuate_ecc - Generate non-inverted ECC bytes.
899 * @mtd: MTD device structure
900 * @dat: The pointer to data on which ecc is computed
901 * @ecc_code: The ecc_code buffer
902 *
903 * Using noninverted ECC can be considered ugly since writing a blank
904 * page ie. padding will clear the ECC bytes. This is no problem as long
905 * nobody is trying to write data on the seemingly unused page. Reading
906 * an erased page will produce an ECC mismatch between generated and read
907 * ECC bytes that has to be dealt with separately.
908 */
909static int omap_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
910 u_char *ecc_code)
911{
912 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
913 mtd);
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700914 u32 val;
915
916 val = readl(info->reg.gpmc_ecc_config);
917 if (((val >> ECC_CONFIG_CS_SHIFT) & ~CS_MASK) != info->gpmc_cs)
918 return -EINVAL;
919
920 /* read ecc result */
921 val = readl(info->reg.gpmc_ecc1_result);
922 *ecc_code++ = val; /* P128e, ..., P1e */
923 *ecc_code++ = val >> 16; /* P128o, ..., P1o */
924 /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
925 *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
926
927 return 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700928}
929
930/**
931 * omap_enable_hwecc - This function enables the hardware ecc functionality
932 * @mtd: MTD device structure
933 * @mode: Read/Write mode
934 */
935static void omap_enable_hwecc(struct mtd_info *mtd, int mode)
936{
937 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
938 mtd);
939 struct nand_chip *chip = mtd->priv;
940 unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700941 u32 val;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700942
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700943 /* clear ecc and enable bits */
944 val = ECCCLEAR | ECC1;
945 writel(val, info->reg.gpmc_ecc_control);
946
947 /* program ecc and result sizes */
948 val = ((((info->nand.ecc.size >> 1) - 1) << ECCSIZE1_SHIFT) |
949 ECC1RESULTSIZE);
950 writel(val, info->reg.gpmc_ecc_size_config);
951
952 switch (mode) {
953 case NAND_ECC_READ:
954 case NAND_ECC_WRITE:
955 writel(ECCCLEAR | ECC1, info->reg.gpmc_ecc_control);
956 break;
957 case NAND_ECC_READSYN:
958 writel(ECCCLEAR, info->reg.gpmc_ecc_control);
959 break;
960 default:
961 dev_info(&info->pdev->dev,
962 "error: unrecognized Mode[%d]!\n", mode);
963 break;
964 }
965
966 /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
967 val = (dev_width << 7) | (info->gpmc_cs << 1) | (0x1);
968 writel(val, info->reg.gpmc_ecc_config);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700969}
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000970
Vimal Singh67ce04b2009-05-12 13:47:03 -0700971/**
972 * omap_wait - wait until the command is done
973 * @mtd: MTD device structure
974 * @chip: NAND Chip structure
975 *
976 * Wait function is called during Program and erase operations and
977 * the way it is called from MTD layer, we should wait till the NAND
978 * chip is ready after the programming/erase operation has completed.
979 *
980 * Erase can take up to 400ms and program up to 20ms according to
981 * general NAND and SmartMedia specs
982 */
983static int omap_wait(struct mtd_info *mtd, struct nand_chip *chip)
984{
985 struct nand_chip *this = mtd->priv;
986 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
987 mtd);
988 unsigned long timeo = jiffies;
Ivan Djelica9c465f2012-04-17 13:11:53 +0200989 int status, state = this->state;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700990
991 if (state == FL_ERASING)
992 timeo += (HZ * 400) / 1000;
993 else
994 timeo += (HZ * 20) / 1000;
995
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700996 writeb(NAND_CMD_STATUS & 0xFF, info->reg.gpmc_nand_command);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700997 while (time_before(jiffies, timeo)) {
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700998 status = readb(info->reg.gpmc_nand_data);
vimal singhc276aca2009-06-27 11:07:06 +0530999 if (status & NAND_STATUS_READY)
Vimal Singh67ce04b2009-05-12 13:47:03 -07001000 break;
vimal singhc276aca2009-06-27 11:07:06 +05301001 cond_resched();
Vimal Singh67ce04b2009-05-12 13:47:03 -07001002 }
Ivan Djelica9c465f2012-04-17 13:11:53 +02001003
Afzal Mohammed4ea1e4b2012-09-29 11:22:21 +05301004 status = readb(info->reg.gpmc_nand_data);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001005 return status;
1006}
1007
1008/**
1009 * omap_dev_ready - calls the platform specific dev_ready function
1010 * @mtd: MTD device structure
1011 */
1012static int omap_dev_ready(struct mtd_info *mtd)
1013{
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +00001014 unsigned int val = 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -07001015 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1016 mtd);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001017
Afzal Mohammed65b97cf2012-08-30 12:53:22 -07001018 val = readl(info->reg.gpmc_status);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001019
Afzal Mohammed65b97cf2012-08-30 12:53:22 -07001020 if ((val & 0x100) == 0x100) {
1021 return 1;
1022 } else {
1023 return 0;
1024 }
Vimal Singh67ce04b2009-05-12 13:47:03 -07001025}
1026
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001027#ifdef CONFIG_MTD_NAND_OMAP_BCH
1028
1029/**
1030 * omap3_enable_hwecc_bch - Program OMAP3 GPMC to perform BCH ECC correction
1031 * @mtd: MTD device structure
1032 * @mode: Read/Write mode
1033 */
1034static void omap3_enable_hwecc_bch(struct mtd_info *mtd, int mode)
1035{
1036 int nerrors;
Afzal Mohammed2ef9f3d2012-10-04 19:03:06 +05301037 unsigned int dev_width, nsectors;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001038 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1039 mtd);
1040 struct nand_chip *chip = mtd->priv;
Afzal Mohammed2ef9f3d2012-10-04 19:03:06 +05301041 u32 val;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001042
1043 nerrors = (info->nand.ecc.bytes == 13) ? 8 : 4;
1044 dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
Afzal Mohammed2ef9f3d2012-10-04 19:03:06 +05301045 nsectors = 1;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001046 /*
1047 * Program GPMC to perform correction on one 512-byte sector at a time.
1048 * Using 4 sectors at a time (i.e. ecc.size = 2048) is also possible and
1049 * gives a slight (5%) performance gain (but requires additional code).
1050 */
Afzal Mohammed2ef9f3d2012-10-04 19:03:06 +05301051
1052 writel(ECC1, info->reg.gpmc_ecc_control);
1053
1054 /*
1055 * When using BCH, sector size is hardcoded to 512 bytes.
1056 * Here we are using wrapping mode 6 both for reading and writing, with:
1057 * size0 = 0 (no additional protected byte in spare area)
1058 * size1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area)
1059 */
1060 val = (32 << ECCSIZE1_SHIFT) | (0 << ECCSIZE0_SHIFT);
1061 writel(val, info->reg.gpmc_ecc_size_config);
1062
1063 /* BCH configuration */
1064 val = ((1 << 16) | /* enable BCH */
1065 (((nerrors == 8) ? 1 : 0) << 12) | /* 8 or 4 bits */
1066 (0x06 << 8) | /* wrap mode = 6 */
1067 (dev_width << 7) | /* bus width */
1068 (((nsectors-1) & 0x7) << 4) | /* number of sectors */
1069 (info->gpmc_cs << 1) | /* ECC CS */
1070 (0x1)); /* enable ECC */
1071
1072 writel(val, info->reg.gpmc_ecc_config);
1073
1074 /* clear ecc and enable bits */
1075 writel(ECCCLEAR | ECC1, info->reg.gpmc_ecc_control);
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001076}
1077
1078/**
1079 * omap3_calculate_ecc_bch4 - Generate 7 bytes of ECC bytes
1080 * @mtd: MTD device structure
1081 * @dat: The pointer to data on which ecc is computed
1082 * @ecc_code: The ecc_code buffer
1083 */
1084static int omap3_calculate_ecc_bch4(struct mtd_info *mtd, const u_char *dat,
1085 u_char *ecc_code)
1086{
1087 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1088 mtd);
Afzal Mohammed2ef9f3d2012-10-04 19:03:06 +05301089 unsigned long nsectors, val1, val2;
1090 int i;
1091
1092 nsectors = ((readl(info->reg.gpmc_ecc_config) >> 4) & 0x7) + 1;
1093
1094 for (i = 0; i < nsectors; i++) {
1095
1096 /* Read hw-computed remainder */
1097 val1 = readl(info->reg.gpmc_bch_result0[i]);
1098 val2 = readl(info->reg.gpmc_bch_result1[i]);
1099
1100 /*
1101 * Add constant polynomial to remainder, in order to get an ecc
1102 * sequence of 0xFFs for a buffer filled with 0xFFs; and
1103 * left-justify the resulting polynomial.
1104 */
1105 *ecc_code++ = 0x28 ^ ((val2 >> 12) & 0xFF);
1106 *ecc_code++ = 0x13 ^ ((val2 >> 4) & 0xFF);
1107 *ecc_code++ = 0xcc ^ (((val2 & 0xF) << 4)|((val1 >> 28) & 0xF));
1108 *ecc_code++ = 0x39 ^ ((val1 >> 20) & 0xFF);
1109 *ecc_code++ = 0x96 ^ ((val1 >> 12) & 0xFF);
1110 *ecc_code++ = 0xac ^ ((val1 >> 4) & 0xFF);
1111 *ecc_code++ = 0x7f ^ ((val1 & 0xF) << 4);
1112 }
1113
1114 return 0;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001115}
1116
1117/**
1118 * omap3_calculate_ecc_bch8 - Generate 13 bytes of ECC bytes
1119 * @mtd: MTD device structure
1120 * @dat: The pointer to data on which ecc is computed
1121 * @ecc_code: The ecc_code buffer
1122 */
1123static int omap3_calculate_ecc_bch8(struct mtd_info *mtd, const u_char *dat,
1124 u_char *ecc_code)
1125{
1126 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1127 mtd);
Afzal Mohammed2ef9f3d2012-10-04 19:03:06 +05301128 unsigned long nsectors, val1, val2, val3, val4;
1129 int i;
1130
1131 nsectors = ((readl(info->reg.gpmc_ecc_config) >> 4) & 0x7) + 1;
1132
1133 for (i = 0; i < nsectors; i++) {
1134
1135 /* Read hw-computed remainder */
1136 val1 = readl(info->reg.gpmc_bch_result0[i]);
1137 val2 = readl(info->reg.gpmc_bch_result1[i]);
1138 val3 = readl(info->reg.gpmc_bch_result2[i]);
1139 val4 = readl(info->reg.gpmc_bch_result3[i]);
1140
1141 /*
1142 * Add constant polynomial to remainder, in order to get an ecc
1143 * sequence of 0xFFs for a buffer filled with 0xFFs.
1144 */
1145 *ecc_code++ = 0xef ^ (val4 & 0xFF);
1146 *ecc_code++ = 0x51 ^ ((val3 >> 24) & 0xFF);
1147 *ecc_code++ = 0x2e ^ ((val3 >> 16) & 0xFF);
1148 *ecc_code++ = 0x09 ^ ((val3 >> 8) & 0xFF);
1149 *ecc_code++ = 0xed ^ (val3 & 0xFF);
1150 *ecc_code++ = 0x93 ^ ((val2 >> 24) & 0xFF);
1151 *ecc_code++ = 0x9a ^ ((val2 >> 16) & 0xFF);
1152 *ecc_code++ = 0xc2 ^ ((val2 >> 8) & 0xFF);
1153 *ecc_code++ = 0x97 ^ (val2 & 0xFF);
1154 *ecc_code++ = 0x79 ^ ((val1 >> 24) & 0xFF);
1155 *ecc_code++ = 0xe5 ^ ((val1 >> 16) & 0xFF);
1156 *ecc_code++ = 0x24 ^ ((val1 >> 8) & 0xFF);
1157 *ecc_code++ = 0xb5 ^ (val1 & 0xFF);
1158 }
1159
1160 return 0;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001161}
1162
1163/**
1164 * omap3_correct_data_bch - Decode received data and correct errors
1165 * @mtd: MTD device structure
1166 * @data: page data
1167 * @read_ecc: ecc read from nand flash
1168 * @calc_ecc: ecc read from HW ECC registers
1169 */
1170static int omap3_correct_data_bch(struct mtd_info *mtd, u_char *data,
1171 u_char *read_ecc, u_char *calc_ecc)
1172{
1173 int i, count;
1174 /* cannot correct more than 8 errors */
1175 unsigned int errloc[8];
1176 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1177 mtd);
1178
1179 count = decode_bch(info->bch, NULL, 512, read_ecc, calc_ecc, NULL,
1180 errloc);
1181 if (count > 0) {
1182 /* correct errors */
1183 for (i = 0; i < count; i++) {
1184 /* correct data only, not ecc bytes */
1185 if (errloc[i] < 8*512)
1186 data[errloc[i]/8] ^= 1 << (errloc[i] & 7);
1187 pr_debug("corrected bitflip %u\n", errloc[i]);
1188 }
1189 } else if (count < 0) {
1190 pr_err("ecc unrecoverable error\n");
1191 }
1192 return count;
1193}
1194
1195/**
1196 * omap3_free_bch - Release BCH ecc resources
1197 * @mtd: MTD device structure
1198 */
1199static void omap3_free_bch(struct mtd_info *mtd)
1200{
1201 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1202 mtd);
1203 if (info->bch) {
1204 free_bch(info->bch);
1205 info->bch = NULL;
1206 }
1207}
1208
1209/**
1210 * omap3_init_bch - Initialize BCH ECC
1211 * @mtd: MTD device structure
1212 * @ecc_opt: OMAP ECC mode (OMAP_ECC_BCH4_CODE_HW or OMAP_ECC_BCH8_CODE_HW)
1213 */
1214static int omap3_init_bch(struct mtd_info *mtd, int ecc_opt)
1215{
Afzal Mohammed2ef9f3d2012-10-04 19:03:06 +05301216 int max_errors;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001217 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1218 mtd);
1219#ifdef CONFIG_MTD_NAND_OMAP_BCH8
1220 const int hw_errors = 8;
1221#else
1222 const int hw_errors = 4;
1223#endif
1224 info->bch = NULL;
1225
1226 max_errors = (ecc_opt == OMAP_ECC_BCH8_CODE_HW) ? 8 : 4;
1227 if (max_errors != hw_errors) {
1228 pr_err("cannot configure %d-bit BCH ecc, only %d-bit supported",
1229 max_errors, hw_errors);
1230 goto fail;
1231 }
1232
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001233 /* software bch library is only used to detect and locate errors */
1234 info->bch = init_bch(13, max_errors, 0x201b /* hw polynomial */);
1235 if (!info->bch)
1236 goto fail;
1237
1238 info->nand.ecc.size = 512;
1239 info->nand.ecc.hwctl = omap3_enable_hwecc_bch;
1240 info->nand.ecc.correct = omap3_correct_data_bch;
1241 info->nand.ecc.mode = NAND_ECC_HW;
1242
1243 /*
1244 * The number of corrected errors in an ecc block that will trigger
1245 * block scrubbing defaults to the ecc strength (4 or 8).
1246 * Set mtd->bitflip_threshold here to define a custom threshold.
1247 */
1248
1249 if (max_errors == 8) {
1250 info->nand.ecc.strength = 8;
1251 info->nand.ecc.bytes = 13;
1252 info->nand.ecc.calculate = omap3_calculate_ecc_bch8;
1253 } else {
1254 info->nand.ecc.strength = 4;
1255 info->nand.ecc.bytes = 7;
1256 info->nand.ecc.calculate = omap3_calculate_ecc_bch4;
1257 }
1258
1259 pr_info("enabling NAND BCH ecc with %d-bit correction\n", max_errors);
1260 return 0;
1261fail:
1262 omap3_free_bch(mtd);
1263 return -1;
1264}
1265
1266/**
1267 * omap3_init_bch_tail - Build an oob layout for BCH ECC correction.
1268 * @mtd: MTD device structure
1269 */
1270static int omap3_init_bch_tail(struct mtd_info *mtd)
1271{
1272 int i, steps;
1273 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1274 mtd);
1275 struct nand_ecclayout *layout = &info->ecclayout;
1276
1277 /* build oob layout */
1278 steps = mtd->writesize/info->nand.ecc.size;
1279 layout->eccbytes = steps*info->nand.ecc.bytes;
1280
1281 /* do not bother creating special oob layouts for small page devices */
1282 if (mtd->oobsize < 64) {
1283 pr_err("BCH ecc is not supported on small page devices\n");
1284 goto fail;
1285 }
1286
1287 /* reserve 2 bytes for bad block marker */
1288 if (layout->eccbytes+2 > mtd->oobsize) {
1289 pr_err("no oob layout available for oobsize %d eccbytes %u\n",
1290 mtd->oobsize, layout->eccbytes);
1291 goto fail;
1292 }
1293
1294 /* put ecc bytes at oob tail */
1295 for (i = 0; i < layout->eccbytes; i++)
1296 layout->eccpos[i] = mtd->oobsize-layout->eccbytes+i;
1297
1298 layout->oobfree[0].offset = 2;
1299 layout->oobfree[0].length = mtd->oobsize-2-layout->eccbytes;
1300 info->nand.ecc.layout = layout;
1301
1302 if (!(info->nand.options & NAND_BUSWIDTH_16))
1303 info->nand.badblock_pattern = &bb_descrip_flashbased;
1304 return 0;
1305fail:
1306 omap3_free_bch(mtd);
1307 return -1;
1308}
1309
1310#else
1311static int omap3_init_bch(struct mtd_info *mtd, int ecc_opt)
1312{
1313 pr_err("CONFIG_MTD_NAND_OMAP_BCH is not enabled\n");
1314 return -1;
1315}
1316static int omap3_init_bch_tail(struct mtd_info *mtd)
1317{
1318 return -1;
1319}
1320static void omap3_free_bch(struct mtd_info *mtd)
1321{
1322}
1323#endif /* CONFIG_MTD_NAND_OMAP_BCH */
1324
Vimal Singh67ce04b2009-05-12 13:47:03 -07001325static int __devinit omap_nand_probe(struct platform_device *pdev)
1326{
1327 struct omap_nand_info *info;
1328 struct omap_nand_platform_data *pdata;
1329 int err;
Sukumar Ghoraif040d332011-01-28 15:42:09 +05301330 int i, offset;
Russell King763e7352012-04-25 00:16:00 +01001331 dma_cap_mask_t mask;
1332 unsigned sig;
Afzal Mohammed9c4c2f82012-08-30 12:53:23 -07001333 struct resource *res;
Vimal Singh67ce04b2009-05-12 13:47:03 -07001334
1335 pdata = pdev->dev.platform_data;
1336 if (pdata == NULL) {
1337 dev_err(&pdev->dev, "platform data missing\n");
1338 return -ENODEV;
1339 }
1340
1341 info = kzalloc(sizeof(struct omap_nand_info), GFP_KERNEL);
1342 if (!info)
1343 return -ENOMEM;
1344
1345 platform_set_drvdata(pdev, info);
1346
1347 spin_lock_init(&info->controller.lock);
1348 init_waitqueue_head(&info->controller.wq);
1349
1350 info->pdev = pdev;
1351
1352 info->gpmc_cs = pdata->cs;
Afzal Mohammed65b97cf2012-08-30 12:53:22 -07001353 info->reg = pdata->reg;
Vimal Singh67ce04b2009-05-12 13:47:03 -07001354
1355 info->mtd.priv = &info->nand;
1356 info->mtd.name = dev_name(&pdev->dev);
1357 info->mtd.owner = THIS_MODULE;
1358
Sukumar Ghoraid5ce2b62011-01-28 15:42:03 +05301359 info->nand.options = pdata->devsize;
Vimal Singh2f70a1e2010-02-15 10:03:33 -08001360 info->nand.options |= NAND_SKIP_BBTSCAN;
Vimal Singh67ce04b2009-05-12 13:47:03 -07001361
Afzal Mohammed9c4c2f82012-08-30 12:53:23 -07001362 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1363 if (res == NULL) {
1364 err = -EINVAL;
1365 dev_err(&pdev->dev, "error getting memory resource\n");
1366 goto out_free_info;
1367 }
Vimal Singh67ce04b2009-05-12 13:47:03 -07001368
Afzal Mohammed9c4c2f82012-08-30 12:53:23 -07001369 info->phys_base = res->start;
1370 info->mem_size = resource_size(res);
1371
1372 if (!request_mem_region(info->phys_base, info->mem_size,
Vimal Singh67ce04b2009-05-12 13:47:03 -07001373 pdev->dev.driver->name)) {
1374 err = -EBUSY;
Vimal Singh2f70a1e2010-02-15 10:03:33 -08001375 goto out_free_info;
Vimal Singh67ce04b2009-05-12 13:47:03 -07001376 }
1377
Afzal Mohammed9c4c2f82012-08-30 12:53:23 -07001378 info->nand.IO_ADDR_R = ioremap(info->phys_base, info->mem_size);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001379 if (!info->nand.IO_ADDR_R) {
1380 err = -ENOMEM;
1381 goto out_release_mem_region;
1382 }
vimal singh59e9c5a2009-07-13 16:26:24 +05301383
Vimal Singh67ce04b2009-05-12 13:47:03 -07001384 info->nand.controller = &info->controller;
1385
1386 info->nand.IO_ADDR_W = info->nand.IO_ADDR_R;
1387 info->nand.cmd_ctrl = omap_hwcontrol;
1388
Vimal Singh67ce04b2009-05-12 13:47:03 -07001389 /*
1390 * If RDY/BSY line is connected to OMAP then use the omap ready
Peter Meerwald4cacbe22012-07-19 13:21:04 +02001391 * function and the generic nand_wait function which reads the status
1392 * register after monitoring the RDY/BSY line. Otherwise use a standard
Vimal Singh67ce04b2009-05-12 13:47:03 -07001393 * chip delay which is slightly more than tR (AC Timing) of the NAND
1394 * device and read status register until you get a failure or success
1395 */
1396 if (pdata->dev_ready) {
1397 info->nand.dev_ready = omap_dev_ready;
1398 info->nand.chip_delay = 0;
1399 } else {
1400 info->nand.waitfunc = omap_wait;
1401 info->nand.chip_delay = 50;
1402 }
1403
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301404 switch (pdata->xfer_type) {
1405 case NAND_OMAP_PREFETCH_POLLED:
vimal singh59e9c5a2009-07-13 16:26:24 +05301406 info->nand.read_buf = omap_read_buf_pref;
1407 info->nand.write_buf = omap_write_buf_pref;
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301408 break;
vimal singhdfe32892009-07-13 16:29:16 +05301409
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301410 case NAND_OMAP_POLLED:
vimal singh59e9c5a2009-07-13 16:26:24 +05301411 if (info->nand.options & NAND_BUSWIDTH_16) {
1412 info->nand.read_buf = omap_read_buf16;
1413 info->nand.write_buf = omap_write_buf16;
1414 } else {
1415 info->nand.read_buf = omap_read_buf8;
1416 info->nand.write_buf = omap_write_buf8;
1417 }
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301418 break;
1419
1420 case NAND_OMAP_PREFETCH_DMA:
Russell King763e7352012-04-25 00:16:00 +01001421 dma_cap_zero(mask);
1422 dma_cap_set(DMA_SLAVE, mask);
1423 sig = OMAP24XX_DMA_GPMC;
1424 info->dma = dma_request_channel(mask, omap_dma_filter_fn, &sig);
1425 if (!info->dma) {
Russell King2df41d02012-04-25 00:19:39 +01001426 dev_err(&pdev->dev, "DMA engine request failed\n");
1427 err = -ENXIO;
1428 goto out_release_mem_region;
Russell King763e7352012-04-25 00:16:00 +01001429 } else {
1430 struct dma_slave_config cfg;
Russell King763e7352012-04-25 00:16:00 +01001431
1432 memset(&cfg, 0, sizeof(cfg));
1433 cfg.src_addr = info->phys_base;
1434 cfg.dst_addr = info->phys_base;
1435 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1436 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1437 cfg.src_maxburst = 16;
1438 cfg.dst_maxburst = 16;
Arnd Bergmannd680e2c2012-08-04 11:05:25 +00001439 err = dmaengine_slave_config(info->dma, &cfg);
1440 if (err) {
Russell King763e7352012-04-25 00:16:00 +01001441 dev_err(&pdev->dev, "DMA engine slave config failed: %d\n",
Arnd Bergmannd680e2c2012-08-04 11:05:25 +00001442 err);
Russell King763e7352012-04-25 00:16:00 +01001443 goto out_release_mem_region;
1444 }
1445 info->nand.read_buf = omap_read_buf_dma_pref;
1446 info->nand.write_buf = omap_write_buf_dma_pref;
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301447 }
1448 break;
1449
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301450 case NAND_OMAP_PREFETCH_IRQ:
Afzal Mohammed5c468452012-08-30 12:53:24 -07001451 info->gpmc_irq_fifo = platform_get_irq(pdev, 0);
1452 if (info->gpmc_irq_fifo <= 0) {
1453 dev_err(&pdev->dev, "error getting fifo irq\n");
1454 err = -ENODEV;
1455 goto out_release_mem_region;
1456 }
1457 err = request_irq(info->gpmc_irq_fifo, omap_nand_irq,
1458 IRQF_SHARED, "gpmc-nand-fifo", info);
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301459 if (err) {
1460 dev_err(&pdev->dev, "requesting irq(%d) error:%d",
Afzal Mohammed5c468452012-08-30 12:53:24 -07001461 info->gpmc_irq_fifo, err);
1462 info->gpmc_irq_fifo = 0;
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301463 goto out_release_mem_region;
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301464 }
Afzal Mohammed5c468452012-08-30 12:53:24 -07001465
1466 info->gpmc_irq_count = platform_get_irq(pdev, 1);
1467 if (info->gpmc_irq_count <= 0) {
1468 dev_err(&pdev->dev, "error getting count irq\n");
1469 err = -ENODEV;
1470 goto out_release_mem_region;
1471 }
1472 err = request_irq(info->gpmc_irq_count, omap_nand_irq,
1473 IRQF_SHARED, "gpmc-nand-count", info);
1474 if (err) {
1475 dev_err(&pdev->dev, "requesting irq(%d) error:%d",
1476 info->gpmc_irq_count, err);
1477 info->gpmc_irq_count = 0;
1478 goto out_release_mem_region;
1479 }
1480
1481 info->nand.read_buf = omap_read_buf_irq_pref;
1482 info->nand.write_buf = omap_write_buf_irq_pref;
1483
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301484 break;
1485
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301486 default:
1487 dev_err(&pdev->dev,
1488 "xfer_type(%d) not supported!\n", pdata->xfer_type);
1489 err = -EINVAL;
1490 goto out_release_mem_region;
vimal singh59e9c5a2009-07-13 16:26:24 +05301491 }
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301492
Peter Meerwald4cacbe22012-07-19 13:21:04 +02001493 /* select the ecc type */
Sukumar Ghoraif3d73f32011-01-28 15:42:08 +05301494 if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_DEFAULT)
1495 info->nand.ecc.mode = NAND_ECC_SOFT;
Sukumar Ghoraif040d332011-01-28 15:42:09 +05301496 else if ((pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW) ||
1497 (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE)) {
Sukumar Ghoraif3d73f32011-01-28 15:42:08 +05301498 info->nand.ecc.bytes = 3;
1499 info->nand.ecc.size = 512;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001500 info->nand.ecc.strength = 1;
Sukumar Ghoraif3d73f32011-01-28 15:42:08 +05301501 info->nand.ecc.calculate = omap_calculate_ecc;
1502 info->nand.ecc.hwctl = omap_enable_hwecc;
1503 info->nand.ecc.correct = omap_correct_data;
1504 info->nand.ecc.mode = NAND_ECC_HW;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001505 } else if ((pdata->ecc_opt == OMAP_ECC_BCH4_CODE_HW) ||
1506 (pdata->ecc_opt == OMAP_ECC_BCH8_CODE_HW)) {
1507 err = omap3_init_bch(&info->mtd, pdata->ecc_opt);
1508 if (err) {
1509 err = -EINVAL;
1510 goto out_release_mem_region;
1511 }
Sukumar Ghoraif3d73f32011-01-28 15:42:08 +05301512 }
Vimal Singh67ce04b2009-05-12 13:47:03 -07001513
1514 /* DIP switches on some boards change between 8 and 16 bit
1515 * bus widths for flash. Try the other width if the first try fails.
1516 */
Jan Weitzela80f1c12011-04-19 16:15:34 +02001517 if (nand_scan_ident(&info->mtd, 1, NULL)) {
Vimal Singh67ce04b2009-05-12 13:47:03 -07001518 info->nand.options ^= NAND_BUSWIDTH_16;
Jan Weitzela80f1c12011-04-19 16:15:34 +02001519 if (nand_scan_ident(&info->mtd, 1, NULL)) {
Vimal Singh67ce04b2009-05-12 13:47:03 -07001520 err = -ENXIO;
1521 goto out_release_mem_region;
1522 }
1523 }
1524
Sukumar Ghoraif040d332011-01-28 15:42:09 +05301525 /* rom code layout */
1526 if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE) {
1527
1528 if (info->nand.options & NAND_BUSWIDTH_16)
1529 offset = 2;
1530 else {
1531 offset = 1;
1532 info->nand.badblock_pattern = &bb_descrip_flashbased;
1533 }
1534 omap_oobinfo.eccbytes = 3 * (info->mtd.oobsize/16);
1535 for (i = 0; i < omap_oobinfo.eccbytes; i++)
1536 omap_oobinfo.eccpos[i] = i+offset;
1537
1538 omap_oobinfo.oobfree->offset = offset + omap_oobinfo.eccbytes;
1539 omap_oobinfo.oobfree->length = info->mtd.oobsize -
1540 (offset + omap_oobinfo.eccbytes);
1541
1542 info->nand.ecc.layout = &omap_oobinfo;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001543 } else if ((pdata->ecc_opt == OMAP_ECC_BCH4_CODE_HW) ||
1544 (pdata->ecc_opt == OMAP_ECC_BCH8_CODE_HW)) {
1545 /* build OOB layout for BCH ECC correction */
1546 err = omap3_init_bch_tail(&info->mtd);
1547 if (err) {
1548 err = -EINVAL;
1549 goto out_release_mem_region;
1550 }
Sukumar Ghoraif040d332011-01-28 15:42:09 +05301551 }
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301552
Jan Weitzela80f1c12011-04-19 16:15:34 +02001553 /* second phase scan */
1554 if (nand_scan_tail(&info->mtd)) {
1555 err = -ENXIO;
1556 goto out_release_mem_region;
1557 }
1558
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001559 mtd_device_parse_register(&info->mtd, NULL, NULL, pdata->parts,
1560 pdata->nr_parts);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001561
1562 platform_set_drvdata(pdev, &info->mtd);
1563
1564 return 0;
1565
1566out_release_mem_region:
Russell King763e7352012-04-25 00:16:00 +01001567 if (info->dma)
1568 dma_release_channel(info->dma);
Afzal Mohammed5c468452012-08-30 12:53:24 -07001569 if (info->gpmc_irq_count > 0)
1570 free_irq(info->gpmc_irq_count, info);
1571 if (info->gpmc_irq_fifo > 0)
1572 free_irq(info->gpmc_irq_fifo, info);
Afzal Mohammed9c4c2f82012-08-30 12:53:23 -07001573 release_mem_region(info->phys_base, info->mem_size);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001574out_free_info:
1575 kfree(info);
1576
1577 return err;
1578}
1579
1580static int omap_nand_remove(struct platform_device *pdev)
1581{
1582 struct mtd_info *mtd = platform_get_drvdata(pdev);
Vimal Singhf35b6ed2010-01-05 16:01:08 +05301583 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1584 mtd);
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001585 omap3_free_bch(&info->mtd);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001586
1587 platform_set_drvdata(pdev, NULL);
Russell King763e7352012-04-25 00:16:00 +01001588 if (info->dma)
1589 dma_release_channel(info->dma);
1590
Afzal Mohammed5c468452012-08-30 12:53:24 -07001591 if (info->gpmc_irq_count > 0)
1592 free_irq(info->gpmc_irq_count, info);
1593 if (info->gpmc_irq_fifo > 0)
1594 free_irq(info->gpmc_irq_fifo, info);
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301595
Vimal Singh67ce04b2009-05-12 13:47:03 -07001596 /* Release NAND device, its internal structures and partitions */
1597 nand_release(&info->mtd);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +00001598 iounmap(info->nand.IO_ADDR_R);
Afzal Mohammed48b51d42012-09-29 11:14:47 +05301599 release_mem_region(info->phys_base, info->mem_size);
Andreas Bießmann7d9b1102012-08-31 13:35:41 +02001600 kfree(info);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001601 return 0;
1602}
1603
1604static struct platform_driver omap_nand_driver = {
1605 .probe = omap_nand_probe,
1606 .remove = omap_nand_remove,
1607 .driver = {
1608 .name = DRIVER_NAME,
1609 .owner = THIS_MODULE,
1610 },
1611};
1612
Axel Linf99640d2011-11-27 20:45:03 +08001613module_platform_driver(omap_nand_driver);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001614
Axel Linc804c732011-03-07 11:04:24 +08001615MODULE_ALIAS("platform:" DRIVER_NAME);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001616MODULE_LICENSE("GPL");
1617MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");