From e55abc95c2b7f7cb8de73e1e21688fb5b5c08d5c Mon Sep 17 00:00:00 2001
From: Mathieu Larouche <mathieu.larouche@matrox.com>
Date: Tue, 7 Feb 2017 11:12:14 -0500
Subject: [PATCH] xf86-video-mga: Add support for a new G200eH3 device

- Added support for the new deviceID for G200eH3
- Added PLL algorithm for the G200eH3
- Removed the bandwidth limitation for the G200eH3

Signed-off-by: Mathieu Larouche <mathieu.larouche@matrox.com>
Reviewed-by: Dave Airlie <airlied@redhat.com>
---
 src/mga.h        |  4 +++-
 src/mga_dacG.c   | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/mga_driver.c | 36 +++++++++++++++++++++++++++++++++---
 src/mga_merge.c  |  2 ++
 src/mga_storm.c  |  1 +
 5 files changed, 94 insertions(+), 5 deletions(-)

Index: xf86-video-mga-1.4.11/src/mga.h
===================================================================
--- xf86-video-mga-1.4.11.orig/src/mga.h
+++ xf86-video-mga-1.4.11/src/mga.h
@@ -149,7 +149,9 @@ void MGAdbg_outreg32(ScrnInfoPtr, int,in
 #define PCI_CHIP_MGAG200_EW3_PCI 0x0536
 #endif
 
-
+#ifndef PCI_CHIP_MGAG200_EH3_PCI
+#define PCI_CHIP_MGAG200_EH3_PCI 0x0538
+#endif
 
 /*
  * These were functions.  Use macros instead to avoid the need to
Index: xf86-video-mga-1.4.11/src/mga_dacG.c
===================================================================
--- xf86-video-mga-1.4.11.orig/src/mga_dacG.c
+++ xf86-video-mga-1.4.11/src/mga_dacG.c
@@ -396,6 +396,52 @@ MGAG200EHComputePLLParam(ScrnInfoPtr pSc
     }
 }
 
+void
+MGAG200EH3ComputePLLParam(ScrnInfoPtr pScrn, long lFo, int *M, int *N, int *P)
+{
+    unsigned int ulComputedFo;
+    unsigned int ulFDelta;
+    unsigned int ulFPermitedDelta;
+    unsigned int ulFTmpDelta;
+    unsigned int ulTestP;
+    unsigned int ulTestM;
+    unsigned int ulTestN;
+    unsigned int ulVCOMax;
+    unsigned int ulVCOMin;
+    unsigned int ulPLLFreqRef;
+
+    ulVCOMax        = 3000000;
+    ulVCOMin        = 1500000;
+    ulPLLFreqRef    = 25000;
+
+    ulTestP         = 0;
+
+    ulFDelta = 0xFFFFFFFF;
+    /* Permited delta is 0.5% as VESA Specification */
+    ulFPermitedDelta = lFo * 5 / 1000;
+
+    /* Then we need to minimize the M while staying within 0.5% */
+    for (ulTestM = 150; ulTestM >= 6; ulTestM--) {
+        if ((lFo * ulTestM) > ulVCOMax) continue;
+        if ((lFo * ulTestM) < ulVCOMin) continue;
+
+        for (ulTestN = 120; ulTestN >= 60; ulTestN--) {
+            ulComputedFo = (ulPLLFreqRef * ulTestN) / ulTestM;
+            if (ulComputedFo > lFo)
+	        ulFTmpDelta = ulComputedFo - lFo;
+            else
+                ulFTmpDelta = lFo - ulComputedFo;
+
+            if (ulFTmpDelta < ulFDelta) {
+                ulFDelta = ulFTmpDelta;
+                *M = (CARD8)(ulTestM);
+                *N = (CARD8)(ulTestN);
+                *P = (CARD8)(ulTestP);
+            }
+        }
+    }
+}
+
 static void
 MGAG200EVPIXPLLSET(ScrnInfoPtr pScrn, MGARegPtr mgaReg)
 {
@@ -1041,7 +1087,14 @@ MGAGSetPCLK( ScrnInfoPtr pScrn, long f_o
 	    pReg->PllN = n;
 	    pReg->PllP = p;
     } else if (pMga->is_G200EH) {
-	    MGAG200EHComputePLLParam(pScrn, f_out, &m, &n, &p);
+            if (pMga->Chipset == PCI_CHIP_MGAG200_EH3_PCI)
+            {
+                 MGAG200EH3ComputePLLParam(pScrn, f_out, &m, &n, &p);
+            }
+            else
+            {
+                 MGAG200EHComputePLLParam(pScrn, f_out, &m, &n, &p);
+            }
 
 	    pReg->PllM = m;
 	    pReg->PllN = n;
@@ -1254,6 +1307,7 @@ MGAGInit(ScrnInfoPtr pScrn, DisplayModeP
 			break;
 
         case PCI_CHIP_MGAG200_EH_PCI:
+        case PCI_CHIP_MGAG200_EH3_PCI:
                 pReg->DacRegs[MGA1064_MISC_CTL] =
                     MGA1064_MISC_CTL_VGA8 |
                     MGA1064_MISC_CTL_DAC_RAM_CS;
Index: xf86-video-mga-1.4.11/src/mga_driver.c
===================================================================
--- xf86-video-mga-1.4.11.orig/src/mga_driver.c
+++ xf86-video-mga-1.4.11/src/mga_driver.c
@@ -434,6 +434,21 @@ static const struct mga_device_attribute
 	},
 
 	16384, 0x4000,          /* Memory probe size & offset values */
+    },
+
+    [17] = { 0, 1, 0, 0, 1, 0, 0, new_BARs,
+            (TRANSC_SOLID_FILL | TWO_PASS_COLOR_EXPAND | USE_LINEAR_EXPANSION),
+	{
+	    { 50000, 230000 }, /* System VCO frequencies */
+	    { 50000, 203400 }, /* Pixel VCO frequencies */
+	    { 0, 0 },          /* Video VCO frequencies */
+	    45000,            /* Memory clock */
+	    27050,             /* PLL reference frequency */
+	    0,                 /* Supports fast bitblt? */
+	    MGA_HOST_PCI       /* Host interface */
+	},
+
+	16384, 0x4000,          /* Memory probe size & offset values */
     }
 };
 
@@ -468,6 +483,8 @@ static const struct pci_id_match mga_dev
 
     MGA_DEVICE_MATCH( PCI_CHIP_MGAG200_EW3_PCI, 16 ),
 
+    MGA_DEVICE_MATCH( PCI_CHIP_MGAG200_EH3_PCI, 17 ),
+
     { 0, 0, 0 },
 };
 #endif
@@ -489,6 +506,7 @@ static SymTabRec MGAChipsets[] = {
     { PCI_CHIP_MGAG200_WINBOND_PCI,	"mgag200 eW Nuvoton" },
     { PCI_CHIP_MGAG200_EW3_PCI,	"mgag200 eW3 Nuvoton" },
     { PCI_CHIP_MGAG200_EH_PCI,	"mgag200eH" },
+     { PCI_CHIP_MGAG200_EH3_PCI,	"mgag200eH3" },
     { PCI_CHIP_MGAG400,		"mgag400" },
     { PCI_CHIP_MGAG550,		"mgag550" },
     {-1,			NULL }
@@ -517,6 +535,8 @@ static PciChipsets MGAPciChipsets[] = {
 	RES_SHARED_VGA },
     { PCI_CHIP_MGAG200_EH_PCI, PCI_CHIP_MGAG200_EH_PCI,
 	RES_SHARED_VGA },
+    { PCI_CHIP_MGAG200_EH3_PCI, PCI_CHIP_MGAG200_EH3_PCI,
+	RES_SHARED_VGA },
     { PCI_CHIP_MGAG400,	    PCI_CHIP_MGAG400,	RES_SHARED_VGA },
     { PCI_CHIP_MGAG550,	    PCI_CHIP_MGAG550,	RES_SHARED_VGA },
     { -1,			-1,		RES_UNDEFINED }
@@ -963,6 +983,10 @@ MGAProbe(DriverPtr drv, int flags)
                 attrib_no = 16;
                 break;
 
+            case PCI_CHIP_MGAG200_EH3_PCI:
+                attrib_no = 17;
+                break;
+
 	    default:
 		return FALSE;
             }
@@ -1680,7 +1704,8 @@ MGAPreInit(ScrnInfoPtr pScrn, int flags)
     pMga->is_G200EV = (pMga->Chipset == PCI_CHIP_MGAG200_EV_PCI);
     pMga->is_G200WB = (pMga->Chipset == PCI_CHIP_MGAG200_WINBOND_PCI)
 	|| (pMga->Chipset ==  PCI_CHIP_MGAG200_EW3_PCI);
-    pMga->is_G200EH = (pMga->Chipset == PCI_CHIP_MGAG200_EH_PCI);
+    pMga->is_G200EH = (pMga->Chipset == PCI_CHIP_MGAG200_EH_PCI)
+        ||  (pMga->Chipset ==  PCI_CHIP_MGAG200_EH3_PCI);
     pMga->is_G200ER = (pMga->Chipset == PCI_CHIP_MGAG200_ER_PCI);
 
 #ifdef USEMGAHAL
@@ -2224,6 +2249,7 @@ MGAPreInit(ScrnInfoPtr pScrn, int flags)
     case PCI_CHIP_MGAG200_EV_PCI:
     case PCI_CHIP_MGAG200_EH_PCI:
     case PCI_CHIP_MGAG200_ER_PCI:	
+    case PCI_CHIP_MGAG200_EH3_PCI:
     case PCI_CHIP_MGAG400:
     case PCI_CHIP_MGAG550:
 	MGAGSetupFuncs(pScrn);
@@ -2338,6 +2364,7 @@ MGAPreInit(ScrnInfoPtr pScrn, int flags)
 	  case PCI_CHIP_MGAG200_EW3_PCI:
 	  case PCI_CHIP_MGAG200_EV_PCI:
 	  case PCI_CHIP_MGAG200_EH_PCI:
+	  case PCI_CHIP_MGAG200_EH3_PCI:
 	  case PCI_CHIP_MGAG200_ER_PCI:	  
 	    pMga->SrcOrg = 0;
 	    pMga->DstOrg = 0;
@@ -2526,7 +2553,8 @@ MGAPreInit(ScrnInfoPtr pScrn, int flags)
         case PCI_CHIP_MGAG200_WINBOND_PCI:
 	case PCI_CHIP_MGAG200_EW3_PCI:
 	case PCI_CHIP_MGAG200_EV_PCI:
-    case PCI_CHIP_MGAG200_EH_PCI:
+        case PCI_CHIP_MGAG200_EH_PCI:
+        case PCI_CHIP_MGAG200_EH3_PCI:
 	case PCI_CHIP_MGAG200_ER_PCI:	
 	case PCI_CHIP_MGAG400:
 	case PCI_CHIP_MGAG550:
@@ -4528,7 +4556,7 @@ MGAValidMode(int scrnIndex, DisplayModeP
     } else if (pMga->is_G200EV
 	       && (xf86ModeBandwidth(mode, pScrn->bitsPerPixel) > 327)) {
         return MODE_BANDWIDTH;
-    } else if (pMga->is_G200EH
+    } else if (pMga->is_G200EH && (pMga->Chipset != PCI_CHIP_MGAG200_EH3_PCI)
                && (xf86ModeBandwidth(mode, pScrn->bitsPerPixel) > 375)) {
         return MODE_BANDWIDTH;
     } else if (pMga->is_G200ER
Index: xf86-video-mga-1.4.11/src/mga_merge.c
===================================================================
--- xf86-video-mga-1.4.11.orig/src/mga_merge.c
+++ xf86-video-mga-1.4.11/src/mga_merge.c
@@ -365,6 +365,7 @@ MGAPreInitMergedFB(ScrnInfoPtr pScrn1, i
     case PCI_CHIP_MGAG200_EV_PCI:
     case PCI_CHIP_MGAG200_EH_PCI:
     case PCI_CHIP_MGAG200_ER_PCI:
+    case PCI_CHIP_MGAG200_EH3_PCI:
     case PCI_CHIP_MGAG400:
     case PCI_CHIP_MGAG550:
 	MGAGSetupFuncs(pScrn);
@@ -522,6 +523,7 @@ MGAPreInitMergedFB(ScrnInfoPtr pScrn1, i
         case PCI_CHIP_MGAG200_EV_PCI:
         case PCI_CHIP_MGAG200_EH_PCI:
 	case PCI_CHIP_MGAG200_ER_PCI:		
+	case PCI_CHIP_MGAG200_EH3_PCI:
 	case PCI_CHIP_MGAG400:
 	case PCI_CHIP_MGAG550:
 	   maxPitch = 4096;
Index: xf86-video-mga-1.4.11/src/mga_storm.c
===================================================================
--- xf86-video-mga-1.4.11.orig/src/mga_storm.c
+++ xf86-video-mga-1.4.11/src/mga_storm.c
@@ -1136,6 +1136,7 @@ void MGAStormEngineInit( ScrnInfoPtr pSc
     case PCI_CHIP_MGAG200_EV_PCI:
     case PCI_CHIP_MGAG200_EH_PCI:
     case PCI_CHIP_MGAG200_ER_PCI:	
+    case PCI_CHIP_MGAG200_EH3_PCI:
 	pMga->SrcOrg = 0;
 	OUTREG(MGAREG_SRCORG, pMga->realSrcOrg);
 	OUTREG(MGAREG_DSTORG, pMga->DstOrg);
