xref: /netbsd-src/sys/dev/mca/com_mca.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: com_mca.c,v 1.10 2003/08/07 16:31:08 agc 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. 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 /*
71  * This driver attaches serial port boards and internal modems.
72  */
73 
74 #include <sys/cdefs.h>
75 __KERNEL_RCSID(0, "$NetBSD: com_mca.c,v 1.10 2003/08/07 16:31:08 agc Exp $");
76 
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/ioctl.h>
80 #include <sys/select.h>
81 #include <sys/tty.h>
82 #include <sys/proc.h>
83 #include <sys/user.h>
84 #include <sys/file.h>
85 #include <sys/uio.h>
86 #include <sys/kernel.h>
87 #include <sys/syslog.h>
88 #include <sys/device.h>
89 
90 #include <machine/intr.h>
91 #include <machine/bus.h>
92 
93 #include <dev/ic/comreg.h>
94 #include <dev/ic/comvar.h>
95 
96 #include <dev/mca/mcavar.h>
97 #include <dev/mca/mcadevs.h>
98 
99 struct com_mca_softc {
100 	struct	com_softc sc_com;	/* real "com" softc */
101 
102 	/* MCA-specific goo. */
103 	void	*sc_ih;			/* interrupt handler */
104 };
105 
106 int com_mca_probe __P((struct device *, struct cfdata *, void *));
107 void com_mca_attach __P((struct device *, struct device *, void *));
108 void com_mca_cleanup __P((void *));
109 
110 static int ibm_modem_getcfg __P((struct mca_attach_args *, int *, int *));
111 static int neocom1_getcfg __P((struct mca_attach_args *, int *, int *));
112 static int ibm_mpcom_getcfg __P((struct mca_attach_args *, int *, int *));
113 
114 CFATTACH_DECL(com_mca, sizeof(struct com_mca_softc),
115     com_mca_probe, com_mca_attach, NULL, NULL);
116 
117 static const struct com_mca_product {
118 	u_int32_t	cp_prodid;	/* MCA product ID */
119 	const char	*cp_name;	/* device name */
120 	int (*cp_getcfg) __P((struct mca_attach_args *, int *iobase, int *irq));
121 					/* get device i/o base and irq */
122 } com_mca_products[] = {
123 	{ MCA_PRODUCT_IBM_MOD,	"IBM Internal Modem",	ibm_modem_getcfg },
124 	{ MCA_PRODUCT_NEOCOM1,	"NeoTecH Single RS-232 Async. Adapter, SM110",
125 		neocom1_getcfg },
126 	{ MCA_PRODUCT_IBM_MPCOM,"IBM Multi-Protocol Communications Adapter",
127 		ibm_mpcom_getcfg },
128 	{ 0,			NULL,			NULL },
129 };
130 
131 static const struct com_mca_product *com_mca_lookup __P((int));
132 
133 static const struct com_mca_product *
134 com_mca_lookup(ma_id)
135 	int ma_id;
136 {
137 	const struct com_mca_product *cpp;
138 
139 	for (cpp = com_mca_products; cpp->cp_name != NULL; cpp++)
140 		if (cpp->cp_prodid == ma_id)
141 			return (cpp);
142 
143 	return (NULL);
144 }
145 
146 int
147 com_mca_probe(parent, match, aux)
148 	struct device *parent;
149 	struct cfdata *match;
150 	void *aux;
151 {
152 	struct mca_attach_args *ma = aux;
153 
154 	if (com_mca_lookup(ma->ma_id))
155 		return (1);
156 
157 	return (0);
158 }
159 
160 void
161 com_mca_attach(parent, self, aux)
162 	struct device *parent, *self;
163 	void *aux;
164 {
165 	struct com_mca_softc *isc = (void *)self;
166 	struct com_softc *sc = &isc->sc_com;
167 	int iobase, irq;
168 	struct mca_attach_args *ma = aux;
169 	const struct com_mca_product *cpp;
170 
171 	cpp = com_mca_lookup(ma->ma_id);
172 
173 	/* get iobase and irq */
174 	if ((*cpp->cp_getcfg)(ma, &iobase, &irq))
175 		return;
176 
177 	if (bus_space_map(ma->ma_iot, iobase, COM_NPORTS, 0, &sc->sc_ioh)) {
178 		printf(": can't map i/o space\n");
179 		return;
180 	}
181 
182 	sc->sc_frequency = COM_FREQ;
183 	sc->sc_iot = ma->ma_iot;
184 	sc->sc_iobase = iobase;
185 
186 	printf(" slot %d i/o %#x-%#x irq %d", ma->ma_slot + 1,
187 		iobase, iobase + COM_NPORTS - 1, irq);
188 
189 	com_attach_subr(sc);
190 
191 	printf("%s: %s\n", sc->sc_dev.dv_xname, cpp->cp_name);
192 
193 	isc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_SERIAL,
194 			comintr, sc);
195 	if (isc->sc_ih == NULL) {
196                 printf("%s: couldn't establish interrupt handler\n",
197                     sc->sc_dev.dv_xname);
198                 return;
199         }
200 
201 	/*
202 	 * Shutdown hook for buggy BIOSs that don't recognize the UART
203 	 * without a disabled FIFO.
204 	 * XXX is this necessary on MCA ? --- jdolecek
205 	 */
206 	if (shutdownhook_establish(com_mca_cleanup, sc) == NULL)
207 		panic("com_mca_attach: could not establish shutdown hook");
208 }
209 
210 void
211 com_mca_cleanup(arg)
212 	void *arg;
213 {
214 	struct com_softc *sc = arg;
215 
216 	if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
217 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_fifo, 0);
218 }
219 
220 /* map serial_X to iobase and irq */
221 static const struct {
222 	int iobase;
223 	int irq;
224 } MCA_SERIAL[] = {
225 	{ 0x03f8,	4 },	/* SERIAL_1 */
226 	{ 0x02f8,	3 },	/* SERIAL_2 */
227 	{ 0x3220,	3 },	/* SERIAL_3 */
228 	{ 0x3228,	3 },	/* SERIAL_4 */
229 	{ 0x4220,	3 },	/* SERIAL_5 */
230 	{ 0x4228,	3 },	/* SERIAL_6 */
231 	{ 0x5220,	3 },	/* SERIAL_7 */
232 	{ 0x5228,	3 },	/* SERIAL_8 */
233 };
234 
235 /*
236  * Get config for IBM Internal Modem (ID 0xEDFF). This beast doesn't
237  * seem to support even AT commands, it's good as example for adding
238  * other stuff though.
239  */
240 static int
241 ibm_modem_getcfg(ma, iobasep, irqp)
242 	struct mca_attach_args *ma;
243 	int *iobasep, *irqp;
244 {
245 	int pos2;
246 	int snum;
247 
248 	pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
249 
250 	/*
251 	 * POS register 2: (adf pos0)
252 	 * 7 6 5 4 3 2 1 0
253 	 *         \__/  \__ enable: 0=adapter disabled, 1=adapter enabled
254 	 *            \_____ Serial Configuration: XX=SERIAL_XX
255 	 */
256 
257 	snum = (pos2 & 0x0e) >> 1;
258 
259 	*iobasep = MCA_SERIAL[snum].iobase;
260 	*irqp = MCA_SERIAL[snum].irq;
261 
262 	return (0);
263 }
264 
265 /*
266  * Get configuration for NeoTecH Single RS-232 Async. Adapter, SM110.
267  */
268 static int
269 neocom1_getcfg(ma, iobasep, irqp)
270 	struct mca_attach_args *ma;
271 	int *iobasep, *irqp;
272 {
273 	int pos2, pos3, pos4;
274 	static const int neotech_irq[] = { 12, 9, 4, 3 };
275 
276 	pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
277 	pos3 = mca_conf_read(ma->ma_mc, ma->ma_slot, 3);
278 	pos4 = mca_conf_read(ma->ma_mc, ma->ma_slot, 4);
279 
280 	/*
281 	 * POS register 2: (adf pos0)
282 	 * 7 6 5 4 3 2 1 0
283 	 *     1     \_/ \__ enable: 0=adapter disabled, 1=adapter enabled
284 	 *             \____ IRQ: 11=3 10=4 01=9 00=12
285 	 *
286 	 * POS register 3: (adf pos1)
287 	 * 7 6 5 4 3 2 1 0
288 	 * \______/
289 	 *        \_________ I/O Address: bits 7-3
290 	 *
291 	 * POS register 4: (adf pos2)
292 	 * 7 6 5 4 3 2 1 0
293 	 * \_____________/
294 	 *               \__ I/O Address: bits 15-8
295 	 */
296 
297 	*iobasep = (pos4 << 8) | (pos3 & 0xf8);
298 	*irqp = neotech_irq[(pos2 & 0x06) >> 1];
299 
300 	return (0);
301 }
302 
303 /*
304  * Get configuration for IBM Multi-Protocol Communications Adapter.
305  * We only support SERIAL mode, bail out if set to SDLC or BISYNC.
306  */
307 static int
308 ibm_mpcom_getcfg(ma, iobasep, irqp)
309 	struct mca_attach_args *ma;
310 	int *iobasep, *irqp;
311 {
312 	int snum, pos2;
313 
314 	pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
315 
316 	/*
317 	 * For SERIAL mode, bit 4 has to be 0.
318 	 *
319 	 * POS register 2: (adf pos0)
320 	 * 7 6 5 4 3 2 1 0
321 	 *       0 \__/  \__ enable: 0=adapter disabled, 1=adapter enabled
322 	 *            \_____ Serial Configuration: XX=SERIAL_XX
323 	 */
324 
325 	if (pos2 & 0x10) {
326 		printf(": not set to SERIAL mode, ignored\n");
327 		return (1);
328 	}
329 
330 	snum = (pos2 & 0x0e) >> 1;
331 
332 	*iobasep = MCA_SERIAL[snum].iobase;
333 	*irqp = MCA_SERIAL[snum].irq;
334 
335 	return (0);
336 }
337