18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  ATI Mach64 CT/VT/GT/LT Cursor Support
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/fb.h>
78c2ecf20Sopenharmony_ci#include <linux/init.h>
88c2ecf20Sopenharmony_ci#include <linux/string.h>
98c2ecf20Sopenharmony_ci#include "../core/fb_draw.h"
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <asm/io.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#ifdef __sparc__
148c2ecf20Sopenharmony_ci#include <asm/fbio.h>
158c2ecf20Sopenharmony_ci#endif
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <video/mach64.h>
188c2ecf20Sopenharmony_ci#include "atyfb.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*
218c2ecf20Sopenharmony_ci * The hardware cursor definition requires 2 bits per pixel. The
228c2ecf20Sopenharmony_ci * Cursor size reguardless of the visible cursor size is 64 pixels
238c2ecf20Sopenharmony_ci * by 64 lines. The total memory required to define the cursor is
248c2ecf20Sopenharmony_ci * 16 bytes / line for 64 lines or 1024 bytes of data. The data
258c2ecf20Sopenharmony_ci * must be in a contigiuos format. The 2 bit cursor code values are
268c2ecf20Sopenharmony_ci * as follows:
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci *	00 - pixel colour = CURSOR_CLR_0
298c2ecf20Sopenharmony_ci *	01 - pixel colour = CURSOR_CLR_1
308c2ecf20Sopenharmony_ci *	10 - pixel colour = transparent (current display pixel)
318c2ecf20Sopenharmony_ci *	11 - pixel colour = 1's complement of current display pixel
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci *	Cursor Offset        64 pixels		 Actual Displayed Area
348c2ecf20Sopenharmony_ci *            \_________________________/
358c2ecf20Sopenharmony_ci *	      |			|	|	|
368c2ecf20Sopenharmony_ci *	      |<--------------->|	|	|
378c2ecf20Sopenharmony_ci *	      | CURS_HORZ_OFFSET|	|	|
388c2ecf20Sopenharmony_ci *	      |			|_______|	|  64 Lines
398c2ecf20Sopenharmony_ci *	      |			   ^	|	|
408c2ecf20Sopenharmony_ci *	      |			   |	|	|
418c2ecf20Sopenharmony_ci *	      |		CURS_VERT_OFFSET|	|
428c2ecf20Sopenharmony_ci *	      |			   |	|	|
438c2ecf20Sopenharmony_ci *	      |____________________|____|	|
448c2ecf20Sopenharmony_ci *
458c2ecf20Sopenharmony_ci *
468c2ecf20Sopenharmony_ci * The Screen position of the top left corner of the displayed
478c2ecf20Sopenharmony_ci * cursor is specificed by CURS_HORZ_VERT_POSN. Care must be taken
488c2ecf20Sopenharmony_ci * when the cursor hot spot is not the top left corner and the
498c2ecf20Sopenharmony_ci * physical cursor position becomes negative. It will be be displayed
508c2ecf20Sopenharmony_ci * if either the horizontal or vertical cursor position is negative
518c2ecf20Sopenharmony_ci *
528c2ecf20Sopenharmony_ci * If x becomes negative the cursor manager must adjust the CURS_HORZ_OFFSET
538c2ecf20Sopenharmony_ci * to a larger number and saturate CUR_HORZ_POSN to zero.
548c2ecf20Sopenharmony_ci *
558c2ecf20Sopenharmony_ci * if Y becomes negative, CUR_VERT_OFFSET must be adjusted to a larger number,
568c2ecf20Sopenharmony_ci * CUR_OFFSET must be adjusted to a point to the appropriate line in the cursor
578c2ecf20Sopenharmony_ci * definitation and CUR_VERT_POSN must be saturated to zero.
588c2ecf20Sopenharmony_ci */
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci    /*
618c2ecf20Sopenharmony_ci     *  Hardware Cursor support.
628c2ecf20Sopenharmony_ci     */
638c2ecf20Sopenharmony_cistatic const u8 cursor_bits_lookup[16] = {
648c2ecf20Sopenharmony_ci	0x00, 0x40, 0x10, 0x50, 0x04, 0x44, 0x14, 0x54,
658c2ecf20Sopenharmony_ci	0x01, 0x41, 0x11, 0x51, 0x05, 0x45, 0x15, 0x55
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic int atyfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	struct atyfb_par *par = (struct atyfb_par *) info->par;
718c2ecf20Sopenharmony_ci	u16 xoff, yoff;
728c2ecf20Sopenharmony_ci	int x, y, h;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#ifdef __sparc__
758c2ecf20Sopenharmony_ci	if (par->mmaped)
768c2ecf20Sopenharmony_ci		return -EPERM;
778c2ecf20Sopenharmony_ci#endif
788c2ecf20Sopenharmony_ci	if (par->asleep)
798c2ecf20Sopenharmony_ci		return -EPERM;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	wait_for_fifo(1, par);
828c2ecf20Sopenharmony_ci	if (cursor->enable)
838c2ecf20Sopenharmony_ci		aty_st_le32(GEN_TEST_CNTL, aty_ld_le32(GEN_TEST_CNTL, par)
848c2ecf20Sopenharmony_ci			    | HWCURSOR_ENABLE, par);
858c2ecf20Sopenharmony_ci	else
868c2ecf20Sopenharmony_ci		aty_st_le32(GEN_TEST_CNTL, aty_ld_le32(GEN_TEST_CNTL, par)
878c2ecf20Sopenharmony_ci				& ~HWCURSOR_ENABLE, par);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	/* set position */
908c2ecf20Sopenharmony_ci	if (cursor->set & FB_CUR_SETPOS) {
918c2ecf20Sopenharmony_ci		x = cursor->image.dx - cursor->hot.x - info->var.xoffset;
928c2ecf20Sopenharmony_ci		if (x < 0) {
938c2ecf20Sopenharmony_ci			xoff = -x;
948c2ecf20Sopenharmony_ci			x = 0;
958c2ecf20Sopenharmony_ci		} else {
968c2ecf20Sopenharmony_ci			xoff = 0;
978c2ecf20Sopenharmony_ci		}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci		y = cursor->image.dy - cursor->hot.y - info->var.yoffset;
1008c2ecf20Sopenharmony_ci		if (y < 0) {
1018c2ecf20Sopenharmony_ci			yoff = -y;
1028c2ecf20Sopenharmony_ci			y = 0;
1038c2ecf20Sopenharmony_ci		} else {
1048c2ecf20Sopenharmony_ci			yoff = 0;
1058c2ecf20Sopenharmony_ci		}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci		h = cursor->image.height;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci		/*
1108c2ecf20Sopenharmony_ci		 * In doublescan mode, the cursor location
1118c2ecf20Sopenharmony_ci		 * and heigh also needs to be doubled.
1128c2ecf20Sopenharmony_ci		 */
1138c2ecf20Sopenharmony_ci                if (par->crtc.gen_cntl & CRTC_DBL_SCAN_EN) {
1148c2ecf20Sopenharmony_ci			y<<=1;
1158c2ecf20Sopenharmony_ci			h<<=1;
1168c2ecf20Sopenharmony_ci		}
1178c2ecf20Sopenharmony_ci		wait_for_fifo(3, par);
1188c2ecf20Sopenharmony_ci		aty_st_le32(CUR_OFFSET, (info->fix.smem_len >> 3) + (yoff << 1), par);
1198c2ecf20Sopenharmony_ci		aty_st_le32(CUR_HORZ_VERT_OFF,
1208c2ecf20Sopenharmony_ci			    ((u32) (64 - h + yoff) << 16) | xoff, par);
1218c2ecf20Sopenharmony_ci		aty_st_le32(CUR_HORZ_VERT_POSN, ((u32) y << 16) | x, par);
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	/* Set color map */
1258c2ecf20Sopenharmony_ci	if (cursor->set & FB_CUR_SETCMAP) {
1268c2ecf20Sopenharmony_ci		u32 fg_idx, bg_idx, fg, bg;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci		fg_idx = cursor->image.fg_color;
1298c2ecf20Sopenharmony_ci		bg_idx = cursor->image.bg_color;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci		fg = ((info->cmap.red[fg_idx] & 0xff) << 24) |
1328c2ecf20Sopenharmony_ci		     ((info->cmap.green[fg_idx] & 0xff) << 16) |
1338c2ecf20Sopenharmony_ci		     ((info->cmap.blue[fg_idx] & 0xff) << 8) | 0xff;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci		bg = ((info->cmap.red[bg_idx] & 0xff) << 24) |
1368c2ecf20Sopenharmony_ci		     ((info->cmap.green[bg_idx] & 0xff) << 16) |
1378c2ecf20Sopenharmony_ci		     ((info->cmap.blue[bg_idx] & 0xff) << 8);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci		wait_for_fifo(2, par);
1408c2ecf20Sopenharmony_ci		aty_st_le32(CUR_CLR0, bg, par);
1418c2ecf20Sopenharmony_ci		aty_st_le32(CUR_CLR1, fg, par);
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
1458c2ecf20Sopenharmony_ci	    u8 *src = (u8 *)cursor->image.data;
1468c2ecf20Sopenharmony_ci	    u8 *msk = (u8 *)cursor->mask;
1478c2ecf20Sopenharmony_ci	    u8 __iomem *dst = (u8 __iomem *)info->sprite.addr;
1488c2ecf20Sopenharmony_ci	    unsigned int width = (cursor->image.width + 7) >> 3;
1498c2ecf20Sopenharmony_ci	    unsigned int height = cursor->image.height;
1508c2ecf20Sopenharmony_ci	    unsigned int align = info->sprite.scan_align;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	    unsigned int i, j, offset;
1538c2ecf20Sopenharmony_ci	    u8 m, b;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	    // Clear cursor image with 1010101010...
1568c2ecf20Sopenharmony_ci	    fb_memset(dst, 0xaa, 1024);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	    offset = align - width*2;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	    for (i = 0; i < height; i++) {
1618c2ecf20Sopenharmony_ci		for (j = 0; j < width; j++) {
1628c2ecf20Sopenharmony_ci			u16 l = 0xaaaa;
1638c2ecf20Sopenharmony_ci			b = *src++;
1648c2ecf20Sopenharmony_ci			m = *msk++;
1658c2ecf20Sopenharmony_ci			switch (cursor->rop) {
1668c2ecf20Sopenharmony_ci			case ROP_XOR:
1678c2ecf20Sopenharmony_ci			    // Upper 4 bits of mask data
1688c2ecf20Sopenharmony_ci			    l = cursor_bits_lookup[(b ^ m) >> 4] |
1698c2ecf20Sopenharmony_ci			    // Lower 4 bits of mask
1708c2ecf20Sopenharmony_ci				    (cursor_bits_lookup[(b ^ m) & 0x0f] << 8);
1718c2ecf20Sopenharmony_ci			    break;
1728c2ecf20Sopenharmony_ci			case ROP_COPY:
1738c2ecf20Sopenharmony_ci			    // Upper 4 bits of mask data
1748c2ecf20Sopenharmony_ci			    l = cursor_bits_lookup[(b & m) >> 4] |
1758c2ecf20Sopenharmony_ci			    // Lower 4 bits of mask
1768c2ecf20Sopenharmony_ci				    (cursor_bits_lookup[(b & m) & 0x0f] << 8);
1778c2ecf20Sopenharmony_ci			    break;
1788c2ecf20Sopenharmony_ci			}
1798c2ecf20Sopenharmony_ci			/*
1808c2ecf20Sopenharmony_ci			 * If cursor size is not a multiple of 8 characters
1818c2ecf20Sopenharmony_ci			 * we must pad it with transparent pattern (0xaaaa).
1828c2ecf20Sopenharmony_ci			 */
1838c2ecf20Sopenharmony_ci			if ((j + 1) * 8 > cursor->image.width) {
1848c2ecf20Sopenharmony_ci				l = comp(l, 0xaaaa,
1858c2ecf20Sopenharmony_ci				    (1 << ((cursor->image.width & 7) * 2)) - 1);
1868c2ecf20Sopenharmony_ci			}
1878c2ecf20Sopenharmony_ci			fb_writeb(l & 0xff, dst++);
1888c2ecf20Sopenharmony_ci			fb_writeb(l >> 8, dst++);
1898c2ecf20Sopenharmony_ci		}
1908c2ecf20Sopenharmony_ci		dst += offset;
1918c2ecf20Sopenharmony_ci	    }
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	return 0;
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ciint aty_init_cursor(struct fb_info *info, struct fb_ops *atyfb_ops)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	unsigned long addr;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	info->fix.smem_len -= PAGE_SIZE;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci#ifdef __sparc__
2048c2ecf20Sopenharmony_ci	addr = (unsigned long) info->screen_base - 0x800000 + info->fix.smem_len;
2058c2ecf20Sopenharmony_ci	info->sprite.addr = (u8 *) addr;
2068c2ecf20Sopenharmony_ci#else
2078c2ecf20Sopenharmony_ci#ifdef __BIG_ENDIAN
2088c2ecf20Sopenharmony_ci	addr = info->fix.smem_start - 0x800000 + info->fix.smem_len;
2098c2ecf20Sopenharmony_ci	info->sprite.addr = (u8 *) ioremap(addr, 1024);
2108c2ecf20Sopenharmony_ci#else
2118c2ecf20Sopenharmony_ci	addr = (unsigned long) info->screen_base + info->fix.smem_len;
2128c2ecf20Sopenharmony_ci	info->sprite.addr = (u8 *) addr;
2138c2ecf20Sopenharmony_ci#endif
2148c2ecf20Sopenharmony_ci#endif
2158c2ecf20Sopenharmony_ci	if (!info->sprite.addr)
2168c2ecf20Sopenharmony_ci		return -ENXIO;
2178c2ecf20Sopenharmony_ci	info->sprite.size = PAGE_SIZE;
2188c2ecf20Sopenharmony_ci	info->sprite.scan_align = 16;	/* Scratch pad 64 bytes wide */
2198c2ecf20Sopenharmony_ci	info->sprite.buf_align = 16; 	/* and 64 lines tall. */
2208c2ecf20Sopenharmony_ci	info->sprite.flags = FB_PIXMAP_IO;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	atyfb_ops->fb_cursor = atyfb_cursor;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	return 0;
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ci
227