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
/*	$NetBSD: mgx.c,v 1.23 2023/06/28 11:08:47 macallan Exp $ */

/*-
 * Copyright (c) 2014 Michael Lorenz
 * 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.
 */

/* a console driver for the SSB 4096V-MGX graphics card */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: mgx.c,v 1.23 2023/06/28 11:08:47 macallan Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/conf.h>
#include <sys/kmem.h>
#include <sys/kauth.h>
#include <sys/atomic.h>

#include <sys/bus.h>
#include <machine/autoconf.h>

#include <dev/sbus/sbusvar.h>
#include <dev/sun/fbio.h>
#include <dev/sun/fbvar.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/wscons/wsdisplay_glyphcachevar.h>

#include <dev/ic/vgareg.h>
#include <dev/sbus/mgxreg.h>

#include "ioconf.h"

#include "opt_wsemul.h"
#include "opt_mgx.h"

struct mgx_softc {
	device_t	sc_dev;
	struct fbdevice	sc_fb;		/* frame buffer device */
	bus_space_tag_t sc_tag;
	bus_space_handle_t sc_blith;
	bus_space_handle_t sc_vgah;
	bus_addr_t	sc_paddr, sc_rpaddr;
	void		*sc_fbaddr;
	uint8_t		*sc_cursor;
	int		sc_width;
	int		sc_height;
	int		sc_stride;
	int		sc_depth;
	int		sc_fbsize;
	int		sc_mode;
	char		sc_name[8];
	uint32_t	sc_dec, sc_r_dec, sc_r_fg;
	u_char		sc_cmap_red[256];
	u_char		sc_cmap_green[256];
	u_char		sc_cmap_blue[256];
	int		sc_cursor_x, sc_cursor_y;
	int		sc_hotspot_x, sc_hotspot_y;
	int		sc_video, sc_buf;
	void (*sc_putchar)(void *, int, int, u_int, long);
	struct vcons_screen sc_console_screen;
	struct wsscreen_descr sc_defaultscreen_descr;
	const struct wsscreen_descr *sc_screens[1];
	struct wsscreen_list sc_screenlist;
	struct vcons_data vd;
	glyphcache 	sc_gc;
	uint8_t		sc_in[256];
};

static int	mgx_match(device_t, cfdata_t, void *);
static void	mgx_attach(device_t, device_t, void *);
static int	mgx_ioctl(void *, void *, u_long, void *, int,
				 struct lwp*);
static paddr_t	mgx_mmap(void *, void *, off_t, int);
static void	mgx_init_screen(void *, struct vcons_screen *, int,
				 long *);
static void	mgx_write_dac(struct mgx_softc *, int, int, int, int);
static void	mgx_setup(struct mgx_softc *, int);
static void	mgx_init_palette(struct mgx_softc *);
static int	mgx_putcmap(struct mgx_softc *, struct wsdisplay_cmap *);
static int 	mgx_getcmap(struct mgx_softc *, struct wsdisplay_cmap *);
static int	mgx_wait_engine(struct mgx_softc *);
__unused static int	mgx_wait_host(struct mgx_softc *);
/*static*/ int	mgx_wait_fifo(struct mgx_softc *, unsigned int);

static void	mgx_bitblt(void *, int, int, int, int, int, int, int);
static void 	mgx_rectfill(void *, int, int, int, int, long);

static void	mgx_putchar_aa(void *, int, int, u_int, long);
static void	mgx_putchar_mono(void *, int, int, u_int, long);
static void	mgx_cursor(void *, int, int, int);
static void	mgx_copycols(void *, int, int, int, int);
static void	mgx_erasecols(void *, int, int, int, long);
static void	mgx_copyrows(void *, int, int, int);
static void	mgx_eraserows(void *, int, int, long);
static void	mgx_adapt(struct vcons_screen *, void *);

static int	mgx_do_cursor(struct mgx_softc *, struct wsdisplay_cursor *);
static void	mgx_set_cursor(struct mgx_softc *);
static void	mgx_set_video(struct mgx_softc *, int);

CFATTACH_DECL_NEW(mgx, sizeof(struct mgx_softc),
    mgx_match, mgx_attach, NULL, NULL);

struct wsdisplay_accessops mgx_accessops = {
	mgx_ioctl,
	mgx_mmap,
	NULL,	/* vcons_alloc_screen */
	NULL,	/* vcons_free_screen */
	NULL,	/* vcons_show_screen */
	NULL,	/* load_font */
	NULL,	/* polls */
	NULL,	/* scroll */
};

static void	mgx_unblank(device_t);

dev_type_open(mgxopen);
dev_type_close(mgxclose);
dev_type_ioctl(mgxioctl);
dev_type_mmap(mgxmmap);

const struct cdevsw mgx_cdevsw = {
	.d_open = mgxopen,
	.d_close = mgxclose,
	.d_read = noread,
	.d_write = nowrite,
	.d_ioctl = mgxioctl,
	.d_stop = nostop,
	.d_tty = notty,
	.d_poll = nopoll,
	.d_mmap = mgxmmap,
	.d_kqfilter = nokqfilter,
	.d_discard = nodiscard,
	.d_flag = D_OTHER
};

/* frame buffer generic driver */
static struct fbdriver mgx_fbdriver = {
	mgx_unblank, mgxopen, mgxclose, mgxioctl, nopoll, mgxmmap,
	nokqfilter
};


static inline void
mgx_write_vga(struct mgx_softc *sc, uint32_t reg, uint8_t val)
{
	bus_space_write_1(sc->sc_tag, sc->sc_vgah, reg ^ 3, val);
}

static inline uint8_t
mgx_read_vga(struct mgx_softc *sc, uint32_t reg)
{
	return bus_space_read_1(sc->sc_tag, sc->sc_vgah, reg ^ 3);
}

static inline void
mgx_write_1(struct mgx_softc *sc, uint32_t reg, uint8_t val)
{
	bus_space_write_1(sc->sc_tag, sc->sc_blith, reg ^ 3, val);
}

static inline uint8_t
mgx_read_1(struct mgx_softc *sc, uint32_t reg)
{
	return bus_space_read_1(sc->sc_tag, sc->sc_blith, reg ^ 3);
}

#if 0
static inline uint32_t
mgx_read_4(struct mgx_softc *sc, uint32_t reg)
{
	return bus_space_read_4(sc->sc_tag, sc->sc_blith, reg);
}
#endif

static inline void
mgx_write_2(struct mgx_softc *sc, uint32_t reg, uint16_t val)
{
	bus_space_write_2(sc->sc_tag, sc->sc_blith, reg ^ 2, val);
}

static inline void
mgx_write_4(struct mgx_softc *sc, uint32_t reg, uint32_t val)
{
	bus_space_write_4(sc->sc_tag, sc->sc_blith, reg, val);
}

static inline void
mgx_set_dec(struct mgx_softc *sc, uint32_t dec)
{
	if (dec == sc->sc_r_dec) return;
	sc->sc_r_dec = dec;
	mgx_wait_engine(sc);
	mgx_write_4(sc, ATR_DEC, dec);
}

static inline void
mgx_set_fg(struct mgx_softc *sc, uint32_t fg)
{
	if (fg == sc->sc_r_fg) return;
	sc->sc_r_fg = fg;
	mgx_wait_fifo(sc, 1);	
	mgx_write_4(sc, ATR_FG, fg);
}

static int
mgx_match(device_t parent, cfdata_t cf, void *aux)
{
	struct sbus_attach_args *sa = aux;

	if (strcmp("SMSI,mgx", sa->sa_name) == 0)
		return 100;
	return 0;
}

/*
 * Attach a display.  We need to notice if it is the console, too.
 */
static void
mgx_attach(device_t parent, device_t self, void *args)
{
	struct mgx_softc *sc = device_private(self);
	struct sbus_attach_args *sa = args;
	struct wsemuldisplaydev_attach_args aa;
	struct fbdevice *fb = &sc->sc_fb;
	struct rasops_info *ri;
	unsigned long defattr;
	bus_space_handle_t bh;
	int node = sa->sa_node, bsize;
	int isconsole;

	aprint_normal("\n");
	sc->sc_dev = self;
	sc->sc_tag = sa->sa_bustag;

	sc->sc_paddr = sbus_bus_addr(sa->sa_bustag, sa->sa_slot,
	    sa->sa_reg[8].oa_base);
	sc->sc_rpaddr = sbus_bus_addr(sa->sa_bustag, sa->sa_slot,
	    sa->sa_reg[5].oa_base + MGX_REG_ATREG_OFFSET);

	/* read geometry information from the device tree */
	sc->sc_width = prom_getpropint(sa->sa_node, "width", 1152);
	sc->sc_height = prom_getpropint(sa->sa_node, "height", 900);
	sc->sc_stride = prom_getpropint(sa->sa_node, "linebytes", 1152);
	sc->sc_fbsize = prom_getpropint(sa->sa_node, "fb_size", 0x00400000);
	sc->sc_fbaddr = NULL;
	if (sc->sc_fbaddr == NULL) {
		if (sbus_bus_map(sa->sa_bustag,
			 sa->sa_slot,
			 sa->sa_reg[8].oa_base,
			 sc->sc_fbsize,
			 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE,
			 &bh) != 0) {
			aprint_error_dev(self, "couldn't map framebuffer\n");
			return;
		}
		sc->sc_fbaddr = bus_space_vaddr(sa->sa_bustag, bh);
	}

	if (sbus_bus_map(sa->sa_bustag,
			 sa->sa_slot,
			 sa->sa_reg[4].oa_base, 0x1000, 0,
			 &sc->sc_vgah) != 0) {
		aprint_error("%s: couldn't map VGA registers\n", 
		    device_xname(sc->sc_dev));
		return;
	}

	if (sbus_bus_map(sa->sa_bustag,
			 sa->sa_slot,
			 sa->sa_reg[5].oa_base + MGX_REG_ATREG_OFFSET, 0x1000,
			 0, &sc->sc_blith) != 0) {
		aprint_error("%s: couldn't map blitter registers\n", 
		    device_xname(sc->sc_dev));
		return;
	}

	mgx_setup(sc, MGX_DEPTH);

	aprint_normal_dev(self, "[%s] %d MB framebuffer, %d x %d\n",
		sc->sc_name, sc->sc_fbsize >> 20, sc->sc_width, sc->sc_height);


	sc->sc_defaultscreen_descr = (struct wsscreen_descr) {
		"default",
		0, 0,
		NULL,
		8, 16,
		WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
		WSSCREEN_RESIZE,
		NULL
	};
	
	sc->sc_cursor_x = 0;
	sc->sc_cursor_y = 0;
	sc->sc_hotspot_x = 0;
	sc->sc_hotspot_y = 0;
	sc->sc_video = WSDISPLAYIO_VIDEO_ON;

	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};

	isconsole = fb_is_console(node);

	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
	wsfont_init();

	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr, &mgx_accessops);
	sc->vd.init_screen = mgx_init_screen;
	sc->vd.show_screen_cookie = sc;
	sc->vd.show_screen_cb = mgx_adapt;

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

	ri = &sc->sc_console_screen.scr_ri;

	sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
	sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
	sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
	sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;

	sc->sc_gc.gc_bitblt = mgx_bitblt;
	sc->sc_gc.gc_rectfill = mgx_rectfill;
	sc->sc_gc.gc_blitcookie = sc;
	sc->sc_gc.gc_rop = ROP_SRC;

	/* 
	 * leave some room between visible screen and glyph cache for upload
	 * buffers used by putchar_mono()
	 */
	bsize = (32 * 1024 + sc->sc_stride - 1) / sc->sc_stride;
	glyphcache_init(&sc->sc_gc,
	    sc->sc_height + bsize,
	    (0x400000 / sc->sc_stride) - (sc->sc_height + bsize),
	    sc->sc_width,
	    ri->ri_font->fontwidth,
	    ri->ri_font->fontheight,
	    defattr);

	mgx_init_palette(sc);

	if(isconsole) {
		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
		    defattr);
		vcons_replay_msgbuf(&sc->sc_console_screen);
	}

	aa.console = isconsole;
	aa.scrdata = &sc->sc_screenlist;
	aa.accessops = &mgx_accessops;
	aa.accesscookie = &sc->vd;

	config_found(self, &aa, wsemuldisplaydevprint, CFARGS_NONE);

	/* now the Sun fb goop */
	fb->fb_driver = &mgx_fbdriver;
	fb->fb_device = sc->sc_dev;
	fb->fb_flags = device_cfdata(sc->sc_dev)->cf_flags & FB_USERMASK;
	fb->fb_type.fb_type = FBTYPE_MGX;
	fb->fb_pixels = NULL;

	fb->fb_type.fb_depth = 32;
	fb->fb_type.fb_width = sc->sc_width;
	fb->fb_type.fb_height = sc->sc_height;
	fb->fb_linebytes = sc->sc_stride * 4;

	fb->fb_type.fb_cmsize = 256;
	fb->fb_type.fb_size = sc->sc_fbsize;
	fb_attach(&sc->sc_fb, isconsole);
}

static void
mgx_write_dac(struct mgx_softc *sc, int idx, int r, int g, int b)
{
	mgx_write_vga(sc, VGA_BASE + VGA_DAC_ADDRW, idx);
	mgx_write_vga(sc, VGA_BASE + VGA_DAC_PALETTE, r);
	mgx_write_vga(sc, VGA_BASE + VGA_DAC_PALETTE, g);
	mgx_write_vga(sc, VGA_BASE + VGA_DAC_PALETTE, b);
}

static void
mgx_init_palette(struct mgx_softc *sc)
{
	struct rasops_info *ri = &sc->sc_console_screen.scr_ri;
	int i, j = 0;
	uint8_t cmap[768];

	if (sc->sc_depth == 8) {
		rasops_get_cmap(ri, cmap, sizeof(cmap));
		for (i = 0; i < 256; i++) {
			sc->sc_cmap_red[i] = cmap[j];
			sc->sc_cmap_green[i] = cmap[j + 1];
			sc->sc_cmap_blue[i] = cmap[j + 2];
			mgx_write_dac(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
			j += 3;
		}
	} else {
		/* linear ramp for true colour modes */
		for (i = 0; i < 256; i++) {
			mgx_write_dac(sc, i, i, i, i);
		}
	}
}

static int
mgx_putcmap(struct mgx_softc *sc, struct wsdisplay_cmap *cm)
{
	u_char *r, *g, *b;
	u_int index = cm->index;
	u_int count = cm->count;
	int i, error;
	u_char rbuf[256], gbuf[256], bbuf[256];

	if (cm->index >= 256 || cm->count > 256 ||
	    (cm->index + cm->count) > 256)
		return EINVAL;
	error = copyin(cm->red, &rbuf[index], count);
	if (error)
		return error;
	error = copyin(cm->green, &gbuf[index], count);
	if (error)
		return error;
	error = copyin(cm->blue, &bbuf[index], count);
	if (error)
		return error;

	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);

	r = &sc->sc_cmap_red[index];
	g = &sc->sc_cmap_green[index];
	b = &sc->sc_cmap_blue[index];
	
	for (i = 0; i < count; i++) {
		mgx_write_dac(sc, index, *r, *g, *b);
		index++;
		r++, g++, b++;
	}
	return 0;
}

static int
mgx_getcmap(struct mgx_softc *sc, struct wsdisplay_cmap *cm)
{
	u_int index = cm->index;
	u_int count = cm->count;
	int error;

	if (index >= 255 || count > 256 || index + count > 256)
		return EINVAL;
	
	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
	if (error)
		return error;
	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
	if (error)
		return error;
	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
	if (error)
		return error;

	return 0;
}

static int
mgx_wait_engine(struct mgx_softc *sc)
{
	unsigned int i;
	uint8_t stat;

	for (i = 100000; i != 0; i--) {
		stat = mgx_read_1(sc, ATR_BLT_STATUS);
		if ((stat & (BLT_HOST_BUSY | BLT_ENGINE_BUSY)) == 0)
			break;
	}

	return i;
}

static inline int
mgx_wait_host(struct mgx_softc *sc)
{
	unsigned int i;
	uint8_t stat;

	for (i = 10000; i != 0; i--) {
		stat = mgx_read_1(sc, ATR_BLT_STATUS);
		if ((stat & BLT_HOST_BUSY) == 0)
			break;
	}

	return i;
}

/*static inline*/ int
mgx_wait_fifo(struct mgx_softc *sc, unsigned int nfifo)
{
	unsigned int i;
	uint8_t stat;

	for (i = 100000; i != 0; i--) {
		stat = mgx_read_1(sc, ATR_FIFO_STATUS);
		stat = (stat & FIFO_MASK) >> FIFO_SHIFT;
		if (stat >= nfifo)
			break;
		mgx_write_1(sc, ATR_FIFO_STATUS, 0);
	}

	return i;
}

static void
mgx_setup(struct mgx_softc *sc, int depth)
{
	uint32_t stride;
	int i;
	uint8_t reg;

	sc->sc_r_dec = 0xffffffff;
	sc->sc_r_fg = 0x12345678;
	/* wait for everything to go idle */
	if (mgx_wait_engine(sc) == 0)
		return;
	if (mgx_wait_fifo(sc, FIFO_AT24) == 0)
		return;

	sc->sc_buf = 0;
	/* read name from sequencer */
	for (i = 0; i < 8; i++) {
		mgx_write_vga(sc, SEQ_INDEX, i + 0x11);
		sc->sc_name[i] = mgx_read_vga(sc, SEQ_DATA);
	}
	sc->sc_name[7] = 0;

	reg = mgx_read_1(sc, ATR_PIXEL);
	reg &= ~PIXEL_DEPTH_MASK;

	switch (depth) {
		case 8:
			sc->sc_dec = DEC_DEPTH_8 << DEC_DEPTH_SHIFT;
			reg |= PIXEL_8;
			break;
		case 15:
			sc->sc_dec = DEC_DEPTH_16 << DEC_DEPTH_SHIFT;
			reg |= PIXEL_15;
			break;
		case 16:
			sc->sc_dec = DEC_DEPTH_16 << DEC_DEPTH_SHIFT;
			reg |= PIXEL_16;
			break;
		case 32:
			sc->sc_dec = DEC_DEPTH_32 << DEC_DEPTH_SHIFT;
			reg |= PIXEL_32;
			break;
		default:
			return; /* not supported */
	}

	/* the chip wants stride in units of 8 bytes */
	sc->sc_stride = sc->sc_width * (depth >> 3);
	stride = sc->sc_stride >> 3;
#ifdef MGX_DEBUG
	sc->sc_height -= 150;
#endif

	sc->sc_depth = depth;

	switch (sc->sc_width) {
		case 640:
			sc->sc_dec |= DEC_WIDTH_640 << DEC_WIDTH_SHIFT;
			break;
		case 800:
			sc->sc_dec |= DEC_WIDTH_800 << DEC_WIDTH_SHIFT;
			break;
		case 1024:
			sc->sc_dec |= DEC_WIDTH_1024 << DEC_WIDTH_SHIFT;
			break;
		case 1152:
			sc->sc_dec |= DEC_WIDTH_1152 << DEC_WIDTH_SHIFT;
			break;
		case 1280:
			sc->sc_dec |= DEC_WIDTH_1280 << DEC_WIDTH_SHIFT;
			break;
		case 1600:
			sc->sc_dec |= DEC_WIDTH_1600 << DEC_WIDTH_SHIFT;
			break;
		default:
			return; /* not supported */
	}
	mgx_wait_fifo(sc, 4);
	mgx_write_1(sc, ATR_CLIP_CONTROL, 0);
	mgx_write_1(sc, ATR_BYTEMASK, 0xff);
	mgx_write_1(sc, ATR_PIXEL, reg);
	mgx_write_4(sc, ATR_OFFSET, 0);
	mgx_wait_fifo(sc, 4);
	mgx_write_vga(sc, CRTC_INDEX, 0x13);
	mgx_write_vga(sc, CRTC_DATA, stride & 0xff);
	mgx_write_vga(sc, CRTC_INDEX, 0x1c);
	mgx_write_vga(sc, CRTC_DATA, (stride & 0xf00) >> 4);

	/* clean up the screen if we're switching to != 8bit */
	if (depth != MGX_DEPTH) 
		mgx_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, 0);	

	mgx_wait_fifo(sc, 4);
	/* initialize hardware cursor stuff */
	mgx_write_2(sc, ATR_CURSOR_ADDRESS, (sc->sc_fbsize - 1024) >> 10);
	mgx_write_1(sc, ATR_CURSOR_ENABLE, 0);
	sc->sc_cursor = (uint8_t *)sc->sc_fbaddr + sc->sc_fbsize - 1024;
	memset(sc->sc_cursor, 0xf0, 1024);
	memset(sc->sc_in, 0, sizeof(sc->sc_in));
}

static void
mgx_bitblt(void *cookie, int xs, int ys, int xd, int yd, int wi, int he,
             int rop)
{
	struct mgx_softc *sc = cookie;
	uint32_t dec = sc->sc_dec;

        dec |= (DEC_COMMAND_BLT << DEC_COMMAND_SHIFT) |
	       (DEC_START_DIMX << DEC_START_SHIFT);
	if (xs < xd) {
		xs += wi - 1;
		xd += wi - 1;
		dec |= DEC_DIR_X_REVERSE;
	}
	if (ys < yd) {
		ys += he - 1;
		yd += he - 1;
		dec |= DEC_DIR_Y_REVERSE;
	}
	mgx_set_dec(sc, dec);
	mgx_wait_fifo(sc, 4);
	mgx_write_1(sc, ATR_ROP, rop);
	mgx_write_4(sc, ATR_SRC_XY, (ys << 16) | xs);
	mgx_write_4(sc, ATR_DST_XY, (yd << 16) | xd);
	mgx_write_4(sc, ATR_WH, (he << 16) | wi);
}
	
static void
mgx_rectfill(void *cookie, int x, int y, int wi, int he, long fg)
{
	struct mgx_softc *sc = cookie;
	struct vcons_screen *scr = sc->vd.active;
	uint32_t dec = sc->sc_dec;
	uint32_t col;

	if (scr == NULL)
		return;
	col = scr->scr_ri.ri_devcmap[fg];

	dec = sc->sc_dec;
	dec |= (DEC_COMMAND_RECT << DEC_COMMAND_SHIFT) |
	       (DEC_START_DIMX << DEC_START_SHIFT);
	mgx_set_dec(sc, dec);
	mgx_set_fg(sc, col);
	mgx_wait_fifo(sc, 3);
	mgx_write_1(sc, ATR_ROP, ROP_SRC);
	mgx_write_4(sc, ATR_DST_XY, (y << 16) | x);
	mgx_write_4(sc, ATR_WH, (he << 16) | wi);
}

static void
mgx_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
{
	struct rasops_info *ri = cookie;
	struct wsdisplay_font *font = PICK_FONT(ri, c);
	struct vcons_screen *scr = ri->ri_hw;
	struct mgx_softc *sc = scr->scr_cookie;
	uint32_t fg, bg;
	int x, y, wi, he, rv;

	wi = font->fontwidth;
	he = font->fontheight;

	bg = (attr >> 16) & 0xf;
	fg = (attr >> 24) & 0xf;

	x = ri->ri_xorigin + col * wi;
	y = ri->ri_yorigin + row * he;

	if (c == 0x20) {
		mgx_rectfill(sc, x, y, wi, he, bg);
		if (attr & 1)
			mgx_rectfill(sc, x, y + he - 2, wi, 1, fg);
		return;
	}

	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
	if (rv != GC_OK) {
		volatile uint32_t junk;

		mgx_wait_engine(sc);
		sc->sc_putchar(cookie, row, col, c, attr & ~1);
		if (rv == GC_ADD) {
			/*
			 * try to make sure the glyph made it all the way to
			 * video memory before trying to blit it into the cache
			 */ 
			junk = *(uint32_t *)sc->sc_fbaddr;
			__USE(junk);
			glyphcache_add(&sc->sc_gc, c, x, y);
		}
	}
	if (attr & 1)
		mgx_rectfill(sc, x, y + he - 2, wi, 1, fg);
}

static void
mgx_putchar_mono(void *cookie, int row, int col, u_int c, long attr)
{
	struct rasops_info *ri = cookie;
	struct wsdisplay_font *font = PICK_FONT(ri, c);
	struct vcons_screen *scr = ri->ri_hw;
	struct mgx_softc *sc = scr->scr_cookie;
	uint8_t *s, *d;
	uint32_t fg, bg, scratch = ((sc->sc_stride * sc->sc_height) + 7) & ~7;
	int x, y, wi, he, len, i;

	wi = font->fontwidth;
	he = font->fontheight;

	bg = (attr >> 16) & 0xf;
	fg = (attr >> 24) & 0xf;

	x = ri->ri_xorigin + col * wi;
	y = ri->ri_yorigin + row * he;

	if (!CHAR_IN_FONT(c, font)) {
		c = 0x20;
#ifdef MGX_DEBUG
		bg = WSCOL_LIGHT_BLUE;
#endif
	}
	if (c == 0x20) {
		mgx_rectfill(sc, x, y, wi, he, bg);
		if (attr & 1)
			mgx_rectfill(sc, x, y + he - 2, wi, 1, fg);
		return;
	}

	mgx_set_fg(sc, ri->ri_devcmap[fg]);
	mgx_wait_fifo(sc, 2);
	mgx_write_4(sc, ATR_BG, ri->ri_devcmap[bg]);
	mgx_write_1(sc, ATR_ROP, ROP_SRC);

	/*
	 * do hardware colour expansion
	 * there has to be an upload port somewhere, since there are host blit
	 * commands, but it's not used or mentioned in the xf86-video-apm driver
	 * so for now we use the vram-to-vram blits to draw characters.
	 * stash most of the font in vram for speed, also:
	 * - the sbus-pci bridge doesn't seem to support 64bit accesses,
	 *   they will cause occasional data corruption and all sorts of weird
	 *   side effects, so copy font bitmaps byte-wise
	 * - at least it doesn't seem to need any kind of buffer flushing
	 * - still use rotation buffers for characters that don't fall into the
	 *   8 bit range
	 */

	len = (ri->ri_fontscale + 7) & ~7;
	s = WSFONT_GLYPH(c, font);
	if ((c > 32) && (c < 256)) {
		scratch += len * c;
		if (sc->sc_in[c] == 0) {
			d = (uint8_t *)sc->sc_fbaddr + scratch;
			for (i = 0; i < ri->ri_fontscale; i++)
				d[i] = s[i];
			sc->sc_in[c] = 1;
		}
	} else {
		sc->sc_buf = (sc->sc_buf + 1) & 7; /* rotate through 8 buffers */
		scratch += sc->sc_buf * len;
		d = (uint8_t *)sc->sc_fbaddr + scratch;
		for (i = 0; i < ri->ri_fontscale; i++)
			d[i] = s[i];
	}
	mgx_set_dec(sc, sc->sc_dec | (DEC_COMMAND_BLT << DEC_COMMAND_SHIFT) |
	       (DEC_START_DIMX << DEC_START_SHIFT) |
	       DEC_SRC_LINEAR | DEC_SRC_CONTIGUOUS | DEC_MONOCHROME);
	mgx_wait_fifo(sc, 3);
	mgx_write_4(sc, ATR_SRC_XY, ((scratch & 0xfff000) << 4) | (scratch & 0xfff));
	mgx_write_4(sc, ATR_DST_XY, (y << 16) | x);
	mgx_write_4(sc, ATR_WH, (he << 16) | wi);

	if (attr & 1)
		mgx_rectfill(sc, x, y + he - 2, wi, 1, fg);
}

static void
mgx_cursor(void *cookie, int on, int row, int col)
{
	struct rasops_info *ri = cookie;
	struct vcons_screen *scr = ri->ri_hw;
	struct mgx_softc *sc = scr->scr_cookie;
	int x, y, wi,he;

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

	if (ri->ri_flg & RI_CURSOR) {
		x = ri->ri_ccol * wi + ri->ri_xorigin;
		y = ri->ri_crow * he + ri->ri_yorigin;
		mgx_bitblt(sc, x, y, x, y, wi, he, ROP_INV);
		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;
		mgx_bitblt(sc, x, y, x, y, wi, he, ROP_INV);
		ri->ri_flg |= RI_CURSOR;
	}
}

static void
mgx_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
{
	struct rasops_info *ri = cookie;
	struct vcons_screen *scr = ri->ri_hw;
	struct mgx_softc *sc = scr->scr_cookie;
	int32_t xs, xd, y, width, height;

	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;
	mgx_bitblt(sc, xs, y, xd, y, width, height, ROP_SRC);
}

static void
mgx_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
{
	struct rasops_info *ri = cookie;
	struct vcons_screen *scr = ri->ri_hw;
	struct mgx_softc *sc = scr->scr_cookie;
	int32_t x, y, width, height, bg;

	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;
	bg = (fillattr >> 16) & 0xff;
	mgx_rectfill(sc, x, y, width, height, bg);
}

static void
mgx_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
{
	struct rasops_info *ri = cookie;
	struct vcons_screen *scr = ri->ri_hw;
	struct mgx_softc *sc = scr->scr_cookie;
	int32_t x, ys, yd, width, height;

	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;
	mgx_bitblt(sc, x, ys, x, yd, width, height, ROP_SRC);
}

static void
mgx_eraserows(void *cookie, int row, int nrows, long fillattr)
{
	struct rasops_info *ri = cookie;
	struct vcons_screen *scr = ri->ri_hw;
	struct mgx_softc *sc = scr->scr_cookie;
	int32_t x, y, width, height, bg;

	if ((row == 0) && (nrows == ri->ri_rows)) {
		x = y = 0;
		width = ri->ri_width;
		height = ri->ri_height;
	} else {
		x = ri->ri_xorigin;
		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
		width = ri->ri_emuwidth;
		height = ri->ri_font->fontheight * nrows;
	}
	bg = (fillattr >> 16) & 0xff;
	mgx_rectfill(sc, x, y, width, height, bg);
}

static void
mgx_adapt(struct vcons_screen *scr, void *cookie)
{
	struct mgx_softc *sc = cookie;
	memset(sc->sc_in, 0, sizeof(sc->sc_in));
	glyphcache_adapt(scr, &sc->sc_gc);
}

static void
mgx_init_screen(void *cookie, struct vcons_screen *scr,
    int existing, long *defattr)
{
	struct mgx_softc *sc = cookie;
	struct rasops_info *ri = &scr->scr_ri;

	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_flg = RI_CENTER | RI_ENABLE_ALPHA;

	if (ri->ri_depth == 8)
		ri->ri_flg |= RI_8BIT_IS_RGB;

#ifdef MGX_NOACCEL
	scr->scr_flags |= VCONS_DONT_READ;
#endif
	scr->scr_flags |= VCONS_LOADFONT;

	ri->ri_rnum = 8;
	ri->ri_rpos = 0;
	ri->ri_gnum = 8;
	ri->ri_gpos = 8;
	ri->ri_bnum = 8;
	ri->ri_bpos = 16;

	ri->ri_bits = sc->sc_fbaddr;

	rasops_init(ri, 0, 0);

	ri->ri_caps = WSSCREEN_REVERSE | WSSCREEN_WSCOLORS |
		      WSSCREEN_UNDERLINE | WSSCREEN_RESIZE;

	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
		    ri->ri_width / ri->ri_font->fontwidth);

	ri->ri_hw = scr;
	
#ifdef MGX_NOACCEL
if (0)
#endif
	{
		if (FONT_IS_ALPHA(ri->ri_font)) {
			sc->sc_putchar = ri->ri_ops.putchar;
			ri->ri_ops.putchar   = mgx_putchar_aa;
		} else {
			ri->ri_ops.putchar   = mgx_putchar_mono;
		}
		ri->ri_ops.cursor    = mgx_cursor;
		ri->ri_ops.copyrows  = mgx_copyrows;
		ri->ri_ops.eraserows = mgx_eraserows;
		ri->ri_ops.copycols  = mgx_copycols;
		ri->ri_ops.erasecols = mgx_erasecols;
	}
}

static int
mgx_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    struct lwp *l)
{
	struct vcons_data *vd = v;
	struct mgx_softc *sc = vd->cookie;
	struct wsdisplay_fbinfo *wdf;
	struct vcons_screen *ms = vd->active;

	switch (cmd) {
	case WSDISPLAYIO_GTYPE:
		*(u_int *)data = WSDISPLAY_TYPE_MGX;
		return 0;

	case WSDISPLAYIO_GINFO:
		wdf = (void *)data;
		wdf->height = sc->sc_height;
		wdf->width = sc->sc_width;
		wdf->depth = 8;
		wdf->cmsize = 256;
		return 0;

	case FBIOGTYPE:
		*(struct fbtype *)data = sc->sc_fb.fb_type;
		return 0;

	case FBIOGATTR:
#define fba ((struct fbgattr *)data)
		fba->real_type = sc->sc_fb.fb_type.fb_type;
		fba->owner = 0;		/* XXX ??? */
		fba->fbtype = sc->sc_fb.fb_type;
		fba->sattr.flags = 0;
		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
		fba->sattr.dev_specific[0] = -1;
		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
		fba->emu_types[1] = -1;
#undef fba
		return 0;
	case FBIOGVIDEO:
	case WSDISPLAYIO_GVIDEO:
		*(int *)data = sc->sc_video;
		return 0;

	case WSDISPLAYIO_SVIDEO:
	case FBIOSVIDEO:
		mgx_set_video(sc, *(int *)data);
		return 0;

	case WSDISPLAYIO_LINEBYTES:
		{
			int *ret = (int *)data;
			*ret = sc->sc_stride;
		}
		return 0;

	case WSDISPLAYIO_SMODE:
		{
			int new_mode = *(int*)data;
			if (new_mode != sc->sc_mode)
			{
				sc->sc_mode = new_mode;
				if (new_mode == WSDISPLAYIO_MODE_EMUL)
				{
					mgx_setup(sc, MGX_DEPTH);
					glyphcache_wipe(&sc->sc_gc);
					mgx_init_palette(sc);
					vcons_redraw_screen(ms);
				} else {
					mgx_setup(sc, 32);
					mgx_init_palette(sc);
				}
			}
		}
		return 0;

	case WSDISPLAYIO_GETCMAP:
		return mgx_getcmap(sc, (struct wsdisplay_cmap *)data);

	case WSDISPLAYIO_PUTCMAP:
		return mgx_putcmap(sc, (struct wsdisplay_cmap *)data);

	case WSDISPLAYIO_GCURPOS:
		{
			struct wsdisplay_curpos *cp = (void *)data;

			cp->x = sc->sc_cursor_x;
			cp->y = sc->sc_cursor_y;
		}
		return 0;

	case WSDISPLAYIO_SCURPOS:
		{
			struct wsdisplay_curpos *cp = (void *)data;

			sc->sc_cursor_x = cp->x;
			sc->sc_cursor_y = cp->y;
			mgx_set_cursor(sc);
		}
		return 0;

	case WSDISPLAYIO_GCURMAX:
		{
			struct wsdisplay_curpos *cp = (void *)data;

			cp->x = 64;
			cp->y = 64;
		}
		return 0;

	case WSDISPLAYIO_SCURSOR:
		{
			struct wsdisplay_cursor *cursor = (void *)data;

			return mgx_do_cursor(sc, cursor);
		}

	case WSDISPLAYIO_GET_FBINFO:
		{
			struct wsdisplayio_fbinfo *fbi = data;
	
			fbi->fbi_fbsize = sc->sc_fbsize - 1024;
			fbi->fbi_width = sc->sc_width;
			fbi->fbi_height = sc->sc_height;
			fbi->fbi_bitsperpixel = sc->sc_depth;
			fbi->fbi_stride = sc->sc_stride;
			fbi->fbi_pixeltype = WSFB_RGB;
			fbi->fbi_subtype.fbi_rgbmasks.red_offset = 8;
			fbi->fbi_subtype.fbi_rgbmasks.red_size = 8;
			fbi->fbi_subtype.fbi_rgbmasks.green_offset = 16;
			fbi->fbi_subtype.fbi_rgbmasks.green_size = 8;
			fbi->fbi_subtype.fbi_rgbmasks.blue_offset = 24;
			fbi->fbi_subtype.fbi_rgbmasks.blue_size = 8;
			fbi->fbi_subtype.fbi_rgbmasks.alpha_offset = 0;
			fbi->fbi_subtype.fbi_rgbmasks.alpha_size = 8;
			return 0;
		}
	}
	return EPASSTHROUGH;
}

static paddr_t
mgx_mmap(void *v, void *vs, off_t offset, int prot)
{
	struct vcons_data *vd = v;
	struct mgx_softc *sc = vd->cookie;
	uint32_t flags = BUS_SPACE_MAP_LINEAR;

#ifdef BUS_SPACE_MAP_LITTLE
	if (offset & MGX_FLIPOFFSET) {
		offset &= ~MGX_FLIPOFFSET;
		flags |= BUS_SPACE_MAP_LITTLE;
	}
#endif

	/* regular fb mapping at 0 */
	if ((offset >= 0) && (offset < sc->sc_fbsize)) {
		return bus_space_mmap(sc->sc_tag, sc->sc_paddr,
		    offset, prot, flags);
	}

	/*
	 * Blitter registers at 0x00800000, only in mapped mode.
	 * Restrict to root, even though I'm fairly sure the DMA engine lives
	 * elsewhere ( and isn't documented anyway )
	 */
	if (kauth_authorize_machdep(kauth_cred_get(),
	    KAUTH_MACHDEP_UNMANAGEDMEM,
	    NULL, NULL, NULL, NULL) != 0) {
		aprint_normal("%s: mmap() rejected.\n",
		    device_xname(sc->sc_dev));
		return -1;
	}
	if ((sc->sc_mode == WSDISPLAYIO_MODE_MAPPED) &&
	    (offset >= MGX_BLTOFFSET) && (offset < MGX_BLTOFFSET + 0x1000)) {
		return bus_space_mmap(sc->sc_tag, sc->sc_rpaddr,
		    offset - MGX_BLTOFFSET, prot, BUS_SPACE_MAP_LINEAR);
	}
	return -1;
}

static int
mgx_do_cursor(struct mgx_softc *sc, struct wsdisplay_cursor *cur)
{
	int i;
	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {

		if (cur->enable) {
			mgx_set_cursor(sc);
			mgx_write_1(sc, ATR_CURSOR_ENABLE, 1);
		} else {
			mgx_write_1(sc, ATR_CURSOR_ENABLE, 0);
		}
	}
	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {

		sc->sc_hotspot_x = cur->hot.x;
		sc->sc_hotspot_y = cur->hot.y;
		mgx_set_cursor(sc);
	}
	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {

		sc->sc_cursor_x = cur->pos.x;
		sc->sc_cursor_y = cur->pos.y;
		mgx_set_cursor(sc);
	}
	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
		int cnt = uimin(2, cur->cmap.count);
		uint8_t c;
		uint8_t r[2], g[2], b[2];

		copyin(cur->cmap.red, r, cnt);
		copyin(cur->cmap.green, g, cnt);
		copyin(cur->cmap.blue, b, cnt);
	
		for (i = 0; i < cnt; i++) {
			c = r[i] & 0xe0;
			c |= (g[i] & 0xe0) >> 3;
			c |= (b[i] & 0xc0) >> 6;
			mgx_write_1(sc, ATR_CURSOR_FG + i, c);
		}
	}
	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
		int j;
		uint8_t *fb = sc->sc_cursor;
		uint8_t temp;
		uint8_t im, ma, px;

		for (i = 0; i < 512; i++) {
			temp = 0;
			copyin(&cur->image[i], &im, 1);
			copyin(&cur->mask[i], &ma, 1);
			for (j = 0; j < 4; j++) {
				temp >>= 2;
				px = (ma & 1) ? 0 : 0x80;
				if (px == 0)
					px |= (im & 1) ? 0 : 0x40;
				temp |= px;
				im >>= 1;
				ma >>= 1;
			}
			*fb = temp;
			fb++;
			temp = 0;
			for (j = 0; j < 4; j++) {
				temp >>= 2;
				px = (ma & 1) ? 0 : 0x80;
				if (px == 0)
					px |= (im & 1) ? 0 : 0x40;
				temp |= px;
				im >>= 1;
				ma >>= 1;
			}
			*fb = temp;
			fb++;
		}
	}
	return 0;
}

static void
mgx_set_cursor(struct mgx_softc *sc)
{
	uint32_t reg;
	uint16_t hot;

	reg = (sc->sc_cursor_y << 16) | (sc->sc_cursor_x & 0xffff);
	mgx_write_4(sc, ATR_CURSOR_POSITION, reg);
	hot = (sc->sc_hotspot_y << 8) | (sc->sc_hotspot_x & 0xff);
	mgx_write_2(sc, ATR_CURSOR_HOTSPOT, hot);
}

static void
mgx_set_video(struct mgx_softc *sc, int v)
{
	uint8_t reg;

	if (sc->sc_video == v)
		return;

	sc->sc_video = v;
	reg = mgx_read_1(sc, ATR_DPMS);

	if (v == WSDISPLAYIO_VIDEO_ON) {
		reg &= ~DPMS_SYNC_DISABLE_ALL;
	} else {
		reg |= DPMS_SYNC_DISABLE_ALL;
	}
	mgx_write_1(sc, ATR_DPMS, reg);
}

/* Sun fb dev goop */
static void
mgx_unblank(device_t dev)
{
	struct mgx_softc *sc = device_private(dev);

	mgx_set_video(sc, WSDISPLAYIO_VIDEO_ON);
}

paddr_t
mgxmmap(dev_t dev, off_t offset, int prot)
{
	struct mgx_softc *sc = device_lookup_private(&mgx_cd, minor(dev));
	uint32_t flags = BUS_SPACE_MAP_LINEAR;

#ifdef BUS_SPACE_MAP_LITTLE
	if (offset & MGX_FLIPOFFSET) {
		offset &= ~MGX_FLIPOFFSET;
		flags |= BUS_SPACE_MAP_LITTLE;
	}
#endif

	/* regular fb mapping at 0 */
	if ((offset >= 0) && (offset < sc->sc_fbsize)) {
		return bus_space_mmap(sc->sc_tag, sc->sc_paddr,
		    offset, prot, flags);
	}

	/*
	 * Blitter registers at 0x00800000, only in mapped mode.
	 * Restrict to root, even though I'm fairly sure the DMA engine lives
	 * elsewhere ( and isn't documented anyway )
	 */
	if (kauth_authorize_machdep(kauth_cred_get(),
	    KAUTH_MACHDEP_UNMANAGEDMEM,
	    NULL, NULL, NULL, NULL) != 0) {
		aprint_normal("%s: mmap() rejected.\n",
		    device_xname(sc->sc_dev));
		return -1;
	}
	if ((sc->sc_mode == WSDISPLAYIO_MODE_MAPPED) &&
	    (offset >= MGX_BLTOFFSET) && (offset < MGX_BLTOFFSET + 0x1000)) {
		return bus_space_mmap(sc->sc_tag, sc->sc_rpaddr,
		    offset - MGX_BLTOFFSET, prot, BUS_SPACE_MAP_LINEAR);
	}
	return -1;
}

int
mgxopen(dev_t dev, int flags, int mode, struct lwp *l)
{
	device_t dv = device_lookup(&mgx_cd, minor(dev));
	struct mgx_softc *sc = device_private(dv);

	if (dv == NULL)
		return ENXIO;
	if (sc->sc_mode == WSDISPLAYIO_MODE_MAPPED)
		return 0;
	sc->sc_mode = WSDISPLAYIO_MODE_MAPPED;
	mgx_setup(sc, 32);
	mgx_init_palette(sc);
	return 0;
}

int
mgxclose(dev_t dev, int flags, int mode, struct lwp *l)
{
	device_t dv = device_lookup(&mgx_cd, minor(dev));
	struct mgx_softc *sc = device_private(dv);
	struct vcons_screen *ms = sc->vd.active;

	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)
		return 0;

	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;

	mgx_setup(sc, MGX_DEPTH);
	glyphcache_wipe(&sc->sc_gc);
	mgx_init_palette(sc);
	if (ms != NULL) {
		vcons_redraw_screen(ms);
	}
	return 0;
}

int
mgxioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
{
	struct mgx_softc *sc = device_lookup_private(&mgx_cd, minor(dev));

	return mgx_ioctl(&sc->vd, NULL, cmd, data, flags, l);
}