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