ast_dp501.c 9.5 KB

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