]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/pc/vgamga2164w.c
devdraw: get rid of softscreen==0xa110c hack and make attachscreen() return Memdata*
[plan9front.git] / sys / src / 9 / pc / vgamga2164w.c
1 #include "u.h"
2 #include "../port/lib.h"
3 #include "mem.h"
4 #include "dat.h"
5 #include "fns.h"
6 #include "io.h"
7 #include "../port/error.h"
8
9 #define Image   IMAGE
10 #include <draw.h>
11 #include <memdraw.h>
12 #include <cursor.h>
13 #include "screen.h"
14
15 /*
16  * Matrox Millennium and Matrox Millennium II.
17  * Matrox MGA-2064W, MGA-2164W 3D graphics accelerators.
18  * Texas Instruments Tvp3026 RAMDAC.
19  */
20
21 enum {
22         /* pci chip manufacturer */
23         MATROX          = 0x102B,
24
25         /* pci chip device ids */
26         MGA2064         = 0x0519,
27         MGA2164         = 0x051B,
28         MGA2164AGP      = 0x051F
29 };
30
31 static void
32 mga2164wenable(VGAscr* scr)
33 {
34         Pcidev *p;
35
36         if(scr->mmio)
37                 return;
38
39         p = scr->pci;
40         if(p == nil || p->vid != MATROX)
41                 return;
42
43         if(p->did == MGA2064){
44                 scr->mmio = vmap(p->mem[0].bar&~0x0F, p->mem[0].size);
45                 if(scr->mmio == nil)
46                         return;
47                 addvgaseg("mga2164wmmio", p->mem[0].bar&~0x0F, p->mem[0].size);
48                 vgalinearaddr(scr, p->mem[1].bar&~0x0F, 8*MB);
49         }else{
50                 scr->mmio = vmap(p->mem[1].bar&~0x0F, p->mem[1].size);
51                 if(scr->mmio == nil)
52                         return;
53                 addvgaseg("mga2164wmmio", p->mem[1].bar&~0x0F, p->mem[1].size);
54                 vgalinearaddr(scr, p->mem[0].bar&~0x0F, 16*MB);
55         }
56         if(scr->paddr)
57                 addvgaseg("mga2164wscreen", scr->paddr, scr->apsize);
58 }
59
60 enum {
61         Index           = 0x00,         /* Index */
62         Data            = 0x0A,         /* Data */
63
64         CaddrW          = 0x04,         /* Colour Write Address */
65         Cdata           = 0x05,         /* Colour Data */
66
67         Cctl            = 0x09,         /* Direct Cursor Control */
68         Cram            = 0x0B,         /* Cursor Ram Data */
69         Cxlsb           = 0x0C,         /* Cursor X LSB */
70         Cxmsb           = 0x0D,         /* Cursor X MSB */
71         Cylsb           = 0x0E,         /* Cursor Y LSB */
72         Cymsb           = 0x0F,         /* Cursor Y MSB */
73
74         Icctl           = 0x06,         /* Indirect Cursor Control */
75 };
76
77 static void
78 tvp3026disable(VGAscr* scr)
79 {
80         uchar *tvp3026;
81
82         if(scr->mmio == 0)
83                 return;
84         tvp3026 = (uchar*)scr->mmio+0x3C00;
85
86         /*
87          * Make sure cursor is off
88          * and direct control enabled.
89          */
90         *(tvp3026+Index) = Icctl;
91         *(tvp3026+Data) = 0x90;
92         *(tvp3026+Cctl) = 0x00;
93 }
94
95 static void
96 tvp3026load(VGAscr* scr, Cursor* curs)
97 {
98         int x, y;
99         uchar *tvp3026;
100
101         if(scr->mmio == 0)
102                 return;
103         tvp3026 = (uchar*)scr->mmio+0x3C00;
104
105         /*
106          * Make sure cursor is off by initialising the cursor
107          * control to defaults.
108          * Write to the indirect control register to make sure
109          * direct register is enabled and upper 2 bits of cursor
110          * RAM address are 0.
111          * Put 0 in index register for lower 8 bits of cursor RAM address.
112          */
113         tvp3026disable(scr);
114         *(tvp3026+Index) = 0;
115
116         /*
117          * Initialise the 64x64 cursor RAM array. There are 2 planes,
118          * p0 and p1. Data is written 8 pixels per byte, with p0 in the
119          * first 512 bytes of the array and p1 in the second.
120          * The cursor is set in 3-colour mode which gives the following
121          * truth table:
122          *      p1 p0   colour
123          *       0  0   transparent
124          *       0  1   cursor colour 0
125          *       1  0   cursor colour 1
126          *       1  1   cursor colour 2
127          * Put the cursor into the top-left of the 64x64 array.
128          * The 0,0 cursor point is bottom-right, so positioning will
129          * have to take that into account.
130          */
131         for(y = 0; y < 64; y++){
132                 for(x = 0; x < 64/8; x++){
133                         if(x < 16/8 && y < 16)
134                                 *(tvp3026+Cram) = curs->clr[x+y*2];
135                         else
136                                 *(tvp3026+Cram) = 0x00;
137                 }
138         }
139         for(y = 0; y < 64; y++){
140                 for(x = 0; x < 64/8; x++){
141                         if(x < 16/8 && y < 16)
142                                 *(tvp3026+Cram) = curs->set[x+y*2];
143                         else
144                                 *(tvp3026+Cram) = 0x00;
145                 }
146         }
147
148         /*
149          * Initialise the cursor hotpoint
150          * and enable the cursor in 3-colour mode.
151          */
152         scr->offset.x = 64+curs->offset.x;
153         scr->offset.y = 64+curs->offset.y;
154         *(tvp3026+Cctl) = 0x01;
155 }
156
157 static int
158 tvp3026move(VGAscr* scr, Point p)
159 {
160         int x, y;
161         uchar *tvp3026;
162
163         if(scr->mmio == 0)
164                 return 1;
165         tvp3026 = (uchar*)scr->mmio+0x3C00;
166
167         x = p.x+scr->offset.x;
168         y = p.y+scr->offset.y;
169
170         *(tvp3026+Cxlsb) = x & 0xFF;
171         *(tvp3026+Cxmsb) = (x>>8) & 0x0F;
172         *(tvp3026+Cylsb) = y & 0xFF;
173         *(tvp3026+Cymsb) = (y>>8) & 0x0F;
174
175         return 0;
176 }
177
178 static void
179 tvp3026enable(VGAscr* scr)
180 {
181         int i;
182         uchar *tvp3026;
183
184         if(scr->mmio == 0)
185                 return;
186         tvp3026 = (uchar*)scr->mmio+0x3C00;
187
188         tvp3026disable(scr);
189
190         /*
191          * Overscan colour,
192          * cursor colour 1 (white),
193          * cursor colour 2, 3 (black).
194          */
195         *(tvp3026+CaddrW) = 0x00;
196         for(i = 0; i < 6; i++)
197                 *(tvp3026+Cdata) = Pwhite; 
198         for(i = 0; i < 6; i++)
199                 *(tvp3026+Cdata) = Pblack; 
200
201         /*
202          * Load, locate and enable the
203          * 64x64 cursor in 3-colour mode.
204          */
205         tvp3026load(scr, &arrow);
206         tvp3026move(scr, ZP);
207         *(tvp3026+Cctl) = 0x01;
208 }
209
210 VGAdev vgamga2164wdev = {
211         "mga2164w",
212
213         mga2164wenable,                 /* enable */
214         0,                              /* disable */
215         0,                              /* page */
216         0,                              /* linear */
217 };
218
219 VGAcur vgamga2164wcur = {
220         "mga2164whwgc",
221
222         tvp3026enable,
223         tvp3026disable,
224         tvp3026load,
225         tvp3026move,
226 };