xref: /freebsd/sys/dev/scc/scc_dev_z8530.c (revision 2f513db72b034fd5ef7f080b11be5c711c15186a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2004-2006 Marcel Moolenaar
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <machine/bus.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/rman.h>
40 #include <sys/serial.h>
41 
42 #include <dev/scc/scc_bfe.h>
43 #include <dev/scc/scc_bus.h>
44 
45 #include <dev/ic/z8530.h>
46 
47 #include "scc_if.h"
48 
49 static int z8530_bfe_attach(struct scc_softc *, int);
50 static int z8530_bfe_iclear(struct scc_softc *, struct scc_chan *);
51 static int z8530_bfe_ipend(struct scc_softc *);
52 static int z8530_bfe_probe(struct scc_softc *);
53 
54 static kobj_method_t z8530_methods[] = {
55 	KOBJMETHOD(scc_attach,	z8530_bfe_attach),
56 	KOBJMETHOD(scc_iclear,	z8530_bfe_iclear),
57 	KOBJMETHOD(scc_ipend,	z8530_bfe_ipend),
58 	KOBJMETHOD(scc_probe,	z8530_bfe_probe),
59 	KOBJMETHOD_END
60 };
61 
62 struct scc_class scc_z8530_class = {
63 	"z8530 class",
64 	z8530_methods,
65 	sizeof(struct scc_softc),
66 	.cl_channels = 2,
67 	.cl_class = SCC_CLASS_Z8530,
68 	.cl_modes = SCC_MODE_ASYNC | SCC_MODE_BISYNC | SCC_MODE_HDLC,
69 	.cl_range = CHAN_B - CHAN_A,
70 };
71 
72 /* Multiplexed I/O. */
73 static __inline uint8_t
74 scc_getmreg(struct scc_bas *bas, int ch, int reg)
75 {
76 
77 	scc_setreg(bas, ch + REG_CTRL, reg);
78 	scc_barrier(bas);
79 	return (scc_getreg(bas, ch + REG_CTRL));
80 }
81 
82 static int
83 z8530_bfe_attach(struct scc_softc *sc __unused, int reset __unused)
84 {
85 
86 	return (0);
87 }
88 
89 static int
90 z8530_bfe_iclear(struct scc_softc *sc, struct scc_chan *ch)
91 {
92 	struct scc_bas *bas;
93 	int c;
94 
95 	bas = &sc->sc_bas;
96 	c = (ch->ch_nr == 1) ? CHAN_A : CHAN_B;
97 	mtx_lock_spin(&sc->sc_hwmtx);
98 	if (ch->ch_ipend & SER_INT_TXIDLE) {
99 		scc_setreg(bas, c + REG_CTRL, CR_RSTTXI);
100 		scc_barrier(bas);
101 	}
102 	if (ch->ch_ipend & SER_INT_RXREADY) {
103 		scc_getreg(bas, c + REG_DATA);
104 		scc_barrier(bas);
105 	}
106 	if (ch->ch_ipend & (SER_INT_OVERRUN|SER_INT_BREAK))
107 		scc_setreg(bas, c + REG_CTRL, CR_RSTERR);
108 	mtx_unlock_spin(&sc->sc_hwmtx);
109 	return (0);
110 }
111 
112 #define	SIGCHG(c, i, s, d)				\
113 	if (c) {					\
114 		i |= (i & s) ? s : s | d;		\
115 	} else {					\
116 		i = (i & s) ? (i & ~s) | d : i;		\
117 	}
118 
119 static int
120 z8530_bfe_ipend(struct scc_softc *sc)
121 {
122 	struct scc_bas *bas;
123 	struct scc_chan *ch[2];
124 	uint32_t sig;
125 	uint8_t bes, ip, src;
126 
127 	bas = &sc->sc_bas;
128 	ch[0] = &sc->sc_chan[0];
129 	ch[1] = &sc->sc_chan[1];
130 	ch[0]->ch_ipend = 0;
131 	ch[1]->ch_ipend = 0;
132 
133 	mtx_lock_spin(&sc->sc_hwmtx);
134 	ip = scc_getmreg(bas, CHAN_A, RR_IP);
135 	if (ip & IP_RIA)
136 		ch[0]->ch_ipend |= SER_INT_RXREADY;
137 	if (ip & IP_RIB)
138 		ch[1]->ch_ipend |= SER_INT_RXREADY;
139 	if (ip & IP_TIA)
140 		ch[0]->ch_ipend |= SER_INT_TXIDLE;
141 	if (ip & IP_TIB)
142 		ch[1]->ch_ipend |= SER_INT_TXIDLE;
143 	if (ip & IP_SIA) {
144 		bes = scc_getmreg(bas, CHAN_A, CR_RSTXSI);
145 		if (bes & BES_BRK)
146 			ch[0]->ch_ipend |= SER_INT_BREAK;
147 		sig = ch[0]->ch_hwsig;
148 		SIGCHG(bes & BES_CTS, sig, SER_CTS, SER_DCTS);
149 		SIGCHG(bes & BES_DCD, sig, SER_DCD, SER_DDCD);
150 		SIGCHG(bes & BES_SYNC, sig, SER_DSR, SER_DDSR);
151 		if (sig & SER_MASK_DELTA) {
152 			ch[0]->ch_hwsig = sig;
153 			ch[0]->ch_ipend |= SER_INT_SIGCHG;
154 		}
155 		src = scc_getmreg(bas, CHAN_A, RR_SRC);
156 		if (src & SRC_OVR)
157 			ch[0]->ch_ipend |= SER_INT_OVERRUN;
158 	}
159 	if (ip & IP_SIB) {
160 		bes = scc_getmreg(bas, CHAN_B, CR_RSTXSI);
161 		if (bes & BES_BRK)
162 			ch[1]->ch_ipend |= SER_INT_BREAK;
163 		sig = ch[1]->ch_hwsig;
164 		SIGCHG(bes & BES_CTS, sig, SER_CTS, SER_DCTS);
165 		SIGCHG(bes & BES_DCD, sig, SER_DCD, SER_DDCD);
166 		SIGCHG(bes & BES_SYNC, sig, SER_DSR, SER_DDSR);
167 		if (sig & SER_MASK_DELTA) {
168 			ch[1]->ch_hwsig = sig;
169 			ch[1]->ch_ipend |= SER_INT_SIGCHG;
170 		}
171 		src = scc_getmreg(bas, CHAN_B, RR_SRC);
172 		if (src & SRC_OVR)
173 			ch[1]->ch_ipend |= SER_INT_OVERRUN;
174 	}
175 	mtx_unlock_spin(&sc->sc_hwmtx);
176 
177 	return (ch[0]->ch_ipend | ch[1]->ch_ipend);
178 }
179 
180 static int
181 z8530_bfe_probe(struct scc_softc *sc __unused)
182 {
183 
184 	return (0);
185 }
186