mach64_ct.c 19 KB

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