i81x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include "pci.h"
  5. #include "vga.h"
  6. /*
  7. * Intel 81x chipset family.
  8. * mem[0]: AGP aperture memory, 64MB for 810-DC100, from 0xF4000000
  9. * mem[1]: GC Register mmio space, 512KB for 810-DC100, from 0xFF000000
  10. * For the memory of David Hogan, died April 9, 2003, who wrote this driver
  11. * first for LCD.
  12. * August 28, 2003 Kenji Okamoto
  13. */
  14. typedef struct {
  15. Pcidev* pci;
  16. ulong mmio;
  17. ulong clk[6];
  18. ulong lcd[9];
  19. ulong pixconf;
  20. } I81x;
  21. static void
  22. snarf(Vga* vga, Ctlr* ctlr)
  23. {
  24. int f, i;
  25. long m;
  26. ulong *rp;
  27. Pcidev *p;
  28. I81x *i81x;
  29. if(vga->private == nil){
  30. vga->private = alloc(sizeof(I81x));
  31. p = nil;
  32. while((p = pcimatch(p, 0x8086, 0)) != nil) {
  33. switch(p->did) {
  34. default:
  35. continue;
  36. case 0x7121: /* Vanilla 82810 */
  37. case 0x7123: /* 810-DC100, DELL OptiPlex GX100 */
  38. case 0x7125: /* 82810E */
  39. case 0x1102: /* 82815 FSB limited to 100MHz */
  40. case 0x1112: /* 82815 no AGP */
  41. case 0x1132: /* 82815 fully featured Solano */
  42. case 0x3577: /* IBM R31 uses intel 830M chipset */
  43. vga->f[1] = 230000000; /* MAX speed of internal DAC (Hz)*/
  44. break;
  45. }
  46. break;
  47. }
  48. if(p == nil)
  49. error("%s: Intel 81x graphics function not found\n", ctlr->name);
  50. if((f = open("#v/vgactl", OWRITE)) < 0)
  51. error("%s: can't open vgactl\n", ctlr->name);
  52. if(write(f, "type i81x", 9) != 9)
  53. error("%s: can't set type\n", ctlr->name);
  54. close(f);
  55. if((m = segattach(0, "i81xmmio", 0, p->mem[1].size)) == -1)
  56. error("%s: can't attach mmio segment\n", ctlr->name);
  57. i81x = vga->private;
  58. i81x->pci = p;
  59. i81x->mmio = m;
  60. }
  61. i81x = vga->private;
  62. /* must give aperture memory size for frame buffer memory
  63. such as 64*1024*1024 */
  64. vga->vma = vga->vmz = i81x->pci->mem[0].size;
  65. // vga->vmz = 8*1024*1024;
  66. vga->apz = i81x->pci->mem[0].size;
  67. ctlr->flag |= Hlinear;
  68. vga->graphics[0x10] = vgaxi(Grx, 0x10);
  69. vga->attribute[0x11] = vgaxi(Attrx, 0x11); /* overscan color */
  70. for(i=0; i < 0x19; i++)
  71. vga->crt[i] = vgaxi(Crtx, i);
  72. for(i=0x30; i <= 0x82; i++) /* set CRT Controller Register (CR) */
  73. vga->crt[i] = vgaxi(Crtx, i);
  74. /* 0x06000: Clock Control Register base address (3 VCO frequency control) */
  75. rp = (ulong*)(i81x->mmio+0x06000);
  76. for(i = 0; i < nelem(i81x->clk); i++)
  77. i81x->clk[i] = *rp++;
  78. /* i830 CRTC registers (A) */
  79. rp = (ulong*)(i81x->mmio+0x60000);
  80. for(i = 0; i < nelem(i81x->lcd); i++)
  81. i81x->lcd[i] = *rp++;
  82. rp = (ulong*)(i81x->mmio+0x70008); /* Pixel Pipeline Control register A */
  83. i81x->pixconf = *rp;
  84. ctlr->flag |= Fsnarf;
  85. }
  86. static void
  87. options(Vga*, Ctlr* ctlr)
  88. {
  89. ctlr->flag |= Hlinear|Foptions;
  90. }
  91. static void
  92. i81xdclk(I81x *i81x, Vga *vga) /* freq = MHz */
  93. {
  94. int m, n, post, mtp, ntp;
  95. double md, freq, error=1.0;
  96. freq = vga->mode->deffrequency/1000000.0;
  97. post = log(600.0/freq)/log(2.0);
  98. for(ntp=3;;ntp++) {
  99. md = freq*(1<<post)/(24.0/(double)ntp)/4.0;
  100. mtp = (int)(md+0.5);
  101. if(mtp<3) mtp=3;
  102. error = 1.0-freq/(md/(ntp*(1<<post))*4*24.0);
  103. if((fabs(error) < 0.001) || ((ntp > 30) && (fabs(error) < 0.005)))
  104. break;
  105. }
  106. m = vga->m[1] = mtp-2;
  107. n = vga->n[1] = ntp-2;
  108. vga->r[1] = post;
  109. i81x->clk[2] = ((n & 0x3FF)<<16) | (m & 0x3FF);
  110. i81x->clk[4] = (i81x->clk[4] & ~0x700000) | ((post & 0x07)<<20);
  111. vga->mode->frequency = (m+2)/((n+2)*(1<<post))*4*24*1000000;
  112. }
  113. static void
  114. init(Vga* vga, Ctlr* ctlr)
  115. {
  116. I81x *i81x;
  117. int vt, vde, vrs, vre;
  118. ulong *rp;
  119. i81x = vga->private;
  120. /* <<TODO>>
  121. i81x->clk[3]: LCD_CLKD: 0x0600c~0x0600f, default=00030013h
  122. (VCO N-divisor=03h, M-divisor=13h)
  123. i81x->clk[4]: DCLK_0DS: 0x06010~0x06013, Post value, default=40404040h means
  124. Post Divisor=16, VCO Loop divisor = 4xM for all clocks.
  125. Display&LCD Clock Devisor Select Reg = 0x40404040 ==> (LCD)(Clock2)(Clock1)(Clock0)
  126. */
  127. i81x->clk[0] = 0x00030013;
  128. i81x->clk[1] = 0x00100053;
  129. rp = (ulong*)i81x->mmio+0x6010;
  130. i81x->clk[4] = *rp;
  131. i81x->clk[4] |= 0x4040;
  132. vga->misc = vgai(MiscR);
  133. switch(vga->virtx) {
  134. case 640: /* 640x480 DCLK_0D 25.175MHz dot clock */
  135. vga->misc &= ~0x0A;
  136. break;
  137. case 720: /* 720x480 DCLK_1D 28.322MHz dot clock */
  138. vga->misc = (vga->misc & ~0x08) | (1<<2);
  139. break;
  140. case 800:
  141. case 1024:
  142. case 1152:
  143. case 1280:
  144. case 1376:
  145. vga->misc = vga->misc | (2<<2) & ~0x02; /* prohibit to access frame buffer */
  146. i81xdclk(i81x, vga);
  147. break;
  148. default: /* for other higher resolution DCLK_2D */
  149. error("%s: Only 800, 1024, 1152, 1280, 1376 resolutions are supported\n", ctlr->name);
  150. }
  151. /* <<TODO>>
  152. i830 LCD Controller, at i81x->mmio+0x60000
  153. i81x->lcd[0]: Horizontal Total Reg. 0x60000
  154. i81x->lcd[1]: Horizontal Blanking Reg. 0x60004
  155. i81x->lcd[2]: Horizontal Sync Reg. 0x60008
  156. i81x->lcd[3]: Vertical Total Reg. 0x6000c
  157. i81x->lcd[4]: Vertical Blanking Reg. 0x60010
  158. i81x->lcd[5]: Vertical Sync Reg. 0x60014
  159. i81x->lcd[6]: Pixel Pipeline A Sequencer Register Control(SRC,0~7) 0x6001c
  160. i81x->lcd[7]: BCLRPAT_A 0x60020
  161. i81x->lcd[8]: 0
  162. */
  163. /*
  164. * Pixel pipeline control register 0x70008:
  165. * 16/24bp bypasses palette,
  166. * hw cursor enabled(1<<12), hi-res mode(1<<0), depth(16-19 bit)
  167. * 8bit DAC enable (1<<15), don't wrap to 256kM memory of VGA(1<<1).
  168. * enable extended palette addressing (1<<8)
  169. */
  170. i81x->pixconf = (1<<12)|(1<<0);
  171. i81x->pixconf &= 0xFFFFFBFF; /* disable overscan color */
  172. switch(vga->mode->z) { /* vga->mode->z: color depth */
  173. case 8:
  174. i81x->pixconf |= (2<<16);
  175. break;
  176. case 16: /* (5:6:5 bit) */
  177. i81x->pixconf |= (5<<16);
  178. break;
  179. case 24:
  180. i81x->pixconf |= (6<<16);
  181. break;
  182. case 32: /* not supported */
  183. i81x->pixconf |= (7<<16);
  184. break;
  185. default:
  186. error("%s: depth %d not supported\n", ctlr->name, vga->mode->z);
  187. }
  188. /* DON'T CARE of Sequencer Reg. */
  189. /* DON'T CARE of Attribute registers other than this */
  190. vga->attribute[0x11] = 0; /* over scancolor = black */
  191. /* DON't CARE of graphics[1], [2], [3], [4], [5], [6], [7] and [8] value */
  192. if(vga->linear && (ctlr->flag & Hlinear)) {
  193. /* enable linear mapping, no VGA memory and no page mapping */
  194. vga->graphics[0x10] = 0x0A;
  195. ctlr->flag |= Ulinear;
  196. }
  197. vt = vga->mode->vt;
  198. vde = vga->virty;
  199. vrs = vga->mode->vrs;
  200. vre = vga->mode->vre+6; /* shift 7 pixel up */
  201. if(vga->mode->interlace == 'v') {
  202. vt /= 2;
  203. vde /= 2;
  204. vrs /= 2;
  205. vre /= 2;
  206. }
  207. /* Reset Row scan */
  208. vga->crt[8] = 0;
  209. /* Line Compare, bit 6 of crt[9], bit 4 of crt[7] and crt[0x18], should be
  210. * vga->crt[9] = vgaxi(Crtx, 9) | ((vde>>9 & 1)<<6) & 0x7F;
  211. * vga->crt[7] = vgaxi(Crtx, 7) | ((vde>>8 & 1)<<4);
  212. * vga->crt[0x18] = vde & 0xFF;
  213. * However, above values don't work!! I don't know why. K.Okamoto
  214. */
  215. vga->crt[9] = 0; /* I don't know why ? */
  216. vga->crt[7] = 0; /* I don't know why ? */
  217. vga->crt[0x18] = 0; /* I don't know why ? */
  218. /* 32 bits Start Address of frame buffer (AGP aperture memory)
  219. vga->crt[0x42] = MSB 8 bits of Start Address Register, extended high start address Reg.
  220. vga->crt[0x40] = higer 6 bits in 0~5 bits, and the MSB = 1, extebded start address Reg.
  221. vga->crt[0x0C] = Start Address High Register
  222. vga->crt[0x0D] = Start Address Low Register
  223. LSB 2 bits of Start Address are always 0
  224. */
  225. vga->crt[0x42] = vga->pci->mem[0].bar>>24 & 0xFF;
  226. vga->crt[0x40] = vga->pci->mem[0].bar>>18 & 0x3F | 0x80;
  227. /* Start Address High */
  228. vga->crt[0x0C] = vga->pci->mem[0].bar>>10 & 0xFF;
  229. /* Start Address Low */
  230. vga->crt[0x0D] = (vga->pci->mem[0].bar >>2 + 1)& 0xFF;
  231. /* Underline Location, Memory Mode, DON'T CARE THIS VALUE */
  232. vga->crt[0x14] = 0x0;
  233. /* CRT Mode Control */
  234. vga->crt[0x17] = 0x80; /* CRT normal mode */
  235. /* Frame buffer memory offset (memory amount for a line) */
  236. /* vga->crt[0x13] = lower 8 bits of Offset Register
  237. vga->crt[0x41] = MSB 4 bits, those value should be
  238. vga->crt[0x13] = (vga->virtx*(vga->mode->z>>3)/4) & 0xFF;
  239. vga->crt[0x41] = (vga->virtx*(vga->mode->z>>3)/4)>>8 & 0x0F;
  240. However, those doesn't work properly K.Okamoto
  241. */
  242. vga->crt[0x41] = (vga->crt[0x13]>>8) & 0x0F; //dhog
  243. /* Horizontal Total */
  244. vga->crt[0] = ((vga->mode->ht>>3)-6) & 0xFF;
  245. /* Extended Horizontal Total Time Reg (ht) */
  246. vga->crt[0x35] = vga->mode->ht>>12 & 0x01;
  247. // vga->crt[0x35] = (((vga->mode->ht>>1)-5)>>8) & 0x01; //dhog
  248. /* Horizontal Display Enable End == horizontal width */
  249. vga->crt[1] = (vga->virtx-1)>>3 & 0xFF;
  250. /* Horizontal Blanking Start */
  251. vga->crt[2] = ((vga->mode->shb>>3)-1) & 0xFF;
  252. /* Horizontal blanking End crt[39](0),crt[5](7),crt[3](4:0) */
  253. vga->crt[3] = (vga->mode->shb - vga->virtx)>>3 & 0x1F;
  254. vga->crt[5] = ((vga->mode->shb - vga->virtx)>>3 & 0x20) <<2;
  255. vga->crt[0x39] = ((vga->mode->shb - vga->virtx)>>3 & 0x40) >>6;
  256. // vga->crt[0x39] = (vga->mode->ehb>>9) & 0x01; //dhog
  257. /* Horizontal Sync Start */
  258. vga->crt[4] = vga->mode->shb>>3 & 0xFF;
  259. /* Horizontal Sync End */
  260. vga->crt[5] |= vga->mode->ehb>>3 & 0x1F;
  261. /* Extended Vertical Total (vt) */
  262. vga->crt[6] = (vt - 2) & 0xFF;
  263. vga->crt[0x30] = (vt - 2)>>8 & 0x0F;
  264. /* Vertical Sync Period */
  265. vga->crt[0x11] = (vre - vrs - 2) & 0x0F;
  266. /* Vertical Blanking End */
  267. vga->crt[0x16] = (vre - vrs) & 0xFF;
  268. /* Extended Vertical Display End (y) */
  269. vga->crt[0x12] = (vde-1) & 0xFF;
  270. vga->crt[0x31] = (vde-1)>>8 & 0x0f;
  271. /* Extended Vertical Sync Start (vrs) */
  272. vga->crt[0x10] = (vrs-1) & 0xFF;
  273. vga->crt[0x32] = (vrs-1)>>8 & 0x0F;
  274. /* Extended Vertical Blanking Start (vrs) */
  275. vga->crt[0x15] = vrs & 0xFF;
  276. vga->crt[0x33] = vrs>>8 & 0x0F;
  277. if(vga->mode->interlace == 'v')
  278. vga->crt[0x70] = vrs | 0x80;
  279. else
  280. vga->crt[0x70] = 0;
  281. vga->crt[0x80] = 1;
  282. ctlr->flag |= Finit;
  283. }
  284. static void
  285. load(Vga* vga, Ctlr* ctlr)
  286. {
  287. int i;
  288. ulong *rp;
  289. I81x *i81x;
  290. i81x = vga->private;
  291. vgaxo(Attrx, 0x11, vga->attribute[0x11]);
  292. /* set the screen graphic mode */
  293. vgaxo(Crtx, 0x80, vga->crt[0x80]);
  294. vgaxo(Grx, 0x10, vga->graphics[0x10]);
  295. vgao(MiscW, vga->misc);
  296. for(i=0; i <= 0x18; i++)
  297. vgaxo(Crtx, i, vga->crt[i]);
  298. for(i=0x30; i <= 0x82; i++)
  299. vgaxo(Crtx, i, vga->crt[i]);
  300. vga->crt[0x40] |= 0x80; /* set CR40, then set the MSB bit of it */
  301. vgaxo(Crtx, 0x40, vga->crt[0x40]);
  302. /* 0x06000 = offset of Vertical Clock Devisor VGA0 */
  303. rp = (ulong*)(i81x->mmio+0x06000);
  304. for(i=0; i < nelem(i81x->clk); i++)
  305. *rp++ = i81x->clk[i];
  306. rp = (ulong*)(i81x->mmio+0x60000);
  307. for(i = 0; i < nelem(i81x->lcd); i++)
  308. *rp++ = i81x->lcd[i];
  309. /* set cursor, graphic mode */
  310. rp = (ulong*)(i81x->mmio+0x70008);
  311. *rp = i81x->pixconf;
  312. ctlr->flag |= Fload;
  313. }
  314. static void
  315. dump(Vga* vga, Ctlr* ctlr)
  316. {
  317. int i;
  318. Pcidev *p;
  319. I81x *i81x;
  320. char *name;
  321. name = ctlr->name;
  322. i81x = vga->private;
  323. printitem(name, "Crt30");
  324. for(i = 0x30; i <= 0x39; i++)
  325. printreg(vga->crt[i]);
  326. printitem(name, "Crt40");
  327. for(i = 0x40; i <= 0x42; i++)
  328. printreg(vga->crt[i]);
  329. printitem(name, "Crt70");
  330. for(i = 0x70; i <= 0x79; i++)
  331. printreg(vga->crt[i]);
  332. printitem(name, "Crt80");
  333. for(i = 0x80; i <= 0x82; i++)
  334. printreg(vga->crt[i]);
  335. printitem(name, "Graphics10");
  336. for(i = 0x10; i <= 0x1f; i++)
  337. printreg(vga->graphics[i]);
  338. printitem(name, "clk");
  339. for(i = 0; i < nelem(i81x->clk); i++)
  340. printreg(i81x->clk[i]);
  341. printitem(name, "lcd");
  342. for(i = 0; i < nelem(i81x->lcd); i++)
  343. printreg(i81x->lcd[i]);
  344. printitem(name, "pixconf");
  345. printreg(i81x->pixconf);
  346. p = i81x->pci;
  347. printitem(name, "mem[0]");
  348. Bprint(&stdout, "base %lux size %d\n", p->mem[0].bar & ~0x0F, p->mem[0].size);
  349. printitem(name, "mem[1]");
  350. Bprint(&stdout, "base %lux size %d\n", p->mem[1].bar & ~0x0F, p->mem[1].size);
  351. }
  352. Ctlr i81x = {
  353. "i81x", /* name */
  354. snarf, /* snarf */
  355. options, /* options */
  356. init, /* init */
  357. load, /* load */
  358. dump, /* dump */
  359. };
  360. Ctlr i81xhwgc = {
  361. "i81xhwgc", /* name */
  362. 0, /* snarf */
  363. 0, /* options */
  364. 0, /* init */
  365. 0, /* load */
  366. 0, /* dump */
  367. };