sbjcn.c revision 1.1.12.3 1 1.1.12.3 nathanw /* $NetBSD: sbjcn.c,v 1.1.12.3 2002/10/18 02:38:48 nathanw Exp $ */
2 1.1.12.2 nathanw
3 1.1.12.2 nathanw /*
4 1.1.12.2 nathanw * Copyright 2000, 2001
5 1.1.12.2 nathanw * Broadcom Corporation. All rights reserved.
6 1.1.12.2 nathanw *
7 1.1.12.2 nathanw * This software is furnished under license and may be used and copied only
8 1.1.12.2 nathanw * in accordance with the following terms and conditions. Subject to these
9 1.1.12.2 nathanw * conditions, you may download, copy, install, use, modify and distribute
10 1.1.12.2 nathanw * modified or unmodified copies of this software in source and/or binary
11 1.1.12.2 nathanw * form. No title or ownership is transferred hereby.
12 1.1.12.2 nathanw *
13 1.1.12.2 nathanw * 1) Any source code used, modified or distributed must reproduce and
14 1.1.12.2 nathanw * retain this copyright notice and list of conditions as they appear in
15 1.1.12.2 nathanw * the source file.
16 1.1.12.2 nathanw *
17 1.1.12.2 nathanw * 2) No right is granted to use any trade name, trademark, or logo of
18 1.1.12.2 nathanw * Broadcom Corporation. Neither the "Broadcom Corporation" name nor any
19 1.1.12.2 nathanw * trademark or logo of Broadcom Corporation may be used to endorse or
20 1.1.12.2 nathanw * promote products derived from this software without the prior written
21 1.1.12.2 nathanw * permission of Broadcom Corporation.
22 1.1.12.2 nathanw *
23 1.1.12.2 nathanw * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR IMPLIED
24 1.1.12.2 nathanw * WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF
25 1.1.12.2 nathanw * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
26 1.1.12.2 nathanw * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM BE LIABLE
27 1.1.12.2 nathanw * FOR ANY DAMAGES WHATSOEVER, AND IN PARTICULAR, BROADCOM SHALL NOT BE
28 1.1.12.2 nathanw * LIABLE FOR DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 1.1.12.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 1.1.12.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 1.1.12.2 nathanw * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 1.1.12.2 nathanw * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33 1.1.12.2 nathanw * OR OTHERWISE), EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1.12.2 nathanw */
35 1.1.12.2 nathanw
36 1.1.12.2 nathanw #define SBJCN_DEBUG
37 1.1.12.2 nathanw
38 1.1.12.2 nathanw /* from: $NetBSD: com.c,v 1.172 2000/05/03 19:19:04 thorpej Exp */
39 1.1.12.2 nathanw
40 1.1.12.2 nathanw /*-
41 1.1.12.2 nathanw * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
42 1.1.12.2 nathanw * All rights reserved.
43 1.1.12.2 nathanw *
44 1.1.12.2 nathanw * This code is derived from software contributed to The NetBSD Foundation
45 1.1.12.2 nathanw * by Charles M. Hannum.
46 1.1.12.2 nathanw *
47 1.1.12.2 nathanw * Redistribution and use in source and binary forms, with or without
48 1.1.12.2 nathanw * modification, are permitted provided that the following conditions
49 1.1.12.2 nathanw * are met:
50 1.1.12.2 nathanw * 1. Redistributions of source code must retain the above copyright
51 1.1.12.2 nathanw * notice, this list of conditions and the following disclaimer.
52 1.1.12.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
53 1.1.12.2 nathanw * notice, this list of conditions and the following disclaimer in the
54 1.1.12.2 nathanw * documentation and/or other materials provided with the distribution.
55 1.1.12.2 nathanw * 3. All advertising materials mentioning features or use of this software
56 1.1.12.2 nathanw * must display the following acknowledgement:
57 1.1.12.2 nathanw * This product includes software developed by the NetBSD
58 1.1.12.2 nathanw * Foundation, Inc. and its contributors.
59 1.1.12.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
60 1.1.12.2 nathanw * contributors may be used to endorse or promote products derived
61 1.1.12.2 nathanw * from this software without specific prior written permission.
62 1.1.12.2 nathanw *
63 1.1.12.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
64 1.1.12.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
65 1.1.12.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
66 1.1.12.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
67 1.1.12.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
68 1.1.12.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
69 1.1.12.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
70 1.1.12.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
71 1.1.12.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
72 1.1.12.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
73 1.1.12.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
74 1.1.12.2 nathanw */
75 1.1.12.2 nathanw
76 1.1.12.2 nathanw /*
77 1.1.12.2 nathanw * Copyright (c) 1991 The Regents of the University of California.
78 1.1.12.2 nathanw * All rights reserved.
79 1.1.12.2 nathanw *
80 1.1.12.2 nathanw * Redistribution and use in source and binary forms, with or without
81 1.1.12.2 nathanw * modification, are permitted provided that the following conditions
82 1.1.12.2 nathanw * are met:
83 1.1.12.2 nathanw * 1. Redistributions of source code must retain the above copyright
84 1.1.12.2 nathanw * notice, this list of conditions and the following disclaimer.
85 1.1.12.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
86 1.1.12.2 nathanw * notice, this list of conditions and the following disclaimer in the
87 1.1.12.2 nathanw * documentation and/or other materials provided with the distribution.
88 1.1.12.2 nathanw * 3. All advertising materials mentioning features or use of this software
89 1.1.12.2 nathanw * must display the following acknowledgement:
90 1.1.12.2 nathanw * This product includes software developed by the University of
91 1.1.12.2 nathanw * California, Berkeley and its contributors.
92 1.1.12.2 nathanw * 4. Neither the name of the University nor the names of its contributors
93 1.1.12.2 nathanw * may be used to endorse or promote products derived from this software
94 1.1.12.2 nathanw * without specific prior written permission.
95 1.1.12.2 nathanw *
96 1.1.12.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
97 1.1.12.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
98 1.1.12.2 nathanw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
99 1.1.12.2 nathanw * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
100 1.1.12.2 nathanw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
101 1.1.12.2 nathanw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
102 1.1.12.2 nathanw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
103 1.1.12.2 nathanw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
104 1.1.12.2 nathanw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
105 1.1.12.2 nathanw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
106 1.1.12.2 nathanw * SUCH DAMAGE.
107 1.1.12.2 nathanw *
108 1.1.12.2 nathanw * @(#)com.c 7.5 (Berkeley) 5/16/91
109 1.1.12.2 nathanw */
110 1.1.12.2 nathanw
111 1.1.12.2 nathanw /*
112 1.1.12.2 nathanw * `sbjcn' driver, supports console over SiByte SB-1250 JTAG.
113 1.1.12.2 nathanw *
114 1.1.12.2 nathanw * Accesses a section of JTAG memory space to mimic a console,
115 1.1.12.2 nathanw * if there's a matching program outside to communicate with.
116 1.1.12.2 nathanw * If nobody is there, things will be very quiet.
117 1.1.12.2 nathanw */
118 1.1.12.2 nathanw
119 1.1.12.2 nathanw #include "opt_ddb.h"
120 1.1.12.2 nathanw
121 1.1.12.2 nathanw #include <sys/param.h>
122 1.1.12.2 nathanw #include <sys/systm.h>
123 1.1.12.2 nathanw #include <sys/ioctl.h>
124 1.1.12.2 nathanw #include <sys/select.h>
125 1.1.12.2 nathanw #include <sys/tty.h>
126 1.1.12.2 nathanw #include <sys/proc.h>
127 1.1.12.2 nathanw #include <sys/user.h>
128 1.1.12.2 nathanw #include <sys/conf.h>
129 1.1.12.2 nathanw #include <sys/file.h>
130 1.1.12.2 nathanw #include <sys/uio.h>
131 1.1.12.2 nathanw #include <sys/kernel.h>
132 1.1.12.2 nathanw #include <sys/syslog.h>
133 1.1.12.2 nathanw #include <sys/types.h>
134 1.1.12.2 nathanw #include <sys/device.h>
135 1.1.12.2 nathanw #include <sys/malloc.h>
136 1.1.12.2 nathanw #include <sys/vnode.h>
137 1.1.12.2 nathanw
138 1.1.12.2 nathanw #include <sbmips/dev/sbscd/sbscdvar.h>
139 1.1.12.2 nathanw #include <sbmips/dev/sbscd/sbjcnvar.h>
140 1.1.12.2 nathanw #include <dev/cons.h>
141 1.1.12.2 nathanw #include <machine/locore.h>
142 1.1.12.2 nathanw
143 1.1.12.2 nathanw void sbjcn_attach_channel(struct sbjcn_softc *sc, int chan, int intr);
144 1.1.12.2 nathanw static void sbjcncn_grabdword(struct sbjcn_channel *ch);
145 1.1.12.2 nathanw static char sbjcncn_nextbyte(struct sbjcn_channel *ch);
146 1.1.12.2 nathanw static void sbjcn_cngrabdword(void);
147 1.1.12.2 nathanw
148 1.1.12.2 nathanw #if defined(DDB) || defined(KGDB)
149 1.1.12.2 nathanw static void sbjcn_enable_debugport(struct sbjcn_channel *ch);
150 1.1.12.2 nathanw #endif
151 1.1.12.2 nathanw void sbjcn_config(struct sbjcn_channel *ch);
152 1.1.12.2 nathanw void sbjcn_shutdown(struct sbjcn_channel *ch);
153 1.1.12.2 nathanw int sbjcn_speed(long, long *);
154 1.1.12.2 nathanw static int cflag2modes(tcflag_t, u_char *, u_char *);
155 1.1.12.2 nathanw int sbjcn_param(struct tty *, struct termios *);
156 1.1.12.2 nathanw void sbjcn_start(struct tty *);
157 1.1.12.2 nathanw int sbjcn_hwiflow(struct tty *, int);
158 1.1.12.2 nathanw
159 1.1.12.2 nathanw void sbjcn_loadchannelregs(struct sbjcn_channel *);
160 1.1.12.2 nathanw void sbjcn_dohwiflow(struct sbjcn_channel *);
161 1.1.12.2 nathanw void sbjcn_break(struct sbjcn_channel *, int);
162 1.1.12.2 nathanw void sbjcn_modem(struct sbjcn_channel *, int);
163 1.1.12.2 nathanw void tiocm_to_sbjcn(struct sbjcn_channel *, int, int);
164 1.1.12.2 nathanw int sbjcn_to_tiocm(struct sbjcn_channel *);
165 1.1.12.2 nathanw void sbjcn_iflush(struct sbjcn_channel *);
166 1.1.12.2 nathanw
167 1.1.12.2 nathanw int sbjcn_init(u_long addr, int chan, int rate, tcflag_t cflag);
168 1.1.12.2 nathanw int sbjcn_common_getc(u_long addr, int chan);
169 1.1.12.2 nathanw void sbjcn_common_putc(u_long addr, int chan, int c);
170 1.1.12.2 nathanw
171 1.1.12.2 nathanw int sbjcn_cngetc(dev_t dev);
172 1.1.12.2 nathanw void sbjcn_cnputc(dev_t dev, int c);
173 1.1.12.2 nathanw void sbjcn_cnpollc(dev_t dev, int on);
174 1.1.12.2 nathanw
175 1.1.12.2 nathanw extern struct cfdriver sbjcn_cd;
176 1.1.12.2 nathanw
177 1.1.12.2 nathanw dev_type_open(sbjcnopen);
178 1.1.12.2 nathanw dev_type_close(sbjcnclose);
179 1.1.12.2 nathanw dev_type_read(sbjcnread);
180 1.1.12.2 nathanw dev_type_write(sbjcnwrite);
181 1.1.12.2 nathanw dev_type_ioctl(sbjcnioctl);
182 1.1.12.2 nathanw dev_type_stop(sbjcnstop);
183 1.1.12.2 nathanw dev_type_tty(sbjcntty);
184 1.1.12.2 nathanw
185 1.1.12.2 nathanw const struct cdevsw sbjcn_cdevsw = {
186 1.1.12.2 nathanw sbjcnopen, sbjcnclose, sbjcnread, sbjcnwrite, sbjcnioctl,
187 1.1.12.2 nathanw sbjcnstop, sbjcntty, nopoll, nommap, D_TTY
188 1.1.12.2 nathanw };
189 1.1.12.2 nathanw
190 1.1.12.2 nathanw #define integrate static inline
191 1.1.12.2 nathanw integrate void sbjcn_rxsoft(struct sbjcn_channel *, struct tty *);
192 1.1.12.2 nathanw integrate void sbjcn_txsoft(struct sbjcn_channel *, struct tty *);
193 1.1.12.2 nathanw integrate void sbjcn_stsoft(struct sbjcn_channel *, struct tty *);
194 1.1.12.2 nathanw integrate void sbjcn_schedrx(struct sbjcn_channel *);
195 1.1.12.2 nathanw integrate void sbjcn_recv(struct sbjcn_channel *ch);
196 1.1.12.2 nathanw void sbjcn_diag(void *);
197 1.1.12.2 nathanw void sbjcn_callout(void *);
198 1.1.12.2 nathanw
199 1.1.12.2 nathanw /*
200 1.1.12.2 nathanw * Make this an option variable one can patch.
201 1.1.12.2 nathanw * But be warned: this must be a power of 2!
202 1.1.12.2 nathanw */
203 1.1.12.2 nathanw u_int sbjcn_rbuf_size = SBJCN_RING_SIZE;
204 1.1.12.2 nathanw
205 1.1.12.2 nathanw /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
206 1.1.12.2 nathanw u_int sbjcn_rbuf_hiwat = (SBJCN_RING_SIZE * 1) / 4;
207 1.1.12.2 nathanw u_int sbjcn_rbuf_lowat = (SBJCN_RING_SIZE * 3) / 4;
208 1.1.12.2 nathanw
209 1.1.12.2 nathanw static int sbjcn_cons_present;
210 1.1.12.2 nathanw static int sbjcn_cons_attached;
211 1.1.12.2 nathanw static u_long sbjcn_cons_addr;
212 1.1.12.2 nathanw static int sbjcn_cons_chan;
213 1.1.12.2 nathanw static int sbjcn_cons_rate;
214 1.1.12.2 nathanw static tcflag_t sbjcn_cons_cflag;
215 1.1.12.2 nathanw static int sbjcn_cons_waiting_input;
216 1.1.12.2 nathanw static uint64_t sbjcn_cons_input_buf;
217 1.1.12.2 nathanw
218 1.1.12.2 nathanw #ifdef KGDB
219 1.1.12.2 nathanw #include <sys/kgdb.h>
220 1.1.12.2 nathanw
221 1.1.12.2 nathanw static int sbjcn_kgdb_present;
222 1.1.12.2 nathanw static int sbjcn_kgdb_attached;
223 1.1.12.2 nathanw static u_long sbjcn_kgdb_addr;
224 1.1.12.2 nathanw static int sbjcn_kgdb_chan;
225 1.1.12.2 nathanw
226 1.1.12.2 nathanw int sbjcn_kgdb_getc(void *);
227 1.1.12.2 nathanw void sbjcn_kgdb_putc(void *, int);
228 1.1.12.2 nathanw #endif /* KGDB */
229 1.1.12.2 nathanw
230 1.1.12.2 nathanw static int sbjcn_match(struct device *, struct cfdata *, void *);
231 1.1.12.2 nathanw static void sbjcn_attach(struct device *, struct device *, void *);
232 1.1.12.2 nathanw
233 1.1.12.3 nathanw CFATTACH_DECL(sbjcn, sizeof(struct sbjcn_softc),
234 1.1.12.3 nathanw sbjcn_match, sbjcn_attach, NULL, NULL);
235 1.1.12.2 nathanw
236 1.1.12.2 nathanw #define READ_REG(rp) (mips3_ld((uint64_t *)(rp)))
237 1.1.12.2 nathanw #define WRITE_REG(rp, val) (mips3_sd((uint64_t *)(rp), (val)))
238 1.1.12.2 nathanw
239 1.1.12.2 nathanw #define JTAG_CONS_CONTROL 0x00
240 1.1.12.2 nathanw #define JTAG_CONS_INPUT 0x20
241 1.1.12.2 nathanw #define JTAG_CONS_OUTPUT 0x40
242 1.1.12.2 nathanw #define JTAG_CONS_MAGICNUM 0x50FABEEF12349873
243 1.1.12.2 nathanw
244 1.1.12.2 nathanw #define jtag_input_len(data) (((data) >> 56) & 0xFF)
245 1.1.12.2 nathanw
246 1.1.12.2 nathanw
247 1.1.12.2 nathanw static int
248 1.1.12.2 nathanw sbjcn_match(struct device *parent, struct cfdata *match, void *aux)
249 1.1.12.2 nathanw {
250 1.1.12.2 nathanw struct sbscd_attach_args *sap = aux;
251 1.1.12.2 nathanw
252 1.1.12.2 nathanw if (sap->sa_locs.sa_type != SBSCD_DEVTYPE_JTAGCONS)
253 1.1.12.2 nathanw return (0);
254 1.1.12.2 nathanw
255 1.1.12.2 nathanw return 1;
256 1.1.12.2 nathanw }
257 1.1.12.2 nathanw
258 1.1.12.2 nathanw static void
259 1.1.12.2 nathanw sbjcn_attach(struct device *parent, struct device *self, void *aux)
260 1.1.12.2 nathanw {
261 1.1.12.2 nathanw struct sbjcn_softc *sc = (struct sbjcn_softc *)self;
262 1.1.12.2 nathanw struct sbscd_attach_args *sap = aux;
263 1.1.12.2 nathanw
264 1.1.12.2 nathanw sc->sc_addr = sap->sa_base + sap->sa_locs.sa_offset;
265 1.1.12.2 nathanw
266 1.1.12.2 nathanw printf("\n");
267 1.1.12.2 nathanw sbjcn_attach_channel(sc, 0, sap->sa_locs.sa_intr[0]);
268 1.1.12.2 nathanw }
269 1.1.12.2 nathanw
270 1.1.12.2 nathanw void
271 1.1.12.2 nathanw sbjcn_attach_channel(struct sbjcn_softc *sc, int chan, int intr)
272 1.1.12.2 nathanw {
273 1.1.12.2 nathanw struct sbjcn_channel *ch = &sc->sc_channels[chan];
274 1.1.12.2 nathanw u_long chan_addr;
275 1.1.12.2 nathanw struct tty *tp;
276 1.1.12.2 nathanw
277 1.1.12.2 nathanw ch->ch_sc = sc;
278 1.1.12.2 nathanw ch->ch_num = chan;
279 1.1.12.2 nathanw
280 1.1.12.2 nathanw chan_addr = sc->sc_addr + (0x100 * chan);
281 1.1.12.2 nathanw ch->ch_base = (void *)MIPS_PHYS_TO_KSEG1(chan_addr);
282 1.1.12.2 nathanw ch->ch_input_reg =
283 1.1.12.2 nathanw (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + JTAG_CONS_INPUT);
284 1.1.12.2 nathanw ch->ch_output_reg =
285 1.1.12.2 nathanw (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + JTAG_CONS_OUTPUT);
286 1.1.12.2 nathanw ch->ch_control_reg =
287 1.1.12.2 nathanw (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + JTAG_CONS_CONTROL);
288 1.1.12.2 nathanw ch->ch_waiting_input = 0;
289 1.1.12.2 nathanw
290 1.1.12.2 nathanw ch->ch_i_dcd = ch->ch_i_dcd_pin = 0 /* XXXCGD */;
291 1.1.12.2 nathanw ch->ch_i_cts = ch->ch_i_cts_pin = 0 /* XXXCGD */;
292 1.1.12.2 nathanw ch->ch_i_dsr = ch->ch_i_dsr_pin = 0 /* XXXCGD */;
293 1.1.12.2 nathanw ch->ch_i_ri = ch->ch_i_ri_pin = 0 /* XXXCGD */;
294 1.1.12.2 nathanw ch->ch_i_mask =
295 1.1.12.2 nathanw ch->ch_i_dcd | ch->ch_i_cts | ch->ch_i_dsr | ch->ch_i_ri;
296 1.1.12.2 nathanw ch->ch_o_dtr = ch->ch_o_dtr_pin = 0 /* XXXCGD */;
297 1.1.12.2 nathanw ch->ch_o_rts = ch->ch_o_rts_pin = 0 /* XXXCGD */;
298 1.1.12.2 nathanw ch->ch_o_mask = ch->ch_o_dtr | ch->ch_o_rts;
299 1.1.12.2 nathanw
300 1.1.12.2 nathanw callout_init(&ch->ch_diag_callout);
301 1.1.12.2 nathanw callout_init(&ch->ch_callout);
302 1.1.12.2 nathanw
303 1.1.12.2 nathanw /* Disable interrupts before configuring the device. */
304 1.1.12.2 nathanw ch->ch_imr = 0;
305 1.1.12.2 nathanw
306 1.1.12.2 nathanw if (sbjcn_cons_present &&
307 1.1.12.2 nathanw sbjcn_cons_addr == chan_addr && sbjcn_cons_chan == chan) {
308 1.1.12.2 nathanw sbjcn_cons_attached = 1;
309 1.1.12.2 nathanw
310 1.1.12.2 nathanw /* Make sure the console is always "hardwired". */
311 1.1.12.2 nathanw delay(1000); /* wait for output to finish */
312 1.1.12.2 nathanw SET(ch->ch_hwflags, SBJCN_HW_CONSOLE);
313 1.1.12.2 nathanw SET(ch->ch_swflags, TIOCFLAG_SOFTCAR);
314 1.1.12.2 nathanw }
315 1.1.12.2 nathanw
316 1.1.12.2 nathanw tp = ttymalloc();
317 1.1.12.2 nathanw tp->t_oproc = sbjcn_start;
318 1.1.12.2 nathanw tp->t_param = sbjcn_param;
319 1.1.12.2 nathanw tp->t_hwiflow = sbjcn_hwiflow;
320 1.1.12.2 nathanw
321 1.1.12.2 nathanw ch->ch_tty = tp;
322 1.1.12.2 nathanw ch->ch_rbuf = malloc(sbjcn_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
323 1.1.12.2 nathanw if (ch->ch_rbuf == NULL) {
324 1.1.12.2 nathanw printf("%s: channel %d: unable to allocate ring buffer\n",
325 1.1.12.2 nathanw sc->sc_dev.dv_xname, chan);
326 1.1.12.2 nathanw return;
327 1.1.12.2 nathanw }
328 1.1.12.2 nathanw ch->ch_ebuf = ch->ch_rbuf + (sbjcn_rbuf_size << 1);
329 1.1.12.2 nathanw
330 1.1.12.2 nathanw tty_attach(tp);
331 1.1.12.2 nathanw
332 1.1.12.2 nathanw if (ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE)) {
333 1.1.12.2 nathanw int maj;
334 1.1.12.2 nathanw
335 1.1.12.2 nathanw /* locate the major number */
336 1.1.12.2 nathanw maj = cdevsw_lookup_major(&sbjcn_cdevsw);
337 1.1.12.2 nathanw
338 1.1.12.2 nathanw cn_tab->cn_dev = makedev(maj, (sc->sc_dev.dv_unit << 1) + chan);
339 1.1.12.2 nathanw
340 1.1.12.2 nathanw printf("%s: channel %d: console\n", sc->sc_dev.dv_xname, chan);
341 1.1.12.2 nathanw }
342 1.1.12.2 nathanw
343 1.1.12.2 nathanw #ifdef KGDB
344 1.1.12.2 nathanw /*
345 1.1.12.2 nathanw * Allow kgdb to "take over" this port. If this is
346 1.1.12.2 nathanw * the kgdb device, it has exclusive use.
347 1.1.12.2 nathanw */
348 1.1.12.2 nathanw if (sbjcn_kgdb_present &&
349 1.1.12.2 nathanw sbjcn_kgdb_addr == chan_addr && sbjcn_kgdb_chan == chan) {
350 1.1.12.2 nathanw sbjcn_kgdb_attached = 1;
351 1.1.12.2 nathanw
352 1.1.12.2 nathanw SET(ch->ch_hwflags, SBJCN_HW_KGDB);
353 1.1.12.2 nathanw printf("%s: channel %d: kgdb\n", sc->sc_dev.dv_xname, chan);
354 1.1.12.2 nathanw }
355 1.1.12.2 nathanw #endif
356 1.1.12.2 nathanw
357 1.1.12.2 nathanw sbjcn_config(ch);
358 1.1.12.2 nathanw
359 1.1.12.2 nathanw callout_reset(&ch->ch_callout, hz/10, sbjcn_callout, ch);
360 1.1.12.2 nathanw
361 1.1.12.2 nathanw SET(ch->ch_hwflags, SBJCN_HW_DEV_OK);
362 1.1.12.2 nathanw }
363 1.1.12.2 nathanw
364 1.1.12.2 nathanw int
365 1.1.12.2 nathanw sbjcn_speed(long speed, long *brcp)
366 1.1.12.2 nathanw {
367 1.1.12.2 nathanw #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
368 1.1.12.2 nathanw
369 1.1.12.2 nathanw int x, err;
370 1.1.12.2 nathanw int frequency = 100000000;
371 1.1.12.2 nathanw
372 1.1.12.2 nathanw *brcp = divrnd(frequency / 20, speed) - 1;
373 1.1.12.2 nathanw
374 1.1.12.2 nathanw if (speed <= 0)
375 1.1.12.2 nathanw return (-1);
376 1.1.12.2 nathanw x = divrnd(frequency / 20, speed);
377 1.1.12.2 nathanw if (x <= 0)
378 1.1.12.2 nathanw return (-1);
379 1.1.12.2 nathanw err = divrnd(((quad_t)frequency) * 1000 / 20, speed * x) - 1000;
380 1.1.12.2 nathanw if (err < 0)
381 1.1.12.2 nathanw err = -err;
382 1.1.12.2 nathanw if (err > SBJCN_TOLERANCE)
383 1.1.12.2 nathanw return (-1);
384 1.1.12.2 nathanw *brcp = x - 1;
385 1.1.12.2 nathanw return (0);
386 1.1.12.2 nathanw
387 1.1.12.2 nathanw #undef divrnd
388 1.1.12.2 nathanw }
389 1.1.12.2 nathanw
390 1.1.12.2 nathanw #ifdef SBJCN_DEBUG
391 1.1.12.2 nathanw void sbjcn_status(struct sbjcn_channel *, char *);
392 1.1.12.2 nathanw
393 1.1.12.2 nathanw int sbjcn_debug = 1 /* XXXCGD */;
394 1.1.12.2 nathanw
395 1.1.12.2 nathanw void
396 1.1.12.2 nathanw sbjcn_status(struct sbjcn_channel *ch, char *str)
397 1.1.12.2 nathanw {
398 1.1.12.2 nathanw struct sbjcn_softc *sc = ch->ch_sc;
399 1.1.12.2 nathanw struct tty *tp = ch->ch_tty;
400 1.1.12.2 nathanw
401 1.1.12.2 nathanw printf("%s: chan %d: %s %sclocal %sdcd %sts_carr_on %sdtr %stx_stopped\n",
402 1.1.12.2 nathanw sc->sc_dev.dv_xname, ch->ch_num, str,
403 1.1.12.2 nathanw ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
404 1.1.12.2 nathanw ISSET(ch->ch_iports, ch->ch_i_dcd) ? "+" : "-",
405 1.1.12.2 nathanw ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
406 1.1.12.2 nathanw ISSET(ch->ch_oports, ch->ch_o_dtr) ? "+" : "-",
407 1.1.12.2 nathanw ch->ch_tx_stopped ? "+" : "-");
408 1.1.12.2 nathanw
409 1.1.12.2 nathanw printf("%s: chan %d: %s %scrtscts %scts %sts_ttstop %srts %xrx_flags\n",
410 1.1.12.2 nathanw sc->sc_dev.dv_xname, ch->ch_num, str,
411 1.1.12.2 nathanw ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
412 1.1.12.2 nathanw ISSET(ch->ch_iports, ch->ch_i_cts) ? "+" : "-",
413 1.1.12.2 nathanw ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
414 1.1.12.2 nathanw ISSET(ch->ch_oports, ch->ch_o_rts) ? "+" : "-",
415 1.1.12.2 nathanw ch->ch_rx_flags);
416 1.1.12.2 nathanw }
417 1.1.12.2 nathanw #endif
418 1.1.12.2 nathanw
419 1.1.12.2 nathanw #if defined(DDB) || defined(KGDB)
420 1.1.12.2 nathanw static void
421 1.1.12.2 nathanw sbjcn_enable_debugport(struct sbjcn_channel *ch)
422 1.1.12.2 nathanw {
423 1.1.12.2 nathanw int s;
424 1.1.12.2 nathanw
425 1.1.12.2 nathanw /* Turn on line break interrupt, set carrier. */
426 1.1.12.2 nathanw s = splserial();
427 1.1.12.2 nathanw
428 1.1.12.2 nathanw ch->ch_imr = 0x04;
429 1.1.12.2 nathanw SET(ch->ch_oports, ch->ch_o_dtr | ch->ch_o_rts);
430 1.1.12.2 nathanw
431 1.1.12.2 nathanw splx(s);
432 1.1.12.2 nathanw }
433 1.1.12.2 nathanw #endif
434 1.1.12.2 nathanw
435 1.1.12.2 nathanw void
436 1.1.12.2 nathanw sbjcn_config(struct sbjcn_channel *ch)
437 1.1.12.2 nathanw {
438 1.1.12.2 nathanw
439 1.1.12.2 nathanw /* Disable interrupts before configuring the device. */
440 1.1.12.2 nathanw ch->ch_imr = 0x00;
441 1.1.12.2 nathanw
442 1.1.12.2 nathanw #ifdef DDB
443 1.1.12.2 nathanw if (ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE))
444 1.1.12.2 nathanw sbjcn_enable_debugport(ch);
445 1.1.12.2 nathanw #endif
446 1.1.12.2 nathanw
447 1.1.12.2 nathanw #ifdef KGDB
448 1.1.12.2 nathanw /*
449 1.1.12.2 nathanw * Allow kgdb to "take over" this port. If this is
450 1.1.12.2 nathanw * the kgdb device, it has exclusive use.
451 1.1.12.2 nathanw */
452 1.1.12.2 nathanw if (ISSET(ch->ch_hwflags, SBJCN_HW_KGDB))
453 1.1.12.2 nathanw sbjcn_enable_debugport(ch);
454 1.1.12.2 nathanw #endif
455 1.1.12.2 nathanw }
456 1.1.12.2 nathanw
457 1.1.12.2 nathanw void
458 1.1.12.2 nathanw sbjcn_shutdown(struct sbjcn_channel *ch)
459 1.1.12.2 nathanw {
460 1.1.12.2 nathanw struct tty *tp = ch->ch_tty;
461 1.1.12.2 nathanw int s;
462 1.1.12.2 nathanw
463 1.1.12.2 nathanw s = splserial();
464 1.1.12.2 nathanw
465 1.1.12.2 nathanw /* If we were asserting flow control, then deassert it. */
466 1.1.12.2 nathanw SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
467 1.1.12.2 nathanw sbjcn_dohwiflow(ch);
468 1.1.12.2 nathanw
469 1.1.12.2 nathanw /* Clear any break condition set with TIOCSBRK. */
470 1.1.12.2 nathanw sbjcn_break(ch, 0);
471 1.1.12.2 nathanw
472 1.1.12.2 nathanw /*
473 1.1.12.2 nathanw * Hang up if necessary. Wait a bit, so the other side has time to
474 1.1.12.2 nathanw * notice even if we immediately open the port again.
475 1.1.12.2 nathanw */
476 1.1.12.2 nathanw if (ISSET(tp->t_cflag, HUPCL)) {
477 1.1.12.2 nathanw sbjcn_modem(ch, 0);
478 1.1.12.2 nathanw (void) tsleep(ch, TTIPRI, ttclos, hz);
479 1.1.12.2 nathanw }
480 1.1.12.2 nathanw
481 1.1.12.2 nathanw /* Turn off interrupts. */
482 1.1.12.2 nathanw #ifdef DDB
483 1.1.12.2 nathanw if (ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE))
484 1.1.12.2 nathanw ch->ch_imr = 0x04; /* interrupt on break */
485 1.1.12.2 nathanw else
486 1.1.12.2 nathanw #endif
487 1.1.12.2 nathanw ch->ch_imr = 0;
488 1.1.12.2 nathanw
489 1.1.12.2 nathanw splx(s);
490 1.1.12.2 nathanw }
491 1.1.12.2 nathanw
492 1.1.12.2 nathanw int
493 1.1.12.2 nathanw sbjcnopen(dev_t dev, int flag, int mode, struct proc *p)
494 1.1.12.2 nathanw {
495 1.1.12.2 nathanw int unit = SBJCN_UNIT(dev);
496 1.1.12.2 nathanw int chan = SBJCN_CHAN(dev);
497 1.1.12.2 nathanw struct sbjcn_softc *sc;
498 1.1.12.2 nathanw struct sbjcn_channel *ch;
499 1.1.12.2 nathanw struct tty *tp;
500 1.1.12.2 nathanw int s, s2;
501 1.1.12.2 nathanw int error;
502 1.1.12.2 nathanw
503 1.1.12.2 nathanw if (unit >= sbjcn_cd.cd_ndevs)
504 1.1.12.2 nathanw return (ENXIO);
505 1.1.12.2 nathanw sc = sbjcn_cd.cd_devs[unit];
506 1.1.12.2 nathanw if (sc == 0)
507 1.1.12.2 nathanw return (ENXIO);
508 1.1.12.2 nathanw ch = &sc->sc_channels[chan];
509 1.1.12.2 nathanw if (!ISSET(ch->ch_hwflags, SBJCN_HW_DEV_OK) || ch->ch_rbuf == NULL)
510 1.1.12.2 nathanw return (ENXIO);
511 1.1.12.2 nathanw
512 1.1.12.2 nathanw #ifdef KGDB
513 1.1.12.2 nathanw /*
514 1.1.12.2 nathanw * If this is the kgdb port, no other use is permitted.
515 1.1.12.2 nathanw */
516 1.1.12.2 nathanw if (ISSET(ch->ch_hwflags, SBJCN_HW_KGDB))
517 1.1.12.2 nathanw return (EBUSY);
518 1.1.12.2 nathanw #endif
519 1.1.12.2 nathanw
520 1.1.12.2 nathanw tp = ch->ch_tty;
521 1.1.12.2 nathanw
522 1.1.12.2 nathanw if (ISSET(tp->t_state, TS_ISOPEN) &&
523 1.1.12.2 nathanw ISSET(tp->t_state, TS_XCLUDE) &&
524 1.1.12.2 nathanw p->p_ucred->cr_uid != 0)
525 1.1.12.2 nathanw return (EBUSY);
526 1.1.12.2 nathanw
527 1.1.12.2 nathanw s = spltty();
528 1.1.12.2 nathanw
529 1.1.12.2 nathanw /*
530 1.1.12.2 nathanw * Do the following iff this is a first open.
531 1.1.12.2 nathanw */
532 1.1.12.2 nathanw if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
533 1.1.12.2 nathanw struct termios t;
534 1.1.12.2 nathanw
535 1.1.12.2 nathanw tp->t_dev = dev;
536 1.1.12.2 nathanw
537 1.1.12.2 nathanw s2 = splserial();
538 1.1.12.2 nathanw
539 1.1.12.2 nathanw /* Turn on receive, break, and status change interrupts. */
540 1.1.12.2 nathanw ch->ch_imr = 0xe;
541 1.1.12.2 nathanw
542 1.1.12.2 nathanw /* Fetch the current modem control status, needed later. */
543 1.1.12.2 nathanw ch->ch_iports = 0;
544 1.1.12.2 nathanw ch->ch_iports_delta = 0;
545 1.1.12.2 nathanw splx(s2);
546 1.1.12.2 nathanw
547 1.1.12.2 nathanw /*
548 1.1.12.2 nathanw * Initialize the termios status to the defaults. Add in the
549 1.1.12.2 nathanw * sticky bits from TIOCSFLAGS.
550 1.1.12.2 nathanw */
551 1.1.12.2 nathanw t.c_ispeed = 0;
552 1.1.12.2 nathanw if (ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE)) {
553 1.1.12.2 nathanw t.c_ospeed = sbjcn_cons_rate;
554 1.1.12.2 nathanw t.c_cflag = sbjcn_cons_cflag;
555 1.1.12.2 nathanw } else {
556 1.1.12.2 nathanw t.c_ospeed = TTYDEF_SPEED;
557 1.1.12.2 nathanw t.c_cflag = TTYDEF_CFLAG;
558 1.1.12.2 nathanw }
559 1.1.12.2 nathanw if (ISSET(ch->ch_swflags, TIOCFLAG_CLOCAL))
560 1.1.12.2 nathanw SET(t.c_cflag, CLOCAL);
561 1.1.12.2 nathanw if (ISSET(ch->ch_swflags, TIOCFLAG_CRTSCTS))
562 1.1.12.2 nathanw SET(t.c_cflag, CRTSCTS);
563 1.1.12.2 nathanw if (ISSET(ch->ch_swflags, TIOCFLAG_MDMBUF))
564 1.1.12.2 nathanw SET(t.c_cflag, MDMBUF);
565 1.1.12.2 nathanw /* Make sure sbjcn_param() will do something. */
566 1.1.12.2 nathanw tp->t_ospeed = 0;
567 1.1.12.2 nathanw (void) sbjcn_param(tp, &t);
568 1.1.12.2 nathanw tp->t_iflag = TTYDEF_IFLAG;
569 1.1.12.2 nathanw tp->t_oflag = TTYDEF_OFLAG;
570 1.1.12.2 nathanw tp->t_lflag = TTYDEF_LFLAG;
571 1.1.12.2 nathanw ttychars(tp);
572 1.1.12.2 nathanw ttsetwater(tp);
573 1.1.12.2 nathanw
574 1.1.12.2 nathanw s2 = splserial();
575 1.1.12.2 nathanw
576 1.1.12.2 nathanw /*
577 1.1.12.2 nathanw * Turn on DTR. We must always do this, even if carrier is not
578 1.1.12.2 nathanw * present, because otherwise we'd have to use TIOCSDTR
579 1.1.12.2 nathanw * immediately after setting CLOCAL, which applications do not
580 1.1.12.2 nathanw * expect. We always assert DTR while the device is open
581 1.1.12.2 nathanw * unless explicitly requested to deassert it.
582 1.1.12.2 nathanw */
583 1.1.12.2 nathanw sbjcn_modem(ch, 1);
584 1.1.12.2 nathanw
585 1.1.12.2 nathanw /* Clear the input ring, and unblock. */
586 1.1.12.2 nathanw ch->ch_rbput = ch->ch_rbget = ch->ch_rbuf;
587 1.1.12.2 nathanw ch->ch_rbavail = sbjcn_rbuf_size;
588 1.1.12.2 nathanw sbjcn_iflush(ch);
589 1.1.12.2 nathanw CLR(ch->ch_rx_flags, RX_ANY_BLOCK);
590 1.1.12.2 nathanw sbjcn_dohwiflow(ch);
591 1.1.12.2 nathanw
592 1.1.12.2 nathanw #ifdef SBJCN_DEBUG
593 1.1.12.2 nathanw if (sbjcn_debug)
594 1.1.12.2 nathanw sbjcn_status(ch, "sbjcnopen ");
595 1.1.12.2 nathanw #endif
596 1.1.12.2 nathanw
597 1.1.12.2 nathanw splx(s2);
598 1.1.12.2 nathanw }
599 1.1.12.2 nathanw
600 1.1.12.2 nathanw splx(s);
601 1.1.12.2 nathanw
602 1.1.12.2 nathanw error = ttyopen(tp, SBJCN_DIALOUT(dev), ISSET(flag, O_NONBLOCK));
603 1.1.12.2 nathanw if (error)
604 1.1.12.2 nathanw goto bad;
605 1.1.12.2 nathanw
606 1.1.12.2 nathanw error = (*tp->t_linesw->l_open)(dev, tp);
607 1.1.12.2 nathanw if (error)
608 1.1.12.2 nathanw goto bad;
609 1.1.12.2 nathanw
610 1.1.12.2 nathanw return (0);
611 1.1.12.2 nathanw
612 1.1.12.2 nathanw bad:
613 1.1.12.2 nathanw if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
614 1.1.12.2 nathanw /*
615 1.1.12.2 nathanw * We failed to open the device, and nobody else had it opened.
616 1.1.12.2 nathanw * Clean up the state as appropriate.
617 1.1.12.2 nathanw */
618 1.1.12.2 nathanw sbjcn_shutdown(ch);
619 1.1.12.2 nathanw }
620 1.1.12.2 nathanw
621 1.1.12.2 nathanw return (error);
622 1.1.12.2 nathanw }
623 1.1.12.2 nathanw
624 1.1.12.2 nathanw int
625 1.1.12.2 nathanw sbjcnclose(dev_t dev, int flag, int mode, struct proc *p)
626 1.1.12.2 nathanw {
627 1.1.12.2 nathanw struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(dev)];
628 1.1.12.2 nathanw struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
629 1.1.12.2 nathanw struct tty *tp = ch->ch_tty;
630 1.1.12.2 nathanw
631 1.1.12.2 nathanw /* XXX This is for cons.c. */
632 1.1.12.2 nathanw if (!ISSET(tp->t_state, TS_ISOPEN))
633 1.1.12.2 nathanw return (0);
634 1.1.12.2 nathanw
635 1.1.12.2 nathanw (*tp->t_linesw->l_close)(tp, flag);
636 1.1.12.2 nathanw ttyclose(tp);
637 1.1.12.2 nathanw
638 1.1.12.2 nathanw if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
639 1.1.12.2 nathanw /*
640 1.1.12.2 nathanw * Although we got a last close, the device may still be in
641 1.1.12.2 nathanw * use; e.g. if this was the dialout node, and there are still
642 1.1.12.2 nathanw * processes waiting for carrier on the non-dialout node.
643 1.1.12.2 nathanw */
644 1.1.12.2 nathanw sbjcn_shutdown(ch);
645 1.1.12.2 nathanw }
646 1.1.12.2 nathanw
647 1.1.12.2 nathanw return (0);
648 1.1.12.2 nathanw }
649 1.1.12.2 nathanw
650 1.1.12.2 nathanw int
651 1.1.12.2 nathanw sbjcnread(dev_t dev, struct uio *uio, int flag)
652 1.1.12.2 nathanw {
653 1.1.12.2 nathanw struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(dev)];
654 1.1.12.2 nathanw struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
655 1.1.12.2 nathanw struct tty *tp = ch->ch_tty;
656 1.1.12.2 nathanw
657 1.1.12.2 nathanw return ((*tp->t_linesw->l_read)(tp, uio, flag));
658 1.1.12.2 nathanw }
659 1.1.12.2 nathanw
660 1.1.12.2 nathanw int
661 1.1.12.2 nathanw sbjcnwrite(dev_t dev, struct uio *uio, int flag)
662 1.1.12.2 nathanw {
663 1.1.12.2 nathanw struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(dev)];
664 1.1.12.2 nathanw struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
665 1.1.12.2 nathanw struct tty *tp = ch->ch_tty;
666 1.1.12.2 nathanw
667 1.1.12.2 nathanw return ((*tp->t_linesw->l_write)(tp, uio, flag));
668 1.1.12.2 nathanw }
669 1.1.12.2 nathanw
670 1.1.12.2 nathanw struct tty *
671 1.1.12.2 nathanw sbjcntty(dev_t dev)
672 1.1.12.2 nathanw {
673 1.1.12.2 nathanw struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(dev)];
674 1.1.12.2 nathanw struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
675 1.1.12.2 nathanw struct tty *tp = ch->ch_tty;
676 1.1.12.2 nathanw
677 1.1.12.2 nathanw return (tp);
678 1.1.12.2 nathanw }
679 1.1.12.2 nathanw
680 1.1.12.2 nathanw int
681 1.1.12.2 nathanw sbjcnioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
682 1.1.12.2 nathanw {
683 1.1.12.2 nathanw struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(dev)];
684 1.1.12.2 nathanw struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
685 1.1.12.2 nathanw struct tty *tp = ch->ch_tty;
686 1.1.12.2 nathanw int error;
687 1.1.12.2 nathanw int s;
688 1.1.12.2 nathanw
689 1.1.12.2 nathanw error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
690 1.1.12.2 nathanw if (error >= 0)
691 1.1.12.2 nathanw return (error);
692 1.1.12.2 nathanw
693 1.1.12.2 nathanw error = ttioctl(tp, cmd, data, flag, p);
694 1.1.12.2 nathanw if (error >= 0)
695 1.1.12.2 nathanw return (error);
696 1.1.12.2 nathanw
697 1.1.12.2 nathanw error = 0;
698 1.1.12.2 nathanw
699 1.1.12.2 nathanw s = splserial();
700 1.1.12.2 nathanw
701 1.1.12.2 nathanw switch (cmd) {
702 1.1.12.2 nathanw case TIOCSBRK:
703 1.1.12.2 nathanw sbjcn_break(ch, 1);
704 1.1.12.2 nathanw break;
705 1.1.12.2 nathanw
706 1.1.12.2 nathanw case TIOCCBRK:
707 1.1.12.2 nathanw sbjcn_break(ch, 0);
708 1.1.12.2 nathanw break;
709 1.1.12.2 nathanw
710 1.1.12.2 nathanw case TIOCSDTR:
711 1.1.12.2 nathanw sbjcn_modem(ch, 1);
712 1.1.12.2 nathanw break;
713 1.1.12.2 nathanw
714 1.1.12.2 nathanw case TIOCCDTR:
715 1.1.12.2 nathanw sbjcn_modem(ch, 0);
716 1.1.12.2 nathanw break;
717 1.1.12.2 nathanw
718 1.1.12.2 nathanw case TIOCGFLAGS:
719 1.1.12.2 nathanw *(int *)data = ch->ch_swflags;
720 1.1.12.2 nathanw break;
721 1.1.12.2 nathanw
722 1.1.12.2 nathanw case TIOCSFLAGS:
723 1.1.12.2 nathanw error = suser(p->p_ucred, &p->p_acflag);
724 1.1.12.2 nathanw if (error)
725 1.1.12.2 nathanw break;
726 1.1.12.2 nathanw ch->ch_swflags = *(int *)data;
727 1.1.12.2 nathanw break;
728 1.1.12.2 nathanw
729 1.1.12.2 nathanw case TIOCMSET:
730 1.1.12.2 nathanw case TIOCMBIS:
731 1.1.12.2 nathanw case TIOCMBIC:
732 1.1.12.2 nathanw tiocm_to_sbjcn(ch, cmd, *(int *)data);
733 1.1.12.2 nathanw break;
734 1.1.12.2 nathanw
735 1.1.12.2 nathanw case TIOCMGET:
736 1.1.12.2 nathanw *(int *)data = sbjcn_to_tiocm(ch);
737 1.1.12.2 nathanw break;
738 1.1.12.2 nathanw
739 1.1.12.2 nathanw default:
740 1.1.12.2 nathanw error = ENOTTY;
741 1.1.12.2 nathanw break;
742 1.1.12.2 nathanw }
743 1.1.12.2 nathanw
744 1.1.12.2 nathanw splx(s);
745 1.1.12.2 nathanw
746 1.1.12.2 nathanw #ifdef SBJCN_DEBUG
747 1.1.12.2 nathanw if (sbjcn_debug)
748 1.1.12.2 nathanw sbjcn_status(ch, "sbjcn_ioctl ");
749 1.1.12.2 nathanw #endif
750 1.1.12.2 nathanw
751 1.1.12.2 nathanw return (error);
752 1.1.12.2 nathanw }
753 1.1.12.2 nathanw
754 1.1.12.2 nathanw integrate void
755 1.1.12.2 nathanw sbjcn_schedrx(struct sbjcn_channel *ch)
756 1.1.12.2 nathanw {
757 1.1.12.2 nathanw
758 1.1.12.2 nathanw ch->ch_rx_ready = 1;
759 1.1.12.2 nathanw
760 1.1.12.2 nathanw /* Next callout will detect this flag. */
761 1.1.12.2 nathanw }
762 1.1.12.2 nathanw
763 1.1.12.2 nathanw void
764 1.1.12.2 nathanw sbjcn_break(struct sbjcn_channel *ch, int onoff)
765 1.1.12.2 nathanw {
766 1.1.12.2 nathanw /* XXXKW do something? */
767 1.1.12.2 nathanw }
768 1.1.12.2 nathanw
769 1.1.12.2 nathanw void
770 1.1.12.2 nathanw sbjcn_modem(struct sbjcn_channel *ch, int onoff)
771 1.1.12.2 nathanw {
772 1.1.12.2 nathanw
773 1.1.12.2 nathanw if (ch->ch_o_dtr == 0)
774 1.1.12.2 nathanw return;
775 1.1.12.2 nathanw
776 1.1.12.2 nathanw if (onoff)
777 1.1.12.2 nathanw SET(ch->ch_oports, ch->ch_o_dtr);
778 1.1.12.2 nathanw else
779 1.1.12.2 nathanw CLR(ch->ch_oports, ch->ch_o_dtr);
780 1.1.12.2 nathanw
781 1.1.12.2 nathanw if (!ch->ch_heldchange) {
782 1.1.12.2 nathanw if (ch->ch_tx_busy) {
783 1.1.12.2 nathanw ch->ch_heldtbc = ch->ch_tbc;
784 1.1.12.2 nathanw ch->ch_tbc = 0;
785 1.1.12.2 nathanw ch->ch_heldchange = 1;
786 1.1.12.2 nathanw } else
787 1.1.12.2 nathanw sbjcn_loadchannelregs(ch);
788 1.1.12.2 nathanw }
789 1.1.12.2 nathanw }
790 1.1.12.2 nathanw
791 1.1.12.2 nathanw void
792 1.1.12.2 nathanw tiocm_to_sbjcn(struct sbjcn_channel *ch, int how, int ttybits)
793 1.1.12.2 nathanw {
794 1.1.12.2 nathanw u_char bits;
795 1.1.12.2 nathanw
796 1.1.12.2 nathanw bits = 0;
797 1.1.12.2 nathanw if (ISSET(ttybits, TIOCM_DTR))
798 1.1.12.2 nathanw SET(bits, ch->ch_o_dtr);
799 1.1.12.2 nathanw if (ISSET(ttybits, TIOCM_RTS))
800 1.1.12.2 nathanw SET(bits, ch->ch_o_rts);
801 1.1.12.2 nathanw
802 1.1.12.2 nathanw switch (how) {
803 1.1.12.2 nathanw case TIOCMBIC:
804 1.1.12.2 nathanw CLR(ch->ch_oports, bits);
805 1.1.12.2 nathanw break;
806 1.1.12.2 nathanw
807 1.1.12.2 nathanw case TIOCMBIS:
808 1.1.12.2 nathanw SET(ch->ch_oports, bits);
809 1.1.12.2 nathanw break;
810 1.1.12.2 nathanw
811 1.1.12.2 nathanw case TIOCMSET:
812 1.1.12.2 nathanw ch->ch_oports = bits;
813 1.1.12.2 nathanw break;
814 1.1.12.2 nathanw }
815 1.1.12.2 nathanw
816 1.1.12.2 nathanw if (!ch->ch_heldchange) {
817 1.1.12.2 nathanw if (ch->ch_tx_busy) {
818 1.1.12.2 nathanw ch->ch_heldtbc = ch->ch_tbc;
819 1.1.12.2 nathanw ch->ch_tbc = 0;
820 1.1.12.2 nathanw ch->ch_heldchange = 1;
821 1.1.12.2 nathanw } else
822 1.1.12.2 nathanw sbjcn_loadchannelregs(ch);
823 1.1.12.2 nathanw }
824 1.1.12.2 nathanw }
825 1.1.12.2 nathanw
826 1.1.12.2 nathanw int
827 1.1.12.2 nathanw sbjcn_to_tiocm(struct sbjcn_channel *ch)
828 1.1.12.2 nathanw {
829 1.1.12.2 nathanw u_char hwbits;
830 1.1.12.2 nathanw int ttybits = 0;
831 1.1.12.2 nathanw
832 1.1.12.2 nathanw hwbits = ch->ch_oports;
833 1.1.12.2 nathanw if (ISSET(hwbits, ch->ch_o_dtr))
834 1.1.12.2 nathanw SET(ttybits, TIOCM_DTR);
835 1.1.12.2 nathanw if (ISSET(hwbits, ch->ch_o_rts))
836 1.1.12.2 nathanw SET(ttybits, TIOCM_RTS);
837 1.1.12.2 nathanw
838 1.1.12.2 nathanw hwbits = ch->ch_iports;
839 1.1.12.2 nathanw if (ISSET(hwbits, ch->ch_i_dcd))
840 1.1.12.2 nathanw SET(ttybits, TIOCM_CD);
841 1.1.12.2 nathanw if (ISSET(hwbits, ch->ch_i_cts))
842 1.1.12.2 nathanw SET(ttybits, TIOCM_CTS);
843 1.1.12.2 nathanw if (ISSET(hwbits, ch->ch_i_dsr))
844 1.1.12.2 nathanw SET(ttybits, TIOCM_DSR);
845 1.1.12.2 nathanw if (ISSET(hwbits, ch->ch_i_ri))
846 1.1.12.2 nathanw SET(ttybits, TIOCM_RI);
847 1.1.12.2 nathanw
848 1.1.12.2 nathanw if (ch->ch_imr != 0)
849 1.1.12.2 nathanw SET(ttybits, TIOCM_LE);
850 1.1.12.2 nathanw
851 1.1.12.2 nathanw return (ttybits);
852 1.1.12.2 nathanw }
853 1.1.12.2 nathanw
854 1.1.12.2 nathanw static int
855 1.1.12.2 nathanw cflag2modes(cflag, mode1p, mode2p)
856 1.1.12.2 nathanw tcflag_t cflag;
857 1.1.12.2 nathanw u_char *mode1p;
858 1.1.12.2 nathanw u_char *mode2p;
859 1.1.12.2 nathanw {
860 1.1.12.2 nathanw u_char mode1;
861 1.1.12.2 nathanw u_char mode2;
862 1.1.12.2 nathanw int err = 0;
863 1.1.12.2 nathanw
864 1.1.12.2 nathanw mode1 = mode2 = 0;
865 1.1.12.2 nathanw
866 1.1.12.2 nathanw switch (ISSET(cflag, CSIZE)) {
867 1.1.12.2 nathanw case CS7:
868 1.1.12.2 nathanw mode1 |= 2; /* XXX */
869 1.1.12.2 nathanw break;
870 1.1.12.2 nathanw default:
871 1.1.12.2 nathanw err = -1;
872 1.1.12.2 nathanw /* FALLTHRU for sanity */
873 1.1.12.2 nathanw case CS8:
874 1.1.12.2 nathanw mode1 |= 3; /* XXX */
875 1.1.12.2 nathanw break;
876 1.1.12.2 nathanw }
877 1.1.12.2 nathanw if (!ISSET(cflag, PARENB))
878 1.1.12.2 nathanw mode1 |= 2 << 3;
879 1.1.12.2 nathanw else {
880 1.1.12.2 nathanw mode1 |= 0 << 3;
881 1.1.12.2 nathanw if (ISSET(cflag, PARODD))
882 1.1.12.2 nathanw mode1 |= 1 << 2;
883 1.1.12.2 nathanw }
884 1.1.12.2 nathanw
885 1.1.12.2 nathanw if (ISSET(cflag, CSTOPB))
886 1.1.12.2 nathanw mode2 |= 1 << 3; /* two stop bits XXX not std */
887 1.1.12.2 nathanw
888 1.1.12.2 nathanw if (ISSET(cflag, CRTSCTS)) {
889 1.1.12.2 nathanw mode1 |= 1 << 7;
890 1.1.12.2 nathanw mode2 |= 1 << 4;
891 1.1.12.2 nathanw }
892 1.1.12.2 nathanw
893 1.1.12.2 nathanw *mode1p = mode1;
894 1.1.12.2 nathanw *mode2p = mode2;
895 1.1.12.2 nathanw return (err);
896 1.1.12.2 nathanw }
897 1.1.12.2 nathanw
898 1.1.12.2 nathanw int
899 1.1.12.2 nathanw sbjcn_param(struct tty *tp, struct termios *t)
900 1.1.12.2 nathanw {
901 1.1.12.2 nathanw struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(tp->t_dev)];
902 1.1.12.2 nathanw struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
903 1.1.12.2 nathanw long brc;
904 1.1.12.2 nathanw u_char mode1, mode2;
905 1.1.12.2 nathanw int s;
906 1.1.12.2 nathanw
907 1.1.12.2 nathanw /* XXX reset to console parameters if console? */
908 1.1.12.2 nathanw #if 0
909 1.1.12.2 nathanw XXX disable, enable.
910 1.1.12.2 nathanw #endif
911 1.1.12.2 nathanw
912 1.1.12.2 nathanw /* Check requested parameters. */
913 1.1.12.2 nathanw if (sbjcn_speed(t->c_ospeed, &brc) < 0)
914 1.1.12.2 nathanw return (EINVAL);
915 1.1.12.2 nathanw if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
916 1.1.12.2 nathanw return (EINVAL);
917 1.1.12.2 nathanw
918 1.1.12.2 nathanw /*
919 1.1.12.2 nathanw * For the console, always force CLOCAL and !HUPCL, so that the port
920 1.1.12.2 nathanw * is always active.
921 1.1.12.2 nathanw */
922 1.1.12.2 nathanw if (ISSET(ch->ch_swflags, TIOCFLAG_SOFTCAR) ||
923 1.1.12.2 nathanw ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE)) {
924 1.1.12.2 nathanw SET(t->c_cflag, CLOCAL);
925 1.1.12.2 nathanw CLR(t->c_cflag, HUPCL);
926 1.1.12.2 nathanw }
927 1.1.12.2 nathanw
928 1.1.12.2 nathanw /*
929 1.1.12.2 nathanw * If there were no changes, don't do anything. This avoids dropping
930 1.1.12.2 nathanw * input and improves performance when all we did was frob things like
931 1.1.12.2 nathanw * VMIN and VTIME.
932 1.1.12.2 nathanw */
933 1.1.12.2 nathanw if (tp->t_ospeed == t->c_ospeed &&
934 1.1.12.2 nathanw tp->t_cflag == t->c_cflag)
935 1.1.12.2 nathanw return (0);
936 1.1.12.2 nathanw
937 1.1.12.2 nathanw if (cflag2modes(t->c_cflag, &mode1, &mode2) < 0)
938 1.1.12.2 nathanw return (EINVAL);
939 1.1.12.2 nathanw
940 1.1.12.2 nathanw s = splserial();
941 1.1.12.2 nathanw
942 1.1.12.2 nathanw ch->ch_mode1 = mode1;
943 1.1.12.2 nathanw ch->ch_mode2 = mode2;
944 1.1.12.2 nathanw
945 1.1.12.2 nathanw /*
946 1.1.12.2 nathanw * If we're not in a mode that assumes a connection is present, then
947 1.1.12.2 nathanw * ignore carrier changes.
948 1.1.12.2 nathanw */
949 1.1.12.2 nathanw if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
950 1.1.12.2 nathanw ch->ch_i_dcd = 0;
951 1.1.12.2 nathanw else
952 1.1.12.2 nathanw ch->ch_i_dcd = ch->ch_i_dcd_pin;
953 1.1.12.2 nathanw /*
954 1.1.12.2 nathanw * Set the flow control pins depending on the current flow control
955 1.1.12.2 nathanw * mode.
956 1.1.12.2 nathanw */
957 1.1.12.2 nathanw if (ISSET(t->c_cflag, CRTSCTS)) {
958 1.1.12.2 nathanw ch->ch_o_dtr = ch->ch_o_dtr_pin;
959 1.1.12.2 nathanw ch->ch_o_rts = ch->ch_o_rts_pin;
960 1.1.12.2 nathanw ch->ch_i_cts = ch->ch_i_cts_pin;
961 1.1.12.2 nathanw /* hw controle enable bits in mod regs set by cflag2modes */
962 1.1.12.2 nathanw } else if (ISSET(t->c_cflag, MDMBUF)) {
963 1.1.12.2 nathanw /*
964 1.1.12.2 nathanw * For DTR/DCD flow control, make sure we don't toggle DTR for
965 1.1.12.2 nathanw * carrier detection.
966 1.1.12.2 nathanw */
967 1.1.12.2 nathanw ch->ch_o_dtr = 0;
968 1.1.12.2 nathanw ch->ch_o_rts = ch->ch_o_dtr_pin;
969 1.1.12.2 nathanw ch->ch_i_cts = ch->ch_i_dcd_pin;
970 1.1.12.2 nathanw } else {
971 1.1.12.2 nathanw /*
972 1.1.12.2 nathanw * If no flow control, then always set RTS. This will make
973 1.1.12.2 nathanw * the other side happy if it mistakenly thinks we're doing
974 1.1.12.2 nathanw * RTS/CTS flow control.
975 1.1.12.2 nathanw */
976 1.1.12.2 nathanw ch->ch_o_dtr = ch->ch_o_dtr_pin | ch->ch_o_rts_pin;
977 1.1.12.2 nathanw ch->ch_o_rts = 0;
978 1.1.12.2 nathanw ch->ch_i_cts = 0;
979 1.1.12.2 nathanw if (ISSET(ch->ch_oports, ch->ch_o_dtr_pin))
980 1.1.12.2 nathanw SET(ch->ch_oports, ch->ch_o_rts_pin);
981 1.1.12.2 nathanw else
982 1.1.12.2 nathanw CLR(ch->ch_oports, ch->ch_o_rts_pin);
983 1.1.12.2 nathanw }
984 1.1.12.2 nathanw /* XXX maybe mask the ports which generate intrs? */
985 1.1.12.2 nathanw
986 1.1.12.2 nathanw ch->ch_brc = brc;
987 1.1.12.2 nathanw
988 1.1.12.2 nathanw /* XXX maybe set fifo-full receive mode if RTSCTS and high speed? */
989 1.1.12.2 nathanw
990 1.1.12.2 nathanw /* And copy to tty. */
991 1.1.12.2 nathanw tp->t_ispeed = 0;
992 1.1.12.2 nathanw tp->t_ospeed = t->c_ospeed;
993 1.1.12.2 nathanw tp->t_cflag = t->c_cflag;
994 1.1.12.2 nathanw
995 1.1.12.2 nathanw if (!ch->ch_heldchange) {
996 1.1.12.2 nathanw if (ch->ch_tx_busy) {
997 1.1.12.2 nathanw ch->ch_heldtbc = ch->ch_tbc;
998 1.1.12.2 nathanw ch->ch_tbc = 0;
999 1.1.12.2 nathanw ch->ch_heldchange = 1;
1000 1.1.12.2 nathanw } else
1001 1.1.12.2 nathanw sbjcn_loadchannelregs(ch);
1002 1.1.12.2 nathanw }
1003 1.1.12.2 nathanw
1004 1.1.12.2 nathanw if (!ISSET(t->c_cflag, CHWFLOW)) {
1005 1.1.12.2 nathanw /* Disable the high water mark. */
1006 1.1.12.2 nathanw ch->ch_r_hiwat = 0;
1007 1.1.12.2 nathanw ch->ch_r_lowat = 0;
1008 1.1.12.2 nathanw if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
1009 1.1.12.2 nathanw CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
1010 1.1.12.2 nathanw sbjcn_schedrx(ch);
1011 1.1.12.2 nathanw }
1012 1.1.12.2 nathanw if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1013 1.1.12.2 nathanw CLR(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1014 1.1.12.2 nathanw sbjcn_dohwiflow(ch);
1015 1.1.12.2 nathanw }
1016 1.1.12.2 nathanw } else {
1017 1.1.12.2 nathanw ch->ch_r_hiwat = sbjcn_rbuf_hiwat;
1018 1.1.12.2 nathanw ch->ch_r_lowat = sbjcn_rbuf_lowat;
1019 1.1.12.2 nathanw }
1020 1.1.12.2 nathanw
1021 1.1.12.2 nathanw splx(s);
1022 1.1.12.2 nathanw
1023 1.1.12.2 nathanw /*
1024 1.1.12.2 nathanw * Update the tty layer's idea of the carrier bit, in case we changed
1025 1.1.12.2 nathanw * CLOCAL or MDMBUF. We don't hang up here; we only do that by
1026 1.1.12.2 nathanw * explicit request.
1027 1.1.12.2 nathanw */
1028 1.1.12.2 nathanw (void) (*tp->t_linesw->l_modem)(tp,
1029 1.1.12.2 nathanw ISSET(ch->ch_iports, ch->ch_i_dcd));
1030 1.1.12.2 nathanw
1031 1.1.12.2 nathanw #ifdef SBJCN_DEBUG
1032 1.1.12.2 nathanw if (sbjcn_debug)
1033 1.1.12.2 nathanw sbjcn_status(ch, "sbjcnparam ");
1034 1.1.12.2 nathanw #endif
1035 1.1.12.2 nathanw
1036 1.1.12.2 nathanw if (!ISSET(t->c_cflag, CHWFLOW)) {
1037 1.1.12.2 nathanw if (ch->ch_tx_stopped) {
1038 1.1.12.2 nathanw ch->ch_tx_stopped = 0;
1039 1.1.12.2 nathanw sbjcn_start(tp);
1040 1.1.12.2 nathanw }
1041 1.1.12.2 nathanw }
1042 1.1.12.2 nathanw
1043 1.1.12.2 nathanw return (0);
1044 1.1.12.2 nathanw }
1045 1.1.12.2 nathanw
1046 1.1.12.2 nathanw void
1047 1.1.12.2 nathanw sbjcn_iflush(struct sbjcn_channel *ch)
1048 1.1.12.2 nathanw {
1049 1.1.12.2 nathanw uint64_t reg;
1050 1.1.12.2 nathanw int timo;
1051 1.1.12.2 nathanw
1052 1.1.12.2 nathanw timo = 50000;
1053 1.1.12.2 nathanw /* flush any pending I/O */
1054 1.1.12.2 nathanw while (((reg = READ_REG(ch->ch_input_reg)) != 0) && --timo)
1055 1.1.12.2 nathanw ;
1056 1.1.12.2 nathanw
1057 1.1.12.2 nathanw #ifdef DIAGNOSTIC
1058 1.1.12.2 nathanw if (!timo)
1059 1.1.12.2 nathanw printf("%s: sbjcn_iflush timeout %02x\n",
1060 1.1.12.2 nathanw ch->ch_sc->sc_dev.dv_xname, reg);
1061 1.1.12.2 nathanw #endif
1062 1.1.12.2 nathanw }
1063 1.1.12.2 nathanw
1064 1.1.12.2 nathanw void
1065 1.1.12.2 nathanw sbjcn_loadchannelregs(struct sbjcn_channel *ch)
1066 1.1.12.2 nathanw {
1067 1.1.12.2 nathanw }
1068 1.1.12.2 nathanw
1069 1.1.12.2 nathanw int
1070 1.1.12.2 nathanw sbjcn_hwiflow(struct tty *tp, int block)
1071 1.1.12.2 nathanw {
1072 1.1.12.2 nathanw struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(tp->t_dev)];
1073 1.1.12.2 nathanw struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
1074 1.1.12.2 nathanw int s;
1075 1.1.12.2 nathanw
1076 1.1.12.2 nathanw if (ch->ch_o_rts == 0)
1077 1.1.12.2 nathanw return (0);
1078 1.1.12.2 nathanw
1079 1.1.12.2 nathanw s = splserial();
1080 1.1.12.2 nathanw if (block) {
1081 1.1.12.2 nathanw if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
1082 1.1.12.2 nathanw SET(ch->ch_rx_flags, RX_TTY_BLOCKED);
1083 1.1.12.2 nathanw sbjcn_dohwiflow(ch);
1084 1.1.12.2 nathanw }
1085 1.1.12.2 nathanw } else {
1086 1.1.12.2 nathanw if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
1087 1.1.12.2 nathanw CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
1088 1.1.12.2 nathanw sbjcn_schedrx(ch);
1089 1.1.12.2 nathanw }
1090 1.1.12.2 nathanw if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
1091 1.1.12.2 nathanw CLR(ch->ch_rx_flags, RX_TTY_BLOCKED);
1092 1.1.12.2 nathanw sbjcn_dohwiflow(ch);
1093 1.1.12.2 nathanw }
1094 1.1.12.2 nathanw }
1095 1.1.12.2 nathanw splx(s);
1096 1.1.12.2 nathanw return (1);
1097 1.1.12.2 nathanw }
1098 1.1.12.2 nathanw
1099 1.1.12.2 nathanw /*
1100 1.1.12.2 nathanw * (un)block input via hw flowcontrol
1101 1.1.12.2 nathanw */
1102 1.1.12.2 nathanw void
1103 1.1.12.2 nathanw sbjcn_dohwiflow(struct sbjcn_channel *ch)
1104 1.1.12.2 nathanw {
1105 1.1.12.2 nathanw
1106 1.1.12.2 nathanw if (ch->ch_o_rts == 0)
1107 1.1.12.2 nathanw return;
1108 1.1.12.2 nathanw
1109 1.1.12.2 nathanw if (ISSET(ch->ch_rx_flags, RX_ANY_BLOCK)) {
1110 1.1.12.2 nathanw CLR(ch->ch_oports, ch->ch_o_rts);
1111 1.1.12.2 nathanw CLR(ch->ch_oports_active, ch->ch_o_rts);
1112 1.1.12.2 nathanw } else {
1113 1.1.12.2 nathanw SET(ch->ch_oports, ch->ch_o_rts);
1114 1.1.12.2 nathanw SET(ch->ch_oports_active, ch->ch_o_rts);
1115 1.1.12.2 nathanw }
1116 1.1.12.2 nathanw }
1117 1.1.12.2 nathanw
1118 1.1.12.2 nathanw void
1119 1.1.12.2 nathanw sbjcn_start(struct tty *tp)
1120 1.1.12.2 nathanw {
1121 1.1.12.2 nathanw struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(tp->t_dev)];
1122 1.1.12.2 nathanw struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
1123 1.1.12.2 nathanw int s;
1124 1.1.12.2 nathanw
1125 1.1.12.2 nathanw s = spltty();
1126 1.1.12.2 nathanw if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1127 1.1.12.2 nathanw goto out;
1128 1.1.12.2 nathanw if (ch->ch_tx_stopped)
1129 1.1.12.2 nathanw goto out;
1130 1.1.12.2 nathanw
1131 1.1.12.2 nathanw if (tp->t_outq.c_cc <= tp->t_lowat) {
1132 1.1.12.2 nathanw if (ISSET(tp->t_state, TS_ASLEEP)) {
1133 1.1.12.2 nathanw CLR(tp->t_state, TS_ASLEEP);
1134 1.1.12.2 nathanw wakeup(&tp->t_outq);
1135 1.1.12.2 nathanw }
1136 1.1.12.2 nathanw selwakeup(&tp->t_wsel);
1137 1.1.12.2 nathanw if (tp->t_outq.c_cc == 0)
1138 1.1.12.2 nathanw goto out;
1139 1.1.12.2 nathanw }
1140 1.1.12.2 nathanw
1141 1.1.12.2 nathanw /* Grab the first contiguous region of buffer space. */
1142 1.1.12.2 nathanw {
1143 1.1.12.2 nathanw u_char *tba;
1144 1.1.12.2 nathanw int tbc;
1145 1.1.12.2 nathanw
1146 1.1.12.2 nathanw tba = tp->t_outq.c_cf;
1147 1.1.12.2 nathanw tbc = ndqb(&tp->t_outq, 0);
1148 1.1.12.2 nathanw
1149 1.1.12.2 nathanw (void)splserial();
1150 1.1.12.2 nathanw
1151 1.1.12.2 nathanw ch->ch_tba = tba;
1152 1.1.12.2 nathanw ch->ch_tbc = tbc;
1153 1.1.12.2 nathanw }
1154 1.1.12.2 nathanw
1155 1.1.12.2 nathanw SET(tp->t_state, TS_BUSY);
1156 1.1.12.2 nathanw ch->ch_tx_busy = 1;
1157 1.1.12.2 nathanw
1158 1.1.12.2 nathanw /* Output the first chunk of the contiguous buffer. */
1159 1.1.12.2 nathanw {
1160 1.1.12.2 nathanw while (ch->ch_tbc) {
1161 1.1.12.2 nathanw uint64_t data;
1162 1.1.12.2 nathanw int bytes, i;
1163 1.1.12.2 nathanw
1164 1.1.12.2 nathanw bytes = (ch->ch_tbc > 7) ? 7 : ch->ch_tbc;
1165 1.1.12.2 nathanw data = bytes;
1166 1.1.12.2 nathanw for (i=0; i<bytes; i++) {
1167 1.1.12.2 nathanw data <<= 8;
1168 1.1.12.2 nathanw data |= *ch->ch_tba++;
1169 1.1.12.2 nathanw }
1170 1.1.12.2 nathanw if (bytes < 7)
1171 1.1.12.2 nathanw data <<= 56-(bytes<<3);
1172 1.1.12.2 nathanw ch->ch_tbc -= bytes;
1173 1.1.12.2 nathanw WRITE_REG(ch->ch_output_reg, data);
1174 1.1.12.2 nathanw }
1175 1.1.12.2 nathanw ch->ch_tx_busy = 0;
1176 1.1.12.2 nathanw ch->ch_tx_done = 1;
1177 1.1.12.2 nathanw }
1178 1.1.12.2 nathanw out:
1179 1.1.12.2 nathanw splx(s);
1180 1.1.12.2 nathanw return;
1181 1.1.12.2 nathanw }
1182 1.1.12.2 nathanw
1183 1.1.12.2 nathanw /*
1184 1.1.12.2 nathanw * Stop output on a line.
1185 1.1.12.2 nathanw */
1186 1.1.12.2 nathanw void
1187 1.1.12.2 nathanw sbjcnstop(struct tty *tp, int flag)
1188 1.1.12.2 nathanw {
1189 1.1.12.2 nathanw struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(tp->t_dev)];
1190 1.1.12.2 nathanw struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
1191 1.1.12.2 nathanw int s;
1192 1.1.12.2 nathanw
1193 1.1.12.2 nathanw s = splserial();
1194 1.1.12.2 nathanw if (ISSET(tp->t_state, TS_BUSY)) {
1195 1.1.12.2 nathanw /* Stop transmitting at the next chunk. */
1196 1.1.12.2 nathanw ch->ch_tbc = 0;
1197 1.1.12.2 nathanw ch->ch_heldtbc = 0;
1198 1.1.12.2 nathanw if (!ISSET(tp->t_state, TS_TTSTOP))
1199 1.1.12.2 nathanw SET(tp->t_state, TS_FLUSH);
1200 1.1.12.2 nathanw }
1201 1.1.12.2 nathanw splx(s);
1202 1.1.12.2 nathanw }
1203 1.1.12.2 nathanw
1204 1.1.12.2 nathanw void
1205 1.1.12.2 nathanw sbjcn_diag(arg)
1206 1.1.12.2 nathanw void *arg;
1207 1.1.12.2 nathanw {
1208 1.1.12.2 nathanw struct sbjcn_channel *ch = arg;
1209 1.1.12.2 nathanw struct sbjcn_softc *sc = ch->ch_sc;
1210 1.1.12.2 nathanw int overflows, floods;
1211 1.1.12.2 nathanw int s;
1212 1.1.12.2 nathanw
1213 1.1.12.2 nathanw s = splserial();
1214 1.1.12.2 nathanw overflows = ch->ch_overflows;
1215 1.1.12.2 nathanw ch->ch_overflows = 0;
1216 1.1.12.2 nathanw floods = ch->ch_floods;
1217 1.1.12.2 nathanw ch->ch_floods = 0;
1218 1.1.12.2 nathanw ch->ch_errors = 0;
1219 1.1.12.2 nathanw splx(s);
1220 1.1.12.2 nathanw
1221 1.1.12.2 nathanw log(LOG_WARNING, "%s: channel %d: %d fifo overflow%s, %d ibuf flood%s\n",
1222 1.1.12.2 nathanw sc->sc_dev.dv_xname, ch->ch_num,
1223 1.1.12.2 nathanw overflows, overflows == 1 ? "" : "s",
1224 1.1.12.2 nathanw floods, floods == 1 ? "" : "s");
1225 1.1.12.2 nathanw }
1226 1.1.12.2 nathanw
1227 1.1.12.2 nathanw integrate void
1228 1.1.12.2 nathanw sbjcn_rxsoft(struct sbjcn_channel *ch, struct tty *tp)
1229 1.1.12.2 nathanw {
1230 1.1.12.2 nathanw int (*rint)(int c, struct tty *tp) = tp->t_linesw->l_rint;
1231 1.1.12.2 nathanw u_char *get, *end;
1232 1.1.12.2 nathanw u_int cc, scc;
1233 1.1.12.2 nathanw u_char sr;
1234 1.1.12.2 nathanw int code;
1235 1.1.12.2 nathanw int s;
1236 1.1.12.2 nathanw
1237 1.1.12.2 nathanw end = ch->ch_ebuf;
1238 1.1.12.2 nathanw get = ch->ch_rbget;
1239 1.1.12.2 nathanw scc = cc = sbjcn_rbuf_size - ch->ch_rbavail;
1240 1.1.12.2 nathanw
1241 1.1.12.2 nathanw if (cc == sbjcn_rbuf_size) {
1242 1.1.12.2 nathanw ch->ch_floods++;
1243 1.1.12.2 nathanw if (ch->ch_errors++ == 0)
1244 1.1.12.2 nathanw callout_reset(&ch->ch_diag_callout, 60 * hz,
1245 1.1.12.2 nathanw sbjcn_diag, ch);
1246 1.1.12.2 nathanw }
1247 1.1.12.2 nathanw
1248 1.1.12.2 nathanw while (cc) {
1249 1.1.12.2 nathanw code = get[0];
1250 1.1.12.2 nathanw sr = get[1];
1251 1.1.12.2 nathanw if (ISSET(sr, 0xf0)) {
1252 1.1.12.2 nathanw if (ISSET(sr, 0x10)) {
1253 1.1.12.2 nathanw ch->ch_overflows++;
1254 1.1.12.2 nathanw if (ch->ch_errors++ == 0)
1255 1.1.12.2 nathanw callout_reset(&ch->ch_diag_callout,
1256 1.1.12.2 nathanw 60 * hz, sbjcn_diag, ch);
1257 1.1.12.2 nathanw }
1258 1.1.12.2 nathanw if (ISSET(sr, 0xc0))
1259 1.1.12.2 nathanw SET(code, TTY_FE);
1260 1.1.12.2 nathanw if (ISSET(sr, 0x20))
1261 1.1.12.2 nathanw SET(code, TTY_PE);
1262 1.1.12.2 nathanw }
1263 1.1.12.2 nathanw if ((*rint)(code, tp) == -1) {
1264 1.1.12.2 nathanw /*
1265 1.1.12.2 nathanw * The line discipline's buffer is out of space.
1266 1.1.12.2 nathanw */
1267 1.1.12.2 nathanw if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
1268 1.1.12.2 nathanw /*
1269 1.1.12.2 nathanw * We're either not using flow control, or the
1270 1.1.12.2 nathanw * line discipline didn't tell us to block for
1271 1.1.12.2 nathanw * some reason. Either way, we have no way to
1272 1.1.12.2 nathanw * know when there's more space available, so
1273 1.1.12.2 nathanw * just drop the rest of the data.
1274 1.1.12.2 nathanw */
1275 1.1.12.2 nathanw get += cc << 1;
1276 1.1.12.2 nathanw if (get >= end)
1277 1.1.12.2 nathanw get -= sbjcn_rbuf_size << 1;
1278 1.1.12.2 nathanw cc = 0;
1279 1.1.12.2 nathanw } else {
1280 1.1.12.2 nathanw /*
1281 1.1.12.2 nathanw * Don't schedule any more receive processing
1282 1.1.12.2 nathanw * until the line discipline tells us there's
1283 1.1.12.2 nathanw * space available (through comhwiflow()).
1284 1.1.12.2 nathanw * Leave the rest of the data in the input
1285 1.1.12.2 nathanw * buffer.
1286 1.1.12.2 nathanw */
1287 1.1.12.2 nathanw SET(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
1288 1.1.12.2 nathanw }
1289 1.1.12.2 nathanw break;
1290 1.1.12.2 nathanw }
1291 1.1.12.2 nathanw get += 2;
1292 1.1.12.2 nathanw if (get >= end)
1293 1.1.12.2 nathanw get = ch->ch_rbuf;
1294 1.1.12.2 nathanw cc--;
1295 1.1.12.2 nathanw }
1296 1.1.12.2 nathanw
1297 1.1.12.2 nathanw if (cc != scc) {
1298 1.1.12.2 nathanw ch->ch_rbget = get;
1299 1.1.12.2 nathanw s = splserial();
1300 1.1.12.2 nathanw cc = ch->ch_rbavail += scc - cc;
1301 1.1.12.2 nathanw /* Buffers should be ok again, release possible block. */
1302 1.1.12.2 nathanw if (cc >= ch->ch_r_lowat) {
1303 1.1.12.2 nathanw if (ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
1304 1.1.12.2 nathanw CLR(ch->ch_rx_flags, RX_IBUF_OVERFLOWED);
1305 1.1.12.2 nathanw SET(ch->ch_imr, 0x02);
1306 1.1.12.2 nathanw }
1307 1.1.12.2 nathanw if (ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED)) {
1308 1.1.12.2 nathanw CLR(ch->ch_rx_flags, RX_IBUF_BLOCKED);
1309 1.1.12.2 nathanw sbjcn_dohwiflow(ch);
1310 1.1.12.2 nathanw }
1311 1.1.12.2 nathanw }
1312 1.1.12.2 nathanw splx(s);
1313 1.1.12.2 nathanw }
1314 1.1.12.2 nathanw }
1315 1.1.12.2 nathanw
1316 1.1.12.2 nathanw integrate void
1317 1.1.12.2 nathanw sbjcn_txsoft(struct sbjcn_channel *ch, struct tty *tp)
1318 1.1.12.2 nathanw {
1319 1.1.12.2 nathanw
1320 1.1.12.2 nathanw CLR(tp->t_state, TS_BUSY);
1321 1.1.12.2 nathanw if (ISSET(tp->t_state, TS_FLUSH))
1322 1.1.12.2 nathanw CLR(tp->t_state, TS_FLUSH);
1323 1.1.12.2 nathanw else
1324 1.1.12.2 nathanw ndflush(&tp->t_outq, (int)(ch->ch_tba - tp->t_outq.c_cf));
1325 1.1.12.2 nathanw (*tp->t_linesw->l_start)(tp);
1326 1.1.12.2 nathanw }
1327 1.1.12.2 nathanw
1328 1.1.12.2 nathanw integrate void
1329 1.1.12.2 nathanw sbjcn_stsoft(struct sbjcn_channel *ch, struct tty *tp)
1330 1.1.12.2 nathanw {
1331 1.1.12.2 nathanw u_char iports, delta;
1332 1.1.12.2 nathanw int s;
1333 1.1.12.2 nathanw
1334 1.1.12.2 nathanw s = splserial();
1335 1.1.12.2 nathanw iports = ch->ch_iports;
1336 1.1.12.2 nathanw delta = ch->ch_iports_delta;
1337 1.1.12.2 nathanw ch->ch_iports_delta = 0;
1338 1.1.12.2 nathanw splx(s);
1339 1.1.12.2 nathanw
1340 1.1.12.2 nathanw if (ISSET(delta, ch->ch_i_dcd)) {
1341 1.1.12.2 nathanw /*
1342 1.1.12.2 nathanw * Inform the tty layer that carrier detect changed.
1343 1.1.12.2 nathanw */
1344 1.1.12.2 nathanw (void) (*tp->t_linesw->l_modem)(tp,
1345 1.1.12.2 nathanw ISSET(iports, ch->ch_i_dcd));
1346 1.1.12.2 nathanw }
1347 1.1.12.2 nathanw
1348 1.1.12.2 nathanw if (ISSET(delta, ch->ch_i_cts)) {
1349 1.1.12.2 nathanw /* Block or unblock output according to flow control. */
1350 1.1.12.2 nathanw if (ISSET(iports, ch->ch_i_cts)) {
1351 1.1.12.2 nathanw ch->ch_tx_stopped = 0;
1352 1.1.12.2 nathanw (*tp->t_linesw->l_start)(tp);
1353 1.1.12.2 nathanw } else {
1354 1.1.12.2 nathanw ch->ch_tx_stopped = 1;
1355 1.1.12.2 nathanw }
1356 1.1.12.2 nathanw }
1357 1.1.12.2 nathanw
1358 1.1.12.2 nathanw #ifdef SBJCN_DEBUG
1359 1.1.12.2 nathanw if (sbjcn_debug)
1360 1.1.12.2 nathanw sbjcn_status(ch, "sbjcn_stsoft");
1361 1.1.12.2 nathanw #endif
1362 1.1.12.2 nathanw }
1363 1.1.12.2 nathanw
1364 1.1.12.2 nathanw integrate void
1365 1.1.12.2 nathanw sbjcn_recv(struct sbjcn_channel *ch)
1366 1.1.12.2 nathanw {
1367 1.1.12.2 nathanw u_char *put, *end;
1368 1.1.12.2 nathanw u_int cc;
1369 1.1.12.2 nathanw
1370 1.1.12.2 nathanw end = ch->ch_ebuf;
1371 1.1.12.2 nathanw put = ch->ch_rbput;
1372 1.1.12.2 nathanw cc = ch->ch_rbavail;
1373 1.1.12.2 nathanw
1374 1.1.12.2 nathanw /* XXX process break */
1375 1.1.12.2 nathanw
1376 1.1.12.2 nathanw sbjcncn_grabdword(ch);
1377 1.1.12.2 nathanw if (ch->ch_waiting_input) {
1378 1.1.12.2 nathanw if (!ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
1379 1.1.12.2 nathanw while (cc > 0) {
1380 1.1.12.2 nathanw put[0] = sbjcncn_nextbyte(ch);
1381 1.1.12.2 nathanw put[1] = 1; /* XXXKW ? */
1382 1.1.12.2 nathanw put += 2;
1383 1.1.12.2 nathanw if (put >= end)
1384 1.1.12.2 nathanw put = ch->ch_rbuf;
1385 1.1.12.2 nathanw cc--;
1386 1.1.12.2 nathanw
1387 1.1.12.2 nathanw if (!ch->ch_waiting_input)
1388 1.1.12.2 nathanw break;
1389 1.1.12.2 nathanw }
1390 1.1.12.2 nathanw
1391 1.1.12.2 nathanw /*
1392 1.1.12.2 nathanw * Current string of incoming characters ended
1393 1.1.12.2 nathanw * because no more data was available or we
1394 1.1.12.2 nathanw * ran out of space. Schedule a receive event
1395 1.1.12.2 nathanw * if any data was received. If we're out of
1396 1.1.12.2 nathanw * space, turn off receive interrupts.
1397 1.1.12.2 nathanw */
1398 1.1.12.2 nathanw ch->ch_rbput = put;
1399 1.1.12.2 nathanw ch->ch_rbavail = cc;
1400 1.1.12.2 nathanw if (!ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED))
1401 1.1.12.2 nathanw ch->ch_rx_ready = 1;
1402 1.1.12.2 nathanw
1403 1.1.12.2 nathanw /*
1404 1.1.12.2 nathanw * See if we are in danger of overflowing a
1405 1.1.12.2 nathanw * buffer. If so, use hardware flow control
1406 1.1.12.2 nathanw * to ease the pressure.
1407 1.1.12.2 nathanw */
1408 1.1.12.2 nathanw if (!ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED) &&
1409 1.1.12.2 nathanw cc < ch->ch_r_hiwat) {
1410 1.1.12.2 nathanw SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
1411 1.1.12.2 nathanw sbjcn_dohwiflow(ch);
1412 1.1.12.2 nathanw }
1413 1.1.12.2 nathanw
1414 1.1.12.2 nathanw /*
1415 1.1.12.2 nathanw * If we're out of space, disable receive
1416 1.1.12.2 nathanw * interrupts until the queue has drained
1417 1.1.12.2 nathanw * a bit.
1418 1.1.12.2 nathanw */
1419 1.1.12.2 nathanw if (!cc) {
1420 1.1.12.2 nathanw SET(ch->ch_rx_flags,
1421 1.1.12.2 nathanw RX_IBUF_OVERFLOWED);
1422 1.1.12.2 nathanw CLR(ch->ch_imr, 0x02);
1423 1.1.12.2 nathanw }
1424 1.1.12.2 nathanw } else {
1425 1.1.12.2 nathanw /* XXX panic? */
1426 1.1.12.2 nathanw CLR(ch->ch_imr, 0x02);
1427 1.1.12.2 nathanw // continue;
1428 1.1.12.2 nathanw }
1429 1.1.12.2 nathanw }
1430 1.1.12.2 nathanw
1431 1.1.12.2 nathanw /*
1432 1.1.12.2 nathanw * If we've delayed a parameter change, do it now, and restart
1433 1.1.12.2 nathanw * output.
1434 1.1.12.2 nathanw */
1435 1.1.12.2 nathanw if (ch->ch_heldchange) {
1436 1.1.12.2 nathanw sbjcn_loadchannelregs(ch);
1437 1.1.12.2 nathanw ch->ch_heldchange = 0;
1438 1.1.12.2 nathanw ch->ch_tbc = ch->ch_heldtbc;
1439 1.1.12.2 nathanw ch->ch_heldtbc = 0;
1440 1.1.12.2 nathanw }
1441 1.1.12.2 nathanw }
1442 1.1.12.2 nathanw
1443 1.1.12.2 nathanw void
1444 1.1.12.2 nathanw sbjcn_callout(void *arg)
1445 1.1.12.2 nathanw {
1446 1.1.12.2 nathanw struct sbjcn_channel *ch = arg;
1447 1.1.12.2 nathanw struct tty *tp = ch->ch_tty;
1448 1.1.12.2 nathanw
1449 1.1.12.2 nathanw /* XXX get stuff */
1450 1.1.12.2 nathanw sbjcn_recv(ch);
1451 1.1.12.2 nathanw
1452 1.1.12.2 nathanw /* XXX check receive */
1453 1.1.12.2 nathanw if (ch->ch_rx_ready) {
1454 1.1.12.2 nathanw ch->ch_rx_ready = 0;
1455 1.1.12.2 nathanw sbjcn_rxsoft(ch, tp);
1456 1.1.12.2 nathanw }
1457 1.1.12.2 nathanw
1458 1.1.12.2 nathanw /* XXX check transmit */
1459 1.1.12.2 nathanw if (ch->ch_tx_done) {
1460 1.1.12.2 nathanw ch->ch_tx_done = 0;
1461 1.1.12.2 nathanw sbjcn_txsoft(ch, tp);
1462 1.1.12.2 nathanw }
1463 1.1.12.2 nathanw
1464 1.1.12.2 nathanw callout_reset(&ch->ch_callout, hz/10, sbjcn_callout, ch);
1465 1.1.12.2 nathanw }
1466 1.1.12.2 nathanw
1467 1.1.12.2 nathanw static char sbjcncn_nextbyte(struct sbjcn_channel *ch)
1468 1.1.12.2 nathanw {
1469 1.1.12.2 nathanw char c;
1470 1.1.12.2 nathanw
1471 1.1.12.2 nathanw sbjcncn_grabdword(ch);
1472 1.1.12.2 nathanw c = (ch->ch_input_buf >> 56) & 0xff;
1473 1.1.12.2 nathanw ch->ch_input_buf <<= 8;
1474 1.1.12.2 nathanw ch->ch_waiting_input--;
1475 1.1.12.2 nathanw return c;
1476 1.1.12.2 nathanw }
1477 1.1.12.2 nathanw
1478 1.1.12.2 nathanw static void sbjcncn_grabdword(struct sbjcn_channel *ch)
1479 1.1.12.2 nathanw {
1480 1.1.12.2 nathanw uint64_t inbuf;
1481 1.1.12.2 nathanw
1482 1.1.12.2 nathanw if (ch->ch_waiting_input)
1483 1.1.12.2 nathanw return;
1484 1.1.12.2 nathanw
1485 1.1.12.2 nathanw inbuf = READ_REG(ch->ch_input_reg);
1486 1.1.12.2 nathanw ch->ch_waiting_input = jtag_input_len(inbuf);
1487 1.1.12.2 nathanw ch->ch_input_buf = inbuf << 8;
1488 1.1.12.2 nathanw }
1489 1.1.12.2 nathanw
1490 1.1.12.2 nathanw /*
1491 1.1.12.2 nathanw * Initialize UART for use as console or KGDB line.
1492 1.1.12.2 nathanw */
1493 1.1.12.2 nathanw int
1494 1.1.12.2 nathanw sbjcn_init(u_long addr, int chan, int rate, tcflag_t cflag)
1495 1.1.12.2 nathanw {
1496 1.1.12.2 nathanw /* XXXKW Anything to do here? */
1497 1.1.12.2 nathanw return (0);
1498 1.1.12.2 nathanw }
1499 1.1.12.2 nathanw
1500 1.1.12.2 nathanw /*
1501 1.1.12.2 nathanw * Following are all routines needed for sbjcn to act as console
1502 1.1.12.2 nathanw */
1503 1.1.12.2 nathanw int
1504 1.1.12.2 nathanw sbjcn_cnattach(u_long addr, int chan, int rate, tcflag_t cflag)
1505 1.1.12.2 nathanw {
1506 1.1.12.2 nathanw int res;
1507 1.1.12.2 nathanw uint64_t ctrl_val;
1508 1.1.12.2 nathanw static struct consdev sbjcn_cons = {
1509 1.1.12.2 nathanw NULL, NULL, sbjcn_cngetc, sbjcn_cnputc, sbjcn_cnpollc, NULL,
1510 1.1.12.2 nathanw NODEV, CN_NORMAL
1511 1.1.12.2 nathanw };
1512 1.1.12.2 nathanw
1513 1.1.12.2 nathanw res = sbjcn_init(addr, chan, rate, cflag);
1514 1.1.12.2 nathanw if (res)
1515 1.1.12.2 nathanw return (res);
1516 1.1.12.2 nathanw
1517 1.1.12.2 nathanw cn_tab = &sbjcn_cons;
1518 1.1.12.2 nathanw
1519 1.1.12.2 nathanw sbjcn_cons_present = 1;
1520 1.1.12.2 nathanw sbjcn_cons_addr = addr;
1521 1.1.12.2 nathanw sbjcn_cons_waiting_input = 0;
1522 1.1.12.2 nathanw sbjcn_cons_chan = chan;
1523 1.1.12.2 nathanw sbjcn_cons_rate = rate;
1524 1.1.12.2 nathanw sbjcn_cons_cflag = cflag;
1525 1.1.12.2 nathanw
1526 1.1.12.2 nathanw /* Wait for sign of life from the other end */
1527 1.1.12.2 nathanw while ((ctrl_val = READ_REG(MIPS_PHYS_TO_KSEG1(sbjcn_cons_addr + JTAG_CONS_CONTROL))) == 0)
1528 1.1.12.2 nathanw ;
1529 1.1.12.2 nathanw
1530 1.1.12.2 nathanw return (ctrl_val != JTAG_CONS_MAGICNUM);
1531 1.1.12.2 nathanw }
1532 1.1.12.2 nathanw
1533 1.1.12.2 nathanw int
1534 1.1.12.2 nathanw sbjcn_cngetc(dev_t dev)
1535 1.1.12.2 nathanw {
1536 1.1.12.2 nathanw char c;
1537 1.1.12.2 nathanw
1538 1.1.12.2 nathanw while (sbjcn_cons_waiting_input == 0)
1539 1.1.12.2 nathanw sbjcn_cngrabdword();
1540 1.1.12.2 nathanw
1541 1.1.12.2 nathanw c = (sbjcn_cons_input_buf >> 56) & 0xff;
1542 1.1.12.2 nathanw sbjcn_cons_input_buf <<= 8;
1543 1.1.12.2 nathanw sbjcn_cons_waiting_input--;
1544 1.1.12.2 nathanw
1545 1.1.12.2 nathanw return c;
1546 1.1.12.2 nathanw }
1547 1.1.12.2 nathanw
1548 1.1.12.2 nathanw /*
1549 1.1.12.2 nathanw * Console kernel output character routine.
1550 1.1.12.2 nathanw */
1551 1.1.12.2 nathanw void
1552 1.1.12.2 nathanw sbjcn_cnputc(dev_t dev, int c)
1553 1.1.12.2 nathanw {
1554 1.1.12.2 nathanw uint64_t outbuf;
1555 1.1.12.2 nathanw
1556 1.1.12.2 nathanw outbuf = (1LL << 56) | (((uint64_t)c) << 48);
1557 1.1.12.2 nathanw WRITE_REG(MIPS_PHYS_TO_KSEG1(sbjcn_cons_addr + JTAG_CONS_OUTPUT), outbuf);
1558 1.1.12.2 nathanw }
1559 1.1.12.2 nathanw
1560 1.1.12.2 nathanw void
1561 1.1.12.2 nathanw sbjcn_cnpollc(dev_t dev, int on)
1562 1.1.12.2 nathanw {
1563 1.1.12.2 nathanw
1564 1.1.12.2 nathanw }
1565 1.1.12.2 nathanw
1566 1.1.12.2 nathanw static void sbjcn_cngrabdword(void)
1567 1.1.12.2 nathanw {
1568 1.1.12.2 nathanw uint64_t inbuf;
1569 1.1.12.2 nathanw
1570 1.1.12.2 nathanw if (sbjcn_cons_waiting_input)
1571 1.1.12.2 nathanw return;
1572 1.1.12.2 nathanw
1573 1.1.12.2 nathanw inbuf = READ_REG(MIPS_PHYS_TO_KSEG1(sbjcn_cons_addr + JTAG_CONS_INPUT));
1574 1.1.12.2 nathanw sbjcn_cons_waiting_input = jtag_input_len(inbuf);
1575 1.1.12.2 nathanw sbjcn_cons_input_buf = inbuf << 8;
1576 1.1.12.2 nathanw }
1577 1.1.12.2 nathanw
1578 1.1.12.2 nathanw #ifdef KGDB
1579 1.1.12.2 nathanw int
1580 1.1.12.2 nathanw sbjcn_kgdb_attach(u_long addr, int chan, int rate, tcflag_t cflag)
1581 1.1.12.2 nathanw {
1582 1.1.12.2 nathanw int res;
1583 1.1.12.2 nathanw
1584 1.1.12.2 nathanw if (!sbjcn_cons_present &&
1585 1.1.12.2 nathanw sbjcn_cons_addr == addr && sbjcn_cons_chan == chan)
1586 1.1.12.2 nathanw return (EBUSY); /* cannot share with console */
1587 1.1.12.2 nathanw
1588 1.1.12.2 nathanw res = sbjcn_init(addr, chan, rate, cflag);
1589 1.1.12.2 nathanw if (res)
1590 1.1.12.2 nathanw return (res);
1591 1.1.12.2 nathanw
1592 1.1.12.2 nathanw kgdb_attach(sbjcn_kgdb_getc, sbjcn_kgdb_putc, NULL);
1593 1.1.12.2 nathanw kgdb_dev = 123; /* unneeded, only to satisfy some tests */
1594 1.1.12.2 nathanw kgdb_rate = rate;
1595 1.1.12.2 nathanw
1596 1.1.12.2 nathanw sbjcn_kgdb_present = 1;
1597 1.1.12.2 nathanw /* XXX sbjcn_init wants addr, but we need the offset addr */
1598 1.1.12.2 nathanw sbjcn_kgdb_addr = addr + (chan * 0x100);
1599 1.1.12.2 nathanw sbjcn_kgdb_chan = chan;
1600 1.1.12.2 nathanw
1601 1.1.12.2 nathanw return (0);
1602 1.1.12.2 nathanw }
1603 1.1.12.2 nathanw
1604 1.1.12.2 nathanw /* ARGSUSED */
1605 1.1.12.2 nathanw int
1606 1.1.12.2 nathanw sbjcn_kgdb_getc(arg)
1607 1.1.12.2 nathanw void *arg;
1608 1.1.12.2 nathanw {
1609 1.1.12.2 nathanw
1610 1.1.12.2 nathanw return (sbjcn_common_getc(sbjcn_kgdb_addr, sbjcn_kgdb_chan));
1611 1.1.12.2 nathanw }
1612 1.1.12.2 nathanw
1613 1.1.12.2 nathanw /* ARGSUSED */
1614 1.1.12.2 nathanw void
1615 1.1.12.2 nathanw sbjcn_kgdb_putc(arg, c)
1616 1.1.12.2 nathanw void *arg;
1617 1.1.12.2 nathanw int c;
1618 1.1.12.2 nathanw {
1619 1.1.12.2 nathanw
1620 1.1.12.2 nathanw sbjcn_common_putc(sbjcn_kgdb_addr, sbjcn_kgdb_chan, c);
1621 1.1.12.2 nathanw }
1622 1.1.12.2 nathanw #endif /* KGDB */
1623 1.1.12.2 nathanw
1624 1.1.12.2 nathanw /*
1625 1.1.12.2 nathanw * helper function to identify the sbjcn channels used by
1626 1.1.12.2 nathanw * console or KGDB (and not yet autoconf attached)
1627 1.1.12.2 nathanw */
1628 1.1.12.2 nathanw int
1629 1.1.12.2 nathanw sbjcn_is_console(u_long addr, int chan)
1630 1.1.12.2 nathanw {
1631 1.1.12.2 nathanw
1632 1.1.12.2 nathanw if (sbjcn_cons_present && !sbjcn_cons_attached &&
1633 1.1.12.2 nathanw sbjcn_cons_addr == addr && sbjcn_cons_chan == chan)
1634 1.1.12.2 nathanw return (1);
1635 1.1.12.2 nathanw #ifdef KGDB
1636 1.1.12.2 nathanw if (sbjcn_kgdb_present && !sbjcn_kgdb_attached &&
1637 1.1.12.2 nathanw sbjcn_kgdb_addr == addr && sbjcn_kgdb_chan == chan)
1638 1.1.12.2 nathanw return (1);
1639 1.1.12.2 nathanw #endif
1640 1.1.12.2 nathanw return (0);
1641 1.1.12.2 nathanw }
1642