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 *		Rudolf Cornelissen, ruud@highsand-juicylake.nl
10 */
11
12
13#include "Ports.h"
14
15#include <ddc.h>
16#include <dp_raw.h>
17#include <stdlib.h>
18#include <string.h>
19#include <Debug.h>
20#include <KernelExport.h>
21
22#include "accelerant.h"
23#include "accelerant_protos.h"
24#include "FlexibleDisplayInterface.h"
25#include "intel_extreme.h"
26#include "PanelFitter.h"
27
28#include <new>
29
30
31#undef TRACE
32#define TRACE_PORTS
33#ifdef TRACE_PORTS
34#   define TRACE(x...) _sPrintf("intel_extreme: " x)
35#else
36#   define TRACE(x...)
37#endif
38
39#define ERROR(x...) _sPrintf("intel_extreme: " x)
40#define CALLED(x...) TRACE("CALLED %s\n", __PRETTY_FUNCTION__)
41
42
43static bool
44wait_for_set(addr_t address, uint32 mask, uint32 timeout)
45{
46	int interval = 50;
47	uint32 i = 0;
48	for(i = 0; i <= timeout; i += interval) {
49		spin(interval);
50		if ((read32(address) & mask) != 0)
51			return true;
52	}
53	return false;
54}
55
56
57static bool
58wait_for_clear(addr_t address, uint32 mask, uint32 timeout)
59{
60	int interval = 50;
61	uint32 i = 0;
62	for(i = 0; i <= timeout; i += interval) {
63		spin(interval);
64		if ((read32(address) & mask) == 0)
65			return true;
66	}
67	return false;
68}
69
70
71static uint32
72wait_for_clear_status(addr_t address, uint32 mask, uint32 timeout)
73{
74	int interval = 50;
75	uint32 i = 0;
76	uint32 status = 0;
77	for(i = 0; i <= timeout; i += interval) {
78		spin(interval);
79		status = read32(address);
80		if ((status & mask) == 0)
81			return status;
82	}
83	return status;
84}
85
86
87Port::Port(port_index index, const char* baseName)
88	:
89	fPipe(NULL),
90	fEDIDState(B_NO_INIT),
91	fPortIndex(index),
92	fPortName(NULL)
93{
94	char portID[2];
95	portID[0] = 'A' + index - INTEL_PORT_A;
96	portID[1] = 0;
97
98	char buffer[32];
99	buffer[0] = 0;
100
101	strlcat(buffer, baseName, sizeof(buffer));
102	strlcat(buffer, " ", sizeof(buffer));
103	strlcat(buffer, portID, sizeof(buffer));
104	fPortName = strdup(buffer);
105}
106
107
108Port::~Port()
109{
110	free(fPortName);
111}
112
113
114bool
115Port::HasEDID()
116{
117	if (fEDIDState == B_NO_INIT)
118		GetEDID(NULL);
119
120	return fEDIDState == B_OK;
121}
122
123
124status_t
125Port::SetPipe(Pipe* pipe)
126{
127	CALLED();
128
129	if (pipe == NULL) {
130		ERROR("%s: Invalid pipe provided!\n", __func__);
131		return B_ERROR;
132	}
133
134	uint32 portRegister = _PortRegister();
135	if (portRegister == 0) {
136		ERROR("%s: Invalid PortRegister ((0x%" B_PRIx32 ") for %s\n", __func__,
137			portRegister, PortName());
138		return B_ERROR;
139	}
140
141	// TODO: UnAssignPipe?  This likely needs reworked a little
142	if (fPipe != NULL) {
143		ERROR("%s: Can't reassign display pipe (yet)\n", __func__);
144		return B_ERROR;
145	}
146
147	switch (pipe->Index()) {
148		case INTEL_PIPE_B:
149			TRACE("%s: Assigning %s (0x%" B_PRIx32 ") to pipe B\n", __func__,
150				PortName(), portRegister);
151			break;
152		case INTEL_PIPE_C:
153			TRACE("%s: Assigning %s (0x%" B_PRIx32 ") to pipe C\n", __func__,
154				PortName(), portRegister);
155			break;
156		case INTEL_PIPE_D:
157			TRACE("%s: Assigning %s (0x%" B_PRIx32 ") to pipe D\n", __func__,
158				PortName(), portRegister);
159			break;
160		default:
161			TRACE("%s: Assigning %s (0x%" B_PRIx32 ") to pipe A\n", __func__,
162				PortName(), portRegister);
163			break;
164	}
165
166	uint32 portState = read32(portRegister);
167
168	// generation 6 gfx SandyBridge/SNB non-DP use the same 2 bits on all ports (eDP = 1 bit).
169	// generation 7 gfx IvyBridge/IVB non-DP use the same 2 bits on all ports (eDP = 2 bits).
170	// DP ports/all DDI ports: Pipe selections works differently, via own SetPipe() implementation.
171	if (gInfo->shared_info->pch_info == INTEL_PCH_CPT) {
172		portState &= ~PORT_TRANS_SEL_MASK;
173		switch (pipe->Index()) {
174			case INTEL_PIPE_B:
175				write32(portRegister, portState | PORT_TRANS_B_SEL_CPT);
176				break;
177			case INTEL_PIPE_C:
178				write32(portRegister, portState | PORT_TRANS_C_SEL_CPT);
179				break;
180			default:
181				write32(portRegister, portState | PORT_TRANS_A_SEL_CPT);
182				break;
183		}
184	} else {
185		// generation 3/4/5 gfx uses the same single bit on all ports
186		if (pipe->Index() == INTEL_PIPE_A)
187			write32(portRegister, portState & ~DISPLAY_MONITOR_PIPE_B);
188		else
189			write32(portRegister, portState | DISPLAY_MONITOR_PIPE_B);
190	}
191	fPipe = pipe;
192
193	if (fPipe == NULL)
194		return B_NO_MEMORY;
195
196	// Disable display pipe until modesetting enables it
197	if (fPipe->IsEnabled())
198		fPipe->Enable(false);
199
200	read32(portRegister);
201
202	return B_OK;
203}
204
205
206status_t
207Port::Power(bool enabled)
208{
209	if (fPipe == NULL) {
210		ERROR("%s: Setting power mode without assigned pipe!\n", __func__);
211		return B_ERROR;
212	}
213
214	fPipe->Enable(enabled);
215
216	return B_OK;
217}
218
219
220status_t
221Port::GetEDID(edid1_info* edid, bool forceRead)
222{
223	CALLED();
224
225	if (fEDIDState == B_NO_INIT || forceRead) {
226		TRACE("%s: trying to read EDID\n", PortName());
227
228		i2c_bus bus;
229		if (SetupI2c(&bus) != B_OK)
230			return fEDIDState;
231
232		fEDIDState = ddc2_read_edid1(&bus, &fEDIDInfo, NULL, NULL);
233
234		if (fEDIDState == B_OK) {
235			TRACE("%s: found EDID information!\n", PortName());
236			edid_dump(&fEDIDInfo);
237		} else if (SetupI2cFallback(&bus) == B_OK) {
238			fEDIDState = ddc2_read_edid1(&bus, &fEDIDInfo, NULL, NULL);
239
240			if (fEDIDState == B_OK) {
241				TRACE("%s: found EDID information!\n", PortName());
242				edid_dump(&fEDIDInfo);
243			}
244		}
245	}
246
247	if (fEDIDState != B_OK) {
248		TRACE("%s: no EDID information found.\n", PortName());
249		return fEDIDState;
250	}
251
252	if (edid != NULL)
253		memcpy(edid, &fEDIDInfo, sizeof(edid1_info));
254
255	return B_OK;
256}
257
258
259status_t
260Port::SetupI2c(i2c_bus *bus)
261{
262	addr_t ddcRegister = _DDCRegister();
263	if (ddcRegister == 0) {
264		TRACE("%s: no DDC register found\n", PortName());
265		fEDIDState = B_ERROR;
266		return fEDIDState;
267	}
268
269	TRACE("%s: using ddc @ 0x%" B_PRIxADDR "\n", PortName(), ddcRegister);
270
271	ddc2_init_timing(bus);
272	bus->cookie = (void*)ddcRegister;
273	bus->set_signals = &_SetI2CSignals;
274	bus->get_signals = &_GetI2CSignals;
275
276	return B_OK;
277}
278
279
280status_t
281Port::SetupI2cFallback(i2c_bus *bus)
282{
283	return B_ERROR;
284}
285
286
287status_t
288Port::GetPLLLimits(pll_limits& limits)
289{
290	return B_ERROR;
291}
292
293
294pipe_index
295Port::PipePreference()
296{
297	CALLED();
298	// Ideally we could just return INTEL_PIPE_ANY for all devices by default, but
299	// this doesn't quite work yet. We need to use the BIOS presetup pipes for now.
300	if (gInfo->shared_info->device_type.Generation() < 4)
301		return INTEL_PIPE_ANY;
302
303	// Notes:
304	// - The BIOSes seen sofar do not use PIPE C by default.
305	// - The BIOSes seen sofar program transcoder A to PIPE A, etc.
306	// - Later devices add a pipe C alongside the added transcoder C.
307
308	if ((gInfo->shared_info->device_type.Generation() <= 7) &&
309		(!gInfo->shared_info->device_type.HasDDI())) {
310		uint32 portState = read32(_PortRegister());
311		if (gInfo->shared_info->pch_info == INTEL_PCH_CPT) {
312			portState &= PORT_TRANS_SEL_MASK;
313			if (portState == PORT_TRANS_B_SEL_CPT)
314				return INTEL_PIPE_B;
315			else
316				return INTEL_PIPE_A;
317		} else {
318			if (portState & DISPLAY_MONITOR_PIPE_B)
319				return INTEL_PIPE_B;
320			else
321				return INTEL_PIPE_A;
322		}
323	}
324
325	if (gInfo->shared_info->device_type.HasDDI()) {
326		// scan all our pipes to find the one connected to the current port
327		uint32 pipeState = 0;
328		for (uint32 pipeCnt = 0; pipeCnt < 4; pipeCnt++) {
329			switch (pipeCnt) {
330				case 0:
331					pipeState = read32(PIPE_DDI_FUNC_CTL_A);
332					break;
333				case 1:
334					pipeState = read32(PIPE_DDI_FUNC_CTL_B);
335					break;
336				case 2:
337					pipeState = read32(PIPE_DDI_FUNC_CTL_C);
338					break;
339				default:
340					pipeState = read32(PIPE_DDI_FUNC_CTL_EDP);
341					break;
342			}
343
344			if ((((pipeState & PIPE_DDI_SELECT_MASK) >> PIPE_DDI_SELECT_SHIFT) + 1)
345				== (uint32)PortIndex()) {
346				switch (pipeCnt) {
347					case 0:
348						return INTEL_PIPE_A;
349					case 1:
350						return INTEL_PIPE_B;
351					case 2:
352						return INTEL_PIPE_C;
353					default:
354						return INTEL_PIPE_D;
355				}
356			}
357		}
358	}
359
360	return INTEL_PIPE_ANY;
361}
362
363
364status_t
365Port::_GetI2CSignals(void* cookie, int* _clock, int* _data)
366{
367	addr_t ioRegister = (addr_t)cookie;
368	uint32 value = read32(ioRegister);
369
370	*_clock = (value & I2C_CLOCK_VALUE_IN) != 0;
371	*_data = (value & I2C_DATA_VALUE_IN) != 0;
372
373	return B_OK;
374}
375
376
377status_t
378Port::_SetI2CSignals(void* cookie, int clock, int data)
379{
380	addr_t ioRegister = (addr_t)cookie;
381	uint32 value;
382
383	if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_83x)) {
384		// on these chips, the reserved values are fixed
385		value = 0;
386	} else {
387		// on all others, we have to preserve them manually
388		value = read32(ioRegister) & I2C_RESERVED;
389	}
390
391	// if we send clk or data, we always send low logic level;
392	// if we want to send high level, we actually receive and let the
393	// external pullup resistors create the high level on the bus.
394	value |= I2C_DATA_VALUE_MASK;  //sets data = 0, always latch
395	value |= I2C_CLOCK_VALUE_MASK; //sets clock = 0, always latch
396
397	if (data != 0)
398		value |= I2C_DATA_DIRECTION_MASK;
399	else {
400		value |= I2C_DATA_DIRECTION_MASK | I2C_DATA_DIRECTION_OUT;
401	}
402
403	if (clock != 0)
404		value |= I2C_CLOCK_DIRECTION_MASK;
405	else {
406		value |= I2C_CLOCK_DIRECTION_MASK | I2C_CLOCK_DIRECTION_OUT;
407	}
408
409	write32(ioRegister, value);
410	read32(ioRegister);
411		// make sure the PCI bus has flushed the write
412
413	return B_OK;
414}
415
416
417bool
418Port::_IsPortInVBT(uint32* foundIndex)
419{
420	// check VBT mapping
421	bool found = false;
422	const uint32 deviceConfigCount = gInfo->shared_info->device_config_count;
423	for (uint32 i = 0; i < deviceConfigCount; i++) {
424		child_device_config& config = gInfo->shared_info->device_configs[i];
425		if (config.dvo_port > DVO_PORT_HDMII) {
426			ERROR("%s: DVO port unknown\n", __func__);
427			continue;
428		}
429		dvo_port port = (dvo_port)config.dvo_port;
430		switch (PortIndex()) {
431			case INTEL_PORT_A:
432				found = port == DVO_PORT_HDMIA || port == DVO_PORT_DPA;
433				break;
434			case INTEL_PORT_B:
435				found = port == DVO_PORT_HDMIB || port == DVO_PORT_DPB;
436				break;
437			case INTEL_PORT_C:
438				found = port == DVO_PORT_HDMIC || port == DVO_PORT_DPC;
439				break;
440			case INTEL_PORT_D:
441				found = port == DVO_PORT_HDMID || port == DVO_PORT_DPD;
442				break;
443			case INTEL_PORT_E:
444				found = port == DVO_PORT_HDMIE || port == DVO_PORT_DPE || port == DVO_PORT_CRT;
445				break;
446			case INTEL_PORT_F:
447				found = port == DVO_PORT_HDMIF || port == DVO_PORT_DPF;
448				break;
449			case INTEL_PORT_G:
450				found = port == DVO_PORT_HDMIG || port == DVO_PORT_DPG;
451				break;
452			default:
453				ERROR("%s: DDI port unknown\n", __func__);
454				break;
455		}
456		if (found) {
457			if (foundIndex != NULL)
458				*foundIndex = i;
459			break;
460		}
461	}
462	return found;
463}
464
465
466bool
467Port::_IsDisplayPortInVBT()
468{
469	uint32 foundIndex = 0;
470	if (!_IsPortInVBT(&foundIndex))
471		return false;
472	child_device_config& config = gInfo->shared_info->device_configs[foundIndex];
473	return config.aux_channel > 0 && (config.device_type & DEVICE_TYPE_DISPLAYPORT_OUTPUT) != 0;
474}
475
476
477bool
478Port::_IsHdmiInVBT()
479{
480	uint32 foundIndex = 0;
481	if (!_IsPortInVBT(&foundIndex))
482		return false;
483	child_device_config& config = gInfo->shared_info->device_configs[foundIndex];
484	return config.ddc_pin > 0 && ((config.device_type & DEVICE_TYPE_NOT_HDMI_OUTPUT) == 0
485			|| (config.device_type & DEVICE_TYPE_TMDS_DVI_SIGNALING) != 0);
486}
487
488
489bool
490Port::_IsEDPPort()
491{
492	uint32 foundIndex = 0;
493	if (!_IsPortInVBT(&foundIndex))
494		return false;
495	child_device_config& config = gInfo->shared_info->device_configs[foundIndex];
496	return (config.device_type & (DEVICE_TYPE_INTERNAL_CONNECTOR | DEVICE_TYPE_DISPLAYPORT_OUTPUT))
497		== (DEVICE_TYPE_INTERNAL_CONNECTOR | DEVICE_TYPE_DISPLAYPORT_OUTPUT);
498}
499
500
501addr_t
502Port::_DDCPin()
503{
504	uint32 foundIndex = 0;
505	if (!_IsPortInVBT(&foundIndex))
506		return 0;
507	child_device_config& config = gInfo->shared_info->device_configs[foundIndex];
508	if (gInfo->shared_info->pch_info >= INTEL_PCH_ICP) {
509		switch (config.ddc_pin) {
510			case 1:
511				return INTEL_I2C_IO_A;
512			case 2:
513				return INTEL_I2C_IO_B;
514			case 3:
515				return INTEL_I2C_IO_C;
516			case 4:
517				return INTEL_I2C_IO_I;
518			case 5:
519				return INTEL_I2C_IO_J;
520			case 6:
521				return INTEL_I2C_IO_K;
522			case 7:
523				return INTEL_I2C_IO_L;
524			case 8:
525				return INTEL_I2C_IO_M;
526			case 9:
527				return INTEL_I2C_IO_N;
528			default:
529				return 0;
530		}
531	} else if (gInfo->shared_info->pch_info >= INTEL_PCH_CNP) {
532		switch (config.ddc_pin) {
533			case 1:
534				return INTEL_I2C_IO_A;
535			case 2:
536				return INTEL_I2C_IO_B;
537			case 3:
538				return INTEL_I2C_IO_D;
539			case 4:
540				return INTEL_I2C_IO_C;
541			default:
542				return 0;
543		}
544	} else if (gInfo->shared_info->device_type.Generation() == 9) {
545		switch (config.ddc_pin) {
546			case 4:
547				return INTEL_I2C_IO_D;
548			case 5:
549				return INTEL_I2C_IO_E;
550			case 6:
551				return INTEL_I2C_IO_F;
552			default:
553				return 0;
554		}
555	} else if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_BDW)) {
556		switch (config.ddc_pin) {
557			case 2:
558				return INTEL_I2C_IO_A;
559			case 4:
560				return INTEL_I2C_IO_D;
561			case 5:
562				return INTEL_I2C_IO_E;
563			case 6:
564				return INTEL_I2C_IO_F;
565			default:
566				return 0;
567		}
568	} else {
569		switch (config.ddc_pin) {
570			case 1:
571				return INTEL_I2C_IO_B;
572			case 2:
573				return INTEL_I2C_IO_A;
574			case 3:
575				return INTEL_I2C_IO_C;
576			case 4:
577				return INTEL_I2C_IO_D;
578			case 5:
579				return INTEL_I2C_IO_E;
580			case 6:
581				return INTEL_I2C_IO_F;
582			default:
583				return 0;
584		}
585	}
586
587}
588
589
590status_t
591Port::_SetupDpAuxI2c(i2c_bus *bus)
592{
593	CALLED();
594
595	ddc2_init_timing(bus);
596	bus->cookie = this;
597	bus->send_receive = &_DpAuxSendReceiveHook;
598
599	if (gInfo->shared_info->device_type.Generation() >= 11) {
600		uint32 value = read32(ICL_PWR_WELL_CTL_AUX2);
601		if ((value & HSW_PWR_WELL_CTL_STATE(0)) != 0)
602			return B_OK;
603
604		write32(ICL_PWR_WELL_CTL_AUX2, value | HSW_PWR_WELL_CTL_REQ(0));
605		if (!wait_for_set(ICL_PWR_WELL_CTL_AUX2, HSW_PWR_WELL_CTL_STATE(0), 1000))
606			ERROR("%s: %s AUX didn't power on within 1000us!\n", __func__, PortName());
607	}
608	return B_OK;
609}
610
611
612status_t
613Port::_DpAuxSendReceive(uint32 slaveAddress,
614	const uint8 *writeBuffer, size_t writeLength, uint8 *readBuffer, size_t readLength)
615{
616	size_t transferLength = 16;
617
618	dp_aux_msg message;
619	memset(&message, 0, sizeof(message));
620
621	if (writeBuffer != NULL) {
622		message.address = slaveAddress;
623		message.buffer = NULL;
624		message.request = DP_AUX_I2C_WRITE;
625		message.size = 0;
626		ssize_t result = _DpAuxTransfer(&message);
627		if (result < 0)
628			return result;
629
630		for (size_t i = 0; i < writeLength;) {
631			message.buffer = (void*)(writeBuffer + i);
632			message.size = min_c(transferLength, writeLength - i);
633			// Middle-Of-Transmission on final transaction
634			if (writeLength - i > transferLength)
635				message.request |= DP_AUX_I2C_MOT;
636			else
637				message.request &= ~DP_AUX_I2C_MOT;
638
639			for (int attempt = 0; attempt < 7; attempt++) {
640				ssize_t result = _DpAuxTransfer(&message);
641				if (result < 0) {
642					ERROR("%s: aux_ch transaction failed!\n", __func__);
643					return result;
644				}
645
646				switch (message.reply & DP_AUX_I2C_REPLY_MASK) {
647					case DP_AUX_I2C_REPLY_ACK:
648						goto nextWrite;
649					case DP_AUX_I2C_REPLY_NACK:
650						TRACE("%s: aux i2c nack\n", __func__);
651						return B_IO_ERROR;
652					case DP_AUX_I2C_REPLY_DEFER:
653						TRACE("%s: aux i2c defer\n", __func__);
654						snooze(400);
655						break;
656					default:
657						TRACE("%s: aux invalid I2C reply: 0x%02x\n",
658							__func__, message.reply);
659						return B_ERROR;
660				}
661			}
662nextWrite:
663			if (result < 0)
664				return result;
665			i += message.size;
666		}
667	}
668
669
670	if (readBuffer != NULL) {
671		message.address = slaveAddress;
672		message.buffer = NULL;
673		message.request = DP_AUX_I2C_READ;
674		message.size = 0;
675		ssize_t result = _DpAuxTransfer(&message);
676		if (result < 0)
677			return result;
678
679		for (size_t i = 0; i < readLength;) {
680			message.buffer = readBuffer + i;
681			message.size = min_c(transferLength, readLength - i);
682			// Middle-Of-Transmission on final transaction
683			if (readLength - i > transferLength)
684				message.request |= DP_AUX_I2C_MOT;
685			else
686				message.request &= ~DP_AUX_I2C_MOT;
687
688			for (int attempt = 0; attempt < 7; attempt++) {
689				result = _DpAuxTransfer(&message);
690				if (result < 0) {
691					ERROR("%s: aux_ch transaction failed!\n", __func__);
692					return result;
693				}
694
695				switch (message.reply & DP_AUX_I2C_REPLY_MASK) {
696					case DP_AUX_I2C_REPLY_ACK:
697						goto nextRead;
698					case DP_AUX_I2C_REPLY_NACK:
699						TRACE("%s: aux i2c nack\n", __func__);
700						return B_IO_ERROR;
701					case DP_AUX_I2C_REPLY_DEFER:
702						TRACE("%s: aux i2c defer\n", __func__);
703						snooze(400);
704						break;
705					default:
706						TRACE("%s: aux invalid I2C reply: 0x%02x\n",
707							__func__, message.reply);
708						return B_ERROR;
709				}
710			}
711nextRead:
712			if (result < 0)
713				return result;
714			if (result == 0)
715				i += message.size;
716		}
717	}
718
719	return B_OK;
720}
721
722
723status_t
724Port::_DpAuxSendReceiveHook(const struct i2c_bus *bus, uint32 slaveAddress,
725	const uint8 *writeBuffer, size_t writeLength, uint8 *readBuffer, size_t readLength)
726{
727	CALLED();
728	Port* port = (Port*)bus->cookie;
729	return port->_DpAuxSendReceive(slaveAddress, writeBuffer, writeLength, readBuffer, readLength);
730}
731
732
733ssize_t
734Port::_DpAuxTransfer(dp_aux_msg* message)
735{
736	CALLED();
737	if (message == NULL) {
738		ERROR("%s: DP message is invalid!\n", __func__);
739		return B_ERROR;
740	}
741
742	if (message->size > 16) {
743		ERROR("%s: Too many bytes! (%" B_PRIuSIZE ")\n", __func__,
744			message->size);
745		return B_ERROR;
746	}
747
748	uint8 transmitSize = message->size > 0 ? 4 : 3;
749	uint8 receiveSize;
750
751	switch(message->request & ~DP_AUX_I2C_MOT) {
752		case DP_AUX_NATIVE_WRITE:
753		case DP_AUX_I2C_WRITE:
754		case DP_AUX_I2C_WRITE_STATUS_UPDATE:
755			transmitSize += message->size;
756			break;
757	}
758
759	// If not bare address, check for buffer
760	if (message->size > 0 && message->buffer == NULL) {
761		ERROR("%s: DP message uninitalized buffer!\n", __func__);
762		return B_ERROR;
763	}
764
765	uint8 receiveBuffer[20];
766	uint8 transmitBuffer[20];
767	transmitBuffer[0] = (message->request << 4) | ((message->address >> 16) & 0xf);
768	transmitBuffer[1] = (message->address >> 8) & 0xff;
769	transmitBuffer[2] = message->address & 0xff;
770	transmitBuffer[3] = message->size != 0 ? (message->size - 1) : 0;
771
772	uint8 retry;
773	for (retry = 0; retry < 7; retry++) {
774		ssize_t result = B_ERROR;
775		switch(message->request & ~DP_AUX_I2C_MOT) {
776			case DP_AUX_NATIVE_WRITE:
777			case DP_AUX_I2C_WRITE:
778			case DP_AUX_I2C_WRITE_STATUS_UPDATE:
779				receiveSize = 2;
780				if (message->buffer != NULL)
781					memcpy(transmitBuffer + 4, message->buffer, message->size);
782				result = _DpAuxTransfer(transmitBuffer,
783					transmitSize, receiveBuffer, receiveSize);
784				if (result > 0) {
785					message->reply = receiveBuffer[0] >> 4;
786					if (result > 1)
787						result = min_c(receiveBuffer[1], message->size);
788					else
789						result = message->size;
790				}
791				break;
792			case DP_AUX_NATIVE_READ:
793			case DP_AUX_I2C_READ:
794				receiveSize = message->size + 1;
795				result = _DpAuxTransfer(transmitBuffer,
796					transmitSize, receiveBuffer, receiveSize);
797				if (result > 0) {
798					message->reply = receiveBuffer[0] >> 4;
799					result--;
800					if (message->buffer != NULL)
801						memcpy(message->buffer, receiveBuffer + 1, result);
802				}
803				break;
804			default:
805				ERROR("%s: Unknown dp_aux_msg request!\n", __func__);
806				return B_ERROR;
807		}
808
809		if (result == B_BUSY)
810			continue;
811		else if (result < B_OK)
812			return result;
813
814		switch (message->reply & DP_AUX_NATIVE_REPLY_MASK) {
815			case DP_AUX_NATIVE_REPLY_ACK:
816				return B_OK;
817			case DP_AUX_NATIVE_REPLY_NACK:
818				TRACE("%s: aux native reply nack\n", __func__);
819				return B_IO_ERROR;
820			case DP_AUX_NATIVE_REPLY_DEFER:
821				TRACE("%s: aux reply defer received. Snoozing.\n", __func__);
822				snooze(400);
823				break;
824			default:
825				TRACE("%s: aux invalid native reply: 0x%02x\n", __func__,
826					message->reply);
827				return B_IO_ERROR;
828		}
829	}
830
831	ERROR("%s: IO Error. %" B_PRIu8 " attempts\n", __func__, retry);
832	return B_IO_ERROR;
833}
834
835
836ssize_t
837Port::_DpAuxTransfer(uint8* transmitBuffer, uint8 transmitSize,
838	uint8* receiveBuffer, uint8 receiveSize)
839{
840	addr_t channelControl;
841	addr_t channelData[5];
842	aux_channel channel = _DpAuxChannel();
843	TRACE("%s: %s DpAuxChannel: 0x%x\n", __func__, PortName(), channel);
844	if (gInfo->shared_info->device_type.Generation() >= 9
845		|| (gInfo->shared_info->pch_info != INTEL_PCH_NONE && channel == AUX_CH_A)) {
846		channelControl = DP_AUX_CH_CTL(channel);
847		for (int i = 0; i < 5; i++)
848			channelData[i] = DP_AUX_CH_DATA(channel, i);
849	} else if (gInfo->shared_info->pch_info != INTEL_PCH_NONE) {
850		channelControl = PCH_DP_AUX_CH_CTL(channel);
851		for (int i = 0; i < 5; i++)
852			channelData[i] = PCH_DP_AUX_CH_DATA(channel, i);
853	} else {
854		ERROR("Port::_DpAuxTransfer() unknown register config\n");
855		return B_BUSY;
856	}
857	if (transmitSize > 20 || receiveSize > 20)
858		return E2BIG;
859
860	int tries = 0;
861	while ((read32(channelControl) & INTEL_DP_AUX_CTL_BUSY) != 0) {
862		if (tries++ == 3) {
863			ERROR("%s: %s AUX channel is busy!\n", __func__, PortName());
864			return B_BUSY;
865		}
866		snooze(1000);
867	}
868
869	uint32 sendControl = 0;
870	if (gInfo->shared_info->device_type.Generation() >= 9) {
871		sendControl = INTEL_DP_AUX_CTL_BUSY | INTEL_DP_AUX_CTL_DONE | INTEL_DP_AUX_CTL_INTERRUPT
872			| INTEL_DP_AUX_CTL_TIMEOUT_ERROR | INTEL_DP_AUX_CTL_TIMEOUT_1600us | INTEL_DP_AUX_CTL_RECEIVE_ERROR
873			| (transmitSize << INTEL_DP_AUX_CTL_MSG_SIZE_SHIFT) | INTEL_DP_AUX_CTL_FW_SYNC_PULSE_SKL(32)
874			| INTEL_DP_AUX_CTL_SYNC_PULSE_SKL(32);
875	} else {
876		uint32 frequency = gInfo->shared_info->hw_cdclk;
877		if (channel != AUX_CH_A)
878			frequency = gInfo->shared_info->hraw_clock;
879		uint32 aux_clock_divider = (frequency + 2000 / 2) / 2000;
880		if (gInfo->shared_info->pch_info == INTEL_PCH_LPT && channel != AUX_CH_A)
881			aux_clock_divider = 0x48; // or 0x3f
882		uint32 timeout = INTEL_DP_AUX_CTL_TIMEOUT_400us;
883		if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_BDW))
884			timeout = INTEL_DP_AUX_CTL_TIMEOUT_600us;
885		sendControl = INTEL_DP_AUX_CTL_BUSY | INTEL_DP_AUX_CTL_DONE | INTEL_DP_AUX_CTL_INTERRUPT
886			| INTEL_DP_AUX_CTL_TIMEOUT_ERROR | timeout | INTEL_DP_AUX_CTL_RECEIVE_ERROR
887			| (transmitSize << INTEL_DP_AUX_CTL_MSG_SIZE_SHIFT) | (3 << INTEL_DP_AUX_CTL_PRECHARGE_2US_SHIFT)
888			| (aux_clock_divider << INTEL_DP_AUX_CTL_BIT_CLOCK_2X_SHIFT);
889	}
890
891	uint8 retry;
892	uint32 status = 0;
893	for (retry = 0; retry < 5; retry++) {
894		for (uint8 i = 0; i < transmitSize;) {
895			uint8 index = i / 4;
896			uint32 data = ((uint32)transmitBuffer[i++]) << 24;
897			if (i < transmitSize)
898				data |= ((uint32)transmitBuffer[i++]) << 16;
899			if (i < transmitSize)
900				data |= ((uint32)transmitBuffer[i++]) << 8;
901			if (i < transmitSize)
902				data |= transmitBuffer[i++];
903			write32(channelData[index], data);
904		}
905		write32(channelControl, sendControl);
906
907		// wait 10 ms reading channelControl until INTEL_DP_AUX_CTL_BUSY
908		status = wait_for_clear_status(channelControl, INTEL_DP_AUX_CTL_BUSY, 10000);
909		if ((status & INTEL_DP_AUX_CTL_BUSY) != 0) {
910			ERROR("%s: %s AUX channel stayed busy for 10000us!\n", __func__, PortName());
911		}
912
913		write32(channelControl, status | INTEL_DP_AUX_CTL_DONE | INTEL_DP_AUX_CTL_TIMEOUT_ERROR
914			| INTEL_DP_AUX_CTL_RECEIVE_ERROR);
915		if ((status & INTEL_DP_AUX_CTL_TIMEOUT_ERROR) != 0)
916			continue;
917		if ((status & INTEL_DP_AUX_CTL_RECEIVE_ERROR) != 0) {
918			snooze(400);
919			continue;
920		}
921		if ((status & INTEL_DP_AUX_CTL_DONE) != 0)
922			goto done;
923	}
924
925	if ((status & INTEL_DP_AUX_CTL_DONE) == 0) {
926		ERROR("%s: Busy Error. %" B_PRIu8 " attempts\n", __func__, retry);
927		return B_BUSY;
928	}
929done:
930	if ((status & INTEL_DP_AUX_CTL_RECEIVE_ERROR) != 0)
931		return B_IO_ERROR;
932	if ((status & INTEL_DP_AUX_CTL_TIMEOUT_ERROR) != 0)
933		return B_TIMEOUT;
934
935	uint8 bytes = (status & INTEL_DP_AUX_CTL_MSG_SIZE_MASK) >> INTEL_DP_AUX_CTL_MSG_SIZE_SHIFT;
936	if (bytes == 0 || bytes > 20) {
937		ERROR("%s: Status byte count incorrect %u\n", __func__, bytes);
938		return B_BUSY;
939	}
940	if (bytes > receiveSize)
941		bytes = receiveSize;
942	for (uint8 i = 0; i < bytes;) {
943		uint32 data = read32(channelData[i / 4]);
944		receiveBuffer[i++] = data >> 24;
945		if (i < bytes)
946			receiveBuffer[i++] = data >> 16;
947		if (i < bytes)
948			receiveBuffer[i++] = data >> 8;
949		if (i < bytes)
950			receiveBuffer[i++] = data;
951	}
952
953	return bytes;
954}
955
956
957aux_channel
958Port::_DpAuxChannel()
959{
960	uint32 foundIndex = 0;
961	if (!_IsPortInVBT(&foundIndex))
962		return AUX_CH_A;
963	child_device_config& config = gInfo->shared_info->device_configs[foundIndex];
964	switch (config.aux_channel) {
965		case DP_AUX_B:
966			return AUX_CH_B;
967		case DP_AUX_C:
968			return AUX_CH_C;
969		case DP_AUX_D:
970			return AUX_CH_D;
971		case DP_AUX_E:
972			return AUX_CH_E;
973		case DP_AUX_F:
974			return AUX_CH_F;
975		default:
976			return AUX_CH_A;
977	}
978}
979
980
981// #pragma mark - Analog Port
982
983
984AnalogPort::AnalogPort()
985	:
986	Port(INTEL_PORT_A, "Analog")
987{
988}
989
990
991bool
992AnalogPort::IsConnected()
993{
994	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
995		_PortRegister());
996	return HasEDID();
997}
998
999
1000addr_t
1001AnalogPort::_DDCRegister()
1002{
1003	// always fixed
1004	return INTEL_I2C_IO_A;
1005}
1006
1007
1008addr_t
1009AnalogPort::_PortRegister()
1010{
1011	// always fixed
1012	return INTEL_ANALOG_PORT;
1013}
1014
1015
1016status_t
1017AnalogPort::SetDisplayMode(display_mode* target, uint32 colorMode)
1018{
1019	CALLED();
1020	TRACE("%s: %s %dx%d\n", __func__, PortName(), target->timing.h_display,
1021		target->timing.v_display);
1022
1023	if (fPipe == NULL) {
1024		ERROR("%s: Setting display mode without assigned pipe!\n", __func__);
1025		return B_ERROR;
1026	}
1027
1028	// Setup PanelFitter and Train FDI if it exists
1029	PanelFitter* fitter = fPipe->PFT();
1030	if (fitter != NULL)
1031		fitter->Enable(target->timing);
1032	FDILink* link = fPipe->FDI();
1033	if (link != NULL) {
1034		uint32 lanes = 0;
1035		uint32 linkBandwidth = 0;
1036		uint32 bitsPerPixel = 0;
1037		link->PreTrain(&target->timing, &linkBandwidth, &lanes, &bitsPerPixel);
1038		fPipe->SetFDILink(target->timing, linkBandwidth, lanes, bitsPerPixel);
1039		link->Train(&target->timing, lanes);
1040	}
1041	pll_divisors divisors;
1042	compute_pll_divisors(&target->timing, &divisors, false);
1043
1044	uint32 extraPLLFlags = 0;
1045	if (gInfo->shared_info->device_type.Generation() >= 3)
1046		extraPLLFlags |= DISPLAY_PLL_MODE_NORMAL;
1047
1048	// Program general pipe config
1049	fPipe->Configure(target);
1050
1051	// Program pipe PLL's
1052	fPipe->ConfigureClocks(divisors, target->timing.pixel_clock, extraPLLFlags);
1053
1054	write32(_PortRegister(), (read32(_PortRegister())
1055		& ~(DISPLAY_MONITOR_POLARITY_MASK | DISPLAY_MONITOR_VGA_POLARITY))
1056		| ((target->timing.flags & B_POSITIVE_HSYNC) != 0
1057			? DISPLAY_MONITOR_POSITIVE_HSYNC : 0)
1058		| ((target->timing.flags & B_POSITIVE_VSYNC) != 0
1059			? DISPLAY_MONITOR_POSITIVE_VSYNC : 0));
1060
1061	// Program target display mode
1062	fPipe->ConfigureTimings(target);
1063
1064	// Set fCurrentMode to our set display mode
1065	memcpy(&fCurrentMode, target, sizeof(display_mode));
1066
1067	return B_OK;
1068}
1069
1070
1071// #pragma mark - LVDS Panel
1072
1073
1074LVDSPort::LVDSPort()
1075	:
1076	Port(INTEL_PORT_C, "LVDS")
1077{
1078	// Always unlock LVDS port as soon as we start messing with it.
1079	uint32 panelControl = INTEL_PANEL_CONTROL;
1080	if (gInfo->shared_info->pch_info != INTEL_PCH_NONE) {
1081		// FIXME writing there results in black screen on SandyBridge
1082		return;
1083		// panelControl = PCH_PANEL_CONTROL;
1084	}
1085	write32(panelControl, read32(panelControl) | PANEL_REGISTER_UNLOCK);
1086}
1087
1088
1089pipe_index
1090LVDSPort::PipePreference()
1091{
1092	CALLED();
1093	// Older devices have hardcoded pipe/port mappings, so just use that
1094	if (gInfo->shared_info->device_type.Generation() < 4)
1095		return INTEL_PIPE_B;
1096
1097	// Ideally we could just return INTEL_PIPE_ANY for the newer devices, but
1098	// this doesn't quite work yet.
1099
1100	// On SandyBridge and later, there is a transcoder C. On SandyBridge at least
1101	// that can't be used by the LVDS port (but A and B would be fine).
1102	// On Ibex Point, SandyBridge and IvyBridge (tested) changing pipes does not
1103	// work yet.
1104	// Notes:
1105	// - Switching Pipes only works reliably when a 'full modeswitch' is executed
1106	//   (FDI training) so we have to reuse the BIOS preset setup always for now.
1107	// - The BIOSes seen sofar do not use PIPE C by default.
1108	// - The BIOSes seen sofar program transcoder A to PIPE A, etc.
1109	// - Later devices add a pipe C alongside the added transcoder C.
1110
1111	// FIXME How's this setup in newer gens? Currently return Pipe B fixed there..
1112	if (gInfo->shared_info->device_type.Generation() <= 7) {
1113		uint32 portState = read32(_PortRegister());
1114		if (gInfo->shared_info->pch_info == INTEL_PCH_CPT) {
1115			portState &= PORT_TRANS_SEL_MASK;
1116			if (portState == PORT_TRANS_B_SEL_CPT)
1117				return INTEL_PIPE_B;
1118			else
1119				return INTEL_PIPE_A;
1120		} else {
1121			if (portState & DISPLAY_MONITOR_PIPE_B)
1122				return INTEL_PIPE_B;
1123			else
1124				return INTEL_PIPE_A;
1125		}
1126	}
1127
1128	return INTEL_PIPE_B;
1129}
1130
1131
1132bool
1133LVDSPort::IsConnected()
1134{
1135	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
1136		_PortRegister());
1137
1138	if (gInfo->shared_info->pch_info != INTEL_PCH_NONE) {
1139		uint32 registerValue = read32(_PortRegister());
1140		// there's a detection bit we can use
1141		if ((registerValue & PCH_LVDS_DETECTED) == 0) {
1142			TRACE("LVDS: Not detected\n");
1143			return false;
1144		}
1145		// TODO: Skip if eDP support
1146	} else if (gInfo->shared_info->device_type.Generation() <= 4) {
1147		// Older generations don't have LVDS detection. If not mobile skip.
1148		if (!gInfo->shared_info->device_type.IsMobile()) {
1149			TRACE("LVDS: Skipping LVDS detection due to gen and not mobile\n");
1150			return false;
1151		}
1152		// If mobile, try to grab EDID
1153		// Linux seems to look at lid status for LVDS port detection
1154		// If we don't get EDID, we can use vbios native mode or vesa?
1155		if (!HasEDID()) {
1156			if (gInfo->shared_info->has_vesa_edid_info) {
1157				TRACE("LVDS: Using VESA edid info\n");
1158				memcpy(&fEDIDInfo, &gInfo->shared_info->vesa_edid_info,
1159					sizeof(edid1_info));
1160				if (fEDIDState != B_OK) {
1161					fEDIDState = B_OK;
1162					// HasEDID now true
1163					edid_dump(&fEDIDInfo);
1164				}
1165			} else if (gInfo->shared_info->got_vbt) {
1166				TRACE("LVDS: No EDID, but force enabled as we have a VBT\n");
1167				return true;
1168			} else {
1169				TRACE("LVDS: Couldn't find any valid EDID!\n");
1170				return false;
1171			}
1172		}
1173	}
1174
1175	// Try getting EDID, as the LVDS port doesn't overlap with anything else,
1176	// we don't run the risk of getting someone else's data.
1177	return HasEDID();
1178}
1179
1180
1181addr_t
1182LVDSPort::_DDCRegister()
1183{
1184	// always fixed
1185	return INTEL_I2C_IO_C;
1186}
1187
1188
1189addr_t
1190LVDSPort::_PortRegister()
1191{
1192	// always fixed
1193	return INTEL_DIGITAL_LVDS_PORT;
1194}
1195
1196
1197status_t
1198LVDSPort::SetDisplayMode(display_mode* target, uint32 colorMode)
1199{
1200	CALLED();
1201	if (target == NULL) {
1202		ERROR("%s: Invalid target mode passed!\n", __func__);
1203		return B_ERROR;
1204	}
1205
1206	TRACE("%s: %s-%d %dx%d\n", __func__, PortName(), PortIndex(),
1207		target->timing.h_display, target->timing.v_display);
1208
1209	if (fPipe == NULL) {
1210		ERROR("%s: Setting display mode without assigned pipe!\n", __func__);
1211		return B_ERROR;
1212	}
1213
1214	addr_t panelControl = INTEL_PANEL_CONTROL;
1215	addr_t panelStatus = INTEL_PANEL_STATUS;
1216	if (gInfo->shared_info->pch_info != INTEL_PCH_NONE) {
1217		panelControl = PCH_PANEL_CONTROL;
1218		panelStatus = PCH_PANEL_STATUS;
1219	}
1220
1221	if (gInfo->shared_info->device_type.Generation() != 4) {
1222		// TODO not needed on any generation if we are using the panel fitter
1223		// Power off Panel
1224		write32(panelControl,
1225			read32(panelControl) & ~PANEL_CONTROL_POWER_TARGET_ON);
1226		read32(panelControl);
1227
1228		if (!wait_for_clear(panelStatus, PANEL_STATUS_POWER_ON, 1000)) {
1229			ERROR("%s: %s didn't power off within 1000ms!\n", __func__,
1230				PortName());
1231		}
1232	}
1233
1234	// For LVDS panels, we may need to set the timings according to the panel
1235	// native video mode, and let the panel fitter do the scaling. But the
1236	// place where the scaling happens varies accross generations of devices.
1237	display_timing hardwareTarget;
1238	bool needsScaling = false;
1239
1240	// TODO figure out how it's done (or if we need to configure something at
1241	// all) for other generations
1242	if (gInfo->shared_info->device_type.Generation() <= 6
1243		&& gInfo->shared_info->device_type.Generation() >= 3
1244		&& gInfo->shared_info->got_vbt) {
1245		// Set vbios hardware panel mode as base
1246		hardwareTarget = gInfo->shared_info->panel_timing;
1247
1248		if (hardwareTarget.h_display == target->timing.h_display
1249				&& hardwareTarget.v_display == target->timing.v_display) {
1250			// We are setting the native video mode, nothing special to do
1251			// Note: this means refresh and timing might vary according to requested mode.
1252			hardwareTarget = target->timing;
1253			TRACE("%s: Setting LVDS to native resolution at %" B_PRIu32 "Hz\n", __func__,
1254				hardwareTarget.pixel_clock * 1000 / (hardwareTarget.h_total * hardwareTarget.v_total));
1255		} else {
1256			// We need to enable the panel fitter
1257			TRACE("%s: Hardware mode will actually be %dx%d at %" B_PRIu32 "Hz\n", __func__,
1258				hardwareTarget.h_display, hardwareTarget.v_display,
1259				hardwareTarget.pixel_clock * 1000 / (hardwareTarget.h_total * hardwareTarget.v_total));
1260
1261			// FIXME we should also get the refresh frequency from the target
1262			// mode, and then "sanitize" the resulting mode we made up.
1263			needsScaling = true;
1264		}
1265	} else {
1266		TRACE("Setting LVDS mode without VBT info or on unhandled hardware "
1267			"generation, scaling may not work\n");
1268		// We don't have VBT data, try to set the requested mode directly
1269		// and hope for the best
1270		hardwareTarget = target->timing;
1271	}
1272
1273	// Setup PanelFitter and Train FDI if it exists
1274	PanelFitter* fitter = fPipe->PFT();
1275	if (fitter != NULL)
1276		fitter->Enable(hardwareTarget);
1277	FDILink* link = fPipe->FDI();
1278	if (link != NULL) {
1279		uint32 lanes = 0;
1280		uint32 linkBandwidth = 0;
1281		uint32 bitsPerPixel = 0;
1282		link->PreTrain(&hardwareTarget, &linkBandwidth, &lanes, &bitsPerPixel);
1283		fPipe->SetFDILink(hardwareTarget, linkBandwidth, lanes, bitsPerPixel);
1284		link->Train(&hardwareTarget, lanes);
1285	}
1286
1287	pll_divisors divisors;
1288	compute_pll_divisors(&hardwareTarget, &divisors, true);
1289
1290	uint32 lvds = read32(_PortRegister())
1291		| LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
1292
1293	if (gInfo->shared_info->device_type.Generation() == 4) {
1294		// LVDS_A3_POWER_UP == 24bpp
1295		// otherwise, 18bpp
1296		if ((lvds & LVDS_A3_POWER_MASK) != LVDS_A3_POWER_UP)
1297			lvds |= LVDS_18BIT_DITHER;
1298	}
1299
1300	// LVDS on PCH needs set before display enable
1301	if (gInfo->shared_info->pch_info == INTEL_PCH_CPT) {
1302		lvds &= ~PORT_TRANS_SEL_MASK;
1303		if (fPipe->Index() == INTEL_PIPE_A)
1304			lvds |= PORT_TRANS_A_SEL_CPT;
1305		else
1306			lvds |= PORT_TRANS_B_SEL_CPT;
1307	}
1308
1309	// Set the B0-B3 data pairs corresponding to whether we're going to
1310	// set the DPLLs for dual-channel mode or not.
1311	if (divisors.p2 == 5 || divisors.p2 == 7) {
1312		TRACE("LVDS: dual channel\n");
1313		lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
1314	} else {
1315		TRACE("LVDS: single channel\n");
1316		lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
1317	}
1318
1319	// LVDS port control moves polarity bits because Intel hates you.
1320	// Set LVDS sync polarity
1321	lvds &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
1322
1323	// set on - polarity.
1324	if ((target->timing.flags & B_POSITIVE_HSYNC) == 0)
1325		lvds |= LVDS_HSYNC_POLARITY;
1326	if ((target->timing.flags & B_POSITIVE_VSYNC) == 0)
1327		lvds |= LVDS_VSYNC_POLARITY;
1328
1329	TRACE("%s: LVDS Write: 0x%" B_PRIx32 "\n", __func__, lvds);
1330	write32(_PortRegister(), lvds);
1331	read32(_PortRegister());
1332
1333	uint32 extraPLLFlags = 0;
1334
1335	// DPLL mode LVDS for i915+
1336	if (gInfo->shared_info->device_type.Generation() >= 3)
1337		extraPLLFlags |= DISPLAY_PLL_MODE_LVDS | DISPLAY_PLL_2X_CLOCK;
1338
1339	// Program general pipe config
1340	fPipe->Configure(target);
1341
1342	// Program pipe PLL's (using the hardware mode timings, since that's what
1343	// the PLL is used for)
1344	fPipe->ConfigureClocks(divisors, hardwareTarget.pixel_clock,
1345		extraPLLFlags);
1346
1347	if (gInfo->shared_info->device_type.Generation() != 4) {
1348		// G45: no need to power the panel off
1349		// Power on Panel
1350		write32(panelControl,
1351			read32(panelControl) | PANEL_CONTROL_POWER_TARGET_ON);
1352		read32(panelControl);
1353
1354		if (!wait_for_set(panelStatus, PANEL_STATUS_POWER_ON, 1000)) {
1355			ERROR("%s: %s didn't power on within 1000us!\n", __func__,
1356				PortName());
1357		}
1358	}
1359
1360	// Program target display mode
1361	fPipe->ConfigureTimings(target, !needsScaling);
1362
1363	if (needsScaling) {
1364		if (gInfo->shared_info->device_type.Generation() <= 4) {
1365			// Enable panel fitter in automatic mode. It will figure out
1366			// the scaling ratios automatically.
1367			uint32 panelFitterControl = read32(INTEL_PANEL_FIT_CONTROL);
1368			panelFitterControl |= PANEL_FITTER_ENABLED;
1369			panelFitterControl &= ~(PANEL_FITTER_SCALING_MODE_MASK
1370				| PANEL_FITTER_PIPE_MASK);
1371			panelFitterControl |= PANEL_FITTER_PIPE_B;
1372			// LVDS is always on pipe B.
1373			write32(INTEL_PANEL_FIT_CONTROL, panelFitterControl);
1374		}
1375		// TODO do we need to do anything on later generations?
1376	} else {
1377		if (gInfo->shared_info->device_type.Generation() == 4
1378			|| gInfo->shared_info->device_type.Generation() == 3) {
1379			// Bypass the panel fitter
1380			uint32 panelFitterControl = read32(INTEL_PANEL_FIT_CONTROL);
1381			panelFitterControl &= ~PANEL_FITTER_ENABLED;
1382			write32(INTEL_PANEL_FIT_CONTROL, panelFitterControl);
1383		} else {
1384			// We don't need to do anything more for later generations, the
1385			// scaling is handled at the transcoder level. We may want to
1386			// configure dithering, but the code below ignores the previous
1387			// value in the register and may mess things up so we should do
1388			// this in a safeer way. For now, assume the BIOS did the right
1389			// thing.
1390#if 0
1391			// Disable panel fitting, but enable 8 to 6-bit dithering
1392			write32(INTEL_PANEL_FIT_CONTROL, 0x4);
1393				// TODO: do not do this if the connected panel is 24-bit
1394				// (I don't know how to detect that)
1395#endif
1396		}
1397	}
1398
1399	// Set fCurrentMode to our set display mode
1400	memcpy(&fCurrentMode, target, sizeof(display_mode));
1401
1402	return B_OK;
1403}
1404
1405
1406// #pragma mark - DVI/SDVO/generic
1407
1408
1409DigitalPort::DigitalPort(port_index index, const char* baseName)
1410	:
1411	Port(index, baseName)
1412{
1413}
1414
1415
1416bool
1417DigitalPort::IsConnected()
1418{
1419	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
1420		_PortRegister());
1421
1422	// As this port overlaps with pretty much everything, this must be called
1423	// after having ruled out all other port types.
1424	return HasEDID();
1425}
1426
1427
1428addr_t
1429DigitalPort::_DDCRegister()
1430{
1431	//TODO: IS BROXTON, B = B, C = C, D = NIL
1432	switch (PortIndex()) {
1433		case INTEL_PORT_B:
1434			return INTEL_I2C_IO_E;
1435		case INTEL_PORT_C:
1436			return INTEL_I2C_IO_D;
1437		case INTEL_PORT_D:
1438			return INTEL_I2C_IO_F;
1439		default:
1440			return 0;
1441	}
1442
1443	return 0;
1444}
1445
1446
1447addr_t
1448DigitalPort::_PortRegister()
1449{
1450	switch (PortIndex()) {
1451		case INTEL_PORT_A:
1452			return INTEL_DIGITAL_PORT_A;
1453		case INTEL_PORT_B:
1454			return INTEL_DIGITAL_PORT_B;
1455		case INTEL_PORT_C:
1456			return INTEL_DIGITAL_PORT_C;
1457		default:
1458			return 0;
1459	}
1460	return 0;
1461}
1462
1463
1464status_t
1465DigitalPort::SetDisplayMode(display_mode* target, uint32 colorMode)
1466{
1467	CALLED();
1468	TRACE("%s: %s %dx%d\n", __func__, PortName(), target->timing.h_display,
1469		target->timing.v_display);
1470
1471	if (fPipe == NULL) {
1472		ERROR("%s: Setting display mode without assigned pipe!\n", __func__);
1473		return B_ERROR;
1474	}
1475
1476	// Setup PanelFitter and Train FDI if it exists
1477	PanelFitter* fitter = fPipe->PFT();
1478	if (fitter != NULL)
1479		fitter->Enable(target->timing);
1480	FDILink* link = fPipe->FDI();
1481	if (link != NULL) {
1482		uint32 lanes = 0;
1483		uint32 linkBandwidth = 0;
1484		uint32 bitsPerPixel = 0;
1485		link->PreTrain(&target->timing, &linkBandwidth, &lanes, &bitsPerPixel);
1486		fPipe->SetFDILink(target->timing, linkBandwidth, lanes, bitsPerPixel);
1487		link->Train(&target->timing, lanes);
1488	}
1489
1490	pll_divisors divisors;
1491	compute_pll_divisors(&target->timing, &divisors, false);
1492
1493	uint32 extraPLLFlags = 0;
1494	if (gInfo->shared_info->device_type.Generation() >= 3)
1495		extraPLLFlags |= DISPLAY_PLL_MODE_NORMAL | DISPLAY_PLL_2X_CLOCK;
1496
1497	// Program general pipe config
1498	fPipe->Configure(target);
1499
1500	// Program pipe PLL's
1501	fPipe->ConfigureClocks(divisors, target->timing.pixel_clock, extraPLLFlags);
1502
1503	// Program target display mode
1504	fPipe->ConfigureTimings(target);
1505
1506	// Set fCurrentMode to our set display mode
1507	memcpy(&fCurrentMode, target, sizeof(display_mode));
1508
1509	return B_OK;
1510}
1511
1512
1513// #pragma mark - LVDS Panel
1514// #pragma mark - HDMI
1515
1516
1517HDMIPort::HDMIPort(port_index index)
1518	:
1519	DigitalPort(index, "HDMI")
1520{
1521}
1522
1523
1524bool
1525HDMIPort::IsConnected()
1526{
1527	if (!gInfo->shared_info->device_type.SupportsHDMI())
1528		return false;
1529
1530	addr_t portRegister = _PortRegister();
1531	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
1532		portRegister);
1533
1534	if (portRegister == 0)
1535		return false;
1536
1537	const uint32 deviceConfigCount = gInfo->shared_info->device_config_count;
1538	if (gInfo->shared_info->device_type.Generation() >= 6 && deviceConfigCount > 0) {
1539		// check VBT mapping
1540		if (!_IsPortInVBT()) {
1541			TRACE("%s: %s: port not found in VBT\n", __func__, PortName());
1542			return false;
1543		} else
1544			TRACE("%s: %s: port found in VBT\n", __func__, PortName());
1545	}
1546
1547	//Notes:
1548	//- DISPLAY_MONITOR_PORT_DETECTED does only tell you *some* sort of digital display is
1549	//  connected to the port *if* you have the AUX channel stuff under power. It does not
1550	//  tell you which -type- of digital display is connected.
1551	//- Since we rely on the BIOS anyway, let's just use the conclusions it made for us :)
1552	//  Beware though: set_display_power_mode() uses this DISPLAY_MONITOR_PORT_ENABLED bit
1553	//  for DPMS as well. So we should better buffer our findings here for i.e. possible
1554	//  accelerant clones starting up. For DPMS there's currently no problem as this bit
1555	//  is only programmed for LVDS, DVI and VGA while we detect presence only for DP and HDMI.
1556	//
1557	//if ((read32(portRegister) & DISPLAY_MONITOR_PORT_DETECTED) == 0)
1558	if ((read32(portRegister) & DISPLAY_MONITOR_PORT_ENABLED) == 0)
1559		return false;
1560
1561	return HasEDID();
1562}
1563
1564
1565addr_t
1566HDMIPort::_PortRegister()
1567{
1568	// on PCH there's an additional port sandwiched in
1569	bool hasPCH = (gInfo->shared_info->pch_info != INTEL_PCH_NONE);
1570	bool fourthGen = gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV);
1571
1572	switch (PortIndex()) {
1573		case INTEL_PORT_B:
1574			if (fourthGen)
1575				return GEN4_HDMI_PORT_B;
1576			return hasPCH ? PCH_HDMI_PORT_B : INTEL_HDMI_PORT_B;
1577		case INTEL_PORT_C:
1578			if (fourthGen)
1579				return GEN4_HDMI_PORT_C;
1580			return hasPCH ? PCH_HDMI_PORT_C : INTEL_HDMI_PORT_C;
1581		case INTEL_PORT_D:
1582			if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_CHV))
1583				return CHV_HDMI_PORT_D;
1584			return hasPCH ? PCH_HDMI_PORT_D : 0;
1585		default:
1586			return 0;
1587		}
1588
1589	return 0;
1590}
1591
1592
1593// #pragma mark - DisplayPort
1594
1595
1596DisplayPort::DisplayPort(port_index index, const char* baseName)
1597	:
1598	Port(index, baseName)
1599{
1600}
1601
1602
1603pipe_index
1604DisplayPort::PipePreference()
1605{
1606	CALLED();
1607	if (gInfo->shared_info->device_type.Generation() <= 4)
1608		return INTEL_PIPE_ANY;
1609
1610	// Notes:
1611	// - The BIOSes seen sofar do not use PIPE C by default.
1612	// - Looks like BIOS selected Transcoder (A,B,C) is not always same as selected Pipe (A,B,C)
1613	//   so these should probably be handled seperately. For now this is OK as we don't touch
1614	//   the pipe for DisplayPort, only the transcoder..
1615	uint32 TranscoderPort = INTEL_TRANS_DP_PORT_NONE;
1616	switch (PortIndex()) {
1617		case INTEL_PORT_A:
1618			if (gInfo->shared_info->device_type.Generation() == 6) {
1619				if (((read32(INTEL_DISPLAY_PORT_A) & INTEL_DISP_PORTA_SNB_PIPE_MASK)
1620					>> INTEL_DISP_PORTA_SNB_PIPE_SHIFT) == INTEL_DISP_PORTA_SNB_PIPE_A) {
1621					return INTEL_PIPE_A;
1622				} else {
1623					return INTEL_PIPE_B;
1624				}
1625			}
1626			if (gInfo->shared_info->device_type.Generation() == 7) {
1627				uint32 Pipe = (read32(INTEL_DISPLAY_PORT_A) & INTEL_DISP_PORTA_IVB_PIPE_MASK)
1628					>> INTEL_DISP_PORTA_IVB_PIPE_SHIFT;
1629				switch (Pipe) {
1630					case INTEL_DISP_PORTA_IVB_PIPE_A:
1631						return INTEL_PIPE_A;
1632					case INTEL_DISP_PORTA_IVB_PIPE_B:
1633						return INTEL_PIPE_B;
1634					case INTEL_DISP_PORTA_IVB_PIPE_C:
1635						return INTEL_PIPE_C;
1636					default:
1637						return INTEL_PIPE_ANY;
1638				}
1639			}
1640			return INTEL_PIPE_ANY;
1641		case INTEL_PORT_B:
1642			TranscoderPort = INTEL_TRANS_DP_PORT_B;
1643			break;
1644		case INTEL_PORT_C:
1645			TranscoderPort = INTEL_TRANS_DP_PORT_C;
1646			break;
1647		case INTEL_PORT_D:
1648			TranscoderPort = INTEL_TRANS_DP_PORT_D;
1649			break;
1650		default:
1651			return INTEL_PIPE_ANY;
1652	}
1653
1654	for (uint32 Transcoder = 0; Transcoder < 3; Transcoder++) {
1655		if ((read32(INTEL_TRANSCODER_A_DP_CTL + (Transcoder << 12)) & INTEL_TRANS_DP_PORT_MASK) ==
1656			INTEL_TRANS_DP_PORT(TranscoderPort)) {
1657			switch (Transcoder) {
1658				case 0:
1659					return INTEL_PIPE_A;
1660				case 1:
1661					return INTEL_PIPE_B;
1662				case 2:
1663					return INTEL_PIPE_C;
1664			}
1665		}
1666	}
1667
1668	return INTEL_PIPE_ANY;
1669}
1670
1671
1672status_t
1673DisplayPort::SetPipe(Pipe* pipe)
1674{
1675	CALLED();
1676
1677	if (pipe == NULL) {
1678		ERROR("%s: Invalid pipe provided!\n", __func__);
1679		return B_ERROR;
1680	}
1681
1682	// TODO: UnAssignPipe?  This likely needs reworked a little
1683	if (fPipe != NULL) {
1684		ERROR("%s: Can't reassign display pipe (yet)\n", __func__);
1685		return B_ERROR;
1686	}
1687
1688	// generation 3/4/5 gfx have no eDP ports.
1689	// generation 6 gfx SandyBridge/SNB uses one bit (b30) on the eDP port.
1690	// generation 7 gfx IvyBridge/IVB uses 2 bits (b29-30) on the eDP port.
1691	// on all other DP ports pipe selections works differently (indirect).
1692	// fixme: implement..
1693	TRACE("%s: Assuming pipe is assigned by BIOS (fixme)\n", __func__);
1694
1695	fPipe = pipe;
1696
1697	if (fPipe == NULL)
1698		return B_NO_MEMORY;
1699
1700	// Disable display pipe until modesetting enables it
1701	if (fPipe->IsEnabled())
1702		fPipe->Enable(false);
1703
1704	return B_OK;
1705}
1706
1707
1708status_t
1709DisplayPort::SetupI2c(i2c_bus *bus)
1710{
1711	CALLED();
1712
1713	const uint32 deviceConfigCount = gInfo->shared_info->device_config_count;
1714	if (gInfo->shared_info->device_type.Generation() >= 6 && deviceConfigCount > 0) {
1715		if (!_IsDisplayPortInVBT())
1716			return Port::SetupI2c(bus);
1717	}
1718
1719	return _SetupDpAuxI2c(bus);
1720}
1721
1722
1723bool
1724DisplayPort::IsConnected()
1725{
1726	addr_t portRegister = _PortRegister();
1727
1728	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
1729		portRegister);
1730
1731	if (portRegister == 0)
1732		return false;
1733
1734	const uint32 deviceConfigCount = gInfo->shared_info->device_config_count;
1735	if (gInfo->shared_info->device_type.Generation() >= 6 && deviceConfigCount > 0) {
1736		// check VBT mapping
1737		if (!_IsPortInVBT()) {
1738			TRACE("%s: %s: port not found in VBT\n", __func__, PortName());
1739			return false;
1740		} else
1741			TRACE("%s: %s: port found in VBT\n", __func__, PortName());
1742	}
1743
1744	//Notes:
1745	//- DISPLAY_MONITOR_PORT_DETECTED does only tell you *some* sort of digital display is
1746	//  connected to the port *if* you have the AUX channel stuff under power. It does not
1747	//  tell you which -type- of digital display is connected.
1748	//- Since we rely on the BIOS anyway, let's just use the conclusions it made for us :)
1749	//  Beware though: set_display_power_mode() uses this DISPLAY_MONITOR_PORT_ENABLED bit
1750	//  for DPMS as well. So we should better buffer our findings here for i.e. possible
1751	//  accelerant clones starting up. For DPMS there's currently no problem as this bit
1752	//  is only programmed for LVDS, DVI and VGA while we detect presence only for DP and HDMI.
1753	//
1754	//if ((read32(portRegister) & DISPLAY_MONITOR_PORT_DETECTED) == 0) {
1755	if ((read32(portRegister) & DISPLAY_MONITOR_PORT_ENABLED) == 0) {
1756		TRACE("%s: %s link not detected\n", __func__, PortName());
1757		return false;
1758	}
1759
1760	TRACE("%s: %s link detected\n", __func__, PortName());
1761	bool edidDetected = HasEDID();
1762
1763	// On laptops we always have an internal panel.. (this is on the eDP port)
1764	if ((gInfo->shared_info->device_type.IsMobile() || _IsEDPPort())
1765		&& (PortIndex() == INTEL_PORT_A) && !edidDetected) {
1766		if (gInfo->shared_info->has_vesa_edid_info) {
1767			TRACE("%s: Laptop. Using VESA edid info\n", __func__);
1768			memcpy(&fEDIDInfo, &gInfo->shared_info->vesa_edid_info,
1769				sizeof(edid1_info));
1770			if (fEDIDState != B_OK) {
1771				fEDIDState = B_OK;
1772				// HasEDID now true
1773				edid_dump(&fEDIDInfo);
1774			}
1775			return true;
1776		} else if (gInfo->shared_info->got_vbt) {
1777			TRACE("%s: Laptop. No EDID, but force enabled as we have a VBT\n", __func__);
1778			return true;
1779		}
1780	}
1781
1782	//since EDID is not correctly implemented yet for this connection type we'll do without it for now
1783	//return HasEDID();
1784	return true;
1785}
1786
1787
1788addr_t
1789DisplayPort::_DDCRegister()
1790{
1791	return 0;
1792}
1793
1794
1795addr_t
1796DisplayPort::_PortRegister()
1797{
1798	// There are 6000 lines of intel linux code probing DP registers
1799	// to properly detect DP vs eDP to then in-turn properly figure out
1800	// what is DP and what is HDMI. It only takes 3 lines to
1801	// ignore DisplayPort on ValleyView / CherryView
1802
1803	if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV)
1804		|| gInfo->shared_info->device_type.InGroup(INTEL_GROUP_CHV)) {
1805		ERROR("TODO: DisplayPort on ValleyView / CherryView");
1806		return 0;
1807	}
1808
1809	// Intel, are humans even involved anymore?
1810	// This is a lot more complex than this code makes it look. (see defines)
1811	// INTEL_DISPLAY_PORT_X moves around a lot based on PCH
1812	// except on ValleyView and CherryView.
1813	switch (PortIndex()) {
1814		case INTEL_PORT_A:
1815			return INTEL_DISPLAY_PORT_A;
1816		case INTEL_PORT_B:
1817			if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV))
1818				return VLV_DISPLAY_PORT_B;
1819			return INTEL_DISPLAY_PORT_B;
1820		case INTEL_PORT_C:
1821			if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV))
1822				return VLV_DISPLAY_PORT_C;
1823			return INTEL_DISPLAY_PORT_C;
1824		case INTEL_PORT_D:
1825			if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_CHV))
1826				return CHV_DISPLAY_PORT_D;
1827			else if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV))
1828				return 0;
1829			return INTEL_DISPLAY_PORT_D;
1830		default:
1831			return 0;
1832	}
1833
1834	return 0;
1835}
1836
1837
1838status_t
1839DisplayPort::_SetPortLinkGen4(const display_timing& timing)
1840{
1841	// Khz / 10. ( each output octet encoded as 10 bits.
1842	//fixme: always so?
1843	uint32 linkBandwidth = 270000; //khz
1844	uint32 fPipeOffset = 0;
1845	if (fPipe->Index() == INTEL_PIPE_B)
1846		fPipeOffset = 0x1000;
1847
1848	TRACE("%s: DP M1 data before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_DATA_M + fPipeOffset));
1849	TRACE("%s: DP N1 data before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_DATA_N + fPipeOffset));
1850	TRACE("%s: DP M1 link before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_LINK_M + fPipeOffset));
1851	TRACE("%s: DP N1 link before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_LINK_N + fPipeOffset));
1852
1853	uint32 bitsPerPixel = 24;	//fixme: always so?
1854	uint32 lanes = 4;			//fixme: always so?
1855
1856	//Setup Data M/N
1857	uint64 linkspeed = lanes * linkBandwidth * 8;
1858	uint64 ret_n = 1;
1859	while(ret_n < linkspeed) {
1860		ret_n *= 2;
1861	}
1862	if (ret_n > 0x800000) {
1863		ret_n = 0x800000;
1864	}
1865	uint64 ret_m = timing.pixel_clock * ret_n * bitsPerPixel / linkspeed;
1866	while ((ret_n > 0xffffff) || (ret_m > 0xffffff)) {
1867		ret_m >>= 1;
1868		ret_n >>= 1;
1869	}
1870	//Set TU size bits (to default, max) before link training so that error detection works
1871	write32(INTEL_PIPE_A_DATA_M + fPipeOffset, ret_m | FDI_PIPE_MN_TU_SIZE_MASK);
1872	write32(INTEL_PIPE_A_DATA_N + fPipeOffset, ret_n);
1873
1874	//Setup Link M/N
1875	linkspeed = linkBandwidth;
1876	ret_n = 1;
1877	while(ret_n < linkspeed) {
1878		ret_n *= 2;
1879	}
1880	if (ret_n > 0x800000) {
1881		ret_n = 0x800000;
1882	}
1883	ret_m = timing.pixel_clock * ret_n / linkspeed;
1884	while ((ret_n > 0xffffff) || (ret_m > 0xffffff)) {
1885		ret_m >>= 1;
1886		ret_n >>= 1;
1887	}
1888	write32(INTEL_PIPE_A_LINK_M + fPipeOffset, ret_m);
1889	//Writing Link N triggers all four registers to be activated also (on next VBlank)
1890	write32(INTEL_PIPE_A_LINK_N + fPipeOffset, ret_n);
1891
1892	TRACE("%s: DP M1 data after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_DATA_M + fPipeOffset));
1893	TRACE("%s: DP N1 data after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_DATA_N + fPipeOffset));
1894	TRACE("%s: DP M1 link after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_LINK_M + fPipeOffset));
1895	TRACE("%s: DP N1 link after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_LINK_N + fPipeOffset));
1896
1897	return B_OK;
1898}
1899
1900
1901status_t
1902DisplayPort::_SetPortLinkGen6(const display_timing& timing)
1903{
1904	// Khz / 10. ( each output octet encoded as 10 bits.
1905	//note: (fixme) eDP is fixed option 162 or 270Mc, other DPs go via DPLL programming to one of the same vals.
1906	uint32 linkBandwidth = 270000; //khz
1907	TRACE("%s: DP link reference clock is %gMhz\n", __func__, linkBandwidth / 1000.0f);
1908
1909	uint32 fPipeOffset = 0;
1910	switch (fPipe->Index()) {
1911		case INTEL_PIPE_B:
1912			fPipeOffset = 0x1000;
1913			break;
1914		case INTEL_PIPE_C:
1915			fPipeOffset = 0x2000;
1916			break;
1917		default:
1918			break;
1919	}
1920
1921	TRACE("%s: DP M1 data before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_TRANSCODER_A_DATA_M1 + fPipeOffset));
1922	TRACE("%s: DP N1 data before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_TRANSCODER_A_DATA_N1 + fPipeOffset));
1923	TRACE("%s: DP M1 link before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_TRANSCODER_A_LINK_M1 + fPipeOffset));
1924	TRACE("%s: DP N1 link before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_TRANSCODER_A_LINK_N1 + fPipeOffset));
1925
1926	uint32 bitsPerPixel =
1927		(read32(INTEL_TRANSCODER_A_DP_CTL + fPipeOffset) & INTEL_TRANS_DP_BPC_MASK) >> INTEL_TRANS_DP_COLOR_SHIFT;
1928	switch (bitsPerPixel) {
1929		case PIPE_DDI_8BPC:
1930			bitsPerPixel = 24;
1931			break;
1932		case PIPE_DDI_10BPC:
1933			bitsPerPixel = 30;
1934			break;
1935		case PIPE_DDI_6BPC:
1936			bitsPerPixel = 18;
1937			break;
1938		case PIPE_DDI_12BPC:
1939			bitsPerPixel = 36;
1940			break;
1941		default:
1942			ERROR("%s: DP illegal link colordepth set.\n", __func__);
1943			return B_ERROR;
1944	}
1945	TRACE("%s: DP link colordepth: %" B_PRIu32 "\n", __func__, bitsPerPixel);
1946
1947	uint32 lanes = ((read32(_PortRegister()) & INTEL_DISP_PORT_WIDTH_MASK) >> INTEL_DISP_PORT_WIDTH_SHIFT) + 1;
1948	if (lanes > 4) {
1949		ERROR("%s: DP illegal number of lanes set.\n", __func__);
1950		return B_ERROR;
1951	}
1952	TRACE("%s: DP mode with %" B_PRIx32 " lane(s) in use\n", __func__, lanes);
1953
1954	//Reserving 5% bandwidth for possible spread spectrum clock use
1955	uint32 bps = timing.pixel_clock * bitsPerPixel * 21 / 20;
1956	//use DIV_ROUND_UP:
1957	uint32 required_lanes = (bps + (linkBandwidth * 8) - 1) / (linkBandwidth * 8);
1958	TRACE("%s: DP mode needs %" B_PRIx32 " lane(s) in use\n", __func__, required_lanes);
1959	if (required_lanes > lanes) {
1960		//Note that we *must* abort as otherwise the PIPE/DP-link hangs forever (without retraining!).
1961		ERROR("%s: DP not enough lanes active for requested mode.\n", __func__);
1962		return B_ERROR;
1963	}
1964
1965	//Setup Data M/N
1966	uint64 linkspeed = lanes * linkBandwidth * 8;
1967	uint64 ret_n = 1;
1968	while(ret_n < linkspeed) {
1969		ret_n *= 2;
1970	}
1971	if (ret_n > 0x800000) {
1972		ret_n = 0x800000;
1973	}
1974	uint64 ret_m = timing.pixel_clock * ret_n * bitsPerPixel / linkspeed;
1975	while ((ret_n > 0xffffff) || (ret_m > 0xffffff)) {
1976		ret_m >>= 1;
1977		ret_n >>= 1;
1978	}
1979	//Set TU size bits (to default, max) before link training so that error detection works
1980	write32(INTEL_TRANSCODER_A_DATA_M1 + fPipeOffset, ret_m | INTEL_TRANSCODER_MN_TU_SIZE_MASK);
1981	write32(INTEL_TRANSCODER_A_DATA_N1 + fPipeOffset, ret_n);
1982
1983	//Setup Link M/N
1984	linkspeed = linkBandwidth;
1985	ret_n = 1;
1986	while(ret_n < linkspeed) {
1987		ret_n *= 2;
1988	}
1989	if (ret_n > 0x800000) {
1990		ret_n = 0x800000;
1991	}
1992	ret_m = timing.pixel_clock * ret_n / linkspeed;
1993	while ((ret_n > 0xffffff) || (ret_m > 0xffffff)) {
1994		ret_m >>= 1;
1995		ret_n >>= 1;
1996	}
1997	write32(INTEL_TRANSCODER_A_LINK_M1 + fPipeOffset, ret_m);
1998	//Writing Link N triggers all four registers to be activated also (on next VBlank)
1999	write32(INTEL_TRANSCODER_A_LINK_N1 + fPipeOffset, ret_n);
2000
2001	TRACE("%s: DP M1 data after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_TRANSCODER_A_DATA_M1 + fPipeOffset));
2002	TRACE("%s: DP N1 data after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_TRANSCODER_A_DATA_N1 + fPipeOffset));
2003	TRACE("%s: DP M1 link after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_TRANSCODER_A_LINK_M1 + fPipeOffset));
2004	TRACE("%s: DP N1 link after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_TRANSCODER_A_LINK_N1 + fPipeOffset));
2005
2006	return B_OK;
2007}
2008
2009
2010status_t
2011DisplayPort::SetDisplayMode(display_mode* target, uint32 colorMode)
2012{
2013	CALLED();
2014	TRACE("%s: %s %dx%d\n", __func__, PortName(), target->timing.h_display,
2015		target->timing.v_display);
2016
2017	if (fPipe == NULL) {
2018		ERROR("%s: Setting display mode without assigned pipe!\n", __func__);
2019		return B_ERROR;
2020	}
2021
2022	status_t result = B_OK;
2023	if (gInfo->shared_info->device_type.Generation() <= 4) {
2024		fPipe->ConfigureTimings(target);
2025		result = _SetPortLinkGen4(target->timing);
2026	} else {
2027		display_timing hardwareTarget = target->timing;
2028		bool needsScaling = false;
2029		if ((PortIndex() == INTEL_PORT_A)
2030			&& (gInfo->shared_info->device_type.IsMobile() || _IsEDPPort())) {
2031			// For internal panels, we may need to set the timings according to the panel
2032			// native video mode, and let the panel fitter do the scaling.
2033			// note: upto/including generation 5 laptop panels are still LVDS types, handled elsewhere.
2034
2035			if (gInfo->shared_info->got_vbt) {
2036				// Set vbios hardware panel mode as base
2037				hardwareTarget = gInfo->shared_info->panel_timing;
2038
2039				if (hardwareTarget.h_display == target->timing.h_display
2040						&& hardwareTarget.v_display == target->timing.v_display) {
2041					// We are feeding the native video mode, nothing to do: disable scaling
2042					TRACE("%s: Requested mode is panel's native resolution: disabling scaling\n", __func__);
2043				} else {
2044					// We need to enable the panel fitter
2045					TRACE("%s: Requested mode is not panel's native resolution: enabling scaling\n", __func__);
2046					needsScaling = true;
2047				}
2048			} else {
2049				//fixme: We should now first try for EDID info detailed timing, highest res in list: that's the
2050				//native mode as well. If we also have no EDID, then fallback to setting mode directly as below.
2051
2052				TRACE("%s: Setting internal panel mode without VBT info generation, scaling may not work\n",
2053					__func__);
2054				// We don't have VBT data, try to set the requested mode directly
2055				// and hope for the best
2056				hardwareTarget = target->timing;
2057			}
2058		}
2059
2060		result = B_OK;
2061		if (PortIndex() != INTEL_PORT_A)
2062			result = _SetPortLinkGen6(hardwareTarget);
2063
2064		if (result == B_OK) {
2065			// Setup PanelFitter and Train FDI if it exists
2066			PanelFitter* fitter = fPipe->PFT();
2067			if (fitter != NULL)
2068				fitter->Enable(hardwareTarget);
2069
2070			uint32 lanes = 0;
2071			uint32 linkBandwidth = 0;
2072			uint32 bitsPerPixel = 0;
2073			if (PortIndex() != INTEL_PORT_A) {
2074				FDILink* link = fPipe->FDI();
2075				if (link != NULL) {
2076					link->PreTrain(&hardwareTarget, &linkBandwidth, &lanes, &bitsPerPixel);
2077					fPipe->SetFDILink(hardwareTarget, linkBandwidth, lanes, bitsPerPixel);
2078					link->Train(&hardwareTarget, lanes);
2079				}
2080			} else {
2081				// 'local' eDP port is in use
2082				linkBandwidth =
2083					(read32(INTEL_DISPLAY_PORT_A) & INTEL_DISP_EDP_PLL_FREQ_MASK) >> INTEL_DISP_EDP_PLL_FREQ_SHIFT;
2084				switch (linkBandwidth) {
2085					case INTEL_DISP_EDP_PLL_FREQ_270:
2086						linkBandwidth = 270000; //khz
2087						break;
2088					case INTEL_DISP_EDP_PLL_FREQ_162:
2089						linkBandwidth = 162000; //khz
2090						break;
2091					default:
2092						TRACE("%s: eDP illegal reference clock ID set, assuming 270Mhz.\n", __func__);
2093						linkBandwidth = 270000; //khz
2094				}
2095
2096				bitsPerPixel =
2097					(read32(INTEL_DISPLAY_A_PIPE_CONTROL) & INTEL_PIPE_BPC_MASK) >> INTEL_PIPE_COLOR_SHIFT;
2098				switch (bitsPerPixel) {
2099					case INTEL_PIPE_8BPC:
2100						bitsPerPixel = 24;
2101						break;
2102					case INTEL_PIPE_10BPC:
2103						bitsPerPixel = 30;
2104						break;
2105					case INTEL_PIPE_6BPC:
2106						bitsPerPixel = 18;
2107						break;
2108					case INTEL_PIPE_12BPC:
2109						bitsPerPixel = 36;
2110						break;
2111					default:
2112						bitsPerPixel = 0;
2113				}
2114
2115				lanes =
2116					((read32(INTEL_DISPLAY_PORT_A) & INTEL_DISP_PORT_WIDTH_MASK) >> INTEL_DISP_PORT_WIDTH_SHIFT) + 1;
2117
2118				fPipe->SetFDILink(hardwareTarget, linkBandwidth, lanes, bitsPerPixel);
2119			}
2120
2121			// Program general pipe config
2122			fPipe->Configure(target);
2123
2124			// Pll programming is not needed for (e)DP..
2125
2126			// Program target display mode
2127			fPipe->ConfigureTimings(target, !needsScaling, PortIndex());
2128		} else {
2129			TRACE("%s: Setting display mode via fallback: using scaling!\n", __func__);
2130			// Keep monitor at native mode and scale image to that
2131			fPipe->ConfigureScalePos(target);
2132		}
2133	}
2134
2135	// Set fCurrentMode to our set display mode
2136	memcpy(&fCurrentMode, target, sizeof(display_mode));
2137
2138	return result;
2139}
2140
2141
2142// #pragma mark - Embedded DisplayPort
2143
2144
2145EmbeddedDisplayPort::EmbeddedDisplayPort()
2146	:
2147	DisplayPort(INTEL_PORT_A, "Embedded DisplayPort")
2148{
2149}
2150
2151
2152bool
2153EmbeddedDisplayPort::IsConnected()
2154{
2155	addr_t portRegister = _PortRegister();
2156
2157	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
2158		portRegister);
2159
2160	if (!gInfo->shared_info->device_type.IsMobile()) {
2161		TRACE("%s: skipping eDP on non-mobile GPU\n", __func__);
2162		return false;
2163	}
2164
2165	if ((read32(portRegister) & DISPLAY_MONITOR_PORT_DETECTED) == 0) {
2166		TRACE("%s: %s link not detected\n", __func__, PortName());
2167		return false;
2168	}
2169
2170	HasEDID();
2171
2172	// If eDP has EDID, awesome. We use it.
2173	// No EDID? The modesetting code falls back to VBIOS panel_mode
2174	return true;
2175}
2176
2177
2178// #pragma mark - Digital Display Port
2179
2180
2181DigitalDisplayInterface::DigitalDisplayInterface(port_index index,
2182		const char* baseName)
2183	:
2184	Port(index, baseName)
2185{
2186	// As of Haswell, Intel decided to change eDP ports to a "DDI" bus...
2187	// on a dare because the hardware engineers were drunk one night.
2188}
2189
2190
2191addr_t
2192DigitalDisplayInterface::_PortRegister()
2193{
2194	// TODO: Linux does a DDI_BUF_CTL(INTEL_PORT_A) which is cleaner
2195	// (but we have to ensure the offsets + region base is correct)
2196	switch (PortIndex()) {
2197		case INTEL_PORT_A:
2198			return DDI_BUF_CTL_A;
2199		case INTEL_PORT_B:
2200			return DDI_BUF_CTL_B;
2201		case INTEL_PORT_C:
2202			return DDI_BUF_CTL_C;
2203		case INTEL_PORT_D:
2204			return DDI_BUF_CTL_D;
2205		case INTEL_PORT_E:
2206			return DDI_BUF_CTL_E;
2207		case INTEL_PORT_F:
2208			if ((gInfo->shared_info->device_type.Generation() > 8) &&
2209				!gInfo->shared_info->device_type.InGroup(INTEL_GROUP_SKY))
2210				return DDI_BUF_CTL_F;
2211			return 0;
2212		case INTEL_PORT_G:
2213			if (gInfo->shared_info->device_type.Generation() >= 12)
2214				return DDI_BUF_CTL_G;
2215			return 0;
2216		default:
2217			return 0;
2218	}
2219	return 0;
2220}
2221
2222
2223addr_t
2224DigitalDisplayInterface::_DDCRegister()
2225{
2226	return Port::_DDCPin();
2227}
2228
2229
2230status_t
2231DigitalDisplayInterface::Power(bool enabled)
2232{
2233	if (fPipe == NULL) {
2234		ERROR("%s: Setting power without assigned pipe!\n", __func__);
2235		return B_ERROR;
2236	}
2237	TRACE("%s: %s DDI enabled: %s\n", __func__, PortName(),
2238		enabled ? "true" : "false");
2239
2240	fPipe->Enable(enabled);
2241
2242	//nogo currently.. (kills output forever)
2243#if 0
2244	addr_t portRegister = _PortRegister();
2245	uint32 state = read32(portRegister);
2246	write32(portRegister,
2247		enabled ? (state | DDI_BUF_CTL_ENABLE) : (state & ~DDI_BUF_CTL_ENABLE));
2248	read32(portRegister);
2249#endif
2250
2251	return B_OK;
2252}
2253
2254
2255status_t
2256DigitalDisplayInterface::SetPipe(Pipe* pipe)
2257{
2258	CALLED();
2259
2260	if (pipe == NULL) {
2261		ERROR("%s: Invalid pipe provided!\n", __func__);
2262		return B_ERROR;
2263	}
2264
2265	// TODO: UnAssignPipe?  This likely needs reworked a little
2266	if (fPipe != NULL) {
2267		ERROR("%s: Can't reassign display pipe (yet)\n", __func__);
2268		return B_ERROR;
2269	}
2270
2271	// all DDI ports pipe selections works differently than on the old port types (indirect).
2272	// fixme: implement..
2273	TRACE("%s: Assuming pipe is assigned by BIOS (fixme)\n", __func__);
2274
2275	fPipe = pipe;
2276
2277	if (fPipe == NULL)
2278		return B_NO_MEMORY;
2279
2280	// Disable display pipe until modesetting enables it
2281	if (fPipe->IsEnabled())
2282		fPipe->Enable(false);
2283
2284	return B_OK;
2285}
2286
2287
2288status_t
2289DigitalDisplayInterface::SetupI2c(i2c_bus *bus)
2290{
2291	CALLED();
2292
2293	const uint32 deviceConfigCount = gInfo->shared_info->device_config_count;
2294	if (gInfo->shared_info->device_type.Generation() >= 6 && deviceConfigCount > 0) {
2295		if (!_IsDisplayPortInVBT())
2296			return Port::SetupI2c(bus);
2297	}
2298
2299	return _SetupDpAuxI2c(bus);
2300}
2301
2302
2303status_t
2304DigitalDisplayInterface::SetupI2cFallback(i2c_bus *bus)
2305{
2306	CALLED();
2307
2308	const uint32 deviceConfigCount = gInfo->shared_info->device_config_count;
2309	if (gInfo->shared_info->device_type.Generation() >= 6 && deviceConfigCount > 0
2310		&& _IsDisplayPortInVBT() && _IsHdmiInVBT()) {
2311		return Port::SetupI2c(bus);
2312	}
2313
2314	return B_ERROR;
2315}
2316
2317
2318bool
2319DigitalDisplayInterface::IsConnected()
2320{
2321	addr_t portRegister = _PortRegister();
2322
2323	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
2324		portRegister);
2325
2326	// Please note: Skylake and up (Desktop) might use eDP for a seperate active VGA converter chip.
2327	if (portRegister == 0) {
2328		TRACE("%s: Port not implemented\n", __func__);
2329		return false;
2330	}
2331
2332	// newer chipsets support 4 lanes on all ports
2333	fMaxLanes = 4;
2334	if ((gInfo->shared_info->device_type.Generation() < 9) ||
2335		gInfo->shared_info->device_type.InGroup(INTEL_GROUP_SKY)) {
2336		// Probe a little port info.
2337		if ((read32(DDI_BUF_CTL_A) & DDI_A_4_LANES) != 0) {
2338			switch (PortIndex()) {
2339				case INTEL_PORT_A:
2340					fMaxLanes = 4;
2341					break;
2342				case INTEL_PORT_E:
2343					fMaxLanes = 0;
2344					break;
2345				default:
2346					fMaxLanes = 4;
2347					break;
2348			}
2349		} else {
2350			switch (PortIndex()) {
2351				case INTEL_PORT_A:
2352					fMaxLanes = 2;
2353					break;
2354				case INTEL_PORT_E:
2355					fMaxLanes = 2;
2356					break;
2357				default:
2358					fMaxLanes = 4;
2359					break;
2360			}
2361		}
2362	}
2363
2364	const uint32 deviceConfigCount = gInfo->shared_info->device_config_count;
2365	if (gInfo->shared_info->device_type.Generation() >= 6 && deviceConfigCount > 0) {
2366		// check VBT mapping
2367		if (!_IsPortInVBT()) {
2368			TRACE("%s: %s: port not found in VBT\n", __func__, PortName());
2369			return false;
2370		} else
2371			TRACE("%s: %s: port found in VBT\n", __func__, PortName());
2372	}
2373
2374	TRACE("%s: %s Maximum Lanes: %" B_PRId8 "\n", __func__,
2375		PortName(), fMaxLanes);
2376
2377	// fetch EDID but determine 'in use' later (below) so we also catch screens that fail EDID
2378	bool edidDetected = HasEDID();
2379
2380	// On laptops we always have an internal panel.. (on the eDP port on DDI systems, fixed on eDP pipe)
2381	uint32 pipeState = 0;
2382	if ((gInfo->shared_info->device_type.IsMobile() || _IsEDPPort())
2383		&& (PortIndex() == INTEL_PORT_A)) {
2384		if (gInfo->shared_info->device_type.Generation() < 12) {
2385			// TODO: the pipe state isn't reliable after gen11
2386			pipeState = read32(PIPE_DDI_FUNC_CTL_EDP);
2387			TRACE("%s: PIPE_DDI_FUNC_CTL_EDP: 0x%" B_PRIx32 "\n", __func__, pipeState);
2388			if (!(pipeState & PIPE_DDI_FUNC_CTL_ENABLE)) {
2389				TRACE("%s: Laptop, but eDP port down\n", __func__);
2390				return false;
2391			}
2392		}
2393		if (edidDetected)
2394			return true;
2395		else if (gInfo->shared_info->has_vesa_edid_info) {
2396			TRACE("%s: Laptop. Using VESA edid info\n", __func__);
2397			memcpy(&fEDIDInfo, &gInfo->shared_info->vesa_edid_info,	sizeof(edid1_info));
2398			if (fEDIDState != B_OK) {
2399				fEDIDState = B_OK;
2400				// HasEDID now true
2401				edid_dump(&fEDIDInfo);
2402			}
2403			return true;
2404		} else if (gInfo->shared_info->got_vbt) {
2405			TRACE("%s: Laptop. No VESA EDID, but force enabled as we have a VBT\n", __func__);
2406			return true;
2407		}
2408		//should not happen:
2409		TRACE("%s: No (panel) type info found, assuming not connected\n", __func__);
2410		return false;
2411	}
2412
2413	// scan all our non-eDP pipes to find the one connected to the current port and check it's enabled
2414	for (uint32 pipeCnt = 0; pipeCnt < 3; pipeCnt++) {
2415		switch (pipeCnt) {
2416			case 1:
2417				pipeState = read32(PIPE_DDI_FUNC_CTL_B);
2418				break;
2419			case 2:
2420				pipeState = read32(PIPE_DDI_FUNC_CTL_C);
2421				break;
2422			default:
2423				pipeState = read32(PIPE_DDI_FUNC_CTL_A);
2424				break;
2425		}
2426		if ((((pipeState & PIPE_DDI_SELECT_MASK) >> PIPE_DDI_SELECT_SHIFT) + 1) == (uint32)PortIndex()) {
2427			TRACE("%s: PIPE_DDI_FUNC_CTL nr %" B_PRIx32 ": 0x%" B_PRIx32 "\n", __func__, pipeCnt + 1, pipeState);
2428			// See if the BIOS enabled our output as it indicates it's in use
2429			if (pipeState & PIPE_DDI_FUNC_CTL_ENABLE) {
2430				TRACE("%s: Connected\n", __func__);
2431				return true;
2432			}
2433		}
2434	}
2435
2436	if (edidDetected) {
2437		for (uint32 pipeCnt = 0; pipeCnt < 3; pipeCnt++) {
2438			uint32 pipeReg = 0;
2439			switch (pipeCnt) {
2440				case 1:
2441					pipeReg = PIPE_DDI_FUNC_CTL_B;
2442					break;
2443				case 2:
2444					pipeReg = PIPE_DDI_FUNC_CTL_C;
2445					break;
2446				default:
2447					pipeReg = PIPE_DDI_FUNC_CTL_A;
2448					break;
2449			}
2450			pipeState = read32(pipeReg);
2451			if ((pipeState & PIPE_DDI_FUNC_CTL_ENABLE) == 0) {
2452				TRACE("%s: Connected but port down\n", __func__);
2453				return false;
2454			}
2455			return true;
2456		}
2457		TRACE("%s: No pipe available, ignoring connected screen\n", __func__);
2458	}
2459
2460	TRACE("%s: Not connected\n", __func__);
2461	return false;
2462}
2463
2464status_t
2465DigitalDisplayInterface::_SetPortLinkGen8(const display_timing& timing, uint32 pllSel)
2466{
2467	//fixme: always so on pre gen 9?
2468	uint32 linkBandwidth = 270000; //khz
2469
2470	if (gInfo->shared_info->device_type.Generation() >= 9) {
2471		if (pllSel != 0xff) {
2472			linkBandwidth = (read32(SKL_DPLL_CTRL1) >> (1 + 6 * pllSel)) & SKL_DPLL_DP_LINKRATE_MASK;
2473			switch (linkBandwidth) {
2474				case SKL_DPLL_CTRL1_2700:
2475					linkBandwidth = 2700000 / 5;
2476					break;
2477				case SKL_DPLL_CTRL1_1350:
2478					linkBandwidth = 1350000 / 5;
2479					break;
2480				case SKL_DPLL_CTRL1_810:
2481					linkBandwidth =  810000 / 5;
2482					break;
2483				case SKL_DPLL_CTRL1_1620:
2484					linkBandwidth = 1620000 / 5;
2485					break;
2486				case SKL_DPLL_CTRL1_1080:
2487					linkBandwidth = 1080000 / 5;
2488					break;
2489				case SKL_DPLL_CTRL1_2160:
2490					linkBandwidth = 2160000 / 5;
2491					break;
2492				default:
2493					linkBandwidth = 270000;
2494					ERROR("%s: DDI No known DP-link reference clock selected, assuming default\n", __func__);
2495					break;
2496			}
2497		} else {
2498			ERROR("%s: DDI No known PLL selected, assuming default DP-link reference\n", __func__);
2499		}
2500	}
2501	TRACE("%s: DDI DP-link reference clock is %gMhz\n", __func__, linkBandwidth / 1000.0f);
2502
2503	uint32 fPipeOffset = 0;
2504	switch (fPipe->Index()) {
2505		case INTEL_PIPE_B:
2506			fPipeOffset = 0x1000;
2507			break;
2508		case INTEL_PIPE_C:
2509			fPipeOffset = 0x2000;
2510			break;
2511		case INTEL_PIPE_D:
2512			fPipeOffset = 0xf000;
2513			break;
2514		default:
2515			break;
2516	}
2517
2518	TRACE("%s: DDI M1 data before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_DATA_M + fPipeOffset));
2519	TRACE("%s: DDI N1 data before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_DATA_N + fPipeOffset));
2520	TRACE("%s: DDI M1 link before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_LINK_M + fPipeOffset));
2521	TRACE("%s: DDI N1 link before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_LINK_N + fPipeOffset));
2522
2523	uint32 pipeFunc = read32(PIPE_DDI_FUNC_CTL_A + fPipeOffset);
2524	uint32 bitsPerPixel = (pipeFunc & PIPE_DDI_BPC_MASK) >> PIPE_DDI_COLOR_SHIFT;
2525	switch (bitsPerPixel) {
2526		case PIPE_DDI_8BPC:
2527			bitsPerPixel = 24;
2528			break;
2529		case PIPE_DDI_10BPC:
2530			bitsPerPixel = 30;
2531			break;
2532		case PIPE_DDI_6BPC:
2533			bitsPerPixel = 18;
2534			break;
2535		case PIPE_DDI_12BPC:
2536			bitsPerPixel = 36;
2537			break;
2538		default:
2539			ERROR("%s: DDI illegal link colordepth set.\n", __func__);
2540			return B_ERROR;
2541	}
2542	TRACE("%s: DDI Link Colordepth: %" B_PRIu32 "\n", __func__, bitsPerPixel);
2543
2544	uint32 lanes = 4;
2545	// Only DP modes supports less than 4 lanes: read current config
2546	if (((pipeFunc & PIPE_DDI_MODESEL_MASK) >> PIPE_DDI_MODESEL_SHIFT) >= PIPE_DDI_MODE_DP_SST) {
2547		// On gen 9.5 IceLake 3x mode exists (DSI only), earlier models: reserved value.
2548		lanes = ((pipeFunc & PIPE_DDI_DP_WIDTH_MASK) >> PIPE_DDI_DP_WIDTH_SHIFT) + 1;
2549		TRACE("%s: DDI in DP mode with %" B_PRIx32 " lane(s) in use\n", __func__, lanes);
2550	} else {
2551		TRACE("%s: DDI in non-DP mode with %" B_PRIx32 " lane(s) in use\n", __func__, lanes);
2552	}
2553
2554	//Setup Data M/N
2555	uint64 linkspeed = lanes * linkBandwidth * 8;
2556	uint64 ret_n = 1;
2557	while(ret_n < linkspeed) {
2558		ret_n *= 2;
2559	}
2560	if (ret_n > 0x800000) {
2561		ret_n = 0x800000;
2562	}
2563	uint64 ret_m = timing.pixel_clock * ret_n * bitsPerPixel / linkspeed;
2564	while ((ret_n > 0xffffff) || (ret_m > 0xffffff)) {
2565		ret_m >>= 1;
2566		ret_n >>= 1;
2567	}
2568	//Set TU size bits (to default, max) before link training so that error detection works
2569	write32(INTEL_DDI_PIPE_A_DATA_M + fPipeOffset, ret_m | FDI_PIPE_MN_TU_SIZE_MASK);
2570	write32(INTEL_DDI_PIPE_A_DATA_N + fPipeOffset, ret_n);
2571
2572	//Setup Link M/N
2573	linkspeed = linkBandwidth;
2574	ret_n = 1;
2575	while(ret_n < linkspeed) {
2576		ret_n *= 2;
2577	}
2578	if (ret_n > 0x800000) {
2579		ret_n = 0x800000;
2580	}
2581	ret_m = timing.pixel_clock * ret_n / linkspeed;
2582	while ((ret_n > 0xffffff) || (ret_m > 0xffffff)) {
2583		ret_m >>= 1;
2584		ret_n >>= 1;
2585	}
2586	write32(INTEL_DDI_PIPE_A_LINK_M + fPipeOffset, ret_m);
2587	//Writing Link N triggers all four registers to be activated also (on next VBlank)
2588	write32(INTEL_DDI_PIPE_A_LINK_N + fPipeOffset, ret_n);
2589
2590	TRACE("%s: DDI M1 data after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_DATA_M + fPipeOffset));
2591	TRACE("%s: DDI N1 data after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_DATA_N + fPipeOffset));
2592	TRACE("%s: DDI M1 link after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_LINK_M + fPipeOffset));
2593	TRACE("%s: DDI N1 link after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_LINK_N + fPipeOffset));
2594
2595	return B_OK;
2596}
2597
2598status_t
2599DigitalDisplayInterface::SetDisplayMode(display_mode* target, uint32 colorMode)
2600{
2601	CALLED();
2602	TRACE("%s: %s %dx%d\n", __func__, PortName(), target->timing.h_display,
2603		target->timing.v_display);
2604
2605	if (fPipe == NULL) {
2606		ERROR("%s: Setting display mode without assigned pipe!\n", __func__);
2607		return B_ERROR;
2608	}
2609
2610	display_timing hardwareTarget = target->timing;
2611	bool needsScaling = false;
2612	if ((PortIndex() == INTEL_PORT_A)
2613		&& (gInfo->shared_info->device_type.IsMobile() || _IsEDPPort())) {
2614		// For internal panels, we may need to set the timings according to the panel
2615		// native video mode, and let the panel fitter do the scaling.
2616
2617		if (gInfo->shared_info->got_vbt || HasEDID()) {
2618			// Set vbios hardware panel mode as base
2619			hardwareTarget = gInfo->shared_info->panel_timing;
2620			if (HasEDID()) {
2621				// the first detailed timing supposed to be the best supported one
2622				int i;
2623				for (i = 0; i < EDID1_NUM_DETAILED_MONITOR_DESC; ++i) {
2624					edid1_detailed_monitor *monitor = &fEDIDInfo.detailed_monitor[i];
2625					if (monitor->monitor_desc_type == EDID1_IS_DETAILED_TIMING)
2626						break;
2627				}
2628				if (i < EDID1_NUM_DETAILED_MONITOR_DESC) {
2629					TRACE("%s: Using EDID detailed timing %d for the internal panel\n",
2630						__func__, i);
2631					const edid1_detailed_timing& timing
2632						= fEDIDInfo.detailed_monitor[i].data.detailed_timing;
2633					hardwareTarget.pixel_clock = timing.pixel_clock * 10;
2634					hardwareTarget.h_display = timing.h_active;
2635					hardwareTarget.h_sync_start = timing.h_active + timing.h_sync_off;
2636					hardwareTarget.h_sync_end = hardwareTarget.h_sync_start + timing.h_sync_width;
2637					hardwareTarget.h_total = timing.h_active + timing.h_blank;
2638					hardwareTarget.v_display = timing.v_active;
2639					hardwareTarget.v_sync_start = timing.v_active + timing.v_sync_off;
2640					hardwareTarget.v_sync_end = hardwareTarget.v_sync_start + timing.v_sync_width;
2641					hardwareTarget.v_total = timing.v_active + timing.v_blank;
2642					hardwareTarget.flags = 0;
2643					if (timing.sync == 3) {
2644						if (timing.misc & 1)
2645							hardwareTarget.flags |= B_POSITIVE_HSYNC;
2646						if (timing.misc & 2)
2647							hardwareTarget.flags |= B_POSITIVE_VSYNC;
2648					}
2649					if (timing.interlaced)
2650						hardwareTarget.flags |= B_TIMING_INTERLACED;
2651				}
2652			}
2653			if (hardwareTarget.h_display == target->timing.h_display
2654					&& hardwareTarget.v_display == target->timing.v_display) {
2655				// We are setting the native video mode, nothing special to do
2656				// Note: this means refresh and timing might vary according to requested mode.
2657				hardwareTarget = target->timing;
2658				TRACE("%s: Setting internal panel to native resolution at %" B_PRIu32 "Hz\n", __func__,
2659					hardwareTarget.pixel_clock * 1000 / (hardwareTarget.h_total * hardwareTarget.v_total));
2660			} else {
2661				// We need to enable the panel fitter
2662				TRACE("%s: Hardware mode will actually be %dx%d at %" B_PRIu32 "Hz\n", __func__,
2663					hardwareTarget.h_display, hardwareTarget.v_display,
2664					hardwareTarget.pixel_clock * 1000 / (hardwareTarget.h_total * hardwareTarget.v_total));
2665
2666				// FIXME we should also get the refresh frequency from the target
2667				// mode, and then "sanitize" the resulting mode we made up.
2668				needsScaling = true;
2669			}
2670		} else {
2671			TRACE("%s: Setting internal panel mode without VBT info generation, scaling may not work\n",
2672				__func__);
2673			// We don't have VBT data, try to set the requested mode directly
2674			// and hope for the best
2675			hardwareTarget = target->timing;
2676		}
2677	}
2678
2679	// Setup PanelFitter
2680	PanelFitter* fitter = fPipe->PFT();
2681	if (fitter != NULL)
2682		fitter->Enable(hardwareTarget);
2683
2684	// skip FDI as it never applies to DDI (on gen7 and 8 only for the real analog VGA port)
2685
2686	// Program general pipe config
2687	fPipe->Configure(target);
2688
2689	uint32 pllSel = 0xff; // no PLL selected
2690	if (gInfo->shared_info->device_type.Generation() <= 8) {
2691		unsigned int r2_out, n2_out, p_out;
2692		hsw_ddi_calculate_wrpll(
2693			hardwareTarget.pixel_clock * 1000 /* in Hz */,
2694			&r2_out, &n2_out, &p_out);
2695	} else {
2696		skl_wrpll_params wrpll_params;
2697		skl_ddi_calculate_wrpll(
2698			hardwareTarget.pixel_clock * 1000 /* in Hz */,
2699			gInfo->shared_info->pll_info.reference_frequency,
2700			&wrpll_params);
2701		fPipe->ConfigureClocksSKL(wrpll_params,
2702			hardwareTarget.pixel_clock,
2703			PortIndex(),
2704			&pllSel);
2705	}
2706
2707	// Program target display mode
2708	fPipe->ConfigureTimings(target, !needsScaling);
2709	_SetPortLinkGen8(hardwareTarget, pllSel);
2710
2711	// Set fCurrentMode to our set display mode
2712	memcpy(&fCurrentMode, target, sizeof(display_mode));
2713
2714	return B_OK;
2715}
2716