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_private
*ast
= data
;
15 release_firmware(ast
->dp501_fw
);
19 static int ast_load_dp501_microcode(struct drm_device
*dev
)
21 struct ast_private
*ast
= to_ast_private(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_private
*ast
)
34 sendack
= ast_get_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0x9b, 0xff);
36 ast_set_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0x9b, 0x00, sendack
);
39 static void send_nack(struct ast_private
*ast
)
42 sendack
= ast_get_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0x9b, 0xff);
44 ast_set_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0x9b, 0x00, sendack
);
47 static bool wait_ack(struct ast_private
*ast
)
52 waitack
= ast_get_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0xd2, 0xff);
55 } while ((!waitack
) && (retry
++ < 1000));
63 static bool wait_nack(struct ast_private
*ast
)
68 waitack
= ast_get_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0xd2, 0xff);
71 } while ((waitack
) && (retry
++ < 1000));
79 static void set_cmd_trigger(struct ast_private
*ast
)
81 ast_set_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0x9b, ~0x40, 0x40);
84 static void clear_cmd_trigger(struct ast_private
*ast
)
86 ast_set_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0x9b, ~0x40, 0x00);
90 static bool wait_fw_ready(struct ast_private
*ast
)
95 waitready
= ast_get_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0xd2, 0xff);
98 } while ((!waitready
) && (retry
++ < 1000));
107 static bool ast_write_cmd(struct drm_device
*dev
, u8 data
)
109 struct ast_private
*ast
= to_ast_private(dev
);
111 if (wait_nack(ast
)) {
113 ast_set_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 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_private
*ast
= to_ast_private(dev
);
133 if (wait_nack(ast
)) {
135 ast_set_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0x9a, 0x00, data
);
147 static bool ast_read_data(struct drm_device
*dev
, u8
*data
)
149 struct ast_private
*ast
= to_ast_private(dev
);
154 if (wait_ack(ast
) == false)
156 tmp
= ast_get_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0xd3, 0xff);
158 if (wait_nack(ast
) == false) {
166 static void clear_cmd(struct ast_private
*ast
)
169 ast_set_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 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_private
*ast
)
183 return ast_mindwm(ast
, 0x1e6e2104) & 0x7fffffff;
186 bool ast_backup_fw(struct drm_device
*dev
, u8
*addr
, u32 size
)
188 struct ast_private
*ast
= to_ast_private(dev
);
192 data
= ast_mindwm(ast
, 0x1e6e2100) & 0x01;
194 boot_address
= get_fw_base(ast
);
195 for (i
= 0; i
< size
; i
+= 4)
196 *(u32
*)(addr
+ i
) = ast_mindwm(ast
, boot_address
+ i
);
202 static bool ast_launch_m68k(struct drm_device
*dev
)
204 struct ast_private
*ast
= to_ast_private(dev
);
205 u32 i
, data
, len
= 0;
210 data
= ast_mindwm(ast
, 0x1e6e2100) & 0x01;
213 if (ast
->dp501_fw_addr
) {
214 fw_addr
= ast
->dp501_fw_addr
;
217 if (!ast
->dp501_fw
&&
218 ast_load_dp501_microcode(dev
) < 0)
221 fw_addr
= (u8
*)ast
->dp501_fw
->data
;
222 len
= ast
->dp501_fw
->size
;
224 /* Get BootAddress */
225 ast_moutdwm(ast
, 0x1e6e2000, 0x1688a8a8);
226 data
= ast_mindwm(ast
, 0x1e6e0004);
227 switch (data
& 0x03) {
229 boot_address
= 0x44000000;
233 boot_address
= 0x48000000;
236 boot_address
= 0x50000000;
239 boot_address
= 0x60000000;
242 boot_address
-= 0x200000; /* -2MB */
244 /* copy image to buffer */
245 for (i
= 0; i
< len
; i
+= 4) {
246 data
= *(u32
*)(fw_addr
+ i
);
247 ast_moutdwm(ast
, boot_address
+ i
, data
);
251 ast_moutdwm(ast
, 0x1e6e2000, 0x1688a8a8);
254 ast_moutdwm(ast
, 0x1e6e2104, 0x80000000 + boot_address
);
255 ast_moutdwm(ast
, 0x1e6e2100, 1);
258 data
= ast_mindwm(ast
, 0x1e6e2040) & 0xfffff1ff; /* D[11:9] = 100b: UEFI handling */
260 ast_moutdwm(ast
, 0x1e6e2040, data
);
262 jreg
= ast_get_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0x99, 0xfc); /* D[1:0]: Reserved Video Buffer */
264 ast_set_index_reg(ast
, AST_IO_CRTC_PORT
, 0x99, jreg
);
269 u8
ast_get_dp501_max_clk(struct drm_device
*dev
)
271 struct ast_private
*ast
= to_ast_private(dev
);
272 u32 boot_address
, offset
, data
;
273 u8 linkcap
[4], linkrate
, linklanes
, maxclk
= 0xff;
275 boot_address
= get_fw_base(ast
);
277 /* validate FW version */
279 data
= ast_mindwm(ast
, boot_address
+ offset
);
280 if ((data
& 0xf0) != 0x10) /* version: 1x */
283 /* Read Link Capability */
285 *(u32
*)linkcap
= ast_mindwm(ast
, boot_address
+ offset
);
286 if (linkcap
[2] == 0) {
287 linkrate
= linkcap
[0];
288 linklanes
= linkcap
[1];
289 data
= (linkrate
== 0x0a) ? (90 * linklanes
) : (54 * linklanes
);
297 bool ast_dp501_read_edid(struct drm_device
*dev
, u8
*ediddata
)
299 struct ast_private
*ast
= to_ast_private(dev
);
300 u32 i
, boot_address
, offset
, data
;
302 boot_address
= get_fw_base(ast
);
304 /* validate FW version */
306 data
= ast_mindwm(ast
, boot_address
+ offset
);
307 if ((data
& 0xf0) != 0x10)
310 /* validate PnP Monitor */
312 data
= ast_mindwm(ast
, boot_address
+ offset
);
318 for (i
= 0; i
< 128; i
+= 4) {
319 data
= ast_mindwm(ast
, boot_address
+ offset
+ i
);
320 *(u32
*)(ediddata
+ i
) = data
;
326 static bool ast_init_dvo(struct drm_device
*dev
)
328 struct ast_private
*ast
= to_ast_private(dev
);
331 ast_write32(ast
, 0xf004, 0x1e6e0000);
332 ast_write32(ast
, 0xf000, 0x1);
333 ast_write32(ast
, 0x12000, 0x1688a8a8);
335 jreg
= ast_get_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0xd0, 0xff);
336 if (!(jreg
& 0x80)) {
337 /* Init SCU DVO Settings */
338 data
= ast_read32(ast
, 0x12008);
342 ast_write32(ast
, 0x12008, data
);
344 if (ast
->chip
== AST2300
) {
345 data
= ast_read32(ast
, 0x12084);
346 /* multi-pins for DVO single-edge */
348 ast_write32(ast
, 0x12084, data
);
350 data
= ast_read32(ast
, 0x12088);
351 /* multi-pins for DVO single-edge */
353 ast_write32(ast
, 0x12088, data
);
355 data
= ast_read32(ast
, 0x12090);
356 /* multi-pins for DVO single-edge */
359 ast_write32(ast
, 0x12090, data
);
360 } else { /* AST2400 */
361 data
= ast_read32(ast
, 0x12088);
362 /* multi-pins for DVO single-edge */
364 ast_write32(ast
, 0x12088, data
);
366 data
= ast_read32(ast
, 0x1208c);
367 /* multi-pins for DVO single-edge */
369 ast_write32(ast
, 0x1208c, data
);
371 data
= ast_read32(ast
, 0x120a4);
372 /* multi-pins for DVO single-edge */
374 ast_write32(ast
, 0x120a4, data
);
376 data
= ast_read32(ast
, 0x120a8);
377 /* multi-pins for DVO single-edge */
379 ast_write32(ast
, 0x120a8, data
);
381 data
= ast_read32(ast
, 0x12094);
382 /* multi-pins for DVO single-edge */
384 ast_write32(ast
, 0x12094, data
);
389 data
= ast_read32(ast
, 0x1202c);
391 ast_write32(ast
, 0x1202c, data
);
393 /* Init VGA DVO Settings */
394 ast_set_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0xa3, 0xcf, 0x80);
399 static void ast_init_analog(struct drm_device
*dev
)
401 struct ast_private
*ast
= to_ast_private(dev
);
405 * Set DAC source to VGA mode in SCU2C via the P2A
406 * bridge. First configure the P2U to target the SCU
407 * in case it isn't at this stage.
409 ast_write32(ast
, 0xf004, 0x1e6e0000);
410 ast_write32(ast
, 0xf000, 0x1);
412 /* Then unlock the SCU with the magic password */
413 ast_write32(ast
, 0x12000, 0x1688a8a8);
414 ast_write32(ast
, 0x12000, 0x1688a8a8);
415 ast_write32(ast
, 0x12000, 0x1688a8a8);
417 /* Finally, clear bits [17:16] of SCU2c */
418 data
= ast_read32(ast
, 0x1202c);
420 ast_write32(ast
, 0, data
);
423 ast_set_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0xa3, 0xcf, 0x00);
426 void ast_init_3rdtx(struct drm_device
*dev
)
428 struct ast_private
*ast
= to_ast_private(dev
);
431 if (ast
->chip
== AST2300
|| ast
->chip
== AST2400
) {
432 jreg
= ast_get_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0xd1, 0xff);
433 switch (jreg
& 0x0e) {
438 ast_launch_m68k(dev
);
444 if (ast
->tx_chip_type
== AST_TX_SIL164
)
447 ast_init_analog(dev
);