841424912ece3d98c0e22785c1e2afc2a208d3bc
[safe/jmp/linux-2.6] / drivers / video / mb862xx / mb862xxfb_accel.c
1 /*
2  * drivers/mb862xx/mb862xxfb_accel.c
3  *
4  * Fujitsu Carmine/Coral-P(A)/Lime framebuffer driver acceleration support
5  *
6  * (C) 2007 Alexander Shishkin <virtuoso@slind.org>
7  * (C) 2009 Valentin Sitdikov <valentin.sitdikov@siemens.com>
8  * (C) 2009 Siemens AG
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  */
15 #include <linux/fb.h>
16 #include <linux/delay.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/pci.h>
20 #include <linux/slab.h>
21 #if defined(CONFIG_OF)
22 #include <linux/of_platform.h>
23 #endif
24 #include "mb862xxfb.h"
25 #include "mb862xx_reg.h"
26 #include "mb862xxfb_accel.h"
27
28 static void mb862xxfb_write_fifo(u32 count, u32 *data, struct fb_info *info)
29 {
30         struct mb862xxfb_par *par = info->par;
31         static u32 free;
32
33         u32 total = 0;
34         while (total < count) {
35                 if (free) {
36                         outreg(geo, GDC_GEO_REG_INPUT_FIFO, data[total]);
37                         total++;
38                         free--;
39                 } else {
40                         free = (u32) inreg(draw, GDC_REG_FIFO_COUNT);
41                 }
42         }
43 }
44
45 static void mb86290fb_copyarea(struct fb_info *info,
46                                const struct fb_copyarea *area)
47 {
48         __u32 cmd[6];
49
50         cmd[0] = (GDC_TYPE_SETREGISTER << 24) | (1 << 16) | GDC_REG_MODE_BITMAP;
51         /* Set raster operation */
52         cmd[1] = (2 << 7) | (GDC_ROP_COPY << 9);
53         cmd[2] = GDC_TYPE_BLTCOPYP << 24;
54
55         if (area->sx >= area->dx && area->sy >= area->dy)
56                 cmd[2] |= GDC_CMD_BLTCOPY_TOP_LEFT << 16;
57         else if (area->sx >= area->dx && area->sy <= area->dy)
58                 cmd[2] |= GDC_CMD_BLTCOPY_BOTTOM_LEFT << 16;
59         else if (area->sx <= area->dx && area->sy >= area->dy)
60                 cmd[2] |= GDC_CMD_BLTCOPY_TOP_RIGHT << 16;
61         else
62                 cmd[2] |= GDC_CMD_BLTCOPY_BOTTOM_RIGHT << 16;
63
64         cmd[3] = (area->sy << 16) | area->sx;
65         cmd[4] = (area->dy << 16) | area->dx;
66         cmd[5] = (area->height << 16) | area->width;
67         mb862xxfb_write_fifo(6, cmd, info);
68 }
69
70 /*
71  * Fill in the cmd array /GDC FIFO commands/ to draw a 1bit image.
72  * Make sure cmd has enough room!
73  */
74 static void mb86290fb_imageblit1(u32 *cmd, u16 step, u16 dx, u16 dy,
75                                  u16 width, u16 height, u32 fgcolor,
76                                  u32 bgcolor, const struct fb_image *image,
77                                  struct fb_info *info)
78 {
79         int i;
80         unsigned const char *line;
81         u16 bytes;
82
83         /* set colors and raster operation regs */
84         cmd[0] = (GDC_TYPE_SETREGISTER << 24) | (1 << 16) | GDC_REG_MODE_BITMAP;
85         /* Set raster operation */
86         cmd[1] = (2 << 7) | (GDC_ROP_COPY << 9);
87         cmd[2] =
88             (GDC_TYPE_SETCOLORREGISTER << 24) | (GDC_CMD_BODY_FORE_COLOR << 16);
89         cmd[3] = fgcolor;
90         cmd[4] =
91             (GDC_TYPE_SETCOLORREGISTER << 24) | (GDC_CMD_BODY_BACK_COLOR << 16);
92         cmd[5] = bgcolor;
93
94         i = 0;
95         line = image->data;
96         bytes = (image->width + 7) >> 3;
97
98         /* and the image */
99         cmd[6] = (GDC_TYPE_DRAWBITMAPP << 24) |
100             (GDC_CMD_BITMAP << 16) | (2 + (step * height));
101         cmd[7] = (dy << 16) | dx;
102         cmd[8] = (height << 16) | width;
103
104         while (i < height) {
105                 memcpy(&cmd[9 + i * step], line, step << 2);
106 #ifdef __LITTLE_ENDIAN
107                 {
108                         int k = 0;
109                         for (k = 0; k < step; k++)
110                                 cmd[9 + i * step + k] =
111                                     cpu_to_be32(cmd[9 + i * step + k]);
112                 }
113 #endif
114                 line += bytes;
115                 i++;
116         }
117 }
118
119 /*
120  * Fill in the cmd array /GDC FIFO commands/ to draw a 8bit image.
121  * Make sure cmd has enough room!
122  */
123 static void mb86290fb_imageblit8(u32 *cmd, u16 step, u16 dx, u16 dy,
124                                  u16 width, u16 height, u32 fgcolor,
125                                  u32 bgcolor, const struct fb_image *image,
126                                  struct fb_info *info)
127 {
128         int i, j;
129         unsigned const char *line, *ptr;
130         u16 bytes;
131
132         cmd[0] = (GDC_TYPE_DRAWBITMAPP << 24) |
133             (GDC_CMD_BLT_DRAW << 16) | (2 + (height * step));
134         cmd[1] = (dy << 16) | dx;
135         cmd[2] = (height << 16) | width;
136
137         i = 0;
138         line = ptr = image->data;
139         bytes = image->width;
140
141         while (i < height) {
142                 ptr = line;
143                 for (j = 0; j < step; j++) {
144                         cmd[3 + i * step + j] =
145                             (((u32 *) (info->pseudo_palette))[*ptr]) & 0xffff;
146                         ptr++;
147                         cmd[3 + i * step + j] |=
148                             ((((u32 *) (info->
149                                         pseudo_palette))[*ptr]) & 0xffff) << 16;
150                         ptr++;
151                 }
152
153                 line += bytes;
154                 i++;
155         }
156 }
157
158 /*
159  * Fill in the cmd array /GDC FIFO commands/ to draw a 16bit image.
160  * Make sure cmd has enough room!
161  */
162 static void mb86290fb_imageblit16(u32 *cmd, u16 step, u16 dx, u16 dy,
163                                   u16 width, u16 height, u32 fgcolor,
164                                   u32 bgcolor, const struct fb_image *image,
165                                   struct fb_info *info)
166 {
167         int i;
168         unsigned const char *line;
169         u16 bytes;
170
171         i = 0;
172         line = image->data;
173         bytes = image->width << 1;
174
175         cmd[0] = (GDC_TYPE_DRAWBITMAPP << 24) |
176             (GDC_CMD_BLT_DRAW << 16) | (2 + step * height);
177         cmd[1] = (dy << 16) | dx;
178         cmd[2] = (height << 16) | width;
179
180         while (i < height) {
181                 memcpy(&cmd[3 + i * step], line, step);
182                 line += bytes;
183                 i++;
184         }
185 }
186
187 static void mb86290fb_imageblit(struct fb_info *info,
188                                 const struct fb_image *image)
189 {
190         int mdr;
191         u32 *cmd = NULL;
192         void (*cmdfn) (u32 *, u16, u16, u16, u16, u16, u32, u32,
193                        const struct fb_image *, struct fb_info *) = NULL;
194         u32 cmdlen;
195         u32 fgcolor = 0, bgcolor = 0;
196         u16 step;
197
198         u16 width = image->width, height = image->height;
199         u16 dx = image->dx, dy = image->dy;
200         int x2, y2, vxres, vyres;
201
202         mdr = (GDC_ROP_COPY << 9);
203         x2 = image->dx + image->width;
204         y2 = image->dy + image->height;
205         vxres = info->var.xres_virtual;
206         vyres = info->var.yres_virtual;
207         x2 = min(x2, vxres);
208         y2 = min(y2, vyres);
209         width = x2 - dx;
210         height = y2 - dy;
211
212         switch (image->depth) {
213         case 1:
214                 step = (width + 31) >> 5;
215                 cmdlen = 9 + height * step;
216                 cmdfn = mb86290fb_imageblit1;
217                 if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
218                     info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
219                         fgcolor =
220                             ((u32 *) (info->pseudo_palette))[image->fg_color];
221                         bgcolor =
222                             ((u32 *) (info->pseudo_palette))[image->bg_color];
223                 } else {
224                         fgcolor = image->fg_color;
225                         bgcolor = image->bg_color;
226                 }
227
228                 break;
229
230         case 8:
231                 step = (width + 1) >> 1;
232                 cmdlen = 3 + height * step;
233                 cmdfn = mb86290fb_imageblit8;
234                 break;
235
236         case 16:
237                 step = (width + 1) >> 1;
238                 cmdlen = 3 + height * step;
239                 cmdfn = mb86290fb_imageblit16;
240                 break;
241
242         default:
243                 cfb_imageblit(info, image);
244                 return;
245         }
246
247         cmd = kmalloc(cmdlen * 4, GFP_DMA);
248         if (!cmd)
249                 return cfb_imageblit(info, image);
250         cmdfn(cmd, step, dx, dy, width, height, fgcolor, bgcolor, image, info);
251         mb862xxfb_write_fifo(cmdlen, cmd, info);
252         kfree(cmd);
253 }
254
255 static void mb86290fb_fillrect(struct fb_info *info,
256                                const struct fb_fillrect *rect)
257 {
258
259         u32 x2, y2, vxres, vyres, height, width, fg;
260         u32 cmd[7];
261
262         vxres = info->var.xres_virtual;
263         vyres = info->var.yres_virtual;
264
265         if (!rect->width || !rect->height || rect->dx > vxres
266             || rect->dy > vyres)
267                 return;
268
269         /* We could use hardware clipping but on many cards you get around
270          * hardware clipping by writing to framebuffer directly. */
271         x2 = rect->dx + rect->width;
272         y2 = rect->dy + rect->height;
273         x2 = min(x2, vxres);
274         y2 = min(y2, vyres);
275         width = x2 - rect->dx;
276         height = y2 - rect->dy;
277         if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
278             info->fix.visual == FB_VISUAL_DIRECTCOLOR)
279                 fg = ((u32 *) (info->pseudo_palette))[rect->color];
280         else
281                 fg = rect->color;
282
283         switch (rect->rop) {
284
285         case ROP_XOR:
286                 /* Set raster operation */
287                 cmd[1] = (2 << 7) | (GDC_ROP_XOR << 9);
288                 break;
289
290         case ROP_COPY:
291                 /* Set raster operation */
292                 cmd[1] = (2 << 7) | (GDC_ROP_COPY << 9);
293                 break;
294
295         }
296
297         cmd[0] = (GDC_TYPE_SETREGISTER << 24) | (1 << 16) | GDC_REG_MODE_BITMAP;
298         /* cmd[1] set earlier */
299         cmd[2] =
300             (GDC_TYPE_SETCOLORREGISTER << 24) | (GDC_CMD_BODY_FORE_COLOR << 16);
301         cmd[3] = fg;
302         cmd[4] = (GDC_TYPE_DRAWRECTP << 24) | (GDC_CMD_BLT_FILL << 16);
303         cmd[5] = (rect->dy << 16) | (rect->dx);
304         cmd[6] = (height << 16) | width;
305
306         mb862xxfb_write_fifo(7, cmd, info);
307 }
308
309 void mb862xxfb_init_accel(struct fb_info *info, int xres)
310 {
311         struct mb862xxfb_par *par = info->par;
312
313         if (info->var.bits_per_pixel == 32) {
314                 info->fbops->fb_fillrect = cfb_fillrect;
315                 info->fbops->fb_copyarea = cfb_copyarea;
316                 info->fbops->fb_imageblit = cfb_imageblit;
317         } else {
318                 outreg(disp, GC_L0EM, 3);
319                 info->fbops->fb_fillrect = mb86290fb_fillrect;
320                 info->fbops->fb_copyarea = mb86290fb_copyarea;
321                 info->fbops->fb_imageblit = mb86290fb_imageblit;
322         }
323         outreg(draw, GDC_REG_DRAW_BASE, 0);
324         outreg(draw, GDC_REG_MODE_MISC, 0x8000);
325         outreg(draw, GDC_REG_X_RESOLUTION, xres);
326
327         info->flags |=
328             FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT |
329             FBINFO_HWACCEL_IMAGEBLIT;
330         info->fix.accel = 0xff; /*FIXME: add right define */
331 }
332 EXPORT_SYMBOL(mb862xxfb_init_accel);