summaryrefslogtreecommitdiff
path: root/src/nv50_sor.c
blob: ea18e77c44ceea753ec2bfc65f8e3a1aaf1252f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * Copyright 2007 NVIDIA, Corporation
 * Copyright 2008 Maarten Maathuis
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "nouveau_modeset.h"
#include "nouveau_crtc.h"
#include "nouveau_output.h"
#include "nouveau_connector.h"

static int
NV50SorModeValid(nouveauOutputPtr output, DisplayModePtr mode)
{
	int high_limit;

	if (output->type == OUTPUT_LVDS)
		high_limit = 400000;
	else
		high_limit = 165000; /* no dual link dvi until we figure it out completely */

	if (mode->Clock > high_limit)
		return MODE_CLOCK_HIGH;

	if (mode->Clock < 25000)
		return MODE_CLOCK_LOW;

	if (mode->Flags & V_DBLSCAN)
		return MODE_NO_DBLESCAN;

	if (mode->HDisplay > output->native_mode->HDisplay || mode->VDisplay > output->native_mode->VDisplay)
		return MODE_PANEL;

	return MODE_OK;
}

static void
NV50SorModeSet(nouveauOutputPtr output, DisplayModePtr mode)
{
	ScrnInfoPtr pScrn = output->scrn;
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "NV50SorModeSet is called.\n");

	const int sorOff = 0x40 * NV50OrOffset(output);
	uint32_t mode_ctl = NV50_SOR_MODE_CTRL_OFF;

	if (!mode) {
		/* Disconnect the SOR */
		xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Disconnecting SOR.\n");
		NV50DisplayCommand(pScrn, NV50_SOR0_MODE_CTRL + sorOff, mode_ctl);
		return;
	}

	/* Anyone know a more appropriate name? */
	DisplayModePtr desired_mode = output->crtc->use_native_mode ? output->crtc->native_mode : mode;

	if (output->type == OUTPUT_LVDS) {
		mode_ctl |= NV50_SOR_MODE_CTRL_LVDS;
	} else {
		mode_ctl |= NV50_SOR_MODE_CTRL_TMDS;
		if (desired_mode->Clock > 165000)
			mode_ctl |= NV50_SOR_MODE_CTRL_TMDS_DUAL_LINK;
	}

	if (output->crtc) {
		if (output->crtc->index == 1)
			mode_ctl |= NV50_SOR_MODE_CTRL_CRTC1;
		else
			mode_ctl |= NV50_SOR_MODE_CTRL_CRTC0;
	} else {
		xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "Warning, output has no crtc.\n");
		return;
	}

	if (desired_mode->Flags & V_NHSYNC)
		mode_ctl |= NV50_SOR_MODE_CTRL_NHSYNC;

	if (desired_mode->Flags & V_NVSYNC)
		mode_ctl |= NV50_SOR_MODE_CTRL_NVSYNC;

	// This wouldn't be necessary, but the server is stupid and calls
	// nv50_sor_dpms after the output is disconnected, even though the hardware
	// turns it off automatically.
	output->SetPowerMode(output, DPMSModeOn);

	NV50DisplayCommand(pScrn, NV50_SOR0_MODE_CTRL + sorOff, mode_ctl);

	output->crtc->SetScaleMode(output->crtc, output->scale_mode);
}

static void
NV50SorSetClockMode(nouveauOutputPtr output, int clock)
{
	ScrnInfoPtr pScrn = output->scrn;
	NVPtr pNv = NVPTR(pScrn);
	const int limit = output->dcb->type == OUTPUT_LVDS ? 112000 : 165000;

	xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		   "NV50SorSetClockMode is called.\n");

#if 0
	ret = nouveau_bios_run_display_table(pScrn, output->dcb, clock);
	if (ret)
		NV_WARN(pScrn, "failed to run output script, may hang!\n");
#else
	/* just to keep the original behaviour until we can reliably run
	 * the output scripts...
	 */
	if (output->dcb->type == OUTPUT_LVDS)
		return;
#endif

	/* 0x70000 was a late addition to nv, mentioned as fixing tmds
	 * initialisation on certain gpu's. */
	/* I presume it's some kind of clock setting, but what precisely i
	 * do not know.
	 */
	NVWrite(pNv, NV50_SOR0_CLK_CTRL2 + NV50OrOffset(output) * 0x800,
		     0x70000 | ((clock > limit) ? 0x101 : 0));
}

static int
NV50SorSense(nouveauOutputPtr output)
{
	switch (output->type) {
		case OUTPUT_TMDS:
		case OUTPUT_LVDS:
			return output->type;
		default:
			return OUTPUT_NONE;
	}
}

static void
NV50SorSetPowerMode(nouveauOutputPtr output, int mode)
{
	ScrnInfoPtr pScrn = output->scrn;
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "NV50SorSetPowerMode is called with mode %d.\n", mode);

	NVPtr pNv = NVPTR(pScrn);
	uint32_t tmp;

	while ((NVRead(pNv, NV50_SOR0_DPMS_CTRL + NV50OrOffset(output) * 0x800) & NV50_SOR_DPMS_CTRL_PENDING));

	tmp = NVRead(pNv, NV50_SOR0_DPMS_CTRL + NV50OrOffset(output) * 0x800);
	tmp |= NV50_SOR_DPMS_CTRL_PENDING;

	if (mode == DPMSModeOn)
		tmp |= NV50_SOR_DPMS_CTRL_MODE_ON;
	else
		tmp &= ~NV50_SOR_DPMS_CTRL_MODE_ON;

	NVWrite(pNv, NV50_SOR0_DPMS_CTRL + NV50OrOffset(output) * 0x800, tmp);
	while ((NVRead(pNv, NV50_SOR0_DPMS_STATE + NV50OrOffset(output) * 0x800) & NV50_SOR_DPMS_STATE_WAIT));
}

static Bool
NV50SorDetect(nouveauOutputPtr output)
{
	if (output->type == OUTPUT_LVDS) /* assume connected */
		return TRUE;

	return FALSE;
}

static nouveauCrtcPtr
NV50SorGetCurrentCrtc(nouveauOutputPtr output)
{
	ScrnInfoPtr pScrn = output->scrn;
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "NV50SorGetCurrentCrtc is called.\n");

	NVPtr pNv = NVPTR(pScrn);
	uint32_t mode_ctrl = NVRead(pNv, NV50_SOR0_MODE_CTRL_VAL + NV50OrOffset(output) * 0x8);

	/* 
	 * MODE_CTRL values only contain one instance of crtc0 and of crtc1.
	 * This is because we disconnect outputs upon modeset.
	 * Crtc might be off even if we get a positive return.
	 * But we are still associated with that crtc.
	 */
	if (mode_ctrl & NV50_SOR_MODE_CTRL_CRTC0)
		return pNv->crtc[0];
	else if (mode_ctrl & NV50_SOR_MODE_CTRL_CRTC1)
		return pNv->crtc[1];

	return NULL;
}

void
NV50SorSetFunctionPointers(nouveauOutputPtr output)
{
	output->ModeValid = NV50SorModeValid;
	output->ModeSet = NV50SorModeSet;
	output->SetClockMode = NV50SorSetClockMode;
	output->Sense = NV50SorSense;
	output->Detect = NV50SorDetect;
	output->SetPowerMode = NV50SorSetPowerMode;
	output->GetCurrentCrtc = NV50SorGetCurrentCrtc;
}