mach64_ct.c 19 KB

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