xref: /freebsd/sys/dev/altera/sdcard/altera_sdcard.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 Robert N. M. Watson
5  * All rights reserved.
6  *
7  * This software was developed by SRI International and the University of
8  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
9  * ("CTSRD"), as part of the DARPA CRASH research programme.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef _DEV_ALTERA_SDCARD_H_
34 #define	_DEV_ALTERA_SDCARD_H_
35 
36 #define	ALTERA_SDCARD_CSD_SIZE	16
37 struct altera_sdcard_csd {
38 	uint8_t		 csd_data[ALTERA_SDCARD_CSD_SIZE];
39 } __aligned(2);		/* CSD is read in 16-bit chunks, so align to match. */
40 
41 struct altera_sdcard_softc {
42 	device_t		 as_dev;
43 	int			 as_unit;
44 	struct resource		*as_res;
45 	int			 as_rid;
46 	struct mtx		 as_lock;
47 	struct cv		 as_condvar;
48 	int			 as_state;
49 	int			 as_flags;
50 	struct disk		*as_disk;
51 	struct taskqueue	*as_taskqueue;
52 	struct timeout_task	 as_task;
53 
54 	/*
55 	 * Fields relating to in-progress and pending I/O, if any.
56 	 */
57 	struct bio_queue_head	 as_bioq;
58 	struct bio		*as_currentbio;
59 	u_int			 as_retriesleft;
60 
61 	/*
62 	 * Infrequently changing fields cached from the SD Card IP Core.
63 	 */
64 	struct altera_sdcard_csd	 as_csd;
65 	uint8_t			 as_csd_structure;	/* CSD version. */
66 	uint64_t		 as_mediasize;
67 };
68 
69 #define	ALTERA_SDCARD_LOCK(sc)		mtx_lock(&(sc)->as_lock)
70 #define	ALTERA_SDCARD_LOCK_ASSERT(sc)	mtx_assert(&(sc)->as_lock, MA_OWNED)
71 #define	ALTERA_SDCARD_LOCK_DESTROY(sc)	mtx_destroy(&(sc)->as_lock)
72 #define	ALTERA_SDCARD_LOCK_INIT(sc)	mtx_init(&(sc)->as_lock,	\
73 					    "altera_sdcard", NULL, MTX_DEF)
74 #define	ALTERA_SDCARD_UNLOCK(sc)	mtx_unlock(&(sc)->as_lock)
75 
76 #define	ALTERA_SDCARD_CONDVAR_DESTROY(sc)	cv_destroy(&(sc)->as_condvar)
77 #define	ALTERA_SDCARD_CONDVAR_INIT(sc)		cv_init(&(sc)->as_condvar, \
78 						    "altera_sdcard_detach_wait")
79 #define	ALTERA_SDCARD_CONDVAR_SIGNAL(dc)	cv_signal(&(sc)->as_condvar)
80 #define	ALTERA_SDCARD_CONDVAR_WAIT(sc)		cv_wait(&(sc)->as_condvar, \
81 						    &(sc)->as_lock)
82 
83 /*
84  * States an instance can be in at any given moment.
85  */
86 #define	ALTERA_SDCARD_STATE_NOCARD	1	/* No card inserted. */
87 #define	ALTERA_SDCARD_STATE_BADCARD	2	/* Card bad/not supported. */
88 #define	ALTERA_SDCARD_STATE_IDLE	3	/* Card present but idle. */
89 #define	ALTERA_SDCARD_STATE_IO		4	/* Card in I/O currently. */
90 #define	ALTERA_SDCARD_STATE_DETACHED	5	/* Driver is detaching. */
91 
92 /*
93  * Different timeout intervals based on state.  When just looking for a card
94  * status change, check twice a second.  When we're actively waiting on I/O
95  * completion, check every millisecond.
96  */
97 #define	ALTERA_SDCARD_TIMEOUT_NOCARD	(hz/2)
98 #define	ALTERA_SDCARD_TIMEOUT_IDLE	(hz/2)
99 #define	ALTERA_SDCARD_TIMEOUT_IO	(1)
100 #define	ALTERA_SDCARD_TIMEOUT_IOERROR	(hz/5)
101 
102 /*
103  * Maximum number of retries on an I/O.
104  */
105 #define	ALTERA_SDCARD_RETRY_LIMIT	10
106 
107 /*
108  * Driver status flags.
109  */
110 #define	ALTERA_SDCARD_FLAG_DETACHREQ	0x00000001	/* Detach requested. */
111 #define	ALTERA_SDCARD_FLAG_IOERROR	0x00000002	/* Error in progress. */
112 
113 /*
114  * Functions for performing low-level register and memory I/O to/from the SD
115  * Card IP Core.  In general, only code in altera_sdcard_io.c is aware of the
116  * hardware interface.
117  */
118 uint16_t	altera_sdcard_read_asr(struct altera_sdcard_softc *sc);
119 int		altera_sdcard_read_csd(struct altera_sdcard_softc *sc);
120 
121 int	altera_sdcard_io_complete(struct altera_sdcard_softc *sc,
122 	    uint16_t asr);
123 void	altera_sdcard_io_start(struct altera_sdcard_softc *sc,
124 	    struct bio *bp);
125 
126 /*
127  * Constants for interpreting the SD Card Card Specific Data (CSD) register.
128  */
129 #define	ALTERA_SDCARD_CSD_STRUCTURE_BYTE	15
130 #define	ALTERA_SDCARD_CSD_STRUCTURE_MASK	0xc0	/* 2 bits */
131 #define	ALTERA_SDCARD_CSD_STRUCTURE_RSHIFT	6
132 
133 #define	ALTERA_SDCARD_CSD_READ_BL_LEN_BYTE	10
134 #define	ALTERA_SDCARD_CSD_READ_BL_LEN_MASK	0x0f	/* 4 bits */
135 
136 /*
137  * C_SIZE is a 12-bit field helpfully split over three differe bytes of CSD
138  * data.  Software ease of use was not a design consideration.
139  */
140 #define	ALTERA_SDCARD_CSD_C_SIZE_BYTE0		7
141 #define	ALTERA_SDCARD_CSD_C_SIZE_MASK0		0xc0	/* top 2 bits */
142 #define	ALTERA_SDCARD_CSD_C_SIZE_RSHIFT0	6
143 
144 #define	ALTERA_SDCARD_CSD_C_SIZE_BYTE1		8
145 #define	ALTERA_SDCARD_CSD_C_SIZE_MASK1		0xff	/* 8 bits */
146 #define	ALTERA_SDCARD_CSD_C_SIZE_LSHIFT1	2
147 
148 #define	ALTERA_SDCARD_CSD_C_SIZE_BYTE2		9
149 #define	ALTERA_SDCARD_CSD_C_SIZE_MASK2		0x03	/* bottom 2 bits */
150 #define	ALTERA_SDCARD_CSD_C_SIZE_LSHIFT2	10
151 
152 #define	ALTERA_SDCARD_CSD_C_SIZE_MULT_BYTE0	5
153 #define	ALTERA_SDCARD_CSD_C_SIZE_MULT_MASK0	0x80	/* top 1 bit */
154 #define	ALTERA_SDCARD_CSD_C_SIZE_MULT_RSHIFT0	7
155 
156 #define	ALTERA_SDCARD_CSD_C_SIZE_MULT_BYTE1	6
157 #define	ALTERA_SDCARD_CSD_C_SIZE_MULT_MASK1	0x03	/* bottom 2 bits */
158 #define	ALTERA_SDCARD_CSD_C_SIZE_MULT_LSHIFT1	1
159 
160 /*
161  * I/O register/buffer offsets, from Table 4.1.1 in the Altera University
162  * Program SD Card IP Core specification.
163  */
164 #define	ALTERA_SDCARD_OFF_RXTX_BUFFER	0	/* 512-byte I/O buffer */
165 #define	ALTERA_SDCARD_OFF_CID		512	/* 16-byte Card ID number */
166 #define	ALTERA_SDCARD_OFF_CSD		528	/* 16-byte Card Specific Data */
167 #define	ALTERA_SDCARD_OFF_OCR		544	/* Operating Conditions Reg */
168 #define	ALTERA_SDCARD_OFF_SR		548	/* SD Card Status Register */
169 #define	ALTERA_SDCARD_OFF_RCA		552	/* Relative Card Address Reg */
170 #define	ALTERA_SDCARD_OFF_CMD_ARG	556	/* Command Argument Register */
171 #define	ALTERA_SDCARD_OFF_CMD		560	/* Command Register */
172 #define	ALTERA_SDCARD_OFF_ASR		564	/* Auxiliary Status Register */
173 #define	ALTERA_SDCARD_OFF_RR1		568	/* Response R1 */
174 
175 /*
176  * The Altera IP Core provides a 16-bit "Additional Status Register" (ASR)
177  * beyond those described in the SD Card specification that captures IP Core
178  * transaction state, such as whether the last command is in progress, the
179  * card has been removed, etc.
180  */
181 #define	ALTERA_SDCARD_ASR_CMDVALID	0x0001
182 #define	ALTERA_SDCARD_ASR_CARDPRESENT	0x0002
183 #define	ALTERA_SDCARD_ASR_CMDINPROGRESS	0x0004
184 #define	ALTERA_SDCARD_ASR_SRVALID	0x0008
185 #define	ALTERA_SDCARD_ASR_CMDTIMEOUT	0x0010
186 #define	ALTERA_SDCARD_ASR_CMDDATAERROR	0x0020
187 
188 /*
189  * The Altera IP Core claims to provide a 16-bit "Response R1" register (RR1)
190  * to provide more detailed error reporting when a read or write fails.
191  *
192  * XXXRW: The specification claims that this field is 16-bit, but then
193  * proceeds to define values as though it is 32-bit.  In practice, 16-bit
194  * seems more likely as the register is not 32-bit aligned.
195  */
196 #define	ALTERA_SDCARD_RR1_INITPROCRUNNING	0x0100
197 #define	ALTERA_SDCARD_RR1_ERASEINTERRUPTED	0x0200
198 #define	ALTERA_SDCARD_RR1_ILLEGALCOMMAND	0x0400
199 #define	ALTERA_SDCARD_RR1_COMMANDCRCFAILED	0x0800
200 #define	ALTERA_SDCARD_RR1_ADDRESSMISALIGNED	0x1000
201 #define	ALTERA_SDCARD_RR1_ADDRBLOCKRANGE	0x2000
202 
203 /*
204  * Not all RR1 values are "errors" per se -- check only for the ones that are
205  * when performing error handling.
206  */
207 #define	ALTERA_SDCARD_RR1_ERRORMASK					      \
208     (ALTERA_SDCARD_RR1_ERASEINTERRUPTED | ALTERA_SDCARD_RR1_ILLEGALCOMMAND |  \
209     ALTERA_SDCARD_RR1_COMMANDCRCFAILED | ALTERA_SDCARD_RR1_ADDRESSMISALIGNED |\
210     ALTERA_SDCARD_RR1_ADDRBLOCKRANGE)
211 
212 /*
213  * Although SD Cards may have various sector sizes, the Altera IP Core
214  * requires that I/O be done in 512-byte chunks.
215  */
216 #define	ALTERA_SDCARD_SECTORSIZE	512
217 
218 /*
219  * SD Card commands used in this driver.
220  */
221 #define	ALTERA_SDCARD_CMD_SEND_RCA	0x03	/* Retrieve card RCA. */
222 #define	ALTERA_SDCARD_CMD_SEND_CSD	0x09	/* Retrieve CSD register. */
223 #define	ALTERA_SDCARD_CMD_SEND_CID	0x0A	/* Retrieve CID register. */
224 #define	ALTERA_SDCARD_CMD_READ_BLOCK	0x11	/* Read block from disk. */
225 #define	ALTERA_SDCARD_CMD_WRITE_BLOCK	0x18	/* Write block to disk. */
226 
227 /*
228  * Functions exposed by the device driver core to newbus(9) bus attachment
229  * implementations.
230  */
231 void	altera_sdcard_attach(struct altera_sdcard_softc *sc);
232 void	altera_sdcard_detach(struct altera_sdcard_softc *sc);
233 void	altera_sdcard_task(void *arg, int pending);
234 
235 /*
236  * Functions exposed by the device driver core to the disk(9) front-end.
237  */
238 void	altera_sdcard_start(struct altera_sdcard_softc *sc);
239 
240 /*
241  * Functions relating to the implementation of disk(9) KPIs for the SD Card
242  * driver.
243  */
244 void	altera_sdcard_disk_insert(struct altera_sdcard_softc *sc);
245 void	altera_sdcard_disk_remove(struct altera_sdcard_softc *sc);
246 
247 #endif	/* _DEV_ALTERA_SDCARD_H_ */
248