xref: /openbmc/u-boot/drivers/video/logicore_dp_tx.c (revision 282ce6454c45409805e2c470bbec170d21a4bd35)
1*25a9f974SMario Six // SPDX-License-Identifier: GPL-2.0+
2*25a9f974SMario Six /*
3*25a9f974SMario Six  * logicore_dp_tx.c
4*25a9f974SMario Six  *
5*25a9f974SMario Six  * Driver for XILINX LogiCore DisplayPort v6.1 TX (Source)
6*25a9f974SMario Six  * based on Xilinx dp_v3_1 driver sources, updated to dp_v4_0
7*25a9f974SMario Six  *
8*25a9f974SMario Six  * (C) Copyright 2016
9*25a9f974SMario Six  * Dirk Eibach,  Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
10*25a9f974SMario Six  */
11*25a9f974SMario Six 
12*25a9f974SMario Six #include <common.h>
13*25a9f974SMario Six #include <clk.h>
14*25a9f974SMario Six #include <display.h>
15*25a9f974SMario Six #include <dm.h>
16*25a9f974SMario Six #include <errno.h>
17*25a9f974SMario Six 
18*25a9f974SMario Six #include "axi.h"
19*25a9f974SMario Six #include "logicore_dp_dpcd.h"
20*25a9f974SMario Six #include "logicore_dp_tx.h"
21*25a9f974SMario Six #include "logicore_dp_tx_regif.h"
22*25a9f974SMario Six 
23*25a9f974SMario Six /* Default AXI clock frequency value */
24*25a9f974SMario Six #define S_AXI_CLK_DEFAULT 100000000
25*25a9f974SMario Six 
26*25a9f974SMario Six /* Default DP phy clock value */
27*25a9f974SMario Six #define PHY_CLOCK_SELECT_DEFAULT PHY_CLOCK_SELECT_540GBPS
28*25a9f974SMario Six 
29*25a9f974SMario Six /* The maximum voltage swing level is 3 */
30*25a9f974SMario Six #define MAXIMUM_VS_LEVEL 3
31*25a9f974SMario Six /* The maximum pre-emphasis level is 3 */
32*25a9f974SMario Six #define MAXIMUM_PE_LEVEL 3
33*25a9f974SMario Six 
34*25a9f974SMario Six /* Error out if an AUX request yields a defer reply more than 50 times */
35*25a9f974SMario Six #define AUX_MAX_DEFER_COUNT 50
36*25a9f974SMario Six /* Error out if an AUX request times out more than 50 times awaiting a reply */
37*25a9f974SMario Six #define AUX_MAX_TIMEOUT_COUNT 50
38*25a9f974SMario Six /* Error out if checking for a connected device times out more than 50 times */
39*25a9f974SMario Six #define IS_CONNECTED_MAX_TIMEOUT_COUNT 50
40*25a9f974SMario Six 
41*25a9f974SMario Six /**
42*25a9f974SMario Six  * enum link_training_states - States for link training state machine
43*25a9f974SMario Six  * @TS_CLOCK_RECOVERY:       State for clock recovery
44*25a9f974SMario Six  * @TS_CHANNEL_EQUALIZATION: State for channel equalization
45*25a9f974SMario Six  * @TS_ADJUST_LINK_RATE:     State where link rate is reduced in reaction to
46*25a9f974SMario Six  *			     failed link training
47*25a9f974SMario Six  * @TS_ADJUST_LANE_COUNT:    State where lane count is reduced in reaction to
48*25a9f974SMario Six  *			     failed link training
49*25a9f974SMario Six  * @TS_FAILURE:              State of link training failure
50*25a9f974SMario Six  * @TS_SUCCESS::             State for successfully completed link training
51*25a9f974SMario Six  */
52*25a9f974SMario Six enum link_training_states {
53*25a9f974SMario Six 	TS_CLOCK_RECOVERY,
54*25a9f974SMario Six 	TS_CHANNEL_EQUALIZATION,
55*25a9f974SMario Six 	TS_ADJUST_LINK_RATE,
56*25a9f974SMario Six 	TS_ADJUST_LANE_COUNT,
57*25a9f974SMario Six 	TS_FAILURE,
58*25a9f974SMario Six 	TS_SUCCESS
59*25a9f974SMario Six };
60*25a9f974SMario Six 
61*25a9f974SMario Six /**
62*25a9f974SMario Six  * struct aux_transaction - Description of an AUX channel transaction
63*25a9f974SMario Six  * @cmd_code:  Command code of the transaction
64*25a9f974SMario Six  * @num_bytes: The number of bytes in the transaction's payload data
65*25a9f974SMario Six  * @address:   The DPCD address of the transaction
66*25a9f974SMario Six  * @data:      Payload data of the AUX channel transaction
67*25a9f974SMario Six  */
68*25a9f974SMario Six struct aux_transaction {
69*25a9f974SMario Six 	u16 cmd_code;
70*25a9f974SMario Six 	u8 num_bytes;
71*25a9f974SMario Six 	u32 address;
72*25a9f974SMario Six 	u8 *data;
73*25a9f974SMario Six };
74*25a9f974SMario Six 
75*25a9f974SMario Six /**
76*25a9f974SMario Six  * struct main_stream_attributes - Main stream attributes
77*25a9f974SMario Six  * @pixel_clock_hz: Pixel clock of the stream (in Hz)
78*25a9f974SMario Six  * @misc_0:                    Miscellaneous stream attributes 0 as specified
79*25a9f974SMario Six  *			       by the DisplayPort 1.2 specification
80*25a9f974SMario Six  * @misc_1:                    Miscellaneous stream attributes 1 as specified
81*25a9f974SMario Six  *			       by the DisplayPort 1.2 specification
82*25a9f974SMario Six  * @n_vid:                     N value for the video stream
83*25a9f974SMario Six  * @m_vid:                     M value used to recover the video clock from the
84*25a9f974SMario Six  *			       link clock
85*25a9f974SMario Six  * @user_pixel_width:          Width of the user data input port
86*25a9f974SMario Six  * @data_per_lane:             Used to translate the number of pixels per line
87*25a9f974SMario Six  *			       to the native internal 16-bit datapath
88*25a9f974SMario Six  * @avg_bytes_per_tu:          Average number of bytes per transfer unit,
89*25a9f974SMario Six  *			       scaled up by a factor of 1000
90*25a9f974SMario Six  * @transfer_unit_size:        Size of the transfer unit in the framing logic
91*25a9f974SMario Six  *			       In MST mode, this is also the number of time
92*25a9f974SMario Six  *			       slots that are alloted in the payload ID table
93*25a9f974SMario Six  * @init_wait:                 Number of initial wait cycles at the start of a
94*25a9f974SMario Six  *			       new line by the framing logic
95*25a9f974SMario Six  * @bits_per_color:            Bits per color component
96*25a9f974SMario Six  * @component_format:          The component format currently in use by the
97*25a9f974SMario Six  *			       video stream
98*25a9f974SMario Six  * @dynamic_range:             The dynamic range currently in use by the video
99*25a9f974SMario Six  *			       stream
100*25a9f974SMario Six  * @y_cb_cr_colorimetry:       The YCbCr colorimetry currently in use by the
101*25a9f974SMario Six  *			       video stream
102*25a9f974SMario Six  * @synchronous_clock_mode:    Synchronous clock mode is currently in use by
103*25a9f974SMario Six  *			       the video stream
104*25a9f974SMario Six  * @override_user_pixel_width: If set to 1, the value stored for
105*25a9f974SMario Six  *			       user_pixel_width will be used as the pixel width
106*25a9f974SMario Six  * @h_start:                   Horizontal blank start (pixels)
107*25a9f974SMario Six  * @h_active:                  Horizontal active resolution (pixels)
108*25a9f974SMario Six  * @h_sync_width:              Horizontal sync width (pixels)
109*25a9f974SMario Six  * @h_total:                   Horizontal total (pixels)
110*25a9f974SMario Six  * @h_sync_polarity:           Horizontal sync polarity (0=neg|1=pos)
111*25a9f974SMario Six  * @v_start:                   Vertical blank start (in lines)
112*25a9f974SMario Six  * @v_active:                  Vertical active resolution (lines)
113*25a9f974SMario Six  * @v_sync_width:              Vertical sync width (lines)
114*25a9f974SMario Six  * @v_total:                   Vertical total (lines)
115*25a9f974SMario Six  * @v_sync_polarity:           Vertical sync polarity (0=neg|1=pos)
116*25a9f974SMario Six  *
117*25a9f974SMario Six  * All porch parameters have been removed, because our videodata is
118*25a9f974SMario Six  * hstart/vstart based, and there is no benefit in keeping the porches
119*25a9f974SMario Six  */
120*25a9f974SMario Six struct main_stream_attributes {
121*25a9f974SMario Six 	u32 pixel_clock_hz;
122*25a9f974SMario Six 	u32 misc_0;
123*25a9f974SMario Six 	u32 misc_1;
124*25a9f974SMario Six 	u32 n_vid;
125*25a9f974SMario Six 	//u32 m_vid;
126*25a9f974SMario Six 	u32 user_pixel_width;
127*25a9f974SMario Six 	u32 data_per_lane;
128*25a9f974SMario Six 	u32 avg_bytes_per_tu;
129*25a9f974SMario Six 	u32 transfer_unit_size;
130*25a9f974SMario Six 	u32 init_wait;
131*25a9f974SMario Six 	u32 bits_per_color;
132*25a9f974SMario Six 	u8 component_format;
133*25a9f974SMario Six 	u8 dynamic_range;
134*25a9f974SMario Six 	u8 y_cb_cr_colorimetry;
135*25a9f974SMario Six 	u8 synchronous_clock_mode;
136*25a9f974SMario Six 	u8 override_user_pixel_width;
137*25a9f974SMario Six 	u32 h_start;
138*25a9f974SMario Six 	u16 h_active;
139*25a9f974SMario Six 	u16 h_sync_width;
140*25a9f974SMario Six 	u16 h_total;
141*25a9f974SMario Six 	bool h_sync_polarity;
142*25a9f974SMario Six 	u32 v_start;
143*25a9f974SMario Six 	u16 v_active;
144*25a9f974SMario Six 	u16 v_sync_width;
145*25a9f974SMario Six 	u16 v_total;
146*25a9f974SMario Six 	bool v_sync_polarity;
147*25a9f974SMario Six };
148*25a9f974SMario Six 
149*25a9f974SMario Six /**
150*25a9f974SMario Six  * struct link_config - Description of link configuration
151*25a9f974SMario Six  * @lane_count:                    Currently selected lane count for this link
152*25a9f974SMario Six  * @link_rate:                     Currently selected link rate for this link
153*25a9f974SMario Six  * @scrambler_en:                  Flag to determine whether the scrambler is
154*25a9f974SMario Six  *				   enabled for this link
155*25a9f974SMario Six  * @enhanced_framing_mode:         Flag to determine whether enhanced framing
156*25a9f974SMario Six  *				   mode is active for this link
157*25a9f974SMario Six  * @max_lane_count:                Maximum lane count for this link
158*25a9f974SMario Six  * @max_link_rate:                 Maximum link rate for this link
159*25a9f974SMario Six  * @support_enhanced_framing_mode: Flag to indicate whether the link supports
160*25a9f974SMario Six  *				   enhanced framing mode
161*25a9f974SMario Six  * @vs_level:                      Voltage swing for each lane
162*25a9f974SMario Six  * @pe_level:                      Pre-emphasis/cursor level for each lane
163*25a9f974SMario Six  */
164*25a9f974SMario Six struct link_config {
165*25a9f974SMario Six 	u8 lane_count;
166*25a9f974SMario Six 	u8 link_rate;
167*25a9f974SMario Six 	bool scrambler_en;
168*25a9f974SMario Six 	bool enhanced_framing_mode;
169*25a9f974SMario Six 	u8 max_lane_count;
170*25a9f974SMario Six 	u8 max_link_rate;
171*25a9f974SMario Six 	bool support_enhanced_framing_mode;
172*25a9f974SMario Six 	u8 vs_level;
173*25a9f974SMario Six 	u8 pe_level;
174*25a9f974SMario Six };
175*25a9f974SMario Six 
176*25a9f974SMario Six /**
177*25a9f974SMario Six  * struct dp_tx - Private data structure of LogiCore DP TX devices
178*25a9f974SMario Six  *
179*25a9f974SMario Six  * @base:                   Address of register base of device
180*25a9f974SMario Six  * @s_axi_clk:              The AXI clock frequency in Hz
181*25a9f974SMario Six  * @train_adaptive:         Use adaptive link trainig (i.e. successively reduce
182*25a9f974SMario Six  *			    link rate and/or lane count) for this device
183*25a9f974SMario Six  * @max_link_rate:          Maximum link rate for this device
184*25a9f974SMario Six  * @max_lane_count:         Maximum lane count for this device
185*25a9f974SMario Six  * @dpcd_rx_caps:           RX device's status registers, see below
186*25a9f974SMario Six  * @lane_status_ajd_reqs:   Lane status and adjustment requests information for
187*25a9f974SMario Six  *			    this device
188*25a9f974SMario Six  * @link_config:            The link configuration for this device
189*25a9f974SMario Six  * @main_stream_attributes: MSA set for this device
190*25a9f974SMario Six  *
191*25a9f974SMario Six  * dpcd_rx_caps is a raw read of the RX device's status registers. The first 4
192*25a9f974SMario Six  * bytes correspond to the lane status associated with clock recovery, channel
193*25a9f974SMario Six  * equalization, symbol lock, and interlane alignment. The remaining 2 bytes
194*25a9f974SMario Six  * represent the pre-emphasis and voltage swing level adjustments requested by
195*25a9f974SMario Six  * the RX device.
196*25a9f974SMario Six  */
197*25a9f974SMario Six struct dp_tx {
198*25a9f974SMario Six 	u32 base;
199*25a9f974SMario Six 	u32 s_axi_clk;
200*25a9f974SMario Six 	bool train_adaptive;
201*25a9f974SMario Six 	u8 max_link_rate;
202*25a9f974SMario Six 	u8 max_lane_count;
203*25a9f974SMario Six 	u8 dpcd_rx_caps[16];
204*25a9f974SMario Six 	u8 lane_status_ajd_reqs[6];
205*25a9f974SMario Six 	struct link_config link_config;
206*25a9f974SMario Six 	struct main_stream_attributes main_stream_attributes;
207*25a9f974SMario Six };
208*25a9f974SMario Six 
209*25a9f974SMario Six /*
210*25a9f974SMario Six  * Internal API
211*25a9f974SMario Six  */
212*25a9f974SMario Six 
213*25a9f974SMario Six /**
214*25a9f974SMario Six  * get_reg() - Read a register of a LogiCore DP TX device
215*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
216*25a9f974SMario Six  * @reg: The offset of the register to read
217*25a9f974SMario Six  *
218*25a9f974SMario Six  * Return: The read register value
219*25a9f974SMario Six  */
get_reg(struct udevice * dev,u32 reg)220*25a9f974SMario Six static u32 get_reg(struct udevice *dev, u32 reg)
221*25a9f974SMario Six {
222*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
223*25a9f974SMario Six 	u32 value = 0;
224*25a9f974SMario Six 	int res;
225*25a9f974SMario Six 
226*25a9f974SMario Six 	/* TODO(mario.six@gdsys.cc): error handling */
227*25a9f974SMario Six 	res = axi_read(dev->parent, dp_tx->base + reg, &value, AXI_SIZE_32);
228*25a9f974SMario Six 	if (res < 0)
229*25a9f974SMario Six 		printf("%s() failed; res = %d\n", __func__, res);
230*25a9f974SMario Six 
231*25a9f974SMario Six 	return value;
232*25a9f974SMario Six }
233*25a9f974SMario Six 
234*25a9f974SMario Six /**
235*25a9f974SMario Six  * set_reg() - Write a register of a LogiCore DP TX device
236*25a9f974SMario Six  * @dev:   The LogiCore DP TX device in question
237*25a9f974SMario Six  * @reg:   The offset of the register to write
238*25a9f974SMario Six  * @value: The value to write to the register
239*25a9f974SMario Six  */
set_reg(struct udevice * dev,u32 reg,u32 value)240*25a9f974SMario Six static void set_reg(struct udevice *dev, u32 reg, u32 value)
241*25a9f974SMario Six {
242*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
243*25a9f974SMario Six 
244*25a9f974SMario Six 	axi_write(dev->parent, dp_tx->base + reg, &value, AXI_SIZE_32);
245*25a9f974SMario Six }
246*25a9f974SMario Six 
247*25a9f974SMario Six /**
248*25a9f974SMario Six  * is_connected() - Check if there is a connected RX device
249*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
250*25a9f974SMario Six  *
251*25a9f974SMario Six  * The Xilinx original calls msleep_interruptible at least once, ignoring
252*25a9f974SMario Six  * status.
253*25a9f974SMario Six  *
254*25a9f974SMario Six  * Return: true if a connected RX device was detected, false otherwise
255*25a9f974SMario Six  */
is_connected(struct udevice * dev)256*25a9f974SMario Six static bool is_connected(struct udevice *dev)
257*25a9f974SMario Six {
258*25a9f974SMario Six 	u8 retries = 0;
259*25a9f974SMario Six 
260*25a9f974SMario Six 	do {
261*25a9f974SMario Six 		int status = get_reg(dev, REG_INTERRUPT_SIG_STATE) &
262*25a9f974SMario Six 			     INTERRUPT_SIG_STATE_HPD_STATE_MASK;
263*25a9f974SMario Six 		if (status)
264*25a9f974SMario Six 			return true;
265*25a9f974SMario Six 
266*25a9f974SMario Six 		udelay(1000);
267*25a9f974SMario Six 	} while (retries++ < IS_CONNECTED_MAX_TIMEOUT_COUNT);
268*25a9f974SMario Six 
269*25a9f974SMario Six 	return false;
270*25a9f974SMario Six }
271*25a9f974SMario Six 
272*25a9f974SMario Six /**
273*25a9f974SMario Six  * wait_phy_ready() - Wait for the DisplayPort PHY to come out of reset
274*25a9f974SMario Six  * @dev:  The LogiCore DP TX device in question
275*25a9f974SMario Six  * @mask: Bit mask specifying which bit in the status register should be waited
276*25a9f974SMario Six  *	  for
277*25a9f974SMario Six  *
278*25a9f974SMario Six  * Return: 0 if wait succeeded, -ve if error occurred
279*25a9f974SMario Six  */
wait_phy_ready(struct udevice * dev,u32 mask)280*25a9f974SMario Six static int wait_phy_ready(struct udevice *dev, u32 mask)
281*25a9f974SMario Six {
282*25a9f974SMario Six 	u16 timeout = 20000;
283*25a9f974SMario Six 	u32 phy_status;
284*25a9f974SMario Six 
285*25a9f974SMario Six 	/* Wait until the PHY is ready. */
286*25a9f974SMario Six 	do {
287*25a9f974SMario Six 		phy_status = get_reg(dev, REG_PHY_STATUS) & mask;
288*25a9f974SMario Six 
289*25a9f974SMario Six 		/* Protect against an infinite loop. */
290*25a9f974SMario Six 		if (!timeout--)
291*25a9f974SMario Six 			return -ETIMEDOUT;
292*25a9f974SMario Six 
293*25a9f974SMario Six 		udelay(20);
294*25a9f974SMario Six 	} while (phy_status != mask);
295*25a9f974SMario Six 
296*25a9f974SMario Six 	return 0;
297*25a9f974SMario Six }
298*25a9f974SMario Six 
299*25a9f974SMario Six /* AUX channel access */
300*25a9f974SMario Six 
301*25a9f974SMario Six /**
302*25a9f974SMario Six  * aux_wait_ready() -  Wait until another request is no longer in progress
303*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
304*25a9f974SMario Six  *
305*25a9f974SMario Six  * Return: 0 if wait succeeded, -ve if error occurred
306*25a9f974SMario Six  */
aux_wait_ready(struct udevice * dev)307*25a9f974SMario Six static int aux_wait_ready(struct udevice *dev)
308*25a9f974SMario Six {
309*25a9f974SMario Six 	int status;
310*25a9f974SMario Six 	u32 timeout = 100;
311*25a9f974SMario Six 
312*25a9f974SMario Six 	/* Wait until the DisplayPort TX core is ready. */
313*25a9f974SMario Six 	do {
314*25a9f974SMario Six 		status = get_reg(dev, REG_INTERRUPT_SIG_STATE);
315*25a9f974SMario Six 
316*25a9f974SMario Six 		/* Protect against an infinite loop. */
317*25a9f974SMario Six 		if (!timeout--)
318*25a9f974SMario Six 			return -ETIMEDOUT;
319*25a9f974SMario Six 		udelay(20);
320*25a9f974SMario Six 	} while (status & REPLY_STATUS_REPLY_IN_PROGRESS_MASK);
321*25a9f974SMario Six 
322*25a9f974SMario Six 	return 0;
323*25a9f974SMario Six }
324*25a9f974SMario Six 
325*25a9f974SMario Six /**
326*25a9f974SMario Six  * aux_wait_reply() - Wait for reply on AUX channel
327*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
328*25a9f974SMario Six  *
329*25a9f974SMario Six  * Wait for a reply indicating that the most recent AUX request
330*25a9f974SMario Six  * has been received by the RX device.
331*25a9f974SMario Six  *
332*25a9f974SMario Six  * Return: 0 if wait succeeded, -ve if error occurred
333*25a9f974SMario Six  */
aux_wait_reply(struct udevice * dev)334*25a9f974SMario Six static int aux_wait_reply(struct udevice *dev)
335*25a9f974SMario Six {
336*25a9f974SMario Six 	u32 timeout = 100;
337*25a9f974SMario Six 
338*25a9f974SMario Six 	while (timeout > 0) {
339*25a9f974SMario Six 		int status = get_reg(dev, REG_REPLY_STATUS);
340*25a9f974SMario Six 
341*25a9f974SMario Six 		/* Check for error. */
342*25a9f974SMario Six 		if (status & REPLY_STATUS_REPLY_ERROR_MASK)
343*25a9f974SMario Six 			return -ETIMEDOUT;
344*25a9f974SMario Six 
345*25a9f974SMario Six 		/* Check for a reply. */
346*25a9f974SMario Six 		if ((status & REPLY_STATUS_REPLY_RECEIVED_MASK) &&
347*25a9f974SMario Six 		    !(status &
348*25a9f974SMario Six 		      REPLY_STATUS_REQUEST_IN_PROGRESS_MASK) &&
349*25a9f974SMario Six 		    !(status &
350*25a9f974SMario Six 		      REPLY_STATUS_REPLY_IN_PROGRESS_MASK)) {
351*25a9f974SMario Six 			return 0;
352*25a9f974SMario Six 		}
353*25a9f974SMario Six 
354*25a9f974SMario Six 		timeout--;
355*25a9f974SMario Six 		udelay(20);
356*25a9f974SMario Six 	}
357*25a9f974SMario Six 
358*25a9f974SMario Six 	return -ETIMEDOUT;
359*25a9f974SMario Six }
360*25a9f974SMario Six 
361*25a9f974SMario Six /**
362*25a9f974SMario Six  * aux_request_send() - Send request on the AUX channel
363*25a9f974SMario Six  * @dev:     The LogiCore DP TX device in question
364*25a9f974SMario Six  * @request: The request to send
365*25a9f974SMario Six  *
366*25a9f974SMario Six  * Submit the supplied AUX request to the RX device over the AUX
367*25a9f974SMario Six  * channel by writing the command, the destination address, (the write buffer
368*25a9f974SMario Six  * for write commands), and the data size to the DisplayPort TX core.
369*25a9f974SMario Six  *
370*25a9f974SMario Six  * This is the lower-level sending routine, which is called by aux_request().
371*25a9f974SMario Six  *
372*25a9f974SMario Six  * Return: 0 if request was sent successfully, -ve on error
373*25a9f974SMario Six  */
aux_request_send(struct udevice * dev,struct aux_transaction * request)374*25a9f974SMario Six static int aux_request_send(struct udevice *dev,
375*25a9f974SMario Six 			    struct aux_transaction *request)
376*25a9f974SMario Six {
377*25a9f974SMario Six 	u32 timeout_count;
378*25a9f974SMario Six 	int status;
379*25a9f974SMario Six 	u8 index;
380*25a9f974SMario Six 
381*25a9f974SMario Six 	/* Ensure that any pending AUX transactions have completed. */
382*25a9f974SMario Six 	timeout_count = 0;
383*25a9f974SMario Six 	do {
384*25a9f974SMario Six 		status = get_reg(dev, REG_REPLY_STATUS);
385*25a9f974SMario Six 
386*25a9f974SMario Six 		udelay(20);
387*25a9f974SMario Six 		timeout_count++;
388*25a9f974SMario Six 		if (timeout_count >= AUX_MAX_TIMEOUT_COUNT)
389*25a9f974SMario Six 			return -ETIMEDOUT;
390*25a9f974SMario Six 	} while ((status & REPLY_STATUS_REQUEST_IN_PROGRESS_MASK) ||
391*25a9f974SMario Six 		 (status & REPLY_STATUS_REPLY_IN_PROGRESS_MASK));
392*25a9f974SMario Six 
393*25a9f974SMario Six 	set_reg(dev, REG_AUX_ADDRESS, request->address);
394*25a9f974SMario Six 
395*25a9f974SMario Six 	if (request->cmd_code == AUX_CMD_WRITE ||
396*25a9f974SMario Six 	    request->cmd_code == AUX_CMD_I2C_WRITE ||
397*25a9f974SMario Six 	    request->cmd_code == AUX_CMD_I2C_WRITE_MOT) {
398*25a9f974SMario Six 		/* Feed write data into the DisplayPort TX core's write FIFO. */
399*25a9f974SMario Six 		for (index = 0; index < request->num_bytes; index++) {
400*25a9f974SMario Six 			set_reg(dev,
401*25a9f974SMario Six 				REG_AUX_WRITE_FIFO, request->data[index]);
402*25a9f974SMario Six 		}
403*25a9f974SMario Six 	}
404*25a9f974SMario Six 
405*25a9f974SMario Six 	/* Submit the command and the data size. */
406*25a9f974SMario Six 	set_reg(dev, REG_AUX_CMD,
407*25a9f974SMario Six 		((request->cmd_code << AUX_CMD_SHIFT) |
408*25a9f974SMario Six 		 ((request->num_bytes - 1) &
409*25a9f974SMario Six 		  AUX_CMD_NBYTES_TRANSFER_MASK)));
410*25a9f974SMario Six 
411*25a9f974SMario Six 	/* Check for a reply from the RX device to the submitted request. */
412*25a9f974SMario Six 	status = aux_wait_reply(dev);
413*25a9f974SMario Six 	if (status)
414*25a9f974SMario Six 		/* Waiting for a reply timed out. */
415*25a9f974SMario Six 		return -ETIMEDOUT;
416*25a9f974SMario Six 
417*25a9f974SMario Six 	/* Analyze the reply. */
418*25a9f974SMario Six 	status = get_reg(dev, REG_AUX_REPLY_CODE);
419*25a9f974SMario Six 	if (status == AUX_REPLY_CODE_DEFER ||
420*25a9f974SMario Six 	    status == AUX_REPLY_CODE_I2C_DEFER) {
421*25a9f974SMario Six 		/* The request was deferred. */
422*25a9f974SMario Six 		return -EAGAIN;
423*25a9f974SMario Six 	} else if ((status == AUX_REPLY_CODE_NACK) ||
424*25a9f974SMario Six 		   (status == AUX_REPLY_CODE_I2C_NACK)) {
425*25a9f974SMario Six 		/* The request was not acknowledged. */
426*25a9f974SMario Six 		return -EIO;
427*25a9f974SMario Six 	}
428*25a9f974SMario Six 
429*25a9f974SMario Six 	/* The request was acknowledged. */
430*25a9f974SMario Six 
431*25a9f974SMario Six 	if (request->cmd_code == AUX_CMD_READ ||
432*25a9f974SMario Six 	    request->cmd_code == AUX_CMD_I2C_READ ||
433*25a9f974SMario Six 	    request->cmd_code == AUX_CMD_I2C_READ_MOT) {
434*25a9f974SMario Six 		/* Wait until all data has been received. */
435*25a9f974SMario Six 		timeout_count = 0;
436*25a9f974SMario Six 		do {
437*25a9f974SMario Six 			status = get_reg(dev, REG_REPLY_DATA_COUNT);
438*25a9f974SMario Six 
439*25a9f974SMario Six 			udelay(100);
440*25a9f974SMario Six 			timeout_count++;
441*25a9f974SMario Six 			if (timeout_count >= AUX_MAX_TIMEOUT_COUNT)
442*25a9f974SMario Six 				return -ETIMEDOUT;
443*25a9f974SMario Six 		} while (status != request->num_bytes);
444*25a9f974SMario Six 
445*25a9f974SMario Six 		/* Obtain the read data from the reply FIFO. */
446*25a9f974SMario Six 		for (index = 0; index < request->num_bytes; index++)
447*25a9f974SMario Six 			request->data[index] = get_reg(dev, REG_AUX_REPLY_DATA);
448*25a9f974SMario Six 	}
449*25a9f974SMario Six 
450*25a9f974SMario Six 	return 0;
451*25a9f974SMario Six }
452*25a9f974SMario Six 
453*25a9f974SMario Six /**
454*25a9f974SMario Six  * aux_request() - Submit request on the AUX channel
455*25a9f974SMario Six  * @dev:     The LogiCore DP TX device in question
456*25a9f974SMario Six  * @request: The request to submit
457*25a9f974SMario Six  *
458*25a9f974SMario Six  * Submit the supplied AUX request to the RX device over the AUX
459*25a9f974SMario Six  * channel. If waiting for a reply times out, or if the DisplayPort TX core
460*25a9f974SMario Six  * indicates that the request was deferred, the request is sent again (up to a
461*25a9f974SMario Six  * maximum specified by AUX_MAX_DEFER_COUNT|AUX_MAX_TIMEOUT_COUNT).
462*25a9f974SMario Six  *
463*25a9f974SMario Six  * Return: 0 if request was submitted successfully, -ve on error
464*25a9f974SMario Six  */
aux_request(struct udevice * dev,struct aux_transaction * request)465*25a9f974SMario Six static int aux_request(struct udevice *dev, struct aux_transaction *request)
466*25a9f974SMario Six {
467*25a9f974SMario Six 	u32 defer_count = 0;
468*25a9f974SMario Six 	u32 timeout_count = 0;
469*25a9f974SMario Six 
470*25a9f974SMario Six 	while ((defer_count < AUX_MAX_DEFER_COUNT) &&
471*25a9f974SMario Six 	       (timeout_count < AUX_MAX_TIMEOUT_COUNT)) {
472*25a9f974SMario Six 		int status = aux_wait_ready(dev);
473*25a9f974SMario Six 
474*25a9f974SMario Six 		if (status) {
475*25a9f974SMario Six 			/* The RX device isn't ready yet. */
476*25a9f974SMario Six 			timeout_count++;
477*25a9f974SMario Six 			continue;
478*25a9f974SMario Six 		}
479*25a9f974SMario Six 
480*25a9f974SMario Six 		status = aux_request_send(dev, request);
481*25a9f974SMario Six 		if (status == -EAGAIN) {
482*25a9f974SMario Six 			/* The request was deferred. */
483*25a9f974SMario Six 			defer_count++;
484*25a9f974SMario Six 		} else if (status == -ETIMEDOUT) {
485*25a9f974SMario Six 			/* Waiting for a reply timed out. */
486*25a9f974SMario Six 			timeout_count++;
487*25a9f974SMario Six 		} else {
488*25a9f974SMario Six 			/*
489*25a9f974SMario Six 			 * -EIO indicates that the request was NACK'ed,
490*25a9f974SMario Six 			 * 0 indicates that the request was ACK'ed.
491*25a9f974SMario Six 			 */
492*25a9f974SMario Six 			return status;
493*25a9f974SMario Six 		}
494*25a9f974SMario Six 
495*25a9f974SMario Six 		udelay(100);
496*25a9f974SMario Six 	}
497*25a9f974SMario Six 
498*25a9f974SMario Six 	/* The request was not successfully received by the RX device. */
499*25a9f974SMario Six 	return -ETIMEDOUT;
500*25a9f974SMario Six }
501*25a9f974SMario Six 
502*25a9f974SMario Six /**
503*25a9f974SMario Six  * aux_common() - Common (read/write) AUX communication transmission
504*25a9f974SMario Six  * @dev:       The LogiCore DP TX device in question
505*25a9f974SMario Six  * @cmd_type:  Command code of the transaction
506*25a9f974SMario Six  * @address:   The DPCD address of the transaction
507*25a9f974SMario Six  * @num_bytes: Number of bytes in the payload data
508*25a9f974SMario Six  * @data:      The payload data of the AUX command
509*25a9f974SMario Six  *
510*25a9f974SMario Six  * Common sequence of submitting an AUX command for AUX read, AUX write,
511*25a9f974SMario Six  * I2C-over-AUX read, and I2C-over-AUX write transactions. If required, the
512*25a9f974SMario Six  * reads and writes are split into multiple requests, each acting on a maximum
513*25a9f974SMario Six  * of 16 bytes.
514*25a9f974SMario Six  *
515*25a9f974SMario Six  * Return: 0 if OK, -ve on error
516*25a9f974SMario Six  */
aux_common(struct udevice * dev,u32 cmd_type,u32 address,u32 num_bytes,u8 * data)517*25a9f974SMario Six static int aux_common(struct udevice *dev, u32 cmd_type, u32 address,
518*25a9f974SMario Six 		      u32 num_bytes, u8 *data)
519*25a9f974SMario Six {
520*25a9f974SMario Six 	struct aux_transaction request;
521*25a9f974SMario Six 	u32 bytes_left;
522*25a9f974SMario Six 
523*25a9f974SMario Six 	/*
524*25a9f974SMario Six 	 * Set the start address for AUX transactions. For I2C transactions,
525*25a9f974SMario Six 	 * this is the address of the I2C bus.
526*25a9f974SMario Six 	 */
527*25a9f974SMario Six 	request.address = address;
528*25a9f974SMario Six 
529*25a9f974SMario Six 	bytes_left = num_bytes;
530*25a9f974SMario Six 	while (bytes_left) {
531*25a9f974SMario Six 		int status;
532*25a9f974SMario Six 
533*25a9f974SMario Six 		request.cmd_code = cmd_type;
534*25a9f974SMario Six 
535*25a9f974SMario Six 		if (cmd_type == AUX_CMD_READ ||
536*25a9f974SMario Six 		    cmd_type == AUX_CMD_WRITE) {
537*25a9f974SMario Six 			/* Increment address for normal AUX transactions. */
538*25a9f974SMario Six 			request.address = address + (num_bytes - bytes_left);
539*25a9f974SMario Six 		}
540*25a9f974SMario Six 
541*25a9f974SMario Six 		/* Increment the pointer to the supplied data buffer. */
542*25a9f974SMario Six 		request.data = &data[num_bytes - bytes_left];
543*25a9f974SMario Six 
544*25a9f974SMario Six 		request.num_bytes = (bytes_left > 16) ? 16 : bytes_left;
545*25a9f974SMario Six 		bytes_left -= request.num_bytes;
546*25a9f974SMario Six 
547*25a9f974SMario Six 		if (cmd_type == AUX_CMD_I2C_READ && bytes_left) {
548*25a9f974SMario Six 			/*
549*25a9f974SMario Six 			 * Middle of a transaction I2C read request. Override
550*25a9f974SMario Six 			 * the command code that was set to cmd_type.
551*25a9f974SMario Six 			 */
552*25a9f974SMario Six 			request.cmd_code = AUX_CMD_I2C_READ_MOT;
553*25a9f974SMario Six 		} else if ((cmd_type == AUX_CMD_I2C_WRITE) && bytes_left) {
554*25a9f974SMario Six 			/*
555*25a9f974SMario Six 			 * Middle of a transaction I2C write request. Override
556*25a9f974SMario Six 			 * the command code that was set to cmd_type.
557*25a9f974SMario Six 			 */
558*25a9f974SMario Six 			request.cmd_code = AUX_CMD_I2C_WRITE_MOT;
559*25a9f974SMario Six 		}
560*25a9f974SMario Six 
561*25a9f974SMario Six 		status = aux_request(dev, &request);
562*25a9f974SMario Six 		if (status)
563*25a9f974SMario Six 			return status;
564*25a9f974SMario Six 	}
565*25a9f974SMario Six 
566*25a9f974SMario Six 	return 0;
567*25a9f974SMario Six }
568*25a9f974SMario Six 
569*25a9f974SMario Six /**
570*25a9f974SMario Six  * aux_read() - Issue AUX read request
571*25a9f974SMario Six  * @dev:           The LogiCore DP TX device in question
572*25a9f974SMario Six  * @dpcd_address:  The DPCD address to read from
573*25a9f974SMario Six  * @bytes_to_read: Number of bytes to read
574*25a9f974SMario Six  * @read_data:     Buffer to receive the read data
575*25a9f974SMario Six  *
576*25a9f974SMario Six  * Issue a read request over the AUX channel that will read from the RX
577*25a9f974SMario Six  * device's DisplayPort Configuration data (DPCD) address space. The read
578*25a9f974SMario Six  * message will be divided into multiple transactions which read a maximum of
579*25a9f974SMario Six  * 16 bytes each.
580*25a9f974SMario Six  *
581*25a9f974SMario Six  * Return: 0 if read operation was successful, -ve on error
582*25a9f974SMario Six  */
aux_read(struct udevice * dev,u32 dpcd_address,u32 bytes_to_read,void * read_data)583*25a9f974SMario Six static int aux_read(struct udevice *dev, u32 dpcd_address, u32 bytes_to_read,
584*25a9f974SMario Six 		    void *read_data)
585*25a9f974SMario Six {
586*25a9f974SMario Six 	int status;
587*25a9f974SMario Six 
588*25a9f974SMario Six 	if (!is_connected(dev))
589*25a9f974SMario Six 		return -ENODEV;
590*25a9f974SMario Six 
591*25a9f974SMario Six 	/* Send AUX read transaction. */
592*25a9f974SMario Six 	status = aux_common(dev, AUX_CMD_READ, dpcd_address,
593*25a9f974SMario Six 			    bytes_to_read, (u8 *)read_data);
594*25a9f974SMario Six 
595*25a9f974SMario Six 	return status;
596*25a9f974SMario Six }
597*25a9f974SMario Six 
598*25a9f974SMario Six /**
599*25a9f974SMario Six  * aux_write() - Issue AUX write request
600*25a9f974SMario Six  * @dev:            The LogiCore DP TX device in question
601*25a9f974SMario Six  * @dpcd_address:   The DPCD address to write to
602*25a9f974SMario Six  * @bytes_to_write: Number of bytes to write
603*25a9f974SMario Six  * @write_data:     Buffer containig data to be written
604*25a9f974SMario Six  *
605*25a9f974SMario Six  * Issue a write request over the AUX channel that will write to
606*25a9f974SMario Six  * the RX device's DisplayPort Configuration data (DPCD) address space. The
607*25a9f974SMario Six  * write message will be divided into multiple transactions which write a
608*25a9f974SMario Six  * maximum of 16 bytes each.
609*25a9f974SMario Six  *
610*25a9f974SMario Six  * Return: 0 if write operation was successful, -ve on error
611*25a9f974SMario Six  */
aux_write(struct udevice * dev,u32 dpcd_address,u32 bytes_to_write,void * write_data)612*25a9f974SMario Six static int aux_write(struct udevice *dev, u32 dpcd_address, u32 bytes_to_write,
613*25a9f974SMario Six 		     void *write_data)
614*25a9f974SMario Six {
615*25a9f974SMario Six 	int status;
616*25a9f974SMario Six 
617*25a9f974SMario Six 	if (!is_connected(dev))
618*25a9f974SMario Six 		return -ENODEV;
619*25a9f974SMario Six 
620*25a9f974SMario Six 	/* Send AUX write transaction. */
621*25a9f974SMario Six 	status = aux_common(dev, AUX_CMD_WRITE, dpcd_address,
622*25a9f974SMario Six 			    bytes_to_write, (u8 *)write_data);
623*25a9f974SMario Six 
624*25a9f974SMario Six 	return status;
625*25a9f974SMario Six }
626*25a9f974SMario Six 
627*25a9f974SMario Six /* Core initialization */
628*25a9f974SMario Six 
629*25a9f974SMario Six /**
630*25a9f974SMario Six  * initialize() - Initialize a LogiCore DP TX device
631*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
632*25a9f974SMario Six  *
633*25a9f974SMario Six  * Return: Always 0
634*25a9f974SMario Six  */
initialize(struct udevice * dev)635*25a9f974SMario Six static int initialize(struct udevice *dev)
636*25a9f974SMario Six {
637*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
638*25a9f974SMario Six 	u32 val;
639*25a9f974SMario Six 	u32 phy_config;
640*25a9f974SMario Six 	unsigned int k;
641*25a9f974SMario Six 
642*25a9f974SMario Six 	/* place the PHY (and GTTXRESET) into reset. */
643*25a9f974SMario Six 	phy_config = get_reg(dev, REG_PHY_CONFIG);
644*25a9f974SMario Six 	set_reg(dev, REG_PHY_CONFIG, phy_config | PHY_CONFIG_GT_ALL_RESET_MASK);
645*25a9f974SMario Six 
646*25a9f974SMario Six 	/* reset the video streams and AUX logic. */
647*25a9f974SMario Six 	set_reg(dev, REG_SOFT_RESET,
648*25a9f974SMario Six 		SOFT_RESET_VIDEO_STREAM_ALL_MASK |
649*25a9f974SMario Six 		SOFT_RESET_AUX_MASK);
650*25a9f974SMario Six 
651*25a9f974SMario Six 	/* disable the DisplayPort TX core. */
652*25a9f974SMario Six 	set_reg(dev, REG_ENABLE, 0);
653*25a9f974SMario Six 
654*25a9f974SMario Six 	/* set the clock divider. */
655*25a9f974SMario Six 	val = get_reg(dev, REG_AUX_CLK_DIVIDER);
656*25a9f974SMario Six 	val &= ~AUX_CLK_DIVIDER_VAL_MASK;
657*25a9f974SMario Six 	val |= dp_tx->s_axi_clk / 1000000;
658*25a9f974SMario Six 	set_reg(dev, REG_AUX_CLK_DIVIDER, val);
659*25a9f974SMario Six 
660*25a9f974SMario Six 	/* set the DisplayPort TX core's clock speed. */
661*25a9f974SMario Six 	set_reg(dev, REG_PHY_CLOCK_SELECT, PHY_CLOCK_SELECT_DEFAULT);
662*25a9f974SMario Six 
663*25a9f974SMario Six 	/* bring the PHY (and GTTXRESET) out of reset. */
664*25a9f974SMario Six 	set_reg(dev, REG_PHY_CONFIG,
665*25a9f974SMario Six 		phy_config & ~PHY_CONFIG_GT_ALL_RESET_MASK);
666*25a9f974SMario Six 
667*25a9f974SMario Six 	/* enable the DisplayPort TX core. */
668*25a9f974SMario Six 	set_reg(dev, REG_ENABLE, 1);
669*25a9f974SMario Six 
670*25a9f974SMario Six 	/* Unmask Hot-Plug-Detect (HPD) interrupts. */
671*25a9f974SMario Six 	set_reg(dev, REG_INTERRUPT_MASK,
672*25a9f974SMario Six 		~INTERRUPT_MASK_HPD_PULSE_DETECTED_MASK &
673*25a9f974SMario Six 		~INTERRUPT_MASK_HPD_EVENT_MASK &
674*25a9f974SMario Six 		~INTERRUPT_MASK_HPD_IRQ_MASK);
675*25a9f974SMario Six 
676*25a9f974SMario Six 	for (k = 0; k < 4; k++) {
677*25a9f974SMario Six 		/* Disable pre-cursor levels. */
678*25a9f974SMario Six 		set_reg(dev, REG_PHY_PRECURSOR_LANE_0 + 4 * k, 0);
679*25a9f974SMario Six 
680*25a9f974SMario Six 		/* Write default voltage swing levels to the TX registers. */
681*25a9f974SMario Six 		set_reg(dev, REG_PHY_VOLTAGE_DIFF_LANE_0 + 4 * k, 0);
682*25a9f974SMario Six 
683*25a9f974SMario Six 		/* Write default pre-emphasis levels to the TX registers. */
684*25a9f974SMario Six 		set_reg(dev, REG_PHY_POSTCURSOR_LANE_0 + 4 * k, 0);
685*25a9f974SMario Six 	}
686*25a9f974SMario Six 
687*25a9f974SMario Six 	return 0;
688*25a9f974SMario Six }
689*25a9f974SMario Six 
690*25a9f974SMario Six /**
691*25a9f974SMario Six  * is_link_rate_valid() - Check if given link rate is valif for device
692*25a9f974SMario Six  * @dev:       The LogiCore DP TX device in question
693*25a9f974SMario Six  * @link_rate: The link rate to be checked for validity
694*25a9f974SMario Six  *
695*25a9f974SMario Six  * Return: true if he supplied link rate is valid, false otherwise
696*25a9f974SMario Six  */
is_link_rate_valid(struct udevice * dev,u8 link_rate)697*25a9f974SMario Six static bool is_link_rate_valid(struct udevice *dev, u8 link_rate)
698*25a9f974SMario Six {
699*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
700*25a9f974SMario Six 	bool valid = true;
701*25a9f974SMario Six 
702*25a9f974SMario Six 	if (link_rate != LINK_BW_SET_162GBPS &&
703*25a9f974SMario Six 	    link_rate != LINK_BW_SET_270GBPS &&
704*25a9f974SMario Six 	    link_rate != LINK_BW_SET_540GBPS)
705*25a9f974SMario Six 		valid = false;
706*25a9f974SMario Six 	else if (link_rate > dp_tx->link_config.max_link_rate)
707*25a9f974SMario Six 		valid = false;
708*25a9f974SMario Six 
709*25a9f974SMario Six 	return valid;
710*25a9f974SMario Six }
711*25a9f974SMario Six 
712*25a9f974SMario Six /**
713*25a9f974SMario Six  * is_lane_count_valid() - Check if given lane count is valif for device
714*25a9f974SMario Six  * @dev:        The LogiCore DP TX device in question
715*25a9f974SMario Six  * @lane_count: The lane count to be checked for validity
716*25a9f974SMario Six  *
717*25a9f974SMario Six  * Return: true if he supplied lane count is valid, false otherwise
718*25a9f974SMario Six  */
is_lane_count_valid(struct udevice * dev,u8 lane_count)719*25a9f974SMario Six static bool is_lane_count_valid(struct udevice *dev, u8 lane_count)
720*25a9f974SMario Six {
721*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
722*25a9f974SMario Six 	bool valid = true;
723*25a9f974SMario Six 
724*25a9f974SMario Six 	if (lane_count != LANE_COUNT_SET_1 &&
725*25a9f974SMario Six 	    lane_count != LANE_COUNT_SET_2 &&
726*25a9f974SMario Six 	    lane_count != LANE_COUNT_SET_4)
727*25a9f974SMario Six 		valid = false;
728*25a9f974SMario Six 	else if (lane_count > dp_tx->link_config.max_lane_count)
729*25a9f974SMario Six 		valid = false;
730*25a9f974SMario Six 
731*25a9f974SMario Six 	return valid;
732*25a9f974SMario Six }
733*25a9f974SMario Six 
734*25a9f974SMario Six /**
735*25a9f974SMario Six  * get_rx_capabilities() - Check if capabilities of RX device are valid for TX
736*25a9f974SMario Six  *			   device
737*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
738*25a9f974SMario Six  *
739*25a9f974SMario Six  * Return: 0 if the capabilities of the RX device are valid for the TX device,
740*25a9f974SMario Six  *	   -ve if not, of an error occurred during capability determination
741*25a9f974SMario Six  */
get_rx_capabilities(struct udevice * dev)742*25a9f974SMario Six static int get_rx_capabilities(struct udevice *dev)
743*25a9f974SMario Six {
744*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
745*25a9f974SMario Six 	int status;
746*25a9f974SMario Six 	u8 rx_max_link_rate;
747*25a9f974SMario Six 	u8 rx_max_lane_count;
748*25a9f974SMario Six 
749*25a9f974SMario Six 	if (!is_connected(dev))
750*25a9f974SMario Six 		return -ENODEV;
751*25a9f974SMario Six 
752*25a9f974SMario Six 	status = aux_read(dev, DPCD_RECEIVER_CAP_FIELD_START, 16,
753*25a9f974SMario Six 			  dp_tx->dpcd_rx_caps);
754*25a9f974SMario Six 	if (status)
755*25a9f974SMario Six 		return -EIO;
756*25a9f974SMario Six 
757*25a9f974SMario Six 	rx_max_link_rate = dp_tx->dpcd_rx_caps[DPCD_MAX_LINK_RATE];
758*25a9f974SMario Six 	rx_max_lane_count = dp_tx->dpcd_rx_caps[DPCD_MAX_LANE_COUNT] &
759*25a9f974SMario Six 			    DPCD_MAX_LANE_COUNT_MASK;
760*25a9f974SMario Six 
761*25a9f974SMario Six 	dp_tx->link_config.max_link_rate =
762*25a9f974SMario Six 		(rx_max_link_rate > dp_tx->max_link_rate) ?
763*25a9f974SMario Six 		dp_tx->max_link_rate : rx_max_link_rate;
764*25a9f974SMario Six 	if (!is_link_rate_valid(dev, rx_max_link_rate))
765*25a9f974SMario Six 		return -EINVAL;
766*25a9f974SMario Six 
767*25a9f974SMario Six 	dp_tx->link_config.max_lane_count =
768*25a9f974SMario Six 		(rx_max_lane_count > dp_tx->max_lane_count) ?
769*25a9f974SMario Six 		dp_tx->max_lane_count : rx_max_lane_count;
770*25a9f974SMario Six 	if (!is_lane_count_valid(dev, rx_max_lane_count))
771*25a9f974SMario Six 		return -EINVAL;
772*25a9f974SMario Six 
773*25a9f974SMario Six 	dp_tx->link_config.support_enhanced_framing_mode =
774*25a9f974SMario Six 		dp_tx->dpcd_rx_caps[DPCD_MAX_LANE_COUNT] &
775*25a9f974SMario Six 		DPCD_ENHANCED_FRAME_SUPPORT_MASK;
776*25a9f974SMario Six 
777*25a9f974SMario Six 	return 0;
778*25a9f974SMario Six }
779*25a9f974SMario Six 
780*25a9f974SMario Six /**
781*25a9f974SMario Six  * enable_main_link() - Switch on main link for a device
782*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
783*25a9f974SMario Six  */
enable_main_link(struct udevice * dev)784*25a9f974SMario Six static void enable_main_link(struct udevice *dev)
785*25a9f974SMario Six {
786*25a9f974SMario Six 	/* reset the scrambler. */
787*25a9f974SMario Six 	set_reg(dev, REG_FORCE_SCRAMBLER_RESET, 0x1);
788*25a9f974SMario Six 
789*25a9f974SMario Six 	/* enable the main stream. */
790*25a9f974SMario Six 	set_reg(dev, REG_ENABLE_MAIN_STREAM, 0x1);
791*25a9f974SMario Six }
792*25a9f974SMario Six 
793*25a9f974SMario Six /**
794*25a9f974SMario Six  * disable_main_link() - Switch off main link for a device
795*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
796*25a9f974SMario Six  */
disable_main_link(struct udevice * dev)797*25a9f974SMario Six static void disable_main_link(struct udevice *dev)
798*25a9f974SMario Six {
799*25a9f974SMario Six 	/* reset the scrambler. */
800*25a9f974SMario Six 	set_reg(dev, REG_FORCE_SCRAMBLER_RESET, 0x1);
801*25a9f974SMario Six 
802*25a9f974SMario Six 	/* Disable the main stream. */
803*25a9f974SMario Six 	set_reg(dev, REG_ENABLE_MAIN_STREAM, 0x0);
804*25a9f974SMario Six }
805*25a9f974SMario Six 
806*25a9f974SMario Six /**
807*25a9f974SMario Six  * reset_dp_phy() - Reset a device
808*25a9f974SMario Six  * @dev:   The LogiCore DP TX device in question
809*25a9f974SMario Six  * @reset: Bit mask determining which bits in the device's config register
810*25a9f974SMario Six  *	   should be set for the reset
811*25a9f974SMario Six  */
reset_dp_phy(struct udevice * dev,u32 reset)812*25a9f974SMario Six static void reset_dp_phy(struct udevice *dev, u32 reset)
813*25a9f974SMario Six {
814*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
815*25a9f974SMario Six 	u32 val;
816*25a9f974SMario Six 
817*25a9f974SMario Six 	set_reg(dev, REG_ENABLE, 0x0);
818*25a9f974SMario Six 
819*25a9f974SMario Six 	val = get_reg(dev, REG_PHY_CONFIG);
820*25a9f974SMario Six 
821*25a9f974SMario Six 	/* Apply reset. */
822*25a9f974SMario Six 	set_reg(dev, REG_PHY_CONFIG, val | reset);
823*25a9f974SMario Six 
824*25a9f974SMario Six 	/* Remove reset. */
825*25a9f974SMario Six 	set_reg(dev, REG_PHY_CONFIG, val);
826*25a9f974SMario Six 
827*25a9f974SMario Six 	/* Wait for the PHY to be ready. */
828*25a9f974SMario Six 	wait_phy_ready(dev, phy_status_lanes_ready_mask(dp_tx->max_lane_count));
829*25a9f974SMario Six 
830*25a9f974SMario Six 	set_reg(dev, REG_ENABLE, 0x1);
831*25a9f974SMario Six }
832*25a9f974SMario Six 
833*25a9f974SMario Six /**
834*25a9f974SMario Six  * set_enhanced_frame_mode() - Enable/Disable enhanced frame mode
835*25a9f974SMario Six  * @dev:    The LogiCore DP TX device in question
836*25a9f974SMario Six  * @enable: Flag to determine whether to enable (1) or disable (0) the enhanced
837*25a9f974SMario Six  *	    frame mode
838*25a9f974SMario Six  *
839*25a9f974SMario Six  * Enable or disable the enhanced framing symbol sequence for
840*25a9f974SMario Six  * both the DisplayPort TX core and the RX device.
841*25a9f974SMario Six  *
842*25a9f974SMario Six  * Return: 0 if enabling/disabling the enhanced frame mode was successful, -ve
843*25a9f974SMario Six  *	   on error
844*25a9f974SMario Six  */
set_enhanced_frame_mode(struct udevice * dev,u8 enable)845*25a9f974SMario Six static int set_enhanced_frame_mode(struct udevice *dev, u8 enable)
846*25a9f974SMario Six {
847*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
848*25a9f974SMario Six 	int status;
849*25a9f974SMario Six 	u8 val;
850*25a9f974SMario Six 
851*25a9f974SMario Six 	if (!is_connected(dev))
852*25a9f974SMario Six 		return -ENODEV;
853*25a9f974SMario Six 
854*25a9f974SMario Six 	if (dp_tx->link_config.support_enhanced_framing_mode)
855*25a9f974SMario Six 		dp_tx->link_config.enhanced_framing_mode = enable;
856*25a9f974SMario Six 	else
857*25a9f974SMario Six 		dp_tx->link_config.enhanced_framing_mode = false;
858*25a9f974SMario Six 
859*25a9f974SMario Six 	/* Write enhanced frame mode enable to the DisplayPort TX core. */
860*25a9f974SMario Six 	set_reg(dev, REG_ENHANCED_FRAME_EN,
861*25a9f974SMario Six 		dp_tx->link_config.enhanced_framing_mode);
862*25a9f974SMario Six 
863*25a9f974SMario Six 	/* Write enhanced frame mode enable to the RX device. */
864*25a9f974SMario Six 	status = aux_read(dev, DPCD_LANE_COUNT_SET, 0x1, &val);
865*25a9f974SMario Six 	if (status)
866*25a9f974SMario Six 		return -EIO;
867*25a9f974SMario Six 
868*25a9f974SMario Six 	if (dp_tx->link_config.enhanced_framing_mode)
869*25a9f974SMario Six 		val |= DPCD_ENHANCED_FRAME_EN_MASK;
870*25a9f974SMario Six 	else
871*25a9f974SMario Six 		val &= ~DPCD_ENHANCED_FRAME_EN_MASK;
872*25a9f974SMario Six 
873*25a9f974SMario Six 	status = aux_write(dev, DPCD_LANE_COUNT_SET, 0x1, &val);
874*25a9f974SMario Six 	if (status)
875*25a9f974SMario Six 		return -EIO;
876*25a9f974SMario Six 
877*25a9f974SMario Six 	return 0;
878*25a9f974SMario Six }
879*25a9f974SMario Six 
880*25a9f974SMario Six /**
881*25a9f974SMario Six  * set_lane_count() - Set the lane count
882*25a9f974SMario Six  * @dev:        The LogiCore DP TX device in question
883*25a9f974SMario Six  * @lane_count: Lane count to set
884*25a9f974SMario Six  *
885*25a9f974SMario Six  * Set the number of lanes to be used by the main link for both
886*25a9f974SMario Six  * the DisplayPort TX core and the RX device.
887*25a9f974SMario Six  *
888*25a9f974SMario Six  * Return: 0 if setting the lane count was successful, -ve on error
889*25a9f974SMario Six  */
set_lane_count(struct udevice * dev,u8 lane_count)890*25a9f974SMario Six static int set_lane_count(struct udevice *dev, u8 lane_count)
891*25a9f974SMario Six {
892*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
893*25a9f974SMario Six 	int status;
894*25a9f974SMario Six 	u8 val;
895*25a9f974SMario Six 
896*25a9f974SMario Six 	if (!is_connected(dev))
897*25a9f974SMario Six 		return -ENODEV;
898*25a9f974SMario Six 
899*25a9f974SMario Six 	printf("       set lane count to %u\n", lane_count);
900*25a9f974SMario Six 
901*25a9f974SMario Six 	dp_tx->link_config.lane_count = lane_count;
902*25a9f974SMario Six 
903*25a9f974SMario Six 	/* Write the new lane count to the DisplayPort TX core. */
904*25a9f974SMario Six 	set_reg(dev, REG_LANE_COUNT_SET, dp_tx->link_config.lane_count);
905*25a9f974SMario Six 
906*25a9f974SMario Six 	/* Write the new lane count to the RX device. */
907*25a9f974SMario Six 	status = aux_read(dev, DPCD_LANE_COUNT_SET, 0x1, &val);
908*25a9f974SMario Six 	if (status)
909*25a9f974SMario Six 		return -EIO;
910*25a9f974SMario Six 	val &= ~DPCD_LANE_COUNT_SET_MASK;
911*25a9f974SMario Six 	val |= dp_tx->link_config.lane_count;
912*25a9f974SMario Six 
913*25a9f974SMario Six 	status = aux_write(dev, DPCD_LANE_COUNT_SET, 0x1, &val);
914*25a9f974SMario Six 	if (status)
915*25a9f974SMario Six 		return -EIO;
916*25a9f974SMario Six 
917*25a9f974SMario Six 	return 0;
918*25a9f974SMario Six }
919*25a9f974SMario Six 
920*25a9f974SMario Six /**
921*25a9f974SMario Six  * set_clk_speed() - Set DP phy clock speed
922*25a9f974SMario Six  * @dev:   The LogiCore DP TX device in question
923*25a9f974SMario Six  * @speed: The clock frquency to set (one of PHY_CLOCK_SELECT_*)
924*25a9f974SMario Six  *
925*25a9f974SMario Six  * Set the clock frequency for the DisplayPort PHY corresponding to a desired
926*25a9f974SMario Six  * data rate.
927*25a9f974SMario Six  *
928*25a9f974SMario Six  * Return: 0 if setting the DP phy clock speed was successful, -ve on error
929*25a9f974SMario Six  */
set_clk_speed(struct udevice * dev,u32 speed)930*25a9f974SMario Six static int set_clk_speed(struct udevice *dev, u32 speed)
931*25a9f974SMario Six {
932*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
933*25a9f974SMario Six 	int status;
934*25a9f974SMario Six 	u32 val;
935*25a9f974SMario Six 	u32 mask;
936*25a9f974SMario Six 
937*25a9f974SMario Six 	/* Disable the DisplayPort TX core first. */
938*25a9f974SMario Six 	val = get_reg(dev, REG_ENABLE);
939*25a9f974SMario Six 	set_reg(dev, REG_ENABLE, 0x0);
940*25a9f974SMario Six 
941*25a9f974SMario Six 	/* Change speed of the feedback clock. */
942*25a9f974SMario Six 	set_reg(dev, REG_PHY_CLOCK_SELECT, speed);
943*25a9f974SMario Six 
944*25a9f974SMario Six 	/* Re-enable the DisplayPort TX core if it was previously enabled. */
945*25a9f974SMario Six 	if (val)
946*25a9f974SMario Six 		set_reg(dev, REG_ENABLE, 0x1);
947*25a9f974SMario Six 
948*25a9f974SMario Six 	/* Wait until the PHY is ready. */
949*25a9f974SMario Six 	mask = phy_status_lanes_ready_mask(dp_tx->max_lane_count);
950*25a9f974SMario Six 	status = wait_phy_ready(dev, mask);
951*25a9f974SMario Six 	if (status)
952*25a9f974SMario Six 		return -EIO;
953*25a9f974SMario Six 
954*25a9f974SMario Six 	return 0;
955*25a9f974SMario Six }
956*25a9f974SMario Six 
957*25a9f974SMario Six /**
958*25a9f974SMario Six  * set_link_rate() - Set the link rate
959*25a9f974SMario Six  * @dev:       The LogiCore DP TX device in question
960*25a9f974SMario Six  * @link_rate: The link rate to set (one of LINK_BW_SET_*)
961*25a9f974SMario Six  *
962*25a9f974SMario Six  * Set the data rate to be used by the main link for both the DisplayPort TX
963*25a9f974SMario Six  * core and the RX device.
964*25a9f974SMario Six  *
965*25a9f974SMario Six  * Return: 0 if setting the link rate was successful, -ve on error
966*25a9f974SMario Six  */
set_link_rate(struct udevice * dev,u8 link_rate)967*25a9f974SMario Six static int set_link_rate(struct udevice *dev, u8 link_rate)
968*25a9f974SMario Six {
969*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
970*25a9f974SMario Six 	int status;
971*25a9f974SMario Six 
972*25a9f974SMario Six 	/* Write a corresponding clock frequency to the DisplayPort TX core. */
973*25a9f974SMario Six 	switch (link_rate) {
974*25a9f974SMario Six 	case LINK_BW_SET_162GBPS:
975*25a9f974SMario Six 		printf("       set link rate to 1.62 Gb/s\n");
976*25a9f974SMario Six 		status = set_clk_speed(dev, PHY_CLOCK_SELECT_162GBPS);
977*25a9f974SMario Six 		break;
978*25a9f974SMario Six 	case LINK_BW_SET_270GBPS:
979*25a9f974SMario Six 		printf("       set link rate to 2.70 Gb/s\n");
980*25a9f974SMario Six 		status = set_clk_speed(dev, PHY_CLOCK_SELECT_270GBPS);
981*25a9f974SMario Six 		break;
982*25a9f974SMario Six 	case LINK_BW_SET_540GBPS:
983*25a9f974SMario Six 		printf("       set link rate to 5.40 Gb/s\n");
984*25a9f974SMario Six 		status = set_clk_speed(dev, PHY_CLOCK_SELECT_540GBPS);
985*25a9f974SMario Six 		break;
986*25a9f974SMario Six 	default:
987*25a9f974SMario Six 		return -EINVAL;
988*25a9f974SMario Six 	}
989*25a9f974SMario Six 	if (status)
990*25a9f974SMario Six 		return -EIO;
991*25a9f974SMario Six 
992*25a9f974SMario Six 	dp_tx->link_config.link_rate = link_rate;
993*25a9f974SMario Six 
994*25a9f974SMario Six 	/* Write new link rate to the DisplayPort TX core. */
995*25a9f974SMario Six 	set_reg(dev, REG_LINK_BW_SET, dp_tx->link_config.link_rate);
996*25a9f974SMario Six 
997*25a9f974SMario Six 	/* Write new link rate to the RX device. */
998*25a9f974SMario Six 	status = aux_write(dev, DPCD_LINK_BW_SET, 1,
999*25a9f974SMario Six 			   &dp_tx->link_config.link_rate);
1000*25a9f974SMario Six 	if (status)
1001*25a9f974SMario Six 		return -EIO;
1002*25a9f974SMario Six 
1003*25a9f974SMario Six 	return 0;
1004*25a9f974SMario Six }
1005*25a9f974SMario Six 
1006*25a9f974SMario Six /* Link training */
1007*25a9f974SMario Six 
1008*25a9f974SMario Six /**
1009*25a9f974SMario Six  * get_training_delay() - Get training delay
1010*25a9f974SMario Six  * @dev:            The LogiCore DP TX device in question
1011*25a9f974SMario Six  * @training_state: The training state for which the required training delay
1012*25a9f974SMario Six  *		    should be queried
1013*25a9f974SMario Six  *
1014*25a9f974SMario Six  * Determine what the RX device's required training delay is for
1015*25a9f974SMario Six  * link training.
1016*25a9f974SMario Six  *
1017*25a9f974SMario Six  * Return: The training delay in us
1018*25a9f974SMario Six  */
get_training_delay(struct udevice * dev,int training_state)1019*25a9f974SMario Six static int get_training_delay(struct udevice *dev, int training_state)
1020*25a9f974SMario Six {
1021*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
1022*25a9f974SMario Six 	u16 delay;
1023*25a9f974SMario Six 
1024*25a9f974SMario Six 	switch (dp_tx->dpcd_rx_caps[DPCD_TRAIN_AUX_RD_INTERVAL]) {
1025*25a9f974SMario Six 	case DPCD_TRAIN_AUX_RD_INT_100_400US:
1026*25a9f974SMario Six 		if (training_state == TS_CLOCK_RECOVERY)
1027*25a9f974SMario Six 			/* delay for the clock recovery phase. */
1028*25a9f974SMario Six 			delay = 100;
1029*25a9f974SMario Six 		else
1030*25a9f974SMario Six 			/* delay for the channel equalization phase. */
1031*25a9f974SMario Six 			delay = 400;
1032*25a9f974SMario Six 		break;
1033*25a9f974SMario Six 	case DPCD_TRAIN_AUX_RD_INT_4MS:
1034*25a9f974SMario Six 		delay = 4000;
1035*25a9f974SMario Six 		break;
1036*25a9f974SMario Six 	case DPCD_TRAIN_AUX_RD_INT_8MS:
1037*25a9f974SMario Six 		delay = 8000;
1038*25a9f974SMario Six 		break;
1039*25a9f974SMario Six 	case DPCD_TRAIN_AUX_RD_INT_12MS:
1040*25a9f974SMario Six 		delay = 12000;
1041*25a9f974SMario Six 		break;
1042*25a9f974SMario Six 	case DPCD_TRAIN_AUX_RD_INT_16MS:
1043*25a9f974SMario Six 		delay = 16000;
1044*25a9f974SMario Six 		break;
1045*25a9f974SMario Six 	default:
1046*25a9f974SMario Six 		/* Default to 20 ms. */
1047*25a9f974SMario Six 		delay = 20000;
1048*25a9f974SMario Six 		break;
1049*25a9f974SMario Six 	}
1050*25a9f974SMario Six 
1051*25a9f974SMario Six 	return delay;
1052*25a9f974SMario Six }
1053*25a9f974SMario Six 
1054*25a9f974SMario Six /**
1055*25a9f974SMario Six  * set_vswing_preemp() - Build AUX data to set voltage swing and pre-emphasis
1056*25a9f974SMario Six  * @dev:      The LogiCore DP TX device in question
1057*25a9f974SMario Six  * @aux_data: Buffer to receive the built AUX data
1058*25a9f974SMario Six  *
1059*25a9f974SMario Six  * Build AUX data to set current voltage swing and pre-emphasis level settings;
1060*25a9f974SMario Six  * the necessary data is taken from the link_config structure.
1061*25a9f974SMario Six  */
set_vswing_preemp(struct udevice * dev,u8 * aux_data)1062*25a9f974SMario Six static void set_vswing_preemp(struct udevice *dev, u8 *aux_data)
1063*25a9f974SMario Six {
1064*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
1065*25a9f974SMario Six 	u8 data;
1066*25a9f974SMario Six 	u8 vs_level_rx = dp_tx->link_config.vs_level;
1067*25a9f974SMario Six 	u8 pe_level_rx = dp_tx->link_config.pe_level;
1068*25a9f974SMario Six 
1069*25a9f974SMario Six 	/* Set up the data buffer for writing to the RX device. */
1070*25a9f974SMario Six 	data = (pe_level_rx << DPCD_TRAINING_LANEX_SET_PE_SHIFT) | vs_level_rx;
1071*25a9f974SMario Six 	/* The maximum voltage swing has been reached. */
1072*25a9f974SMario Six 	if (vs_level_rx == MAXIMUM_VS_LEVEL)
1073*25a9f974SMario Six 		data |= DPCD_TRAINING_LANEX_SET_MAX_VS_MASK;
1074*25a9f974SMario Six 
1075*25a9f974SMario Six 	/* The maximum pre-emphasis level has been reached. */
1076*25a9f974SMario Six 	if (pe_level_rx == MAXIMUM_PE_LEVEL)
1077*25a9f974SMario Six 		data |= DPCD_TRAINING_LANEX_SET_MAX_PE_MASK;
1078*25a9f974SMario Six 	memset(aux_data, data, 4);
1079*25a9f974SMario Six }
1080*25a9f974SMario Six 
1081*25a9f974SMario Six /**
1082*25a9f974SMario Six  * adj_vswing_preemp() - Adjust voltage swing and pre-emphasis
1083*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
1084*25a9f974SMario Six  *
1085*25a9f974SMario Six  * Set new voltage swing and pre-emphasis levels using the
1086*25a9f974SMario Six  * adjustment requests obtained from the RX device.
1087*25a9f974SMario Six  *
1088*25a9f974SMario Six  * Return: 0 if voltage swing and pre-emphasis could be adjusted successfully,
1089*25a9f974SMario Six  *	   -ve on error
1090*25a9f974SMario Six  */
adj_vswing_preemp(struct udevice * dev)1091*25a9f974SMario Six static int adj_vswing_preemp(struct udevice *dev)
1092*25a9f974SMario Six {
1093*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
1094*25a9f974SMario Six 	int status;
1095*25a9f974SMario Six 	u8 index;
1096*25a9f974SMario Six 	u8 vs_level_adj_req[4];
1097*25a9f974SMario Six 	u8 pe_level_adj_req[4];
1098*25a9f974SMario Six 	u8 aux_data[4];
1099*25a9f974SMario Six 	u8 *ajd_reqs = &dp_tx->lane_status_ajd_reqs[4];
1100*25a9f974SMario Six 
1101*25a9f974SMario Six 	/*
1102*25a9f974SMario Six 	 * Analyze the adjustment requests for changes in voltage swing and
1103*25a9f974SMario Six 	 * pre-emphasis levels.
1104*25a9f974SMario Six 	 */
1105*25a9f974SMario Six 	vs_level_adj_req[0] = ajd_reqs[0] & DPCD_ADJ_REQ_LANE_0_2_VS_MASK;
1106*25a9f974SMario Six 	vs_level_adj_req[1] = (ajd_reqs[0] & DPCD_ADJ_REQ_LANE_1_3_VS_MASK) >>
1107*25a9f974SMario Six 			      DPCD_ADJ_REQ_LANE_1_3_VS_SHIFT;
1108*25a9f974SMario Six 	vs_level_adj_req[2] = ajd_reqs[1] & DPCD_ADJ_REQ_LANE_0_2_VS_MASK;
1109*25a9f974SMario Six 	vs_level_adj_req[3] = (ajd_reqs[1] & DPCD_ADJ_REQ_LANE_1_3_VS_MASK) >>
1110*25a9f974SMario Six 			      DPCD_ADJ_REQ_LANE_1_3_VS_SHIFT;
1111*25a9f974SMario Six 	pe_level_adj_req[0] = (ajd_reqs[0] & DPCD_ADJ_REQ_LANE_0_2_PE_MASK) >>
1112*25a9f974SMario Six 			      DPCD_ADJ_REQ_LANE_0_2_PE_SHIFT;
1113*25a9f974SMario Six 	pe_level_adj_req[1] = (ajd_reqs[0] & DPCD_ADJ_REQ_LANE_1_3_PE_MASK) >>
1114*25a9f974SMario Six 			      DPCD_ADJ_REQ_LANE_1_3_PE_SHIFT;
1115*25a9f974SMario Six 	pe_level_adj_req[2] = (ajd_reqs[1] & DPCD_ADJ_REQ_LANE_0_2_PE_MASK) >>
1116*25a9f974SMario Six 			      DPCD_ADJ_REQ_LANE_0_2_PE_SHIFT;
1117*25a9f974SMario Six 	pe_level_adj_req[3] = (ajd_reqs[1] & DPCD_ADJ_REQ_LANE_1_3_PE_MASK) >>
1118*25a9f974SMario Six 			      DPCD_ADJ_REQ_LANE_1_3_PE_SHIFT;
1119*25a9f974SMario Six 
1120*25a9f974SMario Six 	/*
1121*25a9f974SMario Six 	 * Change the drive settings to match the adjustment requests. Use the
1122*25a9f974SMario Six 	 * greatest level requested.
1123*25a9f974SMario Six 	 */
1124*25a9f974SMario Six 	dp_tx->link_config.vs_level = 0;
1125*25a9f974SMario Six 	dp_tx->link_config.pe_level = 0;
1126*25a9f974SMario Six 	for (index = 0; index < dp_tx->link_config.lane_count; index++) {
1127*25a9f974SMario Six 		if (vs_level_adj_req[index] > dp_tx->link_config.vs_level)
1128*25a9f974SMario Six 			dp_tx->link_config.vs_level = vs_level_adj_req[index];
1129*25a9f974SMario Six 		if (pe_level_adj_req[index] > dp_tx->link_config.pe_level)
1130*25a9f974SMario Six 			dp_tx->link_config.pe_level = pe_level_adj_req[index];
1131*25a9f974SMario Six 	}
1132*25a9f974SMario Six 
1133*25a9f974SMario Six 	/*
1134*25a9f974SMario Six 	 * Verify that the voltage swing and pre-emphasis combination is
1135*25a9f974SMario Six 	 * allowed. Some combinations will result in a differential peak-to-peak
1136*25a9f974SMario Six 	 * voltage that is outside the permissible range. See the VESA
1137*25a9f974SMario Six 	 * DisplayPort v1.2 Specification, section 3.1.5.2.
1138*25a9f974SMario Six 	 * The valid combinations are:
1139*25a9f974SMario Six 	 *      PE=0    PE=1    PE=2    PE=3
1140*25a9f974SMario Six 	 * VS=0 valid   valid   valid   valid
1141*25a9f974SMario Six 	 * VS=1 valid   valid   valid
1142*25a9f974SMario Six 	 * VS=2 valid   valid
1143*25a9f974SMario Six 	 * VS=3 valid
1144*25a9f974SMario Six 	 *
1145*25a9f974SMario Six 	 * NOTE:
1146*25a9f974SMario Six 	 * Xilinix dp_v3_1 driver seems to have an off by one error when
1147*25a9f974SMario Six 	 * limiting pe_level which is fixed here.
1148*25a9f974SMario Six 	 */
1149*25a9f974SMario Six 	if (dp_tx->link_config.pe_level > (3 - dp_tx->link_config.vs_level))
1150*25a9f974SMario Six 		dp_tx->link_config.pe_level = 3 - dp_tx->link_config.vs_level;
1151*25a9f974SMario Six 
1152*25a9f974SMario Six 	/*
1153*25a9f974SMario Six 	 * Make the adjustments to both the DisplayPort TX core and the RX
1154*25a9f974SMario Six 	 * device.
1155*25a9f974SMario Six 	 */
1156*25a9f974SMario Six 	set_vswing_preemp(dev, aux_data);
1157*25a9f974SMario Six 	/*
1158*25a9f974SMario Six 	 * Write the voltage swing and pre-emphasis levels for each lane to the
1159*25a9f974SMario Six 	 * RX device.
1160*25a9f974SMario Six 	 */
1161*25a9f974SMario Six 	status = aux_write(dev, DPCD_TRAINING_LANE0_SET, 4, aux_data);
1162*25a9f974SMario Six 	if (status)
1163*25a9f974SMario Six 		return -EIO;
1164*25a9f974SMario Six 
1165*25a9f974SMario Six 	return 0;
1166*25a9f974SMario Six }
1167*25a9f974SMario Six 
1168*25a9f974SMario Six /**
1169*25a9f974SMario Six  * get_lane_status_adj_reqs() - Read lane status and adjustment requests
1170*25a9f974SMario Six  *				information from the device
1171*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
1172*25a9f974SMario Six  *
1173*25a9f974SMario Six  * Do a burst AUX read from the RX device over the AUX channel. The contents of
1174*25a9f974SMario Six  * the status registers will be stored for later use by check_clock_recovery,
1175*25a9f974SMario Six  * check_channel_equalization, and adj_vswing_preemp.
1176*25a9f974SMario Six  *
1177*25a9f974SMario Six  * Return: 0 if the status information were read successfully, -ve on error
1178*25a9f974SMario Six  */
get_lane_status_adj_reqs(struct udevice * dev)1179*25a9f974SMario Six static int get_lane_status_adj_reqs(struct udevice *dev)
1180*25a9f974SMario Six {
1181*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
1182*25a9f974SMario Six 	int status;
1183*25a9f974SMario Six 
1184*25a9f974SMario Six 	/*
1185*25a9f974SMario Six 	 * Read and store 4 bytes of lane status and 2 bytes of adjustment
1186*25a9f974SMario Six 	 * requests.
1187*25a9f974SMario Six 	 */
1188*25a9f974SMario Six 	status = aux_read(dev, DPCD_STATUS_LANE_0_1, 6,
1189*25a9f974SMario Six 			  dp_tx->lane_status_ajd_reqs);
1190*25a9f974SMario Six 	if (status)
1191*25a9f974SMario Six 		return -EIO;
1192*25a9f974SMario Six 
1193*25a9f974SMario Six 	return 0;
1194*25a9f974SMario Six }
1195*25a9f974SMario Six 
1196*25a9f974SMario Six /**
1197*25a9f974SMario Six  * check_clock_recovery() - Check clock recovery success
1198*25a9f974SMario Six  * @dev:        The LogiCore DP TX device in question
1199*25a9f974SMario Six  * @lane_count: The number of lanes for which to check clock recovery success
1200*25a9f974SMario Six  *
1201*25a9f974SMario Six  * Check if the RX device's DisplayPort Configuration data (DPCD) indicates
1202*25a9f974SMario Six  * that the clock recovery sequence during link training was successful - the
1203*25a9f974SMario Six  * RX device's link clock and data recovery unit has realized and maintained
1204*25a9f974SMario Six  * the frequency lock for all lanes currently in use.
1205*25a9f974SMario Six  *
1206*25a9f974SMario Six  * Return: 0 if clock recovery was successful on all lanes in question, -ve if
1207*25a9f974SMario Six  *	   not
1208*25a9f974SMario Six  */
check_clock_recovery(struct udevice * dev,u8 lane_count)1209*25a9f974SMario Six static int check_clock_recovery(struct udevice *dev, u8 lane_count)
1210*25a9f974SMario Six {
1211*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
1212*25a9f974SMario Six 	u8 *lane_status = dp_tx->lane_status_ajd_reqs;
1213*25a9f974SMario Six 
1214*25a9f974SMario Six 	/* Check that all LANEx_CR_DONE bits are set. */
1215*25a9f974SMario Six 	switch (lane_count) {
1216*25a9f974SMario Six 	case LANE_COUNT_SET_4:
1217*25a9f974SMario Six 		if (!(lane_status[1] & DPCD_STATUS_LANE_3_CR_DONE_MASK))
1218*25a9f974SMario Six 			goto out_fail;
1219*25a9f974SMario Six 		if (!(lane_status[1] & DPCD_STATUS_LANE_2_CR_DONE_MASK))
1220*25a9f974SMario Six 			goto out_fail;
1221*25a9f974SMario Six 	/* Drop through and check lane 1. */
1222*25a9f974SMario Six 	case LANE_COUNT_SET_2:
1223*25a9f974SMario Six 		if (!(lane_status[0] & DPCD_STATUS_LANE_1_CR_DONE_MASK))
1224*25a9f974SMario Six 			goto out_fail;
1225*25a9f974SMario Six 	/* Drop through and check lane 0. */
1226*25a9f974SMario Six 	case LANE_COUNT_SET_1:
1227*25a9f974SMario Six 		if (!(lane_status[0] & DPCD_STATUS_LANE_0_CR_DONE_MASK))
1228*25a9f974SMario Six 			goto out_fail;
1229*25a9f974SMario Six 	default:
1230*25a9f974SMario Six 		/* All (lane_count) lanes have achieved clock recovery. */
1231*25a9f974SMario Six 		break;
1232*25a9f974SMario Six 	}
1233*25a9f974SMario Six 
1234*25a9f974SMario Six 	return 0;
1235*25a9f974SMario Six 
1236*25a9f974SMario Six out_fail:
1237*25a9f974SMario Six 	return -EIO;
1238*25a9f974SMario Six }
1239*25a9f974SMario Six 
1240*25a9f974SMario Six /**
1241*25a9f974SMario Six  * check_channel_equalization() - Check channel equalization success
1242*25a9f974SMario Six  * @dev:        The LogiCore DP TX device in question
1243*25a9f974SMario Six  * @lane_count: The number of lanes for which to check channel equalization
1244*25a9f974SMario Six  *		success
1245*25a9f974SMario Six  *
1246*25a9f974SMario Six  * Check if the RX device's DisplayPort Configuration data (DPCD) indicates
1247*25a9f974SMario Six  * that the channel equalization sequence during link training was successful -
1248*25a9f974SMario Six  * the RX device has achieved channel equalization, symbol lock, and interlane
1249*25a9f974SMario Six  * alignment for all lanes currently in use.
1250*25a9f974SMario Six  *
1251*25a9f974SMario Six  * Return: 0 if channel equalization was successful on all lanes in question,
1252*25a9f974SMario Six  *	   -ve if not
1253*25a9f974SMario Six  */
check_channel_equalization(struct udevice * dev,u8 lane_count)1254*25a9f974SMario Six static int check_channel_equalization(struct udevice *dev, u8 lane_count)
1255*25a9f974SMario Six {
1256*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
1257*25a9f974SMario Six 	u8 *lane_status = dp_tx->lane_status_ajd_reqs;
1258*25a9f974SMario Six 
1259*25a9f974SMario Six 	/* Check that all LANEx_CHANNEL_EQ_DONE bits are set. */
1260*25a9f974SMario Six 	switch (lane_count) {
1261*25a9f974SMario Six 	case LANE_COUNT_SET_4:
1262*25a9f974SMario Six 		if (!(lane_status[1] & DPCD_STATUS_LANE_3_CE_DONE_MASK))
1263*25a9f974SMario Six 			goto out_fail;
1264*25a9f974SMario Six 		if (!(lane_status[1] & DPCD_STATUS_LANE_2_CE_DONE_MASK))
1265*25a9f974SMario Six 			goto out_fail;
1266*25a9f974SMario Six 	/* Drop through and check lane 1. */
1267*25a9f974SMario Six 	case LANE_COUNT_SET_2:
1268*25a9f974SMario Six 		if (!(lane_status[0] & DPCD_STATUS_LANE_1_CE_DONE_MASK))
1269*25a9f974SMario Six 			goto out_fail;
1270*25a9f974SMario Six 	/* Drop through and check lane 0. */
1271*25a9f974SMario Six 	case LANE_COUNT_SET_1:
1272*25a9f974SMario Six 		if (!(lane_status[0] & DPCD_STATUS_LANE_0_CE_DONE_MASK))
1273*25a9f974SMario Six 			goto out_fail;
1274*25a9f974SMario Six 	default:
1275*25a9f974SMario Six 		/* All (lane_count) lanes have achieved channel equalization. */
1276*25a9f974SMario Six 		break;
1277*25a9f974SMario Six 	}
1278*25a9f974SMario Six 
1279*25a9f974SMario Six 	/* Check that all LANEx_SYMBOL_LOCKED bits are set. */
1280*25a9f974SMario Six 	switch (lane_count) {
1281*25a9f974SMario Six 	case LANE_COUNT_SET_4:
1282*25a9f974SMario Six 		if (!(lane_status[1] & DPCD_STATUS_LANE_3_SL_DONE_MASK))
1283*25a9f974SMario Six 			goto out_fail;
1284*25a9f974SMario Six 		if (!(lane_status[1] & DPCD_STATUS_LANE_2_SL_DONE_MASK))
1285*25a9f974SMario Six 			goto out_fail;
1286*25a9f974SMario Six 	/* Drop through and check lane 1. */
1287*25a9f974SMario Six 	case LANE_COUNT_SET_2:
1288*25a9f974SMario Six 		if (!(lane_status[0] & DPCD_STATUS_LANE_1_SL_DONE_MASK))
1289*25a9f974SMario Six 			goto out_fail;
1290*25a9f974SMario Six 	/* Drop through and check lane 0. */
1291*25a9f974SMario Six 	case LANE_COUNT_SET_1:
1292*25a9f974SMario Six 		if (!(lane_status[0] & DPCD_STATUS_LANE_0_SL_DONE_MASK))
1293*25a9f974SMario Six 			goto out_fail;
1294*25a9f974SMario Six 	default:
1295*25a9f974SMario Six 		/* All (lane_count) lanes have achieved symbol lock. */
1296*25a9f974SMario Six 		break;
1297*25a9f974SMario Six 	}
1298*25a9f974SMario Six 
1299*25a9f974SMario Six 	/* Check that interlane alignment is done. */
1300*25a9f974SMario Six 	if (!(lane_status[2] & DPCD_LANE_ALIGN_STATUS_UPDATED_IA_DONE_MASK))
1301*25a9f974SMario Six 		goto out_fail;
1302*25a9f974SMario Six 
1303*25a9f974SMario Six 	return 0;
1304*25a9f974SMario Six 
1305*25a9f974SMario Six out_fail:
1306*25a9f974SMario Six 	return -EIO;
1307*25a9f974SMario Six }
1308*25a9f974SMario Six 
1309*25a9f974SMario Six /**
1310*25a9f974SMario Six  * set_training_pattern() - Set training pattern for link training
1311*25a9f974SMario Six  * @dev:     The LogiCore DP TX device in question
1312*25a9f974SMario Six  * @pattern: The training pattern to set
1313*25a9f974SMario Six  *
1314*25a9f974SMario Six  * Set the training pattern to be used during link training for both the
1315*25a9f974SMario Six  * DisplayPort TX core and the RX device.
1316*25a9f974SMario Six  *
1317*25a9f974SMario Six  * Return: 0 if the training pattern could be set successfully, -ve if not
1318*25a9f974SMario Six  */
set_training_pattern(struct udevice * dev,u32 pattern)1319*25a9f974SMario Six static int set_training_pattern(struct udevice *dev, u32 pattern)
1320*25a9f974SMario Six {
1321*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
1322*25a9f974SMario Six 	int status;
1323*25a9f974SMario Six 	u8 aux_data[5];
1324*25a9f974SMario Six 
1325*25a9f974SMario Six 	/* Write to the DisplayPort TX core. */
1326*25a9f974SMario Six 	set_reg(dev, REG_TRAINING_PATTERN_SET, pattern);
1327*25a9f974SMario Six 
1328*25a9f974SMario Six 	aux_data[0] = pattern;
1329*25a9f974SMario Six 
1330*25a9f974SMario Six 	/* Write scrambler disable to the DisplayPort TX core. */
1331*25a9f974SMario Six 	switch (pattern) {
1332*25a9f974SMario Six 	case TRAINING_PATTERN_SET_OFF:
1333*25a9f974SMario Six 		set_reg(dev, REG_SCRAMBLING_DISABLE, 0);
1334*25a9f974SMario Six 		dp_tx->link_config.scrambler_en = 1;
1335*25a9f974SMario Six 		break;
1336*25a9f974SMario Six 	case TRAINING_PATTERN_SET_TP1:
1337*25a9f974SMario Six 	case TRAINING_PATTERN_SET_TP2:
1338*25a9f974SMario Six 	case TRAINING_PATTERN_SET_TP3:
1339*25a9f974SMario Six 		aux_data[0] |= DPCD_TP_SET_SCRAMB_DIS_MASK;
1340*25a9f974SMario Six 		set_reg(dev, REG_SCRAMBLING_DISABLE, 1);
1341*25a9f974SMario Six 		dp_tx->link_config.scrambler_en = 0;
1342*25a9f974SMario Six 		break;
1343*25a9f974SMario Six 	default:
1344*25a9f974SMario Six 		break;
1345*25a9f974SMario Six 	}
1346*25a9f974SMario Six 
1347*25a9f974SMario Six 	/*
1348*25a9f974SMario Six 	 * Make the adjustments to both the DisplayPort TX core and the RX
1349*25a9f974SMario Six 	 * device.
1350*25a9f974SMario Six 	 */
1351*25a9f974SMario Six 	set_vswing_preemp(dev, &aux_data[1]);
1352*25a9f974SMario Six 	/*
1353*25a9f974SMario Six 	 * Write the voltage swing and pre-emphasis levels for each lane to the
1354*25a9f974SMario Six 	 * RX device.
1355*25a9f974SMario Six 	 */
1356*25a9f974SMario Six 	if  (pattern == TRAINING_PATTERN_SET_OFF)
1357*25a9f974SMario Six 		status = aux_write(dev, DPCD_TP_SET, 1, aux_data);
1358*25a9f974SMario Six 	else
1359*25a9f974SMario Six 		status = aux_write(dev, DPCD_TP_SET, 5, aux_data);
1360*25a9f974SMario Six 	if (status)
1361*25a9f974SMario Six 		return -EIO;
1362*25a9f974SMario Six 
1363*25a9f974SMario Six 	return 0;
1364*25a9f974SMario Six }
1365*25a9f974SMario Six 
1366*25a9f974SMario Six /**
1367*25a9f974SMario Six  * training_state_clock_recovery() - Run clock recovery part of link training
1368*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
1369*25a9f974SMario Six  *
1370*25a9f974SMario Six  * Run the clock recovery sequence as part of link training. The
1371*25a9f974SMario Six  * sequence is as follows:
1372*25a9f974SMario Six  *
1373*25a9f974SMario Six  *	0) Start signaling at the minimum voltage swing, pre-emphasis, and
1374*25a9f974SMario Six  *	   post- cursor levels.
1375*25a9f974SMario Six  *	1) Transmit training pattern 1 over the main link with symbol
1376*25a9f974SMario Six  *	   scrambling disabled.
1377*25a9f974SMario Six  *	2) The clock recovery loop. If clock recovery is unsuccessful after
1378*25a9f974SMario Six  *	   MaxIterations loop iterations, return.
1379*25a9f974SMario Six  *	2a) Wait for at least the period of time specified in the RX device's
1380*25a9f974SMario Six  *	    DisplayPort Configuration data (DPCD) register,
1381*25a9f974SMario Six  *	    TRAINING_AUX_RD_INTERVAL.
1382*25a9f974SMario Six  *	2b) Check if all lanes have achieved clock recovery lock. If so,
1383*25a9f974SMario Six  *	    return.
1384*25a9f974SMario Six  *	2c) Check if the same voltage swing level has been used 5 consecutive
1385*25a9f974SMario Six  *	    times or if the maximum level has been reached. If so, return.
1386*25a9f974SMario Six  *	2d) Adjust the voltage swing, pre-emphasis, and post-cursor levels as
1387*25a9f974SMario Six  *	    requested by the RX device.
1388*25a9f974SMario Six  *	2e) Loop back to 2a.
1389*25a9f974SMario Six  *
1390*25a9f974SMario Six  * For a more detailed description of the clock recovery sequence, see section
1391*25a9f974SMario Six  * 3.5.1.2.1 of the DisplayPort 1.2a specification document.
1392*25a9f974SMario Six  *
1393*25a9f974SMario Six  * Return: The next state machine state to advance to
1394*25a9f974SMario Six  */
training_state_clock_recovery(struct udevice * dev)1395*25a9f974SMario Six static unsigned int training_state_clock_recovery(struct udevice *dev)
1396*25a9f974SMario Six {
1397*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
1398*25a9f974SMario Six 	int status;
1399*25a9f974SMario Six 	u32 delay_us;
1400*25a9f974SMario Six 	u8 prev_vs_level = 0;
1401*25a9f974SMario Six 	u8 same_vs_level_count = 0;
1402*25a9f974SMario Six 
1403*25a9f974SMario Six 	/*
1404*25a9f974SMario Six 	 * Obtain the required delay for clock recovery as specified by the
1405*25a9f974SMario Six 	 * RX device.
1406*25a9f974SMario Six 	 */
1407*25a9f974SMario Six 	delay_us = get_training_delay(dev, TS_CLOCK_RECOVERY);
1408*25a9f974SMario Six 
1409*25a9f974SMario Six 	/* Start CRLock. */
1410*25a9f974SMario Six 
1411*25a9f974SMario Six 	/* Transmit training pattern 1. */
1412*25a9f974SMario Six 	/* Disable the scrambler. */
1413*25a9f974SMario Six 	/* Start from minimal voltage swing and pre-emphasis levels. */
1414*25a9f974SMario Six 	dp_tx->link_config.vs_level = 0;
1415*25a9f974SMario Six 	dp_tx->link_config.pe_level = 0;
1416*25a9f974SMario Six 	status = set_training_pattern(dev, TRAINING_PATTERN_SET_TP1);
1417*25a9f974SMario Six 	if (status)
1418*25a9f974SMario Six 		return TS_FAILURE;
1419*25a9f974SMario Six 
1420*25a9f974SMario Six 	while (1) {
1421*25a9f974SMario Six 		/* Wait delay specified in TRAINING_AUX_RD_INTERVAL. */
1422*25a9f974SMario Six 		udelay(delay_us);
1423*25a9f974SMario Six 
1424*25a9f974SMario Six 		/* Get lane and adjustment requests. */
1425*25a9f974SMario Six 		status = get_lane_status_adj_reqs(dev);
1426*25a9f974SMario Six 		if (status)
1427*25a9f974SMario Six 			return TS_FAILURE;
1428*25a9f974SMario Six 
1429*25a9f974SMario Six 		/*
1430*25a9f974SMario Six 		 * Check if all lanes have realized and maintained the frequency
1431*25a9f974SMario Six 		 * lock and get adjustment requests.
1432*25a9f974SMario Six 		 */
1433*25a9f974SMario Six 		status = check_clock_recovery(dev,
1434*25a9f974SMario Six 					      dp_tx->link_config.lane_count);
1435*25a9f974SMario Six 		if (!status)
1436*25a9f974SMario Six 			return TS_CHANNEL_EQUALIZATION;
1437*25a9f974SMario Six 
1438*25a9f974SMario Six 		/*
1439*25a9f974SMario Six 		 * Check if the same voltage swing for each lane has been used 5
1440*25a9f974SMario Six 		 * consecutive times.
1441*25a9f974SMario Six 		 */
1442*25a9f974SMario Six 		if (prev_vs_level == dp_tx->link_config.vs_level) {
1443*25a9f974SMario Six 			same_vs_level_count++;
1444*25a9f974SMario Six 		} else {
1445*25a9f974SMario Six 			same_vs_level_count = 0;
1446*25a9f974SMario Six 			prev_vs_level = dp_tx->link_config.vs_level;
1447*25a9f974SMario Six 		}
1448*25a9f974SMario Six 		if (same_vs_level_count >= 5)
1449*25a9f974SMario Six 			break;
1450*25a9f974SMario Six 
1451*25a9f974SMario Six 		/* Only try maximum voltage swing once. */
1452*25a9f974SMario Six 		if (dp_tx->link_config.vs_level == MAXIMUM_VS_LEVEL)
1453*25a9f974SMario Six 			break;
1454*25a9f974SMario Six 
1455*25a9f974SMario Six 		/* Adjust the drive settings as requested by the RX device. */
1456*25a9f974SMario Six 		status = adj_vswing_preemp(dev);
1457*25a9f974SMario Six 		if (status)
1458*25a9f974SMario Six 			/* The AUX write failed. */
1459*25a9f974SMario Six 			return TS_FAILURE;
1460*25a9f974SMario Six 	}
1461*25a9f974SMario Six 
1462*25a9f974SMario Six 	return TS_ADJUST_LINK_RATE;
1463*25a9f974SMario Six }
1464*25a9f974SMario Six 
1465*25a9f974SMario Six /**
1466*25a9f974SMario Six  * training_state_channel_equalization() - Run channel equalization part of
1467*25a9f974SMario Six  *					   link training
1468*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
1469*25a9f974SMario Six  *
1470*25a9f974SMario Six  * Run the channel equalization sequence as part of link
1471*25a9f974SMario Six  * training. The sequence is as follows:
1472*25a9f974SMario Six  *
1473*25a9f974SMario Six  *	0) Start signaling with the same drive settings used at the end of the
1474*25a9f974SMario Six  *	   clock recovery sequence.
1475*25a9f974SMario Six  *	1) Transmit training pattern 2 (or 3) over the main link with symbol
1476*25a9f974SMario Six  *	   scrambling disabled.
1477*25a9f974SMario Six  *	2) The channel equalization loop. If channel equalization is
1478*25a9f974SMario Six  *	   unsuccessful after 5 loop iterations, return.
1479*25a9f974SMario Six  *	2a) Wait for at least the period of time specified in the RX device's
1480*25a9f974SMario Six  *	    DisplayPort Configuration data (DPCD) register,
1481*25a9f974SMario Six  *	    TRAINING_AUX_RD_INTERVAL.
1482*25a9f974SMario Six  *	2b) Check if all lanes have achieved channel equalization, symbol lock,
1483*25a9f974SMario Six  *	    and interlane alignment. If so, return.
1484*25a9f974SMario Six  *	2c) Check if the same voltage swing level has been used 5 consecutive
1485*25a9f974SMario Six  *	    times or if the maximum level has been reached. If so, return.
1486*25a9f974SMario Six  *	2d) Adjust the voltage swing, pre-emphasis, and post-cursor levels as
1487*25a9f974SMario Six  *	    requested by the RX device.
1488*25a9f974SMario Six  *	2e) Loop back to 2a.
1489*25a9f974SMario Six  *
1490*25a9f974SMario Six  * For a more detailed description of the channel equalization sequence, see
1491*25a9f974SMario Six  * section 3.5.1.2.2 of the DisplayPort 1.2a specification document.
1492*25a9f974SMario Six  *
1493*25a9f974SMario Six  * Return: The next state machine state to advance to
1494*25a9f974SMario Six  */
training_state_channel_equalization(struct udevice * dev)1495*25a9f974SMario Six static int training_state_channel_equalization(struct udevice *dev)
1496*25a9f974SMario Six {
1497*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
1498*25a9f974SMario Six 	int status;
1499*25a9f974SMario Six 	u32 delay_us;
1500*25a9f974SMario Six 	u32 iteration_count = 0;
1501*25a9f974SMario Six 
1502*25a9f974SMario Six 	/*
1503*25a9f974SMario Six 	 * Obtain the required delay for channel equalization as specified by
1504*25a9f974SMario Six 	 * the RX device.
1505*25a9f974SMario Six 	 */
1506*25a9f974SMario Six 	delay_us = get_training_delay(dev, TS_CHANNEL_EQUALIZATION);
1507*25a9f974SMario Six 
1508*25a9f974SMario Six 	/* Start channel equalization. */
1509*25a9f974SMario Six 
1510*25a9f974SMario Six 	/* Write the current drive settings. */
1511*25a9f974SMario Six 	/* Transmit training pattern 2/3. */
1512*25a9f974SMario Six 	if (dp_tx->dpcd_rx_caps[DPCD_MAX_LANE_COUNT] & DPCD_TPS3_SUPPORT_MASK)
1513*25a9f974SMario Six 		status = set_training_pattern(dev, TRAINING_PATTERN_SET_TP3);
1514*25a9f974SMario Six 	else
1515*25a9f974SMario Six 		status = set_training_pattern(dev, TRAINING_PATTERN_SET_TP2);
1516*25a9f974SMario Six 
1517*25a9f974SMario Six 	if (status)
1518*25a9f974SMario Six 		return TS_FAILURE;
1519*25a9f974SMario Six 
1520*25a9f974SMario Six 	while (iteration_count < 5) {
1521*25a9f974SMario Six 		/* Wait delay specified in TRAINING_AUX_RD_INTERVAL. */
1522*25a9f974SMario Six 		udelay(delay_us);
1523*25a9f974SMario Six 
1524*25a9f974SMario Six 		/* Get lane and adjustment requests. */
1525*25a9f974SMario Six 		status = get_lane_status_adj_reqs(dev);
1526*25a9f974SMario Six 		if (status)
1527*25a9f974SMario Six 			/* The AUX read failed. */
1528*25a9f974SMario Six 			return TS_FAILURE;
1529*25a9f974SMario Six 
1530*25a9f974SMario Six 		/* Check that all lanes still have their clocks locked. */
1531*25a9f974SMario Six 		status = check_clock_recovery(dev,
1532*25a9f974SMario Six 					      dp_tx->link_config.lane_count);
1533*25a9f974SMario Six 		if (status)
1534*25a9f974SMario Six 			break;
1535*25a9f974SMario Six 
1536*25a9f974SMario Six 		/*
1537*25a9f974SMario Six 		 * Check if all lanes have accomplished channel equalization,
1538*25a9f974SMario Six 		 * symbol lock, and interlane alignment.
1539*25a9f974SMario Six 		 */
1540*25a9f974SMario Six 		status =
1541*25a9f974SMario Six 		    check_channel_equalization(dev,
1542*25a9f974SMario Six 					       dp_tx->link_config.lane_count);
1543*25a9f974SMario Six 		if (!status)
1544*25a9f974SMario Six 			return TS_SUCCESS;
1545*25a9f974SMario Six 
1546*25a9f974SMario Six 		/* Adjust the drive settings as requested by the RX device. */
1547*25a9f974SMario Six 		status = adj_vswing_preemp(dev);
1548*25a9f974SMario Six 		if (status)
1549*25a9f974SMario Six 			/* The AUX write failed. */
1550*25a9f974SMario Six 			return TS_FAILURE;
1551*25a9f974SMario Six 
1552*25a9f974SMario Six 		iteration_count++;
1553*25a9f974SMario Six 	}
1554*25a9f974SMario Six 
1555*25a9f974SMario Six 	/*
1556*25a9f974SMario Six 	 * Tried 5 times with no success. Try a reduced bitrate first, then
1557*25a9f974SMario Six 	 * reduce the number of lanes.
1558*25a9f974SMario Six 	 */
1559*25a9f974SMario Six 	return TS_ADJUST_LINK_RATE;
1560*25a9f974SMario Six }
1561*25a9f974SMario Six 
1562*25a9f974SMario Six /**
1563*25a9f974SMario Six  * training_state_adjust_link_rate() - Downshift data rate and/or lane count
1564*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
1565*25a9f974SMario Six  *
1566*25a9f974SMario Six  * This function is reached if either the clock recovery or the channel
1567*25a9f974SMario Six  * equalization process failed during training. As a result, the data rate will
1568*25a9f974SMario Six  * be downshifted, and training will be re-attempted (starting with clock
1569*25a9f974SMario Six  * recovery) at the reduced data rate. If the data rate is already at 1.62
1570*25a9f974SMario Six  * Gbps, a downshift in lane count will be attempted.
1571*25a9f974SMario Six  *
1572*25a9f974SMario Six  * Return: The next state machine state to advance to
1573*25a9f974SMario Six  */
training_state_adjust_link_rate(struct udevice * dev)1574*25a9f974SMario Six static int training_state_adjust_link_rate(struct udevice *dev)
1575*25a9f974SMario Six {
1576*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
1577*25a9f974SMario Six 	int status;
1578*25a9f974SMario Six 
1579*25a9f974SMario Six 	switch (dp_tx->link_config.link_rate) {
1580*25a9f974SMario Six 	case LINK_BW_SET_540GBPS:
1581*25a9f974SMario Six 		status = set_link_rate(dev, LINK_BW_SET_270GBPS);
1582*25a9f974SMario Six 		if (status) {
1583*25a9f974SMario Six 			status = TS_FAILURE;
1584*25a9f974SMario Six 			break;
1585*25a9f974SMario Six 		}
1586*25a9f974SMario Six 		status = TS_CLOCK_RECOVERY;
1587*25a9f974SMario Six 		break;
1588*25a9f974SMario Six 	case LINK_BW_SET_270GBPS:
1589*25a9f974SMario Six 		status = set_link_rate(dev, LINK_BW_SET_162GBPS);
1590*25a9f974SMario Six 		if (status) {
1591*25a9f974SMario Six 			status = TS_FAILURE;
1592*25a9f974SMario Six 			break;
1593*25a9f974SMario Six 		}
1594*25a9f974SMario Six 		status = TS_CLOCK_RECOVERY;
1595*25a9f974SMario Six 		break;
1596*25a9f974SMario Six 	default:
1597*25a9f974SMario Six 		/*
1598*25a9f974SMario Six 		 * Already at the lowest link rate. Try reducing the lane
1599*25a9f974SMario Six 		 * count next.
1600*25a9f974SMario Six 		 */
1601*25a9f974SMario Six 		status = TS_ADJUST_LANE_COUNT;
1602*25a9f974SMario Six 		break;
1603*25a9f974SMario Six 	}
1604*25a9f974SMario Six 
1605*25a9f974SMario Six 	return status;
1606*25a9f974SMario Six }
1607*25a9f974SMario Six 
1608*25a9f974SMario Six /**
1609*25a9f974SMario Six  * trainig_state_adjust_lane_count - Downshift lane count
1610*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
1611*25a9f974SMario Six  *
1612*25a9f974SMario Six  * This function is reached if either the clock recovery or the channel
1613*25a9f974SMario Six  * equalization process failed during training, and a minimal data rate of 1.62
1614*25a9f974SMario Six  * Gbps was being used. As a result, the number of lanes in use will be
1615*25a9f974SMario Six  * reduced, and training will be re-attempted (starting with clock recovery) at
1616*25a9f974SMario Six  * this lower lane count.
1617*25a9f974SMario Six  *
1618*25a9f974SMario Six  * Return: The next state machine state to advance to
1619*25a9f974SMario Six  */
trainig_state_adjust_lane_count(struct udevice * dev)1620*25a9f974SMario Six static int trainig_state_adjust_lane_count(struct udevice *dev)
1621*25a9f974SMario Six {
1622*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
1623*25a9f974SMario Six 	int status;
1624*25a9f974SMario Six 
1625*25a9f974SMario Six 	switch (dp_tx->link_config.lane_count) {
1626*25a9f974SMario Six 	case LANE_COUNT_SET_4:
1627*25a9f974SMario Six 		status = set_lane_count(dev, LANE_COUNT_SET_2);
1628*25a9f974SMario Six 		if (status) {
1629*25a9f974SMario Six 			status = TS_FAILURE;
1630*25a9f974SMario Six 			break;
1631*25a9f974SMario Six 		}
1632*25a9f974SMario Six 
1633*25a9f974SMario Six 		status = set_link_rate(dev, dp_tx->link_config.max_link_rate);
1634*25a9f974SMario Six 		if (status) {
1635*25a9f974SMario Six 			status = TS_FAILURE;
1636*25a9f974SMario Six 			break;
1637*25a9f974SMario Six 		}
1638*25a9f974SMario Six 		status = TS_CLOCK_RECOVERY;
1639*25a9f974SMario Six 		break;
1640*25a9f974SMario Six 	case LANE_COUNT_SET_2:
1641*25a9f974SMario Six 		status = set_lane_count(dev, LANE_COUNT_SET_1);
1642*25a9f974SMario Six 		if (status) {
1643*25a9f974SMario Six 			status = TS_FAILURE;
1644*25a9f974SMario Six 			break;
1645*25a9f974SMario Six 		}
1646*25a9f974SMario Six 
1647*25a9f974SMario Six 		status = set_link_rate(dev, dp_tx->link_config.max_link_rate);
1648*25a9f974SMario Six 		if (status) {
1649*25a9f974SMario Six 			status = TS_FAILURE;
1650*25a9f974SMario Six 			break;
1651*25a9f974SMario Six 		}
1652*25a9f974SMario Six 		status = TS_CLOCK_RECOVERY;
1653*25a9f974SMario Six 		break;
1654*25a9f974SMario Six 	default:
1655*25a9f974SMario Six 		/*
1656*25a9f974SMario Six 		 * Already at the lowest lane count. Training has failed at the
1657*25a9f974SMario Six 		 * lowest lane count and link rate.
1658*25a9f974SMario Six 		 */
1659*25a9f974SMario Six 		status = TS_FAILURE;
1660*25a9f974SMario Six 		break;
1661*25a9f974SMario Six 	}
1662*25a9f974SMario Six 
1663*25a9f974SMario Six 	return status;
1664*25a9f974SMario Six }
1665*25a9f974SMario Six 
1666*25a9f974SMario Six /**
1667*25a9f974SMario Six  * check_link_status() - Check status of link
1668*25a9f974SMario Six  * @dev:        The LogiCore DP TX device in question
1669*25a9f974SMario Six  * @lane_count: The lane count to use for the check
1670*25a9f974SMario Six  *
1671*25a9f974SMario Six  * Check if the receiver's DisplayPort Configuration data (DPCD) indicates the
1672*25a9f974SMario Six  * receiver has achieved and maintained clock recovery, channel equalization,
1673*25a9f974SMario Six  * symbol lock, and interlane alignment for all lanes currently in use.
1674*25a9f974SMario Six  *
1675*25a9f974SMario Six  * Return: 0 if the link status is OK, -ve if a error occurred during checking
1676*25a9f974SMario Six  */
check_link_status(struct udevice * dev,u8 lane_count)1677*25a9f974SMario Six static int check_link_status(struct udevice *dev, u8 lane_count)
1678*25a9f974SMario Six {
1679*25a9f974SMario Six 	u8 retry_count = 0;
1680*25a9f974SMario Six 
1681*25a9f974SMario Six 	if (!is_connected(dev))
1682*25a9f974SMario Six 		return -ENODEV;
1683*25a9f974SMario Six 
1684*25a9f974SMario Six 	/* Retrieve AUX info. */
1685*25a9f974SMario Six 	do {
1686*25a9f974SMario Six 		int status;
1687*25a9f974SMario Six 
1688*25a9f974SMario Six 		/* Get lane and adjustment requests. */
1689*25a9f974SMario Six 		status = get_lane_status_adj_reqs(dev);
1690*25a9f974SMario Six 		if (status)
1691*25a9f974SMario Six 			return -EIO;
1692*25a9f974SMario Six 
1693*25a9f974SMario Six 		/* Check if the link needs training. */
1694*25a9f974SMario Six 		if ((check_clock_recovery(dev, lane_count) == 0) &&
1695*25a9f974SMario Six 		    (check_channel_equalization(dev, lane_count) == 0))
1696*25a9f974SMario Six 			return 0;
1697*25a9f974SMario Six 
1698*25a9f974SMario Six 		retry_count++;
1699*25a9f974SMario Six 	} while (retry_count < 5); /* Retry up to 5 times. */
1700*25a9f974SMario Six 
1701*25a9f974SMario Six 	return -EIO;
1702*25a9f974SMario Six }
1703*25a9f974SMario Six 
1704*25a9f974SMario Six /**
1705*25a9f974SMario Six  * run_training() - Run link training
1706*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
1707*25a9f974SMario Six  *
1708*25a9f974SMario Six  * Run the link training process. It is implemented as a state machine, with
1709*25a9f974SMario Six  * each state returning the next state. First, the clock recovery sequence will
1710*25a9f974SMario Six  * be run; if successful, the channel equalization sequence will run. If either
1711*25a9f974SMario Six  * the clock recovery or channel equalization sequence failed, the link rate or
1712*25a9f974SMario Six  * the number of lanes used will be reduced and training will be re-attempted.
1713*25a9f974SMario Six  * If training fails at the minimal data rate, 1.62 Gbps with a single lane,
1714*25a9f974SMario Six  * training will no longer re-attempt and fail.
1715*25a9f974SMario Six  *
1716*25a9f974SMario Six  * ### Here be dragons ###
1717*25a9f974SMario Six  * There are undocumented timeout constraints in the link training process. In
1718*25a9f974SMario Six  * DP v1.2a spec, Chapter 3.5.1.2.2 a 10ms limit for the complete training
1719*25a9f974SMario Six  * process is mentioned. Which individual timeouts are derived and implemented
1720*25a9f974SMario Six  * by sink manufacturers is unknown. So each step should be as short as
1721*25a9f974SMario Six  * possible and link training should start as soon as possible after HPD.
1722*25a9f974SMario Six  *
1723*25a9f974SMario Six  * Return: 0 if the training sequence ran successfully, -ve if a error occurred
1724*25a9f974SMario Six  *	   or the training failed
1725*25a9f974SMario Six  */
run_training(struct udevice * dev)1726*25a9f974SMario Six static int run_training(struct udevice *dev)
1727*25a9f974SMario Six {
1728*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
1729*25a9f974SMario Six 	int status;
1730*25a9f974SMario Six 	int training_state = TS_CLOCK_RECOVERY;
1731*25a9f974SMario Six 
1732*25a9f974SMario Six 	while (1) {
1733*25a9f974SMario Six 		switch (training_state) {
1734*25a9f974SMario Six 		case TS_CLOCK_RECOVERY:
1735*25a9f974SMario Six 			training_state =
1736*25a9f974SMario Six 				training_state_clock_recovery(dev);
1737*25a9f974SMario Six 			break;
1738*25a9f974SMario Six 		case TS_CHANNEL_EQUALIZATION:
1739*25a9f974SMario Six 			training_state =
1740*25a9f974SMario Six 				training_state_channel_equalization(dev);
1741*25a9f974SMario Six 			break;
1742*25a9f974SMario Six 		case TS_ADJUST_LINK_RATE:
1743*25a9f974SMario Six 			training_state =
1744*25a9f974SMario Six 				training_state_adjust_link_rate(dev);
1745*25a9f974SMario Six 			break;
1746*25a9f974SMario Six 		case TS_ADJUST_LANE_COUNT:
1747*25a9f974SMario Six 			training_state =
1748*25a9f974SMario Six 				trainig_state_adjust_lane_count(dev);
1749*25a9f974SMario Six 			break;
1750*25a9f974SMario Six 		default:
1751*25a9f974SMario Six 			break;
1752*25a9f974SMario Six 		}
1753*25a9f974SMario Six 
1754*25a9f974SMario Six 		if (training_state == TS_SUCCESS)
1755*25a9f974SMario Six 			break;
1756*25a9f974SMario Six 		else if (training_state == TS_FAILURE)
1757*25a9f974SMario Six 			return -EIO;
1758*25a9f974SMario Six 
1759*25a9f974SMario Six 		if (training_state == TS_ADJUST_LINK_RATE ||
1760*25a9f974SMario Six 		    training_state == TS_ADJUST_LANE_COUNT) {
1761*25a9f974SMario Six 			if (!dp_tx->train_adaptive)
1762*25a9f974SMario Six 				return -EIO;
1763*25a9f974SMario Six 
1764*25a9f974SMario Six 			status = set_training_pattern(dev,
1765*25a9f974SMario Six 						      TRAINING_PATTERN_SET_OFF);
1766*25a9f974SMario Six 			if (status)
1767*25a9f974SMario Six 				return -EIO;
1768*25a9f974SMario Six 		}
1769*25a9f974SMario Six 	}
1770*25a9f974SMario Six 
1771*25a9f974SMario Six 	/* Final status check. */
1772*25a9f974SMario Six 	status = check_link_status(dev, dp_tx->link_config.lane_count);
1773*25a9f974SMario Six 	if (status)
1774*25a9f974SMario Six 		return -EIO;
1775*25a9f974SMario Six 
1776*25a9f974SMario Six 	return 0;
1777*25a9f974SMario Six }
1778*25a9f974SMario Six 
1779*25a9f974SMario Six /* Link policy maker */
1780*25a9f974SMario Six 
1781*25a9f974SMario Six /**
1782*25a9f974SMario Six  * cfg_main_link_max() - Determine best common capabilities
1783*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
1784*25a9f974SMario Six  *
1785*25a9f974SMario Six  * Determine the common capabilities between the DisplayPort TX core and the RX
1786*25a9f974SMario Six  * device.
1787*25a9f974SMario Six  *
1788*25a9f974SMario Six  * Return: 0 if the determination succeeded, -ve on error
1789*25a9f974SMario Six  */
cfg_main_link_max(struct udevice * dev)1790*25a9f974SMario Six static int cfg_main_link_max(struct udevice *dev)
1791*25a9f974SMario Six {
1792*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
1793*25a9f974SMario Six 	int status;
1794*25a9f974SMario Six 
1795*25a9f974SMario Six 	if (!is_connected(dev))
1796*25a9f974SMario Six 		return -ENODEV;
1797*25a9f974SMario Six 
1798*25a9f974SMario Six 	/*
1799*25a9f974SMario Six 	 * Configure the main link to the maximum common link rate between the
1800*25a9f974SMario Six 	 * DisplayPort TX core and the RX device.
1801*25a9f974SMario Six 	 */
1802*25a9f974SMario Six 	status = set_link_rate(dev, dp_tx->link_config.max_link_rate);
1803*25a9f974SMario Six 	if (status)
1804*25a9f974SMario Six 		return status;
1805*25a9f974SMario Six 
1806*25a9f974SMario Six 	/*
1807*25a9f974SMario Six 	 * Configure the main link to the maximum common lane count between the
1808*25a9f974SMario Six 	 * DisplayPort TX core and the RX device.
1809*25a9f974SMario Six 	 */
1810*25a9f974SMario Six 	status = set_lane_count(dev, dp_tx->link_config.max_lane_count);
1811*25a9f974SMario Six 	if (status)
1812*25a9f974SMario Six 		return status;
1813*25a9f974SMario Six 
1814*25a9f974SMario Six 	return 0;
1815*25a9f974SMario Six }
1816*25a9f974SMario Six 
1817*25a9f974SMario Six /**
1818*25a9f974SMario Six  * establish_link() - Establish a link
1819*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
1820*25a9f974SMario Six  *
1821*25a9f974SMario Six  * Check if the link needs training and run the training sequence if training
1822*25a9f974SMario Six  * is required.
1823*25a9f974SMario Six  *
1824*25a9f974SMario Six  * Return: 0 if the link was established successfully, -ve on error
1825*25a9f974SMario Six  */
establish_link(struct udevice * dev)1826*25a9f974SMario Six static int establish_link(struct udevice *dev)
1827*25a9f974SMario Six {
1828*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
1829*25a9f974SMario Six 	int status;
1830*25a9f974SMario Six 	int status2;
1831*25a9f974SMario Six 	u32 mask;
1832*25a9f974SMario Six 
1833*25a9f974SMario Six 	reset_dp_phy(dev, PHY_CONFIG_PHY_RESET_MASK);
1834*25a9f974SMario Six 
1835*25a9f974SMario Six 	/* Disable main link during training. */
1836*25a9f974SMario Six 	disable_main_link(dev);
1837*25a9f974SMario Six 
1838*25a9f974SMario Six 	/* Wait for the PHY to be ready. */
1839*25a9f974SMario Six 	mask = phy_status_lanes_ready_mask(dp_tx->max_lane_count);
1840*25a9f974SMario Six 	status = wait_phy_ready(dev, mask);
1841*25a9f974SMario Six 	if (status)
1842*25a9f974SMario Six 		return -EIO;
1843*25a9f974SMario Six 
1844*25a9f974SMario Six 	/* Train main link. */
1845*25a9f974SMario Six 	status = run_training(dev);
1846*25a9f974SMario Six 
1847*25a9f974SMario Six 	/* Turn off the training pattern and enable scrambler. */
1848*25a9f974SMario Six 	status2 = set_training_pattern(dev, TRAINING_PATTERN_SET_OFF);
1849*25a9f974SMario Six 	if (status || status2)
1850*25a9f974SMario Six 		return -EIO;
1851*25a9f974SMario Six 
1852*25a9f974SMario Six 	return 0;
1853*25a9f974SMario Six }
1854*25a9f974SMario Six 
1855*25a9f974SMario Six /*
1856*25a9f974SMario Six  * Stream policy maker
1857*25a9f974SMario Six  */
1858*25a9f974SMario Six 
1859*25a9f974SMario Six /**
1860*25a9f974SMario Six  * cfg_msa_recalculate() - Calculate MSA parameters
1861*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
1862*25a9f974SMario Six  *
1863*25a9f974SMario Six  * Calculate the following Main Stream Attributes (MSA):
1864*25a9f974SMario Six  * - Transfer unit size
1865*25a9f974SMario Six  * - User pixel width
1866*25a9f974SMario Six  * - Horizontal total clock
1867*25a9f974SMario Six  * - Vertical total clock
1868*25a9f974SMario Six  * - misc_0
1869*25a9f974SMario Six  * - misc_1
1870*25a9f974SMario Six  * - Data per lane
1871*25a9f974SMario Six  * - Average number of bytes per transfer unit
1872*25a9f974SMario Six  * - Number of initial wait cycles
1873*25a9f974SMario Six  *
1874*25a9f974SMario Six  * These values are derived from:
1875*25a9f974SMario Six  * - Bits per color
1876*25a9f974SMario Six  * - Horizontal resolution
1877*25a9f974SMario Six  * - Vertical resolution
1878*25a9f974SMario Six  * - Horizontal blank start
1879*25a9f974SMario Six  * - Vertical blank start
1880*25a9f974SMario Six  * - Pixel clock (in KHz)
1881*25a9f974SMario Six  * - Horizontal sync polarity
1882*25a9f974SMario Six  * - Vertical sync polarity
1883*25a9f974SMario Six  * - Horizontal sync pulse width
1884*25a9f974SMario Six  * - Vertical sync pulse width
1885*25a9f974SMario Six  */
cfg_msa_recalculate(struct udevice * dev)1886*25a9f974SMario Six static void cfg_msa_recalculate(struct udevice *dev)
1887*25a9f974SMario Six {
1888*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
1889*25a9f974SMario Six 	u32 video_bw;
1890*25a9f974SMario Six 	u32 link_bw;
1891*25a9f974SMario Six 	u32 words_per_line;
1892*25a9f974SMario Six 	u8 bits_per_pixel;
1893*25a9f974SMario Six 	struct main_stream_attributes *msa_config;
1894*25a9f974SMario Six 	struct link_config *link_config;
1895*25a9f974SMario Six 
1896*25a9f974SMario Six 	msa_config = &dp_tx->main_stream_attributes;
1897*25a9f974SMario Six 	link_config = &dp_tx->link_config;
1898*25a9f974SMario Six 
1899*25a9f974SMario Six 	/*
1900*25a9f974SMario Six 	 * Set the user pixel width to handle clocks that exceed the
1901*25a9f974SMario Six 	 * capabilities of the DisplayPort TX core.
1902*25a9f974SMario Six 	 */
1903*25a9f974SMario Six 	if (msa_config->override_user_pixel_width == 0) {
1904*25a9f974SMario Six 		if (msa_config->pixel_clock_hz > 300000000 &&
1905*25a9f974SMario Six 		    link_config->lane_count == LANE_COUNT_SET_4) {
1906*25a9f974SMario Six 			msa_config->user_pixel_width = 4;
1907*25a9f974SMario Six 		} /*
1908*25a9f974SMario Six 		   * Xilinx driver used 75 MHz as a limit here, 150 MHZ should
1909*25a9f974SMario Six 		   * be more sane
1910*25a9f974SMario Six 		   */
1911*25a9f974SMario Six 		else if ((msa_config->pixel_clock_hz > 150000000) &&
1912*25a9f974SMario Six 			 (link_config->lane_count != LANE_COUNT_SET_1)) {
1913*25a9f974SMario Six 			msa_config->user_pixel_width = 2;
1914*25a9f974SMario Six 		} else {
1915*25a9f974SMario Six 			msa_config->user_pixel_width = 1;
1916*25a9f974SMario Six 		}
1917*25a9f974SMario Six 	}
1918*25a9f974SMario Six 
1919*25a9f974SMario Six 	/* Compute the rest of the MSA values. */
1920*25a9f974SMario Six 	msa_config->n_vid = 27 * 1000 * link_config->link_rate;
1921*25a9f974SMario Six 
1922*25a9f974SMario Six 	/* Miscellaneous attributes. */
1923*25a9f974SMario Six 	if (msa_config->bits_per_color == 6)
1924*25a9f974SMario Six 		msa_config->misc_0 = MAIN_STREAMX_MISC0_BDC_6BPC;
1925*25a9f974SMario Six 	else if (msa_config->bits_per_color == 8)
1926*25a9f974SMario Six 		msa_config->misc_0 = MAIN_STREAMX_MISC0_BDC_8BPC;
1927*25a9f974SMario Six 	else if (msa_config->bits_per_color == 10)
1928*25a9f974SMario Six 		msa_config->misc_0 = MAIN_STREAMX_MISC0_BDC_10BPC;
1929*25a9f974SMario Six 	else if (msa_config->bits_per_color == 12)
1930*25a9f974SMario Six 		msa_config->misc_0 = MAIN_STREAMX_MISC0_BDC_12BPC;
1931*25a9f974SMario Six 	else if (msa_config->bits_per_color == 16)
1932*25a9f974SMario Six 		msa_config->misc_0 = MAIN_STREAMX_MISC0_BDC_16BPC;
1933*25a9f974SMario Six 
1934*25a9f974SMario Six 	msa_config->misc_0 = (msa_config->misc_0 <<
1935*25a9f974SMario Six 			      MAIN_STREAMX_MISC0_BDC_SHIFT) |
1936*25a9f974SMario Six 			     (msa_config->y_cb_cr_colorimetry <<
1937*25a9f974SMario Six 			      MAIN_STREAMX_MISC0_YCBCR_COLORIMETRY_SHIFT) |
1938*25a9f974SMario Six 			     (msa_config->dynamic_range <<
1939*25a9f974SMario Six 			      MAIN_STREAMX_MISC0_DYNAMIC_RANGE_SHIFT) |
1940*25a9f974SMario Six 			     (msa_config->component_format <<
1941*25a9f974SMario Six 			      MAIN_STREAMX_MISC0_COMPONENT_FORMAT_SHIFT) |
1942*25a9f974SMario Six 			     (msa_config->synchronous_clock_mode);
1943*25a9f974SMario Six 
1944*25a9f974SMario Six 	msa_config->misc_1 = 0;
1945*25a9f974SMario Six 
1946*25a9f974SMario Six 	/*
1947*25a9f974SMario Six 	 * Determine the number of bits per pixel for the specified color
1948*25a9f974SMario Six 	 * component format.
1949*25a9f974SMario Six 	 */
1950*25a9f974SMario Six 	if (msa_config->component_format ==
1951*25a9f974SMario Six 	    MAIN_STREAMX_MISC0_COMPONENT_FORMAT_YCBCR422)
1952*25a9f974SMario Six 		/* YCbCr422 color component format. */
1953*25a9f974SMario Six 		bits_per_pixel = msa_config->bits_per_color * 2;
1954*25a9f974SMario Six 	else
1955*25a9f974SMario Six 		/* RGB or YCbCr 4:4:4 color component format. */
1956*25a9f974SMario Six 		bits_per_pixel = msa_config->bits_per_color * 3;
1957*25a9f974SMario Six 
1958*25a9f974SMario Six 	/* Calculate the data per lane. */
1959*25a9f974SMario Six 	words_per_line = (msa_config->h_active * bits_per_pixel);
1960*25a9f974SMario Six 	if (words_per_line % 16)
1961*25a9f974SMario Six 		words_per_line += 16;
1962*25a9f974SMario Six 	words_per_line /= 16;
1963*25a9f974SMario Six 
1964*25a9f974SMario Six 	msa_config->data_per_lane = words_per_line - link_config->lane_count;
1965*25a9f974SMario Six 	if (words_per_line % link_config->lane_count)
1966*25a9f974SMario Six 		msa_config->data_per_lane += (words_per_line %
1967*25a9f974SMario Six 					      link_config->lane_count);
1968*25a9f974SMario Six 
1969*25a9f974SMario Six 	/*
1970*25a9f974SMario Six 	 * Allocate a fixed size for single-stream transport (SST)
1971*25a9f974SMario Six 	 * operation.
1972*25a9f974SMario Six 	 */
1973*25a9f974SMario Six 	msa_config->transfer_unit_size = 64;
1974*25a9f974SMario Six 
1975*25a9f974SMario Six 	/*
1976*25a9f974SMario Six 	 * Calculate the average number of bytes per transfer unit.
1977*25a9f974SMario Six 	 * Note: Both the integer and the fractional part is stored in
1978*25a9f974SMario Six 	 * avg_bytes_per_tu.
1979*25a9f974SMario Six 	 */
1980*25a9f974SMario Six 	video_bw = ((msa_config->pixel_clock_hz / 1000) * bits_per_pixel) / 8;
1981*25a9f974SMario Six 	link_bw = (link_config->lane_count * link_config->link_rate * 27);
1982*25a9f974SMario Six 	msa_config->avg_bytes_per_tu = (video_bw *
1983*25a9f974SMario Six 					msa_config->transfer_unit_size) /
1984*25a9f974SMario Six 					link_bw;
1985*25a9f974SMario Six 
1986*25a9f974SMario Six 	/*
1987*25a9f974SMario Six 	 * The number of initial wait cycles at the start of a new line
1988*25a9f974SMario Six 	 * by the framing logic. This allows enough data to be buffered
1989*25a9f974SMario Six 	 * in the input FIFO before video is sent.
1990*25a9f974SMario Six 	 */
1991*25a9f974SMario Six 	if ((msa_config->avg_bytes_per_tu / 1000) <= 4)
1992*25a9f974SMario Six 		msa_config->init_wait = 64;
1993*25a9f974SMario Six 	else
1994*25a9f974SMario Six 		msa_config->init_wait = msa_config->transfer_unit_size -
1995*25a9f974SMario Six 					(msa_config->avg_bytes_per_tu / 1000);
1996*25a9f974SMario Six }
1997*25a9f974SMario Six 
1998*25a9f974SMario Six /**
1999*25a9f974SMario Six  * set_line_reset() - Enable/Disable end-of-line-reset
2000*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
2001*25a9f974SMario Six  *
2002*25a9f974SMario Six  * Disable/enable the end-of-line-reset to the internal video pipe in case of
2003*25a9f974SMario Six  * reduced blanking as required.
2004*25a9f974SMario Six  */
set_line_reset(struct udevice * dev)2005*25a9f974SMario Six static void set_line_reset(struct udevice *dev)
2006*25a9f974SMario Six {
2007*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
2008*25a9f974SMario Six 	u32 reg_val;
2009*25a9f974SMario Six 	u16 h_blank;
2010*25a9f974SMario Six 	u16 h_reduced_blank;
2011*25a9f974SMario Six 	struct main_stream_attributes *msa_config =
2012*25a9f974SMario Six 		&dp_tx->main_stream_attributes;
2013*25a9f974SMario Six 
2014*25a9f974SMario Six 	h_blank = msa_config->h_total - msa_config->h_active;
2015*25a9f974SMario Six 	/* Reduced blanking starts at ceil(0.2 * HTotal). */
2016*25a9f974SMario Six 	h_reduced_blank = 2 * msa_config->h_total;
2017*25a9f974SMario Six 	if (h_reduced_blank % 10)
2018*25a9f974SMario Six 		h_reduced_blank += 10;
2019*25a9f974SMario Six 	h_reduced_blank /= 10;
2020*25a9f974SMario Six 
2021*25a9f974SMario Six 	/* CVT spec. states h_blank is either 80 or 160 for reduced blanking. */
2022*25a9f974SMario Six 	reg_val = get_reg(dev, REG_LINE_RESET_DISABLE);
2023*25a9f974SMario Six 	if (h_blank < h_reduced_blank &&
2024*25a9f974SMario Six 	    (h_blank == 80 || h_blank == 160)) {
2025*25a9f974SMario Six 		reg_val |= LINE_RESET_DISABLE_MASK;
2026*25a9f974SMario Six 	} else {
2027*25a9f974SMario Six 		reg_val &= ~LINE_RESET_DISABLE_MASK;
2028*25a9f974SMario Six 	}
2029*25a9f974SMario Six 	set_reg(dev, REG_LINE_RESET_DISABLE, reg_val);
2030*25a9f974SMario Six }
2031*25a9f974SMario Six 
2032*25a9f974SMario Six /**
2033*25a9f974SMario Six  * clear_msa_values() - Clear MSA values
2034*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
2035*25a9f974SMario Six  *
2036*25a9f974SMario Six  * Clear the main stream attributes registers of the DisplayPort TX core.
2037*25a9f974SMario Six  */
clear_msa_values(struct udevice * dev)2038*25a9f974SMario Six static void clear_msa_values(struct udevice *dev)
2039*25a9f974SMario Six {
2040*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_HTOTAL, 0);
2041*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_VTOTAL, 0);
2042*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_POLARITY, 0);
2043*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_HSWIDTH, 0);
2044*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_VSWIDTH, 0);
2045*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_HRES, 0);
2046*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_VRES, 0);
2047*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_HSTART, 0);
2048*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_VSTART, 0);
2049*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_MISC0, 0);
2050*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_MISC1, 0);
2051*25a9f974SMario Six 	set_reg(dev, REG_USER_PIXEL_WIDTH, 0);
2052*25a9f974SMario Six 	set_reg(dev, REG_USER_DATA_COUNT_PER_LANE, 0);
2053*25a9f974SMario Six 	set_reg(dev, REG_M_VID, 0);
2054*25a9f974SMario Six 	set_reg(dev, REG_N_VID, 0);
2055*25a9f974SMario Six 
2056*25a9f974SMario Six 	set_reg(dev, REG_STREAM1, 0);
2057*25a9f974SMario Six 	set_reg(dev, REG_TU_SIZE, 0);
2058*25a9f974SMario Six 	set_reg(dev, REG_MIN_BYTES_PER_TU, 0);
2059*25a9f974SMario Six 	set_reg(dev, REG_FRAC_BYTES_PER_TU, 0);
2060*25a9f974SMario Six 	set_reg(dev, REG_INIT_WAIT, 0);
2061*25a9f974SMario Six }
2062*25a9f974SMario Six 
2063*25a9f974SMario Six /**
2064*25a9f974SMario Six  * set_msa_values() - Set MSA values
2065*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
2066*25a9f974SMario Six  *
2067*25a9f974SMario Six  * Set the main stream attributes registers of the DisplayPort TX
2068*25a9f974SMario Six  * core with the values specified in the main stream attributes configuration
2069*25a9f974SMario Six  * structure.
2070*25a9f974SMario Six  */
set_msa_values(struct udevice * dev)2071*25a9f974SMario Six static void set_msa_values(struct udevice *dev)
2072*25a9f974SMario Six {
2073*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
2074*25a9f974SMario Six 	struct main_stream_attributes *msa_config =
2075*25a9f974SMario Six 		&dp_tx->main_stream_attributes;
2076*25a9f974SMario Six 
2077*25a9f974SMario Six 	printf("       set MSA %u x %u\n", msa_config->h_active,
2078*25a9f974SMario Six 	       msa_config->v_active);
2079*25a9f974SMario Six 
2080*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_HTOTAL, msa_config->h_total);
2081*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_VTOTAL, msa_config->v_total);
2082*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_POLARITY,
2083*25a9f974SMario Six 		msa_config->h_sync_polarity |
2084*25a9f974SMario Six 		(msa_config->v_sync_polarity <<
2085*25a9f974SMario Six 		 MAIN_STREAMX_POLARITY_VSYNC_POL_SHIFT));
2086*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_HSWIDTH, msa_config->h_sync_width);
2087*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_VSWIDTH, msa_config->v_sync_width);
2088*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_HRES, msa_config->h_active);
2089*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_VRES, msa_config->v_active);
2090*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_HSTART, msa_config->h_start);
2091*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_VSTART, msa_config->v_start);
2092*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_MISC0, msa_config->misc_0);
2093*25a9f974SMario Six 	set_reg(dev, REG_MAIN_STREAM_MISC1, msa_config->misc_1);
2094*25a9f974SMario Six 	set_reg(dev, REG_USER_PIXEL_WIDTH, msa_config->user_pixel_width);
2095*25a9f974SMario Six 
2096*25a9f974SMario Six 	set_reg(dev, REG_M_VID, msa_config->pixel_clock_hz / 1000);
2097*25a9f974SMario Six 	set_reg(dev, REG_N_VID, msa_config->n_vid);
2098*25a9f974SMario Six 	set_reg(dev, REG_USER_DATA_COUNT_PER_LANE, msa_config->data_per_lane);
2099*25a9f974SMario Six 
2100*25a9f974SMario Six 	set_line_reset(dev);
2101*25a9f974SMario Six 
2102*25a9f974SMario Six 	set_reg(dev, REG_TU_SIZE, msa_config->transfer_unit_size);
2103*25a9f974SMario Six 	set_reg(dev, REG_MIN_BYTES_PER_TU, msa_config->avg_bytes_per_tu / 1000);
2104*25a9f974SMario Six 	set_reg(dev, REG_FRAC_BYTES_PER_TU,
2105*25a9f974SMario Six 		(msa_config->avg_bytes_per_tu % 1000) * 1024 / 1000);
2106*25a9f974SMario Six 	set_reg(dev, REG_INIT_WAIT, msa_config->init_wait);
2107*25a9f974SMario Six }
2108*25a9f974SMario Six 
2109*25a9f974SMario Six /*
2110*25a9f974SMario Six  * external API
2111*25a9f974SMario Six  */
2112*25a9f974SMario Six 
2113*25a9f974SMario Six /**
2114*25a9f974SMario Six  * logicore_dp_tx_set_msa() - Set given MSA values on device
2115*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
2116*25a9f974SMario Six  * @msa: The MSA values to set for the device
2117*25a9f974SMario Six  */
logicore_dp_tx_set_msa(struct udevice * dev,struct logicore_dp_tx_msa * msa)2118*25a9f974SMario Six static void logicore_dp_tx_set_msa(struct udevice *dev,
2119*25a9f974SMario Six 				   struct logicore_dp_tx_msa *msa)
2120*25a9f974SMario Six {
2121*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
2122*25a9f974SMario Six 
2123*25a9f974SMario Six 	memset(&dp_tx->main_stream_attributes, 0,
2124*25a9f974SMario Six 	       sizeof(struct main_stream_attributes));
2125*25a9f974SMario Six 
2126*25a9f974SMario Six 	dp_tx->main_stream_attributes.pixel_clock_hz = msa->pixel_clock_hz;
2127*25a9f974SMario Six 	dp_tx->main_stream_attributes.bits_per_color = msa->bits_per_color;
2128*25a9f974SMario Six 	dp_tx->main_stream_attributes.h_active = msa->h_active;
2129*25a9f974SMario Six 	dp_tx->main_stream_attributes.h_start = msa->h_start;
2130*25a9f974SMario Six 	dp_tx->main_stream_attributes.h_sync_polarity = msa->h_sync_polarity;
2131*25a9f974SMario Six 	dp_tx->main_stream_attributes.h_sync_width = msa->h_sync_width;
2132*25a9f974SMario Six 	dp_tx->main_stream_attributes.h_total = msa->h_total;
2133*25a9f974SMario Six 	dp_tx->main_stream_attributes.v_active = msa->v_active;
2134*25a9f974SMario Six 	dp_tx->main_stream_attributes.v_start = msa->v_start;
2135*25a9f974SMario Six 	dp_tx->main_stream_attributes.v_sync_polarity = msa->v_sync_polarity;
2136*25a9f974SMario Six 	dp_tx->main_stream_attributes.v_sync_width = msa->v_sync_width;
2137*25a9f974SMario Six 	dp_tx->main_stream_attributes.v_total = msa->v_total;
2138*25a9f974SMario Six 	dp_tx->main_stream_attributes.override_user_pixel_width =
2139*25a9f974SMario Six 		msa->override_user_pixel_width;
2140*25a9f974SMario Six 	dp_tx->main_stream_attributes.user_pixel_width = msa->user_pixel_width;
2141*25a9f974SMario Six 	dp_tx->main_stream_attributes.synchronous_clock_mode = 0;
2142*25a9f974SMario Six }
2143*25a9f974SMario Six 
2144*25a9f974SMario Six /**
2145*25a9f974SMario Six  * logicore_dp_tx_video_enable() - Enable video output
2146*25a9f974SMario Six  * @dev: The LogiCore DP TX device in question
2147*25a9f974SMario Six  * @msa: The MSA values to set for the device
2148*25a9f974SMario Six  *
2149*25a9f974SMario Six  * Return: 0 if the video was enabled successfully, -ve on error
2150*25a9f974SMario Six  */
logicore_dp_tx_video_enable(struct udevice * dev,struct logicore_dp_tx_msa * msa)2151*25a9f974SMario Six static int logicore_dp_tx_video_enable(struct udevice *dev,
2152*25a9f974SMario Six 				       struct logicore_dp_tx_msa *msa)
2153*25a9f974SMario Six {
2154*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
2155*25a9f974SMario Six 	int res;
2156*25a9f974SMario Six 	u8 power = 0x01;
2157*25a9f974SMario Six 
2158*25a9f974SMario Six 	if (!is_connected(dev)) {
2159*25a9f974SMario Six 		printf("       no DP sink connected\n");
2160*25a9f974SMario Six 		return -EIO;
2161*25a9f974SMario Six 	}
2162*25a9f974SMario Six 
2163*25a9f974SMario Six 	initialize(dev);
2164*25a9f974SMario Six 
2165*25a9f974SMario Six 	disable_main_link(dev);
2166*25a9f974SMario Six 
2167*25a9f974SMario Six 	logicore_dp_tx_set_msa(dev, msa);
2168*25a9f974SMario Six 
2169*25a9f974SMario Six 	get_rx_capabilities(dev);
2170*25a9f974SMario Six 
2171*25a9f974SMario Six 	printf("       DP sink connected\n");
2172*25a9f974SMario Six 	aux_write(dev, DPCD_SET_POWER_DP_PWR_VOLTAGE, 1, &power);
2173*25a9f974SMario Six 	set_enhanced_frame_mode(dev, true);
2174*25a9f974SMario Six 	cfg_main_link_max(dev);
2175*25a9f974SMario Six 	res = establish_link(dev);
2176*25a9f974SMario Six 	printf("       establish_link: %s, vs: %d, pe: %d\n",
2177*25a9f974SMario Six 	       res ? "failed" : "ok", dp_tx->link_config.vs_level,
2178*25a9f974SMario Six 	       dp_tx->link_config.pe_level);
2179*25a9f974SMario Six 
2180*25a9f974SMario Six 	cfg_msa_recalculate(dev);
2181*25a9f974SMario Six 
2182*25a9f974SMario Six 	clear_msa_values(dev);
2183*25a9f974SMario Six 	set_msa_values(dev);
2184*25a9f974SMario Six 
2185*25a9f974SMario Six 	enable_main_link(dev);
2186*25a9f974SMario Six 
2187*25a9f974SMario Six 	return 0;
2188*25a9f974SMario Six }
2189*25a9f974SMario Six 
2190*25a9f974SMario Six /*
2191*25a9f974SMario Six  * Driver functions
2192*25a9f974SMario Six  */
2193*25a9f974SMario Six 
logicore_dp_tx_enable(struct udevice * dev,int panel_bpp,const struct display_timing * timing)2194*25a9f974SMario Six static int logicore_dp_tx_enable(struct udevice *dev, int panel_bpp,
2195*25a9f974SMario Six 				 const struct display_timing *timing)
2196*25a9f974SMario Six {
2197*25a9f974SMario Six 	struct clk pixclock;
2198*25a9f974SMario Six 	struct logicore_dp_tx_msa *msa;
2199*25a9f974SMario Six 	struct logicore_dp_tx_msa mode_640_480_60 = {
2200*25a9f974SMario Six 		.pixel_clock_hz = 25175000,
2201*25a9f974SMario Six 		.bits_per_color = 8,
2202*25a9f974SMario Six 		.h_active = 640,
2203*25a9f974SMario Six 		.h_start = 144,
2204*25a9f974SMario Six 		.h_sync_polarity = false,
2205*25a9f974SMario Six 		.h_sync_width = 96,
2206*25a9f974SMario Six 		.h_total = 800,
2207*25a9f974SMario Six 		.v_active = 480,
2208*25a9f974SMario Six 		.v_start = 35,
2209*25a9f974SMario Six 		.v_sync_polarity = false,
2210*25a9f974SMario Six 		.v_sync_width = 2,
2211*25a9f974SMario Six 		.v_total = 525,
2212*25a9f974SMario Six 		.override_user_pixel_width = false,
2213*25a9f974SMario Six 		.user_pixel_width = 0,
2214*25a9f974SMario Six 	};
2215*25a9f974SMario Six 
2216*25a9f974SMario Six 	struct logicore_dp_tx_msa mode_720_400_70 = {
2217*25a9f974SMario Six 		.pixel_clock_hz = 28300000,
2218*25a9f974SMario Six 		.bits_per_color = 8,
2219*25a9f974SMario Six 		.h_active = 720,
2220*25a9f974SMario Six 		.h_start = 162,
2221*25a9f974SMario Six 		.h_sync_polarity = false,
2222*25a9f974SMario Six 		.h_sync_width = 108,
2223*25a9f974SMario Six 		.h_total = 900,
2224*25a9f974SMario Six 		.v_active = 400,
2225*25a9f974SMario Six 		.v_start = 37,
2226*25a9f974SMario Six 		.v_sync_polarity = true,
2227*25a9f974SMario Six 		.v_sync_width = 2,
2228*25a9f974SMario Six 		.v_total = 449,
2229*25a9f974SMario Six 		.override_user_pixel_width = false,
2230*25a9f974SMario Six 		.user_pixel_width = 0,
2231*25a9f974SMario Six 	};
2232*25a9f974SMario Six 
2233*25a9f974SMario Six 	struct logicore_dp_tx_msa mode_1024_768_60 = {
2234*25a9f974SMario Six 		.pixel_clock_hz = 65000000,
2235*25a9f974SMario Six 		.bits_per_color = 8,
2236*25a9f974SMario Six 		.h_active = 1024,
2237*25a9f974SMario Six 		.h_start = 296,
2238*25a9f974SMario Six 		.h_sync_polarity = false,
2239*25a9f974SMario Six 		.h_sync_width = 136,
2240*25a9f974SMario Six 		.h_total = 1344,
2241*25a9f974SMario Six 		.v_active = 768,
2242*25a9f974SMario Six 		.v_start = 35,
2243*25a9f974SMario Six 		.v_sync_polarity = false,
2244*25a9f974SMario Six 		.v_sync_width = 2,
2245*25a9f974SMario Six 		.v_total = 806,
2246*25a9f974SMario Six 		.override_user_pixel_width = false,
2247*25a9f974SMario Six 		.user_pixel_width = 0,
2248*25a9f974SMario Six 	};
2249*25a9f974SMario Six 
2250*25a9f974SMario Six 	if (timing->hactive.typ == 1024 && timing->vactive.typ == 768)
2251*25a9f974SMario Six 		msa = &mode_1024_768_60;
2252*25a9f974SMario Six 	else if (timing->hactive.typ == 720 && timing->vactive.typ == 400)
2253*25a9f974SMario Six 		msa = &mode_720_400_70;
2254*25a9f974SMario Six 	else
2255*25a9f974SMario Six 		msa = &mode_640_480_60;
2256*25a9f974SMario Six 
2257*25a9f974SMario Six 	if (clk_get_by_index(dev, 0, &pixclock)) {
2258*25a9f974SMario Six 		printf("%s: Could not get pixelclock\n", dev->name);
2259*25a9f974SMario Six 		return -1;
2260*25a9f974SMario Six 	}
2261*25a9f974SMario Six 	clk_set_rate(&pixclock, msa->pixel_clock_hz);
2262*25a9f974SMario Six 
2263*25a9f974SMario Six 	return logicore_dp_tx_video_enable(dev, msa);
2264*25a9f974SMario Six }
2265*25a9f974SMario Six 
logicore_dp_tx_probe(struct udevice * dev)2266*25a9f974SMario Six static int logicore_dp_tx_probe(struct udevice *dev)
2267*25a9f974SMario Six {
2268*25a9f974SMario Six 	struct dp_tx *dp_tx = dev_get_priv(dev);
2269*25a9f974SMario Six 
2270*25a9f974SMario Six 	dp_tx->s_axi_clk = S_AXI_CLK_DEFAULT;
2271*25a9f974SMario Six 	dp_tx->train_adaptive = false;
2272*25a9f974SMario Six 	dp_tx->max_link_rate = DPCD_MAX_LINK_RATE_540GBPS;
2273*25a9f974SMario Six 	dp_tx->max_lane_count = DPCD_MAX_LANE_COUNT_4;
2274*25a9f974SMario Six 
2275*25a9f974SMario Six 	dp_tx->base = dev_read_u32_default(dev, "reg", -1);
2276*25a9f974SMario Six 
2277*25a9f974SMario Six 	return 0;
2278*25a9f974SMario Six }
2279*25a9f974SMario Six 
2280*25a9f974SMario Six static const struct dm_display_ops logicore_dp_tx_ops = {
2281*25a9f974SMario Six 	.enable = logicore_dp_tx_enable,
2282*25a9f974SMario Six };
2283*25a9f974SMario Six 
2284*25a9f974SMario Six static const struct udevice_id logicore_dp_tx_ids[] = {
2285*25a9f974SMario Six 	{ .compatible = "gdsys,logicore_dp_tx" },
2286*25a9f974SMario Six 	{ /* sentinel */ }
2287*25a9f974SMario Six };
2288*25a9f974SMario Six 
2289*25a9f974SMario Six U_BOOT_DRIVER(logicore_dp_tx) = {
2290*25a9f974SMario Six 	.name			= "logicore_dp_tx",
2291*25a9f974SMario Six 	.id			= UCLASS_DISPLAY,
2292*25a9f974SMario Six 	.of_match		= logicore_dp_tx_ids,
2293*25a9f974SMario Six 	.probe			= logicore_dp_tx_probe,
2294*25a9f974SMario Six 	.priv_auto_alloc_size	= sizeof(struct dp_tx),
2295*25a9f974SMario Six 	.ops			= &logicore_dp_tx_ops,
2296*25a9f974SMario Six };
2297