Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
/*	$NetBSD: ser.c,v 1.56 2014/11/15 19:20:01 christos Exp $	*/

/*-
 * Copyright (c) 1997 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Leo Weppelman.
 *
 * 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``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 FOUNDATION OR CONTRIBUTORS
 * 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.
 */
/*-
 * Copyright (c) 1993, 1994, 1995, 1996, 1997
 *      Charles M. Hannum.  All rights reserved.
 *
 * Interrupt processing and hardware flow control partly based on code from
 * Onno van der Linden and Gordon Ross.
 *
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Charles M. Hannum.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * 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.
 */
 
/*
 * Copyright (c) 1991 The Regents of the University of California.
 * 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.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
 *    
 *      @(#)com.c       7.5 (Berkeley) 5/16/91
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ser.c,v 1.56 2014/11/15 19:20:01 christos Exp $");

#include "opt_ddb.h"
#include "opt_mbtype.h"
#include "opt_serconsole.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/tty.h>
#include <sys/proc.h>
#include <sys/conf.h>
#include <sys/file.h>
#include <sys/uio.h>
#include <sys/kernel.h>
#include <sys/syslog.h>
#include <sys/types.h>
#include <sys/device.h>
#include <sys/kauth.h>

#include <m68k/asm_single.h>

#include <machine/iomap.h>
#include <machine/mfp.h>
#include <machine/intr.h>
#include <atari/dev/serreg.h>

#include "ioconf.h"

#if !defined(_MILANHW_)
#include <atari/dev/ym2149reg.h>
#else
	/* MILAN has no ym2149 */
#define ym2149_dtr(set) {					\
	if (set)						\
		single_inst_bset_b(MFP->mf_gpip, 0x08);		\
	else							\
		single_inst_bclr_b(MFP->mf_gpip, 0x08);		\
}

#define ym2149_rts(set) {					\
	if (set)						\
		single_inst_bset_b(MFP->mf_gpip, 0x01);		\
	else							\
		single_inst_bclr_b(MFP->mf_gpip, 0x01);		\
}
#endif /* _MILANHW_ */

/* #define SER_DEBUG */

#define	SERUNIT(x)	TTUNIT(x)
#define	SERDIALOUT(x)	TTDIALOUT(x)

/* XXX */
#define	CONSBAUD	9600
#define	CONSCFLAG	TTYDEF_CFLAG
/* end XXX */

/* Buffer size for character buffer */
#define RXBUFSIZE 2048		/* More than enough..			*/
#define RXBUFMASK (RXBUFSIZE-1)	/* Only iff previous is a power of 2	*/
#define RXHIWAT   (RXBUFSIZE >> 2)

struct ser_softc {
	device_t	 sc_dev;
	struct tty	*sc_tty;

	struct callout sc_diag_ch;

	int		 sc_overflows;
	int		 sc_floods;
	int		 sc_errors;

	uint8_t		 sc_hwflags;
	uint8_t		 sc_swflags;

	int		 sc_ospeed;		/* delay + timer-d data	*/
	uint8_t		 sc_imra;
	uint8_t		 sc_imrb;
	uint8_t		 sc_ucr;		/* Uart control		*/
	uint8_t		 sc_msr;		/* Modem status		*/
	uint8_t		 sc_tsr;		/* Tranceiver status	*/
	uint8_t		 sc_rsr;		/* Receiver status	*/
	uint8_t		 sc_mcr;		/* (Pseudo) Modem ctrl. */

	uint8_t		 sc_msr_delta;
	uint8_t		 sc_msr_mask;
	uint8_t		 sc_mcr_active;
	uint8_t		 sc_mcr_dtr, sc_mcr_rts, sc_msr_cts, sc_msr_dcd;

	int		 sc_r_hiwat;
 	volatile u_int	 sc_rbget;
 	volatile u_int	 sc_rbput;
	volatile u_int	 sc_rbavail;
 	uint8_t		 sc_rbuf[RXBUFSIZE];
	uint8_t		 sc_lbuf[RXBUFSIZE];

	volatile uint8_t sc_rx_blocked;
	volatile uint8_t sc_rx_ready;
	volatile uint8_t sc_tx_busy;
	volatile uint8_t sc_tx_done;
	volatile uint8_t sc_tx_stopped;
	volatile uint8_t sc_st_check;

 	uint8_t		*sc_tba;
 	int		 sc_tbc;
	int		 sc_heldtbc;

	volatile uint8_t sc_heldchange;

	void		*sc_sicookie;
};

/*
 * For sc_hwflags:
 */
#define	SER_HW_CONSOLE	0x01

static void	ser_break(struct ser_softc *, int);
static void	ser_hwiflow(struct ser_softc *, int);
static void	ser_iflush(struct ser_softc *);
static void	ser_loadchannelregs(struct ser_softc *);
static void	ser_modem(struct ser_softc *, int);
static void	serdiag(void *);
static int	serhwiflow(struct tty *, int);
static void	serinit(int);
static void	serinitcons(int);
static int	sermintr(void *);
static int	sertrintr(void *);
static int	serparam(struct tty *, struct termios *);
static void	serstart(struct tty *);

struct consdev;
void	sercnprobe(struct consdev *);
void	sercninit(struct consdev *);
int	sercngetc(dev_t);
void	sercnputc(dev_t, int);
void	sercnpollc(dev_t, int);

static void sermsrint(struct ser_softc *, struct tty*);
static void serrxint(struct ser_softc *, struct tty*);
static void ser_shutdown(struct ser_softc *);
static int serspeed(long);
static void sersoft(void *);
static void sertxint(struct ser_softc *, struct tty*);

/*
 * Autoconfig stuff
 */
static int  sermatch(device_t, cfdata_t, void *);
static void serattach(device_t, device_t, void *);

CFATTACH_DECL_NEW(ser, sizeof(struct ser_softc),
    sermatch, serattach, NULL, NULL);

dev_type_open(seropen);
dev_type_close(serclose);
dev_type_read(serread);
dev_type_write(serwrite);
dev_type_ioctl(serioctl);
dev_type_stop(serstop);
dev_type_tty(sertty);
dev_type_poll(serpoll);

const struct cdevsw ser_cdevsw = {
	.d_open = seropen,
	.d_close = serclose,
	.d_read = serread,
	.d_write = serwrite,
	.d_ioctl = serioctl,
	.d_stop = serstop,
	.d_tty = sertty,
	.d_poll = serpoll,
	.d_mmap = nommap,
	.d_kqfilter = ttykqfilter,
	.d_discard = nodiscard,
	.d_flag = D_TTY
};

#ifndef SERCONSOLE
#define SERCONSOLE	0
#endif
int serconsole = SERCONSOLE;	/* patchable */

/*ARGSUSED*/
static	int
sermatch(device_t parent, cfdata_t cf, void *aux)
{
	static int ser_matched = 0;

	/* Match at most one ser unit */
	if (strcmp((char *)aux, "ser") || ser_matched)
		return 0;

	ser_matched = 1;
	return 1;
}

/*ARGSUSED*/
static void
serattach(device_t parent, device_t self, void *aux)
{
	struct ser_softc *sc = device_private(self);

	sc->sc_dev = self;

	if (intr_establish(1, USER_VEC, 0, (hw_ifun_t)sermintr, sc) == NULL)
		aprint_error(": Can't establish interrupt (1)\n");
	if (intr_establish(2, USER_VEC, 0, (hw_ifun_t)sermintr, sc) == NULL)
		aprint_error(": Can't establish interrupt (2)\n");
	if (intr_establish(14, USER_VEC, 0, (hw_ifun_t)sermintr, sc) == NULL)
		aprint_error(": Can't establish interrupt (14)\n");
	if (intr_establish(9, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
		aprint_error(": Can't establish interrupt (9)\n");
	if (intr_establish(10, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
		aprint_error(": Can't establish interrupt (10)\n");
	if (intr_establish(11, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
		aprint_error(": Can't establish interrupt (11)\n");
	if (intr_establish(12, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
		aprint_error(": Can't establish interrupt (12)\n");

	sc->sc_sicookie = softint_establish(SOFTINT_SERIAL, sersoft, sc);

	ym2149_rts(1);
	ym2149_dtr(1);

	/*
	 * Enable but mask interrupts...
	 * XXX: Look at edge-sensitivity for DCD/CTS interrupts.
	 */
	MFP->mf_ierb |= IB_SCTS|IB_SDCD;
	MFP->mf_iera |= IA_RRDY|IA_RERR|IA_TRDY|IA_TERR;
	MFP->mf_imrb &= ~(IB_SCTS|IB_SDCD);
	MFP->mf_imra &= ~(IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);

	callout_init(&sc->sc_diag_ch, 0);

	if (serconsole) {
		/*
		 * Activate serial console when DCD present...
		 */
		if ((MFP->mf_gpip & MCR_DCD) == 0)
			SET(sc->sc_hwflags, SER_HW_CONSOLE);
	}

	aprint_normal(": modem1 on 68901 MFP1 USART\n");
	if (ISSET(sc->sc_hwflags, SER_HW_CONSOLE)) {
		serinit(CONSBAUD);
		aprint_normal_dev(self, "console\n");
	}
}

#ifdef SER_DEBUG
void serstatus(struct ser_softc *, char *);
void
serstatus(struct ser_softc *sc, char *str)
{
	struct tty *tp = sc->sc_tty;

	printf("%s: %s %sclocal  %sdcd %sts_carr_on %sdtr %stx_stopped\n",
	    device_xname(sc->sc_dev), str,
	    ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
	    ISSET(sc->sc_msr, MCR_DCD) ? "+" : "-",
	    ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
	    ISSET(sc->sc_mcr, MCR_DTR) ? "+" : "-",
	    sc->sc_tx_stopped ? "+" : "-");

	printf("%s: %s %scrtscts %scts %sts_ttstop  %srts %srx_blocked\n",
	    device_xname(sc->sc_dev), str,
	    ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
	    ISSET(sc->sc_msr, MCR_CTS) ? "+" : "-",
	    ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
	    ISSET(sc->sc_mcr, MCR_RTS) ? "+" : "-",
	    sc->sc_rx_blocked ? "+" : "-");
}
#endif /* SER_DEBUG */

int
seropen(dev_t dev, int flag, int mode, struct lwp *l)
{
	int unit = SERUNIT(dev);
	struct ser_softc *sc;
	struct tty *tp;
	int s, s2;
	int error = 0;
 
	sc = device_lookup_private(&ser_cd, unit);
	if (sc == NULL)
		return ENXIO;

	if (!sc->sc_tty) {
		tp = sc->sc_tty = tty_alloc();
		tty_attach(tp);
	} else
		tp = sc->sc_tty;

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

	s = spltty();

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

		/* Turn on interrupts. */
		sc->sc_imra = IA_RRDY|IA_RERR|IA_TRDY|IA_TERR;
		sc->sc_imrb = IB_SCTS|IB_SDCD;
		single_inst_bset_b(MFP->mf_imra, sc->sc_imra);
		single_inst_bset_b(MFP->mf_imrb, sc->sc_imrb);

		/* Fetch the current modem control status, needed later. */
		sc->sc_msr = ~MFP->mf_gpip & (IO_SDCD|IO_SCTS|IO_SRI);

		/* Add some entry points needed by the tty layer. */
		tp->t_oproc = serstart;
		tp->t_param = serparam;
		tp->t_hwiflow = serhwiflow;
		tp->t_dev = dev;

		/*
		 * Initialize the termios status to the defaults.  Add in the
		 * sticky bits from TIOCSFLAGS.
		 */
		t.c_ispeed = 0;
		if (ISSET(sc->sc_hwflags, SER_HW_CONSOLE)) {
			t.c_ospeed = CONSBAUD;
			t.c_cflag  = CONSCFLAG;
		} 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);
		tp->t_iflag = TTYDEF_IFLAG;
		tp->t_oflag = TTYDEF_OFLAG;
		tp->t_lflag = TTYDEF_LFLAG;
		ttychars(tp);
		(void)serparam(tp, &t);
		ttsetwater(tp);

		s2 = splhigh();

		/*
		 * Turn on DTR.  We must always do this, even if carrier is not
		 * present, because otherwise we'd have to use TIOCSDTR
		 * immediately after setting CLOCAL.  We will drop DTR only on
		 * the next high-low transition of DCD, or by explicit request.
		 */
		ser_modem(sc, 1);

		/* Clear the input ring, and unblock. */
		sc->sc_rbput = sc->sc_rbget = 0;
		sc->sc_rbavail = RXBUFSIZE;
		ser_iflush(sc);
		sc->sc_rx_blocked = 0;
		ser_hwiflow(sc, 0);

#ifdef SER_DEBUG
		serstatus(sc, "seropen  ");
#endif

		splx(s2);
	}

	splx(s);

	error = ttyopen(tp, SERDIALOUT(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.
		 */
		ser_shutdown(sc);
	}

	return error;
}
 
int
serclose(dev_t dev, int flag, int mode, struct lwp *l)
{
	int unit = SERUNIT(dev);
	struct ser_softc *sc = device_lookup_private(&ser_cd, unit);
	struct tty *tp = sc->sc_tty;

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

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

	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.
		 */
		ser_shutdown(sc);
	}

	return 0;
}

int
serread(dev_t dev, struct uio *uio, int flag)
{
	struct ser_softc *sc = device_lookup_private(&ser_cd, SERUNIT(dev));
	struct tty *tp = sc->sc_tty;
 
	return (*tp->t_linesw->l_read)(tp, uio, flag);
}
 
int
serwrite(dev_t dev, struct uio *uio, int flag)
{
	struct ser_softc *sc = device_lookup_private(&ser_cd, SERUNIT(dev));
	struct tty *tp = sc->sc_tty;
 
	return (*tp->t_linesw->l_write)(tp, uio, flag);
}

int
serpoll(dev_t dev, int events, struct lwp *l)
{
	struct ser_softc *sc = device_lookup_private(&ser_cd, SERUNIT(dev));
	struct tty *tp = sc->sc_tty;
 
	return (*tp->t_linesw->l_poll)(tp, events, l);
}

struct tty *
sertty(dev_t dev)
{
	struct ser_softc *sc = device_lookup_private(&ser_cd, SERUNIT(dev));
	struct tty *tp = sc->sc_tty;

	return tp;
}

int
serioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
	int unit = SERUNIT(dev);
	struct ser_softc *sc = device_lookup_private(&ser_cd, unit);
	struct tty *tp = sc->sc_tty;
	int error;

	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 TIOCSBRK:
		ser_break(sc, 1);
		break;

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

	case TIOCSDTR:
		ser_modem(sc, 1);
		break;

	case TIOCCDTR:
		ser_modem(sc, 0);
		break;

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

	case TIOCSFLAGS:
		error = kauth_authorize_device_tty(l->l_cred,
		    KAUTH_DEVICE_TTY_PRIVSET, tp); 
		if (error)
			return error; 
		sc->sc_swflags = *(int *)data;
		break;

	case TIOCMSET:
	case TIOCMBIS:
	case TIOCMBIC:
	case TIOCMGET:
	default:
		return EPASSTHROUGH;
	}

#ifdef SER_DEBUG
	serstatus(sc, "serioctl ");
#endif

	return 0;
}

void
ser_break(struct ser_softc *sc, int onoff)
{
	int s;

	s = splhigh();
	if (onoff)
		SET(sc->sc_tsr, TSR_SBREAK);
	else
		CLR(sc->sc_tsr, TSR_SBREAK);

	if (!sc->sc_heldchange) {
		if (sc->sc_tx_busy) {
			sc->sc_heldtbc = sc->sc_tbc;
			sc->sc_tbc = 0;
			sc->sc_heldchange = 1;
		} else
			ser_loadchannelregs(sc);
	}
	splx(s);
}

void
ser_modem(struct ser_softc *sc, int onoff)
{
	int s;

	s = splhigh();
	if (onoff)
		SET(sc->sc_mcr, sc->sc_mcr_dtr);
	else
		CLR(sc->sc_mcr, sc->sc_mcr_dtr);

	if (!sc->sc_heldchange) {
		if (sc->sc_tx_busy) {
			sc->sc_heldtbc = sc->sc_tbc;
			sc->sc_tbc = 0;
			sc->sc_heldchange = 1;
		} else
			ser_loadchannelregs(sc);
	}
	splx(s);
}

int
serparam(struct tty *tp, struct termios *t)
{
	struct ser_softc *sc =
	    device_lookup_private(&ser_cd, SERUNIT(tp->t_dev));
	int ospeed = serspeed(t->c_ospeed);
	uint8_t ucr;
	int s;


	/* check requested parameters */
	if (ospeed < 0)
		return EINVAL;
	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
		return EINVAL;

	sc->sc_rsr = RSR_ENAB;
	sc->sc_tsr = TSR_ENAB;

	ucr = UCR_CLKDIV;

	switch (ISSET(t->c_cflag, CSIZE)) {
	case CS5:
		SET(ucr, UCR_5BITS);
		break;
	case CS6:
		SET(ucr, UCR_6BITS);
		break;
	case CS7:
		SET(ucr, UCR_7BITS);
		break;
	case CS8:
		SET(ucr, UCR_8BITS);
		break;
	}
	if (ISSET(t->c_cflag, PARENB)) {
		SET(ucr, UCR_PENAB);
		if (!ISSET(t->c_cflag, PARODD))
			SET(ucr, UCR_PEVEN);
	}
	if (ISSET(t->c_cflag, CSTOPB))
		SET(ucr, UCR_STOPB2);
	else
		SET(ucr, UCR_STOPB1);

	s = splhigh();

	sc->sc_ucr = ucr;

	/*
	 * 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, SER_HW_CONSOLE)) {
		SET(t->c_cflag, CLOCAL);
		CLR(t->c_cflag, HUPCL);
	}

	/*
	 * 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_msr_dcd = 0;
	else
		sc->sc_msr_dcd = MCR_DCD;
	/*
	 * Set the flow control pins depending on the current flow control
	 * mode.
	 */
	if (ISSET(t->c_cflag, CRTSCTS)) {
		sc->sc_mcr_dtr = MCR_DTR;
		sc->sc_mcr_rts = MCR_RTS;
		sc->sc_msr_cts = MCR_CTS;
		sc->sc_r_hiwat = RXHIWAT;
	} else if (ISSET(t->c_cflag, MDMBUF)) {
		/*
		 * For DTR/DCD flow control, make sure we don't toggle DTR for
		 * carrier detection.
		 */
		sc->sc_mcr_dtr = 0;
		sc->sc_mcr_rts = MCR_DTR;
		sc->sc_msr_cts = MCR_DCD;
		sc->sc_r_hiwat = RXHIWAT;
	} 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_mcr_dtr = MCR_DTR | MCR_RTS;
		sc->sc_mcr_rts = 0;
		sc->sc_msr_cts = 0;
		sc->sc_r_hiwat = 0;
		if (ISSET(sc->sc_mcr, MCR_DTR))
			SET(sc->sc_mcr, MCR_RTS);
		else
			CLR(sc->sc_mcr, MCR_RTS);
	}
	sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;

#if 0
	if (ospeed == 0)
		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
	else
		SET(sc->sc_mcr, sc->sc_mcr_dtr);
#endif

	sc->sc_ospeed = ospeed;
	
	/* and copy to tty */
	tp->t_ispeed = 0;
	tp->t_ospeed = t->c_ospeed;
	tp->t_cflag = t->c_cflag;

	if (!sc->sc_heldchange) {
		if (sc->sc_tx_busy) {
			sc->sc_heldtbc = sc->sc_tbc;
			sc->sc_tbc = 0;
			sc->sc_heldchange = 1;
		} else
			ser_loadchannelregs(sc);
	}

	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 if we
	 * lose carrier while carrier detection is on.
	 */
	(void)(*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, MCR_DCD));

#ifdef SER_DEBUG
	serstatus(sc, "serparam ");
#endif

	/* XXXXX FIX ME */
	/* Block or unblock as needed. */
	if (!ISSET(t->c_cflag, CHWFLOW)) {
		if (sc->sc_rx_blocked) {
			sc->sc_rx_blocked = 0;
			ser_hwiflow(sc, 0);
		}
		if (sc->sc_tx_stopped) {
			sc->sc_tx_stopped = 0;
			serstart(tp);
		}
	} else {
#if 0
		sermsrint(sc, tp);
#endif
	}

	return 0;
}

void
ser_iflush(struct ser_softc *sc)
{
	uint8_t tmp;

	/* flush any pending I/O */
	while (ISSET(MFP->mf_rsr, RSR_CIP|RSR_BFULL))
		tmp = MFP->mf_udr;
	__USE(tmp);
}

void
ser_loadchannelregs(struct ser_softc *sc)
{

	/* XXXXX necessary? */
	ser_iflush(sc);

	/*
	 * No interrupts please...
	 */
	if ((MFP->mf_imra & (IA_RRDY|IA_RERR|IA_TRDY|IA_TERR)) != sc->sc_imra) {
		printf("loadchannelregs: mf_imra: %x sc_imra: %x\n",
		    (u_int)MFP->mf_imra, (u_int)sc->sc_imra);
	}
	if ((MFP->mf_imrb & (IB_SCTS|IB_SDCD)) != sc->sc_imrb) {
		printf("loadchannelregs: mf_imrb: %x sc_imrb: %x\n",
		     (u_int)MFP->mf_imrb, (u_int)sc->sc_imrb);
	}
	single_inst_bclr_b(MFP->mf_imra, IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
	single_inst_bclr_b(MFP->mf_imrb, IB_SCTS|IB_SDCD);

	MFP->mf_ucr = sc->sc_ucr;
	MFP->mf_rsr = sc->sc_rsr;
	MFP->mf_tsr = sc->sc_tsr;

	single_inst_bclr_b(MFP->mf_tcdcr, 0x07);
	MFP->mf_tddr  = sc->sc_ospeed;
	single_inst_bset_b(MFP->mf_tcdcr, (sc->sc_ospeed >> 8) & 0x0f);

	sc->sc_mcr_active = sc->sc_mcr;

	if (machineid & ATARI_HADES) {
		/* PCB fault, wires exchanged..... */
		ym2149_rts(!(sc->sc_mcr_active & MCR_DTR));
		ym2149_dtr(!(sc->sc_mcr_active & MCR_RTS));
	} else {
		ym2149_rts(!(sc->sc_mcr_active & MCR_RTS));
		ym2149_dtr(!(sc->sc_mcr_active & MCR_DTR));
	}

	single_inst_bset_b(MFP->mf_imra, sc->sc_imra);
	single_inst_bset_b(MFP->mf_imrb, sc->sc_imrb);
}

int
serhwiflow(struct tty *tp, int block)
{
	struct ser_softc *sc =
	    device_lookup_private(&ser_cd, SERUNIT(tp->t_dev));
	int s;

	if (sc->sc_mcr_rts == 0)
		return 0;

	s = splhigh();
	if (block) {
		/*
		 * The tty layer is asking us to block input.
		 * If we already did it, just return TRUE.
		 */
		if (sc->sc_rx_blocked)
			goto out;
		sc->sc_rx_blocked = 1;
	} else {
		/*
		 * The tty layer is asking us to resume input.
		 * The input ring is always empty by now.
		 */
		sc->sc_rx_blocked = 0;
	}
	ser_hwiflow(sc, block);
out:
	splx(s);
	return 1;
}

/*
 * (un)block input via hw flowcontrol
 */
void
ser_hwiflow(struct ser_softc *sc, int block)
{

	if (sc->sc_mcr_rts == 0)
		return;

	if (block) {
		CLR(sc->sc_mcr, sc->sc_mcr_rts);
		CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
	} else {
		SET(sc->sc_mcr, sc->sc_mcr_rts);
		SET(sc->sc_mcr_active, sc->sc_mcr_rts);
	}
	if (machineid & ATARI_HADES) {
		/* PCB fault, wires exchanged..... */
		ym2149_dtr(sc->sc_mcr_active & MCR_RTS);
	}
	else {
		ym2149_rts(sc->sc_mcr_active & MCR_RTS);
	}
}

void
serstart(struct tty *tp)
{
	struct ser_softc *sc =
	    device_lookup_private(&ser_cd, SERUNIT(tp->t_dev));
	int s;

	s = spltty();
	if (ISSET(tp->t_state, TS_BUSY))
		goto out;
	if (ISSET(tp->t_state, TS_TIMEOUT | TS_TTSTOP))
		goto stopped;

	if (sc->sc_tx_stopped)
		goto stopped;
	if (!ttypull(tp))
		goto stopped;

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

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

		(void)splhigh();

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

	SET(tp->t_state, TS_BUSY);
	sc->sc_tx_busy = 1;

	/* Enable transmit completion interrupts if necessary. */
	if (!ISSET(sc->sc_imra, IA_TRDY)) {
		SET(sc->sc_imra, IA_TRDY|IA_TERR);
		single_inst_bset_b(MFP->mf_imra, IA_TRDY|IA_TERR);
	}

	/* Output the first char */
	MFP->mf_udr = *sc->sc_tba;
	sc->sc_tbc--;
	sc->sc_tba++;

	splx(s);
	return;

stopped:
	/* Disable transmit completion interrupts if necessary. */
	if (ISSET(sc->sc_imra, IA_TRDY)) {
		CLR(sc->sc_imra, IA_TRDY|IA_TERR);
		single_inst_bclr_b(MFP->mf_imra, IA_TRDY|IA_TERR);
	}
out:
	splx(s);
	return;
}

/*
 * Stop output on a line.
 */
void
serstop(struct tty *tp, int flag)
{
	struct ser_softc *sc =
	    device_lookup_private(&ser_cd, SERUNIT(tp->t_dev));
	int s;

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

void
serdiag(void *arg)
{
	struct ser_softc *sc = arg;
	int overflows, floods;
	int s;

	s = splhigh();
	overflows = sc->sc_overflows;
	sc->sc_overflows = 0;
	floods = sc->sc_floods;
	sc->sc_floods = 0;
	sc->sc_errors = 0;
	splx(s);

	log(LOG_WARNING,
	    "%s: %d silo overflow%s, %d ibuf flood%s\n",
	    device_xname(sc->sc_dev),
	    overflows, overflows == 1 ? "" : "s",
	    floods, floods == 1 ? "" : "s");
}

static void
ser_shutdown(struct ser_softc *sc)
{
	int s;
	struct tty *tp = sc->sc_tty;


	s = splhigh();
	
	/* If we were asserting flow control, then deassert it. */
	sc->sc_rx_blocked = 1;
	ser_hwiflow(sc, 1);

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

	/*
	 * Hang up if necessary.  Wait a bit, so the other side has time to
	 * notice even if we immediately open the port again.
	 */
	if (ISSET(tp->t_cflag, HUPCL)) {
		ser_modem(sc, 0);
		(void)tsleep(sc, TTIPRI, ttclos, hz);
	}

	/* Turn off interrupts. */
	CLR(sc->sc_imra, IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
	CLR(sc->sc_imrb, IB_SCTS|IB_SDCD);
	single_inst_bclr_b(MFP->mf_imrb, IB_SCTS|IB_SDCD);
	single_inst_bclr_b(MFP->mf_imra, IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
	splx(s);
}

static void
serrxint(struct ser_softc *sc, struct tty *tp)
{
	u_int get, cc, scc;
	int code;
	uint8_t rsr;
	int s;
	static const int lsrmap[8] = {
		0,      TTY_PE,
		TTY_FE, TTY_PE|TTY_FE,
		TTY_FE, TTY_PE|TTY_FE,
		TTY_FE, TTY_PE|TTY_FE
	};

	get = sc->sc_rbget;
	scc = cc = RXBUFSIZE - sc->sc_rbavail;

	if (cc == RXBUFSIZE) {
		sc->sc_floods++;
		if (sc->sc_errors++ == 0)
			callout_reset(&sc->sc_diag_ch, 60 * hz, serdiag, sc);
	}

	while (cc--) {
		rsr = sc->sc_lbuf[get];
		if (ISSET(rsr, RSR_BREAK)) {
#ifdef DDB
			if (ISSET(sc->sc_hwflags, SER_HW_CONSOLE))
				Debugger();
#endif
		} else if (ISSET(rsr, RSR_OERR)) {
			sc->sc_overflows++;
			if (sc->sc_errors++ == 0)
				callout_reset(&sc->sc_diag_ch, 60 * hz,
				    serdiag, sc);
		}
		code = sc->sc_rbuf[get] |
		    lsrmap[(rsr & (RSR_BREAK|RSR_FERR|RSR_PERR)) >> 3];
		(*tp->t_linesw->l_rint)(code, tp);
		get = (get + 1) & RXBUFMASK;
	}

	sc->sc_rbget = get;
	s = splhigh();
	sc->sc_rbavail += scc;
	/*
	 * Buffers should be ok again, release possible block, but only if the
	 * tty layer isn't blocking too.
	 */
	if (sc->sc_rx_blocked && !ISSET(tp->t_state, TS_TBLOCK)) {
		sc->sc_rx_blocked = 0;
		ser_hwiflow(sc, 0);
	}
	splx(s);
}

static void
sertxint(struct ser_softc *sc, struct tty *tp)
{

	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);
}

static void
sermsrint(struct ser_softc *sc, struct tty *tp)
{
	uint8_t msr, delta;
	int s;

	s = splhigh();
	msr = sc->sc_msr;
	delta = sc->sc_msr_delta;
	sc->sc_msr_delta = 0;
	splx(s);

	if (ISSET(delta, sc->sc_msr_dcd)) {
		/*
		 * Inform the tty layer that carrier detect changed.
		 */
		(void)(*tp->t_linesw->l_modem)(tp, ISSET(msr, MCR_DCD));
	}

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

#ifdef SER_DEBUG
	serstatus(sc, "sermsrint");
#endif
}

void
sersoft(void *arg)
{
	struct ser_softc *sc = arg;
	struct tty *tp;

	tp = sc->sc_tty;
	if (tp == NULL)
		return;

	if (!ISSET(tp->t_state, TS_ISOPEN) && (tp->t_wopen == 0))
		return;
	
	if (sc->sc_rx_ready) {
		sc->sc_rx_ready = 0;
		serrxint(sc, tp);
	}

	if (sc->sc_st_check) {
		sc->sc_st_check = 0;
		sermsrint(sc, tp);
	}

	if (sc->sc_tx_done) {
		sc->sc_tx_done = 0;
		sertxint(sc, tp);
	}
}

int
sermintr(void *arg)
{
	struct ser_softc *sc = arg;
	uint8_t msr, delta;

	msr = ~MFP->mf_gpip;
	delta = msr ^ sc->sc_msr;
	sc->sc_msr  = sc->sc_msr & ~(MCR_CTS|MCR_DCD|MCR_RI);
	sc->sc_msr |= msr & (MCR_CTS|MCR_DCD|MCR_RI);

	if (ISSET(delta, sc->sc_msr_mask)) {
		sc->sc_msr_delta |= delta;

		/*
		 * Stop output immediately if we lose the output
		 * flow control signal or carrier detect.
		 */
		if (ISSET(~msr, sc->sc_msr_mask)) {
			sc->sc_tbc = 0;
			sc->sc_heldtbc = 0;
#ifdef SER_DEBUG
			serstatus(sc, "sermintr  ");
#endif
		}

		sc->sc_st_check = 1;
	}
	softint_schedule(sc->sc_sicookie);
	return 1;
}

int
sertrintr(void *arg)
{
	struct ser_softc *sc = arg;
	u_int put, cc;
	uint8_t rsr, tsr;

	put = sc->sc_rbput;
	cc = sc->sc_rbavail;

	rsr = MFP->mf_rsr;
	if (ISSET(rsr, RSR_BFULL|RSR_BREAK)) {
		for (; ISSET(rsr, RSR_BFULL|RSR_BREAK) && cc > 0; cc--) {
			sc->sc_rbuf[put] = MFP->mf_udr;
			sc->sc_lbuf[put] = rsr;
			put = (put + 1) & RXBUFMASK;
			if ((rsr & RSR_BREAK) && (MFP->mf_rsr & RSR_BREAK))
				rsr = 0;
			else
				rsr = MFP->mf_rsr;
		}
		/*
		 * Current string of incoming characters ended because
		 * no more data was available. Schedule a receive event
		 * if any data was received. Drop any characters that
		 * we couldn't handle.
		 */
		sc->sc_rbput    = put;
		sc->sc_rbavail  = cc;
		sc->sc_rx_ready = 1;
		/*
		 * See if we are in danger of overflowing a buffer. If
		 * so, use hardware flow control to ease the pressure.
		 */
		if (sc->sc_rx_blocked == 0 &&
		    cc < sc->sc_r_hiwat) {
			sc->sc_rx_blocked = 1;
			ser_hwiflow(sc, 1);
		}
		/*
		 * If we're out of space, throw away any further input.
		 */
		if (!cc) {
			while (ISSET(rsr, RSR_BFULL|RSR_BREAK)) {
				rsr = MFP->mf_udr;
				rsr = MFP->mf_rsr;
			}
		}
	}

	/*
	 * 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.
	 */
	tsr = MFP->mf_tsr;
	if (ISSET(tsr, TSR_BE)) {
		/*
		 * If we've delayed a parameter change, do it now, and restart
		 * output.
		 */
		if (sc->sc_heldchange) {
			ser_loadchannelregs(sc);
			sc->sc_heldchange = 0;
			sc->sc_tbc = sc->sc_heldtbc;
			sc->sc_heldtbc = 0;
		}
		/* Output the next character, if any. */
		if (sc->sc_tbc > 0) {
			MFP->mf_udr = *sc->sc_tba;
			sc->sc_tbc--;
			sc->sc_tba++;
		} else if (sc->sc_tx_busy) {
			sc->sc_tx_busy = 0;
			sc->sc_tx_done = 1;
		}
	}
	softint_schedule(sc->sc_sicookie);
	return 1;
}

static int
serspeed(long speed)
{
#define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */

	int div, x, err;

	if (speed <= 0)
		return -1;

	for (div = 4; div <= 64; div *= 4) {
		x = divrnd((SER_FREQ / div), speed);

		/*
		 * The value must fit in the timer-d dataregister. If
		 * not, try another delay-mode.
		 */
		if ((x / 2) > 255)
			continue;

		/*
		 * Baudrate to high for the interface or cannot be made
		 * within tolerance.
		 */
		if (x <= 0)
			return -1;

		err = divrnd((SER_FREQ / div) * 1000, speed * x) - 1000;
		if (err < 0)
			err = -err;
		if (err > SER_TOLERANCE)
			continue;

		/*
		 * Translate 'div' to delay-code
		 */
		if (div == 4)
			div = 1;
		else if (div == 16)
			div = 3;
		else if (div == 64)
			div = 5;

		return (x / 2) | (div << 8);
	}
	return -1;

#undef	divrnd
}

/*
 * Following are all routines needed for SER to act as console
 */
#include <dev/cons.h>

void
sercnprobe(struct consdev *cp)
{

	/*
	 * Activate serial console when DCD present...
	 */
	if (MFP->mf_gpip & MCR_DCD) {
		cp->cn_pri = CN_DEAD;
		return;
	}

	/* initialize required fields */
	/* XXX: LWP What unit? */
	cp->cn_dev = makedev(cdevsw_lookup_major(&ser_cdevsw), 0);
	if (serconsole)
		cp->cn_pri = CN_REMOTE;	/* Force a serial port console */
	else
		cp->cn_pri = CN_NORMAL;
}

void
sercninit(struct consdev *cp)
{

	serinitcons(CONSBAUD);
}

/*
 * Initialize UART to known state.
 */
void
serinit(int baud)
{
	int ospeed = serspeed(baud);

	MFP->mf_ucr = UCR_CLKDIV|UCR_8BITS|UCR_STOPB1;
	MFP->mf_rsr = RSR_ENAB;
	MFP->mf_tsr = TSR_ENAB;

	single_inst_bclr_b(MFP->mf_tcdcr, 0x07);
	MFP->mf_tddr  = ospeed;
	single_inst_bset_b(MFP->mf_tcdcr, (ospeed >> 8) & 0x0f);
}

/*
 * Set UART for console use. Do normal init, then enable interrupts.
 */
void
serinitcons(int baud)
{

	serinit(baud);

	/* Set rts/dtr */
	ym2149_rts(0);
	ym2149_dtr(0);

	single_inst_bset_b(MFP->mf_imra, (IA_RRDY|IA_RERR|IA_TRDY|IA_TERR));
}

int
sercngetc(dev_t dev)
{
	uint8_t stat, c;
	int s;

	s = splhigh();
	while (!ISSET(stat = MFP->mf_rsr, RSR_BFULL)) {
		if (!ISSET(stat, RSR_ENAB)) /* XXX */
			MFP->mf_rsr |= RSR_ENAB;
		if (stat & (RSR_FERR|RSR_PERR|RSR_OERR))
			c = MFP->mf_udr;
	}
	c = MFP->mf_udr;
	splx(s);
	return c;
}

#if 1
u_int s_imra;
u_int s_stat1, s_stat2, s_stat3;
#endif
void
sercnputc(dev_t dev, int c)
{
	int timo;
	uint8_t stat, imra;

	/* Mask serial interrupts */
	imra  = MFP->mf_imra & (IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
	single_inst_bclr_b(MFP->mf_imra, imra);
#if 1
	s_imra = imra;
#endif

	/* wait for any pending transmission to finish */
	timo = 50000;
#if 1
	s_stat1 = MFP->mf_tsr;
#endif
	while (!ISSET(stat = MFP->mf_tsr, TSR_BE) && --timo)
		;
	MFP->mf_udr = c;
	/* wait for this transmission to complete */
	timo = 1500000;
#if 1
	s_stat2 = MFP->mf_tsr;
#endif
	while (!ISSET(stat = MFP->mf_tsr, TSR_BE) && --timo)
		;

#if 1
	s_stat3 = MFP->mf_tsr;
#endif
	/* Clear pending serial interrupts and re-enable */
	MFP->mf_ipra = (uint8_t)~imra;
	single_inst_bset_b(MFP->mf_imra, imra);
}

void
sercnpollc(dev_t dev, int on)
{

}