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
/* $NetBSD: unichromefb.c,v 1.19 2015/10/25 22:48:23 khorben Exp $ */

/*-
 * Copyright (c) 2006, 2008 Jared D. McNeill <jmcneill@invisible.ca>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE 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 1998-2006 VIA Technologies, Inc. All Rights Reserved.
 * Copyright 2001-2006 S3 Graphics, Inc. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sub license,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) OR COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: unichromefb.c,v 1.19 2015/10/25 22:48:23 khorben Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>

#include <sys/bus.h>

#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcidevs.h>
#include <dev/pci/pciio.h>

#include <dev/wscons/wsdisplayvar.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wsfont/wsfont.h>
#include <dev/rasops/rasops.h>
#include <dev/wscons/wsdisplay_vconsvar.h>
#include <dev/pci/wsdisplay_pci.h>

#include <dev/pci/unichromereg.h>
#include <dev/pci/unichromemode.h>
#include <dev/pci/unichromehw.h>
#include <dev/pci/unichromeconfig.h>
#include <dev/pci/unichromeaccel.h>

#include "vga.h"

#if NVGA > 0
#include <dev/ic/mc6845reg.h>
#include <dev/ic/pcdisplayvar.h>
#include <dev/ic/vgareg.h>
#include <dev/ic/vgavar.h>
#endif

/* XXX */
#define UNICHROMEFB_DEPTH	16
#define UNICHROMEFB_MODE	VIA_RES_1280X1024
#define UNICHROMEFB_WIDTH	1280
#define UNICHROMEFB_HEIGHT	1024

struct unichromefb_softc {
	device_t		sc_dev;
	struct vcons_data	sc_vd;
	void *			sc_fbbase;
	unsigned int		sc_fbaddr;
	unsigned int		sc_fbsize;
	bus_addr_t		sc_mmiobase;
	bus_size_t		sc_mmiosize;

	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_ioh;

	bus_space_tag_t		sc_memt;
	bus_space_handle_t	sc_memh;
	bus_space_tag_t		sc_apmemt;
	bus_space_handle_t	sc_apmemh;

	struct pci_attach_args	sc_pa;

	int			sc_width;
	int			sc_height;
	int			sc_depth;
	int			sc_stride;

	int			sc_wsmode;

	int			sc_accel;
};

static int unichromefb_match(device_t, cfdata_t, void *);
static void unichromefb_attach(device_t, device_t, void *);

static int unichromefb_drm_print(void *, const char *);
static int unichromefb_drm_unmap(struct unichromefb_softc *);
static int unichromefb_drm_map(struct unichromefb_softc *);

struct wsscreen_descr unichromefb_stdscreen = {
	"fb",
	0, 0,
	NULL,
	8, 16,
	WSSCREEN_WSCOLORS, NULL,
};

static int	unichromefb_ioctl(void *, void *, u_long, void *, int,
				  struct lwp *);
static paddr_t	unichromefb_mmap(void *, void *, off_t, int);

static void	unichromefb_init_screen(void *, struct vcons_screen *,
					int, long *);

/* hardware access */
static uint8_t	uni_rd(struct unichromefb_softc *, int, uint8_t);
static void	uni_wr(struct unichromefb_softc *, int, uint8_t, uint8_t);
static void	uni_wr_mask(struct unichromefb_softc *, int, uint8_t,
			    uint8_t, uint8_t);
static void	uni_wr_x(struct unichromefb_softc *, struct io_reg *, int);
static void	uni_wr_dac(struct unichromefb_softc *, uint8_t, uint8_t,
			   uint8_t, uint8_t);

/* helpers */
static struct VideoModeTable *	uni_getmode(int);
static void	uni_setmode(struct unichromefb_softc *, int, int);
static void	uni_crt_lock(struct unichromefb_softc *);
static void	uni_crt_unlock(struct unichromefb_softc *);
static void	uni_crt_enable(struct unichromefb_softc *);
static void	uni_crt_disable(struct unichromefb_softc *);
static void	uni_screen_enable(struct unichromefb_softc *);
static void	uni_screen_disable(struct unichromefb_softc *);
static void	uni_set_start(struct unichromefb_softc *);
static void	uni_set_crtc(struct unichromefb_softc *,
			     struct crt_mode_table *, int, int, int);
static void	uni_load_crtc(struct unichromefb_softc *, struct display_timing,
			      int);
static void	uni_load_reg(struct unichromefb_softc *, int, int,
			     struct io_register *, int);
static void	uni_fix_crtc(struct unichromefb_softc *);
static void	uni_load_offset(struct unichromefb_softc *, int, int, int);
static void	uni_load_fetchcnt(struct unichromefb_softc *, int, int, int);
static void	uni_load_fifo(struct unichromefb_softc *, int, int, int);
static void	uni_set_depth(struct unichromefb_softc *, int, int);
static uint32_t	uni_get_clkval(struct unichromefb_softc *, int);
static void	uni_set_vclk(struct unichromefb_softc *, uint32_t, int);
static void	uni_init_dac(struct unichromefb_softc *, int);
static void	uni_init_accel(struct unichromefb_softc *);
static void	uni_set_accel_depth(struct unichromefb_softc *);

/* graphics ops */
static void	uni_wait_idle(struct unichromefb_softc *);
static void	uni_fillrect(struct unichromefb_softc *,
			     int, int, int, int, int);
static void	uni_rectinvert(struct unichromefb_softc *,
			       int, int, int, int);
static void	uni_bitblit(struct unichromefb_softc *, int, int, int, int,
			    int, int);
static void	uni_setup_mono(struct unichromefb_softc *, int, int, int,
			       int, uint32_t, uint32_t);
#if notyet
static void	uni_cursor_show(struct unichromefb_softc *);
static void	uni_cursor_hide(struct unichromefb_softc *);
#endif

/* rasops glue */
static void	uni_copycols(void *, int, int, int, int);
static void	uni_copyrows(void *, int, int, int);
static void	uni_erasecols(void *, int, int, int, long);
static void	uni_eraserows(void *, int, int, long);
static void	uni_cursor(void *, int, int, int);
static void	uni_putchar(void *, int, int, u_int, long);

struct wsdisplay_accessops unichromefb_accessops = {
	unichromefb_ioctl,
	unichromefb_mmap,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
};

static struct vcons_screen unichromefb_console_screen;

const struct wsscreen_descr *_unichromefb_scrlist[] = {
	&unichromefb_stdscreen,
};

struct wsscreen_list unichromefb_screenlist = {
	sizeof(_unichromefb_scrlist) / sizeof(struct wsscreen_descr *),
	_unichromefb_scrlist
};

CFATTACH_DECL_NEW(unichromefb, sizeof(struct unichromefb_softc),
    unichromefb_match, unichromefb_attach, NULL, NULL);

static int
unichromefb_match(device_t parent, cfdata_t match, void *opaque)
{
	struct pci_attach_args *pa;

	pa = (struct pci_attach_args *)opaque;

	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
		return 0;

	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_VIATECH)
		return 0;

	switch (PCI_PRODUCT(pa->pa_id)) {
	case PCI_PRODUCT_VIATECH_VT3314_IG:
		return 10;	/* beat vga(4) */
	}

	return 0;
}

static void
unichromefb_attach(device_t parent, device_t self, void *opaque)
{
	struct unichromefb_softc *sc = device_private(self);
	struct pci_attach_args *pa;
	struct rasops_info *ri;
	struct wsemuldisplaydev_attach_args aa;
	uint8_t val;
	long defattr;

	pa = (struct pci_attach_args *)opaque;

	sc->sc_dev = self;
	sc->sc_width = UNICHROMEFB_WIDTH;
	sc->sc_height = UNICHROMEFB_HEIGHT;
	sc->sc_depth = UNICHROMEFB_DEPTH;
	sc->sc_stride = sc->sc_width * (sc->sc_depth / 8);

	sc->sc_wsmode = WSDISPLAYIO_MODE_EMUL;

	sc->sc_iot = pa->pa_iot;
	sc->sc_pa = *pa;

#if NVGA > 0
	/* XXX vga_cnattach claims the I/O registers that we need;
	 *     we need to nuke it here so we can take over.
	 */
	vga_cndetach();
#endif

	if (bus_space_map(sc->sc_iot, VIA_REGBASE, 0x20, 0, &sc->sc_ioh)) {
		aprint_error(": failed to map I/O registers\n");
		return;
	}

	sc->sc_apmemt = pa->pa_memt;
	val = uni_rd(sc, VIASR, SR30);
	sc->sc_fbaddr = val << 24;
	val = uni_rd(sc, VIASR, SR39);
	sc->sc_fbsize = val * (4*1024*1024);
	if (sc->sc_fbsize < 16*1024*1024 || sc->sc_fbsize > 64*1024*1024)
		sc->sc_fbsize = 16*1024*1024;
	if (bus_space_map(sc->sc_apmemt, sc->sc_fbaddr, sc->sc_fbsize,
	    BUS_SPACE_MAP_LINEAR, &sc->sc_apmemh)) {
		aprint_error(": failed to map aperture at 0x%08x/0x%x\n",
		    sc->sc_fbaddr, sc->sc_fbsize);
		return;
	}
	sc->sc_fbbase = (void *)bus_space_vaddr(sc->sc_apmemt, sc->sc_apmemh);

	if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_MEM, 0,
	    &sc->sc_memt, &sc->sc_memh, &sc->sc_mmiobase,
	    &sc->sc_mmiosize)) {
		sc->sc_accel = 0;
		aprint_error(": failed to map MMIO registers\n");
	} else {
		sc->sc_accel = 1;
	}

	aprint_naive("\n");
	aprint_normal(": VIA UniChrome frame buffer\n");

	if (sc->sc_accel)
		aprint_normal_dev(self, "MMIO @0x%08x/0x%x\n",
		    (uint32_t)sc->sc_mmiobase,
		    (uint32_t)sc->sc_mmiosize);

	ri = &unichromefb_console_screen.scr_ri;
	memset(ri, 0, sizeof(struct rasops_info));

	vcons_init(&sc->sc_vd, sc, &unichromefb_stdscreen,
	    &unichromefb_accessops);
	sc->sc_vd.init_screen = unichromefb_init_screen;

	uni_setmode(sc, UNICHROMEFB_MODE, sc->sc_depth);

	uni_init_dac(sc, IGA1);
	if (sc->sc_accel) {
		uni_init_accel(sc);
		uni_fillrect(sc, 0, 0, sc->sc_width, sc->sc_height, 0);
	}

	aprint_normal_dev(self, "FB @0x%08x (%dx%dx%d)\n",
	       sc->sc_fbaddr, sc->sc_width, sc->sc_height, sc->sc_depth);

	unichromefb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
	vcons_init_screen(&sc->sc_vd, &unichromefb_console_screen, 1, &defattr);

	unichromefb_stdscreen.ncols = ri->ri_cols;
	unichromefb_stdscreen.nrows = ri->ri_rows;
	unichromefb_stdscreen.textops = &ri->ri_ops;
	unichromefb_stdscreen.capabilities = ri->ri_caps;
	unichromefb_stdscreen.modecookie = NULL;

	wsdisplay_cnattach(&unichromefb_stdscreen, ri, 0, 0, defattr);

	aa.console = 1; /* XXX */
	aa.scrdata = &unichromefb_screenlist;
	aa.accessops = &unichromefb_accessops;
	aa.accesscookie = &sc->sc_vd;

	config_found(self, &aa, wsemuldisplaydevprint);

	config_found_ia(self, "drm", opaque, unichromefb_drm_print);

	return;
}

static int
unichromefb_drm_print(void *opaque, const char *pnp)
{
	if (pnp)
		aprint_normal("drm at %s", pnp);

	return UNCONF;
}

static int
unichromefb_drm_unmap(struct unichromefb_softc *sc)
{
	aprint_debug_dev(sc->sc_dev, "releasing bus resources\n");

	bus_space_unmap(sc->sc_apmemt, sc->sc_apmemh, sc->sc_fbsize);
	bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mmiosize);
	bus_space_unmap(sc->sc_iot, sc->sc_ioh, 0x20);

	return 0;
}

static int
unichromefb_drm_map(struct unichromefb_softc *sc)
{
	int rv;

	rv = bus_space_map(sc->sc_iot, VIA_REGBASE, 0x20, 0,
	    &sc->sc_ioh);
	if (rv) {
		aprint_error_dev(sc->sc_dev, "failed to map I/O registers\n");
		return rv;
	}
	rv = bus_space_map(sc->sc_apmemt, sc->sc_fbaddr, sc->sc_fbsize,
	    BUS_SPACE_MAP_LINEAR, &sc->sc_apmemh);
	if (rv) {
		aprint_error_dev(sc->sc_dev,
		    "failed to map aperture at 0x%08x/0x%x\n",
		    sc->sc_fbaddr, sc->sc_fbsize);
		return rv;
	}
	sc->sc_fbbase = (void *)bus_space_vaddr(sc->sc_apmemt, sc->sc_apmemh);
	rv = pci_mapreg_map(&sc->sc_pa, 0x14, PCI_MAPREG_TYPE_MEM, 0,
	    &sc->sc_memt, &sc->sc_memh, &sc->sc_mmiobase,
	    &sc->sc_mmiosize);
	if (rv) {
		aprint_error_dev(sc->sc_dev, "failed to map MMIO registers\n");
		sc->sc_accel = 0;
	}

	uni_setmode(sc, UNICHROMEFB_MODE, sc->sc_depth);
	uni_init_dac(sc, IGA1);
	if (sc->sc_accel)
		uni_init_accel(sc);

	aprint_debug_dev(sc->sc_dev, "re-acquired bus resources\n");

	return 0;
}

static int
unichromefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
		  struct lwp *l)
{
	struct vcons_data *vd;
	struct unichromefb_softc *sc;
	struct wsdisplay_fbinfo *fb;

	vd = (struct vcons_data *)v;
	sc = (struct unichromefb_softc *)vd->cookie;

	switch (cmd) {
	case WSDISPLAYIO_GTYPE:
		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
		return 0;
	case WSDISPLAYIO_GINFO:
		if (vd->active != NULL) {
			fb = (struct wsdisplay_fbinfo *)data;
			fb->width = sc->sc_width;
			fb->height = sc->sc_height;
			fb->depth = sc->sc_depth;
			fb->cmsize = 256;
			return 0;
		} else
			return ENODEV;
	case WSDISPLAYIO_GVIDEO:
			return ENODEV;
	case WSDISPLAYIO_SVIDEO:
			return ENODEV;
	case WSDISPLAYIO_GETCMAP:
			return EINVAL;
	case WSDISPLAYIO_PUTCMAP:
			return EINVAL;
	case WSDISPLAYIO_LINEBYTES:
		*(u_int *)data = sc->sc_stride;
		return 0;
	case WSDISPLAYIO_SMODE: {
		int new_mode = *(int *)data;
		if (new_mode != sc->sc_wsmode) {
			sc->sc_wsmode = new_mode;
			switch (new_mode) {
			case WSDISPLAYIO_MODE_EMUL:
				unichromefb_drm_map(sc);
				vcons_redraw_screen(vd->active);
				break;
			default:
				unichromefb_drm_unmap(sc);
				break;
			}
		}
		}
		return 0;
	case WSDISPLAYIO_SSPLASH:
		return ENODEV;

	/* PCI config read/write passthrough. */
	case PCI_IOC_CFGREAD:
	case PCI_IOC_CFGWRITE:
		return (pci_devioctl(sc->sc_pa.pa_pc, sc->sc_pa.pa_tag,
		    cmd, data, flag, l));

	case WSDISPLAYIO_GET_BUSID:
		return wsdisplayio_busid_pci(sc->sc_dev,
		    sc->sc_pa.pa_pc, sc->sc_pa.pa_tag, data);

	}

	return EPASSTHROUGH;
}

static paddr_t
unichromefb_mmap(void *v, void *vs, off_t offset, int prot)
{
	return -1;
}

static void
unichromefb_init_screen(void *c, struct vcons_screen *scr, int existing,
			long *defattr)
{
	struct unichromefb_softc *sc;
	struct rasops_info *ri;

	sc = (struct unichromefb_softc *)c;
	ri = &scr->scr_ri;
	ri->ri_flg = RI_CENTER;
	ri->ri_depth = sc->sc_depth;
	ri->ri_width = sc->sc_width;
	ri->ri_height = sc->sc_height;
	ri->ri_stride = sc->sc_stride;
	ri->ri_bits = sc->sc_fbbase;
	if (existing)
		ri->ri_flg |= RI_CLEAR;

	switch (ri->ri_depth) {
	case 32:
		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
		ri->ri_rpos = 16;
		ri->ri_gpos = 8;
		ri->ri_bpos = 0;
		break;
	case 16:
		ri->ri_rnum = 5;
		ri->ri_gnum = 6;
		ri->ri_bnum = 5;
		ri->ri_rpos = 11;
		ri->ri_gpos = 5;
		ri->ri_bpos = 0;
		break;
	}

	rasops_init(ri, sc->sc_height / 16, sc->sc_width / 8);
	ri->ri_caps = WSSCREEN_WSCOLORS;
	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
	    sc->sc_width / ri->ri_font->fontwidth);

	ri->ri_hw = scr;
	if (sc->sc_accel) {
		ri->ri_ops.copyrows = uni_copyrows;
		ri->ri_ops.copycols = uni_copycols;
		ri->ri_ops.eraserows = uni_eraserows;
		ri->ri_ops.erasecols = uni_erasecols;
		ri->ri_ops.cursor = uni_cursor;
		ri->ri_ops.putchar = uni_putchar;
	}

	return;
}

/*
 * hardware access
 */
static uint8_t
uni_rd(struct unichromefb_softc *sc, int off, uint8_t idx)
{
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, off, idx);
	return bus_space_read_1(sc->sc_iot, sc->sc_ioh, off + 1);
}

static void
uni_wr(struct unichromefb_softc *sc, int off, uint8_t idx, uint8_t val)
{
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, off, idx);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, off + 1, val);
}

static void
uni_wr_mask(struct unichromefb_softc *sc, int off, uint8_t idx,
    uint8_t val, uint8_t mask)
{
	uint8_t tmp;

	bus_space_write_1(sc->sc_iot, sc->sc_ioh, off, idx);
	tmp = bus_space_read_1(sc->sc_iot, sc->sc_ioh, off + 1);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, off + 1,
	    ((val & mask) | (tmp & ~mask)));
}

static void
uni_wr_dac(struct unichromefb_softc *sc, uint8_t idx,
    uint8_t r, uint8_t g, uint8_t b)
{
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LUT_INDEX_WRITE, idx);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LUT_DATA, r);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LUT_DATA, g);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LUT_DATA, b);
}

static void
uni_wr_x(struct unichromefb_softc *sc, struct io_reg *tbl, int num)
{
	int i;
	uint8_t tmp;

	for (i = 0; i < num; i++) {
		bus_space_write_1(sc->sc_iot, sc->sc_ioh, tbl[i].port,
		    tbl[i].index);
		tmp = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
		    tbl[i].port + 1);
		tmp = (tmp & (~tbl[i].mask)) | tbl[i].value;
		bus_space_write_1(sc->sc_iot, sc->sc_ioh, tbl[i].index + 1,
		    tmp);
	}
}

/*
 * helpers
 */
static struct VideoModeTable *
uni_getmode(int mode)
{
	int i;

	for (i = 0; i < NUM_TOTAL_MODETABLE; i++)
		if (CLE266Modes[i].ModeIndex == mode)
			return &CLE266Modes[i];

	return NULL;
}

static void
uni_setmode(struct unichromefb_softc *sc, int idx, int bpp)
{
	struct VideoModeTable *vtbl;
	struct crt_mode_table *crt;
	int i;

	/* XXX */
	vtbl = uni_getmode(idx);
	if (vtbl == NULL)
		panic("%s: unsupported mode: %d\n",
		    device_xname(sc->sc_dev), idx);

	crt = vtbl->crtc;

	uni_screen_disable(sc);

	(void)bus_space_read_1(sc->sc_iot, sc->sc_ioh, VIAStatus);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAAR, 0);

	/* XXX assume CN900 for now */
	uni_wr_x(sc, CN900_ModeXregs, NUM_TOTAL_CN900_ModeXregs);

	uni_crt_disable(sc);

	/* Fill VPIT params */
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAWMisc, VPIT.Misc);

	/* Write sequencer */
	for (i = 1; i <= StdSR; i++) {
		bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIASR, i);
		bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIASR + 1,
		    VPIT.SR[i - 1]);
	}

	uni_set_start(sc);

	uni_set_crtc(sc, crt, idx, bpp / 8, IGA1);

	for (i = 0; i < StdGR; i++) {
		bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAGR, i);
		bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAGR + 1,
		    VPIT.GR[i]);
	}

	for (i = 0; i < StdAR; i++) {
		(void)bus_space_read_1(sc->sc_iot, sc->sc_ioh, VIAStatus);
		bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAAR, i);
		bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAAR,
		    VPIT.AR[i]);
	}

	(void)bus_space_read_1(sc->sc_iot, sc->sc_ioh, VIAStatus);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAAR, 0x20);

	uni_set_crtc(sc, crt, idx, bpp / 8, IGA1);
	/* set crt output path */
	uni_wr_mask(sc, VIASR, SR16, 0x00, BIT6);

	uni_crt_enable(sc);
	uni_screen_enable(sc);

	return;
}

static void
uni_crt_lock(struct unichromefb_softc *sc)
{
	uni_wr_mask(sc, VIACR, CR11, BIT7, BIT7);
}

static void
uni_crt_unlock(struct unichromefb_softc *sc)
{
	uni_wr_mask(sc, VIACR, CR11, 0, BIT7);
	uni_wr_mask(sc, VIACR, CR47, 0, BIT0);
}

static void
uni_crt_enable(struct unichromefb_softc *sc)
{
	uni_wr_mask(sc, VIACR, CR36, 0, BIT5+BIT4);
}

static void
uni_crt_disable(struct unichromefb_softc *sc)
{
	uni_wr_mask(sc, VIACR, CR36, BIT5+BIT4, BIT5+BIT4);
}

static void
uni_screen_enable(struct unichromefb_softc *sc)
{
	uni_wr_mask(sc, VIASR, SR01, 0, BIT5);
}

static void
uni_screen_disable(struct unichromefb_softc *sc)
{
	uni_wr_mask(sc, VIASR, SR01, 0x20, BIT5);
}

static void
uni_set_start(struct unichromefb_softc *sc)
{
	uni_crt_unlock(sc);

	uni_wr(sc, VIACR, CR0C, 0x00);
	uni_wr(sc, VIACR, CR0D, 0x00);
	uni_wr(sc, VIACR, CR34, 0x00);
	uni_wr_mask(sc, VIACR, CR48, 0x00, BIT0 + BIT1);

	uni_wr(sc, VIACR, CR62, 0x00);
	uni_wr(sc, VIACR, CR63, 0x00);
	uni_wr(sc, VIACR, CR64, 0x00);
	uni_wr(sc, VIACR, CRA3, 0x00);

	uni_crt_lock(sc);
}

static void
uni_set_crtc(struct unichromefb_softc *sc, struct crt_mode_table *ctbl,
    int mode, int bpp_byte, int iga)
{
	struct VideoModeTable *vtbl;
	struct display_timing crtreg;
	int i;
	int index;
	int haddr, vaddr;
	uint8_t val;
	uint32_t pll_d_n;

	index = 0;

	vtbl = uni_getmode(mode);
	for (i = 0; i < vtbl->mode_array; i++) {
		index = i;
		if (ctbl[i].refresh_rate == 60)
			break;
	}

	crtreg = ctbl[index].crtc;

	haddr = crtreg.hor_addr;
	vaddr = crtreg.ver_addr;

	val = bus_space_read_1(sc->sc_iot, sc->sc_ioh, VIARMisc);
	if (ctbl[index].h_sync_polarity == NEGATIVE) {
		if (ctbl[index].v_sync_polarity == NEGATIVE)
			bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAWMisc,
			    (val & (~(BIT6+BIT7))) | (BIT6+BIT7));
		else
			bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAWMisc,
			    (val & (~(BIT6+BIT7))) | (BIT6));
	} else {
		if (ctbl[index].v_sync_polarity == NEGATIVE)
			bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAWMisc,
			    (val & (~(BIT6+BIT7))) | (BIT7));
		else
			bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAWMisc,
			    (val & (~(BIT6+BIT7))));
	}

	if (iga == IGA1) {
		uni_crt_unlock(sc);
		uni_wr(sc, VIACR, CR09, 0x00);
		uni_wr_mask(sc, VIACR, CR11, 0x00, BIT4+BIT5+BIT6);
		uni_wr_mask(sc, VIACR, CR17, 0x00, BIT7);
	}

	uni_load_crtc(sc, crtreg, iga);
	uni_fix_crtc(sc);
	uni_crt_lock(sc);
	uni_wr_mask(sc, VIACR, CR17, 0x80, BIT7);

	uni_load_offset(sc, haddr, bpp_byte, iga);
	uni_load_fetchcnt(sc, haddr, bpp_byte, iga);
	uni_load_fifo(sc, iga, haddr, vaddr);

	uni_set_depth(sc, bpp_byte, iga);
	pll_d_n = uni_get_clkval(sc, ctbl[index].clk);
	uni_set_vclk(sc, pll_d_n, iga);
}

static void
uni_load_crtc(struct unichromefb_softc *sc,
    struct display_timing device_timing, int iga)
{
	int regnum, val;
	struct io_register *reg;
	int i;

	regnum = val = 0;
	reg = NULL;

	uni_crt_unlock(sc);

	for (i = 0; i < 12; i++) {
		switch (iga) {
		case IGA1:
			switch (i) {
			case H_TOTAL_INDEX:
				val = IGA1_HOR_TOTAL_FORMULA(
				    device_timing.hor_total);
				regnum = iga1_crtc_reg.hor_total.reg_num;
				reg = iga1_crtc_reg.hor_total.reg;
				break;
			case H_ADDR_INDEX:
				val = IGA1_HOR_ADDR_FORMULA(
				    device_timing.hor_addr);
				regnum = iga1_crtc_reg.hor_addr.reg_num;
				reg = iga1_crtc_reg.hor_addr.reg;
				break;
			case H_BLANK_START_INDEX:
				val = IGA1_HOR_BLANK_START_FORMULA(
				    device_timing.hor_blank_start);
				regnum = iga1_crtc_reg.hor_blank_start.reg_num;
				reg = iga1_crtc_reg.hor_blank_start.reg;
				break;
			case H_BLANK_END_INDEX:
				val = IGA1_HOR_BLANK_END_FORMULA(
				    device_timing.hor_blank_start,
				    device_timing.hor_blank_end);
				regnum = iga1_crtc_reg.hor_blank_end.reg_num;
				reg = iga1_crtc_reg.hor_blank_end.reg;
				break;
			case H_SYNC_START_INDEX:
				val = IGA1_HOR_SYNC_START_FORMULA(
				    device_timing.hor_sync_start);
				regnum = iga1_crtc_reg.hor_sync_start.reg_num;
				reg = iga1_crtc_reg.hor_sync_start.reg;
				break;
			case H_SYNC_END_INDEX:
				val = IGA1_HOR_SYNC_END_FORMULA(
				    device_timing.hor_sync_start,
				    device_timing.hor_sync_end);
				regnum = iga1_crtc_reg.hor_sync_end.reg_num;
				reg = iga1_crtc_reg.hor_sync_end.reg;
				break;
			case V_TOTAL_INDEX:
				val = IGA1_VER_TOTAL_FORMULA(
				    device_timing.ver_total);
				regnum = iga1_crtc_reg.ver_total.reg_num;
				reg = iga1_crtc_reg.ver_total.reg;
				break;
			case V_ADDR_INDEX:
				val = IGA1_VER_ADDR_FORMULA(
				    device_timing.ver_addr);
				regnum = iga1_crtc_reg.ver_addr.reg_num;
				reg = iga1_crtc_reg.ver_addr.reg;
				break;
			case V_BLANK_START_INDEX:
				val = IGA1_VER_BLANK_START_FORMULA(
				    device_timing.ver_blank_start);
				regnum = iga1_crtc_reg.ver_blank_start.reg_num;
				reg = iga1_crtc_reg.ver_blank_start.reg;
				break;
			case V_BLANK_END_INDEX:
				val = IGA1_VER_BLANK_END_FORMULA(
				    device_timing.ver_blank_start,
				    device_timing.ver_blank_end);
				regnum = iga1_crtc_reg.ver_blank_end.reg_num;
				reg = iga1_crtc_reg.ver_blank_end.reg;
				break;
			case V_SYNC_START_INDEX:
				val = IGA1_VER_SYNC_START_FORMULA(
				    device_timing.ver_sync_start);
				regnum = iga1_crtc_reg.ver_sync_start.reg_num;
				reg = iga1_crtc_reg.ver_sync_start.reg;
				break;
			case V_SYNC_END_INDEX:
				val = IGA1_VER_SYNC_END_FORMULA(
				    device_timing.ver_sync_start,
				    device_timing.ver_sync_end);
				regnum = iga1_crtc_reg.ver_sync_end.reg_num;
				reg = iga1_crtc_reg.ver_sync_end.reg;
				break;
			default:
				aprint_error_dev(sc->sc_dev,
				    "unknown index %d while setting up CRTC\n",
				    i);
				break;
			}
			break;
		case IGA2:
			aprint_error_dev(sc->sc_dev, "%s: IGA2 not supported\n",
			    __func__);
			break;
		}

		uni_load_reg(sc, val, regnum, reg, VIACR);
	}

	uni_crt_lock(sc);
}

static void
uni_load_reg(struct unichromefb_softc *sc, int timing, int regnum,
    struct io_register *reg, int type)
{
	int regmask, bitnum, data;
	int i, j;
	int shift_next_reg;
	int startidx, endidx, cridx;
	uint16_t getbit;

	bitnum = 0;

	for (i = 0; i < regnum; i++) {
		regmask = data = 0;
		startidx = reg[i].start_bit;
		endidx = reg[i].end_bit;
		cridx = reg[i].io_addr;

		shift_next_reg = bitnum;

		for (j = startidx; j <= endidx; j++) {
			regmask = regmask | (BIT0 << j);
			getbit = (timing & (BIT0 << bitnum));
			data = data | ((getbit >> shift_next_reg) << startidx);
			++bitnum;
		}

		if (type == VIACR)
			uni_wr_mask(sc, VIACR, cridx, data, regmask);
		else
			uni_wr_mask(sc, VIASR, cridx, data, regmask);
	}

	return;
}

static void
uni_fix_crtc(struct unichromefb_softc *sc)
{
	uni_wr_mask(sc, VIACR, CR03, 0x80, BIT7);
	uni_wr(sc, VIACR, CR18, 0xff);
	uni_wr_mask(sc, VIACR, CR07, 0x10, BIT4);
	uni_wr_mask(sc, VIACR, CR09, 0x40, BIT6);
	uni_wr_mask(sc, VIACR, CR35, 0x10, BIT4);
	uni_wr_mask(sc, VIACR, CR33, 0x06, BIT0+BIT1+BIT2);
	uni_wr(sc, VIACR, CR17, 0xe3);
	uni_wr(sc, VIACR, CR08, 0x00);
	uni_wr(sc, VIACR, CR14, 0x00);

	return;
}

static void
uni_load_offset(struct unichromefb_softc *sc, int haddr, int bpp, int iga)
{

	switch (iga) {
	case IGA1:
		uni_load_reg(sc,
		    IGA1_OFFSET_FORMULA(haddr, bpp),
		    offset_reg.iga1_offset_reg.reg_num,
		    offset_reg.iga1_offset_reg.reg,
		    VIACR);
		break;
	default:
		aprint_error_dev(sc->sc_dev, "%s: only IGA1 is supported\n",
		    __func__);
		break;
	}

	return;
}

static void
uni_load_fetchcnt(struct unichromefb_softc *sc, int haddr, int bpp, int iga)
{

	switch (iga) {
	case IGA1:
		uni_load_reg(sc,
		    IGA1_FETCH_COUNT_FORMULA(haddr, bpp),
		    fetch_count_reg.iga1_fetch_count_reg.reg_num,
		    fetch_count_reg.iga1_fetch_count_reg.reg,
		    VIASR);
		break;
	default:
		aprint_error_dev(sc->sc_dev, "%s: only IGA1 is supported\n",
		    __func__);
		break;
	}

	return;
}

static void
uni_load_fifo(struct unichromefb_softc *sc, int iga, int horact, int veract)
{
	int val, regnum;
	struct io_register *reg;
	int iga1_fifo_max_depth, iga1_fifo_threshold;
	int iga1_fifo_high_threshold, iga1_display_queue_expire_num;

	reg = NULL;
	iga1_fifo_max_depth = iga1_fifo_threshold = 0;
	iga1_fifo_high_threshold = iga1_display_queue_expire_num = 0;

	switch (iga) {
	case IGA1:
		/* XXX if (type == CN900) { */
		iga1_fifo_max_depth = CN900_IGA1_FIFO_MAX_DEPTH;
		iga1_fifo_threshold = CN900_IGA1_FIFO_THRESHOLD;
		iga1_fifo_high_threshold = CN900_IGA1_FIFO_HIGH_THRESHOLD;
		if (horact > 1280 && veract > 1024)
			iga1_display_queue_expire_num = 16;
		else
			iga1_display_queue_expire_num =
			    CN900_IGA1_DISPLAY_QUEUE_EXPIRE_NUM;
		/* XXX } */

		/* set display FIFO depth select */
		val = IGA1_FIFO_DEPTH_SELECT_FORMULA(iga1_fifo_max_depth);
		regnum =
		    display_fifo_depth_reg.iga1_fifo_depth_select_reg.reg_num;
		reg = display_fifo_depth_reg.iga1_fifo_depth_select_reg.reg;
		uni_load_reg(sc, val, regnum, reg, VIASR);

		/* set display FIFO threshold select */
		val = IGA1_FIFO_THRESHOLD_FORMULA(iga1_fifo_threshold);
		regnum = fifo_threshold_select_reg.iga1_fifo_threshold_select_reg.reg_num;
		reg = fifo_threshold_select_reg.iga1_fifo_threshold_select_reg.reg;
		uni_load_reg(sc, val, regnum, reg, VIASR);

		/* set display FIFO high threshold select */
		val = IGA1_FIFO_HIGH_THRESHOLD_FORMULA(iga1_fifo_high_threshold);
		regnum = fifo_high_threshold_select_reg.iga1_fifo_high_threshold_select_reg.reg_num;
		reg = fifo_high_threshold_select_reg.iga1_fifo_high_threshold_select_reg.reg;
		uni_load_reg(sc, val, regnum, reg, VIASR);

		/* set display queue expire num */
		val = IGA1_DISPLAY_QUEUE_EXPIRE_NUM_FORMULA(iga1_display_queue_expire_num);
		regnum = display_queue_expire_num_reg.iga1_display_queue_expire_num_reg.reg_num;
		reg = display_queue_expire_num_reg.iga1_display_queue_expire_num_reg.reg;
		uni_load_reg(sc, val, regnum, reg, VIASR);

		break;
	default:
		aprint_error_dev(sc->sc_dev, "%s: only IGA1 is supported\n",
		    __func__);
		break;
	}

	return;
}

static void
uni_set_depth(struct unichromefb_softc *sc, int bpp, int iga)
{
	switch (iga) {
	case IGA1:
		switch (bpp) {
		case MODE_32BPP:
			uni_wr_mask(sc, VIASR, SR15, 0xae, 0xfe);
			break;
		case MODE_16BPP:
			uni_wr_mask(sc, VIASR, SR15, 0xb6, 0xfe);
			break;
		case MODE_8BPP:
			uni_wr_mask(sc, VIASR, SR15, 0x22, 0xfe);
			break;
		default:
			aprint_error_dev(sc->sc_dev,
			    "%s: mode (%d) unsupported\n", __func__, bpp);
		}
		break;
	default:
		aprint_error_dev(sc->sc_dev, "%s: only IGA1 is supported\n",
		    __func__);
		break;
	}
}

static uint32_t
uni_get_clkval(struct unichromefb_softc *sc, int clk)
{
	int i;

	for (i = 0; i < NUM_TOTAL_PLL_TABLE; i++) {
		if (clk == pll_value[i].clk) {
			/* XXX only CN900 supported for now */
			return pll_value[i].k800_pll;
		}
	}

	aprint_error_dev(sc->sc_dev, "can't find matching PLL value\n");

	return 0;
}

static void
uni_set_vclk(struct unichromefb_softc *sc, uint32_t clk, int iga)
{
	uint8_t val;

	/* hardware reset on */
	uni_wr_mask(sc, VIACR, CR17, 0x00, BIT7);

	switch (iga) {
	case IGA1:
		/* XXX only CN900 is supported */
		uni_wr(sc, VIASR, SR44, clk / 0x10000);
		uni_wr(sc, VIASR, SR45, (clk & 0xffff) / 0x100);
		uni_wr(sc, VIASR, SR46, clk % 0x100);
		break;
	default:
		aprint_error_dev(sc->sc_dev, "%s: only IGA1 is supported\n",
		    __func__);
		break;
	}

	/* hardware reset off */
	uni_wr_mask(sc, VIACR, CR17, 0x80, BIT7);

	/* reset pll */
	switch (iga) {
	case IGA1:
		uni_wr_mask(sc, VIASR, SR40, 0x02, BIT1);
		uni_wr_mask(sc, VIASR, SR40, 0x00, BIT1);
		break;
	}

	/* good to go */
	val = bus_space_read_1(sc->sc_iot, sc->sc_ioh, VIARMisc);
	val |= (BIT2+BIT3);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAWMisc, val);

	return;
}

static void
uni_init_dac(struct unichromefb_softc *sc, int iga)
{
	int i;

	/* XXX only IGA1 for now */
	uni_wr_mask(sc, VIASR, SR1A, 0x00, BIT0);
	uni_wr_mask(sc, VIASR, SR18, 0x00, BIT7+BIT6);
	for (i = 0; i < 256; i++)
		uni_wr_dac(sc, i,
		    palLUT_table[i].red, palLUT_table[i].green, palLUT_table[i].blue);

	uni_wr_mask(sc, VIASR, SR18, 0xc0, BIT7+BIT6);

	return;
}

static void
uni_init_accel(struct unichromefb_softc *sc)
{

	/* init 2D engine regs to reset 2D engine */
	MMIO_OUT32(VIA_REG_GEMODE, 0);
	MMIO_OUT32(VIA_REG_SRCPOS, 0);
	MMIO_OUT32(VIA_REG_DSTPOS, 0);
	MMIO_OUT32(VIA_REG_DIMENSION, 0);
	MMIO_OUT32(VIA_REG_PATADDR, 0);
	MMIO_OUT32(VIA_REG_FGCOLOR, 0);
	MMIO_OUT32(VIA_REG_BGCOLOR, 0);
	MMIO_OUT32(VIA_REG_CLIPTL, 0);
	MMIO_OUT32(VIA_REG_CLIPBR, 0);
	MMIO_OUT32(VIA_REG_OFFSET, 0);
	MMIO_OUT32(VIA_REG_KEYCONTROL, 0);
	MMIO_OUT32(VIA_REG_SRCBASE, 0);
	MMIO_OUT32(VIA_REG_DSTBASE, 0);
	MMIO_OUT32(VIA_REG_PITCH, 0);
	MMIO_OUT32(VIA_REG_MONOPAT1, 0);

	/* init AGP and VQ registers */
	MMIO_OUT32(VIA_REG_TRANSET, 0x00100000);
	MMIO_OUT32(VIA_REG_TRANSPACE, 0x00000000);
	MMIO_OUT32(VIA_REG_TRANSPACE, 0x00333004);
	MMIO_OUT32(VIA_REG_TRANSPACE, 0x60000000);
	MMIO_OUT32(VIA_REG_TRANSPACE, 0x61000000);
	MMIO_OUT32(VIA_REG_TRANSPACE, 0x62000000);
	MMIO_OUT32(VIA_REG_TRANSPACE, 0x63000000);
	MMIO_OUT32(VIA_REG_TRANSPACE, 0x64000000);
	MMIO_OUT32(VIA_REG_TRANSPACE, 0x7d000000);

	MMIO_OUT32(VIA_REG_TRANSET, 0xfe020000);
	MMIO_OUT32(VIA_REG_TRANSPACE, 0x00000000);

	/* disable VQ */
	MMIO_OUT32(VIA_REG_TRANSET, 0x00fe0000);
	MMIO_OUT32(VIA_REG_TRANSPACE, 0x00000004);
	MMIO_OUT32(VIA_REG_TRANSPACE, 0x40008c0f);
	MMIO_OUT32(VIA_REG_TRANSPACE, 0x44000000);
	MMIO_OUT32(VIA_REG_TRANSPACE, 0x45080c04);
	MMIO_OUT32(VIA_REG_TRANSPACE, 0x46800408);

	uni_set_accel_depth(sc);

	MMIO_OUT32(VIA_REG_SRCBASE, 0);
	MMIO_OUT32(VIA_REG_DSTBASE, 0);

	MMIO_OUT32(VIA_REG_PITCH, VIA_PITCH_ENABLE |
	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) |
	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) << 16)));

	return;
}

static void
uni_set_accel_depth(struct unichromefb_softc *sc)
{
	uint32_t gemode;

	gemode = MMIO_IN32(0x04) & 0xfffffcff;

	switch (sc->sc_depth) {
	case 32:
		gemode |= VIA_GEM_32bpp;
		break;
	case 16:
		gemode |= VIA_GEM_16bpp;
		break;
	default:
		gemode |= VIA_GEM_8bpp;
		break;
	}

	/* set colour depth and pitch */
	MMIO_OUT32(VIA_REG_GEMODE, gemode);

	return;
}

static void
uni_wait_idle(struct unichromefb_softc *sc)
{
	int loop = 0;

	while (!(MMIO_IN32(VIA_REG_STATUS) & VIA_VR_QUEUE_BUSY) &&
	    (loop++ < MAXLOOP))
		;

	while ((MMIO_IN32(VIA_REG_STATUS) &
	    (VIA_CMD_RGTR_BUSY | VIA_2D_ENG_BUSY | VIA_3D_ENG_BUSY)) &&
	    (loop++ < MAXLOOP))
		;

	if (loop >= MAXLOOP)
		aprint_error_dev(sc->sc_dev, "engine stall\n");

	return;
}

static void
uni_fillrect(struct unichromefb_softc *sc, int x, int y, int width,
    int height, int colour)
{

	uni_wait_idle(sc);

	MMIO_OUT32(VIA_REG_SRCPOS, 0);
	MMIO_OUT32(VIA_REG_SRCBASE, 0);
	MMIO_OUT32(VIA_REG_DSTBASE, 0);
	MMIO_OUT32(VIA_REG_PITCH, VIA_PITCH_ENABLE |
	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) |
	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) << 16)));
	MMIO_OUT32(VIA_REG_DSTPOS, ((y << 16) | x));
	MMIO_OUT32(VIA_REG_DIMENSION,
	    (((height - 1) << 16) | (width - 1)));
	MMIO_OUT32(VIA_REG_FGCOLOR, colour);
	MMIO_OUT32(VIA_REG_GECMD, (0x01 | 0x2000 | 0xf0 << 24));

	return;
}

static void
uni_rectinvert(struct unichromefb_softc *sc, int x, int y, int width,
    int height)
{

	uni_wait_idle(sc);

	MMIO_OUT32(VIA_REG_SRCPOS, 0);
	MMIO_OUT32(VIA_REG_SRCBASE, 0);
	MMIO_OUT32(VIA_REG_DSTBASE, 0);
	MMIO_OUT32(VIA_REG_PITCH, VIA_PITCH_ENABLE |
	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) |
	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) << 16)));
	MMIO_OUT32(VIA_REG_DSTPOS, ((y << 16) | x));
	MMIO_OUT32(VIA_REG_DIMENSION,
	    (((height - 1) << 16) | (width - 1)));
	MMIO_OUT32(VIA_REG_GECMD, (0x01 | 0x2000 | 0x55 << 24));

	return;
}

static void
uni_bitblit(struct unichromefb_softc *sc, int xs, int ys, int xd, int yd, int width, int height)
{
	uint32_t dir;

	dir = 0;

	if (ys < yd) {
		yd += height - 1;
		ys += height - 1;
		dir |= 0x4000;
	}

	if (xs < xd) {
		xd += width - 1;
		xs += width - 1;
		dir |= 0x8000;
	}

	uni_wait_idle(sc);

	MMIO_OUT32(VIA_REG_SRCBASE, 0);
	MMIO_OUT32(VIA_REG_DSTBASE, 0);
	MMIO_OUT32(VIA_REG_PITCH, VIA_PITCH_ENABLE |
	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) |
	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) << 16)));
	MMIO_OUT32(VIA_REG_SRCPOS, ys << 16 | xs);
	MMIO_OUT32(VIA_REG_DSTPOS, yd << 16 | xd);
	MMIO_OUT32(VIA_REG_DIMENSION, ((height - 1) << 16) | (width - 1));
	MMIO_OUT32(VIA_REG_GECMD, (0x01 | dir | (0xcc << 24)));

	return;
}

static void
uni_setup_mono(struct unichromefb_softc *sc, int xd, int yd, int width, int height,
    uint32_t fg, uint32_t bg)
{

	uni_wait_idle(sc);

	MMIO_OUT32(VIA_REG_SRCBASE, 0);
	MMIO_OUT32(VIA_REG_DSTBASE, 0);
	MMIO_OUT32(VIA_REG_PITCH, VIA_PITCH_ENABLE |
	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) |
	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) << 16)));
	MMIO_OUT32(VIA_REG_SRCPOS, 0);
	MMIO_OUT32(VIA_REG_DSTPOS, (yd << 16) | xd);
	MMIO_OUT32(VIA_REG_DIMENSION, ((height - 1) << 16) | (width - 1));
	MMIO_OUT32(VIA_REG_FGCOLOR, fg);
	MMIO_OUT32(VIA_REG_BGCOLOR, bg);
	MMIO_OUT32(VIA_REG_GECMD, 0xcc020142);

	return;
}

#if notyet
static void
uni_cursor_show(struct unichromefb_softc *sc)
{
	uint32_t val;

	val = MMIO_IN32(VIA_REG_CURSOR_MODE);
	val |= 1;
	MMIO_OUT32(VIA_REG_CURSOR_MODE, val);

	return;
}

static void
uni_cursor_hide(struct unichromefb_softc *sc)
{
	uint32_t val;

	val = MMIO_IN32(VIA_REG_CURSOR_MODE);
	val &= 0xfffffffe;
	MMIO_OUT32(VIA_REG_CURSOR_MODE, val);

	return;
}
#endif

/*
 * rasops glue
 */
static void
uni_copycols(void *opaque, int row, int srccol, int dstcol, int ncols)
{
	struct rasops_info *ri;
	struct vcons_screen *scr;
	struct unichromefb_softc *sc;
	int xs, xd, y, width, height;

	ri = (struct rasops_info *)opaque;
	scr = (struct vcons_screen *)ri->ri_hw;
	sc = (struct unichromefb_softc *)scr->scr_cookie;

	if (sc->sc_wsmode == WSDISPLAYIO_MODE_EMUL) {
		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
		width = ri->ri_font->fontwidth * ncols;
		height = ri->ri_font->fontheight;
		uni_bitblit(sc, xs, y, xd, y, width, height);
	}

	return;
}

static void
uni_copyrows(void *opaque, int srcrow, int dstrow, int nrows)
{
	struct rasops_info *ri;
	struct vcons_screen *scr;
	struct unichromefb_softc *sc;
	int x, ys, yd, width, height;

	ri = (struct rasops_info *)opaque;
	scr = (struct vcons_screen *)ri->ri_hw;
	sc = (struct unichromefb_softc *)scr->scr_cookie;

	if (sc->sc_wsmode == WSDISPLAYIO_MODE_EMUL) {
		x = ri->ri_xorigin;
		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
		width = ri->ri_emuwidth;
		height = ri->ri_font->fontheight * nrows;
		uni_bitblit(sc, x, ys, x, yd, width, height);
	}

	return;
}

static void
uni_erasecols(void *opaque, int row, int startcol, int ncols, long fillattr)
{
	struct rasops_info *ri;
	struct vcons_screen *scr;
	struct unichromefb_softc *sc;
	int x, y, width, height, fg, bg, ul;

	ri = (struct rasops_info *)opaque;
	scr = (struct vcons_screen *)ri->ri_hw;
	sc = (struct unichromefb_softc *)scr->scr_cookie;

	if (sc->sc_wsmode == WSDISPLAYIO_MODE_EMUL) {
		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
		width = ri->ri_font->fontwidth * ncols;
		height = ri->ri_font->fontheight;
		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
		uni_fillrect(sc, x, y, width, height, ri->ri_devcmap[bg]);
	}

	return;
}

static void
uni_eraserows(void *opaque, int row, int nrows, long fillattr)
{
	struct rasops_info *ri;
	struct vcons_screen *scr;
	struct unichromefb_softc *sc;
	int x, y, width, height, fg, bg, ul;

	ri = (struct rasops_info *)opaque;
	scr = (struct vcons_screen *)ri->ri_hw;
	sc = (struct unichromefb_softc *)scr->scr_cookie;

	if (sc->sc_wsmode == WSDISPLAYIO_MODE_EMUL) {
		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
		if ((row == 0) && (nrows == ri->ri_rows)) {
			/* clear the whole screen */
			uni_fillrect(sc, 0, 0, ri->ri_width,
			    ri->ri_height, ri->ri_devcmap[bg]);
		} else {
			x = ri->ri_xorigin;
			y = ri->ri_yorigin + ri->ri_font->fontheight * row;
			width = ri->ri_emuwidth;
			height = ri->ri_font->fontheight * nrows;
			uni_fillrect(sc, x, y, width, height,
			    ri->ri_devcmap[bg]);
		}
	}

	return;
}

static void
uni_cursor(void *opaque, int on, int row, int col)
{
	struct rasops_info *ri;
	struct vcons_screen *scr;
	struct unichromefb_softc *sc;
	int x, y, wi, he;

	ri = (struct rasops_info *)opaque;
	scr = (struct vcons_screen *)ri->ri_hw;
	sc = (struct unichromefb_softc *)scr->scr_cookie;

	uni_wait_idle(sc);

	wi = ri->ri_font->fontwidth;
	he = ri->ri_font->fontheight;

	if (sc->sc_wsmode == WSDISPLAYIO_MODE_EMUL) {
		x = ri->ri_ccol * wi + ri->ri_xorigin;
		y = ri->ri_crow * he + ri->ri_yorigin;
		if (ri->ri_flg & RI_CURSOR) {
			uni_rectinvert(sc, x, y, wi, he);
			ri->ri_flg &= ~RI_CURSOR;
		}
		ri->ri_crow = row;
		ri->ri_ccol = col;
		if (on) {
			x = ri->ri_ccol * wi + ri->ri_xorigin;
			y = ri->ri_crow * he + ri->ri_yorigin;
			uni_rectinvert(sc, x, y, wi, he);
			ri->ri_flg |= RI_CURSOR;
		}
	} else {
		ri->ri_flg &= ~RI_CURSOR;
		ri->ri_crow = row;
		ri->ri_ccol = col;
	}

	return;
}

static void
uni_putchar(void *opaque, int row, int col, u_int c, long attr)
{
	struct rasops_info *ri;
	struct vcons_screen *scr;
	struct unichromefb_softc *sc;

	ri = (struct rasops_info *)opaque;
	scr = (struct vcons_screen *)ri->ri_hw;
	sc = (struct unichromefb_softc *)scr->scr_cookie;

	if (sc->sc_wsmode == WSDISPLAYIO_MODE_EMUL) {
		uint32_t *data;
		int fg, bg, ul, uc, i;
		int x, y, wi, he;

		wi = ri->ri_font->fontwidth;
		he = ri->ri_font->fontheight;

		if (!CHAR_IN_FONT(c, ri->ri_font))
			return;

		rasops_unpack_attr(attr, &fg, &bg, &ul);
		x = ri->ri_xorigin + col * wi;
		y = ri->ri_yorigin + row * he;
		if (c == 0x20)
			uni_fillrect(sc, x, y, wi, he, ri->ri_devcmap[bg]);
		else {
			uc = c - ri->ri_font->firstchar;
			data = (uint32_t *)((uint8_t *)ri->ri_font->data +
			    uc * ri->ri_fontscale);
			uni_setup_mono(sc, x, y, wi, he,
			    ri->ri_devcmap[fg], ri->ri_devcmap[bg]);
			for (i = 0; i < (wi * he) / 4; i++) {
				MMIO_OUT32(VIA_MMIO_BLTBASE, *data);
				data++;
			}
		}
	}

	return;
}