clpscom.c revision 1.9 1 1.9 thorpej /* $NetBSD: clpscom.c,v 1.9 2020/11/20 18:03:52 thorpej Exp $ */
2 1.1 kiyohara /*
3 1.1 kiyohara * Copyright (c) 2013 KIYOHARA Takashi
4 1.1 kiyohara * All rights reserved.
5 1.1 kiyohara *
6 1.1 kiyohara * Redistribution and use in source and binary forms, with or without
7 1.1 kiyohara * modification, are permitted provided that the following conditions
8 1.1 kiyohara * are met:
9 1.1 kiyohara * 1. Redistributions of source code must retain the above copyright
10 1.1 kiyohara * notice, this list of conditions and the following disclaimer.
11 1.1 kiyohara * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 kiyohara * notice, this list of conditions and the following disclaimer in the
13 1.1 kiyohara * documentation and/or other materials provided with the distribution.
14 1.1 kiyohara *
15 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 kiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 kiyohara * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 kiyohara * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 1.1 kiyohara * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 1.1 kiyohara * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 kiyohara * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 kiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 1.1 kiyohara * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 1.1 kiyohara * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 kiyohara * POSSIBILITY OF SUCH DAMAGE.
26 1.1 kiyohara */
27 1.1 kiyohara #include <sys/cdefs.h>
28 1.9 thorpej __KERNEL_RCSID(0, "$NetBSD: clpscom.c,v 1.9 2020/11/20 18:03:52 thorpej Exp $");
29 1.1 kiyohara
30 1.1 kiyohara #include "rnd.h"
31 1.1 kiyohara
32 1.1 kiyohara #include <sys/param.h>
33 1.1 kiyohara #include <sys/bus.h>
34 1.1 kiyohara #include <sys/conf.h>
35 1.1 kiyohara #include <sys/device.h>
36 1.1 kiyohara #include <sys/errno.h>
37 1.1 kiyohara #include <sys/fcntl.h>
38 1.1 kiyohara #include <sys/intr.h>
39 1.1 kiyohara #include <sys/kauth.h>
40 1.1 kiyohara #include <sys/lwp.h>
41 1.9 thorpej #include <sys/kmem.h>
42 1.1 kiyohara #include <sys/systm.h>
43 1.1 kiyohara #include <sys/termios.h>
44 1.1 kiyohara #include <sys/tty.h>
45 1.1 kiyohara #include <sys/types.h>
46 1.1 kiyohara
47 1.1 kiyohara #include <arm/clps711x/clps711xreg.h>
48 1.1 kiyohara #include <arm/clps711x/clpssocvar.h>
49 1.1 kiyohara
50 1.1 kiyohara #include <dev/cons.h>
51 1.1 kiyohara
52 1.1 kiyohara #ifdef RND_COM
53 1.6 riastrad #include <sys/rndsource.h>
54 1.1 kiyohara #endif
55 1.1 kiyohara
56 1.1 kiyohara #include "ioconf.h"
57 1.1 kiyohara #include "locators.h"
58 1.1 kiyohara
59 1.5 christos #define COMUNIT(x) TTUNIT(x)
60 1.5 christos #define COMDIALOUT(x) TTDIALOUT(x)
61 1.1 kiyohara
62 1.1 kiyohara #define CLPSCOM_RING_SIZE 2048
63 1.1 kiyohara #define UART_FIFO_SIZE 16
64 1.1 kiyohara
65 1.1 kiyohara #define CLPSCOM_READ_CON(sc) \
66 1.1 kiyohara bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, PS711X_SYSCON)
67 1.1 kiyohara #define CLPSCOM_WRITE_CON(sc, val) \
68 1.1 kiyohara bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, PS711X_SYSCON, (val))
69 1.1 kiyohara #define CLPSCOM_READ_FLG(sc) \
70 1.1 kiyohara bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, PS711X_SYSFLG)
71 1.1 kiyohara #define CLPSCOM_WRITE_FLG(sc, val) \
72 1.1 kiyohara bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, PS711X_SYSFLG, (val))
73 1.1 kiyohara #define CLPSCOM_READ(sc) \
74 1.1 kiyohara bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, PS711X_UARTDR)
75 1.1 kiyohara #define CLPSCOM_WRITE(sc, val) \
76 1.1 kiyohara bus_space_write_1((sc)->sc_iot, (sc)->sc_ioh, PS711X_UARTDR, (val))
77 1.1 kiyohara #define CLPSCOM_WRITE_MULTI(sc, val, n) \
78 1.1 kiyohara bus_space_write_multi_1((sc)->sc_iot, (sc)->sc_ioh, PS711X_UARTDR, (val), (n))
79 1.1 kiyohara #define CLPSCOM_READ_UBRLCR(sc) \
80 1.1 kiyohara bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, PS711X_UBRLCR)
81 1.1 kiyohara #define CLPSCOM_WRITE_UBRLCR(sc, val) \
82 1.1 kiyohara bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, PS711X_UBRLCR, (val))
83 1.1 kiyohara
84 1.1 kiyohara struct clpscom_softc {
85 1.1 kiyohara device_t sc_dev;
86 1.1 kiyohara bus_space_tag_t sc_iot;
87 1.1 kiyohara bus_space_handle_t sc_ioh;
88 1.1 kiyohara int sc_irq[3];
89 1.1 kiyohara void *sc_ih[3];
90 1.1 kiyohara #define CLPSCOM_TXINT 0
91 1.1 kiyohara #define CLPSCOM_RXINT 1
92 1.1 kiyohara #define CLPSCOM_MSINT 2
93 1.1 kiyohara
94 1.1 kiyohara void *sc_si;
95 1.1 kiyohara
96 1.1 kiyohara struct tty *sc_tty;
97 1.1 kiyohara
98 1.1 kiyohara u_char *sc_tba;
99 1.1 kiyohara u_int sc_tbc;
100 1.1 kiyohara u_char *sc_rbuf;
101 1.1 kiyohara char *volatile sc_rbget;
102 1.1 kiyohara char *volatile sc_rbput;
103 1.1 kiyohara volatile int sc_rbavail;
104 1.1 kiyohara
105 1.1 kiyohara #define CLPSCOM_MODEM_STATUS_MASK (SYSFLG_DCD | SYSFLG_DSR | SYSFLG_CTS)
106 1.1 kiyohara uint32_t sc_ms;
107 1.1 kiyohara uint32_t sc_ms_dcd;
108 1.1 kiyohara uint32_t sc_ms_cts;
109 1.1 kiyohara uint32_t sc_ms_mask;
110 1.1 kiyohara uint32_t sc_ms_delta;
111 1.1 kiyohara
112 1.1 kiyohara int sc_tx_stopped;
113 1.1 kiyohara
114 1.1 kiyohara int sc_tx_done;
115 1.1 kiyohara int sc_rx_ready;
116 1.1 kiyohara int sc_ms_changed;
117 1.1 kiyohara
118 1.1 kiyohara int sc_hwflags;
119 1.1 kiyohara #define COM_HW_CONSOLE (1 << 0)
120 1.1 kiyohara #define COM_HW_DEV_OK (1 << 1)
121 1.1 kiyohara #define COM_HW_KGDB (1 << 2)
122 1.1 kiyohara int sc_swflags;
123 1.1 kiyohara
124 1.1 kiyohara #ifdef RND_COM
125 1.1 kiyohara krandsource_t rnd_source;
126 1.1 kiyohara #endif
127 1.1 kiyohara };
128 1.1 kiyohara
129 1.1 kiyohara static int clpscom_match(device_t, cfdata_t, void *);
130 1.1 kiyohara static void clpscom_attach(device_t, device_t, void *);
131 1.1 kiyohara
132 1.1 kiyohara static int clpscom_txintr(void *);
133 1.1 kiyohara static int clpscom_rxintr(void *);
134 1.1 kiyohara static int clpscom_msintr(void *);
135 1.1 kiyohara static void clpscom_soft(void *);
136 1.1 kiyohara
137 1.1 kiyohara static void clpscom_start(struct tty *);
138 1.1 kiyohara static int clpscom_param(struct tty *, struct termios *);
139 1.1 kiyohara static int clpscom_hwiflow(struct tty *, int);
140 1.1 kiyohara
141 1.1 kiyohara dev_type_open(clpscomopen);
142 1.1 kiyohara dev_type_close(clpscomclose);
143 1.1 kiyohara dev_type_read(clpscomread);
144 1.1 kiyohara dev_type_write(clpscomwrite);
145 1.1 kiyohara dev_type_ioctl(clpscomioctl);
146 1.1 kiyohara dev_type_stop(clpscomstop);
147 1.1 kiyohara dev_type_tty(clpscomtty);
148 1.1 kiyohara dev_type_poll(clpscompoll);
149 1.1 kiyohara
150 1.1 kiyohara static void clpscom_iflush(struct clpscom_softc *);
151 1.1 kiyohara static void clpscom_shutdown(struct clpscom_softc *);
152 1.1 kiyohara static void clpscom_break(struct clpscom_softc *, int);
153 1.1 kiyohara static int clpscom_to_tiocm(struct clpscom_softc *);
154 1.1 kiyohara
155 1.1 kiyohara static void clpscom_rxsoft(struct clpscom_softc *, struct tty *);
156 1.1 kiyohara static void clpscom_mssoft(struct clpscom_softc *, struct tty *);
157 1.1 kiyohara
158 1.1 kiyohara static inline uint32_t clpscom_rate2ubrlcr(int);
159 1.1 kiyohara static uint32_t clpscom_cflag2ubrlcr(tcflag_t);
160 1.1 kiyohara
161 1.1 kiyohara static int clpscom_cngetc(dev_t);
162 1.1 kiyohara static void clpscom_cnputc(dev_t, int);
163 1.1 kiyohara static void clpscom_cnpollc(dev_t, int);
164 1.1 kiyohara
165 1.1 kiyohara CFATTACH_DECL_NEW(clpscom, sizeof(struct clpscom_softc),
166 1.1 kiyohara clpscom_match, clpscom_attach, NULL, NULL);
167 1.1 kiyohara
168 1.1 kiyohara const struct cdevsw clpscom_cdevsw = {
169 1.2 dholland .d_open = clpscomopen,
170 1.2 dholland .d_close = clpscomclose,
171 1.2 dholland .d_read = clpscomread,
172 1.2 dholland .d_write = clpscomwrite,
173 1.2 dholland .d_ioctl = clpscomioctl,
174 1.2 dholland .d_stop = clpscomstop,
175 1.2 dholland .d_tty = clpscomtty,
176 1.2 dholland .d_poll = clpscompoll,
177 1.2 dholland .d_mmap = nommap,
178 1.2 dholland .d_kqfilter = ttykqfilter,
179 1.3 dholland .d_discard = nodiscard,
180 1.2 dholland .d_flag = D_TTY
181 1.1 kiyohara };
182 1.1 kiyohara
183 1.1 kiyohara static struct cnm_state clpscom_cnm_state;
184 1.1 kiyohara static vaddr_t clpscom_cnaddr = 0;
185 1.1 kiyohara static int clpscom_cnrate;
186 1.1 kiyohara static tcflag_t clpscom_cncflag;
187 1.1 kiyohara
188 1.1 kiyohara
189 1.1 kiyohara /* ARGSUSED */
190 1.1 kiyohara static int
191 1.1 kiyohara clpscom_match(device_t parent, cfdata_t match, void *aux)
192 1.1 kiyohara {
193 1.1 kiyohara
194 1.1 kiyohara return 1;
195 1.1 kiyohara }
196 1.1 kiyohara
197 1.1 kiyohara /* ARGSUSED */
198 1.1 kiyohara static void
199 1.1 kiyohara clpscom_attach(device_t parent, device_t self, void *aux)
200 1.1 kiyohara {
201 1.1 kiyohara struct clpscom_softc *sc = device_private(self);
202 1.1 kiyohara struct clpssoc_attach_args *aa = aux;
203 1.1 kiyohara int i;
204 1.1 kiyohara
205 1.1 kiyohara aprint_naive("\n");
206 1.1 kiyohara aprint_normal("\n");
207 1.1 kiyohara
208 1.1 kiyohara sc->sc_dev = self;
209 1.1 kiyohara sc->sc_iot = aa->aa_iot;
210 1.1 kiyohara sc->sc_ioh = *aa->aa_ioh;
211 1.1 kiyohara for (i = 0; i < __arraycount(aa->aa_irq); i++) {
212 1.1 kiyohara sc->sc_irq[i] = aa->aa_irq[i];
213 1.1 kiyohara sc->sc_ih[i] = NULL;
214 1.1 kiyohara }
215 1.1 kiyohara
216 1.1 kiyohara if (clpscom_cnaddr != 0)
217 1.1 kiyohara SET(sc->sc_hwflags, COM_HW_CONSOLE);
218 1.1 kiyohara
219 1.1 kiyohara sc->sc_tty = tty_alloc();
220 1.1 kiyohara sc->sc_tty->t_oproc = clpscom_start;
221 1.1 kiyohara sc->sc_tty->t_param = clpscom_param;
222 1.1 kiyohara sc->sc_tty->t_hwiflow = clpscom_hwiflow;
223 1.1 kiyohara
224 1.1 kiyohara sc->sc_tbc = 0;
225 1.9 thorpej sc->sc_rbuf = kmem_alloc(CLPSCOM_RING_SIZE << 1, KM_SLEEP);
226 1.1 kiyohara sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
227 1.1 kiyohara sc->sc_rbavail = CLPSCOM_RING_SIZE;
228 1.1 kiyohara
229 1.1 kiyohara tty_attach(sc->sc_tty);
230 1.1 kiyohara
231 1.1 kiyohara if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
232 1.1 kiyohara int maj = cdevsw_lookup_major(&clpscom_cdevsw);
233 1.1 kiyohara
234 1.1 kiyohara sc->sc_tty->t_dev = makedev(maj, device_unit(sc->sc_dev));
235 1.1 kiyohara cn_tab->cn_dev = sc->sc_tty->t_dev;
236 1.1 kiyohara
237 1.1 kiyohara aprint_normal_dev(self, "console\n");
238 1.1 kiyohara }
239 1.1 kiyohara
240 1.1 kiyohara sc->sc_si = softint_establish(SOFTINT_SERIAL, clpscom_soft, sc);
241 1.1 kiyohara
242 1.1 kiyohara #ifdef RND_COM
243 1.1 kiyohara rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
244 1.4 tls RND_TYPE_TTY, RND_FLAG_DEFAULT);
245 1.1 kiyohara #endif
246 1.1 kiyohara
247 1.1 kiyohara SET(sc->sc_hwflags, COM_HW_DEV_OK);
248 1.1 kiyohara }
249 1.1 kiyohara
250 1.1 kiyohara static int
251 1.1 kiyohara clpscom_txintr(void *arg)
252 1.1 kiyohara {
253 1.1 kiyohara struct clpscom_softc *sc = arg;
254 1.1 kiyohara uint32_t sysflg;
255 1.1 kiyohara
256 1.1 kiyohara if (!device_is_active(sc->sc_dev))
257 1.1 kiyohara return 0;
258 1.1 kiyohara
259 1.1 kiyohara sysflg = CLPSCOM_READ_FLG(sc);
260 1.1 kiyohara
261 1.1 kiyohara /*
262 1.1 kiyohara * Done handling any receive interrupts. See if data can be
263 1.1 kiyohara * transmitted as well. Schedule tx done event if no data left
264 1.1 kiyohara * and tty was marked busy.
265 1.1 kiyohara */
266 1.1 kiyohara
267 1.1 kiyohara if (!ISSET(sysflg, SYSFLG_UTXFF)) {
268 1.1 kiyohara /* Output the next chunk of the contiguous buffer, if any. */
269 1.1 kiyohara if (sc->sc_tbc > 0) {
270 1.1 kiyohara while (sc->sc_tbc > 0 && !ISSET(sysflg, SYSFLG_UTXFF)) {
271 1.1 kiyohara CLPSCOM_WRITE(sc, *sc->sc_tba);
272 1.1 kiyohara sc->sc_tba++;
273 1.1 kiyohara sc->sc_tbc--;
274 1.1 kiyohara sysflg = CLPSCOM_READ_FLG(sc);
275 1.1 kiyohara }
276 1.1 kiyohara } else if (!ISSET(sysflg, SYSFLG_UBUSY) &&
277 1.1 kiyohara sc->sc_ih[CLPSCOM_TXINT] != NULL) {
278 1.1 kiyohara intr_disestablish(sc->sc_ih[CLPSCOM_TXINT]);
279 1.1 kiyohara sc->sc_ih[CLPSCOM_TXINT] = NULL;
280 1.1 kiyohara sc->sc_tx_done = 1;
281 1.1 kiyohara }
282 1.1 kiyohara }
283 1.1 kiyohara
284 1.1 kiyohara /* Wake up the poller. */
285 1.1 kiyohara softint_schedule(sc->sc_si);
286 1.1 kiyohara
287 1.1 kiyohara return 1;
288 1.1 kiyohara }
289 1.1 kiyohara
290 1.1 kiyohara static int
291 1.1 kiyohara clpscom_rxintr(void *arg)
292 1.1 kiyohara {
293 1.1 kiyohara struct clpscom_softc *sc = arg;
294 1.1 kiyohara int cc;
295 1.1 kiyohara uint32_t sysflg;
296 1.1 kiyohara uint16_t data;
297 1.1 kiyohara u_char *put;
298 1.1 kiyohara
299 1.1 kiyohara if (!device_is_active(sc->sc_dev))
300 1.1 kiyohara return 0;
301 1.1 kiyohara
302 1.1 kiyohara if (sc->sc_ih[CLPSCOM_RXINT] != NULL) {
303 1.1 kiyohara put = sc->sc_rbput;
304 1.1 kiyohara cc = sc->sc_rbavail;
305 1.1 kiyohara while (cc > 0) {
306 1.1 kiyohara sysflg = CLPSCOM_READ_FLG(sc);
307 1.1 kiyohara if (ISSET(sysflg, SYSFLG_URXFE))
308 1.1 kiyohara break;
309 1.1 kiyohara data = CLPSCOM_READ(sc);
310 1.1 kiyohara cn_check_magic(sc->sc_tty->t_dev, data & 0xff,
311 1.1 kiyohara clpscom_cnm_state);
312 1.1 kiyohara
313 1.1 kiyohara put[0] = data & 0xff;
314 1.1 kiyohara put[1] = (data >> 8) & 0xff;
315 1.1 kiyohara put += 2;
316 1.1 kiyohara if (put >= sc->sc_rbuf + (CLPSCOM_RING_SIZE << 1))
317 1.1 kiyohara put = sc->sc_rbuf;
318 1.1 kiyohara cc--;
319 1.1 kiyohara sc->sc_rx_ready = 1;
320 1.1 kiyohara }
321 1.1 kiyohara
322 1.1 kiyohara /*
323 1.1 kiyohara * Current string of incoming characters ended because
324 1.1 kiyohara * no more data was available or we ran out of space.
325 1.1 kiyohara * Schedule a receive event if any data was received.
326 1.1 kiyohara * If we're out of space, turn off receive interrupts.
327 1.1 kiyohara */
328 1.1 kiyohara sc->sc_rbput = put;
329 1.1 kiyohara sc->sc_rbavail = cc;
330 1.1 kiyohara
331 1.1 kiyohara /*
332 1.1 kiyohara * See if we are in danger of overflowing a buffer. If
333 1.1 kiyohara * so, use hardware flow control to ease the pressure.
334 1.1 kiyohara */
335 1.1 kiyohara
336 1.1 kiyohara /* but clpscom cannot. X-( */
337 1.1 kiyohara
338 1.1 kiyohara /*
339 1.1 kiyohara * If we're out of space, disable receive interrupts
340 1.1 kiyohara * until the queue has drained a bit.
341 1.1 kiyohara */
342 1.1 kiyohara if (cc <= 0) {
343 1.1 kiyohara intr_disestablish(sc->sc_ih[CLPSCOM_RXINT]);
344 1.1 kiyohara sc->sc_ih[CLPSCOM_RXINT] = NULL;
345 1.1 kiyohara }
346 1.1 kiyohara }
347 1.1 kiyohara
348 1.1 kiyohara /* Wake up the poller. */
349 1.1 kiyohara softint_schedule(sc->sc_si);
350 1.1 kiyohara
351 1.1 kiyohara return 1;
352 1.1 kiyohara }
353 1.1 kiyohara
354 1.1 kiyohara static int
355 1.1 kiyohara clpscom_msintr(void *arg)
356 1.1 kiyohara {
357 1.1 kiyohara struct clpscom_softc *sc = arg;
358 1.1 kiyohara uint32_t ms, delta;
359 1.1 kiyohara
360 1.1 kiyohara if (!device_is_active(sc->sc_dev))
361 1.1 kiyohara return 0;
362 1.1 kiyohara
363 1.1 kiyohara if (sc->sc_ih[CLPSCOM_MSINT] != NULL) {
364 1.1 kiyohara ms = CLPSCOM_READ_FLG(sc) & CLPSCOM_MODEM_STATUS_MASK;
365 1.1 kiyohara delta = ms ^ sc->sc_ms;
366 1.1 kiyohara sc->sc_ms = ms;
367 1.1 kiyohara
368 1.1 kiyohara if (ISSET(delta, sc->sc_ms_mask)) {
369 1.1 kiyohara SET(sc->sc_ms_delta, delta);
370 1.1 kiyohara
371 1.1 kiyohara /*
372 1.1 kiyohara * Stop output immediately if we lose the output
373 1.1 kiyohara * flow control signal or carrier detect.
374 1.1 kiyohara */
375 1.1 kiyohara if (ISSET(~ms, sc->sc_ms_mask))
376 1.1 kiyohara sc->sc_tbc = 0;
377 1.1 kiyohara sc->sc_ms_changed = 1;
378 1.1 kiyohara }
379 1.1 kiyohara }
380 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, PS711X_UMSEOI, 1);
381 1.1 kiyohara
382 1.1 kiyohara /* Wake up the poller. */
383 1.1 kiyohara softint_schedule(sc->sc_si);
384 1.1 kiyohara
385 1.1 kiyohara return 1;
386 1.1 kiyohara }
387 1.1 kiyohara
388 1.1 kiyohara static void
389 1.1 kiyohara clpscom_soft(void *arg)
390 1.1 kiyohara {
391 1.1 kiyohara struct clpscom_softc *sc = arg;
392 1.1 kiyohara struct tty *tp = sc->sc_tty;
393 1.1 kiyohara
394 1.1 kiyohara if (!device_is_active(sc->sc_dev))
395 1.1 kiyohara return;
396 1.1 kiyohara
397 1.1 kiyohara if (sc->sc_rx_ready) {
398 1.1 kiyohara sc->sc_rx_ready = 0;
399 1.1 kiyohara clpscom_rxsoft(sc, tp);
400 1.1 kiyohara }
401 1.1 kiyohara if (sc->sc_tx_done) {
402 1.1 kiyohara sc->sc_tx_done = 0;
403 1.1 kiyohara CLR(tp->t_state, TS_BUSY);
404 1.1 kiyohara if (ISSET(tp->t_state, TS_FLUSH))
405 1.1 kiyohara CLR(tp->t_state, TS_FLUSH);
406 1.1 kiyohara else
407 1.1 kiyohara ndflush(&tp->t_outq,
408 1.1 kiyohara (int)(sc->sc_tba - tp->t_outq.c_cf));
409 1.1 kiyohara (*tp->t_linesw->l_start)(tp);
410 1.1 kiyohara }
411 1.1 kiyohara if (sc->sc_ms_changed == 1) {
412 1.1 kiyohara sc->sc_ms_changed = 0;
413 1.1 kiyohara clpscom_mssoft(sc, tp);
414 1.1 kiyohara }
415 1.1 kiyohara }
416 1.1 kiyohara
417 1.1 kiyohara static void
418 1.1 kiyohara clpscom_start(struct tty *tp)
419 1.1 kiyohara {
420 1.1 kiyohara struct clpscom_softc *sc
421 1.1 kiyohara = device_lookup_private(&clpscom_cd, COMUNIT(tp->t_dev));
422 1.1 kiyohara int s, n;
423 1.1 kiyohara
424 1.1 kiyohara if (!device_is_active(sc->sc_dev))
425 1.1 kiyohara return;
426 1.1 kiyohara
427 1.1 kiyohara s = spltty();
428 1.1 kiyohara if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
429 1.1 kiyohara goto out;
430 1.1 kiyohara if (sc->sc_tx_stopped)
431 1.1 kiyohara goto out;
432 1.1 kiyohara if (!ttypull(tp))
433 1.1 kiyohara goto out;
434 1.1 kiyohara
435 1.1 kiyohara /* Grab the first contiguous region of buffer space. */
436 1.1 kiyohara {
437 1.1 kiyohara u_char *tba;
438 1.1 kiyohara int tbc;
439 1.1 kiyohara
440 1.1 kiyohara tba = tp->t_outq.c_cf;
441 1.1 kiyohara tbc = ndqb(&tp->t_outq, 0);
442 1.1 kiyohara
443 1.1 kiyohara (void)splserial();
444 1.1 kiyohara
445 1.1 kiyohara sc->sc_tba = tba;
446 1.1 kiyohara sc->sc_tbc = tbc;
447 1.1 kiyohara }
448 1.1 kiyohara
449 1.1 kiyohara SET(tp->t_state, TS_BUSY);
450 1.1 kiyohara
451 1.1 kiyohara if (sc->sc_ih[CLPSCOM_TXINT] == NULL) {
452 1.1 kiyohara sc->sc_ih[CLPSCOM_TXINT] =
453 1.1 kiyohara intr_establish(sc->sc_irq[CLPSCOM_TXINT], IPL_SERIAL, 0,
454 1.1 kiyohara clpscom_txintr, sc);
455 1.1 kiyohara if (sc->sc_ih[CLPSCOM_TXINT] == NULL)
456 1.1 kiyohara printf("%s: can't establish tx interrupt\n",
457 1.1 kiyohara device_xname(sc->sc_dev));
458 1.1 kiyohara
459 1.1 kiyohara /* Output the first chunk of the contiguous buffer. */
460 1.7 riastrad n = uimin(sc->sc_tbc, UART_FIFO_SIZE);
461 1.1 kiyohara CLPSCOM_WRITE_MULTI(sc, sc->sc_tba, n);
462 1.1 kiyohara sc->sc_tba += n;
463 1.1 kiyohara sc->sc_tbc -= n;
464 1.1 kiyohara }
465 1.1 kiyohara out:
466 1.1 kiyohara splx(s);
467 1.1 kiyohara return;
468 1.1 kiyohara }
469 1.1 kiyohara
470 1.1 kiyohara static int
471 1.1 kiyohara clpscom_param(struct tty *tp, struct termios *t)
472 1.1 kiyohara {
473 1.1 kiyohara struct clpscom_softc *sc =
474 1.1 kiyohara device_lookup_private(&clpscom_cd, COMUNIT(tp->t_dev));
475 1.1 kiyohara int s;
476 1.1 kiyohara
477 1.1 kiyohara if (!device_is_active(sc->sc_dev))
478 1.1 kiyohara return ENXIO;
479 1.1 kiyohara if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
480 1.1 kiyohara return EINVAL;
481 1.1 kiyohara
482 1.1 kiyohara /*
483 1.1 kiyohara * For the console, always force CLOCAL and !HUPCL, so that the port
484 1.1 kiyohara * is always active.
485 1.1 kiyohara */
486 1.1 kiyohara if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
487 1.1 kiyohara ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
488 1.1 kiyohara SET(t->c_cflag, CLOCAL);
489 1.1 kiyohara CLR(t->c_cflag, HUPCL);
490 1.1 kiyohara }
491 1.1 kiyohara
492 1.1 kiyohara /*
493 1.1 kiyohara * If there were no changes, don't do anything. This avoids dropping
494 1.1 kiyohara * input and improves performance when all we did was frob things like
495 1.1 kiyohara * VMIN and VTIME.
496 1.1 kiyohara */
497 1.1 kiyohara if (tp->t_ospeed == t->c_ospeed &&
498 1.1 kiyohara tp->t_cflag == t->c_cflag)
499 1.1 kiyohara return 0;
500 1.1 kiyohara
501 1.1 kiyohara /*
502 1.1 kiyohara * If we're not in a mode that assumes a connection is present, then
503 1.1 kiyohara * ignore carrier changes.
504 1.1 kiyohara */
505 1.1 kiyohara if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
506 1.1 kiyohara sc->sc_ms_dcd = 0;
507 1.1 kiyohara else
508 1.1 kiyohara sc->sc_ms_dcd = SYSFLG_DCD;
509 1.1 kiyohara /*
510 1.1 kiyohara * Set the flow control pins depending on the current flow control
511 1.1 kiyohara * mode.
512 1.1 kiyohara */
513 1.1 kiyohara if (ISSET(t->c_cflag, CRTSCTS)) {
514 1.1 kiyohara sc->sc_ms_cts = SYSFLG_CTS;
515 1.1 kiyohara } else if (ISSET(t->c_cflag, MDMBUF)) {
516 1.1 kiyohara /*
517 1.1 kiyohara * For DTR/DCD flow control, make sure we don't toggle DTR for
518 1.1 kiyohara * carrier detection.
519 1.1 kiyohara */
520 1.1 kiyohara sc->sc_ms_cts = SYSFLG_DCD;
521 1.1 kiyohara } else {
522 1.1 kiyohara /*
523 1.1 kiyohara * If no flow control, then always set RTS. This will make
524 1.1 kiyohara * the other side happy if it mistakenly thinks we're doing
525 1.1 kiyohara * RTS/CTS flow control.
526 1.1 kiyohara */
527 1.1 kiyohara sc->sc_ms_cts = 0;
528 1.1 kiyohara }
529 1.1 kiyohara sc->sc_ms_mask = sc->sc_ms_cts | sc->sc_ms_dcd;
530 1.1 kiyohara
531 1.1 kiyohara s = splserial();
532 1.1 kiyohara CLPSCOM_WRITE_UBRLCR(sc,
533 1.1 kiyohara UBRLCR_FIFOEN |
534 1.1 kiyohara clpscom_rate2ubrlcr(t->c_ospeed) |
535 1.1 kiyohara clpscom_cflag2ubrlcr(t->c_cflag));
536 1.1 kiyohara
537 1.1 kiyohara /* And copy to tty. */
538 1.1 kiyohara tp->t_ispeed = 0;
539 1.1 kiyohara tp->t_ospeed = t->c_ospeed;
540 1.1 kiyohara tp->t_cflag = t->c_cflag;
541 1.1 kiyohara splx(s);
542 1.1 kiyohara
543 1.1 kiyohara /*
544 1.1 kiyohara * Update the tty layer's idea of the carrier bit, in case we changed
545 1.1 kiyohara * CLOCAL or MDMBUF. We don't hang up here; we only do that by
546 1.1 kiyohara * explicit request.
547 1.1 kiyohara */
548 1.1 kiyohara (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_ms, SYSFLG_DCD));
549 1.1 kiyohara
550 1.1 kiyohara if (!ISSET(t->c_cflag, CHWFLOW))
551 1.1 kiyohara if (sc->sc_tx_stopped) {
552 1.1 kiyohara sc->sc_tx_stopped = 0;
553 1.1 kiyohara clpscom_start(tp);
554 1.1 kiyohara }
555 1.1 kiyohara
556 1.1 kiyohara return 0;
557 1.1 kiyohara }
558 1.1 kiyohara
559 1.1 kiyohara static int
560 1.1 kiyohara clpscom_hwiflow(struct tty *tp, int block)
561 1.1 kiyohara {
562 1.1 kiyohara /* Nothing */
563 1.1 kiyohara return 0;
564 1.1 kiyohara }
565 1.1 kiyohara
566 1.1 kiyohara /* ARGSUSED */
567 1.1 kiyohara int
568 1.1 kiyohara clpscomopen(dev_t dev, int flag, int mode, struct lwp *l)
569 1.1 kiyohara {
570 1.1 kiyohara struct clpscom_softc *sc;
571 1.1 kiyohara struct tty *tp;
572 1.1 kiyohara int error, s, s2;
573 1.1 kiyohara
574 1.1 kiyohara sc = device_lookup_private(&clpscom_cd, COMUNIT(dev));
575 1.1 kiyohara if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
576 1.1 kiyohara return ENXIO;
577 1.1 kiyohara if (!device_is_active(sc->sc_dev))
578 1.1 kiyohara return ENXIO;
579 1.1 kiyohara
580 1.1 kiyohara #ifdef KGDB
581 1.1 kiyohara /*
582 1.1 kiyohara * If this is the kgdb port, no other use is permitted.
583 1.1 kiyohara */
584 1.1 kiyohara if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
585 1.1 kiyohara return EBUSY;
586 1.1 kiyohara #endif
587 1.1 kiyohara
588 1.1 kiyohara tp = sc->sc_tty;
589 1.1 kiyohara
590 1.1 kiyohara if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
591 1.1 kiyohara return EBUSY;
592 1.1 kiyohara
593 1.1 kiyohara s = spltty();
594 1.1 kiyohara
595 1.1 kiyohara /*
596 1.1 kiyohara * Do the following iff this is a first open.
597 1.1 kiyohara */
598 1.1 kiyohara if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
599 1.1 kiyohara struct termios t;
600 1.1 kiyohara
601 1.1 kiyohara tp->t_dev = dev;
602 1.1 kiyohara
603 1.1 kiyohara /* Enable and turn on interrupt */
604 1.1 kiyohara CLPSCOM_WRITE_CON(sc, CLPSCOM_READ_CON(sc) | SYSCON_UARTEN);
605 1.1 kiyohara
606 1.1 kiyohara /* Fetch the current modem control status, needed later. */
607 1.1 kiyohara sc->sc_ms = CLPSCOM_READ_FLG(sc) & CLPSCOM_MODEM_STATUS_MASK;
608 1.1 kiyohara
609 1.1 kiyohara /*
610 1.1 kiyohara * Initialize the termios status to the defaults. Add in the
611 1.1 kiyohara * sticky bits from TIOCSFLAGS.
612 1.1 kiyohara */
613 1.1 kiyohara t.c_ispeed = 0;
614 1.1 kiyohara if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
615 1.1 kiyohara t.c_ospeed = clpscom_cnrate;
616 1.1 kiyohara t.c_cflag = clpscom_cncflag;
617 1.1 kiyohara } else {
618 1.1 kiyohara t.c_ospeed = TTYDEF_SPEED;
619 1.1 kiyohara t.c_cflag = TTYDEF_CFLAG;
620 1.1 kiyohara }
621 1.1 kiyohara if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
622 1.1 kiyohara SET(t.c_cflag, CLOCAL);
623 1.1 kiyohara if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
624 1.1 kiyohara SET(t.c_cflag, CRTSCTS);
625 1.1 kiyohara if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
626 1.1 kiyohara SET(t.c_cflag, MDMBUF);
627 1.1 kiyohara /* Make sure pscom_param() we do something */
628 1.1 kiyohara tp->t_ospeed = 0;
629 1.1 kiyohara clpscom_param(tp, &t);
630 1.1 kiyohara tp->t_iflag = TTYDEF_IFLAG;
631 1.1 kiyohara tp->t_oflag = TTYDEF_OFLAG;
632 1.1 kiyohara tp->t_lflag = TTYDEF_LFLAG;
633 1.1 kiyohara ttychars(tp);
634 1.1 kiyohara ttsetwater(tp);
635 1.1 kiyohara
636 1.1 kiyohara s2 = splserial();
637 1.1 kiyohara
638 1.1 kiyohara /* Clear the input ring. */
639 1.1 kiyohara sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
640 1.1 kiyohara sc->sc_rbavail = CLPSCOM_RING_SIZE;
641 1.1 kiyohara clpscom_iflush(sc);
642 1.1 kiyohara
643 1.1 kiyohara splx(s2);
644 1.1 kiyohara }
645 1.1 kiyohara
646 1.1 kiyohara splx(s);
647 1.1 kiyohara
648 1.1 kiyohara error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
649 1.1 kiyohara if (error)
650 1.1 kiyohara goto bad;
651 1.1 kiyohara
652 1.1 kiyohara error = (*tp->t_linesw->l_open)(dev, tp);
653 1.1 kiyohara if (error)
654 1.1 kiyohara goto bad;
655 1.1 kiyohara return 0;
656 1.1 kiyohara
657 1.1 kiyohara bad:
658 1.1 kiyohara if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
659 1.1 kiyohara /*
660 1.1 kiyohara * We failed to open the device, and nobody else had it opened.
661 1.1 kiyohara * Clean up the state as appropriate.
662 1.1 kiyohara */
663 1.1 kiyohara clpscom_shutdown(sc);
664 1.1 kiyohara
665 1.1 kiyohara /* Disable UART */
666 1.1 kiyohara if (!ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
667 1.1 kiyohara CLPSCOM_WRITE_CON(sc,
668 1.1 kiyohara CLPSCOM_READ_CON(sc) & ~SYSCON_UARTEN);
669 1.1 kiyohara }
670 1.1 kiyohara
671 1.1 kiyohara return error;
672 1.1 kiyohara }
673 1.1 kiyohara
674 1.1 kiyohara /* ARGSUSED */
675 1.1 kiyohara int
676 1.1 kiyohara clpscomclose(dev_t dev, int flag, int mode, struct lwp *l)
677 1.1 kiyohara {
678 1.1 kiyohara struct clpscom_softc *sc =
679 1.1 kiyohara device_lookup_private(&clpscom_cd, COMUNIT(dev));
680 1.1 kiyohara struct tty *tp = sc->sc_tty;
681 1.1 kiyohara
682 1.1 kiyohara /* XXXX This is for cons.c. */
683 1.1 kiyohara if (!ISSET(tp->t_state, TS_ISOPEN))
684 1.1 kiyohara return 0;
685 1.1 kiyohara
686 1.1 kiyohara (*tp->t_linesw->l_close)(tp, flag);
687 1.1 kiyohara ttyclose(tp);
688 1.1 kiyohara
689 1.1 kiyohara if (!device_is_active(sc->sc_dev))
690 1.1 kiyohara return 0;
691 1.1 kiyohara
692 1.1 kiyohara if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
693 1.1 kiyohara /*
694 1.1 kiyohara * Although we got a last close, the device may still be in
695 1.1 kiyohara * use; e.g. if this was the dialout node, and there are still
696 1.1 kiyohara * processes waiting for carrier on the non-dialout node.
697 1.1 kiyohara */
698 1.1 kiyohara clpscom_shutdown(sc);
699 1.1 kiyohara
700 1.1 kiyohara /* Disable UART */
701 1.1 kiyohara if (!ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
702 1.1 kiyohara CLPSCOM_WRITE_CON(sc,
703 1.1 kiyohara CLPSCOM_READ_CON(sc) & ~SYSCON_UARTEN);
704 1.1 kiyohara }
705 1.1 kiyohara
706 1.1 kiyohara return 0;
707 1.1 kiyohara }
708 1.1 kiyohara
709 1.1 kiyohara int
710 1.1 kiyohara clpscomread(dev_t dev, struct uio *uio, int flag)
711 1.1 kiyohara {
712 1.1 kiyohara struct clpscom_softc *sc =
713 1.1 kiyohara device_lookup_private(&clpscom_cd, COMUNIT(dev));
714 1.1 kiyohara struct tty *tp = sc->sc_tty;
715 1.1 kiyohara
716 1.1 kiyohara if (!device_is_active(sc->sc_dev))
717 1.1 kiyohara return EIO;
718 1.1 kiyohara
719 1.1 kiyohara return (*tp->t_linesw->l_read)(tp, uio, flag);
720 1.1 kiyohara }
721 1.1 kiyohara
722 1.1 kiyohara int
723 1.1 kiyohara clpscomwrite(dev_t dev, struct uio *uio, int flag)
724 1.1 kiyohara {
725 1.1 kiyohara struct clpscom_softc *sc =
726 1.1 kiyohara device_lookup_private(&clpscom_cd, COMUNIT(dev));
727 1.1 kiyohara struct tty *tp = sc->sc_tty;
728 1.1 kiyohara
729 1.1 kiyohara if (!device_is_active(sc->sc_dev))
730 1.1 kiyohara return EIO;
731 1.1 kiyohara
732 1.1 kiyohara return (*tp->t_linesw->l_write)(tp, uio, flag);
733 1.1 kiyohara }
734 1.1 kiyohara
735 1.1 kiyohara int
736 1.1 kiyohara clpscomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
737 1.1 kiyohara {
738 1.1 kiyohara struct clpscom_softc *sc =
739 1.1 kiyohara device_lookup_private(&clpscom_cd, COMUNIT(dev));
740 1.1 kiyohara struct tty *tp = sc->sc_tty;
741 1.1 kiyohara int error, s;
742 1.1 kiyohara
743 1.1 kiyohara if (!device_is_active(sc->sc_dev))
744 1.1 kiyohara return EIO;
745 1.1 kiyohara
746 1.1 kiyohara error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
747 1.1 kiyohara if (error != EPASSTHROUGH)
748 1.1 kiyohara return error;
749 1.1 kiyohara
750 1.1 kiyohara error = ttioctl(tp, cmd, data, flag, l);
751 1.1 kiyohara if (error != EPASSTHROUGH)
752 1.1 kiyohara return error;
753 1.1 kiyohara
754 1.1 kiyohara switch (cmd) {
755 1.1 kiyohara case TIOCSFLAGS:
756 1.1 kiyohara error = kauth_authorize_device_tty(l->l_cred,
757 1.1 kiyohara KAUTH_DEVICE_TTY_PRIVSET, tp);
758 1.1 kiyohara break;
759 1.1 kiyohara default:
760 1.1 kiyohara break;
761 1.1 kiyohara }
762 1.1 kiyohara if (error)
763 1.1 kiyohara return error;
764 1.1 kiyohara
765 1.1 kiyohara s = splserial();
766 1.1 kiyohara error = 0;
767 1.1 kiyohara switch (cmd) {
768 1.1 kiyohara case TIOCSBRK:
769 1.1 kiyohara clpscom_break(sc, 1);
770 1.1 kiyohara break;
771 1.1 kiyohara
772 1.1 kiyohara case TIOCCBRK:
773 1.1 kiyohara clpscom_break(sc, 0);
774 1.1 kiyohara break;
775 1.1 kiyohara
776 1.1 kiyohara case TIOCGFLAGS:
777 1.1 kiyohara *(int *)data = sc->sc_swflags;
778 1.1 kiyohara break;
779 1.1 kiyohara
780 1.1 kiyohara case TIOCSFLAGS:
781 1.1 kiyohara sc->sc_swflags = *(int *)data;
782 1.1 kiyohara break;
783 1.1 kiyohara
784 1.1 kiyohara case TIOCMGET:
785 1.1 kiyohara *(int *)data = clpscom_to_tiocm(sc);
786 1.1 kiyohara break;
787 1.1 kiyohara
788 1.1 kiyohara default:
789 1.1 kiyohara error = EPASSTHROUGH;
790 1.1 kiyohara break;
791 1.1 kiyohara }
792 1.1 kiyohara splx(s);
793 1.1 kiyohara return error;
794 1.1 kiyohara }
795 1.1 kiyohara
796 1.1 kiyohara int
797 1.1 kiyohara clpscompoll(dev_t dev, int events, struct lwp *l)
798 1.1 kiyohara {
799 1.1 kiyohara struct clpscom_softc *sc =
800 1.1 kiyohara device_lookup_private(&clpscom_cd, COMUNIT(dev));
801 1.1 kiyohara struct tty *tp = sc->sc_tty;
802 1.1 kiyohara
803 1.1 kiyohara if (!device_is_active(sc->sc_dev))
804 1.1 kiyohara return EIO;
805 1.1 kiyohara
806 1.1 kiyohara return (*tp->t_linesw->l_poll)(tp, events, l);
807 1.1 kiyohara }
808 1.1 kiyohara
809 1.1 kiyohara struct tty *
810 1.1 kiyohara clpscomtty(dev_t dev)
811 1.1 kiyohara {
812 1.1 kiyohara struct clpscom_softc *sc =
813 1.1 kiyohara device_lookup_private(&clpscom_cd, COMUNIT(dev));
814 1.1 kiyohara
815 1.1 kiyohara return sc->sc_tty;
816 1.1 kiyohara }
817 1.1 kiyohara
818 1.1 kiyohara void
819 1.1 kiyohara clpscomstop(struct tty *tp, int flag)
820 1.1 kiyohara {
821 1.1 kiyohara int s;
822 1.1 kiyohara
823 1.1 kiyohara s = splserial();
824 1.1 kiyohara if (ISSET(tp->t_state, TS_BUSY)) {
825 1.1 kiyohara /* Stop transmitting at the next chunk. */
826 1.1 kiyohara if (!ISSET(tp->t_state, TS_TTSTOP))
827 1.1 kiyohara SET(tp->t_state, TS_FLUSH);
828 1.1 kiyohara }
829 1.1 kiyohara splx(s);
830 1.1 kiyohara }
831 1.1 kiyohara
832 1.1 kiyohara
833 1.1 kiyohara static void
834 1.1 kiyohara clpscom_iflush(struct clpscom_softc *sc)
835 1.1 kiyohara {
836 1.1 kiyohara int timo;
837 1.1 kiyohara
838 1.1 kiyohara timo = 50000;
839 1.1 kiyohara while ((CLPSCOM_READ_FLG(sc) & SYSFLG_URXFE) == 0
840 1.1 kiyohara && timo--)
841 1.1 kiyohara CLPSCOM_READ(sc);
842 1.1 kiyohara if (timo == 0)
843 1.1 kiyohara printf("%s: iflush timeout\n", device_xname(sc->sc_dev));
844 1.1 kiyohara }
845 1.1 kiyohara
846 1.1 kiyohara static void
847 1.1 kiyohara clpscom_shutdown(struct clpscom_softc *sc)
848 1.1 kiyohara {
849 1.1 kiyohara int s;
850 1.1 kiyohara
851 1.1 kiyohara s = splserial();
852 1.1 kiyohara
853 1.1 kiyohara /* Turn off all interrupts */
854 1.1 kiyohara if (sc->sc_ih[CLPSCOM_TXINT] != NULL)
855 1.1 kiyohara intr_disestablish(sc->sc_ih[CLPSCOM_TXINT]);
856 1.1 kiyohara sc->sc_ih[CLPSCOM_TXINT] = NULL;
857 1.1 kiyohara if (sc->sc_ih[CLPSCOM_RXINT] != NULL)
858 1.1 kiyohara intr_disestablish(sc->sc_ih[CLPSCOM_RXINT]);
859 1.1 kiyohara sc->sc_ih[CLPSCOM_RXINT] = NULL;
860 1.1 kiyohara if (sc->sc_ih[CLPSCOM_MSINT] != NULL)
861 1.1 kiyohara intr_disestablish(sc->sc_ih[CLPSCOM_MSINT]);
862 1.1 kiyohara sc->sc_ih[CLPSCOM_MSINT] = NULL;
863 1.1 kiyohara
864 1.1 kiyohara /* Clear any break condition set with TIOCSBRK. */
865 1.1 kiyohara clpscom_break(sc, 0);
866 1.1 kiyohara
867 1.1 kiyohara splx(s);
868 1.1 kiyohara }
869 1.1 kiyohara
870 1.1 kiyohara static void
871 1.1 kiyohara clpscom_break(struct clpscom_softc *sc, int onoff)
872 1.1 kiyohara {
873 1.1 kiyohara int s;
874 1.1 kiyohara uint8_t ubrlcr;
875 1.1 kiyohara
876 1.1 kiyohara s = splserial();
877 1.1 kiyohara ubrlcr = CLPSCOM_READ_UBRLCR(sc);
878 1.1 kiyohara if (onoff)
879 1.1 kiyohara SET(ubrlcr, UBRLCR_BREAK);
880 1.1 kiyohara else
881 1.1 kiyohara CLR(ubrlcr, UBRLCR_BREAK);
882 1.1 kiyohara CLPSCOM_WRITE_UBRLCR(sc, ubrlcr);
883 1.1 kiyohara splx(s);
884 1.1 kiyohara }
885 1.1 kiyohara
886 1.1 kiyohara static int
887 1.1 kiyohara clpscom_to_tiocm(struct clpscom_softc *sc)
888 1.1 kiyohara {
889 1.1 kiyohara uint32_t combits;
890 1.1 kiyohara int ttybits = 0;
891 1.1 kiyohara
892 1.1 kiyohara combits = sc->sc_ms;
893 1.1 kiyohara if (ISSET(combits, SYSFLG_DCD))
894 1.1 kiyohara SET(ttybits, TIOCM_CD);
895 1.1 kiyohara if (ISSET(combits, SYSFLG_CTS))
896 1.1 kiyohara SET(ttybits, TIOCM_CTS);
897 1.1 kiyohara if (ISSET(combits, SYSFLG_DSR))
898 1.1 kiyohara SET(ttybits, TIOCM_DSR);
899 1.1 kiyohara
900 1.1 kiyohara return ttybits;
901 1.1 kiyohara }
902 1.1 kiyohara
903 1.1 kiyohara static void
904 1.1 kiyohara clpscom_rxsoft(struct clpscom_softc *sc, struct tty *tp)
905 1.1 kiyohara {
906 1.1 kiyohara int code, s;
907 1.1 kiyohara u_int cc, scc;
908 1.1 kiyohara u_char sts, *get;
909 1.1 kiyohara
910 1.1 kiyohara get = sc->sc_rbget;
911 1.1 kiyohara scc = cc = CLPSCOM_RING_SIZE - sc->sc_rbavail;
912 1.1 kiyohara while (cc) {
913 1.1 kiyohara code = get[0];
914 1.1 kiyohara sts = get[1];
915 1.1 kiyohara if (ISSET(sts, UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR)) {
916 1.1 kiyohara if (ISSET(sts, (UARTDR_FRMERR)))
917 1.1 kiyohara SET(code, TTY_FE);
918 1.1 kiyohara if (ISSET(sts, UARTDR_PARERR))
919 1.1 kiyohara SET(code, TTY_PE);
920 1.1 kiyohara if (ISSET(sts, UARTDR_OVERR))
921 1.1 kiyohara ; /* XXXXX: Overrun */
922 1.1 kiyohara }
923 1.1 kiyohara if ((*tp->t_linesw->l_rint)(code, tp) == -1) {
924 1.1 kiyohara /*
925 1.1 kiyohara * The line discipline's buffer is out of space.
926 1.1 kiyohara */
927 1.1 kiyohara /*
928 1.1 kiyohara * We're either not using flow control, or the
929 1.1 kiyohara * line discipline didn't tell us to block for
930 1.1 kiyohara * some reason. Either way, we have no way to
931 1.1 kiyohara * know when there's more space available, so
932 1.1 kiyohara * just drop the rest of the data.
933 1.1 kiyohara */
934 1.1 kiyohara get += cc << 1;
935 1.1 kiyohara if (get >= sc->sc_rbuf + (CLPSCOM_RING_SIZE << 1))
936 1.1 kiyohara get -= (CLPSCOM_RING_SIZE << 1);
937 1.1 kiyohara cc = 0;
938 1.1 kiyohara break;
939 1.1 kiyohara }
940 1.1 kiyohara get += 2;
941 1.1 kiyohara if (get >= sc->sc_rbuf + (CLPSCOM_RING_SIZE << 1))
942 1.1 kiyohara get = sc->sc_rbuf;
943 1.1 kiyohara cc--;
944 1.1 kiyohara }
945 1.1 kiyohara
946 1.1 kiyohara if (cc != scc) {
947 1.1 kiyohara sc->sc_rbget = get;
948 1.1 kiyohara s = splserial();
949 1.1 kiyohara
950 1.1 kiyohara cc = sc->sc_rbavail += scc - cc;
951 1.1 kiyohara /* Buffers should be ok again, release possible block. */
952 1.1 kiyohara if (cc >= 1) {
953 1.1 kiyohara if (sc->sc_ih[CLPSCOM_RXINT] == NULL) {
954 1.1 kiyohara sc->sc_ih[CLPSCOM_RXINT] =
955 1.1 kiyohara intr_establish(sc->sc_irq[CLPSCOM_RXINT],
956 1.1 kiyohara IPL_SERIAL, 0, clpscom_rxintr, sc);
957 1.1 kiyohara if (sc->sc_ih[CLPSCOM_RXINT] == NULL)
958 1.1 kiyohara printf("%s: can't establish"
959 1.1 kiyohara " rx interrupt\n",
960 1.1 kiyohara device_xname(sc->sc_dev));
961 1.1 kiyohara }
962 1.1 kiyohara if (sc->sc_ih[CLPSCOM_MSINT] == NULL) {
963 1.1 kiyohara sc->sc_ih[CLPSCOM_MSINT] =
964 1.1 kiyohara intr_establish(sc->sc_irq[CLPSCOM_MSINT],
965 1.1 kiyohara IPL_SERIAL, 0, clpscom_msintr, sc);
966 1.1 kiyohara if (sc->sc_ih[CLPSCOM_MSINT] == NULL)
967 1.1 kiyohara printf("%s: can't establish"
968 1.1 kiyohara " ms interrupt\n",
969 1.1 kiyohara device_xname(sc->sc_dev));
970 1.1 kiyohara }
971 1.1 kiyohara }
972 1.1 kiyohara splx(s);
973 1.1 kiyohara }
974 1.1 kiyohara }
975 1.1 kiyohara
976 1.1 kiyohara static void
977 1.1 kiyohara clpscom_mssoft(struct clpscom_softc *sc, struct tty *tp)
978 1.1 kiyohara {
979 1.1 kiyohara uint32_t ms, delta;
980 1.1 kiyohara
981 1.1 kiyohara ms = sc->sc_ms;
982 1.1 kiyohara delta = sc->sc_ms_delta;
983 1.1 kiyohara sc->sc_ms_delta = 0;
984 1.1 kiyohara
985 1.1 kiyohara if (ISSET(delta, sc->sc_ms_dcd))
986 1.1 kiyohara /*
987 1.1 kiyohara * Inform the tty layer that carrier detect changed.
988 1.1 kiyohara */
989 1.1 kiyohara (void) (*tp->t_linesw->l_modem)(tp, ISSET(ms, SYSFLG_DCD));
990 1.1 kiyohara
991 1.1 kiyohara if (ISSET(delta, sc->sc_ms_cts)) {
992 1.1 kiyohara /* Block or unblock output according to flow control. */
993 1.1 kiyohara if (ISSET(ms, sc->sc_ms_cts)) {
994 1.1 kiyohara sc->sc_tx_stopped = 0;
995 1.1 kiyohara (*tp->t_linesw->l_start)(tp);
996 1.1 kiyohara } else
997 1.1 kiyohara sc->sc_tx_stopped = 1;
998 1.1 kiyohara }
999 1.1 kiyohara }
1000 1.1 kiyohara
1001 1.1 kiyohara static inline uint32_t
1002 1.1 kiyohara clpscom_rate2ubrlcr(int rate)
1003 1.1 kiyohara {
1004 1.1 kiyohara
1005 1.1 kiyohara return 230400 / rate - 1;
1006 1.1 kiyohara }
1007 1.1 kiyohara
1008 1.1 kiyohara static uint32_t
1009 1.1 kiyohara clpscom_cflag2ubrlcr(tcflag_t cflag)
1010 1.1 kiyohara {
1011 1.1 kiyohara int32_t ubrlcr = 0;
1012 1.1 kiyohara
1013 1.1 kiyohara switch (cflag & CSIZE) {
1014 1.1 kiyohara case CS5: SET(ubrlcr, UBRLCR_WRDLEN_5B); break;
1015 1.1 kiyohara case CS6: SET(ubrlcr, UBRLCR_WRDLEN_6B); break;
1016 1.1 kiyohara case CS7: SET(ubrlcr, UBRLCR_WRDLEN_7B); break;
1017 1.1 kiyohara case CS8: SET(ubrlcr, UBRLCR_WRDLEN_8B); break;
1018 1.1 kiyohara default: SET(ubrlcr, UBRLCR_WRDLEN_8B); break;
1019 1.1 kiyohara }
1020 1.1 kiyohara if (cflag & CSTOPB)
1021 1.1 kiyohara SET(ubrlcr, UBRLCR_XSTOP);
1022 1.1 kiyohara if (cflag & PARENB) {
1023 1.1 kiyohara SET(ubrlcr, (UBRLCR_PRTEN | UBRLCR_EVENPRT));
1024 1.1 kiyohara if (cflag & PARODD)
1025 1.1 kiyohara CLR(ubrlcr, UBRLCR_EVENPRT);
1026 1.1 kiyohara }
1027 1.1 kiyohara return ubrlcr;
1028 1.1 kiyohara }
1029 1.1 kiyohara
1030 1.1 kiyohara #define CLPSCOM_CNREAD() \
1031 1.1 kiyohara (*(volatile uint32_t *)(clpscom_cnaddr + PS711X_UARTDR))
1032 1.1 kiyohara #define CLPSCOM_CNWRITE(val) \
1033 1.1 kiyohara (*(volatile uint8_t *)(clpscom_cnaddr + PS711X_UARTDR) = val)
1034 1.1 kiyohara #define CLPSCOM_CNSTATUS() \
1035 1.1 kiyohara (*(volatile uint32_t *)(clpscom_cnaddr + PS711X_SYSFLG))
1036 1.1 kiyohara
1037 1.1 kiyohara static struct consdev clpscomcons = {
1038 1.1 kiyohara NULL, NULL, clpscom_cngetc, clpscom_cnputc, clpscom_cnpollc,
1039 1.1 kiyohara NULL, NULL, NULL, NODEV, CN_NORMAL
1040 1.1 kiyohara };
1041 1.1 kiyohara
1042 1.1 kiyohara int
1043 1.1 kiyohara clpscom_cnattach(vaddr_t addr, int rate, tcflag_t cflag)
1044 1.1 kiyohara {
1045 1.1 kiyohara
1046 1.1 kiyohara clpscom_cnaddr = addr;
1047 1.1 kiyohara clpscom_cnrate = rate;
1048 1.1 kiyohara clpscom_cncflag = cflag;
1049 1.1 kiyohara *(volatile uint32_t *)(clpscom_cnaddr + PS711X_SYSFLG) |= SYSCON_UARTEN;
1050 1.1 kiyohara *(volatile uint32_t *)(clpscom_cnaddr + PS711X_UBRLCR) =
1051 1.1 kiyohara UBRLCR_FIFOEN |
1052 1.1 kiyohara clpscom_cflag2ubrlcr(cflag) |
1053 1.1 kiyohara clpscom_rate2ubrlcr(rate);
1054 1.1 kiyohara
1055 1.1 kiyohara cn_tab = &clpscomcons;
1056 1.1 kiyohara cn_init_magic(&clpscom_cnm_state);
1057 1.1 kiyohara cn_set_magic("\047\001"); /* default magic is BREAK */
1058 1.1 kiyohara
1059 1.1 kiyohara return 0;
1060 1.1 kiyohara }
1061 1.1 kiyohara
1062 1.1 kiyohara /* ARGSUSED */
1063 1.1 kiyohara static int
1064 1.1 kiyohara clpscom_cngetc(dev_t dev)
1065 1.1 kiyohara {
1066 1.1 kiyohara int s = splserial();
1067 1.1 kiyohara char ch;
1068 1.1 kiyohara
1069 1.1 kiyohara while (CLPSCOM_CNSTATUS() & SYSFLG_URXFE);
1070 1.1 kiyohara
1071 1.1 kiyohara ch = CLPSCOM_CNREAD();
1072 1.1 kiyohara
1073 1.1 kiyohara {
1074 1.1 kiyohara #ifdef DDB
1075 1.1 kiyohara extern int db_active;
1076 1.1 kiyohara if (!db_active)
1077 1.1 kiyohara #endif
1078 1.1 kiyohara cn_check_magic(dev, ch, clpscom_cnm_state);
1079 1.1 kiyohara }
1080 1.1 kiyohara
1081 1.1 kiyohara splx(s);
1082 1.1 kiyohara return ch;
1083 1.1 kiyohara }
1084 1.1 kiyohara
1085 1.1 kiyohara /* ARGSUSED */
1086 1.1 kiyohara static void
1087 1.1 kiyohara clpscom_cnputc(dev_t dev, int c)
1088 1.1 kiyohara {
1089 1.1 kiyohara int s = splserial();
1090 1.1 kiyohara
1091 1.1 kiyohara while (CLPSCOM_CNSTATUS() & SYSFLG_UTXFF);
1092 1.1 kiyohara
1093 1.1 kiyohara CLPSCOM_CNWRITE(c);
1094 1.1 kiyohara
1095 1.1 kiyohara /* Make sure output. */
1096 1.1 kiyohara while (CLPSCOM_CNSTATUS() & SYSFLG_UBUSY);
1097 1.1 kiyohara
1098 1.1 kiyohara splx(s);
1099 1.1 kiyohara }
1100 1.1 kiyohara
1101 1.1 kiyohara /* ARGSUSED */
1102 1.1 kiyohara static void
1103 1.1 kiyohara clpscom_cnpollc(dev_t dev, int on)
1104 1.1 kiyohara {
1105 1.1 kiyohara /* Nothing */
1106 1.1 kiyohara }
1107