com_pcmcia.c revision 1.3 1 /* $NetBSD: com_pcmcia.c,v 1.3 1998/02/01 23:50:52 marc Exp $ */
2
3 /*-
4 * Copyright (c) 1993, 1994, 1995, 1996
5 * Charles M. Hannum. All rights reserved.
6 * Copyright (c) 1991 The Regents of the University of California.
7 * 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 the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)com.c 7.5 (Berkeley) 5/16/91
38 */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/ioctl.h>
43 #include <sys/select.h>
44 #include <sys/tty.h>
45 #include <sys/proc.h>
46 #include <sys/user.h>
47 #include <sys/conf.h>
48 #include <sys/file.h>
49 #include <sys/uio.h>
50 #include <sys/kernel.h>
51 #include <sys/syslog.h>
52 #include <sys/types.h>
53 #include <sys/device.h>
54
55 #include <machine/intr.h>
56 #include <machine/bus.h>
57
58 #include <dev/pcmcia/pcmciavar.h>
59 #include <dev/pcmcia/pcmciareg.h>
60
61 #include <dev/ic/comreg.h>
62 #include <dev/ic/comvar.h>
63
64 #include <dev/isa/isareg.h>
65
66 #define PCMCIA_MANUFACTURER_3COM 0x101
67 #define PCMCIA_PRODUCT_3COM_3C562 0x562
68
69 #define PCMCIA_MANUFACTURER_MOTOROLA 0x109
70 #define PCMCIA_PRODUCT_MOTOROLA_POWER144 0x105
71
72 #define PCMCIA_MANUFACTURER_IBM 0xa4
73 #define PCMCIA_PRODUCT_IBM_HOME_AND_AWAY 0x2e
74
75 #ifdef __BROKEN_INDIRECT_CONFIG
76 int com_pcmcia_match __P((struct device *, void *, void *));
77 #else
78 int com_pcmcia_match __P((struct device *, struct cfdata *, void *));
79 #endif
80 void com_pcmcia_attach __P((struct device *, struct device *, void *));
81 void com_pcmcia_cleanup __P((void *));
82
83 int com_pcmcia_enable __P((struct com_softc *));
84 void com_pcmcia_disable __P((struct com_softc *));
85
86 struct com_pcmcia_softc {
87 struct com_softc sc_com; /* real "com" softc */
88
89 /* PCMCIA-specific goo */
90 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
91 int sc_io_window; /* our i/o window */
92 struct pcmcia_function *sc_pf; /* our PCMCIA function */
93 void *sc_ih; /* interrupt handler */
94 };
95
96 struct cfattach com_pcmcia_ca = {
97 sizeof(struct com_pcmcia_softc), com_pcmcia_match, com_pcmcia_attach
98 };
99
100 struct com_dev {
101 char *name;
102 int manufacturer;
103 int product;
104 char *cis1_info0;
105 char *cis1_info1;
106 int function;
107 } com_devs[] = {
108 { "3Com 3C562 Modem",
109 PCMCIA_MANUFACTURER_3COM, PCMCIA_PRODUCT_3COM_3C562,
110 NULL, NULL, 1 },
111 { "Motorola Power 14.4 Modem",
112 PCMCIA_MANUFACTURER_MOTOROLA, PCMCIA_PRODUCT_MOTOROLA_POWER144,
113 NULL, NULL, 0 },
114 { "IBM Home and Away Modem",
115 PCMCIA_MANUFACTURER_IBM, PCMCIA_PRODUCT_IBM_HOME_AND_AWAY,
116 NULL, NULL, 0 },
117 { "Megahertz XJ2288 Modem",
118 0xffffffff, 0xffff, "MEGAHERTZ", "XJ2288", 0 },
119 };
120
121 #define com_dev_match(card, fct, n) \
122 (((((com_devs[(n)].cis1_info0 == NULL) && \
123 (com_devs[(n)].cis1_info1 == NULL) && \
124 (com_devs[(n)].manufacturer == (card)->manufacturer) && \
125 (com_devs[(n)].product == (card)->product)) || \
126 ((com_devs[(n)].cis1_info0) && \
127 (com_devs[(n)].cis1_info1) && \
128 (strcmp(com_devs[(n)].cis1_info0, (card)->cis1_info[0]) == 0) && \
129 (strcmp(com_devs[(n)].cis1_info1, (card)->cis1_info[1]) == 0))) && \
130 (com_devs[(n)].function == fct)) \
131 ?&com_devs[(n)]:NULL)
132
133 int
134 com_pcmcia_match(parent, match, aux)
135 struct device *parent;
136 #ifdef __BROKEN_INDIRECT_CONFIG
137 void *match;
138 #else
139 struct cfdata *match;
140 #endif
141 void *aux;
142 {
143 struct pcmcia_attach_args *pa = aux;
144 struct pcmcia_config_entry *cfe;
145 int i;
146
147 for (i=0; i<(sizeof(com_devs)/sizeof(com_devs[0])); i++) {
148 if (com_dev_match(pa->card, pa->pf->number, i))
149 return(1);
150 }
151
152 /* find a cfe we can use (if it matches a standard COM port) */
153
154 for (cfe = pa->pf->cfe_head.sqh_first; cfe;
155 cfe = cfe->cfe_list.sqe_next) {
156 if (cfe->iospace[0].start == IO_COM1 ||
157 cfe->iospace[0].start == IO_COM2 ||
158 cfe->iospace[0].start == IO_COM3 ||
159 cfe->iospace[0].start == IO_COM4)
160 return(1);
161 }
162
163 return(0);
164 }
165
166 void
167 com_pcmcia_attach(parent, self, aux)
168 struct device *parent, *self;
169 void *aux;
170 {
171 struct com_pcmcia_softc *psc = (void *) self;
172 struct com_softc *sc = &psc->sc_com;
173 struct pcmcia_attach_args *pa = aux;
174 struct pcmcia_config_entry *cfe;
175 int i;
176 struct com_dev *comdev;
177
178 psc->sc_pf = pa->pf;
179
180 /* find a cfe we can use */
181
182 for (cfe = pa->pf->cfe_head.sqh_first; cfe;
183 cfe = cfe->cfe_list.sqe_next) {
184 if (cfe->num_memspace != 0)
185 continue;
186
187 if (cfe->num_iospace != 1)
188 continue;
189
190 if (cfe->iomask == 3) {
191 if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
192 cfe->iospace[0].length, &psc->sc_pcioh)) {
193 continue;
194 }
195 } else {
196 if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
197 cfe->iospace[0].length, 0, &psc->sc_pcioh)) {
198 continue;
199 }
200 }
201
202 break;
203 }
204
205 if (!cfe) {
206 printf(": can't allocate i/o space\n");
207 return;
208 }
209
210 sc->sc_iot = psc->sc_pcioh.iot;
211 sc->sc_ioh = psc->sc_pcioh.ioh;
212
213 /* Enable the card. */
214 pcmcia_function_init(pa->pf, cfe);
215 if (com_pcmcia_enable(sc))
216 printf(": function enable failed\n");
217
218 sc->enabled = 1;
219
220 /* map in the io space */
221
222 if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
223 PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, psc->sc_pcioh.size,
224 &psc->sc_pcioh, &psc->sc_io_window)) {
225 printf(": can't map i/o space\n");
226 return;
227 }
228
229 sc->sc_iobase = -1;
230 sc->sc_frequency = COM_FREQ;
231
232 sc->enable = com_pcmcia_enable;
233 sc->disable = com_pcmcia_disable;
234
235 com_attach_subr(sc);
236
237 for (i=0; i<(sizeof(com_devs)/sizeof(com_devs[0])); i++) {
238 if ((comdev = com_dev_match(pa->card, pa->pf->number, i))) {
239 printf(": %s\n", comdev->name);
240 }
241 }
242
243 sc->enabled = 0;
244
245 com_pcmcia_disable(sc);
246 }
247
248 int com_pcmcia_enable(struct com_softc *sc)
249 {
250 struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
251 struct pcmcia_function *pf = psc->sc_pf;
252 int ret;
253
254 /* establish the interrupt. */
255 psc->sc_ih = pcmcia_intr_establish(pf, IPL_SERIAL, comintr, sc);
256 if (psc->sc_ih == NULL) {
257 printf("%s: couldn't establish interrupt\n",
258 sc->sc_dev.dv_xname);
259 return (1);
260 }
261
262 if ((ret = pcmcia_function_enable(pf)))
263 return(ret);
264
265 if (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) {
266 int reg;
267
268 /* turn off the ethernet-disable bit */
269
270 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
271 if (reg & 0x08) {
272 reg &= ~0x08;
273 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
274 }
275 }
276
277 return(ret);
278 }
279
280 void com_pcmcia_disable(struct com_softc *sc)
281 {
282 struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
283
284 pcmcia_function_disable(psc->sc_pf);
285
286 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
287 }
288