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