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