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 #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 
aty_set_cursor_color(struct fb_info_aty * fb)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 
aty_set_cursor_shape(struct fb_info_aty * fb)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
aty_set_cursor(struct fb_info_aty * fb,int on)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                 /* In doublescan mode, the cursor location also needs to be
148 		   doubled. */
149                 if (par->crtc.gen_cntl & CRTC_DBL_SCAN_EN)
150 		   y<<=1;
151 		wait_for_fifo(4, fb);
152 		aty_st_le32(CUR_OFFSET, (c->offset >> 3) + (yoff << 1), fb);
153 		aty_st_le32(CUR_HORZ_VERT_OFF,
154 			    ((u32)(64 - c->size.y + yoff) << 16) | xoff, fb);
155 		aty_st_le32(CUR_HORZ_VERT_POSN, ((u32)y << 16) | x, fb);
156 		aty_st_le32(GEN_TEST_CNTL, aty_ld_le32(GEN_TEST_CNTL, fb)
157 						       | HWCURSOR_ENABLE, fb);
158 	} else {
159 		wait_for_fifo(1, fb);
160 		aty_st_le32(GEN_TEST_CNTL,
161 			    aty_ld_le32(GEN_TEST_CNTL, fb) & ~HWCURSOR_ENABLE,
162 			    fb);
163 	}
164 	if (fb->blitter_may_be_busy)
165 		wait_for_idle(fb);
166 }
167 
168 static void
aty_cursor_timer_handler(unsigned long dev_addr)169 aty_cursor_timer_handler(unsigned long dev_addr)
170 {
171 	struct fb_info_aty *fb = (struct fb_info_aty *)dev_addr;
172 
173 	if (!fb->cursor)
174 		return;
175 
176 	if (!fb->cursor->enable)
177 		goto out;
178 
179 	if (fb->cursor->vbl_cnt && --fb->cursor->vbl_cnt == 0) {
180 		fb->cursor->on ^= 1;
181 		aty_set_cursor(fb, fb->cursor->on);
182 		fb->cursor->vbl_cnt = fb->cursor->blink_rate;
183 	}
184 
185 out:
186 	fb->cursor->timer->expires = jiffies + (HZ / 50);
187 	add_timer(fb->cursor->timer);
188 }
189 
atyfb_cursor(struct display * p,int mode,int x,int y)190 void atyfb_cursor(struct display *p, int mode, int x, int y)
191 {
192 	struct fb_info_aty *fb = (struct fb_info_aty *)p->fb_info;
193 	struct aty_cursor *c = fb->cursor;
194 
195 	if (!c)
196 		return;
197 
198 #ifdef __sparc__
199 	if (fb->mmaped && (!fb->fb_info.display_fg
200 	    || fb->fb_info.display_fg->vc_num == fb->vtconsole))
201 		return;
202 #endif
203 
204 	x *= fontwidth(p);
205 	y *= fontheight(p);
206 	if (c->pos.x == x && c->pos.y == y && (mode == CM_ERASE) == !c->enable)
207 		return;
208 
209 	c->enable = 0;
210 	if (c->on)
211 		aty_set_cursor(fb, 0);
212 	c->pos.x = x;
213 	c->pos.y = y;
214 
215 	switch (mode) {
216 	case CM_ERASE:
217 		c->on = 0;
218 		break;
219 
220 	case CM_DRAW:
221 	case CM_MOVE:
222 		if (c->on)
223 			aty_set_cursor(fb, 1);
224 		else
225 			c->vbl_cnt = CURSOR_DRAW_DELAY;
226 		c->enable = 1;
227 		break;
228 	}
229 }
230 
aty_init_cursor(struct fb_info_aty * fb)231 struct aty_cursor * __init aty_init_cursor(struct fb_info_aty *fb)
232 {
233 	struct aty_cursor *cursor;
234 	unsigned long addr;
235 
236 	cursor = kmalloc(sizeof(struct aty_cursor), GFP_ATOMIC);
237 	if (!cursor)
238 		return 0;
239 	memset(cursor, 0, sizeof(*cursor));
240 
241 	cursor->timer = kmalloc(sizeof(*cursor->timer), GFP_KERNEL);
242 	if (!cursor->timer) {
243 		kfree(cursor);
244 		return 0;
245 	}
246 	memset(cursor->timer, 0, sizeof(*cursor->timer));
247 
248 	cursor->blink_rate = DEFAULT_CURSOR_BLINK_RATE;
249 	fb->total_vram -= PAGE_SIZE;
250 	cursor->offset = fb->total_vram;
251 
252 #ifdef __sparc__
253 	addr = fb->frame_buffer - 0x800000 + cursor->offset;
254 	cursor->ram = (u8 *)addr;
255 #else
256 #ifdef __BIG_ENDIAN
257 	addr = fb->frame_buffer_phys - 0x800000 + cursor->offset;
258 	cursor->ram = (u8 *)ioremap(addr, 1024);
259 #else
260 	addr = fb->frame_buffer + cursor->offset;
261 	cursor->ram = (u8 *)addr;
262 #endif
263 #endif
264 
265 	if (!cursor->ram) {
266 		kfree(cursor);
267 		return NULL;
268 	}
269 
270 	init_timer(cursor->timer);
271 	cursor->timer->expires = jiffies + (HZ / 50);
272 	cursor->timer->data = (unsigned long)fb;
273 	cursor->timer->function = aty_cursor_timer_handler;
274 	add_timer(cursor->timer);
275 
276 	return cursor;
277 }
278 
atyfb_set_font(struct display * d,int width,int height)279 int atyfb_set_font(struct display *d, int width, int height)
280 {
281     struct fb_info_aty *fb = (struct fb_info_aty *)d->fb_info;
282     struct aty_cursor *c = fb->cursor;
283     int i, j;
284 
285     if (c) {
286 	if (!width || !height) {
287 	    width = 8;
288 	    height = 16;
289 	}
290 
291 	c->hot.x = 0;
292 	c->hot.y = 0;
293 	c->size.x = width;
294 	c->size.y = height;
295 
296 	memset(c->bits, 0xff, sizeof(c->bits));
297 	memset(c->mask, 0, sizeof(c->mask));
298 
299 	for (i = 0, j = width; j >= 0; j -= 8, i++) {
300 	    c->mask[i][height-2] = (j >= 8) ? 0xff : (0xff << (8 - j));
301 	    c->mask[i][height-1] = (j >= 8) ? 0xff : (0xff << (8 - j));
302 	}
303 
304 	aty_set_cursor_color(fb);
305 	aty_set_cursor_shape(fb);
306     }
307     return 1;
308 }
309 
310