1 /* SPDX-License-Identifier: MIT */
3 * Authors: Dave Airlie <airlied@redhat.com>
6 #include <device/pci_def.h>
9 #include "ast_dram_tables.h"
11 void ast_set_index_reg_mask(struct ast_private
*ast
,
12 uint32_t base
, uint8_t index
,
13 uint8_t mask
, uint8_t val
)
16 ast_io_write8(ast
, base
, index
);
17 tmp
= (ast_io_read8(ast
, base
+ 1) & mask
) | val
;
18 ast_set_index_reg(ast
, base
, index
, tmp
);
21 uint8_t ast_get_index_reg(struct ast_private
*ast
,
22 uint32_t base
, uint8_t index
)
25 ast_io_write8(ast
, base
, index
);
26 ret
= ast_io_read8(ast
, base
+ 1);
30 uint8_t ast_get_index_reg_mask(struct ast_private
*ast
,
31 uint32_t base
, uint8_t index
, uint8_t mask
)
34 ast_io_write8(ast
, base
, index
);
35 ret
= ast_io_read8(ast
, base
+ 1) & mask
;
39 static void ast_detect_config_mode(struct drm_device
*dev
, u32
*scu_rev
)
41 struct ast_private
*ast
= dev
->dev_private
;
42 uint32_t data
, jregd0
, jregd1
;
45 ast
->config_mode
= ast_use_defaults
;
46 *scu_rev
= 0xffffffff;
48 /* Not all families have a P2A bridge */
49 if (dev
->pdev
->device
!= PCI_CHIP_AST2000
)
53 * The BMC will set SCU 0x40 D[12] to 1 if the P2 bridge
54 * is disabled. We force using P2A if VGA only mode bit
57 jregd0
= ast_get_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0xd0, 0xff);
58 jregd1
= ast_get_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0xd1, 0xff);
59 if (!(jregd0
& 0x80) || !(jregd1
& 0x10)) {
60 /* Double check it's actually working */
61 data
= ast_read32(ast
, 0xf004);
62 if (data
!= 0xFFFFFFFF) {
63 /* P2A works, grab silicon revision */
64 ast
->config_mode
= ast_use_p2a
;
66 DRM_INFO("Using P2A bridge for configuration\n");
68 /* Read SCU7c (silicon revision register) */
69 ast_write32(ast
, 0xf004, 0x1e6e0000);
70 ast_write32(ast
, 0xf000, 0x1);
71 *scu_rev
= ast_read32(ast
, 0x1207c);
76 /* We have a P2A bridge but it's disabled */
77 DRM_INFO("P2A bridge disabled, using default configuration\n");
80 static int ast_detect_chip(struct drm_device
*dev
, bool *need_post
)
82 struct ast_private
*ast
= dev
->dev_private
;
83 uint32_t jreg
, scu_rev
;
86 * If VGA isn't enabled, we need to enable now or subsequent
87 * access to the scratch registers will fail. We also inform
88 * our caller that it needs to POST the chip
89 * (Assumption: VGA not enabled -> need to POST)
91 if (!ast_is_vga_enabled(dev
)) {
93 DRM_INFO("VGA not enabled on entry, requesting chip POST\n");
99 /* Enable extended register access */
100 ast_enable_mmio(dev
);
103 /* Find out whether P2A works or whether to use device-tree */
104 ast_detect_config_mode(dev
, &scu_rev
);
106 /* Identify chipset */
107 if (dev
->pdev
->device
== PCI_CHIP_AST1180
) {
109 DRM_INFO("AST 1180 detected\n");
112 pci_read_config_dword(ast
->dev
->pdev
, 0x08, &data
);
113 uint8_t revision
= data
& 0xff;
114 if (revision
>= 0x40) {
116 DRM_INFO("AST 2500 detected\n");
117 } else if (revision
>= 0x30) {
119 DRM_INFO("AST 2400 detected\n");
120 } else if (revision
>= 0x20) {
122 DRM_INFO("AST 2300 detected\n");
123 } else if (revision
>= 0x10) {
124 switch (scu_rev
& 0x0300) {
127 DRM_INFO("AST 1100 detected\n");
131 DRM_INFO("AST 2200 detected\n");
135 DRM_INFO("AST 2150 detected\n");
139 DRM_INFO("AST 2100 detected\n");
142 ast
->vga2_clone
= false;
145 DRM_INFO("AST 2000 detected\n");
149 /* Check if we support wide screen */
152 ast
->support_wide_screen
= true;
155 ast
->support_wide_screen
= false;
158 jreg
= ast_get_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0xd0, 0xff);
160 ast
->support_wide_screen
= true;
161 else if (jreg
& 0x01)
162 ast
->support_wide_screen
= true;
164 ast
->support_wide_screen
= false;
165 if (ast
->chip
== AST2300
&&
166 (scu_rev
& 0x300) == 0x0) /* ast1300 */
167 ast
->support_wide_screen
= true;
168 if (ast
->chip
== AST2400
&&
169 (scu_rev
& 0x300) == 0x100) /* ast1400 */
170 ast
->support_wide_screen
= true;
171 if (ast
->chip
== AST2500
&&
172 scu_rev
== 0x100) /* ast2510 */
173 ast
->support_wide_screen
= true;
178 /* Check 3rd Tx option (digital output afaik) */
179 ast
->tx_chip_type
= AST_TX_NONE
;
182 * VGACRA3 Enhanced Color Mode Register, check if DVO is already
183 * enabled, in that case, assume we have a SIL164 TMDS transmitter
185 * Don't make that assumption if we the chip wasn't enabled and
186 * is at power-on reset, otherwise we'll incorrectly "detect" a
187 * SIL164 when there is none.
190 jreg
= ast_get_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0xa3, 0xff);
192 ast
->tx_chip_type
= AST_TX_SIL164
;
195 if ((ast
->chip
== AST2300
) || (ast
->chip
== AST2400
)) {
197 * On AST2300 and 2400, look the configuration set by the SoC in
198 * the SOC scratch register #1 bits 11:8 (interestingly marked
199 * as "reserved" in the spec)
201 jreg
= ast_get_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0xd1, 0xff);
204 ast
->tx_chip_type
= AST_TX_SIL164
;
207 ast
->dp501_fw_addr
= kzalloc(32*1024, GFP_KERNEL
);
208 if (ast
->dp501_fw_addr
) {
209 /* backup firmware */
210 if (ast_backup_fw(dev
, ast
->dp501_fw_addr
, 32*1024)) {
211 kfree(ast
->dp501_fw_addr
);
212 ast
->dp501_fw_addr
= NULL
;
217 ast
->tx_chip_type
= AST_TX_DP501
;
221 /* Print stuff for diagnostic purposes */
222 switch (ast
->tx_chip_type
) {
224 DRM_INFO("Using Sil164 TMDS transmitter\n");
227 DRM_INFO("Using DP501 DisplayPort transmitter\n");
230 DRM_INFO("Analog VGA only\n");
235 static int ast_get_dram_info(struct drm_device
*dev
)
237 struct ast_private
*ast
= dev
->dev_private
;
238 uint32_t mcr_cfg
, mcr_scu_mpll
, mcr_scu_strap
;
239 uint32_t denum
, num
, div
, ref_pll
, dsel
;
241 switch (ast
->config_mode
) {
244 * If some properties are missing, use reasonable
245 * defaults for AST2400
247 mcr_cfg
= 0x00000577;
248 mcr_scu_mpll
= 0x000050C0;
252 ast_write32(ast
, 0xf004, 0x1e6e0000);
253 ast_write32(ast
, 0xf000, 0x1);
254 mcr_cfg
= ast_read32(ast
, 0x10004);
255 mcr_scu_mpll
= ast_read32(ast
, 0x10120);
256 mcr_scu_strap
= ast_read32(ast
, 0x10170);
258 case ast_use_defaults
:
260 ast
->dram_bus_width
= 16;
261 ast
->dram_type
= AST_DRAM_1Gx16
;
262 if (ast
->chip
== AST2500
)
270 ast
->dram_bus_width
= 16;
272 ast
->dram_bus_width
= 32;
274 if (ast
->chip
== AST2500
) {
275 switch (mcr_cfg
& 0x03) {
277 ast
->dram_type
= AST_DRAM_1Gx16
;
281 ast
->dram_type
= AST_DRAM_2Gx16
;
284 ast
->dram_type
= AST_DRAM_4Gx16
;
287 ast
->dram_type
= AST_DRAM_8Gx16
;
290 } else if (ast
->chip
== AST2300
|| ast
->chip
== AST2400
) {
291 switch (mcr_cfg
& 0x03) {
293 ast
->dram_type
= AST_DRAM_512Mx16
;
297 ast
->dram_type
= AST_DRAM_1Gx16
;
300 ast
->dram_type
= AST_DRAM_2Gx16
;
303 ast
->dram_type
= AST_DRAM_4Gx16
;
307 switch (mcr_cfg
& 0x0c) {
310 ast
->dram_type
= AST_DRAM_512Mx16
;
314 ast
->dram_type
= AST_DRAM_1Gx16
;
316 ast
->dram_type
= AST_DRAM_512Mx32
;
319 ast
->dram_type
= AST_DRAM_1Gx32
;
324 if (mcr_scu_strap
& 0x2000)
329 denum
= mcr_scu_mpll
& 0x1f;
330 num
= (mcr_scu_mpll
& 0x3fe0) >> 5;
331 dsel
= (mcr_scu_mpll
& 0xc000) >> 14;
344 ast
->mclk
= ref_pll
* (num
+ 2) / (denum
+ 2) * (div
* 1000);
348 static u32
ast_get_vram_info(struct drm_device
*dev
)
350 struct ast_private
*ast
= dev
->dev_private
;
355 vram_size
= AST_VIDMEM_DEFAULT_SIZE
;
356 jreg
= ast_get_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0xaa, 0xff);
358 case 0: vram_size
= AST_VIDMEM_SIZE_8M
; break;
359 case 1: vram_size
= AST_VIDMEM_SIZE_16M
; break;
360 case 2: vram_size
= AST_VIDMEM_SIZE_32M
; break;
361 case 3: vram_size
= AST_VIDMEM_SIZE_64M
; break;
364 jreg
= ast_get_index_reg_mask(ast
, AST_IO_CRTC_PORT
, 0x99, 0xff);
365 switch (jreg
& 0x03) {
367 vram_size
-= 0x100000;
370 vram_size
-= 0x200000;
373 vram_size
-= 0x400000;
380 int ast_driver_load(struct drm_device
*dev
, unsigned long flags
)
382 struct ast_private
*ast
;
385 struct resource
*res
;
387 ast
= kzalloc(sizeof(struct ast_private
), GFP_KERNEL
);
391 dev
->dev_private
= ast
;
395 res
= probe_resource(dev
->pdev
, PCI_BASE_ADDRESS_1
);
397 dev_err(dev
->pdev
, "BAR1 resource not found.\n");
401 ast
->regs
= res2mmio(res
, 0, 0);
408 ast
->io_space_uses_mmap
= false;
409 res
= probe_resource(dev
->pdev
, PCI_BASE_ADDRESS_2
);
411 dev_err(dev
->pdev
, "BAR2 resource not found.\n");
414 * If we don't have IO space at all, use MMIO now and
415 * assume the chip has MMIO enabled by default (rev 0x20
418 if (!res
|| !(res
->flags
& IORESOURCE_IO
)) {
419 DRM_INFO("platform has no IO space, trying MMIO\n");
420 ast
->ioregs
= ast
->regs
+ AST_IO_MM_OFFSET
;
421 ast
->io_space_uses_mmap
= true;
424 /* "map" IO regs if the above hasn't done so already */
426 ast
->ioregs
= res2mmio(res
, 0, 0);
433 ast_detect_chip(dev
, &need_post
);
438 if (ast
->chip
!= AST1180
) {
439 ret
= ast_get_dram_info(dev
);
442 ast
->vram_size
= ast_get_vram_info(dev
);
443 DRM_INFO("dram MCLK=%u Mhz type=%d bus_width=%d size=%08x\n",
444 ast
->mclk
, ast
->dram_type
,
445 ast
->dram_bus_width
, ast
->vram_size
);
451 dev
->dev_private
= NULL
;