Home | History | Annotate | Line # | Download | only in ast
      1  1.2  riastrad /*	$NetBSD: ast_main.c,v 1.3 2021/12/18 23:45:27 riastradh Exp $	*/
      2  1.2  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2012 Red Hat Inc.
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the
      8  1.1  riastrad  * "Software"), to deal in the Software without restriction, including
      9  1.1  riastrad  * without limitation the rights to use, copy, modify, merge, publish,
     10  1.1  riastrad  * distribute, sub license, and/or sell copies of the Software, and to
     11  1.1  riastrad  * permit persons to whom the Software is furnished to do so, subject to
     12  1.1  riastrad  * the following conditions:
     13  1.1  riastrad  *
     14  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     17  1.1  riastrad  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
     18  1.1  riastrad  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     19  1.1  riastrad  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     20  1.1  riastrad  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     21  1.1  riastrad  *
     22  1.1  riastrad  * The above copyright notice and this permission notice (including the
     23  1.1  riastrad  * next paragraph) shall be included in all copies or substantial portions
     24  1.1  riastrad  * of the Software.
     25  1.1  riastrad  *
     26  1.1  riastrad  */
     27  1.1  riastrad /*
     28  1.1  riastrad  * Authors: Dave Airlie <airlied (at) redhat.com>
     29  1.1  riastrad  */
     30  1.3  riastrad 
     31  1.2  riastrad #include <sys/cdefs.h>
     32  1.2  riastrad __KERNEL_RCSID(0, "$NetBSD: ast_main.c,v 1.3 2021/12/18 23:45:27 riastradh Exp $");
     33  1.2  riastrad 
     34  1.3  riastrad #include <linux/pci.h>
     35  1.1  riastrad 
     36  1.3  riastrad #include <drm/drm_atomic_helper.h>
     37  1.3  riastrad #include <drm/drm_crtc_helper.h>
     38  1.1  riastrad #include <drm/drm_fb_helper.h>
     39  1.3  riastrad #include <drm/drm_gem.h>
     40  1.3  riastrad #include <drm/drm_gem_framebuffer_helper.h>
     41  1.3  riastrad #include <drm/drm_gem_vram_helper.h>
     42  1.1  riastrad 
     43  1.3  riastrad #include "ast_drv.h"
     44  1.1  riastrad 
     45  1.1  riastrad void ast_set_index_reg_mask(struct ast_private *ast,
     46  1.1  riastrad 			    uint32_t base, uint8_t index,
     47  1.1  riastrad 			    uint8_t mask, uint8_t val)
     48  1.1  riastrad {
     49  1.1  riastrad 	u8 tmp;
     50  1.1  riastrad 	ast_io_write8(ast, base, index);
     51  1.1  riastrad 	tmp = (ast_io_read8(ast, base + 1) & mask) | val;
     52  1.1  riastrad 	ast_set_index_reg(ast, base, index, tmp);
     53  1.1  riastrad }
     54  1.1  riastrad 
     55  1.1  riastrad uint8_t ast_get_index_reg(struct ast_private *ast,
     56  1.1  riastrad 			  uint32_t base, uint8_t index)
     57  1.1  riastrad {
     58  1.1  riastrad 	uint8_t ret;
     59  1.1  riastrad 	ast_io_write8(ast, base, index);
     60  1.1  riastrad 	ret = ast_io_read8(ast, base + 1);
     61  1.1  riastrad 	return ret;
     62  1.1  riastrad }
     63  1.1  riastrad 
     64  1.1  riastrad uint8_t ast_get_index_reg_mask(struct ast_private *ast,
     65  1.1  riastrad 			       uint32_t base, uint8_t index, uint8_t mask)
     66  1.1  riastrad {
     67  1.1  riastrad 	uint8_t ret;
     68  1.1  riastrad 	ast_io_write8(ast, base, index);
     69  1.1  riastrad 	ret = ast_io_read8(ast, base + 1) & mask;
     70  1.1  riastrad 	return ret;
     71  1.1  riastrad }
     72  1.1  riastrad 
     73  1.2  riastrad static void ast_detect_config_mode(struct drm_device *dev, u32 *scu_rev)
     74  1.2  riastrad {
     75  1.2  riastrad 	struct device_node *np = dev->pdev->dev.of_node;
     76  1.2  riastrad 	struct ast_private *ast = dev->dev_private;
     77  1.2  riastrad 	uint32_t data, jregd0, jregd1;
     78  1.2  riastrad 
     79  1.2  riastrad 	/* Defaults */
     80  1.2  riastrad 	ast->config_mode = ast_use_defaults;
     81  1.2  riastrad 	*scu_rev = 0xffffffff;
     82  1.2  riastrad 
     83  1.2  riastrad 	/* Check if we have device-tree properties */
     84  1.2  riastrad 	if (np && !of_property_read_u32(np, "aspeed,scu-revision-id",
     85  1.2  riastrad 					scu_rev)) {
     86  1.2  riastrad 		/* We do, disable P2A access */
     87  1.2  riastrad 		ast->config_mode = ast_use_dt;
     88  1.2  riastrad 		DRM_INFO("Using device-tree for configuration\n");
     89  1.2  riastrad 		return;
     90  1.2  riastrad 	}
     91  1.2  riastrad 
     92  1.2  riastrad 	/* Not all families have a P2A bridge */
     93  1.2  riastrad 	if (dev->pdev->device != PCI_CHIP_AST2000)
     94  1.2  riastrad 		return;
     95  1.2  riastrad 
     96  1.2  riastrad 	/*
     97  1.2  riastrad 	 * The BMC will set SCU 0x40 D[12] to 1 if the P2 bridge
     98  1.2  riastrad 	 * is disabled. We force using P2A if VGA only mode bit
     99  1.2  riastrad 	 * is set D[7]
    100  1.2  riastrad 	 */
    101  1.2  riastrad 	jregd0 = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
    102  1.2  riastrad 	jregd1 = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
    103  1.2  riastrad 	if (!(jregd0 & 0x80) || !(jregd1 & 0x10)) {
    104  1.2  riastrad 		/* Double check it's actually working */
    105  1.2  riastrad 		data = ast_read32(ast, 0xf004);
    106  1.2  riastrad 		if (data != 0xFFFFFFFF) {
    107  1.2  riastrad 			/* P2A works, grab silicon revision */
    108  1.2  riastrad 			ast->config_mode = ast_use_p2a;
    109  1.2  riastrad 
    110  1.2  riastrad 			DRM_INFO("Using P2A bridge for configuration\n");
    111  1.2  riastrad 
    112  1.2  riastrad 			/* Read SCU7c (silicon revision register) */
    113  1.2  riastrad 			ast_write32(ast, 0xf004, 0x1e6e0000);
    114  1.2  riastrad 			ast_write32(ast, 0xf000, 0x1);
    115  1.2  riastrad 			*scu_rev = ast_read32(ast, 0x1207c);
    116  1.2  riastrad 			return;
    117  1.2  riastrad 		}
    118  1.2  riastrad 	}
    119  1.2  riastrad 
    120  1.2  riastrad 	/* We have a P2A bridge but it's disabled */
    121  1.2  riastrad 	DRM_INFO("P2A bridge disabled, using default configuration\n");
    122  1.2  riastrad }
    123  1.1  riastrad 
    124  1.2  riastrad static int ast_detect_chip(struct drm_device *dev, bool *need_post)
    125  1.1  riastrad {
    126  1.1  riastrad 	struct ast_private *ast = dev->dev_private;
    127  1.2  riastrad 	uint32_t jreg, scu_rev;
    128  1.2  riastrad 
    129  1.2  riastrad 	/*
    130  1.2  riastrad 	 * If VGA isn't enabled, we need to enable now or subsequent
    131  1.2  riastrad 	 * access to the scratch registers will fail. We also inform
    132  1.2  riastrad 	 * our caller that it needs to POST the chip
    133  1.2  riastrad 	 * (Assumption: VGA not enabled -> need to POST)
    134  1.2  riastrad 	 */
    135  1.2  riastrad 	if (!ast_is_vga_enabled(dev)) {
    136  1.2  riastrad 		ast_enable_vga(dev);
    137  1.2  riastrad 		DRM_INFO("VGA not enabled on entry, requesting chip POST\n");
    138  1.2  riastrad 		*need_post = true;
    139  1.2  riastrad 	} else
    140  1.2  riastrad 		*need_post = false;
    141  1.2  riastrad 
    142  1.2  riastrad 
    143  1.2  riastrad 	/* Enable extended register access */
    144  1.3  riastrad 	ast_open_key(ast);
    145  1.2  riastrad 	ast_enable_mmio(dev);
    146  1.2  riastrad 
    147  1.2  riastrad 	/* Find out whether P2A works or whether to use device-tree */
    148  1.2  riastrad 	ast_detect_config_mode(dev, &scu_rev);
    149  1.1  riastrad 
    150  1.2  riastrad 	/* Identify chipset */
    151  1.1  riastrad 	if (dev->pdev->device == PCI_CHIP_AST1180) {
    152  1.1  riastrad 		ast->chip = AST1100;
    153  1.1  riastrad 		DRM_INFO("AST 1180 detected\n");
    154  1.1  riastrad 	} else {
    155  1.3  riastrad 		if (dev->pdev->revision >= 0x40) {
    156  1.3  riastrad 			ast->chip = AST2500;
    157  1.3  riastrad 			DRM_INFO("AST 2500 detected\n");
    158  1.3  riastrad 		} else if (dev->pdev->revision >= 0x30) {
    159  1.2  riastrad 			ast->chip = AST2400;
    160  1.2  riastrad 			DRM_INFO("AST 2400 detected\n");
    161  1.2  riastrad 		} else if (dev->pdev->revision >= 0x20) {
    162  1.1  riastrad 			ast->chip = AST2300;
    163  1.1  riastrad 			DRM_INFO("AST 2300 detected\n");
    164  1.1  riastrad 		} else if (dev->pdev->revision >= 0x10) {
    165  1.2  riastrad 			switch (scu_rev & 0x0300) {
    166  1.1  riastrad 			case 0x0200:
    167  1.1  riastrad 				ast->chip = AST1100;
    168  1.1  riastrad 				DRM_INFO("AST 1100 detected\n");
    169  1.1  riastrad 				break;
    170  1.1  riastrad 			case 0x0100:
    171  1.1  riastrad 				ast->chip = AST2200;
    172  1.1  riastrad 				DRM_INFO("AST 2200 detected\n");
    173  1.1  riastrad 				break;
    174  1.1  riastrad 			case 0x0000:
    175  1.1  riastrad 				ast->chip = AST2150;
    176  1.1  riastrad 				DRM_INFO("AST 2150 detected\n");
    177  1.1  riastrad 				break;
    178  1.1  riastrad 			default:
    179  1.1  riastrad 				ast->chip = AST2100;
    180  1.1  riastrad 				DRM_INFO("AST 2100 detected\n");
    181  1.1  riastrad 				break;
    182  1.1  riastrad 			}
    183  1.1  riastrad 			ast->vga2_clone = false;
    184  1.1  riastrad 		} else {
    185  1.2  riastrad 			ast->chip = AST2000;
    186  1.1  riastrad 			DRM_INFO("AST 2000 detected\n");
    187  1.1  riastrad 		}
    188  1.1  riastrad 	}
    189  1.2  riastrad 
    190  1.2  riastrad 	/* Check if we support wide screen */
    191  1.2  riastrad 	switch (ast->chip) {
    192  1.2  riastrad 	case AST1180:
    193  1.2  riastrad 		ast->support_wide_screen = true;
    194  1.2  riastrad 		break;
    195  1.2  riastrad 	case AST2000:
    196  1.2  riastrad 		ast->support_wide_screen = false;
    197  1.2  riastrad 		break;
    198  1.2  riastrad 	default:
    199  1.2  riastrad 		jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
    200  1.2  riastrad 		if (!(jreg & 0x80))
    201  1.2  riastrad 			ast->support_wide_screen = true;
    202  1.2  riastrad 		else if (jreg & 0x01)
    203  1.2  riastrad 			ast->support_wide_screen = true;
    204  1.2  riastrad 		else {
    205  1.2  riastrad 			ast->support_wide_screen = false;
    206  1.2  riastrad 			if (ast->chip == AST2300 &&
    207  1.2  riastrad 			    (scu_rev & 0x300) == 0x0) /* ast1300 */
    208  1.2  riastrad 				ast->support_wide_screen = true;
    209  1.2  riastrad 			if (ast->chip == AST2400 &&
    210  1.2  riastrad 			    (scu_rev & 0x300) == 0x100) /* ast1400 */
    211  1.2  riastrad 				ast->support_wide_screen = true;
    212  1.3  riastrad 			if (ast->chip == AST2500 &&
    213  1.3  riastrad 			    scu_rev == 0x100)           /* ast2510 */
    214  1.3  riastrad 				ast->support_wide_screen = true;
    215  1.2  riastrad 		}
    216  1.2  riastrad 		break;
    217  1.2  riastrad 	}
    218  1.2  riastrad 
    219  1.2  riastrad 	/* Check 3rd Tx option (digital output afaik) */
    220  1.2  riastrad 	ast->tx_chip_type = AST_TX_NONE;
    221  1.2  riastrad 
    222  1.2  riastrad 	/*
    223  1.2  riastrad 	 * VGACRA3 Enhanced Color Mode Register, check if DVO is already
    224  1.2  riastrad 	 * enabled, in that case, assume we have a SIL164 TMDS transmitter
    225  1.2  riastrad 	 *
    226  1.2  riastrad 	 * Don't make that assumption if we the chip wasn't enabled and
    227  1.2  riastrad 	 * is at power-on reset, otherwise we'll incorrectly "detect" a
    228  1.2  riastrad 	 * SIL164 when there is none.
    229  1.2  riastrad 	 */
    230  1.2  riastrad 	if (!*need_post) {
    231  1.2  riastrad 		jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xff);
    232  1.2  riastrad 		if (jreg & 0x80)
    233  1.2  riastrad 			ast->tx_chip_type = AST_TX_SIL164;
    234  1.2  riastrad 	}
    235  1.2  riastrad 
    236  1.2  riastrad 	if ((ast->chip == AST2300) || (ast->chip == AST2400)) {
    237  1.2  riastrad 		/*
    238  1.2  riastrad 		 * On AST2300 and 2400, look the configuration set by the SoC in
    239  1.2  riastrad 		 * the SOC scratch register #1 bits 11:8 (interestingly marked
    240  1.2  riastrad 		 * as "reserved" in the spec)
    241  1.2  riastrad 		 */
    242  1.2  riastrad 		jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
    243  1.2  riastrad 		switch (jreg) {
    244  1.2  riastrad 		case 0x04:
    245  1.2  riastrad 			ast->tx_chip_type = AST_TX_SIL164;
    246  1.2  riastrad 			break;
    247  1.2  riastrad 		case 0x08:
    248  1.2  riastrad 			ast->dp501_fw_addr = kzalloc(32*1024, GFP_KERNEL);
    249  1.2  riastrad 			if (ast->dp501_fw_addr) {
    250  1.2  riastrad 				/* backup firmware */
    251  1.2  riastrad 				if (ast_backup_fw(dev, ast->dp501_fw_addr, 32*1024)) {
    252  1.2  riastrad 					kfree(ast->dp501_fw_addr);
    253  1.2  riastrad 					ast->dp501_fw_addr = NULL;
    254  1.2  riastrad 				}
    255  1.2  riastrad 			}
    256  1.2  riastrad 			/* fallthrough */
    257  1.2  riastrad 		case 0x0c:
    258  1.2  riastrad 			ast->tx_chip_type = AST_TX_DP501;
    259  1.2  riastrad 		}
    260  1.2  riastrad 	}
    261  1.2  riastrad 
    262  1.2  riastrad 	/* Print stuff for diagnostic purposes */
    263  1.2  riastrad 	switch(ast->tx_chip_type) {
    264  1.2  riastrad 	case AST_TX_SIL164:
    265  1.2  riastrad 		DRM_INFO("Using Sil164 TMDS transmitter\n");
    266  1.2  riastrad 		break;
    267  1.2  riastrad 	case AST_TX_DP501:
    268  1.2  riastrad 		DRM_INFO("Using DP501 DisplayPort transmitter\n");
    269  1.2  riastrad 		break;
    270  1.2  riastrad 	default:
    271  1.2  riastrad 		DRM_INFO("Analog VGA only\n");
    272  1.2  riastrad 	}
    273  1.1  riastrad 	return 0;
    274  1.1  riastrad }
    275  1.1  riastrad 
    276  1.1  riastrad static int ast_get_dram_info(struct drm_device *dev)
    277  1.1  riastrad {
    278  1.2  riastrad 	struct device_node *np = dev->pdev->dev.of_node;
    279  1.1  riastrad 	struct ast_private *ast = dev->dev_private;
    280  1.2  riastrad 	uint32_t mcr_cfg, mcr_scu_mpll, mcr_scu_strap;
    281  1.2  riastrad 	uint32_t denum, num, div, ref_pll, dsel;
    282  1.1  riastrad 
    283  1.2  riastrad 	switch (ast->config_mode) {
    284  1.2  riastrad 	case ast_use_dt:
    285  1.2  riastrad 		/*
    286  1.2  riastrad 		 * If some properties are missing, use reasonable
    287  1.2  riastrad 		 * defaults for AST2400
    288  1.2  riastrad 		 */
    289  1.2  riastrad 		if (of_property_read_u32(np, "aspeed,mcr-configuration",
    290  1.2  riastrad 					 &mcr_cfg))
    291  1.2  riastrad 			mcr_cfg = 0x00000577;
    292  1.2  riastrad 		if (of_property_read_u32(np, "aspeed,mcr-scu-mpll",
    293  1.2  riastrad 					 &mcr_scu_mpll))
    294  1.2  riastrad 			mcr_scu_mpll = 0x000050C0;
    295  1.2  riastrad 		if (of_property_read_u32(np, "aspeed,mcr-scu-strap",
    296  1.2  riastrad 					 &mcr_scu_strap))
    297  1.2  riastrad 			mcr_scu_strap = 0;
    298  1.2  riastrad 		break;
    299  1.2  riastrad 	case ast_use_p2a:
    300  1.2  riastrad 		ast_write32(ast, 0xf004, 0x1e6e0000);
    301  1.2  riastrad 		ast_write32(ast, 0xf000, 0x1);
    302  1.2  riastrad 		mcr_cfg = ast_read32(ast, 0x10004);
    303  1.2  riastrad 		mcr_scu_mpll = ast_read32(ast, 0x10120);
    304  1.2  riastrad 		mcr_scu_strap = ast_read32(ast, 0x10170);
    305  1.2  riastrad 		break;
    306  1.2  riastrad 	case ast_use_defaults:
    307  1.2  riastrad 	default:
    308  1.2  riastrad 		ast->dram_bus_width = 16;
    309  1.2  riastrad 		ast->dram_type = AST_DRAM_1Gx16;
    310  1.3  riastrad 		if (ast->chip == AST2500)
    311  1.3  riastrad 			ast->mclk = 800;
    312  1.3  riastrad 		else
    313  1.3  riastrad 			ast->mclk = 396;
    314  1.2  riastrad 		return 0;
    315  1.2  riastrad 	}
    316  1.1  riastrad 
    317  1.2  riastrad 	if (mcr_cfg & 0x40)
    318  1.1  riastrad 		ast->dram_bus_width = 16;
    319  1.1  riastrad 	else
    320  1.1  riastrad 		ast->dram_bus_width = 32;
    321  1.1  riastrad 
    322  1.3  riastrad 	if (ast->chip == AST2500) {
    323  1.3  riastrad 		switch (mcr_cfg & 0x03) {
    324  1.3  riastrad 		case 0:
    325  1.3  riastrad 			ast->dram_type = AST_DRAM_1Gx16;
    326  1.3  riastrad 			break;
    327  1.3  riastrad 		default:
    328  1.3  riastrad 		case 1:
    329  1.3  riastrad 			ast->dram_type = AST_DRAM_2Gx16;
    330  1.3  riastrad 			break;
    331  1.3  riastrad 		case 2:
    332  1.3  riastrad 			ast->dram_type = AST_DRAM_4Gx16;
    333  1.3  riastrad 			break;
    334  1.3  riastrad 		case 3:
    335  1.3  riastrad 			ast->dram_type = AST_DRAM_8Gx16;
    336  1.3  riastrad 			break;
    337  1.3  riastrad 		}
    338  1.3  riastrad 	} else if (ast->chip == AST2300 || ast->chip == AST2400) {
    339  1.2  riastrad 		switch (mcr_cfg & 0x03) {
    340  1.1  riastrad 		case 0:
    341  1.1  riastrad 			ast->dram_type = AST_DRAM_512Mx16;
    342  1.1  riastrad 			break;
    343  1.1  riastrad 		default:
    344  1.1  riastrad 		case 1:
    345  1.1  riastrad 			ast->dram_type = AST_DRAM_1Gx16;
    346  1.1  riastrad 			break;
    347  1.1  riastrad 		case 2:
    348  1.1  riastrad 			ast->dram_type = AST_DRAM_2Gx16;
    349  1.1  riastrad 			break;
    350  1.1  riastrad 		case 3:
    351  1.1  riastrad 			ast->dram_type = AST_DRAM_4Gx16;
    352  1.1  riastrad 			break;
    353  1.1  riastrad 		}
    354  1.1  riastrad 	} else {
    355  1.2  riastrad 		switch (mcr_cfg & 0x0c) {
    356  1.1  riastrad 		case 0:
    357  1.1  riastrad 		case 4:
    358  1.1  riastrad 			ast->dram_type = AST_DRAM_512Mx16;
    359  1.1  riastrad 			break;
    360  1.1  riastrad 		case 8:
    361  1.2  riastrad 			if (mcr_cfg & 0x40)
    362  1.1  riastrad 				ast->dram_type = AST_DRAM_1Gx16;
    363  1.1  riastrad 			else
    364  1.1  riastrad 				ast->dram_type = AST_DRAM_512Mx32;
    365  1.1  riastrad 			break;
    366  1.1  riastrad 		case 0xc:
    367  1.1  riastrad 			ast->dram_type = AST_DRAM_1Gx32;
    368  1.1  riastrad 			break;
    369  1.1  riastrad 		}
    370  1.1  riastrad 	}
    371  1.1  riastrad 
    372  1.2  riastrad 	if (mcr_scu_strap & 0x2000)
    373  1.1  riastrad 		ref_pll = 14318;
    374  1.1  riastrad 	else
    375  1.1  riastrad 		ref_pll = 12000;
    376  1.1  riastrad 
    377  1.2  riastrad 	denum = mcr_scu_mpll & 0x1f;
    378  1.2  riastrad 	num = (mcr_scu_mpll & 0x3fe0) >> 5;
    379  1.2  riastrad 	dsel = (mcr_scu_mpll & 0xc000) >> 14;
    380  1.2  riastrad 	switch (dsel) {
    381  1.1  riastrad 	case 3:
    382  1.1  riastrad 		div = 0x4;
    383  1.1  riastrad 		break;
    384  1.1  riastrad 	case 2:
    385  1.1  riastrad 	case 1:
    386  1.1  riastrad 		div = 0x2;
    387  1.1  riastrad 		break;
    388  1.1  riastrad 	default:
    389  1.1  riastrad 		div = 0x1;
    390  1.1  riastrad 		break;
    391  1.1  riastrad 	}
    392  1.3  riastrad 	ast->mclk = ref_pll * (num + 2) / ((denum + 2) * (div * 1000));
    393  1.1  riastrad 	return 0;
    394  1.1  riastrad }
    395  1.1  riastrad 
    396  1.3  riastrad enum drm_mode_status ast_mode_config_mode_valid(struct drm_device *dev,
    397  1.3  riastrad 						const struct drm_display_mode *mode)
    398  1.1  riastrad {
    399  1.3  riastrad 	static const unsigned long max_bpp = 4; /* DRM_FORMAT_XRGBA8888 */
    400  1.1  riastrad 
    401  1.3  riastrad 	struct ast_private *ast = dev->dev_private;
    402  1.3  riastrad 	unsigned long fbsize, fbpages, max_fbpages;
    403  1.1  riastrad 
    404  1.3  riastrad 	/* To support double buffering, a framebuffer may not
    405  1.3  riastrad 	 * consume more than half of the available VRAM.
    406  1.3  riastrad 	 */
    407  1.3  riastrad 	max_fbpages = (ast->vram_size / 2) >> PAGE_SHIFT;
    408  1.1  riastrad 
    409  1.3  riastrad 	fbsize = mode->hdisplay * mode->vdisplay * max_bpp;
    410  1.3  riastrad 	fbpages = DIV_ROUND_UP(fbsize, PAGE_SIZE);
    411  1.1  riastrad 
    412  1.3  riastrad 	if (fbpages > max_fbpages)
    413  1.3  riastrad 		return MODE_MEM;
    414  1.1  riastrad 
    415  1.3  riastrad 	return MODE_OK;
    416  1.1  riastrad }
    417  1.1  riastrad 
    418  1.1  riastrad static const struct drm_mode_config_funcs ast_mode_funcs = {
    419  1.3  riastrad 	.fb_create = drm_gem_fb_create,
    420  1.3  riastrad 	.mode_valid = ast_mode_config_mode_valid,
    421  1.3  riastrad 	.atomic_check = drm_atomic_helper_check,
    422  1.3  riastrad 	.atomic_commit = drm_atomic_helper_commit,
    423  1.1  riastrad };
    424  1.1  riastrad 
    425  1.1  riastrad static u32 ast_get_vram_info(struct drm_device *dev)
    426  1.1  riastrad {
    427  1.1  riastrad 	struct ast_private *ast = dev->dev_private;
    428  1.1  riastrad 	u8 jreg;
    429  1.2  riastrad 	u32 vram_size;
    430  1.1  riastrad 	ast_open_key(ast);
    431  1.1  riastrad 
    432  1.2  riastrad 	vram_size = AST_VIDMEM_DEFAULT_SIZE;
    433  1.1  riastrad 	jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xaa, 0xff);
    434  1.1  riastrad 	switch (jreg & 3) {
    435  1.2  riastrad 	case 0: vram_size = AST_VIDMEM_SIZE_8M; break;
    436  1.2  riastrad 	case 1: vram_size = AST_VIDMEM_SIZE_16M; break;
    437  1.2  riastrad 	case 2: vram_size = AST_VIDMEM_SIZE_32M; break;
    438  1.2  riastrad 	case 3: vram_size = AST_VIDMEM_SIZE_64M; break;
    439  1.2  riastrad 	}
    440  1.2  riastrad 
    441  1.2  riastrad 	jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x99, 0xff);
    442  1.2  riastrad 	switch (jreg & 0x03) {
    443  1.2  riastrad 	case 1:
    444  1.2  riastrad 		vram_size -= 0x100000;
    445  1.2  riastrad 		break;
    446  1.2  riastrad 	case 2:
    447  1.2  riastrad 		vram_size -= 0x200000;
    448  1.2  riastrad 		break;
    449  1.2  riastrad 	case 3:
    450  1.2  riastrad 		vram_size -= 0x400000;
    451  1.2  riastrad 		break;
    452  1.1  riastrad 	}
    453  1.2  riastrad 
    454  1.2  riastrad 	return vram_size;
    455  1.1  riastrad }
    456  1.1  riastrad 
    457  1.1  riastrad int ast_driver_load(struct drm_device *dev, unsigned long flags)
    458  1.1  riastrad {
    459  1.1  riastrad 	struct ast_private *ast;
    460  1.2  riastrad 	bool need_post;
    461  1.1  riastrad 	int ret = 0;
    462  1.1  riastrad 
    463  1.1  riastrad 	ast = kzalloc(sizeof(struct ast_private), GFP_KERNEL);
    464  1.1  riastrad 	if (!ast)
    465  1.1  riastrad 		return -ENOMEM;
    466  1.1  riastrad 
    467  1.1  riastrad 	dev->dev_private = ast;
    468  1.1  riastrad 	ast->dev = dev;
    469  1.1  riastrad 
    470  1.1  riastrad 	ast->regs = pci_iomap(dev->pdev, 1, 0);
    471  1.1  riastrad 	if (!ast->regs) {
    472  1.1  riastrad 		ret = -EIO;
    473  1.1  riastrad 		goto out_free;
    474  1.1  riastrad 	}
    475  1.2  riastrad 
    476  1.2  riastrad 	/*
    477  1.2  riastrad 	 * If we don't have IO space at all, use MMIO now and
    478  1.2  riastrad 	 * assume the chip has MMIO enabled by default (rev 0x20
    479  1.2  riastrad 	 * and higher).
    480  1.2  riastrad 	 */
    481  1.2  riastrad 	if (!(pci_resource_flags(dev->pdev, 2) & IORESOURCE_IO)) {
    482  1.2  riastrad 		DRM_INFO("platform has no IO space, trying MMIO\n");
    483  1.2  riastrad 		ast->ioregs = ast->regs + AST_IO_MM_OFFSET;
    484  1.2  riastrad 	}
    485  1.2  riastrad 
    486  1.2  riastrad 	/* "map" IO regs if the above hasn't done so already */
    487  1.1  riastrad 	if (!ast->ioregs) {
    488  1.2  riastrad 		ast->ioregs = pci_iomap(dev->pdev, 2, 0);
    489  1.2  riastrad 		if (!ast->ioregs) {
    490  1.2  riastrad 			ret = -EIO;
    491  1.2  riastrad 			goto out_free;
    492  1.2  riastrad 		}
    493  1.1  riastrad 	}
    494  1.1  riastrad 
    495  1.2  riastrad 	ast_detect_chip(dev, &need_post);
    496  1.1  riastrad 
    497  1.3  riastrad 	if (need_post)
    498  1.3  riastrad 		ast_post_gpu(dev);
    499  1.3  riastrad 
    500  1.1  riastrad 	if (ast->chip != AST1180) {
    501  1.2  riastrad 		ret = ast_get_dram_info(dev);
    502  1.2  riastrad 		if (ret)
    503  1.2  riastrad 			goto out_free;
    504  1.1  riastrad 		ast->vram_size = ast_get_vram_info(dev);
    505  1.3  riastrad 		DRM_INFO("dram MCLK=%u Mhz type=%d bus_width=%d size=%08x\n",
    506  1.3  riastrad 			 ast->mclk, ast->dram_type,
    507  1.3  riastrad 			 ast->dram_bus_width, ast->vram_size);
    508  1.1  riastrad 	}
    509  1.1  riastrad 
    510  1.1  riastrad 	ret = ast_mm_init(ast);
    511  1.1  riastrad 	if (ret)
    512  1.1  riastrad 		goto out_free;
    513  1.1  riastrad 
    514  1.1  riastrad 	drm_mode_config_init(dev);
    515  1.1  riastrad 
    516  1.1  riastrad 	dev->mode_config.funcs = (void *)&ast_mode_funcs;
    517  1.1  riastrad 	dev->mode_config.min_width = 0;
    518  1.1  riastrad 	dev->mode_config.min_height = 0;
    519  1.1  riastrad 	dev->mode_config.preferred_depth = 24;
    520  1.1  riastrad 	dev->mode_config.prefer_shadow = 1;
    521  1.2  riastrad 	dev->mode_config.fb_base = pci_resource_start(ast->dev->pdev, 0);
    522  1.1  riastrad 
    523  1.1  riastrad 	if (ast->chip == AST2100 ||
    524  1.1  riastrad 	    ast->chip == AST2200 ||
    525  1.1  riastrad 	    ast->chip == AST2300 ||
    526  1.2  riastrad 	    ast->chip == AST2400 ||
    527  1.3  riastrad 	    ast->chip == AST2500 ||
    528  1.1  riastrad 	    ast->chip == AST1180) {
    529  1.1  riastrad 		dev->mode_config.max_width = 1920;
    530  1.1  riastrad 		dev->mode_config.max_height = 2048;
    531  1.1  riastrad 	} else {
    532  1.1  riastrad 		dev->mode_config.max_width = 1600;
    533  1.1  riastrad 		dev->mode_config.max_height = 1200;
    534  1.1  riastrad 	}
    535  1.1  riastrad 
    536  1.1  riastrad 	ret = ast_mode_init(dev);
    537  1.1  riastrad 	if (ret)
    538  1.1  riastrad 		goto out_free;
    539  1.1  riastrad 
    540  1.3  riastrad 	drm_mode_config_reset(dev);
    541  1.3  riastrad 
    542  1.3  riastrad 	ret = drm_fbdev_generic_setup(dev, 32);
    543  1.1  riastrad 	if (ret)
    544  1.1  riastrad 		goto out_free;
    545  1.1  riastrad 
    546  1.1  riastrad 	return 0;
    547  1.1  riastrad out_free:
    548  1.1  riastrad 	kfree(ast);
    549  1.1  riastrad 	dev->dev_private = NULL;
    550  1.1  riastrad 	return ret;
    551  1.1  riastrad }
    552  1.1  riastrad 
    553  1.3  riastrad void ast_driver_unload(struct drm_device *dev)
    554  1.1  riastrad {
    555  1.1  riastrad 	struct ast_private *ast = dev->dev_private;
    556  1.1  riastrad 
    557  1.3  riastrad 	/* enable standard VGA decode */
    558  1.3  riastrad 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa1, 0x04);
    559  1.3  riastrad 
    560  1.3  riastrad 	ast_release_firmware(dev);
    561  1.2  riastrad 	kfree(ast->dp501_fw_addr);
    562  1.1  riastrad 	ast_mode_fini(dev);
    563  1.1  riastrad 	drm_mode_config_cleanup(dev);
    564  1.1  riastrad 
    565  1.1  riastrad 	ast_mm_fini(ast);
    566  1.3  riastrad 	if (ast->ioregs != ast->regs + AST_IO_MM_OFFSET)
    567  1.3  riastrad 		pci_iounmap(dev->pdev, ast->ioregs);
    568  1.1  riastrad 	pci_iounmap(dev->pdev, ast->regs);
    569  1.1  riastrad 	kfree(ast);
    570  1.1  riastrad }
    571