ast_dp501.c 9.7 KB

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