xref: /linux/drivers/gpu/drm/amd/display/dc/optc/dcn35/dcn35_optc.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
1b9c96af6SQingqing Zhuo /* SPDX-License-Identifier: MIT */
2b9c96af6SQingqing Zhuo /*
3b9c96af6SQingqing Zhuo  * Copyright 2023 Advanced Micro Devices, Inc.
4b9c96af6SQingqing Zhuo  *
5b9c96af6SQingqing Zhuo  * Permission is hereby granted, free of charge, to any person obtaining a
6b9c96af6SQingqing Zhuo  * copy of this software and associated documentation files (the "Software"),
7b9c96af6SQingqing Zhuo  * to deal in the Software without restriction, including without limitation
8b9c96af6SQingqing Zhuo  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9b9c96af6SQingqing Zhuo  * and/or sell copies of the Software, and to permit persons to whom the
10b9c96af6SQingqing Zhuo  * Software is furnished to do so, subject to the following conditions:
11b9c96af6SQingqing Zhuo  *
12b9c96af6SQingqing Zhuo  * The above copyright notice and this permission notice shall be included in
13b9c96af6SQingqing Zhuo  * all copies or substantial portions of the Software.
14b9c96af6SQingqing Zhuo  *
15b9c96af6SQingqing Zhuo  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b9c96af6SQingqing Zhuo  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b9c96af6SQingqing Zhuo  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b9c96af6SQingqing Zhuo  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19b9c96af6SQingqing Zhuo  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20b9c96af6SQingqing Zhuo  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21b9c96af6SQingqing Zhuo  * OTHER DEALINGS IN THE SOFTWARE.
22b9c96af6SQingqing Zhuo  *
2378964fcaSStylon Wang  * Authors: AMD
2478964fcaSStylon Wang  *
25b9c96af6SQingqing Zhuo  */
26b9c96af6SQingqing Zhuo 
27b9c96af6SQingqing Zhuo #include "dcn35_optc.h"
28b9c96af6SQingqing Zhuo 
29b9c96af6SQingqing Zhuo #include "dcn30/dcn30_optc.h"
30b9c96af6SQingqing Zhuo #include "dcn31/dcn31_optc.h"
31b9c96af6SQingqing Zhuo #include "dcn32/dcn32_optc.h"
32b9c96af6SQingqing Zhuo #include "reg_helper.h"
33b9c96af6SQingqing Zhuo #include "dc.h"
34b9c96af6SQingqing Zhuo #include "dcn_calc_math.h"
35eed4eddaSRobin Chen #include "dc_dmub_srv.h"
36b9c96af6SQingqing Zhuo 
37b9c96af6SQingqing Zhuo #define REG(reg)\
38b9c96af6SQingqing Zhuo 	optc1->tg_regs->reg
39b9c96af6SQingqing Zhuo 
40b9c96af6SQingqing Zhuo #define CTX \
41b9c96af6SQingqing Zhuo 	optc1->base.ctx
42b9c96af6SQingqing Zhuo 
43b9c96af6SQingqing Zhuo #undef FN
44b9c96af6SQingqing Zhuo #define FN(reg_name, field_name) \
45b9c96af6SQingqing Zhuo 	optc1->tg_shift->field_name, optc1->tg_mask->field_name
46b9c96af6SQingqing Zhuo 
47b9c96af6SQingqing Zhuo /**
48eae36991SSrinivasan Shanmugam  * optc35_set_odm_combine() - Enable CRTC - call ASIC Control Object to enable Timing generator.
49eae36991SSrinivasan Shanmugam  *
50eae36991SSrinivasan Shanmugam  * @optc: Output Pipe Timing Combine instance reference.
51eae36991SSrinivasan Shanmugam  * @opp_id: Output Plane Processor instance ID.
52eae36991SSrinivasan Shanmugam  * @opp_cnt: Output Plane Processor count.
5348fdcadbSSrinivasan Shanmugam  * @segment_width: Width of the segment.
5448fdcadbSSrinivasan Shanmugam  * @last_segment_width: Width of the last segment.
55eae36991SSrinivasan Shanmugam  *
56eae36991SSrinivasan Shanmugam  * Return: void.
57b9c96af6SQingqing Zhuo  */
optc35_set_odm_combine(struct timing_generator * optc,int * opp_id,int opp_cnt,int segment_width,int last_segment_width)58b9c96af6SQingqing Zhuo static void optc35_set_odm_combine(struct timing_generator *optc, int *opp_id, int opp_cnt,
59f9d48a88SWenjing Liu 		int segment_width, int last_segment_width)
60b9c96af6SQingqing Zhuo {
61b9c96af6SQingqing Zhuo 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
62b9c96af6SQingqing Zhuo 	uint32_t memory_mask = 0;
63f9d48a88SWenjing Liu 	int h_active = segment_width * opp_cnt;
64b9c96af6SQingqing Zhuo 	/* Each memory instance is 2048x(314x2) bits to support half line of 4096 */
65b9c96af6SQingqing Zhuo 	int odm_mem_count = (h_active + 2047) / 2048;
66b9c96af6SQingqing Zhuo 
67b9c96af6SQingqing Zhuo 	/*
68b9c96af6SQingqing Zhuo 	 * display <= 4k : 2 memories + 2 pipes
69b9c96af6SQingqing Zhuo 	 * 4k < display <= 8k : 4 memories + 2 pipes
70b9c96af6SQingqing Zhuo 	 * 8k < display <= 12k : 6 memories + 4 pipes
71b9c96af6SQingqing Zhuo 	 */
72b9c96af6SQingqing Zhuo 	if (opp_cnt == 4) {
73b9c96af6SQingqing Zhuo 		if (odm_mem_count <= 2)
74b9c96af6SQingqing Zhuo 			memory_mask = 0x3;
75b9c96af6SQingqing Zhuo 		else if (odm_mem_count <= 4)
76b9c96af6SQingqing Zhuo 			memory_mask = 0xf;
77b9c96af6SQingqing Zhuo 		else
78b9c96af6SQingqing Zhuo 			memory_mask = 0x3f;
79b9c96af6SQingqing Zhuo 	} else {
80b9c96af6SQingqing Zhuo 		if (odm_mem_count <= 2)
81b9c96af6SQingqing Zhuo 			memory_mask = 0x1 << (opp_id[0] * 2) | 0x1 << (opp_id[1] * 2);
82b9c96af6SQingqing Zhuo 		else if (odm_mem_count <= 4)
83b9c96af6SQingqing Zhuo 			memory_mask = 0x3 << (opp_id[0] * 2) | 0x3 << (opp_id[1] * 2);
84b9c96af6SQingqing Zhuo 		else
85b9c96af6SQingqing Zhuo 			memory_mask = 0x77;
86b9c96af6SQingqing Zhuo 	}
87b9c96af6SQingqing Zhuo 
88b9c96af6SQingqing Zhuo 	REG_SET(OPTC_MEMORY_CONFIG, 0,
89b9c96af6SQingqing Zhuo 		OPTC_MEM_SEL, memory_mask);
90b9c96af6SQingqing Zhuo 
91b9c96af6SQingqing Zhuo 	if (opp_cnt == 2) {
92b9c96af6SQingqing Zhuo 		REG_SET_3(OPTC_DATA_SOURCE_SELECT, 0,
93b9c96af6SQingqing Zhuo 				OPTC_NUM_OF_INPUT_SEGMENT, 1,
94b9c96af6SQingqing Zhuo 				OPTC_SEG0_SRC_SEL, opp_id[0],
95b9c96af6SQingqing Zhuo 				OPTC_SEG1_SRC_SEL, opp_id[1]);
96b9c96af6SQingqing Zhuo 	} else if (opp_cnt == 4) {
97b9c96af6SQingqing Zhuo 		REG_SET_5(OPTC_DATA_SOURCE_SELECT, 0,
98b9c96af6SQingqing Zhuo 				OPTC_NUM_OF_INPUT_SEGMENT, 3,
99b9c96af6SQingqing Zhuo 				OPTC_SEG0_SRC_SEL, opp_id[0],
100b9c96af6SQingqing Zhuo 				OPTC_SEG1_SRC_SEL, opp_id[1],
101b9c96af6SQingqing Zhuo 				OPTC_SEG2_SRC_SEL, opp_id[2],
102b9c96af6SQingqing Zhuo 				OPTC_SEG3_SRC_SEL, opp_id[3]);
103b9c96af6SQingqing Zhuo 	}
104b9c96af6SQingqing Zhuo 
105b9c96af6SQingqing Zhuo 	REG_UPDATE(OPTC_WIDTH_CONTROL,
106f9d48a88SWenjing Liu 			OPTC_SEGMENT_WIDTH, segment_width);
107b9c96af6SQingqing Zhuo 
108b9c96af6SQingqing Zhuo 	REG_UPDATE(OTG_H_TIMING_CNTL, OTG_H_TIMING_DIV_MODE, opp_cnt - 1);
109b9c96af6SQingqing Zhuo 	optc1->opp_count = opp_cnt;
110b9c96af6SQingqing Zhuo }
111b9c96af6SQingqing Zhuo 
optc35_enable_crtc(struct timing_generator * optc)112b9c96af6SQingqing Zhuo static bool optc35_enable_crtc(struct timing_generator *optc)
113b9c96af6SQingqing Zhuo {
114b9c96af6SQingqing Zhuo 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
115b9c96af6SQingqing Zhuo 
116b9c96af6SQingqing Zhuo 	/* opp instance for OTG, 1 to 1 mapping and odm will adjust */
117b9c96af6SQingqing Zhuo 	REG_UPDATE(OPTC_DATA_SOURCE_SELECT,
118b9c96af6SQingqing Zhuo 			OPTC_SEG0_SRC_SEL, optc->inst);
119b9c96af6SQingqing Zhuo 
120b9c96af6SQingqing Zhuo 	/* VTG enable first is for HW workaround */
121b9c96af6SQingqing Zhuo 	REG_UPDATE(CONTROL,
122b9c96af6SQingqing Zhuo 			VTG0_ENABLE, 1);
123b9c96af6SQingqing Zhuo 
124b9c96af6SQingqing Zhuo 	REG_SEQ_START();
125b9c96af6SQingqing Zhuo 
126b9c96af6SQingqing Zhuo 	/* Enable CRTC */
127b9c96af6SQingqing Zhuo 	REG_UPDATE_2(OTG_CONTROL,
128b9c96af6SQingqing Zhuo 			OTG_DISABLE_POINT_CNTL, 2,
129b9c96af6SQingqing Zhuo 			OTG_MASTER_EN, 1);
130b9c96af6SQingqing Zhuo 
131b9c96af6SQingqing Zhuo 	REG_SEQ_SUBMIT();
132b9c96af6SQingqing Zhuo 	REG_SEQ_WAIT_DONE();
133b9c96af6SQingqing Zhuo 
134b9c96af6SQingqing Zhuo 	return true;
135b9c96af6SQingqing Zhuo }
136b9c96af6SQingqing Zhuo 
137b9c96af6SQingqing Zhuo /* disable_crtc */
optc35_disable_crtc(struct timing_generator * optc)138b9c96af6SQingqing Zhuo static bool optc35_disable_crtc(struct timing_generator *optc)
139b9c96af6SQingqing Zhuo {
140b9c96af6SQingqing Zhuo 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
141b9c96af6SQingqing Zhuo 
142e7b2b108SIlya Bakoulin 	REG_UPDATE_5(OPTC_DATA_SOURCE_SELECT,
143e7b2b108SIlya Bakoulin 			OPTC_SEG0_SRC_SEL, 0xf,
144e7b2b108SIlya Bakoulin 			OPTC_SEG1_SRC_SEL, 0xf,
145e7b2b108SIlya Bakoulin 			OPTC_SEG2_SRC_SEL, 0xf,
146e7b2b108SIlya Bakoulin 			OPTC_SEG3_SRC_SEL, 0xf,
147e7b2b108SIlya Bakoulin 			OPTC_NUM_OF_INPUT_SEGMENT, 0);
148e7b2b108SIlya Bakoulin 
1493ba2a0bfSIlya Bakoulin 	REG_UPDATE(OPTC_MEMORY_CONFIG,
1503ba2a0bfSIlya Bakoulin 			OPTC_MEM_SEL, 0);
1513ba2a0bfSIlya Bakoulin 
1527bdbfb4eSGeorge Shen 	/* disable otg request until end of the first line
1537bdbfb4eSGeorge Shen 	 * in the vertical blank region
1547bdbfb4eSGeorge Shen 	 */
1557bdbfb4eSGeorge Shen 	REG_UPDATE(OTG_CONTROL,
1567bdbfb4eSGeorge Shen 			OTG_MASTER_EN, 0);
1577bdbfb4eSGeorge Shen 
158b9c96af6SQingqing Zhuo 	REG_UPDATE(CONTROL,
159b9c96af6SQingqing Zhuo 			VTG0_ENABLE, 0);
160b9c96af6SQingqing Zhuo 
161b9c96af6SQingqing Zhuo 	/* CRTC disabled, so disable  clock. */
162b9c96af6SQingqing Zhuo 	REG_WAIT(OTG_CLOCK_CONTROL,
163b9c96af6SQingqing Zhuo 			OTG_BUSY, 0,
164b9c96af6SQingqing Zhuo 			1, 100000);
165b9c96af6SQingqing Zhuo 	optc1_clear_optc_underflow(optc);
166b9c96af6SQingqing Zhuo 
167b9c96af6SQingqing Zhuo 	return true;
168b9c96af6SQingqing Zhuo }
169b9c96af6SQingqing Zhuo 
optc35_phantom_crtc_post_enable(struct timing_generator * optc)170b9c96af6SQingqing Zhuo static void optc35_phantom_crtc_post_enable(struct timing_generator *optc)
171b9c96af6SQingqing Zhuo {
172b9c96af6SQingqing Zhuo 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
173b9c96af6SQingqing Zhuo 
174b9c96af6SQingqing Zhuo 	/* Disable immediately. */
175b9c96af6SQingqing Zhuo 	REG_UPDATE_2(OTG_CONTROL, OTG_DISABLE_POINT_CNTL, 0, OTG_MASTER_EN, 0);
176b9c96af6SQingqing Zhuo 
177b9c96af6SQingqing Zhuo 	/* CRTC disabled, so disable  clock. */
178b9c96af6SQingqing Zhuo 	REG_WAIT(OTG_CLOCK_CONTROL, OTG_BUSY, 0, 1, 100000);
179b9c96af6SQingqing Zhuo }
180b9c96af6SQingqing Zhuo 
optc35_configure_crc(struct timing_generator * optc,const struct crc_params * params)181b9c96af6SQingqing Zhuo static bool optc35_configure_crc(struct timing_generator *optc,
182b9c96af6SQingqing Zhuo 				 const struct crc_params *params)
183b9c96af6SQingqing Zhuo {
184b9c96af6SQingqing Zhuo 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
185b9c96af6SQingqing Zhuo 
186b6fcc386SWayne Lin 	/* Cannot configure crc on a CRTC that is disabled */
187b9c96af6SQingqing Zhuo 	if (!optc1_is_tg_enabled(optc))
188b9c96af6SQingqing Zhuo 		return false;
189b6fcc386SWayne Lin 
190b6fcc386SWayne Lin 	if (!params->enable || params->reset)
191b9c96af6SQingqing Zhuo 		REG_WRITE(OTG_CRC_CNTL, 0);
192b6fcc386SWayne Lin 
193b9c96af6SQingqing Zhuo 	if (!params->enable)
194b9c96af6SQingqing Zhuo 		return true;
195b6fcc386SWayne Lin 
196b6fcc386SWayne Lin 	/* Program frame boundaries */
197b6fcc386SWayne Lin 	switch (params->crc_eng_inst) {
198b6fcc386SWayne Lin 	case 0:
199b6fcc386SWayne Lin 		/* Window A x axis start and end. */
200b9c96af6SQingqing Zhuo 		REG_UPDATE_2(OTG_CRC0_WINDOWA_X_CONTROL,
201b9c96af6SQingqing Zhuo 				OTG_CRC0_WINDOWA_X_START, params->windowa_x_start,
202b9c96af6SQingqing Zhuo 				OTG_CRC0_WINDOWA_X_END, params->windowa_x_end);
203b6fcc386SWayne Lin 
204b6fcc386SWayne Lin 		/* Window A y axis start and end. */
205b9c96af6SQingqing Zhuo 		REG_UPDATE_2(OTG_CRC0_WINDOWA_Y_CONTROL,
206b9c96af6SQingqing Zhuo 				OTG_CRC0_WINDOWA_Y_START, params->windowa_y_start,
207b9c96af6SQingqing Zhuo 				OTG_CRC0_WINDOWA_Y_END, params->windowa_y_end);
208b6fcc386SWayne Lin 
209b6fcc386SWayne Lin 		/* Window B x axis start and end. */
210b9c96af6SQingqing Zhuo 		REG_UPDATE_2(OTG_CRC0_WINDOWB_X_CONTROL,
211b9c96af6SQingqing Zhuo 				OTG_CRC0_WINDOWB_X_START, params->windowb_x_start,
212b9c96af6SQingqing Zhuo 				OTG_CRC0_WINDOWB_X_END, params->windowb_x_end);
213b6fcc386SWayne Lin 
214b6fcc386SWayne Lin 		/* Window B y axis start and end. */
215b9c96af6SQingqing Zhuo 		REG_UPDATE_2(OTG_CRC0_WINDOWB_Y_CONTROL,
216b9c96af6SQingqing Zhuo 				OTG_CRC0_WINDOWB_Y_START, params->windowb_y_start,
217b9c96af6SQingqing Zhuo 				OTG_CRC0_WINDOWB_Y_END, params->windowb_y_end);
218b6fcc386SWayne Lin 
219b6fcc386SWayne Lin 		if (optc1->base.ctx->dc->debug.otg_crc_db && optc1->tg_mask->OTG_CRC_WINDOW_DB_EN != 0)
220b9c96af6SQingqing Zhuo 			REG_UPDATE_4(OTG_CRC_CNTL,
221b9c96af6SQingqing Zhuo 					OTG_CRC_CONT_EN, params->continuous_mode ? 1 : 0,
222b9c96af6SQingqing Zhuo 					OTG_CRC0_SELECT, params->selection,
223b9c96af6SQingqing Zhuo 					OTG_CRC_EN, 1,
224b9c96af6SQingqing Zhuo 					OTG_CRC_WINDOW_DB_EN, 1);
225b6fcc386SWayne Lin 		else
226b9c96af6SQingqing Zhuo 			REG_UPDATE_3(OTG_CRC_CNTL,
227b9c96af6SQingqing Zhuo 					OTG_CRC_CONT_EN, params->continuous_mode ? 1 : 0,
228b9c96af6SQingqing Zhuo 					OTG_CRC0_SELECT, params->selection,
229b9c96af6SQingqing Zhuo 					OTG_CRC_EN, 1);
230b6fcc386SWayne Lin 		break;
231b6fcc386SWayne Lin 	case 1:
232b6fcc386SWayne Lin 		/* Window A x axis start and end. */
233b6fcc386SWayne Lin 		REG_UPDATE_2(OTG_CRC1_WINDOWA_X_CONTROL,
234b6fcc386SWayne Lin 				OTG_CRC1_WINDOWA_X_START, params->windowa_x_start,
235b6fcc386SWayne Lin 				OTG_CRC1_WINDOWA_X_END, params->windowa_x_end);
236b6fcc386SWayne Lin 
237b6fcc386SWayne Lin 		/* Window A y axis start and end. */
238b6fcc386SWayne Lin 		REG_UPDATE_2(OTG_CRC1_WINDOWA_Y_CONTROL,
239b6fcc386SWayne Lin 				OTG_CRC1_WINDOWA_Y_START, params->windowa_y_start,
240b6fcc386SWayne Lin 				OTG_CRC1_WINDOWA_Y_END, params->windowa_y_end);
241b6fcc386SWayne Lin 
242b6fcc386SWayne Lin 		/* Window B x axis start and end. */
243b6fcc386SWayne Lin 		REG_UPDATE_2(OTG_CRC1_WINDOWB_X_CONTROL,
244b6fcc386SWayne Lin 				OTG_CRC1_WINDOWB_X_START, params->windowb_x_start,
245b6fcc386SWayne Lin 				OTG_CRC1_WINDOWB_X_END, params->windowb_x_end);
246b6fcc386SWayne Lin 
247b6fcc386SWayne Lin 		/* Window B y axis start and end. */
248b6fcc386SWayne Lin 		REG_UPDATE_2(OTG_CRC1_WINDOWB_Y_CONTROL,
249b6fcc386SWayne Lin 				OTG_CRC1_WINDOWB_Y_START, params->windowb_y_start,
250b6fcc386SWayne Lin 				OTG_CRC1_WINDOWB_Y_END, params->windowb_y_end);
251b6fcc386SWayne Lin 
252b6fcc386SWayne Lin 		if (optc1->base.ctx->dc->debug.otg_crc_db && optc1->tg_mask->OTG_CRC_WINDOW_DB_EN != 0)
253b6fcc386SWayne Lin 			REG_UPDATE_4(OTG_CRC_CNTL,
254b6fcc386SWayne Lin 					OTG_CRC_CONT_EN, params->continuous_mode ? 1 : 0,
255b6fcc386SWayne Lin 					OTG_CRC1_SELECT, params->selection,
256b6fcc386SWayne Lin 					OTG_CRC_EN, 1,
257b6fcc386SWayne Lin 					OTG_CRC_WINDOW_DB_EN, 1);
258b6fcc386SWayne Lin 		else
259b6fcc386SWayne Lin 			REG_UPDATE_3(OTG_CRC_CNTL,
260b6fcc386SWayne Lin 					OTG_CRC_CONT_EN, params->continuous_mode ? 1 : 0,
261b6fcc386SWayne Lin 					OTG_CRC1_SELECT, params->selection,
262b6fcc386SWayne Lin 					OTG_CRC_EN, 1);
263b6fcc386SWayne Lin 		break;
264b6fcc386SWayne Lin 	default:
265b6fcc386SWayne Lin 		return false;
266b6fcc386SWayne Lin 	}
267b9c96af6SQingqing Zhuo 	return true;
268b9c96af6SQingqing Zhuo }
269b9c96af6SQingqing Zhuo 
optc35_setup_manual_trigger(struct timing_generator * optc)270eed4eddaSRobin Chen static void optc35_setup_manual_trigger(struct timing_generator *optc)
271eed4eddaSRobin Chen {
272eed4eddaSRobin Chen 	if (!optc || !optc->ctx)
273eed4eddaSRobin Chen 		return;
274eed4eddaSRobin Chen 
275eed4eddaSRobin Chen 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
276eed4eddaSRobin Chen 	struct dc *dc = optc->ctx->dc;
277eed4eddaSRobin Chen 
278eed4eddaSRobin Chen 	if (dc->caps.dmub_caps.mclk_sw && !dc->debug.disable_fams)
279eed4eddaSRobin Chen 		dc_dmub_srv_set_drr_manual_trigger_cmd(dc, optc->inst);
280eed4eddaSRobin Chen 	else {
281eed4eddaSRobin Chen 		/*
282eed4eddaSRobin Chen 		 * MIN_MASK_EN is gone and MASK is now always enabled.
283eed4eddaSRobin Chen 		 *
284eed4eddaSRobin Chen 		 * To get it to it work with manual trigger we need to make sure
285eed4eddaSRobin Chen 		 * we program the correct bit.
286eed4eddaSRobin Chen 		 */
287eed4eddaSRobin Chen 		REG_UPDATE_4(OTG_V_TOTAL_CONTROL,
288eed4eddaSRobin Chen 				OTG_V_TOTAL_MIN_SEL, 1,
289eed4eddaSRobin Chen 				OTG_V_TOTAL_MAX_SEL, 1,
290eed4eddaSRobin Chen 				OTG_FORCE_LOCK_ON_EVENT, 0,
291eed4eddaSRobin Chen 				OTG_SET_V_TOTAL_MIN_MASK, (1 << 1)); /* TRIGA */
292eed4eddaSRobin Chen 
293eed4eddaSRobin Chen 		// Setup manual flow control for EOF via TRIG_A
294eed4eddaSRobin Chen 		if (optc->funcs && optc->funcs->setup_manual_trigger)
295eed4eddaSRobin Chen 			optc->funcs->setup_manual_trigger(optc);
296eed4eddaSRobin Chen 	}
297eed4eddaSRobin Chen }
298eed4eddaSRobin Chen 
optc35_set_drr(struct timing_generator * optc,const struct drr_params * params)299eed4eddaSRobin Chen void optc35_set_drr(
300eed4eddaSRobin Chen 	struct timing_generator *optc,
301eed4eddaSRobin Chen 	const struct drr_params *params)
302eed4eddaSRobin Chen {
303eed4eddaSRobin Chen 	if (!optc || !params)
304eed4eddaSRobin Chen 		return;
305eed4eddaSRobin Chen 
306eed4eddaSRobin Chen 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
307eed4eddaSRobin Chen 	uint32_t max_otg_v_total = optc1->max_v_total - 1;
308eed4eddaSRobin Chen 
309eed4eddaSRobin Chen 	if (params != NULL &&
310eed4eddaSRobin Chen 		params->vertical_total_max > 0 &&
311eed4eddaSRobin Chen 		params->vertical_total_min > 0) {
312eed4eddaSRobin Chen 
313eed4eddaSRobin Chen 		if (params->vertical_total_mid != 0) {
314eed4eddaSRobin Chen 
315eed4eddaSRobin Chen 			REG_SET(OTG_V_TOTAL_MID, 0,
316eed4eddaSRobin Chen 				OTG_V_TOTAL_MID, params->vertical_total_mid - 1);
317eed4eddaSRobin Chen 
318eed4eddaSRobin Chen 			REG_UPDATE_2(OTG_V_TOTAL_CONTROL,
319eed4eddaSRobin Chen 					OTG_VTOTAL_MID_REPLACING_MAX_EN, 1,
320eed4eddaSRobin Chen 					OTG_VTOTAL_MID_FRAME_NUM,
321eed4eddaSRobin Chen 					(uint8_t)params->vertical_total_mid_frame_num);
322eed4eddaSRobin Chen 
323eed4eddaSRobin Chen 		}
324eed4eddaSRobin Chen 
325eed4eddaSRobin Chen 		if (optc->funcs && optc->funcs->set_vtotal_min_max)
326eed4eddaSRobin Chen 			optc->funcs->set_vtotal_min_max(optc,
327eed4eddaSRobin Chen 				params->vertical_total_min - 1, params->vertical_total_max - 1);
328eed4eddaSRobin Chen 		optc35_setup_manual_trigger(optc);
329eed4eddaSRobin Chen 	} else {
330eed4eddaSRobin Chen 		REG_UPDATE_4(OTG_V_TOTAL_CONTROL,
331eed4eddaSRobin Chen 				OTG_SET_V_TOTAL_MIN_MASK, 0,
332eed4eddaSRobin Chen 				OTG_V_TOTAL_MIN_SEL, 0,
333eed4eddaSRobin Chen 				OTG_V_TOTAL_MAX_SEL, 0,
334eed4eddaSRobin Chen 				OTG_FORCE_LOCK_ON_EVENT, 0);
335eed4eddaSRobin Chen 
336eed4eddaSRobin Chen 		if (optc->funcs && optc->funcs->set_vtotal_min_max)
337eed4eddaSRobin Chen 			optc->funcs->set_vtotal_min_max(optc, 0, 0);
338eed4eddaSRobin Chen 	}
339eed4eddaSRobin Chen 
340eed4eddaSRobin Chen 	REG_WRITE(OTG_V_COUNT_STOP_CONTROL, max_otg_v_total);
341eed4eddaSRobin Chen 	REG_WRITE(OTG_V_COUNT_STOP_CONTROL2, 0);
342eed4eddaSRobin Chen }
343eed4eddaSRobin Chen 
optc35_set_long_vtotal(struct timing_generator * optc,const struct long_vtotal_params * params)344eed4eddaSRobin Chen static void optc35_set_long_vtotal(
345eed4eddaSRobin Chen 	struct timing_generator *optc,
346eed4eddaSRobin Chen 	const struct long_vtotal_params *params)
347eed4eddaSRobin Chen {
348eed4eddaSRobin Chen 	if (!optc || !params)
349eed4eddaSRobin Chen 		return;
350eed4eddaSRobin Chen 
351eed4eddaSRobin Chen 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
352eed4eddaSRobin Chen 	uint32_t vcount_stop_timer = 0, vcount_stop = 0;
353eed4eddaSRobin Chen 	uint32_t max_otg_v_total = optc1->max_v_total - 1;
354eed4eddaSRobin Chen 
355eed4eddaSRobin Chen 	if (params->vertical_total_min <= max_otg_v_total && params->vertical_total_max <= max_otg_v_total)
356eed4eddaSRobin Chen 		return;
357eed4eddaSRobin Chen 
358eed4eddaSRobin Chen 	if (params->vertical_total_max == 0 || params->vertical_total_min == 0) {
359eed4eddaSRobin Chen 		REG_UPDATE_4(OTG_V_TOTAL_CONTROL,
360eed4eddaSRobin Chen 						OTG_SET_V_TOTAL_MIN_MASK, 0,
361eed4eddaSRobin Chen 						OTG_V_TOTAL_MIN_SEL, 0,
362eed4eddaSRobin Chen 						OTG_V_TOTAL_MAX_SEL, 0,
363eed4eddaSRobin Chen 						OTG_FORCE_LOCK_ON_EVENT, 0);
364eed4eddaSRobin Chen 
365eed4eddaSRobin Chen 		if (optc->funcs && optc->funcs->set_vtotal_min_max)
366eed4eddaSRobin Chen 			optc->funcs->set_vtotal_min_max(optc, 0, 0);
367eed4eddaSRobin Chen 	} else if (params->vertical_total_max == params->vertical_total_min) {
368eed4eddaSRobin Chen 		vcount_stop = params->vertical_blank_start;
369eed4eddaSRobin Chen 		vcount_stop_timer = params->vertical_total_max - max_otg_v_total;
370eed4eddaSRobin Chen 
371eed4eddaSRobin Chen 		REG_UPDATE_4(OTG_V_TOTAL_CONTROL,
372eed4eddaSRobin Chen 				OTG_V_TOTAL_MIN_SEL, 1,
373eed4eddaSRobin Chen 				OTG_V_TOTAL_MAX_SEL, 1,
374eed4eddaSRobin Chen 				OTG_FORCE_LOCK_ON_EVENT, 0,
375eed4eddaSRobin Chen 				OTG_SET_V_TOTAL_MIN_MASK, 0);
376eed4eddaSRobin Chen 
377eed4eddaSRobin Chen 		if (optc->funcs && optc->funcs->set_vtotal_min_max)
378eed4eddaSRobin Chen 			optc->funcs->set_vtotal_min_max(optc, max_otg_v_total, max_otg_v_total);
379eed4eddaSRobin Chen 
380eed4eddaSRobin Chen 		REG_WRITE(OTG_V_COUNT_STOP_CONTROL, vcount_stop);
381eed4eddaSRobin Chen 		REG_WRITE(OTG_V_COUNT_STOP_CONTROL2, vcount_stop_timer);
382eed4eddaSRobin Chen 	} else {
383eed4eddaSRobin Chen 		// Variable rate, keep DRR trigger mask
384eed4eddaSRobin Chen 		if (params->vertical_total_min > max_otg_v_total) {
385eed4eddaSRobin Chen 			// cannot be supported
386eed4eddaSRobin Chen 			// If MAX_OTG_V_COUNT < DRR trigger < v_total_min < v_total_max,
387eed4eddaSRobin Chen 			// DRR trigger will drop the vtotal counting directly to a new frame.
388eed4eddaSRobin Chen 			// But it should trigger between v_total_min and v_total_max.
389eed4eddaSRobin Chen 			ASSERT(0);
390eed4eddaSRobin Chen 
391eed4eddaSRobin Chen 			REG_UPDATE_4(OTG_V_TOTAL_CONTROL,
392eed4eddaSRobin Chen 				OTG_SET_V_TOTAL_MIN_MASK, 0,
393eed4eddaSRobin Chen 				OTG_V_TOTAL_MIN_SEL, 0,
394eed4eddaSRobin Chen 				OTG_V_TOTAL_MAX_SEL, 0,
395eed4eddaSRobin Chen 				OTG_FORCE_LOCK_ON_EVENT, 0);
396eed4eddaSRobin Chen 
397eed4eddaSRobin Chen 			if (optc->funcs && optc->funcs->set_vtotal_min_max)
398eed4eddaSRobin Chen 				optc->funcs->set_vtotal_min_max(optc, 0, 0);
399eed4eddaSRobin Chen 
400eed4eddaSRobin Chen 			REG_WRITE(OTG_V_COUNT_STOP_CONTROL, max_otg_v_total);
401eed4eddaSRobin Chen 			REG_WRITE(OTG_V_COUNT_STOP_CONTROL2, 0);
402eed4eddaSRobin Chen 		} else {
403eed4eddaSRobin Chen 			// For total_min <= MAX_OTG_V_COUNT and total_max > MAX_OTG_V_COUNT
404eed4eddaSRobin Chen 			vcount_stop = params->vertical_total_min;
405eed4eddaSRobin Chen 			vcount_stop_timer = params->vertical_total_max - max_otg_v_total;
406eed4eddaSRobin Chen 
407eed4eddaSRobin Chen 			// Example:
408eed4eddaSRobin Chen 			// params->vertical_total_min 1000
409eed4eddaSRobin Chen 			// params->vertical_total_max 2000
410eed4eddaSRobin Chen 			// MAX_OTG_V_COUNT_STOP = 1500
411eed4eddaSRobin Chen 			//
412eed4eddaSRobin Chen 			// If DRR event not happened,
413eed4eddaSRobin Chen 			//     time     0,1,2,3,4,...1000,1001,........,1500,1501,1502,     ...1999
414eed4eddaSRobin Chen 			//     vcount   0,1,2,3,4....1000...................,1001,1002,1003,...1399
415eed4eddaSRobin Chen 			//     vcount2                       0,1,2,3,4,..499,
416eed4eddaSRobin Chen 			// else (DRR event happened, ex : at line 1004)
417eed4eddaSRobin Chen 			//     time    0,1,2,3,4,...1000,1001.....1004, 0
418eed4eddaSRobin Chen 			//     vcount  0,1,2,3,4....1000,.............. 0 (new frame)
419eed4eddaSRobin Chen 			//     vcount2                      0,1,2,   3, -
420eed4eddaSRobin Chen 			if (optc->funcs && optc->funcs->set_vtotal_min_max)
421eed4eddaSRobin Chen 				optc->funcs->set_vtotal_min_max(optc,
422eed4eddaSRobin Chen 					params->vertical_total_min - 1, max_otg_v_total);
423eed4eddaSRobin Chen 			optc35_setup_manual_trigger(optc);
424eed4eddaSRobin Chen 
425eed4eddaSRobin Chen 			REG_WRITE(OTG_V_COUNT_STOP_CONTROL, vcount_stop);
426eed4eddaSRobin Chen 			REG_WRITE(OTG_V_COUNT_STOP_CONTROL2, vcount_stop_timer);
427eed4eddaSRobin Chen 		}
428eed4eddaSRobin Chen 	}
429eed4eddaSRobin Chen }
430eed4eddaSRobin Chen 
431*347efe5bSChristophe JAILLET static const struct timing_generator_funcs dcn35_tg_funcs = {
432b9c96af6SQingqing Zhuo 		.validate_timing = optc1_validate_timing,
433b9c96af6SQingqing Zhuo 		.program_timing = optc1_program_timing,
434b9c96af6SQingqing Zhuo 		.setup_vertical_interrupt0 = optc1_setup_vertical_interrupt0,
435b9c96af6SQingqing Zhuo 		.setup_vertical_interrupt1 = optc1_setup_vertical_interrupt1,
436b9c96af6SQingqing Zhuo 		.setup_vertical_interrupt2 = optc1_setup_vertical_interrupt2,
437b9c96af6SQingqing Zhuo 		.program_global_sync = optc1_program_global_sync,
438b9c96af6SQingqing Zhuo 		.enable_crtc = optc35_enable_crtc,
439b9c96af6SQingqing Zhuo 		.disable_crtc = optc35_disable_crtc,
440b9c96af6SQingqing Zhuo 		.immediate_disable_crtc = optc31_immediate_disable_crtc,
441b9c96af6SQingqing Zhuo 		.phantom_crtc_post_enable = optc35_phantom_crtc_post_enable,
442b9c96af6SQingqing Zhuo 		/* used by enable_timing_synchronization. Not need for FPGA */
443b9c96af6SQingqing Zhuo 		.is_counter_moving = optc1_is_counter_moving,
444b9c96af6SQingqing Zhuo 		.get_position = optc1_get_position,
445b9c96af6SQingqing Zhuo 		.get_frame_count = optc1_get_vblank_counter,
446b9c96af6SQingqing Zhuo 		.get_scanoutpos = optc1_get_crtc_scanoutpos,
447b9c96af6SQingqing Zhuo 		.get_otg_active_size = optc1_get_otg_active_size,
448b9c96af6SQingqing Zhuo 		.set_early_control = optc1_set_early_control,
449b9c96af6SQingqing Zhuo 		/* used by enable_timing_synchronization. Not need for FPGA */
450b9c96af6SQingqing Zhuo 		.wait_for_state = optc1_wait_for_state,
451b9c96af6SQingqing Zhuo 		.set_blank_color = optc3_program_blank_color,
452b9c96af6SQingqing Zhuo 		.did_triggered_reset_occur = optc1_did_triggered_reset_occur,
453b9c96af6SQingqing Zhuo 		.triplebuffer_lock = optc3_triplebuffer_lock,
454b9c96af6SQingqing Zhuo 		.triplebuffer_unlock = optc2_triplebuffer_unlock,
455b9c96af6SQingqing Zhuo 		.enable_reset_trigger = optc1_enable_reset_trigger,
456b9c96af6SQingqing Zhuo 		.enable_crtc_reset = optc1_enable_crtc_reset,
457b9c96af6SQingqing Zhuo 		.disable_reset_trigger = optc1_disable_reset_trigger,
458b9c96af6SQingqing Zhuo 		.lock = optc3_lock,
459b9c96af6SQingqing Zhuo 		.unlock = optc1_unlock,
460b9c96af6SQingqing Zhuo 		.lock_doublebuffer_enable = optc3_lock_doublebuffer_enable,
461b9c96af6SQingqing Zhuo 		.lock_doublebuffer_disable = optc3_lock_doublebuffer_disable,
462b9c96af6SQingqing Zhuo 		.enable_optc_clock = optc1_enable_optc_clock,
463eed4eddaSRobin Chen 		.set_drr = optc35_set_drr,
464b9c96af6SQingqing Zhuo 		.get_last_used_drr_vtotal = optc2_get_last_used_drr_vtotal,
465b9c96af6SQingqing Zhuo 		.set_vtotal_min_max = optc1_set_vtotal_min_max,
466b9c96af6SQingqing Zhuo 		.set_static_screen_control = optc1_set_static_screen_control,
467b9c96af6SQingqing Zhuo 		.program_stereo = optc1_program_stereo,
468b9c96af6SQingqing Zhuo 		.is_stereo_left_eye = optc1_is_stereo_left_eye,
469b9c96af6SQingqing Zhuo 		.tg_init = optc3_tg_init,
470b9c96af6SQingqing Zhuo 		.is_tg_enabled = optc1_is_tg_enabled,
471b9c96af6SQingqing Zhuo 		.is_optc_underflow_occurred = optc1_is_optc_underflow_occurred,
472b9c96af6SQingqing Zhuo 		.clear_optc_underflow = optc1_clear_optc_underflow,
473b9c96af6SQingqing Zhuo 		.setup_global_swap_lock = NULL,
474b9c96af6SQingqing Zhuo 		.get_crc = optc1_get_crc,
475b9c96af6SQingqing Zhuo 		.configure_crc = optc35_configure_crc,
476b9c96af6SQingqing Zhuo 		.set_dsc_config = optc3_set_dsc_config,
477b9c96af6SQingqing Zhuo 		.get_dsc_status = optc2_get_dsc_status,
478b9c96af6SQingqing Zhuo 		.set_dwb_source = NULL,
4793d457727SOvidiu Bunea 		.set_odm_bypass = optc32_set_odm_bypass,
480b9c96af6SQingqing Zhuo 		.set_odm_combine = optc35_set_odm_combine,
481b9c96af6SQingqing Zhuo 		.get_optc_source = optc2_get_optc_source,
482b9c96af6SQingqing Zhuo 		.set_h_timing_div_manual_mode = optc32_set_h_timing_div_manual_mode,
483b9c96af6SQingqing Zhuo 		.set_out_mux = optc3_set_out_mux,
484b9c96af6SQingqing Zhuo 		.set_drr_trigger_window = optc3_set_drr_trigger_window,
485b9c96af6SQingqing Zhuo 		.set_vtotal_change_limit = optc3_set_vtotal_change_limit,
486b9c96af6SQingqing Zhuo 		.set_gsl = optc2_set_gsl,
487b9c96af6SQingqing Zhuo 		.set_gsl_source_select = optc2_set_gsl_source_select,
488b9c96af6SQingqing Zhuo 		.set_vtg_params = optc1_set_vtg_params,
489b9c96af6SQingqing Zhuo 		.program_manual_trigger = optc2_program_manual_trigger,
490b9c96af6SQingqing Zhuo 		.setup_manual_trigger = optc2_setup_manual_trigger,
491b9c96af6SQingqing Zhuo 		.get_hw_timing = optc1_get_hw_timing,
492b9c96af6SQingqing Zhuo 		.init_odm = optc3_init_odm,
493eed4eddaSRobin Chen 		.set_long_vtotal = optc35_set_long_vtotal,
494e6a901a0SWenjing Liu 		.is_two_pixels_per_container = optc1_is_two_pixels_per_container,
49506b0a4adSJosip Pavic 		.read_otg_state = optc31_read_otg_state,
496b9c96af6SQingqing Zhuo };
497b9c96af6SQingqing Zhuo 
dcn35_timing_generator_init(struct optc * optc1)498b9c96af6SQingqing Zhuo void dcn35_timing_generator_init(struct optc *optc1)
499b9c96af6SQingqing Zhuo {
500b9c96af6SQingqing Zhuo 	optc1->base.funcs = &dcn35_tg_funcs;
501b9c96af6SQingqing Zhuo 
502b9c96af6SQingqing Zhuo 	optc1->max_h_total = optc1->tg_mask->OTG_H_TOTAL + 1;
503b9c96af6SQingqing Zhuo 	optc1->max_v_total = optc1->tg_mask->OTG_V_TOTAL + 1;
504b9c96af6SQingqing Zhuo 
505b9c96af6SQingqing Zhuo 	optc1->min_h_blank = 32;
506b9c96af6SQingqing Zhuo 	optc1->min_v_blank = 3;
507b9c96af6SQingqing Zhuo 	optc1->min_v_blank_interlace = 5;
508b9c96af6SQingqing Zhuo 	optc1->min_h_sync_width = 4;
509b9c96af6SQingqing Zhuo 	optc1->min_v_sync_width = 1;
510556db637SAusef Yousof 	optc1->max_frame_count = 0xFFFFFF;
511b9c96af6SQingqing Zhuo 
512b9c96af6SQingqing Zhuo 	dcn35_timing_generator_set_fgcg(
513b9c96af6SQingqing Zhuo 		optc1, CTX->dc->debug.enable_fine_grain_clock_gating.bits.optc);
514b9c96af6SQingqing Zhuo }
515b9c96af6SQingqing Zhuo 
dcn35_timing_generator_set_fgcg(struct optc * optc1,bool enable)516b9c96af6SQingqing Zhuo void dcn35_timing_generator_set_fgcg(struct optc *optc1, bool enable)
517b9c96af6SQingqing Zhuo {
518b9c96af6SQingqing Zhuo 	REG_UPDATE(OPTC_CLOCK_CONTROL, OPTC_FGCG_REP_DIS, !enable);
519b9c96af6SQingqing Zhuo }
520