summaryrefslogtreecommitdiff
path: root/sys/src/9/pc/vgacyber938x.c
blob: cd06f77a688c00e11ff95b4312d3571c530cc7cf (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
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"
#include "../port/pci.h"
#include "../port/error.h"

#define	Image	IMAGE
#include <draw.h>
#include <memdraw.h>
#include <cursor.h>
#include "screen.h"

enum {
	CursorON	= 0xC8,
	CursorOFF	= 0x00,
};

static int
cyber938xpageset(VGAscr*, int page)
{
	int opage;

	opage = inb(0x3D8);

	outb(0x3D8, page);
	outb(0x3D9, page);

	return opage;
}

static void
cyber938xpage(VGAscr* scr, int page)
{
	lock(&scr->devlock);
	cyber938xpageset(scr, page);
	unlock(&scr->devlock);
}

static void
cyber938xlinear(VGAscr* scr, int, int)
{
	Pcidev *p;

	if(scr->vaddr)
		return;
	
	p = scr->pci;
	if(p == nil)
		return;

	vgalinearpci(scr);

	/*
	 * Heuristic to detect the MMIO space.  We're flying blind
	 * here, with only the XFree86 source to guide us.
	 */
	if(p->mem[1].size == 0x20000 && (p->mem[1].bar & 1) == 0)
		scr->mmio = vmap(p->mem[1].bar & ~0x0F, p->mem[1].size);

	if(scr->apsize)
		addvgaseg("cyber938xscreen", scr->paddr, scr->apsize);
	if(scr->mmio)
		addvgaseg("cyber938xmmio", p->mem[1].bar&~0x0F, 0x20000);
}

static void
cyber938xcurdisable(VGAscr*)
{
	vgaxo(Crtx, 0x50, CursorOFF);
}

static void
cyber938xcurload(VGAscr* scr, Cursor* curs)
{
	uchar *p;
	int islinear, opage, y;

	cyber938xcurdisable(scr);

	opage = 0;
	p = scr->vaddr;
	islinear = vgaxi(Crtx, 0x21) & 0x20;
	if(!islinear){
		lock(&scr->devlock);
		opage = cyber938xpageset(scr, scr->storage>>16);
		p += (scr->storage & 0xFFFF);
	}
	else
		p += scr->storage;

	for(y = 0; y < 16; y++){
		*p++ = curs->set[2*y]|curs->clr[2*y];
		*p++ = curs->set[2*y + 1]|curs->clr[2*y + 1];
		*p++ = 0x00;
		*p++ = 0x00;
		*p++ = curs->set[2*y];
		*p++ = curs->set[2*y + 1];
		*p++ = 0x00;
		*p++ = 0x00;
	}
	memset(p, 0, (32-y)*8);

	if(!islinear){
		cyber938xpageset(scr, opage);
		unlock(&scr->devlock);
	}

	/*
	 * Save the cursor hotpoint and enable the cursor.
	 */
	scr->offset = curs->offset;
	vgaxo(Crtx, 0x50, CursorON);
}

static int
cyber938xcurmove(VGAscr* scr, Point p)
{
	int x, xo, y, yo;

	/*
	 * Mustn't position the cursor offscreen even partially,
	 * or it might disappear. Therefore, if x or y is -ve, adjust the
	 * cursor origins instead.
	 */
	if((x = p.x+scr->offset.x) < 0){
		xo = -x;
		x = 0;
	}
	else
		xo = 0;
	if((y = p.y+scr->offset.y) < 0){
		yo = -y;
		y = 0;
	}
	else
		yo = 0;

	/*
	 * Load the new values.
	 */
	vgaxo(Crtx, 0x46, xo);
	vgaxo(Crtx, 0x47, yo);
	vgaxo(Crtx, 0x40, x & 0xFF);
	vgaxo(Crtx, 0x41, (x>>8) & 0xFF);
	vgaxo(Crtx, 0x42, y & 0xFF);
	vgaxo(Crtx, 0x43, (y>>8) & 0xFF);

	return 0;
}

static void
cyber938xcurenable(VGAscr* scr)
{
	int i;
	ulong storage;

	cyber938xcurdisable(scr);

	/*
	 * Cursor colours.
	 */
	for(i = 0x48; i < 0x4C; i++)
		vgaxo(Crtx, i, 0x00);
	for(i = 0x4C; i < 0x50; i++)
		vgaxo(Crtx, i, 0xFF);

	/*
	 * Find a place for the cursor data in display memory.
	 */
	storage = ((scr->pitch*scr->height+1023)/1024);
	vgaxo(Crtx, 0x44, storage & 0xFF);
	vgaxo(Crtx, 0x45, (storage>>8) & 0xFF);
	storage *= 1024;
	scr->storage = storage;

	/*
	 * Load, locate and enable the 32x32 cursor.
	 * (64x64 is bit 0, X11 format is bit 6 and cursor
	 * enable is bit 7). Bit 3 needs to be set on 9382
	 * chips otherwise even the white bits are black.
	 */
	cyber938xcurload(scr, &cursor);
	cyber938xcurmove(scr, ZP);
	vgaxo(Crtx, 0x50, CursorON);
}

VGAdev vgacyber938xdev = {
	"cyber938x",

	nil,				/* enable */
	nil,				/* disable */
	cyber938xpage,			/* page */
	cyber938xlinear,		/* linear */
	nil,				/* drawinit */
};

VGAcur vgacyber938xcur = {
	"cyber938xhwgc",

	cyber938xcurenable,		/* enable */
	cyber938xcurdisable,		/* disable */
	cyber938xcurload,		/* load */
	cyber938xcurmove,		/* move */
};