ast_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 <[email protected]>
  27. */
  28. #include <linux/pci.h>
  29. #include <drm/drm_atomic_helper.h>
  30. #include <drm/drm_crtc_helper.h>
  31. #include <drm/drm_drv.h>
  32. #include <drm/drm_gem.h>
  33. #include <drm/drm_gem_vram_helper.h>
  34. #include <drm/drm_managed.h>
  35. #include "ast_drv.h"
  36. void ast_set_index_reg_mask(struct ast_private *ast,
  37. uint32_t base, uint8_t index,
  38. uint8_t mask, uint8_t val)
  39. {
  40. u8 tmp;
  41. ast_io_write8(ast, base, index);
  42. tmp = (ast_io_read8(ast, base + 1) & mask) | val;
  43. ast_set_index_reg(ast, base, index, tmp);
  44. }
  45. uint8_t ast_get_index_reg(struct ast_private *ast,
  46. uint32_t base, uint8_t index)
  47. {
  48. uint8_t ret;
  49. ast_io_write8(ast, base, index);
  50. ret = ast_io_read8(ast, base + 1);
  51. return ret;
  52. }
  53. uint8_t ast_get_index_reg_mask(struct ast_private *ast,
  54. uint32_t base, uint8_t index, uint8_t mask)
  55. {
  56. uint8_t ret;
  57. ast_io_write8(ast, base, index);
  58. ret = ast_io_read8(ast, base + 1) & mask;
  59. return ret;
  60. }
  61. static void ast_detect_config_mode(struct drm_device *dev, u32 *scu_rev)
  62. {
  63. struct device_node *np = dev->dev->of_node;
  64. struct ast_private *ast = to_ast_private(dev);
  65. struct pci_dev *pdev = to_pci_dev(dev->dev);
  66. uint32_t data, jregd0, jregd1;
  67. /* Defaults */
  68. ast->config_mode = ast_use_defaults;
  69. *scu_rev = 0xffffffff;
  70. /* Check if we have device-tree properties */
  71. if (np && !of_property_read_u32(np, "aspeed,scu-revision-id",
  72. scu_rev)) {
  73. /* We do, disable P2A access */
  74. ast->config_mode = ast_use_dt;
  75. drm_info(dev, "Using device-tree for configuration\n");
  76. return;
  77. }
  78. /* Not all families have a P2A bridge */
  79. if (pdev->device != PCI_CHIP_AST2000)
  80. return;
  81. /*
  82. * The BMC will set SCU 0x40 D[12] to 1 if the P2 bridge
  83. * is disabled. We force using P2A if VGA only mode bit
  84. * is set D[7]
  85. */
  86. jregd0 = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
  87. jregd1 = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
  88. if (!(jregd0 & 0x80) || !(jregd1 & 0x10)) {
  89. /* Patch AST2500 */
  90. if (((pdev->revision & 0xF0) == 0x40)
  91. && ((jregd0 & AST_VRAM_INIT_STATUS_MASK) == 0))
  92. ast_patch_ahb_2500(ast);
  93. /* Double check it's actually working */
  94. data = ast_read32(ast, 0xf004);
  95. if ((data != 0xFFFFFFFF) && (data != 0x00)) {
  96. /* P2A works, grab silicon revision */
  97. ast->config_mode = ast_use_p2a;
  98. drm_info(dev, "Using P2A bridge for configuration\n");
  99. /* Read SCU7c (silicon revision register) */
  100. ast_write32(ast, 0xf004, 0x1e6e0000);
  101. ast_write32(ast, 0xf000, 0x1);
  102. *scu_rev = ast_read32(ast, 0x1207c);
  103. return;
  104. }
  105. }
  106. /* We have a P2A bridge but it's disabled */
  107. drm_info(dev, "P2A bridge disabled, using default configuration\n");
  108. }
  109. static int ast_detect_chip(struct drm_device *dev, bool *need_post)
  110. {
  111. struct ast_private *ast = to_ast_private(dev);
  112. struct pci_dev *pdev = to_pci_dev(dev->dev);
  113. uint32_t jreg, scu_rev;
  114. /*
  115. * If VGA isn't enabled, we need to enable now or subsequent
  116. * access to the scratch registers will fail. We also inform
  117. * our caller that it needs to POST the chip
  118. * (Assumption: VGA not enabled -> need to POST)
  119. */
  120. if (!ast_is_vga_enabled(dev)) {
  121. ast_enable_vga(dev);
  122. drm_info(dev, "VGA not enabled on entry, requesting chip POST\n");
  123. *need_post = true;
  124. } else
  125. *need_post = false;
  126. /* Enable extended register access */
  127. ast_open_key(ast);
  128. ast_enable_mmio(dev);
  129. /* Find out whether P2A works or whether to use device-tree */
  130. ast_detect_config_mode(dev, &scu_rev);
  131. /* Identify chipset */
  132. if (pdev->revision >= 0x50) {
  133. ast->chip = AST2600;
  134. drm_info(dev, "AST 2600 detected\n");
  135. } else if (pdev->revision >= 0x40) {
  136. ast->chip = AST2500;
  137. drm_info(dev, "AST 2500 detected\n");
  138. } else if (pdev->revision >= 0x30) {
  139. ast->chip = AST2400;
  140. drm_info(dev, "AST 2400 detected\n");
  141. } else if (pdev->revision >= 0x20) {
  142. ast->chip = AST2300;
  143. drm_info(dev, "AST 2300 detected\n");
  144. } else if (pdev->revision >= 0x10) {
  145. switch (scu_rev & 0x0300) {
  146. case 0x0200:
  147. ast->chip = AST1100;
  148. drm_info(dev, "AST 1100 detected\n");
  149. break;
  150. case 0x0100:
  151. ast->chip = AST2200;
  152. drm_info(dev, "AST 2200 detected\n");
  153. break;
  154. case 0x0000:
  155. ast->chip = AST2150;
  156. drm_info(dev, "AST 2150 detected\n");
  157. break;
  158. default:
  159. ast->chip = AST2100;
  160. drm_info(dev, "AST 2100 detected\n");
  161. break;
  162. }
  163. ast->vga2_clone = false;
  164. } else {
  165. ast->chip = AST2000;
  166. drm_info(dev, "AST 2000 detected\n");
  167. }
  168. /* Check if we support wide screen */
  169. switch (ast->chip) {
  170. case AST2000:
  171. ast->support_wide_screen = false;
  172. break;
  173. default:
  174. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
  175. if (!(jreg & 0x80))
  176. ast->support_wide_screen = true;
  177. else if (jreg & 0x01)
  178. ast->support_wide_screen = true;
  179. else {
  180. ast->support_wide_screen = false;
  181. if (ast->chip == AST2300 &&
  182. (scu_rev & 0x300) == 0x0) /* ast1300 */
  183. ast->support_wide_screen = true;
  184. if (ast->chip == AST2400 &&
  185. (scu_rev & 0x300) == 0x100) /* ast1400 */
  186. ast->support_wide_screen = true;
  187. if (ast->chip == AST2500 &&
  188. scu_rev == 0x100) /* ast2510 */
  189. ast->support_wide_screen = true;
  190. if (ast->chip == AST2600) /* ast2600 */
  191. ast->support_wide_screen = true;
  192. }
  193. break;
  194. }
  195. /* Check 3rd Tx option (digital output afaik) */
  196. ast->tx_chip_types |= AST_TX_NONE_BIT;
  197. /*
  198. * VGACRA3 Enhanced Color Mode Register, check if DVO is already
  199. * enabled, in that case, assume we have a SIL164 TMDS transmitter
  200. *
  201. * Don't make that assumption if we the chip wasn't enabled and
  202. * is at power-on reset, otherwise we'll incorrectly "detect" a
  203. * SIL164 when there is none.
  204. */
  205. if (!*need_post) {
  206. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xff);
  207. if (jreg & 0x80)
  208. ast->tx_chip_types = AST_TX_SIL164_BIT;
  209. }
  210. if ((ast->chip == AST2300) || (ast->chip == AST2400) || (ast->chip == AST2500)) {
  211. /*
  212. * On AST2300 and 2400, look the configuration set by the SoC in
  213. * the SOC scratch register #1 bits 11:8 (interestingly marked
  214. * as "reserved" in the spec)
  215. */
  216. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
  217. switch (jreg) {
  218. case 0x04:
  219. ast->tx_chip_types = AST_TX_SIL164_BIT;
  220. break;
  221. case 0x08:
  222. ast->dp501_fw_addr = drmm_kzalloc(dev, 32*1024, GFP_KERNEL);
  223. if (ast->dp501_fw_addr) {
  224. /* backup firmware */
  225. if (ast_backup_fw(dev, ast->dp501_fw_addr, 32*1024)) {
  226. drmm_kfree(dev, ast->dp501_fw_addr);
  227. ast->dp501_fw_addr = NULL;
  228. }
  229. }
  230. fallthrough;
  231. case 0x0c:
  232. ast->tx_chip_types = AST_TX_DP501_BIT;
  233. }
  234. } else if (ast->chip == AST2600)
  235. ast_dp_launch(&ast->base, 0);
  236. /* Print stuff for diagnostic purposes */
  237. if (ast->tx_chip_types & AST_TX_NONE_BIT)
  238. drm_info(dev, "Using analog VGA\n");
  239. if (ast->tx_chip_types & AST_TX_SIL164_BIT)
  240. drm_info(dev, "Using Sil164 TMDS transmitter\n");
  241. if (ast->tx_chip_types & AST_TX_DP501_BIT)
  242. drm_info(dev, "Using DP501 DisplayPort transmitter\n");
  243. return 0;
  244. }
  245. static int ast_get_dram_info(struct drm_device *dev)
  246. {
  247. struct device_node *np = dev->dev->of_node;
  248. struct ast_private *ast = to_ast_private(dev);
  249. uint32_t mcr_cfg, mcr_scu_mpll, mcr_scu_strap;
  250. uint32_t denum, num, div, ref_pll, dsel;
  251. switch (ast->config_mode) {
  252. case ast_use_dt:
  253. /*
  254. * If some properties are missing, use reasonable
  255. * defaults for AST2400
  256. */
  257. if (of_property_read_u32(np, "aspeed,mcr-configuration",
  258. &mcr_cfg))
  259. mcr_cfg = 0x00000577;
  260. if (of_property_read_u32(np, "aspeed,mcr-scu-mpll",
  261. &mcr_scu_mpll))
  262. mcr_scu_mpll = 0x000050C0;
  263. if (of_property_read_u32(np, "aspeed,mcr-scu-strap",
  264. &mcr_scu_strap))
  265. mcr_scu_strap = 0;
  266. break;
  267. case ast_use_p2a:
  268. ast_write32(ast, 0xf004, 0x1e6e0000);
  269. ast_write32(ast, 0xf000, 0x1);
  270. mcr_cfg = ast_read32(ast, 0x10004);
  271. mcr_scu_mpll = ast_read32(ast, 0x10120);
  272. mcr_scu_strap = ast_read32(ast, 0x10170);
  273. break;
  274. case ast_use_defaults:
  275. default:
  276. ast->dram_bus_width = 16;
  277. ast->dram_type = AST_DRAM_1Gx16;
  278. if (ast->chip == AST2500)
  279. ast->mclk = 800;
  280. else
  281. ast->mclk = 396;
  282. return 0;
  283. }
  284. if (mcr_cfg & 0x40)
  285. ast->dram_bus_width = 16;
  286. else
  287. ast->dram_bus_width = 32;
  288. if (ast->chip == AST2500) {
  289. switch (mcr_cfg & 0x03) {
  290. case 0:
  291. ast->dram_type = AST_DRAM_1Gx16;
  292. break;
  293. default:
  294. case 1:
  295. ast->dram_type = AST_DRAM_2Gx16;
  296. break;
  297. case 2:
  298. ast->dram_type = AST_DRAM_4Gx16;
  299. break;
  300. case 3:
  301. ast->dram_type = AST_DRAM_8Gx16;
  302. break;
  303. }
  304. } else if (ast->chip == AST2300 || ast->chip == AST2400) {
  305. switch (mcr_cfg & 0x03) {
  306. case 0:
  307. ast->dram_type = AST_DRAM_512Mx16;
  308. break;
  309. default:
  310. case 1:
  311. ast->dram_type = AST_DRAM_1Gx16;
  312. break;
  313. case 2:
  314. ast->dram_type = AST_DRAM_2Gx16;
  315. break;
  316. case 3:
  317. ast->dram_type = AST_DRAM_4Gx16;
  318. break;
  319. }
  320. } else {
  321. switch (mcr_cfg & 0x0c) {
  322. case 0:
  323. case 4:
  324. ast->dram_type = AST_DRAM_512Mx16;
  325. break;
  326. case 8:
  327. if (mcr_cfg & 0x40)
  328. ast->dram_type = AST_DRAM_1Gx16;
  329. else
  330. ast->dram_type = AST_DRAM_512Mx32;
  331. break;
  332. case 0xc:
  333. ast->dram_type = AST_DRAM_1Gx32;
  334. break;
  335. }
  336. }
  337. if (mcr_scu_strap & 0x2000)
  338. ref_pll = 14318;
  339. else
  340. ref_pll = 12000;
  341. denum = mcr_scu_mpll & 0x1f;
  342. num = (mcr_scu_mpll & 0x3fe0) >> 5;
  343. dsel = (mcr_scu_mpll & 0xc000) >> 14;
  344. switch (dsel) {
  345. case 3:
  346. div = 0x4;
  347. break;
  348. case 2:
  349. case 1:
  350. div = 0x2;
  351. break;
  352. default:
  353. div = 0x1;
  354. break;
  355. }
  356. ast->mclk = ref_pll * (num + 2) / ((denum + 2) * (div * 1000));
  357. return 0;
  358. }
  359. /*
  360. * Run this function as part of the HW device cleanup; not
  361. * when the DRM device gets released.
  362. */
  363. static void ast_device_release(void *data)
  364. {
  365. struct ast_private *ast = data;
  366. /* enable standard VGA decode */
  367. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa1, 0x04);
  368. }
  369. struct ast_private *ast_device_create(const struct drm_driver *drv,
  370. struct pci_dev *pdev,
  371. unsigned long flags)
  372. {
  373. struct drm_device *dev;
  374. struct ast_private *ast;
  375. bool need_post;
  376. int ret = 0;
  377. ast = devm_drm_dev_alloc(&pdev->dev, drv, struct ast_private, base);
  378. if (IS_ERR(ast))
  379. return ast;
  380. dev = &ast->base;
  381. pci_set_drvdata(pdev, dev);
  382. ret = drmm_mutex_init(dev, &ast->ioregs_lock);
  383. if (ret)
  384. return ERR_PTR(ret);
  385. ast->regs = pcim_iomap(pdev, 1, 0);
  386. if (!ast->regs)
  387. return ERR_PTR(-EIO);
  388. /*
  389. * After AST2500, MMIO is enabled by default, and it should be adopted
  390. * to be compatible with Arm.
  391. */
  392. if (pdev->revision >= 0x40) {
  393. ast->ioregs = ast->regs + AST_IO_MM_OFFSET;
  394. } else if (!(pci_resource_flags(pdev, 2) & IORESOURCE_IO)) {
  395. drm_info(dev, "platform has no IO space, trying MMIO\n");
  396. ast->ioregs = ast->regs + AST_IO_MM_OFFSET;
  397. }
  398. /* "map" IO regs if the above hasn't done so already */
  399. if (!ast->ioregs) {
  400. ast->ioregs = pcim_iomap(pdev, 2, 0);
  401. if (!ast->ioregs)
  402. return ERR_PTR(-EIO);
  403. }
  404. ast_detect_chip(dev, &need_post);
  405. ret = ast_get_dram_info(dev);
  406. if (ret)
  407. return ERR_PTR(ret);
  408. drm_info(dev, "dram MCLK=%u Mhz type=%d bus_width=%d\n",
  409. ast->mclk, ast->dram_type, ast->dram_bus_width);
  410. if (need_post)
  411. ast_post_gpu(dev);
  412. ret = ast_mm_init(ast);
  413. if (ret)
  414. return ERR_PTR(ret);
  415. /* map reserved buffer */
  416. ast->dp501_fw_buf = NULL;
  417. if (dev->vram_mm->vram_size < pci_resource_len(pdev, 0)) {
  418. ast->dp501_fw_buf = pci_iomap_range(pdev, 0, dev->vram_mm->vram_size, 0);
  419. if (!ast->dp501_fw_buf)
  420. drm_info(dev, "failed to map reserved buffer!\n");
  421. }
  422. ret = ast_mode_config_init(ast);
  423. if (ret)
  424. return ERR_PTR(ret);
  425. ret = devm_add_action_or_reset(dev->dev, ast_device_release, ast);
  426. if (ret)
  427. return ERR_PTR(ret);
  428. return ast;
  429. }