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