File: /usr/src/linux/drivers/video/aty/mach64_cursor.c

1     
2     /*
3      *  ATI Mach64 CT/VT/GT/LT Cursor Support
4      */
5     
6     #include <linux/malloc.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     #include <video/fbcon.h>
15     
16     #ifdef __sparc__
17     #include <asm/pbm.h>
18     #include <asm/fbio.h>
19     #endif
20     
21     #include "mach64.h"
22     #include "atyfb.h"
23     
24     
25     #define DEFAULT_CURSOR_BLINK_RATE	(20)
26     #define CURSOR_DRAW_DELAY		(2)
27     
28     
29         /*
30          *  Hardware Cursor support.
31          */
32     
33     static const u8 cursor_pixel_map[2] = { 0, 15 };
34     static const u8 cursor_color_map[2] = { 0, 0xff };
35     
36     static const u8 cursor_bits_lookup[16] =
37     {
38     	0x00, 0x40, 0x10, 0x50, 0x04, 0x44, 0x14, 0x54,
39     	0x01, 0x41, 0x11, 0x51, 0x05, 0x45, 0x15, 0x55
40     };
41     
42     static const u8 cursor_mask_lookup[16] =
43     {
44     	0xaa, 0x2a, 0x8a, 0x0a, 0xa2, 0x22, 0x82, 0x02,
45     	0xa8, 0x28, 0x88, 0x08, 0xa0, 0x20, 0x80, 0x00
46     };
47     
48     void aty_set_cursor_color(struct fb_info_aty *fb)
49     {
50     	struct aty_cursor *c = fb->cursor;
51     	const u8 *pixel = cursor_pixel_map;	/* ++Geert: Why?? */
52     	const u8 *red = cursor_color_map;
53     	const u8 *green = cursor_color_map;
54     	const u8 *blue = cursor_color_map;
55     	int i;
56     
57     	if (!c)
58     		return;
59     
60     #ifdef __sparc__
61     	if (fb->mmaped && (!fb->fb_info.display_fg
62     	    || fb->fb_info.display_fg->vc_num == fb->vtconsole))
63     		return;
64     #endif
65     
66     	for (i = 0; i < 2; i++) {
67     		c->color[i] =  (u32)red[i] << 24;
68     		c->color[i] |= (u32)green[i] << 16;
69      		c->color[i] |= (u32)blue[i] <<  8;
70      		c->color[i] |= (u32)pixel[i];
71     	}
72     
73     	wait_for_fifo(2, fb);
74     	aty_st_le32(CUR_CLR0, c->color[0], fb);
75     	aty_st_le32(CUR_CLR1, c->color[1], fb);
76     }
77     
78     void aty_set_cursor_shape(struct fb_info_aty *fb)
79     {
80     	struct aty_cursor *c = fb->cursor;
81     	u8 *ram, m, b;
82     	int x, y;
83     
84     	if (!c)
85     		return;
86     
87     #ifdef __sparc__
88     	if (fb->mmaped && (!fb->fb_info.display_fg
89     	    || fb->fb_info.display_fg->vc_num == fb->vtconsole))
90     		return;
91     #endif
92     
93     	ram = c->ram;
94     	for (y = 0; y < c->size.y; y++) {
95     		for (x = 0; x < c->size.x >> 2; x++) {
96     			m = c->mask[x][y];
97     			b = c->bits[x][y];
98     			fb_writeb (cursor_mask_lookup[m >> 4] |
99     				   cursor_bits_lookup[(b & m) >> 4],
100     				   ram++);
101     			fb_writeb (cursor_mask_lookup[m & 0x0f] |
102     				   cursor_bits_lookup[(b & m) & 0x0f],
103     				   ram++);
104     		}
105     		for ( ; x < 8; x++) {
106     			fb_writeb (0xaa, ram++);
107     			fb_writeb (0xaa, ram++);
108     		}
109     	}
110     	fb_memset (ram, 0xaa, (64 - c->size.y) * 16);
111     }
112     
113     static void
114     aty_set_cursor(struct fb_info_aty *fb, int on)
115     {
116     	struct atyfb_par *par = &fb->current_par;
117     	struct aty_cursor *c = fb->cursor;
118     	u16 xoff, yoff;
119     	int x, y;
120     
121     	if (!c)
122     		return;
123     
124     #ifdef __sparc__
125     	if (fb->mmaped && (!fb->fb_info.display_fg
126     	    || fb->fb_info.display_fg->vc_num == fb->vtconsole))
127     		return;
128     #endif
129     
130     	if (on) {
131     		x = c->pos.x - c->hot.x - par->crtc.xoffset;
132     		if (x < 0) {
133     			xoff = -x;
134     			x = 0;
135     		} else {
136     			xoff = 0;
137     		}
138     
139     		y = c->pos.y - c->hot.y - par->crtc.yoffset;
140     		if (y < 0) {
141     			yoff = -y;
142     			y = 0;
143     		} else {
144     			yoff = 0;
145     		}
146     
147     		wait_for_fifo(4, fb);
148     		aty_st_le32(CUR_OFFSET, (c->offset >> 3) + (yoff << 1), fb);
149     		aty_st_le32(CUR_HORZ_VERT_OFF,
150     			    ((u32)(64 - c->size.y + yoff) << 16) | xoff, fb);
151     		aty_st_le32(CUR_HORZ_VERT_POSN, ((u32)y << 16) | x, fb);
152     		aty_st_le32(GEN_TEST_CNTL, aty_ld_le32(GEN_TEST_CNTL, fb)
153     						       | HWCURSOR_ENABLE, fb);
154     	} else {
155     		wait_for_fifo(1, fb);
156     		aty_st_le32(GEN_TEST_CNTL,
157     			    aty_ld_le32(GEN_TEST_CNTL, fb) & ~HWCURSOR_ENABLE,
158     			    fb);
159     	}
160     	if (fb->blitter_may_be_busy)
161     		wait_for_idle(fb);
162     }
163     
164     static void
165     aty_cursor_timer_handler(unsigned long dev_addr)
166     {
167     	struct fb_info_aty *fb = (struct fb_info_aty *)dev_addr;
168     
169     	if (!fb->cursor)
170     		return;
171     
172     	if (!fb->cursor->enable)
173     		goto out;
174     
175     	if (fb->cursor->vbl_cnt && --fb->cursor->vbl_cnt == 0) {
176     		fb->cursor->on ^= 1;
177     		aty_set_cursor(fb, fb->cursor->on);
178     		fb->cursor->vbl_cnt = fb->cursor->blink_rate;
179     	}
180     
181     out:
182     	fb->cursor->timer->expires = jiffies + (HZ / 50);
183     	add_timer(fb->cursor->timer);
184     }
185     
186     void atyfb_cursor(struct display *p, int mode, int x, int y)
187     {
188     	struct fb_info_aty *fb = (struct fb_info_aty *)p->fb_info;
189     	struct aty_cursor *c = fb->cursor;
190     
191     	if (!c)
192     		return;
193     
194     #ifdef __sparc__
195     	if (fb->mmaped && (!fb->fb_info.display_fg
196     	    || fb->fb_info.display_fg->vc_num == fb->vtconsole))
197     		return;
198     #endif
199     
200     	x *= fontwidth(p);
201     	y *= fontheight(p);
202     	if (c->pos.x == x && c->pos.y == y && (mode == CM_ERASE) == !c->enable)
203     		return;
204     
205     	c->enable = 0;
206     	if (c->on)
207     		aty_set_cursor(fb, 0);
208     	c->pos.x = x;
209     	c->pos.y = y;
210     
211     	switch (mode) {
212     	case CM_ERASE:
213     		c->on = 0;
214     		break;
215     
216     	case CM_DRAW:
217     	case CM_MOVE:
218     		if (c->on)
219     			aty_set_cursor(fb, 1);
220     		else
221     			c->vbl_cnt = CURSOR_DRAW_DELAY;
222     		c->enable = 1;
223     		break;
224     	}
225     }
226     
227     struct aty_cursor * __init aty_init_cursor(struct fb_info_aty *fb)
228     {
229     	struct aty_cursor *cursor;
230     	unsigned long addr;
231     
232     	cursor = kmalloc(sizeof(struct aty_cursor), GFP_ATOMIC);
233     	if (!cursor)
234     		return 0;
235     	memset(cursor, 0, sizeof(*cursor));
236     
237     	cursor->timer = kmalloc(sizeof(*cursor->timer), GFP_KERNEL);
238     	if (!cursor->timer) {
239     		kfree(cursor);
240     		return 0;
241     	}
242     	memset(cursor->timer, 0, sizeof(*cursor->timer));
243     
244     	cursor->blink_rate = DEFAULT_CURSOR_BLINK_RATE;
245     	fb->total_vram -= PAGE_SIZE;
246     	cursor->offset = fb->total_vram;
247     
248     #ifdef __sparc__
249     	addr = fb->frame_buffer - 0x800000 + cursor->offset;
250     	cursor->ram = (u8 *)addr;
251     #else
252     #ifdef __BIG_ENDIAN
253     	addr = fb->frame_buffer_phys - 0x800000 + cursor->offset;
254     	cursor->ram = (u8 *)ioremap(addr, 1024);
255     #else
256     	addr = fb->frame_buffer + cursor->offset;
257     	cursor->ram = (u8 *)addr;
258     #endif
259     #endif
260     
261     	if (!cursor->ram) {
262     		kfree(cursor);
263     		return NULL;
264     	}
265     
266     	init_timer(cursor->timer);
267     	cursor->timer->expires = jiffies + (HZ / 50);
268     	cursor->timer->data = (unsigned long)fb;
269     	cursor->timer->function = aty_cursor_timer_handler;
270     	add_timer(cursor->timer);
271     
272     	return cursor;
273     }
274     
275     int atyfb_set_font(struct display *d, int width, int height)
276     {
277         struct fb_info_aty *fb = (struct fb_info_aty *)d->fb_info;
278         struct aty_cursor *c = fb->cursor;
279         int i, j;
280     
281         if (c) {
282     	if (!width || !height) {
283     	    width = 8;
284     	    height = 16;
285     	}
286     
287     	c->hot.x = 0;
288     	c->hot.y = 0;
289     	c->size.x = width;
290     	c->size.y = height;
291     
292     	memset(c->bits, 0xff, sizeof(c->bits));
293     	memset(c->mask, 0, sizeof(c->mask));
294     
295     	for (i = 0, j = width; j >= 0; j -= 8, i++) {
296     	    c->mask[i][height-2] = (j >= 8) ? 0xff : (0xff << (8 - j));
297     	    c->mask[i][height-1] = (j >= 8) ? 0xff : (0xff << (8 - j));
298     	}
299     
300     	aty_set_cursor_color(fb);
301     	aty_set_cursor_shape(fb);
302         }
303         return 1;
304     }
305     
306