All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lubomir Rintel <lkundrak@v3.sk>
To: u-boot@lists.denx.de
Subject: [PATCH RFC 06/20] clk: Add driver for Ingenic JZ4730 CGU
Date: Tue, 17 Nov 2020 22:00:04 +0100	[thread overview]
Message-ID: <20201117210018.751469-7-lkundrak@v3.sk> (raw)
In-Reply-To: <20201117210018.751469-1-lkundrak@v3.sk>

A rather minimal driver for that reads back configured clock rates for
hardware we support.

Signed-off-by: Lubomir Rintel <lkundrak@v3.sk>
---
 drivers/clk/Kconfig      |   8 +++
 drivers/clk/Makefile     |   1 +
 drivers/clk/clk-jz4730.c | 121 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 130 insertions(+)
 create mode 100644 drivers/clk/clk-jz4730.c

diff --git a/drivers/clk/Kconfig b/drivers/clk/Kconfig
index 4dfbad7986b..a138c6ebcde 100644
--- a/drivers/clk/Kconfig
+++ b/drivers/clk/Kconfig
@@ -113,6 +113,14 @@ config CLK_HSDK
 	  Enable this to support the cgu clocks on Synopsys ARC HSDK and
 	  Synopsys ARC HSDK-4xD boards
 
+config CLK_JZ4730
+	bool "Enable clock driver for Ingenic JZ4730 cgu"
+	depends on CLK && SOC_JZ4730
+	default y
+	help
+	  This clock driver adds support for clock generators present on
+	  Ingenic JZ4730 SoC.
+
 config CLK_VERSAL
 	bool "Enable clock driver support for Versal"
 	depends on ARCH_VERSAL
diff --git a/drivers/clk/Makefile b/drivers/clk/Makefile
index d1e295ac7c1..daad6333b36 100644
--- a/drivers/clk/Makefile
+++ b/drivers/clk/Makefile
@@ -27,6 +27,7 @@ obj-$(CONFIG_CLK_BOSTON) += clk_boston.o
 obj-$(CONFIG_CLK_EXYNOS) += exynos/
 obj-$(CONFIG_$(SPL_TPL_)CLK_INTEL) += intel/
 obj-$(CONFIG_CLK_HSDK) += clk-hsdk-cgu.o
+obj-$(CONFIG_CLK_JZ4730) += clk-jz4730.o
 obj-$(CONFIG_CLK_K210) += kendryte/
 obj-$(CONFIG_CLK_MPC83XX) += mpc83xx_clk.o
 obj-$(CONFIG_CLK_OCTEON) += clk_octeon.o
diff --git a/drivers/clk/clk-jz4730.c b/drivers/clk/clk-jz4730.c
new file mode 100644
index 00000000000..332b1ea82d6
--- /dev/null
+++ b/drivers/clk/clk-jz4730.c
@@ -0,0 +1,121 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * JZ4730 Clock Generation Unit driver.
+ *
+ * Copyright (C) 2020 Lubomir Rintel <lkundrak@v3.sk>
+ */
+
+#include <dt-bindings/clock/jz4730-cgu.h>
+#include <common.h>
+#include <dm.h>
+#include <clk-uclass.h>
+#include <div64.h>
+#include <linux/bitops.h>
+#include <asm/io.h>
+
+#define CPM_CFCR	0x00
+#define CPM_PLCR1	0x10
+#define CPM_OCR		0x1c
+
+#define CPM_OCR_EXT_RTC_CLK	BIT(8)
+
+#define CPM_PLCR1_PLL1EN	BIT(8)
+
+#define CPM_CFCR_PFR_SHIFT	8
+
+#define CPM_PLCR1_PLL1FD_SHIFT	23
+#define CPM_PLCR1_PLL1FD_MASK	(0x1ff << CPM_PLCR1_PLL1FD_SHIFT)
+#define CPM_PLCR1_PLL1RD_SHIFT	18
+#define CPM_PLCR1_PLL1RD_MASK	(0x1f << CPM_PLCR1_PLL1RD_SHIFT)
+#define CPM_PLCR1_PLL1OD_SHIFT	16
+#define CPM_PLCR1_PLL1OD_MASK	(0x03 << CPM_PLCR1_PLL1OD_SHIFT)
+
+struct jz4730_cgu_priv {
+	void __iomem *base;
+	unsigned long ext_rate;
+};
+
+static inline unsigned int pdiv(u32 cfcr, u32 shift)
+{
+	static unsigned int pdiv_table[] = { 1, 2, 3, 4, 6, 8, 12, 16, 24, 32 };
+
+	return pdiv_table[cfcr >> shift & 0xf];
+}
+
+static inline unsigned long pll_rate(u32 plcr1, unsigned long ext_rate)
+{
+	unsigned long long rate;
+
+	rate = ext_rate;
+	rate *= ((plcr1 & CPM_PLCR1_PLL1FD_MASK) >> CPM_PLCR1_PLL1FD_SHIFT) + 2;
+	do_div(rate, ((plcr1 & CPM_PLCR1_PLL1RD_MASK) >> CPM_PLCR1_PLL1RD_SHIFT) + 2);
+
+	return rate;
+}
+
+static ulong jz4730_cgu_clk_get_rate(struct clk *clk)
+{
+	struct jz4730_cgu_priv *priv = dev_get_priv(clk->dev);
+	u32 cfcr, plcr1, ocr;
+
+	switch (clk->id) {
+	case JZ4730_CLK_PCLK:
+		plcr1 = readl(priv->base + CPM_PLCR1);
+		if (plcr1 & CPM_PLCR1_PLL1EN) {
+			cfcr = readl(priv->base + CPM_CFCR);
+			return pll_rate(plcr1, priv->ext_rate) /
+				pdiv(cfcr, CPM_CFCR_PFR_SHIFT);
+		}
+		return priv->ext_rate;
+	case JZ4730_CLK_WDT:
+		ocr = readl(priv->base + CPM_OCR);
+		if (ocr & CPM_OCR_EXT_RTC_CLK)
+			return -EINVAL;
+		return priv->ext_rate / 128;
+	case JZ4730_CLK_UART0:
+	case JZ4730_CLK_UART1:
+	case JZ4730_CLK_UART2:
+	case JZ4730_CLK_UART3:
+	case JZ4730_CLK_I2C:
+	case JZ4730_CLK_TCU:
+		return priv->ext_rate;
+	default:
+		return -EINVAL;
+	}
+}
+
+static const struct clk_ops jz4730_cgu_clk_ops = {
+	.get_rate = jz4730_cgu_clk_get_rate,
+};
+
+static int jz4730_cgu_clk_probe(struct udevice *dev)
+{
+	struct jz4730_cgu_priv *priv = dev_get_priv(dev);
+	struct clk clk;
+	int ret;
+
+	priv->base = dev_remap_addr(dev);
+	if (!priv->base)
+		return -EINVAL;
+
+	ret = clk_get_by_name(dev, "ext", &clk);
+	if (ret)
+		return ret;
+	priv->ext_rate = clk_get_rate(&clk);
+
+	return 0;
+}
+
+static const struct udevice_id jz4730_cgu_clk_of_match[] = {
+	{ .compatible = "ingenic,jz4730-cgu" },
+	{ },
+};
+
+U_BOOT_DRIVER(jz4730_cgu_clk) = {
+	.name = "jz4730-cgu",
+	.id = UCLASS_CLK,
+	.of_match = jz4730_cgu_clk_of_match,
+	.probe = jz4730_cgu_clk_probe,
+	.priv_auto_alloc_size = sizeof(struct jz4730_cgu_priv),
+	.ops = &jz4730_cgu_clk_ops,
+};
-- 
2.28.0

  parent reply	other threads:[~2020-11-17 21:00 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-17 20:59 [PATCH RFC 00/20] MIPS: Add support for JZ4730 and Skytone Alpha 400 Lubomir Rintel
2020-11-17 20:59 ` [PATCH RFC 01/20] config: Remove CONFIG_SYS_ID_EEPROM Lubomir Rintel
2020-11-17 21:00 ` [PATCH RFC 02/20] mtd: Allow building nand_spl_simple w/o SPL_NAND_ECC Lubomir Rintel
2020-11-17 21:00 ` [PATCH RFC 03/20] cmd/mac: Don't build unless CONFIG_CMD_MAC is enabled Lubomir Rintel
2020-11-17 22:17   ` Daniel Schwierzeck
2020-11-17 21:00 ` [PATCH RFC 04/20] mips: Don't access CP0_EBASE on JZ47XX Lubomir Rintel
2020-11-17 21:00 ` [PATCH RFC 05/20] ns16550: Turn on the UME bit if on ARCH_JZ47XX Lubomir Rintel
2020-11-17 22:29   ` Daniel Schwierzeck
2020-11-18  6:55     ` Lubomir Rintel
2020-11-17 21:00 ` Lubomir Rintel [this message]
2020-11-17 21:00 ` [PATCH RFC 07/20] timer-uclass: Tolerate failure to get clock rate in pre_probe Lubomir Rintel
2020-11-17 21:00 ` [PATCH RFC 08/20] timer: Add Ingenic JZ4730 timer driver Lubomir Rintel
2020-11-17 21:00 ` [PATCH RFC 09/20] mmc: Default to JZ47XX_MMC=y on ARCH_JZ47XX Lubomir Rintel
2020-11-17 21:00 ` [PATCH RFC 10/20] mmc/jz_mmc: Add a JZ4740 compatible string Lubomir Rintel
2020-11-18 13:56   ` Ezequiel Garcia
2020-11-17 21:00 ` [PATCH RFC 11/20] mmc/jz_mmc: Support wp-gpio/cd-gpio Lubomir Rintel
2020-11-17 21:00 ` [PATCH RFC 12/20] pinctrl: Add Ingenic JZ4730 pin control and GPIO driver Lubomir Rintel
2020-11-17 22:39   ` Daniel Schwierzeck
2020-11-17 21:00 ` [PATCH RFC 13/20] nand: Use correct prototype of board_nand_init() with SPL_NAND_SIMPLE Lubomir Rintel
2020-11-17 21:00 ` [PATCH RFC 14/20] nand/raw: Add Ingenic JZ4730 NAND flash driver Lubomir Rintel
2020-11-17 21:00 ` [PATCH RFC 15/20] watchdog: Add Ingenic JZ4730 watchdog timer driver Lubomir Rintel
2020-11-17 21:00 ` [PATCH RFC 16/20] net: Add Ingenic JZ4730 Ethernet driver Lubomir Rintel
2020-11-17 21:00 ` [PATCH RFC 17/20] mips: dts: Add Ingenic JZ4730 Lubomir Rintel
2020-11-17 21:00 ` [PATCH RFC 18/20] mips/mach-jz47xx: Add Ingenic JZ4730 support Lubomir Rintel
2020-11-17 21:00 ` [PATCH RFC 19/20] mips: dts: Add Skytone Alpha 400 Lubomir Rintel
2020-11-17 21:00 ` [PATCH RFC 20/20] board: " Lubomir Rintel

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=20201117210018.751469-7-lkundrak@v3.sk \
    --to=lkundrak@v3.sk \
    --cc=u-boot@lists.denx.de \
    /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.