xref: /freebsd/sys/dev/pccard/pccardvar.h (revision 2357939bc239bd5334a169b62313806178dd8f30)
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 	bus_addr_t	cardaddr;	/* Absolute address on card */
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 	uint32_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 	uint16_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_funce_disk {
116 	int pfd_interface;
117 };
118 
119 struct pccard_funce_lan {
120 	int pfl_nidlen;
121 	uint8_t pfl_nid[8];
122 };
123 
124 union pccard_funce {
125 	struct pccard_funce_disk pfv_disk;
126 	struct pccard_funce_lan pfv_lan;
127 };
128 
129 struct pccard_function {
130 	/* read off the card */
131 	int		number;
132 	int		function;
133 	int		last_config_index;
134 	uint32_t	ccr_base;	/* Offset with card's memory */
135 	uint32_t	ccr_mask;
136 	struct resource *ccr_res;
137 	int		ccr_rid;
138 	STAILQ_HEAD(, pccard_config_entry) cfe_head;
139 	STAILQ_ENTRY(pccard_function) pf_list;
140 	/* run-time state */
141 	struct pccard_softc *sc;
142 	struct pccard_config_entry *cfe;
143 	struct pccard_mem_handle pf_pcmh;
144 	device_t	dev;
145 #define	pf_ccrt		pf_pcmh.memt
146 #define	pf_ccrh		pf_pcmh.memh
147 #define	pf_ccr_realsize	pf_pcmh.realsize
148 	uint32_t	pf_ccr_offset;	/* Offset from ccr_base of CIS */
149 	int		pf_ccr_window;
150 	bus_addr_t	pf_mfc_iobase;
151 	bus_addr_t	pf_mfc_iomax;
152 	int		pf_flags;
153 	driver_intr_t	*intr_handler;
154 	void		*intr_handler_arg;
155 	void		*intr_handler_cookie;
156 
157 	union pccard_funce pf_funce; /* CISTPL_FUNCE */
158 #define pf_funce_disk_interface pf_funce.pfv_disk.pfd_interface
159 #define pf_funce_lan_nid pf_funce.pfv_lan.pfl_nid
160 #define pf_funce_lan_nidlen pf_funce.pfv_lan.pfl_nidlen
161 };
162 
163 /* pf_flags */
164 #define	PFF_ENABLED	0x0001		/* function is enabled */
165 
166 struct pccard_card {
167 	int		cis1_major;
168 	int		cis1_minor;
169 	/* XXX waste of space? */
170 	char		cis1_info_buf[256];
171 	char		*cis1_info[4];
172 	/*
173 	 * Use int32_t for manufacturer and product so that they can
174 	 * hold the id value found in card CIS and special value that
175 	 * indicates no id was found.
176 	 */
177 	int32_t		manufacturer;
178 #define	PCMCIA_VENDOR_INVALID	-1
179 	int32_t		product;
180 #define	PCMCIA_PRODUCT_INVALID		-1
181 	int16_t		prodext;
182 	uint16_t	error;
183 #define	PCMCIA_CIS_INVALID		{ NULL, NULL, NULL, NULL }
184 	STAILQ_HEAD(, pccard_function) pf_head;
185 };
186 
187 #define	PCCARD_WIDTH_AUTO	0
188 #define	PCCARD_WIDTH_IO8	1
189 #define	PCCARD_WIDTH_IO16	2
190 
191 /* More later? */
192 struct pccard_ivar {
193 	struct resource_list resources;
194 	struct pccard_function *fcn;
195 };
196 
197 struct pccard_softc {
198 	device_t		dev;
199 	/* this stuff is for the socket */
200 
201 	/* this stuff is for the card */
202 	struct pccard_card card;
203 	int		sc_enabled_count;	/* num functions enabled */
204 };
205 
206 struct pccard_cis_quirk {
207 	int32_t manufacturer;
208 	int32_t product;
209 	char *cis1_info[4];
210 	struct pccard_function *pf;
211 	struct pccard_config_entry *cfe;
212 };
213 
214 struct pccard_tuple {
215 	unsigned int	code;
216 	unsigned int	length;
217 	u_long		mult;
218 	bus_addr_t	ptr;
219 	bus_space_tag_t	memt;
220 	bus_space_handle_t memh;
221 };
222 
223 struct pccard_product {
224 	const char	*pp_name;		/* NULL if end of table */
225 #define PCCARD_VENDOR_ANY (0xffffffff)
226 	uint32_t	pp_vendor;
227 #define PCCARD_PRODUCT_ANY (0xffffffff)
228 	uint32_t	pp_product;
229 	int		pp_expfunc;
230 	const char	*pp_cis[4];
231 };
232 
233 typedef int (*pccard_product_match_fn) (device_t dev,
234     const struct pccard_product *ent, int vpfmatch);
235 
236 #include "card_if.h"
237 
238 /*
239  * make this inline so that we don't have to worry about dangling references
240  * to it in the modules or the code.
241  */
242 static __inline const struct pccard_product *
243 pccard_product_lookup(device_t dev, const struct pccard_product *tab,
244     size_t ent_size, pccard_product_match_fn matchfn)
245 {
246 	return CARD_DO_PRODUCT_LOOKUP(device_get_parent(dev), dev,
247 	    tab, ent_size, matchfn);
248 }
249 
250 void	pccard_read_cis(struct pccard_softc *);
251 void	pccard_check_cis_quirks(device_t);
252 void	pccard_print_cis(device_t);
253 int	pccard_scan_cis(device_t,
254 		int (*) (struct pccard_tuple *, void *), void *);
255 
256 #define	pccard_cis_read_1(tuple, idx0)					\
257 	(bus_space_read_1((tuple)->memt, (tuple)->memh, (tuple)->mult*(idx0)))
258 
259 #define	pccard_tuple_read_1(tuple, idx1)				\
260 	(pccard_cis_read_1((tuple), ((tuple)->ptr+(2+(idx1)))))
261 
262 #define	pccard_tuple_read_2(tuple, idx2)				\
263 	(pccard_tuple_read_1((tuple), (idx2)) |				\
264 	 (pccard_tuple_read_1((tuple), (idx2)+1)<<8))
265 
266 #define	pccard_tuple_read_3(tuple, idx3)				\
267 	(pccard_tuple_read_1((tuple), (idx3)) |				\
268 	 (pccard_tuple_read_1((tuple), (idx3)+1)<<8) |			\
269 	 (pccard_tuple_read_1((tuple), (idx3)+2)<<16))
270 
271 #define	pccard_tuple_read_4(tuple, idx4)				\
272 	(pccard_tuple_read_1((tuple), (idx4)) |				\
273 	 (pccard_tuple_read_1((tuple), (idx4)+1)<<8) |			\
274 	 (pccard_tuple_read_1((tuple), (idx4)+2)<<16) |			\
275 	 (pccard_tuple_read_1((tuple), (idx4)+3)<<24))
276 
277 #define	pccard_tuple_read_n(tuple, n, idxn)				\
278 	(((n)==1)?pccard_tuple_read_1((tuple), (idxn)) :		\
279 	 (((n)==2)?pccard_tuple_read_2((tuple), (idxn)) :		\
280 	  (((n)==3)?pccard_tuple_read_3((tuple), (idxn)) :		\
281 	   /* n == 4 */ pccard_tuple_read_4((tuple), (idxn)))))
282 
283 #define	PCCARD_SPACE_MEMORY	1
284 #define	PCCARD_SPACE_IO		2
285 
286 #define	pccard_mfc(sc)							\
287 		(STAILQ_FIRST(&(sc)->card.pf_head) &&			\
288 		 STAILQ_NEXT(STAILQ_FIRST(&(sc)->card.pf_head),pf_list))
289 
290 #define	pccard_io_alloc(pf, start, size, align, pciop)			\
291 	(pccard_chip_io_alloc((pf)->sc->pct, pf->sc->pch, (start),	\
292 	 (size), (align), (pciop)))
293 
294 #define	pccard_io_free(pf, pciohp)					\
295 	(pccard_chip_io_free((pf)->sc->pct, (pf)->sc->pch, (pciohp)))
296 
297 int	pccard_io_map(struct pccard_function *, int, bus_addr_t,
298 	    bus_size_t, struct pccard_io_handle *, int *);
299 void	pccard_io_unmap(struct pccard_function *, int);
300 
301 #define pccard_mem_alloc(pf, size, pcmhp)				\
302 	(pccard_chip_mem_alloc((pf)->sc->pct, (pf)->sc->pch, (size), (pcmhp)))
303 #define pccard_mem_free(pf, pcmhp)					\
304 	(pccard_chip_mem_free((pf)->sc->pct, (pf)->sc->pch, (pcmhp)))
305 #define pccard_mem_map(pf, kind, card_addr, size, pcmhp, offsetp, windowp) \
306 	(pccard_chip_mem_map((pf)->sc->pct, (pf)->sc->pch, (kind),	\
307 	 (card_addr), (size), (pcmhp), (offsetp), (windowp)))
308 #define	pccard_mem_unmap(pf, window)					\
309 	(pccard_chip_mem_unmap((pf)->sc->pct, (pf)->sc->pch, (window)))
310 
311 /* compat layer */
312 static __inline int
313 pccard_compat_probe(device_t dev)
314 {
315 	return (CARD_COMPAT_DO_PROBE(device_get_parent(dev), dev));
316 }
317 
318 static __inline int
319 pccard_compat_attach(device_t dev)
320 {
321 	return (CARD_COMPAT_DO_ATTACH(device_get_parent(dev), dev));
322 }
323 
324 /* ivar interface */
325 enum {
326 	PCCARD_IVAR_ETHADDR,	/* read ethernet address from CIS tupple */
327 	PCCARD_IVAR_VENDOR,
328 	PCCARD_IVAR_PRODUCT,
329 	PCCARD_IVAR_PRODEXT,
330 	PCCARD_IVAR_FUNCTION_NUMBER,
331 	PCCARD_IVAR_VENDOR_STR,	/* CIS string for "Manufacturer" */
332 	PCCARD_IVAR_PRODUCT_STR,/* CIS strnig for "Product" */
333 	PCCARD_IVAR_CIS3_STR,
334 	PCCARD_IVAR_CIS4_STR,
335 	PCCARD_IVAR_FUNCTION
336 };
337 
338 #define PCCARD_ACCESSOR(A, B, T)					\
339 __inline static int							\
340 pccard_get_ ## A(device_t dev, T *t)					\
341 {									\
342 	return BUS_READ_IVAR(device_get_parent(dev), dev,		\
343 	    PCCARD_IVAR_ ## B, (uintptr_t *) t);			\
344 }
345 
346 PCCARD_ACCESSOR(ether,		ETHADDR,		uint8_t)
347 PCCARD_ACCESSOR(vendor,		VENDOR,			uint32_t)
348 PCCARD_ACCESSOR(product,	PRODUCT,		uint32_t)
349 PCCARD_ACCESSOR(prodext,	PRODEXT,		uint16_t)
350 PCCARD_ACCESSOR(function_number,FUNCTION_NUMBER,	uint32_t)
351 PCCARD_ACCESSOR(function,	FUNCTION,		uint32_t)
352 PCCARD_ACCESSOR(vendor_str,	VENDOR_STR,		const char *)
353 PCCARD_ACCESSOR(product_str,	PRODUCT_STR,		const char *)
354 PCCARD_ACCESSOR(cis3_str,	CIS3_STR,		const char *)
355 PCCARD_ACCESSOR(cis4_str,	CIS4_STR,		const char *)
356 
357 /* shared memory flags */
358 enum {
359 	PCCARD_A_MEM_COM,       /* common */
360 	PCCARD_A_MEM_ATTR,      /* attribute */
361 	PCCARD_A_MEM_8BIT,      /* 8 bit */
362 	PCCARD_A_MEM_16BIT      /* 16 bit */
363 };
364 
365 #define PCCARD_SOFTC(d) (struct pccard_softc *) device_get_softc(d)
366 #define PCCARD_IVAR(d) (struct pccard_ivar *) device_get_ivars(d)
367 
368 #define PCCARD_S(a, b) PCMCIA_STR_ ## a ## _ ## b
369 #define PCCARD_P(a, b) PCMCIA_PRODUCT_ ## a ## _ ## b
370 #define PCCARD_C(a, b) PCMCIA_CIS_ ## a ## _ ## b
371 #define PCMCIA_CARD_D(v, p, f) { PCCARD_S(v, p), PCMCIA_VENDOR_ ## v, \
372 		PCCARD_P(v, p), f, PCCARD_C(v, p) }
373 #define PCMCIA_CARD2_D(v1, p1, p2, f) \
374 		{ PCMCIA_STR_ ## p2, PCMCIA_VENDOR_ ## v1, PCCARD_P(v1, p1), \
375 		  f, PCMCIA_CIS_ ## p2}
376 #define PCMCIA_CARD(v, p, f) { NULL, PCMCIA_VENDOR_ ## v, \
377 		PCCARD_P(v, p), f, PCCARD_C(v, p) }
378 #define PCMCIA_CARD2(v1, p1, p2, f) \
379 		{ NULL, PCMCIA_VENDOR_ ## v1, PCCARD_P(v1, p1), \
380 		  f, PCMCIA_CIS_ ## p2}
381