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