1/*
2 * Copyright 2023 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: AMD
23 *
24 */
25#include "link_hwss_hpo_dp.h"
26#include "link_hwss_hpo_fixed_vs_pe_retimer_dp.h"
27#include "link_hwss_dio_fixed_vs_pe_retimer.h"
28
29static void dp_hpo_fixed_vs_pe_retimer_set_tx_ffe(struct dc_link *link,
30		const struct dc_lane_settings *hw_lane_settings)
31{
32	const uint8_t vendor_ffe_preset_table[16] = {
33											0x01, 0x41, 0x61, 0x81,
34											0xB1, 0x05, 0x35, 0x65,
35											0x85, 0xA5, 0x09, 0x39,
36											0x59, 0x89, 0x0F, 0x24};
37
38	const uint8_t ffe_mask[4] = {
39			(hw_lane_settings[0].FFE_PRESET.settings.no_deemphasis != 0 ? 0x0F : 0xFF)
40				& (hw_lane_settings[0].FFE_PRESET.settings.no_preshoot != 0 ? 0xF1 : 0xFF),
41			(hw_lane_settings[1].FFE_PRESET.settings.no_deemphasis != 0 ? 0x0F : 0xFF)
42				& (hw_lane_settings[1].FFE_PRESET.settings.no_preshoot != 0 ? 0xF1 : 0xFF),
43			(hw_lane_settings[2].FFE_PRESET.settings.no_deemphasis != 0 ? 0x0F : 0xFF)
44				& (hw_lane_settings[2].FFE_PRESET.settings.no_preshoot != 0 ? 0xF1 : 0xFF),
45			(hw_lane_settings[3].FFE_PRESET.settings.no_deemphasis != 0 ? 0x0F : 0xFF)
46				& (hw_lane_settings[3].FFE_PRESET.settings.no_preshoot != 0 ? 0xF1 : 0xFF)};
47
48	const uint8_t ffe_cfg[4] = {
49			vendor_ffe_preset_table[hw_lane_settings[0].FFE_PRESET.settings.level] & ffe_mask[0],
50			vendor_ffe_preset_table[hw_lane_settings[1].FFE_PRESET.settings.level] & ffe_mask[1],
51			vendor_ffe_preset_table[hw_lane_settings[2].FFE_PRESET.settings.level] & ffe_mask[2],
52			vendor_ffe_preset_table[hw_lane_settings[3].FFE_PRESET.settings.level] & ffe_mask[3]};
53
54	const uint8_t dp_type = dp_dio_fixed_vs_pe_retimer_lane_cfg_to_hw_cfg(link);
55
56	const uint8_t vendor_lttpr_write_data_ffe1[4] = {0x01, 0x50, dp_type, 0x0F};
57	const uint8_t vendor_lttpr_write_data_ffe2[4] = {0x01, 0x55, dp_type, ffe_cfg[0]};
58	const uint8_t vendor_lttpr_write_data_ffe3[4] = {0x01, 0x56, dp_type, ffe_cfg[1]};
59	const uint8_t vendor_lttpr_write_data_ffe4[4] = {0x01, 0x57, dp_type, ffe_cfg[2]};
60	const uint8_t vendor_lttpr_write_data_ffe5[4] = {0x01, 0x58, dp_type, ffe_cfg[3]};
61
62	link->dc->link_srv->configure_fixed_vs_pe_retimer(link->ddc,
63			&vendor_lttpr_write_data_ffe1[0], sizeof(vendor_lttpr_write_data_ffe1));
64	link->dc->link_srv->configure_fixed_vs_pe_retimer(link->ddc,
65			&vendor_lttpr_write_data_ffe2[0], sizeof(vendor_lttpr_write_data_ffe2));
66	link->dc->link_srv->configure_fixed_vs_pe_retimer(link->ddc,
67			&vendor_lttpr_write_data_ffe3[0], sizeof(vendor_lttpr_write_data_ffe3));
68	link->dc->link_srv->configure_fixed_vs_pe_retimer(link->ddc,
69			&vendor_lttpr_write_data_ffe4[0], sizeof(vendor_lttpr_write_data_ffe4));
70	link->dc->link_srv->configure_fixed_vs_pe_retimer(link->ddc,
71			&vendor_lttpr_write_data_ffe5[0], sizeof(vendor_lttpr_write_data_ffe5));
72}
73
74static void dp_hpo_fixed_vs_pe_retimer_program_override_test_pattern(struct dc_link *link,
75		struct encoder_set_dp_phy_pattern_param *tp_params)
76{
77	const uint8_t vendor_lttpr_write_data_pg0[4] = {0x1, 0x11, 0x0, 0x0};
78	const uint8_t vendor_lttpr_write_data_pg1[4] = {0x1, 0x50, 0x50, 0x0};
79	const uint8_t vendor_lttpr_write_data_pg2[4] = {0x1, 0x51, 0x50, 0x0};
80	const uint8_t vendor_lttpr_write_data_pg3[4]  = {0x1, 0x10, 0x58, 0x21};
81	const uint8_t vendor_lttpr_write_data_pg4[4]  = {0x1, 0x10, 0x59, 0x21};
82	const uint8_t vendor_lttpr_write_data_pg5[4] = {0x1, 0x1C, 0x58, 0x4F};
83	const uint8_t vendor_lttpr_write_data_pg6[4] = {0x1, 0x1C, 0x59, 0x4F};
84	const uint8_t vendor_lttpr_write_data_pg7[4]  = {0x1, 0x30, 0x51, 0x20};
85	const uint8_t vendor_lttpr_write_data_pg8[4]  = {0x1, 0x30, 0x52, 0x20};
86	const uint8_t vendor_lttpr_write_data_pg9[4]  = {0x1, 0x30, 0x54, 0x20};
87	const uint8_t vendor_lttpr_write_data_pg10[4] = {0x1, 0x30, 0x55, 0x20};
88
89	link->dc->link_srv->configure_fixed_vs_pe_retimer(link->ddc,
90			&vendor_lttpr_write_data_pg0[0], sizeof(vendor_lttpr_write_data_pg0));
91	link->dc->link_srv->configure_fixed_vs_pe_retimer(link->ddc,
92			&vendor_lttpr_write_data_pg1[0], sizeof(vendor_lttpr_write_data_pg1));
93	link->dc->link_srv->configure_fixed_vs_pe_retimer(link->ddc,
94			&vendor_lttpr_write_data_pg2[0], sizeof(vendor_lttpr_write_data_pg2));
95	link->dc->link_srv->configure_fixed_vs_pe_retimer(link->ddc,
96			&vendor_lttpr_write_data_pg3[0], sizeof(vendor_lttpr_write_data_pg3));
97	link->dc->link_srv->configure_fixed_vs_pe_retimer(link->ddc,
98			&vendor_lttpr_write_data_pg4[0], sizeof(vendor_lttpr_write_data_pg4));
99	link->dc->link_srv->configure_fixed_vs_pe_retimer(link->ddc,
100			&vendor_lttpr_write_data_pg5[0], sizeof(vendor_lttpr_write_data_pg5));
101	link->dc->link_srv->configure_fixed_vs_pe_retimer(link->ddc,
102			&vendor_lttpr_write_data_pg6[0], sizeof(vendor_lttpr_write_data_pg6));
103
104	if (link->cur_link_settings.lane_count == LANE_COUNT_FOUR)
105		link->dc->link_srv->configure_fixed_vs_pe_retimer(link->ddc,
106				&vendor_lttpr_write_data_pg7[0], sizeof(vendor_lttpr_write_data_pg7));
107
108	link->dc->link_srv->configure_fixed_vs_pe_retimer(link->ddc,
109			&vendor_lttpr_write_data_pg8[0], sizeof(vendor_lttpr_write_data_pg8));
110
111	if (link->cur_link_settings.lane_count == LANE_COUNT_FOUR)
112		link->dc->link_srv->configure_fixed_vs_pe_retimer(link->ddc,
113				&vendor_lttpr_write_data_pg9[0], sizeof(vendor_lttpr_write_data_pg9));
114
115	link->dc->link_srv->configure_fixed_vs_pe_retimer(link->ddc,
116			&vendor_lttpr_write_data_pg10[0], sizeof(vendor_lttpr_write_data_pg10));
117}
118
119static bool dp_hpo_fixed_vs_pe_retimer_set_override_test_pattern(struct dc_link *link,
120		const struct link_resource *link_res, struct encoder_set_dp_phy_pattern_param *tp_params,
121		const struct link_hwss *link_hwss)
122{
123	struct encoder_set_dp_phy_pattern_param hw_tp_params = { 0 };
124	const uint8_t vendor_lttpr_exit_manual_automation_0[4] = {0x1, 0x11, 0x0, 0x06};
125
126	if (tp_params == NULL)
127		return false;
128
129	if (tp_params->dp_phy_pattern < DP_TEST_PATTERN_SQUARE_BEGIN ||
130			tp_params->dp_phy_pattern > DP_TEST_PATTERN_SQUARE_END) {
131		// Deprogram overrides from previously set square wave override
132		if (link->current_test_pattern == DP_TEST_PATTERN_80BIT_CUSTOM ||
133				link->current_test_pattern == DP_TEST_PATTERN_D102)
134			link->dc->link_srv->configure_fixed_vs_pe_retimer(link->ddc,
135					&vendor_lttpr_exit_manual_automation_0[0],
136					sizeof(vendor_lttpr_exit_manual_automation_0));
137		else
138			dp_dio_fixed_vs_pe_retimer_exit_manual_automation(link);
139
140		return false;
141	}
142
143	hw_tp_params.dp_phy_pattern = DP_TEST_PATTERN_PRBS31;
144	hw_tp_params.dp_panel_mode = tp_params->dp_panel_mode;
145
146	if (link_hwss->ext.set_dp_link_test_pattern)
147		link_hwss->ext.set_dp_link_test_pattern(link, link_res, &hw_tp_params);
148
149	dp_hpo_fixed_vs_pe_retimer_program_override_test_pattern(link, tp_params);
150
151	dp_hpo_fixed_vs_pe_retimer_set_tx_ffe(link, &link->cur_lane_setting[0]);
152
153	return true;
154}
155
156static void set_hpo_fixed_vs_pe_retimer_dp_link_test_pattern(struct dc_link *link,
157		const struct link_resource *link_res,
158		struct encoder_set_dp_phy_pattern_param *tp_params)
159{
160	if (!dp_hpo_fixed_vs_pe_retimer_set_override_test_pattern(
161			link, link_res, tp_params, get_hpo_dp_link_hwss())) {
162		link_res->hpo_dp_link_enc->funcs->set_link_test_pattern(
163				link_res->hpo_dp_link_enc, tp_params);
164	}
165	link->dc->link_srv->dp_trace_source_sequence(link, DPCD_SOURCE_SEQ_AFTER_SET_SOURCE_PATTERN);
166}
167
168static void set_hpo_fixed_vs_pe_retimer_dp_lane_settings(struct dc_link *link,
169		const struct link_resource *link_res,
170		const struct dc_link_settings *link_settings,
171		const struct dc_lane_settings lane_settings[LANE_COUNT_DP_MAX])
172{
173	link_res->hpo_dp_link_enc->funcs->set_ffe(
174			link_res->hpo_dp_link_enc,
175			link_settings,
176			lane_settings[0].FFE_PRESET.raw);
177
178	// FFE is programmed when retimer is programmed for SQ128, but explicit
179	// programming needed here as well in case FFE-only update is requested
180	if (link->current_test_pattern >= DP_TEST_PATTERN_SQUARE_BEGIN &&
181			link->current_test_pattern <= DP_TEST_PATTERN_SQUARE_END)
182		dp_hpo_fixed_vs_pe_retimer_set_tx_ffe(link, &lane_settings[0]);
183}
184
185static void enable_hpo_fixed_vs_pe_retimer_dp_link_output(struct dc_link *link,
186		const struct link_resource *link_res,
187		enum amd_signal_type signal,
188		enum clock_source_id clock_source,
189		const struct dc_link_settings *link_settings)
190{
191	if (link_settings->lane_count == LANE_COUNT_FOUR)
192		enable_dio_fixed_vs_pe_retimer_program_4lane_output(link);
193
194	enable_hpo_dp_link_output(link, link_res, signal, clock_source, link_settings);
195}
196
197static const struct link_hwss hpo_fixed_vs_pe_retimer_dp_link_hwss = {
198	.setup_stream_encoder = setup_hpo_dp_stream_encoder,
199	.reset_stream_encoder = reset_hpo_dp_stream_encoder,
200	.setup_stream_attribute = setup_hpo_dp_stream_attribute,
201	.disable_link_output = disable_hpo_dp_link_output,
202	.setup_audio_output = setup_hpo_dp_audio_output,
203	.enable_audio_packet = enable_hpo_dp_audio_packet,
204	.disable_audio_packet = disable_hpo_dp_audio_packet,
205	.ext = {
206		.set_throttled_vcp_size = set_hpo_dp_throttled_vcp_size,
207		.set_hblank_min_symbol_width = set_hpo_dp_hblank_min_symbol_width,
208		.enable_dp_link_output = enable_hpo_fixed_vs_pe_retimer_dp_link_output,
209		.set_dp_link_test_pattern  = set_hpo_fixed_vs_pe_retimer_dp_link_test_pattern,
210		.set_dp_lane_settings = set_hpo_fixed_vs_pe_retimer_dp_lane_settings,
211		.update_stream_allocation_table = update_hpo_dp_stream_allocation_table,
212	},
213};
214
215bool requires_fixed_vs_pe_retimer_hpo_link_hwss(const struct dc_link *link)
216{
217	if (!(link->chip_caps & EXT_DISPLAY_PATH_CAPS__DP_FIXED_VS_EN))
218		return false;
219
220	if (!link->dpcd_caps.lttpr_caps.main_link_channel_coding.bits.DP_128b_132b_SUPPORTED)
221		return false;
222
223	return true;
224}
225
226const struct link_hwss *get_hpo_fixed_vs_pe_retimer_dp_link_hwss(void)
227{
228	return &hpo_fixed_vs_pe_retimer_dp_link_hwss;
229}
230