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 ast_device *ast)
26 struct drm_device *dev = &ast->base;
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 ast_device *ast, u8 data)
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 ast_device *ast, u8 data)
136 if (wait_nack(ast)) {
138 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9a, 0x00, data);
150 static bool ast_read_data(struct drm_device *dev, u8 *data)
152 struct ast_device *ast = to_ast_device(dev);
157 if (wait_ack(ast) == false)
159 tmp = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd3, 0xff);
161 if (wait_nack(ast) == false) {
169 static void clear_cmd(struct ast_device *ast)
172 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9a, 0x00, 0x00);
176 static void ast_set_dp501_video_output(struct ast_device *ast, u8 mode)
178 ast_write_cmd(ast, 0x40);
179 ast_write_data(ast, mode);
184 static u32 get_fw_base(struct ast_device *ast)
186 return ast_mindwm(ast, 0x1e6e2104) & 0x7fffffff;
189 bool ast_backup_fw(struct ast_device *ast, u8 *addr, u32 size)
194 if (ast->config_mode != ast_use_p2a)
197 data = ast_mindwm(ast, 0x1e6e2100) & 0x01;
199 boot_address = get_fw_base(ast);
200 for (i = 0; i < size; i += 4)
201 *(u32 *)(addr + i) = ast_mindwm(ast, boot_address + i);
207 static bool ast_launch_m68k(struct ast_device *ast)
209 u32 i, data, len = 0;
214 if (ast->config_mode != ast_use_p2a)
217 data = ast_mindwm(ast, 0x1e6e2100) & 0x01;
220 if (ast->dp501_fw_addr) {
221 fw_addr = ast->dp501_fw_addr;
224 if (!ast->dp501_fw &&
225 ast_load_dp501_microcode(ast) < 0)
228 fw_addr = (u8 *)ast->dp501_fw->data;
229 len = ast->dp501_fw->size;
231 /* Get BootAddress */
232 ast_moutdwm(ast, 0x1e6e2000, 0x1688a8a8);
233 data = ast_mindwm(ast, 0x1e6e0004);
234 switch (data & 0x03) {
236 boot_address = 0x44000000;
240 boot_address = 0x48000000;
243 boot_address = 0x50000000;
246 boot_address = 0x60000000;
249 boot_address -= 0x200000; /* -2MB */
251 /* copy image to buffer */
252 for (i = 0; i < len; i += 4) {
253 data = *(u32 *)(fw_addr + i);
254 ast_moutdwm(ast, boot_address + i, data);
258 ast_moutdwm(ast, 0x1e6e2000, 0x1688a8a8);
261 ast_moutdwm(ast, 0x1e6e2104, 0x80000000 + boot_address);
262 ast_moutdwm(ast, 0x1e6e2100, 1);
265 data = ast_mindwm(ast, 0x1e6e2040) & 0xfffff1ff; /* D[11:9] = 100b: UEFI handling */
267 ast_moutdwm(ast, 0x1e6e2040, data);
269 jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0x99, 0xfc); /* D[1:0]: Reserved Video Buffer */
271 ast_set_index_reg(ast, AST_IO_VGACRI, 0x99, jreg);
276 static bool ast_dp501_is_connected(struct ast_device *ast)
278 u32 boot_address, offset, data;
280 if (ast->config_mode == ast_use_p2a) {
281 boot_address = get_fw_base(ast);
283 /* validate FW version */
284 offset = AST_DP501_GBL_VERSION;
285 data = ast_mindwm(ast, boot_address + offset);
286 if ((data & AST_DP501_FW_VERSION_MASK) != AST_DP501_FW_VERSION_1)
289 /* validate PnP Monitor */
290 offset = AST_DP501_PNPMONITOR;
291 data = ast_mindwm(ast, boot_address + offset);
292 if (!(data & AST_DP501_PNP_CONNECTED))
295 if (!ast->dp501_fw_buf)
300 data = readl(ast->dp501_fw_buf + offset);
302 /* validate FW version */
303 offset = AST_DP501_GBL_VERSION;
304 data = readl(ast->dp501_fw_buf + offset);
305 if ((data & AST_DP501_FW_VERSION_MASK) != AST_DP501_FW_VERSION_1)
308 /* validate PnP Monitor */
309 offset = AST_DP501_PNPMONITOR;
310 data = readl(ast->dp501_fw_buf + offset);
311 if (!(data & AST_DP501_PNP_CONNECTED))
317 static int ast_dp512_read_edid_block(void *data, u8 *buf, unsigned int block, size_t len)
319 struct ast_device *ast = data;
320 size_t rdlen = round_up(len, 4);
321 u32 i, boot_address, offset, ediddata;
323 if (block > (512 / EDID_LENGTH))
326 offset = AST_DP501_EDID_DATA + block * EDID_LENGTH;
328 if (ast->config_mode == ast_use_p2a) {
329 boot_address = get_fw_base(ast);
331 for (i = 0; i < rdlen; i += 4) {
332 ediddata = ast_mindwm(ast, boot_address + offset + i);
333 memcpy(buf, &ediddata, min((len - i), 4));
337 for (i = 0; i < rdlen; i += 4) {
338 ediddata = readl(ast->dp501_fw_buf + offset + i);
339 memcpy(buf, &ediddata, min((len - i), 4));
347 static bool ast_init_dvo(struct ast_device *ast)
351 ast_write32(ast, 0xf004, 0x1e6e0000);
352 ast_write32(ast, 0xf000, 0x1);
353 ast_write32(ast, 0x12000, 0x1688a8a8);
355 jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd0, 0xff);
356 if (!(jreg & 0x80)) {
357 /* Init SCU DVO Settings */
358 data = ast_read32(ast, 0x12008);
362 ast_write32(ast, 0x12008, data);
364 if (IS_AST_GEN4(ast)) {
365 data = ast_read32(ast, 0x12084);
366 /* multi-pins for DVO single-edge */
368 ast_write32(ast, 0x12084, data);
370 data = ast_read32(ast, 0x12088);
371 /* multi-pins for DVO single-edge */
373 ast_write32(ast, 0x12088, data);
375 data = ast_read32(ast, 0x12090);
376 /* multi-pins for DVO single-edge */
379 ast_write32(ast, 0x12090, data);
380 } else { /* AST GEN5+ */
381 data = ast_read32(ast, 0x12088);
382 /* multi-pins for DVO single-edge */
384 ast_write32(ast, 0x12088, data);
386 data = ast_read32(ast, 0x1208c);
387 /* multi-pins for DVO single-edge */
389 ast_write32(ast, 0x1208c, data);
391 data = ast_read32(ast, 0x120a4);
392 /* multi-pins for DVO single-edge */
394 ast_write32(ast, 0x120a4, data);
396 data = ast_read32(ast, 0x120a8);
397 /* multi-pins for DVO single-edge */
399 ast_write32(ast, 0x120a8, data);
401 data = ast_read32(ast, 0x12094);
402 /* multi-pins for DVO single-edge */
404 ast_write32(ast, 0x12094, data);
409 data = ast_read32(ast, 0x1202c);
411 ast_write32(ast, 0x1202c, data);
413 /* Init VGA DVO Settings */
414 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa3, 0xcf, 0x80);
419 static void ast_init_analog(struct ast_device *ast)
424 * Set DAC source to VGA mode in SCU2C via the P2A
425 * bridge. First configure the P2U to target the SCU
426 * in case it isn't at this stage.
428 ast_write32(ast, 0xf004, 0x1e6e0000);
429 ast_write32(ast, 0xf000, 0x1);
431 /* Then unlock the SCU with the magic password */
432 ast_write32(ast, 0x12000, 0x1688a8a8);
433 ast_write32(ast, 0x12000, 0x1688a8a8);
434 ast_write32(ast, 0x12000, 0x1688a8a8);
436 /* Finally, clear bits [17:16] of SCU2c */
437 data = ast_read32(ast, 0x1202c);
439 ast_write32(ast, 0, data);
442 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa3, 0xcf, 0x00);
445 void ast_init_3rdtx(struct ast_device *ast)
449 if (IS_AST_GEN4(ast) || IS_AST_GEN5(ast)) {
450 vgacrd1 = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd1,
451 AST_IO_VGACRD1_TX_TYPE_MASK);
453 case AST_IO_VGACRD1_TX_SIL164_VBIOS:
456 case AST_IO_VGACRD1_TX_DP501_VBIOS:
457 ast_launch_m68k(ast);
459 case AST_IO_VGACRD1_TX_FW_EMBEDDED_FW:
463 if (ast->tx_chip == AST_TX_SIL164)
466 ast_init_analog(ast);
475 static const struct drm_encoder_funcs ast_dp501_encoder_funcs = {
476 .destroy = drm_encoder_cleanup,
479 static void ast_dp501_encoder_helper_atomic_enable(struct drm_encoder *encoder,
480 struct drm_atomic_state *state)
482 struct ast_device *ast = to_ast_device(encoder->dev);
484 ast_set_dp501_video_output(ast, 1);
487 static void ast_dp501_encoder_helper_atomic_disable(struct drm_encoder *encoder,
488 struct drm_atomic_state *state)
490 struct ast_device *ast = to_ast_device(encoder->dev);
492 ast_set_dp501_video_output(ast, 0);
495 static const struct drm_encoder_helper_funcs ast_dp501_encoder_helper_funcs = {
496 .atomic_enable = ast_dp501_encoder_helper_atomic_enable,
497 .atomic_disable = ast_dp501_encoder_helper_atomic_disable,
504 static int ast_dp501_connector_helper_get_modes(struct drm_connector *connector)
506 struct ast_connector *ast_connector = to_ast_connector(connector);
509 if (ast_connector->physical_status == connector_status_connected) {
510 struct ast_device *ast = to_ast_device(connector->dev);
511 const struct drm_edid *drm_edid;
513 drm_edid = drm_edid_read_custom(connector, ast_dp512_read_edid_block, ast);
514 drm_edid_connector_update(connector, drm_edid);
515 count = drm_edid_connector_add_modes(connector);
516 drm_edid_free(drm_edid);
518 drm_edid_connector_update(connector, NULL);
521 * There's no EDID data without a connected monitor. Set BMC-
522 * compatible modes in this case. The XGA default resolution
523 * should work well for all BMCs.
525 count = drm_add_modes_noedid(connector, 4096, 4096);
527 drm_set_preferred_mode(connector, 1024, 768);
533 static int ast_dp501_connector_helper_detect_ctx(struct drm_connector *connector,
534 struct drm_modeset_acquire_ctx *ctx,
537 struct ast_connector *ast_connector = to_ast_connector(connector);
538 struct ast_device *ast = to_ast_device(connector->dev);
539 enum drm_connector_status status = connector_status_disconnected;
541 if (ast_dp501_is_connected(ast))
542 status = connector_status_connected;
544 if (status != ast_connector->physical_status)
545 ++connector->epoch_counter;
546 ast_connector->physical_status = status;
548 return connector_status_connected;
551 static const struct drm_connector_helper_funcs ast_dp501_connector_helper_funcs = {
552 .get_modes = ast_dp501_connector_helper_get_modes,
553 .detect_ctx = ast_dp501_connector_helper_detect_ctx,
556 static const struct drm_connector_funcs ast_dp501_connector_funcs = {
557 .reset = drm_atomic_helper_connector_reset,
558 .fill_modes = drm_helper_probe_single_connector_modes,
559 .destroy = drm_connector_cleanup,
560 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
561 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
568 int ast_dp501_output_init(struct ast_device *ast)
570 struct drm_device *dev = &ast->base;
571 struct drm_crtc *crtc = &ast->crtc;
572 struct drm_encoder *encoder;
573 struct ast_connector *ast_connector;
574 struct drm_connector *connector;
579 encoder = &ast->output.dp501.encoder;
580 ret = drm_encoder_init(dev, encoder, &ast_dp501_encoder_funcs,
581 DRM_MODE_ENCODER_TMDS, NULL);
584 drm_encoder_helper_add(encoder, &ast_dp501_encoder_helper_funcs);
586 encoder->possible_crtcs = drm_crtc_mask(crtc);
590 ast_connector = &ast->output.dp501.connector;
591 connector = &ast_connector->base;
592 ret = drm_connector_init(dev, connector, &ast_dp501_connector_funcs,
593 DRM_MODE_CONNECTOR_DisplayPort);
596 drm_connector_helper_add(connector, &ast_dp501_connector_helper_funcs);
598 connector->interlace_allowed = 0;
599 connector->doublescan_allowed = 0;
600 connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
602 ast_connector->physical_status = connector->status;
604 ret = drm_connector_attach_encoder(connector, encoder);