ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / video / aty / mach64_cursor.c
1
2 /*
3  *  ATI Mach64 CT/VT/GT/LT Cursor Support
4  */
5
6 #include <linux/slab.h>
7 #include <linux/console.h>
8 #include <linux/fb.h>
9 #include <linux/init.h>
10
11 #include <asm/io.h>
12 #include <asm/uaccess.h>
13
14 #ifdef __sparc__
15 #include <asm/pbm.h>
16 #include <asm/fbio.h>
17 #endif
18
19 #include <video/mach64.h>
20 #include "atyfb.h"
21
22     /*
23      *  Hardware Cursor support.
24      */
25
26 static const u8 cursor_pixel_map[2] = { 0, 15 };
27 static const u8 cursor_color_map[2] = { 0, 0xff };
28
29 static const u8 cursor_bits_lookup[16] = {
30         0x00, 0x40, 0x10, 0x50, 0x04, 0x44, 0x14, 0x54,
31         0x01, 0x41, 0x11, 0x51, 0x05, 0x45, 0x15, 0x55
32 };
33
34 static const u8 cursor_mask_lookup[16] = {
35         0xaa, 0x2a, 0x8a, 0x0a, 0xa2, 0x22, 0x82, 0x02,
36         0xa8, 0x28, 0x88, 0x08, 0xa0, 0x20, 0x80, 0x00
37 };
38
39 void aty_set_cursor_color(struct fb_info *info)
40 {
41         struct atyfb_par *par = (struct atyfb_par *) info->par;
42         struct aty_cursor *c = par->cursor;
43         const u8 *pixel = cursor_pixel_map;     /* ++Geert: Why?? */
44         const u8 *red = cursor_color_map;
45         const u8 *green = cursor_color_map;
46         const u8 *blue = cursor_color_map;
47         u32 fg_color, bg_color;
48
49         if (!c)
50                 return;
51
52 #ifdef __sparc__
53         if (par->mmaped)
54                 return;
55 #endif
56         fg_color = (u32) red[0] << 24;
57         fg_color |= (u32) green[0] << 16;
58         fg_color |= (u32) blue[0] << 8;
59         fg_color |= (u32) pixel[0];
60
61         bg_color = (u32) red[1] << 24;
62         bg_color |= (u32) green[1] << 16;
63         bg_color |= (u32) blue[1] << 8;
64         bg_color |= (u32) pixel[1];
65
66         wait_for_fifo(2, par);
67         aty_st_le32(CUR_CLR0, fg_color, par);
68         aty_st_le32(CUR_CLR1, bg_color, par);
69 }
70
71 void aty_set_cursor_shape(struct fb_info *info)
72 {
73         struct atyfb_par *par = (struct atyfb_par *) info->par;
74         struct fb_cursor *cursor = &info->cursor;
75         struct aty_cursor *c = par->cursor;
76         u8 *ram, m, b;
77         int x, y;
78
79         if (!c)
80                 return;
81 #ifdef __sparc__
82         if (par->mmaped)
83                 return;
84 #endif
85
86         ram = c->ram;
87         for (y = 0; y < cursor->image.height; y++) {
88                 for (x = 0; x < cursor->image.width >> 2; x++) {
89                         m = c->mask[x][y];
90                         b = c->bits[x][y];
91                         fb_writeb(cursor_mask_lookup[m >> 4] |
92                                   cursor_bits_lookup[(b & m) >> 4], ram++);
93                         fb_writeb(cursor_mask_lookup[m & 0x0f] |
94                                   cursor_bits_lookup[(b & m) & 0x0f],
95                                   ram++);
96                 }
97                 for (; x < 8; x++) {
98                         fb_writeb(0xaa, ram++);
99                         fb_writeb(0xaa, ram++);
100                 }
101         }
102         fb_memset(ram, 0xaa, (64 - cursor->image.height) * 16);
103 }
104
105 static void aty_set_cursor(struct fb_info *info)
106 {
107         struct atyfb_par *par = (struct atyfb_par *) info->par;
108         struct fb_cursor *cursor = &info->cursor;
109         struct aty_cursor *c = par->cursor;
110         u16 xoff, yoff;
111         int x, y;
112
113         if (!c)
114                 return;
115
116 #ifdef __sparc__
117         if (par->mmaped)
118                 return;
119 #endif
120
121         if (cursor->enable) {
122                 x = cursor->image.dx - cursor->hot.x - info->var.xoffset;
123                 if (x < 0) {
124                         xoff = -x;
125                         x = 0;
126                 } else {
127                         xoff = 0;
128                 }
129
130                 y = cursor->image.dy - cursor->hot.y - info->var.yoffset;
131                 if (y < 0) {
132                         yoff = -y;
133                         y = 0;
134                 } else {
135                         yoff = 0;
136                 }
137
138                 wait_for_fifo(4, par);
139                 aty_st_le32(CUR_OFFSET, (info->fix.smem_len >> 3) + (yoff << 1),
140                             par);
141                 aty_st_le32(CUR_HORZ_VERT_OFF,
142                             ((u32) (64 - cursor->image.height + yoff) << 16) | xoff,
143                             par);
144                 aty_st_le32(CUR_HORZ_VERT_POSN, ((u32) y << 16) | x, par);
145                 aty_st_le32(GEN_TEST_CNTL, aty_ld_le32(GEN_TEST_CNTL, par)
146                             | HWCURSOR_ENABLE, par);
147         } else {
148                 wait_for_fifo(1, par);
149                 aty_st_le32(GEN_TEST_CNTL,
150                             aty_ld_le32(GEN_TEST_CNTL,
151                                         par) & ~HWCURSOR_ENABLE, par);
152         }
153         if (par->blitter_may_be_busy)
154                 wait_for_idle(par);
155 }
156
157 int atyfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
158 {
159         struct atyfb_par *par = (struct atyfb_par *) info->par;
160         struct aty_cursor *c = par->cursor;
161
162         if (!c)
163                 return -1;
164
165 #ifdef __sparc__
166         if (par->mmaped)
167                 return 0;
168 #endif
169
170         aty_set_cursor(info);
171         cursor->image.dx = info->cursor.image.dx;
172         cursor->image.dy = info->cursor.image.dy;
173
174         aty_set_cursor(info);
175         return 0;
176 }
177
178 struct aty_cursor *__init aty_init_cursor(struct fb_info *info)
179 {
180         struct aty_cursor *cursor;
181         unsigned long addr;
182
183         cursor = kmalloc(sizeof(struct aty_cursor), GFP_ATOMIC);
184         if (!cursor)
185                 return 0;
186         memset(cursor, 0, sizeof(*cursor));
187
188         info->fix.smem_len -= PAGE_SIZE;
189
190 #ifdef __sparc__
191         addr = (unsigned long) info->screen_base - 0x800000 + info->fix.smem_len;
192         cursor->ram = (u8 *) addr;
193 #else
194 #ifdef __BIG_ENDIAN
195         addr = info->fix.smem_start - 0x800000 + info->fix.smem_len;
196         cursor->ram = (u8 *) ioremap(addr, 1024);
197 #else
198         addr = (unsigned long) info->screen_base + info->fix.smem_len;
199         cursor->ram = (u8 *) addr;
200 #endif
201 #endif
202         if (!cursor->ram) {
203                 kfree(cursor);
204                 return NULL;
205         }
206         return cursor;
207 }
208
209 int atyfb_set_font(struct fb_info *info, int width, int height)
210 {
211         struct atyfb_par *par = (struct atyfb_par *) info->par;
212         struct fb_cursor *cursor = &info->cursor;
213         struct aty_cursor *c = par->cursor;
214         int i, j;
215
216         if (c) {
217                 if (!width || !height) {
218                         width = 8;
219                         height = 16;
220                 }
221
222                 cursor->hot.x = 0;
223                 cursor->hot.y = 0;
224                 cursor->image.width = width;
225                 cursor->image.height = height;
226
227                 memset(c->bits, 0xff, sizeof(c->bits));
228                 memset(c->mask, 0, sizeof(c->mask));
229
230                 for (i = 0, j = width; j >= 0; j -= 8, i++) {
231                         c->mask[i][height - 2] =
232                             (j >= 8) ? 0xff : (0xff << (8 - j));
233                         c->mask[i][height - 1] =
234                             (j >= 8) ? 0xff : (0xff << (8 - j));
235                 }
236
237                 aty_set_cursor_color(info);
238                 aty_set_cursor_shape(info);
239         }
240         return 1;
241 }