ast_dp501.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/delay.h>
  3. #include <linux/firmware.h>
  4. #include <linux/module.h>
  5. #include "ast_drv.h"
  6. MODULE_FIRMWARE("ast_dp501_fw.bin");
  7. static void ast_release_firmware(void *data)
  8. {
  9. struct ast_private *ast = data;
  10. release_firmware(ast->dp501_fw);
  11. ast->dp501_fw = NULL;
  12. }
  13. static int ast_load_dp501_microcode(struct drm_device *dev)
  14. {
  15. struct ast_private *ast = to_ast_private(dev);
  16. int ret;
  17. ret = request_firmware(&ast->dp501_fw, "ast_dp501_fw.bin", dev->dev);
  18. if (ret)
  19. return ret;
  20. return devm_add_action_or_reset(dev->dev, ast_release_firmware, ast);
  21. }
  22. static void send_ack(struct ast_private *ast)
  23. {
  24. u8 sendack;
  25. sendack = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9b, 0xff);
  26. sendack |= 0x80;
  27. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9b, 0x00, sendack);
  28. }
  29. static void send_nack(struct ast_private *ast)
  30. {
  31. u8 sendack;
  32. sendack = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9b, 0xff);
  33. sendack &= ~0x80;
  34. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9b, 0x00, sendack);
  35. }
  36. static bool wait_ack(struct ast_private *ast)
  37. {
  38. u8 waitack;
  39. u32 retry = 0;
  40. do {
  41. waitack = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd2, 0xff);
  42. waitack &= 0x80;
  43. udelay(100);
  44. } while ((!waitack) && (retry++ < 1000));
  45. if (retry < 1000)
  46. return true;
  47. else
  48. return false;
  49. }
  50. static bool wait_nack(struct ast_private *ast)
  51. {
  52. u8 waitack;
  53. u32 retry = 0;
  54. do {
  55. waitack = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd2, 0xff);
  56. waitack &= 0x80;
  57. udelay(100);
  58. } while ((waitack) && (retry++ < 1000));
  59. if (retry < 1000)
  60. return true;
  61. else
  62. return false;
  63. }
  64. static void set_cmd_trigger(struct ast_private *ast)
  65. {
  66. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9b, ~0x40, 0x40);
  67. }
  68. static void clear_cmd_trigger(struct ast_private *ast)
  69. {
  70. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9b, ~0x40, 0x00);
  71. }
  72. #if 0
  73. static bool wait_fw_ready(struct ast_private *ast)
  74. {
  75. u8 waitready;
  76. u32 retry = 0;
  77. do {
  78. waitready = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd2, 0xff);
  79. waitready &= 0x40;
  80. udelay(100);
  81. } while ((!waitready) && (retry++ < 1000));
  82. if (retry < 1000)
  83. return true;
  84. else
  85. return false;
  86. }
  87. #endif
  88. static bool ast_write_cmd(struct drm_device *dev, u8 data)
  89. {
  90. struct ast_private *ast = to_ast_private(dev);
  91. int retry = 0;
  92. if (wait_nack(ast)) {
  93. send_nack(ast);
  94. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9a, 0x00, data);
  95. send_ack(ast);
  96. set_cmd_trigger(ast);
  97. do {
  98. if (wait_ack(ast)) {
  99. clear_cmd_trigger(ast);
  100. send_nack(ast);
  101. return true;
  102. }
  103. } while (retry++ < 100);
  104. }
  105. clear_cmd_trigger(ast);
  106. send_nack(ast);
  107. return false;
  108. }
  109. static bool ast_write_data(struct drm_device *dev, u8 data)
  110. {
  111. struct ast_private *ast = to_ast_private(dev);
  112. if (wait_nack(ast)) {
  113. send_nack(ast);
  114. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9a, 0x00, data);
  115. send_ack(ast);
  116. if (wait_ack(ast)) {
  117. send_nack(ast);
  118. return true;
  119. }
  120. }
  121. send_nack(ast);
  122. return false;
  123. }
  124. #if 0
  125. static bool ast_read_data(struct drm_device *dev, u8 *data)
  126. {
  127. struct ast_private *ast = to_ast_private(dev);
  128. u8 tmp;
  129. *data = 0;
  130. if (wait_ack(ast) == false)
  131. return false;
  132. tmp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd3, 0xff);
  133. *data = tmp;
  134. if (wait_nack(ast) == false) {
  135. send_nack(ast);
  136. return false;
  137. }
  138. send_nack(ast);
  139. return true;
  140. }
  141. static void clear_cmd(struct ast_private *ast)
  142. {
  143. send_nack(ast);
  144. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9a, 0x00, 0x00);
  145. }
  146. #endif
  147. void ast_set_dp501_video_output(struct drm_device *dev, u8 mode)
  148. {
  149. ast_write_cmd(dev, 0x40);
  150. ast_write_data(dev, mode);
  151. msleep(10);
  152. }
  153. static u32 get_fw_base(struct ast_private *ast)
  154. {
  155. return ast_mindwm(ast, 0x1e6e2104) & 0x7fffffff;
  156. }
  157. bool ast_backup_fw(struct drm_device *dev, u8 *addr, u32 size)
  158. {
  159. struct ast_private *ast = to_ast_private(dev);
  160. u32 i, data;
  161. u32 boot_address;
  162. if (ast->config_mode != ast_use_p2a)
  163. return false;
  164. data = ast_mindwm(ast, 0x1e6e2100) & 0x01;
  165. if (data) {
  166. boot_address = get_fw_base(ast);
  167. for (i = 0; i < size; i += 4)
  168. *(u32 *)(addr + i) = ast_mindwm(ast, boot_address + i);
  169. return true;
  170. }
  171. return false;
  172. }
  173. static bool ast_launch_m68k(struct drm_device *dev)
  174. {
  175. struct ast_private *ast = to_ast_private(dev);
  176. u32 i, data, len = 0;
  177. u32 boot_address;
  178. u8 *fw_addr = NULL;
  179. u8 jreg;
  180. if (ast->config_mode != ast_use_p2a)
  181. return false;
  182. data = ast_mindwm(ast, 0x1e6e2100) & 0x01;
  183. if (!data) {
  184. if (ast->dp501_fw_addr) {
  185. fw_addr = ast->dp501_fw_addr;
  186. len = 32*1024;
  187. } else {
  188. if (!ast->dp501_fw &&
  189. ast_load_dp501_microcode(dev) < 0)
  190. return false;
  191. fw_addr = (u8 *)ast->dp501_fw->data;
  192. len = ast->dp501_fw->size;
  193. }
  194. /* Get BootAddress */
  195. ast_moutdwm(ast, 0x1e6e2000, 0x1688a8a8);
  196. data = ast_mindwm(ast, 0x1e6e0004);
  197. switch (data & 0x03) {
  198. case 0:
  199. boot_address = 0x44000000;
  200. break;
  201. default:
  202. case 1:
  203. boot_address = 0x48000000;
  204. break;
  205. case 2:
  206. boot_address = 0x50000000;
  207. break;
  208. case 3:
  209. boot_address = 0x60000000;
  210. break;
  211. }
  212. boot_address -= 0x200000; /* -2MB */
  213. /* copy image to buffer */
  214. for (i = 0; i < len; i += 4) {
  215. data = *(u32 *)(fw_addr + i);
  216. ast_moutdwm(ast, boot_address + i, data);
  217. }
  218. /* Init SCU */
  219. ast_moutdwm(ast, 0x1e6e2000, 0x1688a8a8);
  220. /* Launch FW */
  221. ast_moutdwm(ast, 0x1e6e2104, 0x80000000 + boot_address);
  222. ast_moutdwm(ast, 0x1e6e2100, 1);
  223. /* Update Scratch */
  224. data = ast_mindwm(ast, 0x1e6e2040) & 0xfffff1ff; /* D[11:9] = 100b: UEFI handling */
  225. data |= 0x800;
  226. ast_moutdwm(ast, 0x1e6e2040, data);
  227. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x99, 0xfc); /* D[1:0]: Reserved Video Buffer */
  228. jreg |= 0x02;
  229. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x99, jreg);
  230. }
  231. return true;
  232. }
  233. bool ast_dp501_read_edid(struct drm_device *dev, u8 *ediddata)
  234. {
  235. struct ast_private *ast = to_ast_private(dev);
  236. u32 i, boot_address, offset, data;
  237. u32 *pEDIDidx;
  238. if (ast->config_mode == ast_use_p2a) {
  239. boot_address = get_fw_base(ast);
  240. /* validate FW version */
  241. offset = AST_DP501_GBL_VERSION;
  242. data = ast_mindwm(ast, boot_address + offset);
  243. if ((data & AST_DP501_FW_VERSION_MASK) != AST_DP501_FW_VERSION_1)
  244. return false;
  245. /* validate PnP Monitor */
  246. offset = AST_DP501_PNPMONITOR;
  247. data = ast_mindwm(ast, boot_address + offset);
  248. if (!(data & AST_DP501_PNP_CONNECTED))
  249. return false;
  250. /* Read EDID */
  251. offset = AST_DP501_EDID_DATA;
  252. for (i = 0; i < 128; i += 4) {
  253. data = ast_mindwm(ast, boot_address + offset + i);
  254. pEDIDidx = (u32 *)(ediddata + i);
  255. *pEDIDidx = data;
  256. }
  257. } else {
  258. if (!ast->dp501_fw_buf)
  259. return false;
  260. /* dummy read */
  261. offset = 0x0000;
  262. data = readl(ast->dp501_fw_buf + offset);
  263. /* validate FW version */
  264. offset = AST_DP501_GBL_VERSION;
  265. data = readl(ast->dp501_fw_buf + offset);
  266. if ((data & AST_DP501_FW_VERSION_MASK) != AST_DP501_FW_VERSION_1)
  267. return false;
  268. /* validate PnP Monitor */
  269. offset = AST_DP501_PNPMONITOR;
  270. data = readl(ast->dp501_fw_buf + offset);
  271. if (!(data & AST_DP501_PNP_CONNECTED))
  272. return false;
  273. /* Read EDID */
  274. offset = AST_DP501_EDID_DATA;
  275. for (i = 0; i < 128; i += 4) {
  276. data = readl(ast->dp501_fw_buf + offset + i);
  277. pEDIDidx = (u32 *)(ediddata + i);
  278. *pEDIDidx = data;
  279. }
  280. }
  281. return true;
  282. }
  283. static bool ast_init_dvo(struct drm_device *dev)
  284. {
  285. struct ast_private *ast = to_ast_private(dev);
  286. u8 jreg;
  287. u32 data;
  288. ast_write32(ast, 0xf004, 0x1e6e0000);
  289. ast_write32(ast, 0xf000, 0x1);
  290. ast_write32(ast, 0x12000, 0x1688a8a8);
  291. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
  292. if (!(jreg & 0x80)) {
  293. /* Init SCU DVO Settings */
  294. data = ast_read32(ast, 0x12008);
  295. /* delay phase */
  296. data &= 0xfffff8ff;
  297. data |= 0x00000500;
  298. ast_write32(ast, 0x12008, data);
  299. if (ast->chip == AST2300) {
  300. data = ast_read32(ast, 0x12084);
  301. /* multi-pins for DVO single-edge */
  302. data |= 0xfffe0000;
  303. ast_write32(ast, 0x12084, data);
  304. data = ast_read32(ast, 0x12088);
  305. /* multi-pins for DVO single-edge */
  306. data |= 0x000fffff;
  307. ast_write32(ast, 0x12088, data);
  308. data = ast_read32(ast, 0x12090);
  309. /* multi-pins for DVO single-edge */
  310. data &= 0xffffffcf;
  311. data |= 0x00000020;
  312. ast_write32(ast, 0x12090, data);
  313. } else { /* AST2400 */
  314. data = ast_read32(ast, 0x12088);
  315. /* multi-pins for DVO single-edge */
  316. data |= 0x30000000;
  317. ast_write32(ast, 0x12088, data);
  318. data = ast_read32(ast, 0x1208c);
  319. /* multi-pins for DVO single-edge */
  320. data |= 0x000000cf;
  321. ast_write32(ast, 0x1208c, data);
  322. data = ast_read32(ast, 0x120a4);
  323. /* multi-pins for DVO single-edge */
  324. data |= 0xffff0000;
  325. ast_write32(ast, 0x120a4, data);
  326. data = ast_read32(ast, 0x120a8);
  327. /* multi-pins for DVO single-edge */
  328. data |= 0x0000000f;
  329. ast_write32(ast, 0x120a8, data);
  330. data = ast_read32(ast, 0x12094);
  331. /* multi-pins for DVO single-edge */
  332. data |= 0x00000002;
  333. ast_write32(ast, 0x12094, data);
  334. }
  335. }
  336. /* Force to DVO */
  337. data = ast_read32(ast, 0x1202c);
  338. data &= 0xfffbffff;
  339. ast_write32(ast, 0x1202c, data);
  340. /* Init VGA DVO Settings */
  341. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xcf, 0x80);
  342. return true;
  343. }
  344. static void ast_init_analog(struct drm_device *dev)
  345. {
  346. struct ast_private *ast = to_ast_private(dev);
  347. u32 data;
  348. /*
  349. * Set DAC source to VGA mode in SCU2C via the P2A
  350. * bridge. First configure the P2U to target the SCU
  351. * in case it isn't at this stage.
  352. */
  353. ast_write32(ast, 0xf004, 0x1e6e0000);
  354. ast_write32(ast, 0xf000, 0x1);
  355. /* Then unlock the SCU with the magic password */
  356. ast_write32(ast, 0x12000, 0x1688a8a8);
  357. ast_write32(ast, 0x12000, 0x1688a8a8);
  358. ast_write32(ast, 0x12000, 0x1688a8a8);
  359. /* Finally, clear bits [17:16] of SCU2c */
  360. data = ast_read32(ast, 0x1202c);
  361. data &= 0xfffcffff;
  362. ast_write32(ast, 0, data);
  363. /* Disable DVO */
  364. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xcf, 0x00);
  365. }
  366. void ast_init_3rdtx(struct drm_device *dev)
  367. {
  368. struct ast_private *ast = to_ast_private(dev);
  369. u8 jreg;
  370. if (ast->chip == AST2300 || ast->chip == AST2400) {
  371. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
  372. switch (jreg & 0x0e) {
  373. case 0x04:
  374. ast_init_dvo(dev);
  375. break;
  376. case 0x08:
  377. ast_launch_m68k(dev);
  378. break;
  379. case 0x0c:
  380. ast_init_dvo(dev);
  381. break;
  382. default:
  383. if (ast->tx_chip_types & BIT(AST_TX_SIL164))
  384. ast_init_dvo(dev);
  385. else
  386. ast_init_analog(dev);
  387. }
  388. }
  389. }