summaryrefslogtreecommitdiffstats
path: root/drivers/mtd/nand/raw_nand.c
blob: 3595c214249d8e3f1adfb9ce881b0a44c16e9c77 (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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
 * Copyright (c) 2019-2022, STMicroelectronics - All Rights Reserved
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <errno.h>
#include <stddef.h>

#include <common/debug.h>
#include <drivers/delay_timer.h>
#include <drivers/raw_nand.h>
#include <lib/utils.h>

#include <platform_def.h>

#define ONFI_SIGNATURE_ADDR	0x20U

/* CRC calculation */
#define CRC_POLYNOM		0x8005U
#define CRC_INIT_VALUE		0x4F4EU

/* Status register */
#define NAND_STATUS_READY	BIT(6)

static struct rawnand_device rawnand_dev;

#pragma weak plat_get_raw_nand_data
int plat_get_raw_nand_data(struct rawnand_device *device)
{
	return 0;
}

static int nand_send_cmd(uint8_t cmd, unsigned int tim)
{
	struct nand_req req;

	zeromem(&req, sizeof(struct nand_req));
	req.nand = rawnand_dev.nand_dev;
	req.type = NAND_REQ_CMD | cmd;
	req.inst_delay = tim;

	return rawnand_dev.ops->exec(&req);
}

static int nand_send_addr(uint8_t addr, unsigned int tim)
{
	struct nand_req req;

	zeromem(&req, sizeof(struct nand_req));
	req.nand = rawnand_dev.nand_dev;
	req.type = NAND_REQ_ADDR;
	req.addr = &addr;
	req.inst_delay = tim;

	return rawnand_dev.ops->exec(&req);
}

static int nand_send_wait(unsigned int delay, unsigned int tim)
{
	struct nand_req req;

	zeromem(&req, sizeof(struct nand_req));
	req.nand = rawnand_dev.nand_dev;
	req.type = NAND_REQ_WAIT;
	req.inst_delay = tim;
	req.delay_ms = delay;

	return rawnand_dev.ops->exec(&req);
}


static int nand_read_data(uint8_t *data, unsigned int length, bool use_8bit)
{
	struct nand_req req;

	zeromem(&req, sizeof(struct nand_req));
	req.nand = rawnand_dev.nand_dev;
	req.type = NAND_REQ_DATAIN | (use_8bit ? NAND_REQ_BUS_WIDTH_8 : 0U);
	req.addr = data;
	req.length = length;

	return rawnand_dev.ops->exec(&req);
}

int nand_change_read_column_cmd(unsigned int offset, uintptr_t buffer,
				unsigned int len)
{
	int ret;
	uint8_t addr[2];
	unsigned int i;

	ret = nand_send_cmd(NAND_CMD_CHANGE_1ST, 0U);
	if (ret !=  0) {
		return ret;
	}

	if (rawnand_dev.nand_dev->buswidth == NAND_BUS_WIDTH_16) {
		offset /= 2U;
	}

	addr[0] = offset;
	addr[1] = offset >> 8;

	for (i = 0; i < 2U; i++) {
		ret = nand_send_addr(addr[i], 0U);
		if (ret !=  0) {
			return ret;
		}
	}

	ret = nand_send_cmd(NAND_CMD_CHANGE_2ND, NAND_TCCS_MIN);
	if (ret !=  0) {
		return ret;
	}

	return nand_read_data((uint8_t *)buffer, len, false);
}

int nand_read_page_cmd(unsigned int page, unsigned int offset,
		       uintptr_t buffer, unsigned int len)
{
	uint8_t addr[5];
	uint8_t i = 0U;
	uint8_t j;
	int ret;

	VERBOSE(">%s page %u offset %u buffer 0x%lx\n", __func__, page, offset,
		buffer);

	if (rawnand_dev.nand_dev->buswidth == NAND_BUS_WIDTH_16) {
		offset /= 2U;
	}

	addr[i++] = offset;
	addr[i++] = offset >> 8;

	addr[i++] = page;
	addr[i++] = page >> 8;
	if (rawnand_dev.nand_dev->size > SZ_128M) {
		addr[i++] = page >> 16;
	}

	ret = nand_send_cmd(NAND_CMD_READ_1ST, 0U);
	if (ret != 0) {
		return ret;
	}

	for (j = 0U; j < i; j++) {
		ret = nand_send_addr(addr[j], 0U);
		if (ret != 0) {
			return ret;
		}
	}

	ret = nand_send_cmd(NAND_CMD_READ_2ND, NAND_TWB_MAX);
	if (ret != 0) {
		return ret;
	}

	ret = nand_send_wait(PSEC_TO_MSEC(NAND_TR_MAX), NAND_TRR_MIN);
	if (ret != 0) {
		return ret;
	}

	if (buffer != 0U) {
		ret = nand_read_data((uint8_t *)buffer, len, false);
	}

	return ret;
}

static int nand_status(uint8_t *status)
{
	int ret;

	ret = nand_send_cmd(NAND_CMD_STATUS, NAND_TWHR_MIN);
	if (ret != 0) {
		return ret;
	}

	if (status != NULL) {
		ret = nand_read_data(status, 1U, true);
	}

	return ret;
}

int nand_wait_ready(unsigned int delay_ms)
{
	uint8_t status;
	int ret;
	uint64_t timeout;

	/* Wait before reading status */
	udelay(1);

	ret = nand_status(NULL);
	if (ret != 0) {
		return ret;
	}

	timeout = timeout_init_us(delay_ms * 1000U);
	while (!timeout_elapsed(timeout)) {
		ret = nand_read_data(&status, 1U, true);
		if (ret != 0) {
			return ret;
		}

		if ((status & NAND_STATUS_READY) != 0U) {
			return nand_send_cmd(NAND_CMD_READ_1ST, 0U);
		}

		udelay(10);
	}

	return -ETIMEDOUT;
}

static int nand_reset(void)
{
	int ret;

	ret = nand_send_cmd(NAND_CMD_RESET, NAND_TWB_MAX);
	if (ret != 0) {
		return ret;
	}

	return nand_send_wait(PSEC_TO_MSEC(NAND_TRST_MAX), 0U);
}

#if NAND_ONFI_DETECT
static uint16_t nand_check_crc(uint16_t crc, uint8_t *data_in,
			       unsigned int data_len)
{
	uint32_t i;
	uint32_t j;
	uint32_t bit;

	for (i = 0U; i < data_len; i++) {
		uint8_t cur_param = *data_in++;

		for (j = BIT(7); j != 0U; j >>= 1) {
			bit = crc & BIT(15);
			crc <<= 1;

			if ((cur_param & j) != 0U) {
				bit ^= BIT(15);
			}

			if (bit != 0U) {
				crc ^= CRC_POLYNOM;
			}
		}

		crc &= GENMASK(15, 0);
	}

	return crc;
}

static int nand_read_id(uint8_t addr, uint8_t *id, unsigned int size)
{
	int ret;

	ret = nand_send_cmd(NAND_CMD_READID, 0U);
	if (ret !=  0) {
		return ret;
	}

	ret = nand_send_addr(addr, NAND_TWHR_MIN);
	if (ret !=  0) {
		return ret;
	}

	return nand_read_data(id, size, true);
}

static int nand_read_param_page(void)
{
	struct nand_param_page page;
	uint8_t addr = 0U;
	int ret;

	ret = nand_send_cmd(NAND_CMD_READ_PARAM_PAGE, 0U);
	if (ret != 0) {
		return ret;
	}

	ret = nand_send_addr(addr, NAND_TWB_MAX);
	if (ret != 0) {
		return ret;
	}

	ret = nand_send_wait(PSEC_TO_MSEC(NAND_TR_MAX), NAND_TRR_MIN);
	if (ret != 0) {
		return ret;
	}

	ret = nand_read_data((uint8_t *)&page, sizeof(page), true);
	if (ret != 0) {
		return ret;
	}

	if (strncmp((char *)&page.page_sig, "ONFI", 4) != 0) {
		WARN("Error ONFI detection\n");
		return -EINVAL;
	}

	if (nand_check_crc(CRC_INIT_VALUE, (uint8_t *)&page, 254U) !=
	    page.crc16) {
		WARN("Error reading param\n");
		return -EINVAL;
	}

	if ((page.features & ONFI_FEAT_BUS_WIDTH_16) != 0U) {
		rawnand_dev.nand_dev->buswidth = NAND_BUS_WIDTH_16;
	} else {
		rawnand_dev.nand_dev->buswidth = NAND_BUS_WIDTH_8;
	}

	rawnand_dev.nand_dev->block_size = page.num_pages_per_blk *
					   page.bytes_per_page;
	rawnand_dev.nand_dev->page_size = page.bytes_per_page;
	rawnand_dev.nand_dev->size = page.num_pages_per_blk *
				     page.bytes_per_page *
				     page.num_blk_in_lun * page.num_lun;

	if (page.nb_ecc_bits != GENMASK_32(7, 0)) {
		rawnand_dev.nand_dev->ecc.max_bit_corr = page.nb_ecc_bits;
		rawnand_dev.nand_dev->ecc.size = SZ_512;
	}

	VERBOSE("Page size %u, block_size %u, Size %llu, ecc %u, buswidth %u\n",
		rawnand_dev.nand_dev->page_size,
		rawnand_dev.nand_dev->block_size, rawnand_dev.nand_dev->size,
		rawnand_dev.nand_dev->ecc.max_bit_corr,
		rawnand_dev.nand_dev->buswidth);

	return 0;
}

static int detect_onfi(void)
{
	int ret;
	char id[4];

	ret = nand_read_id(ONFI_SIGNATURE_ADDR, (uint8_t *)id, sizeof(id));
	if (ret != 0) {
		return ret;
	}

	if (strncmp(id, "ONFI", sizeof(id)) != 0) {
		WARN("NAND Non ONFI detected\n");
		return -ENODEV;
	}

	return nand_read_param_page();
}
#endif

static int nand_mtd_block_is_bad(unsigned int block)
{
	unsigned int nbpages_per_block = rawnand_dev.nand_dev->block_size /
					 rawnand_dev.nand_dev->page_size;
	uint8_t bbm_marker[2];
	uint8_t page;
	int ret;

	for (page = 0U; page < 2U; page++) {
		ret = nand_read_page_cmd(block * nbpages_per_block,
					 rawnand_dev.nand_dev->page_size,
					 (uintptr_t)bbm_marker,
					 sizeof(bbm_marker));
		if (ret != 0) {
			return ret;
		}

		if ((bbm_marker[0] != GENMASK_32(7, 0)) ||
		    (bbm_marker[1] != GENMASK_32(7, 0))) {
			WARN("Block %u is bad\n", block);
			return 1;
		}
	}

	return 0;
}

static int nand_mtd_read_page_raw(struct nand_device *nand, unsigned int page,
				  uintptr_t buffer)
{
	return nand_read_page_cmd(page, 0U, buffer,
				  rawnand_dev.nand_dev->page_size);
}

void nand_raw_ctrl_init(const struct nand_ctrl_ops *ops)
{
	rawnand_dev.ops = ops;
}

int nand_raw_init(unsigned long long *size, unsigned int *erase_size)
{
	int ret;

	rawnand_dev.nand_dev = get_nand_device();
	if (rawnand_dev.nand_dev == NULL) {
		return -EINVAL;
	}

	rawnand_dev.nand_dev->mtd_block_is_bad = nand_mtd_block_is_bad;
	rawnand_dev.nand_dev->mtd_read_page = nand_mtd_read_page_raw;
	rawnand_dev.nand_dev->ecc.mode = NAND_ECC_NONE;

	if ((rawnand_dev.ops->setup == NULL) ||
	    (rawnand_dev.ops->exec == NULL)) {
		return -ENODEV;
	}

	ret = nand_reset();
	if (ret != 0) {
		return ret;
	}

#if NAND_ONFI_DETECT
	if (detect_onfi() != 0) {
		WARN("Detect ONFI failed\n");
	}
#endif

	if (plat_get_raw_nand_data(&rawnand_dev) != 0) {
		return -EINVAL;
	}

	assert((rawnand_dev.nand_dev->page_size != 0U) &&
	       (rawnand_dev.nand_dev->block_size != 0U) &&
	       (rawnand_dev.nand_dev->size != 0U));

	*size = rawnand_dev.nand_dev->size;
	*erase_size = rawnand_dev.nand_dev->block_size;

	rawnand_dev.ops->setup(rawnand_dev.nand_dev);

	return 0;
}