blob: 3105f22993cd7ee353665f5aa2994f4ce6113eba [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>
31#include <plat/gpmc.h>
Arnd Bergmann22037472012-08-24 15:21:06 +020032#include <linux/platform_data/mtd-nand-omap2.h>
Vimal Singh67ce04b2009-05-12 13:47:03 -070033
Vimal Singh67ce04b2009-05-12 13:47:03 -070034#define DRIVER_NAME "omap2-nand"
Sukumar Ghorai4e070372011-01-28 15:42:06 +053035#define OMAP_NAND_TIMEOUT_MS 5000
Vimal Singh67ce04b2009-05-12 13:47:03 -070036
Vimal Singh67ce04b2009-05-12 13:47:03 -070037#define NAND_Ecc_P1e (1 << 0)
38#define NAND_Ecc_P2e (1 << 1)
39#define NAND_Ecc_P4e (1 << 2)
40#define NAND_Ecc_P8e (1 << 3)
41#define NAND_Ecc_P16e (1 << 4)
42#define NAND_Ecc_P32e (1 << 5)
43#define NAND_Ecc_P64e (1 << 6)
44#define NAND_Ecc_P128e (1 << 7)
45#define NAND_Ecc_P256e (1 << 8)
46#define NAND_Ecc_P512e (1 << 9)
47#define NAND_Ecc_P1024e (1 << 10)
48#define NAND_Ecc_P2048e (1 << 11)
49
50#define NAND_Ecc_P1o (1 << 16)
51#define NAND_Ecc_P2o (1 << 17)
52#define NAND_Ecc_P4o (1 << 18)
53#define NAND_Ecc_P8o (1 << 19)
54#define NAND_Ecc_P16o (1 << 20)
55#define NAND_Ecc_P32o (1 << 21)
56#define NAND_Ecc_P64o (1 << 22)
57#define NAND_Ecc_P128o (1 << 23)
58#define NAND_Ecc_P256o (1 << 24)
59#define NAND_Ecc_P512o (1 << 25)
60#define NAND_Ecc_P1024o (1 << 26)
61#define NAND_Ecc_P2048o (1 << 27)
62
63#define TF(value) (value ? 1 : 0)
64
65#define P2048e(a) (TF(a & NAND_Ecc_P2048e) << 0)
66#define P2048o(a) (TF(a & NAND_Ecc_P2048o) << 1)
67#define P1e(a) (TF(a & NAND_Ecc_P1e) << 2)
68#define P1o(a) (TF(a & NAND_Ecc_P1o) << 3)
69#define P2e(a) (TF(a & NAND_Ecc_P2e) << 4)
70#define P2o(a) (TF(a & NAND_Ecc_P2o) << 5)
71#define P4e(a) (TF(a & NAND_Ecc_P4e) << 6)
72#define P4o(a) (TF(a & NAND_Ecc_P4o) << 7)
73
74#define P8e(a) (TF(a & NAND_Ecc_P8e) << 0)
75#define P8o(a) (TF(a & NAND_Ecc_P8o) << 1)
76#define P16e(a) (TF(a & NAND_Ecc_P16e) << 2)
77#define P16o(a) (TF(a & NAND_Ecc_P16o) << 3)
78#define P32e(a) (TF(a & NAND_Ecc_P32e) << 4)
79#define P32o(a) (TF(a & NAND_Ecc_P32o) << 5)
80#define P64e(a) (TF(a & NAND_Ecc_P64e) << 6)
81#define P64o(a) (TF(a & NAND_Ecc_P64o) << 7)
82
83#define P128e(a) (TF(a & NAND_Ecc_P128e) << 0)
84#define P128o(a) (TF(a & NAND_Ecc_P128o) << 1)
85#define P256e(a) (TF(a & NAND_Ecc_P256e) << 2)
86#define P256o(a) (TF(a & NAND_Ecc_P256o) << 3)
87#define P512e(a) (TF(a & NAND_Ecc_P512e) << 4)
88#define P512o(a) (TF(a & NAND_Ecc_P512o) << 5)
89#define P1024e(a) (TF(a & NAND_Ecc_P1024e) << 6)
90#define P1024o(a) (TF(a & NAND_Ecc_P1024o) << 7)
91
92#define P8e_s(a) (TF(a & NAND_Ecc_P8e) << 0)
93#define P8o_s(a) (TF(a & NAND_Ecc_P8o) << 1)
94#define P16e_s(a) (TF(a & NAND_Ecc_P16e) << 2)
95#define P16o_s(a) (TF(a & NAND_Ecc_P16o) << 3)
96#define P1e_s(a) (TF(a & NAND_Ecc_P1e) << 4)
97#define P1o_s(a) (TF(a & NAND_Ecc_P1o) << 5)
98#define P2e_s(a) (TF(a & NAND_Ecc_P2e) << 6)
99#define P2o_s(a) (TF(a & NAND_Ecc_P2o) << 7)
100
101#define P4e_s(a) (TF(a & NAND_Ecc_P4e) << 0)
102#define P4o_s(a) (TF(a & NAND_Ecc_P4o) << 1)
103
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700104#define PREFETCH_CONFIG1_CS_SHIFT 24
105#define ECC_CONFIG_CS_SHIFT 1
106#define CS_MASK 0x7
107#define ENABLE_PREFETCH (0x1 << 7)
108#define DMA_MPU_MODE_SHIFT 2
109#define ECCSIZE1_SHIFT 22
110#define ECC1RESULTSIZE 0x1
111#define ECCCLEAR 0x100
112#define ECC1 0x1
113
Lokesh Vutlad5e7c862012-10-15 14:03:51 -0700114#define OMAP24XX_DMA_GPMC 4
115
Sukumar Ghoraif040d332011-01-28 15:42:09 +0530116/* oob info generated runtime depending on ecc algorithm and layout selected */
117static struct nand_ecclayout omap_oobinfo;
118/* Define some generic bad / good block scan pattern which are used
119 * while scanning a device for factory marked good / bad blocks
120 */
121static uint8_t scan_ff_pattern[] = { 0xff };
122static struct nand_bbt_descr bb_descrip_flashbased = {
123 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
124 .offs = 0,
125 .len = 1,
126 .pattern = scan_ff_pattern,
127};
vimal singh59e9c5a2009-07-13 16:26:24 +0530128
vimal singh59e9c5a2009-07-13 16:26:24 +0530129
Vimal Singh67ce04b2009-05-12 13:47:03 -0700130struct omap_nand_info {
131 struct nand_hw_control controller;
132 struct omap_nand_platform_data *pdata;
133 struct mtd_info mtd;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700134 struct nand_chip nand;
135 struct platform_device *pdev;
136
137 int gpmc_cs;
138 unsigned long phys_base;
Afzal Mohammed9c4c2f82012-08-30 12:53:23 -0700139 unsigned long mem_size;
vimal singhdfe32892009-07-13 16:29:16 +0530140 struct completion comp;
Russell King763e7352012-04-25 00:16:00 +0100141 struct dma_chan *dma;
Afzal Mohammed5c468452012-08-30 12:53:24 -0700142 int gpmc_irq_fifo;
143 int gpmc_irq_count;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530144 enum {
145 OMAP_NAND_IO_READ = 0, /* read */
146 OMAP_NAND_IO_WRITE, /* write */
147 } iomode;
148 u_char *buf;
149 int buf_len;
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700150 struct gpmc_nand_regs reg;
Ivan Djelic0e618ef2012-04-30 12:17:18 +0200151
152#ifdef CONFIG_MTD_NAND_OMAP_BCH
153 struct bch_control *bch;
154 struct nand_ecclayout ecclayout;
155#endif
Vimal Singh67ce04b2009-05-12 13:47:03 -0700156};
157
158/**
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700159 * omap_prefetch_enable - configures and starts prefetch transfer
160 * @cs: cs (chip select) number
161 * @fifo_th: fifo threshold to be used for read/ write
162 * @dma_mode: dma mode enable (1) or disable (0)
163 * @u32_count: number of bytes to be transferred
164 * @is_write: prefetch read(0) or write post(1) mode
165 */
166static int omap_prefetch_enable(int cs, int fifo_th, int dma_mode,
167 unsigned int u32_count, int is_write, struct omap_nand_info *info)
168{
169 u32 val;
170
171 if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX)
172 return -1;
173
174 if (readl(info->reg.gpmc_prefetch_control))
175 return -EBUSY;
176
177 /* Set the amount of bytes to be prefetched */
178 writel(u32_count, info->reg.gpmc_prefetch_config2);
179
180 /* Set dma/mpu mode, the prefetch read / post write and
181 * enable the engine. Set which cs is has requested for.
182 */
183 val = ((cs << PREFETCH_CONFIG1_CS_SHIFT) |
184 PREFETCH_FIFOTHRESHOLD(fifo_th) | ENABLE_PREFETCH |
185 (dma_mode << DMA_MPU_MODE_SHIFT) | (0x1 & is_write));
186 writel(val, info->reg.gpmc_prefetch_config1);
187
188 /* Start the prefetch engine */
189 writel(0x1, info->reg.gpmc_prefetch_control);
190
191 return 0;
192}
193
194/**
195 * omap_prefetch_reset - disables and stops the prefetch engine
196 */
197static int omap_prefetch_reset(int cs, struct omap_nand_info *info)
198{
199 u32 config1;
200
201 /* check if the same module/cs is trying to reset */
202 config1 = readl(info->reg.gpmc_prefetch_config1);
203 if (((config1 >> PREFETCH_CONFIG1_CS_SHIFT) & CS_MASK) != cs)
204 return -EINVAL;
205
206 /* Stop the PFPW engine */
207 writel(0x0, info->reg.gpmc_prefetch_control);
208
209 /* Reset/disable the PFPW engine */
210 writel(0x0, info->reg.gpmc_prefetch_config1);
211
212 return 0;
213}
214
215/**
Vimal Singh67ce04b2009-05-12 13:47:03 -0700216 * omap_hwcontrol - hardware specific access to control-lines
217 * @mtd: MTD device structure
218 * @cmd: command to device
219 * @ctrl:
220 * NAND_NCE: bit 0 -> don't care
221 * NAND_CLE: bit 1 -> Command Latch
222 * NAND_ALE: bit 2 -> Address Latch
223 *
224 * NOTE: boards may use different bits for these!!
225 */
226static void omap_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
227{
228 struct omap_nand_info *info = container_of(mtd,
229 struct omap_nand_info, mtd);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700230
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000231 if (cmd != NAND_CMD_NONE) {
232 if (ctrl & NAND_CLE)
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700233 writeb(cmd, info->reg.gpmc_nand_command);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700234
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000235 else if (ctrl & NAND_ALE)
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700236 writeb(cmd, info->reg.gpmc_nand_address);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000237
238 else /* NAND_NCE */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700239 writeb(cmd, info->reg.gpmc_nand_data);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700240 }
Vimal Singh67ce04b2009-05-12 13:47:03 -0700241}
242
243/**
vimal singh59e9c5a2009-07-13 16:26:24 +0530244 * omap_read_buf8 - read data from NAND controller into buffer
245 * @mtd: MTD device structure
246 * @buf: buffer to store date
247 * @len: number of bytes to read
248 */
249static void omap_read_buf8(struct mtd_info *mtd, u_char *buf, int len)
250{
251 struct nand_chip *nand = mtd->priv;
252
253 ioread8_rep(nand->IO_ADDR_R, buf, len);
254}
255
256/**
257 * omap_write_buf8 - write buffer to NAND controller
258 * @mtd: MTD device structure
259 * @buf: data buffer
260 * @len: number of bytes to write
261 */
262static void omap_write_buf8(struct mtd_info *mtd, const u_char *buf, int len)
263{
264 struct omap_nand_info *info = container_of(mtd,
265 struct omap_nand_info, mtd);
266 u_char *p = (u_char *)buf;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000267 u32 status = 0;
vimal singh59e9c5a2009-07-13 16:26:24 +0530268
269 while (len--) {
270 iowrite8(*p++, info->nand.IO_ADDR_W);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000271 /* wait until buffer is available for write */
272 do {
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700273 status = readl(info->reg.gpmc_status) &
274 GPMC_STATUS_BUFF_EMPTY;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000275 } while (!status);
vimal singh59e9c5a2009-07-13 16:26:24 +0530276 }
277}
278
279/**
Vimal Singh67ce04b2009-05-12 13:47:03 -0700280 * omap_read_buf16 - read data from NAND controller into buffer
281 * @mtd: MTD device structure
282 * @buf: buffer to store date
283 * @len: number of bytes to read
284 */
285static void omap_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
286{
287 struct nand_chip *nand = mtd->priv;
288
vimal singh59e9c5a2009-07-13 16:26:24 +0530289 ioread16_rep(nand->IO_ADDR_R, buf, len / 2);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700290}
291
292/**
293 * omap_write_buf16 - write buffer to NAND controller
294 * @mtd: MTD device structure
295 * @buf: data buffer
296 * @len: number of bytes to write
297 */
298static void omap_write_buf16(struct mtd_info *mtd, const u_char * buf, int len)
299{
300 struct omap_nand_info *info = container_of(mtd,
301 struct omap_nand_info, mtd);
302 u16 *p = (u16 *) buf;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000303 u32 status = 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700304 /* FIXME try bursts of writesw() or DMA ... */
305 len >>= 1;
306
307 while (len--) {
vimal singh59e9c5a2009-07-13 16:26:24 +0530308 iowrite16(*p++, info->nand.IO_ADDR_W);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000309 /* wait until buffer is available for write */
310 do {
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700311 status = readl(info->reg.gpmc_status) &
312 GPMC_STATUS_BUFF_EMPTY;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000313 } while (!status);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700314 }
315}
vimal singh59e9c5a2009-07-13 16:26:24 +0530316
317/**
318 * omap_read_buf_pref - read data from NAND controller into buffer
319 * @mtd: MTD device structure
320 * @buf: buffer to store date
321 * @len: number of bytes to read
322 */
323static void omap_read_buf_pref(struct mtd_info *mtd, u_char *buf, int len)
324{
325 struct omap_nand_info *info = container_of(mtd,
326 struct omap_nand_info, mtd);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000327 uint32_t r_count = 0;
vimal singh59e9c5a2009-07-13 16:26:24 +0530328 int ret = 0;
329 u32 *p = (u32 *)buf;
330
331 /* take care of subpage reads */
Vimal Singhc3341d02010-01-07 12:16:26 +0530332 if (len % 4) {
333 if (info->nand.options & NAND_BUSWIDTH_16)
334 omap_read_buf16(mtd, buf, len % 4);
335 else
336 omap_read_buf8(mtd, buf, len % 4);
337 p = (u32 *) (buf + len % 4);
338 len -= len % 4;
vimal singh59e9c5a2009-07-13 16:26:24 +0530339 }
vimal singh59e9c5a2009-07-13 16:26:24 +0530340
341 /* configure and start prefetch transfer */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700342 ret = omap_prefetch_enable(info->gpmc_cs,
343 PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x0, info);
vimal singh59e9c5a2009-07-13 16:26:24 +0530344 if (ret) {
345 /* PFPW engine is busy, use cpu copy method */
346 if (info->nand.options & NAND_BUSWIDTH_16)
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530347 omap_read_buf16(mtd, (u_char *)p, len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530348 else
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530349 omap_read_buf8(mtd, (u_char *)p, len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530350 } else {
351 do {
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700352 r_count = readl(info->reg.gpmc_prefetch_status);
353 r_count = GPMC_PREFETCH_STATUS_FIFO_CNT(r_count);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000354 r_count = r_count >> 2;
355 ioread32_rep(info->nand.IO_ADDR_R, p, r_count);
vimal singh59e9c5a2009-07-13 16:26:24 +0530356 p += r_count;
357 len -= r_count << 2;
358 } while (len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530359 /* disable and stop the PFPW engine */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700360 omap_prefetch_reset(info->gpmc_cs, info);
vimal singh59e9c5a2009-07-13 16:26:24 +0530361 }
362}
363
364/**
365 * omap_write_buf_pref - write buffer to NAND controller
366 * @mtd: MTD device structure
367 * @buf: data buffer
368 * @len: number of bytes to write
369 */
370static void omap_write_buf_pref(struct mtd_info *mtd,
371 const u_char *buf, int len)
372{
373 struct omap_nand_info *info = container_of(mtd,
374 struct omap_nand_info, mtd);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530375 uint32_t w_count = 0;
vimal singh59e9c5a2009-07-13 16:26:24 +0530376 int i = 0, ret = 0;
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530377 u16 *p = (u16 *)buf;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530378 unsigned long tim, limit;
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700379 u32 val;
vimal singh59e9c5a2009-07-13 16:26:24 +0530380
381 /* take care of subpage writes */
382 if (len % 2 != 0) {
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000383 writeb(*buf, info->nand.IO_ADDR_W);
vimal singh59e9c5a2009-07-13 16:26:24 +0530384 p = (u16 *)(buf + 1);
385 len--;
386 }
387
388 /* configure and start prefetch transfer */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700389 ret = omap_prefetch_enable(info->gpmc_cs,
390 PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x1, info);
vimal singh59e9c5a2009-07-13 16:26:24 +0530391 if (ret) {
392 /* PFPW engine is busy, use cpu copy method */
393 if (info->nand.options & NAND_BUSWIDTH_16)
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530394 omap_write_buf16(mtd, (u_char *)p, len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530395 else
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530396 omap_write_buf8(mtd, (u_char *)p, len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530397 } else {
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000398 while (len) {
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700399 w_count = readl(info->reg.gpmc_prefetch_status);
400 w_count = GPMC_PREFETCH_STATUS_FIFO_CNT(w_count);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000401 w_count = w_count >> 1;
vimal singh59e9c5a2009-07-13 16:26:24 +0530402 for (i = 0; (i < w_count) && len; i++, len -= 2)
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000403 iowrite16(*p++, info->nand.IO_ADDR_W);
vimal singh59e9c5a2009-07-13 16:26:24 +0530404 }
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000405 /* wait for data to flushed-out before reset the prefetch */
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530406 tim = 0;
407 limit = (loops_per_jiffy *
408 msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700409 do {
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530410 cpu_relax();
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700411 val = readl(info->reg.gpmc_prefetch_status);
412 val = GPMC_PREFETCH_STATUS_COUNT(val);
413 } while (val && (tim++ < limit));
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530414
vimal singh59e9c5a2009-07-13 16:26:24 +0530415 /* disable and stop the PFPW engine */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700416 omap_prefetch_reset(info->gpmc_cs, info);
vimal singh59e9c5a2009-07-13 16:26:24 +0530417 }
418}
419
vimal singhdfe32892009-07-13 16:29:16 +0530420/*
Russell King2df41d02012-04-25 00:19:39 +0100421 * omap_nand_dma_callback: callback on the completion of dma transfer
vimal singhdfe32892009-07-13 16:29:16 +0530422 * @data: pointer to completion data structure
423 */
Russell King763e7352012-04-25 00:16:00 +0100424static void omap_nand_dma_callback(void *data)
425{
426 complete((struct completion *) data);
427}
vimal singhdfe32892009-07-13 16:29:16 +0530428
429/*
Peter Meerwald4cacbe22012-07-19 13:21:04 +0200430 * omap_nand_dma_transfer: configure and start dma transfer
vimal singhdfe32892009-07-13 16:29:16 +0530431 * @mtd: MTD device structure
432 * @addr: virtual address in RAM of source/destination
433 * @len: number of data bytes to be transferred
434 * @is_write: flag for read/write operation
435 */
436static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
437 unsigned int len, int is_write)
438{
439 struct omap_nand_info *info = container_of(mtd,
440 struct omap_nand_info, mtd);
Russell King2df41d02012-04-25 00:19:39 +0100441 struct dma_async_tx_descriptor *tx;
vimal singhdfe32892009-07-13 16:29:16 +0530442 enum dma_data_direction dir = is_write ? DMA_TO_DEVICE :
443 DMA_FROM_DEVICE;
Russell King2df41d02012-04-25 00:19:39 +0100444 struct scatterlist sg;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530445 unsigned long tim, limit;
Russell King2df41d02012-04-25 00:19:39 +0100446 unsigned n;
447 int ret;
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700448 u32 val;
vimal singhdfe32892009-07-13 16:29:16 +0530449
450 if (addr >= high_memory) {
451 struct page *p1;
452
453 if (((size_t)addr & PAGE_MASK) !=
454 ((size_t)(addr + len - 1) & PAGE_MASK))
455 goto out_copy;
456 p1 = vmalloc_to_page(addr);
457 if (!p1)
458 goto out_copy;
459 addr = page_address(p1) + ((size_t)addr & ~PAGE_MASK);
460 }
461
Russell King2df41d02012-04-25 00:19:39 +0100462 sg_init_one(&sg, addr, len);
463 n = dma_map_sg(info->dma->device->dev, &sg, 1, dir);
464 if (n == 0) {
vimal singhdfe32892009-07-13 16:29:16 +0530465 dev_err(&info->pdev->dev,
466 "Couldn't DMA map a %d byte buffer\n", len);
467 goto out_copy;
468 }
469
Russell King2df41d02012-04-25 00:19:39 +0100470 tx = dmaengine_prep_slave_sg(info->dma, &sg, n,
471 is_write ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
472 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
473 if (!tx)
474 goto out_copy_unmap;
475
476 tx->callback = omap_nand_dma_callback;
477 tx->callback_param = &info->comp;
478 dmaengine_submit(tx);
479
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700480 /* configure and start prefetch transfer */
481 ret = omap_prefetch_enable(info->gpmc_cs,
482 PREFETCH_FIFOTHRESHOLD_MAX, 0x1, len, is_write, info);
vimal singhdfe32892009-07-13 16:29:16 +0530483 if (ret)
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530484 /* PFPW engine is busy, use cpu copy method */
Grazvydas Ignotasd7efe222012-04-11 04:04:34 +0300485 goto out_copy_unmap;
vimal singhdfe32892009-07-13 16:29:16 +0530486
487 init_completion(&info->comp);
Russell King2df41d02012-04-25 00:19:39 +0100488 dma_async_issue_pending(info->dma);
vimal singhdfe32892009-07-13 16:29:16 +0530489
490 /* setup and start DMA using dma_addr */
491 wait_for_completion(&info->comp);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530492 tim = 0;
493 limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700494
495 do {
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530496 cpu_relax();
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700497 val = readl(info->reg.gpmc_prefetch_status);
498 val = GPMC_PREFETCH_STATUS_COUNT(val);
499 } while (val && (tim++ < limit));
vimal singhdfe32892009-07-13 16:29:16 +0530500
vimal singhdfe32892009-07-13 16:29:16 +0530501 /* disable and stop the PFPW engine */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700502 omap_prefetch_reset(info->gpmc_cs, info);
vimal singhdfe32892009-07-13 16:29:16 +0530503
Russell King2df41d02012-04-25 00:19:39 +0100504 dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
vimal singhdfe32892009-07-13 16:29:16 +0530505 return 0;
506
Grazvydas Ignotasd7efe222012-04-11 04:04:34 +0300507out_copy_unmap:
Russell King2df41d02012-04-25 00:19:39 +0100508 dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
vimal singhdfe32892009-07-13 16:29:16 +0530509out_copy:
510 if (info->nand.options & NAND_BUSWIDTH_16)
511 is_write == 0 ? omap_read_buf16(mtd, (u_char *) addr, len)
512 : omap_write_buf16(mtd, (u_char *) addr, len);
513 else
514 is_write == 0 ? omap_read_buf8(mtd, (u_char *) addr, len)
515 : omap_write_buf8(mtd, (u_char *) addr, len);
516 return 0;
517}
vimal singhdfe32892009-07-13 16:29:16 +0530518
519/**
520 * omap_read_buf_dma_pref - read data from NAND controller into buffer
521 * @mtd: MTD device structure
522 * @buf: buffer to store date
523 * @len: number of bytes to read
524 */
525static void omap_read_buf_dma_pref(struct mtd_info *mtd, u_char *buf, int len)
526{
527 if (len <= mtd->oobsize)
528 omap_read_buf_pref(mtd, buf, len);
529 else
530 /* start transfer in DMA mode */
531 omap_nand_dma_transfer(mtd, buf, len, 0x0);
532}
533
534/**
535 * omap_write_buf_dma_pref - write buffer to NAND controller
536 * @mtd: MTD device structure
537 * @buf: data buffer
538 * @len: number of bytes to write
539 */
540static void omap_write_buf_dma_pref(struct mtd_info *mtd,
541 const u_char *buf, int len)
542{
543 if (len <= mtd->oobsize)
544 omap_write_buf_pref(mtd, buf, len);
545 else
546 /* start transfer in DMA mode */
Vimal Singhbdaefc42010-01-05 12:49:24 +0530547 omap_nand_dma_transfer(mtd, (u_char *) buf, len, 0x1);
vimal singhdfe32892009-07-13 16:29:16 +0530548}
549
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530550/*
Peter Meerwald4cacbe22012-07-19 13:21:04 +0200551 * omap_nand_irq - GPMC irq handler
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530552 * @this_irq: gpmc irq number
553 * @dev: omap_nand_info structure pointer is passed here
554 */
555static irqreturn_t omap_nand_irq(int this_irq, void *dev)
556{
557 struct omap_nand_info *info = (struct omap_nand_info *) dev;
558 u32 bytes;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530559
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700560 bytes = readl(info->reg.gpmc_prefetch_status);
561 bytes = GPMC_PREFETCH_STATUS_FIFO_CNT(bytes);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530562 bytes = bytes & 0xFFFC; /* io in multiple of 4 bytes */
563 if (info->iomode == OMAP_NAND_IO_WRITE) { /* checks for write io */
Afzal Mohammed5c468452012-08-30 12:53:24 -0700564 if (this_irq == info->gpmc_irq_count)
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530565 goto done;
566
567 if (info->buf_len && (info->buf_len < bytes))
568 bytes = info->buf_len;
569 else if (!info->buf_len)
570 bytes = 0;
571 iowrite32_rep(info->nand.IO_ADDR_W,
572 (u32 *)info->buf, bytes >> 2);
573 info->buf = info->buf + bytes;
574 info->buf_len -= bytes;
575
576 } else {
577 ioread32_rep(info->nand.IO_ADDR_R,
578 (u32 *)info->buf, bytes >> 2);
579 info->buf = info->buf + bytes;
580
Afzal Mohammed5c468452012-08-30 12:53:24 -0700581 if (this_irq == info->gpmc_irq_count)
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530582 goto done;
583 }
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530584
585 return IRQ_HANDLED;
586
587done:
588 complete(&info->comp);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530589
Afzal Mohammed5c468452012-08-30 12:53:24 -0700590 disable_irq_nosync(info->gpmc_irq_fifo);
591 disable_irq_nosync(info->gpmc_irq_count);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530592
593 return IRQ_HANDLED;
594}
595
596/*
597 * omap_read_buf_irq_pref - read data from NAND controller into buffer
598 * @mtd: MTD device structure
599 * @buf: buffer to store date
600 * @len: number of bytes to read
601 */
602static void omap_read_buf_irq_pref(struct mtd_info *mtd, u_char *buf, int len)
603{
604 struct omap_nand_info *info = container_of(mtd,
605 struct omap_nand_info, mtd);
606 int ret = 0;
607
608 if (len <= mtd->oobsize) {
609 omap_read_buf_pref(mtd, buf, len);
610 return;
611 }
612
613 info->iomode = OMAP_NAND_IO_READ;
614 info->buf = buf;
615 init_completion(&info->comp);
616
617 /* configure and start prefetch transfer */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700618 ret = omap_prefetch_enable(info->gpmc_cs,
619 PREFETCH_FIFOTHRESHOLD_MAX/2, 0x0, len, 0x0, info);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530620 if (ret)
621 /* PFPW engine is busy, use cpu copy method */
622 goto out_copy;
623
624 info->buf_len = len;
Afzal Mohammed5c468452012-08-30 12:53:24 -0700625
626 enable_irq(info->gpmc_irq_count);
627 enable_irq(info->gpmc_irq_fifo);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530628
629 /* waiting for read to complete */
630 wait_for_completion(&info->comp);
631
632 /* disable and stop the PFPW engine */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700633 omap_prefetch_reset(info->gpmc_cs, info);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530634 return;
635
636out_copy:
637 if (info->nand.options & NAND_BUSWIDTH_16)
638 omap_read_buf16(mtd, buf, len);
639 else
640 omap_read_buf8(mtd, buf, len);
641}
642
643/*
644 * omap_write_buf_irq_pref - write buffer to NAND controller
645 * @mtd: MTD device structure
646 * @buf: data buffer
647 * @len: number of bytes to write
648 */
649static void omap_write_buf_irq_pref(struct mtd_info *mtd,
650 const u_char *buf, int len)
651{
652 struct omap_nand_info *info = container_of(mtd,
653 struct omap_nand_info, mtd);
654 int ret = 0;
655 unsigned long tim, limit;
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700656 u32 val;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530657
658 if (len <= mtd->oobsize) {
659 omap_write_buf_pref(mtd, buf, len);
660 return;
661 }
662
663 info->iomode = OMAP_NAND_IO_WRITE;
664 info->buf = (u_char *) buf;
665 init_completion(&info->comp);
666
Sukumar Ghorai317379a2011-01-28 15:42:07 +0530667 /* configure and start prefetch transfer : size=24 */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700668 ret = omap_prefetch_enable(info->gpmc_cs,
669 (PREFETCH_FIFOTHRESHOLD_MAX * 3) / 8, 0x0, len, 0x1, info);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530670 if (ret)
671 /* PFPW engine is busy, use cpu copy method */
672 goto out_copy;
673
674 info->buf_len = len;
Afzal Mohammed5c468452012-08-30 12:53:24 -0700675
676 enable_irq(info->gpmc_irq_count);
677 enable_irq(info->gpmc_irq_fifo);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530678
679 /* waiting for write to complete */
680 wait_for_completion(&info->comp);
Afzal Mohammed5c468452012-08-30 12:53:24 -0700681
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530682 /* wait for data to flushed-out before reset the prefetch */
683 tim = 0;
684 limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700685 do {
686 val = readl(info->reg.gpmc_prefetch_status);
687 val = GPMC_PREFETCH_STATUS_COUNT(val);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530688 cpu_relax();
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700689 } while (val && (tim++ < limit));
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530690
691 /* disable and stop the PFPW engine */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700692 omap_prefetch_reset(info->gpmc_cs, info);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530693 return;
694
695out_copy:
696 if (info->nand.options & NAND_BUSWIDTH_16)
697 omap_write_buf16(mtd, buf, len);
698 else
699 omap_write_buf8(mtd, buf, len);
700}
701
Vimal Singh67ce04b2009-05-12 13:47:03 -0700702/**
Vimal Singh67ce04b2009-05-12 13:47:03 -0700703 * gen_true_ecc - This function will generate true ECC value
704 * @ecc_buf: buffer to store ecc code
705 *
706 * This generated true ECC value can be used when correcting
707 * data read from NAND flash memory core
708 */
709static void gen_true_ecc(u8 *ecc_buf)
710{
711 u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
712 ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
713
714 ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
715 P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
716 ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
717 P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
718 ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
719 P1e(tmp) | P2048o(tmp) | P2048e(tmp));
720}
721
722/**
723 * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data
724 * @ecc_data1: ecc code from nand spare area
725 * @ecc_data2: ecc code from hardware register obtained from hardware ecc
726 * @page_data: page data
727 *
728 * This function compares two ECC's and indicates if there is an error.
729 * If the error can be corrected it will be corrected to the buffer.
John Ogness74f1b722011-02-28 13:12:46 +0100730 * If there is no error, %0 is returned. If there is an error but it
731 * was corrected, %1 is returned. Otherwise, %-1 is returned.
Vimal Singh67ce04b2009-05-12 13:47:03 -0700732 */
733static int omap_compare_ecc(u8 *ecc_data1, /* read from NAND memory */
734 u8 *ecc_data2, /* read from register */
735 u8 *page_data)
736{
737 uint i;
738 u8 tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
739 u8 comp0_bit[8], comp1_bit[8], comp2_bit[8];
740 u8 ecc_bit[24];
741 u8 ecc_sum = 0;
742 u8 find_bit = 0;
743 uint find_byte = 0;
744 int isEccFF;
745
746 isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
747
748 gen_true_ecc(ecc_data1);
749 gen_true_ecc(ecc_data2);
750
751 for (i = 0; i <= 2; i++) {
752 *(ecc_data1 + i) = ~(*(ecc_data1 + i));
753 *(ecc_data2 + i) = ~(*(ecc_data2 + i));
754 }
755
756 for (i = 0; i < 8; i++) {
757 tmp0_bit[i] = *ecc_data1 % 2;
758 *ecc_data1 = *ecc_data1 / 2;
759 }
760
761 for (i = 0; i < 8; i++) {
762 tmp1_bit[i] = *(ecc_data1 + 1) % 2;
763 *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
764 }
765
766 for (i = 0; i < 8; i++) {
767 tmp2_bit[i] = *(ecc_data1 + 2) % 2;
768 *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
769 }
770
771 for (i = 0; i < 8; i++) {
772 comp0_bit[i] = *ecc_data2 % 2;
773 *ecc_data2 = *ecc_data2 / 2;
774 }
775
776 for (i = 0; i < 8; i++) {
777 comp1_bit[i] = *(ecc_data2 + 1) % 2;
778 *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
779 }
780
781 for (i = 0; i < 8; i++) {
782 comp2_bit[i] = *(ecc_data2 + 2) % 2;
783 *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
784 }
785
786 for (i = 0; i < 6; i++)
787 ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
788
789 for (i = 0; i < 8; i++)
790 ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
791
792 for (i = 0; i < 8; i++)
793 ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
794
795 ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
796 ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
797
798 for (i = 0; i < 24; i++)
799 ecc_sum += ecc_bit[i];
800
801 switch (ecc_sum) {
802 case 0:
803 /* Not reached because this function is not called if
804 * ECC values are equal
805 */
806 return 0;
807
808 case 1:
809 /* Uncorrectable error */
Brian Norris289c0522011-07-19 10:06:09 -0700810 pr_debug("ECC UNCORRECTED_ERROR 1\n");
Vimal Singh67ce04b2009-05-12 13:47:03 -0700811 return -1;
812
813 case 11:
814 /* UN-Correctable error */
Brian Norris289c0522011-07-19 10:06:09 -0700815 pr_debug("ECC UNCORRECTED_ERROR B\n");
Vimal Singh67ce04b2009-05-12 13:47:03 -0700816 return -1;
817
818 case 12:
819 /* Correctable error */
820 find_byte = (ecc_bit[23] << 8) +
821 (ecc_bit[21] << 7) +
822 (ecc_bit[19] << 6) +
823 (ecc_bit[17] << 5) +
824 (ecc_bit[15] << 4) +
825 (ecc_bit[13] << 3) +
826 (ecc_bit[11] << 2) +
827 (ecc_bit[9] << 1) +
828 ecc_bit[7];
829
830 find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
831
Brian Norris0a32a102011-07-19 10:06:10 -0700832 pr_debug("Correcting single bit ECC error at offset: "
833 "%d, bit: %d\n", find_byte, find_bit);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700834
835 page_data[find_byte] ^= (1 << find_bit);
836
John Ogness74f1b722011-02-28 13:12:46 +0100837 return 1;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700838 default:
839 if (isEccFF) {
840 if (ecc_data2[0] == 0 &&
841 ecc_data2[1] == 0 &&
842 ecc_data2[2] == 0)
843 return 0;
844 }
Brian Norris289c0522011-07-19 10:06:09 -0700845 pr_debug("UNCORRECTED_ERROR default\n");
Vimal Singh67ce04b2009-05-12 13:47:03 -0700846 return -1;
847 }
848}
849
850/**
851 * omap_correct_data - Compares the ECC read with HW generated ECC
852 * @mtd: MTD device structure
853 * @dat: page data
854 * @read_ecc: ecc read from nand flash
855 * @calc_ecc: ecc read from HW ECC registers
856 *
857 * Compares the ecc read from nand spare area with ECC registers values
John Ogness74f1b722011-02-28 13:12:46 +0100858 * and if ECC's mismatched, it will call 'omap_compare_ecc' for error
859 * detection and correction. If there are no errors, %0 is returned. If
860 * there were errors and all of the errors were corrected, the number of
861 * corrected errors is returned. If uncorrectable errors exist, %-1 is
862 * returned.
Vimal Singh67ce04b2009-05-12 13:47:03 -0700863 */
864static int omap_correct_data(struct mtd_info *mtd, u_char *dat,
865 u_char *read_ecc, u_char *calc_ecc)
866{
867 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
868 mtd);
869 int blockCnt = 0, i = 0, ret = 0;
John Ogness74f1b722011-02-28 13:12:46 +0100870 int stat = 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700871
872 /* Ex NAND_ECC_HW12_2048 */
873 if ((info->nand.ecc.mode == NAND_ECC_HW) &&
874 (info->nand.ecc.size == 2048))
875 blockCnt = 4;
876 else
877 blockCnt = 1;
878
879 for (i = 0; i < blockCnt; i++) {
880 if (memcmp(read_ecc, calc_ecc, 3) != 0) {
881 ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
882 if (ret < 0)
883 return ret;
John Ogness74f1b722011-02-28 13:12:46 +0100884 /* keep track of the number of corrected errors */
885 stat += ret;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700886 }
887 read_ecc += 3;
888 calc_ecc += 3;
889 dat += 512;
890 }
John Ogness74f1b722011-02-28 13:12:46 +0100891 return stat;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700892}
893
894/**
895 * omap_calcuate_ecc - Generate non-inverted ECC bytes.
896 * @mtd: MTD device structure
897 * @dat: The pointer to data on which ecc is computed
898 * @ecc_code: The ecc_code buffer
899 *
900 * Using noninverted ECC can be considered ugly since writing a blank
901 * page ie. padding will clear the ECC bytes. This is no problem as long
902 * nobody is trying to write data on the seemingly unused page. Reading
903 * an erased page will produce an ECC mismatch between generated and read
904 * ECC bytes that has to be dealt with separately.
905 */
906static int omap_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
907 u_char *ecc_code)
908{
909 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
910 mtd);
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700911 u32 val;
912
913 val = readl(info->reg.gpmc_ecc_config);
914 if (((val >> ECC_CONFIG_CS_SHIFT) & ~CS_MASK) != info->gpmc_cs)
915 return -EINVAL;
916
917 /* read ecc result */
918 val = readl(info->reg.gpmc_ecc1_result);
919 *ecc_code++ = val; /* P128e, ..., P1e */
920 *ecc_code++ = val >> 16; /* P128o, ..., P1o */
921 /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
922 *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
923
924 return 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700925}
926
927/**
928 * omap_enable_hwecc - This function enables the hardware ecc functionality
929 * @mtd: MTD device structure
930 * @mode: Read/Write mode
931 */
932static void omap_enable_hwecc(struct mtd_info *mtd, int mode)
933{
934 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
935 mtd);
936 struct nand_chip *chip = mtd->priv;
937 unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700938 u32 val;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700939
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700940 /* clear ecc and enable bits */
941 val = ECCCLEAR | ECC1;
942 writel(val, info->reg.gpmc_ecc_control);
943
944 /* program ecc and result sizes */
945 val = ((((info->nand.ecc.size >> 1) - 1) << ECCSIZE1_SHIFT) |
946 ECC1RESULTSIZE);
947 writel(val, info->reg.gpmc_ecc_size_config);
948
949 switch (mode) {
950 case NAND_ECC_READ:
951 case NAND_ECC_WRITE:
952 writel(ECCCLEAR | ECC1, info->reg.gpmc_ecc_control);
953 break;
954 case NAND_ECC_READSYN:
955 writel(ECCCLEAR, info->reg.gpmc_ecc_control);
956 break;
957 default:
958 dev_info(&info->pdev->dev,
959 "error: unrecognized Mode[%d]!\n", mode);
960 break;
961 }
962
963 /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
964 val = (dev_width << 7) | (info->gpmc_cs << 1) | (0x1);
965 writel(val, info->reg.gpmc_ecc_config);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700966}
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000967
Vimal Singh67ce04b2009-05-12 13:47:03 -0700968/**
969 * omap_wait - wait until the command is done
970 * @mtd: MTD device structure
971 * @chip: NAND Chip structure
972 *
973 * Wait function is called during Program and erase operations and
974 * the way it is called from MTD layer, we should wait till the NAND
975 * chip is ready after the programming/erase operation has completed.
976 *
977 * Erase can take up to 400ms and program up to 20ms according to
978 * general NAND and SmartMedia specs
979 */
980static int omap_wait(struct mtd_info *mtd, struct nand_chip *chip)
981{
982 struct nand_chip *this = mtd->priv;
983 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
984 mtd);
985 unsigned long timeo = jiffies;
Ivan Djelica9c465f2012-04-17 13:11:53 +0200986 int status, state = this->state;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700987
988 if (state == FL_ERASING)
989 timeo += (HZ * 400) / 1000;
990 else
991 timeo += (HZ * 20) / 1000;
992
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700993 writeb(NAND_CMD_STATUS & 0xFF, info->reg.gpmc_nand_command);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700994 while (time_before(jiffies, timeo)) {
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700995 status = readb(info->reg.gpmc_nand_data);
vimal singhc276aca2009-06-27 11:07:06 +0530996 if (status & NAND_STATUS_READY)
Vimal Singh67ce04b2009-05-12 13:47:03 -0700997 break;
vimal singhc276aca2009-06-27 11:07:06 +0530998 cond_resched();
Vimal Singh67ce04b2009-05-12 13:47:03 -0700999 }
Ivan Djelica9c465f2012-04-17 13:11:53 +02001000
1001 status = gpmc_nand_read(info->gpmc_cs, GPMC_NAND_DATA);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001002 return status;
1003}
1004
1005/**
1006 * omap_dev_ready - calls the platform specific dev_ready function
1007 * @mtd: MTD device structure
1008 */
1009static int omap_dev_ready(struct mtd_info *mtd)
1010{
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +00001011 unsigned int val = 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -07001012 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1013 mtd);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001014
Afzal Mohammed65b97cf2012-08-30 12:53:22 -07001015 val = readl(info->reg.gpmc_status);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001016
Afzal Mohammed65b97cf2012-08-30 12:53:22 -07001017 if ((val & 0x100) == 0x100) {
1018 return 1;
1019 } else {
1020 return 0;
1021 }
Vimal Singh67ce04b2009-05-12 13:47:03 -07001022}
1023
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001024#ifdef CONFIG_MTD_NAND_OMAP_BCH
1025
1026/**
1027 * omap3_enable_hwecc_bch - Program OMAP3 GPMC to perform BCH ECC correction
1028 * @mtd: MTD device structure
1029 * @mode: Read/Write mode
1030 */
1031static void omap3_enable_hwecc_bch(struct mtd_info *mtd, int mode)
1032{
1033 int nerrors;
1034 unsigned int dev_width;
1035 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1036 mtd);
1037 struct nand_chip *chip = mtd->priv;
1038
1039 nerrors = (info->nand.ecc.bytes == 13) ? 8 : 4;
1040 dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
1041 /*
1042 * Program GPMC to perform correction on one 512-byte sector at a time.
1043 * Using 4 sectors at a time (i.e. ecc.size = 2048) is also possible and
1044 * gives a slight (5%) performance gain (but requires additional code).
1045 */
1046 (void)gpmc_enable_hwecc_bch(info->gpmc_cs, mode, dev_width, 1, nerrors);
1047}
1048
1049/**
1050 * omap3_calculate_ecc_bch4 - Generate 7 bytes of ECC bytes
1051 * @mtd: MTD device structure
1052 * @dat: The pointer to data on which ecc is computed
1053 * @ecc_code: The ecc_code buffer
1054 */
1055static int omap3_calculate_ecc_bch4(struct mtd_info *mtd, const u_char *dat,
1056 u_char *ecc_code)
1057{
1058 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1059 mtd);
1060 return gpmc_calculate_ecc_bch4(info->gpmc_cs, dat, ecc_code);
1061}
1062
1063/**
1064 * omap3_calculate_ecc_bch8 - Generate 13 bytes of ECC bytes
1065 * @mtd: MTD device structure
1066 * @dat: The pointer to data on which ecc is computed
1067 * @ecc_code: The ecc_code buffer
1068 */
1069static int omap3_calculate_ecc_bch8(struct mtd_info *mtd, const u_char *dat,
1070 u_char *ecc_code)
1071{
1072 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1073 mtd);
1074 return gpmc_calculate_ecc_bch8(info->gpmc_cs, dat, ecc_code);
1075}
1076
1077/**
1078 * omap3_correct_data_bch - Decode received data and correct errors
1079 * @mtd: MTD device structure
1080 * @data: page data
1081 * @read_ecc: ecc read from nand flash
1082 * @calc_ecc: ecc read from HW ECC registers
1083 */
1084static int omap3_correct_data_bch(struct mtd_info *mtd, u_char *data,
1085 u_char *read_ecc, u_char *calc_ecc)
1086{
1087 int i, count;
1088 /* cannot correct more than 8 errors */
1089 unsigned int errloc[8];
1090 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1091 mtd);
1092
1093 count = decode_bch(info->bch, NULL, 512, read_ecc, calc_ecc, NULL,
1094 errloc);
1095 if (count > 0) {
1096 /* correct errors */
1097 for (i = 0; i < count; i++) {
1098 /* correct data only, not ecc bytes */
1099 if (errloc[i] < 8*512)
1100 data[errloc[i]/8] ^= 1 << (errloc[i] & 7);
1101 pr_debug("corrected bitflip %u\n", errloc[i]);
1102 }
1103 } else if (count < 0) {
1104 pr_err("ecc unrecoverable error\n");
1105 }
1106 return count;
1107}
1108
1109/**
1110 * omap3_free_bch - Release BCH ecc resources
1111 * @mtd: MTD device structure
1112 */
1113static void omap3_free_bch(struct mtd_info *mtd)
1114{
1115 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1116 mtd);
1117 if (info->bch) {
1118 free_bch(info->bch);
1119 info->bch = NULL;
1120 }
1121}
1122
1123/**
1124 * omap3_init_bch - Initialize BCH ECC
1125 * @mtd: MTD device structure
1126 * @ecc_opt: OMAP ECC mode (OMAP_ECC_BCH4_CODE_HW or OMAP_ECC_BCH8_CODE_HW)
1127 */
1128static int omap3_init_bch(struct mtd_info *mtd, int ecc_opt)
1129{
1130 int ret, max_errors;
1131 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1132 mtd);
1133#ifdef CONFIG_MTD_NAND_OMAP_BCH8
1134 const int hw_errors = 8;
1135#else
1136 const int hw_errors = 4;
1137#endif
1138 info->bch = NULL;
1139
1140 max_errors = (ecc_opt == OMAP_ECC_BCH8_CODE_HW) ? 8 : 4;
1141 if (max_errors != hw_errors) {
1142 pr_err("cannot configure %d-bit BCH ecc, only %d-bit supported",
1143 max_errors, hw_errors);
1144 goto fail;
1145 }
1146
1147 /* initialize GPMC BCH engine */
1148 ret = gpmc_init_hwecc_bch(info->gpmc_cs, 1, max_errors);
1149 if (ret)
1150 goto fail;
1151
1152 /* software bch library is only used to detect and locate errors */
1153 info->bch = init_bch(13, max_errors, 0x201b /* hw polynomial */);
1154 if (!info->bch)
1155 goto fail;
1156
1157 info->nand.ecc.size = 512;
1158 info->nand.ecc.hwctl = omap3_enable_hwecc_bch;
1159 info->nand.ecc.correct = omap3_correct_data_bch;
1160 info->nand.ecc.mode = NAND_ECC_HW;
1161
1162 /*
1163 * The number of corrected errors in an ecc block that will trigger
1164 * block scrubbing defaults to the ecc strength (4 or 8).
1165 * Set mtd->bitflip_threshold here to define a custom threshold.
1166 */
1167
1168 if (max_errors == 8) {
1169 info->nand.ecc.strength = 8;
1170 info->nand.ecc.bytes = 13;
1171 info->nand.ecc.calculate = omap3_calculate_ecc_bch8;
1172 } else {
1173 info->nand.ecc.strength = 4;
1174 info->nand.ecc.bytes = 7;
1175 info->nand.ecc.calculate = omap3_calculate_ecc_bch4;
1176 }
1177
1178 pr_info("enabling NAND BCH ecc with %d-bit correction\n", max_errors);
1179 return 0;
1180fail:
1181 omap3_free_bch(mtd);
1182 return -1;
1183}
1184
1185/**
1186 * omap3_init_bch_tail - Build an oob layout for BCH ECC correction.
1187 * @mtd: MTD device structure
1188 */
1189static int omap3_init_bch_tail(struct mtd_info *mtd)
1190{
1191 int i, steps;
1192 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1193 mtd);
1194 struct nand_ecclayout *layout = &info->ecclayout;
1195
1196 /* build oob layout */
1197 steps = mtd->writesize/info->nand.ecc.size;
1198 layout->eccbytes = steps*info->nand.ecc.bytes;
1199
1200 /* do not bother creating special oob layouts for small page devices */
1201 if (mtd->oobsize < 64) {
1202 pr_err("BCH ecc is not supported on small page devices\n");
1203 goto fail;
1204 }
1205
1206 /* reserve 2 bytes for bad block marker */
1207 if (layout->eccbytes+2 > mtd->oobsize) {
1208 pr_err("no oob layout available for oobsize %d eccbytes %u\n",
1209 mtd->oobsize, layout->eccbytes);
1210 goto fail;
1211 }
1212
1213 /* put ecc bytes at oob tail */
1214 for (i = 0; i < layout->eccbytes; i++)
1215 layout->eccpos[i] = mtd->oobsize-layout->eccbytes+i;
1216
1217 layout->oobfree[0].offset = 2;
1218 layout->oobfree[0].length = mtd->oobsize-2-layout->eccbytes;
1219 info->nand.ecc.layout = layout;
1220
1221 if (!(info->nand.options & NAND_BUSWIDTH_16))
1222 info->nand.badblock_pattern = &bb_descrip_flashbased;
1223 return 0;
1224fail:
1225 omap3_free_bch(mtd);
1226 return -1;
1227}
1228
1229#else
1230static int omap3_init_bch(struct mtd_info *mtd, int ecc_opt)
1231{
1232 pr_err("CONFIG_MTD_NAND_OMAP_BCH is not enabled\n");
1233 return -1;
1234}
1235static int omap3_init_bch_tail(struct mtd_info *mtd)
1236{
1237 return -1;
1238}
1239static void omap3_free_bch(struct mtd_info *mtd)
1240{
1241}
1242#endif /* CONFIG_MTD_NAND_OMAP_BCH */
1243
Vimal Singh67ce04b2009-05-12 13:47:03 -07001244static int __devinit omap_nand_probe(struct platform_device *pdev)
1245{
1246 struct omap_nand_info *info;
1247 struct omap_nand_platform_data *pdata;
1248 int err;
Sukumar Ghoraif040d332011-01-28 15:42:09 +05301249 int i, offset;
Russell King763e7352012-04-25 00:16:00 +01001250 dma_cap_mask_t mask;
1251 unsigned sig;
Afzal Mohammed9c4c2f82012-08-30 12:53:23 -07001252 struct resource *res;
Vimal Singh67ce04b2009-05-12 13:47:03 -07001253
1254 pdata = pdev->dev.platform_data;
1255 if (pdata == NULL) {
1256 dev_err(&pdev->dev, "platform data missing\n");
1257 return -ENODEV;
1258 }
1259
1260 info = kzalloc(sizeof(struct omap_nand_info), GFP_KERNEL);
1261 if (!info)
1262 return -ENOMEM;
1263
1264 platform_set_drvdata(pdev, info);
1265
1266 spin_lock_init(&info->controller.lock);
1267 init_waitqueue_head(&info->controller.wq);
1268
1269 info->pdev = pdev;
1270
1271 info->gpmc_cs = pdata->cs;
Afzal Mohammed65b97cf2012-08-30 12:53:22 -07001272 info->reg = pdata->reg;
Vimal Singh67ce04b2009-05-12 13:47:03 -07001273
1274 info->mtd.priv = &info->nand;
1275 info->mtd.name = dev_name(&pdev->dev);
1276 info->mtd.owner = THIS_MODULE;
1277
Sukumar Ghoraid5ce2b62011-01-28 15:42:03 +05301278 info->nand.options = pdata->devsize;
Vimal Singh2f70a1e2010-02-15 10:03:33 -08001279 info->nand.options |= NAND_SKIP_BBTSCAN;
Vimal Singh67ce04b2009-05-12 13:47:03 -07001280
Afzal Mohammed9c4c2f82012-08-30 12:53:23 -07001281 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1282 if (res == NULL) {
1283 err = -EINVAL;
1284 dev_err(&pdev->dev, "error getting memory resource\n");
1285 goto out_free_info;
1286 }
Vimal Singh67ce04b2009-05-12 13:47:03 -07001287
Afzal Mohammed9c4c2f82012-08-30 12:53:23 -07001288 info->phys_base = res->start;
1289 info->mem_size = resource_size(res);
1290
1291 if (!request_mem_region(info->phys_base, info->mem_size,
Vimal Singh67ce04b2009-05-12 13:47:03 -07001292 pdev->dev.driver->name)) {
1293 err = -EBUSY;
Vimal Singh2f70a1e2010-02-15 10:03:33 -08001294 goto out_free_info;
Vimal Singh67ce04b2009-05-12 13:47:03 -07001295 }
1296
Afzal Mohammed9c4c2f82012-08-30 12:53:23 -07001297 info->nand.IO_ADDR_R = ioremap(info->phys_base, info->mem_size);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001298 if (!info->nand.IO_ADDR_R) {
1299 err = -ENOMEM;
1300 goto out_release_mem_region;
1301 }
vimal singh59e9c5a2009-07-13 16:26:24 +05301302
Vimal Singh67ce04b2009-05-12 13:47:03 -07001303 info->nand.controller = &info->controller;
1304
1305 info->nand.IO_ADDR_W = info->nand.IO_ADDR_R;
1306 info->nand.cmd_ctrl = omap_hwcontrol;
1307
Vimal Singh67ce04b2009-05-12 13:47:03 -07001308 /*
1309 * If RDY/BSY line is connected to OMAP then use the omap ready
Peter Meerwald4cacbe22012-07-19 13:21:04 +02001310 * function and the generic nand_wait function which reads the status
1311 * register after monitoring the RDY/BSY line. Otherwise use a standard
Vimal Singh67ce04b2009-05-12 13:47:03 -07001312 * chip delay which is slightly more than tR (AC Timing) of the NAND
1313 * device and read status register until you get a failure or success
1314 */
1315 if (pdata->dev_ready) {
1316 info->nand.dev_ready = omap_dev_ready;
1317 info->nand.chip_delay = 0;
1318 } else {
1319 info->nand.waitfunc = omap_wait;
1320 info->nand.chip_delay = 50;
1321 }
1322
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301323 switch (pdata->xfer_type) {
1324 case NAND_OMAP_PREFETCH_POLLED:
vimal singh59e9c5a2009-07-13 16:26:24 +05301325 info->nand.read_buf = omap_read_buf_pref;
1326 info->nand.write_buf = omap_write_buf_pref;
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301327 break;
vimal singhdfe32892009-07-13 16:29:16 +05301328
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301329 case NAND_OMAP_POLLED:
vimal singh59e9c5a2009-07-13 16:26:24 +05301330 if (info->nand.options & NAND_BUSWIDTH_16) {
1331 info->nand.read_buf = omap_read_buf16;
1332 info->nand.write_buf = omap_write_buf16;
1333 } else {
1334 info->nand.read_buf = omap_read_buf8;
1335 info->nand.write_buf = omap_write_buf8;
1336 }
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301337 break;
1338
1339 case NAND_OMAP_PREFETCH_DMA:
Russell King763e7352012-04-25 00:16:00 +01001340 dma_cap_zero(mask);
1341 dma_cap_set(DMA_SLAVE, mask);
1342 sig = OMAP24XX_DMA_GPMC;
1343 info->dma = dma_request_channel(mask, omap_dma_filter_fn, &sig);
1344 if (!info->dma) {
Russell King2df41d02012-04-25 00:19:39 +01001345 dev_err(&pdev->dev, "DMA engine request failed\n");
1346 err = -ENXIO;
1347 goto out_release_mem_region;
Russell King763e7352012-04-25 00:16:00 +01001348 } else {
1349 struct dma_slave_config cfg;
Russell King763e7352012-04-25 00:16:00 +01001350
1351 memset(&cfg, 0, sizeof(cfg));
1352 cfg.src_addr = info->phys_base;
1353 cfg.dst_addr = info->phys_base;
1354 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1355 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1356 cfg.src_maxburst = 16;
1357 cfg.dst_maxburst = 16;
Arnd Bergmannd680e2c2012-08-04 11:05:25 +00001358 err = dmaengine_slave_config(info->dma, &cfg);
1359 if (err) {
Russell King763e7352012-04-25 00:16:00 +01001360 dev_err(&pdev->dev, "DMA engine slave config failed: %d\n",
Arnd Bergmannd680e2c2012-08-04 11:05:25 +00001361 err);
Russell King763e7352012-04-25 00:16:00 +01001362 goto out_release_mem_region;
1363 }
1364 info->nand.read_buf = omap_read_buf_dma_pref;
1365 info->nand.write_buf = omap_write_buf_dma_pref;
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301366 }
1367 break;
1368
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301369 case NAND_OMAP_PREFETCH_IRQ:
Afzal Mohammed5c468452012-08-30 12:53:24 -07001370 info->gpmc_irq_fifo = platform_get_irq(pdev, 0);
1371 if (info->gpmc_irq_fifo <= 0) {
1372 dev_err(&pdev->dev, "error getting fifo irq\n");
1373 err = -ENODEV;
1374 goto out_release_mem_region;
1375 }
1376 err = request_irq(info->gpmc_irq_fifo, omap_nand_irq,
1377 IRQF_SHARED, "gpmc-nand-fifo", info);
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301378 if (err) {
1379 dev_err(&pdev->dev, "requesting irq(%d) error:%d",
Afzal Mohammed5c468452012-08-30 12:53:24 -07001380 info->gpmc_irq_fifo, err);
1381 info->gpmc_irq_fifo = 0;
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301382 goto out_release_mem_region;
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301383 }
Afzal Mohammed5c468452012-08-30 12:53:24 -07001384
1385 info->gpmc_irq_count = platform_get_irq(pdev, 1);
1386 if (info->gpmc_irq_count <= 0) {
1387 dev_err(&pdev->dev, "error getting count irq\n");
1388 err = -ENODEV;
1389 goto out_release_mem_region;
1390 }
1391 err = request_irq(info->gpmc_irq_count, omap_nand_irq,
1392 IRQF_SHARED, "gpmc-nand-count", info);
1393 if (err) {
1394 dev_err(&pdev->dev, "requesting irq(%d) error:%d",
1395 info->gpmc_irq_count, err);
1396 info->gpmc_irq_count = 0;
1397 goto out_release_mem_region;
1398 }
1399
1400 info->nand.read_buf = omap_read_buf_irq_pref;
1401 info->nand.write_buf = omap_write_buf_irq_pref;
1402
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301403 break;
1404
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301405 default:
1406 dev_err(&pdev->dev,
1407 "xfer_type(%d) not supported!\n", pdata->xfer_type);
1408 err = -EINVAL;
1409 goto out_release_mem_region;
vimal singh59e9c5a2009-07-13 16:26:24 +05301410 }
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301411
Peter Meerwald4cacbe22012-07-19 13:21:04 +02001412 /* select the ecc type */
Sukumar Ghoraif3d73f32011-01-28 15:42:08 +05301413 if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_DEFAULT)
1414 info->nand.ecc.mode = NAND_ECC_SOFT;
Sukumar Ghoraif040d332011-01-28 15:42:09 +05301415 else if ((pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW) ||
1416 (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE)) {
Sukumar Ghoraif3d73f32011-01-28 15:42:08 +05301417 info->nand.ecc.bytes = 3;
1418 info->nand.ecc.size = 512;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001419 info->nand.ecc.strength = 1;
Sukumar Ghoraif3d73f32011-01-28 15:42:08 +05301420 info->nand.ecc.calculate = omap_calculate_ecc;
1421 info->nand.ecc.hwctl = omap_enable_hwecc;
1422 info->nand.ecc.correct = omap_correct_data;
1423 info->nand.ecc.mode = NAND_ECC_HW;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001424 } else if ((pdata->ecc_opt == OMAP_ECC_BCH4_CODE_HW) ||
1425 (pdata->ecc_opt == OMAP_ECC_BCH8_CODE_HW)) {
1426 err = omap3_init_bch(&info->mtd, pdata->ecc_opt);
1427 if (err) {
1428 err = -EINVAL;
1429 goto out_release_mem_region;
1430 }
Sukumar Ghoraif3d73f32011-01-28 15:42:08 +05301431 }
Vimal Singh67ce04b2009-05-12 13:47:03 -07001432
1433 /* DIP switches on some boards change between 8 and 16 bit
1434 * bus widths for flash. Try the other width if the first try fails.
1435 */
Jan Weitzela80f1c12011-04-19 16:15:34 +02001436 if (nand_scan_ident(&info->mtd, 1, NULL)) {
Vimal Singh67ce04b2009-05-12 13:47:03 -07001437 info->nand.options ^= NAND_BUSWIDTH_16;
Jan Weitzela80f1c12011-04-19 16:15:34 +02001438 if (nand_scan_ident(&info->mtd, 1, NULL)) {
Vimal Singh67ce04b2009-05-12 13:47:03 -07001439 err = -ENXIO;
1440 goto out_release_mem_region;
1441 }
1442 }
1443
Sukumar Ghoraif040d332011-01-28 15:42:09 +05301444 /* rom code layout */
1445 if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE) {
1446
1447 if (info->nand.options & NAND_BUSWIDTH_16)
1448 offset = 2;
1449 else {
1450 offset = 1;
1451 info->nand.badblock_pattern = &bb_descrip_flashbased;
1452 }
1453 omap_oobinfo.eccbytes = 3 * (info->mtd.oobsize/16);
1454 for (i = 0; i < omap_oobinfo.eccbytes; i++)
1455 omap_oobinfo.eccpos[i] = i+offset;
1456
1457 omap_oobinfo.oobfree->offset = offset + omap_oobinfo.eccbytes;
1458 omap_oobinfo.oobfree->length = info->mtd.oobsize -
1459 (offset + omap_oobinfo.eccbytes);
1460
1461 info->nand.ecc.layout = &omap_oobinfo;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001462 } else if ((pdata->ecc_opt == OMAP_ECC_BCH4_CODE_HW) ||
1463 (pdata->ecc_opt == OMAP_ECC_BCH8_CODE_HW)) {
1464 /* build OOB layout for BCH ECC correction */
1465 err = omap3_init_bch_tail(&info->mtd);
1466 if (err) {
1467 err = -EINVAL;
1468 goto out_release_mem_region;
1469 }
Sukumar Ghoraif040d332011-01-28 15:42:09 +05301470 }
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301471
Jan Weitzela80f1c12011-04-19 16:15:34 +02001472 /* second phase scan */
1473 if (nand_scan_tail(&info->mtd)) {
1474 err = -ENXIO;
1475 goto out_release_mem_region;
1476 }
1477
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001478 mtd_device_parse_register(&info->mtd, NULL, NULL, pdata->parts,
1479 pdata->nr_parts);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001480
1481 platform_set_drvdata(pdev, &info->mtd);
1482
1483 return 0;
1484
1485out_release_mem_region:
Russell King763e7352012-04-25 00:16:00 +01001486 if (info->dma)
1487 dma_release_channel(info->dma);
Afzal Mohammed5c468452012-08-30 12:53:24 -07001488 if (info->gpmc_irq_count > 0)
1489 free_irq(info->gpmc_irq_count, info);
1490 if (info->gpmc_irq_fifo > 0)
1491 free_irq(info->gpmc_irq_fifo, info);
Afzal Mohammed9c4c2f82012-08-30 12:53:23 -07001492 release_mem_region(info->phys_base, info->mem_size);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001493out_free_info:
1494 kfree(info);
1495
1496 return err;
1497}
1498
1499static int omap_nand_remove(struct platform_device *pdev)
1500{
1501 struct mtd_info *mtd = platform_get_drvdata(pdev);
Vimal Singhf35b6ed2010-01-05 16:01:08 +05301502 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1503 mtd);
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001504 omap3_free_bch(&info->mtd);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001505
1506 platform_set_drvdata(pdev, NULL);
Russell King763e7352012-04-25 00:16:00 +01001507 if (info->dma)
1508 dma_release_channel(info->dma);
1509
Afzal Mohammed5c468452012-08-30 12:53:24 -07001510 if (info->gpmc_irq_count > 0)
1511 free_irq(info->gpmc_irq_count, info);
1512 if (info->gpmc_irq_fifo > 0)
1513 free_irq(info->gpmc_irq_fifo, info);
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301514
Vimal Singh67ce04b2009-05-12 13:47:03 -07001515 /* Release NAND device, its internal structures and partitions */
1516 nand_release(&info->mtd);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +00001517 iounmap(info->nand.IO_ADDR_R);
Andreas Bießmann4d3d6882012-08-31 13:35:42 +02001518 release_mem_region(info->phys_base, NAND_IO_SIZE);
Andreas Bießmann7d9b1102012-08-31 13:35:41 +02001519 kfree(info);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001520 return 0;
1521}
1522
1523static struct platform_driver omap_nand_driver = {
1524 .probe = omap_nand_probe,
1525 .remove = omap_nand_remove,
1526 .driver = {
1527 .name = DRIVER_NAME,
1528 .owner = THIS_MODULE,
1529 },
1530};
1531
Axel Linf99640d2011-11-27 20:45:03 +08001532module_platform_driver(omap_nand_driver);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001533
Axel Linc804c732011-03-07 11:04:24 +08001534MODULE_ALIAS("platform:" DRIVER_NAME);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001535MODULE_LICENSE("GPL");
1536MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");