mach64_ct.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. * ATI Mach64 CT/VT/GT/LT Support
  3. */
  4. #include <linux/fb.h>
  5. #include <linux/delay.h>
  6. #include <asm/io.h>
  7. #include <video/mach64.h>
  8. #include "atyfb.h"
  9. #undef DEBUG
  10. static int aty_valid_pll_ct (const struct fb_info *info, u32 vclk_per, struct pll_ct *pll);
  11. static int aty_dsp_gt (const struct fb_info *info, u32 bpp, struct pll_ct *pll);
  12. static int aty_var_to_pll_ct(const struct fb_info *info, u32 vclk_per, u32 bpp, union aty_pll *pll);
  13. static u32 aty_pll_to_var_ct(const struct fb_info *info, const union aty_pll *pll);
  14. u8 aty_ld_pll_ct(int offset, const struct atyfb_par *par)
  15. {
  16. u8 res;
  17. /* write addr byte */
  18. aty_st_8(CLOCK_CNTL_ADDR, (offset << 2) & PLL_ADDR, par);
  19. /* read the register value */
  20. res = aty_ld_8(CLOCK_CNTL_DATA, par);
  21. return res;
  22. }
  23. static void aty_st_pll_ct(int offset, u8 val, const struct atyfb_par *par)
  24. {
  25. /* write addr byte */
  26. aty_st_8(CLOCK_CNTL_ADDR, ((offset << 2) & PLL_ADDR) | PLL_WR_EN, par);
  27. /* write the register value */
  28. aty_st_8(CLOCK_CNTL_DATA, val & PLL_DATA, par);
  29. aty_st_8(CLOCK_CNTL_ADDR, ((offset << 2) & PLL_ADDR) & ~PLL_WR_EN, par);
  30. }
  31. /*
  32. * by Daniel Mantione
  33. * <daniel.mantione@freepascal.org>
  34. *
  35. *
  36. * ATI Mach64 CT clock synthesis description.
  37. *
  38. * All clocks on the Mach64 can be calculated using the same principle:
  39. *
  40. * XTALIN * x * FB_DIV
  41. * CLK = ----------------------
  42. * PLL_REF_DIV * POST_DIV
  43. *
  44. * XTALIN is a fixed speed clock. Common speeds are 14.31 MHz and 29.50 MHz.
  45. * PLL_REF_DIV can be set by the user, but is the same for all clocks.
  46. * FB_DIV can be set by the user for each clock individually, it should be set
  47. * between 128 and 255, the chip will generate a bad clock signal for too low
  48. * values.
  49. * x depends on the type of clock; usually it is 2, but for the MCLK it can also
  50. * be set to 4.
  51. * POST_DIV can be set by the user for each clock individually, Possible values
  52. * are 1,2,4,8 and for some clocks other values are available too.
  53. * CLK is of course the clock speed that is generated.
  54. *
  55. * The Mach64 has these clocks:
  56. *
  57. * MCLK The clock rate of the chip
  58. * XCLK The clock rate of the on-chip memory
  59. * VCLK0 First pixel clock of first CRT controller
  60. * VCLK1 Second pixel clock of first CRT controller
  61. * VCLK2 Third pixel clock of first CRT controller
  62. * VCLK3 Fourth pixel clock of first CRT controller
  63. * VCLK Selected pixel clock, one of VCLK0, VCLK1, VCLK2, VCLK3
  64. * V2CLK Pixel clock of the second CRT controller.
  65. * SCLK Multi-purpose clock
  66. *
  67. * - MCLK and XCLK use the same FB_DIV
  68. * - VCLK0 .. VCLK3 use the same FB_DIV
  69. * - V2CLK is needed when the second CRTC is used (can be used for dualhead);
  70. * i.e. CRT monitor connected to laptop has different resolution than built
  71. * in LCD monitor.
  72. * - SCLK is not available on all cards; it is know to exist on the Rage LT-PRO,
  73. * Rage XL and Rage Mobility. It is know not to exist on the Mach64 VT.
  74. * - V2CLK is not available on all cards, most likely only the Rage LT-PRO,
  75. * the Rage XL and the Rage Mobility
  76. *
  77. * SCLK can be used to:
  78. * - Clock the chip instead of MCLK
  79. * - Replace XTALIN with a user defined frequency
  80. * - Generate the pixel clock for the LCD monitor (instead of VCLK)
  81. */
  82. /*
  83. * It can be quite hard to calculate XCLK and MCLK if they don't run at the
  84. * same frequency. Luckily, until now all cards that need asynchrone clock
  85. * speeds seem to have SCLK.
  86. * So this driver uses SCLK to clock the chip and XCLK to clock the memory.
  87. */
  88. /* ------------------------------------------------------------------------- */
  89. /*
  90. * PLL programming (Mach64 CT family)
  91. *
  92. *
  93. * This procedure sets the display fifo. The display fifo is a buffer that
  94. * contains data read from the video memory that waits to be processed by
  95. * the CRT controller.
  96. *
  97. * On the more modern Mach64 variants, the chip doesn't calculate the
  98. * interval after which the display fifo has to be reloaded from memory
  99. * automatically, the driver has to do it instead.
  100. */
  101. #define Maximum_DSP_PRECISION 7
  102. static u8 postdividers[] = {1,2,4,8,3};
  103. static int aty_dsp_gt(const struct fb_info *info, u32 bpp, struct pll_ct *pll)
  104. {
  105. u32 dsp_off, dsp_on, dsp_xclks;
  106. u32 multiplier, divider, ras_multiplier, ras_divider, tmp;
  107. u8 vshift, xshift;
  108. s8 dsp_precision;
  109. multiplier = ((u32)pll->mclk_fb_div) * pll->vclk_post_div_real;
  110. divider = ((u32)pll->vclk_fb_div) * pll->xclk_ref_div;
  111. ras_multiplier = pll->xclkmaxrasdelay;
  112. ras_divider = 1;
  113. if (bpp>=8)
  114. divider = divider * (bpp >> 2);
  115. vshift = (6 - 2) - pll->xclk_post_div; /* FIFO is 64 bits wide in accelerator mode ... */
  116. if (bpp == 0)
  117. vshift--; /* ... but only 32 bits in VGA mode. */
  118. #ifdef CONFIG_FB_ATY_GENERIC_LCD
  119. if (pll->xres != 0) {
  120. struct atyfb_par *par = (struct atyfb_par *) info->par;
  121. multiplier = multiplier * par->lcd_width;
  122. divider = divider * pll->xres & ~7;
  123. ras_multiplier = ras_multiplier * par->lcd_width;
  124. ras_divider = ras_divider * pll->xres & ~7;
  125. }
  126. #endif
  127. /* If we don't do this, 32 bits for multiplier & divider won't be
  128. enough in certain situations! */
  129. while (((multiplier | divider) & 1) == 0) {
  130. multiplier = multiplier >> 1;
  131. divider = divider >> 1;
  132. }
  133. /* Determine DSP precision first */
  134. tmp = ((multiplier * pll->fifo_size) << vshift) / divider;
  135. for (dsp_precision = -5; tmp; dsp_precision++)
  136. tmp >>= 1;
  137. if (dsp_precision < 0)
  138. dsp_precision = 0;
  139. else if (dsp_precision > Maximum_DSP_PRECISION)
  140. dsp_precision = Maximum_DSP_PRECISION;
  141. xshift = 6 - dsp_precision;
  142. vshift += xshift;
  143. /* Move on to dsp_off */
  144. dsp_off = ((multiplier * (pll->fifo_size - 1)) << vshift) / divider -
  145. (1 << (vshift - xshift));
  146. /* if (bpp == 0)
  147. dsp_on = ((multiplier * 20 << vshift) + divider) / divider;
  148. else */
  149. {
  150. dsp_on = ((multiplier << vshift) + divider) / divider;
  151. tmp = ((ras_multiplier << xshift) + ras_divider) / ras_divider;
  152. if (dsp_on < tmp)
  153. dsp_on = tmp;
  154. dsp_on = dsp_on + (tmp * 2) + (pll->xclkpagefaultdelay << xshift);
  155. }
  156. /* Calculate rounding factor and apply it to dsp_on */
  157. tmp = ((1 << (Maximum_DSP_PRECISION - dsp_precision)) - 1) >> 1;
  158. dsp_on = ((dsp_on + tmp) / (tmp + 1)) * (tmp + 1);
  159. if (dsp_on >= ((dsp_off / (tmp + 1)) * (tmp + 1))) {
  160. dsp_on = dsp_off - (multiplier << vshift) / divider;
  161. dsp_on = (dsp_on / (tmp + 1)) * (tmp + 1);
  162. }
  163. /* Last but not least: dsp_xclks */
  164. dsp_xclks = ((multiplier << (vshift + 5)) + divider) / divider;
  165. /* Get register values. */
  166. pll->dsp_on_off = (dsp_on << 16) + dsp_off;
  167. pll->dsp_config = (dsp_precision << 20) | (pll->dsp_loop_latency << 16) | dsp_xclks;
  168. #ifdef DEBUG
  169. printk("atyfb(%s): dsp_config 0x%08x, dsp_on_off 0x%08x\n",
  170. __func__, pll->dsp_config, pll->dsp_on_off);
  171. #endif
  172. return 0;
  173. }
  174. static int aty_valid_pll_ct(const struct fb_info *info, u32 vclk_per, struct pll_ct *pll)
  175. {
  176. u32 q;
  177. struct atyfb_par *par = (struct atyfb_par *) info->par;
  178. int pllvclk;
  179. /* FIXME: use the VTB/GTB /{3,6,12} post dividers if they're better suited */
  180. q = par->ref_clk_per * pll->pll_ref_div * 4 / vclk_per;
  181. if (q < 16*8 || q > 255*8) {
  182. printk(KERN_CRIT "atyfb: vclk out of range\n");
  183. return -EINVAL;
  184. } else {
  185. pll->vclk_post_div = (q < 128*8);
  186. pll->vclk_post_div += (q < 64*8);
  187. pll->vclk_post_div += (q < 32*8);
  188. }
  189. pll->vclk_post_div_real = postdividers[pll->vclk_post_div];
  190. // pll->vclk_post_div <<= 6;
  191. pll->vclk_fb_div = q * pll->vclk_post_div_real / 8;
  192. pllvclk = (1000000 * 2 * pll->vclk_fb_div) /
  193. (par->ref_clk_per * pll->pll_ref_div);
  194. #ifdef DEBUG
  195. printk("atyfb(%s): pllvclk=%d MHz, vclk=%d MHz\n",
  196. __func__, pllvclk, pllvclk / pll->vclk_post_div_real);
  197. #endif
  198. pll->pll_vclk_cntl = 0x03; /* VCLK = PLL_VCLK/VCLKx_POST */
  199. /* Set ECP (scaler/overlay clock) divider */
  200. if (par->pll_limits.ecp_max) {
  201. int ecp = pllvclk / pll->vclk_post_div_real;
  202. int ecp_div = 0;
  203. while (ecp > par->pll_limits.ecp_max && ecp_div < 2) {
  204. ecp >>= 1;
  205. ecp_div++;
  206. }
  207. pll->pll_vclk_cntl |= ecp_div << 4;
  208. }
  209. return 0;
  210. }
  211. static int aty_var_to_pll_ct(const struct fb_info *info, u32 vclk_per, u32 bpp, union aty_pll *pll)
  212. {
  213. struct atyfb_par *par = (struct atyfb_par *) info->par;
  214. int err;
  215. if ((err = aty_valid_pll_ct(info, vclk_per, &pll->ct)))
  216. return err;
  217. if (M64_HAS(GTB_DSP) && (err = aty_dsp_gt(info, bpp, &pll->ct)))
  218. return err;
  219. /*aty_calc_pll_ct(info, &pll->ct);*/
  220. return 0;
  221. }
  222. static u32 aty_pll_to_var_ct(const struct fb_info *info, const union aty_pll *pll)
  223. {
  224. struct atyfb_par *par = (struct atyfb_par *) info->par;
  225. u32 ret;
  226. ret = par->ref_clk_per * pll->ct.pll_ref_div * pll->ct.vclk_post_div_real / pll->ct.vclk_fb_div / 2;
  227. #ifdef CONFIG_FB_ATY_GENERIC_LCD
  228. if(pll->ct.xres > 0) {
  229. ret *= par->lcd_width;
  230. ret /= pll->ct.xres;
  231. }
  232. #endif
  233. #ifdef DEBUG
  234. printk("atyfb(%s): calculated 0x%08X(%i)\n", __func__, ret, ret);
  235. #endif
  236. return ret;
  237. }
  238. void aty_set_pll_ct(const struct fb_info *info, const union aty_pll *pll)
  239. {
  240. struct atyfb_par *par = (struct atyfb_par *) info->par;
  241. u32 crtc_gen_cntl, lcd_gen_cntrl;
  242. u8 tmp, tmp2;
  243. lcd_gen_cntrl = 0;
  244. #ifdef DEBUG
  245. printk("atyfb(%s): about to program:\n"
  246. "pll_ext_cntl=0x%02x pll_gen_cntl=0x%02x pll_vclk_cntl=0x%02x\n",
  247. __func__,
  248. pll->ct.pll_ext_cntl, pll->ct.pll_gen_cntl, pll->ct.pll_vclk_cntl);
  249. printk("atyfb(%s): setting clock %lu for FeedBackDivider %i, ReferenceDivider %i, PostDivider %i(%i)\n",
  250. __func__,
  251. par->clk_wr_offset, pll->ct.vclk_fb_div,
  252. pll->ct.pll_ref_div, pll->ct.vclk_post_div, pll->ct.vclk_post_div_real);
  253. #endif
  254. #ifdef CONFIG_FB_ATY_GENERIC_LCD
  255. if (par->lcd_table != 0) {
  256. /* turn off LCD */
  257. lcd_gen_cntrl = aty_ld_lcd(LCD_GEN_CNTL, par);
  258. aty_st_lcd(LCD_GEN_CNTL, lcd_gen_cntrl & ~LCD_ON, par);
  259. }
  260. #endif
  261. aty_st_8(CLOCK_CNTL, par->clk_wr_offset | CLOCK_STROBE, par);
  262. /* Temporarily switch to accelerator mode */
  263. crtc_gen_cntl = aty_ld_le32(CRTC_GEN_CNTL, par);
  264. if (!(crtc_gen_cntl & CRTC_EXT_DISP_EN))
  265. aty_st_le32(CRTC_GEN_CNTL, crtc_gen_cntl | CRTC_EXT_DISP_EN, par);
  266. /* Reset VCLK generator */
  267. aty_st_pll_ct(PLL_VCLK_CNTL, pll->ct.pll_vclk_cntl, par);
  268. /* Set post-divider */
  269. tmp2 = par->clk_wr_offset << 1;
  270. tmp = aty_ld_pll_ct(VCLK_POST_DIV, par);
  271. tmp &= ~(0x03U << tmp2);
  272. tmp |= ((pll->ct.vclk_post_div & 0x03U) << tmp2);
  273. aty_st_pll_ct(VCLK_POST_DIV, tmp, par);
  274. /* Set extended post-divider */
  275. tmp = aty_ld_pll_ct(PLL_EXT_CNTL, par);
  276. tmp &= ~(0x10U << par->clk_wr_offset);
  277. tmp &= 0xF0U;
  278. tmp |= pll->ct.pll_ext_cntl;
  279. aty_st_pll_ct(PLL_EXT_CNTL, tmp, par);
  280. /* Set feedback divider */
  281. tmp = VCLK0_FB_DIV + par->clk_wr_offset;
  282. aty_st_pll_ct(tmp, (pll->ct.vclk_fb_div & 0xFFU), par);
  283. aty_st_pll_ct(PLL_GEN_CNTL, (pll->ct.pll_gen_cntl & (~(PLL_OVERRIDE | PLL_MCLK_RST))) | OSC_EN, par);
  284. /* End VCLK generator reset */
  285. aty_st_pll_ct(PLL_VCLK_CNTL, pll->ct.pll_vclk_cntl & ~(PLL_VCLK_RST), par);
  286. mdelay(5);
  287. aty_st_pll_ct(PLL_GEN_CNTL, pll->ct.pll_gen_cntl, par);
  288. aty_st_pll_ct(PLL_VCLK_CNTL, pll->ct.pll_vclk_cntl, par);
  289. mdelay(1);
  290. /* Restore mode register */
  291. if (!(crtc_gen_cntl & CRTC_EXT_DISP_EN))
  292. aty_st_le32(CRTC_GEN_CNTL, crtc_gen_cntl, par);
  293. if (M64_HAS(GTB_DSP)) {
  294. u8 dll_cntl;
  295. if (M64_HAS(XL_DLL))
  296. dll_cntl = 0x80;
  297. else if (par->ram_type >= SDRAM)
  298. dll_cntl = 0xa6;
  299. else
  300. dll_cntl = 0xa0;
  301. aty_st_pll_ct(DLL_CNTL, dll_cntl, par);
  302. aty_st_pll_ct(VFC_CNTL, 0x1b, par);
  303. aty_st_le32(DSP_CONFIG, pll->ct.dsp_config, par);
  304. aty_st_le32(DSP_ON_OFF, pll->ct.dsp_on_off, par);
  305. mdelay(10);
  306. aty_st_pll_ct(DLL_CNTL, dll_cntl, par);
  307. mdelay(10);
  308. aty_st_pll_ct(DLL_CNTL, dll_cntl | 0x40, par);
  309. mdelay(10);
  310. aty_st_pll_ct(DLL_CNTL, dll_cntl & ~0x40, par);
  311. }
  312. #ifdef CONFIG_FB_ATY_GENERIC_LCD
  313. if (par->lcd_table != 0) {
  314. /* restore LCD */
  315. aty_st_lcd(LCD_GEN_CNTL, lcd_gen_cntrl, par);
  316. }
  317. #endif
  318. }
  319. static void __devinit aty_get_pll_ct(const struct fb_info *info,
  320. union aty_pll *pll)
  321. {
  322. struct atyfb_par *par = (struct atyfb_par *) info->par;
  323. u8 tmp, clock;
  324. clock = aty_ld_8(CLOCK_CNTL, par) & 0x03U;
  325. tmp = clock << 1;
  326. pll->ct.vclk_post_div = (aty_ld_pll_ct(VCLK_POST_DIV, par) >> tmp) & 0x03U;
  327. pll->ct.pll_ext_cntl = aty_ld_pll_ct(PLL_EXT_CNTL, par) & 0x0FU;
  328. pll->ct.vclk_fb_div = aty_ld_pll_ct(VCLK0_FB_DIV + clock, par) & 0xFFU;
  329. pll->ct.pll_ref_div = aty_ld_pll_ct(PLL_REF_DIV, par);
  330. pll->ct.mclk_fb_div = aty_ld_pll_ct(MCLK_FB_DIV, par);
  331. pll->ct.pll_gen_cntl = aty_ld_pll_ct(PLL_GEN_CNTL, par);
  332. pll->ct.pll_vclk_cntl = aty_ld_pll_ct(PLL_VCLK_CNTL, par);
  333. if (M64_HAS(GTB_DSP)) {
  334. pll->ct.dsp_config = aty_ld_le32(DSP_CONFIG, par);
  335. pll->ct.dsp_on_off = aty_ld_le32(DSP_ON_OFF, par);
  336. }
  337. }
  338. static int __devinit aty_init_pll_ct(const struct fb_info *info,
  339. union aty_pll *pll)
  340. {
  341. struct atyfb_par *par = (struct atyfb_par *) info->par;
  342. u8 mpost_div, xpost_div, sclk_post_div_real;
  343. u32 q, memcntl, trp;
  344. u32 dsp_config, dsp_on_off, vga_dsp_config, vga_dsp_on_off;
  345. #ifdef DEBUG
  346. int pllmclk, pllsclk;
  347. #endif
  348. pll->ct.pll_ext_cntl = aty_ld_pll_ct(PLL_EXT_CNTL, par);
  349. pll->ct.xclk_post_div = pll->ct.pll_ext_cntl & 0x07;
  350. pll->ct.xclk_ref_div = 1;
  351. switch (pll->ct.xclk_post_div) {
  352. case 0: case 1: case 2: case 3:
  353. break;
  354. case 4:
  355. pll->ct.xclk_ref_div = 3;
  356. pll->ct.xclk_post_div = 0;
  357. break;
  358. default:
  359. printk(KERN_CRIT "atyfb: Unsupported xclk source: %d.\n", pll->ct.xclk_post_div);
  360. return -EINVAL;
  361. }
  362. pll->ct.mclk_fb_mult = 2;
  363. if(pll->ct.pll_ext_cntl & PLL_MFB_TIMES_4_2B) {
  364. pll->ct.mclk_fb_mult = 4;
  365. pll->ct.xclk_post_div -= 1;
  366. }
  367. #ifdef DEBUG
  368. printk("atyfb(%s): mclk_fb_mult=%d, xclk_post_div=%d\n",
  369. __func__, pll->ct.mclk_fb_mult, pll->ct.xclk_post_div);
  370. #endif
  371. memcntl = aty_ld_le32(MEM_CNTL, par);
  372. trp = (memcntl & 0x300) >> 8;
  373. pll->ct.xclkpagefaultdelay = ((memcntl & 0xc00) >> 10) + ((memcntl & 0x1000) >> 12) + trp + 2;
  374. pll->ct.xclkmaxrasdelay = ((memcntl & 0x70000) >> 16) + trp + 2;
  375. if (M64_HAS(FIFO_32)) {
  376. pll->ct.fifo_size = 32;
  377. } else {
  378. pll->ct.fifo_size = 24;
  379. pll->ct.xclkpagefaultdelay += 2;
  380. pll->ct.xclkmaxrasdelay += 3;
  381. }
  382. switch (par->ram_type) {
  383. case DRAM:
  384. if (info->fix.smem_len<=ONE_MB) {
  385. pll->ct.dsp_loop_latency = 10;
  386. } else {
  387. pll->ct.dsp_loop_latency = 8;
  388. pll->ct.xclkpagefaultdelay += 2;
  389. }
  390. break;
  391. case EDO:
  392. case PSEUDO_EDO:
  393. if (info->fix.smem_len<=ONE_MB) {
  394. pll->ct.dsp_loop_latency = 9;
  395. } else {
  396. pll->ct.dsp_loop_latency = 8;
  397. pll->ct.xclkpagefaultdelay += 1;
  398. }
  399. break;
  400. case SDRAM:
  401. if (info->fix.smem_len<=ONE_MB) {
  402. pll->ct.dsp_loop_latency = 11;
  403. } else {
  404. pll->ct.dsp_loop_latency = 10;
  405. pll->ct.xclkpagefaultdelay += 1;
  406. }
  407. break;
  408. case SGRAM:
  409. pll->ct.dsp_loop_latency = 8;
  410. pll->ct.xclkpagefaultdelay += 3;
  411. break;
  412. default:
  413. pll->ct.dsp_loop_latency = 11;
  414. pll->ct.xclkpagefaultdelay += 3;
  415. break;
  416. }
  417. if (pll->ct.xclkmaxrasdelay <= pll->ct.xclkpagefaultdelay)
  418. pll->ct.xclkmaxrasdelay = pll->ct.xclkpagefaultdelay + 1;
  419. /* Allow BIOS to override */
  420. dsp_config = aty_ld_le32(DSP_CONFIG, par);
  421. dsp_on_off = aty_ld_le32(DSP_ON_OFF, par);
  422. vga_dsp_config = aty_ld_le32(VGA_DSP_CONFIG, par);
  423. vga_dsp_on_off = aty_ld_le32(VGA_DSP_ON_OFF, par);
  424. if (dsp_config)
  425. pll->ct.dsp_loop_latency = (dsp_config & DSP_LOOP_LATENCY) >> 16;
  426. #if 0
  427. FIXME: is it relevant for us?
  428. if ((!dsp_on_off && !M64_HAS(RESET_3D)) ||
  429. ((dsp_on_off == vga_dsp_on_off) &&
  430. (!dsp_config || !((dsp_config ^ vga_dsp_config) & DSP_XCLKS_PER_QW)))) {
  431. vga_dsp_on_off &= VGA_DSP_OFF;
  432. vga_dsp_config &= VGA_DSP_XCLKS_PER_QW;
  433. if (ATIDivide(vga_dsp_on_off, vga_dsp_config, 5, 1) > 24)
  434. pll->ct.fifo_size = 32;
  435. else
  436. pll->ct.fifo_size = 24;
  437. }
  438. #endif
  439. /* Exit if the user does not want us to tamper with the clock
  440. rates of her chip. */
  441. if (par->mclk_per == 0) {
  442. u8 mclk_fb_div, pll_ext_cntl;
  443. pll->ct.pll_ref_div = aty_ld_pll_ct(PLL_REF_DIV, par);
  444. pll_ext_cntl = aty_ld_pll_ct(PLL_EXT_CNTL, par);
  445. pll->ct.xclk_post_div_real = postdividers[pll_ext_cntl & 0x07];
  446. mclk_fb_div = aty_ld_pll_ct(MCLK_FB_DIV, par);
  447. if (pll_ext_cntl & PLL_MFB_TIMES_4_2B)
  448. mclk_fb_div <<= 1;
  449. pll->ct.mclk_fb_div = mclk_fb_div;
  450. return 0;
  451. }
  452. pll->ct.pll_ref_div = par->pll_per * 2 * 255 / par->ref_clk_per;
  453. /* FIXME: use the VTB/GTB /3 post divider if it's better suited */
  454. q = par->ref_clk_per * pll->ct.pll_ref_div * 8 /
  455. (pll->ct.mclk_fb_mult * par->xclk_per);
  456. if (q < 16*8 || q > 255*8) {
  457. printk(KERN_CRIT "atxfb: xclk out of range\n");
  458. return -EINVAL;
  459. } else {
  460. xpost_div = (q < 128*8);
  461. xpost_div += (q < 64*8);
  462. xpost_div += (q < 32*8);
  463. }
  464. pll->ct.xclk_post_div_real = postdividers[xpost_div];
  465. pll->ct.mclk_fb_div = q * pll->ct.xclk_post_div_real / 8;
  466. #ifdef DEBUG
  467. pllmclk = (1000000 * pll->ct.mclk_fb_mult * pll->ct.mclk_fb_div) /
  468. (par->ref_clk_per * pll->ct.pll_ref_div);
  469. printk("atyfb(%s): pllmclk=%d MHz, xclk=%d MHz\n",
  470. __func__, pllmclk, pllmclk / pll->ct.xclk_post_div_real);
  471. #endif
  472. if (M64_HAS(SDRAM_MAGIC_PLL) && (par->ram_type >= SDRAM))
  473. pll->ct.pll_gen_cntl = OSC_EN;
  474. else
  475. pll->ct.pll_gen_cntl = OSC_EN | DLL_PWDN /* | FORCE_DCLK_TRI_STATE */;
  476. if (M64_HAS(MAGIC_POSTDIV))
  477. pll->ct.pll_ext_cntl = 0;
  478. else
  479. pll->ct.pll_ext_cntl = xpost_div;
  480. if (pll->ct.mclk_fb_mult == 4)
  481. pll->ct.pll_ext_cntl |= PLL_MFB_TIMES_4_2B;
  482. if (par->mclk_per == par->xclk_per) {
  483. pll->ct.pll_gen_cntl |= (xpost_div << 4); /* mclk == xclk */
  484. } else {
  485. /*
  486. * The chip clock is not equal to the memory clock.
  487. * Therefore we will use sclk to clock the chip.
  488. */
  489. pll->ct.pll_gen_cntl |= (6 << 4); /* mclk == sclk */
  490. q = par->ref_clk_per * pll->ct.pll_ref_div * 4 / par->mclk_per;
  491. if (q < 16*8 || q > 255*8) {
  492. printk(KERN_CRIT "atyfb: mclk out of range\n");
  493. return -EINVAL;
  494. } else {
  495. mpost_div = (q < 128*8);
  496. mpost_div += (q < 64*8);
  497. mpost_div += (q < 32*8);
  498. }
  499. sclk_post_div_real = postdividers[mpost_div];
  500. pll->ct.sclk_fb_div = q * sclk_post_div_real / 8;
  501. pll->ct.spll_cntl2 = mpost_div << 4;
  502. #ifdef DEBUG
  503. pllsclk = (1000000 * 2 * pll->ct.sclk_fb_div) /
  504. (par->ref_clk_per * pll->ct.pll_ref_div);
  505. printk("atyfb(%s): use sclk, pllsclk=%d MHz, sclk=mclk=%d MHz\n",
  506. __func__, pllsclk, pllsclk / sclk_post_div_real);
  507. #endif
  508. }
  509. /* Disable the extra precision pixel clock controls since we do not use them. */
  510. pll->ct.ext_vpll_cntl = aty_ld_pll_ct(EXT_VPLL_CNTL, par);
  511. pll->ct.ext_vpll_cntl &= ~(EXT_VPLL_EN | EXT_VPLL_VGA_EN | EXT_VPLL_INSYNC);
  512. return 0;
  513. }
  514. static void aty_resume_pll_ct(const struct fb_info *info,
  515. union aty_pll *pll)
  516. {
  517. struct atyfb_par *par = info->par;
  518. if (par->mclk_per != par->xclk_per) {
  519. /*
  520. * This disables the sclk, crashes the computer as reported:
  521. * aty_st_pll_ct(SPLL_CNTL2, 3, info);
  522. *
  523. * So it seems the sclk must be enabled before it is used;
  524. * so PLL_GEN_CNTL must be programmed *after* the sclk.
  525. */
  526. aty_st_pll_ct(SCLK_FB_DIV, pll->ct.sclk_fb_div, par);
  527. aty_st_pll_ct(SPLL_CNTL2, pll->ct.spll_cntl2, par);
  528. /*
  529. * SCLK has been started. Wait for the PLL to lock. 5 ms
  530. * should be enough according to mach64 programmer's guide.
  531. */
  532. mdelay(5);
  533. }
  534. aty_st_pll_ct(PLL_REF_DIV, pll->ct.pll_ref_div, par);
  535. aty_st_pll_ct(PLL_GEN_CNTL, pll->ct.pll_gen_cntl, par);
  536. aty_st_pll_ct(MCLK_FB_DIV, pll->ct.mclk_fb_div, par);
  537. aty_st_pll_ct(PLL_EXT_CNTL, pll->ct.pll_ext_cntl, par);
  538. aty_st_pll_ct(EXT_VPLL_CNTL, pll->ct.ext_vpll_cntl, par);
  539. }
  540. static int dummy(void)
  541. {
  542. return 0;
  543. }
  544. const struct aty_dac_ops aty_dac_ct = {
  545. .set_dac = (void *) dummy,
  546. };
  547. const struct aty_pll_ops aty_pll_ct = {
  548. .var_to_pll = aty_var_to_pll_ct,
  549. .pll_to_var = aty_pll_to_var_ct,
  550. .set_pll = aty_set_pll_ct,
  551. .get_pll = aty_get_pll_ct,
  552. .init_pll = aty_init_pll_ct,
  553. .resume_pll = aty_resume_pll_ct,
  554. };