summaryrefslogtreecommitdiffstats
path: root/include/drivers/nand.h
blob: 5e5607c648555223295ec8c7430435b43b6cc6b6 (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
/*
 * Copyright (c) 2019-2022, STMicroelectronics - All Rights Reserved
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#ifndef DRIVERS_NAND_H
#define DRIVERS_NAND_H

#include <stddef.h>
#include <stdint.h>

#include <lib/utils_def.h>

#define PSEC_TO_MSEC(x)	div_round_up((x), 1000000000ULL)

struct ecc {
	unsigned int mode; /* ECC mode NAND_ECC_MODE_{NONE|HW|ONDIE} */
	unsigned int size; /* Data byte per ECC step */
	unsigned int bytes; /* ECC bytes per step */
	unsigned int max_bit_corr; /* Max correctible bits per ECC steps */
};

struct nand_device {
	unsigned int block_size;
	unsigned int page_size;
	unsigned long long size;
	unsigned int nb_planes;
	unsigned int buswidth;
	struct ecc ecc;
	int (*mtd_block_is_bad)(unsigned int block);
	int (*mtd_read_page)(struct nand_device *nand, unsigned int page,
			     uintptr_t buffer);
};

void plat_get_scratch_buffer(void **buffer_addr, size_t *buf_size);

/*
 * Read bytes from NAND device
 *
 * @offset: Byte offset to read from in device
 * @buffer: [out] Bytes read from device
 * @length: Number of bytes to read
 * @length_read: [out] Number of bytes read from device
 * Return: 0 on success, a negative errno on failure
 */
int nand_read(unsigned int offset, uintptr_t buffer, size_t length,
	      size_t *length_read);

/*
 * Look for an extra offset to be added in case of bad blocks
 *
 * @base: Base address of the area
 * @offset: Byte offset to read from in device
 * @extra_offset: [out] Extra offset to be added if bad blocks are found
 * Return: 0 on success, a negative errno on failure
 */
int nand_seek_bb(uintptr_t base, unsigned int offset, size_t *extra_offset);

/*
 * Get NAND device instance
 *
 * Return: NAND device instance reference
 */
struct nand_device *get_nand_device(void);

#endif	/* DRIVERS_NAND_H */