xref: /openbsd/sys/dev/pcmcia/aic_pcmcia.c (revision d89ec533)
1 /*	$OpenBSD: aic_pcmcia.c,v 1.18 2021/03/07 06:20:09 jsg Exp $	*/
2 /*	$NetBSD: aic_pcmcia.c,v 1.6 1998/07/19 17:28:15 christos Exp $	*/
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/param.h>
34 #include <sys/systm.h>
35 #include <sys/selinfo.h>
36 #include <sys/device.h>
37 
38 #include <machine/cpu.h>
39 #include <machine/bus.h>
40 #include <machine/intr.h>
41 
42 #include <scsi/scsi_all.h>
43 #include <scsi/scsiconf.h>
44 
45 #include <dev/ic/aic6360var.h>
46 
47 #include <dev/pcmcia/pcmciavar.h>
48 #include <dev/pcmcia/pcmciadevs.h>
49 
50 int	aic_pcmcia_match(struct device *, void *, void *);
51 void	aic_pcmcia_attach(struct device *, struct device *, void *);
52 int	aic_pcmcia_detach(struct device *, int);
53 
54 struct aic_pcmcia_softc {
55 	struct aic_softc sc_aic;		/* real "aic" softc */
56 
57 	/* PCMCIA-specific goo. */
58 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
59 	int sc_io_window;			/* our i/o window */
60 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
61 	void *sc_ih;				/* interrupt handler */
62 };
63 
64 struct cfattach aic_pcmcia_ca = {
65 	sizeof(struct aic_pcmcia_softc), aic_pcmcia_match, aic_pcmcia_attach,
66 	aic_pcmcia_detach
67 };
68 
69 struct aic_pcmcia_product {
70 	u_int16_t	app_vendor;		/* PCMCIA vendor ID */
71 	u_int16_t	app_product;		/* PCMCIA product ID */
72 	int		app_expfunc;		/* expected function number */
73 } aic_pcmcia_prod[] = {
74 	{ PCMCIA_VENDOR_ADAPTEC,	PCMCIA_PRODUCT_ADAPTEC_APA1460_1,
75 	  0 },
76 
77 	{ PCMCIA_VENDOR_ADAPTEC,	PCMCIA_PRODUCT_ADAPTEC_APA1460_2,
78 	  0 },
79 
80 	{ PCMCIA_VENDOR_NEWMEDIA,	PCMCIA_PRODUCT_NEWMEDIA_BUSTOASTER,
81 	  0 }
82 };
83 
84 int
85 aic_pcmcia_match(struct device *parent, void *match, void *aux)
86 {
87 	struct pcmcia_attach_args *pa = aux;
88 	int i;
89 
90 	for (i = 0; i < nitems(aic_pcmcia_prod); i++)
91 		if (pa->manufacturer == aic_pcmcia_prod[i].app_vendor &&
92 		    pa->product == aic_pcmcia_prod[i].app_product &&
93 		    pa->pf->number == aic_pcmcia_prod[i].app_expfunc)
94 			return (1);
95 	return (0);
96 }
97 
98 void
99 aic_pcmcia_attach(struct device *parent, struct device *self, void *aux)
100 {
101 	struct aic_pcmcia_softc *psc = (void *)self;
102 	struct aic_softc *sc = &psc->sc_aic;
103 	struct pcmcia_attach_args *pa = aux;
104 	struct pcmcia_config_entry *cfe;
105 	struct pcmcia_function *pf = pa->pf;
106 	const char *intrstr;
107 
108 	psc->sc_pf = pf;
109 
110 	for (cfe = SIMPLEQ_FIRST(&pf->cfe_head); cfe != NULL;
111 	    cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
112 		if (cfe->num_memspace != 0 ||
113 		    cfe->num_iospace != 1)
114 			continue;
115 
116 		/* The bustoaster has a default config as first
117 		 * entry, we don't want to use that. */
118 
119 		if (pa->manufacturer == PCMCIA_VENDOR_NEWMEDIA &&
120 		    pa->product == PCMCIA_PRODUCT_NEWMEDIA_BUSTOASTER &&
121 		    cfe->iospace[0].start == 0)
122 			continue;
123 
124 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
125 		    cfe->iospace[0].length, AIC_NPORTS, &psc->sc_pcioh) == 0)
126 			break;
127 	}
128 
129 	if (cfe == 0) {
130 		printf(": can't alloc i/o space\n");
131 		return;
132 	}
133 
134 	sc->sc_iot = psc->sc_pcioh.iot;
135 	sc->sc_ioh = psc->sc_pcioh.ioh;
136 
137 	/* Enable the card. */
138 	pcmcia_function_init(pf, cfe);
139 	if (pcmcia_function_enable(pf)) {
140 		printf(": function enable failed\n");
141 		return;
142 	}
143 
144 	/* Map in the io space */
145 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
146 	    &psc->sc_pcioh, &psc->sc_io_window)) {
147 		printf(": can't map i/o space\n");
148 		return;
149 	}
150 
151 	printf(" port 0x%lx/%lu", psc->sc_pcioh.addr,
152 	    (u_long)psc->sc_pcioh.size);
153 
154 	if (!aic_find(sc->sc_iot, sc->sc_ioh)) {
155 		printf(": unable to detect chip!\n");
156 		return;
157 	}
158 
159 	/* Establish the interrupt handler. */
160 	psc->sc_ih = pcmcia_intr_establish(pa->pf, IPL_BIO,
161 	    aicintr, sc, sc->sc_dev.dv_xname);
162 	intrstr = pcmcia_intr_string(psc->sc_pf, psc->sc_ih);
163 	printf("%s%s\n", *intrstr ? ", " : "", intrstr);
164 	if (psc->sc_ih == NULL)
165 		return;
166 
167 	aicattach(sc);
168 
169 }
170 
171 int
172 aic_pcmcia_detach(struct device *self, int flags)
173 {
174 	struct aic_pcmcia_softc *sc= (void *)self;
175 	int error;
176 
177 	error = aic_detach(self, flags);
178 	if (error)
179 		return (error);
180 
181 	/* Unmap our i/o window and i/o space. */
182 	pcmcia_io_unmap(sc->sc_pf, sc->sc_io_window);
183 	pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
184 
185 	return (0);
186 }
187