mach64_ct.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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 aty_get_pll_ct(const struct fb_info *info, union aty_pll *pll)
  323. {
  324. struct atyfb_par *par = (struct atyfb_par *) info->par;
  325. u8 tmp, clock;
  326. clock = aty_ld_8(CLOCK_CNTL, par) & 0x03U;
  327. tmp = clock << 1;
  328. pll->ct.vclk_post_div = (aty_ld_pll_ct(VCLK_POST_DIV, par) >> tmp) & 0x03U;
  329. pll->ct.pll_ext_cntl = aty_ld_pll_ct(PLL_EXT_CNTL, par) & 0x0FU;
  330. pll->ct.vclk_fb_div = aty_ld_pll_ct(VCLK0_FB_DIV + clock, par) & 0xFFU;
  331. pll->ct.pll_ref_div = aty_ld_pll_ct(PLL_REF_DIV, par);
  332. pll->ct.mclk_fb_div = aty_ld_pll_ct(MCLK_FB_DIV, par);
  333. pll->ct.pll_gen_cntl = aty_ld_pll_ct(PLL_GEN_CNTL, par);
  334. pll->ct.pll_vclk_cntl = aty_ld_pll_ct(PLL_VCLK_CNTL, par);
  335. if (M64_HAS(GTB_DSP)) {
  336. pll->ct.dsp_config = aty_ld_le32(DSP_CONFIG, par);
  337. pll->ct.dsp_on_off = aty_ld_le32(DSP_ON_OFF, par);
  338. }
  339. }
  340. static int aty_init_pll_ct(const struct fb_info *info, union aty_pll *pll)
  341. {
  342. struct atyfb_par *par = (struct atyfb_par *) info->par;
  343. u8 mpost_div, xpost_div, sclk_post_div_real;
  344. u32 q, memcntl, trp;
  345. u32 dsp_config, dsp_on_off, vga_dsp_config, vga_dsp_on_off;
  346. #ifdef DEBUG
  347. int pllmclk, pllsclk;
  348. #endif
  349. pll->ct.pll_ext_cntl = aty_ld_pll_ct(PLL_EXT_CNTL, par);
  350. pll->ct.xclk_post_div = pll->ct.pll_ext_cntl & 0x07;
  351. pll->ct.xclk_ref_div = 1;
  352. switch (pll->ct.xclk_post_div) {
  353. case 0: case 1: case 2: case 3:
  354. break;
  355. case 4:
  356. pll->ct.xclk_ref_div = 3;
  357. pll->ct.xclk_post_div = 0;
  358. break;
  359. default:
  360. printk(KERN_CRIT "atyfb: Unsupported xclk source: %d.\n", pll->ct.xclk_post_div);
  361. return -EINVAL;
  362. }
  363. pll->ct.mclk_fb_mult = 2;
  364. if(pll->ct.pll_ext_cntl & PLL_MFB_TIMES_4_2B) {
  365. pll->ct.mclk_fb_mult = 4;
  366. pll->ct.xclk_post_div -= 1;
  367. }
  368. #ifdef DEBUG
  369. printk("atyfb(%s): mclk_fb_mult=%d, xclk_post_div=%d\n",
  370. __func__, pll->ct.mclk_fb_mult, pll->ct.xclk_post_div);
  371. #endif
  372. memcntl = aty_ld_le32(MEM_CNTL, par);
  373. trp = (memcntl & 0x300) >> 8;
  374. pll->ct.xclkpagefaultdelay = ((memcntl & 0xc00) >> 10) + ((memcntl & 0x1000) >> 12) + trp + 2;
  375. pll->ct.xclkmaxrasdelay = ((memcntl & 0x70000) >> 16) + trp + 2;
  376. if (M64_HAS(FIFO_32)) {
  377. pll->ct.fifo_size = 32;
  378. } else {
  379. pll->ct.fifo_size = 24;
  380. pll->ct.xclkpagefaultdelay += 2;
  381. pll->ct.xclkmaxrasdelay += 3;
  382. }
  383. switch (par->ram_type) {
  384. case DRAM:
  385. if (info->fix.smem_len<=ONE_MB) {
  386. pll->ct.dsp_loop_latency = 10;
  387. } else {
  388. pll->ct.dsp_loop_latency = 8;
  389. pll->ct.xclkpagefaultdelay += 2;
  390. }
  391. break;
  392. case EDO:
  393. case PSEUDO_EDO:
  394. if (info->fix.smem_len<=ONE_MB) {
  395. pll->ct.dsp_loop_latency = 9;
  396. } else {
  397. pll->ct.dsp_loop_latency = 8;
  398. pll->ct.xclkpagefaultdelay += 1;
  399. }
  400. break;
  401. case SDRAM:
  402. if (info->fix.smem_len<=ONE_MB) {
  403. pll->ct.dsp_loop_latency = 11;
  404. } else {
  405. pll->ct.dsp_loop_latency = 10;
  406. pll->ct.xclkpagefaultdelay += 1;
  407. }
  408. break;
  409. case SGRAM:
  410. pll->ct.dsp_loop_latency = 8;
  411. pll->ct.xclkpagefaultdelay += 3;
  412. break;
  413. default:
  414. pll->ct.dsp_loop_latency = 11;
  415. pll->ct.xclkpagefaultdelay += 3;
  416. break;
  417. }
  418. if (pll->ct.xclkmaxrasdelay <= pll->ct.xclkpagefaultdelay)
  419. pll->ct.xclkmaxrasdelay = pll->ct.xclkpagefaultdelay + 1;
  420. /* Allow BIOS to override */
  421. dsp_config = aty_ld_le32(DSP_CONFIG, par);
  422. dsp_on_off = aty_ld_le32(DSP_ON_OFF, par);
  423. vga_dsp_config = aty_ld_le32(VGA_DSP_CONFIG, par);
  424. vga_dsp_on_off = aty_ld_le32(VGA_DSP_ON_OFF, par);
  425. if (dsp_config)
  426. pll->ct.dsp_loop_latency = (dsp_config & DSP_LOOP_LATENCY) >> 16;
  427. #if 0
  428. FIXME: is it relevant for us?
  429. if ((!dsp_on_off && !M64_HAS(RESET_3D)) ||
  430. ((dsp_on_off == vga_dsp_on_off) &&
  431. (!dsp_config || !((dsp_config ^ vga_dsp_config) & DSP_XCLKS_PER_QW)))) {
  432. vga_dsp_on_off &= VGA_DSP_OFF;
  433. vga_dsp_config &= VGA_DSP_XCLKS_PER_QW;
  434. if (ATIDivide(vga_dsp_on_off, vga_dsp_config, 5, 1) > 24)
  435. pll->ct.fifo_size = 32;
  436. else
  437. pll->ct.fifo_size = 24;
  438. }
  439. #endif
  440. /* Exit if the user does not want us to tamper with the clock
  441. rates of her chip. */
  442. if (par->mclk_per == 0) {
  443. u8 mclk_fb_div, pll_ext_cntl;
  444. pll->ct.pll_ref_div = aty_ld_pll_ct(PLL_REF_DIV, par);
  445. pll_ext_cntl = aty_ld_pll_ct(PLL_EXT_CNTL, par);
  446. pll->ct.xclk_post_div_real = postdividers[pll_ext_cntl & 0x07];
  447. mclk_fb_div = aty_ld_pll_ct(MCLK_FB_DIV, par);
  448. if (pll_ext_cntl & PLL_MFB_TIMES_4_2B)
  449. mclk_fb_div <<= 1;
  450. pll->ct.mclk_fb_div = mclk_fb_div;
  451. return 0;
  452. }
  453. pll->ct.pll_ref_div = par->pll_per * 2 * 255 / par->ref_clk_per;
  454. /* FIXME: use the VTB/GTB /3 post divider if it's better suited */
  455. q = par->ref_clk_per * pll->ct.pll_ref_div * 8 /
  456. (pll->ct.mclk_fb_mult * par->xclk_per);
  457. if (q < 16*8 || q > 255*8) {
  458. printk(KERN_CRIT "atxfb: xclk out of range\n");
  459. return -EINVAL;
  460. } else {
  461. xpost_div = (q < 128*8);
  462. xpost_div += (q < 64*8);
  463. xpost_div += (q < 32*8);
  464. }
  465. pll->ct.xclk_post_div_real = postdividers[xpost_div];
  466. pll->ct.mclk_fb_div = q * pll->ct.xclk_post_div_real / 8;
  467. #ifdef CONFIG_PPC
  468. if (machine_is(powermac)) {
  469. /* Override PLL_EXT_CNTL & 0x07. */
  470. pll->ct.xclk_post_div = xpost_div;
  471. pll->ct.xclk_ref_div = 1;
  472. }
  473. #endif
  474. #ifdef DEBUG
  475. pllmclk = (1000000 * pll->ct.mclk_fb_mult * pll->ct.mclk_fb_div) /
  476. (par->ref_clk_per * pll->ct.pll_ref_div);
  477. printk("atyfb(%s): pllmclk=%d MHz, xclk=%d MHz\n",
  478. __func__, pllmclk, pllmclk / pll->ct.xclk_post_div_real);
  479. #endif
  480. if (M64_HAS(SDRAM_MAGIC_PLL) && (par->ram_type >= SDRAM))
  481. pll->ct.pll_gen_cntl = OSC_EN;
  482. else
  483. pll->ct.pll_gen_cntl = OSC_EN | DLL_PWDN /* | FORCE_DCLK_TRI_STATE */;
  484. if (M64_HAS(MAGIC_POSTDIV))
  485. pll->ct.pll_ext_cntl = 0;
  486. else
  487. pll->ct.pll_ext_cntl = xpost_div;
  488. if (pll->ct.mclk_fb_mult == 4)
  489. pll->ct.pll_ext_cntl |= PLL_MFB_TIMES_4_2B;
  490. if (par->mclk_per == par->xclk_per) {
  491. pll->ct.pll_gen_cntl |= (xpost_div << 4); /* mclk == xclk */
  492. } else {
  493. /*
  494. * The chip clock is not equal to the memory clock.
  495. * Therefore we will use sclk to clock the chip.
  496. */
  497. pll->ct.pll_gen_cntl |= (6 << 4); /* mclk == sclk */
  498. q = par->ref_clk_per * pll->ct.pll_ref_div * 4 / par->mclk_per;
  499. if (q < 16*8 || q > 255*8) {
  500. printk(KERN_CRIT "atyfb: mclk out of range\n");
  501. return -EINVAL;
  502. } else {
  503. mpost_div = (q < 128*8);
  504. mpost_div += (q < 64*8);
  505. mpost_div += (q < 32*8);
  506. }
  507. sclk_post_div_real = postdividers[mpost_div];
  508. pll->ct.sclk_fb_div = q * sclk_post_div_real / 8;
  509. pll->ct.spll_cntl2 = mpost_div << 4;
  510. #ifdef DEBUG
  511. pllsclk = (1000000 * 2 * pll->ct.sclk_fb_div) /
  512. (par->ref_clk_per * pll->ct.pll_ref_div);
  513. printk("atyfb(%s): use sclk, pllsclk=%d MHz, sclk=mclk=%d MHz\n",
  514. __func__, pllsclk, pllsclk / sclk_post_div_real);
  515. #endif
  516. }
  517. /* Disable the extra precision pixel clock controls since we do not use them. */
  518. pll->ct.ext_vpll_cntl = aty_ld_pll_ct(EXT_VPLL_CNTL, par);
  519. pll->ct.ext_vpll_cntl &= ~(EXT_VPLL_EN | EXT_VPLL_VGA_EN | EXT_VPLL_INSYNC);
  520. return 0;
  521. }
  522. static void aty_resume_pll_ct(const struct fb_info *info,
  523. union aty_pll *pll)
  524. {
  525. struct atyfb_par *par = info->par;
  526. if (par->mclk_per != par->xclk_per) {
  527. /*
  528. * This disables the sclk, crashes the computer as reported:
  529. * aty_st_pll_ct(SPLL_CNTL2, 3, info);
  530. *
  531. * So it seems the sclk must be enabled before it is used;
  532. * so PLL_GEN_CNTL must be programmed *after* the sclk.
  533. */
  534. aty_st_pll_ct(SCLK_FB_DIV, pll->ct.sclk_fb_div, par);
  535. aty_st_pll_ct(SPLL_CNTL2, pll->ct.spll_cntl2, par);
  536. /*
  537. * SCLK has been started. Wait for the PLL to lock. 5 ms
  538. * should be enough according to mach64 programmer's guide.
  539. */
  540. mdelay(5);
  541. }
  542. aty_st_pll_ct(PLL_REF_DIV, pll->ct.pll_ref_div, par);
  543. aty_st_pll_ct(PLL_GEN_CNTL, pll->ct.pll_gen_cntl, par);
  544. aty_st_pll_ct(MCLK_FB_DIV, pll->ct.mclk_fb_div, par);
  545. aty_st_pll_ct(PLL_EXT_CNTL, pll->ct.pll_ext_cntl, par);
  546. aty_st_pll_ct(EXT_VPLL_CNTL, pll->ct.ext_vpll_cntl, par);
  547. }
  548. static int dummy(void)
  549. {
  550. return 0;
  551. }
  552. const struct aty_dac_ops aty_dac_ct = {
  553. .set_dac = (void *) dummy,
  554. };
  555. const struct aty_pll_ops aty_pll_ct = {
  556. .var_to_pll = aty_var_to_pll_ct,
  557. .pll_to_var = aty_pll_to_var_ct,
  558. .set_pll = aty_set_pll_ct,
  559. .get_pll = aty_get_pll_ct,
  560. .init_pll = aty_init_pll_ct,
  561. .resume_pll = aty_resume_pll_ct,
  562. };