pccardvar.h revision 75761
1/*	$NetBSD: pcmciavar.h,v 1.12 2000/02/08 12:51:31 enami Exp $	*/
2/* $FreeBSD: head/sys/dev/pccard/pccardvar.h 75761 2001-04-21 04:08:39Z imp $ */
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
39extern int	pccard_verbose;
40
41/*
42 * Contains information about mapped/allocated i/o spaces.
43 */
44struct 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 */
58struct 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;
65	int		kind;
66};
67
68/* pccard itself */
69
70#define PCCARD_CFE_MWAIT_REQUIRED	0x0001
71#define PCCARD_CFE_RDYBSY_ACTIVE	0x0002
72#define PCCARD_CFE_WP_ACTIVE		0x0004
73#define PCCARD_CFE_BVD_ACTIVE		0x0008
74#define PCCARD_CFE_IO8			0x0010
75#define PCCARD_CFE_IO16			0x0020
76#define PCCARD_CFE_IRQSHARE		0x0040
77#define PCCARD_CFE_IRQPULSE		0x0080
78#define PCCARD_CFE_IRQLEVEL		0x0100
79#define PCCARD_CFE_POWERDOWN		0x0200
80#define PCCARD_CFE_READONLY		0x0400
81#define PCCARD_CFE_AUDIO		0x0800
82
83struct pccard_config_entry {
84	int		number;
85	u_int32_t	flags;
86	int		iftype;
87	int		num_iospace;
88
89	/*
90	 * The card will only decode this mask in any case, so we can
91	 * do dynamic allocation with this in mind, in case the suggestions
92	 * below are no good.
93	 */
94	u_long		iomask;
95	struct {
96		u_long	length;
97		u_long	start;
98	} iospace[4];		/* XXX this could be as high as 16 */
99	u_int16_t	irqmask;
100	int		num_memspace;
101	struct {
102		u_long	length;
103		u_long	cardaddr;
104		u_long	hostaddr;
105	} memspace[2];		/* XXX this could be as high as 8 */
106	int		maxtwins;
107	struct resource *iores[4];
108	int		iorid[4];
109	struct resource *irqres;
110	int		irqrid;
111	struct resource *memres[2];
112	int		memrid[2];
113	STAILQ_ENTRY(pccard_config_entry) cfe_list;
114};
115
116struct pccard_function {
117	/* read off the card */
118	int		number;
119	int		function;
120	int		last_config_index;
121	u_long		ccr_base;
122	u_long		ccr_mask;
123	struct resource *ccr_res;
124	int		ccr_rid;
125	STAILQ_HEAD(, pccard_config_entry) cfe_head;
126	STAILQ_ENTRY(pccard_function) pf_list;
127	/* run-time state */
128	struct pccard_softc *sc;
129	struct pccard_config_entry *cfe;
130	struct pccard_mem_handle pf_pcmh;
131	device_t	dev;
132#define	pf_ccrt		pf_pcmh.memt
133#define	pf_ccrh		pf_pcmh.memh
134#define	pf_ccr_realsize	pf_pcmh.realsize
135	bus_addr_t	pf_ccr_offset;
136	int		pf_ccr_window;
137	long		pf_mfc_iobase;
138	long		pf_mfc_iomax;
139	int		pf_flags;
140	driver_intr_t	*intr_handler;
141	void		*intr_handler_arg;
142	void		*intr_handler_cookie;
143};
144
145/* pf_flags */
146#define	PFF_ENABLED	0x0001		/* function is enabled */
147
148struct pccard_card {
149	int		cis1_major;
150	int		cis1_minor;
151	/* XXX waste of space? */
152	char		cis1_info_buf[256];
153	char		*cis1_info[4];
154	/*
155	 * Use int32_t for manufacturer and product so that they can
156	 * hold the id value found in card CIS and special value that
157	 * indicates no id was found.
158	 */
159	int32_t		manufacturer;
160#define	PCCARD_VENDOR_INVALID	-1
161	int32_t		product;
162#define	PCCARD_PRODUCT_INVALID		-1
163	u_int16_t	error;
164#define	PCCARD_CIS_INVALID		{ NULL, NULL, NULL, NULL }
165	STAILQ_HEAD(, pccard_function) pf_head;
166};
167
168#define	PCCARD_MEM_ATTR		1
169#define	PCCARD_MEM_COMMON	2
170
171#define	PCCARD_WIDTH_AUTO	0
172#define	PCCARD_WIDTH_IO8	1
173#define	PCCARD_WIDTH_IO16	2
174
175/* More later? */
176struct pccard_ivar {
177	struct resource_list resources;
178	struct pccard_function *fcn;
179};
180
181struct pccard_softc {
182	device_t		dev;
183	/* this stuff is for the socket */
184
185	/* this stuff is for the card */
186	struct pccard_card card;
187	int		sc_enabled_count;	/* num functions enabled */
188	int		intr_handler_count;
189};
190
191void
192pccardbus_if_setup(struct pccard_softc*);
193
194struct pccard_cis_quirk {
195	int32_t manufacturer;
196	int32_t product;
197	char *cis1_info[4];
198	struct pccard_function *pf;
199	struct pccard_config_entry *cfe;
200};
201
202struct pccard_tuple {
203	unsigned int	code;
204	unsigned int	length;
205	u_long		mult;
206	bus_addr_t	ptr;
207	bus_space_tag_t	memt;
208	bus_space_handle_t memh;
209};
210
211struct pccard_product {
212	const char	*pp_name;		/* NULL if end of table */
213#define PCCARD_VENDOR_ANY ((u_int32_t) -1)
214	u_int32_t	pp_vendor;
215#define PCCARD_PRODUCT_ANY ((u_int32_t) -1)
216	u_int32_t	pp_product;
217	int		pp_expfunc;
218	const char	*pp_cis[4];
219};
220
221typedef int (*pccard_product_match_fn) (device_t dev,
222    const struct pccard_product *ent, int vpfmatch);
223
224const struct pccard_product
225	*pccard_product_lookup(device_t dev, const struct pccard_product *tab,
226	    size_t ent_size, pccard_product_match_fn matchfn);
227
228void	pccard_read_cis(struct pccard_softc *);
229void	pccard_check_cis_quirks(device_t);
230void	pccard_print_cis(device_t);
231int	pccard_scan_cis(device_t,
232		int (*) (struct pccard_tuple *, void *), void *);
233
234#define	pccard_cis_read_1(tuple, idx0)					\
235	(bus_space_read_1((tuple)->memt, (tuple)->memh, (tuple)->mult*(idx0)))
236
237#define	pccard_tuple_read_1(tuple, idx1)				\
238	(pccard_cis_read_1((tuple), ((tuple)->ptr+(2+(idx1)))))
239
240#define	pccard_tuple_read_2(tuple, idx2)				\
241	(pccard_tuple_read_1((tuple), (idx2)) | 			\
242	 (pccard_tuple_read_1((tuple), (idx2)+1)<<8))
243
244#define	pccard_tuple_read_3(tuple, idx3)				\
245	(pccard_tuple_read_1((tuple), (idx3)) |				\
246	 (pccard_tuple_read_1((tuple), (idx3)+1)<<8) |			\
247	 (pccard_tuple_read_1((tuple), (idx3)+2)<<16))
248
249#define	pccard_tuple_read_4(tuple, idx4)				\
250	(pccard_tuple_read_1((tuple), (idx4)) |				\
251	 (pccard_tuple_read_1((tuple), (idx4)+1)<<8) |			\
252	 (pccard_tuple_read_1((tuple), (idx4)+2)<<16) |			\
253	 (pccard_tuple_read_1((tuple), (idx4)+3)<<24))
254
255#define	pccard_tuple_read_n(tuple, n, idxn)				\
256	(((n)==1)?pccard_tuple_read_1((tuple), (idxn)) :		\
257	 (((n)==2)?pccard_tuple_read_2((tuple), (idxn)) :		\
258	  (((n)==3)?pccard_tuple_read_3((tuple), (idxn)) :		\
259	   /* n == 4 */ pccard_tuple_read_4((tuple), (idxn)))))
260
261#define	PCCARD_SPACE_MEMORY	1
262#define	PCCARD_SPACE_IO		2
263
264int	pccard_ccr_read(struct pccard_function *, int);
265void	pccard_ccr_write(struct pccard_function *, int, int);
266
267#define	pccard_mfc(sc)	(STAILQ_FIRST(&(sc)->card.pf_head) &&		\
268		 STAILQ_NEXT(STAILQ_FIRST(&(sc)->card.pf_head),pf_list))
269
270/* The following is the vestages of the NetBSD driver api */
271
272void	pccard_function_init(struct pccard_function *);
273int	pccard_function_enable(struct pccard_function *);
274void	pccard_function_disable(struct pccard_function *);
275
276#define	pccard_io_alloc(pf, start, size, align, pciop)			\
277	(pccard_chip_io_alloc((pf)->sc->pct, pf->sc->pch, (start),	\
278	 (size), (align), (pciop)))
279
280#define	pccard_io_free(pf, pciohp)					\
281	(pccard_chip_io_free((pf)->sc->pct, (pf)->sc->pch, (pciohp)))
282
283int	pccard_io_map(struct pccard_function *, int, bus_addr_t,
284	    bus_size_t, struct pccard_io_handle *, int *);
285void	pccard_io_unmap(struct pccard_function *, int);
286
287#define pccard_mem_alloc(pf, size, pcmhp)				\
288	(pccard_chip_mem_alloc((pf)->sc->pct, (pf)->sc->pch, (size), (pcmhp)))
289#define pccard_mem_free(pf, pcmhp)					\
290	(pccard_chip_mem_free((pf)->sc->pct, (pf)->sc->pch, (pcmhp)))
291#define pccard_mem_map(pf, kind, card_addr, size, pcmhp, offsetp, windowp) \
292	(pccard_chip_mem_map((pf)->sc->pct, (pf)->sc->pch, (kind),	\
293	 (card_addr), (size), (pcmhp), (offsetp), (windowp)))
294#define	pccard_mem_unmap(pf, window)					\
295	(pccard_chip_mem_unmap((pf)->sc->pct, (pf)->sc->pch, (window)))
296
297/* compat layer */
298static __inline int
299pccard_compat_probe(device_t dev)
300{
301	return (CARD_COMPAT_DO_PROBE(device_get_parent(dev), dev));
302}
303
304static __inline int
305pccard_compat_attach(device_t dev)
306{
307	return (CARD_COMPAT_DO_ATTACH(device_get_parent(dev), dev));
308}
309
310/* ivar interface */
311enum {
312	PCCARD_IVAR_ETHADDR,	/* read ethernet address from CIS tupple */
313	PCCARD_IVAR_VENDOR,
314	PCCARD_IVAR_PRODUCT,
315	PCCARD_IVAR_FUNCTION_NUMBER,
316	PCCARD_IVAR_VENDOR_STR,	/* CIS string for "Manufacturer" */
317	PCCARD_IVAR_PRODUCT_STR,/* CIS strnig for "Product" */
318	PCCARD_IVAR_CIS3_STR,
319	PCCARD_IVAR_CIS4_STR,
320	PCCARD_IVAR_FUNCTION
321};
322
323#define PCCARD_ACCESSOR(A, B, T)					\
324__inline static int							\
325pccard_get_ ## A(device_t dev, T *t)					\
326{									\
327	return BUS_READ_IVAR(device_get_parent(dev), dev, 		\
328	    PCCARD_IVAR_ ## B, (uintptr_t *) t);			\
329}
330
331PCCARD_ACCESSOR(ether,		ETHADDR,		u_int8_t)
332PCCARD_ACCESSOR(vendor,		VENDOR,			u_int32_t)
333PCCARD_ACCESSOR(product,	PRODUCT,		u_int32_t)
334PCCARD_ACCESSOR(function_number,FUNCTION_NUMBER,	u_int32_t)
335PCCARD_ACCESSOR(function,	FUNCTION,		u_int32_t)
336PCCARD_ACCESSOR(vendor_str,	VENDOR_STR,		char *)
337PCCARD_ACCESSOR(product_str,	PRODUCT_STR,		char *)
338PCCARD_ACCESSOR(cis3_str,	CIS3_STR,		char *)
339
340enum {
341	PCCARD_A_MEM_ATTR = 0x1
342};
343
344#define PCCARD_SOFTC(d) (struct pccard_softc *) device_get_softc(d)
345#define PCCARD_IVAR(d) (struct pccard_ivar *) device_get_ivars(d)
346