xref: /netbsd-src/sys/arch/sparc/dev/com_obio.c (revision 6d4870476f69938cdd065da8ea9ad989cef459f4)
1*6d487047Sthorpej /*	$NetBSD: com_obio.c,v 1.25 2018/12/08 17:46:13 thorpej Exp $	*/
2d4b90560Smatt 
3d4b90560Smatt /*-
4d4b90560Smatt  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5d4b90560Smatt  * All rights reserved.
6d4b90560Smatt  *
7d4b90560Smatt  * This code is derived from software contributed to The NetBSD Foundation
8d4b90560Smatt  * by Charles M. Hannum.
9d4b90560Smatt  *
10d4b90560Smatt  * Redistribution and use in source and binary forms, with or without
11d4b90560Smatt  * modification, are permitted provided that the following conditions
12d4b90560Smatt  * are met:
13d4b90560Smatt  * 1. Redistributions of source code must retain the above copyright
14d4b90560Smatt  *    notice, this list of conditions and the following disclaimer.
15d4b90560Smatt  * 2. Redistributions in binary form must reproduce the above copyright
16d4b90560Smatt  *    notice, this list of conditions and the following disclaimer in the
17d4b90560Smatt  *    documentation and/or other materials provided with the distribution.
18d4b90560Smatt  *
19d4b90560Smatt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20d4b90560Smatt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21d4b90560Smatt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22d4b90560Smatt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23d4b90560Smatt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24d4b90560Smatt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25d4b90560Smatt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26d4b90560Smatt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27d4b90560Smatt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28d4b90560Smatt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29d4b90560Smatt  * POSSIBILITY OF SUCH DAMAGE.
30d4b90560Smatt  */
31d4b90560Smatt 
32d4b90560Smatt /*-
33d4b90560Smatt  * Copyright (c) 1991 The Regents of the University of California.
34d4b90560Smatt  * All rights reserved.
35d4b90560Smatt  *
36d4b90560Smatt  * Redistribution and use in source and binary forms, with or without
37d4b90560Smatt  * modification, are permitted provided that the following conditions
38d4b90560Smatt  * are met:
39d4b90560Smatt  * 1. Redistributions of source code must retain the above copyright
40d4b90560Smatt  *    notice, this list of conditions and the following disclaimer.
41d4b90560Smatt  * 2. Redistributions in binary form must reproduce the above copyright
42d4b90560Smatt  *    notice, this list of conditions and the following disclaimer in the
43d4b90560Smatt  *    documentation and/or other materials provided with the distribution.
44aad01611Sagc  * 3. Neither the name of the University nor the names of its contributors
45d4b90560Smatt  *    may be used to endorse or promote products derived from this software
46d4b90560Smatt  *    without specific prior written permission.
47d4b90560Smatt  *
48d4b90560Smatt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49d4b90560Smatt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50d4b90560Smatt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51d4b90560Smatt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52d4b90560Smatt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53d4b90560Smatt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54d4b90560Smatt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55d4b90560Smatt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56d4b90560Smatt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57d4b90560Smatt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58d4b90560Smatt  * SUCH DAMAGE.
59d4b90560Smatt  *
60d4b90560Smatt  *	@(#)com.c	7.5 (Berkeley) 5/16/91
61d4b90560Smatt  */
62d4b90560Smatt 
63a4183603Slukem #include <sys/cdefs.h>
64*6d487047Sthorpej __KERNEL_RCSID(0, "$NetBSD: com_obio.c,v 1.25 2018/12/08 17:46:13 thorpej Exp $");
65a4183603Slukem 
66d4b90560Smatt #include <sys/param.h>
67d4b90560Smatt #include <sys/systm.h>
68d4b90560Smatt #include <sys/ioctl.h>
69d4b90560Smatt #include <sys/select.h>
70d4b90560Smatt #include <sys/tty.h>
71d4b90560Smatt #include <sys/proc.h>
72d4b90560Smatt #include <sys/conf.h>
73d4b90560Smatt #include <sys/file.h>
74d4b90560Smatt #include <sys/uio.h>
75d4b90560Smatt #include <sys/kernel.h>
76d4b90560Smatt #include <sys/syslog.h>
77d4b90560Smatt #include <sys/types.h>
78d4b90560Smatt #include <sys/device.h>
7925d726e4Suwe #include <sys/termios.h>
80d4b90560Smatt 
81b6584574Sdyoung #include <sys/bus.h>
82d4b90560Smatt #include <machine/autoconf.h>
83406e0f77Spk #include <machine/intr.h>
84d4b90560Smatt 
85d4b90560Smatt #include <dev/ic/comreg.h>
86d4b90560Smatt #include <dev/ic/comvar.h>
87d4b90560Smatt 
8873ae3282Smatt #include <sparc/sparc/auxreg.h>
8973ae3282Smatt 
90d4b90560Smatt struct com_obio_softc {
91d4b90560Smatt 	struct com_softc osc_com;	/* real "com" softc */
92d4b90560Smatt 
931d97a430Smatt 	int osc_tadpole;		/* is this on a tadpole */
94d4b90560Smatt 	/* OBIO-specific goo. */
95d4b90560Smatt 	struct evcnt osc_intrcnt;	/* interrupt counting */
96d4b90560Smatt };
97d4b90560Smatt 
98607ead0eScube static int com_obio_match(device_t, cfdata_t , void *);
99607ead0eScube static void com_obio_attach(device_t, device_t, void *);
100d4b90560Smatt 
101607ead0eScube CFATTACH_DECL_NEW(com_obio, sizeof(struct com_obio_softc),
1024bf871a7Sthorpej     com_obio_match, com_obio_attach, NULL, NULL);
103d4b90560Smatt 
104d4b90560Smatt static int
com_obio_match(device_t parent,cfdata_t cf,void * aux)105607ead0eScube com_obio_match(device_t parent, cfdata_t cf, void *aux)
106d4b90560Smatt {
107d4b90560Smatt 	union obio_attach_args *uoba = aux;
108d4b90560Smatt 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
1091d97a430Smatt 	int tadpole = 0;
1101d97a430Smatt 	int need_probe = 0;
1111d97a430Smatt 	int rv = 0;
112cd23cf5cSchs 	uint8_t auxregval = 0;
113d4b90560Smatt 
114d4b90560Smatt 	if (uoba->uoba_isobio4 != 0) {
115d4b90560Smatt 		return (0);
116d4b90560Smatt 	}
117d4b90560Smatt 
118cd23cf5cSchs 	/*
119cd23cf5cSchs 	 * Tadpole 3GX/3GS uses "modem" for a 16450 port
1201d97a430Smatt 	 * (We need to enable it before probing)
121d4b90560Smatt 	 */
122d4b90560Smatt 	if (strcmp("modem", sa->sa_name) == 0) {
1231d97a430Smatt 		auxregval = *AUXIO4M_REG;
12473ae3282Smatt 		*AUXIO4M_REG = auxregval | (AUXIO4M_LED|AUXIO4M_LTE);
12573ae3282Smatt 		DELAY(100);
1261d97a430Smatt 		tadpole = 1;
1271d97a430Smatt 		need_probe = 1;
1281d97a430Smatt 	}
1291d97a430Smatt 
130cd23cf5cSchs 	/*
131cd23cf5cSchs 	 * Sun JavaStation 1 uses "su" for a 16550 port
1321d97a430Smatt 	 */
1331d97a430Smatt 	if (strcmp("su", sa->sa_name) == 0) {
1341d97a430Smatt 		need_probe = 1;
1351d97a430Smatt 	}
1361d97a430Smatt 
1371d97a430Smatt 	if (need_probe) {
1381d97a430Smatt 		bus_space_handle_t ioh;
1391d97a430Smatt 
1407e8becd6Spk 		if (sbus_bus_map(sa->sa_bustag,
1417e8becd6Spk 				 sa->sa_slot, sa->sa_offset, sa->sa_size,
1427e8becd6Spk 				 BUS_SPACE_MAP_LINEAR, &ioh) == 0) {
14342c15639Smatt 			rv = comprobe1(sa->sa_bustag, ioh);
14473ae3282Smatt #if 0
14573ae3282Smatt 			printf("modem: probe: lcr=0x%02x iir=0x%02x\n",
14673ae3282Smatt 				bus_space_read_1(sa->sa_bustag, ioh, 3),
14773ae3282Smatt 				bus_space_read_1(sa->sa_bustag, ioh, 2));
14873ae3282Smatt #endif
14973ae3282Smatt 			bus_space_unmap(sa->sa_bustag, ioh, sa->sa_size);
15042c15639Smatt 		}
151d4b90560Smatt 	}
1521d97a430Smatt 
1531d97a430Smatt 	/* Disable the com port if tadpole */
1541d97a430Smatt 	if (tadpole)
1551d97a430Smatt 		*AUXIO4M_REG = auxregval;
1561d97a430Smatt 
1571d97a430Smatt 	return (rv);
158d4b90560Smatt }
159d4b90560Smatt 
160d4b90560Smatt static void
com_obio_attach(device_t parent,device_t self,void * aux)161607ead0eScube com_obio_attach(device_t parent, device_t self, void *aux)
162d4b90560Smatt {
163607ead0eScube 	struct com_obio_softc *osc = device_private(self);
164d4b90560Smatt 	struct com_softc *sc = &osc->osc_com;
165d4b90560Smatt 	union obio_attach_args *uoba = aux;
166d4b90560Smatt 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
16734537908Sgdamore 	bus_space_handle_t ioh;
16834537908Sgdamore 	bus_space_tag_t iot;
16934537908Sgdamore 	bus_addr_t iobase;
170d4b90560Smatt 
171607ead0eScube 	sc->sc_dev = self;
172607ead0eScube 
1731d97a430Smatt 	if (strcmp("modem", sa->sa_name) == 0) {
1741d97a430Smatt 		osc->osc_tadpole = 1;
1751d97a430Smatt 	}
1761d97a430Smatt 
177d4b90560Smatt 	/*
17873ae3282Smatt 	 * We're living on an obio that looks like an sbus slot.
179d4b90560Smatt 	 */
18034537908Sgdamore 	iot = sa->sa_bustag;
18134537908Sgdamore 	iobase = sa->sa_offset;
18225d726e4Suwe 	sc->sc_frequency = COM_FREQ;
18325d726e4Suwe 
18425d726e4Suwe 	/*
18525d726e4Suwe 	 * XXX: It would be nice to be able to split console input and
18625d726e4Suwe 	 * output to different devices.  For now switch to serial
18725d726e4Suwe 	 * console if PROM stdin is on serial (so that we can use DDB).
18825d726e4Suwe 	 */
18925d726e4Suwe 	if (prom_instance_to_package(prom_stdin()) == sa->sa_node)
19034537908Sgdamore 		comcnattach(iot, iobase, B9600, sc->sc_frequency,
19134537908Sgdamore 		    COM_TYPE_NORMAL, (CLOCAL | CREAD | CS8));
19225d726e4Suwe 
19334537908Sgdamore 	if (!com_is_console(iot, iobase, &ioh) &&
19434537908Sgdamore 	    sbus_bus_map(iot, sa->sa_slot, iobase, sa->sa_size,
19534537908Sgdamore 			 BUS_SPACE_MAP_LINEAR, &ioh) != 0) {
196607ead0eScube 		aprint_error(": can't map registers\n");
197d4b90560Smatt 		return;
198d4b90560Smatt 	}
199d4b90560Smatt 
200*6d487047Sthorpej 	com_init_regs(&sc->sc_regs, iot, ioh, iobase);
20134537908Sgdamore 
2021d97a430Smatt 	if (osc->osc_tadpole) {
20373ae3282Smatt 		*AUXIO4M_REG |= (AUXIO4M_LED|AUXIO4M_LTE);
20473ae3282Smatt 		do {
20573ae3282Smatt 			DELAY(100);
20634537908Sgdamore 		} while (!com_probe_subr(&sc->sc_regs));
20773ae3282Smatt #if 0
20873ae3282Smatt 		printf("modem: attach: lcr=0x%02x iir=0x%02x\n",
20934537908Sgdamore 			bus_space_read_1(sc->sc_regs.iot, sc->sc_regs.ioh, 3),
21034537908Sgdamore 			bus_space_read_1(sc->sc_regs.iot, sc->sc_regs.ioh, 2));
21173ae3282Smatt #endif
2121d97a430Smatt 	}
213d4b90560Smatt 
214d4b90560Smatt 	com_attach_subr(sc);
215d4b90560Smatt 
216633fd1e1Spk 	if (sa->sa_nintr != 0) {
21734537908Sgdamore 		(void)bus_intr_establish(sc->sc_regs.cr_iot, sa->sa_pri,
21834537908Sgdamore 		    IPL_SERIAL, comintr, sc);
219cffb5808Scgd 		evcnt_attach_dynamic(&osc->osc_intrcnt, EVCNT_TYPE_INTR, NULL,
220607ead0eScube 		    device_xname(self), "intr");
221633fd1e1Spk 	}
222d4b90560Smatt 
223e4f38925Sdyoung 	if (!pmf_device_register1(self, com_suspend, com_resume, com_cleanup)) {
224607ead0eScube 		aprint_error_dev(self, "could not establish shutdown hook");
225d4b90560Smatt 	}
226d4b90560Smatt }
227