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