vgamga2164w.c 5.5 KB

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