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 int ast_load_dp501_microcode(struct drm_device *dev)
13 struct ast_private *ast = to_ast_private(dev);
15 return request_firmware(&ast->dp501_fw, "ast_dp501_fw.bin", dev->dev);
18 static void send_ack(struct ast_private *ast)
21 sendack = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9b, 0xff);
23 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9b, 0x00, sendack);
26 static void send_nack(struct ast_private *ast)
29 sendack = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9b, 0xff);
31 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9b, 0x00, sendack);
34 static bool wait_ack(struct ast_private *ast)
39 waitack = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd2, 0xff);
42 } while ((!waitack) && (retry++ < 1000));
50 static bool wait_nack(struct ast_private *ast)
55 waitack = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd2, 0xff);
58 } while ((waitack) && (retry++ < 1000));
66 static void set_cmd_trigger(struct ast_private *ast)
68 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9b, ~0x40, 0x40);
71 static void clear_cmd_trigger(struct ast_private *ast)
73 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9b, ~0x40, 0x00);
77 static bool wait_fw_ready(struct ast_private *ast)
82 waitready = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd2, 0xff);
85 } while ((!waitready) && (retry++ < 1000));
94 static bool ast_write_cmd(struct drm_device *dev, u8 data)
96 struct ast_private *ast = to_ast_private(dev);
100 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9a, 0x00, data);
102 set_cmd_trigger(ast);
105 clear_cmd_trigger(ast);
109 } while (retry++ < 100);
111 clear_cmd_trigger(ast);
116 static bool ast_write_data(struct drm_device *dev, u8 data)
118 struct ast_private *ast = to_ast_private(dev);
120 if (wait_nack(ast)) {
122 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9a, 0x00, data);
134 static bool ast_read_data(struct drm_device *dev, u8 *data)
136 struct ast_private *ast = to_ast_private(dev);
141 if (wait_ack(ast) == false)
143 tmp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd3, 0xff);
145 if (wait_nack(ast) == false) {
153 static void clear_cmd(struct ast_private *ast)
156 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9a, 0x00, 0x00);
160 void ast_set_dp501_video_output(struct drm_device *dev, u8 mode)
162 ast_write_cmd(dev, 0x40);
163 ast_write_data(dev, mode);
168 static u32 get_fw_base(struct ast_private *ast)
170 return ast_mindwm(ast, 0x1e6e2104) & 0x7fffffff;
173 bool ast_backup_fw(struct drm_device *dev, u8 *addr, u32 size)
175 struct ast_private *ast = to_ast_private(dev);
179 data = ast_mindwm(ast, 0x1e6e2100) & 0x01;
181 boot_address = get_fw_base(ast);
182 for (i = 0; i < size; i += 4)
183 *(u32 *)(addr + i) = ast_mindwm(ast, boot_address + i);
189 static bool ast_launch_m68k(struct drm_device *dev)
191 struct ast_private *ast = to_ast_private(dev);
192 u32 i, data, len = 0;
197 data = ast_mindwm(ast, 0x1e6e2100) & 0x01;
200 if (ast->dp501_fw_addr) {
201 fw_addr = ast->dp501_fw_addr;
204 if (!ast->dp501_fw &&
205 ast_load_dp501_microcode(dev) < 0)
208 fw_addr = (u8 *)ast->dp501_fw->data;
209 len = ast->dp501_fw->size;
211 /* Get BootAddress */
212 ast_moutdwm(ast, 0x1e6e2000, 0x1688a8a8);
213 data = ast_mindwm(ast, 0x1e6e0004);
214 switch (data & 0x03) {
216 boot_address = 0x44000000;
220 boot_address = 0x48000000;
223 boot_address = 0x50000000;
226 boot_address = 0x60000000;
229 boot_address -= 0x200000; /* -2MB */
231 /* copy image to buffer */
232 for (i = 0; i < len; i += 4) {
233 data = *(u32 *)(fw_addr + i);
234 ast_moutdwm(ast, boot_address + i, data);
238 ast_moutdwm(ast, 0x1e6e2000, 0x1688a8a8);
241 ast_moutdwm(ast, 0x1e6e2104, 0x80000000 + boot_address);
242 ast_moutdwm(ast, 0x1e6e2100, 1);
245 data = ast_mindwm(ast, 0x1e6e2040) & 0xfffff1ff; /* D[11:9] = 100b: UEFI handling */
247 ast_moutdwm(ast, 0x1e6e2040, data);
249 jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x99, 0xfc); /* D[1:0]: Reserved Video Buffer */
251 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x99, jreg);
256 u8 ast_get_dp501_max_clk(struct drm_device *dev)
258 struct ast_private *ast = to_ast_private(dev);
259 u32 boot_address, offset, data;
260 u8 linkcap[4], linkrate, linklanes, maxclk = 0xff;
262 boot_address = get_fw_base(ast);
264 /* validate FW version */
266 data = ast_mindwm(ast, boot_address + offset);
267 if ((data & 0xf0) != 0x10) /* version: 1x */
270 /* Read Link Capability */
272 *(u32 *)linkcap = ast_mindwm(ast, boot_address + offset);
273 if (linkcap[2] == 0) {
274 linkrate = linkcap[0];
275 linklanes = linkcap[1];
276 data = (linkrate == 0x0a) ? (90 * linklanes) : (54 * linklanes);
284 bool ast_dp501_read_edid(struct drm_device *dev, u8 *ediddata)
286 struct ast_private *ast = to_ast_private(dev);
287 u32 i, boot_address, offset, data;
289 boot_address = get_fw_base(ast);
291 /* validate FW version */
293 data = ast_mindwm(ast, boot_address + offset);
294 if ((data & 0xf0) != 0x10)
297 /* validate PnP Monitor */
299 data = ast_mindwm(ast, boot_address + offset);
305 for (i = 0; i < 128; i += 4) {
306 data = ast_mindwm(ast, boot_address + offset + i);
307 *(u32 *)(ediddata + i) = data;
313 static bool ast_init_dvo(struct drm_device *dev)
315 struct ast_private *ast = to_ast_private(dev);
318 ast_write32(ast, 0xf004, 0x1e6e0000);
319 ast_write32(ast, 0xf000, 0x1);
320 ast_write32(ast, 0x12000, 0x1688a8a8);
322 jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
323 if (!(jreg & 0x80)) {
324 /* Init SCU DVO Settings */
325 data = ast_read32(ast, 0x12008);
329 ast_write32(ast, 0x12008, data);
331 if (ast->chip == AST2300) {
332 data = ast_read32(ast, 0x12084);
333 /* multi-pins for DVO single-edge */
335 ast_write32(ast, 0x12084, data);
337 data = ast_read32(ast, 0x12088);
338 /* multi-pins for DVO single-edge */
340 ast_write32(ast, 0x12088, data);
342 data = ast_read32(ast, 0x12090);
343 /* multi-pins for DVO single-edge */
346 ast_write32(ast, 0x12090, data);
347 } else { /* AST2400 */
348 data = ast_read32(ast, 0x12088);
349 /* multi-pins for DVO single-edge */
351 ast_write32(ast, 0x12088, data);
353 data = ast_read32(ast, 0x1208c);
354 /* multi-pins for DVO single-edge */
356 ast_write32(ast, 0x1208c, data);
358 data = ast_read32(ast, 0x120a4);
359 /* multi-pins for DVO single-edge */
361 ast_write32(ast, 0x120a4, data);
363 data = ast_read32(ast, 0x120a8);
364 /* multi-pins for DVO single-edge */
366 ast_write32(ast, 0x120a8, data);
368 data = ast_read32(ast, 0x12094);
369 /* multi-pins for DVO single-edge */
371 ast_write32(ast, 0x12094, data);
376 data = ast_read32(ast, 0x1202c);
378 ast_write32(ast, 0x1202c, data);
380 /* Init VGA DVO Settings */
381 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xcf, 0x80);
386 static void ast_init_analog(struct drm_device *dev)
388 struct ast_private *ast = to_ast_private(dev);
392 * Set DAC source to VGA mode in SCU2C via the P2A
393 * bridge. First configure the P2U to target the SCU
394 * in case it isn't at this stage.
396 ast_write32(ast, 0xf004, 0x1e6e0000);
397 ast_write32(ast, 0xf000, 0x1);
399 /* Then unlock the SCU with the magic password */
400 ast_write32(ast, 0x12000, 0x1688a8a8);
401 ast_write32(ast, 0x12000, 0x1688a8a8);
402 ast_write32(ast, 0x12000, 0x1688a8a8);
404 /* Finally, clear bits [17:16] of SCU2c */
405 data = ast_read32(ast, 0x1202c);
407 ast_write32(ast, 0, data);
410 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xcf, 0x00);
413 void ast_init_3rdtx(struct drm_device *dev)
415 struct ast_private *ast = to_ast_private(dev);
418 if (ast->chip == AST2300 || ast->chip == AST2400) {
419 jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
420 switch (jreg & 0x0e) {
425 ast_launch_m68k(dev);
431 if (ast->tx_chip_type == AST_TX_SIL164)
434 ast_init_analog(dev);
439 void ast_release_firmware(struct drm_device *dev)
441 struct ast_private *ast = to_ast_private(dev);
443 release_firmware(ast->dp501_fw);
444 ast->dp501_fw = NULL;