kernel: update 4.1 to 4.1.5
[openwrt.git] / target / linux / sunxi / patches-4.1 / 113-mtd-nand-add-pst.patch
1 From bec69bb8e85151729014d859106dcc3fe652b1d4 Mon Sep 17 00:00:00 2001
2 From: Boris BREZILLON <boris.brezillon@free-electrons.com>
3 Date: Mon, 28 Jul 2014 14:45:40 +0200
4 Subject: [PATCH] mtd: nand: Add page status table (pst)
5
6 Page status table is an byte array storing pages status.
7 It defines 3 status:
8  - unknown: the page has not been read yet and we do not know its current
9    state
10  - empty: the page contains only FFs
11  - filled: the page has been filled with data
12
13 Care must be taken: an empty page does not mean it can be written, because
14 it might have already been written with only FFs.
15
16 These page status are useful to check wether the controller should try to
17 correct errors (using ECC) or a derandomize data (using a randomizer
18 block).
19
20 Signed-off-by: Boris BREZILLON <boris.brezillon@free-electrons.com>
21 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
22 ---
23  drivers/mtd/nand/nand_base.c | 154 +++++++++++++++++++++++++++++++++++++++++++
24  include/linux/mtd/nand.h     |  21 ++++++
25  2 files changed, 175 insertions(+)
26
27 --- a/drivers/mtd/nand/nand_base.c
28 +++ b/drivers/mtd/nand/nand_base.c
29 @@ -1102,6 +1102,138 @@ out:
30  EXPORT_SYMBOL(nand_lock);
31  
32  /**
33 + * nand_page_is_empty - check wether a NAND page contains only FFs
34 + * @mtd:       mtd info
35 + * @data:      data buffer
36 + * @oob:       oob buffer
37 + *
38 + * Reads the data stored in the databuf buffer and check if it contains only
39 + * FFs.
40 + *
41 + * Return true if it does else return false.
42 + */
43 +bool nand_page_is_empty(struct mtd_info *mtd, void *data, void *oob)
44 +{
45 +       u8 *buf;
46 +       int length;
47 +       u32 pattern = 0xffffffff;
48 +       int bitflips = 0;
49 +       int cnt;
50 +
51 +       buf = data;
52 +       length = mtd->writesize;
53 +       while (length) {
54 +               cnt = length < sizeof(pattern) ? length : sizeof(pattern);
55 +               if (memcmp(&pattern, buf, cnt)) {
56 +                       int i;
57 +                       for (i = 0; i < cnt * BITS_PER_BYTE; i++) {
58 +                               if (!(buf[i / BITS_PER_BYTE] &
59 +                                     (1 << (i % BITS_PER_BYTE)))) {
60 +                                       bitflips++;
61 +                                       if (bitflips > mtd->ecc_strength)
62 +                                               return false;
63 +                               }
64 +                       }
65 +               }
66 +
67 +               buf += sizeof(pattern);
68 +               length -= sizeof(pattern);
69 +       }
70 +
71 +       buf = oob;
72 +       length = mtd->oobsize;
73 +       while (length) {
74 +               cnt = length < sizeof(pattern) ? length : sizeof(pattern);
75 +               if (memcmp(&pattern, buf, cnt)) {
76 +                       int i;
77 +                       for (i = 0; i < cnt * BITS_PER_BYTE; i++) {
78 +                               if (!(buf[i / BITS_PER_BYTE] &
79 +                                     (1 << (i % BITS_PER_BYTE)))) {
80 +                                       bitflips++;
81 +                                       if (bitflips > mtd->ecc_strength)
82 +                                               return false;
83 +                               }
84 +                       }
85 +               }
86 +
87 +               buf += sizeof(pattern);
88 +               length -= sizeof(pattern);
89 +       }
90 +
91 +       return true;
92 +}
93 +EXPORT_SYMBOL(nand_page_is_empty);
94 +
95 +/**
96 + * nand_page_get_status - retrieve page status from the page status table (pst)
97 + * @mtd:       mtd info
98 + * @page:      page you want to get status on
99 + *
100 + * Return the page status.
101 + */
102 +int nand_page_get_status(struct mtd_info *mtd, int page)
103 +{
104 +       struct nand_chip *chip = mtd->priv;
105 +       u8 shift = (page % 4) * 2;
106 +       uint64_t offset = page / 4;
107 +       int ret = NAND_PAGE_STATUS_UNKNOWN;
108 +
109 +       if (chip->pst)
110 +               ret = (chip->pst[offset] >> shift) & 0x3;
111 +
112 +       return ret;
113 +}
114 +EXPORT_SYMBOL(nand_page_get_status);
115 +
116 +/**
117 + * nand_page_set_status - assign page status from in the page status table
118 + * @mtd:       mtd info
119 + * @page:      page you want to get status on
120 + * @status:    new status to assign
121 + */
122 +void nand_page_set_status(struct mtd_info *mtd, int page,
123 +                         enum nand_page_status status)
124 +{
125 +       struct nand_chip *chip = mtd->priv;
126 +       u8 shift;
127 +       uint64_t offset;
128 +
129 +       if (!chip->pst)
130 +               return;
131 +
132 +       shift = (page % 4) * 2;
133 +       offset = page / 4;
134 +       chip->pst[offset] &= ~(0x3 << shift);
135 +       chip->pst[offset] |= (status & 0x3) << shift;
136 +}
137 +EXPORT_SYMBOL(nand_page_set_status);
138 +
139 +/**
140 + * nand_pst_create - create a page status table
141 + * @mtd:       mtd info
142 + *
143 + * Allocate a page status table and assign it to the mtd device.
144 + *
145 + * Returns 0 in case of success or -ERRNO in case of error.
146 + */
147 +int nand_pst_create(struct mtd_info *mtd)
148 +{
149 +       struct nand_chip *chip = mtd->priv;
150 +
151 +       if (chip->pst)
152 +               return 0;
153 +
154 +       chip->pst = kzalloc(mtd->size >>
155 +                           (chip->page_shift + mtd->subpage_sft + 2),
156 +                           GFP_KERNEL);
157 +       if (!chip->pst)
158 +               return -ENOMEM;
159 +
160 +       return 0;
161 +}
162 +EXPORT_SYMBOL(nand_pst_create);
163 +
164 +/**
165   * nand_read_page_raw - [INTERN] read raw page data without ecc
166   * @mtd: mtd info structure
167   * @chip: nand chip info structure
168 @@ -2539,6 +2671,7 @@ static int nand_do_write_ops(struct mtd_
169                 uint8_t *wbuf = buf;
170                 int use_bufpoi;
171                 int part_pagewr = (column || writelen < (mtd->writesize - 1));
172 +               int subpage;
173  
174                 if (part_pagewr)
175                         use_bufpoi = 1;
176 @@ -2574,6 +2707,14 @@ static int nand_do_write_ops(struct mtd_
177                 if (ret)
178                         break;
179  
180 +               for (subpage = column / chip->subpagesize;
181 +                    subpage < (column + writelen) / chip->subpagesize;
182 +                    subpage++)
183 +                       nand_page_set_status(mtd,
184 +                                            (page << mtd->subpage_sft) +
185 +                                            subpage,
186 +                                            NAND_PAGE_FILLED);
187 +
188                 writelen -= bytes;
189                 if (!writelen)
190                         break;
191 @@ -2979,6 +3120,7 @@ int nand_erase_nand(struct mtd_info *mtd
192         int page, status, pages_per_block, ret, chipnr;
193         struct nand_chip *chip = mtd->priv;
194         loff_t len;
195 +       int i;
196  
197         pr_debug("%s: start = 0x%012llx, len = %llu\n",
198                         __func__, (unsigned long long)instr->addr,
199 @@ -3051,6 +3193,18 @@ int nand_erase_nand(struct mtd_info *mtd
200                         goto erase_exit;
201                 }
202  
203 +               for (i = 0; i < pages_per_block; i++) {
204 +                       int subpage;
205 +                       for (subpage = 0;
206 +                            subpage < 1 << mtd->subpage_sft;
207 +                            subpage++) {
208 +                               nand_page_set_status(mtd,
209 +                                       ((page + i) << mtd->subpage_sft) +
210 +                                       subpage,
211 +                                       NAND_PAGE_EMPTY);
212 +                       }
213 +               }
214 +
215                 /* Increment page address and decrement length */
216                 len -= (1ULL << chip->phys_erase_shift);
217                 page += pages_per_block;
218 --- a/include/linux/mtd/nand.h
219 +++ b/include/linux/mtd/nand.h
220 @@ -521,6 +521,24 @@ struct nand_ecc_ctrl {
221                         int page);
222  };
223  
224 +/*
225 + * Constants for page status
226 + */
227 +enum nand_page_status {
228 +       NAND_PAGE_STATUS_UNKNOWN,
229 +       NAND_PAGE_EMPTY,
230 +       NAND_PAGE_FILLED,
231 +};
232 +
233 +bool nand_page_is_empty(struct mtd_info *mtd, void *data, void *oob);
234 +
235 +int nand_page_get_status(struct mtd_info *mtd, int page);
236 +
237 +void nand_page_set_status(struct mtd_info *mtd, int page,
238 +                         enum nand_page_status status);
239 +
240 +int nand_pst_create(struct mtd_info *mtd);
241 +
242  /**
243   * struct nand_buffers - buffer structure for read/write
244   * @ecccalc:   buffer pointer for calculated ECC, size is oobsize.
245 @@ -630,6 +648,7 @@ struct nand_buffers {
246   * @bbt_md:            [REPLACEABLE] bad block table mirror descriptor
247   * @badblock_pattern:  [REPLACEABLE] bad block scan pattern used for initial
248   *                     bad block scan.
249 + * @pst:               [INTERN] page status table
250   * @controller:                [REPLACEABLE] a pointer to a hardware controller
251   *                     structure which is shared among multiple independent
252   *                     devices.
253 @@ -718,6 +737,8 @@ struct nand_chip {
254  
255         struct nand_bbt_descr *badblock_pattern;
256  
257 +       uint8_t *pst;
258 +
259         struct list_head partitions;
260         struct mutex part_lock;
261