Skip to content

Commit f02ea4e

Browse files
Huang Shijiecomputersforpeace
authored andcommitted
mtd: nand: kill the the NAND_MAX_PAGESIZE/NAND_MAX_OOBSIZE for nand_buffers{}
The patch converts the arrays to buffer pointers for nand_buffers{}. The cafe_nand.c is the only NAND_OWN_BUFFERS user which allocates nand_buffers{} itself. This patch disables the DMA for nand_scan_ident, and restores the DMA status after we finish the nand_scan_ident. This way, we can get page size and OOB size and use them to allocate cafe->dmabuf. Since the cafe_nand.c uses the NAND_ECC_HW_SYNDROME ECC mode, we do not allocate the buffers for @ecccalc and @ecccode. Signed-off-by: Huang Shijie <b32955@freescale.com> Signed-off-by: Brian Norris <computersforpeace@gmail.com>
1 parent 3ea5b03 commit f02ea4e

3 files changed

Lines changed: 67 additions & 32 deletions

File tree

‎drivers/mtd/nand/cafe_nand.c‎

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,8 @@ static int cafe_nand_probe(struct pci_dev *pdev,
627627
struct cafe_priv *cafe;
628628
uint32_t ctrl;
629629
int err = 0;
630+
int old_dma;
631+
struct nand_buffers *nbuf;
630632

631633
/* Very old versions shared the same PCI ident for all three
632634
functions on the chip. Verify the class too... */
@@ -655,13 +657,6 @@ static int cafe_nand_probe(struct pci_dev *pdev,
655657
err = -ENOMEM;
656658
goto out_free_mtd;
657659
}
658-
cafe->dmabuf = dma_alloc_coherent(&cafe->pdev->dev, 2112 + sizeof(struct nand_buffers),
659-
&cafe->dmaaddr, GFP_KERNEL);
660-
if (!cafe->dmabuf) {
661-
err = -ENOMEM;
662-
goto out_ior;
663-
}
664-
cafe->nand.buffers = (void *)cafe->dmabuf + 2112;
665660

666661
cafe->rs = init_rs_non_canonical(12, &cafe_mul, 0, 1, 8);
667662
if (!cafe->rs) {
@@ -721,7 +716,7 @@ static int cafe_nand_probe(struct pci_dev *pdev,
721716
"CAFE NAND", mtd);
722717
if (err) {
723718
dev_warn(&pdev->dev, "Could not register IRQ %d\n", pdev->irq);
724-
goto out_free_dma;
719+
goto out_ior;
725720
}
726721

727722
/* Disable master reset, enable NAND clock */
@@ -735,6 +730,32 @@ static int cafe_nand_probe(struct pci_dev *pdev,
735730
cafe_writel(cafe, 0x7006, GLOBAL_CTRL);
736731
cafe_writel(cafe, 0x700a, GLOBAL_CTRL);
737732

733+
/* Enable NAND IRQ in global IRQ mask register */
734+
cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK);
735+
cafe_dev_dbg(&cafe->pdev->dev, "Control %x, IRQ mask %x\n",
736+
cafe_readl(cafe, GLOBAL_CTRL),
737+
cafe_readl(cafe, GLOBAL_IRQ_MASK));
738+
739+
/* Do not use the DMA for the nand_scan_ident() */
740+
old_dma = usedma;
741+
usedma = 0;
742+
743+
/* Scan to find existence of the device */
744+
if (nand_scan_ident(mtd, 2, NULL)) {
745+
err = -ENXIO;
746+
goto out_irq;
747+
}
748+
749+
cafe->dmabuf = dma_alloc_coherent(&cafe->pdev->dev,
750+
2112 + sizeof(struct nand_buffers) +
751+
mtd->writesize + mtd->oobsize,
752+
&cafe->dmaaddr, GFP_KERNEL);
753+
if (!cafe->dmabuf) {
754+
err = -ENOMEM;
755+
goto out_irq;
756+
}
757+
cafe->nand.buffers = nbuf = (void *)cafe->dmabuf + 2112;
758+
738759
/* Set up DMA address */
739760
cafe_writel(cafe, cafe->dmaaddr & 0xffffffff, NAND_DMA_ADDR0);
740761
if (sizeof(cafe->dmaaddr) > 4)
@@ -746,16 +767,13 @@ static int cafe_nand_probe(struct pci_dev *pdev,
746767
cafe_dev_dbg(&cafe->pdev->dev, "Set DMA address to %x (virt %p)\n",
747768
cafe_readl(cafe, NAND_DMA_ADDR0), cafe->dmabuf);
748769

749-
/* Enable NAND IRQ in global IRQ mask register */
750-
cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK);
751-
cafe_dev_dbg(&cafe->pdev->dev, "Control %x, IRQ mask %x\n",
752-
cafe_readl(cafe, GLOBAL_CTRL), cafe_readl(cafe, GLOBAL_IRQ_MASK));
770+
/* this driver does not need the @ecccalc and @ecccode */
771+
nbuf->ecccalc = NULL;
772+
nbuf->ecccode = NULL;
773+
nbuf->databuf = (uint8_t *)(nbuf + 1);
753774

754-
/* Scan to find existence of the device */
755-
if (nand_scan_ident(mtd, 2, NULL)) {
756-
err = -ENXIO;
757-
goto out_irq;
758-
}
775+
/* Restore the DMA flag */
776+
usedma = old_dma;
759777

760778
cafe->ctl2 = 1<<27; /* Reed-Solomon ECC */
761779
if (mtd->writesize == 2048)
@@ -773,7 +791,7 @@ static int cafe_nand_probe(struct pci_dev *pdev,
773791
} else {
774792
printk(KERN_WARNING "Unexpected NAND flash writesize %d. Aborting\n",
775793
mtd->writesize);
776-
goto out_irq;
794+
goto out_free_dma;
777795
}
778796
cafe->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
779797
cafe->nand.ecc.size = mtd->writesize;
@@ -790,7 +808,7 @@ static int cafe_nand_probe(struct pci_dev *pdev,
790808

791809
err = nand_scan_tail(mtd);
792810
if (err)
793-
goto out_irq;
811+
goto out_free_dma;
794812

795813
pci_set_drvdata(pdev, mtd);
796814

@@ -799,12 +817,15 @@ static int cafe_nand_probe(struct pci_dev *pdev,
799817

800818
goto out;
801819

820+
out_free_dma:
821+
dma_free_coherent(&cafe->pdev->dev,
822+
2112 + sizeof(struct nand_buffers) +
823+
mtd->writesize + mtd->oobsize,
824+
cafe->dmabuf, cafe->dmaaddr);
802825
out_irq:
803826
/* Disable NAND IRQ in global IRQ mask register */
804827
cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
805828
free_irq(pdev->irq, mtd);
806-
out_free_dma:
807-
dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
808829
out_ior:
809830
pci_iounmap(pdev, cafe->mmio);
810831
out_free_mtd:
@@ -824,7 +845,10 @@ static void cafe_nand_remove(struct pci_dev *pdev)
824845
nand_release(mtd);
825846
free_rs(cafe->rs);
826847
pci_iounmap(pdev, cafe->mmio);
827-
dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
848+
dma_free_coherent(&cafe->pdev->dev,
849+
2112 + sizeof(struct nand_buffers) +
850+
mtd->writesize + mtd->oobsize,
851+
cafe->dmabuf, cafe->dmaaddr);
828852
kfree(mtd);
829853
}
830854

‎drivers/mtd/nand/nand_base.c‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3696,15 +3696,26 @@ int nand_scan_tail(struct mtd_info *mtd)
36963696
int i;
36973697
struct nand_chip *chip = mtd->priv;
36983698
struct nand_ecc_ctrl *ecc = &chip->ecc;
3699+
struct nand_buffers *nbuf;
36993700

37003701
/* New bad blocks should be marked in OOB, flash-based BBT, or both */
37013702
BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
37023703
!(chip->bbt_options & NAND_BBT_USE_FLASH));
37033704

3704-
if (!(chip->options & NAND_OWN_BUFFERS))
3705-
chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
3706-
if (!chip->buffers)
3707-
return -ENOMEM;
3705+
if (!(chip->options & NAND_OWN_BUFFERS)) {
3706+
nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
3707+
+ mtd->oobsize * 3, GFP_KERNEL);
3708+
if (!nbuf)
3709+
return -ENOMEM;
3710+
nbuf->ecccalc = (uint8_t *)(nbuf + 1);
3711+
nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
3712+
nbuf->databuf = nbuf->ecccode + mtd->oobsize;
3713+
3714+
chip->buffers = nbuf;
3715+
} else {
3716+
if (!chip->buffers)
3717+
return -ENOMEM;
3718+
}
37083719

37093720
/* Set the internal oob buffer location, just after the page data */
37103721
chip->oob_poi = chip->buffers->databuf + mtd->writesize;

‎include/linux/mtd/nand.h‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,17 +435,17 @@ struct nand_ecc_ctrl {
435435

436436
/**
437437
* struct nand_buffers - buffer structure for read/write
438-
* @ecccalc: buffer for calculated ECC
439-
* @ecccode: buffer for ECC read from flash
440-
* @databuf: buffer for data - dynamically sized
438+
* @ecccalc: buffer pointer for calculated ECC, size is oobsize.
439+
* @ecccode: buffer pointer for ECC read from flash, size is oobsize.
440+
* @databuf: buffer pointer for data, size is (page size + oobsize).
441441
*
442442
* Do not change the order of buffers. databuf and oobrbuf must be in
443443
* consecutive order.
444444
*/
445445
struct nand_buffers {
446-
uint8_t ecccalc[NAND_MAX_OOBSIZE];
447-
uint8_t ecccode[NAND_MAX_OOBSIZE];
448-
uint8_t databuf[NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE];
446+
uint8_t *ecccalc;
447+
uint8_t *ecccode;
448+
uint8_t *databuf;
449449
};
450450

451451
/**

0 commit comments

Comments
 (0)