com_pcmcia.c revision 1.46 1 /* $NetBSD: com_pcmcia.c,v 1.46 2004/08/10 18:43:49 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c) 1991 The Regents of the University of California.
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * @(#)com.c 7.5 (Berkeley) 5/16/91
68 */
69
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: com_pcmcia.c,v 1.46 2004/08/10 18:43:49 mycroft Exp $");
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/ioctl.h>
76 #include <sys/select.h>
77 #include <sys/tty.h>
78 #include <sys/proc.h>
79 #include <sys/user.h>
80 #include <sys/conf.h>
81 #include <sys/file.h>
82 #include <sys/uio.h>
83 #include <sys/kernel.h>
84 #include <sys/syslog.h>
85 #include <sys/device.h>
86
87 #include <machine/intr.h>
88 #include <machine/bus.h>
89
90 #include <dev/pcmcia/pcmciavar.h>
91 #include <dev/pcmcia/pcmciareg.h>
92 #include <dev/pcmcia/pcmciadevs.h>
93
94 #include <dev/ic/comreg.h>
95 #include <dev/ic/comvar.h>
96
97 #include <dev/isa/isareg.h>
98
99 struct com_dev {
100 char *cis1_info[4];
101 };
102
103 /* Devices that we need to match by CIS strings */
104 static struct com_dev com_devs[] = {
105 { PCMCIA_CIS_MEGAHERTZ_XJ2288 },
106 };
107
108
109 static int com_devs_size = sizeof(com_devs) / sizeof(com_devs[0]);
110 static struct com_dev *com_dev_match __P((struct pcmcia_card *));
111
112 int com_pcmcia_match __P((struct device *, struct cfdata *, void *));
113 int com_pcmcia_validate_config __P((struct pcmcia_config_entry *));
114 void com_pcmcia_attach __P((struct device *, struct device *, void *));
115 int com_pcmcia_detach __P((struct device *, int));
116 void com_pcmcia_cleanup __P((void *));
117
118 int com_pcmcia_enable __P((struct com_softc *));
119 void com_pcmcia_disable __P((struct com_softc *));
120
121 struct com_pcmcia_softc {
122 struct com_softc sc_com; /* real "com" softc */
123
124 /* PCMCIA-specific goo */
125 struct pcmcia_function *sc_pf; /* our PCMCIA function */
126 void *sc_ih; /* interrupt handler */
127 int sc_attached;
128 };
129
130 CFATTACH_DECL(com_pcmcia, sizeof(struct com_pcmcia_softc),
131 com_pcmcia_match, com_pcmcia_attach, com_pcmcia_detach, com_activate);
132
133 /* Look for pcmcia cards with particular CIS strings */
134 static struct com_dev *
135 com_dev_match(card)
136 struct pcmcia_card *card;
137 {
138 int i, j;
139
140 for (i = 0; i < com_devs_size; i++) {
141 for (j = 0; j < 4; j++)
142 if (com_devs[i].cis1_info[j] &&
143 strcmp(com_devs[i].cis1_info[j],
144 card->cis1_info[j]) != 0)
145 break;
146 if (j == 4)
147 return &com_devs[i];
148 }
149
150 return NULL;
151 }
152
153
154 int
155 com_pcmcia_match(parent, match, aux)
156 struct device *parent;
157 struct cfdata *match;
158 void *aux;
159 {
160 int comportmask;
161 struct pcmcia_attach_args *pa = aux;
162 struct pcmcia_config_entry *cfe;
163
164 /* 1. Does it claim to be a serial device? */
165 if (pa->pf->function == PCMCIA_FUNCTION_SERIAL)
166 return 1;
167
168 /* 2. Does it have all four 'standard' port ranges? */
169 comportmask = 0;
170 SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
171 switch (cfe->iospace[0].start) {
172 case IO_COM1:
173 comportmask |= 1;
174 break;
175 case IO_COM2:
176 comportmask |= 2;
177 break;
178 case IO_COM3:
179 comportmask |= 4;
180 break;
181 case IO_COM4:
182 comportmask |= 8;
183 break;
184 }
185 }
186
187 if (comportmask == 15)
188 return 1;
189
190 /* 3. Is this a card we know about? */
191 if (com_dev_match(pa->card) != NULL)
192 return 1;
193
194 return 0;
195 }
196
197 int
198 com_pcmcia_validate_config(cfe)
199 struct pcmcia_config_entry *cfe;
200 {
201 if (cfe->iftype != PCMCIA_IFTYPE_IO ||
202 cfe->num_iospace != 1)
203 return (EINVAL);
204 /* Some cards have a memory space, but we don't use it. */
205 cfe->num_memspace = 0;
206 /* Some cards don't have IO8 in their CIS. */
207 cfe->flags |= PCMCIA_CFE_IO8;
208 return (0);
209 }
210
211 void
212 com_pcmcia_attach(parent, self, aux)
213 struct device *parent, *self;
214 void *aux;
215 {
216 struct com_pcmcia_softc *psc = (void *) self;
217 struct com_softc *sc = &psc->sc_com;
218 struct pcmcia_attach_args *pa = aux;
219 struct pcmcia_config_entry *cfe;
220 int error;
221
222 psc->sc_pf = pa->pf;
223
224 error = pcmcia_function_configure(pa->pf, com_pcmcia_validate_config);
225 if (error) {
226 aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
227 error);
228 return;
229 }
230
231 cfe = pa->pf->cfe;
232 sc->sc_iot = cfe->iospace[0].handle.iot;
233 sc->sc_ioh = cfe->iospace[0].handle.ioh;
234
235 error = com_pcmcia_enable(sc);
236 if (error) {
237 aprint_error("%s: enable failed, error=%d\n", self->dv_xname,
238 error);
239 goto fail;
240 }
241
242 sc->enabled = 1;
243
244 sc->sc_iobase = -1;
245 sc->sc_frequency = COM_FREQ;
246
247 sc->enable = com_pcmcia_enable;
248 sc->disable = com_pcmcia_disable;
249
250 aprint_normal("%s", self->dv_xname);
251
252 com_attach_subr(sc);
253
254 sc->enabled = 0;
255
256 psc->sc_attached = 1;
257 com_pcmcia_disable(sc);
258 return;
259
260 fail:
261 pcmcia_function_unconfigure(pa->pf);
262 }
263
264 int
265 com_pcmcia_detach(self, flags)
266 struct device *self;
267 int flags;
268 {
269 struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) self;
270 int error;
271
272 if (!psc->sc_attached)
273 return (0);
274
275 if ((error = com_detach(self, flags)) != 0)
276 return error;
277
278 pcmcia_function_unconfigure(psc->sc_pf);
279 return (0);
280 }
281
282 int
283 com_pcmcia_enable(sc)
284 struct com_softc *sc;
285 {
286 struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
287 struct pcmcia_function *pf = psc->sc_pf;
288 int error;
289
290 /* establish the interrupt. */
291 psc->sc_ih = pcmcia_intr_establish(pf, IPL_SERIAL, comintr, sc);
292 if (psc->sc_ih == NULL) {
293 printf("%s: couldn't establish interrupt\n",
294 sc->sc_dev.dv_xname);
295 return (1);
296 }
297
298 if ((error = pcmcia_function_enable(pf)) != 0) {
299 pcmcia_intr_disestablish(pf, psc->sc_ih);
300 psc->sc_ih = 0;
301 return (error);
302 }
303
304 if ((psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) ||
305 (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556) ||
306 (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556INT)) {
307 int reg;
308
309 /* turn off the ethernet-disable bit */
310 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
311 if (reg & 0x08) {
312 reg &= ~0x08;
313 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
314 }
315 }
316
317 return (0);
318 }
319
320 void
321 com_pcmcia_disable(sc)
322 struct com_softc *sc;
323 {
324 struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
325
326 pcmcia_function_disable(psc->sc_pf);
327 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
328 psc->sc_ih = 0;
329 }
330