1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * (C) Copyright 2005
4 * 2N Telekomunikace, a.s. <www.2n.cz>
5 * Ladislav Michl <michl@2n.cz>
6 */
7
8#ifndef _NAND_H_
9#define _NAND_H_
10
11#include <config.h>
12
13extern void nand_init(void);
14void nand_reinit(void);
15unsigned long nand_size(void);
16unsigned int nand_page_size(void);
17
18#include <linux/compat.h>
19#include <linux/mtd/mtd.h>
20
21int nand_mtd_to_devnum(struct mtd_info *mtd);
22
23#if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
24void board_nand_init(void);
25int nand_register(int devnum, struct mtd_info *mtd);
26void nand_unregister(struct mtd_info *mtd);
27#else
28struct nand_chip;
29
30extern int board_nand_init(struct nand_chip *nand);
31#endif
32
33extern int nand_curr_device;
34
35static inline int nand_read(struct mtd_info *info, loff_t ofs, size_t *len,
36			    u_char *buf)
37{
38	return mtd_read(info, ofs, *len, (size_t *)len, buf);
39}
40
41static inline int nand_write(struct mtd_info *info, loff_t ofs, size_t *len,
42			     u_char *buf)
43{
44	return mtd_write(info, ofs, *len, (size_t *)len, buf);
45}
46
47static inline int nand_block_isbad(struct mtd_info *info, loff_t ofs)
48{
49	return mtd_block_isbad(info, ofs);
50}
51
52static inline int nand_erase(struct mtd_info *info, loff_t off, size_t size)
53{
54	struct erase_info instr;
55
56	instr.mtd = info;
57	instr.addr = off;
58	instr.len = size;
59
60	return mtd_erase(info, &instr);
61}
62
63
64/*****************************************************************************
65 * declarations from nand_util.c
66 ****************************************************************************/
67
68typedef struct mtd_oob_ops mtd_oob_ops_t;
69
70struct nand_erase_options {
71	loff_t length;		/* number of bytes to erase */
72	loff_t offset;		/* first address in NAND to erase */
73	int quiet;		/* don't display progress messages */
74	int jffs2;		/* if true: format for jffs2 usage
75				 * (write appropriate cleanmarker blocks) */
76	int scrub;		/* if true, really clean NAND by erasing
77				 * bad blocks (UNSAFE) */
78
79	/* Don't include skipped bad blocks in size to be erased */
80	int spread;
81	/* maximum size that actual may be in order to not exceed the buf */
82	loff_t lim;
83};
84
85typedef struct nand_erase_options nand_erase_options_t;
86
87int nand_read_skip_bad(struct mtd_info *mtd, loff_t offset, size_t *length,
88		       size_t *actual, loff_t lim, u_char *buffer);
89
90#define WITH_DROP_FFS	(1 << 0) /* drop trailing all-0xff pages */
91#define WITH_WR_VERIFY	(1 << 1) /* verify data was written correctly */
92
93int nand_write_skip_bad(struct mtd_info *mtd, loff_t offset, size_t *length,
94			size_t *actual, loff_t lim, u_char *buffer, int flags);
95int nand_erase_opts(struct mtd_info *mtd,
96		    const nand_erase_options_t *opts);
97int nand_torture(struct mtd_info *mtd, loff_t offset);
98int nand_verify_page_oob(struct mtd_info *mtd, struct mtd_oob_ops *ops,
99			 loff_t ofs);
100int nand_verify(struct mtd_info *mtd, loff_t ofs, size_t len, u_char *buf);
101
102#define NAND_LOCK_STATUS_TIGHT	0x01
103#define NAND_LOCK_STATUS_UNLOCK 0x04
104
105int nand_lock(struct mtd_info *mtd, int tight);
106int nand_unlock(struct mtd_info *mtd, loff_t start, size_t length,
107		int allexcept);
108int nand_get_lock_status(struct mtd_info *mtd, loff_t offset);
109
110u32 nand_spl_adjust_offset(u32 sector, u32 offs);
111int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst);
112int nand_spl_read_block(int block, int offset, int len, void *dst);
113void nand_deselect(void);
114
115#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
116void board_nand_select_device(struct nand_chip *nand, int chip);
117#endif
118
119__attribute__((noreturn)) void nand_boot(void);
120
121#ifdef CONFIG_ENV_OFFSET_OOB
122#define ENV_OOB_MARKER 0x30425645 /*"EVB0" in little-endian -- offset is stored
123				    as block number*/
124#define ENV_OOB_MARKER_OLD 0x30564e45 /*"ENV0" in little-endian -- offset is
125					stored as byte number */
126#define ENV_OFFSET_SIZE 8
127int get_nand_env_oob(struct mtd_info *mtd, unsigned long *result);
128#endif
129int spl_nand_erase_one(int block, int page);
130
131/* platform specific init functions */
132void sunxi_nand_init(void);
133
134/*
135 * get_nand_dev_by_index - Get the nand info based in index.
136 *
137 * @dev - index to the nand device.
138 *
139 * returns pointer to the nand device info structure or NULL on failure.
140 */
141struct mtd_info *get_nand_dev_by_index(int dev);
142
143#endif /* _NAND_H_ */
144