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