sunxi: add support for 4.1
[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 diff --git a/drivers/mtd/nand/nand_base.c b/drivers/mtd/nand/nand_base.c
28 index a30b67f..8a5d12e 100644
29 --- a/drivers/mtd/nand/nand_base.c
30 +++ b/drivers/mtd/nand/nand_base.c
31 @@ -1102,6 +1102,138 @@ int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
32  EXPORT_SYMBOL(nand_lock);
33  
34  /**
35 + * nand_page_is_empty - check wether a NAND page contains only FFs
36 + * @mtd:       mtd info
37 + * @data:      data buffer
38 + * @oob:       oob buffer
39 + *
40 + * Reads the data stored in the databuf buffer and check if it contains only
41 + * FFs.
42 + *
43 + * Return true if it does else return false.
44 + */
45 +bool nand_page_is_empty(struct mtd_info *mtd, void *data, void *oob)
46 +{
47 +       u8 *buf;
48 +       int length;
49 +       u32 pattern = 0xffffffff;
50 +       int bitflips = 0;
51 +       int cnt;
52 +
53 +       buf = data;
54 +       length = mtd->writesize;
55 +       while (length) {
56 +               cnt = length < sizeof(pattern) ? length : sizeof(pattern);
57 +               if (memcmp(&pattern, buf, cnt)) {
58 +                       int i;
59 +                       for (i = 0; i < cnt * BITS_PER_BYTE; i++) {
60 +                               if (!(buf[i / BITS_PER_BYTE] &
61 +                                     (1 << (i % BITS_PER_BYTE)))) {
62 +                                       bitflips++;
63 +                                       if (bitflips > mtd->ecc_strength)
64 +                                               return false;
65 +                               }
66 +                       }
67 +               }
68 +
69 +               buf += sizeof(pattern);
70 +               length -= sizeof(pattern);
71 +       }
72 +
73 +       buf = oob;
74 +       length = mtd->oobsize;
75 +       while (length) {
76 +               cnt = length < sizeof(pattern) ? length : sizeof(pattern);
77 +               if (memcmp(&pattern, buf, cnt)) {
78 +                       int i;
79 +                       for (i = 0; i < cnt * BITS_PER_BYTE; i++) {
80 +                               if (!(buf[i / BITS_PER_BYTE] &
81 +                                     (1 << (i % BITS_PER_BYTE)))) {
82 +                                       bitflips++;
83 +                                       if (bitflips > mtd->ecc_strength)
84 +                                               return false;
85 +                               }
86 +                       }
87 +               }
88 +
89 +               buf += sizeof(pattern);
90 +               length -= sizeof(pattern);
91 +       }
92 +
93 +       return true;
94 +}
95 +EXPORT_SYMBOL(nand_page_is_empty);
96 +
97 +/**
98 + * nand_page_get_status - retrieve page status from the page status table (pst)
99 + * @mtd:       mtd info
100 + * @page:      page you want to get status on
101 + *
102 + * Return the page status.
103 + */
104 +int nand_page_get_status(struct mtd_info *mtd, int page)
105 +{
106 +       struct nand_chip *chip = mtd->priv;
107 +       u8 shift = (page % 4) * 2;
108 +       uint64_t offset = page / 4;
109 +       int ret = NAND_PAGE_STATUS_UNKNOWN;
110 +
111 +       if (chip->pst)
112 +               ret = (chip->pst[offset] >> shift) & 0x3;
113 +
114 +       return ret;
115 +}
116 +EXPORT_SYMBOL(nand_page_get_status);
117 +
118 +/**
119 + * nand_page_set_status - assign page status from in the page status table
120 + * @mtd:       mtd info
121 + * @page:      page you want to get status on
122 + * @status:    new status to assign
123 + */
124 +void nand_page_set_status(struct mtd_info *mtd, int page,
125 +                         enum nand_page_status status)
126 +{
127 +       struct nand_chip *chip = mtd->priv;
128 +       u8 shift;
129 +       uint64_t offset;
130 +
131 +       if (!chip->pst)
132 +               return;
133 +
134 +       shift = (page % 4) * 2;
135 +       offset = page / 4;
136 +       chip->pst[offset] &= ~(0x3 << shift);
137 +       chip->pst[offset] |= (status & 0x3) << shift;
138 +}
139 +EXPORT_SYMBOL(nand_page_set_status);
140 +
141 +/**
142 + * nand_pst_create - create a page status table
143 + * @mtd:       mtd info
144 + *
145 + * Allocate a page status table and assign it to the mtd device.
146 + *
147 + * Returns 0 in case of success or -ERRNO in case of error.
148 + */
149 +int nand_pst_create(struct mtd_info *mtd)
150 +{
151 +       struct nand_chip *chip = mtd->priv;
152 +
153 +       if (chip->pst)
154 +               return 0;
155 +
156 +       chip->pst = kzalloc(mtd->size >>
157 +                           (chip->page_shift + mtd->subpage_sft + 2),
158 +                           GFP_KERNEL);
159 +       if (!chip->pst)
160 +               return -ENOMEM;
161 +
162 +       return 0;
163 +}
164 +EXPORT_SYMBOL(nand_pst_create);
165 +
166 +/**
167   * nand_read_page_raw - [INTERN] read raw page data without ecc
168   * @mtd: mtd info structure
169   * @chip: nand chip info structure
170 @@ -2539,6 +2671,7 @@ static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
171                 uint8_t *wbuf = buf;
172                 int use_bufpoi;
173                 int part_pagewr = (column || writelen < (mtd->writesize - 1));
174 +               int subpage;
175  
176                 if (part_pagewr)
177                         use_bufpoi = 1;
178 @@ -2574,6 +2707,14 @@ static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
179                 if (ret)
180                         break;
181  
182 +               for (subpage = column / chip->subpagesize;
183 +                    subpage < (column + writelen) / chip->subpagesize;
184 +                    subpage++)
185 +                       nand_page_set_status(mtd,
186 +                                            (page << mtd->subpage_sft) +
187 +                                            subpage,
188 +                                            NAND_PAGE_FILLED);
189 +
190                 writelen -= bytes;
191                 if (!writelen)
192                         break;
193 @@ -2979,6 +3120,7 @@ int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
194         int page, status, pages_per_block, ret, chipnr;
195         struct nand_chip *chip = mtd->priv;
196         loff_t len;
197 +       int i;
198  
199         pr_debug("%s: start = 0x%012llx, len = %llu\n",
200                         __func__, (unsigned long long)instr->addr,
201 @@ -3051,6 +3193,18 @@ int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
202                         goto erase_exit;
203                 }
204  
205 +               for (i = 0; i < pages_per_block; i++) {
206 +                       int subpage;
207 +                       for (subpage = 0;
208 +                            subpage < 1 << mtd->subpage_sft;
209 +                            subpage++) {
210 +                               nand_page_set_status(mtd,
211 +                                       ((page + i) << mtd->subpage_sft) +
212 +                                       subpage,
213 +                                       NAND_PAGE_EMPTY);
214 +                       }
215 +               }
216 +
217                 /* Increment page address and decrement length */
218                 len -= (1ULL << chip->phys_erase_shift);
219                 page += pages_per_block;
220 diff --git a/include/linux/mtd/nand.h b/include/linux/mtd/nand.h
221 index 5616f51..4f7ca8d 100644
222 --- a/include/linux/mtd/nand.h
223 +++ b/include/linux/mtd/nand.h
224 @@ -521,6 +521,24 @@ struct nand_ecc_ctrl {
225                         int page);
226  };
227  
228 +/*
229 + * Constants for page status
230 + */
231 +enum nand_page_status {
232 +       NAND_PAGE_STATUS_UNKNOWN,
233 +       NAND_PAGE_EMPTY,
234 +       NAND_PAGE_FILLED,
235 +};
236 +
237 +bool nand_page_is_empty(struct mtd_info *mtd, void *data, void *oob);
238 +
239 +int nand_page_get_status(struct mtd_info *mtd, int page);
240 +
241 +void nand_page_set_status(struct mtd_info *mtd, int page,
242 +                         enum nand_page_status status);
243 +
244 +int nand_pst_create(struct mtd_info *mtd);
245 +
246  /**
247   * struct nand_buffers - buffer structure for read/write
248   * @ecccalc:   buffer pointer for calculated ECC, size is oobsize.
249 @@ -630,6 +648,7 @@ struct nand_buffers {
250   * @bbt_md:            [REPLACEABLE] bad block table mirror descriptor
251   * @badblock_pattern:  [REPLACEABLE] bad block scan pattern used for initial
252   *                     bad block scan.
253 + * @pst:               [INTERN] page status table
254   * @controller:                [REPLACEABLE] a pointer to a hardware controller
255   *                     structure which is shared among multiple independent
256   *                     devices.
257 @@ -718,6 +737,8 @@ struct nand_chip {
258  
259         struct nand_bbt_descr *badblock_pattern;
260  
261 +       uint8_t *pst;
262 +
263         struct list_head partitions;
264         struct mutex part_lock;
265