xref: /haiku/src/add-ons/accelerants/intel_extreme/Ports.cpp (revision 4c8e85b316c35a9161f5a1c50ad70bc91c83a76f)
1 /*
2  * Copyright 2006-2015, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, axeld@pinc-software.de
7  *		Michael Lotz, mmlr@mlotz.ch
8  *		Alexander von Gluck IV, kallisti5@unixzen.com
9  */
10 
11 
12 #include "Ports.h"
13 
14 #include <ddc.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <Debug.h>
18 #include <KernelExport.h>
19 
20 #include "accelerant.h"
21 #include "accelerant_protos.h"
22 #include "FlexibleDisplayInterface.h"
23 #include "intel_extreme.h"
24 #include "PanelFitter.h"
25 
26 #include <new>
27 
28 
29 #undef TRACE
30 #define TRACE_PORTS
31 #ifdef TRACE_PORTS
32 #   define TRACE(x...) _sPrintf("intel_extreme: " x)
33 #else
34 #   define TRACE(x...)
35 #endif
36 
37 #define ERROR(x...) _sPrintf("intel_extreme: " x)
38 #define CALLED(x...) TRACE("CALLED %s\n", __PRETTY_FUNCTION__)
39 
40 
41 static bool
42 wait_for_set(addr_t address, uint32 mask, uint32 timeout)
43 {
44 	int interval = 50;
45 	uint32 i = 0;
46 	for(i = 0; i <= timeout; i += interval) {
47 		spin(interval);
48 		if ((read32(address) & mask) != 0)
49 			return true;
50 	}
51 	return false;
52 }
53 
54 
55 static bool
56 wait_for_clear(addr_t address, uint32 mask, uint32 timeout)
57 {
58 	int interval = 50;
59 	uint32 i = 0;
60 	for(i = 0; i <= timeout; i += interval) {
61 		spin(interval);
62 		if ((read32(address) & mask) == 0)
63 			return true;
64 	}
65 	return false;
66 }
67 
68 
69 Port::Port(port_index index, const char* baseName)
70 	:
71 	fPipe(NULL),
72 	fEDIDState(B_NO_INIT),
73 	fPortIndex(index),
74 	fPortName(NULL)
75 {
76 	char portID[2];
77 	portID[0] = 'A' + index - INTEL_PORT_A;
78 	portID[1] = 0;
79 
80 	char buffer[32];
81 	buffer[0] = 0;
82 
83 	strlcat(buffer, baseName, sizeof(buffer));
84 	strlcat(buffer, " ", sizeof(buffer));
85 	strlcat(buffer, portID, sizeof(buffer));
86 	fPortName = strdup(buffer);
87 }
88 
89 
90 Port::~Port()
91 {
92 	free(fPortName);
93 }
94 
95 
96 bool
97 Port::HasEDID()
98 {
99 	if (fEDIDState == B_NO_INIT)
100 		GetEDID(NULL);
101 
102 	return fEDIDState == B_OK;
103 }
104 
105 
106 status_t
107 Port::SetPipe(Pipe* pipe)
108 {
109 	CALLED();
110 
111 	if (pipe == NULL) {
112 		ERROR("%s: Invalid pipe provided!\n", __func__);
113 		return B_ERROR;
114 	}
115 
116 	uint32 portRegister = _PortRegister();
117 	if (portRegister == 0) {
118 		ERROR("%s: Invalid PortRegister ((0x%" B_PRIx32 ") for %s\n", __func__,
119 			portRegister, PortName());
120 		return B_ERROR;
121 	}
122 
123 	// TODO: UnAssignPipe?  This likely needs reworked a little
124 	if (fPipe != NULL) {
125 		ERROR("%s: Can't reassign display pipe (yet)\n", __func__);
126 		return B_ERROR;
127 	}
128 
129 	switch (pipe->Index()) {
130 		case INTEL_PIPE_B:
131 			TRACE("%s: Assigning %s (0x%" B_PRIx32 ") to pipe B\n", __func__,
132 				PortName(), portRegister);
133 			break;
134 		case INTEL_PIPE_C:
135 			TRACE("%s: Assigning %s (0x%" B_PRIx32 ") to pipe C\n", __func__,
136 				PortName(), portRegister);
137 			break;
138 		case INTEL_PIPE_D:
139 			TRACE("%s: Assigning %s (0x%" B_PRIx32 ") to pipe D\n", __func__,
140 				PortName(), portRegister);
141 			break;
142 		default:
143 			TRACE("%s: Assigning %s (0x%" B_PRIx32 ") to pipe A\n", __func__,
144 				PortName(), portRegister);
145 			break;
146 	}
147 
148 	uint32 portState = read32(portRegister);
149 
150 	// FIXME is the use of PORT_TRANS_* constants correct for Sandy Bridge /
151 	// Cougar Point? Or is it only for Ivy Bridge / Panther point onwards?
152 	if (gInfo->shared_info->pch_info == INTEL_PCH_CPT) {
153 		portState &= ~PORT_TRANS_SEL_MASK;
154 		if (pipe->Index() == INTEL_PIPE_A)
155 			write32(portRegister, portState | PORT_TRANS_A_SEL_CPT);
156 		else
157 			write32(portRegister, portState | PORT_TRANS_B_SEL_CPT);
158 	} else {
159 		if (pipe->Index() == INTEL_PIPE_A)
160 			write32(portRegister, portState & ~DISPLAY_MONITOR_PIPE_B);
161 		else
162 			write32(portRegister, portState | DISPLAY_MONITOR_PIPE_B);
163 	}
164 	fPipe = pipe;
165 
166 	if (fPipe == NULL)
167 		return B_NO_MEMORY;
168 
169 	// Disable display pipe until modesetting enables it
170 	if (fPipe->IsEnabled())
171 		fPipe->Enable(false);
172 
173 	read32(portRegister);
174 
175 	return B_OK;
176 }
177 
178 
179 status_t
180 Port::Power(bool enabled)
181 {
182 	if (fPipe == NULL) {
183 		ERROR("%s: Setting power mode without assigned pipe!\n", __func__);
184 		return B_ERROR;
185 	}
186 
187 	fPipe->Enable(enabled);
188 
189 	return B_OK;
190 }
191 
192 
193 status_t
194 Port::GetEDID(edid1_info* edid, bool forceRead)
195 {
196 	CALLED();
197 
198 	if (fEDIDState == B_NO_INIT || forceRead) {
199 		TRACE("%s: trying to read EDID\n", PortName());
200 
201 		addr_t ddcRegister = _DDCRegister();
202 		if (ddcRegister == 0) {
203 			TRACE("%s: no DDC register found\n", PortName());
204 			fEDIDState = B_ERROR;
205 			return fEDIDState;
206 		}
207 
208 		TRACE("%s: using ddc @ 0x%" B_PRIxADDR "\n", PortName(), ddcRegister);
209 
210 		i2c_bus bus;
211 		bus.cookie = (void*)ddcRegister;
212 		bus.set_signals = &_SetI2CSignals;
213 		bus.get_signals = &_GetI2CSignals;
214 		ddc2_init_timing(&bus);
215 
216 		fEDIDState = ddc2_read_edid1(&bus, &fEDIDInfo, NULL, NULL);
217 
218 		if (fEDIDState == B_OK) {
219 			TRACE("%s: found EDID information!\n", PortName());
220 			edid_dump(&fEDIDInfo);
221 		}
222 	}
223 
224 	if (fEDIDState != B_OK) {
225 		TRACE("%s: no EDID information found.\n", PortName());
226 		return fEDIDState;
227 	}
228 
229 	if (edid != NULL)
230 		memcpy(edid, &fEDIDInfo, sizeof(edid1_info));
231 
232 	return B_OK;
233 }
234 
235 
236 status_t
237 Port::GetPLLLimits(pll_limits& limits)
238 {
239 	return B_ERROR;
240 }
241 
242 
243 pipe_index
244 Port::PipePreference()
245 {
246 	CALLED();
247 	// Ideally we could just return INTEL_PIPE_ANY for all devices by default, but
248 	// this doesn't quite work yet. We need to use the BIOS presetup pipes for now.
249 	if (gInfo->shared_info->device_type.Generation() < 4)
250 		return INTEL_PIPE_ANY;
251 
252 	// Notes:
253 	// - The BIOSes seen sofar do not use PIPE C by default.
254 	// - The BIOSes seen sofar program transcoder A to PIPE A, etc.
255 	// - Later devices add a pipe C alongside the added transcoder C.
256 
257 	// FIXME How's this setup in newer gens? Currently return INTEL_PIPE_ANY there..
258 	if ((gInfo->shared_info->device_type.Generation() <= 7) &&
259 		(!gInfo->shared_info->device_type.HasDDI())) {
260 		uint32 portState = read32(_PortRegister());
261 		if (gInfo->shared_info->pch_info == INTEL_PCH_CPT) {
262 			portState &= PORT_TRANS_SEL_MASK;
263 			if (portState == PORT_TRANS_B_SEL_CPT)
264 				return INTEL_PIPE_B;
265 			else
266 				return INTEL_PIPE_A;
267 		} else {
268 			if (portState & DISPLAY_MONITOR_PIPE_B)
269 				return INTEL_PIPE_B;
270 			else
271 				return INTEL_PIPE_A;
272 		}
273 	}
274 
275 	if (gInfo->shared_info->device_type.HasDDI()) {
276 		// scan all our pipes to find the one connected to the current port
277 		uint32 pipeState = 0;
278 		for (uint32 pipeCnt = 0; pipeCnt < 4; pipeCnt++) {
279 			switch (pipeCnt) {
280 				case 0:
281 					pipeState = read32(PIPE_DDI_FUNC_CTL_A);
282 					break;
283 				case 1:
284 					pipeState = read32(PIPE_DDI_FUNC_CTL_B);
285 					break;
286 				case 2:
287 					pipeState = read32(PIPE_DDI_FUNC_CTL_C);
288 					break;
289 				default:
290 					pipeState = read32(PIPE_DDI_FUNC_CTL_EDP);
291 					break;
292 			}
293 
294 			if ((((pipeState & PIPE_DDI_SELECT_MASK) >> PIPE_DDI_SELECT_SHIFT) + 1)
295 				== (uint32)PortIndex()) {
296 				switch (pipeCnt) {
297 					case 0:
298 						return INTEL_PIPE_A;
299 					case 1:
300 						return INTEL_PIPE_B;
301 					case 2:
302 						return INTEL_PIPE_C;
303 					default:
304 						return INTEL_PIPE_D;
305 				}
306 			}
307 		}
308 	}
309 
310 	return INTEL_PIPE_ANY;
311 }
312 
313 
314 status_t
315 Port::_GetI2CSignals(void* cookie, int* _clock, int* _data)
316 {
317 	addr_t ioRegister = (addr_t)cookie;
318 	uint32 value = read32(ioRegister);
319 
320 	*_clock = (value & I2C_CLOCK_VALUE_IN) != 0;
321 	*_data = (value & I2C_DATA_VALUE_IN) != 0;
322 
323 	return B_OK;
324 }
325 
326 
327 status_t
328 Port::_SetI2CSignals(void* cookie, int clock, int data)
329 {
330 	addr_t ioRegister = (addr_t)cookie;
331 	uint32 value;
332 
333 	if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_83x)) {
334 		// on these chips, the reserved values are fixed
335 		value = 0;
336 	} else {
337 		// on all others, we have to preserve them manually
338 		value = read32(ioRegister) & I2C_RESERVED;
339 	}
340 
341 	// if we send clk or data, we always send low logic level;
342 	// if we want to send high level, we actually receive and let the
343 	// external pullup resistors create the high level on the bus.
344 	value |= I2C_DATA_VALUE_MASK;  //sets data = 0, always latch
345 	value |= I2C_CLOCK_VALUE_MASK; //sets clock = 0, always latch
346 
347 	if (data != 0)
348 		value |= I2C_DATA_DIRECTION_MASK;
349 	else {
350 		value |= I2C_DATA_DIRECTION_MASK | I2C_DATA_DIRECTION_OUT;
351 	}
352 
353 	if (clock != 0)
354 		value |= I2C_CLOCK_DIRECTION_MASK;
355 	else {
356 		value |= I2C_CLOCK_DIRECTION_MASK | I2C_CLOCK_DIRECTION_OUT;
357 	}
358 
359 	write32(ioRegister, value);
360 	read32(ioRegister);
361 		// make sure the PCI bus has flushed the write
362 
363 	return B_OK;
364 }
365 
366 
367 // #pragma mark - Analog Port
368 
369 
370 AnalogPort::AnalogPort()
371 	:
372 	Port(INTEL_PORT_A, "Analog")
373 {
374 }
375 
376 
377 bool
378 AnalogPort::IsConnected()
379 {
380 	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
381 		_PortRegister());
382 	return HasEDID();
383 }
384 
385 
386 addr_t
387 AnalogPort::_DDCRegister()
388 {
389 	// always fixed
390 	return INTEL_I2C_IO_A;
391 }
392 
393 
394 addr_t
395 AnalogPort::_PortRegister()
396 {
397 	// always fixed
398 	return INTEL_ANALOG_PORT;
399 }
400 
401 
402 status_t
403 AnalogPort::SetDisplayMode(display_mode* target, uint32 colorMode)
404 {
405 	CALLED();
406 	TRACE("%s: %s %dx%d\n", __func__, PortName(), target->timing.h_display,
407 		target->timing.v_display);
408 
409 	if (fPipe == NULL) {
410 		ERROR("%s: Setting display mode without assigned pipe!\n", __func__);
411 		return B_ERROR;
412 	}
413 
414 	// Setup PanelFitter and Train FDI if it exists
415 	PanelFitter* fitter = fPipe->PFT();
416 	if (fitter != NULL)
417 		fitter->Enable(target->timing);
418 	FDILink* link = fPipe->FDI();
419 	if (link != NULL)
420 		link->Train(&target->timing);
421 
422 	pll_divisors divisors;
423 	compute_pll_divisors(&target->timing, &divisors, false);
424 
425 	uint32 extraPLLFlags = 0;
426 	if (gInfo->shared_info->device_type.Generation() >= 3)
427 		extraPLLFlags |= DISPLAY_PLL_MODE_NORMAL;
428 
429 	// Program general pipe config
430 	fPipe->Configure(target);
431 
432 	// Program pipe PLL's
433 	fPipe->ConfigureClocks(divisors, target->timing.pixel_clock, extraPLLFlags);
434 
435 	write32(_PortRegister(), (read32(_PortRegister())
436 		& ~(DISPLAY_MONITOR_POLARITY_MASK | DISPLAY_MONITOR_VGA_POLARITY))
437 		| ((target->timing.flags & B_POSITIVE_HSYNC) != 0
438 			? DISPLAY_MONITOR_POSITIVE_HSYNC : 0)
439 		| ((target->timing.flags & B_POSITIVE_VSYNC) != 0
440 			? DISPLAY_MONITOR_POSITIVE_VSYNC : 0));
441 
442 	// Program target display mode
443 	fPipe->ConfigureTimings(target);
444 
445 	// Set fCurrentMode to our set display mode
446 	memcpy(&fCurrentMode, target, sizeof(display_mode));
447 
448 	return B_OK;
449 }
450 
451 
452 // #pragma mark - LVDS Panel
453 
454 
455 LVDSPort::LVDSPort()
456 	:
457 	Port(INTEL_PORT_C, "LVDS")
458 {
459 	// Always unlock LVDS port as soon as we start messing with it.
460 	uint32 panelControl = INTEL_PANEL_CONTROL;
461 	if (gInfo->shared_info->pch_info != INTEL_PCH_NONE) {
462 		// FIXME writing there results in black screen on SandyBridge
463 		return;
464 		// panelControl = PCH_PANEL_CONTROL;
465 	}
466 	write32(panelControl, read32(panelControl) | PANEL_REGISTER_UNLOCK);
467 }
468 
469 
470 pipe_index
471 LVDSPort::PipePreference()
472 {
473 	CALLED();
474 	// Older devices have hardcoded pipe/port mappings, so just use that
475 	if (gInfo->shared_info->device_type.Generation() < 4)
476 		return INTEL_PIPE_B;
477 
478 	// Ideally we could just return INTEL_PIPE_ANY for the newer devices, but
479 	// this doesn't quite work yet.
480 
481 	// On SandyBridge and later, there is a transcoder C. On SandyBridge at least
482 	// that can't be used by the LVDS port (but A and B would be fine).
483 	// On Ibex Point, SandyBridge and IvyBridge (tested) changing pipes does not
484 	// work yet.
485 	// Notes:
486 	// - Switching Pipes only works reliably when a 'full modeswitch' is executed
487 	//   (FDI training) so we have to reuse the BIOS preset setup always for now.
488 	// - The BIOSes seen sofar do not use PIPE C by default.
489 	// - The BIOSes seen sofar program transcoder A to PIPE A, etc.
490 	// - Later devices add a pipe C alongside the added transcoder C.
491 
492 	// FIXME How's this setup in newer gens? Currently return Pipe B fixed there..
493 	if (gInfo->shared_info->device_type.Generation() <= 7) {
494 		uint32 portState = read32(_PortRegister());
495 		if (gInfo->shared_info->pch_info == INTEL_PCH_CPT) {
496 			portState &= PORT_TRANS_SEL_MASK;
497 			if (portState == PORT_TRANS_B_SEL_CPT)
498 				return INTEL_PIPE_B;
499 			else
500 				return INTEL_PIPE_A;
501 		} else {
502 			if (portState & DISPLAY_MONITOR_PIPE_B)
503 				return INTEL_PIPE_B;
504 			else
505 				return INTEL_PIPE_A;
506 		}
507 	}
508 
509 	return INTEL_PIPE_B;
510 }
511 
512 
513 bool
514 LVDSPort::IsConnected()
515 {
516 	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
517 		_PortRegister());
518 
519 	if (gInfo->shared_info->pch_info != INTEL_PCH_NONE) {
520 		uint32 registerValue = read32(_PortRegister());
521 		// there's a detection bit we can use
522 		if ((registerValue & PCH_LVDS_DETECTED) == 0) {
523 			TRACE("LVDS: Not detected\n");
524 			return false;
525 		}
526 		// TODO: Skip if eDP support
527 	} else if (gInfo->shared_info->device_type.Generation() <= 4) {
528 		// Older generations don't have LVDS detection. If not mobile skip.
529 		if (!gInfo->shared_info->device_type.IsMobile()) {
530 			TRACE("LVDS: Skipping LVDS detection due to gen and not mobile\n");
531 			return false;
532 		}
533 		// If mobile, try to grab EDID
534 		// Linux seems to look at lid status for LVDS port detection
535 		// If we don't get EDID, we can use vbios native mode or vesa?
536 		if (!HasEDID()) {
537 			if (gInfo->shared_info->has_vesa_edid_info) {
538 				TRACE("LVDS: Using VESA edid info\n");
539 				memcpy(&fEDIDInfo, &gInfo->shared_info->vesa_edid_info,
540 					sizeof(edid1_info));
541 				fEDIDState = B_OK;
542 				// HasEDID now true
543 			} else if (gInfo->shared_info->got_vbt) {
544 				TRACE("LVDS: No EDID, but force enabled as we have a VBT\n");
545 				return true;
546 			} else {
547 				TRACE("LVDS: Couldn't find any valid EDID!\n");
548 				return false;
549 			}
550 		}
551 	}
552 
553 	// Try getting EDID, as the LVDS port doesn't overlap with anything else,
554 	// we don't run the risk of getting someone else's data.
555 	return HasEDID();
556 }
557 
558 
559 addr_t
560 LVDSPort::_DDCRegister()
561 {
562 	// always fixed
563 	return INTEL_I2C_IO_C;
564 }
565 
566 
567 addr_t
568 LVDSPort::_PortRegister()
569 {
570 	// always fixed
571 	return INTEL_DIGITAL_LVDS_PORT;
572 }
573 
574 
575 status_t
576 LVDSPort::SetDisplayMode(display_mode* target, uint32 colorMode)
577 {
578 	CALLED();
579 	if (target == NULL) {
580 		ERROR("%s: Invalid target mode passed!\n", __func__);
581 		return B_ERROR;
582 	}
583 
584 	TRACE("%s: %s-%d %dx%d\n", __func__, PortName(), PortIndex(),
585 		target->timing.h_display, target->timing.v_display);
586 
587 	if (fPipe == NULL) {
588 		ERROR("%s: Setting display mode without assigned pipe!\n", __func__);
589 		return B_ERROR;
590 	}
591 
592 	addr_t panelControl = INTEL_PANEL_CONTROL;
593 	addr_t panelStatus = INTEL_PANEL_STATUS;
594 	if (gInfo->shared_info->pch_info != INTEL_PCH_NONE) {
595 		panelControl = PCH_PANEL_CONTROL;
596 		panelStatus = PCH_PANEL_STATUS;
597 	}
598 
599 	if (gInfo->shared_info->device_type.Generation() != 4) {
600 		// TODO not needed on any generation if we are using the panel fitter
601 		// Power off Panel
602 		write32(panelControl,
603 			read32(panelControl) & ~PANEL_CONTROL_POWER_TARGET_ON);
604 		read32(panelControl);
605 
606 		if (!wait_for_clear(panelStatus, PANEL_STATUS_POWER_ON, 1000)) {
607 			ERROR("%s: %s didn't power off within 1000ms!\n", __func__,
608 				PortName());
609 		}
610 	}
611 
612 	// For LVDS panels, we may need to set the timings according to the panel
613 	// native video mode, and let the panel fitter do the scaling. But the
614 	// place where the scaling happens varies accross generations of devices.
615 	display_timing hardwareTarget;
616 	bool needsScaling = false;
617 
618 	// TODO figure out how it's done (or if we need to configure something at
619 	// all) for other generations
620 	if (gInfo->shared_info->device_type.Generation() <= 6
621 		&& gInfo->shared_info->device_type.Generation() >= 3
622 		&& gInfo->shared_info->got_vbt) {
623 		// Set vbios hardware panel mode as base
624 		hardwareTarget = gInfo->shared_info->panel_timing;
625 
626 		if (hardwareTarget.h_display == target->timing.h_display
627 				&& hardwareTarget.v_display == target->timing.v_display) {
628 			// We are setting the native video mode, nothing special to do
629 			// Note: this means refresh and timing might vary according to requested mode.
630 			hardwareTarget = target->timing;
631 			TRACE("%s: Setting LVDS to native resolution at %" B_PRIu32 "Hz\n", __func__,
632 				hardwareTarget.pixel_clock * 1000 / (hardwareTarget.h_total * hardwareTarget.v_total));
633 		} else {
634 			// We need to enable the panel fitter
635 			TRACE("%s: Hardware mode will actually be %dx%d at %" B_PRIu32 "Hz\n", __func__,
636 				hardwareTarget.h_display, hardwareTarget.v_display,
637 				hardwareTarget.pixel_clock * 1000 / (hardwareTarget.h_total * hardwareTarget.v_total));
638 
639 			// FIXME we should also get the refresh frequency from the target
640 			// mode, and then "sanitize" the resulting mode we made up.
641 			needsScaling = true;
642 		}
643 	} else {
644 		TRACE("Setting LVDS mode without VBT info or on unhandled hardware "
645 			"generation, scaling may not work\n");
646 		// We don't have VBT data, try to set the requested mode directly
647 		// and hope for the best
648 		hardwareTarget = target->timing;
649 	}
650 
651 	// Setup PanelFitter and Train FDI if it exists
652 	PanelFitter* fitter = fPipe->PFT();
653 	if (fitter != NULL)
654 		fitter->Enable(hardwareTarget);
655 	FDILink* link = fPipe->FDI();
656 	if (link != NULL)
657 		link->Train(&hardwareTarget);
658 
659 	pll_divisors divisors;
660 	compute_pll_divisors(&hardwareTarget, &divisors, true);
661 
662 	uint32 lvds = read32(_PortRegister())
663 		| LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
664 
665 	if (gInfo->shared_info->device_type.Generation() == 4) {
666 		// LVDS_A3_POWER_UP == 24bpp
667 		// otherwise, 18bpp
668 		if ((lvds & LVDS_A3_POWER_MASK) != LVDS_A3_POWER_UP)
669 			lvds |= LVDS_18BIT_DITHER;
670 	}
671 
672 	// LVDS on PCH needs set before display enable
673 	if (gInfo->shared_info->pch_info == INTEL_PCH_CPT) {
674 		lvds &= ~PORT_TRANS_SEL_MASK;
675 		if (fPipe->Index() == INTEL_PIPE_A)
676 			lvds |= PORT_TRANS_A_SEL_CPT;
677 		else
678 			lvds |= PORT_TRANS_B_SEL_CPT;
679 	}
680 
681 	// Set the B0-B3 data pairs corresponding to whether we're going to
682 	// set the DPLLs for dual-channel mode or not.
683 	if (divisors.p2 == 5 || divisors.p2 == 7) {
684 		TRACE("LVDS: dual channel\n");
685 		lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
686 	} else {
687 		TRACE("LVDS: single channel\n");
688 		lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
689 	}
690 
691 	// LVDS port control moves polarity bits because Intel hates you.
692 	// Set LVDS sync polarity
693 	lvds &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
694 
695 	// set on - polarity.
696 	if ((target->timing.flags & B_POSITIVE_HSYNC) == 0)
697 		lvds |= LVDS_HSYNC_POLARITY;
698 	if ((target->timing.flags & B_POSITIVE_VSYNC) == 0)
699 		lvds |= LVDS_VSYNC_POLARITY;
700 
701 	TRACE("%s: LVDS Write: 0x%" B_PRIx32 "\n", __func__, lvds);
702 	write32(_PortRegister(), lvds);
703 	read32(_PortRegister());
704 
705 	uint32 extraPLLFlags = 0;
706 
707 	// DPLL mode LVDS for i915+
708 	if (gInfo->shared_info->device_type.Generation() >= 3)
709 		extraPLLFlags |= DISPLAY_PLL_MODE_LVDS | DISPLAY_PLL_2X_CLOCK;
710 
711 	// Program general pipe config
712 	fPipe->Configure(target);
713 
714 	// Program pipe PLL's (using the hardware mode timings, since that's what
715 	// the PLL is used for)
716 	fPipe->ConfigureClocks(divisors, hardwareTarget.pixel_clock,
717 		extraPLLFlags);
718 
719 	if (gInfo->shared_info->device_type.Generation() != 4) {
720 		// G45: no need to power the panel off
721 		// Power on Panel
722 		write32(panelControl,
723 			read32(panelControl) | PANEL_CONTROL_POWER_TARGET_ON);
724 		read32(panelControl);
725 
726 		if (!wait_for_set(panelStatus, PANEL_STATUS_POWER_ON, 1000)) {
727 			ERROR("%s: %s didn't power on within 1000ms!\n", __func__,
728 				PortName());
729 		}
730 	}
731 
732 	// Program target display mode
733 	fPipe->ConfigureTimings(target, !needsScaling);
734 
735 	if (needsScaling) {
736 		if (gInfo->shared_info->device_type.Generation() <= 4) {
737 			// Enable panel fitter in automatic mode. It will figure out
738 			// the scaling ratios automatically.
739 			uint32 panelFitterControl = read32(INTEL_PANEL_FIT_CONTROL);
740 			panelFitterControl |= PANEL_FITTER_ENABLED;
741 			panelFitterControl &= ~(PANEL_FITTER_SCALING_MODE_MASK
742 				| PANEL_FITTER_PIPE_MASK);
743 			panelFitterControl |= PANEL_FITTER_PIPE_B;
744 			// LVDS is always on pipe B.
745 			write32(INTEL_PANEL_FIT_CONTROL, panelFitterControl);
746 		}
747 		// TODO do we need to do anything on later generations?
748 	} else {
749 		if (gInfo->shared_info->device_type.Generation() == 4
750 			|| gInfo->shared_info->device_type.Generation() == 3) {
751 			// Bypass the panel fitter
752 			uint32 panelFitterControl = read32(INTEL_PANEL_FIT_CONTROL);
753 			panelFitterControl &= ~PANEL_FITTER_ENABLED;
754 			write32(INTEL_PANEL_FIT_CONTROL, panelFitterControl);
755 		} else {
756 			// We don't need to do anything more for later generations, the
757 			// scaling is handled at the transcoder level. We may want to
758 			// configure dithering, but the code below ignores the previous
759 			// value in the register and may mess things up so we should do
760 			// this in a safeer way. For now, assume the BIOS did the right
761 			// thing.
762 #if 0
763 			// Disable panel fitting, but enable 8 to 6-bit dithering
764 			write32(INTEL_PANEL_FIT_CONTROL, 0x4);
765 				// TODO: do not do this if the connected panel is 24-bit
766 				// (I don't know how to detect that)
767 #endif
768 		}
769 	}
770 
771 	// Set fCurrentMode to our set display mode
772 	memcpy(&fCurrentMode, target, sizeof(display_mode));
773 
774 	return B_OK;
775 }
776 
777 
778 // #pragma mark - DVI/SDVO/generic
779 
780 
781 DigitalPort::DigitalPort(port_index index, const char* baseName)
782 	:
783 	Port(index, baseName)
784 {
785 }
786 
787 
788 bool
789 DigitalPort::IsConnected()
790 {
791 	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
792 		_PortRegister());
793 
794 	// As this port overlaps with pretty much everything, this must be called
795 	// after having ruled out all other port types.
796 	return HasEDID();
797 }
798 
799 
800 addr_t
801 DigitalPort::_DDCRegister()
802 {
803 	//TODO: IS BROXTON, B = B, C = C, D = NIL
804 	switch (PortIndex()) {
805 		case INTEL_PORT_B:
806 			return INTEL_I2C_IO_E;
807 		case INTEL_PORT_C:
808 			return INTEL_I2C_IO_D;
809 		case INTEL_PORT_D:
810 			return INTEL_I2C_IO_F;
811 		default:
812 			return 0;
813 	}
814 
815 	return 0;
816 }
817 
818 
819 addr_t
820 DigitalPort::_PortRegister()
821 {
822 	switch (PortIndex()) {
823 		case INTEL_PORT_A:
824 			return INTEL_DIGITAL_PORT_A;
825 		case INTEL_PORT_B:
826 			return INTEL_DIGITAL_PORT_B;
827 		case INTEL_PORT_C:
828 			return INTEL_DIGITAL_PORT_C;
829 		default:
830 			return 0;
831 	}
832 	return 0;
833 }
834 
835 
836 status_t
837 DigitalPort::SetDisplayMode(display_mode* target, uint32 colorMode)
838 {
839 	CALLED();
840 	TRACE("%s: %s %dx%d\n", __func__, PortName(), target->timing.h_display,
841 		target->timing.v_display);
842 
843 	if (fPipe == NULL) {
844 		ERROR("%s: Setting display mode without assigned pipe!\n", __func__);
845 		return B_ERROR;
846 	}
847 
848 	// Setup PanelFitter and Train FDI if it exists
849 	PanelFitter* fitter = fPipe->PFT();
850 	if (fitter != NULL)
851 		fitter->Enable(target->timing);
852 	FDILink* link = fPipe->FDI();
853 	if (link != NULL)
854 		link->Train(&target->timing);
855 
856 	pll_divisors divisors;
857 	compute_pll_divisors(&target->timing, &divisors, false);
858 
859 	uint32 extraPLLFlags = 0;
860 	if (gInfo->shared_info->device_type.Generation() >= 3)
861 		extraPLLFlags |= DISPLAY_PLL_MODE_NORMAL | DISPLAY_PLL_2X_CLOCK;
862 
863 	// Program general pipe config
864 	fPipe->Configure(target);
865 
866 	// Program pipe PLL's
867 	fPipe->ConfigureClocks(divisors, target->timing.pixel_clock, extraPLLFlags);
868 
869 	// Program target display mode
870 	fPipe->ConfigureTimings(target);
871 
872 	// Set fCurrentMode to our set display mode
873 	memcpy(&fCurrentMode, target, sizeof(display_mode));
874 
875 	return B_OK;
876 }
877 
878 
879 // #pragma mark - LVDS Panel
880 // #pragma mark - HDMI
881 
882 
883 HDMIPort::HDMIPort(port_index index)
884 	:
885 	DigitalPort(index, "HDMI")
886 {
887 }
888 
889 
890 bool
891 HDMIPort::IsConnected()
892 {
893 	if (!gInfo->shared_info->device_type.SupportsHDMI())
894 		return false;
895 
896 	addr_t portRegister = _PortRegister();
897 	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
898 		portRegister);
899 
900 	if (portRegister == 0)
901 		return false;
902 
903 	//Notes:
904 	//- DISPLAY_MONITOR_PORT_DETECTED does only tell you *some* sort of digital display is
905 	//  connected to the port *if* you have the AUX channel stuff under power. It does not
906 	//  tell you which -type- of digital display is connected.
907 	//- Since we rely on the BIOS anyway, let's just use the conclusions it made for us :)
908 	//  Beware though: set_display_power_mode() uses this DISPLAY_MONITOR_PORT_ENABLED bit
909 	//  for DPMS as well. So we should better buffer our findings here for i.e. possible
910 	//  accelerant clones starting up. For DPMS there's currently no problem as this bit
911 	//  is only programmed for LVDS, DVI and VGA while we detect presence only for DP and HDMI.
912 	//
913 	//if ((read32(portRegister) & DISPLAY_MONITOR_PORT_DETECTED) == 0)
914 	if ((read32(portRegister) & DISPLAY_MONITOR_PORT_ENABLED) == 0)
915 		return false;
916 
917 	return HasEDID();
918 }
919 
920 
921 addr_t
922 HDMIPort::_PortRegister()
923 {
924 	// on PCH there's an additional port sandwiched in
925 	bool hasPCH = (gInfo->shared_info->pch_info != INTEL_PCH_NONE);
926 	bool fourthGen = gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV);
927 
928 	switch (PortIndex()) {
929 		case INTEL_PORT_B:
930 			if (fourthGen)
931 				return GEN4_HDMI_PORT_B;
932 			return hasPCH ? PCH_HDMI_PORT_B : INTEL_HDMI_PORT_B;
933 		case INTEL_PORT_C:
934 			if (fourthGen)
935 				return GEN4_HDMI_PORT_C;
936 			return hasPCH ? PCH_HDMI_PORT_C : INTEL_HDMI_PORT_C;
937 		case INTEL_PORT_D:
938 			if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_CHV))
939 				return CHV_HDMI_PORT_D;
940 			return hasPCH ? PCH_HDMI_PORT_D : 0;
941 		default:
942 			return 0;
943 		}
944 
945 	return 0;
946 }
947 
948 
949 // #pragma mark - DisplayPort
950 
951 
952 DisplayPort::DisplayPort(port_index index, const char* baseName)
953 	:
954 	Port(index, baseName)
955 {
956 }
957 
958 
959 pipe_index
960 DisplayPort::PipePreference()
961 {
962 	CALLED();
963 	if (gInfo->shared_info->device_type.Generation() <= 4)
964 		return INTEL_PIPE_ANY;
965 
966 	// Notes:
967 	// - The BIOSes seen sofar do not use PIPE C by default.
968 	// - Looks like BIOS selected Transcoder (A,B,C) is not always same as selected Pipe (A,B,C)
969 	//   so these should probably be handled seperately. For now this is OK as we don't touch
970 	//   the pipe for DisplayPort, only the transcoder..
971 	uint32 TranscoderPort = INTEL_TRANS_DP_PORT_NONE;
972 	switch (PortIndex()) {
973 		case INTEL_PORT_A:
974 			return INTEL_PIPE_ANY;
975 		case INTEL_PORT_B:
976 			TranscoderPort = INTEL_TRANS_DP_PORT_B;
977 			break;
978 		case INTEL_PORT_C:
979 			TranscoderPort = INTEL_TRANS_DP_PORT_C;
980 			break;
981 		case INTEL_PORT_D:
982 			TranscoderPort = INTEL_TRANS_DP_PORT_D;
983 			break;
984 		default:
985 			return INTEL_PIPE_ANY;
986 	}
987 
988 	for (uint32 Transcoder = 0; Transcoder < 3; Transcoder++) {
989 		if ((read32(INTEL_TRANSCODER_A_DP_CTL + (Transcoder << 12)) & INTEL_TRANS_DP_PORT_MASK) ==
990 			INTEL_TRANS_DP_PORT(TranscoderPort)) {
991 			switch (Transcoder) {
992 				case 0:
993 					return INTEL_PIPE_A;
994 				case 1:
995 					return INTEL_PIPE_B;
996 				case 2:
997 					return INTEL_PIPE_C;
998 			}
999 		}
1000 	}
1001 
1002 	return INTEL_PIPE_ANY;
1003 }
1004 
1005 
1006 bool
1007 DisplayPort::IsConnected()
1008 {
1009 	addr_t portRegister = _PortRegister();
1010 
1011 	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
1012 		portRegister);
1013 
1014 	if (portRegister == 0)
1015 		return false;
1016 
1017 	//Notes:
1018 	//- DISPLAY_MONITOR_PORT_DETECTED does only tell you *some* sort of digital display is
1019 	//  connected to the port *if* you have the AUX channel stuff under power. It does not
1020 	//  tell you which -type- of digital display is connected.
1021 	//- Since we rely on the BIOS anyway, let's just use the conclusions it made for us :)
1022 	//  Beware though: set_display_power_mode() uses this DISPLAY_MONITOR_PORT_ENABLED bit
1023 	//  for DPMS as well. So we should better buffer our findings here for i.e. possible
1024 	//  accelerant clones starting up. For DPMS there's currently no problem as this bit
1025 	//  is only programmed for LVDS, DVI and VGA while we detect presence only for DP and HDMI.
1026 	//
1027 	//if ((read32(portRegister) & DISPLAY_MONITOR_PORT_DETECTED) == 0) {
1028 	if ((read32(portRegister) & DISPLAY_MONITOR_PORT_ENABLED) == 0) {
1029 		TRACE("%s: %s link not detected\n", __func__, PortName());
1030 		return false;
1031 	}
1032 
1033 	//since EDID is not correctly implemented yet for this connection type we'll do without it for now
1034 	//return HasEDID();
1035 	TRACE("%s: %s link detected\n", __func__, PortName());
1036 	return true;
1037 }
1038 
1039 
1040 addr_t
1041 DisplayPort::_DDCRegister()
1042 {
1043 	// TODO: Do VLV + CHV use the VLV_DP_AUX_CTL_B + VLV_DP_AUX_CTL_C?
1044 	switch (PortIndex()) {
1045 		case INTEL_PORT_A:
1046 			return INTEL_DP_AUX_CTL_A;
1047 		case INTEL_PORT_B:
1048 			if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV))
1049 				return VLV_DP_AUX_CTL_B;
1050 			return INTEL_DP_AUX_CTL_B;
1051 		case INTEL_PORT_C:
1052 			if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV))
1053 				return VLV_DP_AUX_CTL_C;
1054 			return INTEL_DP_AUX_CTL_C;
1055 		case INTEL_PORT_D:
1056 			if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_CHV))
1057 				return CHV_DP_AUX_CTL_D;
1058 			else if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV))
1059 				return 0;
1060 			return INTEL_DP_AUX_CTL_D;
1061 		default:
1062 			return 0;
1063 	}
1064 
1065 	return 0;
1066 }
1067 
1068 
1069 addr_t
1070 DisplayPort::_PortRegister()
1071 {
1072 	// There are 6000 lines of intel linux code probing DP registers
1073 	// to properly detect DP vs eDP to then in-turn properly figure out
1074 	// what is DP and what is HDMI. It only takes 3 lines to
1075 	// ignore DisplayPort on ValleyView / CherryView
1076 
1077 	if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV)
1078 		|| gInfo->shared_info->device_type.InGroup(INTEL_GROUP_CHV)) {
1079 		ERROR("TODO: DisplayPort on ValleyView / CherryView");
1080 		return 0;
1081 	}
1082 
1083 	// Intel, are humans even involved anymore?
1084 	// This is a lot more complex than this code makes it look. (see defines)
1085 	// INTEL_DISPLAY_PORT_X moves around a lot based on PCH
1086 	// except on ValleyView and CherryView.
1087 	switch (PortIndex()) {
1088 		case INTEL_PORT_A:
1089 			return INTEL_DISPLAY_PORT_A;
1090 		case INTEL_PORT_B:
1091 			if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV))
1092 				return VLV_DISPLAY_PORT_B;
1093 			return INTEL_DISPLAY_PORT_B;
1094 		case INTEL_PORT_C:
1095 			if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV))
1096 				return VLV_DISPLAY_PORT_C;
1097 			return INTEL_DISPLAY_PORT_C;
1098 		case INTEL_PORT_D:
1099 			if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_CHV))
1100 				return CHV_DISPLAY_PORT_D;
1101 			else if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV))
1102 				return 0;
1103 			return INTEL_DISPLAY_PORT_D;
1104 		default:
1105 			return 0;
1106 	}
1107 
1108 	return 0;
1109 }
1110 
1111 
1112 status_t
1113 DisplayPort::_SetPortLinkGen4(const display_timing& timing)
1114 {
1115 	// Khz / 10. ( each output octet encoded as 10 bits.
1116 	//uint32 linkBandwidth = gInfo->shared_info->fdi_link_frequency * 1000 / 10; //=270000 khz
1117 	//fixme: always so?
1118 	uint32 linkBandwidth = 270000; //khz
1119 	uint32 fPipeOffset = 0;
1120 	if (fPipe->Index() == INTEL_PIPE_B)
1121 		fPipeOffset = 0x1000;
1122 
1123 	TRACE("%s: DP M1 data before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_DATA_M + fPipeOffset));
1124 	TRACE("%s: DP N1 data before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_DATA_N + fPipeOffset));
1125 	TRACE("%s: DP M1 link before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_LINK_M + fPipeOffset));
1126 	TRACE("%s: DP N1 link before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_LINK_N + fPipeOffset));
1127 
1128 	uint32 bitsPerPixel = 24;	//fixme: always so?
1129 	uint32 lanes = 4;			//fixme: always so?
1130 
1131 	//Setup Data M/N
1132 	uint64 linkspeed = lanes * linkBandwidth * 8;
1133 	uint64 ret_n = 1;
1134 	while(ret_n < linkspeed) {
1135 		ret_n *= 2;
1136 	}
1137 	if (ret_n > 0x800000) {
1138 		ret_n = 0x800000;
1139 	}
1140 	uint64 ret_m = timing.pixel_clock * ret_n * bitsPerPixel / linkspeed;
1141 	while ((ret_n > 0xffffff) || (ret_m > 0xffffff)) {
1142 		ret_m >>= 1;
1143 		ret_n >>= 1;
1144 	}
1145 	//Set TU size bits (to default, max) before link training so that error detection works
1146 	write32(INTEL_PIPE_A_DATA_M + fPipeOffset, ret_m | FDI_PIPE_MN_TU_SIZE_MASK);
1147 	write32(INTEL_PIPE_A_DATA_N + fPipeOffset, ret_n);
1148 
1149 	//Setup Link M/N
1150 	linkspeed = linkBandwidth;
1151 	ret_n = 1;
1152 	while(ret_n < linkspeed) {
1153 		ret_n *= 2;
1154 	}
1155 	if (ret_n > 0x800000) {
1156 		ret_n = 0x800000;
1157 	}
1158 	ret_m = timing.pixel_clock * ret_n / linkspeed;
1159 	while ((ret_n > 0xffffff) || (ret_m > 0xffffff)) {
1160 		ret_m >>= 1;
1161 		ret_n >>= 1;
1162 	}
1163 	write32(INTEL_PIPE_A_LINK_M + fPipeOffset, ret_m);
1164 	//Writing Link N triggers all four registers to be activated also (on next VBlank)
1165 	write32(INTEL_PIPE_A_LINK_N + fPipeOffset, ret_n);
1166 
1167 	TRACE("%s: DP M1 data after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_DATA_M + fPipeOffset));
1168 	TRACE("%s: DP N1 data after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_DATA_N + fPipeOffset));
1169 	TRACE("%s: DP M1 link after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_LINK_M + fPipeOffset));
1170 	TRACE("%s: DP N1 link after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_LINK_N + fPipeOffset));
1171 
1172 	return B_OK;
1173 }
1174 
1175 
1176 status_t
1177 DisplayPort::SetDisplayMode(display_mode* target, uint32 colorMode)
1178 {
1179 	CALLED();
1180 	TRACE("%s: %s %dx%d\n", __func__, PortName(), target->timing.h_display,
1181 		target->timing.v_display);
1182 
1183 	if (fPipe == NULL) {
1184 		ERROR("%s: Setting display mode without assigned pipe!\n", __func__);
1185 		return B_ERROR;
1186 	}
1187 
1188 	status_t result = B_OK;
1189 	if (gInfo->shared_info->device_type.Generation() <= 4) {
1190 		fPipe->ConfigureTimings(target);
1191 		result = _SetPortLinkGen4(target->timing);
1192 	} else {
1193 		//fixme: doesn't work yet. For now just scale to native mode.
1194 #if 0
1195 		// Setup PanelFitter and Train FDI if it exists
1196 		PanelFitter* fitter = fPipe->PFT();
1197 		if (fitter != NULL)
1198 			fitter->Enable(*target);
1199 		// skip FDI if it doesn't exist
1200 		if (gInfo->shared_info->device_type.Generation() <= 8) {
1201 			FDILink* link = fPipe->FDI();
1202 			if (link != NULL)
1203 				link->Train(target);
1204 		}
1205 		pll_divisors divisors;
1206 		compute_pll_divisors(target, &divisors, false);
1207 
1208 		uint32 extraPLLFlags = 0;
1209 		if (gInfo->shared_info->device_type.Generation() >= 3)
1210 			extraPLLFlags |= DISPLAY_PLL_MODE_NORMAL | DISPLAY_PLL_2X_CLOCK;
1211 
1212 		// Program general pipe config
1213 		fPipe->Configure(target);
1214 
1215 		// Program pipe PLL's
1216 		fPipe->ConfigureClocks(divisors, target->timing.pixel_clock, extraPLLFlags);
1217 
1218 		// Program target display mode
1219 		fPipe->ConfigureTimings(target);
1220 #endif
1221 
1222 		// Keep monitor at native mode and scale image to that
1223 		fPipe->ConfigureScalePos(target);
1224 	}
1225 
1226 	// Set fCurrentMode to our set display mode
1227 	memcpy(&fCurrentMode, target, sizeof(display_mode));
1228 
1229 	return result;
1230 }
1231 
1232 
1233 // #pragma mark - Embedded DisplayPort
1234 
1235 
1236 EmbeddedDisplayPort::EmbeddedDisplayPort()
1237 	:
1238 	DisplayPort(INTEL_PORT_A, "Embedded DisplayPort")
1239 {
1240 }
1241 
1242 
1243 bool
1244 EmbeddedDisplayPort::IsConnected()
1245 {
1246 	addr_t portRegister = _PortRegister();
1247 
1248 	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
1249 		portRegister);
1250 
1251 	if (!gInfo->shared_info->device_type.IsMobile()) {
1252 		TRACE("%s: skipping eDP on non-mobile GPU\n", __func__);
1253 		return false;
1254 	}
1255 
1256 	if ((read32(portRegister) & DISPLAY_MONITOR_PORT_DETECTED) == 0) {
1257 		TRACE("%s: %s link not detected\n", __func__, PortName());
1258 		return false;
1259 	}
1260 
1261 	HasEDID();
1262 
1263 	// If eDP has EDID, awesome. We use it.
1264 	// No EDID? The modesetting code falls back to VBIOS panel_mode
1265 	return true;
1266 }
1267 
1268 
1269 // #pragma mark - Digital Display Port
1270 
1271 
1272 DigitalDisplayInterface::DigitalDisplayInterface(port_index index,
1273 		const char* baseName)
1274 	:
1275 	Port(index, baseName)
1276 {
1277 	// As of Haswell, Intel decided to change eDP ports to a "DDI" bus...
1278 	// on a dare because the hardware engineers were drunk one night.
1279 }
1280 
1281 
1282 addr_t
1283 DigitalDisplayInterface::_PortRegister()
1284 {
1285 	// TODO: Linux does a DDI_BUF_CTL(INTEL_PORT_A) which is cleaner
1286 	// (but we have to ensure the offsets + region base is correct)
1287 	switch (PortIndex()) {
1288 		case INTEL_PORT_A:
1289 			return DDI_BUF_CTL_A;
1290 		case INTEL_PORT_B:
1291 			return DDI_BUF_CTL_B;
1292 		case INTEL_PORT_C:
1293 			return DDI_BUF_CTL_C;
1294 		case INTEL_PORT_D:
1295 			return DDI_BUF_CTL_D;
1296 		case INTEL_PORT_E:
1297 			return DDI_BUF_CTL_E;
1298 		case INTEL_PORT_F:
1299 			if ((gInfo->shared_info->device_type.Generation() > 8) &&
1300 				!gInfo->shared_info->device_type.InGroup(INTEL_GROUP_SKY))
1301 				return DDI_BUF_CTL_F;
1302 			return 0;
1303 		default:
1304 			return 0;
1305 	}
1306 	return 0;
1307 }
1308 
1309 
1310 addr_t
1311 DigitalDisplayInterface::_DDCRegister()
1312 {
1313 	switch (PortIndex()) {
1314 		case INTEL_PORT_B:
1315 			return INTEL_I2C_IO_E;
1316 		case INTEL_PORT_C:
1317 			return INTEL_I2C_IO_D;
1318 		case INTEL_PORT_D:
1319 			return INTEL_I2C_IO_F;
1320 		default:
1321 			return 0;
1322 	}
1323 
1324 	return 0;
1325 }
1326 
1327 
1328 status_t
1329 DigitalDisplayInterface::Power(bool enabled)
1330 {
1331 	if (fPipe == NULL) {
1332 		ERROR("%s: Setting power without assigned pipe!\n", __func__);
1333 		return B_ERROR;
1334 	}
1335 	TRACE("%s: %s DDI enabled: %s\n", __func__, PortName(),
1336 		enabled ? "true" : "false");
1337 
1338 	fPipe->Enable(enabled);
1339 
1340 	//nogo currently.. (kills output forever)
1341 #if 0
1342 	addr_t portRegister = _PortRegister();
1343 	uint32 state = read32(portRegister);
1344 	write32(portRegister,
1345 		enabled ? (state | DDI_BUF_CTL_ENABLE) : (state & ~DDI_BUF_CTL_ENABLE));
1346 	read32(portRegister);
1347 #endif
1348 
1349 	return B_OK;
1350 }
1351 
1352 
1353 bool
1354 DigitalDisplayInterface::IsConnected()
1355 {
1356 	addr_t portRegister = _PortRegister();
1357 
1358 	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
1359 		portRegister);
1360 
1361 	// Please note: Skylake and up (Desktop) might use eDP for a seperate active VGA converter chip.
1362 	if (portRegister == 0) {
1363 		TRACE("%s: Port not implemented\n", __func__);
1364 		return false;
1365 	}
1366 
1367 	// newer chipsets support 4 lanes on all ports
1368 	fMaxLanes = 4;
1369 	if ((gInfo->shared_info->device_type.Generation() < 9) ||
1370 		gInfo->shared_info->device_type.InGroup(INTEL_GROUP_SKY)) {
1371 		// Probe a little port info.
1372 		if ((read32(DDI_BUF_CTL_A) & DDI_A_4_LANES) != 0) {
1373 			switch (PortIndex()) {
1374 				case INTEL_PORT_A:
1375 					fMaxLanes = 4;
1376 					break;
1377 				case INTEL_PORT_E:
1378 					fMaxLanes = 0;
1379 					break;
1380 				default:
1381 					fMaxLanes = 4;
1382 					break;
1383 			}
1384 		} else {
1385 			switch (PortIndex()) {
1386 				case INTEL_PORT_A:
1387 					fMaxLanes = 2;
1388 					break;
1389 				case INTEL_PORT_E:
1390 					fMaxLanes = 2;
1391 					break;
1392 				default:
1393 					fMaxLanes = 4;
1394 					break;
1395 			}
1396 		}
1397 	}
1398 
1399 	TRACE("%s: %s Maximum Lanes: %" B_PRId8 "\n", __func__,
1400 		PortName(), fMaxLanes);
1401 
1402 	// fetch EDID but determine 'in use' later (below) so we also catch screens that fail EDID
1403 	HasEDID();
1404 
1405 	// scan all our pipes to find the one connected to the current port and check it's enabled
1406 	uint32 pipeState = 0;
1407 	for (uint32 pipeCnt = 0; pipeCnt < 4; pipeCnt++) {
1408 		switch (pipeCnt) {
1409 			case 0:
1410 				pipeState = read32(PIPE_DDI_FUNC_CTL_A);
1411 				break;
1412 			case 1:
1413 				pipeState = read32(PIPE_DDI_FUNC_CTL_B);
1414 				break;
1415 			case 2:
1416 				pipeState = read32(PIPE_DDI_FUNC_CTL_C);
1417 				break;
1418 			default:
1419 				pipeState = read32(PIPE_DDI_FUNC_CTL_EDP);
1420 				break;
1421 		}
1422 
1423 		if ((((pipeState & PIPE_DDI_SELECT_MASK) >> PIPE_DDI_SELECT_SHIFT) + 1) == (uint32)PortIndex()) {
1424 			// See if the BIOS enabled our output as it indicates it's in use
1425 			if (pipeState & PIPE_DDI_FUNC_CTL_ENABLE) {
1426 				TRACE("%s: Connected\n", __func__);
1427 				return true;
1428 			}
1429 		}
1430 	}
1431 
1432 	// On laptops we always have an internal panel.. (this is on the eDP port)
1433 	if (gInfo->shared_info->device_type.IsMobile() && (PortIndex() == INTEL_PORT_E)) {
1434 		if (gInfo->shared_info->has_vesa_edid_info) {
1435 			TRACE("%s: Laptop. Using VESA edid info\n", __func__);
1436 			memcpy(&fEDIDInfo, &gInfo->shared_info->vesa_edid_info,
1437 				sizeof(edid1_info));
1438 			fEDIDState = B_OK;
1439 			// HasEDID now true
1440 			return true;
1441 		} else if (gInfo->shared_info->got_vbt) {
1442 			TRACE("%s: Laptop. No EDID, but force enabled as we have a VBT\n", __func__);
1443 			return true;
1444 		}
1445 	}
1446 
1447 	TRACE("%s: Not connected\n", __func__);
1448 	return false;
1449 }
1450 
1451 status_t
1452 DigitalDisplayInterface::_SetPortLinkGen8(const display_timing& timing, uint32 pllSel)
1453 {
1454 	//fixme: always so on pre gen 9?
1455 	uint32 linkBandwidth = 270000; //khz
1456 
1457 	if (gInfo->shared_info->device_type.Generation() >= 9) {
1458 		if (pllSel != 0xff) {
1459 			linkBandwidth = (read32(SKL_DPLL_CTRL1) >> (1 + 6 * pllSel)) & SKL_DPLL_DP_LINKRATE_MASK;
1460 			switch (linkBandwidth) {
1461 				case SKL_DPLL_CTRL1_2700:
1462 					linkBandwidth = 2700000 / 5;
1463 					break;
1464 				case SKL_DPLL_CTRL1_1350:
1465 					linkBandwidth = 1350000 / 5;
1466 					break;
1467 				case SKL_DPLL_CTRL1_810:
1468 					linkBandwidth =  810000 / 5;
1469 					break;
1470 				case SKL_DPLL_CTRL1_1620:
1471 					linkBandwidth = 1620000 / 5;
1472 					break;
1473 				case SKL_DPLL_CTRL1_1080:
1474 					linkBandwidth = 1080000 / 5;
1475 					break;
1476 				case SKL_DPLL_CTRL1_2160:
1477 					linkBandwidth = 2160000 / 5;
1478 					break;
1479 				default:
1480 					linkBandwidth = 270000;
1481 					ERROR("%s: DDI No known DP-link reference clock selected, assuming default\n", __func__);
1482 					break;
1483 			}
1484 		} else {
1485 			ERROR("%s: DDI No known PLL selected, assuming default DP-link reference\n", __func__);
1486 		}
1487 	}
1488 	TRACE("%s: DDI DP-link reference clock is %gMhz\n", __func__, linkBandwidth / 1000.0f);
1489 
1490 	uint32 fPipeOffset = 0;
1491 	switch (fPipe->Index()) {
1492 		case INTEL_PIPE_B:
1493 			fPipeOffset = 0x1000;
1494 			break;
1495 		case INTEL_PIPE_C:
1496 			fPipeOffset = 0x2000;
1497 			break;
1498 		case INTEL_PIPE_D:
1499 			fPipeOffset = 0xf000;
1500 			break;
1501 		default:
1502 			break;
1503 	}
1504 
1505 	TRACE("%s: DDI M1 data before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_DATA_M + fPipeOffset));
1506 	TRACE("%s: DDI N1 data before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_DATA_N + fPipeOffset));
1507 	TRACE("%s: DDI M1 link before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_LINK_M + fPipeOffset));
1508 	TRACE("%s: DDI N1 link before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_LINK_N + fPipeOffset));
1509 
1510 	uint32 pipeFunc = read32(PIPE_DDI_FUNC_CTL_A + fPipeOffset);
1511 	uint32 bitsPerPixel = (pipeFunc & PIPE_DDI_BPC_MASK) >> PIPE_DDI_COLOR_SHIFT;
1512 	switch (bitsPerPixel) {
1513 		case PIPE_DDI_8BPC:
1514 			bitsPerPixel = 24;
1515 			break;
1516 		case PIPE_DDI_10BPC:
1517 			bitsPerPixel = 30;
1518 			break;
1519 		case PIPE_DDI_6BPC:
1520 			bitsPerPixel = 18;
1521 			break;
1522 		case PIPE_DDI_12BPC:
1523 			bitsPerPixel = 36;
1524 			break;
1525 		default:
1526 			ERROR("%s: DDI illegal link colordepth set.\n", __func__);
1527 			return B_ERROR;
1528 	}
1529 	TRACE("%s: DDI Link Colordepth: %" B_PRIu32 "\n", __func__, bitsPerPixel);
1530 
1531 	uint32 lanes = 4;
1532 	// Only DP modes supports less than 4 lanes: read current config
1533 	if (((pipeFunc & PIPE_DDI_MODESEL_MASK) >> PIPE_DDI_MODESEL_SHIFT) >= PIPE_DDI_MODE_DP_SST) {
1534 		// On gen 9.5 IceLake 3x mode exists (DSI only), earlier models: reserved value.
1535 		lanes = ((pipeFunc & PIPE_DDI_DP_WIDTH_MASK) >> PIPE_DDI_DP_WIDTH_SHIFT) + 1;
1536 		TRACE("%s: DDI in DP mode with %" B_PRIx32 " lanes in use\n", __func__, lanes);
1537 	} else {
1538 		TRACE("%s: DDI in non-DP mode with %" B_PRIx32 " lanes in use\n", __func__, lanes);
1539 	}
1540 
1541 	//Setup Data M/N
1542 	uint64 linkspeed = lanes * linkBandwidth * 8;
1543 	uint64 ret_n = 1;
1544 	while(ret_n < linkspeed) {
1545 		ret_n *= 2;
1546 	}
1547 	if (ret_n > 0x800000) {
1548 		ret_n = 0x800000;
1549 	}
1550 	uint64 ret_m = timing.pixel_clock * ret_n * bitsPerPixel / linkspeed;
1551 	while ((ret_n > 0xffffff) || (ret_m > 0xffffff)) {
1552 		ret_m >>= 1;
1553 		ret_n >>= 1;
1554 	}
1555 	//Set TU size bits (to default, max) before link training so that error detection works
1556 	write32(INTEL_DDI_PIPE_A_DATA_M + fPipeOffset, ret_m | FDI_PIPE_MN_TU_SIZE_MASK);
1557 	write32(INTEL_DDI_PIPE_A_DATA_N + fPipeOffset, ret_n);
1558 
1559 	//Setup Link M/N
1560 	linkspeed = linkBandwidth;
1561 	ret_n = 1;
1562 	while(ret_n < linkspeed) {
1563 		ret_n *= 2;
1564 	}
1565 	if (ret_n > 0x800000) {
1566 		ret_n = 0x800000;
1567 	}
1568 	ret_m = timing.pixel_clock * ret_n / linkspeed;
1569 	while ((ret_n > 0xffffff) || (ret_m > 0xffffff)) {
1570 		ret_m >>= 1;
1571 		ret_n >>= 1;
1572 	}
1573 	write32(INTEL_DDI_PIPE_A_LINK_M + fPipeOffset, ret_m);
1574 	//Writing Link N triggers all four registers to be activated also (on next VBlank)
1575 	write32(INTEL_DDI_PIPE_A_LINK_N + fPipeOffset, ret_n);
1576 
1577 	TRACE("%s: DDI M1 data after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_DATA_M + fPipeOffset));
1578 	TRACE("%s: DDI N1 data after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_DATA_N + fPipeOffset));
1579 	TRACE("%s: DDI M1 link after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_LINK_M + fPipeOffset));
1580 	TRACE("%s: DDI N1 link after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_LINK_N + fPipeOffset));
1581 
1582 	return B_OK;
1583 }
1584 
1585 status_t
1586 DigitalDisplayInterface::SetDisplayMode(display_mode* target, uint32 colorMode)
1587 {
1588 	CALLED();
1589 	TRACE("%s: %s %dx%d\n", __func__, PortName(), target->timing.h_display,
1590 		target->timing.v_display);
1591 
1592 	if (fPipe == NULL) {
1593 		ERROR("%s: Setting display mode without assigned pipe!\n", __func__);
1594 		return B_ERROR;
1595 	}
1596 
1597 	display_timing hardwareTarget = target->timing;
1598 	bool needsScaling = false;
1599 	if ((PortIndex() == INTEL_PORT_E) && gInfo->shared_info->device_type.IsMobile()) {
1600 		// For internal panels, we may need to set the timings according to the panel
1601 		// native video mode, and let the panel fitter do the scaling.
1602 
1603 		if (gInfo->shared_info->got_vbt) {
1604 			// Set vbios hardware panel mode as base
1605 			hardwareTarget = gInfo->shared_info->panel_timing;
1606 
1607 			if (hardwareTarget.h_display == target->timing.h_display
1608 					&& hardwareTarget.v_display == target->timing.v_display) {
1609 				// We are setting the native video mode, nothing special to do
1610 				// Note: this means refresh and timing might vary according to requested mode.
1611 				hardwareTarget = target->timing;
1612 				TRACE("%s: Setting internal panel to native resolution at %" B_PRIu32 "Hz\n", __func__,
1613 					hardwareTarget.pixel_clock * 1000 / (hardwareTarget.h_total * hardwareTarget.v_total));
1614 			} else {
1615 				// We need to enable the panel fitter
1616 				TRACE("%s: Hardware mode will actually be %dx%d at %" B_PRIu32 "Hz\n", __func__,
1617 					hardwareTarget.h_display, hardwareTarget.v_display,
1618 					hardwareTarget.pixel_clock * 1000 / (hardwareTarget.h_total * hardwareTarget.v_total));
1619 
1620 				// FIXME we should also get the refresh frequency from the target
1621 				// mode, and then "sanitize" the resulting mode we made up.
1622 				needsScaling = true;
1623 			}
1624 		} else {
1625 			TRACE("%s: Setting internal panel mode without VBT info generation, scaling may not work\n",
1626 				__func__);
1627 			// We don't have VBT data, try to set the requested mode directly
1628 			// and hope for the best
1629 			hardwareTarget = target->timing;
1630 		}
1631 	}
1632 
1633 	// Setup PanelFitter
1634 	PanelFitter* fitter = fPipe->PFT();
1635 	if (fitter != NULL)
1636 		fitter->Enable(hardwareTarget);
1637 
1638 	// skip FDI as it never applies to DDI (on gen7 and 8 only for the real analog VGA port)
1639 
1640 	// Program general pipe config
1641 	fPipe->Configure(target);
1642 
1643 	uint32 pllSel = 0xff; // no PLL selected
1644 	if (gInfo->shared_info->device_type.Generation() <= 8) {
1645 		unsigned int r2_out, n2_out, p_out;
1646 		hsw_ddi_calculate_wrpll(
1647 			hardwareTarget.pixel_clock * 1000 /* in Hz */,
1648 			&r2_out, &n2_out, &p_out);
1649 	} else {
1650 		skl_wrpll_params wrpll_params;
1651 		skl_ddi_calculate_wrpll(
1652 			hardwareTarget.pixel_clock * 1000 /* in Hz */,
1653 			gInfo->shared_info->pll_info.reference_frequency,
1654 			&wrpll_params);
1655 		fPipe->ConfigureClocksSKL(wrpll_params,
1656 			hardwareTarget.pixel_clock,
1657 			PortIndex(),
1658 			&pllSel);
1659 	}
1660 
1661 	// Program target display mode
1662 	fPipe->ConfigureTimings(target, !needsScaling);
1663 	_SetPortLinkGen8(hardwareTarget, pllSel);
1664 
1665 	// Set fCurrentMode to our set display mode
1666 	memcpy(&fCurrentMode, target, sizeof(display_mode));
1667 
1668 	return B_OK;
1669 }
1670