blob: ec20f67e8949927a57bd9537b35ce14140b1eda7 [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
Arnd Bergmann22037472012-08-24 15:21:06 +020030#include <linux/platform_data/mtd-nand-omap2.h>
Vimal Singh67ce04b2009-05-12 13:47:03 -070031
Vimal Singh67ce04b2009-05-12 13:47:03 -070032#define DRIVER_NAME "omap2-nand"
Sukumar Ghorai4e070372011-01-28 15:42:06 +053033#define OMAP_NAND_TIMEOUT_MS 5000
Vimal Singh67ce04b2009-05-12 13:47:03 -070034
Vimal Singh67ce04b2009-05-12 13:47:03 -070035#define NAND_Ecc_P1e (1 << 0)
36#define NAND_Ecc_P2e (1 << 1)
37#define NAND_Ecc_P4e (1 << 2)
38#define NAND_Ecc_P8e (1 << 3)
39#define NAND_Ecc_P16e (1 << 4)
40#define NAND_Ecc_P32e (1 << 5)
41#define NAND_Ecc_P64e (1 << 6)
42#define NAND_Ecc_P128e (1 << 7)
43#define NAND_Ecc_P256e (1 << 8)
44#define NAND_Ecc_P512e (1 << 9)
45#define NAND_Ecc_P1024e (1 << 10)
46#define NAND_Ecc_P2048e (1 << 11)
47
48#define NAND_Ecc_P1o (1 << 16)
49#define NAND_Ecc_P2o (1 << 17)
50#define NAND_Ecc_P4o (1 << 18)
51#define NAND_Ecc_P8o (1 << 19)
52#define NAND_Ecc_P16o (1 << 20)
53#define NAND_Ecc_P32o (1 << 21)
54#define NAND_Ecc_P64o (1 << 22)
55#define NAND_Ecc_P128o (1 << 23)
56#define NAND_Ecc_P256o (1 << 24)
57#define NAND_Ecc_P512o (1 << 25)
58#define NAND_Ecc_P1024o (1 << 26)
59#define NAND_Ecc_P2048o (1 << 27)
60
61#define TF(value) (value ? 1 : 0)
62
63#define P2048e(a) (TF(a & NAND_Ecc_P2048e) << 0)
64#define P2048o(a) (TF(a & NAND_Ecc_P2048o) << 1)
65#define P1e(a) (TF(a & NAND_Ecc_P1e) << 2)
66#define P1o(a) (TF(a & NAND_Ecc_P1o) << 3)
67#define P2e(a) (TF(a & NAND_Ecc_P2e) << 4)
68#define P2o(a) (TF(a & NAND_Ecc_P2o) << 5)
69#define P4e(a) (TF(a & NAND_Ecc_P4e) << 6)
70#define P4o(a) (TF(a & NAND_Ecc_P4o) << 7)
71
72#define P8e(a) (TF(a & NAND_Ecc_P8e) << 0)
73#define P8o(a) (TF(a & NAND_Ecc_P8o) << 1)
74#define P16e(a) (TF(a & NAND_Ecc_P16e) << 2)
75#define P16o(a) (TF(a & NAND_Ecc_P16o) << 3)
76#define P32e(a) (TF(a & NAND_Ecc_P32e) << 4)
77#define P32o(a) (TF(a & NAND_Ecc_P32o) << 5)
78#define P64e(a) (TF(a & NAND_Ecc_P64e) << 6)
79#define P64o(a) (TF(a & NAND_Ecc_P64o) << 7)
80
81#define P128e(a) (TF(a & NAND_Ecc_P128e) << 0)
82#define P128o(a) (TF(a & NAND_Ecc_P128o) << 1)
83#define P256e(a) (TF(a & NAND_Ecc_P256e) << 2)
84#define P256o(a) (TF(a & NAND_Ecc_P256o) << 3)
85#define P512e(a) (TF(a & NAND_Ecc_P512e) << 4)
86#define P512o(a) (TF(a & NAND_Ecc_P512o) << 5)
87#define P1024e(a) (TF(a & NAND_Ecc_P1024e) << 6)
88#define P1024o(a) (TF(a & NAND_Ecc_P1024o) << 7)
89
90#define P8e_s(a) (TF(a & NAND_Ecc_P8e) << 0)
91#define P8o_s(a) (TF(a & NAND_Ecc_P8o) << 1)
92#define P16e_s(a) (TF(a & NAND_Ecc_P16e) << 2)
93#define P16o_s(a) (TF(a & NAND_Ecc_P16o) << 3)
94#define P1e_s(a) (TF(a & NAND_Ecc_P1e) << 4)
95#define P1o_s(a) (TF(a & NAND_Ecc_P1o) << 5)
96#define P2e_s(a) (TF(a & NAND_Ecc_P2e) << 6)
97#define P2o_s(a) (TF(a & NAND_Ecc_P2o) << 7)
98
99#define P4e_s(a) (TF(a & NAND_Ecc_P4e) << 0)
100#define P4o_s(a) (TF(a & NAND_Ecc_P4o) << 1)
101
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700102#define PREFETCH_CONFIG1_CS_SHIFT 24
103#define ECC_CONFIG_CS_SHIFT 1
104#define CS_MASK 0x7
105#define ENABLE_PREFETCH (0x1 << 7)
106#define DMA_MPU_MODE_SHIFT 2
Afzal Mohammed2ef9f3d2012-10-04 19:03:06 +0530107#define ECCSIZE0_SHIFT 12
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700108#define ECCSIZE1_SHIFT 22
109#define ECC1RESULTSIZE 0x1
110#define ECCCLEAR 0x100
111#define ECC1 0x1
Afzal Mohammed47f88af42012-09-29 18:20:11 +0530112#define PREFETCH_FIFOTHRESHOLD_MAX 0x40
113#define PREFETCH_FIFOTHRESHOLD(val) ((val) << 8)
114#define PREFETCH_STATUS_COUNT(val) (val & 0x00003fff)
115#define PREFETCH_STATUS_FIFO_CNT(val) ((val >> 24) & 0x7F)
116#define STATUS_BUFF_EMPTY 0x00000001
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700117
Lokesh Vutlad5e7c862012-10-15 14:03:51 -0700118#define OMAP24XX_DMA_GPMC 4
119
Philip Avinashc3e4b992013-01-04 13:26:49 +0530120#define BCH8_MAX_ERROR 8 /* upto 8 bit correctable */
121#define BCH4_MAX_ERROR 4 /* upto 4 bit correctable */
122
Sukumar Ghoraif040d332011-01-28 15:42:09 +0530123/* oob info generated runtime depending on ecc algorithm and layout selected */
124static struct nand_ecclayout omap_oobinfo;
125/* Define some generic bad / good block scan pattern which are used
126 * while scanning a device for factory marked good / bad blocks
127 */
128static uint8_t scan_ff_pattern[] = { 0xff };
129static struct nand_bbt_descr bb_descrip_flashbased = {
130 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
131 .offs = 0,
132 .len = 1,
133 .pattern = scan_ff_pattern,
134};
vimal singh59e9c5a2009-07-13 16:26:24 +0530135
vimal singh59e9c5a2009-07-13 16:26:24 +0530136
Vimal Singh67ce04b2009-05-12 13:47:03 -0700137struct omap_nand_info {
138 struct nand_hw_control controller;
139 struct omap_nand_platform_data *pdata;
140 struct mtd_info mtd;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700141 struct nand_chip nand;
142 struct platform_device *pdev;
143
144 int gpmc_cs;
145 unsigned long phys_base;
Afzal Mohammed9c4c2f82012-08-30 12:53:23 -0700146 unsigned long mem_size;
vimal singhdfe32892009-07-13 16:29:16 +0530147 struct completion comp;
Russell King763e7352012-04-25 00:16:00 +0100148 struct dma_chan *dma;
Afzal Mohammed5c468452012-08-30 12:53:24 -0700149 int gpmc_irq_fifo;
150 int gpmc_irq_count;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530151 enum {
152 OMAP_NAND_IO_READ = 0, /* read */
153 OMAP_NAND_IO_WRITE, /* write */
154 } iomode;
155 u_char *buf;
156 int buf_len;
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700157 struct gpmc_nand_regs reg;
Ivan Djelic0e618ef2012-04-30 12:17:18 +0200158
159#ifdef CONFIG_MTD_NAND_OMAP_BCH
160 struct bch_control *bch;
161 struct nand_ecclayout ecclayout;
162#endif
Vimal Singh67ce04b2009-05-12 13:47:03 -0700163};
164
165/**
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700166 * omap_prefetch_enable - configures and starts prefetch transfer
167 * @cs: cs (chip select) number
168 * @fifo_th: fifo threshold to be used for read/ write
169 * @dma_mode: dma mode enable (1) or disable (0)
170 * @u32_count: number of bytes to be transferred
171 * @is_write: prefetch read(0) or write post(1) mode
172 */
173static int omap_prefetch_enable(int cs, int fifo_th, int dma_mode,
174 unsigned int u32_count, int is_write, struct omap_nand_info *info)
175{
176 u32 val;
177
178 if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX)
179 return -1;
180
181 if (readl(info->reg.gpmc_prefetch_control))
182 return -EBUSY;
183
184 /* Set the amount of bytes to be prefetched */
185 writel(u32_count, info->reg.gpmc_prefetch_config2);
186
187 /* Set dma/mpu mode, the prefetch read / post write and
188 * enable the engine. Set which cs is has requested for.
189 */
190 val = ((cs << PREFETCH_CONFIG1_CS_SHIFT) |
191 PREFETCH_FIFOTHRESHOLD(fifo_th) | ENABLE_PREFETCH |
192 (dma_mode << DMA_MPU_MODE_SHIFT) | (0x1 & is_write));
193 writel(val, info->reg.gpmc_prefetch_config1);
194
195 /* Start the prefetch engine */
196 writel(0x1, info->reg.gpmc_prefetch_control);
197
198 return 0;
199}
200
201/**
202 * omap_prefetch_reset - disables and stops the prefetch engine
203 */
204static int omap_prefetch_reset(int cs, struct omap_nand_info *info)
205{
206 u32 config1;
207
208 /* check if the same module/cs is trying to reset */
209 config1 = readl(info->reg.gpmc_prefetch_config1);
210 if (((config1 >> PREFETCH_CONFIG1_CS_SHIFT) & CS_MASK) != cs)
211 return -EINVAL;
212
213 /* Stop the PFPW engine */
214 writel(0x0, info->reg.gpmc_prefetch_control);
215
216 /* Reset/disable the PFPW engine */
217 writel(0x0, info->reg.gpmc_prefetch_config1);
218
219 return 0;
220}
221
222/**
Vimal Singh67ce04b2009-05-12 13:47:03 -0700223 * omap_hwcontrol - hardware specific access to control-lines
224 * @mtd: MTD device structure
225 * @cmd: command to device
226 * @ctrl:
227 * NAND_NCE: bit 0 -> don't care
228 * NAND_CLE: bit 1 -> Command Latch
229 * NAND_ALE: bit 2 -> Address Latch
230 *
231 * NOTE: boards may use different bits for these!!
232 */
233static void omap_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
234{
235 struct omap_nand_info *info = container_of(mtd,
236 struct omap_nand_info, mtd);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700237
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000238 if (cmd != NAND_CMD_NONE) {
239 if (ctrl & NAND_CLE)
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700240 writeb(cmd, info->reg.gpmc_nand_command);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700241
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000242 else if (ctrl & NAND_ALE)
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700243 writeb(cmd, info->reg.gpmc_nand_address);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000244
245 else /* NAND_NCE */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700246 writeb(cmd, info->reg.gpmc_nand_data);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700247 }
Vimal Singh67ce04b2009-05-12 13:47:03 -0700248}
249
250/**
vimal singh59e9c5a2009-07-13 16:26:24 +0530251 * omap_read_buf8 - read data from NAND controller into buffer
252 * @mtd: MTD device structure
253 * @buf: buffer to store date
254 * @len: number of bytes to read
255 */
256static void omap_read_buf8(struct mtd_info *mtd, u_char *buf, int len)
257{
258 struct nand_chip *nand = mtd->priv;
259
260 ioread8_rep(nand->IO_ADDR_R, buf, len);
261}
262
263/**
264 * omap_write_buf8 - write buffer to NAND controller
265 * @mtd: MTD device structure
266 * @buf: data buffer
267 * @len: number of bytes to write
268 */
269static void omap_write_buf8(struct mtd_info *mtd, const u_char *buf, int len)
270{
271 struct omap_nand_info *info = container_of(mtd,
272 struct omap_nand_info, mtd);
273 u_char *p = (u_char *)buf;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000274 u32 status = 0;
vimal singh59e9c5a2009-07-13 16:26:24 +0530275
276 while (len--) {
277 iowrite8(*p++, info->nand.IO_ADDR_W);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000278 /* wait until buffer is available for write */
279 do {
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700280 status = readl(info->reg.gpmc_status) &
Afzal Mohammed47f88af42012-09-29 18:20:11 +0530281 STATUS_BUFF_EMPTY;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000282 } while (!status);
vimal singh59e9c5a2009-07-13 16:26:24 +0530283 }
284}
285
286/**
Vimal Singh67ce04b2009-05-12 13:47:03 -0700287 * omap_read_buf16 - read data from NAND controller into buffer
288 * @mtd: MTD device structure
289 * @buf: buffer to store date
290 * @len: number of bytes to read
291 */
292static void omap_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
293{
294 struct nand_chip *nand = mtd->priv;
295
vimal singh59e9c5a2009-07-13 16:26:24 +0530296 ioread16_rep(nand->IO_ADDR_R, buf, len / 2);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700297}
298
299/**
300 * omap_write_buf16 - write buffer to NAND controller
301 * @mtd: MTD device structure
302 * @buf: data buffer
303 * @len: number of bytes to write
304 */
305static void omap_write_buf16(struct mtd_info *mtd, const u_char * buf, int len)
306{
307 struct omap_nand_info *info = container_of(mtd,
308 struct omap_nand_info, mtd);
309 u16 *p = (u16 *) buf;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000310 u32 status = 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700311 /* FIXME try bursts of writesw() or DMA ... */
312 len >>= 1;
313
314 while (len--) {
vimal singh59e9c5a2009-07-13 16:26:24 +0530315 iowrite16(*p++, info->nand.IO_ADDR_W);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000316 /* wait until buffer is available for write */
317 do {
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700318 status = readl(info->reg.gpmc_status) &
Afzal Mohammed47f88af42012-09-29 18:20:11 +0530319 STATUS_BUFF_EMPTY;
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000320 } while (!status);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700321 }
322}
vimal singh59e9c5a2009-07-13 16:26:24 +0530323
324/**
325 * omap_read_buf_pref - read data from NAND controller into buffer
326 * @mtd: MTD device structure
327 * @buf: buffer to store date
328 * @len: number of bytes to read
329 */
330static void omap_read_buf_pref(struct mtd_info *mtd, u_char *buf, int len)
331{
332 struct omap_nand_info *info = container_of(mtd,
333 struct omap_nand_info, mtd);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000334 uint32_t r_count = 0;
vimal singh59e9c5a2009-07-13 16:26:24 +0530335 int ret = 0;
336 u32 *p = (u32 *)buf;
337
338 /* take care of subpage reads */
Vimal Singhc3341d02010-01-07 12:16:26 +0530339 if (len % 4) {
340 if (info->nand.options & NAND_BUSWIDTH_16)
341 omap_read_buf16(mtd, buf, len % 4);
342 else
343 omap_read_buf8(mtd, buf, len % 4);
344 p = (u32 *) (buf + len % 4);
345 len -= len % 4;
vimal singh59e9c5a2009-07-13 16:26:24 +0530346 }
vimal singh59e9c5a2009-07-13 16:26:24 +0530347
348 /* configure and start prefetch transfer */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700349 ret = omap_prefetch_enable(info->gpmc_cs,
350 PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x0, info);
vimal singh59e9c5a2009-07-13 16:26:24 +0530351 if (ret) {
352 /* PFPW engine is busy, use cpu copy method */
353 if (info->nand.options & NAND_BUSWIDTH_16)
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530354 omap_read_buf16(mtd, (u_char *)p, len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530355 else
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530356 omap_read_buf8(mtd, (u_char *)p, len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530357 } else {
358 do {
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700359 r_count = readl(info->reg.gpmc_prefetch_status);
Afzal Mohammed47f88af42012-09-29 18:20:11 +0530360 r_count = PREFETCH_STATUS_FIFO_CNT(r_count);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000361 r_count = r_count >> 2;
362 ioread32_rep(info->nand.IO_ADDR_R, p, r_count);
vimal singh59e9c5a2009-07-13 16:26:24 +0530363 p += r_count;
364 len -= r_count << 2;
365 } while (len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530366 /* disable and stop the PFPW engine */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700367 omap_prefetch_reset(info->gpmc_cs, info);
vimal singh59e9c5a2009-07-13 16:26:24 +0530368 }
369}
370
371/**
372 * omap_write_buf_pref - write buffer to NAND controller
373 * @mtd: MTD device structure
374 * @buf: data buffer
375 * @len: number of bytes to write
376 */
377static void omap_write_buf_pref(struct mtd_info *mtd,
378 const u_char *buf, int len)
379{
380 struct omap_nand_info *info = container_of(mtd,
381 struct omap_nand_info, mtd);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530382 uint32_t w_count = 0;
vimal singh59e9c5a2009-07-13 16:26:24 +0530383 int i = 0, ret = 0;
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530384 u16 *p = (u16 *)buf;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530385 unsigned long tim, limit;
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700386 u32 val;
vimal singh59e9c5a2009-07-13 16:26:24 +0530387
388 /* take care of subpage writes */
389 if (len % 2 != 0) {
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000390 writeb(*buf, info->nand.IO_ADDR_W);
vimal singh59e9c5a2009-07-13 16:26:24 +0530391 p = (u16 *)(buf + 1);
392 len--;
393 }
394
395 /* configure and start prefetch transfer */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700396 ret = omap_prefetch_enable(info->gpmc_cs,
397 PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x1, info);
vimal singh59e9c5a2009-07-13 16:26:24 +0530398 if (ret) {
399 /* PFPW engine is busy, use cpu copy method */
400 if (info->nand.options & NAND_BUSWIDTH_16)
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530401 omap_write_buf16(mtd, (u_char *)p, len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530402 else
Kishore Kadiyalac5d8c0c2011-05-11 21:17:27 +0530403 omap_write_buf8(mtd, (u_char *)p, len);
vimal singh59e9c5a2009-07-13 16:26:24 +0530404 } else {
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000405 while (len) {
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700406 w_count = readl(info->reg.gpmc_prefetch_status);
Afzal Mohammed47f88af42012-09-29 18:20:11 +0530407 w_count = PREFETCH_STATUS_FIFO_CNT(w_count);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000408 w_count = w_count >> 1;
vimal singh59e9c5a2009-07-13 16:26:24 +0530409 for (i = 0; (i < w_count) && len; i++, len -= 2)
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000410 iowrite16(*p++, info->nand.IO_ADDR_W);
vimal singh59e9c5a2009-07-13 16:26:24 +0530411 }
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000412 /* wait for data to flushed-out before reset the prefetch */
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530413 tim = 0;
414 limit = (loops_per_jiffy *
415 msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700416 do {
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530417 cpu_relax();
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700418 val = readl(info->reg.gpmc_prefetch_status);
Afzal Mohammed47f88af42012-09-29 18:20:11 +0530419 val = PREFETCH_STATUS_COUNT(val);
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700420 } while (val && (tim++ < limit));
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530421
vimal singh59e9c5a2009-07-13 16:26:24 +0530422 /* disable and stop the PFPW engine */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700423 omap_prefetch_reset(info->gpmc_cs, info);
vimal singh59e9c5a2009-07-13 16:26:24 +0530424 }
425}
426
vimal singhdfe32892009-07-13 16:29:16 +0530427/*
Russell King2df41d02012-04-25 00:19:39 +0100428 * omap_nand_dma_callback: callback on the completion of dma transfer
vimal singhdfe32892009-07-13 16:29:16 +0530429 * @data: pointer to completion data structure
430 */
Russell King763e7352012-04-25 00:16:00 +0100431static void omap_nand_dma_callback(void *data)
432{
433 complete((struct completion *) data);
434}
vimal singhdfe32892009-07-13 16:29:16 +0530435
436/*
Peter Meerwald4cacbe22012-07-19 13:21:04 +0200437 * omap_nand_dma_transfer: configure and start dma transfer
vimal singhdfe32892009-07-13 16:29:16 +0530438 * @mtd: MTD device structure
439 * @addr: virtual address in RAM of source/destination
440 * @len: number of data bytes to be transferred
441 * @is_write: flag for read/write operation
442 */
443static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
444 unsigned int len, int is_write)
445{
446 struct omap_nand_info *info = container_of(mtd,
447 struct omap_nand_info, mtd);
Russell King2df41d02012-04-25 00:19:39 +0100448 struct dma_async_tx_descriptor *tx;
vimal singhdfe32892009-07-13 16:29:16 +0530449 enum dma_data_direction dir = is_write ? DMA_TO_DEVICE :
450 DMA_FROM_DEVICE;
Russell King2df41d02012-04-25 00:19:39 +0100451 struct scatterlist sg;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530452 unsigned long tim, limit;
Russell King2df41d02012-04-25 00:19:39 +0100453 unsigned n;
454 int ret;
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700455 u32 val;
vimal singhdfe32892009-07-13 16:29:16 +0530456
457 if (addr >= high_memory) {
458 struct page *p1;
459
460 if (((size_t)addr & PAGE_MASK) !=
461 ((size_t)(addr + len - 1) & PAGE_MASK))
462 goto out_copy;
463 p1 = vmalloc_to_page(addr);
464 if (!p1)
465 goto out_copy;
466 addr = page_address(p1) + ((size_t)addr & ~PAGE_MASK);
467 }
468
Russell King2df41d02012-04-25 00:19:39 +0100469 sg_init_one(&sg, addr, len);
470 n = dma_map_sg(info->dma->device->dev, &sg, 1, dir);
471 if (n == 0) {
vimal singhdfe32892009-07-13 16:29:16 +0530472 dev_err(&info->pdev->dev,
473 "Couldn't DMA map a %d byte buffer\n", len);
474 goto out_copy;
475 }
476
Russell King2df41d02012-04-25 00:19:39 +0100477 tx = dmaengine_prep_slave_sg(info->dma, &sg, n,
478 is_write ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
479 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
480 if (!tx)
481 goto out_copy_unmap;
482
483 tx->callback = omap_nand_dma_callback;
484 tx->callback_param = &info->comp;
485 dmaengine_submit(tx);
486
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700487 /* configure and start prefetch transfer */
488 ret = omap_prefetch_enable(info->gpmc_cs,
489 PREFETCH_FIFOTHRESHOLD_MAX, 0x1, len, is_write, info);
vimal singhdfe32892009-07-13 16:29:16 +0530490 if (ret)
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530491 /* PFPW engine is busy, use cpu copy method */
Grazvydas Ignotasd7efe222012-04-11 04:04:34 +0300492 goto out_copy_unmap;
vimal singhdfe32892009-07-13 16:29:16 +0530493
494 init_completion(&info->comp);
Russell King2df41d02012-04-25 00:19:39 +0100495 dma_async_issue_pending(info->dma);
vimal singhdfe32892009-07-13 16:29:16 +0530496
497 /* setup and start DMA using dma_addr */
498 wait_for_completion(&info->comp);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530499 tim = 0;
500 limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700501
502 do {
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530503 cpu_relax();
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700504 val = readl(info->reg.gpmc_prefetch_status);
Afzal Mohammed47f88af42012-09-29 18:20:11 +0530505 val = PREFETCH_STATUS_COUNT(val);
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700506 } while (val && (tim++ < limit));
vimal singhdfe32892009-07-13 16:29:16 +0530507
vimal singhdfe32892009-07-13 16:29:16 +0530508 /* disable and stop the PFPW engine */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700509 omap_prefetch_reset(info->gpmc_cs, info);
vimal singhdfe32892009-07-13 16:29:16 +0530510
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 +0530512 return 0;
513
Grazvydas Ignotasd7efe222012-04-11 04:04:34 +0300514out_copy_unmap:
Russell King2df41d02012-04-25 00:19:39 +0100515 dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
vimal singhdfe32892009-07-13 16:29:16 +0530516out_copy:
517 if (info->nand.options & NAND_BUSWIDTH_16)
518 is_write == 0 ? omap_read_buf16(mtd, (u_char *) addr, len)
519 : omap_write_buf16(mtd, (u_char *) addr, len);
520 else
521 is_write == 0 ? omap_read_buf8(mtd, (u_char *) addr, len)
522 : omap_write_buf8(mtd, (u_char *) addr, len);
523 return 0;
524}
vimal singhdfe32892009-07-13 16:29:16 +0530525
526/**
527 * omap_read_buf_dma_pref - read data from NAND controller into buffer
528 * @mtd: MTD device structure
529 * @buf: buffer to store date
530 * @len: number of bytes to read
531 */
532static void omap_read_buf_dma_pref(struct mtd_info *mtd, u_char *buf, int len)
533{
534 if (len <= mtd->oobsize)
535 omap_read_buf_pref(mtd, buf, len);
536 else
537 /* start transfer in DMA mode */
538 omap_nand_dma_transfer(mtd, buf, len, 0x0);
539}
540
541/**
542 * omap_write_buf_dma_pref - write buffer to NAND controller
543 * @mtd: MTD device structure
544 * @buf: data buffer
545 * @len: number of bytes to write
546 */
547static void omap_write_buf_dma_pref(struct mtd_info *mtd,
548 const u_char *buf, int len)
549{
550 if (len <= mtd->oobsize)
551 omap_write_buf_pref(mtd, buf, len);
552 else
553 /* start transfer in DMA mode */
Vimal Singhbdaefc42010-01-05 12:49:24 +0530554 omap_nand_dma_transfer(mtd, (u_char *) buf, len, 0x1);
vimal singhdfe32892009-07-13 16:29:16 +0530555}
556
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530557/*
Peter Meerwald4cacbe22012-07-19 13:21:04 +0200558 * omap_nand_irq - GPMC irq handler
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530559 * @this_irq: gpmc irq number
560 * @dev: omap_nand_info structure pointer is passed here
561 */
562static irqreturn_t omap_nand_irq(int this_irq, void *dev)
563{
564 struct omap_nand_info *info = (struct omap_nand_info *) dev;
565 u32 bytes;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530566
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700567 bytes = readl(info->reg.gpmc_prefetch_status);
Afzal Mohammed47f88af42012-09-29 18:20:11 +0530568 bytes = PREFETCH_STATUS_FIFO_CNT(bytes);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530569 bytes = bytes & 0xFFFC; /* io in multiple of 4 bytes */
570 if (info->iomode == OMAP_NAND_IO_WRITE) { /* checks for write io */
Afzal Mohammed5c468452012-08-30 12:53:24 -0700571 if (this_irq == info->gpmc_irq_count)
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530572 goto done;
573
574 if (info->buf_len && (info->buf_len < bytes))
575 bytes = info->buf_len;
576 else if (!info->buf_len)
577 bytes = 0;
578 iowrite32_rep(info->nand.IO_ADDR_W,
579 (u32 *)info->buf, bytes >> 2);
580 info->buf = info->buf + bytes;
581 info->buf_len -= bytes;
582
583 } else {
584 ioread32_rep(info->nand.IO_ADDR_R,
585 (u32 *)info->buf, bytes >> 2);
586 info->buf = info->buf + bytes;
587
Afzal Mohammed5c468452012-08-30 12:53:24 -0700588 if (this_irq == info->gpmc_irq_count)
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530589 goto done;
590 }
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530591
592 return IRQ_HANDLED;
593
594done:
595 complete(&info->comp);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530596
Afzal Mohammed5c468452012-08-30 12:53:24 -0700597 disable_irq_nosync(info->gpmc_irq_fifo);
598 disable_irq_nosync(info->gpmc_irq_count);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530599
600 return IRQ_HANDLED;
601}
602
603/*
604 * omap_read_buf_irq_pref - read data from NAND controller into buffer
605 * @mtd: MTD device structure
606 * @buf: buffer to store date
607 * @len: number of bytes to read
608 */
609static void omap_read_buf_irq_pref(struct mtd_info *mtd, u_char *buf, int len)
610{
611 struct omap_nand_info *info = container_of(mtd,
612 struct omap_nand_info, mtd);
613 int ret = 0;
614
615 if (len <= mtd->oobsize) {
616 omap_read_buf_pref(mtd, buf, len);
617 return;
618 }
619
620 info->iomode = OMAP_NAND_IO_READ;
621 info->buf = buf;
622 init_completion(&info->comp);
623
624 /* configure and start prefetch transfer */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700625 ret = omap_prefetch_enable(info->gpmc_cs,
626 PREFETCH_FIFOTHRESHOLD_MAX/2, 0x0, len, 0x0, info);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530627 if (ret)
628 /* PFPW engine is busy, use cpu copy method */
629 goto out_copy;
630
631 info->buf_len = len;
Afzal Mohammed5c468452012-08-30 12:53:24 -0700632
633 enable_irq(info->gpmc_irq_count);
634 enable_irq(info->gpmc_irq_fifo);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530635
636 /* waiting for read to complete */
637 wait_for_completion(&info->comp);
638
639 /* disable and stop the PFPW engine */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700640 omap_prefetch_reset(info->gpmc_cs, info);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530641 return;
642
643out_copy:
644 if (info->nand.options & NAND_BUSWIDTH_16)
645 omap_read_buf16(mtd, buf, len);
646 else
647 omap_read_buf8(mtd, buf, len);
648}
649
650/*
651 * omap_write_buf_irq_pref - write buffer to NAND controller
652 * @mtd: MTD device structure
653 * @buf: data buffer
654 * @len: number of bytes to write
655 */
656static void omap_write_buf_irq_pref(struct mtd_info *mtd,
657 const u_char *buf, int len)
658{
659 struct omap_nand_info *info = container_of(mtd,
660 struct omap_nand_info, mtd);
661 int ret = 0;
662 unsigned long tim, limit;
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700663 u32 val;
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530664
665 if (len <= mtd->oobsize) {
666 omap_write_buf_pref(mtd, buf, len);
667 return;
668 }
669
670 info->iomode = OMAP_NAND_IO_WRITE;
671 info->buf = (u_char *) buf;
672 init_completion(&info->comp);
673
Sukumar Ghorai317379a2011-01-28 15:42:07 +0530674 /* configure and start prefetch transfer : size=24 */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700675 ret = omap_prefetch_enable(info->gpmc_cs,
676 (PREFETCH_FIFOTHRESHOLD_MAX * 3) / 8, 0x0, len, 0x1, info);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530677 if (ret)
678 /* PFPW engine is busy, use cpu copy method */
679 goto out_copy;
680
681 info->buf_len = len;
Afzal Mohammed5c468452012-08-30 12:53:24 -0700682
683 enable_irq(info->gpmc_irq_count);
684 enable_irq(info->gpmc_irq_fifo);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530685
686 /* waiting for write to complete */
687 wait_for_completion(&info->comp);
Afzal Mohammed5c468452012-08-30 12:53:24 -0700688
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530689 /* wait for data to flushed-out before reset the prefetch */
690 tim = 0;
691 limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700692 do {
693 val = readl(info->reg.gpmc_prefetch_status);
Afzal Mohammed47f88af42012-09-29 18:20:11 +0530694 val = PREFETCH_STATUS_COUNT(val);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530695 cpu_relax();
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700696 } while (val && (tim++ < limit));
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530697
698 /* disable and stop the PFPW engine */
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700699 omap_prefetch_reset(info->gpmc_cs, info);
Sukumar Ghorai4e070372011-01-28 15:42:06 +0530700 return;
701
702out_copy:
703 if (info->nand.options & NAND_BUSWIDTH_16)
704 omap_write_buf16(mtd, buf, len);
705 else
706 omap_write_buf8(mtd, buf, len);
707}
708
Vimal Singh67ce04b2009-05-12 13:47:03 -0700709/**
Vimal Singh67ce04b2009-05-12 13:47:03 -0700710 * gen_true_ecc - This function will generate true ECC value
711 * @ecc_buf: buffer to store ecc code
712 *
713 * This generated true ECC value can be used when correcting
714 * data read from NAND flash memory core
715 */
716static void gen_true_ecc(u8 *ecc_buf)
717{
718 u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
719 ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
720
721 ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
722 P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
723 ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
724 P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
725 ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
726 P1e(tmp) | P2048o(tmp) | P2048e(tmp));
727}
728
729/**
730 * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data
731 * @ecc_data1: ecc code from nand spare area
732 * @ecc_data2: ecc code from hardware register obtained from hardware ecc
733 * @page_data: page data
734 *
735 * This function compares two ECC's and indicates if there is an error.
736 * If the error can be corrected it will be corrected to the buffer.
John Ogness74f1b722011-02-28 13:12:46 +0100737 * If there is no error, %0 is returned. If there is an error but it
738 * was corrected, %1 is returned. Otherwise, %-1 is returned.
Vimal Singh67ce04b2009-05-12 13:47:03 -0700739 */
740static int omap_compare_ecc(u8 *ecc_data1, /* read from NAND memory */
741 u8 *ecc_data2, /* read from register */
742 u8 *page_data)
743{
744 uint i;
745 u8 tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
746 u8 comp0_bit[8], comp1_bit[8], comp2_bit[8];
747 u8 ecc_bit[24];
748 u8 ecc_sum = 0;
749 u8 find_bit = 0;
750 uint find_byte = 0;
751 int isEccFF;
752
753 isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
754
755 gen_true_ecc(ecc_data1);
756 gen_true_ecc(ecc_data2);
757
758 for (i = 0; i <= 2; i++) {
759 *(ecc_data1 + i) = ~(*(ecc_data1 + i));
760 *(ecc_data2 + i) = ~(*(ecc_data2 + i));
761 }
762
763 for (i = 0; i < 8; i++) {
764 tmp0_bit[i] = *ecc_data1 % 2;
765 *ecc_data1 = *ecc_data1 / 2;
766 }
767
768 for (i = 0; i < 8; i++) {
769 tmp1_bit[i] = *(ecc_data1 + 1) % 2;
770 *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
771 }
772
773 for (i = 0; i < 8; i++) {
774 tmp2_bit[i] = *(ecc_data1 + 2) % 2;
775 *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
776 }
777
778 for (i = 0; i < 8; i++) {
779 comp0_bit[i] = *ecc_data2 % 2;
780 *ecc_data2 = *ecc_data2 / 2;
781 }
782
783 for (i = 0; i < 8; i++) {
784 comp1_bit[i] = *(ecc_data2 + 1) % 2;
785 *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
786 }
787
788 for (i = 0; i < 8; i++) {
789 comp2_bit[i] = *(ecc_data2 + 2) % 2;
790 *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
791 }
792
793 for (i = 0; i < 6; i++)
794 ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
795
796 for (i = 0; i < 8; i++)
797 ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
798
799 for (i = 0; i < 8; i++)
800 ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
801
802 ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
803 ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
804
805 for (i = 0; i < 24; i++)
806 ecc_sum += ecc_bit[i];
807
808 switch (ecc_sum) {
809 case 0:
810 /* Not reached because this function is not called if
811 * ECC values are equal
812 */
813 return 0;
814
815 case 1:
816 /* Uncorrectable error */
Brian Norris289c0522011-07-19 10:06:09 -0700817 pr_debug("ECC UNCORRECTED_ERROR 1\n");
Vimal Singh67ce04b2009-05-12 13:47:03 -0700818 return -1;
819
820 case 11:
821 /* UN-Correctable error */
Brian Norris289c0522011-07-19 10:06:09 -0700822 pr_debug("ECC UNCORRECTED_ERROR B\n");
Vimal Singh67ce04b2009-05-12 13:47:03 -0700823 return -1;
824
825 case 12:
826 /* Correctable error */
827 find_byte = (ecc_bit[23] << 8) +
828 (ecc_bit[21] << 7) +
829 (ecc_bit[19] << 6) +
830 (ecc_bit[17] << 5) +
831 (ecc_bit[15] << 4) +
832 (ecc_bit[13] << 3) +
833 (ecc_bit[11] << 2) +
834 (ecc_bit[9] << 1) +
835 ecc_bit[7];
836
837 find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
838
Brian Norris0a32a102011-07-19 10:06:10 -0700839 pr_debug("Correcting single bit ECC error at offset: "
840 "%d, bit: %d\n", find_byte, find_bit);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700841
842 page_data[find_byte] ^= (1 << find_bit);
843
John Ogness74f1b722011-02-28 13:12:46 +0100844 return 1;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700845 default:
846 if (isEccFF) {
847 if (ecc_data2[0] == 0 &&
848 ecc_data2[1] == 0 &&
849 ecc_data2[2] == 0)
850 return 0;
851 }
Brian Norris289c0522011-07-19 10:06:09 -0700852 pr_debug("UNCORRECTED_ERROR default\n");
Vimal Singh67ce04b2009-05-12 13:47:03 -0700853 return -1;
854 }
855}
856
857/**
858 * omap_correct_data - Compares the ECC read with HW generated ECC
859 * @mtd: MTD device structure
860 * @dat: page data
861 * @read_ecc: ecc read from nand flash
862 * @calc_ecc: ecc read from HW ECC registers
863 *
864 * Compares the ecc read from nand spare area with ECC registers values
John Ogness74f1b722011-02-28 13:12:46 +0100865 * and if ECC's mismatched, it will call 'omap_compare_ecc' for error
866 * detection and correction. If there are no errors, %0 is returned. If
867 * there were errors and all of the errors were corrected, the number of
868 * corrected errors is returned. If uncorrectable errors exist, %-1 is
869 * returned.
Vimal Singh67ce04b2009-05-12 13:47:03 -0700870 */
871static int omap_correct_data(struct mtd_info *mtd, u_char *dat,
872 u_char *read_ecc, u_char *calc_ecc)
873{
874 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
875 mtd);
876 int blockCnt = 0, i = 0, ret = 0;
John Ogness74f1b722011-02-28 13:12:46 +0100877 int stat = 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700878
879 /* Ex NAND_ECC_HW12_2048 */
880 if ((info->nand.ecc.mode == NAND_ECC_HW) &&
881 (info->nand.ecc.size == 2048))
882 blockCnt = 4;
883 else
884 blockCnt = 1;
885
886 for (i = 0; i < blockCnt; i++) {
887 if (memcmp(read_ecc, calc_ecc, 3) != 0) {
888 ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
889 if (ret < 0)
890 return ret;
John Ogness74f1b722011-02-28 13:12:46 +0100891 /* keep track of the number of corrected errors */
892 stat += ret;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700893 }
894 read_ecc += 3;
895 calc_ecc += 3;
896 dat += 512;
897 }
John Ogness74f1b722011-02-28 13:12:46 +0100898 return stat;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700899}
900
901/**
902 * omap_calcuate_ecc - Generate non-inverted ECC bytes.
903 * @mtd: MTD device structure
904 * @dat: The pointer to data on which ecc is computed
905 * @ecc_code: The ecc_code buffer
906 *
907 * Using noninverted ECC can be considered ugly since writing a blank
908 * page ie. padding will clear the ECC bytes. This is no problem as long
909 * nobody is trying to write data on the seemingly unused page. Reading
910 * an erased page will produce an ECC mismatch between generated and read
911 * ECC bytes that has to be dealt with separately.
912 */
913static int omap_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
914 u_char *ecc_code)
915{
916 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
917 mtd);
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700918 u32 val;
919
920 val = readl(info->reg.gpmc_ecc_config);
921 if (((val >> ECC_CONFIG_CS_SHIFT) & ~CS_MASK) != info->gpmc_cs)
922 return -EINVAL;
923
924 /* read ecc result */
925 val = readl(info->reg.gpmc_ecc1_result);
926 *ecc_code++ = val; /* P128e, ..., P1e */
927 *ecc_code++ = val >> 16; /* P128o, ..., P1o */
928 /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
929 *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
930
931 return 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700932}
933
934/**
935 * omap_enable_hwecc - This function enables the hardware ecc functionality
936 * @mtd: MTD device structure
937 * @mode: Read/Write mode
938 */
939static void omap_enable_hwecc(struct mtd_info *mtd, int mode)
940{
941 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
942 mtd);
943 struct nand_chip *chip = mtd->priv;
944 unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700945 u32 val;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700946
Afzal Mohammed65b97cf2012-08-30 12:53:22 -0700947 /* clear ecc and enable bits */
948 val = ECCCLEAR | ECC1;
949 writel(val, info->reg.gpmc_ecc_control);
950
951 /* program ecc and result sizes */
952 val = ((((info->nand.ecc.size >> 1) - 1) << ECCSIZE1_SHIFT) |
953 ECC1RESULTSIZE);
954 writel(val, info->reg.gpmc_ecc_size_config);
955
956 switch (mode) {
957 case NAND_ECC_READ:
958 case NAND_ECC_WRITE:
959 writel(ECCCLEAR | ECC1, info->reg.gpmc_ecc_control);
960 break;
961 case NAND_ECC_READSYN:
962 writel(ECCCLEAR, info->reg.gpmc_ecc_control);
963 break;
964 default:
965 dev_info(&info->pdev->dev,
966 "error: unrecognized Mode[%d]!\n", mode);
967 break;
968 }
969
970 /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
971 val = (dev_width << 7) | (info->gpmc_cs << 1) | (0x1);
972 writel(val, info->reg.gpmc_ecc_config);
Vimal Singh67ce04b2009-05-12 13:47:03 -0700973}
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +0000974
Vimal Singh67ce04b2009-05-12 13:47:03 -0700975/**
976 * omap_wait - wait until the command is done
977 * @mtd: MTD device structure
978 * @chip: NAND Chip structure
979 *
980 * Wait function is called during Program and erase operations and
981 * the way it is called from MTD layer, we should wait till the NAND
982 * chip is ready after the programming/erase operation has completed.
983 *
984 * Erase can take up to 400ms and program up to 20ms according to
985 * general NAND and SmartMedia specs
986 */
987static int omap_wait(struct mtd_info *mtd, struct nand_chip *chip)
988{
989 struct nand_chip *this = mtd->priv;
990 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
991 mtd);
992 unsigned long timeo = jiffies;
Ivan Djelica9c465f2012-04-17 13:11:53 +0200993 int status, state = this->state;
Vimal Singh67ce04b2009-05-12 13:47:03 -0700994
995 if (state == FL_ERASING)
996 timeo += (HZ * 400) / 1000;
997 else
998 timeo += (HZ * 20) / 1000;
999
Afzal Mohammed65b97cf2012-08-30 12:53:22 -07001000 writeb(NAND_CMD_STATUS & 0xFF, info->reg.gpmc_nand_command);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001001 while (time_before(jiffies, timeo)) {
Afzal Mohammed65b97cf2012-08-30 12:53:22 -07001002 status = readb(info->reg.gpmc_nand_data);
vimal singhc276aca2009-06-27 11:07:06 +05301003 if (status & NAND_STATUS_READY)
Vimal Singh67ce04b2009-05-12 13:47:03 -07001004 break;
vimal singhc276aca2009-06-27 11:07:06 +05301005 cond_resched();
Vimal Singh67ce04b2009-05-12 13:47:03 -07001006 }
Ivan Djelica9c465f2012-04-17 13:11:53 +02001007
Afzal Mohammed4ea1e4b2012-09-29 11:22:21 +05301008 status = readb(info->reg.gpmc_nand_data);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001009 return status;
1010}
1011
1012/**
1013 * omap_dev_ready - calls the platform specific dev_ready function
1014 * @mtd: MTD device structure
1015 */
1016static int omap_dev_ready(struct mtd_info *mtd)
1017{
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +00001018 unsigned int val = 0;
Vimal Singh67ce04b2009-05-12 13:47:03 -07001019 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1020 mtd);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001021
Afzal Mohammed65b97cf2012-08-30 12:53:22 -07001022 val = readl(info->reg.gpmc_status);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001023
Afzal Mohammed65b97cf2012-08-30 12:53:22 -07001024 if ((val & 0x100) == 0x100) {
1025 return 1;
1026 } else {
1027 return 0;
1028 }
Vimal Singh67ce04b2009-05-12 13:47:03 -07001029}
1030
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001031#ifdef CONFIG_MTD_NAND_OMAP_BCH
1032
1033/**
1034 * omap3_enable_hwecc_bch - Program OMAP3 GPMC to perform BCH ECC correction
1035 * @mtd: MTD device structure
1036 * @mode: Read/Write mode
1037 */
1038static void omap3_enable_hwecc_bch(struct mtd_info *mtd, int mode)
1039{
1040 int nerrors;
Afzal Mohammed2ef9f3d2012-10-04 19:03:06 +05301041 unsigned int dev_width, nsectors;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001042 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1043 mtd);
1044 struct nand_chip *chip = mtd->priv;
Afzal Mohammed2ef9f3d2012-10-04 19:03:06 +05301045 u32 val;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001046
Philip Avinashc3e4b992013-01-04 13:26:49 +05301047 nerrors = info->nand.ecc.strength;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001048 dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
Afzal Mohammed2ef9f3d2012-10-04 19:03:06 +05301049 nsectors = 1;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001050 /*
1051 * Program GPMC to perform correction on one 512-byte sector at a time.
1052 * Using 4 sectors at a time (i.e. ecc.size = 2048) is also possible and
1053 * gives a slight (5%) performance gain (but requires additional code).
1054 */
Afzal Mohammed2ef9f3d2012-10-04 19:03:06 +05301055
1056 writel(ECC1, info->reg.gpmc_ecc_control);
1057
1058 /*
1059 * When using BCH, sector size is hardcoded to 512 bytes.
1060 * Here we are using wrapping mode 6 both for reading and writing, with:
1061 * size0 = 0 (no additional protected byte in spare area)
1062 * size1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area)
1063 */
1064 val = (32 << ECCSIZE1_SHIFT) | (0 << ECCSIZE0_SHIFT);
1065 writel(val, info->reg.gpmc_ecc_size_config);
1066
1067 /* BCH configuration */
1068 val = ((1 << 16) | /* enable BCH */
1069 (((nerrors == 8) ? 1 : 0) << 12) | /* 8 or 4 bits */
1070 (0x06 << 8) | /* wrap mode = 6 */
1071 (dev_width << 7) | /* bus width */
1072 (((nsectors-1) & 0x7) << 4) | /* number of sectors */
1073 (info->gpmc_cs << 1) | /* ECC CS */
1074 (0x1)); /* enable ECC */
1075
1076 writel(val, info->reg.gpmc_ecc_config);
1077
1078 /* clear ecc and enable bits */
1079 writel(ECCCLEAR | ECC1, info->reg.gpmc_ecc_control);
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001080}
1081
1082/**
1083 * omap3_calculate_ecc_bch4 - Generate 7 bytes of ECC bytes
1084 * @mtd: MTD device structure
1085 * @dat: The pointer to data on which ecc is computed
1086 * @ecc_code: The ecc_code buffer
1087 */
1088static int omap3_calculate_ecc_bch4(struct mtd_info *mtd, const u_char *dat,
1089 u_char *ecc_code)
1090{
1091 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1092 mtd);
Afzal Mohammed2ef9f3d2012-10-04 19:03:06 +05301093 unsigned long nsectors, val1, val2;
1094 int i;
1095
1096 nsectors = ((readl(info->reg.gpmc_ecc_config) >> 4) & 0x7) + 1;
1097
1098 for (i = 0; i < nsectors; i++) {
1099
1100 /* Read hw-computed remainder */
1101 val1 = readl(info->reg.gpmc_bch_result0[i]);
1102 val2 = readl(info->reg.gpmc_bch_result1[i]);
1103
1104 /*
1105 * Add constant polynomial to remainder, in order to get an ecc
1106 * sequence of 0xFFs for a buffer filled with 0xFFs; and
1107 * left-justify the resulting polynomial.
1108 */
1109 *ecc_code++ = 0x28 ^ ((val2 >> 12) & 0xFF);
1110 *ecc_code++ = 0x13 ^ ((val2 >> 4) & 0xFF);
1111 *ecc_code++ = 0xcc ^ (((val2 & 0xF) << 4)|((val1 >> 28) & 0xF));
1112 *ecc_code++ = 0x39 ^ ((val1 >> 20) & 0xFF);
1113 *ecc_code++ = 0x96 ^ ((val1 >> 12) & 0xFF);
1114 *ecc_code++ = 0xac ^ ((val1 >> 4) & 0xFF);
1115 *ecc_code++ = 0x7f ^ ((val1 & 0xF) << 4);
1116 }
1117
1118 return 0;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001119}
1120
1121/**
1122 * omap3_calculate_ecc_bch8 - Generate 13 bytes of ECC bytes
1123 * @mtd: MTD device structure
1124 * @dat: The pointer to data on which ecc is computed
1125 * @ecc_code: The ecc_code buffer
1126 */
1127static int omap3_calculate_ecc_bch8(struct mtd_info *mtd, const u_char *dat,
1128 u_char *ecc_code)
1129{
1130 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1131 mtd);
Afzal Mohammed2ef9f3d2012-10-04 19:03:06 +05301132 unsigned long nsectors, val1, val2, val3, val4;
1133 int i;
1134
1135 nsectors = ((readl(info->reg.gpmc_ecc_config) >> 4) & 0x7) + 1;
1136
1137 for (i = 0; i < nsectors; i++) {
1138
1139 /* Read hw-computed remainder */
1140 val1 = readl(info->reg.gpmc_bch_result0[i]);
1141 val2 = readl(info->reg.gpmc_bch_result1[i]);
1142 val3 = readl(info->reg.gpmc_bch_result2[i]);
1143 val4 = readl(info->reg.gpmc_bch_result3[i]);
1144
1145 /*
1146 * Add constant polynomial to remainder, in order to get an ecc
1147 * sequence of 0xFFs for a buffer filled with 0xFFs.
1148 */
1149 *ecc_code++ = 0xef ^ (val4 & 0xFF);
1150 *ecc_code++ = 0x51 ^ ((val3 >> 24) & 0xFF);
1151 *ecc_code++ = 0x2e ^ ((val3 >> 16) & 0xFF);
1152 *ecc_code++ = 0x09 ^ ((val3 >> 8) & 0xFF);
1153 *ecc_code++ = 0xed ^ (val3 & 0xFF);
1154 *ecc_code++ = 0x93 ^ ((val2 >> 24) & 0xFF);
1155 *ecc_code++ = 0x9a ^ ((val2 >> 16) & 0xFF);
1156 *ecc_code++ = 0xc2 ^ ((val2 >> 8) & 0xFF);
1157 *ecc_code++ = 0x97 ^ (val2 & 0xFF);
1158 *ecc_code++ = 0x79 ^ ((val1 >> 24) & 0xFF);
1159 *ecc_code++ = 0xe5 ^ ((val1 >> 16) & 0xFF);
1160 *ecc_code++ = 0x24 ^ ((val1 >> 8) & 0xFF);
1161 *ecc_code++ = 0xb5 ^ (val1 & 0xFF);
1162 }
1163
1164 return 0;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001165}
1166
1167/**
1168 * omap3_correct_data_bch - Decode received data and correct errors
1169 * @mtd: MTD device structure
1170 * @data: page data
1171 * @read_ecc: ecc read from nand flash
1172 * @calc_ecc: ecc read from HW ECC registers
1173 */
1174static int omap3_correct_data_bch(struct mtd_info *mtd, u_char *data,
1175 u_char *read_ecc, u_char *calc_ecc)
1176{
1177 int i, count;
1178 /* cannot correct more than 8 errors */
1179 unsigned int errloc[8];
1180 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1181 mtd);
1182
1183 count = decode_bch(info->bch, NULL, 512, read_ecc, calc_ecc, NULL,
1184 errloc);
1185 if (count > 0) {
1186 /* correct errors */
1187 for (i = 0; i < count; i++) {
1188 /* correct data only, not ecc bytes */
1189 if (errloc[i] < 8*512)
1190 data[errloc[i]/8] ^= 1 << (errloc[i] & 7);
1191 pr_debug("corrected bitflip %u\n", errloc[i]);
1192 }
1193 } else if (count < 0) {
1194 pr_err("ecc unrecoverable error\n");
1195 }
1196 return count;
1197}
1198
1199/**
1200 * omap3_free_bch - Release BCH ecc resources
1201 * @mtd: MTD device structure
1202 */
1203static void omap3_free_bch(struct mtd_info *mtd)
1204{
1205 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1206 mtd);
1207 if (info->bch) {
1208 free_bch(info->bch);
1209 info->bch = NULL;
1210 }
1211}
1212
1213/**
1214 * omap3_init_bch - Initialize BCH ECC
1215 * @mtd: MTD device structure
1216 * @ecc_opt: OMAP ECC mode (OMAP_ECC_BCH4_CODE_HW or OMAP_ECC_BCH8_CODE_HW)
1217 */
1218static int omap3_init_bch(struct mtd_info *mtd, int ecc_opt)
1219{
Afzal Mohammed2ef9f3d2012-10-04 19:03:06 +05301220 int max_errors;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001221 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1222 mtd);
1223#ifdef CONFIG_MTD_NAND_OMAP_BCH8
Philip Avinashc3e4b992013-01-04 13:26:49 +05301224 const int hw_errors = BCH8_MAX_ERROR;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001225#else
Philip Avinashc3e4b992013-01-04 13:26:49 +05301226 const int hw_errors = BCH4_MAX_ERROR;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001227#endif
1228 info->bch = NULL;
1229
Philip Avinashc3e4b992013-01-04 13:26:49 +05301230 max_errors = (ecc_opt == OMAP_ECC_BCH8_CODE_HW) ?
1231 BCH8_MAX_ERROR : BCH4_MAX_ERROR;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001232 if (max_errors != hw_errors) {
1233 pr_err("cannot configure %d-bit BCH ecc, only %d-bit supported",
1234 max_errors, hw_errors);
1235 goto fail;
1236 }
1237
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001238 /* software bch library is only used to detect and locate errors */
1239 info->bch = init_bch(13, max_errors, 0x201b /* hw polynomial */);
1240 if (!info->bch)
1241 goto fail;
1242
1243 info->nand.ecc.size = 512;
1244 info->nand.ecc.hwctl = omap3_enable_hwecc_bch;
1245 info->nand.ecc.correct = omap3_correct_data_bch;
1246 info->nand.ecc.mode = NAND_ECC_HW;
1247
1248 /*
1249 * The number of corrected errors in an ecc block that will trigger
1250 * block scrubbing defaults to the ecc strength (4 or 8).
1251 * Set mtd->bitflip_threshold here to define a custom threshold.
1252 */
1253
1254 if (max_errors == 8) {
1255 info->nand.ecc.strength = 8;
1256 info->nand.ecc.bytes = 13;
1257 info->nand.ecc.calculate = omap3_calculate_ecc_bch8;
1258 } else {
1259 info->nand.ecc.strength = 4;
1260 info->nand.ecc.bytes = 7;
1261 info->nand.ecc.calculate = omap3_calculate_ecc_bch4;
1262 }
1263
1264 pr_info("enabling NAND BCH ecc with %d-bit correction\n", max_errors);
1265 return 0;
1266fail:
1267 omap3_free_bch(mtd);
1268 return -1;
1269}
1270
1271/**
1272 * omap3_init_bch_tail - Build an oob layout for BCH ECC correction.
1273 * @mtd: MTD device structure
1274 */
1275static int omap3_init_bch_tail(struct mtd_info *mtd)
1276{
1277 int i, steps;
1278 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1279 mtd);
1280 struct nand_ecclayout *layout = &info->ecclayout;
1281
1282 /* build oob layout */
1283 steps = mtd->writesize/info->nand.ecc.size;
1284 layout->eccbytes = steps*info->nand.ecc.bytes;
1285
1286 /* do not bother creating special oob layouts for small page devices */
1287 if (mtd->oobsize < 64) {
1288 pr_err("BCH ecc is not supported on small page devices\n");
1289 goto fail;
1290 }
1291
1292 /* reserve 2 bytes for bad block marker */
1293 if (layout->eccbytes+2 > mtd->oobsize) {
1294 pr_err("no oob layout available for oobsize %d eccbytes %u\n",
1295 mtd->oobsize, layout->eccbytes);
1296 goto fail;
1297 }
1298
1299 /* put ecc bytes at oob tail */
1300 for (i = 0; i < layout->eccbytes; i++)
1301 layout->eccpos[i] = mtd->oobsize-layout->eccbytes+i;
1302
1303 layout->oobfree[0].offset = 2;
1304 layout->oobfree[0].length = mtd->oobsize-2-layout->eccbytes;
1305 info->nand.ecc.layout = layout;
1306
1307 if (!(info->nand.options & NAND_BUSWIDTH_16))
1308 info->nand.badblock_pattern = &bb_descrip_flashbased;
1309 return 0;
1310fail:
1311 omap3_free_bch(mtd);
1312 return -1;
1313}
1314
1315#else
1316static int omap3_init_bch(struct mtd_info *mtd, int ecc_opt)
1317{
1318 pr_err("CONFIG_MTD_NAND_OMAP_BCH is not enabled\n");
1319 return -1;
1320}
1321static int omap3_init_bch_tail(struct mtd_info *mtd)
1322{
1323 return -1;
1324}
1325static void omap3_free_bch(struct mtd_info *mtd)
1326{
1327}
1328#endif /* CONFIG_MTD_NAND_OMAP_BCH */
1329
Bill Pemberton06f25512012-11-19 13:23:07 -05001330static int omap_nand_probe(struct platform_device *pdev)
Vimal Singh67ce04b2009-05-12 13:47:03 -07001331{
1332 struct omap_nand_info *info;
1333 struct omap_nand_platform_data *pdata;
1334 int err;
Sukumar Ghoraif040d332011-01-28 15:42:09 +05301335 int i, offset;
Russell King763e7352012-04-25 00:16:00 +01001336 dma_cap_mask_t mask;
1337 unsigned sig;
Afzal Mohammed9c4c2f82012-08-30 12:53:23 -07001338 struct resource *res;
Vimal Singh67ce04b2009-05-12 13:47:03 -07001339
1340 pdata = pdev->dev.platform_data;
1341 if (pdata == NULL) {
1342 dev_err(&pdev->dev, "platform data missing\n");
1343 return -ENODEV;
1344 }
1345
1346 info = kzalloc(sizeof(struct omap_nand_info), GFP_KERNEL);
1347 if (!info)
1348 return -ENOMEM;
1349
1350 platform_set_drvdata(pdev, info);
1351
1352 spin_lock_init(&info->controller.lock);
1353 init_waitqueue_head(&info->controller.wq);
1354
1355 info->pdev = pdev;
1356
1357 info->gpmc_cs = pdata->cs;
Afzal Mohammed65b97cf2012-08-30 12:53:22 -07001358 info->reg = pdata->reg;
Vimal Singh67ce04b2009-05-12 13:47:03 -07001359
1360 info->mtd.priv = &info->nand;
1361 info->mtd.name = dev_name(&pdev->dev);
1362 info->mtd.owner = THIS_MODULE;
1363
Sukumar Ghoraid5ce2b62011-01-28 15:42:03 +05301364 info->nand.options = pdata->devsize;
Vimal Singh2f70a1e2010-02-15 10:03:33 -08001365 info->nand.options |= NAND_SKIP_BBTSCAN;
Vimal Singh67ce04b2009-05-12 13:47:03 -07001366
Afzal Mohammed9c4c2f82012-08-30 12:53:23 -07001367 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1368 if (res == NULL) {
1369 err = -EINVAL;
1370 dev_err(&pdev->dev, "error getting memory resource\n");
1371 goto out_free_info;
1372 }
Vimal Singh67ce04b2009-05-12 13:47:03 -07001373
Afzal Mohammed9c4c2f82012-08-30 12:53:23 -07001374 info->phys_base = res->start;
1375 info->mem_size = resource_size(res);
1376
1377 if (!request_mem_region(info->phys_base, info->mem_size,
Vimal Singh67ce04b2009-05-12 13:47:03 -07001378 pdev->dev.driver->name)) {
1379 err = -EBUSY;
Vimal Singh2f70a1e2010-02-15 10:03:33 -08001380 goto out_free_info;
Vimal Singh67ce04b2009-05-12 13:47:03 -07001381 }
1382
Afzal Mohammed9c4c2f82012-08-30 12:53:23 -07001383 info->nand.IO_ADDR_R = ioremap(info->phys_base, info->mem_size);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001384 if (!info->nand.IO_ADDR_R) {
1385 err = -ENOMEM;
1386 goto out_release_mem_region;
1387 }
vimal singh59e9c5a2009-07-13 16:26:24 +05301388
Vimal Singh67ce04b2009-05-12 13:47:03 -07001389 info->nand.controller = &info->controller;
1390
1391 info->nand.IO_ADDR_W = info->nand.IO_ADDR_R;
1392 info->nand.cmd_ctrl = omap_hwcontrol;
1393
Vimal Singh67ce04b2009-05-12 13:47:03 -07001394 /*
1395 * If RDY/BSY line is connected to OMAP then use the omap ready
Peter Meerwald4cacbe22012-07-19 13:21:04 +02001396 * function and the generic nand_wait function which reads the status
1397 * register after monitoring the RDY/BSY line. Otherwise use a standard
Vimal Singh67ce04b2009-05-12 13:47:03 -07001398 * chip delay which is slightly more than tR (AC Timing) of the NAND
1399 * device and read status register until you get a failure or success
1400 */
1401 if (pdata->dev_ready) {
1402 info->nand.dev_ready = omap_dev_ready;
1403 info->nand.chip_delay = 0;
1404 } else {
1405 info->nand.waitfunc = omap_wait;
1406 info->nand.chip_delay = 50;
1407 }
1408
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301409 switch (pdata->xfer_type) {
1410 case NAND_OMAP_PREFETCH_POLLED:
vimal singh59e9c5a2009-07-13 16:26:24 +05301411 info->nand.read_buf = omap_read_buf_pref;
1412 info->nand.write_buf = omap_write_buf_pref;
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301413 break;
vimal singhdfe32892009-07-13 16:29:16 +05301414
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301415 case NAND_OMAP_POLLED:
vimal singh59e9c5a2009-07-13 16:26:24 +05301416 if (info->nand.options & NAND_BUSWIDTH_16) {
1417 info->nand.read_buf = omap_read_buf16;
1418 info->nand.write_buf = omap_write_buf16;
1419 } else {
1420 info->nand.read_buf = omap_read_buf8;
1421 info->nand.write_buf = omap_write_buf8;
1422 }
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301423 break;
1424
1425 case NAND_OMAP_PREFETCH_DMA:
Russell King763e7352012-04-25 00:16:00 +01001426 dma_cap_zero(mask);
1427 dma_cap_set(DMA_SLAVE, mask);
1428 sig = OMAP24XX_DMA_GPMC;
1429 info->dma = dma_request_channel(mask, omap_dma_filter_fn, &sig);
1430 if (!info->dma) {
Russell King2df41d02012-04-25 00:19:39 +01001431 dev_err(&pdev->dev, "DMA engine request failed\n");
1432 err = -ENXIO;
1433 goto out_release_mem_region;
Russell King763e7352012-04-25 00:16:00 +01001434 } else {
1435 struct dma_slave_config cfg;
Russell King763e7352012-04-25 00:16:00 +01001436
1437 memset(&cfg, 0, sizeof(cfg));
1438 cfg.src_addr = info->phys_base;
1439 cfg.dst_addr = info->phys_base;
1440 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1441 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1442 cfg.src_maxburst = 16;
1443 cfg.dst_maxburst = 16;
Arnd Bergmannd680e2c2012-08-04 11:05:25 +00001444 err = dmaengine_slave_config(info->dma, &cfg);
1445 if (err) {
Russell King763e7352012-04-25 00:16:00 +01001446 dev_err(&pdev->dev, "DMA engine slave config failed: %d\n",
Arnd Bergmannd680e2c2012-08-04 11:05:25 +00001447 err);
Russell King763e7352012-04-25 00:16:00 +01001448 goto out_release_mem_region;
1449 }
1450 info->nand.read_buf = omap_read_buf_dma_pref;
1451 info->nand.write_buf = omap_write_buf_dma_pref;
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301452 }
1453 break;
1454
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301455 case NAND_OMAP_PREFETCH_IRQ:
Afzal Mohammed5c468452012-08-30 12:53:24 -07001456 info->gpmc_irq_fifo = platform_get_irq(pdev, 0);
1457 if (info->gpmc_irq_fifo <= 0) {
1458 dev_err(&pdev->dev, "error getting fifo irq\n");
1459 err = -ENODEV;
1460 goto out_release_mem_region;
1461 }
1462 err = request_irq(info->gpmc_irq_fifo, omap_nand_irq,
1463 IRQF_SHARED, "gpmc-nand-fifo", info);
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301464 if (err) {
1465 dev_err(&pdev->dev, "requesting irq(%d) error:%d",
Afzal Mohammed5c468452012-08-30 12:53:24 -07001466 info->gpmc_irq_fifo, err);
1467 info->gpmc_irq_fifo = 0;
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301468 goto out_release_mem_region;
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301469 }
Afzal Mohammed5c468452012-08-30 12:53:24 -07001470
1471 info->gpmc_irq_count = platform_get_irq(pdev, 1);
1472 if (info->gpmc_irq_count <= 0) {
1473 dev_err(&pdev->dev, "error getting count irq\n");
1474 err = -ENODEV;
1475 goto out_release_mem_region;
1476 }
1477 err = request_irq(info->gpmc_irq_count, omap_nand_irq,
1478 IRQF_SHARED, "gpmc-nand-count", info);
1479 if (err) {
1480 dev_err(&pdev->dev, "requesting irq(%d) error:%d",
1481 info->gpmc_irq_count, err);
1482 info->gpmc_irq_count = 0;
1483 goto out_release_mem_region;
1484 }
1485
1486 info->nand.read_buf = omap_read_buf_irq_pref;
1487 info->nand.write_buf = omap_write_buf_irq_pref;
1488
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301489 break;
1490
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301491 default:
1492 dev_err(&pdev->dev,
1493 "xfer_type(%d) not supported!\n", pdata->xfer_type);
1494 err = -EINVAL;
1495 goto out_release_mem_region;
vimal singh59e9c5a2009-07-13 16:26:24 +05301496 }
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301497
Peter Meerwald4cacbe22012-07-19 13:21:04 +02001498 /* select the ecc type */
Sukumar Ghoraif3d73f32011-01-28 15:42:08 +05301499 if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_DEFAULT)
1500 info->nand.ecc.mode = NAND_ECC_SOFT;
Sukumar Ghoraif040d332011-01-28 15:42:09 +05301501 else if ((pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW) ||
1502 (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE)) {
Sukumar Ghoraif3d73f32011-01-28 15:42:08 +05301503 info->nand.ecc.bytes = 3;
1504 info->nand.ecc.size = 512;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001505 info->nand.ecc.strength = 1;
Sukumar Ghoraif3d73f32011-01-28 15:42:08 +05301506 info->nand.ecc.calculate = omap_calculate_ecc;
1507 info->nand.ecc.hwctl = omap_enable_hwecc;
1508 info->nand.ecc.correct = omap_correct_data;
1509 info->nand.ecc.mode = NAND_ECC_HW;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001510 } else if ((pdata->ecc_opt == OMAP_ECC_BCH4_CODE_HW) ||
1511 (pdata->ecc_opt == OMAP_ECC_BCH8_CODE_HW)) {
1512 err = omap3_init_bch(&info->mtd, pdata->ecc_opt);
1513 if (err) {
1514 err = -EINVAL;
1515 goto out_release_mem_region;
1516 }
Sukumar Ghoraif3d73f32011-01-28 15:42:08 +05301517 }
Vimal Singh67ce04b2009-05-12 13:47:03 -07001518
1519 /* DIP switches on some boards change between 8 and 16 bit
1520 * bus widths for flash. Try the other width if the first try fails.
1521 */
Jan Weitzela80f1c12011-04-19 16:15:34 +02001522 if (nand_scan_ident(&info->mtd, 1, NULL)) {
Vimal Singh67ce04b2009-05-12 13:47:03 -07001523 info->nand.options ^= NAND_BUSWIDTH_16;
Jan Weitzela80f1c12011-04-19 16:15:34 +02001524 if (nand_scan_ident(&info->mtd, 1, NULL)) {
Vimal Singh67ce04b2009-05-12 13:47:03 -07001525 err = -ENXIO;
1526 goto out_release_mem_region;
1527 }
1528 }
1529
Sukumar Ghoraif040d332011-01-28 15:42:09 +05301530 /* rom code layout */
1531 if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE) {
1532
1533 if (info->nand.options & NAND_BUSWIDTH_16)
1534 offset = 2;
1535 else {
1536 offset = 1;
1537 info->nand.badblock_pattern = &bb_descrip_flashbased;
1538 }
1539 omap_oobinfo.eccbytes = 3 * (info->mtd.oobsize/16);
1540 for (i = 0; i < omap_oobinfo.eccbytes; i++)
1541 omap_oobinfo.eccpos[i] = i+offset;
1542
1543 omap_oobinfo.oobfree->offset = offset + omap_oobinfo.eccbytes;
1544 omap_oobinfo.oobfree->length = info->mtd.oobsize -
1545 (offset + omap_oobinfo.eccbytes);
1546
1547 info->nand.ecc.layout = &omap_oobinfo;
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001548 } else if ((pdata->ecc_opt == OMAP_ECC_BCH4_CODE_HW) ||
1549 (pdata->ecc_opt == OMAP_ECC_BCH8_CODE_HW)) {
1550 /* build OOB layout for BCH ECC correction */
1551 err = omap3_init_bch_tail(&info->mtd);
1552 if (err) {
1553 err = -EINVAL;
1554 goto out_release_mem_region;
1555 }
Sukumar Ghoraif040d332011-01-28 15:42:09 +05301556 }
Sukumar Ghorai1b0b323c2011-01-28 15:42:04 +05301557
Jan Weitzela80f1c12011-04-19 16:15:34 +02001558 /* second phase scan */
1559 if (nand_scan_tail(&info->mtd)) {
1560 err = -ENXIO;
1561 goto out_release_mem_region;
1562 }
1563
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001564 mtd_device_parse_register(&info->mtd, NULL, NULL, pdata->parts,
1565 pdata->nr_parts);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001566
1567 platform_set_drvdata(pdev, &info->mtd);
1568
1569 return 0;
1570
1571out_release_mem_region:
Russell King763e7352012-04-25 00:16:00 +01001572 if (info->dma)
1573 dma_release_channel(info->dma);
Afzal Mohammed5c468452012-08-30 12:53:24 -07001574 if (info->gpmc_irq_count > 0)
1575 free_irq(info->gpmc_irq_count, info);
1576 if (info->gpmc_irq_fifo > 0)
1577 free_irq(info->gpmc_irq_fifo, info);
Afzal Mohammed9c4c2f82012-08-30 12:53:23 -07001578 release_mem_region(info->phys_base, info->mem_size);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001579out_free_info:
1580 kfree(info);
1581
1582 return err;
1583}
1584
1585static int omap_nand_remove(struct platform_device *pdev)
1586{
1587 struct mtd_info *mtd = platform_get_drvdata(pdev);
Vimal Singhf35b6ed2010-01-05 16:01:08 +05301588 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1589 mtd);
Ivan Djelic0e618ef2012-04-30 12:17:18 +02001590 omap3_free_bch(&info->mtd);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001591
1592 platform_set_drvdata(pdev, NULL);
Russell King763e7352012-04-25 00:16:00 +01001593 if (info->dma)
1594 dma_release_channel(info->dma);
1595
Afzal Mohammed5c468452012-08-30 12:53:24 -07001596 if (info->gpmc_irq_count > 0)
1597 free_irq(info->gpmc_irq_count, info);
1598 if (info->gpmc_irq_fifo > 0)
1599 free_irq(info->gpmc_irq_fifo, info);
Sukumar Ghorai4e070372011-01-28 15:42:06 +05301600
Vimal Singh67ce04b2009-05-12 13:47:03 -07001601 /* Release NAND device, its internal structures and partitions */
1602 nand_release(&info->mtd);
Sukumar Ghorai2c01946c2010-07-09 09:14:45 +00001603 iounmap(info->nand.IO_ADDR_R);
Afzal Mohammed48b51d42012-09-29 11:14:47 +05301604 release_mem_region(info->phys_base, info->mem_size);
Andreas Bießmann7d9b1102012-08-31 13:35:41 +02001605 kfree(info);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001606 return 0;
1607}
1608
1609static struct platform_driver omap_nand_driver = {
1610 .probe = omap_nand_probe,
1611 .remove = omap_nand_remove,
1612 .driver = {
1613 .name = DRIVER_NAME,
1614 .owner = THIS_MODULE,
1615 },
1616};
1617
Axel Linf99640d2011-11-27 20:45:03 +08001618module_platform_driver(omap_nand_driver);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001619
Axel Linc804c732011-03-07 11:04:24 +08001620MODULE_ALIAS("platform:" DRIVER_NAME);
Vimal Singh67ce04b2009-05-12 13:47:03 -07001621MODULE_LICENSE("GPL");
1622MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");