1/*	$NetBSD: pcmcia_cis_quirks.c,v 1.6 2000/04/12 21:07:55 scw Exp $ */
2
3#include <sys/cdefs.h>
4__FBSDID("$FreeBSD$");
5
6#define	PCCARDDEBUG
7
8/*-
9 * Copyright (c) 1998 Marc Horowitz.  All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by Marc Horowitz.
22 * 4. The name of the author may not be used to endorse or promote products
23 *    derived from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include <sys/param.h>
38#include <sys/bus.h>
39#include <sys/malloc.h>
40#include <sys/systm.h>
41
42#include <machine/bus.h>
43
44#include <dev/pccard/pccard_cis.h>
45#include <dev/pccard/pccardvar.h>
46#include <dev/pccard/pccardvarp.h>
47
48#include "pccarddevs.h"
49
50/* There are cards out there whose CIS flat-out lies.  This file
51   contains struct pccard_function chains for those devices. */
52
53/* these structures are just static templates which are then copied
54   into "live" allocated structures */
55
56struct pccard_function pccard_3cxem556_func0 = {
57	0,			/* function number */
58	PCCARD_FUNCTION_NETWORK,
59	0x07,			/* last cfe number */
60	0x800,			/* ccr_base */
61	0x63,			/* ccr_mask */
62};
63
64struct pccard_config_entry pccard_3cxem556_func0_cfe0 = {
65	0x07,			/* cfe number */
66	PCCARD_CFE_IO8 | PCCARD_CFE_IO16 | PCCARD_CFE_IRQLEVEL,
67	PCCARD_IFTYPE_IO,
68	1,			/* num_iospace */
69	4,			/* iomask */
70	{ { 0x0010, 0 } },	/* iospace */
71	0xffff,			/* irqmask */
72	0,			/* num_memspace */
73	{ },			/* memspace */
74	0,			/* maxtwins */
75};
76
77static struct pccard_function pccard_3cxem556_func1 = {
78	1,			/* function number */
79	PCCARD_FUNCTION_SERIAL,
80	0x27,			/* last cfe number */
81	0x900,			/* ccr_base */
82	0x63,			/* ccr_mask */
83};
84
85static struct pccard_config_entry pccard_3cxem556_func1_cfe0 = {
86	0x27,			/* cfe number */
87	PCCARD_CFE_IO8 | PCCARD_CFE_IRQLEVEL,
88	PCCARD_IFTYPE_IO,
89	1,			/* num_iospace */
90	3,			/* iomask */
91	{ { 0x0008, 0 } },	/* iospace */
92	0xffff,			/* irqmask */
93	0,			/* num_memspace */
94	{ },			/* memspace */
95	0,			/* maxtwins */
96};
97
98static struct pccard_function pccard_3ccfem556bi_func0 = {
99	0,			/* function number */
100	PCCARD_FUNCTION_NETWORK,
101	0x07,			/* last cfe number */
102	0x1000,			/* ccr_base */
103	0x267,			/* ccr_mask */
104};
105
106static struct pccard_config_entry pccard_3ccfem556bi_func0_cfe0 = {
107	0x07,			/* cfe number */
108	PCCARD_CFE_IO8 | PCCARD_CFE_IO16 | PCCARD_CFE_IRQLEVEL,
109	PCCARD_IFTYPE_IO,
110	1,			/* num_iospace */
111	5,			/* iomask */
112	{ { 0x0020, 0 } },	/* iospace */
113	0xffff,			/* irqmask */
114	0,			/* num_memspace */
115	{ },			/* memspace */
116	0,			/* maxtwins */
117};
118
119static struct pccard_function pccard_3ccfem556bi_func1 = {
120	1,			/* function number */
121	PCCARD_FUNCTION_SERIAL,
122	0x27,			/* last cfe number */
123	0x1100,			/* ccr_base */
124	0x277,			/* ccr_mask */
125};
126
127static struct pccard_config_entry pccard_3ccfem556bi_func1_cfe0 = {
128	0x27,			/* cfe number */
129	PCCARD_CFE_IO8 | PCCARD_CFE_IRQLEVEL,
130	PCCARD_IFTYPE_IO,
131	1,			/* num_iospace */
132	3,			/* iomask */
133	{ { 0x0008, 0 } },	/* iospace */
134	0xffff,			/* irqmask */
135	0,			/* num_memspace */
136	{ },			/* memspace */
137	0,			/* maxtwins */
138};
139
140static struct pccard_function pccard_3c1_func0 = {
141	0,			/* function number */
142	PCCARD_FUNCTION_NETWORK,
143	0x05,			/* last cfe number */
144	0x400,			/* ccr_base */
145	0x267,			/* ccr_mask */
146};
147
148static struct pccard_config_entry pccard_3c1_func0_cfe0 = {
149	0x05,			/* cfe number */
150	PCCARD_CFE_IO8 | PCCARD_CFE_IO16 | PCCARD_CFE_IRQLEVEL,
151	PCCARD_IFTYPE_IO,
152	1,			/* num_iospace */
153	5,			/* iomask */
154	{ { 0x0010, 0 } },	/* iospace */
155	0xffff,			/* irqmask */
156	0,			/* num_memspace */
157	{ },			/* memspace */
158	0,			/* maxtwins */
159};
160
161static struct pccard_function pccard_sveclancard_func0 = {
162	0,			/* function number */
163	PCCARD_FUNCTION_NETWORK,
164	0x1,			/* last cfe number */
165	0x100,			/* ccr_base */
166	0x1,			/* ccr_mask */
167};
168
169static struct pccard_config_entry pccard_sveclancard_func0_cfe0 = {
170	0x1,			/* cfe number */
171	PCCARD_CFE_MWAIT_REQUIRED | PCCARD_CFE_RDYBSY_ACTIVE |
172	PCCARD_CFE_WP_ACTIVE | PCCARD_CFE_BVD_ACTIVE | PCCARD_CFE_IO16,
173	PCCARD_IFTYPE_IO,
174	1,			/* num_iospace */
175	5,			/* iomask */
176	{ { 0x20, 0x300 } },	/* iospace */
177	0xdeb8,			/* irqmask */
178	0,			/* num_memspace */
179	{ },			/* memspace */
180	0,			/* maxtwins */
181};
182
183static struct pccard_function pccard_ndc_nd5100_func0 = {
184	0,			/* function number */
185	PCCARD_FUNCTION_NETWORK,
186	0x23,			/* last cfe number */
187	0x3f8,			/* ccr_base */
188	0x3,			/* ccr_mask */
189};
190
191static struct pccard_config_entry pccard_ndc_nd5100_func0_cfe0 = {
192	0x20,			/* cfe number */
193	PCCARD_CFE_MWAIT_REQUIRED | PCCARD_CFE_IO16 | PCCARD_CFE_IRQLEVEL,
194	PCCARD_IFTYPE_IO,
195	1,			/* num_iospace */
196	5,			/* iomask */
197	{ { 0x20, 0x300 } },	/* iospace */
198	0xdeb8,			/* irqmask */
199	0,			/* num_memspace */
200	{ },			/* memspace */
201	0,			/* maxtwins */
202};
203
204static struct pccard_function pccard_sierra_a555_func1 = {
205	1,			/* function number */
206	PCCARD_FUNCTION_SERIAL,
207	0x24,			/* last cfe number */
208	0x700,			/* ccr_base */
209	0x73,			/* ccr_mask */
210};
211
212static struct pccard_config_entry pccard_sierra_a555_func1_cfe0 = {
213	0x22,			/* cfe number */
214	PCCARD_CFE_IO8 | PCCARD_CFE_IRQLEVEL,
215	PCCARD_IFTYPE_IO,
216	1,			/* num_iospace */
217	0,			/* iomask */
218	{ { 0x0008, 0x3e8 } },	/* iospace */
219	0x3fbc,			/* irqmask */
220	0,			/* num_memspace */
221	{ },			/* memspace */
222	0,			/* maxtwins */
223};
224
225static struct pccard_cis_quirk pccard_cis_quirks[] = {
226	{ PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CXEM556, PCMCIA_CIS_INVALID,
227	  &pccard_3cxem556_func0, &pccard_3cxem556_func0_cfe0 },
228	{ PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CXEM556, PCMCIA_CIS_INVALID,
229	  &pccard_3cxem556_func1, &pccard_3cxem556_func1_cfe0 },
230	{ PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CXEM556INT, PCMCIA_CIS_INVALID,
231	  &pccard_3cxem556_func0, &pccard_3cxem556_func0_cfe0 },
232	{ PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CXEM556INT, PCMCIA_CIS_INVALID,
233	  &pccard_3cxem556_func1, &pccard_3cxem556_func1_cfe0 },
234	{ PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CCFEM556BI,
235	  PCMCIA_CIS_INVALID,
236	  &pccard_3ccfem556bi_func0, &pccard_3ccfem556bi_func0_cfe0 },
237	{ PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CCFEM556BI,
238	  PCMCIA_CIS_INVALID,
239	  &pccard_3ccfem556bi_func1, &pccard_3ccfem556bi_func1_cfe0 },
240	{ PCMCIA_VENDOR_SIERRA, PCMCIA_PRODUCT_SIERRA_A550,
241	  PCMCIA_CIS_INVALID,
242	  &pccard_sierra_a555_func1, &pccard_sierra_a555_func1_cfe0 },
243	{ PCMCIA_VENDOR_SIERRA, PCMCIA_PRODUCT_SIERRA_A555,
244	  PCMCIA_CIS_INVALID,
245	  &pccard_sierra_a555_func1, &pccard_sierra_a555_func1_cfe0 },
246	{ PCMCIA_VENDOR_SIERRA, PCMCIA_PRODUCT_SIERRA_A710,
247	  PCMCIA_CIS_INVALID,
248	  &pccard_sierra_a555_func1, &pccard_sierra_a555_func1_cfe0 },
249	{ PCMCIA_VENDOR_SIERRA, PCMCIA_PRODUCT_SIERRA_AC710,
250	  PCMCIA_CIS_INVALID,
251	  &pccard_sierra_a555_func1, &pccard_sierra_a555_func1_cfe0 },
252	{ PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3C1, PCMCIA_CIS_INVALID,
253	  &pccard_3c1_func0, &pccard_3c1_func0_cfe0 },
254	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID, PCMCIA_CIS_SVEC_LANCARD,
255	  &pccard_sveclancard_func0, &pccard_sveclancard_func0_cfe0 },
256	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID, PCMCIA_CIS_NDC_ND5100_E,
257	  &pccard_ndc_nd5100_func0, &pccard_ndc_nd5100_func0_cfe0 },
258};
259
260static int
261pccard_cis_quirk_match(struct pccard_softc *sc, struct pccard_cis_quirk *q)
262{
263	if ((sc->card.manufacturer == q->manufacturer) &&
264		(sc->card.product == q->product) &&
265		(((sc->card.manufacturer != PCMCIA_VENDOR_INVALID) &&
266		  (sc->card.product != PCMCIA_PRODUCT_INVALID)) ||
267		 ((sc->card.manufacturer == PCMCIA_VENDOR_INVALID) &&
268		  (sc->card.product == PCMCIA_PRODUCT_INVALID) &&
269		  sc->card.cis1_info[0] &&
270		  (strcmp(sc->card.cis1_info[0], q->cis1_info[0]) == 0) &&
271		  sc->card.cis1_info[1] &&
272		  (strcmp(sc->card.cis1_info[1], q->cis1_info[1]) == 0))))
273		return (1);
274	return (0);
275}
276
277void pccard_check_cis_quirks(device_t dev)
278{
279	struct pccard_softc *sc = PCCARD_SOFTC(dev);
280	int wiped = 0;
281	int i, j;
282	struct pccard_function *pf, *pf_next, *pf_last;
283	struct pccard_config_entry *cfe, *cfe_next;
284	struct pccard_cis_quirk *q;
285
286	pf = NULL;
287	pf_last = NULL;
288
289	for (i = 0; i < nitems(pccard_cis_quirks); i++) {
290		q = &pccard_cis_quirks[i];
291		if (!pccard_cis_quirk_match(sc, q))
292			continue;
293		if (!wiped) {
294			if (bootverbose) {
295				device_printf(dev, "using CIS quirks for ");
296				for (j = 0; j < 4; j++) {
297					if (sc->card.cis1_info[j] == NULL)
298						break;
299					if (j)
300						printf(", ");
301					printf("%s", sc->card.cis1_info[j]);
302				}
303				printf("\n");
304			}
305
306			for (pf = STAILQ_FIRST(&sc->card.pf_head); pf != NULL;
307			     pf = pf_next) {
308				for (cfe = STAILQ_FIRST(&pf->cfe_head); cfe != NULL;
309				     cfe = cfe_next) {
310					cfe_next = STAILQ_NEXT(cfe, cfe_list);
311					free(cfe, M_DEVBUF);
312				}
313				pf_next = STAILQ_NEXT(pf, pf_list);
314				free(pf, M_DEVBUF);
315			}
316
317			STAILQ_INIT(&sc->card.pf_head);
318			wiped = 1;
319		}
320
321		if (pf_last == q->pf) {
322			cfe = malloc(sizeof(*cfe), M_DEVBUF, M_NOWAIT);
323			if (cfe == NULL) {
324				device_printf(dev, "no memory for quirk (1)\n");
325				continue;
326			}
327			*cfe = *q->cfe;
328			STAILQ_INSERT_TAIL(&pf->cfe_head, cfe, cfe_list);
329		} else {
330			pf = malloc(sizeof(*pf), M_DEVBUF, M_NOWAIT);
331			if (pf == NULL) {
332				device_printf(dev,
333					"no memory for pccard function\n");
334				continue;
335			}
336			*pf = *q->pf;
337			STAILQ_INIT(&pf->cfe_head);
338			cfe = malloc(sizeof(*cfe), M_DEVBUF, M_NOWAIT);
339			if (cfe == NULL) {
340				free(pf, M_DEVBUF);
341				device_printf(dev, "no memory for quirk (2)\n");
342				continue;
343			}
344			*cfe = *q->cfe;
345			STAILQ_INSERT_TAIL(&pf->cfe_head, cfe, cfe_list);
346			STAILQ_INSERT_TAIL(&sc->card.pf_head, pf, pf_list);
347			pf_last = q->pf;
348		}
349	}
350}
351