1195534Sscottl/*
2195534Sscottl	Haiku ATI video driver adapted from the X.org ATI driver.
3195534Sscottl
4195534Sscottl	Copyright 1999, 2000 ATI Technologies Inc., Markham, Ontario,
5195534Sscottl						 Precision Insight, Inc., Cedar Park, Texas, and
6195534Sscottl						 VA Linux Systems Inc., Fremont, California.
7195534Sscottl
8195534Sscottl	Copyright 2009 Haiku, Inc.  All rights reserved.
9195534Sscottl	Distributed under the terms of the MIT license.
10195534Sscottl
11195534Sscottl	Authors:
12195534Sscottl	Gerald Zajac 2009
13195534Sscottl*/
14195534Sscottl
15195534Sscottl
16195534Sscottl#include "accelerant.h"
17195534Sscottl#include "rage128.h"
18195534Sscottl
19195534Sscottl
20195534Sscottl
21195534Sscottl
22195534Sscottlvoid
23195534SscottlRage128_EngineFlush()
24195534Sscottl{
25195534Sscottl	// Flush all dirty data in the Pixel Cache to memory.
26195534Sscottl
27195534Sscottl	OUTREGM(R128_PC_NGUI_CTLSTAT, R128_PC_FLUSH_ALL, R128_PC_FLUSH_ALL);
28195534Sscottl
29195534Sscottl	for (int i = 0; i < R128_TIMEOUT; i++) {
30195534Sscottl		if ( ! (INREG(R128_PC_NGUI_CTLSTAT) & R128_PC_BUSY))
31195534Sscottl			break;
32195534Sscottl	}
33195534Sscottl}
34195534Sscottl
35195534Sscottl
36195534Sscottlvoid
37195534SscottlRage128_EngineReset()
38195534Sscottl{
39195534Sscottl	// Reset graphics card to known state.
40195534Sscottl
41195534Sscottl	Rage128_EngineFlush();
42195534Sscottl
43195534Sscottl	uint32 clockCntlIndex = INREG(R128_CLOCK_CNTL_INDEX);
44195534Sscottl	uint32 mclkCntl = GetPLLReg(R128_MCLK_CNTL);
45195534Sscottl
46195534Sscottl	SetPLLReg(R128_MCLK_CNTL, mclkCntl | R128_FORCE_GCP | R128_FORCE_PIPE3D_CP);
47195534Sscottl
48195534Sscottl	uint32 genResetCntl = INREG(R128_GEN_RESET_CNTL);
49195534Sscottl
50195534Sscottl	OUTREG(R128_GEN_RESET_CNTL, genResetCntl | R128_SOFT_RESET_GUI);
51195534Sscottl	INREG(R128_GEN_RESET_CNTL);
52195534Sscottl	OUTREG(R128_GEN_RESET_CNTL, genResetCntl & ~R128_SOFT_RESET_GUI);
53195534Sscottl	INREG(R128_GEN_RESET_CNTL);
54195534Sscottl
55195534Sscottl	SetPLLReg(R128_MCLK_CNTL, mclkCntl);
56195534Sscottl	OUTREG(R128_CLOCK_CNTL_INDEX, clockCntlIndex);
57195534Sscottl	OUTREG(R128_GEN_RESET_CNTL, genResetCntl);
58195534Sscottl}
59195534Sscottl
60195534Sscottl
61195534Sscottlvoid
62195534SscottlRage128_EngineInit(const DisplayModeEx& mode)
63195534Sscottl{
64195534Sscottl	// Initialize the acceleration hardware.
65195534Sscottl
66195534Sscottl	SharedInfo& si = *gInfo.sharedInfo;
67195534Sscottl
68195534Sscottl	TRACE("Rage128_EngineInit()  bits/pixel: %d\n", mode.bitsPerPixel);
69195534Sscottl
70195534Sscottl	OUTREG(R128_SCALE_3D_CNTL, 0);
71195534Sscottl	Rage128_EngineReset();
72195534Sscottl
73195534Sscottl	uint32 dataType = 0;
74195534Sscottl
75195534Sscottl	switch (mode.bitsPerPixel) {
76195534Sscottl	case 8:
77195534Sscottl		dataType = 2;
78195534Sscottl		break;
79195534Sscottl	case 15:
80195534Sscottl		dataType = 3;
81195534Sscottl		break;
82195534Sscottl	case 16:
83195534Sscottl		dataType = 4;
84195534Sscottl		break;
85195534Sscottl	case 32:
86195534Sscottl		dataType = 6;
87195534Sscottl		break;
88195534Sscottl	default:
89195534Sscottl		TRACE("Unsupported color depth: %d bits/pixel\n", mode.bitsPerPixel);
90195534Sscottl	}
91195534Sscottl
92195534Sscottl	gInfo.WaitForFifo(2);
93195534Sscottl	OUTREG(R128_DEFAULT_OFFSET, gInfo.sharedInfo->frameBufferOffset);
94195534Sscottl	OUTREG(R128_DEFAULT_PITCH, mode.timing.h_display / 8);
95195534Sscottl
96195534Sscottl	gInfo.WaitForFifo(4);
97195534Sscottl	OUTREG(R128_AUX_SC_CNTL, 0);
98195534Sscottl	OUTREG(R128_DEFAULT_SC_BOTTOM_RIGHT, (R128_DEFAULT_SC_RIGHT_MAX
99195534Sscottl										| R128_DEFAULT_SC_BOTTOM_MAX));
100195534Sscottl	OUTREG(R128_SC_TOP_LEFT, 0);
101195534Sscottl	OUTREG(R128_SC_BOTTOM_RIGHT, (R128_DEFAULT_SC_RIGHT_MAX
102195534Sscottl								| R128_DEFAULT_SC_BOTTOM_MAX));
103195534Sscottl
104195534Sscottl	si.r128_dpGuiMasterCntl = ((dataType << R128_GMC_DST_DATATYPE_SHIFT)
105195534Sscottl								| R128_GMC_CLR_CMP_CNTL_DIS
106195534Sscottl								| R128_GMC_AUX_CLIP_DIS);
107195534Sscottl	gInfo.WaitForFifo(1);
108195534Sscottl	OUTREG(R128_DP_GUI_MASTER_CNTL, (si.r128_dpGuiMasterCntl
109195534Sscottl									| R128_GMC_BRUSH_SOLID_COLOR
110195534Sscottl									| R128_GMC_SRC_DATATYPE_COLOR));
111195534Sscottl
112195534Sscottl	gInfo.WaitForFifo(8);
113195534Sscottl	OUTREG(R128_DST_BRES_ERR, 0);
114195534Sscottl	OUTREG(R128_DST_BRES_INC, 0);
115195534Sscottl	OUTREG(R128_DST_BRES_DEC, 0);
116195534Sscottl	OUTREG(R128_DP_BRUSH_FRGD_CLR, 0xffffffff);
117195534Sscottl	OUTREG(R128_DP_BRUSH_BKGD_CLR, 0x00000000);
118195534Sscottl	OUTREG(R128_DP_SRC_FRGD_CLR, 0xffffffff);
119195534Sscottl	OUTREG(R128_DP_SRC_BKGD_CLR, 0x00000000);
120195534Sscottl	OUTREG(R128_DP_WRITE_MASK, 0xffffffff);
121195534Sscottl
122195534Sscottl	gInfo.WaitForFifo(1);
123195534Sscottl	OUTREGM(R128_DP_DATATYPE, 0, R128_HOST_BIG_ENDIAN_EN);
124195534Sscottl
125195534Sscottl	gInfo.WaitForIdle();
126195534Sscottl}
127195534Sscottl
128195534Sscottl
129195534Sscottlvoid
130195534SscottlRage128_FillRectangle(engine_token *et, uint32 color, fill_rect_params *pList, uint32 count)
131195534Sscottl{
132195534Sscottl	(void)et;		// avoid compiler warning for unused arg
133195534Sscottl
134195534Sscottl	gInfo.WaitForFifo(3);
135195534Sscottl	OUTREG(R128_DP_GUI_MASTER_CNTL, (gInfo.sharedInfo->r128_dpGuiMasterCntl
136195534Sscottl									| R128_GMC_BRUSH_SOLID_COLOR
137195534Sscottl									| R128_GMC_SRC_DATATYPE_COLOR
138195534Sscottl									| R128_ROP3_P));	// use GXcopy for rop
139195534Sscottl
140195534Sscottl	OUTREG(R128_DP_BRUSH_FRGD_CLR, color);
141195534Sscottl	OUTREG(R128_DP_CNTL, R128_DST_X_LEFT_TO_RIGHT | R128_DST_Y_TOP_TO_BOTTOM);
142195534Sscottl
143195534Sscottl	while (count--) {
144195534Sscottl		int x = pList->left;
145195534Sscottl		int y = pList->top;
146195534Sscottl		int w = pList->right - x + 1;
147195534Sscottl		int h = pList->bottom - y + 1;
148195534Sscottl
149195534Sscottl		gInfo.WaitForFifo(2);
150195534Sscottl		OUTREG(R128_DST_Y_X, (y << 16) | x);
151195534Sscottl		OUTREG(R128_DST_WIDTH_HEIGHT, (w << 16) | h);
152195534Sscottl
153195534Sscottl		pList++;
154195534Sscottl	}
155195534Sscottl}
156195534Sscottl
157195534Sscottl
158195534Sscottlvoid
159195534SscottlRage128_FillSpan(engine_token *et, uint32 color, uint16 *pList, uint32 count)
160195534Sscottl{
161195534Sscottl	(void)et;		// avoid compiler warning for unused arg
162195534Sscottl
163195534Sscottl	gInfo.WaitForFifo(2);
164195534Sscottl	OUTREG(R128_DP_GUI_MASTER_CNTL, (gInfo.sharedInfo->r128_dpGuiMasterCntl
165195534Sscottl									| R128_GMC_BRUSH_SOLID_COLOR
166195534Sscottl									| R128_GMC_SRC_DATATYPE_COLOR
167195534Sscottl									| R128_ROP3_P));	// use GXcopy for rop
168195534Sscottl
169195534Sscottl	OUTREG(R128_DP_BRUSH_FRGD_CLR, color);
170195534Sscottl
171195534Sscottl	while (count--) {
172195534Sscottl		int y = *pList++;
173195534Sscottl		int x = *pList++;
174195534Sscottl		int w = *pList++ - x + 1;
175195534Sscottl
176195534Sscottl		if (w <= 0)
177195534Sscottl			continue;	// discard span with zero or negative width
178195534Sscottl
179196656Smav		gInfo.WaitForFifo(2);
180196656Smav		OUTREG(R128_DST_Y_X, (y << 16) | x);
181196656Smav		OUTREG(R128_DST_WIDTH_HEIGHT, (w << 16) | 1);
182196656Smav	}
183196656Smav}
184196656Smav
185196656Smav
186196656Smavvoid
187196656SmavRage128_InvertRectangle(engine_token *et, fill_rect_params *pList, uint32 count)
188196656Smav{
189196656Smav	(void)et;		// avoid compiler warning for unused arg
190196656Smav
191196656Smav	gInfo.WaitForFifo(2);
192196656Smav	OUTREG(R128_DP_GUI_MASTER_CNTL, (gInfo.sharedInfo->r128_dpGuiMasterCntl
193196656Smav									| R128_GMC_BRUSH_NONE
194195534Sscottl									| R128_GMC_SRC_DATATYPE_COLOR
195195534Sscottl									| R128_DP_SRC_SOURCE_MEMORY
196195534Sscottl									| R128_ROP3_Dn));	// use GXinvert for rop
197195534Sscottl
198195534Sscottl	OUTREG(R128_DP_CNTL, R128_DST_X_LEFT_TO_RIGHT | R128_DST_Y_TOP_TO_BOTTOM);
199195534Sscottl
200195534Sscottl	while (count--) {
201195534Sscottl		int x = pList->left;
202195534Sscottl		int y = pList->top;
203195534Sscottl		int w = pList->right - x + 1;
204195534Sscottl		int h = pList->bottom - y + 1;
205195534Sscottl
206195534Sscottl		gInfo.WaitForFifo(2);
207195534Sscottl		OUTREG(R128_DST_Y_X, (y << 16) | x);
208195534Sscottl		OUTREG(R128_DST_WIDTH_HEIGHT, (w << 16) | h);
209195534Sscottl
210195534Sscottl		pList++;
211195534Sscottl	}
212195534Sscottl}
213195534Sscottl
214195534Sscottl
215195534Sscottlvoid
216195534SscottlRage128_ScreenToScreenBlit(engine_token *et, blit_params *pList, uint32 count)
217195534Sscottl{
218195534Sscottl	(void)et;		// avoid compiler warning for unused arg
219195534Sscottl
220195534Sscottl	gInfo.WaitForFifo(1);
221195534Sscottl	OUTREG(R128_DP_GUI_MASTER_CNTL, (gInfo.sharedInfo->r128_dpGuiMasterCntl
222195534Sscottl									| R128_GMC_BRUSH_NONE
223195534Sscottl									| R128_GMC_SRC_DATATYPE_COLOR
224195534Sscottl									| R128_DP_SRC_SOURCE_MEMORY
225195534Sscottl									| R128_ROP3_S));	// use GXcopy for rop
226195534Sscottl
227195534Sscottl	while (count--) {
228195534Sscottl		int cmd = 0;
229195534Sscottl		int src_x = pList->src_left;
230195534Sscottl		int src_y = pList->src_top;
231195534Sscottl		int dest_x = pList->dest_left;
232195534Sscottl		int dest_y = pList->dest_top;
233195534Sscottl		int width = pList->width;
234195534Sscottl		int height = pList->height;
235195534Sscottl
236195534Sscottl		if (dest_x <= src_x) {
237195534Sscottl			cmd |= R128_DST_X_LEFT_TO_RIGHT;
238195534Sscottl		} else {
239195534Sscottl			src_x += width;
240195534Sscottl			dest_x += width;
241195534Sscottl		}
242195534Sscottl
243195534Sscottl		if (dest_y <= src_y) {
244195534Sscottl			cmd |= R128_DST_Y_TOP_TO_BOTTOM;
245195534Sscottl		} else {
246195534Sscottl			src_y += height;
247195534Sscottl			dest_y += height;
248195534Sscottl		}
249195534Sscottl
250195534Sscottl		gInfo.WaitForFifo(4);
251195534Sscottl		OUTREG(R128_DP_CNTL, cmd);
252195534Sscottl		OUTREG(R128_SRC_Y_X, (src_y << 16) | src_x);
253195534Sscottl		OUTREG(R128_DST_Y_X, (dest_y << 16) | dest_x);
254195534Sscottl		OUTREG(R128_DST_HEIGHT_WIDTH, ((height + 1) << 16) | (width + 1));
255195534Sscottl
256195534Sscottl		pList ++;
257195534Sscottl	}
258195534Sscottl}
259195534Sscottl