summaryrefslogtreecommitdiff
path: root/src/atombios_output.c
blob: 3c5e8a8255e328b2ddd7ec3f987682f6df164d55 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
 * Copyright © 2007 Dave Airlie
 */
/*
 * avivo output handling functions. 
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/* DPMS */
#define DPMS_SERVER
#include <X11/extensions/dpms.h>
#include <unistd.h>

#include "radeon.h"
#include "radeon_reg.h"
#include "radeon_macros.h"
#include "radeon_atombios.h"

static AtomBiosResult
atom_bios_display_device_control(atomBIOSHandlePtr atomBIOS, int device, Bool state)
{
    DISPLAY_DEVICE_OUTPUT_CONTROL_PS_ALLOCATION disp_data;
    AtomBIOSArg data;
    unsigned char *space;
    AtomBiosResult ret;

    disp_data.ucAction = state;
    data.exec.index = device;
    data.exec.dataSpace = (void *)&space;
    data.exec.pspace = &disp_data;
    
    if (RHDAtomBIOSFunc(atomBIOS->scrnIndex, atomBIOS, ATOMBIOS_EXEC, &data) == ATOM_SUCCESS) {
	ErrorF("Output %d enable success\n", device);
	return ATOM_SUCCESS;
    }
    
    ErrorF("Output %d enable failed\n", device);
    return ATOM_NOT_IMPLEMENTED;
}

static void
atom_bios_enable_crt(atomBIOSHandlePtr atomBIOS, int dac, Bool state)
{
    int output;
    if (dac == DAC_PRIMARY)
	output = GetIndexIntoMasterTable(COMMAND, DAC1OutputControl);
    else
	output = GetIndexIntoMasterTable(COMMAND, DAC2OutputControl);

    atom_bios_display_device_control(atomBIOS, output, state);
}

static int
atombios_output_dac_setup(xf86OutputPtr output, DisplayModePtr mode)
{
    RADEONOutputPrivatePtr radeon_output = output->driver_private;
    RADEONInfoPtr info       = RADEONPTR(output->scrn);
    DAC_ENCODER_CONTROL_PS_ALLOCATION disp_data;
    AtomBIOSArg data;
    unsigned char *space;
    AtomBiosResult ret;

    disp_data.ucAction = 1;
    disp_data.ucDacStandard = 1;
    disp_data.usPixelClock = mode->Clock / 10;
    if (radeon_output->DACType == DAC_PRIMARY)
	data.exec.index = GetIndexIntoMasterTable(COMMAND, DAC1EncoderControl);
    else
	data.exec.index = GetIndexIntoMasterTable(COMMAND, DAC2EncoderControl);
    data.exec.dataSpace = (void *)&space;
    data.exec.pspace = &disp_data;
    
    if (RHDAtomBIOSFunc(info->atomBIOS->scrnIndex, info->atomBIOS, ATOMBIOS_EXEC, &data) == ATOM_SUCCESS) {
	ErrorF("Output DAC %d enable success\n", radeon_output->DACType);
	return ATOM_SUCCESS;
    }
    
    ErrorF("Output DAC %d enable failed\n", radeon_output->DACType);
    return ATOM_NOT_IMPLEMENTED;

#if 0
    atom_bios_display_device_control(info->atomBIOS, GetIndexIntoMasterTable(COMMAND, CRT1OutputControl), ATOM_TRANSMITTER_ACTION_INIT);
    atom_bios_display_device_control(info->atomBIOS, GetIndexIntoMasterTable(COMMAND, CRT1OutputControl), ATOM_TRANSMITTER_ACTION_SETUP);
    atom_bios_display_device_control(info->atomBIOS, GetIndexIntoMasterTable(COMMAND, CRT1OutputControl), ATOM_TRANSMITTER_ACTION_ENABLE_OUTPUT);
    return ATOM_SUCCESS;
#endif
}

int
atombios_external_tmds_setup(xf86OutputPtr output, DisplayModePtr mode)
{
    RADEONOutputPrivatePtr radeon_output = output->driver_private;
    RADEONInfoPtr info       = RADEONPTR(output->scrn);
    ENABLE_EXTERNAL_TMDS_ENCODER_PS_ALLOCATION disp_data;
    AtomBIOSArg data;
    unsigned char *space;
    AtomBiosResult ret;

    disp_data.sXTmdsEncoder.ucEnable = 1;

    if (mode->Clock > 165000)
	disp_data.sXTmdsEncoder.ucMisc = 1;
    else
	disp_data.sXTmdsEncoder.ucMisc = 0;

    if (!info->dac6bits)
	disp_data.sXTmdsEncoder.ucMisc |= (1 << 1);

    data.exec.index = GetIndexIntoMasterTable(COMMAND, EnableExternalTMDS_Encoder);
    data.exec.dataSpace = (void *)&space;
    data.exec.pspace = &disp_data;
    
    if (RHDAtomBIOSFunc(info->atomBIOS->scrnIndex, info->atomBIOS, ATOMBIOS_EXEC, &data) == ATOM_SUCCESS) {
	ErrorF("External TMDS enable success\n");
	return ATOM_SUCCESS;
    }
    
    ErrorF("External TMDS enable failed\n", radeon_output->DACType);
    return ATOM_NOT_IMPLEMENTED;
}

static int
atombios_output_tmds1_setup(xf86OutputPtr output, DisplayModePtr mode)
{
    RADEONOutputPrivatePtr radeon_output = output->driver_private;
    RADEONInfoPtr info       = RADEONPTR(output->scrn);
    unsigned char *RADEONMMIO = info->MMIO;
    unsigned int tmp;
    TMDS1_ENCODER_CONTROL_PS_ALLOCATION disp_data;
    AtomBIOSArg data;
    unsigned char *space;
    AtomBiosResult ret;

    disp_data.ucAction = 1;
    if (mode->Clock > 165000)
	disp_data.ucMisc = 1;
    else
	disp_data.ucMisc = 0;
    disp_data.usPixelClock = mode->Clock / 10;
    data.exec.index = GetIndexIntoMasterTable(COMMAND, TMDS1EncoderControl);
    data.exec.dataSpace = (void *)&space;
    data.exec.pspace = &disp_data;
    
    if (RHDAtomBIOSFunc(info->atomBIOS->scrnIndex, info->atomBIOS, ATOMBIOS_EXEC, &data) == ATOM_SUCCESS) {
	ErrorF("Output TMDS1 enable success\n");
	return ATOM_SUCCESS;
    }
    
    ErrorF("Output TMDS1 enable failed\n");
    return ATOM_NOT_IMPLEMENTED;

#if 0
    atom_bios_display_device_control(info->atomBIOS, GetIndexIntoMasterTable(COMMAND, DFP1OutputControl), ATOM_TRANSMITTER_ACTION_INIT);
    atom_bios_display_device_control(info->atomBIOS, GetIndexIntoMasterTable(COMMAND, DFP1OutputControl), ATOM_TRANSMITTER_ACTION_SETUP);
    atom_bios_display_device_control(info->atomBIOS, GetIndexIntoMasterTable(COMMAND, DFP1OutputControl), ATOM_TRANSMITTER_ACTION_ENABLE_OUTPUT);
    return ATOM_SUCCESS;
#endif
}

static void
atombios_output_tmds2_setup(xf86OutputPtr output, DisplayModePtr mode)
{
    RADEONOutputPrivatePtr radeon_output = output->driver_private;
    RADEONInfoPtr info       = RADEONPTR(output->scrn);
    unsigned int tmp;
    TMDS2_ENCODER_CONTROL_PS_ALLOCATION disp_data;
    AtomBIOSArg data;
    unsigned char *space;
    AtomBiosResult ret;

    disp_data.ucAction = 1;
    if (mode->Clock > 165000)
	disp_data.ucMisc = 1;
    else
	disp_data.ucMisc = 0;
    disp_data.usPixelClock = mode->Clock / 10;
    data.exec.index = GetIndexIntoMasterTable(COMMAND, TMDS2EncoderControl);
    data.exec.dataSpace = (void *)&space;
    data.exec.pspace = &disp_data;
    
    if (RHDAtomBIOSFunc(info->atomBIOS->scrnIndex, info->atomBIOS, ATOMBIOS_EXEC, &data) == ATOM_SUCCESS) {
	ErrorF("Output TMDS2 enable success\n");
	return ATOM_SUCCESS;
    }
    
    ErrorF("Output TMDS2 enable failed\n");
    return ATOM_NOT_IMPLEMENTED;
}

static void
atombios_output_dac_dpms(xf86OutputPtr output, int mode)
{
    RADEONOutputPrivatePtr radeon_output = output->driver_private;
    RADEONInfoPtr info       = RADEONPTR(output->scrn);

    switch(mode) {
    case DPMSModeOn:
	atom_bios_enable_crt(info->atomBIOS, radeon_output->DACType, ATOM_ENABLE);
        break;
    case DPMSModeStandby:
    case DPMSModeSuspend:
    case DPMSModeOff:
	atom_bios_enable_crt(info->atomBIOS, radeon_output->DACType, ATOM_DISABLE);
	break;
    }
}

static void
atombios_output_tmds1_dpms(xf86OutputPtr output, int mode)
{
    RADEONOutputPrivatePtr avivo_output = output->driver_private;
    RADEONInfoPtr info       = RADEONPTR(output->scrn);

    switch(mode) {
    case DPMSModeOn:
	/* TODO */
	atom_bios_display_device_control(info->atomBIOS, GetIndexIntoMasterTable(COMMAND, TMDSAOutputControl), ATOM_ENABLE);
    
        break;
    case DPMSModeStandby:
    case DPMSModeSuspend:
    case DPMSModeOff:
	/* TODO */
	atom_bios_display_device_control(info->atomBIOS, GetIndexIntoMasterTable(COMMAND, TMDSAOutputControl), ATOM_DISABLE);
        break;
    }
}

static void
atombios_output_tmds2_dpms(xf86OutputPtr output, int mode)
{
    RADEONOutputPrivatePtr avivo_output = output->driver_private;
    RADEONInfoPtr info       = RADEONPTR(output->scrn);
    unsigned char *RADEONMMIO = info->MMIO;

    switch(mode) {
    case DPMSModeOn:
	atom_bios_display_device_control(info->atomBIOS, GetIndexIntoMasterTable(COMMAND, LVTMAOutputControl), ATOM_ENABLE);
	/* TODO */
        break;
    case DPMSModeStandby:
    case DPMSModeSuspend:
    case DPMSModeOff:
	atom_bios_display_device_control(info->atomBIOS, GetIndexIntoMasterTable(COMMAND, LVTMAOutputControl), ATOM_DISABLE);
	/* TODO */
        break;
    }
}

static void
atombios_output_lvds_dpms(xf86OutputPtr output, int mode)
{
    atombios_output_tmds2_dpms(output, mode);
}

void
atombios_output_dpms(xf86OutputPtr output, int mode)
{
    RADEONOutputPrivatePtr radeon_output = output->driver_private;
    RADEONInfoPtr info       = RADEONPTR(output->scrn);
    unsigned char *RADEONMMIO = info->MMIO;
    int tmp, count;

#if 1
    /* try to grab card lock or at least somethings that looks like a lock
     * if it fails more than 5 times with 1000ms wait btw each try than we
     * assume we can process.
     */
    count = 0;
    tmp = INREG(0x0028);
    while((tmp & 0x100) && (count < 5)) {
        tmp = INREG(0x0028);
        count++;
        usleep(1000);
    }
    if (count >= 5) {
        xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
                   "%s (WARNING) failed to grab card lock process anyway.\n",
                   __func__);
    }
    OUTREG(0x0028, tmp | 0x100);
#endif

    ErrorF("AGD: output dpms\n");

   if (radeon_output->MonType == MT_LCD) {
       atombios_output_tmds2_dpms(output, mode);
   } else if (radeon_output->MonType == MT_DFP) {
       ErrorF("AGD: tmds dpms\n");
       atombios_output_tmds1_dpms(output, mode);
   } else if (radeon_output->MonType == MT_CRT) {
       ErrorF("AGD: dac dpms\n");
       atombios_output_dac_dpms(output, mode);
   }

#if 1
    /* release card lock */
    tmp = INREG(0x0028);
    OUTREG(0x0028, tmp & (~0x100));
#endif
}

static void
atombios_set_crtc_source(xf86OutputPtr output)
{
    RADEONOutputPrivatePtr radeon_output = output->driver_private;
    RADEONCrtcPrivatePtr radeon_crtc = output->crtc->driver_private;
    RADEONInfoPtr info       = RADEONPTR(output->scrn);
    unsigned char *RADEONMMIO = info->MMIO;
    AtomBIOSArg data;
    unsigned char *space;
    SELECT_CRTC_SOURCE_PS_ALLOCATION crtc_src_param;
    int index = GetIndexIntoMasterTable(COMMAND, SelectCRTC_Source);
    int major, minor;
    
    atombios_get_command_table_version(info->atomBIOS, index, &major, &minor);
    
    ErrorF("select crtc source table is %d %d\n", major, minor);

    switch(major) {
    case 1: {
	switch(minor) {
	case 0:
	case 1:
	default:
	    crtc_src_param.ucCRTC = radeon_crtc->crtc_id;
	    if (radeon_output->MonType == MT_CRT) {
		if (radeon_output->DACType == DAC_PRIMARY)
		    crtc_src_param.ucDevice = 0;
		else
		    crtc_src_param.ucDevice = 4;
	    } else if (radeon_output->MonType == MT_DFP) {
		if (radeon_output->TMDSType == TMDS_INT)
		    crtc_src_param.ucDevice = 3;
		else
		    crtc_src_param.ucDevice = 7;
	    } else if (radeon_output->MonType == MT_LCD)
		crtc_src_param.ucDevice = 1;
	    else if (radeon_output->MonType == MT_STV || radeon_output->MonType == MT_CTV)
		crtc_src_param.ucDevice = 2;
	    else if (radeon_output->MonType == MT_CV)
		crtc_src_param.ucDevice = 8;
	    break;
	}
	break;
    }
    default:
	break;
    }

    data.exec.index = index;
    data.exec.dataSpace = (void *)&space;
    data.exec.pspace = &crtc_src_param;
    
    if (RHDAtomBIOSFunc(info->atomBIOS->scrnIndex, info->atomBIOS, ATOMBIOS_EXEC, &data) == ATOM_SUCCESS) {
	ErrorF("Set CRTC Source success\n");
	return;
    }
  
    ErrorF("Set CRTC Source failed\n");
    return;
}

void
atombios_output_mode_set(xf86OutputPtr output,
			 DisplayModePtr mode,
			 DisplayModePtr adjusted_mode)
{
    RADEONInfoPtr info       = RADEONPTR(output->scrn);
    unsigned char *RADEONMMIO = info->MMIO;
    RADEONOutputPrivatePtr radeon_output = output->driver_private;

    atombios_set_crtc_source(output);

    if (radeon_output->MonType == MT_CRT) {
	atombios_output_dac_setup(output, adjusted_mode);
    } else if (radeon_output->MonType == MT_DFP) {
	if (radeon_output->TMDSType == TMDS_INT) 
	    atombios_output_tmds1_setup(output, adjusted_mode);
	else
	    atombios_output_tmds2_setup(output, adjusted_mode);
    } else if (radeon_output->MonType == MT_LCD) {
	atombios_output_tmds2_setup(output, adjusted_mode);
    }
}

static AtomBiosResult
atom_bios_dac_load_detect(atomBIOSHandlePtr atomBIOS, int dac)
{
  DAC_LOAD_DETECTION_PS_ALLOCATION dac_data;
  AtomBIOSArg data;
  unsigned char *space;

  dac_data.sDacload.usDeviceID = 0;
  dac_data.sDacload.ucDacType = 0;
  dac_data.sDacload.ucMisc = 0;

  data.exec.index = GetIndexIntoMasterTable(COMMAND, DAC_LoadDetection);
  data.exec.dataSpace = (void *)&space;
  data.exec.pspace = &dac_data;
  
  if (RHDAtomBIOSFunc(atomBIOS->scrnIndex, atomBIOS, ATOMBIOS_EXEC, &data) == ATOM_SUCCESS) {

    ErrorF("Dac detection success\n");
    return ATOM_SUCCESS ;
  }
  
  ErrorF("DAC detection failed\n");
    return ATOM_NOT_IMPLEMENTED;
}

RADEONMonitorType
atombios_dac_detect(ScrnInfoPtr pScrn, xf86OutputPtr output)
{
    RADEONInfoPtr info       = RADEONPTR(pScrn);
    unsigned char *RADEONMMIO = info->MMIO;
    RADEONOutputPrivatePtr radeon_output = output->driver_private;
    RADEONMonitorType MonType = MT_NONE;
    AtomBiosResult ret;
    uint32_t bios_0_scratch;

    ret = atom_bios_dac_load_detect(info->atomBIOS, radeon_output->DACType);
    if (ret == ATOM_SUCCESS) {
	ErrorF("DAC connect %08X\n", INREG(0x10));
	bios_0_scratch = INREG(RADEON_BIOS_0_SCRATCH);
	
	if (radeon_output->DACType == DAC_PRIMARY) {
	    if (bios_0_scratch & ATOM_S0_CRT1_COLOR)
		MonType = MT_CRT;
	} else {
	    if (bios_0_scratch & ATOM_S0_CRT2_COLOR)
		MonType = MT_CRT;
	}
    }
    return MonType;
}