aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/mediatek/files-5.10/drivers/mtd/mtk-snand/mtk-snand-ecc.c
blob: 6dd0f346c8e80fb7665c1dddca20205590828d67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
/*
 * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
 *
 * Author: Weijie Gao <weijie.gao@mediatek.com>
 */

#include "mtk-snand-def.h"

/* ECC registers */
#define ECC_ENCCON			0x000
#define ENC_EN				BIT(0)

#define ECC_ENCCNFG			0x004
#define ENC_MS_S			16
#define ENC_BURST_EN			BIT(8)
#define ENC_TNUM_S			0

#define ECC_ENCIDLE			0x00c
#define ENC_IDLE			BIT(0)

#define ECC_DECCON			0x100
#define DEC_EN				BIT(0)

#define ECC_DECCNFG			0x104
#define DEC_EMPTY_EN			BIT(31)
#define DEC_CS_S			16
#define DEC_CON_S			12
#define   DEC_CON_CORRECT		3
#define DEC_BURST_EN			BIT(8)
#define DEC_TNUM_S			0

#define ECC_DECIDLE			0x10c
#define DEC_IDLE			BIT(0)

#define ECC_DECENUM0			0x114
#define ECC_DECENUM(n)			(ECC_DECENUM0 + (n) * 4)

/* ECC_ENCIDLE & ECC_DECIDLE */
#define ECC_IDLE			BIT(0)

/* ENC_MODE & DEC_MODE */
#define ECC_MODE_NFI			1

#define ECC_TIMEOUT			500000

static const uint8_t mt7622_ecc_caps[] = { 4, 6, 8, 10, 12 };

static const uint32_t mt7622_ecc_regs[] = {
	[ECC_DECDONE] = 0x11c,
};

static const struct mtk_ecc_soc_data mtk_ecc_socs[__SNAND_SOC_MAX] = {
	[SNAND_SOC_MT7622] = {
		.ecc_caps = mt7622_ecc_caps,
		.num_ecc_cap = ARRAY_SIZE(mt7622_ecc_caps),
		.regs = mt7622_ecc_regs,
		.mode_shift = 4,
		.errnum_bits = 5,
		.errnum_shift = 5,
	},
	[SNAND_SOC_MT7629] = {
		.ecc_caps = mt7622_ecc_caps,
		.num_ecc_cap = ARRAY_SIZE(mt7622_ecc_caps),
		.regs = mt7622_ecc_regs,
		.mode_shift = 4,
		.errnum_bits = 5,
		.errnum_shift = 5,
	},
};

static inline uint32_t ecc_read32(struct mtk_snand *snf, uint32_t reg)
{
	return readl(snf->ecc_base + reg);
}

static inline void ecc_write32(struct mtk_snand *snf, uint32_t reg,
			       uint32_t val)
{
	writel(val, snf->ecc_base + reg);
}

static inline void ecc_write16(struct mtk_snand *snf, uint32_t reg,
			       uint16_t val)
{
	writew(val, snf->ecc_base + reg);
}

static int mtk_ecc_poll(struct mtk_snand *snf, uint32_t reg, uint32_t bits)
{
	uint32_t val;

	return read16_poll_timeout(snf->ecc_base + reg, val, (val & bits), 0,
				   ECC_TIMEOUT);
}

static int mtk_ecc_wait_idle(struct mtk_snand *snf, uint32_t reg)
{
	int ret;

	ret = mtk_ecc_poll(snf, reg, ECC_IDLE);
	if (ret) {
		snand_log_ecc(snf->pdev, "ECC engine is busy\n");
		return -EBUSY;
	}

	return 0;
}

int mtk_ecc_setup(struct mtk_snand *snf, void *fmdaddr, uint32_t max_ecc_bytes,
		  uint32_t msg_size)
{
	uint32_t i, val, ecc_msg_bits, ecc_strength;
	int ret;

	snf->ecc_soc = &mtk_ecc_socs[snf->soc];

	snf->ecc_parity_bits = fls(1 + 8 * msg_size);
	ecc_strength = max_ecc_bytes * 8 / snf->ecc_parity_bits;

	for (i = snf->ecc_soc->num_ecc_cap - 1; i >= 0; i--) {
		if (snf->ecc_soc->ecc_caps[i] <= ecc_strength)
			break;
	}

	if (unlikely(i < 0)) {
		snand_log_ecc(snf->pdev, "Page size %u+%u is not supported\n",
			      snf->writesize, snf->oobsize);
		return -ENOTSUPP;
	}

	snf->ecc_strength = snf->ecc_soc->ecc_caps[i];
	snf->ecc_bytes = DIV_ROUND_UP(snf->ecc_strength * snf->ecc_parity_bits,
				      8);

	/* Encoder config */
	ecc_write16(snf, ECC_ENCCON, 0);
	ret = mtk_ecc_wait_idle(snf, ECC_ENCIDLE);
	if (ret)
		return ret;

	ecc_msg_bits = msg_size * 8;
	val = (ecc_msg_bits << ENC_MS_S) |
	      (ECC_MODE_NFI << snf->ecc_soc->mode_shift) | i;
	ecc_write32(snf, ECC_ENCCNFG, val);

	/* Decoder config */
	ecc_write16(snf, ECC_DECCON, 0);
	ret = mtk_ecc_wait_idle(snf, ECC_DECIDLE);
	if (ret)
		return ret;

	ecc_msg_bits += snf->ecc_strength * snf->ecc_parity_bits;
	val = DEC_EMPTY_EN | (ecc_msg_bits << DEC_CS_S) |
	      (DEC_CON_CORRECT << DEC_CON_S) |
	      (ECC_MODE_NFI << snf->ecc_soc->mode_shift) | i;
	ecc_write32(snf, ECC_DECCNFG, val);

	return 0;
}

int mtk_snand_ecc_encoder_start(struct mtk_snand *snf)
{
	int ret;

	ret = mtk_ecc_wait_idle(snf, ECC_ENCIDLE);
	if (ret) {
		ecc_write16(snf, ECC_ENCCON, 0);
		mtk_ecc_wait_idle(snf, ECC_ENCIDLE);
	}

	ecc_write16(snf, ECC_ENCCON, ENC_EN);

	return 0;
}

void mtk_snand_ecc_encoder_stop(struct mtk_snand *snf)
{
	mtk_ecc_wait_idle(snf, ECC_ENCIDLE);
	ecc_write16(snf, ECC_ENCCON, 0);
}

int mtk_snand_ecc_decoder_start(struct mtk_snand *snf)
{
	int ret;

	ret = mtk_ecc_wait_idle(snf, ECC_DECIDLE);
	if (ret) {
		ecc_write16(snf, ECC_DECCON, 0);
		mtk_ecc_wait_idle(snf, ECC_DECIDLE);
	}

	ecc_write16(snf, ECC_DECCON, DEC_EN);

	return 0;
}

void mtk_snand_ecc_decoder_stop(struct mtk_snand *snf)
{
	mtk_ecc_wait_idle(snf, ECC_DECIDLE);
	ecc_write16(snf, ECC_DECCON, 0);
}

int mtk_ecc_wait_decoder_done(struct mtk_snand *snf)
{
	uint16_t val, step_mask = (1 << snf->ecc_steps) - 1;
	uint32_t reg = snf->ecc_soc->regs[ECC_DECDONE];
	int ret;

	ret = read16_poll_timeout(snf->ecc_base + reg, val,
				  (val & step_mask) == step_mask, 0,
				  ECC_TIMEOUT);
	if (ret)
		snand_log_ecc(snf->pdev, "ECC decoder is busy\n");

	return ret;
}

int mtk_ecc_check_decode_error(struct mtk_snand *snf)
{
	uint32_t i, regi, fi, errnum;
	uint32_t errnum_shift = snf->ecc_soc->errnum_shift;
	uint32_t errnum_mask = (1 << snf->ecc_soc->errnum_bits) - 1;
	int ret = 0;

	for (i = 0; i < snf->ecc_steps; i++) {
		regi = i / 4;
		fi = i % 4;

		errnum = ecc_read32(snf, ECC_DECENUM(regi));
		errnum = (errnum >> (fi * errnum_shift)) & errnum_mask;

		if (errnum <= snf->ecc_strength) {
			snf->sect_bf[i] = errnum;
		} else {
			snf->sect_bf[i] = -1;
			ret = -EBADMSG;
		}
	}

	return ret;
}

static int mtk_ecc_check_buf_bitflips(struct mtk_snand *snf, const void *buf,
				      size_t len, uint32_t bitflips)
{
	const uint8_t *buf8 = buf;
	const uint32_t *buf32;
	uint32_t d, weight;

	while (len && ((uintptr_t)buf8) % sizeof(uint32_t)) {
		weight = hweight8(*buf8);
		bitflips += BITS_PER_BYTE - weight;
		buf8++;
		len--;

		if (bitflips > snf->ecc_strength)
			return -EBADMSG;
	}

	buf32 = (const uint32_t *)buf8;
	while (len >= sizeof(uint32_t)) {
		d = *buf32;

		if (d != ~0) {
			weight = hweight32(d);
			bitflips += sizeof(uint32_t) * BITS_PER_BYTE - weight;
		}

		buf32++;
		len -= sizeof(uint32_t);

		if (bitflips > snf->ecc_strength)
			return -EBADMSG;
	}

	buf8 = (const uint8_t *)buf32;
	while (len) {
		weight = hweight8(*buf8);
		bitflips += BITS_PER_BYTE - weight;
		buf8++;
		len--;

		if (bitflips > snf->ecc_strength)
			return -EBADMSG;
	}

	return bitflips;
}

static int mtk_ecc_check_parity_bitflips(struct mtk_snand *snf, const void *buf,
					 uint32_t bits, uint32_t bitflips)
{
	uint32_t len, i;
	uint8_t b;
	int rc;

	len = bits >> 3;
	bits &= 7;

	rc = mtk_ecc_check_buf_bitflips(snf, buf, len, bitflips);
	if (!bits || rc < 0)
		return rc;

	bitflips = rc;

	/* We want a precise count of bits */
	b = ((const uint8_t *)buf)[len];
	for (i = 0; i < bits; i++) {
		if (!(b & BIT(i)))
			bitflips++;
	}

	if (bitflips > snf->ecc_strength)
		return -EBADMSG;

	return bitflips;
}

static void mtk_ecc_reset_parity(void *buf, uint32_t bits)
{
	uint32_t len;

	len = bits >> 3;
	bits &= 7;

	memset(buf, 0xff, len);

	/* Only reset bits protected by ECC to 1 */
	if (bits)
		((uint8_t *)buf)[len] |= GENMASK(bits - 1, 0);
}

int mtk_ecc_fixup_empty_sector(struct mtk_snand *snf, uint32_t sect)
{
	uint32_t ecc_bytes = snf->spare_per_sector - snf->nfi_soc->fdm_size;
	uint8_t *oob = snf->page_cache + snf->writesize;
	uint8_t *data_ptr, *fdm_ptr, *ecc_ptr;
	int bitflips = 0, ecc_bits, parity_bits;

	parity_bits = fls(snf->nfi_soc->sector_size * 8);
	ecc_bits = snf->ecc_strength * parity_bits;

	data_ptr = snf->page_cache + sect * snf->nfi_soc->sector_size;
	fdm_ptr = oob + sect * snf->nfi_soc->fdm_size;
	ecc_ptr = oob + snf->ecc_steps * snf->nfi_soc->fdm_size +
		  sect * ecc_bytes;

	/*
	 * Check whether DATA + FDM + ECC of a sector contains correctable
	 * bitflips
	 */
	bitflips = mtk_ecc_check_buf_bitflips(snf, data_ptr,
					      snf->nfi_soc->sector_size,
					      bitflips);
	if (bitflips < 0)
		return -EBADMSG;

	bitflips = mtk_ecc_check_buf_bitflips(snf, fdm_ptr,
					      snf->nfi_soc->fdm_ecc_size,
					      bitflips);
	if (bitflips < 0)
		return -EBADMSG;

	bitflips = mtk_ecc_check_parity_bitflips(snf, ecc_ptr, ecc_bits,
						 bitflips);
	if (bitflips < 0)
		return -EBADMSG;

	if (!bitflips)
		return 0;

	/* Reset the data of this sector to 0xff */
	memset(data_ptr, 0xff, snf->nfi_soc->sector_size);
	memset(fdm_ptr, 0xff, snf->nfi_soc->fdm_ecc_size);
	mtk_ecc_reset_parity(ecc_ptr, ecc_bits);

	return bitflips;
}