2
* drivers/mtd/nand/pxa3xx_nand.c
4
* Copyright c 2005 Intel Corporation
5
* Copyright c 2006 Marvell International Ltd.
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.
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>
22
#include <linux/irq.h>
25
#include <mach/dove_nand.h>
26
#include <asm/hardware/pxa-dma.h>
30
#include "pdma/mvPdma.h"
31
#include "pdma/mvPdmaRegs.h"
32
#include "nfc/mvNfc.h"
33
#include "nfc/mvNfcRegs.h"
35
#define NFC_DPRINT(x) /*printk x*/
37
#define CHIP_DELAY_TIMEOUT (20 * HZ/10)
39
#define NFC_SR_MASK (0xfff)
40
#define NFC_SR_BBD_MASK (NFC_SR_CS0_BBD_MASK | NFC_SR_CS1_BBD_MASK)
42
/* error code and state */
61
struct pxa3xx_nand_flash {
62
uint32_t page_per_block;/* Pages per block (PG_PER_BLK) */
63
uint32_t page_size; /* Page size in bytes (PAGE_SZ) */
64
uint32_t flash_width; /* Width of Flash memory (DWIDTH_M) */
69
struct pxa3xx_nand_info {
70
struct nand_chip nand_chip;
72
struct platform_device *pdev;
73
struct pxa3xx_nand_flash flash_info;
76
void __iomem *mmio_base;
77
unsigned int mmio_phys_base;
79
unsigned int buf_start;
80
unsigned int buf_count;
82
unsigned char *data_buff;
83
dma_addr_t data_buff_phys;
84
size_t data_buff_size;
86
/* saved column/page_addr during CMD_SEQIN */
90
/* relate to the command */
93
int use_ecc; /* use HW ECC ? */
94
int use_dma; /* use DMA ? */
95
int use_bch; /* use BCH if use_ecc == 1 */
97
size_t data_size; /* data size in FIFO */
100
struct completion cmd_complete;
108
/* RW buffer chunks config */
109
MV_U32 sgBuffAddr[MV_NFC_RW_MAX_BUFF_NUM];
110
MV_U32 sgBuffSize[MV_NFC_RW_MAX_BUFF_NUM];
113
/* suspend / resume data */
114
MV_U32 nfcUnitData[128];
116
MV_U32 pdmaUnitData[128];
122
static int use_dma = 0;
123
module_param(use_dma, bool, 0444);
124
MODULE_PARM_DESC(use_dma, "enable DMA for data transfering to/from NAND HW");
125
static int use_ecc = 0;
126
module_param(use_ecc, bool, 0444);
127
MODULE_PARM_DESC(use_ecc, "enable ECC calculation/checking for data transfering to/from NAND HW");
128
static int use_bch = 0;
129
module_param(use_bch, bool, 0444);
130
MODULE_PARM_DESC(use_bch, "enable 16bit BCH ECC calculation if ECC operations are enabled");
132
static int prepare_read_prog_cmd(struct pxa3xx_nand_info *info,
133
int column, int page_addr)
137
if (mvNfcFlashPageSizeGet(&info->nfcCtrl, &size, &info->data_size)
144
static int handle_data_pio(struct pxa3xx_nand_info *info)
146
int ret, timeout = CHIP_DELAY_TIMEOUT;
147
NFC_DPRINT((KERN_INFO "handle_data_pio() - state = %d.\n",info->state));
148
switch (info->state) {
149
case STATE_PIO_WRITING:
150
mvNfcReadWrite(&info->nfcCtrl, info->cmd, (MV_U32*)info->data_buff, info->data_buff_phys);
151
mvNfcIntrEnable(&info->nfcCtrl, MV_NFC_STATUS_BBD | MV_NFC_STATUS_CMDD, MV_TRUE);
153
ret = wait_for_completion_timeout(&info->cmd_complete, timeout);
155
printk(KERN_ERR "program command time out\n");
159
case STATE_PIO_READING:
160
NFC_DPRINT((KERN_INFO "handle_data_pio() - data_size = %d.\n",info->data_size));
161
mvNfcReadWrite(&info->nfcCtrl, info->cmd, (MV_U32*)info->data_buff, info->data_buff_phys);
164
printk(KERN_ERR "%s: invalid state %d\n", __func__,
169
info->state = STATE_READY;
173
static void pxa3xx_nand_data_dma_irq(int irq, void *data)
175
struct pxa3xx_nand_info *info = data;
177
int channel = info->nfcCtrl.dataChanHndl.chanNumber;
179
intr = MV_REG_READ(PDMA_INTR_CAUSE_REG);
180
dcsr = MV_REG_READ(PDMA_CTRL_STATUS_REG(channel));
181
MV_REG_WRITE(PDMA_CTRL_STATUS_REG(channel), dcsr);
183
NFC_DPRINT((KERN_INFO "pxa3xx_nand_data_dma_irq(0x%x, 0x%x) - 1.\n", dcsr, intr));
185
if(info->chained_cmd) {
186
if (dcsr & DCSR_BUSERRINTR) {
187
info->retcode = ERR_DMABUSERR;
188
complete(&info->cmd_complete);
190
if ((info->state == STATE_DMA_READING) && (dcsr & DCSR_ENDINTR)) {
191
info->state = STATE_READY;
192
complete(&info->cmd_complete);
197
if (dcsr & DCSR_BUSERRINTR) {
198
info->retcode = ERR_DMABUSERR;
199
complete(&info->cmd_complete);
202
if (info->state == STATE_DMA_WRITING) {
203
info->state = STATE_DMA_DONE;
204
mvNfcIntrEnable(&info->nfcCtrl, MV_NFC_STATUS_BBD | MV_NFC_STATUS_RDY , MV_TRUE);
206
info->state = STATE_READY;
207
complete(&info->cmd_complete);
213
static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
215
struct pxa3xx_nand_info *info = devid;
218
status = MV_REG_READ(NFC_STATUS_REG);
220
NFC_DPRINT((KERN_INFO "pxa3xx_nand_irq(0x%x) - 1.\n", status));
222
if(!info->chained_cmd) {
223
if (status & (NFC_SR_RDDREQ_MASK | NFC_SR_UNCERR_MASK)) {
224
if (status & NFC_SR_UNCERR_MASK)
225
info->retcode = ERR_DBERR;
226
mvNfcIntrEnable(&info->nfcCtrl, NFC_SR_RDDREQ_MASK | NFC_SR_UNCERR_MASK, MV_FALSE);
228
info->state = STATE_DMA_READING;
229
mvNfcReadWrite(&info->nfcCtrl, info->cmd, (MV_U32*)info->data_buff, info->data_buff_phys);
231
info->state = STATE_PIO_READING;
232
complete(&info->cmd_complete);
234
} else if (status & NFC_SR_WRDREQ_MASK) {
235
mvNfcIntrEnable(&info->nfcCtrl, NFC_SR_WRDREQ_MASK, MV_FALSE);
237
info->state = STATE_DMA_WRITING;
238
NFC_DPRINT((KERN_INFO "Calling mvNfcReadWrite().\n"));
239
if (mvNfcReadWrite(&info->nfcCtrl, info->cmd,
240
(MV_U32 *)info->data_buff,
241
info->data_buff_phys)
243
printk(KERN_ERR "mvNfcReadWrite() failed.\n");
245
info->state = STATE_PIO_WRITING;
246
complete(&info->cmd_complete);
248
} else if (status & (NFC_SR_BBD_MASK | MV_NFC_CS0_CMD_DONE_INT |
249
NFC_SR_RDY0_MASK | MV_NFC_CS1_CMD_DONE_INT |
251
if (status & NFC_SR_BBD_MASK)
252
info->retcode = ERR_BBERR;
253
mvNfcIntrEnable(&info->nfcCtrl, MV_NFC_STATUS_BBD |
254
MV_NFC_STATUS_CMDD | MV_NFC_STATUS_RDY,
256
info->state = STATE_READY;
257
complete(&info->cmd_complete);
259
} else if (status & (NFC_SR_BBD_MASK | NFC_SR_RDY0_MASK |
260
NFC_SR_RDY1_MASK | NFC_SR_UNCERR_MASK)) {
261
if (status & (NFC_SR_BBD_MASK | NFC_SR_UNCERR_MASK))
262
info->retcode = ERR_DBERR;
263
mvNfcIntrEnable(&info->nfcCtrl, MV_NFC_STATUS_BBD |
264
MV_NFC_STATUS_RDY | MV_NFC_STATUS_CMDD,
266
if ((info->state != STATE_DMA_READING) ||
267
(info->retcode == ERR_DBERR)) {
268
info->state = STATE_READY;
269
complete(&info->cmd_complete);
272
MV_REG_WRITE(NFC_STATUS_REG, status);
276
static void pxa3xx_nand_set_rw_chunks(struct pxa3xx_nand_info *info)
279
uint32_t spare_size, single_trans;
282
if ((info->nfcCtrl.ifMode != MV_NFC_IF_2X8) &&
283
(info->flash_info.page_size <= 2048)) {
284
/* Non-Ganged mode */
285
info->sgNumBuffs = 1;
291
** need to split the MTD read / write buffer into several buffers
292
** according to what the HW expects.
294
** ---------------------------
295
** | Data (4K) | Spare | ECC |
296
** ---------------------------
298
** -----------------------------------------------------
299
** | Data (2K) | Spare | ECC | Data (2K) | Spare | ECC |
300
** -----------------------------------------------------
302
** In BCH mode, we read only the spare area.
304
if (info->flash_info.page_size == 1024) { /* 2 x 512 */
308
} else if (info->flash_info.page_size == 4096) {/* 2 x 2048 */
312
} else if (info->flash_info.page_size == 8192) {/* 2 x 4096 */
320
for (i = 0; i < (info->flash_info.page_size / single_trans); i++) {
321
info->sgBuffAddr[2 * i] = info->data_buff_phys +
323
info->sgBuffSize[2 * i] = single_trans;
324
info->sgBuffAddr[2 * i + 1] = info->data_buff_phys +
325
info->flash_info.page_size + (oob_size * i);
326
info->sgBuffSize[2 * i + 1] = spare_size;
329
info->sgNumBuffs = idx;
334
static int pxa3xx_nand_do_cmd_multiple(struct pxa3xx_nand_info *info, uint32_t event)
337
int ret, timeout = CHIP_DELAY_TIMEOUT;
340
MV_U32 i, singleBuffSize;
341
MV_NFC_MULTI_CMD descInfo[6], *currDesc;
343
/* Clear all status bits. */
344
MV_REG_WRITE(NFC_STATUS_REG, NFC_SR_MASK);
346
mvNfcIntrEnable(&info->nfcCtrl, event, MV_TRUE);
348
NFC_DPRINT((KERN_INFO "About to issue multi command %d (cs %d) - 0x%x.\n",
349
info->cmd,info->nfcCtrl.currCs, MV_REG_READ(NFC_CONTROL_REG)));
350
if(info->cmd == MV_NFC_CMD_READ_MONOLITHIC)
351
info->state = STATE_DMA_READING;
353
info->state = STATE_CMD_HANDLE;
354
info->chained_cmd = 1;
357
if(info->cmd == MV_NFC_CMD_READ_MONOLITHIC) {
358
mvNfcTransferDataLength(&info->nfcCtrl, info->cmd, &singleBuffSize);
359
numCmds = info->data_size / singleBuffSize;
361
currDesc->cmd = MV_NFC_CMD_READ_MONOLITHIC;
362
currDesc->pageAddr = info->page_addr;
363
currDesc->pageCount = 1;
364
currDesc->virtAddr = (MV_U32*)info->data_buff;
365
currDesc->physAddr = info->data_buff_phys;
366
currDesc->numSgBuffs = ((info->sgNumBuffs == 1) ? 1 : 2);
367
if(currDesc->numSgBuffs > 1) {
368
memcpy(currDesc->sgBuffAddr, info->sgBuffAddr, sizeof(MV_U32) * 2);
369
memcpy(currDesc->sgBuffSize, info->sgBuffSize, sizeof(MV_U32) * 2);
373
for(i = 1; i < numCmds; i++) {
374
if(i == (numCmds - 1)) /* Last desc */
375
currDesc->cmd = MV_NFC_CMD_READ_LAST_NAKED;
377
currDesc->cmd = MV_NFC_CMD_READ_NAKED;
379
currDesc->pageAddr = info->page_addr;
380
currDesc->pageCount = 1;
381
currDesc->virtAddr = (MV_U32 *)(info->data_buff +
382
(i * singleBuffSize));
383
currDesc->physAddr = info->data_buff_phys +
384
(i * singleBuffSize);
385
currDesc->numSgBuffs =
386
((info->sgNumBuffs == 1) ? 1 : 2);
387
if(currDesc->numSgBuffs > 1) {
388
memcpy(currDesc->sgBuffAddr, info->sgBuffAddr +
389
i*2, sizeof(MV_U32) * 2);
390
memcpy(currDesc->sgBuffSize, info->sgBuffSize +
391
i*2, sizeof(MV_U32) * 2);
395
} else if(info->cmd == MV_NFC_CMD_WRITE_MONOLITHIC) {
396
mvNfcTransferDataLength(&info->nfcCtrl, info->cmd, &singleBuffSize);
397
numCmds = info->data_size / singleBuffSize;
398
/* Add 2 commands for Write dispatch start / end */
401
currDesc->cmd = MV_NFC_CMD_WRITE_DISPATCH_START;
402
currDesc->pageAddr = info->page_addr;
403
currDesc->pageCount = 1;
404
currDesc->numSgBuffs = 1;
407
for(i = 0; i < (numCmds - 2); i++) {
408
currDesc->cmd = MV_NFC_CMD_WRITE_NAKED;
409
currDesc->pageAddr = info->page_addr;
410
currDesc->pageCount = 1;
411
currDesc->virtAddr = (MV_U32 *)(info->data_buff +
412
(i * singleBuffSize));
413
currDesc->physAddr = info->data_buff_phys +
414
(i * singleBuffSize);
415
currDesc->numSgBuffs = ((info->sgNumBuffs == 1) ? 1 : 2);
416
if(currDesc->numSgBuffs > 1) {
417
memcpy(currDesc->sgBuffAddr,
418
info->sgBuffAddr + (i * 2),
420
memcpy(currDesc->sgBuffSize,
421
info->sgBuffSize + (i * 2),
426
currDesc->cmd = MV_NFC_CMD_WRITE_DISPATCH_END;
427
currDesc->pageAddr = info->page_addr;
428
currDesc->pageCount = 1;
429
currDesc->numSgBuffs = 1;
431
descInfo[0].cmd = info->cmd;
432
descInfo[0].pageAddr = info->page_addr;
433
descInfo[0].pageCount = 1;
434
descInfo[0].virtAddr = (MV_U32*)info->data_buff;
435
descInfo[0].physAddr = info->data_buff_phys;
436
descInfo[0].numSgBuffs = 1;
439
status = mvNfcCommandMultiple(&info->nfcCtrl,descInfo, numCmds);
440
if(status != MV_OK) {
441
printk(KERN_ERR "mvNfcCommandMultiple() failed for command %d (%d).\n",info->cmd, status);
444
NFC_DPRINT((KERN_INFO "After issue command %d - 0x%x.\n", info->cmd, MV_REG_READ(NFC_STATUS_REG)));
446
ret = wait_for_completion_timeout(&info->cmd_complete, timeout);
448
printk(KERN_ERR "command %d execution timed out (0x%x) - cs %d.\n",info->cmd, MV_REG_READ(NFC_STATUS_REG),
449
info->nfcCtrl.currCs);
450
info->retcode = ERR_SENDCMD;
454
if (info->use_dma == 0 && info->data_size > 0)
455
if (handle_data_pio(info))
458
mvNfcIntrEnable(&info->nfcCtrl, event | MV_NFC_STATUS_CMDD, MV_FALSE);
460
while (MV_PDMA_CHANNEL_STOPPED !=
461
mvPdmaChannelStateGet(&info->nfcCtrl.dataChanHndl)) {
462
if (info->retcode == ERR_NONE)
470
ndcr = MV_REG_READ(NFC_CONTROL_REG);
471
MV_REG_WRITE(NFC_CONTROL_REG, ndcr & ~NFC_CTRL_ND_RUN_MASK);
477
static int pxa3xx_nand_do_cmd(struct pxa3xx_nand_info *info, uint32_t event)
480
int ret, timeout = CHIP_DELAY_TIMEOUT;
483
/* Clear all status bits. */
484
MV_REG_WRITE(NFC_STATUS_REG, NFC_SR_MASK);
485
mvNfcIntrEnable(&info->nfcCtrl, NFC_SR_WRCMDREQ_MASK, MV_FALSE);
486
mvNfcIntrEnable(&info->nfcCtrl, event, MV_TRUE);
488
NFC_DPRINT((KERN_INFO "About to issue command %d (cs %d) - 0x%x.\n",
489
info->cmd,info->nfcCtrl.currCs, MV_REG_READ(NFC_CONTROL_REG)));
490
info->state = STATE_CMD_HANDLE;
492
status = mvNfcCommandIssue(&info->nfcCtrl,info->cmd,
493
info->page_addr,info->column);
494
if(status != MV_OK) {
495
printk(KERN_ERR "mvNfcCommandIssue() failed for command %d (%d).\n",info->cmd, status);
498
NFC_DPRINT((KERN_INFO "After issue command %d - 0x%x.\n", info->cmd, MV_REG_READ(NFC_STATUS_REG)));
500
ret = wait_for_completion_timeout(&info->cmd_complete, timeout);
502
printk(KERN_ERR "command %d execution timed out (0x%x).\n",info->cmd, MV_REG_READ(NFC_STATUS_REG));
503
info->retcode = ERR_SENDCMD;
507
if(info->state != STATE_READY)
508
printk(KERN_INFO "%s - %d - Bad state.\n", __FUNCTION__, __LINE__);
509
if (info->use_dma == 0 && info->data_size > 0)
510
if (handle_data_pio(info))
513
mvNfcIntrEnable(&info->nfcCtrl, event | MV_NFC_STATUS_CMDD, MV_FALSE);
515
if(MV_PDMA_CHANNEL_STOPPED != mvPdmaChannelStateGet(&info->nfcCtrl.dataChanHndl))
521
ndcr = MV_REG_READ(NFC_CONTROL_REG);
522
MV_REG_WRITE(NFC_CONTROL_REG, ndcr & ~NFC_CTRL_ND_RUN_MASK);
528
static int pxa3xx_nand_dev_ready(struct mtd_info *mtd)
530
return (MV_REG_READ(NFC_STATUS_REG) & (NFC_SR_RDY0_MASK | NFC_SR_RDY1_MASK)) ? 1 : 0;
533
static inline int is_buf_blank(uint8_t *buf, size_t len)
535
for (; len > 0; len--)
541
static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
542
int column, int page_addr)
544
struct pxa3xx_nand_info *info = mtd->priv;
545
struct pxa3xx_nand_flash *flash_info = &info->flash_info;
548
info->use_dma = (use_dma) ? 1 : 0;
549
info->use_bch = (use_bch) ? 1 : 0;
552
info->state = STATE_READY;
553
info->chained_cmd = 0;
554
info->retcode = ERR_NONE;
555
init_completion(&info->cmd_complete);
558
case NAND_CMD_READOOB:
559
info->buf_count = mtd->writesize + mtd->oobsize;
560
info->buf_start = mtd->writesize + column;
561
info->cmd = MV_NFC_CMD_READ_MONOLITHIC;
562
info->column = column;
563
info->page_addr = page_addr;
564
if (prepare_read_prog_cmd(info, column, page_addr))
566
pxa3xx_nand_do_cmd_multiple(info,
567
MV_NFC_STATUS_RDY | NFC_SR_UNCERR_MASK);
568
/*pxa3xx_nand_do_cmd(info,
569
NFC_SR_RDDREQ_MASK | NFC_SR_UNCERR_MASK);*/
570
/* We only are OOB, so if the data has error, does not matter */
571
if (info->retcode == ERR_DBERR)
572
info->retcode = ERR_NONE;
577
info->buf_start = column;
578
info->buf_count = mtd->writesize + mtd->oobsize;
579
memset(info->data_buff, 0xff, info->buf_count);
580
info->cmd = MV_NFC_CMD_READ_MONOLITHIC;
581
info->column = column;
582
info->page_addr = page_addr;
583
if (prepare_read_prog_cmd(info, column, page_addr))
585
pxa3xx_nand_do_cmd_multiple(info,
586
MV_NFC_STATUS_RDY | NFC_SR_UNCERR_MASK);
587
/*pxa3xx_nand_do_cmd(info,
588
NFC_SR_RDDREQ_MASK | NFC_SR_UNCERR_MASK);*/
589
if (info->retcode == ERR_DBERR) {
590
/* for blank page (all 0xff), HW will calculate its ECC as
591
* 0, which is different from the ECC information within
592
* OOB, ignore such double bit errors
594
if (is_buf_blank(info->data_buff, mtd->writesize))
595
info->retcode = ERR_NONE;
597
printk(KERN_INFO "%s: retCode == ERR_DBERR\n", __FUNCTION__);
601
info->buf_start = column;
602
info->buf_count = mtd->writesize + mtd->oobsize;
603
memset(info->data_buff + mtd->writesize, 0xff, mtd->oobsize);
605
/* save column/page_addr for next CMD_PAGEPROG */
606
info->seqin_column = column;
607
info->seqin_page_addr = page_addr;
609
case NAND_CMD_PAGEPROG:
610
info->use_ecc = (info->seqin_column >= mtd->writesize) ? 0 : 1;
611
info->column = info->seqin_column;
612
info->page_addr = info->seqin_page_addr;
613
info->cmd = MV_NFC_CMD_WRITE_MONOLITHIC;
614
if (prepare_read_prog_cmd(info,
615
info->seqin_column, info->seqin_page_addr)) {
616
printk(KERN_ERR "prepare_read_prog_cmd() failed.\n");
619
pxa3xx_nand_do_cmd_multiple(info, MV_NFC_STATUS_RDY);
620
/*pxa3xx_nand_do_cmd(info, NFC_SR_WRDREQ_MASK);*/
622
case NAND_CMD_ERASE1:
624
info->page_addr = page_addr;
625
info->cmd = MV_NFC_CMD_ERASE;
627
pxa3xx_nand_do_cmd_multiple(info, MV_NFC_STATUS_BBD | MV_NFC_STATUS_RDY);
628
/*pxa3xx_nand_do_cmd(info, MV_NFC_STATUS_BBD | MV_NFC_STATUS_RDY);*/
630
case NAND_CMD_ERASE2:
632
case NAND_CMD_READID:
633
case NAND_CMD_STATUS:
635
info->buf_count = (command == NAND_CMD_READID) ?
636
flash_info->read_id_bytes : 1;
640
info->cmd = (command == NAND_CMD_READID) ?
641
MV_NFC_CMD_READ_ID : MV_NFC_CMD_READ_STATUS;
642
pxa3xx_nand_do_cmd(info,NFC_SR_RDDREQ_MASK);
647
info->cmd = MV_NFC_CMD_RESET;
648
ret = pxa3xx_nand_do_cmd_multiple(info, MV_NFC_STATUS_CMDD);
649
/*ret = pxa3xx_nand_do_cmd(info, MV_NFC_STATUS_CMDD);*/
655
if (MV_REG_READ(NFC_STATUS_REG) & (NFC_SR_RDY0_MASK | NFC_SR_RDY1_MASK))
660
ndcr = MV_REG_READ(NFC_CONTROL_REG);
661
MV_REG_WRITE(NFC_CONTROL_REG, ndcr & ~NFC_CTRL_ND_RUN_MASK);
665
printk(KERN_ERR "non-supported command.\n");
669
if (info->retcode == ERR_DBERR) {
670
printk(KERN_ERR "double bit error @ page %08x\n", page_addr);
671
info->retcode = ERR_NONE;
675
static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
677
struct pxa3xx_nand_info *info = mtd->priv;
680
if (info->buf_start < info->buf_count)
681
/* Has just send a new command? */
682
retval = info->data_buff[info->buf_start++];
686
static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
688
struct pxa3xx_nand_info *info = mtd->priv;
691
if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
692
retval = *((u16 *)(info->data_buff+info->buf_start));
693
info->buf_start += 2;
696
printk(KERN_ERR "\n%s: returning 0xFFFF (%d, %d).\n",__FUNCTION__, info->buf_start,info->buf_count);
701
static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
703
struct pxa3xx_nand_info *info = mtd->priv;
704
int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
706
memcpy(buf, info->data_buff + info->buf_start, real_len);
707
info->buf_start += real_len;
710
static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
711
const uint8_t *buf, int len)
713
struct pxa3xx_nand_info *info = mtd->priv;
714
int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
716
memcpy(info->data_buff + info->buf_start, buf, real_len);
717
info->buf_start += real_len;
720
static int pxa3xx_nand_verify_buf(struct mtd_info *mtd,
721
const uint8_t *buf, int len)
726
static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
728
struct pxa3xx_nand_info *info = mtd->priv;
729
mvNfcSelectChip(&info->nfcCtrl, MV_NFC_CS_0 + chip);
733
static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
735
struct pxa3xx_nand_info *info = mtd->priv;
737
/* pxa3xx_nand_send_command has waited for command complete */
738
if (this->state == FL_WRITING || this->state == FL_ERASING) {
739
if (info->retcode == ERR_NONE)
743
* any error make it return 0x01 which will tell
744
* the caller the erase and write fail
753
static void pxa3xx_nand_ecc_hwctl(struct mtd_info *mtd, int mode)
758
static int pxa3xx_nand_ecc_calculate(struct mtd_info *mtd,
759
const uint8_t *dat, uint8_t *ecc_code)
764
static int pxa3xx_nand_ecc_correct(struct mtd_info *mtd,
765
uint8_t *dat, uint8_t *read_ecc, uint8_t *calc_ecc)
767
struct pxa3xx_nand_info *info = mtd->priv;
769
* Any error include ERR_SEND_CMD, ERR_DBERR, ERR_BUSERR, we
770
* consider it as a ecc error which will tell the caller the
771
* read fail We have distinguish all the errors, but the
772
* nand_read_ecc only check this function return value
774
if (info->retcode != ERR_NONE)
780
static int pxa3xx_nand_detect_flash(struct pxa3xx_nand_info *info)
782
struct platform_device *pdev = info->pdev;
783
struct dove_nand_platform_data *pdata = pdev->dev.platform_data;
784
struct pxa3xx_nand_flash *f = &info->flash_info;
786
mvNfcFlashPageSizeGet(&info->nfcCtrl, &f->page_size, NULL);
787
if (f->page_size != 8192 && f->page_size != 4096 &&
788
f->page_size != 2048 && f->page_size != 512)
791
f->flash_width = pdata->nfc_width;
792
if (f->flash_width != 16 && f->flash_width != 8)
795
/* calculate flash information */
796
f->read_id_bytes = (f->page_size >= 2048) ? 4 : 2;
797
f->num_devs = pdata->num_devs;
802
/* the maximum possible buffer size for large page with OOB data
803
* is: 4096 + 128 = 4224 bytes, allocate a page here for both the
804
* data buffer and the DMA descriptor
805
* Multiply by 2 for Ganged mode.
807
#define MAX_BUFF_SIZE (PAGE_SIZE << 2)
809
static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
811
struct platform_device *pdev = info->pdev;
815
info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
816
if (info->data_buff == NULL)
821
info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
822
&info->data_buff_phys, GFP_KERNEL);
823
if (info->data_buff == NULL) {
824
dev_err(&pdev->dev, "failed to allocate dma buffer\n");
827
memset(info->data_buff, 0xff, MAX_BUFF_SIZE);
829
ret = pxa_request_dma_intr ("nand-data", info->nfcCtrl.dataChanHndl.chanNumber,
830
pxa3xx_nand_data_dma_irq, info);
832
dev_err(&pdev->dev, "failed to request PDMA IRQ\n");
838
static struct nand_ecclayout hw_smallpage_ecclayout = {
840
.eccpos = {8, 9, 10, 11, 12, 13 },
841
.oobfree = { {2, 6} }
844
static struct nand_ecclayout hw_largepage_ecclayout = {
847
40, 41, 42, 43, 44, 45, 46, 47,
848
48, 49, 50, 51, 52, 53, 54, 55,
849
56, 57, 58, 59, 60, 61, 62, 63},
850
.oobfree = { {2, 38} }
853
static struct nand_ecclayout hw_largepage_bch_ecclayout = {
856
32, 33, 34, 35, 36, 37, 38, 39,
857
40, 41, 42, 43, 44, 45, 46, 47,
858
48, 49, 50, 51, 52, 53, 54, 55,
859
56, 57, 58, 59, 60, 61, 62, 63},
860
.oobfree = { {2, 30} }
863
static struct nand_ecclayout hw_ganged_largepage_bch_ecclayout = {
866
32, 33, 34, 35, 36, 37, 38, 39,
867
40, 41, 42, 43, 44, 45, 46, 47,
868
48, 49, 50, 51, 52, 53, 54, 55,
869
56, 57, 58, 59, 60, 61, 62, 63,
870
96, 97, 98, 99, 100, 101, 102, 103,
871
104, 105, 106, 107, 108, 109, 110, 111,
872
112, 113, 114, 115, 116, 117, 118, 119,
873
120, 121, 122, 123, 124, 125, 126, 127},
874
/* Bootrom looks in bytes 0 & 5 for bad blocks */
875
.oobfree = { {1, 4}, {6, 26}, { 64, 32} }
878
static struct nand_ecclayout hw_ganged_largepage_8k_bch_ecclayout = {
881
32, 33, 34, 35, 36, 37, 38, 39,
882
40, 41, 42, 43, 44, 45, 46, 47,
883
48, 49, 50, 51, 52, 53, 54, 55,
884
56, 57, 58, 59, 60, 61, 62, 63,
886
96, 97, 98, 99, 100, 101, 102, 103,
887
104, 105, 106, 107, 108, 109, 110, 111,
888
112, 113, 114, 115, 116, 117, 118, 119,
889
120, 121, 122, 123, 124, 125, 126, 127,
891
160, 161, 162, 163, 164, 165, 166, 167,
892
168, 169, 170, 171, 172, 173, 174, 175,
893
176, 177, 178, 179, 180, 181, 182, 183,
894
184, 185, 186, 187, 188, 189, 190, 191,
896
224, 225, 226, 227, 228, 229, 230, 231,
897
232, 233, 234, 235, 236, 237, 238, 239,
898
240, 241, 242, 243, 244, 245, 246, 247,
899
248, 249, 250, 251, 252, 253, 254, 255},
901
/* Bootrom looks in bytes 0 & 5 for bad blocks */
902
.oobfree = { {1, 4}, {6, 26}, { 64, 32}, {128, 32}, {192, 32} }
905
static uint8_t mv_bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
906
static uint8_t mv_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
908
static struct nand_bbt_descr mvbbt_main_descr = {
909
.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
910
| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
914
.maxblocks = 8, /* Last 8 blocks in each chip */
915
.pattern = mv_bbt_pattern
918
static struct nand_bbt_descr mvbbt_mirror_descr = {
919
.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
920
| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
924
.maxblocks = 8, /* Last 8 blocks in each chip */
925
.pattern = mv_mirror_pattern
928
static void pxa3xx_nand_init_mtd(struct mtd_info *mtd,
929
struct pxa3xx_nand_info *info)
931
struct pxa3xx_nand_flash *f = &info->flash_info;
932
struct nand_chip *this = &info->nand_chip;
933
struct dove_nand_platform_data *pdata = info->pdev->dev.platform_data;
935
this->options = (f->flash_width == 16) ? NAND_BUSWIDTH_16: 0;
937
this->num_devs = pdata->num_devs;
938
this->options |= NAND_USE_FLASH_BBT;
940
this->waitfunc = pxa3xx_nand_waitfunc;
941
this->select_chip = pxa3xx_nand_select_chip;
942
this->dev_ready = pxa3xx_nand_dev_ready;
943
this->cmdfunc = pxa3xx_nand_cmdfunc;
944
this->read_word = pxa3xx_nand_read_word;
945
this->read_byte = pxa3xx_nand_read_byte;
946
this->read_buf = pxa3xx_nand_read_buf;
947
this->write_buf = pxa3xx_nand_write_buf;
948
this->verify_buf = pxa3xx_nand_verify_buf;
950
this->ecc.mode = NAND_ECC_HW;
951
this->ecc.hwctl = pxa3xx_nand_ecc_hwctl;
952
this->ecc.calculate = pxa3xx_nand_ecc_calculate;
953
this->ecc.correct = pxa3xx_nand_ecc_correct;
954
this->ecc.size = f->page_size;
956
if (f->page_size >= 2048) {
957
if (info->nfcCtrl.ifMode != MV_NFC_IF_2X8) {
958
if (f->page_size < 4096) {
961
&hw_largepage_bch_ecclayout;
964
&hw_largepage_ecclayout;
967
&hw_ganged_largepage_bch_ecclayout;
969
if (f->page_size <= 4096)
971
&hw_ganged_largepage_bch_ecclayout;
974
&hw_ganged_largepage_8k_bch_ecclayout;
977
this->ecc.layout = &hw_smallpage_ecclayout;
979
this->bbt_td = &mvbbt_main_descr;
980
this->bbt_md = &mvbbt_mirror_descr;
982
this->chip_delay = 25;
985
static int pxa3xx_nand_probe(struct platform_device *pdev)
987
struct dove_nand_platform_data *pdata;
988
struct pxa3xx_nand_info *info;
989
struct nand_chip *this;
990
struct mtd_info *mtd;
993
char * stat[2] = {"Disabled", "Enabled"};
996
pdata = pdev->dev.platform_data;
999
dev_err(&pdev->dev, "no platform data defined\n");
1003
/* Set global parameters based on platform data */
1004
if (pdata->use_dma) use_dma = 1;
1005
if (pdata->use_ecc) use_ecc = 1;
1006
if (pdata->use_bch) use_bch = 1;
1008
dev_info(&pdev->dev, "Initialize HAL based NFC in %dbit mode with "
1009
"DMA %s, ECC %s, BCH %s\n",
1010
pdata->nfc_width, stat[use_dma], stat[use_ecc],
1013
mtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct pxa3xx_nand_info),
1016
dev_err(&pdev->dev, "failed to allocate memory\n");
1020
info = (struct pxa3xx_nand_info *)(&mtd[1]);
1023
this = &info->nand_chip;
1025
mtd->owner = THIS_MODULE;
1027
info->clk = clk_get_sys("dove-nand", NULL);
1028
if (IS_ERR(info->clk)) {
1029
dev_err(&pdev->dev, "failed to get nand clock\n");
1030
ret = PTR_ERR(info->clk);
1033
clk_enable(info->clk);
1035
irq = platform_get_irq(pdev, 0);
1037
dev_err(&pdev->dev, "no IRQ resource defined\n");
1042
r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1044
dev_err(&pdev->dev, "no IO memory resource defined\n");
1049
r = devm_request_mem_region(&pdev->dev, r->start, r->end - r->start + 1,
1052
dev_err(&pdev->dev, "failed to request memory resource\n");
1057
info->mmio_base = devm_ioremap(&pdev->dev, r->start,
1058
r->end - r->start + 1);
1059
if (info->mmio_base == NULL) {
1060
dev_err(&pdev->dev, "ioremap() failed\n");
1065
info->mmio_phys_base = r->start;
1068
if (mvPdmaHalInit(MV_PDMA_MAX_CHANNELS_NUM) != MV_OK) {
1069
dev_err(&pdev->dev, "mvPdmaHalInit() failed.\n");
1073
/* Initialize NFC HAL */
1074
nfcInfo.ioMode = (use_dma ? MV_NFC_PDMA_ACCESS : MV_NFC_PIO_ACCESS);
1077
nfcInfo.eccMode = MV_NFC_ECC_BCH;
1079
nfcInfo.eccMode = MV_NFC_ECC_HAMMING;
1081
nfcInfo.eccMode = MV_NFC_ECC_DISABLE;
1083
if(pdata->num_devs == 1)
1084
nfcInfo.ifMode = ((pdata->nfc_width == 8) ? MV_NFC_IF_1X8 : MV_NFC_IF_1X16);
1086
nfcInfo.ifMode = MV_NFC_IF_2X8;
1087
nfcInfo.autoStatusRead = MV_FALSE;
1088
nfcInfo.tclk = pdata->tclk;
1089
nfcInfo.readyBypass = MV_FALSE;
1090
nfcInfo.osHandle = NULL;
1091
nfcInfo.regsPhysAddr = DOVE_SB_REGS_PHYS_BASE;
1092
nfcInfo.dataPdmaIntMask = MV_PDMA_END_OF_RX_INTR_EN | MV_PDMA_END_INTR_EN;
1093
nfcInfo.cmdPdmaIntMask = 0x0;
1094
if (mvNfcInit(&nfcInfo, &info->nfcCtrl) != MV_OK) {
1095
dev_err(&pdev->dev, "mvNfcInit() failed.\n");
1099
mvNfcSelectChip(&info->nfcCtrl, MV_NFC_CS_0);
1100
mvNfcIntrEnable(&info->nfcCtrl, 0xFFF, MV_FALSE);
1101
mvNfcSelectChip(&info->nfcCtrl, MV_NFC_CS_1);
1102
mvNfcIntrEnable(&info->nfcCtrl, 0xFFF, MV_FALSE);
1103
mvNfcSelectChip(&info->nfcCtrl, MV_NFC_CS_NONE);
1105
ret = pxa3xx_nand_init_buff(info);
1109
/* Clear all old events on the status register */
1110
MV_REG_WRITE(NFC_STATUS_REG, MV_REG_READ(NFC_STATUS_REG));
1111
ret = request_irq(IRQ_NAND, pxa3xx_nand_irq, IRQF_DISABLED,
1114
dev_err(&pdev->dev, "failed to request IRQ\n");
1118
ret = pxa3xx_nand_detect_flash(info);
1120
dev_err(&pdev->dev, "failed to detect flash\n");
1125
pxa3xx_nand_set_rw_chunks(info);
1127
pxa3xx_nand_init_mtd(mtd, info);
1129
platform_set_drvdata(pdev, mtd);
1131
if (nand_scan(mtd, pdata->num_cs)) {
1132
dev_err(&pdev->dev, "failed to scan nand\n");
1137
return add_mtd_partitions(mtd, pdata->parts, pdata->nr_parts);
1140
free_irq(IRQ_NAND, info);
1143
dma_free_coherent(&pdev->dev, info->data_buff_size,
1144
info->data_buff, info->data_buff_phys);
1146
kfree(info->data_buff);
1148
clk_disable(info->clk);
1155
static int pxa3xx_nand_remove(struct platform_device *pdev)
1157
struct mtd_info *mtd = platform_get_drvdata(pdev);
1158
struct pxa3xx_nand_info *info = mtd->priv;
1160
platform_set_drvdata(pdev, NULL);
1162
del_mtd_device(mtd);
1163
del_mtd_partitions(mtd);
1164
free_irq(IRQ_NAND, info);
1166
dma_free_writecombine(&pdev->dev, info->data_buff_size,
1167
info->data_buff, info->data_buff_phys);
1169
kfree(info->data_buff);
1171
clk_disable(info->clk);
1178
static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1180
struct mtd_info *mtd = (struct mtd_info *)platform_get_drvdata(pdev);
1181
struct pxa3xx_nand_info *info = mtd->priv;
1183
if (info->state != STATE_READY) {
1184
dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1188
/* Store PDMA registers. */
1189
info->pdmaDataLen = 128;
1190
mvPdmaUnitStateStore(info->pdmaUnitData, &info->pdmaDataLen);
1192
/* Store NFC registers. */
1193
info->nfcDataLen = 128;
1194
mvNfcUnitStateStore(info->nfcUnitData, &info->nfcDataLen);
1196
clk_disable(info->clk);
1200
static int pxa3xx_nand_resume(struct platform_device *pdev)
1202
struct mtd_info *mtd = (struct mtd_info *)platform_get_drvdata(pdev);
1203
struct pxa3xx_nand_info *info = mtd->priv;
1206
clk_enable(info->clk);
1208
/* restore PDMA registers */
1209
for(i = 0; i < info->pdmaDataLen; i+=2)
1210
MV_REG_WRITE(info->pdmaUnitData[i], info->pdmaUnitData[i+1]);
1212
/* Clear all NAND interrupts */
1213
MV_REG_WRITE(NFC_STATUS_REG, MV_REG_READ(NFC_STATUS_REG));
1215
/* restore NAND registers */
1216
for(i = 0; i < info->nfcDataLen; i+=2)
1217
MV_REG_WRITE(info->nfcUnitData[i], info->nfcUnitData[i+1]);
1222
#define pxa3xx_nand_suspend NULL
1223
#define pxa3xx_nand_resume NULL
1226
static struct platform_driver pxa3xx_nand_driver = {
1228
.name = "dove-nand-hal",
1229
.owner = THIS_MODULE,
1231
.probe = pxa3xx_nand_probe,
1232
.remove = pxa3xx_nand_remove,
1233
.suspend = pxa3xx_nand_suspend,
1234
.resume = pxa3xx_nand_resume,
1237
static int __init pxa3xx_nand_init(void)
1239
return platform_driver_register(&pxa3xx_nand_driver);
1241
module_init(pxa3xx_nand_init);
1243
static void __exit pxa3xx_nand_exit(void)
1245
platform_driver_unregister(&pxa3xx_nand_driver);
1247
module_exit(pxa3xx_nand_exit);
1249
MODULE_LICENSE("GPL");
1250
MODULE_DESCRIPTION("Dove NAND controller driver");