NV50: Some misc cleanup.
[nouveau] / src / nv50_sor.c
1 /*
2  * Copyright (c) 2007 NVIDIA, Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23
24 #define DPMS_SERVER
25 #include <X11/extensions/dpms.h>
26 #include <X11/Xatom.h>
27
28 #include "nv_include.h"
29 #include "nv50_type.h"
30 #include "nv50_display.h"
31 #include "nv50_output.h"
32
33 void
34 NV50SorSetPClk(xf86OutputPtr output, int pclk)
35 {
36         ScrnInfoPtr pScrn = output->scrn;
37         NVPtr pNv = NVPTR(pScrn);
38         const int limit = 165000;
39
40         /* 0x70000 was a late addition to nv, mentioned as fixing tmds initialisation on certain gpu's. */
41         /* This *may* have solved my shaking image problem, but i am not sure. */
42         /* I presume it's some kind of clock setting, but what precisely i do not know. */
43         NVWrite(pNv, 0x00614300 + NV50OrOffset(output) * 0x800, 0x70000 | ((pclk > limit) ? 0x101 : 0));
44 }
45
46 static void
47 NV50SorDPMSSet(xf86OutputPtr output, int mode)
48 {
49         ScrnInfoPtr pScrn = output->scrn;
50         NVPtr pNv = NVPTR(pScrn);
51         CARD32 tmp;
52
53         while((NVRead(pNv, NV50_SOR0_DPMS_CTRL + NV50OrOffset(output) * 0x800) & NV50_SOR_DPMS_CTRL_PENDING));
54
55         tmp = NVRead(pNv, NV50_SOR0_DPMS_CTRL + NV50OrOffset(output) * 0x800);
56         tmp |= NV50_SOR_DPMS_CTRL_PENDING;
57
58         if(mode == DPMSModeOn)
59                 tmp |= NV50_SOR_DPMS_CTRL_MODE_ON;
60         else
61                 tmp &= ~NV50_SOR_DPMS_CTRL_MODE_ON;
62
63         NVWrite(pNv, NV50_SOR0_DPMS_CTRL + NV50OrOffset(output) * 0x800, tmp);
64         while((NVRead(pNv, 0x0061c030 + NV50OrOffset(output) * 0x800) & 0x10000000));
65 }
66
67 static int
68 NV50TMDSModeValid(xf86OutputPtr output, DisplayModePtr mode)
69 {
70         NVOutputPrivatePtr nv_output = output->driver_private;
71
72         // Disable dual-link modes until I can find a way to make them work
73         // reliably.
74         if (mode->Clock > 165000)
75                 return MODE_CLOCK_HIGH;
76
77         if (mode->HDisplay > nv_output->fpWidth || mode->VDisplay > nv_output->fpHeight)
78                 return MODE_PANEL;
79
80         return NV50OutputModeValid(output, mode);
81 }
82
83 static int
84 NV50LVDSModeValid(xf86OutputPtr output, DisplayModePtr mode)
85 {
86         NVOutputPrivatePtr nv_output = output->driver_private;
87         DisplayModePtr native = nv_output->native_mode;
88
89         // Ignore modes larger than the native res.
90         if (mode->HDisplay > native->HDisplay || mode->VDisplay > native->VDisplay)
91                 return MODE_PANEL;
92
93         return NV50OutputModeValid(output, mode);
94 }
95
96 static void
97 NV50SorModeSet(xf86OutputPtr output, DisplayModePtr mode,
98                 DisplayModePtr adjusted_mode)
99 {
100         ScrnInfoPtr pScrn = output->scrn;
101         NVOutputPrivatePtr nv_output = output->driver_private;
102         const int sorOff = 0x40 * NV50OrOffset(output);
103         uint32_t mode_ctl = NV50_SOR_MODE_CTRL_OFF;
104
105         if (!adjusted_mode) {
106                 /* Disconnect the SOR */
107                 NV50DisplayCommand(pScrn, NV50_SOR0_MODE_CTRL + sorOff, mode_ctl);
108                 return;
109         }
110
111         if (nv_output->type == OUTPUT_LVDS) {
112                 mode_ctl |= NV50_SOR_MODE_CTRL_LVDS;
113         } else {
114                 mode_ctl |= NV50_SOR_MODE_CTRL_TMDS;
115                 if (adjusted_mode->Clock > 165000)
116                         mode_ctl |= NV50_SOR_MODE_CTRL_TMDS_DUAL_LINK;
117         }
118
119         if (output->crtc) {
120                 NVCrtcPrivatePtr nv_crtc = output->crtc->driver_private;
121                 if (nv_crtc->head == 1)
122                         mode_ctl |= NV50_SOR_MODE_CTRL_CRTC1;
123                 else
124                         mode_ctl |= NV50_SOR_MODE_CTRL_CRTC0;
125         } else {
126                 return;
127         }
128
129         if (adjusted_mode->Flags & V_NHSYNC)
130                 mode_ctl |= NV50_SOR_MODE_CTRL_NHSYNC;
131
132         if (adjusted_mode->Flags & V_NVSYNC)
133                 mode_ctl |= NV50_SOR_MODE_CTRL_NVSYNC;
134
135         // This wouldn't be necessary, but the server is stupid and calls
136         // NV50SorDPMSSet after the output is disconnected, even though the hardware
137         // turns it off automatically.
138         NV50SorDPMSSet(output, DPMSModeOn);
139
140         NV50DisplayCommand(pScrn, NV50_SOR0_MODE_CTRL + sorOff, mode_ctl);
141
142         NV50CrtcSetScale(output->crtc, mode, adjusted_mode, nv_output->scaling_mode);
143 }
144
145 static xf86OutputStatus
146 NV50SorDetect(xf86OutputPtr output)
147 {
148         NVOutputPrivatePtr nv_output = output->driver_private;
149         xf86MonPtr ddc_mon;
150
151         if (nv_output->pDDCBus == NULL)
152                 return XF86OutputStatusDisconnected;
153
154         ddc_mon = NV50OutputGetEDID(output, nv_output->pDDCBus);
155         if (!ddc_mon)
156                 return XF86OutputStatusDisconnected;
157
158         if (!ddc_mon->features.input_type) /* Analog? */
159                 return XF86OutputStatusDisconnected;
160
161         return XF86OutputStatusConnected;
162 }
163
164 static xf86OutputStatus
165 NV50SorLVDSDetect(xf86OutputPtr output)
166 {
167         /* Assume LVDS is always connected */
168         return XF86OutputStatusConnected;
169 }
170
171 static void
172 NV50SorDestroy(xf86OutputPtr output)
173 {
174         NVOutputPrivatePtr nv_output = output->driver_private;
175
176         NV50OutputDestroy(output);
177
178         xf86DeleteMode(&nv_output->native_mode, nv_output->native_mode);
179
180         xfree(output->driver_private);
181         output->driver_private = NULL;
182 }
183
184 static Bool
185 NV50SorModeFixup(xf86OutputPtr output, DisplayModePtr mode,
186                  DisplayModePtr adjusted_mode)
187 {
188         NVOutputPrivatePtr nv_output = output->driver_private;
189
190         if (nv_output->native_mode && nv_output->scaling_mode != SCALE_PANEL) {
191                 adjusted_mode->HDisplay = nv_output->native_mode->HDisplay;
192                 adjusted_mode->HSkew = nv_output->native_mode->HSkew;
193                 adjusted_mode->HSyncStart = nv_output->native_mode->HSyncStart;
194                 adjusted_mode->HSyncEnd = nv_output->native_mode->HSyncEnd;
195                 adjusted_mode->HTotal = nv_output->native_mode->HTotal;
196                 adjusted_mode->VDisplay = nv_output->native_mode->VDisplay;
197                 adjusted_mode->VScan = nv_output->native_mode->VScan;
198                 adjusted_mode->VSyncStart = nv_output->native_mode->VSyncStart;
199                 adjusted_mode->VSyncEnd = nv_output->native_mode->VSyncEnd;
200                 adjusted_mode->VTotal = nv_output->native_mode->VTotal;
201                 adjusted_mode->Clock = nv_output->native_mode->Clock;
202                 adjusted_mode->Flags = nv_output->native_mode->Flags;
203
204                 /* No INTERLACE_HALVE_V, because we manually correct. */
205                 xf86SetModeCrtc(adjusted_mode, 0);
206         }
207         return TRUE;
208 }
209
210 static DisplayModePtr
211 NV50SorGetLVDSModes(xf86OutputPtr output)
212 {
213         NVOutputPrivatePtr nv_output = output->driver_private;
214         return xf86DuplicateMode(nv_output->native_mode);
215 }
216
217 static const xf86OutputFuncsRec NV50SorTMDSOutputFuncs = {
218         .dpms = NV50SorDPMSSet,
219         .save = NULL,
220         .restore = NULL,
221         .mode_valid = NV50TMDSModeValid,
222         .mode_fixup = NV50SorModeFixup,
223         .prepare = NV50OutputPrepare,
224         .commit = NV50OutputCommit,
225         .mode_set = NV50SorModeSet,
226         .detect = NV50SorDetect,
227         .get_modes = NV50OutputGetDDCModes,
228         .create_resources = nv_digital_output_create_resources,
229         .set_property = nv_digital_output_set_property,
230         .destroy = NV50SorDestroy,
231 };
232
233 static const xf86OutputFuncsRec NV50SorLVDSOutputFuncs = {
234         .dpms = NV50SorDPMSSet,
235         .save = NULL,
236         .restore = NULL,
237         .mode_valid = NV50LVDSModeValid,
238         .mode_fixup = NV50SorModeFixup,
239         .prepare = NV50OutputPrepare,
240         .commit = NV50OutputCommit,
241         .mode_set = NV50SorModeSet,
242         .detect = NV50SorLVDSDetect,
243         .get_modes = NV50SorGetLVDSModes,
244         .create_resources = nv_digital_output_create_resources,
245         .set_property = nv_digital_output_set_property,
246         .destroy = NV50SorDestroy,
247 };
248
249 const xf86OutputFuncsRec * nv50_get_tmds_output_funcs()
250 {
251         return &NV50SorTMDSOutputFuncs;
252 }
253
254 const xf86OutputFuncsRec * nv50_get_lvds_output_funcs()
255 {
256         return &NV50SorLVDSOutputFuncs;
257 }
258
259 static DisplayModePtr
260 ReadLVDSNativeMode(ScrnInfoPtr pScrn, const int off)
261 {
262         NVPtr pNv = NVPTR(pScrn);
263         DisplayModePtr mode = xnfcalloc(1, sizeof(DisplayModeRec));
264         const CARD32 size = NVRead(pNv, 0x00610b4c + off);
265         const int width = size & 0x3fff;
266         const int height = (size >> 16) & 0x3fff;
267
268         mode->HDisplay = width;
269         mode->VDisplay = height;
270         mode->Clock = NVRead(pNv, 0x00610ad4 + off) & 0x3fffff;
271
272         /* We should investigate what else is found in these register ranges. */
273         uint32_t unk1 = NVRead(pNv, 0x00610afc + off);
274         uint32_t unk2 = NVRead(pNv, 0x00610b04 + off);
275         uint32_t unk3 = NVRead(pNv, 0x00610ae8 + off);
276         /*uint32_t unk4 = NVRead(pNv, 0x00610af4 + off);*/
277
278         /* Recontruct our mode, so it can be handled normally. */
279         mode->HTotal = (unk1 & 0xFFFF);
280         mode->VTotal = (unk1 >> 16);
281
282         /* Assuming no interlacing. */
283         mode->HSyncStart = mode->HTotal - (unk3 & 0xFFFF) - 1;
284         mode->VSyncStart = mode->VTotal - (unk3 >> 16) - 1;
285
286         mode->HSyncEnd = mode->HSyncStart + (unk2 & 0xFFFF) + 1;
287         mode->VSyncEnd = mode->VSyncStart + (unk2 >> 16) + 1;
288
289         mode->next = mode->prev = NULL;
290         mode->status = MODE_OK;
291         mode->type = M_T_DRIVER | M_T_PREFERRED;
292
293         xf86SetModeDefaultName(mode);
294
295         return mode;
296 }
297
298 DisplayModePtr
299 GetLVDSNativeMode(ScrnInfoPtr pScrn)
300 {
301         NVPtr pNv = NVPTR(pScrn);
302         CARD32 val = NVRead(pNv, 0x00610050);
303
304         if ((val & 0x3) == 0x2) {
305                 return ReadLVDSNativeMode(pScrn, 0);
306         } else if ((val & 0x300) == 0x200) {
307                 return ReadLVDSNativeMode(pScrn, 0x540);
308         }
309
310         return NULL;
311 }