summaryrefslogtreecommitdiff
path: root/sys/dev/ic/ct65550.c
blob: f92631153e00654dd26c239b41c4734129db353d (plain)
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
/*	$NetBSD: ct65550.c,v 1.15 2021/08/07 16:19:12 thorpej Exp $	*/

/*
 * Copyright (c) 2006 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 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.
 */

/*
 * A console driver for Chips & Technologies 65550 graphics controllers
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ct65550.c,v 1.15 2021/08/07 16:19:12 thorpej Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/kauth.h>
#include <sys/bus.h>
#include <dev/videomode/videomode.h>

#include <dev/ic/ct65550reg.h>
#include <dev/ic/ct65550var.h>

#include "opt_wsemul.h"
#include "opt_chipsfb.h"

static struct vcons_screen chipsfb_console_screen;

extern const u_char rasops_cmap[768];

static void 	chipsfb_init(struct chipsfb_softc *);

static void	chipsfb_cursor(void *, int, int, int);
static void	chipsfb_copycols(void *, int, int, int, int);
static void	chipsfb_erasecols(void *, int, int, int, long);
static void	chipsfb_copyrows(void *, int, int, int);
static void	chipsfb_eraserows(void *, int, int, long);

#if 0
static int	chipsfb_allocattr(void *, int, int, int, long *);
static void	chipsfb_scroll(void *, void *, int);
static int	chipsfb_load_font(void *, void *, struct wsdisplay_font *);
#endif

static int	chipsfb_putcmap(struct chipsfb_softc *,
			    struct wsdisplay_cmap *);
static int 	chipsfb_getcmap(struct chipsfb_softc *,
			    struct wsdisplay_cmap *);
static int 	chipsfb_putpalreg(struct chipsfb_softc *, uint8_t, uint8_t,
			    uint8_t, uint8_t);

static void	chipsfb_bitblt(void *, int, int, int, int,
			    int, int, int);
static void	chipsfb_rectfill(struct chipsfb_softc *, int, int, int, int,
			    int);
static void	chipsfb_putchar(void *, int, int, u_int, long);
static void	chipsfb_putchar_aa(void *, int, int, u_int, long);
static void	chipsfb_setup_mono(struct chipsfb_softc *, int, int, int,
			    int, uint32_t, uint32_t);
static void	chipsfb_feed(struct chipsfb_softc *, int, uint8_t *);

#if 0
static void	chipsfb_showpal(struct chipsfb_softc *);
#endif
static void	chipsfb_restore_palette(struct chipsfb_softc *);

static void	chipsfb_wait_idle(struct chipsfb_softc *);

struct wsscreen_descr chipsfb_defaultscreen = {
	"default",	/* name */
	0, 0,		/* ncols, nrows */
	NULL,		/* textops */
	8, 16,		/* fontwidth, fontheight */
	WSSCREEN_WSCOLORS | WSSCREEN_HILIT, /* capabilities */
	NULL,		/* modecookie */
};

const struct wsscreen_descr *_chipsfb_scrlist[] = {
	&chipsfb_defaultscreen,
	/* XXX other formats, graphics screen? */
};

struct wsscreen_list chipsfb_screenlist = {
	sizeof(_chipsfb_scrlist) / sizeof(struct wsscreen_descr *), _chipsfb_scrlist
};

static int	chipsfb_ioctl(void *, void *, u_long, void *, int,
		    struct lwp *);
static paddr_t	chipsfb_mmap(void *, void *, off_t, int);
static void	chipsfb_clearscreen(struct chipsfb_softc *);
static void	chipsfb_init_screen(void *, struct vcons_screen *, int,
			    long *);


struct wsdisplay_accessops chipsfb_accessops = {
	chipsfb_ioctl,
	chipsfb_mmap,
	NULL,	/* vcons_alloc_screen */
	NULL,	/* vcons_free_screen */
	NULL,	/* vcons_show_screen */
	NULL,	/* load_font */
	NULL,	/* polls */
	NULL,	/* scroll */
};

/*
 * Inline functions for getting access to register aperture.
 */
static inline void
chipsfb_write32(struct chipsfb_softc *sc, uint32_t reg, uint32_t val)
{
	bus_space_write_4(sc->sc_memt, sc->sc_mmregh, reg, val);
}

static inline uint32_t
chipsfb_read32(struct chipsfb_softc *sc, uint32_t reg)
{
	return bus_space_read_4(sc->sc_memt, sc->sc_mmregh, reg);
}

static inline void
chipsfb_write_vga(struct chipsfb_softc *sc, uint32_t reg,  uint8_t val)
{
	bus_space_write_1(sc->sc_iot, sc->sc_ioregh, reg, val);
}

static inline uint8_t
chipsfb_read_vga(struct chipsfb_softc *sc, uint32_t reg)
{
	return bus_space_read_1(sc->sc_iot, sc->sc_ioregh, reg);
}

static inline uint8_t
chipsfb_read_indexed(struct chipsfb_softc *sc, uint32_t reg, uint8_t index)
{

	chipsfb_write_vga(sc, reg & 0xfffe, index);
	return chipsfb_read_vga(sc, reg | 0x0001);
}

static inline void
chipsfb_write_indexed(struct chipsfb_softc *sc, uint32_t reg, uint8_t index,
    uint8_t val)
{

	chipsfb_write_vga(sc, reg & 0xfffe, index);
	chipsfb_write_vga(sc, reg | 0x0001, val);
}

static void
chipsfb_wait_idle(struct chipsfb_softc *sc)
{

#ifdef CHIPSFB_DEBUG
	chipsfb_write32(sc, CT_OFF_FB + (800 * 598) - 4, 0);
#endif

	/* spin until the blitter is idle */
	while ((chipsfb_read32(sc, CT_BLT_CONTROL) & BLT_IS_BUSY) != 0) {
	}

#ifdef CHIPSFB_DEBUG
	chipsfb_write32(sc, CT_OFF_FB + (800 * 598) - 4, 0xffffffff);
#endif
}

void
chipsfb_do_attach(struct chipsfb_softc *sc)
{
	struct wsemuldisplaydev_attach_args aa;
	struct rasops_info *ri;
	prop_dictionary_t dict;
	ulong defattr;
	bool console = false;
	int width, height, i, j;
	uint32_t bg, fg, ul;
	uint8_t cmap[768];

	dict = device_properties(sc->sc_dev);
	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
	sc->sc_dacw = -1;

#ifdef CHIPSFB_DEBUG
	printf(prop_dictionary_externalize(dict));
#endif
	chipsfb_init(sc);

	width = height = -1;

	/* detect panel size */
	width = chipsfb_read_indexed(sc, CT_FP_INDEX, FP_HSIZE_LSB);
	width |= (chipsfb_read_indexed(sc, CT_FP_INDEX, FP_HORZ_OVERFLOW_1)
	    & 0x0f) << 8;
	width = (width + 1) * 8;
	height = chipsfb_read_indexed(sc, CT_FP_INDEX, FP_VSIZE_LSB);
	height |= (chipsfb_read_indexed(sc, CT_FP_INDEX, FP_VERT_OVERFLOW_1)
	    & 0x0f) << 8;
	height++;
	if ((width < 640) || ( width > 1280) || (height < 480) ||
	    (height > 1024)) {
		/* no sane values in the panel registers */
		width = height = -1;
	} else
		aprint_verbose("Panel size: %d x %d\n", width, height);

	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width))
		sc->sc_width = width;
	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height))
		sc->sc_height = height;
	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_bits_per_pixel))
		sc->sc_bits_per_pixel = 8;
	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_linebytes))
		sc->sc_linebytes = (sc->sc_width * sc->sc_bits_per_pixel) >> 3;

	prop_dictionary_get_bool(dict, "is_console", &console);

#ifdef notyet
	/* XXX this should at least be configurable via kernel config */
	chipsfb_set_videomode(sc, &videomode_list[16]);
#endif

	vcons_init(&sc->vd, sc, &chipsfb_defaultscreen, &chipsfb_accessops);
	sc->vd.init_screen = chipsfb_init_screen;

	sc->sc_gc.gc_bitblt = chipsfb_bitblt;
	sc->sc_gc.gc_blitcookie = sc;
	sc->sc_gc.gc_rop = ROP_COPY;

	ri = &chipsfb_console_screen.scr_ri;
	if (console) {
		vcons_init_screen(&sc->vd, &chipsfb_console_screen, 1,
		    &defattr);
		chipsfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;

		chipsfb_defaultscreen.textops = &ri->ri_ops;
		chipsfb_defaultscreen.capabilities = ri->ri_caps;
		chipsfb_defaultscreen.nrows = ri->ri_rows;
		chipsfb_defaultscreen.ncols = ri->ri_cols;
		glyphcache_init(&sc->sc_gc, sc->sc_height + 1,
				(sc->sc_fbsize / sc->sc_linebytes) - sc->sc_height - 1,
				sc->sc_width,
				ri->ri_font->fontwidth,
				ri->ri_font->fontheight,
				defattr);
		wsdisplay_cnattach(&chipsfb_defaultscreen, ri, 0, 0, defattr);
	} else {
		if (chipsfb_console_screen.scr_ri.ri_rows == 0) {
			/* do some minimal setup to avoid weirdnesses later */
			vcons_init_screen(&sc->vd, &chipsfb_console_screen, 1,
			    &defattr);
		} else
			(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);

		glyphcache_init(&sc->sc_gc, sc->sc_height + 1,
				(sc->sc_fbsize / sc->sc_linebytes) - sc->sc_height - 1,
				sc->sc_width,
				ri->ri_font->fontwidth,
				ri->ri_font->fontheight,
				defattr);
	}

	rasops_unpack_attr(defattr, &fg, &bg, &ul);
	sc->sc_bg = ri->ri_devcmap[bg];
	chipsfb_clearscreen(sc);

	if (console)
		vcons_replay_msgbuf(&chipsfb_console_screen);

	aprint_normal_dev(sc->sc_dev, "%d MB aperture, %d MB VRAM at 0x%08x\n",
	    (u_int)(sc->sc_fbsize >> 20),
	    (int)sc->memsize >> 20, (u_int)sc->sc_fb);
#ifdef CHIPSFB_DEBUG
	aprint_debug("fb: %08lx\n", (ulong)ri->ri_bits);
#endif

	j = 0;
	rasops_get_cmap(ri, cmap, sizeof(cmap));
	for (i = 0; i < 256; i++) {
		chipsfb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
		j += 3;
	}
	
	aa.console = console;
	aa.scrdata = &chipsfb_screenlist;
	aa.accessops = &chipsfb_accessops;
	aa.accesscookie = &sc->vd;

	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint, CFARGS_NONE);
}

static int
chipsfb_putpalreg(struct chipsfb_softc *sc, uint8_t index, uint8_t r,
    uint8_t g, uint8_t b)
{

	sc->sc_cmap_red[index] = r;
	sc->sc_cmap_green[index] = g;
	sc->sc_cmap_blue[index] = b;

	chipsfb_write_vga(sc, CT_DACMASK, 0xff);
	chipsfb_write_vga(sc, CT_WRITEINDEX, index);
	chipsfb_write_vga(sc, CT_DACDATA, r);
	chipsfb_write_vga(sc, CT_DACDATA, g);
	chipsfb_write_vga(sc, CT_DACDATA, b);

	return 0;
}

static int
chipsfb_putcmap(struct chipsfb_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];

#ifdef CHIPSFB_DEBUG
	aprint_debug("putcmap: %d %d\n",index, count);
#endif
	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++) {
		chipsfb_putpalreg(sc, index, *r, *g, *b);
		index++;
		r++, g++, b++;
	}
	return 0;
}

static int
chipsfb_getcmap(struct chipsfb_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 void
chipsfb_clearscreen(struct chipsfb_softc *sc)
{
	chipsfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, sc->sc_bg);
}

/*
 * wsdisplay_emulops
 */

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

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

	if (sc->sc_mode == 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) {
			chipsfb_bitblt(sc, x, y, x, y, wi, he, ROP_NOT_DST);
			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;
			chipsfb_bitblt(sc, x, y, x, y, wi, he, ROP_NOT_DST);
			ri->ri_flg |= RI_CURSOR;
		}
	} else {
		ri->ri_flg &= ~RI_CURSOR;
		ri->ri_crow = row;
		ri->ri_ccol = col;
	}
}

#if 0
int
chipsfb_mapchar(void *cookie, int uni, u_int *index)
{
	return 0;
}
#endif

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

	if (sc->sc_mode == 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;
		chipsfb_bitblt(sc, xs, y, xd, y, width, height, ROP_COPY);
	}
}

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

	if (sc->sc_mode == 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);

		chipsfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
	}
}

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

	if (sc->sc_mode == 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;
		chipsfb_bitblt(sc, x, ys, x, yd, width, height, ROP_COPY);
	}
}

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

	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
		if ((row == 0) && (nrows == ri->ri_rows)) {
			/* clear the whole screen */
			chipsfb_rectfill(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;
			chipsfb_rectfill(sc, x, y, width, height,
			    ri->ri_devcmap[bg]);
		}
	}
}

static void
chipsfb_bitblt(void *cookie, int xs, int ys, int xd, int yd,
    int width, int height, int rop)
{
	struct chipsfb_softc *sc = cookie;
	uint32_t src, dst, cmd = rop, stride, size;

	cmd |= BLT_PAT_IS_SOLID;

	/* we assume 8 bit for now */
	src = xs + ys * sc->sc_linebytes;
	dst = xd + yd * sc->sc_linebytes;

	if (xs < xd) {
		/* right-to-left operation */
		cmd |= BLT_START_RIGHT;
		src += width - 1;
		dst += width - 1;
	}

	if (ys < yd) {
		/* bottom-to-top operation */
		cmd |= BLT_START_BOTTOM;
		src += (height - 1) * sc->sc_linebytes;
		dst += (height - 1) * sc->sc_linebytes;
	}

	stride = (sc->sc_linebytes << 16) | sc->sc_linebytes;
	size = (height << 16) | width;

	chipsfb_wait_idle(sc);
	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
	chipsfb_write32(sc, CT_BLT_SRCADDR, src);
	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
	chipsfb_write32(sc, CT_BLT_SIZE, size);
#ifdef CHIPSFB_WAIT
	chipsfb_wait_idle(sc);
#endif
}

static void
chipsfb_rectfill(struct chipsfb_softc *sc, int x, int y, int width,
    int height, int colour)
{
	uint32_t dst, cmd, stride, size;

	cmd = BLT_PAT_IS_SOLID | BLT_PAT_IS_MONO | ROP_PAT;

	/* we assume 8 bit for now */
	dst = x + y * sc->sc_linebytes;

	stride = (sc->sc_linebytes << 16) | sc->sc_linebytes;
	size = (height << 16) | width;

	chipsfb_wait_idle(sc);
	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
	chipsfb_write32(sc, CT_BLT_SRCADDR, dst);
	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
	chipsfb_write32(sc, CT_BLT_BG, colour);
	chipsfb_write32(sc, CT_BLT_FG, colour);
	chipsfb_write32(sc, CT_BLT_SIZE, size);
#ifdef CHIPSFB_WAIT
	chipsfb_wait_idle(sc);
#endif
}

static void
chipsfb_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 chipsfb_softc *sc = scr->scr_cookie;
	uint32_t bg, latch = 0, bg8, fg8, pixel, dst, stride, size;
	int i, l, x, y, wi, he, r, g, b, aval;
	int r1, g1, b1, r0, g0, b0, fgo, bgo, off, pad;
	uint8_t *data8;
	int rv;

	if (__predict_false((unsigned int)row > ri->ri_rows ||
	    (unsigned int)col > ri->ri_cols))
		return;

	if (__predict_false((sc->sc_mode != WSDISPLAYIO_MODE_EMUL)))
		return;
	
	if (__predict_false((!CHAR_IN_FONT(c, font))))
		return;

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

	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
	x = ri->ri_xorigin + col * wi;
	y = ri->ri_yorigin + row * he;

	if (c == 0x20) {
		chipsfb_rectfill(sc, x, y, wi, he, bg);
		return;
	}

	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
	if (rv == GC_OK)
		return;

	data8 = WSFONT_GLYPH(c, font);

	/* we assume 8 bit for now */
	dst = x + y * sc->sc_linebytes;

	stride = sc->sc_linebytes << 16;
	size = (he << 16) | wi;

	/* set up for host blit */
	chipsfb_wait_idle(sc);
	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
	chipsfb_write32(sc, CT_BLT_SRCADDR, 0);
	chipsfb_write32(sc, CT_BLT_CONTROL,
	    BLT_PAT_IS_SOLID | BLT_SRC_IS_CPU | ROP_COPY);
	chipsfb_write32(sc, CT_BLT_SIZE, size);

	/*
	 * we need the RGB colours here, so get offsets into rasops_cmap
	 */
	fgo = ((attr >> 24) & 0xf) * 3;
	bgo = ((attr >> 16) & 0xf) * 3;

	r0 = rasops_cmap[bgo];
	r1 = rasops_cmap[fgo];
	g0 = rasops_cmap[bgo + 1];
	g1 = rasops_cmap[fgo + 1];
	b0 = rasops_cmap[bgo + 2];
	b1 = rasops_cmap[fgo + 2];
#define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
	bg8 = R3G3B2(r0, g0, b0);
	fg8 = R3G3B2(r1, g1, b1);

	/* see if we need to pad lines to 64bit */
	pad = (wi + 3) & 4;
	for (l = 0; l < he; l++) {
		off = 0;
		latch = 0;
		for (i = 0; i < wi; i++) {
			aval = *data8;
			if (aval == 0) {
				pixel = bg8;
			} else if (aval == 255) {
				pixel = fg8;
			} else {
				r = aval * r1 + (255 - aval) * r0;
				g = aval * g1 + (255 - aval) * g0;
				b = aval * b1 + (255 - aval) * b0;
				pixel = ((r & 0xe000) >> 8) |
					((g & 0xe000) >> 11) |
					((b & 0xc000) >> 14);
			}
			latch |= pixel << off;
			off += 8;
			/* write in 32bit chunks */
			if ((i & 3) == 3) {
				chipsfb_write32(sc,
				    CT_OFF_DATA - CT_OFF_BITBLT, latch);
				latch = 0;
				off = 0;
			}
			data8++;
		}
		/* if we have pixels left in latch write them out */
		if ((i & 3) != 0) {
			chipsfb_write32(sc, CT_OFF_DATA - CT_OFF_BITBLT, latch);
		}
		/* this chip needs scanlines 64bit aligned */
		if (pad) chipsfb_write32(sc, CT_OFF_DATA - CT_OFF_BITBLT, 0);
	}

	if (rv == GC_ADD) {
		glyphcache_add(&sc->sc_gc, c, x, y);
	}
}

static void
chipsfb_putchar(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 chipsfb_softc *sc = scr->scr_cookie;

	if (__predict_false((unsigned int)row > ri->ri_rows ||
	    (unsigned int)col > ri->ri_cols))
		return;

	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
		uint8_t *data;
		int fg, bg, uc;
		int x, y, wi, he;

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

		if (!CHAR_IN_FONT(c, font))
			return;
		bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xf];
		fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xf];
		x = ri->ri_xorigin + col * wi;
		y = ri->ri_yorigin + row * he;
		if (c == 0x20) {
			chipsfb_rectfill(sc, x, y, wi, he, bg);
		} else {
			uc = c - font->firstchar;
			data = (uint8_t *)font->data + uc *
			    ri->ri_fontscale;
			chipsfb_setup_mono(sc, x, y, wi, he, fg, bg);
			chipsfb_feed(sc, font->stride * he, data);
		}
	}
}

static void
chipsfb_setup_mono(struct chipsfb_softc *sc, int xd, int yd, int width,
    int height, uint32_t fg, uint32_t bg)
{
	uint32_t dst, cmd, stride, size;

	cmd = BLT_PAT_IS_SOLID | BLT_SRC_IS_CPU | BLT_SRC_IS_MONO | ROP_COPY;

	/* we assume 8 bit for now */
	dst = xd + yd * sc->sc_linebytes;

	stride = (sc->sc_linebytes << 16);
	size = (height << 16) | width;

	chipsfb_wait_idle(sc);
	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
	chipsfb_write32(sc, CT_BLT_SRCADDR, 0);
	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
	chipsfb_write32(sc, CT_BLT_BG, bg);
	chipsfb_write32(sc, CT_BLT_FG, fg);
	chipsfb_write32(sc, CT_BLT_SIZE, size);
}

static void
chipsfb_feed(struct chipsfb_softc *sc, int count, uint8_t *data)
{
	int i;
	uint32_t latch = 0, bork;
	int shift = 0;

	for (i = 0; i < count; i++) {
		bork = data[i];
		latch |= (bork << shift);
		if (shift == 24) {
			chipsfb_write32(sc, CT_OFF_DATA - CT_OFF_BITBLT, latch);
			latch = 0;
			shift = 0;
		} else
			shift += 8;
	}

	if (shift != 0) {
		chipsfb_write32(sc, CT_OFF_DATA - CT_OFF_BITBLT, latch);
	}

	/* apparently the chip wants 64bit-aligned data or it won't go idle */
	if ((count + 3) & 0x04) {
		chipsfb_write32(sc, CT_OFF_DATA - CT_OFF_BITBLT, 0);
	}
#ifdef CHIPSFB_WAIT
	chipsfb_wait_idle(sc);
#endif
}

#if 0
static void
chipsfb_showpal(struct chipsfb_softc *sc)
{
	int i, x = 0;

	for (i = 0; i < 16; i++) {
		chipsfb_rectfill(sc, x, 0, 64, 64, i);
		x += 64;
	}
}
#endif

#if 0
static int
chipsfb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
{

	return 0;
}
#endif

static void
chipsfb_restore_palette(struct chipsfb_softc *sc)
{
	int i;

	for (i = 0; i < 256; i++) {
		chipsfb_putpalreg(sc,
		   i,
		   sc->sc_cmap_red[i],
		   sc->sc_cmap_green[i],
		   sc->sc_cmap_blue[i]);
	}
}

/*
 * wsdisplay_accessops
 */

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

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

	case WSDISPLAYIO_GINFO:
		wdf = (void *)data;
		wdf->height = ms->scr_ri.ri_height;
		wdf->width = ms->scr_ri.ri_width;
		wdf->depth = ms->scr_ri.ri_depth;
		wdf->cmsize = 256;
		return 0;

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

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


	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) {
				chipsfb_init(sc);
				chipsfb_restore_palette(sc);
				glyphcache_wipe(&sc->sc_gc);
				vcons_redraw_screen(ms);
			}
		}
		}
		return 0;
	
	case WSDISPLAYIO_GET_FBINFO: {
		struct wsdisplayio_fbinfo *fbi = data;
		return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
	}

	default:
		if (sc->sc_ioctl != NULL)
			return sc->sc_ioctl(v, vs, cmd, data, flag, l);
	}
	return EPASSTHROUGH;
}

static paddr_t
chipsfb_mmap(void *v, void *vs, off_t offset, int prot)
{
	struct vcons_data *vd = v;
	struct chipsfb_softc *sc = vd->cookie;
	paddr_t pa;

	if (sc->sc_mmap != NULL) {
		pa = sc->sc_mmap(v, vs, offset, prot);
		if (pa != -1) return pa;
	}

	/* 'regular' framebuffer mmap()ing */
	if (offset < sc->memsize) {
		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb, offset, prot,
		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
		return pa;
	}

	/*
	 * restrict all other mappings to processes with superuser privileges
	 * or the kernel itself
	 */
	if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
	    NULL, NULL, NULL, NULL) != 0) {
		aprint_normal_dev(sc->sc_dev, "mmap() rejected.\n");
		return -1;
	}

	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
		return pa;
	}

#ifdef PCI_MAGIC_IO_RANGE
	/* allow mapping of IO space */
	if ((offset >= PCI_MAGIC_IO_RANGE) &&
	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
		    0, prot, BUS_SPACE_MAP_LINEAR);
		return pa;
	}
#endif

	return -1;
}

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

	ri->ri_depth = sc->sc_bits_per_pixel;
	ri->ri_width = sc->sc_width;
	ri->ri_height = sc->sc_height;
	ri->ri_stride = sc->sc_width;
	ri->ri_flg = RI_CENTER | RI_FULLCLEAR |
		     RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;

	ri->ri_bits = bus_space_vaddr(sc->sc_memt, sc->sc_fbh);

#ifdef CHIPSFB_DEBUG
	aprint_debug("addr: %08lx\n", (ulong)ri->ri_bits);
#endif
	if (existing) {
		ri->ri_flg |= RI_CLEAR;
	}

	rasops_init(ri, 0, 0);
	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;
	ri->ri_ops.copyrows = chipsfb_copyrows;
	ri->ri_ops.copycols = chipsfb_copycols;
	ri->ri_ops.eraserows = chipsfb_eraserows;
	ri->ri_ops.erasecols = chipsfb_erasecols;
	ri->ri_ops.cursor = chipsfb_cursor;
	if (FONT_IS_ALPHA(ri->ri_font)) {
		ri->ri_ops.putchar = chipsfb_putchar_aa;
	} else
		ri->ri_ops.putchar = chipsfb_putchar;
}

#if 0
int
chipsfb_load_font(void *v, void *cookie, struct wsdisplay_font *data)
{

	return 0;
}
#endif

static void
chipsfb_init(struct chipsfb_softc *sc)
{
	uint8_t reg;

	chipsfb_wait_idle(sc);

	/* setup the blitter */
	chipsfb_write32(sc, CT_BLT_EXPCTL, MONO_SRC_ALIGN_BYTE);

	/* put DAC into 8bit mode */
	reg = chipsfb_read_indexed(sc, CT_CONF_INDEX, XR_PIXEL_PIPELINE_CTL_0);
	reg |= ENABLE_8BIT_DAC;
	chipsfb_write_indexed(sc, CT_CONF_INDEX, XR_PIXEL_PIPELINE_CTL_0, reg);
}

uint32_t
chipsfb_probe_vram(struct chipsfb_softc *sc)
{
	uint32_t ofs = 0x00080000;	/* 512kB */

	/*
	 * advance in 0.5MB steps, see if we can read back what we wrote and
	 * if what we wrote to 0 is left untouched. Max. fb size is 4MB so
	 * we voluntarily stop there.
	 */
	bus_space_write_4(sc->sc_memt, sc->sc_fbh, 0, 0xf0f0f0f0);
	bus_space_write_4(sc->sc_memt, sc->sc_fbh, ofs, 0x0f0f0f0f);
	while ((bus_space_read_4(sc->sc_memt, sc->sc_fbh, 0) == 0xf0f0f0f0) &&
	    (bus_space_read_4(sc->sc_memt, sc->sc_fbh, ofs) == 0x0f0f0f0f) &&
	    (ofs < 0x00400000)) {

		ofs += 0x00080000;
		bus_space_write_4(sc->sc_memt, sc->sc_fbh, ofs, 0x0f0f0f0f);
	}

	return ofs;
}