ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / video / aty / mach64_accel.c
1
2 /*
3  *  ATI Mach64 Hardware Acceleration
4  */
5
6 #include <linux/sched.h>
7 #include <linux/delay.h>
8 #include <linux/fb.h>
9 #include <video/mach64.h>
10 #include "atyfb.h"
11
12     /*
13      *  Generic Mach64 routines
14      */
15
16 void aty_reset_engine(const struct atyfb_par *par)
17 {
18         /* reset engine */
19         aty_st_le32(GEN_TEST_CNTL,
20                     aty_ld_le32(GEN_TEST_CNTL, par) & ~GUI_ENGINE_ENABLE,
21                     par);
22         /* enable engine */
23         aty_st_le32(GEN_TEST_CNTL,
24                     aty_ld_le32(GEN_TEST_CNTL, par) | GUI_ENGINE_ENABLE,
25                     par);
26         /* ensure engine is not locked up by clearing any FIFO or */
27         /* HOST errors */
28         aty_st_le32(BUS_CNTL,
29                     aty_ld_le32(BUS_CNTL,
30                                 par) | BUS_HOST_ERR_ACK | BUS_FIFO_ERR_ACK,
31                     par);
32 }
33
34 static void reset_GTC_3D_engine(const struct atyfb_par *par)
35 {
36         aty_st_le32(SCALE_3D_CNTL, 0xc0, par);
37         mdelay(GTC_3D_RESET_DELAY);
38         aty_st_le32(SETUP_CNTL, 0x00, par);
39         mdelay(GTC_3D_RESET_DELAY);
40         aty_st_le32(SCALE_3D_CNTL, 0x00, par);
41         mdelay(GTC_3D_RESET_DELAY);
42 }
43
44 void aty_init_engine(struct atyfb_par *par, struct fb_info *info)
45 {
46         u32 pitch_value;
47
48         /* determine modal information from global mode structure */
49         pitch_value = info->var.xres_virtual;
50
51         if (info->var.bits_per_pixel == 24) {
52                 /* In 24 bpp, the engine is in 8 bpp - this requires that all */
53                 /* horizontal coordinates and widths must be adjusted */
54                 pitch_value = pitch_value * 3;
55         }
56
57         /* On GTC (RagePro), we need to reset the 3D engine before */
58         if (M64_HAS(RESET_3D))
59                 reset_GTC_3D_engine(par);
60
61         /* Reset engine, enable, and clear any engine errors */
62         aty_reset_engine(par);
63         /* Ensure that vga page pointers are set to zero - the upper */
64         /* page pointers are set to 1 to handle overflows in the */
65         /* lower page */
66         aty_st_le32(MEM_VGA_WP_SEL, 0x00010000, par);
67         aty_st_le32(MEM_VGA_RP_SEL, 0x00010000, par);
68
69         /* ---- Setup standard engine context ---- */
70
71         /* All GUI registers here are FIFOed - therefore, wait for */
72         /* the appropriate number of empty FIFO entries */
73         wait_for_fifo(14, par);
74
75         /* enable all registers to be loaded for context loads */
76         aty_st_le32(CONTEXT_MASK, 0xFFFFFFFF, par);
77
78         /* set destination pitch to modal pitch, set offset to zero */
79         aty_st_le32(DST_OFF_PITCH, (pitch_value / 8) << 22, par);
80
81         /* zero these registers (set them to a known state) */
82         aty_st_le32(DST_Y_X, 0, par);
83         aty_st_le32(DST_HEIGHT, 0, par);
84         aty_st_le32(DST_BRES_ERR, 0, par);
85         aty_st_le32(DST_BRES_INC, 0, par);
86         aty_st_le32(DST_BRES_DEC, 0, par);
87
88         /* set destination drawing attributes */
89         aty_st_le32(DST_CNTL, DST_LAST_PEL | DST_Y_TOP_TO_BOTTOM |
90                     DST_X_LEFT_TO_RIGHT, par);
91
92         /* set source pitch to modal pitch, set offset to zero */
93         aty_st_le32(SRC_OFF_PITCH, (pitch_value / 8) << 22, par);
94
95         /* set these registers to a known state */
96         aty_st_le32(SRC_Y_X, 0, par);
97         aty_st_le32(SRC_HEIGHT1_WIDTH1, 1, par);
98         aty_st_le32(SRC_Y_X_START, 0, par);
99         aty_st_le32(SRC_HEIGHT2_WIDTH2, 1, par);
100
101         /* set source pixel retrieving attributes */
102         aty_st_le32(SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT, par);
103
104         /* set host attributes */
105         wait_for_fifo(13, par);
106         aty_st_le32(HOST_CNTL, 0, par);
107
108         /* set pattern attributes */
109         aty_st_le32(PAT_REG0, 0, par);
110         aty_st_le32(PAT_REG1, 0, par);
111         aty_st_le32(PAT_CNTL, 0, par);
112
113         /* set scissors to modal size */
114         aty_st_le32(SC_LEFT, 0, par);
115         aty_st_le32(SC_TOP, 0, par);
116         aty_st_le32(SC_BOTTOM, par->crtc.vyres - 1, par);
117         aty_st_le32(SC_RIGHT, pitch_value - 1, par);
118
119         /* set background color to minimum value (usually BLACK) */
120         aty_st_le32(DP_BKGD_CLR, 0, par);
121
122         /* set foreground color to maximum value (usually WHITE) */
123         aty_st_le32(DP_FRGD_CLR, 0xFFFFFFFF, par);
124
125         /* set write mask to effect all pixel bits */
126         aty_st_le32(DP_WRITE_MASK, 0xFFFFFFFF, par);
127
128         /* set foreground mix to overpaint and background mix to */
129         /* no-effect */
130         aty_st_le32(DP_MIX, FRGD_MIX_S | BKGD_MIX_D, par);
131
132         /* set primary source pixel channel to foreground color */
133         /* register */
134         aty_st_le32(DP_SRC, FRGD_SRC_FRGD_CLR, par);
135
136         /* set compare functionality to false (no-effect on */
137         /* destination) */
138         wait_for_fifo(3, par);
139         aty_st_le32(CLR_CMP_CLR, 0, par);
140         aty_st_le32(CLR_CMP_MASK, 0xFFFFFFFF, par);
141         aty_st_le32(CLR_CMP_CNTL, 0, par);
142
143         /* set pixel depth */
144         wait_for_fifo(2, par);
145         aty_st_le32(DP_PIX_WIDTH, par->crtc.dp_pix_width, par);
146         aty_st_le32(DP_CHAIN_MASK, par->crtc.dp_chain_mask, par);
147
148         wait_for_fifo(5, par);
149         aty_st_le32(SCALE_3D_CNTL, 0, par);
150         aty_st_le32(Z_CNTL, 0, par);
151         aty_st_le32(CRTC_INT_CNTL, aty_ld_le32(CRTC_INT_CNTL, par) & ~0x20,
152                     par);
153         aty_st_le32(GUI_TRAJ_CNTL, 0x100023, par);
154
155         /* insure engine is idle before leaving */
156         wait_for_idle(par);
157 }
158
159     /*
160      *  Accelerated functions
161      */
162
163 static inline void draw_rect(s16 x, s16 y, u16 width, u16 height,
164                              struct atyfb_par *par)
165 {
166         /* perform rectangle fill */
167         wait_for_fifo(2, par);
168         aty_st_le32(DST_Y_X, (x << 16) | y, par);
169         aty_st_le32(DST_HEIGHT_WIDTH, (width << 16) | height, par);
170         par->blitter_may_be_busy = 1;
171 }
172
173 void atyfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
174 {
175         struct atyfb_par *par = (struct atyfb_par *) info->par;
176         u32 dy = area->dy, sy = area->sy, direction = DST_LAST_PEL;
177         u32 sx = area->sx, dx = area->dx, width = area->width;  
178         u32 pitch_value;
179
180         if (!area->width || !area->height)
181                 return;
182         if (!par->accel_flags) {
183                 if (par->blitter_may_be_busy)
184                         wait_for_idle(par);
185                 cfb_copyarea(info, area);
186                 return;
187         }
188
189         pitch_value = info->var.xres_virtual;
190         if (info->var.bits_per_pixel == 24) {
191                 /* In 24 bpp, the engine is in 8 bpp - this requires that all */
192                 /* horizontal coordinates and widths must be adjusted */
193                 pitch_value *= 3;
194                 sx *= 3;
195                 dx *= 3;
196                 width *= 3;
197         }
198
199         if (area->sy < area->dy) {
200                 dy += area->height - 1;
201                 sy += area->height - 1;
202         } else
203                 direction |= DST_Y_TOP_TO_BOTTOM;
204
205         if (sx < dx) {
206                 dx += width - 1;
207                 sx += width - 1;
208         } else
209                 direction |= DST_X_LEFT_TO_RIGHT;
210
211         wait_for_fifo(4, par);
212         aty_st_le32(DP_SRC, FRGD_SRC_BLIT, par);
213         aty_st_le32(SRC_Y_X, (sx << 16) | sy, par);
214         aty_st_le32(SRC_HEIGHT1_WIDTH1, (width << 16) | area->height, par);
215         aty_st_le32(DST_CNTL, direction, par);
216         draw_rect(dx, dy, width, area->height, par);
217 }
218
219 void atyfb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
220 {
221         struct atyfb_par *par = (struct atyfb_par *) info->par;
222         u32 color = rect->color, dx = rect->dx, width = rect->width;
223
224         if (!rect->width || !rect->height)
225                 return;
226         if (!par->accel_flags) {
227                 if (par->blitter_may_be_busy)
228                         wait_for_idle(par);
229                 cfb_fillrect(info, rect);
230                 return;
231         }
232
233         color |= (rect->color << 8);
234         color |= (rect->color << 16);
235
236         if (info->var.bits_per_pixel == 24) {
237                 /* In 24 bpp, the engine is in 8 bpp - this requires that all */
238                 /* horizontal coordinates and widths must be adjusted */
239                 dx *= 3;
240                 width *= 3;
241         }
242
243         wait_for_fifo(3, par);
244         aty_st_le32(DP_FRGD_CLR, color, par);
245         aty_st_le32(DP_SRC,
246                     BKGD_SRC_BKGD_CLR | FRGD_SRC_FRGD_CLR | MONO_SRC_ONE,
247                     par);
248         aty_st_le32(DST_CNTL,
249                     DST_LAST_PEL | DST_Y_TOP_TO_BOTTOM |
250                     DST_X_LEFT_TO_RIGHT, par);
251         draw_rect(dx, rect->dy, width, rect->height, par);
252 }
253
254 void atyfb_imageblit(struct fb_info *info, const struct fb_image *image)
255 {
256         struct atyfb_par *par = (struct atyfb_par *) info->par;
257     
258         if (par->blitter_may_be_busy)
259                 wait_for_idle(par);
260         cfb_imageblit(info, image);
261 }