xref: /netbsd-src/sys/arch/sparc/dev/com_obio.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: com_obio.c,v 1.22 2008/04/28 20:23:35 martin 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*-
33  * Copyright (c) 1991 The Regents of the University of California.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)com.c	7.5 (Berkeley) 5/16/91
61  */
62 
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: com_obio.c,v 1.22 2008/04/28 20:23:35 martin Exp $");
65 
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/ioctl.h>
69 #include <sys/select.h>
70 #include <sys/tty.h>
71 #include <sys/proc.h>
72 #include <sys/user.h>
73 #include <sys/conf.h>
74 #include <sys/file.h>
75 #include <sys/uio.h>
76 #include <sys/kernel.h>
77 #include <sys/syslog.h>
78 #include <sys/types.h>
79 #include <sys/device.h>
80 #include <sys/termios.h>
81 
82 #include <machine/bus.h>
83 #include <machine/autoconf.h>
84 #include <machine/intr.h>
85 
86 #include <dev/ic/comreg.h>
87 #include <dev/ic/comvar.h>
88 
89 #include <sparc/sparc/auxreg.h>
90 
91 struct com_obio_softc {
92 	struct com_softc osc_com;	/* real "com" softc */
93 
94 	int osc_tadpole;		/* is this on a tadpole */
95 	/* OBIO-specific goo. */
96 	struct evcnt osc_intrcnt;	/* interrupt counting */
97 };
98 
99 static int com_obio_match(device_t, cfdata_t , void *);
100 static void com_obio_attach(device_t, device_t, void *);
101 
102 CFATTACH_DECL_NEW(com_obio, sizeof(struct com_obio_softc),
103     com_obio_match, com_obio_attach, NULL, NULL);
104 
105 static int
106 com_obio_match(device_t parent, cfdata_t cf, void *aux)
107 {
108 	union obio_attach_args *uoba = aux;
109 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
110 	int tadpole = 0;
111 	int need_probe = 0;
112 	int rv = 0;
113 	uint8_t auxregval = 0;
114 
115 	if (uoba->uoba_isobio4 != 0) {
116 		return (0);
117 	}
118 
119 	/*
120 	 * Tadpole 3GX/3GS uses "modem" for a 16450 port
121 	 * (We need to enable it before probing)
122 	 */
123 	if (strcmp("modem", sa->sa_name) == 0) {
124 		auxregval = *AUXIO4M_REG;
125 		*AUXIO4M_REG = auxregval | (AUXIO4M_LED|AUXIO4M_LTE);
126 		DELAY(100);
127 		tadpole = 1;
128 		need_probe = 1;
129 	}
130 
131 	/*
132 	 * Sun JavaStation 1 uses "su" for a 16550 port
133 	 */
134 	if (strcmp("su", sa->sa_name) == 0) {
135 		need_probe = 1;
136 	}
137 
138 	if (need_probe) {
139 		bus_space_handle_t ioh;
140 
141 		if (sbus_bus_map(sa->sa_bustag,
142 				 sa->sa_slot, sa->sa_offset, sa->sa_size,
143 				 BUS_SPACE_MAP_LINEAR, &ioh) == 0) {
144 			rv = comprobe1(sa->sa_bustag, ioh);
145 #if 0
146 			printf("modem: probe: lcr=0x%02x iir=0x%02x\n",
147 				bus_space_read_1(sa->sa_bustag, ioh, 3),
148 				bus_space_read_1(sa->sa_bustag, ioh, 2));
149 #endif
150 			bus_space_unmap(sa->sa_bustag, ioh, sa->sa_size);
151 		}
152 	}
153 
154 	/* Disable the com port if tadpole */
155 	if (tadpole)
156 		*AUXIO4M_REG = auxregval;
157 
158 	return (rv);
159 }
160 
161 static void
162 com_obio_attach(device_t parent, device_t self, void *aux)
163 {
164 	struct com_obio_softc *osc = device_private(self);
165 	struct com_softc *sc = &osc->osc_com;
166 	union obio_attach_args *uoba = aux;
167 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
168 	bus_space_handle_t ioh;
169 	bus_space_tag_t iot;
170 	bus_addr_t iobase;
171 
172 	sc->sc_dev = self;
173 
174 	if (strcmp("modem", sa->sa_name) == 0) {
175 		osc->osc_tadpole = 1;
176 	}
177 
178 	/*
179 	 * We're living on an obio that looks like an sbus slot.
180 	 */
181 	iot = sa->sa_bustag;
182 	iobase = sa->sa_offset;
183 	sc->sc_frequency = COM_FREQ;
184 
185 	/*
186 	 * XXX: It would be nice to be able to split console input and
187 	 * output to different devices.  For now switch to serial
188 	 * console if PROM stdin is on serial (so that we can use DDB).
189 	 */
190 	if (prom_instance_to_package(prom_stdin()) == sa->sa_node)
191 		comcnattach(iot, iobase, B9600, sc->sc_frequency,
192 		    COM_TYPE_NORMAL, (CLOCAL | CREAD | CS8));
193 
194 	if (!com_is_console(iot, iobase, &ioh) &&
195 	    sbus_bus_map(iot, sa->sa_slot, iobase, sa->sa_size,
196 			 BUS_SPACE_MAP_LINEAR, &ioh) != 0) {
197 		aprint_error(": can't map registers\n");
198 		return;
199 	}
200 
201 	COM_INIT_REGS(sc->sc_regs, iot, ioh, iobase);
202 
203 	if (osc->osc_tadpole) {
204 		*AUXIO4M_REG |= (AUXIO4M_LED|AUXIO4M_LTE);
205 		do {
206 			DELAY(100);
207 		} while (!com_probe_subr(&sc->sc_regs));
208 #if 0
209 		printf("modem: attach: lcr=0x%02x iir=0x%02x\n",
210 			bus_space_read_1(sc->sc_regs.iot, sc->sc_regs.ioh, 3),
211 			bus_space_read_1(sc->sc_regs.iot, sc->sc_regs.ioh, 2));
212 #endif
213 	}
214 
215 	com_attach_subr(sc);
216 
217 	if (sa->sa_nintr != 0) {
218 		(void)bus_intr_establish(sc->sc_regs.cr_iot, sa->sa_pri,
219 		    IPL_SERIAL, comintr, sc);
220 		evcnt_attach_dynamic(&osc->osc_intrcnt, EVCNT_TYPE_INTR, NULL,
221 		    device_xname(self), "intr");
222 	}
223 
224 	if (!pmf_device_register1(self, com_suspend, com_resume, com_cleanup)) {
225 		aprint_error_dev(self, "could not establish shutdown hook");
226 	}
227 }
228