203
213
static struct pxa3xx_nand_flash builtin_flash_types[] = {
204
{ 0, 0, 2048, 8, 8, 0, &default_cmdset, &timing[0] },
205
{ 0x46ec, 32, 512, 16, 16, 4096, &default_cmdset, &timing[1] },
206
{ 0xdaec, 64, 2048, 8, 8, 2048, &default_cmdset, &timing[1] },
207
{ 0xd7ec, 128, 4096, 8, 8, 8192, &default_cmdset, &timing[1] },
208
{ 0xa12c, 64, 2048, 8, 8, 1024, &default_cmdset, &timing[2] },
209
{ 0xb12c, 64, 2048, 16, 16, 1024, &default_cmdset, &timing[2] },
210
{ 0xdc2c, 64, 2048, 8, 8, 4096, &default_cmdset, &timing[2] },
211
{ 0xcc2c, 64, 2048, 16, 16, 4096, &default_cmdset, &timing[2] },
212
{ 0xba20, 64, 2048, 16, 16, 2048, &default_cmdset, &timing[3] },
214
{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
215
{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
216
{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
217
{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
218
{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
219
{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
220
{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
221
{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
222
{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
215
225
/* Define a default flash type setting serve as flash detecting only */
216
226
#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
228
const char *mtd_names[] = {"pxa3xx_nand-0", NULL};
218
230
#define NDTR0_tCH(c) (min((c), 7) << 19)
219
231
#define NDTR0_tCS(c) (min((c), 7) << 16)
220
232
#define NDTR0_tWH(c) (min((c), 7) << 11)
294
static int prepare_read_prog_cmd(struct pxa3xx_nand_info *info,
295
uint16_t cmd, int column, int page_addr)
297
const struct pxa3xx_nand_cmdset *cmdset = info->cmdset;
298
pxa3xx_set_datasize(info);
300
/* generate values for NDCBx registers */
301
info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
304
info->ndcb0 |= NDCB0_ADDR_CYC(info->row_addr_cycles + info->col_addr_cycles);
306
if (info->col_addr_cycles == 2) {
307
/* large block, 2 cycles for column address
308
* row address starts from 3rd cycle
310
info->ndcb1 |= page_addr << 16;
311
if (info->row_addr_cycles == 3)
312
info->ndcb2 = (page_addr >> 16) & 0xff;
314
/* small block, 1 cycles for column address
315
* row address starts from 2nd cycle
317
info->ndcb1 = page_addr << 8;
319
if (cmd == cmdset->program)
320
info->ndcb0 |= NDCB0_CMD_TYPE(1) | NDCB0_AUTO_RS;
325
static int prepare_erase_cmd(struct pxa3xx_nand_info *info,
326
uint16_t cmd, int page_addr)
328
info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
329
info->ndcb0 |= NDCB0_CMD_TYPE(2) | NDCB0_AUTO_RS | NDCB0_ADDR_CYC(3);
330
info->ndcb1 = page_addr;
335
static int prepare_other_cmd(struct pxa3xx_nand_info *info, uint16_t cmd)
337
const struct pxa3xx_nand_cmdset *cmdset = info->cmdset;
339
info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
344
if (cmd == cmdset->read_id) {
345
info->ndcb0 |= NDCB0_CMD_TYPE(3);
347
} else if (cmd == cmdset->read_status) {
348
info->ndcb0 |= NDCB0_CMD_TYPE(4);
350
} else if (cmd == cmdset->reset || cmd == cmdset->lock ||
351
cmd == cmdset->unlock) {
352
info->ndcb0 |= NDCB0_CMD_TYPE(5);
359
static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
363
ndcr = nand_readl(info, NDCR);
364
nand_writel(info, NDCR, ndcr & ~int_mask);
367
static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
371
ndcr = nand_readl(info, NDCR);
372
nand_writel(info, NDCR, ndcr | int_mask);
375
/* NOTE: it is a must to set ND_RUN firstly, then write command buffer
376
* otherwise, it does not work
288
* NOTE: it is a must to set ND_RUN firstly, then write
289
* command buffer, otherwise, it does not work.
290
* We enable all the interrupt at the same time, and
291
* let pxa3xx_nand_irq to handle all logic.
378
static int write_cmd(struct pxa3xx_nand_info *info)
293
static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
382
/* clear status bits and run */
383
nand_writel(info, NDSR, NDSR_MASK);
385
297
ndcr = info->reg_ndcr;
387
298
ndcr |= info->use_ecc ? NDCR_ECC_EN : 0;
388
299
ndcr |= info->use_dma ? NDCR_DMA_EN : 0;
389
300
ndcr |= NDCR_ND_RUN;
302
/* clear status bits and run */
303
nand_writel(info, NDCR, 0);
304
nand_writel(info, NDSR, NDSR_MASK);
391
305
nand_writel(info, NDCR, ndcr);
393
if (wait_for_event(info, NDSR_WRCMDREQ)) {
394
printk(KERN_ERR "timed out writing command\n");
398
nand_writel(info, NDCB0, info->ndcb0);
399
nand_writel(info, NDCB0, info->ndcb1);
400
nand_writel(info, NDCB0, info->ndcb2);
404
static int handle_data_pio(struct pxa3xx_nand_info *info)
406
int ret, timeout = CHIP_DELAY_TIMEOUT;
308
static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
311
int timeout = NAND_STOP_DELAY;
313
/* wait RUN bit in NDCR become 0 */
314
ndcr = nand_readl(info, NDCR);
315
while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
316
ndcr = nand_readl(info, NDCR);
321
ndcr &= ~NDCR_ND_RUN;
322
nand_writel(info, NDCR, ndcr);
324
/* clear status bits */
325
nand_writel(info, NDSR, NDSR_MASK);
328
static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
332
ndcr = nand_readl(info, NDCR);
333
nand_writel(info, NDCR, ndcr & ~int_mask);
336
static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
340
ndcr = nand_readl(info, NDCR);
341
nand_writel(info, NDCR, ndcr | int_mask);
344
static void handle_data_pio(struct pxa3xx_nand_info *info)
408
346
switch (info->state) {
409
347
case STATE_PIO_WRITING:
410
348
__raw_writesl(info->mmio_base + NDDB, info->data_buff,
472
406
if (dcsr & DCSR_BUSERR) {
473
407
info->retcode = ERR_DMABUSERR;
474
complete(&info->cmd_complete);
477
if (info->state == STATE_DMA_WRITING) {
478
info->state = STATE_DMA_DONE;
479
enable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
481
info->state = STATE_READY;
482
complete(&info->cmd_complete);
410
info->state = STATE_DMA_DONE;
411
enable_int(info, NDCR_INT_MASK);
412
nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
486
415
static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
488
417
struct pxa3xx_nand_info *info = devid;
418
unsigned int status, is_completed = 0;
491
420
status = nand_readl(info, NDSR);
493
if (status & (NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR)) {
494
if (status & NDSR_DBERR)
495
info->retcode = ERR_DBERR;
496
else if (status & NDSR_SBERR)
497
info->retcode = ERR_SBERR;
499
disable_int(info, NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR);
502
info->state = STATE_DMA_READING;
503
start_data_dma(info, 0);
505
info->state = STATE_PIO_READING;
506
complete(&info->cmd_complete);
508
} else if (status & NDSR_WRDREQ) {
509
disable_int(info, NDSR_WRDREQ);
511
info->state = STATE_DMA_WRITING;
512
start_data_dma(info, 1);
514
info->state = STATE_PIO_WRITING;
515
complete(&info->cmd_complete);
517
} else if (status & (NDSR_CS0_BBD | NDSR_CS0_CMDD)) {
518
if (status & NDSR_CS0_BBD)
519
info->retcode = ERR_BBERR;
521
disable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
422
if (status & NDSR_DBERR)
423
info->retcode = ERR_DBERR;
424
if (status & NDSR_SBERR)
425
info->retcode = ERR_SBERR;
426
if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
427
/* whether use dma to transfer data */
429
disable_int(info, NDCR_INT_MASK);
430
info->state = (status & NDSR_RDDREQ) ?
431
STATE_DMA_READING : STATE_DMA_WRITING;
432
start_data_dma(info);
433
goto NORMAL_IRQ_EXIT;
435
info->state = (status & NDSR_RDDREQ) ?
436
STATE_PIO_READING : STATE_PIO_WRITING;
437
handle_data_pio(info);
440
if (status & NDSR_CS0_CMDD) {
441
info->state = STATE_CMD_DONE;
444
if (status & NDSR_FLASH_RDY) {
522
446
info->state = STATE_READY;
523
complete(&info->cmd_complete);
449
if (status & NDSR_WRCMDREQ) {
450
nand_writel(info, NDSR, NDSR_WRCMDREQ);
451
status &= ~NDSR_WRCMDREQ;
452
info->state = STATE_CMD_HANDLE;
453
nand_writel(info, NDCB0, info->ndcb0);
454
nand_writel(info, NDCB0, info->ndcb1);
455
nand_writel(info, NDCB0, info->ndcb2);
458
/* clear NDSR to let the controller exit the IRQ */
525
459
nand_writel(info, NDSR, status);
461
complete(&info->cmd_complete);
526
463
return IRQ_HANDLED;
529
static int pxa3xx_nand_do_cmd(struct pxa3xx_nand_info *info, uint32_t event)
532
int ret, timeout = CHIP_DELAY_TIMEOUT;
534
if (write_cmd(info)) {
535
info->retcode = ERR_SENDCMD;
539
info->state = STATE_CMD_HANDLE;
541
enable_int(info, event);
543
ret = wait_for_completion_timeout(&info->cmd_complete, timeout);
545
printk(KERN_ERR "command execution timed out\n");
546
info->retcode = ERR_SENDCMD;
550
if (info->use_dma == 0 && info->data_size > 0)
551
if (handle_data_pio(info))
557
ndcr = nand_readl(info, NDCR);
558
nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
563
466
static int pxa3xx_nand_dev_ready(struct mtd_info *mtd)
565
468
struct pxa3xx_nand_info *info = mtd->priv;
577
static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
578
int column, int page_addr)
480
static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
481
uint16_t column, int page_addr)
580
struct pxa3xx_nand_info *info = mtd->priv;
581
const struct pxa3xx_nand_cmdset *cmdset = info->cmdset;
584
info->use_dma = (use_dma) ? 1 : 0;
587
info->state = STATE_READY;
589
init_completion(&info->cmd_complete);
484
int addr_cycle, exec_cmd, ndcb0;
485
struct mtd_info *mtd = info->mtd;
491
/* reset data and oob column point to handle data */
497
info->retcode = ERR_NONE;
591
499
switch (command) {
592
case NAND_CMD_READOOB:
593
/* disable HW ECC to get all the OOB data */
594
info->buf_count = mtd->writesize + mtd->oobsize;
595
info->buf_start = mtd->writesize + column;
596
memset(info->data_buff, 0xFF, info->buf_count);
598
if (prepare_read_prog_cmd(info, cmdset->read1, column, page_addr))
601
pxa3xx_nand_do_cmd(info, NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR);
603
/* We only are OOB, so if the data has error, does not matter */
604
if (info->retcode == ERR_DBERR)
605
info->retcode = ERR_NONE;
608
500
case NAND_CMD_READ0:
501
case NAND_CMD_PAGEPROG:
609
502
info->use_ecc = 1;
610
info->retcode = ERR_NONE;
611
info->buf_start = column;
503
case NAND_CMD_READOOB:
504
pxa3xx_set_datasize(info);
516
addr_cycle = NDCB0_ADDR_CYC(info->row_addr_cycles
517
+ info->col_addr_cycles);
520
case NAND_CMD_READOOB:
522
cmd = info->cmdset->read1;
523
if (command == NAND_CMD_READOOB)
524
info->buf_start = mtd->writesize + column;
526
info->buf_start = column;
528
if (unlikely(info->page_size < PAGE_CHUNK_SIZE))
529
info->ndcb0 |= NDCB0_CMD_TYPE(0)
531
| (cmd & NDCB0_CMD1_MASK);
533
info->ndcb0 |= NDCB0_CMD_TYPE(0)
539
/* small page addr setting */
540
if (unlikely(info->page_size < PAGE_CHUNK_SIZE)) {
541
info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
546
info->ndcb1 = ((page_addr & 0xFFFF) << 16)
549
if (page_addr & 0xFF0000)
550
info->ndcb2 = (page_addr & 0xFF0000) >> 16;
612
555
info->buf_count = mtd->writesize + mtd->oobsize;
613
556
memset(info->data_buff, 0xFF, info->buf_count);
615
if (prepare_read_prog_cmd(info, cmdset->read1, column, page_addr))
618
pxa3xx_nand_do_cmd(info, NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR);
620
if (info->retcode == ERR_DBERR) {
621
/* for blank page (all 0xff), HW will calculate its ECC as
622
* 0, which is different from the ECC information within
623
* OOB, ignore such double bit errors
625
if (is_buf_blank(info->data_buff, mtd->writesize))
626
info->retcode = ERR_NONE;
630
info->buf_start = column;
631
info->buf_count = mtd->writesize + mtd->oobsize;
632
memset(info->data_buff, 0xff, info->buf_count);
634
/* save column/page_addr for next CMD_PAGEPROG */
635
info->seqin_column = column;
636
info->seqin_page_addr = page_addr;
638
560
case NAND_CMD_PAGEPROG:
639
info->use_ecc = (info->seqin_column >= mtd->writesize) ? 0 : 1;
641
if (prepare_read_prog_cmd(info, cmdset->program,
642
info->seqin_column, info->seqin_page_addr))
645
pxa3xx_nand_do_cmd(info, NDSR_WRDREQ);
647
case NAND_CMD_ERASE1:
648
if (prepare_erase_cmd(info, cmdset->erase, page_addr))
651
pxa3xx_nand_do_cmd(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
653
case NAND_CMD_ERASE2:
561
if (is_buf_blank(info->data_buff,
562
(mtd->writesize + mtd->oobsize))) {
567
cmd = info->cmdset->program;
568
info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
655
576
case NAND_CMD_READID:
577
cmd = info->cmdset->read_id;
578
info->buf_count = info->read_id_bytes;
579
info->ndcb0 |= NDCB0_CMD_TYPE(3)
656
585
case NAND_CMD_STATUS:
657
info->use_dma = 0; /* force PIO read */
659
info->buf_count = (command == NAND_CMD_READID) ?
660
info->read_id_bytes : 1;
662
if (prepare_other_cmd(info, (command == NAND_CMD_READID) ?
663
cmdset->read_id : cmdset->read_status))
666
pxa3xx_nand_do_cmd(info, NDSR_RDDREQ);
586
cmd = info->cmdset->read_status;
588
info->ndcb0 |= NDCB0_CMD_TYPE(4)
595
case NAND_CMD_ERASE1:
596
cmd = info->cmdset->erase;
597
info->ndcb0 |= NDCB0_CMD_TYPE(2)
602
info->ndcb1 = page_addr;
668
606
case NAND_CMD_RESET:
669
if (prepare_other_cmd(info, cmdset->reset))
672
ret = pxa3xx_nand_do_cmd(info, NDSR_CS0_CMDD);
678
if (nand_readl(info, NDSR) & NDSR_RDY)
683
ndcr = nand_readl(info, NDCR);
684
nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
607
cmd = info->cmdset->reset;
608
info->ndcb0 |= NDCB0_CMD_TYPE(5)
613
case NAND_CMD_ERASE2:
688
printk(KERN_ERR "non-supported command.\n");
619
printk(KERN_ERR "pxa3xx-nand: non-supported"
620
" command %x\n", command);
692
if (info->retcode == ERR_DBERR) {
693
printk(KERN_ERR "double bit error @ page %08x\n", page_addr);
694
info->retcode = ERR_NONE;
627
static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
628
int column, int page_addr)
630
struct pxa3xx_nand_info *info = mtd->priv;
634
* if this is a x16 device ,then convert the input
635
* "byte" address into a "word" address appropriate
636
* for indexing a word-oriented device
638
if (info->reg_ndcr & NDCR_DWIDTH_M)
641
exec_cmd = prepare_command_pool(info, command, column, page_addr);
643
init_completion(&info->cmd_complete);
644
pxa3xx_nand_start(info);
646
ret = wait_for_completion_timeout(&info->cmd_complete,
649
printk(KERN_ERR "Wait time out!!!\n");
650
/* Stop State Machine for next command cycle */
651
pxa3xx_nand_stop(info);
653
info->state = STATE_IDLE;
657
static void pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
658
struct nand_chip *chip, const uint8_t *buf)
660
chip->write_buf(mtd, buf, mtd->writesize);
661
chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
664
static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
665
struct nand_chip *chip, uint8_t *buf, int page)
667
struct pxa3xx_nand_info *info = mtd->priv;
669
chip->read_buf(mtd, buf, mtd->writesize);
670
chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
672
if (info->retcode == ERR_SBERR) {
673
switch (info->use_ecc) {
675
mtd->ecc_stats.corrected++;
681
} else if (info->retcode == ERR_DBERR) {
683
* for blank page (all 0xff), HW will calculate its ECC as
684
* 0, which is different from the ECC information within
685
* OOB, ignore such double bit errors
687
if (is_buf_blank(buf, mtd->writesize))
688
mtd->ecc_stats.failed++;
698
694
static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
772
static void pxa3xx_nand_ecc_hwctl(struct mtd_info *mtd, int mode)
777
static int pxa3xx_nand_ecc_calculate(struct mtd_info *mtd,
778
const uint8_t *dat, uint8_t *ecc_code)
783
static int pxa3xx_nand_ecc_correct(struct mtd_info *mtd,
784
uint8_t *dat, uint8_t *read_ecc, uint8_t *calc_ecc)
786
struct pxa3xx_nand_info *info = mtd->priv;
788
* Any error include ERR_SEND_CMD, ERR_DBERR, ERR_BUSERR, we
789
* consider it as a ecc error which will tell the caller the
790
* read fail We have distinguish all the errors, but the
791
* nand_read_ecc only check this function return value
793
* Corrected (single-bit) errors must also be noted.
795
if (info->retcode == ERR_SBERR)
797
else if (info->retcode != ERR_NONE)
803
static int __readid(struct pxa3xx_nand_info *info, uint32_t *id)
805
const struct pxa3xx_nand_cmdset *cmdset = info->cmdset;
809
if (prepare_other_cmd(info, cmdset->read_id)) {
810
printk(KERN_ERR "failed to prepare command\n");
818
/* Wait for CMDDM(command done successfully) */
819
if (wait_for_event(info, NDSR_RDDREQ))
822
__raw_readsl(info->mmio_base + NDDB, id_buff, 2);
823
*id = id_buff[0] | (id_buff[1] << 8);
827
ndcr = nand_readl(info, NDCR);
828
nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
833
768
static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
834
769
const struct pxa3xx_nand_flash *f)
836
771
struct platform_device *pdev = info->pdev;
837
772
struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
838
uint32_t ndcr = 0x00000FFF; /* disable all interrupts */
773
uint32_t ndcr = 0x0; /* enable all interrupts */
840
775
if (f->page_size != 2048 && f->page_size != 512)
876
810
static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
878
812
uint32_t ndcr = nand_readl(info, NDCR);
879
struct nand_flash_dev *type = NULL;
880
uint32_t id = -1, page_per_block, num_blocks;
883
page_per_block = ndcr & NDCR_PG_PER_BLK ? 64 : 32;
884
813
info->page_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
885
/* set info fields needed to __readid */
814
/* set info fields needed to read id */
886
815
info->read_id_bytes = (info->page_size == 2048) ? 4 : 2;
887
816
info->reg_ndcr = ndcr;
888
817
info->cmdset = &default_cmdset;
890
if (__readid(info, &id))
893
/* Lookup the flash id */
894
id = (id >> 8) & 0xff; /* device id is byte 2 */
895
for (i = 0; nand_flash_ids[i].name != NULL; i++) {
896
if (id == nand_flash_ids[i].id) {
897
type = &nand_flash_ids[i];
905
/* fill the missing flash information */
906
i = __ffs(page_per_block * info->page_size);
907
num_blocks = type->chipsize << (20 - i);
909
/* calculate addressing information */
910
info->col_addr_cycles = (info->page_size == 2048) ? 2 : 1;
912
if (num_blocks * page_per_block > 65536)
913
info->row_addr_cycles = 3;
915
info->row_addr_cycles = 2;
917
819
info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
918
820
info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
923
static int pxa3xx_nand_detect_flash(struct pxa3xx_nand_info *info,
924
const struct pxa3xx_nand_platform_data *pdata)
926
const struct pxa3xx_nand_flash *f;
930
if (pdata->keep_config)
931
if (pxa3xx_nand_detect_config(info) == 0)
934
/* we use default timing to detect id */
935
f = DEFAULT_FLASH_TYPE;
936
pxa3xx_nand_config_flash(info, f);
937
if (__readid(info, &id))
940
for (i=0; i<ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1; i++) {
941
/* we first choose the flash definition from platfrom */
942
if (i < pdata->num_flash)
943
f = pdata->flash + i;
945
f = &builtin_flash_types[i - pdata->num_flash + 1];
946
if (f->chip_id == id) {
947
dev_info(&info->pdev->dev, "detect chip id: 0x%x\n", id);
948
pxa3xx_nand_config_flash(info, f);
953
dev_warn(&info->pdev->dev,
954
"failed to detect configured nand flash; found %04x instead of\n",
960
825
/* the maximum possible buffer size for large page with OOB data
961
826
* is: 2048 + 64 = 2112 bytes, allocate a page here for both the
962
827
* data buffer and the DMA descriptor
1001
static struct nand_ecclayout hw_smallpage_ecclayout = {
1003
.eccpos = {8, 9, 10, 11, 12, 13 },
1004
.oobfree = { {2, 6} }
1007
static struct nand_ecclayout hw_largepage_ecclayout = {
1010
40, 41, 42, 43, 44, 45, 46, 47,
1011
48, 49, 50, 51, 52, 53, 54, 55,
1012
56, 57, 58, 59, 60, 61, 62, 63},
1013
.oobfree = { {2, 38} }
1016
static void pxa3xx_nand_init_mtd(struct mtd_info *mtd,
1017
struct pxa3xx_nand_info *info)
1019
struct nand_chip *this = &info->nand_chip;
1021
this->options = (info->reg_ndcr & NDCR_DWIDTH_C) ? NAND_BUSWIDTH_16: 0;
1023
this->waitfunc = pxa3xx_nand_waitfunc;
1024
this->select_chip = pxa3xx_nand_select_chip;
1025
this->dev_ready = pxa3xx_nand_dev_ready;
1026
this->cmdfunc = pxa3xx_nand_cmdfunc;
1027
this->read_word = pxa3xx_nand_read_word;
1028
this->read_byte = pxa3xx_nand_read_byte;
1029
this->read_buf = pxa3xx_nand_read_buf;
1030
this->write_buf = pxa3xx_nand_write_buf;
1031
this->verify_buf = pxa3xx_nand_verify_buf;
1033
this->ecc.mode = NAND_ECC_HW;
1034
this->ecc.hwctl = pxa3xx_nand_ecc_hwctl;
1035
this->ecc.calculate = pxa3xx_nand_ecc_calculate;
1036
this->ecc.correct = pxa3xx_nand_ecc_correct;
1037
this->ecc.size = info->page_size;
1039
if (info->page_size == 2048)
1040
this->ecc.layout = &hw_largepage_ecclayout;
1042
this->ecc.layout = &hw_smallpage_ecclayout;
1044
this->chip_delay = 25;
1047
static int pxa3xx_nand_probe(struct platform_device *pdev)
1049
struct pxa3xx_nand_platform_data *pdata;
866
static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
868
struct mtd_info *mtd = info->mtd;
869
struct nand_chip *chip = mtd->priv;
871
/* use the common timing to make a try */
872
pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
873
chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
880
static int pxa3xx_nand_scan(struct mtd_info *mtd)
882
struct pxa3xx_nand_info *info = mtd->priv;
883
struct platform_device *pdev = info->pdev;
884
struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
885
struct nand_flash_dev pxa3xx_flash_ids[2] = { {NULL,}, {NULL,} };
886
const struct pxa3xx_nand_flash *f = NULL;
887
struct nand_chip *chip = mtd->priv;
892
if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
895
ret = pxa3xx_nand_sensing(info);
899
printk(KERN_INFO "There is no nand chip on cs 0!\n");
904
chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
905
id = *((uint16_t *)(info->data_buff));
907
printk(KERN_INFO "Detect a flash id %x\n", id);
911
printk(KERN_WARNING "Read out ID 0, potential timing set wrong!!\n");
916
num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
917
for (i = 0; i < num; i++) {
918
if (i < pdata->num_flash)
919
f = pdata->flash + i;
921
f = &builtin_flash_types[i - pdata->num_flash + 1];
923
/* find the chip in default list */
924
if (f->chip_id == id)
928
if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
931
printk(KERN_ERR "ERROR!! flash not defined!!!\n");
936
pxa3xx_nand_config_flash(info, f);
937
pxa3xx_flash_ids[0].name = f->name;
938
pxa3xx_flash_ids[0].id = (f->chip_id >> 8) & 0xffff;
939
pxa3xx_flash_ids[0].pagesize = f->page_size;
940
chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
941
pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
942
pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
943
if (f->flash_width == 16)
944
pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
946
if (nand_scan_ident(mtd, 1, pxa3xx_flash_ids))
948
/* calculate addressing information */
949
info->col_addr_cycles = (mtd->writesize >= 2048) ? 2 : 1;
950
info->oob_buff = info->data_buff + mtd->writesize;
951
if ((mtd->size >> chip->page_shift) > 65536)
952
info->row_addr_cycles = 3;
954
info->row_addr_cycles = 2;
955
mtd->name = mtd_names[0];
956
chip->ecc.mode = NAND_ECC_HW;
957
chip->ecc.size = f->page_size;
959
chip->options = (f->flash_width == 16) ? NAND_BUSWIDTH_16 : 0;
960
chip->options |= NAND_NO_AUTOINCR;
961
chip->options |= NAND_NO_READRDY;
963
return nand_scan_tail(mtd);
967
struct pxa3xx_nand_info *alloc_nand_resource(struct platform_device *pdev)
1050
969
struct pxa3xx_nand_info *info;
1051
struct nand_chip *this;
970
struct nand_chip *chip;
1052
971
struct mtd_info *mtd;
1053
972
struct resource *r;
1056
pdata = pdev->dev.platform_data;
1059
dev_err(&pdev->dev, "no platform data defined\n");
1063
975
mtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct pxa3xx_nand_info),
1066
978
dev_err(&pdev->dev, "failed to allocate memory\n");
1070
982
info = (struct pxa3xx_nand_info *)(&mtd[1]);
983
chip = (struct nand_chip *)(&mtd[1]);
1071
984
info->pdev = pdev;
1073
this = &info->nand_chip;
1074
986
mtd->priv = info;
1075
987
mtd->owner = THIS_MODULE;
989
chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
990
chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
991
chip->controller = &info->controller;
992
chip->waitfunc = pxa3xx_nand_waitfunc;
993
chip->select_chip = pxa3xx_nand_select_chip;
994
chip->dev_ready = pxa3xx_nand_dev_ready;
995
chip->cmdfunc = pxa3xx_nand_cmdfunc;
996
chip->read_word = pxa3xx_nand_read_word;
997
chip->read_byte = pxa3xx_nand_read_byte;
998
chip->read_buf = pxa3xx_nand_read_buf;
999
chip->write_buf = pxa3xx_nand_write_buf;
1000
chip->verify_buf = pxa3xx_nand_verify_buf;
1002
spin_lock_init(&chip->controller->lock);
1003
init_waitqueue_head(&chip->controller->wq);
1077
1004
info->clk = clk_get(&pdev->dev, NULL);
1078
1005
if (IS_ERR(info->clk)) {
1079
1006
dev_err(&pdev->dev, "failed to get nand clock\n");
1226
1118
clk_disable(info->clk);
1227
1119
clk_put(info->clk);
1122
del_mtd_device(mtd);
1123
#ifdef CONFIG_MTD_PARTITIONS
1124
del_mtd_partitions(mtd);
1131
static int pxa3xx_nand_probe(struct platform_device *pdev)
1133
struct pxa3xx_nand_platform_data *pdata;
1134
struct pxa3xx_nand_info *info;
1136
pdata = pdev->dev.platform_data;
1138
dev_err(&pdev->dev, "no platform data defined\n");
1142
info = alloc_nand_resource(pdev);
1146
if (pxa3xx_nand_scan(info->mtd)) {
1147
dev_err(&pdev->dev, "failed to scan nand\n");
1148
pxa3xx_nand_remove(pdev);
1152
#ifdef CONFIG_MTD_PARTITIONS
1153
if (mtd_has_cmdlinepart()) {
1154
const char *probes[] = { "cmdlinepart", NULL };
1155
struct mtd_partition *parts;
1158
nr_parts = parse_mtd_partitions(info->mtd, probes, &parts, 0);
1161
return add_mtd_partitions(info->mtd, parts, nr_parts);
1164
return add_mtd_partitions(info->mtd, pdata->parts, pdata->nr_parts);
1233
1170
#ifdef CONFIG_PM
1234
1171
static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1236
struct mtd_info *mtd = (struct mtd_info *)platform_get_drvdata(pdev);
1237
struct pxa3xx_nand_info *info = mtd->priv;
1173
struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1174
struct mtd_info *mtd = info->mtd;
1239
if (info->state != STATE_READY) {
1240
1177
dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1241
1178
return -EAGAIN;