1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/delay.h>
4 #include <linux/firmware.h>
5 #include <linux/module.h>
9 MODULE_FIRMWARE("ast_dp501_fw.bin");
11 static void ast_release_firmware(void *data)
13 struct ast_device *ast = data;
15 release_firmware(ast->dp501_fw);
19 static int ast_load_dp501_microcode(struct drm_device *dev)
21 struct ast_device *ast = to_ast_device(dev);
24 ret = request_firmware(&ast->dp501_fw, "ast_dp501_fw.bin", dev->dev);
28 return devm_add_action_or_reset(dev->dev, ast_release_firmware, ast);
31 static void send_ack(struct ast_device *ast)
34 sendack = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0x9b, 0xff);
36 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9b, 0x00, sendack);
39 static void send_nack(struct ast_device *ast)
42 sendack = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0x9b, 0xff);
44 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9b, 0x00, sendack);
47 static bool wait_ack(struct ast_device *ast)
52 waitack = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd2, 0xff);
55 } while ((!waitack) && (retry++ < 1000));
63 static bool wait_nack(struct ast_device *ast)
68 waitack = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd2, 0xff);
71 } while ((waitack) && (retry++ < 1000));
79 static void set_cmd_trigger(struct ast_device *ast)
81 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9b, ~0x40, 0x40);
84 static void clear_cmd_trigger(struct ast_device *ast)
86 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9b, ~0x40, 0x00);
90 static bool wait_fw_ready(struct ast_device *ast)
95 waitready = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd2, 0xff);
98 } while ((!waitready) && (retry++ < 1000));
107 static bool ast_write_cmd(struct drm_device *dev, u8 data)
109 struct ast_device *ast = to_ast_device(dev);
111 if (wait_nack(ast)) {
113 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9a, 0x00, data);
115 set_cmd_trigger(ast);
118 clear_cmd_trigger(ast);
122 } while (retry++ < 100);
124 clear_cmd_trigger(ast);
129 static bool ast_write_data(struct drm_device *dev, u8 data)
131 struct ast_device *ast = to_ast_device(dev);
133 if (wait_nack(ast)) {
135 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9a, 0x00, data);
147 static bool ast_read_data(struct drm_device *dev, u8 *data)
149 struct ast_device *ast = to_ast_device(dev);
154 if (wait_ack(ast) == false)
156 tmp = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd3, 0xff);
158 if (wait_nack(ast) == false) {
166 static void clear_cmd(struct ast_device *ast)
169 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9a, 0x00, 0x00);
173 void ast_set_dp501_video_output(struct drm_device *dev, u8 mode)
175 ast_write_cmd(dev, 0x40);
176 ast_write_data(dev, mode);
181 static u32 get_fw_base(struct ast_device *ast)
183 return ast_mindwm(ast, 0x1e6e2104) & 0x7fffffff;
186 bool ast_backup_fw(struct drm_device *dev, u8 *addr, u32 size)
188 struct ast_device *ast = to_ast_device(dev);
192 if (ast->config_mode != ast_use_p2a)
195 data = ast_mindwm(ast, 0x1e6e2100) & 0x01;
197 boot_address = get_fw_base(ast);
198 for (i = 0; i < size; i += 4)
199 *(u32 *)(addr + i) = ast_mindwm(ast, boot_address + i);
205 static bool ast_launch_m68k(struct drm_device *dev)
207 struct ast_device *ast = to_ast_device(dev);
208 u32 i, data, len = 0;
213 if (ast->config_mode != ast_use_p2a)
216 data = ast_mindwm(ast, 0x1e6e2100) & 0x01;
219 if (ast->dp501_fw_addr) {
220 fw_addr = ast->dp501_fw_addr;
223 if (!ast->dp501_fw &&
224 ast_load_dp501_microcode(dev) < 0)
227 fw_addr = (u8 *)ast->dp501_fw->data;
228 len = ast->dp501_fw->size;
230 /* Get BootAddress */
231 ast_moutdwm(ast, 0x1e6e2000, 0x1688a8a8);
232 data = ast_mindwm(ast, 0x1e6e0004);
233 switch (data & 0x03) {
235 boot_address = 0x44000000;
239 boot_address = 0x48000000;
242 boot_address = 0x50000000;
245 boot_address = 0x60000000;
248 boot_address -= 0x200000; /* -2MB */
250 /* copy image to buffer */
251 for (i = 0; i < len; i += 4) {
252 data = *(u32 *)(fw_addr + i);
253 ast_moutdwm(ast, boot_address + i, data);
257 ast_moutdwm(ast, 0x1e6e2000, 0x1688a8a8);
260 ast_moutdwm(ast, 0x1e6e2104, 0x80000000 + boot_address);
261 ast_moutdwm(ast, 0x1e6e2100, 1);
264 data = ast_mindwm(ast, 0x1e6e2040) & 0xfffff1ff; /* D[11:9] = 100b: UEFI handling */
266 ast_moutdwm(ast, 0x1e6e2040, data);
268 jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0x99, 0xfc); /* D[1:0]: Reserved Video Buffer */
270 ast_set_index_reg(ast, AST_IO_VGACRI, 0x99, jreg);
275 bool ast_dp501_is_connected(struct ast_device *ast)
277 u32 boot_address, offset, data;
279 if (ast->config_mode == ast_use_p2a) {
280 boot_address = get_fw_base(ast);
282 /* validate FW version */
283 offset = AST_DP501_GBL_VERSION;
284 data = ast_mindwm(ast, boot_address + offset);
285 if ((data & AST_DP501_FW_VERSION_MASK) != AST_DP501_FW_VERSION_1)
288 /* validate PnP Monitor */
289 offset = AST_DP501_PNPMONITOR;
290 data = ast_mindwm(ast, boot_address + offset);
291 if (!(data & AST_DP501_PNP_CONNECTED))
294 if (!ast->dp501_fw_buf)
299 data = readl(ast->dp501_fw_buf + offset);
301 /* validate FW version */
302 offset = AST_DP501_GBL_VERSION;
303 data = readl(ast->dp501_fw_buf + offset);
304 if ((data & AST_DP501_FW_VERSION_MASK) != AST_DP501_FW_VERSION_1)
307 /* validate PnP Monitor */
308 offset = AST_DP501_PNPMONITOR;
309 data = readl(ast->dp501_fw_buf + offset);
310 if (!(data & AST_DP501_PNP_CONNECTED))
316 bool ast_dp501_read_edid(struct drm_device *dev, u8 *ediddata)
318 struct ast_device *ast = to_ast_device(dev);
319 u32 i, boot_address, offset, data;
322 if (!ast_dp501_is_connected(ast))
325 if (ast->config_mode == ast_use_p2a) {
326 boot_address = get_fw_base(ast);
329 offset = AST_DP501_EDID_DATA;
330 for (i = 0; i < 128; i += 4) {
331 data = ast_mindwm(ast, boot_address + offset + i);
332 pEDIDidx = (u32 *)(ediddata + i);
337 offset = AST_DP501_EDID_DATA;
338 for (i = 0; i < 128; i += 4) {
339 data = readl(ast->dp501_fw_buf + offset + i);
340 pEDIDidx = (u32 *)(ediddata + i);
348 static bool ast_init_dvo(struct drm_device *dev)
350 struct ast_device *ast = to_ast_device(dev);
353 ast_write32(ast, 0xf004, 0x1e6e0000);
354 ast_write32(ast, 0xf000, 0x1);
355 ast_write32(ast, 0x12000, 0x1688a8a8);
357 jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd0, 0xff);
358 if (!(jreg & 0x80)) {
359 /* Init SCU DVO Settings */
360 data = ast_read32(ast, 0x12008);
364 ast_write32(ast, 0x12008, data);
366 if (IS_AST_GEN4(ast)) {
367 data = ast_read32(ast, 0x12084);
368 /* multi-pins for DVO single-edge */
370 ast_write32(ast, 0x12084, data);
372 data = ast_read32(ast, 0x12088);
373 /* multi-pins for DVO single-edge */
375 ast_write32(ast, 0x12088, data);
377 data = ast_read32(ast, 0x12090);
378 /* multi-pins for DVO single-edge */
381 ast_write32(ast, 0x12090, data);
382 } else { /* AST GEN5+ */
383 data = ast_read32(ast, 0x12088);
384 /* multi-pins for DVO single-edge */
386 ast_write32(ast, 0x12088, data);
388 data = ast_read32(ast, 0x1208c);
389 /* multi-pins for DVO single-edge */
391 ast_write32(ast, 0x1208c, data);
393 data = ast_read32(ast, 0x120a4);
394 /* multi-pins for DVO single-edge */
396 ast_write32(ast, 0x120a4, data);
398 data = ast_read32(ast, 0x120a8);
399 /* multi-pins for DVO single-edge */
401 ast_write32(ast, 0x120a8, data);
403 data = ast_read32(ast, 0x12094);
404 /* multi-pins for DVO single-edge */
406 ast_write32(ast, 0x12094, data);
411 data = ast_read32(ast, 0x1202c);
413 ast_write32(ast, 0x1202c, data);
415 /* Init VGA DVO Settings */
416 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa3, 0xcf, 0x80);
421 static void ast_init_analog(struct drm_device *dev)
423 struct ast_device *ast = to_ast_device(dev);
427 * Set DAC source to VGA mode in SCU2C via the P2A
428 * bridge. First configure the P2U to target the SCU
429 * in case it isn't at this stage.
431 ast_write32(ast, 0xf004, 0x1e6e0000);
432 ast_write32(ast, 0xf000, 0x1);
434 /* Then unlock the SCU with the magic password */
435 ast_write32(ast, 0x12000, 0x1688a8a8);
436 ast_write32(ast, 0x12000, 0x1688a8a8);
437 ast_write32(ast, 0x12000, 0x1688a8a8);
439 /* Finally, clear bits [17:16] of SCU2c */
440 data = ast_read32(ast, 0x1202c);
442 ast_write32(ast, 0, data);
445 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa3, 0xcf, 0x00);
448 void ast_init_3rdtx(struct drm_device *dev)
450 struct ast_device *ast = to_ast_device(dev);
453 if (IS_AST_GEN4(ast) || IS_AST_GEN5(ast)) {
454 jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd1, 0xff);
455 switch (jreg & 0x0e) {
460 ast_launch_m68k(dev);
466 if (ast->tx_chip_types & BIT(AST_TX_SIL164))
469 ast_init_analog(dev);