1/*	$NetBSD: pcmciavar.h,v 1.12 2000/02/08 12:51:31 enami Exp $	*/
2/* $FreeBSD$ */
3
4/*-
5 * SPDX-License-Identifier: BSD-4-Clause
6 *
7 * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *	This product includes software developed by Marc Horowitz.
20 * 4. The name of the author may not be used to endorse or promote products
21 *    derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35/*
36 * Contains information about mapped/allocated i/o spaces.
37 */
38struct pccard_io_handle {
39	bus_space_tag_t iot;		/* bus space tag (from chipset) */
40	bus_space_handle_t ioh;		/* mapped space handle */
41	bus_addr_t      addr;		/* resulting address in bus space */
42	bus_size_t      size;		/* size of i/o space */
43	int             flags;		/* misc. information */
44	int		width;
45};
46
47#define	PCCARD_IO_ALLOCATED	0x01	/* i/o space was allocated */
48
49/*
50 * Contains information about allocated memory space.
51 */
52struct pccard_mem_handle {
53	bus_space_tag_t memt;		/* bus space tag (from chipset) */
54	bus_space_handle_t memh;	/* mapped space handle */
55	bus_addr_t      addr;		/* resulting address in bus space */
56	bus_size_t      size;		/* size of mem space */
57	bus_size_t      realsize;	/* how much we really allocated */
58	bus_addr_t	cardaddr;	/* Absolute address on card */
59	int		kind;
60};
61
62/* Bits for kind */
63#define	PCCARD_MEM_16BIT	1	/* 1 -> 16bit 0 -> 8bit */
64#define PCCARD_MEM_ATTR		2	/* 1 -> attribute mem 0 -> common */
65
66#define	PCCARD_WIDTH_AUTO	0
67#define	PCCARD_WIDTH_IO8	1
68#define	PCCARD_WIDTH_IO16	2
69
70struct pccard_tuple {
71	unsigned int	code;
72	unsigned int	length;
73	u_long		mult;		/* dist btn successive bytes */
74	bus_addr_t	ptr;
75	bus_space_tag_t	memt;
76	bus_space_handle_t memh;
77};
78
79typedef int (*pccard_scan_t)(const struct pccard_tuple *, void *);
80
81struct pccard_product {
82	const char	*pp_name;
83#define PCCARD_VENDOR_ANY (0xffffffff)
84	uint32_t	pp_vendor;		/* 0 == end of table */
85#define PCCARD_PRODUCT_ANY (0xffffffff)
86	uint32_t	pp_product;
87	const char	*pp_cis[4];
88};
89
90/**
91 * Note: There's no cis3 or cis4 reported for NOMATCH / pnpinfo events for
92 * pccard.  It's unclear if we actually need that for automatic loading or
93 * not.  These strings are informative, according to the standard.  Some Linux
94 * drivers match on them, for example.  However, FreeBSD's hardware probing is a
95 * little different than Linux, so it turns out we don't need them.  Some cards
96 * use CIS3 or CIS4 for a textual representation of the MAC address.  In short,
97 * belief that all the entries in Linux don't actually need to be separate there
98 * either, but they persist since it's hard to eliminate them and retest on old,
99 * possibly rare, hardware.  Despite years of collecting ~300 different PC Cards
100 * off E-Bay, I've not been able to find any that need CIS3/CIS4 to select which
101 * device attaches.
102 */
103#define PCCARD_PNP_DESCR "D:#;V32:manufacturer;V32:product;Z:cisvendor;Z:cisproduct;"
104#define PCCARD_PNP_INFO(t) \
105	MODULE_PNP_INFO(PCCARD_PNP_DESCR, pccard, t, t, nitems(t) - 1)
106
107typedef int (*pccard_product_match_fn) (device_t dev,
108    const struct pccard_product *ent, int vpfmatch);
109
110#include "card_if.h"
111
112/*
113 * Make this inline so that we don't have to worry about dangling references
114 * to it in the modules or the code.
115 */
116static inline const struct pccard_product *
117pccard_product_lookup(device_t dev, const struct pccard_product *tab,
118    size_t ent_size, pccard_product_match_fn matchfn)
119{
120	return CARD_DO_PRODUCT_LOOKUP(device_get_parent(dev), dev,
121	    tab, ent_size, matchfn);
122}
123
124#define	pccard_cis_read_1(tuple, idx0)					\
125	(bus_space_read_1((tuple)->memt, (tuple)->memh, (tuple)->mult*(idx0)))
126
127#define	pccard_tuple_read_1(tuple, idx1)				\
128	(pccard_cis_read_1((tuple), ((tuple)->ptr+(2+(idx1)))))
129
130#define	pccard_tuple_read_2(tuple, idx2)				\
131	(pccard_tuple_read_1((tuple), (idx2)) |				\
132	 (pccard_tuple_read_1((tuple), (idx2)+1)<<8))
133
134#define	pccard_tuple_read_3(tuple, idx3)				\
135	(pccard_tuple_read_1((tuple), (idx3)) |				\
136	 (pccard_tuple_read_1((tuple), (idx3)+1)<<8) |			\
137	 (pccard_tuple_read_1((tuple), (idx3)+2)<<16))
138
139#define	pccard_tuple_read_4(tuple, idx4)				\
140	(pccard_tuple_read_1((tuple), (idx4)) |				\
141	 (pccard_tuple_read_1((tuple), (idx4)+1)<<8) |			\
142	 (pccard_tuple_read_1((tuple), (idx4)+2)<<16) |			\
143	 (pccard_tuple_read_1((tuple), (idx4)+3)<<24))
144
145#define	pccard_tuple_read_n(tuple, n, idxn)				\
146	(((n)==1)?pccard_tuple_read_1((tuple), (idxn)) :		\
147	 (((n)==2)?pccard_tuple_read_2((tuple), (idxn)) :		\
148	  (((n)==3)?pccard_tuple_read_3((tuple), (idxn)) :		\
149	   /* n == 4 */ pccard_tuple_read_4((tuple), (idxn)))))
150
151#define	PCCARD_SPACE_MEMORY	1
152#define	PCCARD_SPACE_IO		2
153
154#define	pccard_mfc(sc)							\
155		(STAILQ_FIRST(&(sc)->card.pf_head) &&			\
156		 STAILQ_NEXT(STAILQ_FIRST(&(sc)->card.pf_head),pf_list))
157
158/* Convenience functions */
159
160static inline int
161pccard_cis_scan(device_t dev, pccard_scan_t fct, void *arg)
162{
163	return (CARD_CIS_SCAN(device_get_parent(dev), dev, fct, arg));
164}
165
166static inline int
167pccard_attr_read_1(device_t dev, uint32_t offset, uint8_t *val)
168{
169	return (CARD_ATTR_READ(device_get_parent(dev), dev, offset, val));
170}
171
172static inline int
173pccard_attr_write_1(device_t dev, uint32_t offset, uint8_t val)
174{
175	return (CARD_ATTR_WRITE(device_get_parent(dev), dev, offset, val));
176}
177
178static inline int
179pccard_ccr_read_1(device_t dev, uint32_t offset, uint8_t *val)
180{
181	return (CARD_CCR_READ(device_get_parent(dev), dev, offset, val));
182}
183
184static inline int
185pccard_ccr_write_1(device_t dev, uint32_t offset, uint8_t val)
186{
187	return (CARD_CCR_WRITE(device_get_parent(dev), dev, offset, val));
188}
189
190/* Hack */
191int pccard_select_cfe(device_t dev, int entry);
192
193/* ivar interface */
194enum {
195	PCCARD_IVAR_ETHADDR,	/* read ethernet address from CIS tupple */
196	PCCARD_IVAR_VENDOR,
197	PCCARD_IVAR_PRODUCT,
198	PCCARD_IVAR_PRODEXT,
199	PCCARD_IVAR_FUNCTION_NUMBER,
200	PCCARD_IVAR_VENDOR_STR,	/* CIS string for "Manufacturer" */
201	PCCARD_IVAR_PRODUCT_STR,/* CIS string for "Product" */
202	PCCARD_IVAR_CIS3_STR,
203	PCCARD_IVAR_CIS4_STR,
204	PCCARD_IVAR_FUNCTION,
205	PCCARD_IVAR_FUNCE_DISK
206};
207
208#define PCCARD_ACCESSOR(A, B, T)					\
209static inline int							\
210pccard_get_ ## A(device_t dev, T *t)					\
211{									\
212	return BUS_READ_IVAR(device_get_parent(dev), dev,		\
213	    PCCARD_IVAR_ ## B, (uintptr_t *) t);			\
214}
215
216PCCARD_ACCESSOR(ether,		ETHADDR,		uint8_t)
217PCCARD_ACCESSOR(vendor,		VENDOR,			uint32_t)
218PCCARD_ACCESSOR(product,	PRODUCT,		uint32_t)
219PCCARD_ACCESSOR(prodext,	PRODEXT,		uint16_t)
220PCCARD_ACCESSOR(function_number,FUNCTION_NUMBER,	uint32_t)
221PCCARD_ACCESSOR(function,	FUNCTION,		uint32_t)
222PCCARD_ACCESSOR(funce_disk,	FUNCE_DISK,		uint16_t)
223PCCARD_ACCESSOR(vendor_str,	VENDOR_STR,		const char *)
224PCCARD_ACCESSOR(product_str,	PRODUCT_STR,		const char *)
225PCCARD_ACCESSOR(cis3_str,	CIS3_STR,		const char *)
226PCCARD_ACCESSOR(cis4_str,	CIS4_STR,		const char *)
227
228/* shared memory flags */
229enum {
230	PCCARD_A_MEM_COM,       /* common */
231	PCCARD_A_MEM_ATTR,      /* attribute */
232	PCCARD_A_MEM_8BIT,      /* 8 bit */
233	PCCARD_A_MEM_16BIT      /* 16 bit */
234};
235
236#define PCCARD_S(a, b) PCMCIA_STR_ ## a ## _ ## b
237#define PCCARD_P(a, b) PCMCIA_PRODUCT_ ## a ## _ ## b
238#define PCCARD_C(a, b) PCMCIA_CIS_ ## a ## _ ## b
239#define PCMCIA_CARD_D(v, p) { PCCARD_S(v, p), PCMCIA_VENDOR_ ## v, \
240		PCCARD_P(v, p), PCCARD_C(v, p) }
241#define PCMCIA_CARD(v, p) { PCCARD_S(v, p), PCMCIA_VENDOR_ ## v, \
242		PCCARD_P(v, p), PCCARD_C(v, p) }
243
244/*
245 * Defines to decode the get_funce_disk return value.  See the PCMCIA standard
246 * for all the details of what these bits mean.
247 */
248#define	PFD_I_V_MASK		0x3
249#define PFD_I_V_NONE_REQUIRED	0x0
250#define PFD_I_V_REQ_MOD_ACC	0x1
251#define PFD_I_V_REQ_ACC		0x2
252#define PFD_I_V_REQ_ALWYS	0x1
253#define PFD_I_S			0x4	/* 0 rotating, 1 silicon */
254#define PFD_I_U			0x8	/* SN Uniq? */
255#define	PFD_I_D			0x10	/* 0 - 1 drive, 1 - 2 drives */
256#define PFD_P_P0		0x100
257#define PFD_P_P1		0x200
258#define PFD_P_P2		0x400
259#define PFD_P_P3		0x800
260#define PFD_P_N			0x1000	/* 3f7/377 excluded? */
261#define PFD_P_E			0x2000	/* Index bit supported? */
262#define	PFD_P_I			0x4000	/* twincard */
263