Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1 | /* |
| 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 King | 763e735 | 2012-04-25 00:16:00 +0100 | [diff] [blame] | 12 | #include <linux/dmaengine.h> |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 13 | #include <linux/dma-mapping.h> |
| 14 | #include <linux/delay.h> |
Paul Gortmaker | a0e5cc5 | 2011-07-03 15:17:31 -0400 | [diff] [blame] | 15 | #include <linux/module.h> |
Sukumar Ghorai | 4e07037 | 2011-01-28 15:42:06 +0530 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
vimal singh | c276aca | 2009-06-27 11:07:06 +0530 | [diff] [blame] | 17 | #include <linux/jiffies.h> |
| 18 | #include <linux/sched.h> |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 19 | #include <linux/mtd/mtd.h> |
| 20 | #include <linux/mtd/nand.h> |
| 21 | #include <linux/mtd/partitions.h> |
Russell King | 763e735 | 2012-04-25 00:16:00 +0100 | [diff] [blame] | 22 | #include <linux/omap-dma.h> |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 23 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 25 | |
Ivan Djelic | 0e618ef | 2012-04-30 12:17:18 +0200 | [diff] [blame] | 26 | #ifdef CONFIG_MTD_NAND_OMAP_BCH |
| 27 | #include <linux/bch.h> |
| 28 | #endif |
| 29 | |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 30 | #include <plat/dma.h> |
| 31 | #include <plat/gpmc.h> |
| 32 | #include <plat/nand.h> |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 33 | |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 34 | #define DRIVER_NAME "omap2-nand" |
Sukumar Ghorai | 4e07037 | 2011-01-28 15:42:06 +0530 | [diff] [blame] | 35 | #define OMAP_NAND_TIMEOUT_MS 5000 |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 36 | |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 37 | #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 | |
Sukumar Ghorai | f040d33 | 2011-01-28 15:42:09 +0530 | [diff] [blame] | 104 | /* oob info generated runtime depending on ecc algorithm and layout selected */ |
| 105 | static struct nand_ecclayout omap_oobinfo; |
| 106 | /* Define some generic bad / good block scan pattern which are used |
| 107 | * while scanning a device for factory marked good / bad blocks |
| 108 | */ |
| 109 | static uint8_t scan_ff_pattern[] = { 0xff }; |
| 110 | static struct nand_bbt_descr bb_descrip_flashbased = { |
| 111 | .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES, |
| 112 | .offs = 0, |
| 113 | .len = 1, |
| 114 | .pattern = scan_ff_pattern, |
| 115 | }; |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 116 | |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 117 | |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 118 | struct omap_nand_info { |
| 119 | struct nand_hw_control controller; |
| 120 | struct omap_nand_platform_data *pdata; |
| 121 | struct mtd_info mtd; |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 122 | struct nand_chip nand; |
| 123 | struct platform_device *pdev; |
| 124 | |
| 125 | int gpmc_cs; |
| 126 | unsigned long phys_base; |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 127 | struct completion comp; |
Russell King | 763e735 | 2012-04-25 00:16:00 +0100 | [diff] [blame] | 128 | struct dma_chan *dma; |
Sukumar Ghorai | 4e07037 | 2011-01-28 15:42:06 +0530 | [diff] [blame] | 129 | int gpmc_irq; |
| 130 | enum { |
| 131 | OMAP_NAND_IO_READ = 0, /* read */ |
| 132 | OMAP_NAND_IO_WRITE, /* write */ |
| 133 | } iomode; |
| 134 | u_char *buf; |
| 135 | int buf_len; |
Ivan Djelic | 0e618ef | 2012-04-30 12:17:18 +0200 | [diff] [blame] | 136 | |
| 137 | #ifdef CONFIG_MTD_NAND_OMAP_BCH |
| 138 | struct bch_control *bch; |
| 139 | struct nand_ecclayout ecclayout; |
| 140 | #endif |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | /** |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 144 | * omap_hwcontrol - hardware specific access to control-lines |
| 145 | * @mtd: MTD device structure |
| 146 | * @cmd: command to device |
| 147 | * @ctrl: |
| 148 | * NAND_NCE: bit 0 -> don't care |
| 149 | * NAND_CLE: bit 1 -> Command Latch |
| 150 | * NAND_ALE: bit 2 -> Address Latch |
| 151 | * |
| 152 | * NOTE: boards may use different bits for these!! |
| 153 | */ |
| 154 | static void omap_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl) |
| 155 | { |
| 156 | struct omap_nand_info *info = container_of(mtd, |
| 157 | struct omap_nand_info, mtd); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 158 | |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 159 | if (cmd != NAND_CMD_NONE) { |
| 160 | if (ctrl & NAND_CLE) |
| 161 | gpmc_nand_write(info->gpmc_cs, GPMC_NAND_COMMAND, cmd); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 162 | |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 163 | else if (ctrl & NAND_ALE) |
| 164 | gpmc_nand_write(info->gpmc_cs, GPMC_NAND_ADDRESS, cmd); |
| 165 | |
| 166 | else /* NAND_NCE */ |
| 167 | gpmc_nand_write(info->gpmc_cs, GPMC_NAND_DATA, cmd); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 168 | } |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 172 | * omap_read_buf8 - read data from NAND controller into buffer |
| 173 | * @mtd: MTD device structure |
| 174 | * @buf: buffer to store date |
| 175 | * @len: number of bytes to read |
| 176 | */ |
| 177 | static void omap_read_buf8(struct mtd_info *mtd, u_char *buf, int len) |
| 178 | { |
| 179 | struct nand_chip *nand = mtd->priv; |
| 180 | |
| 181 | ioread8_rep(nand->IO_ADDR_R, buf, len); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * omap_write_buf8 - write buffer to NAND controller |
| 186 | * @mtd: MTD device structure |
| 187 | * @buf: data buffer |
| 188 | * @len: number of bytes to write |
| 189 | */ |
| 190 | static void omap_write_buf8(struct mtd_info *mtd, const u_char *buf, int len) |
| 191 | { |
| 192 | struct omap_nand_info *info = container_of(mtd, |
| 193 | struct omap_nand_info, mtd); |
| 194 | u_char *p = (u_char *)buf; |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 195 | u32 status = 0; |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 196 | |
| 197 | while (len--) { |
| 198 | iowrite8(*p++, info->nand.IO_ADDR_W); |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 199 | /* wait until buffer is available for write */ |
| 200 | do { |
| 201 | status = gpmc_read_status(GPMC_STATUS_BUFFER); |
| 202 | } while (!status); |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 207 | * omap_read_buf16 - read data from NAND controller into buffer |
| 208 | * @mtd: MTD device structure |
| 209 | * @buf: buffer to store date |
| 210 | * @len: number of bytes to read |
| 211 | */ |
| 212 | static void omap_read_buf16(struct mtd_info *mtd, u_char *buf, int len) |
| 213 | { |
| 214 | struct nand_chip *nand = mtd->priv; |
| 215 | |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 216 | ioread16_rep(nand->IO_ADDR_R, buf, len / 2); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
| 220 | * omap_write_buf16 - write buffer to NAND controller |
| 221 | * @mtd: MTD device structure |
| 222 | * @buf: data buffer |
| 223 | * @len: number of bytes to write |
| 224 | */ |
| 225 | static void omap_write_buf16(struct mtd_info *mtd, const u_char * buf, int len) |
| 226 | { |
| 227 | struct omap_nand_info *info = container_of(mtd, |
| 228 | struct omap_nand_info, mtd); |
| 229 | u16 *p = (u16 *) buf; |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 230 | u32 status = 0; |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 231 | /* FIXME try bursts of writesw() or DMA ... */ |
| 232 | len >>= 1; |
| 233 | |
| 234 | while (len--) { |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 235 | iowrite16(*p++, info->nand.IO_ADDR_W); |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 236 | /* wait until buffer is available for write */ |
| 237 | do { |
| 238 | status = gpmc_read_status(GPMC_STATUS_BUFFER); |
| 239 | } while (!status); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 242 | |
| 243 | /** |
| 244 | * omap_read_buf_pref - 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 | */ |
| 249 | static void omap_read_buf_pref(struct mtd_info *mtd, u_char *buf, int len) |
| 250 | { |
| 251 | struct omap_nand_info *info = container_of(mtd, |
| 252 | struct omap_nand_info, mtd); |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 253 | uint32_t r_count = 0; |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 254 | int ret = 0; |
| 255 | u32 *p = (u32 *)buf; |
| 256 | |
| 257 | /* take care of subpage reads */ |
Vimal Singh | c3341d0 | 2010-01-07 12:16:26 +0530 | [diff] [blame] | 258 | if (len % 4) { |
| 259 | if (info->nand.options & NAND_BUSWIDTH_16) |
| 260 | omap_read_buf16(mtd, buf, len % 4); |
| 261 | else |
| 262 | omap_read_buf8(mtd, buf, len % 4); |
| 263 | p = (u32 *) (buf + len % 4); |
| 264 | len -= len % 4; |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 265 | } |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 266 | |
| 267 | /* configure and start prefetch transfer */ |
Sukumar Ghorai | 317379a | 2011-01-28 15:42:07 +0530 | [diff] [blame] | 268 | ret = gpmc_prefetch_enable(info->gpmc_cs, |
| 269 | PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x0); |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 270 | if (ret) { |
| 271 | /* PFPW engine is busy, use cpu copy method */ |
| 272 | if (info->nand.options & NAND_BUSWIDTH_16) |
Kishore Kadiyala | c5d8c0c | 2011-05-11 21:17:27 +0530 | [diff] [blame] | 273 | omap_read_buf16(mtd, (u_char *)p, len); |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 274 | else |
Kishore Kadiyala | c5d8c0c | 2011-05-11 21:17:27 +0530 | [diff] [blame] | 275 | omap_read_buf8(mtd, (u_char *)p, len); |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 276 | } else { |
| 277 | do { |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 278 | r_count = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT); |
| 279 | r_count = r_count >> 2; |
| 280 | ioread32_rep(info->nand.IO_ADDR_R, p, r_count); |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 281 | p += r_count; |
| 282 | len -= r_count << 2; |
| 283 | } while (len); |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 284 | /* disable and stop the PFPW engine */ |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 285 | gpmc_prefetch_reset(info->gpmc_cs); |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * omap_write_buf_pref - write buffer to NAND controller |
| 291 | * @mtd: MTD device structure |
| 292 | * @buf: data buffer |
| 293 | * @len: number of bytes to write |
| 294 | */ |
| 295 | static void omap_write_buf_pref(struct mtd_info *mtd, |
| 296 | const u_char *buf, int len) |
| 297 | { |
| 298 | struct omap_nand_info *info = container_of(mtd, |
| 299 | struct omap_nand_info, mtd); |
Sukumar Ghorai | 4e07037 | 2011-01-28 15:42:06 +0530 | [diff] [blame] | 300 | uint32_t w_count = 0; |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 301 | int i = 0, ret = 0; |
Kishore Kadiyala | c5d8c0c | 2011-05-11 21:17:27 +0530 | [diff] [blame] | 302 | u16 *p = (u16 *)buf; |
Sukumar Ghorai | 4e07037 | 2011-01-28 15:42:06 +0530 | [diff] [blame] | 303 | unsigned long tim, limit; |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 304 | |
| 305 | /* take care of subpage writes */ |
| 306 | if (len % 2 != 0) { |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 307 | writeb(*buf, info->nand.IO_ADDR_W); |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 308 | p = (u16 *)(buf + 1); |
| 309 | len--; |
| 310 | } |
| 311 | |
| 312 | /* configure and start prefetch transfer */ |
Sukumar Ghorai | 317379a | 2011-01-28 15:42:07 +0530 | [diff] [blame] | 313 | ret = gpmc_prefetch_enable(info->gpmc_cs, |
| 314 | PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x1); |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 315 | if (ret) { |
| 316 | /* PFPW engine is busy, use cpu copy method */ |
| 317 | if (info->nand.options & NAND_BUSWIDTH_16) |
Kishore Kadiyala | c5d8c0c | 2011-05-11 21:17:27 +0530 | [diff] [blame] | 318 | omap_write_buf16(mtd, (u_char *)p, len); |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 319 | else |
Kishore Kadiyala | c5d8c0c | 2011-05-11 21:17:27 +0530 | [diff] [blame] | 320 | omap_write_buf8(mtd, (u_char *)p, len); |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 321 | } else { |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 322 | while (len) { |
| 323 | w_count = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT); |
| 324 | w_count = w_count >> 1; |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 325 | for (i = 0; (i < w_count) && len; i++, len -= 2) |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 326 | iowrite16(*p++, info->nand.IO_ADDR_W); |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 327 | } |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 328 | /* wait for data to flushed-out before reset the prefetch */ |
Sukumar Ghorai | 4e07037 | 2011-01-28 15:42:06 +0530 | [diff] [blame] | 329 | tim = 0; |
| 330 | limit = (loops_per_jiffy * |
| 331 | msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS)); |
| 332 | while (gpmc_read_status(GPMC_PREFETCH_COUNT) && (tim++ < limit)) |
| 333 | cpu_relax(); |
| 334 | |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 335 | /* disable and stop the PFPW engine */ |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 336 | gpmc_prefetch_reset(info->gpmc_cs); |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 340 | /* |
Russell King | 2df41d0 | 2012-04-25 00:19:39 +0100 | [diff] [blame] | 341 | * omap_nand_dma_callback: callback on the completion of dma transfer |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 342 | * @data: pointer to completion data structure |
| 343 | */ |
Russell King | 763e735 | 2012-04-25 00:16:00 +0100 | [diff] [blame] | 344 | static void omap_nand_dma_callback(void *data) |
| 345 | { |
| 346 | complete((struct completion *) data); |
| 347 | } |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 348 | |
| 349 | /* |
Peter Meerwald | 4cacbe2 | 2012-07-19 13:21:04 +0200 | [diff] [blame^] | 350 | * omap_nand_dma_transfer: configure and start dma transfer |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 351 | * @mtd: MTD device structure |
| 352 | * @addr: virtual address in RAM of source/destination |
| 353 | * @len: number of data bytes to be transferred |
| 354 | * @is_write: flag for read/write operation |
| 355 | */ |
| 356 | static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr, |
| 357 | unsigned int len, int is_write) |
| 358 | { |
| 359 | struct omap_nand_info *info = container_of(mtd, |
| 360 | struct omap_nand_info, mtd); |
Russell King | 2df41d0 | 2012-04-25 00:19:39 +0100 | [diff] [blame] | 361 | struct dma_async_tx_descriptor *tx; |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 362 | enum dma_data_direction dir = is_write ? DMA_TO_DEVICE : |
| 363 | DMA_FROM_DEVICE; |
Russell King | 2df41d0 | 2012-04-25 00:19:39 +0100 | [diff] [blame] | 364 | struct scatterlist sg; |
Sukumar Ghorai | 4e07037 | 2011-01-28 15:42:06 +0530 | [diff] [blame] | 365 | unsigned long tim, limit; |
Russell King | 2df41d0 | 2012-04-25 00:19:39 +0100 | [diff] [blame] | 366 | unsigned n; |
| 367 | int ret; |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 368 | |
| 369 | if (addr >= high_memory) { |
| 370 | struct page *p1; |
| 371 | |
| 372 | if (((size_t)addr & PAGE_MASK) != |
| 373 | ((size_t)(addr + len - 1) & PAGE_MASK)) |
| 374 | goto out_copy; |
| 375 | p1 = vmalloc_to_page(addr); |
| 376 | if (!p1) |
| 377 | goto out_copy; |
| 378 | addr = page_address(p1) + ((size_t)addr & ~PAGE_MASK); |
| 379 | } |
| 380 | |
Russell King | 2df41d0 | 2012-04-25 00:19:39 +0100 | [diff] [blame] | 381 | sg_init_one(&sg, addr, len); |
| 382 | n = dma_map_sg(info->dma->device->dev, &sg, 1, dir); |
| 383 | if (n == 0) { |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 384 | dev_err(&info->pdev->dev, |
| 385 | "Couldn't DMA map a %d byte buffer\n", len); |
| 386 | goto out_copy; |
| 387 | } |
| 388 | |
Russell King | 2df41d0 | 2012-04-25 00:19:39 +0100 | [diff] [blame] | 389 | tx = dmaengine_prep_slave_sg(info->dma, &sg, n, |
| 390 | is_write ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM, |
| 391 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 392 | if (!tx) |
| 393 | goto out_copy_unmap; |
| 394 | |
| 395 | tx->callback = omap_nand_dma_callback; |
| 396 | tx->callback_param = &info->comp; |
| 397 | dmaengine_submit(tx); |
| 398 | |
| 399 | /* configure and start prefetch transfer */ |
Sukumar Ghorai | 317379a | 2011-01-28 15:42:07 +0530 | [diff] [blame] | 400 | ret = gpmc_prefetch_enable(info->gpmc_cs, |
Russell King | 2df41d0 | 2012-04-25 00:19:39 +0100 | [diff] [blame] | 401 | PREFETCH_FIFOTHRESHOLD_MAX, 0x1, len, is_write); |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 402 | if (ret) |
Sukumar Ghorai | 4e07037 | 2011-01-28 15:42:06 +0530 | [diff] [blame] | 403 | /* PFPW engine is busy, use cpu copy method */ |
Grazvydas Ignotas | d7efe22 | 2012-04-11 04:04:34 +0300 | [diff] [blame] | 404 | goto out_copy_unmap; |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 405 | |
| 406 | init_completion(&info->comp); |
Russell King | 2df41d0 | 2012-04-25 00:19:39 +0100 | [diff] [blame] | 407 | dma_async_issue_pending(info->dma); |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 408 | |
| 409 | /* setup and start DMA using dma_addr */ |
| 410 | wait_for_completion(&info->comp); |
Sukumar Ghorai | 4e07037 | 2011-01-28 15:42:06 +0530 | [diff] [blame] | 411 | tim = 0; |
| 412 | limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS)); |
| 413 | while (gpmc_read_status(GPMC_PREFETCH_COUNT) && (tim++ < limit)) |
| 414 | cpu_relax(); |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 415 | |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 416 | /* disable and stop the PFPW engine */ |
Daniel J Blueman | f12f662 | 2010-09-29 21:01:55 +0100 | [diff] [blame] | 417 | gpmc_prefetch_reset(info->gpmc_cs); |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 418 | |
Russell King | 2df41d0 | 2012-04-25 00:19:39 +0100 | [diff] [blame] | 419 | dma_unmap_sg(info->dma->device->dev, &sg, 1, dir); |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 420 | return 0; |
| 421 | |
Grazvydas Ignotas | d7efe22 | 2012-04-11 04:04:34 +0300 | [diff] [blame] | 422 | out_copy_unmap: |
Russell King | 2df41d0 | 2012-04-25 00:19:39 +0100 | [diff] [blame] | 423 | dma_unmap_sg(info->dma->device->dev, &sg, 1, dir); |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 424 | out_copy: |
| 425 | if (info->nand.options & NAND_BUSWIDTH_16) |
| 426 | is_write == 0 ? omap_read_buf16(mtd, (u_char *) addr, len) |
| 427 | : omap_write_buf16(mtd, (u_char *) addr, len); |
| 428 | else |
| 429 | is_write == 0 ? omap_read_buf8(mtd, (u_char *) addr, len) |
| 430 | : omap_write_buf8(mtd, (u_char *) addr, len); |
| 431 | return 0; |
| 432 | } |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 433 | |
| 434 | /** |
| 435 | * omap_read_buf_dma_pref - read data from NAND controller into buffer |
| 436 | * @mtd: MTD device structure |
| 437 | * @buf: buffer to store date |
| 438 | * @len: number of bytes to read |
| 439 | */ |
| 440 | static void omap_read_buf_dma_pref(struct mtd_info *mtd, u_char *buf, int len) |
| 441 | { |
| 442 | if (len <= mtd->oobsize) |
| 443 | omap_read_buf_pref(mtd, buf, len); |
| 444 | else |
| 445 | /* start transfer in DMA mode */ |
| 446 | omap_nand_dma_transfer(mtd, buf, len, 0x0); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * omap_write_buf_dma_pref - write buffer to NAND controller |
| 451 | * @mtd: MTD device structure |
| 452 | * @buf: data buffer |
| 453 | * @len: number of bytes to write |
| 454 | */ |
| 455 | static void omap_write_buf_dma_pref(struct mtd_info *mtd, |
| 456 | const u_char *buf, int len) |
| 457 | { |
| 458 | if (len <= mtd->oobsize) |
| 459 | omap_write_buf_pref(mtd, buf, len); |
| 460 | else |
| 461 | /* start transfer in DMA mode */ |
Vimal Singh | bdaefc4 | 2010-01-05 12:49:24 +0530 | [diff] [blame] | 462 | omap_nand_dma_transfer(mtd, (u_char *) buf, len, 0x1); |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 463 | } |
| 464 | |
Sukumar Ghorai | 4e07037 | 2011-01-28 15:42:06 +0530 | [diff] [blame] | 465 | /* |
Peter Meerwald | 4cacbe2 | 2012-07-19 13:21:04 +0200 | [diff] [blame^] | 466 | * omap_nand_irq - GPMC irq handler |
Sukumar Ghorai | 4e07037 | 2011-01-28 15:42:06 +0530 | [diff] [blame] | 467 | * @this_irq: gpmc irq number |
| 468 | * @dev: omap_nand_info structure pointer is passed here |
| 469 | */ |
| 470 | static irqreturn_t omap_nand_irq(int this_irq, void *dev) |
| 471 | { |
| 472 | struct omap_nand_info *info = (struct omap_nand_info *) dev; |
| 473 | u32 bytes; |
| 474 | u32 irq_stat; |
| 475 | |
| 476 | irq_stat = gpmc_read_status(GPMC_GET_IRQ_STATUS); |
| 477 | bytes = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT); |
| 478 | bytes = bytes & 0xFFFC; /* io in multiple of 4 bytes */ |
| 479 | if (info->iomode == OMAP_NAND_IO_WRITE) { /* checks for write io */ |
| 480 | if (irq_stat & 0x2) |
| 481 | goto done; |
| 482 | |
| 483 | if (info->buf_len && (info->buf_len < bytes)) |
| 484 | bytes = info->buf_len; |
| 485 | else if (!info->buf_len) |
| 486 | bytes = 0; |
| 487 | iowrite32_rep(info->nand.IO_ADDR_W, |
| 488 | (u32 *)info->buf, bytes >> 2); |
| 489 | info->buf = info->buf + bytes; |
| 490 | info->buf_len -= bytes; |
| 491 | |
| 492 | } else { |
| 493 | ioread32_rep(info->nand.IO_ADDR_R, |
| 494 | (u32 *)info->buf, bytes >> 2); |
| 495 | info->buf = info->buf + bytes; |
| 496 | |
| 497 | if (irq_stat & 0x2) |
| 498 | goto done; |
| 499 | } |
| 500 | gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, irq_stat); |
| 501 | |
| 502 | return IRQ_HANDLED; |
| 503 | |
| 504 | done: |
| 505 | complete(&info->comp); |
| 506 | /* disable irq */ |
| 507 | gpmc_cs_configure(info->gpmc_cs, GPMC_ENABLE_IRQ, 0); |
| 508 | |
| 509 | /* clear status */ |
| 510 | gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, irq_stat); |
| 511 | |
| 512 | return IRQ_HANDLED; |
| 513 | } |
| 514 | |
| 515 | /* |
| 516 | * omap_read_buf_irq_pref - read data from NAND controller into buffer |
| 517 | * @mtd: MTD device structure |
| 518 | * @buf: buffer to store date |
| 519 | * @len: number of bytes to read |
| 520 | */ |
| 521 | static void omap_read_buf_irq_pref(struct mtd_info *mtd, u_char *buf, int len) |
| 522 | { |
| 523 | struct omap_nand_info *info = container_of(mtd, |
| 524 | struct omap_nand_info, mtd); |
| 525 | int ret = 0; |
| 526 | |
| 527 | if (len <= mtd->oobsize) { |
| 528 | omap_read_buf_pref(mtd, buf, len); |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | info->iomode = OMAP_NAND_IO_READ; |
| 533 | info->buf = buf; |
| 534 | init_completion(&info->comp); |
| 535 | |
| 536 | /* configure and start prefetch transfer */ |
Sukumar Ghorai | 317379a | 2011-01-28 15:42:07 +0530 | [diff] [blame] | 537 | ret = gpmc_prefetch_enable(info->gpmc_cs, |
| 538 | PREFETCH_FIFOTHRESHOLD_MAX/2, 0x0, len, 0x0); |
Sukumar Ghorai | 4e07037 | 2011-01-28 15:42:06 +0530 | [diff] [blame] | 539 | if (ret) |
| 540 | /* PFPW engine is busy, use cpu copy method */ |
| 541 | goto out_copy; |
| 542 | |
| 543 | info->buf_len = len; |
| 544 | /* enable irq */ |
| 545 | gpmc_cs_configure(info->gpmc_cs, GPMC_ENABLE_IRQ, |
| 546 | (GPMC_IRQ_FIFOEVENTENABLE | GPMC_IRQ_COUNT_EVENT)); |
| 547 | |
| 548 | /* waiting for read to complete */ |
| 549 | wait_for_completion(&info->comp); |
| 550 | |
| 551 | /* disable and stop the PFPW engine */ |
| 552 | gpmc_prefetch_reset(info->gpmc_cs); |
| 553 | return; |
| 554 | |
| 555 | out_copy: |
| 556 | if (info->nand.options & NAND_BUSWIDTH_16) |
| 557 | omap_read_buf16(mtd, buf, len); |
| 558 | else |
| 559 | omap_read_buf8(mtd, buf, len); |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * omap_write_buf_irq_pref - write buffer to NAND controller |
| 564 | * @mtd: MTD device structure |
| 565 | * @buf: data buffer |
| 566 | * @len: number of bytes to write |
| 567 | */ |
| 568 | static void omap_write_buf_irq_pref(struct mtd_info *mtd, |
| 569 | const u_char *buf, int len) |
| 570 | { |
| 571 | struct omap_nand_info *info = container_of(mtd, |
| 572 | struct omap_nand_info, mtd); |
| 573 | int ret = 0; |
| 574 | unsigned long tim, limit; |
| 575 | |
| 576 | if (len <= mtd->oobsize) { |
| 577 | omap_write_buf_pref(mtd, buf, len); |
| 578 | return; |
| 579 | } |
| 580 | |
| 581 | info->iomode = OMAP_NAND_IO_WRITE; |
| 582 | info->buf = (u_char *) buf; |
| 583 | init_completion(&info->comp); |
| 584 | |
Sukumar Ghorai | 317379a | 2011-01-28 15:42:07 +0530 | [diff] [blame] | 585 | /* configure and start prefetch transfer : size=24 */ |
| 586 | ret = gpmc_prefetch_enable(info->gpmc_cs, |
| 587 | (PREFETCH_FIFOTHRESHOLD_MAX * 3) / 8, 0x0, len, 0x1); |
Sukumar Ghorai | 4e07037 | 2011-01-28 15:42:06 +0530 | [diff] [blame] | 588 | if (ret) |
| 589 | /* PFPW engine is busy, use cpu copy method */ |
| 590 | goto out_copy; |
| 591 | |
| 592 | info->buf_len = len; |
| 593 | /* enable irq */ |
| 594 | gpmc_cs_configure(info->gpmc_cs, GPMC_ENABLE_IRQ, |
| 595 | (GPMC_IRQ_FIFOEVENTENABLE | GPMC_IRQ_COUNT_EVENT)); |
| 596 | |
| 597 | /* waiting for write to complete */ |
| 598 | wait_for_completion(&info->comp); |
| 599 | /* wait for data to flushed-out before reset the prefetch */ |
| 600 | tim = 0; |
| 601 | limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS)); |
| 602 | while (gpmc_read_status(GPMC_PREFETCH_COUNT) && (tim++ < limit)) |
| 603 | cpu_relax(); |
| 604 | |
| 605 | /* disable and stop the PFPW engine */ |
| 606 | gpmc_prefetch_reset(info->gpmc_cs); |
| 607 | return; |
| 608 | |
| 609 | out_copy: |
| 610 | if (info->nand.options & NAND_BUSWIDTH_16) |
| 611 | omap_write_buf16(mtd, buf, len); |
| 612 | else |
| 613 | omap_write_buf8(mtd, buf, len); |
| 614 | } |
| 615 | |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 616 | /** |
| 617 | * omap_verify_buf - Verify chip data against buffer |
| 618 | * @mtd: MTD device structure |
| 619 | * @buf: buffer containing the data to compare |
| 620 | * @len: number of bytes to compare |
| 621 | */ |
| 622 | static int omap_verify_buf(struct mtd_info *mtd, const u_char * buf, int len) |
| 623 | { |
| 624 | struct omap_nand_info *info = container_of(mtd, struct omap_nand_info, |
| 625 | mtd); |
| 626 | u16 *p = (u16 *) buf; |
| 627 | |
| 628 | len >>= 1; |
| 629 | while (len--) { |
| 630 | if (*p++ != cpu_to_le16(readw(info->nand.IO_ADDR_R))) |
| 631 | return -EFAULT; |
| 632 | } |
| 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 637 | /** |
| 638 | * gen_true_ecc - This function will generate true ECC value |
| 639 | * @ecc_buf: buffer to store ecc code |
| 640 | * |
| 641 | * This generated true ECC value can be used when correcting |
| 642 | * data read from NAND flash memory core |
| 643 | */ |
| 644 | static void gen_true_ecc(u8 *ecc_buf) |
| 645 | { |
| 646 | u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) | |
| 647 | ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8); |
| 648 | |
| 649 | ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) | |
| 650 | P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp)); |
| 651 | ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) | |
| 652 | P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp)); |
| 653 | ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) | |
| 654 | P1e(tmp) | P2048o(tmp) | P2048e(tmp)); |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data |
| 659 | * @ecc_data1: ecc code from nand spare area |
| 660 | * @ecc_data2: ecc code from hardware register obtained from hardware ecc |
| 661 | * @page_data: page data |
| 662 | * |
| 663 | * This function compares two ECC's and indicates if there is an error. |
| 664 | * If the error can be corrected it will be corrected to the buffer. |
John Ogness | 74f1b72 | 2011-02-28 13:12:46 +0100 | [diff] [blame] | 665 | * If there is no error, %0 is returned. If there is an error but it |
| 666 | * was corrected, %1 is returned. Otherwise, %-1 is returned. |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 667 | */ |
| 668 | static int omap_compare_ecc(u8 *ecc_data1, /* read from NAND memory */ |
| 669 | u8 *ecc_data2, /* read from register */ |
| 670 | u8 *page_data) |
| 671 | { |
| 672 | uint i; |
| 673 | u8 tmp0_bit[8], tmp1_bit[8], tmp2_bit[8]; |
| 674 | u8 comp0_bit[8], comp1_bit[8], comp2_bit[8]; |
| 675 | u8 ecc_bit[24]; |
| 676 | u8 ecc_sum = 0; |
| 677 | u8 find_bit = 0; |
| 678 | uint find_byte = 0; |
| 679 | int isEccFF; |
| 680 | |
| 681 | isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF); |
| 682 | |
| 683 | gen_true_ecc(ecc_data1); |
| 684 | gen_true_ecc(ecc_data2); |
| 685 | |
| 686 | for (i = 0; i <= 2; i++) { |
| 687 | *(ecc_data1 + i) = ~(*(ecc_data1 + i)); |
| 688 | *(ecc_data2 + i) = ~(*(ecc_data2 + i)); |
| 689 | } |
| 690 | |
| 691 | for (i = 0; i < 8; i++) { |
| 692 | tmp0_bit[i] = *ecc_data1 % 2; |
| 693 | *ecc_data1 = *ecc_data1 / 2; |
| 694 | } |
| 695 | |
| 696 | for (i = 0; i < 8; i++) { |
| 697 | tmp1_bit[i] = *(ecc_data1 + 1) % 2; |
| 698 | *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2; |
| 699 | } |
| 700 | |
| 701 | for (i = 0; i < 8; i++) { |
| 702 | tmp2_bit[i] = *(ecc_data1 + 2) % 2; |
| 703 | *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2; |
| 704 | } |
| 705 | |
| 706 | for (i = 0; i < 8; i++) { |
| 707 | comp0_bit[i] = *ecc_data2 % 2; |
| 708 | *ecc_data2 = *ecc_data2 / 2; |
| 709 | } |
| 710 | |
| 711 | for (i = 0; i < 8; i++) { |
| 712 | comp1_bit[i] = *(ecc_data2 + 1) % 2; |
| 713 | *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2; |
| 714 | } |
| 715 | |
| 716 | for (i = 0; i < 8; i++) { |
| 717 | comp2_bit[i] = *(ecc_data2 + 2) % 2; |
| 718 | *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2; |
| 719 | } |
| 720 | |
| 721 | for (i = 0; i < 6; i++) |
| 722 | ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2]; |
| 723 | |
| 724 | for (i = 0; i < 8; i++) |
| 725 | ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i]; |
| 726 | |
| 727 | for (i = 0; i < 8; i++) |
| 728 | ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i]; |
| 729 | |
| 730 | ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0]; |
| 731 | ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1]; |
| 732 | |
| 733 | for (i = 0; i < 24; i++) |
| 734 | ecc_sum += ecc_bit[i]; |
| 735 | |
| 736 | switch (ecc_sum) { |
| 737 | case 0: |
| 738 | /* Not reached because this function is not called if |
| 739 | * ECC values are equal |
| 740 | */ |
| 741 | return 0; |
| 742 | |
| 743 | case 1: |
| 744 | /* Uncorrectable error */ |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 745 | pr_debug("ECC UNCORRECTED_ERROR 1\n"); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 746 | return -1; |
| 747 | |
| 748 | case 11: |
| 749 | /* UN-Correctable error */ |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 750 | pr_debug("ECC UNCORRECTED_ERROR B\n"); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 751 | return -1; |
| 752 | |
| 753 | case 12: |
| 754 | /* Correctable error */ |
| 755 | find_byte = (ecc_bit[23] << 8) + |
| 756 | (ecc_bit[21] << 7) + |
| 757 | (ecc_bit[19] << 6) + |
| 758 | (ecc_bit[17] << 5) + |
| 759 | (ecc_bit[15] << 4) + |
| 760 | (ecc_bit[13] << 3) + |
| 761 | (ecc_bit[11] << 2) + |
| 762 | (ecc_bit[9] << 1) + |
| 763 | ecc_bit[7]; |
| 764 | |
| 765 | find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1]; |
| 766 | |
Brian Norris | 0a32a10 | 2011-07-19 10:06:10 -0700 | [diff] [blame] | 767 | pr_debug("Correcting single bit ECC error at offset: " |
| 768 | "%d, bit: %d\n", find_byte, find_bit); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 769 | |
| 770 | page_data[find_byte] ^= (1 << find_bit); |
| 771 | |
John Ogness | 74f1b72 | 2011-02-28 13:12:46 +0100 | [diff] [blame] | 772 | return 1; |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 773 | default: |
| 774 | if (isEccFF) { |
| 775 | if (ecc_data2[0] == 0 && |
| 776 | ecc_data2[1] == 0 && |
| 777 | ecc_data2[2] == 0) |
| 778 | return 0; |
| 779 | } |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 780 | pr_debug("UNCORRECTED_ERROR default\n"); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 781 | return -1; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | /** |
| 786 | * omap_correct_data - Compares the ECC read with HW generated ECC |
| 787 | * @mtd: MTD device structure |
| 788 | * @dat: page data |
| 789 | * @read_ecc: ecc read from nand flash |
| 790 | * @calc_ecc: ecc read from HW ECC registers |
| 791 | * |
| 792 | * Compares the ecc read from nand spare area with ECC registers values |
John Ogness | 74f1b72 | 2011-02-28 13:12:46 +0100 | [diff] [blame] | 793 | * and if ECC's mismatched, it will call 'omap_compare_ecc' for error |
| 794 | * detection and correction. If there are no errors, %0 is returned. If |
| 795 | * there were errors and all of the errors were corrected, the number of |
| 796 | * corrected errors is returned. If uncorrectable errors exist, %-1 is |
| 797 | * returned. |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 798 | */ |
| 799 | static int omap_correct_data(struct mtd_info *mtd, u_char *dat, |
| 800 | u_char *read_ecc, u_char *calc_ecc) |
| 801 | { |
| 802 | struct omap_nand_info *info = container_of(mtd, struct omap_nand_info, |
| 803 | mtd); |
| 804 | int blockCnt = 0, i = 0, ret = 0; |
John Ogness | 74f1b72 | 2011-02-28 13:12:46 +0100 | [diff] [blame] | 805 | int stat = 0; |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 806 | |
| 807 | /* Ex NAND_ECC_HW12_2048 */ |
| 808 | if ((info->nand.ecc.mode == NAND_ECC_HW) && |
| 809 | (info->nand.ecc.size == 2048)) |
| 810 | blockCnt = 4; |
| 811 | else |
| 812 | blockCnt = 1; |
| 813 | |
| 814 | for (i = 0; i < blockCnt; i++) { |
| 815 | if (memcmp(read_ecc, calc_ecc, 3) != 0) { |
| 816 | ret = omap_compare_ecc(read_ecc, calc_ecc, dat); |
| 817 | if (ret < 0) |
| 818 | return ret; |
John Ogness | 74f1b72 | 2011-02-28 13:12:46 +0100 | [diff] [blame] | 819 | /* keep track of the number of corrected errors */ |
| 820 | stat += ret; |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 821 | } |
| 822 | read_ecc += 3; |
| 823 | calc_ecc += 3; |
| 824 | dat += 512; |
| 825 | } |
John Ogness | 74f1b72 | 2011-02-28 13:12:46 +0100 | [diff] [blame] | 826 | return stat; |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | /** |
| 830 | * omap_calcuate_ecc - Generate non-inverted ECC bytes. |
| 831 | * @mtd: MTD device structure |
| 832 | * @dat: The pointer to data on which ecc is computed |
| 833 | * @ecc_code: The ecc_code buffer |
| 834 | * |
| 835 | * Using noninverted ECC can be considered ugly since writing a blank |
| 836 | * page ie. padding will clear the ECC bytes. This is no problem as long |
| 837 | * nobody is trying to write data on the seemingly unused page. Reading |
| 838 | * an erased page will produce an ECC mismatch between generated and read |
| 839 | * ECC bytes that has to be dealt with separately. |
| 840 | */ |
| 841 | static int omap_calculate_ecc(struct mtd_info *mtd, const u_char *dat, |
| 842 | u_char *ecc_code) |
| 843 | { |
| 844 | struct omap_nand_info *info = container_of(mtd, struct omap_nand_info, |
| 845 | mtd); |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 846 | return gpmc_calculate_ecc(info->gpmc_cs, dat, ecc_code); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | /** |
| 850 | * omap_enable_hwecc - This function enables the hardware ecc functionality |
| 851 | * @mtd: MTD device structure |
| 852 | * @mode: Read/Write mode |
| 853 | */ |
| 854 | static void omap_enable_hwecc(struct mtd_info *mtd, int mode) |
| 855 | { |
| 856 | struct omap_nand_info *info = container_of(mtd, struct omap_nand_info, |
| 857 | mtd); |
| 858 | struct nand_chip *chip = mtd->priv; |
| 859 | unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0; |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 860 | |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 861 | gpmc_enable_hwecc(info->gpmc_cs, mode, dev_width, info->nand.ecc.size); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 862 | } |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 863 | |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 864 | /** |
| 865 | * omap_wait - wait until the command is done |
| 866 | * @mtd: MTD device structure |
| 867 | * @chip: NAND Chip structure |
| 868 | * |
| 869 | * Wait function is called during Program and erase operations and |
| 870 | * the way it is called from MTD layer, we should wait till the NAND |
| 871 | * chip is ready after the programming/erase operation has completed. |
| 872 | * |
| 873 | * Erase can take up to 400ms and program up to 20ms according to |
| 874 | * general NAND and SmartMedia specs |
| 875 | */ |
| 876 | static int omap_wait(struct mtd_info *mtd, struct nand_chip *chip) |
| 877 | { |
| 878 | struct nand_chip *this = mtd->priv; |
| 879 | struct omap_nand_info *info = container_of(mtd, struct omap_nand_info, |
| 880 | mtd); |
| 881 | unsigned long timeo = jiffies; |
Ivan Djelic | a9c465f | 2012-04-17 13:11:53 +0200 | [diff] [blame] | 882 | int status, state = this->state; |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 883 | |
| 884 | if (state == FL_ERASING) |
| 885 | timeo += (HZ * 400) / 1000; |
| 886 | else |
| 887 | timeo += (HZ * 20) / 1000; |
| 888 | |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 889 | gpmc_nand_write(info->gpmc_cs, |
| 890 | GPMC_NAND_COMMAND, (NAND_CMD_STATUS & 0xFF)); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 891 | while (time_before(jiffies, timeo)) { |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 892 | status = gpmc_nand_read(info->gpmc_cs, GPMC_NAND_DATA); |
vimal singh | c276aca | 2009-06-27 11:07:06 +0530 | [diff] [blame] | 893 | if (status & NAND_STATUS_READY) |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 894 | break; |
vimal singh | c276aca | 2009-06-27 11:07:06 +0530 | [diff] [blame] | 895 | cond_resched(); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 896 | } |
Ivan Djelic | a9c465f | 2012-04-17 13:11:53 +0200 | [diff] [blame] | 897 | |
| 898 | status = gpmc_nand_read(info->gpmc_cs, GPMC_NAND_DATA); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 899 | return status; |
| 900 | } |
| 901 | |
| 902 | /** |
| 903 | * omap_dev_ready - calls the platform specific dev_ready function |
| 904 | * @mtd: MTD device structure |
| 905 | */ |
| 906 | static int omap_dev_ready(struct mtd_info *mtd) |
| 907 | { |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 908 | unsigned int val = 0; |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 909 | struct omap_nand_info *info = container_of(mtd, struct omap_nand_info, |
| 910 | mtd); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 911 | |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 912 | val = gpmc_read_status(GPMC_GET_IRQ_STATUS); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 913 | if ((val & 0x100) == 0x100) { |
| 914 | /* Clear IRQ Interrupt */ |
| 915 | val |= 0x100; |
| 916 | val &= ~(0x0); |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 917 | gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, val); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 918 | } else { |
| 919 | unsigned int cnt = 0; |
| 920 | while (cnt++ < 0x1FF) { |
| 921 | if ((val & 0x100) == 0x100) |
| 922 | return 0; |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 923 | val = gpmc_read_status(GPMC_GET_IRQ_STATUS); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
| 927 | return 1; |
| 928 | } |
| 929 | |
Ivan Djelic | 0e618ef | 2012-04-30 12:17:18 +0200 | [diff] [blame] | 930 | #ifdef CONFIG_MTD_NAND_OMAP_BCH |
| 931 | |
| 932 | /** |
| 933 | * omap3_enable_hwecc_bch - Program OMAP3 GPMC to perform BCH ECC correction |
| 934 | * @mtd: MTD device structure |
| 935 | * @mode: Read/Write mode |
| 936 | */ |
| 937 | static void omap3_enable_hwecc_bch(struct mtd_info *mtd, int mode) |
| 938 | { |
| 939 | int nerrors; |
| 940 | unsigned int dev_width; |
| 941 | struct omap_nand_info *info = container_of(mtd, struct omap_nand_info, |
| 942 | mtd); |
| 943 | struct nand_chip *chip = mtd->priv; |
| 944 | |
| 945 | nerrors = (info->nand.ecc.bytes == 13) ? 8 : 4; |
| 946 | dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0; |
| 947 | /* |
| 948 | * Program GPMC to perform correction on one 512-byte sector at a time. |
| 949 | * Using 4 sectors at a time (i.e. ecc.size = 2048) is also possible and |
| 950 | * gives a slight (5%) performance gain (but requires additional code). |
| 951 | */ |
| 952 | (void)gpmc_enable_hwecc_bch(info->gpmc_cs, mode, dev_width, 1, nerrors); |
| 953 | } |
| 954 | |
| 955 | /** |
| 956 | * omap3_calculate_ecc_bch4 - Generate 7 bytes of ECC bytes |
| 957 | * @mtd: MTD device structure |
| 958 | * @dat: The pointer to data on which ecc is computed |
| 959 | * @ecc_code: The ecc_code buffer |
| 960 | */ |
| 961 | static int omap3_calculate_ecc_bch4(struct mtd_info *mtd, const u_char *dat, |
| 962 | u_char *ecc_code) |
| 963 | { |
| 964 | struct omap_nand_info *info = container_of(mtd, struct omap_nand_info, |
| 965 | mtd); |
| 966 | return gpmc_calculate_ecc_bch4(info->gpmc_cs, dat, ecc_code); |
| 967 | } |
| 968 | |
| 969 | /** |
| 970 | * omap3_calculate_ecc_bch8 - Generate 13 bytes of ECC bytes |
| 971 | * @mtd: MTD device structure |
| 972 | * @dat: The pointer to data on which ecc is computed |
| 973 | * @ecc_code: The ecc_code buffer |
| 974 | */ |
| 975 | static int omap3_calculate_ecc_bch8(struct mtd_info *mtd, const u_char *dat, |
| 976 | u_char *ecc_code) |
| 977 | { |
| 978 | struct omap_nand_info *info = container_of(mtd, struct omap_nand_info, |
| 979 | mtd); |
| 980 | return gpmc_calculate_ecc_bch8(info->gpmc_cs, dat, ecc_code); |
| 981 | } |
| 982 | |
| 983 | /** |
| 984 | * omap3_correct_data_bch - Decode received data and correct errors |
| 985 | * @mtd: MTD device structure |
| 986 | * @data: page data |
| 987 | * @read_ecc: ecc read from nand flash |
| 988 | * @calc_ecc: ecc read from HW ECC registers |
| 989 | */ |
| 990 | static int omap3_correct_data_bch(struct mtd_info *mtd, u_char *data, |
| 991 | u_char *read_ecc, u_char *calc_ecc) |
| 992 | { |
| 993 | int i, count; |
| 994 | /* cannot correct more than 8 errors */ |
| 995 | unsigned int errloc[8]; |
| 996 | struct omap_nand_info *info = container_of(mtd, struct omap_nand_info, |
| 997 | mtd); |
| 998 | |
| 999 | count = decode_bch(info->bch, NULL, 512, read_ecc, calc_ecc, NULL, |
| 1000 | errloc); |
| 1001 | if (count > 0) { |
| 1002 | /* correct errors */ |
| 1003 | for (i = 0; i < count; i++) { |
| 1004 | /* correct data only, not ecc bytes */ |
| 1005 | if (errloc[i] < 8*512) |
| 1006 | data[errloc[i]/8] ^= 1 << (errloc[i] & 7); |
| 1007 | pr_debug("corrected bitflip %u\n", errloc[i]); |
| 1008 | } |
| 1009 | } else if (count < 0) { |
| 1010 | pr_err("ecc unrecoverable error\n"); |
| 1011 | } |
| 1012 | return count; |
| 1013 | } |
| 1014 | |
| 1015 | /** |
| 1016 | * omap3_free_bch - Release BCH ecc resources |
| 1017 | * @mtd: MTD device structure |
| 1018 | */ |
| 1019 | static void omap3_free_bch(struct mtd_info *mtd) |
| 1020 | { |
| 1021 | struct omap_nand_info *info = container_of(mtd, struct omap_nand_info, |
| 1022 | mtd); |
| 1023 | if (info->bch) { |
| 1024 | free_bch(info->bch); |
| 1025 | info->bch = NULL; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | /** |
| 1030 | * omap3_init_bch - Initialize BCH ECC |
| 1031 | * @mtd: MTD device structure |
| 1032 | * @ecc_opt: OMAP ECC mode (OMAP_ECC_BCH4_CODE_HW or OMAP_ECC_BCH8_CODE_HW) |
| 1033 | */ |
| 1034 | static int omap3_init_bch(struct mtd_info *mtd, int ecc_opt) |
| 1035 | { |
| 1036 | int ret, max_errors; |
| 1037 | struct omap_nand_info *info = container_of(mtd, struct omap_nand_info, |
| 1038 | mtd); |
| 1039 | #ifdef CONFIG_MTD_NAND_OMAP_BCH8 |
| 1040 | const int hw_errors = 8; |
| 1041 | #else |
| 1042 | const int hw_errors = 4; |
| 1043 | #endif |
| 1044 | info->bch = NULL; |
| 1045 | |
| 1046 | max_errors = (ecc_opt == OMAP_ECC_BCH8_CODE_HW) ? 8 : 4; |
| 1047 | if (max_errors != hw_errors) { |
| 1048 | pr_err("cannot configure %d-bit BCH ecc, only %d-bit supported", |
| 1049 | max_errors, hw_errors); |
| 1050 | goto fail; |
| 1051 | } |
| 1052 | |
| 1053 | /* initialize GPMC BCH engine */ |
| 1054 | ret = gpmc_init_hwecc_bch(info->gpmc_cs, 1, max_errors); |
| 1055 | if (ret) |
| 1056 | goto fail; |
| 1057 | |
| 1058 | /* software bch library is only used to detect and locate errors */ |
| 1059 | info->bch = init_bch(13, max_errors, 0x201b /* hw polynomial */); |
| 1060 | if (!info->bch) |
| 1061 | goto fail; |
| 1062 | |
| 1063 | info->nand.ecc.size = 512; |
| 1064 | info->nand.ecc.hwctl = omap3_enable_hwecc_bch; |
| 1065 | info->nand.ecc.correct = omap3_correct_data_bch; |
| 1066 | info->nand.ecc.mode = NAND_ECC_HW; |
| 1067 | |
| 1068 | /* |
| 1069 | * The number of corrected errors in an ecc block that will trigger |
| 1070 | * block scrubbing defaults to the ecc strength (4 or 8). |
| 1071 | * Set mtd->bitflip_threshold here to define a custom threshold. |
| 1072 | */ |
| 1073 | |
| 1074 | if (max_errors == 8) { |
| 1075 | info->nand.ecc.strength = 8; |
| 1076 | info->nand.ecc.bytes = 13; |
| 1077 | info->nand.ecc.calculate = omap3_calculate_ecc_bch8; |
| 1078 | } else { |
| 1079 | info->nand.ecc.strength = 4; |
| 1080 | info->nand.ecc.bytes = 7; |
| 1081 | info->nand.ecc.calculate = omap3_calculate_ecc_bch4; |
| 1082 | } |
| 1083 | |
| 1084 | pr_info("enabling NAND BCH ecc with %d-bit correction\n", max_errors); |
| 1085 | return 0; |
| 1086 | fail: |
| 1087 | omap3_free_bch(mtd); |
| 1088 | return -1; |
| 1089 | } |
| 1090 | |
| 1091 | /** |
| 1092 | * omap3_init_bch_tail - Build an oob layout for BCH ECC correction. |
| 1093 | * @mtd: MTD device structure |
| 1094 | */ |
| 1095 | static int omap3_init_bch_tail(struct mtd_info *mtd) |
| 1096 | { |
| 1097 | int i, steps; |
| 1098 | struct omap_nand_info *info = container_of(mtd, struct omap_nand_info, |
| 1099 | mtd); |
| 1100 | struct nand_ecclayout *layout = &info->ecclayout; |
| 1101 | |
| 1102 | /* build oob layout */ |
| 1103 | steps = mtd->writesize/info->nand.ecc.size; |
| 1104 | layout->eccbytes = steps*info->nand.ecc.bytes; |
| 1105 | |
| 1106 | /* do not bother creating special oob layouts for small page devices */ |
| 1107 | if (mtd->oobsize < 64) { |
| 1108 | pr_err("BCH ecc is not supported on small page devices\n"); |
| 1109 | goto fail; |
| 1110 | } |
| 1111 | |
| 1112 | /* reserve 2 bytes for bad block marker */ |
| 1113 | if (layout->eccbytes+2 > mtd->oobsize) { |
| 1114 | pr_err("no oob layout available for oobsize %d eccbytes %u\n", |
| 1115 | mtd->oobsize, layout->eccbytes); |
| 1116 | goto fail; |
| 1117 | } |
| 1118 | |
| 1119 | /* put ecc bytes at oob tail */ |
| 1120 | for (i = 0; i < layout->eccbytes; i++) |
| 1121 | layout->eccpos[i] = mtd->oobsize-layout->eccbytes+i; |
| 1122 | |
| 1123 | layout->oobfree[0].offset = 2; |
| 1124 | layout->oobfree[0].length = mtd->oobsize-2-layout->eccbytes; |
| 1125 | info->nand.ecc.layout = layout; |
| 1126 | |
| 1127 | if (!(info->nand.options & NAND_BUSWIDTH_16)) |
| 1128 | info->nand.badblock_pattern = &bb_descrip_flashbased; |
| 1129 | return 0; |
| 1130 | fail: |
| 1131 | omap3_free_bch(mtd); |
| 1132 | return -1; |
| 1133 | } |
| 1134 | |
| 1135 | #else |
| 1136 | static int omap3_init_bch(struct mtd_info *mtd, int ecc_opt) |
| 1137 | { |
| 1138 | pr_err("CONFIG_MTD_NAND_OMAP_BCH is not enabled\n"); |
| 1139 | return -1; |
| 1140 | } |
| 1141 | static int omap3_init_bch_tail(struct mtd_info *mtd) |
| 1142 | { |
| 1143 | return -1; |
| 1144 | } |
| 1145 | static void omap3_free_bch(struct mtd_info *mtd) |
| 1146 | { |
| 1147 | } |
| 1148 | #endif /* CONFIG_MTD_NAND_OMAP_BCH */ |
| 1149 | |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1150 | static int __devinit omap_nand_probe(struct platform_device *pdev) |
| 1151 | { |
| 1152 | struct omap_nand_info *info; |
| 1153 | struct omap_nand_platform_data *pdata; |
| 1154 | int err; |
Sukumar Ghorai | f040d33 | 2011-01-28 15:42:09 +0530 | [diff] [blame] | 1155 | int i, offset; |
Russell King | 763e735 | 2012-04-25 00:16:00 +0100 | [diff] [blame] | 1156 | dma_cap_mask_t mask; |
| 1157 | unsigned sig; |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1158 | |
| 1159 | pdata = pdev->dev.platform_data; |
| 1160 | if (pdata == NULL) { |
| 1161 | dev_err(&pdev->dev, "platform data missing\n"); |
| 1162 | return -ENODEV; |
| 1163 | } |
| 1164 | |
| 1165 | info = kzalloc(sizeof(struct omap_nand_info), GFP_KERNEL); |
| 1166 | if (!info) |
| 1167 | return -ENOMEM; |
| 1168 | |
| 1169 | platform_set_drvdata(pdev, info); |
| 1170 | |
| 1171 | spin_lock_init(&info->controller.lock); |
| 1172 | init_waitqueue_head(&info->controller.wq); |
| 1173 | |
| 1174 | info->pdev = pdev; |
| 1175 | |
| 1176 | info->gpmc_cs = pdata->cs; |
Vimal Singh | 2f70a1e | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 1177 | info->phys_base = pdata->phys_base; |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1178 | |
| 1179 | info->mtd.priv = &info->nand; |
| 1180 | info->mtd.name = dev_name(&pdev->dev); |
| 1181 | info->mtd.owner = THIS_MODULE; |
| 1182 | |
Sukumar Ghorai | d5ce2b6 | 2011-01-28 15:42:03 +0530 | [diff] [blame] | 1183 | info->nand.options = pdata->devsize; |
Vimal Singh | 2f70a1e | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 1184 | info->nand.options |= NAND_SKIP_BBTSCAN; |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1185 | |
| 1186 | /* NAND write protect off */ |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 1187 | gpmc_cs_configure(info->gpmc_cs, GPMC_CONFIG_WP, 0); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1188 | |
| 1189 | if (!request_mem_region(info->phys_base, NAND_IO_SIZE, |
| 1190 | pdev->dev.driver->name)) { |
| 1191 | err = -EBUSY; |
Vimal Singh | 2f70a1e | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 1192 | goto out_free_info; |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | info->nand.IO_ADDR_R = ioremap(info->phys_base, NAND_IO_SIZE); |
| 1196 | if (!info->nand.IO_ADDR_R) { |
| 1197 | err = -ENOMEM; |
| 1198 | goto out_release_mem_region; |
| 1199 | } |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 1200 | |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1201 | info->nand.controller = &info->controller; |
| 1202 | |
| 1203 | info->nand.IO_ADDR_W = info->nand.IO_ADDR_R; |
| 1204 | info->nand.cmd_ctrl = omap_hwcontrol; |
| 1205 | |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1206 | /* |
| 1207 | * If RDY/BSY line is connected to OMAP then use the omap ready |
Peter Meerwald | 4cacbe2 | 2012-07-19 13:21:04 +0200 | [diff] [blame^] | 1208 | * function and the generic nand_wait function which reads the status |
| 1209 | * register after monitoring the RDY/BSY line. Otherwise use a standard |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1210 | * chip delay which is slightly more than tR (AC Timing) of the NAND |
| 1211 | * device and read status register until you get a failure or success |
| 1212 | */ |
| 1213 | if (pdata->dev_ready) { |
| 1214 | info->nand.dev_ready = omap_dev_ready; |
| 1215 | info->nand.chip_delay = 0; |
| 1216 | } else { |
| 1217 | info->nand.waitfunc = omap_wait; |
| 1218 | info->nand.chip_delay = 50; |
| 1219 | } |
| 1220 | |
Sukumar Ghorai | 1b0b323c | 2011-01-28 15:42:04 +0530 | [diff] [blame] | 1221 | switch (pdata->xfer_type) { |
| 1222 | case NAND_OMAP_PREFETCH_POLLED: |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 1223 | info->nand.read_buf = omap_read_buf_pref; |
| 1224 | info->nand.write_buf = omap_write_buf_pref; |
Sukumar Ghorai | 1b0b323c | 2011-01-28 15:42:04 +0530 | [diff] [blame] | 1225 | break; |
vimal singh | dfe3289 | 2009-07-13 16:29:16 +0530 | [diff] [blame] | 1226 | |
Sukumar Ghorai | 1b0b323c | 2011-01-28 15:42:04 +0530 | [diff] [blame] | 1227 | case NAND_OMAP_POLLED: |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 1228 | if (info->nand.options & NAND_BUSWIDTH_16) { |
| 1229 | info->nand.read_buf = omap_read_buf16; |
| 1230 | info->nand.write_buf = omap_write_buf16; |
| 1231 | } else { |
| 1232 | info->nand.read_buf = omap_read_buf8; |
| 1233 | info->nand.write_buf = omap_write_buf8; |
| 1234 | } |
Sukumar Ghorai | 1b0b323c | 2011-01-28 15:42:04 +0530 | [diff] [blame] | 1235 | break; |
| 1236 | |
| 1237 | case NAND_OMAP_PREFETCH_DMA: |
Russell King | 763e735 | 2012-04-25 00:16:00 +0100 | [diff] [blame] | 1238 | dma_cap_zero(mask); |
| 1239 | dma_cap_set(DMA_SLAVE, mask); |
| 1240 | sig = OMAP24XX_DMA_GPMC; |
| 1241 | info->dma = dma_request_channel(mask, omap_dma_filter_fn, &sig); |
| 1242 | if (!info->dma) { |
Russell King | 2df41d0 | 2012-04-25 00:19:39 +0100 | [diff] [blame] | 1243 | dev_err(&pdev->dev, "DMA engine request failed\n"); |
| 1244 | err = -ENXIO; |
| 1245 | goto out_release_mem_region; |
Russell King | 763e735 | 2012-04-25 00:16:00 +0100 | [diff] [blame] | 1246 | } else { |
| 1247 | struct dma_slave_config cfg; |
Russell King | 763e735 | 2012-04-25 00:16:00 +0100 | [diff] [blame] | 1248 | |
| 1249 | memset(&cfg, 0, sizeof(cfg)); |
| 1250 | cfg.src_addr = info->phys_base; |
| 1251 | cfg.dst_addr = info->phys_base; |
| 1252 | cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 1253 | cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 1254 | cfg.src_maxburst = 16; |
| 1255 | cfg.dst_maxburst = 16; |
Arnd Bergmann | d680e2c | 2012-08-04 11:05:25 +0000 | [diff] [blame] | 1256 | err = dmaengine_slave_config(info->dma, &cfg); |
| 1257 | if (err) { |
Russell King | 763e735 | 2012-04-25 00:16:00 +0100 | [diff] [blame] | 1258 | dev_err(&pdev->dev, "DMA engine slave config failed: %d\n", |
Arnd Bergmann | d680e2c | 2012-08-04 11:05:25 +0000 | [diff] [blame] | 1259 | err); |
Russell King | 763e735 | 2012-04-25 00:16:00 +0100 | [diff] [blame] | 1260 | goto out_release_mem_region; |
| 1261 | } |
| 1262 | info->nand.read_buf = omap_read_buf_dma_pref; |
| 1263 | info->nand.write_buf = omap_write_buf_dma_pref; |
Sukumar Ghorai | 1b0b323c | 2011-01-28 15:42:04 +0530 | [diff] [blame] | 1264 | } |
| 1265 | break; |
| 1266 | |
Sukumar Ghorai | 4e07037 | 2011-01-28 15:42:06 +0530 | [diff] [blame] | 1267 | case NAND_OMAP_PREFETCH_IRQ: |
| 1268 | err = request_irq(pdata->gpmc_irq, |
| 1269 | omap_nand_irq, IRQF_SHARED, "gpmc-nand", info); |
| 1270 | if (err) { |
| 1271 | dev_err(&pdev->dev, "requesting irq(%d) error:%d", |
| 1272 | pdata->gpmc_irq, err); |
| 1273 | goto out_release_mem_region; |
| 1274 | } else { |
| 1275 | info->gpmc_irq = pdata->gpmc_irq; |
| 1276 | info->nand.read_buf = omap_read_buf_irq_pref; |
| 1277 | info->nand.write_buf = omap_write_buf_irq_pref; |
| 1278 | } |
| 1279 | break; |
| 1280 | |
Sukumar Ghorai | 1b0b323c | 2011-01-28 15:42:04 +0530 | [diff] [blame] | 1281 | default: |
| 1282 | dev_err(&pdev->dev, |
| 1283 | "xfer_type(%d) not supported!\n", pdata->xfer_type); |
| 1284 | err = -EINVAL; |
| 1285 | goto out_release_mem_region; |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 1286 | } |
Sukumar Ghorai | 1b0b323c | 2011-01-28 15:42:04 +0530 | [diff] [blame] | 1287 | |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 1288 | info->nand.verify_buf = omap_verify_buf; |
| 1289 | |
Peter Meerwald | 4cacbe2 | 2012-07-19 13:21:04 +0200 | [diff] [blame^] | 1290 | /* select the ecc type */ |
Sukumar Ghorai | f3d73f3 | 2011-01-28 15:42:08 +0530 | [diff] [blame] | 1291 | if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_DEFAULT) |
| 1292 | info->nand.ecc.mode = NAND_ECC_SOFT; |
Sukumar Ghorai | f040d33 | 2011-01-28 15:42:09 +0530 | [diff] [blame] | 1293 | else if ((pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW) || |
| 1294 | (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE)) { |
Sukumar Ghorai | f3d73f3 | 2011-01-28 15:42:08 +0530 | [diff] [blame] | 1295 | info->nand.ecc.bytes = 3; |
| 1296 | info->nand.ecc.size = 512; |
Mike Dunn | 6a918ba | 2012-03-11 14:21:11 -0700 | [diff] [blame] | 1297 | info->nand.ecc.strength = 1; |
Sukumar Ghorai | f3d73f3 | 2011-01-28 15:42:08 +0530 | [diff] [blame] | 1298 | info->nand.ecc.calculate = omap_calculate_ecc; |
| 1299 | info->nand.ecc.hwctl = omap_enable_hwecc; |
| 1300 | info->nand.ecc.correct = omap_correct_data; |
| 1301 | info->nand.ecc.mode = NAND_ECC_HW; |
Ivan Djelic | 0e618ef | 2012-04-30 12:17:18 +0200 | [diff] [blame] | 1302 | } else if ((pdata->ecc_opt == OMAP_ECC_BCH4_CODE_HW) || |
| 1303 | (pdata->ecc_opt == OMAP_ECC_BCH8_CODE_HW)) { |
| 1304 | err = omap3_init_bch(&info->mtd, pdata->ecc_opt); |
| 1305 | if (err) { |
| 1306 | err = -EINVAL; |
| 1307 | goto out_release_mem_region; |
| 1308 | } |
Sukumar Ghorai | f3d73f3 | 2011-01-28 15:42:08 +0530 | [diff] [blame] | 1309 | } |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1310 | |
| 1311 | /* DIP switches on some boards change between 8 and 16 bit |
| 1312 | * bus widths for flash. Try the other width if the first try fails. |
| 1313 | */ |
Jan Weitzel | a80f1c1 | 2011-04-19 16:15:34 +0200 | [diff] [blame] | 1314 | if (nand_scan_ident(&info->mtd, 1, NULL)) { |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1315 | info->nand.options ^= NAND_BUSWIDTH_16; |
Jan Weitzel | a80f1c1 | 2011-04-19 16:15:34 +0200 | [diff] [blame] | 1316 | if (nand_scan_ident(&info->mtd, 1, NULL)) { |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1317 | err = -ENXIO; |
| 1318 | goto out_release_mem_region; |
| 1319 | } |
| 1320 | } |
| 1321 | |
Sukumar Ghorai | f040d33 | 2011-01-28 15:42:09 +0530 | [diff] [blame] | 1322 | /* rom code layout */ |
| 1323 | if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE) { |
| 1324 | |
| 1325 | if (info->nand.options & NAND_BUSWIDTH_16) |
| 1326 | offset = 2; |
| 1327 | else { |
| 1328 | offset = 1; |
| 1329 | info->nand.badblock_pattern = &bb_descrip_flashbased; |
| 1330 | } |
| 1331 | omap_oobinfo.eccbytes = 3 * (info->mtd.oobsize/16); |
| 1332 | for (i = 0; i < omap_oobinfo.eccbytes; i++) |
| 1333 | omap_oobinfo.eccpos[i] = i+offset; |
| 1334 | |
| 1335 | omap_oobinfo.oobfree->offset = offset + omap_oobinfo.eccbytes; |
| 1336 | omap_oobinfo.oobfree->length = info->mtd.oobsize - |
| 1337 | (offset + omap_oobinfo.eccbytes); |
| 1338 | |
| 1339 | info->nand.ecc.layout = &omap_oobinfo; |
Ivan Djelic | 0e618ef | 2012-04-30 12:17:18 +0200 | [diff] [blame] | 1340 | } else if ((pdata->ecc_opt == OMAP_ECC_BCH4_CODE_HW) || |
| 1341 | (pdata->ecc_opt == OMAP_ECC_BCH8_CODE_HW)) { |
| 1342 | /* build OOB layout for BCH ECC correction */ |
| 1343 | err = omap3_init_bch_tail(&info->mtd); |
| 1344 | if (err) { |
| 1345 | err = -EINVAL; |
| 1346 | goto out_release_mem_region; |
| 1347 | } |
Sukumar Ghorai | f040d33 | 2011-01-28 15:42:09 +0530 | [diff] [blame] | 1348 | } |
Sukumar Ghorai | 1b0b323c | 2011-01-28 15:42:04 +0530 | [diff] [blame] | 1349 | |
Jan Weitzel | a80f1c1 | 2011-04-19 16:15:34 +0200 | [diff] [blame] | 1350 | /* second phase scan */ |
| 1351 | if (nand_scan_tail(&info->mtd)) { |
| 1352 | err = -ENXIO; |
| 1353 | goto out_release_mem_region; |
| 1354 | } |
| 1355 | |
Artem Bityutskiy | 42d7fbe | 2012-03-09 19:24:26 +0200 | [diff] [blame] | 1356 | mtd_device_parse_register(&info->mtd, NULL, NULL, pdata->parts, |
| 1357 | pdata->nr_parts); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1358 | |
| 1359 | platform_set_drvdata(pdev, &info->mtd); |
| 1360 | |
| 1361 | return 0; |
| 1362 | |
| 1363 | out_release_mem_region: |
Russell King | 763e735 | 2012-04-25 00:16:00 +0100 | [diff] [blame] | 1364 | if (info->dma) |
| 1365 | dma_release_channel(info->dma); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1366 | release_mem_region(info->phys_base, NAND_IO_SIZE); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1367 | out_free_info: |
| 1368 | kfree(info); |
| 1369 | |
| 1370 | return err; |
| 1371 | } |
| 1372 | |
| 1373 | static int omap_nand_remove(struct platform_device *pdev) |
| 1374 | { |
| 1375 | struct mtd_info *mtd = platform_get_drvdata(pdev); |
Vimal Singh | f35b6ed | 2010-01-05 16:01:08 +0530 | [diff] [blame] | 1376 | struct omap_nand_info *info = container_of(mtd, struct omap_nand_info, |
| 1377 | mtd); |
Ivan Djelic | 0e618ef | 2012-04-30 12:17:18 +0200 | [diff] [blame] | 1378 | omap3_free_bch(&info->mtd); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1379 | |
| 1380 | platform_set_drvdata(pdev, NULL); |
Russell King | 763e735 | 2012-04-25 00:16:00 +0100 | [diff] [blame] | 1381 | if (info->dma) |
| 1382 | dma_release_channel(info->dma); |
| 1383 | |
Sukumar Ghorai | 4e07037 | 2011-01-28 15:42:06 +0530 | [diff] [blame] | 1384 | if (info->gpmc_irq) |
| 1385 | free_irq(info->gpmc_irq, info); |
| 1386 | |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1387 | /* Release NAND device, its internal structures and partitions */ |
| 1388 | nand_release(&info->mtd); |
Sukumar Ghorai | 2c01946c | 2010-07-09 09:14:45 +0000 | [diff] [blame] | 1389 | iounmap(info->nand.IO_ADDR_R); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1390 | kfree(&info->mtd); |
| 1391 | return 0; |
| 1392 | } |
| 1393 | |
| 1394 | static struct platform_driver omap_nand_driver = { |
| 1395 | .probe = omap_nand_probe, |
| 1396 | .remove = omap_nand_remove, |
| 1397 | .driver = { |
| 1398 | .name = DRIVER_NAME, |
| 1399 | .owner = THIS_MODULE, |
| 1400 | }, |
| 1401 | }; |
| 1402 | |
Axel Lin | f99640d | 2011-11-27 20:45:03 +0800 | [diff] [blame] | 1403 | module_platform_driver(omap_nand_driver); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1404 | |
Axel Lin | c804c73 | 2011-03-07 11:04:24 +0800 | [diff] [blame] | 1405 | MODULE_ALIAS("platform:" DRIVER_NAME); |
Vimal Singh | 67ce04b | 2009-05-12 13:47:03 -0700 | [diff] [blame] | 1406 | MODULE_LICENSE("GPL"); |
| 1407 | MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards"); |