eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1 | /* |
| 2 | * drivers/mtd/nand/pxa3xx_nand.c |
| 3 | * |
| 4 | * Copyright © 2005 Intel Corporation |
| 5 | * Copyright © 2006 Marvell International Ltd. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/dma-mapping.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/clk.h> |
| 18 | #include <linux/mtd/mtd.h> |
| 19 | #include <linux/mtd/nand.h> |
| 20 | #include <linux/mtd/partitions.h> |
David Woodhouse | a1c06ee | 2008-04-22 20:39:43 +0100 | [diff] [blame] | 21 | #include <linux/io.h> |
| 22 | #include <linux/irq.h> |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 23 | #include <asm/dma.h> |
| 24 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 25 | #include <mach/pxa-regs.h> |
| 26 | #include <mach/pxa3xx_nand.h> |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 27 | |
| 28 | #define CHIP_DELAY_TIMEOUT (2 * HZ/10) |
| 29 | |
| 30 | /* registers and bit definitions */ |
| 31 | #define NDCR (0x00) /* Control register */ |
| 32 | #define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */ |
| 33 | #define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */ |
| 34 | #define NDSR (0x14) /* Status Register */ |
| 35 | #define NDPCR (0x18) /* Page Count Register */ |
| 36 | #define NDBDR0 (0x1C) /* Bad Block Register 0 */ |
| 37 | #define NDBDR1 (0x20) /* Bad Block Register 1 */ |
| 38 | #define NDDB (0x40) /* Data Buffer */ |
| 39 | #define NDCB0 (0x48) /* Command Buffer0 */ |
| 40 | #define NDCB1 (0x4C) /* Command Buffer1 */ |
| 41 | #define NDCB2 (0x50) /* Command Buffer2 */ |
| 42 | |
| 43 | #define NDCR_SPARE_EN (0x1 << 31) |
| 44 | #define NDCR_ECC_EN (0x1 << 30) |
| 45 | #define NDCR_DMA_EN (0x1 << 29) |
| 46 | #define NDCR_ND_RUN (0x1 << 28) |
| 47 | #define NDCR_DWIDTH_C (0x1 << 27) |
| 48 | #define NDCR_DWIDTH_M (0x1 << 26) |
| 49 | #define NDCR_PAGE_SZ (0x1 << 24) |
| 50 | #define NDCR_NCSX (0x1 << 23) |
| 51 | #define NDCR_ND_MODE (0x3 << 21) |
| 52 | #define NDCR_NAND_MODE (0x0) |
| 53 | #define NDCR_CLR_PG_CNT (0x1 << 20) |
| 54 | #define NDCR_CLR_ECC (0x1 << 19) |
| 55 | #define NDCR_RD_ID_CNT_MASK (0x7 << 16) |
| 56 | #define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK) |
| 57 | |
| 58 | #define NDCR_RA_START (0x1 << 15) |
| 59 | #define NDCR_PG_PER_BLK (0x1 << 14) |
| 60 | #define NDCR_ND_ARB_EN (0x1 << 12) |
| 61 | |
| 62 | #define NDSR_MASK (0xfff) |
| 63 | #define NDSR_RDY (0x1 << 11) |
| 64 | #define NDSR_CS0_PAGED (0x1 << 10) |
| 65 | #define NDSR_CS1_PAGED (0x1 << 9) |
| 66 | #define NDSR_CS0_CMDD (0x1 << 8) |
| 67 | #define NDSR_CS1_CMDD (0x1 << 7) |
| 68 | #define NDSR_CS0_BBD (0x1 << 6) |
| 69 | #define NDSR_CS1_BBD (0x1 << 5) |
| 70 | #define NDSR_DBERR (0x1 << 4) |
| 71 | #define NDSR_SBERR (0x1 << 3) |
| 72 | #define NDSR_WRDREQ (0x1 << 2) |
| 73 | #define NDSR_RDDREQ (0x1 << 1) |
| 74 | #define NDSR_WRCMDREQ (0x1) |
| 75 | |
| 76 | #define NDCB0_AUTO_RS (0x1 << 25) |
| 77 | #define NDCB0_CSEL (0x1 << 24) |
| 78 | #define NDCB0_CMD_TYPE_MASK (0x7 << 21) |
| 79 | #define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK) |
| 80 | #define NDCB0_NC (0x1 << 20) |
| 81 | #define NDCB0_DBC (0x1 << 19) |
| 82 | #define NDCB0_ADDR_CYC_MASK (0x7 << 16) |
| 83 | #define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK) |
| 84 | #define NDCB0_CMD2_MASK (0xff << 8) |
| 85 | #define NDCB0_CMD1_MASK (0xff) |
| 86 | #define NDCB0_ADDR_CYC_SHIFT (16) |
| 87 | |
| 88 | /* dma-able I/O address for the NAND data and commands */ |
| 89 | #define NDCB0_DMA_ADDR (0x43100048) |
| 90 | #define NDDB_DMA_ADDR (0x43100040) |
| 91 | |
| 92 | /* macros for registers read/write */ |
| 93 | #define nand_writel(info, off, val) \ |
| 94 | __raw_writel((val), (info)->mmio_base + (off)) |
| 95 | |
| 96 | #define nand_readl(info, off) \ |
| 97 | __raw_readl((info)->mmio_base + (off)) |
| 98 | |
| 99 | /* error code and state */ |
| 100 | enum { |
| 101 | ERR_NONE = 0, |
| 102 | ERR_DMABUSERR = -1, |
| 103 | ERR_SENDCMD = -2, |
| 104 | ERR_DBERR = -3, |
| 105 | ERR_BBERR = -4, |
| 106 | }; |
| 107 | |
| 108 | enum { |
| 109 | STATE_READY = 0, |
| 110 | STATE_CMD_HANDLE, |
| 111 | STATE_DMA_READING, |
| 112 | STATE_DMA_WRITING, |
| 113 | STATE_DMA_DONE, |
| 114 | STATE_PIO_READING, |
| 115 | STATE_PIO_WRITING, |
| 116 | }; |
| 117 | |
| 118 | struct pxa3xx_nand_timing { |
| 119 | unsigned int tCH; /* Enable signal hold time */ |
| 120 | unsigned int tCS; /* Enable signal setup time */ |
| 121 | unsigned int tWH; /* ND_nWE high duration */ |
| 122 | unsigned int tWP; /* ND_nWE pulse time */ |
| 123 | unsigned int tRH; /* ND_nRE high duration */ |
| 124 | unsigned int tRP; /* ND_nRE pulse width */ |
| 125 | unsigned int tR; /* ND_nWE high to ND_nRE low for read */ |
| 126 | unsigned int tWHR; /* ND_nWE high to ND_nRE low for status read */ |
| 127 | unsigned int tAR; /* ND_ALE low to ND_nRE low delay */ |
| 128 | }; |
| 129 | |
| 130 | struct pxa3xx_nand_cmdset { |
| 131 | uint16_t read1; |
| 132 | uint16_t read2; |
| 133 | uint16_t program; |
| 134 | uint16_t read_status; |
| 135 | uint16_t read_id; |
| 136 | uint16_t erase; |
| 137 | uint16_t reset; |
| 138 | uint16_t lock; |
| 139 | uint16_t unlock; |
| 140 | uint16_t lock_status; |
| 141 | }; |
| 142 | |
| 143 | struct pxa3xx_nand_flash { |
| 144 | struct pxa3xx_nand_timing *timing; /* NAND Flash timing */ |
| 145 | struct pxa3xx_nand_cmdset *cmdset; |
| 146 | |
| 147 | uint32_t page_per_block;/* Pages per block (PG_PER_BLK) */ |
| 148 | uint32_t page_size; /* Page size in bytes (PAGE_SZ) */ |
| 149 | uint32_t flash_width; /* Width of Flash memory (DWIDTH_M) */ |
| 150 | uint32_t dfc_width; /* Width of flash controller(DWIDTH_C) */ |
| 151 | uint32_t num_blocks; /* Number of physical blocks in Flash */ |
| 152 | uint32_t chip_id; |
| 153 | |
| 154 | /* NOTE: these are automatically calculated, do not define */ |
| 155 | size_t oob_size; |
| 156 | size_t read_id_bytes; |
| 157 | |
| 158 | unsigned int col_addr_cycles; |
| 159 | unsigned int row_addr_cycles; |
| 160 | }; |
| 161 | |
| 162 | struct pxa3xx_nand_info { |
| 163 | struct nand_chip nand_chip; |
| 164 | |
| 165 | struct platform_device *pdev; |
| 166 | struct pxa3xx_nand_flash *flash_info; |
| 167 | |
| 168 | struct clk *clk; |
| 169 | void __iomem *mmio_base; |
| 170 | |
| 171 | unsigned int buf_start; |
| 172 | unsigned int buf_count; |
| 173 | |
| 174 | /* DMA information */ |
| 175 | int drcmr_dat; |
| 176 | int drcmr_cmd; |
| 177 | |
| 178 | unsigned char *data_buff; |
| 179 | dma_addr_t data_buff_phys; |
| 180 | size_t data_buff_size; |
| 181 | int data_dma_ch; |
| 182 | struct pxa_dma_desc *data_desc; |
| 183 | dma_addr_t data_desc_addr; |
| 184 | |
| 185 | uint32_t reg_ndcr; |
| 186 | |
| 187 | /* saved column/page_addr during CMD_SEQIN */ |
| 188 | int seqin_column; |
| 189 | int seqin_page_addr; |
| 190 | |
| 191 | /* relate to the command */ |
| 192 | unsigned int state; |
| 193 | |
| 194 | int use_ecc; /* use HW ECC ? */ |
| 195 | int use_dma; /* use DMA ? */ |
| 196 | |
| 197 | size_t data_size; /* data size in FIFO */ |
| 198 | int retcode; |
| 199 | struct completion cmd_complete; |
| 200 | |
| 201 | /* generated NDCBx register values */ |
| 202 | uint32_t ndcb0; |
| 203 | uint32_t ndcb1; |
| 204 | uint32_t ndcb2; |
| 205 | }; |
| 206 | |
| 207 | static int use_dma = 1; |
| 208 | module_param(use_dma, bool, 0444); |
| 209 | MODULE_PARM_DESC(use_dma, "enable DMA for data transfering to/from NAND HW"); |
| 210 | |
| 211 | static struct pxa3xx_nand_cmdset smallpage_cmdset = { |
| 212 | .read1 = 0x0000, |
| 213 | .read2 = 0x0050, |
| 214 | .program = 0x1080, |
| 215 | .read_status = 0x0070, |
| 216 | .read_id = 0x0090, |
| 217 | .erase = 0xD060, |
| 218 | .reset = 0x00FF, |
| 219 | .lock = 0x002A, |
| 220 | .unlock = 0x2423, |
| 221 | .lock_status = 0x007A, |
| 222 | }; |
| 223 | |
| 224 | static struct pxa3xx_nand_cmdset largepage_cmdset = { |
| 225 | .read1 = 0x3000, |
| 226 | .read2 = 0x0050, |
| 227 | .program = 0x1080, |
| 228 | .read_status = 0x0070, |
| 229 | .read_id = 0x0090, |
| 230 | .erase = 0xD060, |
| 231 | .reset = 0x00FF, |
| 232 | .lock = 0x002A, |
| 233 | .unlock = 0x2423, |
| 234 | .lock_status = 0x007A, |
| 235 | }; |
| 236 | |
| 237 | static struct pxa3xx_nand_timing samsung512MbX16_timing = { |
| 238 | .tCH = 10, |
| 239 | .tCS = 0, |
| 240 | .tWH = 20, |
| 241 | .tWP = 40, |
| 242 | .tRH = 30, |
| 243 | .tRP = 40, |
| 244 | .tR = 11123, |
| 245 | .tWHR = 110, |
| 246 | .tAR = 10, |
| 247 | }; |
| 248 | |
| 249 | static struct pxa3xx_nand_flash samsung512MbX16 = { |
| 250 | .timing = &samsung512MbX16_timing, |
| 251 | .cmdset = &smallpage_cmdset, |
| 252 | .page_per_block = 32, |
| 253 | .page_size = 512, |
| 254 | .flash_width = 16, |
| 255 | .dfc_width = 16, |
| 256 | .num_blocks = 4096, |
| 257 | .chip_id = 0x46ec, |
| 258 | }; |
| 259 | |
| 260 | static struct pxa3xx_nand_timing micron_timing = { |
| 261 | .tCH = 10, |
| 262 | .tCS = 25, |
| 263 | .tWH = 15, |
| 264 | .tWP = 25, |
| 265 | .tRH = 15, |
| 266 | .tRP = 25, |
| 267 | .tR = 25000, |
| 268 | .tWHR = 60, |
| 269 | .tAR = 10, |
| 270 | }; |
| 271 | |
| 272 | static struct pxa3xx_nand_flash micron1GbX8 = { |
| 273 | .timing = µn_timing, |
| 274 | .cmdset = &largepage_cmdset, |
| 275 | .page_per_block = 64, |
| 276 | .page_size = 2048, |
| 277 | .flash_width = 8, |
| 278 | .dfc_width = 8, |
| 279 | .num_blocks = 1024, |
| 280 | .chip_id = 0xa12c, |
| 281 | }; |
| 282 | |
| 283 | static struct pxa3xx_nand_flash micron1GbX16 = { |
| 284 | .timing = µn_timing, |
| 285 | .cmdset = &largepage_cmdset, |
| 286 | .page_per_block = 64, |
| 287 | .page_size = 2048, |
| 288 | .flash_width = 16, |
| 289 | .dfc_width = 16, |
| 290 | .num_blocks = 1024, |
| 291 | .chip_id = 0xb12c, |
| 292 | }; |
| 293 | |
Semun Lee | 4262bd2 | 2008-09-01 11:49:27 +0100 | [diff] [blame^] | 294 | static struct pxa3xx_nand_timing stm2GbX16_timing = { |
| 295 | .tCH = 10, |
| 296 | .tCS = 35, |
| 297 | .tWH = 15, |
| 298 | .tWP = 25, |
| 299 | .tRH = 15, |
| 300 | .tRP = 25, |
| 301 | .tR = 25000, |
| 302 | .tWHR = 60, |
| 303 | .tAR = 10, |
| 304 | }; |
| 305 | |
| 306 | static struct pxa3xx_nand_flash stm2GbX16 = { |
| 307 | .timing = &stm2GbX16_timing, |
| 308 | .page_per_block = 64, |
| 309 | .page_size = 2048, |
| 310 | .flash_width = 16, |
| 311 | .dfc_width = 16, |
| 312 | .num_blocks = 2048, |
| 313 | .chip_id = 0xba20, |
| 314 | }; |
| 315 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 316 | static struct pxa3xx_nand_flash *builtin_flash_types[] = { |
| 317 | &samsung512MbX16, |
| 318 | µn1GbX8, |
| 319 | µn1GbX16, |
Semun Lee | 4262bd2 | 2008-09-01 11:49:27 +0100 | [diff] [blame^] | 320 | &stm2GbX16, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 321 | }; |
| 322 | |
| 323 | #define NDTR0_tCH(c) (min((c), 7) << 19) |
| 324 | #define NDTR0_tCS(c) (min((c), 7) << 16) |
| 325 | #define NDTR0_tWH(c) (min((c), 7) << 11) |
| 326 | #define NDTR0_tWP(c) (min((c), 7) << 8) |
| 327 | #define NDTR0_tRH(c) (min((c), 7) << 3) |
| 328 | #define NDTR0_tRP(c) (min((c), 7) << 0) |
| 329 | |
| 330 | #define NDTR1_tR(c) (min((c), 65535) << 16) |
| 331 | #define NDTR1_tWHR(c) (min((c), 15) << 4) |
| 332 | #define NDTR1_tAR(c) (min((c), 15) << 0) |
| 333 | |
| 334 | /* convert nano-seconds to nand flash controller clock cycles */ |
| 335 | #define ns2cycle(ns, clk) (int)(((ns) * (clk / 1000000) / 1000) + 1) |
| 336 | |
| 337 | static void pxa3xx_nand_set_timing(struct pxa3xx_nand_info *info, |
| 338 | struct pxa3xx_nand_timing *t) |
| 339 | { |
| 340 | unsigned long nand_clk = clk_get_rate(info->clk); |
| 341 | uint32_t ndtr0, ndtr1; |
| 342 | |
| 343 | ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) | |
| 344 | NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) | |
| 345 | NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) | |
| 346 | NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) | |
| 347 | NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) | |
| 348 | NDTR0_tRP(ns2cycle(t->tRP, nand_clk)); |
| 349 | |
| 350 | ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) | |
| 351 | NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) | |
| 352 | NDTR1_tAR(ns2cycle(t->tAR, nand_clk)); |
| 353 | |
| 354 | nand_writel(info, NDTR0CS0, ndtr0); |
| 355 | nand_writel(info, NDTR1CS0, ndtr1); |
| 356 | } |
| 357 | |
| 358 | #define WAIT_EVENT_TIMEOUT 10 |
| 359 | |
| 360 | static int wait_for_event(struct pxa3xx_nand_info *info, uint32_t event) |
| 361 | { |
| 362 | int timeout = WAIT_EVENT_TIMEOUT; |
| 363 | uint32_t ndsr; |
| 364 | |
| 365 | while (timeout--) { |
| 366 | ndsr = nand_readl(info, NDSR) & NDSR_MASK; |
| 367 | if (ndsr & event) { |
| 368 | nand_writel(info, NDSR, ndsr); |
| 369 | return 0; |
| 370 | } |
| 371 | udelay(10); |
| 372 | } |
| 373 | |
| 374 | return -ETIMEDOUT; |
| 375 | } |
| 376 | |
| 377 | static int prepare_read_prog_cmd(struct pxa3xx_nand_info *info, |
| 378 | uint16_t cmd, int column, int page_addr) |
| 379 | { |
| 380 | struct pxa3xx_nand_flash *f = info->flash_info; |
| 381 | struct pxa3xx_nand_cmdset *cmdset = f->cmdset; |
| 382 | |
| 383 | /* calculate data size */ |
| 384 | switch (f->page_size) { |
| 385 | case 2048: |
| 386 | info->data_size = (info->use_ecc) ? 2088 : 2112; |
| 387 | break; |
| 388 | case 512: |
| 389 | info->data_size = (info->use_ecc) ? 520 : 528; |
| 390 | break; |
| 391 | default: |
| 392 | return -EINVAL; |
| 393 | } |
| 394 | |
| 395 | /* generate values for NDCBx registers */ |
| 396 | info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0); |
| 397 | info->ndcb1 = 0; |
| 398 | info->ndcb2 = 0; |
| 399 | info->ndcb0 |= NDCB0_ADDR_CYC(f->row_addr_cycles + f->col_addr_cycles); |
| 400 | |
| 401 | if (f->col_addr_cycles == 2) { |
| 402 | /* large block, 2 cycles for column address |
| 403 | * row address starts from 3rd cycle |
| 404 | */ |
| 405 | info->ndcb1 |= (page_addr << 16) | (column & 0xffff); |
| 406 | if (f->row_addr_cycles == 3) |
| 407 | info->ndcb2 = (page_addr >> 16) & 0xff; |
| 408 | } else |
| 409 | /* small block, 1 cycles for column address |
| 410 | * row address starts from 2nd cycle |
| 411 | */ |
| 412 | info->ndcb1 = (page_addr << 8) | (column & 0xff); |
| 413 | |
| 414 | if (cmd == cmdset->program) |
| 415 | info->ndcb0 |= NDCB0_CMD_TYPE(1) | NDCB0_AUTO_RS; |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | static int prepare_erase_cmd(struct pxa3xx_nand_info *info, |
| 421 | uint16_t cmd, int page_addr) |
| 422 | { |
| 423 | info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0); |
| 424 | info->ndcb0 |= NDCB0_CMD_TYPE(2) | NDCB0_AUTO_RS | NDCB0_ADDR_CYC(3); |
| 425 | info->ndcb1 = page_addr; |
| 426 | info->ndcb2 = 0; |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static int prepare_other_cmd(struct pxa3xx_nand_info *info, uint16_t cmd) |
| 431 | { |
| 432 | struct pxa3xx_nand_cmdset *cmdset = info->flash_info->cmdset; |
| 433 | |
| 434 | info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0); |
| 435 | info->ndcb1 = 0; |
| 436 | info->ndcb2 = 0; |
| 437 | |
| 438 | if (cmd == cmdset->read_id) { |
| 439 | info->ndcb0 |= NDCB0_CMD_TYPE(3); |
| 440 | info->data_size = 8; |
| 441 | } else if (cmd == cmdset->read_status) { |
| 442 | info->ndcb0 |= NDCB0_CMD_TYPE(4); |
| 443 | info->data_size = 8; |
| 444 | } else if (cmd == cmdset->reset || cmd == cmdset->lock || |
| 445 | cmd == cmdset->unlock) { |
| 446 | info->ndcb0 |= NDCB0_CMD_TYPE(5); |
| 447 | } else |
| 448 | return -EINVAL; |
| 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask) |
| 454 | { |
| 455 | uint32_t ndcr; |
| 456 | |
| 457 | ndcr = nand_readl(info, NDCR); |
| 458 | nand_writel(info, NDCR, ndcr & ~int_mask); |
| 459 | } |
| 460 | |
| 461 | static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask) |
| 462 | { |
| 463 | uint32_t ndcr; |
| 464 | |
| 465 | ndcr = nand_readl(info, NDCR); |
| 466 | nand_writel(info, NDCR, ndcr | int_mask); |
| 467 | } |
| 468 | |
| 469 | /* NOTE: it is a must to set ND_RUN firstly, then write command buffer |
| 470 | * otherwise, it does not work |
| 471 | */ |
| 472 | static int write_cmd(struct pxa3xx_nand_info *info) |
| 473 | { |
| 474 | uint32_t ndcr; |
| 475 | |
| 476 | /* clear status bits and run */ |
| 477 | nand_writel(info, NDSR, NDSR_MASK); |
| 478 | |
| 479 | ndcr = info->reg_ndcr; |
| 480 | |
| 481 | ndcr |= info->use_ecc ? NDCR_ECC_EN : 0; |
| 482 | ndcr |= info->use_dma ? NDCR_DMA_EN : 0; |
| 483 | ndcr |= NDCR_ND_RUN; |
| 484 | |
| 485 | nand_writel(info, NDCR, ndcr); |
| 486 | |
| 487 | if (wait_for_event(info, NDSR_WRCMDREQ)) { |
| 488 | printk(KERN_ERR "timed out writing command\n"); |
| 489 | return -ETIMEDOUT; |
| 490 | } |
| 491 | |
| 492 | nand_writel(info, NDCB0, info->ndcb0); |
| 493 | nand_writel(info, NDCB0, info->ndcb1); |
| 494 | nand_writel(info, NDCB0, info->ndcb2); |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static int handle_data_pio(struct pxa3xx_nand_info *info) |
| 499 | { |
| 500 | int ret, timeout = CHIP_DELAY_TIMEOUT; |
| 501 | |
| 502 | switch (info->state) { |
| 503 | case STATE_PIO_WRITING: |
| 504 | __raw_writesl(info->mmio_base + NDDB, info->data_buff, |
| 505 | info->data_size << 2); |
| 506 | |
| 507 | enable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD); |
| 508 | |
| 509 | ret = wait_for_completion_timeout(&info->cmd_complete, timeout); |
| 510 | if (!ret) { |
| 511 | printk(KERN_ERR "program command time out\n"); |
| 512 | return -1; |
| 513 | } |
| 514 | break; |
| 515 | case STATE_PIO_READING: |
| 516 | __raw_readsl(info->mmio_base + NDDB, info->data_buff, |
| 517 | info->data_size << 2); |
| 518 | break; |
| 519 | default: |
David Woodhouse | a1c06ee | 2008-04-22 20:39:43 +0100 | [diff] [blame] | 520 | printk(KERN_ERR "%s: invalid state %d\n", __func__, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 521 | info->state); |
| 522 | return -EINVAL; |
| 523 | } |
| 524 | |
| 525 | info->state = STATE_READY; |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | static void start_data_dma(struct pxa3xx_nand_info *info, int dir_out) |
| 530 | { |
| 531 | struct pxa_dma_desc *desc = info->data_desc; |
| 532 | int dma_len = ALIGN(info->data_size, 32); |
| 533 | |
| 534 | desc->ddadr = DDADR_STOP; |
| 535 | desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len; |
| 536 | |
| 537 | if (dir_out) { |
| 538 | desc->dsadr = info->data_buff_phys; |
| 539 | desc->dtadr = NDDB_DMA_ADDR; |
| 540 | desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG; |
| 541 | } else { |
| 542 | desc->dtadr = info->data_buff_phys; |
| 543 | desc->dsadr = NDDB_DMA_ADDR; |
| 544 | desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC; |
| 545 | } |
| 546 | |
| 547 | DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch; |
| 548 | DDADR(info->data_dma_ch) = info->data_desc_addr; |
| 549 | DCSR(info->data_dma_ch) |= DCSR_RUN; |
| 550 | } |
| 551 | |
| 552 | static void pxa3xx_nand_data_dma_irq(int channel, void *data) |
| 553 | { |
| 554 | struct pxa3xx_nand_info *info = data; |
| 555 | uint32_t dcsr; |
| 556 | |
| 557 | dcsr = DCSR(channel); |
| 558 | DCSR(channel) = dcsr; |
| 559 | |
| 560 | if (dcsr & DCSR_BUSERR) { |
| 561 | info->retcode = ERR_DMABUSERR; |
| 562 | complete(&info->cmd_complete); |
| 563 | } |
| 564 | |
| 565 | if (info->state == STATE_DMA_WRITING) { |
| 566 | info->state = STATE_DMA_DONE; |
| 567 | enable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD); |
| 568 | } else { |
| 569 | info->state = STATE_READY; |
| 570 | complete(&info->cmd_complete); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | static irqreturn_t pxa3xx_nand_irq(int irq, void *devid) |
| 575 | { |
| 576 | struct pxa3xx_nand_info *info = devid; |
| 577 | unsigned int status; |
| 578 | |
| 579 | status = nand_readl(info, NDSR); |
| 580 | |
| 581 | if (status & (NDSR_RDDREQ | NDSR_DBERR)) { |
| 582 | if (status & NDSR_DBERR) |
| 583 | info->retcode = ERR_DBERR; |
| 584 | |
| 585 | disable_int(info, NDSR_RDDREQ | NDSR_DBERR); |
| 586 | |
| 587 | if (info->use_dma) { |
| 588 | info->state = STATE_DMA_READING; |
| 589 | start_data_dma(info, 0); |
| 590 | } else { |
| 591 | info->state = STATE_PIO_READING; |
| 592 | complete(&info->cmd_complete); |
| 593 | } |
| 594 | } else if (status & NDSR_WRDREQ) { |
| 595 | disable_int(info, NDSR_WRDREQ); |
| 596 | if (info->use_dma) { |
| 597 | info->state = STATE_DMA_WRITING; |
| 598 | start_data_dma(info, 1); |
| 599 | } else { |
| 600 | info->state = STATE_PIO_WRITING; |
| 601 | complete(&info->cmd_complete); |
| 602 | } |
| 603 | } else if (status & (NDSR_CS0_BBD | NDSR_CS0_CMDD)) { |
| 604 | if (status & NDSR_CS0_BBD) |
| 605 | info->retcode = ERR_BBERR; |
| 606 | |
| 607 | disable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD); |
| 608 | info->state = STATE_READY; |
| 609 | complete(&info->cmd_complete); |
| 610 | } |
| 611 | nand_writel(info, NDSR, status); |
| 612 | return IRQ_HANDLED; |
| 613 | } |
| 614 | |
| 615 | static int pxa3xx_nand_do_cmd(struct pxa3xx_nand_info *info, uint32_t event) |
| 616 | { |
| 617 | uint32_t ndcr; |
| 618 | int ret, timeout = CHIP_DELAY_TIMEOUT; |
| 619 | |
| 620 | if (write_cmd(info)) { |
| 621 | info->retcode = ERR_SENDCMD; |
| 622 | goto fail_stop; |
| 623 | } |
| 624 | |
| 625 | info->state = STATE_CMD_HANDLE; |
| 626 | |
| 627 | enable_int(info, event); |
| 628 | |
| 629 | ret = wait_for_completion_timeout(&info->cmd_complete, timeout); |
| 630 | if (!ret) { |
| 631 | printk(KERN_ERR "command execution timed out\n"); |
| 632 | info->retcode = ERR_SENDCMD; |
| 633 | goto fail_stop; |
| 634 | } |
| 635 | |
| 636 | if (info->use_dma == 0 && info->data_size > 0) |
| 637 | if (handle_data_pio(info)) |
| 638 | goto fail_stop; |
| 639 | |
| 640 | return 0; |
| 641 | |
| 642 | fail_stop: |
| 643 | ndcr = nand_readl(info, NDCR); |
| 644 | nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN); |
| 645 | udelay(10); |
| 646 | return -ETIMEDOUT; |
| 647 | } |
| 648 | |
| 649 | static int pxa3xx_nand_dev_ready(struct mtd_info *mtd) |
| 650 | { |
| 651 | struct pxa3xx_nand_info *info = mtd->priv; |
| 652 | return (nand_readl(info, NDSR) & NDSR_RDY) ? 1 : 0; |
| 653 | } |
| 654 | |
| 655 | static inline int is_buf_blank(uint8_t *buf, size_t len) |
| 656 | { |
| 657 | for (; len > 0; len--) |
| 658 | if (*buf++ != 0xff) |
| 659 | return 0; |
| 660 | return 1; |
| 661 | } |
| 662 | |
| 663 | static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command, |
David Woodhouse | a1c06ee | 2008-04-22 20:39:43 +0100 | [diff] [blame] | 664 | int column, int page_addr) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 665 | { |
| 666 | struct pxa3xx_nand_info *info = mtd->priv; |
David Woodhouse | a1c06ee | 2008-04-22 20:39:43 +0100 | [diff] [blame] | 667 | struct pxa3xx_nand_flash *flash_info = info->flash_info; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 668 | struct pxa3xx_nand_cmdset *cmdset = flash_info->cmdset; |
| 669 | int ret; |
| 670 | |
| 671 | info->use_dma = (use_dma) ? 1 : 0; |
| 672 | info->use_ecc = 0; |
| 673 | info->data_size = 0; |
| 674 | info->state = STATE_READY; |
| 675 | |
| 676 | init_completion(&info->cmd_complete); |
| 677 | |
| 678 | switch (command) { |
| 679 | case NAND_CMD_READOOB: |
| 680 | /* disable HW ECC to get all the OOB data */ |
| 681 | info->buf_count = mtd->writesize + mtd->oobsize; |
| 682 | info->buf_start = mtd->writesize + column; |
| 683 | |
| 684 | if (prepare_read_prog_cmd(info, cmdset->read1, column, page_addr)) |
| 685 | break; |
| 686 | |
| 687 | pxa3xx_nand_do_cmd(info, NDSR_RDDREQ | NDSR_DBERR); |
| 688 | |
| 689 | /* We only are OOB, so if the data has error, does not matter */ |
| 690 | if (info->retcode == ERR_DBERR) |
| 691 | info->retcode = ERR_NONE; |
| 692 | break; |
| 693 | |
| 694 | case NAND_CMD_READ0: |
| 695 | info->use_ecc = 1; |
| 696 | info->retcode = ERR_NONE; |
| 697 | info->buf_start = column; |
| 698 | info->buf_count = mtd->writesize + mtd->oobsize; |
| 699 | memset(info->data_buff, 0xFF, info->buf_count); |
| 700 | |
| 701 | if (prepare_read_prog_cmd(info, cmdset->read1, column, page_addr)) |
| 702 | break; |
| 703 | |
| 704 | pxa3xx_nand_do_cmd(info, NDSR_RDDREQ | NDSR_DBERR); |
| 705 | |
| 706 | if (info->retcode == ERR_DBERR) { |
| 707 | /* for blank page (all 0xff), HW will calculate its ECC as |
| 708 | * 0, which is different from the ECC information within |
| 709 | * OOB, ignore such double bit errors |
| 710 | */ |
| 711 | if (is_buf_blank(info->data_buff, mtd->writesize)) |
| 712 | info->retcode = ERR_NONE; |
| 713 | } |
| 714 | break; |
| 715 | case NAND_CMD_SEQIN: |
| 716 | info->buf_start = column; |
| 717 | info->buf_count = mtd->writesize + mtd->oobsize; |
| 718 | memset(info->data_buff, 0xff, info->buf_count); |
| 719 | |
| 720 | /* save column/page_addr for next CMD_PAGEPROG */ |
| 721 | info->seqin_column = column; |
| 722 | info->seqin_page_addr = page_addr; |
| 723 | break; |
| 724 | case NAND_CMD_PAGEPROG: |
| 725 | info->use_ecc = (info->seqin_column >= mtd->writesize) ? 0 : 1; |
| 726 | |
| 727 | if (prepare_read_prog_cmd(info, cmdset->program, |
| 728 | info->seqin_column, info->seqin_page_addr)) |
| 729 | break; |
| 730 | |
| 731 | pxa3xx_nand_do_cmd(info, NDSR_WRDREQ); |
| 732 | break; |
| 733 | case NAND_CMD_ERASE1: |
| 734 | if (prepare_erase_cmd(info, cmdset->erase, page_addr)) |
| 735 | break; |
| 736 | |
| 737 | pxa3xx_nand_do_cmd(info, NDSR_CS0_BBD | NDSR_CS0_CMDD); |
| 738 | break; |
| 739 | case NAND_CMD_ERASE2: |
| 740 | break; |
| 741 | case NAND_CMD_READID: |
| 742 | case NAND_CMD_STATUS: |
| 743 | info->use_dma = 0; /* force PIO read */ |
| 744 | info->buf_start = 0; |
| 745 | info->buf_count = (command == NAND_CMD_READID) ? |
| 746 | flash_info->read_id_bytes : 1; |
| 747 | |
| 748 | if (prepare_other_cmd(info, (command == NAND_CMD_READID) ? |
| 749 | cmdset->read_id : cmdset->read_status)) |
| 750 | break; |
| 751 | |
| 752 | pxa3xx_nand_do_cmd(info, NDSR_RDDREQ); |
| 753 | break; |
| 754 | case NAND_CMD_RESET: |
| 755 | if (prepare_other_cmd(info, cmdset->reset)) |
| 756 | break; |
| 757 | |
| 758 | ret = pxa3xx_nand_do_cmd(info, NDSR_CS0_CMDD); |
| 759 | if (ret == 0) { |
| 760 | int timeout = 2; |
| 761 | uint32_t ndcr; |
| 762 | |
| 763 | while (timeout--) { |
| 764 | if (nand_readl(info, NDSR) & NDSR_RDY) |
| 765 | break; |
| 766 | msleep(10); |
| 767 | } |
| 768 | |
| 769 | ndcr = nand_readl(info, NDCR); |
| 770 | nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN); |
| 771 | } |
| 772 | break; |
| 773 | default: |
| 774 | printk(KERN_ERR "non-supported command.\n"); |
| 775 | break; |
| 776 | } |
| 777 | |
| 778 | if (info->retcode == ERR_DBERR) { |
| 779 | printk(KERN_ERR "double bit error @ page %08x\n", page_addr); |
| 780 | info->retcode = ERR_NONE; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd) |
| 785 | { |
| 786 | struct pxa3xx_nand_info *info = mtd->priv; |
| 787 | char retval = 0xFF; |
| 788 | |
| 789 | if (info->buf_start < info->buf_count) |
| 790 | /* Has just send a new command? */ |
| 791 | retval = info->data_buff[info->buf_start++]; |
| 792 | |
| 793 | return retval; |
| 794 | } |
| 795 | |
| 796 | static u16 pxa3xx_nand_read_word(struct mtd_info *mtd) |
| 797 | { |
| 798 | struct pxa3xx_nand_info *info = mtd->priv; |
| 799 | u16 retval = 0xFFFF; |
| 800 | |
| 801 | if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) { |
| 802 | retval = *((u16 *)(info->data_buff+info->buf_start)); |
| 803 | info->buf_start += 2; |
| 804 | } |
| 805 | return retval; |
| 806 | } |
| 807 | |
| 808 | static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
| 809 | { |
| 810 | struct pxa3xx_nand_info *info = mtd->priv; |
| 811 | int real_len = min_t(size_t, len, info->buf_count - info->buf_start); |
| 812 | |
| 813 | memcpy(buf, info->data_buff + info->buf_start, real_len); |
| 814 | info->buf_start += real_len; |
| 815 | } |
| 816 | |
| 817 | static void pxa3xx_nand_write_buf(struct mtd_info *mtd, |
| 818 | const uint8_t *buf, int len) |
| 819 | { |
| 820 | struct pxa3xx_nand_info *info = mtd->priv; |
| 821 | int real_len = min_t(size_t, len, info->buf_count - info->buf_start); |
| 822 | |
| 823 | memcpy(info->data_buff + info->buf_start, buf, real_len); |
| 824 | info->buf_start += real_len; |
| 825 | } |
| 826 | |
| 827 | static int pxa3xx_nand_verify_buf(struct mtd_info *mtd, |
| 828 | const uint8_t *buf, int len) |
| 829 | { |
| 830 | return 0; |
| 831 | } |
| 832 | |
| 833 | static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip) |
| 834 | { |
| 835 | return; |
| 836 | } |
| 837 | |
| 838 | static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this) |
| 839 | { |
| 840 | struct pxa3xx_nand_info *info = mtd->priv; |
| 841 | |
| 842 | /* pxa3xx_nand_send_command has waited for command complete */ |
| 843 | if (this->state == FL_WRITING || this->state == FL_ERASING) { |
| 844 | if (info->retcode == ERR_NONE) |
| 845 | return 0; |
| 846 | else { |
| 847 | /* |
| 848 | * any error make it return 0x01 which will tell |
| 849 | * the caller the erase and write fail |
| 850 | */ |
| 851 | return 0x01; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | static void pxa3xx_nand_ecc_hwctl(struct mtd_info *mtd, int mode) |
| 859 | { |
| 860 | return; |
| 861 | } |
| 862 | |
| 863 | static int pxa3xx_nand_ecc_calculate(struct mtd_info *mtd, |
| 864 | const uint8_t *dat, uint8_t *ecc_code) |
| 865 | { |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | static int pxa3xx_nand_ecc_correct(struct mtd_info *mtd, |
| 870 | uint8_t *dat, uint8_t *read_ecc, uint8_t *calc_ecc) |
| 871 | { |
| 872 | struct pxa3xx_nand_info *info = mtd->priv; |
| 873 | /* |
| 874 | * Any error include ERR_SEND_CMD, ERR_DBERR, ERR_BUSERR, we |
| 875 | * consider it as a ecc error which will tell the caller the |
| 876 | * read fail We have distinguish all the errors, but the |
| 877 | * nand_read_ecc only check this function return value |
| 878 | */ |
| 879 | if (info->retcode != ERR_NONE) |
| 880 | return -1; |
| 881 | |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | static int __readid(struct pxa3xx_nand_info *info, uint32_t *id) |
| 886 | { |
| 887 | struct pxa3xx_nand_flash *f = info->flash_info; |
| 888 | struct pxa3xx_nand_cmdset *cmdset = f->cmdset; |
| 889 | uint32_t ndcr; |
| 890 | uint8_t id_buff[8]; |
| 891 | |
| 892 | if (prepare_other_cmd(info, cmdset->read_id)) { |
| 893 | printk(KERN_ERR "failed to prepare command\n"); |
| 894 | return -EINVAL; |
| 895 | } |
| 896 | |
| 897 | /* Send command */ |
| 898 | if (write_cmd(info)) |
| 899 | goto fail_timeout; |
| 900 | |
| 901 | /* Wait for CMDDM(command done successfully) */ |
| 902 | if (wait_for_event(info, NDSR_RDDREQ)) |
| 903 | goto fail_timeout; |
| 904 | |
| 905 | __raw_readsl(info->mmio_base + NDDB, id_buff, 2); |
| 906 | *id = id_buff[0] | (id_buff[1] << 8); |
| 907 | return 0; |
| 908 | |
| 909 | fail_timeout: |
| 910 | ndcr = nand_readl(info, NDCR); |
| 911 | nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN); |
| 912 | udelay(10); |
| 913 | return -ETIMEDOUT; |
| 914 | } |
| 915 | |
| 916 | static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info, |
| 917 | struct pxa3xx_nand_flash *f) |
| 918 | { |
| 919 | struct platform_device *pdev = info->pdev; |
| 920 | struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data; |
| 921 | uint32_t ndcr = 0x00000FFF; /* disable all interrupts */ |
| 922 | |
| 923 | if (f->page_size != 2048 && f->page_size != 512) |
| 924 | return -EINVAL; |
| 925 | |
| 926 | if (f->flash_width != 16 && f->flash_width != 8) |
| 927 | return -EINVAL; |
| 928 | |
| 929 | /* calculate flash information */ |
| 930 | f->oob_size = (f->page_size == 2048) ? 64 : 16; |
| 931 | f->read_id_bytes = (f->page_size == 2048) ? 4 : 2; |
| 932 | |
| 933 | /* calculate addressing information */ |
| 934 | f->col_addr_cycles = (f->page_size == 2048) ? 2 : 1; |
| 935 | |
| 936 | if (f->num_blocks * f->page_per_block > 65536) |
| 937 | f->row_addr_cycles = 3; |
| 938 | else |
| 939 | f->row_addr_cycles = 2; |
| 940 | |
| 941 | ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0; |
| 942 | ndcr |= (f->col_addr_cycles == 2) ? NDCR_RA_START : 0; |
| 943 | ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0; |
| 944 | ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0; |
| 945 | ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0; |
| 946 | ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0; |
| 947 | |
| 948 | ndcr |= NDCR_RD_ID_CNT(f->read_id_bytes); |
| 949 | ndcr |= NDCR_SPARE_EN; /* enable spare by default */ |
| 950 | |
| 951 | info->reg_ndcr = ndcr; |
| 952 | |
| 953 | pxa3xx_nand_set_timing(info, f->timing); |
| 954 | info->flash_info = f; |
| 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | static int pxa3xx_nand_detect_flash(struct pxa3xx_nand_info *info) |
| 959 | { |
| 960 | struct pxa3xx_nand_flash *f; |
| 961 | uint32_t id; |
| 962 | int i; |
| 963 | |
| 964 | for (i = 0; i < ARRAY_SIZE(builtin_flash_types); i++) { |
| 965 | |
| 966 | f = builtin_flash_types[i]; |
| 967 | |
| 968 | if (pxa3xx_nand_config_flash(info, f)) |
| 969 | continue; |
| 970 | |
| 971 | if (__readid(info, &id)) |
| 972 | continue; |
| 973 | |
| 974 | if (id == f->chip_id) |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | return -ENODEV; |
| 979 | } |
| 980 | |
| 981 | /* the maximum possible buffer size for large page with OOB data |
| 982 | * is: 2048 + 64 = 2112 bytes, allocate a page here for both the |
| 983 | * data buffer and the DMA descriptor |
| 984 | */ |
| 985 | #define MAX_BUFF_SIZE PAGE_SIZE |
| 986 | |
| 987 | static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info) |
| 988 | { |
| 989 | struct platform_device *pdev = info->pdev; |
| 990 | int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc); |
| 991 | |
| 992 | if (use_dma == 0) { |
| 993 | info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL); |
| 994 | if (info->data_buff == NULL) |
| 995 | return -ENOMEM; |
| 996 | return 0; |
| 997 | } |
| 998 | |
| 999 | info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE, |
| 1000 | &info->data_buff_phys, GFP_KERNEL); |
| 1001 | if (info->data_buff == NULL) { |
| 1002 | dev_err(&pdev->dev, "failed to allocate dma buffer\n"); |
| 1003 | return -ENOMEM; |
| 1004 | } |
| 1005 | |
| 1006 | info->data_buff_size = MAX_BUFF_SIZE; |
| 1007 | info->data_desc = (void *)info->data_buff + data_desc_offset; |
| 1008 | info->data_desc_addr = info->data_buff_phys + data_desc_offset; |
| 1009 | |
| 1010 | info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW, |
| 1011 | pxa3xx_nand_data_dma_irq, info); |
| 1012 | if (info->data_dma_ch < 0) { |
| 1013 | dev_err(&pdev->dev, "failed to request data dma\n"); |
| 1014 | dma_free_coherent(&pdev->dev, info->data_buff_size, |
| 1015 | info->data_buff, info->data_buff_phys); |
| 1016 | return info->data_dma_ch; |
| 1017 | } |
| 1018 | |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
| 1022 | static struct nand_ecclayout hw_smallpage_ecclayout = { |
| 1023 | .eccbytes = 6, |
| 1024 | .eccpos = {8, 9, 10, 11, 12, 13 }, |
| 1025 | .oobfree = { {2, 6} } |
| 1026 | }; |
| 1027 | |
| 1028 | static struct nand_ecclayout hw_largepage_ecclayout = { |
| 1029 | .eccbytes = 24, |
| 1030 | .eccpos = { |
| 1031 | 40, 41, 42, 43, 44, 45, 46, 47, |
| 1032 | 48, 49, 50, 51, 52, 53, 54, 55, |
| 1033 | 56, 57, 58, 59, 60, 61, 62, 63}, |
| 1034 | .oobfree = { {2, 38} } |
| 1035 | }; |
| 1036 | |
| 1037 | static void pxa3xx_nand_init_mtd(struct mtd_info *mtd, |
| 1038 | struct pxa3xx_nand_info *info) |
| 1039 | { |
| 1040 | struct pxa3xx_nand_flash *f = info->flash_info; |
| 1041 | struct nand_chip *this = &info->nand_chip; |
| 1042 | |
| 1043 | this->options = (f->flash_width == 16) ? NAND_BUSWIDTH_16: 0; |
| 1044 | |
| 1045 | this->waitfunc = pxa3xx_nand_waitfunc; |
| 1046 | this->select_chip = pxa3xx_nand_select_chip; |
| 1047 | this->dev_ready = pxa3xx_nand_dev_ready; |
| 1048 | this->cmdfunc = pxa3xx_nand_cmdfunc; |
| 1049 | this->read_word = pxa3xx_nand_read_word; |
| 1050 | this->read_byte = pxa3xx_nand_read_byte; |
| 1051 | this->read_buf = pxa3xx_nand_read_buf; |
| 1052 | this->write_buf = pxa3xx_nand_write_buf; |
| 1053 | this->verify_buf = pxa3xx_nand_verify_buf; |
| 1054 | |
| 1055 | this->ecc.mode = NAND_ECC_HW; |
| 1056 | this->ecc.hwctl = pxa3xx_nand_ecc_hwctl; |
| 1057 | this->ecc.calculate = pxa3xx_nand_ecc_calculate; |
| 1058 | this->ecc.correct = pxa3xx_nand_ecc_correct; |
| 1059 | this->ecc.size = f->page_size; |
| 1060 | |
| 1061 | if (f->page_size == 2048) |
| 1062 | this->ecc.layout = &hw_largepage_ecclayout; |
| 1063 | else |
| 1064 | this->ecc.layout = &hw_smallpage_ecclayout; |
| 1065 | |
David Woodhouse | a1c06ee | 2008-04-22 20:39:43 +0100 | [diff] [blame] | 1066 | this->chip_delay = 25; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | static int pxa3xx_nand_probe(struct platform_device *pdev) |
| 1070 | { |
| 1071 | struct pxa3xx_nand_platform_data *pdata; |
| 1072 | struct pxa3xx_nand_info *info; |
| 1073 | struct nand_chip *this; |
| 1074 | struct mtd_info *mtd; |
| 1075 | struct resource *r; |
| 1076 | int ret = 0, irq; |
| 1077 | |
| 1078 | pdata = pdev->dev.platform_data; |
| 1079 | |
David Woodhouse | a1c06ee | 2008-04-22 20:39:43 +0100 | [diff] [blame] | 1080 | if (!pdata) { |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1081 | dev_err(&pdev->dev, "no platform data defined\n"); |
| 1082 | return -ENODEV; |
| 1083 | } |
| 1084 | |
| 1085 | mtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct pxa3xx_nand_info), |
| 1086 | GFP_KERNEL); |
David Woodhouse | a1c06ee | 2008-04-22 20:39:43 +0100 | [diff] [blame] | 1087 | if (!mtd) { |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1088 | dev_err(&pdev->dev, "failed to allocate memory\n"); |
| 1089 | return -ENOMEM; |
David Woodhouse | a1c06ee | 2008-04-22 20:39:43 +0100 | [diff] [blame] | 1090 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1091 | |
| 1092 | info = (struct pxa3xx_nand_info *)(&mtd[1]); |
| 1093 | info->pdev = pdev; |
| 1094 | |
| 1095 | this = &info->nand_chip; |
| 1096 | mtd->priv = info; |
| 1097 | |
| 1098 | info->clk = clk_get(&pdev->dev, "NANDCLK"); |
| 1099 | if (IS_ERR(info->clk)) { |
| 1100 | dev_err(&pdev->dev, "failed to get nand clock\n"); |
| 1101 | ret = PTR_ERR(info->clk); |
| 1102 | goto fail_free_mtd; |
| 1103 | } |
| 1104 | clk_enable(info->clk); |
| 1105 | |
| 1106 | r = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 1107 | if (r == NULL) { |
| 1108 | dev_err(&pdev->dev, "no resource defined for data DMA\n"); |
| 1109 | ret = -ENXIO; |
| 1110 | goto fail_put_clk; |
| 1111 | } |
| 1112 | info->drcmr_dat = r->start; |
| 1113 | |
| 1114 | r = platform_get_resource(pdev, IORESOURCE_DMA, 1); |
| 1115 | if (r == NULL) { |
| 1116 | dev_err(&pdev->dev, "no resource defined for command DMA\n"); |
| 1117 | ret = -ENXIO; |
| 1118 | goto fail_put_clk; |
| 1119 | } |
| 1120 | info->drcmr_cmd = r->start; |
| 1121 | |
| 1122 | irq = platform_get_irq(pdev, 0); |
| 1123 | if (irq < 0) { |
| 1124 | dev_err(&pdev->dev, "no IRQ resource defined\n"); |
| 1125 | ret = -ENXIO; |
| 1126 | goto fail_put_clk; |
| 1127 | } |
| 1128 | |
| 1129 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1130 | if (r == NULL) { |
| 1131 | dev_err(&pdev->dev, "no IO memory resource defined\n"); |
| 1132 | ret = -ENODEV; |
| 1133 | goto fail_put_clk; |
| 1134 | } |
| 1135 | |
| 1136 | r = request_mem_region(r->start, r->end - r->start + 1, pdev->name); |
| 1137 | if (r == NULL) { |
| 1138 | dev_err(&pdev->dev, "failed to request memory resource\n"); |
| 1139 | ret = -EBUSY; |
| 1140 | goto fail_put_clk; |
| 1141 | } |
| 1142 | |
| 1143 | info->mmio_base = ioremap(r->start, r->end - r->start + 1); |
| 1144 | if (info->mmio_base == NULL) { |
| 1145 | dev_err(&pdev->dev, "ioremap() failed\n"); |
| 1146 | ret = -ENODEV; |
| 1147 | goto fail_free_res; |
| 1148 | } |
| 1149 | |
| 1150 | ret = pxa3xx_nand_init_buff(info); |
| 1151 | if (ret) |
| 1152 | goto fail_free_io; |
| 1153 | |
| 1154 | ret = request_irq(IRQ_NAND, pxa3xx_nand_irq, IRQF_DISABLED, |
| 1155 | pdev->name, info); |
| 1156 | if (ret < 0) { |
| 1157 | dev_err(&pdev->dev, "failed to request IRQ\n"); |
| 1158 | goto fail_free_buf; |
| 1159 | } |
| 1160 | |
| 1161 | ret = pxa3xx_nand_detect_flash(info); |
| 1162 | if (ret) { |
| 1163 | dev_err(&pdev->dev, "failed to detect flash\n"); |
| 1164 | ret = -ENODEV; |
| 1165 | goto fail_free_irq; |
| 1166 | } |
| 1167 | |
| 1168 | pxa3xx_nand_init_mtd(mtd, info); |
| 1169 | |
| 1170 | platform_set_drvdata(pdev, mtd); |
| 1171 | |
| 1172 | if (nand_scan(mtd, 1)) { |
| 1173 | dev_err(&pdev->dev, "failed to scan nand\n"); |
| 1174 | ret = -ENXIO; |
| 1175 | goto fail_free_irq; |
| 1176 | } |
| 1177 | |
| 1178 | return add_mtd_partitions(mtd, pdata->parts, pdata->nr_parts); |
| 1179 | |
| 1180 | fail_free_irq: |
| 1181 | free_irq(IRQ_NAND, info); |
| 1182 | fail_free_buf: |
| 1183 | if (use_dma) { |
| 1184 | pxa_free_dma(info->data_dma_ch); |
| 1185 | dma_free_coherent(&pdev->dev, info->data_buff_size, |
| 1186 | info->data_buff, info->data_buff_phys); |
| 1187 | } else |
| 1188 | kfree(info->data_buff); |
| 1189 | fail_free_io: |
| 1190 | iounmap(info->mmio_base); |
| 1191 | fail_free_res: |
| 1192 | release_mem_region(r->start, r->end - r->start + 1); |
| 1193 | fail_put_clk: |
| 1194 | clk_disable(info->clk); |
| 1195 | clk_put(info->clk); |
| 1196 | fail_free_mtd: |
| 1197 | kfree(mtd); |
| 1198 | return ret; |
| 1199 | } |
| 1200 | |
| 1201 | static int pxa3xx_nand_remove(struct platform_device *pdev) |
| 1202 | { |
| 1203 | struct mtd_info *mtd = platform_get_drvdata(pdev); |
| 1204 | struct pxa3xx_nand_info *info = mtd->priv; |
| 1205 | |
| 1206 | platform_set_drvdata(pdev, NULL); |
| 1207 | |
| 1208 | del_mtd_device(mtd); |
| 1209 | del_mtd_partitions(mtd); |
| 1210 | free_irq(IRQ_NAND, info); |
| 1211 | if (use_dma) { |
| 1212 | pxa_free_dma(info->data_dma_ch); |
| 1213 | dma_free_writecombine(&pdev->dev, info->data_buff_size, |
| 1214 | info->data_buff, info->data_buff_phys); |
| 1215 | } else |
| 1216 | kfree(info->data_buff); |
| 1217 | kfree(mtd); |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
| 1221 | #ifdef CONFIG_PM |
| 1222 | static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state) |
| 1223 | { |
| 1224 | struct mtd_info *mtd = (struct mtd_info *)platform_get_drvdata(pdev); |
| 1225 | struct pxa3xx_nand_info *info = mtd->priv; |
| 1226 | |
| 1227 | if (info->state != STATE_READY) { |
| 1228 | dev_err(&pdev->dev, "driver busy, state = %d\n", info->state); |
| 1229 | return -EAGAIN; |
| 1230 | } |
| 1231 | |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
| 1235 | static int pxa3xx_nand_resume(struct platform_device *pdev) |
| 1236 | { |
| 1237 | struct mtd_info *mtd = (struct mtd_info *)platform_get_drvdata(pdev); |
| 1238 | struct pxa3xx_nand_info *info = mtd->priv; |
| 1239 | |
| 1240 | clk_enable(info->clk); |
| 1241 | |
Eric Miao | 9b62d86 | 2008-05-21 17:26:15 +0800 | [diff] [blame] | 1242 | return pxa3xx_nand_config_flash(info, info->flash_info); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1243 | } |
| 1244 | #else |
| 1245 | #define pxa3xx_nand_suspend NULL |
| 1246 | #define pxa3xx_nand_resume NULL |
| 1247 | #endif |
| 1248 | |
| 1249 | static struct platform_driver pxa3xx_nand_driver = { |
| 1250 | .driver = { |
| 1251 | .name = "pxa3xx-nand", |
| 1252 | }, |
| 1253 | .probe = pxa3xx_nand_probe, |
| 1254 | .remove = pxa3xx_nand_remove, |
| 1255 | .suspend = pxa3xx_nand_suspend, |
| 1256 | .resume = pxa3xx_nand_resume, |
| 1257 | }; |
| 1258 | |
| 1259 | static int __init pxa3xx_nand_init(void) |
| 1260 | { |
| 1261 | return platform_driver_register(&pxa3xx_nand_driver); |
| 1262 | } |
| 1263 | module_init(pxa3xx_nand_init); |
| 1264 | |
| 1265 | static void __exit pxa3xx_nand_exit(void) |
| 1266 | { |
| 1267 | platform_driver_unregister(&pxa3xx_nand_driver); |
| 1268 | } |
| 1269 | module_exit(pxa3xx_nand_exit); |
| 1270 | |
| 1271 | MODULE_LICENSE("GPL"); |
| 1272 | MODULE_DESCRIPTION("PXA3xx NAND controller driver"); |