Skip to content

Commit 401e67e

Browse files
Lei WenDavid Woodhouse
authored andcommitted
mtd: pxa3xx_nand: mtd scan id process could be defined by driver itself
Different NAND driver may require its unique detection. For pxa3xx_nand, it use its self id database to get the necessary info. Signed-off-by: Lei Wen <[email protected]> Signed-off-by: Haojian Zhuang <[email protected]> Signed-off-by: David Woodhouse <[email protected]>
1 parent 4eb2da8 commit 401e67e

File tree

1 file changed

+123
-93
lines changed

1 file changed

+123
-93
lines changed

drivers/mtd/nand/pxa3xx_nand.c

Lines changed: 123 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ enum {
123123
struct pxa3xx_nand_info {
124124
struct nand_chip nand_chip;
125125

126+
struct nand_hw_control controller;
126127
struct platform_device *pdev;
127128
struct pxa3xx_nand_cmdset *cmdset;
128129

@@ -157,6 +158,7 @@ struct pxa3xx_nand_info {
157158

158159
int use_ecc; /* use HW ECC ? */
159160
int use_dma; /* use DMA ? */
161+
int is_ready;
160162

161163
unsigned int page_size; /* page size of attached chip */
162164
unsigned int data_size; /* data size in FIFO */
@@ -223,6 +225,8 @@ static struct pxa3xx_nand_flash builtin_flash_types[] = {
223225
/* Define a default flash type setting serve as flash detecting only */
224226
#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
225227

228+
const char *mtd_names[] = {"pxa3xx_nand-0", NULL};
229+
226230
#define NDTR0_tCH(c) (min((c), 7) << 19)
227231
#define NDTR0_tCS(c) (min((c), 7) << 16)
228232
#define NDTR0_tWH(c) (min((c), 7) << 11)
@@ -437,8 +441,10 @@ static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
437441
info->state = STATE_CMD_DONE;
438442
is_completed = 1;
439443
}
440-
if (status & NDSR_FLASH_RDY)
444+
if (status & NDSR_FLASH_RDY) {
445+
info->is_ready = 1;
441446
info->state = STATE_READY;
447+
}
442448

443449
if (status & NDSR_WRCMDREQ) {
444450
nand_writel(info, NDSR, NDSR_WRCMDREQ);
@@ -483,10 +489,11 @@ static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
483489
exec_cmd = 1;
484490

485491
/* reset data and oob column point to handle data */
486-
info->buf_start = 0;
487-
info->buf_count = 0;
492+
info->buf_start = 0;
493+
info->buf_count = 0;
488494
info->oob_size = 0;
489495
info->use_ecc = 0;
496+
info->is_ready = 0;
490497
info->retcode = ERR_NONE;
491498

492499
switch (command) {
@@ -849,42 +856,6 @@ static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
849856
return 0;
850857
}
851858

852-
static int pxa3xx_nand_detect_flash(struct pxa3xx_nand_info *info,
853-
const struct pxa3xx_nand_platform_data *pdata)
854-
{
855-
const struct pxa3xx_nand_flash *f;
856-
uint32_t id = -1;
857-
int i;
858-
859-
if (pdata->keep_config)
860-
if (pxa3xx_nand_detect_config(info) == 0)
861-
return 0;
862-
863-
/* we use default timing to detect id */
864-
f = DEFAULT_FLASH_TYPE;
865-
pxa3xx_nand_config_flash(info, f);
866-
pxa3xx_nand_cmdfunc(info->mtd, NAND_CMD_READID, 0, 0);
867-
id = *((uint16_t *)(info->data_buff));
868-
869-
for (i=0; i<ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1; i++) {
870-
/* we first choose the flash definition from platfrom */
871-
if (i < pdata->num_flash)
872-
f = pdata->flash + i;
873-
else
874-
f = &builtin_flash_types[i - pdata->num_flash + 1];
875-
if (f->chip_id == id) {
876-
dev_info(&info->pdev->dev, "detect chip id: 0x%x\n", id);
877-
pxa3xx_nand_config_flash(info, f);
878-
return 0;
879-
}
880-
}
881-
882-
dev_warn(&info->pdev->dev,
883-
"failed to detect configured nand flash; found %04x instead of\n",
884-
id);
885-
return -ENODEV;
886-
}
887-
888859
/* the maximum possible buffer size for large page with OOB data
889860
* is: 2048 + 64 = 2112 bytes, allocate a page here for both the
890861
* data buffer and the DMA descriptor
@@ -926,57 +897,110 @@ static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
926897
return 0;
927898
}
928899

929-
static struct nand_ecclayout hw_smallpage_ecclayout = {
930-
.eccbytes = 6,
931-
.eccpos = {8, 9, 10, 11, 12, 13 },
932-
.oobfree = { {2, 6} }
933-
};
900+
static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
901+
{
902+
struct mtd_info *mtd = info->mtd;
903+
struct nand_chip *chip = mtd->priv;
934904

935-
static struct nand_ecclayout hw_largepage_ecclayout = {
936-
.eccbytes = 24,
937-
.eccpos = {
938-
40, 41, 42, 43, 44, 45, 46, 47,
939-
48, 49, 50, 51, 52, 53, 54, 55,
940-
56, 57, 58, 59, 60, 61, 62, 63},
941-
.oobfree = { {2, 38} }
942-
};
905+
/* use the common timing to make a try */
906+
pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
907+
chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
908+
if (info->is_ready)
909+
return 1;
910+
else
911+
return 0;
912+
}
943913

944-
static void pxa3xx_nand_init_mtd(struct mtd_info *mtd,
945-
struct pxa3xx_nand_info *info)
914+
static int pxa3xx_nand_scan(struct mtd_info *mtd)
946915
{
947-
struct nand_chip *this = &info->nand_chip;
948-
949-
this->options = (info->reg_ndcr & NDCR_DWIDTH_C) ? NAND_BUSWIDTH_16: 0;
950-
this->options |= NAND_NO_AUTOINCR;
951-
952-
this->waitfunc = pxa3xx_nand_waitfunc;
953-
this->select_chip = pxa3xx_nand_select_chip;
954-
this->dev_ready = pxa3xx_nand_dev_ready;
955-
this->cmdfunc = pxa3xx_nand_cmdfunc;
956-
this->ecc.read_page = pxa3xx_nand_read_page_hwecc;
957-
this->ecc.write_page = pxa3xx_nand_write_page_hwecc;
958-
this->read_word = pxa3xx_nand_read_word;
959-
this->read_byte = pxa3xx_nand_read_byte;
960-
this->read_buf = pxa3xx_nand_read_buf;
961-
this->write_buf = pxa3xx_nand_write_buf;
962-
this->verify_buf = pxa3xx_nand_verify_buf;
963-
964-
this->ecc.mode = NAND_ECC_HW;
965-
this->ecc.size = info->page_size;
966-
967-
if (info->page_size == 2048)
968-
this->ecc.layout = &hw_largepage_ecclayout;
969-
else
970-
this->ecc.layout = &hw_smallpage_ecclayout;
916+
struct pxa3xx_nand_info *info = mtd->priv;
917+
struct platform_device *pdev = info->pdev;
918+
struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
919+
const struct pxa3xx_nand_flash *f = NULL;
920+
struct nand_chip *chip = mtd->priv;
921+
uint32_t id = -1;
922+
int i, ret, num;
923+
924+
if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
925+
return 0;
926+
927+
ret = pxa3xx_nand_sensing(info);
928+
if (!ret) {
929+
kfree(mtd);
930+
info->mtd = NULL;
931+
printk(KERN_INFO "There is no nand chip on cs 0!\n");
932+
933+
return -EINVAL;
934+
}
935+
936+
chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
937+
id = *((uint16_t *)(info->data_buff));
938+
if (id != 0)
939+
printk(KERN_INFO "Detect a flash id %x\n", id);
940+
else {
941+
kfree(mtd);
942+
info->mtd = NULL;
943+
printk(KERN_WARNING "Read out ID 0, potential timing set wrong!!\n");
944+
945+
return -EINVAL;
946+
}
947+
948+
num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
949+
for (i = 0; i < num; i++) {
950+
if (i < pdata->num_flash)
951+
f = pdata->flash + i;
952+
else
953+
f = &builtin_flash_types[i - pdata->num_flash + 1];
954+
955+
/* find the chip in default list */
956+
if (f->chip_id == id) {
957+
pxa3xx_nand_config_flash(info, f);
958+
mtd->writesize = f->page_size;
959+
mtd->writesize_shift = ffs(mtd->writesize) - 1;
960+
mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
961+
mtd->oobsize = mtd->writesize / 32;
962+
mtd->erasesize = f->page_size * f->page_per_block;
963+
mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
964+
mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
965+
966+
mtd->name = mtd_names[0];
967+
break;
968+
}
969+
}
970+
971+
if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash)) {
972+
kfree(mtd);
973+
info->mtd = NULL;
974+
printk(KERN_ERR "ERROR!! flash not defined!!!\n");
975+
976+
return -EINVAL;
977+
}
978+
979+
chip->ecc.mode = NAND_ECC_HW;
980+
chip->ecc.size = f->page_size;
981+
chip->chipsize = (uint64_t)f->num_blocks * f->page_per_block
982+
* f->page_size;
983+
mtd->size = chip->chipsize;
984+
985+
/* Calculate the address shift from the page size */
986+
chip->page_shift = ffs(mtd->writesize) - 1;
987+
chip->pagemask = mtd_div_by_ws(chip->chipsize, mtd) - 1;
988+
chip->numchips = 1;
989+
chip->phys_erase_shift = ffs(mtd->erasesize) - 1;
990+
chip->bbt_erase_shift = chip->phys_erase_shift;
991+
992+
chip->options = (f->flash_width == 16) ? NAND_BUSWIDTH_16 : 0;
993+
chip->options |= NAND_NO_AUTOINCR;
994+
chip->options |= NAND_NO_READRDY;
971995

972-
this->chip_delay = 25;
996+
return nand_scan_tail(mtd);
973997
}
974998

975999
static
9761000
struct pxa3xx_nand_info *alloc_nand_resource(struct platform_device *pdev)
9771001
{
978-
struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
9791002
struct pxa3xx_nand_info *info;
1003+
struct nand_chip *chip;
9801004
struct mtd_info *mtd;
9811005
struct resource *r;
9821006
int ret, irq;
@@ -989,12 +1013,27 @@ struct pxa3xx_nand_info *alloc_nand_resource(struct platform_device *pdev)
9891013
}
9901014

9911015
info = (struct pxa3xx_nand_info *)(&mtd[1]);
1016+
chip = (struct nand_chip *)(&mtd[1]);
9921017
info->pdev = pdev;
993-
994-
mtd->priv = info;
9951018
info->mtd = mtd;
1019+
mtd->priv = info;
9961020
mtd->owner = THIS_MODULE;
9971021

1022+
chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1023+
chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1024+
chip->controller = &info->controller;
1025+
chip->waitfunc = pxa3xx_nand_waitfunc;
1026+
chip->select_chip = pxa3xx_nand_select_chip;
1027+
chip->dev_ready = pxa3xx_nand_dev_ready;
1028+
chip->cmdfunc = pxa3xx_nand_cmdfunc;
1029+
chip->read_word = pxa3xx_nand_read_word;
1030+
chip->read_byte = pxa3xx_nand_read_byte;
1031+
chip->read_buf = pxa3xx_nand_read_buf;
1032+
chip->write_buf = pxa3xx_nand_write_buf;
1033+
chip->verify_buf = pxa3xx_nand_verify_buf;
1034+
1035+
spin_lock_init(&chip->controller->lock);
1036+
init_waitqueue_head(&chip->controller->wq);
9981037
info->clk = clk_get(&pdev->dev, NULL);
9991038
if (IS_ERR(info->clk)) {
10001039
dev_err(&pdev->dev, "failed to get nand clock\n");
@@ -1062,21 +1101,12 @@ struct pxa3xx_nand_info *alloc_nand_resource(struct platform_device *pdev)
10621101
goto fail_free_buf;
10631102
}
10641103

1065-
ret = pxa3xx_nand_detect_flash(info, pdata);
1066-
if (ret) {
1067-
dev_err(&pdev->dev, "failed to detect flash\n");
1068-
ret = -ENODEV;
1069-
goto fail_free_irq;
1070-
}
1071-
1072-
pxa3xx_nand_init_mtd(mtd, info);
10731104
platform_set_drvdata(pdev, info);
10741105

10751106
return info;
10761107

1077-
fail_free_irq:
1078-
free_irq(irq, info);
10791108
fail_free_buf:
1109+
free_irq(irq, info);
10801110
if (use_dma) {
10811111
pxa_free_dma(info->data_dma_ch);
10821112
dma_free_coherent(&pdev->dev, info->data_buff_size,
@@ -1146,7 +1176,7 @@ static int pxa3xx_nand_probe(struct platform_device *pdev)
11461176
if (info == NULL)
11471177
return -ENOMEM;
11481178

1149-
if (nand_scan(info->mtd, 1)) {
1179+
if (pxa3xx_nand_scan(info->mtd)) {
11501180
dev_err(&pdev->dev, "failed to scan nand\n");
11511181
pxa3xx_nand_remove(pdev);
11521182
return -ENODEV;

0 commit comments

Comments
 (0)