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
29 #include <linux/pci.h>
31 #include <drm/drm_atomic_helper.h>
32 #include <drm/drm_drv.h>
33 #include <drm/drm_gem.h>
34 #include <drm/drm_managed.h>
38 static bool ast_is_vga_enabled(struct drm_device *dev)
40 struct ast_device *ast = to_ast_device(dev);
43 ch = ast_io_read8(ast, AST_IO_VGAER);
48 static void ast_enable_vga(struct drm_device *dev)
50 struct ast_device *ast = to_ast_device(dev);
52 ast_io_write8(ast, AST_IO_VGAER, 0x01);
53 ast_io_write8(ast, AST_IO_VGAMR_W, 0x01);
57 * Run this function as part of the HW device cleanup; not
58 * when the DRM device gets released.
60 static void ast_enable_mmio_release(void *data)
62 struct ast_device *ast = data;
64 /* enable standard VGA decode */
65 ast_set_index_reg(ast, AST_IO_VGACRI, 0xa1, 0x04);
68 static int ast_enable_mmio(struct ast_device *ast)
70 struct drm_device *dev = &ast->base;
72 ast_set_index_reg(ast, AST_IO_VGACRI, 0xa1, 0x06);
74 return devm_add_action_or_reset(dev->dev, ast_enable_mmio_release, ast);
77 static void ast_open_key(struct ast_device *ast)
79 ast_set_index_reg(ast, AST_IO_VGACRI, 0x80, 0xA8);
82 static int ast_device_config_init(struct ast_device *ast)
84 struct drm_device *dev = &ast->base;
85 struct pci_dev *pdev = to_pci_dev(dev->dev);
86 struct device_node *np = dev->dev->of_node;
87 uint32_t scu_rev = 0xffffffff;
92 * Find configuration mode and read SCU revision
95 ast->config_mode = ast_use_defaults;
97 /* Check if we have device-tree properties */
98 if (np && !of_property_read_u32(np, "aspeed,scu-revision-id", &data)) {
99 /* We do, disable P2A access */
100 ast->config_mode = ast_use_dt;
102 } else if (pdev->device == PCI_CHIP_AST2000) { // Not all families have a P2A bridge
104 * The BMC will set SCU 0x40 D[12] to 1 if the P2 bridge
105 * is disabled. We force using P2A if VGA only mode bit
108 jregd0 = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd0, 0xff);
109 jregd1 = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd1, 0xff);
110 if (!(jregd0 & 0x80) || !(jregd1 & 0x10)) {
113 * We have a P2A bridge and it is enabled.
116 /* Patch AST2500/AST2510 */
117 if ((pdev->revision & 0xf0) == 0x40) {
118 if (!(jregd0 & AST_VRAM_INIT_STATUS_MASK))
119 ast_patch_ahb_2500(ast);
122 /* Double check that it's actually working */
123 data = ast_read32(ast, 0xf004);
124 if ((data != 0xffffffff) && (data != 0x00)) {
125 ast->config_mode = ast_use_p2a;
127 /* Read SCU7c (silicon revision register) */
128 ast_write32(ast, 0xf004, 0x1e6e0000);
129 ast_write32(ast, 0xf000, 0x1);
130 scu_rev = ast_read32(ast, 0x1207c);
135 switch (ast->config_mode) {
136 case ast_use_defaults:
137 drm_info(dev, "Using default configuration\n");
140 drm_info(dev, "Using device-tree for configuration\n");
143 drm_info(dev, "Using P2A bridge for configuration\n");
151 if (pdev->revision >= 0x50) {
153 drm_info(dev, "AST 2600 detected\n");
154 } else if (pdev->revision >= 0x40) {
155 switch (scu_rev & 0x300) {
158 drm_info(dev, "AST 2510 detected\n");
162 drm_info(dev, "AST 2500 detected\n");
164 } else if (pdev->revision >= 0x30) {
165 switch (scu_rev & 0x300) {
168 drm_info(dev, "AST 1400 detected\n");
172 drm_info(dev, "AST 2400 detected\n");
174 } else if (pdev->revision >= 0x20) {
175 switch (scu_rev & 0x300) {
178 drm_info(dev, "AST 1300 detected\n");
182 drm_info(dev, "AST 2300 detected\n");
185 } else if (pdev->revision >= 0x10) {
186 switch (scu_rev & 0x0300) {
189 drm_info(dev, "AST 1100 detected\n");
193 drm_info(dev, "AST 2200 detected\n");
197 drm_info(dev, "AST 2150 detected\n");
201 drm_info(dev, "AST 2100 detected\n");
206 drm_info(dev, "AST 2000 detected\n");
212 static void ast_detect_widescreen(struct ast_device *ast)
216 /* Check if we support wide screen */
217 switch (AST_GEN(ast)) {
219 ast->support_wide_screen = false;
222 jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd0, 0xff);
224 ast->support_wide_screen = true;
225 else if (jreg & 0x01)
226 ast->support_wide_screen = true;
228 ast->support_wide_screen = false;
229 if (ast->chip == AST1300)
230 ast->support_wide_screen = true;
231 if (ast->chip == AST1400)
232 ast->support_wide_screen = true;
233 if (ast->chip == AST2510)
234 ast->support_wide_screen = true;
235 if (IS_AST_GEN7(ast))
236 ast->support_wide_screen = true;
242 static void ast_detect_tx_chip(struct ast_device *ast, bool need_post)
244 struct drm_device *dev = &ast->base;
247 /* Check 3rd Tx option (digital output afaik) */
248 ast->tx_chip_types |= AST_TX_NONE_BIT;
251 * VGACRA3 Enhanced Color Mode Register, check if DVO is already
252 * enabled, in that case, assume we have a SIL164 TMDS transmitter
254 * Don't make that assumption if we the chip wasn't enabled and
255 * is at power-on reset, otherwise we'll incorrectly "detect" a
256 * SIL164 when there is none.
259 jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xa3, 0xff);
261 ast->tx_chip_types = AST_TX_SIL164_BIT;
264 if (IS_AST_GEN4(ast) || IS_AST_GEN5(ast) || IS_AST_GEN6(ast)) {
266 * On AST GEN4+, look the configuration set by the SoC in
267 * the SOC scratch register #1 bits 11:8 (interestingly marked
268 * as "reserved" in the spec)
270 jreg = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd1, 0xff);
273 ast->tx_chip_types = AST_TX_SIL164_BIT;
276 ast->dp501_fw_addr = drmm_kzalloc(dev, 32*1024, GFP_KERNEL);
277 if (ast->dp501_fw_addr) {
278 /* backup firmware */
279 if (ast_backup_fw(dev, ast->dp501_fw_addr, 32*1024)) {
280 drmm_kfree(dev, ast->dp501_fw_addr);
281 ast->dp501_fw_addr = NULL;
286 ast->tx_chip_types = AST_TX_DP501_BIT;
288 } else if (IS_AST_GEN7(ast)) {
289 if (ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xD1, TX_TYPE_MASK) ==
291 ast->tx_chip_types = AST_TX_ASTDP_BIT;
292 ast_dp_launch(&ast->base);
296 /* Print stuff for diagnostic purposes */
297 if (ast->tx_chip_types & AST_TX_NONE_BIT)
298 drm_info(dev, "Using analog VGA\n");
299 if (ast->tx_chip_types & AST_TX_SIL164_BIT)
300 drm_info(dev, "Using Sil164 TMDS transmitter\n");
301 if (ast->tx_chip_types & AST_TX_DP501_BIT)
302 drm_info(dev, "Using DP501 DisplayPort transmitter\n");
303 if (ast->tx_chip_types & AST_TX_ASTDP_BIT)
304 drm_info(dev, "Using ASPEED DisplayPort transmitter\n");
307 static int ast_get_dram_info(struct drm_device *dev)
309 struct device_node *np = dev->dev->of_node;
310 struct ast_device *ast = to_ast_device(dev);
311 uint32_t mcr_cfg, mcr_scu_mpll, mcr_scu_strap;
312 uint32_t denum, num, div, ref_pll, dsel;
314 switch (ast->config_mode) {
317 * If some properties are missing, use reasonable
320 if (of_property_read_u32(np, "aspeed,mcr-configuration",
322 mcr_cfg = 0x00000577;
323 if (of_property_read_u32(np, "aspeed,mcr-scu-mpll",
325 mcr_scu_mpll = 0x000050C0;
326 if (of_property_read_u32(np, "aspeed,mcr-scu-strap",
331 ast_write32(ast, 0xf004, 0x1e6e0000);
332 ast_write32(ast, 0xf000, 0x1);
333 mcr_cfg = ast_read32(ast, 0x10004);
334 mcr_scu_mpll = ast_read32(ast, 0x10120);
335 mcr_scu_strap = ast_read32(ast, 0x10170);
337 case ast_use_defaults:
339 ast->dram_bus_width = 16;
340 ast->dram_type = AST_DRAM_1Gx16;
341 if (IS_AST_GEN6(ast))
349 ast->dram_bus_width = 16;
351 ast->dram_bus_width = 32;
353 if (IS_AST_GEN6(ast)) {
354 switch (mcr_cfg & 0x03) {
356 ast->dram_type = AST_DRAM_1Gx16;
360 ast->dram_type = AST_DRAM_2Gx16;
363 ast->dram_type = AST_DRAM_4Gx16;
366 ast->dram_type = AST_DRAM_8Gx16;
369 } else if (IS_AST_GEN4(ast) || IS_AST_GEN5(ast)) {
370 switch (mcr_cfg & 0x03) {
372 ast->dram_type = AST_DRAM_512Mx16;
376 ast->dram_type = AST_DRAM_1Gx16;
379 ast->dram_type = AST_DRAM_2Gx16;
382 ast->dram_type = AST_DRAM_4Gx16;
386 switch (mcr_cfg & 0x0c) {
389 ast->dram_type = AST_DRAM_512Mx16;
393 ast->dram_type = AST_DRAM_1Gx16;
395 ast->dram_type = AST_DRAM_512Mx32;
398 ast->dram_type = AST_DRAM_1Gx32;
403 if (mcr_scu_strap & 0x2000)
408 denum = mcr_scu_mpll & 0x1f;
409 num = (mcr_scu_mpll & 0x3fe0) >> 5;
410 dsel = (mcr_scu_mpll & 0xc000) >> 14;
423 ast->mclk = ref_pll * (num + 2) / ((denum + 2) * (div * 1000));
427 struct ast_device *ast_device_create(const struct drm_driver *drv,
428 struct pci_dev *pdev,
431 struct drm_device *dev;
432 struct ast_device *ast;
433 bool need_post = false;
436 ast = devm_drm_dev_alloc(&pdev->dev, drv, struct ast_device, base);
441 pci_set_drvdata(pdev, dev);
443 ret = drmm_mutex_init(dev, &ast->ioregs_lock);
447 ast->regs = pcim_iomap(pdev, 1, 0);
449 return ERR_PTR(-EIO);
452 * After AST2500, MMIO is enabled by default, and it should be adopted
453 * to be compatible with Arm.
455 if (pdev->revision >= 0x40) {
456 ast->ioregs = ast->regs + AST_IO_MM_OFFSET;
457 } else if (!(pci_resource_flags(pdev, 2) & IORESOURCE_IO)) {
458 drm_info(dev, "platform has no IO space, trying MMIO\n");
459 ast->ioregs = ast->regs + AST_IO_MM_OFFSET;
462 /* "map" IO regs if the above hasn't done so already */
464 ast->ioregs = pcim_iomap(pdev, 2, 0);
466 return ERR_PTR(-EIO);
469 if (!ast_is_vga_enabled(dev)) {
470 drm_info(dev, "VGA not enabled on entry, requesting chip POST\n");
475 * If VGA isn't enabled, we need to enable now or subsequent
476 * access to the scratch registers will fail.
481 /* Enable extended register access */
483 ret = ast_enable_mmio(ast);
487 ret = ast_device_config_init(ast);
491 ast_detect_widescreen(ast);
492 ast_detect_tx_chip(ast, need_post);
494 ret = ast_get_dram_info(dev);
498 drm_info(dev, "dram MCLK=%u Mhz type=%d bus_width=%d\n",
499 ast->mclk, ast->dram_type, ast->dram_bus_width);
504 ret = ast_mm_init(ast);
508 /* map reserved buffer */
509 ast->dp501_fw_buf = NULL;
510 if (ast->vram_size < pci_resource_len(pdev, 0)) {
511 ast->dp501_fw_buf = pci_iomap_range(pdev, 0, ast->vram_size, 0);
512 if (!ast->dp501_fw_buf)
513 drm_info(dev, "failed to map reserved buffer!\n");
516 ret = ast_mode_config_init(ast);