xref: /freebsd/sys/dev/pccard/pccardvar.h (revision 1669d8afc64812c8d2d1d147ae1fd42ff441e1b1)
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 /*
34  * PCCARD_API_LEVEL.  When set to 5, we provide a 5.x compatable API
35  * for driver writers that have to share their code between 5.x and 6.x.
36  * The 5.x compatibility interfaces will be unsupported in 7.0, at which
37  * point we'll only support 6 and newer, etc.
38  */
39 #ifndef PCCARD_API_LEVEL
40 #define PCCARD_API_LEVEL 6
41 #elif PCCARD_API_LEVEL < 5
42 #error "pccard API less than 5 unsupported"
43 #endif
44 
45 /*
46  * Contains information about mapped/allocated i/o spaces.
47  */
48 struct pccard_io_handle {
49 	bus_space_tag_t iot;		/* bus space tag (from chipset) */
50 	bus_space_handle_t ioh;		/* mapped space handle */
51 	bus_addr_t      addr;		/* resulting address in bus space */
52 	bus_size_t      size;		/* size of i/o space */
53 	int             flags;		/* misc. information */
54 	int		width;
55 };
56 
57 #define	PCCARD_IO_ALLOCATED	0x01	/* i/o space was allocated */
58 
59 /*
60  * Contains information about allocated memory space.
61  */
62 struct pccard_mem_handle {
63 	bus_space_tag_t memt;		/* bus space tag (from chipset) */
64 	bus_space_handle_t memh;	/* mapped space handle */
65 	bus_addr_t      addr;		/* resulting address in bus space */
66 	bus_size_t      size;		/* size of mem space */
67 	bus_size_t      realsize;	/* how much we really allocated */
68 	bus_addr_t	cardaddr;	/* Absolute address on card */
69 	int		kind;
70 };
71 
72 #define	PCCARD_WIDTH_AUTO	0
73 #define	PCCARD_WIDTH_IO8	1
74 #define	PCCARD_WIDTH_IO16	2
75 
76 struct pccard_tuple {
77 	unsigned int	code;
78 	unsigned int	length;
79 	u_long		mult;		/* dist btn successive bytes */
80 	bus_addr_t	ptr;
81 	bus_space_tag_t	memt;
82 	bus_space_handle_t memh;
83 };
84 
85 typedef int (*pccard_scan_t)(const struct pccard_tuple *, void *);
86 
87 struct pccard_product {
88 	const char	*pp_name;
89 #define PCCARD_VENDOR_ANY (0xffffffff)
90 	uint32_t	pp_vendor;		/* 0 == end of table */
91 #define PCCARD_PRODUCT_ANY (0xffffffff)
92 	uint32_t	pp_product;
93 	const char	*pp_cis[4];
94 };
95 
96 typedef int (*pccard_product_match_fn) (device_t dev,
97     const struct pccard_product *ent, int vpfmatch);
98 
99 #include "card_if.h"
100 
101 /*
102  * make this inline so that we don't have to worry about dangling references
103  * to it in the modules or the code.
104  */
105 static __inline const struct pccard_product *
106 pccard_product_lookup(device_t dev, const struct pccard_product *tab,
107     size_t ent_size, pccard_product_match_fn matchfn)
108 {
109 	return CARD_DO_PRODUCT_LOOKUP(device_get_parent(dev), dev,
110 	    tab, ent_size, matchfn);
111 }
112 
113 #define	pccard_cis_read_1(tuple, idx0)					\
114 	(bus_space_read_1((tuple)->memt, (tuple)->memh, (tuple)->mult*(idx0)))
115 
116 #define	pccard_tuple_read_1(tuple, idx1)				\
117 	(pccard_cis_read_1((tuple), ((tuple)->ptr+(2+(idx1)))))
118 
119 #define	pccard_tuple_read_2(tuple, idx2)				\
120 	(pccard_tuple_read_1((tuple), (idx2)) |				\
121 	 (pccard_tuple_read_1((tuple), (idx2)+1)<<8))
122 
123 #define	pccard_tuple_read_3(tuple, idx3)				\
124 	(pccard_tuple_read_1((tuple), (idx3)) |				\
125 	 (pccard_tuple_read_1((tuple), (idx3)+1)<<8) |			\
126 	 (pccard_tuple_read_1((tuple), (idx3)+2)<<16))
127 
128 #define	pccard_tuple_read_4(tuple, idx4)				\
129 	(pccard_tuple_read_1((tuple), (idx4)) |				\
130 	 (pccard_tuple_read_1((tuple), (idx4)+1)<<8) |			\
131 	 (pccard_tuple_read_1((tuple), (idx4)+2)<<16) |			\
132 	 (pccard_tuple_read_1((tuple), (idx4)+3)<<24))
133 
134 #define	pccard_tuple_read_n(tuple, n, idxn)				\
135 	(((n)==1)?pccard_tuple_read_1((tuple), (idxn)) :		\
136 	 (((n)==2)?pccard_tuple_read_2((tuple), (idxn)) :		\
137 	  (((n)==3)?pccard_tuple_read_3((tuple), (idxn)) :		\
138 	   /* n == 4 */ pccard_tuple_read_4((tuple), (idxn)))))
139 
140 #define	PCCARD_SPACE_MEMORY	1
141 #define	PCCARD_SPACE_IO		2
142 
143 #define	pccard_mfc(sc)							\
144 		(STAILQ_FIRST(&(sc)->card.pf_head) &&			\
145 		 STAILQ_NEXT(STAILQ_FIRST(&(sc)->card.pf_head),pf_list))
146 
147 /* Convenience functions */
148 
149 static __inline int
150 pccard_cis_scan(device_t dev, pccard_scan_t fct, void *arg)
151 {
152 	return (CARD_CIS_SCAN(device_get_parent(dev), dev, fct, arg));
153 }
154 
155 static __inline int
156 pccard_attr_read_1(device_t dev, uint32_t offset, uint8_t *val)
157 {
158 	return (CARD_ATTR_READ(device_get_parent(dev), dev, offset, val));
159 }
160 
161 static __inline int
162 pccard_attr_write_1(device_t dev, uint32_t offset, uint8_t val)
163 {
164 	return (CARD_ATTR_WRITE(device_get_parent(dev), dev, offset, val));
165 }
166 
167 static __inline int
168 pccard_ccr_read_1(device_t dev, uint32_t offset, uint8_t *val)
169 {
170 	return (CARD_CCR_READ(device_get_parent(dev), dev, offset, val));
171 }
172 
173 static __inline int
174 pccard_ccr_write_1(device_t dev, uint32_t offset, uint8_t val)
175 {
176 	return (CARD_CCR_WRITE(device_get_parent(dev), dev, offset, val));
177 }
178 
179 /* ivar interface */
180 enum {
181 	PCCARD_IVAR_ETHADDR,	/* read ethernet address from CIS tupple */
182 	PCCARD_IVAR_VENDOR,
183 	PCCARD_IVAR_PRODUCT,
184 	PCCARD_IVAR_PRODEXT,
185 	PCCARD_IVAR_FUNCTION_NUMBER,
186 	PCCARD_IVAR_VENDOR_STR,	/* CIS string for "Manufacturer" */
187 	PCCARD_IVAR_PRODUCT_STR,/* CIS strnig for "Product" */
188 	PCCARD_IVAR_CIS3_STR,
189 	PCCARD_IVAR_CIS4_STR,
190 	PCCARD_IVAR_FUNCTION
191 };
192 
193 #define PCCARD_ACCESSOR(A, B, T)					\
194 __inline static int							\
195 pccard_get_ ## A(device_t dev, T *t)					\
196 {									\
197 	return BUS_READ_IVAR(device_get_parent(dev), dev,		\
198 	    PCCARD_IVAR_ ## B, (uintptr_t *) t);			\
199 }
200 
201 PCCARD_ACCESSOR(ether,		ETHADDR,		uint8_t)
202 PCCARD_ACCESSOR(vendor,		VENDOR,			uint32_t)
203 PCCARD_ACCESSOR(product,	PRODUCT,		uint32_t)
204 PCCARD_ACCESSOR(prodext,	PRODEXT,		uint16_t)
205 PCCARD_ACCESSOR(function_number,FUNCTION_NUMBER,	uint32_t)
206 PCCARD_ACCESSOR(function,	FUNCTION,		uint32_t)
207 PCCARD_ACCESSOR(vendor_str,	VENDOR_STR,		const char *)
208 PCCARD_ACCESSOR(product_str,	PRODUCT_STR,		const char *)
209 PCCARD_ACCESSOR(cis3_str,	CIS3_STR,		const char *)
210 PCCARD_ACCESSOR(cis4_str,	CIS4_STR,		const char *)
211 
212 /* shared memory flags */
213 enum {
214 	PCCARD_A_MEM_COM,       /* common */
215 	PCCARD_A_MEM_ATTR,      /* attribute */
216 	PCCARD_A_MEM_8BIT,      /* 8 bit */
217 	PCCARD_A_MEM_16BIT      /* 16 bit */
218 };
219 
220 #define PCCARD_S(a, b) PCMCIA_STR_ ## a ## _ ## b
221 #define PCCARD_P(a, b) PCMCIA_PRODUCT_ ## a ## _ ## b
222 #define PCCARD_C(a, b) PCMCIA_CIS_ ## a ## _ ## b
223 #if PCCARD_API_LEVEL >= 6
224 #define PCMCIA_CARD_D(v, p) { PCCARD_S(v, p), PCMCIA_VENDOR_ ## v, \
225 		PCCARD_P(v, p), PCCARD_C(v, p) }
226 #define PCMCIA_CARD2_D(v1, p1, p2) \
227 		{ PCMCIA_STR_ ## p2, PCMCIA_VENDOR_ ## v1, PCCARD_P(v1, p1), \
228 		  PCMCIA_CIS_ ## p2}
229 #define PCMCIA_CARD(v, p) { NULL, PCMCIA_VENDOR_ ## v, \
230 		PCCARD_P(v, p), PCCARD_C(v, p) }
231 #define PCMCIA_CARD2(v1, p1, p2) \
232 		{ NULL, PCMCIA_VENDOR_ ## v1, PCCARD_P(v1, p1), \
233 		  PCMCIA_CIS_ ## p2}
234 #else
235 #define PCMCIA_CARD_D(v, p, f) { PCCARD_S(v, p), PCMCIA_VENDOR_ ## v, \
236 		PCCARD_P(v, p), PCCARD_C(v, p) }
237 #define PCMCIA_CARD2_D(v1, p1, p2, f) \
238 		{ PCMCIA_STR_ ## p2, PCMCIA_VENDOR_ ## v1, PCCARD_P(v1, p1), \
239 		  PCMCIA_CIS_ ## p2}
240 #define PCMCIA_CARD(v, p, f) { NULL, PCMCIA_VENDOR_ ## v, \
241 		PCCARD_P(v, p), PCCARD_C(v, p) }
242 #define PCMCIA_CARD2(v1, p1, p2, f) \
243 		{ NULL, PCMCIA_VENDOR_ ## v1, PCCARD_P(v1, p1), \
244 		  PCMCIA_CIS_ ## p2}
245 #endif
246