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