166200Simp/*	$NetBSD: pcmciavar.h,v 1.12 2000/02/08 12:51:31 enami Exp $	*/
252506Simp/* $FreeBSD$ */
352506Simp
4139749Simp/*-
552506Simp * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
652506Simp *
752506Simp * Redistribution and use in source and binary forms, with or without
852506Simp * modification, are permitted provided that the following conditions
952506Simp * are met:
1052506Simp * 1. Redistributions of source code must retain the above copyright
1152506Simp *    notice, this list of conditions and the following disclaimer.
1252506Simp * 2. Redistributions in binary form must reproduce the above copyright
1352506Simp *    notice, this list of conditions and the following disclaimer in the
1452506Simp *    documentation and/or other materials provided with the distribution.
1552506Simp * 3. All advertising materials mentioning features or use of this software
1652506Simp *    must display the following acknowledgement:
1752506Simp *	This product includes software developed by Marc Horowitz.
1852506Simp * 4. The name of the author may not be used to endorse or promote products
1952506Simp *    derived from this software without specific prior written permission.
2052506Simp *
2152506Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2252506Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2352506Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2452506Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2552506Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2652506Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2752506Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2852506Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2952506Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3052506Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3152506Simp */
3252506Simp
3352506Simp/*
34215034Sbrucec * PCCARD_API_LEVEL.  When set to 5, we provide a 5.x compatible API
35147580Simp * for driver writers that have to share their code between 5.x and 6.x.
36147580Simp * The 5.x compatibility interfaces will be unsupported in 7.0, at which
37147580Simp * point we'll only support 6 and newer, etc.
38147580Simp */
39147580Simp#ifndef PCCARD_API_LEVEL
40147580Simp#define PCCARD_API_LEVEL 6
41147580Simp#elif PCCARD_API_LEVEL < 5
42147580Simp#error "pccard API less than 5 unsupported"
43147580Simp#endif
44147580Simp
45147580Simp/*
4652506Simp * Contains information about mapped/allocated i/o spaces.
4752506Simp */
4852506Simpstruct pccard_io_handle {
4952506Simp	bus_space_tag_t iot;		/* bus space tag (from chipset) */
5052506Simp	bus_space_handle_t ioh;		/* mapped space handle */
5152506Simp	bus_addr_t      addr;		/* resulting address in bus space */
5252506Simp	bus_size_t      size;		/* size of i/o space */
5352506Simp	int             flags;		/* misc. information */
5454250Simp	int		width;
5552506Simp};
5652506Simp
5752506Simp#define	PCCARD_IO_ALLOCATED	0x01	/* i/o space was allocated */
5852506Simp
5952506Simp/*
6052506Simp * Contains information about allocated memory space.
6152506Simp */
6252506Simpstruct pccard_mem_handle {
6352506Simp	bus_space_tag_t memt;		/* bus space tag (from chipset) */
6452506Simp	bus_space_handle_t memh;	/* mapped space handle */
6552506Simp	bus_addr_t      addr;		/* resulting address in bus space */
6652506Simp	bus_size_t      size;		/* size of mem space */
6752506Simp	bus_size_t      realsize;	/* how much we really allocated */
6889959Simp	bus_addr_t	cardaddr;	/* Absolute address on card */
6954250Simp	int		kind;
7052506Simp};
7152506Simp
72186795Simp/* Bits for kind */
73186795Simp#define	PCCARD_MEM_16BIT	1	/* 1 -> 16bit 0 -> 8bit */
74186795Simp#define PCCARD_MEM_ATTR		2	/* 1 -> attribute mem 0 -> common */
75186795Simp
7659389Simp#define	PCCARD_WIDTH_AUTO	0
7759389Simp#define	PCCARD_WIDTH_IO8	1
7859389Simp#define	PCCARD_WIDTH_IO16	2
7959389Simp
8052506Simpstruct pccard_tuple {
8152506Simp	unsigned int	code;
8252506Simp	unsigned int	length;
83150362Simp	u_long		mult;		/* dist btn successive bytes */
8452506Simp	bus_addr_t	ptr;
8552506Simp	bus_space_tag_t	memt;
8652506Simp	bus_space_handle_t memh;
8752506Simp};
8852506Simp
89147729Simptypedef int (*pccard_scan_t)(const struct pccard_tuple *, void *);
90147711Simp
9166200Simpstruct pccard_product {
92150532Simp	const char	*pp_name;
93113241Simp#define PCCARD_VENDOR_ANY (0xffffffff)
94150532Simp	uint32_t	pp_vendor;		/* 0 == end of table */
95113241Simp#define PCCARD_PRODUCT_ANY (0xffffffff)
96113241Simp	uint32_t	pp_product;
9771322Simp	const char	*pp_cis[4];
9866200Simp};
9966200Simp
10066200Simptypedef int (*pccard_product_match_fn) (device_t dev,
10166200Simp    const struct pccard_product *ent, int vpfmatch);
10266200Simp
103100218Simp#include "card_if.h"
10466200Simp
105100218Simp/*
106100218Simp * make this inline so that we don't have to worry about dangling references
107100218Simp * to it in the modules or the code.
108100218Simp */
109100218Simpstatic __inline const struct pccard_product *
110100218Simppccard_product_lookup(device_t dev, const struct pccard_product *tab,
111100218Simp    size_t ent_size, pccard_product_match_fn matchfn)
112100218Simp{
113100218Simp	return CARD_DO_PRODUCT_LOOKUP(device_get_parent(dev), dev,
114100218Simp	    tab, ent_size, matchfn);
115100218Simp}
116100218Simp
11752506Simp#define	pccard_cis_read_1(tuple, idx0)					\
11852506Simp	(bus_space_read_1((tuple)->memt, (tuple)->memh, (tuple)->mult*(idx0)))
11952506Simp
12052506Simp#define	pccard_tuple_read_1(tuple, idx1)				\
12152506Simp	(pccard_cis_read_1((tuple), ((tuple)->ptr+(2+(idx1)))))
12252506Simp
12352506Simp#define	pccard_tuple_read_2(tuple, idx2)				\
12482378Sjon	(pccard_tuple_read_1((tuple), (idx2)) |				\
12552506Simp	 (pccard_tuple_read_1((tuple), (idx2)+1)<<8))
12652506Simp
12752506Simp#define	pccard_tuple_read_3(tuple, idx3)				\
12852506Simp	(pccard_tuple_read_1((tuple), (idx3)) |				\
12952506Simp	 (pccard_tuple_read_1((tuple), (idx3)+1)<<8) |			\
13052506Simp	 (pccard_tuple_read_1((tuple), (idx3)+2)<<16))
13152506Simp
13252506Simp#define	pccard_tuple_read_4(tuple, idx4)				\
13352506Simp	(pccard_tuple_read_1((tuple), (idx4)) |				\
13452506Simp	 (pccard_tuple_read_1((tuple), (idx4)+1)<<8) |			\
13552506Simp	 (pccard_tuple_read_1((tuple), (idx4)+2)<<16) |			\
13652506Simp	 (pccard_tuple_read_1((tuple), (idx4)+3)<<24))
13752506Simp
13852506Simp#define	pccard_tuple_read_n(tuple, n, idxn)				\
13952506Simp	(((n)==1)?pccard_tuple_read_1((tuple), (idxn)) :		\
14052506Simp	 (((n)==2)?pccard_tuple_read_2((tuple), (idxn)) :		\
14152506Simp	  (((n)==3)?pccard_tuple_read_3((tuple), (idxn)) :		\
14252506Simp	   /* n == 4 */ pccard_tuple_read_4((tuple), (idxn)))))
14352506Simp
14452506Simp#define	PCCARD_SPACE_MEMORY	1
14552506Simp#define	PCCARD_SPACE_IO		2
14652506Simp
147120849Simp#define	pccard_mfc(sc)							\
148120849Simp		(STAILQ_FIRST(&(sc)->card.pf_head) &&			\
14952506Simp		 STAILQ_NEXT(STAILQ_FIRST(&(sc)->card.pf_head),pf_list))
15052506Simp
151150098Simp/* Convenience functions */
152150098Simp
153150098Simpstatic __inline int
154150098Simppccard_cis_scan(device_t dev, pccard_scan_t fct, void *arg)
155150098Simp{
156150098Simp	return (CARD_CIS_SCAN(device_get_parent(dev), dev, fct, arg));
157150098Simp}
158150098Simp
159150098Simpstatic __inline int
160150098Simppccard_attr_read_1(device_t dev, uint32_t offset, uint8_t *val)
161150098Simp{
162150098Simp	return (CARD_ATTR_READ(device_get_parent(dev), dev, offset, val));
163150098Simp}
164150098Simp
165150098Simpstatic __inline int
166150098Simppccard_attr_write_1(device_t dev, uint32_t offset, uint8_t val)
167150098Simp{
168150098Simp	return (CARD_ATTR_WRITE(device_get_parent(dev), dev, offset, val));
169150098Simp}
170150098Simp
171150098Simpstatic __inline int
172150098Simppccard_ccr_read_1(device_t dev, uint32_t offset, uint8_t *val)
173150098Simp{
174150098Simp	return (CARD_CCR_READ(device_get_parent(dev), dev, offset, val));
175150098Simp}
176150098Simp
177150098Simpstatic __inline int
178150098Simppccard_ccr_write_1(device_t dev, uint32_t offset, uint8_t val)
179150098Simp{
180150098Simp	return (CARD_CCR_WRITE(device_get_parent(dev), dev, offset, val));
181150098Simp}
182150098Simp
183181342Simp/* Hack */
184181342Simpint pccard_select_cfe(device_t dev, int entry);
185181342Simp
18656361Shosokawa/* ivar interface */
18756361Shosokawaenum {
18856361Shosokawa	PCCARD_IVAR_ETHADDR,	/* read ethernet address from CIS tupple */
18966200Simp	PCCARD_IVAR_VENDOR,
19066200Simp	PCCARD_IVAR_PRODUCT,
19190964Sshiba	PCCARD_IVAR_PRODEXT,
19266200Simp	PCCARD_IVAR_FUNCTION_NUMBER,
19366200Simp	PCCARD_IVAR_VENDOR_STR,	/* CIS string for "Manufacturer" */
194215034Sbrucec	PCCARD_IVAR_PRODUCT_STR,/* CIS string for "Product" */
19567167Simp	PCCARD_IVAR_CIS3_STR,
19675761Simp	PCCARD_IVAR_CIS4_STR,
197189318Simp	PCCARD_IVAR_FUNCTION,
198189318Simp	PCCARD_IVAR_FUNCE_DISK
19956361Shosokawa};
20056361Shosokawa
20166200Simp#define PCCARD_ACCESSOR(A, B, T)					\
20266200Simp__inline static int							\
20366200Simppccard_get_ ## A(device_t dev, T *t)					\
20466200Simp{									\
20582378Sjon	return BUS_READ_IVAR(device_get_parent(dev), dev,		\
20666200Simp	    PCCARD_IVAR_ ## B, (uintptr_t *) t);			\
20756361Shosokawa}
20858581Simp
209113241SimpPCCARD_ACCESSOR(ether,		ETHADDR,		uint8_t)
210113241SimpPCCARD_ACCESSOR(vendor,		VENDOR,			uint32_t)
211113241SimpPCCARD_ACCESSOR(product,	PRODUCT,		uint32_t)
212113241SimpPCCARD_ACCESSOR(prodext,	PRODEXT,		uint16_t)
213113241SimpPCCARD_ACCESSOR(function_number,FUNCTION_NUMBER,	uint32_t)
214113241SimpPCCARD_ACCESSOR(function,	FUNCTION,		uint32_t)
215189318SimpPCCARD_ACCESSOR(funce_disk,	FUNCE_DISK,		uint16_t)
216121521SimpPCCARD_ACCESSOR(vendor_str,	VENDOR_STR,		const char *)
217121521SimpPCCARD_ACCESSOR(product_str,	PRODUCT_STR,		const char *)
218121521SimpPCCARD_ACCESSOR(cis3_str,	CIS3_STR,		const char *)
219121521SimpPCCARD_ACCESSOR(cis4_str,	CIS4_STR,		const char *)
22066200Simp
22176387Sdmlb/* shared memory flags */
22258581Simpenum {
22376387Sdmlb	PCCARD_A_MEM_COM,       /* common */
22476387Sdmlb	PCCARD_A_MEM_ATTR,      /* attribute */
22576387Sdmlb	PCCARD_A_MEM_8BIT,      /* 8 bit */
22676387Sdmlb	PCCARD_A_MEM_16BIT      /* 16 bit */
22758581Simp};
22858581Simp
22986385Simp#define PCCARD_S(a, b) PCMCIA_STR_ ## a ## _ ## b
23086385Simp#define PCCARD_P(a, b) PCMCIA_PRODUCT_ ## a ## _ ## b
23186385Simp#define PCCARD_C(a, b) PCMCIA_CIS_ ## a ## _ ## b
232147580Simp#if PCCARD_API_LEVEL >= 6
233147580Simp#define PCMCIA_CARD_D(v, p) { PCCARD_S(v, p), PCMCIA_VENDOR_ ## v, \
234147580Simp		PCCARD_P(v, p), PCCARD_C(v, p) }
235147580Simp#define PCMCIA_CARD2_D(v1, p1, p2) \
236147580Simp		{ PCMCIA_STR_ ## p2, PCMCIA_VENDOR_ ## v1, PCCARD_P(v1, p1), \
237147580Simp		  PCMCIA_CIS_ ## p2}
238147580Simp#define PCMCIA_CARD(v, p) { NULL, PCMCIA_VENDOR_ ## v, \
239147580Simp		PCCARD_P(v, p), PCCARD_C(v, p) }
240147580Simp#define PCMCIA_CARD2(v1, p1, p2) \
241147580Simp		{ NULL, PCMCIA_VENDOR_ ## v1, PCCARD_P(v1, p1), \
242147580Simp		  PCMCIA_CIS_ ## p2}
243147580Simp#else
244113322Simp#define PCMCIA_CARD_D(v, p, f) { PCCARD_S(v, p), PCMCIA_VENDOR_ ## v, \
245147580Simp		PCCARD_P(v, p), PCCARD_C(v, p) }
246113322Simp#define PCMCIA_CARD2_D(v1, p1, p2, f) \
24786385Simp		{ PCMCIA_STR_ ## p2, PCMCIA_VENDOR_ ## v1, PCCARD_P(v1, p1), \
248147580Simp		  PCMCIA_CIS_ ## p2}
249113322Simp#define PCMCIA_CARD(v, p, f) { NULL, PCMCIA_VENDOR_ ## v, \
250147580Simp		PCCARD_P(v, p), PCCARD_C(v, p) }
251113322Simp#define PCMCIA_CARD2(v1, p1, p2, f) \
252113257Simp		{ NULL, PCMCIA_VENDOR_ ## v1, PCCARD_P(v1, p1), \
253147580Simp		  PCMCIA_CIS_ ## p2}
254147580Simp#endif
255189318Simp
256189318Simp/*
257215034Sbrucec * Defines to decode the get_funce_disk return value.  See the PCMCIA standard
258189318Simp * for all the details of what these bits mean.
259189318Simp */
260189318Simp#define	PFD_I_V_MASK		0x3
261189318Simp#define PFD_I_V_NONE_REQUIRED	0x0
262189318Simp#define PFD_I_V_REQ_MOD_ACC	0x1
263189318Simp#define PFD_I_V_REQ_ACC		0x2
264189318Simp#define PFD_I_V_REQ_ALWYS	0x1
265189318Simp#define PFD_I_S			0x4	/* 0 rotating, 1 silicon */
266189318Simp#define PFD_I_U			0x8	/* SN Uniq? */
267189318Simp#define	PFD_I_D			0x10	/* 0 - 1 drive, 1 - 2 drives */
268189318Simp#define PFD_P_P0		0x100
269189318Simp#define PFD_P_P1		0x200
270189318Simp#define PFD_P_P2		0x400
271189318Simp#define PFD_P_P3		0x800
272189318Simp#define PFD_P_N			0x1000	/* 3f7/377 excluded? */
273189318Simp#define PFD_P_E			0x2000	/* Index bit supported? */
274189318Simp#define	PFD_P_I			0x4000	/* twincard */
275