1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2021, ASPEED Technology Inc.
3 // Authors: KuoHsiang Chou <[email protected]>
4 
5 #include <linux/firmware.h>
6 #include <linux/delay.h>
7 
8 #include <drm/drm_atomic_state_helper.h>
9 #include <drm/drm_edid.h>
10 #include <drm/drm_modeset_helper_vtables.h>
11 #include <drm/drm_print.h>
12 #include <drm/drm_probe_helper.h>
13 
14 #include "ast_drv.h"
15 
ast_astdp_is_connected(struct ast_device * ast)16 static bool ast_astdp_is_connected(struct ast_device *ast)
17 {
18 	if (!ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xDF, AST_IO_VGACRDF_HPD))
19 		return false;
20 	/*
21 	 * HPD might be set even if no monitor is connected, so also check that
22 	 * the link training was successful.
23 	 */
24 	if (!ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xDC, AST_IO_VGACRDC_LINK_SUCCESS))
25 		return false;
26 	return true;
27 }
28 
ast_astdp_read_edid_block(void * data,u8 * buf,unsigned int block,size_t len)29 static int ast_astdp_read_edid_block(void *data, u8 *buf, unsigned int block, size_t len)
30 {
31 	struct ast_device *ast = data;
32 	size_t rdlen = round_up(len, 4);
33 	int ret = 0;
34 	unsigned int i;
35 
36 	if (block > 0)
37 		return -EIO; /* extension headers not supported */
38 
39 	/*
40 	 * Protect access to I/O registers from concurrent modesetting
41 	 * by acquiring the I/O-register lock.
42 	 */
43 	mutex_lock(&ast->modeset_lock);
44 
45 	/* Start reading EDID data */
46 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xe5, (u8)~AST_IO_VGACRE5_EDID_READ_DONE, 0x00);
47 
48 	for (i = 0; i < rdlen; i += 4) {
49 		unsigned int offset;
50 		unsigned int j;
51 		u8 ediddata[4];
52 		u8 vgacre4;
53 
54 		offset = (i + block * EDID_LENGTH) / 4;
55 		if (offset >= 64) {
56 			ret = -EIO;
57 			goto out;
58 		}
59 		vgacre4 = offset;
60 
61 		/*
62 		 * CRE4[7:0]: Read-Pointer for EDID (Unit: 4bytes); valid range: 0~64
63 		 */
64 		ast_set_index_reg(ast, AST_IO_VGACRI, 0xe4, vgacre4);
65 
66 		/*
67 		 * CRD7[b0]: valid flag for EDID
68 		 * CRD6[b0]: mirror read pointer for EDID
69 		 */
70 		for (j = 0; j < 200; ++j) {
71 			u8 vgacrd7, vgacrd6;
72 
73 			/*
74 			 * Delay are getting longer with each retry.
75 			 *
76 			 * 1. No delay on first try
77 			 * 2. The Delays are often 2 loops when users request "Display Settings"
78 			 *	  of right-click of mouse.
79 			 * 3. The Delays are often longer a lot when system resume from S3/S4.
80 			 */
81 			if (j)
82 				mdelay(j + 1);
83 
84 			/* Wait for EDID offset to show up in mirror register */
85 			vgacrd7 = ast_get_index_reg(ast, AST_IO_VGACRI, 0xd7);
86 			if (vgacrd7 & AST_IO_VGACRD7_EDID_VALID_FLAG) {
87 				vgacrd6 = ast_get_index_reg(ast, AST_IO_VGACRI, 0xd6);
88 				if (vgacrd6 == offset)
89 					break;
90 			}
91 		}
92 		if (j == 200) {
93 			ret = -EBUSY;
94 			goto out;
95 		}
96 
97 		ediddata[0] = ast_get_index_reg(ast, AST_IO_VGACRI, 0xd8);
98 		ediddata[1] = ast_get_index_reg(ast, AST_IO_VGACRI, 0xd9);
99 		ediddata[2] = ast_get_index_reg(ast, AST_IO_VGACRI, 0xda);
100 		ediddata[3] = ast_get_index_reg(ast, AST_IO_VGACRI, 0xdb);
101 
102 		if (i == 31) {
103 			/*
104 			 * For 128-bytes EDID_1.3,
105 			 * 1. Add the value of Bytes-126 to Bytes-127.
106 			 *		The Bytes-127 is Checksum. Sum of all 128bytes should
107 			 *		equal 0	(mod 256).
108 			 * 2. Modify Bytes-126 to be 0.
109 			 *		The Bytes-126 indicates the Number of extensions to
110 			 *		follow. 0 represents noextensions.
111 			 */
112 			ediddata[3] = ediddata[3] + ediddata[2];
113 			ediddata[2] = 0;
114 		}
115 
116 		memcpy(buf, ediddata, min((len - i), 4));
117 		buf += 4;
118 	}
119 
120 out:
121 	/* Signal end of reading */
122 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xe5, (u8)~AST_IO_VGACRE5_EDID_READ_DONE,
123 			       AST_IO_VGACRE5_EDID_READ_DONE);
124 
125 	mutex_unlock(&ast->modeset_lock);
126 
127 	return ret;
128 }
129 
130 /*
131  * Launch Aspeed DP
132  */
ast_dp_launch(struct ast_device * ast)133 int ast_dp_launch(struct ast_device *ast)
134 {
135 	struct drm_device *dev = &ast->base;
136 	unsigned int i = 10;
137 
138 	while (i) {
139 		u8 vgacrd1 = ast_get_index_reg(ast, AST_IO_VGACRI, 0xd1);
140 
141 		if (vgacrd1 & AST_IO_VGACRD1_MCU_FW_EXECUTING)
142 			break;
143 		--i;
144 		msleep(100);
145 	}
146 	if (!i) {
147 		drm_err(dev, "Wait DPMCU executing timeout\n");
148 		return -ENODEV;
149 	}
150 
151 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xe5,
152 			       (u8) ~AST_IO_VGACRE5_EDID_READ_DONE,
153 			       AST_IO_VGACRE5_EDID_READ_DONE);
154 
155 	return 0;
156 }
157 
ast_dp_get_phy_sleep(struct ast_device * ast)158 static bool ast_dp_get_phy_sleep(struct ast_device *ast)
159 {
160 	u8 vgacre3 = ast_get_index_reg(ast, AST_IO_VGACRI, 0xe3);
161 
162 	return (vgacre3 & AST_IO_VGACRE3_DP_PHY_SLEEP);
163 }
164 
ast_dp_set_phy_sleep(struct ast_device * ast,bool sleep)165 static void ast_dp_set_phy_sleep(struct ast_device *ast, bool sleep)
166 {
167 	u8 vgacre3 = 0x00;
168 
169 	if (sleep)
170 		vgacre3 |= AST_IO_VGACRE3_DP_PHY_SLEEP;
171 
172 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xe3, (u8)~AST_IO_VGACRE3_DP_PHY_SLEEP,
173 			       vgacre3);
174 	msleep(50);
175 }
176 
ast_dp_link_training(struct ast_device * ast)177 static void ast_dp_link_training(struct ast_device *ast)
178 {
179 	struct drm_device *dev = &ast->base;
180 	int i;
181 
182 	for (i = 0; i < 10; i++) {
183 		u8 vgacrdc;
184 
185 		if (i)
186 			msleep(100);
187 
188 		vgacrdc = ast_get_index_reg(ast, AST_IO_VGACRI, 0xdc);
189 		if (vgacrdc & AST_IO_VGACRDC_LINK_SUCCESS)
190 			return;
191 	}
192 	drm_err(dev, "Link training failed\n");
193 }
194 
__ast_dp_wait_enable(struct ast_device * ast,bool enabled)195 static bool __ast_dp_wait_enable(struct ast_device *ast, bool enabled)
196 {
197 	u8 vgacrdf_test = 0x00;
198 	u8 vgacrdf;
199 	unsigned int i;
200 
201 	if (enabled)
202 		vgacrdf_test |= AST_IO_VGACRDF_DP_VIDEO_ENABLE;
203 
204 	for (i = 0; i < 1000; ++i) {
205 		if (i)
206 			mdelay(1);
207 		vgacrdf = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xdf,
208 						 AST_IO_VGACRDF_DP_VIDEO_ENABLE);
209 		if (vgacrdf == vgacrdf_test)
210 			return true;
211 	}
212 
213 	return false;
214 }
215 
ast_dp_set_enable(struct ast_device * ast,bool enabled)216 static void ast_dp_set_enable(struct ast_device *ast, bool enabled)
217 {
218 	struct drm_device *dev = &ast->base;
219 	u8 vgacre3 = 0x00;
220 
221 	if (enabled)
222 		vgacre3 |= AST_IO_VGACRE3_DP_VIDEO_ENABLE;
223 
224 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xe3, (u8)~AST_IO_VGACRE3_DP_VIDEO_ENABLE,
225 			       vgacre3);
226 
227 	drm_WARN_ON(dev, !__ast_dp_wait_enable(ast, enabled));
228 }
229 
ast_dp_set_mode(struct drm_crtc * crtc,struct ast_vbios_mode_info * vbios_mode)230 static void ast_dp_set_mode(struct drm_crtc *crtc, struct ast_vbios_mode_info *vbios_mode)
231 {
232 	struct ast_device *ast = to_ast_device(crtc->dev);
233 
234 	u32 ulRefreshRateIndex;
235 	u8 ModeIdx;
236 
237 	ulRefreshRateIndex = vbios_mode->enh_table->refresh_rate_index - 1;
238 
239 	switch (crtc->mode.crtc_hdisplay) {
240 	case 320:
241 		ModeIdx = ASTDP_320x240_60;
242 		break;
243 	case 400:
244 		ModeIdx = ASTDP_400x300_60;
245 		break;
246 	case 512:
247 		ModeIdx = ASTDP_512x384_60;
248 		break;
249 	case 640:
250 		ModeIdx = (ASTDP_640x480_60 + (u8) ulRefreshRateIndex);
251 		break;
252 	case 800:
253 		ModeIdx = (ASTDP_800x600_56 + (u8) ulRefreshRateIndex);
254 		break;
255 	case 1024:
256 		ModeIdx = (ASTDP_1024x768_60 + (u8) ulRefreshRateIndex);
257 		break;
258 	case 1152:
259 		ModeIdx = ASTDP_1152x864_75;
260 		break;
261 	case 1280:
262 		if (crtc->mode.crtc_vdisplay == 800)
263 			ModeIdx = (ASTDP_1280x800_60_RB - (u8) ulRefreshRateIndex);
264 		else		// 1024
265 			ModeIdx = (ASTDP_1280x1024_60 + (u8) ulRefreshRateIndex);
266 		break;
267 	case 1360:
268 	case 1366:
269 		ModeIdx = ASTDP_1366x768_60;
270 		break;
271 	case 1440:
272 		ModeIdx = (ASTDP_1440x900_60_RB - (u8) ulRefreshRateIndex);
273 		break;
274 	case 1600:
275 		if (crtc->mode.crtc_vdisplay == 900)
276 			ModeIdx = (ASTDP_1600x900_60_RB - (u8) ulRefreshRateIndex);
277 		else		//1200
278 			ModeIdx = ASTDP_1600x1200_60;
279 		break;
280 	case 1680:
281 		ModeIdx = (ASTDP_1680x1050_60_RB - (u8) ulRefreshRateIndex);
282 		break;
283 	case 1920:
284 		if (crtc->mode.crtc_vdisplay == 1080)
285 			ModeIdx = ASTDP_1920x1080_60;
286 		else		//1200
287 			ModeIdx = ASTDP_1920x1200_60;
288 		break;
289 	default:
290 		return;
291 	}
292 
293 	/*
294 	 * CRE0[7:0]: MISC0 ((0x00: 18-bpp) or (0x20: 24-bpp)
295 	 * CRE1[7:0]: MISC1 (default: 0x00)
296 	 * CRE2[7:0]: video format index (0x00 ~ 0x20 or 0x40 ~ 0x50)
297 	 */
298 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xE0, ASTDP_AND_CLEAR_MASK,
299 			       ASTDP_MISC0_24bpp);
300 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xE1, ASTDP_AND_CLEAR_MASK, ASTDP_MISC1);
301 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xE2, ASTDP_AND_CLEAR_MASK, ModeIdx);
302 }
303 
ast_wait_for_vretrace(struct ast_device * ast)304 static void ast_wait_for_vretrace(struct ast_device *ast)
305 {
306 	unsigned long timeout = jiffies + HZ;
307 	u8 vgair1;
308 
309 	do {
310 		vgair1 = ast_io_read8(ast, AST_IO_VGAIR1_R);
311 	} while (!(vgair1 & AST_IO_VGAIR1_VREFRESH) && time_before(jiffies, timeout));
312 }
313 
314 /*
315  * Encoder
316  */
317 
318 static const struct drm_encoder_funcs ast_astdp_encoder_funcs = {
319 	.destroy = drm_encoder_cleanup,
320 };
321 
ast_astdp_encoder_helper_atomic_mode_set(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)322 static void ast_astdp_encoder_helper_atomic_mode_set(struct drm_encoder *encoder,
323 						     struct drm_crtc_state *crtc_state,
324 						     struct drm_connector_state *conn_state)
325 {
326 	struct drm_crtc *crtc = crtc_state->crtc;
327 	struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
328 	struct ast_vbios_mode_info *vbios_mode_info = &ast_crtc_state->vbios_mode_info;
329 
330 	ast_dp_set_mode(crtc, vbios_mode_info);
331 }
332 
ast_astdp_encoder_helper_atomic_enable(struct drm_encoder * encoder,struct drm_atomic_state * state)333 static void ast_astdp_encoder_helper_atomic_enable(struct drm_encoder *encoder,
334 						   struct drm_atomic_state *state)
335 {
336 	struct ast_device *ast = to_ast_device(encoder->dev);
337 	struct ast_connector *ast_connector = &ast->output.astdp.connector;
338 
339 	if (ast_connector->physical_status == connector_status_connected) {
340 		ast_dp_set_phy_sleep(ast, false);
341 		ast_dp_link_training(ast);
342 
343 		ast_wait_for_vretrace(ast);
344 		ast_dp_set_enable(ast, true);
345 	}
346 }
347 
ast_astdp_encoder_helper_atomic_disable(struct drm_encoder * encoder,struct drm_atomic_state * state)348 static void ast_astdp_encoder_helper_atomic_disable(struct drm_encoder *encoder,
349 						    struct drm_atomic_state *state)
350 {
351 	struct ast_device *ast = to_ast_device(encoder->dev);
352 
353 	ast_dp_set_enable(ast, false);
354 	ast_dp_set_phy_sleep(ast, true);
355 }
356 
357 static const struct drm_encoder_helper_funcs ast_astdp_encoder_helper_funcs = {
358 	.atomic_mode_set = ast_astdp_encoder_helper_atomic_mode_set,
359 	.atomic_enable = ast_astdp_encoder_helper_atomic_enable,
360 	.atomic_disable = ast_astdp_encoder_helper_atomic_disable,
361 };
362 
363 /*
364  * Connector
365  */
366 
ast_astdp_connector_helper_get_modes(struct drm_connector * connector)367 static int ast_astdp_connector_helper_get_modes(struct drm_connector *connector)
368 {
369 	struct ast_connector *ast_connector = to_ast_connector(connector);
370 	int count;
371 
372 	if (ast_connector->physical_status == connector_status_connected) {
373 		struct ast_device *ast = to_ast_device(connector->dev);
374 		const struct drm_edid *drm_edid;
375 
376 		drm_edid = drm_edid_read_custom(connector, ast_astdp_read_edid_block, ast);
377 		drm_edid_connector_update(connector, drm_edid);
378 		count = drm_edid_connector_add_modes(connector);
379 		drm_edid_free(drm_edid);
380 	} else {
381 		drm_edid_connector_update(connector, NULL);
382 
383 		/*
384 		 * There's no EDID data without a connected monitor. Set BMC-
385 		 * compatible modes in this case. The XGA default resolution
386 		 * should work well for all BMCs.
387 		 */
388 		count = drm_add_modes_noedid(connector, 4096, 4096);
389 		if (count)
390 			drm_set_preferred_mode(connector, 1024, 768);
391 	}
392 
393 	return count;
394 }
395 
ast_astdp_connector_helper_detect_ctx(struct drm_connector * connector,struct drm_modeset_acquire_ctx * ctx,bool force)396 static int ast_astdp_connector_helper_detect_ctx(struct drm_connector *connector,
397 						 struct drm_modeset_acquire_ctx *ctx,
398 						 bool force)
399 {
400 	struct ast_connector *ast_connector = to_ast_connector(connector);
401 	struct ast_device *ast = to_ast_device(connector->dev);
402 	enum drm_connector_status status = connector_status_disconnected;
403 	bool phy_sleep;
404 
405 	mutex_lock(&ast->modeset_lock);
406 
407 	phy_sleep = ast_dp_get_phy_sleep(ast);
408 	if (phy_sleep)
409 		ast_dp_set_phy_sleep(ast, false);
410 
411 	if (ast_astdp_is_connected(ast))
412 		status = connector_status_connected;
413 
414 	if (phy_sleep && status == connector_status_disconnected)
415 		ast_dp_set_phy_sleep(ast, true);
416 
417 	mutex_unlock(&ast->modeset_lock);
418 
419 	if (status != ast_connector->physical_status)
420 		++connector->epoch_counter;
421 	ast_connector->physical_status = status;
422 
423 	return connector_status_connected;
424 }
425 
426 static const struct drm_connector_helper_funcs ast_astdp_connector_helper_funcs = {
427 	.get_modes = ast_astdp_connector_helper_get_modes,
428 	.detect_ctx = ast_astdp_connector_helper_detect_ctx,
429 };
430 
431 /*
432  * Output
433  */
434 
435 static const struct drm_connector_funcs ast_astdp_connector_funcs = {
436 	.reset = drm_atomic_helper_connector_reset,
437 	.fill_modes = drm_helper_probe_single_connector_modes,
438 	.destroy = drm_connector_cleanup,
439 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
440 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
441 };
442 
ast_astdp_output_init(struct ast_device * ast)443 int ast_astdp_output_init(struct ast_device *ast)
444 {
445 	struct drm_device *dev = &ast->base;
446 	struct drm_crtc *crtc = &ast->crtc;
447 	struct drm_encoder *encoder;
448 	struct ast_connector *ast_connector;
449 	struct drm_connector *connector;
450 	int ret;
451 
452 	/* encoder */
453 
454 	encoder = &ast->output.astdp.encoder;
455 	ret = drm_encoder_init(dev, encoder, &ast_astdp_encoder_funcs,
456 			       DRM_MODE_ENCODER_TMDS, NULL);
457 	if (ret)
458 		return ret;
459 	drm_encoder_helper_add(encoder, &ast_astdp_encoder_helper_funcs);
460 
461 	encoder->possible_crtcs = drm_crtc_mask(crtc);
462 
463 	/* connector */
464 
465 	ast_connector = &ast->output.astdp.connector;
466 	connector = &ast_connector->base;
467 	ret = drm_connector_init(dev, connector, &ast_astdp_connector_funcs,
468 				 DRM_MODE_CONNECTOR_DisplayPort);
469 	if (ret)
470 		return ret;
471 	drm_connector_helper_add(connector, &ast_astdp_connector_helper_funcs);
472 
473 	connector->interlace_allowed = 0;
474 	connector->doublescan_allowed = 0;
475 	connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
476 
477 	ast_connector->physical_status = connector->status;
478 
479 	ret = drm_connector_attach_encoder(connector, encoder);
480 	if (ret)
481 		return ret;
482 
483 	return 0;
484 }
485