sunxi: add support for 4.1
[openwrt.git] / target / linux / sunxi / patches-4.1 / 110-mtd-move-nand_ecc_ctrl-init.patch
1 From 3282055a7d0a304d541dbdbe2e32167e1a2f117c Mon Sep 17 00:00:00 2001
2 From: Boris BREZILLON <boris.brezillon@free-electrons.com>
3 Date: Mon, 28 Jul 2014 14:20:54 +0200
4 Subject: [PATCH] mtd: nand: Take nand_ecc_ctrl initialization out of
5  nand_scan_tail
6
7 Take ECC initialization code portion out of nand_scan_tail so that we can
8 re-use this implementation.
9
10 This commit only moves some code around and makes no functional changes.
11
12 Signed-off-by: Boris BREZILLON <boris.brezillon@free-electrons.com>
13 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
14 ---
15  drivers/mtd/nand/nand_base.c | 91 +++++++++++++++++++++++++++-----------------
16  1 file changed, 56 insertions(+), 35 deletions(-)
17
18 diff --git a/drivers/mtd/nand/nand_base.c b/drivers/mtd/nand/nand_base.c
19 index c2e1232..f580ed1 100644
20 --- a/drivers/mtd/nand/nand_base.c
21 +++ b/drivers/mtd/nand/nand_base.c
22 @@ -3892,42 +3892,15 @@ static bool nand_ecc_strength_good(struct mtd_info *mtd)
23         return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
24  }
25  
26 -/**
27 - * nand_scan_tail - [NAND Interface] Scan for the NAND device
28 - * @mtd: MTD device structure
29 - *
30 - * This is the second phase of the normal nand_scan() function. It fills out
31 - * all the uninitialized function pointers with the defaults and scans for a
32 - * bad block table if appropriate.
33 +/*
34 + * Initialize ECC struct:
35 + *  - fill ECC struct with default function/values when these ones are undefined
36 + *  - fill ECC infos based on MTD device
37   */
38 -int nand_scan_tail(struct mtd_info *mtd)
39 +static int nand_ecc_ctrl_init(struct mtd_info *mtd, struct nand_ecc_ctrl *ecc)
40  {
41         int i;
42 -       struct nand_chip *chip = mtd->priv;
43 -       struct nand_ecc_ctrl *ecc = &chip->ecc;
44 -       struct nand_buffers *nbuf;
45 -
46 -       /* New bad blocks should be marked in OOB, flash-based BBT, or both */
47 -       BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
48 -                       !(chip->bbt_options & NAND_BBT_USE_FLASH));
49 -
50 -       if (!(chip->options & NAND_OWN_BUFFERS)) {
51 -               nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
52 -                               + mtd->oobsize * 3, GFP_KERNEL);
53 -               if (!nbuf)
54 -                       return -ENOMEM;
55 -               nbuf->ecccalc = (uint8_t *)(nbuf + 1);
56 -               nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
57 -               nbuf->databuf = nbuf->ecccode + mtd->oobsize;
58  
59 -               chip->buffers = nbuf;
60 -       } else {
61 -               if (!chip->buffers)
62 -                       return -ENOMEM;
63 -       }
64 -
65 -       /* Set the internal oob buffer location, just after the page data */
66 -       chip->oob_poi = chip->buffers->databuf + mtd->writesize;
67  
68         /*
69          * If no default placement scheme is given, select an appropriate one.
70 @@ -3953,9 +3926,6 @@ int nand_scan_tail(struct mtd_info *mtd)
71                 }
72         }
73  
74 -       if (!chip->write_page)
75 -               chip->write_page = nand_write_page;
76 -
77         /*
78          * Check ECC mode, default to software if 3byte/512byte hardware ECC is
79          * selected and we have 256 byte pagesize fallback to software ECC
80 @@ -4125,6 +4095,57 @@ int nand_scan_tail(struct mtd_info *mtd)
81         }
82         ecc->total = ecc->steps * ecc->bytes;
83  
84 +       return 0;
85 +}
86 +
87 +/**
88 + * nand_scan_tail - [NAND Interface] Scan for the NAND device
89 + * @mtd: MTD device structure
90 + *
91 + * This is the second phase of the normal nand_scan() function. It fills out
92 + * all the uninitialized function pointers with the defaults and scans for a
93 + * bad block table if appropriate.
94 + */
95 +int nand_scan_tail(struct mtd_info *mtd)
96 +{
97 +       int ret;
98 +       struct nand_chip *chip = mtd->priv;
99 +       struct nand_ecc_ctrl *ecc = &chip->ecc;
100 +       struct nand_buffers *nbuf;
101 +
102 +       /* New bad blocks should be marked in OOB, flash-based BBT, or both */
103 +       BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
104 +                       !(chip->bbt_options & NAND_BBT_USE_FLASH));
105 +
106 +       if (!(chip->options & NAND_OWN_BUFFERS)) {
107 +               nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
108 +                               + mtd->oobsize * 3, GFP_KERNEL);
109 +               if (!nbuf)
110 +                       return -ENOMEM;
111 +               nbuf->ecccalc = (uint8_t *)(nbuf + 1);
112 +               nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
113 +               nbuf->databuf = nbuf->ecccode + mtd->oobsize;
114 +               chip->buffers = nbuf;
115 +       } else {
116 +               if (!chip->buffers)
117 +                       return -ENOMEM;
118 +       }
119 +
120 +       /* Set the internal oob buffer location, just after the page data */
121 +       chip->oob_poi = chip->buffers->databuf + mtd->writesize;
122 +
123 +       if (!chip->write_page)
124 +               chip->write_page = nand_write_page;
125 +
126 +       /* Initialize ECC struct */
127 +       ret = nand_ecc_ctrl_init(mtd, ecc);
128 +       if (ret) {
129 +               if (!(chip->options & NAND_OWN_BUFFERS))
130 +                       kfree(chip->buffers);
131 +
132 +               return ret;
133 +       }
134 +
135         /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
136         if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
137                 switch (ecc->steps) {