PageRenderTime 1024ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/add-ons/accelerants/nvidia_gpgpu/engine/nv_dac.c

https://github.com/mariuz/haiku
C | 527 lines | 319 code | 72 blank | 136 comment | 60 complexity | 59ed2469540063b1cd05bd090c61b976 MD5 | raw file
  1. /* program the DAC */
  2. /* Author:
  3. Rudolf Cornelissen 12/2003-6/2008
  4. */
  5. #define MODULE_BIT 0x00010000
  6. #include "nv_std.h"
  7. static status_t nv4_nv10_nv20_dac_pix_pll_find(
  8. display_mode target,float * calc_pclk,uint8 * m_result,uint8 * n_result,uint8 * p_result, uint8 test);
  9. /* see if an analog VGA monitor is connected to connector #1 */
  10. bool nv_dac_crt_connected(void)
  11. {
  12. uint32 output, dac;
  13. bool present;
  14. /* save output connector setting */
  15. output = DACR(OUTPUT);
  16. /* save DAC state */
  17. dac = DACR(TSTCTRL);
  18. /* turn on DAC */
  19. DACW(TSTCTRL, (DACR(TSTCTRL) & 0xfffeffff));
  20. if (si->ps.secondary_head)
  21. {
  22. /* select primary CRTC (head) and turn off CRT (and DVI?) outputs */
  23. DACW(OUTPUT, (output & 0x0000feee));
  24. }
  25. else
  26. {
  27. /* turn off CRT (and DVI?) outputs */
  28. /* note:
  29. * Don't touch the CRTC (head) assignment bit, as that would have undefined
  30. * results. Confirmed NV15 cards getting into lasting RAM access trouble
  31. * otherwise!! (goes for both system gfx RAM access and CRTC/DAC RAM access.) */
  32. DACW(OUTPUT, (output & 0x0000ffee));
  33. }
  34. /* wait for signal lines to stabilize */
  35. snooze(1000);
  36. /* re-enable CRT output */
  37. DACW(OUTPUT, (DACR(OUTPUT) | 0x00000001));
  38. /* setup RGB test signal levels to approx 30% of DAC range and enable them */
  39. DACW(TSTDATA, ((0x2 << 30) | (0x140 << 20) | (0x140 << 10) | (0x140 << 0)));
  40. /* route test signals to output */
  41. DACW(TSTCTRL, (DACR(TSTCTRL) | 0x00001000));
  42. /* wait for signal lines to stabilize */
  43. snooze(1000);
  44. /* do actual detection: all signals paths high == CRT connected */
  45. if (DACR(TSTCTRL) & 0x10000000)
  46. {
  47. present = true;
  48. LOG(4,("DAC: CRT detected on connector #1\n"));
  49. }
  50. else
  51. {
  52. present = false;
  53. LOG(4,("DAC: no CRT detected on connector #1\n"));
  54. }
  55. /* kill test signal routing */
  56. DACW(TSTCTRL, (DACR(TSTCTRL) & 0xffffefff));
  57. /* restore output connector setting */
  58. DACW(OUTPUT, output);
  59. /* restore DAC state */
  60. DACW(TSTCTRL, dac);
  61. return present;
  62. }
  63. /*set the mode, brightness is a value from 0->2 (where 1 is equivalent to direct)*/
  64. status_t nv_dac_mode(int mode,float brightness)
  65. {
  66. uint8 *r,*g,*b;
  67. int i, ri;
  68. /*set colour arrays to point to space reserved in shared info*/
  69. r = si->color_data;
  70. g = r + 256;
  71. b = g + 256;
  72. LOG(4,("DAC: Setting screen mode %d brightness %f\n", mode, brightness));
  73. /* init the palette for brightness specified */
  74. /* (Nvidia cards always use MSbits from screenbuffer as index for PAL) */
  75. for (i = 0; i < 256; i++)
  76. {
  77. ri = i * brightness;
  78. if (ri > 255) ri = 255;
  79. b[i] = g[i] = r[i] = ri;
  80. }
  81. if (nv_dac_palette(r,g,b) != B_OK) return B_ERROR;
  82. /* disable palette RAM adressing mask */
  83. NV_REG8(NV8_PALMASK) = 0xff;
  84. LOG(2,("DAC: PAL pixrdmsk readback $%02x\n", NV_REG8(NV8_PALMASK)));
  85. return B_OK;
  86. }
  87. /*program the DAC palette using the given r,g,b values*/
  88. status_t nv_dac_palette(uint8 r[256],uint8 g[256],uint8 b[256])
  89. {
  90. int i;
  91. LOG(4,("DAC: setting palette\n"));
  92. /* select first PAL adress before starting programming */
  93. NV_REG8(NV8_PALINDW) = 0x00;
  94. /* loop through all 256 to program DAC */
  95. for (i = 0; i < 256; i++)
  96. {
  97. /* the 6 implemented bits are on b0-b5 of the bus */
  98. NV_REG8(NV8_PALDATA) = r[i];
  99. NV_REG8(NV8_PALDATA) = g[i];
  100. NV_REG8(NV8_PALDATA) = b[i];
  101. }
  102. if (NV_REG8(NV8_PALINDW) != 0x00)
  103. {
  104. LOG(8,("DAC: PAL write index incorrect after programming\n"));
  105. return B_ERROR;
  106. }
  107. if (1)
  108. {//reread LUT
  109. uint8 R, G, B;
  110. /* select first PAL adress to read (modulo 3 counter) */
  111. NV_REG8(NV8_PALINDR) = 0x00;
  112. for (i = 0; i < 256; i++)
  113. {
  114. R = NV_REG8(NV8_PALDATA);
  115. G = NV_REG8(NV8_PALDATA);
  116. B = NV_REG8(NV8_PALDATA);
  117. if ((r[i] != R) || (g[i] != G) || (b[i] != B))
  118. LOG(1,("DAC palette %d: w %x %x %x, r %x %x %x\n", i, r[i], g[i], b[i], R, G, B)); // apsed
  119. }
  120. }
  121. return B_OK;
  122. }
  123. /*program the pixpll - frequency in kHz*/
  124. status_t nv_dac_set_pix_pll(display_mode target)
  125. {
  126. uint8 m=0,n=0,p=0;
  127. // uint time = 0;
  128. float pix_setting, req_pclk;
  129. status_t result;
  130. /* we offer this option because some panels have very tight restrictions,
  131. * and there's no overlapping settings range that makes them all work.
  132. * note:
  133. * this assumes the cards BIOS correctly programmed the panel (is likely) */
  134. //fixme: when VESA DDC EDID stuff is implemented, this option can be deleted...
  135. if (si->ps.tmds1_active && !si->settings.pgm_panel)
  136. {
  137. LOG(4,("DAC: Not programming DFP refresh (specified in nv.settings)\n"));
  138. return B_OK;
  139. }
  140. /* fix a DVI or laptop flatpanel to 60Hz refresh! */
  141. /* Note:
  142. * The pixelclock drives the flatpanel modeline, not the CRTC modeline. */
  143. if (si->ps.tmds1_active)
  144. {
  145. LOG(4,("DAC: Fixing DFP refresh to 60Hz!\n"));
  146. /* use the panel's modeline to determine the needed pixelclock */
  147. target.timing.pixel_clock = si->ps.p1_timing.pixel_clock;
  148. }
  149. req_pclk = (target.timing.pixel_clock)/1000.0;
  150. LOG(4,("DAC: Setting PIX PLL for pixelclock %f\n", req_pclk));
  151. /* signal that we actually want to set the mode */
  152. result = nv_dac_pix_pll_find(target,&pix_setting,&m,&n,&p, 1);
  153. if (result != B_OK)
  154. {
  155. return result;
  156. }
  157. /*reprogram (disable,select,wait for stability,enable)*/
  158. // DXIW(PIXCLKCTRL,(DXIR(PIXCLKCTRL)&0x0F)|0x04); /*disable the PIXPLL*/
  159. // DXIW(PIXCLKCTRL,(DXIR(PIXCLKCTRL)&0x0C)|0x01); /*select the PIXPLL*/
  160. /* program new frequency */
  161. DACW(PIXPLLC, ((p << 16) | (n << 8) | m));
  162. /* program 2nd set N and M scalers if they exist (b31=1 enables them) */
  163. if (si->ps.ext_pll) DACW(PIXPLLC2, 0x80000401);
  164. /* Wait for the PIXPLL frequency to lock until timeout occurs */
  165. //fixme: do NV cards have a LOCK indication bit??
  166. /* while((!(DXIR(PIXPLLSTAT)&0x40)) & (time <= 2000))
  167. {
  168. time++;
  169. snooze(1);
  170. }
  171. if (time > 2000)
  172. LOG(2,("DAC: PIX PLL frequency not locked!\n"));
  173. else
  174. LOG(2,("DAC: PIX PLL frequency locked\n"));
  175. DXIW(PIXCLKCTRL,DXIR(PIXCLKCTRL)&0x0B); //enable the PIXPLL
  176. */
  177. //for now:
  178. /* Give the PIXPLL frequency some time to lock... */
  179. snooze(1000);
  180. LOG(2,("DAC: PIX PLL frequency should be locked now...\n"));
  181. return B_OK;
  182. }
  183. /* find nearest valid pix pll */
  184. status_t nv_dac_pix_pll_find
  185. (display_mode target,float * calc_pclk,uint8 * m_result,uint8 * n_result,uint8 * p_result, uint8 test)
  186. {
  187. switch (si->ps.card_type) {
  188. default: return nv4_nv10_nv20_dac_pix_pll_find(target, calc_pclk, m_result, n_result, p_result, test);
  189. }
  190. return B_ERROR;
  191. }
  192. /* find nearest valid pixel PLL setting */
  193. static status_t nv4_nv10_nv20_dac_pix_pll_find(
  194. display_mode target,float * calc_pclk,uint8 * m_result,uint8 * n_result,uint8 * p_result, uint8 test)
  195. {
  196. int m = 0, n = 0, p = 0/*, m_max*/;
  197. float error, error_best = 999999999;
  198. int best[3];
  199. float f_vco, max_pclk;
  200. float req_pclk = target.timing.pixel_clock/1000.0;
  201. /* determine the max. reference-frequency postscaler setting for the
  202. * current card (see G100, G200 and G400 specs). */
  203. /* switch(si->ps.card_type)
  204. {
  205. case G100:
  206. LOG(4,("DAC: G100 restrictions apply\n"));
  207. m_max = 7;
  208. break;
  209. case G200:
  210. LOG(4,("DAC: G200 restrictions apply\n"));
  211. m_max = 7;
  212. break;
  213. default:
  214. LOG(4,("DAC: G400/G400MAX restrictions apply\n"));
  215. m_max = 32;
  216. break;
  217. }
  218. */
  219. LOG(4,("DAC: NV4/NV10/NV20 restrictions apply\n"));
  220. /* determine the max. pixelclock for the current videomode */
  221. switch (target.space)
  222. {
  223. case B_CMAP8:
  224. max_pclk = si->ps.max_dac1_clock_8;
  225. break;
  226. case B_RGB15_LITTLE:
  227. case B_RGB16_LITTLE:
  228. max_pclk = si->ps.max_dac1_clock_16;
  229. break;
  230. case B_RGB24_LITTLE:
  231. max_pclk = si->ps.max_dac1_clock_24;
  232. break;
  233. case B_RGB32_LITTLE:
  234. max_pclk = si->ps.max_dac1_clock_32;
  235. break;
  236. default:
  237. /* use fail-safe value */
  238. max_pclk = si->ps.max_dac1_clock_32;
  239. break;
  240. }
  241. /* if some dualhead mode is active, an extra restriction might apply */
  242. if ((target.flags & DUALHEAD_BITS) && (target.space == B_RGB32_LITTLE))
  243. max_pclk = si->ps.max_dac1_clock_32dh;
  244. /* Make sure the requested pixelclock is within the PLL's operational limits */
  245. /* lower limit is min_pixel_vco divided by highest postscaler-factor */
  246. if (req_pclk < (si->ps.min_pixel_vco / 16.0))
  247. {
  248. LOG(4,("DAC: clamping pixclock: requested %fMHz, set to %fMHz\n",
  249. req_pclk, (float)(si->ps.min_pixel_vco / 16.0)));
  250. req_pclk = (si->ps.min_pixel_vco / 16.0);
  251. }
  252. /* upper limit is given by pins in combination with current active mode */
  253. if (req_pclk > max_pclk)
  254. {
  255. LOG(4,("DAC: clamping pixclock: requested %fMHz, set to %fMHz\n",
  256. req_pclk, (float)max_pclk));
  257. req_pclk = max_pclk;
  258. }
  259. /* iterate through all valid PLL postscaler settings */
  260. for (p=0x01; p < 0x20; p = p<<1)
  261. {
  262. /* calculate the needed VCO frequency for this postscaler setting */
  263. f_vco = req_pclk * p;
  264. /* check if this is within range of the VCO specs */
  265. if ((f_vco >= si->ps.min_pixel_vco) && (f_vco <= si->ps.max_pixel_vco))
  266. {
  267. /* FX5600 and FX5700 tweak for 2nd set N and M scalers */
  268. if (si->ps.ext_pll) f_vco /= 4;
  269. /* iterate trough all valid reference-frequency postscaler settings */
  270. for (m = 7; m <= 14; m++)
  271. {
  272. /* check if phase-discriminator will be within operational limits */
  273. if (((si->ps.f_ref / m) < 1.0) || ((si->ps.f_ref / m) > 2.0)) continue;
  274. /* calculate VCO postscaler setting for current setup.. */
  275. n = (int)(((f_vco * m) / si->ps.f_ref) + 0.5);
  276. /* ..and check for validity */
  277. if ((n < 1) || (n > 255)) continue;
  278. /* find error in frequency this setting gives */
  279. if (si->ps.ext_pll)
  280. {
  281. /* FX5600 and FX5700 tweak for 2nd set N and M scalers */
  282. error = fabs((req_pclk / 4) - (((si->ps.f_ref / m) * n) / p));
  283. }
  284. else
  285. error = fabs(req_pclk - (((si->ps.f_ref / m) * n) / p));
  286. /* note the setting if best yet */
  287. if (error < error_best)
  288. {
  289. error_best = error;
  290. best[0]=m;
  291. best[1]=n;
  292. best[2]=p;
  293. }
  294. }
  295. }
  296. }
  297. /* setup the scalers programming values for found optimum setting */
  298. m = best[0];
  299. n = best[1];
  300. p = best[2];
  301. /* log the VCO frequency found */
  302. f_vco = ((si->ps.f_ref / m) * n);
  303. /* FX5600 and FX5700 tweak for 2nd set N and M scalers */
  304. if (si->ps.ext_pll) f_vco *= 4;
  305. LOG(2,("DAC: pix VCO frequency found %fMhz\n", f_vco));
  306. /* return the results */
  307. *calc_pclk = (f_vco / p);
  308. *m_result = m;
  309. *n_result = n;
  310. switch(p)
  311. {
  312. case 1:
  313. p = 0x00;
  314. break;
  315. case 2:
  316. p = 0x01;
  317. break;
  318. case 4:
  319. p = 0x02;
  320. break;
  321. case 8:
  322. p = 0x03;
  323. break;
  324. case 16:
  325. p = 0x04;
  326. break;
  327. }
  328. *p_result = p;
  329. /* display the found pixelclock values */
  330. LOG(2,("DAC: pix PLL check: requested %fMHz got %fMHz, mnp 0x%02x 0x%02x 0x%02x\n",
  331. req_pclk, *calc_pclk, *m_result, *n_result, *p_result));
  332. return B_OK;
  333. }
  334. /* find nearest valid system PLL setting */
  335. status_t nv_dac_sys_pll_find(
  336. float req_sclk, float* calc_sclk, uint8* m_result, uint8* n_result, uint8* p_result, uint8 test)
  337. {
  338. int m = 0, n = 0, p = 0, m_max, p_max;
  339. float error, error_best = 999999999;
  340. int best[3];
  341. float f_vco, discr_low, discr_high;
  342. /* determine the max. reference-frequency postscaler setting for the
  343. * current requested clock */
  344. LOG(4,("DAC: NV10/NV20/NV30 restrictions apply\n"));
  345. /* set max. useable reference frequency postscaler divider factor;
  346. * apparantly we would get distortions on high PLL output frequencies if
  347. * we use the phase-discriminator at low frequencies */
  348. if (req_sclk > 340.0) m_max = 2; /* Fpll > 340Mhz */
  349. else if (req_sclk > 250.0) m_max = 6; /* 250Mhz < Fpll <= 340Mhz */
  350. else m_max = 14; /* Fpll < 250Mhz */
  351. /* set max. useable VCO output postscaler divider factor */
  352. p_max = 16;
  353. /* set phase-discriminator frequency range (Mhz) (verified) */
  354. discr_low = 1.0;
  355. /* (high discriminator spec is failsafe) */
  356. discr_high = 14.0;
  357. LOG(4,("DAC: PLL reference frequency postscaler divider range is 1 - %d\n", m_max));
  358. LOG(4,("DAC: PLL VCO output postscaler divider range is 1 - %d\n", p_max));
  359. LOG(4,("DAC: PLL discriminator input frequency range is %2.2fMhz - %2.2fMhz\n",
  360. discr_low, discr_high));
  361. /* Make sure the requested clock is within the PLL's operational limits */
  362. /* lower limit is min_system_vco divided by highest postscaler-factor */
  363. if (req_sclk < (si->ps.min_system_vco / ((float)p_max)))
  364. {
  365. LOG(4,("DAC: clamping sysclock: requested %fMHz, set to %fMHz\n",
  366. req_sclk, (si->ps.min_system_vco / ((float)p_max))));
  367. req_sclk = (si->ps.min_system_vco / ((float)p_max));
  368. }
  369. /* upper limit is given by pins */
  370. if (req_sclk > si->ps.max_system_vco)
  371. {
  372. LOG(4,("DAC: clamping sysclock: requested %fMHz, set to %fMHz\n",
  373. req_sclk, (float)si->ps.max_system_vco));
  374. req_sclk = si->ps.max_system_vco;
  375. }
  376. /* iterate through all valid PLL postscaler settings */
  377. for (p=0x01; p <= p_max; p = p<<1)
  378. {
  379. /* calculate the needed VCO frequency for this postscaler setting */
  380. f_vco = req_sclk * p;
  381. /* check if this is within range of the VCO specs */
  382. if ((f_vco >= si->ps.min_system_vco) && (f_vco <= si->ps.max_system_vco))
  383. {
  384. /* FX5600 and FX5700 tweak for 2nd set N and M scalers */
  385. if (si->ps.ext_pll) f_vco /= 4;
  386. /* iterate trough all valid reference-frequency postscaler settings */
  387. for (m = 1; m <= m_max; m++)
  388. {
  389. /* check if phase-discriminator will be within operational limits */
  390. if (((si->ps.f_ref / m) < discr_low) || ((si->ps.f_ref / m) > discr_high))
  391. continue;
  392. /* calculate VCO postscaler setting for current setup.. */
  393. n = (int)(((f_vco * m) / si->ps.f_ref) + 0.5);
  394. /* ..and check for validity */
  395. if ((n < 1) || (n > 255)) continue;
  396. /* find error in frequency this setting gives */
  397. if (si->ps.ext_pll)
  398. {
  399. /* FX5600 and FX5700 tweak for 2nd set N and M scalers */
  400. error = fabs((req_sclk / 4) - (((si->ps.f_ref / m) * n) / p));
  401. }
  402. else
  403. error = fabs(req_sclk - (((si->ps.f_ref / m) * n) / p));
  404. /* note the setting if best yet */
  405. if (error < error_best)
  406. {
  407. error_best = error;
  408. best[0]=m;
  409. best[1]=n;
  410. best[2]=p;
  411. }
  412. }
  413. }
  414. }
  415. /* setup the scalers programming values for found optimum setting */
  416. m = best[0];
  417. n = best[1];
  418. p = best[2];
  419. /* log the VCO frequency found */
  420. f_vco = ((si->ps.f_ref / m) * n);
  421. /* FX5600 and FX5700 tweak for 2nd set N and M scalers */
  422. if (si->ps.ext_pll) f_vco *= 4;
  423. LOG(2,("DAC: sys VCO frequency found %fMhz\n", f_vco));
  424. /* return the results */
  425. *calc_sclk = (f_vco / p);
  426. *m_result = m;
  427. *n_result = n;
  428. switch(p)
  429. {
  430. case 1:
  431. p = 0x00;
  432. break;
  433. case 2:
  434. p = 0x01;
  435. break;
  436. case 4:
  437. p = 0x02;
  438. break;
  439. case 8:
  440. p = 0x03;
  441. break;
  442. case 16:
  443. p = 0x04;
  444. break;
  445. case 32:
  446. p = 0x05;
  447. break;
  448. }
  449. *p_result = p;
  450. /* display the found pixelclock values */
  451. LOG(2,("DAC: sys PLL check: requested %fMHz got %fMHz, mnp 0x%02x 0x%02x 0x%02x\n",
  452. req_sclk, *calc_sclk, *m_result, *n_result, *p_result));
  453. return B_OK;
  454. }