ast_main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sub license, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  15. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  16. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  17. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  18. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. *
  20. * The above copyright notice and this permission notice (including the
  21. * next paragraph) shall be included in all copies or substantial portions
  22. * of the Software.
  23. *
  24. */
  25. /*
  26. * Authors: Dave Airlie <airlied@redhat.com>
  27. */
  28. #include <linux/of.h>
  29. #include <linux/pci.h>
  30. #include <drm/drm_atomic_helper.h>
  31. #include <drm/drm_drv.h>
  32. #include <drm/drm_gem.h>
  33. #include <drm/drm_managed.h>
  34. #include "ast_drv.h"
  35. static void ast_detect_widescreen(struct ast_device *ast)
  36. {
  37. u8 jreg;
  38. /* Check if we support wide screen */
  39. switch (AST_GEN(ast)) {
  40. case 1:
  41. ast->support_wide_screen = false;
  42. break;
  43. default:
  44. jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd0, 0xff);
  45. if (!(jreg & 0x80))
  46. ast->support_wide_screen = true;
  47. else if (jreg & 0x01)
  48. ast->support_wide_screen = true;
  49. else {
  50. ast->support_wide_screen = false;
  51. if (ast->chip == AST1300)
  52. ast->support_wide_screen = true;
  53. if (ast->chip == AST1400)
  54. ast->support_wide_screen = true;
  55. if (ast->chip == AST2510)
  56. ast->support_wide_screen = true;
  57. if (IS_AST_GEN7(ast))
  58. ast->support_wide_screen = true;
  59. }
  60. break;
  61. }
  62. }
  63. static void ast_detect_tx_chip(struct ast_device *ast, bool need_post)
  64. {
  65. struct drm_device *dev = &ast->base;
  66. u8 jreg;
  67. /* Check 3rd Tx option (digital output afaik) */
  68. ast->tx_chip_types |= AST_TX_NONE_BIT;
  69. /*
  70. * VGACRA3 Enhanced Color Mode Register, check if DVO is already
  71. * enabled, in that case, assume we have a SIL164 TMDS transmitter
  72. *
  73. * Don't make that assumption if we the chip wasn't enabled and
  74. * is at power-on reset, otherwise we'll incorrectly "detect" a
  75. * SIL164 when there is none.
  76. */
  77. if (!need_post) {
  78. jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xa3, 0xff);
  79. if (jreg & 0x80)
  80. ast->tx_chip_types = AST_TX_SIL164_BIT;
  81. }
  82. if (IS_AST_GEN4(ast) || IS_AST_GEN5(ast) || IS_AST_GEN6(ast)) {
  83. /*
  84. * On AST GEN4+, look the configuration set by the SoC in
  85. * the SOC scratch register #1 bits 11:8 (interestingly marked
  86. * as "reserved" in the spec)
  87. */
  88. jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd1, 0xff);
  89. switch (jreg) {
  90. case 0x04:
  91. ast->tx_chip_types = AST_TX_SIL164_BIT;
  92. break;
  93. case 0x08:
  94. ast->dp501_fw_addr = drmm_kzalloc(dev, 32*1024, GFP_KERNEL);
  95. if (ast->dp501_fw_addr) {
  96. /* backup firmware */
  97. if (ast_backup_fw(dev, ast->dp501_fw_addr, 32*1024)) {
  98. drmm_kfree(dev, ast->dp501_fw_addr);
  99. ast->dp501_fw_addr = NULL;
  100. }
  101. }
  102. fallthrough;
  103. case 0x0c:
  104. ast->tx_chip_types = AST_TX_DP501_BIT;
  105. }
  106. } else if (IS_AST_GEN7(ast)) {
  107. if (ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xD1, TX_TYPE_MASK) ==
  108. ASTDP_DPMCU_TX) {
  109. int ret = ast_dp_launch(ast);
  110. if (!ret)
  111. ast->tx_chip_types = AST_TX_ASTDP_BIT;
  112. }
  113. }
  114. /* Print stuff for diagnostic purposes */
  115. if (ast->tx_chip_types & AST_TX_NONE_BIT)
  116. drm_info(dev, "Using analog VGA\n");
  117. if (ast->tx_chip_types & AST_TX_SIL164_BIT)
  118. drm_info(dev, "Using Sil164 TMDS transmitter\n");
  119. if (ast->tx_chip_types & AST_TX_DP501_BIT)
  120. drm_info(dev, "Using DP501 DisplayPort transmitter\n");
  121. if (ast->tx_chip_types & AST_TX_ASTDP_BIT)
  122. drm_info(dev, "Using ASPEED DisplayPort transmitter\n");
  123. }
  124. static int ast_get_dram_info(struct drm_device *dev)
  125. {
  126. struct device_node *np = dev->dev->of_node;
  127. struct ast_device *ast = to_ast_device(dev);
  128. uint32_t mcr_cfg, mcr_scu_mpll, mcr_scu_strap;
  129. uint32_t denum, num, div, ref_pll, dsel;
  130. switch (ast->config_mode) {
  131. case ast_use_dt:
  132. /*
  133. * If some properties are missing, use reasonable
  134. * defaults for GEN5
  135. */
  136. if (of_property_read_u32(np, "aspeed,mcr-configuration",
  137. &mcr_cfg))
  138. mcr_cfg = 0x00000577;
  139. if (of_property_read_u32(np, "aspeed,mcr-scu-mpll",
  140. &mcr_scu_mpll))
  141. mcr_scu_mpll = 0x000050C0;
  142. if (of_property_read_u32(np, "aspeed,mcr-scu-strap",
  143. &mcr_scu_strap))
  144. mcr_scu_strap = 0;
  145. break;
  146. case ast_use_p2a:
  147. ast_write32(ast, 0xf004, 0x1e6e0000);
  148. ast_write32(ast, 0xf000, 0x1);
  149. mcr_cfg = ast_read32(ast, 0x10004);
  150. mcr_scu_mpll = ast_read32(ast, 0x10120);
  151. mcr_scu_strap = ast_read32(ast, 0x10170);
  152. break;
  153. case ast_use_defaults:
  154. default:
  155. ast->dram_bus_width = 16;
  156. ast->dram_type = AST_DRAM_1Gx16;
  157. if (IS_AST_GEN6(ast))
  158. ast->mclk = 800;
  159. else
  160. ast->mclk = 396;
  161. return 0;
  162. }
  163. if (mcr_cfg & 0x40)
  164. ast->dram_bus_width = 16;
  165. else
  166. ast->dram_bus_width = 32;
  167. if (IS_AST_GEN6(ast)) {
  168. switch (mcr_cfg & 0x03) {
  169. case 0:
  170. ast->dram_type = AST_DRAM_1Gx16;
  171. break;
  172. default:
  173. case 1:
  174. ast->dram_type = AST_DRAM_2Gx16;
  175. break;
  176. case 2:
  177. ast->dram_type = AST_DRAM_4Gx16;
  178. break;
  179. case 3:
  180. ast->dram_type = AST_DRAM_8Gx16;
  181. break;
  182. }
  183. } else if (IS_AST_GEN4(ast) || IS_AST_GEN5(ast)) {
  184. switch (mcr_cfg & 0x03) {
  185. case 0:
  186. ast->dram_type = AST_DRAM_512Mx16;
  187. break;
  188. default:
  189. case 1:
  190. ast->dram_type = AST_DRAM_1Gx16;
  191. break;
  192. case 2:
  193. ast->dram_type = AST_DRAM_2Gx16;
  194. break;
  195. case 3:
  196. ast->dram_type = AST_DRAM_4Gx16;
  197. break;
  198. }
  199. } else {
  200. switch (mcr_cfg & 0x0c) {
  201. case 0:
  202. case 4:
  203. ast->dram_type = AST_DRAM_512Mx16;
  204. break;
  205. case 8:
  206. if (mcr_cfg & 0x40)
  207. ast->dram_type = AST_DRAM_1Gx16;
  208. else
  209. ast->dram_type = AST_DRAM_512Mx32;
  210. break;
  211. case 0xc:
  212. ast->dram_type = AST_DRAM_1Gx32;
  213. break;
  214. }
  215. }
  216. if (mcr_scu_strap & 0x2000)
  217. ref_pll = 14318;
  218. else
  219. ref_pll = 12000;
  220. denum = mcr_scu_mpll & 0x1f;
  221. num = (mcr_scu_mpll & 0x3fe0) >> 5;
  222. dsel = (mcr_scu_mpll & 0xc000) >> 14;
  223. switch (dsel) {
  224. case 3:
  225. div = 0x4;
  226. break;
  227. case 2:
  228. case 1:
  229. div = 0x2;
  230. break;
  231. default:
  232. div = 0x1;
  233. break;
  234. }
  235. ast->mclk = ref_pll * (num + 2) / ((denum + 2) * (div * 1000));
  236. return 0;
  237. }
  238. struct drm_device *ast_device_create(struct pci_dev *pdev,
  239. const struct drm_driver *drv,
  240. enum ast_chip chip,
  241. enum ast_config_mode config_mode,
  242. void __iomem *regs,
  243. void __iomem *ioregs,
  244. bool need_post)
  245. {
  246. struct drm_device *dev;
  247. struct ast_device *ast;
  248. int ret;
  249. ast = devm_drm_dev_alloc(&pdev->dev, drv, struct ast_device, base);
  250. if (IS_ERR(ast))
  251. return ERR_CAST(ast);
  252. dev = &ast->base;
  253. ast->chip = chip;
  254. ast->config_mode = config_mode;
  255. ast->regs = regs;
  256. ast->ioregs = ioregs;
  257. ast_detect_widescreen(ast);
  258. ast_detect_tx_chip(ast, need_post);
  259. ret = ast_get_dram_info(dev);
  260. if (ret)
  261. return ERR_PTR(ret);
  262. drm_info(dev, "dram MCLK=%u Mhz type=%d bus_width=%d\n",
  263. ast->mclk, ast->dram_type, ast->dram_bus_width);
  264. if (need_post)
  265. ast_post_gpu(dev);
  266. ret = ast_mm_init(ast);
  267. if (ret)
  268. return ERR_PTR(ret);
  269. /* map reserved buffer */
  270. ast->dp501_fw_buf = NULL;
  271. if (ast->vram_size < pci_resource_len(pdev, 0)) {
  272. ast->dp501_fw_buf = pci_iomap_range(pdev, 0, ast->vram_size, 0);
  273. if (!ast->dp501_fw_buf)
  274. drm_info(dev, "failed to map reserved buffer!\n");
  275. }
  276. ret = ast_mode_config_init(ast);
  277. if (ret)
  278. return ERR_PTR(ret);
  279. return dev;
  280. }