aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/nand/nandsim_chip.h
blob: 86ced5ea2bf271687853810be581435318060d34 (plain) (blame)
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
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (C) 2009-2012 Semihalf
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#ifndef _NANDSIM_CHIP_H
#define _NANDSIM_CHIP_H

#include <sys/malloc.h>
#include <sys/callout.h>
#include <dev/nand/nand.h>
#include <dev/nand/nandsim.h>
#include <dev/nand/nandsim_swap.h>

MALLOC_DECLARE(M_NANDSIM);

#define MAX_CS_NUM	4
struct nandsim_chip;

typedef void nandsim_evh_t(struct nandsim_chip *chip, uint32_t ev, void *data);

enum addr_type {
	ADDR_NONE,
	ADDR_ID,
	ADDR_ROW,
	ADDR_ROWCOL
};

struct nandsim_softc {
	struct nand_softc	nand_dev;
	device_t		dev;

	struct nandsim_chip	*chips[MAX_CS_NUM];
	struct nandsim_chip	*active_chip;

	uint8_t			address_cycle;
	enum addr_type		address_type;
	int			log_idx;
	char			*log_buff;
	struct alq		*alq;
};

struct nandsim_ev {
	STAILQ_ENTRY(nandsim_ev)	links;
	struct nandsim_chip		*chip;
	uint8_t		type;
	void		*data;
};

struct nandsim_data {
	uint8_t		*data_ptr;
	uint32_t	index;
	uint32_t	size;
};

struct nandsim_block_state {
	int32_t		wear_lev;
	uint8_t		is_bad;
};

#define NANDSIM_CHIP_ACTIVE	0x1
#define NANDSIM_CHIP_FROZEN	0x2
#define NANDSIM_CHIP_GET_STATUS	0x4

struct nandsim_chip {
	struct nandsim_softc	*sc;
	struct thread		*nandsim_td;

	STAILQ_HEAD(, nandsim_ev) nandsim_events;
	nandsim_evh_t		*ev_handler;
	struct mtx		ns_lock;
	struct callout		ns_callout;

	struct chip_geom	cg;
	struct nand_id		id;
	struct onfi_params	params;
	struct nandsim_data	data;
	struct nandsim_block_state *blk_state;

	struct chip_swap	*swap;

	uint32_t	error_ratio;
	uint32_t	wear_level;
	uint32_t	sm_state;
	uint32_t	sm_addr_cycle;

	uint32_t	erase_delay;
	uint32_t	prog_delay;
	uint32_t	read_delay;
	struct timeval	delay_tv;

	uint8_t		flags;
	uint8_t		chip_status;
	uint8_t		ctrl_num;
	uint8_t		chip_num;
};

struct sim_ctrl_conf {
	uint8_t		num;
	uint8_t		num_cs;
	uint8_t		ecc;
	uint8_t		running;
	uint8_t		created;
	device_t	sim_ctrl_dev;
	struct sim_chip	*chips[MAX_CTRL_CS];
	uint16_t	ecc_layout[MAX_ECC_BYTES];
	char		filename[FILENAME_SIZE];
};

#define NANDSIM_STATE_IDLE		0x0
#define NANDSIM_STATE_WAIT_ADDR_BYTE	0x1
#define NANDSIM_STATE_WAIT_CMD		0x2
#define NANDSIM_STATE_TIMEOUT		0x3
#define	NANDSIM_STATE_WAIT_ADDR_ROW	0x4
#define	NANDSIM_STATE_WAIT_ADDR_COL	0x5

#define NANDSIM_EV_START	0x1
#define NANDSIM_EV_CMD		0x2
#define NANDSIM_EV_ADDR		0x3
#define NANDSIM_EV_TIMEOUT	0x4
#define NANDSIM_EV_EXIT		0xff

struct nandsim_chip *nandsim_chip_init(struct nandsim_softc *,
    uint8_t, struct sim_chip *);
void nandsim_chip_destroy(struct nandsim_chip *);
void nandsim_chip_freeze(struct nandsim_chip *);
void nandsim_chip_timeout(struct nandsim_chip *);
int nandsim_chip_check_bad_block(struct nandsim_chip *, int);

uint8_t nandchip_get_status(struct nandsim_chip *);

void destroy_event(struct nandsim_ev *);
int send_event(struct nandsim_ev *);
struct nandsim_ev *create_event(struct nandsim_chip *, uint8_t, uint8_t);

#endif /*  _NANDSIM_CHIP_H */