Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | |
2 | /* | |
3 | * ATI Mach64 CT/VT/GT/LT Support | |
4 | */ | |
5 | ||
6 | #include <linux/fb.h> | |
7 | #include <linux/delay.h> | |
8 | #include <asm/io.h> | |
9 | #include <video/mach64.h> | |
10 | #include "atyfb.h" | |
11 | ||
12 | #undef DEBUG | |
13 | ||
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 | ||
19 | u8 aty_ld_pll_ct(int offset, const struct atyfb_par *par) | |
20 | { | |
21 | u8 res; | |
22 | ||
23 | /* write addr byte */ | |
24 | aty_st_8(CLOCK_CNTL_ADDR, (offset << 2) & PLL_ADDR, par); | |
25 | /* read the register value */ | |
26 | res = aty_ld_8(CLOCK_CNTL_DATA, par); | |
27 | return res; | |
28 | } | |
29 | ||
9c5b39e0 | 30 | static void aty_st_pll_ct(int offset, u8 val, const struct atyfb_par *par) |
1da177e4 LT |
31 | { |
32 | /* write addr byte */ | |
33 | aty_st_8(CLOCK_CNTL_ADDR, ((offset << 2) & PLL_ADDR) | PLL_WR_EN, par); | |
34 | /* write the register value */ | |
35 | aty_st_8(CLOCK_CNTL_DATA, val & PLL_DATA, par); | |
36 | aty_st_8(CLOCK_CNTL_ADDR, ((offset << 2) & PLL_ADDR) & ~PLL_WR_EN, par); | |
37 | } | |
38 | ||
39 | /* | |
40 | * by Daniel Mantione | |
41 | * <daniel.mantione@freepascal.org> | |
42 | * | |
43 | * | |
44 | * ATI Mach64 CT clock synthesis description. | |
45 | * | |
46 | * All clocks on the Mach64 can be calculated using the same principle: | |
47 | * | |
48 | * XTALIN * x * FB_DIV | |
49 | * CLK = ---------------------- | |
50 | * PLL_REF_DIV * POST_DIV | |
51 | * | |
52 | * XTALIN is a fixed speed clock. Common speeds are 14.31 MHz and 29.50 MHz. | |
53 | * PLL_REF_DIV can be set by the user, but is the same for all clocks. | |
54 | * FB_DIV can be set by the user for each clock individually, it should be set | |
55 | * between 128 and 255, the chip will generate a bad clock signal for too low | |
56 | * values. | |
57 | * x depends on the type of clock; usually it is 2, but for the MCLK it can also | |
58 | * be set to 4. | |
59 | * POST_DIV can be set by the user for each clock individually, Possible values | |
60 | * are 1,2,4,8 and for some clocks other values are available too. | |
61 | * CLK is of course the clock speed that is generated. | |
62 | * | |
63 | * The Mach64 has these clocks: | |
64 | * | |
65 | * MCLK The clock rate of the chip | |
66 | * XCLK The clock rate of the on-chip memory | |
67 | * VCLK0 First pixel clock of first CRT controller | |
68 | * VCLK1 Second pixel clock of first CRT controller | |
69 | * VCLK2 Third pixel clock of first CRT controller | |
70 | * VCLK3 Fourth pixel clock of first CRT controller | |
71 | * VCLK Selected pixel clock, one of VCLK0, VCLK1, VCLK2, VCLK3 | |
72 | * V2CLK Pixel clock of the second CRT controller. | |
73 | * SCLK Multi-purpose clock | |
74 | * | |
75 | * - MCLK and XCLK use the same FB_DIV | |
76 | * - VCLK0 .. VCLK3 use the same FB_DIV | |
77 | * - V2CLK is needed when the second CRTC is used (can be used for dualhead); | |
78 | * i.e. CRT monitor connected to laptop has different resolution than built | |
79 | * in LCD monitor. | |
80 | * - SCLK is not available on all cards; it is know to exist on the Rage LT-PRO, | |
81 | * Rage XL and Rage Mobility. It is know not to exist on the Mach64 VT. | |
82 | * - V2CLK is not available on all cards, most likely only the Rage LT-PRO, | |
83 | * the Rage XL and the Rage Mobility | |
84 | * | |
85 | * SCLK can be used to: | |
86 | * - Clock the chip instead of MCLK | |
87 | * - Replace XTALIN with a user defined frequency | |
88 | * - Generate the pixel clock for the LCD monitor (instead of VCLK) | |
89 | */ | |
90 | ||
91 | /* | |
92 | * It can be quite hard to calculate XCLK and MCLK if they don't run at the | |
93 | * same frequency. Luckily, until now all cards that need asynchrone clock | |
94 | * speeds seem to have SCLK. | |
95 | * So this driver uses SCLK to clock the chip and XCLK to clock the memory. | |
96 | */ | |
97 | ||
98 | /* ------------------------------------------------------------------------- */ | |
99 | ||
100 | /* | |
101 | * PLL programming (Mach64 CT family) | |
102 | * | |
103 | * | |
104 | * This procedure sets the display fifo. The display fifo is a buffer that | |
105 | * contains data read from the video memory that waits to be processed by | |
106 | * the CRT controller. | |
107 | * | |
108 | * On the more modern Mach64 variants, the chip doesn't calculate the | |
109 | * interval after which the display fifo has to be reloaded from memory | |
110 | * automatically, the driver has to do it instead. | |
111 | */ | |
112 | ||
113 | #define Maximum_DSP_PRECISION 7 | |
114 | static u8 postdividers[] = {1,2,4,8,3}; | |
115 | ||
116 | static int aty_dsp_gt(const struct fb_info *info, u32 bpp, struct pll_ct *pll) | |
117 | { | |
118 | u32 dsp_off, dsp_on, dsp_xclks; | |
119 | u32 multiplier, divider, ras_multiplier, ras_divider, tmp; | |
120 | u8 vshift, xshift; | |
121 | s8 dsp_precision; | |
122 | ||
123 | multiplier = ((u32)pll->mclk_fb_div) * pll->vclk_post_div_real; | |
124 | divider = ((u32)pll->vclk_fb_div) * pll->xclk_ref_div; | |
125 | ||
126 | ras_multiplier = pll->xclkmaxrasdelay; | |
127 | ras_divider = 1; | |
128 | ||
129 | if (bpp>=8) | |
130 | divider = divider * (bpp >> 2); | |
131 | ||
132 | vshift = (6 - 2) - pll->xclk_post_div; /* FIFO is 64 bits wide in accelerator mode ... */ | |
133 | ||
134 | if (bpp == 0) | |
135 | vshift--; /* ... but only 32 bits in VGA mode. */ | |
136 | ||
137 | #ifdef CONFIG_FB_ATY_GENERIC_LCD | |
138 | if (pll->xres != 0) { | |
139 | struct atyfb_par *par = (struct atyfb_par *) info->par; | |
140 | ||
141 | multiplier = multiplier * par->lcd_width; | |
142 | divider = divider * pll->xres & ~7; | |
143 | ||
144 | ras_multiplier = ras_multiplier * par->lcd_width; | |
145 | ras_divider = ras_divider * pll->xres & ~7; | |
146 | } | |
147 | #endif | |
148 | /* If we don't do this, 32 bits for multiplier & divider won't be | |
149 | enough in certain situations! */ | |
150 | while (((multiplier | divider) & 1) == 0) { | |
151 | multiplier = multiplier >> 1; | |
152 | divider = divider >> 1; | |
153 | } | |
154 | ||
155 | /* Determine DSP precision first */ | |
156 | tmp = ((multiplier * pll->fifo_size) << vshift) / divider; | |
157 | ||
158 | for (dsp_precision = -5; tmp; dsp_precision++) | |
159 | tmp >>= 1; | |
160 | if (dsp_precision < 0) | |
161 | dsp_precision = 0; | |
162 | else if (dsp_precision > Maximum_DSP_PRECISION) | |
163 | dsp_precision = Maximum_DSP_PRECISION; | |
164 | ||
165 | xshift = 6 - dsp_precision; | |
166 | vshift += xshift; | |
167 | ||
168 | /* Move on to dsp_off */ | |
169 | dsp_off = ((multiplier * (pll->fifo_size - 1)) << vshift) / divider - | |
170 | (1 << (vshift - xshift)); | |
171 | ||
172 | /* if (bpp == 0) | |
173 | dsp_on = ((multiplier * 20 << vshift) + divider) / divider; | |
174 | else */ | |
175 | { | |
176 | dsp_on = ((multiplier << vshift) + divider) / divider; | |
177 | tmp = ((ras_multiplier << xshift) + ras_divider) / ras_divider; | |
178 | if (dsp_on < tmp) | |
179 | dsp_on = tmp; | |
180 | dsp_on = dsp_on + (tmp * 2) + (pll->xclkpagefaultdelay << xshift); | |
181 | } | |
182 | ||
183 | /* Calculate rounding factor and apply it to dsp_on */ | |
184 | tmp = ((1 << (Maximum_DSP_PRECISION - dsp_precision)) - 1) >> 1; | |
185 | dsp_on = ((dsp_on + tmp) / (tmp + 1)) * (tmp + 1); | |
186 | ||
187 | if (dsp_on >= ((dsp_off / (tmp + 1)) * (tmp + 1))) { | |
188 | dsp_on = dsp_off - (multiplier << vshift) / divider; | |
189 | dsp_on = (dsp_on / (tmp + 1)) * (tmp + 1); | |
190 | } | |
191 | ||
192 | /* Last but not least: dsp_xclks */ | |
193 | dsp_xclks = ((multiplier << (vshift + 5)) + divider) / divider; | |
194 | ||
195 | /* Get register values. */ | |
196 | pll->dsp_on_off = (dsp_on << 16) + dsp_off; | |
197 | pll->dsp_config = (dsp_precision << 20) | (pll->dsp_loop_latency << 16) | dsp_xclks; | |
198 | #ifdef DEBUG | |
199 | printk("atyfb(%s): dsp_config 0x%08x, dsp_on_off 0x%08x\n", | |
200 | __FUNCTION__, pll->dsp_config, pll->dsp_on_off); | |
201 | #endif | |
202 | return 0; | |
203 | } | |
204 | ||
205 | static int aty_valid_pll_ct(const struct fb_info *info, u32 vclk_per, struct pll_ct *pll) | |
206 | { | |
207 | u32 q; | |
208 | struct atyfb_par *par = (struct atyfb_par *) info->par; | |
1da177e4 | 209 | int pllvclk; |
1da177e4 LT |
210 | |
211 | /* FIXME: use the VTB/GTB /{3,6,12} post dividers if they're better suited */ | |
212 | q = par->ref_clk_per * pll->pll_ref_div * 4 / vclk_per; | |
213 | if (q < 16*8 || q > 255*8) { | |
214 | printk(KERN_CRIT "atyfb: vclk out of range\n"); | |
215 | return -EINVAL; | |
216 | } else { | |
217 | pll->vclk_post_div = (q < 128*8); | |
218 | pll->vclk_post_div += (q < 64*8); | |
219 | pll->vclk_post_div += (q < 32*8); | |
220 | } | |
221 | pll->vclk_post_div_real = postdividers[pll->vclk_post_div]; | |
222 | // pll->vclk_post_div <<= 6; | |
223 | pll->vclk_fb_div = q * pll->vclk_post_div_real / 8; | |
1da177e4 LT |
224 | pllvclk = (1000000 * 2 * pll->vclk_fb_div) / |
225 | (par->ref_clk_per * pll->pll_ref_div); |