xref: /netbsd-src/sys/dev/mca/com_mca.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: com_mca.c,v 1.20 2008/03/14 15:09:11 cube 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.20 2008/03/14 15:09:11 cube 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 <sys/intr.h>
91 #include <sys/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(device_t, cfdata_t , void *);
107 void com_mca_attach(device_t, device_t, void *);
108 
109 static int ibm_modem_getcfg(struct mca_attach_args *, int *, int *);
110 static int neocom1_getcfg(struct mca_attach_args *, int *, int *);
111 static int ibm_mpcom_getcfg(struct mca_attach_args *, int *, int *);
112 
113 CFATTACH_DECL_NEW(com_mca, sizeof(struct com_mca_softc),
114     com_mca_probe, com_mca_attach, NULL, NULL);
115 
116 static const struct com_mca_product {
117 	u_int32_t	cp_prodid;	/* MCA product ID */
118 	const char	*cp_name;	/* device name */
119 	int (*cp_getcfg)(struct mca_attach_args *, int *iobase, int *irq);
120 					/* get device i/o base and irq */
121 } com_mca_products[] = {
122 	{ MCA_PRODUCT_IBM_MOD,	"IBM Internal Modem",	ibm_modem_getcfg },
123 	{ MCA_PRODUCT_NEOCOM1,	"NeoTecH Single RS-232 Async. Adapter, SM110",
124 		neocom1_getcfg },
125 	{ MCA_PRODUCT_IBM_MPCOM,"IBM Multi-Protocol Communications Adapter",
126 		ibm_mpcom_getcfg },
127 	{ 0,			NULL,			NULL },
128 };
129 
130 static const struct com_mca_product *com_mca_lookup(int);
131 
132 static const struct com_mca_product *
133 com_mca_lookup(int ma_id)
134 {
135 	const struct com_mca_product *cpp;
136 
137 	for (cpp = com_mca_products; cpp->cp_name != NULL; cpp++)
138 		if (cpp->cp_prodid == ma_id)
139 			return (cpp);
140 
141 	return (NULL);
142 }
143 
144 int
145 com_mca_probe(device_t parent, cfdata_t match, void *aux)
146 {
147 	struct mca_attach_args *ma = aux;
148 
149 	if (com_mca_lookup(ma->ma_id))
150 		return (1);
151 
152 	return (0);
153 }
154 
155 void
156 com_mca_attach(device_t parent, device_t self, void *aux)
157 {
158 	struct com_mca_softc *isc = device_private(self);
159 	struct com_softc *sc = &isc->sc_com;
160 	int iobase, irq;
161 	struct mca_attach_args *ma = aux;
162 	const struct com_mca_product *cpp;
163 	bus_space_handle_t ioh;
164 
165 	sc->sc_dev = self;
166 	cpp = com_mca_lookup(ma->ma_id);
167 
168 	/* get iobase and irq */
169 	if ((*cpp->cp_getcfg)(ma, &iobase, &irq))
170 		return;
171 
172 	if (bus_space_map(ma->ma_iot, iobase, COM_NPORTS, 0, &ioh)) {
173 		aprint_error(": can't map i/o space\n");
174 		return;
175 	}
176 
177 	COM_INIT_REGS(sc->sc_regs, ma->ma_iot, ioh, iobase);
178 	sc->sc_frequency = COM_FREQ;
179 
180 	aprint_normal(" slot %d i/o %#x-%#x irq %d", ma->ma_slot + 1,
181 		iobase, iobase + COM_NPORTS - 1, irq);
182 
183 	com_attach_subr(sc);
184 
185 	aprint_normal_dev(self, "%s\n", cpp->cp_name);
186 
187 	isc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_SERIAL,
188 			comintr, sc);
189 	if (isc->sc_ih == NULL) {
190                 aprint_error_dev(self,
191 		    "couldn't establish interrupt handler\n");
192                 return;
193         }
194 
195 	/*
196 	 * com_cleanup: shutdown hook for buggy BIOSs that don't
197 	 * recognize the UART without a disabled FIFO.
198 	 * XXX is this necessary on MCA ? --- jdolecek
199 	 */
200 	if (!pmf_device_register1(self, com_suspend, com_resume, com_cleanup))
201 		aprint_error_dev(self, "could not establish shutdown hook\n");
202 }
203 
204 /* map serial_X to iobase and irq */
205 static const struct {
206 	int iobase;
207 	int irq;
208 } MCA_SERIAL[] = {
209 	{ 0x03f8,	4 },	/* SERIAL_1 */
210 	{ 0x02f8,	3 },	/* SERIAL_2 */
211 	{ 0x3220,	3 },	/* SERIAL_3 */
212 	{ 0x3228,	3 },	/* SERIAL_4 */
213 	{ 0x4220,	3 },	/* SERIAL_5 */
214 	{ 0x4228,	3 },	/* SERIAL_6 */
215 	{ 0x5220,	3 },	/* SERIAL_7 */
216 	{ 0x5228,	3 },	/* SERIAL_8 */
217 };
218 
219 /*
220  * Get config for IBM Internal Modem (ID 0xEDFF). This beast doesn't
221  * seem to support even AT commands, it's good as example for adding
222  * other stuff though.
223  */
224 static int
225 ibm_modem_getcfg(struct mca_attach_args *ma, int *iobasep, int *irqp)
226 {
227 	int pos2;
228 	int snum;
229 
230 	pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
231 
232 	/*
233 	 * POS register 2: (adf pos0)
234 	 * 7 6 5 4 3 2 1 0
235 	 *         \__/  \__ enable: 0=adapter disabled, 1=adapter enabled
236 	 *            \_____ Serial Configuration: XX=SERIAL_XX
237 	 */
238 
239 	snum = (pos2 & 0x0e) >> 1;
240 
241 	*iobasep = MCA_SERIAL[snum].iobase;
242 	*irqp = MCA_SERIAL[snum].irq;
243 
244 	return (0);
245 }
246 
247 /*
248  * Get configuration for NeoTecH Single RS-232 Async. Adapter, SM110.
249  */
250 static int
251 neocom1_getcfg(struct mca_attach_args *ma, int *iobasep, int *irqp)
252 {
253 	int pos2, pos3, pos4;
254 	static const int neotech_irq[] = { 12, 9, 4, 3 };
255 
256 	pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
257 	pos3 = mca_conf_read(ma->ma_mc, ma->ma_slot, 3);
258 	pos4 = mca_conf_read(ma->ma_mc, ma->ma_slot, 4);
259 
260 	/*
261 	 * POS register 2: (adf pos0)
262 	 * 7 6 5 4 3 2 1 0
263 	 *     1     \_/ \__ enable: 0=adapter disabled, 1=adapter enabled
264 	 *             \____ IRQ: 11=3 10=4 01=9 00=12
265 	 *
266 	 * POS register 3: (adf pos1)
267 	 * 7 6 5 4 3 2 1 0
268 	 * \______/
269 	 *        \_________ I/O Address: bits 7-3
270 	 *
271 	 * POS register 4: (adf pos2)
272 	 * 7 6 5 4 3 2 1 0
273 	 * \_____________/
274 	 *               \__ I/O Address: bits 15-8
275 	 */
276 
277 	*iobasep = (pos4 << 8) | (pos3 & 0xf8);
278 	*irqp = neotech_irq[(pos2 & 0x06) >> 1];
279 
280 	return (0);
281 }
282 
283 /*
284  * Get configuration for IBM Multi-Protocol Communications Adapter.
285  * We only support SERIAL mode, bail out if set to SDLC or BISYNC.
286  */
287 static int
288 ibm_mpcom_getcfg(struct mca_attach_args *ma, int *iobasep, int *irqp)
289 {
290 	int snum, pos2;
291 
292 	pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
293 
294 	/*
295 	 * For SERIAL mode, bit 4 has to be 0.
296 	 *
297 	 * POS register 2: (adf pos0)
298 	 * 7 6 5 4 3 2 1 0
299 	 *       0 \__/  \__ enable: 0=adapter disabled, 1=adapter enabled
300 	 *            \_____ Serial Configuration: XX=SERIAL_XX
301 	 */
302 
303 	if (pos2 & 0x10) {
304 		aprint_error(": not set to SERIAL mode, ignored\n");
305 		return (1);
306 	}
307 
308 	snum = (pos2 & 0x0e) >> 1;
309 
310 	*iobasep = MCA_SERIAL[snum].iobase;
311 	*irqp = MCA_SERIAL[snum].irq;
312 
313 	return (0);
314 }
315