xref: /freebsd-12.1/sys/dev/nand/nandsim_chip.h (revision 718cf2cc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2009-2012 Semihalf
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef _NANDSIM_CHIP_H
32 #define _NANDSIM_CHIP_H
33 
34 #include <sys/malloc.h>
35 #include <sys/callout.h>
36 #include <dev/nand/nand.h>
37 #include <dev/nand/nandsim.h>
38 #include <dev/nand/nandsim_swap.h>
39 
40 MALLOC_DECLARE(M_NANDSIM);
41 
42 #define MAX_CS_NUM	4
43 struct nandsim_chip;
44 
45 typedef void nandsim_evh_t(struct nandsim_chip *chip, uint32_t ev, void *data);
46 
47 enum addr_type {
48 	ADDR_NONE,
49 	ADDR_ID,
50 	ADDR_ROW,
51 	ADDR_ROWCOL
52 };
53 
54 struct nandsim_softc {
55 	struct nand_softc	nand_dev;
56 	device_t		dev;
57 
58 	struct nandsim_chip	*chips[MAX_CS_NUM];
59 	struct nandsim_chip	*active_chip;
60 
61 	uint8_t			address_cycle;
62 	enum addr_type		address_type;
63 	int			log_idx;
64 	char			*log_buff;
65 	struct alq		*alq;
66 };
67 
68 struct nandsim_ev {
69 	STAILQ_ENTRY(nandsim_ev)	links;
70 	struct nandsim_chip		*chip;
71 	uint8_t		type;
72 	void		*data;
73 };
74 
75 struct nandsim_data {
76 	uint8_t		*data_ptr;
77 	uint32_t	index;
78 	uint32_t	size;
79 };
80 
81 struct nandsim_block_state {
82 	int32_t		wear_lev;
83 	uint8_t		is_bad;
84 };
85 
86 #define NANDSIM_CHIP_ACTIVE	0x1
87 #define NANDSIM_CHIP_FROZEN	0x2
88 #define NANDSIM_CHIP_GET_STATUS	0x4
89 
90 struct nandsim_chip {
91 	struct nandsim_softc	*sc;
92 	struct thread		*nandsim_td;
93 
94 	STAILQ_HEAD(, nandsim_ev) nandsim_events;
95 	nandsim_evh_t		*ev_handler;
96 	struct mtx		ns_lock;
97 	struct callout		ns_callout;
98 
99 	struct chip_geom	cg;
100 	struct nand_id		id;
101 	struct onfi_params	params;
102 	struct nandsim_data	data;
103 	struct nandsim_block_state *blk_state;
104 
105 	struct chip_swap	*swap;
106 
107 	uint32_t	error_ratio;
108 	uint32_t	wear_level;
109 	uint32_t	sm_state;
110 	uint32_t	sm_addr_cycle;
111 
112 	uint32_t	erase_delay;
113 	uint32_t	prog_delay;
114 	uint32_t	read_delay;
115 	struct timeval	delay_tv;
116 
117 	uint8_t		flags;
118 	uint8_t		chip_status;
119 	uint8_t		ctrl_num;
120 	uint8_t		chip_num;
121 };
122 
123 struct sim_ctrl_conf {
124 	uint8_t		num;
125 	uint8_t		num_cs;
126 	uint8_t		ecc;
127 	uint8_t		running;
128 	uint8_t		created;
129 	device_t	sim_ctrl_dev;
130 	struct sim_chip	*chips[MAX_CTRL_CS];
131 	uint16_t	ecc_layout[MAX_ECC_BYTES];
132 	char		filename[FILENAME_SIZE];
133 };
134 
135 #define NANDSIM_STATE_IDLE		0x0
136 #define NANDSIM_STATE_WAIT_ADDR_BYTE	0x1
137 #define NANDSIM_STATE_WAIT_CMD		0x2
138 #define NANDSIM_STATE_TIMEOUT		0x3
139 #define	NANDSIM_STATE_WAIT_ADDR_ROW	0x4
140 #define	NANDSIM_STATE_WAIT_ADDR_COL	0x5
141 
142 #define NANDSIM_EV_START	0x1
143 #define NANDSIM_EV_CMD		0x2
144 #define NANDSIM_EV_ADDR		0x3
145 #define NANDSIM_EV_TIMEOUT	0x4
146 #define NANDSIM_EV_EXIT		0xff
147 
148 struct nandsim_chip *nandsim_chip_init(struct nandsim_softc *,
149     uint8_t, struct sim_chip *);
150 void nandsim_chip_destroy(struct nandsim_chip *);
151 void nandsim_chip_freeze(struct nandsim_chip *);
152 void nandsim_chip_timeout(struct nandsim_chip *);
153 int nandsim_chip_check_bad_block(struct nandsim_chip *, int);
154 
155 uint8_t nandchip_get_status(struct nandsim_chip *);
156 
157 void destroy_event(struct nandsim_ev *);
158 int send_event(struct nandsim_ev *);
159 struct nandsim_ev *create_event(struct nandsim_chip *, uint8_t, uint8_t);
160 
161 #endif /*  _NANDSIM_CHIP_H */
162