xref: /freebsd/sys/dev/pccard/pccardvar.h (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
1 /*	$NetBSD: pcmciavar.h,v 1.12 2000/02/08 12:51:31 enami Exp $	*/
2 /* $FreeBSD$ */
3 
4 /*
5  * Copyright (c) 1997 Marc Horowitz.  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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Marc Horowitz.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/queue.h>
35 
36 #include <machine/bus.h>
37 
38 extern int	pccard_verbose;
39 
40 /*
41  * Contains information about mapped/allocated i/o spaces.
42  */
43 struct pccard_io_handle {
44 	bus_space_tag_t iot;		/* bus space tag (from chipset) */
45 	bus_space_handle_t ioh;		/* mapped space handle */
46 	bus_addr_t      addr;		/* resulting address in bus space */
47 	bus_size_t      size;		/* size of i/o space */
48 	int             flags;		/* misc. information */
49 	int		width;
50 };
51 
52 #define	PCCARD_IO_ALLOCATED	0x01	/* i/o space was allocated */
53 
54 /*
55  * Contains information about allocated memory space.
56  */
57 struct pccard_mem_handle {
58 	bus_space_tag_t memt;		/* bus space tag (from chipset) */
59 	bus_space_handle_t memh;	/* mapped space handle */
60 	bus_addr_t      addr;		/* resulting address in bus space */
61 	bus_size_t      size;		/* size of mem space */
62 	bus_size_t      realsize;	/* how much we really allocated */
63 	long		offset;
64 	int		kind;
65 };
66 
67 /* pccard itself */
68 
69 #define PCCARD_CFE_MWAIT_REQUIRED	0x0001
70 #define PCCARD_CFE_RDYBSY_ACTIVE	0x0002
71 #define PCCARD_CFE_WP_ACTIVE		0x0004
72 #define PCCARD_CFE_BVD_ACTIVE		0x0008
73 #define PCCARD_CFE_IO8			0x0010
74 #define PCCARD_CFE_IO16			0x0020
75 #define PCCARD_CFE_IRQSHARE		0x0040
76 #define PCCARD_CFE_IRQPULSE		0x0080
77 #define PCCARD_CFE_IRQLEVEL		0x0100
78 #define PCCARD_CFE_POWERDOWN		0x0200
79 #define PCCARD_CFE_READONLY		0x0400
80 #define PCCARD_CFE_AUDIO		0x0800
81 
82 struct pccard_config_entry {
83 	int		number;
84 	u_int32_t	flags;
85 	int		iftype;
86 	int		num_iospace;
87 
88 	/*
89 	 * The card will only decode this mask in any case, so we can
90 	 * do dynamic allocation with this in mind, in case the suggestions
91 	 * below are no good.
92 	 */
93 	u_long		iomask;
94 	struct {
95 		u_long	length;
96 		u_long	start;
97 	} iospace[4];		/* XXX this could be as high as 16 */
98 	u_int16_t	irqmask;
99 	int		num_memspace;
100 	struct {
101 		u_long	length;
102 		u_long	cardaddr;
103 		u_long	hostaddr;
104 	} memspace[2];		/* XXX this could be as high as 8 */
105 	int		maxtwins;
106 	struct resource *iores[4];
107 	int		iorid[4];
108 	struct resource *irqres;
109 	int		irqrid;
110 	struct resource *memres[2];
111 	int		memrid[2];
112 	STAILQ_ENTRY(pccard_config_entry) cfe_list;
113 };
114 
115 struct pccard_function {
116 	/* read off the card */
117 	int		number;
118 	int		function;
119 	int		last_config_index;
120 	u_long		ccr_base;
121 	u_long		ccr_mask;
122 	struct resource *ccr_res;
123 	int		ccr_rid;
124 	STAILQ_HEAD(, pccard_config_entry) cfe_head;
125 	STAILQ_ENTRY(pccard_function) pf_list;
126 	/* run-time state */
127 	struct pccard_softc *sc;
128 	struct pccard_config_entry *cfe;
129 	struct pccard_mem_handle pf_pcmh;
130 	device_t	dev;
131 #define	pf_ccrt		pf_pcmh.memt
132 #define	pf_ccrh		pf_pcmh.memh
133 #define	pf_ccr_realsize	pf_pcmh.realsize
134 	bus_addr_t	pf_ccr_offset;
135 	int		pf_ccr_window;
136 	long		pf_mfc_iobase;
137 	long		pf_mfc_iomax;
138 	int		pf_flags;
139 };
140 
141 /* pf_flags */
142 #define	PFF_ENABLED	0x0001		/* function is enabled */
143 
144 struct pccard_card {
145 	int		cis1_major;
146 	int		cis1_minor;
147 	/* XXX waste of space? */
148 	char		cis1_info_buf[256];
149 	char		*cis1_info[4];
150 	/*
151 	 * Use int32_t for manufacturer and product so that they can
152 	 * hold the id value found in card CIS and special value that
153 	 * indicates no id was found.
154 	 */
155 	int32_t		manufacturer;
156 #define	PCCARD_VENDOR_INVALID	-1
157 	int32_t		product;
158 #define	PCCARD_PRODUCT_INVALID		-1
159 	u_int16_t	error;
160 #define	PCCARD_CIS_INVALID		{ NULL, NULL, NULL, NULL }
161 	STAILQ_HEAD(, pccard_function) pf_head;
162 };
163 
164 #define	PCCARD_MEM_ATTR		1
165 #define	PCCARD_MEM_COMMON	2
166 
167 #define	PCCARD_WIDTH_AUTO	0
168 #define	PCCARD_WIDTH_IO8	1
169 #define	PCCARD_WIDTH_IO16	2
170 
171 /* More later? */
172 struct pccard_ivar {
173 	struct resource_list resources;
174 	struct pccard_function *fcn;
175 };
176 
177 struct pccard_softc {
178 	device_t		dev;
179 	/* this stuff is for the socket */
180 
181 	/* this stuff is for the card */
182 	struct pccard_card card;
183 	int		sc_enabled_count;	/* num functions enabled */
184 };
185 
186 void
187 pccardbus_if_setup(struct pccard_softc*);
188 
189 struct pccard_cis_quirk {
190 	int32_t manufacturer;
191 	int32_t product;
192 	char *cis1_info[4];
193 	struct pccard_function *pf;
194 	struct pccard_config_entry *cfe;
195 };
196 
197 struct pccard_tuple {
198 	unsigned int	code;
199 	unsigned int	length;
200 	u_long		mult;
201 	bus_addr_t	ptr;
202 	bus_space_tag_t	memt;
203 	bus_space_handle_t memh;
204 };
205 
206 struct pccard_product {
207 	const char	*pp_name;		/* NULL if end of table */
208 #define PCCARD_VENDOR_ANY ((u_int32_t) -1)
209 	u_int32_t	pp_vendor;
210 #define PCCARD_PRODUCT_ANY ((u_int32_t) -1)
211 	u_int32_t	pp_product;
212 	int		pp_expfunc;
213 	const char	*pp_vendor_str;		/* NULL to not match */
214 	const char	*pp_product_str;	/* NULL to not match */
215 };
216 
217 typedef int (*pccard_product_match_fn) (device_t dev,
218     const struct pccard_product *ent, int vpfmatch);
219 
220 const struct pccard_product
221 	*pccard_product_lookup(device_t dev, const struct pccard_product *tab,
222 	    size_t ent_size, pccard_product_match_fn matchfn);
223 
224 void	pccard_read_cis(struct pccard_softc *);
225 void	pccard_check_cis_quirks(device_t);
226 void	pccard_print_cis(device_t);
227 int	pccard_scan_cis(device_t,
228 		int (*) (struct pccard_tuple *, void *), void *);
229 
230 #define	pccard_cis_read_1(tuple, idx0)					\
231 	(bus_space_read_1((tuple)->memt, (tuple)->memh, (tuple)->mult*(idx0)))
232 
233 #define	pccard_tuple_read_1(tuple, idx1)				\
234 	(pccard_cis_read_1((tuple), ((tuple)->ptr+(2+(idx1)))))
235 
236 #define	pccard_tuple_read_2(tuple, idx2)				\
237 	(pccard_tuple_read_1((tuple), (idx2)) | 			\
238 	 (pccard_tuple_read_1((tuple), (idx2)+1)<<8))
239 
240 #define	pccard_tuple_read_3(tuple, idx3)				\
241 	(pccard_tuple_read_1((tuple), (idx3)) |				\
242 	 (pccard_tuple_read_1((tuple), (idx3)+1)<<8) |			\
243 	 (pccard_tuple_read_1((tuple), (idx3)+2)<<16))
244 
245 #define	pccard_tuple_read_4(tuple, idx4)				\
246 	(pccard_tuple_read_1((tuple), (idx4)) |				\
247 	 (pccard_tuple_read_1((tuple), (idx4)+1)<<8) |			\
248 	 (pccard_tuple_read_1((tuple), (idx4)+2)<<16) |			\
249 	 (pccard_tuple_read_1((tuple), (idx4)+3)<<24))
250 
251 #define	pccard_tuple_read_n(tuple, n, idxn)				\
252 	(((n)==1)?pccard_tuple_read_1((tuple), (idxn)) :		\
253 	 (((n)==2)?pccard_tuple_read_2((tuple), (idxn)) :		\
254 	  (((n)==3)?pccard_tuple_read_3((tuple), (idxn)) :		\
255 	   /* n == 4 */ pccard_tuple_read_4((tuple), (idxn)))))
256 
257 #define	PCCARD_SPACE_MEMORY	1
258 #define	PCCARD_SPACE_IO		2
259 
260 int	pccard_ccr_read(struct pccard_function *, int);
261 void	pccard_ccr_write(struct pccard_function *, int, int);
262 
263 #define	pccard_mfc(sc)	(STAILQ_FIRST(&(sc)->card.pf_head) &&		\
264 		 STAILQ_NEXT(STAILQ_FIRST(&(sc)->card.pf_head),pf_list))
265 
266 /* The following is the vestages of the NetBSD driver api */
267 
268 void	pccard_function_init(struct pccard_function *);
269 int	pccard_function_enable(struct pccard_function *);
270 void	pccard_function_disable(struct pccard_function *);
271 
272 #define	pccard_io_alloc(pf, start, size, align, pciop)			\
273 	(pccard_chip_io_alloc((pf)->sc->pct, pf->sc->pch, (start),	\
274 	 (size), (align), (pciop)))
275 
276 #define	pccard_io_free(pf, pciohp)					\
277 	(pccard_chip_io_free((pf)->sc->pct, (pf)->sc->pch, (pciohp)))
278 
279 int	pccard_io_map(struct pccard_function *, int, bus_addr_t,
280 	    bus_size_t, struct pccard_io_handle *, int *);
281 void	pccard_io_unmap(struct pccard_function *, int);
282 
283 #define pccard_mem_alloc(pf, size, pcmhp)				\
284 	(pccard_chip_mem_alloc((pf)->sc->pct, (pf)->sc->pch, (size), (pcmhp)))
285 #define pccard_mem_free(pf, pcmhp)					\
286 	(pccard_chip_mem_free((pf)->sc->pct, (pf)->sc->pch, (pcmhp)))
287 #define pccard_mem_map(pf, kind, card_addr, size, pcmhp, offsetp, windowp) \
288 	(pccard_chip_mem_map((pf)->sc->pct, (pf)->sc->pch, (kind),	\
289 	 (card_addr), (size), (pcmhp), (offsetp), (windowp)))
290 #define	pccard_mem_unmap(pf, window)					\
291 	(pccard_chip_mem_unmap((pf)->sc->pct, (pf)->sc->pch, (window)))
292 
293 /* compat layer */
294 int pccard_compat_probe(device_t dev);
295 int pccard_compat_attach(device_t dev);
296 
297 /* ivar interface */
298 enum {
299 	PCCARD_IVAR_ETHADDR,	/* read ethernet address from CIS tupple */
300 	PCCARD_IVAR_VENDOR,
301 	PCCARD_IVAR_PRODUCT,
302 	PCCARD_IVAR_FUNCTION_NUMBER,
303 	PCCARD_IVAR_VENDOR_STR,	/* CIS string for "Manufacturer" */
304 	PCCARD_IVAR_PRODUCT_STR,/* CIS strnig for "Product" */
305 	PCCARD_IVAR_CIS3_STR,
306 	PCCARD_IVAR_CIS4_STR
307 };
308 
309 #define PCCARD_ACCESSOR(A, B, T)					\
310 __inline static int							\
311 pccard_get_ ## A(device_t dev, T *t)					\
312 {									\
313 	return BUS_READ_IVAR(device_get_parent(dev), dev, 		\
314 	    PCCARD_IVAR_ ## B, (uintptr_t *) t);			\
315 }
316 
317 PCCARD_ACCESSOR(ether,		ETHADDR,		u_int8_t)
318 PCCARD_ACCESSOR(vendor,		VENDOR,			u_int32_t)
319 PCCARD_ACCESSOR(product,	PRODUCT,		u_int32_t)
320 PCCARD_ACCESSOR(function_number,FUNCTION_NUMBER,	u_int32_t)
321 PCCARD_ACCESSOR(vendor_str,	VENDOR_STR,		char *)
322 PCCARD_ACCESSOR(product_str,	PRODUCT_STR,		char *)
323 PCCARD_ACCESSOR(cis3_str,	CIS3_STR,		char *)
324 
325 enum {
326 	PCCARD_A_MEM_ATTR = 0x1
327 };
328 
329 #define PCCARD_SOFTC(d) (struct pccard_softc *) device_get_softc(d)
330 #define PCCARD_IVAR(d) (struct pccard_ivar *) device_get_ivars(d)
331