1157299Smarcel/*-
2157299Smarcel * Copyright (c) 2004-2006 Marcel Moolenaar
3157299Smarcel * All rights reserved.
4157299Smarcel *
5157299Smarcel * Redistribution and use in source and binary forms, with or without
6157299Smarcel * modification, are permitted provided that the following conditions
7157299Smarcel * are met:
8157299Smarcel *
9157299Smarcel * 1. Redistributions of source code must retain the above copyright
10157299Smarcel *    notice, this list of conditions and the following disclaimer.
11157299Smarcel * 2. Redistributions in binary form must reproduce the above copyright
12157299Smarcel *    notice, this list of conditions and the following disclaimer in the
13157299Smarcel *    documentation and/or other materials provided with the distribution.
14157299Smarcel *
15157299Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16157299Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17157299Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18157299Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19157299Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20157299Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21157299Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22157299Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23157299Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24157299Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25157299Smarcel */
26157299Smarcel
27157299Smarcel#include <sys/cdefs.h>
28157299Smarcel__FBSDID("$FreeBSD$");
29157299Smarcel
30157299Smarcel#include <sys/param.h>
31157299Smarcel#include <sys/systm.h>
32157299Smarcel#include <sys/bus.h>
33157299Smarcel#include <sys/conf.h>
34157299Smarcel#include <machine/bus.h>
35157299Smarcel#include <sys/rman.h>
36157299Smarcel#include <sys/serial.h>
37157299Smarcel
38157299Smarcel#include <dev/scc/scc_bfe.h>
39157299Smarcel#include <dev/scc/scc_bus.h>
40157299Smarcel
41157299Smarcel#include <dev/ic/z8530.h>
42157299Smarcel
43157299Smarcel#include "scc_if.h"
44157299Smarcel
45157299Smarcelstatic int z8530_bfe_attach(struct scc_softc *, int);
46157299Smarcelstatic int z8530_bfe_iclear(struct scc_softc *, struct scc_chan *);
47157299Smarcelstatic int z8530_bfe_ipend(struct scc_softc *);
48157299Smarcelstatic int z8530_bfe_probe(struct scc_softc *);
49157299Smarcel
50157299Smarcelstatic kobj_method_t z8530_methods[] = {
51157299Smarcel	KOBJMETHOD(scc_attach,	z8530_bfe_attach),
52157299Smarcel	KOBJMETHOD(scc_iclear,	z8530_bfe_iclear),
53157299Smarcel	KOBJMETHOD(scc_ipend,	z8530_bfe_ipend),
54157299Smarcel	KOBJMETHOD(scc_probe,	z8530_bfe_probe),
55253899Smarius	KOBJMETHOD_END
56157299Smarcel};
57157299Smarcel
58157299Smarcelstruct scc_class scc_z8530_class = {
59157299Smarcel	"z8530 class",
60157299Smarcel	z8530_methods,
61157299Smarcel	sizeof(struct scc_softc),
62157299Smarcel	.cl_channels = 2,
63157299Smarcel	.cl_class = SCC_CLASS_Z8530,
64157299Smarcel	.cl_modes = SCC_MODE_ASYNC | SCC_MODE_BISYNC | SCC_MODE_HDLC,
65160689Smarcel	.cl_range = CHAN_B - CHAN_A,
66157299Smarcel};
67157299Smarcel
68157299Smarcel/* Multiplexed I/O. */
69157299Smarcelstatic __inline uint8_t
70157299Smarcelscc_getmreg(struct scc_bas *bas, int ch, int reg)
71157299Smarcel{
72157299Smarcel
73157299Smarcel	scc_setreg(bas, ch + REG_CTRL, reg);
74157299Smarcel	scc_barrier(bas);
75157299Smarcel	return (scc_getreg(bas, ch + REG_CTRL));
76157299Smarcel}
77157299Smarcel
78157299Smarcelstatic int
79253899Smariusz8530_bfe_attach(struct scc_softc *sc __unused, int reset __unused)
80157299Smarcel{
81157299Smarcel
82157299Smarcel	return (0);
83157299Smarcel}
84157299Smarcel
85157299Smarcelstatic int
86157299Smarcelz8530_bfe_iclear(struct scc_softc *sc, struct scc_chan *ch)
87157299Smarcel{
88157299Smarcel	struct scc_bas *bas;
89157299Smarcel	int c;
90157299Smarcel
91157299Smarcel	bas = &sc->sc_bas;
92157299Smarcel	c = (ch->ch_nr == 1) ? CHAN_A : CHAN_B;
93157299Smarcel	mtx_lock_spin(&sc->sc_hwmtx);
94157299Smarcel	if (ch->ch_ipend & SER_INT_TXIDLE) {
95157299Smarcel		scc_setreg(bas, c + REG_CTRL, CR_RSTTXI);
96157299Smarcel		scc_barrier(bas);
97157299Smarcel	}
98157299Smarcel	if (ch->ch_ipend & SER_INT_RXREADY) {
99157299Smarcel		scc_getreg(bas, c + REG_DATA);
100157299Smarcel		scc_barrier(bas);
101157299Smarcel	}
102157299Smarcel	if (ch->ch_ipend & (SER_INT_OVERRUN|SER_INT_BREAK))
103157299Smarcel		scc_setreg(bas, c + REG_CTRL, CR_RSTERR);
104157299Smarcel	mtx_unlock_spin(&sc->sc_hwmtx);
105157299Smarcel	return (0);
106157299Smarcel}
107157299Smarcel
108157299Smarcel#define	SIGCHG(c, i, s, d)				\
109157299Smarcel	if (c) {					\
110157299Smarcel		i |= (i & s) ? s : s | d;		\
111157299Smarcel	} else {					\
112157299Smarcel		i = (i & s) ? (i & ~s) | d : i;		\
113157299Smarcel	}
114157299Smarcel
115157299Smarcelstatic int
116157299Smarcelz8530_bfe_ipend(struct scc_softc *sc)
117157299Smarcel{
118157299Smarcel	struct scc_bas *bas;
119157299Smarcel	struct scc_chan *ch[2];
120157299Smarcel	uint32_t sig;
121157299Smarcel	uint8_t bes, ip, src;
122157299Smarcel
123157299Smarcel	bas = &sc->sc_bas;
124157299Smarcel	ch[0] = &sc->sc_chan[0];
125157299Smarcel	ch[1] = &sc->sc_chan[1];
126157299Smarcel	ch[0]->ch_ipend = 0;
127157299Smarcel	ch[1]->ch_ipend = 0;
128157299Smarcel
129157299Smarcel	mtx_lock_spin(&sc->sc_hwmtx);
130157299Smarcel	ip = scc_getmreg(bas, CHAN_A, RR_IP);
131157299Smarcel	if (ip & IP_RIA)
132157299Smarcel		ch[0]->ch_ipend |= SER_INT_RXREADY;
133157299Smarcel	if (ip & IP_RIB)
134157299Smarcel		ch[1]->ch_ipend |= SER_INT_RXREADY;
135157299Smarcel	if (ip & IP_TIA)
136157299Smarcel		ch[0]->ch_ipend |= SER_INT_TXIDLE;
137157299Smarcel	if (ip & IP_TIB)
138157299Smarcel		ch[1]->ch_ipend |= SER_INT_TXIDLE;
139157299Smarcel	if (ip & IP_SIA) {
140260287Sdim		bes = scc_getmreg(bas, CHAN_A, CR_RSTXSI);
141157299Smarcel		if (bes & BES_BRK)
142157299Smarcel			ch[0]->ch_ipend |= SER_INT_BREAK;
143157299Smarcel		sig = ch[0]->ch_hwsig;
144157299Smarcel		SIGCHG(bes & BES_CTS, sig, SER_CTS, SER_DCTS);
145157299Smarcel		SIGCHG(bes & BES_DCD, sig, SER_DCD, SER_DDCD);
146157299Smarcel		SIGCHG(bes & BES_SYNC, sig, SER_DSR, SER_DDSR);
147157299Smarcel		if (sig & SER_MASK_DELTA) {
148157299Smarcel			ch[0]->ch_hwsig = sig;
149157299Smarcel			ch[0]->ch_ipend |= SER_INT_SIGCHG;
150157299Smarcel		}
151157299Smarcel		src = scc_getmreg(bas, CHAN_A, RR_SRC);
152157299Smarcel		if (src & SRC_OVR)
153157299Smarcel			ch[0]->ch_ipend |= SER_INT_OVERRUN;
154157299Smarcel	}
155157299Smarcel	if (ip & IP_SIB) {
156260287Sdim		bes = scc_getmreg(bas, CHAN_B, CR_RSTXSI);
157157299Smarcel		if (bes & BES_BRK)
158157299Smarcel			ch[1]->ch_ipend |= SER_INT_BREAK;
159157299Smarcel		sig = ch[1]->ch_hwsig;
160157299Smarcel		SIGCHG(bes & BES_CTS, sig, SER_CTS, SER_DCTS);
161157299Smarcel		SIGCHG(bes & BES_DCD, sig, SER_DCD, SER_DDCD);
162157299Smarcel		SIGCHG(bes & BES_SYNC, sig, SER_DSR, SER_DDSR);
163157299Smarcel		if (sig & SER_MASK_DELTA) {
164157299Smarcel			ch[1]->ch_hwsig = sig;
165157299Smarcel			ch[1]->ch_ipend |= SER_INT_SIGCHG;
166157299Smarcel		}
167157299Smarcel		src = scc_getmreg(bas, CHAN_B, RR_SRC);
168157299Smarcel		if (src & SRC_OVR)
169157299Smarcel			ch[1]->ch_ipend |= SER_INT_OVERRUN;
170157299Smarcel	}
171157299Smarcel	mtx_unlock_spin(&sc->sc_hwmtx);
172157299Smarcel
173157299Smarcel	return (ch[0]->ch_ipend | ch[1]->ch_ipend);
174157299Smarcel}
175157299Smarcel
176157299Smarcelstatic int
177253899Smariusz8530_bfe_probe(struct scc_softc *sc __unused)
178157299Smarcel{
179157299Smarcel
180157299Smarcel	return (0);
181157299Smarcel}
182