ast_dp501.c 9.7 KB

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