com_dio.c revision 1.10 1 1.10 thorpej /* $NetBSD: com_dio.c,v 1.10 2024/01/16 05:48:28 thorpej Exp $ */
2 1.1 tsutsui
3 1.1 tsutsui /*-
4 1.1 tsutsui * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 tsutsui * All rights reserved.
6 1.1 tsutsui *
7 1.1 tsutsui * This code is derived from software contributed to The NetBSD Foundation
8 1.1 tsutsui * by Charles M. Hannum.
9 1.1 tsutsui *
10 1.1 tsutsui * Redistribution and use in source and binary forms, with or without
11 1.1 tsutsui * modification, are permitted provided that the following conditions
12 1.1 tsutsui * are met:
13 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright
14 1.1 tsutsui * notice, this list of conditions and the following disclaimer.
15 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the
17 1.1 tsutsui * documentation and/or other materials provided with the distribution.
18 1.1 tsutsui *
19 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 tsutsui * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 tsutsui * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 tsutsui * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 tsutsui * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 tsutsui * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 tsutsui * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 tsutsui * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 tsutsui * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 tsutsui * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 tsutsui * POSSIBILITY OF SUCH DAMAGE.
30 1.1 tsutsui */
31 1.1 tsutsui
32 1.1 tsutsui /*-
33 1.1 tsutsui * Copyright (c) 1991 The Regents of the University of California.
34 1.1 tsutsui * All rights reserved.
35 1.1 tsutsui *
36 1.1 tsutsui * Redistribution and use in source and binary forms, with or without
37 1.1 tsutsui * modification, are permitted provided that the following conditions
38 1.1 tsutsui * are met:
39 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright
40 1.1 tsutsui * notice, this list of conditions and the following disclaimer.
41 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the
43 1.1 tsutsui * documentation and/or other materials provided with the distribution.
44 1.1 tsutsui * 3. Neither the name of the University nor the names of its contributors
45 1.1 tsutsui * may be used to endorse or promote products derived from this software
46 1.1 tsutsui * without specific prior written permission.
47 1.1 tsutsui *
48 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 1.1 tsutsui * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 1.1 tsutsui * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 1.1 tsutsui * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 1.1 tsutsui * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 1.1 tsutsui * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 1.1 tsutsui * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 1.1 tsutsui * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 1.1 tsutsui * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 1.1 tsutsui * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 1.1 tsutsui * SUCH DAMAGE.
59 1.1 tsutsui *
60 1.1 tsutsui * @(#)com.c 7.5 (Berkeley) 5/16/91
61 1.1 tsutsui */
62 1.1 tsutsui
63 1.1 tsutsui #include <sys/cdefs.h>
64 1.10 thorpej __KERNEL_RCSID(0, "$NetBSD: com_dio.c,v 1.10 2024/01/16 05:48:28 thorpej Exp $");
65 1.1 tsutsui
66 1.1 tsutsui #include <sys/param.h>
67 1.1 tsutsui #include <sys/systm.h>
68 1.1 tsutsui #include <sys/device.h>
69 1.1 tsutsui #include <sys/termios.h>
70 1.1 tsutsui #include <sys/ttydefaults.h>
71 1.1 tsutsui
72 1.1 tsutsui #include <machine/bus.h>
73 1.1 tsutsui
74 1.1 tsutsui #include <dev/ic/comreg.h>
75 1.1 tsutsui #include <dev/ic/comvar.h>
76 1.1 tsutsui
77 1.1 tsutsui #include <hp300/dev/diovar.h>
78 1.1 tsutsui #include <hp300/dev/diodevs.h>
79 1.1 tsutsui #include <hp300/dev/com_dioreg.h>
80 1.1 tsutsui #include <hp300/dev/com_diovar.h>
81 1.1 tsutsui
82 1.1 tsutsui struct com_dio_softc {
83 1.1 tsutsui struct com_softc sc_com; /* real "com" softc */
84 1.1 tsutsui
85 1.1 tsutsui /* DIO-specific goo. */
86 1.1 tsutsui struct bus_space_tag sc_tag; /* device specific bus space tag */
87 1.1 tsutsui void *sc_ih; /* interrupt handler */
88 1.1 tsutsui };
89 1.1 tsutsui
90 1.7 cube static int com_dio_match(device_t, cfdata_t , void *);
91 1.7 cube static void com_dio_attach(device_t, device_t, void *);
92 1.1 tsutsui
93 1.7 cube CFATTACH_DECL_NEW(com_dio, sizeof(struct com_dio_softc),
94 1.1 tsutsui com_dio_match, com_dio_attach, NULL, NULL);
95 1.1 tsutsui
96 1.1 tsutsui static int com_dio_speed = TTYDEF_SPEED;
97 1.1 tsutsui static struct bus_space_tag comcntag;
98 1.1 tsutsui
99 1.1 tsutsui #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
100 1.1 tsutsui
101 1.2 thorpej static int
102 1.7 cube com_dio_match(device_t parent, cfdata_t match, void *aux)
103 1.1 tsutsui {
104 1.1 tsutsui struct dio_attach_args *da = aux;
105 1.1 tsutsui
106 1.1 tsutsui switch (da->da_id) {
107 1.1 tsutsui case DIO_DEVICE_ID_DCA0:
108 1.1 tsutsui case DIO_DEVICE_ID_DCA0REM:
109 1.1 tsutsui case DIO_DEVICE_ID_DCA1:
110 1.1 tsutsui case DIO_DEVICE_ID_DCA1REM:
111 1.1 tsutsui return 1;
112 1.1 tsutsui }
113 1.1 tsutsui
114 1.1 tsutsui return 0;
115 1.1 tsutsui }
116 1.1 tsutsui
117 1.2 thorpej static void
118 1.7 cube com_dio_attach(device_t parent, device_t self, void *aux)
119 1.1 tsutsui {
120 1.7 cube struct com_dio_softc *dsc = device_private(self);
121 1.1 tsutsui struct com_softc *sc = &dsc->sc_com;
122 1.1 tsutsui bus_space_tag_t iot;
123 1.1 tsutsui bus_space_handle_t iohdca, iohcom;
124 1.1 tsutsui struct dio_attach_args *da = aux;
125 1.1 tsutsui int isconsole;
126 1.1 tsutsui
127 1.7 cube sc->sc_dev = self;
128 1.1 tsutsui isconsole = com_is_console(&comcntag, da->da_addr + DCA_COM_OFFSET,
129 1.1 tsutsui &iohcom);
130 1.1 tsutsui
131 1.1 tsutsui if (isconsole) {
132 1.1 tsutsui iot = &comcntag;
133 1.1 tsutsui bus_space_unmap(iot, iohcom, COM_NPORTS);
134 1.1 tsutsui } else {
135 1.1 tsutsui iot = &dsc->sc_tag;
136 1.1 tsutsui memcpy(iot, da->da_bst, sizeof(struct bus_space_tag));
137 1.1 tsutsui dio_set_bus_space_oddbyte(iot);
138 1.1 tsutsui }
139 1.1 tsutsui
140 1.1 tsutsui if (bus_space_map(iot, da->da_addr, DCA_SIZE, 0, &iohdca) ||
141 1.1 tsutsui bus_space_subregion(iot, iohdca, DCA_COM_OFFSET,
142 1.1 tsutsui COM_NPORTS << 1, &iohcom)) {
143 1.7 cube aprint_error(": can't map i/o space\n");
144 1.1 tsutsui return;
145 1.1 tsutsui }
146 1.1 tsutsui
147 1.1 tsutsui if (!isconsole) {
148 1.1 tsutsui bus_space_write_1(iot, iohdca, DCA_RESET, 0xff);
149 1.1 tsutsui DELAY(1000);
150 1.1 tsutsui }
151 1.1 tsutsui
152 1.9 thorpej com_init_regs(&sc->sc_regs, iot, iohcom,
153 1.5 gdamore da->da_addr + DCA_COM_OFFSET);
154 1.5 gdamore
155 1.1 tsutsui sc->sc_frequency = COM_DIO_FREQ;
156 1.1 tsutsui
157 1.1 tsutsui com_attach_subr(sc);
158 1.1 tsutsui
159 1.10 thorpej dsc->sc_ih = dio_intr_establish(comintr, sc, da->da_ipl, ISRPRI_TTY);
160 1.1 tsutsui
161 1.1 tsutsui /* Enable interrupts. */
162 1.1 tsutsui bus_space_write_1(iot, iohdca, DCA_IC, IC_IE);
163 1.1 tsutsui }
164 1.1 tsutsui
165 1.1 tsutsui int
166 1.1 tsutsui com_dio_cnattach(bus_space_tag_t bst, bus_addr_t addr, int scode)
167 1.1 tsutsui {
168 1.1 tsutsui bus_space_tag_t iot = &comcntag;
169 1.1 tsutsui bus_space_handle_t iohdca;
170 1.3 tsutsui uint8_t id;
171 1.1 tsutsui
172 1.1 tsutsui memcpy(iot, bst, sizeof(struct bus_space_tag));
173 1.1 tsutsui dio_set_bus_space_oddbyte(iot);
174 1.1 tsutsui
175 1.1 tsutsui if (bus_space_map(iot, addr, DCA_SIZE, 0, &iohdca))
176 1.1 tsutsui return 1;
177 1.1 tsutsui bus_space_write_1(iot, iohdca, DCA_RESET, 0xff);
178 1.1 tsutsui DELAY(100);
179 1.1 tsutsui bus_space_write_1(iot, iohdca, DCA_IC, IC_IE);
180 1.1 tsutsui
181 1.1 tsutsui id = bus_space_read_1(iot, iohdca, DCA_ID);
182 1.1 tsutsui bus_space_unmap(iot, iohdca, DCA_SIZE);
183 1.1 tsutsui
184 1.1 tsutsui switch (id) {
185 1.1 tsutsui #ifdef CONSCODE
186 1.1 tsutsui case DCAID0:
187 1.1 tsutsui case DCAID1:
188 1.1 tsutsui #endif
189 1.1 tsutsui case DCAREMID0:
190 1.1 tsutsui case DCAREMID1:
191 1.1 tsutsui break;
192 1.1 tsutsui default:
193 1.1 tsutsui return 1;
194 1.1 tsutsui }
195 1.1 tsutsui
196 1.1 tsutsui comcnattach(iot, addr + DCA_COM_OFFSET, com_dio_speed, COM_DIO_FREQ,
197 1.1 tsutsui COM_TYPE_NORMAL, CONMODE);
198 1.1 tsutsui
199 1.1 tsutsui return 0;
200 1.1 tsutsui }
201