xref: /freebsd/sys/dev/pccard/pccardvar.h (revision eacee0ff7ec955b32e09515246bd97b6edcd2b0f)
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 #include "card_if.h"
38 
39 extern int	pccard_verbose;
40 
41 /*
42  * Contains information about mapped/allocated i/o spaces.
43  */
44 struct pccard_io_handle {
45 	bus_space_tag_t iot;		/* bus space tag (from chipset) */
46 	bus_space_handle_t ioh;		/* mapped space handle */
47 	bus_addr_t      addr;		/* resulting address in bus space */
48 	bus_size_t      size;		/* size of i/o space */
49 	int             flags;		/* misc. information */
50 	int		width;
51 };
52 
53 #define	PCCARD_IO_ALLOCATED	0x01	/* i/o space was allocated */
54 
55 /*
56  * Contains information about allocated memory space.
57  */
58 struct pccard_mem_handle {
59 	bus_space_tag_t memt;		/* bus space tag (from chipset) */
60 	bus_space_handle_t memh;	/* mapped space handle */
61 	bus_addr_t      addr;		/* resulting address in bus space */
62 	bus_size_t      size;		/* size of mem space */
63 	bus_size_t      realsize;	/* how much we really allocated */
64 	long		offset;		/* mapped Offset on card */
65 	bus_addr_t	cardaddr;	/* Absolute address on card */
66 	int		kind;
67 };
68 
69 /* pccard itself */
70 
71 #define PCCARD_CFE_MWAIT_REQUIRED	0x0001
72 #define PCCARD_CFE_RDYBSY_ACTIVE	0x0002
73 #define PCCARD_CFE_WP_ACTIVE		0x0004
74 #define PCCARD_CFE_BVD_ACTIVE		0x0008
75 #define PCCARD_CFE_IO8			0x0010
76 #define PCCARD_CFE_IO16			0x0020
77 #define PCCARD_CFE_IRQSHARE		0x0040
78 #define PCCARD_CFE_IRQPULSE		0x0080
79 #define PCCARD_CFE_IRQLEVEL		0x0100
80 #define PCCARD_CFE_POWERDOWN		0x0200
81 #define PCCARD_CFE_READONLY		0x0400
82 #define PCCARD_CFE_AUDIO		0x0800
83 
84 struct pccard_config_entry {
85 	int		number;
86 	u_int32_t	flags;
87 	int		iftype;
88 	int		num_iospace;
89 
90 	/*
91 	 * The card will only decode this mask in any case, so we can
92 	 * do dynamic allocation with this in mind, in case the suggestions
93 	 * below are no good.
94 	 */
95 	u_long		iomask;
96 	struct {
97 		u_long	length;
98 		u_long	start;
99 	} iospace[4];		/* XXX this could be as high as 16 */
100 	u_int16_t	irqmask;
101 	int		num_memspace;
102 	struct {
103 		u_long	length;
104 		u_long	cardaddr;
105 		u_long	hostaddr;
106 	} memspace[2];		/* XXX this could be as high as 8 */
107 	int		maxtwins;
108 	struct resource *iores[4];
109 	int		iorid[4];
110 	struct resource *irqres;
111 	int		irqrid;
112 	struct resource *memres[2];
113 	int		memrid[2];
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 	u_int8_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 	u_long		ccr_base;
137 	u_long		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 	bus_addr_t	pf_ccr_offset;
151 	int		pf_ccr_window;
152 	long		pf_mfc_iobase;
153 	long		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 	u_int16_t	error;
184 #define	PCMCIA_CIS_INVALID		{ NULL, NULL, NULL, NULL }
185 	STAILQ_HEAD(, pccard_function) pf_head;
186 };
187 
188 #define	PCCARD_MEM_ATTR		1
189 #define	PCCARD_MEM_COMMON	2
190 
191 #define	PCCARD_WIDTH_AUTO	0
192 #define	PCCARD_WIDTH_IO8	1
193 #define	PCCARD_WIDTH_IO16	2
194 
195 /* More later? */
196 struct pccard_ivar {
197 	struct resource_list resources;
198 	struct pccard_function *fcn;
199 };
200 
201 struct pccard_softc {
202 	device_t		dev;
203 	/* this stuff is for the socket */
204 
205 	/* this stuff is for the card */
206 	struct pccard_card card;
207 	int		sc_enabled_count;	/* num functions enabled */
208 };
209 
210 void
211 pccardbus_if_setup(struct pccard_softc*);
212 
213 struct pccard_cis_quirk {
214 	int32_t manufacturer;
215 	int32_t product;
216 	char *cis1_info[4];
217 	struct pccard_function *pf;
218 	struct pccard_config_entry *cfe;
219 };
220 
221 struct pccard_tuple {
222 	unsigned int	code;
223 	unsigned int	length;
224 	u_long		mult;
225 	bus_addr_t	ptr;
226 	bus_space_tag_t	memt;
227 	bus_space_handle_t memh;
228 };
229 
230 struct pccard_product {
231 	const char	*pp_name;		/* NULL if end of table */
232 #define PCCARD_VENDOR_ANY ((u_int32_t) -1)
233 	u_int32_t	pp_vendor;
234 #define PCCARD_PRODUCT_ANY ((u_int32_t) -1)
235 	u_int32_t	pp_product;
236 	int		pp_expfunc;
237 	const char	*pp_cis[4];
238 };
239 
240 typedef int (*pccard_product_match_fn) (device_t dev,
241     const struct pccard_product *ent, int vpfmatch);
242 
243 const struct pccard_product
244 	*pccard_product_lookup(device_t dev, const struct pccard_product *tab,
245 	    size_t ent_size, pccard_product_match_fn matchfn);
246 
247 void	pccard_read_cis(struct pccard_softc *);
248 void	pccard_check_cis_quirks(device_t);
249 void	pccard_print_cis(device_t);
250 int	pccard_scan_cis(device_t,
251 		int (*) (struct pccard_tuple *, void *), void *);
252 
253 #define	pccard_cis_read_1(tuple, idx0)					\
254 	(bus_space_read_1((tuple)->memt, (tuple)->memh, (tuple)->mult*(idx0)))
255 
256 #define	pccard_tuple_read_1(tuple, idx1)				\
257 	(pccard_cis_read_1((tuple), ((tuple)->ptr+(2+(idx1)))))
258 
259 #define	pccard_tuple_read_2(tuple, idx2)				\
260 	(pccard_tuple_read_1((tuple), (idx2)) |				\
261 	 (pccard_tuple_read_1((tuple), (idx2)+1)<<8))
262 
263 #define	pccard_tuple_read_3(tuple, idx3)				\
264 	(pccard_tuple_read_1((tuple), (idx3)) |				\
265 	 (pccard_tuple_read_1((tuple), (idx3)+1)<<8) |			\
266 	 (pccard_tuple_read_1((tuple), (idx3)+2)<<16))
267 
268 #define	pccard_tuple_read_4(tuple, idx4)				\
269 	(pccard_tuple_read_1((tuple), (idx4)) |				\
270 	 (pccard_tuple_read_1((tuple), (idx4)+1)<<8) |			\
271 	 (pccard_tuple_read_1((tuple), (idx4)+2)<<16) |			\
272 	 (pccard_tuple_read_1((tuple), (idx4)+3)<<24))
273 
274 #define	pccard_tuple_read_n(tuple, n, idxn)				\
275 	(((n)==1)?pccard_tuple_read_1((tuple), (idxn)) :		\
276 	 (((n)==2)?pccard_tuple_read_2((tuple), (idxn)) :		\
277 	  (((n)==3)?pccard_tuple_read_3((tuple), (idxn)) :		\
278 	   /* n == 4 */ pccard_tuple_read_4((tuple), (idxn)))))
279 
280 #define	PCCARD_SPACE_MEMORY	1
281 #define	PCCARD_SPACE_IO		2
282 
283 #define	pccard_mfc(sc)	(STAILQ_FIRST(&(sc)->card.pf_head) &&		\
284 		 STAILQ_NEXT(STAILQ_FIRST(&(sc)->card.pf_head),pf_list))
285 
286 #define	pccard_io_alloc(pf, start, size, align, pciop)			\
287 	(pccard_chip_io_alloc((pf)->sc->pct, pf->sc->pch, (start),	\
288 	 (size), (align), (pciop)))
289 
290 #define	pccard_io_free(pf, pciohp)					\
291 	(pccard_chip_io_free((pf)->sc->pct, (pf)->sc->pch, (pciohp)))
292 
293 int	pccard_io_map(struct pccard_function *, int, bus_addr_t,
294 	    bus_size_t, struct pccard_io_handle *, int *);
295 void	pccard_io_unmap(struct pccard_function *, int);
296 
297 #define pccard_mem_alloc(pf, size, pcmhp)				\
298 	(pccard_chip_mem_alloc((pf)->sc->pct, (pf)->sc->pch, (size), (pcmhp)))
299 #define pccard_mem_free(pf, pcmhp)					\
300 	(pccard_chip_mem_free((pf)->sc->pct, (pf)->sc->pch, (pcmhp)))
301 #define pccard_mem_map(pf, kind, card_addr, size, pcmhp, offsetp, windowp) \
302 	(pccard_chip_mem_map((pf)->sc->pct, (pf)->sc->pch, (kind),	\
303 	 (card_addr), (size), (pcmhp), (offsetp), (windowp)))
304 #define	pccard_mem_unmap(pf, window)					\
305 	(pccard_chip_mem_unmap((pf)->sc->pct, (pf)->sc->pch, (window)))
306 
307 /* compat layer */
308 static __inline int
309 pccard_compat_probe(device_t dev)
310 {
311 	return (CARD_COMPAT_DO_PROBE(device_get_parent(dev), dev));
312 }
313 
314 static __inline int
315 pccard_compat_attach(device_t dev)
316 {
317 	return (CARD_COMPAT_DO_ATTACH(device_get_parent(dev), dev));
318 }
319 
320 /* ivar interface */
321 enum {
322 	PCCARD_IVAR_ETHADDR,	/* read ethernet address from CIS tupple */
323 	PCCARD_IVAR_VENDOR,
324 	PCCARD_IVAR_PRODUCT,
325 	PCCARD_IVAR_FUNCTION_NUMBER,
326 	PCCARD_IVAR_VENDOR_STR,	/* CIS string for "Manufacturer" */
327 	PCCARD_IVAR_PRODUCT_STR,/* CIS strnig for "Product" */
328 	PCCARD_IVAR_CIS3_STR,
329 	PCCARD_IVAR_CIS4_STR,
330 	PCCARD_IVAR_FUNCTION
331 };
332 
333 #define PCCARD_ACCESSOR(A, B, T)					\
334 __inline static int							\
335 pccard_get_ ## A(device_t dev, T *t)					\
336 {									\
337 	return BUS_READ_IVAR(device_get_parent(dev), dev,		\
338 	    PCCARD_IVAR_ ## B, (uintptr_t *) t);			\
339 }
340 
341 PCCARD_ACCESSOR(ether,		ETHADDR,		u_int8_t)
342 PCCARD_ACCESSOR(vendor,		VENDOR,			u_int32_t)
343 PCCARD_ACCESSOR(product,	PRODUCT,		u_int32_t)
344 PCCARD_ACCESSOR(function_number,FUNCTION_NUMBER,	u_int32_t)
345 PCCARD_ACCESSOR(function,	FUNCTION,		u_int32_t)
346 PCCARD_ACCESSOR(vendor_str,	VENDOR_STR,		char *)
347 PCCARD_ACCESSOR(product_str,	PRODUCT_STR,		char *)
348 PCCARD_ACCESSOR(cis3_str,	CIS3_STR,		char *)
349 
350 /* shared memory flags */
351 enum {
352 	PCCARD_A_MEM_COM,       /* common */
353 	PCCARD_A_MEM_ATTR,      /* attribute */
354 	PCCARD_A_MEM_8BIT,      /* 8 bit */
355 	PCCARD_A_MEM_16BIT      /* 16 bit */
356 };
357 
358 #define PCCARD_SOFTC(d) (struct pccard_softc *) device_get_softc(d)
359 #define PCCARD_IVAR(d) (struct pccard_ivar *) device_get_ivars(d)
360 
361 #define PCCARD_S(a, b) PCMCIA_STR_ ## a ## _ ## b
362 #define PCCARD_P(a, b) PCMCIA_PRODUCT_ ## a ## _ ## b
363 #define PCCARD_C(a, b) PCMCIA_CIS_ ## a ## _ ## b
364 #define PCMCIA_CARD(v, p, f) { PCCARD_S(v, p), PCMCIA_VENDOR_ ## v, \
365 		PCCARD_P(v, p), f, PCCARD_C(v, p) }
366 #define PCMCIA_CARD2(v1, p1, p2, f) \
367 		{ PCMCIA_STR_ ## p2, PCMCIA_VENDOR_ ## v1, PCCARD_P(v1, p1), \
368 		  f, PCMCIA_CIS_ ## p2}
369 
370