com_mca.c revision 1.8 1 /* $NetBSD: com_mca.c,v 1.8 2002/09/30 21:36:45 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2001 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. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)com.c 7.5 (Berkeley) 5/16/91
72 */
73
74 /*
75 * This driver attaches serial port boards and internal modems.
76 */
77
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: com_mca.c,v 1.8 2002/09/30 21:36:45 thorpej Exp $");
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/ioctl.h>
84 #include <sys/select.h>
85 #include <sys/tty.h>
86 #include <sys/proc.h>
87 #include <sys/user.h>
88 #include <sys/file.h>
89 #include <sys/uio.h>
90 #include <sys/kernel.h>
91 #include <sys/syslog.h>
92 #include <sys/device.h>
93
94 #include <machine/intr.h>
95 #include <machine/bus.h>
96
97 #include <dev/ic/comreg.h>
98 #include <dev/ic/comvar.h>
99
100 #include <dev/mca/mcavar.h>
101 #include <dev/mca/mcadevs.h>
102
103 struct com_mca_softc {
104 struct com_softc sc_com; /* real "com" softc */
105
106 /* MCA-specific goo. */
107 void *sc_ih; /* interrupt handler */
108 };
109
110 int com_mca_probe __P((struct device *, struct cfdata *, void *));
111 void com_mca_attach __P((struct device *, struct device *, void *));
112 void com_mca_cleanup __P((void *));
113
114 static int ibm_modem_getcfg __P((struct mca_attach_args *, int *, int *));
115 static int neocom1_getcfg __P((struct mca_attach_args *, int *, int *));
116 static int ibm_mpcom_getcfg __P((struct mca_attach_args *, int *, int *));
117
118 CFATTACH_DECL(com_mca, sizeof(struct com_mca_softc),
119 com_mca_probe, com_mca_attach, NULL, NULL)
120
121 static const struct com_mca_product {
122 u_int32_t cp_prodid; /* MCA product ID */
123 const char *cp_name; /* device name */
124 int (*cp_getcfg) __P((struct mca_attach_args *, int *iobase, int *irq));
125 /* get device i/o base and irq */
126 } com_mca_products[] = {
127 { MCA_PRODUCT_IBM_MOD, "IBM Internal Modem", ibm_modem_getcfg },
128 { MCA_PRODUCT_NEOCOM1, "NeoTecH Single RS-232 Async. Adapter, SM110",
129 neocom1_getcfg },
130 { MCA_PRODUCT_IBM_MPCOM,"IBM Multi-Protocol Communications Adapter",
131 ibm_mpcom_getcfg },
132 { 0, NULL, NULL },
133 };
134
135 static const struct com_mca_product *com_mca_lookup __P((int));
136
137 static const struct com_mca_product *
138 com_mca_lookup(ma_id)
139 int ma_id;
140 {
141 const struct com_mca_product *cpp;
142
143 for (cpp = com_mca_products; cpp->cp_name != NULL; cpp++)
144 if (cpp->cp_prodid == ma_id)
145 return (cpp);
146
147 return (NULL);
148 }
149
150 int
151 com_mca_probe(parent, match, aux)
152 struct device *parent;
153 struct cfdata *match;
154 void *aux;
155 {
156 struct mca_attach_args *ma = aux;
157
158 if (com_mca_lookup(ma->ma_id))
159 return (1);
160
161 return (0);
162 }
163
164 void
165 com_mca_attach(parent, self, aux)
166 struct device *parent, *self;
167 void *aux;
168 {
169 struct com_mca_softc *isc = (void *)self;
170 struct com_softc *sc = &isc->sc_com;
171 int iobase, irq;
172 struct mca_attach_args *ma = aux;
173 const struct com_mca_product *cpp;
174
175 cpp = com_mca_lookup(ma->ma_id);
176
177 /* get iobase and irq */
178 if ((*cpp->cp_getcfg)(ma, &iobase, &irq))
179 return;
180
181 if (bus_space_map(ma->ma_iot, iobase, COM_NPORTS, 0, &sc->sc_ioh)) {
182 printf(": can't map i/o space\n");
183 return;
184 }
185
186 sc->sc_frequency = COM_FREQ;
187 sc->sc_iot = ma->ma_iot;
188 sc->sc_iobase = iobase;
189
190 printf(" slot %d i/o %#x-%#x irq %d", ma->ma_slot + 1,
191 iobase, iobase + COM_NPORTS - 1, irq);
192
193 com_attach_subr(sc);
194
195 printf("%s: %s\n", sc->sc_dev.dv_xname, cpp->cp_name);
196
197 isc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_SERIAL,
198 comintr, sc);
199 if (isc->sc_ih == NULL) {
200 printf("%s: couldn't establish interrupt handler\n",
201 sc->sc_dev.dv_xname);
202 return;
203 }
204
205 /*
206 * Shutdown hook for buggy BIOSs that don't recognize the UART
207 * without a disabled FIFO.
208 * XXX is this necessary on MCA ? --- jdolecek
209 */
210 if (shutdownhook_establish(com_mca_cleanup, sc) == NULL)
211 panic("com_mca_attach: could not establish shutdown hook");
212 }
213
214 void
215 com_mca_cleanup(arg)
216 void *arg;
217 {
218 struct com_softc *sc = arg;
219
220 if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
221 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_fifo, 0);
222 }
223
224 /* map serial_X to iobase and irq */
225 static const struct {
226 int iobase;
227 int irq;
228 } MCA_SERIAL[] = {
229 { 0x03f8, 4 }, /* SERIAL_1 */
230 { 0x02f8, 3 }, /* SERIAL_2 */
231 { 0x3220, 3 }, /* SERIAL_3 */
232 { 0x3228, 3 }, /* SERIAL_4 */
233 { 0x4220, 3 }, /* SERIAL_5 */
234 { 0x4228, 3 }, /* SERIAL_6 */
235 { 0x5220, 3 }, /* SERIAL_7 */
236 { 0x5228, 3 }, /* SERIAL_8 */
237 };
238
239 /*
240 * Get config for IBM Internal Modem (ID 0xEDFF). This beast doesn't
241 * seem to support even AT commands, it's good as example for adding
242 * other stuff though.
243 */
244 static int
245 ibm_modem_getcfg(ma, iobasep, irqp)
246 struct mca_attach_args *ma;
247 int *iobasep, *irqp;
248 {
249 int pos2;
250 int snum;
251
252 pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
253
254 /*
255 * POS register 2: (adf pos0)
256 * 7 6 5 4 3 2 1 0
257 * \__/ \__ enable: 0=adapter disabled, 1=adapter enabled
258 * \_____ Serial Configuration: XX=SERIAL_XX
259 */
260
261 snum = (pos2 & 0x0e) >> 1;
262
263 *iobasep = MCA_SERIAL[snum].iobase;
264 *irqp = MCA_SERIAL[snum].irq;
265
266 return (0);
267 }
268
269 /*
270 * Get configuration for NeoTecH Single RS-232 Async. Adapter, SM110.
271 */
272 static int
273 neocom1_getcfg(ma, iobasep, irqp)
274 struct mca_attach_args *ma;
275 int *iobasep, *irqp;
276 {
277 int pos2, pos3, pos4;
278 static const int neotech_irq[] = { 12, 9, 4, 3 };
279
280 pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
281 pos3 = mca_conf_read(ma->ma_mc, ma->ma_slot, 3);
282 pos4 = mca_conf_read(ma->ma_mc, ma->ma_slot, 4);
283
284 /*
285 * POS register 2: (adf pos0)
286 * 7 6 5 4 3 2 1 0
287 * 1 \_/ \__ enable: 0=adapter disabled, 1=adapter enabled
288 * \____ IRQ: 11=3 10=4 01=9 00=12
289 *
290 * POS register 3: (adf pos1)
291 * 7 6 5 4 3 2 1 0
292 * \______/
293 * \_________ I/O Address: bits 7-3
294 *
295 * POS register 4: (adf pos2)
296 * 7 6 5 4 3 2 1 0
297 * \_____________/
298 * \__ I/O Address: bits 15-8
299 */
300
301 *iobasep = (pos4 << 8) | (pos3 & 0xf8);
302 *irqp = neotech_irq[(pos2 & 0x06) >> 1];
303
304 return (0);
305 }
306
307 /*
308 * Get configuration for IBM Multi-Protocol Communications Adapter.
309 * We only support SERIAL mode, bail out if set to SDLC or BISYNC.
310 */
311 static int
312 ibm_mpcom_getcfg(ma, iobasep, irqp)
313 struct mca_attach_args *ma;
314 int *iobasep, *irqp;
315 {
316 int snum, pos2;
317
318 pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
319
320 /*
321 * For SERIAL mode, bit 4 has to be 0.
322 *
323 * POS register 2: (adf pos0)
324 * 7 6 5 4 3 2 1 0
325 * 0 \__/ \__ enable: 0=adapter disabled, 1=adapter enabled
326 * \_____ Serial Configuration: XX=SERIAL_XX
327 */
328
329 if (pos2 & 0x10) {
330 printf(": not set to SERIAL mode, ignored\n");
331 return (1);
332 }
333
334 snum = (pos2 & 0x0e) >> 1;
335
336 *iobasep = MCA_SERIAL[snum].iobase;
337 *irqp = MCA_SERIAL[snum].irq;
338
339 return (0);
340 }
341