xref: /netbsd/sys/arch/sparc/dev/com_obio.c (revision bf9ec67e)
1 /*	$NetBSD: com_obio.c,v 1.9 2002/03/11 16:27:01 pk Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 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 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/ioctl.h>
77 #include <sys/select.h>
78 #include <sys/tty.h>
79 #include <sys/proc.h>
80 #include <sys/user.h>
81 #include <sys/conf.h>
82 #include <sys/file.h>
83 #include <sys/uio.h>
84 #include <sys/kernel.h>
85 #include <sys/syslog.h>
86 #include <sys/types.h>
87 #include <sys/device.h>
88 #include <sys/termios.h>
89 
90 #include <machine/bus.h>
91 #include <machine/autoconf.h>
92 #include <machine/intr.h>
93 
94 #include <dev/ic/comreg.h>
95 #include <dev/ic/comvar.h>
96 
97 #include <sparc/sparc/auxreg.h>
98 
99 struct com_obio_softc {
100 	struct com_softc osc_com;	/* real "com" softc */
101 
102 	int osc_tadpole;		/* is this on a tadpole */
103 	/* OBIO-specific goo. */
104 	struct evcnt osc_intrcnt;	/* interrupt counting */
105 };
106 
107 static int com_obio_match __P((struct device *, struct cfdata *, void *));
108 static void com_obio_attach __P((struct device *, struct device *, void *));
109 static void com_obio_cleanup __P((void *));
110 
111 struct cfattach com_obio_ca = {
112 	sizeof(struct com_obio_softc), com_obio_match, com_obio_attach
113 };
114 
115 static int
116 com_obio_match(parent, cf, aux)
117 	struct device *parent;
118 	struct cfdata *cf;
119 	void *aux;
120 {
121 	union obio_attach_args *uoba = aux;
122 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
123 	int tadpole = 0;
124 	int need_probe = 0;
125 	int rv = 0;
126 	u_int8_t auxregval;
127 
128 	if (uoba->uoba_isobio4 != 0) {
129 		return (0);
130 	}
131 
132 	/* Tadpole 3GX/3GS uses "modem" for a 16450 port
133 	 * (We need to enable it before probing)
134 	 */
135 	if (strcmp("modem", sa->sa_name) == 0) {
136 		auxregval = *AUXIO4M_REG;
137 		*AUXIO4M_REG = auxregval | (AUXIO4M_LED|AUXIO4M_LTE);
138 		DELAY(100);
139 		tadpole = 1;
140 		need_probe = 1;
141 	}
142 
143 	/* Sun JavaStation 1 uses "su" for a 16550 port
144 	 */
145 	if (strcmp("su", sa->sa_name) == 0) {
146 		need_probe = 1;
147 	}
148 
149 	if (need_probe) {
150 		bus_space_handle_t ioh;
151 
152 		if (sbus_bus_map(sa->sa_bustag,
153 				 sa->sa_slot, sa->sa_offset, sa->sa_size,
154 				 BUS_SPACE_MAP_LINEAR, &ioh) == 0) {
155 			rv = comprobe1(sa->sa_bustag, ioh);
156 #if 0
157 			printf("modem: probe: lcr=0x%02x iir=0x%02x\n",
158 				bus_space_read_1(sa->sa_bustag, ioh, 3),
159 				bus_space_read_1(sa->sa_bustag, ioh, 2));
160 #endif
161 			bus_space_unmap(sa->sa_bustag, ioh, sa->sa_size);
162 		}
163 	}
164 
165 	/* Disable the com port if tadpole */
166 	if (tadpole)
167 		*AUXIO4M_REG = auxregval;
168 
169 	return (rv);
170 }
171 
172 static void
173 com_obio_attach(parent, self, aux)
174 	struct device *parent, *self;
175 	void *aux;
176 {
177 	struct com_obio_softc *osc = (void *)self;
178 	struct com_softc *sc = &osc->osc_com;
179 	union obio_attach_args *uoba = aux;
180 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
181 
182 	if (strcmp("modem", sa->sa_name) == 0) {
183 		osc->osc_tadpole = 1;
184 	}
185 
186 	/*
187 	 * We're living on an obio that looks like an sbus slot.
188 	 */
189 	sc->sc_iot = sa->sa_bustag;
190 	sc->sc_iobase = sa->sa_offset;
191 	sc->sc_frequency = COM_FREQ;
192 
193 	/*
194 	 * XXX: It would be nice to be able to split console input and
195 	 * output to different devices.  For now switch to serial
196 	 * console if PROM stdin is on serial (so that we can use DDB).
197 	 */
198 	if (prom_instance_to_package(prom_stdin()) == sa->sa_node)
199 		comcnattach(sc->sc_iot, sc->sc_iobase,
200 			    B9600, sc->sc_frequency, (CLOCAL | CREAD | CS8));
201 
202 	if (!com_is_console(sc->sc_iot, sc->sc_iobase, &sc->sc_ioh) &&
203 	    sbus_bus_map(sc->sc_iot,
204 			 sa->sa_slot, sc->sc_iobase, sa->sa_size,
205 			 BUS_SPACE_MAP_LINEAR, &sc->sc_ioh) != 0) {
206 		printf(": can't map registers\n");
207 		return;
208 	}
209 
210 	if (osc->osc_tadpole) {
211 		*AUXIO4M_REG |= (AUXIO4M_LED|AUXIO4M_LTE);
212 		do {
213 			DELAY(100);
214 		} while (!comprobe1(sc->sc_iot, sc->sc_ioh));
215 #if 0
216 		printf("modem: attach: lcr=0x%02x iir=0x%02x\n",
217 			bus_space_read_1(sc->sc_iot, sc->sc_ioh, 3),
218 			bus_space_read_1(sc->sc_iot, sc->sc_ioh, 2));
219 #endif
220 	}
221 
222 	com_attach_subr(sc);
223 
224 	if (sa->sa_nintr != 0) {
225 		(void)bus_intr_establish(sc->sc_iot, sa->sa_pri, IPL_SERIAL,
226 					 0, comintr, sc);
227 		evcnt_attach_dynamic(&osc->osc_intrcnt, EVCNT_TYPE_INTR, NULL,
228 		    osc->osc_com.sc_dev.dv_xname, "intr");
229 	}
230 
231 	/*
232 	 * Shutdown hook for buggy BIOSs that don't recognize the UART
233 	 * without a disabled FIFO.
234 	 */
235 	if (shutdownhook_establish(com_obio_cleanup, sc) == NULL) {
236 		panic("com_obio_attach: could not establish shutdown hook");
237 	}
238 }
239 
240 static void
241 com_obio_cleanup(arg)
242 	void *arg;
243 {
244 	struct com_softc *sc = arg;
245 
246 	if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
247 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_fifo, 0);
248 }
249