xref: /dragonfly/sys/bus/pccard/pccardvar.h (revision 8a7bdfea)
1 /*	$NetBSD: pcmciavar.h,v 1.12 2000/02/08 12:51:31 enami Exp $	*/
2 /* $FreeBSD: src/sys/dev/pccard/pccardvar.h,v 1.54 2005/07/13 15:00:59 imp Exp $ */
3 /* $DragonFly: src/sys/bus/pccard/pccardvar.h,v 1.8 2007/08/14 15:32:32 sephe Exp $ */
4 
5 /*-
6  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Marc Horowitz.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 extern int	pccard_verbose;
35 
36 /*
37  * PCCARD_API_LEVEL.  When set to 5, we provide a 5.x compatable API
38  * for driver writers that have to share their code between 5.x and 6.x.
39  * The 5.x compatibility interfaces will be unsupported in 7.0, at which
40  * point we'll only support 6 and newer, etc.
41  */
42 #ifndef PCCARD_API_LEVEL
43 #define PCCARD_API_LEVEL 5
44 #elif PCCARD_API_LEVEL < 5
45 #error "pccard API less than 5 unsupported"
46 #endif
47 
48 /*
49  * Contains information about mapped/allocated i/o spaces.
50  */
51 struct pccard_io_handle {
52 	bus_space_tag_t iot;		/* bus space tag (from chipset) */
53 	bus_space_handle_t ioh;		/* mapped space handle */
54 	bus_addr_t      addr;		/* resulting address in bus space */
55 	bus_size_t      size;		/* size of i/o space */
56 	int             flags;		/* misc. information */
57 	int		width;
58 };
59 
60 #define	PCCARD_IO_ALLOCATED	0x01	/* i/o space was allocated */
61 
62 /*
63  * Contains information about allocated memory space.
64  */
65 struct pccard_mem_handle {
66 	bus_space_tag_t memt;		/* bus space tag (from chipset) */
67 	bus_space_handle_t memh;	/* mapped space handle */
68 	bus_addr_t      addr;		/* resulting address in bus space */
69 	bus_size_t      size;		/* size of mem space */
70 	bus_size_t      realsize;	/* how much we really allocated */
71 	bus_addr_t	cardaddr;	/* Absolute address on card */
72 	int		kind;
73 };
74 
75 /* pccard itself */
76 
77 #define PCCARD_CFE_MWAIT_REQUIRED	0x0001
78 #define PCCARD_CFE_RDYBSY_ACTIVE	0x0002
79 #define PCCARD_CFE_WP_ACTIVE		0x0004
80 #define PCCARD_CFE_BVD_ACTIVE		0x0008
81 #define PCCARD_CFE_IO8			0x0010
82 #define PCCARD_CFE_IO16			0x0020
83 #define PCCARD_CFE_IRQSHARE		0x0040
84 #define PCCARD_CFE_IRQPULSE		0x0080
85 #define PCCARD_CFE_IRQLEVEL		0x0100
86 #define PCCARD_CFE_POWERDOWN		0x0200
87 #define PCCARD_CFE_READONLY		0x0400
88 #define PCCARD_CFE_AUDIO		0x0800
89 
90 struct pccard_config_entry {
91 	int		number;
92 	uint32_t	flags;
93 	int		iftype;
94 	int		num_iospace;
95 
96 	/*
97 	 * The card will only decode this mask in any case, so we can
98 	 * do dynamic allocation with this in mind, in case the suggestions
99 	 * below are no good.
100 	 */
101 	u_long		iomask;
102 	struct {
103 		u_long	length;
104 		u_long	start;
105 	} iospace[4];		/* XXX this could be as high as 16 */
106 	uint16_t	irqmask;
107 	int		num_memspace;
108 	struct {
109 		u_long	length;
110 		u_long	cardaddr;
111 		u_long	hostaddr;
112 	} memspace[2];		/* XXX this could be as high as 8 */
113 	int		maxtwins;
114 	STAILQ_ENTRY(pccard_config_entry) cfe_list;
115 };
116 
117 struct pccard_funce_disk {
118 	int pfd_interface;
119 };
120 
121 struct pccard_funce_lan {
122 	int pfl_nidlen;
123 	uint8_t pfl_nid[8];
124 };
125 
126 union pccard_funce {
127 	struct pccard_funce_disk pfv_disk;
128 	struct pccard_funce_lan pfv_lan;
129 };
130 
131 struct pccard_function {
132 	/* read off the card */
133 	int		number;
134 	int		function;
135 	int		last_config_index;
136 	uint32_t	ccr_base;	/* Offset with card's memory */
137 	uint32_t	ccr_mask;
138 	struct resource *ccr_res;
139 	int		ccr_rid;
140 	STAILQ_HEAD(, pccard_config_entry) cfe_head;
141 	STAILQ_ENTRY(pccard_function) pf_list;
142 	/* run-time state */
143 	struct pccard_softc *sc;
144 	struct pccard_config_entry *cfe;
145 	struct pccard_mem_handle pf_pcmh;
146 	device_t	dev;
147 #define	pf_ccrt		pf_pcmh.memt
148 #define	pf_ccrh		pf_pcmh.memh
149 #define	pf_ccr_realsize	pf_pcmh.realsize
150 	uint32_t	pf_ccr_offset;	/* Offset from ccr_base of CIS */
151 	int		pf_ccr_window;
152 	bus_addr_t	pf_mfc_iobase;
153 	bus_addr_t	pf_mfc_iomax;
154 	int		pf_flags;
155 	driver_intr_t	*intr_handler;
156 	void		*intr_handler_arg;
157 	void		*intr_handler_cookie;
158 
159 	union pccard_funce pf_funce; /* CISTPL_FUNCE */
160 #define pf_funce_disk_interface pf_funce.pfv_disk.pfd_interface
161 #define pf_funce_lan_nid pf_funce.pfv_lan.pfl_nid
162 #define pf_funce_lan_nidlen pf_funce.pfv_lan.pfl_nidlen
163 };
164 
165 /* pf_flags */
166 #define	PFF_ENABLED	0x0001		/* function is enabled */
167 
168 struct pccard_card {
169 	int		cis1_major;
170 	int		cis1_minor;
171 	/* XXX waste of space? */
172 	char		cis1_info_buf[256];
173 	char		*cis1_info[4];
174 	/*
175 	 * Use int32_t for manufacturer and product so that they can
176 	 * hold the id value found in card CIS and special value that
177 	 * indicates no id was found.
178 	 */
179 	int32_t		manufacturer;
180 #define	PCMCIA_VENDOR_INVALID	-1
181 	int32_t		product;
182 #define	PCMCIA_PRODUCT_INVALID		-1
183 	int16_t		prodext;
184 	uint16_t	error;
185 #define	PCMCIA_CIS_INVALID		{ NULL, NULL, NULL, NULL }
186 	STAILQ_HEAD(, pccard_function) pf_head;
187 };
188 
189 #define	PCCARD_WIDTH_AUTO	0
190 #define	PCCARD_WIDTH_IO8	1
191 #define	PCCARD_WIDTH_IO16	2
192 
193 /* More later? */
194 struct pccard_ivar {
195 	struct resource_list resources;
196 	struct pccard_function *pf;
197 };
198 
199 struct pccard_softc {
200 	device_t		dev;
201 	/* this stuff is for the socket */
202 
203 	/* this stuff is for the card */
204 	struct pccard_card card;
205 	int		sc_enabled_count;	/* num functions enabled */
206 };
207 
208 struct pccard_cis_quirk {
209 	int32_t manufacturer;
210 	int32_t product;
211 	char *cis1_info[4];
212 	struct pccard_function *pf;
213 	struct pccard_config_entry *cfe;
214 };
215 
216 struct pccard_tuple {
217 	unsigned int	code;
218 	unsigned int	length;
219 	u_long		mult;
220 	bus_addr_t	ptr;
221 	bus_space_tag_t	memt;
222 	bus_space_handle_t memh;
223 };
224 
225 typedef int (*pccard_scan_t)(const struct pccard_tuple *, void *);
226 
227 struct pccard_product {
228 	const char	*pp_name;		/* NULL if end of table */
229 #define PCCARD_VENDOR_ANY (0xffffffff)
230 	uint32_t	pp_vendor;
231 #define PCCARD_PRODUCT_ANY (0xffffffff)
232 	uint32_t	pp_product;
233 	const char	*pp_cis[4];
234 };
235 
236 typedef int (*pccard_product_match_fn) (device_t dev,
237     const struct pccard_product *ent, int vpfmatch);
238 
239 #include "card_if.h"
240 
241 /*
242  * make this inline so that we don't have to worry about dangling references
243  * to it in the modules or the code.
244  */
245 static __inline const struct pccard_product *
246 pccard_product_lookup(device_t dev, const struct pccard_product *tab,
247     size_t ent_size, pccard_product_match_fn matchfn)
248 {
249 	return CARD_DO_PRODUCT_LOOKUP(device_get_parent(dev), dev,
250 	    tab, ent_size, matchfn);
251 }
252 
253 void	pccard_read_cis(struct pccard_softc *);
254 void	pccard_check_cis_quirks(device_t);
255 void	pccard_print_cis(device_t);
256 int	pccard_scan_cis(device_t, pccard_scan_t, void *);
257 
258 #define	pccard_cis_read_1(tuple, idx0)					\
259 	(bus_space_read_1((tuple)->memt, (tuple)->memh, (tuple)->mult*(idx0)))
260 
261 #define	pccard_tuple_read_1(tuple, idx1)				\
262 	(pccard_cis_read_1((tuple), ((tuple)->ptr+(2+(idx1)))))
263 
264 #define	pccard_tuple_read_2(tuple, idx2)				\
265 	(pccard_tuple_read_1((tuple), (idx2)) |				\
266 	 (pccard_tuple_read_1((tuple), (idx2)+1)<<8))
267 
268 #define	pccard_tuple_read_3(tuple, idx3)				\
269 	(pccard_tuple_read_1((tuple), (idx3)) |				\
270 	 (pccard_tuple_read_1((tuple), (idx3)+1)<<8) |			\
271 	 (pccard_tuple_read_1((tuple), (idx3)+2)<<16))
272 
273 #define	pccard_tuple_read_4(tuple, idx4)				\
274 	(pccard_tuple_read_1((tuple), (idx4)) |				\
275 	 (pccard_tuple_read_1((tuple), (idx4)+1)<<8) |			\
276 	 (pccard_tuple_read_1((tuple), (idx4)+2)<<16) |			\
277 	 (pccard_tuple_read_1((tuple), (idx4)+3)<<24))
278 
279 #define	pccard_tuple_read_n(tuple, n, idxn)				\
280 	(((n)==1)?pccard_tuple_read_1((tuple), (idxn)) :		\
281 	 (((n)==2)?pccard_tuple_read_2((tuple), (idxn)) :		\
282 	  (((n)==3)?pccard_tuple_read_3((tuple), (idxn)) :		\
283 	   /* n == 4 */ pccard_tuple_read_4((tuple), (idxn)))))
284 
285 #define	PCCARD_SPACE_MEMORY	1
286 #define	PCCARD_SPACE_IO		2
287 
288 #define	pccard_mfc(sc)							\
289 		(STAILQ_FIRST(&(sc)->card.pf_head) &&			\
290 		 STAILQ_NEXT(STAILQ_FIRST(&(sc)->card.pf_head),pf_list))
291 
292 #define	pccard_io_alloc(pf, start, size, align, pciop)			\
293 	(pccard_chip_io_alloc((pf)->sc->pct, pf->sc->pch, (start),	\
294 	 (size), (align), (pciop)))
295 
296 #define	pccard_io_free(pf, pciohp)					\
297 	(pccard_chip_io_free((pf)->sc->pct, (pf)->sc->pch, (pciohp)))
298 
299 int	pccard_io_map(struct pccard_function *, int, bus_addr_t,
300 	    bus_size_t, struct pccard_io_handle *, int *);
301 void	pccard_io_unmap(struct pccard_function *, int);
302 
303 #define pccard_mem_alloc(pf, size, pcmhp)				\
304 	(pccard_chip_mem_alloc((pf)->sc->pct, (pf)->sc->pch, (size), (pcmhp)))
305 #define pccard_mem_free(pf, pcmhp)					\
306 	(pccard_chip_mem_free((pf)->sc->pct, (pf)->sc->pch, (pcmhp)))
307 #define pccard_mem_map(pf, kind, card_addr, size, pcmhp, offsetp, windowp) \
308 	(pccard_chip_mem_map((pf)->sc->pct, (pf)->sc->pch, (kind),	\
309 	 (card_addr), (size), (pcmhp), (offsetp), (windowp)))
310 #define	pccard_mem_unmap(pf, window)					\
311 	(pccard_chip_mem_unmap((pf)->sc->pct, (pf)->sc->pch, (window)))
312 
313 /* compat layer */
314 static __inline int
315 pccard_compat_probe(device_t dev)
316 {
317 	return (CARD_COMPAT_DO_PROBE(device_get_parent(dev), dev));
318 }
319 
320 static __inline int
321 pccard_compat_attach(device_t dev)
322 {
323 	return (CARD_COMPAT_DO_ATTACH(device_get_parent(dev), dev));
324 }
325 
326 /* ivar interface */
327 enum {
328 	PCCARD_IVAR_ETHADDR,	/* read ethernet address from CIS tupple */
329 	PCCARD_IVAR_VENDOR,
330 	PCCARD_IVAR_PRODUCT,
331 	PCCARD_IVAR_PRODEXT,
332 	PCCARD_IVAR_FUNCTION_NUMBER,
333 	PCCARD_IVAR_VENDOR_STR,	/* CIS string for "Manufacturer" */
334 	PCCARD_IVAR_PRODUCT_STR,/* CIS strnig for "Product" */
335 	PCCARD_IVAR_CIS3_STR,
336 	PCCARD_IVAR_CIS4_STR,
337 	PCCARD_IVAR_FUNCTION
338 };
339 
340 #define PCCARD_ACCESSOR(A, B, T)					\
341 __inline static T							\
342 pccard_get_ ## A(device_t dev)						\
343 {									\
344 	uintptr_t v;							\
345 	BUS_READ_IVAR(device_get_parent(dev), dev, PCCARD_IVAR_ ## B, &v); \
346 	return (T)v;							\
347 }
348 
349 PCCARD_ACCESSOR(ether,		ETHADDR,		const uint8_t *)
350 PCCARD_ACCESSOR(vendor,		VENDOR,			uint32_t)
351 PCCARD_ACCESSOR(product,	PRODUCT,		uint32_t)
352 PCCARD_ACCESSOR(prodext,	PRODEXT,		uint16_t)
353 PCCARD_ACCESSOR(function_number,FUNCTION_NUMBER,	uint32_t)
354 PCCARD_ACCESSOR(function,	FUNCTION,		uint32_t)
355 PCCARD_ACCESSOR(vendor_str,	VENDOR_STR,		const char *)
356 PCCARD_ACCESSOR(product_str,	PRODUCT_STR,		const char *)
357 PCCARD_ACCESSOR(cis3_str,	CIS3_STR,		const char *)
358 PCCARD_ACCESSOR(cis4_str,	CIS4_STR,		const char *)
359 
360 /* shared memory flags */
361 enum {
362 	PCCARD_A_MEM_COM,       /* common */
363 	PCCARD_A_MEM_ATTR,      /* attribute */
364 	PCCARD_A_MEM_8BIT,      /* 8 bit */
365 	PCCARD_A_MEM_16BIT      /* 16 bit */
366 };
367 
368 #define PCCARD_SOFTC(d) (struct pccard_softc *) device_get_softc(d)
369 #define PCCARD_IVAR(d) (struct pccard_ivar *) device_get_ivars(d)
370 
371 #define PCCARD_S(a, b) PCMCIA_STR_ ## a ## _ ## b
372 #define PCCARD_P(a, b) PCMCIA_PRODUCT_ ## a ## _ ## b
373 #define PCCARD_C(a, b) PCMCIA_CIS_ ## a ## _ ## b
374 #if PCCARD_API_LEVEL >= 6
375 #define PCMCIA_CARD_D(v, p) { PCCARD_S(v, p), PCMCIA_VENDOR_ ## v, \
376 		PCCARD_P(v, p), PCCARD_C(v, p) }
377 #define PCMCIA_CARD2_D(v1, p1, p2) \
378 		{ PCMCIA_STR_ ## p2, PCMCIA_VENDOR_ ## v1, PCCARD_P(v1, p1), \
379 		  PCMCIA_CIS_ ## p2}
380 #define PCMCIA_CARD(v, p) { NULL, PCMCIA_VENDOR_ ## v, \
381 		PCCARD_P(v, p), PCCARD_C(v, p) }
382 #define PCMCIA_CARD2(v1, p1, p2) \
383 		{ NULL, PCMCIA_VENDOR_ ## v1, PCCARD_P(v1, p1), \
384 		  PCMCIA_CIS_ ## p2}
385 #else
386 #define PCMCIA_CARD_D(v, p, f) { PCCARD_S(v, p), PCMCIA_VENDOR_ ## v, \
387 		PCCARD_P(v, p), PCCARD_C(v, p) }
388 #define PCMCIA_CARD2_D(v1, p1, p2, f) \
389 		{ PCMCIA_STR_ ## p2, PCMCIA_VENDOR_ ## v1, PCCARD_P(v1, p1), \
390 		  PCMCIA_CIS_ ## p2}
391 #define PCMCIA_CARD(v, p, f) { NULL, PCMCIA_VENDOR_ ## v, \
392 		PCCARD_P(v, p), PCCARD_C(v, p) }
393 #define PCMCIA_CARD2(v1, p1, p2, f) \
394 		{ NULL, PCMCIA_VENDOR_ ## v1, PCCARD_P(v1, p1), \
395 		  PCMCIA_CIS_ ## p2}
396 #endif
397