matroxfb: color setting fixes
[powerpc.git] / drivers / video / matrox / matroxfb_accel.c
1 /*
2  *
3  * Hardware accelerated Matrox Millennium I, II, Mystique, G100, G200 and G400
4  *
5  * (c) 1998-2002 Petr Vandrovec <vandrove@vc.cvut.cz>
6  *
7  * Version: 1.65 2002/08/14
8  *
9  * MTRR stuff: 1998 Tom Rini <trini@kernel.crashing.org>
10  *
11  * Contributors: "menion?" <menion@mindless.com>
12  *                     Betatesting, fixes, ideas
13  *
14  *               "Kurt Garloff" <garloff@suse.de>
15  *                     Betatesting, fixes, ideas, videomodes, videomodes timmings
16  *
17  *               "Tom Rini" <trini@kernel.crashing.org>
18  *                     MTRR stuff, PPC cleanups, betatesting, fixes, ideas
19  *
20  *               "Bibek Sahu" <scorpio@dodds.net>
21  *                     Access device through readb|w|l and write b|w|l
22  *                     Extensive debugging stuff
23  *
24  *               "Daniel Haun" <haund@usa.net>
25  *                     Testing, hardware cursor fixes
26  *
27  *               "Scott Wood" <sawst46+@pitt.edu>
28  *                     Fixes
29  *
30  *               "Gerd Knorr" <kraxel@goldbach.isdn.cs.tu-berlin.de>
31  *                     Betatesting
32  *
33  *               "Kelly French" <targon@hazmat.com>
34  *               "Fernando Herrera" <fherrera@eurielec.etsit.upm.es>
35  *                     Betatesting, bug reporting
36  *
37  *               "Pablo Bianucci" <pbian@pccp.com.ar>
38  *                     Fixes, ideas, betatesting
39  *
40  *               "Inaky Perez Gonzalez" <inaky@peloncho.fis.ucm.es>
41  *                     Fixes, enhandcements, ideas, betatesting
42  *
43  *               "Ryuichi Oikawa" <roikawa@rr.iiij4u.or.jp>
44  *                     PPC betatesting, PPC support, backward compatibility
45  *
46  *               "Paul Womar" <Paul@pwomar.demon.co.uk>
47  *               "Owen Waller" <O.Waller@ee.qub.ac.uk>
48  *                     PPC betatesting
49  *
50  *               "Thomas Pornin" <pornin@bolet.ens.fr>
51  *                     Alpha betatesting
52  *
53  *               "Pieter van Leuven" <pvl@iae.nl>
54  *               "Ulf Jaenicke-Roessler" <ujr@physik.phy.tu-dresden.de>
55  *                     G100 testing
56  *
57  *               "H. Peter Arvin" <hpa@transmeta.com>
58  *                     Ideas
59  *
60  *               "Cort Dougan" <cort@cs.nmt.edu>
61  *                     CHRP fixes and PReP cleanup
62  *
63  *               "Mark Vojkovich" <mvojkovi@ucsd.edu>
64  *                     G400 support
65  *
66  * (following author is not in any relation with this code, but his code
67  *  is included in this driver)
68  *
69  * Based on framebuffer driver for VBE 2.0 compliant graphic boards
70  *     (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
71  *
72  * (following author is not in any relation with this code, but his ideas
73  *  were used when writing this driver)
74  *
75  *               FreeVBE/AF (Matrox), "Shawn Hargreaves" <shawn@talula.demon.co.uk>
76  *
77  */
78
79 #include "matroxfb_accel.h"
80 #include "matroxfb_DAC1064.h"
81 #include "matroxfb_Ti3026.h"
82 #include "matroxfb_misc.h"
83
84 #define curr_ydstorg(x) ACCESS_FBINFO2(x, curr.ydstorg.pixels)
85
86 #define mga_ydstlen(y,l) mga_outl(M_YDSTLEN | M_EXEC, ((y) << 16) | (l))
87
88 static inline void matrox_cfb4_pal(u_int32_t* pal) {
89         unsigned int i;
90         
91         for (i = 0; i < 16; i++) {
92                 pal[i] = i * 0x11111111U;
93         }
94         pal[i] = 0xFFFFFFFF;
95 }
96
97 static inline void matrox_cfb8_pal(u_int32_t* pal) {
98         unsigned int i;
99         
100         for (i = 0; i < 16; i++) {
101                 pal[i] = i * 0x01010101U;
102         }
103         pal[i] = 0x0F0F0F0F;
104 }
105
106 static void matroxfb_copyarea(struct fb_info* info, const struct fb_copyarea* area);
107 static void matroxfb_fillrect(struct fb_info* info, const struct fb_fillrect* rect);
108 static void matroxfb_imageblit(struct fb_info* info, const struct fb_image* image);
109 static void matroxfb_cfb4_fillrect(struct fb_info* info, const struct fb_fillrect* rect);
110 static void matroxfb_cfb4_copyarea(struct fb_info* info, const struct fb_copyarea* area);
111
112 void matrox_cfbX_init(WPMINFO2) {
113         u_int32_t maccess;
114         u_int32_t mpitch;
115         u_int32_t mopmode;
116         int accel;
117
118         DBG(__FUNCTION__)
119
120         mpitch = ACCESS_FBINFO(fbcon).var.xres_virtual;
121
122         ACCESS_FBINFO(fbops).fb_copyarea = cfb_copyarea;
123         ACCESS_FBINFO(fbops).fb_fillrect = cfb_fillrect;
124         ACCESS_FBINFO(fbops).fb_imageblit = cfb_imageblit;
125         ACCESS_FBINFO(fbops).fb_cursor = NULL;
126
127         accel = (ACCESS_FBINFO(fbcon).var.accel_flags & FB_ACCELF_TEXT) == FB_ACCELF_TEXT;
128
129         switch (ACCESS_FBINFO(fbcon).var.bits_per_pixel) {
130                 case 4:         maccess = 0x00000000;   /* accelerate as 8bpp video */
131                                 mpitch = (mpitch >> 1) | 0x8000; /* disable linearization */
132                                 mopmode = M_OPMODE_4BPP;
133                                 matrox_cfb4_pal(ACCESS_FBINFO(cmap));
134                                 if (accel && !(mpitch & 1)) {
135                                         ACCESS_FBINFO(fbops).fb_copyarea = matroxfb_cfb4_copyarea;
136                                         ACCESS_FBINFO(fbops).fb_fillrect = matroxfb_cfb4_fillrect;
137                                 }
138                                 break;
139                 case 8:         maccess = 0x00000000;
140                                 mopmode = M_OPMODE_8BPP;
141                                 matrox_cfb8_pal(ACCESS_FBINFO(cmap));
142                                 if (accel) {
143                                         ACCESS_FBINFO(fbops).fb_copyarea = matroxfb_copyarea;
144                                         ACCESS_FBINFO(fbops).fb_fillrect = matroxfb_fillrect;
145                                         ACCESS_FBINFO(fbops).fb_imageblit = matroxfb_imageblit;
146                                 }
147                                 break;
148                 case 16:        if (ACCESS_FBINFO(fbcon).var.green.length == 5)
149                                         maccess = 0xC0000001;
150                                 else
151                                         maccess = 0x40000001;
152                                 mopmode = M_OPMODE_16BPP;
153                                 if (accel) {
154                                         ACCESS_FBINFO(fbops).fb_copyarea = matroxfb_copyarea;
155                                         ACCESS_FBINFO(fbops).fb_fillrect = matroxfb_fillrect;
156                                         ACCESS_FBINFO(fbops).fb_imageblit = matroxfb_imageblit;
157                                 }
158                                 break;
159                 case 24:        maccess = 0x00000003;
160                                 mopmode = M_OPMODE_24BPP;
161                                 if (accel) {
162                                         ACCESS_FBINFO(fbops).fb_copyarea = matroxfb_copyarea;
163                                         ACCESS_FBINFO(fbops).fb_fillrect = matroxfb_fillrect;
164                                         ACCESS_FBINFO(fbops).fb_imageblit = matroxfb_imageblit;
165                                 }
166                                 break;
167                 case 32:        maccess = 0x00000002;
168                                 mopmode = M_OPMODE_32BPP;
169                                 if (accel) {
170                                         ACCESS_FBINFO(fbops).fb_copyarea = matroxfb_copyarea;
171                                         ACCESS_FBINFO(fbops).fb_fillrect = matroxfb_fillrect;
172                                         ACCESS_FBINFO(fbops).fb_imageblit = matroxfb_imageblit;
173                                 }
174                                 break;
175                 default:        maccess = 0x00000000;
176                                 mopmode = 0x00000000;
177                                 break;  /* turn off acceleration!!! */
178         }
179         mga_fifo(8);
180         mga_outl(M_PITCH, mpitch);
181         mga_outl(M_YDSTORG, curr_ydstorg(MINFO));
182         if (ACCESS_FBINFO(capable.plnwt))
183                 mga_outl(M_PLNWT, -1);
184         if (ACCESS_FBINFO(capable.srcorg)) {
185                 mga_outl(M_SRCORG, 0);
186                 mga_outl(M_DSTORG, 0);
187         }
188         mga_outl(M_OPMODE, mopmode);
189         mga_outl(M_CXBNDRY, 0xFFFF0000);
190         mga_outl(M_YTOP, 0);
191         mga_outl(M_YBOT, 0x01FFFFFF);
192         mga_outl(M_MACCESS, maccess);
193         ACCESS_FBINFO(accel.m_dwg_rect) = M_DWG_TRAP | M_DWG_SOLID | M_DWG_ARZERO | M_DWG_SGNZERO | M_DWG_SHIFTZERO;
194         if (isMilleniumII(MINFO)) ACCESS_FBINFO(accel.m_dwg_rect) |= M_DWG_TRANSC;
195         ACCESS_FBINFO(accel.m_opmode) = mopmode;
196 }
197
198 EXPORT_SYMBOL(matrox_cfbX_init);
199
200 static void matrox_accel_bmove(WPMINFO int vxres, int sy, int sx, int dy, int dx, int height, int width) {
201         int start, end;
202         CRITFLAGS
203
204         DBG(__FUNCTION__)
205
206         CRITBEGIN
207
208         if ((dy < sy) || ((dy == sy) && (dx <= sx))) {
209                 mga_fifo(2);
210                 mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_SGNZERO |
211                          M_DWG_BFCOL | M_DWG_REPLACE);
212                 mga_outl(M_AR5, vxres);
213                 width--;
214                 start = sy*vxres+sx+curr_ydstorg(MINFO);
215                 end = start+width;
216         } else {
217                 mga_fifo(3);
218                 mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_BFCOL | M_DWG_REPLACE);
219                 mga_outl(M_SGN, 5);
220                 mga_outl(M_AR5, -vxres);
221                 width--;
222                 end = (sy+height-1)*vxres+sx+curr_ydstorg(MINFO);
223                 start = end+width;
224                 dy += height-1;
225         }
226         mga_fifo(4);
227         mga_outl(M_AR0, end);
228         mga_outl(M_AR3, start);
229         mga_outl(M_FXBNDRY, ((dx+width)<<16) | dx);
230         mga_ydstlen(dy, height);
231         WaitTillIdle();
232
233         CRITEND
234 }
235
236 static void matrox_accel_bmove_lin(WPMINFO int vxres, int sy, int sx, int dy, int dx, int height, int width) {
237         int start, end;
238         CRITFLAGS
239
240         DBG(__FUNCTION__)
241
242         CRITBEGIN
243
244         if ((dy < sy) || ((dy == sy) && (dx <= sx))) {
245                 mga_fifo(2);
246                 mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_SGNZERO |
247                         M_DWG_BFCOL | M_DWG_REPLACE);
248                 mga_outl(M_AR5, vxres);
249                 width--;
250                 start = sy*vxres+sx+curr_ydstorg(MINFO);
251                 end = start+width;
252         } else {
253                 mga_fifo(3);
254                 mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_BFCOL | M_DWG_REPLACE);
255                 mga_outl(M_SGN, 5);
256                 mga_outl(M_AR5, -vxres);
257                 width--;
258                 end = (sy+height-1)*vxres+sx+curr_ydstorg(MINFO);
259                 start = end+width;
260                 dy += height-1;
261         }
262         mga_fifo(5);
263         mga_outl(M_AR0, end);
264         mga_outl(M_AR3, start);
265         mga_outl(M_FXBNDRY, ((dx+width)<<16) | dx);
266         mga_outl(M_YDST, dy*vxres >> 5);
267         mga_outl(M_LEN | M_EXEC, height);
268         WaitTillIdle();
269
270         CRITEND
271 }
272
273 static void matroxfb_cfb4_copyarea(struct fb_info* info, const struct fb_copyarea* area) {
274         MINFO_FROM_INFO(info);
275
276         if ((area->sx | area->dx | area->width) & 1)
277                 cfb_copyarea(info, area);
278         else
279                 matrox_accel_bmove_lin(PMINFO ACCESS_FBINFO(fbcon.var.xres_virtual) >> 1, area->sy, area->sx >> 1, area->dy, area->dx >> 1, area->height, area->width >> 1);
280 }
281
282 static void matroxfb_copyarea(struct fb_info* info, const struct fb_copyarea* area) {
283         MINFO_FROM_INFO(info);
284
285         matrox_accel_bmove(PMINFO ACCESS_FBINFO(fbcon.var.xres_virtual), area->sy, area->sx, area->dy, area->dx, area->height, area->width);
286 }
287
288 static void matroxfb_accel_clear(WPMINFO u_int32_t color, int sy, int sx, int height,
289                 int width) {
290         CRITFLAGS
291
292         DBG(__FUNCTION__)
293
294         CRITBEGIN
295
296         mga_fifo(5);
297         mga_outl(M_DWGCTL, ACCESS_FBINFO(accel.m_dwg_rect) | M_DWG_REPLACE);
298         mga_outl(M_FCOL, color);
299         mga_outl(M_FXBNDRY, ((sx + width) << 16) | sx);
300         mga_ydstlen(sy, height);
301         WaitTillIdle();
302
303         CRITEND
304 }
305
306 static void matroxfb_fillrect(struct fb_info* info, const struct fb_fillrect* rect) {
307         MINFO_FROM_INFO(info);
308
309         switch (rect->rop) {
310                 case ROP_COPY:
311                         matroxfb_accel_clear(PMINFO ((u_int32_t*)info->pseudo_palette)[rect->color], rect->dy, rect->dx, rect->height, rect->width);
312                         break;
313         }
314 }
315
316 static void matroxfb_cfb4_clear(WPMINFO u_int32_t bgx, int sy, int sx, int height, int width) {
317         int whattodo;
318         CRITFLAGS
319
320         DBG(__FUNCTION__)
321
322         CRITBEGIN
323
324         whattodo = 0;
325         if (sx & 1) {
326                 sx ++;
327                 if (!width) return;
328                 width --;
329                 whattodo = 1;
330         }
331         if (width & 1) {
332                 whattodo |= 2;
333         }
334         width >>= 1;
335         sx >>= 1;
336         if (width) {
337                 mga_fifo(5);
338                 mga_outl(M_DWGCTL, ACCESS_FBINFO(accel.m_dwg_rect) | M_DWG_REPLACE2);
339                 mga_outl(M_FCOL, bgx);
340                 mga_outl(M_FXBNDRY, ((sx + width) << 16) | sx);
341                 mga_outl(M_YDST, sy * ACCESS_FBINFO(fbcon).var.xres_virtual >> 6);
342                 mga_outl(M_LEN | M_EXEC, height);
343                 WaitTillIdle();
344         }
345         if (whattodo) {
346                 u_int32_t step = ACCESS_FBINFO(fbcon).var.xres_virtual >> 1;
347                 vaddr_t vbase = ACCESS_FBINFO(video.vbase);
348                 if (whattodo & 1) {
349                         unsigned int uaddr = sy * step + sx - 1;
350                         u_int32_t loop;
351                         u_int8_t bgx2 = bgx & 0xF0;
352                         for (loop = height; loop > 0; loop --) {
353                                 mga_writeb(vbase, uaddr, (mga_readb(vbase, uaddr) & 0x0F) | bgx2);
354                                 uaddr += step;
355                         }
356                 }
357                 if (whattodo & 2) {
358                         unsigned int uaddr = sy * step + sx + width;
359                         u_int32_t loop;
360                         u_int8_t bgx2 = bgx & 0x0F;
361                         for (loop = height; loop > 0; loop --) {
362                                 mga_writeb(vbase, uaddr, (mga_readb(vbase, uaddr) & 0xF0) | bgx2);
363                                 uaddr += step;
364                         }
365                 }
366         }
367
368         CRITEND
369 }
370
371 static void matroxfb_cfb4_fillrect(struct fb_info* info, const struct fb_fillrect* rect) {
372         MINFO_FROM_INFO(info);
373
374         switch (rect->rop) {
375                 case ROP_COPY:
376                         matroxfb_cfb4_clear(PMINFO ((u_int32_t*)info->pseudo_palette)[rect->color], rect->dy, rect->dx, rect->height, rect->width);
377                         break;
378         }
379 }
380
381 static void matroxfb_1bpp_imageblit(WPMINFO u_int32_t fgx, u_int32_t bgx,
382                 const u_int8_t* chardata, int width, int height, int yy, int xx) {
383         u_int32_t step;
384         u_int32_t ydstlen;
385         u_int32_t xlen;
386         u_int32_t ar0;
387         u_int32_t charcell;
388         u_int32_t fxbndry;
389         vaddr_t mmio;
390         int easy;
391         CRITFLAGS
392
393         DBG_HEAVY(__FUNCTION__);
394
395         step = (width + 7) >> 3;
396         charcell = height * step;
397         xlen = (charcell + 3) & ~3;
398         ydstlen = (yy << 16) | height;
399         if (width == step << 3) {
400                 ar0 = height * width - 1;
401                 easy = 1;
402         } else {
403                 ar0 = width - 1;
404                 easy = 0;
405         }
406
407         CRITBEGIN
408
409         mga_fifo(3);
410         if (easy)
411                 mga_outl(M_DWGCTL, M_DWG_ILOAD | M_DWG_SGNZERO | M_DWG_SHIFTZERO | M_DWG_BMONOWF | M_DWG_LINEAR | M_DWG_REPLACE);
412         else
413                 mga_outl(M_DWGCTL, M_DWG_ILOAD | M_DWG_SGNZERO | M_DWG_SHIFTZERO | M_DWG_BMONOWF | M_DWG_REPLACE);
414         mga_outl(M_FCOL, fgx);
415         mga_outl(M_BCOL, bgx);
416         fxbndry = ((xx + width - 1) << 16) | xx;
417         mmio = ACCESS_FBINFO(mmio.vbase);
418
419         mga_fifo(6);
420         mga_writel(mmio, M_FXBNDRY, fxbndry);
421         mga_writel(mmio, M_AR0, ar0);
422         mga_writel(mmio, M_AR3, 0);
423         if (easy) {
424                 mga_writel(mmio, M_YDSTLEN | M_EXEC, ydstlen);
425                 mga_memcpy_toio(mmio, chardata, xlen);
426         } else {
427                 mga_writel(mmio, M_AR5, 0);
428                 mga_writel(mmio, M_YDSTLEN | M_EXEC, ydstlen);
429                 if ((step & 3) == 0) {
430                         /* Great. Source has 32bit aligned lines, so we can feed them
431                            directly to the accelerator. */
432                         mga_memcpy_toio(mmio, chardata, charcell);
433                 } else if (step == 1) {
434                         /* Special case for 1..8bit widths */
435                         while (height--) {
436 #if defined(__BIG_ENDIAN)
437                                 fb_writel((*chardata) << 24, mmio.vaddr);
438 #else
439                                 fb_writel(*chardata, mmio.vaddr);
440 #endif
441                                 chardata++;
442                         }
443                 } else if (step == 2) {
444                         /* Special case for 9..15bit widths */
445                         while (height--) {
446 #if defined(__BIG_ENDIAN)
447                                 fb_writel((*(u_int16_t*)chardata) << 16, mmio.vaddr);
448 #else
449                                 fb_writel(*(u_int16_t*)chardata, mmio.vaddr);
450 #endif
451                                 chardata += 2;
452                         }
453                 } else {
454                         /* Tell... well, why bother... */
455                         while (height--) {
456                                 size_t i;
457                                 
458                                 for (i = 0; i < step; i += 4) {
459                                         /* Hope that there are at least three readable bytes beyond the end of bitmap */
460                                         fb_writel(get_unaligned((u_int32_t*)(chardata + i)),mmio.vaddr);
461                                 }
462                                 chardata += step;
463                         }
464                 }
465         }
466         WaitTillIdle();
467         CRITEND
468 }
469
470
471 static void matroxfb_imageblit(struct fb_info* info, const struct fb_image* image) {
472         MINFO_FROM_INFO(info);
473
474         DBG_HEAVY(__FUNCTION__);
475
476         if (image->depth == 1) {
477                 u_int32_t fgx, bgx;
478
479                 fgx = ((u_int32_t*)info->pseudo_palette)[image->fg_color];
480                 bgx = ((u_int32_t*)info->pseudo_palette)[image->bg_color];
481                 matroxfb_1bpp_imageblit(PMINFO fgx, bgx, image->data, image->width, image->height, image->dy, image->dx);
482         } else {
483                 /* Danger! image->depth is useless: logo painting code always
484                    passes framebuffer color depth here, although logo data are
485                    always 8bpp and info->pseudo_palette is changed to contain
486                    logo palette to be used (but only for true/direct-color... sic...).
487                    So do it completely in software... */
488                 cfb_imageblit(info, image);
489         }
490 }
491
492 MODULE_LICENSE("GPL");