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
/*	$NetBSD: spx.c,v 1.9 2016/07/07 06:55:39 msaitoh Exp $ */
/*
 * SPX/LCSPX/SPXg/SPXgt accelerated framebuffer driver for NetBSD/VAX
 * Copyright (c) 2005 Blaz Antonic
 * All rights reserved.
 *
 * This software contains code written by Michael L. Hitch.
 *
 * 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 abovementioned copyrights
 * 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.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: spx.c,v 1.9 2016/07/07 06:55:39 msaitoh Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/callout.h>
#include <sys/conf.h>
#include <sys/cpu.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/time.h>

#include <machine/vsbus.h>
#include <machine/sid.h>
#include <machine/ka420.h>

#include <dev/cons.h>

#include <dev/dec/dzreg.h>
#include <dev/dec/dzvar.h>
#include <dev/dec/dzkbdvar.h>

#include <dev/wscons/wsdisplayvar.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wscons_callbacks.h>
#include <dev/wsfont/wsfont.h>

#include "machine/scb.h"

#include "dzkbd.h"

#define CONF_LCSPX		0x02
#define CONF_SPXg		0x10

#define FB_IS_SPX		1
#define FB_IS_SPXg		2

/* Screen hardware defs */
#define	SPX_FB_ADDR		0x38000000	/* Frame buffer */
#define SPXg_FB_ADDR_KA46	0x22200000
#define SPXg_FB_ADDR_KA49	0x28200000
#define SPXg_FB_ADDR		((vax_boardtype == VAX_BTYP_46) ? \
				 SPXg_FB_ADDR_KA46 : SPXg_FB_ADDR_KA49)

#define SPXg_WIN_SIZE		0x00100000	/* Window size */
/* # of pixels that fit into single window */
#define SPXg_WIN_LINEAR		(SPXg_WIN_SIZE / 2)

#define SPXg_DELAY		5

/*
 * off-screen font storage space 
 * 32x16 glyphs, 256 regular and underlined chars
 */
#define FONT_STORAGE_START	(spx_xsize * spx_ysize)
#define FONT_STORAGE_SIZE	(32 * 16 * 256 * 2)

/* register space defines */
#define SPX_REG_SIZE		0x00002000
#define SPX_REG_ADDR		0x39302000	/* 1st set of SPX registers */

#define SPXg_REG_SIZE		0x00004000
#define SPXg_REG_ADDR_KA46	0x22004000
#define SPXg_REG_ADDR_KA49	0x28004000
#define SPXg_REG_ADDR		((vax_boardtype == VAX_BTYP_46) ? \
				 SPXg_REG_ADDR_KA46 : SPXg_REG_ADDR_KA49)

#define SPX_REG1_SIZE		0x00001000
#define SPX_REG1_ADDR		0x39b00000	/* 2nd set of SPX registers */

#define SPXg_REG1_SIZE		0x00001000
#define SPXg_REG1_ADDR_KA46	0x22100000
#define SPXg_REG1_ADDR_KA49	0x28100000
#define SPXg_REG1_ADDR		((vax_boardtype == VAX_BTYP_46) ? \
				 SPXg_REG1_ADDR_KA46 : SPXg_REG1_ADDR_KA49)
#define SPX_REG(reg)		regaddr[(reg - 0x2000) / 4]
#define SPXg_REG(reg)		regaddr[(reg - 0x2000) / 2]

#define SPX_REG1(reg)		regaddr1[(reg) / 4]
#define SPXg_REG1(reg)		regaddr1[(reg) / 4]

/* few SPX register names */
#define SPX_COMMAND		0x21ec
#define SPX_XSTART		0x21d0
#define SPX_YSTART		0x21d4
#define SPX_XEND		0x21d8
#define SPX_YEND		0x21dc
#define SPX_DSTPIX		0x21e0
#define SPX_DSTPIX1		0x20e0
#define SPX_SRCPIX		0x21e4
#define SPX_SRCPIX1		0x20e4
#define SPX_MAIN3		0x219c
#define SPX_STRIDE		0x21e8	/* SRC | DST */ 
#define SPX_SRCMASK		0x21f0
#define SPX_DSTMASK		0x21f4
#define SPX_FG			0x2260
#define SPX_BG			0x2264
#define SPX_DESTLOOP		0x2270
#define SPX_MPC			0x21fc

/* few SPX opcodes (microcode entry points); rasterop # = opcode ? */
#define SPX_OP_FILLRECT		0x19
#define	SPX_OP_COPYRECT		0x1a

/* bt459 locations */
#define SPX_BT459_ADDRL		0x39b10000
#define SPX_BT459_ADDRH		0x39b14000
#define SPX_BT459_REG		0x39b18000
#define SPX_BT459_CMAP		0x39b1c000

/* bt460 locations */
#define SPXg_BT460_BASE_KA46	0x200f0000
#define SPXg_BT460_BASE_KA49	0x2a000000
#define SPXg_BT460_BASE		((vax_boardtype == VAX_BTYP_46) ? \
				 SPXg_BT460_BASE_KA46 : SPXg_BT460_BASE_KA49)

#define	SPX_BG_COLOR		WS_DEFAULT_BG
#define	SPX_FG_COLOR		WS_DEFAULT_FG

/* 
 * cursor X = 0, Y = 0 on-screen bias
 * FIXME: BIAS for various HW types
 */
#define CUR_XBIAS		360
#define CUR_YBIAS		37

/* few BT459 & BT460 indirect register defines */
#define SPXDAC_REG_CCOLOR_1	0x181
#define SPXDAC_REG_CCOLOR_2	0x182
#define SPXDAC_REG_CCOLOR_3	0x183
#define SPXDAC_REG_ID		0x200
#define SPXDAC_REG_CMD0		0x201
#define SPXDAC_REG_CMD1		0x202
#define SPXDAC_REG_CMD2		0x203
#define SPXDAC_REG_PRM		0x204
#define SPXDAC_REG_CCR		0x300
#define SPXDAC_REG_CXLO		0x301
#define SPXDAC_REG_CXHI		0x302
#define SPXDAC_REG_CYLO		0x303
#define SPXDAC_REG_CYHI		0x304
#define SPXDAC_REG_WXLO		0x305
#define SPXDAC_REG_WXHI		0x306
#define SPXDAC_REG_WYLO		0x307
#define SPXDAC_REG_WYHI		0x308
#define SPXDAC_REG_WWLO		0x309
#define SPXDAC_REG_WWHI		0x30a
#define SPXDAC_REG_WHLO		0x30b
#define SPXDAC_REG_WHHI		0x30c
#define SPXDAC_REG_CRAM_BASE	0x400

/*
 * used to access top/bottom 8 bits of a 16-bit argument for split RAMDAC
 * addressing
 */
#define HI(x)	(((x) >> 8) & 0xff)
#define LO(x)	((x) & 0xff)

static	int	spx_match(device_t, cfdata_t, void *);
static	void	spx_attach(device_t, device_t, void *);

struct	spx_softc {
	device_t ss_dev;
};

CFATTACH_DECL_NEW(spx, sizeof(struct spx_softc),
		  spx_match, spx_attach, NULL, NULL);

static void	spx_cursor(void *, int, int, int);
static int	spx_mapchar(void *, int, unsigned int *);
static void	spx_putchar(void *, int, int, u_int, long);
static void	spx_copycols(void *, int, int, int, int);
static void	spx_erasecols(void *, int, int, int, long);
static void	spx_copyrows(void *, int, int, int);
static void	spx_eraserows(void *, int, int, long);
static int	spx_allocattr(void *, int, int, int, long *);
static int	spx_get_cmap(struct wsdisplay_cmap *);
static int	spx_set_cmap(struct wsdisplay_cmap *);

static int	spx_map_regs(device_t, u_int, u_int, u_int, u_int);
static void	SPX_render_font(void);
static void	SPXg_render_font(void);
static int	SPX_map_fb(device_t, struct vsbus_attach_args *);
static int	SPXg_map_fb(device_t, struct vsbus_attach_args *);
static void	spx_init_common(device_t, struct vsbus_attach_args *);
	
#define SPX_MAP_FB(self, va, type) if (! type ## _map_fb(self, va)) return   
#define SPX_MAP_REGS(self, type) if (!spx_map_regs(self,		\
						   type ## _REG_ADDR,	\
						   type ## _REG_SIZE,	\
						   type ## _REG1_ADDR,	\
						   type ## _REG1_SIZE)) \
					return

const struct wsdisplay_emulops spx_emulops = {
	spx_cursor,
	spx_mapchar,
	spx_putchar,
	spx_copycols,
	spx_erasecols,
	spx_copyrows,
	spx_eraserows,
	spx_allocattr
};

static char spx_stdscreen_name[10] = "160x128";
struct wsscreen_descr spx_stdscreen = {
	spx_stdscreen_name, 160, 128,		/* dynamically set */
	&spx_emulops,
	8, 15,					/* dynamically set */
	WSSCREEN_UNDERLINE|WSSCREEN_REVERSE|WSSCREEN_WSCOLORS,
};

const struct wsscreen_descr *_spx_scrlist[] = {
	&spx_stdscreen,
};

const struct wsscreen_list spx_screenlist = {
	sizeof(_spx_scrlist) / sizeof(struct wsscreen_descr *),
	_spx_scrlist,
};

static volatile char *spxaddr;
static volatile long *regaddr;
static volatile long *regaddr1;

static volatile char *bt_addrl;
static volatile char *bt_addrh;
static volatile char *bt_reg;
static volatile char *bt_cmap;
static volatile char *bt_wait; /* SPXg/gt only */

static int	spx_xsize;
static int	spx_ysize;
static int	spx_depth;
static int	spx_cols;
static int	spx_rows;
static long	spx_fb_size;

/* Our current hardware colormap */
static struct hwcmap256 {
#define	CMAP_SIZE	256	/* 256 R/G/B entries */
	u_int8_t r[CMAP_SIZE];
	u_int8_t g[CMAP_SIZE];
	u_int8_t b[CMAP_SIZE];
} spx_cmap;

/* The default colormap */
static struct {
	u_int8_t r[8];
	u_int8_t g[8];
	u_int8_t b[8];
} spx_default_cmap = {
	{ 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff },
	{ 0x00, 0x00, 0xff, 0x7f, 0x00, 0x00, 0xff, 0xff },
	{ 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff }
};

static char fb_type = 0;
static char spxg_current_page = 0;
#define spxg_switch_page(nr) (SPXg_REG1(0x20) = ((1 << (nr) & 0xff)) << 16)

struct wsdisplay_font spx_font;
static u_char *qf;
static u_short *qf2;

#define QCHAR_INVALID(c)     (((c) < spx_font.firstchar) ||		\
			      ((c) >= spx_font.firstchar + spx_font.numchars))
#define QCHAR(c)	     (QCHAR_INVALID(c) ? 0 : (c) - spx_font.firstchar)

#define QFONT_INDEX(c, line) (QCHAR(c) * spx_font.fontheight + line)
#define QFONT_QF(c, line)    qf[QFONT_INDEX(c, line)]
#define QFONT_QF2(c, line)   qf2[QFONT_INDEX(c, line)]
#define QFONT(c, line)       (spx_font.stride == 2 ? \
			      QFONT_QF2(c, line) :   \
			      QFONT_QF(c, line))

/* replicate color value */
#define COLOR(x) (((x) << 24) | ((x) << 16) | ((x) << 8) | (x))

/* convert coordinates into linear offset */
#define LINEAR(x, y) ((y * spx_xsize) + x)

/* Linear pixel address */
#define SPX_POS(row, col, line, dot)		   \
	((col) * spx_font.fontwidth +		   \
	 (row) * spx_font.fontheight * spx_xsize + \
	 (line) * spx_xsize + dot)

#define	SPX_ADDR(row, col, line, dot) spxaddr[SPX_POS(row, col, line, dot)]

/* Recalculate absolute pixel address from linear address */
#define SPXg_ADDR(linear) spxaddr[((((linear) % SPXg_WIN_LINEAR) / 4) * 8) + \
				  (((linear) % SPXg_WIN_LINEAR) % 4)]


static int	spx_ioctl(void *, void *, u_long, void *, int, struct lwp *);
static paddr_t	spx_mmap(void *, void *, off_t, int);
static int	spx_alloc_screen(void *, const struct wsscreen_descr *,
				      void **, int *, int *, long *);
static void	spx_free_screen(void *, void *);
static int	spx_show_screen(void *, void *, int, void (*) (void *, int, int),
				void *);

static void	spx_update_cmap(int entry, char red, char green, char blue);
static void	set_btreg(u_int addr, u_char value);
static u_char	get_btreg(u_int addr);

/* SPXg/gt delay */
static void	spxg_delay(void);

/*
 * SPX HW accelerated block copy
 * common code in spx_blkcpy,
 * HW-specific code accessed through spx_blkcpy_func pointer
 */
static void	spx_blkcpy(u_int sxpos, u_int sypos,
			   u_int dxpos, u_int dypos,
			   u_int xdim, u_int ydim);

static void	SPX_blkcpy(u_int sxpos, u_int sypos,
			   u_int dxpos, u_int dypos,
			   u_int xdim, u_int ydim,
			   char direction);
static void	SPXg_blkcpy(u_int sxpos, u_int sypos,
			    u_int dxpos, u_int dypos,
			    u_int xdim, u_int ydim,
			    char direction);
static void	(*spx_blkcpy_func)(u_int, u_int,
				   u_int, u_int,
				   u_int, u_int,
				   char);

/* SPX HW accelerated block set, no common code */
static void	SPX_blkset(u_int xpos, u_int ypos,
			   u_int xdim, u_int ydim, char color);
static void	SPXg_blkset(u_int xpos, u_int ypos,
			    u_int xdim, u_int ydim, char color);
static void	(*spx_blkset_func)(u_int, u_int, u_int, u_int, char);
#define spx_blkset(x, y, xd, yd, c) spx_blkset_func(x, y, xd, yd, c)

/* SPX HW accelerated part of spx_putchar */
static void	SPX_putchar(int, int, u_int, char, char);
static void	SPXg_putchar(int, int, u_int, char, char);
static void	(*spx_putchar_func)(int, int, u_int, char, char);

const struct wsdisplay_accessops spx_accessops = {
	spx_ioctl,
	spx_mmap,
	spx_alloc_screen,
	spx_free_screen,
	spx_show_screen,
	0 /* load_font */
};

/* TODO allocate ss_image dynamically for consoles beyond first one */
struct	spx_screen {
	int	ss_curx;
	int	ss_cury;
	int	ss_cur_fg;
	int	ss_cur_bg;
	struct {
		u_char	data;	/* Image character */
		u_char	attr;	/* Attribute: 80/70/08/07 */
	} ss_image[160 * 128];	/* allow for maximum possible cell matrix */
};

#define	SPX_ATTR_UNDERLINE	0x80
#define	SPX_BG_MASK		0x70
#define	SPX_ATTR_REVERSE	0x08
#define	SPX_FG_MASK		0x07

static	struct spx_screen spx_conscreen;
static	struct spx_screen *prevscr, *curscr;
static	struct spx_screen *savescr;

static	int cur_fg, cur_bg;
static	int spx_off = 1;

static void
spx_update_cmap(int entry, char red, char green, char blue)
{
	*bt_addrl = LO(entry);
	*bt_addrh = HI(entry);
	if ((entry >= 0x181) && (entry <= 0x183)) {
		*bt_reg = red;
		*bt_reg = green;
		*bt_reg = blue;
	} else {
		*bt_cmap = red;
		*bt_cmap = green;
		*bt_cmap = blue;
	}
}

static void
set_btreg(u_int addr, u_char value)
{
	*bt_addrl = LO(addr);
	*bt_addrh = HI(addr);
	*bt_reg = value;
}

static u_char
get_btreg(u_int addr)
{
	*bt_addrl = LO(addr);
	*bt_addrh = HI(addr);
	return *bt_reg & 0xff;
}

static void
spxg_delay(void)
{
	char i;
	
	for (i = 0; i <= SPXg_DELAY; i++)
		*bt_wait = *bt_wait + i;
}

static void
SPX_blkcpy(u_int sxpos, u_int sypos,
	   u_int dxpos, u_int dypos,
	   u_int xdim, u_int ydim,
	   char direction)
{
	u_int counter = 0xffffe;
	long temp = 0;

	SPX_REG(SPX_COMMAND) = 0x84884648 | direction; /* 0x848c4648? */
	SPX_REG(SPX_XSTART) = dxpos << 16;
	SPX_REG(SPX_YSTART) = dypos << 16;
	SPX_REG(SPX_XEND) = (dxpos + xdim) << 16;
	SPX_REG(SPX_YEND) = (dypos + ydim) << 16;
	
	temp = ((SPX_REG1(0x10) & 0xc0) << (30 - 6)) |		\
		((SPX_REG1(0x10) & 0x3f) << 24);
	
	SPX_REG(SPX_DSTPIX) = temp + LINEAR(dxpos, dypos);
	SPX_REG(SPX_SRCPIX) = temp + LINEAR(sxpos, sypos);
	SPX_REG(SPX_SRCPIX1) = 0;
	SPX_REG(SPX_STRIDE) = (spx_xsize << 16) | spx_xsize;
	SPX_REG(SPX_SRCMASK) = 0xff;
	SPX_REG(SPX_DSTMASK) = 0xff;
	
	SPX_REG(SPX_DESTLOOP) = 0x00112003;
	SPX_REG(SPX_MPC) = 0x2000 | SPX_OP_COPYRECT;
	
	SPX_REG1(0x18) = 0xffffffff;
	while ((counter) && ((SPX_REG1(0x18) & 2) == 0))
		counter--;
}

static void
SPXg_blkcpy(u_int sxpos, u_int sypos,
	    u_int dxpos, u_int dypos,
	    u_int xdim, u_int ydim,
	    char direction)
{
	u_int counter = 0xffffe;
	long temp = 0;

	SPXg_REG(SPX_COMMAND) = 0x84884648 | direction; spxg_delay();
	SPXg_REG(SPX_XSTART) = dxpos << 16; spxg_delay();
	SPXg_REG(SPX_YSTART) = dypos << 16; spxg_delay();
	SPXg_REG(SPX_XEND) = (dxpos + xdim) << 16; spxg_delay();
	SPXg_REG(SPX_YEND) = (dypos + ydim) << 16; spxg_delay();

	temp = 0x3f << 24;

	SPXg_REG(SPX_DSTPIX) = temp + LINEAR(dxpos, dypos); spxg_delay();
	SPXg_REG(SPX_DSTPIX1) = 0xff000000; spxg_delay();
	SPXg_REG(SPX_SRCPIX) = temp + LINEAR(sxpos, sypos); spxg_delay();
	SPXg_REG(SPX_SRCPIX1) = 0; spxg_delay();
	SPXg_REG(SPX_STRIDE) = (spx_xsize << 16) | spx_xsize; spxg_delay();
	SPXg_REG(SPX_SRCMASK) = 0xffffffff; spxg_delay();
	SPXg_REG(SPX_DSTMASK) = 0xffffffff; spxg_delay();

	SPXg_REG(SPX_DESTLOOP) = 0x00112003; spxg_delay();
	SPXg_REG(SPX_MPC) = 0x2000 | SPX_OP_COPYRECT; spxg_delay();

	while ((counter) && ((SPXg_REG1(0x0) & 0x8000) == 0))
		counter--;
}

static void
spx_blkcpy(u_int sxpos, u_int sypos,
	   u_int dxpos, u_int dypos,
	   u_int xdim, u_int ydim)
{
	char direction = 0;

	/* don't waste time checking whether src and dest actually overlap */
	if (sxpos < dxpos) {
		direction |= 0x01; /* X decrement */
		sxpos += xdim;
		dxpos += xdim;
		xdim = -xdim;
	}
	if (sypos < dypos) {
		direction |= 0x02; /* Y decrement */
		sypos += ydim;
		dypos += ydim;
		ydim = -ydim;
	}

	spx_blkcpy_func(sxpos, sypos, dxpos, dypos, xdim, ydim, direction);
}

static void
SPX_blkset(u_int xpos, u_int ypos, u_int xdim, u_int ydim, char color)
{
	u_int counter = 0xfffe;
	long temp = 0;

	SPX_REG(SPX_COMMAND) = 0x84884608;
	SPX_REG(SPX_XSTART) = xpos << 16;
	SPX_REG(SPX_YSTART) = ypos << 16;
	SPX_REG(SPX_XEND) = (xpos + xdim) << 16;
	SPX_REG(SPX_YEND) = (ypos + ydim) << 16;
	SPX_REG(SPX_STRIDE) = (spx_xsize << 16) | spx_xsize;
	SPX_REG(SPX_DESTLOOP) = 0x20102003;

	temp = ((SPX_REG1(0x10) & 0xc0) << (30 - 6)) | 
		((SPX_REG1(0x10) & 0x3f) << 24);
	SPX_REG(SPX_DSTPIX) = temp + LINEAR(xpos, ypos);

	SPX_REG(SPX_FG) = COLOR(color);

	SPX_REG(SPX_MPC) = 0x2000 | SPX_OP_FILLRECT;

	SPX_REG1(0x18) = 0xffffffff;
	while ((counter) && ((SPX_REG1(0x18) & 2) == 0))
		counter--;
}

static void
SPXg_blkset(u_int xpos, u_int ypos, u_int xdim, u_int ydim, char color)
{
	u_int counter = 0xfffe;
	long temp = 0;

	SPXg_REG(SPX_COMMAND) = 0x84884608; spxg_delay();
	SPXg_REG(SPX_XSTART) = xpos << 16; spxg_delay();
	SPXg_REG(SPX_YSTART) = ypos << 16; spxg_delay();
	SPXg_REG(SPX_XEND) = (xpos + xdim) << 16; spxg_delay();
	SPXg_REG(SPX_YEND) = (ypos + ydim) << 16; spxg_delay();
	SPXg_REG(SPX_STRIDE) = (spx_xsize << 16) | spx_xsize; spxg_delay();

	temp = 0x3f << 24;
	SPXg_REG(SPX_DSTPIX) = temp + LINEAR(xpos, ypos); spxg_delay();
	SPXg_REG(SPX_DSTPIX1) = 0xff000000; spxg_delay();

	SPXg_REG(SPX_FG) = COLOR(color); spxg_delay();

	SPXg_REG(SPX_DESTLOOP) = 0x20102003; spxg_delay();
	SPXg_REG(SPX_MPC) = 0x2000 | SPX_OP_FILLRECT; spxg_delay();

	while ((counter) && ((SPXg_REG1(0x0) & 0x8000) == 0))
		counter--;
}

int
spx_match(device_t parent, cfdata_t match, void *aux)
{
	struct vsbus_softc *sc = device_private(parent);
#if 0
	struct vsbus_attach_args *va = aux;
	char *ch = (char *)va->va_addr;
#endif

/*
 * FIXME:
 * add KA46 when/if ever somebody reports SPXg vax_confdata ID on VS 4000/60
 * Ditto for SPX ID on KA42 & KA43
 */
	if ((vax_boardtype != VAX_BTYP_49) ||
	    ((vax_confdata & (CONF_LCSPX | CONF_SPXg)) == 0))
		return 0;

/* FIXME add RAMDAC ID code ??? */
#if 0
	/* 
	 * FIXME:
	 * are memory addresses besides va->va_addr off limits at this time ?
	 * RAMDAC ID register test, should read 0x4a for LCSPX, 0x4b for SPXg
	 */
	if (get_btreg(SPXDAC_REG_ID) != 0x4a)
		return 0;
#endif

	sc->sc_mask = 0x04; /* XXX - should be generated */
	scb_fake(0x14c, 0x15);

	return 20;
}

static void
spx_attach(device_t parent, device_t self, void *aux)
{
	struct spx_softc *sc = device_private(self);
	struct vsbus_attach_args *va = aux;
	struct wsemuldisplaydev_attach_args aa;

	sc->ss_dev = self;

	aprint_normal("\n");
	aa.console = spxaddr != NULL;

	spx_init_common(self, va);

	/* display FB type based on RAMDAC ID */
	switch (get_btreg(SPXDAC_REG_ID) & 0xff) {
	case 0x4a:
		aprint_normal_dev(self,
			"RAMDAC ID: 0x%x, Bt459 (SPX/LCSPX) RAMDAC type\n",
			get_btreg(SPXDAC_REG_ID) & 0xff);
		break;
			
	case 0x4b:
		aprint_normal_dev(self,
			"RAMDAC ID: 0x%x, Bt460 (SPXg) RAMDAC type\n", 
			get_btreg(SPXDAC_REG_ID) & 0xff);
		break;
	default:
		aprint_error_dev(self, "unknown RAMDAC type 0x%x\n",
			get_btreg(SPXDAC_REG_ID) & 0xff);
	}

	curscr = &spx_conscreen;
	prevscr = curscr;

	aa.scrdata = &spx_screenlist;
	aa.accessops = &spx_accessops;

	/* enable cursor */
	set_btreg(SPXDAC_REG_CCR, 0xc1);

	config_found(self, &aa, wsemuldisplaydevprint);
}

static	int cur_on;

static void
spx_cursor(void *id, int on, int row, int col)
{
	struct spx_screen *ss = id;
	int attr, data __unused;

	attr = ss->ss_image[row * spx_cols + col].attr;
	data = ss->ss_image[row * spx_cols + col].data;

	if (attr & SPX_ATTR_REVERSE) {
		cur_bg = attr & SPX_FG_MASK;
		cur_fg = (attr & SPX_BG_MASK) >> 4;
	} else {
		cur_fg = attr & SPX_FG_MASK;
		cur_bg = (attr & SPX_BG_MASK) >> 4;
	}

	if (ss == curscr) {
#ifdef SOFTCURSOR
		if ((cur_on = on))
			spx_putchar_func(row, col, data, cur_bg, cur_fg);
		else
			spx_putchar_func(row, col, data, cur_fg, cur_bg);
#else
		/* change cursor foreground color colormap entry */
		spx_update_cmap(SPXDAC_REG_CCOLOR_3, 
				spx_cmap.r[cur_fg],
				spx_cmap.g[cur_fg],
				spx_cmap.b[cur_fg]);
		
		/* update cursor position registers */
		set_btreg(SPXDAC_REG_CXLO,
			  LO(col * spx_font.fontwidth + CUR_XBIAS));
		set_btreg(SPXDAC_REG_CXHI,
			  HI(col * spx_font.fontwidth + CUR_XBIAS));
		set_btreg(SPXDAC_REG_CYLO,
			  LO(row * spx_font.fontheight + CUR_YBIAS));
		set_btreg(SPXDAC_REG_CYHI,
			  HI(row * spx_font.fontheight + CUR_YBIAS));
		
		if ((cur_on = on))
			/* enable cursor */
			set_btreg(SPXDAC_REG_CCR, 0xc1);
		else
			/* disable cursor */
			set_btreg(SPXDAC_REG_CCR, 0x01);
#endif
	}

	ss->ss_curx = col;
	ss->ss_cury = row;
	ss->ss_cur_bg = cur_bg;
	ss->ss_cur_fg = cur_fg;
}

static int
spx_mapchar(void *id, int uni, unsigned int *index)
{
	if (uni < 256) {
		*index = uni;
		return (5);
	}
	*index = ' ';
	return (0);
}

static void
SPX_putchar(int row, int col, u_int c, char dot_fg, char dot_bg)
{
	u_int counter = 0xffffe;
	long temp = 0;

	SPX_REG(SPX_FG) = COLOR(dot_fg);
	SPX_REG(SPX_BG) = COLOR(dot_bg);
	SPX_REG(SPX_MAIN3) = 0x01010101;
	SPX_REG(SPX_COMMAND) = 0x84884648;
	SPX_REG(SPX_XSTART) = (col * spx_font.fontwidth) << 16;
	SPX_REG(SPX_YSTART) = (row * spx_font.fontheight) << 16;
	SPX_REG(SPX_XEND) = ((col + 1) * spx_font.fontwidth) << 16;
	SPX_REG(SPX_YEND) = ((row + 1) * spx_font.fontheight) << 16;

	temp = ((SPX_REG1(0x10) & 0xc0) << (30 - 6)) |	\
		((SPX_REG1(0x10) & 0x3f) << 24);

	SPX_REG(SPX_DSTPIX) = temp + LINEAR(col * spx_font.fontwidth,
					    row * spx_font.fontheight);
	SPX_REG(SPX_SRCPIX) = temp + FONT_STORAGE_START
		+ (c * spx_font.fontheight * spx_font.fontwidth);
	SPX_REG(SPX_SRCPIX1) = 0;
	SPX_REG(SPX_STRIDE) = (spx_font.fontwidth << 16) | spx_xsize;
	SPX_REG(SPX_SRCMASK) = 0xff;
	SPX_REG(SPX_DSTMASK) = 0xff;
	
	SPX_REG(SPX_DESTLOOP) = 0x20142003;
	SPX_REG(SPX_MPC) = 0x2000 | SPX_OP_COPYRECT;
	
	SPX_REG1(0x18) = 0xffffffff;

	while ((counter) && ((SPX_REG1(0x18) & 2) == 0))
		counter--;
}

static void
SPXg_putchar(int row, int col, u_int c, char dot_fg, char dot_bg)
{
	u_int counter = 0xffffe;
	long temp = 0;

	SPXg_REG(SPX_FG) = COLOR(dot_fg);
	spxg_delay();
	SPXg_REG(SPX_BG) = COLOR(dot_bg);
	spxg_delay();
	SPXg_REG(SPX_MAIN3) = 0x01010101;
	spxg_delay();
	SPXg_REG(SPX_COMMAND) = 0x84884648;
	spxg_delay();
	SPXg_REG(SPX_XSTART) = (col * spx_font.fontwidth) << 16;
	spxg_delay();
	SPXg_REG(SPX_YSTART) = (row * spx_font.fontheight) << 16;
	spxg_delay();
	SPXg_REG(SPX_XEND) = ((col + 1) * spx_font.fontwidth) << 16;
	spxg_delay();
	SPXg_REG(SPX_YEND) = ((row + 1) * spx_font.fontheight) << 16;
	spxg_delay();

	temp = 0x3f << 24;

	SPXg_REG(SPX_DSTPIX) = temp + LINEAR(col * spx_font.fontwidth,
					     row * spx_font.fontheight);
	spxg_delay();
	SPXg_REG(SPX_DSTPIX1) = 0xff000000;
	spxg_delay();
	SPXg_REG(SPX_SRCPIX) = temp + FONT_STORAGE_START +
		(c * spx_font.fontheight * spx_font.fontwidth);
	spxg_delay();
	SPXg_REG(SPX_SRCPIX1) = 0;
	spxg_delay();
	SPXg_REG(SPX_STRIDE) = (spx_font.fontwidth << 16) | spx_xsize;
	spxg_delay();
	SPXg_REG(SPX_SRCMASK) = 0xffffffff;
	spxg_delay();
	SPXg_REG(SPX_DSTMASK) = 0xffffffff;
	spxg_delay();

	SPXg_REG(SPX_DESTLOOP) = 0x20142003;
	spxg_delay();
	SPXg_REG(SPX_MPC) = 0x2000 | SPX_OP_COPYRECT;
	spxg_delay();

	while ((counter) && ((SPXg_REG1(0x0) & 0x8000) == 0))
		counter--;
}

static void
spx_putchar(void *id, int row, int col, u_int c, long attr)
{
	struct spx_screen *ss = id;
	char dot_fg, dot_bg;

	c &= 0xff;

	ss->ss_image[row * spx_cols + col].data = c;
	ss->ss_image[row * spx_cols + col].attr = attr;
	if (ss != curscr)
		return;

	dot_fg = attr & SPX_FG_MASK;
	dot_bg = (attr & SPX_BG_MASK) >> 4;
	if (attr & SPX_ATTR_REVERSE) {
		dot_fg = (attr & SPX_BG_MASK) >> 4;
		dot_bg = attr & SPX_FG_MASK;
	}

	/* adjust glyph index for underlined text */
	if (attr & SPX_ATTR_UNDERLINE)
		c += 0x100;

	spx_putchar_func(row, col, c, dot_fg, dot_bg);
}

/*
 * copies columns inside a row.
 */
static void
spx_copycols(void *id, int row, int srccol, int dstcol, int ncols)
{
	struct spx_screen *ss = id;

	bcopy(&ss->ss_image[row * spx_cols + srccol],
	      &ss->ss_image[row * spx_cols + dstcol],
	      ncols * sizeof(ss->ss_image[0]));

	if (ss == curscr)
		spx_blkcpy(srccol * spx_font.fontwidth,
			   row * spx_font.fontheight,
			   dstcol * spx_font.fontwidth,
			   row * spx_font.fontheight,
			   ncols * spx_font.fontwidth,
			   spx_font.fontheight);
}

/*
 * Erases a bunch of chars inside one row.
 */
static void
spx_erasecols(void *id, int row, int startcol, int ncols, long fillattr)
{
	struct spx_screen *ss = id;
	int offset = row * spx_cols + startcol;
	int i;

	memset(&ss->ss_image[row * spx_cols + startcol], 0, 
	      ncols * sizeof(ss->ss_image[0]));

	for (i = offset; i < offset + ncols; ++i)
		ss->ss_image[i].attr = fillattr;

	if (ss == curscr)
		spx_blkset(startcol * spx_font.fontwidth,
			   row * spx_font.fontheight,
			   ncols * spx_font.fontwidth,
			   spx_font.fontheight,
			   (fillattr & SPX_BG_MASK) >> 4);
}

static void
spx_copyrows(void *id, int srcrow, int dstrow, int nrows)
{
	struct spx_screen *ss = id;

	bcopy(&ss->ss_image[srcrow * spx_cols],
	      &ss->ss_image[dstrow * spx_cols],
	      nrows * spx_cols * sizeof(ss->ss_image[0]));

	if (ss == curscr)
		spx_blkcpy(0, (srcrow * spx_font.fontheight),
			   0, (dstrow * spx_font.fontheight),
			   spx_xsize, (nrows * spx_font.fontheight));
}

static void
spx_eraserows(void *id, int startrow, int nrows, long fillattr)
{
	struct spx_screen *ss = id;
	int i;
	
	memset(&ss->ss_image[startrow * spx_cols], 0,
	      nrows * spx_cols * sizeof(ss->ss_image[0]));

	for (i = startrow * spx_cols; i < startrow + nrows * spx_cols; ++i)
		ss->ss_image[i].attr = fillattr;

	if (ss == curscr)
		spx_blkset(0, (startrow * spx_font.fontheight),
			   spx_xsize, (nrows * spx_font.fontheight),
			   (fillattr & SPX_BG_MASK) >> 4);
}

static int
spx_allocattr(void *id, int fg, int bg, int flags, long *attrp)
{
	long myattr;

	if (flags & WSATTR_WSCOLORS)
		myattr = (fg & SPX_FG_MASK) | ((bg << 4) & SPX_BG_MASK);
	else
		myattr = WSCOL_WHITE << 4;	/* XXXX */
	if (flags & WSATTR_REVERSE)
		myattr |= SPX_ATTR_REVERSE;
	if (flags & WSATTR_UNDERLINE)
		myattr |= SPX_ATTR_UNDERLINE;
	*attrp = myattr;
	return 0;
}

static int
spx_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
{
	struct wsdisplay_fbinfo *fb = data;
	int i;

	switch (cmd) {
	case WSDISPLAYIO_GTYPE:
		*(u_int *)data = WSDISPLAY_TYPE_SPX;
		break;

	case WSDISPLAYIO_GINFO:
		fb->height = spx_ysize;
		fb->width = spx_xsize;
		fb->depth = spx_depth;
		fb->cmsize = 1 << spx_depth;
		break;

	case WSDISPLAYIO_GETCMAP:
		return spx_get_cmap((struct wsdisplay_cmap *)data);

	case WSDISPLAYIO_PUTCMAP:
		return spx_set_cmap((struct wsdisplay_cmap *)data);

	case WSDISPLAYIO_GMODE:
		return EPASSTHROUGH;

	case WSDISPLAYIO_SMODE:
		/*
		 * if setting WSDISPLAYIO_MODE_EMUL, restore console cmap,
		 * current screen
		 */
		if (*(u_int *)data == WSDISPLAYIO_MODE_EMUL) {
			for (i = 0; i < 8; i++) {
				spx_cmap.r[i] = spx_default_cmap.r[i];
				spx_cmap.g[i] = spx_default_cmap.g[i];
				spx_cmap.b[i] = spx_default_cmap.b[i];
				spx_update_cmap(i, spx_cmap.r[i], spx_cmap.g[i],
						spx_cmap.b[i]);
			}
			if (savescr != NULL)
				spx_show_screen(NULL, savescr, 0, NULL, NULL);
			savescr = NULL;
		} else {		/* WSDISPLAYIO_MODE_MAPPED */
			savescr = curscr;
			curscr = NULL;
			/* clear screen? */
		}

		return EPASSTHROUGH;

	case WSDISPLAYIO_SVIDEO:
		if (*(u_int *)data == WSDISPLAYIO_VIDEO_ON && spx_off) {
			/* Unblank video */
			set_btreg(SPXDAC_REG_CMD0, 0x48);

			/* Enable sync */
			set_btreg(SPXDAC_REG_CMD2, 0xc0);

			spx_off = 0;
		} else if (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF && !spx_off) {
			/* Blank video */
			set_btreg(SPXDAC_REG_CMD0, 68);

			/* Disable sync */
			set_btreg(SPXDAC_REG_CMD2, 0x40);
			spx_off = 1;
		}
		break;

	case WSDISPLAYIO_GVIDEO:
		*(u_int *)data = spx_off == 0 ?
		WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
		break;

	case WSDISPLAYIO_LINEBYTES:
		*(u_int *)data = spx_xsize;
		break;

	default:
		return EPASSTHROUGH;
	}
	return 0;
}

/*
 * Bad idea on SPXg/gt due to windowed framebuffer access 
 */
static paddr_t
spx_mmap(void *v, void *vs, off_t offset, int prot)
{
	if (fb_type != FB_IS_SPX)
		return -1;

	if (offset >= spx_fb_size || offset < 0)
		return -1;

	return (SPX_FB_ADDR + offset) >> PGSHIFT;
}

static int
spx_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
		 int *curxp, int *curyp, long *defattrp)
{
	int i;
	struct spx_screen *ss;

	ss = malloc(sizeof(struct spx_screen), M_DEVBUF, M_WAITOK | M_ZERO);

	*cookiep = ss;
	*curxp = *curyp = 0;
	*defattrp = (SPX_BG_COLOR << 4) | SPX_FG_COLOR;

	for (i = 0; i < spx_cols * spx_rows; ++i)
		ss->ss_image[i].attr = *defattrp;

	return 0;
}

static void
spx_free_screen(void *v, void *cookie)
{
/* FIXME add something to actually free malloc()ed screen? */
}

static int
spx_show_screen(void *v, void *cookie, int waitok,
		void (*cb)(void *, int, int), void *cbarg)
{
	struct spx_screen *ss = cookie;
	u_int row, col, attr;
	u_char c;

	if (ss == curscr)
		return (0);

	prevscr = curscr;
	curscr = ss;

	for (row = 0; row < spx_rows; row++) {
		for (col = 0; col < spx_cols; col++) {
			u_int idx = row * spx_cols + col;
			attr = ss->ss_image[idx].attr;
			c = ss->ss_image[idx].data;

			/* check if cell requires updating */
			if ((c != prevscr->ss_image[idx].data) ||
			    (attr != prevscr->ss_image[idx].attr)) {
				if (c < 32) c = 32;
				spx_putchar(ss, row, col, c, attr);
			}
		}
	}

	row = ss->ss_cury; col = ss->ss_curx;

	spx_cursor(ss, cur_on, row, col);

	cur_fg = ss->ss_cur_fg;
	cur_bg = ss->ss_cur_bg;

	return (0);
}

static int
spx_get_cmap(struct wsdisplay_cmap *p)
{
	u_int index = p->index, count = p->count;
	int error;

	if (index >= (1 << spx_depth) || count > (1 << spx_depth) - index)
		return (EINVAL);

	error = copyout(&spx_cmap.r[index], p->red, count);
	if (error)
		return error;

	error = copyout(&spx_cmap.g[index], p->green, count);
	if (error)
		return error;

	error = copyout(&spx_cmap.b[index], p->blue, count);
	return error;
}

static int
spx_set_cmap(struct wsdisplay_cmap *p)
{
	u_int index = p->index, count = p->count;
	int error, s;

	if (index >= (1 << spx_depth) || count > (1 << spx_depth) - index)
		return (EINVAL);

	error = copyin(p->red, &spx_cmap.r[index], count);
	if (error)
		return error;

	error = copyin(p->green, &spx_cmap.g[index], count);
	if (error)
		return error;

	error = copyin(p->blue, &spx_cmap.b[index], count);

	if (error)
		return error;

	s = spltty();
	while (count-- > 0) {
		spx_update_cmap(index, 
				spx_cmap.r[index],
				spx_cmap.g[index],
				spx_cmap.b[index]);
		index++;
	}
	splx(s);
	return (0);
}

cons_decl(spx);

void
spxcninit(struct consdev *cndev)
{
	int i;

	spx_blkset(0, 0, spx_xsize, spx_ysize, SPX_BG_COLOR);

	curscr = &spx_conscreen;

	for (i = 0; i < spx_cols * spx_rows; i++)
		spx_conscreen.ss_image[i].attr =
			(SPX_BG_COLOR << 4) | SPX_FG_COLOR;
	wsdisplay_cnattach(&spx_stdscreen, &spx_conscreen, 0, 0,
			   (SPX_BG_COLOR << 4) | SPX_FG_COLOR);
	cn_tab->cn_pri = CN_INTERNAL;

#if NDZKBD > 0
	dzkbd_cnattach(0); /* Connect keyboard and screen together */
#endif
}

/*
 * Called very early to setup the glass tty as console.
 * Because it's called before the VM system is inited, virtual memory
 * for the framebuffer can be stolen directly without disturbing anything.
 */
void
spxcnprobe(struct consdev *cndev)
{
	extern const struct cdevsw wsdisplay_cdevsw;

	/* Only for VS 4000/90, 90A and 96 with LCSPX or SPXg/gt*/
	if ((vax_boardtype != VAX_BTYP_49) ||
	    ((vax_confdata & (CONF_LCSPX | CONF_SPXg)) == 0))
		return;

	if (((vax_confdata & 8) && (vax_boardtype == VAX_BTYP_49)) ||
	    (((vax_confdata & KA420_CFG_L3CON) ||
	      (vax_confdata & KA420_CFG_MULTU)) && 
	     ((vax_boardtype == VAX_BTYP_420) || 
	      (vax_boardtype == VAX_BTYP_43)))) {
		aprint_normal("spxcnprobe: Diagnostic console\n");
		return; /* Diagnostic console */
	}

	spx_init_common(NULL, NULL);

	cndev->cn_pri = CN_INTERNAL;
	cndev->cn_dev = makedev(cdevsw_lookup_major(&wsdisplay_cdevsw), 0);
}

static int
spx_map_regs(device_t self,
	     u_int raddr, u_int rsize, u_int r1addr, u_int r1size)
{
	extern vaddr_t virtual_avail;

	if (!self) {
		regaddr = (long*)virtual_avail;
		virtual_avail += rsize;
		ioaccess((vaddr_t)regaddr, raddr, rsize/VAX_NBPG);

		regaddr1 = (long*)virtual_avail;
		virtual_avail += r1size;
		ioaccess((vaddr_t)regaddr1, r1addr, r1size/VAX_NBPG);

		return 1;
	}

	regaddr = (long*)vax_map_physmem(raddr, rsize/VAX_NBPG);
	if (regaddr == 0) {
		aprint_error_dev(self,
				 "unable to allocate register memory (1).\n");
		return 0;
	}

	regaddr1 = (long*)vax_map_physmem(r1addr, r1size/VAX_NBPG);
	if (regaddr1 == 0) {
		aprint_error_dev(self,
				 "unable to allocate register memory (2).\n");
		return 0;
	}

	return 1;
}

static int
SPX_map_fb(device_t self, struct vsbus_attach_args *va)
{
	int fbsize = (spx_fb_size + FONT_STORAGE_SIZE)/VAX_NBPG;
	extern vaddr_t virtual_avail;

	if (!self) {
		spxaddr = (char *)virtual_avail;
		virtual_avail += (spx_fb_size + FONT_STORAGE_SIZE);
		ioaccess((vaddr_t)spxaddr, SPX_FB_ADDR, fbsize);

		bt_addrl = (char *)virtual_avail;
		virtual_avail += VAX_NBPG;
		ioaccess((vaddr_t)bt_addrl, SPX_BT459_ADDRL, 1);

		bt_addrh = (char *)virtual_avail;
		virtual_avail += VAX_NBPG;
		ioaccess((vaddr_t)bt_addrh, SPX_BT459_ADDRH, 1);

		bt_reg = (char *)virtual_avail;
		virtual_avail += VAX_NBPG;
		ioaccess((vaddr_t)bt_reg, SPX_BT459_REG, 1);

		bt_cmap = (char *)virtual_avail;
		virtual_avail += VAX_NBPG;
		ioaccess((vaddr_t)bt_cmap, SPX_BT459_CMAP, 1);

		return 1;
	}

	spxaddr = (char *)vax_map_physmem(va->va_paddr, fbsize);
	if (spxaddr == 0) {
		aprint_error_dev(self, "unable to map framebuffer memory.\n");
		return 0;
	}

	/* map all four RAMDAC registers */
	bt_addrl = (char *)vax_map_physmem(SPX_BT459_ADDRL, 1);
	if (bt_addrl == 0) {
		aprint_error_dev(self,
				 "unable to map BT459 Low Address register.\n");
		return 0;
	}

	bt_addrh = (char *)vax_map_physmem(SPX_BT459_ADDRH, 1);
	if (bt_addrh == 0) {
		aprint_error_dev(self,
				 "unable to map BT459 High Address register.\n");
		return 0;
	}

	bt_reg = (char *)vax_map_physmem(SPX_BT459_REG, 1);
	if (bt_reg == 0) {
		aprint_error_dev(self,
				 "unable to map BT459 I/O Register.\n");
		return 0;
	}

	bt_cmap = (char *)vax_map_physmem(SPX_BT459_CMAP, 1);
	if (bt_cmap == 0) {
		aprint_error_dev(self,
				 "unable to map BT459 Color Map register.\n");
		return 0;
	}

	return 1;
}

static int
SPXg_map_fb(device_t self, struct vsbus_attach_args *va)
{
	int fbsize = SPXg_WIN_SIZE/VAX_NBPG;
	extern vaddr_t virtual_avail;

	if (!self) {
		spxaddr = (char *)virtual_avail;
		virtual_avail += SPXg_WIN_SIZE;
		ioaccess((vaddr_t)spxaddr, SPXg_FB_ADDR, fbsize);

		bt_addrl = (char *)virtual_avail;
		virtual_avail += VAX_NBPG;
		ioaccess((vaddr_t)bt_addrl, SPXg_BT460_BASE, 1);

		bt_addrh = bt_addrl + 0x04;
		bt_reg = bt_addrl + 0x08;
		bt_cmap = bt_addrl + 0x0c;
		bt_wait = bt_addrl + 0x34;

		return 1;
	}

	spxaddr = (char *)vax_map_physmem(va->va_paddr, fbsize);
	if (spxaddr == 0) {
		aprint_error_dev(self,
				 "unable to map framebuffer memory window.\n");
		return 0;
	}
	
	bt_addrl = (char *)vax_map_physmem(SPXg_BT460_BASE, 1);
	if (bt_addrl == 0) {
		aprint_error_dev(self,
				 "unable to map BT460 Low Address register.\n");
		return 0;
	}
	bt_addrh = bt_addrl + 0x04;
	bt_reg   = bt_addrl + 0x08;
	bt_cmap  = bt_addrl + 0x0c;
	bt_wait  = bt_addrl + 0x34;

	return 1;
}

static void
SPX_render_font(void)
{
	volatile char *fontaddr = spxaddr + FONT_STORAGE_START;
	int i, j, k, ch, pixel;

	for (i = 0; i < 256; i++) for (j = 0; j < spx_font.fontheight; j++) {
		ch = QFONT(i, j);

		/* regular characters */
		pixel = ((i * spx_font.fontheight)+ j) * spx_font.fontwidth;
		for (k = 0; k < spx_font.fontwidth; k++)
			if (ch & (1 << k))
				fontaddr[pixel++] = 0xff;
			else 
				fontaddr[pixel++] = 0x00;
		
		/* underlined characters */
		pixel = (((i + 256) * spx_font.fontheight) + j)
			* spx_font.fontwidth;
		for (k = 0; k < spx_font.fontwidth; k++)
			if ((ch & (1 << k)) || (j == (spx_font.fontheight - 1)))
				fontaddr[pixel++] = 0xff;
			else
				fontaddr[pixel++] = 0x00;
	}
}

static void
SPXg_render_font(void)
{
	int i, j, k, ch, pixel;

	for (i = 0; i < 256; i++) for (j = 0; j < spx_font.fontheight; j++) {
		ch = QFONT(i, j);
		/* regular characters */
		for (k = 0; k < spx_font.fontwidth; k++) {
			pixel = FONT_STORAGE_START
				+ (((i * spx_font.fontheight) + j)
				   * spx_font.fontwidth) + k;
			if ((pixel / SPXg_WIN_LINEAR) != spxg_current_page) {
				spxg_switch_page(pixel / SPXg_WIN_LINEAR);
				spxg_current_page = (pixel / SPXg_WIN_LINEAR);
			}
			SPXg_ADDR(pixel) = ch & (1 << k) ? 0xff : 0x00;
			spxg_delay();
		}

		/* underlined characters */
		for (k = 0; k < spx_font.fontwidth; k++) {
			pixel = FONT_STORAGE_START
				+ ((((i + 256) * spx_font.fontheight) + j)
				   * spx_font.fontwidth) + k;
			if ((pixel / SPXg_WIN_LINEAR) != spxg_current_page) {
				spxg_switch_page(pixel / SPXg_WIN_LINEAR);
				spxg_current_page = (pixel / SPXg_WIN_LINEAR);
			}
			if ((ch & (1 << k)) || (j == (spx_font.fontheight - 1)))
				SPXg_ADDR(pixel) = 0xff;
			else
				SPXg_ADDR(pixel) = 0x00;
			spxg_delay();
		}
	}
}

static void
spx_init_common(device_t self, struct vsbus_attach_args *va)
{
	u_int i, j, k;
	int cookie;
	struct wsdisplay_font *wf;
	static int init_done;

	if (init_done)
		return;

	/* KA49: Identify framebuffer type */
	switch (vax_confdata & (CONF_LCSPX | CONF_SPXg)) {
	case CONF_LCSPX:
		fb_type = FB_IS_SPX;
		spx_blkcpy_func = SPX_blkcpy;
		spx_blkset_func = SPX_blkset;
		spx_putchar_func = SPX_putchar;
		break;
	case CONF_SPXg:
		fb_type = FB_IS_SPXg;
		spx_blkcpy_func = SPXg_blkcpy;
		spx_blkset_func = SPXg_blkset;
		spx_putchar_func = SPXg_putchar;
		break;
	case CONF_LCSPX | CONF_SPXg:
		panic("spxcnprobe: incorrect FB configuration\n");
		break;
	}

	/* map SPX registers first */
	if (fb_type == FB_IS_SPX) {
		SPX_MAP_REGS(self, SPX);
	} else if(fb_type == FB_IS_SPXg) {
		SPX_MAP_REGS(self, SPXg);
	}

	if (fb_type == FB_IS_SPXg)
		spxg_switch_page(0);

	/* any KA42/43 SPX resolution detection code should be placed here */
	spx_depth = 8;
	spx_xsize = 1280;
	spx_ysize = 1024;

	wsfont_init();

	cookie = wsfont_find(NULL, 12, 21, 0, WSDISPLAY_FONTORDER_R2L,
			     WSDISPLAY_FONTORDER_L2R, WSFONT_FIND_BITMAP);
	if (cookie == -1)
		cookie = wsfont_find(NULL, 16, 0, 0, WSDISPLAY_FONTORDER_R2L, 0,
		    WSFONT_FIND_BITMAP);
	if (cookie == -1)
		cookie = wsfont_find(NULL, 12, 0, 0, WSDISPLAY_FONTORDER_R2L, 0,
		    WSFONT_FIND_BITMAP);
	if (cookie == -1)
		cookie = wsfont_find(NULL, 8, 0, 0, WSDISPLAY_FONTORDER_R2L, 0,
		    WSFONT_FIND_BITMAP);
	if (cookie == -1)
		cookie = wsfont_find(NULL, 0, 0, 0, WSDISPLAY_FONTORDER_R2L,
		    WSDISPLAY_FONTORDER_L2R, WSFONT_FIND_BITMAP);

	if (cookie == -1 || wsfont_lock(cookie, &wf))
		panic("spx_common_init: unable to load console font");

	spx_font = *wf;

	if (self != NULL) {
		aprint_normal_dev(self, "Using %s %dx%d font\n", wf->name,
				  spx_font.fontwidth, spx_font.fontheight);
	}

	spx_cols = spx_xsize / spx_font.fontwidth;
	spx_rows = spx_ysize / spx_font.fontheight;
	spx_fb_size = spx_xsize * spx_ysize;
	spx_stdscreen.ncols = spx_cols;
	spx_stdscreen.nrows = spx_rows;
	spx_stdscreen.fontwidth = spx_font.fontwidth;
	spx_stdscreen.fontheight = spx_font.fontheight;
	snprintf(spx_stdscreen_name, sizeof(spx_stdscreen_name),
            "%dx%d", spx_cols, spx_rows);

	/* for SPXg spx_fb_size represents FB window size, not FB length */
	if (fb_type == FB_IS_SPXg)
		spx_fb_size = SPXg_WIN_SIZE;

	qf = spx_font.data;
	qf2 = (u_short *)spx_font.data;

	if (fb_type == FB_IS_SPX) {
		SPX_MAP_FB(self, va, SPX);
	} else if (fb_type == FB_IS_SPXg) {
		SPX_MAP_FB(self, va, SPXg);
	}

	/* render font glyphs to off-screen memory */
	if (fb_type == FB_IS_SPX)
		SPX_render_font();
	else if (fb_type == FB_IS_SPXg)
		SPXg_render_font();

	/* set up default console color palette */
	for (i = 0; i < 8; i++) {
		spx_cmap.r[i] = spx_default_cmap.r[i];
		spx_cmap.g[i] = spx_default_cmap.g[i];
		spx_cmap.b[i] = spx_default_cmap.b[i];
		spx_update_cmap(i, spx_cmap.r[i], spx_cmap.g[i], spx_cmap.b[i]);
	}
	
	/*
	 * Build 64x64 console cursor bitmap based on font dimensions.
	 * Palette colors are not used, but 4 pixels 2bpp of cursor sprite,
	 * fg/bg respectively.
	 */
	for (i = 0; i < 1024; i++)
		set_btreg(SPXDAC_REG_CRAM_BASE + i, 0);

	/* build cursor line, 1 to 3 pixels high depending on font height */
	if (spx_font.fontheight > 26)
		i = spx_font.fontheight - 3;
	else if (spx_font.fontheight > 16)
		i = spx_font.fontheight - 2;
	else
		i = spx_font.fontheight - 1;

	for (; i <= spx_font.fontheight - 1; i++) {
		for (j = 0; j < (spx_font.fontwidth >> 2); j++)
			set_btreg(SPXDAC_REG_CRAM_BASE + i*16 + j, 0xff);
		k = (spx_font.fontwidth & 3) << 1;
		set_btreg(SPXDAC_REG_CRAM_BASE + i*16 + j, (1 << k) - 1);
	}

	if (fb_type == FB_IS_SPX) {
		/* set SPX write/read plane masks */
		SPX_REG(0x3170) = 0xffffffff;
		SPX_REG(0x3174) = 0xffffffff;
	}

	/* RAMDAC type-specific configuration */
	if (fb_type == FB_IS_SPX)
		set_btreg(SPXDAC_REG_CMD2, 0xc0);
	
	/* common configuration */
	set_btreg(SPXDAC_REG_CMD0, 0x48);
	set_btreg(SPXDAC_REG_CMD1, 0x00);
	set_btreg(SPXDAC_REG_PRM, 0xff);

	set_btreg(SPXDAC_REG_CXLO, 0);
	set_btreg(SPXDAC_REG_CXHI, 0);
	set_btreg(SPXDAC_REG_CYLO, 0);
	set_btreg(SPXDAC_REG_CYHI, 0);

	set_btreg(SPXDAC_REG_WXLO, 0);
	set_btreg(SPXDAC_REG_WXHI, 0);
	set_btreg(SPXDAC_REG_WYLO, 0);
	set_btreg(SPXDAC_REG_WYHI, 0);

	set_btreg(SPXDAC_REG_WWLO, 0);
	set_btreg(SPXDAC_REG_WWHI, 0);
	set_btreg(SPXDAC_REG_WHLO, 0);
	set_btreg(SPXDAC_REG_WHHI, 0);

	init_done = 1;
}