zynq_uart.c revision 1.2.8.2 1 1.2.8.2 martin /* $NetBSD: zynq_uart.c,v 1.2.8.2 2020/04/13 08:03:38 martin Exp $ */
2 1.2.8.2 martin
3 1.2.8.2 martin /*
4 1.2.8.2 martin * Copyright (c) 2012 Genetec Corporation. All rights reserved.
5 1.2.8.2 martin * Written by Hiroyuki Bessho, Hashimoto Kenichi for Genetec Corporation.
6 1.2.8.2 martin *
7 1.2.8.2 martin * Redistribution and use in source and binary forms, with or without
8 1.2.8.2 martin * modification, are permitted provided that the following conditions
9 1.2.8.2 martin * are met:
10 1.2.8.2 martin * 1. Redistributions of source code must retain the above copyright
11 1.2.8.2 martin * notice, this list of conditions and the following disclaimer.
12 1.2.8.2 martin * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.8.2 martin * notice, this list of conditions and the following disclaimer in the
14 1.2.8.2 martin * documentation and/or other materials provided with the distribution.
15 1.2.8.2 martin *
16 1.2.8.2 martin * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
17 1.2.8.2 martin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.2.8.2 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.2.8.2 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION
20 1.2.8.2 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.2.8.2 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.2.8.2 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.2.8.2 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.2.8.2 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.2.8.2 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.2.8.2 martin * POSSIBILITY OF SUCH DAMAGE.
27 1.2.8.2 martin *
28 1.2.8.2 martin */
29 1.2.8.2 martin
30 1.2.8.2 martin /*
31 1.2.8.2 martin * derived from sys/dev/ic/com.c
32 1.2.8.2 martin */
33 1.2.8.2 martin
34 1.2.8.2 martin /*-
35 1.2.8.2 martin * Copyright (c) 1998, 1999, 2004, 2008 The NetBSD Foundation, Inc.
36 1.2.8.2 martin * All rights reserved.
37 1.2.8.2 martin *
38 1.2.8.2 martin * This code is derived from software contributed to The NetBSD Foundation
39 1.2.8.2 martin * by Charles M. Hannum.
40 1.2.8.2 martin *
41 1.2.8.2 martin * Redistribution and use in source and binary forms, with or without
42 1.2.8.2 martin * modification, are permitted provided that the following conditions
43 1.2.8.2 martin * are met:
44 1.2.8.2 martin * 1. Redistributions of source code must retain the above copyright
45 1.2.8.2 martin * notice, this list of conditions and the following disclaimer.
46 1.2.8.2 martin * 2. Redistributions in binary form must reproduce the above copyright
47 1.2.8.2 martin * notice, this list of conditions and the following disclaimer in the
48 1.2.8.2 martin * documentation and/or other materials provided with the distribution.
49 1.2.8.2 martin *
50 1.2.8.2 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
51 1.2.8.2 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52 1.2.8.2 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53 1.2.8.2 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
54 1.2.8.2 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55 1.2.8.2 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56 1.2.8.2 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57 1.2.8.2 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58 1.2.8.2 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59 1.2.8.2 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 1.2.8.2 martin * POSSIBILITY OF SUCH DAMAGE.
61 1.2.8.2 martin */
62 1.2.8.2 martin
63 1.2.8.2 martin /*
64 1.2.8.2 martin * Copyright (c) 1991 The Regents of the University of California.
65 1.2.8.2 martin * All rights reserved.
66 1.2.8.2 martin *
67 1.2.8.2 martin * Redistribution and use in source and binary forms, with or without
68 1.2.8.2 martin * modification, are permitted provided that the following conditions
69 1.2.8.2 martin * are met:
70 1.2.8.2 martin * 1. Redistributions of source code must retain the above copyright
71 1.2.8.2 martin * notice, this list of conditions and the following disclaimer.
72 1.2.8.2 martin * 2. Redistributions in binary form must reproduce the above copyright
73 1.2.8.2 martin * notice, this list of conditions and the following disclaimer in the
74 1.2.8.2 martin * documentation and/or other materials provided with the distribution.
75 1.2.8.2 martin * 3. Neither the name of the University nor the names of its contributors
76 1.2.8.2 martin * may be used to endorse or promote products derived from this software
77 1.2.8.2 martin * without specific prior written permission.
78 1.2.8.2 martin *
79 1.2.8.2 martin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
80 1.2.8.2 martin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
81 1.2.8.2 martin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
82 1.2.8.2 martin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
83 1.2.8.2 martin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
84 1.2.8.2 martin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
85 1.2.8.2 martin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
86 1.2.8.2 martin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
87 1.2.8.2 martin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
88 1.2.8.2 martin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
89 1.2.8.2 martin * SUCH DAMAGE.
90 1.2.8.2 martin *
91 1.2.8.2 martin * @(#)com.c 7.5 (Berkeley) 5/16/91
92 1.2.8.2 martin */
93 1.2.8.2 martin
94 1.2.8.2 martin /*
95 1.2.8.2 martin * driver for UART in Zynq-7000.
96 1.2.8.2 martin */
97 1.2.8.2 martin
98 1.2.8.2 martin #include <sys/cdefs.h>
99 1.2.8.2 martin __KERNEL_RCSID(0, "$NetBSD: zynq_uart.c,v 1.2.8.2 2020/04/13 08:03:38 martin Exp $");
100 1.2.8.2 martin
101 1.2.8.2 martin #include "opt_soc.h"
102 1.2.8.2 martin #include "opt_console.h"
103 1.2.8.2 martin #include "opt_com.h"
104 1.2.8.2 martin #include "opt_ddb.h"
105 1.2.8.2 martin #include "opt_kgdb.h"
106 1.2.8.2 martin #include "opt_ntp.h"
107 1.2.8.2 martin
108 1.2.8.2 martin /*
109 1.2.8.2 martin * Override cnmagic(9) macro before including <sys/systm.h>.
110 1.2.8.2 martin * We need to know if cn_check_magic triggered debugger, so set a flag.
111 1.2.8.2 martin * Callers of cn_check_magic must declare int cn_trapped = 0;
112 1.2.8.2 martin * XXX: this is *ugly*!
113 1.2.8.2 martin */
114 1.2.8.2 martin #define cn_trap() \
115 1.2.8.2 martin do { \
116 1.2.8.2 martin console_debugger(); \
117 1.2.8.2 martin cn_trapped = 1; \
118 1.2.8.2 martin } while (/* CONSTCOND */ 0)
119 1.2.8.2 martin
120 1.2.8.2 martin #include <sys/param.h>
121 1.2.8.2 martin
122 1.2.8.2 martin #include <sys/bus.h>
123 1.2.8.2 martin #include <sys/conf.h>
124 1.2.8.2 martin #include <dev/cons.h>
125 1.2.8.2 martin #include <sys/device.h>
126 1.2.8.2 martin #include <sys/file.h>
127 1.2.8.2 martin #include <sys/kauth.h>
128 1.2.8.2 martin #include <sys/kernel.h>
129 1.2.8.2 martin #include <sys/malloc.h>
130 1.2.8.2 martin #include <sys/poll.h>
131 1.2.8.2 martin #include <sys/proc.h>
132 1.2.8.2 martin #include <sys/systm.h>
133 1.2.8.2 martin #include <sys/tty.h>
134 1.2.8.2 martin
135 1.2.8.2 martin #ifdef RND_COM
136 1.2.8.2 martin #include <sys/rndsource.h>
137 1.2.8.2 martin #endif
138 1.2.8.2 martin
139 1.2.8.2 martin #include <arm/xilinx/zynq_uartreg.h>
140 1.2.8.2 martin #include <arm/xilinx/zynq_uartvar.h>
141 1.2.8.2 martin
142 1.2.8.2 martin #ifndef ZYNQUART_RING_SIZE
143 1.2.8.2 martin #define ZYNQUART_RING_SIZE 2048
144 1.2.8.2 martin #endif
145 1.2.8.2 martin
146 1.2.8.2 martin #define UART_SIZE 0x00000048
147 1.2.8.2 martin
148 1.2.8.2 martin typedef struct zynquart_softc {
149 1.2.8.2 martin device_t sc_dev;
150 1.2.8.2 martin
151 1.2.8.2 martin struct zynquart_regs {
152 1.2.8.2 martin bus_space_tag_t ur_iot;
153 1.2.8.2 martin bus_space_handle_t ur_ioh;
154 1.2.8.2 martin bus_addr_t ur_iobase;
155 1.2.8.2 martin } sc_regs;
156 1.2.8.2 martin
157 1.2.8.2 martin #define sc_bt sc_regs.ur_iot
158 1.2.8.2 martin #define sc_bh sc_regs.ur_ioh
159 1.2.8.2 martin
160 1.2.8.2 martin uint32_t sc_intrspec_enb;
161 1.2.8.2 martin uint32_t sc_cr;
162 1.2.8.2 martin uint32_t sc_mcr;
163 1.2.8.2 martin uint32_t sc_msr;
164 1.2.8.2 martin
165 1.2.8.2 martin uint sc_init_cnt;
166 1.2.8.2 martin
167 1.2.8.2 martin bus_addr_t sc_addr;
168 1.2.8.2 martin bus_size_t sc_size;
169 1.2.8.2 martin
170 1.2.8.2 martin u_char sc_hwflags;
171 1.2.8.2 martin /* Hardware flag masks */
172 1.2.8.2 martin #define ZYNQUART_HW_FLOW __BIT(0)
173 1.2.8.2 martin #define ZYNQUART_HW_DEV_OK __BIT(1)
174 1.2.8.2 martin #define ZYNQUART_HW_CONSOLE __BIT(2)
175 1.2.8.2 martin #define ZYNQUART_HW_KGDB __BIT(3)
176 1.2.8.2 martin
177 1.2.8.2 martin bool enabled;
178 1.2.8.2 martin
179 1.2.8.2 martin u_char sc_swflags;
180 1.2.8.2 martin
181 1.2.8.2 martin u_char sc_rx_flags;
182 1.2.8.2 martin #define ZYNQUART_RX_TTY_BLOCKED __BIT(0)
183 1.2.8.2 martin #define ZYNQUART_RX_TTY_OVERFLOWED __BIT(1)
184 1.2.8.2 martin #define ZYNQUART_RX_IBUF_BLOCKED __BIT(2)
185 1.2.8.2 martin #define ZYNQUART_RX_IBUF_OVERFLOWED __BIT(3)
186 1.2.8.2 martin #define ZYNQUART_RX_ANY_BLOCK \
187 1.2.8.2 martin (ZYNQUART_RX_TTY_BLOCKED|ZYNQUART_RX_TTY_OVERFLOWED| \
188 1.2.8.2 martin ZYNQUART_RX_IBUF_BLOCKED|ZYNQUART_RX_IBUF_OVERFLOWED)
189 1.2.8.2 martin
190 1.2.8.2 martin bool sc_tx_busy, sc_tx_done, sc_tx_stopped;
191 1.2.8.2 martin bool sc_rx_ready,sc_st_check;
192 1.2.8.2 martin u_short sc_txfifo_len, sc_txfifo_thresh;
193 1.2.8.2 martin
194 1.2.8.2 martin uint16_t *sc_rbuf;
195 1.2.8.2 martin u_int sc_rbuf_size;
196 1.2.8.2 martin u_int sc_rbuf_in;
197 1.2.8.2 martin u_int sc_rbuf_out;
198 1.2.8.2 martin #define ZYNQUART_RBUF_AVAIL(sc) \
199 1.2.8.2 martin ((sc->sc_rbuf_out <= sc->sc_rbuf_in) ? \
200 1.2.8.2 martin (sc->sc_rbuf_in - sc->sc_rbuf_out) : \
201 1.2.8.2 martin (sc->sc_rbuf_size - (sc->sc_rbuf_out - sc->sc_rbuf_in)))
202 1.2.8.2 martin
203 1.2.8.2 martin #define ZYNQUART_RBUF_SPACE(sc) \
204 1.2.8.2 martin ((sc->sc_rbuf_in <= sc->sc_rbuf_out ? \
205 1.2.8.2 martin sc->sc_rbuf_size - (sc->sc_rbuf_out - sc->sc_rbuf_in) : \
206 1.2.8.2 martin sc->sc_rbuf_in - sc->sc_rbuf_out) - 1)
207 1.2.8.2 martin /* increment ringbuffer pointer */
208 1.2.8.2 martin #define ZYNQUART_RBUF_INC(sc,v,i) (((v) + (i))&((sc->sc_rbuf_size)-1))
209 1.2.8.2 martin u_int sc_r_lowat;
210 1.2.8.2 martin u_int sc_r_hiwat;
211 1.2.8.2 martin
212 1.2.8.2 martin /* output chunk */
213 1.2.8.2 martin u_char *sc_tba;
214 1.2.8.2 martin u_int sc_tbc;
215 1.2.8.2 martin u_int sc_heldtbc;
216 1.2.8.2 martin /* pending parameter changes */
217 1.2.8.2 martin u_char sc_pending;
218 1.2.8.2 martin #define ZYNQUART_PEND_PARAM __BIT(0)
219 1.2.8.2 martin #define ZYNQUART_PEND_SPEED __BIT(1)
220 1.2.8.2 martin
221 1.2.8.2 martin
222 1.2.8.2 martin struct callout sc_diag_callout;
223 1.2.8.2 martin kmutex_t sc_lock;
224 1.2.8.2 martin void *sc_ih; /* interrupt handler */
225 1.2.8.2 martin void *sc_si; /* soft interrupt */
226 1.2.8.2 martin struct tty *sc_tty;
227 1.2.8.2 martin
228 1.2.8.2 martin /* power management hooks */
229 1.2.8.2 martin int (*enable)(struct zynquart_softc *);
230 1.2.8.2 martin void (*disable)(struct zynquart_softc *);
231 1.2.8.2 martin
232 1.2.8.2 martin struct {
233 1.2.8.2 martin ulong err;
234 1.2.8.2 martin ulong brk;
235 1.2.8.2 martin ulong prerr;
236 1.2.8.2 martin ulong frmerr;
237 1.2.8.2 martin ulong ovrrun;
238 1.2.8.2 martin } sc_errors;
239 1.2.8.2 martin
240 1.2.8.2 martin struct zynquart_baudrate_ratio {
241 1.2.8.2 martin uint16_t numerator; /* UBIR */
242 1.2.8.2 martin uint16_t modulator; /* UBMR */
243 1.2.8.2 martin } sc_ratio;
244 1.2.8.2 martin
245 1.2.8.2 martin } zynquart_softc_t;
246 1.2.8.2 martin
247 1.2.8.2 martin
248 1.2.8.2 martin int zynquartspeed(long, struct zynquart_baudrate_ratio *);
249 1.2.8.2 martin int zynquartparam(struct tty *, struct termios *);
250 1.2.8.2 martin void zynquartstart(struct tty *);
251 1.2.8.2 martin int zynquarthwiflow(struct tty *, int);
252 1.2.8.2 martin
253 1.2.8.2 martin void zynquart_shutdown(struct zynquart_softc *);
254 1.2.8.2 martin void zynquart_loadchannelregs(struct zynquart_softc *);
255 1.2.8.2 martin void zynquart_hwiflow(struct zynquart_softc *);
256 1.2.8.2 martin void zynquart_break(struct zynquart_softc *, bool);
257 1.2.8.2 martin void zynquart_modem(struct zynquart_softc *, int);
258 1.2.8.2 martin void tiocm_to_zynquart(struct zynquart_softc *, u_long, int);
259 1.2.8.2 martin int zynquart_to_tiocm(struct zynquart_softc *);
260 1.2.8.2 martin void zynquart_iflush(struct zynquart_softc *);
261 1.2.8.2 martin
262 1.2.8.2 martin int zynquart_common_getc(dev_t, struct zynquart_regs *);
263 1.2.8.2 martin void zynquart_common_putc(dev_t, struct zynquart_regs *, int);
264 1.2.8.2 martin
265 1.2.8.2 martin
266 1.2.8.2 martin int zynquart_init(struct zynquart_regs *, int, tcflag_t);
267 1.2.8.2 martin
268 1.2.8.2 martin int zynquartcngetc(dev_t);
269 1.2.8.2 martin void zynquartcnputc(dev_t, int);
270 1.2.8.2 martin
271 1.2.8.2 martin static void zynquartintr_read(struct zynquart_softc *);
272 1.2.8.2 martin static void zynquartintr_send(struct zynquart_softc *);
273 1.2.8.2 martin
274 1.2.8.2 martin static void zynquart_enable_debugport(struct zynquart_softc *);
275 1.2.8.2 martin static void zynquart_disable_all_interrupts(struct zynquart_softc *);
276 1.2.8.2 martin static void zynquart_control_rxint(struct zynquart_softc *, bool);
277 1.2.8.2 martin static void zynquart_control_txint(struct zynquart_softc *, bool);
278 1.2.8.2 martin
279 1.2.8.2 martin static uint32_t cflag_to_zynquart(tcflag_t, uint32_t);
280 1.2.8.2 martin
281 1.2.8.2 martin #define integrate static inline
282 1.2.8.2 martin void zynquartsoft(void *);
283 1.2.8.2 martin integrate void zynquart_rxsoft(struct zynquart_softc *, struct tty *);
284 1.2.8.2 martin integrate void zynquart_txsoft(struct zynquart_softc *, struct tty *);
285 1.2.8.2 martin integrate void zynquart_stsoft(struct zynquart_softc *, struct tty *);
286 1.2.8.2 martin integrate void zynquart_schedrx(struct zynquart_softc *);
287 1.2.8.2 martin void zynquartdiag(void *);
288 1.2.8.2 martin static void zynquart_load_speed(struct zynquart_softc *);
289 1.2.8.2 martin static void zynquart_load_params(struct zynquart_softc *);
290 1.2.8.2 martin integrate void zynquart_load_pendings(struct zynquart_softc *);
291 1.2.8.2 martin
292 1.2.8.2 martin
293 1.2.8.2 martin extern struct cfdriver zynquart_cd;
294 1.2.8.2 martin
295 1.2.8.2 martin dev_type_open(zynquartopen);
296 1.2.8.2 martin dev_type_close(zynquartclose);
297 1.2.8.2 martin dev_type_read(zynquartread);
298 1.2.8.2 martin dev_type_write(zynquartwrite);
299 1.2.8.2 martin dev_type_ioctl(zynquartioctl);
300 1.2.8.2 martin dev_type_stop(zynquartstop);
301 1.2.8.2 martin dev_type_tty(zynquarttty);
302 1.2.8.2 martin dev_type_poll(zynquartpoll);
303 1.2.8.2 martin
304 1.2.8.2 martin const struct cdevsw zynquart_cdevsw = {
305 1.2.8.2 martin .d_open = zynquartopen,
306 1.2.8.2 martin .d_close = zynquartclose,
307 1.2.8.2 martin .d_read = zynquartread,
308 1.2.8.2 martin .d_write = zynquartwrite,
309 1.2.8.2 martin .d_ioctl = zynquartioctl,
310 1.2.8.2 martin .d_stop = zynquartstop,
311 1.2.8.2 martin .d_tty = zynquarttty,
312 1.2.8.2 martin .d_poll = zynquartpoll,
313 1.2.8.2 martin .d_mmap = nommap,
314 1.2.8.2 martin .d_kqfilter = ttykqfilter,
315 1.2.8.2 martin .d_discard = nodiscard,
316 1.2.8.2 martin .d_flag = D_TTY
317 1.2.8.2 martin };
318 1.2.8.2 martin
319 1.2.8.2 martin /*
320 1.2.8.2 martin * Make this an option variable one can patch.
321 1.2.8.2 martin * But be warned: this must be a power of 2!
322 1.2.8.2 martin */
323 1.2.8.2 martin u_int zynquart_rbuf_size = ZYNQUART_RING_SIZE;
324 1.2.8.2 martin
325 1.2.8.2 martin /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
326 1.2.8.2 martin u_int zynquart_rbuf_hiwat = (ZYNQUART_RING_SIZE * 1) / 4;
327 1.2.8.2 martin u_int zynquart_rbuf_lowat = (ZYNQUART_RING_SIZE * 3) / 4;
328 1.2.8.2 martin
329 1.2.8.2 martin static struct zynquart_regs zynquartconsregs;
330 1.2.8.2 martin static int zynquartconsattached;
331 1.2.8.2 martin static int zynquartconsrate;
332 1.2.8.2 martin static tcflag_t zynquartconscflag;
333 1.2.8.2 martin static struct cnm_state zynquart_cnm_state;
334 1.2.8.2 martin
335 1.2.8.2 martin u_int zynquart_freq;
336 1.2.8.2 martin u_int zynquart_freqdiv;
337 1.2.8.2 martin
338 1.2.8.2 martin #ifdef KGDB
339 1.2.8.2 martin #include <sys/kgdb.h>
340 1.2.8.2 martin
341 1.2.8.2 martin static struct zynquart_regs zynquart_kgdb_regs;
342 1.2.8.2 martin static int zynquart_kgdb_attached;
343 1.2.8.2 martin
344 1.2.8.2 martin int zynquart_kgdb_getc(void *);
345 1.2.8.2 martin void zynquart_kgdb_putc(void *, int);
346 1.2.8.2 martin #endif /* KGDB */
347 1.2.8.2 martin
348 1.2.8.2 martin #define ZYNQUART_UNIT_MASK 0x7ffff
349 1.2.8.2 martin #define ZYNQUART_DIALOUT_MASK 0x80000
350 1.2.8.2 martin
351 1.2.8.2 martin #define ZYNQUART_UNIT(x) (minor(x) & ZYNQUART_UNIT_MASK)
352 1.2.8.2 martin #define ZYNQUART_DIALOUT(x) (minor(x) & ZYNQUART_DIALOUT_MASK)
353 1.2.8.2 martin
354 1.2.8.2 martin #define ZYNQUART_ISALIVE(sc) ((sc)->enabled != 0 && \
355 1.2.8.2 martin device_is_active((sc)->sc_dev))
356 1.2.8.2 martin
357 1.2.8.2 martin #define BR BUS_SPACE_BARRIER_READ
358 1.2.8.2 martin #define BW BUS_SPACE_BARRIER_WRITE
359 1.2.8.2 martin #define ZYNQUART_BARRIER(r, f) \
360 1.2.8.2 martin bus_space_barrier((r)->ur_iot, (r)->ur_ioh, 0, UART_SIZE, (f))
361 1.2.8.2 martin
362 1.2.8.2 martin CFATTACH_DECL_NEW(zynquart, sizeof(struct zynquart_softc),
363 1.2.8.2 martin zynquart_match, zynquart_attach, NULL, NULL);
364 1.2.8.2 martin
365 1.2.8.2 martin void
366 1.2.8.2 martin zynquart_attach_common(device_t parent, device_t self,
367 1.2.8.2 martin bus_space_tag_t iot, paddr_t iobase, size_t size, int flags)
368 1.2.8.2 martin {
369 1.2.8.2 martin zynquart_softc_t *sc = device_private(self);
370 1.2.8.2 martin struct zynquart_regs *regsp = &sc->sc_regs;
371 1.2.8.2 martin struct tty *tp;
372 1.2.8.2 martin bus_space_handle_t ioh;
373 1.2.8.2 martin
374 1.2.8.2 martin aprint_naive("\n");
375 1.2.8.2 martin aprint_normal("\n");
376 1.2.8.2 martin
377 1.2.8.2 martin sc->sc_dev = self;
378 1.2.8.2 martin
379 1.2.8.2 martin if (size <= 0)
380 1.2.8.2 martin size = UART_SIZE;
381 1.2.8.2 martin
382 1.2.8.2 martin regsp->ur_iot = iot;
383 1.2.8.2 martin regsp->ur_iobase = iobase;
384 1.2.8.2 martin
385 1.2.8.2 martin if (bus_space_map(iot, regsp->ur_iobase, size, 0, &ioh)) {
386 1.2.8.2 martin return;
387 1.2.8.2 martin }
388 1.2.8.2 martin regsp->ur_ioh = ioh;
389 1.2.8.2 martin
390 1.2.8.2 martin callout_init(&sc->sc_diag_callout, 0);
391 1.2.8.2 martin mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH);
392 1.2.8.2 martin
393 1.2.8.2 martin sc->sc_cr = bus_space_read_4(iot, ioh, UART_CONTROL);
394 1.2.8.2 martin sc->sc_cr |= CR_TXEN | CR_RXEN;
395 1.2.8.2 martin sc->sc_cr &= ~(CR_TXDIS | CR_RXDIS);
396 1.2.8.2 martin bus_space_write_4(iot, ioh, UART_CONTROL, sc->sc_cr);
397 1.2.8.2 martin
398 1.2.8.2 martin /* Disable interrupts before configuring the device. */
399 1.2.8.2 martin zynquart_disable_all_interrupts(sc);
400 1.2.8.2 martin
401 1.2.8.2 martin if (regsp->ur_iobase == zynquartconsregs.ur_iobase) {
402 1.2.8.2 martin zynquartconsattached = 1;
403 1.2.8.2 martin
404 1.2.8.2 martin /* Make sure the console is always "hardwired". */
405 1.2.8.2 martin SET(sc->sc_hwflags, ZYNQUART_HW_CONSOLE);
406 1.2.8.2 martin SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
407 1.2.8.2 martin }
408 1.2.8.2 martin
409 1.2.8.2 martin tp = tty_alloc();
410 1.2.8.2 martin tp->t_oproc = zynquartstart;
411 1.2.8.2 martin tp->t_param = zynquartparam;
412 1.2.8.2 martin tp->t_hwiflow = zynquarthwiflow;
413 1.2.8.2 martin
414 1.2.8.2 martin sc->sc_tty = tp;
415 1.2.8.2 martin sc->sc_rbuf = malloc(sizeof (*sc->sc_rbuf) * zynquart_rbuf_size,
416 1.2.8.2 martin M_DEVBUF, M_WAITOK);
417 1.2.8.2 martin sc->sc_rbuf_size = zynquart_rbuf_size;
418 1.2.8.2 martin sc->sc_rbuf_in = sc->sc_rbuf_out = 0;
419 1.2.8.2 martin sc->sc_txfifo_len = 64;
420 1.2.8.2 martin sc->sc_txfifo_thresh = 32;
421 1.2.8.2 martin
422 1.2.8.2 martin tty_attach(tp);
423 1.2.8.2 martin
424 1.2.8.2 martin if (ISSET(sc->sc_hwflags, ZYNQUART_HW_CONSOLE)) {
425 1.2.8.2 martin int maj;
426 1.2.8.2 martin
427 1.2.8.2 martin /* locate the major number */
428 1.2.8.2 martin maj = cdevsw_lookup_major(&zynquart_cdevsw);
429 1.2.8.2 martin
430 1.2.8.2 martin if (maj != NODEVMAJOR) {
431 1.2.8.2 martin tp->t_dev = cn_tab->cn_dev = makedev(maj,
432 1.2.8.2 martin device_unit(sc->sc_dev));
433 1.2.8.2 martin
434 1.2.8.2 martin aprint_normal_dev(sc->sc_dev, "console\n");
435 1.2.8.2 martin }
436 1.2.8.2 martin }
437 1.2.8.2 martin
438 1.2.8.2 martin /* reset receive time out */
439 1.2.8.2 martin bus_space_write_4(iot, ioh, UART_RCVR_TIMEOUT, 0);
440 1.2.8.2 martin bus_space_write_4(iot, ioh, UART_RCVR_FIFO_TRIGGER, 1);
441 1.2.8.2 martin
442 1.2.8.2 martin #ifdef KGDB
443 1.2.8.2 martin /*
444 1.2.8.2 martin * Allow kgdb to "take over" this port. If this is
445 1.2.8.2 martin * not the console and is the kgdb device, it has
446 1.2.8.2 martin * exclusive use. If it's the console _and_ the
447 1.2.8.2 martin * kgdb device, it doesn't.
448 1.2.8.2 martin */
449 1.2.8.2 martin if (regsp->ur_iobase == zynquart_kgdb_regs.ur_iobase) {
450 1.2.8.2 martin if (!ISSET(sc->sc_hwflags, ZYNQUART_HW_CONSOLE)) {
451 1.2.8.2 martin zynquart_kgdb_attached = 1;
452 1.2.8.2 martin
453 1.2.8.2 martin SET(sc->sc_hwflags, ZYNQUART_HW_KGDB);
454 1.2.8.2 martin }
455 1.2.8.2 martin aprint_normal_dev(sc->sc_dev, "kgdb\n");
456 1.2.8.2 martin }
457 1.2.8.2 martin #endif
458 1.2.8.2 martin
459 1.2.8.2 martin sc->sc_si = softint_establish(SOFTINT_SERIAL, zynquartsoft, sc);
460 1.2.8.2 martin
461 1.2.8.2 martin #ifdef RND_COM
462 1.2.8.2 martin rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
463 1.2.8.2 martin RND_TYPE_TTY, 0);
464 1.2.8.2 martin #endif
465 1.2.8.2 martin
466 1.2.8.2 martin /* if there are no enable/disable functions, assume the device
467 1.2.8.2 martin is always enabled */
468 1.2.8.2 martin if (!sc->enable)
469 1.2.8.2 martin sc->enabled = 1;
470 1.2.8.2 martin
471 1.2.8.2 martin zynquart_enable_debugport(sc);
472 1.2.8.2 martin
473 1.2.8.2 martin SET(sc->sc_hwflags, ZYNQUART_HW_DEV_OK);
474 1.2.8.2 martin }
475 1.2.8.2 martin
476 1.2.8.2 martin int
477 1.2.8.2 martin zynquartspeed(long speed, struct zynquart_baudrate_ratio *ratio)
478 1.2.8.2 martin {
479 1.2.8.2 martin return 0;
480 1.2.8.2 martin }
481 1.2.8.2 martin
482 1.2.8.2 martin #ifdef ZYNQUART_DEBUG
483 1.2.8.2 martin int zynquart_debug = 0;
484 1.2.8.2 martin
485 1.2.8.2 martin void zynquartstatus(struct zynquart_softc *, const char *);
486 1.2.8.2 martin void
487 1.2.8.2 martin zynquartstatus(struct zynquart_softc *sc, const char *str)
488 1.2.8.2 martin {
489 1.2.8.2 martin struct tty *tp = sc->sc_tty;
490 1.2.8.2 martin
491 1.2.8.2 martin aprint_normal_dev(sc->sc_dev,
492 1.2.8.2 martin "%s %cclocal %cdcd %cts_carr_on %cdtr %ctx_stopped\n",
493 1.2.8.2 martin str,
494 1.2.8.2 martin ISSET(tp->t_cflag, CLOCAL) ? '+' : '-',
495 1.2.8.2 martin ISSET(sc->sc_msr, MSR_DCD) ? '+' : '-',
496 1.2.8.2 martin ISSET(tp->t_state, TS_CARR_ON) ? '+' : '-',
497 1.2.8.2 martin ISSET(sc->sc_mcr, MCR_DTR) ? '+' : '-',
498 1.2.8.2 martin sc->sc_tx_stopped ? '+' : '-');
499 1.2.8.2 martin
500 1.2.8.2 martin aprint_normal_dev(sc->sc_dev,
501 1.2.8.2 martin "%s %ccrtscts %ccts %cts_ttstop %crts rx_flags=0x%x\n",
502 1.2.8.2 martin str,
503 1.2.8.2 martin ISSET(tp->t_cflag, CRTSCTS) ? '+' : '-',
504 1.2.8.2 martin ISSET(sc->sc_msr, MSR_CTS) ? '+' : '-',
505 1.2.8.2 martin ISSET(tp->t_state, TS_TTSTOP) ? '+' : '-',
506 1.2.8.2 martin ISSET(sc->sc_mcr, MCR_RTS) ? '+' : '-',
507 1.2.8.2 martin sc->sc_rx_flags);
508 1.2.8.2 martin }
509 1.2.8.2 martin #endif
510 1.2.8.2 martin
511 1.2.8.2 martin #if 0
512 1.2.8.2 martin int
513 1.2.8.2 martin zynquart_detach(device_t self, int flags)
514 1.2.8.2 martin {
515 1.2.8.2 martin struct zynquart_softc *sc = device_private(self);
516 1.2.8.2 martin int maj, mn;
517 1.2.8.2 martin
518 1.2.8.2 martin if (ISSET(sc->sc_hwflags, ZYNQUART_HW_CONSOLE))
519 1.2.8.2 martin return EBUSY;
520 1.2.8.2 martin
521 1.2.8.2 martin /* locate the major number */
522 1.2.8.2 martin maj = cdevsw_lookup_major(&zynquart_cdevsw);
523 1.2.8.2 martin
524 1.2.8.2 martin /* Nuke the vnodes for any open instances. */
525 1.2.8.2 martin mn = device_unit(self);
526 1.2.8.2 martin vdevgone(maj, mn, mn, VCHR);
527 1.2.8.2 martin
528 1.2.8.2 martin mn |= ZYNQUART_DIALOUT_MASK;
529 1.2.8.2 martin vdevgone(maj, mn, mn, VCHR);
530 1.2.8.2 martin
531 1.2.8.2 martin if (sc->sc_rbuf == NULL) {
532 1.2.8.2 martin /*
533 1.2.8.2 martin * Ring buffer allocation failed in the zynquart_attach_subr,
534 1.2.8.2 martin * only the tty is allocated, and nothing else.
535 1.2.8.2 martin */
536 1.2.8.2 martin tty_free(sc->sc_tty);
537 1.2.8.2 martin return 0;
538 1.2.8.2 martin }
539 1.2.8.2 martin
540 1.2.8.2 martin /* Free the receive buffer. */
541 1.2.8.2 martin free(sc->sc_rbuf, M_DEVBUF);
542 1.2.8.2 martin
543 1.2.8.2 martin /* Detach and free the tty. */
544 1.2.8.2 martin tty_detach(sc->sc_tty);
545 1.2.8.2 martin tty_free(sc->sc_tty);
546 1.2.8.2 martin
547 1.2.8.2 martin /* Unhook the soft interrupt handler. */
548 1.2.8.2 martin softint_disestablish(sc->sc_si);
549 1.2.8.2 martin
550 1.2.8.2 martin #ifdef RND_COM
551 1.2.8.2 martin /* Unhook the entropy source. */
552 1.2.8.2 martin rnd_detach_source(&sc->rnd_source);
553 1.2.8.2 martin #endif
554 1.2.8.2 martin callout_destroy(&sc->sc_diag_callout);
555 1.2.8.2 martin
556 1.2.8.2 martin /* Destroy the lock. */
557 1.2.8.2 martin mutex_destroy(&sc->sc_lock);
558 1.2.8.2 martin
559 1.2.8.2 martin return (0);
560 1.2.8.2 martin }
561 1.2.8.2 martin #endif
562 1.2.8.2 martin
563 1.2.8.2 martin #ifdef notyet
564 1.2.8.2 martin int
565 1.2.8.2 martin zynquart_activate(device_t self, enum devact act)
566 1.2.8.2 martin {
567 1.2.8.2 martin struct zynquart_softc *sc = device_private(self);
568 1.2.8.2 martin int rv = 0;
569 1.2.8.2 martin
570 1.2.8.2 martin switch (act) {
571 1.2.8.2 martin case DVACT_ACTIVATE:
572 1.2.8.2 martin rv = EOPNOTSUPP;
573 1.2.8.2 martin break;
574 1.2.8.2 martin
575 1.2.8.2 martin case DVACT_DEACTIVATE:
576 1.2.8.2 martin if (sc->sc_hwflags & (ZYNQUART_HW_CONSOLE|ZYNQUART_HW_KGDB)) {
577 1.2.8.2 martin rv = EBUSY;
578 1.2.8.2 martin break;
579 1.2.8.2 martin }
580 1.2.8.2 martin
581 1.2.8.2 martin if (sc->disable != NULL && sc->enabled != 0) {
582 1.2.8.2 martin (*sc->disable)(sc);
583 1.2.8.2 martin sc->enabled = 0;
584 1.2.8.2 martin }
585 1.2.8.2 martin break;
586 1.2.8.2 martin }
587 1.2.8.2 martin
588 1.2.8.2 martin return (rv);
589 1.2.8.2 martin }
590 1.2.8.2 martin #endif
591 1.2.8.2 martin
592 1.2.8.2 martin void
593 1.2.8.2 martin zynquart_shutdown(struct zynquart_softc *sc)
594 1.2.8.2 martin {
595 1.2.8.2 martin struct tty *tp = sc->sc_tty;
596 1.2.8.2 martin
597 1.2.8.2 martin mutex_spin_enter(&sc->sc_lock);
598 1.2.8.2 martin
599 1.2.8.2 martin /* If we were asserting flow control, then deassert it. */
600 1.2.8.2 martin SET(sc->sc_rx_flags, ZYNQUART_RX_IBUF_BLOCKED);
601 1.2.8.2 martin zynquart_hwiflow(sc);
602 1.2.8.2 martin
603 1.2.8.2 martin /* Clear any break condition set with TIOCSBRK. */
604 1.2.8.2 martin zynquart_break(sc, false);
605 1.2.8.2 martin
606 1.2.8.2 martin /*
607 1.2.8.2 martin * Hang up if necessary. Wait a bit, so the other side has time to
608 1.2.8.2 martin * notice even if we immediately open the port again.
609 1.2.8.2 martin * Avoid tsleeping above splhigh().
610 1.2.8.2 martin */
611 1.2.8.2 martin if (ISSET(tp->t_cflag, HUPCL)) {
612 1.2.8.2 martin zynquart_modem(sc, 0);
613 1.2.8.2 martin mutex_spin_exit(&sc->sc_lock);
614 1.2.8.2 martin /* XXX will only timeout */
615 1.2.8.2 martin (void) kpause(ttclos, false, hz, NULL);
616 1.2.8.2 martin mutex_spin_enter(&sc->sc_lock);
617 1.2.8.2 martin }
618 1.2.8.2 martin
619 1.2.8.2 martin /* Turn off interrupts. */
620 1.2.8.2 martin zynquart_disable_all_interrupts(sc);
621 1.2.8.2 martin /* re-enable recv interrupt for console or kgdb port */
622 1.2.8.2 martin zynquart_enable_debugport(sc);
623 1.2.8.2 martin
624 1.2.8.2 martin mutex_spin_exit(&sc->sc_lock);
625 1.2.8.2 martin
626 1.2.8.2 martin #ifdef notyet
627 1.2.8.2 martin if (sc->disable) {
628 1.2.8.2 martin #ifdef DIAGNOSTIC
629 1.2.8.2 martin if (!sc->enabled)
630 1.2.8.2 martin panic("zynquart_shutdown: not enabled?");
631 1.2.8.2 martin #endif
632 1.2.8.2 martin (*sc->disable)(sc);
633 1.2.8.2 martin sc->enabled = 0;
634 1.2.8.2 martin }
635 1.2.8.2 martin #endif
636 1.2.8.2 martin }
637 1.2.8.2 martin
638 1.2.8.2 martin int
639 1.2.8.2 martin zynquartopen(dev_t dev, int flag, int mode, struct lwp *l)
640 1.2.8.2 martin {
641 1.2.8.2 martin struct zynquart_softc *sc;
642 1.2.8.2 martin struct tty *tp;
643 1.2.8.2 martin int s;
644 1.2.8.2 martin int error;
645 1.2.8.2 martin
646 1.2.8.2 martin sc = device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(dev));
647 1.2.8.2 martin if (sc == NULL || !ISSET(sc->sc_hwflags, ZYNQUART_HW_DEV_OK) ||
648 1.2.8.2 martin sc->sc_rbuf == NULL)
649 1.2.8.2 martin return (ENXIO);
650 1.2.8.2 martin
651 1.2.8.2 martin if (!device_is_active(sc->sc_dev))
652 1.2.8.2 martin return (ENXIO);
653 1.2.8.2 martin
654 1.2.8.2 martin #ifdef KGDB
655 1.2.8.2 martin /*
656 1.2.8.2 martin * If this is the kgdb port, no other use is permitted.
657 1.2.8.2 martin */
658 1.2.8.2 martin if (ISSET(sc->sc_hwflags, ZYNQUART_HW_KGDB))
659 1.2.8.2 martin return (EBUSY);
660 1.2.8.2 martin #endif
661 1.2.8.2 martin
662 1.2.8.2 martin tp = sc->sc_tty;
663 1.2.8.2 martin
664 1.2.8.2 martin if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
665 1.2.8.2 martin return (EBUSY);
666 1.2.8.2 martin
667 1.2.8.2 martin s = spltty();
668 1.2.8.2 martin
669 1.2.8.2 martin /*
670 1.2.8.2 martin * Do the following iff this is a first open.
671 1.2.8.2 martin */
672 1.2.8.2 martin if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
673 1.2.8.2 martin struct termios t;
674 1.2.8.2 martin
675 1.2.8.2 martin tp->t_dev = dev;
676 1.2.8.2 martin
677 1.2.8.2 martin
678 1.2.8.2 martin #ifdef notyet
679 1.2.8.2 martin if (sc->enable) {
680 1.2.8.2 martin if ((*sc->enable)(sc)) {
681 1.2.8.2 martin splx(s);
682 1.2.8.2 martin aprint_error_dev(sc->sc_dev,
683 1.2.8.2 martin "device enable failed\n");
684 1.2.8.2 martin return (EIO);
685 1.2.8.2 martin }
686 1.2.8.2 martin sc->enabled = 1;
687 1.2.8.2 martin }
688 1.2.8.2 martin #endif
689 1.2.8.2 martin
690 1.2.8.2 martin mutex_spin_enter(&sc->sc_lock);
691 1.2.8.2 martin
692 1.2.8.2 martin zynquart_disable_all_interrupts(sc);
693 1.2.8.2 martin
694 1.2.8.2 martin /* Fetch the current modem control status, needed later. */
695 1.2.8.2 martin
696 1.2.8.2 martin #ifdef ZYNQUART_PPS
697 1.2.8.2 martin /* Clear PPS capture state on first open. */
698 1.2.8.2 martin mutex_spin_enter(&timecounter_lock);
699 1.2.8.2 martin memset(&sc->sc_pps_state, 0, sizeof(sc->sc_pps_state));
700 1.2.8.2 martin sc->sc_pps_state.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
701 1.2.8.2 martin pps_init(&sc->sc_pps_state);
702 1.2.8.2 martin mutex_spin_exit(&timecounter_lock);
703 1.2.8.2 martin #endif
704 1.2.8.2 martin
705 1.2.8.2 martin mutex_spin_exit(&sc->sc_lock);
706 1.2.8.2 martin
707 1.2.8.2 martin /*
708 1.2.8.2 martin * Initialize the termios status to the defaults. Add in the
709 1.2.8.2 martin * sticky bits from TIOCSFLAGS.
710 1.2.8.2 martin */
711 1.2.8.2 martin if (ISSET(sc->sc_hwflags, ZYNQUART_HW_CONSOLE)) {
712 1.2.8.2 martin t.c_ospeed = zynquartconsrate;
713 1.2.8.2 martin t.c_cflag = zynquartconscflag;
714 1.2.8.2 martin } else {
715 1.2.8.2 martin t.c_ospeed = TTYDEF_SPEED;
716 1.2.8.2 martin t.c_cflag = TTYDEF_CFLAG;
717 1.2.8.2 martin }
718 1.2.8.2 martin t.c_ispeed = t.c_ospeed;
719 1.2.8.2 martin if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
720 1.2.8.2 martin SET(t.c_cflag, CLOCAL);
721 1.2.8.2 martin if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
722 1.2.8.2 martin SET(t.c_cflag, CRTSCTS);
723 1.2.8.2 martin if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
724 1.2.8.2 martin SET(t.c_cflag, MDMBUF);
725 1.2.8.2 martin /* Make sure zynquartparam() will do something. */
726 1.2.8.2 martin tp->t_ospeed = 0;
727 1.2.8.2 martin (void) zynquartparam(tp, &t);
728 1.2.8.2 martin tp->t_iflag = TTYDEF_IFLAG;
729 1.2.8.2 martin tp->t_oflag = TTYDEF_OFLAG;
730 1.2.8.2 martin tp->t_lflag = TTYDEF_LFLAG;
731 1.2.8.2 martin ttychars(tp);
732 1.2.8.2 martin ttsetwater(tp);
733 1.2.8.2 martin
734 1.2.8.2 martin mutex_spin_enter(&sc->sc_lock);
735 1.2.8.2 martin
736 1.2.8.2 martin /*
737 1.2.8.2 martin * Turn on DTR. We must always do this, even if carrier is not
738 1.2.8.2 martin * present, because otherwise we'd have to use TIOCSDTR
739 1.2.8.2 martin * immediately after setting CLOCAL, which applications do not
740 1.2.8.2 martin * expect. We always assert DTR while the device is open
741 1.2.8.2 martin * unless explicitly requested to deassert it.
742 1.2.8.2 martin */
743 1.2.8.2 martin zynquart_modem(sc, 1);
744 1.2.8.2 martin
745 1.2.8.2 martin /* Clear the input ring, and unblock. */
746 1.2.8.2 martin sc->sc_rbuf_in = sc->sc_rbuf_out = 0;
747 1.2.8.2 martin zynquart_iflush(sc);
748 1.2.8.2 martin CLR(sc->sc_rx_flags, ZYNQUART_RX_ANY_BLOCK);
749 1.2.8.2 martin zynquart_hwiflow(sc);
750 1.2.8.2 martin
751 1.2.8.2 martin /* Turn on interrupts. */
752 1.2.8.2 martin zynquart_control_rxint(sc, true);
753 1.2.8.2 martin
754 1.2.8.2 martin #ifdef ZYNQUART_DEBUG
755 1.2.8.2 martin if (zynquart_debug)
756 1.2.8.2 martin zynquartstatus(sc, "zynquartopen ");
757 1.2.8.2 martin #endif
758 1.2.8.2 martin
759 1.2.8.2 martin mutex_spin_exit(&sc->sc_lock);
760 1.2.8.2 martin }
761 1.2.8.2 martin
762 1.2.8.2 martin splx(s);
763 1.2.8.2 martin
764 1.2.8.2 martin #if 0
765 1.2.8.2 martin error = ttyopen(tp, ZYNQUART_DIALOUT(dev), ISSET(flag, O_NONBLOCK));
766 1.2.8.2 martin #else
767 1.2.8.2 martin error = ttyopen(tp, 1, ISSET(flag, O_NONBLOCK));
768 1.2.8.2 martin #endif
769 1.2.8.2 martin if (error)
770 1.2.8.2 martin goto bad;
771 1.2.8.2 martin
772 1.2.8.2 martin error = (*tp->t_linesw->l_open)(dev, tp);
773 1.2.8.2 martin if (error)
774 1.2.8.2 martin goto bad;
775 1.2.8.2 martin
776 1.2.8.2 martin return (0);
777 1.2.8.2 martin
778 1.2.8.2 martin bad:
779 1.2.8.2 martin if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
780 1.2.8.2 martin /*
781 1.2.8.2 martin * We failed to open the device, and nobody else had it opened.
782 1.2.8.2 martin * Clean up the state as appropriate.
783 1.2.8.2 martin */
784 1.2.8.2 martin zynquart_shutdown(sc);
785 1.2.8.2 martin }
786 1.2.8.2 martin
787 1.2.8.2 martin return (error);
788 1.2.8.2 martin }
789 1.2.8.2 martin
790 1.2.8.2 martin int
791 1.2.8.2 martin zynquartclose(dev_t dev, int flag, int mode, struct lwp *l)
792 1.2.8.2 martin {
793 1.2.8.2 martin struct zynquart_softc *sc =
794 1.2.8.2 martin device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(dev));
795 1.2.8.2 martin struct tty *tp = sc->sc_tty;
796 1.2.8.2 martin
797 1.2.8.2 martin /* XXX This is for cons.c. */
798 1.2.8.2 martin if (!ISSET(tp->t_state, TS_ISOPEN))
799 1.2.8.2 martin return (0);
800 1.2.8.2 martin
801 1.2.8.2 martin (*tp->t_linesw->l_close)(tp, flag);
802 1.2.8.2 martin ttyclose(tp);
803 1.2.8.2 martin
804 1.2.8.2 martin if (ZYNQUART_ISALIVE(sc) == 0)
805 1.2.8.2 martin return (0);
806 1.2.8.2 martin
807 1.2.8.2 martin if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
808 1.2.8.2 martin /*
809 1.2.8.2 martin * Although we got a last close, the device may still be in
810 1.2.8.2 martin * use; e.g. if this was the dialout node, and there are still
811 1.2.8.2 martin * processes waiting for carrier on the non-dialout node.
812 1.2.8.2 martin */
813 1.2.8.2 martin zynquart_shutdown(sc);
814 1.2.8.2 martin }
815 1.2.8.2 martin
816 1.2.8.2 martin return (0);
817 1.2.8.2 martin }
818 1.2.8.2 martin
819 1.2.8.2 martin int
820 1.2.8.2 martin zynquartread(dev_t dev, struct uio *uio, int flag)
821 1.2.8.2 martin {
822 1.2.8.2 martin struct zynquart_softc *sc =
823 1.2.8.2 martin device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(dev));
824 1.2.8.2 martin struct tty *tp = sc->sc_tty;
825 1.2.8.2 martin
826 1.2.8.2 martin if (ZYNQUART_ISALIVE(sc) == 0)
827 1.2.8.2 martin return (EIO);
828 1.2.8.2 martin
829 1.2.8.2 martin return ((*tp->t_linesw->l_read)(tp, uio, flag));
830 1.2.8.2 martin }
831 1.2.8.2 martin
832 1.2.8.2 martin int
833 1.2.8.2 martin zynquartwrite(dev_t dev, struct uio *uio, int flag)
834 1.2.8.2 martin {
835 1.2.8.2 martin struct zynquart_softc *sc =
836 1.2.8.2 martin device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(dev));
837 1.2.8.2 martin struct tty *tp = sc->sc_tty;
838 1.2.8.2 martin
839 1.2.8.2 martin if (ZYNQUART_ISALIVE(sc) == 0)
840 1.2.8.2 martin return (EIO);
841 1.2.8.2 martin
842 1.2.8.2 martin return ((*tp->t_linesw->l_write)(tp, uio, flag));
843 1.2.8.2 martin }
844 1.2.8.2 martin
845 1.2.8.2 martin int
846 1.2.8.2 martin zynquartpoll(dev_t dev, int events, struct lwp *l)
847 1.2.8.2 martin {
848 1.2.8.2 martin struct zynquart_softc *sc =
849 1.2.8.2 martin device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(dev));
850 1.2.8.2 martin struct tty *tp = sc->sc_tty;
851 1.2.8.2 martin
852 1.2.8.2 martin if (ZYNQUART_ISALIVE(sc) == 0)
853 1.2.8.2 martin return (POLLHUP);
854 1.2.8.2 martin
855 1.2.8.2 martin return ((*tp->t_linesw->l_poll)(tp, events, l));
856 1.2.8.2 martin }
857 1.2.8.2 martin
858 1.2.8.2 martin struct tty *
859 1.2.8.2 martin zynquarttty(dev_t dev)
860 1.2.8.2 martin {
861 1.2.8.2 martin struct zynquart_softc *sc =
862 1.2.8.2 martin device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(dev));
863 1.2.8.2 martin struct tty *tp = sc->sc_tty;
864 1.2.8.2 martin
865 1.2.8.2 martin return (tp);
866 1.2.8.2 martin }
867 1.2.8.2 martin
868 1.2.8.2 martin int
869 1.2.8.2 martin zynquartioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
870 1.2.8.2 martin {
871 1.2.8.2 martin struct zynquart_softc *sc;
872 1.2.8.2 martin struct tty *tp;
873 1.2.8.2 martin int error;
874 1.2.8.2 martin
875 1.2.8.2 martin sc = device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(dev));
876 1.2.8.2 martin if (sc == NULL)
877 1.2.8.2 martin return ENXIO;
878 1.2.8.2 martin if (ZYNQUART_ISALIVE(sc) == 0)
879 1.2.8.2 martin return (EIO);
880 1.2.8.2 martin
881 1.2.8.2 martin tp = sc->sc_tty;
882 1.2.8.2 martin
883 1.2.8.2 martin error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
884 1.2.8.2 martin if (error != EPASSTHROUGH)
885 1.2.8.2 martin return (error);
886 1.2.8.2 martin
887 1.2.8.2 martin error = ttioctl(tp, cmd, data, flag, l);
888 1.2.8.2 martin if (error != EPASSTHROUGH)
889 1.2.8.2 martin return (error);
890 1.2.8.2 martin
891 1.2.8.2 martin error = 0;
892 1.2.8.2 martin switch (cmd) {
893 1.2.8.2 martin case TIOCSFLAGS:
894 1.2.8.2 martin error = kauth_authorize_device_tty(l->l_cred,
895 1.2.8.2 martin KAUTH_DEVICE_TTY_PRIVSET, tp);
896 1.2.8.2 martin break;
897 1.2.8.2 martin default:
898 1.2.8.2 martin /* nothing */
899 1.2.8.2 martin break;
900 1.2.8.2 martin }
901 1.2.8.2 martin if (error) {
902 1.2.8.2 martin return error;
903 1.2.8.2 martin }
904 1.2.8.2 martin
905 1.2.8.2 martin mutex_spin_enter(&sc->sc_lock);
906 1.2.8.2 martin
907 1.2.8.2 martin switch (cmd) {
908 1.2.8.2 martin case TIOCSBRK:
909 1.2.8.2 martin zynquart_break(sc, true);
910 1.2.8.2 martin break;
911 1.2.8.2 martin
912 1.2.8.2 martin case TIOCCBRK:
913 1.2.8.2 martin zynquart_break(sc, false);
914 1.2.8.2 martin break;
915 1.2.8.2 martin
916 1.2.8.2 martin case TIOCSDTR:
917 1.2.8.2 martin zynquart_modem(sc, 1);
918 1.2.8.2 martin break;
919 1.2.8.2 martin
920 1.2.8.2 martin case TIOCCDTR:
921 1.2.8.2 martin zynquart_modem(sc, 0);
922 1.2.8.2 martin break;
923 1.2.8.2 martin
924 1.2.8.2 martin case TIOCGFLAGS:
925 1.2.8.2 martin *(int *)data = sc->sc_swflags;
926 1.2.8.2 martin break;
927 1.2.8.2 martin
928 1.2.8.2 martin case TIOCSFLAGS:
929 1.2.8.2 martin sc->sc_swflags = *(int *)data;
930 1.2.8.2 martin break;
931 1.2.8.2 martin
932 1.2.8.2 martin case TIOCMSET:
933 1.2.8.2 martin case TIOCMBIS:
934 1.2.8.2 martin case TIOCMBIC:
935 1.2.8.2 martin tiocm_to_zynquart(sc, cmd, *(int *)data);
936 1.2.8.2 martin break;
937 1.2.8.2 martin
938 1.2.8.2 martin case TIOCMGET:
939 1.2.8.2 martin *(int *)data = zynquart_to_tiocm(sc);
940 1.2.8.2 martin break;
941 1.2.8.2 martin
942 1.2.8.2 martin #ifdef notyet
943 1.2.8.2 martin case PPS_IOC_CREATE:
944 1.2.8.2 martin case PPS_IOC_DESTROY:
945 1.2.8.2 martin case PPS_IOC_GETPARAMS:
946 1.2.8.2 martin case PPS_IOC_SETPARAMS:
947 1.2.8.2 martin case PPS_IOC_GETCAP:
948 1.2.8.2 martin case PPS_IOC_FETCH:
949 1.2.8.2 martin #ifdef PPS_SYNC
950 1.2.8.2 martin case PPS_IOC_KCBIND:
951 1.2.8.2 martin #endif
952 1.2.8.2 martin mutex_spin_enter(&timecounter_lock);
953 1.2.8.2 martin error = pps_ioctl(cmd, data, &sc->sc_pps_state);
954 1.2.8.2 martin mutex_spin_exit(&timecounter_lock);
955 1.2.8.2 martin break;
956 1.2.8.2 martin
957 1.2.8.2 martin case TIOCDCDTIMESTAMP: /* XXX old, overloaded API used by xntpd v3 */
958 1.2.8.2 martin mutex_spin_enter(&timecounter_lock);
959 1.2.8.2 martin #ifndef PPS_TRAILING_EDGE
960 1.2.8.2 martin TIMESPEC_TO_TIMEVAL((struct timeval *)data,
961 1.2.8.2 martin &sc->sc_pps_state.ppsinfo.assert_timestamp);
962 1.2.8.2 martin #else
963 1.2.8.2 martin TIMESPEC_TO_TIMEVAL((struct timeval *)data,
964 1.2.8.2 martin &sc->sc_pps_state.ppsinfo.clear_timestamp);
965 1.2.8.2 martin #endif
966 1.2.8.2 martin mutex_spin_exit(&timecounter_lock);
967 1.2.8.2 martin break;
968 1.2.8.2 martin #endif
969 1.2.8.2 martin
970 1.2.8.2 martin default:
971 1.2.8.2 martin error = EPASSTHROUGH;
972 1.2.8.2 martin break;
973 1.2.8.2 martin }
974 1.2.8.2 martin
975 1.2.8.2 martin mutex_spin_exit(&sc->sc_lock);
976 1.2.8.2 martin
977 1.2.8.2 martin #ifdef ZYNQUART_DEBUG
978 1.2.8.2 martin if (zynquart_debug)
979 1.2.8.2 martin zynquartstatus(sc, "zynquartioctl ");
980 1.2.8.2 martin #endif
981 1.2.8.2 martin
982 1.2.8.2 martin return (error);
983 1.2.8.2 martin }
984 1.2.8.2 martin
985 1.2.8.2 martin integrate void
986 1.2.8.2 martin zynquart_schedrx(struct zynquart_softc *sc)
987 1.2.8.2 martin {
988 1.2.8.2 martin sc->sc_rx_ready = 1;
989 1.2.8.2 martin
990 1.2.8.2 martin /* Wake up the poller. */
991 1.2.8.2 martin softint_schedule(sc->sc_si);
992 1.2.8.2 martin }
993 1.2.8.2 martin
994 1.2.8.2 martin void
995 1.2.8.2 martin zynquart_break(struct zynquart_softc *sc, bool onoff)
996 1.2.8.2 martin {
997 1.2.8.2 martin bus_space_tag_t iot = sc->sc_regs.ur_iot;
998 1.2.8.2 martin bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
999 1.2.8.2 martin
1000 1.2.8.2 martin if (onoff)
1001 1.2.8.2 martin SET(sc->sc_cr, CR_STPBRK);
1002 1.2.8.2 martin else
1003 1.2.8.2 martin CLR(sc->sc_cr, CR_STPBRK);
1004 1.2.8.2 martin
1005 1.2.8.2 martin bus_space_write_4(iot, ioh, UART_CONTROL, sc->sc_cr);
1006 1.2.8.2 martin }
1007 1.2.8.2 martin
1008 1.2.8.2 martin void
1009 1.2.8.2 martin zynquart_modem(struct zynquart_softc *sc, int onoff)
1010 1.2.8.2 martin {
1011 1.2.8.2 martin #ifdef notyet
1012 1.2.8.2 martin if (sc->sc_mcr_dtr == 0)
1013 1.2.8.2 martin return;
1014 1.2.8.2 martin
1015 1.2.8.2 martin if (onoff)
1016 1.2.8.2 martin SET(sc->sc_mcr, sc->sc_mcr_dtr);
1017 1.2.8.2 martin else
1018 1.2.8.2 martin CLR(sc->sc_mcr, sc->sc_mcr_dtr);
1019 1.2.8.2 martin
1020 1.2.8.2 martin if (!sc->sc_heldchange) {
1021 1.2.8.2 martin if (sc->sc_tx_busy) {
1022 1.2.8.2 martin sc->sc_heldtbc = sc->sc_tbc;
1023 1.2.8.2 martin sc->sc_tbc = 0;
1024 1.2.8.2 martin sc->sc_heldchange = 1;
1025 1.2.8.2 martin } else
1026 1.2.8.2 martin zynquart_loadchannelregs(sc);
1027 1.2.8.2 martin }
1028 1.2.8.2 martin #endif
1029 1.2.8.2 martin }
1030 1.2.8.2 martin
1031 1.2.8.2 martin void
1032 1.2.8.2 martin tiocm_to_zynquart(struct zynquart_softc *sc, u_long how, int ttybits)
1033 1.2.8.2 martin {
1034 1.2.8.2 martin bus_space_tag_t iot = sc->sc_regs.ur_iot;
1035 1.2.8.2 martin bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1036 1.2.8.2 martin
1037 1.2.8.2 martin u_char combits;
1038 1.2.8.2 martin
1039 1.2.8.2 martin combits = 0;
1040 1.2.8.2 martin if (ISSET(ttybits, TIOCM_DTR))
1041 1.2.8.2 martin SET(combits, MODEMCR_DTR);
1042 1.2.8.2 martin if (ISSET(ttybits, TIOCM_RTS))
1043 1.2.8.2 martin SET(combits, MODEMCR_RTS);
1044 1.2.8.2 martin
1045 1.2.8.2 martin switch (how) {
1046 1.2.8.2 martin case TIOCMBIC:
1047 1.2.8.2 martin CLR(sc->sc_mcr, combits);
1048 1.2.8.2 martin break;
1049 1.2.8.2 martin
1050 1.2.8.2 martin case TIOCMBIS:
1051 1.2.8.2 martin SET(sc->sc_mcr, combits);
1052 1.2.8.2 martin break;
1053 1.2.8.2 martin
1054 1.2.8.2 martin case TIOCMSET:
1055 1.2.8.2 martin CLR(sc->sc_mcr, MODEMCR_DTR | MODEMCR_RTS);
1056 1.2.8.2 martin SET(sc->sc_mcr, combits);
1057 1.2.8.2 martin break;
1058 1.2.8.2 martin }
1059 1.2.8.2 martin
1060 1.2.8.2 martin bus_space_write_4(iot, ioh, UART_MODEM_CTRL, sc->sc_mcr);
1061 1.2.8.2 martin }
1062 1.2.8.2 martin
1063 1.2.8.2 martin int
1064 1.2.8.2 martin zynquart_to_tiocm(struct zynquart_softc *sc)
1065 1.2.8.2 martin {
1066 1.2.8.2 martin #ifdef notyet
1067 1.2.8.2 martin bus_space_tag_t iot = sc->sc_regs.ur_iot;
1068 1.2.8.2 martin bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1069 1.2.8.2 martin #endif
1070 1.2.8.2 martin uint32_t combits;
1071 1.2.8.2 martin int ttybits = 0;
1072 1.2.8.2 martin
1073 1.2.8.2 martin combits = sc->sc_mcr;
1074 1.2.8.2 martin if (ISSET(combits, MODEMCR_DTR))
1075 1.2.8.2 martin SET(ttybits, TIOCM_DTR);
1076 1.2.8.2 martin if (ISSET(combits, MODEMCR_RTS))
1077 1.2.8.2 martin SET(ttybits, TIOCM_RTS);
1078 1.2.8.2 martin
1079 1.2.8.2 martin combits = sc->sc_msr;
1080 1.2.8.2 martin if (ISSET(combits, MODEMSR_DCD))
1081 1.2.8.2 martin SET(ttybits, TIOCM_CD);
1082 1.2.8.2 martin if (ISSET(combits, MODEMSR_CTS))
1083 1.2.8.2 martin SET(ttybits, TIOCM_CTS);
1084 1.2.8.2 martin if (ISSET(combits, MODEMSR_DSR))
1085 1.2.8.2 martin SET(ttybits, TIOCM_DSR);
1086 1.2.8.2 martin if (ISSET(combits, MODEMSR_RI | MODEMSR_TERI))
1087 1.2.8.2 martin SET(ttybits, TIOCM_RI);
1088 1.2.8.2 martin
1089 1.2.8.2 martin #ifdef notyet
1090 1.2.8.2 martin combits = bus_space_read_4(iot, ioh, UART_INTRPT_MASK);
1091 1.2.8.2 martin if (ISSET(sc->sc_imr, IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC))
1092 1.2.8.2 martin SET(ttybits, TIOCM_LE);
1093 1.2.8.2 martin #endif
1094 1.2.8.2 martin
1095 1.2.8.2 martin return (ttybits);
1096 1.2.8.2 martin }
1097 1.2.8.2 martin
1098 1.2.8.2 martin static uint32_t
1099 1.2.8.2 martin cflag_to_zynquart(tcflag_t cflag, uint32_t oldval)
1100 1.2.8.2 martin {
1101 1.2.8.2 martin uint32_t val = oldval;
1102 1.2.8.2 martin
1103 1.2.8.2 martin CLR(val, MR_CHMODE | MR_NBSTOP | MR_PAR | MR_CHRL | MR_CLKS);
1104 1.2.8.2 martin
1105 1.2.8.2 martin switch (cflag & CSIZE) {
1106 1.2.8.2 martin case CS5:
1107 1.2.8.2 martin /* not suppreted. use 7-bits */
1108 1.2.8.2 martin case CS6:
1109 1.2.8.2 martin SET(val, CHRL_6BIT);
1110 1.2.8.2 martin break;
1111 1.2.8.2 martin case CS7:
1112 1.2.8.2 martin SET(val, CHRL_7BIT);
1113 1.2.8.2 martin break;
1114 1.2.8.2 martin case CS8:
1115 1.2.8.2 martin SET(val, CHRL_8BIT);
1116 1.2.8.2 martin break;
1117 1.2.8.2 martin }
1118 1.2.8.2 martin
1119 1.2.8.2 martin if (ISSET(cflag, PARENB)) {
1120 1.2.8.2 martin /* odd parity */
1121 1.2.8.2 martin if (!ISSET(cflag, PARODD))
1122 1.2.8.2 martin SET(val, PAR_ODD);
1123 1.2.8.2 martin else
1124 1.2.8.2 martin SET(val, PAR_EVEN);
1125 1.2.8.2 martin } else {
1126 1.2.8.2 martin SET(val, PAR_NONE);
1127 1.2.8.2 martin }
1128 1.2.8.2 martin
1129 1.2.8.2 martin if (ISSET(cflag, CSTOPB))
1130 1.2.8.2 martin SET(val, NBSTOP_2);
1131 1.2.8.2 martin
1132 1.2.8.2 martin return val;
1133 1.2.8.2 martin }
1134 1.2.8.2 martin
1135 1.2.8.2 martin int
1136 1.2.8.2 martin zynquartparam(struct tty *tp, struct termios *t)
1137 1.2.8.2 martin {
1138 1.2.8.2 martin struct zynquart_softc *sc =
1139 1.2.8.2 martin device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(tp->t_dev));
1140 1.2.8.2 martin struct zynquart_baudrate_ratio ratio;
1141 1.2.8.2 martin uint32_t mcr;
1142 1.2.8.2 martin bool change_speed = tp->t_ospeed != t->c_ospeed;
1143 1.2.8.2 martin
1144 1.2.8.2 martin if (ZYNQUART_ISALIVE(sc) == 0)
1145 1.2.8.2 martin return (EIO);
1146 1.2.8.2 martin
1147 1.2.8.2 martin /* Check requested parameters. */
1148 1.2.8.2 martin if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
1149 1.2.8.2 martin return (EINVAL);
1150 1.2.8.2 martin
1151 1.2.8.2 martin /*
1152 1.2.8.2 martin * For the console, always force CLOCAL and !HUPCL, so that the port
1153 1.2.8.2 martin * is always active.
1154 1.2.8.2 martin */
1155 1.2.8.2 martin if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
1156 1.2.8.2 martin ISSET(sc->sc_hwflags, ZYNQUART_HW_CONSOLE)) {
1157 1.2.8.2 martin SET(t->c_cflag, CLOCAL);
1158 1.2.8.2 martin CLR(t->c_cflag, HUPCL);
1159 1.2.8.2 martin }
1160 1.2.8.2 martin
1161 1.2.8.2 martin /*
1162 1.2.8.2 martin * If there were no changes, don't do anything. This avoids dropping
1163 1.2.8.2 martin * input and improves performance when all we did was frob things like
1164 1.2.8.2 martin * VMIN and VTIME.
1165 1.2.8.2 martin */
1166 1.2.8.2 martin if ( !change_speed && tp->t_cflag == t->c_cflag)
1167 1.2.8.2 martin return (0);
1168 1.2.8.2 martin
1169 1.2.8.2 martin if (change_speed) {
1170 1.2.8.2 martin /* calculate baudrate modulator value */
1171 1.2.8.2 martin if (zynquartspeed(t->c_ospeed, &ratio) < 0)
1172 1.2.8.2 martin return (EINVAL);
1173 1.2.8.2 martin sc->sc_ratio = ratio;
1174 1.2.8.2 martin }
1175 1.2.8.2 martin
1176 1.2.8.2 martin mcr = cflag_to_zynquart(t->c_cflag, sc->sc_mcr);
1177 1.2.8.2 martin
1178 1.2.8.2 martin mutex_spin_enter(&sc->sc_lock);
1179 1.2.8.2 martin
1180 1.2.8.2 martin #if 0
1181 1.2.8.2 martin /* flow control stuff. not yet */
1182 1.2.8.2 martin /*
1183 1.2.8.2 martin * If we're not in a mode that assumes a connection is present, then
1184 1.2.8.2 martin * ignore carrier changes.
1185 1.2.8.2 martin */
1186 1.2.8.2 martin if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
1187 1.2.8.2 martin sc->sc_msr_dcd = 0;
1188 1.2.8.2 martin else
1189 1.2.8.2 martin sc->sc_msr_dcd = MSR_DCD;
1190 1.2.8.2 martin /*
1191 1.2.8.2 martin * Set the flow control pins depending on the current flow control
1192 1.2.8.2 martin * mode.
1193 1.2.8.2 martin */
1194 1.2.8.2 martin if (ISSET(t->c_cflag, CRTSCTS)) {
1195 1.2.8.2 martin sc->sc_mcr_dtr = MCR_DTR;
1196 1.2.8.2 martin sc->sc_mcr_rts = MCR_RTS;
1197 1.2.8.2 martin sc->sc_msr_cts = MSR_CTS;
1198 1.2.8.2 martin sc->sc_efr = EFR_AUTORTS | EFR_AUTOCTS;
1199 1.2.8.2 martin } else if (ISSET(t->c_cflag, MDMBUF)) {
1200 1.2.8.2 martin /*
1201 1.2.8.2 martin * For DTR/DCD flow control, make sure we don't toggle DTR for
1202 1.2.8.2 martin * carrier detection.
1203 1.2.8.2 martin */
1204 1.2.8.2 martin sc->sc_mcr_dtr = 0;
1205 1.2.8.2 martin sc->sc_mcr_rts = MCR_DTR;
1206 1.2.8.2 martin sc->sc_msr_cts = MSR_DCD;
1207 1.2.8.2 martin sc->sc_efr = 0;
1208 1.2.8.2 martin } else {
1209 1.2.8.2 martin /*
1210 1.2.8.2 martin * If no flow control, then always set RTS. This will make
1211 1.2.8.2 martin * the other side happy if it mistakenly thinks we're doing
1212 1.2.8.2 martin * RTS/CTS flow control.
1213 1.2.8.2 martin */
1214 1.2.8.2 martin sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
1215 1.2.8.2 martin sc->sc_mcr_rts = 0;
1216 1.2.8.2 martin sc->sc_msr_cts = 0;
1217 1.2.8.2 martin sc->sc_efr = 0;
1218 1.2.8.2 martin if (ISSET(sc->sc_mcr, MCR_DTR))
1219 1.2.8.2 martin SET(sc->sc_mcr, MCR_RTS);
1220 1.2.8.2 martin else
1221 1.2.8.2 martin CLR(sc->sc_mcr, MCR_RTS);
1222 1.2.8.2 martin }
1223 1.2.8.2 martin sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
1224 1.2.8.2 martin #endif
1225 1.2.8.2 martin
1226 1.2.8.2 martin /* And copy to tty. */
1227 1.2.8.2 martin tp->t_ispeed = t->c_ospeed;
1228 1.2.8.2 martin tp->t_ospeed = t->c_ospeed;
1229 1.2.8.2 martin tp->t_cflag = t->c_cflag;
1230 1.2.8.2 martin
1231 1.2.8.2 martin if (!change_speed && mcr == sc->sc_mcr) {
1232 1.2.8.2 martin /* noop */
1233 1.2.8.2 martin } else if (!sc->sc_pending && !sc->sc_tx_busy) {
1234 1.2.8.2 martin if (mcr != sc->sc_mcr) {
1235 1.2.8.2 martin sc->sc_mcr = mcr;
1236 1.2.8.2 martin zynquart_load_params(sc);
1237 1.2.8.2 martin }
1238 1.2.8.2 martin if (change_speed)
1239 1.2.8.2 martin zynquart_load_speed(sc);
1240 1.2.8.2 martin } else {
1241 1.2.8.2 martin if (!sc->sc_pending) {
1242 1.2.8.2 martin sc->sc_heldtbc = sc->sc_tbc;
1243 1.2.8.2 martin sc->sc_tbc = 0;
1244 1.2.8.2 martin }
1245 1.2.8.2 martin sc->sc_pending |=
1246 1.2.8.2 martin (mcr == sc->sc_mcr ? 0 : ZYNQUART_PEND_PARAM) |
1247 1.2.8.2 martin (change_speed ? 0 : ZYNQUART_PEND_SPEED);
1248 1.2.8.2 martin sc->sc_mcr = mcr;
1249 1.2.8.2 martin }
1250 1.2.8.2 martin
1251 1.2.8.2 martin if (!ISSET(t->c_cflag, CHWFLOW)) {
1252 1.2.8.2 martin /* Disable the high water mark. */
1253 1.2.8.2 martin sc->sc_r_hiwat = 0;
1254 1.2.8.2 martin sc->sc_r_lowat = 0;
1255 1.2.8.2 martin if (ISSET(sc->sc_rx_flags, ZYNQUART_RX_TTY_OVERFLOWED)) {
1256 1.2.8.2 martin CLR(sc->sc_rx_flags, ZYNQUART_RX_TTY_OVERFLOWED);
1257 1.2.8.2 martin zynquart_schedrx(sc);
1258 1.2.8.2 martin }
1259 1.2.8.2 martin if (ISSET(sc->sc_rx_flags,
1260 1.2.8.2 martin ZYNQUART_RX_TTY_BLOCKED|ZYNQUART_RX_IBUF_BLOCKED)) {
1261 1.2.8.2 martin CLR(sc->sc_rx_flags,
1262 1.2.8.2 martin ZYNQUART_RX_TTY_BLOCKED|ZYNQUART_RX_IBUF_BLOCKED);
1263 1.2.8.2 martin zynquart_hwiflow(sc);
1264 1.2.8.2 martin }
1265 1.2.8.2 martin } else {
1266 1.2.8.2 martin sc->sc_r_hiwat = zynquart_rbuf_hiwat;
1267 1.2.8.2 martin sc->sc_r_lowat = zynquart_rbuf_lowat;
1268 1.2.8.2 martin }
1269 1.2.8.2 martin
1270 1.2.8.2 martin mutex_spin_exit(&sc->sc_lock);
1271 1.2.8.2 martin
1272 1.2.8.2 martin /*
1273 1.2.8.2 martin * Update the tty layer's idea of the carrier bit, in case we changed
1274 1.2.8.2 martin * CLOCAL or MDMBUF. We don't hang up here; we only do that by
1275 1.2.8.2 martin * explicit request.
1276 1.2.8.2 martin */
1277 1.2.8.2 martin (void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, MODEMSR_DCD));
1278 1.2.8.2 martin
1279 1.2.8.2 martin #ifdef ZYNQUART_DEBUG
1280 1.2.8.2 martin if (zynquart_debug)
1281 1.2.8.2 martin zynquartstatus(sc, "zynquartparam ");
1282 1.2.8.2 martin #endif
1283 1.2.8.2 martin
1284 1.2.8.2 martin if (!ISSET(t->c_cflag, CHWFLOW)) {
1285 1.2.8.2 martin if (sc->sc_tx_stopped) {
1286 1.2.8.2 martin sc->sc_tx_stopped = 0;
1287 1.2.8.2 martin zynquartstart(tp);
1288 1.2.8.2 martin }
1289 1.2.8.2 martin }
1290 1.2.8.2 martin
1291 1.2.8.2 martin return (0);
1292 1.2.8.2 martin }
1293 1.2.8.2 martin
1294 1.2.8.2 martin void
1295 1.2.8.2 martin zynquart_iflush(struct zynquart_softc *sc)
1296 1.2.8.2 martin {
1297 1.2.8.2 martin bus_space_tag_t iot = sc->sc_regs.ur_iot;
1298 1.2.8.2 martin bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1299 1.2.8.2 martin #ifdef DIAGNOSTIC
1300 1.2.8.2 martin uint32_t reg = 0xffff;
1301 1.2.8.2 martin #endif
1302 1.2.8.2 martin int timo;
1303 1.2.8.2 martin
1304 1.2.8.2 martin timo = 50000;
1305 1.2.8.2 martin /* flush any pending I/O */
1306 1.2.8.2 martin while (!ISSET(bus_space_read_4(iot, ioh, UART_CHANNEL_STS), STS_REMPTY) &&
1307 1.2.8.2 martin --timo)
1308 1.2.8.2 martin #ifdef DIAGNOSTIC
1309 1.2.8.2 martin reg =
1310 1.2.8.2 martin #else
1311 1.2.8.2 martin (void)
1312 1.2.8.2 martin #endif
1313 1.2.8.2 martin bus_space_read_4(iot, ioh, UART_TX_RX_FIFO);
1314 1.2.8.2 martin
1315 1.2.8.2 martin #ifdef DIAGNOSTIC
1316 1.2.8.2 martin if (!timo)
1317 1.2.8.2 martin aprint_error_dev(sc->sc_dev, "zynquart_iflush timeout %02x\n", reg);
1318 1.2.8.2 martin #endif
1319 1.2.8.2 martin }
1320 1.2.8.2 martin
1321 1.2.8.2 martin int
1322 1.2.8.2 martin zynquarthwiflow(struct tty *tp, int block)
1323 1.2.8.2 martin {
1324 1.2.8.2 martin struct zynquart_softc *sc =
1325 1.2.8.2 martin device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(tp->t_dev));
1326 1.2.8.2 martin
1327 1.2.8.2 martin if (ZYNQUART_ISALIVE(sc) == 0)
1328 1.2.8.2 martin return (0);
1329 1.2.8.2 martin
1330 1.2.8.2 martin #ifdef notyet
1331 1.2.8.2 martin if (sc->sc_mcr_rts == 0)
1332 1.2.8.2 martin return (0);
1333 1.2.8.2 martin #endif
1334 1.2.8.2 martin
1335 1.2.8.2 martin mutex_spin_enter(&sc->sc_lock);
1336 1.2.8.2 martin
1337 1.2.8.2 martin if (block) {
1338 1.2.8.2 martin if (!ISSET(sc->sc_rx_flags, ZYNQUART_RX_TTY_BLOCKED)) {
1339 1.2.8.2 martin SET(sc->sc_rx_flags, ZYNQUART_RX_TTY_BLOCKED);
1340 1.2.8.2 martin zynquart_hwiflow(sc);
1341 1.2.8.2 martin }
1342 1.2.8.2 martin } else {
1343 1.2.8.2 martin if (ISSET(sc->sc_rx_flags, ZYNQUART_RX_TTY_OVERFLOWED)) {
1344 1.2.8.2 martin CLR(sc->sc_rx_flags, ZYNQUART_RX_TTY_OVERFLOWED);
1345 1.2.8.2 martin zynquart_schedrx(sc);
1346 1.2.8.2 martin }
1347 1.2.8.2 martin if (ISSET(sc->sc_rx_flags, ZYNQUART_RX_TTY_BLOCKED)) {
1348 1.2.8.2 martin CLR(sc->sc_rx_flags, ZYNQUART_RX_TTY_BLOCKED);
1349 1.2.8.2 martin zynquart_hwiflow(sc);
1350 1.2.8.2 martin }
1351 1.2.8.2 martin }
1352 1.2.8.2 martin
1353 1.2.8.2 martin mutex_spin_exit(&sc->sc_lock);
1354 1.2.8.2 martin return (1);
1355 1.2.8.2 martin }
1356 1.2.8.2 martin
1357 1.2.8.2 martin /*
1358 1.2.8.2 martin * (un)block input via hw flowcontrol
1359 1.2.8.2 martin */
1360 1.2.8.2 martin void
1361 1.2.8.2 martin zynquart_hwiflow(struct zynquart_softc *sc)
1362 1.2.8.2 martin {
1363 1.2.8.2 martin #ifdef notyet
1364 1.2.8.2 martin struct zynquart_regs *regsp= &sc->sc_regs;
1365 1.2.8.2 martin
1366 1.2.8.2 martin if (sc->sc_mcr_rts == 0)
1367 1.2.8.2 martin return;
1368 1.2.8.2 martin
1369 1.2.8.2 martin if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
1370 1.2.8.2 martin CLR(sc->sc_mcr, sc->sc_mcr_rts);
1371 1.2.8.2 martin CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
1372 1.2.8.2 martin } else {
1373 1.2.8.2 martin SET(sc->sc_mcr, sc->sc_mcr_rts);
1374 1.2.8.2 martin SET(sc->sc_mcr_active, sc->sc_mcr_rts);
1375 1.2.8.2 martin }
1376 1.2.8.2 martin UR_WRITE_1(regsp, ZYNQUART_REG_MCR, sc->sc_mcr_active);
1377 1.2.8.2 martin #endif
1378 1.2.8.2 martin }
1379 1.2.8.2 martin
1380 1.2.8.2 martin
1381 1.2.8.2 martin void
1382 1.2.8.2 martin zynquartstart(struct tty *tp)
1383 1.2.8.2 martin {
1384 1.2.8.2 martin struct zynquart_softc *sc =
1385 1.2.8.2 martin device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(tp->t_dev));
1386 1.2.8.2 martin int s;
1387 1.2.8.2 martin u_char *tba;
1388 1.2.8.2 martin int tbc;
1389 1.2.8.2 martin bus_space_tag_t iot = sc->sc_regs.ur_iot;
1390 1.2.8.2 martin bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1391 1.2.8.2 martin
1392 1.2.8.2 martin if (ZYNQUART_ISALIVE(sc) == 0)
1393 1.2.8.2 martin return;
1394 1.2.8.2 martin
1395 1.2.8.2 martin s = spltty();
1396 1.2.8.2 martin if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1397 1.2.8.2 martin goto out;
1398 1.2.8.2 martin if (sc->sc_tx_stopped)
1399 1.2.8.2 martin goto out;
1400 1.2.8.2 martin if (!ttypull(tp))
1401 1.2.8.2 martin goto out;
1402 1.2.8.2 martin
1403 1.2.8.2 martin /* Grab the first contiguous region of buffer space. */
1404 1.2.8.2 martin tba = tp->t_outq.c_cf;
1405 1.2.8.2 martin tbc = ndqb(&tp->t_outq, 0);
1406 1.2.8.2 martin
1407 1.2.8.2 martin mutex_spin_enter(&sc->sc_lock);
1408 1.2.8.2 martin
1409 1.2.8.2 martin sc->sc_tba = tba;
1410 1.2.8.2 martin sc->sc_tbc = tbc;
1411 1.2.8.2 martin
1412 1.2.8.2 martin SET(tp->t_state, TS_BUSY);
1413 1.2.8.2 martin sc->sc_tx_busy = 1;
1414 1.2.8.2 martin
1415 1.2.8.2 martin while (sc->sc_tbc > 0 &&
1416 1.2.8.2 martin !(bus_space_read_4(iot, ioh, UART_CHANNEL_STS) & STS_TFUL)) {
1417 1.2.8.2 martin bus_space_write_4(iot, ioh, UART_TX_RX_FIFO, *sc->sc_tba);
1418 1.2.8.2 martin sc->sc_tbc--;
1419 1.2.8.2 martin sc->sc_tba++;
1420 1.2.8.2 martin }
1421 1.2.8.2 martin
1422 1.2.8.2 martin /* Enable transmit completion interrupts */
1423 1.2.8.2 martin zynquart_control_txint(sc, true);
1424 1.2.8.2 martin
1425 1.2.8.2 martin mutex_spin_exit(&sc->sc_lock);
1426 1.2.8.2 martin out:
1427 1.2.8.2 martin splx(s);
1428 1.2.8.2 martin return;
1429 1.2.8.2 martin }
1430 1.2.8.2 martin
1431 1.2.8.2 martin /*
1432 1.2.8.2 martin * Stop output on a line.
1433 1.2.8.2 martin */
1434 1.2.8.2 martin void
1435 1.2.8.2 martin zynquartstop(struct tty *tp, int flag)
1436 1.2.8.2 martin {
1437 1.2.8.2 martin struct zynquart_softc *sc =
1438 1.2.8.2 martin device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(tp->t_dev));
1439 1.2.8.2 martin
1440 1.2.8.2 martin mutex_spin_enter(&sc->sc_lock);
1441 1.2.8.2 martin if (ISSET(tp->t_state, TS_BUSY)) {
1442 1.2.8.2 martin /* Stop transmitting at the next chunk. */
1443 1.2.8.2 martin sc->sc_tbc = 0;
1444 1.2.8.2 martin sc->sc_heldtbc = 0;
1445 1.2.8.2 martin if (!ISSET(tp->t_state, TS_TTSTOP))
1446 1.2.8.2 martin SET(tp->t_state, TS_FLUSH);
1447 1.2.8.2 martin }
1448 1.2.8.2 martin mutex_spin_exit(&sc->sc_lock);
1449 1.2.8.2 martin }
1450 1.2.8.2 martin
1451 1.2.8.2 martin void
1452 1.2.8.2 martin zynquartdiag(void *arg)
1453 1.2.8.2 martin {
1454 1.2.8.2 martin #ifdef notyet
1455 1.2.8.2 martin struct zynquart_softc *sc = arg;
1456 1.2.8.2 martin int overflows, floods;
1457 1.2.8.2 martin
1458 1.2.8.2 martin mutex_spin_enter(&sc->sc_lock);
1459 1.2.8.2 martin overflows = sc->sc_overflows;
1460 1.2.8.2 martin sc->sc_overflows = 0;
1461 1.2.8.2 martin floods = sc->sc_floods;
1462 1.2.8.2 martin sc->sc_floods = 0;
1463 1.2.8.2 martin sc->sc_errors = 0;
1464 1.2.8.2 martin mutex_spin_exit(&sc->sc_lock);
1465 1.2.8.2 martin
1466 1.2.8.2 martin log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1467 1.2.8.2 martin device_xname(sc->sc_dev),
1468 1.2.8.2 martin overflows, overflows == 1 ? "" : "s",
1469 1.2.8.2 martin floods, floods == 1 ? "" : "s");
1470 1.2.8.2 martin #endif
1471 1.2.8.2 martin }
1472 1.2.8.2 martin
1473 1.2.8.2 martin integrate void
1474 1.2.8.2 martin zynquart_rxsoft(struct zynquart_softc *sc, struct tty *tp)
1475 1.2.8.2 martin {
1476 1.2.8.2 martin int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
1477 1.2.8.2 martin u_int cc, scc, outp;
1478 1.2.8.2 martin uint16_t data;
1479 1.2.8.2 martin u_int code;
1480 1.2.8.2 martin
1481 1.2.8.2 martin scc = cc = ZYNQUART_RBUF_AVAIL(sc);
1482 1.2.8.2 martin
1483 1.2.8.2 martin #if 0
1484 1.2.8.2 martin if (cc == zynquart_rbuf_size-1) {
1485 1.2.8.2 martin sc->sc_floods++;
1486 1.2.8.2 martin if (sc->sc_errors++ == 0)
1487 1.2.8.2 martin callout_reset(&sc->sc_diag_callout, 60 * hz,
1488 1.2.8.2 martin zynquartdiag, sc);
1489 1.2.8.2 martin }
1490 1.2.8.2 martin #endif
1491 1.2.8.2 martin
1492 1.2.8.2 martin /* If not yet open, drop the entire buffer content here */
1493 1.2.8.2 martin if (!ISSET(tp->t_state, TS_ISOPEN)) {
1494 1.2.8.2 martin sc->sc_rbuf_out = sc->sc_rbuf_in;
1495 1.2.8.2 martin cc = 0;
1496 1.2.8.2 martin }
1497 1.2.8.2 martin
1498 1.2.8.2 martin outp = sc->sc_rbuf_out;
1499 1.2.8.2 martin
1500 1.2.8.2 martin #define ERRBITS (INT_PARE|INT_FRAME|INT_ROVR)
1501 1.2.8.2 martin
1502 1.2.8.2 martin while (cc) {
1503 1.2.8.2 martin data = sc->sc_rbuf[outp];
1504 1.2.8.2 martin code = data & 0xff;
1505 1.2.8.2 martin if (ISSET(__SHIFTOUT(data, ERROR_BITS), ERRBITS)) {
1506 1.2.8.2 martin if (sc->sc_errors.err == 0)
1507 1.2.8.2 martin callout_reset(&sc->sc_diag_callout,
1508 1.2.8.2 martin 60 * hz, zynquartdiag, sc);
1509 1.2.8.2 martin if (ISSET(__SHIFTOUT(data, ERROR_BITS), INT_ROVR))
1510 1.2.8.2 martin sc->sc_errors.ovrrun++;
1511 1.2.8.2 martin if (ISSET(__SHIFTOUT(data, ERROR_BITS), INT_FRAME)) {
1512 1.2.8.2 martin sc->sc_errors.frmerr++;
1513 1.2.8.2 martin SET(code, TTY_FE);
1514 1.2.8.2 martin }
1515 1.2.8.2 martin if (ISSET(__SHIFTOUT(data, ERROR_BITS), INT_PARE)) {
1516 1.2.8.2 martin sc->sc_errors.prerr++;
1517 1.2.8.2 martin SET(code, TTY_PE);
1518 1.2.8.2 martin }
1519 1.2.8.2 martin }
1520 1.2.8.2 martin if ((*rint)(code, tp) == -1) {
1521 1.2.8.2 martin /*
1522 1.2.8.2 martin * The line discipline's buffer is out of space.
1523 1.2.8.2 martin */
1524 1.2.8.2 martin if (!ISSET(sc->sc_rx_flags, ZYNQUART_RX_TTY_BLOCKED)) {
1525 1.2.8.2 martin /*
1526 1.2.8.2 martin * We're either not using flow control, or the
1527 1.2.8.2 martin * line discipline didn't tell us to block for
1528 1.2.8.2 martin * some reason. Either way, we have no way to
1529 1.2.8.2 martin * know when there's more space available, so
1530 1.2.8.2 martin * just drop the rest of the data.
1531 1.2.8.2 martin */
1532 1.2.8.2 martin sc->sc_rbuf_out = sc->sc_rbuf_in;
1533 1.2.8.2 martin cc = 0;
1534 1.2.8.2 martin } else {
1535 1.2.8.2 martin /*
1536 1.2.8.2 martin * Don't schedule any more receive processing
1537 1.2.8.2 martin * until the line discipline tells us there's
1538 1.2.8.2 martin * space available (through zynquarthwiflow()).
1539 1.2.8.2 martin * Leave the rest of the data in the input
1540 1.2.8.2 martin * buffer.
1541 1.2.8.2 martin */
1542 1.2.8.2 martin SET(sc->sc_rx_flags, ZYNQUART_RX_TTY_OVERFLOWED);
1543 1.2.8.2 martin }
1544 1.2.8.2 martin break;
1545 1.2.8.2 martin }
1546 1.2.8.2 martin outp = ZYNQUART_RBUF_INC(sc, outp, 1);
1547 1.2.8.2 martin cc--;
1548 1.2.8.2 martin }
1549 1.2.8.2 martin
1550 1.2.8.2 martin if (cc != scc) {
1551 1.2.8.2 martin sc->sc_rbuf_out = outp;
1552 1.2.8.2 martin mutex_spin_enter(&sc->sc_lock);
1553 1.2.8.2 martin
1554 1.2.8.2 martin cc = ZYNQUART_RBUF_SPACE(sc);
1555 1.2.8.2 martin
1556 1.2.8.2 martin /* Buffers should be ok again, release possible block. */
1557 1.2.8.2 martin if (cc >= sc->sc_r_lowat) {
1558 1.2.8.2 martin if (ISSET(sc->sc_rx_flags, ZYNQUART_RX_IBUF_OVERFLOWED)) {
1559 1.2.8.2 martin CLR(sc->sc_rx_flags, ZYNQUART_RX_IBUF_OVERFLOWED);
1560 1.2.8.2 martin zynquart_control_rxint(sc, true);
1561 1.2.8.2 martin }
1562 1.2.8.2 martin if (ISSET(sc->sc_rx_flags, ZYNQUART_RX_IBUF_BLOCKED)) {
1563 1.2.8.2 martin CLR(sc->sc_rx_flags, ZYNQUART_RX_IBUF_BLOCKED);
1564 1.2.8.2 martin zynquart_hwiflow(sc);
1565 1.2.8.2 martin }
1566 1.2.8.2 martin }
1567 1.2.8.2 martin mutex_spin_exit(&sc->sc_lock);
1568 1.2.8.2 martin }
1569 1.2.8.2 martin }
1570 1.2.8.2 martin
1571 1.2.8.2 martin integrate void
1572 1.2.8.2 martin zynquart_txsoft(struct zynquart_softc *sc, struct tty *tp)
1573 1.2.8.2 martin {
1574 1.2.8.2 martin
1575 1.2.8.2 martin CLR(tp->t_state, TS_BUSY);
1576 1.2.8.2 martin if (ISSET(tp->t_state, TS_FLUSH))
1577 1.2.8.2 martin CLR(tp->t_state, TS_FLUSH);
1578 1.2.8.2 martin else
1579 1.2.8.2 martin ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1580 1.2.8.2 martin (*tp->t_linesw->l_start)(tp);
1581 1.2.8.2 martin }
1582 1.2.8.2 martin
1583 1.2.8.2 martin integrate void
1584 1.2.8.2 martin zynquart_stsoft(struct zynquart_softc *sc, struct tty *tp)
1585 1.2.8.2 martin {
1586 1.2.8.2 martin #ifdef notyet
1587 1.2.8.2 martin u_char msr, delta;
1588 1.2.8.2 martin
1589 1.2.8.2 martin mutex_spin_enter(&sc->sc_lock);
1590 1.2.8.2 martin msr = sc->sc_msr;
1591 1.2.8.2 martin delta = sc->sc_msr_delta;
1592 1.2.8.2 martin sc->sc_msr_delta = 0;
1593 1.2.8.2 martin mutex_spin_exit(&sc->sc_lock);
1594 1.2.8.2 martin
1595 1.2.8.2 martin if (ISSET(delta, sc->sc_msr_dcd)) {
1596 1.2.8.2 martin /*
1597 1.2.8.2 martin * Inform the tty layer that carrier detect changed.
1598 1.2.8.2 martin */
1599 1.2.8.2 martin (void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD));
1600 1.2.8.2 martin }
1601 1.2.8.2 martin
1602 1.2.8.2 martin if (ISSET(delta, sc->sc_msr_cts)) {
1603 1.2.8.2 martin /* Block or unblock output according to flow control. */
1604 1.2.8.2 martin if (ISSET(msr, sc->sc_msr_cts)) {
1605 1.2.8.2 martin sc->sc_tx_stopped = 0;
1606 1.2.8.2 martin (*tp->t_linesw->l_start)(tp);
1607 1.2.8.2 martin } else {
1608 1.2.8.2 martin sc->sc_tx_stopped = 1;
1609 1.2.8.2 martin }
1610 1.2.8.2 martin }
1611 1.2.8.2 martin
1612 1.2.8.2 martin #endif
1613 1.2.8.2 martin #ifdef ZYNQUART_DEBUG
1614 1.2.8.2 martin if (zynquart_debug)
1615 1.2.8.2 martin zynquartstatus(sc, "zynquart_stsoft");
1616 1.2.8.2 martin #endif
1617 1.2.8.2 martin }
1618 1.2.8.2 martin
1619 1.2.8.2 martin void
1620 1.2.8.2 martin zynquartsoft(void *arg)
1621 1.2.8.2 martin {
1622 1.2.8.2 martin struct zynquart_softc *sc = arg;
1623 1.2.8.2 martin struct tty *tp;
1624 1.2.8.2 martin
1625 1.2.8.2 martin if (ZYNQUART_ISALIVE(sc) == 0)
1626 1.2.8.2 martin return;
1627 1.2.8.2 martin
1628 1.2.8.2 martin tp = sc->sc_tty;
1629 1.2.8.2 martin
1630 1.2.8.2 martin if (sc->sc_rx_ready) {
1631 1.2.8.2 martin sc->sc_rx_ready = 0;
1632 1.2.8.2 martin zynquart_rxsoft(sc, tp);
1633 1.2.8.2 martin }
1634 1.2.8.2 martin
1635 1.2.8.2 martin if (sc->sc_st_check) {
1636 1.2.8.2 martin sc->sc_st_check = 0;
1637 1.2.8.2 martin zynquart_stsoft(sc, tp);
1638 1.2.8.2 martin }
1639 1.2.8.2 martin
1640 1.2.8.2 martin if (sc->sc_tx_done) {
1641 1.2.8.2 martin sc->sc_tx_done = 0;
1642 1.2.8.2 martin zynquart_txsoft(sc, tp);
1643 1.2.8.2 martin }
1644 1.2.8.2 martin }
1645 1.2.8.2 martin
1646 1.2.8.2 martin int
1647 1.2.8.2 martin zynquartintr(void *arg)
1648 1.2.8.2 martin {
1649 1.2.8.2 martin struct zynquart_softc *sc = arg;
1650 1.2.8.2 martin uint32_t sts;
1651 1.2.8.2 martin uint32_t int_sts;
1652 1.2.8.2 martin bus_space_tag_t iot = sc->sc_regs.ur_iot;
1653 1.2.8.2 martin bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1654 1.2.8.2 martin
1655 1.2.8.2 martin if (ZYNQUART_ISALIVE(sc) == 0)
1656 1.2.8.2 martin return (0);
1657 1.2.8.2 martin
1658 1.2.8.2 martin mutex_spin_enter(&sc->sc_lock);
1659 1.2.8.2 martin
1660 1.2.8.2 martin int_sts = bus_space_read_4(iot, ioh, UART_CHNL_INT_STS);
1661 1.2.8.2 martin do {
1662 1.2.8.2 martin sts = bus_space_read_4(iot, ioh, UART_CHANNEL_STS);
1663 1.2.8.2 martin if (!(sts & STS_REMPTY))
1664 1.2.8.2 martin zynquartintr_read(sc);
1665 1.2.8.2 martin } while (!(sts & STS_REMPTY));
1666 1.2.8.2 martin
1667 1.2.8.2 martin if (sts & STS_TEMPTY)
1668 1.2.8.2 martin zynquartintr_send(sc);
1669 1.2.8.2 martin
1670 1.2.8.2 martin bus_space_write_4(iot, ioh, UART_CHNL_INT_STS, int_sts);
1671 1.2.8.2 martin
1672 1.2.8.2 martin mutex_spin_exit(&sc->sc_lock);
1673 1.2.8.2 martin
1674 1.2.8.2 martin /* Wake up the poller. */
1675 1.2.8.2 martin softint_schedule(sc->sc_si);
1676 1.2.8.2 martin
1677 1.2.8.2 martin #ifdef RND_COM
1678 1.2.8.2 martin rnd_add_uint32(&sc->rnd_source, iir | lsr);
1679 1.2.8.2 martin #endif
1680 1.2.8.2 martin
1681 1.2.8.2 martin return (1);
1682 1.2.8.2 martin }
1683 1.2.8.2 martin
1684 1.2.8.2 martin
1685 1.2.8.2 martin /*
1686 1.2.8.2 martin * called when there is least one character in rxfifo
1687 1.2.8.2 martin *
1688 1.2.8.2 martin */
1689 1.2.8.2 martin
1690 1.2.8.2 martin static void
1691 1.2.8.2 martin zynquartintr_read(struct zynquart_softc *sc)
1692 1.2.8.2 martin {
1693 1.2.8.2 martin int cc;
1694 1.2.8.2 martin uint16_t rd;
1695 1.2.8.2 martin uint32_t sts;
1696 1.2.8.2 martin bus_space_tag_t iot = sc->sc_regs.ur_iot;
1697 1.2.8.2 martin bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1698 1.2.8.2 martin
1699 1.2.8.2 martin cc = ZYNQUART_RBUF_SPACE(sc);
1700 1.2.8.2 martin
1701 1.2.8.2 martin /* clear aging timer interrupt */
1702 1.2.8.2 martin bus_space_write_4(iot, ioh, UART_CHNL_INT_STS, INT_TIMEOUT);
1703 1.2.8.2 martin
1704 1.2.8.2 martin while (cc > 0) {
1705 1.2.8.2 martin int cn_trapped = 0;
1706 1.2.8.2 martin
1707 1.2.8.2 martin sc->sc_rbuf[sc->sc_rbuf_in] = rd =
1708 1.2.8.2 martin bus_space_read_4(iot, ioh, UART_TX_RX_FIFO);
1709 1.2.8.2 martin
1710 1.2.8.2 martin cn_check_magic(sc->sc_tty->t_dev,
1711 1.2.8.2 martin rd & 0xff, zynquart_cnm_state);
1712 1.2.8.2 martin
1713 1.2.8.2 martin if (!cn_trapped) {
1714 1.2.8.2 martin sc->sc_rbuf_in = ZYNQUART_RBUF_INC(sc, sc->sc_rbuf_in, 1);
1715 1.2.8.2 martin cc--;
1716 1.2.8.2 martin }
1717 1.2.8.2 martin
1718 1.2.8.2 martin sts = bus_space_read_4(iot, ioh, UART_CHANNEL_STS);
1719 1.2.8.2 martin if (sts & STS_REMPTY)
1720 1.2.8.2 martin break;
1721 1.2.8.2 martin }
1722 1.2.8.2 martin
1723 1.2.8.2 martin /*
1724 1.2.8.2 martin * Current string of incoming characters ended because
1725 1.2.8.2 martin * no more data was available or we ran out of space.
1726 1.2.8.2 martin * Schedule a receive event if any data was received.
1727 1.2.8.2 martin * If we're out of space, turn off receive interrupts.
1728 1.2.8.2 martin */
1729 1.2.8.2 martin if (!ISSET(sc->sc_rx_flags, ZYNQUART_RX_TTY_OVERFLOWED))
1730 1.2.8.2 martin sc->sc_rx_ready = 1;
1731 1.2.8.2 martin /*
1732 1.2.8.2 martin * See if we are in danger of overflowing a buffer. If
1733 1.2.8.2 martin * so, use hardware flow control to ease the pressure.
1734 1.2.8.2 martin */
1735 1.2.8.2 martin if (!ISSET(sc->sc_rx_flags, ZYNQUART_RX_IBUF_BLOCKED) &&
1736 1.2.8.2 martin cc < sc->sc_r_hiwat) {
1737 1.2.8.2 martin sc->sc_rx_flags |= ZYNQUART_RX_IBUF_BLOCKED;
1738 1.2.8.2 martin zynquart_hwiflow(sc);
1739 1.2.8.2 martin }
1740 1.2.8.2 martin
1741 1.2.8.2 martin /*
1742 1.2.8.2 martin * If we're out of space, disable receive interrupts
1743 1.2.8.2 martin * until the queue has drained a bit.
1744 1.2.8.2 martin */
1745 1.2.8.2 martin if (!cc) {
1746 1.2.8.2 martin sc->sc_rx_flags |= ZYNQUART_RX_IBUF_OVERFLOWED;
1747 1.2.8.2 martin zynquart_control_rxint(sc, false);
1748 1.2.8.2 martin }
1749 1.2.8.2 martin }
1750 1.2.8.2 martin
1751 1.2.8.2 martin void
1752 1.2.8.2 martin zynquartintr_send(struct zynquart_softc *sc)
1753 1.2.8.2 martin {
1754 1.2.8.2 martin uint32_t sts;
1755 1.2.8.2 martin bus_space_tag_t iot = sc->sc_regs.ur_iot;
1756 1.2.8.2 martin bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1757 1.2.8.2 martin
1758 1.2.8.2 martin sts = bus_space_read_4(iot, ioh, UART_CHANNEL_STS);
1759 1.2.8.2 martin
1760 1.2.8.2 martin if (sc->sc_pending) {
1761 1.2.8.2 martin if (sts & STS_TEMPTY) {
1762 1.2.8.2 martin zynquart_load_pendings(sc);
1763 1.2.8.2 martin sc->sc_tbc = sc->sc_heldtbc;
1764 1.2.8.2 martin sc->sc_heldtbc = 0;
1765 1.2.8.2 martin } else {
1766 1.2.8.2 martin /* wait for TX fifo empty */
1767 1.2.8.2 martin zynquart_control_txint(sc, true);
1768 1.2.8.2 martin return;
1769 1.2.8.2 martin }
1770 1.2.8.2 martin }
1771 1.2.8.2 martin
1772 1.2.8.2 martin while (sc->sc_tbc > 0 &&
1773 1.2.8.2 martin !(bus_space_read_4(iot, ioh, UART_CHANNEL_STS) & STS_TFUL)) {
1774 1.2.8.2 martin bus_space_write_4(iot, ioh, UART_TX_RX_FIFO, *sc->sc_tba);
1775 1.2.8.2 martin sc->sc_tbc--;
1776 1.2.8.2 martin sc->sc_tba++;
1777 1.2.8.2 martin }
1778 1.2.8.2 martin
1779 1.2.8.2 martin if (sc->sc_tbc > 0)
1780 1.2.8.2 martin zynquart_control_txint(sc, true);
1781 1.2.8.2 martin else {
1782 1.2.8.2 martin /* no more chars to send.
1783 1.2.8.2 martin we don't need tx interrupt any more. */
1784 1.2.8.2 martin zynquart_control_txint(sc, false);
1785 1.2.8.2 martin if (sc->sc_tx_busy) {
1786 1.2.8.2 martin sc->sc_tx_busy = 0;
1787 1.2.8.2 martin sc->sc_tx_done = 1;
1788 1.2.8.2 martin }
1789 1.2.8.2 martin }
1790 1.2.8.2 martin }
1791 1.2.8.2 martin
1792 1.2.8.2 martin static void
1793 1.2.8.2 martin zynquart_disable_all_interrupts(struct zynquart_softc *sc)
1794 1.2.8.2 martin {
1795 1.2.8.2 martin bus_space_tag_t iot = sc->sc_regs.ur_iot;
1796 1.2.8.2 martin bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1797 1.2.8.2 martin
1798 1.2.8.2 martin bus_space_write_4(iot, ioh, UART_INTRPT_DIS, 0xffffffff);
1799 1.2.8.2 martin }
1800 1.2.8.2 martin
1801 1.2.8.2 martin static void
1802 1.2.8.2 martin zynquart_control_rxint(struct zynquart_softc *sc, bool enable)
1803 1.2.8.2 martin {
1804 1.2.8.2 martin bus_space_tag_t iot = sc->sc_regs.ur_iot;
1805 1.2.8.2 martin bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1806 1.2.8.2 martin uint32_t mask = INT_TIMEOUT | INT_PARE | INT_FRAME | INT_ROVR | INT_RFUL | INT_RTRIG;
1807 1.2.8.2 martin uint32_t sts;
1808 1.2.8.2 martin
1809 1.2.8.2 martin /* clear */
1810 1.2.8.2 martin sts = bus_space_read_4(iot, ioh, UART_CHNL_INT_STS);
1811 1.2.8.2 martin bus_space_write_4(iot, ioh, UART_CHNL_INT_STS, sts);
1812 1.2.8.2 martin
1813 1.2.8.2 martin if (enable)
1814 1.2.8.2 martin bus_space_write_4(iot, ioh, UART_INTRPT_EN, mask);
1815 1.2.8.2 martin else
1816 1.2.8.2 martin bus_space_write_4(iot, ioh, UART_INTRPT_DIS, mask);
1817 1.2.8.2 martin }
1818 1.2.8.2 martin
1819 1.2.8.2 martin static void
1820 1.2.8.2 martin zynquart_control_txint(struct zynquart_softc *sc, bool enable)
1821 1.2.8.2 martin {
1822 1.2.8.2 martin bus_space_tag_t iot = sc->sc_regs.ur_iot;
1823 1.2.8.2 martin bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1824 1.2.8.2 martin uint32_t mask = INT_TEMPTY;
1825 1.2.8.2 martin
1826 1.2.8.2 martin if (enable)
1827 1.2.8.2 martin bus_space_write_4(iot, ioh, UART_INTRPT_EN, mask);
1828 1.2.8.2 martin else
1829 1.2.8.2 martin bus_space_write_4(iot, ioh, UART_INTRPT_DIS, mask);
1830 1.2.8.2 martin }
1831 1.2.8.2 martin
1832 1.2.8.2 martin
1833 1.2.8.2 martin static void
1834 1.2.8.2 martin zynquart_load_params(struct zynquart_softc *sc)
1835 1.2.8.2 martin {
1836 1.2.8.2 martin bus_space_tag_t iot = sc->sc_regs.ur_iot;
1837 1.2.8.2 martin bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1838 1.2.8.2 martin
1839 1.2.8.2 martin bus_space_write_4(iot, ioh, UART_MODE, sc->sc_mcr);
1840 1.2.8.2 martin }
1841 1.2.8.2 martin
1842 1.2.8.2 martin static void
1843 1.2.8.2 martin zynquart_load_speed(struct zynquart_softc *sc)
1844 1.2.8.2 martin {
1845 1.2.8.2 martin /* bus_space_tag_t iot = sc->sc_regs.ur_iot; */
1846 1.2.8.2 martin /* bus_space_handle_t ioh = sc->sc_regs.ur_ioh; */
1847 1.2.8.2 martin
1848 1.2.8.2 martin /* XXX */
1849 1.2.8.2 martin }
1850 1.2.8.2 martin
1851 1.2.8.2 martin
1852 1.2.8.2 martin static void
1853 1.2.8.2 martin zynquart_load_pendings(struct zynquart_softc *sc)
1854 1.2.8.2 martin {
1855 1.2.8.2 martin if (sc->sc_pending & ZYNQUART_PEND_PARAM)
1856 1.2.8.2 martin zynquart_load_params(sc);
1857 1.2.8.2 martin if (sc->sc_pending & ZYNQUART_PEND_SPEED)
1858 1.2.8.2 martin zynquart_load_speed(sc);
1859 1.2.8.2 martin sc->sc_pending = 0;
1860 1.2.8.2 martin }
1861 1.2.8.2 martin
1862 1.2.8.2 martin /*
1863 1.2.8.2 martin * The following functions are polled getc and putc routines, shared
1864 1.2.8.2 martin * by the console and kgdb glue.
1865 1.2.8.2 martin *
1866 1.2.8.2 martin * The read-ahead code is so that you can detect pending in-band
1867 1.2.8.2 martin * cn_magic in polled mode while doing output rather than having to
1868 1.2.8.2 martin * wait until the kernel decides it needs input.
1869 1.2.8.2 martin */
1870 1.2.8.2 martin
1871 1.2.8.2 martin #define READAHEAD_RING_LEN 16
1872 1.2.8.2 martin static int zynquart_readahead[READAHEAD_RING_LEN];
1873 1.2.8.2 martin static int zynquart_readahead_in = 0;
1874 1.2.8.2 martin static int zynquart_readahead_out = 0;
1875 1.2.8.2 martin #define READAHEAD_IS_EMPTY() (zynquart_readahead_in==zynquart_readahead_out)
1876 1.2.8.2 martin #define READAHEAD_IS_FULL() \
1877 1.2.8.2 martin (((zynquart_readahead_in+1) & (READAHEAD_RING_LEN-1)) ==zynquart_readahead_out)
1878 1.2.8.2 martin
1879 1.2.8.2 martin int
1880 1.2.8.2 martin zynquart_common_getc(dev_t dev, struct zynquart_regs *regsp)
1881 1.2.8.2 martin {
1882 1.2.8.2 martin int s = splserial();
1883 1.2.8.2 martin u_char c;
1884 1.2.8.2 martin bus_space_tag_t iot = regsp->ur_iot;
1885 1.2.8.2 martin bus_space_handle_t ioh = regsp->ur_ioh;
1886 1.2.8.2 martin uint32_t sts;
1887 1.2.8.2 martin
1888 1.2.8.2 martin /* got a character from reading things earlier */
1889 1.2.8.2 martin if (zynquart_readahead_in != zynquart_readahead_out) {
1890 1.2.8.2 martin
1891 1.2.8.2 martin c = zynquart_readahead[zynquart_readahead_out];
1892 1.2.8.2 martin zynquart_readahead_out = (zynquart_readahead_out + 1) &
1893 1.2.8.2 martin (READAHEAD_RING_LEN-1);
1894 1.2.8.2 martin splx(s);
1895 1.2.8.2 martin return (c);
1896 1.2.8.2 martin }
1897 1.2.8.2 martin
1898 1.2.8.2 martin /* block until a character becomes available */
1899 1.2.8.2 martin while ((sts = bus_space_read_4(iot, ioh, UART_CHANNEL_STS)) & STS_REMPTY)
1900 1.2.8.2 martin ;
1901 1.2.8.2 martin
1902 1.2.8.2 martin c = 0xff & bus_space_read_4(iot, ioh, UART_TX_RX_FIFO);
1903 1.2.8.2 martin
1904 1.2.8.2 martin {
1905 1.2.8.2 martin int __attribute__((__unused__))cn_trapped = 0; /* unused */
1906 1.2.8.2 martin #ifdef DDB
1907 1.2.8.2 martin extern int db_active;
1908 1.2.8.2 martin if (!db_active)
1909 1.2.8.2 martin #endif
1910 1.2.8.2 martin cn_check_magic(dev, c, zynquart_cnm_state);
1911 1.2.8.2 martin }
1912 1.2.8.2 martin splx(s);
1913 1.2.8.2 martin return (c);
1914 1.2.8.2 martin }
1915 1.2.8.2 martin
1916 1.2.8.2 martin void
1917 1.2.8.2 martin zynquart_common_putc(dev_t dev, struct zynquart_regs *regsp, int c)
1918 1.2.8.2 martin {
1919 1.2.8.2 martin int s = splserial();
1920 1.2.8.2 martin int cin, timo;
1921 1.2.8.2 martin bus_space_tag_t iot = regsp->ur_iot;
1922 1.2.8.2 martin bus_space_handle_t ioh = regsp->ur_ioh;
1923 1.2.8.2 martin
1924 1.2.8.2 martin if (!READAHEAD_IS_FULL() &&
1925 1.2.8.2 martin !(bus_space_read_4(iot, ioh, UART_CHANNEL_STS) & STS_REMPTY)) {
1926 1.2.8.2 martin
1927 1.2.8.2 martin int __attribute__((__unused__))cn_trapped = 0;
1928 1.2.8.2 martin cin = bus_space_read_4(iot, ioh, UART_TX_RX_FIFO);
1929 1.2.8.2 martin cn_check_magic(dev, cin & 0xff, zynquart_cnm_state);
1930 1.2.8.2 martin zynquart_readahead_in = (zynquart_readahead_in + 1) &
1931 1.2.8.2 martin (READAHEAD_RING_LEN-1);
1932 1.2.8.2 martin }
1933 1.2.8.2 martin
1934 1.2.8.2 martin /* wait for any pending transmission to finish */
1935 1.2.8.2 martin timo = 150000;
1936 1.2.8.2 martin do {
1937 1.2.8.2 martin if (!(bus_space_read_4(iot, ioh, UART_CHANNEL_STS) & STS_TFUL)) {
1938 1.2.8.2 martin bus_space_write_4(iot, ioh, UART_TX_RX_FIFO, c);
1939 1.2.8.2 martin break;
1940 1.2.8.2 martin }
1941 1.2.8.2 martin } while(--timo > 0);
1942 1.2.8.2 martin
1943 1.2.8.2 martin ZYNQUART_BARRIER(regsp, BR | BW);
1944 1.2.8.2 martin
1945 1.2.8.2 martin splx(s);
1946 1.2.8.2 martin }
1947 1.2.8.2 martin
1948 1.2.8.2 martin /*
1949 1.2.8.2 martin * Initialize UART for use as console or KGDB line.
1950 1.2.8.2 martin */
1951 1.2.8.2 martin int
1952 1.2.8.2 martin zynquart_init(struct zynquart_regs *regsp, int rate, tcflag_t cflag)
1953 1.2.8.2 martin {
1954 1.2.8.2 martin struct zynquart_baudrate_ratio ratio;
1955 1.2.8.2 martin
1956 1.2.8.2 martin if (bus_space_map(regsp->ur_iot, regsp->ur_iobase, UART_SIZE, 0,
1957 1.2.8.2 martin ®sp->ur_ioh))
1958 1.2.8.2 martin return ENOMEM; /* ??? */
1959 1.2.8.2 martin
1960 1.2.8.2 martin if (zynquartspeed(rate, &ratio) < 0)
1961 1.2.8.2 martin return EINVAL;
1962 1.2.8.2 martin
1963 1.2.8.2 martin /* clear status registers */
1964 1.2.8.2 martin bus_space_write_4(regsp->ur_iot, regsp->ur_ioh, UART_CHNL_INT_STS, 0xffff);
1965 1.2.8.2 martin bus_space_write_4(regsp->ur_iot, regsp->ur_ioh, UART_CHANNEL_STS, 0xffff);
1966 1.2.8.2 martin
1967 1.2.8.2 martin return (0);
1968 1.2.8.2 martin }
1969 1.2.8.2 martin
1970 1.2.8.2 martin
1971 1.2.8.2 martin
1972 1.2.8.2 martin /*
1973 1.2.8.2 martin * Following are all routines needed for UART to act as console
1974 1.2.8.2 martin */
1975 1.2.8.2 martin struct consdev zynquartcons = {
1976 1.2.8.2 martin .cn_getc = zynquartcngetc,
1977 1.2.8.2 martin .cn_putc = zynquartcnputc,
1978 1.2.8.2 martin .cn_pollc = nullcnpollc
1979 1.2.8.2 martin };
1980 1.2.8.2 martin
1981 1.2.8.2 martin
1982 1.2.8.2 martin int
1983 1.2.8.2 martin zynquart_cons_attach(bus_space_tag_t iot, paddr_t iobase, u_int rate,
1984 1.2.8.2 martin tcflag_t cflag)
1985 1.2.8.2 martin {
1986 1.2.8.2 martin struct zynquart_regs regs;
1987 1.2.8.2 martin int res;
1988 1.2.8.2 martin
1989 1.2.8.2 martin regs.ur_iot = iot;
1990 1.2.8.2 martin regs.ur_iobase = iobase;
1991 1.2.8.2 martin
1992 1.2.8.2 martin res = zynquart_init(®s, rate, cflag);
1993 1.2.8.2 martin if (res)
1994 1.2.8.2 martin return (res);
1995 1.2.8.2 martin
1996 1.2.8.2 martin cn_tab = &zynquartcons;
1997 1.2.8.2 martin cn_init_magic(&zynquart_cnm_state);
1998 1.2.8.2 martin cn_set_magic("\047\001"); /* default magic is BREAK */
1999 1.2.8.2 martin
2000 1.2.8.2 martin zynquartconsrate = rate;
2001 1.2.8.2 martin zynquartconscflag = cflag;
2002 1.2.8.2 martin
2003 1.2.8.2 martin zynquartconsregs = regs;
2004 1.2.8.2 martin
2005 1.2.8.2 martin return 0;
2006 1.2.8.2 martin }
2007 1.2.8.2 martin
2008 1.2.8.2 martin int
2009 1.2.8.2 martin zynquartcngetc(dev_t dev)
2010 1.2.8.2 martin {
2011 1.2.8.2 martin return (zynquart_common_getc(dev, &zynquartconsregs));
2012 1.2.8.2 martin }
2013 1.2.8.2 martin
2014 1.2.8.2 martin /*
2015 1.2.8.2 martin * Console kernel output character routine.
2016 1.2.8.2 martin */
2017 1.2.8.2 martin void
2018 1.2.8.2 martin zynquartcnputc(dev_t dev, int c)
2019 1.2.8.2 martin {
2020 1.2.8.2 martin zynquart_common_putc(dev, &zynquartconsregs, c);
2021 1.2.8.2 martin }
2022 1.2.8.2 martin
2023 1.2.8.2 martin #ifdef KGDB
2024 1.2.8.2 martin int
2025 1.2.8.2 martin zynquart_kgdb_attach(bus_space_tag_t iot, paddr_t iobase, u_int rate,
2026 1.2.8.2 martin tcflag_t cflag)
2027 1.2.8.2 martin {
2028 1.2.8.2 martin int res;
2029 1.2.8.2 martin
2030 1.2.8.2 martin if (iot == zynquartconsregs.ur_iot &&
2031 1.2.8.2 martin iobase == zynquartconsregs.ur_iobase) {
2032 1.2.8.2 martin #if !defined(DDB)
2033 1.2.8.2 martin return (EBUSY); /* cannot share with console */
2034 1.2.8.2 martin #else
2035 1.2.8.2 martin zynquart_kgdb_regs.ur_iot = iot;
2036 1.2.8.2 martin zynquart_kgdb_regs.ur_ioh = zynquartconsregs.ur_ioh;
2037 1.2.8.2 martin zynquart_kgdb_regs.ur_iobase = iobase;
2038 1.2.8.2 martin #endif
2039 1.2.8.2 martin } else {
2040 1.2.8.2 martin zynquart_kgdb_regs.ur_iot = iot;
2041 1.2.8.2 martin zynquart_kgdb_regs.ur_iobase = iobase;
2042 1.2.8.2 martin
2043 1.2.8.2 martin res = zynquart_init(&zynquart_kgdb_regs, rate, cflag);
2044 1.2.8.2 martin if (res)
2045 1.2.8.2 martin return (res);
2046 1.2.8.2 martin
2047 1.2.8.2 martin /*
2048 1.2.8.2 martin * XXXfvdl this shouldn't be needed, but the cn_magic goo
2049 1.2.8.2 martin * expects this to be initialized
2050 1.2.8.2 martin */
2051 1.2.8.2 martin cn_init_magic(&zynquart_cnm_state);
2052 1.2.8.2 martin cn_set_magic("\047\001");
2053 1.2.8.2 martin }
2054 1.2.8.2 martin
2055 1.2.8.2 martin kgdb_attach(zynquart_kgdb_getc, zynquart_kgdb_putc, &zynquart_kgdb_regs);
2056 1.2.8.2 martin kgdb_dev = 123; /* unneeded, only to satisfy some tests */
2057 1.2.8.2 martin
2058 1.2.8.2 martin return (0);
2059 1.2.8.2 martin }
2060 1.2.8.2 martin
2061 1.2.8.2 martin /* ARGSUSED */
2062 1.2.8.2 martin int
2063 1.2.8.2 martin zynquart_kgdb_getc(void *arg)
2064 1.2.8.2 martin {
2065 1.2.8.2 martin struct zynquart_regs *regs = arg;
2066 1.2.8.2 martin
2067 1.2.8.2 martin return (zynquart_common_getc(NODEV, regs));
2068 1.2.8.2 martin }
2069 1.2.8.2 martin
2070 1.2.8.2 martin /* ARGSUSED */
2071 1.2.8.2 martin void
2072 1.2.8.2 martin zynquart_kgdb_putc(void *arg, int c)
2073 1.2.8.2 martin {
2074 1.2.8.2 martin struct zynquart_regs *regs = arg;
2075 1.2.8.2 martin
2076 1.2.8.2 martin zynquart_common_putc(NODEV, regs, c);
2077 1.2.8.2 martin }
2078 1.2.8.2 martin #endif /* KGDB */
2079 1.2.8.2 martin
2080 1.2.8.2 martin /* helper function to identify the zynquart ports used by
2081 1.2.8.2 martin console or KGDB (and not yet autoconf attached) */
2082 1.2.8.2 martin int
2083 1.2.8.2 martin zynquart_is_console(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t *ioh)
2084 1.2.8.2 martin {
2085 1.2.8.2 martin bus_space_handle_t help;
2086 1.2.8.2 martin
2087 1.2.8.2 martin if (!zynquartconsattached &&
2088 1.2.8.2 martin iot == zynquartconsregs.ur_iot && iobase == zynquartconsregs.ur_iobase)
2089 1.2.8.2 martin help = zynquartconsregs.ur_ioh;
2090 1.2.8.2 martin #ifdef KGDB
2091 1.2.8.2 martin else if (!zynquart_kgdb_attached &&
2092 1.2.8.2 martin iot == zynquart_kgdb_regs.ur_iot && iobase == zynquart_kgdb_regs.ur_iobase)
2093 1.2.8.2 martin help = zynquart_kgdb_regs.ur_ioh;
2094 1.2.8.2 martin #endif
2095 1.2.8.2 martin else
2096 1.2.8.2 martin return (0);
2097 1.2.8.2 martin
2098 1.2.8.2 martin if (ioh)
2099 1.2.8.2 martin *ioh = help;
2100 1.2.8.2 martin return (1);
2101 1.2.8.2 martin }
2102 1.2.8.2 martin
2103 1.2.8.2 martin #ifdef notyet
2104 1.2.8.2 martin
2105 1.2.8.2 martin bool
2106 1.2.8.2 martin zynquart_cleanup(device_t self, int how)
2107 1.2.8.2 martin {
2108 1.2.8.2 martin /*
2109 1.2.8.2 martin * this routine exists to serve as a shutdown hook for systems that
2110 1.2.8.2 martin * have firmware which doesn't interact properly with a zynquart device in
2111 1.2.8.2 martin * FIFO mode.
2112 1.2.8.2 martin */
2113 1.2.8.2 martin struct zynquart_softc *sc = device_private(self);
2114 1.2.8.2 martin
2115 1.2.8.2 martin if (ISSET(sc->sc_hwflags, ZYNQUART_HW_FIFO))
2116 1.2.8.2 martin UR_WRITE_1(&sc->sc_regs, ZYNQUART_REG_FIFO, 0);
2117 1.2.8.2 martin
2118 1.2.8.2 martin return true;
2119 1.2.8.2 martin }
2120 1.2.8.2 martin #endif
2121 1.2.8.2 martin
2122 1.2.8.2 martin #ifdef notyet
2123 1.2.8.2 martin bool
2124 1.2.8.2 martin zynquart_suspend(device_t self PMF_FN_ARGS)
2125 1.2.8.2 martin {
2126 1.2.8.2 martin struct zynquart_softc *sc = device_private(self);
2127 1.2.8.2 martin
2128 1.2.8.2 martin UR_WRITE_1(&sc->sc_regs, ZYNQUART_REG_IER, 0);
2129 1.2.8.2 martin (void)CSR_READ_1(&sc->sc_regs, ZYNQUART_REG_IIR);
2130 1.2.8.2 martin
2131 1.2.8.2 martin return true;
2132 1.2.8.2 martin }
2133 1.2.8.2 martin #endif
2134 1.2.8.2 martin
2135 1.2.8.2 martin #ifdef notyet
2136 1.2.8.2 martin bool
2137 1.2.8.2 martin zynquart_resume(device_t self PMF_FN_ARGS)
2138 1.2.8.2 martin {
2139 1.2.8.2 martin struct zynquart_softc *sc = device_private(self);
2140 1.2.8.2 martin
2141 1.2.8.2 martin mutex_spin_enter(&sc->sc_lock);
2142 1.2.8.2 martin zynquart_loadchannelregs(sc);
2143 1.2.8.2 martin mutex_spin_exit(&sc->sc_lock);
2144 1.2.8.2 martin
2145 1.2.8.2 martin return true;
2146 1.2.8.2 martin }
2147 1.2.8.2 martin #endif
2148 1.2.8.2 martin
2149 1.2.8.2 martin static void
2150 1.2.8.2 martin zynquart_enable_debugport(struct zynquart_softc *sc)
2151 1.2.8.2 martin {
2152 1.2.8.2 martin /* bus_space_tag_t iot = sc->sc_regs.ur_iot; */
2153 1.2.8.2 martin /* bus_space_handle_t ioh = sc->sc_regs.ur_ioh; */
2154 1.2.8.2 martin }
2155 1.2.8.2 martin
2156 1.2.8.2 martin
2157 1.2.8.2 martin void
2158 1.2.8.2 martin zynquart_set_frequency(u_int freq, u_int div)
2159 1.2.8.2 martin {
2160 1.2.8.2 martin zynquart_freq = freq;
2161 1.2.8.2 martin zynquart_freqdiv = div;
2162 1.2.8.2 martin }
2163