/* SPDX-License-Identifier: GPL-2.0 */
/*
 * (C) Copyright 2005
 * 2N Telekomunikace, a.s. <www.2n.cz>
 * Ladislav Michl <michl@2n.cz>
 */

#ifndef _NAND_H_
#define _NAND_H_

#include <config.h>

extern void nand_init(void);
unsigned long nand_size(void);

#include <linux/compat.h>
#include <linux/mtd/mtd.h>

int nand_mtd_to_devnum(struct mtd_info *mtd);

#if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
void board_nand_init(void);
int nand_register(int devnum, struct mtd_info *mtd);
#else
struct nand_chip;

extern int board_nand_init(struct nand_chip *nand);
#endif

extern int nand_curr_device;

static inline int nand_read(struct mtd_info *info, loff_t ofs, size_t *len,
			    u_char *buf)
{
	return mtd_read(info, ofs, *len, (size_t *)len, buf);
}

static inline int nand_write(struct mtd_info *info, loff_t ofs, size_t *len,
			     u_char *buf)
{
	return mtd_write(info, ofs, *len, (size_t *)len, buf);
}

static inline int nand_block_isbad(struct mtd_info *info, loff_t ofs)
{
	return mtd_block_isbad(info, ofs);
}

static inline int nand_erase(struct mtd_info *info, loff_t off, size_t size)
{
	struct erase_info instr;

	instr.mtd = info;
	instr.addr = off;
	instr.len = size;

	return mtd_erase(info, &instr);
}


/*****************************************************************************
 * declarations from nand_util.c
 ****************************************************************************/

typedef struct mtd_oob_ops mtd_oob_ops_t;

struct nand_erase_options {
	loff_t length;		/* number of bytes to erase */
	loff_t offset;		/* first address in NAND to erase */
	int quiet;		/* don't display progress messages */
	int jffs2;		/* if true: format for jffs2 usage
				 * (write appropriate cleanmarker blocks) */
	int scrub;		/* if true, really clean NAND by erasing
				 * bad blocks (UNSAFE) */

	/* Don't include skipped bad blocks in size to be erased */
	int spread;
	/* maximum size that actual may be in order to not exceed the buf */
	loff_t lim;
};

typedef struct nand_erase_options nand_erase_options_t;

int nand_read_skip_bad(struct mtd_info *mtd, loff_t offset, size_t *length,
		       size_t *actual, loff_t lim, u_char *buffer);

#define WITH_DROP_FFS	(1 << 0) /* drop trailing all-0xff pages */
#define WITH_WR_VERIFY	(1 << 1) /* verify data was written correctly */

int nand_write_skip_bad(struct mtd_info *mtd, loff_t offset, size_t *length,
			size_t *actual, loff_t lim, u_char *buffer, int flags);
int nand_erase_opts(struct mtd_info *mtd,
		    const nand_erase_options_t *opts);
int nand_torture(struct mtd_info *mtd, loff_t offset);
int nand_verify_page_oob(struct mtd_info *mtd, struct mtd_oob_ops *ops,
			 loff_t ofs);
int nand_verify(struct mtd_info *mtd, loff_t ofs, size_t len, u_char *buf);

#define NAND_LOCK_STATUS_TIGHT	0x01
#define NAND_LOCK_STATUS_UNLOCK 0x04

int nand_lock(struct mtd_info *mtd, int tight);
int nand_unlock(struct mtd_info *mtd, loff_t start, size_t length,
		int allexcept);
int nand_get_lock_status(struct mtd_info *mtd, loff_t offset);

u32 nand_spl_adjust_offset(u32 sector, u32 offs);
int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst);
int nand_spl_read_block(int block, int offset, int len, void *dst);
void nand_deselect(void);

#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
void board_nand_select_device(struct nand_chip *nand, int chip);
#endif

__attribute__((noreturn)) void nand_boot(void);

#ifdef CONFIG_ENV_OFFSET_OOB
#define ENV_OOB_MARKER 0x30425645 /*"EVB0" in little-endian -- offset is stored
				    as block number*/
#define ENV_OOB_MARKER_OLD 0x30564e45 /*"ENV0" in little-endian -- offset is
					stored as byte number */
#define ENV_OFFSET_SIZE 8
int get_nand_env_oob(struct mtd_info *mtd, unsigned long *result);
#endif
int spl_nand_erase_one(int block, int page);

/* platform specific init functions */
void sunxi_nand_init(void);

/*
 * get_nand_dev_by_index - Get the nand info based in index.
 *
 * @dev - index to the nand device.
 *
 * returns pointer to the nand device info structure or NULL on failure.
 */
struct mtd_info *get_nand_dev_by_index(int dev);

#endif /* _NAND_H_ */
