xref: /netbsd-src/sys/arch/hp300/dev/com_dio.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: com_dio.c,v 1.4 2005/12/11 12:17:13 christos 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. 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 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: com_dio.c,v 1.4 2005/12/11 12:17:13 christos Exp $");
72 
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/device.h>
76 #include <sys/termios.h>
77 #include <sys/ttydefaults.h>
78 
79 #include <machine/bus.h>
80 
81 #include <dev/ic/comreg.h>
82 #include <dev/ic/comvar.h>
83 
84 #include <hp300/dev/diovar.h>
85 #include <hp300/dev/diodevs.h>
86 #include <hp300/dev/com_dioreg.h>
87 #include <hp300/dev/com_diovar.h>
88 
89 struct com_dio_softc {
90 	struct	com_softc sc_com;	/* real "com" softc */
91 
92 	/* DIO-specific goo. */
93 	struct	bus_space_tag sc_tag;	/* device specific bus space tag */
94 	void	*sc_ih;			/* interrupt handler */
95 };
96 
97 static int	com_dio_match(struct device *, struct cfdata *, void *);
98 static void	com_dio_attach(struct device *, struct device *, void *);
99 
100 CFATTACH_DECL(com_dio, sizeof(struct com_dio_softc),
101     com_dio_match, com_dio_attach, NULL, NULL);
102 
103 static int com_dio_speed = TTYDEF_SPEED;
104 static struct bus_space_tag comcntag;
105 
106 #define CONMODE	((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
107 
108 static int
109 com_dio_match(struct device *parent, struct cfdata *match, void *aux)
110 {
111 	struct dio_attach_args *da = aux;
112 
113 	switch (da->da_id) {
114 	case DIO_DEVICE_ID_DCA0:
115 	case DIO_DEVICE_ID_DCA0REM:
116 	case DIO_DEVICE_ID_DCA1:
117 	case DIO_DEVICE_ID_DCA1REM:
118 		return 1;
119 	}
120 
121 	return 0;
122 }
123 
124 static void
125 com_dio_attach(struct device *parent, struct device *self, void *aux)
126 {
127 	struct com_dio_softc *dsc = (void *)self;
128 	struct com_softc *sc = &dsc->sc_com;
129 	bus_space_tag_t iot;
130 	bus_space_handle_t iohdca, iohcom;
131 	struct dio_attach_args *da = aux;
132 	int isconsole;
133 
134 	isconsole = com_is_console(&comcntag, da->da_addr + DCA_COM_OFFSET,
135 	    &iohcom);
136 
137 	if (isconsole) {
138 		iot = &comcntag;
139 		bus_space_unmap(iot, iohcom, COM_NPORTS);
140 	} else {
141 		iot = &dsc->sc_tag;
142 		memcpy(iot, da->da_bst, sizeof(struct bus_space_tag));
143 		dio_set_bus_space_oddbyte(iot);
144 	}
145 
146 	if (bus_space_map(iot, da->da_addr, DCA_SIZE, 0, &iohdca) ||
147 	    bus_space_subregion(iot, iohdca, DCA_COM_OFFSET,
148 	    COM_NPORTS << 1, &iohcom)) {
149 		printf(": can't map i/o space\n");
150 		return;
151 	}
152 
153 	if (!isconsole) {
154 		bus_space_write_1(iot, iohdca, DCA_RESET, 0xff);
155 		DELAY(1000);
156 	}
157 
158 	sc->sc_iot = iot;
159 	sc->sc_ioh = iohcom;
160 	sc->sc_iobase = da->da_addr + DCA_COM_OFFSET;
161 	sc->sc_frequency = COM_DIO_FREQ;
162 
163 	com_attach_subr(sc);
164 
165 	dsc->sc_ih = dio_intr_establish(comintr, sc, da->da_ipl,
166 	    ((sc->sc_hwflags & COM_HW_FIFO) != 0) ? IPL_TTY : IPL_TTYNOBUF);
167 
168 	/* Enable interrupts. */
169 	bus_space_write_1(iot, iohdca, DCA_IC, IC_IE);
170 }
171 
172 int
173 com_dio_cnattach(bus_space_tag_t bst, bus_addr_t addr, int scode)
174 {
175 	bus_space_tag_t iot = &comcntag;
176 	bus_space_handle_t iohdca;
177 	uint8_t id;
178 
179 	memcpy(iot, bst, sizeof(struct bus_space_tag));
180 	dio_set_bus_space_oddbyte(iot);
181 
182 	if (bus_space_map(iot, addr, DCA_SIZE, 0, &iohdca))
183 		return 1;
184 	bus_space_write_1(iot, iohdca, DCA_RESET, 0xff);
185 	DELAY(100);
186 	bus_space_write_1(iot, iohdca, DCA_IC, IC_IE);
187 
188 	id = bus_space_read_1(iot, iohdca, DCA_ID);
189 	bus_space_unmap(iot, iohdca, DCA_SIZE);
190 
191 	switch (id) {
192 #ifdef CONSCODE
193 	case DCAID0:
194 	case DCAID1:
195 #endif
196 	case DCAREMID0:
197 	case DCAREMID1:
198 		break;
199 	default:
200 		return 1;
201 	}
202 
203 	comcnattach(iot, addr + DCA_COM_OFFSET, com_dio_speed, COM_DIO_FREQ,
204 	    COM_TYPE_NORMAL, CONMODE);
205 
206 	return 0;
207 }
208