clgd542x.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include "pci.h"
  5. #include "vga.h"
  6. /*
  7. * Cirrus Logic True Color VGA Family - CL-GD542X.
  8. * Also works for Alpine VGA Family - CL-GD543X.
  9. * Just the basics. BUGS:
  10. * the added capabilities of the 543X aren't used.
  11. */
  12. typedef struct {
  13. uchar id; /* Id */
  14. ulong vclk; /* Maximum dot clock */
  15. } Gd542x;
  16. static Gd542x family[] = {
  17. { 0x88, 75000000, }, /* CL-GD5420 */
  18. { 0x8C, 80000000, }, /* CL-GD5422 */
  19. { 0x94, 80000000, }, /* CL-GD5424 */
  20. { 0x90, 80000000, }, /* CL-GD5426 */
  21. { 0x98, 80000000, }, /* CL-GD5428 */
  22. { 0x9C, 86000000, }, /* CL-GD5429 */
  23. { 0xA0, 86000000, }, /* CL-GD5430 */
  24. { 0xA8, 86000000, }, /* CL-GD5434 */
  25. { 0xAC, 135000000, }, /* CL-GD5436 */
  26. { 0xB8, 135000000, }, /* CL-GD5446 */
  27. { 0x30, 80000000, }, /* CL-GD7543 */
  28. { 0x00, },
  29. };
  30. static Gd542x*
  31. identify(Vga* vga, Ctlr* ctlr)
  32. {
  33. Gd542x *gd542x;
  34. uchar id;
  35. id = vga->crt[0x27] & ~0x03;
  36. for(gd542x = &family[0]; gd542x->id; gd542x++){
  37. if(gd542x->id == id)
  38. return gd542x;
  39. }
  40. error("%s: unknown chip id - 0x%2.2X\n", ctlr->name, vga->crt[0x27]);
  41. return 0;
  42. }
  43. static void
  44. snarf(Vga* vga, Ctlr* ctlr)
  45. {
  46. int i;
  47. Gd542x *gd542x;
  48. /*
  49. * Unlock extended registers.
  50. */
  51. vgaxo(Seqx, 0x06, 0x12);
  52. /*
  53. * Save all the registers, even though we'll only
  54. * change a handful.
  55. */
  56. for(i = 0x06; i < 0x20; i++)
  57. vga->sequencer[i] = vgaxi(Seqx, i);
  58. for(i = 0x09; i < 0x3A; i++)
  59. vga->graphics[i] = vgaxi(Grx, i);
  60. for(i = 0x19; i < 0x1E; i++)
  61. vga->crt[i] = vgaxi(Crtx, i);
  62. vga->crt[0x27] = vgaxi(Crtx, 0x27);
  63. /*
  64. * Hack for Hidden DAC Register. Do 4 dummy reads
  65. * of Pixmask first.
  66. */
  67. for(i = 0; i < 4; i++)
  68. vgai(Pixmask);
  69. vga->crt[0x28] = vgai(Pixmask);
  70. i = 0;
  71. switch(vga->crt[0x27] & ~0x03){
  72. case 0x88: /* CL-GD5420 */
  73. case 0x8C: /* CL-GD5422 */
  74. case 0x94: /* CL-GD5424 */
  75. case 0x80: /* CL-GD5425 */
  76. case 0x90: /* CL-GD5426 */
  77. case 0x98: /* CL-GD5427 */
  78. case 0x9C: /* CL-GD5429 */
  79. /*
  80. * The BIOS leaves the memory size in Seq0A, bits 4 and 3.
  81. * See Technical Reference Manual Appendix E1, Section 1.3.2.
  82. *
  83. * The storage area for the 64x64 cursors is the last 16Kb of
  84. * display memory.
  85. */
  86. i = (vga->sequencer[0x0A]>>3) & 0x03;
  87. break;
  88. case 0xA0: /* CL-GD5430 */
  89. case 0xA8: /* CL-GD5434 */
  90. case 0xAC: /* CL-GD5436 */
  91. case 0xB8: /* CL-GD5446 */
  92. case 0x30: /* CL-GD7543 */
  93. /*
  94. * Attempt to intuit the memory size from the DRAM control
  95. * register. Minimum is 512KB.
  96. * If DRAM bank switching is on then there's double.
  97. */
  98. i = (vga->sequencer[0x0F]>>3) & 0x03;
  99. if(vga->sequencer[0x0F] & 0x80)
  100. i++;
  101. /*
  102. * If it's a PCI card, can do linear.
  103. * Most of the Cirrus chips can do linear addressing with
  104. * all the different buses, but it can get messy. It's easy
  105. * to cut PCI on the CLGD543x chips out of the pack.
  106. */
  107. if(((vga->sequencer[0x17]>>3) & 0x07) == 0x04)
  108. ctlr->flag |= Hlinear;
  109. break;
  110. default: /* uh, ah dunno */
  111. break;
  112. }
  113. if(vga->linear && (ctlr->flag & Hlinear)){
  114. vga->vmz = 16*1024*1024;
  115. vga->vma = 16*1024*1024;
  116. ctlr->flag |= Ulinear;
  117. }
  118. else
  119. vga->vmz = (256<<i)*1024;
  120. gd542x = identify(vga, ctlr);
  121. if(vga->f[1] == 0 || vga->f[1] > gd542x->vclk)
  122. vga->f[1] = gd542x->vclk;
  123. ctlr->flag |= Fsnarf;
  124. }
  125. void
  126. clgd54xxclock(Vga* vga, Ctlr* ctlr)
  127. {
  128. int f;
  129. ulong d, dmin, fmin, n, nmin, p;
  130. trace("%s->init->clgd54xxclock\n", ctlr->name);
  131. /*
  132. * Athough the Technical Reference Manual says only a handful
  133. * of frequencies are tested, it also gives the following formula
  134. * which can be used to generate any frequency within spec.,
  135. * including the tested ones.
  136. *
  137. * Numerator is 7-bits, denominator 5-bits.
  138. * Guess from the Technical Reference Manual that
  139. * The post divisor is 1 for vclk<40MHz.
  140. *
  141. * Look for values of n and d and p that give
  142. * the least error for
  143. * vclk = (RefFreq*n)/(d*(1+p));
  144. *
  145. * There's nothing like brute force and ignorance.
  146. */
  147. fmin = vga->f[0];
  148. nmin = 69;
  149. dmin = 24;
  150. if(vga->f[0] >= 40000000)
  151. p = 0;
  152. else
  153. p = 1;
  154. for(n = 1; n < 128; n++){
  155. for(d = 1; d < 32; d++){
  156. f = vga->f[0] - (RefFreq*n)/(d*(1+p));
  157. if(f < 0)
  158. f = -f;
  159. if(f <= fmin){
  160. fmin = f;
  161. nmin = n;
  162. dmin = d;
  163. }
  164. }
  165. }
  166. vga->f[0] = (RefFreq*nmin)/(dmin*(1+p));
  167. vga->d[0] = dmin;
  168. vga->n[0] = nmin;
  169. vga->p[0] = p;
  170. }
  171. void
  172. init(Vga* vga, Ctlr* ctlr)
  173. {
  174. Mode *mode;
  175. Gd542x *gd542x;
  176. ushort x;
  177. mode = vga->mode;
  178. gd542x = identify(vga, ctlr);
  179. if(vga->f[0] == 0)
  180. vga->f[0] = vga->mode->frequency;
  181. if(vga->f[0] > gd542x->vclk)
  182. error("%s: pclk %lud too high (> %lud)\n",
  183. ctlr->name, vga->f[0], gd542x->vclk);
  184. if(mode->z > 8)
  185. error("%s: depth %d not supported\n", ctlr->name, mode->z);
  186. /*
  187. * VCLK3
  188. */
  189. clgd54xxclock(vga, ctlr);
  190. vga->misc |= 0x0C;
  191. vga->sequencer[0x0E] = vga->n[0];
  192. vga->sequencer[0x1E] = (vga->d[0]<<1)|vga->p[0];
  193. vga->sequencer[0x07] = 0x00;
  194. if(mode->z == 8)
  195. vga->sequencer[0x07] |= 0x01;
  196. if(vga->f[0] >= 42000000)
  197. vga->sequencer[0x0F] |= 0x20;
  198. else
  199. vga->sequencer[0x0F] &= ~0x20;
  200. vga->sequencer[0x16] = (vga->sequencer[0x16] & 0xF0)|0x08;
  201. /*
  202. * Overflow bits.
  203. */
  204. vga->crt[0x1A] = 0x00;
  205. x = mode->ehb>>3;
  206. if(x & 0x40)
  207. vga->crt[0x1A] |= 0x10;
  208. if(x & 0x80)
  209. vga->crt[0x1A] |= 0x20;
  210. if(vga->crt[0x16] & 0x100)
  211. vga->crt[0x1A] |= 0x40;
  212. if(vga->crt[0x16] & 0x200)
  213. vga->crt[0x1A] |= 0x80;
  214. vga->crt[0x1B] = 0x22;
  215. if(vga->crt[0x13] & 0x100)
  216. vga->crt[0x1B] |= 0x10;
  217. vga->graphics[0x0B] = 0x00;
  218. if(vga->vmz > 1024*1024)
  219. vga->graphics[0x0B] |= 0x20;
  220. if(mode->interlace == 'v'){
  221. vga->crt[0x19] = vga->crt[0x00]/2;
  222. vga->crt[0x1A] |= 0x01;
  223. }
  224. }
  225. static void
  226. load(Vga* vga, Ctlr* ctlr)
  227. {
  228. vgaxo(Seqx, 0x0E, vga->sequencer[0x0E]);
  229. vgaxo(Seqx, 0x1E, vga->sequencer[0x1E]);
  230. if(ctlr->flag & Ulinear)
  231. vga->sequencer[0x07] |= 0xE0;
  232. vgaxo(Seqx, 0x07, vga->sequencer[0x07]);
  233. vgaxo(Seqx, 0x0F, vga->sequencer[0x0F]);
  234. vgaxo(Seqx, 0x16, vga->sequencer[0x16]);
  235. if(vga->mode->interlace == 'v')
  236. vgaxo(Crtx, 0x19, vga->crt[0x19]);
  237. vgaxo(Crtx, 0x1A, vga->crt[0x1A]);
  238. vgaxo(Crtx, 0x1B, vga->crt[0x1B]);
  239. vgaxo(Grx, 0x0B, vga->graphics[0x0B]);
  240. }
  241. static void
  242. dump(Vga* vga, Ctlr* ctlr)
  243. {
  244. int i;
  245. char *name;
  246. name = ctlr->name;
  247. printitem(name, "Seq06");
  248. for(i = 0x06; i < 0x20; i++)
  249. printreg(vga->sequencer[i]);
  250. printitem(name, "Crt19");
  251. for(i = 0x19; i < 0x1E; i++)
  252. printreg(vga->crt[i]);
  253. printitem(name, "Gr09");
  254. for(i = 0x09; i < 0x3A; i++)
  255. printreg(vga->graphics[i]);
  256. printitem(name, "Id Hdr");
  257. printreg(vga->crt[0x27]);
  258. printreg(vga->crt[0x28]);
  259. }
  260. Ctlr clgd542x = {
  261. "clgd542x", /* name */
  262. snarf, /* snarf */
  263. 0, /* options */
  264. init, /* init */
  265. load, /* load */
  266. dump, /* dump */
  267. };
  268. Ctlr clgd542xhwgc = {
  269. "clgd542xhwgc", /* name */
  270. 0, /* snarf */
  271. 0, /* options */
  272. 0, /* init */
  273. 0, /* load */
  274. 0, /* dump */
  275. };