summaryrefslogtreecommitdiff
path: root/sys/arch/arm/clps711x/clpscom.c
blob: d7f368a9008a0c2788a9497d84ae11ae7c366980 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
/*      $NetBSD: clpscom.c,v 1.10 2022/10/26 23:38:06 riastradh Exp $      */
/*
 * Copyright (c) 2013 KIYOHARA Takashi
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: clpscom.c,v 1.10 2022/10/26 23:38:06 riastradh Exp $");

#include "rnd.h"

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/intr.h>
#include <sys/kauth.h>
#include <sys/lwp.h>
#include <sys/kmem.h>
#include <sys/systm.h>
#include <sys/termios.h>
#include <sys/tty.h>
#include <sys/types.h>

#include <ddb/db_active.h>

#include <arm/clps711x/clps711xreg.h>
#include <arm/clps711x/clpssocvar.h>

#include <dev/cons.h>

#ifdef RND_COM
#include <sys/rndsource.h>
#endif

#include "ioconf.h"
#include "locators.h"

#define COMUNIT(x)	TTUNIT(x)
#define COMDIALOUT(x)	TTDIALOUT(x)

#define CLPSCOM_RING_SIZE	2048
#define UART_FIFO_SIZE		16

#define CLPSCOM_READ_CON(sc) \
	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, PS711X_SYSCON)
#define CLPSCOM_WRITE_CON(sc, val) \
	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, PS711X_SYSCON, (val))
#define CLPSCOM_READ_FLG(sc) \
	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, PS711X_SYSFLG)
#define CLPSCOM_WRITE_FLG(sc, val) \
	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, PS711X_SYSFLG, (val))
#define CLPSCOM_READ(sc) \
	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, PS711X_UARTDR)
#define CLPSCOM_WRITE(sc, val) \
	bus_space_write_1((sc)->sc_iot, (sc)->sc_ioh, PS711X_UARTDR, (val))
#define CLPSCOM_WRITE_MULTI(sc, val, n) \
  bus_space_write_multi_1((sc)->sc_iot, (sc)->sc_ioh, PS711X_UARTDR, (val), (n))
#define CLPSCOM_READ_UBRLCR(sc) \
	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, PS711X_UBRLCR)
#define CLPSCOM_WRITE_UBRLCR(sc, val) \
	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, PS711X_UBRLCR, (val))

struct clpscom_softc {
	device_t sc_dev;
	bus_space_tag_t sc_iot;
	bus_space_handle_t sc_ioh;
	int sc_irq[3];
	void *sc_ih[3];
#define CLPSCOM_TXINT	0
#define CLPSCOM_RXINT	1
#define CLPSCOM_MSINT	2

	void *sc_si;

	struct tty *sc_tty;

	u_char *sc_tba;
	u_int sc_tbc;
	u_char *sc_rbuf;
	char *volatile sc_rbget;
	char *volatile sc_rbput;
	volatile int sc_rbavail;

#define CLPSCOM_MODEM_STATUS_MASK (SYSFLG_DCD | SYSFLG_DSR | SYSFLG_CTS)
	uint32_t sc_ms;
	uint32_t sc_ms_dcd;
	uint32_t sc_ms_cts;
	uint32_t sc_ms_mask;
	uint32_t sc_ms_delta;

	int sc_tx_stopped;

	int sc_tx_done;
	int sc_rx_ready;
	int sc_ms_changed;

	int sc_hwflags;
#define COM_HW_CONSOLE	(1 << 0)
#define COM_HW_DEV_OK	(1 << 1)
#define COM_HW_KGDB	(1 << 2)
	int sc_swflags;

#ifdef RND_COM
	krandsource_t rnd_source;
#endif
};

static int clpscom_match(device_t, cfdata_t, void *);
static void clpscom_attach(device_t, device_t, void *);

static int clpscom_txintr(void *);
static int clpscom_rxintr(void *);
static int clpscom_msintr(void *);
static void clpscom_soft(void *);

static void clpscom_start(struct tty *);
static int clpscom_param(struct tty *, struct termios *);
static int clpscom_hwiflow(struct tty *, int);

dev_type_open(clpscomopen);
dev_type_close(clpscomclose);
dev_type_read(clpscomread);
dev_type_write(clpscomwrite);
dev_type_ioctl(clpscomioctl);
dev_type_stop(clpscomstop);
dev_type_tty(clpscomtty);
dev_type_poll(clpscompoll);

static void clpscom_iflush(struct clpscom_softc *);
static void clpscom_shutdown(struct clpscom_softc *);
static void clpscom_break(struct clpscom_softc *, int);
static int clpscom_to_tiocm(struct clpscom_softc *);

static void clpscom_rxsoft(struct clpscom_softc *, struct tty *);
static void clpscom_mssoft(struct clpscom_softc *, struct tty *);

static inline uint32_t clpscom_rate2ubrlcr(int);
static uint32_t clpscom_cflag2ubrlcr(tcflag_t);

static int clpscom_cngetc(dev_t);
static void clpscom_cnputc(dev_t, int);
static void clpscom_cnpollc(dev_t, int);

CFATTACH_DECL_NEW(clpscom, sizeof(struct clpscom_softc),
    clpscom_match, clpscom_attach, NULL, NULL);

const struct cdevsw clpscom_cdevsw = {
	.d_open = clpscomopen,
	.d_close = clpscomclose,
	.d_read = clpscomread,
	.d_write = clpscomwrite,
	.d_ioctl = clpscomioctl,
	.d_stop = clpscomstop,
	.d_tty = clpscomtty,
	.d_poll = clpscompoll,
	.d_mmap = nommap,
	.d_kqfilter = ttykqfilter,
	.d_discard = nodiscard,
	.d_flag = D_TTY
};

static struct cnm_state clpscom_cnm_state;
static vaddr_t clpscom_cnaddr = 0;
static int clpscom_cnrate;
static tcflag_t clpscom_cncflag;


/* ARGSUSED */
static int
clpscom_match(device_t parent, cfdata_t match, void *aux)
{

	return 1;
}

/* ARGSUSED */
static void
clpscom_attach(device_t parent, device_t self, void *aux)
{
	struct clpscom_softc *sc = device_private(self);
	struct clpssoc_attach_args *aa = aux;
	int i;

	aprint_naive("\n");
	aprint_normal("\n");

	sc->sc_dev = self;
	sc->sc_iot = aa->aa_iot;
	sc->sc_ioh = *aa->aa_ioh;
	for (i = 0; i < __arraycount(aa->aa_irq); i++) {
		sc->sc_irq[i] = aa->aa_irq[i];
		sc->sc_ih[i] = NULL;
	}

	if (clpscom_cnaddr != 0)
		SET(sc->sc_hwflags, COM_HW_CONSOLE);

	sc->sc_tty = tty_alloc();
	sc->sc_tty->t_oproc = clpscom_start;
	sc->sc_tty->t_param = clpscom_param;
	sc->sc_tty->t_hwiflow = clpscom_hwiflow;

	sc->sc_tbc = 0;
	sc->sc_rbuf = kmem_alloc(CLPSCOM_RING_SIZE << 1, KM_SLEEP);
	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
	sc->sc_rbavail = CLPSCOM_RING_SIZE;

	tty_attach(sc->sc_tty);

	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
		int maj = cdevsw_lookup_major(&clpscom_cdevsw);

		sc->sc_tty->t_dev = makedev(maj, device_unit(sc->sc_dev));
		cn_tab->cn_dev = sc->sc_tty->t_dev;

		aprint_normal_dev(self, "console\n");
	}

	sc->sc_si = softint_establish(SOFTINT_SERIAL, clpscom_soft, sc);

#ifdef RND_COM
	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
	    RND_TYPE_TTY, RND_FLAG_DEFAULT);
#endif

	SET(sc->sc_hwflags, COM_HW_DEV_OK);
}

static int
clpscom_txintr(void *arg)
{
	struct clpscom_softc *sc = arg;
	uint32_t sysflg;

	if (!device_is_active(sc->sc_dev))
		return 0;

	sysflg = CLPSCOM_READ_FLG(sc);

	/*
	 * Done handling any receive interrupts. See if data can be
	 * transmitted as well. Schedule tx done event if no data left
	 * and tty was marked busy.
	 */

	if (!ISSET(sysflg, SYSFLG_UTXFF)) {
		/* Output the next chunk of the contiguous buffer, if any. */
		if (sc->sc_tbc > 0) {
			while (sc->sc_tbc > 0 && !ISSET(sysflg, SYSFLG_UTXFF)) {
				CLPSCOM_WRITE(sc, *sc->sc_tba);
				sc->sc_tba++;
				sc->sc_tbc--;
				sysflg = CLPSCOM_READ_FLG(sc);
			}
		} else if (!ISSET(sysflg, SYSFLG_UBUSY) &&
					sc->sc_ih[CLPSCOM_TXINT] != NULL) {
			intr_disestablish(sc->sc_ih[CLPSCOM_TXINT]);
			sc->sc_ih[CLPSCOM_TXINT] = NULL;
			sc->sc_tx_done = 1;
		}
	}

	/* Wake up the poller. */
	softint_schedule(sc->sc_si);

	return 1;
}

static int
clpscom_rxintr(void *arg)
{
	struct clpscom_softc *sc = arg;
	int cc;
	uint32_t sysflg;
	uint16_t data;
	u_char *put;

	if (!device_is_active(sc->sc_dev))
		return 0;

	if (sc->sc_ih[CLPSCOM_RXINT] != NULL) {
		put = sc->sc_rbput;
		cc = sc->sc_rbavail;
		while (cc > 0) {
			sysflg = CLPSCOM_READ_FLG(sc);
			if (ISSET(sysflg, SYSFLG_URXFE))
				break;
			data = CLPSCOM_READ(sc);
			cn_check_magic(sc->sc_tty->t_dev, data & 0xff,
			    clpscom_cnm_state);

			put[0] = data & 0xff;
			put[1] = (data >> 8) & 0xff;
			put += 2;
			if (put >= sc->sc_rbuf + (CLPSCOM_RING_SIZE << 1))
				put = sc->sc_rbuf;
			cc--;
			sc->sc_rx_ready = 1;
		}

		/*
		 * Current string of incoming characters ended because
		 * no more data was available or we ran out of space.
		 * Schedule a receive event if any data was received.
		 * If we're out of space, turn off receive interrupts.
		 */
		sc->sc_rbput = put;
		sc->sc_rbavail = cc;

		/*
		 * See if we are in danger of overflowing a buffer. If
		 * so, use hardware flow control to ease the pressure.
		 */

		/* but clpscom cannot. X-( */

		/*
		 * If we're out of space, disable receive interrupts
		 * until the queue has drained a bit.
		 */
		if (cc <= 0) {
			intr_disestablish(sc->sc_ih[CLPSCOM_RXINT]);
			sc->sc_ih[CLPSCOM_RXINT] = NULL;
		}
	}

	/* Wake up the poller. */
	softint_schedule(sc->sc_si);

	return 1;
}

static int
clpscom_msintr(void *arg)
{
	struct clpscom_softc *sc = arg;
	uint32_t ms, delta;

	if (!device_is_active(sc->sc_dev))
		return 0;

	if (sc->sc_ih[CLPSCOM_MSINT] != NULL) {
		ms = CLPSCOM_READ_FLG(sc) & CLPSCOM_MODEM_STATUS_MASK;
		delta = ms ^ sc->sc_ms;
		sc->sc_ms = ms;

		if (ISSET(delta, sc->sc_ms_mask)) {
			SET(sc->sc_ms_delta, delta);

			/*
			 * Stop output immediately if we lose the output
			 * flow control signal or carrier detect.
			 */
			if (ISSET(~ms, sc->sc_ms_mask))
				sc->sc_tbc = 0;
			sc->sc_ms_changed = 1;
		}
	}
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, PS711X_UMSEOI, 1);

	/* Wake up the poller. */
	softint_schedule(sc->sc_si);

	return 1;
}

static void
clpscom_soft(void *arg)
{
	struct clpscom_softc *sc = arg;
	struct tty *tp = sc->sc_tty;

	if (!device_is_active(sc->sc_dev))
		return;

	if (sc->sc_rx_ready) {
		sc->sc_rx_ready = 0;
		clpscom_rxsoft(sc, tp);
	}
	if (sc->sc_tx_done) {
		sc->sc_tx_done = 0;
		CLR(tp->t_state, TS_BUSY);
		if (ISSET(tp->t_state, TS_FLUSH))
			CLR(tp->t_state, TS_FLUSH);
		else
			ndflush(&tp->t_outq,
			    (int)(sc->sc_tba - tp->t_outq.c_cf));
		(*tp->t_linesw->l_start)(tp);
	}
	if (sc->sc_ms_changed == 1) {
		sc->sc_ms_changed = 0;
		clpscom_mssoft(sc, tp);
	}
}

static void
clpscom_start(struct tty *tp)
{
	struct clpscom_softc *sc
		= device_lookup_private(&clpscom_cd, COMUNIT(tp->t_dev));
	int s, n;

	if (!device_is_active(sc->sc_dev))
		return;

	s = spltty();
	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
		goto out;
	if (sc->sc_tx_stopped)
		goto out;
	if (!ttypull(tp))
		goto out;

	/* Grab the first contiguous region of buffer space. */
	{
		u_char *tba;
		int tbc;

		tba = tp->t_outq.c_cf;
		tbc = ndqb(&tp->t_outq, 0);

		(void)splserial();

		sc->sc_tba = tba;
		sc->sc_tbc = tbc;
	}

	SET(tp->t_state, TS_BUSY);

	if (sc->sc_ih[CLPSCOM_TXINT] == NULL) {
		sc->sc_ih[CLPSCOM_TXINT] =
		    intr_establish(sc->sc_irq[CLPSCOM_TXINT], IPL_SERIAL, 0,
		    clpscom_txintr, sc);
		if (sc->sc_ih[CLPSCOM_TXINT] == NULL)
			printf("%s: can't establish tx interrupt\n",
			    device_xname(sc->sc_dev));

		/* Output the first chunk of the contiguous buffer. */
		n = uimin(sc->sc_tbc, UART_FIFO_SIZE);
		CLPSCOM_WRITE_MULTI(sc, sc->sc_tba, n);
		sc->sc_tba += n;
		sc->sc_tbc -= n;
	}
out:
	splx(s);
	return;
}

static int
clpscom_param(struct tty *tp, struct termios *t)
{
	struct clpscom_softc *sc =
	    device_lookup_private(&clpscom_cd, COMUNIT(tp->t_dev));
	int s;

	if (!device_is_active(sc->sc_dev))
		return ENXIO;
	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
		return EINVAL;

	/*
	 * For the console, always force CLOCAL and !HUPCL, so that the port
	 * is always active.
	 */
	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
		SET(t->c_cflag, CLOCAL);
		CLR(t->c_cflag, HUPCL);
	}

	/*
	 * If there were no changes, don't do anything.  This avoids dropping
	 * input and improves performance when all we did was frob things like
	 * VMIN and VTIME.
	 */
	if (tp->t_ospeed == t->c_ospeed &&
	    tp->t_cflag == t->c_cflag)
		return 0;

	/*
	 * If we're not in a mode that assumes a connection is present, then
	 * ignore carrier changes.
	 */
	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
		sc->sc_ms_dcd = 0;
	else
		sc->sc_ms_dcd = SYSFLG_DCD;
	/*
	 * Set the flow control pins depending on the current flow control
	 * mode.
	 */
	if (ISSET(t->c_cflag, CRTSCTS)) {
		sc->sc_ms_cts = SYSFLG_CTS;
	} else if (ISSET(t->c_cflag, MDMBUF)) {
		/*
		 * For DTR/DCD flow control, make sure we don't toggle DTR for
		 * carrier detection.
		 */
		sc->sc_ms_cts = SYSFLG_DCD;
	} else {
		/*
		 * If no flow control, then always set RTS.  This will make
		 * the other side happy if it mistakenly thinks we're doing
		 * RTS/CTS flow control.
		 */
		sc->sc_ms_cts = 0;
	}
	sc->sc_ms_mask = sc->sc_ms_cts | sc->sc_ms_dcd;

	s = splserial();
	CLPSCOM_WRITE_UBRLCR(sc,
	    UBRLCR_FIFOEN |
	    clpscom_rate2ubrlcr(t->c_ospeed) |
	    clpscom_cflag2ubrlcr(t->c_cflag));

	/* And copy to tty. */
	tp->t_ispeed = 0;
	tp->t_ospeed = t->c_ospeed;
	tp->t_cflag = t->c_cflag;
	splx(s);

	/*
	 * Update the tty layer's idea of the carrier bit, in case we changed
	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
	 * explicit request.
	 */
	(*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_ms, SYSFLG_DCD));

	if (!ISSET(t->c_cflag, CHWFLOW))
		if (sc->sc_tx_stopped) {
			sc->sc_tx_stopped = 0;
			clpscom_start(tp);
		}

	return 0;
}

static int
clpscom_hwiflow(struct tty *tp, int block)
{
	/* Nothing */
	return 0;
}

/* ARGSUSED */
int
clpscomopen(dev_t dev, int flag, int mode, struct lwp *l)
{
	struct clpscom_softc *sc;
	struct tty *tp;
	int error, s, s2;

	sc = device_lookup_private(&clpscom_cd, COMUNIT(dev));
	if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
		return ENXIO;
	if (!device_is_active(sc->sc_dev))
		return ENXIO;

#ifdef KGDB
	/*
	 * If this is the kgdb port, no other use is permitted.
	 */
	if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
		return EBUSY;
#endif

	tp = sc->sc_tty;

	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
		return EBUSY;

	s = spltty();

	/*
	 * Do the following iff this is a first open.
	 */
	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
		struct termios t;

		tp->t_dev = dev;

		/* Enable and turn on interrupt */
		CLPSCOM_WRITE_CON(sc, CLPSCOM_READ_CON(sc) | SYSCON_UARTEN);

		/* Fetch the current modem control status, needed later. */
		sc->sc_ms = CLPSCOM_READ_FLG(sc) & CLPSCOM_MODEM_STATUS_MASK;

		/*
		 * Initialize the termios status to the defaults.  Add in the
		 * sticky bits from TIOCSFLAGS.
		 */
		t.c_ispeed = 0;
		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
			t.c_ospeed = clpscom_cnrate;
			t.c_cflag = clpscom_cncflag;
		} else {
			t.c_ospeed = TTYDEF_SPEED;
			t.c_cflag = TTYDEF_CFLAG;
		}
		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
			SET(t.c_cflag, CLOCAL);
		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
			SET(t.c_cflag, CRTSCTS);
		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
			SET(t.c_cflag, MDMBUF);
		/* Make sure pscom_param() we do something */
		tp->t_ospeed = 0;
		clpscom_param(tp, &t);
		tp->t_iflag = TTYDEF_IFLAG;
		tp->t_oflag = TTYDEF_OFLAG;
		tp->t_lflag = TTYDEF_LFLAG;
		ttychars(tp);
		ttsetwater(tp);

		s2 = splserial();

		/* Clear the input ring. */
		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
		sc->sc_rbavail = CLPSCOM_RING_SIZE;
		clpscom_iflush(sc);

		splx(s2);
	}

	splx(s);

	error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
	if (error)
		goto bad;

	error = (*tp->t_linesw->l_open)(dev, tp);
	if (error)
		goto bad;
	return 0;

bad:
	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
		/*
		 * We failed to open the device, and nobody else had it opened.
		 * Clean up the state as appropriate.
		 */
		clpscom_shutdown(sc);

		/* Disable UART */
		if (!ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
			CLPSCOM_WRITE_CON(sc,
			    CLPSCOM_READ_CON(sc) & ~SYSCON_UARTEN);
	}

	return error;
}

/* ARGSUSED */
int
clpscomclose(dev_t dev, int flag, int mode, struct lwp *l)
{
	struct clpscom_softc *sc =
	    device_lookup_private(&clpscom_cd, COMUNIT(dev));
	struct tty *tp = sc->sc_tty;

	/* XXXX This is for cons.c. */
	if (!ISSET(tp->t_state, TS_ISOPEN))
		return 0;

	(*tp->t_linesw->l_close)(tp, flag);
	ttyclose(tp);

	if (!device_is_active(sc->sc_dev))
		return 0;

	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
		/*
		 * Although we got a last close, the device may still be in
		 * use; e.g. if this was the dialout node, and there are still
		 * processes waiting for carrier on the non-dialout node.
		 */
		clpscom_shutdown(sc);

		/* Disable UART */
		if (!ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
			CLPSCOM_WRITE_CON(sc,
			    CLPSCOM_READ_CON(sc) & ~SYSCON_UARTEN);
	}

	return 0;
}

int
clpscomread(dev_t dev, struct uio *uio, int flag)
{
	struct clpscom_softc *sc =
	    device_lookup_private(&clpscom_cd, COMUNIT(dev));
	struct tty *tp = sc->sc_tty;

	if (!device_is_active(sc->sc_dev))
		return EIO;

	return (*tp->t_linesw->l_read)(tp, uio, flag);
}

int
clpscomwrite(dev_t dev, struct uio *uio, int flag)
{
	struct clpscom_softc *sc =
	    device_lookup_private(&clpscom_cd, COMUNIT(dev));
	struct tty *tp = sc->sc_tty;

	if (!device_is_active(sc->sc_dev))
		return EIO;

	return (*tp->t_linesw->l_write)(tp, uio, flag);
}

int
clpscomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
	struct clpscom_softc *sc =
	    device_lookup_private(&clpscom_cd, COMUNIT(dev));
	struct tty *tp = sc->sc_tty;
	int error, s;

	if (!device_is_active(sc->sc_dev))
		return EIO;

	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
	if (error != EPASSTHROUGH)
		return error;

	error = ttioctl(tp, cmd, data, flag, l);
	if (error != EPASSTHROUGH)
		return error;

	switch (cmd) {
	case TIOCSFLAGS:
		error = kauth_authorize_device_tty(l->l_cred,
		    KAUTH_DEVICE_TTY_PRIVSET, tp);
		break;
	default:
		break;
	}
	if (error)
		return error;

	s = splserial();
	error = 0;
	switch (cmd) {
	case TIOCSBRK:
		clpscom_break(sc, 1);
		break;

	case TIOCCBRK:
		clpscom_break(sc, 0);
		break;

	case TIOCGFLAGS:
		*(int *)data = sc->sc_swflags;
		break;

	case TIOCSFLAGS:
		sc->sc_swflags = *(int *)data;
		break;

	case TIOCMGET:
		*(int *)data = clpscom_to_tiocm(sc);
		break;

	default:
		error = EPASSTHROUGH;
		break;
	}
	splx(s);
	return error;
}

int
clpscompoll(dev_t dev, int events, struct lwp *l)
{
	struct clpscom_softc *sc =
	    device_lookup_private(&clpscom_cd, COMUNIT(dev));
	struct tty *tp = sc->sc_tty;

	if (!device_is_active(sc->sc_dev))
		return EIO;

	return (*tp->t_linesw->l_poll)(tp, events, l);
}

struct tty *
clpscomtty(dev_t dev)
{
	struct clpscom_softc *sc =
	    device_lookup_private(&clpscom_cd, COMUNIT(dev));

	return sc->sc_tty;
}

void
clpscomstop(struct tty *tp, int flag)
{
	int s;

	s = splserial();
	if (ISSET(tp->t_state, TS_BUSY)) {
		/* Stop transmitting at the next chunk. */
		if (!ISSET(tp->t_state, TS_TTSTOP))
			SET(tp->t_state, TS_FLUSH);
	}
	splx(s);
}


static void
clpscom_iflush(struct clpscom_softc *sc)
{
	int timo;

	timo = 50000;
	while ((CLPSCOM_READ_FLG(sc) & SYSFLG_URXFE) == 0
	    && timo--)
		CLPSCOM_READ(sc);
	if (timo == 0)
		printf("%s: iflush timeout\n", device_xname(sc->sc_dev));
}

static void
clpscom_shutdown(struct clpscom_softc *sc)
{
	int s;

	s = splserial();

	/* Turn off all interrupts */
	if (sc->sc_ih[CLPSCOM_TXINT] != NULL)
		intr_disestablish(sc->sc_ih[CLPSCOM_TXINT]);
	sc->sc_ih[CLPSCOM_TXINT] = NULL;
	if (sc->sc_ih[CLPSCOM_RXINT] != NULL)
		intr_disestablish(sc->sc_ih[CLPSCOM_RXINT]);
	sc->sc_ih[CLPSCOM_RXINT] = NULL;
	if (sc->sc_ih[CLPSCOM_MSINT] != NULL)
		intr_disestablish(sc->sc_ih[CLPSCOM_MSINT]);
	sc->sc_ih[CLPSCOM_MSINT] = NULL;

	/* Clear any break condition set with TIOCSBRK. */
	clpscom_break(sc, 0);

	splx(s);
}

static void
clpscom_break(struct clpscom_softc *sc, int onoff)
{
	int s;
	uint8_t ubrlcr;

	s = splserial();
	ubrlcr = CLPSCOM_READ_UBRLCR(sc);
	if (onoff)
		SET(ubrlcr, UBRLCR_BREAK);
	else
		CLR(ubrlcr, UBRLCR_BREAK);
	CLPSCOM_WRITE_UBRLCR(sc, ubrlcr);
	splx(s);
}

static int
clpscom_to_tiocm(struct clpscom_softc *sc)
{
	uint32_t combits;
	int ttybits = 0;

	combits = sc->sc_ms;
	if (ISSET(combits, SYSFLG_DCD))
		SET(ttybits, TIOCM_CD);
	if (ISSET(combits, SYSFLG_CTS))
		SET(ttybits, TIOCM_CTS);
	if (ISSET(combits, SYSFLG_DSR))
		SET(ttybits, TIOCM_DSR);

	return ttybits;
}

static void
clpscom_rxsoft(struct clpscom_softc *sc, struct tty *tp)
{
	int code, s;
	u_int cc, scc;
	u_char sts, *get;

	get = sc->sc_rbget;
	scc = cc = CLPSCOM_RING_SIZE - sc->sc_rbavail;
	while (cc) {
		code = get[0];
		sts = get[1];
		if (ISSET(sts, UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR)) {
			if (ISSET(sts, (UARTDR_FRMERR)))
				SET(code, TTY_FE);
			if (ISSET(sts, UARTDR_PARERR))
				SET(code, TTY_PE);
			if (ISSET(sts, UARTDR_OVERR))
				;		/* XXXXX: Overrun */
		}
		if ((*tp->t_linesw->l_rint)(code, tp) == -1) {
			/*
			 * The line discipline's buffer is out of space.
			 */
			/*
			 * We're either not using flow control, or the
			 * line discipline didn't tell us to block for
			 * some reason.  Either way, we have no way to
			 * know when there's more space available, so
			 * just drop the rest of the data.
			 */
			get += cc << 1;
			if (get >= sc->sc_rbuf + (CLPSCOM_RING_SIZE << 1))
				get -= (CLPSCOM_RING_SIZE << 1);
			cc = 0;
			break;
		}
		get += 2;
		if (get >= sc->sc_rbuf + (CLPSCOM_RING_SIZE << 1))
			get = sc->sc_rbuf;
		cc--;
	}

	if (cc != scc) {
		sc->sc_rbget = get;
		s = splserial();
		
		cc = sc->sc_rbavail += scc - cc;
		/* Buffers should be ok again, release possible block. */
		if (cc >= 1) {
			if (sc->sc_ih[CLPSCOM_RXINT] == NULL) {
				sc->sc_ih[CLPSCOM_RXINT] =
				    intr_establish(sc->sc_irq[CLPSCOM_RXINT],
				    IPL_SERIAL, 0, clpscom_rxintr, sc);
				if (sc->sc_ih[CLPSCOM_RXINT] == NULL)
					printf("%s: can't establish"
					    " rx interrupt\n",
					    device_xname(sc->sc_dev));
			}
			if (sc->sc_ih[CLPSCOM_MSINT] == NULL) {
				sc->sc_ih[CLPSCOM_MSINT] =
				    intr_establish(sc->sc_irq[CLPSCOM_MSINT],
				    IPL_SERIAL, 0, clpscom_msintr, sc);
				if (sc->sc_ih[CLPSCOM_MSINT] == NULL)
					printf("%s: can't establish"
					    " ms interrupt\n",
					    device_xname(sc->sc_dev));
			}
		}
		splx(s);
	}
}

static void
clpscom_mssoft(struct clpscom_softc *sc, struct tty *tp)
{
	uint32_t ms, delta;

	ms = sc->sc_ms;
	delta = sc->sc_ms_delta;
	sc->sc_ms_delta = 0;

	if (ISSET(delta, sc->sc_ms_dcd))
		/*
		 * Inform the tty layer that carrier detect changed.
		 */
		(void) (*tp->t_linesw->l_modem)(tp, ISSET(ms, SYSFLG_DCD));

	if (ISSET(delta, sc->sc_ms_cts)) {
		/* Block or unblock output according to flow control. */
		if (ISSET(ms, sc->sc_ms_cts)) {
			sc->sc_tx_stopped = 0;
			(*tp->t_linesw->l_start)(tp);
		} else
			sc->sc_tx_stopped = 1;
	}
}

static inline uint32_t
clpscom_rate2ubrlcr(int rate)
{

	return 230400 / rate - 1;
}

static uint32_t
clpscom_cflag2ubrlcr(tcflag_t cflag)
{
	int32_t ubrlcr = 0;

	switch (cflag & CSIZE) {
	case CS5: SET(ubrlcr, UBRLCR_WRDLEN_5B); break;
	case CS6: SET(ubrlcr, UBRLCR_WRDLEN_6B); break;
	case CS7: SET(ubrlcr, UBRLCR_WRDLEN_7B); break;
	case CS8: SET(ubrlcr, UBRLCR_WRDLEN_8B); break;
	default:  SET(ubrlcr, UBRLCR_WRDLEN_8B); break;
	}
	if (cflag & CSTOPB)
		SET(ubrlcr, UBRLCR_XSTOP);
	if (cflag & PARENB) {
		SET(ubrlcr, (UBRLCR_PRTEN | UBRLCR_EVENPRT));
		if (cflag & PARODD)
			CLR(ubrlcr, UBRLCR_EVENPRT);
	}
	return ubrlcr;
}

#define CLPSCOM_CNREAD() \
	(*(volatile uint32_t *)(clpscom_cnaddr + PS711X_UARTDR))
#define CLPSCOM_CNWRITE(val) \
	(*(volatile uint8_t *)(clpscom_cnaddr + PS711X_UARTDR) = val)
#define CLPSCOM_CNSTATUS() \
	(*(volatile uint32_t *)(clpscom_cnaddr + PS711X_SYSFLG))

static struct consdev clpscomcons = {
	NULL, NULL, clpscom_cngetc, clpscom_cnputc, clpscom_cnpollc,
	NULL, NULL, NULL, NODEV, CN_NORMAL
};

int
clpscom_cnattach(vaddr_t addr, int rate, tcflag_t cflag)
{

	clpscom_cnaddr = addr;
	clpscom_cnrate = rate;
	clpscom_cncflag = cflag;
	*(volatile uint32_t *)(clpscom_cnaddr + PS711X_SYSFLG) |= SYSCON_UARTEN;
	*(volatile uint32_t *)(clpscom_cnaddr + PS711X_UBRLCR) =
	    UBRLCR_FIFOEN |
	    clpscom_cflag2ubrlcr(cflag) |
	    clpscom_rate2ubrlcr(rate);

	cn_tab = &clpscomcons;
	cn_init_magic(&clpscom_cnm_state);
	cn_set_magic("\047\001");	/* default magic is BREAK */

	return 0;
}

/* ARGSUSED */
static int
clpscom_cngetc(dev_t dev)
{
	int s = splserial();
	char ch;

	while (CLPSCOM_CNSTATUS() & SYSFLG_URXFE);

	ch = CLPSCOM_CNREAD();

	if (!db_active)
		cn_check_magic(dev, ch, clpscom_cnm_state);

	splx(s);
	return ch;
}

/* ARGSUSED */
static void
clpscom_cnputc(dev_t dev, int c)
{
	int s = splserial();

	while (CLPSCOM_CNSTATUS() & SYSFLG_UTXFF);

	CLPSCOM_CNWRITE(c);

	/* Make sure output. */
	while (CLPSCOM_CNSTATUS() & SYSFLG_UBUSY);

	splx(s);
}

/* ARGSUSED */
static void
clpscom_cnpollc(dev_t dev, int on)
{
	/* Nothing */
}