ast_dp501.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/delay.h>
  3. #include <linux/firmware.h>
  4. #include <linux/module.h>
  5. #include <drm/drm_atomic_state_helper.h>
  6. #include <drm/drm_edid.h>
  7. #include <drm/drm_modeset_helper_vtables.h>
  8. #include <drm/drm_probe_helper.h>
  9. #include "ast_drv.h"
  10. MODULE_FIRMWARE("ast_dp501_fw.bin");
  11. static void ast_release_firmware(void *data)
  12. {
  13. struct ast_device *ast = data;
  14. release_firmware(ast->dp501_fw);
  15. ast->dp501_fw = NULL;
  16. }
  17. static int ast_load_dp501_microcode(struct drm_device *dev)
  18. {
  19. struct ast_device *ast = to_ast_device(dev);
  20. int ret;
  21. ret = request_firmware(&ast->dp501_fw, "ast_dp501_fw.bin", dev->dev);
  22. if (ret)
  23. return ret;
  24. return devm_add_action_or_reset(dev->dev, ast_release_firmware, ast);
  25. }
  26. static void send_ack(struct ast_device *ast)
  27. {
  28. u8 sendack;
  29. sendack = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0x9b, 0xff);
  30. sendack |= 0x80;
  31. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9b, 0x00, sendack);
  32. }
  33. static void send_nack(struct ast_device *ast)
  34. {
  35. u8 sendack;
  36. sendack = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0x9b, 0xff);
  37. sendack &= ~0x80;
  38. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9b, 0x00, sendack);
  39. }
  40. static bool wait_ack(struct ast_device *ast)
  41. {
  42. u8 waitack;
  43. u32 retry = 0;
  44. do {
  45. waitack = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd2, 0xff);
  46. waitack &= 0x80;
  47. udelay(100);
  48. } while ((!waitack) && (retry++ < 1000));
  49. if (retry < 1000)
  50. return true;
  51. else
  52. return false;
  53. }
  54. static bool wait_nack(struct ast_device *ast)
  55. {
  56. u8 waitack;
  57. u32 retry = 0;
  58. do {
  59. waitack = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd2, 0xff);
  60. waitack &= 0x80;
  61. udelay(100);
  62. } while ((waitack) && (retry++ < 1000));
  63. if (retry < 1000)
  64. return true;
  65. else
  66. return false;
  67. }
  68. static void set_cmd_trigger(struct ast_device *ast)
  69. {
  70. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9b, ~0x40, 0x40);
  71. }
  72. static void clear_cmd_trigger(struct ast_device *ast)
  73. {
  74. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9b, ~0x40, 0x00);
  75. }
  76. #if 0
  77. static bool wait_fw_ready(struct ast_device *ast)
  78. {
  79. u8 waitready;
  80. u32 retry = 0;
  81. do {
  82. waitready = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd2, 0xff);
  83. waitready &= 0x40;
  84. udelay(100);
  85. } while ((!waitready) && (retry++ < 1000));
  86. if (retry < 1000)
  87. return true;
  88. else
  89. return false;
  90. }
  91. #endif
  92. static bool ast_write_cmd(struct drm_device *dev, u8 data)
  93. {
  94. struct ast_device *ast = to_ast_device(dev);
  95. int retry = 0;
  96. if (wait_nack(ast)) {
  97. send_nack(ast);
  98. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9a, 0x00, data);
  99. send_ack(ast);
  100. set_cmd_trigger(ast);
  101. do {
  102. if (wait_ack(ast)) {
  103. clear_cmd_trigger(ast);
  104. send_nack(ast);
  105. return true;
  106. }
  107. } while (retry++ < 100);
  108. }
  109. clear_cmd_trigger(ast);
  110. send_nack(ast);
  111. return false;
  112. }
  113. static bool ast_write_data(struct drm_device *dev, u8 data)
  114. {
  115. struct ast_device *ast = to_ast_device(dev);
  116. if (wait_nack(ast)) {
  117. send_nack(ast);
  118. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9a, 0x00, data);
  119. send_ack(ast);
  120. if (wait_ack(ast)) {
  121. send_nack(ast);
  122. return true;
  123. }
  124. }
  125. send_nack(ast);
  126. return false;
  127. }
  128. #if 0
  129. static bool ast_read_data(struct drm_device *dev, u8 *data)
  130. {
  131. struct ast_device *ast = to_ast_device(dev);
  132. u8 tmp;
  133. *data = 0;
  134. if (wait_ack(ast) == false)
  135. return false;
  136. tmp = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd3, 0xff);
  137. *data = tmp;
  138. if (wait_nack(ast) == false) {
  139. send_nack(ast);
  140. return false;
  141. }
  142. send_nack(ast);
  143. return true;
  144. }
  145. static void clear_cmd(struct ast_device *ast)
  146. {
  147. send_nack(ast);
  148. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x9a, 0x00, 0x00);
  149. }
  150. #endif
  151. static void ast_set_dp501_video_output(struct drm_device *dev, u8 mode)
  152. {
  153. ast_write_cmd(dev, 0x40);
  154. ast_write_data(dev, mode);
  155. msleep(10);
  156. }
  157. static u32 get_fw_base(struct ast_device *ast)
  158. {
  159. return ast_mindwm(ast, 0x1e6e2104) & 0x7fffffff;
  160. }
  161. bool ast_backup_fw(struct drm_device *dev, u8 *addr, u32 size)
  162. {
  163. struct ast_device *ast = to_ast_device(dev);
  164. u32 i, data;
  165. u32 boot_address;
  166. if (ast->config_mode != ast_use_p2a)
  167. return false;
  168. data = ast_mindwm(ast, 0x1e6e2100) & 0x01;
  169. if (data) {
  170. boot_address = get_fw_base(ast);
  171. for (i = 0; i < size; i += 4)
  172. *(u32 *)(addr + i) = ast_mindwm(ast, boot_address + i);
  173. return true;
  174. }
  175. return false;
  176. }
  177. static bool ast_launch_m68k(struct drm_device *dev)
  178. {
  179. struct ast_device *ast = to_ast_device(dev);
  180. u32 i, data, len = 0;
  181. u32 boot_address;
  182. u8 *fw_addr = NULL;
  183. u8 jreg;
  184. if (ast->config_mode != ast_use_p2a)
  185. return false;
  186. data = ast_mindwm(ast, 0x1e6e2100) & 0x01;
  187. if (!data) {
  188. if (ast->dp501_fw_addr) {
  189. fw_addr = ast->dp501_fw_addr;
  190. len = 32*1024;
  191. } else {
  192. if (!ast->dp501_fw &&
  193. ast_load_dp501_microcode(dev) < 0)
  194. return false;
  195. fw_addr = (u8 *)ast->dp501_fw->data;
  196. len = ast->dp501_fw->size;
  197. }
  198. /* Get BootAddress */
  199. ast_moutdwm(ast, 0x1e6e2000, 0x1688a8a8);
  200. data = ast_mindwm(ast, 0x1e6e0004);
  201. switch (data & 0x03) {
  202. case 0:
  203. boot_address = 0x44000000;
  204. break;
  205. default:
  206. case 1:
  207. boot_address = 0x48000000;
  208. break;
  209. case 2:
  210. boot_address = 0x50000000;
  211. break;
  212. case 3:
  213. boot_address = 0x60000000;
  214. break;
  215. }
  216. boot_address -= 0x200000; /* -2MB */
  217. /* copy image to buffer */
  218. for (i = 0; i < len; i += 4) {
  219. data = *(u32 *)(fw_addr + i);
  220. ast_moutdwm(ast, boot_address + i, data);
  221. }
  222. /* Init SCU */
  223. ast_moutdwm(ast, 0x1e6e2000, 0x1688a8a8);
  224. /* Launch FW */
  225. ast_moutdwm(ast, 0x1e6e2104, 0x80000000 + boot_address);
  226. ast_moutdwm(ast, 0x1e6e2100, 1);
  227. /* Update Scratch */
  228. data = ast_mindwm(ast, 0x1e6e2040) & 0xfffff1ff; /* D[11:9] = 100b: UEFI handling */
  229. data |= 0x800;
  230. ast_moutdwm(ast, 0x1e6e2040, data);
  231. jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0x99, 0xfc); /* D[1:0]: Reserved Video Buffer */
  232. jreg |= 0x02;
  233. ast_set_index_reg(ast, AST_IO_VGACRI, 0x99, jreg);
  234. }
  235. return true;
  236. }
  237. static bool ast_dp501_is_connected(struct ast_device *ast)
  238. {
  239. u32 boot_address, offset, data;
  240. if (ast->config_mode == ast_use_p2a) {
  241. boot_address = get_fw_base(ast);
  242. /* validate FW version */
  243. offset = AST_DP501_GBL_VERSION;
  244. data = ast_mindwm(ast, boot_address + offset);
  245. if ((data & AST_DP501_FW_VERSION_MASK) != AST_DP501_FW_VERSION_1)
  246. return false;
  247. /* validate PnP Monitor */
  248. offset = AST_DP501_PNPMONITOR;
  249. data = ast_mindwm(ast, boot_address + offset);
  250. if (!(data & AST_DP501_PNP_CONNECTED))
  251. return false;
  252. } else {
  253. if (!ast->dp501_fw_buf)
  254. return false;
  255. /* dummy read */
  256. offset = 0x0000;
  257. data = readl(ast->dp501_fw_buf + offset);
  258. /* validate FW version */
  259. offset = AST_DP501_GBL_VERSION;
  260. data = readl(ast->dp501_fw_buf + offset);
  261. if ((data & AST_DP501_FW_VERSION_MASK) != AST_DP501_FW_VERSION_1)
  262. return false;
  263. /* validate PnP Monitor */
  264. offset = AST_DP501_PNPMONITOR;
  265. data = readl(ast->dp501_fw_buf + offset);
  266. if (!(data & AST_DP501_PNP_CONNECTED))
  267. return false;
  268. }
  269. return true;
  270. }
  271. static int ast_dp512_read_edid_block(void *data, u8 *buf, unsigned int block, size_t len)
  272. {
  273. struct ast_device *ast = data;
  274. size_t rdlen = round_up(len, 4);
  275. u32 i, boot_address, offset, ediddata;
  276. if (block > (512 / EDID_LENGTH))
  277. return -EIO;
  278. offset = AST_DP501_EDID_DATA + block * EDID_LENGTH;
  279. if (ast->config_mode == ast_use_p2a) {
  280. boot_address = get_fw_base(ast);
  281. for (i = 0; i < rdlen; i += 4) {
  282. ediddata = ast_mindwm(ast, boot_address + offset + i);
  283. memcpy(buf, &ediddata, min((len - i), 4));
  284. buf += 4;
  285. }
  286. } else {
  287. for (i = 0; i < rdlen; i += 4) {
  288. ediddata = readl(ast->dp501_fw_buf + offset + i);
  289. memcpy(buf, &ediddata, min((len - i), 4));
  290. buf += 4;
  291. }
  292. }
  293. return true;
  294. }
  295. static bool ast_init_dvo(struct drm_device *dev)
  296. {
  297. struct ast_device *ast = to_ast_device(dev);
  298. u8 jreg;
  299. u32 data;
  300. ast_write32(ast, 0xf004, 0x1e6e0000);
  301. ast_write32(ast, 0xf000, 0x1);
  302. ast_write32(ast, 0x12000, 0x1688a8a8);
  303. jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd0, 0xff);
  304. if (!(jreg & 0x80)) {
  305. /* Init SCU DVO Settings */
  306. data = ast_read32(ast, 0x12008);
  307. /* delay phase */
  308. data &= 0xfffff8ff;
  309. data |= 0x00000500;
  310. ast_write32(ast, 0x12008, data);
  311. if (IS_AST_GEN4(ast)) {
  312. data = ast_read32(ast, 0x12084);
  313. /* multi-pins for DVO single-edge */
  314. data |= 0xfffe0000;
  315. ast_write32(ast, 0x12084, data);
  316. data = ast_read32(ast, 0x12088);
  317. /* multi-pins for DVO single-edge */
  318. data |= 0x000fffff;
  319. ast_write32(ast, 0x12088, data);
  320. data = ast_read32(ast, 0x12090);
  321. /* multi-pins for DVO single-edge */
  322. data &= 0xffffffcf;
  323. data |= 0x00000020;
  324. ast_write32(ast, 0x12090, data);
  325. } else { /* AST GEN5+ */
  326. data = ast_read32(ast, 0x12088);
  327. /* multi-pins for DVO single-edge */
  328. data |= 0x30000000;
  329. ast_write32(ast, 0x12088, data);
  330. data = ast_read32(ast, 0x1208c);
  331. /* multi-pins for DVO single-edge */
  332. data |= 0x000000cf;
  333. ast_write32(ast, 0x1208c, data);
  334. data = ast_read32(ast, 0x120a4);
  335. /* multi-pins for DVO single-edge */
  336. data |= 0xffff0000;
  337. ast_write32(ast, 0x120a4, data);
  338. data = ast_read32(ast, 0x120a8);
  339. /* multi-pins for DVO single-edge */
  340. data |= 0x0000000f;
  341. ast_write32(ast, 0x120a8, data);
  342. data = ast_read32(ast, 0x12094);
  343. /* multi-pins for DVO single-edge */
  344. data |= 0x00000002;
  345. ast_write32(ast, 0x12094, data);
  346. }
  347. }
  348. /* Force to DVO */
  349. data = ast_read32(ast, 0x1202c);
  350. data &= 0xfffbffff;
  351. ast_write32(ast, 0x1202c, data);
  352. /* Init VGA DVO Settings */
  353. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa3, 0xcf, 0x80);
  354. return true;
  355. }
  356. static void ast_init_analog(struct drm_device *dev)
  357. {
  358. struct ast_device *ast = to_ast_device(dev);
  359. u32 data;
  360. /*
  361. * Set DAC source to VGA mode in SCU2C via the P2A
  362. * bridge. First configure the P2U to target the SCU
  363. * in case it isn't at this stage.
  364. */
  365. ast_write32(ast, 0xf004, 0x1e6e0000);
  366. ast_write32(ast, 0xf000, 0x1);
  367. /* Then unlock the SCU with the magic password */
  368. ast_write32(ast, 0x12000, 0x1688a8a8);
  369. ast_write32(ast, 0x12000, 0x1688a8a8);
  370. ast_write32(ast, 0x12000, 0x1688a8a8);
  371. /* Finally, clear bits [17:16] of SCU2c */
  372. data = ast_read32(ast, 0x1202c);
  373. data &= 0xfffcffff;
  374. ast_write32(ast, 0, data);
  375. /* Disable DVO */
  376. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa3, 0xcf, 0x00);
  377. }
  378. void ast_init_3rdtx(struct drm_device *dev)
  379. {
  380. struct ast_device *ast = to_ast_device(dev);
  381. u8 jreg;
  382. if (IS_AST_GEN4(ast) || IS_AST_GEN5(ast)) {
  383. jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd1, 0xff);
  384. switch (jreg & 0x0e) {
  385. case 0x04:
  386. ast_init_dvo(dev);
  387. break;
  388. case 0x08:
  389. ast_launch_m68k(dev);
  390. break;
  391. case 0x0c:
  392. ast_init_dvo(dev);
  393. break;
  394. default:
  395. if (ast->tx_chip_types & BIT(AST_TX_SIL164))
  396. ast_init_dvo(dev);
  397. else
  398. ast_init_analog(dev);
  399. }
  400. }
  401. }
  402. /*
  403. * Encoder
  404. */
  405. static const struct drm_encoder_funcs ast_dp501_encoder_funcs = {
  406. .destroy = drm_encoder_cleanup,
  407. };
  408. static void ast_dp501_encoder_helper_atomic_enable(struct drm_encoder *encoder,
  409. struct drm_atomic_state *state)
  410. {
  411. struct drm_device *dev = encoder->dev;
  412. ast_set_dp501_video_output(dev, 1);
  413. }
  414. static void ast_dp501_encoder_helper_atomic_disable(struct drm_encoder *encoder,
  415. struct drm_atomic_state *state)
  416. {
  417. struct drm_device *dev = encoder->dev;
  418. ast_set_dp501_video_output(dev, 0);
  419. }
  420. static const struct drm_encoder_helper_funcs ast_dp501_encoder_helper_funcs = {
  421. .atomic_enable = ast_dp501_encoder_helper_atomic_enable,
  422. .atomic_disable = ast_dp501_encoder_helper_atomic_disable,
  423. };
  424. /*
  425. * Connector
  426. */
  427. static int ast_dp501_connector_helper_get_modes(struct drm_connector *connector)
  428. {
  429. struct ast_connector *ast_connector = to_ast_connector(connector);
  430. int count;
  431. if (ast_connector->physical_status == connector_status_connected) {
  432. struct ast_device *ast = to_ast_device(connector->dev);
  433. const struct drm_edid *drm_edid;
  434. drm_edid = drm_edid_read_custom(connector, ast_dp512_read_edid_block, ast);
  435. drm_edid_connector_update(connector, drm_edid);
  436. count = drm_edid_connector_add_modes(connector);
  437. drm_edid_free(drm_edid);
  438. } else {
  439. drm_edid_connector_update(connector, NULL);
  440. /*
  441. * There's no EDID data without a connected monitor. Set BMC-
  442. * compatible modes in this case. The XGA default resolution
  443. * should work well for all BMCs.
  444. */
  445. count = drm_add_modes_noedid(connector, 4096, 4096);
  446. if (count)
  447. drm_set_preferred_mode(connector, 1024, 768);
  448. }
  449. return count;
  450. }
  451. static int ast_dp501_connector_helper_detect_ctx(struct drm_connector *connector,
  452. struct drm_modeset_acquire_ctx *ctx,
  453. bool force)
  454. {
  455. struct ast_connector *ast_connector = to_ast_connector(connector);
  456. struct ast_device *ast = to_ast_device(connector->dev);
  457. enum drm_connector_status status = connector_status_disconnected;
  458. if (ast_dp501_is_connected(ast))
  459. status = connector_status_connected;
  460. if (status != ast_connector->physical_status)
  461. ++connector->epoch_counter;
  462. ast_connector->physical_status = status;
  463. return connector_status_connected;
  464. }
  465. static const struct drm_connector_helper_funcs ast_dp501_connector_helper_funcs = {
  466. .get_modes = ast_dp501_connector_helper_get_modes,
  467. .detect_ctx = ast_dp501_connector_helper_detect_ctx,
  468. };
  469. static const struct drm_connector_funcs ast_dp501_connector_funcs = {
  470. .reset = drm_atomic_helper_connector_reset,
  471. .fill_modes = drm_helper_probe_single_connector_modes,
  472. .destroy = drm_connector_cleanup,
  473. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  474. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  475. };
  476. static int ast_dp501_connector_init(struct drm_device *dev, struct drm_connector *connector)
  477. {
  478. int ret;
  479. ret = drm_connector_init(dev, connector, &ast_dp501_connector_funcs,
  480. DRM_MODE_CONNECTOR_DisplayPort);
  481. if (ret)
  482. return ret;
  483. drm_connector_helper_add(connector, &ast_dp501_connector_helper_funcs);
  484. connector->interlace_allowed = 0;
  485. connector->doublescan_allowed = 0;
  486. connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
  487. return 0;
  488. }
  489. int ast_dp501_output_init(struct ast_device *ast)
  490. {
  491. struct drm_device *dev = &ast->base;
  492. struct drm_crtc *crtc = &ast->crtc;
  493. struct drm_encoder *encoder = &ast->output.dp501.encoder;
  494. struct ast_connector *ast_connector = &ast->output.dp501.connector;
  495. struct drm_connector *connector = &ast_connector->base;
  496. int ret;
  497. ret = drm_encoder_init(dev, encoder, &ast_dp501_encoder_funcs,
  498. DRM_MODE_ENCODER_TMDS, NULL);
  499. if (ret)
  500. return ret;
  501. drm_encoder_helper_add(encoder, &ast_dp501_encoder_helper_funcs);
  502. encoder->possible_crtcs = drm_crtc_mask(crtc);
  503. ret = ast_dp501_connector_init(dev, connector);
  504. if (ret)
  505. return ret;
  506. ast_connector->physical_status = connector->status;
  507. ret = drm_connector_attach_encoder(connector, encoder);
  508. if (ret)
  509. return ret;
  510. return 0;
  511. }