xref: /openbsd/sys/dev/sdmmc/sdmmcvar.h (revision 404b540a)
1 /*	$OpenBSD: sdmmcvar.h,v 1.16 2009/04/07 16:35:52 blambert Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef _SDMMCVAR_H_
20 #define _SDMMCVAR_H_
21 
22 #include <sys/queue.h>
23 #include <sys/lock.h>
24 
25 #include <scsi/scsi_all.h>
26 #include <scsi/scsiconf.h>
27 
28 #include <dev/sdmmc/sdmmcchip.h>
29 #include <dev/sdmmc/sdmmcreg.h>
30 
31 struct sdmmc_csd {
32 	int	csdver;		/* CSD structure format */
33 	int	mmcver;		/* MMC version (for CID format) */
34 	int	capacity;	/* total number of sectors */
35 	int	sector_size;	/* sector size in bytes */
36 	int	read_bl_len;	/* block length for reads */
37 	/* ... */
38 };
39 
40 struct sdmmc_cid {
41 	int	mid;		/* manufacturer identification number */
42 	int	oid;		/* OEM/product identification number */
43 	char	pnm[8];		/* product name (MMC v1 has the longest) */
44 	int	rev;		/* product revision */
45 	int	psn;		/* product serial number */
46 	int	mdt;		/* manufacturing date */
47 };
48 
49 typedef u_int32_t sdmmc_response[4];
50 
51 struct sdmmc_softc;
52 
53 struct sdmmc_task {
54 	void (*func)(void *arg);
55 	void *arg;
56 	int onqueue;
57 	struct sdmmc_softc *sc;
58 	TAILQ_ENTRY(sdmmc_task) next;
59 };
60 
61 #define	sdmmc_init_task(xtask, xfunc, xarg) do {			\
62 	(xtask)->func = (xfunc);					\
63 	(xtask)->arg = (xarg);						\
64 	(xtask)->onqueue = 0;						\
65 	(xtask)->sc = NULL;						\
66 } while (0)
67 
68 #define sdmmc_task_pending(xtask) ((xtask)->onqueue)
69 
70 struct sdmmc_command {
71 	struct sdmmc_task c_task;	/* task queue entry */
72 	u_int16_t	 c_opcode;	/* SD or MMC command index */
73 	u_int32_t	 c_arg;		/* SD/MMC command argument */
74 	sdmmc_response	 c_resp;	/* response buffer */
75 	void		*c_data;	/* buffer to send or read into */
76 	int		 c_datalen;	/* length of data buffer */
77 	int		 c_blklen;	/* block length */
78 	int		 c_flags;	/* see below */
79 #define SCF_ITSDONE	 0x0001		/* command is complete */
80 #define SCF_CMD(flags)	 ((flags) & 0x00f0)
81 #define SCF_CMD_AC	 0x0000
82 #define SCF_CMD_ADTC	 0x0010
83 #define SCF_CMD_BC	 0x0020
84 #define SCF_CMD_BCR	 0x0030
85 #define SCF_CMD_READ	 0x0040		/* read command (data expected) */
86 #define SCF_RSP_BSY	 0x0100
87 #define SCF_RSP_136	 0x0200
88 #define SCF_RSP_CRC	 0x0400
89 #define SCF_RSP_IDX	 0x0800
90 #define SCF_RSP_PRESENT	 0x1000
91 /* response types */
92 #define SCF_RSP_R0	 0 /* none */
93 #define SCF_RSP_R1	 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
94 #define SCF_RSP_R1B	 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX|SCF_RSP_BSY)
95 #define SCF_RSP_R2	 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_136)
96 #define SCF_RSP_R3	 (SCF_RSP_PRESENT)
97 #define SCF_RSP_R4	 (SCF_RSP_PRESENT)
98 #define SCF_RSP_R5	 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
99 #define SCF_RSP_R5B	 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX|SCF_RSP_BSY)
100 #define SCF_RSP_R6	 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
101 #define SCF_RSP_R7	 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
102 	int		 c_error;	/* errno value on completion */
103 
104 	/* Host controller owned fields for data xfer in progress */
105 	int c_resid;			/* remaining I/O */
106 	u_char *c_buf;			/* remaining data */
107 };
108 
109 /*
110  * Decoded PC Card 16 based Card Information Structure (CIS),
111  * per card (function 0) and per function (1 and greater).
112  */
113 struct sdmmc_cis {
114 	u_int16_t	 manufacturer;
115 #define SDMMC_VENDOR_INVALID	0xffff
116 	u_int16_t	 product;
117 #define SDMMC_PRODUCT_INVALID	0xffff
118 	u_int8_t	 function;
119 #define SDMMC_FUNCTION_INVALID	0xff
120 	u_char		 cis1_major;
121 	u_char		 cis1_minor;
122 	char		 cis1_info_buf[256];
123 	char		*cis1_info[4];
124 };
125 
126 /*
127  * Structure describing either an SD card I/O function or a SD/MMC
128  * memory card from a "stack of cards" that responded to CMD2.  For a
129  * combo card with one I/O function and one memory card, there will be
130  * two of these structures allocated.  Each card slot has such a list
131  * of sdmmc_function structures.
132  */
133 struct sdmmc_function {
134 	/* common members */
135 	struct sdmmc_softc *sc;		/* card slot softc */
136 	u_int16_t rca;			/* relative card address */
137 	int flags;
138 #define SFF_ERROR		0x0001	/* function is poo; ignore it */
139 #define SFF_SDHC		0x0002	/* SD High Capacity card */
140 	SIMPLEQ_ENTRY(sdmmc_function) sf_list;
141 	/* SD card I/O function members */
142 	int number;			/* I/O function number or -1 */
143 	struct device *child;		/* function driver */
144 	struct sdmmc_cis cis;		/* decoded CIS */
145 	/* SD/MMC memory card members */
146 	struct sdmmc_csd csd;		/* decoded CSD value */
147 	struct sdmmc_cid cid;		/* decoded CID value */
148 	sdmmc_response raw_cid;		/* temp. storage for decoding */
149 };
150 
151 /*
152  * Structure describing a single SD/MMC/SDIO card slot.
153  */
154 struct sdmmc_softc {
155 	struct device sc_dev;		/* base device */
156 #define SDMMCDEVNAME(sc)	((sc)->sc_dev.dv_xname)
157 	sdmmc_chipset_tag_t sct;	/* host controller chipset tag */
158 	sdmmc_chipset_handle_t sch;	/* host controller chipset handle */
159 	int sc_flags;
160 #define SMF_SD_MODE		0x0001	/* host in SD mode (MMC otherwise) */
161 #define SMF_IO_MODE		0x0002	/* host in I/O mode (SD mode only) */
162 #define SMF_MEM_MODE		0x0004	/* host in memory mode (SD or MMC) */
163 #define SMF_CARD_PRESENT	0x0010	/* card presence noticed */
164 #define SMF_CARD_ATTACHED	0x0020	/* card driver(s) attached */
165 #define	SMF_STOP_AFTER_MULTIPLE	0x0040	/* send a stop after a multiple cmd */
166 	int sc_function_count;		/* number of I/O functions (SDIO) */
167 	struct sdmmc_function *sc_card;	/* selected card */
168 	struct sdmmc_function *sc_fn0;	/* function 0, the card itself */
169 	SIMPLEQ_HEAD(, sdmmc_function) sf_head; /* list of card functions */
170 	int sc_dying;			/* bus driver is shutting down */
171 	struct proc *sc_task_thread;	/* asynchronous tasks */
172 	TAILQ_HEAD(, sdmmc_task) sc_tskq;   /* task thread work queue */
173 	struct sdmmc_task sc_discover_task; /* card attach/detach task */
174 	struct sdmmc_task sc_intr_task;	/* card interrupt task */
175 	struct lock sc_lock;		/* lock around host controller */
176 	void *sc_scsibus;		/* SCSI bus emulation softc */
177 	TAILQ_HEAD(, sdmmc_intr_handler) sc_intrq; /* interrupt handlers */
178 	long sc_max_xfer;		/* maximum transfer size */
179 };
180 
181 /*
182  * Attach devices at the sdmmc bus.
183  */
184 struct sdmmc_attach_args {
185 	struct scsi_link *scsi_link;	/* XXX */
186 	struct sdmmc_function *sf;
187 };
188 
189 #define IPL_SDMMC	IPL_BIO
190 #define splsdmmc()	splbio()
191 
192 #define SDMMC_LOCK(sc)	 lockmgr(&(sc)->sc_lock, LK_EXCLUSIVE, NULL)
193 #define SDMMC_UNLOCK(sc) lockmgr(&(sc)->sc_lock, LK_RELEASE, NULL)
194 #define	SDMMC_ASSERT_LOCKED(sc) \
195 	KASSERT(lockstatus(&((sc))->sc_lock) == LK_EXCLUSIVE)
196 
197 void	sdmmc_add_task(struct sdmmc_softc *, struct sdmmc_task *);
198 void	sdmmc_del_task(struct sdmmc_task *);
199 
200 struct	sdmmc_function *sdmmc_function_alloc(struct sdmmc_softc *);
201 void	sdmmc_function_free(struct sdmmc_function *);
202 int	sdmmc_set_bus_power(struct sdmmc_softc *, u_int32_t, u_int32_t);
203 int	sdmmc_mmc_command(struct sdmmc_softc *, struct sdmmc_command *);
204 int	sdmmc_app_command(struct sdmmc_softc *, struct sdmmc_command *);
205 void	sdmmc_go_idle_state(struct sdmmc_softc *);
206 int	sdmmc_select_card(struct sdmmc_softc *, struct sdmmc_function *);
207 int	sdmmc_set_relative_addr(struct sdmmc_softc *,
208 	    struct sdmmc_function *);
209 int	sdmmc_send_if_cond(struct sdmmc_softc *, uint32_t);
210 
211 void	sdmmc_intr_enable(struct sdmmc_function *);
212 void	sdmmc_intr_disable(struct sdmmc_function *);
213 void	*sdmmc_intr_establish(struct device *, int (*)(void *),
214 	    void *, const char *);
215 void	sdmmc_intr_disestablish(void *);
216 void	sdmmc_intr_task(void *);
217 
218 int	sdmmc_io_enable(struct sdmmc_softc *);
219 void	sdmmc_io_scan(struct sdmmc_softc *);
220 int	sdmmc_io_init(struct sdmmc_softc *, struct sdmmc_function *);
221 void	sdmmc_io_attach(struct sdmmc_softc *);
222 void	sdmmc_io_detach(struct sdmmc_softc *);
223 u_int8_t sdmmc_io_read_1(struct sdmmc_function *, int);
224 u_int16_t sdmmc_io_read_2(struct sdmmc_function *, int);
225 u_int32_t sdmmc_io_read_4(struct sdmmc_function *, int);
226 int	sdmmc_io_read_multi_1(struct sdmmc_function *, int, u_char *, int);
227 void	sdmmc_io_write_1(struct sdmmc_function *, int, u_int8_t);
228 void	sdmmc_io_write_2(struct sdmmc_function *, int, u_int16_t);
229 void	sdmmc_io_write_4(struct sdmmc_function *, int, u_int32_t);
230 int	sdmmc_io_write_multi_1(struct sdmmc_function *, int, u_char *, int);
231 int	sdmmc_io_function_ready(struct sdmmc_function *);
232 int	sdmmc_io_function_enable(struct sdmmc_function *);
233 void	sdmmc_io_function_disable(struct sdmmc_function *);
234 
235 int	sdmmc_read_cis(struct sdmmc_function *, struct sdmmc_cis *);
236 void	sdmmc_print_cis(struct sdmmc_function *);
237 void	sdmmc_check_cis_quirks(struct sdmmc_function *);
238 
239 int	sdmmc_mem_enable(struct sdmmc_softc *);
240 void	sdmmc_mem_scan(struct sdmmc_softc *);
241 int	sdmmc_mem_init(struct sdmmc_softc *, struct sdmmc_function *);
242 int	sdmmc_mem_read_block(struct sdmmc_function *, int, u_char *, size_t);
243 int	sdmmc_mem_write_block(struct sdmmc_function *, int, u_char *, size_t);
244 
245 /* ioctls */
246 
247 #include <sys/ioccom.h>
248 
249 struct bio_sdmmc_command {
250 	void *cookie;
251 	struct sdmmc_command cmd;
252 };
253 
254 struct bio_sdmmc_debug {
255 	void *cookie;
256 	int debug;
257 };
258 
259 #define SDIOCEXECMMC	_IOWR('S',0, struct bio_sdmmc_command)
260 #define SDIOCEXECAPP	_IOWR('S',1, struct bio_sdmmc_command)
261 #define SDIOCSETDEBUG	_IOWR('S',2, struct bio_sdmmc_debug)
262 
263 #endif
264