1/* $NetBSD: summit_cursor.c,v 1.1 2025/01/22 12:55:06 macallan Exp $ */
2/*
3 * Copyright (c) 2025 Michael Lorenz
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 *    - Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    - Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials provided
15 *      with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 *
30 */
31
32#include <sys/types.h>
33#include <dev/ic/summitreg.h>
34#include "ngle.h"
35
36static void SummitLoadCursorImage(ScrnInfoPtr pScrn, unsigned char *src);
37static void SummitSetCursorPosition(ScrnInfoPtr pScrn, int x, int y);
38static void SummitSetCursorColors(ScrnInfoPtr pScrn, int bg, int fg);
39
40static void
41SummitLoadCursorImage(ScrnInfoPtr pScrn, unsigned char *src)
42{
43	NGLEPtr pNGLE = NGLEPTR(pScrn);
44	uint32_t latch;
45	int i;
46
47	/* image first */
48	NGLEWrite4(pNGLE, VISFX_CURSOR_INDEX, 0x80);
49	for (i = 0; i < 128; i++) {
50		memcpy(&latch, src, 4);
51		NGLEWrite4(pNGLE, VISFX_CURSOR_DATA, latch);
52		src += 4;
53	}
54
55	/* and now the mask */
56	NGLEWrite4(pNGLE, VISFX_CURSOR_INDEX, 0);
57	for (i = 0; i < 128; i++) {
58		memcpy(&latch, src, 4);
59		NGLEWrite4(pNGLE, VISFX_CURSOR_DATA, latch);
60		src += 4;
61	}
62}
63
64void
65SummitShowCursor(ScrnInfoPtr pScrn)
66{
67	NGLEPtr pNGLE = NGLEPTR(pScrn);
68
69	pNGLE->cursor.enable = 1;
70	NGLEWrite4(pNGLE, VISFX_CURSOR_POS, pNGLE->creg | VISFX_CURSOR_ENABLE);
71}
72
73void
74SummitHideCursor(ScrnInfoPtr pScrn)
75{
76	NGLEPtr pNGLE = NGLEPTR(pScrn);
77
78	pNGLE->cursor.enable = 0;
79	NGLEWrite4(pNGLE, VISFX_CURSOR_POS, pNGLE->creg);
80}
81
82static void
83SummitSetCursorPosition(ScrnInfoPtr pScrn, int x, int y)
84{
85	NGLEPtr pNGLE = NGLEPTR(pScrn);
86
87	if (x < 0)
88		x = 0x1000 - x;
89	if (y < 0)
90		y = 0x1000 - y;
91	pNGLE->creg = (x << 16) | y;
92	NGLEWrite4(pNGLE, VISFX_CURSOR_POS, pNGLE->creg |
93	    (pNGLE->cursor.enable ? VISFX_CURSOR_ENABLE : 0));
94}
95
96static void
97SummitSetCursorColors(ScrnInfoPtr pScrn, int bg, int fg)
98{
99	NGLEPtr pNGLE = NGLEPTR(pScrn);
100
101	NGLEWrite4(pNGLE, VISFX_CURSOR_FG, fg);
102	NGLEWrite4(pNGLE, VISFX_CURSOR_BG, bg);
103}
104
105Bool
106SummitSetupCursor(ScreenPtr pScreen)
107{
108	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
109	NGLEPtr pNGLE = NGLEPTR(pScrn);
110	xf86CursorInfoPtr infoPtr;
111
112	pNGLE->cursor.enable = 0;
113	pNGLE->creg = 0;
114
115	infoPtr = xf86CreateCursorInfoRec();
116	if(!infoPtr) return FALSE;
117
118	pNGLE->CursorInfoRec = infoPtr;
119
120	xf86Msg(X_INFO, "Summit HW cursor enabled\n");
121
122	infoPtr->MaxWidth = 64;
123	infoPtr->MaxHeight = 64;
124	pNGLE->maskoffset = 8 * 64;
125
126	infoPtr->Flags = HARDWARE_CURSOR_AND_SOURCE_WITH_MASK |
127	    HARDWARE_CURSOR_TRUECOLOR_AT_8BPP;
128
129	infoPtr->SetCursorColors = SummitSetCursorColors;
130	infoPtr->SetCursorPosition = SummitSetCursorPosition;
131	infoPtr->LoadCursorImage = SummitLoadCursorImage;
132	infoPtr->HideCursor = SummitHideCursor;
133	infoPtr->ShowCursor = SummitShowCursor;
134	infoPtr->UseHWCursor = NULL;
135
136	return xf86InitCursor(pScreen, infoPtr);
137}
138