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
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
/* $NetBSD: sbjcn.c,v 1.30 2014/07/25 08:10:34 dholland Exp $ */

/*
 * Copyright 2000, 2001
 * Broadcom Corporation. All rights reserved.
 *
 * This software is furnished under license and may be used and copied only
 * in accordance with the following terms and conditions.  Subject to these
 * conditions, you may download, copy, install, use, modify and distribute
 * modified or unmodified copies of this software in source and/or binary
 * form. No title or ownership is transferred hereby.
 *
 * 1) Any source code used, modified or distributed must reproduce and
 *    retain this copyright notice and list of conditions as they appear in
 *    the source file.
 *
 * 2) No right is granted to use any trade name, trademark, or logo of
 *    Broadcom Corporation.  The "Broadcom Corporation" name may not be
 *    used to endorse or promote products derived from this software
 *    without the prior written permission of Broadcom Corporation.
 *
 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR IMPLIED
 *    WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF
 *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 *    NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM BE LIABLE
 *    FOR ANY DAMAGES WHATSOEVER, AND IN PARTICULAR, BROADCOM SHALL NOT BE
 *    LIABLE FOR 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), EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* from: $NetBSD: com.c,v 1.172 2000/05/03 19:19:04 thorpej Exp */

/*-
 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Charles M. Hannum.
 *
 * 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) 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
 */

/*
 * `sbjcn' driver, supports console over SiByte SB-1250 JTAG.
 *
 *  Accesses a section of JTAG memory space to mimic a console,
 *  if there's a matching program outside to communicate with.
 *  If nobody is there, things will be very quiet.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sbjcn.c,v 1.30 2014/07/25 08:10:34 dholland Exp $");

#define	SBJCN_DEBUG

#include "opt_ddb.h"
#include "ioconf.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/malloc.h>
#include <sys/vnode.h>
#include <sys/kauth.h>

#include <sbmips/dev/sbscd/sbscdvar.h>
#include <sbmips/dev/sbscd/sbjcnvar.h>

#include <dev/cons.h>

#include <mips/locore.h>

void	sbjcn_attach_channel(struct sbjcn_softc *sc, int chan, int intr);
static void sbjcncn_grabdword(struct sbjcn_channel *ch);
static char sbjcncn_nextbyte(struct sbjcn_channel *ch);
static void sbjcn_cngrabdword(void);

#if defined(DDB) || defined(KGDB)
static void sbjcn_enable_debugport(struct sbjcn_channel *ch);
#endif
void	sbjcn_config(struct sbjcn_channel *ch);
void	sbjcn_shutdown(struct sbjcn_channel *ch);
int	sbjcn_speed(long, long *);
static int cflag2modes(tcflag_t, u_char *, u_char *);
int	sbjcn_param(struct tty *, struct termios *);
void	sbjcn_start(struct tty *);
int	sbjcn_hwiflow(struct tty *, int);

void	sbjcn_loadchannelregs(struct sbjcn_channel *);
void	sbjcn_dohwiflow(struct sbjcn_channel *);
void	sbjcn_break(struct sbjcn_channel *, int);
void	sbjcn_modem(struct sbjcn_channel *, int);
void	tiocm_to_sbjcn(struct sbjcn_channel *, int, int);
int	sbjcn_to_tiocm(struct sbjcn_channel *);
void	sbjcn_iflush(struct sbjcn_channel *);

int	sbjcn_init(u_long addr, int chan, int rate, tcflag_t cflag);
int	sbjcn_common_getc(u_long addr, int chan);
void	sbjcn_common_putc(u_long addr, int chan, int c);

int	sbjcn_cngetc(dev_t dev);
void	sbjcn_cnputc(dev_t dev, int c);
void	sbjcn_cnpollc(dev_t dev, int on);

dev_type_open(sbjcnopen);
dev_type_close(sbjcnclose);
dev_type_read(sbjcnread);
dev_type_write(sbjcnwrite);
dev_type_ioctl(sbjcnioctl);
dev_type_stop(sbjcnstop);
dev_type_tty(sbjcntty);

const struct cdevsw sbjcn_cdevsw = {
	.d_open = sbjcnopen,
	.d_close = sbjcnclose,
	.d_read = sbjcnread,
	.d_write = sbjcnwrite,
	.d_ioctl = sbjcnioctl,
	.d_stop = sbjcnstop,
	.d_tty = sbjcntty,
	.d_poll = nopoll,
	.d_mmap = nommap,
	.d_kqfilter = ttykqfilter,
	.d_discard = nodiscard,
	.d_flag = D_TTY
};

#define	integrate	static inline
integrate void sbjcn_rxsoft(struct sbjcn_channel *, struct tty *);
integrate void sbjcn_txsoft(struct sbjcn_channel *, struct tty *);
integrate void sbjcn_stsoft(struct sbjcn_channel *, struct tty *);
integrate void sbjcn_schedrx(struct sbjcn_channel *);
integrate void sbjcn_recv(struct sbjcn_channel *ch);
void	sbjcn_diag(void *);
void	sbjcn_callout(void *);

/*
 * Make this an option variable one can patch.
 * But be warned:  this must be a power of 2!
 */
u_int sbjcn_rbuf_size = SBJCN_RING_SIZE;

/* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
u_int sbjcn_rbuf_hiwat = (SBJCN_RING_SIZE * 1) / 4;
u_int sbjcn_rbuf_lowat = (SBJCN_RING_SIZE * 3) / 4;

static int	sbjcn_cons_present;
static int	sbjcn_cons_attached;
static u_long	sbjcn_cons_addr;
static int	sbjcn_cons_chan;
static int	sbjcn_cons_rate;
static tcflag_t	sbjcn_cons_cflag;
static int	sbjcn_cons_waiting_input;
static uint64_t	sbjcn_cons_input_buf;

#ifdef KGDB
#include <sys/kgdb.h>

static int	sbjcn_kgdb_present;
static int	sbjcn_kgdb_attached;
static u_long	sbjcn_kgdb_addr;
static int	sbjcn_kgdb_chan;

int	sbjcn_kgdb_getc(void *);
void	sbjcn_kgdb_putc(void *, int);
#endif /* KGDB */

static int	sbjcn_match(device_t, cfdata_t, void *);
static void	sbjcn_attach(device_t, device_t, void *);

CFATTACH_DECL_NEW(sbjcn, sizeof(struct sbjcn_softc),
    sbjcn_match, sbjcn_attach, NULL, NULL);

#define	READ_REG(rp)		(mips3_ld((volatile uint64_t *)(rp)))
#define	WRITE_REG(rp, val)	(mips3_sd((volatile uint64_t *)(rp), (val)))

#define	JTAG_CONS_CONTROL  0x00
#define	JTAG_CONS_INPUT    0x20
#define	JTAG_CONS_OUTPUT   0x40
#define	JTAG_CONS_MAGICNUM 0x50FABEEF12349873

#define	jtag_input_len(data)    (((data) >> 56) & 0xFF)


static int
sbjcn_match(device_t parent, cfdata_t match, void *aux)
{
	struct sbscd_attach_args *sa = aux;

	if (sa->sa_locs.sa_type != SBSCD_DEVTYPE_JTAGCONS)
		return (0);

	return 1;
}

static void
sbjcn_attach(device_t parent, device_t self, void *aux)
{
	struct sbjcn_softc *sc = device_private(self);
	struct sbscd_attach_args *sa = aux;

	sc->sc_dev = self;
	sc->sc_addr = sa->sa_base + sa->sa_locs.sa_offset;

	aprint_normal("\n");
	sbjcn_attach_channel(sc, 0, sa->sa_locs.sa_intr[0]);
}

void
sbjcn_attach_channel(struct sbjcn_softc *sc, int chan, int intr)
{
	struct sbjcn_channel *ch = &sc->sc_channels[chan];
	u_long chan_addr;
	struct tty *tp;

	ch->ch_sc = sc;
	ch->ch_num = chan;

	chan_addr = sc->sc_addr + (0x100 * chan);
	ch->ch_base = (void *)MIPS_PHYS_TO_KSEG1(chan_addr);
	ch->ch_input_reg =
	    (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + JTAG_CONS_INPUT);
	ch->ch_output_reg =
	    (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + JTAG_CONS_OUTPUT);
	ch->ch_control_reg =
	    (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + JTAG_CONS_CONTROL);
	ch->ch_waiting_input = 0;

	ch->ch_i_dcd = ch->ch_i_dcd_pin = 0 /* XXXCGD */;
	ch->ch_i_cts = ch->ch_i_cts_pin = 0 /* XXXCGD */;
	ch->ch_i_dsr = ch->ch_i_dsr_pin = 0 /* XXXCGD */;
	ch->ch_i_ri = ch->ch_i_ri_pin = 0 /* XXXCGD */;
	ch->ch_i_mask =
	    ch->ch_i_dcd | ch->ch_i_cts | ch->ch_i_dsr | ch->ch_i_ri;
	ch->ch_o_dtr = ch->ch_o_dtr_pin = 0 /* XXXCGD */;
	ch->ch_o_rts = ch->ch_o_rts_pin = 0 /* XXXCGD */;
	ch->ch_o_mask = ch->ch_o_dtr | ch->ch_o_rts;

	callout_init(&ch->ch_diag_callout, 0);
	callout_init(&ch->ch_callout, 0);

	/* Disable interrupts before configuring the device. */
	ch->ch_imr = 0;

	if (sbjcn_cons_present &&
	    sbjcn_cons_addr == chan_addr && sbjcn_cons_chan == chan) {
		sbjcn_cons_attached = 1;

		/* Make sure the console is always "hardwired". */
		delay(1000);			/* wait for output to finish */
		SET(ch->ch_hwflags, SBJCN_HW_CONSOLE);
		SET(ch->ch_swflags, TIOCFLAG_SOFTCAR);
	}

	tp = tty_alloc();
	tp->t_oproc = sbjcn_start;
	tp->t_param = sbjcn_param;
	tp->t_hwiflow = sbjcn_hwiflow;

	ch->ch_tty = tp;
	ch->ch_rbuf = malloc(sbjcn_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
	if (ch->ch_rbuf == NULL) {
		aprint_error_dev(sc->sc_dev,
		    "channel %d: unable to allocate ring buffer\n",
		    chan);
		return;
	}
	ch->ch_ebuf = ch->ch_rbuf + (sbjcn_rbuf_size << 1);

	tty_attach(tp);

	if (ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE)) {
		int maj;

		/* locate the major number */
		maj = cdevsw_lookup_major(&sbjcn_cdevsw);

		cn_tab->cn_dev = makedev(maj,
		    (device_unit(sc->sc_dev) << 1) + chan);

		aprint_normal_dev(sc->sc_dev, "channel %d: %s\n",
		    chan, "console");
	}

#ifdef KGDB
	/*
	 * Allow kgdb to "take over" this port.  If this is
	 * the kgdb device, it has exclusive use.
	 */
	if (sbjcn_kgdb_present &&
	    sbjcn_kgdb_addr == chan_addr && sbjcn_kgdb_chan == chan) {
		sbjcn_kgdb_attached = 1;

		SET(ch->ch_hwflags, SBJCN_HW_KGDB);
		aprint_normal_dev(sc->sc_dev, "channel %d: %s\n",
		    chan, "kgdb");
	}
#endif

	sbjcn_config(ch);

	callout_reset(&ch->ch_callout, hz/10, sbjcn_callout, ch);

	SET(ch->ch_hwflags, SBJCN_HW_DEV_OK);
}

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

	int x, err;
	int frequency = 100000000;

	*brcp = divrnd(frequency / 20, speed) - 1;

	if (speed <= 0)
		return (-1);
	x = divrnd(frequency / 20, speed);
	if (x <= 0)
		return (-1);
	err = divrnd(((quad_t)frequency) * 1000 / 20, speed * x) - 1000;
	if (err < 0)
		err = -err;
	if (err > SBJCN_TOLERANCE)
		return (-1);
	*brcp = x - 1;
	return (0);

#undef	divrnd
}

#ifdef SBJCN_DEBUG
void	sbjcn_status(struct sbjcn_channel *, char *);

int	sbjcn_debug = 1 /* XXXCGD */;

void
sbjcn_status(struct sbjcn_channel *ch, char *str)
{
	struct sbjcn_softc *sc = ch->ch_sc;
	struct tty *tp = ch->ch_tty;

	aprint_normal_dev(sc->sc_dev,
	    "chan %d: %s %sclocal  %sdcd %sts_carr_on %sdtr %stx_stopped\n",
	    ch->ch_num, str,
	    ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
	    ISSET(ch->ch_iports, ch->ch_i_dcd) ? "+" : "-",
	    ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
	    ISSET(ch->ch_oports, ch->ch_o_dtr) ? "+" : "-",
	    ch->ch_tx_stopped ? "+" : "-");

	aprint_normal_dev(sc->sc_dev,
	    "chan %d: %s %scrtscts %scts %sts_ttstop  %srts %xrx_flags\n",
	    ch->ch_num, str,
	    ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
	    ISSET(ch->ch_iports, ch->ch_i_cts) ? "+" : "-",
	    ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
	    ISSET(ch->ch_oports, ch->ch_o_rts) ? "+" : "-",
	    ch->ch_rx_flags);
}
#endif

#if defined(DDB) || defined(KGDB)
static void
sbjcn_enable_debugport(struct sbjcn_channel *ch)
{
	int s;

	/* Turn on line break interrupt, set carrier. */
	s = splserial();

	ch->ch_imr = 0x04;
	SET(ch->ch_oports, ch->ch_o_dtr | ch->ch_o_rts);

	splx(s);
}
#endif

void
sbjcn_config(struct sbjcn_channel *ch)
{

	/* Disable interrupts before configuring the device. */
	ch->ch_imr = 0x00;

#ifdef DDB
	if (ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE))
		sbjcn_enable_debugport(ch);
#endif

#ifdef KGDB
	/*
	 * Allow kgdb to "take over" this port.  If this is
	 * the kgdb device, it has exclusive use.
	 */
	if (ISSET(ch->ch_hwflags, SBJCN_HW_KGDB))
		sbjcn_enable_debugport(ch);
#endif
}

void
sbjcn_shutdown(struct sbjcn_channel *ch)
{
	struct tty *tp = ch->ch_tty;
	int s;

	s = splserial();

	/* If we were asserting flow control, then deassert it. */
	SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
	sbjcn_dohwiflow(ch);

	/* Clear any break condition set with TIOCSBRK. */
	sbjcn_break(ch, 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)) {
		sbjcn_modem(ch, 0);
		(void) tsleep(ch, TTIPRI, ttclos, hz);
	}

	/* Turn off interrupts. */
#ifdef DDB
	if (ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE))
		ch->ch_imr = 0x04; /* interrupt on break */
	else
#endif
		ch->ch_imr = 0;

	splx(s);
}

int
sbjcnopen(dev_t dev, int flag, int mode, struct lwp *l)
{
	int chan = SBJCN_CHAN(dev);
	struct sbjcn_softc *sc;
	struct sbjcn_channel *ch;
	struct tty *tp;
	int s, s2;
	int error;

	sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(dev));
	if (sc == NULL)
		return (ENXIO);

	ch = &sc->sc_channels[chan];
	if (!ISSET(ch->ch_hwflags, SBJCN_HW_DEV_OK) || ch->ch_rbuf == NULL)
		return (ENXIO);

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

	tp = ch->ch_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;

		s2 = splserial();

		/* Turn on receive, break, and status change interrupts. */
		ch->ch_imr = 0xe;

		/* Fetch the current modem control status, needed later. */
		ch->ch_iports = 0;
		ch->ch_iports_delta = 0;
		splx(s2);

		/*
		 * Initialize the termios status to the defaults.  Add in the
		 * sticky bits from TIOCSFLAGS.
		 */
		t.c_ispeed = 0;
		if (ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE)) {
			t.c_ospeed = sbjcn_cons_rate;
			t.c_cflag = sbjcn_cons_cflag;
		} else {
			t.c_ospeed = TTYDEF_SPEED;
			t.c_cflag = TTYDEF_CFLAG;
		}
		if (ISSET(ch->ch_swflags, TIOCFLAG_CLOCAL))
			SET(t.c_cflag, CLOCAL);
		if (ISSET(ch->ch_swflags, TIOCFLAG_CRTSCTS))
			SET(t.c_cflag, CRTSCTS);
		if (ISSET(ch->ch_swflags, TIOCFLAG_MDMBUF))
			SET(t.c_cflag, MDMBUF);
		/* Make sure sbjcn_param() will do something. */
		tp->t_ospeed = 0;
		(void) sbjcn_param(tp, &t);
		tp->t_iflag = TTYDEF_IFLAG;
		tp->t_oflag = TTYDEF_OFLAG;
		tp->t_lflag = TTYDEF_LFLAG;
		ttychars(tp);
		ttsetwater(tp);

		s2 = splserial();

		/*
		 * 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, which applications do not
		 * expect.  We always assert DTR while the device is open
		 * unless explicitly requested to deassert it.
		 */
		sbjcn_modem(ch, 1);

		/* Clear the input ring, and unblock. */
		ch->ch_rbput = ch->ch_rbget = ch->ch_rbuf;
		ch->ch_rbavail = sbjcn_rbuf_size;
		sbjcn_iflush(ch);
		CLR(ch->ch_rx_flags, RX_ANY_BLOCK);
		sbjcn_dohwiflow(ch);

#ifdef SBJCN_DEBUG
		if (sbjcn_debug)
			sbjcn_status(ch, "sbjcnopen  ");
#endif

		splx(s2);
	}

	splx(s);

	error = ttyopen(tp, SBJCN_DIALOUT(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.
		 */
		sbjcn_shutdown(ch);
	}

	return (error);
}

int
sbjcnclose(dev_t dev, int flag, int mode, struct proc *p)
{
	struct sbjcn_softc *sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(dev));
	struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
	struct tty *tp = ch->ch_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.
		 */
		sbjcn_shutdown(ch);
	}

	return (0);
}

int
sbjcnread(dev_t dev, struct uio *uio, int flag)
{
	struct sbjcn_softc *sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(dev));
	struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
	struct tty *tp = ch->ch_tty;

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

int
sbjcnwrite(dev_t dev, struct uio *uio, int flag)
{
	struct sbjcn_softc *sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(dev));
	struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
	struct tty *tp = ch->ch_tty;

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

struct tty *
sbjcntty(dev_t dev)
{
	struct sbjcn_softc *sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(dev));
	struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
	struct tty *tp = ch->ch_tty;

	return (tp);
}

int
sbjcnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
	struct sbjcn_softc *sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(dev));
	struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
	struct tty *tp = ch->ch_tty;
	int error;
	int s;

	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
	if (error >= 0)
		return (error);

	error = ttioctl(tp, cmd, data, flag, p);
	if (error >= 0)
		return (error);

	error = 0;

	s = splserial();

	switch (cmd) {
	case TIOCSBRK:
		sbjcn_break(ch, 1);
		break;

	case TIOCCBRK:
		sbjcn_break(ch, 0);
		break;

	case TIOCSDTR:
		sbjcn_modem(ch, 1);
		break;

	case TIOCCDTR:
		sbjcn_modem(ch, 0);
		break;

	case TIOCGFLAGS:
		*(int *)data = ch->ch_swflags;
		break;

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

	case TIOCMSET:
	case TIOCMBIS:
	case TIOCMBIC:
		tiocm_to_sbjcn(ch, cmd, *(int *)data);
		break;

	case TIOCMGET:
		*(int *)data = sbjcn_to_tiocm(ch);
		break;

	default:
		error = ENOTTY;
		break;
	}

	splx(s);

#ifdef SBJCN_DEBUG
	if (sbjcn_debug)
		sbjcn_status(ch, "sbjcn_ioctl ");
#endif

	return (error);
}

integrate void
sbjcn_schedrx(struct sbjcn_channel *ch)
{

	ch->ch_rx_ready = 1;

	/* Next callout will detect this flag. */
}

void
sbjcn_break(struct sbjcn_channel *ch, int onoff)
{
	/* XXXKW do something? */
}

void
sbjcn_modem(struct sbjcn_channel *ch, int onoff)
{

	if (ch->ch_o_dtr == 0)
		return;

	if (onoff)
		SET(ch->ch_oports, ch->ch_o_dtr);
	else
		CLR(ch->ch_oports, ch->ch_o_dtr);

	if (!ch->ch_heldchange) {
		if (ch->ch_tx_busy) {
			ch->ch_heldtbc = ch->ch_tbc;
			ch->ch_tbc = 0;
			ch->ch_heldchange = 1;
		} else
			sbjcn_loadchannelregs(ch);
	}
}

void
tiocm_to_sbjcn(struct sbjcn_channel *ch, int how, int ttybits)
{
	u_char bits;

	bits = 0;
	if (ISSET(ttybits, TIOCM_DTR))
		SET(bits, ch->ch_o_dtr);
	if (ISSET(ttybits, TIOCM_RTS))
		SET(bits, ch->ch_o_rts);

	switch (how) {
	case TIOCMBIC:
		CLR(ch->ch_oports, bits);
		break;

	case TIOCMBIS:
		SET(ch->ch_oports, bits);
		break;

	case TIOCMSET:
		ch->ch_oports = bits;
		break;
	}

	if (!ch->ch_heldchange) {
		if (ch->ch_tx_busy) {
			ch->ch_heldtbc = ch->ch_tbc;
			ch->ch_tbc = 0;
			ch->ch_heldchange = 1;
		} else
			sbjcn_loadchannelregs(ch);
	}
}

int
sbjcn_to_tiocm(struct sbjcn_channel *ch)
{
	u_char hwbits;
	int ttybits = 0;

	hwbits = ch->ch_oports;
	if (ISSET(hwbits, ch->ch_o_dtr))
		SET(ttybits, TIOCM_DTR);
	if (ISSET(hwbits, ch->ch_o_rts))
		SET(ttybits, TIOCM_RTS);

	hwbits = ch->ch_iports;
	if (ISSET(hwbits, ch->ch_i_dcd))
		SET(ttybits, TIOCM_CD);
	if (ISSET(hwbits, ch->ch_i_cts))
		SET(ttybits, TIOCM_CTS);
	if (ISSET(hwbits, ch->ch_i_dsr))
		SET(ttybits, TIOCM_DSR);
	if (ISSET(hwbits, ch->ch_i_ri))
		SET(ttybits, TIOCM_RI);

	if (ch->ch_imr != 0)
		SET(ttybits, TIOCM_LE);

	return (ttybits);
}

static int
cflag2modes(tcflag_t cflag, u_char *mode1p, u_char *mode2p)
{
	u_char mode1;
	u_char mode2;
	int err = 0;

	mode1 = mode2 = 0;

	switch (ISSET(cflag, CSIZE)) {
	case CS7:
		mode1 |= 2;			/* XXX */
		break;
	default:
		err = -1;
		/* FALLTHRU for sanity */
	case CS8:
		mode1 |= 3;			/* XXX */
		break;
	}
	if (!ISSET(cflag, PARENB))
		mode1 |= 2 << 3;
	else {
		mode1 |= 0 << 3;
		if (ISSET(cflag, PARODD))
			mode1 |= 1 << 2;
	}

	if (ISSET(cflag, CSTOPB))
		mode2 |= 1 << 3;		/* two stop bits XXX not std */

	if (ISSET(cflag, CRTSCTS)) {
		mode1 |= 1 << 7;
		mode2 |= 1 << 4;
	}

	*mode1p = mode1;
	*mode2p = mode2;
	return (err);
}

int
sbjcn_param(struct tty *tp, struct termios *t)
{
	struct sbjcn_softc *sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(tp->t_dev));
	struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
	long brc;
	u_char mode1, mode2;
	int s;

/* XXX reset to console parameters if console? */
#if 0
XXX disable, enable.
#endif

	/* Check requested parameters. */
	if (sbjcn_speed(t->c_ospeed, &brc) < 0)
		return (EINVAL);
	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(ch->ch_swflags, TIOCFLAG_SOFTCAR) ||
	    ISSET(ch->ch_hwflags, SBJCN_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 (cflag2modes(t->c_cflag, &mode1, &mode2) < 0)
		return (EINVAL);

	s = splserial();

	ch->ch_mode1 = mode1;
	ch->ch_mode2 = mode2;

	/*
	 * If we're not in a mode that assumes a connection is present, then
	 * ignore carrier changes.
	 */
	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
		ch->ch_i_dcd = 0;
	else
		ch->ch_i_dcd = ch->ch_i_dcd_pin;
	/*
	 * Set the flow control pins depending on the current flow control
	 * mode.
	 */
	if (ISSET(t->c_cflag, CRTSCTS)) {
		ch->ch_o_dtr = ch->ch_o_dtr_pin;
		ch->ch_o_rts = ch->ch_o_rts_pin;
		ch->ch_i_cts = ch->ch_i_cts_pin;
		/* hw controle enable bits in mod regs set by cflag2modes */
	} else if (ISSET(t->c_cflag, MDMBUF)) {
		/*
		 * For DTR/DCD flow control, make sure we don't toggle DTR for
		 * carrier detection.
		 */
		ch->ch_o_dtr = 0;
		ch->ch_o_rts = ch->ch_o_dtr_pin;
		ch->ch_i_cts = ch->ch_i_dcd_pin;
	} 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.
		 */
		ch->ch_o_dtr = ch->ch_o_dtr_pin | ch->ch_o_rts_pin;
		ch->ch_o_rts = 0;
		ch->ch_i_cts = 0;
		if (ISSET(ch->ch_oports, ch->ch_o_dtr_pin))
			SET(ch->ch_oports, ch->ch_o_rts_pin);
		else
			CLR(ch->ch_oports, ch->ch_o_rts_pin);
	}
	/* XXX maybe mask the ports which generate intrs? */

	ch->ch_brc = brc;

	/* XXX maybe set fifo-full receive mode if RTSCTS and high speed? */

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

	if (!ch->ch_heldchange) {
		if (ch->ch_tx_busy) {
			ch->ch_heldtbc = ch->ch_tbc;
			ch->ch_tbc = 0;
			ch->ch_heldchange = 1;
		} else
			sbjcn_loadchannelregs(ch);
	}

	if (!ISSET(t->c_cflag, CHWFLOW)) {
		/* Disable the high water mark. */
		ch->ch_r_hiwat = 0;
		ch->ch_r_lowat = 0;
		if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
			CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
			sbjcn_schedrx(ch);
		}
		if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
			CLR(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
			sbjcn_dohwiflow(ch);
		}
	} else {
		ch->ch_r_hiwat = sbjcn_rbuf_hiwat;
		ch->ch_r_lowat = sbjcn_rbuf_lowat;
	}

	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.
	 */
	(void) (*tp->t_linesw->l_modem)(tp,
	    ISSET(ch->ch_iports, ch->ch_i_dcd));

#ifdef SBJCN_DEBUG
	if (sbjcn_debug)
		sbjcn_status(ch, "sbjcnparam ");
#endif

	if (!ISSET(t->c_cflag, CHWFLOW)) {
		if (ch->ch_tx_stopped) {
			ch->ch_tx_stopped = 0;
			sbjcn_start(tp);
		}
	}

	return (0);
}

void
sbjcn_iflush(struct sbjcn_channel *ch)
{
	uint64_t reg;
	int timo;

	timo = 50000;
	/* flush any pending I/O */
	while (((reg = READ_REG(ch->ch_input_reg)) != 0) && --timo)
		;

#ifdef DIAGNOSTIC
	if (!timo)
		aprint_error_dev(ch->ch_sc->sc_dev,
		    "sbjcn_iflush timeout %02x\n", reg);
#endif
}

void
sbjcn_loadchannelregs(struct sbjcn_channel *ch)
{
}

int
sbjcn_hwiflow(struct tty *tp, int block)
{
	struct sbjcn_softc *sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(tp->t_dev));
	struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
	int s;

	if (ch->ch_o_rts == 0)
		return (0);

	s = splserial();
	if (block) {
		if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
			SET(ch->ch_rx_flags, RX_TTY_BLOCKED);
			sbjcn_dohwiflow(ch);
		}
	} else {
		if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
			CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
			sbjcn_schedrx(ch);
		}
		if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
			CLR(ch->ch_rx_flags, RX_TTY_BLOCKED);
			sbjcn_dohwiflow(ch);
		}
	}
	splx(s);
	return (1);
}

/*
 * (un)block input via hw flowcontrol
 */
void
sbjcn_dohwiflow(struct sbjcn_channel *ch)
{

	if (ch->ch_o_rts == 0)
		return;

	if (ISSET(ch->ch_rx_flags, RX_ANY_BLOCK)) {
		CLR(ch->ch_oports, ch->ch_o_rts);
		CLR(ch->ch_oports_active, ch->ch_o_rts);
	} else {
		SET(ch->ch_oports, ch->ch_o_rts);
		SET(ch->ch_oports_active, ch->ch_o_rts);
	}
}

void
sbjcn_start(struct tty *tp)
{
	struct sbjcn_softc *sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(tp->t_dev));
	struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
	int s;

	s = spltty();
	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
		goto out;
	if (ch->ch_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();

		ch->ch_tba = tba;
		ch->ch_tbc = tbc;
	}

	SET(tp->t_state, TS_BUSY);
	ch->ch_tx_busy = 1;

	/* Output the first chunk of the contiguous buffer. */
	{
		while (ch->ch_tbc) {
			uint64_t data;
			int bytes, i;

			bytes = (ch->ch_tbc > 7) ? 7 : ch->ch_tbc;
			data = bytes;
			for (i=0; i<bytes; i++) {
				data <<= 8;
				data |= *ch->ch_tba++;
			}
			if (bytes < 7)
				data <<= 56-(bytes<<3);
			ch->ch_tbc -= bytes;
			WRITE_REG(ch->ch_output_reg, data);
		}
		ch->ch_tx_busy = 0;
		ch->ch_tx_done = 1;
	}
out:
	splx(s);
	return;
}

/*
 * Stop output on a line.
 */
void
sbjcnstop(struct tty *tp, int flag)
{
	struct sbjcn_softc *sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(tp->t_dev));
	struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
	int s;

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

void
sbjcn_diag(void *arg)
{
	struct sbjcn_channel *ch = arg;
	struct sbjcn_softc *sc = ch->ch_sc;
	int overflows, floods;
	int s;

	s = splserial();
	overflows = ch->ch_overflows;
	ch->ch_overflows = 0;
	floods = ch->ch_floods;
	ch->ch_floods = 0;
	ch->ch_errors = 0;
	splx(s);

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

integrate void
sbjcn_rxsoft(struct sbjcn_channel *ch, struct tty *tp)
{
	int (*rint)(int c, struct tty *tp) = tp->t_linesw->l_rint;
	u_char *get, *end;
	u_int cc, scc;
	u_char sr;
	int code;
	int s;

	end = ch->ch_ebuf;
	get = ch->ch_rbget;
	scc = cc = sbjcn_rbuf_size - ch->ch_rbavail;

	if (cc == sbjcn_rbuf_size) {
		ch->ch_floods++;
		if (ch->ch_errors++ == 0)
			callout_reset(&ch->ch_diag_callout, 60 * hz,
			    sbjcn_diag, ch);
	}

	while (cc) {
		code = get[0];
		sr = get[1];
		if (ISSET(sr, 0xf0)) {
			if (ISSET(sr, 0x10)) {
				ch->ch_overflows++;
				if (ch->ch_errors++ == 0)
					callout_reset(&ch->ch_diag_callout,
					    60 * hz, sbjcn_diag, ch);
			}
			if (ISSET(sr, 0xc0))
				SET(code, TTY_FE);
			if (ISSET(sr, 0x20))
				SET(code, TTY_PE);
		}
		if ((*rint)(code, tp) == -1) {
			/*
			 * The line discipline's buffer is out of space.
			 */
			if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
				/*
				 * 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 >= end)
					get -= sbjcn_rbuf_size << 1;
				cc = 0;
			} else {
				/*
				 * Don't schedule any more receive processing
				 * until the line discipline tells us there's
				 * space available (through comhwiflow()).
				 * Leave the rest of the data in the input
				 * buffer.
				 */
				SET(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
			}
			break;
		}
		get += 2;
		if (get >= end)
			get = ch->ch_rbuf;
		cc--;
	}

	if (cc != scc) {
		ch->ch_rbget = get;
		s = splserial();
		cc = ch->ch_rbavail += scc - cc;
		/* Buffers should be ok again, release possible block. */
		if (cc >= ch->ch_r_lowat) {
			if (ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
				CLR(ch->ch_rx_flags, RX_IBUF_OVERFLOWED);
				SET(ch->ch_imr, 0x02);
			}
			if (ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED)) {
				CLR(ch->ch_rx_flags, RX_IBUF_BLOCKED);
				sbjcn_dohwiflow(ch);
			}
		}
		splx(s);
	}
}

integrate void
sbjcn_txsoft(struct sbjcn_channel *ch, 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)(ch->ch_tba - tp->t_outq.c_cf));
	(*tp->t_linesw->l_start)(tp);
}

integrate void
sbjcn_stsoft(struct sbjcn_channel *ch, struct tty *tp)
{
	u_char iports, delta;
	int s;

	s = splserial();
	iports = ch->ch_iports;
	delta = ch->ch_iports_delta;
	ch->ch_iports_delta = 0;
	splx(s);

	if (ISSET(delta, ch->ch_i_dcd)) {
		/*
		 * Inform the tty layer that carrier detect changed.
		 */
		(void) (*tp->t_linesw->l_modem)(tp,
		    ISSET(iports, ch->ch_i_dcd));
	}

	if (ISSET(delta, ch->ch_i_cts)) {
		/* Block or unblock output according to flow control. */
		if (ISSET(iports, ch->ch_i_cts)) {
			ch->ch_tx_stopped = 0;
			(*tp->t_linesw->l_start)(tp);
		} else {
			ch->ch_tx_stopped = 1;
		}
	}

#ifdef SBJCN_DEBUG
	if (sbjcn_debug)
		sbjcn_status(ch, "sbjcn_stsoft");
#endif
}

integrate void
sbjcn_recv(struct sbjcn_channel *ch)
{
	u_char *put, *end;
	u_int cc;

	end = ch->ch_ebuf;
	put = ch->ch_rbput;
	cc = ch->ch_rbavail;

	/* XXX process break */

	sbjcncn_grabdword(ch);
	if (ch->ch_waiting_input) {
		if (!ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
			while (cc > 0) {
				put[0] = sbjcncn_nextbyte(ch);
				put[1] = 1; /* XXXKW ? */
				put += 2;
				if (put >= end)
					put = ch->ch_rbuf;
				cc--;

				if (!ch->ch_waiting_input)
					break;
			}

			/*
			 * 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.
			 */
			ch->ch_rbput = put;
			ch->ch_rbavail = cc;
			if (!ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED))
				ch->ch_rx_ready = 1;

			/*
			 * See if we are in danger of overflowing a
			 * buffer. If so, use hardware flow control
			 * to ease the pressure.
			 */
			if (!ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED) &&
			    cc < ch->ch_r_hiwat) {
				SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
				sbjcn_dohwiflow(ch);
			}

			/*
			 * If we're out of space, disable receive
			 * interrupts until the queue has drained
			 * a bit.
			 */
			if (!cc) {
				SET(ch->ch_rx_flags,
				RX_IBUF_OVERFLOWED);
				CLR(ch->ch_imr, 0x02);
			}
		} else {
			/* XXX panic? */
			CLR(ch->ch_imr, 0x02);
			//			continue;
		}
	}

	/*
	 * If we've delayed a parameter change, do it now, and restart
	 * output.
	 */
	if (ch->ch_heldchange) {
		sbjcn_loadchannelregs(ch);
		ch->ch_heldchange = 0;
		ch->ch_tbc = ch->ch_heldtbc;
		ch->ch_heldtbc = 0;
	}
}

void
sbjcn_callout(void *arg)
{
	struct sbjcn_channel *ch = arg;
	struct tty *tp = ch->ch_tty;

	/* XXX get stuff */
	sbjcn_recv(ch);

	/* XXX check receive */
	if (ch->ch_rx_ready) {
		ch->ch_rx_ready = 0;
		sbjcn_rxsoft(ch, tp);
	}

	/* XXX check transmit */
	if (ch->ch_tx_done) {
		ch->ch_tx_done = 0;
		sbjcn_txsoft(ch, tp);
	}

	callout_reset(&ch->ch_callout, hz/10, sbjcn_callout, ch);
}

static char sbjcncn_nextbyte(struct sbjcn_channel *ch)
{
	char c;

	sbjcncn_grabdword(ch);
	c = (ch->ch_input_buf >> 56) & 0xff;
	ch->ch_input_buf <<= 8;
	ch->ch_waiting_input--;
	return c;
}

static void sbjcncn_grabdword(struct sbjcn_channel *ch)
{
	uint64_t inbuf;

	if (ch->ch_waiting_input)
		return;

	inbuf = READ_REG(ch->ch_input_reg);
	ch->ch_waiting_input = jtag_input_len(inbuf);
	ch->ch_input_buf = inbuf << 8;
}

/*
 * Initialize UART for use as console or KGDB line.
 */
int
sbjcn_init(u_long addr, int chan, int rate, tcflag_t cflag)
{
	/* XXXKW Anything to do here? */
	return (0);
}

/*
 * Following are all routines needed for sbjcn to act as console
 */
int
sbjcn_cnattach(u_long addr, int chan, int rate, tcflag_t cflag)
{
	int res;
	uint64_t ctrl_val;
	static struct consdev sbjcn_cons = {
		NULL, NULL, sbjcn_cngetc, sbjcn_cnputc, sbjcn_cnpollc, NULL,
		    NODEV, CN_NORMAL
	};

	res = sbjcn_init(addr, chan, rate, cflag);
	if (res)
		return (res);

	cn_tab = &sbjcn_cons;

	sbjcn_cons_present = 1;
	sbjcn_cons_addr = addr;
	sbjcn_cons_waiting_input = 0;
	sbjcn_cons_chan = chan;
	sbjcn_cons_rate = rate;
	sbjcn_cons_cflag = cflag;

	/* Wait for sign of life from the other end */
	while ((ctrl_val = READ_REG(MIPS_PHYS_TO_KSEG1(sbjcn_cons_addr + JTAG_CONS_CONTROL))) == 0)
		;

	return (ctrl_val != JTAG_CONS_MAGICNUM);
}

int
sbjcn_cngetc(dev_t dev)
{
	char c;

	while (sbjcn_cons_waiting_input == 0)
		sbjcn_cngrabdword();

	c = (sbjcn_cons_input_buf >> 56) & 0xff;
	sbjcn_cons_input_buf <<= 8;
	sbjcn_cons_waiting_input--;

	return c;
}

/*
 * Console kernel output character routine.
 */
void
sbjcn_cnputc(dev_t dev, int c)
{
	uint64_t outbuf;

	outbuf = (1LL << 56) | (((uint64_t)c) << 48);
	WRITE_REG(MIPS_PHYS_TO_KSEG1(sbjcn_cons_addr + JTAG_CONS_OUTPUT), outbuf);
}

void
sbjcn_cnpollc(dev_t dev, int on)
{

}

static void sbjcn_cngrabdword(void)
{
	uint64_t inbuf;

	if (sbjcn_cons_waiting_input)
		return;

	inbuf = READ_REG(MIPS_PHYS_TO_KSEG1(sbjcn_cons_addr + JTAG_CONS_INPUT));
	sbjcn_cons_waiting_input = jtag_input_len(inbuf);
	sbjcn_cons_input_buf = inbuf << 8;
}

#ifdef KGDB
int
sbjcn_kgdb_attach(u_long addr, int chan, int rate, tcflag_t cflag)
{
	int res;

	if (!sbjcn_cons_present &&
	    sbjcn_cons_addr == addr && sbjcn_cons_chan == chan)
		return (EBUSY); /* cannot share with console */

	res = sbjcn_init(addr, chan, rate, cflag);
	if (res)
		return (res);

	kgdb_attach(sbjcn_kgdb_getc, sbjcn_kgdb_putc, NULL);
	kgdb_dev = 123; /* unneeded, only to satisfy some tests */
	kgdb_rate = rate;

	sbjcn_kgdb_present = 1;
	/* XXX sbjcn_init wants addr, but we need the offset addr */
	sbjcn_kgdb_addr = addr + (chan * 0x100);
	sbjcn_kgdb_chan = chan;

	return (0);
}

/* ARGSUSED */
int
sbjcn_kgdb_getc(void *arg)
{

	return (sbjcn_common_getc(sbjcn_kgdb_addr, sbjcn_kgdb_chan));
}

/* ARGSUSED */
void
sbjcn_kgdb_putc(void *arg, int c)
{

	sbjcn_common_putc(sbjcn_kgdb_addr, sbjcn_kgdb_chan, c);
}
#endif /* KGDB */

/*
 * helper function to identify the sbjcn channels used by
 * console or KGDB (and not yet autoconf attached)
 */
int
sbjcn_is_console(u_long addr, int chan)
{

	if (sbjcn_cons_present && !sbjcn_cons_attached &&
	    sbjcn_cons_addr == addr && sbjcn_cons_chan == chan)
		return (1);
#ifdef KGDB
	if (sbjcn_kgdb_present && !sbjcn_kgdb_attached &&
	    sbjcn_kgdb_addr == addr && sbjcn_kgdb_chan == chan)
		return (1);
#endif
	return (0);
}