All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Brace <kevinbrace@gmx.com>
To: dri-devel@lists.freedesktop.org
Cc: Kevin Brace <kevinbrace@bracecomputerlab.com>
Subject: [PATCH 19/28] drm/via: Add via_pll.c
Date: Fri, 24 Jun 2022 15:26:24 -0500	[thread overview]
Message-ID: <20220624202633.3978-20-kevinbrace@gmx.com> (raw)
In-Reply-To: <20220624202633.3978-1-kevinbrace@gmx.com>

From: Kevin Brace <kevinbrace@bracecomputerlab.com>

Signed-off-by: Kevin Brace <kevinbrace@bracecomputerlab.com>
---
 drivers/gpu/drm/via/via_pll.c | 263 ++++++++++++++++++++++++++++++++++
 1 file changed, 263 insertions(+)
 create mode 100644 drivers/gpu/drm/via/via_pll.c

diff --git a/drivers/gpu/drm/via/via_pll.c b/drivers/gpu/drm/via/via_pll.c
new file mode 100644
index 000000000000..ec61d044504d
--- /dev/null
+++ b/drivers/gpu/drm/via/via_pll.c
@@ -0,0 +1,263 @@
+/*
+ * Copyright 2012 James Simmons. All Rights Reserved.
+ * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved.
+ * Copyright 2001-2009 S3 Graphics, Inc. All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sub license,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHOR(S) OR COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Author(s):
+ * James Simmons <jsimmons@infradead.org>
+ */
+
+#include <linux/delay.h>
+#include <linux/pci.h>
+#include <linux/pci_ids.h>
+
+#include "via_drv.h"
+
+
+#define CSR_VCO_UP	600000000
+#define CSR_VCO_DOWN	300000000
+
+#define PLL_DTZ_DEFAULT		(BIT(0) | BIT(1))
+
+#define VIA_CLK_REFERENCE	14318180
+
+struct pll_mrn_value {
+	u32 pll_m;
+	u32 pll_r;
+	u32 pll_n;
+	u32 diff_clk;
+	u32 pll_fout;
+};
+
+/*
+ * This function first gets the best frequency M, R, N value
+ * to program the PLL according to the supplied frequence
+ * passed in. After we get the MRN values the results are
+ * formatted to fit properly into the PLL clock registers.
+ *
+ * PLL registers M, R, N value
+ * [31:16]  DM[7:0]
+ * [15:8 ]  DR[2:0]
+ * [7 :0 ]  DN[6:0]
+ */
+u32 via_get_clk_value(struct drm_device *dev, u32 freq)
+{
+	struct pci_dev *pdev = to_pci_dev(dev->dev);
+	u32 best_pll_n = 2, best_pll_r = 0, best_pll_m = 2, best_clk_diff = freq;
+	u32 pll_fout, pll_fvco, pll_mrn = 0;
+	u32 pll_n, pll_r, pll_m, clk_diff;
+	struct pll_mrn_value pll_tmp[5] = {
+		{ 0, 0, 0, 0, 0 },
+		{ 0, 0, 0, 0, 0 },
+		{ 0, 0, 0, 0, 0 },
+		{ 0, 0, 0, 0, 0 },
+		{ 0, 0, 0, 0, 0 } };
+	int count;
+
+	if ((pdev->device != PCI_DEVICE_ID_VIA_CLE266) &&
+		(pdev->device != PCI_DEVICE_ID_VIA_KM400)) {
+		/* DN[6:0] */
+		for (pll_n = 2; pll_n < 6; pll_n++) {
+			/* DR[2:0] */
+			for (pll_r = 0; pll_r < 6; pll_r++) {
+				/* DM[9:0] */
+				for (pll_m = 2; pll_m < 512; pll_m++) {
+					/* first divide pll_n then multiply
+					 * pll_m. We have to reduce pll_m
+					 * to 512 to get rid of the overflow */
+					pll_fvco = (VIA_CLK_REFERENCE / pll_n) * pll_m;
+					if ((pll_fvco >= CSR_VCO_DOWN) && (pll_fvco <= CSR_VCO_UP)) {
+						pll_fout = pll_fvco >> pll_r;
+						if (pll_fout < freq)
+							clk_diff = freq - pll_fout;
+						else
+							clk_diff = pll_fout - freq;
+
+						/* if frequency (which is the PLL we want
+						 * to set) > 150MHz, the MRN value we
+						 * write in register must < frequency, and
+						 * get MRN value whose M is the largeset */
+						if (freq >= 150000000) {
+							if ((clk_diff <= pll_tmp[0].diff_clk) || pll_tmp[0].pll_fout == 0) {
+								for (count = ARRAY_SIZE(pll_tmp) - 1; count >= 1; count--)
+									pll_tmp[count] = pll_tmp[count - 1];
+
+								pll_tmp[0].pll_m = pll_m;
+								pll_tmp[0].pll_r = pll_r;
+								pll_tmp[0].pll_n = pll_n;
+								pll_tmp[0].diff_clk = clk_diff;
+								pll_tmp[0].pll_fout = pll_fout;
+							}
+						}
+
+						if (clk_diff < best_clk_diff) {
+							best_clk_diff = clk_diff;
+							best_pll_m = pll_m;
+							best_pll_n = pll_n;
+							best_pll_r = pll_r;
+						}
+					} /* if pll_fvco in VCO range */
+				} /* for PLL M */
+			} /* for PLL R */
+		} /* for PLL N */
+
+		/* if frequency(which is the PLL we want to set) > 150MHz,
+		 * the MRN value we write in register must < frequency,
+		 * and get MRN value whose M is the largeset */
+		if (freq > 150000000) {
+			best_pll_m = pll_tmp[0].pll_m;
+			best_pll_r = pll_tmp[0].pll_r;
+			best_pll_n = pll_tmp[0].pll_n;
+		}
+	/* UniChrome IGP (CLE266, KM400(A), KN400, and P4M800 chipsets)
+	 * requires a different formula for calculating the PLL parameters.
+	 * The code was borrowed from OpenChrome DDX device driver UMS
+	 * (User Mode Setting) section, but was modified to not use float type
+	 * variables. */
+	} else {
+		for (pll_r = 0; pll_r < 4; ++pll_r) {
+			for (pll_n = (pll_r == 0) ? 2 : 1; pll_n <= 7; ++pll_n) {
+				for (pll_m = 1; pll_m <= 127; ++pll_m) {
+					pll_fout = VIA_CLK_REFERENCE * pll_m;
+					pll_fout /= (pll_n << pll_r);
+					if (pll_fout < freq)
+						clk_diff = freq - pll_fout;
+					else
+						clk_diff = pll_fout - freq;
+
+					if (clk_diff < best_clk_diff) {
+						best_clk_diff = clk_diff;
+						best_pll_m = pll_m & 0x7F;
+						best_pll_n = pll_n & 0x1F;
+						best_pll_r = pll_r & 0x03;
+					}
+				}
+			}
+		}
+	}
+
+	switch (pdev->device) {
+	case PCI_DEVICE_ID_VIA_CLE266:
+	case PCI_DEVICE_ID_VIA_KM400:
+		/* Clock Synthesizer Value 0[7:6]: DR[1:0]
+		 * Clock Synthesizer Value 0[5:0]: DN[5:0] */
+		pll_mrn = ((best_pll_r & 0x3) << 14 |
+				(best_pll_n & 0x1F) << 8);
+		/* Clock Synthesizer Value 1[6:0]: DM[6:0] */
+		pll_mrn |= (best_pll_m & 0x7F);
+		break;
+	case PCI_DEVICE_ID_VIA_VX875:
+	case PCI_DEVICE_ID_VIA_VX900_VGA:
+		/* Clock Synthesizer Value 0 : DM[7:0] */
+		pll_mrn = (best_pll_m & 0xFF) << 16;
+		/* Clock Synthesizer Value 1[1:0] : DM[9:8]
+		 * Clock Synthesizer Value 1[4:2] : DR[2:0]
+		 * Clock Synthesizer Value 1[7] : DTZ[0] */
+		pll_mrn |= (((PLL_DTZ_DEFAULT & 0x1) << 7) |
+				((best_pll_r & 0x7) << 2) |
+				(((best_pll_m) >> 8) & 0x3)) << 8;
+		/* Clock Synthesizer Value 2[6:0] : DN[6:0]
+		 * Clock Synthesizer Value 2[7] : DTZ[1] */
+		pll_mrn |= (((PLL_DTZ_DEFAULT >> 1) & 0x1) << 7) |
+				((best_pll_n) & 0x7F);
+		break;
+	default:
+		/* Clock Synthesizer Value 0 : DM[7:0] */
+		pll_mrn = ((best_pll_m - 2) & 0xFF) << 16;
+		/* Clock Synthesizer Value 1[1:0] : DM[9:8]
+		 * Clock Synthesizer Value 1[4:2] : DR[2:0]
+		 * Clock Synthesizer Value 1[7] : DTZ[0] */
+		pll_mrn |= (((PLL_DTZ_DEFAULT & 0x1) << 7) |
+				((best_pll_r & 0x7) << 2) |
+				(((best_pll_m - 2) >> 8) & 0x3)) << 8;
+		/* Clock Synthesizer Value 2[6:0] : DN[6:0]
+		 * Clock Synthesizer Value 2[7] : DTZ[1] */
+		pll_mrn |= (((PLL_DTZ_DEFAULT >> 1) & 0x1) << 7) |
+				((best_pll_n - 2) & 0x7F);
+		break;
+	}
+	return pll_mrn;
+}
+
+/* Set VCLK */
+void via_set_vclock(struct drm_crtc *crtc, u32 clk)
+{
+	struct via_crtc *iga = container_of(crtc, struct via_crtc, base);
+	struct drm_device *dev = crtc->dev;
+	struct pci_dev *pdev = to_pci_dev(dev->dev);
+	struct via_drm_priv *dev_priv = to_via_drm_priv(dev);
+	unsigned long max_loop = 50, i = 0;
+
+	if (!iga->index) {
+		/* IGA1 HW Reset Enable */
+		svga_wcrt_mask(VGABASE, 0x17, 0x00, BIT(7));
+
+		/* set clk */
+		if ((pdev->device == PCI_DEVICE_ID_VIA_CLE266) ||
+			(pdev->device == PCI_DEVICE_ID_VIA_KM400)) {
+			vga_wseq(VGABASE, 0x46, (clk & 0xFF00) >> 8);	/* rshift + divisor */
+			vga_wseq(VGABASE, 0x47, (clk & 0x00FF));	/* multiplier */
+		} else {
+			vga_wseq(VGABASE, 0x44, (clk & 0xFF0000) >> 16);
+			vga_wseq(VGABASE, 0x45, (clk & 0x00FF00) >> 8);
+			vga_wseq(VGABASE, 0x46, (clk & 0x0000FF));
+		}
+		/* Fire */
+		svga_wmisc_mask(VGABASE, BIT(3) | BIT(2), BIT(3) | BIT(2));
+
+		/* reset pll */
+		svga_wseq_mask(VGABASE, 0x40, 0x02, 0x02);
+		svga_wseq_mask(VGABASE, 0x40, 0x00, 0x02);
+
+		/* exit hw reset */
+		while ((vga_rseq(VGABASE, 0x3C) & BIT(3)) == 0 && i++ < max_loop)
+			udelay(20);
+
+		/* IGA1 HW Reset Disable */
+		svga_wcrt_mask(VGABASE, 0x17, BIT(7), BIT(7));
+	} else {
+		/* IGA2 HW Reset Enable */
+		svga_wcrt_mask(VGABASE, 0x6A, 0x00, BIT(6));
+
+		/* set clk */
+		if ((pdev->device == PCI_DEVICE_ID_VIA_CLE266) ||
+			(pdev->device == PCI_DEVICE_ID_VIA_KM400)) {
+			vga_wseq(VGABASE, 0x44, (clk & 0xFF00) >> 8);
+			vga_wseq(VGABASE, 0x45, (clk & 0x00FF));
+		} else {
+			vga_wseq(VGABASE, 0x4A, (clk & 0xFF0000) >> 16);
+			vga_wseq(VGABASE, 0x4B, (clk & 0x00FF00) >> 8);
+			vga_wseq(VGABASE, 0x4C, (clk & 0x0000FF));
+		}
+
+		/* reset pll */
+		svga_wseq_mask(VGABASE, 0x40, 0x04, 0x04);
+		svga_wseq_mask(VGABASE, 0x40, 0x00, 0x04);
+
+		/* exit hw reset */
+		while ((vga_rseq(VGABASE, 0x3C) & BIT(2)) == 0 && i++ < max_loop)
+			udelay(20);
+
+		/* IGA2 HW Reset Disble, CR6A[6] = 1 */
+		svga_wcrt_mask(VGABASE, 0x6A, BIT(6), BIT(6));
+	}
+}
--
2.35.1


  parent reply	other threads:[~2022-06-24 20:28 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-24 20:26 [PATCH 00/28] OpenChrome DRM for Linux 5.20 Kevin Brace
2022-06-24 20:26 ` [PATCH 01/28] drm/via: Add via_3d_reg.h Kevin Brace
2022-06-24 20:26 ` [PATCH 02/28] drm/via: Add via_crtc_hw.h Kevin Brace
2022-06-24 20:26 ` [PATCH 03/28] drm/via: Add via_disp_reg.h Kevin Brace
2022-06-24 20:26 ` [PATCH 04/28] drm/via: Add via_drv.h Kevin Brace
2022-06-25  6:51   ` Sam Ravnborg
2022-06-28 14:33     ` Jani Nikula
2022-06-28 14:32   ` Jani Nikula
2022-06-24 20:26 ` [PATCH 05/28] drm/via: Add via_regs.h Kevin Brace
2022-06-24 20:26 ` [PATCH 06/28] drm/via: Add via_crtc.c Kevin Brace
2022-06-24 20:26 ` [PATCH 07/28] drm/via: Add via_crtc_hw.c Kevin Brace
2022-06-24 20:26 ` [PATCH 08/28] drm/via: Add via_cursor.c Kevin Brace
2022-06-25  6:59   ` Sam Ravnborg
2022-06-24 20:26 ` [PATCH 09/28] drm/via: Add via_dac.c Kevin Brace
2022-06-24 20:26 ` [PATCH 10/28] drm/via: Add via_display.c Kevin Brace
2022-06-24 20:26 ` [PATCH 11/28] drm/via: Add via_drv.c Kevin Brace
2022-06-25  7:15   ` Sam Ravnborg
2022-06-24 20:26 ` [PATCH 12/28] drm/via: Add via_encoder.c Kevin Brace
2022-06-24 20:26 ` [PATCH 13/28] drm/via: Add via_hdmi.c Kevin Brace
2022-06-24 20:26 ` [PATCH 14/28] drm/via: Add via_i2c.c Kevin Brace
2022-06-24 20:26 ` [PATCH 15/28] drm/via: Add via_init.c Kevin Brace
2022-06-24 20:26 ` [PATCH 16/28] drm/via: Add via_ioctl.c Kevin Brace
2022-06-24 20:26 ` [PATCH 17/28] drm/via: Add via_lvds.c Kevin Brace
2022-06-24 20:26 ` [PATCH 18/28] drm/via: Add via_object.c Kevin Brace
2022-06-24 20:26 ` Kevin Brace [this message]
2022-06-24 21:15 ` [PATCH 00/28] OpenChrome DRM for Linux 5.20 Sam Ravnborg
2022-06-24 23:14   ` Kevin Brace
2022-06-27  7:37 ` Thomas Zimmermann
2022-06-27 21:32   ` Sam Ravnborg
2022-06-28 12:21     ` Thomas Zimmermann
2022-06-30  8:07       ` Javier Martinez Canillas
2022-06-30  8:19         ` Thomas Zimmermann
2022-06-30  8:27           ` Javier Martinez Canillas
2022-06-30  8:33           ` Thomas Zimmermann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220624202633.3978-20-kevinbrace@gmx.com \
    --to=kevinbrace@gmx.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=kevinbrace@bracecomputerlab.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.