scc_dev_z8530.c revision 160689
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: head/sys/dev/scc/scc_dev_z8530.c 160689 2006-07-26 03:10:01Z marcel $");
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),
55157299Smarcel	{ 0, 0 }
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 void
70157299Smarcelscc_setmreg(struct scc_bas *bas, int ch, int reg, int val)
71157299Smarcel{
72157299Smarcel
73157299Smarcel	scc_setreg(bas, ch + REG_CTRL, reg);
74157299Smarcel	scc_barrier(bas);
75157299Smarcel	scc_setreg(bas, ch + REG_CTRL, val);
76157299Smarcel}
77157299Smarcel
78157299Smarcelstatic __inline uint8_t
79157299Smarcelscc_getmreg(struct scc_bas *bas, int ch, int reg)
80157299Smarcel{
81157299Smarcel
82157299Smarcel	scc_setreg(bas, ch + REG_CTRL, reg);
83157299Smarcel	scc_barrier(bas);
84157299Smarcel	return (scc_getreg(bas, ch + REG_CTRL));
85157299Smarcel}
86157299Smarcel
87157299Smarcelstatic int
88157299Smarcelz8530_bfe_attach(struct scc_softc *sc, int reset)
89157299Smarcel{
90157299Smarcel	struct scc_bas *bas;
91157299Smarcel
92157299Smarcel	bas = &sc->sc_bas;
93157299Smarcel	return (0);
94157299Smarcel}
95157299Smarcel
96157299Smarcelstatic int
97157299Smarcelz8530_bfe_iclear(struct scc_softc *sc, struct scc_chan *ch)
98157299Smarcel{
99157299Smarcel	struct scc_bas *bas;
100157299Smarcel	int c;
101157299Smarcel
102157299Smarcel	bas = &sc->sc_bas;
103157299Smarcel	c = (ch->ch_nr == 1) ? CHAN_A : CHAN_B;
104157299Smarcel	mtx_lock_spin(&sc->sc_hwmtx);
105157299Smarcel	if (ch->ch_ipend & SER_INT_TXIDLE) {
106157299Smarcel		scc_setreg(bas, c + REG_CTRL, CR_RSTTXI);
107157299Smarcel		scc_barrier(bas);
108157299Smarcel	}
109157299Smarcel	if (ch->ch_ipend & SER_INT_RXREADY) {
110157299Smarcel		scc_getreg(bas, c + REG_DATA);
111157299Smarcel		scc_barrier(bas);
112157299Smarcel	}
113157299Smarcel	if (ch->ch_ipend & (SER_INT_OVERRUN|SER_INT_BREAK))
114157299Smarcel		scc_setreg(bas, c + REG_CTRL, CR_RSTERR);
115157299Smarcel	mtx_unlock_spin(&sc->sc_hwmtx);
116157299Smarcel	return (0);
117157299Smarcel}
118157299Smarcel
119157299Smarcel#define	SIGCHG(c, i, s, d)				\
120157299Smarcel	if (c) {					\
121157299Smarcel		i |= (i & s) ? s : s | d;		\
122157299Smarcel	} else {					\
123157299Smarcel		i = (i & s) ? (i & ~s) | d : i;		\
124157299Smarcel	}
125157299Smarcel
126157299Smarcelstatic int
127157299Smarcelz8530_bfe_ipend(struct scc_softc *sc)
128157299Smarcel{
129157299Smarcel	struct scc_bas *bas;
130157299Smarcel	struct scc_chan *ch[2];
131157299Smarcel	uint32_t sig;
132157299Smarcel	uint8_t bes, ip, src;
133157299Smarcel
134157299Smarcel	bas = &sc->sc_bas;
135157299Smarcel	ch[0] = &sc->sc_chan[0];
136157299Smarcel	ch[1] = &sc->sc_chan[1];
137157299Smarcel	ch[0]->ch_ipend = 0;
138157299Smarcel	ch[1]->ch_ipend = 0;
139157299Smarcel
140157299Smarcel	mtx_lock_spin(&sc->sc_hwmtx);
141157299Smarcel	ip = scc_getmreg(bas, CHAN_A, RR_IP);
142157299Smarcel	if (ip & IP_RIA)
143157299Smarcel		ch[0]->ch_ipend |= SER_INT_RXREADY;
144157299Smarcel	if (ip & IP_RIB)
145157299Smarcel		ch[1]->ch_ipend |= SER_INT_RXREADY;
146157299Smarcel	if (ip & IP_TIA)
147157299Smarcel		ch[0]->ch_ipend |= SER_INT_TXIDLE;
148157299Smarcel	if (ip & IP_TIB)
149157299Smarcel		ch[1]->ch_ipend |= SER_INT_TXIDLE;
150157299Smarcel	if (ip & IP_SIA) {
151157299Smarcel		scc_setreg(bas, CHAN_A + REG_CTRL, CR_RSTXSI);
152157299Smarcel		scc_barrier(bas);
153157299Smarcel		bes = scc_getreg(bas, CHAN_A + REG_CTRL);
154157299Smarcel		if (bes & BES_BRK)
155157299Smarcel			ch[0]->ch_ipend |= SER_INT_BREAK;
156157299Smarcel		sig = ch[0]->ch_hwsig;
157157299Smarcel		SIGCHG(bes & BES_CTS, sig, SER_CTS, SER_DCTS);
158157299Smarcel		SIGCHG(bes & BES_DCD, sig, SER_DCD, SER_DDCD);
159157299Smarcel		SIGCHG(bes & BES_SYNC, sig, SER_DSR, SER_DDSR);
160157299Smarcel		if (sig & SER_MASK_DELTA) {
161157299Smarcel			ch[0]->ch_hwsig = sig;
162157299Smarcel			ch[0]->ch_ipend |= SER_INT_SIGCHG;
163157299Smarcel		}
164157299Smarcel		src = scc_getmreg(bas, CHAN_A, RR_SRC);
165157299Smarcel		if (src & SRC_OVR)
166157299Smarcel			ch[0]->ch_ipend |= SER_INT_OVERRUN;
167157299Smarcel	}
168157299Smarcel	if (ip & IP_SIB) {
169157299Smarcel		scc_setreg(bas, CHAN_B + REG_CTRL, CR_RSTXSI);
170157299Smarcel		scc_barrier(bas);
171157299Smarcel		bes = scc_getreg(bas, CHAN_B + REG_CTRL);
172157299Smarcel		if (bes & BES_BRK)
173157299Smarcel			ch[1]->ch_ipend |= SER_INT_BREAK;
174157299Smarcel		sig = ch[1]->ch_hwsig;
175157299Smarcel		SIGCHG(bes & BES_CTS, sig, SER_CTS, SER_DCTS);
176157299Smarcel		SIGCHG(bes & BES_DCD, sig, SER_DCD, SER_DDCD);
177157299Smarcel		SIGCHG(bes & BES_SYNC, sig, SER_DSR, SER_DDSR);
178157299Smarcel		if (sig & SER_MASK_DELTA) {
179157299Smarcel			ch[1]->ch_hwsig = sig;
180157299Smarcel			ch[1]->ch_ipend |= SER_INT_SIGCHG;
181157299Smarcel		}
182157299Smarcel		src = scc_getmreg(bas, CHAN_B, RR_SRC);
183157299Smarcel		if (src & SRC_OVR)
184157299Smarcel			ch[1]->ch_ipend |= SER_INT_OVERRUN;
185157299Smarcel	}
186157299Smarcel	mtx_unlock_spin(&sc->sc_hwmtx);
187157299Smarcel
188157299Smarcel	return (ch[0]->ch_ipend | ch[1]->ch_ipend);
189157299Smarcel}
190157299Smarcel
191157299Smarcelstatic int
192157299Smarcelz8530_bfe_probe(struct scc_softc *sc)
193157299Smarcel{
194157299Smarcel	struct scc_bas *bas;
195157299Smarcel
196157299Smarcel	bas = &sc->sc_bas;
197157299Smarcel	return (0);
198157299Smarcel}
199