1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/delay.h>
4 #include <linux/firmware.h>
5 #include <linux/module.h>
7 #include <drm/drm_atomic_state_helper.h>
8 #include <drm/drm_edid.h>
9 #include <drm/drm_modeset_helper_vtables.h>
10 #include <drm/drm_probe_helper.h>
14 MODULE_FIRMWARE("ast_dp501_fw.bin");
16 static void ast_release_firmware(void *data)
18 struct ast_device *ast = data;
20 release_firmware(ast->dp501_fw);
24 static int ast_load_dp501_microcode(struct drm_device *dev)
26 struct ast_device *ast = to_ast_device(dev);
29 ret = request_firmware(&ast->dp501_fw, "ast_dp501_fw.bin", dev->dev);
33 return devm_add_action_or_reset(dev->dev, ast_release_firmware, ast);
36 static void send_ack(struct ast_device *ast)
39 sendack = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0x9b, 0xff);
41 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9b, 0x00, sendack);
44 static void send_nack(struct ast_device *ast)
47 sendack = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0x9b, 0xff);
49 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9b, 0x00, sendack);
52 static bool wait_ack(struct ast_device *ast)
57 waitack = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd2, 0xff);
60 } while ((!waitack) && (retry++ < 1000));
68 static bool wait_nack(struct ast_device *ast)
73 waitack = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd2, 0xff);
76 } while ((waitack) && (retry++ < 1000));
84 static void set_cmd_trigger(struct ast_device *ast)
86 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9b, ~0x40, 0x40);
89 static void clear_cmd_trigger(struct ast_device *ast)
91 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9b, ~0x40, 0x00);
95 static bool wait_fw_ready(struct ast_device *ast)
100 waitready = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd2, 0xff);
103 } while ((!waitready) && (retry++ < 1000));
112 static bool ast_write_cmd(struct drm_device *dev, u8 data)
114 struct ast_device *ast = to_ast_device(dev);
116 if (wait_nack(ast)) {
118 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9a, 0x00, data);
120 set_cmd_trigger(ast);
123 clear_cmd_trigger(ast);
127 } while (retry++ < 100);
129 clear_cmd_trigger(ast);
134 static bool ast_write_data(struct drm_device *dev, u8 data)
136 struct ast_device *ast = to_ast_device(dev);
138 if (wait_nack(ast)) {
140 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9a, 0x00, data);
152 static bool ast_read_data(struct drm_device *dev, u8 *data)
154 struct ast_device *ast = to_ast_device(dev);
159 if (wait_ack(ast) == false)
161 tmp = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd3, 0xff);
163 if (wait_nack(ast) == false) {
171 static void clear_cmd(struct ast_device *ast)
174 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9a, 0x00, 0x00);
178 static void ast_set_dp501_video_output(struct drm_device *dev, u8 mode)
180 ast_write_cmd(dev, 0x40);
181 ast_write_data(dev, mode);
186 static u32 get_fw_base(struct ast_device *ast)
188 return ast_mindwm(ast, 0x1e6e2104) & 0x7fffffff;
191 bool ast_backup_fw(struct drm_device *dev, u8 *addr, u32 size)
193 struct ast_device *ast = to_ast_device(dev);
197 if (ast->config_mode != ast_use_p2a)
200 data = ast_mindwm(ast, 0x1e6e2100) & 0x01;
202 boot_address = get_fw_base(ast);
203 for (i = 0; i < size; i += 4)
204 *(u32 *)(addr + i) = ast_mindwm(ast, boot_address + i);
210 static bool ast_launch_m68k(struct drm_device *dev)
212 struct ast_device *ast = to_ast_device(dev);
213 u32 i, data, len = 0;
218 if (ast->config_mode != ast_use_p2a)
221 data = ast_mindwm(ast, 0x1e6e2100) & 0x01;
224 if (ast->dp501_fw_addr) {
225 fw_addr = ast->dp501_fw_addr;
228 if (!ast->dp501_fw &&
229 ast_load_dp501_microcode(dev) < 0)
232 fw_addr = (u8 *)ast->dp501_fw->data;
233 len = ast->dp501_fw->size;
235 /* Get BootAddress */
236 ast_moutdwm(ast, 0x1e6e2000, 0x1688a8a8);
237 data = ast_mindwm(ast, 0x1e6e0004);
238 switch (data & 0x03) {
240 boot_address = 0x44000000;
244 boot_address = 0x48000000;
247 boot_address = 0x50000000;
250 boot_address = 0x60000000;
253 boot_address -= 0x200000; /* -2MB */
255 /* copy image to buffer */
256 for (i = 0; i < len; i += 4) {
257 data = *(u32 *)(fw_addr + i);
258 ast_moutdwm(ast, boot_address + i, data);
262 ast_moutdwm(ast, 0x1e6e2000, 0x1688a8a8);
265 ast_moutdwm(ast, 0x1e6e2104, 0x80000000 + boot_address);
266 ast_moutdwm(ast, 0x1e6e2100, 1);
269 data = ast_mindwm(ast, 0x1e6e2040) & 0xfffff1ff; /* D[11:9] = 100b: UEFI handling */
271 ast_moutdwm(ast, 0x1e6e2040, data);
273 jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0x99, 0xfc); /* D[1:0]: Reserved Video Buffer */
275 ast_set_index_reg(ast, AST_IO_VGACRI, 0x99, jreg);
280 static bool ast_dp501_is_connected(struct ast_device *ast)
282 u32 boot_address, offset, data;
284 if (ast->config_mode == ast_use_p2a) {
285 boot_address = get_fw_base(ast);
287 /* validate FW version */
288 offset = AST_DP501_GBL_VERSION;
289 data = ast_mindwm(ast, boot_address + offset);
290 if ((data & AST_DP501_FW_VERSION_MASK) != AST_DP501_FW_VERSION_1)
293 /* validate PnP Monitor */
294 offset = AST_DP501_PNPMONITOR;
295 data = ast_mindwm(ast, boot_address + offset);
296 if (!(data & AST_DP501_PNP_CONNECTED))
299 if (!ast->dp501_fw_buf)
304 data = readl(ast->dp501_fw_buf + offset);
306 /* validate FW version */
307 offset = AST_DP501_GBL_VERSION;
308 data = readl(ast->dp501_fw_buf + offset);
309 if ((data & AST_DP501_FW_VERSION_MASK) != AST_DP501_FW_VERSION_1)
312 /* validate PnP Monitor */
313 offset = AST_DP501_PNPMONITOR;
314 data = readl(ast->dp501_fw_buf + offset);
315 if (!(data & AST_DP501_PNP_CONNECTED))
321 static int ast_dp512_read_edid_block(void *data, u8 *buf, unsigned int block, size_t len)
323 struct ast_device *ast = data;
324 size_t rdlen = round_up(len, 4);
325 u32 i, boot_address, offset, ediddata;
327 if (block > (512 / EDID_LENGTH))
330 offset = AST_DP501_EDID_DATA + block * EDID_LENGTH;
332 if (ast->config_mode == ast_use_p2a) {
333 boot_address = get_fw_base(ast);
335 for (i = 0; i < rdlen; i += 4) {
336 ediddata = ast_mindwm(ast, boot_address + offset + i);
337 memcpy(buf, &ediddata, min((len - i), 4));
341 for (i = 0; i < rdlen; i += 4) {
342 ediddata = readl(ast->dp501_fw_buf + offset + i);
343 memcpy(buf, &ediddata, min((len - i), 4));
351 static bool ast_init_dvo(struct drm_device *dev)
353 struct ast_device *ast = to_ast_device(dev);
356 ast_write32(ast, 0xf004, 0x1e6e0000);
357 ast_write32(ast, 0xf000, 0x1);
358 ast_write32(ast, 0x12000, 0x1688a8a8);
360 jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd0, 0xff);
361 if (!(jreg & 0x80)) {
362 /* Init SCU DVO Settings */
363 data = ast_read32(ast, 0x12008);
367 ast_write32(ast, 0x12008, data);
369 if (IS_AST_GEN4(ast)) {
370 data = ast_read32(ast, 0x12084);
371 /* multi-pins for DVO single-edge */
373 ast_write32(ast, 0x12084, data);
375 data = ast_read32(ast, 0x12088);
376 /* multi-pins for DVO single-edge */
378 ast_write32(ast, 0x12088, data);
380 data = ast_read32(ast, 0x12090);
381 /* multi-pins for DVO single-edge */
384 ast_write32(ast, 0x12090, data);
385 } else { /* AST GEN5+ */
386 data = ast_read32(ast, 0x12088);
387 /* multi-pins for DVO single-edge */
389 ast_write32(ast, 0x12088, data);
391 data = ast_read32(ast, 0x1208c);
392 /* multi-pins for DVO single-edge */
394 ast_write32(ast, 0x1208c, data);
396 data = ast_read32(ast, 0x120a4);
397 /* multi-pins for DVO single-edge */
399 ast_write32(ast, 0x120a4, data);
401 data = ast_read32(ast, 0x120a8);
402 /* multi-pins for DVO single-edge */
404 ast_write32(ast, 0x120a8, data);
406 data = ast_read32(ast, 0x12094);
407 /* multi-pins for DVO single-edge */
409 ast_write32(ast, 0x12094, data);
414 data = ast_read32(ast, 0x1202c);
416 ast_write32(ast, 0x1202c, data);
418 /* Init VGA DVO Settings */
419 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa3, 0xcf, 0x80);
424 static void ast_init_analog(struct drm_device *dev)
426 struct ast_device *ast = to_ast_device(dev);
430 * Set DAC source to VGA mode in SCU2C via the P2A
431 * bridge. First configure the P2U to target the SCU
432 * in case it isn't at this stage.
434 ast_write32(ast, 0xf004, 0x1e6e0000);
435 ast_write32(ast, 0xf000, 0x1);
437 /* Then unlock the SCU with the magic password */
438 ast_write32(ast, 0x12000, 0x1688a8a8);
439 ast_write32(ast, 0x12000, 0x1688a8a8);
440 ast_write32(ast, 0x12000, 0x1688a8a8);
442 /* Finally, clear bits [17:16] of SCU2c */
443 data = ast_read32(ast, 0x1202c);
445 ast_write32(ast, 0, data);
448 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa3, 0xcf, 0x00);
451 void ast_init_3rdtx(struct drm_device *dev)
453 struct ast_device *ast = to_ast_device(dev);
456 if (IS_AST_GEN4(ast) || IS_AST_GEN5(ast)) {
457 jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd1, 0xff);
458 switch (jreg & 0x0e) {
463 ast_launch_m68k(dev);
469 if (ast->tx_chip_types & BIT(AST_TX_SIL164))
472 ast_init_analog(dev);
481 static const struct drm_encoder_funcs ast_dp501_encoder_funcs = {
482 .destroy = drm_encoder_cleanup,
485 static void ast_dp501_encoder_helper_atomic_enable(struct drm_encoder *encoder,
486 struct drm_atomic_state *state)
488 struct drm_device *dev = encoder->dev;
490 ast_set_dp501_video_output(dev, 1);
493 static void ast_dp501_encoder_helper_atomic_disable(struct drm_encoder *encoder,
494 struct drm_atomic_state *state)
496 struct drm_device *dev = encoder->dev;
498 ast_set_dp501_video_output(dev, 0);
501 static const struct drm_encoder_helper_funcs ast_dp501_encoder_helper_funcs = {
502 .atomic_enable = ast_dp501_encoder_helper_atomic_enable,
503 .atomic_disable = ast_dp501_encoder_helper_atomic_disable,
510 static int ast_dp501_connector_helper_get_modes(struct drm_connector *connector)
512 struct ast_connector *ast_connector = to_ast_connector(connector);
515 if (ast_connector->physical_status == connector_status_connected) {
516 struct ast_device *ast = to_ast_device(connector->dev);
517 const struct drm_edid *drm_edid;
519 drm_edid = drm_edid_read_custom(connector, ast_dp512_read_edid_block, ast);
520 drm_edid_connector_update(connector, drm_edid);
521 count = drm_edid_connector_add_modes(connector);
522 drm_edid_free(drm_edid);
524 drm_edid_connector_update(connector, NULL);
527 * There's no EDID data without a connected monitor. Set BMC-
528 * compatible modes in this case. The XGA default resolution
529 * should work well for all BMCs.
531 count = drm_add_modes_noedid(connector, 4096, 4096);
533 drm_set_preferred_mode(connector, 1024, 768);
539 static int ast_dp501_connector_helper_detect_ctx(struct drm_connector *connector,
540 struct drm_modeset_acquire_ctx *ctx,
543 struct ast_connector *ast_connector = to_ast_connector(connector);
544 struct ast_device *ast = to_ast_device(connector->dev);
545 enum drm_connector_status status = connector_status_disconnected;
547 if (ast_dp501_is_connected(ast))
548 status = connector_status_connected;
550 if (status != ast_connector->physical_status)
551 ++connector->epoch_counter;
552 ast_connector->physical_status = status;
554 return connector_status_connected;
557 static const struct drm_connector_helper_funcs ast_dp501_connector_helper_funcs = {
558 .get_modes = ast_dp501_connector_helper_get_modes,
559 .detect_ctx = ast_dp501_connector_helper_detect_ctx,
562 static const struct drm_connector_funcs ast_dp501_connector_funcs = {
563 .reset = drm_atomic_helper_connector_reset,
564 .fill_modes = drm_helper_probe_single_connector_modes,
565 .destroy = drm_connector_cleanup,
566 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
567 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
570 static int ast_dp501_connector_init(struct drm_device *dev, struct drm_connector *connector)
574 ret = drm_connector_init(dev, connector, &ast_dp501_connector_funcs,
575 DRM_MODE_CONNECTOR_DisplayPort);
579 drm_connector_helper_add(connector, &ast_dp501_connector_helper_funcs);
581 connector->interlace_allowed = 0;
582 connector->doublescan_allowed = 0;
584 connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
589 int ast_dp501_output_init(struct ast_device *ast)
591 struct drm_device *dev = &ast->base;
592 struct drm_crtc *crtc = &ast->crtc;
593 struct drm_encoder *encoder = &ast->output.dp501.encoder;
594 struct ast_connector *ast_connector = &ast->output.dp501.connector;
595 struct drm_connector *connector = &ast_connector->base;
598 ret = drm_encoder_init(dev, encoder, &ast_dp501_encoder_funcs,
599 DRM_MODE_ENCODER_TMDS, NULL);
602 drm_encoder_helper_add(encoder, &ast_dp501_encoder_helper_funcs);
604 encoder->possible_crtcs = drm_crtc_mask(crtc);
606 ret = ast_dp501_connector_init(dev, connector);
609 ast_connector->physical_status = connector->status;
611 ret = drm_connector_attach_encoder(connector, encoder);