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