2 * Copyright 2012 Red Hat Inc.
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:
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.
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
30 #include <linux/pci.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <drm/drm_drv.h>
34 #include <drm/drm_gem.h>
35 #include <drm/drm_managed.h>
39 static void ast_detect_widescreen(struct ast_device *ast)
43 /* Check if we support wide screen */
44 switch (AST_GEN(ast)) {
46 ast->support_wide_screen = false;
49 jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd0, 0xff);
51 ast->support_wide_screen = true;
53 ast->support_wide_screen = true;
55 ast->support_wide_screen = false;
56 if (ast->chip == AST1300)
57 ast->support_wide_screen = true;
58 if (ast->chip == AST1400)
59 ast->support_wide_screen = true;
60 if (ast->chip == AST2510)
61 ast->support_wide_screen = true;
63 ast->support_wide_screen = true;
69 static void ast_detect_tx_chip(struct ast_device *ast, bool need_post)
71 static const char * const info_str[] = {
73 "Sil164 TMDS transmitter",
74 "DP501 DisplayPort transmitter",
75 "ASPEED DisplayPort transmitter",
78 struct drm_device *dev = &ast->base;
82 * Several of the listed TX chips are not explicitly supported
83 * by the ast driver. If these exist in real-world devices, they
84 * are most likely reported as VGA or SIL164 outputs. We warn here
85 * to get bug reports for these devices. If none come in for some
86 * time, we can begin to fail device probing on these values.
88 vgacrd1 = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd1, AST_IO_VGACRD1_TX_TYPE_MASK);
89 drm_WARN(dev, vgacrd1 == AST_IO_VGACRD1_TX_ITE66121_VBIOS,
90 "ITE IT66121 detected, 0x%x, Gen%lu\n", vgacrd1, AST_GEN(ast));
91 drm_WARN(dev, vgacrd1 == AST_IO_VGACRD1_TX_CH7003_VBIOS,
92 "Chrontel CH7003 detected, 0x%x, Gen%lu\n", vgacrd1, AST_GEN(ast));
93 drm_WARN(dev, vgacrd1 == AST_IO_VGACRD1_TX_ANX9807_VBIOS,
94 "Analogix ANX9807 detected, 0x%x, Gen%lu\n", vgacrd1, AST_GEN(ast));
96 /* Check 3rd Tx option (digital output afaik) */
97 ast->tx_chip = AST_TX_NONE;
100 * VGACRA3 Enhanced Color Mode Register, check if DVO is already
101 * enabled, in that case, assume we have a SIL164 TMDS transmitter
103 * Don't make that assumption if we the chip wasn't enabled and
104 * is at power-on reset, otherwise we'll incorrectly "detect" a
105 * SIL164 when there is none.
108 jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xa3, 0xff);
110 ast->tx_chip = AST_TX_SIL164;
113 if (IS_AST_GEN4(ast) || IS_AST_GEN5(ast) || IS_AST_GEN6(ast)) {
115 * On AST GEN4+, look the configuration set by the SoC in
116 * the SOC scratch register #1 bits 11:8 (interestingly marked
117 * as "reserved" in the spec)
119 jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd1,
120 AST_IO_VGACRD1_TX_TYPE_MASK);
122 case AST_IO_VGACRD1_TX_SIL164_VBIOS:
123 ast->tx_chip = AST_TX_SIL164;
125 case AST_IO_VGACRD1_TX_DP501_VBIOS:
126 ast->dp501_fw_addr = drmm_kzalloc(dev, 32*1024, GFP_KERNEL);
127 if (ast->dp501_fw_addr) {
128 /* backup firmware */
129 if (ast_backup_fw(ast, ast->dp501_fw_addr, 32*1024)) {
130 drmm_kfree(dev, ast->dp501_fw_addr);
131 ast->dp501_fw_addr = NULL;
135 case AST_IO_VGACRD1_TX_FW_EMBEDDED_FW:
136 ast->tx_chip = AST_TX_DP501;
138 } else if (IS_AST_GEN7(ast)) {
139 if (ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd1, AST_IO_VGACRD1_TX_TYPE_MASK) ==
140 AST_IO_VGACRD1_TX_ASTDP) {
141 int ret = ast_dp_launch(ast);
144 ast->tx_chip = AST_TX_ASTDP;
148 drm_info(dev, "Using %s\n", info_str[ast->tx_chip]);
151 static int ast_get_dram_info(struct ast_device *ast)
153 struct drm_device *dev = &ast->base;
154 struct device_node *np = dev->dev->of_node;
155 uint32_t mcr_cfg, mcr_scu_mpll, mcr_scu_strap;
156 uint32_t denum, num, div, ref_pll, dsel;
158 switch (ast->config_mode) {
161 * If some properties are missing, use reasonable
164 if (of_property_read_u32(np, "aspeed,mcr-configuration",
166 mcr_cfg = 0x00000577;
167 if (of_property_read_u32(np, "aspeed,mcr-scu-mpll",
169 mcr_scu_mpll = 0x000050C0;
170 if (of_property_read_u32(np, "aspeed,mcr-scu-strap",
175 ast_write32(ast, 0xf004, 0x1e6e0000);
176 ast_write32(ast, 0xf000, 0x1);
177 mcr_cfg = ast_read32(ast, 0x10004);
178 mcr_scu_mpll = ast_read32(ast, 0x10120);
179 mcr_scu_strap = ast_read32(ast, 0x10170);
181 case ast_use_defaults:
183 ast->dram_bus_width = 16;
184 ast->dram_type = AST_DRAM_1Gx16;
185 if (IS_AST_GEN6(ast))
193 ast->dram_bus_width = 16;
195 ast->dram_bus_width = 32;
197 if (IS_AST_GEN6(ast)) {
198 switch (mcr_cfg & 0x03) {
200 ast->dram_type = AST_DRAM_1Gx16;
204 ast->dram_type = AST_DRAM_2Gx16;
207 ast->dram_type = AST_DRAM_4Gx16;
210 ast->dram_type = AST_DRAM_8Gx16;
213 } else if (IS_AST_GEN4(ast) || IS_AST_GEN5(ast)) {
214 switch (mcr_cfg & 0x03) {
216 ast->dram_type = AST_DRAM_512Mx16;
220 ast->dram_type = AST_DRAM_1Gx16;
223 ast->dram_type = AST_DRAM_2Gx16;
226 ast->dram_type = AST_DRAM_4Gx16;
230 switch (mcr_cfg & 0x0c) {
233 ast->dram_type = AST_DRAM_512Mx16;
237 ast->dram_type = AST_DRAM_1Gx16;
239 ast->dram_type = AST_DRAM_512Mx32;
242 ast->dram_type = AST_DRAM_1Gx32;
247 if (mcr_scu_strap & 0x2000)
252 denum = mcr_scu_mpll & 0x1f;
253 num = (mcr_scu_mpll & 0x3fe0) >> 5;
254 dsel = (mcr_scu_mpll & 0xc000) >> 14;
267 ast->mclk = ref_pll * (num + 2) / ((denum + 2) * (div * 1000));
271 struct drm_device *ast_device_create(struct pci_dev *pdev,
272 const struct drm_driver *drv,
274 enum ast_config_mode config_mode,
276 void __iomem *ioregs,
279 struct drm_device *dev;
280 struct ast_device *ast;
283 ast = devm_drm_dev_alloc(&pdev->dev, drv, struct ast_device, base);
285 return ERR_CAST(ast);
289 ast->config_mode = config_mode;
291 ast->ioregs = ioregs;
293 ast_detect_widescreen(ast);
294 ast_detect_tx_chip(ast, need_post);
296 ret = ast_get_dram_info(ast);
300 drm_info(dev, "dram MCLK=%u Mhz type=%d bus_width=%d\n",
301 ast->mclk, ast->dram_type, ast->dram_bus_width);
306 ret = ast_mm_init(ast);
310 /* map reserved buffer */
311 ast->dp501_fw_buf = NULL;
312 if (ast->vram_size < pci_resource_len(pdev, 0)) {
313 ast->dp501_fw_buf = pci_iomap_range(pdev, 0, ast->vram_size, 0);
314 if (!ast->dp501_fw_buf)
315 drm_info(dev, "failed to map reserved buffer!\n");
318 ret = ast_mode_config_init(ast);