Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[pandora-kernel.git] / drivers / video / aty / mach64_cursor.c
1 /*
2  *  ATI Mach64 CT/VT/GT/LT Cursor Support
3  */
4
5 #include <linux/slab.h>
6 #include <linux/fb.h>
7 #include <linux/init.h>
8 #include <linux/string.h>
9
10 #include <asm/io.h>
11 #include <asm/uaccess.h>
12
13 #ifdef __sparc__
14 #include <asm/pbm.h>
15 #include <asm/fbio.h>
16 #endif
17
18 #include <video/mach64.h>
19 #include "atyfb.h"
20
21 /*
22  * The hardware cursor definition requires 2 bits per pixel. The
23  * Cursor size reguardless of the visible cursor size is 64 pixels
24  * by 64 lines. The total memory required to define the cursor is
25  * 16 bytes / line for 64 lines or 1024 bytes of data. The data
26  * must be in a contigiuos format. The 2 bit cursor code values are
27  * as follows:
28  *
29  *      00 - pixel colour = CURSOR_CLR_0
30  *      01 - pixel colour = CURSOR_CLR_1
31  *      10 - pixel colour = transparent (current display pixel)
32  *      11 - pixel colour = 1's complement of current display pixel
33  *
34  *      Cursor Offset        64 pixels           Actual Displayed Area
35  *            \_________________________/
36  *            |                 |       |       |
37  *            |<--------------->|       |       |
38  *            | CURS_HORZ_OFFSET|       |       |
39  *            |                 |_______|       |  64 Lines
40  *            |                    ^    |       |
41  *            |                    |    |       |
42  *            |         CURS_VERT_OFFSET|       |
43  *            |                    |    |       |
44  *            |____________________|____|       |
45  *
46  *
47  * The Screen position of the top left corner of the displayed
48  * cursor is specificed by CURS_HORZ_VERT_POSN. Care must be taken
49  * when the cursor hot spot is not the top left corner and the
50  * physical cursor position becomes negative. It will be be displayed
51  * if either the horizontal or vertical cursor position is negative
52  *
53  * If x becomes negative the cursor manager must adjust the CURS_HORZ_OFFSET
54  * to a larger number and saturate CUR_HORZ_POSN to zero.
55  *
56  * if Y becomes negative, CUR_VERT_OFFSET must be adjusted to a larger number,
57  * CUR_OFFSET must be adjusted to a point to the appropraite line in the cursor
58  * definitation and CUR_VERT_POSN must be saturated to zero.
59  */
60
61     /*
62      *  Hardware Cursor support.
63      */
64 static const u8 cursor_bits_lookup[16] = {
65         0x00, 0x40, 0x10, 0x50, 0x04, 0x44, 0x14, 0x54,
66         0x01, 0x41, 0x11, 0x51, 0x05, 0x45, 0x15, 0x55
67 };
68
69 static int atyfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
70 {
71         struct atyfb_par *par = (struct atyfb_par *) info->par;
72         u16 xoff, yoff;
73         int x, y, h;
74
75 #ifdef __sparc__
76         if (par->mmaped)
77                 return -EPERM;
78 #endif
79         if (par->asleep)
80                 return -EPERM;
81
82         /* Hide cursor */
83         wait_for_fifo(1, par);
84         aty_st_le32(GEN_TEST_CNTL, aty_ld_le32(GEN_TEST_CNTL, par) & ~HWCURSOR_ENABLE, par);
85
86         /* set position */
87         if (cursor->set & FB_CUR_SETPOS) {
88                 x = cursor->image.dx - cursor->hot.x - info->var.xoffset;
89                 if (x < 0) {
90                         xoff = -x;
91                         x = 0;
92                 } else {
93                         xoff = 0;
94                 }
95
96                 y = cursor->image.dy - cursor->hot.y - info->var.yoffset;
97                 if (y < 0) {
98                         yoff = -y;
99                         y = 0;
100                 } else {
101                         yoff = 0;
102                 }
103
104                 h = cursor->image.height;
105
106                 /*
107                  * In doublescan mode, the cursor location
108                  * and heigh also needs to be doubled.
109                  */
110                 if (par->crtc.gen_cntl & CRTC_DBL_SCAN_EN) {
111                         y<<=1;
112                         h<<=1;
113                 }
114                 wait_for_fifo(4, par);
115                 aty_st_le32(CUR_OFFSET, (info->fix.smem_len >> 3) + (yoff << 1), par);
116                 aty_st_le32(CUR_HORZ_VERT_OFF,
117                             ((u32) (64 - h + yoff) << 16) | xoff, par);
118                 aty_st_le32(CUR_HORZ_VERT_POSN, ((u32) y << 16) | x, par);
119         }
120
121         /* Set color map */
122         if (cursor->set & FB_CUR_SETCMAP) {
123                 u32 fg_idx, bg_idx, fg, bg;
124
125                 fg_idx = cursor->image.fg_color;
126                 bg_idx = cursor->image.bg_color;
127
128                 fg = ((info->cmap.red[fg_idx] & 0xff) << 24) |
129                      ((info->cmap.green[fg_idx] & 0xff) << 16) |
130                      ((info->cmap.blue[fg_idx] & 0xff) << 8) | 0xff;
131
132                 bg = ((info->cmap.red[bg_idx] & 0xff) << 24) |
133                      ((info->cmap.green[bg_idx] & 0xff) << 16) |
134                      ((info->cmap.blue[bg_idx] & 0xff) << 8);
135
136                 wait_for_fifo(2, par);
137                 aty_st_le32(CUR_CLR0, bg, par);
138                 aty_st_le32(CUR_CLR1, fg, par);
139         }
140
141         if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
142             u8 *src = (u8 *)cursor->image.data;
143             u8 *msk = (u8 *)cursor->mask;
144             u8 __iomem *dst = (u8 __iomem *)info->sprite.addr;
145             unsigned int width = (cursor->image.width + 7) >> 3;
146             unsigned int height = cursor->image.height;
147             unsigned int align = info->sprite.scan_align;
148
149             unsigned int i, j, offset;
150             u8 m, b;
151
152             // Clear cursor image with 1010101010...
153             fb_memset(dst, 0xaa, 1024);
154
155             offset = align - width*2;
156
157             for (i = 0; i < height; i++) {
158                 for (j = 0; j < width; j++) {
159                         b = *src++;
160                         m = *msk++;
161                         switch (cursor->rop) {
162                         case ROP_XOR:
163                             // Upper 4 bits of mask data
164                             fb_writeb(cursor_bits_lookup[(b ^ m) >> 4], dst++);
165                             // Lower 4 bits of mask
166                             fb_writeb(cursor_bits_lookup[(b ^ m) & 0x0f],
167                                       dst++);
168                             break;
169                         case ROP_COPY:
170                             // Upper 4 bits of mask data
171                             fb_writeb(cursor_bits_lookup[(b & m) >> 4], dst++);
172                             // Lower 4 bits of mask
173                             fb_writeb(cursor_bits_lookup[(b & m) & 0x0f],
174                                       dst++);
175                             break;
176                         }
177                 }
178                 dst += offset;
179             }
180         }
181
182         if (cursor->enable) {
183                 wait_for_fifo(1, par);
184                 aty_st_le32(GEN_TEST_CNTL, aty_ld_le32(GEN_TEST_CNTL, par)
185                             | HWCURSOR_ENABLE, par);
186         }
187         return 0;
188 }
189
190 int __devinit aty_init_cursor(struct fb_info *info)
191 {
192         unsigned long addr;
193
194         info->fix.smem_len -= PAGE_SIZE;
195
196 #ifdef __sparc__
197         addr = (unsigned long) info->screen_base - 0x800000 + info->fix.smem_len;
198         info->sprite.addr = (u8 *) addr;
199 #else
200 #ifdef __BIG_ENDIAN
201         addr = info->fix.smem_start - 0x800000 + info->fix.smem_len;
202         info->sprite.addr = (u8 *) ioremap(addr, 1024);
203 #else
204         addr = (unsigned long) info->screen_base + info->fix.smem_len;
205         info->sprite.addr = (u8 *) addr;
206 #endif
207 #endif
208         if (!info->sprite.addr)
209                 return -ENXIO;
210         info->sprite.size = PAGE_SIZE;
211         info->sprite.scan_align = 16;   /* Scratch pad 64 bytes wide */
212         info->sprite.buf_align = 16;    /* and 64 lines tall. */
213         info->sprite.flags = FB_PIXMAP_IO;
214
215         info->fbops->fb_cursor = atyfb_cursor;
216
217         return 0;
218 }
219